Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20110603153400

Diff

Diff from 1.55 to:

Annotations

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

Annotated File View

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