]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/cc/com.c
cc: emit right acid format for address
[plan9front.git] / sys / src / cmd / cc / com.c
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
71         if(n == Z) {
72                 diag(Z, "Z in tcom");
73                 errorexit();
74         }
75         n->addable = 0;
76         l = n->left;
77         r = n->right;
78
79         switch(n->op) {
80         default:
81                 diag(n, "unknown op in type complex: %O", n->op);
82                 goto bad;
83
84         case ODOTDOT:
85                 /*
86                  * tcom has already been called on this subtree
87                  */
88                 *n = *n->left;
89                 if(n->type == T)
90                         goto bad;
91                 break;
92
93         case OCAST:
94                 if(n->type == T)
95                         break;
96                 if(n->type->width == types[TLONG]->width) {
97                         if(tcomo(l, ADDROF|CASTOF))
98                                 goto bad;
99                 } else
100                         if(tcom(l))
101                                 goto bad;
102                 if(isfunct(n))
103                         break;
104                 if(tcompat(n, l->type, n->type, tcast))
105                         goto bad;
106                 break;
107
108         case ORETURN:
109                 if(l == Z) {
110                         if(n->type->etype != TVOID)
111                                 warn(n, "null return of a typed function");
112                         break;
113                 }
114                 if(tcom(l))
115                         goto bad;
116                 typeext(n->type, l);
117                 if(tcompat(n, n->type, l->type, tasign))
118                         break;
119                 constas(n, n->type, l->type);
120                 if(!sametype(n->type, l->type)) {
121                         l = new1(OCAST, l, Z);
122                         l->type = n->type;
123                         n->left = l;
124                 }
125                 break;
126
127         case OASI:      /* same as as, but no test for const */
128                 n->op = OAS;
129                 o = tcom(l);
130                 if(o | tcom(r))
131                         goto bad;
132
133                 typeext(l->type, r);
134                 if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
135                         goto bad;
136                 if(!sametype(l->type, r->type)) {
137                         r = new1(OCAST, r, Z);
138                         r->type = l->type;
139                         n->right = r;
140                 }
141                 n->type = l->type;
142                 break;
143
144         case OAS:
145                 o = tcom(l);
146                 if(o | tcom(r))
147                         goto bad;
148                 if(tlvalue(l))
149                         goto bad;
150                 if(isfunct(n))
151                         break;
152                 typeext(l->type, r);
153                 if(tcompat(n, l->type, r->type, tasign))
154                         goto bad;
155                 constas(n, l->type, r->type);
156                 if(!sametype(l->type, r->type)) {
157                         r = new1(OCAST, r, Z);
158                         r->type = l->type;
159                         n->right = r;
160                 }
161                 n->type = l->type;
162                 break;
163
164         case OASADD:
165         case OASSUB:
166                 o = tcom(l);
167                 if(o | tcom(r))
168                         goto bad;
169                 if(tlvalue(l))
170                         goto bad;
171                 if(isfunct(n))
172                         break;
173                 typeext1(l->type, r);
174                 if(tcompat(n, l->type, r->type, tasadd))
175                         goto bad;
176                 constas(n, l->type, r->type);
177                 t = l->type;
178                 arith(n, 0);
179                 while(n->left->op == OCAST)
180                         n->left = n->left->left;
181                 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
182                         r = new1(OCAST, n->right, Z);
183                         r->type = t;
184                         n->right = r;
185                         n->type = t;
186                 }
187                 break;
188
189         case OASMUL:
190         case OASLMUL:
191         case OASDIV:
192         case OASLDIV:
193                 o = tcom(l);
194                 if(o | tcom(r))
195                         goto bad;
196                 if(tlvalue(l))
197                         goto bad;
198                 if(isfunct(n))
199                         break;
200                 typeext1(l->type, r);
201                 if(tcompat(n, l->type, r->type, tmul))
202                         goto bad;
203                 constas(n, l->type, r->type);
204                 t = l->type;
205                 arith(n, 0);
206                 while(n->left->op == OCAST)
207                         n->left = n->left->left;
208                 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
209                         r = new1(OCAST, n->right, Z);
210                         r->type = t;
211                         n->right = r;
212                         n->type = t;
213                 }
214                 if(typeu[n->type->etype]) {
215                         if(n->op == OASDIV)
216                                 n->op = OASLDIV;
217                         if(n->op == OASMUL)
218                                 n->op = OASLMUL;
219                 }
220                 break;
221
222         case OASLSHR:
223         case OASASHR:
224         case OASASHL:
225                 o = tcom(l);
226                 if(o | tcom(r))
227                         goto bad;
228                 if(tlvalue(l))
229                         goto bad;
230                 if(isfunct(n))
231                         break;
232                 if(tcompat(n, l->type, r->type, tand))
233                         goto bad;
234                 n->type = l->type;
235                 n->right = new1(OCAST, r, Z);
236                 n->right->type = types[TINT];
237                 if(typeu[n->type->etype]) {
238                         if(n->op == OASASHR)
239                                 n->op = OASLSHR;
240                 }
241                 break;
242
243         case OASMOD:
244         case OASLMOD:
245         case OASOR:
246         case OASAND:
247         case OASXOR:
248                 o = tcom(l);
249                 if(o | tcom(r))
250                         goto bad;
251                 if(tlvalue(l))
252                         goto bad;
253                 if(isfunct(n))
254                         break;
255                 if(tcompat(n, l->type, r->type, tand))
256                         goto bad;
257                 t = l->type;
258                 arith(n, 0);
259                 while(n->left->op == OCAST)
260                         n->left = n->left->left;
261                 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
262                         r = new1(OCAST, n->right, Z);
263                         r->type = t;
264                         n->right = r;
265                         n->type = t;
266                 }
267                 if(typeu[n->type->etype]) {
268                         if(n->op == OASMOD)
269                                 n->op = OASLMOD;
270                 }
271                 break;
272
273         case OPREINC:
274         case OPREDEC:
275         case OPOSTINC:
276         case OPOSTDEC:
277                 if(tcom(l))
278                         goto bad;
279                 if(tlvalue(l))
280                         goto bad;
281                 if(isfunct(n))
282                         break;
283                 if(tcompat(n, l->type, types[TINT], tadd))
284                         goto bad;
285                 n->type = l->type;
286                 if(n->type->etype == TIND)
287                 if(n->type->link->width < 1) {
288                         snap(n->type->link);
289                         if(n->type->link->width < 1)
290                                 diag(n, "inc/dec of a void pointer");
291                 }
292                 break;
293
294         case OEQ:
295         case ONE:
296                 o = tcom(l);
297                 if(o | tcom(r))
298                         goto bad;
299                 if(isfunct(n))
300                         break;
301                 typeext(l->type, r);
302                 typeext(r->type, l);
303                 if(tcompat(n, l->type, r->type, trel))
304                         goto bad;
305                 arith(n, 0);
306                 n->type = types[TINT];
307                 break;
308
309         case OLT:
310         case OGE:
311         case OGT:
312         case OLE:
313                 o = tcom(l);
314                 if(o | tcom(r))
315                         goto bad;
316                 if(isfunct(n))
317                         break;
318                 typeext1(l->type, r);
319                 typeext1(r->type, l);
320                 if(tcompat(n, l->type, r->type, trel))
321                         goto bad;
322                 arith(n, 0);
323                 if(typeu[n->type->etype])
324                         n->op = logrel[relindex(n->op)];
325                 n->type = types[TINT];
326                 break;
327
328         case OCOND:
329                 o = tcom(l);
330                 o |= tcom(r->left);
331                 if(o | tcom(r->right))
332                         goto bad;
333                 if(r->right->type->etype == TIND && vconst(r->left) == 0) {
334                         r->left->type = r->right->type;
335                         r->left->vconst = 0;
336                 }
337                 if(r->left->type->etype == TIND && vconst(r->right) == 0) {
338                         r->right->type = r->left->type;
339                         r->right->vconst = 0;
340                 }
341                 if(sametype(r->right->type, r->left->type)) {
342                         r->type = r->right->type;
343                         n->type = r->type;
344                         break;
345                 }
346                 if(tcompat(r, r->left->type, r->right->type, trel))
347                         goto bad;
348                 arith(r, 0);
349                 n->type = r->type;
350                 break;
351
352         case OADD:
353                 o = tcom(l);
354                 if(o | tcom(r))
355                         goto bad;
356                 if(isfunct(n))
357                         break;
358                 if(tcompat(n, l->type, r->type, tadd))
359                         goto bad;
360                 arith(n, 1);
361                 break;
362
363         case OSUB:
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, tsub))
370                         goto bad;
371                 arith(n, 1);
372                 break;
373
374         case OMUL:
375         case OLMUL:
376         case ODIV:
377         case OLDIV:
378                 o = tcom(l);
379                 if(o | tcom(r))
380                         goto bad;
381                 if(isfunct(n))
382                         break;
383                 if(tcompat(n, l->type, r->type, tmul))
384                         goto bad;
385                 arith(n, 1);
386                 if(typeu[n->type->etype]) {
387                         if(n->op == ODIV)
388                                 n->op = OLDIV;
389                         if(n->op == OMUL)
390                                 n->op = OLMUL;
391                 }
392                 break;
393
394         case OLSHR:
395         case OASHL:
396         case OASHR:
397                 o = tcom(l);
398                 if(o | tcom(r))
399                         goto bad;
400                 if(isfunct(n))
401                         break;
402                 if(tcompat(n, l->type, r->type, tand))
403                         goto bad;
404                 n->right = Z;
405                 arith(n, 1);
406                 n->right = new1(OCAST, r, Z);
407                 n->right->type = types[TINT];
408                 if(typeu[n->type->etype])
409                         if(n->op == OASHR)
410                                 n->op = OLSHR;
411                 break;
412
413         case OAND:
414         case OOR:
415         case OXOR:
416                 o = tcom(l);
417                 if(o | tcom(r))
418                         goto bad;
419                 if(isfunct(n))
420                         break;
421                 if(tcompat(n, l->type, r->type, tand))
422                         goto bad;
423                 arith(n, 1);
424                 break;
425
426         case OMOD:
427         case OLMOD:
428                 o = tcom(l);
429                 if(o | tcom(r))
430                         goto bad;
431                 if(isfunct(n))
432                         break;
433                 if(tcompat(n, l->type, r->type, tand))
434                         goto bad;
435                 arith(n, 1);
436                 if(typeu[n->type->etype])
437                         n->op = OLMOD;
438                 break;
439
440         case OPOS:
441                 if(tcom(l))
442                         goto bad;
443                 if(isfunct(n))
444                         break;
445
446                 r = l;
447                 l = new(OCONST, Z, Z);
448                 l->vconst = 0;
449                 l->type = types[TINT];
450                 n->op = OADD;
451                 n->right = r;
452                 n->left = l;
453
454                 if(tcom(l))
455                         goto bad;
456                 if(tcompat(n, l->type, r->type, tsub))
457                         goto bad;
458                 arith(n, 1);
459                 break;
460
461         case ONEG:
462                 if(tcom(l))
463                         goto bad;
464                 if(isfunct(n))
465                         break;
466
467                 if(!machcap(n)) {
468                         r = l;
469                         l = new(OCONST, Z, Z);
470                         l->vconst = 0;
471                         l->type = types[TINT];
472                         n->op = OSUB;
473                         n->right = r;
474                         n->left = l;
475
476                         if(tcom(l))
477                                 goto bad;
478                         if(tcompat(n, l->type, r->type, tsub))
479                                 goto bad;
480                 }
481                 arith(n, 1);
482                 break;
483
484         case OCOM:
485                 if(tcom(l))
486                         goto bad;
487                 if(isfunct(n))
488                         break;
489
490                 if(!machcap(n)) {
491                         r = l;
492                         l = new(OCONST, Z, Z);
493                         l->vconst = -1;
494                         l->type = types[TINT];
495                         n->op = OXOR;
496                         n->right = r;
497                         n->left = l;
498
499                         if(tcom(l))
500                                 goto bad;
501                         if(tcompat(n, l->type, r->type, tand))
502                                 goto bad;
503                 }
504                 arith(n, 1);
505                 break;
506
507         case ONOT:
508                 if(tcom(l))
509                         goto bad;
510                 if(isfunct(n))
511                         break;
512                 if(tcompat(n, T, l->type, tnot))
513                         goto bad;
514                 n->type = types[TINT];
515                 break;
516
517         case OANDAND:
518         case OOROR:
519                 o = tcom(l);
520                 if(o | tcom(r))
521                         goto bad;
522                 if(tcompat(n, T, l->type, tnot) |
523                    tcompat(n, T, r->type, tnot))
524                         goto bad;
525                 n->type = types[TINT];
526                 break;
527
528         case OCOMMA:
529                 o = tcom(l);
530                 if(o | tcom(r))
531                         goto bad;
532                 n->type = r->type;
533                 break;
534
535
536         case OSIGN:     /* extension signof(type) returns a hash */
537                 if(l != Z) {
538                         if(l->op != OSTRING && l->op != OLSTRING)
539                                 if(tcomo(l, 0))
540                                         goto bad;
541                         if(l->op == OBIT) {
542                                 diag(n, "signof bitfield");
543                                 goto bad;
544                         }
545                         n->type = l->type;
546                 }
547                 if(n->type == T)
548                         goto bad;
549                 if(n->type->width < 0) {
550                         diag(n, "signof undefined type");
551                         goto bad;
552                 }
553                 n->op = OCONST;
554                 n->left = Z;
555                 n->right = Z;
556                 n->vconst = convvtox(signature(n->type), TULONG);
557                 n->type = types[TULONG];
558                 break;
559
560         case OSIZE:
561                 if(l != Z) {
562                         if(l->op != OSTRING && l->op != OLSTRING)
563                                 if(tcomo(l, 0))
564                                         goto bad;
565                         if(l->op == OBIT) {
566                                 diag(n, "sizeof bitfield");
567                                 goto bad;
568                         }
569                         n->type = l->type;
570                 }
571                 if(n->type == T)
572                         goto bad;
573                 if(n->type->width <= 0) {
574                         diag(n, "sizeof undefined type");
575                         goto bad;
576                 }
577                 if(n->type->etype == TFUNC) {
578                         diag(n, "sizeof function");
579                         goto bad;
580                 }
581                 n->op = OCONST;
582                 n->left = Z;
583                 n->right = Z;
584                 n->vconst = convvtox(n->type->width, TINT);
585                 n->type = types[TINT];
586                 break;
587
588         case OFUNC:
589                 o = tcomo(l, 0);
590                 if(o)
591                         goto bad;
592                 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
593                         l = new1(OIND, l, Z);
594                         l->type = l->left->type->link;
595                         n->left = l;
596                 }
597                 if(tcompat(n, T, l->type, tfunct))
598                         goto bad;
599                 if(o | tcoma(l, r, l->type->down, 1))
600                         goto bad;
601                 n->type = l->type->link;
602                 if(!debug['B'])
603                         if(l->type->down == T || l->type->down->etype == TOLD) {
604                                 nerrors--;
605                                 diag(n, "function args not checked: %F", l);
606                         }
607                 dpcheck(n);
608                 break;
609
610         case ONAME:
611                 if(n->type == T) {
612                         diag(n, "name not declared: %F", n);
613                         goto bad;
614                 }
615                 if(n->type->etype == TENUM) {
616                         n->op = OCONST;
617                         n->type = n->sym->tenum;
618                         if(!typefd[n->type->etype])
619                                 n->vconst = n->sym->vconst;
620                         else
621                                 n->fconst = n->sym->fconst;
622                         break;
623                 }
624                 n->addable = 1;
625                 if(n->class == CEXREG) {
626                         n->op = OREGISTER;
627                         if(thechar == '8')
628                                 n->op = OEXREG;
629                         n->reg = n->sym->offset;
630                         n->xoffset = 0;
631                         break;
632                 }
633                 break;
634
635         case OLSTRING:
636                 if(n->type->link != types[TRUNE]) {
637                         o = outstring(0, 0);
638                         while(o & 3) {
639                                 Rune str[1] = {0};
640                                 outlstring(str, sizeof(Rune));
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                 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
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
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
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
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