Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20140524151953

Diff

Diff from 1.172 to:

Annotations

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

Annotated File View

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