xref: /inferno-os/utils/cc/com.c (revision 9dbf735d35c339c90deaed43fc0ae17f16c122f7)
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
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
58 tcom(Node *n)
59 {
60 
61 	return tcomo(n, ADDROF);
62 }
63 
64 int
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
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
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
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
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*
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*
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
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
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 		if(castucom(n))
1044 			warn(n, "32-bit unsigned complement zero-extended to 64 bits");
1045 		ccom(l);
1046 		if(l->op == OCONST) {
1047 			evconst(n);
1048 			if(n->op == OCONST)
1049 				break;
1050 		}
1051 		if(nocast(l->type, n->type) &&
1052 		   (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1053 			l->type = n->type;
1054 			*n = *l;
1055 		}
1056 		break;
1057 
1058 	case OCOND:
1059 		ccom(l);
1060 		ccom(r);
1061 		if(l->op == OCONST)
1062 			if(vconst(l) == 0)
1063 				*n = *r->right;
1064 			else
1065 				*n = *r->left;
1066 		break;
1067 
1068 	case OREGISTER:
1069 	case OINDREG:
1070 	case OCONST:
1071 	case ONAME:
1072 		break;
1073 
1074 	case OADDR:
1075 		ccom(l);
1076 		l->etype = TVOID;
1077 		if(l->op == OIND) {
1078 			l->left->type = n->type;
1079 			*n = *l->left;
1080 			break;
1081 		}
1082 		goto common;
1083 
1084 	case OIND:
1085 		ccom(l);
1086 		if(l->op == OADDR) {
1087 			l->left->type = n->type;
1088 			*n = *l->left;
1089 			break;
1090 		}
1091 		goto common;
1092 
1093 	case OEQ:
1094 	case ONE:
1095 
1096 	case OLE:
1097 	case OGE:
1098 	case OLT:
1099 	case OGT:
1100 
1101 	case OLS:
1102 	case OHS:
1103 	case OLO:
1104 	case OHI:
1105 		ccom(l);
1106 		ccom(r);
1107 		if(compar(n, 0) || compar(n, 1))
1108 			break;
1109 		relcon(l, r);
1110 		relcon(r, l);
1111 		goto common;
1112 
1113 	case OASHR:
1114 	case OASHL:
1115 	case OLSHR:
1116 		ccom(l);
1117 		if(vconst(l) == 0 && !side(r)) {
1118 			*n = *l;
1119 			break;
1120 		}
1121 		ccom(r);
1122 		if(vconst(r) == 0) {
1123 			*n = *l;
1124 			break;
1125 		}
1126 		if(r->op == OCONST) {
1127 			t = n->type->width * 8;	/* bits per byte */
1128 			if(r->vconst >= t || r->vconst <= -t)
1129 				warn(n, "stupid shift: %lld", r->vconst);
1130 		}
1131 		goto common;
1132 
1133 	case OMUL:
1134 	case OLMUL:
1135 		ccom(l);
1136 		t = vconst(l);
1137 		if(t == 0 && !side(r)) {
1138 			*n = *l;
1139 			break;
1140 		}
1141 		if(t == 1) {
1142 			*n = *r;
1143 			goto loop;
1144 		}
1145 		ccom(r);
1146 		t = vconst(r);
1147 		if(t == 0 && !side(l)) {
1148 			*n = *r;
1149 			break;
1150 		}
1151 		if(t == 1) {
1152 			*n = *l;
1153 			break;
1154 		}
1155 		goto common;
1156 
1157 	case ODIV:
1158 	case OLDIV:
1159 		ccom(l);
1160 		if(vconst(l) == 0 && !side(r)) {
1161 			*n = *l;
1162 			break;
1163 		}
1164 		ccom(r);
1165 		t = vconst(r);
1166 		if(t == 0) {
1167 			diag(n, "divide check");
1168 			*n = *r;
1169 			break;
1170 		}
1171 		if(t == 1) {
1172 			*n = *l;
1173 			break;
1174 		}
1175 		goto common;
1176 
1177 	case OSUB:
1178 		ccom(r);
1179 		if(r->op == OCONST) {
1180 			if(typefd[r->type->etype]) {
1181 				n->op = OADD;
1182 				r->fconst = -r->fconst;
1183 				goto loop;
1184 			} else {
1185 				n->op = OADD;
1186 				r->vconst = -r->vconst;
1187 				goto loop;
1188 			}
1189 		}
1190 		ccom(l);
1191 		goto common;
1192 
1193 	case OXOR:
1194 	case OOR:
1195 	case OADD:
1196 		ccom(l);
1197 		if(vconst(l) == 0) {
1198 			*n = *r;
1199 			goto loop;
1200 		}
1201 		ccom(r);
1202 		if(vconst(r) == 0) {
1203 			*n = *l;
1204 			break;
1205 		}
1206 		goto commute;
1207 
1208 	case OAND:
1209 		ccom(l);
1210 		ccom(r);
1211 		if(vconst(l) == 0 && !side(r)) {
1212 			*n = *l;
1213 			break;
1214 		}
1215 		if(vconst(r) == 0 && !side(l)) {
1216 			*n = *r;
1217 			break;
1218 		}
1219 
1220 	commute:
1221 		/* look for commutative constant */
1222 		if(r->op == OCONST) {
1223 			if(l->op == n->op) {
1224 				if(l->left->op == OCONST) {
1225 					n->right = l->right;
1226 					l->right = r;
1227 					goto loop;
1228 				}
1229 				if(l->right->op == OCONST) {
1230 					n->right = l->left;
1231 					l->left = r;
1232 					goto loop;
1233 				}
1234 			}
1235 		}
1236 		if(l->op == OCONST) {
1237 			if(r->op == n->op) {
1238 				if(r->left->op == OCONST) {
1239 					n->left = r->right;
1240 					r->right = l;
1241 					goto loop;
1242 				}
1243 				if(r->right->op == OCONST) {
1244 					n->left = r->left;
1245 					r->left = l;
1246 					goto loop;
1247 				}
1248 			}
1249 		}
1250 		goto common;
1251 
1252 	case OANDAND:
1253 		ccom(l);
1254 		if(vconst(l) == 0) {
1255 			*n = *l;
1256 			break;
1257 		}
1258 		ccom(r);
1259 		goto common;
1260 
1261 	case OOROR:
1262 		ccom(l);
1263 		if(l->op == OCONST && l->vconst != 0) {
1264 			*n = *l;
1265 			n->vconst = 1;
1266 			break;
1267 		}
1268 		ccom(r);
1269 		goto common;
1270 
1271 	default:
1272 		if(l != Z)
1273 			ccom(l);
1274 		if(r != Z)
1275 			ccom(r);
1276 	common:
1277 		if(l != Z)
1278 		if(l->op != OCONST)
1279 			break;
1280 		if(r != Z)
1281 		if(r->op != OCONST)
1282 			break;
1283 		evconst(n);
1284 	}
1285 }
1286 
1287 /*	OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1288 static char *cmps[12] =
1289 {
1290 	"==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1291 };
1292 
1293 /* 128-bit numbers */
1294 typedef struct Big Big;
1295 struct Big
1296 {
1297 	vlong a;
1298 	uvlong b;
1299 };
1300 static int
1301 cmp(Big x, Big y)
1302 {
1303 	if(x.a != y.a){
1304 		if(x.a < y.a)
1305 			return -1;
1306 		return 1;
1307 	}
1308 	if(x.b != y.b){
1309 		if(x.b < y.b)
1310 			return -1;
1311 		return 1;
1312 	}
1313 	return 0;
1314 }
1315 static Big
1316 add(Big x, int y)
1317 {
1318 	uvlong ob;
1319 
1320 	ob = x.b;
1321 	x.b += y;
1322 	if(y > 0 && x.b < ob)
1323 		x.a++;
1324 	if(y < 0 && x.b > ob)
1325 		x.a--;
1326 	return x;
1327 }
1328 
1329 Big
1330 big(vlong a, uvlong b)
1331 {
1332 	Big x;
1333 
1334 	x.a = a;
1335 	x.b = b;
1336 	return x;
1337 }
1338 
1339 int
1340 compar(Node *n, int reverse)
1341 {
1342 	Big lo, hi, x;
1343 	int op;
1344 	char xbuf[40], cmpbuf[50];
1345 	Node *l, *r;
1346 	Type *lt, *rt;
1347 
1348 	/*
1349 	 * The point of this function is to diagnose comparisons
1350 	 * that can never be true or that look misleading because
1351 	 * of the `usual arithmetic conversions'.  As an example
1352 	 * of the latter, if x is a ulong, then if(x <= -1) really means
1353 	 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1354 	 * what it says (but 8c compiles it wrong anyway).
1355 	 */
1356 
1357 	if(reverse){
1358 		r = n->left;
1359 		l = n->right;
1360 		op = comrel[relindex(n->op)];
1361 	}else{
1362 		l = n->left;
1363 		r = n->right;
1364 		op = n->op;
1365 	}
1366 
1367 	/*
1368 	 * Skip over left casts to find out the original expression range.
1369 	 */
1370 	while(l->op == OCAST)
1371 		l = l->left;
1372 	if(l->op == OCONST)
1373 		return 0;
1374 	lt = l->type;
1375 	if(l->op == ONAME && l->sym->type){
1376 		lt = l->sym->type;
1377 		if(lt->etype == TARRAY)
1378 			lt = lt->link;
1379 	}
1380 	if(lt == T)
1381 		return 0;
1382 	if(lt->etype == TXXX || lt->etype > TUVLONG)
1383 		return 0;
1384 
1385 	/*
1386 	 * Skip over the right casts to find the on-screen value.
1387 	 */
1388 	if(r->op != OCONST)
1389 		return 0;
1390 	while(r->oldop == OCAST && !r->xcast)
1391 		r = r->left;
1392 	rt = r->type;
1393 	if(rt == T)
1394 		return 0;
1395 
1396 	x.b = r->vconst;
1397 	x.a = 0;
1398 	if((rt->etype&1) && r->vconst < 0)	/* signed negative */
1399 		x.a = ~(uvlong)0;
1400 
1401 	if((lt->etype&1)==0){
1402 		/* unsigned */
1403 		lo = big(0, 0);
1404 		if(lt->width == 8)
1405 			hi = big(0, ~(uvlong)0);
1406 		else
1407 			hi = big(0, ((uvlong)1<<(l->type->width*8))-1);
1408 	}else{
1409 		lo = big(~(uvlong)0, -((uvlong)1<<(l->type->width*8-1)));
1410 		hi = big(0, ((uvlong)1<<(l->type->width*8-1))-1);
1411 	}
1412 
1413 	switch(op){
1414 	case OLT:
1415 	case OLO:
1416 	case OGE:
1417 	case OHS:
1418 		if(cmp(x, lo) <= 0)
1419 			goto useless;
1420 		if(cmp(x, add(hi, 1)) >= 0)
1421 			goto useless;
1422 		break;
1423 	case OLE:
1424 	case OLS:
1425 	case OGT:
1426 	case OHI:
1427 		if(cmp(x, add(lo, -1)) <= 0)
1428 			goto useless;
1429 		if(cmp(x, hi) >= 0)
1430 			goto useless;
1431 		break;
1432 	case OEQ:
1433 	case ONE:
1434 		/*
1435 		 * Don't warn about comparisons if the expression
1436 		 * is as wide as the value: the compiler-supplied casts
1437 		 * will make both outcomes possible.
1438 		 */
1439 		if(lt->width >= rt->width && debug['w'] < 2)
1440 			return 0;
1441 		if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1442 			goto useless;
1443 		break;
1444 	}
1445 	return 0;
1446 
1447 useless:
1448 	if((x.a==0 && x.b<=9) || (x.a==~(uvlong)0 && x.b >= -(uvlong)9))
1449 		snprint(xbuf, sizeof xbuf, "%lld", x.b);
1450 	else if(x.a == 0)
1451 		snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1452 	else
1453 		snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1454 	if(reverse)
1455 		snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1456 			xbuf, cmps[relindex(n->op)], lt);
1457 	else
1458 		snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1459 			lt, cmps[relindex(n->op)], xbuf);
1460 if(debug['y']) prtree(n, "strange");
1461 	warn(n, "useless or misleading comparison: %s", cmpbuf);
1462 	return 0;
1463 }
1464 
1465