Debian Codegraph @

arch/vax/local2.c

raw symbols SCIP
1 /* $Id: local2.c,v 1.44 2018/04/03 14:49:46 ragge Exp $ */
2 /*
3 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * Redistributions of source code and documentation must retain the above
10 * copyright notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditionsand the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed or owned by Caldera
17 * International, Inc.
18 * Neither the name of Caldera International, Inc. nor the names of other
19 * contributors may be used to endorse or promote products derived from
20 * this software without specific prior written permission.
21 *
22 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
23 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
27 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
31 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33 * POSSIBILITY OF SUCH DAMAGE.
34 */
35
36 # include "pass2.h"
37 # include "ctype.h"
38 /* a lot of the machine dependent parts of the second pass */
39
40 static void prtype(NODE *n);
41 static void acon(NODE *p);
42
43 /*
44 * Print out the prolog assembler.
45 * addto and regoff are already calculated.
46 */
47 void
48 prologue(struct interpass_prolog *ipp)
49 {
50 printf(" .word 0x%llx\n", (unsigned long long)p2env.p_regs[0]);
51 if (p2maxautooff)
52 printf(" subl2 $%d,%%sp\n", p2maxautooff);
53 if (pflag) {
54 int i = getlab2();
55 printf("\tmovab\t" LABFMT ",%%r0\n", i);
56 printf("\tjsb\t__mcount\n");
57 printf("\t.data\n");
58 printf("\t.align 2\n");
59 printf(LABFMT ":\t.long\t0\n", i);
60 printf("\t.text\n");
61 }
62 }
63
64 /*
65 * Called after all instructions in a function are emitted.
66 * Generates code for epilog here.
67 */
68 void
69 eoftn(struct interpass_prolog *ipp)
70 {
71 if (ipp->ipp_ip.ip_lbl == 0)
72 return; /* no code needs to be generated */
73 printf(" ret\n");
74 }
75
76 struct hoptab { int opmask; char * opstring; } ioptab[] = {
77
78 { PLUS, "add", },
79 { MINUS, "sub", },
80 { MUL, "mul", },
81 { DIV, "div", },
82 { OR, "bis", },
83 { ER, "xor", },
84 { AND, "bic", },
85 { -1, "" },
86 };
87
88 void
89 hopcode( f, o ){
90 /* output the appropriate string from the above table */
91
92 register struct hoptab *q;
93
94 for( q = ioptab; q->opmask>=0; ++q ){
95 if( q->opmask == o ){
96 printf( "%s", q->opstring );
97 /* tbl
98 if( f == 'F' ) printf( "e" );
99 else if( f == 'D' ) printf( "d" );
100 tbl */
101 /* tbl */
102 switch( f ) {
103 case 'L':
104 case 'W':
105 case 'B':
106 case 'D':
107 case 'F':
108 printf("%c", tolower(f));
109 break;
110
111 }
112 /* tbl */
113 return;
114 }
115 }
116 cerror( "no hoptab for %s", opst[o] );
117 }
118
119 char *
120 rnames[] = { /* keyed to register number tokens */
121
122 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5",
123 "%r6", "%r7", "%r8", "%r9", "%r10", "%r11",
124 "%ap", "%fp", "%sp", "%pc",
125 /* The concatenated regs has the name of the lowest */
126 "%r0", "%r1", "%r2", "%r3", "%r4", "%r5",
127 "%r6", "%r7", "%r8", "%r9", "%r10"
128 };
129
130 int
131 tlen(NODE *p)
132 {
133 switch(p->n_type) {
134 case CHAR:
135 case UCHAR:
136 return(1);
137
138 case SHORT:
139 case USHORT:
140 return(2);
141
142 case DOUBLE:
143 case LONGLONG:
144 case ULONGLONG:
145 return(8);
146
147 default:
148 return(4);
149 }
150 }
151
152 void
153 prtype(NODE *n)
154 {
155 static char pt[] = { 0, 0, 'b', 'b', 'w', 'w', 'l', 'l', 0, 0,
156 'q', 'q', 'f', 'd' };
157 TWORD t = n->n_type;
158
159 if (ISPTR(t))
160 t = UNSIGNED;
161
162 if (t > DOUBLE || pt[t] == 0)
163 comperr("prtype: bad type");
164 putchar(pt[t]);
165 }
166
167 /*
168 * Emit conversions as given by the following table. Dest is always reg,
169 * if it should be something else let peephole optimizer deal with it.
170 * This code ensures type correctness in 32-bit registers.
171 * XXX is that necessary?
172 *
173 * From To
174 * char uchar short ushort int uint ll ull float double
175 * char movb movb cvtbw cvtbw cvtbl cvtbl A A cvtbf cvtbd
176 * uchar movb movb movzbw movzbw movzbl movzbl B B G G
177 * short movb movb movw movw cvtwl cvtwl C(A) C(A) cvtwf cvtwd
178 * ushrt movb movb movw movw movzwl movzwl D(B) D(B) H H
179 * int movb movb movw movw movl movl E E cvtlf cvtld
180 * uint movb movb movw movw movl movl F F I I
181 * ll movb movb movw movw movl movl movq movq J K
182 * ull movb movb movw movw movl movl movq movq L M
183 * float cvtfb cvtfb cvtfw cvtfw cvtfl cvtfl N O movf cvtfd
184 * doubl cvtdb cvtdb cvtdw cvtdw cvtdl cvtdl P Q cvtdf movd
185 *
186 * A: cvtbl + sign extend
187 * B: movzbl + zero extend
188 * G: movzbw + cvtwX
189 * H: movzwl + cvtwX
190 * I: cvtld + addX
191 * J: call __floatdisf
192 * K: call __floatdidf
193 * L: xxx + call __floatdisf
194 * M: xxx + call __floatdidf
195 * N: call __fixsfdi
196 * O: call __fixunssfdi
197 * P: call __fixdfdi
198 * Q: call __fixunsdfdi
199 */
200
201 #define MVD 1 /* mov + dest type */
202 #define CVT 2 /* cvt + src type + dst type */
203 #define MVZ 3 /* movz + src type + dst type */
204 #define CSE 4 /* cvt + src type + l + sign extend upper */
205 #define MZE 5 /* movz + src type + l + zero extend upper */
206 #define MLE 6 /* movl + sign extend upper */
207 #define MLZ 7 /* movl + zero extend upper */
208 #define MZC 8 /* movz + cvt */
209
210 static char scary[][10] = {
211 { MVD, MVD, CVT, CVT, CVT, CVT, CSE, CSE, CVT, CVT },
212 { MVD, MVD, MVZ, MVZ, MVZ, MVZ, MZE, MZE, MZC, MZC },
213 { MVD, MVD, MVD, MVD, CVT, CVT, CSE, CSE, CVT, CVT },
214 { MVD, MVD, MVD, MVD, MVZ, MVZ, MZE, MZE, MZC, MZC },
215 { MVD, MVD, MVD, MVD, MVD, MVD, MLE, MLE, CVT, CVT },
216 { MVD, MVD, MVD, MVD, MVD, MVD, MLZ, MLZ, 'I', 'I' },
217 { MVD, MVD, MVD, MVD, MVD, MVD, MVD, MVD, 'J', 'K' },
218 { MVD, MVD, MVD, MVD, MVD, MVD, MVD, MVD, 'L', 'M' },
219 { CVT, CVT, CVT, CVT, CVT, CVT, 'N', 'O', MVD, CVT },
220 { CVT, CVT, CVT, CVT, CVT, CVT, 'P', 'Q', CVT, MVD },
221 };
222
223 static void
224 sconv(NODE *p)
225 {
226 NODE *l = p->n_left;
227 TWORD ts, td;
228 int o;
229
230 /*
231 * Source node may be in register or memory.
232 * Result is always in register.
233 */
234 ts = l->n_type;
235 if (ISPTR(ts))
236 ts = UNSIGNED;
237 td = p->n_type;
238 ts = ts < LONG ? ts-2 : ts-4;
239 td = td < LONG ? td-2 : td-4;
240
241 o = scary[ts][td];
242 switch (o) {
243 case MLE:
244 case MLZ:
245 expand(p, INAREG|INBREG, "\tmovl\tAL,A1\n");
246 break;
247
248 case MVD:
249 if (l->n_op == REG && regno(l) == regno(getlr(p, '1')))
250 break; /* unneccessary move */
251 expand(p, INAREG|INBREG, "\tmovZR\tAL,A1\n");
252 break;
253
254 case CSE:
255 expand(p, INAREG|INBREG, "\tcvtZLl\tAL,A1\n");
256 break;
257
258 case CVT:
259 expand(p, INAREG|INBREG, "\tcvtZLZR\tAL,A1\n");
260 break;
261
262 case MZE:
263 expand(p, INAREG|INBREG, "\tmovzZLl\tAL,A1\n");
264 break;
265
266 case MVZ:
267 expand(p, INAREG|INBREG, "\tmovzZLZR\tAL,A1\n");
268 break;
269
270 case MZC:
271 expand(p, INAREG|INBREG, "\tmovzZLl\tAL,A1\n");
272 expand(p, INAREG|INBREG, "\tcvtlZR\tA1,A1\n");
273 break;
274
275 case 'I': /* unsigned to double */
276 expand(p, INAREG|INBREG, "\tcvtld\tAL,A1\n");
277 printf("\tjgeq\t1f\n");
278 expand(p, INAREG|INBREG, "\taddd2\t$0d4.294967296e+9,A1\n");
279 printf("1:\n");
280 break;
281 default:
282 comperr("unsupported conversion %d", o);
283 }
284 switch (o) {
285 case MLE:
286 case CSE:
287 expand(p, INBREG, "\tashl\t$-31,A1,U1\n");
288 break;
289 case MLZ:
290 case MZE:
291 expand(p, INAREG|INBREG, "\tclrl\tU1\n");
292 break;
293 }
294 }
295
296 /*
297 * Assign a constant from p to q. Both are expected to be leaves by now.
298 * This is for 64-bit integers.
299 */
300 static void
301 casg64(NODE *p)
302 {
303 NODE *l, *r;
304 char *str;
305 int mneg = 1;
306
307 l = p->n_left;
308 r = p->n_right;
309
310 #ifdef PCC_DEBUG
311 if (r->n_op != ICON)
312 comperr("casg");
313 #endif
314 if (r->n_name[0] != '\0') {
315 /* named constant, nothing to do */
316 str = "movq\tAR,AL";
317 mneg = 0;
318 } else if (getlval(r) == 0) {
319 str = "clrq\tAL";
320 mneg = 0;
321 } else if (getlval(r) < 0) {
322 if (getlval(r) >= -63) {
323 setlval(r, -getlval(r));
324 str = "mnegl\tAR,AL";
325 } else if (getlval(r) >= -128) {
326 str = "cvtbl\tAR,AL";
327 } else if (getlval(r) >= -32768) {
328 str = "cvtwl\tAR,AL";
329 } else if (getlval(r) >= -4294967296LL) {
330 str = "movl\tAR,AL";
331 } else {
332 str = "movq\tAR,AL";
333 mneg = 0;
334 }
335 } else {
336 mneg = 0;
337 if (getlval(r) <= 63 || getlval(r) > 4294967295LL) {
338 str = "movq\tAR,AL";
339 } else if (getlval(r) <= 255) {
340 str = "movzbl\tAR,AL\n\tclrl\tUL";
341 } else if (getlval(r) <= 65535) {
342 str = "movzwl\tAR,AL\n\tclrl\tUL";
343 } else /* if (getlval(r) <= 4294967295) */ {
344 str = "movl\tAR,AL\n\tclrl\tUL";
345 }
346 }
347 expand(p, FOREFF, str);
348 if (mneg)
349 expand(p, FOREFF, "\n\tmnegl $1,UL");
350 }
351
352 /*
353 * Assign a constant from p to q. Both are expected to be leaves by now.
354 * This is only for 32-bit integer types.
355 */
356 static void
357 casg(NODE *p)
358 {
359 NODE *l, *r;
360 char *str;
361
362 l = p->n_left;
363 r = p->n_right;
364
365 #ifdef PCC_DEBUG
366 if (r->n_op != ICON)
367 comperr("casg");
368 #endif
369 if (r->n_name[0] != '\0') {
370 /* named constant, nothing to do */
371 str = "movZL\tAR,AL";
372 } else if (getlval(r) == 0) {
373 str = "clrZL\tAL";
374 } else if (getlval(r) < 0) {
375 if (getlval(r) >= -63) {
376 setlval(r, -getlval(r));
377 str = "mnegZL\tAR,AL";
378 } else if (getlval(r) >= -128) {
379 if (l->n_type == CHAR)
380 str = "movb\tAR,AL";
381 else
382 str = "cvtbZL\tAR,AL";
383 } else if (getlval(r) >= -32768) {
384 if (l->n_type == SHORT)
385 str = "movw\tAR,AL";
386 else
387 str = "cvtwZL\tAR,AL";
388 } else
389 str = "movZL\tAR,AL";
390 } else {
391 if (getlval(r) <= 63 || getlval(r) > 65535) {
392 str = "movZL\tAR,AL";
393 } else if (getlval(r) <= 255) {
394 str = l->n_type < SHORT ?
395 "movb\tAR,AL" : "movzbZL\tAR,AL";
396 } else /* if (getlval(r) <= 65535) */ {
397 str = l->n_type < INT ?
398 "movw\tAR,AL" : "movzwZL\tAR,AL";
399 }
400 }
401 expand(p, FOREFF, str);
402 }
403
404 /*
405 * Emit code to compare two longlong numbers.
406 */
407 static void
408 twollcomp(NODE *p)
409 {
410 int u;
411 int s = getlab2();
412 int e = p->n_label;
413 int cb1, cb2;
414
415 u = p->n_op;
416 switch (p->n_op) {
417 case NE:
418 cb1 = 0;
419 cb2 = NE;
420 break;
421 case EQ:
422 cb1 = NE;
423 cb2 = 0;
424 break;
425 case LE:
426 case LT:
427 u += (ULE-LE);
428 /* FALLTHROUGH */
429 case ULE:
430 case ULT:
431 cb1 = GT;
432 cb2 = LT;
433 break;
434 case GE:
435 case GT:
436 u += (ULE-LE);
437 /* FALLTHROUGH */
438 case UGE:
439 case UGT:
440 cb1 = LT;
441 cb2 = GT;
442 break;
443
444 default:
445 cb1 = cb2 = 0; /* XXX gcc */
446 }
447 if (p->n_op >= ULE)
448 cb1 += 4, cb2 += 4;
449 expand(p, 0, " cmpl UL,UR\n");
450 if (cb1) cbgen(cb1, s);
451 if (cb2) cbgen(cb2, e);
452 expand(p, 0, " cmpl AL,AR\n");
453 cbgen(u, e);
454 deflab(s);
455 }
456
457
458 void
459 zzzcode(NODE *p, int c)
460 {
461 NODE *l, *r;
462 TWORD t;
463 int m;
464 char *ch;
465
466 switch (c) {
467 case 'N': /* logical ops, turned into 0-1 */
468 /* use register given by register 1 */
469 cbgen( 0, m=getlab2());
470 deflab( p->n_label );
471 printf( " clrl %s\n", rnames[getlr( p, '1' )->n_rval] );
472 deflab( m );
473 return;
474
475 case 'A': /* Assign a constant directly to a memory position */
476 printf("\t");
477 if (p->n_type < LONG || ISPTR(p->n_type))
478 casg(p);
479 else
480 casg64(p);
481 printf("\n");
482 break;
483
484 case 'B': /* long long compare */
485 twollcomp(p);
486 break;
487
488 case 'C': /* num words pushed on arg stack */
489 printf("$%d", p->n_qual);
490 break;
491
492 case 'D': /* INCR and DECR */
493 zzzcode(p->n_left, 'A');
494 printf("\n ");
495
496 #if 0
497 case 'E': /* INCR and DECR, FOREFF */
498 if (getlval(p->n_right) == 1)
499 {
500 printf("%s", (p->n_op == INCR ? "inc" : "dec") );
501 prtype(p->n_left);
502 printf(" ");
503 adrput(stdout, p->n_left);
504 return;
505 }
506 printf("%s", (p->n_op == INCR ? "add" : "sub") );
507 prtype(p->n_left);
508 printf("2 ");
509 adrput(stdout, p->n_right);
510 printf(",");
511 adrput(p->n_left);
512 return;
513 #endif
514
515 case 'F': /* register type of right operand */
516 {
517 register NODE *n;
518 register int ty;
519
520 n = getlr( p, 'R' );
521 ty = n->n_type;
522
523 if (x2debug) printf("->%d<-", ty);
524
525 if ( ty==DOUBLE) printf("d");
526 else if ( ty==FLOAT ) printf("f");
527 else printf("l");
528 return;
529 }
530
531 case 'G': /* emit conversion instructions */
532 sconv(p);
533 break;
534
535 case 'J': /* jump or ret? */
536 {
537 struct interpass *ip =
538 DLIST_PREV((struct interpass *)p2env.epp, qelem);
539 if (ip->type != IP_DEFLAB ||
540 ip->ip_lbl != getlval(getlr(p, 'L')))
541 expand(p, FOREFF, "jbr LL");
542 else
543 printf("ret");
544 }
545 break;
546
547 case 'L': /* type of left operand */
548 case 'R': /* type of right operand */
549 {
550 register NODE *n;
551
552 n = getlr ( p, c);
553 if (x2debug) printf("->%d<-", n->n_type);
554
555 prtype(n);
556 return;
557 }
558
559 case 'l': /* print out long long constant as hex */
560 case 'r': /* works around a bug in gas */
561 l = getlr(p, c == 'l' ? 'L' : 'R');
562 if (l->n_op == ICON && ISLONGLONG(l->n_type)) {
563 printf("$0x%llx", getlval(l));
564 } else
565 adrput(stdout, l);
566 break;
567
568 case 'O': /* print out emulated ops */
569 expand(p, FOREFF, "\tmovq AR,-(%sp)\n");
570 expand(p, FOREFF, "\tmovq AL,-(%sp)\n");
571 if (p->n_op == DIV && p->n_type == ULONGLONG) ch = "udiv";
572 else if (p->n_op == DIV) ch = "div";
573 else if (p->n_op == MOD && p->n_type == ULONGLONG) ch = "umod";
574 else if (p->n_op == MOD) ch = "mod";
575 else if (p->n_op == MUL) ch = "mul";
576 else ch = 0, comperr("ZO %d", p->n_op);
577 printf("\tcalls $4,__%sdi3\n", ch);
578 break;
579
580
581 case 'Z': /* complement mask for bit instr */
582 printf("$%lld", ~getlval(p->n_right));
583 return;
584
585 case 'U': /* 32 - n, for unsigned right shifts */
586 t = DEUNSIGN(p->n_left->n_type);
587 m = t == CHAR ? 8 : t == SHORT ? 16 : 32;
588 printf("$" CONFMT, m - getlval(p->n_right));
589 return;
590
591 case 'T': /* rounded structure length for arguments */
592 {
593 int size;
594
595 size = attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
596 SETOFF( size, 4);
597 printf("$%d", size);
598 return;
599 }
600
601 case 'S': /* structure assignment */
602 {
603 register int size;
604
605 size = attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
606 SETOFF(size, 4);
607 l = r = NULL; /* XXX gcc */
608 if( p->n_op == STASG ){
609 l = p->n_left;
610 r = p->n_right;
611
612 }
613 else if( p->n_op == STARG ){
614 /* store an arg into a temporary */
615 printf("\tsubl2 $%d,%%sp\n",
616 size < 4 ? 4 : size);
617 l = mklnode(OREG, 0, SP, INT);
618 r = p->n_left;
619 }
620 else cerror( "STASG bad" );
621
622 if( r->n_op == ICON ) r->n_op = NAME;
623 else if( r->n_op == REG ) r->n_op = OREG;
624 else if( r->n_op != OREG ) cerror( "STASG-r" );
625
626 if (size != 0) {
627 if( size <= 0 || size > 65535 )
628 cerror("structure size <0=0 or >65535");
629
630 switch(size) {
631 case 1:
632 printf(" movb ");
633 break;
634 case 2:
635 printf(" movw ");
636 break;
637 case 4:
638 printf(" movl ");
639 break;
640 case 8:
641 printf(" movq ");
642 break;
643 default:
644 printf(" movc3 $%d,", size);
645 break;
646 }
647 adrput(stdout, r);
648 printf(",");
649 adrput(stdout, l);
650 printf("\n");
651 }
652
653 if( r->n_op == NAME ) r->n_op = ICON;
654 else if( r->n_op == OREG ) r->n_op = REG;
655 if (p->n_op == STARG)
656 tfree(l);
657
658 }
659 break;
660
661 default:
662 comperr("illegal zzzcode '%c'", c);
663 }
664 }
665
666 void
667 rmove(int rt, int rs, TWORD t)
668 {
669 char c = (t == FLOAT ? 'f' : t == DOUBLE ? 'd' :
670 t == LONGLONG || t == ULONGLONG ? 'q' : 'l');
671 printf(" mov%c %s,%s\n", c, rnames[rt], rnames[rs]);
672 }
673
674 int
675 rewfld(NODE *p)
676 {
677 return(1);
678 }
679
680 #if 0
681 int
682 callreg(NODE *p)
683 {
684 return( R0 );
685 }
686
687 int
688 base(register NODE *p)
689 {
690 register int o = p->op;
691
692 if( (o==ICON && p->name[0] != '\0')) return( 100 ); /* ie no base reg */
693 if( o==REG ) return( p->rval );
694 if( (o==PLUS || o==MINUS) && p->left->op == REG && p->right->op==ICON)
695 return( p->left->rval );
696 if( o==OREG && !R2TEST(p->rval) && (p->type==INT || p->type==UNSIGNED || ISPTR(p->type)) )
697 return( p->rval + 0200*1 );
698 if( o==INCR && p->left->op==REG ) return( p->left->rval + 0200*2 );
699 if( o==ASG MINUS && p->left->op==REG) return( p->left->rval + 0200*4 );
700 if( o==UNARY MUL && p->left->op==INCR && p->left->left->op==REG
701 && (p->type==INT || p->type==UNSIGNED || ISPTR(p->type)) )
702 return( p->left->left->rval + 0200*(1+2) );
703 return( -1 );
704 }
705
706 int
707 offset(register NODE *p, int tyl)
708 {
709
710 if( tyl==1 && p->op==REG && (p->type==INT || p->type==UNSIGNED) ) return( p->rval );
711 if( (p->op==LS && p->left->op==REG && (p->left->type==INT || p->left->type==UNSIGNED) &&
712 (p->right->op==ICON && p->right->name[0]=='\0')
713 && (1<<p->right->lval)==tyl))
714 return( p->left->rval );
715 return( -1 );
716 }
717 #endif
718
719 #if 0
720 void
721 makeor2( p, q, b, o) register NODE *p, *q; register int b, o; {
722 register NODE *t;
723 NODE *f;
724
725 p->n_op = OREG;
726 f = p->n_left; /* have to free this subtree later */
727
728 /* init base */
729 switch (q->n_op) {
730 case ICON:
731 case REG:
732 case OREG:
733 t = q;
734 break;
735
736 case MINUS:
737 q->n_right->n_lval = -q->n_right->n_lval;
738 case PLUS:
739 t = q->n_right;
740 break;
741
742 case UMUL:
743 t = q->n_left->n_left;
744 break;
745
746 default:
747 cerror("illegal makeor2");
748 t = NULL; /* XXX gcc */
749 }
750
751 p->n_lval = t->n_lval;
752 p->n_name = t->n_name;
753
754 /* init offset */
755 p->n_rval = R2PACK( (b & 0177), o, (b>>7) );
756
757 tfree(f);
758 return;
759 }
760
761 int
762 canaddr( p ) NODE *p; {
763 register int o = p->n_op;
764
765 if( o==NAME || o==REG || o==ICON || o==OREG || (o==UMUL && shumul(p->n_left, STARNM|SOREG)) ) return(1);
766 return(0);
767 }
768
769 shltype( o, p ) register NODE *p; {
770 return( o== REG || o == NAME || o == ICON || o == OREG || ( o==UMUL && shumul(p->n_left, STARNM|SOREG)) );
771 }
772 #endif
773
774 int
775 fldexpand(NODE *p, int cookie, char **cp)
776 {
777 return 0;
778 }
779
780 int
781 flshape(register NODE *p)
782 {
783 return( p->n_op == REG || p->n_op == NAME || p->n_op == ICON ||
784 (p->n_op == OREG && (!R2TEST(p->n_rval) || tlen(p) == 1)) );
785 }
786
787 int
788 shtemp(register NODE *p)
789 {
790 if( p->n_op == STARG ) p = p->n_left;
791 return( p->n_op==NAME || p->n_op ==ICON || p->n_op == OREG || (p->n_op==UMUL && shumul(p->n_left, STARNM|SOREG)) );
792 }
793
794 /*
795 * Shape matches for UMUL. Cooperates with offstar().
796 */
797 int
798 shumul(NODE *p, int shape)
799 {
800
801 if (x2debug)
802 printf("shumul(%p)\n", p);
803
804 /* Turns currently anything into OREG on vax */
805 if (shape & SOREG)
806 return SROREG;
807 return SRNOPE;
808 }
809
810
811 #ifdef notdef
812 int
813 shumul( p, shape ) register NODE *p; int shape; {
814 register int o;
815
816 if (x2debug) {
817 printf("\nshumul:op=%d,lop=%d,rop=%d", p->n_op, p->n_left->n_op, p->n_right->n_op);
818 printf(" prname=%s,plty=%d, prlval=%lld\n", p->n_right->n_name, p->n_left->n_type, p->n_right->n_lval);
819 }
820
821
822 o = p->n_op;
823 if( o == NAME || (o == OREG && !R2TEST(p->n_rval)) || o == ICON )
824 if (shape & STARNM)
825 return SRDIR;
826
827 if( ( o == INCR || o == ASG MINUS ) &&
828 ( p->n_left->n_op == REG && p->n_right->n_op == ICON ) &&
829 p->n_right->n_name[0] == '\0' )
830 {
831 switch (p->n_left->n_type)
832 {
833 case CHAR|PTR:
834 case UCHAR|PTR:
835 o = 1;
836 break;
837
838 case SHORT|PTR:
839 case USHORT|PTR:
840 o = 2;
841 break;
842
843 case INT|PTR:
844 case UNSIGNED|PTR:
845 case LONG|PTR:
846 case ULONG|PTR:
847 case FLOAT|PTR:
848 o = 4;
849 break;
850
851 case DOUBLE|PTR:
852 o = 8;
853 break;
854
855 default:
856 if ( ISPTR(p->n_left->n_type) ) {
857 o = 4;
858 break;
859 }
860 else return(0);
861 }
862 return( p->n_right->n_lval == o ? STARREG : 0);
863 }
864
865 return( SRNOPE );
866 }
867 #endif
868
869 void
870 adrcon(CONSZ val)
871 {
872 comperr("adrcon");
873 printf( "$" );
874 printf( CONFMT, val );
875 }
876
877 void
878 conput(FILE *fp, NODE *p)
879 {
880 switch( p->n_op ){
881
882 case ICON:
883 acon( p );
884 return;
885
886 case REG:
887 printf( "%s", rnames[p->n_rval] );
888 return;
889
890 default:
891 cerror( "illegal conput" );
892 }
893 }
894
895 void
896 insput(register NODE *p)
897 {
898 cerror( "insput" );
899 }
900
901 /*
902 * Write out the upper address, like the upper register of a 2-register
903 * reference, or the next memory location.
904 */
905 void
906 upput(NODE *p, int size)
907 {
908
909 size /= SZCHAR;
910 switch (p->n_op) {
911 case REG:
912 printf("%s", rnames[regno(p)-16+1]);
913 break;
914
915 case NAME:
916 if (kflag)
917 comperr("upput NAME");
918 case OREG:
919 setlval(p, getlval(p) + size);
920 adrput(stdout, p);
921 setlval(p, getlval(p) - size);
922 break;
923 case ICON:
924 printf("$" CONFMT, (getlval(p) >> 32) & 0xffffffff);
925 break;
926 default:
927 comperr("upput bad op %d size %d", p->n_op, size);
928 }
929 }
930
931 void
932 adrput(FILE *fp, NODE *p)
933 {
934 register int r;
935 /* output an address, with offsets, from p */
936
937 if( p->n_op == FLD ){
938 p = p->n_left;
939 }
940 switch( p->n_op ){
941
942 case NAME:
943 acon( p );
944 return;
945
946 case ICON:
947 /* addressable value of the constant */
948 if (p->n_name[0] == '\0') /* uses xxxab */
949 printf("$");
950 if (ISLONGLONG(p->n_type))
951 printf("0x%llx", getlval(p));
952 else
953 acon(p);
954 return;
955
956 case REG:
957 printf( "%s", rnames[p->n_rval] );
958 return;
959
960 case OREG:
961 r = p->n_rval;
962 if( R2TEST(r) ){ /* double indexing */
963 register int flags;
964
965 flags = R2UPK3(r);
966 if( flags & 1 ) printf("*");
967 if( flags & 4 ) printf("-");
968 if( getlval(p) != 0 || p->n_name[0] != '\0' ) acon(p);
969 if( R2UPK1(r) != 100) printf( "(%s)", rnames[R2UPK1(r)] );
970 if( flags & 2 ) printf("+");
971 printf( "[%s]", rnames[R2UPK2(r)] );
972 return;
973 }
974 if( r == AP ){ /* in the argument region */
975 if( getlval(p) <= 0 || p->n_name[0] != '\0' )
976 werror( "bad arg temp" );
977 printf( CONFMT, getlval(p) );
978 printf( "(%%ap)" );
979 return;
980 }
981 if( getlval(p) != 0 || p->n_name[0] != '\0') acon( p );
982 printf( "(%s)", rnames[p->n_rval] );
983 return;
984
985 case UMUL:
986 /* STARNM or STARREG found */
987 if( tshape(p, STARNM) ) {
988 printf( "*" );
989 adrput(0, p->n_left);
990 }
991 else { /* STARREG - really auto inc or dec */
992 register NODE *q;
993
994 /* tbl
995 p = p->n_left;
996 p->n_left->n_op = OREG;
997 if( p->n_op == INCR ) {
998 adrput( p->n_left );
999 printf( "+" );
1000 }
1001 else {
1002 printf( "-" );
1003 adrput( p->n_left );
1004 }
1005 tbl */
1006 #ifdef notyet
1007 printf("%c(%s)%c", (p->n_left->n_op==INCR ? '\0' : '-'),
1008 rnames[p->n_left->n_left->n_rval],
1009 (p->n_left->n_op==INCR ? '+' : '\0') );
1010 #else
1011 printf("%c(%s)%c", '-',
1012 rnames[p->n_left->n_left->n_rval],
1013 '\0' );
1014 #endif
1015 p->n_op = OREG;
1016 p->n_rval = p->n_left->n_left->n_rval;
1017 q = p->n_left;
1018 #ifdef notyet
1019
1020 setlval(p, (p->n_left->n_op == INCR ? -p->n_left->n_right->n_lval : 0));
1021 #else
1022 setlval(p, 0);
1023 #endif
1024 p->n_name[0] = '\0';
1025 tfree(q);
1026 }
1027 return;
1028
1029 default:
1030 cerror( "illegal address" );
1031 return;
1032 }
1033
1034 }
1035
1036 /*
1037 * print out a constant
1038 */
1039 void
1040 acon(NODE *p)
1041 {
1042 int u = (int)getlval(p);
1043 CONSZ v = u;
1044
1045 if (p->n_name[0] == '\0') {
1046 printf(CONFMT, v);
1047 } else if( getlval(p) == 0 ) {
1048 printf("%s", p->n_name);
1049 } else {
1050 printf("%s+", p->n_name);
1051 printf(CONFMT, v);
1052 }
1053 }
1054
1055 #if 0
1056 genscall( p, cookie ) register NODE *p; {
1057 /* structure valued call */
1058 return( gencall( p, cookie ) );
1059 }
1060
1061 /* tbl */
1062 int gc_numbytes;
1063 /* tbl */
1064
1065 gencall( p, cookie ) register NODE *p; {
1066 /* generate the call given by p */
1067 register NODE *p1, *ptemp;
1068 register temp, temp1;
1069 register m;
1070
1071 if( p->right ) temp = argsize( p->right );
1072 else temp = 0;
1073
1074 if( p->op == STCALL || p->op == UNARY STCALL ){
1075 /* set aside room for structure return */
1076
1077 if( p->stsize > temp ) temp1 = p->stsize;
1078 else temp1 = temp;
1079 }
1080
1081 if( temp > maxargs ) maxargs = temp;
1082 SETOFF(temp1,4);
1083
1084 if( p->right ){ /* make temp node, put offset in, and generate args */
1085 ptemp = talloc();
1086 ptemp->op = OREG;
1087 setlval(ptemp, -1);
1088 ptemp->rval = SP;
1089 ptemp->name[0] = '\0';
1090 ptemp->rall = NOPREF;
1091 ptemp->su = 0;
1092 genargs( p->right, ptemp );
1093 nfree(ptemp);
1094 }
1095
1096 p1 = p->left;
1097 if( p1->op != ICON ){
1098 if( p1->op != REG ){
1099 if( p1->op != OREG || R2TEST(p1->rval) ){
1100 if( p1->op != NAME ){
1101 order( p1, INAREG );
1102 }
1103 }
1104 }
1105 }
1106
1107 /*
1108 if( p1->op == REG && p->rval == R5 ){
1109 cerror( "call register overwrite" );
1110 }
1111 */
1112 /* tbl
1113 setup gc_numbytes so reference to ZC works */
1114
1115 gc_numbytes = temp;
1116 /* tbl */
1117
1118 p->op = UNARY CALL;
1119 m = match( p, INTAREG|INTBREG );
1120 /* tbl
1121 switch( temp ) {
1122 case 0:
1123 break;
1124 case 2:
1125 printf( " tst (%sp)+\n" );
1126 break;
1127 case 4:
1128 printf( " cmp (%sp)+,(%sp)+\n" );
1129 break;
1130 default:
1131 printf( " add $%d,%sp\n", temp);
1132 }
1133 tbl */
1134 return(m != MDONE);
1135 }
1136 #endif
1137
1138 static char *
1139 ccbranches[] = {
1140 "jeql",
1141 "jneq",
1142 "jleq",
1143 "jlss",
1144 "jgeq",
1145 "jgtr",
1146 "jlequ",
1147 "jlssu",
1148 "jgequ",
1149 "jgtru",
1150 };
1151
1152 /*
1153 * printf conditional and unconditional branches
1154 */
1155 void
1156 cbgen(int o, int lab)
1157 {
1158
1159 if (o == 0) {
1160 printf(" jbr " LABFMT "\n", lab);
1161 } else {
1162 if (o > UGT)
1163 comperr("bad conditional branch: %s", opst[o]);
1164 printf("\t%s\t" LABFMT "\n", ccbranches[o-EQ], lab);
1165 }
1166 }
1167
1168 static void
1169 mkcall(NODE *p, char *name)
1170 {
1171 p->n_op = CALL;
1172 p->n_right = mkunode(FUNARG, p->n_left, 0, p->n_left->n_type);
1173 p->n_left = mklnode(ICON, 0, 0, FTN|p->n_type);
1174 p->n_left->n_name = name;
1175 }
1176
1177 /* do local tree transformations and optimizations */
1178 static void
1179 optim2(NODE *p, void *arg)
1180 {
1181 NODE *r, *s;
1182 TWORD lt;
1183
1184 switch (p->n_op) {
1185 case DIV:
1186 case MOD:
1187 if (p->n_type == USHORT || p->n_type == UCHAR) {
1188 r = mkunode(SCONV, p->n_left, 0, UNSIGNED);
1189 r = mkunode(FUNARG, r, 0, UNSIGNED);
1190 s = mkunode(SCONV, p->n_right, 0, UNSIGNED);
1191 s = mkunode(FUNARG, s, 0, UNSIGNED);
1192 r = mkbinode(CM, r, s, INT);
1193 s = mklnode(ICON, 0, 0, FTN|UNSIGNED);
1194 s->n_name = p->n_op == MOD ? "__urem" : "__udiv";
1195 p->n_left = mkbinode(CALL, s, r, UNSIGNED);
1196 p->n_op = SCONV;
1197 } else if (p->n_type == UNSIGNED) {
1198 p->n_left = mkunode(FUNARG, p->n_left, 0, UNSIGNED);
1199 p->n_right = mkunode(FUNARG, p->n_right, 0, UNSIGNED);
1200 p->n_right = mkbinode(CM, p->n_left, p->n_right, INT);
1201 p->n_left = mklnode(ICON, 0, 0, FTN|UNSIGNED);
1202 p->n_left->n_name = p->n_op == MOD ? "__urem" : "__udiv";
1203 p->n_op = CALL;
1204 }
1205 break;
1206
1207 case RS:
1208 if (p->n_type == ULONGLONG) {
1209 p->n_right = mkbinode(CM,
1210 mkunode(FUNARG, p->n_left, 0, p->n_left->n_type),
1211 mkunode(FUNARG, p->n_right, 0, p->n_right->n_type),
1212 INT);
1213 p->n_left = mklnode(ICON, 0, 0, FTN|p->n_type);
1214 p->n_left->n_name = "__lshrdi3";
1215 p->n_op = CALL;
1216 } else if (p->n_type == INT || p->n_type == LONGLONG) {
1217 /* convert >> to << with negative shift count */
1218 /* RS of char & short must use extv */
1219 if (p->n_right->n_op == ICON) {
1220 setlval(p->n_right, -getlval(p->n_right));
1221 } else if (p->n_right->n_op == UMINUS) {
1222 r = p->n_right->n_left;
1223 nfree(p->n_right);
1224 p->n_right = r;
1225 } else {
1226 p->n_right = mkunode(UMINUS, p->n_right,
1227 0, p->n_right->n_type);
1228 }
1229 p->n_op = LS;
1230 }
1231 break;
1232
1233 case AND:
1234 /* commute L and R to eliminate compliments and constants */
1235 if ((p->n_left->n_op == ICON && p->n_left->n_name[0] == 0) ||
1236 p->n_left->n_op==COMPL) {
1237 r = p->n_left;
1238 p->n_left = p->n_right;
1239 p->n_right = r;
1240 }
1241 /* change meaning of AND to ~R&L - bic on pdp11 */
1242 r = p->n_right;
1243 if (r->n_op == ICON && r->n_name[0] == 0) {
1244 /* compliment constant */
1245 setlval(r, ~getlval(r));
1246 } else if (r->n_op == COMPL) { /* ~~A => A */
1247 s = r->n_left;
1248 nfree(r);
1249 p->n_right = s;
1250 } else { /* insert complement node */
1251 p->n_right = mkunode(COMPL, r, 0, r->n_type);
1252 }
1253 break;
1254 case SCONV:
1255 lt = p->n_left->n_type;
1256 switch (p->n_type) {
1257 case LONGLONG:
1258 if (lt == FLOAT)
1259 mkcall(p, "__fixsfdi");
1260 else if (lt == DOUBLE)
1261 mkcall(p, "__fixdfdi");
1262 break;
1263 case ULONGLONG:
1264 if (lt == FLOAT)
1265 mkcall(p, "__fixunssfdi");
1266 else if (lt == DOUBLE)
1267 mkcall(p, "__fixunsdfdi");
1268 break;
1269 case FLOAT:
1270 if (lt == LONGLONG)
1271 mkcall(p, "__floatdisf");
1272 else if (lt == ULONGLONG) {
1273 p->n_left = mkunode(SCONV, p->n_left,0, DOUBLE);
1274 p->n_type = FLOAT;
1275 mkcall(p->n_left, "__floatundidf");
1276 } else if (lt == UNSIGNED) {
1277 /* insert an extra double-to-float sconv */
1278 p->n_left = mkunode(SCONV, p->n_left,0, DOUBLE);
1279 }
1280 break;
1281 case DOUBLE:
1282 if (lt == LONGLONG)
1283 mkcall(p, "__floatdidf");
1284 else if (lt == ULONGLONG)
1285 mkcall(p, "__floatundidf");
1286 break;
1287
1288 }
1289 break;
1290 }
1291 }
1292
1293 static void
1294 aofname(NODE *p, void *arg)
1295 {
1296 int o = optype(p->n_op);
1297 TWORD t;
1298
1299 if (o == LTYPE || p->n_op == ADDROF)
1300 return;
1301 t = p->n_left->n_type;
1302 if (p->n_left->n_op == NAME && ISLONGLONG(t))
1303 p->n_left = mkunode(UMUL,
1304 mkunode(ADDROF, p->n_left, 0, INCREF(t)), 0, t);
1305 if (o == BITYPE && p->n_right->n_op == NAME &&
1306 ISLONGLONG(p->n_right->n_type)) {
1307 t = p->n_right->n_type;
1308 p->n_right = mkunode(UMUL,
1309 mkunode(ADDROF, p->n_right, 0, INCREF(t)), 0, t);
1310 }
1311 }
1312
1313 void
1314 myreader(struct interpass *ipole)
1315 {
1316 struct interpass *ip;
1317
1318 DLIST_FOREACH(ip, ipole, qelem) {
1319 if (ip->type != IP_NODE)
1320 continue;
1321 if (kflag)
1322 walkf(ip->ip_node, aofname, 0);
1323 walkf(ip->ip_node, optim2, 0);
1324 }
1325 }
1326
1327 void
1328 mycanon(NODE *p)
1329 {
1330 }
1331
1332 void
1333 myoptim(struct interpass *ip)
1334 {
1335 }
1336
1337 /*
1338 * Return argument size in regs.
1339 */
1340 static int
1341 argsiz(NODE *p)
1342 {
1343 TWORD t = p->n_type;
1344
1345 if (t == STRTY || t == UNIONTY)
1346 return (attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0)+3)/4;
1347 return szty(t);
1348 }
1349
1350 /*
1351 * Last chance to do something before calling a function.
1352 */
1353 void
1354 lastcall(NODE *p)
1355 {
1356 NODE *op = p;
1357 int size = 0;
1358
1359 /* Calculate argument sizes */
1360 p->n_qual = 0;
1361 if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
1362 return;
1363 for (p = p->n_right; p->n_op == CM; p = p->n_left)
1364 size += argsiz(p->n_right);
1365 if (p->n_op != ASSIGN)
1366 size += argsiz(p);
1367 op->n_qual = size; /* XXX */
1368 }
1369
1370 /*
1371 * Return a class suitable for a specific type.
1372 */
1373 int
1374 gclass(TWORD t)
1375 {
1376 return (szty(t) == 2 ? CLASSB : CLASSA);
1377 }
1378
1379 /*
1380 * For class c, find worst-case displacement of the number of
1381 * registers in the array r[] indexed by class.
1382 */
1383 int
1384 COLORMAP(int c, int *r)
1385 {
1386 int num;
1387 int a,b;
1388
1389 a = r[CLASSA];
1390 b = r[CLASSB];
1391 switch (c) {
1392 case CLASSA:
1393 /* there are 12 classa, so min 6 classb are needed to block */
1394 num = b * 2;
1395 num += a;
1396 return num < 12;
1397 case CLASSB:
1398 if (b > 3) return 0;
1399 if (b > 2 && a) return 0;
1400 if (b > 1 && a > 2) return 0;
1401 if (b && a > 3) return 0;
1402 if (a > 5) return 0;
1403 return 1;
1404 }
1405 comperr("COLORMAP");
1406 return 0; /* XXX gcc */
1407 }
1408
1409 /*
1410 * Special shapes.
1411 */
1412 int
1413 special(NODE *p, int shape)
1414 {
1415 return SRNOPE;
1416 }
1417
1418 /*
1419 * Target-dependent command-line options.
1420 */
1421 void
1422 mflags(char *str)
1423 {
1424 }
1425 /*
1426 * Do something target-dependent for xasm arguments.
1427 * Supposed to find target-specific constraints and rewrite them.
1428 */
1429 int
1430 myxasm(struct interpass *ip, NODE *p)
1431 {
1432 char *c;
1433 int i;
1434
1435 /* Discard o<> constraints since they will not be generated */
1436 for (c = p->n_name; *c; c++) {
1437 if (*c == 'o' || *c == '<' || *c == '>') {
1438 for (i = 0; c[i]; i++)
1439 c[i] = c[i+1];
1440 c--;
1441 }
1442 }
1443 return 0;
1444 }
1445
1446 int
1447 xasmconstregs(char *s)
1448 {
1449 int i;
1450
1451 for (i = 0; i < 16; i++)
1452 if (strcmp(&rnames[i][1], s) == 0)
1453 return i;
1454 return -1;
1455 }