Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:gmcgarry:20140604064349

Diff

Diff from 1.177 to:

Annotations

Annotate by Age | Author | Mixed | None
/fisheye/browse/pcc/pcc/arch/i386/local2.c

Annotated File View

gmcgarry
1.177
1 /*      $Id: local2.c,v 1.177 2014/06/04 06:43:49 gmcgarry Exp $        */
ragge
1.1
2 /*
3  * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 # include "pass2.h"
30 # include <ctype.h>
ragge
1.65
31 # include <string.h>
ragge
1.1
32
ragge
1.158
33 #if defined(PECOFFABI) || defined(MACHOABI) || defined(AOUTABI)
gmcgarry
1.113
34 #define EXPREFIX        "_"
35 #else
36 #define EXPREFIX        ""
37 #endif
38
39
ragge
1.71
40 static int stkpos;
41
ragge
1.1
42 void
43 deflab(int label)
44 {
45         printf(LABFMT ":\n"label);
46 }
47
ragge
1.48
48 static int regoff[7];
ragge
1.28
49 static TWORD ftype;
ragge
1.1
50
ragge
1.45
51 /*
52  * Print out the prolog assembler.
53  * addto and regoff are already calculated.
54  */
55 static void
56 prtprolog(struct interpass_prolog *ippint addto)
57 {
mickey
1.115
58         int i;
ragge
1.45
59
ragge
1.173
60 #if 1
gmcgarry
1.108
61 #if defined(MACHOABI)
ragge
1.173
62         addto += 8;
63 #endif
plunky
1.174
64         if (addto == 0 || addto > 65535) {
ragge
1.173
65                 printf("        pushl %%ebp\n\tmovl %%esp,%%ebp\n");
plunky
1.174
66                 if (addto)
67                         printf("        subl $%d,%%esp\n"addto);
68         } else
ragge
1.173
69                 printf("        enter $%d,$0\n"addto);
gmcgarry
1.108
70 #endif
mickey
1.115
71         for (i = 0i < MAXREGSi++)
72                 if (TESTBIT(ipp->ipp_regsi))
plunky
1.169
73                         printf("        movl %s,-%d(%s)\n",
mickey
1.115
74                             rnames[i], regoff[i], rnames[FPREG]);
ragge
1.45
75 }
76
77 /*
ragge
1.71
78  * calculate stack size and offsets
79  */
ragge
1.45
80 static int
81 offcalc(struct interpass_prolog *ipp)
82 {
mickey
1.115
83         int iaddto;
ragge
1.45
84
85         addto = p2maxautooff;
ragge
1.71
86         if (addto >= AUTOINIT/SZCHAR)
87                 addto -= AUTOINIT/SZCHAR;
mickey
1.115
88         for (i = 0i < MAXREGSi++)
89                 if (TESTBIT(ipp->ipp_regsi)) {
ragge
1.48
90                         addto += SZINT/SZCHAR;
mickey
1.115
91                         regoff[i] = addto;
ragge
1.48
92                 }
ragge
1.45
93         return addto;
94 }
95
ragge
1.49
96 void
97 prologue(struct interpass_prolog *ipp)
98 {
99         int addto;
100
101         ftype = ipp->ipp_type;
gmcgarry
1.111
102
ragge
1.102
103 #ifdef LANG_F77
ragge
1.49
104         if (ipp->ipp_vis)
105                 printf("        .globl %s\n"ipp->ipp_name);
106         printf("        .align 4\n");
107         printf("%s:\n"ipp->ipp_name);
ragge
1.100
108 #endif
ragge
1.49
109         /*
110          * We here know what register to save and how much to 
111          * add to the stack.
112          */
113         addto = offcalc(ipp);
gmcgarry
1.108
114 #if defined(MACHOABI)
115         addto = (addto + 15) & ~15;     /* stack alignment */
116 #endif
ragge
1.49
117         prtprolog(ippaddto);
118 }
119
120 void
121 eoftn(struct interpass_prolog *ipp)
122 {
mickey
1.115
123         int i;
ragge
1.49
124
125         if (ipp->ipp_ip.ip_lbl == 0)
126                 return/* no code needs to be generated */
127
128         /* return from function code */
mickey
1.115
129         for (i = 0i < MAXREGSi++)
130                 if (TESTBIT(ipp->ipp_regsi))
plunky
1.169
131                         printf("        movl -%d(%s),%s\n",
mickey
1.115
132                             regoff[i], rnames[FPREG], rnames[i]);
ragge
1.49
133
134         /* struct return needs special treatment */
135         if (ftype == STRTY || ftype == UNIONTY) {
136                 printf("        movl 8(%%ebp),%%eax\n");
137                 printf("        leave\n");
gmcgarry
1.111
138                 printf("        ret $%d\n"4 + ipp->ipp_argstacksize);
ragge
1.49
139         } else {
140                 printf("        leave\n");
gmcgarry
1.111
141                 if (ipp->ipp_argstacksize)
142                         printf("        ret $%d\n"ipp->ipp_argstacksize);
143                 else
144                         printf("        ret\n");
ragge
1.49
145         }
gmcgarry
1.111
146
147 #if defined(ELFABI)
gmcgarry
1.113
148         printf("\t.size " EXPREFIX "%s,.-" EXPREFIX "%s\n"ipp->ipp_name,
149             ipp->ipp_name);
gmcgarry
1.111
150 #endif
ragge
1.49
151 }
ragge
1.1
152
153 /*
154  * add/sub/...
155  *
156  * Param given:
157  */
158 void
159 hopcode(int fint o)
160 {
161         char *str;
162
163         switch (o) {
164         case PLUS:
165                 str = "add";
166                 break;
167         case MINUS:
168                 str = "sub";
169                 break;
170         case AND:
171                 str = "and";
172                 break;
173         case OR:
ragge
1.10
174                 str = "or";
ragge
1.1
175                 break;
176         case ER:
177                 str = "xor";
178                 break;
179         default:
ragge
1.14
180                 comperr("hopcode2: %d"o);
ragge
1.32
181                 str = 0/* XXX gcc */
ragge
1.1
182         }
ragge
1.7
183         printf("%s%c"strf);
ragge
1.1
184 }
185
ragge
1.72
186 /*
187  * Return type size in bytes.  Used by R2REGS, arg 2 to offset().
188  */
ragge
1.1
189 int
plunky
1.167
190 tlen(NODE *p)
ragge
1.1
191 {
192         switch(p->n_type) {
193                 case CHAR:
194                 case UCHAR:
195                         return(1);
196
197                 case SHORT:
198                 case USHORT:
199                         return(SZSHORT/SZCHAR);
200
201                 case DOUBLE:
202                         return(SZDOUBLE/SZCHAR);
203
204                 case INT:
205                 case UNSIGNED:
206                 case LONG:
207                 case ULONG:
208                         return(SZINT/SZCHAR);
209
210                 case LONGLONG:
211                 case ULONGLONG:
212                         return SZLONGLONG/SZCHAR;
213
214                 default:
215                         if (!ISPTR(p->n_type))
ragge
1.14
216                                 comperr("tlen type %d not pointer");
ragge
1.39
217                         return SZPOINT(p->n_type)/SZCHAR;
ragge
1.1
218                 }
219 }
220
ragge
1.17
221 /*
222  * Emit code to compare two longlong numbers.
223  */
224 static void
225 twollcomp(NODE *p)
226 {
ragge
1.135
227         int u;
ragge
1.118
228         int s = getlab2();
ragge
1.17
229         int e = p->n_label;
230         int cb1cb2;
231
ragge
1.135
232         u = p->n_op;
233         switch (p->n_op) {
ragge
1.17
234         case NE:
235                 cb1 = 0;
236                 cb2 = NE;
237                 break;
238         case EQ:
239                 cb1 = NE;
240                 cb2 = 0;
241                 break;
242         case LE:
243         case LT:
ragge
1.135
244                 u += (ULE-LE);
245                 /* FALLTHROUGH */
246         case ULE:
247         case ULT:
ragge
1.17
248                 cb1 = GT;
249                 cb2 = LT;
250                 break;
251         case GE:
252         case GT:
ragge
1.135
253                 u += (ULE-LE);
254                 /* FALLTHROUGH */
255         case UGE:
256         case UGT:
ragge
1.17
257                 cb1 = LT;
258                 cb2 = GT;
259                 break;
260         
ragge
1.32
261         default:
262                 cb1 = cb2 = 0/* XXX gcc */
ragge
1.17
263         }
264         if (p->n_op >= ULE)
265                 cb1 += 4cb2 += 4;
266         expand(p0"  cmpl UR,UL\n");
267         if (cb1cbgen(cb1s);
268         if (cb2cbgen(cb2e);
269         expand(p0"  cmpl AR,AL\n");
ragge
1.135
270         cbgen(ue);
ragge
1.17
271         deflab(s);
272 }
273
stefan
1.98
274 int
275 fldexpand(NODE *pint cookiechar **cp)
276 {
ragge
1.160
277         comperr("fldexpand");
278         return 0;
ragge
1.149
279 }
280
ragge
1.23
281 /*
282  * Push a structure on stack as argument.
283  * the scratch registers are already free here
284  */
285 static void
286 starg(NODE *p)
287 {
ragge
1.159
288         NODE *q = p->n_left;
289
plunky
1.168
290         printf("        subl $%d,%%esp\n", (p->n_stsize + 3) & ~3);
ragge
1.159
291         p->n_left = mklnode(OREG0ESPINT);
292         zzzcode(p'Q');
293         tfree(p->n_left);
294         p->n_left = q;
ragge
1.23
295 }
296
ragge
1.25
297 /*
298  * Compare two floating point numbers.
299  */
300 static void
301 fcomp(NODE *p)  
302 {
ragge
1.148
303         static char *fpcb[] = { "jz""jnz""jbe""jc""jnc""ja" };
ragge
1.140
304
ragge
1.148
305         if ((p->n_su & DORIGHT) == 0)
plunky
1.163
306                 expand(p0"\tfxch\n");
307         expand(p0"\tfucomip %st(1),%st\n"); /* emit compare insn  */
308         expand(p0"\tfstp %st(0)\n");        /* pop fromstack */
ragge
1.148
309
310         if (p->n_op == NE || p->n_op == GT || p->n_op == GE)
plunky
1.163
311                 expand(p0"\tjp LC\n");
ragge
1.148
312         else if (p->n_op == EQ)
313                 printf("\tjp 1f\n");
314         printf("        %s "fpcb[p->n_op - EQ]);
315         expand(p0"LC\n");
316         if (p->n_op == EQ)
317                 printf("1:\n");
ragge
1.25
318 }
319
ragge
1.29
320 /*
321  * Convert an unsigned long long to floating point number.
322  */
323 static void
324 ulltofp(NODE *p)
325 {
gmcgarry
1.170
326         int jmplab;
327
gmcgarry
1.165
328 #if defined(ELFABI) || defined(PECOFFABI)
ragge
1.29
329         static int loadlab;
330
331         if (loadlab == 0) {
ragge
1.118
332                 loadlab = getlab2();
ragge
1.29
333                 expand(p0"  .data\n");
334                 printf(LABFMT ":        .long 0,0x80000000,0x403f\n"loadlab);
335                 expand(p0"  .text\n");
336         }
gmcgarry
1.170
337 #endif
338
ragge
1.118
339         jmplab = getlab2();
ragge
1.29
340         expand(p0"  pushl UL\n      pushl AL\n");
341         expand(p0"  fildq (%esp)\n");
342         expand(p0"  addl $8,%esp\n");
343         expand(p0"  cmpl $0,UL\n");
344         printf("        jge " LABFMT "\n"jmplab);
gmcgarry
1.170
345
346 #if defined(ELFABI)
ragge
1.161
347         printf("        fldt " LABFMT "%s\n"loadlabkflag ? "@GOTOFF" : "");
gmcgarry
1.170
348 #elif defined(MACHOABI)
349         printf("\tpushl 0x5f800000\n");
350         printf("\tfadds (%%esp)\n");
351         printf("\taddl $4,%%esp\n");
gmcgarry
1.164
352 #else
353 #error incomplete implementation
354 #endif
gmcgarry
1.170
355
356         printf("        faddp %%st,%%st(1)\n");
357         printf(LABFMT ":\n"jmplab);
ragge
1.29
358 }
359
ragge
1.69
360 static int
361 argsiz(NODE *p)
362 {
363         TWORD t = p->n_type;
364
ragge
1.81
365         if (t < LONGLONG || t == FLOAT || t > BTMASK)
ragge
1.69
366                 return 4;
367         if (t == LONGLONG || t == ULONGLONG || t == DOUBLE)
368                 return 8;
369         if (t == LDOUBLE)
370                 return 12;
ragge
1.81
371         if (t == STRTY || t == UNIONTY)
ragge
1.143
372                 return (p->n_stsize+3) & ~3;
ragge
1.69
373         comperr("argsiz");
374         return 0;
375 }
376
ragge
1.133
377 static void
378 fcast(NODE *p)
379 {
380         TWORD t = p->n_type;
381         int szc;
382
383         if (t >= p->n_left->n_type)
384                 return/* cast to more precision */
385         if (t == FLOAT)
386                 sz = 4c = 's';
387         else
388                 sz = 8c = 'l';
389
390         printf("        sub $%d,%%esp\n"sz);
391         printf("        fstp%c (%%esp)\n"c);
392         printf("        fld%c (%%esp)\n"c);
393         printf("        add $%d,%%esp\n"sz);
394 }
395
ragge
1.151
396 static void
397 llshft(NODE *p)
398 {
399         char *d[3];
400
401         if (p->n_op == LS) {
402                 d[0] = "l"d[1] = "%eax"d[2] = "%edx";
403         } else
404                 d[0] = "r"d[1] = "%edx"d[2] = "%eax";
405
406         printf("\tsh%sdl %s,%s\n",d[0], d[1], d[2]);
407         printf("\ts%s%sl %%cl,%s\n"p->n_op == RS &&
408             p->n_left->n_type == ULONGLONG ? "h" : "a"d[0], d[1]);
409         printf("\ttestb $32,%%cl\n");
410         printf("\tje 1f\n");
411         printf("\tmovl %s,%s\n"d[1], d[2]);
412         if (p->n_op == RS && p->n_left->n_type == LONGLONG)
413                 printf("\tsarl $31,%%edx\n");
414         else
415                 printf("\txorl %s,%s\n",d[1],d[1]);
416         printf("1:\n");
417 }
418
ragge
1.1
419 void
420 zzzcode(NODE *pint c)
421 {
ragge
1.137
422         NODE *l;
ragge
1.162
423         int prlr;
ragge
1.69
424         char *ch;
ragge
1.1
425
426         switch (c) {
ragge
1.84
427         case 'A'/* swap st0 and st1 if right is evaluated second */
ragge
1.83
428                 if ((p->n_su & DORIGHT) == 0) {
429                         if (logop(p->n_op))
430                                 printf("        fxch\n");
431                         else
432                                 printf("r");
433                 }
434                 break;
ragge
1.9
435
ragge
1.12
436         case 'C':  /* remove from stack after subroutine call */
ragge
1.175
437 #ifdef notdef
gmcgarry
1.111
438                 if (p->n_left->n_flags & FSTDCALL)
439                         break;
ragge
1.175
440 #endif
ragge
1.71
441                 pr = p->n_qual;
ragge
1.139
442                 if (p->n_flags & FFPPOP)
443                         printf("        fstp    %%st(0)\n");
ragge
1.69
444                 if (p->n_op == UCALL)
445                         return/* XXX remove ZC from UCALL */
ragge
1.71
446                 if (pr)
447                         printf("        addl $%d, %s\n"prrnames[ESP]);
ragge
1.12
448                 break;
ragge
1.17
449
450         case 'D'/* Long long comparision */
451                 twollcomp(p);
452                 break;
ragge
1.12
453
ragge
1.23
454         case 'F'/* Structure argument */
ragge
1.71
455                 if (p->n_stalign != 0/* already on stack */
456                         starg(p);
ragge
1.23
457                 break;
458
ragge
1.25
459         case 'G'/* Floating point compare */
460                 fcomp(p);
461                 break;
462
ragge
1.141
463         case 'H'/* assign of longlong between regs */
464                 rmove(DECRA(p->n_right->n_reg0),
465                     DECRA(p->n_left->n_reg0), LONGLONG);
466                 break;
467
ragge
1.133
468         case 'I'/* float casts */
469                 fcast(p);
470                 break;
471
ragge
1.29
472         case 'J'/* convert unsigned long long to floating point */
473                 ulltofp(p);
474                 break;
475
ragge
1.144
476         case 'K'/* Load longlong reg into another reg */
477                 rmove(regno(p), DECRA(p->n_reg0), LONGLONG);
478                 break;
479
ragge
1.59
480         case 'M'/* Output sconv move, if needed */
481                 l = getlr(p'L');
482                 /* XXX fixneed: regnum */
ragge
1.68
483                 pr = DECRA(p->n_reg0);
484                 lr = DECRA(l->n_reg0);
ragge
1.66
485                 if ((pr == AL && lr == EAX) || (pr == BL && lr == EBX) ||
486                     (pr == CL && lr == ECX) || (pr == DL && lr == EDX))
ragge
1.59
487                         ;
488                 else
ragge
1.66
489                         printf("        movb %%%cl,%s\n",
490                             rnames[lr][2], rnames[pr]);
ragge
1.59
491                 l->n_rval = l->n_reg = p->n_reg/* XXX - not pretty */
492                 break;
ragge
1.67
493
494         case 'N'/* output extended reg name */
495                 printf("%s"rnames[getlr(p'1')->n_rval]);
496                 break;
497
ragge
1.69
498         case 'O'/* print out emulated ops */
ragge
1.76
499                 pr = 16;
500                 if (p->n_op == RS || p->n_op == LS) {
ragge
1.151
501                         llshft(p);
502                         break;
ragge
1.153
503                 } else if (p->n_op == MUL) {
504                         printf("\timull %%ecx, %%edx\n");
505                         printf("\timull %%eax, %%esi\n");
506                         printf("\taddl %%edx, %%esi\n");
507                         printf("\tmull %%ecx\n");
508                         printf("\taddl %%esi, %%edx\n");
509                         break;
ragge
1.151
510                 }
511                 expand(pINCREG"\tpushl UR\n\tpushl AR\n");
ragge
1.69
512                 expand(pINCREG"\tpushl UL\n\tpushl AL\n");
gmcgarry
1.113
513                 if (p->n_op == DIV && p->n_type == ULONGLONGch = "udiv";
514                 else if (p->n_op == DIVch = "div";
515                 else if (p->n_op == MOD && p->n_type == ULONGLONGch = "umod";
516                 else if (p->n_op == MODch = "mod";
ragge
1.69
517                 else ch = 0comperr("ZO");
gmcgarry
1.142
518 #ifdef ELFABI
ragge
1.140
519                 printf("\tcall " EXPREFIX "__%sdi3%s\n\taddl $%d,%s\n",
520                         ch, (kflag ? "@PLT" : ""), prrnames[ESP]);
gmcgarry
1.142
521 #else
522                 printf("\tcall " EXPREFIX "__%sdi3\n\taddl $%d,%s\n",
523                         chprrnames[ESP]);
524 #endif
ragge
1.69
525                 break;
526
ragge
1.71
527         case 'Q'/* emit struct assign */
ragge
1.130
528                 /*
ragge
1.172
529                  * Put out some combination of movs{b,w,l}
ragge
1.130
530                  * esi/edi/ecx are available.
531                  */
ragge
1.172
532                 expand(pINAREG"     leal AL,%edi\n");
533                 if (p->n_stsize < 32) {
534                         int i = p->n_stsize >> 2;
535                         while (i) {
536                                 expand(pINAREG"     movsl\n");
537                                 i--;
ragge
1.130
538                         }
ragge
1.172
539                 } else {
540                         printf("\tmovl $%d,%%ecx\n"p->n_stsize >> 2);
541                         printf("        rep movsl\n");
gmcgarry
1.122
542                 }
ragge
1.172
543                 if (p->n_stsize & 2)
544                         printf("        movsw\n");
545                 if (p->n_stsize & 1)
546                         printf("        movsb\n");
ragge
1.71
547                 break;
548
ragge
1.73
549         case 'S'/* emit eventual move after cast from longlong */
550                 pr = DECRA(p->n_reg0);
551                 lr = p->n_left->n_rval;
552                 switch (p->n_type) {
553                 case CHAR:
554                 case UCHAR:
ragge
1.75
555                         if (rnames[pr][2] == 'l' && rnames[lr][2] == 'x' &&
556                             rnames[pr][1] == rnames[lr][1])
557                                 break;
558                         if (rnames[lr][2] == 'x') {
559                                 printf("\tmovb %%%cl,%s\n",
560                                     rnames[lr][1], rnames[pr]);
561                                 break;
562                         }
ragge
1.87
563                         /* Must go via stack */
ragge
1.162
564                         expand(pINAREG"\tmovl AL,A2\n");
565                         expand(pINBREG"\tmovb A2,A1\n");
566 #ifdef notdef
567                         /* cannot use freetemp() in instruction emission */
ragge
1.176
568                         s = freetemp(1);
ragge
1.87
569                         printf("\tmovl %%e%ci,%d(%%ebp)\n"rnames[lr][1], s);
570                         printf("\tmovb %d(%%ebp),%s\n"srnames[pr]);
ragge
1.162
571 #endif
ragge
1.73
572                         break;
ragge
1.74
573
ragge
1.79
574                 case SHORT:
575                 case USHORT:
576                         if (rnames[lr][1] == rnames[pr][2] &&
577                             rnames[lr][2] == rnames[pr][3])
578                                 break;
579                         printf("\tmovw %%%c%c,%%%s\n",
580                             rnames[lr][1], rnames[lr][2], rnames[pr]+2);
581                         break;
ragge
1.74
582                 case INT:
583                 case UNSIGNED:
ragge
1.78
584                         if (rnames[lr][1] == rnames[pr][2] &&
585                             rnames[lr][2] == rnames[pr][3])
586                                 break;
587                         printf("\tmovl %%e%c%c,%s\n",
588                                     rnames[lr][1], rnames[lr][2], rnames[pr]);
ragge
1.74
589                         break;
590
ragge
1.73
591                 default:
ragge
1.78
592                         if (rnames[lr][1] == rnames[pr][2] &&
593                             rnames[lr][2] == rnames[pr][3])
594                                 break;
595                         comperr("SCONV2 %s->%s"rnames[lr], rnames[pr]);
ragge
1.73
596                         break;
597                 }
598                 break;
599
ragge
1.1
600         default:
ragge
1.14
601                 comperr("zzzcode %c"c);
ragge
1.1
602         }
603 }
604
605 int canaddr(NODE *);
606 int
607 canaddr(NODE *p)
608 {
609         int o = p->n_op;
610
611         if (o==NAME || o==REG || o==ICON || o==OREG ||
ragge
1.114
612             (o==UMUL && shumul(p->n_leftSOREG)))
ragge
1.1
613                 return(1);
614         return(0);
615 }
616
ragge
1.21
617 /*
618  * Does the bitfield shape match?
619  */
ragge
1.1
620 int
621 flshape(NODE *p)
622 {
ragge
1.160
623         comperr("flshape");
624         return 0;
ragge
1.1
625 }
626
627 /* INTEMP shapes must not contain any temporary registers */
ragge
1.15
628 /* XXX should this go away now? */
ragge
1.1
629 int
630 shtemp(NODE *p)
631 {
ragge
1.15
632         return 0;
633 #if 0
ragge
1.1
634         int r;
635
636         if (p->n_op == STARG )
637                 p = p->n_left;
638
639         switch (p->n_op) {
640         case REG:
641                 return (!istreg(p->n_rval));
642
643         case OREG:
644                 r = p->n_rval;
645                 if (R2TEST(r)) {
646                         if (istreg(R2UPK1(r)))
647                                 return(0);
648                         r = R2UPK2(r);
649                 }
650                 return (!istreg(r));
651
ragge
1.12
652         case UMUL:
ragge
1.1
653                 p = p->n_left;
ragge
1.12
654                 return (p->n_op != UMUL && shtemp(p));
ragge
1.1
655         }
656
657         if (optype(p->n_op) != LTYPE)
658                 return(0);
659         return(1);
ragge
1.15
660 #endif
ragge
1.1
661 }
662
663 void
664 adrcon(CONSZ val)
665 {
ragge
1.2
666         printf("$" CONFMTval);
ragge
1.1
667 }
668
669 void
ragge
1.14
670 conput(FILE *fpNODE *p)
ragge
1.1
671 {
gmcgarry
1.128
672         int val = (int)p->n_lval;
ragge
1.17
673
ragge
1.1
674         switch (p->n_op) {
675         case ICON:
ragge
1.17
676                 if (p->n_name[0] != '\0') {
ragge
1.14
677                         fprintf(fp"%s"p->n_name);
ragge
1.17
678                         if (val)
679                                 fprintf(fp"+%d"val);
680                 } else
681                         fprintf(fp"%d"val);
ragge
1.1
682                 return;
683
684         default:
ragge
1.74
685                 comperr("illegal conput, p %p"p);
ragge
1.1
686         }
687 }
688
689 /*ARGSUSED*/
690 void
691 insput(NODE *p)
692 {
ragge
1.14
693         comperr("insput");
ragge
1.1
694 }
695
696 /*
697  * Write out the upper address, like the upper register of a 2-register
698  * reference, or the next memory location.
699  */
700 void
701 upput(NODE *pint size)
702 {
703
ragge
1.17
704         size /= SZCHAR;
ragge
1.1
705         switch (p->n_op) {
706         case REG:
plunky
1.169
707                 printf("%%%s", &rnames[p->n_rval][3]);
ragge
1.1
708                 break;
709
710         case NAME:
711         case OREG:
712                 p->n_lval += size;
ragge
1.12
713                 adrput(stdoutp);
ragge
1.1
714                 p->n_lval -= size;
715                 break;
716         case ICON:
plunky
1.169
717                 printf("$" CONFMTp->n_lval >> 32);
ragge
1.1
718                 break;
719         default:
ragge
1.14
720                 comperr("upput bad op %d size %d"p->n_opsize);
ragge
1.1
721         }
722 }
723
724 void
ragge
1.12
725 adrput(FILE *ioNODE *p)
ragge
1.1
726 {
727         int r;
728         /* output an address, with offsets, from p */
729
730         switch (p->n_op) {
731
732         case NAME:
ragge
1.88
733                 if (p->n_name[0] != '\0') {
ragge
1.13
734                         fputs(p->n_nameio);
ragge
1.88
735                         if (p->n_lval != 0)
736                                 fprintf(io"+" CONFMTp->n_lval);
737                 } else
738                         fprintf(ioCONFMTp->n_lval);
ragge
1.1
739                 return;
740
741         case OREG:
742                 r = p->n_rval;
ragge
1.92
743                 if (p->n_name[0])
744                         printf("%s%s"p->n_namep->n_lval ? "+" : "");
ragge
1.17
745                 if (p->n_lval)
746                         fprintf(io"%d", (int)p->n_lval);
ragge
1.82
747                 if (R2TEST(r)) {
748                         fprintf(io"(%s,%s,4)"rnames[R2UPK1(r)],
749                             rnames[R2UPK2(r)]);
750                 } else
751                         fprintf(io"(%s)"rnames[p->n_rval]);
ragge
1.1
752                 return;
753         case ICON:
ragge
1.97
754 #ifdef PCC_DEBUG
755                 /* Sanitycheck for PIC, to catch adressable constants */
ragge
1.119
756                 if (kflag && p->n_name[0] && 0) {
ragge
1.97
757                         static int foo;
758
759                         if (foo++ == 0) {
760                                 printf("\nfailing...\n");
761                                 fwalk(pe2print0);
762                                 comperr("pass2 conput");
763                         }
764                 }
765 #endif
ragge
1.1
766                 /* addressable value of the constant */
ragge
1.14
767                 fputc('$'io);
768                 conput(iop);
ragge
1.1
769                 return;
770
771         case REG:
ragge
1.66
772                 switch (p->n_type) {
773                 case LONGLONG:
774                 case ULONGLONG:
ragge
1.63
775                         fprintf(io"%%%c%c%c"rnames[p->n_rval][0],
776                             rnames[p->n_rval][1], rnames[p->n_rval][2]);
ragge
1.66
777                         break;
778                 case SHORT:
779                 case USHORT:
780                         fprintf(io"%%%s", &rnames[p->n_rval][2]);
781                         break;
782                 default:
ragge
1.63
783                         fprintf(io"%s"rnames[p->n_rval]);
ragge
1.66
784                 }
ragge
1.1
785                 return;
786
787         default:
ragge
1.26
788                 comperr("illegal address, op %d, node %p"p->n_opp);
ragge
1.1
789                 return;
790
791         }
792 }
793
ragge
1.6
794 static char *
795 ccbranches[] = {
796         "je",           /* jumpe */
797         "jne",          /* jumpn */
798         "jle",          /* jumple */
799         "jl",           /* jumpl */
800         "jge",          /* jumpge */
801         "jg",           /* jumpg */
802         "jbe",          /* jumple (jlequ) */
803         "jb",           /* jumpl (jlssu) */
804         "jae",          /* jumpge (jgequ) */
805         "ja",           /* jumpg (jgtru) */
806 };
807
ragge
1.1
808
809 /*   printf conditional and unconditional branches */
810 void
ragge
1.6
811 cbgen(int oint lab)
ragge
1.1
812 {
ragge
1.6
813         if (o < EQ || o > UGT)
ragge
1.14
814                 comperr("bad conditional branch: %s"opst[o]);
ragge
1.6
815         printf("        %s " LABFMT "\n"ccbranches[o-EQ], lab);
ragge
1.1
816 }
817
ragge
1.28
818 static void
ragge
1.116
819 fixcalls(NODE *pvoid *arg)
ragge
1.28
820 {
ragge
1.83
821         /* Prepare for struct return by allocating bounce space on stack */
ragge
1.71
822         switch (p->n_op) {
823         case STCALL:
824         case USTCALL:
ragge
1.77
825                 if (p->n_stsize+p2autooff > stkpos)
826                         stkpos = p->n_stsize+p2autooff;
ragge
1.71
827                 break;
ragge
1.152
828         case LS:
829         case RS:
830                 if (p->n_type != LONGLONG && p->n_type != ULONGLONG)
831                         break;
832                 if (p->n_right->n_op == ICON/* constants must be char */
833                         p->n_right->n_type = CHAR;
834                 break;
ragge
1.71
835         }
ragge
1.83
836 }
837
838 /*
839  * Must store floats in memory if there are two function calls involved.
840  */
841 static int
842 storefloat(struct interpass *ipNODE *p)
843 {
844         int lr;
845
846         switch (optype(p->n_op)) {
847         case BITYPE:
848                 l = storefloat(ipp->n_left);
849                 r = storefloat(ipp->n_right);
850                 if (p->n_op == CM)
851                         return 0/* arguments, don't care */
852                 if (callop(p->n_op))
853                         return 1/* found one */
854 #define ISF(p) ((p)->n_type == FLOAT || (p)->n_type == DOUBLE || \
855         (p)->n_type == LDOUBLE)
856                 if (ISF(p->n_left) && ISF(p->n_right) && l && r) {
857                         /* must store one. store left */
858                         struct interpass *nip;
859                         TWORD t = p->n_left->n_type;
860                         NODE *ll;
861                         int off;
862
ragge
1.176
863                         off = freetemp(szty(t));
ragge
1.83
864                         ll = mklnode(OREGoffFPREGt);
865                         nip = ipnode(mkbinode(ASSIGNllp->n_leftt));
866                         p->n_left = mklnode(OREGoffFPREGt);
867                         DLIST_INSERT_BEFORE(ipnipqelem);
868                 }
869                 return l|r;
870
871         case UTYPE:
872                 l = storefloat(ipp->n_left);
873                 if (callop(p->n_op))
874                         l = 1;
875                 return l;
876         default:
877                 return 0;
878         }
ragge
1.28
879 }
ragge
1.1
880
ragge
1.132
881 static void
882 outfargs(struct interpass *ipNODE **aryint numint *cwpint c)
883 {
884         struct interpass *ip2;
885         NODE *q, *r;
886         int i;
887
888         for (i = 0i < numi++)
889                 if (XASMVAL(cwp[i]) == c && (cwp[i] & (XASMASG|XASMINOUT)))
890                         break;
891         if (i == num)
892                 return;
893         q = ary[i]->n_left;
894         r = mklnode(REG0c == 'u' ? 040 : 037q->n_type);
895         ary[i]->n_left = tcopy(r);
896         ip2 = ipnode(mkbinode(ASSIGNqrq->n_type));
897         DLIST_INSERT_AFTER(ipip2qelem);
898 }
899
900 static void
901 infargs(struct interpass *ipNODE **aryint numint *cwpint c)
902 {
903         struct interpass *ip2;
904         NODE *q, *r;
905         int i;
906
907         for (i = 0i < numi++)
908                 if (XASMVAL(cwp[i]) == c && (cwp[i] & XASMASG) == 0)
909                         break;
910         if (i == num)
911                 return;
912         q = ary[i]->n_left;
913         q = (cwp[i] & XASMINOUT) ? tcopy(q) : q;
914         r = mklnode(REG0c == 'u' ? 040 : 037q->n_type);
915         if ((cwp[i] & XASMINOUT) == 0)
916                 ary[i]->n_left = tcopy(r);
917         ip2 = ipnode(mkbinode(ASSIGNrqq->n_type));
918         DLIST_INSERT_BEFORE(ipip2qelem);
919 }
920
921 /*
922  * Extract float args to XASM and ensure that they are put on the stack
923  * in correct order.
924  * This should be done sow other way.
925  */
926 static void
927 fixxfloat(struct interpass *ipNODE *p)
928 {
929         NODE *w, **ary;
930         int nnic, *cwp;
931
932         nn = 1;
933         w = p->n_left;
934         if (w->n_op == ICON && w->n_type == STRTY)
935                 return;
936         /* index all xasm args first */
937         for (; w->n_op == CMw = w->n_left)
938                 nn++;
939         ary = tmpcalloc(nn * sizeof(NODE *));
940         cwp = tmpcalloc(nn * sizeof(int));
941         for (i = 0w = p->n_leftw->n_op == CMw = w->n_left) {
942                 ary[i] = w->n_right;
943                 cwp[i] = xasmcode(ary[i]->n_name);
944                 i++;
945         }
946         ary[i] = w;
947         cwp[i] = xasmcode(ary[i]->n_name);
948         for (i = 0i < nni++)
949                 if (XASMVAL(cwp[i]) == 't' || XASMVAL(cwp[i]) == 'u')
950                         break;
951         if (i == nn)
952                 return;
953
954         for (i = 0i < nni++) {
955                 c = XASMVAL(cwp[i]);
956                 if (c >= '0' && c <= '9')
957                         cwp[i] = (cwp[i] & ~0377) | XASMVAL(cwp[c-'0']);
958         }
959         infargs(iparynncwp'u');
960         infargs(iparynncwp't');
961         outfargs(iparynncwp't');
962         outfargs(iparynncwp'u');
963 }
964
ragge
1.172
965 static NODE *
966 lptr(NODE *p)
967 {
968         if (p->n_op == ASSIGN && p->n_right->n_op == REG &&
969             regno(p->n_right) == EBP)
970                 return p->n_right;
971         if (p->n_op == FUNARG && p->n_left->n_op == REG &&
972             regno(p->n_left) == EBP)
973                 return p->n_left;
974         return NIL;
975 }
976
977 /*
978  * Find arg reg that should be struct reference instead.
979  */