~john-koepi/ubuntu/trusty/golang/default

« back to all changes in this revision

Viewing changes to src/cmd/cc/cc.y

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-04-20 17:36:48 UTC
  • Revision ID: james.westby@ubuntu.com-20110420173648-ifergoxyrm832trd
Tags: upstream-2011.03.07.1
Import upstream version 2011.03.07.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Inferno utils/cc/cc.y
 
2
// http://code.google.com/p/inferno-os/source/browse/utils/cc/cc.y
 
3
//
 
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.
 
12
//
 
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:
 
19
//
 
20
// The above copyright notice and this permission notice shall be included in
 
21
// all copies or substantial portions of the Software.
 
22
//
 
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
 
29
// THE SOFTWARE.
 
30
 
 
31
%{
 
32
#include <stdio.h>      /* if we don't, bison will, and cc.h re-#defines getc */
 
33
#include "cc.h"
 
34
%}
 
35
%union  {
 
36
        Node*   node;
 
37
        Sym*    sym;
 
38
        Type*   type;
 
39
        struct
 
40
        {
 
41
                Type*   t;
 
42
                uchar   c;
 
43
        } tycl;
 
44
        struct
 
45
        {
 
46
                Type*   t1;
 
47
                Type*   t2;
 
48
                Type*   t3;
 
49
                uchar   c;
 
50
        } tyty;
 
51
        struct
 
52
        {
 
53
                char*   s;
 
54
                int32   l;
 
55
        } sval;
 
56
        int32   lval;
 
57
        double  dval;
 
58
        vlong   vval;
 
59
}
 
60
%type   <sym>   ltag
 
61
%type   <lval>  gctname gcname cname gname tname
 
62
%type   <lval>  gctnlist gcnlist zgnlist
 
63
%type   <type>  tlist sbody complex
 
64
%type   <tycl>  types
 
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
 
72
 
 
73
%left   ';'
 
74
%left   ','
 
75
%right  '=' LPE LME LMLE LDVE LMDE LRSHE LLSHE LANDE LXORE LORE
 
76
%right  '?' ':'
 
77
%left   LOROR
 
78
%left   LANDAND
 
79
%left   '|'
 
80
%left   '^'
 
81
%left   '&'
 
82
%left   LEQ LNE
 
83
%left   '<' '>' LLE LGE
 
84
%left   LLSH LRSH
 
85
%left   '+' '-'
 
86
%left   '*' '/' '%'
 
87
%right  LMM LPP LMG '.' '[' '('
 
88
 
 
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
 
99
%%
 
100
prog:
 
101
|       prog xdecl
 
102
 
 
103
/*
 
104
 * external declarator
 
105
 */
 
106
xdecl:
 
107
        zctlist ';'
 
108
        {
 
109
                dodecl(xdecl, lastclass, lasttype, Z);
 
110
        }
 
111
|       zctlist xdlist ';'
 
112
|       zctlist xdecor
 
113
        {
 
114
                lastdcl = T;
 
115
                firstarg = S;
 
116
                dodecl(xdecl, lastclass, lasttype, $2);
 
117
                if(lastdcl == T || lastdcl->etype != TFUNC) {
 
118
                        diag($2, "not a function");
 
119
                        lastdcl = types[TFUNC];
 
120
                }
 
121
                thisfn = lastdcl;
 
122
                markdcl();
 
123
                firstdcl = dclstack;
 
124
                argmark($2, 0);
 
125
        }
 
126
        pdecl
 
127
        {
 
128
                argmark($2, 1);
 
129
        }
 
130
        block
 
131
        {
 
132
                Node *n;
 
133
 
 
134
                n = revertdcl();
 
135
                if(n)
 
136
                        $6 = new(OLIST, n, $6);
 
137
                if(!debug['a'] && !debug['Z'])
 
138
                        codgen($6, $2);
 
139
        }
 
140
 
 
141
xdlist:
 
142
        xdecor
 
143
        {
 
144
                dodecl(xdecl, lastclass, lasttype, $1);
 
145
        }
 
146
|       xdecor
 
147
        {
 
148
                $1 = dodecl(xdecl, lastclass, lasttype, $1);
 
149
        }
 
150
        '=' init
 
151
        {
 
152
                doinit($1->sym, $1->type, 0L, $4);
 
153
        }
 
154
|       xdlist ',' xdlist
 
155
 
 
156
xdecor:
 
157
        xdecor2
 
158
|       '*' zgnlist xdecor
 
159
        {
 
160
                $$ = new(OIND, $3, Z);
 
161
                $$->garb = simpleg($2);
 
162
        }
 
163
 
 
164
xdecor2:
 
165
        tag
 
166
|       '(' xdecor ')'
 
167
        {
 
168
                $$ = $2;
 
169
        }
 
170
|       xdecor2 '(' zarglist ')'
 
171
        {
 
172
                $$ = new(OFUNC, $1, $3);
 
173
        }
 
