Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:plunky:20140527154857

Diff

Diff from 1.174 to:

Annotations

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

Annotated File View

plunky
1.174
1 /*      $Id: local2.c,v 1.174 2014/05/27 15:48:57 plunky 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.143
288 #if defined(MACHOABI)
plunky
1.168
289         printf("        subl $%d,%%esp\n"p->n_stsize);
290         printf("        subl $4,%%esp\n");
291         printf("        pushl $%d\n"p->n_stsize);
ragge
1.23
292         expand(p0"  pushl AL\n");
gmcgarry
1.124
293         expand(p0"  leal 12(%esp),A1\n");
ragge
1.23
294         expand(p0"  pushl A1\n");
gmcgarry
1.122
295         if (kflag) {
plunky
1.168
296                 printf("        call L%s$stub\n"EXPREFIX "memcpy");
gmcgarry
1.122
297                 addstub(&stublistEXPREFIX "memcpy");
298         } else {
plunky
1.168
299                 printf("        call %s\n"EXPREFIX "memcpy");
gmcgarry
1.122
300         }
plunky
1.168
301         printf("        addl $16,%%esp\n");
gmcgarry
1.108
302 #else
ragge
1.159
303         NODE *q = p->n_left;
304
plunky
1.168
305         printf("        subl $%d,%%esp\n", (p->n_stsize + 3) & ~3);
ragge
1.159
306         p->n_left = mklnode(OREG0ESPINT);
307         zzzcode(p'Q');
308         tfree(p->n_left);
309         p->n_left = q;
gmcgarry
1.108
310 #endif
ragge
1.23
311 }
312
ragge
1.25
313 /*
314  * Compare two floating point numbers.
315  */
316 static void
317 fcomp(NODE *p)  
318 {
ragge
1.148
319         static char *fpcb[] = { "jz""jnz""jbe""jc""jnc""ja" };
ragge
1.140
320
ragge
1.148
321         if ((p->n_su & DORIGHT) == 0)
plunky
1.163
322                 expand(p0"\tfxch\n");
323         expand(p0"\tfucomip %st(1),%st\n"); /* emit compare insn  */
324         expand(p0"\tfstp %st(0)\n");        /* pop fromstack */
ragge
1.148
325
326         if (p->n_op == NE || p->n_op == GT || p->n_op == GE)
plunky
1.163
327                 expand(p0"\tjp LC\n");
ragge
1.148
328         else if (p->n_op == EQ)
329                 printf("\tjp 1f\n");
330         printf("        %s "fpcb[p->n_op - EQ]);
331         expand(p0"LC\n");
332         if (p->n_op == EQ)
333                 printf("1:\n");
ragge
1.25
334 }
335
ragge
1.29
336 /*
337  * Convert an unsigned long long to floating point number.
338  */
339 static void
340 ulltofp(NODE *p)
341 {
gmcgarry
1.170
342         int jmplab;
343
gmcgarry
1.165
344 #if defined(ELFABI) || defined(PECOFFABI)
ragge
1.29
345         static int loadlab;
346
347         if (loadlab == 0) {
ragge
1.118
348                 loadlab = getlab2();
ragge
1.29
349                 expand(p0"  .data\n");
350                 printf(LABFMT ":        .long 0,0x80000000,0x403f\n"loadlab);
351                 expand(p0"  .text\n");
352         }
gmcgarry
1.170
353 #endif
354
ragge
1.118
355         jmplab = getlab2();
ragge
1.29
356         expand(p0"  pushl UL\n      pushl AL\n");
357         expand(p0"  fildq (%esp)\n");
358         expand(p0"  addl $8,%esp\n");
359         expand(p0"  cmpl $0,UL\n");
360         printf("        jge " LABFMT "\n"jmplab);
gmcgarry
1.170
361
362 #if defined(ELFABI)
ragge
1.161
363         printf("        fldt " LABFMT "%s\n"loadlabkflag ? "@GOTOFF" : "");
gmcgarry
1.170
364 #elif defined(MACHOABI)
365         printf("\tpushl 0x5f800000\n");
366         printf("\tfadds (%%esp)\n");
367         printf("\taddl $4,%%esp\n");
gmcgarry
1.164
368 #else
369 #error incomplete implementation
370 #endif
gmcgarry
1.170
371
372         printf("        faddp %%st,%%st(1)\n");
373         printf(LABFMT ":\n"jmplab);
ragge
1.29
374 }
375
ragge
1.69
376 static int
377 argsiz(NODE *p)
378 {
379         TWORD t = p->n_type;
380
ragge
1.81
381         if (t < LONGLONG || t == FLOAT || t > BTMASK)
ragge
1.69
382                 return 4;
383         if (t == LONGLONG || t == ULONGLONG || t == DOUBLE)
384                 return 8;
385         if (t == LDOUBLE)
386                 return 12;
ragge
1.81
387         if (t == STRTY || t == UNIONTY)
ragge
1.143
388                 return (p->n_stsize+3) & ~3;
ragge
1.69
389         comperr("argsiz");
390         return 0;
391 }
392
ragge
1.133
393 static void
394 fcast(NODE *p)
395 {
396         TWORD t = p->n_type;
397         int szc;
398
399         if (t >= p->n_left->n_type)
400                 return/* cast to more precision */
401         if (t == FLOAT)
402                 sz = 4c = 's';
403         else
404                 sz = 8c = 'l';
405
406         printf("        sub $%d,%%esp\n"sz);
407         printf("        fstp%c (%%esp)\n"c);
408         printf("        fld%c (%%esp)\n"c);
409         printf("        add $%d,%%esp\n"sz);
410 }
411
ragge
1.151
412 static void
413 llshft(NODE *p)
414 {
415         char *d[3];
416
417         if (p->n_op == LS) {
418                 d[0] = "l"d[1] = "%eax"d[2] = "%edx";
419         } else
420                 d[0] = "r"d[1] = "%edx"d[2] = "%eax";
421
422         printf("\tsh%sdl %s,%s\n",d[0], d[1], d[2]);
423         printf("\ts%s%sl %%cl,%s\n"p->n_op == RS &&
424             p->n_left->n_type == ULONGLONG ? "h" : "a"d[0], d[1]);
425         printf("\ttestb $32,%%cl\n");
426         printf("\tje 1f\n");
427         printf("\tmovl %s,%s\n"d[1], d[2]);
428         if (p->n_op == RS && p->n_left->n_type == LONGLONG)
429                 printf("\tsarl $31,%%edx\n");
430         else
431                 printf("\txorl %s,%s\n",d[1],d[1]);
432         printf("1:\n");
433 }
434
ragge
1.1
435 void
436 zzzcode(NODE *pint c)
437 {
ragge
1.137
438         NODE *l;
ragge
1.162
439         int prlr;
ragge
1.69
440         char *ch;
ragge
1.1
441
442         switch (c) {
ragge
1.84
443         case 'A'/* swap st0 and st1 if right is evaluated second */
ragge
1.83
444                 if ((p->n_su & DORIGHT) == 0) {
445                         if (logop(p->n_op))
446                                 printf("        fxch\n");
447                         else
448                                 printf("r");
449                 }
450                 break;
ragge
1.9
451
ragge
1.12
452         case 'C':  /* remove from stack after subroutine call */
gmcgarry
1.111
453                 if (p->n_left->n_flags & FSTDCALL)
454                         break;
ragge
1.71
455                 pr = p->n_qual;
ragge
1.139
456                 if (p->n_flags & FFPPOP)
457                         printf("        fstp    %%st(0)\n");
ragge
1.69
458                 if (p->n_op == UCALL)
459                         return/* XXX remove ZC from UCALL */
ragge
1.71
460                 if (pr)
461                         printf("        addl $%d, %s\n"prrnames[ESP]);
ragge
1.12
462                 break;
ragge
1.17
463
464         case 'D'/* Long long comparision */
465                 twollcomp(p);
466                 break;
ragge
1.12
467
ragge
1.23
468         case 'F'/* Structure argument */
ragge
1.71
469                 if (p->n_stalign != 0/* already on stack */
470                         starg(p);
ragge
1.23
471                 break;
472
ragge
1.25
473         case 'G'/* Floating point compare */
474                 fcomp(p);
475                 break;
476
ragge
1.141
477         case 'H'/* assign of longlong between regs */
478                 rmove(DECRA(p->n_right->n_reg0),
479                     DECRA(p->n_left->n_reg0), LONGLONG);
480                 break;
481
ragge
1.133
482         case 'I'/* float casts */
483                 fcast(p);
484                 break;
485
ragge
1.29
486         case 'J'/* convert unsigned long long to floating point */
487                 ulltofp(p);
488                 break;
489
ragge
1.144
490         case 'K'/* Load longlong reg into another reg */
491                 rmove(regno(p), DECRA(p->n_reg0), LONGLONG);
492                 break;
493
ragge
1.59
494         case 'M'/* Output sconv move, if needed */
495                 l = getlr(p'L');
496                 /* XXX fixneed: regnum */
ragge
1.68
497                 pr = DECRA(p->n_reg0);
498                 lr = DECRA(l->n_reg0);
ragge
1.66
499                 if ((pr == AL && lr == EAX) || (pr == BL && lr == EBX) ||
500                     (pr == CL && lr == ECX) || (pr == DL && lr == EDX))
ragge
1.59
501                         ;
502                 else
ragge
1.66
503                         printf("        movb %%%cl,%s\n",
504                             rnames[lr][2], rnames[pr]);
ragge
1.59
505                 l->n_rval = l->n_reg = p->n_reg/* XXX - not pretty */
506                 break;
ragge
1.67
507
508         case 'N'/* output extended reg name */
509                 printf("%s"rnames[getlr(p'1')->n_rval]);
510                 break;
511
ragge
1.69
512         case 'O'/* print out emulated ops */
ragge
1.76
513                 pr = 16;
514                 if (p->n_op == RS || p->n_op == LS) {
ragge
1.151
515                         llshft(p);
516                         break;
ragge
1.153
517                 } else if (p->n_op == MUL) {
518                         printf("\timull %%ecx, %%edx\n");
519                         printf("\timull %%eax, %%esi\n");
520                         printf("\taddl %%edx, %%esi\n");
521                         printf("\tmull %%ecx\n");
522                         printf("\taddl %%esi, %%edx\n");
523                         break;
ragge
1.151
524                 }
525                 expand(pINCREG"\tpushl UR\n\tpushl AR\n");
ragge
1.69
526                 expand(pINCREG"\tpushl UL\n\tpushl AL\n");
gmcgarry
1.113
527                 if (p->n_op == DIV && p->n_type == ULONGLONGch = "udiv";
528                 else if (p->n_op == DIVch = "div";
529                 else if (p->n_op == MOD && p->n_type == ULONGLONGch = "umod";
530                 else if (p->n_op == MODch = "mod";
ragge
1.69
531                 else ch = 0comperr("ZO");
gmcgarry
1.142
532 #ifdef ELFABI
ragge
1.140
533                 printf("\tcall " EXPREFIX "__%sdi3%s\n\taddl $%d,%s\n",
534                         ch, (kflag ? "@PLT" : ""), prrnames[ESP]);
gmcgarry
1.142
535 #else
536                 printf("\tcall " EXPREFIX "__%sdi3\n\taddl $%d,%s\n",
537                         chprrnames[ESP]);
538 #endif
ragge
1.69
539                 break;
540
ragge
1.71
541         case 'Q'/* emit struct assign */
ragge
1.130
542                 /*
ragge
1.172
543                  * Put out some combination of movs{b,w,l}
ragge
1.130
544                  * esi/edi/ecx are available.
545                  */
ragge
1.172
546                 expand(pINAREG"     leal AL,%edi\n");
547                 if (p->n_stsize < 32) {
548                         int i = p->n_stsize >> 2;
549                         while (i) {
550                                 expand(pINAREG"     movsl\n");
551                                 i--;
ragge
1.130
552                         }
ragge
1.172
553                 } else {
554                         printf("\tmovl $%d,%%ecx\n"p->n_stsize >> 2);
555                         printf("        rep movsl\n");
gmcgarry
1.122
556                 }
ragge
1.172
557                 if (p->n_stsize & 2)
558                         printf("        movsw\n");
559                 if (p->n_stsize & 1)
560                         printf("        movsb\n");
ragge
1.71
561                 break;
562
ragge
1.73
563         case 'S'/* emit eventual move after cast from longlong */
564                 pr = DECRA(p->n_reg0);
565                 lr = p->n_left->n_rval;
566                 switch (p->n_type) {
567                 case CHAR:
568                 case UCHAR:
ragge
1.75
569                         if (rnames[pr][2] == 'l' && rnames[lr][2] == 'x' &&
570                             rnames[pr][1] == rnames[lr][1])
571                                 break;
572                         if (rnames[lr][2] == 'x') {
573                                 printf("\tmovb %%%cl,%s\n",
574                                     rnames[lr][1], rnames[pr]);
575                                 break;
576                         }
ragge
1.87
577                         /* Must go via stack */
ragge
1.162
578                         expand(pINAREG"\tmovl AL,A2\n");
579                         expand(pINBREG"\tmovb A2,A1\n");
580 #ifdef notdef
581                         /* cannot use freetemp() in instruction emission */
ragge
1.87
582                         s = BITOOR(freetemp(1));
583                         printf("\tmovl %%e%ci,%d(%%ebp)\n"rnames[lr][1], s);
584                         printf("\tmovb %d(%%ebp),%s\n"srnames[pr]);
ragge
1.162
585 #endif
ragge
1.73
586                         break;
ragge
1.74
587
ragge
1.79
588                 case SHORT:
589                 case USHORT:
590                         if (rnames[lr][1] == rnames[pr][2] &&
591                             rnames[lr][2] == rnames[pr][3])
592                                 break;
593                         printf("\tmovw %%%c%c,%%%s\n",
594                             rnames[lr][1], rnames[lr][2], rnames[pr]+2);
595                         break;
ragge
1.74
596                 case INT:
597                 case UNSIGNED:
ragge
1.78
598                         if (rnames[lr][1] == rnames[pr][2] &&
599                             rnames[lr][2] == rnames[pr][3])
600                                 break;
601                         printf("\tmovl %%e%c%c,%s\n",
602                                     rnames[lr][1], rnames[lr][2], rnames[pr]);
ragge
1.74
603                         break;
604
ragge
1.73
605                 default:
ragge
1.78
606                         if (rnames[lr][1] == rnames[pr][2] &&
607                             rnames[lr][2] == rnames[pr][3])
608                                 break;
609                         comperr("SCONV2 %s->%s"rnames[lr], rnames[pr]);
ragge
1.73
610                         break;
611                 }
612                 break;
613
ragge
1.1
614         default:
ragge
1.14
615                 comperr("zzzcode %c"c);
ragge
1.1
616         }
617 }
618
619 int canaddr(NODE *);
620 int
621 canaddr(NODE *p)
622 {
623         int o = p->n_op;
624
625         if (o==NAME || o==REG || o==ICON || o==OREG ||
ragge
1.114
626             (o==UMUL && shumul(p->n_leftSOREG)))
ragge
1.1
627                 return(1);
628         return(0);
629 }
630
ragge
1.21
631 /*
632  * Does the bitfield shape match?
633  */
ragge
1.1
634 int
635 flshape(NODE *p)
636 {
ragge
1.160
637         comperr("flshape");
638         return 0;
ragge
1.1
639 }
640
641 /* INTEMP shapes must not contain any temporary registers */
ragge
1.15
642 /* XXX should this go away now? */
ragge
1.1
643 int
644 shtemp(NODE *p)
645 {
ragge
1.15
646         return 0;
647 #if 0
ragge
1.1
648         int r;
649
650         if (p->n_op == STARG )
651                 p = p->n_left;
652
653         switch (p->n_op) {
654         case REG:
655                 return (!istreg(p->n_rval));
656
657         case OREG:
658                 r = p->n_rval;
659                 if (R2TEST(r)) {
660                         if (istreg(R2UPK1(r)))
661                                 return(0);
662                         r = R2UPK2(r);
663                 }
664                 return (!istreg(r));
665
ragge
1.12
666         case UMUL:
ragge
1.1
667                 p = p->n_left;
ragge
1.12
668                 return (p->n_op != UMUL && shtemp(p));
ragge
1.1
669         }
670
671         if (optype(p->n_op) != LTYPE)
672                 return(0);
673         return(1);
ragge
1.15
674 #endif
ragge
1.1
675 }
676
677 void
678 adrcon(CONSZ val)
679 {
ragge
1.2
680         printf("$" CONFMTval);
ragge
1.1
681 }
682
683 void
ragge
1.14
684 conput(FILE *fpNODE *p)
ragge
1.1
685 {
gmcgarry
1.128
686         int val = (int)p->n_lval;
ragge
1.17
687
ragge
1.1
688         switch (p->n_op) {
689         case ICON:
ragge
1.17
690                 if (p->n_name[0] != '\0') {
ragge
1.14
691                         fprintf(fp"%s"p->n_name);
ragge
1.17
692                         if (val)
693                                 fprintf(fp"+%d"val);
694                 } else
695                         fprintf(fp"%d"val);
ragge
1.1
696                 return;
697
698         default:
ragge
1.74
699                 comperr("illegal conput, p %p"p);
ragge
1.1
700         }
701 }
702
703 /*ARGSUSED*/
704 void
705 insput(NODE *p)
706 {
ragge
1.14
707         comperr("insput");
ragge
1.1
708 }
709
710 /*
711  * Write out the upper address, like the upper register of a 2-register
712  * reference, or the next memory location.
713  */
714 void
715 upput(NODE *pint size)
716 {
717
ragge
1.17
718         size /= SZCHAR;
ragge
1.1
719         switch (p->n_op) {
720         case REG:
plunky
1.169
721                 printf("%%%s", &rnames[p->n_rval][3]);
ragge
1.1
722                 break;
723
724         case NAME:
725         case OREG:
726                 p->n_lval += size;
ragge
1.12
727                 adrput(stdoutp);
ragge
1.1
728                 p->n_lval -= size;
729                 break;
730         case ICON:
plunky
1.169
731                 printf("$" CONFMTp->n_lval >> 32);
ragge
1.1
732                 break;
733         default:
ragge
1.14
734                 comperr("upput bad op %d size %d"p->n_opsize);
ragge
1.1
735         }
736 }
737
738 void
ragge
1.12
739 adrput(FILE *ioNODE *p)
ragge
1.1
740 {
741         int r;
742         /* output an address, with offsets, from p */
743
744         switch (p->n_op) {
745
746         case NAME:
ragge
1.88
747                 if (p->n_name[0] != '\0') {
ragge
1.13
748                         fputs(p->n_nameio);
ragge
1.88
749                         if (p->n_lval != 0)
750                                 fprintf(io"+" CONFMTp->n_lval);
751                 } else
752                         fprintf(ioCONFMTp->n_lval);
ragge
1.1
753                 return;
754
755         case OREG:
756                 r = p->n_rval;
ragge
1.92
757                 if (p->n_name[0])
758                         printf("%s%s"p->n_namep->n_lval ? "+" : "");
ragge
1.17
759                 if (p->n_lval)
760                         fprintf(io"%d", (int)p->n_lval);
ragge
1.82
761                 if (R2TEST(r)) {
762                         fprintf(io"(%s,%s,4)"rnames[R2UPK1(r)],
763                             rnames[R2UPK2(r)]);
764                 } else
765                         fprintf(io"(%s)"rnames[p->n_rval]);
ragge
1.1
766                 return;
767         case ICON:
ragge
1.97
768 #ifdef PCC_DEBUG
769                 /* Sanitycheck for PIC, to catch adressable constants */
ragge
1.119
770                 if (kflag && p->n_name[0] && 0) {
ragge
1.97
771                         static int foo;
772
773                         if (foo++ == 0) {
774                                 printf("\nfailing...\n");
775                                 fwalk(pe2print0);
776                                 comperr("pass2 conput");
777                         }
778                 }
779 #endif
ragge
1.1
780                 /* addressable value of the constant */
ragge
1.14
781                 fputc('$'io);
782                 conput(iop);
ragge
1.1
783                 return;
784
785         case REG:
ragge
1.66
786                 switch (p->n_type) {
787                 case LONGLONG:
788                 case ULONGLONG:
ragge
1.63
789                         fprintf(io"%%%c%c%c"rnames[p->n_rval][0],
790                             rnames[p->n_rval][1], rnames[p->n_rval][2]);
ragge
1.66
791                         break;
792                 case SHORT:
793                 case USHORT:
794                         fprintf(io"%%%s", &rnames[p->n_rval][2]);
795                         break;
796                 default:
ragge
1.63
797                         fprintf(io"%s"rnames[p->n_rval]);
ragge
1.66
798                 }
ragge
1.1
799                 return;
800
801         default:
ragge
1.26
802                 comperr("illegal address, op %d, node %p"p->n_opp);
ragge
1.1
803                 return;
804
805         }
806 }
807
ragge
1.6
808 static char *
809 ccbranches[] = {
810         "je",           /* jumpe */
811         "jne",          /* jumpn */
812         "jle",          /* jumple */
813         "jl",           /* jumpl */
814         "jge",          /* jumpge */
815         "jg",           /* jumpg */
816         "jbe",          /* jumple (jlequ) */
817         "jb",           /* jumpl (jlssu) */
818         "jae",          /* jumpge (jgequ) */
819         "ja",           /* jumpg (jgtru) */
820 };
821
ragge
1.1
822
823 /*   printf conditional and unconditional branches */
824 void
ragge
1.6
825 cbgen(int oint lab)
ragge
1.1
826 {
ragge
1.6
827         if (o < EQ || o > UGT)
ragge
1.14
828                 comperr("bad conditional branch: %s"opst[o]);
ragge
1.6
829         printf("        %s " LABFMT "\n"ccbranches[o-EQ], lab);
ragge
1.1
830 }
831
ragge
1.28
832 static void
ragge
1.116
833 fixcalls(NODE *pvoid *arg)
ragge
1.28
834 {
ragge
1.83
835         /* Prepare for struct return by allocating bounce space on stack */
ragge
1.71
836         switch (p->n_op) {
837         case STCALL:
838         case USTCALL:
ragge
1.77
839                 if (p->n_stsize+p2autooff > stkpos)
840                         stkpos = p->n_stsize+p2autooff;
ragge
1.71
841                 break;
ragge
1.152
842         case LS:
843         case RS:
844                 if (p->n_type != LONGLONG && p->n_type != ULONGLONG)
845                         break;
846                 if (p->n_right->n_op == ICON/* constants must be char */
847                         p->n_right->n_type = CHAR;
848                 break;
ragge
1.71
849         }
ragge
1.83
850 }
851
852 /*
853  * Must store floats in memory if there are two function calls involved.
854  */
855 static int
856 storefloat(struct interpass *ipNODE *p)
857 {
858         int lr;
859
860         switch (optype(p->n_op)) {
861         case BITYPE:
862                 l = storefloat(ipp->n_left);
863                 r = storefloat(ipp->n_right);
864                 if (p->n_op == CM)
865                         return 0/* arguments, don't care */
866                 if (callop(p->n_op))
867                         return 1/* found one */
868 #define ISF(p) ((p)->n_type == FLOAT || (p)->n_type == DOUBLE || \
869         (p)->n_type == LDOUBLE)
870                 if (ISF(p->n_left) && ISF(p->n_right) && l && r) {
871                         /* must store one. store left */
872                         struct interpass *nip;
873                         TWORD t = p->n_left->n_type;
874                         NODE *ll;
875                         int off;
876
877                         off = BITOOR(freetemp(szty(t)));
878                         ll = mklnode(OREGoffFPREGt);
879                         nip = ipnode(mkbinode(ASSIGNllp->n_leftt));
880                         p->n_left = mklnode(OREGoffFPREGt);
881                         DLIST_INSERT_BEFORE(ipnipqelem);
882                 }
883                 return l|r;
884
885         case UTYPE:
886                 l = storefloat(ipp->n_left);
887                 if (callop(p->n_op))
888                         l = 1;
889                 return l;
890         default:
891                 return 0;
892         }
ragge
1.28
893 }
ragge
1.1
894
ragge
1.132
895 static void
896 outfargs(struct interpass *ipNODE **aryint numint *cwpint c)
897 {
898         struct interpass *ip2;
899         NODE *q, *r;
900         int i;
901
902         for (i = 0i < numi++)
903                 if (XASMVAL(cwp[i]) == c && (cwp[i] & (XASMASG|XASMINOUT)))
904                         break;
905         if (i == num)
906                 return;
907         q = ary[i]->n_left;
908         r = mklnode(REG0c == 'u' ? 040 : 037q->n_type);
909         ary[i]->n_left = tcopy(r);
910         ip2 = ipnode(mkbinode(ASSIGNqrq->n_type));
911         DLIST_INSERT_AFTER(ipip2qelem);
912 }
913
914 static void
915 infargs(struct interpass *ipNODE **aryint numint *cwpint c)
916 {
917         struct interpass *ip2;
918         NODE *q, *r;
919         int i;
920
921         for (i = 0i < numi++)
922                 if (XASMVAL(cwp[i]) == c && (cwp[i] & XASMASG) == 0)
923                         break;
924         if (i == num)
925                 return;
926         q = ary[i]->n_left;
927         q = (cwp[i] & XASMINOUT) ? tcopy(q) : q;
928         r = mklnode(REG0c == 'u' ? 040 : 037q->n_type);
929         if ((cwp[i] & XASMINOUT) == 0)
930                 ary[i]->n_left = tcopy(r);
931         ip2 = ipnode(mkbinode(ASSIGNrqq->n_type));
932         DLIST_INSERT_BEFORE(ipip2qelem);
933 }
934
935 /*
936  * Extract float args to XASM and ensure that they are put on the stack
937  * in correct order.
938  * This should be done sow other way.
939  */
940 static void
941 fixxfloat(struct interpass *ipNODE *p)
942 {
943         NODE *w, **ary;
944         int nnic, *cwp;
945
946         nn = 1;
947         w = p->n_left;
948         if (w->n_op == ICON && w->n_type == STRTY)
949                 return;
950         /* index all xasm args first */
951         for (; w->n_op == CMw = w->n_left)
952                 nn++;
953         ary = tmpcalloc(nn * sizeof(NODE *));
954         cwp = tmpcalloc(nn * sizeof(int));
955         for (i = 0w = p->n_leftw->n_op == CMw = w->n_left) {
956                 ary[i] = w->n_right;
957                 cwp[i] = xasmcode(ary[i]->n_name);
958                 i++;
959         }
960         ary[i] = w;
961         cwp[i] = xasmcode(ary[i]->n_name);
962         for (i = 0i < nni++)
963                 if (XASMVAL(cwp[i]) == 't' || XASMVAL(cwp[i]) == 'u')
964                         break;
965         if (i == nn)
966                 return;
967
968         for (i = 0i < nni++) {
969                 c = XASMVAL(cwp[i]);
970                 if (c >= '0' && c <= '9')
971                         cwp[i] = (cwp[i] & ~0377) | XASMVAL(cwp[c-'0']);
972         }
973         infargs(iparynncwp'u');