Debian Codegraph @

arch/nova/local2.c

raw symbols SCIP
1 /* $Id: local2.c,v 1.21 2022/01/11 08:22:37 ragge Exp $ */
2 /*
3 * Copyright (c) 2006 Anders Magnusson ([email protected]).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29
30 # include "pass2.h"
31 # include <ctype.h>
32 # include <string.h>
33 #include <stdlib.h>
34
35 void acon(NODE *p);
36 int argsize(NODE *p);
37
38 static int maxargsz;
39
40 void
41 deflab(int label)
42 {
43 printf(LABFMT ":\n", label);
44 }
45
46 void
47 prologue(struct interpass_prolog *ipp)
48 {
49
50 #ifdef os_none
51 if (ipp->ipp_vis)
52 printf(" .ENT %s\n", ipp->ipp_name);
53 printf(" .ZREL\n");
54 printf("%s: .%s\n", ipp->ipp_name, ipp->ipp_name);
55 printf(" .NREL\n");
56 printf(" 0%o\n", p2maxautooff);
57 printf(".%s:\n", ipp->ipp_name);
58 printf(" sta 3,@csp\n"); /* put ret pc on stack */
59 printf(" jsr @prolog\n"); /* jump to prolog */
60 #else
61 printf("#BEGFTN\n");
62 if (ipp->ipp_vis)
63 printf(" .globl %s\n", ipp->ipp_name);
64 printf("%s:\n", ipp->ipp_name);
65 printf(" sta 3,@spref\n"); /* put ret pc on stack */
66 printf(" jsr @csav\n"); /* jump to prolog */
67 printf(" .word 0%o\n", p2maxautooff);
68 #endif
69 }
70
71 void
72 eoftn(struct interpass_prolog *ipp)
73 {
74 if (ipp->ipp_ip.ip_lbl == 0)
75 return; /* no code needs to be generated */
76 printf(" jmp @cret\n");
77 printf("#ENDFTN\n");
78 }
79
80 /*
81 * add/sub/...
82 *
83 * Param given:
84 */
85 void
86 hopcode(int f, int o)
87 {
88 char *str = 0;
89
90 switch (o) {
91 case PLUS:
92 str = "add";
93 break;
94 case MINUS:
95 str = "sub";
96 break;
97 case AND:
98 str = "and";
99 break;
100 case OR:
101 cerror("hopcode OR");
102 break;
103 case ER:
104 cerror("hopcode xor");
105 str = "xor";
106 break;
107 default:
108 comperr("hopcode2: %d", o);
109 str = 0; /* XXX gcc */
110 }
111 printf("%s%c", str, f);
112 }
113
114 #if 0
115 /*
116 * Return type size in bytes. Used by R2REGS, arg 2 to offset().
117 */
118 int
119 tlen(p) NODE *p;
120 {
121 switch(p->n_type) {
122 case CHAR:
123 case UCHAR:
124 return(1);
125
126 case SHORT:
127 case USHORT:
128 return(SZSHORT/SZCHAR);
129
130 case DOUBLE:
131 return(SZDOUBLE/SZCHAR);
132
133 case INT:
134 case UNSIGNED:
135 case LONG:
136 case ULONG:
137 return(SZINT/SZCHAR);
138
139 case LONGLONG:
140 case ULONGLONG:
141 return SZLONGLONG/SZCHAR;
142
143 default:
144 if (!ISPTR(p->n_type))
145 comperr("tlen type %d not pointer");
146 return SZPOINT(p->n_type)/SZCHAR;
147 }
148 }
149 #endif
150
151 int
152 fldexpand(NODE *p, int cookie, char **cp)
153 {
154 return 0;
155 }
156
157 #if 0
158 /*
159 * Assign to a bitfield.
160 * Clumsy at least, but what to do?
161 */
162 static void
163 bfasg(NODE *p)
164 {
165 NODE *fn = p->n_left;
166 int shift = UPKFOFF(fn->n_rval);
167 int fsz = UPKFSZ(fn->n_rval);
168 int andval, tch = 0;
169
170 /* get instruction size */
171 switch (p->n_type) {
172 case CHAR: case UCHAR: tch = 'b'; break;
173 case SHORT: case USHORT: tch = 'w'; break;
174 case INT: case UNSIGNED: tch = 'l'; break;
175 default: comperr("bfasg");
176 }
177
178 /* put src into a temporary reg */
179 printf(" mov%c ", tch);
180 adrput(stdout, getlr(p, 'R'));
181 printf(",");
182 adrput(stdout, getlr(p, '1'));
183 printf("\n");
184
185 /* AND away the bits from dest */
186 andval = ~(((1 << fsz) - 1) << shift);
187 printf(" and%c $%d,", tch, andval);
188 adrput(stdout, fn->n_left);
189 printf("\n");
190
191 /* AND away unwanted bits from src */
192 andval = ((1 << fsz) - 1);
193 printf(" and%c $%d,", tch, andval);
194 adrput(stdout, getlr(p, '1'));
195 printf("\n");
196
197 /* SHIFT left src number of bits */
198 if (shift) {
199 printf(" sal%c $%d,", tch, shift);
200 adrput(stdout, getlr(p, '1'));
201 printf("\n");
202 }
203
204 /* OR in src to dest */
205 printf(" or%c ", tch);
206 adrput(stdout, getlr(p, '1'));
207 printf(",");
208 adrput(stdout, fn->n_left);
209 printf("\n");
210 }
211 #endif
212
213 #if 0
214 /*
215 * Push a structure on stack as argument.
216 * the scratch registers are already free here
217 */
218 static void
219 starg(NODE *p)
220 {
221 int sz = attr_find(p->n_ap, ATTR_P2STRUCT)->iarg(0);
222 printf(" subl $%d,%%esp\n", sz);
223 printf(" pushl $%d\n", sz);
224 expand(p, 0, " pushl AL\n");
225 expand(p, 0, " leal 8(%esp),A1\n");
226 expand(p, 0, " pushl A1\n");
227 printf(" call memcpy\n");
228 printf(" addl $12,%%esp\n");
229 }
230 #endif
231
232 /*
233 * Compare long against 0.
234 * Value is in AC0/AC1.
235 */
236 static void
237 lzero(NODE *p)
238 {
239 int l = 0;
240
241 switch (p->n_op) {
242 case EQ:
243 expand(p, 0, " add# 0,1,snr\n");
244 break;
245 case NE:
246 expand(p, 0, " add# 0,1,szr\n");
247 break;
248 case GE:
249 expand(p, 0, " movl# 0,0,snc\n");
250 break;
251 case LT:
252 expand(p, 0, " movl# 0,0,szc\n");
253 break;
254 case GT:
255 l = getlab2();
256 expand(p, 0, " movl# 0,0,szc\n"); /* high word */
257 printf(" jmp " LABFMT "\n", l);
258 expand(p, 0, " add# 0,1,szr\n");
259 break;
260 case LE:
261 expand(p, 0, " movl# 0,0,szc\n jmp LC\n"); /* high word */
262 expand(p, 0, " add# 0,1,snr\n");
263 break;
264 default:
265 comperr("lzero %d", p->n_op);
266 }
267 expand(p, 0, " jmp LC\n");
268 if (l)
269 printf(LABFMT ":\n", l);
270 }
271
272 /*
273 * Compare two long values.
274 * Left value is in AC0/AC1, right in memory.
275 * AC2 is available for use.
276 */
277 static void
278 lcmp(NODE *p)
279 {
280
281 expand(p, 0, " lda 2,AR\n");
282 expand(p, 0, " sta 2,@sp\n");
283 expand(p, 0, " lda 2,UR\n");
284 expand(p, 0, " sta 2,@sp\n");
285
286 switch (p->n_op) {
287 case LE:
288 expand(p, 0, " jsr __pcc_lle\n");
289 expand(p, 0, " jmp LC\n");
290 break;
291
292 default:
293 comperr("lcmp %d", p->n_op);
294 }
295 }
296
297 void
298 zzzcode(NODE *p, int c)
299 {
300 struct attr *ap;
301 NODE *l;
302
303 switch (c) {
304 case 'N': /* compare two long */
305 lcmp(p);
306 break;
307
308 case 'Q': /* struct assign */
309 /* right is in ac2, left is name or oreg */
310 l = getlr(p, 'L');
311 if (l->n_op == NAME) {
312 l->n_op = ICON;
313 expand(p, 0, " lda 0,AL\n");
314 l->n_op = NAME;
315 } else { /* in OREG */
316 printf(" lda 0,[.word %d]\n", (int)getlval(l));
317 printf(" add 3,0\n");
318 }
319 ap = attr_find(p->n_ap, ATTR_P2STRUCT);
320 printf(" lda 1,[.word %d]\n", -(ap->iarg(0) >> 1));
321 printf(" jsr __stcpy\n");
322 break;
323
324 case 'Z': /* compare long against 0 */
325 lzero(p);
326 break;
327
328
329 default:
330 comperr("zzzcode %c", c);
331 }
332 }
333
334 /*ARGSUSED*/
335 int
336 rewfld(NODE *p)
337 {
338 return(1);
339 }
340
341 int canaddr(NODE *);
342 int
343 canaddr(NODE *p)
344 {
345 int o = p->n_op;
346
347 if (o==NAME || o==REG || o==ICON || o==OREG ||
348 (o==UMUL && shumul(p->n_left, SOREG)))
349 return(1);
350 return(0);
351 }
352
353 /*
354 * Does the bitfield shape match?
355 */
356 int
357 flshape(NODE *p)
358 {
359 int o = p->n_op;
360
361 cerror("flshape");
362 if (o == OREG || o == REG || o == NAME)
363 return SRDIR; /* Direct match */
364 if (o == UMUL && shumul(p->n_left, SOREG))
365 return SROREG; /* Convert into oreg */
366 return SRREG; /* put it into a register */
367 }
368
369 /* INTEMP shapes must not contain any temporary registers */
370 /* XXX should this go away now? */
371 int
372 shtemp(NODE *p)
373 {
374 return 0;
375 #if 0
376 int r;
377
378 if (p->n_op == STARG )
379 p = p->n_left;
380
381 switch (p->n_op) {
382 case REG:
383 return (!istreg(p->n_rval));
384
385 case OREG:
386 r = p->n_rval;
387 if (R2TEST(r)) {
388 if (istreg(R2UPK1(r)))
389 return(0);
390 r = R2UPK2(r);
391 }
392 return (!istreg(r));
393
394 case UMUL:
395 p = p->n_left;
396 return (p->n_op != UMUL && shtemp(p));
397 }
398
399 if (optype(p->n_op) != LTYPE)
400 return(0);
401 return(1);
402 #endif
403 }
404
405 void
406 adrcon(CONSZ val)
407 {
408 printf("[" CONFMT "]", val);
409 }
410
411 /*
412 * Conput prints out a constant.
413 */
414 void
415 conput(FILE *fp, NODE *p)
416 {
417 int val = getlval(p);
418
419 switch (p->n_op) {
420 case NAME:
421 case ICON:
422 if (p->n_name[0] != '\0') {
423 fprintf(fp, "%s", p->n_name);
424 if (val)
425 fprintf(fp, "+0%o", val & 0177777);
426 } else
427 fprintf(fp, "0%o", val & 0177777);
428 return;
429
430 default:
431 comperr("illegal conput, p %p", p);
432 }
433 }
434
435 /*ARGSUSED*/
436 void
437 insput(NODE *p)
438 {
439 comperr("insput");
440 }
441
442 /*
443 * Write out the upper address, like the upper register of a 2-register
444 * reference, or the next memory location.
445 */
446 void
447 upput(NODE *p, int size)
448 {
449 //printf("upput\n");
450 //fwalk(p, e2print, 0);
451 switch (p->n_op) {
452 case REG:
453 printf("1"); /* ac1 is always upper */
454 break;
455
456 case NAME:
457 case OREG:
458 setlval(p, getlval(p) + 1);
459 adrput(stdout, p);
460 setlval(p, getlval(p) - 1);
461 break;
462 case ICON:
463 printf("[.word " CONFMT "]", getlval(p) & 0177777);
464 break;
465 default:
466 comperr("upput bad op %d size %d", p->n_op, size);
467 }
468 }
469
470 void
471 adrput(FILE *io, NODE *p)
472 {
473 int i;
474 /* output an address, with offsets, from p */
475
476 static int looping = 7;
477 if (looping == 0) {
478 looping = 1;
479 printf("adrput %p\n", p);
480 fwalk(p, e2print, 0);
481 looping = 0;
482 }
483 if (p->n_op == FLD)
484 p = p->n_left;
485
486 switch (p->n_op) {
487 case NAME:
488 fputc('@', io);
489 /* FALLTHROUGH */
490 case ICON:
491 fputc('[', io);
492 if (p->n_type == INCREF(CHAR) || p->n_type == INCREF(UCHAR))
493 fprintf(io, ".bptr ");
494 else
495 fprintf(io, ".word ");
496 if (p->n_type == LONG || p->n_type == ULONG)
497 printf(CONFMT, getlval(p) >> 16);
498 else
499 conput(io, p);
500 fputc(']', io);
501 break;
502
503 case OREG:
504 if (p->n_name[0])
505 comperr("name in OREG");
506 i = (int)getlval(p);
507 if (i < 0) {
508 putchar('-');
509 i = -i;
510 }
511 if (regno(p) != AC2 && regno(p) != AC3)
512 comperr("bad index reg %d", regno(p));
513 printf("0%o,%s", i, rnames[regno(p)]);
514 break;
515
516 case REG:
517 if (p->n_type == LONG || p->n_type == ULONG)
518 fprintf(io, "0");
519 else
520 fprintf(io, "%s", rnames[p->n_rval]);
521 break;
522
523 default:
524 comperr("illegal address, op %d, node %p", p->n_op, p);
525 break;
526
527 }
528 }
529
530 /* printf conditional and unconditional branches */
531 void
532 cbgen(int o, int lab)
533 {
534 comperr("cbgen");
535 }
536
537 void
538 myreader(struct interpass *ipole)
539 {
540 if (x2debug)
541 printip(ipole);
542 }
543
544 void
545 mycanon(NODE *p)
546 {
547 int size = 0;
548
549 p->n_qual = 0;
550 if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
551 return;
552 for (p = p->n_right; p->n_op == CM; p = p->n_left) {
553 if (p->n_right->n_op != ASSIGN)
554 size += szty(p->n_right->n_type);
555 }
556 if (p->n_op != ASSIGN)
557 size += szty(p->n_type);
558
559 if (maxargsz < size)
560 maxargsz = size;
561
562 }
563
564 void
565 myoptim(struct interpass *ip)
566 {
567 }
568
569 void
570 rmove(int s, int d, TWORD t)
571 {
572 printf(" mov %s,%s\n", rnames[s], rnames[d]);
573 if (t > UNSIGNED && !ISPTR(t))
574 comperr("rmove");
575 }
576
577 /*
578 * For class c, find worst-case displacement of the number of
579 * registers in the array r[] indexed by class.
580 * Return true if we always can find a color.
581 */
582 int
583 COLORMAP(int c, int *r)
584 {
585 int num = 0;
586
587 switch (c) {
588 case CLASSA:
589 /* must have at least one reg free to guarantee */
590 if (r[CLASSB]*2+r[CLASSA] < 3)
591 num = 1;
592 break;
593 case CLASSB:
594 if (r[CLASSB] || r[CLASSA])
595 num = 0;
596 break;
597 case CLASSC:
598 num = r[CLASSC] < CREGCNT;
599 break;
600 }
601 return num;
602 }
603
604 char *rnames[] = {
605 "0", "1", "2", "3", "lc0", "fp0", "cfp", "csp",
606 };
607
608 /*
609 * Return a class suitable for a specific type.
610 */
611 int
612 gclass(TWORD t)
613 {
614 return (t==LONG||t==ULONG) ? CLASSB : CLASSA;
615 }
616
617 /*
618 * Calculate argument sizes.
619 */
620 void
621 lastcall(NODE *p)
622 {
623 NODE *op = p;
624 int size = 0;
625
626 p->n_qual = 0;
627 if (p->n_op != CALL && p->n_op != FORTCALL && p->n_op != STCALL)
628 return;
629 for (p = p->n_right; p->n_op == CM; p = p->n_left) {
630 if (p->n_right->n_op != ASSIGN)
631 size += szty(p->n_right->n_type);
632 }
633 if (p->n_op != ASSIGN)
634 size += szty(p->n_type);
635
636 op->n_qual = size; /* XXX */
637 if (maxargsz < size)
638 maxargsz = size;
639 }
640
641 /*
642 * Special shapes.
643 */
644 int
645 special(NODE *p, int shape)
646 {
647 switch (shape) {
648 case SLDFPSP:
649 return regno(p) == FPREG || regno(p) == STKREG;
650 }
651 return SRNOPE;
652 }
653
654 /*
655 * Target-dependent command-line options.
656 */
657 void
658 mflags(char *str)
659 {
660 }
661 /*
662 * Do something target-dependent for xasm arguments.
663 * Supposed to find target-specific constraints and rewrite them.
664 */
665 int
666 myxasm(struct interpass *ip, NODE *p)
667 {
668 return 0;
669 }