174
|       xdecor2 '[' zexpr ']'
 
175
        {
 
176
                $$ = new(OARRAY, $1, $3);
 
177
        }
 
178
 
 
179
/*
 
180
 * automatic declarator
 
181
 */
 
182
adecl:
 
183
        ctlist ';'
 
184
        {
 
185
                $$ = dodecl(adecl, lastclass, lasttype, Z);
 
186
        }
 
187
|       ctlist adlist ';'
 
188
        {
 
189
                $$ = $2;
 
190
        }
 
191
 
 
192
adlist:
 
193
        xdecor
 
194
        {
 
195
                dodecl(adecl, lastclass, lasttype, $1);
 
196
                $$ = Z;
 
197
        }
 
198
|       xdecor
 
199
        {
 
200
                $1 = dodecl(adecl, lastclass, lasttype, $1);
 
201
        }
 
202
        '=' init
 
203
        {
 
204
                int32 w;
 
205
 
 
206
                w = $1->sym->type->width;
 
207
                $$ = doinit($1->sym, $1->type, 0L, $4);
 
208
                $$ = contig($1->sym, $$, w);
 
209
        }
 
210
|       adlist ',' adlist
 
211
        {
 
212
                $$ = $1;
 
213
                if($3 != Z) {
 
214
                        $$ = $3;
 
215
                        if($1 != Z)
 
216
                                $$ = new(OLIST, $1, $3);
 
217
                }
 
218
        }
 
219
 
 
220
/*
 
221
 * parameter declarator
 
222
 */
 
223
pdecl:
 
224
|       pdecl ctlist pdlist ';'
 
225
 
 
226
pdlist:
 
227
        xdecor
 
228
        {
 
229
                dodecl(pdecl, lastclass, lasttype, $1);
 
230
        }
 
231
|       pdlist ',' pdlist
 
232
 
 
233
/*
 
234
 * structure element declarator
 
235
 */
 
236
edecl:
 
237
        tlist
 
238
        {
 
239
                lasttype = $1;
 
240
        }
 
241
        zedlist ';'
 
242
|       edecl tlist
 
243
        {
 
244
                lasttype = $2;
 
245
        }
 
246
        zedlist ';'
 
247
 
 
248
zedlist:                                        /* extension */
 
249
        {
 
250
                lastfield = 0;
 
251
                edecl(CXXX, lasttype, S);
 
252
        }
 
253
|       edlist
 
254
 
 
255
edlist:
 
256
        edecor
 
257
        {
 
258
                dodecl(edecl, CXXX, lasttype, $1);
 
259
        }
 
260
|       edlist ',' edlist
 
261
 
 
262
edecor:
 
263
        xdecor
 
264
        {
 
265
                lastbit = 0;
 
266
                firstbit = 1;
 
267
        }
 
268
|       tag ':' lexpr
 
269
        {
 
270
                $$ = new(OBIT, $1, $3);
 
271
        }
 
272
|       ':' lexpr
 
273
        {
 
274
                $$ = new(OBIT, Z, $2);
 
275
        }
 
276
 
 
277
/*
 
278
 * abstract declarator
 
279
 */
 
280
abdecor:
 
281
        {
 
282
                $$ = (Z);
 
283
        }
 
284
|       abdecor1
 
285
 
 
286
abdecor1:
 
287
        '*' zgnlist
 
288
        {
 
289
                $$ = new(OIND, (Z), Z);
 
290
                $$->garb = simpleg($2);
 
291
        }
 
292
|       '*' zgnlist abdecor1
 
293
        {
 
294
                $$ = new(OIND, $3, Z);
 
295
                $$->garb = simpleg($2);
 
296
        }
 
297
|       abdecor2
 
298
 
 
299
abdecor2:
 
300
        abdecor3
 
301
|       abdecor2 '(' zarglist ')'
 
302
        {
 
303
                $$ = new(OFUNC, $1, $3);
 
304
        }
 
305
|       abdecor2 '[' zexpr ']'
 
306
        {
 
307
                $$ = new(OARRAY, $1, $3);
 
308
        }
 
309
 
 
310
abdecor3:
 
311
        '(' ')'
 
312
        {
 
313
                $$ = new(OFUNC, (Z), Z);
 
314
        }
 
315
|       '[' zexpr ']'
 
316
        {
 
317
                $$ = new(OARRAY, (Z), $2);
 
318
        }
 
319
|       '(' abdecor1 ')'
 
320
        {
 
321
                $$ = $2;
 
322
        }
 
323
 
 
324
init:
 
325
        expr
 
326
|       '{' ilist '}'
 
327
        {
 
328
                $$ = new(OINIT, invert($2), Z);
 
329
        }
 
330
 
 
331
qual:
 
332
        '[' lexpr ']'
 
333
        {
 
334
                $$ = new(OARRAY, $2, Z);
 
335
        }
 
