1 #include "cc.h"
2
3 void
complex(Node * n)4 complex(Node *n)
5 {
6
7 if(n == Z)
8 return;
9
10 nearln = n->lineno;
11 if(tcom(n))
12 return;
13 ccom(n);
14 acom(n);
15 }
16
17 /*
18 * evaluate types
19 * evaluate lvalues (addable == 1)
20 */
21 enum
22 {
23 ADDROF = 1<<0,
24 CASTOF = 1<<1,
25 ADDROP = 1<<2,
26 };
27
28 int
tcom(Node * n)29 tcom(Node *n)
30 {
31
32 return tcomo(n, ADDROF);
33 }
34
35 int
tcomo(Node * n,int f)36 tcomo(Node *n, int f)
37 {
38 Node *l, *r;
39 Type *t;
40 int o;
41
42 if(n == Z) {
43 diag(Z, "Z in tcom");
44 errorexit();
45 }
46 l = n->left;
47 r = n->right;
48
49 switch(n->op) {
50 default:
51 diag(n, "unknown op in type complex: %O", n->op);
52 goto bad;
53
54 case ODOTDOT:
55 /*
56 * tcom has already been called on this subtree
57 */
58 *n = *n->left;
59 if(n->type == T)
60 goto bad;
61 break;
62
63 case OCAST:
64 if(n->type == T)
65 break;
66 if(n->type->width == types[TLONG]->width) {
67 if(tcomo(l, ADDROF|CASTOF))
68 goto bad;
69 } else
70 if(tcom(l))
71 goto bad;
72 if(tcompat(n, l->type, n->type, tcast))
73 goto bad;
74 break;
75
76 case ORETURN:
77 if(l == Z) {
78 if(n->type->etype != TVOID)
79 warn(n, "null return of a typed function");
80 break;
81 }
82 if(tcom(l))
83 goto bad;
84 typeext(n->type, l);
85 if(tcompat(n, n->type, l->type, tasign))
86 break;
87 constas(n, n->type, l->type);
88 if(!sametype(n->type, l->type)) {
89 l = new1(OCAST, l, Z);
90 l->type = n->type;
91 n->left = l;
92 }
93 break;
94
95 case OASI: /* same as as, but no test for const */
96 n->op = OAS;
97 o = tcom(l);
98 if(o | tcom(r))
99 goto bad;
100
101 typeext(l->type, r);
102 if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
103 goto bad;
104 if(!sametype(l->type, r->type)) {
105 r = new1(OCAST, r, Z);
106 r->type = l->type;
107 n->right = r;
108 }
109 n->type = l->type;
110 break;
111
112 case OAS:
113 case OASD:
114 o = tcom(l);
115 if(o | tcom(r))
116 goto bad;
117
118 typeext(l->type, r);
119 if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
120 goto bad;
121 constas(n, l->type, r->type);
122 if(!sametype(l->type, r->type)) {
123 r = new1(OCAST, r, Z);
124 r->type = l->type;
125 n->right = r;
126 }
127 n->type = l->type;
128 break;
129
130 case OASADD:
131 case OASSUB:
132 o = tcom(l);
133 if(o | tcom(r))
134 goto bad;
135 typeext1(l->type, r);
136 if(tlvalue(l) || tcompat(n, l->type, r->type, tasadd))
137 goto bad;
138 constas(n, l->type, r->type);
139 t = l->type;
140 arith(n, 0);
141 while(n->left->op == OCAST)
142 n->left = n->left->left;
143 if(!sametype(t, n->type)) {
144 r = new1(OCAST, n->right, Z);
145 r->type = t;
146 n->right = r;
147 n->type = t;
148 }
149 break;
150
151 case OASMUL:
152 case OASLMUL:
153 case OASDIV:
154 case OASLDIV:
155 o = tcom(l);
156 if(o | tcom(r))
157 goto bad;
158 typeext1(l->type, r);
159 if(tlvalue(l) || tcompat(n, l->type, r->type, tmul))
160 goto bad;
161 constas(n, l->type, r->type);
162 t = l->type;
163 arith(n, 0);
164 while(n->left->op == OCAST)
165 n->left = n->left->left;
166 if(!sametype(t, n->type)) {
167 r = new1(OCAST, n->right, Z);
168 r->type = t;
169 n->right = r;
170 n->type = t;
171 }
172 if(typeu[n->type->etype]) {
173 if(n->op == OASDIV)
174 n->op = OASLDIV;
175 if(n->op == OASMUL)
176 n->op = OASLMUL;
177 }
178 break;
179
180 case OASLSHR:
181 case OASASHR:
182 case OASASHL:
183 o = tcom(l);
184 if(o | tcom(r))
185 goto bad;
186 if(tlvalue(l) || tcompat(n, l->type, r->type, tand))
187 goto bad;
188 n->type = l->type;
189 if(typeu[n->type->etype]) {
190 if(n->op == OASASHR)
191 n->op = OASLSHR;
192 }
193 break;
194
195 case OASMOD:
196 case OASLMOD:
197 case OASOR:
198 case OASAND:
199 case OASXOR:
200 o = tcom(l);
201 if(o | tcom(r))
202 goto bad;
203 if(tlvalue(l) || tcompat(n, l->type, r->type, tand))
204 goto bad;
205 t = l->type;
206 arith(n, 0);
207 while(n->left->op == OCAST)
208 n->left = n->left->left;
209 if(!sametype(t, n->type)) {
210 r = new1(OCAST, n->right, Z);
211 r->type = t;
212 n->right = r;
213 n->type = t;
214 }
215 if(typeu[n->type->etype]) {
216 if(n->op == OASMOD)
217 n->op = OASLMOD;
218 }
219 break;
220
221 case OPREINC:
222 case OPREDEC:
223 case OPOSTINC:
224 case OPOSTDEC:
225 if(tcom(l))
226 goto bad;
227 if(tlvalue(l) || tcompat(n, l->type, types[TINT], tadd))
228 goto bad;
229 n->type = l->type;
230 if(n->type->etype == TIND)
231 if(n->type->link->width < 1)
232 diag(n, "inc/dec of a void pointer");
233 break;
234
235 case OEQ:
236 case ONE:
237 o = tcom(l);
238 if(o | tcom(r))
239 goto bad;
240 typeext(l->type, r);
241 typeext(r->type, l);
242 if(tcompat(n, l->type, r->type, trel))
243 goto bad;
244 arith(n, 0);
245 n->type = types[TINT];
246 break;
247
248 case OLT:
249 case OGE:
250 case OGT:
251 case OLE:
252 o = tcom(l);
253 if(o | tcom(r))
254 goto bad;
255 typeext1(l->type, r);
256 typeext1(r->type, l);
257 if(tcompat(n, l->type, r->type, trel))
258 goto bad;
259 arith(n, 0);
260 if(typeu[n->type->etype])
261 n->op = logrel[relindex(n->op)];
262 n->type = types[TINT];
263 break;
264
265 case OCOND:
266 o = tcom(l);
267 o |= tcom(r->left);
268 if(o | tcom(r->right))
269 goto bad;
270 if(r->right->type->etype == TIND && vconst(r->left) == 0) {
271 r->left->type = r->right->type;
272 r->left->vconst = 0;
273 }
274 if(r->left->type->etype == TIND && vconst(r->right) == 0) {
275 r->right->type = r->left->type;
276 r->right->vconst = 0;
277 }
278 if(sametype(r->right->type, r->left->type)) {
279 r->type = r->right->type;
280 n->type = r->type;
281 break;
282 }
283 if(tcompat(r, r->left->type, r->right->type, trel))
284 goto bad;
285 arith(r, 0);
286 n->type = r->type;
287 break;
288
289 case OADD:
290 o = tcom(l);
291 if(o | tcom(r))
292 goto bad;
293 if(tcompat(n, l->type, r->type, tadd))
294 goto bad;
295 arith(n, 1);
296 break;
297
298 case OSUB:
299 o = tcom(l);
300 if(o | tcom(r))
301 goto bad;
302 if(tcompat(n, l->type, r->type, tsub))
303 goto bad;
304 arith(n, 1);
305 break;
306
307 case OMUL:
308 case OLMUL:
309 case ODIV:
310 case OLDIV:
311 o = tcom(l);
312 if(o | tcom(r))
313 goto bad;
314 if(tcompat(n, l->type, r->type, tmul))
315 goto bad;
316 arith(n, 1);
317 if(typeu[n->type->etype]) {
318 if(n->op == ODIV)
319 n->op = OLDIV;
320 if(n->op == OMUL)
321 n->op = OLMUL;
322 }
323 break;
324
325 case OLSHR:
326 case OASHL:
327 case OASHR:
328 o = tcom(l);
329 if(o | tcom(r))
330 goto bad;
331 if(tcompat(n, l->type, r->type, tand))
332 goto bad;
333 n->right = Z;
334 arith(n, 1);
335 n->right = new1(OCAST, r, Z);
336 n->right->type = types[TINT];
337 if(typeu[n->type->etype])
338 if(n->op == OASHR)
339 n->op = OLSHR;
340 break;
341
342 case OAND:
343 case OOR:
344 case OXOR:
345 o = tcom(l);
346 if(o | tcom(r))
347 goto bad;
348 if(tcompat(n, l->type, r->type, tand))
349 goto bad;
350 arith(n, 1);
351 break;
352
353 case OMOD:
354 case OLMOD:
355 o = tcom(l);
356 if(o | tcom(r))
357 goto bad;
358 if(tcompat(n, l->type, r->type, tand))
359 goto bad;
360 arith(n, 1);
361 if(typeu[n->type->etype])
362 n->op = OLMOD;
363 break;
364
365 case ONOT:
366 if(tcom(l))
367 goto bad;
368 if(tcompat(n, T, l->type, tnot))
369 goto bad;
370 n->type = types[TINT];
371 break;
372
373 case OPOS:
374 case ONEG:
375 case OCOM:
376 if(tcom(l))
377 goto bad;
378 n->type = l->type;
379 break;
380
381 case ONUL:
382 break;
383
384 case OIOTA:
385 n->type = types[TINT];
386 break;
387
388 case ODAS:
389 n->type = n->left->type;
390 break;
391
392 case OANDAND:
393 case OOROR:
394 o = tcom(l);
395 if(o | tcom(r))
396 goto bad;
397 if(tcompat(n, T, l->type, tnot) |
398 tcompat(n, T, r->type, tnot))
399 goto bad;
400 n->type = types[TINT];
401 break;
402
403 case OCOMMA:
404 o = tcom(l);
405 if(o | tcom(r))
406 goto bad;
407 n->type = r->type;
408 break;
409
410
411 case OSIGN: /* extension signof(type) returns a hash */
412 if(l != Z) {
413 if(l->op != OSTRING && l->op != OLSTRING)
414 if(tcomo(l, 0))
415 goto bad;
416 if(l->op == OBIT) {
417 diag(n, "signof bitfield");
418 goto bad;
419 }
420 n->type = l->type;
421 }
422 if(n->type == T)
423 goto bad;
424 if(n->type->width < 0) {
425 diag(n, "signof undefined type");
426 goto bad;
427 }
428 n->right = ncopy(n);
429 n->op = OCONST;
430 n->left = Z;
431 /* n->right = Z; */
432 n->vconst = convvtox(signature(n->type, 10), TULONG);
433 n->type = types[TULONG];
434 break;
435
436 case OSIZE:
437 if(l != Z) {
438 if(l->op != OSTRING && l->op != OLSTRING)
439 if(tcomo(l, 0))
440 goto bad;
441 if(l->op == OBIT) {
442 diag(n, "sizeof bitfield");
443 goto bad;
444 }
445 n->type = l->type;
446 }
447 if(n->type == T)
448 goto bad;
449 if(n->type->width <= 0) {
450 diag(n, "sizeof undefined type");
451 goto bad;
452 }
453 if(n->type->etype == TFUNC) {
454 diag(n, "sizeof function");
455 goto bad;
456 }
457 n->right = ncopy(n);
458 n->op = OCONST;
459 n->left = Z;
460 /* n->right = Z; */
461 n->vconst = convvtox(n->type->width, TINT);
462 n->type = types[TINT];
463 break;
464
465 case OFUNC:
466 o = tcomo(l, 0);
467 if(o)
468 goto bad;
469 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
470 l = new1(OIND, l, Z);
471 l->type = l->left->type->link;
472 n->left = l;
473 }
474 if(tcompat(n, T, l->type, tfunct))
475 goto bad;
476 if(o | tcoma(l, r, l->type->down, 1))
477 goto bad;
478 n->type = l->type->link;
479 if(1)
480 if(l->type->down == T || l->type->down->etype == TOLD) {
481 nerrors--;
482 diag(n, "function args not checked: %F", l);
483 }
484 dpcheck(n);
485 break;
486
487 case ONAME:
488 if(n->type == T) {
489 diag(n, "name not declared: %F", n);
490 goto bad;
491 }
492 if(n->type->etype == TENUM) {
493 if(n->sym->tenum->etype == TIND){
494 /* n->op = OSTRING; */
495 n->type = n->sym->tenum;
496 /* n->cstring = n->sym->sconst; */
497 break;
498 }
499 n->left = ncopy(n);
500 n->op = OCONST;
501 n->type = n->sym->tenum;
502 if(!typefd[n->type->etype])
503 n->vconst = n->sym->vconst;
504 else{
505 n->fconst = n->sym->fconst;
506 n->cstring = n->sym->cstring;
507 }
508 break;
509 }
510 break;
511
512 case OLSTRING:
513 case OSTRING:
514 case OCONST:
515 break;
516
517 case ODOT:
518 if(tcom(l))
519 goto bad;
520 if(tcompat(n, T, l->type, tdot))
521 goto bad;
522 if(tcomd(n, l->type))
523 goto bad;
524 break;
525
526 case ODOTIND:
527 if(tcom(l))
528 goto bad;
529 if(tcompat(n, T, l->type, tindir))
530 goto bad;
531 if(tcompat(n, T, l->type->link, tdot))
532 goto bad;
533 if(tcomd(n, l->type->link))
534 goto bad;
535 break;
536
537 case OARRIND:
538 if(tcom(l))
539 goto bad;
540 if(tcompat(n, T, l->type, tindir))
541 goto bad;
542 n->type = l->type->link;
543 if(tcom(r))
544 goto bad;
545 break;
546
547 case OADDR:
548 if(tcomo(l, ADDROP))
549 goto bad;
550 if(tlvalue(l))
551 goto bad;
552 if(l->type->nbits) {
553 diag(n, "address of a bit field");
554 goto bad;
555 }
556 if(l->op == OREGISTER) {
557 diag(n, "address of a register");
558 goto bad;
559 }
560 n->type = typ1(TIND, l->type);
561 n->type->width = types[TIND]->width;
562 break;
563
564 case OIND:
565 if(tcom(l))
566 goto bad;
567 if(tcompat(n, T, l->type, tindir))
568 goto bad;
569 n->type = l->type->link;
570 break;
571
572 case OSTRUCT:
573 if(tcomx(n))
574 goto bad;
575 break;
576 }
577 t = n->type;
578 if(t == T)
579 goto bad;
580 if(t->width < 0) {
581 snap(t);
582 if(t->width < 0) {
583 if(typesu[t->etype] && t->tag)
584 diag(n, "structure not fully declared %s", t->tag->name);
585 else
586 diag(n, "structure not fully declared");
587 goto bad;
588 }
589 }
590 if(typeaf[t->etype]) {
591 if(f & ADDROF)
592 goto addaddr;
593 if(f & ADDROP)
594 warn(n, "address of array/func ignored");
595 }
596 return 0;
597
598 addaddr:
599 if(n->type->etype == TARRAY)
600 n->type = typ1(TIND, n->type->link);
601 return 0;
602 #ifdef WHATEVA
603 if(tlvalue(n))
604 goto bad;
605 l = new1(OXXX, Z, Z);
606 *l = *n;
607 n->op = OADDR;
608 if(l->type->etype == TARRAY)
609 l->type = l->type->link;
610 n->left = l;
611 n->right = Z;
612 n->type = typ1(TIND, l->type);
613 n->type->width = types[TIND]->width;
614 return 0;
615 #endif
616
617 bad:
618 n->type = T;
619 return 1;
620 }
621
622 int
tcoma(Node * l,Node * n,Type * t,int f)623 tcoma(Node *l, Node *n, Type *t, int f)
624 {
625 Node *n1;
626 int o;
627
628 if(t != T)
629 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
630 t = T;
631 if(n == Z) {
632 if(t != T && !sametype(t, types[TVOID])) {
633 diag(n, "not enough function arguments: %F", l);
634 return 1;
635 }
636 return 0;
637 }
638 if(n->op == OLIST) {
639 o = tcoma(l, n->left, t, 0);
640 if(t != T) {
641 t = t->down;
642 if(t == T)
643 t = types[TVOID];
644 }
645 return o | tcoma(l, n->right, t, 1);
646 }
647 if(f && t != T)
648 tcoma(l, Z, t->down, 0);
649 if(tcom(n) || tcompat(n, T, n->type, targ))
650 return 1;
651 if(sametype(t, types[TVOID])) {
652 diag(n, "too many function arguments: %F", l);
653 return 1;
654 }
655 if(t != T) {
656 typeext(t, n);
657 if(stcompat(nodproto, t, n->type, tasign)) {
658 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
659 n->type, t, l);
660 return 1;
661 }
662 switch(t->etype) {
663 case TCHAR:
664 case TSHORT:
665 /* t = types[TINT]; */
666 break;
667
668 case TUCHAR:
669 case TUSHORT:
670 /* t = types[TUINT]; */
671 break;
672 }
673 } else {
674 switch(n->type->etype)
675 {
676 case TCHAR:
677 case TSHORT:
678 /* t = types[TINT]; */
679 t = n->type;
680 break;
681
682 case TUCHAR:
683 case TUSHORT:
684 /* t = types[TUINT]; */
685 t = n->type;
686 break;
687
688 case TFLOAT:
689 /* t = types[TDOUBLE]; */
690 t = n->type;
691 }
692 }
693 if(t != T && !sametype(t, n->type)) {
694 n1 = new1(OXXX, Z, Z);
695 *n1 = *n;
696 n->op = OCAST;
697 n->left = n1;
698 n->right = Z;
699 n->type = t;
700 }
701 return 0;
702 }
703
704 int
tcomd(Node * n,Type * t)705 tcomd(Node *n, Type *t)
706 {
707 long o;
708
709 o = 0;
710 /* t = n->left->type; */
711 for(;;) {
712 t = dotsearch(n->sym, t->link, n);
713 if(t == T) {
714 diag(n, "not a member of struct/union: %F", n);
715 return 1;
716 }
717 o += t->offset;
718 if(t->sym == n->sym)
719 break;
720 if(sametype(t, n->sym->type))
721 break;
722 }
723 n->type = t;
724 return 0;
725 }
726
727 int
tcomx(Node * n)728 tcomx(Node *n)
729 {
730 Type *t;
731 Node *l, *r, **ar, **al;
732 int e;
733
734 e = 0;
735 if(n->type->etype != TSTRUCT) {
736 diag(n, "constructor must be a structure");
737 return 1;
738 }
739 l = invert(n->left);
740 n->left = l;
741 al = &n->left;
742 for(t = n->type->link; t != T; t = t->down) {
743 if(l == Z) {
744 diag(n, "constructor list too short");
745 return 1;
746 }
747 if(l->op == OLIST) {
748 r = l->left;
749 ar = &l->left;
750 al = &l->right;
751 l = l->right;
752 } else {
753 r = l;
754 ar = al;
755 l = Z;
756 }
757 if(tcom(r))
758 e++;
759 typeext(t, r);
760 if(tcompat(n, t, r->type, tasign))
761 e++;
762 constas(n, t, r->type);
763 if(!e && !sametype(t, r->type)) {
764 r = new1(OCAST, r, Z);
765 r->type = t;
766 *ar = r;
767 }
768 }
769 if(l != Z) {
770 diag(n, "constructor list too long");
771 return 1;
772 }
773 return e;
774 }
775
776 int
tlvalue(Node * n)777 tlvalue(Node *n)
778 {
779
780 if(0) {
781 diag(n, "not an l-value");
782 return 1;
783 }
784 return 0;
785 }
786
787 /*
788 * general rewrite
789 * (IND(ADDR x)) ==> x
790 * (ADDR(IND x)) ==> x
791 * remove some zero operands
792 * remove no op casts
793 * evaluate constants
794 */
795 void
ccom(Node * n)796 ccom(Node *n)
797 {
798 Node *l, *r;
799 int t;
800
801 if(n == Z)
802 return;
803 l = n->left;
804 r = n->right;
805 switch(n->op) {
806
807 case OAS:
808 case OASD:
809 case OASXOR:
810 case OASAND:
811 case OASOR:
812 case OASMOD:
813 case OASLMOD:
814 case OASLSHR:
815 case OASASHR:
816 case OASASHL:
817 case OASDIV:
818 case OASLDIV:
819 case OASMUL:
820 case OASLMUL:
821 case OASSUB:
822 case OASADD:
823 ccom(l);
824 ccom(r);
825 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
826 if(r->op == OCONST) {
827 t = n->type->width * 8; /* bits per byte */
828 if(r->vconst >= t || r->vconst < 0)
829 warn(n, "stupid shift: %lld", r->vconst);
830 }
831 break;
832
833 case OCAST:
834 ccom(l);
835 if(l->op == OCONST) {
836 evconst(n);
837 if(n->op == OCONST)
838 break;
839 }
840 if(nocast(l->type, n->type)) {
841 l->type = n->type;
842 *n = *l;
843 }
844 break;
845
846 case OCOND:
847 ccom(l);
848 ccom(r);
849 break;
850
851 case OREGISTER:
852 case OINDREG:
853 case OCONST:
854 case ONAME:
855 break;
856
857 case OADDR:
858 ccom(l);
859 /* l->etype = TVOID; */
860 if(l->op == OIND) {
861 l->left->type = n->type;
862 *n = *l->left;
863 break;
864 }
865 goto common;
866
867 case OIND:
868 ccom(l);
869 if(l->op == OADDR) {
870 l->left->type = n->type;
871 *n = *l->left;
872 break;
873 }
874 goto common;
875
876 case OEQ:
877 case ONE:
878
879 case OLE:
880 case OGE:
881 case OLT:
882 case OGT:
883
884 case OLS:
885 case OHS:
886 case OLO:
887 case OHI:
888 ccom(l);
889 ccom(r);
890 relcon(l, r);
891 relcon(r, l);
892 goto common;
893
894 case OASHR:
895 case OASHL:
896 case OLSHR:
897 ccom(l);
898 ccom(r);
899 if(r->op == OCONST) {
900 t = n->type->width * 8; /* bits per byte */
901 if(r->vconst >= t || r->vconst <= -t)
902 warn(n, "stupid shift: %lld", r->vconst);
903 }
904 goto common;
905
906 default:
907 if(l != Z)
908 ccom(l);
909 if(r != Z)
910 ccom(r);
911 common:
912 if(l != Z)
913 if(l->op != OCONST)
914 break;
915 if(r != Z)
916 if(r->op != OCONST)
917 break;
918 evconst(n);
919 }
920 }
921