Debian Codegraph @

mip/reader.c

raw symbols SCIP
1 /* $Id: reader.c,v 1.307 2021/10/09 12:46:09 ragge Exp $ */
2 /*
3 * Copyright (c) 2003 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 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*
28 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
29 *
30 * Redistribution and use in source and binary forms, with or without
31 * modification, are permitted provided that the following conditions
32 * are met:
33 *
34 * Redistributions of source code and documentation must retain the above
35 * copyright notice, this list of conditions and the following disclaimer.
36 * Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditionsand the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
41 * This product includes software developed or owned by Caldera
42 * International, Inc.
43 * Neither the name of Caldera International, Inc. nor the names of other
44 * contributors may be used to endorse or promote products derived from
45 * this software without specific prior written permission.
46 *
47 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
48 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
50 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
51 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
52 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
56 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
57 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
58 * POSSIBILITY OF SUCH DAMAGE.
59 */
60
61 /*
62 * Everything is entered via pass2_compile(). No functions are
63 * allowed to recurse back into pass2_compile().
64 */
65
66 # include "pass2.h"
67
68 #include <string.h>
69 #include <stdarg.h>
70 #include <stdlib.h>
71
72 /* some storage declarations */
73 int nrecur;
74 int thisline;
75 int fregs;
76 int p2autooff, p2maxautooff;
77
78 NODE *nodepole;
79 struct interpass prepole;
80
81 void saveip(struct interpass *ip);
82 void deltemp(NODE *p, void *);
83 static void cvtemps(struct interpass *ipole, int op, int off);
84 static void fixxasm(struct p2env *);
85
86 static void gencode(NODE *p, int cookie);
87 static void genxasm(NODE *p);
88 static void afree(void);
89
90 struct p2env p2env;
91
92 int crslab2 = 11;
93 /*
94 * Return a number for internal labels.
95 */
96 int
97 getlab2(void)
98 {
99 crslab2++;
100 if (crslab2 < p2env.ipp->ip_lblnum)
101 comperr("getlab2 %d outside boundaries %d-%d",
102 crslab2, p2env.ipp->ip_lblnum, p2env.epp->ip_lblnum);
103 if (crslab2 >= p2env.epp->ip_lblnum)
104 p2env.epp->ip_lblnum = crslab2+1;
105 return crslab2++;
106 }
107
108
109 #ifdef PCC_DEBUG
110 static int *lbldef, *lbluse;
111 static void
112 cktree(NODE *p, void *arg)
113 {
114 int i;
115
116 if (p->n_op > MAXOP)
117 cerror("%p) op %d slipped through", p, p->n_op);
118 #ifndef FIELDOPS
119 if (p->n_op == FLD)
120 cerror("%p) FLD slipped through", p);
121 #endif
122 if (BTYPE(p->n_type) > MAXTYPES)
123 cerror("%p) type %x slipped through", p, p->n_type);
124 if (p->n_op == CBRANCH) {
125 if (!logop(p->n_left->n_op))
126 cerror("%p) not logop branch", p);
127 i = (int)getlval(p->n_right);
128 if (i < p2env.ipp->ip_lblnum || i >= p2env.epp->ip_lblnum)
129 cerror("%p) label %d outside boundaries %d-%d",
130 p, i, p2env.ipp->ip_lblnum, p2env.epp->ip_lblnum);
131 lbluse[i-p2env.ipp->ip_lblnum] = 1;
132 }
133 if ((dope[p->n_op] & ASGOPFLG) && p->n_op != RETURN)
134 cerror("%p) asgop %d slipped through", p, p->n_op);
135 if (p->n_op == TEMP &&
136 (regno(p) < p2env.ipp->ip_tmpnum || regno(p) >= p2env.epp->ip_tmpnum))
137 cerror("%p) temporary %d outside boundaries %d-%d",
138 p, regno(p), p2env.ipp->ip_tmpnum, p2env.epp->ip_tmpnum);
139 if (p->n_op == GOTO && p->n_left->n_op == ICON) {
140 i = (int)getlval(p->n_left);
141 if (i < p2env.ipp->ip_lblnum || i >= p2env.epp->ip_lblnum)
142 cerror("%p) label %d outside boundaries %d-%d",
143 p, i, p2env.ipp->ip_lblnum, p2env.epp->ip_lblnum);
144 lbluse[i-p2env.ipp->ip_lblnum] = 1;
145 }
146 }
147
148 /*
149 * Check that the trees are in a suitable state for pass2.
150 */
151 static void
152 sanitychecks(struct p2env *p2e)
153 {
154 struct interpass *ip;
155 int i, sz;
156
157 sz = (int)sizeof(int) * (p2e->epp->ip_lblnum - p2e->ipp->ip_lblnum);
158 lbldef = xcalloc(sz, 1);
159 lbluse = xcalloc(sz, 1);
160
161 DLIST_FOREACH(ip, &p2env.ipole, qelem) {
162 if (ip->type == IP_DEFLAB) {
163 i = ip->ip_lbl;
164 if (i < p2e->ipp->ip_lblnum || i >= p2e->epp->ip_lblnum)
165 cerror("label %d outside boundaries %d-%d",
166 i, p2e->ipp->ip_lblnum, p2e->epp->ip_lblnum);
167 lbldef[i-p2e->ipp->ip_lblnum] = 1;
168 }
169 if (ip->type == IP_NODE)
170 walkf(ip->ip_node, cktree, 0);
171 }
172 for (i = 0; i < (p2e->epp->ip_lblnum - p2e->ipp->ip_lblnum); i++)
173 if (lbluse[i] != 0 && lbldef[i] == 0)
174 cerror("internal label %d not defined",
175 i + p2e->ipp->ip_lblnum);
176
177 free(lbldef);
178 free(lbluse);
179 }
180 #endif
181
182 /*
183 * Look if a temporary comes from a on-stack argument, in that case
184 * use the already existing stack position instead of moving it to
185 * a new place, and remove the move-to-temp statement.
186 */
187 static int
188 stkarg(int tnr, int (*soff)[2])
189 {
190 struct p2env *p2e = &p2env;
191 struct interpass *ip;
192 NODE *p;
193
194 ip = DLIST_NEXT((struct interpass *)p2e->ipp, qelem);
195 while (ip->type != IP_DEFLAB) /* search for first DEFLAB */
196 ip = DLIST_NEXT(ip, qelem);
197
198 ip = DLIST_NEXT(ip, qelem); /* first NODE */
199
200 for (; ip->type != IP_DEFLAB; ip = DLIST_NEXT(ip, qelem)) {
201 if (ip->type != IP_NODE)
202 continue;
203
204 p = ip->ip_node;
205 if (p->n_op == XASM)
206 continue; /* XXX - hack for x86 PIC */
207 #ifdef notdef
208 if (p->n_op != ASSIGN || p->n_left->n_op != TEMP)
209 comperr("temparg");
210 #endif
211 if (p->n_op != ASSIGN || p->n_left->n_op != TEMP)
212 continue; /* unknown tree */
213
214 if (p->n_right->n_op != OREG && p->n_right->n_op != UMUL)
215 continue; /* arg in register */
216 if (tnr != regno(p->n_left))
217 continue; /* wrong assign */
218 p = p->n_right;
219 if (p->n_op == UMUL &&
220 p->n_left->n_op == PLUS &&
221 p->n_left->n_left->n_op == REG &&
222 p->n_left->n_right->n_op == ICON) {
223 soff[0][0] = regno(p->n_left->n_left);
224 soff[0][1] = (int)getlval(p->n_left->n_right);
225 } else if (p->n_op == OREG) {
226 soff[0][0] = regno(p);
227 soff[0][1] = (int)getlval(p);
228 } else
229 comperr("stkarg: bad arg");
230 tfree(ip->ip_node);
231 DLIST_REMOVE(ip, qelem);
232 return 1;
233 }
234 return 0;
235 }
236
237 /*
238 * See if an ADDROF is somewhere inside the expression tree.
239 * If so, fill in the offset table.
240 */
241 static void
242 findaof(NODE *p, void *arg)
243 {
244 int (*aof)[2] = arg;
245 int tnr;
246
247 if (p->n_op != ADDROF || p->n_left->n_op != TEMP)
248 return;
249 tnr = regno(p->n_left);
250 if (aof[tnr][0])
251 return; /* already gotten stack address */
252 if (stkarg(tnr, &aof[tnr]))
253 return; /* argument was on stack */
254 aof[tnr][0] = FPREG;
255 aof[tnr][1] = freetemp(szty(p->n_left->n_type));
256 }
257
258 /*
259 * Check if a node has side effects.
260 */
261 static int
262 isuseless(NODE *n)
263 {
264 switch (n->n_op) {
265 case XASM:
266 case FUNARG:
267 case UCALL:
268 case UFORTCALL:
269 case FORCE:
270 case ASSIGN:
271 case CALL:
272 case FORTCALL:
273 case CBRANCH:
274 case RETURN:
275 case GOTO:
276 case STCALL:
277 case USTCALL:
278 case STASG:
279 case STARG:
280 return 0;
281 default:
282 return 1;
283 }
284 }
285
286 /*
287 * Delete statements with no meaning (like a+b; or 513.4;)
288 */
289 NODE *
290 deluseless(NODE *p)
291 {
292 struct interpass *ip;
293 NODE *l, *r;
294
295 if (optype(p->n_op) == LTYPE) {
296 nfree(p);
297 return NULL;
298 }
299 if (isuseless(p) == 0)
300 return p;
301
302 if (optype(p->n_op) == UTYPE) {
303 l = p->n_left;
304 nfree(p);
305 return deluseless(l);
306 }
307
308 /* Be sure that both leaves may be valid */
309 l = deluseless(p->n_left);
310 r = deluseless(p->n_right);
311 nfree(p);
312 if (l && r) {
313 ip = ipnode(l);
314 DLIST_INSERT_AFTER(&prepole, ip, qelem);
315 return r;
316 } else if (l)
317 return l;
318 else if (r)
319 return r;
320 return NULL;
321 }
322
323 #ifdef PASS2
324
325 #define SKIPWS(p) while (*p == ' ') p++
326 #define SZIBUF 256
327 static int inpline;
328 static char inpbuf[SZIBUF];
329 static char *
330 rdline(void)
331 {
332 int l;
333
334 if (fgets(inpbuf, sizeof(inpbuf), stdin) == NULL)
335 return NULL;
336 inpline++;
337 l = strlen(inpbuf);
338 if (inpbuf[0] < 33 || inpbuf[1] != ' ' || inpbuf[l-1] != '\n')
339 comperr("sync error in-line %d string '%s'", inpline, inpbuf);
340 inpbuf[l-1] = 0;
341 return inpbuf;
342 }
343
344 /*
345 * Read an int and traverse over it. count up s.
346 */
347 static int
348 rdint(char **s)
349 {
350 char *p = *s;
351 int rv;
352
353 SKIPWS(p);
354 rv = atoi(p);
355 if (*p == '-' || *p == '+') p++;
356 while (*p >= '0' && *p <= '9') p++;
357 *s = p;
358 return rv;
359 }
360
361 static struct attr *
362 rdattr(char **p)
363 {
364 struct attr *ap, *app = NULL;
365 int i, a, sz;
366
367 (*p)++; /* skip + */
368 onemore:
369 a = rdint(p);
370 sz = rdint(p);
371 ap = attr_new(a, sz);
372 for (i = 0; i < sz; i++)
373 ap->iarg(i) = rdint(p);
374 ap->next = app;
375 app = ap;
376 SKIPWS((*p));
377 if (**p != 0)
378 goto onemore;
379
380 return app;
381 }
382
383 /*
384 * Read in an indentifier and save on tmp heap. Return saved string.
385 */
386 static char *
387 rdstr(char **p)
388 {
389 char *t, *s = *p;
390 int sz;
391
392 SKIPWS(s);
393 *p = s;
394 for (sz = 0; *s && *s != ' '; s++, sz++)
395 ;
396 t = tmpalloc(sz+1);
397 memcpy(t, *p, sz);
398 t[sz] = 0;
399 *p = s;
400 return t;
401 }
402
403 /*
404 * Read and check node structs from pass1.
405 */
406 static NODE *
407 rdnode(char *s)
408 {
409 NODE *p = talloc();
410 int ty;
411
412 if (s[0] != '"')
413 comperr("rdnode sync error");
414 s++; s++;
415 p->n_regw = NULL;
416 p->n_ap = NULL;
417 p->n_su = p->n_rval = 0;
418 setlval(p, 0);
419 p->n_op = rdint(&s);
420 p->n_type = rdint(&s);
421 p->n_qual = rdint(&s);
422 p->n_name = "";
423 SKIPWS(s);
424 ty = optype(p->n_op);
425 if (p->n_op == XASM) {
426 int i = strlen(s);
427 p->n_name = tmpalloc(i+1);
428 memcpy(p->n_name, s, i);
429 p->n_name[i] = 0;
430 s += i;
431 }
432 if (ty == UTYPE) {
433 p->n_rval = rdint(&s);
434 } else if (ty == LTYPE) {
435 setlval(p, strtoll(s, &s, 10));
436 if (p->n_op == NAME || p->n_op == ICON) {
437 SKIPWS(s);
438 if (*s && *s != '+')
439 p->n_name = rdstr(&s);
440 } else
441 p->n_rval = rdint(&s);
442 }
443 SKIPWS(s);
444 if (p->n_op == XARG) {
445 int i = strlen(s);
446 p->n_name = tmpalloc(i+1);
447 memcpy(p->n_name, s, i);
448 p->n_name[i] = 0;
449 s += i;
450 }
451 if (*s == '+')
452 p->n_ap = rdattr(&s);
453 SKIPWS(s);
454 if (*s)
455 comperr("in-line %d failed read, buf '%s'", inpline, inpbuf);
456 if (ty != LTYPE)
457 p->n_left = rdnode(rdline());
458 if (ty == BITYPE)
459 p->n_right = rdnode(rdline());
460 return p;
461 }
462
463 /*
464 * Read everything from pass1.
465 */
466 void
467 mainp2()
468 {
469 static int foo[] = { 0 };
470 struct interpass_prolog *ipp;
471 struct interpass *ip;
472 char nam[SZIBUF], *p, *b;
473 extern char *ftitle;
474
475 while ((p = rdline()) != NULL) {
476 b = p++;
477 p++;
478
479 switch (*b) {
480 case '*': /* pass thru line */
481 printf("%s\n", p);
482 break;
483 case '&': /* current filename */
484 free(ftitle);
485 ftitle = xstrdup(p);
486 break;
487 case '#':
488 lineno = atoi(p);
489 break;
490 case '"':
491 ip = malloc(sizeof(struct interpass));
492 ip->type = IP_NODE;
493 ip->ip_node = rdnode(b);
494 pass2_compile(ip);
495 break;
496 case '^':
497 ip = malloc(sizeof(struct interpass));
498 ip->type = IP_DEFLAB;
499 ip->ip_lbl = atoi(p);
500 pass2_compile(ip);
501 break;
502 case '!': /* prolog */
503 ipp = malloc(sizeof(struct interpass_prolog));
504 ip = (void *)ipp;
505 ip->type = IP_PROLOG;
506 sscanf(p, "%d %d %d %d %d %s", &ipp->ipp_type,
507 &ipp->ipp_vis, &ip->ip_lbl, &ipp->ip_tmpnum,
508 &ipp->ip_lblnum, nam);
509 ipp->ipp_name = xstrdup(nam);
510 ipp->ipp_autos = -1;
511 ipp->ip_labels = foo;
512 #ifdef TARGET_IPP_MEMBERS
513 if (*(p = rdline()) != '(')
514 comperr("target member error");
515 p += 2;
516 target_members_read_prolog(ipp);
517 SKIPWS(p);
518 if (*p)
519 comperr("bad prolog '%s' '%s'", p, inpbuf);
520 #endif
521 pass2_compile((struct interpass *)ipp);
522 break;
523
524 case '%': /* epilog */
525 ipp = malloc(sizeof(struct interpass_prolog));
526 ip = (void *)ipp;
527 ip->type = IP_EPILOG;
528 ipp->ipp_autos = rdint(&p);
529 ip->ip_lbl = rdint(&p);
530 ipp->ip_tmpnum = rdint(&p);
531 ipp->ip_lblnum = rdint(&p);
532 ipp->ipp_name = rdstr(&p);
533 SKIPWS(p);
534 if (*p == '+') {
535 int num, i;
536 p++;
537 num = rdint(&p) + 1;
538 ipp->ip_labels = tmpalloc(sizeof(int)*num);
539 for (i = 0; i < num; i++)
540 ipp->ip_labels[i] = rdint(&p);
541 ipp->ip_labels[num] = 0;
542 } else
543 ipp->ip_labels = foo;
544 SKIPWS(p);
545 if (*p)
546 comperr("bad epilog '%s' '%s'", p, inpbuf);
547 #ifdef TARGET_IPP_MEMBERS
548 if (*(p = rdline()) != ')')
549 comperr("target epi member error");
550 p += 2;
551 target_members_read_epilog(ipp);
552 SKIPWS(p);
553 if (*p)
554 comperr("bad epilog2 '%s' '%s'", p, inpbuf);
555 #endif
556 pass2_compile((struct interpass *)ipp);
557 break;
558 case '$': /* assembler */
559 if (*p) {
560 int sz = strlen(p);
561 ip = malloc(sizeof(struct interpass));
562 ip->type = IP_ASM;
563 ip->ip_asm = tmpalloc(sz+1);
564 memcpy(ip->ip_asm, p, sz);
565 ip->ip_asm[sz] = 0;
566 pass2_compile(ip);
567 }
568 break;
569 default:
570 comperr("bad string %s", b);
571 }
572 }
573 }
574
575
576 #endif
577
578 /*
579 * Receives interpass structs from pass1.
580 */
581 void
582 pass2_compile(struct interpass *ip)
583 {
584 void deljumps(struct p2env *);
585 struct p2env *p2e = &p2env;
586 int (*addrp)[2];
587
588 if (ip->type == IP_PROLOG) {
589 memset(p2e, 0, sizeof(struct p2env));
590 p2e->ipp = (struct interpass_prolog *)ip;
591 if (crslab2 < p2e->ipp->ip_lblnum)
592 crslab2 = p2e->ipp->ip_lblnum;
593 DLIST_INIT(&p2e->ipole, qelem);
594 }
595 DLIST_INSERT_BEFORE(&p2e->ipole, ip, qelem);
596 if (ip->type != IP_EPILOG)
597 return;
598
599 afree();
600 p2e->epp = (struct interpass_prolog *)DLIST_PREV(&p2e->ipole, qelem);
601 p2maxautooff = p2autooff = p2e->epp->ipp_autos;
602
603 #ifdef PCC_DEBUG
604 if (e2debug) {
605 printf("Entering pass2\n");
606 printip(&p2e->ipole);
607 }
608 #endif
609
610 #ifdef PCC_DEBUG
611 sanitychecks(p2e);
612 #endif
613 myreader(&p2e->ipole); /* local massage of input */
614
615 /*
616 * Do initial modification of the trees. Two loops;
617 * - first, search for ADDROF of TEMPs, these must be
618 * converterd to OREGs on stack.
619 * - second, do the actual conversions, in case of not xtemps
620 * convert all temporaries to stack references.
621 */
622
623 if (p2e->epp->ip_tmpnum != p2e->ipp->ip_tmpnum) {
624 addrp = xcalloc(sizeof(*addrp),
625 (p2e->epp->ip_tmpnum - p2e->ipp->ip_tmpnum));
626 addrp -= p2e->ipp->ip_tmpnum;
627 } else
628 addrp = NULL;
629 if (xtemps) {
630 DLIST_FOREACH(ip, &p2e->ipole, qelem) {
631 if (ip->type == IP_NODE)
632 walkf(ip->ip_node, findaof, addrp);
633 }
634 }
635 DLIST_FOREACH(ip, &p2e->ipole, qelem)
636 if (ip->type == IP_NODE)
637 walkf(ip->ip_node, deltemp, addrp);
638 if (addrp)
639 free(addrp + p2e->ipp->ip_tmpnum);
640
641 #ifdef PCC_DEBUG
642 if (e2debug) {
643 printf("Efter ADDROF/TEMP\n");
644 printip(&p2e->ipole);
645 }
646 #endif
647
648 DLIST_INIT(&prepole, qelem);
649 DLIST_FOREACH(ip, &p2e->ipole, qelem) {
650 if (ip->type != IP_NODE)
651 continue;
652 canon(ip->ip_node);
653 if ((ip->ip_node = deluseless(ip->ip_node)) == NULL) {
654 DLIST_REMOVE(ip, qelem);
655 } else while (!DLIST_ISEMPTY(&prepole, qelem)) {
656 struct interpass *tipp;
657
658 tipp = DLIST_NEXT(&prepole, qelem);
659 DLIST_REMOVE(tipp, qelem);
660 DLIST_INSERT_BEFORE(ip, tipp, qelem);
661 }
662 }
663
664 fixxasm(p2e); /* setup for extended asm */
665
666 optimize(p2e);
667 ngenregs(p2e);
668
669 if (xtemps && xdeljumps)
670 deljumps(p2e);
671
672 DLIST_FOREACH(ip, &p2e->ipole, qelem)
673 emit(ip);
674 }
675
676 void
677 emit(struct interpass *ip)
678 {
679 NODE *p, *r;
680 struct optab *op;
681 int o;
682
683 switch (ip->type) {
684 case IP_NODE:
685 p = ip->ip_node;
686
687 nodepole = p;
688 canon(p); /* may convert stuff after genregs */
689 #ifdef PCC_DEBUG
690 if (c2debug > 1) {
691 printf("emit IP_NODE:\n");
692 fwalk(p, e2print, 0);
693 }
694 #endif
695 switch (p->n_op) {
696 case CBRANCH:
697 /* Only emit branch insn if RESCC */
698 /* careful when an OPLOG has been elided */
699 if (p->n_left->n_su == 0 && p->n_left->n_left != NULL) {
700 op = &table[TBLIDX(p->n_left->n_left->n_su)];
701 r = p->n_left;
702 } else {
703 op = &table[TBLIDX(p->n_left->n_su)];
704 r = p;
705 }
706 if (op->rewrite & RESCC) {
707 o = p->n_left->n_op;
708 gencode(r, FORCC);
709 cbgen(o, getlval(p->n_right));
710 } else {
711 gencode(r, FORCC);
712 }
713 break;
714 case FORCE:
715 gencode(p->n_left, INREGS);
716 break;
717 case XASM:
718 genxasm(p);
719 break;
720 default:
721 if (p->n_op != REG || p->n_type != VOID) /* XXX */
722 gencode(p, FOREFF); /* Emit instructions */
723 }
724
725 tfree(p);
726 break;
727 case IP_PROLOG:
728 prologue((struct interpass_prolog *)ip);
729 break;
730 case IP_EPILOG:
731 eoftn((struct interpass_prolog *)ip);
732 p2maxautooff = p2autooff = AUTOINIT/SZCHAR;
733 break;
734 case IP_DEFLAB:
735 deflab(ip->ip_lbl);
736 break;
737 case IP_ASM:
738 printf("%s\n", ip->ip_asm);
739 break;
740 default:
741 cerror("emit %d", ip->type);
742 }
743 }
744
745 #ifdef PCC_DEBUG
746 char *cnames[] = {
747 "SANY",
748 "SAREG",
749 "SBREG",
750 "SCREG",
751 "SDREG",
752 "SCC",
753 "SNAME",
754 "SCON",
755 "SFLD",
756 "SOREG",
757 "STARNM",
758 "STARREG",
759 "INTEMP",
760 "FORARG",
761 "SWADD",
762 0,
763 };
764
765 /*
766 * print a nice-looking description of cookie
767 */
768 char *
769 prcook(int cookie)
770 {
771 static char buf[50];
772 int i, flag;
773
774 if (cookie & SPECIAL) {
775 switch (cookie) {
776 case SZERO:
777 return "SZERO";
778 case SONE:
779 return "SONE";
780 case SMONE:
781 return "SMONE";
782 default:
783 snprintf(buf, sizeof(buf), "SPECIAL+%d", cookie & ~SPECIAL);
784 return buf;
785 }
786 }
787
788 flag = 0;
789 buf[0] = 0;
790 for (i = 0; cnames[i]; ++i) {
791 if (cookie & (1<<i)) {
792 if (flag)
793 strlcat(buf, "|", sizeof(buf));
794 ++flag;
795 strlcat(buf, cnames[i], sizeof(buf));
796 }
797 }
798 return buf;
799 }
800 #endif
801
802 int
803 geninsn(NODE *p, int cookie)
804 {
805 NODE *p1, *p2;
806 int q, o, rv = 0;
807
808 #ifdef PCC_DEBUG
809 if (o2debug) {
810 printf("geninsn(%p, %s)\n", p, prcook(cookie));
811 fwalk(p, e2print, 0);
812 }
813 #endif
814
815 q = cookie & QUIET;
816 cookie &= ~QUIET; /* XXX - should not be necessary */
817
818 again: switch (o = p->n_op) {
819 case EQ:
820 case NE:
821 case LE:
822 case LT:
823 case GE:
824 case GT:
825 case ULE:
826 case ULT:
827 case UGE:
828 case UGT:
829 p1 = p->n_left;
830 p2 = p->n_right;
831 if (p2->n_op == ICON && getlval(p2) == 0 && *p2->n_name == 0 &&
832 (dope[p1->n_op] & (FLOFLG|DIVFLG|SIMPFLG|SHFFLG))) {
833 #ifdef mach_pdp11 /* XXX all targets? */
834 if ((rv = geninsn(p1, FORCC|QUIET)) != FFAIL)
835 break;
836 #else
837 if (findops(p1, FORCC) > 0)
838 break;
839 #endif
840 }
841 rv = relops(p);
842 break;
843
844 case PLUS:
845 case MINUS:
846 case MUL:
847 case DIV:
848 case MOD:
849 case AND:
850 case OR:
851 case ER:
852 case LS:
853 case RS:
854 rv = findops(p, cookie);
855 break;
856
857 case ASSIGN:
858 #ifdef FINDMOPS
859 if ((rv = findmops(p, cookie)) != FFAIL)
860 break;
861 /* FALLTHROUGH */
862 #endif
863 case STASG:
864 rv = findasg(p, cookie);
865 break;
866
867 case UMUL: /* May turn into an OREG */
868 rv = findumul(p, cookie);
869 break;
870
871 case REG:
872 case TEMP:
873 case NAME:
874 case ICON:
875 case FCON:
876 case OREG:
877 rv = findleaf(p, cookie);
878 break;
879
880 case STCALL:
881 case CALL:
882 /* CALL arguments are handled special */
883 for (p1 = p->n_right; p1->n_op == CM; p1 = p1->n_left)
884 (void)geninsn(p1->n_right, FOREFF);
885 (void)geninsn(p1, FOREFF);
886 /* FALLTHROUGH */
887 case FLD:
888 case COMPL:
889 case UMINUS:
890 case PCONV:
891 case SCONV:
892 /* case INIT: */
893 case GOTO:
894 case FUNARG:
895 case STARG:
896 case UCALL:
897 case USTCALL:
898 case ADDROF:
899 rv = finduni(p, cookie);
900 break;
901
902 case CBRANCH:
903 p1 = p->n_left;
904 p2 = p->n_right;
905 p1->n_label = (int)getlval(p2);
906 (void)geninsn(p1, FORCC);
907 p->n_su = 0;
908 break;
909
910 case FORCE: /* XXX needed? */
911 (void)geninsn(p->n_left, INREGS);
912 p->n_su = 0; /* su calculations traverse left */
913 break;
914
915 case XASM:
916 for (p1 = p->n_left; p1->n_op == CM; p1 = p1->n_left)
917 (void)geninsn(p1->n_right, FOREFF);
918 (void)geninsn(p1, FOREFF);
919 break; /* all stuff already done? */
920
921 case XARG:
922 /* generate code for correct class here */
923 break;
924
925 default:
926 comperr("geninsn: bad op %s, node %p", opst[o], p);
927 }
928 if (rv == FFAIL && !q)
929 comperr("Cannot generate code, node %p op %s", p,opst[p->n_op]);
930 if (rv == FRETRY)
931 goto again;
932 #ifdef PCC_DEBUG
933 if (o2debug) {
934 printf("geninsn(%p, %s) rv %d\n", p, prcook(cookie), rv);
935 fwalk(p, e2print, 0);
936 }
937 #endif
938 return rv;
939 }
940
941 #ifdef PCC_DEBUG
942 #define CDEBUG(x) if (c2debug) printf x
943 #else
944 #define CDEBUG(x)
945 #endif
946
947 /*
948 * Do a register-register move if necessary.
949 * Called if a RLEFT or RRIGHT is found.
950 */
951 static void
952 ckmove(NODE *p, NODE *q)
953 {
954 struct optab *t = &table[TBLIDX(p->n_su)];
955 int reg;
956
957 if (q->n_op != REG || p->n_reg == -1)
958 return; /* no register */
959
960 /* do we have a need for special reg? */
961 #ifdef NEWNEED
962 {
963 char *w;
964
965 if ((w = hasneed(t->needs, p->n_left == q ? cNL : cNR)) != NULL)
966 reg = w[1];
967 else
968 reg = DECRA(p->n_reg, 0);
969 }
970 #else
971 if ((t->needs & NSPECIAL) &&
972 (reg = rspecial(t, p->n_left == q ? NLEFT : NRIGHT)) >= 0)
973 ;
974 else
975 reg = DECRA(p->n_reg, 0);
976 #endif
977
978 if (reg < 0 || reg == DECRA(q->n_reg, 0))
979 return; /* no move necessary */
980
981 CDEBUG(("rmove: node %p, %s -> %s\n", p, rnames[DECRA(q->n_reg, 0)],
982 rnames[reg]));
983 rmove(DECRA(q->n_reg, 0), reg, p->n_type);
984 q->n_reg = q->n_rval = reg;
985 }
986
987 /*
988 * Rewrite node to register after instruction emit.
989 */
990 static void
991 rewrite(NODE *p, int dorewrite, int cookie)
992 {
993 NODE *l, *r;
994 int o;
995
996 l = getlr(p, 'L');
997 r = getlr(p, 'R');
998 o = p->n_op;
999 p->n_op = REG;
1000 setlval(p, 0);
1001 p->n_name = "";
1002
1003 if (o == ASSIGN || o == STASG) {
1004 /* special rewrite care */
1005 int reg = DECRA(p->n_reg, 0);
1006 #define TL(x) (TBLIDX(x->n_su) || x->n_op == REG)
1007 if (p->n_reg == -1)
1008 ;
1009 else if (TL(l) && (DECRA(l->n_reg, 0) == reg))
1010 ;
1011 else if (TL(r) && (DECRA(r->n_reg, 0) == reg))
1012 ;
1013 else if (TL(l))
1014 rmove(DECRA(l->n_reg, 0), reg, p->n_type);
1015 else if (TL(r))
1016 rmove(DECRA(r->n_reg, 0), reg, p->n_type);
1017 #if 0
1018 else
1019 comperr("rewrite");
1020 #endif
1021 #undef TL
1022 }
1023 if (optype(o) != LTYPE)
1024 tfree(l);
1025 if (optype(o) == BITYPE)
1026 tfree(r);
1027 if (dorewrite == 0)
1028 return;
1029 CDEBUG(("rewrite: %p, reg %s\n", p,
1030 p->n_reg == -1? "<none>" : rnames[DECRA(p->n_reg, 0)]));
1031 p->n_rval = DECRA(p->n_reg, 0);
1032 }
1033
1034 #ifndef XASM_TARGARG
1035 #define XASM_TARGARG(x,y) 0
1036 #endif
1037
1038 /*
1039 * printout extended assembler.
1040 */
1041 void
1042 genxasm(NODE *p)
1043 {
1044 NODE *q, **nary;
1045 int n = 1, o = 0, v = 0;
1046 char *w;
1047
1048 if (p->n_left->n_op != ICON || p->n_left->n_type != STRTY) {
1049 for (q = p->n_left; q->n_op == CM; q = q->n_left)
1050 n++;
1051 nary = tmpcalloc(sizeof(NODE *)*(n+1));
1052 o = n;
1053 for (q = p->n_left; q->n_op == CM; q = q->n_left) {
1054 gencode(q->n_right->n_left, INREGS);
1055 nary[--o] = q->n_right;
1056 }
1057 gencode(q->n_left, INREGS);
1058 nary[--o] = q;
1059 } else
1060 nary = 0;
1061
1062 w = p->n_name;
1063 putchar('\t');
1064 while (*w != 0) {
1065 if (*w == '%') {
1066 if (w[1] == '%')
1067 putchar('%');
1068 else if (XASM_TARGARG(w, nary))
1069 ; /* handled by target */
1070 else if (w[1] == '=') {
1071 if (v == 0) v = getlab2();
1072 printf("%d", v);
1073 } else if (w[1] == 'c') {
1074 q = nary[(int)w[2]-'0'];
1075 if (q->n_left->n_op != ICON)
1076 uerror("impossible constraint");
1077 printf(CONFMT, getlval(q->n_left));
1078 w++;
1079 } else if (w[1] < '0' || w[1] > (n + '0'))
1080 uerror("bad xasm arg number %c", w[1]);
1081 else {
1082 if (w[1] == (n + '0'))
1083 q = nary[(int)w[1]-'0' - 1]; /* XXX */
1084 else
1085 q = nary[(int)w[1]-'0'];
1086 adrput(stdout, q->n_left);
1087 }
1088 w++;
1089 } else if (*w == '\\') { /* Always 3-digit octal */
1090 int num = *++w - '0';
1091 num = (num << 3) + *++w - '0';
1092 num = (num << 3) + *++w - '0';
1093 putchar(num);
1094 } else
1095 putchar(*w);
1096 w++;
1097 }
1098 putchar('\n');
1099 }
1100
1101 /*
1102 * Allocate temporary registers for use while emitting this table entry.
1103 */
1104 static void
1105 allo(NODE *p, struct optab *q)
1106 {
1107 extern int stktemp;
1108 int i, n;
1109
1110 n = ncnt(q->needs);
1111
1112 for (i = 0; i < NRESC; i++)
1113 if (resc[i].n_op != FREE)
1114 comperr("allo: used reg");
1115 #ifdef NEWNEED
1116 if (n == 0 && hasneed(q->needs, cNTEMP) == 0)
1117 return;
1118 #else
1119 if (n == 0 && (q->needs & NTMASK) == 0)
1120 return;
1121 #endif
1122 for (i = 0; i < n+1; i++) {
1123 resc[i].n_op = REG;
1124 resc[i].n_type = p->n_type; /* XXX should be correct type */
1125 resc[i].n_rval = DECRA(p->n_reg, i);
1126 resc[i].n_su = p->n_su; /* ??? */
1127 }
1128 if (i > NRESC)
1129 comperr("allo: too many allocs");
1130 #ifdef NEWNEED
1131 if (hasneed(q->needs, cNTEMP)) {
1132 #else
1133 if (q->needs & NTMASK) {
1134 #endif
1135 #ifdef MYALLOTEMP
1136 MYALLOTEMP(resc[i], stktemp);
1137 #else
1138 resc[i].n_op = OREG;
1139 setlval(&resc[i], stktemp);
1140 resc[i].n_rval = FPREG;
1141 resc[i].n_su = p->n_su; /* ??? */
1142 resc[i].n_name = "";
1143 #endif
1144 }
1145 }
1146
1147 static void
1148 afree(void)
1149 {
1150 int i;
1151
1152 for (i = 0; i < NRESC; i++)
1153 resc[i].n_op = FREE;
1154 }
1155
1156 void
1157 gencode(NODE *p, int cookie)
1158 {
1159 struct optab *q = &table[TBLIDX(p->n_su)];
1160 NODE *p1, *l, *r;
1161 int o = optype(p->n_op);
1162 #ifdef FINDMOPS
1163 int ismops = (p->n_op == ASSIGN && (p->n_su & ISMOPS));
1164 #endif
1165
1166 l = p->n_left;
1167 r = p->n_right;
1168
1169 if (TBLIDX(p->n_su) == 0) {
1170 if (o == BITYPE && (p->n_su & DORIGHT))
1171 gencode(r, 0);
1172 if (optype(p->n_op) != LTYPE)
1173 gencode(l, 0);
1174 if (o == BITYPE && !(p->n_su & DORIGHT))
1175 gencode(r, 0);
1176 return;
1177 }
1178
1179 CDEBUG(("gencode: node %p\n", p));
1180
1181 if (p->n_op == REG && DECRA(p->n_reg, 0) == p->n_rval)
1182 return; /* meaningless move to itself */
1183
1184 if (callop(p->n_op))
1185 lastcall(p); /* last chance before function args */
1186 if (p->n_op == CALL || p->n_op == FORTCALL || p->n_op == STCALL) {
1187 /* Print out arguments first */
1188 for (p1 = r; p1->n_op == CM; p1 = p1->n_left)
1189 gencode(p1->n_right, FOREFF);
1190 gencode(p1, FOREFF);
1191 o = UTYPE; /* avoid going down again */
1192 }
1193
1194 if (o == BITYPE && (p->n_su & DORIGHT)) {
1195 gencode(r, INREGS);
1196 if (q->rewrite & RRIGHT)
1197 ckmove(p, r);
1198 }
1199 if (o != LTYPE) {
1200 gencode(l, INREGS);
1201 #ifdef FINDMOPS
1202 if (ismops)
1203 ;
1204 else
1205 #endif
1206 if (q->rewrite & RLEFT)
1207 ckmove(p, l);
1208 }
1209 if (o == BITYPE && !(p->n_su & DORIGHT)) {
1210 gencode(r, INREGS);
1211 if (q->rewrite & RRIGHT)
1212 ckmove(p, r);
1213 }
1214
1215 #ifdef FINDMOPS
1216 if (ismops) {
1217 /* reduce right tree to make expand() work */
1218 if (optype(r->n_op) != LTYPE) {
1219 p->n_op = r->n_op;
1220 r = tcopy(r->n_right);
1221 tfree(p->n_right);
1222 p->n_right = r;
1223 }
1224 }
1225 #endif
1226
1227 canon(p);
1228
1229 #ifdef NEWNEED
1230 if (q->needs) {
1231 char *w;
1232 int rr = ((w = hasneed(q->needs, cNR)) ? w[1] : -1);
1233 int lr = ((w = hasneed(q->needs, cNL)) ? w[1] : -1);
1234 #else
1235 if (q->needs & NSPECIAL) {
1236 int rr = rspecial(q, NRIGHT);
1237 int lr = rspecial(q, NLEFT);
1238 #endif
1239
1240 if (rr >= 0) {
1241 #ifdef PCC_DEBUG
1242 if (optype(p->n_op) != BITYPE)
1243 comperr("gencode: rspecial borked");
1244 #endif
1245 if (r->n_op != REG)
1246 comperr("gencode: rop != REG");
1247 if (rr != r->n_rval)
1248 rmove(r->n_rval, rr, r->n_type);
1249 r->n_rval = r->n_reg = rr;
1250 }
1251 if (lr >= 0) {
1252 if (l->n_op != REG)
1253 comperr("gencode: %p lop != REG", p);
1254 if (lr != l->n_rval)
1255 rmove(l->n_rval, lr, l->n_type);
1256 l->n_rval = l->n_reg = lr;
1257 }
1258 if (rr >= 0 && lr >= 0 && (l->n_reg == rr || r->n_reg == lr))
1259 comperr("gencode: cross-reg-move");
1260 }
1261
1262 if (p->n_op == ASSIGN &&
1263 p->n_left->n_op == REG && p->n_right->n_op == REG &&
1264 p->n_left->n_rval == p->n_right->n_rval &&
1265 (p->n_su & RVCC) == 0) { /* XXX should check if necessary */
1266 /* do not emit anything */
1267 CDEBUG(("gencode(%p) assign nothing\n", p));
1268 rewrite(p, q->rewrite, cookie);
1269 return;
1270 }
1271
1272 CDEBUG(("emitting node %p\n", p));
1273 if (TBLIDX(p->n_su) == 0)
1274 return;
1275
1276 allo(p, q);
1277 expand(p, cookie, q->cstring);
1278
1279 #ifdef FINDMOPS
1280 if (ismops && DECRA(p->n_reg, 0) != regno(l) && cookie != FOREFF) {
1281 CDEBUG(("gencode(%p) rmove\n", p));
1282 rmove(regno(l), DECRA(p->n_reg, 0), p->n_type);
1283 } else
1284 #endif
1285 if (callop(p->n_op) && cookie != FOREFF &&
1286 DECRA(p->n_reg, 0) != RETREG(p->n_type)) {
1287 CDEBUG(("gencode(%p) retreg\n", p));
1288 rmove(RETREG(p->n_type), DECRA(p->n_reg, 0), p->n_type);
1289 #ifdef NEWNEED
1290 } else if (q->needs) {
1291 char *w;
1292 int rr = ((w = hasneed(q->needs, cNRES)) ? w[1] : -1);
1293 #else
1294 } else if (q->needs & NSPECIAL) {
1295 int rr = rspecial(q, NRES);
1296 #endif
1297
1298 if (rr >= 0 && DECRA(p->n_reg, 0) != rr) {
1299 CDEBUG(("gencode(%p) nspec retreg\n", p));
1300 rmove(rr, DECRA(p->n_reg, 0), p->n_type);
1301 }
1302 } else if ((q->rewrite & RESC1) &&
1303 (DECRA(p->n_reg, 1) != DECRA(p->n_reg, 0))) {
1304 CDEBUG(("gencode(%p) RESC1 retreg\n", p));
1305 rmove(DECRA(p->n_reg, 1), DECRA(p->n_reg, 0), p->n_type);
1306 }
1307 #if 0
1308 /* XXX - kolla upp det h{r */
1309 else if (p->n_op == ASSIGN) {
1310 /* may need move added if RLEFT/RRIGHT */
1311 /* XXX should be handled in sucomp() */
1312 if ((q->rewrite & RLEFT) && (p->n_left->n_op == REG) &&
1313 (p->n_left->n_rval != DECRA(p->n_reg, 0)) &&
1314 TCLASS(p->n_su)) {
1315 rmove(p->n_left->n_rval, DECRA(p->n_reg, 0), p->n_type);
1316 } else if ((q->rewrite & RRIGHT) && (p->n_right->n_op == REG) &&
1317 (p->n_right->n_rval != DECRA(p->n_reg, 0)) &&
1318 TCLASS(p->n_su)) {
1319 rmove(p->n_right->n_rval, DECRA(p->n_reg, 0), p->n_type);
1320 }
1321 }
1322 #endif
1323 rewrite(p, q->rewrite, cookie);
1324 afree();
1325 }
1326
1327 int negrel[] = { NE, EQ, GT, GE, LT, LE, UGT, UGE, ULT, ULE } ; /* negatives of relationals */
1328 size_t negrelsize = sizeof negrel / sizeof negrel[0];
1329
1330 #ifdef PCC_DEBUG
1331 #undef PRTABLE
1332 void
1333 e2print(NODE *p, int down, int *a, int *b)
1334 {
1335 struct attr *ap;
1336 #ifdef PRTABLE
1337 extern int tablesize;
1338 #endif
1339
1340 *a = *b = down+1;
1341 while( down >= 2 ){
1342 printf("\t");
1343 down -= 2;
1344 }
1345 if( down-- ) printf(" " );
1346
1347
1348 printf("%p) %s", p, opst[p->n_op] );
1349 switch( p->n_op ) { /* special cases */
1350
1351 case FLD:
1352 printf(" sz=%d, shift=%d",
1353 UPKFSZ(p->n_rval), UPKFOFF(p->n_rval));
1354 break;
1355
1356 case REG:
1357 printf(" %s", rnames[p->n_rval] );
1358 break;
1359
1360 case TEMP:
1361 printf(" %d", regno(p));
1362 break;
1363
1364 case XASM:
1365 case XARG:
1366 printf(" '%s'", p->n_name);
1367 break;
1368
1369 case ICON:
1370 case NAME:
1371 case OREG:
1372 printf(" " );
1373 adrput(stdout, p );
1374 break;
1375
1376 case STCALL:
1377 case USTCALL:
1378 case STARG:
1379 case STASG:
1380 ap = attr_find(p->n_ap, ATTR_P2STRUCT);
1381 printf(" size=%d", ap->iarg(0));
1382 printf(" align=%d", ap->iarg(1));
1383 break;
1384 }
1385
1386 printf(", " );
1387 tprint(p->n_type, p->n_qual);
1388 printf(", " );
1389
1390 prtreg(p);
1391 printf(", SU= %d(%cREG,%s,%s,%s,%s)\n",
1392 TBLIDX(p->n_su),
1393 TCLASS(p->n_su)+'@',
1394 #ifdef PRTABLE
1395 TBLIDX(p->n_su) >= 0 && TBLIDX(p->n_su) <= tablesize ?
1396 table[TBLIDX(p->n_su)].cstring : "",
1397 #else
1398 "",
1399 #endif
1400 p->n_su & RVEFF ? "RVEFF" : "", p->n_su & RVCC ? "RVCC" : "",
1401 p->n_su & DORIGHT ? "DORIGHT" : "");
1402 }
1403 #endif
1404
1405 /*
1406 * change left TEMPs into OREGs
1407 */
1408 void
1409 deltemp(NODE *p, void *arg)
1410 {
1411 int (*aor)[2] = arg;
1412 NODE *l;
1413
1414 if (p->n_op == TEMP) {
1415 if (aor[regno(p)][0] == 0) {
1416 if (xtemps)
1417 return;
1418 aor[regno(p)][0] = FPREG;
1419 aor[regno(p)][1] = freetemp(szty(p->n_type));
1420 }
1421 storemod(p, aor[regno(p)][1], aor[regno(p)][0]);
1422 } else if (p->n_op == ADDROF && p->n_left->n_op == OREG) {
1423 p->n_op = PLUS;
1424 l = p->n_left;
1425 l->n_op = REG;
1426 l->n_type = INCREF(l->n_type);
1427 p->n_right = mklnode(ICON, getlval(l), 0, INT);
1428 } else if (p->n_op == ADDROF && p->n_left->n_op == UMUL) {
1429 l = p->n_left;
1430 *p = *p->n_left->n_left;
1431 nfree(l->n_left);
1432 nfree(l);
1433 }
1434 }
1435
1436 /*
1437 * for pointer/integer arithmetic, set pointer at left node
1438 */
1439 static void
1440 setleft(NODE *p, void *arg)
1441 {
1442 NODE *q;
1443
1444 /* only additions for now */
1445 if (p->n_op != PLUS)
1446 return;
1447 if (ISPTR(p->n_right->n_type) && !ISPTR(p->n_left->n_type)) {
1448 q = p->n_right;
1449 p->n_right = p->n_left;
1450 p->n_left = q;
1451 }
1452 }
1453
1454 /* It is OK to have these as externals */
1455 static int oregr;
1456 static CONSZ oregtemp;
1457 static char *oregcp;
1458 /*
1459 * look for situations where we can turn * into OREG
1460 * If sharp then do not allow temps.
1461 */
1462 int
1463 oregok(NODE *p, int sharp)
1464 {
1465
1466 NODE *q;
1467 NODE *ql, *qr;
1468 int r;
1469 CONSZ temp;
1470 char *cp;
1471
1472 q = p->n_left;
1473 #if 0
1474 if ((q->n_op == REG || (q->n_op == TEMP && !sharp)) &&
1475 q->n_rval == DECRA(q->n_reg, 0)) {
1476 #endif
1477 if (q->n_op == REG || (q->n_op == TEMP && !sharp)) {
1478 temp = getlval(q);
1479 r = q->n_rval;
1480 cp = q->n_name;
1481 goto ormake;
1482 }
1483
1484 if (q->n_op != PLUS && q->n_op != MINUS)
1485 return 0;
1486 ql = q->n_left;
1487 qr = q->n_right;
1488
1489 #ifdef R2REGS
1490
1491 /* look for doubly indexed expressions */
1492 /* XXX - fix checks */
1493
1494 if( q->n_op == PLUS) {
1495 int i;
1496 if( (r=base(ql))>=0 && (i=offset(qr, tlen(p)))>=0) {
1497 makeor2(p, ql, r, i);
1498 return 1;
1499 } else if((r=base(qr))>=0 && (i=offset(ql, tlen(p)))>=0) {
1500 makeor2(p, qr, r, i);
1501 return 1;
1502 }
1503 }
1504
1505
1506 #endif
1507
1508 #if 0
1509 if( (q->n_op==PLUS || q->n_op==MINUS) && qr->n_op == ICON &&
1510 (ql->n_op==REG || (ql->n_op==TEMP && !sharp)) &&
1511 szty(qr->n_type)==1 &&
1512 (ql->n_rval == DECRA(ql->n_reg, 0) ||
1513 /* XXX */
1514 ql->n_rval == FPREG || ql->n_rval == STKREG)) {
1515 #endif
1516 if ((q->n_op==PLUS || q->n_op==MINUS) && qr->n_op == ICON &&
1517 (ql->n_op==REG || (ql->n_op==TEMP && !sharp))) {
1518
1519 temp = getlval(qr);
1520 if( q->n_op == MINUS ) temp = -temp;
1521 r = ql->n_rval;
1522 temp += getlval(ql);
1523 cp = qr->n_name;
1524 if( *cp && ( q->n_op == MINUS || *ql->n_name ) )
1525 return 0;
1526 if( !*cp ) cp = ql->n_name;
1527
1528 ormake:
1529 if( notoff( p->n_type, r, temp, cp ))
1530 return 0;
1531 oregtemp = temp;
1532 oregr = r;
1533 oregcp = cp;
1534 return 1;
1535 }
1536 return 0;
1537 }
1538
1539 static void
1540 ormake(NODE *p)
1541 {
1542 NODE *q = p->n_left;
1543
1544 p->n_op = OREG;
1545 p->n_rval = oregr;
1546 setlval(p, oregtemp);
1547 p->n_name = oregcp;
1548 tfree(q);
1549 }
1550
1551 /*
1552 * look for situations where we can turn * into OREG
1553 */
1554 void
1555 oreg2(NODE *p, void *arg)
1556 {
1557 if (p->n_op != UMUL)
1558 return;
1559 if (oregok(p, 1))
1560 ormake(p);
1561 if (p->n_op == UMUL)
1562 myormake(p);
1563 }
1564
1565 void
1566 canon(NODE *p)
1567 {
1568 /* put p in canonical form */
1569
1570 walkf(p, setleft, 0); /* ptrs at left node for arithmetic */
1571 walkf(p, oreg2, 0); /* look for and create OREG nodes */
1572 mycanon(p); /* your own canonicalization routine(s) */
1573 }
1574
1575 void
1576 comperr(char *str, ...)
1577 {
1578 extern char *ftitle;
1579 va_list ap;
1580
1581 if (nerrors) {
1582 fprintf(stderr,
1583 "cannot recover from earlier errors: goodbye!\n");
1584 exit(1);
1585 }
1586
1587 va_start(ap, str);
1588 fprintf(stderr, "%s, line %d: compiler error: ", ftitle, thisline);
1589 vfprintf(stderr, str, ap);
1590 fprintf(stderr, "\n");
1591 va_end(ap);
1592
1593 #ifdef PCC_DEBUG
1594 if (nodepole && nodepole->n_op != FREE)
1595 fwalk(nodepole, e2print, 0);
1596 #endif
1597 exit(1);
1598 }
1599
1600 /*
1601 * allocate k integers worth of temp space
1602 * we also make the convention that, if the number of words is
1603 * more than 1, it must be aligned for storing doubles...
1604 * Returns bytes offset from base register.
1605 */
1606 int
1607 freetemp(int k)
1608 {
1609 int t, al, sz;
1610
1611 #ifdef WORD_ADDRESSED
1612 sz = k; /* temps is in "words" */
1613 al = k;
1614 #else
1615 al = (k > 1 ? ALDOUBLE/ALCHAR : ALINT/ALCHAR);
1616 sz = k * (SZINT/SZCHAR);
1617 #endif
1618
1619 #ifndef STACK_DOWN
1620 SETOFF(p2autooff, al);
1621 t = p2autooff;
1622 p2autooff += sz;
1623 #else
1624 p2autooff += sz;
1625 SETOFF(p2autooff, al);
1626 t = ( -p2autooff );
1627 #endif
1628 if (p2autooff > p2maxautooff)
1629 p2maxautooff = p2autooff;
1630 return (t);
1631 }
1632
1633 NODE *
1634 storenode(TWORD t, int off)
1635 {
1636 NODE *p;
1637
1638 p = talloc();
1639 p->n_type = t;
1640 storemod(p, off, FPREG); /* XXX */
1641 return p;
1642 }
1643
1644 #ifndef MYSTOREMOD
1645 void
1646 storemod(NODE *q, int off, int reg)
1647 {
1648 NODE *l, *r, *p;
1649
1650 l = mklnode(REG, 0, reg, INCREF(q->n_type));
1651 r = mklnode(ICON, off, 0, INT);
1652 p = mkbinode(PLUS, l, r, INCREF(q->n_type));
1653 q->n_op = UMUL;
1654 q->n_left = p;
1655 q->n_rval = q->n_su = 0;
1656 }
1657 #endif
1658
1659 NODE *
1660 mklnode(int op, CONSZ lval, int rval, TWORD type)
1661 {
1662 NODE *p = talloc();
1663
1664 p->n_name = "";
1665 p->n_qual = 0;
1666 p->n_ap = 0;
1667 p->n_op = op;
1668 setlval(p, lval);
1669 p->n_rval = rval;
1670 p->n_type = type;
1671 p->n_regw = NULL;
1672 p->n_su = 0;
1673 return p;
1674 }
1675
1676 NODE *
1677 mkbinode(int op, NODE *left, NODE *right, TWORD type)
1678 {
1679 NODE *p = talloc();
1680
1681 p->n_name = "";
1682 p->n_qual = 0;
1683 p->n_ap = 0;
1684 p->n_op = op;
1685 p->n_left = left;
1686 p->n_right = right;
1687 p->n_type = type;
1688 p->n_regw = NULL;
1689 p->n_su = 0;
1690 return p;
1691 }
1692
1693 NODE *
1694 mkunode(int op, NODE *left, int rval, TWORD type)
1695 {
1696 NODE *p = talloc();
1697
1698 p->n_name = "";
1699 p->n_qual = 0;
1700 p->n_ap = 0;
1701 p->n_op = op;
1702 p->n_left = left;
1703 p->n_rval = rval;
1704 p->n_type = type;
1705 p->n_regw = NULL;
1706 p->n_su = 0;
1707 return p;
1708 }
1709
1710 struct interpass *
1711 ipnode(NODE *p)
1712 {
1713 struct interpass *ip = tmpalloc(sizeof(struct interpass));
1714
1715 ip->ip_node = p;
1716 ip->type = IP_NODE;
1717 ip->lineno = thisline;
1718 return ip;
1719 }
1720
1721 #ifndef NEWNEED
1722 int
1723 rspecial(struct optab *q, int what)
1724 {
1725 struct rspecial *r = nspecial(q);
1726 while (r->op) {
1727 if (r->op == what)
1728 return r->num;
1729 r++;
1730 }
1731 return -1;
1732 }
1733 #endif
1734
1735 #ifndef XASM_NUMCONV
1736 #define XASM_NUMCONV(x,y,z) 0
1737 #endif
1738
1739 /*
1740 * change numeric argument redirections to the correct node type after
1741 * cleaning up the other nodes.
1742 * be careful about input operands that may have different value than output.
1743 */
1744 static void
1745 delnums(NODE *p, void *arg)
1746 {
1747 struct interpass *ip = arg, *ip2;
1748 NODE *r = ip->ip_node->n_left;
1749 NODE *q;
1750 TWORD t;
1751 int cnt, num;
1752
1753 /* gcc allows % in constraints, but we ignore it */
1754 if (p->n_name[0] == '%' && p->n_name[1] >= '0' && p->n_name[1] <= '9')
1755 p->n_name++;
1756
1757 if (p->n_name[0] < '0' || p->n_name[0] > '9')
1758 return; /* not numeric */
1759 if ((q = listarg(r, p->n_name[0] - '0', &cnt)) == NIL)
1760 comperr("bad delnums");
1761
1762 /* target may have opinions whether to do this conversion */
1763 if (XASM_NUMCONV(ip, p, q))
1764 return;
1765
1766 /* Delete number by adding move-to/from-temp. Later on */
1767 /* the temps may be rewritten to other LTYPEs */
1768 num = p2env.epp->ip_tmpnum++;
1769
1770 /* pre node */
1771 t = p->n_left->n_type;
1772 r = mklnode(TEMP, 0, num, t);
1773 ip2 = ipnode(mkbinode(ASSIGN, tcopy(r), p->n_left, t));
1774 DLIST_INSERT_BEFORE(ip, ip2, qelem);
1775 p->n_left = r;
1776
1777 /* post node */
1778 t = q->n_left->n_type;
1779 r = mklnode(TEMP, 0, num, t);
1780 ip2 = ipnode(mkbinode(ASSIGN, q->n_left, tcopy(r), t));
1781 DLIST_INSERT_AFTER(ip, ip2, qelem);
1782 q->n_left = r;
1783
1784 p->n_name = tmpstrdup(q->n_name);
1785 if (*p->n_name == '=')
1786 p->n_name++;
1787 }
1788
1789 /*
1790 * Ensure that a node is correct for the destination.
1791 */
1792 static void
1793 ltypify(NODE *p, void *arg)
1794 {
1795 struct interpass *ip = arg;
1796 struct interpass *ip2;
1797 TWORD t = p->n_left->n_type;
1798 NODE *q, *r;
1799 int cw, ooff, ww;
1800 char *c;
1801
1802 again:
1803 if (myxasm(ip, p))
1804 return; /* handled by target-specific code */
1805
1806 cw = xasmcode(p->n_name);
1807 switch (ww = XASMVAL(cw)) {
1808 case 'p':
1809 /* pointer */
1810 /* just make register of it */
1811 p->n_name = tmpstrdup(p->n_name);
1812 c = strchr(p->n_name, XASMVAL(cw)); /* cannot fail */
1813 *c = 'r';
1814 /* FALLTHROUGH */
1815 case 'g': /* general; any operand */
1816 if (ww == 'g' && p->n_left->n_op == ICON) {
1817 /* should only be input */
1818 p->n_name = "i";
1819 break;
1820 }
1821 /* FALLTHROUGH */
1822 case 'r': /* general reg */
1823 /* set register class */
1824 if (p->n_left->n_op == REG || p->n_left->n_op == TEMP)
1825 break;
1826 q = p->n_left;
1827 r = (cw & XASMINOUT ? tcopy(q) : q);
1828 p->n_left = mklnode(TEMP, 0, p2env.epp->ip_tmpnum++, t);
1829 if ((cw & XASMASG) == 0) {
1830 ip2 = ipnode(mkbinode(ASSIGN, tcopy(p->n_left), r, t));
1831 DLIST_INSERT_BEFORE(ip, ip2, qelem);
1832 }
1833 if (cw & (XASMASG|XASMINOUT)) {
1834 /* output parameter */
1835 ip2 = ipnode(mkbinode(ASSIGN, q, tcopy(p->n_left), t));
1836 DLIST_INSERT_AFTER(ip, ip2, qelem);
1837 }
1838 break;
1839
1840 case '0': case '1': case '2': case '3': case '4':
1841 case '5': case '6': case '7': case '8': case '9':
1842 break;
1843
1844 case 'm': /* memory operand */
1845 /* store and reload value */
1846 q = p->n_left;
1847 if (optype(q->n_op) == LTYPE) {
1848 if (q->n_op == TEMP) {
1849 ooff = freetemp(szty(t));
1850 cvtemps(ip, q->n_rval, ooff);
1851 } else if (q->n_op == REG)
1852 comperr("xasm m and reg");
1853 } else if (q->n_op == UMUL &&
1854 (q->n_left->n_op != TEMP && q->n_left->n_op != REG)) {
1855 t = q->n_left->n_type;
1856 ooff = p2env.epp->ip_tmpnum++;
1857 ip2 = ipnode(mkbinode(ASSIGN,
1858 mklnode(TEMP, 0, ooff, t), q->n_left, t));
1859 q->n_left = mklnode(TEMP, 0, ooff, t);
1860 DLIST_INSERT_BEFORE(ip, ip2, qelem);
1861 }
1862 break;
1863
1864 case 'i': /* immediate constant */
1865 case 'n': /* numeric constant */
1866 if (p->n_left->n_op == ICON)
1867 break;
1868 p->n_name = tmpstrdup(p->n_name);
1869 c = strchr(p->n_name, XASMVAL(cw)); /* cannot fail */
1870 if (c[1]) {
1871 c[0] = c[1], c[1] = 0;
1872 goto again;
1873 } else
1874 uerror("constant required");
1875 break;
1876
1877 default:
1878 uerror("unsupported xasm option string '%s'", p->n_name);
1879 }
1880 }
1881
1882 /* Extended assembler hacks */
1883 static void
1884 fixxasm(struct p2env *p2e)
1885 {
1886 struct interpass *pole = &p2e->ipole;
1887 struct interpass *ip;
1888 NODE *p;
1889
1890 DLIST_FOREACH(ip, pole, qelem) {
1891 if (ip->type != IP_NODE || ip->ip_node->n_op != XASM)
1892 continue;
1893 thisline = ip->lineno;
1894 p = ip->ip_node->n_left;
1895
1896 if (p->n_op == ICON && p->n_type == STRTY)
1897 continue;
1898
1899 /* replace numeric redirections with its underlying type */
1900 flist(p, delnums, ip);
1901
1902 /*
1903 * Ensure that the arg nodes can be directly addressable
1904 * We decide that everything shall be LTYPE here.
1905 */
1906 flist(p, ltypify, ip);
1907 }
1908 }
1909
1910 /*
1911 * Extract codeword from xasm string */
1912 int
1913 xasmcode(char *s)
1914 {
1915 int cw = 0, nm = 0;
1916
1917 while (*s) {
1918 switch ((int)*s) {
1919 case '=': cw |= XASMASG; break;
1920 case '&': cw |= XASMCONSTR; break;
1921 case '+': cw |= XASMINOUT; break;
1922 case '%': break;
1923 default:
1924 if ((*s >= 'a' && *s <= 'z') ||
1925 (*s >= 'A' && *s <= 'Z') ||
1926 (*s >= '0' && *s <= '9')) {
1927 if (nm == 0)
1928 cw |= *s;
1929 else
1930 cw |= (*s << ((nm + 1) * 8));
1931 nm++;
1932 break;
1933 }
1934 uerror("bad xasm constraint %c", *s);
1935 }
1936 s++;
1937 }
1938 return cw;
1939 }
1940
1941 static int xasnum, xoffnum;
1942
1943 static void
1944 xconv(NODE *p, void *arg)
1945 {
1946 if (p->n_op != TEMP || p->n_rval != xasnum)
1947 return;
1948 storemod(p, xoffnum, FPREG); /* XXX */
1949 }
1950
1951 /*
1952 * Convert nodes of type TEMP to op with lval off.
1953 */
1954 static void
1955 cvtemps(struct interpass *ipl, int tnum, int off)
1956 {
1957 struct interpass *ip;
1958
1959 xasnum = tnum;
1960 xoffnum = off;
1961
1962 DLIST_FOREACH(ip, ipl, qelem)
1963 if (ip->type == IP_NODE)
1964 walkf(ip->ip_node, xconv, 0);
1965 walkf(ipl->ip_node, xconv, 0);
1966 }