28 %type <lval> gctname gcname cname gname tname
29 %type <lval> gctnlist gcnlist zgnlist
30 %type <type> tlist sbody complex
32 %type <node> zarglist arglist zcexpr
33 %type <node> name block stmnt cexpr expr xuexpr pexpr
34 %type <node> zelist elist adecl slist uexpr string lstring
35 %type <node> xdecor xdecor2 labels label ulstmnt
36 %type <node> adlist edecor tag qual qlist
37 %type <node> abdecor abdecor1 abdecor2 abdecor3
38 %type <node> zexpr lexpr init ilist forexpr
42 %right '=' LPE LME LMLE LDVE LMDE LRSHE LLSHE LANDE LXORE LORE
54 %right LMM LPP LMG '.' '[' '('
56 %token <sym> LNAME LTYPE
57 %token <dval> LFCONST LDCONST
58 %token <vval> LCONST LLCONST LUCONST LULCONST LVLCONST LUVLCONST
59 %token <sval> LSTRING LLSTRING
60 %token LAUTO LBREAK LCASE LCHAR LCONTINUE LDEFAULT LDO
61 %token LDOUBLE LELSE LEXTERN LFLOAT LFOR LGOTO
62 %token LIF LINT LLONG LREGISTER LRETURN LSHORT LSIZEOF LUSED
63 %token LSTATIC LSTRUCT LSWITCH LTYPEDEF LTYPESTR LUNION LUNSIGNED
64 %token LWHILE LVOID LENUM LSIGNED LCONSTNT LVOLATILE LSET LSIGNOF
65 %token LRESTRICT LINLINE
76 dodecl(xdecl, lastclass, lasttype, Z);
83 dodecl(xdecl, lastclass, lasttype, $2);
84 if(lastdcl == T || lastdcl->etype != TFUNC) {
85 diag($2, "not a function");
86 lastdcl = types[TFUNC];
103 $6 = new(OLIST, n, $6);
104 if(!debug['a'] && !debug['Z'])
111 dodecl(xdecl, lastclass, lasttype, $1);
115 $1 = dodecl(xdecl, lastclass, lasttype, $1);
119 doinit($1->sym, $1->type, 0L, $4);
127 $$ = new(OIND, $3, Z);
128 $$->garb = simpleg($2);
137 | xdecor2 '(' zarglist ')'
139 $$ = new(OFUNC, $1, $3);
141 | xdecor2 '[' zexpr ']'
143 $$ = new(OARRAY, $1, $3);
147 * automatic declarator
152 $$ = dodecl(adecl, lastclass, lasttype, Z);
162 dodecl(adecl, lastclass, lasttype, $1);
167 $1 = dodecl(adecl, lastclass, lasttype, $1);
173 w = $1->sym->type->width;
174 $$ = doinit($1->sym, $1->type, 0L, $4);
175 $$ = contig($1->sym, $$, w);
183 $$ = new(OLIST, $1, $3);
188 * parameter declarator
191 | pdecl ctlist pdlist ';'
196 dodecl(pdecl, lastclass, lasttype, $1);
201 * structure element declarator
215 zedlist: /* extension */
218 edecl(CXXX, lasttype, S);
225 dodecl(edecl, CXXX, lasttype, $1);
237 $$ = new(OBIT, $1, $3);
241 $$ = new(OBIT, Z, $2);
245 * abstract declarator
256 $$ = new(OIND, (Z), Z);
257 $$->garb = simpleg($2);
259 | '*' zgnlist abdecor1
261 $$ = new(OIND, $3, Z);
262 $$->garb = simpleg($2);
268 | abdecor2 '(' zarglist ')'
270 $$ = new(OFUNC, $1, $3);
272 | abdecor2 '[' zexpr ']'
274 $$ = new(OARRAY, $1, $3);
280 $$ = new(OFUNC, (Z), Z);
284 $$ = new(OARRAY, (Z), $2);
295 $$ = new(OINIT, invert($2), Z);
301 $$ = new(OARRAY, $2, Z);
305 $$ = new(OELEM, Z, Z);
314 $$ = new(OLIST, $1, $2);
319 $$ = new(OLIST, $1, $2);
327 $$ = new(OLIST, $1, $2);
344 $$ = new(OPROTO, $2, Z);
349 $$ = new(OPROTO, $2, Z);
354 $$ = new(ODOTDOT, Z, Z);
356 | arglist ',' arglist
358 $$ = new(OLIST, $1, $3);
366 // $$ = new(OLIST, $2, $$);
368 $$ = new(OLIST, Z, Z);
377 $$ = new(OLIST, $1, $2);
381 $$ = new(OLIST, $1, $2);
388 $$ = new(OLIST, $1, $2);
394 $$ = new(OCASE, $2, Z);
398 $$ = new(OCASE, Z, Z);
402 $$ = new(OLABEL, dcllabel($1, 1), Z);
413 $$ = new(OLIST, $1, $2);
432 $$ = new(OLIST, $$, $2);
436 | LIF '(' cexpr ')' stmnt
438 $$ = new(OIF, $3, new(OLIST, $5, Z));
440 warn($3, "empty if body");
442 | LIF '(' cexpr ')' stmnt LELSE stmnt
444 $$ = new(OIF, $3, new(OLIST, $5, $7));
446 warn($3, "empty if body");
448 warn($3, "empty else body");
450 | { markdcl(); } LFOR '(' forexpr ';' zcexpr ';' zcexpr ')' stmnt
455 $4 = new(OLIST, $$, $4);
459 $$ = new(OFOR, new(OLIST, $6, new(OLIST, $4, $8)), $10);
461 | LWHILE '(' cexpr ')' stmnt
463 $$ = new(OWHILE, $3, $5);
465 | LDO stmnt LWHILE '(' cexpr ')' ';'
467 $$ = new(ODWHILE, $5, $2);
471 $$ = new(ORETURN, $2, Z);
472 $$->type = thisfn->link;
474 | LSWITCH '(' cexpr ')' stmnt
476 $$ = new(OCONST, Z, Z);
478 $$->type = types[TINT];
479 $3 = new(OSUB, $$, $3);
481 $$ = new(OCONST, Z, Z);
483 $$->type = types[TINT];
484 $3 = new(OSUB, $$, $3);
486 $$ = new(OSWITCH, $3, $5);
490 $$ = new(OBREAK, Z, Z);
494 $$ = new(OCONTINUE, Z, Z);
498 $$ = new(OGOTO, dcllabel($2, 0), Z);
500 | LUSED '(' zelist ')' ';'
502 $$ = new(OUSED, $3, Z);
504 | LSET '(' zelist ')' ';'
506 $$ = new(OSET, $3, Z);
524 $$ = new(OCAST, $1, Z);
525 $$->type = types[TLONG];
532 $$ = new(OCOMMA, $1, $3);
539 $$ = new(OMUL, $1, $3);
543 $$ = new(ODIV, $1, $3);
547 $$ = new(OMOD, $1, $3);
551 $$ = new(OADD, $1, $3);
555 $$ = new(OSUB, $1, $3);
559 $$ = new(OASHR, $1, $3);
563 $$ = new(OASHL, $1, $3);
567 $$ = new(OLT, $1, $3);
571 $$ = new(OGT, $1, $3);
575 $$ = new(OLE, $1, $3);
579 $$ = new(OGE, $1, $3);
583 $$ = new(OEQ, $1, $3);
587 $$ = new(ONE, $1, $3);
591 $$ = new(OAND, $1, $3);
595 $$ = new(OXOR, $1, $3);
599 $$ = new(OOR, $1, $3);
603 $$ = new(OANDAND, $1, $3);
607 $$ = new(OOROR, $1, $3);
609 | expr '?' cexpr ':' expr
611 $$ = new(OCOND, $1, new(OLIST, $3, $5));
615 $$ = new(OAS, $1, $3);
619 $$ = new(OASADD, $1, $3);
623 $$ = new(OASSUB, $1, $3);
627 $$ = new(OASMUL, $1, $3);
631 $$ = new(OASDIV, $1, $3);
635 $$ = new(OASMOD, $1, $3);
639 $$ = new(OASASHL, $1, $3);
643 $$ = new(OASASHR, $1, $3);
647 $$ = new(OASAND, $1, $3);
651 $$ = new(OASXOR, $1, $3);
655 $$ = new(OASOR, $1, $3);
660 | '(' tlist abdecor ')' xuexpr
662 $$ = new(OCAST, $5, Z);
663 dodecl(NODECL, CXXX, $2, $3);
667 | '(' tlist abdecor ')' '{' ilist '}' /* extension */
669 $$ = new(OSTRUCT, $6, Z);
670 dodecl(NODECL, CXXX, $2, $3);
678 $$ = new(OIND, $2, Z);
682 $$ = new(OADDR, $2, Z);
686 $$ = new(OPOS, $2, Z);
690 $$ = new(ONEG, $2, Z);
694 $$ = new(ONOT, $2, Z);
698 $$ = new(OCOM, $2, Z);
702 $$ = new(OPREINC, $2, Z);
706 $$ = new(OPREDEC, $2, Z);
710 $$ = new(OSIZE, $2, Z);
714 $$ = new(OSIGN, $2, Z);
722 | LSIZEOF '(' tlist abdecor ')'
724 $$ = new(OSIZE, Z, Z);
725 dodecl(NODECL, CXXX, $3, $4);
728 | LSIGNOF '(' tlist abdecor ')'
730 $$ = new(OSIGN, Z, Z);
731 dodecl(NODECL, CXXX, $3, $4);
734 | pexpr '(' zelist ')'
736 $$ = new(OFUNC, $1, Z);
739 dodecl(xdecl, CXXX, types[TINT], $$);
740 $$->right = invert($3);
742 | pexpr '[' cexpr ']'
744 $$ = new(OIND, new(OADD, $1, $3), Z);
748 $$ = new(ODOT, new(OIND, $1, Z), Z);
753 $$ = new(ODOT, $1, Z);
758 $$ = new(OPOSTINC, $1, Z);
762 $$ = new(OPOSTDEC, $1, Z);
767 $$ = new(OCONST, Z, Z);
768 $$->type = types[TINT];
770 $$->cstring = strdup(symb);
774 $$ = new(OCONST, Z, Z);
775 $$->type = types[TLONG];
777 $$->cstring = strdup(symb);
781 $$ = new(OCONST, Z, Z);
782 $$->type = types[TUINT];
784 $$->cstring = strdup(symb);
788 $$ = new(OCONST, Z, Z);
789 $$->type = types[TULONG];
791 $$->cstring = strdup(symb);
795 $$ = new(OCONST, Z, Z);
796 $$->type = types[TDOUBLE];
798 $$->cstring = strdup(symb);
802 $$ = new(OCONST, Z, Z);
803 $$->type = types[TFLOAT];
805 $$->cstring = strdup(symb);
809 $$ = new(OCONST, Z, Z);
810 $$->type = types[TVLONG];
812 $$->cstring = strdup(symb);
816 $$ = new(OCONST, Z, Z);
817 $$->type = types[TUVLONG];
819 $$->cstring = strdup(symb);
827 $$ = new(OSTRING, Z, Z);
828 $$->type = typ(TARRAY, types[TCHAR]);
829 $$->type->width = $1.l + 1;
840 n = $1->type->width - 1;
841 s = alloc(n+$2.l+MAXALIGN);
843 memcpy(s, $1->cstring, n);
844 memcpy(s+n, $2.s, $2.l);
848 $$->type->width += $2.l;
855 $$ = new(OLSTRING, Z, Z);
856 $$->type = typ(TARRAY, types[TUSHORT]);
857 $$->type->width = $1.l + sizeof(ushort);
858 $$->rstring = (ushort*)$1.s;
868 n = $1->type->width - sizeof(ushort);
869 s = alloc(n+$2.l+MAXALIGN);
871 memcpy(s, $1->rstring, n);
872 memcpy(s+n, $2.s, $2.l);
873 *(ushort*)(s+n+$2.l) = 0;
876 $$->type->width += $2.l;
877 $$->rstring = (ushort*)s;
890 $$ = new(OLIST, $1, $3);
913 lasttype = types[TINT];
932 $$.t = garbt($$.t, $1);
938 $$.t = garbt($$.t, $2);
939 if($2 & ~BCLASS & ~BGARB)
940 diag(Z, "duplicate types given: %T and %Q", $1, $2);
944 $$.t = simplet(typebitor($1, $2));
946 $$.t = garbt($$.t, $2);
948 | gcnlist complex zgnlist
952 $$.t = garbt($$.t, $1|$3);
958 $$.t = garbt($$.t, $1);
960 | gcnlist tname gctnlist
962 $$.t = simplet(typebitor($2, $3));
963 $$.c = simplec($1|$3);
964 $$.t = garbt($$.t, $1|$3);
972 diag(Z, "illegal combination of class 4: %s", cnames[$1.c]);
985 dotag($2, TSTRUCT, 0);
990 dotag($2, TSTRUCT, autobn);
996 diag(Z, "redeclare tag: %s", $2->name);
1003 sprint(symb, "_%d_", taggen);
1004 $$ = dotag(lookup(), TSTRUCT, autobn);
1010 dotag($2, TUNION, 0);
1015 dotag($2, TUNION, autobn);
1021 diag(Z, "redeclare tag: %s", $2->name);
1028 sprint(symb, "_%d_", taggen);
1029 $$ = dotag(lookup(), TUNION, autobn);
1035 dotag($2, TENUM, 0);
1038 $$->link = types[TINT];
1043 dotag($2, TENUM, autobn);
1054 diag(Z, "redeclare tag: %s", $2->name);
1056 diag(Z, "enum type ambiguous: %s", $2->name);
1057 en.tenum = types[TINT];
1059 $$->link = en.tenum;
1073 $$ = tcopy($1->type);
1080 $$ = typebitor($1, $2);
1089 $$ = typebitor($1, $2);
1101 $$ = typebitor($1, $2);
1120 tname: /* type words */
1121 LCHAR { $$ = BCHAR; }
1122 | LSHORT { $$ = BSHORT; }
1123 | LINT { $$ = BINT; }
1124 | LLONG { $$ = BLONG; }
1125 | LSIGNED { $$ = BSIGNED; }
1126 | LUNSIGNED { $$ = BUNSIGNED; }
1127 | LFLOAT { $$ = BFLOAT; }
1128 | LDOUBLE { $$ = BDOUBLE; }
1129 | LVOID { $$ = BVOID; }
1131 cname: /* class words */
1132 LAUTO { $$ = BAUTO; }
1133 | LSTATIC { $$ = BSTATIC; }
1134 | LEXTERN { $$ = BEXTERN; }
1135 | LTYPEDEF { $$ = BTYPEDEF; }
1136 | LTYPESTR { $$ = BTYPESTR; }
1137 | LREGISTER { $$ = BREGISTER; }
1138 | LINLINE { $$ = 0; }
1140 gname: /* garbage words */
1141 LCONSTNT { $$ = BCONSTNT; }
1142 | LVOLATILE { $$ = BVOLATILE; }
1143 | LRESTRICT { $$ = 0; }
1148 $$ = new(ONAME, Z, Z);
1149 if($1->class == CLOCAL)
1152 $$->type = $1->type;
1155 $$->etype = $$->type->etype;
1156 $$->xoffset = $1->offset;
1157 $$->class = $1->class;
1163 $$ = new(ONAME, Z, Z);
1165 $$->type = $1->type;
1168 $$->etype = $$->type->etype;
1169 $$->xoffset = $1->offset;
1170 $$->class = $1->class;