Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110803191542

Diff

Diff from 1.60 to:

Annotations

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

Annotated File View

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