Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20110515170835

Diff

Diff from 1.54 to:

Annotations

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

Annotated File View

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