Quick Search:

View

Revision:
Expand:  
Changeset: MAIN:ragge:20100412162709

Diff

Diff from 1.17 to:

Annotations

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

Annotated File View

ragge
1.17
1 /*      $Id: local2.c,v 1.17 2010/04/12 16:27:09 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
gmcgarry
1.3
203 #if 0
mickey
1.1
204 /*
205  * Emit code to compare two longlong numbers.
206  */
207 static void
208 twollcomp(NODE *p)
209 {
210         int o = p->n_op;
mickey
1.2
211         int s = getlab2();
mickey
1.1
212         int e = p->n_label;
213         int cb1cb2;
214
215         if (o >= ULE)
216                 o -= (ULE-LE);
217         switch (o) {
218         case NE:
219                 cb1 = 0;
220                 cb2 = NE;
221                 break;
222         case EQ:
223                 cb1 = NE;
224                 cb2 = 0;
225                 break;
226         case LE:
227         case LT:
228                 cb1 = GT;
229                 cb2 = LT;
230                 break;
231         case GE:
232         case GT:
233                 cb1 = LT;
234                 cb2 = GT;
235                 break;
236         
237         default:
238                 cb1 = cb2 = 0/* XXX gcc */
239         }
240         if (p->n_op >= ULE)
241                 cb1 += 4cb2 += 4;
242         expand(p0"  cmpl UR,UL\n");
243         if (cb1cbgen(cb1s);
244         if (cb2cbgen(cb2e);
245         expand(p0"  cmpl AR,AL\n");
246         cbgen(p->n_ope);
247         deflab(s);
248 }
gmcgarry
1.3
249 #endif
mickey
1.1
250
251 int
252 fldexpand(NODE *pint cookiechar **cp)
253 {
254         CONSZ val;
255
256         if (p->n_op == ASSIGN)
257                 p = p->n_left;
258         switch (**cp) {
259         case 'S':
260                 printf("%d"UPKFSZ(p->n_rval));
261                 break;
262         case 'H':
263                 printf("%d"UPKFOFF(p->n_rval));
264                 break;
265         case 'M':
266         case 'N':
267                 val = (CONSZ)1 << UPKFSZ(p->n_rval);
268                 --val;
269                 val <<= UPKFOFF(p->n_rval);
ragge
1.13
270                 if (p->n_type > UNSIGNED)
271                         printf("0x%llx", (**cp == 'M' ? val : ~val));
272                 else
273                         printf("0x%llx", (**cp == 'M' ? val : ~val)&0xffffffff);
mickey
1.1
274                 break;
275         default:
276                 comperr("fldexpand");
277         }
278         return 1;
279 }
280
281 static void
282 bfext(NODE *p)
283 {
284         int ch = 0sz = 0;
285
286         if (ISUNSIGNED(p->n_right->n_type))
287                 return;
288         switch (p->n_right->n_type) {
289         case CHAR:
290                 ch = 'b';
291                 sz = 8;
292                 break;
293         case SHORT:
294                 ch = 'w';
295                 sz = 16;
296                 break;
297         case INT:
298                 ch = 'l';
299                 sz = 32;
300                 break;
301         case LONG:
302                 ch = 'q';
303                 sz = 64;
304                 break;
305         default:
306                 comperr("bfext");
307         }
308
309         sz -= UPKFSZ(p->n_left->n_rval);
310         printf("\tshl%c $%d,"chsz);
311         adrput(stdoutgetlr(p'D'));
312         printf("\n\tsar%c $%d,"chsz);
313         adrput(stdoutgetlr(p'D'));
314         printf("\n");
315 }
ragge
1.11
316 #if 0
mickey
1.1
317
318 /*
319  * Push a structure on stack as argument.
320  * the scratch registers are already free here
321  */
322 static void
323 starg(NODE *p)
324 {
325         FILE *fp = stdout;
326
327         fprintf(fp"   subl $%d,%%esp\n"p->n_stsize);
328         fprintf(fp"   pushl $%d\n"p->n_stsize);
329         expand(p0"  pushl AL\n");
330         expand(p0"  leal 8(%esp),A1\n");
331         expand(p0"  pushl A1\n");
332         fprintf(fp"   call memcpy\n");
333         fprintf(fp"   addl $12,%%esp\n");
334 }
335
ragge
1.7
336 #endif
mickey
1.1
337 /*
ragge
1.7
338  * Generate code to convert an unsigned long to xmm float/double.
mickey
1.1
339  */
340 static void
ragge
1.7
341 ultofd(NODE *p)
mickey
1.1
342 {
343
ragge
1.7
344 #define E(x)    expand(p, 0, x)
345         E("     movq AL,A1\n");
ragge
1.9
346         E("     testq A1,A1\n");
ragge
1.7
347         E("     js 2f\n");
348         E("     cvtsi2sZfq A1,A3\n");
349         E("     jmp 3f\n");
350         E("2:\n");
351         E("     movq A1,A2\n");
352         E("     shrq A2\n");
ragge
1.9
353         E("     andq $1,A1\n");
ragge
1.7
354         E("     orq A1,A2\n");
355         E("     cvtsi2sZfq A2,A3\n");
356         E("     addsZf A3,A3\n");
357         E("3:\n");
358 #undef E
mickey
1.1
359 }
360
361 static int
362 argsiz(NODE *p)
363 {
364         TWORD t = p->n_type;
365
ragge
1.10
366         if (p->n_left->n_op == REG)
367                 return 0/* not on stack */
mickey
1.1
368         if (t == LDOUBLE)
369                 return 16;
370         if (t == STRTY || t == UNIONTY)
371                 return p->n_stsize;
ragge
1.10
372         return 8;
mickey
1.1
373 }
374
375 void
376 zzzcode(NODE *pint c)
377 {
ragge
1.6
378         NODE *l;
ragge
1.7
379         int prlrs;
ragge
1.14
380         char **rt;
mickey
1.1
381
382         switch (c) {
ragge
1.4
383 #if 0
mickey
1.1
384         case 'A'/* swap st0 and st1 if right is evaluated second */
385                 if ((p->n_su & DORIGHT) == 0) {
386                         if (logop(p->n_op))
387                                 printf("        fxch\n");
388                         else
389                                 printf("r");
390                 }
391                 break;
ragge
1.5
392 #endif
mickey
1.1
393
394         case 'C':  /* remove from stack after subroutine call */
395                 pr = p->n_qual;
396                 if (p->n_op == UCALL)
397                         return/* XXX remove ZC from UCALL */
398                 if (pr)
ragge
1.10
399                         printf("        addq $%d, %s\n"prrnames[RSP]);
mickey
1.1
400                 break;
401
402         case 'E'/* Perform bitfield sign-extension */
403                 bfext(p);
404                 break;
405
ragge
1.11
406 #if 0
mickey
1.1
407         case 'F'/* Structure argument */
408                 if (p->n_stalign != 0/* already on stack */
409                         starg(p);
410                 break;
ragge
1.7
411 #endif
412         case 'j'/* convert unsigned long to f/d */
413                 ultofd(p);
mickey
1.1
414                 break;
415
416         case 'M'/* Output sconv move, if needed */
417                 l = getlr(p'L');
418                 /* XXX fixneed: regnum */
419                 pr = DECRA(p->n_reg0);
420                 lr = DECRA(l->n_reg0);
ragge
1.6
421                 if (pr == lr)
422                         break;
423                 printf("        movb %s,%s\n"rbyte[lr], rbyte[pr]);
mickey
1.1
424                 l->n_rval = l->n_reg = p->n_reg/* XXX - not pretty */
425                 break;
426
ragge
1.6
427 #if 0
mickey
1.1
428         case 'N'/* output extended reg name */
429                 printf("%s"rnames[getlr(p'1')->n_rval]);
430                 break;
ragge
1.16
431 #endif
432
433         case 'P'/* Put hidden argument in rdi */
434                 printf("\tleaq -%d(%%rbp),%%rdi\n"stkpos);
435                 break;
436
437 #if 0
mickey
1.1
438
439         case 'S'/* emit eventual move after cast from longlong */
440                 pr = DECRA(p->n_reg0);
441                 lr = p->n_left->n_rval;
442                 switch (p->n_type) {
443                 case CHAR:
444                 case UCHAR:
445                         if (rnames[pr][2] == 'l' && rnames[lr][2] == 'x' &&
446                             rnames[pr][1] == rnames[lr][1])
447                                 break;
448                         if (rnames[lr][2] == 'x') {
449                                 printf("\tmovb %%%cl,%s\n",
450                                     rnames[lr][1], rnames[pr]);
451                                 break;
452                         }
453                         /* Must go via stack */
454                         s = BITOOR(freetemp(1));
455                         printf("\tmovl %%e%ci,%d(%%rbp)\n"rnames[lr][1], s);
456                         printf("\tmovb %d(%%rbp),%s\n"srnames[pr]);
457                         comperr("SCONV1 %s->%s"rnames[lr], rnames[pr]);
458                         break;
459
460                 case SHORT:
461                 case USHORT:
462                         if (rnames[lr][1] == rnames[pr][2] &&
463                             rnames[lr][2] == rnames[pr][3])
464                                 break;
465                         printf("\tmovw %%%c%c,%%%s\n",
466                             rnames[lr][1], rnames[lr][2], rnames[pr]+2);
467                         comperr("SCONV2 %s->%s"rnames[lr], rnames[pr]);
468                         break;
469                 case INT:
470                 case UNSIGNED:
471                         if (rnames[lr][1] == rnames[pr][2] &&
472                             rnames[lr][2] == rnames[pr][3])
473                                 break;
474                         printf("\tmovl %%e%c%c,%s\n",
475                                     rnames[lr][1], rnames[lr][2], rnames[pr]);
476                         comperr("SCONV3 %s->%s"rnames[lr], rnames[pr]);
477                         break;
478
479                 default:
480                         if (rnames[lr][1] == rnames[pr][2] &&
481                             rnames[lr][2] == rnames[pr][3])
482                                 break;
483                         comperr("SCONV4 %s->%s"rnames[lr], rnames[pr]);
484                         break;
485                 }
486                 break;
ragge
1.4
487 #endif
ragge
1.10
488         case 'Q'/* emit struct assign */
489                 /* XXX - optimize for small structs */
490                 printf("\tmovq $%d,%%rdx\n"p->n_stsize);
491                 expand(pINAREG"\tmovq AR,%rsi\n");
492                 expand(pINAREG"\tleaq AL,%rdi\n\n");
493                 printf("\tcall memcpy\n");
494                 break;
495
ragge
1.7
496         case 'R'/* print opname based on right type */
497         case 'L'/* print opname based on left type */
498                 switch (getlr(pc)->n_type) {
499                 case CHARcase UCHARs = 'b'break;
500                 case SHORTcase USHORTs = 'w'break;
501                 case INTcase UNSIGNEDs = 'l'break;
502                 defaults = 'q'break;
503                 printf("%c"s);
504                 }
505                 break;
ragge
1.14
506
507         case '8'/* special reg name printout (64-bit) */
508         case '1'/* special reg name printout (32-bit) */
ragge
1.7
509                 l = getlr(p'1');
ragge
1.14
510                 rt = c == '8' ? rnames : rlong;
511                 printf("%s"rt[l->n_rval]);
ragge
1.7
512                 break;
513
514         case 'g':
515                 p = p->n_left;
516                 /* FALLTHROUGH */
517         case 'f'/* float or double */
518                 printf("%c"p->n_type == FLOAT ? 's' : 'd');
519                 break;
520
521         case 'q'/* int or long */
522                 printf("%c"p->n_left->n_type == LONG ? 'q' : ' ');
523                 break;
524
mickey
1.1
525         default:
526                 comperr("zzzcode %c"c);
527         }
528 }
529
530 /*ARGSUSED*/
531 int
532 rewfld(NODE *p)
533 {
534         return(1);
535 }
536
537 int canaddr(NODE *);
538 int
539 canaddr(NODE *p)
540 {
541         int o = p->n_op;
542
543         if (o==NAME || o==REG || o==ICON || o==OREG ||
544             (o==UMUL && shumul(p->n_leftSOREG)))
545                 return(1);
546         return(0);
547 }
548
549 /*
550  * Does the bitfield shape match?
551  */
552 int
553 flshape(NODE *p)
554 {
555         int o = p->n_op;
556
557         if (o == OREG || o == REG || o == NAME)
558                 return SRDIR/* Direct match */
559         if (o == UMUL && shumul(p->n_leftSOREG))
560                 return SROREG/* Convert into oreg */
561         return SRREG/* put it into a register */
562 }
563
564 /* INTEMP shapes must not contain any temporary registers */
565 /* XXX should this go away now? */
566 int
567 shtemp(NODE *p)
568 {
569         return 0;
570 #if 0
571         int r;
572
573         if (p->n_op == STARG )
574                 p = p->n_left;
575
576         switch (p->n_op) {
577         case REG:
578                 return (!istreg(p->n_rval));
579
580         case OREG:
581                 r = p->n_rval;
582                 if (R2TEST(r)) {
583                         if (istreg(R2UPK1(r)))
584                                 return(0);
585                         r = R2UPK2(r);
586                 }
587                 return (!istreg(r));
588
589         case UMUL:
590                 p = p->n_left;
591                 return (p->n_op != UMUL && shtemp(p));
592         }
593
594         if (optype(p->n_op) != LTYPE)
595                 return(0);
596         return(1);
597 #endif
598 }
599
600 void
601 adrcon(CONSZ val)
602 {
603         printf("$" CONFMTval);
604 }
605
606 void
607 conput(FILE *fpNODE *p)
608 {
ragge
1.17
609         long val = p->n_lval;
mickey
1.1
610
611         switch (p->n_op) {
612         case ICON:
613                 if (p->n_name[0] != '\0') {
614                         fprintf(fp"%s"p->n_name);
615                         if (val)
ragge
1.17
616                                 fprintf(fp"+%ld"val);
mickey
1.1
617                 } else
ragge
1.17
618                         fprintf(fp"%ld"val);
mickey
1.1
619                 return;
620
621         default:
622                 comperr("illegal conput, p %p"p);
623         }
624 }
625
626 /*ARGSUSED*/
627 void
628 insput(NODE *p)
629 {
630         comperr("insput");
631 }
632
633 /*
634  * Write out the upper address, like the upper register of a 2-register
635  * reference, or the next memory location.
ragge
1.17
636  * XXX - not needed on amd64
mickey
1.1
637  */
638 void
639 upput(NODE *pint size)
640 {
641
642         size /= SZCHAR;
643         switch (p->n_op) {
644         case REG:
645                 fprintf(stdout"%%%s", &rnames[p->n_rval][3]);
646                 break;
647
648         case NAME:
649         case OREG:
650                 p->n_lval += size;
651                 adrput(stdoutp);
652                 p->n_lval -= size;
653                 break;
654         case ICON:
655                 fprintf(stdout"$" CONFMTp->n_lval >> 32);
656                 break;
657         default:
658                 comperr("upput bad op %d size %d"p->n_opsize);
659         }
660 }
661
662 void
663 adrput(FILE *ioNODE *p)
664 {
665         int r;
ragge
1.5
666         char **rc;
mickey
1.1
667         /* output an address, with offsets, from p */
668
669         if (p->n_op == FLD)
670                 p = p->n_left;
671
672         switch (p->n_op) {
673
674         case NAME:
675                 if (p->n_name[0] != '\0') {
676                         fputs(p->n_nameio);
677                         if (p->n_lval != 0)
678                                 fprintf(io"+" CONFMTp->n_lval);
679                 } else
680                         fprintf(ioCONFMTp->n_lval);
681                 return;
682
683         case OREG:
684                 r = p->n_rval;
685                 if (p->n_name[0])
686                         printf("%s%s"p->n_namep->n_lval ? "+" : "");
687                 if (p->n_lval)
ragge
1.17
688                         fprintf(io"%lld"p->n_lval);
mickey
1.1
689                 if (R2TEST(r)) {
690                         fprintf(io"(%s,%s,8)"rnames[R2UPK1(r)],
691                             rnames[R2UPK2(r)]);
692                 } else
693                         fprintf(io"(%s)"rnames[p->n_rval]);
694                 return;
695         case ICON:
696 #ifdef PCC_DEBUG
697                 /* Sanitycheck for PIC, to catch adressable constants */
698                 if (kflag && p->n_name[0]) {
699                         static int foo;
700
701                         if (foo++ == 0) {
702                                 printf("\nfailing...\n");
703                                 fwalk(pe2print0);
704                                 comperr("pass2 conput");
705                         }
706                 }
707 #endif
708                 /* addressable value of the constant */
709                 fputc('$'io);
710                 conput(iop);
711                 return;
712
713         case REG:
714                 switch (p->n_type) {
ragge
1.5
715                 case CHAR:
716                 case UCHAR:
717                         rc = rbyte;
mickey
1.1
718                         break;
719                 case SHORT:
720                 case USHORT:
ragge
1.5
721                         rc = rshort;
722                         break;
723                 case INT:
724                 case UNSIGNED:
725                         rc = rlong;
mickey
1.1
726                         break;
727                 default:
ragge
1.5
728                         rc = rnames;
729                         break;
mickey
1.1
730                 }
ragge
1.5
731                 fprintf(io"%s"rc[p->n_rval]);
mickey
1.1
732                 return;
733
734         default:
735                 comperr("illegal address, op %d, node %p"p->n_opp);
736                 return;
737
738         }
739 }
740
741 static char *
742 ccbranches[] = {
743         "je",           /* jumpe */
744         "jne",          /* jumpn */
745         "jle",          /* jumple */
746         "jl",           /* jumpl */
747         "jge",          /* jumpge */
748         "jg",           /* jumpg */
749         "jbe",          /* jumple (jlequ) */
750         "jb",           /* jumpl (jlssu) */
751         "jae",          /* jumpge (jgequ) */
752         "ja",           /* jumpg (jgtru) */
753 };
754
755
756 /*   printf conditional and unconditional branches */
757 void
758 cbgen(int oint lab)
759 {
760         if (o < EQ || o > UGT)
761                 comperr("bad conditional branch: %s"opst[o]);
762         printf("        %s " LABFMT "\n"ccbranches[o-EQ], lab);
763 }
764
765 static void
766 fixcalls(NODE *pvoid *arg)
767 {
768         /* Prepare for struct return by allocating bounce space on stack */
769         switch (p->n_op) {
770         case STCALL:
771         case USTCALL:
772                 if (p->n_stsize+p2autooff > stkpos)
773                         stkpos = p->n_stsize+p2autooff;
774                 break;
775         }
776 }
777
778 void
779 myreader(struct interpass *ipole)
780 {
781         struct interpass *ip;
782
783         stkpos = p2autooff;
784         DLIST_FOREACH(ipipoleqelem) {
785                 if (ip->type != IP_NODE)
786                         continue;
787                 walkf(ip->ip_nodefixcalls0);
788         }
789         if (stkpos > p2autooff)
790                 p2autooff = stkpos;
791         if (stkpos > p2maxautooff)
792                 p2maxautooff = stkpos;
793         if (x2debug)
794                 printip(ipole);
795 }
796
797 /*
798  * Remove some PCONVs after OREGs are created.
799  */
800 static void
801 pconv2(NODE *pvoid *arg)
802 {
803         NODE *q;
804
805         if (p->n_op == PLUS) {
806                 if (p->n_type == (PTR|SHORT) || p->n_type == (PTR|USHORT)) {
807                         if (p->n_right->n_op != ICON)
808                                 return;
809                         if (p->n_left->n_op != PCONV)
810                                 return;
811                         if (p->n_left->n_left->n_op != OREG)
812                                 return;
813                         q = p->n_left->n_left;
814                         nfree(p->n_left);
815                         p->n_left = q;
816                         /*
817                          * This will be converted to another OREG later.
818                          */
819                 }
820         }
821 }
822
823 void
824 mycanon(NODE *p)
825 {
826         walkf(ppconv20);
827 }
828
829 void
830 myoptim(struct interpass *ip)
831 {
832 }
833
834 void
835 rmove(int sint dTWORD t)
836 {
837
838         switch (t) {
ragge
1.11
839         case INT:
840         case UNSIGNED:
841                 printf("        movl %s,%s\n"rlong[s], rlong[d]);
mickey
1.1
842                 break;
843         case CHAR:
844         case UCHAR:
ragge
1.4
845                 printf("        movb %s,%s\n"rbyte[s], rbyte[d]);
846                 break;
847         case SHORT:
848         case USHORT:
849                 printf("        movw %s,%s\n"rshort[s], rshort[d]);
mickey
1.1
850                 break;
851         case FLOAT:
ragge
1.11
852                 printf("        movss %s,%s\n"rnames[s], rnames[d]);
853                 break;
mickey
1.1
854         case DOUBLE:
ragge
1.11
855                 printf("        movsd %s,%s\n"rnames[s], rnames[d]);
856                 break;
mickey
1.1
857         case LDOUBLE:
858                 /* a=b()*c(); will generate this */
859                 comperr("bad float rmove: %d %d"sd);
860                 break;
861         default:
ragge
1.11
862                 printf("        movq %s,%s\n"rnames[s], rnames[d]);
863                 break;
mickey
1.1
864         }
865 }
866
867 /*
868  * For class c, find worst-case displacement of the number of
869  * registers in the array r[] indexed by class.
870  */
871 int
872 COLORMAP(int cint *r)
873 {
874
875         switch (c) {
876         case CLASSA:
ragge
1.4
877                 return r[CLASSA] < 14;
mickey
1.1
878         case CLASSB:
ragge
1.4
879                 return r[CLASSB] < 16;
mickey
1.1
880         }
881         return 0/* XXX gcc */
882 }
883
884 char *rnames[] = {
ragge
1.4
885         "%rax""%rdx""%rcx""%rbx""%rsi""%rdi""%rbp""%rsp",
886         "%r8""%r9""%r10""%r11""%r12""%r13""%r14""%r15",
887         "%xmm0""%xmm1""%xmm2""%xmm3""%xmm4""%xmm5""%xmm6""%xmm7",
888         "%xmm8""%xmm9""%xmm10""%xmm11""%xmm12""%xmm13""%xmm14",
889         "%xmm15",
890 };
891
892 /* register names for shorter sizes */
893 char *rbyte[] = {
894         "%al""%dl""%cl""%bl""%sil""%dil""%bpl""%spl",
895         "%r8b""%r9b""%r10b""%r11b""%r12b""%r13b""%r14b""%r15b"
896 };
897 char *rshort[] = {
898         "%ax""%dx""%cx""%bx""%si""%di""%bp""%sp",
899         "%r8w""%r9w""%r10w""%r11w""%r12w""%r13w""%r14w""%r15w"
900 };
901 char *rlong[] = {
mickey
1.1
902         "%eax""%edx""%ecx""%ebx""%esi""%edi""%ebp""%esp",
ragge
1.7
903         "%r8d""%r9d""%r10d""%r11d""%r12d""%r13d""%r14d""%r15d"
mickey
1.1
904 };
905
ragge
1.4
906
mickey
1.1
907 /*
908  * Return a class suitable for a specific type.
909  */
910 int
911 gclass(TWORD t)
912 {
ragge
1.4
913         if (t == FLOAT || t == DOUBLE || t == LDOUBLE)
mickey
1.1
914                 return CLASSB;
915         return CLASSA;
916 }
917
918 /*
919  * Calculate argument sizes.
920  */
921 void
922 lastcall(NODE *p)
923 {
924         NODE *op = p;
925         int size = 0;
926
927         p->n_qual = 0;
928         if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
929                 return;
930         for (p = p->n_rightp->n_op == CMp = p->n_left)
931                 size += argsiz(p->n_right);
932         size += argsiz(p);
ragge
1.10
933         size = (size+15) & ~15;
934         if (size)
935                 printf("        subq $%d,%s\n"sizernames[RSP]);
mickey
1.1
936         op->n_qual = size/* XXX */
937 }
938
939 /*
940  * Special shapes.
941  */
942 int
943 special(NODE *pint shape)
944 {
945         int o = p->n_op;
946
947         switch (shape) {
948         case SFUNCALL:
949                 if (o == STCALL || o == USTCALL)
950                         return SRREG;
951                 break;
952         case SPCON:
953                 if (o != ICON || p->n_name[0] ||
954                     p->n_lval < 0 || p->n_lval > 0x7fffffff)
955                         break;
956                 return SRDIR;
957         case SMIXOR:
958                 return tshape(pSZERO);
959         case SMILWXOR:
960                 if (o != ICON || p->n_name[0] ||
961                     p->n_lval == 0 || p->n_lval & 0xffffffff)
962                         break;
963                 return SRDIR;
964         case SMIHWXOR:
965                 if (o != ICON || p->n_name[0] ||
966                      p->n_lval == 0 || (p->n_lval >> 32) != 0)
967                         break;
968                 return SRDIR;
ragge
1.12
969         case SCON32:
970                 if (o != ICON || p->n_name[0])
971                         break;
972                 if (p->n_lval < MIN_INT || p->n_lval > MAX_INT)
973                         break;
974                 return SRDIR;
975         default:
976                 cerror("special: %x\n"shape);
mickey
1.1
977         }
978         return SRNOPE;
979 }
980
981 /*
982  * Target-dependent command-line options.
983  */
984 void
985 mflags(char *str)
986 {
987 }
988
989 /*
990  * Do something target-dependent for xasm arguments.
991  */
992 int
993 myxasm(struct interpass *ipNODE *p)
994 {
ragge
1.15
995         return 0;
ragge
1.4
996 #if 0
mickey
1.1
997         struct interpass *ip2;
998         NODE *in = 0, *ut = 0;
999         TWORD t;
1000         char *w;
1001         int reg;
1002         int cw;
1003
1004         cw = xasmcode(p->n_name);
1005         if (cw & (XASMASG|XASMINOUT))
1006                 ut = p->n_left;
1007         if ((cw & XASMASG) == 0)
1008                 in = p->n_left;
1009
1010         switch (XASMVAL(cw)) {
1011         case 'D'reg = EDIbreak;
1012         case 'S'reg = ESIbreak;
1013         case 'a'reg = EAXbreak;
1014         case 'b'reg = EBXbreak;
1015         case 'c'reg = ECXbreak;
1016         case 'd'reg = EDXbreak;
1017         case 't'reg = 0break;
1018         case 'u'reg = 1break;
1019         case 'A'reg = EAXEDXbreak;
1020         case 'q'/* XXX let it be CLASSA as for now */
1021                 p->n_name = tmpstrdup(p->n_name);
1022                 w = strchr(p->n_name'q');
1023                 *w = 'r';
1024                 return 0;
1025         default:
1026                 return 0;
1027         }
1028         p->n_name = tmpstrdup(p->n_name);
1029         for (w = p->n_name; *ww++)
1030                 ;
1031         w[-1] = 'r'/* now reg */
1032         t = p->n_left->n_type;
1033         if (reg == EAXEDX) {
1034                 p->n_label = CLASSC;
1035         } else {
1036                 p->n_label = CLASSA;
1037                 if (t == CHAR || t == UCHAR) {
1038                         p->n_label = CLASSB;
1039                         reg = reg * 2 + 8;
1040                 }
1041         }
1042         if (t == FLOAT || t == DOUBLE || t == LDOUBLE) {
1043                 p->n_label = CLASSD;
1044                 reg += 037;
1045         }
1046
1047         if (in && ut)
1048                 in = tcopy(in);
1049         p->n_left = mklnode(REG0regt);
1050         if (ut) {
1051                 ip2 = ipnode(mkbinode(ASSIGNuttcopy(p->n_left), t));
1052                 DLIST_INSERT_AFTER(ipip2qelem);
1053         }
1054         if (in) {
1055                 ip2 = ipnode(mkbinode(ASSIGNtcopy(p->n_left), int));
1056                 DLIST_INSERT_BEFORE(ipip2qelem);
1057         }
ragge
1.4
1058 #endif
mickey
1.1
1059         return 1;
1060 }
1061
1062 void
1063 targarg(char *wvoid *arg)
1064 {
ragge
1.4
1065 cerror("targarg");
1066 #if 0
mickey
1.1
1067         NODE **ary = arg;
1068         NODE *p, *q;
1069
1070         p = ary[(int)w[1]-'0']->n_left;
1071         if (optype(p->n_op) != LTYPE)
1072                 comperr("bad xarg op %d"p->n_op);
1073         q = tcopy(p);
1074         if (q->n_op == REG) {
1075                 if (*w == 'k') {
1076                         q->n_type = INT;
1077                 } else if (*w != 'w') {
1078                         if (q->n_type > UCHAR) {
1079                                 regno(q) = regno(q)*2+8;
1080                                 if (*w == 'h')
1081                                         regno(q)++;
1082                         }
1083                         q->n_type = INT;
1084                 } else
1085                         q->n_type = SHORT;
1086         }
1087         adrput(stdoutq);
1088         tfree(q);
ragge
1.4
1089 #endif
mickey
1.1
1090 }
1091
1092 /*
1093  * target-specific conversion of numeric arguments.
1094  */
1095 int
1096 numconv(void *ipvoid *p1void *q1)
1097 {
1098         NODE *p = p1, *q = q1;
1099         int cw = xasmcode(q->n_name);
1100
1101         switch (XASMVAL(cw)) {
1102         case 'a':
1103         case 'b':
1104         case 'c':
1105         case 'd':
1106                 p->n_name = tmpcalloc(2);
1107                 p->n_name[0] = XASMVAL(cw);
1108                 return 1;
1109         default:
1110                 return 0;
1111         }
1112 }
FishEye: Open Source License registered to PCC.
Your maintenance has expired. You can renew your license at http://www.atlassian.com/fisheye/renew
Atlassian FishEye, CVS analysis. (Version:1.6.3 Build:build-336 2008-11-04) - Administration - Page generated 2014-10-01 20:19 +0200