336
|       '.' ltag
 
337
        {
 
338
                $$ = new(OELEM, Z, Z);
 
339
                $$->sym = $2;
 
340
        }
 
341
|       qual '='
 
342
 
 
343
qlist:
 
344
        init ','
 
345
|       qlist init ','
 
346
        {
 
347
                $$ = new(OLIST, $1, $2);
 
348
        }
 
349
|       qual
 
350
|       qlist qual
 
351
        {
 
352
                $$ = new(OLIST, $1, $2);
 
353
        }
 
354
 
 
355
ilist:
 
356
        qlist
 
357
|       init
 
358
|       qlist init
 
359
        {
 
360
                $$ = new(OLIST, $1, $2);
 
361
        }
 
362
 
 
363
zarglist:
 
364
        {
 
365
                $$ = Z;
 
366
        }
 
367
|       arglist
 
368
        {
 
369
                $$ = invert($1);
 
370
        }
 
371
 
 
372
 
 
373
arglist:
 
374
        name
 
375
|       tlist abdecor
 
376
        {
 
377
                $$ = new(OPROTO, $2, Z);
 
378
                $$->type = $1;
 
379
        }
 
380
|       tlist xdecor
 
381
        {
 
382
                $$ = new(OPROTO, $2, Z);
 
383
                $$->type = $1;
 
384
        }
 
385
|       '.' '.' '.'
 
386
        {
 
387
                $$ = new(ODOTDOT, Z, Z);
 
388
        }
 
389
|       arglist ',' arglist
 
390
        {
 
391
                $$ = new(OLIST, $1, $3);
 
392
        }
 
393
 
 
394
block:
 
395
        '{' slist '}'
 
396
        {
 
397
                $$ = invert($2);
 
398
        //      if($2 != Z)
 
399
        //              $$ = new(OLIST, $2, $$);
 
400
                if($$ == Z)
 
401
                        $$ = new(OLIST, Z, Z);
 
402
        }
 
403
 
 
404
slist:
 
405
        {
 
406
                $$ = Z;
 
407
        }
 
408
|       slist adecl
 
409
        {
 
410
                $$ = new(OLIST, $1, $2);
 
411
        }
 
412
|       slist stmnt
 
413
        {
 
414
                $$ = new(OLIST, $1, $2);
 
415
        }
 
416
 
 
417
labels:
 
418
        label
 
419
|       labels label
 
420
        {
 
421
                $$ = new(OLIST, $1, $2);
 
422
        }
 
423
 
 
424
label:
 
425
        LCASE expr ':'
 
426
        {
 
427
                $$ = new(OCASE, $2, Z);
 
428
        }
 
429
|       LDEFAULT ':'
 
430
        {
 
431
                $$ = new(OCASE, Z, Z);
 
432
        }
 
433
|       LNAME ':'
 
434
        {
 
435
                $$ = new(OLABEL, dcllabel($1, 1), Z);
 
436
        }
 
437
 
 
438
stmnt:
 
439
        error ';'
 
440
        {
 
441
                $$ = Z;
 
442
        }
 
443
|       ulstmnt
 
444
|       labels ulstmnt
 
445
        {
 
446
                $$ = new(OLIST, $1, $2);
 
447
        }
 
448
 
 
449
forexpr:
 
450
        zcexpr
 
451
|       ctlist adlist
 
452
        {
 
453
                $$ = $2;
 
454
        }
 
455
 
 
456
ulstmnt:
 
457
        zcexpr ';'
 
458
|       {
 
459
                markdcl();
 
460
        }
 
461
        block
 
462
        {
 
463
                $$ = revertdcl();
 
464
                if($$)
 
465
                        $$ = new(OLIST, $$, $2);
 
466
                else
 
467
                        $$ = $2;
 
468
        }
 
469
|       LIF '(' cexpr ')' stmnt
 
470
        {
 
471
                $$ = new(OIF, $3, new(OLIST, $5, Z));
 
472
                if($5 == Z)
 
473
                        warn($3, "empty if body");
 
474
        }
 
475
|       LIF '(' cexpr ')' stmnt LELSE stmnt
 
476
        {
 
477
                $$ = new(OIF, $3, new(OLIST, $5, $7));
 
478
                if($5 == Z)
 
479
                        warn($3, "empty if body");
 
480
                if($7 == Z)
 
481
                        warn($3, "empty else body");
 
482
        }
 
483
|       { markdcl(); } LFOR '(' forexpr ';' zcexpr ';' zcexpr ')' stmnt
 
484
        {
 
485
                $$ = revertdcl();
 
486
                if($$){
 
487
                        if($4)
 
488
                                $4 = new(OLIST, $$, $4);
 
489
                        else
 
490
                                $4 = $$;
 
491
                }
 
492
                $$ = new(OFOR, new(OLIST, $6, new(OLIST, $4, $8)), $10);
 
493
        }
 
