1
// Inferno utils/cc/cc.y
2
// http://code.google.com/p/inferno-os/source/browse/utils/cc/cc.y
4
// Copyright © 1994-1999 Lucent Technologies Inc. All rights reserved.
5
// Portions Copyright © 1995-1997 C H Forsyth (forsyth@terzarima.net)
6
// Portions Copyright © 1997-1999 Vita Nuova Limited
7
// Portions Copyright © 2000-2007 Vita Nuova Holdings Limited (www.vitanuova.com)
8
// Portions Copyright © 2004,2006 Bruce Ellis
9
// Portions Copyright © 2005-2007 C H Forsyth (forsyth@terzarima.net)
10
// Revisions Copyright © 2000-2007 Lucent Technologies Inc. and others
11
// Portions Copyright © 2009 The Go Authors. All rights reserved.
13
// Permission is hereby granted, free of charge, to any person obtaining a copy
14
// of this software and associated documentation files (the "Software"), to deal
15
// in the Software without restriction, including without limitation the rights
16
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
// copies of the Software, and to permit persons to whom the Software is
18
// furnished to do so, subject to the following conditions:
20
// The above copyright notice and this permission notice shall be included in
21
// all copies or substantial portions of the Software.
23
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32
#include <stdio.h> /* if we don't, bison will, and cc.h re-#defines getc */
61
%type <lval> gctname gcname cname gname tname
62
%type <lval> gctnlist gcnlist zgnlist
63
%type <type> tlist sbody complex
65
%type <node> zarglist arglist zcexpr
66
%type <node> name block stmnt cexpr expr xuexpr pexpr
67
%type <node> zelist elist adecl slist uexpr string lstring
68
%type <node> xdecor xdecor2 labels label ulstmnt
69
%type <node> adlist edecor tag qual qlist
70
%type <node> abdecor abdecor1 abdecor2 abdecor3
71
%type <node> zexpr lexpr init ilist forexpr
75
%right '=' LPE LME LMLE LDVE LMDE LRSHE LLSHE LANDE LXORE LORE
87
%right LMM LPP LMG '.' '[' '('
89
%token <sym> LNAME LTYPE
90
%token <dval> LFCONST LDCONST
91
%token <vval> LCONST LLCONST LUCONST LULCONST LVLCONST LUVLCONST
92
%token <sval> LSTRING LLSTRING
93
%token LAUTO LBREAK LCASE LCHAR LCONTINUE LDEFAULT LDO
94
%token LDOUBLE LELSE LEXTERN LFLOAT LFOR LGOTO
95
%token LIF LINT LLONG LREGISTER LRETURN LSHORT LSIZEOF LUSED
96
%token LSTATIC LSTRUCT LSWITCH LTYPEDEF LTYPESTR LUNION LUNSIGNED
97
%token LWHILE LVOID LENUM LSIGNED LCONSTNT LVOLATILE LSET LSIGNOF
98
%token LRESTRICT LINLINE
104
* external declarator
109
dodecl(xdecl, lastclass, lasttype, Z);
116
dodecl(xdecl, lastclass, lasttype, $2);
117
if(lastdcl == T || lastdcl->etype != TFUNC) {
118
diag($2, "not a function");
119
lastdcl = types[TFUNC];
136
$6 = new(OLIST, n, $6);
137
if(!debug['a'] && !debug['Z'])
144
dodecl(xdecl, lastclass, lasttype, $1);
148
$1 = dodecl(xdecl, lastclass, lasttype, $1);
152
doinit($1->sym, $1->type, 0L, $4);
160
$$ = new(OIND, $3, Z);
161
$$->garb = simpleg($2);
170
| xdecor2 '(' zarglist ')'
172
$$ = new(OFUNC, $1, $3);
174
| xdecor2 '[' zexpr ']'
176
$$ = new(OARRAY, $1, $3);
180
* automatic declarator
185
$$ = dodecl(adecl, lastclass, lasttype, Z);
195
dodecl(adecl, lastclass, lasttype, $1);
200
$1 = dodecl(adecl, lastclass, lasttype, $1);
206
w = $1->sym->type->width;
207
$$ = doinit($1->sym, $1->type, 0L, $4);
208
$$ = contig($1->sym, $$, w);
216
$$ = new(OLIST, $1, $3);
221
* parameter declarator
224
| pdecl ctlist pdlist ';'
229
dodecl(pdecl, lastclass, lasttype, $1);
234
* structure element declarator
248
zedlist: /* extension */
251
edecl(CXXX, lasttype, S);
258
dodecl(edecl, CXXX, lasttype, $1);
270
$$ = new(OBIT, $1, $3);
274
$$ = new(OBIT, Z, $2);
278
* abstract declarator
289
$$ = new(OIND, (Z), Z);
290
$$->garb = simpleg($2);
292
| '*' zgnlist abdecor1
294
$$ = new(OIND, $3, Z);
295
$$->garb = simpleg($2);
301
| abdecor2 '(' zarglist ')'
303
$$ = new(OFUNC, $1, $3);
305
| abdecor2 '[' zexpr ']'
307
$$ = new(OARRAY, $1, $3);
313
$$ = new(OFUNC, (Z), Z);
317
$$ = new(OARRAY, (Z), $2);
328
$$ = new(OINIT, invert($2), Z);
334
$$ = new(OARRAY, $2, Z);
338
$$ = new(OELEM, Z, Z);
347
$$ = new(OLIST, $1, $2);
352
$$ = new(OLIST, $1, $2);
360
$$ = new(OLIST, $1, $2);
377
$$ = new(OPROTO, $2, Z);
382
$$ = new(OPROTO, $2, Z);
387
$$ = new(ODOTDOT, Z, Z);
389
| arglist ',' arglist
391
$$ = new(OLIST, $1, $3);
399
// $$ = new(OLIST, $2, $$);
401
$$ = new(OLIST, Z, Z);
410
$$ = new(OLIST, $1, $2);
414
$$ = new(OLIST, $1, $2);
421
$$ = new(OLIST, $1, $2);
427
$$ = new(OCASE, $2, Z);
431
$$ = new(OCASE, Z, Z);
435
$$ = new(OLABEL, dcllabel($1, 1), Z);
446
$$ = new(OLIST, $1, $2);
465
$$ = new(OLIST, $$, $2);
469
| LIF '(' cexpr ')' stmnt
471
$$ = new(OIF, $3, new(OLIST, $5, Z));
473
warn($3, "empty if body");
475
| LIF '(' cexpr ')' stmnt LELSE stmnt
477
$$ = new(OIF, $3, new(OLIST, $5, $7));
479
warn($3, "empty if body");
481
warn($3, "empty else body");
483
| { markdcl(); } LFOR '(' forexpr ';' zcexpr ';' zcexpr ')' stmnt
488
$4 = new(OLIST, $$, $4);
492
$$ = new(OFOR, new(OLIST, $6, new(OLIST, $4, $8)), $10);
494
| LWHILE '(' cexpr ')' stmnt
496
$$ = new(OWHILE, $3, $5);
498
| LDO stmnt LWHILE '(' cexpr ')' ';'
500
$$ = new(ODWHILE, $5, $2);
504
$$ = new(ORETURN, $2, Z);
505
$$->type = thisfn->link;
507
| LSWITCH '(' cexpr ')' stmnt
509
$$ = new(OCONST, Z, Z);
511
$$->type = types[TINT];
512
$3 = new(OSUB, $$, $3);
514
$$ = new(OCONST, Z, Z);
516
$$->type = types[TINT];
517
$3 = new(OSUB, $$, $3);
519
$$ = new(OSWITCH, $3, $5);
523
$$ = new(OBREAK, Z, Z);
527
$$ = new(OCONTINUE, Z, Z);
531
$$ = new(OGOTO, dcllabel($2, 0), Z);
533
| LUSED '(' zelist ')' ';'
535
$$ = new(OUSED, $3, Z);
537
| LSET '(' zelist ')' ';'
539
$$ = new(OSET, $3, Z);
557
$$ = new(OCAST, $1, Z);
558
$$->type = types[TLONG];
565
$$ = new(OCOMMA, $1, $3);
572
$$ = new(OMUL, $1, $3);
576
$$ = new(ODIV, $1, $3);
580
$$ = new(OMOD, $1, $3);
584
$$ = new(OADD, $1, $3);
588
$$ = new(OSUB, $1, $3);
592
$$ = new(OASHR, $1, $3);
596
$$ = new(OASHL, $1, $3);
600
$$ = new(OLT, $1, $3);
604
$$ = new(OGT, $1, $3);
608
$$ = new(OLE, $1, $3);
612
$$ = new(OGE, $1, $3);
616
$$ = new(OEQ, $1, $3);
620
$$ = new(ONE, $1, $3);
624
$$ = new(OAND, $1, $3);
628
$$ = new(OXOR, $1, $3);
632
$$ = new(OOR, $1, $3);
636
$$ = new(OANDAND, $1, $3);
640
$$ = new(OOROR, $1, $3);
642
| expr '?' cexpr ':' expr
644
$$ = new(OCOND, $1, new(OLIST, $3, $5));
648
$$ = new(OAS, $1, $3);
652
$$ = new(OASADD, $1, $3);
656
$$ = new(OASSUB, $1, $3);
660
$$ = new(OASMUL, $1, $3);
664
$$ = new(OASDIV, $1, $3);
668
$$ = new(OASMOD, $1, $3);
672
$$ = new(OASASHL, $1, $3);
676
$$ = new(OASASHR, $1, $3);
680
$$ = new(OASAND, $1, $3);
684
$$ = new(OASXOR, $1, $3);
688
$$ = new(OASOR, $1, $3);
693
| '(' tlist abdecor ')' xuexpr
695
$$ = new(OCAST, $5, Z);
696
dodecl(NODECL, CXXX, $2, $3);
700
| '(' tlist abdecor ')' '{' ilist '}' /* extension */
702
$$ = new(OSTRUCT, $6, Z);
703
dodecl(NODECL, CXXX, $2, $3);
711
$$ = new(OIND, $2, Z);
715
$$ = new(OADDR, $2, Z);
719
$$ = new(OPOS, $2, Z);
723
$$ = new(ONEG, $2, Z);
727
$$ = new(ONOT, $2, Z);
731
$$ = new(OCOM, $2, Z);
735
$$ = new(OPREINC, $2, Z);
739
$$ = new(OPREDEC, $2, Z);
743
$$ = new(OSIZE, $2, Z);
747
$$ = new(OSIGN, $2, Z);
755
| LSIZEOF '(' tlist abdecor ')'
757
$$ = new(OSIZE, Z, Z);
758
dodecl(NODECL, CXXX, $3, $4);
761
| LSIGNOF '(' tlist abdecor ')'
763
$$ = new(OSIGN, Z, Z);
764
dodecl(NODECL, CXXX, $3, $4);
767
| pexpr '(' zelist ')'
769
$$ = new(OFUNC, $1, Z);
772
dodecl(xdecl, CXXX, types[TINT], $$);
773
$$->right = invert($3);
775
| pexpr '[' cexpr ']'
777
$$ = new(OIND, new(OADD, $1, $3), Z);
781
$$ = new(ODOT, new(OIND, $1, Z), Z);
786
$$ = new(ODOT, $1, Z);
791
$$ = new(OPOSTINC, $1, Z);
795
$$ = new(OPOSTDEC, $1, Z);
800
$$ = new(OCONST, Z, Z);
801
$$->type = types[TINT];
803
$$->cstring = strdup(symb);
807
$$ = new(OCONST, Z, Z);
808
$$->type = types[TLONG];
810
$$->cstring = strdup(symb);
814
$$ = new(OCONST, Z, Z);
815
$$->type = types[TUINT];
817
$$->cstring = strdup(symb);
821
$$ = new(OCONST, Z, Z);
822
$$->type = types[TULONG];
824
$$->cstring = strdup(symb);
828
$$ = new(OCONST, Z, Z);
829
$$->type = types[TDOUBLE];
831
$$->cstring = strdup(symb);
835
$$ = new(OCONST, Z, Z);
836
$$->type = types[TFLOAT];
838
$$->cstring = strdup(symb);
842
$$ = new(OCONST, Z, Z);
843
$$->type = types[TVLONG];
845
$$->cstring = strdup(symb);
849
$$ = new(OCONST, Z, Z);
850
$$->type = types[TUVLONG];
852
$$->cstring = strdup(symb);
860
$$ = new(OSTRING, Z, Z);
861
$$->type = typ(TARRAY, types[TCHAR]);
862
$$->type->width = $1.l + 1;
873
n = $1->type->width - 1;
874
s = alloc(n+$2.l+MAXALIGN);
876
memcpy(s, $1->cstring, n);
877
memcpy(s+n, $2.s, $2.l);
881
$$->type->width += $2.l;
888
$$ = new(OLSTRING, Z, Z);
889
$$->type = typ(TARRAY, types[TUSHORT]);
890
$$->type->width = $1.l + sizeof(ushort);
891
$$->rstring = (ushort*)$1.s;
901
n = $1->type->width - sizeof(ushort);
902
s = alloc(n+$2.l+MAXALIGN);
904
memcpy(s, $1->rstring, n);
905
memcpy(s+n, $2.s, $2.l);
906
*(ushort*)(s+n+$2.l) = 0;
909
$$->type->width += $2.l;
910
$$->rstring = (ushort*)s;
923
$$ = new(OLIST, $1, $3);
931
$<tyty>$.t3 = lasttype;
932
$<tyty>$.c = lastclass;
945
lasttype = $<tyty>2.t3;
946
lastclass = $<tyty>2.c;
952
lasttype = types[TINT];
971
$$.t = garbt($$.t, $1);
977
$$.t = garbt($$.t, $2);
978
if($2 & ~BCLASS & ~BGARB)
979
diag(Z, "duplicate types given: %T and %Q", $1, $2);
983
$$.t = simplet(typebitor($1, $2));
985
$$.t = garbt($$.t, $2);
987
| gcnlist complex zgnlist
991
$$.t = garbt($$.t, $1|$3);
997
$$.t = garbt($$.t, $1);
999
| gcnlist tname gctnlist
1001
$$.t = simplet(typebitor($2, $3));
1002
$$.c = simplec($1|$3);
1003
$$.t = garbt($$.t, $1|$3);
1011
diag(Z, "illegal combination of class 4: %s", cnames[$1.c]);
1024
dotag($2, TSTRUCT, 0);
1029
dotag($2, TSTRUCT, autobn);
1035
diag(Z, "redeclare tag: %s", $2->name);
1042
sprint(symb, "_%d_", taggen);
1043
$$ = dotag(lookup(), TSTRUCT, autobn);
1049
dotag($2, TUNION, 0);
1054
dotag($2, TUNION, autobn);
1060
diag(Z, "redeclare tag: %s", $2->name);
1067
sprint(symb, "_%d_", taggen);
1068
$$ = dotag(lookup(), TUNION, autobn);
1074
dotag($2, TENUM, 0);
1077
$$->link = types[TINT];
1082
dotag($2, TENUM, autobn);
1093
diag(Z, "redeclare tag: %s", $2->name);
1095
diag(Z, "enum type ambiguous: %s", $2->name);
1096
en.tenum = types[TINT];
1098
$$->link = en.tenum;
1112
$$ = tcopy($1->type);
1119
$$ = typebitor($1, $2);
1128
$$ = typebitor($1, $2);
1140
$$ = typebitor($1, $2);
1159
tname: /* type words */
1160
LCHAR { $$ = BCHAR; }
1161
| LSHORT { $$ = BSHORT; }
1162
| LINT { $$ = BINT; }
1163
| LLONG { $$ = BLONG; }
1164
| LSIGNED { $$ = BSIGNED; }
1165
| LUNSIGNED { $$ = BUNSIGNED; }
1166
| LFLOAT { $$ = BFLOAT; }
1167
| LDOUBLE { $$ = BDOUBLE; }
1168
| LVOID { $$ = BVOID; }
1170
cname: /* class words */
1171
LAUTO { $$ = BAUTO; }
1172
| LSTATIC { $$ = BSTATIC; }
1173
| LEXTERN { $$ = BEXTERN; }
1174
| LTYPEDEF { $$ = BTYPEDEF; }
1175
| LTYPESTR { $$ = BTYPESTR; }
1176
| LREGISTER { $$ = BREGISTER; }
1177
| LINLINE { $$ = 0; }
1179
gname: /* garbage words */
1180
LCONSTNT { $$ = BCONSTNT; }
1181
| LVOLATILE { $$ = BVOLATILE; }
1182
| LRESTRICT { $$ = 0; }
1187
$$ = new(ONAME, Z, Z);
1188
if($1->class == CLOCAL)
1191
$$->type = $1->type;
1194
$$->etype = $$->type->etype;
1195
$$->xoffset = $1->offset;
1196
$$->class = $1->class;
1202
$$ = new(ONAME, Z, Z);
1204
$$->type = $1->type;
1207
$$->etype = $$->type->etype;
1208
$$->xoffset = $1->offset;
1209
$$->class = $1->class;