3 typedef struct Com Com;
10 int compar(Node*, int);
11 static void comma(Node*);
12 static Node* commas(Com*, Node*);
24 prtree(n, "pre complex");
31 prtree(n, "t complex");
35 prtree(n, "c complex");
39 prtree(n, "a complex");
43 prtree(n, "x complex");
48 * evaluate lvalues (addable == 1)
61 return tcomo(n, ADDROF);
81 diag(n, "unknown op in type complex: %O", n->op);
86 * tcom has already been called on this subtree
96 if(n->type->width == types[TLONG]->width) {
97 if(tcomo(l, ADDROF|CASTOF))
104 if(tcompat(n, l->type, n->type, tcast))
110 if(n->type->etype != TVOID)
111 warn(n, "null return of a typed function");
117 if(tcompat(n, n->type, l->type, tasign))
119 constas(n, n->type, l->type);
120 if(!sametype(n->type, l->type)) {
121 l = new1(OCAST, l, Z);
127 case OASI: /* same as as, but no test for const */
134 if(tlvalue(l) || tcompat(n, l->type, r->type, tasign))
136 if(!sametype(l->type, r->type)) {
137 r = new1(OCAST, r, Z);
153 if(tcompat(n, l->type, r->type, tasign))
155 constas(n, l->type, r->type);
156 if(!sametype(l->type, r->type)) {
157 r = new1(OCAST, r, Z);
173 typeext1(l->type, r);
174 if(tcompat(n, l->type, r->type, tasadd))
176 constas(n, l->type, r->type);
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);
203 typeext1(l->type, r);
204 if(tcompat(n, l->type, r->type, tmul))
206 constas(n, l->type, r->type);
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);
222 if(typeu[n->type->etype]) {
240 if(tcompat(n, l->type, r->type, tand))
243 n->right = new1(OCAST, r, Z);
244 n->right->type = types[TINT];
245 if(typeu[n->type->etype]) {
263 if(tcompat(n, l->type, r->type, tand))
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);
275 if(typeu[n->type->etype]) {
291 if(tcompat(n, l->type, types[TINT], tadd))
294 if(n->type->etype == TIND)
295 if(n->type->link->width < 1) {
297 if(n->type->link->width < 1)
298 diag(n, "inc/dec of a void pointer");
311 if(tcompat(n, l->type, r->type, trel))
314 n->type = types[TINT];
326 typeext1(l->type, r);
327 typeext1(r->type, l);
328 if(tcompat(n, l->type, r->type, trel))
331 if(typeu[n->type->etype])
332 n->op = logrel[relindex(n->op)];
333 n->type = types[TINT];
339 if(o | tcom(r->right))
341 if(r->right->type->etype == TIND && vconst(r->left) == 0) {
342 r->left->type = r->right->type;
345 if(r->left->type->etype == TIND && vconst(r->right) == 0) {
346 r->right->type = r->left->type;
347 r->right->vconst = 0;
349 if(sametype(r->right->type, r->left->type)) {
350 r->type = r->right->type;
354 if(tcompat(r, r->left->type, r->right->type, trel))
366 if(tcompat(n, l->type, r->type, tadd))
377 if(tcompat(n, l->type, r->type, tsub))
391 if(tcompat(n, l->type, r->type, tmul))
394 if(typeu[n->type->etype]) {
410 if(tcompat(n, l->type, r->type, tand))
414 n->right = new1(OCAST, r, Z);
415 n->right->type = types[TINT];
416 if(typeu[n->type->etype])
429 if(tcompat(n, l->type, r->type, tand))
441 if(tcompat(n, l->type, r->type, tand))
444 if(typeu[n->type->etype])
455 l = new(OCONST, Z, Z);
457 l->type = types[TINT];
464 if(tcompat(n, l->type, r->type, tsub))
477 l = new(OCONST, Z, Z);
479 l->type = types[TINT];
486 if(tcompat(n, l->type, r->type, tsub))
500 l = new(OCONST, Z, Z);
502 l->type = types[TINT];
509 if(tcompat(n, l->type, r->type, tand))
520 if(tcompat(n, T, l->type, tnot))
522 n->type = types[TINT];
530 if(tcompat(n, T, l->type, tnot) |
531 tcompat(n, T, r->type, tnot))
533 n->type = types[TINT];
544 case OSIGN: /* extension signof(type) returns a hash */
546 if(l->op != OSTRING && l->op != OLSTRING)
550 diag(n, "signof bitfield");
557 if(n->type->width < 0) {
558 diag(n, "signof undefined type");
564 n->vconst = convvtox(signature(n->type), TULONG);
565 n->type = types[TULONG];
570 if(l->op != OSTRING && l->op != OLSTRING)
574 diag(n, "sizeof bitfield");
581 if(n->type->width <= 0) {
582 diag(n, "sizeof undefined type");
585 if(n->type->etype == TFUNC) {
586 diag(n, "sizeof function");
592 n->vconst = convvtox(n->type->width, TINT);
593 n->type = types[TINT];
600 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
601 l = new1(OIND, l, Z);
602 l->type = l->left->type->link;
605 if(tcompat(n, T, l->type, tfunct))
607 if(o | tcoma(l, r, l->type->down, 1))
609 n->type = l->type->link;
611 if(l->type->down == T || l->type->down->etype == TOLD) {
613 diag(n, "function args not checked: %F", l);
620 diag(n, "name not declared: %F", n);
623 if(n->type->etype == TENUM) {
625 n->type = n->sym->tenum;
626 if(!typefd[n->type->etype])
627 n->vconst = n->sym->vconst;
629 n->fconst = n->sym->fconst;
633 if(n->class == CEXREG) {
637 n->reg = n->sym->offset;
644 if(n->type->link != types[TRUNE]) {
648 outlstring(str, sizeof(Rune));
649 o = outlstring(0, 0);
653 n->xoffset = outlstring(n->rstring, n->type->width);
658 if(n->type->link != types[TCHAR]) {
666 n->xoffset = outstring(n->cstring, n->type->width);
676 if(tcompat(n, T, l->type, tdot))
688 diag(n, "address of a bit field");
691 if(l->op == OREGISTER) {
692 diag(n, "address of a register");
695 n->type = typ(TIND, l->type);
696 n->type->width = types[TIND]->width;
702 if(tcompat(n, T, l->type, tindir))
704 n->type = l->type->link;
719 if(typesu[t->etype] && t->tag)
720 diag(n, "structure not fully declared %s", t->tag->name);
722 diag(n, "structure not fully declared");
726 if(typeaf[t->etype]) {
730 warn(n, "address of array/func ignored");
737 l = new1(OXXX, Z, Z);
740 if(l->type->etype == TARRAY)
741 l->type = l->type->link;
745 n->type = typ(TIND, l->type);
746 n->type->width = types[TIND]->width;
755 tcoma(Node *l, Node *n, Type *t, int f)
761 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
764 if(t != T && !sametype(t, types[TVOID])) {
765 diag(n, "not enough function arguments: %F", l);
771 o = tcoma(l, n->left, t, 0);
777 return o | tcoma(l, n->right, t, 1);
780 tcoma(l, Z, t->down, 0);
781 if(tcom(n) || tcompat(n, T, n->type, targ))
783 if(sametype(t, types[TVOID])) {
784 diag(n, "too many function arguments: %F", l);
789 if(stcompat(nodproto, t, n->type, tasign)) {
790 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
806 switch(n->type->etype)
821 if(t != T && !sametype(t, n->type)) {
822 n1 = new1(OXXX, Z, Z);
840 t = dotsearch(n->sym, n->left->type->link, n, &o);
842 diag(n, "not a member of struct/union: %F", n);
853 Node *l, *r, **ar, **al;
857 if(n->type->etype != TSTRUCT) {
858 diag(n, "constructor must be a structure");
864 for(t = n->type->link; t != T; t = t->down) {
866 diag(n, "constructor list too short");
882 if(tcompat(n, t, r->type, tasign))
884 constas(n, t, r->type);
885 if(!e && !sametype(t, r->type)) {
886 r = new1(OCAST, r, Z);
892 diag(n, "constructor list too long");
903 diag(n, "not an l-value");
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)
917 comargs(Com *com, Node *n)
919 if(n != Z && n->op == OLIST){
920 n->left = comargs(com, n->left);
921 n->right = comargs(com, n->right);
923 return commas(com, n);
927 commas(Com *com, Node *n)
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);
950 n->left = commas(com, n->left);
951 n->right = comargs(com, n->right);
955 n->left = commas(com, n->left);
956 comma(n->right->left);
957 comma(n->right->right);
962 n->left = commas(com, n->left);
970 n->left = commas(com, n->left);
972 n->right = commas(com, n->right);
983 nn = commas(&com, n);
985 if(debug['y'])print("n=%d\n", com.n);
986 if(debug['y']) prtree(nn, "res");
990 if(debug['y']) prtree(com.t[com.n-1], "tree");
991 nn = new1(OXXX, Z, Z);
995 n->left = com.t[--com.n];
997 n->lineno = n->left->lineno;
999 if(debug['y']) prtree(n, "final");
1001 fatal(n, "odd tree");
1006 * (IND(ADDR x)) ==> x
1007 * (ADDR(IND x)) ==> x
1008 * remove some zero operands
1009 * remove no op casts
1010 * evaluate constants
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);
1052 if(l->op == OCONST) {
1057 if(nocast(l->type, n->type) &&
1058 (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1084 l->left->type = n->type;
1092 if(l->op == OADDR) {
1093 l->left->type = n->type;
1113 if(compar(n, 0) || compar(n, 1))
1123 if(vconst(l) == 0 && !side(r)) {
1128 if(vconst(r) == 0) {
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);
1143 if(t == 0 && !side(r)) {
1153 if(t == 0 && !side(l)) {
1166 if(vconst(l) == 0 && !side(r)) {
1173 diag(n, "divide check");
1185 if(r->op == OCONST) {
1186 if(typefd[r->type->etype]) {
1188 r->fconst = -r->fconst;
1192 r->vconst = -r->vconst;
1203 if(vconst(l) == 0) {
1208 if(vconst(r) == 0) {
1217 if(vconst(l) == 0 && !side(r)) {
1221 if(vconst(r) == 0 && !side(l)) {
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;
1235 if(l->right->op == OCONST) {
1242 if(l->op == OCONST) {
1243 if(r->op == n->op) {
1244 if(r->left->op == OCONST) {
1249 if(r->right->op == OCONST) {
1260 if(vconst(l) == 0) {
1269 if(l->op == OCONST && l->vconst != 0) {
1293 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1294 static char *cmps[12] =
1296 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1299 /* 128-bit numbers */
1300 typedef struct Big Big;
1328 if(y > 0 && x.b < ob)
1330 if(y < 0 && x.b > ob)
1336 big(vlong a, uvlong b)
1346 compar(Node *n, int reverse)
1350 char xbuf[40], cmpbuf[50];
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).
1366 op = comrel[relindex(n->op)];
1374 * Skip over left casts to find out the original expression range.
1376 while(l->op == OCAST)
1383 if(lt->etype == TXXX || lt->etype > TUVLONG)
1387 * Skip over the right casts to find the on-screen value.
1391 while(r->oldop == OCAST && !r->xcast)
1399 if((rt->etype&1) && r->vconst < 0) /* signed negative */
1402 if((lt->etype&1)==0){
1408 hi = big(0, (1LL<<(l->type->width*8))-1);
1410 lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
1411 hi = big(0, (1LL<<(l->type->width*8-1))-1);
1421 if(cmp(x, add(hi, 1)) >= 0)
1428 if(cmp(x, add(lo, -1)) <= 0)
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.
1440 if(lt->width >= rt->width && debug['w'] < 2)
1442 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1449 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1450 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1452 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1454 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1456 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1457 xbuf, cmps[relindex(n->op)], lt);
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);