1 #include "cc.h"
2
3 typedef struct Com Com;
4 struct Com
5 {
6 int n;
7 Node *t[500];
8 };
9
10 int compar(Node*, int);
11 static void comma(Node*);
12 static Node* commas(Com*, Node*);
13
14 void
complex(Node * n)15 complex(Node *n)
16 {
17
18 if(n == Z)
19 return;
20
21 nearln = n->lineno;
22 if(debug['t'])
23 if(n->op != OCONST)
24 prtree(n, "pre complex");
25 if(tcom(n))
26 return;
27 if(debug['y'] || 1)
28 comma(n);
29 if(debug['t'])
30 if(n->op != OCONST)
31 prtree(n, "t complex");
32 ccom(n);
33 if(debug['t'])
34 if(n->op != OCONST)
35 prtree(n, "c complex");
36 acom(n);
37 if(debug['t'])
38 if(n->op != OCONST)
39 prtree(n, "a complex");
40 xcom(n);
41 if(debug['t'])
42 if(n->op != OCONST)
43 prtree(n, "x complex");
44 }
45
46 /*
47 * evaluate types
48 * evaluate lvalues (addable == 1)
49 */
50 enum
51 {
52 ADDROF = 1<<0,
53 CASTOF = 1<<1,
54 ADDROP = 1<<2,
55 };
56
57 int
tcom(Node * n)58 tcom(Node *n)
59 {
60
61 return tcomo(n, ADDROF);
62 }
63
64 int
tcomo(Node * n,int f)65 tcomo(Node *n, int f)
66 {
67 Node *l, *r;
68 Type *t;
69 int o;
70 static TRune zer;
71
72 if(n == Z) {
73 diag(Z, "Z in tcom");
74 errorexit();
75 }
76 n->addable = 0;
77 l = n->left;
78 r = n->right;
79
80 switch(n->op) {
81 default:
82 diag(n, "unknown op in type complex: %O", n->op);
83 goto bad;
84
85 case ODOTDOT:
86 /*
87 * tcom has already been called on this subtree
88 */
89 *n = *n->left;
90 if(n->type == T)
91 goto bad;
92 break;
93
94 case OCAST:
95 if(n->type == T)
96 break;
97 if(n->type->width == types[TLONG]->width) {
98 if(tcomo(l, ADDROF|CASTOF))
99 goto bad;
100 } else
101 if(tcom(l))
102 goto bad;
103 if(isfunct(n))
104 break;
105 if(tcompat(n, l->type, n->type, tcast))
106 goto bad;
107 break;
108
109 case ORETURN:
110 if(l == Z) {
111 if(n->type->etype != TVOID)
112 warn(n, "null return of a typed function");
113 break;
114 }
115 if(tcom(l))
116 goto bad;
117 typeext(n->type, l);
118 if(tcompat(n, n->type, l->type, tasign))
119 break;
120 constas(n, n->type, l->type);
121 if(!sametype(n->type, l->type)) {
122 l = new1(OCAST, l, Z);
123 l->type = n->type;
124 n->left = l;
125 }
126 break;
127
128 case OASI: /* same as as, but no test for const */
129 n->op = OAS;
130 o = tcom(l);
131 if(o | tcom(r))
132 goto bad;
133
134 typeext(l->type, r);
135 if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
136 goto bad;
137 if(!sametype(l->type, r->type)) {
138 r = new1(OCAST, r, Z);
139 r->type = l->type;
140 n->right = r;
141 }
142 n->type = l->type;
143 break;
144
145 case OAS:
146 o = tcom(l);
147 if(o | tcom(r))
148 goto bad;
149 if(tlvalue(l))
150 goto bad;
151 if(isfunct(n))
152 break;
153 typeext(l->type, r);
154 if(tcompat(n, l->type, r->type, tasign))
155 goto bad;
156 constas(n, l->type, r->type);
157 if(!sametype(l->type, r->type)) {
158 r = new1(OCAST, r, Z);
159 r->type = l->type;
160 n->right = r;
161 }
162 n->type = l->type;
163 break;
164
165 case OASADD:
166 case OASSUB:
167 o = tcom(l);
168 if(o | tcom(r))
169 goto bad;
170 if(tlvalue(l))
171 goto bad;
172 if(isfunct(n))
173 break;
174 typeext1(l->type, r);
175 if(tcompat(n, l->type, r->type, tasadd))
176 goto bad;
177 constas(n, l->type, r->type);
178 t = l->type;
179 arith(n, 0);
180 while(n->left->op == OCAST)
181 n->left = n->left->left;
182 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
183 r = new1(OCAST, n->right, Z);
184 r->type = t;
185 n->right = r;
186 n->type = t;
187 }
188 break;
189
190 case OASMUL:
191 case OASLMUL:
192 case OASDIV:
193 case OASLDIV:
194 o = tcom(l);
195 if(o | tcom(r))
196 goto bad;
197 if(tlvalue(l))
198 goto bad;
199 if(isfunct(n))
200 break;
201 typeext1(l->type, r);
202 if(tcompat(n, l->type, r->type, tmul))
203 goto bad;
204 constas(n, l->type, r->type);
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) && !mixedasop(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 == OASDIV)
217 n->op = OASLDIV;
218 if(n->op == OASMUL)
219 n->op = OASLMUL;
220 }
221 break;
222
223 case OASLSHR:
224 case OASASHR:
225 case OASASHL:
226 o = tcom(l);
227 if(o | tcom(r))
228 goto bad;
229 if(tlvalue(l))
230 goto bad;
231 if(isfunct(n))
232 break;
233 if(tcompat(n, l->type, r->type, tand))
234 goto bad;
235 n->type = l->type;
236 n->right = new1(OCAST, r, Z);
237 n->right->type = types[TINT];
238 if(typeu[n->type->etype]) {
239 if(n->op == OASASHR)
240 n->op = OASLSHR;
241 }
242 break;
243
244 case OASMOD:
245 case OASLMOD:
246 case OASOR:
247 case OASAND:
248 case OASXOR:
249 o = tcom(l);
250 if(o | tcom(r))
251 goto bad;
252 if(tlvalue(l))
253 goto bad;
254 if(isfunct(n))
255 break;
256 if(tcompat(n, l->type, r->type, tand))
257 goto bad;
258 t = l->type;
259 arith(n, 0);
260 while(n->left->op == OCAST)
261 n->left = n->left->left;
262 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
263 r = new1(OCAST, n->right, Z);
264 r->type = t;
265 n->right = r;
266 n->type = t;
267 }
268 if(typeu[n->type->etype]) {
269 if(n->op == OASMOD)
270 n->op = OASLMOD;
271 }
272 break;
273
274 case OPREINC:
275 case OPREDEC:
276 case OPOSTINC:
277 case OPOSTDEC:
278 if(tcom(l))
279 goto bad;
280 if(tlvalue(l))
281 goto bad;
282 if(isfunct(n))
283 break;
284 if(tcompat(n, l->type, types[TINT], tadd))
285 goto bad;
286 n->type = l->type;
287 if(n->type->etype == TIND)
288 if(n->type->link->width < 1) {
289 snap(n->type->link);
290 if(n->type->link->width < 1)
291 diag(n, "inc/dec of a void pointer");
292 }
293 break;
294
295 case OEQ:
296 case ONE:
297 o = tcom(l);
298 if(o | tcom(r))
299 goto bad;
300 if(isfunct(n))
301 break;
302 typeext(l->type, r);
303 typeext(r->type, l);
304 if(tcompat(n, l->type, r->type, trel))
305 goto bad;
306 arith(n, 0);
307 n->type = types[TINT];
308 break;
309
310 case OLT:
311 case OGE:
312 case OGT:
313 case OLE:
314 o = tcom(l);
315 if(o | tcom(r))
316 goto bad;
317 if(isfunct(n))
318 break;
319 typeext1(l->type, r);
320 typeext1(r->type, l);
321 if(tcompat(n, l->type, r->type, trel))
322 goto bad;
323 arith(n, 0);
324 if(typeu[n->type->etype])
325 n->op = logrel[relindex(n->op)];
326 n->type = types[TINT];
327 break;
328
329 case OCOND:
330 o = tcom(l);
331 o |= tcom(r->left);
332 if(o | tcom(r->right))
333 goto bad;
334 if(r->right->type->etype == TIND && vconst(r->left) == 0) {
335 r->left->type = r->right->type;
336 r->left->vconst = 0;
337 }
338 if(r->left->type->etype == TIND && vconst(r->right) == 0) {
339 r->right->type = r->left->type;
340 r->right->vconst = 0;
341 }
342 if(sametype(r->right->type, r->left->type)) {
343 r->type = r->right->type;
344 n->type = r->type;
345 break;
346 }
347 if(tcompat(r, r->left->type, r->right->type, trel))
348 goto bad;
349 arith(r, 0);
350 n->type = r->type;
351 break;
352
353 case OADD:
354 o = tcom(l);
355 if(o | tcom(r))
356 goto bad;
357 if(isfunct(n))
358 break;
359 if(tcompat(n, l->type, r->type, tadd))
360 goto bad;
361 arith(n, 1);
362 break;
363
364 case OSUB:
365 o = tcom(l);
366 if(o | tcom(r))
367 goto bad;
368 if(isfunct(n))
369 break;
370 if(tcompat(n, l->type, r->type, tsub))
371 goto bad;
372 arith(n, 1);
373 break;
374
375 case OMUL:
376 case OLMUL:
377 case ODIV:
378 case OLDIV:
379 o = tcom(l);
380 if(o | tcom(r))
381 goto bad;
382 if(isfunct(n))
383 break;
384 if(tcompat(n, l->type, r->type, tmul))
385 goto bad;
386 arith(n, 1);
387 if(typeu[n->type->etype]) {
388 if(n->op == ODIV)
389 n->op = OLDIV;
390 if(n->op == OMUL)
391 n->op = OLMUL;
392 }
393 break;
394
395 case OLSHR:
396 case OASHL:
397 case OASHR:
398 o = tcom(l);
399 if(o | tcom(r))
400 goto bad;
401 if(isfunct(n))
402 break;
403 if(tcompat(n, l->type, r->type, tand))
404 goto bad;
405 n->right = Z;
406 arith(n, 1);
407 n->right = new1(OCAST, r, Z);
408 n->right->type = types[TINT];
409 if(typeu[n->type->etype])
410 if(n->op == OASHR)
411 n->op = OLSHR;
412 break;
413
414 case OAND:
415 case OOR:
416 case OXOR:
417 o = tcom(l);
418 if(o | tcom(r))
419 goto bad;
420 if(isfunct(n))
421 break;
422 if(tcompat(n, l->type, r->type, tand))
423 goto bad;
424 arith(n, 1);
425 break;
426
427 case OMOD:
428 case OLMOD:
429 o = tcom(l);
430 if(o | tcom(r))
431 goto bad;
432 if(isfunct(n))
433 break;
434 if(tcompat(n, l->type, r->type, tand))
435 goto bad;
436 arith(n, 1);
437 if(typeu[n->type->etype])
438 n->op = OLMOD;
439 break;
440
441 case OPOS:
442 if(tcom(l))
443 goto bad;
444 if(isfunct(n))
445 break;
446
447 r = l;
448 l = new(OCONST, Z, Z);
449 l->vconst = 0;
450 l->type = types[TINT];
451 n->op = OADD;
452 n->right = r;
453 n->left = l;
454
455 if(tcom(l))
456 goto bad;
457 if(tcompat(n, l->type, r->type, tsub))
458 goto bad;
459 arith(n, 1);
460 break;
461
462 case ONEG:
463 if(tcom(l))
464 goto bad;
465 if(isfunct(n))
466 break;
467
468 if(!machcap(n)) {
469 r = l;
470 l = new(OCONST, Z, Z);
471 l->vconst = 0;
472 l->type = types[TINT];
473 n->op = OSUB;
474 n->right = r;
475 n->left = l;
476
477 if(tcom(l))
478 goto bad;
479 if(tcompat(n, l->type, r->type, tsub))
480 goto bad;
481 }
482 arith(n, 1);
483 break;
484
485 case OCOM:
486 if(tcom(l))
487 goto bad;
488 if(isfunct(n))
489 break;
490
491 if(!machcap(n)) {
492 r = l;
493 l = new(OCONST, Z, Z);
494 l->vconst = -1;
495 l->type = types[TINT];
496 n->op = OXOR;
497 n->right = r;
498 n->left = l;
499
500 if(tcom(l))
501 goto bad;
502 if(tcompat(n, l->type, r->type, tand))
503 goto bad;
504 }
505 arith(n, 1);
506 break;
507
508 case ONOT:
509 if(tcom(l))
510 goto bad;
511 if(isfunct(n))
512 break;
513 if(tcompat(n, T, l->type, tnot))
514 goto bad;
515 n->type = types[TINT];
516 break;
517
518 case OANDAND:
519 case OOROR:
520 o = tcom(l);
521 if(o | tcom(r))
522 goto bad;
523 if(tcompat(n, T, l->type, tnot) |
524 tcompat(n, T, r->type, tnot))
525 goto bad;
526 n->type = types[TINT];
527 break;
528
529 case OCOMMA:
530 o = tcom(l);
531 if(o | tcom(r))
532 goto bad;
533 n->type = r->type;
534 break;
535
536
537 case OSIGN: /* extension signof(type) returns a hash */
538 if(l != Z) {
539 if(l->op != OSTRING && l->op != OLSTRING)
540 if(tcomo(l, 0))
541 goto bad;
542 if(l->op == OBIT) {
543 diag(n, "signof bitfield");
544 goto bad;
545 }
546 n->type = l->type;
547 }
548 if(n->type == T)
549 goto bad;
550 if(n->type->width < 0) {
551 diag(n, "signof undefined type");
552 goto bad;
553 }
554 n->op = OCONST;
555 n->left = Z;
556 n->right = Z;
557 n->vconst = convvtox(signature(n->type), TULONG);
558 n->type = types[TULONG];
559 break;
560
561 case OSIZE:
562 if(l != Z) {
563 if(l->op != OSTRING && l->op != OLSTRING)
564 if(tcomo(l, 0))
565 goto bad;
566 if(l->op == OBIT) {
567 diag(n, "sizeof bitfield");
568 goto bad;
569 }
570 n->type = l->type;
571 }
572 if(n->type == T)
573 goto bad;
574 if(n->type->width <= 0) {
575 diag(n, "sizeof undefined type");
576 goto bad;
577 }
578 if(n->type->etype == TFUNC) {
579 diag(n, "sizeof function");
580 goto bad;
581 }
582 n->op = OCONST;
583 n->left = Z;
584 n->right = Z;
585 n->vconst = convvtox(n->type->width, TINT);
586 n->type = types[TINT];
587 break;
588
589 case OFUNC:
590 o = tcomo(l, 0);
591 if(o)
592 goto bad;
593 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
594 l = new1(OIND, l, Z);
595 l->type = l->left->type->link;
596 n->left = l;
597 }
598 if(tcompat(n, T, l->type, tfunct))
599 goto bad;
600 if(o | tcoma(l, r, l->type->down, 1))
601 goto bad;
602 n->type = l->type->link;
603 if(!debug['B'])
604 if(l->type->down == T || l->type->down->etype == TOLD)
605 diag(n, "function args not checked: %F", l);
606 dpcheck(n);
607 break;
608
609 case ONAME:
610 if(n->type == T) {
611 diag(n, "name not declared: %F", n);
612 goto bad;
613 }
614 if(n->type->etype == TENUM) {
615 n->op = OCONST;
616 n->type = n->sym->tenum;
617 if(!typefd[n->type->etype])
618 n->vconst = n->sym->vconst;
619 else
620 n->fconst = n->sym->fconst;
621 break;
622 }
623 n->addable = 1;
624 if(n->class == CEXREG) {
625 n->op = OREGISTER;
626 if(thechar == '8')
627 n->op = OEXREG;
628 n->reg = n->sym->offset;
629 n->xoffset = 0;
630 break;
631 }
632 break;
633
634 case OLSTRING:
635 if(n->type->link != types[TRUNE]) {
636 o = outstring(0, 0);
637 while(o & 3) {
638 outlstring(&zer, sizeof(TRune));
639 o = outlstring(0, 0);
640 }
641 }
642 n->op = ONAME;
643 n->xoffset = outlstring(n->rstring, n->type->width);
644 n->addable = 1;
645 break;
646
647 case OSTRING:
648 if(n->type->link != types[TCHAR]) {
649 o = outstring(0, 0);
650 while(o & 3) {
651 outstring("", 1);
652 o = outstring(0, 0);
653 }
654 }
655 n->op = ONAME;
656 n->xoffset = outstring(n->cstring, n->type->width);
657 n->addable = 1;
658 break;
659
660 case OCONST:
661 break;
662
663 case ODOT:
664 if(tcom(l))
665 goto bad;
666 if(tcompat(n, T, l->type, tdot))
667 goto bad;
668 if(tcomd(n))
669 goto bad;
670 break;
671
672 case OADDR:
673 if(tcomo(l, ADDROP))
674 goto bad;
675 if(tlvalue(l))
676 goto bad;
677 if(l->type->nbits) {
678 diag(n, "address of a bit field");
679 goto bad;
680 }
681 if(l->op == OREGISTER) {
682 diag(n, "address of a register");
683 goto bad;
684 }
685 n->type = typ(TIND, l->type);
686 n->type->width = types[TIND]->width;
687 break;
688
689 case OIND:
690 if(tcom(l))
691 goto bad;
692 if(tcompat(n, T, l->type, tindir))
693 goto bad;
694 n->type = l->type->link;
695 n->addable = 1;
696 break;
697
698 case OSTRUCT:
699 if(tcomx(n))
700 goto bad;
701 break;
702 }
703 t = n->type;
704 if(t == T)
705 goto bad;
706 if(t->width < 0) {
707 snap(t);
708 if(t->width < 0) {
709 if(typesu[t->etype] && t->tag)
710 diag(n, "structure not fully declared %s", t->tag->name);
711 else
712 diag(n, "structure not fully declared");
713 goto bad;
714 }
715 }
716 if(typeaf[t->etype]) {
717 if(f & ADDROF)
718 goto addaddr;
719 if(f & ADDROP)
720 warn(n, "address of array/func ignored");
721 }
722 return 0;
723
724 addaddr:
725 if(tlvalue(n))
726 goto bad;
727 l = new1(OXXX, Z, Z);
728 *l = *n;
729 n->op = OADDR;
730 if(l->type->etype == TARRAY)
731 l->type = l->type->link;
732 n->left = l;
733 n->right = Z;
734 n->addable = 0;
735 n->type = typ(TIND, l->type);
736 n->type->width = types[TIND]->width;
737 return 0;
738
739 bad:
740 n->type = T;
741 return 1;
742 }
743
744 int
tcoma(Node * l,Node * n,Type * t,int f)745 tcoma(Node *l, Node *n, Type *t, int f)
746 {
747 Node *n1;
748 int o;
749
750 if(t != T)
751 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
752 t = T;
753 if(n == Z) {
754 if(t != T && !sametype(t, types[TVOID])) {
755 diag(n, "not enough function arguments: %F", l);
756 return 1;
757 }
758 return 0;
759 }
760 if(n->op == OLIST) {
761 o = tcoma(l, n->left, t, 0);
762 if(t != T) {
763 t = t->down;
764 if(t == T)
765 t = types[TVOID];
766 }
767 return o | tcoma(l, n->right, t, 1);
768 }
769 if(f && t != T)
770 tcoma(l, Z, t->down, 0);
771 if(tcom(n) || tcompat(n, T, n->type, targ))
772 return 1;
773 if(sametype(t, types[TVOID])) {
774 diag(n, "too many function arguments: %F", l);
775 return 1;
776 }
777 if(t != T) {
778 typeext(t, n);
779 if(stcompat(nodproto, t, n->type, tasign)) {
780 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
781 n->type, t, l);
782 return 1;
783 }
784 switch(t->etype) {
785 case TCHAR:
786 case TSHORT:
787 t = types[TINT];
788 break;
789
790 case TUCHAR:
791 case TUSHORT:
792 t = types[TUINT];
793 break;
794 }
795 } else
796 switch(n->type->etype)
797 {
798 case TCHAR:
799 case TSHORT:
800 t = types[TINT];
801 break;
802
803 case TUCHAR:
804 case TUSHORT:
805 t = types[TUINT];
806 break;
807
808 case TFLOAT:
809 t = types[TDOUBLE];
810 }
811 if(t != T && !sametype(t, n->type)) {
812 n1 = new1(OXXX, Z, Z);
813 *n1 = *n;
814 n->op = OCAST;
815 n->left = n1;
816 n->right = Z;
817 n->type = t;
818 n->addable = 0;
819 }
820 return 0;
821 }
822
823 int
tcomd(Node * n)824 tcomd(Node *n)
825 {
826 Type *t;
827 long o;
828
829 o = 0;
830 t = dotsearch(n->sym, n->left->type->link, n, &o);
831 if(t == T) {
832 diag(n, "not a member of struct/union: %F", n);
833 return 1;
834 }
835 makedot(n, t, o);
836 return 0;
837 }
838
839 int
tcomx(Node * n)840 tcomx(Node *n)
841 {
842 Type *t;
843 Node *l, *r, **ar, **al;
844 int e;
845
846 e = 0;
847 if(n->type->etype != TSTRUCT) {
848 diag(n, "constructor must be a structure");
849 return 1;
850 }
851 l = invert(n->left);
852 n->left = l;
853 al = &n->left;
854 for(t = n->type->link; t != T; t = t->down) {
855 if(l == Z) {
856 diag(n, "constructor list too short");
857 return 1;
858 }
859 if(l->op == OLIST) {
860 r = l->left;
861 ar = &l->left;
862 al = &l->right;
863 l = l->right;
864 } else {
865 r = l;
866 ar = al;
867 l = Z;
868 }
869 if(tcom(r))
870 e++;
871 typeext(t, r);
872 if(tcompat(n, t, r->type, tasign))
873 e++;
874 constas(n, t, r->type);
875 if(!e && !sametype(t, r->type)) {
876 r = new1(OCAST, r, Z);
877 r->type = t;
878 *ar = r;
879 }
880 }
881 if(l != Z) {
882 diag(n, "constructor list too long");
883 return 1;
884 }
885 return e;
886 }
887
888 int
tlvalue(Node * n)889 tlvalue(Node *n)
890 {
891
892 if(!n->addable) {
893 diag(n, "not an l-value");
894 return 1;
895 }
896 return 0;
897 }
898
899 /*
900 * hoist comma operators out of expressions
901 * (a,b) OP c => (a, b OP c)
902 * OP(a,b) => (a, OP b)
903 * a OP (b,c) => (b, a OP c)
904 */
905
906 static Node*
comargs(Com * com,Node * n)907 comargs(Com *com, Node *n)
908 {
909 if(n != Z && n->op == OLIST){
910 n->left = comargs(com, n->left);
911 n->right = comargs(com, n->right);
912 }
913 return commas(com, n);
914 }
915
916 static Node*
commas(Com * com,Node * n)917 commas(Com *com, Node *n)
918 {
919 Node *t;
920
921 if(n == Z)
922 return n;
923 switch(n->op){
924 case OREGISTER:
925 case OINDREG:
926 case OCONST:
927 case ONAME:
928 case OSTRING:
929 /* leaf */
930 return n;
931
932 case OCOMMA:
933 t = commas(com, n->left);
934 if(com->n >= nelem(com->t))
935 fatal(n, "comma list overflow");
936 com->t[com->n++] = t;
937 return commas(com, n->right);
938
939 case OFUNC:
940 n->left = commas(com, n->left);
941 n->right = comargs(com, n->right);
942 return n;
943
944 case OCOND:
945 n->left = commas(com, n->left);
946 comma(n->right->left);
947 comma(n->right->right);
948 return n;
949
950 case OANDAND:
951 case OOROR:
952 n->left = commas(com, n->left);
953 comma(n->right);
954 return n;
955
956 case ORETURN:
957 comma(n->left);
958 return n;
959 }
960 n->left = commas(com, n->left);
961 if(n->right != Z)
962 n->right = commas(com, n->right);
963 return n;
964 }
965
966 static void
comma(Node * n)967 comma(Node *n)
968 {
969 Com com;
970 Node *nn;
971
972 com.n = 0;
973 nn = commas(&com, n);
974 if(com.n > 0){
975 if(debug['y'])print("n=%d\n", com.n);
976 if(debug['y']) prtree(nn, "res");
977 if(nn != n)
978 *n = *nn;
979 while(com.n > 0){
980 if(debug['y']) prtree(com.t[com.n-1], "tree");
981 nn = new1(OXXX, Z, Z);
982 *nn = *n;
983 n->op = OCOMMA;
984 n->type = nn->type;
985 n->left = com.t[--com.n];
986 n->right = nn;
987 n->lineno = n->left->lineno;
988 }
989 if(debug['y']) prtree(n, "final");
990 }else if(n != nn)
991 fatal(n, "odd tree");
992 }
993
994 /*
995 * general rewrite
996 * (IND(ADDR x)) ==> x
997 * (ADDR(IND x)) ==> x
998 * remove some zero operands
999 * remove no op casts
1000 * evaluate constants
1001 */
1002 void
ccom(Node * n)1003 ccom(Node *n)
1004 {
1005 Node *l, *r;
1006 int t;
1007
1008 loop:
1009 if(n == Z)
1010 return;
1011 l = n->left;
1012 r = n->right;
1013 switch(n->op) {
1014
1015 case OAS:
1016 case OASXOR:
1017 case OASAND:
1018 case OASOR:
1019 case OASMOD:
1020 case OASLMOD:
1021 case OASLSHR:
1022 case OASASHR:
1023 case OASASHL:
1024 case OASDIV:
1025 case OASLDIV:
1026 case OASMUL:
1027 case OASLMUL:
1028 case OASSUB:
1029 case OASADD:
1030 ccom(l);
1031 ccom(r);
1032 if(n->op == OASMOD || n->op == OASLMOD || n->op == OASDIV || n->op == OASLDIV)
1033 if(r->op == OCONST){
1034 if(!typefd[r->type->etype] && r->vconst == 0) {
1035 if(n->op == OASMOD || n->op == OASLMOD)
1036 diag(n, "modulo by zero");
1037 else
1038 diag(n, "divide by zero");
1039 r->vconst = ~0;
1040 }
1041 if(typefd[r->type->etype] && r->fconst == 0.) {
1042 if(n->op == OASMOD || n->op == OASLMOD)
1043 diag(n, "modulo by zero");
1044 else
1045 diag(n, "divide by zero");
1046 r->fconst = 1e10;
1047 }
1048 }
1049 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
1050 if(r->op == OCONST) {
1051 t = n->type->width * 8; /* bits per byte */
1052 if(r->vconst >= t || r->vconst < 0)
1053 warn(n, "stupid shift: %lld", r->vconst);
1054 }
1055 break;
1056
1057 case OCAST:
1058 if(castucom(n))
1059 warn(n, "32-bit unsigned complement zero-extended to 64 bits");
1060 ccom(l);
1061 if(l->op == OCONST) {
1062 evconst(n);
1063 if(n->op == OCONST)
1064 break;
1065 }
1066 if(nocast(l->type, n->type) &&
1067 (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1068 l->type = n->type;
1069 *n = *l;
1070 }
1071 break;
1072
1073 case OCOND:
1074 ccom(l);
1075 ccom(r);
1076 if(l->op == OCONST)
1077 if(vconst(l) == 0)
1078 *n = *r->right;
1079 else
1080 *n = *r->left;
1081 break;
1082
1083 case OREGISTER:
1084 case OINDREG:
1085 case OCONST:
1086 case ONAME:
1087 break;
1088
1089 case OADDR:
1090 ccom(l);
1091 l->etype = TVOID;
1092 if(l->op == OIND) {
1093 l->left->type = n->type;
1094 *n = *l->left;
1095 break;
1096 }
1097 goto common;
1098
1099 case OIND:
1100 ccom(l);
1101 if(l->op == OADDR) {
1102 l->left->type = n->type;
1103 *n = *l->left;
1104 break;
1105 }
1106 goto common;
1107
1108 case OEQ:
1109 case ONE:
1110
1111 case OLE:
1112 case OGE:
1113 case OLT:
1114 case OGT:
1115
1116 case OLS:
1117 case OHS:
1118 case OLO:
1119 case OHI:
1120 ccom(l);
1121 ccom(r);
1122 if(compar(n, 0) || compar(n, 1))
1123 break;
1124 relcon(l, r);
1125 relcon(r, l);
1126 goto common;
1127
1128 case OASHR:
1129 case OASHL:
1130 case OLSHR:
1131 ccom(l);
1132 if(vconst(l) == 0 && !side(r)) {
1133 *n = *l;
1134 break;
1135 }
1136 ccom(r);
1137 if(vconst(r) == 0) {
1138 *n = *l;
1139 break;
1140 }
1141 if(r->op == OCONST) {
1142 t = n->type->width * 8; /* bits per byte */
1143 if(r->vconst >= t || r->vconst <= -t)
1144 warn(n, "stupid shift: %lld", r->vconst);
1145 }
1146 goto common;
1147
1148 case OMUL:
1149 case OLMUL:
1150 ccom(l);
1151 t = vconst(l);
1152 if(t == 0 && !side(r)) {
1153 *n = *l;
1154 break;
1155 }
1156 if(t == 1) {
1157 *n = *r;
1158 goto loop;
1159 }
1160 ccom(r);
1161 t = vconst(r);
1162 if(t == 0 && !side(l)) {
1163 *n = *r;
1164 break;
1165 }
1166 if(t == 1) {
1167 *n = *l;
1168 break;
1169 }
1170 goto common;
1171
1172 case ODIV:
1173 case OLDIV:
1174 ccom(l);
1175 if(vconst(l) == 0 && !side(r)) {
1176 *n = *l;
1177 break;
1178 }
1179 ccom(r);
1180 t = vconst(r);
1181 if(t == 0) {
1182 diag(n, "divide check");
1183 *n = *r;
1184 break;
1185 }
1186 if(t == 1) {
1187 *n = *l;
1188 break;
1189 }
1190 goto common;
1191
1192 case OSUB:
1193 ccom(r);
1194 if(r->op == OCONST) {
1195 if(typefd[r->type->etype]) {
1196 n->op = OADD;
1197 r->fconst = -r->fconst;
1198 goto loop;
1199 } else {
1200 n->op = OADD;
1201 r->vconst = -r->vconst;
1202 goto loop;
1203 }
1204 }
1205 ccom(l);
1206 goto common;
1207
1208 case OXOR:
1209 case OOR:
1210 case OADD:
1211 ccom(l);
1212 if(vconst(l) == 0) {
1213 *n = *r;
1214 goto loop;
1215 }
1216 ccom(r);
1217 if(vconst(r) == 0) {
1218 *n = *l;
1219 break;
1220 }
1221 goto commute;
1222
1223 case OAND:
1224 ccom(l);
1225 ccom(r);
1226 if(vconst(l) == 0 && !side(r)) {
1227 *n = *l;
1228 break;
1229 }
1230 if(vconst(r) == 0 && !side(l)) {
1231 *n = *r;
1232 break;
1233 }
1234
1235 commute:
1236 /* look for commutative constant */
1237 if(r->op == OCONST) {
1238 if(l->op == n->op) {
1239 if(l->left->op == OCONST) {
1240 n->right = l->right;
1241 l->right = r;
1242 goto loop;
1243 }
1244 if(l->right->op == OCONST) {
1245 n->right = l->left;
1246 l->left = r;
1247 goto loop;
1248 }
1249 }
1250 }
1251 if(l->op == OCONST) {
1252 if(r->op == n->op) {
1253 if(r->left->op == OCONST) {
1254 n->left = r->right;
1255 r->right = l;
1256 goto loop;
1257 }
1258 if(r->right->op == OCONST) {
1259 n->left = r->left;
1260 r->left = l;
1261 goto loop;
1262 }
1263 }
1264 }
1265 goto common;
1266
1267 case OANDAND:
1268 ccom(l);
1269 if(vconst(l) == 0) {
1270 *n = *l;
1271 break;
1272 }
1273 ccom(r);
1274 goto common;
1275
1276 case OOROR:
1277 ccom(l);
1278 if(l->op == OCONST && l->vconst != 0) {
1279 *n = *l;
1280 n->vconst = 1;
1281 break;
1282 }
1283 ccom(r);
1284 goto common;
1285
1286 default:
1287 if(l != Z)
1288 ccom(l);
1289 if(r != Z)
1290 ccom(r);
1291 common:
1292 if(l != Z)
1293 if(l->op != OCONST)
1294 break;
1295 if(r != Z)
1296 if(r->op != OCONST)
1297 break;
1298 evconst(n);
1299 }
1300 }
1301
1302 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1303 static char *cmps[12] =
1304 {
1305 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1306 };
1307
1308 /* 128-bit numbers */
1309 typedef struct Big Big;
1310 struct Big
1311 {
1312 vlong a;
1313 uvlong b;
1314 };
1315 static int
cmp(Big x,Big y)1316 cmp(Big x, Big y)
1317 {
1318 if(x.a != y.a){
1319 if(x.a < y.a)
1320 return -1;
1321 return 1;
1322 }
1323 if(x.b != y.b){
1324 if(x.b < y.b)
1325 return -1;
1326 return 1;
1327 }
1328 return 0;
1329 }
1330 static Big
add(Big x,int y)1331 add(Big x, int y)
1332 {
1333 uvlong ob;
1334
1335 ob = x.b;
1336 x.b += y;
1337 if(y > 0 && x.b < ob)
1338 x.a++;
1339 if(y < 0 && x.b > ob)
1340 x.a--;
1341 return x;
1342 }
1343
1344 Big
big(vlong a,uvlong b)1345 big(vlong a, uvlong b)
1346 {
1347 Big x;
1348
1349 x.a = a;
1350 x.b = b;
1351 return x;
1352 }
1353
1354 int
compar(Node * n,int reverse)1355 compar(Node *n, int reverse)
1356 {
1357 Big lo, hi, x;
1358 int op;
1359 char xbuf[40], cmpbuf[50];
1360 Node *l, *r;
1361 Type *lt, *rt;
1362
1363 /*
1364 * The point of this function is to diagnose comparisons
1365 * that can never be true or that look misleading because
1366 * of the `usual arithmetic conversions'. As an example
1367 * of the latter, if x is a ulong, then if(x <= -1) really means
1368 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1369 * what it says (but 8c compiles it wrong anyway).
1370 */
1371
1372 if(reverse){
1373 r = n->left;
1374 l = n->right;
1375 op = comrel[relindex(n->op)];
1376 }else{
1377 l = n->left;
1378 r = n->right;
1379 op = n->op;
1380 }
1381
1382 /*
1383 * Skip over left casts to find out the original expression range.
1384 */
1385 while(l->op == OCAST)
1386 l = l->left;
1387 if(l->op == OCONST)
1388 return 0;
1389 lt = l->type;
1390 if(l->op == ONAME && l->sym->type){
1391 lt = l->sym->type;
1392 if(lt->etype == TARRAY)
1393 lt = lt->link;
1394 }
1395 if(lt == T)
1396 return 0;
1397 if(lt->etype == TXXX || lt->etype > TUVLONG)
1398 return 0;
1399
1400 /*
1401 * Skip over the right casts to find the on-screen value.
1402 */
1403 if(r->op != OCONST)
1404 return 0;
1405 while(r->oldop == OCAST && !r->xcast)
1406 r = r->left;
1407 rt = r->type;
1408 if(rt == T)
1409 return 0;
1410
1411 x.b = r->vconst;
1412 x.a = 0;
1413 if((rt->etype&1) && r->vconst < 0) /* signed negative */
1414 x.a = ~0ULL;
1415
1416 if((lt->etype&1)==0){
1417 /* unsigned */
1418 lo = big(0, 0);
1419 if(lt->width == 8)
1420 hi = big(0, ~0ULL);
1421 else
1422 hi = big(0, (1LL<<(l->type->width*8))-1);
1423 }else{
1424 lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
1425 hi = big(0, (1LL<<(l->type->width*8-1))-1);
1426 }
1427
1428 switch(op){
1429 case OLT:
1430 case OLO:
1431 case OGE:
1432 case OHS:
1433 if(cmp(x, lo) <= 0)
1434 goto useless;
1435 if(cmp(x, add(hi, 1)) >= 0)
1436 goto useless;
1437 break;
1438 case OLE:
1439 case OLS:
1440 case OGT:
1441 case OHI:
1442 if(cmp(x, add(lo, -1)) <= 0)
1443 goto useless;
1444 if(cmp(x, hi) >= 0)
1445 goto useless;
1446 break;
1447 case OEQ:
1448 case ONE:
1449 /*
1450 * Don't warn about comparisons if the expression
1451 * is as wide as the value: the compiler-supplied casts
1452 * will make both outcomes possible.
1453 */
1454 if(lt->width >= rt->width && debug['w'] < 2)
1455 return 0;
1456 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1457 goto useless;
1458 break;
1459 }
1460 return 0;
1461
1462 useless:
1463 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1464 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1465 else if(x.a == 0)
1466 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1467 else
1468 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1469 if(reverse)
1470 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1471 xbuf, cmps[relindex(n->op)], lt);
1472 else
1473 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1474 lt, cmps[relindex(n->op)], xbuf);
1475 if(debug['y']) prtree(n, "strange");
1476 warn(n, "useless or misleading comparison: %s", cmpbuf);
1477 return 0;
1478 }
1479
1480