494
|       LWHILE '(' cexpr ')' stmnt
 
495
        {
 
496
                $$ = new(OWHILE, $3, $5);
 
497
        }
 
498
|       LDO stmnt LWHILE '(' cexpr ')' ';'
 
499
        {
 
500
                $$ = new(ODWHILE, $5, $2);
 
501
        }
 
502
|       LRETURN zcexpr ';'
 
503
        {
 
504
                $$ = new(ORETURN, $2, Z);
 
505
                $$->type = thisfn->link;
 
506
        }
 
507
|       LSWITCH '(' cexpr ')' stmnt
 
508
        {
 
509
                $$ = new(OCONST, Z, Z);
 
510
                $$->vconst = 0;
 
511
                $$->type = types[TINT];
 
512
                $3 = new(OSUB, $$, $3);
 
513
 
 
514
                $$ = new(OCONST, Z, Z);
 
515
                $$->vconst = 0;
 
516
                $$->type = types[TINT];
 
517
                $3 = new(OSUB, $$, $3);
 
518
 
 
519
                $$ = new(OSWITCH, $3, $5);
 
520
        }
 
521
|       LBREAK ';'
 
522
        {
 
523
                $$ = new(OBREAK, Z, Z);
 
524
        }
 
525
|       LCONTINUE ';'
 
526
        {
 
527
                $$ = new(OCONTINUE, Z, Z);
 
528
        }
 
529
|       LGOTO ltag ';'
 
530
        {
 
531
                $$ = new(OGOTO, dcllabel($2, 0), Z);
 
532
        }
 
533
|       LUSED '(' zelist ')' ';'
 
534
        {
 
535
                $$ = new(OUSED, $3, Z);
 
536
        }
 
537
|       LSET '(' zelist ')' ';'
 
538
        {
 
539
                $$ = new(OSET, $3, Z);
 
540
        }
 
541
 
 
542
zcexpr:
 
543
        {
 
544
                $$ = Z;
 
545
        }
 
546
|       cexpr
 
547
 
 
548
zexpr:
 
549
        {
 
550
                $$ = Z;
 
551
        }
 
552
|       lexpr
 
553
 
 
554
lexpr:
 
555
        expr
 
556
        {
 
557
                $$ = new(OCAST, $1, Z);
 
558
                $$->type = types[TLONG];
 
559
        }
 
560
 
 
561
cexpr:
 
562
        expr
 
563
|       cexpr ',' cexpr
 
564
        {
 
565
                $$ = new(OCOMMA, $1, $3);
 
566
        }
 
567
 
 
568
expr:
 
569
        xuexpr
 
570
|       expr '*' expr
 
571
        {
 
572
                $$ = new(OMUL, $1, $3);
 
573
        }
 
574
|       expr '/' expr
 
575
        {
 
576
                $$ = new(ODIV, $1, $3);
 
577
        }
 
578
|       expr '%' expr
 
579
        {
 
580
                $$ = new(OMOD, $1, $3);
 
581
        }
 
582
|       expr '+' expr
 
583
        {
 
584
                $$ = new(OADD, $1, $3);
 
585
        }
 
586
|       expr '-' expr
 
587
        {
 
588
                $$ = new(OSUB, $1, $3);
 
589
        }
 
590
|       expr LRSH expr
 
591
        {
 
592
                $$ = new(OASHR, $1, $3);
 
593
        }
 
594
|       expr LLSH expr
 
595
        {
 
596
                $$ = new(OASHL, $1, $3);
 
597
        }
 
598
|       expr '<' expr
 
599
        {
 
600
                $$ = new(OLT, $1, $3);
 
601
        }
 
602
|       expr '>' expr
 
603
        {
 
604
                $$ = new(OGT, $1, $3);
 
605
        }
 
606
|       expr LLE expr
 
607
        {
 
608
                $$ = new(OLE, $1, $3);
 
609
        }
 
610
|       expr LGE expr
 
611
        {
 
612
                $$ = new(OGE, $1, $3);
 
613
        }
 
614
|       expr LEQ expr
 
615
        {
 
616
                $$ = new(OEQ, $1, $3);
 
617
        }
 
618
|       expr LNE expr
 
619
        {
 
620
                $$ = new(ONE, $1, $3);
 
621
        }
 
622
|       expr '&' expr
 
623
        {
 
624
                $$ = new(OAND, $1, $3);
 
625
        }
 
626
|       expr '^' expr
 
627
        {
 
628
                $$ = new(OXOR, $1, $3);
 
629
        }
 
630
|       expr '|' expr
 
631
        {
 
632
                $$ = new(OOR, $1, $3);
 
633
        }
 
634
|       expr LANDAND expr
 
635
        {
 
636
                $$ = new(OANDAND, $1, $3);
 
637
        }
 
638
|       expr LOROR expr
 
639
        {
 
640
                $$ = new(OOROR, $1, $3);
 
641
        }
 
