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