1 /*-----------------------------------------------------------------------
3 SDCC.y - parser definition file for sdcc :
4 Written By : Sandeep Dutta . sandeep.dutta@usa.net (1997)
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 In other words, you are welcome to use, share and improve this program.
21 You are forbidden to forbid anyone else to use, share and improve
22 what you give them. Help stamp out software-hoarding!
23 -------------------------------------------------------------------------*/
28 #include "SDCCglobl.h"
30 #include "SDCChasht.h"
38 extern int yyerror (char *);
40 int NestLevel = 0 ; /* current NestLevel */
41 int stackPtr = 1 ; /* stack pointer */
42 int xstackPtr = 0 ; /* xstack pointer */
44 int blockNo = 0 ; /* sequential block number */
49 char lbuff[1024]; /* local buffer */
51 /* break & continue stacks */
52 STACK_DCL(continueStack ,symbol *,MAX_NEST_LEVEL)
53 STACK_DCL(breakStack ,symbol *,MAX_NEST_LEVEL)
54 STACK_DCL(forStack ,symbol *,MAX_NEST_LEVEL)
55 STACK_DCL(swStk ,ast *,MAX_NEST_LEVEL)
56 STACK_DCL(blockNum,int,MAX_NEST_LEVEL*3)
58 value *cenum = NULL ; /* current enumeration type chain*/
64 symbol *sym ; /* symbol table pointer */
65 structdef *sdef; /* structure definition */
66 char yychar[SDCC_NAME_MAX+1];
67 sym_link *lnk ; /* declarator or specifier */
68 int yyint; /* integer value returned */
69 value *val ; /* for integer constant */
70 initList *ilist; /* initial list */
71 char *yyinline; /* inlined assembler code */
72 ast *asts; /* expression tree */
75 %token <yychar> IDENTIFIER TYPE_NAME
76 %token <val> CONSTANT STRING_LITERAL
78 %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
80 %token <yyint> MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
81 %token <yyint> SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
82 %token <yyint> XOR_ASSIGN OR_ASSIGN
83 %token TYPEDEF EXTERN STATIC AUTO REGISTER CODE EEPROM INTERRUPT SFR AT SBIT
84 %token REENTRANT USING XDATA DATA IDATA PDATA VAR_ARGS CRITICAL NONBANKED BANKED
85 %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID BIT
86 %token STRUCT UNION ENUM ELIPSIS RANGE FAR
87 %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
89 %token <yyinline> INLINEASM
90 %token IFX ADDRESS_OF GET_VALUE_AT_ADDRESS SPIL UNSPIL GETHBIT
91 %token BITWISEAND UNARYMINUS IPUSH IPOP PCALL ENDFUNCTION JUMPTABLE
93 %token CAST CALL PARAM NULLOP BLOCK LABEL RECEIVE SEND ARRAYINIT
95 %type <yyint> Interrupt_storage
96 %type <sym> identifier declarator declarator2 enumerator_list enumerator
97 %type <sym> struct_declarator
98 %type <sym> struct_declarator_list struct_declaration struct_declaration_list
99 %type <sym> declaration init_declarator_list init_declarator
100 %type <sym> declaration_list identifier_list parameter_identifier_list
101 %type <sym> declarator2_function_attributes while do for
102 %type <lnk> pointer type_specifier_list type_specifier type_name
103 %type <lnk> storage_class_specifier struct_or_union_specifier
104 %type <lnk> declaration_specifiers sfr_reg_bit type_specifier2
105 %type <lnk> function_attribute function_attributes enum_specifier
106 %type <lnk> abstract_declarator abstract_declarator2 unqualified_pointer
107 %type <val> parameter_type_list parameter_list parameter_declaration opt_assign_expr
108 %type <sdef> stag opt_stag
109 %type <asts> primary_expr
110 %type <asts> postfix_expr unary_expr cast_expr multiplicative_expr
111 %type <asts> additive_expr shift_expr relational_expr equality_expr
112 %type <asts> and_expr exclusive_or_expr inclusive_or_expr logical_or_expr
113 %type <asts> logical_and_expr conditional_expr assignment_expr constant_expr
114 %type <asts> expr argument_expr_list function_definition expr_opt
115 %type <asts> statement_list statement labeled_statement compound_statement
116 %type <asts> expression_statement selection_statement iteration_statement
117 %type <asts> jump_statement function_body else_statement string_literal
118 %type <ilist> initializer initializer_list
119 %type <yyint> unary_operator assignment_operator struct_or_union
126 : external_definition
127 | file external_definition
131 : function_definition {
136 && IS_FUNC($1->type))
138 /* The only legal storage classes for
139 * a function prototype (declaration)
140 * are extern and static. extern is the
141 * default. Thus, if this function isn't
142 * explicitly marked static, mark it
146 && IS_SPEC($1->etype)
147 && !SPEC_STAT($1->etype))
149 SPEC_EXTR($1->etype) = 1;
153 allocVariables ($1) ;
154 cleanUpLevel (SymbolTab,1);
159 : declarator function_body { /* function type not specified */
160 /* assume it to be 'int' */
161 addDecl($1,0,newIntLink());
162 $$ = createFunction($1,$2);
164 | declaration_specifiers declarator function_body
166 pointerTypes($2->type,copyLinkChain($1));
168 $$ = createFunction($2,$3);
173 : function_attributes
174 | function_attributes function_attribute { $$ = mergeSpec($1,$2,"function_attribute"); }
180 $$->class = SPECIFIER ;
181 FUNC_REGBANK($$) = (int) floatFromVal($2);
183 | REENTRANT { $$ = newLink ();
184 $$->class = SPECIFIER ;
187 | CRITICAL { $$ = newLink ();
188 $$->class = SPECIFIER ;
189 FUNC_ISCRITICAL($$) = 1;
191 | NAKED { $$ = newLink ();
192 $$->class = SPECIFIER ;
195 | NONBANKED {$$ = newLink ();
196 $$->class = SPECIFIER ;
197 FUNC_NONBANKED($$) = 1;
198 if (FUNC_BANKED($$)) {
199 werror(W_BANKED_WITH_NONBANKED);
202 | BANKED {$$ = newLink ();
203 $$->class = SPECIFIER ;
205 if (FUNC_NONBANKED($$)) {
206 werror(W_BANKED_WITH_NONBANKED);
209 werror(W_BANKED_WITH_STATIC);
215 $$->class = SPECIFIER ;
216 FUNC_INTNO($$) = $1 ;
223 | declaration_list compound_statement
225 werror(E_OLD_STYLE,($1 ? $1->name: "")) ;
231 : identifier { $$ = newAst_VALUE(symbolVal($1)); }
232 | CONSTANT { $$ = newAst_VALUE($1); }
234 | '(' expr ')' { $$ = $2 ; }
238 : STRING_LITERAL { $$ = newAst_VALUE($1); }
243 | postfix_expr '[' expr ']' { $$ = newNode ('[', $1, $3) ; }
244 | postfix_expr '(' ')' { $$ = newNode (CALL,$1,NULL);
245 $$->left->funcName = 1;}
246 | postfix_expr '(' argument_expr_list ')'
248 $$ = newNode (CALL,$1,$3) ; $$->left->funcName = 1;
250 | postfix_expr '.' identifier
252 $3 = newSymbol($3->name,NestLevel);
254 $$ = newNode(PTR_OP,newNode('&',$1,NULL),newAst_VALUE(symbolVal($3)));
255 /* $$ = newNode('.',$1,newAst(EX_VALUE,symbolVal($3))) ; */
257 | postfix_expr PTR_OP identifier
259 $3 = newSymbol($3->name,NestLevel);
261 $$ = newNode(PTR_OP,$1,newAst_VALUE(symbolVal($3)));
263 | postfix_expr INC_OP
264 { $$ = newNode(INC_OP,$1,NULL);}
265 | postfix_expr DEC_OP
266 { $$ = newNode(DEC_OP,$1,NULL); }
271 | assignment_expr ',' argument_expr_list { $$ = newNode(PARAM,$1,$3); }
276 | INC_OP unary_expr { $$ = newNode(INC_OP,NULL,$2); }
277 | DEC_OP unary_expr { $$ = newNode(DEC_OP,NULL,$2); }
278 | unary_operator cast_expr { $$ = newNode($1,$2,NULL) ; }
279 | SIZEOF unary_expr { $$ = newNode(SIZEOF,NULL,$2); }
280 | SIZEOF '(' type_name ')' { $$ = newAst_VALUE(sizeofOp($3)); }
281 | TYPEOF unary_expr { $$ = newNode(TYPEOF,NULL,$2); }
295 | '(' type_name ')' cast_expr { $$ = newNode(CAST,newAst_LINK($2),$4); }
300 | multiplicative_expr '*' cast_expr { $$ = newNode('*',$1,$3);}
301 | multiplicative_expr '/' cast_expr { $$ = newNode('/',$1,$3);}
302 | multiplicative_expr '%' cast_expr { $$ = newNode('%',$1,$3);}
306 : multiplicative_expr
307 | additive_expr '+' multiplicative_expr { $$=newNode('+',$1,$3);}
308 | additive_expr '-' multiplicative_expr { $$=newNode('-',$1,$3);}
313 | shift_expr LEFT_OP additive_expr { $$ = newNode(LEFT_OP,$1,$3); }
314 | shift_expr RIGHT_OP additive_expr { $$ = newNode(RIGHT_OP,$1,$3); }
319 | relational_expr '<' shift_expr {
321 newNode('!',newNode(GE_OP,$1,$3),NULL) :
322 newNode('<', $1,$3));
324 | relational_expr '>' shift_expr {
326 newNode('!',newNode(LE_OP,$1,$3),NULL) :
329 | relational_expr LE_OP shift_expr {
331 newNode('!', newNode('>', $1 , $3 ), NULL) :
332 newNode(LE_OP,$1,$3));
334 | relational_expr GE_OP shift_expr {
336 newNode('!', newNode('<', $1 , $3 ), NULL) :
337 newNode(GE_OP,$1,$3));
343 | equality_expr EQ_OP relational_expr {
345 newNode('!',newNode(NE_OP,$1,$3),NULL) :
346 newNode(EQ_OP,$1,$3));
348 | equality_expr NE_OP relational_expr {
350 newNode('!', newNode(EQ_OP,$1,$3), NULL) :
351 newNode(NE_OP,$1,$3));
357 | and_expr '&' equality_expr { $$ = newNode('&',$1,$3);}
362 | exclusive_or_expr '^' and_expr { $$ = newNode('^',$1,$3);}
367 | inclusive_or_expr '|' exclusive_or_expr { $$ = newNode('|',$1,$3);}
372 | logical_and_expr AND_OP inclusive_or_expr
373 { $$ = newNode(AND_OP,$1,$3);}
378 | logical_or_expr OR_OP logical_and_expr
379 { $$ = newNode(OR_OP,$1,$3); }
384 | logical_or_expr '?' logical_or_expr ':' conditional_expr
386 $$ = newNode(':',$3,$5) ;
387 $$ = newNode('?',$1,$$) ;
393 | unary_expr assignment_operator assignment_expr
398 $$ = newNode($2,$1,$3);
401 $$ = newNode('=',$1,newNode('*',copyAst($1),$3));
404 $$ = newNode('=',$1,newNode('/',copyAst($1),$3));
407 $$ = newNode('=',$1,newNode('%',copyAst($1),$3));
410 $$ = newNode('=',$1,newNode('+',copyAst($1),$3));
413 $$ = newNode('=',$1,newNode('-',copyAst($1),$3));
416 $$ = newNode('=',$1,newNode(LEFT_OP,copyAst($1),$3));
419 $$ = newNode('=',$1,newNode(RIGHT_OP,copyAst($1),$3));
422 $$ = newNode('=',$1,newNode('&',copyAst($1),$3));
425 $$ = newNode('=',$1,newNode('^',copyAst($1),$3));
428 $$ = newNode('=',$1,newNode('|',copyAst($1),$3));
453 | expr ',' assignment_expr { $$ = newNode(',',$1,$3);}
461 : declaration_specifiers ';' { $$ = NULL ; }
462 | declaration_specifiers init_declarator_list ';'
464 /* add the specifier list to the id */
467 for (sym1 = sym = reverseSyms($2);sym != NULL;sym = sym->next) {
468 sym_link *lnk = copyLinkChain($1);
469 /* do the pointer stuff */
470 pointerTypes(sym->type,lnk);
471 addDecl (sym,0,lnk) ;
478 declaration_specifiers
479 : storage_class_specifier { $$ = $1; }
480 | storage_class_specifier declaration_specifiers {
481 /* if the decl $2 is not a specifier */
482 /* find the spec and replace it */
485 while (lnk && !IS_SPEC(lnk->next))
487 lnk->next = mergeSpec($1,lnk->next, yytext);
491 $$ = mergeSpec($1,$2, yytext);
493 | type_specifier { $$ = $1; }
494 | type_specifier declaration_specifiers {
495 /* if the decl $2 is not a specifier */
496 /* find the spec and replace it */
499 while (lnk && !IS_SPEC(lnk->next))
501 lnk->next = mergeSpec($1,lnk->next, yytext);
505 $$ = mergeSpec($1,$2, yytext);
511 | init_declarator_list ',' init_declarator { $3->next = $1 ; $$ = $3;}
515 : declarator { $1->ival = NULL ; }
516 | declarator '=' initializer { $1->ival = $3 ; }
520 storage_class_specifier
523 $$->class = SPECIFIER ;
524 SPEC_TYPEDEF($$) = 1 ;
528 $$->class = SPECIFIER ;
533 $$->class = SPECIFIER ;
538 $$->class = SPECIFIER ;
539 SPEC_SCLS($$) = S_AUTO ;
543 $$->class = SPECIFIER ;
544 SPEC_SCLS($$) = S_REGISTER ;
549 : INTERRUPT CONSTANT { $$ = (int) floatFromVal($2) ; }
554 | type_specifier2 AT constant_expr
556 /* add this to the storage class specifier */
557 SPEC_ABSA($1) = 1; /* set the absolute addr flag */
558 /* now get the abs addr from value */
559 SPEC_ADDR($1) = (int) floatFromVal(constExprValue($3,TRUE)) ;
566 $$->class = SPECIFIER ;
567 SPEC_NOUN($$) = V_CHAR ;
571 $$->class = SPECIFIER ;
572 $$->select.s._short = 1 ;
576 $$->class = SPECIFIER ;
577 SPEC_NOUN($$) = V_INT ;
581 $$->class = SPECIFIER ;
586 $$->class = SPECIFIER ;
587 $$->select.s._signed = 1;
591 $$->class = SPECIFIER ;
596 $$->class = SPECIFIER ;
597 SPEC_NOUN($$) = V_VOID ;
601 $$->class = SPECIFIER ;
606 $$->class = SPECIFIER ;
607 SPEC_VOLATILE($$) = 1 ;
611 SPEC_NOUN($$) = V_FLOAT;
612 $$->class = SPECIFIER ;
616 $$->class = SPECIFIER ;
617 SPEC_SCLS($$) = S_XDATA ;
621 $$->class = SPECIFIER ;
622 SPEC_SCLS($$) = S_CODE ;
626 $$->class = SPECIFIER ;
627 SPEC_SCLS($$) = S_EEPROM ;
631 $$->class = SPECIFIER ;
632 SPEC_SCLS($$) = S_DATA ;
636 $$->class = SPECIFIER ;
637 SPEC_SCLS($$) = S_IDATA ;
641 $$->class = SPECIFIER ;
642 SPEC_SCLS($$) = S_PDATA ;
646 $$->class = SPECIFIER ;
647 SPEC_NOUN($$) = V_BIT ;
648 SPEC_SCLS($$) = S_BIT ;
653 | struct_or_union_specifier
662 sym = findSym(TypedefTab,NULL,$1) ;
663 $$ = p = copyLinkChain(sym->type);
664 SPEC_TYPEDEF(getSpec(p)) = 0;
672 $$->class = SPECIFIER ;
673 SPEC_NOUN($$) = V_SBIT;
674 SPEC_SCLS($$) = S_SBIT;
678 $$->class = SPECIFIER ;
679 SPEC_NOUN($$) = V_CHAR;
680 SPEC_SCLS($$) = S_SFR ;
685 struct_or_union_specifier
686 : struct_or_union opt_stag '{' struct_declaration_list '}'
690 /* Create a structdef */
692 sdef->fields = reverseSyms($4) ; /* link the fields */
693 sdef->size = compStructSize($1,sdef); /* update size of */
695 /* Create the specifier */
697 $$->class = SPECIFIER ;
698 SPEC_NOUN($$) = V_STRUCT;
699 SPEC_STRUCT($$)= sdef ;
701 | struct_or_union stag
704 $$->class = SPECIFIER ;
705 SPEC_NOUN($$) = V_STRUCT;
706 SPEC_STRUCT($$) = $2 ;
711 : STRUCT { $$ = STRUCT ; }
712 | UNION { $$ = UNION ; }
717 | { /* synthesize a name add to structtable */
718 $$ = newStruct(genSymName(NestLevel)) ;
719 $$->level = NestLevel ;
720 addSym (StructTab, $$, $$->tag,$$->level,currBlockno, 0);
724 : identifier { /* add name to structure table */
725 $$ = findSymWithBlock (StructTab,$1,currBlockno);
727 $$ = newStruct($1->name) ;
728 $$->level = NestLevel ;
729 addSym (StructTab, $$, $$->tag,$$->level,currBlockno,0);
734 struct_declaration_list
736 | struct_declaration_list struct_declaration
739 /* go to the end of the chain */
740 while (sym->next) sym = sym->next;
748 : type_specifier_list struct_declarator_list ';'
750 /* add this type to all the symbols */
752 for ( sym = $2 ; sym != NULL ; sym = sym->next ) {
754 /* make the symbol one level up */
757 pointerTypes(sym->type,copyLinkChain($1));
759 sym->type = copyLinkChain($1);
760 sym->etype = getSpec(sym->type);
763 addDecl (sym,0,cloneSpec($1));
764 /* make sure the type is complete and sane */
765 checkTypeSanity(sym->etype, sym->name);
771 struct_declarator_list
773 | struct_declarator_list ',' struct_declarator
782 | ':' constant_expr {
783 $$ = newSymbol (genSymName(NestLevel),NestLevel) ;
784 $$->bitVar = (int) floatFromVal(constExprValue($2,TRUE));
786 | declarator ':' constant_expr
788 $1->bitVar = (int) floatFromVal(constExprValue($3,TRUE));
793 : ENUM '{' enumerator_list '}' {
795 allocVariables(reverseSyms($3)) ;
796 $$ = copyLinkChain(cenum->type);
798 | ENUM identifier '{' enumerator_list '}' {
801 $2->type = copyLinkChain(cenum->type);
802 $2->etype = getSpec($2->type);
803 /* add this to the enumerator table */
804 if (!(csym=findSym(enumTab,$2,$2->name)) &&
805 (csym && csym->level == $2->level))
806 werror(E_DUPLICATE_TYPEDEF,csym->name);
808 addSym ( enumTab,$2,$2->name,$2->level,$2->block, 0);
810 allocVariables (reverseSyms($4));
811 $$ = copyLinkChain(cenum->type);
812 SPEC_SCLS(getSpec($$)) = 0 ;
817 /* check the enumerator table */
818 if ((csym = findSym(enumTab,$2,$2->name)))
819 $$ = copyLinkChain(csym->type);
822 $$->class = SPECIFIER ;
823 SPEC_NOUN($$) = V_INT ;
826 SPEC_SCLS(getSpec($$)) = 0 ;
832 | enumerator_list ',' {
834 | enumerator_list ',' enumerator {
841 : identifier opt_assign_expr {
842 /* make the symbol one level up */
844 $1->type = copyLinkChain($2->type);
845 $1->etype= getSpec($1->type);
846 SPEC_ENUM($1->etype) = 1;
853 : '=' constant_expr {
856 val = constExprValue($2,TRUE);
861 sprintf(lbuff,"%d",(int) floatFromVal(cenum)+1);
862 $$ = cenum = constVal(lbuff);
865 sprintf(lbuff,"%d",0);
866 $$ = cenum = constVal(lbuff);
872 : declarator2_function_attributes { $$ = $1; }
873 | pointer declarator2_function_attributes
875 addDecl ($2,0,reverseLink($1));
880 declarator2_function_attributes
881 : declarator2 { $$ = $1 ; }
882 | declarator2 function_attribute {
883 // copy the functionAttributes (not the args and hasVargs !!)
884 sym_link *funcType=$1->etype;
885 struct value *args=FUNC_ARGS(funcType);
886 unsigned hasVargs=FUNC_HASVARARGS(funcType);
888 memcpy (&funcType->funcAttrs, &$2->funcAttrs,
889 sizeof($2->funcAttrs));
891 FUNC_ARGS(funcType)=args;
892 FUNC_HASVARARGS(funcType)=hasVargs;
895 memset (&$2->funcAttrs, 0,
896 sizeof($2->funcAttrs));
904 | '(' declarator ')' { $$ = $2; }
905 | declarator2 '[' ']'
910 DCL_TYPE(p) = ARRAY ;
914 | declarator2 '[' constant_expr ']'
919 p = (tval = constExprValue($3,TRUE))->etype;
920 /* if it is not a constant then Error */
921 if ( SPEC_SCLS(p) != S_LITERAL)
922 werror(E_CONST_EXPECTED) ;
925 DCL_TYPE(p) = ARRAY ;
926 DCL_ELEM(p) = (int) floatFromVal(tval) ;
930 | declarator2 '(' ')' { addDecl ($1,FUNCTION,NULL) ; }
931 | declarator2 '(' { NestLevel++ ; currBlockno++; } parameter_type_list ')'
934 addDecl ($1,FUNCTION,NULL) ;
936 FUNC_HASVARARGS($1->type) = IS_VARG($4);
937 FUNC_ARGS($1->type) = reverseVal($4);
939 /* nest level was incremented to take care of the parms */
943 // if this was a pointer (to a function)
944 if (IS_PTR($1->type)) {
945 // move the args and hasVargs to the function
946 FUNC_ARGS($1->etype)=FUNC_ARGS($1->type);
947 FUNC_HASVARARGS($1->etype)=FUNC_HASVARARGS($1->type);
948 memset (&$1->type->funcAttrs, 0,
949 sizeof($1->type->funcAttrs));
950 // remove the symbol args (if any)
951 cleanUpLevel(SymbolTab,NestLevel+1);
956 | declarator2 '(' parameter_identifier_list ')'
958 werror(E_OLD_STYLE,$1->name) ;
960 /* assume it returns an int */
961 $1->type = $1->etype = newIntLink();
967 : unqualified_pointer { $$ = $1 ;}
968 | unqualified_pointer type_specifier_list
973 | unqualified_pointer pointer
977 DCL_TYPE($2)=GPOINTER;
979 | unqualified_pointer type_specifier_list pointer
982 if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
983 DCL_PTR_CONST($1) = SPEC_CONST($2);
984 DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
985 switch (SPEC_SCLS($2)) {
987 DCL_TYPE($3) = FPOINTER;
990 DCL_TYPE($3) = IPOINTER ;
993 DCL_TYPE($3) = PPOINTER ;
996 DCL_TYPE($3) = POINTER ;
999 DCL_PTR_CONST($3) = 1;
1000 DCL_TYPE($3) = CPOINTER ;
1003 DCL_TYPE($3) = EEPPOINTER;
1006 // this could be just "constant"
1007 // werror(W_PTR_TYPE_INVALID);
1011 werror (W_PTR_TYPE_INVALID);
1020 DCL_TYPE($$)=UPOINTER;
1026 //| type_specifier_list type_specifier { $$ = mergeSpec ($1,$2, "type_specifier_list"); }
1027 | type_specifier_list type_specifier {
1028 /* if the decl $2 is not a specifier */
1029 /* find the spec and replace it */
1030 if ( !IS_SPEC($2)) {
1031 sym_link *lnk = $2 ;
1032 while (lnk && !IS_SPEC(lnk->next))
1034 lnk->next = mergeSpec($1,lnk->next, "type_specifier_list");
1038 $$ = mergeSpec($1,$2, "type_specifier_list");
1042 parameter_identifier_list
1044 | identifier_list ',' ELIPSIS
1049 | identifier_list ',' identifier
1058 | parameter_list ',' VAR_ARGS { $1->vArgs = 1;}
1062 : parameter_declaration
1063 | parameter_list ',' parameter_declaration
1070 parameter_declaration
1071 : type_specifier_list declarator
1074 pointerTypes($2->type,$1);
1076 for (loop=$2;loop;loop->_isparm=1,loop=loop->next);
1083 $$->etype = getSpec($$->type);
1088 : type_specifier_list { $$ = $1 ;}
1089 | type_specifier_list abstract_declarator
1091 /* go to the end of the list */
1093 pointerTypes($2,$1);
1094 for ( p = $2 ; p->next ; p=p->next);
1101 : pointer { $$ = reverseLink($1); }
1102 | abstract_declarator2
1103 | pointer abstract_declarator2 { $1 = reverseLink($1); $1->next = $2 ; $$ = $1;}
1106 abstract_declarator2
1107 : '(' abstract_declarator ')' { $$ = $2 ; }
1110 DCL_TYPE($$) = ARRAY ;
1113 | '[' constant_expr ']' {
1116 DCL_TYPE($$) = ARRAY ;
1117 DCL_ELEM($$) = (int) floatFromVal(val = constExprValue($2,TRUE));
1119 | abstract_declarator2 '[' ']' {
1121 DCL_TYPE($$) = ARRAY ;
1125 | abstract_declarator2 '[' constant_expr ']'
1129 DCL_TYPE($$) = ARRAY ;
1130 DCL_ELEM($$) = (int) floatFromVal(val = constExprValue($3,TRUE));
1133 | '(' ')' { $$ = NULL;}
1134 | '(' parameter_type_list ')' { $$ = NULL;}
1135 | abstract_declarator2 '(' ')' {
1136 // $1 must be a pointer to a function
1137 sym_link *p=newLink();
1138 DCL_TYPE(p) = FUNCTION;
1141 | abstract_declarator2 '(' parameter_type_list ')' {
1142 if (!IS_VOID($3->type)) {
1143 // this is nonsense, so let's just burp something
1144 werror(E_TOO_FEW_PARMS);
1146 // $1 must be a pointer to a function
1147 sym_link *p=newLink();
1148 DCL_TYPE(p) = FUNCTION;
1154 : assignment_expr { $$ = newiList(INIT_NODE,$1); }
1155 | '{' initializer_list '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
1156 | '{' initializer_list ',' '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
1161 | initializer_list ',' initializer { $3->next = $1; $$ = $3; }
1166 | compound_statement
1167 | expression_statement
1168 | selection_statement
1169 | iteration_statement
1172 ast *ex = newNode(INLINEASM,NULL,NULL);
1173 ex->values.inlineasm = malloc(strlen($1)+1);
1174 strcpy(ex->values.inlineasm,$1);
1180 // : identifier ':' statement { $$ = createLabel($1,$3); }
1181 : identifier ':' { $$ = createLabel($1,NULL); }
1182 | CASE constant_expr ':' statement { $$ = createCase(STACK_PEEK(swStk),$2,$4); }
1183 | DEFAULT ':' statement { $$ = createDefault(STACK_PEEK(swStk),$3); }
1186 start_block : '{' { STACK_PUSH(blockNum,currBlockno); currBlockno = ++blockNo ; }
1189 end_block : '}' { currBlockno = STACK_POP(blockNum); }
1193 : start_block end_block { $$ = createBlock(NULL,NULL); }
1194 | start_block statement_list end_block { $$ = createBlock(NULL,$2) ; }
1196 declaration_list { addSymChain($2); }
1197 end_block { $$ = createBlock($2,NULL) ; }
1199 declaration_list { addSymChain ($2); }
1201 end_block {$$ = createBlock($2,$4) ; }
1202 | error ';' { $$ = NULL ; }
1208 /* if this is typedef declare it immediately */
1209 if ( $1 && IS_TYPEDEF($1->etype)) {
1210 allocVariables ($1);
1217 | declaration_list declaration
1221 /* if this is a typedef */
1222 if ($2 && IS_TYPEDEF($2->etype)) {
1223 allocVariables ($2);
1227 /* get to the end of the previous decl */
1242 | statement_list statement { $$ = newNode(NULLOP,$1,$2) ;}
1245 expression_statement
1251 : ELSE statement { $$ = $2 ; }
1257 : IF '(' expr ')' statement else_statement { noLineno++ ; $$ = createIf ($3, $5, $6 ); noLineno--;}
1258 | SWITCH '(' expr ')' {
1260 static int swLabel = 0 ;
1262 /* create a node for expression */
1263 ex = newNode(SWITCH,$3,NULL);
1264 STACK_PUSH(swStk,ex); /* save it in the stack */
1265 ex->values.switchVals.swNum = swLabel ;
1267 /* now create the label */
1268 sprintf(lbuff,"_swBrk_%d",swLabel++);
1269 $<sym>$ = newSymbol(lbuff,NestLevel);
1270 /* put label in the break stack */
1271 STACK_PUSH(breakStack,$<sym>$);
1274 /* get back the switch form the stack */
1275 $$ = STACK_POP(swStk) ;
1276 $$->right = newNode (NULLOP,$6,createLabel($<sym>5,NULL));
1277 STACK_POP(breakStack);
1281 while : WHILE { /* create and push the continue , break & body labels */
1282 static int Lblnum = 0 ;
1284 sprintf (lbuff,"_whilecontinue_%d",Lblnum);
1285 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1287 sprintf (lbuff,"_whilebreak_%d",Lblnum);
1288 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1290 sprintf (lbuff,"_whilebody_%d",Lblnum++);
1291 $$ = newSymbol(lbuff,NestLevel);
1294 do : DO { /* create and push the continue , break & body Labels */
1295 static int Lblnum = 0 ;
1298 sprintf(lbuff,"_docontinue_%d",Lblnum);
1299 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1301 sprintf (lbuff,"_dobreak_%d",Lblnum);
1302 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1304 sprintf (lbuff,"_dobody_%d",Lblnum++);
1305 $$ = newSymbol (lbuff,NestLevel);
1307 for : FOR { /* create & push continue, break & body labels */
1308 static int Lblnum = 0 ;
1311 sprintf (lbuff,"_forcontinue_%d",Lblnum);
1312 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1314 sprintf (lbuff,"_forbreak_%d",Lblnum);
1315 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1317 sprintf (lbuff,"_forbody_%d",Lblnum);
1318 $$ = newSymbol(lbuff,NestLevel);
1320 sprintf (lbuff,"_forcond_%d",Lblnum++);
1321 STACK_PUSH(forStack,newSymbol(lbuff,NestLevel));
1325 : while '(' expr ')' statement
1328 $$ = createWhile ( $1, STACK_POP(continueStack),
1329 STACK_POP(breakStack), $3, $5 );
1330 $$->lineno = $1->lineDef ;
1333 | do statement WHILE '(' expr ')' ';'
1336 $$ = createDo ( $1 , STACK_POP(continueStack),
1337 STACK_POP(breakStack), $5, $2);
1338 $$->lineno = $1->lineDef ;
1341 | for '(' expr_opt ';' expr_opt ';' expr_opt ')' statement
1345 /* if break or continue statement present
1346 then create a general case loop */
1347 if (STACK_PEEK(continueStack)->isref ||
1348 STACK_PEEK(breakStack)->isref) {
1349 $$ = createFor ($1, STACK_POP(continueStack),
1350 STACK_POP(breakStack) ,
1351 STACK_POP(forStack) ,
1354 $$ = newNode(FOR,$9,NULL);
1355 AST_FOR($$,trueLabel) = $1;
1356 AST_FOR($$,continueLabel) = STACK_POP(continueStack);
1357 AST_FOR($$,falseLabel) = STACK_POP(breakStack);
1358 AST_FOR($$,condLabel) = STACK_POP(forStack) ;
1359 AST_FOR($$,initExpr) = $3;
1360 AST_FOR($$,condExpr) = $5;
1361 AST_FOR($$,loopExpr) = $7;
1374 : GOTO identifier ';' {
1376 $$ = newAst_VALUE(symbolVal($2));
1377 $$ = newNode(GOTO,$$,NULL);
1380 /* make sure continue is in context */
1381 if (STACK_PEEK(continueStack) == NULL) {
1382 werror(E_BREAK_CONTEXT);
1386 $$ = newAst_VALUE(symbolVal(STACK_PEEK(continueStack)));
1387 $$ = newNode(GOTO,$$,NULL);
1388 /* mark the continue label as referenced */
1389 STACK_PEEK(continueStack)->isref = 1;
1393 if (STACK_PEEK(breakStack) == NULL) {
1394 werror(E_BREAK_CONTEXT);
1397 $$ = newAst_VALUE(symbolVal(STACK_PEEK(breakStack)));
1398 $$ = newNode(GOTO,$$,NULL);
1399 STACK_PEEK(breakStack)->isref = 1;
1402 | RETURN ';' { $$ = newNode(RETURN,NULL,NULL) ; }
1403 | RETURN expr ';' { $$ = newNode(RETURN,NULL,$2) ; }
1407 : IDENTIFIER { $$ = newSymbol ($1,NestLevel) ; }