642
|       expr '?' cexpr ':' expr
 
643
        {
 
644
                $$ = new(OCOND, $1, new(OLIST, $3, $5));
 
645
        }
 
646
|       expr '=' expr
 
647
        {
 
648
                $$ = new(OAS, $1, $3);
 
649
        }
 
650
|       expr LPE expr
 
651
        {
 
652
                $$ = new(OASADD, $1, $3);
 
653
        }
 
654
|       expr LME expr
 
655
        {
 
656
                $$ = new(OASSUB, $1, $3);
 
657
        }
 
658
|       expr LMLE expr
 
659
        {
 
660
                $$ = new(OASMUL, $1, $3);
 
661
        }
 
662
|       expr LDVE expr
 
663
        {
 
664
                $$ = new(OASDIV, $1, $3);
 
665
        }
 
666
|       expr LMDE expr
 
667
        {
 
668
                $$ = new(OASMOD, $1, $3);
 
669
        }
 
670
|       expr LLSHE expr
 
671
        {
 
672
                $$ = new(OASASHL, $1, $3);
 
673
        }
 
674
|       expr LRSHE expr
 
675
        {
 
676
                $$ = new(OASASHR, $1, $3);
 
677
        }
 
678
|       expr LANDE expr
 
679
        {
 
680
                $$ = new(OASAND, $1, $3);
 
681
        }
 
682
|       expr LXORE expr
 
683
        {
 
684
                $$ = new(OASXOR, $1, $3);
 
685
        }
 
686
|       expr LORE expr
 
687
        {
 
688
                $$ = new(OASOR, $1, $3);
 
689
        }
 
690
 
 
691
xuexpr:
 
692
        uexpr
 
693
|       '(' tlist abdecor ')' xuexpr
 
694
        {
 
695
                $$ = new(OCAST, $5, Z);
 
696
                dodecl(NODECL, CXXX, $2, $3);
 
697
                $$->type = lastdcl;
 
698
                $$->xcast = 1;
 
699
        }
 
700
|       '(' tlist abdecor ')' '{' ilist '}'     /* extension */
 
701
        {
 
702
                $$ = new(OSTRUCT, $6, Z);
 
703
                dodecl(NODECL, CXXX, $2, $3);
 
704
                $$->type = lastdcl;
 
705
        }
 
706
 
 
707
uexpr:
 
708
        pexpr
 
709
|       '*' xuexpr
 
710
        {
 
711
                $$ = new(OIND, $2, Z);
 
712
        }
 
713
|       '&' xuexpr
 
714
        {
 
715
                $$ = new(OADDR, $2, Z);
 
716
        }
 
717
|       '+' xuexpr
 
718
        {
 
719
                $$ = new(OPOS, $2, Z);
 
720
        }
 
721
|       '-' xuexpr
 
722
        {
 
723
                $$ = new(ONEG, $2, Z);
 
724
        }
 
725
|       '!' xuexpr
 
726
        {
 
727
                $$ = new(ONOT, $2, Z);
 
728
        }
 
729
|       '~' xuexpr
 
730
        {
 
731
                $$ = new(OCOM, $2, Z);
 
732
        }
 
733
|       LPP xuexpr
 
734
        {
 
735
                $$ = new(OPREINC, $2, Z);
 
736
        }
 
737
|       LMM xuexpr
 
738
        {
 
739
                $$ = new(OPREDEC, $2, Z);
 
740
        }
 
741
|       LSIZEOF uexpr
 
742
        {
 
743
                $$ = new(OSIZE, $2, Z);
 
744
        }
 
745
|       LSIGNOF uexpr
 
746
        {
 
747
                $$ = new(OSIGN, $2, Z);
 
748
        }
 
749
 
 
750
pexpr:
 
751
        '(' cexpr ')'
 
752
        {
 
753
                $$ = $2;
 
754
        }
 
755
|       LSIZEOF '(' tlist abdecor ')'
 
756
        {
 
757
                $$ = new(OSIZE, Z, Z);
 
758
                dodecl(NODECL, CXXX, $3, $4);
 
759
                $$->type = lastdcl;
 
760
        }
 
761
|       LSIGNOF '(' tlist abdecor ')'
 
762
        {
 
763
                $$ = new(OSIGN, Z, Z);
 
764
                dodecl(NODECL, CXXX, $3, $4);
 
765
                $$->type = lastdcl;
 
766
        }
 
767
|       pexpr '(' zelist ')'
 
768
        {
 
769
                $$ = new(OFUNC, $1, Z);
 
770
                if($1->op == ONAME)
 
771
                if($1->type == T)
 
772
                        dodecl(xdecl, CXXX, types[TINT], $$);
 
773
                $$->right = invert($3);
 
774
        }
 
775
|       pexpr '[' cexpr ']'
 
776
        {
 
777
                $$ = new(OIND, new(OADD, $1, $3), Z);
 
778
        }
 
