Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20101016123806

Diff

Diff from 1.28 to:

Annotations

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

Annotated File View

ragge
1.28
1 /*      $Id: code.c,v 1.28 2010/10/16 12:38:06 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
242                 case INTMEM:
243                         sp->soffset = nrsp;
244                         nrsp += SZLONG;
245                         if (xtemps) {
ragge
1.23
246                                 p = tempnode(0sp->stypesp->sdfsp->sap);
ragge
1.9
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.17
254                 case STRMEM/* Struct in memory */
255                         sp->soffset = nrsp;
ragge
1.23
256                         nrsp += tsize(sp->stypesp->sdfsp->sap);
ragge
1.17
257                         break;
258
259                 case STRREG/* Struct in register */
260                         /* Allocate space on stack for the struct */
261                         /* For simplicity always fetch two longwords */
262                         autooff += (2*SZLONG);
263
ragge
1.23
264                         r = block(REGNILNILLONG0MKAP(LONG));
ragge
1.17
265                         regno(r) = argregsi[ngpr++];
266                         ecomp(movtomem(r, -autooffFPREG));
267
ragge
1.23
268                         if (tsize(sp->stypesp->sdfsp->sap) > SZLONG) {
269                                 r = block(REGNILNILLONG0MKAP(LONG));
ragge
1.17
270                                 regno(r) = argregsi[ngpr++];
271                                 ecomp(movtomem(r, -autooff+SZLONGFPREG));
272                         }
273
274                         sp->soffset = -autooff;
275                         break;
276
ragge
1.8
277                 default:
ragge
1.9
278                         cerror("bfcode: %d"typ);
mickey
1.1
279                 }
280         }
ragge
1.11
281
282         /* Check if there are varargs */
283         if (cftnsp->sdf == NULL || cftnsp->sdf->dfun == NULL)
284                 return/* no prototype */
285         al = cftnsp->sdf->dfun;
ragge
1.15
286
ragge
1.11
287         for (; al->type != TELLIPSISal++) {
288                 if (al->type == TNULL)
289                         return;
ragge
1.15
290                 if (BTYPE(al->type) == STRTY || BTYPE(al->type) == UNIONTY ||
291                     ISARY(al->type))
ragge
1.11
292                         al++;
293         }
294
295         /* fix stack offset */
296         SETOFF(autooffALMAX);
297
298         /* Save reg arguments in the reg save area */
299         p = NIL;
300         for (i = ngpri < 6i++) {
ragge
1.23
301                 r = block(REGNILNILLONG0MKAP(LONG));
ragge
1.11
302                 regno(r) = argregsi[i];
ragge
1.14
303                 r = movtomem(r, -RSALONGOFF(i)-autooffFPREG);
ragge
1.23
304                 p = (p == NIL ? r : block(COMOPprINT0MKAP(INT)));
ragge
1.11
305         }
306         for (i = nssei < 8i++) {
ragge
1.23
307                 r = block(REGNILNILDOUBLE0MKAP(DOUBLE));
ragge
1.11
308                 regno(r) = i + XMM0;
ragge
1.14
309                 r = movtomem(r, -RSADBLOFF(i)-autooffFPREG);
ragge
1.23
310                 p = (p == NIL ? r : block(COMOPprINT0MKAP(INT)));
ragge
1.11
311         }
312         autooff += RSASZ;
313         rsaoff = autooff;
ragge
1.14
314         thissse = nsse;
315         thisgpr = ngpr;
316         thisrsp = nrsp;
ragge
1.11
317
318         ecomp(p);
mickey
1.1
319 }
320
321
322 /*
323  * by now, the automatics and register variables are allocated
324  */
325 void
326 bccode()
327 {
328         SETOFF(autooffSZINT);
329 }
330
331 /* called just before final exit */
332 /* flag is 1 if errors, 0 if none */
333 void
334 ejobcode(int flag )
335 {
336 #define _MKSTR(x) #x
337 #define MKSTR(x) _MKSTR(x)
338 #define OS MKSTR(TARGOS)
339         printf("\t.ident \"PCC: %s (%s)\"\n\t.end\n"PACKAGE_STRINGOS);
340 }
341
ragge
1.11
342 /*
343  * Varargs stuff:
344  * The ABI says that va_list should be declared as this typedef.
345  * We handcraft it here and then just reference it.
346  *
347  * typedef struct {
348  *      unsigned int gp_offset;
349  *      unsigned int fp_offset;
350  *      void *overflow_arg_area;
351  *      void *reg_save_area;
352  * } __builtin_va_list[1];
353  */
ragge
1.13
354
ragge
1.11
355 static char *gp_offset, *fp_offset, *overflow_arg_area, *reg_save_area;
ragge
1.13
356
mickey
1.1
357 void
358 bjobcode()
359 {
ragge
1.11
360         struct rstack *rp;
361         NODE *p, *q;
362         char *c;
363
364         gp_offset = addname("gp_offset");
365         fp_offset = addname("fp_offset");
366         overflow_arg_area = addname("overflow_arg_area");
367         reg_save_area = addname("reg_save_area");
368
369         rp = bstruct(NULLSTNAMENULL);
ragge
1.23
370         p = block(NAMENILNILUNSIGNED0MKAP(UNSIGNED));
ragge
1.11
371         soumemb(pgp_offset0);
372         soumemb(pfp_offset0);
373         p->n_type = VOID+PTR;
ragge
1.23
374         p->n_ap = MKAP(VOID);
ragge
1.11
375         soumemb(poverflow_arg_area0);
376         soumemb(preg_save_area0);
377         nfree(p);
378         q = dclstruct(rp);
379         c = addname("__builtin_va_list");
ragge
1.23
380         p = block(LBbdty(NAMEc), bcon(1), INT0MKAP(INT));
ragge
1.11
381         p = tymerge(qp);
382         p->n_sp = lookup(c0);
383         defid(pTYPEDEF);
384         nfree(q);
385         nfree(p);
386 }
387
388 static NODE *
389 mkstkref(int offTWORD typ)
390 {
391         NODE *p;
392
ragge
1.23
393         p = block(REGNILNILPTR|typ0MKAP(LONG));
ragge
1.11
394         regno(p) = FPREG;
395         return buildtree(PLUSpbcon(off/SZCHAR));
396 }
397
398 NODE *
ragge
1.24
399 amd64_builtin_stdarg_start(NODE *fNODE *aTWORD t)
ragge
1.11
400 {
401         NODE *p, *r;
402
403         /* use the values from the function header */
404         p = a->n_left;
405         r = buildtree(ASSIGNstructref(ccopy(p), STREFreg_save_area),
406             mkstkref(-rsaoffVOID));
407         r = buildtree(COMOPr,
408             buildtree(ASSIGNstructref(ccopy(p), STREFoverflow_arg_area),
ragge
1.14
409             mkstkref(thisrspVOID)));
ragge
1.11
410         r = buildtree(COMOPr,
411             buildtree(ASSIGNstructref(ccopy(p), STREFgp_offset),
ragge
1.14
412             bcon(thisgpr*(SZLONG/SZCHAR))));
ragge
1.11
413         r = buildtree(COMOPr,
414             buildtree(ASSIGNstructref(ccopy(p), STREFfp_offset),
ragge
1.14
415             bcon(thissse*(SZDOUBLE*2/SZCHAR)+48)));
ragge
1.11
416
417         tfree(f);
418         tfree(a);
419         return r;
mickey
1.1
420 }
421
ragge
1.12
422 /*
423  * Create a tree that should be like the expression
424  *      ((long *)(l->gp_offset >= 48 ?
425  *          l->overflow_arg_area += 8, l->overflow_arg_area :
426  *          l->gp_offset += 8, l->reg_save_area + l->gp_offset))[-1]
ragge
1.13
427  * ...or similar for floats.
ragge
1.12
428  */
429 static NODE *
ragge
1.13
430 bva(NODE *apTWORD dtchar *otint addtoint max)
ragge
1.12
431 {
432         NODE *cm1, *cm2, *gpo, *ofa, *l1, *qc;
ragge
1.13
433         TWORD nt;
ragge
1.12
434
435         ofa = structref(ccopy(ap), STREFoverflow_arg_area);
ragge
1.13
436         l1 = buildtree(PLUSEQccopy(ofa), bcon(addto));
ragge
1.12
437         cm1 = buildtree(COMOPl1ofa);
438
ragge
1.13
439         gpo = structref(ccopy(ap), STREFot);
440         l1 = buildtree(PLUSEQccopy(gpo), bcon(addto));
ragge
1.12
441         cm2 = buildtree(COMOPl1buildtree(PLUSccopy(gpo),
442             structref(ccopy(ap), STREFreg_save_area)));
443         qc = buildtree(QUEST,
ragge
1.13
444             buildtree(GEgpobcon(max)),
ragge
1.12
445             buildtree(COLONcm1cm2));
ragge
1.13
446
447         nt = (dt == DOUBLE ? DOUBLE : LONG);
ragge
1.23
448         l1 = block(NAMENILNILnt|PTR0MKAP(nt));
ragge
1.13
449         l1 = buildtree(CASTl1qc);
450         qc = l1->n_right;
451         nfree(l1->n_left);
452         nfree(l1);
453
ragge
1.14
454         /* qc has now a real type, for indexing */
455         addto = dt == DOUBLE ? 2 : 1;
ragge
1.13
456         qc = buildtree(UMULbuildtree(PLUSqcbcon(-addto)), NIL);
457
ragge
1.23
458         l1 = block(NAMENILNILdt0MKAP(BTYPE(dt)));
ragge
1.12
459         l1 = buildtree(CASTl1qc);
ragge
1.13
460         qc = l1->n_right;
ragge
1.12
461         nfree(l1->n_left);
462         nfree(l1);
ragge
1.13
463
464         return qc;
ragge
1.12
465 }
466
ragge
1.11
467 NODE *
ragge
1.24
468 amd64_builtin_va_arg(NODE *fNODE *aTWORD t)
ragge
1.12
469 {
470         NODE *ap, *r;
ragge
1.13
471         TWORD dt;
ragge
1.12
472
473         ap = a->n_left;
ragge
1.13
474         dt = a->n_right->n_type;
475         if (dt <= ULONGLONG || ISPTR(dt)) {
ragge
1.12
476                 /* type might be in general register */
ragge
1.13
477                 r = bva(apdtgp_offset848);
478         } else if (dt == FLOAT || dt == DOUBLE) {
479                 /* Float are promoted to double here */
480                 if (dt == FLOAT)
481                         dt = DOUBLE;
482                 r = bva(apdtfp_offset16RSASZ/SZCHAR);
ragge
1.12
483         } else {
ragge
1.13
484                 uerror("amd64_builtin_va_arg not supported type");
485                 goto bad;
ragge
1.12
486         }
487         tfree(a);
488         tfree(f);
489         return r;
490 bad:
491         uerror("bad argument to __builtin_va_arg");
492         return bcon(0);
493 }
ragge
1.11
494
495 NODE *
ragge
1.24
496 amd64_builtin_va_end(NODE *fNODE *aTWORD t)
ragge
1.13
497 {
498         tfree(f);
499         tfree(a);
500         return bcon(0); /* nothing */
501 }
ragge
1.11
502
503 NODE *
ragge
1.24
504 amd64_builtin_va_copy(NODE *fNODE *aTWORD t) { cerror("amd64_builtin_va_copy"); return NULL; }
ragge
1.11
505
ragge
1.7
506 static NODE *
507 movtoreg(NODE *pint rno)
mickey
1.1
508 {
509         NODE *r;
510
ragge
1.23
511         r = block(REGNILNILp->n_typep->n_dfp->n_ap);
ragge
1.7
512         regno(r) = rno;
513         return clocal(buildtree(ASSIGNrp));
514 }  
515
ragge
1.9
516 static NODE *
ragge
1.14
517 movtomem(NODE *pint offint reg)
ragge
1.9
518 {
519         struct symtab s;
520         NODE *r, *l;
521
522         s.stype = p->n_type;
ragge
1.25
523         s.squal = 0;
ragge
1.9
524         s.sdf = p->n_df;
ragge
1.23
525         s.sap = p->n_ap;
ragge
1.9
526         s.soffset = off;
ragge
1.11
527         s.sclass = AUTO;
ragge
1.9
528
529         l = block(REGNILNILPTR+STRTY00);
530         l->n_lval = 0;
ragge
1.14
531         regno(l) = reg;
ragge
1.9
532
ragge
1.23
533         r = block(NAMENILNILp->n_typep->n_dfp->n_ap);
ragge
1.9
534         r->n_sp = &s;
535         r = stref(block(STREFlr000));
536
537         return clocal(buildtree(ASSIGNrp));
538 }  
539
ragge
1.7
540
541 /*
542  * AMD64 parameter classification.
543  */
544 static int
ragge
1.23
545 argtyp(TWORD tunion dimfun *dfstruct attr *ap)
ragge
1.7
546 {
547         int cl = 0;
548
ragge
1.9
549         if (t <= ULONG || ISPTR(t)) {
ragge
1.7
550                 cl = ngpr < 6 ? INTEGER : INTMEM;
551         } else if (t == FLOAT || t == DOUBLE) {
ragge
1.8
552                 cl = nsse < 8 ? SSE : SSEMEM;
ragge
1.7
553         } else if (t == LDOUBLE) {
554                 cl = X87/* XXX */
ragge
1.26
555         } else if (t == STRTY || t == UNIONTY) {
ragge
1.17
556                 /* XXX no SSEOP handling */
ragge
1.23
557                 if ((tsize(tdfap) > 2*SZLONG) ||
558                     (attr_find(apGCC_ATYP_PACKED) != NULL))
ragge
1.7
559                         cl = STRMEM;
560                 else
ragge
1.17
561                         cl = STRREG;
ragge
1.7
562         } else
563                 cerror("FIXME: classify");
564         return cl;
565 }
566
ragge
1.26
567 static NODE *
ragge
1.7
568 argput(NODE *p)
569 {
570         NODE *q;
ragge
1.17
571         int typrssz;
mickey
1.1
572
ragge
1.26
573         if (p->n_op == CM) {
574                 p->n_left = argput(p->n_left);
575                 p->n_right = argput(p->n_right);
576                 return p;
577         }
578
ragge
1.7
579         /* first arg may be struct return pointer */
580         /* XXX - check if varargs; setup al */
ragge
1.23
581         switch (typ = argtyp(p->n_typep->n_dfp->n_ap)) {
ragge
1.7
582         case INTEGER:
583         case SSE:
584                 if (typ == SSE)
585                         r = XMM0 + nsse++;
586                 else
587                         r = argregsi[ngpr++];
ragge
1.26
588                 p = movtoreg(pr);
ragge
1.7
589                 break;
ragge
1.26
590
ragge
1.7
591         case X87:
ragge
1.25
592                 r = nrsp;
593                 nrsp += SZLDOUBLE;
ragge
1.26
594                 p = movtomem(prSTKREG);
ragge
1.7
595                 break;
ragge
1.9
596
597         case INTMEM:
598                 r = nrsp;
599                 nrsp += SZLONG;
ragge
1.26
600                 p = movtomem(prSTKREG);
ragge
1.9
601                 break;
602
ragge
1.17
603         case STRREG/* Struct in registers */
604                 /* Cast to long pointer and move to the registers */
ragge
1.27
605                 /* XXX can overrun struct size */
ragge
1.23
606                 ssz = tsize(p->n_typep->n_dfp->n_ap);
ragge
1.17
607
608                 if (ssz <= SZLONG) {
609                         q = cast(p->n_leftLONG+PTR0);
ragge
1.27
610                         nfree(p);
ragge
1.17
611                         q = buildtree(UMULqNIL);
ragge
1.26
612                         p = movtoreg(qargregsi[ngpr++]);
ragge
1.17
613                 } else if (ssz <= SZLONG*2) {
614                         NODE *qt, *q1, *q2, *ql, *qr;
615
ragge
1.23
616                         qt = tempnode(0LONG+PTR0MKAP(LONG));
ragge
1.17
617                         q1 = ccopy(qt);
618                         q2 = ccopy(qt);
619                         ql = buildtree(ASSIGNqtcast(p->n_left,LONG+PTR0));
ragge
1.27
620                         nfree(p);
ragge
1.17
621                         qr = movtoreg(buildtree(UMULq1NIL),
622                             argregsi[ngpr++]);
623                         ql = buildtree(COMOPqlqr);
624                         qr = buildtree(UMULbuildtree(PLUSq2bcon(1)), NIL);
625                         qr = movtoreg(qrargregsi[ngpr++]);
ragge
1.26
626                         p = buildtree(COMOPqlqr);
ragge
1.17
627                 } else
628                         cerror("STRREG");
629                 break;
630
ragge
1.26
631         case STRMEM: {
632                 struct symtab s;
633                 NODE *l, *t;
634
635                 q = buildtree(UMULp->n_leftNIL);
636
637                 s.stype = p->n_type;
638                 s.squal = 0;
639                 s.sdf = p->n_df;
640                 s.sap = p->n_ap;
641                 s.soffset = nrsp;
642                 s.sclass = AUTO;
643
644                 nrsp += tsize(p->n_typep->n_dfp->n_ap);
645
646                 l = block(REGNILNILPTR+STRTY00);
647                 l->n_lval = 0;
648                 regno(l) = STKREG;
649
650                 t = block(NAMENILNILp->n_typep->n_dfp->n_ap);
651                 t->n_sp = &s;
652                 t = stref(block(STREFlt000));
653
654                 t = (buildtree(ASSIGNtq));
655                 nfree(p);
656                 p = t->n_left;
657                 nfree(t);
658                 break;
659                 }
660
ragge
1.7
661         default:
ragge
1.9
662                 cerror("argument %d"typ);
mickey
1.1
663         }
ragge
1.26
664         return p;
ragge
1.7
665 }
mickey
1.1
666
ragge
1.26
667 /*
668  * Sort arglist so that register assignments ends up last.
669  */
670 static int
671 argsort(NODE *p)
672 {
673         NODE *q;
674         int rv = 0;
675
676         if (p->n_op != CM)
677                 return rv;
678         if (p->n_right->n_op == ASSIGN && p->n_right->n_left->n_op == REG) {
679                 if (p->n_left->n_op == CM &&
680                     p->n_left->n_right->n_op == STASG) {
681                         q = p->n_left->n_right;
682                         p->n_left->n_right = p->n_right;
683                         p->n_right = q;
684                         rv = 1;
685                 } else if (p->n_left->n_op == STASG) {
686                         q = p->n_left;
687                         p->n_left = p->n_right;
688                         p->n_right = q;
689                         rv = 1;
690                 }
691         }
692         return rv | argsort(p->n_left);
693 }
mickey
1.1
694
695 /*
696  * Called with a function call with arguments as argument.
697  * This is done early in buildtree() and only done once.
698  * Returns p.
699  */
700 NODE *
701 funcode(NODE *p)
702 {
ragge
1.8
703         NODE *l, *r;
mickey
1.1
704
ragge
1.9
705         nsse = ngpr = nrsp = 0;
ragge
1.17
706         /* Check if hidden arg needed */
707         /* If so, add it in pass2 */
708         if ((l = p->n_left)->n_type == INCREF(FTN)+STRTY ||
709             l->n_type == INCREF(FTN)+UNIONTY) {
ragge
1.23
710                 int ssz = tsize(BTYPE(l->n_type), l->n_dfl->n_ap);
ragge
1.17
711                 if (ssz > 2*SZLONG)
712                         ngpr++;
713         }
ragge
1.26
714
715         /* Convert just regs to assign insn's */
716         p->n_right = argput(p->n_right);
717
718         /* Must sort arglist so that STASG ends up first */
719         /* This avoids registers being clobbered */
720         while (argsort(p->n_right))
721                 ;
ragge
1.8
722
ragge
1.22
723         /* Check if there are varargs */
724         if (nsse || l->n_df == NULL || l->n_df->dfun == NULL) {
725                 ; /* Need RAX */
726         } else {
727                 union arglist *al = l->n_df->dfun;
728
729                 for (; al->type != TELLIPSISal++) {
730                         if (al->type == TNULL)
731                                 return p/* No need */
732                         if (BTYPE(al->type) == STRTY ||
733                             BTYPE(al->type) == UNIONTY ||
734                             ISARY(al->type))
735                                 al++;
736                 }
737         }
738
ragge
1.8
739         /* Always emit number of SSE regs used */
740         l = movtoreg(bcon(nsse), RAX);
741         if (p->n_right->n_op != CM) {
ragge
1.23
742                 p->n_right = block(CMlp->n_rightINT0MKAP(INT));
ragge
1.8
743         } else {
744                 for (r = p->n_rightr->n_left->n_op == CMr = r->n_left)
745                         ;
ragge
1.23
746                 r->n_left = block(CMlr->n_leftINT0MKAP(INT));
ragge
1.8
747         }
mickey
1.1
748         return p;
749 }
750
751 /*
752  * return the alignment of field of type t
753  */
754 int
755 fldal(unsigned int t)
756 {
757         uerror("illegal field type");
758         return(ALINT);
759 }
760
761 /* fix up type of field p */
762 void
763 fldty(struct symtab *p)
764 {
765 }
766
767 /*
768  * XXX - fix genswitch.
769  */
770 int
771 mygenswitch(int numTWORD typestruct swents **pint n)
772 {
773         return 0;
774 }
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-10-31 18:13 +0100