]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/cc/com.c
sshfs: usage
[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 || l->type->down->etype == TOLD) {
612                                 nerrors--;
613                                 diag(n, "function args not checked: %F", l);
614                         }
615                 dpcheck(n);
616                 break;
617
618         case ONAME:
619                 if(n->type == T) {
620                         diag(n, "name not declared: %F", n);
621                         goto bad;
622                 }
623                 if(n->type->etype == TENUM) {
624                         n->op = OCONST;
625                         n->type = n->sym->tenum;
626                         if(!typefd[n->type->etype])
627                                 n->vconst = n->sym->vconst;
628                         else
629                                 n->fconst = n->sym->fconst;
630                         break;
631                 }
632                 n->addable = 1;
633                 if(n->class == CEXREG) {
634                         n->op = OREGISTER;
635                         if(thechar == '8')
636                                 n->op = OEXREG;
637                         n->reg = n->sym->offset;
638                         n->xoffset = 0;
639                         break;
640                 }
641                 break;
642
643         case OLSTRING:
644                 if(n->type->link != types[TRUNE]) {
645                         o = outstring(0, 0);
646                         while(o & 3) {
647                                 Rune str[1] = {0};
648                                 outlstring(str, sizeof(Rune));
649                                 o = outlstring(0, 0);
650                         }
651                 }
652                 n->op = ONAME;
653                 n->xoffset = outlstring(n->rstring, n->type->width);
654                 n->addable = 1;
655                 break;
656
657         case OSTRING:
658                 if(n->type->link != types[TCHAR]) {
659                         o = outstring(0, 0);
660                         while(o & 3) {
661                                 outstring("", 1);
662                                 o = outstring(0, 0);
663                         }
664                 }
665                 n->op = ONAME;
666                 n->xoffset = outstring(n->cstring, n->type->width);
667                 n->addable = 1;
668                 break;
669
670         case OCONST:
671                 break;
672
673         case ODOT:
674                 if(tcom(l))
675                         goto bad;
676                 if(tcompat(n, T, l->type, tdot))
677                         goto bad;
678                 if(tcomd(n))
679                         goto bad;
680                 break;
681
682         case OADDR:
683                 if(tcomo(l, ADDROP))
684                         goto bad;
685                 if(tlvalue(l))
686                         goto bad;
687                 if(l->type->nbits) {
688                         diag(n, "address of a bit field");
689                         goto bad;
690                 }
691                 if(l->op == OREGISTER) {
692                         diag(n, "address of a register");
693                         goto bad;
694                 }
695                 n->type = typ(TIND, l->type);
696                 n->type->width = types[TIND]->width;
697                 break;
698
699         case OIND:
700                 if(tcom(l))
701                         goto bad;
702                 if(tcompat(n, T, l->type, tindir))
703                         goto bad;
704                 n->type = l->type->link;
705                 n->addable = 1;
706                 break;
707
708         case OSTRUCT:
709                 if(tcomx(n))
710                         goto bad;
711                 break;
712         }
713         t = n->type;
714         if(t == T)
715                 goto bad;
716         if(t->width < 0) {
717                 snap(t);
718                 if(t->width < 0) {
719                         if(typesu[t->etype] && t->tag)
720                                 diag(n, "structure not fully declared %s", t->tag->name);
721                         else
722                                 diag(n, "structure not fully declared");
723                         goto bad;
724                 }
725         }
726         if(typeaf[t->etype]) {
727                 if(f & ADDROF)
728                         goto addaddr;
729                 if(f & ADDROP)
730                         warn(n, "address of array/func ignored");
731         }
732         return 0;
733
734 addaddr:
735         if(tlvalue(n))
736                 goto bad;
737         l = new1(OXXX, Z, Z);
738         *l = *n;
739         n->op = OADDR;
740         if(l->type->etype == TARRAY)
741                 l->type = l->type->link;
742         n->left = l;
743         n->right = Z;
744         n->addable = 0;
745         n->type = typ(TIND, l->type);
746         n->type->width = types[TIND]->width;
747         return 0;
748
749 bad:
750         n->type = T;
751         return 1;
752 }
753
754 int
755 tcoma(Node *l, Node *n, Type *t, int f)
756 {
757         Node *n1;
758         int o;
759
760         if(t != T)
761         if(t->etype == TOLD || t->etype == TDOT)        /* .../old in prototype */
762                 t = T;
763         if(n == Z) {
764                 if(t != T && !sametype(t, types[TVOID])) {
765                         diag(n, "not enough function arguments: %F", l);
766                         return 1;
767                 }
768                 return 0;
769         }
770         if(n->op == OLIST) {
771                 o = tcoma(l, n->left, t, 0);
772                 if(t != T) {
773                         t = t->down;
774                         if(t == T)
775                                 t = types[TVOID];
776                 }
777                 return o | tcoma(l, n->right, t, 1);
778         }
779         if(f && t != T)
780                 tcoma(l, Z, t->down, 0);
781         if(tcom(n) || tcompat(n, T, n->type, targ))
782                 return 1;
783         if(sametype(t, types[TVOID])) {
784                 diag(n, "too many function arguments: %F", l);
785                 return 1;
786         }
787         if(t != T) {
788                 typeext(t, n);
789                 if(stcompat(nodproto, t, n->type, tasign)) {
790                         diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
791                                 n->type, t, l);
792                         return 1;
793                 }
794                 switch(t->etype) {
795                 case TCHAR:
796                 case TSHORT:
797                         t = types[TINT];
798                         break;
799
800                 case TUCHAR:
801                 case TUSHORT:
802                         t = types[TUINT];
803                         break;
804                 }
805         } else
806         switch(n->type->etype)
807         {
808         case TCHAR:
809         case TSHORT:
810                 t = types[TINT];
811                 break;
812
813         case TUCHAR:
814         case TUSHORT:
815                 t = types[TUINT];
816                 break;
817
818         case TFLOAT:
819                 t = types[TDOUBLE];
820         }
821         if(t != T && !sametype(t, n->type)) {
822                 n1 = new1(OXXX, Z, Z);
823                 *n1 = *n;
824                 n->op = OCAST;
825                 n->left = n1;
826                 n->right = Z;
827                 n->type = t;
828                 n->addable = 0;
829         }
830         return 0;
831 }
832
833 int
834 tcomd(Node *n)
835 {
836         Type *t;
837         long o;
838
839         o = 0;
840         t = dotsearch(n->sym, n->left->type->link, n, &o);
841         if(t == T) {
842                 diag(n, "not a member of struct/union: %F", n);
843                 return 1;
844         }
845         makedot(n, t, o);
846         return 0;
847 }
848
849 int
850 tcomx(Node *n)
851 {
852         Type *t;
853         Node *l, *r, **ar, **al;
854         int e;
855
856         e = 0;
857         if(n->type->etype != TSTRUCT) {
858                 diag(n, "constructor must be a structure");
859                 return 1;
860         }
861         l = invert(n->left);
862         n->left = l;
863         al = &n->left;
864         for(t = n->type->link; t != T; t = t->down) {
865                 if(l == Z) {
866                         diag(n, "constructor list too short");
867                         return 1;
868                 }
869                 if(l->op == OLIST) {
870                         r = l->left;
871                         ar = &l->left;
872                         al = &l->right;
873                         l = l->right;
874                 } else {
875                         r = l;
876                         ar = al;
877                         l = Z;
878                 }
879                 if(tcom(r))
880                         e++;
881                 typeext(t, r);
882                 if(tcompat(n, t, r->type, tasign))
883                         e++;
884                 constas(n, t, r->type);
885                 if(!e && !sametype(t, r->type)) {
886                         r = new1(OCAST, r, Z);
887                         r->type = t;
888                         *ar = r;
889                 }
890         }
891         if(l != Z) {
892                 diag(n, "constructor list too long");
893                 return 1;
894         }
895         return e;
896 }
897
898 int
899 tlvalue(Node *n)
900 {
901
902         if(!n->addable) {
903                 diag(n, "not an l-value");
904                 return 1;
905         }
906         return 0;
907 }
908
909 /*
910  * hoist comma operators out of expressions
911  *      (a,b) OP c => (a, b OP c)
912  *      OP(a,b) =>      (a, OP b)
913  *      a OP (b,c) => (b, a OP c)
914  */
915
916 static Node*
917 comargs(Com *com, Node *n)
918 {
919         if(n != Z && n->op == OLIST){
920                 n->left = comargs(com, n->left);
921                 n->right = comargs(com, n->right);
922         }
923         return commas(com, n);
924 }
925
926 static Node*
927 commas(Com *com, Node *n)
928 {
929         Node *t;
930
931         if(n == Z)
932                 return n;
933         switch(n->op){
934         case OREGISTER:
935         case OINDREG:
936         case OCONST:
937         case ONAME:
938         case OSTRING:
939                 /* leaf */
940                 return n;
941
942         case OCOMMA:
943                 t = commas(com, n->left);
944                 if(com->n >= nelem(com->t))
945                         fatal(n, "comma list overflow");
946                 com->t[com->n++] = t;
947                 return commas(com, n->right);
948
949         case OFUNC:
950                 n->left = commas(com, n->left);
951                 n->right = comargs(com, n->right);
952                 return n;
953
954         case OCOND:
955                 n->left = commas(com, n->left);
956                 comma(n->right->left);
957                 comma(n->right->right);
958                 return n;
959
960         case OANDAND:
961         case OOROR:
962                 n->left = commas(com, n->left);
963                 comma(n->right);
964                 return n;
965
966         case ORETURN:
967                 comma(n->left);
968                 return n;
969         }
970         n->left = commas(com, n->left);
971         if(n->right != Z)
972                 n->right = commas(com, n->right);
973         return n;
974 }
975
976 static void
977 comma(Node *n)
978 {
979         Com com;
980         Node *nn;
981
982         com.n = 0;
983         nn = commas(&com, n);
984         if(com.n > 0){
985 if(debug['y'])print("n=%d\n", com.n);
986 if(debug['y']) prtree(nn, "res");
987                 if(nn != n)
988                         *n = *nn;
989                 while(com.n > 0){
990 if(debug['y']) prtree(com.t[com.n-1], "tree");
991                         nn = new1(OXXX, Z, Z);
992                         *nn = *n;
993                         n->op = OCOMMA;
994                         n->type = nn->type;
995                         n->left = com.t[--com.n];
996                         n->right = nn;
997                         n->lineno = n->left->lineno;
998                 }
999 if(debug['y']) prtree(n, "final");
1000         }else if(n != nn)
1001                 fatal(n, "odd tree");
1002 }
1003
1004 /*
1005  *      general rewrite
1006  *      (IND(ADDR x)) ==> x
1007  *      (ADDR(IND x)) ==> x
1008  *      remove some zero operands
1009  *      remove no op casts
1010  *      evaluate constants
1011  */
1012 void
1013 ccom(Node *n)
1014 {
1015         Node *l, *r;
1016         int t;
1017
1018 loop:
1019         if(n == Z)
1020                 return;
1021         l = n->left;
1022         r = n->right;
1023         switch(n->op) {
1024
1025         case OAS:
1026         case OASXOR:
1027         case OASAND:
1028         case OASOR:
1029         case OASMOD:
1030         case OASLMOD:
1031         case OASLSHR:
1032         case OASASHR:
1033         case OASASHL:
1034         case OASDIV:
1035         case OASLDIV:
1036         case OASMUL:
1037         case OASLMUL:
1038         case OASSUB:
1039         case OASADD:
1040                 ccom(l);
1041                 ccom(r);
1042                 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
1043                 if(r->op == OCONST) {
1044                         t = n->type->width * 8; /* bits per byte */
1045                         if(r->vconst >= t || r->vconst < 0)
1046                                 warn(n, "stupid shift: %lld", r->vconst);
1047                 }
1048                 break;
1049
1050         case OCAST:
1051                 ccom(l);
1052                 if(l->op == OCONST) {
1053                         evconst(n);
1054                         if(n->op == OCONST)
1055                                 break;
1056                 }
1057                 if(nocast(l->type, n->type) &&
1058                    (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1059                         l->type = n->type;
1060                         *n = *l;
1061                 }
1062                 break;
1063
1064         case OCOND:
1065                 ccom(l);
1066                 ccom(r);
1067                 if(l->op == OCONST)
1068                         if(vconst(l) == 0)
1069                                 *n = *r->right;
1070                         else
1071                                 *n = *r->left;
1072                 break;
1073
1074         case OREGISTER:
1075         case OINDREG:
1076         case OCONST:
1077         case ONAME:
1078                 break;
1079
1080         case OADDR:
1081                 ccom(l);
1082                 l->etype = TVOID;
1083                 if(l->op == OIND) {
1084                         l->left->type = n->type;
1085                         *n = *l->left;
1086                         break;
1087                 }
1088                 goto common;
1089
1090         case OIND:
1091                 ccom(l);
1092                 if(l->op == OADDR) {
1093                         l->left->type = n->type;
1094                         *n = *l->left;
1095                         break;
1096                 }
1097                 goto common;
1098
1099         case OEQ:
1100         case ONE:
1101
1102         case OLE:
1103         case OGE:
1104         case OLT:
1105         case OGT:
1106
1107         case OLS:
1108         case OHS:
1109         case OLO:
1110         case OHI:
1111                 ccom(l);
1112                 ccom(r);
1113                 if(compar(n, 0) || compar(n, 1))
1114                         break;
1115                 relcon(l, r);
1116                 relcon(r, l);
1117                 goto common;
1118
1119         case OASHR:
1120         case OASHL:
1121         case OLSHR:
1122                 ccom(l);
1123                 if(vconst(l) == 0 && !side(r)) {
1124                         *n = *l;
1125                         break;
1126                 }
1127                 ccom(r);
1128                 if(vconst(r) == 0) {
1129                         *n = *l;
1130                         break;
1131                 }
1132                 if(r->op == OCONST) {
1133                         t = n->type->width * 8; /* bits per byte */
1134                         if(r->vconst >= t || r->vconst <= -t)
1135                                 warn(n, "stupid shift: %lld", r->vconst);
1136                 }
1137                 goto common;
1138
1139         case OMUL:
1140         case OLMUL:
1141                 ccom(l);
1142                 t = vconst(l);
1143                 if(t == 0 && !side(r)) {
1144                         *n = *l;
1145                         break;
1146                 }
1147                 if(t == 1) {
1148                         *n = *r;
1149                         goto loop;
1150                 }
1151                 ccom(r);
1152                 t = vconst(r);
1153                 if(t == 0 && !side(l)) {
1154                         *n = *r;
1155                         break;
1156                 }
1157                 if(t == 1) {
1158                         *n = *l;
1159                         break;
1160                 }
1161                 goto common;
1162
1163         case ODIV:
1164         case OLDIV:
1165                 ccom(l);
1166                 if(vconst(l) == 0 && !side(r)) {
1167                         *n = *l;
1168                         break;
1169                 }
1170                 ccom(r);
1171                 t = vconst(r);
1172                 if(t == 0) {
1173                         diag(n, "divide check");
1174                         *n = *r;
1175                         break;
1176                 }
1177                 if(t == 1) {
1178                         *n = *l;
1179                         break;
1180                 }
1181                 goto common;
1182
1183         case OSUB:
1184                 ccom(r);
1185                 if(r->op == OCONST) {
1186                         if(typefd[r->type->etype]) {
1187                                 n->op = OADD;
1188                                 r->fconst = -r->fconst;
1189                                 goto loop;
1190                         } else {
1191                                 n->op = OADD;
1192                                 r->vconst = -r->vconst;
1193                                 goto loop;
1194                         }
1195                 }
1196                 ccom(l);
1197                 goto common;
1198
1199         case OXOR:
1200         case OOR:
1201         case OADD:
1202                 ccom(l);
1203                 if(vconst(l) == 0) {
1204                         *n = *r;
1205                         goto loop;
1206                 }
1207                 ccom(r);
1208                 if(vconst(r) == 0) {
1209                         *n = *l;
1210                         break;
1211                 }
1212                 goto commute;
1213
1214         case OAND:
1215                 ccom(l);
1216                 ccom(r);
1217                 if(vconst(l) == 0 && !side(r)) {
1218                         *n = *l;
1219                         break;
1220                 }
1221                 if(vconst(r) == 0 && !side(l)) {
1222                         *n = *r;
1223                         break;
1224                 }
1225
1226         commute:
1227                 /* look for commutative constant */
1228                 if(r->op == OCONST) {
1229                         if(l->op == n->op) {
1230                                 if(l->left->op == OCONST) {
1231                                         n->right = l->right;
1232                                         l->right = r;
1233                                         goto loop;
1234                                 }
1235                                 if(l->right->op == OCONST) {
1236                                         n->right = l->left;
1237                                         l->left = r;
1238                                         goto loop;
1239                                 }
1240                         }
1241                 }
1242                 if(l->op == OCONST) {
1243                         if(r->op == n->op) {
1244                                 if(r->left->op == OCONST) {
1245                                         n->left = r->right;
1246                                         r->right = l;
1247                                         goto loop;
1248                                 }
1249                                 if(r->right->op == OCONST) {
1250                                         n->left = r->left;
1251                                         r->left = l;
1252                                         goto loop;
1253                                 }
1254                         }
1255                 }
1256                 goto common;
1257
1258         case OANDAND:
1259                 ccom(l);
1260                 if(vconst(l) == 0) {
1261                         *n = *l;
1262                         break;
1263                 }
1264                 ccom(r);
1265                 goto common;
1266
1267         case OOROR:
1268                 ccom(l);
1269                 if(l->op == OCONST && l->vconst != 0) {
1270                         *n = *l;
1271                         n->vconst = 1;
1272                         break;
1273                 }
1274                 ccom(r);
1275                 goto common;
1276
1277         default:
1278                 if(l != Z)
1279                         ccom(l);
1280                 if(r != Z)
1281                         ccom(r);
1282         common:
1283                 if(l != Z)
1284                 if(l->op != OCONST)
1285                         break;
1286                 if(r != Z)
1287                 if(r->op != OCONST)
1288                         break;
1289                 evconst(n);
1290         }
1291 }
1292
1293 /*      OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1294 static char *cmps[12] = 
1295 {
1296         "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1297 };
1298
1299 /* 128-bit numbers */
1300 typedef struct Big Big;
1301 struct Big
1302 {
1303         vlong a;
1304         uvlong b;
1305 };
1306 static int
1307 cmp(Big x, Big y)
1308 {
1309         if(x.a != y.a){
1310                 if(x.a < y.a)
1311                         return -1;
1312                 return 1;
1313         }
1314         if(x.b != y.b){
1315                 if(x.b < y.b)
1316                         return -1;
1317                 return 1;
1318         }
1319         return 0;
1320 }
1321 static Big
1322 add(Big x, int y)
1323 {
1324         uvlong ob;
1325         
1326         ob = x.b;
1327         x.b += y;
1328         if(y > 0 && x.b < ob)
1329                 x.a++;
1330         if(y < 0 && x.b > ob)
1331                 x.a--;
1332         return x;
1333
1334
1335 Big
1336 big(vlong a, uvlong b)
1337 {
1338         Big x;
1339
1340         x.a = a;
1341         x.b = b;
1342         return x;
1343 }
1344
1345 int
1346 compar(Node *n, int reverse)
1347 {
1348         Big lo, hi, x;
1349         int op;
1350         char xbuf[40], cmpbuf[50];
1351         Node *l, *r;
1352         Type *lt, *rt;
1353
1354         /*
1355          * The point of this function is to diagnose comparisons 
1356          * that can never be true or that look misleading because
1357          * of the `usual arithmetic conversions'.  As an example 
1358          * of the latter, if x is a ulong, then if(x <= -1) really means
1359          * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1360          * what it says (but 8c compiles it wrong anyway).
1361          */
1362
1363         if(reverse){
1364                 r = n->left;
1365                 l = n->right;
1366                 op = comrel[relindex(n->op)];
1367         }else{
1368                 l = n->left;
1369                 r = n->right;
1370                 op = n->op;
1371         }
1372
1373         /*
1374          * Skip over left casts to find out the original expression range.
1375          */
1376         while(l->op == OCAST)
1377                 l = l->left;
1378         if(l->op == OCONST)
1379                 return 0;
1380         lt = l->type;
1381         if(lt == T)
1382                 return 0;
1383         if(lt->etype == TXXX || lt->etype > TUVLONG)
1384                 return 0;
1385         
1386         /*
1387          * Skip over the right casts to find the on-screen value.
1388          */
1389         if(r->op != OCONST)
1390                 return 0;
1391         while(r->oldop == OCAST && !r->xcast)
1392                 r = r->left;
1393         rt = r->type;
1394         if(rt == T)
1395                 return 0;
1396
1397         x.b = r->vconst;
1398         x.a = 0;
1399         if((rt->etype&1) && r->vconst < 0)      /* signed negative */
1400                 x.a = ~0ULL;
1401
1402         if((lt->etype&1)==0){
1403                 /* unsigned */
1404                 lo = big(0, 0);
1405                 if(lt->width == 8)
1406                         hi = big(0, ~0ULL);
1407                 else
1408                         hi = big(0, (1LL<<(l->type->width*8))-1);
1409         }else{
1410                 lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
1411                 hi = big(0, (1LL<<(l->type->width*8-1))-1);
1412         }
1413
1414         switch(op){
1415         case OLT:
1416         case OLO:
1417         case OGE:
1418         case OHS:
1419                 if(cmp(x, lo) <= 0)
1420                         goto useless;
1421                 if(cmp(x, add(hi, 1)) >= 0)
1422                         goto useless;
1423                 break;
1424         case OLE:
1425         case OLS:
1426         case OGT:
1427         case OHI:
1428                 if(cmp(x, add(lo, -1)) <= 0)
1429                         goto useless;
1430                 if(cmp(x, hi) >= 0)
1431                         goto useless;
1432                 break;
1433         case OEQ:
1434         case ONE:
1435                 /*
1436                  * Don't warn about comparisons if the expression
1437                  * is as wide as the value: the compiler-supplied casts
1438                  * will make both outcomes possible.
1439                  */
1440                 if(lt->width >= rt->width && debug['w'] < 2)
1441                         return 0;
1442                 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1443                         goto useless;
1444                 break;
1445         }
1446         return 0;
1447
1448 useless:
1449         if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1450                 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1451         else if(x.a == 0)
1452                 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1453         else
1454                 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1455         if(reverse)
1456                 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1457                         xbuf, cmps[relindex(n->op)], lt);
1458         else
1459                 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1460                         lt, cmps[relindex(n->op)], xbuf);
1461 if(debug['y']) prtree(n, "strange");
1462         warn(n, "useless or misleading comparison: %s", cmpbuf);
1463         return 0;
1464 }
1465