Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20101031181452

Diff

Diff from 1.33 to:

Annotations

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

Annotated File View

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