779
|       pexpr LMG ltag
 
780
        {
 
781
                $$ = new(ODOT, new(OIND, $1, Z), Z);
 
782
                $$->sym = $3;
 
783
        }
 
784
|       pexpr '.' ltag
 
785
        {
 
786
                $$ = new(ODOT, $1, Z);
 
787
                $$->sym = $3;
 
788
        }
 
789
|       pexpr LPP
 
790
        {
 
791
                $$ = new(OPOSTINC, $1, Z);
 
792
        }
 
793
|       pexpr LMM
 
794
        {
 
795
                $$ = new(OPOSTDEC, $1, Z);
 
796
        }
 
797
|       name
 
798
|       LCONST
 
799
        {
 
800
                $$ = new(OCONST, Z, Z);
 
801
                $$->type = types[TINT];
 
802
                $$->vconst = $1;
 
803
                $$->cstring = strdup(symb);
 
804
        }
 
805
|       LLCONST
 
806
        {
 
807
                $$ = new(OCONST, Z, Z);
 
808
                $$->type = types[TLONG];
 
809
                $$->vconst = $1;
 
810
                $$->cstring = strdup(symb);
 
811
        }
 
812
|       LUCONST
 
813
        {
 
814
                $$ = new(OCONST, Z, Z);
 
815
                $$->type = types[TUINT];
 
816
                $$->vconst = $1;
 
817
                $$->cstring = strdup(symb);
 
818
        }
 
819
|       LULCONST
 
820
        {
 
821
                $$ = new(OCONST, Z, Z);
 
822
                $$->type = types[TULONG];
 
823
                $$->vconst = $1;
 
824
                $$->cstring = strdup(symb);
 
825
        }
 
826
|       LDCONST
 
827
        {
 
828
                $$ = new(OCONST, Z, Z);
 
829
                $$->type = types[TDOUBLE];
 
830
                $$->fconst = $1;
 
831
                $$->cstring = strdup(symb);
 
832
        }
 
833
|       LFCONST
 
834
        {
 
835
                $$ = new(OCONST, Z, Z);
 
836
                $$->type = types[TFLOAT];
 
837
                $$->fconst = $1;
 
838
                $$->cstring = strdup(symb);
 
839
        }
 
840
|       LVLCONST
 
841
        {
 
842
                $$ = new(OCONST, Z, Z);
 
843
                $$->type = types[TVLONG];
 
844
                $$->vconst = $1;
 
845
                $$->cstring = strdup(symb);
 
846
        }
 
847
|       LUVLCONST
 
848
        {
 
849
                $$ = new(OCONST, Z, Z);
 
850
                $$->type = types[TUVLONG];
 
851
                $$->vconst = $1;
 
852
                $$->cstring = strdup(symb);
 
853
        }
 
854
|       string
 
855
|       lstring
 
856
 
 
857
string:
 
858
        LSTRING
 
859
        {
 
860
                $$ = new(OSTRING, Z, Z);
 
861
                $$->type = typ(TARRAY, types[TCHAR]);
 
862
                $$->type->width = $1.l + 1;
 
863
                $$->cstring = $1.s;
 
864
                $$->sym = symstring;
 
865
                $$->etype = TARRAY;
 
866
                $$->class = CSTATIC;
 
867
        }
 
868
|       string LSTRING
 
869
        {
 
870
                char *s;
 
871
                int n;
 
872
 
 
873
                n = $1->type->width - 1;
 
874
                s = alloc(n+$2.l+MAXALIGN);
 
875
 
 
876
                memcpy(s, $1->cstring, n);
 
877
                memcpy(s+n, $2.s, $2.l);
 
878
                s[n+$2.l] = 0;
 
879
 
 
880
                $$ = $1;
 
881
                $$->type->width += $2.l;
 
882
                $$->cstring = s;
 
883
        }
 
884
 
 
885
lstring:
 
886
        LLSTRING
 
887
        {
 
888
                $$ = new(OLSTRING, Z, Z);
 
889
                $$->type = typ(TARRAY, types[TUSHORT]);
 
890
                $$->type->width = $1.l + sizeof(ushort);
 
891
                $$->rstring = (ushort*)$1.s;
 
892
                $$->sym = symstring;
 
893
                $$->etype = TARRAY;
 
894
                $$->class = CSTATIC;
 
895
        }
 
896
|       lstring LLSTRING
 
897
        {
 
898
                char *s;
 
899
                int n;
 
900
 
 
901
                n = $1->type->width - sizeof(ushort);
 
902
                s = alloc(n+$2.l+MAXALIGN);
 
903
 
 
904
                memcpy(s, $1->rstring, n);
 
905
                memcpy(s+n, $2.s, $2.l);
 
906
                *(ushort*)(s+n+$2.l) = 0;
 
907
 
 
908
                $$ = $1;
 
909
                $$->type->width += $2.l;
 
910
                $$->rstring = (ushort*)s;
 
911
        }
 
