Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20101018173708

Diff

Diff from 1.30 to:

Annotations

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

Annotated File View

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