4 dodecl(void (*f)(int,Type*,Sym*), int c, Type *t, Node *n)
17 diag(n, "unknown declarator: %O", n->op);
31 diag(n, "array size must be a positive constant");
34 t->width = v * t->link->width;
55 lastfield = n1->vconst;
57 diag(n, "field width must be non-negative constant");
64 diag(n, "zero width named field");
68 if(!typei[t->etype]) {
69 diag(n, "field type must be int-like");
73 if(lastfield > tfield->width*8) {
74 diag(n, "field width larger than field unit");
78 if(lastbit > tfield->width*8) {
90 if(s->class == CLOCAL)
95 n->xoffset = s->offset;
99 n->etype = n->type->etype;
103 s->varlineno = lineno;
115 if(s->class != CLOCAL)
117 snprint(symb, NSYMB, "%s$%d", s->name, s->block);
119 if(s1->class != CSTATIC) {
121 s1->offset = s->offset;
122 s1->block = s->block;
129 * make a copy of a typedef
130 * the problem is to split out incomplete
131 * arrays so that it is in the variable
132 * rather than the typedef.
147 (et == TARRAY && t->width == 0)) {
156 doinit(Sym *s, Type *t, long o, Node *a)
162 if(s->class == CEXTERN) {
168 print("t = %T; o = %ld; n = %s\n", t, o, s->name);
169 prtree(a, "doinit value");
178 a = init1(s, t, o, 0);
180 diag(initlist, "more initializers than structure: %s",
188 * get next major operator,
189 * dont advance initlist.
209 * consume and return next element on
210 * initlist. expand strings.
228 b = new(OCONST, Z, Z);
229 b->type = a->type->link;
230 if(a->op == OSTRING) {
231 b->vconst = convvtox(*a->cstring, TCHAR);
234 if(a->op == OLSTRING) {
235 b->vconst = convvtox(*a->rstring, TRUNE);
238 a->type->width -= b->type->width;
239 if(a->type->width <= 0)
248 isstruct(Node *a, Type *t)
255 if(n && n->type && sametype(n->type, t))
265 n = new(ODOTDOT, Z, Z);
269 * ODOTDOT is a flag for tcom
270 * a second tcom will not be performed
279 if(sametype(n->type, t))
285 init1(Sym *s, Type *t, long o, int exflag)
287 Node *a, *l, *r, nod;
296 print("t = %T; o = %ld; n = %s\n", t, o, s->name);
297 prtree(a, "init1 value");
300 if(exflag && a->op == OINIT)
301 return doinit(s, t, o, nextinit());
305 diag(Z, "unknown type in initialization: %T to: %s", t, s->name);
322 if(a->op == OARRAY || a->op == OELEM)
330 diag(Z, "cannot initialize bitfields");
331 if(s->class == CAUTO) {
332 l = new(ONAME, Z, Z);
337 l->etype = s->type->etype;
338 l->xoffset = s->offset + o;
349 if(a->op == OCONST) {
350 if(vconst(a) && t->etype == TIND && a->type && a->type->etype != TIND){
351 diag(a, "initialize pointer to an integer: %s", s->name);
354 if(!sametype(a->type, t)) {
355 /* hoop jumping to save malloc */
357 nodcast = new(OCAST, Z, Z);
361 nod.lineno = a->lineno;
366 if(a->op != OCONST) {
367 diag(a, "initializer is not a constant: %s",
375 if(t->etype == TIND) {
377 warn(a, "CAST in initialization ignored");
378 if(!sametype(t, a->type))
379 diag(a, "initialization of incompatible pointers: %s\n%T and %T",
380 s->name, t, a->type);
383 while(a->op == OCAST)
389 warn(a, "initialize pointer to an integer: %s", s->name);
394 * Constants will be folded before this point, which just leaves offsets
399 if(l->op == OADDR && r->op == OCONST || r->op == OADDR && l->op == OCONST)
402 diag(a, "initializer is not a constant: %s", s->name);
407 gextern(s, a, o, t->width);
413 if(a->op == OSTRING || a->op == OLSTRING)
414 if(typei[t->link->etype]) {
416 * get rid of null if sizes match exactly
420 so = a->type->width/a->type->link->width;
423 diag(a, "string initialization larger than array");
424 a->type->width -= a->type->link->width;
428 * arrange strings to be expanded
429 * inside OINIT braces.
431 a = new(OUSED, a, Z);
432 return doinit(s, t, o, a);
439 * peek ahead for element initializer
444 if(a->op == OELEM && t->link->etype != TSTRUCT)
446 if(a->op == OARRAY) {
452 if(r->op != OCONST) {
453 diag(r, "initializer subscript must be constant");
458 if(e < 0 || e*w >= t->width) {
459 diag(a, "initialization index out of range: %ld", e);
470 r = init1(s, t->link, o+so, 1);
481 * peek ahead to find type of rhs.
482 * if its a structure, then treat
483 * this element as a variable
484 * rather than an aggregate.
490 diag(Z, "incomplete structure: %s", s->name);
496 for(t1 = t->link; t1 != T; t1 = t1->down) {
497 if(a->op == OARRAY && t1->etype != TARRAY)
500 if(t1->sym != a->sym)
504 r = init1(s, t1, o+t1->offset, 1);
512 if(a && a->op == OELEM)
513 diag(a, "structure element not found %F", a);
519 newlist(Node *l, Node *r)
525 return new(OLIST, l, r);
540 for(l = t->link; l != T; l = l->down) {
543 l->shift = -l->shift;
544 w = round(w, tfield->width);
551 l->width == 0 && l->down != T)
553 diag(Z, "incomplete structure element: %s",
556 diag(Z, "incomplete structure element");
557 w = align(w, l, Ael1);
559 w = align(w, l, Ael2);
562 w = align(w, t, Asu2);
571 for(l = t->link; l != T; l = l->down) {
574 diag(Z, "incomplete union element: %s",
577 diag(Z, "incomplete union element");
580 o = align(align(0, l, Ael1), l, Ael2);
584 w = align(w, t, Asu2);
591 diag(Z, "unknown type in sualign: %T", t);
601 if(w <= 0 || w > 8) {
602 diag(Z, "rounding by %d", w);
620 tl = ofnproto(n->left);
621 tr = ofnproto(n->right);
628 t = copytyp(n->sym->type);
639 argmark(Node *n, int pass)
643 autoffset = align(0, thisfn->link, Aarg0);
645 for(; n->left != Z; n = n->left) {
646 if(n->op != OFUNC || n->left->op != ONAME)
648 walkparam(n->right, pass);
649 if(pass != 0 && anyproto(n->right) == OLDPROTO) {
650 t = typ(TFUNC, n->left->sym->type->link);
651 t->down = typ(TOLD, T);
652 t->down->down = ofnproto(n->right);
653 tmerge(t, n->left->sym);
654 n->left->sym->type = t;
663 walkparam(Node *n, int pass)
668 if(n != Z && n->op == OPROTO && n->left == Z && n->type == types[TVOID])
676 diag(n, "argument not a name/prototype: %O", n->op);
680 walkparam(n->left, pass);
685 for(n1 = n; n1 != Z; n1=n1->left)
686 if(n1->op == ONAME) {
693 dodecl(pdecl, CPARAM, n->type, n->left);
701 * allow no name in argument declaration
702 diag(Z, "no name in argument declaration");
706 dodecl(NODECL, CPARAM, n->type, n->left);
707 pdecl(CPARAM, lastdcl, S);
720 if(s->offset != -1) {
723 firstargtype = s->type;
725 autoffset = align(autoffset, s->type, Aarg1);
726 s->offset = autoffset;
727 autoffset = align(autoffset, s->type, Aarg2);
729 dodecl(pdecl, CXXX, types[TINT], n);
742 d->offset = autoffset;
758 diag(Z, "pop off dcl stack");
765 autoffset = d->offset;
771 print("revert1 \"%s\"\n", s->name);
773 nearln = s->varlineno;
774 if(s->class == CAUTO)
775 warn(Z, "auto declared and not used: %s", s->name);
776 if(s->class == CPARAM)
777 warn(Z, "param declared and not used: %s", s->name);
779 if(s->type && (s->type->garb & GVOLATILE)) {
780 n1 = new(ONAME, Z, Z);
785 n1->etype = n1->type->etype;
786 n1->xoffset = s->offset;
787 n1->class = s->class;
789 n1 = new(OADDR, n1, Z);
790 n1 = new(OUSED, n1, Z);
794 n = new(OLIST, n1, n);
798 s->offset = d->offset;
800 s->varlineno = d->varlineno;
806 print("revert2 \"%s\"\n", s->name);
808 s->sueblock = d->block;
813 print("revert3 \"%s\"\n", s->name);
814 if(s->label && s->label->addable == 0)
815 warn(s->label, "label declared and not used \"%s\"", s->name);
828 r = anyproto(n->right);
829 if(r == 0 || (r & OLDPROTO)) {
831 diag(n, "mixed ansi/old function declaration: %F", n->left);
834 return fnproto1(n->right);
849 r |= anyproto(n->left);
855 return r | ANSIPROTO;
869 t = fnproto1(n->left);
871 t->down = fnproto1(n->right);
876 dodecl(NODECL, CXXX, n->type, n->left);
879 *t = *paramconv(lastdcl, 1);
883 diag(n, "incomplete argument prototype");
889 diag(n, "unknown op in fnproto");
896 print("decl \"%s\": C=%s [B=%d:O=%ld] T=%T\n",
897 s->name, cnames[s->class], s->block, s->offset, s->type);
905 d = alloc(sizeof(*d));
921 d->offset = s->offset;
923 d->varlineno = s->varlineno;
929 sametype(Type *t1, Type *t2)
934 return rsametype(t1, t2, 5, 1);
938 rsametype(Type *t1, Type *t2, int n, int f)
946 if(t1 == T || t2 == T)
954 if(!rsametype(t1->link, t2->link, n, 0))
958 while(t1 != T && t2 != T) {
959 if(t1->etype == TOLD) {
963 if(t2->etype == TOLD) {
967 while(t1 != T || t2 != T) {
968 if(!rsametype(t1, t2, n, 0))
978 if(t1->width != t2->width && t1->width != 0 && t2->width != 0)
985 if(t1 != t2 && t1->link == T && t2->link == T){
986 /* structs with missing or different tag names aren't considered equal */
987 if(t1->tag == nil || t2->tag == nil ||
988 strcmp(t1->tag->name, t2->tag->name) != 0)
996 if(!rsametype(t1, t2, n, 0))
1004 if((f || !debug['V']) && et == TIND) {
1005 if(t1 != T && t1->etype == TVOID)
1007 if(t2 != T && t2->etype == TVOID)
1013 typedef struct Typetab Typetab;
1021 sigind(Type *t, Typetab *tt)
1024 Type **a, **na, **p, **e;
1029 /* linear search seems ok */
1030 for(p = a ; p < e; p++)
1034 na = malloc((n+16)*sizeof(Type*));
1035 memmove(na, a, n*sizeof(Type*));
1044 signat(Type *t, Typetab *tt)
1051 for(; t; t=t->link) {
1052 s = s*thash1 + thash[t->etype];
1053 if(t->garb&GINCOMPLETE)
1059 s = s*thash2 + 0; /* was t->width */
1062 for(t1=t->down; t1; t1=t1->down)
1063 s = s*thash3 + signat(t1, tt);
1067 if((i = sigind(t, tt)) >= 0){
1071 for(t1=t->link; t1; t1=t1->down)
1072 s = s*thash3 + signat(t1, tt);
1100 if(s->sig == SIGINTERN)
1102 if((t = s->type) == T)
1113 if(typesu[t->etype])
1114 if(t->link == T && t->tag && t->tag->suetag) {
1115 t->link = t->tag->suetag->link;
1116 t->width = t->tag->suetag->width;
1121 dotag(Sym *s, int et, int bn)
1125 if(bn != 0 && bn != s->sueblock) {
1129 d->type = s->suetag;
1130 d->block = s->sueblock;
1133 if(s->suetag == T) {
1134 s->suetag = typ(et, T);
1135 s->sueblock = autobn;
1137 if(s->suetag->etype != et)
1138 diag(Z, "tag used for more than one type: %s",
1140 if(s->suetag->tag == S)
1146 dcllabel(Sym *s, int f)
1155 diag(Z, "label reused: %s", s->name);
1156 n->complex = 1; // declared
1158 n->addable = 1; // used
1174 n = new(OXXX, Z, Z);
1186 paramconv(Type *t, int f)
1191 t = typ(TIND, t->link);
1192 t->width = types[TIND]->width;
1197 t->width = types[TIND]->width;
1221 adecl(int c, Type *t, Sym *s)
1226 if(t->etype == TFUNC) {
1231 if(c == CAUTO || c == CEXREG)
1232 diag(Z, "function cannot be %s %s", cnames[c], s->name);
1237 if(s->class == CSTATIC)
1238 if(c == CEXTERN || c == CGLOBL) {
1239 warn(Z, "just say static: %s", s->name);
1242 if(s->class == CAUTO || s->class == CPARAM || s->class == CLOCAL)
1243 if(s->block == autobn)
1244 diag(Z, "auto redeclaration of: %s", s->name);
1255 autoffset = align(autoffset, t, Aaut3);
1256 stkoff = maxround(stkoff, autoffset);
1257 s->offset = -autoffset;
1261 if(autoffset == 0) {
1265 autoffset = align(autoffset, t, Aarg1);
1267 s->offset = autoffset;
1268 autoffset = align(autoffset, t, Aarg2);
1274 pdecl(int c, Type *t, Sym *s)
1276 if(s && s->offset != -1) {
1277 diag(Z, "not a parameter: %s", s->name);
1280 t = paramconv(t, c==CPARAM);
1284 diag(Z, "parameter cannot have class: %s", s->name);
1287 if(typesu[t->etype] && t->width <= 0)
1288 diag(Z, "incomplete structure: %s", t->tag->name);
1293 xdecl(int c, Type *t, Sym *s)
1303 if(s->class == CGLOBL)
1308 if(s->class == CGLOBL)
1314 if(s->class == CEXTERN)
1319 diag(Z, "overspecified class: %s %s %s", s->name, cnames[c], cnames[s->class]);
1324 if(!typesuv[t->etype]) {
1325 diag(Z, "typestr must be struct/union: %s", s->name);
1332 if(s->class == CSTATIC)
1333 if(c == CEXTERN || c == CGLOBL) {
1334 warn(Z, "overspecified class: %s %s %s", s->name, cnames[c], cnames[s->class]);
1338 if(s->class != c || !sametype(t, s->type) || t->etype == TENUM) {
1339 diag(Z, "external redeclaration of: %s", s->name);
1340 Bprint(&diagbuf, " %s %T %L\n", cnames[c], t, nearln);
1341 Bprint(&diagbuf, " %s %T %L\n", cnames[s->class], s->type, s->varlineno);
1351 tmerge(Type *t1, Sym *s)
1356 /*print("merge %T; %T\n", t1, t2);/**/
1358 if(t1 == T || t2 == T || t1 == t2)
1360 if(t1->etype != t2->etype)
1372 while(ta != T && tb != T) {
1375 /* ignore old-style flag */
1376 if(ta->etype == TOLD) {
1380 if(tb->etype == TOLD) {
1384 /* checking terminated by ... */
1385 if(ta->etype == TDOT && tb->etype == TDOT) {
1390 if(!sametype(ta, tb))
1396 diag(Z, "function inconsistently declared: %s", s->name);
1398 /* take new-style over old-style */
1401 if(ta != T && ta->etype == TOLD)
1402 if(tb != T && tb->etype != TOLD)
1407 /* should we check array size change? */
1408 if(t2->width > t1->width)
1409 t1->width = t2->width;
1422 edecl(int c, Type *t, Sym *s)
1427 if(!typesu[t->etype])
1428 diag(Z, "unnamed structure element must be struct/union");
1430 diag(Z, "unnamed structure element cannot have class");
1433 diag(Z, "structure element cannot have class: %s", s->name);
1439 t->shift = lastbit - lastfield;
1440 t->nbits = lastfield;
1442 t->shift = -t->shift;
1444 t->etype = tufield->etype;
1446 t->etype = tfield->etype;
1456 * this routine is very suspect.
1457 * ansi requires the enum type to
1458 * be represented as an 'int'
1459 * this means that 0x81234567
1460 * would be illegal. this routine
1461 * makes signed and unsigned go
1465 maxtype(Type *t1, Type *t2)
1472 if(t1->etype > t2->etype)
1478 doenum(Sym *s, Node *n)
1483 if(n->op != OCONST) {
1484 diag(n, "enum not a constant: %s", s->name);
1488 en.tenum = maxtype(en.cenum, en.tenum);
1490 if(!typefd[en.cenum->etype])
1491 en.lastenum = n->vconst;
1493 en.floatenum = n->fconst;
1497 xdecl(CXXX, types[TENUM], s);
1500 en.tenum = types[TINT];
1501 en.cenum = types[TINT];
1504 s->tenum = en.cenum;
1506 if(!typefd[s->tenum->etype]) {
1507 s->vconst = convvtox(en.lastenum, s->tenum->etype);
1510 s->fconst = en.floatenum;
1520 symadjust(Sym *s, Node *n, long del)
1526 symadjust(s, n->left, del);
1528 symadjust(s, n->right, del);
1546 covered1(long o, Node *n)
1549 return covered1(o, n->left) || covered1(o, n->right);
1551 return covered1(o, n->left);
1554 return o >= n->xoffset && o < n->xoffset+n->type->width;
1558 covered(long o, long w, Node *n)
1560 while(w > 0 && covered1(o, n)) {
1568 initz(Node *q, Type *t, long o, Node *n)
1572 if(covered(o, t->width, n))
1575 p = new(ONAME, Z, Z);
1580 q = new(OCONST, Z, Z);
1584 return new(OLIST, new(OASI, p, q), n);
1588 contig(Sym *s, Node *n, long v)
1590 Node *p, *r, *q, *m;
1595 print("contig v = %ld; s = %s\n", v, s->name);
1596 prtree(n, "doinit value");
1604 * nightmare: an automatic array whose size
1605 * increases when it is initialized
1609 diag(n, "automatic adjustable array: %s", s->name);
1611 autoffset = align(autoffset, s->type, Aaut3);
1612 s->offset = -autoffset;
1613 stkoff = maxround(stkoff, autoffset);
1614 symadjust(s, n, v - s->offset);
1617 diag(Z, "oops in contig");
1620 if(n->left->type->width == w)
1623 for(q=n; q->op != ONAME; q=q->left)
1628 /* unaligned front */
1629 while(w > 0 && (v % ewidth[TIND]) != 0) {
1631 if(w >= ewidth[TLONG] && (v % ewidth[TLONG]) == 0)
1633 n = initz(q, zt, v, n);
1638 /* skip initialized words */
1639 while(w >= ewidth[TIND] && covered(v, ewidth[TIND], n)) {
1644 /* unaligned (or small) back */
1645 while(w > 0 && ((w % ewidth[TIND]) != 0 || w <= 16)) {
1647 if(w >= ewidth[TLONG] && (w % ewidth[TLONG]) == 0)
1650 n = initz(q, zt, v + w, n);
1657 * insert the following code, where long becomes vlong if pointers are fat
1659 *(long**)&X = (long*)((char*)X + sizeof(X));
1663 } while(*(long**)&X);
1666 zt = ewidth[TIND] > ewidth[TLONG]? types[TVLONG]: types[TLONG];
1668 p = new(ONAME, Z, Z);
1670 p->type = typ(TIND, zt);
1673 r = new(ONAME, Z, Z);
1675 r = new(OPOSTDEC, r, Z);
1677 q = new(ONAME, Z, Z);
1679 q = new(OIND, q, Z);
1681 m = new(OCONST, Z, Z);
1687 r = new(OLIST, r, q);
1689 q = new(ONAME, Z, Z);
1691 r = new(ODWHILE, q, r);
1693 q = new(ONAME, Z, Z);
1695 q->type = q->type->link;
1697 q = new(OADDR, q, 0);
1699 q = new(OASI, p, q);
1700 r = new(OLIST, q, r);
1702 n = new(OLIST, r, n);