Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20101104211205

Diff

Diff from 1.36 to:

Annotations

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

Annotated File View

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