Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110407185015

Diff

Diff from 1.50 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/arch/amd64/code.c

Annotated File View

ragge
1.50
1 /*      $Id: code.c,v 1.50 2011/04/07 18:50:15 ragge Exp $      */
mickey
1.1
2 /*
3  * Copyright (c) 2008 Michael Shalayeff
4  * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
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
ragge
1.11
33 static int nssengprnrsprsaoff;
ragge
1.14
34 static int thisssethisgprthisrsp;
ragge
1.46
35 enum { INTEGER = 1INTMEMSSESSEMEMX87STRREGSTRMEMSTRCPX };
ragge
1.10
36 static const int argregsi[] = { RDIRSIRDXRCXR08R09 };
ragge
1.11
37 /*
38  * The Register Save Area looks something like this.
39  * It is put first on stack with fixed offsets.
40  * struct {
41  *      long regs[6];
42  *      double xmm[8][2]; // 16 byte in width
43  * };
44  */
45 #define RSASZ           (6*SZLONG+8*2*SZDOUBLE)
46 #define RSALONGOFF(x)   (RSASZ-(x)*SZLONG)
47 #define RSADBLOFF(x)    ((8*2*SZDOUBLE)-(x)*SZDOUBLE*2)
48 /* va_list */
49 #define VAARGSZ         (SZINT*2+SZPOINT(CHAR)*2)
50 #define VAGPOFF(x)      (x)
51 #define VAFPOFF(x)      (x-SZINT)
52 #define VAOFA(x)        (x-SZINT-SZINT)
53 #define VARSA(x)        (x-SZINT-SZINT-SZPOINT(0))
mickey
1.1
54
55 int lastloc = -1;
ragge
1.18
56 static int stroffset;
mickey
1.1
57
ragge
1.33
58 static int varneeds;
59 #define NEED_GPNEXT     001
60 #define NEED_FPNEXT     002
61 #define NEED_1REGREF    004
62 #define NEED_2REGREF    010
63 #define NEED_MEMREF     020
64
ragge
1.23
65 static int argtyp(TWORD tunion dimfun *dfstruct attr *ap);
ragge
1.14
66 static NODE *movtomem(NODE *pint offint reg);
ragge
1.18
67 static NODE *movtoreg(NODE *pint rno);
ragge
1.48
68 void varattrib(char *namestruct attr *sap);
ragge
1.8
69
mickey
1.1
70 /*
71  * Define everything needed to print out some data (or text).
72  * This means segment, alignment, visibility, etc.
73  */
74 void
75 defloc(struct symtab *sp)
76 {
77         extern char *nextsect;
78         static char *loctbl[] = { "text""data""section .rodata" };
ragge
1.40
79         extern int tbss;
ragge
1.18
80         char *name;
mickey
1.1
81         TWORD t;
82         int s;
83
84         if (sp == NULL) {
85                 lastloc = -1;
86                 return;
87         }
ragge
1.28
88         if (kflag) {
ragge
1.44
89 #ifdef MACHOABI
90                 loctbl[DATA] = "section .data.rel.rw,\"aw\"";
91                 loctbl[RDATA] = "section .data.rel.ro,\"aw\"";
92 #else
ragge
1.28
93                 loctbl[DATA] = "section .data.rel.rw,\"aw\",@progbits";
94                 loctbl[RDATA] = "section .data.rel.ro,\"aw\",@progbits";
ragge
1.44
95 #endif
ragge
1.28
96         }
mickey
1.1
97         t = sp->stype;
98         s = ISFTN(t) ? PROG : ISCON(cqual(tsp->squal)) ? RDATA : DATA;
ragge
1.18
99         if ((name = sp->soname) == NULL)
100                 name = exname(sp->sname);
ragge
1.40
101
mickey
1.1
102         if (sp->sflags & STLS) {
103                 if (s != DATA)
104                         cerror("non-data symbol in tls section");
ragge
1.40
105                 if (tbss)
106                         nextsect = ".tbss,\"awT\",@nobits";
107                 else
108                         nextsect = ".tdata,\"awT\",@progbits";
109                 tbss = 0;
110                 lastloc = -1;
mickey
1.1
111         }
ragge
1.40
112
ragge
1.48
113         varattrib(namesp->sap);
ragge
1.6
114
mickey
1.1
115         if (nextsect) {
116                 printf("        .section %s\n"nextsect);
117                 nextsect = NULL;
118                 s = -1;
119         } else if (s != lastloc)
120                 printf("        .%s\n"loctbl[s]);
121         lastloc = s;
122         while (ISARY(t))
123                 t = DECREF(t);
ragge
1.23
124         s = ISFTN(t) ? ALINT : talign(tsp->sap);
ragge
1.3
125         if (s > ALCHAR)
126                 printf("        .align %d\n"s/ALCHAR);
ragge
1.48
127         if (sp->sclass == EXTDEF) {
mickey
1.16
128                 printf("\t.globl %s\n"name);
ragge
1.44
129 #ifndef MACHOABI
mickey
1.16
130                 printf("\t.type %s,@%s\n"name,
131                     ISFTN(t)? "function" : "object");
ragge
1.44
132 #endif
mickey
1.16
133         }
mickey
1.1
134         if (sp->slevel == 0)
ragge
1.6
135                 printf("%s:\n"name);
mickey
1.1
136         else
137                 printf(LABFMT ":\n"sp->soffset);
138 }
139
140 /*
ragge
1.48
141  * Print out variable attributes.
142  */
143 void
144 varattrib(char *namestruct attr *sap)
145 {
146         extern char *nextsect;
147         struct attr *ga;
148
149         if ((ga = attr_find(sapGCC_ATYP_SECTION)) != NULL)
150                 nextsect = ga->sarg(0);
151         if ((ga = attr_find(sapGCC_ATYP_WEAK)) != NULL)
152                 printf("        .weak %s\n"name);
153         if (attr_find(sapGCC_ATYP_DESTRUCTOR)) {
154                 printf("\t.section\t.dtors,\"aw\",@progbits\n");
155                 printf("\t.align 8\n\t.quad\t%s\n"name);
156                 lastloc = -1;
157         }
158         if (attr_find(sapGCC_ATYP_CONSTRUCTOR)) {
159                 printf("\t.section\t.ctors,\"aw\",@progbits\n");
160                 printf("\t.align 8\n\t.quad\t%s\n"name);
161                 lastloc = -1;
162         }
163         if ((ga = attr_find(sapGCC_ATYP_VISIBILITY)) &&
164             strcmp(ga->sarg(0), "default"))
165                 printf("\t.%s %s\n"ga->sarg(0), name);
166         if ((ga = attr_find(sapGCC_ATYP_ALIASWEAK))) {
167                 printf("        .weak %s\n"ga->sarg(0));
168                 printf("        .set %s,%s\n"ga->sarg(0), name);
169         }
170 }
171
172 /*
mickey
1.1
173  * code for the end of a function
174  * deals with struct return here
ragge
1.38
175  * The return value is in (or pointed to by) RETREG.
mickey
1.1
176  */
177 void
178 efcode()
179 {
ragge
1.18
180         struct symtab *sp;
mickey
1.1
181         extern int gotnr;
ragge
1.18
182         TWORD t;
ragge
1.21
183         NODE *p, *r, *l;
ragge
1.46
184         int typsszrno;
mickey
1.1
185
186         gotnr = 0;      /* new number for next fun */
ragge
1.18
187         sp = cftnsp;
188         t = DECREF(sp->stype);
ragge
1.21
189         if (t != STRTY && t != UNIONTY)
mickey
1.1
190                 return;
ragge
1.18
191
ragge
1.38
192         /* XXX should have one routine for this */
ragge
1.46
193         if ((typ = argtyp(tsp->sdfsp->sap)) == STRREG || typ == STRCPX) {
ragge
1.38
194                 /* Cast to long pointer and move to the registers */
195                 /* XXX can overrun struct size */
196                 /* XXX check carefully for SSE members */
ragge
1.21
197
ragge
1.38
198                 if ((ssz = tsize(tsp->sdfsp->sap)) > SZLONG*2)
199                         cerror("efcode1");
ragge
1.21
200
ragge
1.46
201                 if (typ == STRCPX) {
202                         t = DOUBLE;
203                         rno = XMM0;
204                 } else {
205                         t = LONG;
206                         rno = RAX;
207                 }
ragge
1.38
208                 if (ssz > SZLONG) {
ragge
1.50
209                         p = block(REGNILNILINCREF(t), 00);
ragge
1.38
210                         regno(p) = RAX;
211                         p = buildtree(UMULbuildtree(PLUSpbcon(1)), NIL);
ragge
1.46
212                         ecomp(movtoreg(prno+1));
ragge
1.38
213                 }
ragge
1.50
214                 p = block(REGNILNILINCREF(t), 00);
ragge
1.38
215                 regno(p) = RAX;
216                 p = buildtree(UMULpNIL);
ragge
1.46
217                 ecomp(movtoreg(prno));
ragge
1.38
218         } else if (typ == STRMEM) {
219                 r = block(REGNILNILINCREF(t), sp->sdfsp->sap);
220                 regno(r) = RAX;
221                 r = buildtree(UMULrNIL);
222                 l = tempnode(stroffsetINCREF(t), sp->sdfsp->sap);
223                 l = buildtree(UMULlNIL);
224                 ecomp(buildtree(ASSIGNlr));
ragge
1.50
225                 l = block(REGNILNILLONG00);
ragge
1.38
226                 regno(l) = RAX;
ragge
1.50
227                 r = tempnode(stroffsetLONG00);
ragge
1.38
228                 ecomp(buildtree(ASSIGNlr));
229         } else
230                 cerror("efcode");
mickey
1.1
231 }
232
233 /*
234  * code for the beginning of a function; a is an array of
235  * indices in symtab for the arguments; n is the number
236  */
237 void
ragge
1.8
238 bfcode(struct symtab **sint cnt)
mickey
1.1
239 {
ragge
1.11
240         union arglist *al;
ragge
1.8
241         struct symtab *sp;
242         NODE *p, *r;
ragge
1.37
243         TWORD t;
ragge
1.8
244         int irnotyp;
mickey
1.1
245
246         /* recalculate the arg offset and create TEMP moves */
ragge
1.8
247         /* Always do this for reg, even if not optimizing, to free arg regs */
ragge
1.9
248         nsse = ngpr = 0;
249         nrsp = ARGINIT;
ragge
1.17
250         if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
251                 sp = cftnsp;
ragge
1.23
252                 if (argtyp(DECREF(sp->stype), sp->sdfsp->sap) == STRMEM) {
ragge
1.50
253                         r = block(REGNILNILLONG00);
ragge
1.21
254                         regno(r) = argregsi[ngpr++];
ragge
1.23
255                         p = tempnode(0r->n_typer->n_dfr->n_ap);
ragge
1.18
256                         stroffset = regno(p);
257                         ecomp(buildtree(ASSIGNpr));
258                 }
ragge
1.17
259         }
260
ragge
1.8
261         for (i = 0i < cnti++) {
262                 sp = s[i];
263
264                 if (sp == NULL)
265                         continue/* XXX when happens this? */
266
ragge
1.23
267                 switch (typ = argtyp(sp->stypesp->sdfsp->sap)) {
ragge
1.8
268                 case INTEGER:
269                 case SSE:
270                         if (typ == SSE)
271                                 rno = XMM0 + nsse++;
272                         else
273                                 rno = argregsi[ngpr++];
ragge
1.23
274                         r = block(REGNILNILsp->stypesp->sdfsp->sap);
ragge
1.8
275                         regno(r) = rno;
ragge
1.23
276                         p = tempnode(0sp->stypesp->sdfsp->sap);
ragge
1.8
277                         sp->soffset = regno(p);
278                         sp->sflags |= STNODE;
279                         ecomp(buildtree(ASSIGNpr));
280                         break;
ragge
1.9
281
ragge
1.30
282                 case SSEMEM:
283                         sp->soffset = nrsp;
284                         nrsp += SZDOUBLE;
285                         if (xtemps) {
286                                 p = tempnode(0sp->stypesp->sdfsp->sap);
287                                 p = buildtree(ASSIGNpnametree(sp));
288                                 sp->soffset = regno(p->n_left);
289                                 sp->sflags |= STNODE;
290                                 ecomp(p);
291                         }
292                         break;
293
ragge
1.9
294                 case INTMEM:
295                         sp->soffset = nrsp;
296                         nrsp += SZLONG;
297                         if (xtemps) {
ragge
1.23
298                                 p = tempnode(0sp->stypesp->sdfsp->sap);
ragge
1.9
299                                 p = buildtree(ASSIGNpnametree(sp));
300                                 sp->soffset = regno(p->n_left);
301                                 sp->sflags |= STNODE;
302                                 ecomp(p);
303                         }
304                         break;
305
ragge
1.17
306                 case STRMEM/* Struct in memory */
307                         sp->soffset = nrsp;
ragge
1.23
308                         nrsp += tsize(sp->stypesp->sdfsp->sap);
ragge
1.17
309                         break;
310
ragge
1.31
311                 case X87/* long double args */
312                         sp->soffset = nrsp;
313                         nrsp += SZLDOUBLE;
314                         break;
315
ragge
1.46
316                 case STRCPX:
ragge
1.17
317                 case STRREG/* Struct in register */
318                         /* Allocate space on stack for the struct */
319                         /* For simplicity always fetch two longwords */
320                         autooff += (2*SZLONG);
321
ragge
1.46
322                         if (typ == STRCPX) {
323                                 t = DOUBLE;
324                                 rno = XMM0 + nsse++;
325                         } else {
326                                 t = LONG;
327                                 rno = argregsi[ngpr++];
328                         }
ragge
1.50
329                         r = block(REGNILNILt00);
ragge
1.46
330                         regno(r) = rno;
ragge
1.17
331                         ecomp(movtomem(r, -autooffFPREG));
332
ragge
1.23
333                         if (tsize(sp->stypesp->sdfsp->sap) > SZLONG) {
ragge
1.50
334                                 r = block(REGNILNILt00);
ragge
1.46
335                                 regno(r) = (typ == STRCPX ?
336                                     XMM0 + nsse++ : argregsi[ngpr++]);
ragge
1.17
337                                 ecomp(movtomem(r, -autooff+SZLONGFPREG));
338                         }
339
340                         sp->soffset = -autooff;
341                         break;
342
ragge
1.8
343                 default:
ragge
1.9
344                         cerror("bfcode: %d"typ);
mickey
1.1
345                 }
346         }
ragge
1.11
347
348         /* Check if there are varargs */
349         if (cftnsp->sdf == NULL || cftnsp->sdf->dfun == NULL)
350                 return/* no prototype */
351         al = cftnsp->sdf->dfun;
ragge
1.15
352
ragge
1.11
353         for (; al->type != TELLIPSISal++) {
ragge
1.37
354                 t = al->type;
355                 if (t == TNULL)
ragge
1.11
356                         return;
ragge
1.37
357                 if (BTYPE(t) == STRTY || BTYPE(t) == UNIONTY)
ragge
1.11
358                         al++;
ragge
1.37
359                 for (; t > BTMASKt = DECREF(t))
360                         if (ISARY(t) || ISFTN(t))
361                                 al++;
ragge
1.11
362         }
363
364         /* fix stack offset */
365         SETOFF(autooffALMAX);
366
367         /* Save reg arguments in the reg save area */
368         p = NIL;
369         for (i = ngpri < 6i++) {
ragge
1.50
370                 r = block(REGNILNILLONG00);
ragge
1.11
371                 regno(r) = argregsi[i];
ragge
1.14
372                 r = movtomem(r, -RSALONGOFF(i)-autooffFPREG);
ragge
1.50
373                 p = (p == NIL ? r : block(COMOPprINT00));
ragge
1.11
374         }
375         for (i = nssei < 8i++) {
ragge
1.50
376                 r = block(REGNILNILDOUBLE00);
ragge
1.11
377                 regno(r) = i + XMM0;
ragge
1.14
378                 r = movtomem(r, -RSADBLOFF(i)-autooffFPREG);
ragge
1.50
379                 p = (p == NIL ? r : block(COMOPprINT00));
ragge
1.11
380         }
381         autooff += RSASZ;
382         rsaoff = autooff;
ragge
1.14
383         thissse = nsse;
384         thisgpr = ngpr;
385         thisrsp = nrsp;
ragge
1.11
386
387         ecomp(p);
mickey
1.1
388 }
389
390
391 /*
392  * by now, the automatics and register variables are allocated
393  */
394 void
395 bccode()
396 {
397         SETOFF(autooffSZINT);
398 }
399
400 /* called just before final exit */
401 /* flag is 1 if errors, 0 if none */
402 void
403 ejobcode(int flag )
404 {
ragge
1.33
405         if (flag)
406                 return;
407
ragge
1.44
408 #ifdef MACHOAPI
409 #define PT(x)
410 #else
411 #define PT(x) printf(".type __pcc_" x ",@function\n")
412 #endif
413
ragge
1.33
414         /* printout varargs routines if used */
415         if (varneeds & NEED_GPNEXT) {
ragge
1.44
416                 printf(".text\n.align 4\n");
417                 PT("gpnext");
ragge
1.33
418                 printf("__pcc_gpnext:\n");
419                 printf("cmpl $48,(%%rdi)\njae 1f\n");
420                 printf("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
421                 printf("movq (%%rax),%%rax\naddl $8,(%%rdi)\nret\n");
422                 printf("1:movq 8(%%rdi),%%rax\nmovq (%%rax),%%rax\n");
423                 printf("addq $8,8(%%rdi)\nret\n");
424         }
425         if (varneeds & NEED_FPNEXT) {
ragge
1.44
426                 printf(".text\n.align 4\n");
427                 PT("fpnext");
ragge
1.33
428                 printf("__pcc_fpnext:\n");
429                 printf("cmpl $176,4(%%rdi)\njae 1f\n");
430                 printf("movl 4(%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
431                 printf("movsd (%%rax),%%xmm0\naddl $16,4(%%rdi)\nret\n");
432                 printf("1:movq 8(%%rdi),%%rax\nmovsd (%%rax),%%xmm0\n");
433                 printf("addq $8,8(%%rdi)\nret\n");
434         }
435         if (varneeds & NEED_1REGREF) {
ragge
1.44
436                 printf(".text\n.align 4\n");
437                 PT("1regref");
ragge
1.33
438                 printf("__pcc_1regref:\n");
439                 printf("cmpl $48,(%%rdi)\njae 1f\n");
440                 printf("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
441                 printf("addl $8,(%%rdi)\nret\n");
442                 printf("1:movq 8(%%rdi),%%rax\n");
443                 printf("addq $8,8(%%rdi)\nret\n");
444         }
445         if (varneeds & NEED_2REGREF) {
ragge
1.44
446                 printf(".text\n.align 4\n");
447                 PT("2regref");
ragge
1.33
448                 printf("__pcc_2regref:\n");
449                 printf("cmpl $40,(%%rdi)\njae 1f\n");
450                 printf("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
451                 printf("addl $16,(%%rdi)\nret\n");
452                 printf("1:movq 8(%%rdi),%%rax\n");
453                 printf("addq $16,8(%%rdi)\nret\n");
454         }
455         if (varneeds & NEED_MEMREF) {
ragge
1.44
456                 printf(".text\n.align 4\n");
457                 PT("memref");
ragge
1.33
458                 printf("__pcc_memref:\n");
459                 printf("movq 8(%%rdi),%%rax\n");
460                 printf("addq %%rsi,8(%%rdi)\nret\n");
461         }
462                 
463
mickey
1.1
464 #define _MKSTR(x) #x
465 #define MKSTR(x) _MKSTR(x)
466 #define OS MKSTR(TARGOS)
ragge
1.44
467 #ifdef MACHOABI
468         printf("\t.ident \"PCC: %s (%s)\"\n"PACKAGE_STRINGOS);
469 #else
mickey
1.1
470         printf("\t.ident \"PCC: %s (%s)\"\n\t.end\n"PACKAGE_STRINGOS);
ragge
1.44
471 #endif
mickey
1.1
472 }
473
ragge
1.11
474 /*
475  * Varargs stuff:
476  * The ABI says that va_list should be declared as this typedef.
477  * We handcraft it here and then just reference it.
478  *
479  * typedef struct {
480  *      unsigned int gp_offset;
481  *      unsigned int fp_offset;
482  *      void *overflow_arg_area;
483  *      void *reg_save_area;
484  * } __builtin_va_list[1];
ragge
1.33
485  *
486  * There are a number of asm routines printed out if varargs are used:
487  *      long __pcc_gpnext(va)   - get a gpreg value
488  *      long __pcc_fpnext(va)   - get a fpreg value
489  *      void *__pcc_1regref(va) - get reference to a onereg struct 
490  *      void *__pcc_2regref(va) - get reference to a tworeg struct 
491  *      void *__pcc_memref(va,sz)       - get reference to a large struct 
ragge
1.11
492  */
ragge
1.13
493
ragge
1.11
494 static char *gp_offset, *fp_offset, *overflow_arg_area, *reg_save_area;
ragge
1.33
495 static char *gpnext, *fpnext, *_1regref, *_2regref, *memref;
ragge
1.13
496
mickey
1.1
497 void
498 bjobcode()
499 {
ragge
1.33
500         struct symtab *sp;
ragge
1.11
501         struct rstack *rp;
502         NODE *p, *q;
503         char *c;
504
505         gp_offset = addname("gp_offset");
506         fp_offset = addname("fp_offset");
507         overflow_arg_area = addname("overflow_arg_area");
508         reg_save_area = addname("reg_save_area");
509
510         rp = bstruct(NULLSTNAMENULL);
ragge
1.50
511         p = block(NAMENILNILUNSIGNED00);
ragge
1.11
512         soumemb(pgp_offset0);
513         soumemb(pfp_offset0);
514         p->n_type = VOID+PTR;
ragge
1.50
515         p->n_ap = NULL;
ragge
1.11
516         soumemb(poverflow_arg_area0);
517         soumemb(preg_save_area0);
518         nfree(p);
519         q = dclstruct(rp);
520         c = addname("__builtin_va_list");
ragge
1.50
521         p = block(LBbdty(NAMEc), bcon(1), INT00);
ragge
1.11
522         p = tymerge(qp);
523         p->n_sp = lookup(c0);
524         defid(pTYPEDEF);
525         nfree(q);
526         nfree(p);
ragge
1.33
527
528         /* for the static varargs functions */
529 #define MKN(vn, rn, tp) \
530         { vn = addname(rn); sp = lookup(vnSNORMAL); \
531           sp->sclass = USTATICsp->stype = tp; }
532
533         MKN(gpnext"__pcc_gpnext"FTN|LONG);
534         MKN(fpnext"__pcc_fpnext"FTN|DOUBLE);
535         MKN(_1regref"__pcc_1regref"FTN|VOID|(PTR<<TSHIFT));
536         MKN(_2regref"__pcc_2regref"FTN|VOID|(PTR<<TSHIFT));
537         MKN(memref"__pcc_memref"FTN|VOID|(PTR<<TSHIFT));
ragge
1.11
538 }
539
540 static NODE *
541 mkstkref(int offTWORD typ)
542 {
543         NODE *p;
544
ragge
1.50
545         p = block(REGNILNILPTR|typ00);
ragge
1.11
546         regno(p) = FPREG;
547         return buildtree(PLUSpbcon(off/SZCHAR));
548 }
549
550 NODE *
ragge
1.24
551 amd64_builtin_stdarg_start(NODE *fNODE *aTWORD t)
ragge
1.11
552 {
553         NODE *p, *r;
554
555         /* use the values from the function header */
556         p = a->n_left;
557         r = buildtree(ASSIGNstructref(ccopy(p), STREFreg_save_area),
558             mkstkref(-rsaoffVOID));
559         r = buildtree(COMOPr,
560             buildtree(ASSIGNstructref(ccopy(p), STREFoverflow_arg_area),
ragge
1.14
561             mkstkref(thisrspVOID)));
ragge
1.11
562         r = buildtree(COMOPr,
563             buildtree(ASSIGNstructref(ccopy(p), STREFgp_offset),
ragge
1.14
564             bcon(thisgpr*(SZLONG/SZCHAR))));
ragge
1.11
565         r = buildtree(COMOPr,
566             buildtree(ASSIGNstructref(ccopy(p), STREFfp_offset),
ragge
1.14
567             bcon(thissse*(SZDOUBLE*2/SZCHAR)+48)));
ragge
1.11
568
569         tfree(f);
570         tfree(a);
571         return r;
mickey
1.1
572 }
573
ragge
1.11
574 NODE *
ragge
1.24
575 amd64_builtin_va_arg(NODE *fNODE *aTWORD t)
ragge
1.12
576 {
ragge
1.29
577         NODE *ap, *r, *dp;
ragge
1.12
578
579         ap = a->n_left;
ragge
1.29
580         dp = a->n_right;
ragge
1.33
581         if (dp->n_type <= ULONGLONG || ISPTR(dp->n_type) ||
582             dp->n_type == FLOAT || dp->n_type == DOUBLE) {
ragge
1.12
583                 /* type might be in general register */
ragge
1.33
584                 if (dp->n_type == FLOAT || dp->n_type == DOUBLE) {
585                         f->n_sp = lookup(fpnextSNORMAL);
586                         varneeds |= NEED_FPNEXT;
587                 } else {
588                         f->n_sp = lookup(gpnextSNORMAL);
589                         varneeds |= NEED_GPNEXT;
590                 }
ragge
1.35
591                 f->n_type = f->n_sp->stype = INCREF(dp->n_type) + (FTN-PTR);
592                 f->n_ap = dp->n_ap;
ragge
1.49
593                 f->n_df = /* dp->n_df */ NULL;
ragge
1.33
594                 f = clocal(f);
595                 r = buildtree(CALLfccopy(ap));
ragge
1.39
596         } else if (ISSOU(dp->n_type) || dp->n_type == LDOUBLE) {
ragge
1.33
597                 /* put a reference directly to the stack */
598                 int sz = tsize(dp->n_typedp->n_dfdp->n_ap);
599                 int al = talign(dp->n_typedp->n_ap);
600                 if (al < ALLONG)
601                         al = ALLONG;
602                 if (sz <= SZLONG*2 && al == ALLONG) {
603                         if (sz <= SZLONG) {
604                                 f->n_sp = lookup(_1regrefSNORMAL);
605                                 varneeds |= NEED_1REGREF;
606                         } else {
607                                 f->n_sp = lookup(_2regrefSNORMAL);
608                                 varneeds |= NEED_2REGREF;
609                         }
610                         f->n_type = f->n_sp->stype;
611                         f = clocal(f);
612                         r = buildtree(CALLfccopy(ap));
613                         r = ccast(rINCREF(dp->n_type), 0dp->n_dfdp->n_ap);
614                         r = buildtree(UMULrNIL);
615                 } else {
616                         f->n_sp = lookup(memrefSNORMAL);
617                         varneeds |= NEED_MEMREF;
618                         f->n_type = f->n_sp->stype;
619                         f = clocal(f);
620                         SETOFF(szal);
621                         r = buildtree(CALLf,
622                             buildtree(CMccopy(ap), bcon(sz/SZCHAR)));
623                         r = ccast(rINCREF(dp->n_type), 0dp->n_dfdp->n_ap);
624                         r = buildtree(UMULrNIL);
625                 }
ragge
1.12
626         } else {
ragge
1.13
627                 uerror("amd64_builtin_va_arg not supported type");
628                 goto bad;
ragge
1.12
629         }
630         tfree(a);
631         return r;
632 bad:
633         uerror("bad argument to __builtin_va_arg");
634         return bcon(0);
635 }
ragge
1.11
636
637 NODE *
ragge
1.24
638 amd64_builtin_va_end(NODE *fNODE *aTWORD t)
ragge
1.13
639 {
640         tfree(f);
641         tfree(a);
642         return bcon(0); /* nothing */
643 }
ragge
1.11
644
645 NODE *
ragge
1.33
646 amd64_builtin_va_copy(NODE *fNODE *aTWORD t)
647 {
648         tfree(f);
649         f = buildtree(ASSIGNbuildtree(UMULa->n_leftNIL),
650             buildtree(UMULa->n_rightNIL));
651         nfree(a);
652         return f;
653 }
ragge
1.11
654
ragge
1.7
655 static NODE *
656 movtoreg(NODE *pint rno)
mickey
1.1
657 {
658         NODE *r;
659
ragge
1.23
660         r = block(REGNILNILp->n_typep->n_dfp->n_ap);
ragge
1.7
661         regno(r) = rno;
662         return clocal(buildtree(ASSIGNrp));
663 }  
664
ragge
1.9
665 static NODE *
ragge
1.14
666 movtomem(NODE *pint offint reg)
ragge
1.9
667 {
668         struct symtab s;
669         NODE *r, *l;
670
671         s.stype = p->n_type;
ragge
1.25
672         s.squal = 0;
ragge
1.9
673         s.sdf = p->n_df;
ragge
1.23
674         s.sap = p->n_ap;
ragge
1.9
675         s.soffset = off;
ragge
1.11
676         s.sclass = AUTO;
ragge
1.9
677
678         l = block(REGNILNILPTR+STRTY00);
679         l->n_lval = 0;
ragge
1.14
680         regno(l) = reg;
ragge
1.9
681
ragge
1.23
682         r = block(NAMENILNILp->n_typep->n_dfp->n_ap);
ragge
1.9
683         r->n_sp = &s;
684         r = stref(block(STREFlr000));
685
686         return clocal(buildtree(ASSIGNrp));
687 }  
688
ragge
1.7
689
690 /*
691  * AMD64 parameter classification.
692  */
693 static int
ragge
1.23
694 argtyp(TWORD tunion dimfun *dfstruct attr *ap)
ragge
1.7
695 {
696         int cl = 0;
697
ragge
1.41
698         if (t <= ULONG || ISPTR(t) || t == BOOL) {
ragge
1.7
699                 cl = ngpr < 6 ? INTEGER : INTMEM;
ragge
1.47
700         } else if (t == FLOAT || t == DOUBLE || t == FIMAG || t == IMAG) {
ragge
1.8
701                 cl = nsse < 8 ? SSE : SSEMEM;
ragge
1.47
702         } else if (t == LDOUBLE || t == LIMAG) {
ragge
1.7
703                 cl = X87/* XXX */
ragge
1.26
704         } else if (t == STRTY || t == UNIONTY) {
ragge
1.32
705                 int sz = tsize(tdfap);
706
ragge
1.46
707                 if (sz <= 2*SZLONG && attr_find(apATTR_COMPLEX) != NULL) {
708                         cl = nsse < 7 ? STRCPX : STRMEM;
709                 } else if (sz > 2*SZLONG || ((sz+SZLONG)/SZLONG)+ngpr > 6 ||
ragge
1.32
710                     attr_find(apGCC_ATYP_PACKED) != NULL)
ragge
1.7
711                         cl = STRMEM;
712                 else
ragge
1.17
713                         cl = STRREG;
ragge
1.7
714         } else
715                 cerror("FIXME: classify");
716         return cl;
717 }
718
ragge
1.42
719 /*
720  * Do the "hard work" in assigning correct destination for arguments.
721  * Also convert arguments < INT to inte (default argument promotions).
722  * XXX - should be dome elsewhere.
723  */
ragge
1.26
724 static NODE *
ragge
1.7
725 argput(NODE *p)
726 {
727         NODE *q;
ragge
1.46
728         TWORD ty;
ragge
1.17
729         int typrssz;
mickey
1.1
730
ragge
1.26
731         if (p->n_op == CM) {
732                 p->n_left = argput(p->n_left);
733                 p->n_right = argput(p->n_right);
734                 return p;
735         }
736
ragge
1.7
737         /* first arg may be struct return pointer */
738         /* XXX - check if varargs; setup al */
ragge
1.23
739         switch (typ = argtyp(p->n_typep->n_dfp->n_ap)) {
ragge
1.7
740         case INTEGER:
741         case SSE:
742                 if (typ == SSE)
743                         r = XMM0 + nsse++;
744                 else
745                         r = argregsi[ngpr++];
ragge
1.43
746                 if (p->n_type < INT || p->n_type == BOOL)
ragge
1.42
747                         p = cast(pINT0);
ragge
1.26
748                 p = movtoreg(pr);
ragge
1.7
749                 break;
ragge
1.26
750
ragge
1.7
751         case X87:
ragge
1.25
752                 r = nrsp;
753                 nrsp += SZLDOUBLE;
ragge
1.26
754                 p = movtomem(prSTKREG);
ragge
1.7
755                 break;
ragge
1.9
756
ragge
1.30
757         case SSEMEM:
758                 r = nrsp;
759                 nrsp += SZDOUBLE;
760                 p = movtomem(prSTKREG);
761                 break;
762
ragge
1.9
763         case INTMEM:
764                 r = nrsp;
765                 nrsp += SZLONG;
ragge
1.26
766                 p = movtomem(prSTKREG);
ragge
1.9
767                 break;
768
ragge
1.46
769         case STRCPX:
ragge
1.17
770         case STRREG/* Struct in registers */
771                 /* Cast to long pointer and move to the registers */
ragge
1.27
772                 /* XXX can overrun struct size */
ragge
1.36
773                 /* XXX check carefully for SSE members */
ragge
1.23
774                 ssz = tsize(p->n_typep->n_dfp->n_ap);
ragge
1.17
775
ragge
1.46
776                 if (typ == STRCPX) {
777                         ty = DOUBLE;
778                         r = XMM0 + nsse++;
779                 } else {
780                         ty = LONG;
781                         r = argregsi[ngpr++];
782                 }
ragge
1.17
783                 if (ssz <= SZLONG) {
ragge
1.46
784                         q = cast(p->n_leftINCREF(ty), 0);
ragge
1.27
785                         nfree(p);
ragge
1.17
786                         q = buildtree(UMULqNIL);
ragge
1.46
787                         p = movtoreg(qr);
ragge
1.17
788                 } else if (ssz <= SZLONG*2) {
ragge
1.33
789                         NODE *ql, *qr;
790
ragge
1.46
791                         qr = cast(ccopy(p->n_left), INCREF(ty), 0);
792                         qr = movtoreg(buildtree(UMULqrNIL), r);
ragge
1.33
793
ragge
1.46
794                         ql = cast(p->n_leftINCREF(ty), 0);
ragge
1.33
795                         ql = buildtree(UMULbuildtree(PLUSqlbcon(1)), NIL);
ragge
1.46
796                         r = (typ == STRCPX ? XMM0 + nsse++ : argregsi[ngpr++]);
797                         ql = movtoreg(qlr);
ragge
1.17
798
ragge
1.27
799                         nfree(p);
ragge
1.33
800                         p = buildtree(CMqlqr);
ragge
1.17
801                 } else
802                         cerror("STRREG");
803                 break;
804
ragge
1.26
805         case STRMEM: {
806                 struct symtab s;
807                 NODE *l, *t;
808
809                 q = buildtree(UMULp->n_leftNIL);
810
811                 s.stype = p->n_type;
812                 s.squal = 0;
813                 s.sdf = p->n_df;
814                 s.sap = p->n_ap;
815                 s.soffset = nrsp;
816                 s.sclass = AUTO;
817
818                 nrsp += tsize(p->n_typep->n_dfp->n_ap);
819
820                 l = block(REGNILNILPTR+STRTY00);
821                 l->n_lval = 0;
822                 regno(l) = STKREG;
823
824                 t = block(NAMENILNILp->n_typep->n_dfp->n_ap);
825                 t->n_sp = &s;
826                 t = stref(block(STREFlt000));
827
828                 t = (buildtree(ASSIGNtq));
829                 nfree(p);
830                 p = t->n_left;
831                 nfree(t);
832                 break;
833                 }
834
ragge
1.7
835         default:
ragge
1.9
836                 cerror("argument %d"typ);
mickey
1.1
837         }
ragge
1.26
838         return p;
ragge
1.7
839 }
mickey
1.1
840
ragge
1.26
841 /*
842  * Sort arglist so that register assignments ends up last.
843  */
844 static int
845 argsort(NODE *p)
846 {
ragge
1.36
847         NODE *q, *r;
ragge
1.26
848         int rv = 0;
849
ragge
1.36
850         if (p->n_op != CM) {
851                 if (p->n_op == ASSIGN && p->n_left->n_op == REG &&
852                     coptype(p->n_right->n_op) != LTYPE) {
853                         q = tempnode(0p->n_typep->n_dfp->n_ap);
854                         r = ccopy(q);
855                         p->n_right = buildtree(COMOP,
856                             buildtree(ASSIGNqp->n_right), r);
857                 }
ragge
1.26
858                 return rv;
ragge
1.36
859         }
ragge
1.33
860         if (p->n_right->n_op == CM) {
861                 /* fixup for small structs in regs */
862                 q = p->n_right->n_left;
863                 p->n_right->n_left = p->n_left;
864                 p->n_left = p->n_right;
865                 p->n_right = q;
866         }
ragge
1.36
867         if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG &&
868             coptype(p->n_right->n_right->n_op) != LTYPE) {
869                 /* move before everything to avoid reg trashing */
870                 q = tempnode(0p->n_right->n_type,
871                     p->n_right->n_dfp->n_right->n_ap);
872                 r = ccopy(q);
873                 p->n_right->n_right = buildtree(COMOP,
874                     buildtree(ASSIGNqp->n_right->n_right), r);
875         }
ragge
1.26
876         if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG) {
877                 if (p->n_left->n_op == CM &&
878                     p->n_left->n_right->n_op == STASG) {
879                         q = p->n_left->n_right;
880                         p->n_left->n_right = p->n_right;
881                         p->n_right = q;
882                         rv = 1;
883                 } else if (p->n_left->n_op == STASG) {
884                         q = p->n_left;
885                         p->n_left = p->n_right;
886                         p->n_right = q;
887                         rv = 1;
888                 }
889         }
890         return rv | argsort(p->n_left);
891 }
mickey
1.1
892
893 /*
894  * Called with a function call with arguments as argument.
895  * This is done early in buildtree() and only done once.
896  * Returns p.
897  */
898 NODE *
899 funcode(NODE *p)
900 {
ragge
1.8
901         NODE *l, *r;
ragge
1.45
902         TWORD t;
mickey
1.1
903
ragge
1.9
904         nsse = ngpr = nrsp = 0;
ragge
1.17
905         /* Check if hidden arg needed */
906         /* If so, add it in pass2 */
907         if ((l = p->n_left)->n_type == INCREF(FTN)+STRTY ||
908             l->n_type == INCREF(FTN)+UNIONTY) {
ragge
1.23
909                 int ssz = tsize(BTYPE(l->n_type), l->n_dfl->n_ap);
ragge
1.17
910                 if (ssz > 2*SZLONG)
911                         ngpr++;
912         }
ragge
1.26
913
914         /* Convert just regs to assign insn's */
915         p->n_right = argput(p->n_right);
916
917         /* Must sort arglist so that STASG ends up first */
918         /* This avoids registers being clobbered */
919         while (argsort(p->n_right))
920                 ;
ragge
1.22
921         /* Check if there are varargs */
922         if (nsse || l->n_df == NULL || l->n_df->dfun == NULL) {
923                 ; /* Need RAX */
924         } else {
925                 union arglist *al = l->n_df->dfun;
926
927                 for (; al->type != TELLIPSISal++) {
ragge
1.45
928                         if ((t = al->type) == TNULL)
ragge
1.22
929                                 return p/* No need */
ragge
1.45
930                         if (BTYPE(t) == STRTY || BTYPE(t) == UNIONTY)
ragge
1.22
931                                 al++;
ragge
1.45
932                         for (; t > BTMASKt = DECREF(t))
933                                 if (ISARY(t) || ISFTN(t))
934                                         al++;
ragge
1.22
935                 }
936         }
937
ragge
1.8
938         /* Always emit number of SSE regs used */
939         l = movtoreg(bcon(nsse), RAX);
940         if (p->n_right->n_op != CM) {
ragge
1.50
941                 p->n_right = block(CMlp->n_rightINT00);
ragge
1.8
942         } else {
943                 for (r = p->n_rightr->n_left->n_op == CMr = r->n_left)
944                         ;
ragge
1.50
945                 r->n_left = block(CMlr->n_leftINT00);
ragge
1.8
946         }
mickey
1.1
<