912
 
 
913
zelist:
 
914
        {
 
915
                $$ = Z;
 
916
        }
 
917
|       elist
 
918
 
 
919
elist:
 
920
        expr
 
921
|       elist ',' elist
 
922
        {
 
923
                $$ = new(OLIST, $1, $3);
 
924
        }
 
925
 
 
926
sbody:
 
927
        '{'
 
928
        {
 
929
                $<tyty>$.t1 = strf;
 
930
                $<tyty>$.t2 = strl;
 
931
                $<tyty>$.t3 = lasttype;
 
932
                $<tyty>$.c = lastclass;
 
933
                strf = T;
 
934
                strl = T;
 
935
                lastbit = 0;
 
936
                firstbit = 1;
 
937
                lastclass = CXXX;
 
938
                lasttype = T;
 
939
        }
 
940
        edecl '}'
 
941
        {
 
942
                $$ = strf;
 
943
                strf = $<tyty>2.t1;
 
944
                strl = $<tyty>2.t2;
 
945
                lasttype = $<tyty>2.t3;
 
946
                lastclass = $<tyty>2.c;
 
947
        }
 
948
 
 
949
zctlist:
 
950
        {
 
951
                lastclass = CXXX;
 
952
                lasttype = types[TINT];
 
953
        }
 
954
|       ctlist
 
955
 
 
956
types:
 
957
        complex
 
958
        {
 
959
                $$.t = $1;
 
960
                $$.c = CXXX;
 
961
        }
 
962
|       tname
 
963
        {
 
964
                $$.t = simplet($1);
 
965
                $$.c = CXXX;
 
966
        }
 
967
|       gcnlist
 
968
        {
 
969
                $$.t = simplet($1);
 
970
                $$.c = simplec($1);
 
971
                $$.t = garbt($$.t, $1);
 
972
        }
 
973
|       complex gctnlist
 
974
        {
 
975
                $$.t = $1;
 
976
                $$.c = simplec($2);
 
977
                $$.t = garbt($$.t, $2);
 
978
                if($2 & ~BCLASS & ~BGARB)
 
979
                        diag(Z, "duplicate types given: %T and %Q", $1, $2);
 
980
        }
 
981
|       tname gctnlist
 
982
        {
 
983
                $$.t = simplet(typebitor($1, $2));
 
984
                $$.c = simplec($2);
 
985
                $$.t = garbt($$.t, $2);
 
986
        }
 
987
|       gcnlist complex zgnlist
 
988
        {
 
989
                $$.t = $2;
 
990
                $$.c = simplec($1);
 
991
                $$.t = garbt($$.t, $1|$3);
 
992
        }
 
993
|       gcnlist tname
 
994
        {
 
995
                $$.t = simplet($2);
 
996
                $$.c = simplec($1);
 
997
                $$.t = garbt($$.t, $1);
 
998
        }
 
999
|       gcnlist tname gctnlist
 
1000
        {
 
1001
                $$.t = simplet(typebitor($2, $3));
 
1002
                $$.c = simplec($1|$3);
 
1003
                $$.t = garbt($$.t, $1|$3);
 
1004
        }
 
1005
 
 
1006
tlist:
 
1007
        types
 
1008
        {
 
1009
                $$ = $1.t;
 
1010
                if($1.c != CXXX)
 
1011
                        diag(Z, "illegal combination of class 4: %s", cnames[$1.c]);
 
1012
        }
 
1013
 
 
1014
ctlist:
 
1015
        types
 
1016
        {
 
1017
                lasttype = $1.t;
 
1018
                lastclass = $1.c;
 
1019
        }
 
1020
 
 
1021
complex:
 
1022
        LSTRUCT ltag
 
1023
        {
 
1024
                dotag($2, TSTRUCT, 0);
 
1025
                $$ = $2->suetag;
 
1026
        }
 
1027
|       LSTRUCT ltag
 
1028
        {
 
1029
                dotag($2, TSTRUCT, autobn);
 
1030
        }
 
1031
        sbody
 
1032
        {
 
1033
                $$ = $2->suetag;
 
1034
                if($$->link != T)
 
1035
                        diag(Z, "redeclare tag: %s", $2->name);
 
1036
                $$->link = $4;
 
1037
                sualign($$);
 
1038
        }
 
1039
|       LSTRUCT sbody
 
1040
        {
 
1041
                taggen++;
 
1042
                sprint(symb, "_%d_", taggen);
 
1043
                $$ = dotag(lookup(), TSTRUCT, autobn);
 
1044
                $$->link = $2;
 
1045
                sualign($$);
 
1046
        }
 
1047
|       LUNION ltag
 
1048
        {
 
1049
                dotag($2, TUNION, 0);
 
1050
                $$ = $2->suetag;
 
1051
        }
 
1052
|       LUNION ltag
 
