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(!mixedasop(t, n->type)) {
270 if(!sametype(t, n->type)) {
271 r = new1(OCAST, n->right, Z);
278 if(typeu[n->type->etype]) {
294 if(tcompat(n, l->type, types[TINT], tadd))
297 if(n->type->etype == TIND)
298 if(n->type->link->width < 1) {
300 if(n->type->link->width < 1)
301 diag(n, "inc/dec of a void pointer");
314 if(tcompat(n, l->type, r->type, trel))
317 n->type = types[TINT];
329 typeext1(l->type, r);
330 typeext1(r->type, l);
331 if(tcompat(n, l->type, r->type, trel))
334 if(typeu[n->type->etype])
335 n->op = logrel[relindex(n->op)];
336 n->type = types[TINT];
342 if(o | tcom(r->right))
344 if(r->right->type->etype == TIND && vconst(r->left) == 0) {
345 r->left->type = r->right->type;
348 if(r->left->type->etype == TIND && vconst(r->right) == 0) {
349 r->right->type = r->left->type;
350 r->right->vconst = 0;
352 if(sametype(r->right->type, r->left->type)) {
353 r->type = r->right->type;
357 if(tcompat(r, r->left->type, r->right->type, trel))
369 if(tcompat(n, l->type, r->type, tadd))
380 if(tcompat(n, l->type, r->type, tsub))
394 if(tcompat(n, l->type, r->type, tmul))
397 if(typeu[n->type->etype]) {
413 if(tcompat(n, l->type, r->type, tand))
417 n->right = new1(OCAST, r, Z);
418 n->right->type = types[TINT];
419 if(typeu[n->type->etype])
432 if(tcompat(n, l->type, r->type, tand))
444 if(tcompat(n, l->type, r->type, tand))
447 if(typeu[n->type->etype])
458 l = new(OCONST, Z, Z);
460 l->type = types[TINT];
467 if(tcompat(n, l->type, r->type, tsub))
480 l = new(OCONST, Z, Z);
482 l->type = types[TINT];
489 if(tcompat(n, l->type, r->type, tsub))
503 l = new(OCONST, Z, Z);
505 l->type = types[TINT];
512 if(tcompat(n, l->type, r->type, tand))
523 if(tcompat(n, T, l->type, tnot))
525 n->type = types[TINT];
533 if(tcompat(n, T, l->type, tnot) |
534 tcompat(n, T, r->type, tnot))
536 n->type = types[TINT];
547 case OSIGN: /* extension signof(type) returns a hash */
549 if(l->op != OSTRING && l->op != OLSTRING)
553 diag(n, "signof bitfield");
560 if(n->type->width < 0) {
561 diag(n, "signof undefined type");
567 n->vconst = convvtox(signature(n->type), TULONG);
568 n->type = types[TULONG];
573 if(l->op != OSTRING && l->op != OLSTRING)
577 diag(n, "sizeof bitfield");
584 if(n->type->width <= 0) {
585 diag(n, "sizeof undefined type");
588 if(n->type->etype == TFUNC) {
589 diag(n, "sizeof function");
595 n->vconst = convvtox(n->type->width, TINT);
596 n->type = types[TINT];
603 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
604 l = new1(OIND, l, Z);
605 l->type = l->left->type->link;
608 if(tcompat(n, T, l->type, tfunct))
610 if(o | tcoma(l, r, l->type->down, 1))
612 n->type = l->type->link;
614 if(l->type->down == T){
617 diag(n, "function not declared: %F", l);
618 }else if(l->type->down->etype == TOLD) {
620 diag(n, "function args not checked: %F", l);
628 diag(n, "name not declared: %F", n);
631 if(n->type->etype == TENUM) {
633 n->type = n->sym->tenum;
634 if(!typefd[n->type->etype])
635 n->vconst = n->sym->vconst;
637 n->fconst = n->sym->fconst;
641 if(n->class == CEXREG) {
645 n->reg = n->sym->offset;
652 if(n->type->link != types[TRUNE]) {
656 outlstring(str, sizeof(Rune));
657 o = outlstring(0, 0);
661 n->xoffset = outlstring(n->rstring, n->type->width);
666 if(n->type->link != types[TCHAR]) {
674 n->xoffset = outstring(n->cstring, n->type->width);
684 if(tcompat(n, T, l->type, tdot))
696 diag(n, "address of a bit field");
699 if(l->op == OREGISTER) {
700 diag(n, "address of a register");
703 n->type = typ(TIND, l->type);
704 n->type->width = types[TIND]->width;
710 if(tcompat(n, T, l->type, tindir))
712 n->type = l->type->link;
727 if(typesu[t->etype] && t->tag)
728 diag(n, "structure not fully declared %s", t->tag->name);
730 diag(n, "structure not fully declared");
734 if(typeaf[t->etype]) {
738 warn(n, "address of array/func ignored");
745 l = new1(OXXX, Z, Z);
748 if(l->type->etype == TARRAY)
749 l->type = l->type->link;
753 n->type = typ(TIND, l->type);
754 n->type->width = types[TIND]->width;
763 tcoma(Node *l, Node *n, Type *t, int f)
769 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
772 if(t != T && !sametype(t, types[TVOID])) {
773 diag(n, "not enough function arguments: %F", l);
779 o = tcoma(l, n->left, t, 0);
785 return o | tcoma(l, n->right, t, 1);
788 tcoma(l, Z, t->down, 0);
789 if(tcom(n) || tcompat(n, T, n->type, targ))
791 if(sametype(t, types[TVOID])) {
792 diag(n, "too many function arguments: %F", l);
797 if(stcompat(nodproto, t, n->type, tasign)) {
798 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
814 switch(n->type->etype)
829 if(t != T && !sametype(t, n->type)) {
830 n1 = new1(OXXX, Z, Z);
848 t = dotsearch(n->sym, n->left->type->link, n, &o);
850 diag(n, "not a member of struct/union: %F", n);
861 Node *l, *r, **ar, **al;
865 if(n->type->etype != TSTRUCT) {
866 diag(n, "constructor must be a structure");
872 for(t = n->type->link; t != T; t = t->down) {
874 diag(n, "constructor list too short");
890 if(tcompat(n, t, r->type, tasign))
892 constas(n, t, r->type);
893 if(!e && !sametype(t, r->type)) {
894 r = new1(OCAST, r, Z);
900 diag(n, "constructor list too long");
911 diag(n, "not an l-value");
918 * hoist comma operators out of expressions
919 * (a,b) OP c => (a, b OP c)
920 * OP(a,b) => (a, OP b)
921 * a OP (b,c) => (b, a OP c)
925 comargs(Com *com, Node *n)
927 if(n != Z && n->op == OLIST){
928 n->left = comargs(com, n->left);
929 n->right = comargs(com, n->right);
931 return commas(com, n);
935 commas(Com *com, Node *n)
951 t = commas(com, n->left);
952 if(com->n >= nelem(com->t))
953 fatal(n, "comma list overflow");
954 com->t[com->n++] = t;
955 return commas(com, n->right);
958 n->left = commas(com, n->left);
959 n->right = comargs(com, n->right);
963 n->left = commas(com, n->left);
964 comma(n->right->left);
965 comma(n->right->right);
970 n->left = commas(com, n->left);
978 n->left = commas(com, n->left);
980 n->right = commas(com, n->right);
991 nn = commas(&com, n);
993 if(debug['y'])print("n=%d\n", com.n);
994 if(debug['y']) prtree(nn, "res");
998 if(debug['y']) prtree(com.t[com.n-1], "tree");
999 nn = new1(OXXX, Z, Z);
1003 n->left = com.t[--com.n];
1005 n->lineno = n->left->lineno;
1007 if(debug['y']) prtree(n, "final");
1009 fatal(n, "odd tree");
1014 * (IND(ADDR x)) ==> x
1015 * (ADDR(IND x)) ==> x
1016 * remove some zero operands
1017 * remove no op casts
1018 * evaluate constants
1050 if(n->op == OASMOD || n->op == OASLMOD || n->op == OASDIV || n->op == OASLDIV)
1051 if(r->op == OCONST){
1052 if(!typefd[r->type->etype] && r->vconst == 0) {
1053 if(n->op == OASMOD || n->op == OASLMOD)
1054 diag(n, "modulo by zero");
1056 diag(n, "divide by zero");
1059 if(typefd[r->type->etype] && r->fconst == 0.) {
1060 if(n->op == OASMOD || n->op == OASLMOD)
1061 diag(n, "modulo by zero");
1063 diag(n, "divide by zero");
1067 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
1068 if(r->op == OCONST) {
1069 t = n->type->width * 8; /* bits per byte */
1070 if(r->vconst >= t || r->vconst < 0)
1071 warn(n, "stupid shift: %lld", r->vconst);
1076 if(n->type == types[TVOID] && !side(l)){
1082 warn(n, "32-bit unsigned complement zero-extended to 64 bits");
1089 if(l->op == OCONST) {
1094 if(nocast(l->type, n->type) &&
1095 (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1121 l->left->type = n->type;
1129 if(l->op == OADDR) {
1130 l->left->type = n->type;
1150 if(compar(n, 0) || compar(n, 1))
1160 if(vconst(l) == 0 && !side(r)) {
1165 if(vconst(r) == 0) {
1169 if(r->op == OCONST) {
1170 t = n->type->width * 8; /* bits per byte */
1171 if(r->vconst >= t || r->vconst <= -t)
1172 warn(n, "stupid shift: %lld", r->vconst);
1180 if(t == 0 && !side(r)) {
1190 if(t == 0 && !side(l)) {
1203 if(vconst(l) == 0 && !side(r)) {
1210 diag(n, "divide check");
1222 if(r->op == OCONST) {
1223 if(typefd[r->type->etype]) {
1225 r->fconst = -r->fconst;
1229 r->vconst = -r->vconst;
1240 if(vconst(l) == 0) {
1245 if(vconst(r) == 0) {
1254 if(vconst(l) == 0 && !side(r)) {
1258 if(vconst(r) == 0 && !side(l)) {
1264 /* look for commutative constant */
1265 if(r->op == OCONST) {
1266 if(l->op == n->op) {
1267 if(l->left->op == OCONST) {
1268 n->right = l->right;
1272 if(l->right->op == OCONST) {
1279 if(l->op == OCONST) {
1280 if(r->op == n->op) {
1281 if(r->left->op == OCONST) {
1286 if(r->right->op == OCONST) {
1297 if(vconst(l) == 0) {
1306 if(l->op == OCONST && l->vconst != 0) {
1330 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1331 static char *cmps[12] =
1333 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1336 /* 128-bit numbers */
1337 typedef struct Big Big;
1365 if(y > 0 && x.b < ob)
1367 if(y < 0 && x.b > ob)
1373 big(vlong a, uvlong b)
1383 compar(Node *n, int reverse)
1387 char xbuf[40], cmpbuf[50];
1392 * The point of this function is to diagnose comparisons
1393 * that can never be true or that look misleading because
1394 * of the `usual arithmetic conversions'. As an example
1395 * of the latter, if x is a ulong, then if(x <= -1) really means
1396 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1397 * what it says (but 8c compiles it wrong anyway).
1403 op = comrel[relindex(n->op)];
1411 * Skip over left casts to find out the original expression range.
1413 while(l->op == OCAST){
1416 if(lt == T || rt == T)
1418 if(lt->width < rt->width)
1420 if(lt->width == rt->width && ((lt->etype ^ rt->etype) & 1) != 0)
1427 if(lt == T || lt->etype == TXXX || lt->etype > TUVLONG)
1431 * Skip over the right casts to find the on-screen value.
1435 while(r->oldop == OCAST && !r->xcast)
1443 if((rt->etype&1) && r->vconst < 0) /* signed negative */
1448 lo = big(~0ULL, -(1LL<<(lt->width*8-1)));
1449 hi = big(0, (1LL<<(lt->width*8-1))-1);
1456 hi = big(0, (1LL<<(lt->width*8))-1);
1466 if(cmp(x, add(hi, 1)) >= 0)
1473 if(cmp(x, add(lo, -1)) <= 0)
1481 * Don't warn about comparisons if the expression
1482 * is as wide as the value: the compiler-supplied casts
1483 * will make both outcomes possible.
1485 if(lt->width >= rt->width && debug['w'] < 2)
1487 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1494 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1495 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1497 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1499 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1501 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1502 xbuf, cmps[relindex(n->op)], lt);
1504 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1505 lt, cmps[relindex(n->op)], xbuf);
1506 if(debug['y']) prtree(n, "strange");
1507 warn(n, "useless or misleading comparison: %s", cmpbuf);