Debian Codegraph @

cc/ccom/cgram.y

raw
1 /* $Id: cgram.y,v 1.422 2019/09/08 09:30:51 ragge Exp $ */
2
3 /*
4 * Copyright (c) 2003 Anders Magnusson ([email protected]).
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
30 *
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
33 * are met:
34 *
35 * Redistributions of source code and documentation must retain the above
36 * copyright notice, this list of conditions and the following disclaimer.
37 * Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed or owned by Caldera
43 * International, Inc.
44 * Neither the name of Caldera International, Inc. nor the names of other
45 * contributors may be used to endorse or promote products derived from
46 * this software without specific prior written permission.
47 *
48 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
49 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
50 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
51 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
53 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
57 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
58 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
59 * POSSIBILITY OF SUCH DAMAGE.
60 */
61
62 /*
63 * Comments for this grammar file. Ragge 021123
64 *
65 * ANSI support required rewrite of the function header and declaration
66 * rules almost totally.
67 *
68 * The lex/yacc shared keywords are now split from the keywords used
69 * in the rest of the compiler, to simplify use of other frontends.
70 */
71
72 /*
73 * At last count, there were 5 shift/reduce and no reduce/reduce conflicts
74 * All are accounted for;
75 * One is "dangling else"
76 * Two is in attribute parsing
77 * Two is in ({ }) parsing
78 */
79
80 /*
81 * Token used in C lex/yacc communications.
82 */
83 %token C_STRING /* a string constant */
84 %token C_ICON /* an integer constant */
85 %token C_FCON /* a floating point constant */
86 %token C_NAME /* an identifier */
87 %token C_TYPENAME /* a typedef'd name */
88 %token C_ANDAND /* && */
89 %token C_OROR /* || */
90 %token C_GOTO /* unconditional goto */
91 %token C_RETURN /* return from function */
92 %token C_TYPE /* a type */
93 %token C_CLASS /* a storage class */
94 %token C_ASOP /* assignment ops */
95 %token C_RELOP /* <=, <, >=, > */
96 %token C_EQUOP /* ==, != */
97 %token C_DIVOP /* /, % */
98 %token C_SHIFTOP /* <<, >> */
99 %token C_INCOP /* ++, -- */
100 %token C_UNOP /* !, ~ */
101 %token C_STROP /* ., -> */
102 %token C_STRUCT
103 %token C_IF
104 %token C_ELSE
105 %token C_SWITCH
106 %token C_BREAK
107 %token C_CONTINUE
108 %token C_WHILE
109 %token C_DO
110 %token C_FOR
111 %token C_DEFAULT
112 %token C_CASE
113 %token C_SIZEOF
114 %token C_ENUM
115 %token C_ELLIPSIS
116 %token C_QUALIFIER
117 %token C_FUNSPEC
118 %token C_ASM
119 %token NOMATCH
120 %token C_TYPEOF /* COMPAT_GCC */
121 %token C_ATTRIBUTE /* COMPAT_GCC */
122 %token PCC_OFFSETOF
123 %token GCC_DESIG
124
125 /* C11 keywords */
126 %token C_STATICASSERT
127 %token C_ALIGNAS
128 %token C_ALIGNOF
129 %token C_GENERIC
130 %token C_ATOMIC
131
132 /*
133 * Precedence
134 */
135 %left ','
136 %right '=' C_ASOP
137 %right '?' ':'
138 %left C_OROR
139 %left C_ANDAND
140 %left '|'
141 %left '^'
142 %left '&'
143 %left C_EQUOP
144 %left C_RELOP
145 %left C_SHIFTOP
146 %left '+' '-'
147 %left '*' C_DIVOP
148 %right C_UNOP
149 %right C_INCOP C_SIZEOF
150 %left '[' '(' C_STROP
151 %{
152 # include "pass1.h"
153 # include <stdarg.h>
154 # include <string.h>
155 # include <stdlib.h>
156
157 int fun_inline; /* Reading an inline function */
158 int oldstyle; /* Current function being defined */
159 static struct symtab *xnf;
160 extern int enummer, tvaloff, inattr;
161 extern struct rstack *rpole;
162 static int alwinl;
163 P1ND *cftnod;
164 static int attrwarn = 1;
165
166 #define NORETYP SNOCREAT /* no return type, save in unused field in symtab */
167
168 struct genlist {
169 struct genlist *next;
170 P1ND *p;
171 TWORD t;
172 };
173
174 P1ND *bdty(int op, ...);
175 static void fend(void);
176 static void fundef(P1ND *tp, P1ND *p);
177 static void olddecl(P1ND *p, P1ND *a);
178 static struct symtab *init_declarator(P1ND *tn, P1ND *p, int assign, P1ND *a,
179 char *as);
180 static void resetbc(int mask);
181 static void swend(void);
182 static void addcase(P1ND *p);
183 #ifdef GCC_COMPAT
184 static void gcccase(P1ND *p, P1ND *);
185 #endif
186 static struct attr *gcc_attr_wrapper(P1ND *p);
187 static void adddef(void);
188 static void savebc(void);
189 static void swstart(int, TWORD);
190 static void genswitch(int, TWORD, struct swents **, int);
191 static char *mkpstr(char *str);
192 static struct symtab *clbrace(P1ND *);
193 static P1ND *cmop(P1ND *l, P1ND *r);
194 static P1ND *xcmop(P1ND *out, P1ND *in, P1ND *str);
195 static void mkxasm(char *str, P1ND *p);
196 static P1ND *xasmop(char *str, P1ND *p);
197 static P1ND *biop(int op, P1ND *l, P1ND *r);
198 static void flend(void);
199 static P1ND *gccexpr(int bn, P1ND *q);
200 static char * simname(char *s);
201 static P1ND *tyof(P1ND *); /* COMPAT_GCC */
202 static P1ND *voidcon(void);
203 static P1ND *funargs(P1ND *p);
204 static void oldargs(P1ND *p);
205 static void uawarn(P1ND *p, char *s);
206 static int con_e(P1ND *p);
207 static void dainit(P1ND *d, P1ND *a);
208 static P1ND *tymfix(P1ND *p);
209 static P1ND *namekill(P1ND *p, int clr);
210 static P1ND *aryfix(P1ND *p);
211 static P1ND *dogen(struct genlist *g, P1ND *e);
212 static struct genlist *newgen(P1ND *p, P1ND *q);
213 static struct genlist *addgen(struct genlist *g, struct genlist *h);
214
215 static void savlab(int);
216 static void xcbranch(P1ND *, int);
217 extern int *mkclabs(void);
218
219 #define TYMFIX(inp) { \
220 P1ND *pp = inp; \
221 inp = tymerge(pp->n_left, pp->n_right); \
222 p1nfree(pp->n_left); p1nfree(pp); }
223
224 struct xalloc;
225 extern struct xalloc *bkpole, *sapole;
226 extern int cbkp, cstp;
227 extern int usdnodes;
228 struct bks {
229 struct xalloc *ptr;
230 int off;
231 };
232
233 /*
234 * State for saving current switch state (when nested switches).
235 */
236 struct savbc {
237 struct savbc *next;
238 int brklab;
239 int contlab;
240 int flostat;
241 int swx;
242 struct xalloc *bkptr;
243 int bkoff;
244 struct xalloc *stptr;
245 int stoff;
246 int numnode;
247 } *savbc, *savctx;
248
249 %}
250
251 %union {
252 TWORD type;
253 int intval;
254 P1ND *nodep;
255 struct symtab *symp;
256 struct rstack *rp;
257 char *strp;
258 struct bks *bkp;
259 struct flt flt;
260 struct lexint li;
261 struct genlist *g;
262 }
263
264 /* define types */
265 %start ext_def_list
266
267 %type <intval> ifelprefix ifprefix whprefix forprefix doprefix switchpart
268 xbegin
269 %type <nodep> e .e term enum_dcl struct_dcl cast_type declarator
270 elist type_sq cf_spec merge_attribs e2 ecq
271 parameter_declaration abstract_declarator initializer
272 parameter_type_list parameter_list
273 declaration_specifiers designation
274 specifier_qualifier_list merge_specifiers
275 identifier_list arg_param_list type_qualifier_list
276 designator_list designator xasm oplist oper cnstr funtype
277 typeof attribute attribute_specifier /* COMPAT_GCC */
278 attribute_list attr_spec_list attr_var /* COMPAT_GCC */
279
280 %type <g> gen_ass_list gen_assoc
281 %type <strp> string C_STRING GCC_DESIG svstr
282 %type <rp> str_head
283 %type <symp> xnfdeclarator clbrace enum_head
284
285 %type <intval> C_STRUCT C_RELOP C_DIVOP C_SHIFTOP
286 C_ANDAND C_OROR C_STROP C_INCOP C_UNOP C_ASOP C_EQUOP
287
288 %type <type> C_TYPE C_QUALIFIER C_CLASS C_FUNSPEC
289
290 %type <li> C_ICON
291
292 %type <flt> C_FCON
293
294 %type <strp> C_NAME C_TYPENAME
295 %%
296
297 ext_def_list: ext_def_list external_def
298 | { ftnend(); }
299 ;
300
301 external_def: funtype kr_args compoundstmt { fend(); }
302 | declaration { blevel = 0; symclear(0); }
303 | asmstatement ';'
304 | ';'
305 | error { blevel = 0; }
306 ;
307
308 funtype: /* no type given */ declarator {
309 fundef(mkty(INT, 0, 0), $1);
310 cftnsp->sflags |= NORETYP;
311 }
312 | declaration_specifiers declarator { fundef($1,$2); }
313 ;
314
315 kr_args: /* empty */
316 | arg_dcl_list
317 ;
318
319 /*
320 * Returns a node pointer or NULL, if no types at all given.
321 * Type trees are checked for correctness and merged into one
322 * type node in typenode().
323 */
324 declaration_specifiers:
325 merge_attribs { $$ = typenode($1); }
326 ;
327
328 merge_attribs: type_sq { $$ = $1; }
329 | type_sq merge_attribs { $$ = cmop($2, $1); }
330 | cf_spec { $$ = $1; }
331 | cf_spec merge_attribs { $$ = cmop($2, $1); }
332 ;
333
334 type_sq: C_TYPE { $$ = mkty($1, 0, 0); }
335 | C_TYPENAME {
336 struct symtab *sp = lookup($1, 0);
337 if (sp->stype == ENUMTY) {
338 sp->stype = strmemb(sp->sap)->stype;
339 }
340 $$ = mkty(sp->stype, sp->sdf, sp->sap);
341 $$->n_sp = sp;
342 }
343 | struct_dcl { $$ = $1; }
344 | enum_dcl { $$ = $1; }
345 | C_QUALIFIER { $$ = block(QUALIFIER, NULL, NULL, 0, 0, 0); $$->n_qual = $1; }
346 | attribute_specifier { $$ = biop(ATTRIB, $1, 0); }
347 | C_ALIGNAS '(' e ')' {
348 $$ = biop(ALIGN, NULL, NULL);
349 slval($$, con_e($3));
350 }
351 | C_ALIGNAS '(' cast_type ')' {
352 TYMFIX($3);
353 $$ = biop(ALIGN, NULL, NULL);
354 slval($$, talign($3->n_type, $3->n_ap)/SZCHAR);
355 p1tfree($3);
356 }
357 | C_ATOMIC { uerror("_Atomic not supported"); $$ = bcon(0); }
358 | C_ATOMIC '(' cast_type ')' {
359 uerror("_Atomic not supported"); $$ = $3;
360 }
361 | typeof { $$ = $1; }
362 ;
363
364 cf_spec: C_CLASS { $$ = block(CLASS, NULL, NULL, $1, 0, 0); }
365 | C_FUNSPEC { $$ = block(FUNSPEC, NULL, NULL, $1, 0, 0); }
366 ;
367
368 typeof: C_TYPEOF '(' e ')' { $$ = tyof(eve($3)); }
369 | C_TYPEOF '(' cast_type ')' { TYMFIX($3); $$ = tyof($3); }
370 ;
371
372 attribute_specifier :
373 C_ATTRIBUTE '(' '(' attribute_list ')' ')' { $$ = $4; }
374 /*COMPAT_GCC*/ ;
375
376 attribute_list: attribute
377 | attribute ',' attribute_list { $$ = cmop($3, $1); }
378 ;
379
380 attribute: {
381 #ifdef GCC_COMPAT
382 $$ = voidcon();
383 #endif
384 }
385 | C_NAME { $$ = bdty(NAME, $1); }
386 | C_NAME '(' elist ')' {
387 $$ = bdty($3 == NULL ? UCALL : CALL, bdty(NAME, $1), $3);
388 }
389 ;
390
391 /*
392 * Adds a pointer list to front of the declarators.
393 */
394 declarator: '*' declarator { $$ = bdty(UMUL, $2); }
395 | '*' type_qualifier_list declarator {
396 $$ = $2;
397 $$->n_left = $3;
398 }
399 | C_NAME { $$ = bdty(NAME, $1); }
400 | '(' attr_spec_list declarator ')' {
401 $$ = $3;
402 $$->n_ap = attr_add($$->n_ap, gcc_attr_wrapper($2));
403 }
404 | '(' declarator ')' { $$ = $2; }
405 | declarator '[' ecq ']' { $$ = biop(LB, $1, $3); }
406 | declarator '(' parameter_type_list ')' {
407 $$ = bdty(CALL, $1, $3);
408 }
409 | declarator '(' identifier_list ')' {
410 $$ = bdty(CALL, $1, $3);
411 oldstyle = 1;
412 }
413 | declarator '(' ')' { $$ = bdty(UCALL, $1); }
414 ;
415
416 ecq: maybe_r { $$ = bcon(NOOFFSET); }
417 | e { $$ = $1; }
418 | r e { $$ = $2; }
419 | c maybe_r e { $$ = $3; }
420 | r c e { $$ = $3; }
421 | '*' { $$ = bcon(NOOFFSET); }
422 | r '*' { $$ = bcon(NOOFFSET); }
423 ;
424
425 r: C_QUALIFIER {
426 if ($1 != 0)
427 uerror("bad qualifier");
428 }
429 ;
430
431 c: C_CLASS {
432 if ($1 != STATIC)
433 uerror("bad class keyword");
434 }
435 ;
436
437 type_qualifier_list:
438 C_QUALIFIER { $$ = biop(UMUL, 0, 0); $$->n_qual = $1; }
439 | type_qualifier_list C_QUALIFIER {
440 $$ = $1;
441 $$->n_qual |= $2;
442 }
443 | attribute_specifier {
444 $$ = block(UMUL, NULL, NULL, 0, 0, gcc_attr_wrapper($1));
445 }
446 | type_qualifier_list attribute_specifier {
447 $1->n_ap = attr_add($1->n_ap, gcc_attr_wrapper($2));
448 }
449 ;
450
451 identifier_list: C_NAME { $$ = bdty(NAME, $1); oldargs($$); }
452 | identifier_list ',' C_NAME {
453 $$ = cmop($1, bdty(NAME, $3));
454 oldargs($$->n_right);
455 }
456 ;
457
458 /*
459 * Returns as parameter_list, but can add an additional ELLIPSIS node.
460 */
461 parameter_type_list:
462 parameter_list { $$ = $1; }
463 | parameter_list ',' C_ELLIPSIS {
464 $$ = cmop($1, biop(ELLIPSIS, NULL, NULL));
465 }
466 ;
467
468 /*
469 * Returns a linked lists of nodes of op CM with parameters on
470 * its right and additional CM nodes of its left pointer.
471 * No CM nodes if only one parameter.
472 */
473 parameter_list: parameter_declaration { $$ = $1; }
474 | parameter_list ',' parameter_declaration {
475 $$ = cmop($1, $3);
476 }
477 ;
478
479 /*
480 * Returns a node pointer to the declaration.
481 */
482 parameter_declaration:
483 declaration_specifiers declarator attr_var {
484 if (glval($1) != SNULL && glval($1) != REGISTER)
485 uerror("illegal parameter class");
486 $$ = block(TYMERGE, $1, $2, INT, 0,
487 gcc_attr_wrapper($3));
488 }
489 | declaration_specifiers abstract_declarator {
490 $1->n_ap = attr_add($1->n_ap, $2->n_ap);
491 $$ = block(TYMERGE, $1, $2, INT, 0, 0);
492 }
493 | declaration_specifiers {
494 $$ = block(TYMERGE, $1, bdty(NAME, NULL), INT, 0, 0);
495 }
496 ;
497
498 abstract_declarator:
499 '*' { $$ = bdty(UMUL, bdty(NAME, NULL)); }
500 | '*' type_qualifier_list {
501 $$ = $2;
502 $$->n_left = bdty(NAME, NULL);
503 }
504 | '*' abstract_declarator { $$ = bdty(UMUL, $2); }
505 | '*' type_qualifier_list abstract_declarator {
506 $$ = $2;
507 $$->n_left = $3;
508 }
509 | '(' abstract_declarator ')' { $$ = $2; }
510 | '[' maybe_r ']' attr_var {
511 $$ = block(LB, bdty(NAME, NULL), bcon(NOOFFSET),
512 INT, 0, gcc_attr_wrapper($4));
513 }
514 | '[' e ']' attr_var {
515 $$ = block(LB, bdty(NAME, NULL), $2,
516 INT, 0, gcc_attr_wrapper($4));
517 }
518 | abstract_declarator '[' maybe_r ']' attr_var {
519 $$ = block(LB, $1, bcon(NOOFFSET),
520 INT, 0, gcc_attr_wrapper($5));
521 }
522 | abstract_declarator '[' e ']' attr_var {
523 $$ = block(LB, $1, $3, INT, 0, gcc_attr_wrapper($5));
524 }
525 | '(' ')' attr_var {
526 $$ = bdty(UCALL, bdty(NAME, NULL));
527 $$->n_ap = gcc_attr_wrapper($3);
528 }
529 | '(' ib2 parameter_type_list ')' attr_var {
530 $$ = block(CALL, bdty(NAME, NULL), $3, INT, 0,
531 gcc_attr_wrapper($5));
532 }
533 | abstract_declarator '(' ')' attr_var {
534 $$ = block(UCALL, $1, NULL, INT, 0, gcc_attr_wrapper($4));
535 }
536 | abstract_declarator '(' ib2 parameter_type_list ')' attr_var {
537 $$ = block(CALL, $1, $4, INT, 0, gcc_attr_wrapper($6));
538 }
539 ;
540
541 ib2: { }
542 ;
543
544 maybe_r: { }
545 | C_QUALIFIER { }
546 ;
547
548 /*
549 * K&R arg declaration, between ) and {
550 */
551 arg_dcl_list: arg_declaration
552 | arg_dcl_list arg_declaration
553 ;
554
555
556 arg_declaration: declaration_specifiers arg_param_list ';' {
557 p1nfree($1);
558 }
559 ;
560
561 arg_param_list: declarator attr_var {
562 olddecl(block(TYMERGE, p1tcopy($<nodep>0), $1,
563 INT, 0, 0), $2);
564 }
565 | arg_param_list ',' declarator attr_var {
566 olddecl(block(TYMERGE, p1tcopy($<nodep>0), $3,
567 INT, 0, 0), $4);
568 }
569 ;
570
571 /*
572 * Declarations in beginning of blocks.
573 */
574 block_item_list: block_item
575 | block_item_list block_item
576 ;
577
578 block_item: declaration
579 | statement { stmtfree(); }
580 ;
581
582 /*
583 * Here starts the old YACC code.
584 */
585
586 /*
587 * Variables are declared in init_declarator.
588 */
589 declaration: declaration_specifiers ';' { p1tfree($1); fun_inline = 0; }
590 | declaration_specifiers init_declarator_list ';' {
591 p1tfree($1);
592 fun_inline = 0;
593 }
594 | C_STATICASSERT '(' e ',' string ')' ';' {
595 int r = con_e($3);
596 if (r == 0) /* false */
597 uerror($5);
598 }
599 ;
600
601 /*
602 * Normal declaration of variables. curtype contains the current type node.
603 * Returns nothing, variables are declared in init_declarator.
604 */
605 init_declarator_list:
606 init_declarator { symclear(blevel); }
607 | init_declarator_list ',' attr_var { $<nodep>$ = $<nodep>0; } init_declarator {
608 uawarn($3, "init_declarator");
609 symclear(blevel);
610 }
611 ;
612
613 enum_dcl: enum_head '{' moe_list optcomma '}' { $$ = enumdcl($1); }
614 | C_ENUM C_NAME { $$ = enumref($2); }
615 ;
616
617 enum_head: C_ENUM { $$ = enumhd(NULL); }
618 | C_ENUM C_NAME { $$ = enumhd($2); }
619 ;
620
621 moe_list: moe
622 | moe_list ',' moe
623 ;
624
625 moe: C_NAME { moedef($1); }
626 | C_TYPENAME { moedef($1); }
627 | C_NAME '=' e { enummer = con_e($3); moedef($1); }
628 | C_TYPENAME '=' e { enummer = con_e($3); moedef($1); }
629 ;
630
631 struct_dcl: str_head '{' struct_dcl_list '}' {
632 P1ND *p;
633
634 $$ = dclstruct($1);
635 if (pragma_allpacked) {
636 p = bdty(CALL, bdty(NAME, "packed"),
637 bcon(pragma_allpacked));
638 $$->n_ap = attr_add($$->n_ap,gcc_attr_wrapper(p)); }
639 }
640 | C_STRUCT attr_var C_NAME {
641 $$ = rstruct($3,$1);
642 uawarn($2, "struct_dcl");
643 }
644 /*COMPAT_GCC*/ | str_head '{' '}' { $$ = dclstruct($1); }
645 ;
646
647 attr_var: {
648 P1ND *q, *p;
649
650 p = pragma_aligned ? bdty(CALL, bdty(NAME, "aligned"),
651 bcon(pragma_aligned)) : NULL;
652 if (pragma_packed) {
653 q = bdty(NAME, "packed");
654 p = (p == NULL ? q : cmop(p, q));
655 }
656 pragma_aligned = pragma_packed = 0;
657 $$ = p;
658 }
659 /*COMPAT_GCC*/ | attr_spec_list
660 ;
661
662 attr_spec_list: attribute_specifier
663 | attr_spec_list attribute_specifier { $$ = cmop($1, $2); }
664 ;
665
666 str_head: C_STRUCT attr_var { $$ = bstruct(NULL, $1, $2); }
667 | C_STRUCT attr_var C_NAME { $$ = bstruct($3, $1, $2); }
668 ;
669
670 struct_dcl_list: struct_declaration
671 | struct_dcl_list struct_declaration
672 ;
673
674 struct_declaration:
675 specifier_qualifier_list struct_declarator_list optsemi {
676 p1tfree($1);
677 }
678 ;
679
680 optsemi: ';' { }
681 | optsemi ';' { werror("extra ; in struct"); }
682 ;
683
684 specifier_qualifier_list:
685 merge_specifiers { $$ = typenode($1); }
686 ;
687
688 merge_specifiers: type_sq merge_specifiers { $$ = cmop($2, $1); }
689 | type_sq { $$ = $1; }
690 ;
691
692 struct_declarator_list:
693 struct_declarator { symclear(blevel); }
694 | struct_declarator_list ',' { $<nodep>$=$<nodep>0; }
695 struct_declarator { symclear(blevel); }
696 ;
697
698 struct_declarator: declarator attr_var {
699 P1ND *p;
700
701 $1 = aryfix($1);
702 p = tymerge($<nodep>0, tymfix($1));
703 if ($2)
704 p->n_ap = attr_add(p->n_ap, gcc_attr_wrapper($2));
705 soumemb(p, (char *)$1->n_sp, 0);
706 p1tfree(p);
707 }
708 | ':' e {
709 int ie = con_e($2);
710 if (fldchk(ie))
711 ie = 1;
712 falloc(NULL, ie, $<nodep>0);
713 }
714 | declarator ':' e {
715 int ie = con_e($3);
716 if (fldchk(ie))
717 ie = 1;
718 if ($1->n_op == NAME) {
719 /* XXX - tymfix() may alter $1 */
720 tymerge($<nodep>0, tymfix($1));
721 soumemb($1, (char *)$1->n_sp, FIELD | ie);
722 p1nfree($1);
723 } else
724 uerror("illegal declarator");
725 }
726 | declarator ':' e attr_spec_list {
727 int ie = con_e($3);
728 if (fldchk(ie))
729 ie = 1;
730 if ($1->n_op == NAME) {
731 /* XXX - tymfix() may alter $1 */
732 tymerge($<nodep>0, tymfix($1));
733 if ($4)
734 $1->n_ap = attr_add($1->n_ap,
735 gcc_attr_wrapper($4));
736 soumemb($1, (char *)$1->n_sp, FIELD | ie);
737 p1nfree($1);
738 } else
739 uerror("illegal declarator");
740 }
741 | /* unnamed member */ {
742 P1ND *p = $<nodep>0;
743 char *c = permalloc(10);
744
745 if (p->n_type != STRTY && p->n_type != UNIONTY)
746 uerror("bad unnamed member type");
747 snprintf(c, 10, "*%dFAKE", getlab());
748 soumemb(p, c, 0);
749 }
750 ;
751
752 /* always preceeded by attributes */
753 xnfdeclarator: declarator attr_var {
754 $$ = xnf = init_declarator($<nodep>0, $1, 1, $2, 0);
755 }
756 | declarator C_ASM '(' svstr ')' {
757 $$ = xnf = init_declarator($<nodep>0, $1, 1, NULL, $4);
758 }
759 ;
760
761 /*
762 * Handles declarations and assignments.
763 * Returns nothing.
764 */
765 init_declarator: declarator attr_var {
766 init_declarator($<nodep>0, $1, 0, $2, 0);
767 }
768 | declarator C_ASM '(' svstr ')' attr_var {
769 init_declarator($<nodep>0, $1, 0, $6, $4);
770 }
771 | xnfdeclarator '=' e {
772 if ($1->sclass == STATIC || $1->sclass == EXTDEF)
773 statinit++;
774 simpleinit($1, eve($3));
775 if ($1->sclass == STATIC || $1->sclass == EXTDEF)
776 statinit--;
777 xnf = NULL;
778 }
779 | xnfdeclarator '=' begbr init_list optcomma '}' {
780 endinit(0);
781 xnf = NULL;
782 }
783 /*COMPAT_GCC*/ | xnfdeclarator '=' begbr '}' { endinit(0); xnf = NULL; }
784 ;
785
786 begbr: '{' { beginit($<symp>-1); }
787 ;
788
789 initializer: e %prec ',' { $$ = eve($1); }
790 | ibrace init_list optcomma '}' { $$ = NULL; }
791 | ibrace '}' { asginit(bcon(0)); $$ = NULL; }
792 ;
793
794 init_list: designation initializer { dainit($1, $2); }
795 | init_list ',' designation initializer { dainit($3, $4); }
796 ;
797
798 designation: designator_list '=' { desinit($1); $$ = NULL; }
799 | GCC_DESIG { desinit(bdty(NAME, $1)); $$ = NULL; }
800 | '[' e C_ELLIPSIS e ']' '=' { $$ = biop(CM, $2, $4); }
801 | { $$ = NULL; }
802 ;
803
804 designator_list: designator { $$ = $1; }
805 | designator_list designator { $$ = $2; $$->n_left = $1; }
806 ;
807
808 designator: '[' e ']' {
809 int ie = con_e($2);
810 if (ie < 0) {
811 uerror("designator must be non-negative");
812 ie = 0;
813 }
814 $$ = biop(LB, NULL, bcon(ie));
815 }
816 | C_STROP C_TYPENAME {
817 if ($1 != DOT)
818 uerror("invalid designator");
819 $$ = bdty(NAME, $2);
820 }
821 | C_STROP C_NAME {
822 if ($1 != DOT)
823 uerror("invalid designator");
824 $$ = bdty(NAME, $2);
825 }
826 ;
827
828 optcomma : /* VOID */
829 | ','
830 ;
831
832 ibrace: '{' { ilbrace(); }
833 ;
834
835 /* STATEMENTS */
836
837 compoundstmt: begin block_item_list '}' { flend(); }
838 | begin '}' { flend(); }
839 ;
840
841 begin: '{' {
842 struct savbc *bc = malloc(sizeof(struct savbc));
843 if (blevel == 1) {
844 #ifdef STABS
845 if (gflag)
846 stabs_line(lineno);
847 #endif
848 dclargs();
849 }
850 #ifdef STABS
851 if (gflag && blevel > 1)
852 stabs_lbrac(blevel+1);
853 #endif
854 ++blevel;
855 oldstyle = 0;
856 bc->contlab = autooff;
857 bc->next = savctx;
858 bc->bkptr = bkpole;
859 bc->bkoff = cbkp;
860 bc->stptr = sapole;
861 bc->stoff = cstp;
862 bc->numnode = usdnodes;
863 usdnodes = 0;
864 bkpole = sapole = NULL;
865 cbkp = cstp = 0;
866 savctx = bc;
867 if (!isinlining && sspflag && blevel == 2)
868 sspstart();
869 }
870 ;
871
872 statement: e ';' { ecomp(eve($1)); symclear(blevel); }
873 | compoundstmt
874 | ifprefix statement { plabel($1); reached = 1; }
875 | ifelprefix statement {
876 if ($1 != NOLAB) {
877 plabel( $1);
878 reached = 1;
879 }
880 }
881 | whprefix statement {
882 branch(contlab);
883 plabel( brklab );
884 if( (flostat&FBRK) || !(flostat&FLOOP))
885 reached = 1;
886 else
887 reached = 0;
888 resetbc(0);
889 }
890 | doprefix statement C_WHILE '(' e ')' ';' {
891 plabel(contlab);
892 if (flostat & FCONT)
893 reached = 1;
894 if (reached)
895 cbranch(buildtree(NE, eve($5), bcon(0)),
896 bcon($1));
897 else
898 p1tfree(eve($5));
899 plabel( brklab);
900 reached = 1;
901 resetbc(0);
902 }
903 | forprefix .e ')' statement
904 { plabel( contlab );
905 if( flostat&FCONT ) reached = 1;
906 if( $2 ) ecomp( $2 );
907 branch($1);
908 plabel( brklab );
909 if( (flostat&FBRK) || !(flostat&FLOOP) ) reached = 1;
910 else reached = 0;
911 resetbc(0);
912 blevel--;
913 symclear(blevel);
914 }
915 | switchpart statement
916 { if( reached ) branch( brklab );
917 plabel( $1 );
918 swend();
919 plabel( brklab);
920 if( (flostat&FBRK) || !(flostat&FDEF) ) reached = 1;
921 resetbc(FCONT);
922 }
923 | C_BREAK ';' {
924 if (brklab == NOLAB)
925 uerror("illegal break");
926 else if (reached)
927 branch(brklab);
928 flostat |= FBRK;
929 reached = 0;
930 }
931 | C_CONTINUE ';' {
932 if (contlab == NOLAB)
933 uerror("illegal continue");
934 else
935 branch(contlab);
936 flostat |= FCONT;
937 goto rch;
938 }
939 | C_RETURN ';' {
940 branch(retlab);
941 if (cftnsp->stype != VOID &&
942 (cftnsp->sflags & NORETYP) == 0 &&
943 cftnsp->stype != VOID+FTN)
944 uerror("return value required");
945 rch:
946 if (!reached)
947 warner(Wunreachable_code);
948 reached = 0;
949 }
950 | C_RETURN e ';' {
951 P1ND *p, *q;
952
953 p = nametree(cftnsp);
954 p->n_type = DECREF(p->n_type);
955 q = eve($2);
956 #ifdef TARGET_TIMODE
957 {
958 P1ND *r;
959 if ((r = gcc_eval_ticast(RETURN, p, q)) != NULL)
960 q = r;
961 }
962 #endif
963 #ifndef NO_COMPLEX
964 if (ANYCX(q) || ANYCX(p))
965 q = cxret(q, p);
966 else if (ISITY(p->n_type) || ISITY(q->n_type)) {
967 q = imret(q, p);
968 if (ISITY(p->n_type))
969 p->n_type -= (FIMAG-FLOAT);
970 if (ISITY(q->n_type))
971 q->n_type -= (FIMAG-FLOAT);
972 }
973 #endif
974 p = buildtree(RETURN, p, q);
975 if (p->n_type == VOID) {
976 ecomp(p->n_right);
977 } else {
978 if (cftnod == NULL) {
979 P1ND *r = tempnode(0, p->n_type,
980 p->n_df, p->n_ap);
981 cftnod = tmpalloc(sizeof(P1ND));
982 *cftnod = *r;
983 p1tfree(r);
984 }
985 ecomp(buildtree(ASSIGN,
986 p1tcopy(cftnod), p->n_right));
987 }
988 p1tfree(p->n_left);
989 p1nfree(p);
990 branch(retlab);
991 reached = 0;
992 }
993 | C_GOTO C_NAME ';' { gotolabel($2); goto rch; }
994 | C_GOTO '*' e ';' { ecomp(biop(GOTO, eve($3), NULL)); }
995 | asmstatement ';'
996 | ';'
997 | error ';'
998 | error '}'
999 | label statement
1000 ;
1001
1002 asmstatement: C_ASM mvol '(' svstr ')' { send_passt(IP_ASM, mkpstr($4)); }
1003 | C_ASM mvol '(' svstr xasm ')' { mkxasm($4, $5); }
1004 ;
1005
1006 svstr: string { $$ = addstring($1); }
1007 ;
1008
1009 mvol: /* empty */
1010 | C_QUALIFIER { }
1011 ;
1012
1013 xasm: ':' oplist { $$ = xcmop($2, NULL, NULL); }
1014 | ':' oplist ':' oplist { $$ = xcmop($2, $4, NULL); }
1015 | ':' oplist ':' oplist ':' cnstr { $$ = xcmop($2, $4, $6); }
1016 ;
1017
1018 oplist: /* nothing */ { $$ = NULL; }
1019 | oper { $$ = $1; }
1020 ;
1021
1022 oper: svstr '(' e ')' { $$ = xasmop($1, pconvert(eve($3))); }
1023 | oper ',' svstr '(' e ')' {
1024 $$ = cmop($1, xasmop($3, pconvert(eve($5))));
1025 }
1026 ;
1027
1028 cnstr: svstr { $$ = xasmop($1, bcon(0)); }
1029 | cnstr ',' svstr { $$ = cmop($1, xasmop($3, bcon(0))); }
1030 ;
1031
1032 label: C_NAME ':' attr_var { deflabel($1, $3); reached = 1; }
1033 | C_TYPENAME ':' attr_var { deflabel($1, $3); reached = 1; }
1034 | C_CASE e ':' { addcase(eve($2)); reached = 1; }
1035 /* COMPAT_GCC */| C_CASE e C_ELLIPSIS e ':' {
1036 #ifdef GCC_COMPAT
1037 gcccase(eve($2), eve($4)); reached = 1;
1038 #endif
1039 }
1040 | C_DEFAULT ':' { reached = 1; adddef(); flostat |= FDEF; }
1041 ;
1042
1043 doprefix: C_DO {
1044 savebc();
1045 brklab = getlab();
1046 contlab = getlab();
1047 plabel( $$ = getlab());
1048 reached = 1;
1049 }
1050 ;
1051 ifprefix: C_IF '(' e ')' {
1052 xcbranch(eve($3), $$ = getlab());
1053 reached = 1;
1054 }
1055 ;
1056 ifelprefix: ifprefix statement C_ELSE {
1057 if (reached)
1058 branch($$ = getlab());
1059 else
1060 $$ = NOLAB;
1061 plabel( $1);
1062 reached = 1;
1063 }
1064 ;
1065
1066 whprefix: C_WHILE '(' e ')' {
1067 savebc();
1068 $3 = eve($3);
1069 if ($3->n_op == ICON && glval($3) != 0)
1070 flostat = FLOOP | (flostat & FP_CONTR_CBR);
1071 plabel( contlab = getlab());
1072 reached = 1;
1073 brklab = getlab();
1074 if (flostat & FLOOP)
1075 p1tfree($3);
1076 else
1077 xcbranch($3, brklab);
1078 }
1079 ;
1080 forprefix: C_FOR '(' .e ';' .e ';' {
1081 ++blevel;
1082 if ($3)
1083 ecomp($3);
1084 savebc();
1085 contlab = getlab();
1086 brklab = getlab();
1087 plabel( $$ = getlab());
1088 reached = 1;
1089 if ($5)
1090 xcbranch($5, brklab);
1091 else
1092 flostat |= FLOOP;
1093 }
1094 | C_FOR '(' { ++blevel; } declaration .e ';' {
1095 savebc();
1096 contlab = getlab();
1097 brklab = getlab();
1098 plabel( $$ = getlab());
1099 reached = 1;
1100 if ($5)
1101 xcbranch($5, brklab);
1102 else
1103 flostat |= FLOOP;
1104 }
1105 ;
1106
1107 switchpart: C_SWITCH '(' e ')' {
1108 P1ND *p;
1109 int num;
1110 TWORD t;
1111
1112 savebc();
1113 brklab = getlab();
1114 $3 = eve($3);
1115 if (!ISINTEGER($3->n_type)) {
1116 uerror("switch expression must have integer "
1117 "type");
1118 t = INT;
1119 } else {
1120 $3 = intprom($3);
1121 t = $3->n_type;
1122 }
1123 p = tempnode(0, t, 0, 0);
1124 num = regno(p);
1125 ecomp(buildtree(ASSIGN, p, $3));
1126 branch( $$ = getlab());
1127 swstart(num, t);
1128 reached = 0;
1129 }
1130 ;
1131 /* EXPRESSIONS */
1132 .e: e { $$ = eve($1); }
1133 | { $$=0; }
1134 ;
1135
1136 elist: { $$ = NULL; }
1137 | e2 { $$ = $1; }
1138 ;
1139
1140 e2: e %prec ','
1141 | e2 ',' e { $$ = biop(CM, $1, $3); }
1142 | e2 ',' cast_type { /* hack for stdarg */
1143 TYMFIX($3);
1144 $3->n_op = TYPE;
1145 $$ = biop(CM, $1, $3);
1146 }
1147 | cast_type { TYMFIX($1); $1->n_op = TYPE; $$ = $1; }
1148 ;
1149
1150 /*
1151 * Precedence order of operators.
1152 */
1153 e: e ',' e { $$ = biop(COMOP, $1, $3); }
1154 | e '=' e { $$ = biop(ASSIGN, $1, $3); }
1155 | e C_ASOP e { $$ = biop($2, $1, $3); }
1156 | e '?' e ':' e { $$=biop(QUEST, $1, biop(COLON, $3, $5)); }
1157 /* COMPAT_GCC */| e '?' ':' e { $$ = biop(BIQUEST, $1, $4); }
1158 | e C_OROR e { $$ = biop($2, $1, $3); }
1159 | e C_ANDAND e { $$ = biop($2, $1, $3); }
1160 | e '|' e { $$ = biop(OR, $1, $3); }
1161 | e '^' e { $$ = biop(ER, $1, $3); }
1162 | e '&' e { $$ = biop(AND, $1, $3); }
1163 | e C_EQUOP e { $$ = biop($2, $1, $3); }
1164 | e C_RELOP e { $$ = biop($2, $1, $3); }
1165 | e C_SHIFTOP e { $$ = biop($2, $1, $3); }
1166 | e '+' e { $$ = biop(PLUS, $1, $3); }
1167 | e '-' e { $$ = biop(MINUS, $1, $3); }
1168 | e C_DIVOP e { $$ = biop($2, $1, $3); }
1169 | e '*' e { $$ = biop(MUL, $1, $3); }
1170 | term
1171 ;
1172
1173 xbegin: begin {
1174 $$ = getlab(); getlab(); getlab();
1175 branch($$); plabel(($$)+2);
1176 }
1177 ;
1178
1179 term: term C_INCOP { $$ = biop($2, $1, bcon(1)); }
1180 | '*' term { $$ = biop(UMUL, $2, NULL); }
1181 | '&' term { $$ = biop(ADDROF, $2, NULL); }
1182 | '-' term { $$ = biop(UMINUS, $2, NULL ); }
1183 | '+' term { $$ = biop(UPLUS, $2, NULL ); }
1184 | C_UNOP term { $$ = biop($1, $2, NULL); }
1185 | C_INCOP term {
1186 $$ = biop($1 == INCR ? PLUSEQ : MINUSEQ, $2, bcon(1));
1187 }
1188 | C_SIZEOF xa term { $$ = biop(SZOF, $3, bcon(0)); inattr = $<intval>2; }
1189 | '(' cast_type ')' term %prec C_INCOP {
1190 TYMFIX($2);
1191 $$ = biop(CAST, $2, $4);
1192 }
1193 | C_SIZEOF xa '(' cast_type ')' %prec C_SIZEOF {
1194 $$ = biop(SZOF, $4, bcon(1));
1195 inattr = $<intval>2;
1196 }
1197 | C_ALIGNOF xa '(' cast_type ')' {
1198 int al;
1199 TYMFIX($4);
1200 al = talign($4->n_type, $4->n_ap);
1201 $$ = bcon(al/SZCHAR);
1202 inattr = $<intval>2;
1203 p1tfree($4);
1204 }
1205 | '(' cast_type ')' clbrace init_list optcomma '}' {
1206 endinit(0);
1207 $$ = bdty(NAME, $4);
1208 $$->n_op = CLOP;
1209 }
1210 | '(' cast_type ')' clbrace '}' {
1211 endinit(0);
1212 $$ = bdty(NAME, $4);
1213 $$->n_op = CLOP;
1214 }
1215 | term '[' e ']' { $$ = biop(LB, $1, $3); }
1216 | C_NAME '(' elist ')' {
1217 $$ = biop($3 ? CALL : UCALL, bdty(NAME, $1), $3);
1218 }
1219 | term '(' elist ')' { $$ = biop($3 ? CALL : UCALL, $1, $3); }
1220 | term C_STROP C_NAME { $$ = biop($2, $1, bdty(NAME, $3)); }
1221 | term C_STROP C_TYPENAME { $$ = biop($2, $1, bdty(NAME, $3));}
1222 | C_NAME %prec C_SIZEOF /* below ( */{ $$ = bdty(NAME, $1); }
1223 | PCC_OFFSETOF '(' cast_type ',' term ')' {
1224 TYMFIX($3);
1225 $3->n_type = INCREF($3->n_type);
1226 $3 = biop(CAST, $3, bcon(0));
1227 if ($5->n_op == NAME) {
1228 $$ = biop(STREF, $3, $5);
1229 } else {
1230 P1ND *p = $5;
1231 while (p->n_left->n_op != NAME)
1232 p = p->n_left;
1233 p->n_left = biop(STREF, $3, p->n_left);
1234 $$ = $5;
1235 }
1236 $$ = biop(ADDROF, $$, NULL);
1237 $3 = block(NAME, NULL, NULL, ENUNSIGN(INTPTR), 0, 0);
1238 $$ = biop(CAST, $3, $$);
1239 }
1240 | C_ICON { $$ = bdty(ICON, &($1)); }
1241 | C_FCON { $$ = bdty(FCON, &($1)); }
1242 | svstr { $$ = bdty(STRING, $1, styp()); }
1243 | '(' e ')' { $$=$2; }
1244 | '(' xbegin e ';' '}' ')' { $$ = gccexpr($2, eve($3)); }
1245 | '(' xbegin block_item_list e ';' '}' ')' {
1246 $$ = gccexpr($2, eve($4));
1247 }
1248 | '(' xbegin block_item_list '}' ')' {
1249 $$ = gccexpr($2, voidcon());
1250 }
1251 | C_ANDAND C_NAME {
1252 struct symtab *s = lookup($2, SLBLNAME|STEMP);
1253 if (s->soffset == 0) {
1254 s->soffset = -getlab();
1255 s->sclass = STATIC;
1256 }
1257 savlab(s->soffset);
1258 $$ = biop(ADDROF, bdty(GOTO, $2), NULL);
1259 }
1260 | C_GENERIC '(' e ',' gen_ass_list ')' { $$ = dogen($5, $3); }
1261 ;
1262
1263 gen_ass_list: gen_assoc { $$ = $1; }
1264 | gen_ass_list ',' gen_assoc { $$ = addgen($1, $3); }
1265 ;
1266
1267 gen_assoc: cast_type ':' e { TYMFIX($1); $$ = newgen($1, $3); }
1268 | C_DEFAULT ':' e { $$ = newgen(0, $3); }
1269 ;
1270
1271 xa: { $<intval>$ = inattr; inattr = 0; }
1272 ;
1273
1274 clbrace: '{' { P1ND *q = $<nodep>-1; TYMFIX(q); $$ = clbrace(q); }
1275 ;
1276
1277 string: C_STRING { $$ = stradd(NULL, $1); }
1278 | string C_STRING { $$ = stradd($1, $2); }
1279 ;
1280
1281 cast_type: specifier_qualifier_list {
1282 $$ = biop(TYMERGE, $1, bdty(NAME, NULL));
1283 }
1284 | specifier_qualifier_list abstract_declarator {
1285 $$ = biop(TYMERGE, $1, aryfix($2));
1286 }
1287 ;
1288
1289 %%
1290
1291 P1ND *
1292 mkty(TWORD t, union dimfun *d, struct attr *sue)
1293 {
1294 return block(TYPE, NULL, NULL, t, d, sue);
1295 }
1296
1297 P1ND *
1298 bdty(int op, ...)
1299 {
1300 struct lexint *li;
1301 FLT *f2;
1302 CONSZ c;
1303 va_list ap;
1304 int val;
1305 register P1ND *q;
1306
1307 va_start(ap, op);
1308 q = biop(op, NULL, NULL);
1309
1310 switch (op) {
1311 case UMUL:
1312 case UCALL:
1313 q->n_left = va_arg(ap, P1ND *);
1314 q->n_rval = 0;
1315 break;
1316
1317 case FCON:
1318 f2 = va_arg(ap, FLT *);
1319 q->n_scon = sfallo();
1320 *q->n_scon = f2->sf;
1321 q->n_type = f2->t;
1322 break;
1323
1324 case ICON:
1325 li = va_arg(ap, struct lexint *);
1326 slval(q, li->val);
1327 q->n_type = li->t;
1328 break;
1329
1330 case CALL:
1331 q->n_left = va_arg(ap, P1ND *);
1332 q->n_right = va_arg(ap, P1ND *);
1333 break;
1334
1335 case LB:
1336 q->n_left = va_arg(ap, P1ND *);
1337 if ((val = va_arg(ap, int)) <= 0) {
1338 uerror("array size must be positive");
1339 val = 1;
1340 }
1341 q->n_right = bcon(val);
1342 break;
1343
1344 case GOTO: /* for named labels */
1345 q->n_ap = attr_add(q->n_ap, attr_new(ATTR_P1LABELS, 1));
1346 /* FALLTHROUGH */
1347 case NAME:
1348 q->n_op = NAME;
1349 q->n_sp = va_arg(ap, struct symtab *); /* XXX survive tymerge */
1350 break;
1351
1352 case STRING:
1353 q->n_type = PTR|CHAR;
1354 q->n_name = va_arg(ap, char *);
1355 c = va_arg(ap, TWORD);
1356 slval(q, c);
1357 break;
1358
1359 default:
1360 cerror("bad bdty");
1361 }
1362 va_end(ap);
1363
1364 return q;
1365 }
1366
1367 static void
1368 flend(void)
1369 {
1370 struct savbc *sc;
1371
1372 if (!isinlining && sspflag && blevel == 2)
1373 sspend();
1374 #ifdef STABS
1375 if (gflag && blevel > 2)
1376 stabs_rbrac(blevel);
1377 #endif
1378 --blevel;
1379 if( blevel == 1 )
1380 blevel = 0;
1381 symclear(blevel); /* Clean ut the symbol table */
1382 if (autooff > maxautooff)
1383 maxautooff = autooff;
1384 autooff = savctx->contlab;
1385 blkfree();
1386 stmtfree();
1387 bkpole = savctx->bkptr;
1388 cbkp = savctx->bkoff;
1389 sapole = savctx->stptr;
1390 cstp = savctx->stoff;
1391 usdnodes = savctx->numnode;
1392 sc = savctx->next;
1393 free(savctx);
1394 savctx = sc;
1395 }
1396
1397 /*
1398 * XXX workaround routines for block level cleansing in gcc compat mode.
1399 * Temporary should be re reserved for this value before.
1400 */
1401 static P1ND *
1402 p1mcopy(P1ND *p)
1403 {
1404 P1ND *q;
1405
1406 q = xmalloc(sizeof(P1ND));
1407 *q = *p;
1408
1409 switch (coptype(q->n_op)) {
1410 case BITYPE:
1411 q->n_right = p1mcopy(p->n_right);
1412 /* FALLTHROUGH */
1413 case UTYPE:
1414 q->n_left = p1mcopy(p->n_left);
1415 }
1416
1417 return(q);
1418 }
1419
1420 static void
1421 p1mfree(P1ND *p)
1422 {
1423 int o = coptype(p->n_op);
1424 if (o == BITYPE)
1425 p1mfree(p->n_right);
1426 if (o != LTYPE)
1427 p1mfree(p->n_left);
1428 free(p);
1429 }
1430
1431
1432 static P1ND *
1433 gccexpr(int bn, P1ND *q)
1434 {
1435 P1ND *r, *p, *s;
1436
1437 branch(bn+4);
1438 plabel(bn);
1439 r = buildtree(COMOP, biop(GOTO, bcon(bn+2), NULL), q);
1440 /* XXX hack to survive flend() */
1441 s = p1mcopy(r);
1442 p1tfree(r);
1443 flend();
1444 r = p1tcopy(s);
1445 p1mfree(s);
1446 q = r->n_right;
1447 /* XXX end hack */
1448 if (!(q->n_op == ICON && q->n_type == STRTY) && (r->n_type != VOID)) {
1449 p = tempnode(0, q->n_type, q->n_df, q->n_ap);
1450 r = buildtree(ASSIGN, p1tcopy(p), r);
1451 r = buildtree(COMOP, r, p);
1452 }
1453 return r;
1454 }
1455
1456 static void
1457 savebc(void)
1458 {
1459 struct savbc *bc = malloc(sizeof(struct savbc));
1460
1461 bc->brklab = brklab;
1462 bc->contlab = contlab;
1463 bc->flostat = flostat;
1464 bc->next = savbc;
1465 savbc = bc;
1466 flostat &= FP_CONTR_CBR;
1467 }
1468
1469 static void
1470 resetbc(int mask)
1471 {
1472 struct savbc *bc;
1473
1474 flostat = savbc->flostat | (flostat&mask);
1475 contlab = savbc->contlab;
1476 brklab = savbc->brklab;
1477 bc = savbc->next;
1478 free(savbc);
1479 savbc = bc;
1480 }
1481
1482 struct swdef {
1483 struct swdef *next; /* Next in list */
1484 int deflbl; /* Label for "default" */
1485 struct swents *ents; /* Linked sorted list of case entries */
1486 int nents; /* # of entries in list */
1487 int num; /* Node value will end up in */
1488 TWORD type; /* Type of switch expression */
1489 } *swpole;
1490
1491 /*
1492 * add case to switch
1493 */
1494 static void
1495 addcase(P1ND *p)
1496 {
1497 struct swents **put, *w, *sw = malloc(sizeof(struct swents));
1498 CONSZ val;
1499
1500 p = optloop(p); /* change enum to ints */
1501 if (p->n_op != ICON || p->n_sp != NULL) {
1502 uerror( "non-constant case expression");
1503 return;
1504 }
1505 if (swpole == NULL) {
1506 uerror("case not in switch");
1507 return;
1508 }
1509
1510 if (DEUNSIGN(swpole->type) != DEUNSIGN(p->n_type)) {
1511 val = glval(p);
1512 p = makety(p, swpole->type, 0, 0, 0);
1513 if (p->n_op != ICON)
1514 cerror("could not cast case value to type of switch "
1515 "expression");
1516 if (glval(p) != val)
1517 werror("case expression truncated");
1518 }
1519 sw->sval = glval(p);
1520 p1tfree(p);
1521 put = &swpole->ents;
1522 if (ISUNSIGNED(swpole->type)) {
1523 for (w = swpole->ents;
1524 w != NULL && (U_CONSZ)w->sval < (U_CONSZ)sw->sval;
1525 w = w->next)
1526 put = &w->next;
1527 } else {
1528 for (w = swpole->ents; w != NULL && w->sval < sw->sval;
1529 w = w->next)
1530 put = &w->next;
1531 }
1532 if (w != NULL && w->sval == sw->sval) {
1533 uerror("duplicate case in switch");
1534 return;
1535 }
1536 plabel(sw->slab = getlab());
1537 *put = sw;
1538 sw->next = w;
1539 swpole->nents++;
1540 }
1541
1542 #ifdef GCC_COMPAT
1543 void
1544 gcccase(P1ND *ln, P1ND *hn)
1545 {
1546 CONSZ i, l, h;
1547
1548 l = icons(optim(ln));
1549 h = icons(optim(hn));
1550
1551 if (h < l)
1552 i = l, l = h, h = i;
1553
1554 for (i = l; i <= h; i++)
1555 addcase(xbcon(i, NULL, hn->n_type));
1556 }
1557 #endif
1558
1559 /*
1560 * add default case to switch
1561 */
1562 static void
1563 adddef(void)
1564 {
1565 if (swpole == NULL)
1566 uerror("default not inside switch");
1567 else if (swpole->deflbl != 0)
1568 uerror("duplicate default in switch");
1569 else
1570 plabel( swpole->deflbl = getlab());
1571 }
1572
1573 static void
1574 swstart(int num, TWORD type)
1575 {
1576 struct swdef *sw = malloc(sizeof(struct swdef));
1577
1578 sw->deflbl = sw->nents = 0;
1579 sw->ents = NULL;
1580 sw->next = swpole;
1581 sw->num = num;
1582 sw->type = type;
1583 swpole = sw;
1584 }
1585
1586 /*
1587 * end a switch block
1588 */
1589 static void
1590 swend(void)
1591 {
1592 struct swents *sw, **swp;
1593 struct swdef *sp;
1594 int i;
1595
1596 sw = FUNALLO(sizeof(struct swents));
1597 swp = FUNALLO(sizeof(struct swents *) * (swpole->nents+1));
1598
1599 sw->slab = swpole->deflbl;
1600 swp[0] = sw;
1601
1602 for (i = 1; i <= swpole->nents; i++) {
1603 swp[i] = swpole->ents;
1604 swpole->ents = swpole->ents->next;
1605 }
1606 genswitch(swpole->num, swpole->type, swp, swpole->nents);
1607
1608 FUNFREE(sw);
1609 FUNFREE(swp);
1610 while (swpole->ents) {
1611 sw = swpole->ents;
1612 swpole->ents = sw->next;
1613 free(sw);
1614 }
1615 sp = swpole->next;
1616 free(swpole);
1617 swpole = sp;
1618 }
1619
1620 /*
1621 * num: tempnode the value of the switch expression is in
1622 * type: type of the switch expression
1623 *
1624 * p points to an array of structures, each consisting
1625 * of a constant value and a label.
1626 * The first is >=0 if there is a default label;
1627 * its value is the label number
1628 * The entries p[1] to p[n] are the nontrivial cases
1629 * n is the number of case statements (length of list)
1630 */
1631 static void
1632 genswitch(int num, TWORD type, struct swents **p, int n)
1633 {
1634 P1ND *r, *q;
1635 int i;
1636
1637 if (mygenswitch(num, type, p, n))
1638 return;
1639
1640 /* simple switch code */
1641 for (i = 1; i <= n; ++i) {
1642 /* already in 1 */
1643 r = tempnode(num, type, 0, 0);
1644 q = xbcon(p[i]->sval, NULL, type);
1645 r = buildtree(NE, r, clocal(q));
1646 xcbranch(r, p[i]->slab);
1647 }
1648 if (p[0]->slab > 0)
1649 branch(p[0]->slab);
1650 }
1651
1652 /*
1653 * Declare a variable or prototype.
1654 */
1655 static struct symtab *
1656 init_declarator(P1ND *tn, P1ND *p, int assign, P1ND *a, char *as)
1657 {
1658 int class = (int)glval(tn);
1659 struct symtab *sp;
1660
1661 p = aryfix(p);
1662 p = tymerge(tn, p);
1663 if (a) {
1664 struct attr *ap = gcc_attr_wrapper(a);
1665 p->n_ap = attr_add(p->n_ap, ap);
1666 }
1667
1668 p->n_sp = sp = lookup((char *)p->n_sp, 0); /* XXX */
1669
1670 if (fun_inline && ISFTN(p->n_type))
1671 sp->sflags |= SINLINE;
1672
1673 if (!ISFTN(p->n_type)) {
1674 if (assign) {
1675 defid2(p, class, as);
1676 sp = p->n_sp;
1677 sp->sflags |= SASG;
1678 if (sp->sflags & SDYNARRAY)
1679 uerror("can't initialize dynamic arrays");
1680 lcommdel(sp);
1681 } else
1682 nidcl2(p, class, as);
1683 } else {
1684 extern P1ND *parlink;
1685 if (assign)
1686 uerror("cannot initialise function");
1687 defid2(p, uclass(class), as);
1688 sp = p->n_sp;
1689 if (sp->sdf->dfun == 0 && !issyshdr)
1690 warner(Wstrict_prototypes);
1691 if (parlink) {
1692 /* dynamic sized arrays in prototypes */
1693 p1tfree(parlink); /* Free delayed tree */
1694 parlink = NULL;
1695 }
1696 }
1697 p1tfree(p);
1698 if (issyshdr)
1699 sp->sflags |= SINSYS; /* declared in system header */
1700 return sp;
1701 }
1702
1703 /*
1704 * Declare old-stype function arguments.
1705 */
1706 static void
1707 oldargs(P1ND *p)
1708 {
1709 blevel++;
1710 p->n_op = TYPE;
1711 p->n_type = FARG;
1712 p->n_sp = lookup((char *)p->n_sp, 0);/* XXX */
1713 defid(p, PARAM);
1714 blevel--;
1715 }
1716
1717 /*
1718 * Set NAME nodes to a null name and index of LB nodes to NOOFFSET
1719 * unless clr is one, in that case preserve variable name.
1720 */
1721 static P1ND *
1722 namekill(P1ND *p, int clr)
1723 {
1724 P1ND *q;
1725 int o = p->n_op;
1726
1727 switch (coptype(o)) {
1728 case LTYPE:
1729 if (o == NAME) {
1730 if (clr)
1731 p->n_sp = NULL;
1732 else
1733 p->n_sp = lookup((char *)p->n_sp, 0);/* XXX */
1734 }
1735 break;
1736
1737 case UTYPE:
1738 p->n_left = namekill(p->n_left, clr);
1739 break;
1740
1741 case BITYPE:
1742 p->n_left = namekill(p->n_left, clr);
1743 if (o == LB) {
1744 if (clr) {
1745 p1tfree(p->n_right);
1746 p->n_right = bcon(NOOFFSET);
1747 } else
1748 p->n_right = eve(p->n_right);
1749 } else if (o == CALL)
1750 p->n_right = namekill(p->n_right, 1);
1751 else
1752 p->n_right = namekill(p->n_right, clr);
1753 if (o == TYMERGE) {
1754 q = tymerge(p->n_left, p->n_right);
1755 q->n_ap = attr_add(q->n_ap, p->n_ap);
1756 p1tfree(p->n_left);
1757 p1nfree(p);
1758 p = q;
1759 }
1760 break;
1761 }
1762 return p;
1763 }
1764
1765 /*
1766 * Declare function arguments.
1767 */
1768 static P1ND *
1769 funargs(P1ND *p)
1770 {
1771 extern P1ND *arrstk[10];
1772
1773 if (p->n_op == ELLIPSIS)
1774 return p;
1775
1776 p = namekill(p, 0);
1777 if (ISFTN(p->n_type))
1778 p->n_type = INCREF(p->n_type);
1779 if (ISARY(p->n_type)) {
1780 p->n_type += (PTR-ARY);
1781 if (p->n_df->ddim == -1)
1782 p1tfree(arrstk[0]), arrstk[0] = NULL;
1783 p->n_df++;
1784 }
1785 if (p->n_type == VOID && p->n_sp->sname == NULL)
1786 return p; /* sanitycheck later */
1787 else if (p->n_sp->sname == NULL)
1788 uerror("argument missing");
1789 else
1790 defid(p, PARAM);
1791 return p;
1792 }
1793
1794 static P1ND *
1795 listfw(P1ND *p, P1ND * (*f)(P1ND *))
1796 {
1797 if (p->n_op == CM) {
1798 p->n_left = listfw(p->n_left, f);
1799 p->n_right = (*f)(p->n_right);
1800 } else
1801 p = (*f)(p);
1802 return p;
1803 }
1804
1805
1806 /*
1807 * Declare a function.
1808 */
1809 static void
1810 fundef(P1ND *tp, P1ND *p)
1811 {
1812 extern int prolab;
1813 struct symtab *s;
1814 P1ND *q, *typ;
1815 int class = (int)glval(tp), oclass, ctval;
1816
1817 /*
1818 * We discard all names except for those needed for
1819 * parameter declaration. While doing that, also change
1820 * non-constant array sizes to unknown.
1821 */
1822 ctval = tvaloff;
1823 for (q = p; coptype(q->n_op) != LTYPE &&
1824 q->n_left->n_op != NAME; q = q->n_left) {
1825 if (q->n_op == CALL)
1826 q->n_right = namekill(q->n_right, 1);
1827 }
1828 if (q->n_op != CALL && q->n_op != UCALL) {
1829 uerror("invalid function definition");
1830 p = bdty(UCALL, p);
1831 } else if (q->n_op == CALL) {
1832 blevel = 1;
1833 argoff = ARGINIT;
1834 if (oldstyle == 0)
1835 q->n_right = listfw(q->n_right, funargs);
1836 p1listf(q->n_right, argsave);
1837 blevel = 0;
1838 }
1839
1840 p = typ = tymerge(tp, p);
1841 #ifdef GCC_COMPAT
1842 /* gcc seems to discard __builtin_ when declaring functions */
1843 if (strncmp("__builtin_", (char *)typ->n_sp, 10) == 0)
1844 typ->n_sp = (struct symtab *)((char *)typ->n_sp + 10);
1845 #endif
1846 s = typ->n_sp = lookup((char *)typ->n_sp, 0); /* XXX */
1847
1848 oclass = s->sclass;
1849 if (class == STATIC && oclass == EXTERN)
1850 werror("%s was first declared extern, then static", s->sname);
1851
1852 if (fun_inline) {
1853 /* special syntax for inline functions */
1854 if (! strcmp(s->sname,"main"))
1855 uerror("cannot inline main()");
1856
1857 s->sflags |= SINLINE;
1858 inline_start(s, class);
1859 if (class == EXTERN)
1860 class = EXTDEF;
1861 } else if (class == EXTERN)
1862 class = SNULL; /* same result */
1863
1864 cftnsp = s;
1865 defid(p, class);
1866 if (s->sdf->dfun == 0 && !issyshdr)
1867 warner(Wstrict_prototypes);
1868 #ifdef GCC_COMPAT
1869 if (attr_find(p->n_ap, GCC_ATYP_ALW_INL)) {
1870 /* Temporary turn on temps to make always_inline work */
1871 alwinl = 1;
1872 if (xtemps == 0) alwinl |= 2;
1873 xtemps = 1;
1874 }
1875 #endif
1876 prolab = getlab();
1877 send_passt(IP_PROLOG, -1, getexname(cftnsp), cftnsp->stype,
1878 cftnsp->sclass == EXTDEF, prolab, ctval);
1879 blevel++;
1880 #ifdef STABS
1881 if (gflag)
1882 stabs_func(s);
1883 #endif
1884 p1tfree(tp);
1885 p1tfree(p);
1886
1887 }
1888
1889 static void
1890 fend(void)
1891 {
1892 if (blevel)
1893 cerror("function level error");
1894 ftnend();
1895 fun_inline = 0;
1896 if (alwinl & 2) xtemps = 0;
1897 alwinl = 0;
1898 cftnsp = NULL;
1899 }
1900
1901 P1ND *
1902 structref(P1ND *p, int f, char *name)
1903 {
1904 P1ND *r;
1905
1906 if (f == DOT)
1907 p = buildtree(ADDROF, p, NULL);
1908 r = biop(NAME, NULL, NULL);
1909 r->n_name = name;
1910 r = buildtree(STREF, p, r);
1911 return r;
1912 }
1913
1914 static void
1915 olddecl(P1ND *p, P1ND *a)
1916 {
1917 struct symtab *s;
1918
1919 p = namekill(p, 0);
1920 s = p->n_sp;
1921 if (s->slevel != 1 || s->stype == UNDEF)
1922 uerror("parameter '%s' not defined", s->sname);
1923 else if (s->stype != FARG)
1924 uerror("parameter '%s' redefined", s->sname);
1925
1926 s->stype = p->n_type;
1927 s->sdf = p->n_df;
1928 s->sap = p->n_ap;
1929 if (a)
1930 attr_add(s->sap, gcc_attr_wrapper(a));
1931 p1nfree(p);
1932 }
1933
1934 void
1935 branch(int lbl)
1936 {
1937 int r = reached++;
1938 ecomp(biop(GOTO, bcon(lbl), NULL));
1939 reached = r;
1940 }
1941
1942 /*
1943 * Create a printable string based on an encoded string.
1944 */
1945 static char *
1946 mkpstr(char *str)
1947 {
1948 char *os, *s;
1949 size_t l = strlen(str) + 3; /* \t + \n + \0 */
1950
1951 os = s = stmtalloc(l);
1952 *s++ = '\t';
1953 while (*str) {
1954 if (*str == '\\')
1955 *s++ = esccon(&str);
1956 else
1957 *s++ = *str++;
1958 }
1959 *s++ = '\n';
1960 *s = 0;
1961
1962 return os;
1963 }
1964
1965 /*
1966 * Fake a symtab entry for compound literals.
1967 */
1968 static struct symtab *
1969 clbrace(P1ND *p)
1970 {
1971 struct symtab *sp;
1972
1973 sp = getsymtab(simname("cl"), STEMP);
1974 sp->stype = p->n_type;
1975 sp->squal = p->n_qual;
1976 sp->sdf = p->n_df;
1977 sp->sap = p->n_ap;
1978 p1tfree(p);
1979 if (blevel == 0 && xnf != NULL) {
1980 sp->sclass = STATIC;
1981 sp->slevel = 2;
1982 sp->soffset = getlab();
1983 } else {
1984 sp->sclass = blevel ? AUTO : STATIC;
1985 if (!ISARY(sp->stype) || sp->sdf->ddim != NOOFFSET) {
1986 sp->soffset = NOOFFSET;
1987 oalloc(sp, &autooff);
1988 }
1989 }
1990 beginit(sp);
1991 return sp;
1992 }
1993
1994 char *
1995 simname(char *s)
1996 {
1997 size_t len = strlen(s) + 10 + 1;
1998 char *w = tmpalloc(len); /* uncommon */
1999
2000 snprintf(w, len, "%s%d", s, getlab());
2001 return w;
2002 }
2003
2004 P1ND *
2005 biop(int op, P1ND *l, P1ND *r)
2006 {
2007 return block(op, l, r, INT, 0, 0);
2008 }
2009
2010 static P1ND *
2011 cmop(P1ND *l, P1ND *r)
2012 {
2013 return biop(CM, l, r);
2014 }
2015
2016 static P1ND *
2017 voidcon(void)
2018 {
2019 return block(ICON, NULL, NULL, STRTY, 0, 0);
2020 }
2021
2022 /* Support for extended assembler a' la' gcc style follows below */
2023
2024 static P1ND *
2025 xmrg(P1ND *out, P1ND *in)
2026 {
2027 P1ND *p = in;
2028
2029 if (p->n_op == XARG) {
2030 in = cmop(out, p);
2031 } else {
2032 while (p->n_left->n_op == CM)
2033 p = p->n_left;
2034 p->n_left = cmop(out, p->n_left);
2035 }
2036 return in;
2037 }
2038
2039 /*
2040 * Put together in and out node lists in one list, and balance it with
2041 * the constraints on the right side of a CM node.
2042 */
2043 static P1ND *
2044 xcmop(P1ND *out, P1ND *in, P1ND *str)
2045 {
2046 P1ND *p, *q;
2047
2048 if (out) {
2049 /* D out-list sanity check */
2050 for (p = out; p->n_op == CM; p = p->n_left) {
2051 q = p->n_right;
2052 if (q->n_name[0] != '=' && q->n_name[0] != '+')
2053 uerror("output missing =");
2054 }
2055 if (p->n_name[0] != '=' && p->n_name[0] != '+')
2056 uerror("output missing =");
2057 if (in == NULL)
2058 p = out;
2059 else
2060 p = xmrg(out, in);
2061 } else if (in) {
2062 p = in;
2063 } else
2064 p = voidcon();
2065
2066 if (str == NULL)
2067 str = voidcon();
2068 return cmop(p, str);
2069 }
2070
2071 /*
2072 * Generate a XARG node based on a string and an expression.
2073 */
2074 static P1ND *
2075 xasmop(char *str, P1ND *p)
2076 {
2077
2078 p = biop(XARG, p, NULL);
2079 p->n_name = str;
2080 return p;
2081 }
2082
2083 /*
2084 * Generate a XASM node based on a string and an expression.
2085 */
2086 static void
2087 mkxasm(char *str, P1ND *p)
2088 {
2089 P1ND *q;
2090
2091 q = biop(XASM, p->n_left, p->n_right);
2092 q->n_name = str;
2093 p1nfree(p);
2094 ecomp(optloop(q));
2095 }
2096
2097 static struct attr *
2098 gcc_attr_wrapper(P1ND *p)
2099 {
2100 #ifdef GCC_COMPAT
2101 return gcc_attr_parse(p);
2102 #else
2103 if (p != NULL)
2104 uerror("gcc attribute used");
2105 return NULL;
2106 #endif
2107 }
2108
2109 #ifdef GCC_COMPAT
2110 static P1ND *
2111 tyof(P1ND *p)
2112 {
2113 static struct symtab spp;
2114 P1ND *q = block(TYPE, NULL, NULL, p->n_type, p->n_df, p->n_ap);
2115 q->n_qual = p->n_qual;
2116 q->n_sp = &spp; /* for typenode */
2117 p1walkf(p, putjops, 0);
2118 p1tfree(p);
2119 return q;
2120 }
2121
2122 #else
2123 static P1ND *
2124 tyof(P1ND *p)
2125 {
2126 uerror("typeof gcc extension");
2127 return bcon(0);
2128 }
2129 #endif
2130
2131 /*
2132 * Traverse an unhandled expression tree bottom-up and call buildtree()
2133 * or equivalent as needed.
2134 */
2135 P1ND *
2136 eve(P1ND *p)
2137 {
2138 struct symtab *sp;
2139 P1ND *r, *p1, *p2;
2140 int x;
2141
2142 p1 = p->n_left;
2143 p2 = p->n_right;
2144 switch (p->n_op) {
2145 case NAME:
2146 sp = lookup((char *)p->n_sp,
2147 attr_find(p->n_ap, ATTR_P1LABELS) ? SLBLNAME|STEMP : 0);
2148 if (sp->sflags & SINLINE)
2149 inline_ref(sp);
2150 r = nametree(sp);
2151 if (sp->sflags & SDYNARRAY)
2152 r = buildtree(UMUL, r, NULL);
2153 #ifdef GCC_COMPAT
2154 if (attr_find(sp->sap, GCC_ATYP_DEPRECATED))
2155 warner(Wdeprecated_declarations, sp->sname);
2156 #endif
2157 break;
2158
2159 case DOT:
2160 case STREF:
2161 r = structref(eve(p1), p->n_op, (char *)p2->n_sp);
2162 p1nfree(p2);
2163 break;
2164
2165 case CAST:
2166 p2 = eve(p2);
2167 #ifndef NO_COMPLEX
2168 if (ANYCX(p1) || ANYCX(p2)) {
2169 r = cxcast(p1, p2);
2170 break;
2171 }
2172 #endif
2173 #ifdef TARGET_TIMODE
2174 if ((r = gcc_eval_ticast(CAST, p1, p2)) != NULL)
2175 break;
2176 #endif
2177 p1 = buildtree(CAST, p1, p2);
2178 p1nfree(p1->n_left);
2179 r = p1->n_right;
2180 p1nfree(p1);
2181 break;
2182
2183
2184 case SZOF:
2185 x = xinline; xinline = 0; /* XXX hack */
2186 if (glval(p2) == 0)
2187 p1 = eve(p1);
2188 else
2189 TYMFIX(p1);
2190 p1nfree(p2);
2191 r = doszof(p1);
2192 xinline = x;
2193 break;
2194
2195 case LB:
2196 p1 = eve(p1);
2197 p2 = eve(p2);
2198 #ifdef TARGET_TIMODE
2199 if (isti(p2)) {
2200 P1ND *s = block(NAME, NULL, NULL, LONG, 0, 0);
2201 if ((r = gcc_eval_ticast(CAST, s, p2)) != NULL)
2202 p2 = r;
2203 p1nfree(s);
2204 }
2205 #endif
2206 r = buildtree(UMUL, buildtree(PLUS, p1, p2), NULL);
2207 break;
2208
2209 case COMPL:
2210 #ifndef NO_COMPLEX
2211 p1 = eve(p1);
2212 if (ANYCX(p1))
2213 r = cxconj(p1);
2214 else
2215 r = buildtree(COMPL, p1, NULL);
2216 break;
2217 #endif
2218 case UPLUS:
2219 r = eve(p1);
2220 if (r->n_op == FLD || r->n_type < INT)
2221 r = buildtree(PLUS, r, bcon(0)); /* must be size int */
2222 break;
2223
2224 case UMINUS:
2225 #ifndef NO_COMPLEX
2226 p1 = eve(p1);
2227 if (ANYCX(p1))
2228 r = cxop(UMINUS, p1, p1);
2229 else
2230 r = buildtree(UMINUS, p1, NULL);
2231 break;
2232 #endif
2233 case NOT:
2234 case UMUL:
2235 p1 = eve(p1);
2236 #ifdef TARGET_TIMODE
2237 if ((r = gcc_eval_tiuni(p->n_op, p1)) != NULL)
2238 break;
2239 #endif
2240 #ifndef NO_COMPLEX
2241 if (p->n_op == NOT && ANYCX(p1))
2242 p1 = cxop(NE, p1, bcon(0));
2243 #endif
2244 r = buildtree(p->n_op, p1, NULL);
2245 break;
2246
2247 case ADDROF:
2248 r = eve(p1);
2249 if (ISFTN(p->n_type)/* || ISARY(p->n_type) */){
2250 #ifdef notdef
2251 werror( "& before array or function: ignored" );
2252 #endif
2253 } else
2254 r = buildtree(ADDROF, r, NULL);
2255 break;
2256
2257 case UCALL:
2258 p2 = NULL;
2259 /* FALLTHROUGH */
2260 case CALL:
2261 if (p1->n_op == NAME) {
2262 sp = lookup((char *)p1->n_sp, 0);
2263 #ifndef NO_C_BUILTINS
2264 if (sp->sflags & SBUILTIN) {
2265 p1nfree(p1);
2266 r = builtin_check(sp, p2);
2267 break;
2268 }
2269 #endif
2270 if (sp->stype == UNDEF) {
2271 p1->n_type = FTN|INT;
2272 p1->n_sp = sp;
2273 p1->n_ap = NULL;
2274 defid(p1, EXTERN);
2275 }
2276 p1nfree(p1);
2277 #ifdef GCC_COMPAT
2278 if (attr_find(sp->sap, GCC_ATYP_DEPRECATED))
2279 warner(Wdeprecated_declarations, sp->sname);
2280 #endif
2281 if (p->n_op == CALL)
2282 p2 = eve(p2);
2283 r = doacall(sp, nametree(sp), p2);
2284 } else {
2285 if (p->n_op == CALL)
2286 p2 = eve(p2);
2287 r = doacall(NULL, eve(p1), p2);
2288 }
2289 break;
2290
2291 #ifndef NO_COMPLEX
2292 case XREAL:
2293 case XIMAG:
2294 p1 = eve(p1);
2295 r = cxelem(p->n_op, p1);
2296 break;
2297 #endif
2298
2299 case COLON:
2300 case MUL:
2301 case DIV:
2302 case PLUS:
2303 case MINUS:
2304 case ASSIGN:
2305 case EQ:
2306 case NE:
2307 case OROR:
2308 case ANDAND:
2309 #ifndef NO_COMPLEX
2310 p1 = eve(p1);
2311 p2 = eve(p2);
2312 #ifdef TARGET_TIMODE
2313 if ((r = gcc_eval_timode(p->n_op, p1, p2)) != NULL)
2314 break;
2315 #endif
2316 if (ANYCX(p1) || ANYCX(p2)) {
2317 r = cxop(p->n_op, p1, p2);
2318 } else if (ISITY(p1->n_type) || ISITY(p2->n_type)) {
2319 r = imop(p->n_op, p1, p2);
2320 } else
2321 r = buildtree(p->n_op, p1, p2);
2322 break;
2323 #endif
2324 case MOD:
2325 case CM:
2326 case GT:
2327 case GE:
2328 case LT:
2329 case LE:
2330 case RS:
2331 case LS:
2332 case RSEQ:
2333 case LSEQ:
2334 case AND:
2335 case OR:
2336 case ER:
2337 case EREQ:
2338 case OREQ:
2339 case ANDEQ:
2340 case QUEST:
2341 p1 = eve(p1);
2342 p2 = eve(p2);
2343 #ifdef TARGET_TIMODE
2344 if ((r = gcc_eval_timode(p->n_op, p1, p2)) != NULL)
2345 break;
2346 #endif
2347 r = buildtree(p->n_op, p1, p2);
2348 break;
2349
2350 case BIQUEST: /* gcc e ?: e op */
2351 p1 = eve(p1);
2352 r = tempnode(0, p1->n_type, p1->n_df, p1->n_ap);
2353 p2 = eve(biop(COLON, p1tcopy(r), p2));
2354 r = buildtree(QUEST, buildtree(ASSIGN, r, p1), p2);
2355 break;
2356
2357 case INCR:
2358 case DECR:
2359 case MODEQ:
2360 case MINUSEQ:
2361 case PLUSEQ:
2362 case MULEQ:
2363 case DIVEQ:
2364 p1 = eve(p1);
2365 p2 = eve(p2);
2366 #ifdef TARGET_TIMODE
2367 if ((r = gcc_eval_timode(p->n_op, p1, p2)) != NULL)
2368 break;
2369 #endif
2370 #ifndef NO_COMPLEX
2371 if (ANYCX(p1) || ANYCX(p2)) {
2372 r = cxop(UNASG p->n_op, p1tcopy(p1), p2);
2373 r = cxop(ASSIGN, p1, r);
2374 break;
2375 } else if (ISITY(p1->n_type) || ISITY(p2->n_type)) {
2376 r = imop(UNASG p->n_op, p1tcopy(p1), p2);
2377 r = cxop(ASSIGN, p1, r);
2378 break;
2379 }
2380 /* FALLTHROUGH */
2381 #endif
2382 r = buildtree(p->n_op, p1, p2);
2383 break;
2384
2385 case STRING:
2386 r = strend(p->n_name, (TWORD)glval(p));
2387 break;
2388
2389 case COMOP:
2390 if (p1->n_op == GOTO) {
2391 /* inside ({ }), eve already called */
2392 r = buildtree(p->n_op, p1, p2);
2393 } else {
2394 p1 = eve(p1);
2395 r = buildtree(p->n_op, p1, eve(p2));
2396 }
2397 break;
2398
2399 case TYPE:
2400 case ICON:
2401 case FCON:
2402 case TEMP:
2403 return p;
2404
2405 case CLOP:
2406 r = nametree(p->n_sp);
2407 break;
2408
2409 default:
2410 #ifdef PCC_DEBUG
2411 p1fwalk(p, eprint, 0);
2412 #endif
2413 cerror("eve");
2414 r = NULL;
2415 }
2416 p1nfree(p);
2417 return r;
2418 }
2419
2420 int
2421 con_e(P1ND *p)
2422 {
2423 return (int)icons(optloop(eve(p)));
2424 }
2425
2426 void
2427 uawarn(P1ND *p, char *s)
2428 {
2429 if (p == 0)
2430 return;
2431 if (attrwarn)
2432 werror("unhandled %s attribute", s);
2433 p1tfree(p);
2434 }
2435
2436 static void
2437 dainit(P1ND *d, P1ND *a)
2438 {
2439 if (d == NULL) {
2440 asginit(a);
2441 } else if (d->n_op == CM) {
2442 int is = con_e(d->n_left);
2443 int ie = con_e(d->n_right);
2444 int i;
2445
2446 p1nfree(d);
2447 if (ie < is)
2448 uerror("negative initializer range");
2449 desinit(biop(LB, NULL, bcon(is)));
2450 for (i = is; i < ie; i++)
2451 asginit(p1tcopy(a));
2452 asginit(a);
2453 } else {
2454 cerror("dainit");
2455 }
2456 }
2457
2458 /*
2459 * Traverse down and tymerge() where appropriate.
2460 */
2461 static P1ND *
2462 tymfix(P1ND *p)
2463 {
2464 P1ND *q;
2465 int o = coptype(p->n_op);
2466
2467 switch (o) {
2468 case LTYPE:
2469 break;
2470 case UTYPE:
2471 p->n_left = tymfix(p->n_left);
2472 break;
2473 case BITYPE:
2474 p->n_left = tymfix(p->n_left);
2475 p->n_right = tymfix(p->n_right);
2476 if (p->n_op == TYMERGE) {
2477 q = tymerge(p->n_left, p->n_right);
2478 q->n_ap = attr_add(q->n_ap, p->n_ap);
2479 p1tfree(p->n_left);
2480 p1nfree(p);
2481 p = q;
2482 }
2483 break;
2484 }
2485 return p;
2486 }
2487
2488 static P1ND *
2489 aryfix(P1ND *p)
2490 {
2491 P1ND *q;
2492
2493 for (q = p; q->n_op != NAME; q = q->n_left) {
2494 if (q->n_op == LB) {
2495 q->n_right = optloop(eve(q->n_right));
2496 if ((blevel == 0 || rpole != NULL) &&
2497 !nncon(q->n_right))
2498 uerror("array size not constant");
2499 /*
2500 * Checks according to 6.7.5.2 clause 1:
2501 * "...the expression shall have an integer type."
2502 * "If the expression is a constant expression,
2503 * it shall have a value greater than zero."
2504 */
2505 if (!ISINTEGER(q->n_right->n_type))
2506 werror("array size is not an integer");
2507 else if (q->n_right->n_op == ICON &&
2508 glval(q->n_right) < 0 &&
2509 glval(q->n_right) != NOOFFSET) {
2510 uerror("array size cannot be negative");
2511 slval(q->n_right, 1);
2512 }
2513 } else if (q->n_op == CALL)
2514 q->n_right = namekill(q->n_right, 1);
2515 }
2516 return p;
2517 }
2518
2519 struct labs {
2520 struct labs *next;
2521 int lab;
2522 } *labp;
2523
2524 static void
2525 savlab(int lab)
2526 {
2527 struct labs *l = tmpalloc(sizeof(struct labs)); /* uncommon */
2528 l->lab = lab < 0 ? -lab : lab;
2529 l->next = labp;
2530 labp = l;
2531 }
2532
2533 int *
2534 mkclabs(void)
2535 {
2536 struct labs *l;
2537 int i, *rv;
2538
2539 for (i = 0, l = labp; l; l = l->next, i++)
2540 ;
2541 rv = tmpalloc((i+1)*sizeof(int)); /* uncommon */
2542 for (i = 0, l = labp; l; l = l->next, i++)
2543 rv[i] = l->lab;
2544 rv[i] = 0;
2545 labp = 0;
2546 return rv;
2547 }
2548
2549 void
2550 xcbranch(P1ND *p, int lab)
2551 {
2552 #ifndef NO_COMPLEX
2553 if (ANYCX(p))
2554 p = cxop(NE, p, bcon(0));
2555 #endif
2556 cbranch(buildtree(NOT, p, NULL), bcon(lab));
2557 }
2558
2559 /*
2560 * New a case entry to genlist.
2561 * tn is type, e is expression.
2562 */
2563 static struct genlist *
2564 newgen(P1ND *tn, P1ND *e)
2565 {
2566 struct genlist *ng;
2567 TWORD t;
2568
2569 if (tn) {
2570 t = tn->n_type;
2571 p1tfree(tn);
2572 } else
2573 t = 0;
2574
2575 /* add new entry */
2576 ng = malloc(sizeof(struct genlist));
2577 ng->next = NULL;
2578 ng->t = t;
2579 ng->p = e;
2580 return ng;
2581 }
2582
2583 /*
2584 * Add a case entry to genlist.
2585 * g is list, ng is new entry.
2586 */
2587 static struct genlist *
2588 addgen(struct genlist *g, struct genlist *ng)
2589 {
2590 struct genlist *w;
2591
2592 /* search for duplicate type */
2593 for (w = g; w; w = w->next) {
2594 if (w->t == ng->t)
2595 uerror("duplicate type in _Generic");
2596 }
2597 ng->next = g;
2598 return ng;
2599 }
2600
2601 static P1ND *
2602 dogen(struct genlist *g, P1ND *e)
2603 {
2604 struct genlist *ng;
2605 P1ND *w, *p;
2606
2607 e = eve(e);
2608
2609 /* search for direct match */
2610 for (ng = g, w = p = NULL; ng; ng = ng->next) {
2611 if (ng->t == 0)
2612 p = ng->p; /* save default */
2613 if (e->n_type == ng->t)
2614 w = ng->p;
2615 }
2616
2617 /* if no match, use generic */
2618 if (w == NULL) {
2619 if (p == NULL) {
2620 uerror("_Generic: no default found");
2621 p = bcon(0);
2622 }
2623 w = p;
2624 }
2625
2626 /* free tree */
2627 while (g) {
2628 if (g->p != w)
2629 p1tfree(g->p);
2630 ng = g->next;
2631 free(g);
2632 g = ng;
2633 }
2634
2635 p1tfree(e);
2636 return w;
2637 }