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 && zpconst(r->left)) {
345 r->type = r->right->type;
346 r->left->type = r->right->type;
349 if(r->left->type->etype == TIND && zpconst(r->right)) {
350 r->type = r->left->type;
351 r->right->type = r->left->type;
352 r->right->vconst = 0;
354 if(sametype(r->right->type, r->left->type)) {
355 r->type = r->right->type;
359 if(tcompat(r, r->left->type, r->right->type, trel))
371 if(tcompat(n, l->type, r->type, tadd))
382 if(tcompat(n, l->type, r->type, tsub))
396 if(tcompat(n, l->type, r->type, tmul))
399 if(typeu[n->type->etype]) {
415 if(tcompat(n, l->type, r->type, tand))
419 n->right = new1(OCAST, r, Z);
420 n->right->type = types[TINT];
421 if(typeu[n->type->etype])
434 if(tcompat(n, l->type, r->type, tand))
446 if(tcompat(n, l->type, r->type, tand))
449 if(typeu[n->type->etype])
460 l = new(OCONST, Z, Z);
462 l->type = types[TINT];
469 if(tcompat(n, l->type, r->type, tsub))
482 l = new(OCONST, Z, Z);
484 l->type = types[TINT];
491 if(tcompat(n, l->type, r->type, tsub))
505 l = new(OCONST, Z, Z);
507 l->type = types[TINT];
514 if(tcompat(n, l->type, r->type, tand))
525 if(tcompat(n, T, l->type, tnot))
527 n->type = types[TINT];
535 if(tcompat(n, T, l->type, tnot) |
536 tcompat(n, T, r->type, tnot))
538 n->type = types[TINT];
549 case OSIGN: /* extension signof(type) returns a hash */
551 if(l->op != OSTRING && l->op != OLSTRING)
555 diag(n, "signof bitfield");
562 if(n->type->width < 0) {
563 diag(n, "signof undefined type");
569 n->vconst = convvtox(signature(n->type), TULONG);
570 n->type = types[TULONG];
575 if(l->op != OSTRING && l->op != OLSTRING)
579 diag(n, "sizeof bitfield");
586 if(n->type->width <= 0) {
587 diag(n, "sizeof undefined type");
590 if(n->type->etype == TFUNC) {
591 diag(n, "sizeof function");
597 n->vconst = convvtox(n->type->width, TINT);
598 n->type = types[TINT];
605 if(l->type->etype == TIND && l->type->link->etype == TFUNC) {
606 l = new1(OIND, l, Z);
607 l->type = l->left->type->link;
610 if(tcompat(n, T, l->type, tfunct))
612 if(o | tcoma(l, r, l->type->down, 1))
614 n->type = l->type->link;
616 if(l->type->down == T){
619 diag(n, "function not declared: %F", l);
620 }else if(l->type->down->etype == TOLD) {
622 diag(n, "function args not checked: %F", l);
630 diag(n, "name not declared: %F", n);
633 if(n->type->etype == TENUM) {
635 n->type = n->sym->tenum;
636 if(!typefd[n->type->etype])
637 n->vconst = n->sym->vconst;
639 n->fconst = n->sym->fconst;
643 if(n->class == CEXREG) {
647 n->reg = n->sym->offset;
654 if(n->type->link != types[TRUNE]) {
658 outlstring(str, sizeof(Rune));
659 o = outlstring(0, 0);
663 n->xoffset = outlstring(n->rstring, n->type->width);
668 if(n->type->link != types[TCHAR]) {
676 n->xoffset = outstring(n->cstring, n->type->width);
686 if(tcompat(n, T, l->type, tdot))
698 diag(n, "address of a bit field");
701 if(l->op == OREGISTER) {
702 diag(n, "address of a register");
705 n->type = typ(TIND, l->type);
706 n->type->width = types[TIND]->width;
712 if(tcompat(n, T, l->type, tindir))
714 n->type = l->type->link;
729 if(typesu[t->etype] && t->tag)
730 diag(n, "structure not fully declared %s", t->tag->name);
732 diag(n, "structure not fully declared");
736 if(typeaf[t->etype]) {
740 warn(n, "address of array/func ignored");
747 l = new1(OXXX, Z, Z);
750 if(l->type->etype == TARRAY)
751 l->type = l->type->link;
755 n->type = typ(TIND, l->type);
756 n->type->width = types[TIND]->width;
765 tcoma(Node *l, Node *n, Type *t, int f)
771 if(t->etype == TOLD || t->etype == TDOT) /* .../old in prototype */
774 if(t != T && !sametype(t, types[TVOID])) {
775 diag(n, "not enough function arguments: %F", l);
781 o = tcoma(l, n->left, t, 0);
787 return o | tcoma(l, n->right, t, 1);
790 tcoma(l, Z, t->down, 0);
791 if(tcom(n) || tcompat(n, T, n->type, targ))
793 if(sametype(t, types[TVOID])) {
794 diag(n, "too many function arguments: %F", l);
799 if(stcompat(nodproto, t, n->type, tasign)) {
800 diag(l, "argument prototype mismatch \"%T\" for \"%T\": %F",
816 switch(n->type->etype)
831 if(t != T && !sametype(t, n->type)) {
832 n1 = new1(OXXX, Z, Z);
850 t = dotsearch(n->sym, n->left->type->link, n, &o);
852 diag(n, "not a member of struct/union: %F", n);
863 Node *l, *r, **ar, **al;
867 if(n->type->etype != TSTRUCT) {
868 diag(n, "constructor must be a structure");
874 for(t = n->type->link; t != T; t = t->down) {
876 diag(n, "constructor list too short");
892 if(tcompat(n, t, r->type, tasign))
894 constas(n, t, r->type);
895 if(!e && !sametype(t, r->type)) {
896 r = new1(OCAST, r, Z);
902 diag(n, "constructor list too long");
913 diag(n, "not an l-value");
920 * hoist comma operators out of expressions
921 * (a,b) OP c => (a, b OP c)
922 * OP(a,b) => (a, OP b)
923 * a OP (b,c) => (b, a OP c)
927 comargs(Com *com, Node *n)
929 if(n != Z && n->op == OLIST){
930 n->left = comargs(com, n->left);
931 n->right = comargs(com, n->right);
933 return commas(com, n);
937 commas(Com *com, Node *n)
953 t = commas(com, n->left);
954 if(com->n >= nelem(com->t))
955 fatal(n, "comma list overflow");
956 com->t[com->n++] = t;
957 return commas(com, n->right);
960 n->left = commas(com, n->left);
961 n->right = comargs(com, n->right);
965 n->left = commas(com, n->left);
966 comma(n->right->left);
967 comma(n->right->right);
972 n->left = commas(com, n->left);
980 n->left = commas(com, n->left);
982 n->right = commas(com, n->right);
993 nn = commas(&com, n);
995 if(debug['y'])print("n=%d\n", com.n);
996 if(debug['y']) prtree(nn, "res");
1000 if(debug['y']) prtree(com.t[com.n-1], "tree");
1001 nn = new1(OXXX, Z, Z);
1005 n->left = com.t[--com.n];
1007 n->lineno = n->left->lineno;
1009 if(debug['y']) prtree(n, "final");
1011 fatal(n, "odd tree");
1016 * (IND(ADDR x)) ==> x
1017 * (ADDR(IND x)) ==> x
1018 * remove some zero operands
1019 * remove no op casts
1020 * evaluate constants
1021 * Note: ccom may be called on the same node
1054 if(n->op == OASMOD || n->op == OASLMOD || n->op == OASDIV || n->op == OASLDIV)
1055 if(r->op == OCONST){
1056 if(!typefd[r->type->etype] && r->vconst == 0) {
1057 if(n->op == OASMOD || n->op == OASLMOD)
1058 diag(n, "modulo by zero");
1060 diag(n, "divide by zero");
1063 if(typefd[r->type->etype] && r->fconst == 0.) {
1064 if(n->op == OASMOD || n->op == OASLMOD)
1065 diag(n, "modulo by zero");
1067 diag(n, "divide by zero");
1071 if(n->op == OASLSHR || n->op == OASASHR || n->op == OASASHL)
1072 if(r->op == OCONST) {
1073 t = n->type->width * 8; /* bits per byte */
1074 if(r->vconst >= t || r->vconst < 0)
1075 warn(n, "stupid shift: %lld", r->vconst);
1080 if(n->type == types[TVOID] && !side(l)){
1087 warn(n, "32-bit unsigned complement zero-extended to 64 bits");
1094 if(l->op == OCONST) {
1099 if(nocast(l->type, n->type) &&
1100 (!typefd[l->type->etype] || typeu[l->type->etype] && typeu[n->type->etype])) {
1126 l->left->type = n->type;
1134 if(l->op == OADDR) {
1135 l->left->type = n->type;
1155 if(compar(n, 0) || compar(n, 1))
1165 if(vconst(l) == 0 && !side(r)) {
1170 if(vconst(r) == 0) {
1174 if(r->op == OCONST) {
1175 t = n->type->width * 8; /* bits per byte */
1176 if(r->vconst >= t || r->vconst <= -t)
1177 warn(n, "stupid shift: %lld", r->vconst);
1185 if(t == 0 && !side(r)) {
1195 if(t == 0 && !side(l)) {
1208 if(vconst(l) == 0 && !side(r)) {
1215 diag(n, "divide check");
1227 if(r->op == OCONST) {
1228 if(typefd[r->type->etype]) {
1230 r->fconst = -r->fconst;
1234 r->vconst = -r->vconst;
1245 if(vconst(l) == 0) {
1250 if(vconst(r) == 0) {
1259 if(vconst(l) == 0 && !side(r)) {
1263 if(vconst(r) == 0 && !side(l)) {
1269 /* look for commutative constant */
1270 if(r->op == OCONST) {
1271 if(l->op == n->op) {
1272 if(l->left->op == OCONST) {
1273 n->right = l->right;
1277 if(l->right->op == OCONST) {
1284 if(l->op == OCONST) {
1285 if(r->op == n->op) {
1286 if(r->left->op == OCONST) {
1291 if(r->right->op == OCONST) {
1302 if(vconst(l) == 0) {
1311 if(l->op == OCONST && l->vconst != 0) {
1335 /* OEQ, ONE, OLE, OLS, OLT, OLO, OGE, OHS, OGT, OHI */
1336 static char *cmps[12] =
1338 "==", "!=", "<=", "<=", "<", "<", ">=", ">=", ">", ">",
1341 /* 128-bit numbers */
1342 typedef struct Big Big;
1370 if(y > 0 && x.b < ob)
1372 if(y < 0 && x.b > ob)
1378 big(vlong a, uvlong b)
1388 compar(Node *n, int reverse)
1392 char xbuf[40], cmpbuf[50];
1397 * The point of this function is to diagnose comparisons
1398 * that can never be true or that look misleading because
1399 * of the `usual arithmetic conversions'. As an example
1400 * of the latter, if x is a ulong, then if(x <= -1) really means
1401 * if(x <= 0xFFFFFFFF), while if(x <= -1LL) really means
1402 * what it says (but 8c compiles it wrong anyway).
1408 op = comrel[relindex(n->op)];
1416 * Skip over left casts to find out the original expression range.
1418 while(l->op == OCAST){
1421 if(lt == T || rt == T)
1423 if(lt->width < rt->width)
1425 if(lt->width == rt->width && ((lt->etype ^ rt->etype) & 1) != 0)
1432 if(lt == T || lt->etype == TXXX || lt->etype > TUVLONG)
1436 * Skip over the right casts to find the on-screen value.
1440 while(r->oldop == OCAST && !r->xcast)
1448 if((rt->etype&1) && r->vconst < 0) /* signed negative */
1453 lo = big(~0ULL, -(1LL<<(lt->width*8-1)));
1454 hi = big(0, (1LL<<(lt->width*8-1))-1);
1461 hi = big(0, (1LL<<(lt->width*8))-1);
1471 if(cmp(x, add(hi, 1)) >= 0)
1478 if(cmp(x, add(lo, -1)) <= 0)
1486 * Don't warn about comparisons if the expression
1487 * is as wide as the value: the compiler-supplied casts
1488 * will make both outcomes possible.
1490 if(lt->width >= rt->width && debug['w'] < 2)
1492 if(cmp(x, lo) < 0 || cmp(x, hi) > 0)
1499 if((x.a==0 && x.b<=9) || (x.a==~0LL && x.b >= -9ULL))
1500 snprint(xbuf, sizeof xbuf, "%lld", x.b);
1502 snprint(xbuf, sizeof xbuf, "%#llux", x.b);
1504 snprint(xbuf, sizeof xbuf, "%#llx", x.b);
1506 snprint(cmpbuf, sizeof cmpbuf, "%s %s %T",
1507 xbuf, cmps[relindex(n->op)], lt);
1509 snprint(cmpbuf, sizeof cmpbuf, "%T %s %s",
1510 lt, cmps[relindex(n->op)], xbuf);
1511 if(debug['y']) prtree(n, "strange");
1512 warn(n, "useless or misleading comparison: %s", cmpbuf);