30 %type <lval> gctname gcname cname gname tname
31 %type <lval> gctnlist gcnlist zgnlist
32 %type <type> tlist sbody complex
34 %type <node> zarglist arglist zcexpr
35 %type <node> name block stmnt cexpr expr xuexpr pexpr
36 %type <node> zelist elist adecl slist uexpr string lstring
37 %type <node> xdecor xdecor2 labels label ulstmnt
38 %type <node> adlist edecor tag qual qlist
39 %type <node> abdecor abdecor1 abdecor2 abdecor3
40 %type <node> zexpr lexpr init ilist forexpr
44 %right '=' LPE LME LMLE LDVE LMDE LRSHE LLSHE LANDE LXORE LORE
56 %right LMM LPP LMG '.' '[' '('
58 %token <sym> LNAME LTYPE
59 %token <dval> LFCONST LDCONST
60 %token <vval> LCONST LLCONST LUCONST LULCONST LVLCONST LUVLCONST
61 %token <sval> LSTRING LLSTRING
62 %token LAUTO LBREAK LCASE LCHAR LCONTINUE LDEFAULT LDO
63 %token LDOUBLE LELSE LEXTERN LFLOAT LFOR LGOTO
64 %token LIF LINT LLONG LREGISTER LRETURN LSHORT LSIZEOF LUSED
65 %token LSTATIC LSTRUCT LSWITCH LTYPEDEF LTYPESTR LUNION LUNSIGNED
66 %token LWHILE LVOID LENUM LSIGNED LCONSTNT LVOLATILE LSET LSIGNOF
67 %token LRESTRICT LINLINE
78 dodecl(xdecl, lastclass, lasttype, Z);
85 dodecl(xdecl, lastclass, lasttype, $2);
86 if(lastdcl == T || lastdcl->etype != TFUNC) {
87 diag($2, "not a function");
88 lastdcl = types[TFUNC];
105 $6 = new(OLIST, n, $6);
106 if(!debug['a'] && !debug['Z'])
113 dodecl(xdecl, lastclass, lasttype, $1);
117 $1 = dodecl(xdecl, lastclass, lasttype, $1);
121 doinit($1->sym, $1->type, 0L, $4);
129 $$ = new(OIND, $3, Z);
130 $$->garb = simpleg($2);
139 | xdecor2 '(' zarglist ')'
141 $$ = new(OFUNC, $1, $3);
143 | xdecor2 '[' zexpr ']'
145 $$ = new(OARRAY, $1, $3);
149 * automatic declarator
154 $$ = dodecl(adecl, lastclass, lasttype, Z);
164 dodecl(adecl, lastclass, lasttype, $1);
169 $1 = dodecl(adecl, lastclass, lasttype, $1);
175 w = $1->sym->type->width;
176 $$ = doinit($1->sym, $1->type, 0L, $4);
177 $$ = contig($1->sym, $$, w);
185 $$ = new(OLIST, $1, $3);
190 * parameter declarator
193 | pdecl ctlist pdlist ';'
198 dodecl(pdecl, lastclass, lasttype, $1);
203 * structure element declarator
217 zedlist: /* extension */
220 edecl(CXXX, lasttype, S);
227 dodecl(edecl, CXXX, lasttype, $1);
239 $$ = new(OBIT, $1, $3);
243 $$ = new(OBIT, Z, $2);
247 * abstract declarator
258 $$ = new(OIND, (Z), Z);
259 $$->garb = simpleg($2);
261 | '*' zgnlist abdecor1
263 $$ = new(OIND, $3, Z);
264 $$->garb = simpleg($2);
270 | abdecor2 '(' zarglist ')'
272 $$ = new(OFUNC, $1, $3);
274 | abdecor2 '[' zexpr ']'
276 $$ = new(OARRAY, $1, $3);
282 $$ = new(OFUNC, (Z), Z);
286 $$ = new(OARRAY, (Z), $2);
297 $$ = new(OINIT, invert($2), Z);
303 $$ = new(OARRAY, $2, Z);
307 $$ = new(OELEM, Z, Z);
316 $$ = new(OLIST, $1, $2);
321 $$ = new(OLIST, $1, $2);
329 $$ = new(OLIST, $1, $2);
346 $$ = new(OPROTO, $2, Z);
351 $$ = new(OPROTO, $2, Z);
356 $$ = new(ODOTDOT, Z, Z);
358 | arglist ',' arglist
360 $$ = new(OLIST, $1, $3);
368 // $$ = new(OLIST, $2, $$);
370 $$ = new(OLIST, Z, Z);
379 $$ = new(OLIST, $1, $2);
383 $$ = new(OLIST, $1, $2);
390 $$ = new(OLIST, $1, $2);
396 $$ = new(OCASE, $2, Z);
400 $$ = new(OCASE, Z, Z);
404 $$ = new(OLABEL, dcllabel($1, 1), Z);
415 $$ = new(OLIST, $1, $2);
434 $$ = new(OLIST, $$, $2);
438 | LIF '(' cexpr ')' stmnt
440 $$ = new(OIF, $3, new(OLIST, $5, Z));
442 warn($3, "empty if body");
444 | LIF '(' cexpr ')' stmnt LELSE stmnt
446 $$ = new(OIF, $3, new(OLIST, $5, $7));
448 warn($3, "empty if body");
450 warn($3, "empty else body");
452 | { markdcl(); } LFOR '(' forexpr ';' zcexpr ';' zcexpr ')' stmnt
457 $4 = new(OLIST, $$, $4);
461 $$ = new(OFOR, new(OLIST, $6, new(OLIST, $4, $8)), $10);
463 | LWHILE '(' cexpr ')' stmnt
465 $$ = new(OWHILE, $3, $5);
467 | LDO stmnt LWHILE '(' cexpr ')' ';'
469 $$ = new(ODWHILE, $5, $2);
473 $$ = new(ORETURN, $2, Z);
474 $$->type = thisfn->link;
476 | LSWITCH '(' cexpr ')' stmnt
478 $$ = new(OCONST, Z, Z);
480 $$->type = types[TINT];
481 $3 = new(OSUB, $$, $3);
483 $$ = new(OCONST, Z, Z);
485 $$->type = types[TINT];
486 $3 = new(OSUB, $$, $3);
488 $$ = new(OSWITCH, $3, $5);
492 $$ = new(OBREAK, Z, Z);
496 $$ = new(OCONTINUE, Z, Z);
500 $$ = new(OGOTO, dcllabel($2, 0), Z);
502 | LUSED '(' zelist ')' ';'
504 $$ = new(OUSED, $3, Z);
506 | LSET '(' zelist ')' ';'
508 $$ = new(OSET, $3, Z);
526 $$ = new(OCAST, $1, Z);
527 $$->type = types[TLONG];
534 $$ = new(OCOMMA, $1, $3);
541 $$ = new(OMUL, $1, $3);
545 $$ = new(ODIV, $1, $3);
549 $$ = new(OMOD, $1, $3);
553 $$ = new(OADD, $1, $3);
557 $$ = new(OSUB, $1, $3);
561 $$ = new(OASHR, $1, $3);
565 $$ = new(OASHL, $1, $3);
569 $$ = new(OLT, $1, $3);
573 $$ = new(OGT, $1, $3);
577 $$ = new(OLE, $1, $3);
581 $$ = new(OGE, $1, $3);
585 $$ = new(OEQ, $1, $3);
589 $$ = new(ONE, $1, $3);
593 $$ = new(OAND, $1, $3);
597 $$ = new(OXOR, $1, $3);
601 $$ = new(OOR, $1, $3);
605 $$ = new(OANDAND, $1, $3);
609 $$ = new(OOROR, $1, $3);
611 | expr '?' cexpr ':' expr
613 $$ = new(OCOND, $1, new(OLIST, $3, $5));
617 $$ = new(OAS, $1, $3);
621 $$ = new(OASADD, $1, $3);
625 $$ = new(OASSUB, $1, $3);
629 $$ = new(OASMUL, $1, $3);
633 $$ = new(OASDIV, $1, $3);
637 $$ = new(OASMOD, $1, $3);
641 $$ = new(OASASHL, $1, $3);
645 $$ = new(OASASHR, $1, $3);
649 $$ = new(OASAND, $1, $3);
653 $$ = new(OASXOR, $1, $3);
657 $$ = new(OASOR, $1, $3);
662 | '(' tlist abdecor ')' xuexpr
664 $$ = new(OCAST, $5, Z);
665 dodecl(NODECL, CXXX, $2, $3);
669 | '(' tlist abdecor ')' '{' ilist '}' /* extension */
671 $$ = new(OSTRUCT, $6, Z);
672 dodecl(NODECL, CXXX, $2, $3);
680 $$ = new(OIND, $2, Z);
684 $$ = new(OADDR, $2, Z);
688 $$ = new(OPOS, $2, Z);
692 $$ = new(ONEG, $2, Z);
696 $$ = new(ONOT, $2, Z);
700 $$ = new(OCOM, $2, Z);
704 $$ = new(OPREINC, $2, Z);
708 $$ = new(OPREDEC, $2, Z);
712 $$ = new(OSIZE, $2, Z);
716 $$ = new(OSIGN, $2, Z);
724 | LSIZEOF '(' tlist abdecor ')'
726 $$ = new(OSIZE, Z, Z);
727 dodecl(NODECL, CXXX, $3, $4);
730 | LSIGNOF '(' tlist abdecor ')'
732 $$ = new(OSIGN, Z, Z);
733 dodecl(NODECL, CXXX, $3, $4);
736 | pexpr '(' zelist ')'
738 $$ = new(OFUNC, $1, Z);
741 dodecl(xdecl, CXXX, types[TINT], $$);
742 $$->right = invert($3);
744 | pexpr '[' cexpr ']'
746 $$ = new(OIND, new(OADD, $1, $3), Z);
750 $$ = new(ODOT, new(OIND, $1, Z), Z);
755 $$ = new(ODOT, $1, Z);
760 $$ = new(OPOSTINC, $1, Z);
764 $$ = new(OPOSTDEC, $1, Z);
769 $$ = new(OCONST, Z, Z);
770 $$->type = types[TINT];
772 $$->cstring = strdup(symb);
776 $$ = new(OCONST, Z, Z);
777 $$->type = types[TLONG];
779 $$->cstring = strdup(symb);
783 $$ = new(OCONST, Z, Z);
784 $$->type = types[TUINT];
786 $$->cstring = strdup(symb);
790 $$ = new(OCONST, Z, Z);
791 $$->type = types[TULONG];
793 $$->cstring = strdup(symb);
797 $$ = new(OCONST, Z, Z);
798 $$->type = types[TDOUBLE];
800 $$->cstring = strdup(symb);
804 $$ = new(OCONST, Z, Z);
805 $$->type = types[TFLOAT];
807 $$->cstring = strdup(symb);
811 $$ = new(OCONST, Z, Z);
812 $$->type = types[TVLONG];
814 $$->cstring = strdup(symb);
818 $$ = new(OCONST, Z, Z);
819 $$->type = types[TUVLONG];
821 $$->cstring = strdup(symb);
829 $$ = new(OSTRING, Z, Z);
830 $$->type = typ(TARRAY, types[TCHAR]);
831 $$->type->width = $1.l + 1;
842 n = $1->type->width - 1;
843 s = alloc(n+$2.l+MAXALIGN);
845 memcpy(s, $1->cstring, n);
846 memcpy(s+n, $2.s, $2.l);
850 $$->type->width += $2.l;
857 $$ = new(OLSTRING, Z, Z);
858 $$->type = typ(TARRAY, types[TRUNE]);
859 $$->type->width = $1.l + sizeof(Rune);
860 $$->rstring = (Rune*)$1.s;
870 n = $1->type->width - sizeof(Rune);
871 s = alloc(n+$2.l+MAXALIGN);
873 memcpy(s, $1->rstring, n);
874 memcpy(s+n, $2.s, $2.l);
875 *(Rune*)(s+n+$2.l) = 0;
878 $$->type->width += $2.l;
879 $$->rstring = (Rune*)s;
892 $$ = new(OLIST, $1, $3);
900 $<tyty>$.t3 = lasttype;
901 $<tyty>$.c = lastclass;
914 lasttype = $<tyty>2.t3;
915 lastclass = $<tyty>2.c;
921 lasttype = types[TINT];
940 $$.t = garbt($$.t, $1);
946 $$.t = garbt($$.t, $2);
947 if($2 & ~BCLASS & ~BGARB)
948 diag(Z, "duplicate types given: %T and %Q", $1, $2);
952 $$.t = simplet(typebitor($1, $2));
954 $$.t = garbt($$.t, $2);
956 | gcnlist complex zgnlist
960 $$.t = garbt($$.t, $1|$3);
966 $$.t = garbt($$.t, $1);
968 | gcnlist tname gctnlist
970 $$.t = simplet(typebitor($2, $3));
971 $$.c = simplec($1|$3);
972 $$.t = garbt($$.t, $1|$3);
980 diag(Z, "illegal combination of class 4: %s", cnames[$1.c]);
993 dotag($2, TSTRUCT, 0);
998 dotag($2, TSTRUCT, autobn);
1004 diag(Z, "redeclare tag: %s", $2->name);
1011 sprint(symb, "_%d_", taggen);
1012 $$ = dotag(lookup(), TSTRUCT, autobn);
1018 dotag($2, TUNION, 0);
1023 dotag($2, TUNION, autobn);
1029 diag(Z, "redeclare tag: %s", $2->name);
1036 sprint(symb, "_%d_", taggen);
1037 $$ = dotag(lookup(), TUNION, autobn);
1043 dotag($2, TENUM, 0);
1046 $$->link = types[TINT];
1051 dotag($2, TENUM, autobn);
1062 diag(Z, "redeclare tag: %s", $2->name);
1064 diag(Z, "enum type ambiguous: %s", $2->name);
1065 en.tenum = types[TINT];
1067 $$->link = en.tenum;
1081 $$ = tcopy($1->type);
1088 $$ = typebitor($1, $2);
1097 $$ = typebitor($1, $2);
1109 $$ = typebitor($1, $2);
1128 tname: /* type words */
1129 LCHAR { $$ = BCHAR; }
1130 | LSHORT { $$ = BSHORT; }
1131 | LINT { $$ = BINT; }
1132 | LLONG { $$ = BLONG; }
1133 | LSIGNED { $$ = BSIGNED; }
1134 | LUNSIGNED { $$ = BUNSIGNED; }
1135 | LFLOAT { $$ = BFLOAT; }
1136 | LDOUBLE { $$ = BDOUBLE; }
1137 | LVOID { $$ = BVOID; }
1139 cname: /* class words */
1140 LAUTO { $$ = BAUTO; }
1141 | LSTATIC { $$ = BSTATIC; }
1142 | LEXTERN { $$ = BEXTERN; }
1143 | LTYPEDEF { $$ = BTYPEDEF; }
1144 | LTYPESTR { $$ = BTYPESTR; }
1145 | LREGISTER { $$ = BREGISTER; }
1146 | LINLINE { $$ = 0; }
1148 gname: /* garbage words */
1149 LCONSTNT { $$ = BCONSTNT; }
1150 | LVOLATILE { $$ = BVOLATILE; }
1151 | LRESTRICT { $$ = 0; }
1156 $$ = new(ONAME, Z, Z);
1157 if($1->class == CLOCAL)
1160 $$->type = $1->type;
1163 $$->etype = $$->type->etype;
1164 $$->xoffset = $1->offset;
1165 $$->class = $1->class;
1171 $$ = new(ONAME, Z, Z);
1173 $$->type = $1->type;
1176 $$->etype = $$->type->etype;
1177 $$->xoffset = $1->offset;
1178 $$->class = $1->class;