1053
        {
 
1054
                dotag($2, TUNION, autobn);
 
1055
        }
 
1056
        sbody
 
1057
        {
 
1058
                $$ = $2->suetag;
 
1059
                if($$->link != T)
 
1060
                        diag(Z, "redeclare tag: %s", $2->name);
 
1061
                $$->link = $4;
 
1062
                sualign($$);
 
1063
        }
 
1064
|       LUNION sbody
 
1065
        {
 
1066
                taggen++;
 
1067
                sprint(symb, "_%d_", taggen);
 
1068
                $$ = dotag(lookup(), TUNION, autobn);
 
1069
                $$->link = $2;
 
1070
                sualign($$);
 
1071
        }
 
1072
|       LENUM ltag
 
1073
        {
 
1074
                dotag($2, TENUM, 0);
 
1075
                $$ = $2->suetag;
 
1076
                if($$->link == T)
 
1077
                        $$->link = types[TINT];
 
1078
                $$ = $$->link;
 
1079
        }
 
1080
|       LENUM ltag
 
1081
        {
 
1082
                dotag($2, TENUM, autobn);
 
1083
        }
 
1084
        '{'
 
1085
        {
 
1086
                en.tenum = T;
 
1087
                en.cenum = T;
 
1088
        }
 
1089
        enum '}'
 
1090
        {
 
1091
                $$ = $2->suetag;
 
1092
                if($$->link != T)
 
1093
                        diag(Z, "redeclare tag: %s", $2->name);
 
1094
                if(en.tenum == T) {
 
1095
                        diag(Z, "enum type ambiguous: %s", $2->name);
 
1096
                        en.tenum = types[TINT];
 
1097
                }
 
1098
                $$->link = en.tenum;
 
1099
                $$ = en.tenum;
 
1100
        }
 
1101
|       LENUM '{'
 
1102
        {
 
1103
                en.tenum = T;
 
1104
                en.cenum = T;
 
1105
        }
 
1106
        enum '}'
 
1107
        {
 
1108
                $$ = en.tenum;
 
1109
        }
 
1110
|       LTYPE
 
1111
        {
 
1112
                $$ = tcopy($1->type);
 
1113
        }
 
1114
 
 
1115
gctnlist:
 
1116
        gctname
 
1117
|       gctnlist gctname
 
1118
        {
 
1119
                $$ = typebitor($1, $2);
 
1120
        }
 
1121
 
 
1122
zgnlist:
 
1123
        {
 
1124
                $$ = 0;
 
1125
        }
 
1126
|       zgnlist gname
 
1127
        {
 
1128
                $$ = typebitor($1, $2);
 
1129
        }
 
1130
 
 
1131
gctname:
 
1132
        tname
 
1133
|       gname
 
1134
|       cname
 
1135
 
 
1136
gcnlist:
 
1137
        gcname
 
1138
|       gcnlist gcname
 
1139
        {
 
1140
                $$ = typebitor($1, $2);
 
1141
        }
 
1142
 
 
1143
gcname:
 
1144
        gname
 
1145
|       cname
 
1146
 
 
1147
enum:
 
1148
        LNAME
 
1149
        {
 
1150
                doenum($1, Z);
 
1151
        }
 
1152
|       LNAME '=' expr
 
1153
        {
 
1154
                doenum($1, $3);
 
1155
        }
 
1156
|       enum ','
 
1157
|       enum ',' enum
 
1158
 
 
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; }
 
1169
 
 
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; }
 
1178
 
 
1179
gname:  /* garbage words */
 
1180
        LCONSTNT { $$ = BCONSTNT; }
 
1181
|       LVOLATILE { $$ = BVOLATILE; }
 
1182
|       LRESTRICT { $$ = 0; }
 
1183
 
 
1184
name:
 
1185
        LNAME
 
1186
        {
 
1187
                $$ = new(ONAME, Z, Z);
 
1188
                if($1->class == CLOCAL)
 
1189
                        $1 = mkstatic($1);
 
1190
                $$->sym = $1;
 
1191
                $$->type = $1->type;
 
1192
                $$->etype = TVOID;
 
1193
                if($$->type != T)
 
1194
                        $$->etype = $$->type->etype;
 
1195
                $$->xoffset = $1->offset;
 
1196
                $$->class = $1->class;
 
1197
                $1->aused = 1;
 
1198
        }
 
1199
tag:
 
1200
        ltag
 
1201
        {
 
1202
                $$ = new(ONAME, Z, Z);
 
1203
                $$->sym = $1;
 
1204
                $$->type = $1->type;
 
1205
                $$->etype = TVOID;
 
1206
                if($$->type != T)
 
1207
                        $$->etype = $$->type->etype;
 
1208
                $$->xoffset = $1->offset;
 
1209
                $$->class = $1->class;
 
1210
        }
 
1211
ltag:
 
1212
        LNAME
 
1213
|       LTYPE
 
1214
%%