Debian Codegraph @

cc/ccom/trees.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* $Id: trees.c,v 1.393 2021/10/13 17:07:04 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 * 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 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 *
36 * Redistributions of source code and documentation must retain the above
37 * copyright notice, this list of conditions and the following disclaimer.
38 * Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditionsand the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed or owned by Caldera
44 * International, Inc.
45 * Neither the name of Caldera International, Inc. nor the names of other
46 * contributors may be used to endorse or promote products derived from
47 * this software without specific prior written permission.
48 *
49 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
50 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
51 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
54 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
58 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
59 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
60 * POSSIBILITY OF SUCH DAMAGE.
61 */
62 /*
63 * Some of the changes from 32V include:
64 * - Understand "void" as type.
65 * - Handle enums as ints everywhere.
66 * - Convert some C-specific ops into branches.
67 */
68
69 # include "pass1.h"
70 # include "pass2.h"
71
72 # include <stdarg.h>
73 # include <string.h>
74 # include <stdlib.h>
75
76 static void chkpun(P1ND *p);
77 static int opact(P1ND *p);
78 static int moditype(TWORD);
79 static P1ND *strargs(P1ND *);
80 static void rmcops(P1ND *p);
81 static P1ND *tymatch(P1ND *p);
82 static P1ND *rewincop(P1ND *p1, P1ND *p2, int op);
83 static int has_se(P1ND *p);
84 static struct symtab *findmember(struct symtab *, char *);
85 int inftn; /* currently between epilog/prolog */
86 P1ND *cstknode(TWORD t, union dimfun *df, struct attr *ap);
87
88 static char *tnames[] = {
89 "undef",
90 "farg",
91 "char",
92 "unsigned char",
93 "short",
94 "unsigned short",
95 "int",
96 "unsigned int",
97 "long",
98 "unsigned long",
99 "long long",
100 "unsigned long long",
101 "float",
102 "double",
103 "long double",
104 "strty",
105 "unionty",
106 "enumty",
107 "moety",
108 "void",
109 "signed", /* pass1 */
110 "bool", /* pass1 */
111 "fimag", /* pass1 */
112 "dimag", /* pass1 */
113 "limag", /* pass1 */
114 "fcomplex", /* pass1 */
115 "dcomplex", /* pass1 */
116 "lcomplex", /* pass1 */
117 "enumty", /* pass1 */
118 "?", "?"
119 };
120
121 /* some special actions, used in finding the type of nodes */
122 # define NCVT 01
123 # define PUN 02
124 # define TYPL 04
125 # define TYPR 010
126 # define TYMATCH 040
127 # define LVAL 0100
128 # define CVTO 0200
129 # define CVTL 0400
130 # define CVTR 01000
131 # define PTMATCH 02000
132 # define OTHER 04000
133 # define NCVTR 010000
134 # define PROML 020000 /* promote left operand */
135
136 /* node conventions:
137
138 NAME: rval>0 is stab index for external
139 rval<0 is -inlabel number
140 lval is offset in bits
141 ICON: lval has the value
142 rval has the STAB index, or - label number,
143 if a name whose address is in the constant
144 rval = NONAME means no name
145 REG: rval is reg. identification cookie
146
147 */
148
149 /* negatives of relationals */
150 int p1negrel[] = { NE, EQ, GT, GE, LT, LE, UGT, UGE, ULT, ULE };
151
152 /* Have some defaults for most common targets */
153 #ifndef WORD_ADDRESSED
154 #define offcon(o,t,d,ap) xbcon((o/SZCHAR), NULL, INTPTR)
155 #define VBLOCK(p,b,t,d,a) buildtree(DIV, p, b)
156 #define MBLOCK(p,b,t,d,a) buildtree(MUL, p, b)
157 #else
158 #define VBLOCK(p,b,t,d,a) block(PVCONV, p, b, t, d, a)
159 #define MBLOCK(p,b,t,d,a) block(PMCONV, p, b, t, d, a)
160 #endif
161
162 P1ND *
163 buildtree(int o, P1ND *l, P1ND *r)
164 {
165 P1ND *p, *q;
166 int actions;
167 int opty, n;
168 struct symtab *sp = NULL; /* XXX gcc */
169 P1ND *lr, *ll;
170
171 #ifdef PCC_DEBUG
172 if (bdebug) {
173 printf("buildtree(%s, %p, %p)\n", copst(o), l, r);
174 if (l) p1fwalk(l, eprint, 0);
175 if (r) p1fwalk(r, eprint, 0);
176 }
177 #endif
178 opty = coptype(o);
179
180 /* check for constants */
181
182 if (o == ANDAND || o == OROR || o == NOT) {
183 if (l->n_op == FCON) {
184 p = bcon(!FLOAT_ISZERO(l->n_scon));
185 p1nfree(l);
186 l = p;
187 }
188 if (o != NOT && r->n_op == FCON) {
189 p = bcon(!FLOAT_ISZERO(r->n_scon));
190 p1nfree(r);
191 r = p;
192 }
193 }
194
195 if( opty == UTYPE && l->n_op == ICON ){
196
197 switch( o ){
198
199 case NOT:
200 case UMINUS:
201 case COMPL:
202 if( conval( l, o, l ) ) return(l);
203 break;
204 }
205 } else if (o == NOT && l->n_op == FCON) {
206 l = clocal(block(SCONV, l, NULL, INT, 0, 0));
207 } else if( o == UMINUS && l->n_op == FCON){
208 FLOAT_NEG(l->n_scon);
209 return(l);
210
211 } else if( o==QUEST &&
212 (l->n_op==ICON || (l->n_op==NAME && ISARY(l->n_type)))) {
213 CONSZ c = glval(l);
214 if (l->n_op==NAME)
215 c = 1; /* will become constant later */
216 p1nfree(l);
217 if (c) {
218 p1walkf(r->n_right, putjops, 0);
219 p1tfree(r->n_right);
220 l = r->n_left;
221 } else {
222 p1walkf(r->n_left, putjops, 0);
223 p1tfree(r->n_left);
224 l = r->n_right;
225 }
226 p1nfree(r);
227 return(l);
228 } else if( opty == BITYPE && l->n_op == ICON && r->n_op == ICON ){
229
230 switch( o ){
231
232 case PLUS:
233 case MINUS:
234 case MUL:
235 case DIV:
236 case MOD:
237 /*
238 * Do type propagation for simple types here.
239 * The constant value is correct anyway.
240 * Maybe this op shortcut should be removed?
241 */
242 if (l->n_sp == NULL && r->n_sp == NULL &&
243 l->n_type < BTMASK && r->n_type < BTMASK) {
244 if (l->n_type > r->n_type)
245 r->n_type = l->n_type;
246 else
247 l->n_type = r->n_type;
248 }
249 /* FALLTHROUGH */
250 case ULT:
251 case UGT:
252 case ULE:
253 case UGE:
254 case LT:
255 case GT:
256 case LE:
257 case GE:
258 case EQ:
259 case NE:
260 case ANDAND:
261 case OROR:
262 case AND:
263 case OR:
264 case ER:
265 case LS:
266 case RS:
267 if (!ISPTR(l->n_type) && !ISPTR(r->n_type)) {
268 if( conval( l, o, r ) ) {
269 p1nfree(r);
270 return(l);
271 }
272 }
273 break;
274 }
275 } else if (opty == BITYPE && (l->n_op == FCON || l->n_op == ICON) &&
276 (r->n_op == FCON || r->n_op == ICON) && (o == PLUS || o == MINUS ||
277 o == MUL || o == DIV || (o >= EQ && o <= GT) )) {
278 /* at least one side is FCON */
279
280 #ifndef CC_DIV_0
281 if (o == DIV &&
282 ((r->n_op == ICON && glval(r) == 0) ||
283 (r->n_op == FCON && FLOAT_ISZERO(r->n_scon))))
284 goto runtime; /* HW dependent */
285 #endif
286 if (l->n_op == ICON) {
287 if (!concast(l, r->n_type))
288 cerror("fail cast const");
289 } else if (r->n_op == ICON) {
290 if (!concast(r, l->n_type))
291 cerror("fail cast const");
292 }
293
294 switch(o){
295 case PLUS:
296 case MINUS:
297 case MUL:
298 case DIV:
299 switch (o) {
300 case PLUS:
301 FLOAT_PLUS(l, r);
302 break;
303 case MINUS:
304 FLOAT_MINUS(l, r);
305 break;
306 case MUL:
307 FLOAT_MUL(l, r);
308 break;
309 case DIV:
310 FLOAT_DIV(l, r);
311 break;
312 }
313 p1nfree(r);
314 return(l);
315 case EQ:
316 case NE:
317 case LE:
318 case LT:
319 case GE:
320 case GT:
321 switch (o) {
322 case EQ:
323 n = FLOAT_EQ(l->n_scon, r->n_scon);
324 break;
325 case NE:
326 n = FLOAT_NE(l->n_scon, r->n_scon);
327 break;
328 case LE:
329 n = FLOAT_LE(l->n_scon, r->n_scon);
330 break;
331 case LT:
332 n = FLOAT_LT(l->n_scon, r->n_scon);
333 break;
334 case GE:
335 n = FLOAT_GE(l->n_scon, r->n_scon);
336 break;
337 case GT:
338 n = FLOAT_GT(l->n_scon, r->n_scon);
339 break;
340 default:
341 n = 0; /* XXX flow analysis */
342 }
343 p1nfree(r);
344 p1nfree(l);
345 return bcon(n);
346 }
347 } else if ((cdope(o)&ASGOPFLG) && o != RETURN && o != CAST) {
348 if (l->n_op == SCONV && l->n_left->n_op == FLD)
349 l = p1nfree(l);
350 /*
351 * Handle side effects by storing address in temporary q.
352 * Side effect nodes always have an UMUL.
353 */
354 if (has_se(l)) {
355 ll = l->n_left;
356
357 q = cstknode(ll->n_type, ll->n_df, ll->n_ap);
358 l->n_left = p1tcopy(q);
359 q = buildtree(ASSIGN, q, ll);
360 } else
361 q = bcon(0); /* No side effects */
362
363 /*
364 * Modify the trees so that the compound op is rewritten.
365 */
366 /* avoid casting of LHS */
367 if ((cdope(o) & SIMPFLG) && ISINTEGER(l->n_type) &&
368 l->n_type != BOOL)
369 r = ccast(r, l->n_type, l->n_qual, l->n_df, l->n_ap);
370
371 r = buildtree(UNASG o, p1tcopy(l), r);
372 r = buildtree(ASSIGN, l, r);
373 l = q;
374 o = COMOP;
375 } else if (o == INCR || o == DECR) {
376 if (l->n_op == SCONV && l->n_left->n_op == FLD)
377 l = p1nfree(l);
378 /*
379 * Rewrite to (t=d,d=d+1,t)
380 */
381 if (has_se(l)) {
382 ll = l->n_left;
383
384 q = cstknode(ll->n_type, ll->n_df, ll->n_ap);
385 l->n_left = p1tcopy(q);
386 q = buildtree(ASSIGN, q, ll);
387 } else
388 q = bcon(0); /* No side effects */
389
390 /* Boolean has special syntax. */
391 if (l->n_type == BOOL) {
392 r = rewincop(l, r, o == INCR ? ASSIGN : EREQ);
393 } else
394 r = rewincop(l, r, o == INCR ? PLUSEQ : MINUSEQ);
395 l = q;
396 o = COMOP;
397 } else if (o == ASSIGN && l->n_op == SCONV && l->n_left->n_op == FLD) {
398 l = p1nfree(l);
399 }
400
401
402 #ifndef CC_DIV_0
403 runtime:
404 #endif
405 /* its real; we must make a new node */
406
407 p = block(o, l, r, INT, 0, 0);
408
409 actions = opact(p);
410
411 if (actions & PROML)
412 p->n_left = intprom(p->n_left);
413
414 if (actions & LVAL) { /* check left descendent */
415 if (notlval(p->n_left)) {
416 uerror("lvalue required");
417 p1nfree(p);
418 return l;
419 #ifdef notyet
420 } else {
421 if ((l->n_type > BTMASK && ISCON(l->n_qual)) ||
422 (l->n_type <= BTMASK && ISCON(l->n_qual << TSHIFT)))
423 if (blevel > 0)
424 uerror("lvalue is declared const");
425 #endif
426 }
427 }
428
429 if( actions & NCVTR ){
430 p->n_left = pconvert( p->n_left );
431 }
432 else if( !(actions & NCVT ) ){
433 switch( opty ){
434
435 case BITYPE:
436 p->n_right = pconvert( p->n_right );
437 /* FALLTHROUGH */
438 case UTYPE:
439 p->n_left = pconvert( p->n_left );
440
441 }
442 }
443
444 if ((actions&PUN) && (o!=CAST))
445 chkpun(p);
446
447 if( actions & (TYPL|TYPR) ){
448
449 q = (actions&TYPL) ? p->n_left : p->n_right;
450
451 p->n_type = q->n_type;
452 p->n_qual = q->n_qual;
453 p->n_df = q->n_df;
454 p->n_ap = q->n_ap;
455 }
456
457 if( actions & CVTL ) p = convert( p, CVTL );
458 if( actions & CVTR ) p = convert( p, CVTR );
459 if( actions & TYMATCH ) p = tymatch(p);
460 if( actions & PTMATCH ) p = ptmatch(p);
461
462 if( actions & OTHER ){
463 struct symtab *sp1;
464
465 l = p->n_left;
466 r = p->n_right;
467
468 switch(o){
469
470 case NAME:
471 cerror("buildtree NAME");
472
473 case STREF:
474 /* p->x turned into *(p+offset) */
475 /* rhs must be a name; check correctness */
476
477 /* Find member symbol struct */
478 if (l->n_type != PTR+STRTY && l->n_type != PTR+UNIONTY){
479 uerror("struct or union required");
480 break;
481 }
482
483 if ((sp1 = strmemb(l->n_ap)) == NULL) {
484 uerror("undefined struct or union");
485 break;
486 }
487
488 if ((sp = findmember(sp1, r->n_name)) == NULL) {
489 uerror("member '%s' not declared", r->n_name);
490 break;
491 }
492
493 r->n_sp = sp;
494 p = stref(p);
495 break;
496
497 case UMUL:
498 if (l->n_op == ADDROF) {
499 p1nfree(p);
500 p = p1nfree(l);
501 }
502 if( !ISPTR(l->n_type))uerror("illegal indirection");
503 p->n_type = DECREF(l->n_type);
504 p->n_qual = DECREF(l->n_qual);
505 p->n_df = l->n_df;
506 p->n_ap = l->n_ap;
507 break;
508
509 case ADDROF:
510 switch( l->n_op ){
511
512 case UMUL:
513 p1nfree(p);
514 p = p1nfree(l);
515 /* FALLTHROUGH */
516 case TEMP:
517 case NAME:
518 p->n_type = INCREF(l->n_type);
519 p->n_qual = INCQAL(l->n_qual);
520 p->n_df = l->n_df;
521 p->n_ap = l->n_ap;
522 break;
523
524 case COMOP:
525 p1nfree(p);
526 lr = buildtree(ADDROF, l->n_right, NULL);
527 p = buildtree( COMOP, l->n_left, lr );
528 p1nfree(l);
529 break;
530
531 case QUEST:
532 lr = buildtree( ADDROF, l->n_right->n_right, NULL );
533 ll = buildtree( ADDROF, l->n_right->n_left, NULL );
534 p1nfree(p); p1nfree(l->n_right);
535 p = buildtree( QUEST, l->n_left, buildtree( COLON, ll, lr ) );
536 p1nfree(l);
537 break;
538
539 default:
540 uerror("unacceptable operand of &: %d", l->n_op );
541 break;
542 }
543 break;
544
545 case LS:
546 case RS: /* must make type size at least int... */
547 if (p->n_type == CHAR || p->n_type == SHORT) {
548 p->n_left = makety(l, INT, 0, 0, 0);
549 } else if (p->n_type == UCHAR || p->n_type == USHORT) {
550 p->n_left = makety(l, UNSIGNED, 0, 0, 0);
551 }
552 l = p->n_left;
553 p->n_type = l->n_type;
554 p->n_qual = l->n_qual;
555 p->n_df = l->n_df;
556 p->n_ap = l->n_ap;
557 if(tsize(r->n_type, r->n_df, r->n_ap) > SZINT)
558 p->n_right = makety(r, INT, 0, 0, 0);
559 break;
560
561 case RETURN:
562 case ASSIGN:
563 case CAST:
564 /* structure assignment */
565 /* take the addresses of the two sides; then make an
566 * operator using STASG and
567 * the addresses of left and right */
568
569 if (strmemb(l->n_ap) != strmemb(r->n_ap))
570 uerror("assignment of different structures");
571
572 r = buildtree(ADDROF, r, NULL);
573
574 l = block(STASG, l, r, r->n_type, r->n_df, r->n_ap);
575 l = clocal(l);
576
577 if( o == RETURN ){
578 p1nfree(p);
579 p = l;
580 break;
581 }
582
583 p->n_op = UMUL;
584 p->n_left = l;
585 p->n_right = NULL;
586 break;
587
588 case QUEST: /* fixup types of : */
589 if (r->n_left->n_type != p->n_type)
590 r->n_left = makety(r->n_left, p->n_type,
591 p->n_qual, p->n_df, p->n_ap);
592 if (r->n_right->n_type != p->n_type)
593 r->n_right = makety(r->n_right, p->n_type,
594 p->n_qual, p->n_df, p->n_ap);
595 break;
596
597 case COLON:
598 /* structure colon */
599
600 if (strmemb(l->n_ap) != strmemb(r->n_ap))
601 uerror( "type clash in conditional" );
602 break;
603
604 case CALL:
605 p->n_right = r = strargs(p->n_right);
606 p = funcode(p);
607 /* FALLTHROUGH */
608 case UCALL:
609 if (!ISPTR(l->n_type))
610 uerror("illegal function");
611 p->n_type = DECREF(l->n_type);
612 if (!ISFTN(p->n_type))
613 uerror("illegal function");
614 p->n_type = DECREF(p->n_type);
615 p->n_df = l->n_df+1; /* add one for prototypes */
616 p->n_ap = l->n_ap;
617 if (p->n_type == STRTY || p->n_type == UNIONTY) {
618 /* function returning structure */
619 /* make function really return ptr to str., with * */
620
621 p->n_op += STCALL-CALL;
622 p->n_type = INCREF(p->n_type);
623 p = clocal(p); /* before recursing */
624 p = buildtree(UMUL, p, NULL);
625
626 }
627 break;
628
629 default:
630 cerror( "other code %d", o );
631 }
632 }
633
634 /* fixup type in bit-field assignment */
635 if (p->n_op == ASSIGN && l->n_op == FLD && UPKFSZ(l->n_rval) < SZINT)
636 p = makety(p, INT, 0, 0, 0);
637
638 /*
639 * Allow (void)0 casts.
640 * XXX - anything on the right side must be possible to cast.
641 * XXX - remove void types further on.
642 */
643 if (p->n_op == CAST && p->n_type == VOID &&
644 p->n_right->n_op == ICON)
645 p->n_right->n_type = VOID;
646
647 if (actions & CVTO)
648 p = oconvert(p);
649 p = clocal(p);
650
651 #ifdef PCC_DEBUG
652 if (bdebug) {
653 printf("End of buildtree:\n");
654 p1fwalk(p, eprint, 0);
655 }
656 #endif
657
658 return(p);
659
660 }
661
662 /*
663 * Rewrite ++/-- to (t=p, p++, t) ops on types that do not act act as usual.
664 */
665 static P1ND *
666 rewincop(P1ND *p1, P1ND *p2, int op)
667 {
668 P1ND *t, *r;
669
670 t = cstknode(p1->n_type, p1->n_df, p1->n_ap);
671 r = buildtree(ASSIGN, p1tcopy(t), p1tcopy(p1));
672 r = buildtree(COMOP, r, buildtree(op, p1, eve(p2)));
673 return buildtree(COMOP, r, t);
674 }
675
676
677 /* Find a member in a struct or union. May be an unnamed member */
678 static struct symtab *
679 findmember(struct symtab *sp, char *s)
680 {
681 struct symtab *sp2, *sp3;
682
683 for (; sp != NULL; sp = sp->snext) {
684 if (sp->sname[0] == '*') {
685 /* unnamed member, recurse down */
686 if ((sp2 = findmember(strmemb(sp->sap), s))) {
687 sp3 = tmpalloc(sizeof (struct symtab));
688 *sp3 = *sp2;
689 sp3->soffset += sp->soffset;
690 return sp3;
691 }
692 } else if (sp->sname == s)
693 return sp;
694 }
695 return NULL;
696 }
697
698
699 /*
700 * Check if there will be a lost label destination inside of a ?:
701 * It cannot be reached so just print it out.
702 */
703 void
704 putjops(P1ND *p, void *arg)
705 {
706 if (p->n_op == COMOP && p->n_left->n_op == GOTO)
707 plabel((int)glval(p->n_left->n_left)+2);
708 }
709
710 /*
711 * Build a name node based on a symtab entry.
712 * broken out from buildtree().
713 */
714 P1ND *
715 nametree(struct symtab *sp)
716 {
717 P1ND *p;
718
719 p = block(NAME, NULL, NULL, sp->stype, sp->sdf, sp->sap);
720 p->n_qual = sp->squal;
721 p->n_sp = sp;
722
723 #ifndef NO_C_BUILTINS
724 if (sp->sname[0] == '_' && strncmp(sp->sname, "__builtin_", 10) == 0)
725 return p; /* do not touch builtins here */
726
727 #endif
728
729 if (sp->sflags & STNODE) {
730 /* Generated for optimizer */
731 p->n_op = TEMP;
732 p->n_rval = sp->soffset;
733 }
734
735 #ifdef GCC_COMPAT
736 /* Get a label name */
737 if (sp->sflags == SLBLNAME)
738 sp->stype = p->n_type = VOID;
739 if (sp->sflags & SINREG) {
740 p->n_op = REG;
741 regno(p) = (int)sp->soffset;
742 }
743 #endif
744 if (sp->stype == UNDEF) {
745 uerror("%s undefined", sp->sname);
746 /* make p look reasonable */
747 p->n_type = INT;
748 p->n_df = NULL;
749 defid(p, SNULL);
750 }
751 if (sp->sclass == MOE) {
752 p->n_op = ICON;
753 slval(p, sp->soffset);
754 p->n_df = NULL;
755 p->n_sp = NULL;
756 }
757 return clocal(p);
758 }
759
760 /*
761 * Cast a node to another type by inserting a cast.
762 * Just a nicer interface to buildtree.
763 * Returns the new tree.
764 */
765 P1ND *
766 cast(P1ND *p, TWORD t, TWORD u)
767 {
768 P1ND *q;
769
770 q = block(NAME, NULL, NULL, t, 0, 0);
771 q->n_qual = u;
772 q = buildtree(CAST, q, p);
773 p = q->n_right;
774 p1nfree(q->n_left);
775 p1nfree(q);
776 return p;
777 }
778
779 /*
780 * Cast and complain if necessary by not inserining a cast.
781 */
782 P1ND *
783 ccast(P1ND *p, TWORD t, TWORD u, union dimfun *df, struct attr *ap)
784 {
785 P1ND *q;
786
787 /* let buildtree do typechecking (and casting) */
788 q = block(NAME, NULL, NULL, t, df, ap);
789 p = buildtree(ASSIGN, q, p);
790 p1nfree(p->n_left);
791 q = optim(p->n_right);
792 p1nfree(p);
793 return q;
794 }
795
796 /*
797 * Do an actual cast of a constant (if possible).
798 * Routine assumes 2-complement. p is cast to type t.
799 * Returns 1 if handled, 0 otherwise.
800 */
801 int
802 concast(P1ND *p, TWORD t)
803 {
804 extern short sztable[];
805 CONSZ val;
806
807 if (p->n_op != ICON && p->n_op != FCON) /* only constants */
808 return 0;
809 if (p->n_op == ICON && p->n_sp != NULL) { /* no addresses */
810 if (t == BOOL) {
811 slval(p, 1), p->n_type = BOOL, p->n_sp = NULL;
812 return 1;
813 }
814 return 0;
815 }
816 if (((p->n_type & TMASK) && t != BOOL) || (t & TMASK)) /* no pointers */
817 return 0;
818
819 //printf("concast till %d\n", t);
820 //p1fwalk(p, eprint, 0);
821
822 #define TYPMSK(y) ((((1LL << (y-1))-1) << 1) | 1)
823 if (p->n_op == ICON) {
824 val = glval(p);
825
826 if (t == BOOL) {
827 if (val)
828 slval(p, 1);
829 } else if (t <= ULONGLONG) {
830 slval(p, val & TYPMSK(sztable[t]));
831 if (!ISUNSIGNED(t)) {
832 if (val & (1LL << (sztable[t]-1)))
833 slval(p, glval(p) | ~TYPMSK(sztable[t]));
834 }
835 } else if (t <= LDOUBLE) {
836 p->n_op = FCON;
837 p->n_scon = sfallo();
838 FLOAT_INT2FP(p->n_scon, val, p->n_type);
839 }
840 } else { /* p->n_op == FCON */
841 if (t == BOOL) {
842 p->n_op = ICON;
843 slval(p, !FLOAT_ISZERO(p->n_scon));
844 p->n_sp = NULL;
845 } else if (t <= ULONGLONG) {
846 p->n_op = ICON;
847 FLOAT_FP2INT(glval(p), p->n_scon, t);
848 p->n_sp = NULL;
849 } else {
850 FLOAT_FP2FP(p->n_scon, t);
851 }
852 }
853 p->n_type = t;
854 //fwalk(p, eprint, 0);
855 return 1;
856 }
857
858 /*
859 * Do a conditional branch.
860 */
861 void
862 cbranch(P1ND *p, P1ND *q)
863 {
864 p = buildtree(CBRANCH, p, q);
865 if (p->n_left->n_op == ICON) {
866 if (glval(p->n_left) != 0) {
867 branch((int)glval(q)); /* branch always */
868 reached = 0;
869 }
870 p1tfree(p);
871 return;
872 }
873 ecomp(p);
874 }
875
876 P1ND *
877 strargs(register P1ND *p)
878 {
879 /* rewrite structure flavored arguments */
880
881 if( p->n_op == CM ){
882 p->n_left = strargs( p->n_left );
883 p->n_right = strargs( p->n_right );
884 return( p );
885 }
886
887 if( p->n_type == STRTY || p->n_type == UNIONTY ){
888 p = block(STARG, p, NULL, p->n_type, p->n_df, p->n_ap);
889 p->n_left = buildtree( ADDROF, p->n_left, NULL );
890 p = clocal(p);
891 }
892 return( p );
893 }
894
895 /*
896 * apply the op o to the lval part of p; if binary, rhs is val
897 */
898 int
899 conval(P1ND *p, int o, P1ND *q)
900 {
901 TWORD tl = p->n_type, tr = q->n_type, td;
902 int i, u;
903 CONSZ val;
904 U_CONSZ v1, v2;
905
906 val = glval(q);
907
908 /* make both sides same type */
909 if (tl < BTMASK && tr < BTMASK) {
910 td = tl > tr ? tl : tr;
911 if (td < INT)
912 td = INT;
913 u = ISUNSIGNED(td);
914 if (tl != td)
915 p = makety(p, td, 0, 0, 0);
916 if (tr != td)
917 q = makety(q, td, 0, 0, 0);
918 } else
919 u = ISUNSIGNED(tl) || ISUNSIGNED(tr);
920 if( u && (o==LE||o==LT||o==GE||o==GT)) o += (UGE-GE);
921
922 if (p->n_sp != NULL && q->n_sp != NULL)
923 return(0);
924 if (q->n_sp != NULL && o != PLUS)
925 return(0);
926 if (p->n_sp != NULL && o != PLUS && o != MINUS)
927 return(0);
928
929 v1 = glval(p);
930 v2 = glval(q);
931 if (v2 == 0 && (cdope(o) & DIVFLG))
932 return 0; /* leave division by zero to runtime */
933 switch( o ){
934
935 case PLUS:
936 slval(p, (glval(p) + val));
937 if (p->n_sp == NULL) {
938 p->n_right = q->n_right;
939 p->n_type = q->n_type;
940 }
941 break;
942 case MINUS:
943 slval(p, (glval(p) - val));
944 break;
945 case MUL:
946 slval(p, (glval(p) * val));
947 break;
948 case DIV:
949 if (u) {
950 v1 /= v2;
951 slval(p, v1);
952 } else
953 slval(p, (glval(p) / val));
954 break;
955 case MOD:
956 if (u) {
957 v1 %= v2;
958 slval(p, v1);
959 } else
960 slval(p, (glval(p) % val));
961 break;
962 case AND:
963 slval(p, (glval(p) & val));
964 break;
965 case OR:
966 slval(p, (glval(p) | val));
967 break;
968 case ER:
969 slval(p, (glval(p) ^ val));
970 break;
971 case LS:
972 i = (int)val;
973 slval(p, glval(p) << i);
974 break;
975 case RS:
976 i = (int)val;
977 if (u) {
978 v1 = v1 >> i;
979 slval(p, v1);
980 } else {
981 slval(p, glval(p) >> i);
982 }
983 break;
984
985 case UMINUS:
986 slval(p, (-glval(p)));
987 break;
988 case COMPL:
989 slval(p, (~glval(p)));
990 break;
991 case NOT:
992 slval(p, (!glval(p)));
993 p->n_type = INT;
994 break;
995 case LT:
996 slval(p, (glval(p) < val));
997 break;
998 case LE:
999 slval(p, (glval(p) <= val));
1000 break;
1001 case GT:
1002 slval(p, (glval(p) > val));
1003 break;
1004 case GE:
1005 slval(p, (glval(p) >= val));
1006 break;
1007 case ULT:
1008 slval(p, (v1 < v2));
1009 break;
1010 case ULE:
1011 slval(p, (v1 <= v2));
1012 break;
1013 case UGT:
1014 slval(p, (v1 > v2));
1015 break;
1016 case UGE:
1017 slval(p, (v1 >= v2));
1018 break;
1019 case EQ:
1020 slval(p, (glval(p) == val));
1021 break;
1022 case NE:
1023 slval(p, (glval(p) != val));
1024 break;
1025 case ANDAND:
1026 slval(p, (glval(p) && val));
1027 break;
1028 case OROR:
1029 slval(p, (glval(p) || val));
1030 break;
1031 default:
1032 return(0);
1033 }
1034 /* Do the best in making everything type correct after calc */
1035 if (clogop(o))
1036 p->n_type = INT;
1037 if (p->n_sp == NULL && q->n_sp == NULL) {
1038 slval(p, valcast(glval(p), p->n_type));
1039 }
1040 return(1);
1041 }
1042
1043 /*
1044 * Ensure that v matches the type t; sign- or zero-extended
1045 * as suitable to CONSZ.
1046 * Only to be used for integer types.
1047 */
1048 CONSZ
1049 valcast(CONSZ v, TWORD t)
1050 {
1051 CONSZ r;
1052 int sz;
1053
1054 if (t < CHAR || t > ULONGLONG)
1055 return v; /* cannot cast */
1056
1057 if (t >= LONGLONG)
1058 return v; /* already largest */
1059
1060 #define M(x) ((((1ULL << ((x)-1)) - 1) << 1) + 1)
1061 #define NOTM(x) (~M(x))
1062 #define SBIT(x) (1ULL << ((x)-1))
1063
1064 sz = (int)tsize(t, NULL, NULL);
1065 r = v & M(sz);
1066 if (!ISUNSIGNED(t) && (SBIT(sz) & r))
1067 r = r | NOTM(sz);
1068 return r;
1069 }
1070
1071 /*
1072 * Checks p for the existence of a pun. This is called when the op of p
1073 * is ASSIGN, RETURN, CAST, COLON, or relational.
1074 * One case is when enumerations are used: this applies only to lint.
1075 * In the other case, one operand is a pointer, the other integer type
1076 * we check that this integer is in fact a constant zero...
1077 * in the case of ASSIGN, any assignment of pointer to integer is illegal
1078 * this falls out, because the LHS is never 0.
1079 * XXX - check for COMOPs in assignment RHS?
1080 */
1081 void
1082 chkpun(P1ND *p)
1083 {
1084 union dimfun *d1, *d2;
1085 P1ND *q;
1086 int t1, t2;
1087
1088 t1 = p->n_left->n_type;
1089 t2 = p->n_right->n_type;
1090
1091 switch (p->n_op) {
1092 case RETURN:
1093 /* return of void allowed but nothing else */
1094 if (t1 == VOID && t2 == VOID)
1095 return;
1096 if (t1 == VOID) {
1097 werror("returning value from void function");
1098 return;
1099 }
1100 if (t2 == VOID) {
1101 uerror("using void value");
1102 return;
1103 }
1104 break;
1105 case COLON:
1106 if (t1 == VOID && t2 == VOID)
1107 return;
1108 break;
1109 default:
1110 if ((t1 == VOID && t2 != VOID) || (t1 != VOID && t2 == VOID)) {
1111 uerror("value of void expression used");
1112 return;
1113 }
1114 break;
1115 }
1116
1117 /* allow void pointer assignments in any direction */
1118 if (BTYPE(t1) == VOID && (t2 & TMASK))
1119 return;
1120 if (BTYPE(t2) == VOID && (t1 & TMASK))
1121 return;
1122
1123 /* boolean have special syntax */
1124 if (t1 == BOOL) {
1125 if (!ISARY(t2)) /* Anything scalar */
1126 return;
1127 }
1128
1129 if (ISPTR(t1) || ISARY(t1))
1130 q = p->n_right;
1131 else
1132 q = p->n_left;
1133
1134 if (!ISPTR(q->n_type) && !ISARY(q->n_type)) {
1135 if (q->n_op != ICON || glval(q) != 0)
1136 werror("illegal combination of pointer and integer");
1137 } else {
1138 if (t1 == t2) {
1139 if (ISSOU(BTYPE(t1)) &&
1140 !suemeq(p->n_left->n_ap, p->n_right->n_ap))
1141 werror("illegal structure pointer combination");
1142 return;
1143 }
1144 d1 = p->n_left->n_df;
1145 d2 = p->n_right->n_df;
1146 for (;;) {
1147 if (ISARY(t1) || ISPTR(t1)) {
1148 if (!ISARY(t2) && !ISPTR(t2))
1149 break;
1150 if (ISARY(t1) && ISARY(t2) && d1->ddim != d2->ddim) {
1151 werror("illegal array size combination");
1152 return;
1153 }
1154 if (ISARY(t1))
1155 ++d1;
1156 if (ISARY(t2))
1157 ++d2;
1158 } else if (ISFTN(t1)) {
1159 if (chkftn(d1->dfun, d2->dfun)) {
1160 werror("illegal function "
1161 "pointer combination");
1162 return;
1163 }
1164 ++d1;
1165 ++d2;
1166 } else
1167 break;
1168 t1 = DECREF(t1);
1169 t2 = DECREF(t2);
1170 }
1171 if (DEUNSIGN(t1) != DEUNSIGN(t2))
1172 werror("illegal pointer combination");
1173 else if (t1 != t2)
1174 warner(Wpointer_sign);
1175 }
1176 }
1177
1178 static P1ND *
1179 offplus(P1ND *p, int off, TWORD t, TWORD q, union dimfun *d, struct attr *ap) {
1180 if (off != 0) {
1181 p = block(PLUS, p, offcon(off, t, d, ap), t, d, ap);
1182 p->n_qual = q;
1183 p = optim(p);
1184 }
1185
1186 return buildtree(UMUL, p, NULL);
1187 }
1188
1189 P1ND *
1190 stref(P1ND *p)
1191 {
1192 P1ND *r;
1193 struct attr *ap, *xap, *yap;
1194 union dimfun *d;
1195 TWORD t, q;
1196 int dsc, fsz;
1197 OFFSZ off;
1198 struct symtab *s;
1199
1200 /* make p->x */
1201 /* this is also used to reference automatic variables */
1202
1203 s = p->n_right->n_sp;
1204 p1nfree(p->n_right);
1205 r = p1nfree(p);
1206 #ifdef GCC_COMPAT
1207 xap = attr_find(r->n_ap, GCC_ATYP_PACKED);
1208 #else
1209 xap = NULL;
1210 #endif
1211
1212 p = pconvert(r);
1213
1214 /* make p look like ptr to x */
1215
1216 if (!ISPTR(p->n_type))
1217 p->n_type = PTR+UNIONTY;
1218
1219 t = INCREF(s->stype);
1220 q = INCQAL(s->squal);
1221 d = s->sdf;
1222 ap = s->sap;
1223 #ifdef GCC_COMPAT
1224 if ((yap = attr_find(ap, GCC_ATYP_PACKED)) != NULL)
1225 xap = yap;
1226 else if (xap != NULL)
1227 ap = attr_add(ap, attr_dup(xap));
1228 /* xap set if packed struct */
1229 #else
1230 yap = NULL;
1231 #endif
1232
1233 p = makety(p, t, q, d, ap);
1234
1235 /* compute the offset to be added */
1236
1237 off = s->soffset;
1238 dsc = s->sclass;
1239
1240 if (dsc & FIELD) {
1241 TWORD ftyp = s->stype;
1242 int fal = talign(ftyp, ap);
1243 fsz = dsc&FLDSIZ;
1244 off = (off/fal)*fal;
1245 p = offplus(p, off, t, q, d, ap);
1246 p = block(FLD, p, NULL, ftyp, 0, ap);
1247 p->n_qual = q;
1248 p->n_rval = PKFIELD(fsz, s->soffset%fal);
1249 /* make type int or some other signed type */
1250 if (fsz < SZINT)
1251 ftyp = INT;
1252 else if (fsz > SZINT && fsz < SZLONG && ftyp < LONG)
1253 ftyp = LONG;
1254 else if (fsz > SZLONG && fsz < SZLONGLONG && ftyp < LONGLONG)
1255 ftyp = LONGLONG;
1256 if (ftyp != p->n_type)
1257 p = makety(p, ftyp, 0, 0, 0);
1258 } else {
1259 p = offplus(p, off, t, q, d, ap);
1260 #ifndef CAN_UNALIGN
1261 /* if target cannot handle unaligned addresses, fix here */
1262 #endif
1263 }
1264
1265 p = clocal(p);
1266 return p;
1267 }
1268
1269 int
1270 notlval(register P1ND *p)
1271 {
1272 /* return 0 if p an lvalue, 1 otherwise */
1273
1274 again:
1275
1276 switch( p->n_op ){
1277
1278 case FLD:
1279 p = p->n_left;
1280 goto again;
1281
1282 case NAME:
1283 case OREG:
1284 case UMUL:
1285 if( ISARY(p->n_type) || ISFTN(p->n_type) ) return(1);
1286 /* FALLTHROUGH */
1287 case TEMP:
1288 case REG:
1289 return(0);
1290
1291 default:
1292 return(1);
1293 }
1294 }
1295
1296 /* make a constant node with value i */
1297 P1ND *
1298 bcon(int i)
1299 {
1300 return xbcon(i, NULL, INT);
1301 }
1302
1303 P1ND *
1304 xbcon(CONSZ val, struct symtab *sp, TWORD type)
1305 {
1306 P1ND *p;
1307
1308 p = block(ICON, NULL, NULL, type, 0, 0);
1309 slval(p, val);
1310 p->n_sp = sp;
1311 return clocal(p);
1312 }
1313
1314 P1ND *
1315 bpsize(P1ND *p)
1316 {
1317 int isdyn(struct symtab *sp);
1318 struct symtab s;
1319 P1ND *q, *r;
1320 TWORD t;
1321 int sz;
1322
1323 s.stype = DECREF(p->n_type);
1324 s.sdf = p->n_df;
1325 if (isdyn(&s)) {
1326 q = bcon(1);
1327 for (t = s.stype; t > BTMASK; t = DECREF(t)) {
1328 if (ISPTR(t))
1329 return buildtree(MUL, q, bcon(SZPOINT(t)));
1330 if (ISARY(t)) {
1331 if (s.sdf->ddim < 0)
1332 r = tempnode(-s.sdf->ddim, INT, 0, 0);
1333 else
1334 r = bcon(s.sdf->ddim/SZCHAR);
1335 q = buildtree(MUL, q, r);
1336 s.sdf++;
1337 }
1338 }
1339 sz = (int)tsize(t, s.sdf, p->n_ap);
1340 p = buildtree(MUL, q, bcon(sz/SZCHAR));
1341 } else
1342 p = (offcon(psize(p), p->n_type, p->n_df, p->n_ap));
1343 return p;
1344 }
1345
1346 /*
1347 * p is a node of type pointer; psize returns the
1348 * size of the thing pointed to
1349 */
1350 OFFSZ
1351 psize(P1ND *p)
1352 {
1353
1354 if (!ISPTR(p->n_type)) {
1355 uerror("pointer required");
1356 return(SZINT);
1357 }
1358 /* note: no pointers to fields */
1359 return(tsize(DECREF(p->n_type), p->n_df, p->n_ap));
1360 }
1361
1362 /*
1363 * convert an operand of p
1364 * f is either CVTL or CVTR
1365 * operand has type int, and is converted by the size of the other side
1366 * convert is called when an integer is to be added to a pointer, for
1367 * example in arrays or structures.
1368 */
1369 P1ND *
1370 convert(P1ND *p, int f)
1371 {
1372 union dimfun *df;
1373 TWORD ty, ty2;
1374 P1ND *q, *r, *s, *rv;
1375
1376 if (f == CVTL) {
1377 q = p->n_left;
1378 s = p->n_right;
1379 } else {
1380 q = p->n_right;
1381 s = p->n_left;
1382 }
1383 ty2 = ty = DECREF(s->n_type);
1384 while (ISARY(ty))
1385 ty = DECREF(ty);
1386
1387 r = offcon(tsize(ty, s->n_df, s->n_ap), s->n_type, s->n_df, s->n_ap);
1388 ty = ty2;
1389 rv = bcon(1);
1390 df = s->n_df;
1391 while (ISARY(ty)) {
1392 rv = buildtree(MUL, rv, df->ddim >= 0 ? bcon(df->ddim) :
1393 tempnode(-df->ddim, INT, 0, 0));
1394 df++;
1395 ty = DECREF(ty);
1396 }
1397 rv = clocal(MBLOCK(rv, r, INT, 0, 0));
1398 rv = optim(rv);
1399
1400 r = MBLOCK(q, rv, INT, 0, 0);
1401 r = clocal(r);
1402 /*
1403 * Indexing is only allowed with integer arguments, so insert
1404 * SCONV here if arg is not an integer.
1405 * XXX - complain?
1406 */
1407 if (r->n_type != INTPTR)
1408 r = clocal(makety(r, INTPTR, 0, 0, 0));
1409 if (f == CVTL)
1410 p->n_left = r;
1411 else
1412 p->n_right = r;
1413 return(p);
1414 }
1415
1416 P1ND *
1417 pconvert(register P1ND *p)
1418 {
1419 /* if p should be changed into a pointer, do so */
1420
1421 if( ISARY( p->n_type) ){
1422 p->n_type = DECREF( p->n_type );
1423 ++p->n_df;
1424 return( buildtree( ADDROF, p, NULL ) );
1425 }
1426 if( ISFTN( p->n_type) )
1427 return( buildtree( ADDROF, p, NULL ) );
1428
1429 return( p );
1430 }
1431
1432 P1ND *
1433 oconvert(register P1ND *p)
1434 {
1435 /* convert the result itself: used for pointer and unsigned */
1436
1437 switch(p->n_op) {
1438
1439 case LE:
1440 case LT:
1441 case GE:
1442 case GT:
1443 if(ISUNSIGNED(p->n_left->n_type) ||
1444 ISUNSIGNED(p->n_right->n_type) ||
1445 ISPTR(p->n_left->n_type) ||
1446 ISPTR(p->n_right->n_type))
1447 p->n_op += (ULE-LE);
1448 /* FALLTHROUGH */
1449 case EQ:
1450 case NE:
1451 return( p );
1452
1453 case MINUS:
1454 p->n_type = INTPTR;
1455 p->n_ap = NULL;
1456 p = (clocal(VBLOCK(p, bpsize(p->n_left), INT, 0, 0)));
1457 return( p );
1458 }
1459
1460 cerror( "illegal oconvert: %d", p->n_op );
1461
1462 return(p);
1463 }
1464
1465 /*
1466 * makes the operands of p agree; they are
1467 * either pointers or integers, by this time
1468 * with MINUS, the sizes must be the same
1469 * with COLON, the types must be the same
1470 */
1471 P1ND *
1472 ptmatch(P1ND *p)
1473 {
1474 struct attr *ap, *ap2;
1475 union dimfun *d, *d2;
1476 TWORD t1, t2, t, q1, q2, q;
1477 int o;
1478
1479 o = p->n_op;
1480 t = t1 = p->n_left->n_type;
1481 q = q1 = p->n_left->n_qual;
1482 t2 = p->n_right->n_type;
1483 q2 = p->n_right->n_qual;
1484 d = p->n_left->n_df;
1485 d2 = p->n_right->n_df;
1486 ap = p->n_left->n_ap;
1487 ap2 = p->n_right->n_ap;
1488
1489 switch( o ){
1490
1491 case ASSIGN:
1492 case RETURN:
1493 { break; }
1494
1495 case CAST:
1496 if (t == VOID) {
1497 /* just paint over */
1498 p->n_right = block(SCONV, p->n_right, NULL, VOID, 0, 0);
1499 return p;
1500 }
1501 break;
1502
1503 case MINUS: {
1504 int isdyn(struct symtab *sp);
1505 struct symtab s1, s2;
1506
1507 s1.stype = DECREF(t);
1508 s1.sdf = d;
1509 s2.stype = DECREF(t2);
1510 s2.sdf = d2;
1511 if (isdyn(&s1) || isdyn(&s2))
1512 ; /* We don't know */
1513 else if (psize(p->n_left) != psize(p->n_right))
1514 uerror("illegal pointer subtraction");
1515 break;
1516 }
1517
1518 case COLON:
1519 if (t1 != t2) {
1520 /*
1521 * Check for void pointer types. They are allowed
1522 * to cast to/from any pointers; 6.5.15 #6.
1523 * XXX qualified versions of void?
1524 */
1525 if (ISPTR(t1) && ISPTR(t2)) {
1526 if (BTYPE(t1) == VOID) {
1527 t = t2;
1528 d = d2;
1529 ap = ap2;
1530 break;
1531 }
1532 if (BTYPE(t2) == VOID)
1533 break;
1534 }
1535 uerror("illegal types in :");
1536 }
1537 break;
1538
1539 default: /* must work harder: relationals or comparisons */
1540
1541 if( !ISPTR(t1) ){
1542 t = t2;
1543 q = q2;
1544 d = d2;
1545 ap = ap2;
1546 break;
1547 }
1548 if( !ISPTR(t2) ){
1549 break;
1550 }
1551
1552 /* both are pointers */
1553 if( talign(t2,ap2) < talign(t,ap) ){
1554 t = t2;
1555 q = q2;
1556 ap = ap2;
1557 }
1558 break;
1559 }
1560
1561 p->n_left = makety( p->n_left, t, q, d, ap );
1562 p->n_right = makety( p->n_right, t, q, d, ap );
1563 if( o!=MINUS && !clogop(o) ){
1564
1565 p->n_type = t;
1566 p->n_qual = q;
1567 p->n_df = d;
1568 p->n_ap = ap;
1569 }
1570
1571 return(clocal(p));
1572 }
1573
1574 /*
1575 * Satisfy the types of various arithmetic binary ops.
1576 *
1577 * rules are:
1578 * if assignment, type of LHS
1579 * if any doubles, make double
1580 * else if any float make float
1581 * else if any longlongs, make long long
1582 * else if any longs, make long
1583 * else etcetc.
1584 *
1585 * If the op with the highest rank is unsigned, this is the resulting type.
1586 * See: 6.3.1.1 rank order equal of signed and unsigned types
1587 * 6.3.1.8 Usual arithmetic conversions
1588 */
1589 static P1ND *
1590 tymatch(P1ND *p)
1591 {
1592 TWORD tl, tr, t;
1593 P1ND *l, *r;
1594 int o;
1595
1596 o = p->n_op;
1597 r = p->n_right;
1598 l = p->n_left;
1599
1600 tl = l->n_type;
1601 tr = r->n_type;
1602
1603 if (tl == BOOL) tl = BOOL_TYPE;
1604 if (tr == BOOL) tr = BOOL_TYPE;
1605
1606 if (casgop(o)) {
1607 if (r->n_op != ICON && tl < FLOAT && tr < FLOAT &&
1608 tr > UNSIGNED &&
1609 DEUNSIGN(tl) < DEUNSIGN(tr) && o != CAST)
1610 warner(Wtruncate, tnames[tr], tnames[tl]);
1611 if (l->n_type == BOOL && r->n_type != BOOL) {
1612 /* must create a ?: */
1613 p->n_right = buildtree(QUEST, p->n_right,
1614 buildtree(COLON, bcon(1), bcon(0)));
1615 }
1616 p->n_right = makety(p->n_right, l->n_type, 0, 0, 0);
1617 t = p->n_type = l->n_type;
1618 p->n_ap = l->n_ap;
1619 } else {
1620 t = tl > tr ? tl : tr; /* MAX */
1621 /* This depends on ctype() called early */
1622 if (o != COLON && t < INT)
1623 t = INT;
1624 if (tl != t) p->n_left = makety(p->n_left, t, 0, 0, 0);
1625 if (tr != t) p->n_right = makety(p->n_right, t, 0, 0, 0);
1626 if (o == COLON && l->n_type == BOOL && r->n_type == BOOL)
1627 t = p->n_type = BOOL;
1628 else if (!clogop(o))
1629 p->n_type = t;
1630 }
1631 #ifdef PCC_DEBUG
1632 if (tdebug) {
1633 printf("tymatch(%p): ", p);
1634 tprint(tl, 0);
1635 printf(" %s ", copst(o));
1636 tprint(tr, 0);
1637 printf(" => ");
1638 tprint(t, 0);
1639 printf("\n");
1640 p1fwalk(p, eprint, 0);
1641 }
1642 #endif
1643 return p;
1644 }
1645
1646 /*
1647 * make p into type t by inserting a conversion
1648 */
1649 P1ND *
1650 makety(P1ND *p, TWORD t, TWORD q, union dimfun *d, struct attr *ap)
1651 {
1652
1653 if (t == p->n_type && t != FLOAT && t != DOUBLE) {
1654 p->n_df = d;
1655 p->n_ap = ap;
1656 p->n_qual = q;
1657 return(p);
1658 }
1659
1660 if (ISITY(t) || ISCTY(t) || ISITY(p->n_type) || ISCTY(p->n_type))
1661 cerror("makety");
1662
1663 if (concast(p, t))
1664 return clocal(p);
1665
1666 p = block(t & TMASK ? PCONV : SCONV, p, NULL, t, d, ap);
1667 p->n_qual = q;
1668 return clocal(p);
1669 }
1670
1671 P1ND *
1672 block(int o, P1ND *l, P1ND *r, TWORD t, union dimfun *d, struct attr *ap)
1673 {
1674 register P1ND *p;
1675
1676 p = p1alloc();
1677 p->n_rval = 0;
1678 p->n_op = o;
1679 slval(p, 0);
1680 p->n_left = l;
1681 p->n_right = r;
1682 p->n_type = t;
1683 p->n_qual = 0;
1684 p->n_df = d;
1685 p->n_ap = ap;
1686 return(p);
1687 }
1688
1689 /*
1690 * Return the constant value from an ICON.
1691 */
1692 CONSZ
1693 icons(P1ND *p)
1694 {
1695 /* if p is an integer constant, return its value */
1696 CONSZ val;
1697
1698 if (p->n_op != ICON || p->n_sp != NULL) {
1699 uerror( "constant expected");
1700 val = 1;
1701 } else
1702 val = glval(p);
1703 p1tfree(p);
1704 return(val);
1705 }
1706
1707 /*
1708 * the intent of this table is to examine the
1709 * operators, and to check them for
1710 * correctness.
1711 *
1712 * The table is searched for the op and the
1713 * modified type (where this is one of the
1714 * types INT (includes char and short), LONG,
1715 * DOUBLE (includes FLOAT), and POINTER
1716 *
1717 * The default action is to make the node type integer
1718 *
1719 * The actions taken include:
1720 * PUN check for puns
1721 * CVTL convert the left operand
1722 * CVTR convert the right operand
1723 * TYPL the type is determined by the left operand
1724 * TYPR the type is determined by the right operand
1725 * TYMATCH force type of left and right to match,by inserting conversions
1726 * PTMATCH like TYMATCH, but for pointers
1727 * LVAL left operand must be lval
1728 * CVTO convert the op
1729 * NCVT do not convert the operands
1730 * OTHER handled by code
1731 * NCVTR convert the left operand, not the right...
1732 *
1733 */
1734
1735 # define MINT 01 /* integer */
1736 # define MDBI 02 /* integer or double */
1737 # define MSTR 04 /* structure */
1738 # define MPTR 010 /* pointer */
1739 # define MPTI 020 /* pointer or integer */
1740
1741 int
1742 opact(P1ND *p)
1743 {
1744 int mt12, mt1, mt2, o;
1745
1746 mt1 = mt2 = mt12 = 0;
1747
1748 switch (coptype(o = p->n_op)) {
1749 case BITYPE:
1750 mt12=mt2 = moditype(p->n_right->n_type);
1751 /* FALLTHROUGH */
1752 case UTYPE:
1753 mt12 &= (mt1 = moditype(p->n_left->n_type));
1754 break;
1755 }
1756
1757 switch( o ){
1758
1759 case NAME :
1760 case ICON :
1761 case FCON :
1762 case CALL :
1763 case UCALL:
1764 case UMUL:
1765 { return( OTHER ); }
1766 case UMINUS:
1767 if( mt1 & MDBI ) return( TYPL+PROML );
1768 break;
1769
1770 case COMPL:
1771 if( mt1 & MINT ) return( TYPL+PROML );
1772 break;
1773
1774 case ADDROF:
1775 return( NCVT+OTHER );
1776 case NOT:
1777 return( PROML );
1778
1779 /* case INIT: */
1780 case CM:
1781 case CBRANCH:
1782 case ANDAND:
1783 case OROR:
1784 return( 0 );
1785
1786 case MUL:
1787 case DIV:
1788 if( mt12 & MDBI ) return( TYMATCH );
1789 break;
1790
1791 case MOD:
1792 case AND:
1793 case OR:
1794 case ER:
1795 if( mt12 & MINT ) return( TYMATCH );
1796 break;
1797
1798 case LS:
1799 case RS:
1800 if( mt12 & MINT ) return( TYPL+OTHER+PROML );
1801 break;
1802
1803 case EQ:
1804 case NE:
1805 case LT:
1806 case LE:
1807 case GT:
1808 case GE:
1809 if( mt12 & MDBI ) return( TYMATCH+CVTO );
1810 else if( mt12 & MPTR ) return( PTMATCH+PUN+CVTO );
1811 else if( mt12 & MPTI ) return( PTMATCH+PUN );
1812 else break;
1813
1814 case QUEST:
1815 return( TYPR+OTHER );
1816 case COMOP:
1817 return( TYPR );
1818
1819 case STREF:
1820 return( NCVTR+OTHER );
1821
1822 case FORCE:
1823 return( TYPL );
1824
1825 case COLON:
1826 if( mt12 & MDBI ) return( TYMATCH );
1827 else if( mt12 & MPTR ) return( TYPL+PTMATCH+PUN );
1828 else if( (mt1&MINT) && (mt2&MPTR) ) return( TYPR+PUN );
1829 else if( (mt1&MPTR) && (mt2&MINT) ) return( TYPL+PUN );
1830 else if( mt12 & MSTR ) return( NCVT+TYPL+OTHER );
1831 break;
1832
1833 case ASSIGN:
1834 case RETURN:
1835 if( mt12 & MSTR ) return( LVAL+NCVT+TYPL+OTHER );
1836 /* FALLTHROUGH */
1837 case CAST:
1838 if( mt12 & MDBI ) return( TYPL+LVAL+TYMATCH );
1839 else if( mt1 & MPTR) return( LVAL+PTMATCH+PUN );
1840 else if( mt12 & MPTI ) return( TYPL+LVAL+TYMATCH+PUN );
1841 break;
1842
1843 case MINUS:
1844 if (mt12 & MPTR)
1845 return(CVTO+PTMATCH+PUN);
1846 if (mt2 & MPTR)
1847 break;
1848 /* FALLTHROUGH */
1849 case PLUS:
1850 if (mt12 & MDBI)
1851 return(TYMATCH);
1852 else if ((mt1&MPTR) && (mt2&MINT))
1853 return(TYPL+CVTR);
1854 else if ((mt1&MINT) && (mt2&MPTR))
1855 return(TYPR+CVTL);
1856
1857 }
1858 uerror("operands of %s have incompatible types", copst(o));
1859 return(NCVT);
1860 }
1861
1862 int
1863 moditype(TWORD ty)
1864 {
1865 switch (ty) {
1866
1867 case STRTY:
1868 case UNIONTY:
1869 return( MSTR );
1870
1871 case BOOL:
1872 case CHAR:
1873 case SHORT:
1874 case UCHAR:
1875 case USHORT:
1876 case UNSIGNED:
1877 case ULONG:
1878 case ULONGLONG:
1879 case INT:
1880 case LONG:
1881 case LONGLONG:
1882 return( MINT|MDBI|MPTI );
1883 case FLOAT:
1884 case DOUBLE:
1885 case LDOUBLE:
1886 #ifndef NO_COMPLEX
1887 case FCOMPLEX:
1888 case COMPLEX:
1889 case LCOMPLEX:
1890 case FIMAG:
1891 case IMAG:
1892 case LIMAG:
1893 #endif
1894 return( MDBI );
1895 default:
1896 return( MPTR|MPTI );
1897
1898 }
1899 }
1900
1901 int tvaloff = MAXREGS+NPERMREG > 100 ? MAXREGS+NPERMREG + 100 : 100;
1902
1903 static int
1904 getn(TWORD t)
1905 {
1906 int n;
1907
1908 if (tvaloff == -NOOFFSET)
1909 tvaloff++; /* Skip this for array indexing */
1910 n = tvaloff;
1911 tvaloff += szty(t);
1912 return n;
1913 }
1914
1915 /*
1916 * Returns a TEMP node with temp number nr.
1917 * If nr == 0, return a node with a new number.
1918 */
1919 P1ND *
1920 tempnode(int nr, TWORD type, union dimfun *df, struct attr *ap)
1921 {
1922 P1ND *r;
1923
1924 r = block(TEMP, NULL, NULL, type, df, ap);
1925 regno(r) = nr ? nr : getn(type);
1926 return r;
1927 }
1928
1929 /*
1930 * Return a tempnode number for a symtab entry (if OK), else 0.
1931 */
1932 int
1933 tnodenr(struct symtab *sp)
1934 {
1935 if (xtemps && (sp->stype < STRTY || ISPTR(sp->stype)) &&
1936 !(cqual(sp->stype, sp->squal) & VOL) && cisreg(sp->stype)) {
1937 return getn(sp->stype);
1938 }
1939 return 0;
1940 }
1941
1942 /*
1943 * Do sizeof on p.
1944 */
1945 P1ND *
1946 doszof(P1ND *p)
1947 {
1948 union dimfun *df;
1949 TWORD ty;
1950 P1ND *rv, *q;
1951
1952 if (p->n_op == FLD)
1953 uerror("can't apply sizeof to bit-field");
1954
1955 /*
1956 * Arrays may be dynamic, may need to make computations.
1957 */
1958
1959 rv = bcon(1);
1960 df = p->n_df;
1961 ty = p->n_type;
1962 while (ISARY(ty)) {
1963 if (df->ddim == NOOFFSET)
1964 uerror("sizeof of incomplete type");
1965 if (df->ddim < 0) {
1966 q = tempnode(-df->ddim, INT, 0, 0);
1967 } else
1968 q = bcon(df->ddim);
1969 rv = buildtree(MUL, rv, q);
1970 df++;
1971 ty = DECREF(ty);
1972 }
1973 rv = buildtree(MUL, rv,
1974 xbcon(tsize(ty, p->n_df, p->n_ap)/SZCHAR, NULL, INTPTR));
1975 p1tfree(p);
1976 return rv;
1977 }
1978
1979 #ifdef PCC_DEBUG
1980 void
1981 eprint(P1ND *p, int down, int *a, int *b)
1982 {
1983 int ty;
1984
1985 *a = *b = down+1;
1986 while( down > 1 ){
1987 printf( "\t" );
1988 down -= 2;
1989 }
1990 if( down ) printf( " " );
1991
1992 ty = coptype( p->n_op );
1993
1994 printf("%p) %s, ", p, copst(p->n_op));
1995 if (p->n_op == XARG || p->n_op == XASM)
1996 printf("id '%s', ", p->n_name);
1997 if (ty == LTYPE) {
1998 printf(CONFMT, glval(p));
1999 if (p->n_op == NAME || p->n_op == ICON)
2000 printf(", %p, ", p->n_sp);
2001 #ifdef notyet
2002 else if (p->n_op == FCON)
2003 printf(", %Lf, ", p->n_dcon->fp);
2004 #endif
2005 else
2006 printf(", %d, ", p->n_rval);
2007 }
2008 tprint(p->n_type, p->n_qual);
2009 printf( ", %p, ", p->n_df);
2010 #ifdef GCC_COMPAT
2011 dump_attr(p->n_ap);
2012 #endif
2013 }
2014 # endif
2015
2016 /*
2017 * Emit everything that should be emitted on the left side
2018 * of a comma operator, and remove the operator.
2019 * Do not traverse through QUEST, ANDAND and OROR.
2020 * Enable this for all targets when stable enough.
2021 */
2022 static void
2023 comops(P1ND *p)
2024 {
2025 int o;
2026 P1ND *q;
2027
2028 while (p->n_op == COMOP) {
2029 /* XXX hack for GCC ({ }) ops */
2030 if (p->n_left->n_op == GOTO) {
2031 int v = (int)glval(p->n_left->n_left);
2032 ecomp(p->n_left);
2033 plabel(v+2);
2034 } else
2035 ecomp(p->n_left); /* will recurse if more COMOPs */
2036 q = p->n_right;
2037 *p = *q;
2038 p1nfree(q);
2039 }
2040 o = coptype(p->n_op);
2041 if (p->n_op == QUEST || p->n_op == ANDAND || p->n_op == OROR)
2042 o = UTYPE;
2043 if (o != LTYPE)
2044 comops(p->n_left);
2045 if (o == BITYPE)
2046 comops(p->n_right);
2047 }
2048
2049 /*
2050 * Walk up through the tree from the leaves,
2051 * removing constant operators.
2052 */
2053 static void
2054 logwalk(P1ND *p)
2055 {
2056 int o = coptype(p->n_op);
2057 P1ND *l, *r;
2058
2059 l = p->n_left;
2060 r = p->n_right;
2061 switch (o) {
2062 case LTYPE:
2063 return;
2064 case BITYPE:
2065 logwalk(r);
2066 /* FALLTHROUGH */
2067 case UTYPE:
2068 logwalk(l);
2069 }
2070 if (!clogop(p->n_op))
2071 return;
2072 if (p->n_op == NOT && l->n_op == ICON) {
2073 slval(p, (glval(l) == 0));
2074 p1nfree(l);
2075 p->n_op = ICON;
2076 }
2077 if (l->n_op == ICON && r->n_op == ICON) {
2078 if (conval(l, p->n_op, r) == 0) {
2079 /*
2080 * people sometimes tend to do really odd compares,
2081 * like "if ("abc" == "def")" etc.
2082 * do it runtime instead.
2083 */
2084 } else {
2085 slval(p, glval(l));
2086 p->n_sp = NULL;
2087 p->n_op = ICON;
2088 p1nfree(l);
2089 p1nfree(r);
2090 }
2091 }
2092 }
2093
2094 /*
2095 * Removes redundant logical operators for branch conditions.
2096 */
2097 static void
2098 fixbranch(P1ND *p, int label)
2099 {
2100
2101 logwalk(p);
2102
2103 if (p->n_op == ICON) {
2104 if (glval(p) != 0 || p->n_sp != NULL)
2105 branch(label);
2106 p1nfree(p);
2107 } else {
2108 if (!clogop(p->n_op)) /* Always conditional */
2109 p = buildtree(NE, p, bcon(0));
2110 ecode(buildtree(CBRANCH, p, bcon(label)));
2111 }
2112 }
2113
2114 /*
2115 * Write out logical expressions as branches.
2116 */
2117 static void
•2118 andorbr(P1ND *p, int true_, int false_)
2119 {
2120 P1ND *q;
2121 int o, lab;
2122
2123 lab = -1;
2124 switch (o = p->n_op) {
2125 case EQ:
2126 case NE:
2127 /*
2128 * Remove redundant EQ/NE nodes.
2129 */
2130 while (((o = p->n_left->n_op) == EQ || o == NE) &&
2131 p->n_right->n_op == ICON) {
2132 o = p->n_op;
2133 q = p->n_left;
2134 if (glval(p->n_right) == 0) {
2135 p1nfree(p->n_right);
2136 *p = *q;
2137 p1nfree(q);
2138 if (o == EQ)
2139 p->n_op = p1negrel[p->n_op - EQ];
2140 #if 0
2141 p->n_op = NE; /* toggla */
2142 #endif
2143 } else if (glval(p->n_right) == 1) {
2144 p1nfree(p->n_right);
2145 *p = *q;
2146 p1nfree(q);
2147 if (o == NE)
2148 p->n_op = p1negrel[p->n_op - EQ];
2149 #if 0
2150 p->n_op = EQ; /* toggla */
2151 #endif
2152 } else
2153 break; /* XXX - should always be false */
2154
2155 }
2156 /* FALLTHROUGH */
2157 case LE:
2158 case LT:
2159 case GE:
2160 case GT:
•2161 calc: if (true_ < 0) {
2162 p->n_op = p1negrel[p->n_op - EQ];
2163 p->n_ap = attr_add(p->n_ap,
2164 attr_new(ATTR_FP_SWAPPED, 3));
2165 p->n_ap->aa[0].iarg = 1;
•2166 true_ = false_;
2167 false_ = -1;
2168 }
2169
2170 rmcops(p->n_left);
2171 rmcops(p->n_right);
•2172 fixbranch(p, true_);
2173 if (false_ >= 0)
2174 branch(false_);
2175 break;
2176
2177 case ULE:
2178 case UGT:
2179 /* Convert to friendlier ops */
2180 if (nncon(p->n_right) && glval(p->n_right) == 0)
2181 p->n_op = o == ULE ? EQ : NE;
2182 goto calc;
2183
2184 case UGE:
2185 case ULT:
2186 /* Already true/false by definition */
2187 if (nncon(p->n_right) && glval(p->n_right) == 0) {
•2188 if (true_ < 0) {
2189 o = o == ULT ? UGE : ULT;
•2190 true_ = false_;
2191 }
2192 rmcops(p->n_left);
2193 ecode(p->n_left);
2194 rmcops(p->n_right);
2195 ecode(p->n_right);
2196 p1nfree(p);
2197 if (o == UGE) /* true */
•2198 branch(true_);
2199 break;
2200 }
2201 goto calc;
2202
2203 case ANDAND:
•2204 lab = false_<0 ? getlab() : false_ ;
2205 andorbr(p->n_left, -1, lab);
2206 comops(p->n_right);
•2207 andorbr(p->n_right, true_, false_);
2208 if (false_ < 0)
2209 plabel( lab);
2210 p1nfree(p);
2211 break;
2212
2213 case OROR:
•2214 lab = true_<0 ? getlab() : true_;
2215 andorbr(p->n_left, lab, -1);
2216 comops(p->n_right);
•2217 andorbr(p->n_right, true_, false_);
2218 if (true_ < 0)
2219 plabel( lab);
2220 p1nfree(p);
2221 break;
2222
2223 case NOT:
•2224 andorbr(p->n_left, false_, true_);
2225 p1nfree(p);
2226 break;
2227
2228 default:
2229 rmcops(p);
•2230 if (true_ >= 0)
2231 fixbranch(p, true_);
2232 if (false_ >= 0) {
2233 if (true_ >= 0)
2234 branch(false_);
2235 else
•2236 fixbranch(buildtree(EQ, p, bcon(0)), false_);
2237 }
2238 }
2239 }
2240
2241 /*
2242 * Create a node for either TEMP or on-stack storage.
2243 */
2244 P1ND *
2245 cstknode(TWORD t, union dimfun *df, struct attr *ap)
2246 {
2247 struct symtab *sp;
2248 int i;
2249
2250 /* create a symtab entry suitable for this type */
2251 sp = getsymtab("0hej", SSTMT);
2252 sp->stype = t;
2253 sp->squal = 0;
2254 sp->sdf = df;
2255 sp->sap = ap;
2256 sp->sclass = AUTO;
2257 sp->soffset = NOOFFSET;
2258 if ((i = tnodenr(sp)) != 0) {
2259 sp->soffset = i;
2260 sp->sflags |= STNODE;
2261 } else
2262 oalloc(sp, &autooff);
2263 return nametree(sp);
2264
2265 }
2266
2267 /*
2268 * Massage the output trees to remove C-specific nodes:
2269 * COMOPs are split into separate statements.
2270 * QUEST/COLON are rewritten to branches.
2271 * ANDAND/OROR/NOT are rewritten to branches for lazy-evaluation.
2272 * CBRANCH conditions are rewritten for lazy-evaluation.
2273 */
2274 static void
2275 rmcops(P1ND *p)
2276 {
2277 TWORD type;
2278 P1ND *q, *r, *tval;
2279 int o, ty, lbl, lbl2;
2280
2281 tval = NULL;
2282 o = p->n_op;
2283 ty = coptype(o);
2284 if (BTYPE(p->n_type) == ENUMTY) { /* fixup enum */
2285 struct symtab *sp = strmemb(p->n_ap);
2286 MODTYPE(p->n_type, sp->stype);
2287 /*
2288 * XXX may fail if these are true:
2289 * - variable-sized enums
2290 * - non-byte-addressed targets.
2291 */
2292 if (BTYPE(p->n_type) == ENUMTY && ISPTR(p->n_type))
2293 MODTYPE(p->n_type, INT); /* INT ok? */
2294 }
2295 switch (o) {
2296 case QUEST:
2297
2298 /*
2299 * Create a branch node from ?:
2300 * || and && must be taken special care of.
2301 */
2302 type = p->n_type;
2303 andorbr(p->n_left, -1, lbl = getlab());
2304
2305 /* Make ASSIGN node */
2306 /* Only if type is not void */
2307 q = p->n_right->n_left;
2308 comops(q);
2309 if (type != VOID) {
2310 tval = cstknode(q->n_type, q->n_df, q->n_ap);
2311 q = buildtree(ASSIGN, p1tcopy(tval), q);
2312 }
2313 rmcops(q);
2314 ecode(q); /* Done with assign */
2315 branch(lbl2 = getlab());
2316 plabel( lbl);
2317
2318 q = p->n_right->n_right;
2319 comops(q);
2320 if (type != VOID) {
2321 q = buildtree(ASSIGN, p1tcopy(tval), q);
2322 }
2323 rmcops(q);
2324 ecode(q); /* Done with assign */
2325
2326 plabel( lbl2);
2327
2328 p1nfree(p->n_right);
2329 if (p->n_type != VOID) {
2330 *p = *tval;
2331 p1nfree(tval);
2332 } else {
2333 p->n_op = ICON;
2334 slval(p, 0);
2335 p->n_sp = NULL;
2336 }
2337 break;
2338
2339 case ULE:
2340 case ULT:
2341 case UGE:
2342 case UGT:
2343 case EQ:
2344 case NE:
2345 case LE:
2346 case LT:
2347 case GE:
2348 case GT:
2349 case ANDAND:
2350 case OROR:
2351 case NOT:
2352 #ifdef SPECIAL_CCODES
2353 #error fix for private CCODES handling
2354 #else
2355 r = p1alloc();
2356 *r = *p;
2357 andorbr(r, -1, lbl = getlab());
2358
2359 tval = cstknode(p->n_type, p->n_df, p->n_ap);
2360
2361 ecode(buildtree(ASSIGN, p1tcopy(tval), bcon(1)));
2362 branch(lbl2 = getlab());
2363 plabel( lbl);
2364 ecode(buildtree(ASSIGN, p1tcopy(tval), bcon(0)));
2365 plabel( lbl2);
2366
2367 *p = *tval;
2368 p1nfree(tval);
2369
2370 #endif
2371 break;
2372 case CBRANCH:
2373 andorbr(p->n_left, glval(p->n_right), -1);
2374 p1nfree(p->n_right);
2375 p->n_op = ICON; p->n_type = VOID;
2376 break;
2377 case COMOP:
2378 cerror("COMOP error");
2379
2380 default:
2381 if (ty == LTYPE)
2382 return;
2383 rmcops(p->n_left);
2384 if (ty == BITYPE)
2385 rmcops(p->n_right);
2386 }
2387 }
2388
2389 /*
2390 * Return 1 if an assignment is found.
2391 */
2392 static int
2393 has_se(P1ND *p)
2394 {
2395 if (p->n_op == COMOP && p->n_left->n_op == GOTO)
2396 return 1;
2397 if (cdope(p->n_op) & ASGFLG)
2398 return 1;
2399 if (coptype(p->n_op) == LTYPE)
2400 return 0;
2401 if (has_se(p->n_left))
2402 return 1;
2403 if (coptype(p->n_op) == BITYPE)
2404 return has_se(p->n_right);
2405 return 0;
2406 }
2407
2408 #ifndef FIELDOPS
2409
2410 /* avoid promotion to int */
2411 #define TYPMOD(o, p, n, t) clocal(block(o, p, n, t, 0, 0))
2412 #define TYPLS(p, n, t) TYPMOD(LS, p, n, t)
2413 #define TYPRS(p, n, t) TYPMOD(RS, p, n, t)
2414 #define TYPOR(p, q, t) TYPMOD(OR, p, q, t)
2415 #define TYPAND(p, q, t) TYPMOD(AND, p, q, t)
2416
2417 /*
2418 * Read an unaligned bitfield from position pointed to by p starting at
2419 * off and size fsz and return a tree of type t with resulting data.
2420 * ct is the type we must use to read data.
2421 */
2422 static P1ND *
2423 rdualfld(P1ND *p, TWORD t, TWORD ct, int off, int fsz)
2424 {
2425 int t2f, inbits, tsz, ctsz;
2426 P1ND *q, *r;
2427
2428 ct = ENUNSIGN(ct);
2429 ctsz = (int)tsize(ct, 0, 0);
2430
2431 /* traverse until first data byte */
2432 for (t2f = 0; off >= ctsz; t2f++, off -= ctsz)
2433 ;
2434 #ifdef UNALIGNED_ACCESS
2435 /* try to squeeze it into an int */
2436 if (off + fsz > ctsz && off + fsz <= SZINT) {
2437 ct = UNSIGNED;
2438 ctsz = SZINT;
2439 }
2440 #endif
2441 p = makety(p, PTR|ct, 0, 0, 0);
2442 if (off + fsz <= ctsz) {
2443 /* only one operation needed */
2444 q = buildtree(UMUL, buildtree(PLUS, p, bcon(t2f)), 0);
2445 q = makety(q, t, 0, 0, 0);
2446 ctsz = tsize(t, 0, 0);
2447 q = buildtree(LS, q, bcon(ctsz-fsz-off));
2448 q = buildtree(RS, q, bcon(ctsz-fsz));
2449 } else {
2450 q = buildtree(UMUL, buildtree(PLUS, p1tcopy(p), bcon(t2f)), 0);
2451 q = makety(TYPRS(q, bcon(off), ct), t, 0, 0, 0);
2452 inbits = ctsz - off;
2453 t2f++;
2454
2455 while (fsz > inbits) {
2456 r = buildtree(UMUL,
2457 buildtree(PLUS, p1tcopy(p), bcon(t2f)), 0);
2458 r = makety(r, t, 0, 0, 0);
2459 r = TYPLS(r, bcon(inbits), t);
2460 q = TYPOR(q, r, t);
2461 inbits += ctsz;
2462 t2f++;
2463 }
2464 /* sign/zero extend XXX - RS must sign extend */
2465 tsz = (int)tsize(t, 0, 0);
2466 if (!ISUNSIGNED(t)) {
2467 t = DEUNSIGN(t);
2468 q = makety(q, t, 0, 0, 0);
2469 }
2470 q = TYPLS(q, bcon(tsz-fsz), t);
2471 q = TYPRS(q, bcon(tsz-fsz), t);
2472 p1tfree(p);
2473 }
2474
2475 return q;
2476 }
2477
2478 /*
2479 * Write val to a (unaligned) bitfield with length fsz positioned off bits
2480 * from d. Bitfield type is t, and type to use when writing is ct.
2481 * neither f nor d should have any side effects if copied.
2482 * Multiples of ct are supposed to be written without problems.
2483 * Both val and d are free'd after use.
2484 */
2485 static P1ND *
2486 wrualfld(P1ND *val, P1ND *d, TWORD t, TWORD ct, int off, int fsz)
2487 {
2488 P1ND *p, *q, *r, *rn, *s;
2489 int ctsz, t2f, inbits;
2490
2491 ctsz = (int)tsize(ct, 0, 0);
2492
2493 ct = ENUNSIGN(ct);
2494 d = makety(d, PTR|ct, 0, 0, 0);
2495
2496 for (t2f = 0; off >= ctsz; t2f++, off -= ctsz)
2497 ;
2498
2499 if (off + fsz <= ctsz) {
2500 r = tempnode(0, ct, 0, 0);
2501
2502 /* only one operation needed */
2503 d = buildtree(UMUL, buildtree(PLUS, d, bcon(t2f)), 0);
2504 p = p1tcopy(d);
2505 p = TYPAND(p, xbcon(~(SZMASK(fsz) << off), 0, ct), ct);
2506
2507 val = makety(val, ct, 0, 0, 0);
2508 q = TYPAND(val, xbcon(SZMASK(fsz), 0, ct), ct);
2509 q = buildtree(ASSIGN, p1tcopy(r), q);
2510
2511 q = TYPLS(q, bcon(off), ct);
2512 p = TYPOR(p, q, ct);
2513 p = makety(p, t, 0, 0, 0);
2514 rn = buildtree(ASSIGN, d, p);
2515 rn = buildtree(COMOP, rn, makety(r, t, 0, 0, 0));
2516 } else {
2517 s = makety(p1tcopy(val), t, 0, 0, 0);
2518 s = TYPAND(s, xbcon(SZMASK(fsz), 0, t), t);
2519
2520 r = buildtree(UMUL, buildtree(PLUS, p1tcopy(d), bcon(t2f)), 0);
2521 p = p1tcopy(r);
2522 p = TYPAND(p, xbcon(SZMASK(off), 0, ct), ct);
2523 q = p1tcopy(val);
2524 q = TYPLS(q, bcon(off), t);
2525 q = makety(q, ct, 0, 0, 0);
2526 p = TYPOR(p, q, ct);
2527 rn = buildtree(ASSIGN, r, p);
2528 inbits = ctsz - off;
2529 t2f++;
2530
2531 while (fsz > inbits+ctsz) {
2532 r = buildtree(UMUL,
2533 buildtree(PLUS, p1tcopy(d), bcon(t2f)), 0);
2534 q = p1tcopy(val);
2535 q = TYPRS(q, bcon(inbits), t);
2536 q = makety(q, ct, 0, 0, 0);
2537 rn = buildtree(COMOP, rn, buildtree(ASSIGN, r, q));
2538 t2f++;
2539 inbits += ctsz;
2540 }
2541
2542 r = buildtree(UMUL, buildtree(PLUS, d, bcon(t2f)), 0);
2543 p = p1tcopy(r);
2544 p = TYPAND(p, makety(xbcon(~SZMASK(fsz-inbits), 0, ct),
2545 ct, 0, 0, 0), ct);
2546 q = TYPRS(val, bcon(inbits), t);
2547 q = TYPAND(q, xbcon(SZMASK(fsz-inbits), 0, t), t);
2548 q = makety(q, ct, 0, 0, 0);
2549 p = TYPOR(p, q, ct);
2550 rn = buildtree(COMOP, rn, buildtree(ASSIGN, r, p));
2551 rn = buildtree(COMOP, rn, s);
2552 }
2553 return rn;
2554 }
2555
2556 /*
2557 * Rewrite bitfield operations to shifts.
2558 */
2559 static P1ND *
2560 rmfldops(P1ND *p)
2561 {
2562 TWORD t, ct;
2563 P1ND *q, *r, *t1, *t2, *bt;
2564 int fsz, foff;
2565
2566 if (p->n_op == FLD) {
2567 /* Rewrite a field read operation */
2568 fsz = UPKFSZ(p->n_rval);
2569 foff = UPKFOFF(p->n_rval);
2570 q = buildtree(ADDROF, p->n_left, NULL);
2571
2572 ct = t = p->n_type;
2573 if (t < INT) {
2574 t = INT;
2575 if (ISUNSIGNED(p->n_type))
2576 t++;
2577 }
2578
2579 #ifdef GCC_COMPAT
2580 if (attr_find(p->n_ap, GCC_ATYP_PACKED) &&
2581 coptype(q->n_op) != LTYPE) {
2582 t1 = tempnode(0, q->n_type, 0, 0);
2583 bt = buildtree(ASSIGN, p1tcopy(t1), q);
2584 q = t1;
2585 #ifndef UNALIGNED_ACCESS
2586 ct = UCHAR;
2587 #endif
2588 } else
2589 #endif
2590 bt = bcon(0);
2591 #if TARGET_ENDIAN == TARGET_BE
2592 foff = (int)tsize(t, 0, 0) - fsz - foff;
2593 #endif
2594 q = rdualfld(q, t, ct, foff, fsz);
2595 if (fsz < SZINT)
2596 q = makety(q, INT, 0, 0, 0);
2597 if (p->n_type != INT)
2598 q = makety(q, p->n_type, 0, 0, 0);
2599 p->n_left = bt;
2600 p->n_right = q;
2601 p->n_op = COMOP;
2602 } else if (((cdope(p->n_op)&ASGOPFLG) || p->n_op == ASSIGN ||
2603 p->n_op == INCR || p->n_op == DECR) && p->n_left->n_op == FLD) {
2604 /*
2605 * Rewrite a field write operation
2606 * More difficult than a read op since we must care
2607 * about side effects.
2608 */
2609 q = p->n_left;
2610 fsz = UPKFSZ(q->n_rval);
2611 foff = UPKFOFF(q->n_rval);
2612 t = q->n_left->n_type;
2613 #if TARGET_ENDIAN == TARGET_BE
2614 foff = (int)tsize(t, 0, 0) - fsz - foff;
2615 #endif
2616 bt = NULL;
2617 if (p->n_right->n_op != ICON && p->n_right->n_op != NAME) {
2618 t2 = tempnode(0, p->n_right->n_type, 0, 0);
2619 bt = buildtree(ASSIGN, p1tcopy(t2), p->n_right);
2620 } else
2621 t2 = p->n_right;
2622
2623 ct = t;
2624 #ifdef GCC_COMPAT
2625 #ifndef UNALIGNED_ACCESS
2626 if (attr_find(q->n_ap, GCC_ATYP_PACKED))
2627 ct = UCHAR;
2628 #endif
2629 #endif
2630 /* t2 is what we have to write (RHS of ASSIGN) */
2631 /* bt is (eventually) something that must be written */
2632
2633 if (q->n_left->n_op == UMUL) {
2634 /* LHS of assignment may have side effects */
2635 q = q->n_left;
2636 t1 = tempnode(0, q->n_left->n_type, 0, 0);
2637 r = buildtree(ASSIGN, p1tcopy(t1), q->n_left);
2638
2639 bt = bt ? block(COMOP, bt, r, INT, 0, 0) : r;
2640 q->n_left = t1;
2641 }
2642 t1 = buildtree(ADDROF, p->n_left->n_left, 0);
2643
2644 /* t1 is lval where to write (and read) */
2645
2646 if (p->n_op == ASSIGN) {
2647 q = wrualfld(t2, t1, t, ct, foff, fsz);
2648 if (bt)
2649 q = block(COMOP, bt, q, t, 0, 0);
2650 p1nfree(p->n_left);
2651 p->n_left = bcon(0);
2652 p->n_right = q;
2653 p->n_op = COMOP;
2654 } else
2655 cerror("NOTASSIGN!");
2656
2657 t = p->n_type;
2658 if (ISUNSIGNED(p->n_type)) {
2659 /* mask away unwanted bits */
2660 if ((t == LONGLONG && fsz == SZLONGLONG-1) ||
2661 (t == LONG && fsz == SZLONG-1) ||
2662 (t == INT && fsz == SZINT-1))
2663 p = buildtree(AND, p,
2664 xbcon((1LL << fsz)-1, 0, t));
2665 } else {
2666 /* Correct value in case of signed bitfield. */
2667 if (t == LONGLONG)
2668 fsz = SZLONGLONG - fsz;
2669 else if (t == LONG)
2670 fsz = SZLONG - fsz;
2671 else
2672 fsz = SZINT - fsz;
2673 p = buildtree(LS, p, bcon(fsz));
2674 #ifdef RS_DIVIDES
2675 p = buildtree(RS, p, bcon(fsz));
2676 #else
2677 {
2678 p = buildtree(DIV, p, xbcon(1LL << fsz, 0, t));
2679 /* avoid wrong sign if divisor gets negative */
2680 if ((t == LONGLONG && fsz == SZLONGLONG-1) ||
2681 (t == LONG && fsz == SZLONG-1) ||
2682 (t == INT && fsz == SZINT-1))
2683 p = buildtree(UMINUS, p, NULL);
2684 }
2685 #endif
2686 }
2687 }
2688 if (coptype(p->n_op) != LTYPE)
2689 p->n_left = rmfldops(p->n_left);
2690 if (coptype(p->n_op) == BITYPE)
2691 p->n_right = rmfldops(p->n_right);
2692 return p;
2693 }
2694 #endif
2695
2696 void
2697 ecomp(P1ND *p)
2698 {
2699
2700 #ifdef PCC_DEBUG
2701 if (edebug) {
2702 printf("ecomp\n");
2703 p1fwalk(p, eprint, 0);
2704 }
2705 #endif
2706 if (!reached) {
2707 warner(Wunreachable_code);
2708 reached = 1;
2709 }
2710 p = optim(p);
2711 #ifndef FIELDOPS
2712 p = rmfldops(p);
2713 #endif
2714 comops(p);
2715 rmcops(p);
2716 if (p->n_op == ICON && p->n_type == VOID)
2717 p1tfree(p);
2718 else
2719 ecode(p);
2720 }
2721
2722
2723 #ifdef PASS1
2724 /*
2725 * Print out full tree.
2726 * Nodes are already converted to pass2 style.
2727 */
2728 static void
2729 p2print(NODE *p)
2730 {
2731 struct attr *ap;
2732 int ty, i;
2733
2734 ty = optype(p->n_op);
2735
2736 printf("\" %d ", p->n_op);
2737
2738 printf("%d %d ", p->n_type, p->n_qual);
2739 if (ty == LTYPE)
2740 printf(CONFMT " ", glval(p));
2741 if (ty != BITYPE) {
2742 if (p->n_op != NAME && p->n_op != ICON)
2743 printf("%d ", p->n_rval);
2744 }
2745
2746 /* handle special cases */
2747 if (p->n_op == NAME || p->n_op == ICON ||
2748 p->n_op == XASM || p->n_op == XARG)
2749 printf("%s", p->n_name);
2750
2751 if (p->n_ap) {
2752 printf(" + ");
2753 for (ap = p->n_ap; ap; ap = ap->next) {
2754 printf("%d %d ", ap->atype, ap->sz);
2755 for (i = 0; i < ap->sz; i++)
2756 printf("%d ", ap->iarg(i));
2757 }
2758 }
2759 printf("\n");
2760
2761 if (ty != LTYPE)
2762 p2print(p->n_left);
2763 if (ty == BITYPE)
2764 p2print(p->n_right);
2765 }
2766
2767 /*
2768 * Print out the code trees for pass2.
2769 * First on line is always a sync char, second is space:
2770 * ! - Prologue.
2771 * " - Node
2772 * ^ - Label
2773 * $ - Assembler statement
2774 * % - Epilog.
2775 * # - Line number
2776 * & - File name
2777 * * - Passthrough line.
2778 */
2779 void
2780 pass2_compile(struct interpass *ip)
2781 {
2782 struct interpass_prolog *ipp;
2783 static int oldlineno;
2784 int i;
2785
2786 if (oldlineno != ip->lineno)
2787 printf("# %d\n", oldlineno = ip->lineno);
2788
2789 switch (ip->type) {
2790 case IP_PROLOG:
2791 ipp = (struct interpass_prolog *)ip;
2792 printf("! %d %d %d %d %d %s\n",
2793 ipp->ipp_type, ipp->ipp_vis, ip->ip_lbl, ipp->ip_tmpnum,
2794 ipp->ip_lblnum, ipp->ipp_name);
2795 #ifdef TARGET_IPP_MEMBERS
2796 printf("( ");
2797 target_members_print_prolog(ipp);
2798 printf("\n");
2799 #endif
2800 break;
2801 case IP_NODE:
2802 p2print(ip->ip_node);
2803 tfree(ip->ip_node);
2804 break;
2805 case IP_DEFLAB:
2806 printf("^ %d\n", ip->ip_lbl);
2807 break;
2808 case IP_ASM:
2809 {
2810 const char *s = ip->ip_asm;
2811 printf("$ ");
2812 while (*s != '\0') {
2813 putchar(*s);
2814 if (*s++ == '\n')
2815 printf("$ ");
2816 }
2817 printf("\n");
2818 }
2819 break;
2820 case IP_EPILOG:
2821 ipp = (struct interpass_prolog *)ip;
2822 printf("%% %d %d %d %d %s",
2823 ipp->ipp_autos, ip->ip_lbl, ipp->ip_tmpnum,
2824 ipp->ip_lblnum, ipp->ipp_name);
2825 if (ipp->ip_labels[0]) {
2826 for (i = 0; ipp->ip_labels[i]; i++)
2827 ;
2828 printf(" + %d", i);
2829 for (i = 0; ipp->ip_labels[i]; i++)
2830 printf(" %d", ipp->ip_labels[i]);
2831 }
2832 printf("\n");
2833 #ifdef TARGET_IPP_MEMBERS
2834 printf(") ");
2835 target_members_print_epilog(ipp);
2836 printf("\n");
2837 #endif
2838 break;
2839 default:
2840 cerror("Missing %d", ip->type);
2841 }
2842 free(ip);
2843 }
2844 #endif
2845
2846 static char *
2847 sptostr(struct symtab *sp)
2848 {
2849 char *cp = tmpalloc(32);
2850 int n = sp->soffset;
2851 if (n < 0)
2852 n = -n;
2853 snprintf(cp, 32, LABFMT, n);
2854 return cp;
2855 }
2856
2857 static NODE *
2858 p2tree(P1ND *p)
2859 {
2860 struct attr *ap;
2861 struct symtab *q;
2862 NODE *np;
2863 int ty;
2864
2865 myp2tree(p); /* local action can be taken here */
2866
2867 /* Fix left imaginary types */
2868 if (ISITY(BTYPE(p->n_type)))
2869 MODTYPE(p->n_type, p->n_type - (FIMAG-FLOAT));
2870
2871 ty = coptype(p->n_op);
2872 np = memset(talloc(), 0, sizeof(NODE));
2873
2874 /* Copy common data */
2875 np->n_op = p->n_op;
2876 np->n_type = p->n_type;
2877 np->n_qual = p->n_qual;
2878 if (ty != BITYPE)
2879 np->n_rval = p->n_rval;
2880 if (ty == LTYPE) {
2881 setlval(np, glval(p));
2882 }
2883
2884 /* cleanup attributes.
2885 * copy those that are supposed to go into pass2 */
2886 for (ap = p->n_ap; ap; ap = ap->next)
2887 if (ap->atype < ATTR_MI_MAX)
2888 np->n_ap = attr_add(np->n_ap, attr_dup(ap));
2889
2890 switch( p->n_op ){
2891 case NAME:
2892 case ICON:
2893 if ((q = p->n_sp) != NULL) {
2894 if ((q->sclass == STATIC && q->slevel > 0)
2895 #ifdef GCC_COMPAT
2896 || q->sflags == SLBLNAME
2897 #endif
2898 ) {
2899 np->n_name = sptostr(q);
2900 if ((q->sflags & SMASK) == SSTRING)
2901 q->sflags |= SASG;
2902 } else
2903 np->n_name = getexname(q);
2904 } else
2905 np->n_name = "";
2906 break;
2907
2908 case STASG:
2909 case STARG:
2910 case STCALL:
2911 case USTCALL:
2912 ap = attr_new(ATTR_P2STRUCT, 2);
2913 np->n_ap = attr_add(np->n_ap, ap);
2914 /* STASG used for stack array init */
2915 if (p->n_op == STASG && ISARY(p->n_type)) {
2916 int size1 = (int)tsize(p->n_type, p->n_left->n_df,
2917 p->n_left->n_ap)/SZCHAR;
2918 ap->iarg(0) = (int)tsize(p->n_type, p->n_right->n_df,
2919 p->n_right->n_ap)/SZCHAR;
2920 if (size1 < ap->iarg(0))
2921 ap->iarg(0) = size1;
2922 ap->iarg(1) = talign(p->n_type,
2923 p->n_left->n_ap)/SZCHAR;
2924 break;
2925 }
2926 /* set up size parameters */
2927 ap->iarg(0) = (int)((tsize(STRTY, p->n_left->n_df,
2928 p->n_left->n_ap)+SZCHAR-1)/SZCHAR);
2929 ap->iarg(1) = talign(STRTY,p->n_left->n_ap)/SZCHAR;
2930 if (ap->iarg(1) == 0)
2931 ap->iarg(1) = 1; /* At least char for packed structs */
2932 break;
2933
2934 case XARG:
2935 case XASM:
2936 np->n_name = tmpstrdup(p->n_name);
2937 break;
2938
2939 default:
2940 np->n_name = "";
2941 }
2942
2943 if (ty != LTYPE)
2944 np->n_left = p2tree(p->n_left);
2945 if (ty == BITYPE)
2946 np->n_right = p2tree(p->n_right);
2947 return np;
2948 }
2949
2950
2951 /*
2952 * Change void data types into char.
2953 */
2954 static void
2955 delvoid(P1ND *p, void *arg)
2956 {
2957 /* Convert "PTR undef" (void *) to "PTR uchar" */
2958 if (BTYPE(p->n_type) == VOID)
2959 p->n_type = (p->n_type & ~BTMASK) | UCHAR;
2960 if (BTYPE(p->n_type) == BOOL) {
2961 if (p->n_op == SCONV && p->n_type == BOOL) {
2962 /* create a jump and a set */
2963 P1ND *r;
2964 int l, l2;
2965
2966 r = tempnode(0, BOOL_TYPE, NULL, 0);
2967 cbranch(buildtree(EQ, p->n_left, bcon(0)),
2968 bcon(l = getlab()));
2969 *p = *r;
2970 ecode(buildtree(ASSIGN, p1tcopy(r), bcon(1)));
2971 branch(l2 = getlab());
2972 plabel(l);
2973 ecode(buildtree(ASSIGN, r, bcon(0)));
2974 plabel(l2);
2975 } else
2976 p->n_type = (p->n_type & ~BTMASK) | BOOL_TYPE;
2977 }
2978
2979 }
2980
2981 /*
2982 * Change calls inside calls to separate statement.
2983 */
2984 static P1ND *
2985 deldcall(P1ND *p, int split)
2986 {
2987 P1ND *q, *r;
2988 int o = p->n_op;
2989
2990 if (cdope(o) & CALLFLG) {
2991 if (split) {
2992 q = cstknode(p->n_type, p->n_df, p->n_ap);
2993 r = p1tcopy(q);
2994 q = block(ASSIGN, q, p, p->n_type, p->n_df, p->n_ap);
2995 ecode(q);
2996 return r;
2997 }
2998 split++;
2999 }
3000 if (coptype(o) == BITYPE)
3001 p->n_right = deldcall(p->n_right, split);
3002 if (coptype(o) != LTYPE)
3003 p->n_left = deldcall(p->n_left, split);
3004 return p;
3005 }
3006
3007 #ifndef WORD_ADDRESSED
3008
3009 static P1ND *
3010 pprop(P1ND *p, TWORD t, struct attr *ap)
3011 {
3012 int o = p->n_op;
3013 TWORD t2;
3014
3015 #ifdef PCC_DEBUG
3016 if (p->n_op == TEMP && p->n_type != t &&
3017 !(ISPTR(p->n_type) && ISPTR(t))) {
3018 cerror("TEMP type change: %x -> %x", p->n_type, t);
3019 }
3020 #endif
3021
3022 p->n_type = t;
3023 p->n_ap = ap;
3024 switch (o) {
3025 case UMUL:
3026 t = INCREF(t);
3027 break;
3028 case ADDROF:
3029 t2 = p->n_left->n_type;
3030 if (p->n_left->n_op == TEMP) {
3031 /* Will be converted to memory in pass2 */
3032 /* Do not convert if:
3033 * - t2 not ptr and decref(t) != t2
3034 * - decref(t) not ptr and decref(t) != t2
3035 * XXX add PCONV again? Will be removed upwards.
3036 */
3037 if ((!ISPTR(t2) && DECREF(t) != t2) ||
3038 (ISPTR(t2) && !ISPTR(DECREF(t))))
3039 ; /* Cannot convert this */
3040 else
3041 p->n_left->n_type = DECREF(t);
3042 return p;
3043 }
3044 if (ISPTR(t2) && !ISPTR(DECREF(t)))
3045 break; /* not quite correct */
3046 t = DECREF(t);
3047 break;
3048 case PCONV:
3049 return p;
3050
3051 case PLUS:
3052 if (!ISPTR(p->n_left->n_type)) {
3053 if (!ISPTR(p->n_right->n_type))
3054 cerror("%p: no * in PLUS", p);
3055 p->n_right = pprop(p->n_right, t, ap);
3056 } else
3057 p->n_left = pprop(p->n_left, t, ap);
3058 return p;
3059
3060 case MINUS:
3061 if (ISPTR(p->n_left->n_type)) {
3062 if (ISPTR(p->n_right->n_type))
3063 break; /* change both */
3064 p->n_left = pprop(p->n_left, t, ap);
3065 } else
3066 p->n_right = pprop(p->n_right, t, ap);
3067 return p;
3068
3069 case CALL:
3070 case UCALL:
3071 case STCALL: /* may end up here if struct passed in regs */
3072 case USTCALL:
3073 return p;
3074
3075 case STASG: /* if struct is cast to pointer */
3076 return p;
3077
3078 case ASSIGN:
3079 break;
3080
3081 case COMOP:
3082 p->n_right = pprop(p->n_right, t, ap);
3083 return p;
3084
3085 default:
3086 if (coptype(o) == LTYPE)
3087 break;
3088
3089 #ifdef PCC_DEBUG
3090 p1fwalk(p, eprint, 0);
3091 #endif
3092 cerror("pprop op error %d\n", o);
3093 }
3094 if (coptype(o) == BITYPE)
3095 p->n_right = pprop(p->n_right, t, ap);
3096 if (coptype(o) != LTYPE)
3097 p->n_left = pprop(p->n_left, t, ap);
3098 return p;
3099 }
3100
3101 /*
3102 * Search for PCONV's that can be removed while still keeping
3103 * the type correctness.
3104 */
3105 P1ND *
3106 rmpconv(P1ND *p)
3107 {
3108 struct symtab *sp;
3109 int o = p->n_op;
3110 int ot = coptype(o);
3111 P1ND *q, *l;
3112
3113 if (ot != LTYPE)
3114 p->n_left = rmpconv(p->n_left);
3115 if (ot == BITYPE)
3116 p->n_right = rmpconv(p->n_right);
3117 if (o != PCONV)
3118 return p;
3119 l = p->n_left;
3120 if (nncon(l) || (cdope(l->n_op) & CALLFLG))
3121 ; /* Let any nonamed constant be cast to pointer directly */
3122 else if (l->n_type >= INTPTR && l->n_op == ICON) {
3123 /* named constants only if >= pointer size */
3124 /* create INTPTR type */
3125 sp = l->n_sp;
3126 l->n_sp = NULL;
3127 concast(l, INTPTR);
3128 l->n_sp = sp;
3129 } else if (!ISPTR(l->n_type))
3130 return p;
3131 q = pprop(p->n_left, p->n_type, p->n_ap);
3132 p1nfree(p);
3133 return q;
3134 }
3135 #endif
3136
3137 P1ND *
3138 optloop(P1ND *p)
3139 {
3140 extern int usdnodes;
3141 int n;
3142
3143 do {
3144 n = usdnodes;
3145 p = rmpconv(p);
3146 p = optim(p);
3147 } while (n != usdnodes);
3148
3149 return p;
3150 }
3151
3152 void
3153 ecode(P1ND *p)
3154 {
3155 NODE *r;
3156 /* walk the tree and write out the nodes.. */
3157
3158 if (nerrors)
3159 return;
3160
3161 #ifdef GCC_COMPAT
3162 {
3163 P1ND *q = p;
3164
3165 if (q->n_op == UMUL)
3166 q = p->n_left;
3167 if (cdope(q->n_op)&CALLFLG &&
3168 attr_find(q->n_ap, GCC_ATYP_WARN_UNUSED_RESULT))
3169 werror("return value ignored");
3170 }
3171 #endif
3172 #ifndef WORD_ADDRESSED
3173 p = rmpconv(p);
3174 #endif
3175 p = optim(p);
3176 p = deldcall(p, 0);
3177 p1walkf(p, delvoid, 0);
3178 #ifdef PCC_DEBUG
3179 if (xdebug) {
3180 printf("Fulltree:\n");
3181 p1fwalk(p, eprint, 0);
3182 }
3183 #endif
3184 if (p->n_op == LABEL) {
3185 plabel(glval(p->n_left));
3186 p1tfree(p);
3187 return;
3188 }
3189 r = p2tree(p);
3190 p1tfree(p);
3191 send_passt(IP_NODE, r);
3192 }
3193
3194 /*
3195 * Send something further on to the next pass.
3196 */
3197 void
3198 send_passt(int type, ...)
3199 {
3200 struct interpass *ip;
3201 struct interpass_prolog *ipp;
3202 extern int crslab;
3203 va_list ap;
3204 int sz;
3205
3206 va_start(ap, type);
3207 if (cftnsp == NULL && type != IP_ASM) {
3208 #ifdef notyet
3209 cerror("no function");
3210 #endif
3211 if (type == IP_NODE)
3212 tfree(va_arg(ap, NODE *));
3213 return;
3214 }
3215 if (type == IP_PROLOG || type == IP_EPILOG)
3216 sz = sizeof(struct interpass_prolog);
3217 else
3218 sz = sizeof(struct interpass);
3219
3220 ip = xmalloc(sz);
3221 ip->type = type;
3222 ip->lineno = lineno;
3223 switch (type) {
3224 case IP_NODE:
3225 ip->ip_node = va_arg(ap, NODE *);
3226 break;
3227 case IP_EPILOG:
3228 if (!isinlining) {
3229 locctr(PROG, cftnsp);
3230 defloc(cftnsp);
3231 }
3232 /* FALLTHROUGH */
3233 case IP_PROLOG:
3234 inftn = type == IP_PROLOG ? 1 : 0;
3235 ipp = (struct interpass_prolog *)ip;
3236 ipp->ipp_autos = va_arg(ap, int);
3237 ipp->ipp_name = va_arg(ap, char *);
3238 ipp->ipp_type = va_arg(ap, TWORD);
3239 ipp->ipp_vis = va_arg(ap, int);
3240 ip->ip_lbl = va_arg(ap, int);
3241 ipp->ip_tmpnum = va_arg(ap, int);
3242 ipp->ip_lblnum = crslab;
3243 ipp->ip_labels = va_arg(ap, int *);;
3244 if (type == IP_PROLOG)
3245 ipp->ip_lblnum-=2;
3246 break;
3247 case IP_DEFLAB:
3248 ip->ip_lbl = va_arg(ap, int);
3249 break;
3250 case IP_ASM:
3251 if (blevel == 0) { /* outside function */
3252 printf("%s", va_arg(ap, char *));
3253 va_end(ap);
3254 locctr(NOSEG, NULL);
3255 return;
3256 }
3257 ip->ip_asm = va_arg(ap, char *);
3258 ip->ip_asm = tmpstrdup(ip->ip_asm);
3259 break;
3260 default:
3261 cerror("bad send_passt type %d", type);
3262 }
3263 va_end(ap);
3264 pass1_lastchance(ip); /* target-specific info */
3265 if (isinlining)
3266 inline_addarg(ip);
3267 else
3268 pass2_compile(ip);
3269 }
3270
3271 char *
3272 copst(int op)
3273 {
3274 if (op <= MAXOP)
3275 return opst[op];
3276 #define SNAM(x,y) case x: return #y;
3277 switch (op) {
3278 SNAM(QUALIFIER,QUALIFIER)
3279 SNAM(CLASS,CLASS)
3280 SNAM(RB,])
3281 SNAM(DOT,.)
3282 SNAM(ELLIPSIS,...)
3283 SNAM(LB,[)
3284 SNAM(TYPE,TYPE)
3285 SNAM(COMOP,COMOP)
3286 SNAM(QUEST,?)
3287 SNAM(BIQUEST,?:)
3288 SNAM(COLON,:)
3289 SNAM(ANDAND,&&)
3290 SNAM(OROR,||)
3291 SNAM(NOT,!)
3292 SNAM(CAST,CAST)
3293 SNAM(PLUSEQ,+=)
3294 SNAM(MINUSEQ,-=)
3295 SNAM(MULEQ,*=)
3296 SNAM(DIVEQ,/=)
3297 SNAM(MODEQ,%=)
3298 SNAM(ANDEQ,&=)
3299 SNAM(OREQ,|=)
3300 SNAM(EREQ,^=)
3301 SNAM(LSEQ,<<=)
3302 SNAM(RSEQ,>>=)
3303 SNAM(INCR,++)
3304 SNAM(DECR,--)
3305 SNAM(STRING,STRING)
3306 SNAM(SZOF,SIZEOF)
3307 SNAM(ATTRIB,ATTRIBUTE)
3308 SNAM(TYMERGE,TYMERGE)
3309 SNAM(LABEL,LABEL)
3310 SNAM(UPLUS,U+)
3311 SNAM(ALIGN,ALIGNMENT)
3312 SNAM(FUNSPEC,FUNSPEC)
3313 SNAM(STREF,->)
3314 #ifdef GCC_COMPAT
3315 SNAM(XREAL,__real__)
3316 SNAM(XIMAG,__imag__)
3317 #endif
3318 default:
3319 cerror("bad copst %d", op);
3320 }
3321 return 0; /* XXX gcc */
3322 }
3323
3324 int
3325 cdope(int op)
3326 {
3327 if (op <= MAXOP)
3328 return dope[op];
3329 switch (op) {
3330 case CLOP:
3331 case STRING:
3332 case QUALIFIER:
3333 case CLASS:
3334 case ELLIPSIS:
3335 case TYPE:
3336 case ALIGN:
3337 case FUNSPEC:
3338 return LTYPE;
3339 case DOT:
3340 case SZOF:
3341 case COMOP:
3342 case QUEST:
3343 case BIQUEST:
3344 case COLON:
3345 case LB:
3346 case TYMERGE:
3347 case STREF:
3348 return BITYPE;
3349 case XIMAG:
3350 case XREAL:
3351 case ATTRIB:
3352 case LABEL:
3353 case UPLUS:
3354 case RB:
3355 return UTYPE;
3356 case ANDAND:
3357 case OROR:
3358 return BITYPE|LOGFLG;
3359 case NOT:
3360 return UTYPE|LOGFLG;
3361 case CAST:
3362 return BITYPE|ASGFLG|ASGOPFLG;
3363 case PLUSEQ:
3364 return BITYPE|ASGFLG|ASGOPFLG|FLOFLG|SIMPFLG|COMMFLG;
3365 case MINUSEQ:
3366 return BITYPE|FLOFLG|SIMPFLG|ASGFLG|ASGOPFLG;
3367 case MULEQ:
3368 return BITYPE|FLOFLG|MULFLG|ASGFLG|ASGOPFLG;
3369 case OREQ:
3370 case EREQ:
3371 case ANDEQ:
3372 return BITYPE|SIMPFLG|COMMFLG|ASGFLG|ASGOPFLG;
3373 case DIVEQ:
3374 return BITYPE|FLOFLG|MULFLG|DIVFLG|ASGFLG|ASGOPFLG;
3375 case MODEQ:
3376 return BITYPE|DIVFLG|ASGFLG|ASGOPFLG;
3377 case LSEQ:
3378 case RSEQ:
3379 return BITYPE|SHFFLG|ASGFLG|ASGOPFLG;
3380 case INCR:
3381 case DECR:
3382 return BITYPE|ASGFLG;
3383 }
3384 cerror("cdope missing op %d", op);
3385 return 0; /* XXX gcc */
3386 }
3387
3388 /*
3389 * make a fresh copy of p
3390 */
3391 P1ND *
3392 p1tcopy(P1ND *p)
3393 {
3394 P1ND *q;
3395
3396 q = p1alloc();
3397 *q = *p;
3398
3399 switch (coptype(q->n_op)) {
3400 case BITYPE:
3401 q->n_right = p1tcopy(p->n_right);
3402 /* FALLTHROUGH */
3403 case UTYPE:
3404 q->n_left = p1tcopy(p->n_left);
3405 }
3406
3407 return(q);
3408 }
3409
3410 P1ND *frelink;
3411 int usdnodes;
3412 /*
3413 * Free a node, and return its left descendant.
3414 * It is up to the caller to know whether the return value is usable.
3415 */
3416 P1ND *
3417 p1nfree(P1ND *p)
3418 {
3419 P1ND *l;
3420 #ifdef PCC_DEBUG_NODES
3421 P1ND *q;
3422 #endif
3423
3424 if (p == NULL)
3425 cerror("freeing blank node!");
3426
3427 l = p->n_left;
3428 if (p->n_op == FREE)
3429 cerror("freeing FREE node", p);
3430 #ifdef PCC_DEBUG_NODES
3431 q = frelink;
3432 while (q != NULL) {
3433 if (q == p)
3434 cerror("freeing free node %p", p);
3435 q = q->n_left;
3436 }
3437 #endif
3438
3439 if (ndebug)
3440 printf("freeing p1node %p\n", p);
3441 p->n_op = FREE;
3442 p->n_left = frelink;
3443 frelink = p;
3444 usdnodes--;
3445 return l;
3446 }
3447
3448 P1ND *
3449 p1alloc(void)
3450 {
3451 register P1ND *p;
3452
3453 usdnodes++;
3454
3455 if (frelink != NULL) {
3456 p = frelink;
3457 frelink = p->n_left;
3458 if (p->n_op != FREE)
3459 cerror("node not FREE: %p", p);
3460 if (ndebug)
3461 printf("alloc p1node %p from freelist\n", p);
3462 return p;
3463 }
3464
3465 p = stmtalloc(sizeof(P1ND));
3466 p->n_op = FREE;
3467 if (ndebug)
3468 printf("alloc p1node %p from memory\n", p);
3469 return p;
3470 }
3471
3472
3473 /*
3474 * free the tree p
3475 */
3476 void
3477 p1tfree(P1ND *p)
3478 {
3479 #ifdef PCC_DEBUG
3480 if (p->n_op == FREE)
3481 cerror("freeing FREE node");
3482 #endif
3483 p1walkf(p, (void (*)(P1ND *, void *))p1nfree, 0);
3484 }
3485
3486 void
3487 p1fwalk(P1ND *t, void (*f)(P1ND *, int, int *, int *), int down)
3488 {
3489
3490 int down1, down2;
3491
3492 more:
3493 down1 = down2 = 0;
3494
3495 (*f)(t, down, &down1, &down2);
3496
3497 switch (coptype( t->n_op )) {
3498
3499 case BITYPE:
3500 p1fwalk( t->n_left, f, down1 );
3501 t = t->n_right;
3502 down = down2;
3503 goto more;
3504
3505 case UTYPE:
3506 t = t->n_left;
3507 down = down1;
3508 goto more;
3509
3510 }
3511 }
3512
3513 void
3514 p1walkf(P1ND *t, void (*f)(P1ND *, void *), void *arg)
3515 {
3516 int opty;
3517
3518 opty = coptype(t->n_op);
3519
3520 if (opty != LTYPE)
3521 p1walkf( t->n_left, f, arg );
3522 if (opty == BITYPE)
3523 p1walkf( t->n_right, f, arg );
3524 (*f)(t, arg);
3525 }
3526
3527 /*
3528 * Do a preorder walk of the CM list p and apply function f on each element.
3529 */
3530 void
3531 p1flist(P1ND *p, void (*f)(P1ND *, void *), void *arg)
3532 {
3533 if (p->n_op == CM) {
3534 (*f)(p->n_right, arg);
3535 p1flist(p->n_left, f, arg);
3536 } else
3537 (*f)(p, arg);
3538 }
3539
3540 /*
3541 * The same as flist but postorder.
3542 */
3543 void
3544 p1listf(P1ND *p, void (*f)(P1ND *))
3545 {
3546 if (p->n_op == CM) {
3547 p1listf(p->n_left, f);
3548 (*f)(p->n_right);
3549 } else
3550 (*f)(p);
3551 }
3552
3553 P1ND *
3554 nlabel(int label)
3555 {
3556 return block(LABEL, bcon(label), NULL, 0, 0, 0);
3557 }
3558
3559 /*
3560 * set PROG-seg label.
3561 */
3562 void
3563 plabel(int label)
3564 {
3565 reached = 1; /* Will this always be correct? */
3566 send_passt(IP_DEFLAB, label);
3567 }
3568
3569 /*
3570 * Perform integer promotion on node n.
3571 */
3572 P1ND *
3573 intprom(P1ND *n)
3574 {
3575 if (n->n_op == FLD && UPKFSZ(n->n_rval) < SZINT)
3576 return makety(n, INT, 0, 0, 0);
3577
3578 if ((n->n_type >= CHAR && n->n_type < INT) || n->n_type == BOOL) {
3579 if ((n->n_type == UCHAR && MAX_UCHAR > MAX_INT) ||
3580 (n->n_type == USHORT && MAX_USHORT > MAX_INT))
3581 return makety(n, UNSIGNED, 0, 0, 0);
3582 return makety(n, INT, 0, 0, 0);
3583 }
3584 return n;
3585 }
3586
3587 /*
3588 * Return CON/VOL/0, whichever are active for the current type.
3589 */
3590 int
3591 cqual(TWORD t, TWORD q)
3592 {
3593 while (ISARY(t))
3594 t = DECREF(t), q = DECQAL(q);
3595 if (t <= BTMASK)
3596 q <<= TSHIFT;
3597 return q & (CON|VOL);
3598 }
3599
3600 int crslab = 11;
3601 /*
3602 * Return a number for internal labels.
3603 */
3604 int
3605 getlab(void)
3606 {
3607 int l2 = crslab++;
3608 crslab++;
3609 return l2;
3610 }