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