Debian Codegraph @

arch/amd64/code.c

raw symbols SCIP
1 /* $Id: code.c,v 1.94 2020/04/10 20:00:08 plunky Exp $ */
2 /*
3 * Copyright (c) 2008 Michael Shalayeff
4 * Copyright (c) 2003 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30
31 # include "pass1.h"
32
33 #ifndef LANG_CXX
34 #undef NIL
35 #define NIL NULL
36 #define NODE P1ND
37 #define nfree p1nfree
38 #define ccopy p1tcopy
39 #define tfree p1tfree
40 #endif
41
42 static int nsse, ngpr, nrsp, rsaoff;
43 static int thissse, thisgpr, thisrsp;
44 enum { NO_CLASS, INTEGER, INTMEM, SSE, SSEMEM, X87,
45 STRREG, STRMEM, STRSSE, STRIF, STRFI, STRX87 };
46 static const int argregsi[] = { RDI, RSI, RDX, RCX, R08, R09 };
47 /*
48 * The Register Save Area looks something like this.
49 * It is put first on stack with fixed offsets.
50 * struct {
51 * long regs[6];
52 * double xmm[8][2]; // 16 byte in width
53 * };
54 */
55 #define RSASZ (6*SZLONG+8*2*SZDOUBLE)
56 #define RSALONGOFF(x) (RSASZ-(x)*SZLONG)
57 #define RSADBLOFF(x) ((8*2*SZDOUBLE)-(x)*SZDOUBLE*2)
58 /* va_list */
59 #define VAARGSZ (SZINT*2+SZPOINT(CHAR)*2)
60 #define VAGPOFF(x) (x)
61 #define VAFPOFF(x) (x-SZINT)
62 #define VAOFA(x) (x-SZINT-SZINT)
63 #define VARSA(x) (x-SZINT-SZINT-SZPOINT(0))
64
65 static int stroffset;
66
67 static int varneeds;
68 #define NEED_1FPREF 001
69 #define NEED_2FPREF 002
70 #define NEED_1REGREF 004
71 #define NEED_2REGREF 010
72 #define NEED_MEMREF 020
73 #define NEED_STRFI 040
74 #define NEED_STRIF 0100
75
76 static int argtyp(TWORD t, union dimfun *df, struct attr *ap);
77 static NODE *movtomem(NODE *p, int off, int reg);
78 static NODE *movtoreg(NODE *p, int rno);
79 void varattrib(char *name, struct attr *sap);
80
81 /*
82 * Print out assembler segment name.
83 */
84 #ifdef MACHOABI
85 void
86 setseg(int seg, char *name)
87 {
88 switch (seg) {
89 case PROG: name = ".text"; break;
90 case DATA:
91 case LDATA: name = ".data"; break;
92 case RDATA: name = ".const"; break;
93 case STRNG: name = ".cstring"; break;
94 case UDATA: break;
95 case CTORS: name = ".mod_init_func"; break;
96 case DTORS: name = ".mod_term_func"; break;
97 default:
98 cerror("unknown seg %d", seg);
99 }
100 printf("\t%s\n", name);
101 }
102
103 #else
104 void
105 setseg(int seg, char *name)
106 {
107 switch (seg) {
108 case PROG: name = ".text"; break;
109 case DATA:
110 case LDATA: name = ".data"; break;
111 case STRNG:
112 case RDATA: name = ".section .rodata"; break;
113 case UDATA: break;
114 case PICLDATA:
115 case PICDATA: name = ".section .data.rel.rw,\"aw\",@progbits"; break;
116 case PICRDATA: name = ".section .data.rel.ro,\"aw\",@progbits"; break;
117 case TLSDATA: name = ".section .tdata,\"awT\",@progbits"; break;
118 case TLSUDATA: name = ".section .tbss,\"awT\",@nobits"; break;
119 case CTORS: name = ".section\t.ctors,\"aw\",@progbits"; break;
120 case DTORS: name = ".section\t.dtors,\"aw\",@progbits"; break;
121 case NMSEG:
122 printf("\t.section %s,\"a%c\",@progbits\n", name,
123 cftnsp ? 'x' : 'w');
124 return;
125 }
126 printf("\t%s\n", name);
127 }
128 #endif
129
130 /*
131 * Define everything needed to print out some data (or text).
132 * This means segment, alignment, visibility, etc.
133 */
134 void
135 defloc(struct symtab *sp)
136 {
137 char *name;
138
139 name = getexname(sp);
140
141 if (sp->sclass == EXTDEF) {
142 printf("\t.globl %s\n", name);
143 #ifndef MACHOABI
144 if (ISFTN(sp->stype)) {
145 printf("\t.type %s,@function\n", name);
146 } else {
147 printf("\t.type %s,@object\n", name);
148 printf("\t.size %s,%d\n", name,
149 (int)tsize(sp->stype, sp->sdf, sp->sap)/SZCHAR);
150 }
151 #endif
152 }
153 if (sp->slevel == 0)
154 printf("%s:\n", name);
155 else
156 printf(LABFMT ":\n", sp->soffset);
157 }
158
159 /*
160 * code for the end of a function
161 * deals with struct return here
162 * The return value is in (or pointed to by) RETREG.
163 */
164 void
165 efcode(void)
166 {
167 struct symtab *sp;
168 extern int gotnr;
169 TWORD t;
170 NODE *p, *r, *l;
171 int typ;
172
173 gotnr = 0; /* new number for next fun */
174 sp = cftnsp;
175 t = DECREF(sp->stype);
176 if (t != STRTY && t != UNIONTY)
177 return;
178
179 /* XXX should have one routine for this */
180 ngpr = nsse = 0;
181 typ = argtyp(t, sp->sdf, sp->sap);
182 if (typ == STRMEM) {
183 r = block(REG, NIL, NIL, INCREF(t), sp->sdf, sp->sap);
184 regno(r) = RAX;
185 r = buildtree(UMUL, r, NIL);
186 l = tempnode(stroffset, INCREF(t), sp->sdf, sp->sap);
187 l = buildtree(UMUL, l, NIL);
188 ecomp(buildtree(ASSIGN, l, r));
189 l = block(REG, NIL, NIL, LONG, 0, 0);
190 regno(l) = RAX;
191 r = tempnode(stroffset, LONG, 0, 0);
192 ecomp(buildtree(ASSIGN, l, r));
193 } else if (typ == STRX87) {
194 p = block(REG, NIL, NIL, INCREF(LDOUBLE), 0, 0);
195 regno(p) = RAX;
196 p = buildtree(UMUL, buildtree(PLUS, p, bcon(1)), NIL);
197 ecomp(movtoreg(p, 041));
198 p = block(REG, NIL, NIL, INCREF(LDOUBLE), 0, 0);
199 regno(p) = RAX;
200 p = buildtree(UMUL, p, NIL);
201 ecomp(movtoreg(p, 040));
202 } else {
203 TWORD t1, t2;
204 int r1, r2;
205 if (typ == STRSSE || typ == STRFI)
206 r1 = XMM0, t1 = DOUBLE;
207 else
208 r1 = RAX, t1 = LONG;
209 if (typ == STRSSE)
210 r2 = XMM1, t2 = DOUBLE;
211 else if (typ == STRFI)
212 r2 = RAX, t2 = LONG;
213 else if (typ == STRIF)
214 r2 = XMM0, t2 = DOUBLE;
215 else /* if (typ == STRREG) */
216 r2 = RDX, t2 = LONG;
217
218 if (tsize(t, sp->sdf, sp->sap) > SZLONG) {
219 p = block(REG, NIL, NIL, INCREF(t2), 0, 0);
220 regno(p) = RAX;
221 p = buildtree(UMUL, buildtree(PLUS, p, bcon(1)), NIL);
222 ecomp(movtoreg(p, r2));
223 }
224 p = block(REG, NIL, NIL, INCREF(t1), 0, 0);
225 regno(p) = RAX;
226 p = buildtree(UMUL, p, NIL);
227 ecomp(movtoreg(p, r1));
228 }
229 }
230
231 /*
232 * code for the beginning of a function; a is an array of
233 * indices in symtab for the arguments; n is the number
234 */
235 void
236 bfcode(struct symtab **s, int cnt)
237 {
238 union arglist *al;
239 struct symtab *sp;
240 NODE *p, *r;
241 TWORD t;
242 int i, rno, typ, ssz;
243
244 /* recalculate the arg offset and create TEMP moves */
245 /* Always do this for reg, even if not optimizing, to free arg regs */
246 nsse = ngpr = 0;
247 nrsp = ARGINIT;
248 if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
249 sp = cftnsp;
250 if (argtyp(DECREF(sp->stype), sp->sdf, sp->sap) == STRMEM) {
251 r = block(REG, NIL, NIL, LONG, 0, 0);
252 regno(r) = argregsi[ngpr++];
253 p = tempnode(0, r->n_type, r->n_df, r->n_ap);
254 stroffset = regno(p);
255 ecomp(buildtree(ASSIGN, p, r));
256 }
257 }
258
259 for (i = 0; i < cnt; i++) {
260 sp = s[i];
261
262 if (sp == NULL)
263 continue; /* XXX when happens this? */
264
265 ssz = (int)tsize(sp->stype, sp->sdf, sp->sap);
266 switch (typ = argtyp(sp->stype, sp->sdf, sp->sap)) {
267 case INTEGER:
268 case SSE:
269 if (typ == SSE)
270 rno = XMM0 + nsse++;
271 else
272 rno = argregsi[ngpr++];
273 r = block(REG, NIL, NIL, sp->stype, sp->sdf, sp->sap);
274 regno(r) = rno;
275 p = tempnode(0, sp->stype, sp->sdf, sp->sap);
276 sp->soffset = regno(p);
277 sp->sflags |= STNODE;
278 ecomp(buildtree(ASSIGN, p, r));
279 break;
280
281 case SSEMEM:
282 sp->soffset = nrsp;
283 nrsp += SZDOUBLE;
284 if (xtemps) {
285 p = tempnode(0, sp->stype, sp->sdf, sp->sap);
286 p = buildtree(ASSIGN, p, nametree(sp));
287 sp->soffset = regno(p->n_left);
288 sp->sflags |= STNODE;
289 ecomp(p);
290 }
291 break;
292
293 case INTMEM:
294 sp->soffset = nrsp;
295 nrsp += SZLONG;
296 if (xtemps) {
297 p = tempnode(0, sp->stype, sp->sdf, sp->sap);
298 p = buildtree(ASSIGN, p, nametree(sp));
299 sp->soffset = regno(p->n_left);
300 sp->sflags |= STNODE;
301 ecomp(p);
302 }
303 break;
304
305 case STRX87:
306 case STRMEM: /* Struct in memory */
307 sp->soffset = nrsp;
308 nrsp += ssz;
309 break;
310
311 case X87: /* long double args */
312 sp->soffset = nrsp;
313 nrsp += SZLDOUBLE;
314 break;
315
316 case STRFI:
317 case STRIF:
318 case STRSSE:
319 case STRREG: /* Struct in register */
320 autooff += (2*SZLONG);
321
322 if (typ == STRSSE || typ == STRFI) {
323 rno = XMM0 + nsse++;
324 t = DOUBLE;
325 } else {
326 rno = argregsi[ngpr++];
327 t = LONG;
328 }
329 r = block(REG, NIL, NIL, t, 0, 0);
330 regno(r) = rno;
331 ecomp(movtomem(r, -autooff, FPREG));
332
333 if (ssz > SZLONG) {
334 if (typ == STRSSE || typ == STRIF) {
335 rno = XMM0 + nsse++;
336 t = DOUBLE;
337 } else {
338 rno = argregsi[ngpr++];
339 t = LONG;
340 }
341 r = block(REG, NIL, NIL, t, 0, 0);
342 regno(r) = rno;
343 ecomp(movtomem(r, -autooff+SZLONG, FPREG));
344 }
345 sp->soffset = -autooff;
346 break;
347
348 default:
349 cerror("bfcode: %d", typ);
350 }
351 }
352
353 /* Check if there are varargs */
354 if (cftnsp->sdf == NULL || cftnsp->sdf->dfun == NULL)
355 return; /* no prototype */
356 al = cftnsp->sdf->dfun;
357
358 for (; al->type != TELLIPSIS; al++) {
359 t = al->type;
360 if (t == TNULL)
361 return;
362 if (ISSOU(BTYPE(t)))
363 al++;
364 for (i = 0; t > BTMASK; t = DECREF(t))
365 if (ISARY(t) || ISFTN(t))
366 i++;
367 if (i)
368 al++;
369 }
370
371 /* fix stack offset */
372 SETOFF(autooff, ALMAX);
373
374 /* Save reg arguments in the reg save area */
375 p = NIL;
376 for (i = ngpr; i < 6; i++) {
377 r = block(REG, NIL, NIL, LONG, 0, 0);
378 regno(r) = argregsi[i];
379 r = movtomem(r, -RSALONGOFF(i)-autooff, FPREG);
380 p = (p == NIL ? r : block(COMOP, p, r, INT, 0, 0));
381 }
382 for (i = nsse; i < 8; i++) {
383 r = block(REG, NIL, NIL, DOUBLE, 0, 0);
384 regno(r) = i + XMM0;
385 r = movtomem(r, -RSADBLOFF(i)-autooff, FPREG);
386 p = (p == NIL ? r : block(COMOP, p, r, INT, 0, 0));
387 }
388 autooff += RSASZ;
389 rsaoff = autooff;
390 thissse = nsse;
391 thisgpr = ngpr;
392 thisrsp = nrsp;
393
394 ecomp(p);
395 }
396
397
398 /* called just before final exit */
399 /* flag is 1 if errors, 0 if none */
400 void
401 ejobcode(int flag)
402 {
403 if (flag)
404 return;
405
406 #ifdef MACHOABI
407 #define PT(x)
408 #else
409 #define PT(x) printf(".type __pcc_" x ",@function\n")
410 #endif
411
412 #define P(x) printf(x "\n")
413 /* printout varargs routines if used */
414 if (varneeds & NEED_STRFI) { /* struct with one float and then int */
415 P(".text\n.align 4");
416 PT("strif");
417 P("__pcc_strif:");
418 P("cmpl $176,4(%%rdi)\njae .Ladd16");
419 P("cmpl $48,(%%rdi)\njae .Ladd16\n");
420 P("movl 4(%%rdi),%%eax\naddq 16(%%rdi),%%rax");
421 P("movq (%%rax),%%rdx\nmovq %%rdx,24(%%rdi)");
422 P("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax");
423 P("movq 16(%%rax),%%rdx\nmovq %%rdx,32(%%rdi)");
424 P("leaq 24(%%rdi),%%rax\nret");
425 }
426 if (varneeds & NEED_STRIF) { /* struct with one int and one float */
427 P(".text\n.align 4");
428 PT("strif");
429 P("__pcc_strif:");
430 P("cmpl $176,4(%%rdi)\njae .Ladd16");
431 P("cmpl $48,(%%rdi)\njae .Ladd16\n");
432 P("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax");
433 P("movq (%%rax),%%rdx\nmovq %%rdx,24(%%rdi)");
434 P("movl 4(%%rdi),%%eax\naddq 16(%%rdi),%%rax");
435 P("movq 16(%%rax),%%rdx\nmovq %%rdx,32(%%rdi)");
436 P("leaq 24(%%rdi),%%rax\nret");
437 }
438 if (varneeds & NEED_2FPREF) { /* struct with two float regs */
439 P(".text\n.align 4");
440 PT("2fpref");
441 P("__pcc_2fpref:");
442 P("cmpl $160,4(%%rdi)\njae .Ladd16");
443 P("movl 4(%%rdi),%%eax\naddq 16(%%rdi),%%rax");
444 P("addl $32,4(%%rdi)");
445 P("movq (%%rax),%%rdx\nmovq %%rdx,24(%%rdi)");
446 P("movq 16(%%rax),%%rdx\nmovq %%rdx,32(%%rdi)");
447 P("leaq 24(%%rdi),%%rax\nret");
448 }
449 if (varneeds & NEED_1FPREF) {
450 printf(".text\n.align 4\n");
451 PT("1fpref");
452 printf("__pcc_1fpref:\n");
453 printf("cmpl $176,4(%%rdi)\njae .Ladd8\n");
454 printf("movl 4(%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
455 printf("addl $16,4(%%rdi)\nret\n");
456 }
457 if (varneeds & NEED_1REGREF) {
458 printf(".text\n.align 4\n");
459 PT("1regref");
460 printf("__pcc_1regref:\n");
461 printf("cmpl $48,(%%rdi)\njae .Ladd8\n");
462 printf("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
463 printf("addl $8,(%%rdi)\nret\n");
464 }
465 if (varneeds & NEED_2REGREF) {
466 printf(".text\n.align 4\n");
467 PT("2regref");
468 printf("__pcc_2regref:\n");
469 printf("cmpl $40,(%%rdi)\njae .Ladd16\n");
470 printf("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
471 printf("addl $16,(%%rdi)\nret\n");
472 }
473 if (varneeds & NEED_MEMREF) {
474 printf(".text\n.align 4\n");
475 PT("memref");
476 printf("__pcc_memref:\n");
477 printf("movq 8(%%rdi),%%rax\n");
478 printf("addq %%rsi,8(%%rdi)\nret\n");
479 }
480
481 if (varneeds & (NEED_1FPREF|NEED_1REGREF)) {
482 P(".Ladd8:");
483 P("movq 8(%%rdi),%%rax");
484 P("addq $8,8(%%rdi)");
485 P("ret");
486 }
487 if (varneeds & (NEED_2FPREF|NEED_2REGREF|NEED_STRFI|NEED_STRIF)) {
488 P(".Ladd16:");
489 P("movq 8(%%rdi),%%rax");
490 P("addq $16,8(%%rdi)");
491 P("ret");
492 }
493
494 #ifdef MACHOABI
495 printf("\t.ident \"PCC: %s\"\n", VERSSTR);
496 #else
497 printf("\t.ident \"PCC: %s\"\n\t.end\n", VERSSTR);
498 #endif
499 }
500
501 /*
502 * Varargs stuff:
503 * The ABI says that va_list should be declared as this typedef.
504 * We handcraft it here and then just reference it.
505 *
506 * typedef struct {
507 * unsigned int gp_offset;
508 * unsigned int fp_offset;
509 * void *overflow_arg_area;
510 * void *reg_save_area;
511 * } __builtin_va_list[1];
512 *
513 * ...actually, we allocate two of them and use the second one as
514 * bounce buffers for floating point structs...
515 *
516 * There are a number of asm routines printed out if varargs are used:
517 * long __pcc_gpnext(va) - get a gpreg value
518 * long __pcc_fpnext(va) - get a fpreg value
519 * void *__pcc_1regref(va) - get reference to a onereg struct
520 * void *__pcc_2regref(va) - get reference to a tworeg struct
521 * void *__pcc_memref(va,sz) - get reference to a large struct
522 */
523
524 static char *gp_offset, *fp_offset, *overflow_arg_area, *reg_save_area;
525 static char *_1fpref, *_2fpref, *_1regref, *_2regref, *memref;
526 static char *strif, *strfi;
527
528 void
529 bjobcode(void)
530 {
531 struct symtab *sp;
532 struct rstack *rp;
533 NODE *p, *q;
534 char *c;
535
536 #if defined(__GNUC__) || defined(__PCC__)
537 /* Be sure that the compiler uses full x87 */
538 /* XXX cross-compiling will fail here */
539 volatile int fcw = 0;
540 __asm("fstcw (%0)" : : "r"(&fcw));
541 fcw |= 0x33f;
542 __asm("fldcw (%0)" : : "r"(&fcw));
543 #endif
544
545 /* amd64 names for some asm constant printouts */
546 astypnames[INT] = astypnames[UNSIGNED] = "\t.long";
547 astypnames[LONG] = astypnames[ULONG] = "\t.quad";
548
549 gp_offset = addname("gp_offset");
550 fp_offset = addname("fp_offset");
551 overflow_arg_area = addname("overflow_arg_area");
552 reg_save_area = addname("reg_save_area");
553
554 rp = bstruct(NULL, STNAME, NULL);
555 p = block(NAME, NIL, NIL, UNSIGNED, 0, 0);
556 soumemb(p, gp_offset, 0);
557 soumemb(p, fp_offset, 0);
558 p->n_type = VOID+PTR;
559 p->n_ap = NULL;
560 soumemb(p, overflow_arg_area, 0);
561 soumemb(p, reg_save_area, 0);
562 nfree(p);
563 q = dclstruct(rp);
564 c = addname("__builtin_va_list");
565 p = block(LB, bdty(NAME, c), bcon(2), INT, 0, 0);
566 p = tymerge(q, p);
567 p->n_sp = lookup(c, 0);
568 defid(p, TYPEDEF);
569 nfree(q);
570 nfree(p);
571
572 #ifdef GCC_COMPAT
573 /*
574 * gcc defines __float128 on amd64. We handcraft a typedef
575 * of long double here to make glibc happy. (size is same).
576 */
577 p = bdty(NAME, c = addname("__float128"));
578 p = tymerge(q = mkty(LDOUBLE, 0, 0), p);
579 p->n_sp = lookup(c, 0);
580 defid(p, TYPEDEF);
581 nfree(q);
582 nfree(p);
583 #endif
584
585 /* for the static varargs functions */
586 #define MKN(vn, rn) \
587 { vn = addname(rn); sp = lookup(vn, SNORMAL); \
588 sp->sclass = USTATIC; sp->stype = FTN|VOID|(PTR<<TSHIFT); }
589
590 MKN(strfi, "__pcc_strfi");
591 MKN(strif, "__pcc_strif");
592 MKN(_1fpref, "__pcc_1fpref");
593 MKN(_2fpref, "__pcc_2fpref");
594 MKN(_1regref, "__pcc_1regref");
595 MKN(_2regref, "__pcc_2regref");
596 MKN(memref, "__pcc_memref");
597 }
598
599 static NODE *
600 mkstkref(int off, TWORD typ)
601 {
602 NODE *p;
603
604 p = block(REG, NIL, NIL, PTR|typ, 0, 0);
605 regno(p) = FPREG;
606 return buildtree(PLUS, p, bcon(off/SZCHAR));
607 }
608
609 NODE *
610 amd64_builtin_stdarg_start(const struct bitable *bt, NODE *a)
611 {
612 NODE *p, *r;
613
614 /* use the values from the function header */
615 p = a->n_left;
616 r = buildtree(ASSIGN, structref(ccopy(p), STREF, reg_save_area),
617 mkstkref(-rsaoff, VOID));
618 r = buildtree(COMOP, r,
619 buildtree(ASSIGN, structref(ccopy(p), STREF, overflow_arg_area),
620 mkstkref(thisrsp, VOID)));
621 r = buildtree(COMOP, r,
622 buildtree(ASSIGN, structref(ccopy(p), STREF, gp_offset),
623 bcon(thisgpr*(SZLONG/SZCHAR))));
624 r = buildtree(COMOP, r,
625 buildtree(ASSIGN, structref(ccopy(p), STREF, fp_offset),
626 bcon(thissse*(SZDOUBLE*2/SZCHAR)+48)));
627
628 tfree(a);
629 return r;
630 }
631
632 static NODE *
633 mkvacall(char *fun, NODE *a, int typ)
634 {
635 NODE *r, *f = block(NAME, NIL, NIL, INT, 0, 0);
636 NODE *ap = a->n_left;
637 NODE *dp = a->n_right;
638 OFFSZ sz = tsize(dp->n_type, dp->n_df, dp->n_ap);
639
640 f->n_sp = lookup(fun, SNORMAL);
641 varneeds |= typ;
642 f->n_type = f->n_sp->stype;
643 f = clocal(f);
644 SETOFF(sz, ALLONG);
645 r = buildtree(CALL, f,
646 buildtree(CM, ccopy(ap), bcon(sz/SZCHAR)));
647 r = ccast(r, INCREF(dp->n_type), 0, dp->n_df, dp->n_ap);
648 r = buildtree(UMUL, r, NIL);
649 return r;
650 }
651
652 NODE *
653 amd64_builtin_va_arg(const struct bitable *bt, NODE *a)
654 {
655 NODE *r, *dp;
656 int typ;
657 OFFSZ sz;
658
659 dp = a->n_right;
660
661 nsse = ngpr = 0;
662 sz = tsize(dp->n_type, dp->n_df, dp->n_ap);
663 switch (typ = argtyp(dp->n_type, dp->n_df, dp->n_ap)) {
664 case INTEGER:
665 r = mkvacall(_1regref, a, NEED_1REGREF);
666 break;
667
668 case SSE:
669 r = mkvacall(_1fpref, a, NEED_1FPREF);
670 break;
671
672 default:
673 cerror("va_arg: bad type %d", typ);
674
675 case X87:
676 case STRX87:
677 case STRMEM: /* stored in memory */
678 r = mkvacall(memref, a, NEED_MEMREF);
679 break;
680
681 case STRREG: /* struct in general regs */
682 if (sz <= SZLONG)
683 r = mkvacall(_1regref, a, NEED_1REGREF);
684 else
685 r = mkvacall(_2regref, a, NEED_2REGREF);
686 break;
687
688 case STRSSE:
689 if (sz <= SZLONG)
690 r = mkvacall(_1fpref, a, NEED_1FPREF);
691 else
692 r = mkvacall(_2fpref, a, NEED_2FPREF);
693 break;
694
695 case STRIF:
696 r = mkvacall(strif, a, NEED_STRIF);
697 break;
698
699 case STRFI:
700 r = mkvacall(strfi, a, NEED_STRFI);
701 break;
702 }
703
704 tfree(a);
705 return r;
706 }
707
708 NODE *
709 amd64_builtin_va_end(const struct bitable *bt, NODE *a)
710 {
711 tfree(a);
712 return bcon(0); /* nothing */
713 }
714
715 NODE *
716 amd64_builtin_va_copy(const struct bitable *bt, NODE *a)
717 {
718 NODE *f;
719
720 f = buildtree(ASSIGN, buildtree(UMUL, a->n_left, NIL),
721 buildtree(UMUL, a->n_right, NIL));
722 nfree(a);
723 return f;
724 }
725
726 static NODE *
727 movtoreg(NODE *p, int rno)
728 {
729 NODE *r;
730
731 r = block(REG, NIL, NIL, p->n_type, p->n_df, p->n_ap);
732 regno(r) = rno;
733 return clocal(buildtree(ASSIGN, r, p));
734 }
735
736 static NODE *
737 movtomem(NODE *p, int off, int reg)
738 {
739 struct symtab s;
740 NODE *r, *l;
741
742 s.stype = p->n_type;
743 s.squal = 0;
744 s.sdf = p->n_df;
745 s.sap = p->n_ap;
746 s.soffset = off;
747 s.sclass = AUTO;
748
749 l = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
750 slval(l, 0);
751 regno(l) = reg;
752
753 r = block(NAME, NIL, NIL, p->n_type, p->n_df, p->n_ap);
754 r->n_sp = &s;
755 r = stref(block(STREF, l, r, 0, 0, 0));
756
757 return clocal(buildtree(ASSIGN, r, p));
758 }
759
760 /*
761 * Check what to do with a struct/union. We traverse down in the struct to
762 * find which types it is and where the struct really should be.
763 * The return vals we may end up with are:
764 * STRREG - The whole struct is saved in general registers.
765 * STRMEM - the struct is saved in memory.
766 * STRSSE - the whole struct is saved in SSE registers.
767 * STRIF - First word of struct is saved in general reg, other SSE.
768 * STRFI - First word of struct is saved in SSE, next in general reg.
769 *
770 * INTEGER, MEMORY, X87, X87UP, X87COMPLEX, SSE, NO_CLASS.
771 *
772 * - If size > 16 bytes or there are packed fields, use memory.
773 * - If any part of an eight-byte should be in a general register,
774 * the eight-byte is stored in a general register
775 * - If the eight-byte only contains float or double, use a SSE register
776 * - Otherwise use memory.
777 *
778 * Arrays must be broken up as separate elements, since the elements
779 * are classified separately. For example;
780 * struct s { short s; float f[3]; } S;
781 * will have the first 64 bits passed in general reg and the second in SSE.
782 *
783 * sp below is a pointer to a member list.
784 * off tells how many bits in that the classification should start.
785 */
786
787 /*
788 * fill in an array of what elements are classified as.
789 * May return:
790 * - NO_CLASS (if more checks needed)
791 * - STRMEM (if known to end up in memory)
792 */
793 #define MAXCLELEM 128
794 static struct {
795 int off;
796 int cl;
797 } cla[MAXCLELEM];
798 static int clp;
799
800 static int fillstr(struct symtab *sp);
801 static int fillun(struct symtab *sp);
802
803 static int
804 flatten(TWORD t, struct symtab *sp)
805 {
806 int cl;
807
808 clp = 0;
809 cl = (t == STRTY ? fillstr(sp) : fillun(sp));
810 return cl;
811 }
812 static int
813 fillstr(struct symtab *sp)
814 {
815 int cl = NO_CLASS;
816 TWORD t;
817
818 for (; sp; sp = sp->snext) {
819 t = sp->stype;
820 while (ISARY(t))
821 t = DECREF(t);
822
823 if (t <= ULONGLONG || ISPTR(t)) {
824 cla[clp].cl = STRREG;
825 cla[clp++].off = (int)sp->soffset;
826 } else if (t <= DOUBLE) {
827 cla[clp].cl = STRSSE;
828 cla[clp++].off = (int)sp->soffset;
829 } else if (t == LDOUBLE) {
830 cl = STRMEM;
831 break;
832 } else { /* struct or union */
833 #ifdef GCC_COMPAT
834 if (attr_find(sp->sap, GCC_ATYP_PACKED)) {
835 cl = STRMEM;
836 break;
837 }
838 #endif
839 if (t == STRTY)
840 cl = fillstr(strmemb(sp->sap));
841 else if (t == UNIONTY)
842 cl = fillun(strmemb(sp->sap));
843 else
844 cerror("fillstr: %d", t);
845 if (cl == STRMEM)
846 break;
847 }
848 }
849 return cl;
850 }
851
852 /* NO_CLASS, STRMEM, STRREG, STRSSE, X87 */
853 static int
854 unmerge(int old, int new)
855 {
856 int cl = old;
857
858 if (new == STRMEM)
859 cl = new;
860 else switch (old) {
861 case NO_CLASS:
862 cl = new;
863 break;
864 case STRREG:
865 case STRMEM:
866 break;
867 case X87:
868 if (new == STRREG)
869 cl = new;
870 else if (new == STRSSE)
871 cl = STRMEM;
872 else
873 cl = X87;
874 break;
875 case STRSSE:
876 if (new == X87)
877 cl = STRMEM;
878 else
879 cl = new;
880 break;
881 }
882 return cl;
883 }
884
885 static int
886 fillun(struct symtab *sp)
887 {
888 int cl = NO_CLASS;
889 TWORD t;
890
891 cla[clp].off = (int)sp->soffset;
892 for (; sp; sp = sp->snext) {
893 t = sp->stype;
894 while (ISARY(t))
895 t = DECREF(t);
896
897 if (t <= ULONGLONG || ISPTR(t)) {
898 cl = unmerge(cl, STRREG);
899 } else if (t <= DOUBLE) {
900 cl = unmerge(cl, STRSSE);
901 } else if (t == LDOUBLE) {
902 cl = unmerge(cl, X87);
903 } else { /* struct or union */
904 #ifdef GCC_COMPAT
905 if (attr_find(sp->sap, GCC_ATYP_PACKED)) {
906 cl = STRMEM;
907 } else
908 #endif
909 { int cl2 = NO_CLASS;
910 if (t == STRTY)
911 cl2 = fillstr(strmemb(sp->sap));
912 else if (t == UNIONTY)
913 cl2 = fillun(strmemb(sp->sap));
914 else
915 cerror("fillstr: %d", t);
916 cl = unmerge(cl, cl2);
917 }
918 }
919 if (cl == STRMEM)
920 break;
921 }
922 if (cl == X87)
923 cl = STRMEM;
924 cla[clp].cl = cl;
925 clp++;
926 return cl;
927 }
928
929
930 /*
931 * Check for long double complex structs.
932 */
933 static int
934 iscplx87(struct symtab *sp)
935 {
936 if (sp->stype == LDOUBLE && sp->snext->stype == LDOUBLE &&
937 sp->snext->snext == NULL)
938 return STRX87;
939 return 0;
940 }
941
942 /*
943 * AMD64 parameter classification.
944 */
945 static int
946 argtyp(TWORD t, union dimfun *df, struct attr *ap)
947 {
948 int i, cl2, cl = 0;
949
950 if (t <= ULONG || ISPTR(t) || t == BOOL) {
951 cl = ngpr < 6 ? INTEGER : INTMEM;
952 } else if (t == FLOAT || t == DOUBLE || t == FIMAG || t == IMAG) {
953 cl = nsse < 8 ? SSE : SSEMEM;
954 } else if (t == LDOUBLE || t == LIMAG) {
955 cl = X87; /* XXX */
956 } else if (t == STRTY || t == UNIONTY) {
957 int sz = (int)tsize(t, df, ap);
958 #ifdef GCC_COMPAT
959 if (attr_find(ap, GCC_ATYP_PACKED)) {
960 cl = STRMEM;
961 } else
962 #endif
963 if (iscplx87(strmemb(ap)) == STRX87) {
964 cl = STRX87;
965 } else if (sz > 2*SZLONG) {
966 cl = STRMEM;
967 } else {
968 if ((cl = flatten(t, strmemb(ap))) == STRMEM)
969 return STRMEM;
970 cl = cl2 = NO_CLASS;
971 for (i = 0; i < clp; i++) {
972 if (cla[i].off < SZLONG) {
973 if (cl == NO_CLASS || cl == STRSSE)
974 cl = cla[i].cl;
975 } else {
976 if (cl2 == NO_CLASS || cl2 == STRSSE)
977 cl2 = cla[i].cl;
978 }
979 }
980 if (cl == STRMEM || cl2 == STRMEM)
981 cl = STRMEM;
982 else if (cl == STRREG && cl2 == STRSSE)
983 cl = STRIF;
984 else if (cl2 == STRREG && cl == STRSSE)
985 cl = STRFI;
986
987 if (cl == STRREG && ngpr > 4)
988 cl = STRMEM;
989 else if (cl == STRSSE && nsse > 6)
990 cl = STRMEM;
991 else if ((cl == STRIF || cl == STRFI) &&
992 (ngpr > 5 || nsse > 7))
993 cl = STRMEM;
994 }
995 } else
996 cerror("FIXME: classify");
997 return cl;
998 }
999
1000 /*
1001 * Do the "hard work" in assigning correct destination for arguments.
1002 * Also convert arguments < INT to inte (default argument promotions).
1003 * XXX - should be dome elsewhere.
1004 */
1005 static NODE *
1006 argput(NODE *p)
1007 {
1008 NODE *q, *ql;
1009 TWORD ty;
1010 int typ, r, ssz, rn;
1011
1012 if (p->n_op == CM) {
1013 p->n_left = argput(p->n_left);
1014 p->n_right = argput(p->n_right);
1015 return p;
1016 }
1017
1018 /* first arg may be struct return pointer */
1019 /* XXX - check if varargs; setup al */
1020 switch (typ = argtyp(p->n_type, p->n_df, p->n_ap)) {
1021 case INTEGER:
1022 case SSE:
1023 if (typ == SSE)
1024 r = XMM0 + nsse++;
1025 else
1026 r = argregsi[ngpr++];
1027 if (p->n_type < INT || p->n_type == BOOL)
1028 p = cast(p, INT, 0);
1029 p = movtoreg(p, r);
1030 break;
1031
1032 case X87:
1033 r = nrsp;
1034 nrsp += SZLDOUBLE;
1035 p = movtomem(p, r, STKREG);
1036 break;
1037
1038 case SSEMEM:
1039 r = nrsp;
1040 nrsp += SZDOUBLE;
1041 p = movtomem(p, r, STKREG);
1042 break;
1043
1044 case INTMEM:
1045 r = nrsp;
1046 nrsp += SZLONG;
1047 if (p->n_type < INT || p->n_type == BOOL)
1048 p = cast(p, INT, 0);
1049 p = movtomem(p, r, STKREG);
1050 break;
1051
1052 case STRFI:
1053 case STRIF:
1054 case STRSSE:
1055 case STRREG: /* Struct in registers */
1056 /* Cast to long/sse pointer and move to the registers */
1057 /* XXX can overrun struct size */
1058 ssz = (int)tsize(p->n_type, p->n_df, p->n_ap);
1059
1060 if (typ == STRSSE || typ == STRFI) {
1061 r = XMM0 + nsse++;
1062 ty = DOUBLE;
1063 } else {
1064 r = argregsi[ngpr++];
1065 ty = LONG;
1066 }
1067
1068 p = nfree(p); /* remove STARG */
1069 p = makety(p, PTR|ty, 0, 0, 0);
1070 ql = tempnode(0, PTR|ty, 0, 0);
1071 rn = regno(ql);
1072 p = buildtree(ASSIGN, ql, p);
1073 ql = tempnode(rn, PTR|ty, 0, 0);
1074 ql = movtoreg(buildtree(UMUL, ql, NIL), r);
1075 p = buildtree(COMOP, p, ql);
1076
1077 if (ssz > SZLONG) {
1078 if (typ == STRSSE || typ == STRIF) {
1079 r = XMM0 + nsse++;
1080 ty = DOUBLE;
1081 } else {
1082 r = argregsi[ngpr++];
1083 ty = LONG;
1084 }
1085
1086 ql = tempnode(rn, PTR|ty, 0, 0);
1087 ql = buildtree(UMUL, buildtree(PLUS, ql, bcon(1)), NIL);
1088 ql = movtoreg(ql, r);
1089
1090 p = buildtree(CM, p, ql);
1091 }
1092 break;
1093
1094 case STRX87:
1095 case STRMEM: {
1096 struct symtab s;
1097 NODE *l, *t;
1098
1099 q = buildtree(UMUL, p->n_left, NIL);
1100
1101 s.stype = p->n_type;
1102 s.squal = 0;
1103 s.sdf = p->n_df;
1104 s.sap = p->n_ap;
1105 s.soffset = nrsp;
1106 s.sclass = AUTO;
1107
1108 nrsp += (int)tsize(p->n_type, p->n_df, p->n_ap);
1109
1110 l = block(REG, NIL, NIL, PTR+STRTY, 0, 0);
1111 slval(l, 0);
1112 regno(l) = STKREG;
1113
1114 t = block(NAME, NIL, NIL, p->n_type, p->n_df, p->n_ap);
1115 t->n_sp = &s;
1116 t = stref(block(STREF, l, t, 0, 0, 0));
1117
1118 t = (buildtree(ASSIGN, t, q));
1119 nfree(p);
1120 p = t->n_left;
1121 nfree(t);
1122 break;
1123 }
1124
1125 default:
1126 cerror("argument %d", typ);
1127 }
1128 return p;
1129 }
1130
1131 /*
1132 * Sort arglist so that register assignments ends up last.
1133 */
1134 static int
1135 argsort(NODE *p)
1136 {
1137 NODE *q, *r;
1138 int rv = 0;
1139
1140 if (p->n_op != CM) {
1141 if (p->n_op == ASSIGN && p->n_left->n_op == REG &&
1142 coptype(p->n_right->n_op) != LTYPE) {
1143 q = tempnode(0, p->n_type, p->n_df, p->n_ap);
1144 r = ccopy(q);
1145 p->n_right = buildtree(COMOP,
1146 buildtree(ASSIGN, q, p->n_right), r);
1147 }
1148 return rv;
1149 }
1150 if (p->n_right->n_op == CM) {
1151 /* fixup for small structs in regs */
1152 q = p->n_right->n_left;
1153 p->n_right->n_left = p->n_left;
1154 p->n_left = p->n_right;
1155 p->n_right = p->n_left->n_right;
1156 p->n_left->n_right = q;
1157 }
1158 if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG &&
1159 coptype(p->n_right->n_right->n_op) != LTYPE) {
1160 /* move before everything to avoid reg trashing */
1161 q = tempnode(0, p->n_right->n_type,
1162 p->n_right->n_df, p->n_right->n_ap);
1163 r = ccopy(q);
1164 p->n_right->n_right = buildtree(COMOP,
1165 buildtree(ASSIGN, q, p->n_right->n_right), r);
1166 }
1167 if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG) {
1168 if (p->n_left->n_op == CM &&
1169 p->n_left->n_right->n_op == STASG) {
1170 q = p->n_left->n_right;
1171 p->n_left->n_right = p->n_right;
1172 p->n_right = q;
1173 rv = 1;
1174 } else if (p->n_left->n_op == STASG) {
1175 q = p->n_left;
1176 p->n_left = p->n_right;
1177 p->n_right = q;
1178 rv = 1;
1179 }
1180 }
1181 return rv | argsort(p->n_left);
1182 }
1183
1184 /*
1185 * Called with a function call with arguments as argument.
1186 * This is done early in buildtree() and only done once.
1187 * Returns p.
1188 */
1189 NODE *
1190 funcode(NODE *p)
1191 {
1192 NODE *l, *r;
1193 TWORD t;
1194 int i;
1195
1196 nsse = ngpr = nrsp = 0;
1197 /* Check if hidden arg needed */
1198 /* If so, add it in pass2 */
1199 if ((l = p->n_left)->n_type == INCREF(FTN)+STRTY ||
1200 l->n_type == INCREF(FTN)+UNIONTY) {
1201 OFFSZ ssz = tsize(BTYPE(l->n_type), l->n_df, l->n_ap);
1202 struct symtab *sp = strmemb(l->n_ap);
1203 if (ssz == 2*SZLDOUBLE && sp->stype == LDOUBLE &&
1204 sp->snext->stype == LDOUBLE)
1205 ; /* long complex struct */
1206 else if (ssz > 2*SZLONG)
1207 ngpr++;
1208 }
1209
1210 /* Convert just regs to assign insn's */
1211 p->n_right = argput(p->n_right);
1212
1213 /* Must sort arglist so that STASG ends up first */
1214 /* This avoids registers being clobbered */
1215 while (argsort(p->n_right))
1216 ;
1217 /* Check if there are varargs */
1218 if (nsse || l->n_df == NULL || l->n_df->dfun == NULL) {
1219 ; /* Need RAX */
1220 } else {
1221 union arglist *al = l->n_df->dfun;
1222
1223 for (; al->type != TELLIPSIS; al++) {
1224 if ((t = al->type) == TNULL)
1225 return p; /* No need */
1226 if (ISSOU(BTYPE(t)))
1227 al++;
1228 for (i = 0; t > BTMASK; t = DECREF(t))
1229 if (ISARY(t) || ISFTN(t))
1230 i++;
1231 if (i)
1232 al++;
1233 }
1234 }
1235
1236 /* Always emit number of SSE regs used */
1237 l = movtoreg(bcon(nsse), RAX);
1238 if (p->n_right->n_op != CM) {
1239 p->n_right = block(CM, l, p->n_right, INT, 0, 0);
1240 } else {
1241 for (r = p->n_right; r->n_left->n_op == CM; r = r->n_left)
1242 ;
1243 r->n_left = block(CM, l, r->n_left, INT, 0, 0);
1244 }
1245 return p;
1246 }
1247
1248 /* fix up type of field p */
1249 void
1250 fldty(struct symtab *p)
1251 {
1252 }
1253
1254 /*
1255 * XXX - fix genswitch.
1256 */
1257 int
1258 mygenswitch(int num, TWORD type, struct swents **p, int n)
1259 {
1260 return 0;
1261 }
1262
1263 /*
1264 * Return return as given by a.
1265 */
1266 NODE *
1267 builtin_return_address(const struct bitable *bt, NODE *a)
1268 {
1269 int nframes;
1270 NODE *f;
1271
1272 nframes = (int)glval(a);
1273 tfree(a);
1274
1275 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
1276 regno(f) = FPREG;
1277
1278 while (nframes--)
1279 f = block(UMUL, f, NIL, PTR+VOID, 0, 0);
1280
1281 f = block(PLUS, f, bcon(8), INCREF(PTR+VOID), 0, 0);
1282 f = buildtree(UMUL, f, NIL);
1283
1284 return f;
1285 }
1286
1287 /*
1288 * Return frame as given by a.
1289 */
1290 NODE *
1291 builtin_frame_address(const struct bitable *bt, NODE *a)
1292 {
1293 int nframes;
1294 NODE *f;
1295
1296 nframes = (int)glval(a);
1297 tfree(a);
1298
1299 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
1300 regno(f) = FPREG;
1301
1302 while (nframes--)
1303 f = block(UMUL, f, NIL, PTR+VOID, 0, 0);
1304
1305 return f;
1306 }
1307
1308 /*
1309 * Return "canonical frame address".
1310 */
1311 NODE *
1312 builtin_cfa(const struct bitable *bt, NODE *a)
1313 {
1314 NODE *f;
1315
1316 f = block(REG, NIL, NIL, PTR+VOID, 0, 0);
1317 regno(f) = FPREG;
1318 return block(PLUS, f, bcon(16), INCREF(PTR+VOID), 0, 0);
1319 }
1320
1321 int codeatyp(NODE *);
1322 int
1323 codeatyp(NODE *p)
1324 {
1325 TWORD t;
1326 int typ;
1327
1328 ngpr = nsse = 0;
1329 t = DECREF(p->n_type);
1330 if (ISSOU(t) == 0) {
1331 p = p->n_left;
1332 t = DECREF(DECREF(p->n_type));
1333 }
1334 if (ISSOU(t) == 0)
1335 cerror("codeatyp");
1336 typ = argtyp(t, p->n_df, p->n_ap);
1337 return typ;
1338 }