Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20101107133930

Diff

Diff from 1.37 to:

Annotations

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

Annotated File View

ragge
1.37
1 /*      $Id: code.c,v 1.37 2010/11/07 13:39:30 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.34
184                 p = block(NAMENILNILVOID|FTN|(PTR<<TSHIFT), 0sp->sap);
ragge
1.21
185                 p->n_sp = sp;
186                 defid(pEXTERN);
187                 nfree(p);
188         }
189         blevel--;
190
191         p = doacall(spnametree(sp), r);
mickey
1.1
192         ecomp(p);
ragge
1.21
193         
194
195         /* RAX contains destination after memcpy() */
mickey
1.1
196 }
197
198 /*
199  * code for the beginning of a function; a is an array of
200  * indices in symtab for the arguments; n is the number
201  */
202 void
ragge
1.8
203 bfcode(struct symtab **sint cnt)
mickey
1.1
204 {
ragge
1.11
205         union arglist *al;
ragge
1.8
206         struct symtab *sp;
207         NODE *p, *r;
ragge
1.37
208         TWORD t;
ragge
1.8
209         int irnotyp;
mickey
1.1
210
211         /* recalculate the arg offset and create TEMP moves */
ragge
1.8
212         /* Always do this for reg, even if not optimizing, to free arg regs */
ragge
1.9
213         nsse = ngpr = 0;
214         nrsp = ARGINIT;
ragge
1.17
215         if (cftnsp->stype == STRTY+FTN || cftnsp->stype == UNIONTY+FTN) {
216                 sp = cftnsp;
ragge
1.23
217                 if (argtyp(DECREF(sp->stype), sp->sdfsp->sap) == STRMEM) {
218                         r = block(REGNILNILLONG0MKAP(LONG));
ragge
1.21
219                         regno(r) = argregsi[ngpr++];
ragge
1.23
220                         p = tempnode(0r->n_typer->n_dfr->n_ap);
ragge
1.18
221                         stroffset = regno(p);
222                         ecomp(buildtree(ASSIGNpr));
223                 }
ragge
1.17
224         }
225
ragge
1.8
226         for (i = 0i < cnti++) {
227                 sp = s[i];
228
229                 if (sp == NULL)
230                         continue/* XXX when happens this? */
231
ragge
1.23
232                 switch (typ = argtyp(sp->stypesp->sdfsp->sap)) {
ragge
1.8
233                 case INTEGER:
234                 case SSE:
235                         if (typ == SSE)
236                                 rno = XMM0 + nsse++;
237                         else
238                                 rno = argregsi[ngpr++];
ragge
1.23
239                         r = block(REGNILNILsp->stypesp->sdfsp->sap);
ragge
1.8
240                         regno(r) = rno;
ragge
1.23
241                         p = tempnode(0sp->stypesp->sdfsp->sap);
ragge
1.8
242                         sp->soffset = regno(p);
243                         sp->sflags |= STNODE;
244                         ecomp(buildtree(ASSIGNpr));
245                         break;
ragge
1.9
246
ragge
1.30
247                 case SSEMEM:
248                         sp->soffset = nrsp;
249                         nrsp += SZDOUBLE;
250                         if (xtemps) {
251                                 p = tempnode(0sp->stypesp->sdfsp->sap);
252                                 p = buildtree(ASSIGNpnametree(sp));
253                                 sp->soffset = regno(p->n_left);
254                                 sp->sflags |= STNODE;
255                                 ecomp(p);
256                         }
257                         break;
258
ragge
1.9
259                 case INTMEM:
260                         sp->soffset = nrsp;
261                         nrsp += SZLONG;
262                         if (xtemps) {
ragge
1.23
263                                 p = tempnode(0sp->stypesp->sdfsp->sap);
ragge
1.9
264                                 p = buildtree(ASSIGNpnametree(sp));
265                                 sp->soffset = regno(p->n_left);
266                                 sp->sflags |= STNODE;
267                                 ecomp(p);
268                         }
269                         break;
270
ragge
1.17
271                 case STRMEM/* Struct in memory */
272                         sp->soffset = nrsp;
ragge
1.23
273                         nrsp += tsize(sp->stypesp->sdfsp->sap);
ragge
1.17
274                         break;
275
ragge
1.31
276                 case X87/* long double args */
277                         sp->soffset = nrsp;
278                         nrsp += SZLDOUBLE;
279                         break;
280
ragge
1.17
281                 case STRREG/* Struct in register */
282                         /* Allocate space on stack for the struct */
283                         /* For simplicity always fetch two longwords */
284                         autooff += (2*SZLONG);
285
ragge
1.23
286                         r = block(REGNILNILLONG0MKAP(LONG));
ragge
1.17
287                         regno(r) = argregsi[ngpr++];
288                         ecomp(movtomem(r, -autooffFPREG));
289
ragge
1.23
290                         if (tsize(sp->stypesp->sdfsp->sap) > SZLONG) {
291                                 r = block(REGNILNILLONG0MKAP(LONG));
ragge
1.17
292                                 regno(r) = argregsi[ngpr++];
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.23
326                 r = block(REGNILNILLONG0MKAP(LONG));
ragge
1.11
327                 regno(r) = argregsi[i];
ragge
1.14
328                 r = movtomem(r, -RSALONGOFF(i)-autooffFPREG);
ragge
1.23
329                 p = (p == NIL ? r : block(COMOPprINT0MKAP(INT)));
ragge
1.11
330         }
331         for (i = nssei < 8i++) {
ragge
1.23
332                 r = block(REGNILNILDOUBLE0MKAP(DOUBLE));
ragge
1.11
333                 regno(r) = i + XMM0;
ragge
1.14
334                 r = movtomem(r, -RSADBLOFF(i)-autooffFPREG);
ragge
1.23
335                 p = (p == NIL ? r : block(COMOPprINT0MKAP(INT)));
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 /*
348  * by now, the automatics and register variables are allocated
349  */
350 void
351 bccode()
352 {
353         SETOFF(autooffSZINT);
354 }
355
356 /* called just before final exit */
357 /* flag is 1 if errors, 0 if none */
358 void
359 ejobcode(int flag )
360 {
ragge
1.33
361         if (flag)
362                 return;
363
364         /* printout varargs routines if used */
365         if (varneeds & NEED_GPNEXT) {
366                 printf(".text\n.align 4\n.type __pcc_gpnext,@function\n");
367                 printf("__pcc_gpnext:\n");
368                 printf("cmpl $48,(%%rdi)\njae 1f\n");
369                 printf("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
370                 printf("movq (%%rax),%%rax\naddl $8,(%%rdi)\nret\n");
371                 printf("1:movq 8(%%rdi),%%rax\nmovq (%%rax),%%rax\n");
372                 printf("addq $8,8(%%rdi)\nret\n");
373         }
374         if (varneeds & NEED_FPNEXT) {
375                 printf(".text\n.align 4\n.type __pcc_fpnext,@function\n");
376                 printf("__pcc_fpnext:\n");
377                 printf("cmpl $176,4(%%rdi)\njae 1f\n");
378                 printf("movl 4(%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
379                 printf("movsd (%%rax),%%xmm0\naddl $16,4(%%rdi)\nret\n");
380                 printf("1:movq 8(%%rdi),%%rax\nmovsd (%%rax),%%xmm0\n");
381                 printf("addq $8,8(%%rdi)\nret\n");
382         }
383         if (varneeds & NEED_1REGREF) {
384                 printf(".text\n.align 4\n.type __pcc_1regref,@function\n");
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) {
393                 printf(".text\n.align 4\n.type __pcc_2regref,@function\n");
394                 printf("__pcc_2regref:\n");
395                 printf("cmpl $40,(%%rdi)\njae 1f\n");
396                 printf("movl (%%rdi),%%eax\naddq 16(%%rdi),%%rax\n");
397                 printf("addl $16,(%%rdi)\nret\n");
398                 printf("1:movq 8(%%rdi),%%rax\n");
399                 printf("addq $16,8(%%rdi)\nret\n");
400         }
401         if (varneeds & NEED_MEMREF) {
402                 printf(".text\n.align 4\n.type __pcc_memref,@function\n");
403                 printf("__pcc_memref:\n");
404                 printf("movq 8(%%rdi),%%rax\n");
405                 printf("addq %%rsi,8(%%rdi)\nret\n");
406         }
407                 
408
mickey
1.1
409 #define _MKSTR(x) #x
410 #define MKSTR(x) _MKSTR(x)
411 #define OS MKSTR(TARGOS)
412         printf("\t.ident \"PCC: %s (%s)\"\n\t.end\n"PACKAGE_STRINGOS);
413 }
414
ragge
1.11
415 /*
416  * Varargs stuff:
417  * The ABI says that va_list should be declared as this typedef.
418  * We handcraft it here and then just reference it.
419  *
420  * typedef struct {
421  *      unsigned int gp_offset;
422  *      unsigned int fp_offset;
423  *      void *overflow_arg_area;
424  *      void *reg_save_area;
425  * } __builtin_va_list[1];
ragge
1.33
426  *
427  * There are a number of asm routines printed out if varargs are used:
428  *      long __pcc_gpnext(va)   - get a gpreg value
429  *      long __pcc_fpnext(va)   - get a fpreg value
430  *      void *__pcc_1regref(va) - get reference to a onereg struct 
431  *      void *__pcc_2regref(va) - get reference to a tworeg struct 
432  *      void *__pcc_memref(va,sz)       - get reference to a large struct 
ragge
1.11
433  */
ragge
1.13
434
ragge
1.11
435 static char *gp_offset, *fp_offset, *overflow_arg_area, *reg_save_area;
ragge
1.33
436 static char *gpnext, *fpnext, *_1regref, *_2regref, *memref;
ragge
1.13
437
mickey
1.1
438 void
439 bjobcode()
440 {
ragge
1.33
441         struct symtab *sp;
ragge
1.11
442         struct rstack *rp;
443         NODE *p, *q;
444         char *c;
445
446         gp_offset = addname("gp_offset");
447         fp_offset = addname("fp_offset");
448         overflow_arg_area = addname("overflow_arg_area");
449         reg_save_area = addname("reg_save_area");
450
451         rp = bstruct(NULLSTNAMENULL);
ragge
1.23
452         p = block(NAMENILNILUNSIGNED0MKAP(UNSIGNED));
ragge
1.11
453         soumemb(pgp_offset0);
454         soumemb(pfp_offset0);
455         p->n_type = VOID+PTR;
ragge
1.23
456         p->n_ap = MKAP(VOID);
ragge
1.11
457         soumemb(poverflow_arg_area0);
458         soumemb(preg_save_area0);
459         nfree(p);
460         q = dclstruct(rp);
461         c = addname("__builtin_va_list");
ragge
1.23
462         p = block(LBbdty(NAMEc), bcon(1), INT0MKAP(INT));
ragge
1.11
463         p = tymerge(qp);
464         p->n_sp = lookup(c0);
465         defid(pTYPEDEF);
466         nfree(q);
467         nfree(p);
ragge
1.33
468
469         /* for the static varargs functions */
470 #define MKN(vn, rn, tp) \
471         { vn = addname(rn); sp = lookup(vnSNORMAL); \
472           sp->sclass = USTATICsp->stype = tp; }
473
474         MKN(gpnext"__pcc_gpnext"FTN|LONG);
475         MKN(fpnext"__pcc_fpnext"FTN|DOUBLE);
476         MKN(_1regref"__pcc_1regref"FTN|VOID|(PTR<<TSHIFT));
477         MKN(_2regref"__pcc_2regref"FTN|VOID|(PTR<<TSHIFT));
478         MKN(memref"__pcc_memref"FTN|VOID|(PTR<<TSHIFT));
ragge
1.11
479 }
480
481 static NODE *
482 mkstkref(int offTWORD typ)
483 {
484         NODE *p;
485
ragge
1.23
486         p = block(REGNILNILPTR|typ0MKAP(LONG));
ragge
1.11
487         regno(p) = FPREG;
488         return buildtree(PLUSpbcon(off/SZCHAR));
489 }
490
491 NODE *
ragge
1.24
492 amd64_builtin_stdarg_start(NODE *fNODE *aTWORD t)
ragge
1.11
493 {
494         NODE *p, *r;
495
496         /* use the values from the function header */
497         p = a->n_left;
498         r = buildtree(ASSIGNstructref(ccopy(p), STREFreg_save_area),
499             mkstkref(-rsaoffVOID));
500         r = buildtree(COMOPr,
501             buildtree(ASSIGNstructref(ccopy(p), STREFoverflow_arg_area),
ragge
1.14
502             mkstkref(thisrspVOID)));
ragge
1.11
503         r = buildtree(COMOPr,
504             buildtree(ASSIGNstructref(ccopy(p), STREFgp_offset),
ragge
1.14
505             bcon(thisgpr*(SZLONG/SZCHAR))));
ragge
1.11
506         r = buildtree(COMOPr,
507             buildtree(ASSIGNstructref(ccopy(p), STREFfp_offset),
ragge
1.14
508             bcon(thissse*(SZDOUBLE*2/SZCHAR)+48)));
ragge
1.11
509
510         tfree(f);
511         tfree(a);
512         return r;
mickey
1.1
513 }
514
ragge
1.11
515 NODE *
ragge
1.24
516 amd64_builtin_va_arg(NODE *fNODE *aTWORD t)
ragge
1.12
517 {
ragge
1.29
518         NODE *ap, *r, *dp;
ragge
1.12
519
520         ap = a->n_left;
ragge
1.29
521         dp = a->n_right;
ragge
1.33
522         if (dp->n_type <= ULONGLONG || ISPTR(dp->n_type) ||
523             dp->n_type == FLOAT || dp->n_type == DOUBLE) {
ragge
1.12
524                 /* type might be in general register */
ragge
1.33
525                 if (dp->n_type == FLOAT || dp->n_type == DOUBLE) {
526                         f->n_sp = lookup(fpnextSNORMAL);
527                         varneeds |= NEED_FPNEXT;
528                 } else {
529                         f->n_sp = lookup(gpnextSNORMAL);
530                         varneeds |= NEED_GPNEXT;
531                 }
ragge
1.35
532                 f->n_type = f->n_sp->stype = INCREF(dp->n_type) + (FTN-PTR);
533                 f->n_ap = dp->n_ap;
534                 f->n_df = dp->n_df;
ragge
1.33
535                 f = clocal(f);
536                 r = buildtree(CALLfccopy(ap));
537         } else if (ISSOU(dp->n_type)) {
538                 /* put a reference directly to the stack */
539                 int sz = tsize(dp->n_typedp->n_dfdp->n_ap);
540                 int al = talign(dp->n_typedp->n_ap);
541                 if (al < ALLONG)
542                         al = ALLONG;
543                 if (sz <= SZLONG*2 && al == ALLONG) {
544                         if (sz <= SZLONG) {
545                                 f->n_sp = lookup(_1regrefSNORMAL);
546                                 varneeds |= NEED_1REGREF;
547                         } else {
548                                 f->n_sp = lookup(_2regrefSNORMAL);
549                                 varneeds |= NEED_2REGREF;
550                         }
551                         f->n_type = f->n_sp->stype;
552                         f = clocal(f);
553                         r = buildtree(CALLfccopy(ap));
554                         r = ccast(rINCREF(dp->n_type), 0dp->n_dfdp->n_ap);
555                         r = buildtree(UMULrNIL);
556                 } else {
557                         f->n_sp = lookup(memrefSNORMAL);
558                         varneeds |= NEED_MEMREF;
559                         f->n_type = f->n_sp->stype;
560                         f = clocal(f);
561                         SETOFF(szal);
562                         r = buildtree(CALLf,
563                             buildtree(CMccopy(ap), bcon(sz/SZCHAR)));
564                         r = ccast(rINCREF(dp->n_type), 0dp->n_dfdp->n_ap);
565                         r = buildtree(UMULrNIL);
566                 }
ragge
1.12
567         } else {
ragge
1.13
568                 uerror("amd64_builtin_va_arg not supported type");
569                 goto bad;
ragge
1.12
570         }
571         tfree(a);
572         return r;
573 bad:
574         uerror("bad argument to __builtin_va_arg");
575         return bcon(0);
576 }
ragge
1.11
577
578 NODE *
ragge
1.24
579 amd64_builtin_va_end(NODE *fNODE *aTWORD t)
ragge
1.13
580 {
581         tfree(f);
582         tfree(a);
583         return bcon(0); /* nothing */
584 }
ragge
1.11
585
586 NODE *
ragge
1.33
587 amd64_builtin_va_copy(NODE *fNODE *aTWORD t)
588 {
589         tfree(f);
590         f = buildtree(ASSIGNbuildtree(UMULa->n_leftNIL),
591             buildtree(UMULa->n_rightNIL));
592         nfree(a);
593         return f;
594 }
ragge
1.11
595
ragge
1.7
596 static NODE *
597 movtoreg(NODE *pint rno)
mickey
1.1
598 {
599         NODE *r;
600
ragge
1.23
601         r = block(REGNILNILp->n_typep->n_dfp->n_ap);
ragge
1.7
602         regno(r) = rno;
603         return clocal(buildtree(ASSIGNrp));
604 }  
605
ragge
1.9
606 static NODE *
ragge
1.14
607 movtomem(NODE *pint offint reg)
ragge
1.9
608 {
609         struct symtab s;
610         NODE *r, *l;
611
612         s.stype = p->n_type;
ragge
1.25
613         s.squal = 0;
ragge
1.9
614         s.sdf = p->n_df;
ragge
1.23
615         s.sap = p->n_ap;
ragge
1.9
616         s.soffset = off;
ragge
1.11
617         s.sclass = AUTO;
ragge
1.9
618
619         l = block(REGNILNILPTR+STRTY00);
620         l->n_lval = 0;
ragge
1.14
621         regno(l) = reg;
ragge
1.9
622
ragge
1.23
623         r = block(NAMENILNILp->n_typep->n_dfp->n_ap);
ragge
1.9
624         r->n_sp = &s;
625         r = stref(block(STREFlr000));
626
627         return clocal(buildtree(ASSIGNrp));
628 }  
629
ragge
1.7
630
631 /*
632  * AMD64 parameter classification.
633  */
634 static int
ragge
1.23
635 argtyp(TWORD tunion dimfun *dfstruct attr *ap)
ragge
1.7
636 {
637         int cl = 0;
638
ragge
1.9
639         if (t <= ULONG || ISPTR(t)) {
ragge
1.7
640                 cl = ngpr < 6 ? INTEGER : INTMEM;
641         } else if (t == FLOAT || t == DOUBLE) {
ragge
1.8
642                 cl = nsse < 8 ? SSE : SSEMEM;
ragge
1.7
643         } else if (t == LDOUBLE) {
644                 cl = X87/* XXX */
ragge
1.26
645         } else if (t == STRTY || t == UNIONTY) {
ragge
1.32
646                 int sz = tsize(tdfap);
647
648                 if (sz > 2*SZLONG || ((sz+SZLONG)/SZLONG)+ngpr > 6 ||
649                     attr_find(apGCC_ATYP_PACKED) != NULL)
ragge
1.7
650                         cl = STRMEM;
651                 else
ragge
1.17
652                         cl = STRREG;
ragge
1.7
653         } else
654                 cerror("FIXME: classify");
655         return cl;
656 }
657
ragge
1.26
658 static NODE *
ragge
1.7
659 argput(NODE *p)
660 {
661         NODE *q;
ragge
1.17
662         int typrssz;
mickey
1.1
663
ragge
1.26
664         if (p->n_op == CM) {
665                 p->n_left = argput(p->n_left);
666                 p->n_right = argput(p->n_right);
667                 return p;
668         }
669
ragge
1.7
670         /* first arg may be struct return pointer */
671         /* XXX - check if varargs; setup al */
ragge
1.23
672         switch (typ = argtyp(p->n_typep->n_dfp->n_ap)) {
ragge
1.7
673         case INTEGER:
674         case SSE:
675                 if (typ == SSE)
676                         r = XMM0 + nsse++;
677                 else
678                         r = argregsi[ngpr++];
ragge
1.26
679                 p = movtoreg(pr);
ragge
1.7
680                 break;
ragge
1.26
681
ragge
1.7
682         case X87:
ragge
1.25
683                 r = nrsp;
684                 nrsp += SZLDOUBLE;
ragge
1.26
685                 p = movtomem(prSTKREG);
ragge
1.7
686                 break;
ragge
1.9
687
ragge
1.30
688         case SSEMEM:
689                 r = nrsp;
690                 nrsp += SZDOUBLE;
691                 p = movtomem(prSTKREG);
692                 break;
693
ragge
1.9
694         case INTMEM:
695                 r = nrsp;
696                 nrsp += SZLONG;
ragge
1.26
697                 p = movtomem(prSTKREG);
ragge
1.9
698                 break;
699
ragge
1.17
700         case STRREG/* Struct in registers */
701                 /* Cast to long pointer and move to the registers */
ragge
1.27
702                 /* XXX can overrun struct size */
ragge
1.36
703                 /* XXX check carefully for SSE members */
ragge
1.23
704                 ssz = tsize(p->n_typep->n_dfp->n_ap);
ragge
1.17
705
706                 if (ssz <= SZLONG) {
707                         q = cast(p->n_leftLONG+PTR0);
ragge
1.27
708                         nfree(p);
ragge
1.17
709                         q = buildtree(UMULqNIL);
ragge
1.26
710                         p = movtoreg(qargregsi[ngpr++]);
ragge
1.17
711                 } else if (ssz <= SZLONG*2) {
ragge
1.33
712                         NODE *ql, *qr;
713
714                         qr = cast(ccopy(p->n_left), LONG+PTR0);
715                         qr = movtoreg(buildtree(UMULqrNIL),
716                             argregsi[ngpr++]);
717
718                         ql = cast(p->n_leftLONG+PTR0);
719                         ql = buildtree(UMULbuildtree(PLUSqlbcon(1)), NIL);
720                         ql = movtoreg(qlargregsi[ngpr++]);
ragge
1.17
721
ragge
1.27
722                         nfree(p);
ragge
1.33
723                         p = buildtree(CMqlqr);
ragge
1.17
724                 } else
725                         cerror("STRREG");
726                 break;
727
ragge
1.26
728         case STRMEM: {
729                 struct symtab s;
730                 NODE *l, *t;
731
732                 q = buildtree(UMULp->n_leftNIL);
733
734                 s.stype = p->n_type;
735                 s.squal = 0;
736                 s.sdf = p->n_df;
737                 s.sap = p->n_ap;
738                 s.soffset = nrsp;
739                 s.sclass = AUTO;
740
741                 nrsp += tsize(p->n_typep->n_dfp->n_ap);
742
743                 l = block(REGNILNILPTR+STRTY00);
744                 l->n_lval = 0;
745                 regno(l) = STKREG;
746
747                 t = block(NAMENILNILp->n_typep->n_dfp->n_ap);
748                 t->n_sp = &s;
749                 t = stref(block(STREFlt000));
750
751                 t = (buildtree(ASSIGNtq));
752                 nfree(p);
753                 p = t->n_left;
754                 nfree(t);
755                 break;
756                 }
757
ragge
1.7
758         default:
ragge
1.9
759                 cerror("argument %d"typ);
mickey
1.1
760         }
ragge
1.26
761         return p;
ragge
1.7
762 }
mickey
1.1
763
ragge
1.26
764 /*
765  * Sort arglist so that register assignments ends up last.
766  */
767 static int
768 argsort(NODE *p)
769 {
ragge
1.36
770         NODE *q, *r;
ragge
1.26
771         int rv = 0;
772
ragge
1.36
773         if (p->n_op != CM) {
774                 if (p->n_op == ASSIGN && p->n_left->n_op == REG &&
775                     coptype(p->n_right->n_op) != LTYPE) {
776                         q = tempnode(0p->n_typep->n_dfp->n_ap);
777                         r = ccopy(q);
778                         p->n_right = buildtree(COMOP,
779                             buildtree(ASSIGNqp->n_right), r);
780                 }
ragge
1.26
781                 return rv;
ragge
1.36
782         }
ragge
1.33
783         if (p->n_right->n_op == CM) {
784                 /* fixup for small structs in regs */
785                 q = p->n_right->n_left;
786                 p->n_right->n_left = p->n_left;
787                 p->n_left = p->n_right;
788                 p->n_right = q;
789         }
ragge
1.36
790         if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG &&
791             coptype(p->n_right->n_right->n_op) != LTYPE) {
792                 /* move before everything to avoid reg trashing */
793                 q = tempnode(0p->n_right->n_type,
794                     p->n_right->n_dfp->n_right->n_ap);
795                 r = ccopy(q);
796                 p->n_right->n_right = buildtree(COMOP,
797                     buildtree(ASSIGNqp->n_right->n_right), r);
798         }
ragge
1.26
799         if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG) {
800                 if (p->n_left->n_op == CM &&
801                     p->n_left->n_right->n_op == STASG) {
802                         q = p->n_left->n_right;
803                         p->n_left->n_right = p->n_right;
804                         p->n_right = q;
805                         rv = 1;
806                 } else if (p->n_left->n_op == STASG) {
807                         q = p->n_left;
808                         p->n_left = p->n_right;
809                         p->n_right = q;
810                         rv = 1;
811                 }
812         }
813         return rv | argsort(p->n_left);
814 }
mickey
1.1
815
816 /*
817  * Called with a function call with arguments as argument.
818  * This is done early in buildtree() and only done once.
819  * Returns p.
820  */
821 NODE *
822 funcode(NODE *p)
823 {
ragge
1.8
824         NODE *l, *r;
mickey
1.1
825
ragge
1.9
826         nsse = ngpr = nrsp = 0;
ragge
1.17
827         /* Check if hidden arg needed */
828         /* If so, add it in pass2 */
829         if ((l = p->n_left)->n_type == INCREF(FTN)+STRTY ||
830             l->n_type == INCREF(FTN)+UNIONTY) {
ragge
1.23
831                 int ssz = tsize(BTYPE(l->n_type), l->n_dfl->n_ap);
ragge
1.17
832                 if (ssz > 2*SZLONG)
833                         ngpr++;
834         }
ragge
1.26
835
836         /* Convert just regs to assign insn's */
837         p->n_right = argput(p->n_right);
838
839         /* Must sort arglist so that STASG ends up first */
840         /* This avoids registers being clobbered */
841         while (argsort(p->n_right))
842                 ;
ragge
1.22
843         /* Check if there are varargs */
844         if (nsse || l->n_df == NULL || l->n_df->dfun == NULL) {
845                 ; /* Need RAX */
846         } else {
847                 union arglist *al = l->n_df->dfun;
848
849                 for (; al->type != TELLIPSISal++) {
850                         if (al->type == TNULL)
851                                 return p/* No need */
852                         if (BTYPE(al->type) == STRTY ||
853                             BTYPE(al->type) == UNIONTY ||
854                             ISARY(al->type))
855                                 al++;
856                 }
857         }
858
ragge
1.8
859         /* Always emit number of SSE regs used */
860         l = movtoreg(bcon(nsse), RAX);
861         if (p->n_right->n_op != CM) {
ragge
1.23
862                 p->n_right = block(CMlp->n_rightINT0MKAP(INT));
ragge
1.8
863         } else {
864                 for (r = p->n_rightr->n_left->n_op == CMr = r->n_left)
865                         ;
ragge
1.23
866                 r->n_left = block(CMlr->n_leftINT0MKAP(INT));
ragge
1.8
867         }
mickey
1.1
868         return p;
869 }
870
871 /*
872  * return the alignment of field of type t
873  */
874 int
875 fldal(unsigned int t)
876 {
877         uerror("illegal field type");
878         return(ALINT);
879 }
880
881 /* fix up type of field p */
882 void
883 fldty(struct symtab *p)
884 {
885 }
886
887 /*
888  * XXX - fix genswitch.
889  */
890 int
891 mygenswitch(int numTWORD typestruct swents **pint n)
892 {
893         return 0;
894 }
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-11-01 01:32 +0100