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