Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20100919205515

Diff

Diff from 1.19 to:

Annotations

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

Annotated File View

ragge
1.19
1 /*      $Id: local2.c,v 1.19 2010/09/19 20:55:15 ragge Exp $    */
mickey
1.1
2 /*
3  * Copyright (c) 2008 Michael Shalayeff
4  * Copyright (c) 2003 Anders Magnusson (ragge@ludd.luth.se).
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 # include "pass2.h"
31 # include <ctype.h>
32 # include <string.h>
33
34 static int stkpos;
35
36 void
37 deflab(int label)
38 {
39         printf(LABFMT ":\n"label);
40 }
41
ragge
1.11
42 static int regoff[MAXREGS];
mickey
1.1
43 static TWORD ftype;
ragge
1.4
44 char *rbyte[], *rshort[], *rlong[];
mickey
1.1
45
46 /*
47  * Print out the prolog assembler.
48  * addto and regoff are already calculated.
49  */
50 static void
51 prtprolog(struct interpass_prolog *ippint addto)
52 {
53         int i;
54
ragge
1.5
55         /* XXX should look if there is any need to emit this */
mickey
1.2
56         printf("\tpushq %%rbp\n");
57         printf("\tmovq %%rsp,%%rbp\n");
ragge
1.10
58         addto = (addto+15) & ~15/* 16-byte aligned */
mickey
1.1
59         if (addto)
mickey
1.2
60                 printf("\tsubq $%d,%%rsp\n"addto);
ragge
1.10
61
62         /* save permanent registers */
mickey
1.1
63         for (i = 0i < MAXREGSi++)
ragge
1.10
64                 if (TESTBIT(ipp->ipp_regsi))
ragge
1.11
65                         fprintf(stdout"\tmovq %s,-%d(%s)\n",
mickey
1.1
66                             rnames[i], regoff[i], rnames[FPREG]);
ragge
1.4
67 }
mickey
1.1
68
69 /*
70  * calculate stack size and offsets
71  */
72 static int
73 offcalc(struct interpass_prolog *ipp)
74 {
75         int iaddto;
76
77         addto = p2maxautooff;
78         if (addto >= AUTOINIT/SZCHAR)
79                 addto -= AUTOINIT/SZCHAR;
80         for (i = 0i < MAXREGSi++)
81                 if (TESTBIT(ipp->ipp_regsi)) {
ragge
1.10
82                         addto += SZLONG/SZCHAR;
mickey
1.1
83                         regoff[i] = addto;
84                 }
85         return addto;
86 }
87
88 void
89 prologue(struct interpass_prolog *ipp)
90 {
91         int addto;
92
93         ftype = ipp->ipp_type;
94
95 #ifdef LANG_F77
96         if (ipp->ipp_vis)
97                 printf("        .globl %s\n"ipp->ipp_name);
ragge
1.10
98         printf("        .align 16\n");
mickey
1.1
99         printf("%s:\n"ipp->ipp_name);
100 #endif
101         /*
102          * We here know what register to save and how much to 
103          * add to the stack.
104          */
105         addto = offcalc(ipp);
106         prtprolog(ippaddto);
107 }
108
109 void
110 eoftn(struct interpass_prolog *ipp)
111 {
112         int i;
113
114         if (ipp->ipp_ip.ip_lbl == 0)
115                 return/* no code needs to be generated */
116
117         /* return from function code */
118         for (i = 0i < MAXREGSi++)
119                 if (TESTBIT(ipp->ipp_regsi))
ragge
1.10
120                         fprintf(stdout"       movq -%d(%s),%s\n",
mickey
1.1
121                             regoff[i], rnames[FPREG], rnames[i]);
122
123         /* struct return needs special treatment */
124         if (ftype == STRTY || ftype == UNIONTY) {
125                 printf("        movl 8(%%ebp),%%eax\n");
126                 printf("        leave\n");
127                 printf("        ret $%d\n"4);
128         } else {
129                 printf("        leave\n");
ragge
1.11
130                 printf("        ret\n");
mickey
1.1
131         }
ragge
1.10
132         printf("\t.size %s,.-%s\n"ipp->ipp_nameipp->ipp_name);
mickey
1.1
133 }
134
135 /*
136  * add/sub/...
137  *
138  * Param given:
139  */
140 void
141 hopcode(int fint o)
142 {
143         char *str;
144
145         switch (o) {
146         case PLUS:
147                 str = "add";
148                 break;
149         case MINUS:
150                 str = "sub";
151                 break;
152         case AND:
153                 str = "and";
154                 break;
155         case OR:
156                 str = "or";
157                 break;
158         case ER:
159                 str = "xor";
160                 break;
161         default:
162                 comperr("hopcode2: %d"o);
163                 str = 0/* XXX gcc */
164         }
165         printf("%s%c"strf);
166 }
167
168 /*
169  * Return type size in bytes.  Used by R2REGS, arg 2 to offset().
170  */
171 int
mickey
1.2
172 tlen(NODE *p)
mickey
1.1
173 {
174         switch(p->n_type) {
175                 case CHAR:
176                 case UCHAR:
177                         return(1);
178
179                 case SHORT:
180                 case USHORT:
181                         return(SZSHORT/SZCHAR);
182
183                 case DOUBLE:
184                         return(SZDOUBLE/SZCHAR);
185
186                 case INT:
187                 case UNSIGNED:
188                         return(SZINT/SZCHAR);
189
190                 case LONG:
191                 case ULONG:
192                 case LONGLONG:
193                 case ULONGLONG:
194                         return SZLONGLONG/SZCHAR;
195
196                 default:
197                         if (!ISPTR(p->n_type))
198                                 comperr("tlen type %d not pointer");
199                         return SZPOINT(p->n_type)/SZCHAR;
200                 }
201 }
202
ragge
1.19
203 /*
204  * Compare two floating point numbers.
205  */
206 static void
207 fcomp(NODE *p)  
208 {
209
210         if (p->n_left->n_op == REG) {
211                 if (p->n_su & DORIGHT)
212                         expand(p0"  fxch\n");
213                 expand(p0"  fucompp\n");    /* emit compare insn  */
214         } else
215                 comperr("bad compare %p\n"p);
216         expand(p0"  fnstsw %ax\n"); /* move status reg to ax */
217         
218         switch (p->n_op) {
219         case EQ:
220                 expand(p0"  andb $69,%ah\n  xorb $64,%ah\n  je LC\n");
221                 break;
222         case NE:
223                 expand(p0"  andb $69,%ah\n  xorb $64,%ah\n  jne LC\n");
224                 break;
225         case LE:
226                 expand(p0"  andb $69,%ah\n  xorb $1,%ah\n   jne LC\n");
227                 break;
228         case LT:
229                 expand(p0"  andb $69,%ah\n  je LC\n");
230                 break;
231         case GT:
232                 expand(p0"  andb $1,%ah\n   jne LC\n");
233                 break;
234         case GE:
235                 expand(p0"  andb $69,%ah\n  jne LC\n");
236                 break;
237         default:
238                 comperr("fcomp op %d\n"p->n_op);
239         }
240 }
241
242
gmcgarry
1.3
243 #if 0
mickey
1.1
244 /*
245  * Emit code to compare two longlong numbers.
246  */
247 static void
248 twollcomp(NODE *p)
249 {
250         int o = p->n_op;
mickey
1.2
251         int s = getlab2();
mickey
1.1
252         int e = p->n_label;
253         int cb1cb2;
254
255         if (o >= ULE)
256                 o -= (ULE-LE);
257         switch (o) {
258         case NE:
259                 cb1 = 0;
260                 cb2 = NE;
261                 break;
262         case EQ:
263                 cb1 = NE;
264                 cb2 = 0;
265                 break;
266         case LE:
267         case LT:
268                 cb1 = GT;
269                 cb2 = LT;
270                 break;
271         case GE:
272         case GT:
273                 cb1 = LT;
274                 cb2 = GT;
275                 break;
276         
277         default:
278                 cb1 = cb2 = 0/* XXX gcc */
279         }
280         if (p->n_op >= ULE)
281                 cb1 += 4cb2 += 4;
282         expand(p0"  cmpl UR,UL\n");
283         if (cb1cbgen(cb1s);
284         if (cb2cbgen(cb2e);
285         expand(p0"  cmpl AR,AL\n");
286         cbgen(p->n_ope);
287         deflab(s);
288 }
gmcgarry
1.3
289 #endif
mickey
1.1
290
291 int
292 fldexpand(NODE *pint cookiechar **cp)
293 {
294         CONSZ val;
295
296         if (p->n_op == ASSIGN)
297                 p = p->n_left;
298         switch (**cp) {
299         case 'S':
300                 printf("%d"UPKFSZ(p->n_rval));
301                 break;
302         case 'H':
303                 printf("%d"UPKFOFF(p->n_rval));
304                 break;
305         case 'M':
306         case 'N':
307                 val = (CONSZ)1 << UPKFSZ(p->n_rval);
308                 --val;
309                 val <<= UPKFOFF(p->n_rval);
ragge
1.13
310                 if (p->n_type > UNSIGNED)
311                         printf("0x%llx", (**cp == 'M' ? val : ~val));
312                 else
313                         printf("0x%llx", (**cp == 'M' ? val : ~val)&0xffffffff);
mickey
1.1
314                 break;
315         default:
316                 comperr("fldexpand");
317         }
318         return 1;
319 }
320
321 static void
322 bfext(NODE *p)
323 {
324         int ch = 0sz = 0;
325
326         if (ISUNSIGNED(p->n_right->n_type))
327                 return;
328         switch (p->n_right->n_type) {
329         case CHAR:
330                 ch = 'b';
331                 sz = 8;
332                 break;
333         case SHORT:
334                 ch = 'w';
335                 sz = 16;
336                 break;
337         case INT:
338                 ch = 'l';
339                 sz = 32;
340                 break;
341         case LONG:
342                 ch = 'q';
343                 sz = 64;
344                 break;
345         default:
346                 comperr("bfext");
347         }
348
349         sz -= UPKFSZ(p->n_left->n_rval);
350         printf("\tshl%c $%d,"chsz);
351         adrput(stdoutgetlr(p'D'));
352         printf("\n\tsar%c $%d,"chsz);
353         adrput(stdoutgetlr(p'D'));
354         printf("\n");
355 }
ragge
1.11
356 #if 0
mickey
1.1
357
358 /*
359  * Push a structure on stack as argument.
360  * the scratch registers are already free here
361  */
362 static void
363 starg(NODE *p)
364 {
365         FILE *fp = stdout;
366
367         fprintf(fp"   subl $%d,%%esp\n"p->n_stsize);
368         fprintf(fp"   pushl $%d\n"p->n_stsize);
369         expand(p0"  pushl AL\n");
370         expand(p0"  leal 8(%esp),A1\n");
371         expand(p0"  pushl A1\n");
372         fprintf(fp"   call memcpy\n");
373         fprintf(fp"   addl $12,%%esp\n");
374 }
375
ragge
1.7
376 #endif
mickey
1.1
377 /*
ragge
1.7
378  * Generate code to convert an unsigned long to xmm float/double.
mickey
1.1
379  */
380 static void
ragge
1.7
381 ultofd(NODE *p)
mickey
1.1
382 {
383
ragge
1.7
384 #define E(x)    expand(p, 0, x)
385         E("     movq AL,A1\n");
ragge
1.9
386         E("     testq A1,A1\n");
ragge
1.7
387         E("     js 2f\n");
388         E("     cvtsi2sZfq A1,A3\n");
389         E("     jmp 3f\n");
390         E("2:\n");
391         E("     movq A1,A2\n");
392         E("     shrq A2\n");
ragge
1.9
393         E("     andq $1,A1\n");
ragge
1.7
394         E("     orq A1,A2\n");
395         E("     cvtsi2sZfq A2,A3\n");
396         E("     addsZf A3,A3\n");
397         E("3:\n");
398 #undef E
mickey
1.1
399 }
400
401 static int
402 argsiz(NODE *p)
403 {
404         TWORD t = p->n_type;
405
ragge
1.10
406         if (p->n_left->n_op == REG)
407                 return 0/* not on stack */
mickey
1.1
408         if (t == LDOUBLE)
409                 return 16;
410         if (t == STRTY || t == UNIONTY)
411                 return p->n_stsize;
ragge
1.10
412         return 8;
mickey
1.1
413 }
414
415 void
416 zzzcode(NODE *pint c)
417 {
ragge
1.6
418         NODE *l;
ragge
1.7
419         int prlrs;
ragge
1.14
420         char **rt;
mickey
1.1
421
422         switch (c) {
ragge
1.4
423 #if 0
mickey
1.1
424         case 'A'/* swap st0 and st1 if right is evaluated second */
425                 if ((p->n_su & DORIGHT) == 0) {
426                         if (logop(p->n_op))
427                                 printf("        fxch\n");
428                         else
429                                 printf("r");
430                 }
431                 break;
ragge
1.5
432 #endif
mickey
1.1
433
434         case 'C':  /* remove from stack after subroutine call */
435                 pr = p->n_qual;
436                 if (p->n_op == UCALL)
437                         return/* XXX remove ZC from UCALL */
438                 if (pr)
ragge
1.10
439                         printf("        addq $%d, %s\n"prrnames[RSP]);
mickey
1.1
440                 break;
441
442         case 'E'/* Perform bitfield sign-extension */
443                 bfext(p);
444                 break;
445
ragge
1.11
446 #if 0
mickey
1.1
447         case 'F'/* Structure argument */
448                 if (p->n_stalign != 0/* already on stack */
449                         starg(p);
450                 break;
ragge
1.7
451 #endif
ragge
1.19
452
453         case 'G'/* Floating point compare */
454                 fcomp(p);
455                 break;
456
ragge
1.7
457         case 'j'/* convert unsigned long to f/d */
458                 ultofd(p);
mickey
1.1
459                 break;
460
461         case 'M'/* Output sconv move, if needed */
462                 l = getlr(p'L');
463                 /* XXX fixneed: regnum */
464                 pr = DECRA(p->n_reg0);
465                 lr = DECRA(l->n_reg0);
ragge
1.6
466                 if (pr == lr)
467                         break;
468                 printf("        movb %s,%s\n"rbyte[lr], rbyte[pr]);
mickey
1.1
469                 l->n_rval = l->n_reg = p->n_reg/* XXX - not pretty */
470                 break;
471
ragge
1.6
472 #if 0
mickey
1.1
473         case 'N'/* output extended reg name */
474                 printf("%s"rnames[getlr(p'1')->n_rval]);
475                 break;
ragge
1.16
476 #endif
477
478         case 'P'/* Put hidden argument in rdi */
479                 printf("\tleaq -%d(%%rbp),%%rdi\n"stkpos);
480                 break;
481
482 #if 0
mickey
1.1
483
484         case 'S'/* emit eventual move after cast from longlong */
485                 pr = DECRA(p->n_reg0);
486                 lr = p->n_left->n_rval;
487                 switch (p->n_type) {
488                 case CHAR:
489                 case UCHAR:
490                         if (rnames[pr][2] == 'l' && rnames[lr][2] == 'x' &&
491                             rnames[pr][1] == rnames[lr][1])
492                                 break;
493                         if (rnames[lr][2] == 'x') {
494                                 printf("\tmovb %%%cl,%s\n",
495                                     rnames[lr][1], rnames[pr]);
496                                 break;
497                         }
498                         /* Must go via stack */
499                         s = BITOOR(freetemp(1));
500                         printf("\tmovl %%e%ci,%d(%%rbp)\n"rnames[lr][1], s);
501                         printf("\tmovb %d(%%rbp),%s\n"srnames[pr]);
502                         comperr("SCONV1 %s->%s"rnames[lr], rnames[pr]);
503                         break;
504
505                 case SHORT:
506                 case USHORT:
507                         if (rnames[lr][1] == rnames[pr][2] &&
508                             rnames[lr][2] == rnames[pr][3])
509                                 break;
510                         printf("\tmovw %%%c%c,%%%s\n",
511                             rnames[lr][1], rnames[lr][2], rnames[pr]+2);
512                         comperr("SCONV2 %s->%s"rnames[lr], rnames[pr]);
513                         break;
514                 case INT:
515                 case UNSIGNED:
516                         if (rnames[lr][1] == rnames[pr][2] &&
517                             rnames[lr][2] == rnames[pr][3])
518                                 break;
519                         printf("\tmovl %%e%c%c,%s\n",
520                                     rnames[lr][1], rnames[lr][2], rnames[pr]);
521                         comperr("SCONV3 %s->%s"rnames[lr], rnames[pr]);
522                         break;
523
524                 default:
525                         if (rnames[lr][1] == rnames[pr][2] &&
526                             rnames[lr][2] == rnames[pr][3])
527                                 break;
528                         comperr("SCONV4 %s->%s"rnames[lr], rnames[pr]);
529                         break;
530                 }
531                 break;
ragge
1.4
532 #endif
ragge
1.10
533         case 'Q'/* emit struct assign */
534                 /* XXX - optimize for small structs */
535                 printf("\tmovq $%d,%%rdx\n"p->n_stsize);
536                 expand(pINAREG"\tmovq AR,%rsi\n");
537                 expand(pINAREG"\tleaq AL,%rdi\n\n");
538                 printf("\tcall memcpy\n");
539                 break;
540
ragge
1.7
541         case 'R'/* print opname based on right type */
542         case 'L'/* print opname based on left type */
543                 switch (getlr(pc)->n_type) {
544                 case CHARcase UCHARs = 'b'break;
545                 case SHORTcase USHORTs = 'w'break;
546                 case INTcase UNSIGNEDs = 'l'break;
547                 defaults = 'q'break;
548                 printf("%c"s);
549                 }
550                 break;
ragge
1.14
551
552         case '8'/* special reg name printout (64-bit) */
553         case '1'/* special reg name printout (32-bit) */
ragge
1.7
554                 l = getlr(p'1');
ragge
1.14
555                 rt = c == '8' ? rnames : rlong;
556                 printf("%s"rt[l->n_rval]);
ragge
1.7
557                 break;
558
559         case 'g':
560                 p = p->n_left;
561                 /* FALLTHROUGH */
562         case 'f'/* float or double */
563                 printf("%c"p->n_type == FLOAT ? 's' : 'd');
564                 break;
565
566         case 'q'/* int or long */
567                 printf("%c"p->n_left->n_type == LONG ? 'q' : ' ');
568                 break;
569
mickey
1.1
570         default:
571                 comperr("zzzcode %c"c);
572         }
573 }
574
575 /*ARGSUSED*/
576 int
577 rewfld(NODE *p)
578 {
579         return(1);
580 }
581
582 int canaddr(NODE *);
583 int
584 canaddr(NODE *p)
585 {
586         int o = p->n_op;
587
588         if (o==NAME || o==REG || o==ICON || o==OREG ||
589             (o==UMUL && shumul(p->n_leftSOREG)))
590                 return(1);
591         return(0);
592 }
593
594 /*
595  * Does the bitfield shape match?
596  */
597 int
598 flshape(NODE *p)
599 {
600         int o = p->n_op;
601
602         if (o == OREG || o == REG || o == NAME)
603                 return SRDIR/* Direct match */
604         if (o == UMUL && shumul(p->n_leftSOREG))
605                 return SROREG/* Convert into oreg */
606         return SRREG/* put it into a register */
607 }
608
609 /* INTEMP shapes must not contain any temporary registers */
610 /* XXX should this go away now? */
611 int
612 shtemp(NODE *p)
613 {
614         return 0;
615 #if 0
616         int r;
617
618         if (p->n_op == STARG )
619                 p = p->n_left;
620
621         switch (p->n_op) {
622         case REG:
623                 return (!istreg(p->n_rval));
624
625         case OREG:
626                 r = p->n_rval;
627                 if (R2TEST(r)) {
628                         if (istreg(R2UPK1(r)))
629                                 return(0);
630                         r = R2UPK2(r);
631                 }
632                 return (!istreg(r));
633
634         case UMUL:
635                 p = p->n_left;
636                 return (p->n_op != UMUL && shtemp(p));
637         }
638
639         if (optype(p->n_op) != LTYPE)
640                 return(0);
641         return(1);
642 #endif
643 }
644
645 void
646 adrcon(CONSZ val)
647 {
648         printf("$" CONFMTval);
649 }
650
651 void
652 conput(FILE *fpNODE *p)
653 {
ragge
1.17
654         long val = p->n_lval;
mickey
1.1
655
656         switch (p->n_op) {
657         case ICON:
658                 if (p->n_name[0] != '\0') {
659                         fprintf(fp"%s"p->n_name);
660                         if (val)
ragge
1.17
661                                 fprintf(fp"+%ld"val);
mickey
1.1
662                 } else
ragge
1.17
663                         fprintf(fp"%ld"val);
mickey
1.1
664                 return;
665
666         default:
667                 comperr("illegal conput, p %p"p);
668         }
669 }
670
671 /*ARGSUSED*/
672 void
673 insput(NODE *p)
674 {
675         comperr("insput");
676 }
677
678 /*
679  * Write out the upper address, like the upper register of a 2-register
680  * reference, or the next memory location.
ragge
1.17
681  * XXX - not needed on amd64
mickey
1.1
682  */
683 void
684 upput(NODE *pint size)
685 {
686
687         size /= SZCHAR;
688         switch (p->n_op) {
689         case REG:
690                 fprintf(stdout"%%%s", &rnames[p->n_rval][3]);
691                 break;
692
693         case NAME:
694         case OREG:
695                 p->n_lval += size;
696                 adrput(stdoutp);
697                 p->n_lval -= size;
698                 break;
699         case ICON:
700                 fprintf(stdout"$" CONFMTp->n_lval >> 32);
701                 break;
702         default:
703                 comperr("upput bad op %d size %d"p->n_opsize);
704         }
705 }
706
707 void
708 adrput(FILE *ioNODE *p)
709 {
710         int r;
ragge
1.5
711         char **rc;
mickey
1.1
712         /* output an address, with offsets, from p */
713
714         if (p->n_op == FLD)
715                 p = p->n_left;
716
717         switch (p->n_op) {
718
719         case NAME:
720                 if (p->n_name[0] != '\0') {
721                         fputs(p->n_nameio);
722                         if (p->n_lval != 0)
723                                 fprintf(io"+" CONFMTp->n_lval);
724                 } else
725                         fprintf(ioCONFMTp->n_lval);
726                 return;
727
728         case OREG:
729                 r = p->n_rval;
730                 if (p->n_name[0])
731                         printf("%s%s"p->n_namep->n_lval ? "+" : "");
732                 if (p->n_lval)
ragge
1.17
733                         fprintf(io"%lld"p->n_lval);
mickey
1.1
734                 if (R2TEST(r)) {
ragge
1.18
735                         int r1 = R2UPK1(r);
736                         int r2 = R2UPK2(r);
737                         int sh = R2UPK3(r);
738
739                         fprintf(io"(%s,%s,%d)"
740                             r1 == MAXREGS ? "" : rnames[r1],
741                             r2 == MAXREGS ? "" : rnames[r2], sh);
mickey
1.1
742                 } else
743                         fprintf(io"(%s)"rnames[p->n_rval]);
744                 return;
745         case ICON:
746 #ifdef PCC_DEBUG
747                 /* Sanitycheck for PIC, to catch adressable constants */
748                 if (kflag && p->n_name[0]) {
749                         static int foo;
750
751                         if (foo++ == 0) {
752                                 printf("\nfailing...\n");
753                                 fwalk(pe2print0);
754                                 comperr("pass2 conput");
755                         }
756                 }
757 #endif
758                 /* addressable value of the constant */
759                 fputc('$'io);
760                 conput(iop);
761                 return;
762
763         case REG:
764                 switch (p->n_type) {
ragge
1.5
765                 case CHAR:
766                 case UCHAR:
767                         rc = rbyte;
mickey
1.1
768                         break;
769                 case SHORT:
770                 case USHORT:
ragge
1.5
771                         rc = rshort;
772                         break;
773                 case INT:
774                 case UNSIGNED:
775                         rc = rlong;
mickey
1.1
776                         break;
777                 default:
ragge
1.5
778                         rc = rnames;
779                         break;
mickey
1.1
780                 }
ragge
1.5
781                 fprintf(io"%s"rc[p->n_rval]);
mickey
1.1
782                 return;
783
784         default:
785                 comperr("illegal address, op %d, node %p"p->n_opp);
786                 return;
787
788         }
789 }
790
791 static char *
792 ccbranches[] = {
793         "je",           /* jumpe */
794         "jne",          /* jumpn */
795         "jle",          /* jumple */
796         "jl",           /* jumpl */
797         "jge",          /* jumpge */
798         "jg",           /* jumpg */
799         "jbe",          /* jumple (jlequ) */
800         "jb",           /* jumpl (jlssu) */
801         "jae",          /* jumpge (jgequ) */
802         "ja",           /* jumpg (jgtru) */
803 };
804
805
806 /*   printf conditional and unconditional branches */
807 void
808 cbgen(int oint lab)
809 {
810         if (o < EQ || o > UGT)
811                 comperr("bad conditional branch: %s"opst[o]);
812         printf("        %s " LABFMT "\n"ccbranches[o-EQ], lab);
813 }
814
815 static void
816 fixcalls(NODE *pvoid *arg)
817 {
818         /* Prepare for struct return by allocating bounce space on stack */
819         switch (p->n_op) {
820         case STCALL:
821         case USTCALL:
822                 if (p->n_stsize+p2autooff > stkpos)
823                         stkpos = p->n_stsize+p2autooff;
824                 break;
825         }
826 }
827
828 void
829 myreader(struct interpass *ipole)
830 {
831         struct interpass *ip;
832
833         stkpos = p2autooff;
834         DLIST_FOREACH(ipipoleqelem) {
835                 if (ip->type != IP_NODE)
836                         continue;
837                 walkf(ip->ip_nodefixcalls0);
838         }
839         if (stkpos > p2autooff)
840                 p2autooff = stkpos;
841         if (stkpos > p2maxautooff)
842                 p2maxautooff = stkpos;
843         if (x2debug)
844                 printip(ipole);
845 }
846
847 /*
848  * Remove some PCONVs after OREGs are created.
849  */
850 static void
851 pconv2(NODE *pvoid *arg)
852 {
853         NODE *q;
854
855         if (p->n_op == PLUS) {
856                 if (p->n_type == (PTR|SHORT) || p->n_type == (PTR|USHORT)) {
857                         if (p->n_right->n_op != ICON)
858                                 return;
859                         if (p->n_left->n_op != PCONV)
860                                 return;
861                         if (p->n_left->n_left->n_op != OREG)
862                                 return;
863                         q = p->n_left->n_left;
864                         nfree(p->n_left);
865                         p->n_left = q;
866                         /*
867                          * This will be converted to another OREG later.
868                          */
869                 }
870         }
871 }
872
873 void
874 mycanon(NODE *p)
875 {
876         walkf(ppconv20);
877 }
878
879 void
880 myoptim(struct interpass *ip)
881 {
882 }
883
884 void
885 rmove(int sint dTWORD t)
886 {
887
888         switch (t) {
ragge
1.11
889         case INT:
890         case UNSIGNED:
891                 printf("        movl %s,%s\n"rlong[s], rlong[d]);
mickey
1.1
892                 break;
893         case CHAR:
894         case UCHAR:
ragge
1.4
895                 printf("        movb %s,%s\n"rbyte[s], rbyte[d]);
896                 break;
897         case SHORT:
898         case USHORT:
899                 printf("        movw %s,%s\n"rshort[s], rshort[d]);
mickey
1.1
900                 break;
901         case FLOAT:
ragge
1.11
902                 printf("        movss %s,%s\n"rnames[s], rnames[d]);
903                 break;
mickey
1.1
904         case DOUBLE:
ragge
1.11
905                 printf("        movsd %s,%s\n"rnames[s], rnames[d]);
906                 break;
mickey
1.1
907         case LDOUBLE:
908                 /* a=b()*c(); will generate this */
909                 comperr("bad float rmove: %d %d"sd);
910                 break;
911         default:
ragge
1.11
912                 printf("        movq %s,%s\n"rnames[s], rnames[d]);
913                 break;
mickey
1.1
914         }
915 }
916
917 /*
918  * For class c, find worst-case displacement of the number of
919  * registers in the array r[] indexed by class.
920  */
921 int
922 COLORMAP(int cint *r)
923 {
924
925         switch (c) {
926         case CLASSA:
ragge
1.4
927                 return r[CLASSA] < 14;
mickey
1.1
928         case CLASSB:
ragge
1.4
929                 return r[CLASSB] < 16;
mickey
1.1
930         }
931         return 0/* XXX gcc */
932 }
933
934 char *rnames[] = {
ragge
1.4
935         "%rax""%rdx""%rcx""%rbx""%rsi""%rdi""%rbp""%rsp",
936         "%r8""%r9""%r10""%r11""%r12""%r13""%r14""%r15",
937         "%xmm0""%xmm1""%xmm2""%xmm3""%xmm4""%xmm5""%xmm6""%xmm7",
938         "%xmm8""%xmm9""%xmm10""%xmm11""%xmm12""%xmm13""%xmm14",
939         "%xmm15",
940 };
941
942 /* register names for shorter sizes */
943 char *rbyte[] = {
944         "%al""%dl""%cl""%bl""%sil""%dil""%bpl""%spl",
945         "%r8b""%r9b""%r10b""%r11b""%r12b""%r13b""%r14b""%r15b"
946 };
947 char *rshort[] = {
948         "%ax""%dx""%cx""%bx""%si""%di""%bp""%sp",
949         "%r8w""%r9w""%r10w""%r11w""%r12w""%r13w""%r14w""%r15w"
950 };
951 char *rlong[] = {
mickey
1.1
952         "%eax""%edx""%ecx""%ebx""%esi""%edi""%ebp""%esp",
ragge
1.7
953         "%r8d""%r9d""%r10d""%r11d""%r12d""%r13d""%r14d""%r15d"
mickey
1.1
954 };
955
ragge
1.4
956
mickey
1.1
957 /*
958  * Return a class suitable for a specific type.
959  */
960 int
961 gclass(TWORD t)
962 {
ragge
1.4
963         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
mickey
1.1
964                 return CLASSB;
965         return CLASSA;
966 }
967
968 /*
969  * Calculate argument sizes.
970  */
971 void
972 lastcall(NODE *p)
973 {
974         NODE *op = p;
975         int size = 0;
976
977         p->n_qual = 0;
978         if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
979                 return;
980         for (p = p->n_rightp->n_op == CMp = p->n_left)
981                 size += argsiz(p->n_right);
982         size += argsiz(p);
ragge
1.10
983         size = (size+15) & ~15;
984         if (size)
985                 printf("        subq $%d,%s\n"sizernames[RSP]);
mickey
1.1
986         op->n_qual = size/* XXX */
987 }
988
989 /*
990  * Special shapes.
991  */
992 int
993 special(NODE *pint shape)
994 {
995         int o = p->n_op;
996
997         switch (shape) {
998         case SFUNCALL:
999                 if (o == STCALL || o == USTCALL)
1000                         return SRREG;
1001                 break;
1002         case SPCON:
1003                 if (o != ICON || p->n_name[0] ||
1004                     p->n_lval < 0 || p->n_lval > 0x7fffffff)
1005                         break;
1006                 return SRDIR;
1007         case SMIXOR:
1008                 return tshape(pSZERO);
1009         case SMILWXOR:
1010                 if (o != ICON || p->n_name[0] ||
1011                     p->n_lval == 0 || p->n_lval & 0xffffffff)
1012                         break;
1013                 return SRDIR;
1014         case SMIHWXOR:
1015                 if (o != ICON || p->n_name[0] ||
1016                      p->n_lval == 0 || (p->n_lval >> 32) != 0)
1017                         break;
1018                 return SRDIR;
ragge
1.12
1019         case SCON32:
1020                 if (o != ICON || p->n_name[0])
1021                         break;
1022                 if (p->n_lval < MIN_INT || p->n_lval > MAX_INT)
1023                         break;
1024                 return SRDIR;
1025         default:
1026                 cerror("special: %x\n"shape);
mickey
1.1
1027         }
1028         return SRNOPE;
1029 }
1030
1031 /*
1032  * Target-dependent command-line options.
1033  */
1034 void
1035 mflags(char *str)
1036 {
1037 }
1038
1039 /*
1040  * Do something target-dependent for xasm arguments.
1041  */
1042 int
1043 myxasm(struct interpass *ipNODE *p)
1044 {
ragge
1.15
1045         return 0;
ragge
1.4
1046 #if 0
mickey
1.1
1047         struct interpass *ip2;
1048         NODE *in = 0, *ut = 0;
1049         TWORD t;
1050         char *w;
1051         int reg;
1052         int cw;
1053
1054         cw = xasmcode(p->n_name);
1055         if (cw & (XASMASG|XASMINOUT))
1056                 ut = p->n_left;
1057         if ((cw & XASMASG) == 0)
1058                 in = p->n_left;
1059
1060         switch (XASMVAL(cw)) {
1061         case 'D'reg = EDIbreak;
1062         case 'S'reg = ESIbreak;
1063         case 'a'reg = EAXbreak;
1064         case 'b'reg = EBXbreak;
1065         case 'c'reg = ECXbreak;
1066         case 'd'reg = EDXbreak;
1067         case 't'reg = 0break;
1068         case 'u'reg = 1break;
1069         case 'A'reg = EAXEDXbreak;
1070         case 'q'/* XXX let it be CLASSA as for now */
1071                 p->n_name = tmpstrdup(p->n_name);
1072                 w = strchr(p->n_name'q');
1073                 *w = 'r';
1074                 return 0;
1075         default:
1076                 return 0;
1077         }
1078         p->n_name = tmpstrdup(p->n_name);
1079         for (w = p->n_name; *ww++)
1080                 ;
1081         w[-1] = 'r'/* now reg */
1082         t = p->n_left->n_type;
1083         if (reg == EAXEDX) {
1084                 p->n_label = CLASSC;
1085         } else {
1086                 p->n_label = CLASSA;
1087                 if (t == CHAR || t == UCHAR) {
1088                         p->n_label = CLASSB;
1089                         reg = reg * 2 + 8;
1090                 }
1091         }
1092         if (t == FLOAT || t == DOUBLE || t == LDOUBLE) {
1093                 p->n_label = CLASSD;
1094                 reg += 037;
1095         }
1096
1097         if (in && ut)
1098                 in = tcopy(in);
1099         p->n_left = mklnode(REG0regt);
1100         if (ut) {
1101                 ip2 = ipnode(mkbinode(ASSIGNuttcopy(p->n_left), t));
1102                 DLIST_INSERT_AFTER(ipip2qelem);
1103         }
1104         if (in) {
1105                 ip2 = ipnode(mkbinode(ASSIGNtcopy(p->n_left), int));
1106                 DLIST_INSERT_BEFORE(ipip2qelem);
1107         }
ragge
1.4
1108 #endif
mickey
1.1
1109         return 1;
1110 }
1111
1112 void
1113 targarg(char *wvoid *arg)
1114 {
ragge
1.4
1115 cerror("targarg");
1116 #if 0
mickey
1.1
1117         NODE **ary = arg;
1118         NODE *p, *q;
1119
1120         p = ary[(int)w[1]-'0']->n_left;
1121         if (optype(p->n_op) != LTYPE)
1122                 comperr("bad xarg op %d"p->n_op);
1123         q = tcopy(p);
1124         if (q->n_op == REG) {
1125                 if (*w == 'k') {
1126                         q->n_type = INT;
1127                 } else if (*w != 'w') {
1128                         if (q->n_type > UCHAR) {
1129                                 regno(q) = regno(q)*2+8;
1130                                 if (*w == 'h')
1131                                         regno(q)++;
1132                         }
1133                         q->n_type = INT;
1134                 } else
1135                         q->n_type = SHORT;
1136         }
1137         adrput(stdoutq);
1138         tfree(q);
ragge
1.4
1139 #endif
mickey
1.1
1140 }
1141
1142 /*
1143  * target-specific conversion of numeric arguments.
1144  */
1145