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"
39 extern int yyerror (char *);
41 int NestLevel = 0 ; /* current NestLevel */
42 int stackPtr = 1 ; /* stack pointer */
43 int xstackPtr = 0 ; /* xstack pointer */
45 int blockNo = 0 ; /* sequential block number */
51 char lbuff[1024]; /* local buffer */
53 /* break & continue stacks */
54 STACK_DCL(continueStack ,symbol *,MAX_NEST_LEVEL)
55 STACK_DCL(breakStack ,symbol *,MAX_NEST_LEVEL)
56 STACK_DCL(forStack ,symbol *,MAX_NEST_LEVEL)
57 STACK_DCL(swStk ,ast *,MAX_NEST_LEVEL)
58 STACK_DCL(blockNum,int,MAX_NEST_LEVEL*3)
60 value *cenum = NULL ; /* current enumeration type chain*/
61 bool uselessDecl = TRUE;
67 symbol *sym ; /* symbol table pointer */
68 structdef *sdef; /* structure definition */
69 char yychar[SDCC_NAME_MAX+1];
70 sym_link *lnk ; /* declarator or specifier */
71 int yyint; /* integer value returned */
72 value *val ; /* for integer constant */
73 initList *ilist; /* initial list */
74 const char *yyinline; /* inlined assembler code */
75 ast *asts; /* expression tree */
78 %token <yychar> IDENTIFIER TYPE_NAME
79 %token <val> CONSTANT STRING_LITERAL
81 %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
83 %token <yyint> MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
84 %token <yyint> SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
85 %token <yyint> XOR_ASSIGN OR_ASSIGN
86 %token TYPEDEF EXTERN STATIC AUTO REGISTER CODE EEPROM INTERRUPT SFR AT SBIT
87 %token REENTRANT USING XDATA DATA IDATA PDATA VAR_ARGS CRITICAL NONBANKED BANKED
88 %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID BIT
89 %token STRUCT UNION ENUM ELIPSIS RANGE FAR
90 %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
91 %token NAKED JAVANATIVE OVERLAY
92 %token <yyinline> INLINEASM
93 %token IFX ADDRESS_OF GET_VALUE_AT_ADDRESS SPIL UNSPIL GETHBIT
94 %token BITWISEAND UNARYMINUS IPUSH IPOP PCALL ENDFUNCTION JUMPTABLE
96 %token CAST CALL PARAM NULLOP BLOCK LABEL RECEIVE SEND ARRAYINIT
97 %token DUMMY_READ_VOLATILE ENDCRITICAL SWAP
99 %type <yyint> Interrupt_storage
100 %type <sym> identifier declarator declarator2 enumerator_list enumerator
101 %type <sym> struct_declarator
102 %type <sym> struct_declarator_list struct_declaration struct_declaration_list
103 %type <sym> declaration init_declarator_list init_declarator
104 %type <sym> declaration_list identifier_list parameter_identifier_list
105 %type <sym> declarator2_function_attributes while do for critical
106 %type <lnk> pointer type_specifier_list type_specifier type_name
107 %type <lnk> storage_class_specifier struct_or_union_specifier
108 %type <lnk> declaration_specifiers sfr_reg_bit sfr_attributes type_specifier2
109 %type <lnk> function_attribute function_attributes enum_specifier
110 %type <lnk> abstract_declarator abstract_declarator2 unqualified_pointer
111 %type <val> parameter_type_list parameter_list parameter_declaration opt_assign_expr
112 %type <sdef> stag opt_stag
113 %type <asts> primary_expr
114 %type <asts> postfix_expr unary_expr cast_expr multiplicative_expr
115 %type <asts> additive_expr shift_expr relational_expr equality_expr
116 %type <asts> and_expr exclusive_or_expr inclusive_or_expr logical_or_expr
117 %type <asts> logical_and_expr conditional_expr assignment_expr constant_expr
118 %type <asts> expr argument_expr_list function_definition expr_opt
119 %type <asts> statement_list statement labeled_statement compound_statement
120 %type <asts> expression_statement selection_statement iteration_statement
121 %type <asts> jump_statement function_body else_statement string_literal
122 %type <asts> critical_statement
123 %type <ilist> initializer initializer_list
124 %type <yyint> unary_operator assignment_operator struct_or_union
131 : external_definition
132 | file external_definition
136 : function_definition {
141 && IS_FUNC($1->type))
143 /* The only legal storage classes for
144 * a function prototype (declaration)
145 * are extern and static. extern is the
146 * default. Thus, if this function isn't
147 * explicitly marked static, mark it
151 && IS_SPEC($1->etype)
152 && !SPEC_STAT($1->etype))
154 SPEC_EXTR($1->etype) = 1;
158 allocVariables ($1) ;
159 cleanUpLevel (SymbolTab,1);
164 : declarator function_body { /* function type not specified */
165 /* assume it to be 'int' */
166 addDecl($1,0,newIntLink());
167 $$ = createFunction($1,$2);
169 | declaration_specifiers declarator function_body
171 pointerTypes($2->type,copyLinkChain($1));
173 $$ = createFunction($2,$3);
178 : function_attributes
179 | function_attributes function_attribute { $$ = mergeSpec($1,$2,"function_attribute"); }
184 $$ = newLink(SPECIFIER) ;
185 FUNC_REGBANK($$) = (int) floatFromVal($2);
187 | REENTRANT { $$ = newLink (SPECIFIER);
190 | CRITICAL { $$ = newLink (SPECIFIER);
191 FUNC_ISCRITICAL($$) = 1;
193 | NAKED { $$ = newLink (SPECIFIER);
196 | JAVANATIVE { $$ = newLink (SPECIFIER);
197 FUNC_ISJAVANATIVE($$)=1;
199 | OVERLAY { $$ = newLink (SPECIFIER);
200 FUNC_ISOVERLAY($$)=1;
202 | NONBANKED {$$ = newLink (SPECIFIER);
203 FUNC_NONBANKED($$) = 1;
204 if (FUNC_BANKED($$)) {
205 werror(W_BANKED_WITH_NONBANKED);
208 | BANKED {$$ = newLink (SPECIFIER);
210 if (FUNC_NONBANKED($$)) {
211 werror(W_BANKED_WITH_NONBANKED);
214 werror(W_BANKED_WITH_STATIC);
219 $$ = newLink (SPECIFIER) ;
220 FUNC_INTNO($$) = $1 ;
227 | declaration_list compound_statement
229 werror(E_OLD_STYLE,($1 ? $1->name: "")) ;
235 : identifier { $$ = newAst_VALUE(symbolVal($1)); }
236 | CONSTANT { $$ = newAst_VALUE($1); }
238 | '(' expr ')' { $$ = $2 ; }
242 : STRING_LITERAL { $$ = newAst_VALUE($1); }
247 | postfix_expr '[' expr ']' { $$ = newNode ('[', $1, $3) ; }
248 | postfix_expr '(' ')' { $$ = newNode (CALL,$1,NULL);
249 $$->left->funcName = 1;}
250 | postfix_expr '(' argument_expr_list ')'
252 $$ = newNode (CALL,$1,$3) ; $$->left->funcName = 1;
254 | postfix_expr '.' identifier
256 $3 = newSymbol($3->name,NestLevel);
258 $$ = newNode(PTR_OP,newNode('&',$1,NULL),newAst_VALUE(symbolVal($3)));
259 /* $$ = newNode('.',$1,newAst(EX_VALUE,symbolVal($3))) ; */
261 | postfix_expr PTR_OP identifier
263 $3 = newSymbol($3->name,NestLevel);
265 $$ = newNode(PTR_OP,$1,newAst_VALUE(symbolVal($3)));
267 | postfix_expr INC_OP
268 { $$ = newNode(INC_OP,$1,NULL);}
269 | postfix_expr DEC_OP
270 { $$ = newNode(DEC_OP,$1,NULL); }
275 | assignment_expr ',' argument_expr_list { $$ = newNode(PARAM,$1,$3); }
280 | INC_OP unary_expr { $$ = newNode(INC_OP,NULL,$2); }
281 | DEC_OP unary_expr { $$ = newNode(DEC_OP,NULL,$2); }
282 | unary_operator cast_expr { $$ = newNode($1,$2,NULL) ; }
283 | SIZEOF unary_expr { $$ = newNode(SIZEOF,NULL,$2); }
284 | SIZEOF '(' type_name ')' { $$ = newAst_VALUE(sizeofOp($3)); }
285 | TYPEOF unary_expr { $$ = newNode(TYPEOF,NULL,$2); }
299 | '(' type_name ')' cast_expr { $$ = newNode(CAST,newAst_LINK($2),$4); }
304 | multiplicative_expr '*' cast_expr { $$ = newNode('*',$1,$3);}
305 | multiplicative_expr '/' cast_expr { $$ = newNode('/',$1,$3);}
306 | multiplicative_expr '%' cast_expr { $$ = newNode('%',$1,$3);}
310 : multiplicative_expr
311 | additive_expr '+' multiplicative_expr { $$=newNode('+',$1,$3);}
312 | additive_expr '-' multiplicative_expr { $$=newNode('-',$1,$3);}
317 | shift_expr LEFT_OP additive_expr { $$ = newNode(LEFT_OP,$1,$3); }
318 | shift_expr RIGHT_OP additive_expr { $$ = newNode(RIGHT_OP,$1,$3); }
323 | relational_expr '<' shift_expr {
325 newNode('!',newNode(GE_OP,$1,$3),NULL) :
326 newNode('<', $1,$3));
328 | relational_expr '>' shift_expr {
330 newNode('!',newNode(LE_OP,$1,$3),NULL) :
333 | relational_expr LE_OP shift_expr {
335 newNode('!', newNode('>', $1 , $3 ), NULL) :
336 newNode(LE_OP,$1,$3));
338 | relational_expr GE_OP shift_expr {
340 newNode('!', newNode('<', $1 , $3 ), NULL) :
341 newNode(GE_OP,$1,$3));
347 | equality_expr EQ_OP relational_expr {
349 newNode('!',newNode(NE_OP,$1,$3),NULL) :
350 newNode(EQ_OP,$1,$3));
352 | equality_expr NE_OP relational_expr {
354 newNode('!', newNode(EQ_OP,$1,$3), NULL) :
355 newNode(NE_OP,$1,$3));
361 | and_expr '&' equality_expr { $$ = newNode('&',$1,$3);}
366 | exclusive_or_expr '^' and_expr { $$ = newNode('^',$1,$3);}
371 | inclusive_or_expr '|' exclusive_or_expr { $$ = newNode('|',$1,$3);}
376 | logical_and_expr AND_OP inclusive_or_expr
377 { $$ = newNode(AND_OP,$1,$3);}
382 | logical_or_expr OR_OP logical_and_expr
383 { $$ = newNode(OR_OP,$1,$3); }
388 | logical_or_expr '?' logical_or_expr ':' conditional_expr
390 $$ = newNode(':',$3,$5) ;
391 $$ = newNode('?',$1,$$) ;
397 | unary_expr assignment_operator assignment_expr
402 $$ = newNode($2,$1,$3);
405 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
406 newNode('*',removePreIncDecOps(copyAst($1)),$3));
409 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
410 newNode('/',removePreIncDecOps(copyAst($1)),$3));
413 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
414 newNode('%',removePreIncDecOps(copyAst($1)),$3));
417 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
418 newNode('+',removePreIncDecOps(copyAst($1)),$3));
421 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
422 newNode('-',removePreIncDecOps(copyAst($1)),$3));
425 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
426 newNode(LEFT_OP,removePreIncDecOps(copyAst($1)),$3));
429 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
430 newNode(RIGHT_OP,removePreIncDecOps(copyAst($1)),$3));
433 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
434 newNode('&',removePreIncDecOps(copyAst($1)),$3));
437 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
438 newNode('^',removePreIncDecOps(copyAst($1)),$3));
441 /* $$ = newNode('=',$1,newNode('|',removeIncDecOps(copyAst($1)),$3)); */
442 $$ = newNode('=',removePostIncDecOps(copyAst($1)),
443 newNode('|',removePreIncDecOps(copyAst($1)),$3));
468 | expr ',' assignment_expr { $$ = newNode(',',$1,$3);}
476 : declaration_specifiers ';'
479 werror(W_USELESS_DECL);
483 | declaration_specifiers init_declarator_list ';'
485 /* add the specifier list to the id */
488 for (sym1 = sym = reverseSyms($2);sym != NULL;sym = sym->next) {
489 sym_link *lnk = copyLinkChain($1);
490 /* do the pointer stuff */
491 pointerTypes(sym->type,lnk);
492 addDecl (sym,0,lnk) ;
500 declaration_specifiers
501 : storage_class_specifier { $$ = $1; }
502 | storage_class_specifier declaration_specifiers {
503 /* if the decl $2 is not a specifier */
504 /* find the spec and replace it */
507 while (lnk && !IS_SPEC(lnk->next))
509 lnk->next = mergeSpec($1,lnk->next, "storage_class_specifier declaration_specifiers - skipped");
513 $$ = mergeSpec($1,$2, "storage_class_specifier declaration_specifiers");
515 | type_specifier { $$ = $1; }
516 | type_specifier declaration_specifiers {
517 /* if the decl $2 is not a specifier */
518 /* find the spec and replace it */
521 while (lnk && !IS_SPEC(lnk->next))
523 lnk->next = mergeSpec($1,lnk->next, "type_specifier declaration_specifiers - skipped");
527 $$ = mergeSpec($1,$2, "type_specifier declaration_specifiers");
533 | init_declarator_list ',' init_declarator { $3->next = $1 ; $$ = $3;}
537 : declarator { $1->ival = NULL ; }
538 | declarator '=' initializer { $1->ival = $3 ; }
542 storage_class_specifier
544 $$ = newLink (SPECIFIER) ;
545 SPEC_TYPEDEF($$) = 1 ;
548 $$ = newLink(SPECIFIER);
552 $$ = newLink (SPECIFIER);
556 $$ = newLink (SPECIFIER) ;
557 SPEC_SCLS($$) = S_AUTO ;
560 $$ = newLink (SPECIFIER);
561 SPEC_SCLS($$) = S_REGISTER ;
566 : INTERRUPT { $$ = INTNO_UNSPEC ; }
568 { int intno = (int) floatFromVal($2);
569 if ((intno >= 0) && (intno <= INTNO_MAX))
573 werror(E_INT_BAD_INTNO, intno);
581 | type_specifier2 AT constant_expr
583 /* add this to the storage class specifier */
584 SPEC_ABSA($1) = 1; /* set the absolute addr flag */
585 /* now get the abs addr from value */
586 SPEC_ADDR($1) = (int) floatFromVal(constExprValue($3,TRUE)) ;
592 $$=newLink(SPECIFIER);
593 SPEC_NOUN($$) = V_CHAR ;
596 $$=newLink(SPECIFIER);
597 $$->select.s._short = 1 ;
600 $$=newLink(SPECIFIER);
601 SPEC_NOUN($$) = V_INT ;
604 $$=newLink(SPECIFIER);
608 $$=newLink(SPECIFIER);
609 $$->select.s._signed = 1;
612 $$=newLink(SPECIFIER);
616 $$=newLink(SPECIFIER);
617 SPEC_NOUN($$) = V_VOID ;
620 $$=newLink(SPECIFIER);
624 $$=newLink(SPECIFIER);
625 SPEC_VOLATILE($$) = 1 ;
628 $$=newLink(SPECIFIER);
629 SPEC_NOUN($$) = V_FLOAT;
632 $$ = newLink (SPECIFIER);
633 SPEC_SCLS($$) = S_XDATA ;
636 $$ = newLink (SPECIFIER) ;
637 SPEC_SCLS($$) = S_CODE ;
640 $$ = newLink (SPECIFIER) ;
641 SPEC_SCLS($$) = S_EEPROM ;
644 $$ = newLink (SPECIFIER);
645 SPEC_SCLS($$) = S_DATA ;
648 $$ = newLink (SPECIFIER);
649 SPEC_SCLS($$) = S_IDATA ;
652 $$ = newLink (SPECIFIER);
653 SPEC_SCLS($$) = S_PDATA ;
656 $$=newLink(SPECIFIER);
657 SPEC_NOUN($$) = V_BIT ;
658 SPEC_SCLS($$) = S_BIT ;
663 | struct_or_union_specifier {
676 sym = findSym(TypedefTab,NULL,$1) ;
677 $$ = p = copyLinkChain(sym->type);
678 SPEC_TYPEDEF(getSpec(p)) = 0;
685 $$ = newLink(SPECIFIER) ;
686 SPEC_NOUN($$) = V_SBIT;
687 SPEC_SCLS($$) = S_SBIT;
694 $$ = newLink(SPECIFIER) ;
695 FUNC_REGBANK($$) = 0;
696 SPEC_NOUN($$) = V_CHAR;
697 SPEC_SCLS($$) = S_SFR ;
701 $$ = newLink(SPECIFIER) ;
702 FUNC_REGBANK($$) = 1;
703 SPEC_NOUN($$) = V_CHAR;
704 SPEC_SCLS($$) = S_SFR ;
709 struct_or_union_specifier
710 : struct_or_union opt_stag '{' struct_declaration_list '}'
715 // check for duplicate structure members
716 for (sym=$4; sym; sym=sym->next) {
717 for (dsym=sym->next; dsym; dsym=dsym->next) {
718 if (strcmp(sym->name, dsym->name)==0) {
719 werror(E_DUPLICATE_MEMBER,
720 $1==STRUCT ? "struct" : "union", sym->name);
725 /* Create a structdef */
727 sdef->fields = reverseSyms($4) ; /* link the fields */
728 sdef->size = compStructSize($1,sdef); /* update size of */
730 /* Create the specifier */
731 $$ = newLink (SPECIFIER) ;
732 SPEC_NOUN($$) = V_STRUCT;
733 SPEC_STRUCT($$)= sdef ;
735 | struct_or_union stag
737 $$ = newLink(SPECIFIER) ;
738 SPEC_NOUN($$) = V_STRUCT;
739 SPEC_STRUCT($$) = $2 ;
744 : STRUCT { $$ = STRUCT ; }
745 | UNION { $$ = UNION ; }
750 | { /* synthesize a name add to structtable */
751 $$ = newStruct(genSymName(NestLevel)) ;
752 $$->level = NestLevel ;
753 addSym (StructTab, $$, $$->tag,$$->level,currBlockno, 0);
757 : identifier { /* add name to structure table */
758 $$ = findSymWithBlock (StructTab,$1,currBlockno);
760 $$ = newStruct($1->name) ;
761 $$->level = NestLevel ;
762 addSym (StructTab, $$, $$->tag,$$->level,currBlockno,0);
767 struct_declaration_list
769 | struct_declaration_list struct_declaration
773 /* go to the end of the chain */
774 while (sym->next) sym=sym->next;
782 : type_specifier_list struct_declarator_list ';'
784 /* add this type to all the symbols */
786 for ( sym = $2 ; sym != NULL ; sym = sym->next ) {
788 /* make the symbol one level up */
791 pointerTypes(sym->type,copyLinkChain($1));
793 sym->type = copyLinkChain($1);
794 sym->etype = getSpec(sym->type);
797 addDecl (sym,0,copyLinkChain($1));
798 /* make sure the type is complete and sane */
799 checkTypeSanity(sym->etype, sym->name);
805 struct_declarator_list
807 | struct_declarator_list ',' struct_declarator
816 | ':' constant_expr {
818 $$ = newSymbol (genSymName(NestLevel),NestLevel) ;
819 bitsize= (int) floatFromVal(constExprValue($2,TRUE));
820 if (bitsize > (port->s.int_size * 8)) {
821 bitsize = port->s.int_size * 8;
822 werror(E_BITFLD_SIZE, bitsize);
825 bitsize = BITVAR_PAD;
826 $$->bitVar = bitsize;
828 | declarator ':' constant_expr
831 bitsize= (int) floatFromVal(constExprValue($3,TRUE));
832 if (bitsize > (port->s.int_size * 8)) {
833 bitsize = port->s.int_size * 8;
834 werror(E_BITFLD_SIZE, bitsize);
837 $$ = newSymbol (genSymName(NestLevel),NestLevel) ;
838 $$->bitVar = BITVAR_PAD;
839 werror(W_BITFLD_NAMED);
842 $1->bitVar = bitsize;
847 : ENUM '{' enumerator_list '}' {
851 // check for duplicate enums
852 for (sym=$3; sym; sym=sym->next) {
853 for (dsym=sym->next; dsym; dsym=dsym->next) {
854 if (strcmp(sym->name, dsym->name)==0) {
855 werror(E_DUPLICATE_MEMBER, "enum", sym->name);
861 $$ = copyLinkChain(cenum->type);
868 | ENUM identifier '{' enumerator_list '}' {
871 $2->type = copyLinkChain(cenum->type);
872 $2->etype = getSpec($2->type);
873 /* add this to the enumerator table */
874 if (!(csym=findSym(enumTab,$2,$2->name)) &&
875 (csym && csym->level == $2->level))
876 werror(E_DUPLICATE_TYPEDEF,csym->name);
878 addSym ( enumTab,$2,$2->name,$2->level,$2->block, 0);
880 //allocVariables (reverseSyms($4));
881 $$ = copyLinkChain(cenum->type);
882 SPEC_SCLS(getSpec($$)) = 0 ;
887 /* check the enumerator table */
888 if ((csym = findSym(enumTab,$2,$2->name)))
889 $$ = copyLinkChain(csym->type);
891 $$ = newLink(SPECIFIER) ;
892 SPEC_NOUN($$) = V_INT ;
895 SPEC_SCLS(getSpec($$)) = 0 ;
901 | enumerator_list ',' {
903 | enumerator_list ',' enumerator {
910 : identifier opt_assign_expr
912 /* make the symbol one level up */
914 $1->type = copyLinkChain($2->type);
915 $1->etype= getSpec($1->type);
916 SPEC_ENUM($1->etype) = 1;
918 // do this now, so we can use it for the next enums in the list
924 : '=' constant_expr {
927 val = constExprValue($2,TRUE);
932 SNPRINTF(lbuff, sizeof(lbuff),
933 "%d",(int) floatFromVal(cenum)+1);
934 $$ = cenum = constVal(lbuff);
937 SNPRINTF(lbuff, sizeof(lbuff),
939 $$ = cenum = constVal(lbuff);
945 : declarator2_function_attributes { $$ = $1; }
946 | pointer declarator2_function_attributes
948 addDecl ($2,0,reverseLink($1));
953 declarator2_function_attributes
954 : declarator2 { $$ = $1 ; }
955 | declarator2 function_attribute {
956 if ((! $1) || (! IS_FUNC($1->etype)))
958 // function_attribute is only allowed if declarator2 was
959 // an actual function
965 // copy the functionAttributes (not the args and hasVargs !!)
966 sym_link *funcType=$1->etype;
967 struct value *args=FUNC_ARGS(funcType);
968 unsigned hasVargs=FUNC_HASVARARGS(funcType);
970 memcpy (&funcType->funcAttrs, &$2->funcAttrs,
971 sizeof($2->funcAttrs));
973 FUNC_ARGS(funcType)=args;
974 FUNC_HASVARARGS(funcType)=hasVargs;
977 memset (&$2->funcAttrs, 0,
978 sizeof($2->funcAttrs));
987 | '(' declarator ')' { $$ = $2; }
988 | declarator2 '[' ']'
992 p = newLink (DECLARATOR);
993 DCL_TYPE(p) = ARRAY ;
997 | declarator2 '[' constant_expr ']'
1002 tval = constExprValue($3,TRUE);
1003 /* if it is not a constant then Error */
1004 p = newLink (DECLARATOR);
1005 DCL_TYPE(p) = ARRAY ;
1006 if ( !tval || (SPEC_SCLS(tval->etype) != S_LITERAL)) {
1007 werror(E_CONST_EXPECTED) ;
1008 /* Assume a single item array to limit the cascade */
1009 /* of additional errors. */
1013 DCL_ELEM(p) = (int) floatFromVal(tval) ;
1017 | declarator2 '(' ')' { addDecl ($1,FUNCTION,NULL) ; }
1018 | declarator2 '(' { NestLevel++ ; currBlockno++; } parameter_type_list ')'
1021 addDecl ($1,FUNCTION,NULL) ;
1023 FUNC_HASVARARGS($1->type) = IS_VARG($4);
1024 FUNC_ARGS($1->type) = reverseVal($4);
1026 /* nest level was incremented to take care of the parms */
1030 // if this was a pointer (to a function)
1031 if (IS_PTR($1->type)) {
1032 // move the args and hasVargs to the function
1033 FUNC_ARGS($1->etype)=FUNC_ARGS($1->type);
1034 FUNC_HASVARARGS($1->etype)=FUNC_HASVARARGS($1->type);
1035 memset (&$1->type->funcAttrs, 0,
1036 sizeof($1->type->funcAttrs));
1037 // remove the symbol args (if any)
1038 cleanUpLevel(SymbolTab,NestLevel+1);
1043 | declarator2 '(' parameter_identifier_list ')'
1045 werror(E_OLD_STYLE,$1->name) ;
1047 /* assume it returns an int */
1048 $1->type = $1->etype = newIntLink();
1054 : unqualified_pointer { $$ = $1 ;}
1055 | unqualified_pointer type_specifier_list
1060 DCL_PTR_CONST($1) = SPEC_CONST($2);
1061 DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
1064 werror (W_PTR_TYPE_INVALID);
1066 | unqualified_pointer pointer
1070 DCL_TYPE($2)=port->unqualified_pointer;
1072 | unqualified_pointer type_specifier_list pointer
1075 if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
1076 DCL_PTR_CONST($1) = SPEC_CONST($2);
1077 DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
1078 switch (SPEC_SCLS($2)) {
1080 DCL_TYPE($3) = FPOINTER;
1083 DCL_TYPE($3) = IPOINTER ;
1086 DCL_TYPE($3) = PPOINTER ;
1089 DCL_TYPE($3) = POINTER ;
1092 DCL_TYPE($3) = CPOINTER ;
1095 DCL_TYPE($3) = EEPPOINTER;
1098 // this could be just "constant"
1099 // werror(W_PTR_TYPE_INVALID);
1104 werror (W_PTR_TYPE_INVALID);
1112 $$ = newLink(DECLARATOR);
1113 DCL_TYPE($$)=UPOINTER;
1119 //| type_specifier_list type_specifier { $$ = mergeSpec ($1,$2, "type_specifier_list"); }
1120 | type_specifier_list type_specifier {
1121 /* if the decl $2 is not a specifier */
1122 /* find the spec and replace it */
1123 if ( !IS_SPEC($2)) {
1124 sym_link *lnk = $2 ;
1125 while (lnk && !IS_SPEC(lnk->next))
1127 lnk->next = mergeSpec($1,lnk->next, "type_specifier_list type_specifier skipped");
1131 $$ = mergeSpec($1,$2, "type_specifier_list type_specifier");
1135 parameter_identifier_list
1137 | identifier_list ',' ELIPSIS
1142 | identifier_list ',' identifier
1151 | parameter_list ',' VAR_ARGS { $1->vArgs = 1;}
1155 : parameter_declaration
1156 | parameter_list ',' parameter_declaration
1163 parameter_declaration
1164 : type_specifier_list declarator
1167 pointerTypes($2->type,$1);
1169 for (loop=$2;loop;loop->_isparm=1,loop=loop->next);
1176 $$->etype = getSpec($$->type);
1181 : type_specifier_list { $$ = $1 ;}
1182 | type_specifier_list abstract_declarator
1184 /* go to the end of the list */
1186 pointerTypes($2,$1);
1187 for ( p = $2 ; p && p->next ; p=p->next);
1189 werror(E_SYNTAX_ERROR, yytext);
1198 : pointer { $$ = reverseLink($1); }
1199 | abstract_declarator2
1200 | pointer abstract_declarator2 { $1 = reverseLink($1); $1->next = $2 ; $$ = $1;
1201 if (IS_PTR($1) && IS_FUNC($2))
1202 DCL_TYPE($1) = CPOINTER;
1206 abstract_declarator2
1207 : '(' abstract_declarator ')' { $$ = $2 ; }
1209 $$ = newLink (DECLARATOR);
1210 DCL_TYPE($$) = ARRAY ;
1213 | '[' constant_expr ']' {
1215 $$ = newLink (DECLARATOR);
1216 DCL_TYPE($$) = ARRAY ;
1217 DCL_ELEM($$) = (int) floatFromVal(val = constExprValue($2,TRUE));
1219 | abstract_declarator2 '[' ']' {
1220 $$ = newLink (DECLARATOR);
1221 DCL_TYPE($$) = ARRAY ;
1225 | abstract_declarator2 '[' constant_expr ']'
1228 $$ = newLink (DECLARATOR);
1229 DCL_TYPE($$) = ARRAY ;
1230 DCL_ELEM($$) = (int) floatFromVal(val = constExprValue($3,TRUE));
1233 | '(' ')' { $$ = NULL;}
1234 | '(' parameter_type_list ')' { $$ = NULL;}
1235 | abstract_declarator2 '(' ')' {
1236 // $1 must be a pointer to a function
1237 sym_link *p=newLink(DECLARATOR);
1238 DCL_TYPE(p) = FUNCTION;
1240 // ((void (code *) ()) 0) ()
1241 $1=newLink(DECLARATOR);
1242 DCL_TYPE($1)=CPOINTER;
1247 | abstract_declarator2 '(' { NestLevel++ ; currBlockno++; } parameter_type_list ')' {
1248 sym_link *p=newLink(DECLARATOR);
1249 DCL_TYPE(p) = FUNCTION;
1251 FUNC_HASVARARGS(p) = IS_VARG($4);
1252 FUNC_ARGS(p) = reverseVal($4);
1254 /* nest level was incremented to take care of the parms */
1260 // remove the symbol args (if any)
1261 cleanUpLevel(SymbolTab,NestLevel+1);
1266 : assignment_expr { $$ = newiList(INIT_NODE,$1); }
1267 | '{' initializer_list '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
1268 | '{' initializer_list ',' '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
1273 | initializer_list ',' initializer { $3->next = $1; $$ = $3; }
1278 | compound_statement
1279 | expression_statement
1280 | selection_statement
1281 | iteration_statement
1283 | critical_statement
1285 ast *ex = newNode(INLINEASM,NULL,NULL);
1286 ex->values.inlineasm = strdup($1);
1294 STACK_PUSH(continueStack,NULL);
1295 STACK_PUSH(breakStack,NULL);
1301 : critical statement {
1302 STACK_POP(breakStack);
1303 STACK_POP(continueStack);
1305 $$ = newNode(CRITICAL,$2,NULL);
1310 // : identifier ':' statement { $$ = createLabel($1,$3); }
1311 : identifier ':' { $$ = createLabel($1,NULL); }
1312 | CASE constant_expr ':' statement { $$ = createCase(STACK_PEEK(swStk),$2,$4); }
1313 | DEFAULT ':' statement { $$ = createDefault(STACK_PEEK(swStk),$3); }
1316 start_block : '{' { STACK_PUSH(blockNum,currBlockno); currBlockno = ++blockNo ; }
1319 end_block : '}' { currBlockno = STACK_POP(blockNum); }
1323 : start_block end_block { $$ = createBlock(NULL,NULL); }
1324 | start_block statement_list end_block { $$ = createBlock(NULL,$2) ; }
1326 declaration_list { addSymChain($2); }
1327 end_block { $$ = createBlock($2,NULL) ; }
1329 declaration_list { addSymChain ($2); }
1331 end_block {$$ = createBlock($2,$4) ; }
1332 | error ';' { $$ = NULL ; }
1338 /* if this is typedef declare it immediately */
1339 if ( $1 && IS_TYPEDEF($1->etype)) {
1340 allocVariables ($1);
1347 | declaration_list declaration
1351 /* if this is a typedef */
1352 if ($2 && IS_TYPEDEF($2->etype)) {
1353 allocVariables ($2);
1357 /* get to the end of the previous decl */
1372 | statement_list statement { $$ = newNode(NULLOP,$1,$2) ;}
1375 expression_statement
1381 : ELSE statement { $$ = $2 ; }
1387 : IF '(' expr ')' statement else_statement { noLineno++ ; $$ = createIf ($3, $5, $6 ); noLineno--;}
1388 | SWITCH '(' expr ')' {
1390 static int swLabel = 0 ;
1392 /* create a node for expression */
1393 ex = newNode(SWITCH,$3,NULL);
1394 STACK_PUSH(swStk,ex); /* save it in the stack */
1395 ex->values.switchVals.swNum = swLabel ;
1397 /* now create the label */
1398 SNPRINTF(lbuff, sizeof(lbuff),
1399 "_swBrk_%d",swLabel++);
1400 $<sym>$ = newSymbol(lbuff,NestLevel);
1401 /* put label in the break stack */
1402 STACK_PUSH(breakStack,$<sym>$);
1405 /* get back the switch form the stack */
1406 $$ = STACK_POP(swStk) ;
1407 $$->right = newNode (NULLOP,$6,createLabel($<sym>5,NULL));
1408 STACK_POP(breakStack);
1412 while : WHILE { /* create and push the continue , break & body labels */
1413 static int Lblnum = 0 ;
1415 SNPRINTF (lbuff, sizeof(lbuff), "_whilecontinue_%d",Lblnum);
1416 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1418 SNPRINTF (lbuff, sizeof(lbuff), "_whilebreak_%d",Lblnum);
1419 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1421 SNPRINTF (lbuff, sizeof(lbuff), "_whilebody_%d",Lblnum++);
1422 $$ = newSymbol(lbuff,NestLevel);
1426 do : DO { /* create and push the continue , break & body Labels */
1427 static int Lblnum = 0 ;
1430 SNPRINTF(lbuff, sizeof(lbuff), "_docontinue_%d",Lblnum);
1431 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1433 SNPRINTF(lbuff, sizeof(lbuff), "_dobreak_%d",Lblnum);
1434 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1436 SNPRINTF(lbuff, sizeof(lbuff), "_dobody_%d",Lblnum++);
1437 $$ = newSymbol (lbuff,NestLevel);
1441 for : FOR { /* create & push continue, break & body labels */
1442 static int Lblnum = 0 ;
1445 SNPRINTF(lbuff, sizeof(lbuff), "_forcontinue_%d",Lblnum);
1446 STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
1448 SNPRINTF(lbuff, sizeof(lbuff), "_forbreak_%d",Lblnum);
1449 STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
1451 SNPRINTF(lbuff, sizeof(lbuff), "_forbody_%d",Lblnum);
1452 $$ = newSymbol(lbuff,NestLevel);
1454 SNPRINTF(lbuff, sizeof(lbuff), "_forcond_%d",Lblnum++);
1455 STACK_PUSH(forStack,newSymbol(lbuff,NestLevel));
1460 : while '(' expr ')' statement
1463 $$ = createWhile ( $1, STACK_POP(continueStack),
1464 STACK_POP(breakStack), $3, $5 );
1465 $$->lineno = $1->lineDef ;
1468 | do statement WHILE '(' expr ')' ';'
1471 $$ = createDo ( $1 , STACK_POP(continueStack),
1472 STACK_POP(breakStack), $5, $2);
1473 $$->lineno = $1->lineDef ;
1476 | for '(' expr_opt ';' expr_opt ';' expr_opt ')' statement
1480 /* if break or continue statement present
1481 then create a general case loop */
1482 if (STACK_PEEK(continueStack)->isref ||
1483 STACK_PEEK(breakStack)->isref) {
1484 $$ = createFor ($1, STACK_POP(continueStack),
1485 STACK_POP(breakStack) ,
1486 STACK_POP(forStack) ,
1489 $$ = newNode(FOR,$9,NULL);
1490 AST_FOR($$,trueLabel) = $1;
1491 AST_FOR($$,continueLabel) = STACK_POP(continueStack);
1492 AST_FOR($$,falseLabel) = STACK_POP(breakStack);
1493 AST_FOR($$,condLabel) = STACK_POP(forStack) ;
1494 AST_FOR($$,initExpr) = $3;
1495 AST_FOR($$,condExpr) = $5;
1496 AST_FOR($$,loopExpr) = $7;
1509 : GOTO identifier ';' {
1511 $$ = newAst_VALUE(symbolVal($2));
1512 $$ = newNode(GOTO,$$,NULL);
1515 /* make sure continue is in context */
1516 if (STACK_PEEK(continueStack) == NULL) {
1517 werror(E_BREAK_CONTEXT);
1521 $$ = newAst_VALUE(symbolVal(STACK_PEEK(continueStack)));
1522 $$ = newNode(GOTO,$$,NULL);
1523 /* mark the continue label as referenced */
1524 STACK_PEEK(continueStack)->isref = 1;
1528 if (STACK_PEEK(breakStack) == NULL) {
1529 werror(E_BREAK_CONTEXT);
1532 $$ = newAst_VALUE(symbolVal(STACK_PEEK(breakStack)));
1533 $$ = newNode(GOTO,$$,NULL);
1534 STACK_PEEK(breakStack)->isref = 1;
1539 werror(E_INVALID_CRITICAL);
1542 $$ = newNode(RETURN,NULL,NULL);
1547 werror(E_INVALID_CRITICAL);
1550 $$ = newNode(RETURN,NULL,$2);
1556 : IDENTIFIER { $$ = newSymbol ($1,NestLevel) ; }