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 nerrors--;
606 diag(n, "function args not checked: %F", l);
607 }
608 dpcheck(n);
609 break;
610
611 case ONAME:
612 if(n->type == T) {
613 diag(n, "name not declared: %F", n);
614 goto bad;
615 }
616 if(n->type->etype == TENUM) {
617 n->op = OCONST;
618 n->type = n->sym->tenum;
619 if(!typefd[n->type->etype])
620 n->vconst = n->sym->vconst;
621 else
622 n->fconst = n->sym->fconst;
623 break;
624 }
625 n->addable = 1;
626 if(n->class == CEXREG) {
627 n->op = OREGISTER;
628 if(thechar == '8')
629 n->op = OEXREG;
630 n->reg = n->sym->offset;
631 n->xoffset = 0;
632 break;
633 }
634 break;
635
636 case OLSTRING:
637 if(n->type->link != types[TRUNE]) {
638 o = outstring(0, 0);
639 while(o & 3) {
640 outlstring(&zer, sizeof(TRune));
641 o = outlstring(0, 0);
642 }
643 }
644 n->op = ONAME;
645 n->xoffset = outlstring(n->rstring, n->type->width);
646 n->addable = 1;
647 break;
648
649 case OSTRING:
650 if(n->type->link != types[TCHAR]) {
651 o = outstring(0, 0);
652 while(o & 3) {
653 outstring("", 1);
654 o = outstring(0, 0);
655 }
656 }
657 n->op = ONAME;
658 n->xoffset = outstring(n->cstring, n->type->width);
659 n->addable = 1;
660 break;
661
662 case OCONST:
663 break;
664
665 case ODOT:
666 if(tcom(l))
667 goto bad;
668 if(tcompat(n, T, l->type, tdot))
669 goto bad;
670 if(tcomd(n))
671 goto bad;
672 break;
673
674 case OADDR:
675 if(tcomo(l, ADDROP))
676 goto bad;
677 if(tlvalue(l))
678 goto bad;
679 if(l->type->nbits) {
680 diag(n, "address of a bit field");
681 goto bad;
682 }
683 if(l->op == OREGISTER) {
684 diag(n, "address of a register");
685 goto bad;
686 }
687 n->type = typ(TIND, l->type);
688 n->type->width = types[TIND]->width;
689 break;
690
691 case OIND:
692 if(tcom(l))
693 goto bad;
694 if(tcompat(n, T, l->type, tindir))
695 goto bad;
696 n->type = l->type->link;
697 n->addable = 1;
698 break;
699
700 case OSTRUCT:
701 if(tcomx(n))
702 goto bad;
703 break;
704 }
705 t = n->type;
706 if(t == T)
707 goto bad;
708 if(t->width < 0) {
709 snap(t);
710 if(t->width < 0) {
711 if(typesu[t->etype] && t->tag)
712 diag(n, "structure not fully declared %s", t->tag->name);
713 else
714 diag(n, "structure not fully declared");
715 goto bad;
716 }
717 }
718 if(typeaf[t->etype]) {
719 if(f & ADDROF)
720 goto addaddr;
721 if(f & ADDROP)
722 warn(n, "address of array/func ignored");
723 }
724 return 0;
725
726 addaddr:
727 if(tlvalue(n))
728 goto bad;
729 l = new1(OXXX, Z, Z);
730 *l = *n;
731 n->op = OADDR;
732 if(l->type->etype == TARRAY)
733 l->type = l->type->link;
734 n->left = l;
735 n->right = Z;
736 n->addable = 0;
737 n->type = typ(TIND, l->type);
738 n->type->width = types[TIND]->width;
739 return 0;
740
741 bad:
742 n->type = T;
743 return 1;
744 }
745
746 int
tcoma(Node * l,Node * n,Type * t,int f)747 tcoma(Node *l, Node *n, Type *t, int f)
748 {
749 Node *n1;
750 int o;
751
752 if(t != T)
753 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
754 t = T;
755 if(n == Z) {
756 if(t != T && !sametype(t, types[TVOID])) {
757 diag(n, "not enough function arguments: %F", l);
758 return 1;
759 }
760 return 0;
761 }
762 if(n->op == OLIST) {
763 o = tcoma(l, n->left, t, 0);
764 if(t != T) {
765 t = t->down;
766 if(t == T)
767 t = types[TVOID];
768 }
769 return o | tcoma(l, n->right, t, 1);
770 }
771 if(f && t != T)
772 tcoma(l, Z, t->down, 0);
773 if(tcom(n) || tcompat(n, T, n->type, targ))
774 return 1;
775 if(sametype(t, types[TVOID])) {
776 diag(n, "too many function arguments: %F", l);
777 return 1;
778 }
779 if(t != T) {
780 typeext(t, n);
781 if(stcompat(nodproto, t, n->type, tasign)) {
782 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
783 n->type, t, l);
784 return 1;
785 }
786 switch(t->etype) {
787 case TCHAR:
788 case TSHORT:
789 t = types[TINT];
790 break;
791
792 case TUCHAR:
793 case TUSHORT:
794 t = types[TUINT];
795 break;
796 }
797 } else
798 switch(n->type->etype)
799 {
800 case TCHAR:
801 case TSHORT:
802 t = types[TINT];
803 break;
804
805 case TUCHAR:
806 case TUSHORT:
807 t = types[TUINT];
808 break;
809
810 case TFLOAT:
811 t = types[TDOUBLE];
812 }
813 if(t != T && !sametype(t, n->type)) {
814 n1 = new1(OXXX, Z, Z);
815 *n1 = *n;
816 n->op = OCAST;
817 n->left = n1;
818 n->right = Z;
819 n->type = t;
820 n->addable = 0;
821 }
822 return 0;
823 }
824
825 int
tcomd(Node * n)826 tcomd(Node *n)
827 {
828 Type *t;
829 long o;
830
831 o = 0;
832 t = dotsearch(n->sym, n->left->type->link, n, &o);
833 if(t == T) {
834 diag(n, "not a member of struct/union: %F", n);
835 return 1;
836 }
837 makedot(n, t, o);
838 return 0;
839 }
840
841 int
tcomx(Node * n)842 tcomx(Node *n)
843 {
844 Type *t;
845 Node *l, *r, **ar, **al;
846 int e;
847
848 e = 0;
849 if(n->type->etype != TSTRUCT) {
850 diag(n, "constructor must be a structure");
851 return 1;
852 }
853 l = invert(n->left);
854 n->left = l;
855 al = &n->left;
856 for(t = n->type->link; t != T; t = t->down) {
857 if(l == Z) {
858 diag(n, "constructor list too short");
859 return 1;
860 }
861 if(l->op == OLIST) {
862 r = l->left;
863 ar = &l->left;
864 al = &l->right;
865 l = l->right;
866 } else {
867 r = l;
868 ar = al;
869 l = Z;
870 }
871 if(tcom(r))
872 e++;
873 typeext(t, r);
874 if(tcompat(n, t, r->type, tasign))
875 e++;
876 constas(n, t, r->type);
877 if(!e && !sametype(t, r->type)) {
878 r = new1(OCAST, r, Z);
879 r->type = t;
880 *ar = r;
881 }
882 }
883 if(l != Z) {
884 diag(n, "constructor list too long");
885 return 1;
886 }
887 return e;
888 }
889
890 int
tlvalue(Node * n)891 tlvalue(Node *n)
892 {
893
894 if(!n->addable) {
895 diag(n, "not an l-value");
896 return 1;
897 }
898 return 0;
899 }
900
901 /*
902 * hoist comma operators out of expressions
903 * (a,b) OP c => (a, b OP c)
904 * OP(a,b) => (a, OP b)
905 * a OP (b,c) => (b, a OP c)
906 */
907
908 static Node*
comargs(Com * com,Node * n)909 comargs(Com *com, Node *n)
910 {
911 if(n != Z && n->op == OLIST){
912 n->left = comargs(com, n->left);
913 n->right = comargs(com, n->right);
914 }
915 return commas(com, n);
916 }
917
918 static Node*
commas(Com * com,Node * n)919 commas(Com *com, Node *n)
920 {
921 Node *t;
922
923 if(n == Z)
924 return n;
925 switch(n->op){
926 case OREGISTER:
927 case OINDREG:
928 case OCONST:
929 case ONAME:
930 case OSTRING:
931 /* leaf */
932 return n;
933
934 case OCOMMA:
935 t = commas(com, n->left);
936 if(com->n >= nelem(com->t))
937 fatal(n, "comma list overflow");
938 com->t[com->n++] = t;
939 return commas(com, n->right);
940
941 case OFUNC:
942 n->left = commas(com, n->left);
943 n->right = comargs(com, n->right);
944 return n;
945
946 case OCOND:
947 n->left = commas(com, n->left);
948 comma(n->right->left);
949 comma(n->right->right);
950 return n;
951
952 case OANDAND:
953 case OOROR:
954 n->left = commas(com, n->left);
955 comma(n->right);
956 return n;
957
958 case ORETURN:
959 comma(n->left);
960 return n;
961 }
962 n->left = commas(com, n->left);
963 if(n->right != Z)
964 n->right = commas(com, n->right);
965 return n;
966 }
967
968 static void
comma(Node * n)969 comma(Node *n)
970 {
971 Com com;
972 Node *nn;
973
974 com.n = 0;
975 nn = commas(&com, n);
976 if(com.n > 0){
977 if(debug['y'])print("n=%d\n", com.n);
978 if(debug['y']) prtree(nn, "res");
979 if(nn != n)
980 *n = *nn;
981 while(com.n > 0){
982 if(debug['y']) prtree(com.t[com.n-1], "tree");
983 nn = new1(OXXX, Z, Z);
984 *nn = *n;
985 n->op = OCOMMA;
986 n->type = nn->type;
987 n->left = com.t[--com.n];
988 n->right = nn;
989 n->lineno = n->left->lineno;
990 }
991 if(debug['y']) prtree(n, "final");
992 }else if(n != nn)
993 fatal(n, "odd tree");
994 }
995
996 /*
997 * general rewrite
998 * (IND(ADDR x)) ==> x
999 * (ADDR(IND x)) ==> x
1000 * remove some zero operands
1001 * remove no op casts
1002 * evaluate constants
1003 */
1004 void
ccom(Node * n)1005 ccom(Node *n)
1006 {
1007 Node *l, *r;
1008 int t;
1009
1010 loop:
1011 if(n == Z)
1012 return;
1013 l = n->left;
1014 r = n->right;
1015 switch(n->op) {
1016
1017 case OAS:
1018 case OASXOR:
1019 case OASAND:
1020 case OASOR:
1021 case OASMOD:
1022 case OASLMOD:
1023 case OASLSHR:
1024 case OASASHR:
1025 case OASASHL:
1026 case OASDIV:
1027 case OASLDIV:
1028 case OASMUL:
1029 case OASLMUL:
1030 case OASSUB:
1031 case OASADD:
1032 ccom(l);
1033 ccom(r);
1034 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
1035 if(r->op == OCONST) {
1036 t = n->type->width * 8; /* bits per byte */
1037 if(r->vconst >= t || r->vconst < 0)
1038 warn(n, "stupid shift: %lld", r->vconst);
1039 }
1040 break;
1041
1042 case OCAST:
1043 ccom(l);
1044 if(l->op == OCONST) {
1045 evconst(n);
1046 if(n->op == OCONST)
1047 break;
1048 }
1049 if(nocast(l->type, n->type) &&
1050 (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1051 l->type = n->type;
1052 *n = *l;
1053 }
1054 break;
1055
1056 case OCOND:
1057 ccom(l);
1058 ccom(r);
1059 if(l->op == OCONST)
1060 if(vconst(l) == 0)
1061 *n = *r->right;
1062 else
1063 *n = *r->left;
1064 break;
1065
1066 case OREGISTER:
1067 case OINDREG:
1068 case OCONST:
1069 case ONAME:
1070 break;
1071
1072 case OADDR:
1073 ccom(l);
1074 l->etype = TVOID;
1075 if(l->op == OIND) {
1076 l->left->type = n->type;
1077 *n = *l->left;
1078 break;
1079 }
1080 goto common;
1081
1082 case OIND:
1083 ccom(l);
1084 if(l->op == OADDR) {
1085 l->left->type = n->type;
1086 *n = *l->left;
1087 break;
1088 }
1089 goto common;
1090
1091 case OEQ:
1092 case ONE:
1093
1094 case OLE:
1095 case OGE:
1096 case OLT:
1097 case OGT:
1098
1099 case OLS:
1100 case OHS:
1101 case OLO:
1102 case OHI:
1103 ccom(l);
1104 ccom(r);
1105 if(compar(n, 0) || compar(n, 1))
1106 break;
1107 relcon(l, r);
1108 relcon(r, l);
1109 goto common;
1110
1111 case OASHR:
1112 case OASHL:
1113 case OLSHR:
1114 ccom(l);
1115 if(vconst(l) == 0 && !side(r)) {
1116 *n = *l;
1117 break;
1118 }
1119 ccom(r);
1120 if(vconst(r) == 0) {
1121 *n = *l;
1122 break;
1123 }
1124 if(r->op == OCONST) {
1125 t = n->type->width * 8; /* bits per byte */
1126 if(r->vconst >= t || r->vconst <= -t)
1127 warn(n, "stupid shift: %lld", r->vconst);
1128 }
1129 goto common;
1130
1131 case OMUL:
1132 case OLMUL:
1133 ccom(l);
1134 t = vconst(l);
1135 if(t == 0 && !side(r)) {
1136 *n = *l;
1137 break;
1138 }
1139 if(t == 1) {
1140 *n = *r;
1141 goto loop;
1142 }
1143 ccom(r);
1144 t = vconst(r);
1145 if(t == 0 && !side(l)) {
1146 *n = *r;
1147 break;
1148 }
1149 if(t == 1) {
1150 *n = *l;
1151 break;
1152 }
1153 goto common;
1154
1155 case ODIV:
1156 case OLDIV:
1157 ccom(l);
1158 if(vconst(l) == 0 && !side(r)) {
1159 *n = *l;
1160 break;
1161 }
1162 ccom(r);
1163 t = vconst(r);
1164 if(t == 0) {
1165 diag(n, "divide check");
1166 *n = *r;
1167 break;
1168 }
1169 if(t == 1) {
1170 *n = *l;
1171 break;
1172 }
1173 goto common;
1174
1175 case OSUB:
1176 ccom(r);
1177 if(r->op == OCONST) {
1178 if(typefd[r->type->etype]) {
1179 n->op = OADD;
1180 r->fconst = -r->fconst;
1181 goto loop;
1182 } else {
1183 n->op = OADD;
1184 r->vconst = -r->vconst;
1185 goto loop;
1186 }
1187 }
1188 ccom(l);
1189 goto common;
1190
1191 case OXOR:
1192 case OOR:
1193 case OADD:
1194 ccom(l);
1195 if(vconst(l) == 0) {
1196 *n = *r;
1197 goto loop;
1198 }
1199 ccom(r);
1200 if(vconst(r) == 0) {
1201 *n = *l;
1202 break;
1203 }
1204 goto commute;
1205
1206 case OAND:
1207 ccom(l);
1208 ccom(r);
1209 if(vconst(l) == 0 && !side(r)) {
1210 *n = *l;
1211 break;
1212 }
1213 if(vconst(r) == 0 && !side(l)) {
1214 *n = *r;
1215 break;
1216 }
1217
1218 commute:
1219 /* look for commutative constant */
1220 if(r->op == OCONST) {
1221 if(l->op == n->op) {
1222 if(l->left->op == OCONST) {
1223 n->right = l->right;
1224 l->right = r;
1225 goto loop;
1226 }
1227 if(l->right->op == OCONST) {
1228 n->right = l->left;
1229 l->left = r;
1230 goto loop;
1231 }
1232 }
1233 }
1234 if(l->op == OCONST) {
1235 if(r->op == n->op) {
1236 if(r->left->op == OCONST) {
1237 n->left = r->right;
1238 r->right = l;
1239 goto loop;
1240 }
1241 if(r->right->op == OCONST) {
1242 n->left = r->left;
1243 r->left = l;
1244 goto loop;
1245 }
1246 }
1247 }
1248 goto common;
1249
1250 case OANDAND:
1251 ccom(l);
1252 if(vconst(l) == 0) {
1253 *n = *l;
1254 break;
1255 }
1256 ccom(r);
1257 goto common;
1258
1259 case OOROR:
1260 ccom(l);
1261 if(l->op == OCONST && l->vconst != 0) {
1262 *n = *l;
1263 n->vconst = 1;
1264 break;
1265 }
1266 ccom(r);
1267 goto common;
1268
1269 default:
1270 if(l != Z)
1271 ccom(l);
1272 if(r != Z)
1273 ccom(r);
1274 common:
1275 if(l != Z)
1276 if(l->op != OCONST)
1277 break;
1278 if(r != Z)
1279 if(r->op != OCONST)
1280 break;
1281 evconst(n);
1282 }
1283 }
1284
1285 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1286 static char *cmps[12] =
1287 {
1288 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1289 };
1290
1291 /* 128-bit numbers */
1292 typedef struct Big Big;
1293 struct Big
1294 {
1295 vlong a;
1296 uvlong b;
1297 };
1298 static int
cmp(Big x,Big y)1299 cmp(Big x, Big y)
1300 {
1301 if(x.a != y.a){
1302 if(x.a < y.a)
1303 return -1;
1304 return 1;
1305 }
1306 if(x.b != y.b){
1307 if(x.b < y.b)
1308 return -1;
1309 return 1;
1310 }
1311 return 0;
1312 }
1313 static Big
add(Big x,int y)1314 add(Big x, int y)
1315 {
1316 uvlong ob;
1317
1318 ob = x.b;
1319 x.b += y;
1320 if(y > 0 && x.b < ob)
1321 x.a++;
1322 if(y < 0 && x.b > ob)
1323 x.a--;
1324 return x;
1325 }
1326
1327 Big
big(vlong a,uvlong b)1328 big(vlong a, uvlong b)
1329 {
1330 Big x;
1331
1332 x.a = a;
1333 x.b = b;
1334 return x;
1335 }
1336
1337 int
compar(Node * n,int reverse)1338 compar(Node *n, int reverse)
1339 {
1340 Big lo, hi, x;
1341 int op;
1342 char xbuf[40], cmpbuf[50];
1343 Node *l, *r;
1344 Type *lt, *rt;
1345
1346 /*
1347 * The point of this function is to diagnose comparisons
1348 * that can never be true or that look misleading because
1349 * of the `usual arithmetic conversions'. As an example
1350 * of the latter, if x is a ulong, then if(x <= -1) really means
1351 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1352 * what it says (but 8c compiles it wrong anyway).
1353 */
1354
1355 if(reverse){
1356 r = n->left;
1357 l = n->right;
1358 op = comrel[relindex(n->op)];
1359 }else{
1360 l = n->left;
1361 r = n->right;
1362 op = n->op;
1363 }
1364
1365 /*
1366 * Skip over left casts to find out the original expression range.
1367 */
1368 while(l->op == OCAST)
1369 l = l->left;
1370 if(l->op == OCONST)
1371 return 0;
1372 lt = l->type;
1373 if(l->op == ONAME && l->sym->type){
1374 lt = l->sym->type;
1375 if(lt->etype == TARRAY)
1376 lt = lt->link;
1377 }
1378 if(lt == T)
1379 return 0;
1380 if(lt->etype == TXXX || lt->etype > TUVLONG)
1381 return 0;
1382
1383 /*
1384 * Skip over the right casts to find the on-screen value.
1385 */
1386 if(r->op != OCONST)
1387 return 0;
1388 while(r->oldop == OCAST && !r->xcast)
1389 r = r->left;
1390 rt = r->type;
1391 if(rt == T)
1392 return 0;
1393
1394 x.b = r->vconst;
1395 x.a = 0;
1396 if((rt->etype&1) && r->vconst < 0) /* signed negative */
1397 x.a = ~0ULL;
1398
1399 if((lt->etype&1)==0){
1400 /* unsigned */
1401 lo = big(0, 0);
1402 if(lt->width == 8)
1403 hi = big(0, ~0ULL);
1404 else
1405 hi = big(0, (1LL<<(l->type->width*8))-1);
1406 }else{
1407 lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
1408 hi = big(0, (1LL<<(l->type->width*8-1))-1);
1409 }
1410
1411 switch(op){
1412 case OLT:
1413 case OLO:
1414 case OGE:
1415 case OHS:
1416 if(cmp(x, lo) <= 0)
1417 goto useless;
1418 if(cmp(x, add(hi, 1)) >= 0)
1419 goto useless;
1420 break;
1421 case OLE:
1422 case OLS:
1423 case OGT:
1424 case OHI:
1425 if(cmp(x, add(lo, -1)) <= 0)
1426 goto useless;
1427 if(cmp(x, hi) >= 0)
1428 goto useless;
1429 break;
1430 case OEQ:
1431 case ONE:
1432 /*
1433 * Don't warn about comparisons if the expression
1434 * is as wide as the value: the compiler-supplied casts
1435 * will make both outcomes possible.
1436 */
1437 if(lt->width >= rt->width && debug['w'] < 2)
1438 return 0;
1439 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1440 goto useless;
1441 break;
1442 }
1443 return 0;
1444
1445 useless:
1446 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1447 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1448 else if(x.a == 0)
1449 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1450 else
1451 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1452 if(reverse)
1453 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1454 xbuf, cmps[relindex(n->op)], lt);
1455 else
1456 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1457 lt, cmps[relindex(n->op)], xbuf);
1458 if(debug['y']) prtree(n, "strange");
1459 warn(n, "useless or misleading comparison: %s", cmpbuf);
1460 return 0;
1461 }
1462
1463