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(!sametype(t, n->type) && !mixedasop(t, n->type)) {
182 r = new1(OCAST, n->right, Z);
200 typeext1(l->type, r);
201 if(tcompat(n, l->type, r->type, tmul))
203 constas(n, l->type, r->type);
206 while(n->left->op == OCAST)
207 n->left = n->left->left;
208 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
209 r = new1(OCAST, n->right, Z);
214 if(typeu[n->type->etype]) {
232 if(tcompat(n, l->type, r->type, tand))
235 n->right = new1(OCAST, r, Z);
236 n->right->type = types[TINT];
237 if(typeu[n->type->etype]) {
255 if(tcompat(n, l->type, r->type, tand))
259 while(n->left->op == OCAST)
260 n->left = n->left->left;
261 if(!sametype(t, n->type) && !mixedasop(t, n->type)) {
262 r = new1(OCAST, n->right, Z);
267 if(typeu[n->type->etype]) {
283 if(tcompat(n, l->type, types[TINT], tadd))
286 if(n->type->etype == TIND)
287 if(n->type->link->width < 1) {
289 if(n->type->link->width < 1)
290 diag(n, "inc/dec of a void pointer");
303 if(tcompat(n, l->type, r->type, trel))
306 n->type = types[TINT];
318 typeext1(l->type, r);
319 typeext1(r->type, l);
320 if(tcompat(n, l->type, r->type, trel))
323 if(typeu[n->type->etype])
324 n->op = logrel[relindex(n->op)];
325 n->type = types[TINT];
331 if(o | tcom(r->right))
333 if(r->right->type->etype == TIND && vconst(r->left) == 0) {
334 r->left->type = r->right->type;
337 if(r->left->type->etype == TIND && vconst(r->right) == 0) {
338 r->right->type = r->left->type;
339 r->right->vconst = 0;
341 if(sametype(r->right->type, r->left->type)) {
342 r->type = r->right->type;
346 if(tcompat(r, r->left->type, r->right->type, trel))
358 if(tcompat(n, l->type, r->type, tadd))
369 if(tcompat(n, l->type, r->type, tsub))
383 if(tcompat(n, l->type, r->type, tmul))
386 if(typeu[n->type->etype]) {
402 if(tcompat(n, l->type, r->type, tand))
406 n->right = new1(OCAST, r, Z);
407 n->right->type = types[TINT];
408 if(typeu[n->type->etype])
421 if(tcompat(n, l->type, r->type, tand))
433 if(tcompat(n, l->type, r->type, tand))
436 if(typeu[n->type->etype])
447 l = new(OCONST, Z, Z);
449 l->type = types[TINT];
456 if(tcompat(n, l->type, r->type, tsub))
469 l = new(OCONST, Z, Z);
471 l->type = types[TINT];
478 if(tcompat(n, l->type, r->type, tsub))
492 l = new(OCONST, Z, Z);
494 l->type = types[TINT];
501 if(tcompat(n, l->type, r->type, tand))
512 if(tcompat(n, T, l->type, tnot))
514 n->type = types[TINT];
522 if(tcompat(n, T, l->type, tnot) |
523 tcompat(n, T, r->type, tnot))
525 n->type = types[TINT];
536 case OSIGN: /* extension signof(type) returns a hash */
538 if(l->op != OSTRING && l->op != OLSTRING)
542 diag(n, "signof bitfield");
549 if(n->type->width < 0) {
550 diag(n, "signof undefined type");
556 n->vconst = convvtox(signature(n->type), TULONG);
557 n->type = types[TULONG];
562 if(l->op != OSTRING && l->op != OLSTRING)
566 diag(n, "sizeof bitfield");
573 if(n->type->width <= 0) {
574 diag(n, "sizeof undefined type");
577 if(n->type->etype == TFUNC) {
578 diag(n, "sizeof function");
584 n->vconst = convvtox(n->type->width, TINT);
585 n->type = types[TINT];
592 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
593 l = new1(OIND, l, Z);
594 l->type = l->left->type->link;
597 if(tcompat(n, T, l->type, tfunct))
599 if(o | tcoma(l, r, l->type->down, 1))
601 n->type = l->type->link;
603 if(l->type->down == T || l->type->down->etype == TOLD) {
605 diag(n, "function args not checked: %F", l);
612 diag(n, "name not declared: %F", n);
615 if(n->type->etype == TENUM) {
617 n->type = n->sym->tenum;
618 if(!typefd[n->type->etype])
619 n->vconst = n->sym->vconst;
621 n->fconst = n->sym->fconst;
625 if(n->class == CEXREG) {
629 n->reg = n->sym->offset;
636 if(n->type->link != types[TRUNE]) {
640 outlstring(str, sizeof(Rune));
641 o = outlstring(0, 0);
645 n->xoffset = outlstring(n->rstring, n->type->width);
650 if(n->type->link != types[TCHAR]) {
658 n->xoffset = outstring(n->cstring, n->type->width);
668 if(tcompat(n, T, l->type, tdot))
680 diag(n, "address of a bit field");
683 if(l->op == OREGISTER) {
684 diag(n, "address of a register");
687 n->type = typ(TIND, l->type);
688 n->type->width = types[TIND]->width;
694 if(tcompat(n, T, l->type, tindir))
696 n->type = l->type->link;
711 if(typesu[t->etype] && t->tag)
712 diag(n, "structure not fully declared %s", t->tag->name);
714 diag(n, "structure not fully declared");
718 if(typeaf[t->etype]) {
722 warn(n, "address of array/func ignored");
729 l = new1(OXXX, Z, Z);
732 if(l->type->etype == TARRAY)
733 l->type = l->type->link;
737 n->type = typ(TIND, l->type);
738 n->type->width = types[TIND]->width;
747 tcoma(Node *l, Node *n, Type *t, int f)
753 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
756 if(t != T && !sametype(t, types[TVOID])) {
757 diag(n, "not enough function arguments: %F", l);
763 o = tcoma(l, n->left, t, 0);
769 return o | tcoma(l, n->right, t, 1);
772 tcoma(l, Z, t->down, 0);
773 if(tcom(n) || tcompat(n, T, n->type, targ))
775 if(sametype(t, types[TVOID])) {
776 diag(n, "too many function arguments: %F", l);
781 if(stcompat(nodproto, t, n->type, tasign)) {
782 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
798 switch(n->type->etype)
813 if(t != T && !sametype(t, n->type)) {
814 n1 = new1(OXXX, Z, Z);
832 t = dotsearch(n->sym, n->left->type->link, n, &o);
834 diag(n, "not a member of struct/union: %F", n);
845 Node *l, *r, **ar, **al;
849 if(n->type->etype != TSTRUCT) {
850 diag(n, "constructor must be a structure");
856 for(t = n->type->link; t != T; t = t->down) {
858 diag(n, "constructor list too short");
874 if(tcompat(n, t, r->type, tasign))
876 constas(n, t, r->type);
877 if(!e && !sametype(t, r->type)) {
878 r = new1(OCAST, r, Z);
884 diag(n, "constructor list too long");
895 diag(n, "not an l-value");
902 * hoist comma operators out of expressions
903 * (a,b) OP c => (a, b OP c)
904 * OP(a,b) => (a, OP b)
905 * a OP (b,c) => (b, a OP c)
909 comargs(Com *com, Node *n)
911 if(n != Z && n->op == OLIST){
912 n->left = comargs(com, n->left);
913 n->right = comargs(com, n->right);
915 return commas(com, n);
919 commas(Com *com, Node *n)
935 t = commas(com, n->left);
936 if(com->n >= nelem(com->t))
937 fatal(n, "comma list overflow");
938 com->t[com->n++] = t;
939 return commas(com, n->right);
942 n->left = commas(com, n->left);
943 n->right = comargs(com, n->right);
947 n->left = commas(com, n->left);
948 comma(n->right->left);
949 comma(n->right->right);
954 n->left = commas(com, n->left);
962 n->left = commas(com, n->left);
964 n->right = commas(com, n->right);
975 nn = commas(&com, n);
977 if(debug['y'])print("n=%d\n", com.n);
978 if(debug['y']) prtree(nn, "res");
982 if(debug['y']) prtree(com.t[com.n-1], "tree");
983 nn = new1(OXXX, Z, Z);
987 n->left = com.t[--com.n];
989 n->lineno = n->left->lineno;
991 if(debug['y']) prtree(n, "final");
993 fatal(n, "odd tree");
998 * (IND(ADDR x)) ==> x
999 * (ADDR(IND x)) ==> x
1000 * remove some zero operands
1001 * remove no op casts
1002 * evaluate constants
1034 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
1035 if(r->op == OCONST) {
1036 t = n->type->width * 8; /* bits per byte */
1037 if(r->vconst >= t || r->vconst < 0)
1038 warn(n, "stupid shift: %lld", r->vconst);
1044 if(l->op == OCONST) {
1049 if(nocast(l->type, n->type) &&
1050 (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1076 l->left->type = n->type;
1084 if(l->op == OADDR) {
1085 l->left->type = n->type;
1105 if(compar(n, 0) || compar(n, 1))
1115 if(vconst(l) == 0 && !side(r)) {
1120 if(vconst(r) == 0) {
1124 if(r->op == OCONST) {
1125 t = n->type->width * 8; /* bits per byte */
1126 if(r->vconst >= t || r->vconst <= -t)
1127 warn(n, "stupid shift: %lld", r->vconst);
1135 if(t == 0 && !side(r)) {
1145 if(t == 0 && !side(l)) {
1158 if(vconst(l) == 0 && !side(r)) {
1165 diag(n, "divide check");
1177 if(r->op == OCONST) {
1178 if(typefd[r->type->etype]) {
1180 r->fconst = -r->fconst;
1184 r->vconst = -r->vconst;
1195 if(vconst(l) == 0) {
1200 if(vconst(r) == 0) {
1209 if(vconst(l) == 0 && !side(r)) {
1213 if(vconst(r) == 0 && !side(l)) {
1219 /* look for commutative constant */
1220 if(r->op == OCONST) {
1221 if(l->op == n->op) {
1222 if(l->left->op == OCONST) {
1223 n->right = l->right;
1227 if(l->right->op == OCONST) {
1234 if(l->op == OCONST) {
1235 if(r->op == n->op) {
1236 if(r->left->op == OCONST) {
1241 if(r->right->op == OCONST) {
1252 if(vconst(l) == 0) {
1261 if(l->op == OCONST && l->vconst != 0) {
1285 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1286 static char *cmps[12] =
1288 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1291 /* 128-bit numbers */
1292 typedef struct Big Big;
1320 if(y > 0 && x.b < ob)
1322 if(y < 0 && x.b > ob)
1328 big(vlong a, uvlong b)
1338 compar(Node *n, int reverse)
1342 char xbuf[40], cmpbuf[50];
1347 * The point of this function is to diagnose comparisons
1348 * that can never be true or that look misleading because
1349 * of the `usual arithmetic conversions'. As an example
1350 * of the latter, if x is a ulong, then if(x <= -1) really means
1351 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1352 * what it says (but 8c compiles it wrong anyway).
1358 op = comrel[relindex(n->op)];
1366 * Skip over left casts to find out the original expression range.
1368 while(l->op == OCAST)
1373 if(l->op == ONAME && l->sym->type){
1375 if(lt->etype == TARRAY)
1380 if(lt->etype == TXXX || lt->etype > TUVLONG)
1384 * Skip over the right casts to find the on-screen value.
1388 while(r->oldop == OCAST && !r->xcast)
1396 if((rt->etype&1) && r->vconst < 0) /* signed negative */
1399 if((lt->etype&1)==0){
1405 hi = big(0, (1LL<<(l->type->width*8))-1);
1407 lo = big(~0ULL, -(1LL<<(l->type->width*8-1)));
1408 hi = big(0, (1LL<<(l->type->width*8-1))-1);
1418 if(cmp(x, add(hi, 1)) >= 0)
1425 if(cmp(x, add(lo, -1)) <= 0)
1433 * Don't warn about comparisons if the expression
1434 * is as wide as the value: the compiler-supplied casts
1435 * will make both outcomes possible.
1437 if(lt->width >= rt->width && debug['w'] < 2)
1439 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1446 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1447 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1449 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1451 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1453 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1454 xbuf, cmps[relindex(n->op)], lt);
1456 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1457 lt, cmps[relindex(n->op)], xbuf);
1458 if(debug['y']) prtree(n, "strange");
1459 warn(n, "useless or misleading comparison: %s", cmpbuf);