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