PageRenderTime 55ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/branches/stack-compact/sdcc/src/SDCC.y

#
Happy | 1844 lines | 1681 code | 163 blank | 0 comment | 0 complexity | dd7fce26dfacc0703bdc65286bab506d MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, GPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. /*-----------------------------------------------------------------------
  2. SDCC.y - parser definition file for sdcc :
  3. Written By : Sandeep Dutta . sandeep.dutta@usa.net (1997)
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. In other words, you are welcome to use, share and improve this program.
  16. You are forbidden to forbid anyone else to use, share and improve
  17. what you give them. Help stamp out software-hoarding!
  18. -------------------------------------------------------------------------*/
  19. %{
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <string.h>
  23. #include "SDCCglobl.h"
  24. #include "SDCCsymt.h"
  25. #include "SDCChasht.h"
  26. #include "SDCCval.h"
  27. #include "SDCCmem.h"
  28. #include "SDCCast.h"
  29. #include "port.h"
  30. #include "newalloc.h"
  31. #include "SDCCerr.h"
  32. #include "SDCCutil.h"
  33. #include "SDCCbtree.h"
  34. extern int yyerror (char *);
  35. extern FILE *yyin;
  36. int NestLevel = 0 ; /* current NestLevel */
  37. int stackPtr = 1 ; /* stack pointer */
  38. int xstackPtr = 0 ; /* xstack pointer */
  39. int reentrant = 0 ;
  40. int blockNo = 0 ; /* sequential block number */
  41. int currBlockno=0 ;
  42. int inCritical= 0 ;
  43. int seqPointNo= 1 ; /* sequence point number */
  44. int ignoreTypedefType=0;
  45. extern int yylex();
  46. int yyparse(void);
  47. extern int noLineno ;
  48. char lbuff[1024]; /* local buffer */
  49. /* break & continue stacks */
  50. STACK_DCL(continueStack ,symbol *,MAX_NEST_LEVEL)
  51. STACK_DCL(breakStack ,symbol *,MAX_NEST_LEVEL)
  52. STACK_DCL(forStack ,symbol *,MAX_NEST_LEVEL)
  53. STACK_DCL(swStk ,ast *,MAX_NEST_LEVEL)
  54. STACK_DCL(blockNum,int,MAX_NEST_LEVEL*3)
  55. value *cenum = NULL ; /* current enumeration type chain*/
  56. bool uselessDecl = TRUE;
  57. #define YYDEBUG 1
  58. %}
  59. %expect 6
  60. %union {
  61. symbol *sym ; /* symbol table pointer */
  62. structdef *sdef; /* structure definition */
  63. char yychar[SDCC_NAME_MAX+1];
  64. sym_link *lnk ; /* declarator or specifier */
  65. int yyint; /* integer value returned */
  66. value *val ; /* for integer constant */
  67. initList *ilist; /* initial list */
  68. designation*dsgn; /* designator */
  69. const char *yyinline; /* inlined assembler code */
  70. ast *asts; /* expression tree */
  71. }
  72. %token <yychar> IDENTIFIER TYPE_NAME ADDRSPACE_NAME
  73. %token <val> CONSTANT STRING_LITERAL
  74. %token SIZEOF TYPEOF OFFSETOF
  75. %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
  76. %token AND_OP OR_OP
  77. %token <yyint> MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
  78. %token <yyint> SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
  79. %token <yyint> XOR_ASSIGN OR_ASSIGN
  80. %token TYPEDEF EXTERN STATIC AUTO REGISTER CODE EEPROM INTERRUPT SFR SFR16 SFR32 ADDRESSMOD
  81. %token AT SBIT REENTRANT USING XDATA DATA IDATA PDATA VAR_ARGS CRITICAL
  82. %token NONBANKED BANKED SHADOWREGS SD_WPARAM
  83. %token SD_BOOL SD_CHAR SD_SHORT SD_INT SD_LONG SIGNED UNSIGNED SD_FLOAT DOUBLE FIXED16X16 SD_CONST VOLATILE SD_VOID BIT
  84. %token STRUCT UNION ENUM RANGE SD_FAR
  85. %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
  86. %token NAKED JAVANATIVE OVERLAY
  87. %token <yyinline> INLINEASM
  88. %token IFX ADDRESS_OF GET_VALUE_AT_ADDRESS SPIL UNSPIL GETHBIT GETABIT GETBYTE GETWORD
  89. %token BITWISEAND UNARYMINUS IPUSH IPOP PCALL ENDFUNCTION JUMPTABLE
  90. %token RRC RLC
  91. %token CAST CALL PARAM NULLOP BLOCK LABEL RECEIVE SEND ARRAYINIT
  92. %token DUMMY_READ_VOLATILE ENDCRITICAL SWAP INLINE NORETURN RESTRICT SMALLC
  93. %type <yyint> Interrupt_storage
  94. %type <sym> identifier declarator declarator2 declarator3 enumerator_list enumerator
  95. %type <sym> struct_declarator function_declarator function_declarator2
  96. %type <sym> struct_declarator_list struct_declaration struct_declaration_list
  97. %type <sym> declaration init_declarator_list init_declarator
  98. %type <sym> declaration_list identifier_list
  99. %type <sym> declarator2_function_attributes while do for critical
  100. %type <sym> addressmod
  101. %type <lnk> pointer type_specifier_list type_specifier_list_ type_specifier type_name
  102. %type <lnk> storage_class_specifier struct_or_union_specifier function_specifier
  103. %type <lnk> declaration_specifiers declaration_specifiers_ sfr_reg_bit sfr_attributes
  104. %type <lnk> function_attribute function_attributes enum_specifier
  105. %type <lnk> abstract_declarator abstract_declarator2 unqualified_pointer
  106. %type <val> parameter_type_list parameter_list parameter_declaration opt_assign_expr
  107. %type <sdef> stag opt_stag
  108. %type <asts> primary_expr
  109. %type <asts> postfix_expr unary_expr offsetof_member_designator cast_expr multiplicative_expr
  110. %type <asts> additive_expr shift_expr relational_expr equality_expr
  111. %type <asts> and_expr exclusive_or_expr inclusive_or_expr logical_or_expr
  112. %type <asts> logical_and_expr conditional_expr assignment_expr constant_expr
  113. %type <asts> expr argument_expr_list function_definition expr_opt
  114. %type <asts> statement_list statement labeled_statement compound_statement
  115. %type <asts> expression_statement selection_statement iteration_statement
  116. %type <asts> jump_statement function_body else_statement string_literal
  117. %type <asts> critical_statement
  118. %type <dsgn> designator designator_list designation designation_opt
  119. %type <ilist> initializer initializer_list
  120. %type <yyint> unary_operator assignment_operator struct_or_union
  121. %start file
  122. %%
  123. file
  124. : /* empty */
  125. {
  126. if (!options.lessPedantic)
  127. werror(W_EMPTY_SOURCE_FILE);
  128. }
  129. | program
  130. ;
  131. program
  132. : external_definition
  133. | program external_definition
  134. ;
  135. external_definition
  136. : function_definition
  137. {
  138. //blockNo = 0;
  139. //btree_init ();
  140. }
  141. | declaration
  142. {
  143. ignoreTypedefType = 0;
  144. if ($1 && $1->type && IS_FUNC($1->type))
  145. {
  146. /* The only legal storage classes for
  147. * a function prototype (declaration)
  148. * are extern and static. extern is the
  149. * default. Thus, if this function isn't
  150. * explicitly marked static, mark it
  151. * extern.
  152. */
  153. if ($1->etype && IS_SPEC($1->etype) && !SPEC_STAT($1->etype))
  154. {
  155. SPEC_EXTR($1->etype) = 1;
  156. }
  157. }
  158. addSymChain (&$1);
  159. allocVariables ($1);
  160. cleanUpLevel (SymbolTab, 1);
  161. }
  162. | addressmod
  163. ;
  164. function_definition
  165. : function_declarator function_body { /* function type not specified */
  166. /* assume it to be 'int' */
  167. addDecl($1,0,newIntLink());
  168. $$ = createFunction($1,$2);
  169. }
  170. | declaration_specifiers function_declarator function_body
  171. {
  172. pointerTypes($2->type,copyLinkChain($1));
  173. addDecl($2,0,$1);
  174. $$ = createFunction($2,$3);
  175. }
  176. ;
  177. function_attribute
  178. : function_attributes
  179. | function_attributes function_attribute { $$ = mergeSpec($1,$2,"function_attribute"); }
  180. ;
  181. function_attributes
  182. : USING constant_expr {
  183. $$ = newLink(SPECIFIER) ;
  184. FUNC_REGBANK($$) = (int) ulFromVal(constExprValue($2,TRUE));
  185. }
  186. | REENTRANT { $$ = newLink (SPECIFIER);
  187. FUNC_ISREENT($$)=1;
  188. }
  189. | CRITICAL { $$ = newLink (SPECIFIER);
  190. FUNC_ISCRITICAL($$) = 1;
  191. }
  192. | NAKED { $$ = newLink (SPECIFIER);
  193. FUNC_ISNAKED($$)=1;
  194. }
  195. | JAVANATIVE { $$ = newLink (SPECIFIER);
  196. FUNC_ISJAVANATIVE($$)=1;
  197. }
  198. | OVERLAY { $$ = newLink (SPECIFIER);
  199. FUNC_ISOVERLAY($$)=1;
  200. }
  201. | NONBANKED {$$ = newLink (SPECIFIER);
  202. FUNC_NONBANKED($$) = 1;
  203. if (FUNC_BANKED($$)) {
  204. werror(W_BANKED_WITH_NONBANKED);
  205. }
  206. }
  207. | SHADOWREGS {$$ = newLink (SPECIFIER);
  208. FUNC_ISSHADOWREGS($$) = 1;
  209. }
  210. | SD_WPARAM {$$ = newLink (SPECIFIER);
  211. FUNC_ISWPARAM($$) = 1;
  212. }
  213. | BANKED {$$ = newLink (SPECIFIER);
  214. FUNC_BANKED($$) = 1;
  215. if (FUNC_NONBANKED($$)) {
  216. werror(W_BANKED_WITH_NONBANKED);
  217. }
  218. }
  219. | Interrupt_storage
  220. {
  221. $$ = newLink (SPECIFIER) ;
  222. FUNC_INTNO($$) = $1 ;
  223. FUNC_ISISR($$) = 1;
  224. }
  225. | SMALLC { $$ = newLink (SPECIFIER);
  226. FUNC_ISSMALLC($$)=1;
  227. }
  228. ;
  229. function_body
  230. : compound_statement
  231. | declaration_list compound_statement
  232. {
  233. werror (E_OLD_STYLE, ($1 ? $1->name: "")) ;
  234. exit (1);
  235. }
  236. ;
  237. offsetof_member_designator
  238. : identifier { $$ = newAst_VALUE (symbolVal ($1)); }
  239. | offsetof_member_designator '.' { ignoreTypedefType = 1; } identifier
  240. {
  241. ignoreTypedefType = 0;
  242. $4 = newSymbol ($4->name, NestLevel);
  243. $4->implicit = 1;
  244. $$ = newNode ('.', $1, newAst_VALUE (symbolVal ($4))) ;
  245. }
  246. | offsetof_member_designator '[' expr ']'
  247. {
  248. $$ = newNode ('[', $1, $3);
  249. }
  250. ;
  251. primary_expr
  252. : identifier { $$ = newAst_VALUE (symbolVal ($1)); }
  253. | CONSTANT { $$ = newAst_VALUE ($1); }
  254. | string_literal
  255. | '(' expr ')' { $$ = $2; }
  256. ;
  257. string_literal
  258. : STRING_LITERAL { $$ = newAst_VALUE($1); }
  259. ;
  260. postfix_expr
  261. : primary_expr
  262. | postfix_expr '[' expr ']' { $$ = newNode ('[', $1, $3) ; }
  263. | postfix_expr '(' ')' { $$ = newNode (CALL,$1,NULL);
  264. $$->left->funcName = 1;}
  265. | postfix_expr '(' argument_expr_list ')'
  266. {
  267. $$ = newNode (CALL,$1,$3) ; $$->left->funcName = 1;
  268. }
  269. | postfix_expr '.' { ignoreTypedefType = 1; } identifier
  270. {
  271. ignoreTypedefType = 0;
  272. $4 = newSymbol($4->name,NestLevel);
  273. $4->implicit = 1;
  274. $$ = newNode(PTR_OP,newNode('&',$1,NULL),newAst_VALUE(symbolVal($4)));
  275. }
  276. | postfix_expr PTR_OP { ignoreTypedefType = 1; } identifier
  277. {
  278. ignoreTypedefType = 0;
  279. $4 = newSymbol($4->name,NestLevel);
  280. $4->implicit = 1;
  281. $$ = newNode(PTR_OP,$1,newAst_VALUE(symbolVal($4)));
  282. }
  283. | postfix_expr INC_OP
  284. { $$ = newNode(INC_OP,$1,NULL);}
  285. | postfix_expr DEC_OP
  286. { $$ = newNode(DEC_OP,$1,NULL); }
  287. ;
  288. argument_expr_list
  289. : assignment_expr
  290. | assignment_expr ',' argument_expr_list { $$ = newNode(PARAM,$1,$3); }
  291. ;
  292. unary_expr
  293. : postfix_expr
  294. | INC_OP unary_expr { $$ = newNode (INC_OP, NULL, $2); }
  295. | DEC_OP unary_expr { $$ = newNode (DEC_OP, NULL, $2); }
  296. | unary_operator cast_expr { $$ = newNode ($1, $2, NULL); }
  297. | SIZEOF unary_expr { $$ = newNode (SIZEOF, NULL, $2); }
  298. | SIZEOF '(' type_name ')' { $$ = newAst_VALUE (sizeofOp ($3)); }
  299. | TYPEOF unary_expr { $$ = newNode (TYPEOF, NULL, $2); }
  300. | OFFSETOF '(' type_name ',' offsetof_member_designator ')' { $$ = offsetofOp($3, $5); }
  301. ;
  302. unary_operator
  303. : '&' { $$ = '&' ;}
  304. | '*' { $$ = '*' ;}
  305. | '+' { $$ = '+' ;}
  306. | '-' { $$ = '-' ;}
  307. | '~' { $$ = '~' ;}
  308. | '!' { $$ = '!' ;}
  309. ;
  310. cast_expr
  311. : unary_expr
  312. | '(' type_name ')' cast_expr { $$ = newNode(CAST,newAst_LINK($2),$4); }
  313. ;
  314. multiplicative_expr
  315. : cast_expr
  316. | multiplicative_expr '*' cast_expr { $$ = newNode('*',$1,$3);}
  317. | multiplicative_expr '/' cast_expr { $$ = newNode('/',$1,$3);}
  318. | multiplicative_expr '%' cast_expr { $$ = newNode('%',$1,$3);}
  319. ;
  320. additive_expr
  321. : multiplicative_expr
  322. | additive_expr '+' multiplicative_expr { $$=newNode('+',$1,$3);}
  323. | additive_expr '-' multiplicative_expr { $$=newNode('-',$1,$3);}
  324. ;
  325. shift_expr
  326. : additive_expr
  327. | shift_expr LEFT_OP additive_expr { $$ = newNode(LEFT_OP,$1,$3); }
  328. | shift_expr RIGHT_OP additive_expr { $$ = newNode(RIGHT_OP,$1,$3); }
  329. ;
  330. relational_expr
  331. : shift_expr
  332. | relational_expr '<' shift_expr { $$ = newNode('<', $1,$3);}
  333. | relational_expr '>' shift_expr { $$ = newNode('>', $1,$3);}
  334. | relational_expr LE_OP shift_expr { $$ = newNode(LE_OP,$1,$3);}
  335. | relational_expr GE_OP shift_expr { $$ = newNode(GE_OP,$1,$3);}
  336. ;
  337. equality_expr
  338. : relational_expr
  339. | equality_expr EQ_OP relational_expr { $$ = newNode(EQ_OP,$1,$3);}
  340. | equality_expr NE_OP relational_expr { $$ = newNode(NE_OP,$1,$3);}
  341. ;
  342. and_expr
  343. : equality_expr
  344. | and_expr '&' equality_expr { $$ = newNode('&',$1,$3);}
  345. ;
  346. exclusive_or_expr
  347. : and_expr
  348. | exclusive_or_expr '^' and_expr { $$ = newNode('^',$1,$3);}
  349. ;
  350. inclusive_or_expr
  351. : exclusive_or_expr
  352. | inclusive_or_expr '|' exclusive_or_expr { $$ = newNode('|',$1,$3);}
  353. ;
  354. logical_and_expr
  355. : inclusive_or_expr
  356. | logical_and_expr AND_OP { seqPointNo++;} inclusive_or_expr
  357. { $$ = newNode(AND_OP,$1,$4);}
  358. ;
  359. logical_or_expr
  360. : logical_and_expr
  361. | logical_or_expr OR_OP { seqPointNo++;} logical_and_expr
  362. { $$ = newNode(OR_OP,$1,$4); }
  363. ;
  364. conditional_expr
  365. : logical_or_expr
  366. | logical_or_expr '?' { seqPointNo++;} logical_or_expr ':' conditional_expr
  367. {
  368. $$ = newNode(':',$4,$6) ;
  369. $$ = newNode('?',$1,$$) ;
  370. }
  371. ;
  372. assignment_expr
  373. : conditional_expr
  374. | cast_expr assignment_operator assignment_expr
  375. {
  376. switch ($2) {
  377. case '=':
  378. $$ = newNode($2,$1,$3);
  379. break;
  380. case MUL_ASSIGN:
  381. $$ = createRMW($1, '*', $3);
  382. break;
  383. case DIV_ASSIGN:
  384. $$ = createRMW($1, '/', $3);
  385. break;
  386. case MOD_ASSIGN:
  387. $$ = createRMW($1, '%', $3);
  388. break;
  389. case ADD_ASSIGN:
  390. $$ = createRMW($1, '+', $3);
  391. break;
  392. case SUB_ASSIGN:
  393. $$ = createRMW($1, '-', $3);
  394. break;
  395. case LEFT_ASSIGN:
  396. $$ = createRMW($1, LEFT_OP, $3);
  397. break;
  398. case RIGHT_ASSIGN:
  399. $$ = createRMW($1, RIGHT_OP, $3);
  400. break;
  401. case AND_ASSIGN:
  402. $$ = createRMW($1, '&', $3);
  403. break;
  404. case XOR_ASSIGN:
  405. $$ = createRMW($1, '^', $3);
  406. break;
  407. case OR_ASSIGN:
  408. $$ = createRMW($1, '|', $3);
  409. break;
  410. default :
  411. $$ = NULL;
  412. }
  413. }
  414. ;
  415. assignment_operator
  416. : '=' { $$ = '=' ;}
  417. | MUL_ASSIGN
  418. | DIV_ASSIGN
  419. | MOD_ASSIGN
  420. | ADD_ASSIGN
  421. | SUB_ASSIGN
  422. | LEFT_ASSIGN
  423. | RIGHT_ASSIGN
  424. | AND_ASSIGN
  425. | XOR_ASSIGN
  426. | OR_ASSIGN
  427. ;
  428. expr
  429. : assignment_expr
  430. | expr ',' { seqPointNo++;} assignment_expr { $$ = newNode(',',$1,$4);}
  431. ;
  432. constant_expr
  433. : conditional_expr
  434. ;
  435. declaration
  436. : declaration_specifiers ';'
  437. {
  438. if (uselessDecl)
  439. werror(W_USELESS_DECL);
  440. uselessDecl = TRUE;
  441. $$ = NULL ;
  442. }
  443. | declaration_specifiers init_declarator_list ';'
  444. {
  445. /* add the specifier list to the id */
  446. symbol *sym , *sym1;
  447. for (sym1 = sym = reverseSyms($2);sym != NULL;sym = sym->next) {
  448. sym_link *lnk = copyLinkChain($1);
  449. /* do the pointer stuff */
  450. pointerTypes(sym->type,lnk);
  451. addDecl (sym,0,lnk) ;
  452. }
  453. uselessDecl = TRUE;
  454. $$ = sym1 ;
  455. }
  456. ;
  457. declaration_specifiers : declaration_specifiers_ { $$ = finalizeSpec($1); } ;
  458. declaration_specifiers_
  459. : storage_class_specifier { $$ = $1; }
  460. | storage_class_specifier declaration_specifiers_ {
  461. /* if the decl $2 is not a specifier */
  462. /* find the spec and replace it */
  463. $$ = mergeDeclSpec($1, $2, "storage_class_specifier declaration_specifiers - skipped");
  464. }
  465. | type_specifier { $$ = $1; }
  466. | type_specifier declaration_specifiers_ {
  467. /* if the decl $2 is not a specifier */
  468. /* find the spec and replace it */
  469. $$ = mergeDeclSpec($1, $2, "type_specifier declaration_specifiers - skipped");
  470. }
  471. | function_specifier { $$ = $1; }
  472. | function_specifier declaration_specifiers_ {
  473. /* if the decl $2 is not a specifier */
  474. /* find the spec and replace it */
  475. $$ = mergeDeclSpec($1, $2, "function_specifier declaration_specifiers - skipped");
  476. }
  477. ;
  478. init_declarator_list
  479. : init_declarator
  480. | init_declarator_list ',' init_declarator { $3->next = $1 ; $$ = $3;}
  481. ;
  482. init_declarator
  483. : declarator { $1->ival = NULL ; }
  484. | declarator '=' initializer { $1->ival = $3 ; }
  485. ;
  486. designation_opt
  487. : { $$ = NULL; }
  488. | designation
  489. ;
  490. designation
  491. : designator_list '=' { $$ = revDesignation($1); }
  492. ;
  493. designator_list
  494. : designator
  495. | designator_list designator { $2->next = $1; $$ = $2; }
  496. ;
  497. designator
  498. : '[' constant_expr ']'
  499. {
  500. value *tval;
  501. int elemno;
  502. tval = constExprValue($2, TRUE);
  503. /* if it is not a constant then Error */
  504. if (!tval || (SPEC_SCLS(tval->etype) != S_LITERAL))
  505. {
  506. werror (E_CONST_EXPECTED);
  507. elemno = 0; /* arbitrary fixup */
  508. }
  509. else
  510. {
  511. if ((elemno = (int) ulFromVal(tval)) < 0)
  512. {
  513. werror (E_BAD_DESIGNATOR);
  514. elemno = 0; /* arbitrary fixup */
  515. }
  516. }
  517. $$ = newDesignation(DESIGNATOR_ARRAY, &elemno);
  518. }
  519. | '.' identifier { $$ = newDesignation(DESIGNATOR_STRUCT,$2); }
  520. ;
  521. storage_class_specifier
  522. : TYPEDEF {
  523. $$ = newLink (SPECIFIER) ;
  524. SPEC_TYPEDEF($$) = 1 ;
  525. }
  526. | EXTERN {
  527. $$ = newLink(SPECIFIER);
  528. SPEC_EXTR($$) = 1 ;
  529. }
  530. | STATIC {
  531. $$ = newLink (SPECIFIER);
  532. SPEC_STAT($$) = 1 ;
  533. }
  534. | AUTO {
  535. $$ = newLink (SPECIFIER) ;
  536. SPEC_SCLS($$) = S_AUTO ;
  537. }
  538. | REGISTER {
  539. $$ = newLink (SPECIFIER);
  540. SPEC_SCLS($$) = S_REGISTER ;
  541. }
  542. ;
  543. function_specifier
  544. : INLINE {
  545. $$ = newLink (SPECIFIER) ;
  546. SPEC_INLINE($$) = 1 ;
  547. }
  548. | NORETURN {
  549. $$ = newLink (SPECIFIER) ;
  550. SPEC_NORETURN($$) = 1 ;
  551. }
  552. ;
  553. Interrupt_storage
  554. : INTERRUPT { $$ = INTNO_UNSPEC ; }
  555. | INTERRUPT constant_expr
  556. { int intno = (int) ulFromVal(constExprValue($2,TRUE));
  557. if ((intno >= 0) && (intno <= INTNO_MAX))
  558. $$ = intno;
  559. else
  560. {
  561. werror(E_INT_BAD_INTNO, intno);
  562. $$ = INTNO_UNSPEC;
  563. }
  564. }
  565. ;
  566. type_specifier
  567. : SD_BOOL {
  568. $$=newLink(SPECIFIER);
  569. SPEC_NOUN($$) = V_BOOL ;
  570. ignoreTypedefType = 1;
  571. }
  572. | SD_CHAR {
  573. $$=newLink(SPECIFIER);
  574. SPEC_NOUN($$) = V_CHAR ;
  575. ignoreTypedefType = 1;
  576. }
  577. | SD_SHORT {
  578. $$=newLink(SPECIFIER);
  579. SPEC_SHORT($$) = 1 ;
  580. ignoreTypedefType = 1;
  581. }
  582. | SD_INT {
  583. $$=newLink(SPECIFIER);
  584. SPEC_NOUN($$) = V_INT ;
  585. ignoreTypedefType = 1;
  586. }
  587. | SD_LONG {
  588. $$=newLink(SPECIFIER);
  589. SPEC_LONG($$) = 1 ;
  590. ignoreTypedefType = 1;
  591. }
  592. | SIGNED {
  593. $$=newLink(SPECIFIER);
  594. $$->select.s.b_signed = 1;
  595. ignoreTypedefType = 1;
  596. }
  597. | UNSIGNED {
  598. $$=newLink(SPECIFIER);
  599. SPEC_USIGN($$) = 1 ;
  600. ignoreTypedefType = 1;
  601. }
  602. | SD_VOID {
  603. $$=newLink(SPECIFIER);
  604. SPEC_NOUN($$) = V_VOID ;
  605. ignoreTypedefType = 1;
  606. }
  607. | SD_CONST {
  608. $$=newLink(SPECIFIER);
  609. SPEC_CONST($$) = 1;
  610. }
  611. | VOLATILE {
  612. $$=newLink(SPECIFIER);
  613. SPEC_VOLATILE($$) = 1 ;
  614. }
  615. | RESTRICT {
  616. $$=newLink(SPECIFIER);
  617. SPEC_RESTRICT($$) = 1 ;
  618. }
  619. | ADDRSPACE_NAME {
  620. $$=newLink(SPECIFIER);
  621. SPEC_ADDRSPACE($$) = findSym (AddrspaceTab, 0, $1);
  622. }
  623. | SD_FLOAT {
  624. $$=newLink(SPECIFIER);
  625. SPEC_NOUN($$) = V_FLOAT;
  626. ignoreTypedefType = 1;
  627. }
  628. | FIXED16X16 {
  629. $$=newLink(SPECIFIER);
  630. SPEC_NOUN($$) = V_FIXED16X16;
  631. ignoreTypedefType = 1;
  632. }
  633. | XDATA {
  634. $$ = newLink (SPECIFIER);
  635. SPEC_SCLS($$) = S_XDATA ;
  636. }
  637. | CODE {
  638. $$ = newLink (SPECIFIER) ;
  639. SPEC_SCLS($$) = S_CODE ;
  640. }
  641. | EEPROM {
  642. $$ = newLink (SPECIFIER) ;
  643. SPEC_SCLS($$) = S_EEPROM ;
  644. }
  645. | DATA {
  646. $$ = newLink (SPECIFIER);
  647. SPEC_SCLS($$) = S_DATA ;
  648. }
  649. | IDATA {
  650. $$ = newLink (SPECIFIER);
  651. SPEC_SCLS($$) = S_IDATA ;
  652. }
  653. | PDATA {
  654. $$ = newLink (SPECIFIER);
  655. SPEC_SCLS($$) = S_PDATA ;
  656. }
  657. | BIT {
  658. $$=newLink(SPECIFIER);
  659. SPEC_NOUN($$) = V_BIT ;
  660. SPEC_SCLS($$) = S_BIT ;
  661. SPEC_BLEN($$) = 1;
  662. SPEC_BSTR($$) = 0;
  663. ignoreTypedefType = 1;
  664. }
  665. | AT constant_expr {
  666. $$=newLink(SPECIFIER);
  667. /* add this to the storage class specifier */
  668. SPEC_ABSA($$) = 1; /* set the absolute addr flag */
  669. /* now get the abs addr from value */
  670. SPEC_ADDR($$) = (unsigned int) ulFromVal(constExprValue($2,TRUE)) ;
  671. }
  672. | struct_or_union_specifier {
  673. uselessDecl = FALSE;
  674. $$ = $1 ;
  675. ignoreTypedefType = 1;
  676. }
  677. | enum_specifier {
  678. cenum = NULL ;
  679. uselessDecl = FALSE;
  680. ignoreTypedefType = 1;
  681. $$ = $1 ;
  682. }
  683. | TYPE_NAME
  684. {
  685. symbol *sym;
  686. sym_link *p ;
  687. sym = findSym(TypedefTab,NULL,$1) ;
  688. $$ = p = copyLinkChain(sym ? sym->type : NULL);
  689. SPEC_TYPEDEF(getSpec(p)) = 0;
  690. ignoreTypedefType = 1;
  691. }
  692. | sfr_reg_bit
  693. ;
  694. sfr_reg_bit
  695. : SBIT {
  696. $$ = newLink(SPECIFIER) ;
  697. SPEC_NOUN($$) = V_SBIT;
  698. SPEC_SCLS($$) = S_SBIT;
  699. SPEC_BLEN($$) = 1;
  700. SPEC_BSTR($$) = 0;
  701. ignoreTypedefType = 1;
  702. }
  703. | sfr_attributes
  704. ;
  705. sfr_attributes
  706. : SFR {
  707. $$ = newLink(SPECIFIER) ;
  708. FUNC_REGBANK($$) = 0;
  709. SPEC_NOUN($$) = V_CHAR;
  710. SPEC_SCLS($$) = S_SFR ;
  711. SPEC_USIGN($$) = 1 ;
  712. ignoreTypedefType = 1;
  713. }
  714. | SFR BANKED {
  715. $$ = newLink(SPECIFIER) ;
  716. FUNC_REGBANK($$) = 1;
  717. SPEC_NOUN($$) = V_CHAR;
  718. SPEC_SCLS($$) = S_SFR ;
  719. SPEC_USIGN($$) = 1 ;
  720. ignoreTypedefType = 1;
  721. }
  722. ;
  723. sfr_attributes
  724. : SFR16 {
  725. $$ = newLink(SPECIFIER) ;
  726. FUNC_REGBANK($$) = 0;
  727. SPEC_NOUN($$) = V_INT;
  728. SPEC_SCLS($$) = S_SFR;
  729. SPEC_USIGN($$) = 1 ;
  730. ignoreTypedefType = 1;
  731. }
  732. ;
  733. sfr_attributes
  734. : SFR32 {
  735. $$ = newLink(SPECIFIER) ;
  736. FUNC_REGBANK($$) = 0;
  737. SPEC_NOUN($$) = V_INT;
  738. SPEC_SCLS($$) = S_SFR;
  739. SPEC_LONG($$) = 1;
  740. SPEC_USIGN($$) = 1;
  741. ignoreTypedefType = 1;
  742. }
  743. ;
  744. struct_or_union_specifier
  745. : struct_or_union opt_stag
  746. {
  747. if (!$2->type)
  748. {
  749. $2->type = $1;
  750. }
  751. else
  752. {
  753. if ($2->type != $1)
  754. werror(E_BAD_TAG, $2->tag, $1==STRUCT ? "struct" : "union");
  755. }
  756. }
  757. '{' struct_declaration_list '}'
  758. {
  759. structdef *sdef;
  760. symbol *sym, *dsym;
  761. // check for errors in structure members
  762. for (sym=$5; sym; sym=sym->next)
  763. {
  764. if (IS_ABSOLUTE(sym->etype))
  765. {
  766. werrorfl(sym->fileDef, sym->lineDef, E_NOT_ALLOWED, "'at'");
  767. SPEC_ABSA(sym->etype) = 0;
  768. }
  769. if (IS_SPEC(sym->etype) && SPEC_SCLS(sym->etype))
  770. {
  771. werrorfl(sym->fileDef, sym->lineDef, E_NOT_ALLOWED, "storage class");
  772. printTypeChainRaw (sym->type, NULL);
  773. SPEC_SCLS(sym->etype) = 0;
  774. }
  775. for (dsym=sym->next; dsym; dsym=dsym->next)
  776. {
  777. if (*dsym->name && strcmp(sym->name, dsym->name)==0)
  778. {
  779. werrorfl(sym->fileDef, sym->lineDef, E_DUPLICATE_MEMBER,
  780. $1==STRUCT ? "struct" : "union", sym->name);
  781. werrorfl(dsym->fileDef, dsym->lineDef, E_PREVIOUS_DEF);
  782. }
  783. }
  784. }
  785. /* Create a structdef */
  786. sdef = $2;
  787. sdef->fields = reverseSyms($5); /* link the fields */
  788. sdef->size = compStructSize($1, sdef); /* update size of */
  789. promoteAnonStructs ($1, sdef);
  790. /* Create the specifier */
  791. $$ = newLink (SPECIFIER);
  792. SPEC_NOUN($$) = V_STRUCT;
  793. SPEC_STRUCT($$)= sdef;
  794. }
  795. | struct_or_union stag
  796. {
  797. $$ = newLink(SPECIFIER);
  798. SPEC_NOUN($$) = V_STRUCT;
  799. SPEC_STRUCT($$) = $2;
  800. if (!$2->type)
  801. {
  802. $2->type = $1;
  803. }
  804. else
  805. {
  806. if ($2->type != $1)
  807. werror(E_BAD_TAG, $2->tag, $1==STRUCT ? "struct" : "union");
  808. }
  809. }
  810. ;
  811. struct_or_union
  812. : STRUCT { $$ = STRUCT ; ignoreTypedefType = 1; }
  813. | UNION { $$ = UNION ; ignoreTypedefType = 1; }
  814. ;
  815. opt_stag
  816. : stag
  817. | { /* synthesize a name add to structtable */
  818. ignoreTypedefType = 0;
  819. $$ = newStruct(genSymName(NestLevel));
  820. $$->level = NestLevel;
  821. addSym (StructTab, $$, $$->tag, $$->level, currBlockno, 0);
  822. }
  823. ;
  824. stag
  825. : identifier
  826. { /* add name to structure table */
  827. ignoreTypedefType = 0;
  828. $$ = findSymWithBlock (StructTab, $1, currBlockno);
  829. if (! $$ )
  830. {
  831. $$ = newStruct($1->name);
  832. $$->level = NestLevel;
  833. addSym (StructTab, $$, $$->tag, $$->level, currBlockno, 0);
  834. }
  835. }
  836. ;
  837. struct_declaration_list
  838. : struct_declaration
  839. | struct_declaration_list struct_declaration
  840. {
  841. symbol *sym = $2;
  842. /* go to the end of the chain */
  843. while (sym->next) sym = sym->next;
  844. sym->next = $1;
  845. $$ = $2;
  846. }
  847. ;
  848. struct_declaration
  849. : type_specifier_list struct_declarator_list ';'
  850. {
  851. /* add this type to all the symbols */
  852. symbol *sym ;
  853. for ( sym = $2 ; sym != NULL ; sym = sym->next )
  854. {
  855. sym_link *btype = copyLinkChain($1);
  856. pointerTypes(sym->type, btype);
  857. if (!sym->type)
  858. {
  859. sym->type = btype;
  860. sym->etype = getSpec(sym->type);
  861. }
  862. else
  863. addDecl (sym, 0, btype);
  864. /* make sure the type is complete and sane */
  865. checkTypeSanity(sym->etype, sym->name);
  866. }
  867. ignoreTypedefType = 0;
  868. $$ = $2;
  869. }
  870. ;
  871. struct_declarator_list
  872. : struct_declarator
  873. | struct_declarator_list ',' struct_declarator
  874. {
  875. $3->next = $1;
  876. $$ = $3;
  877. }
  878. ;
  879. struct_declarator
  880. : declarator
  881. | ':' constant_expr
  882. {
  883. unsigned int bitsize;
  884. $$ = newSymbol (genSymName(NestLevel), NestLevel);
  885. bitsize = (unsigned int) ulFromVal(constExprValue($2, TRUE));
  886. if (bitsize > (port->s.int_size * 8))
  887. {
  888. bitsize = port->s.int_size * 8;
  889. werror(E_BITFLD_SIZE, bitsize);
  890. }
  891. if (!bitsize)
  892. bitsize = BITVAR_PAD;
  893. $$->bitVar = bitsize;
  894. $$->bitUnnamed = 1;
  895. }
  896. | declarator ':' constant_expr
  897. {
  898. unsigned int bitsize;
  899. bitsize = (unsigned int) ulFromVal(constExprValue($3, TRUE));
  900. if (bitsize > (port->s.int_size * 8))
  901. {
  902. bitsize = port->s.int_size * 8;
  903. werror(E_BITFLD_SIZE, bitsize);
  904. }
  905. if (!bitsize)
  906. {
  907. $$ = newSymbol (genSymName(NestLevel), NestLevel);
  908. $$->bitVar = BITVAR_PAD;
  909. werror(W_BITFLD_NAMED);
  910. }
  911. else
  912. $1->bitVar = bitsize;
  913. }
  914. | { $$ = newSymbol ("", NestLevel); }
  915. ;
  916. enum_specifier
  917. : ENUM '{' enumerator_list '}'
  918. {
  919. $$ = newEnumType ($3);
  920. SPEC_SCLS(getSpec($$)) = 0;
  921. }
  922. | ENUM identifier '{' enumerator_list '}'
  923. {
  924. symbol *csym ;
  925. sym_link *enumtype;
  926. csym = findSymWithLevel(enumTab, $2);
  927. if ((csym && csym->level == $2->level))
  928. {
  929. werrorfl($2->fileDef, $2->lineDef, E_DUPLICATE_TYPEDEF, csym->name);
  930. werrorfl(csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
  931. }
  932. enumtype = newEnumType ($4);
  933. SPEC_SCLS(getSpec(enumtype)) = 0;
  934. $2->type = enumtype;
  935. /* add this to the enumerator table */
  936. if (!csym)
  937. addSym (enumTab, $2, $2->name, $2->level, $2->block, 0);
  938. $$ = copyLinkChain(enumtype);
  939. }
  940. | ENUM identifier
  941. {
  942. symbol *csym ;
  943. /* check the enumerator table */
  944. if ((csym = findSymWithLevel(enumTab, $2)))
  945. $$ = copyLinkChain(csym->type);
  946. else
  947. {
  948. $$ = newLink(SPECIFIER);
  949. SPEC_NOUN($$) = V_INT;
  950. }
  951. }
  952. ;
  953. enumerator_list
  954. : enumerator
  955. | enumerator_list ','
  956. | enumerator_list ',' enumerator
  957. {
  958. $3->next = $1;
  959. $$ = $3;
  960. }
  961. ;
  962. enumerator
  963. : identifier opt_assign_expr
  964. {
  965. symbol *sym;
  966. // check if the symbol at the same level already exists
  967. if ((sym = findSymWithLevel (SymbolTab, $1)) && sym->level == $1->level)
  968. {
  969. werrorfl ($1->fileDef, $1->lineDef, E_DUPLICATE_MEMBER, "enum", $1->name);
  970. werrorfl (sym->fileDef, sym->lineDef, E_PREVIOUS_DEF);
  971. }
  972. $1->type = copyLinkChain ($2->type);
  973. $1->etype = getSpec ($1->type);
  974. SPEC_ENUM ($1->etype) = 1;
  975. $$ = $1;
  976. // do this now, so we can use it for the next enums in the list
  977. addSymChain (&$1);
  978. }
  979. ;
  980. opt_assign_expr
  981. : '=' constant_expr
  982. {
  983. value *val;
  984. val = constExprValue($2, TRUE);
  985. if (!IS_INT(val->type) && !IS_CHAR(val->type) && !IS_BOOL(val->type))
  986. {
  987. werror(E_ENUM_NON_INTEGER);
  988. SNPRINTF(lbuff, sizeof(lbuff), "%d", (int) ulFromVal(val));
  989. val = constVal(lbuff);
  990. }
  991. $$ = cenum = val ;
  992. }
  993. | {
  994. if (cenum)
  995. {
  996. SNPRINTF(lbuff, sizeof(lbuff), "%d", (int) ulFromVal(cenum)+1);
  997. $$ = cenum = constVal(lbuff);
  998. }
  999. else
  1000. {
  1001. $$ = cenum = constCharVal(0);
  1002. }
  1003. }
  1004. ;
  1005. declarator
  1006. : declarator3 { $$ = $1 ; }
  1007. | pointer declarator3
  1008. {
  1009. addDecl ($2,0,reverseLink($1));
  1010. $$ = $2 ;
  1011. }
  1012. ;
  1013. declarator3
  1014. : declarator2_function_attributes { $$ = $1 ; }
  1015. | declarator2 { $$ = $1 ; }
  1016. ;
  1017. function_declarator
  1018. : declarator2_function_attributes { $$ = $1; }
  1019. | pointer declarator2_function_attributes
  1020. {
  1021. addDecl ($2,0,reverseLink($1));
  1022. $$ = $2 ;
  1023. }
  1024. ;
  1025. declarator2_function_attributes
  1026. : function_declarator2 { $$ = $1 ; }
  1027. | function_declarator2 function_attribute {
  1028. // copy the functionAttributes (not the args and hasVargs !!)
  1029. struct value *args;
  1030. unsigned hasVargs;
  1031. sym_link *funcType=$1->type;
  1032. while (funcType && !IS_FUNC(funcType))
  1033. funcType = funcType->next;
  1034. if (!funcType)
  1035. werror (E_FUNC_ATTR);
  1036. else
  1037. {
  1038. args=FUNC_ARGS(funcType);
  1039. hasVargs=FUNC_HASVARARGS(funcType);
  1040. memcpy (&funcType->funcAttrs, &$2->funcAttrs,
  1041. sizeof($2->funcAttrs));
  1042. FUNC_ARGS(funcType)=args;
  1043. FUNC_HASVARARGS(funcType)=hasVargs;
  1044. // just to be sure
  1045. memset (&$2->funcAttrs, 0,
  1046. sizeof($2->funcAttrs));
  1047. addDecl ($1,0,$2);
  1048. }
  1049. }
  1050. ;
  1051. declarator2
  1052. : identifier
  1053. | '(' declarator ')' { $$ = $2; }
  1054. | declarator3 '[' ']'
  1055. {
  1056. sym_link *p;
  1057. p = newLink (DECLARATOR);
  1058. DCL_TYPE(p) = ARRAY ;
  1059. DCL_ELEM(p) = 0 ;
  1060. addDecl($1,0,p);
  1061. }
  1062. | declarator3 '[' constant_expr ']'
  1063. {
  1064. sym_link *p;
  1065. value *tval;
  1066. int size;
  1067. tval = constExprValue($3, TRUE);
  1068. /* if it is not a constant then Error */
  1069. p = newLink (DECLARATOR);
  1070. DCL_TYPE(p) = ARRAY;
  1071. if (!tval || (SPEC_SCLS(tval->etype) != S_LITERAL))
  1072. {
  1073. werror(E_CONST_EXPECTED);
  1074. /* Assume a single item array to limit the cascade */
  1075. /* of additional errors. */
  1076. size = 1;
  1077. }
  1078. else
  1079. {
  1080. if ((size = (int) ulFromVal(tval)) < 0)
  1081. {
  1082. werror(E_NEGATIVE_ARRAY_SIZE, $1->name);
  1083. size = 1;
  1084. }
  1085. }
  1086. DCL_ELEM(p) = size;
  1087. addDecl($1, 0, p);
  1088. }
  1089. ;
  1090. function_declarator2
  1091. : declarator2 '(' ')'
  1092. {
  1093. addDecl ($1, FUNCTION, NULL);
  1094. }
  1095. | declarator2 '('
  1096. {
  1097. NestLevel++;
  1098. STACK_PUSH(blockNum, currBlockno);
  1099. currBlockno = ++blockNo;
  1100. }
  1101. parameter_type_list ')'
  1102. {
  1103. sym_link *funcType;
  1104. addDecl ($1, FUNCTION, NULL);
  1105. funcType = $1->type;
  1106. while (funcType && !IS_FUNC(funcType))
  1107. funcType = funcType->next;
  1108. assert (funcType);
  1109. FUNC_HASVARARGS(funcType) = IS_VARG($4);
  1110. FUNC_ARGS(funcType) = reverseVal($4);
  1111. /* nest level was incremented to take care of the parms */
  1112. NestLevel--;
  1113. currBlockno = STACK_POP(blockNum);
  1114. // if this was a pointer (to a function)
  1115. if (!IS_FUNC($1->type))
  1116. cleanUpLevel(SymbolTab, NestLevel + 1);
  1117. $$ = $1;
  1118. }
  1119. | declarator2 '(' identifier_list ')'
  1120. {
  1121. werror(E_OLD_STYLE,$1->name);
  1122. /* assume it returns an int */
  1123. $1->type = $1->etype = newIntLink();
  1124. $$ = $1;
  1125. }
  1126. ;
  1127. pointer
  1128. : unqualified_pointer { $$ = $1 ;}
  1129. | unqualified_pointer type_specifier_list
  1130. {
  1131. $$ = $1 ;
  1132. if (IS_SPEC($2)) {
  1133. DCL_TSPEC($1) = $2;
  1134. DCL_PTR_CONST($1) = SPEC_CONST($2);
  1135. DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
  1136. DCL_PTR_RESTRICT($1) = SPEC_RESTRICT($2);
  1137. DCL_PTR_ADDRSPACE($1) = SPEC_ADDRSPACE($2);
  1138. }
  1139. else
  1140. werror (W_PTR_TYPE_INVALID);
  1141. }
  1142. | unqualified_pointer pointer
  1143. {
  1144. $$ = $1 ;
  1145. $$->next = $2 ;
  1146. DCL_TYPE($2)=port->unqualified_pointer;
  1147. }
  1148. | unqualified_pointer type_specifier_list pointer
  1149. {
  1150. $$ = $1 ;
  1151. if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
  1152. DCL_PTR_CONST($1) = SPEC_CONST($2);
  1153. DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
  1154. DCL_PTR_RESTRICT($1) = SPEC_RESTRICT($2);
  1155. DCL_PTR_ADDRSPACE($1) = SPEC_ADDRSPACE($2);
  1156. switch (SPEC_SCLS($2)) {
  1157. case S_XDATA:
  1158. DCL_TYPE($3) = FPOINTER;
  1159. break;
  1160. case S_IDATA:
  1161. DCL_TYPE($3) = IPOINTER ;
  1162. break;
  1163. case S_PDATA:
  1164. DCL_TYPE($3) = PPOINTER ;
  1165. break;
  1166. case S_DATA:
  1167. DCL_TYPE($3) = POINTER ;
  1168. break;
  1169. case S_CODE:
  1170. DCL_TYPE($3) = CPOINTER ;
  1171. break;
  1172. case S_EEPROM:
  1173. DCL_TYPE($3) = EEPPOINTER;
  1174. break;
  1175. default:
  1176. // this could be just "constant"
  1177. // werror(W_PTR_TYPE_INVALID);
  1178. ;
  1179. }
  1180. }
  1181. else
  1182. werror (W_PTR_TYPE_INVALID);
  1183. $$->next = $3 ;
  1184. }
  1185. ;
  1186. unqualified_pointer
  1187. : '*'
  1188. {
  1189. $$ = newLink(DECLARATOR);
  1190. DCL_TYPE($$)=UPOINTER;
  1191. }
  1192. ;
  1193. type_specifier_list : type_specifier_list_ { $$ = finalizeSpec($1); } ;
  1194. type_specifier_list_
  1195. : type_specifier
  1196. //| type_specifier_list_ type_specifier { $$ = mergeSpec ($1,$2, "type_specifier_list"); }
  1197. | type_specifier_list_ type_specifier {
  1198. /* if the decl $2 is not a specifier */
  1199. /* find the spec and replace it */
  1200. $$ = mergeDeclSpec($1, $2, "type_specifier_list type_specifier skipped");
  1201. }
  1202. ;
  1203. identifier_list
  1204. : identifier
  1205. | identifier_list ',' identifier
  1206. {
  1207. $3->next = $1;
  1208. $$ = $3 ;
  1209. }
  1210. ;
  1211. parameter_type_list
  1212. : parameter_list
  1213. | parameter_list ',' VAR_ARGS { $1->vArgs = 1;}
  1214. ;
  1215. parameter_list
  1216. : parameter_declaration
  1217. | parameter_list ',' parameter_declaration
  1218. {
  1219. $3->next = $1 ;
  1220. $$ = $3 ;
  1221. }
  1222. ;
  1223. parameter_declaration
  1224. : declaration_specifiers declarator
  1225. {
  1226. symbol *loop;
  1227. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1228. {
  1229. werror (E_STORAGE_CLASS_FOR_PARAMETER, $2->name);
  1230. }
  1231. pointerTypes ($2->type, $1);
  1232. addDecl ($2, 0, $1);
  1233. for (loop = $2; loop; loop->_isparm = 1, loop = loop->next)
  1234. ;
  1235. addSymChain (&$2);
  1236. $$ = symbolVal ($2);
  1237. ignoreTypedefType = 0;
  1238. }
  1239. | type_name
  1240. {
  1241. $$ = newValue ();
  1242. $$->type = $1;
  1243. $$->etype = getSpec ($$->type);
  1244. ignoreTypedefType = 0;
  1245. }
  1246. ;
  1247. type_name
  1248. : declaration_specifiers
  1249. {
  1250. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1251. {
  1252. werror (E_STORAGE_CLASS_FOR_PARAMETER, "type name");
  1253. }
  1254. $$ = $1; ignoreTypedefType = 0;
  1255. }
  1256. | declaration_specifiers abstract_declarator
  1257. {
  1258. /* go to the end of the list */
  1259. sym_link *p;
  1260. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1261. {
  1262. werror (E_STORAGE_CLASS_FOR_PARAMETER, "type name");
  1263. }
  1264. pointerTypes ($2,$1);
  1265. for (p = $2; p && p->next; p = p->next)
  1266. ;
  1267. if (!p)
  1268. {
  1269. werror(E_SYNTAX_ERROR, yytext);
  1270. }
  1271. else
  1272. {
  1273. p->next = $1 ;
  1274. }
  1275. $$ = $2 ;
  1276. ignoreTypedefType = 0;
  1277. }
  1278. ;
  1279. abstract_declarator
  1280. : pointer { $$ = reverseLink($1); }
  1281. | abstract_declarator2
  1282. | pointer abstract_declarator2 { $1 = reverseLink($1); $1->next = $2 ; $$ = $1;
  1283. if (IS_PTR($1) && IS_FUNC($2))
  1284. DCL_TYPE($1) = CPOINTER;
  1285. }
  1286. ;
  1287. abstract_declarator2
  1288. : '(' abstract_declarator ')' { $$ = $2 ; }
  1289. | '[' ']' {
  1290. $$ = newLink (DECLARATOR);
  1291. DCL_TYPE($$) = ARRAY ;
  1292. DCL_ELEM($$) = 0 ;
  1293. }
  1294. | '[' constant_expr ']' {
  1295. value *val ;
  1296. $$ = newLink (DECLARATOR);
  1297. DCL_TYPE($$) = ARRAY ;
  1298. DCL_ELEM($$) = (int) ulFromVal(val = constExprValue($2,TRUE));
  1299. }
  1300. | abstract_declarator2 '[' ']' {
  1301. $$ = newLink (DECLARATOR);
  1302. DCL_TYPE($$) = ARRAY ;
  1303. DCL_ELEM($$) = 0 ;
  1304. $$->next = $1 ;
  1305. }
  1306. | abstract_declarator2 '[' constant_expr ']'
  1307. {
  1308. value *val ;
  1309. $$ = newLink (DECLARATOR);
  1310. DCL_TYPE($$) = ARRAY ;
  1311. DCL_ELEM($$) = (int) ulFromVal(val = constExprValue($3,TRUE));
  1312. $$->next = $1 ;
  1313. }
  1314. | '(' ')' { $$ = NULL;}
  1315. | '(' parameter_type_list ')' { $$ = NULL;}
  1316. | abstract_declarator2 '(' ')' {
  1317. // $1 must be a pointer to a function
  1318. sym_link *p=newLink(DECLARATOR);
  1319. DCL_TYPE(p) = FUNCTION;
  1320. if (!$1) {
  1321. // ((void (code *) ()) 0) ()
  1322. $1=newLink(DECLARATOR);
  1323. DCL_TYPE($1)=CPOINTER;
  1324. $$ = $1;
  1325. }
  1326. $1->next=p;
  1327. }
  1328. | abstract_declarator2 '('
  1329. {
  1330. NestLevel++;
  1331. STACK_PUSH(blockNum, currBlockno);
  1332. currBlockno = ++blockNo;
  1333. }
  1334. parameter_type_list ')'
  1335. {
  1336. sym_link *p = newLink(DECLARATOR);
  1337. DCL_TYPE(p) = FUNCTION;
  1338. FUNC_HASVARARGS(p) = IS_VARG($4);
  1339. FUNC_ARGS(p) = reverseVal($4);
  1340. /* nest level was incremented to take care of the parms */
  1341. NestLevel--;
  1342. currBlockno = STACK_POP(blockNum);
  1343. if (!$1)
  1344. {
  1345. /* ((void (code *) (void)) 0) () */
  1346. $1 = newLink(DECLARATOR);
  1347. DCL_TYPE($1) = CPOINTER;
  1348. $$ = $1;
  1349. }
  1350. $1->next = p;
  1351. // disabled to fix bug 3190029
  1352. // // remove the symbol args (if any)
  1353. // cleanUpLevel(SymbolTab, NestLevel + 1);
  1354. }
  1355. ;
  1356. initializer
  1357. : assignment_expr { $$ = newiList(INIT_NODE,$1); }
  1358. | '{' initializer_list '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
  1359. | '{' initializer_list ',' '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
  1360. ;
  1361. initializer_list
  1362. : designation_opt initializer { $2->designation = $1; $$ = $2; }
  1363. | initializer_list ',' designation_opt initializer
  1364. {
  1365. $4->designation = $3;
  1366. $4->next = $1;
  1367. $$ = $4;
  1368. }
  1369. ;
  1370. statement
  1371. : labeled_statement
  1372. | compound_statement
  1373. | expression_statement
  1374. | selection_statement
  1375. | iteration_statement
  1376. | jump_statement
  1377. | critical_statement
  1378. | INLINEASM ';' {
  1379. ast *ex;
  1380. seqPointNo++;
  1381. ex = newNode(INLINEASM,NULL,NULL);
  1382. ex->values.inlineasm = strdup($1);
  1383. seqPointNo++;
  1384. $$ = ex;
  1385. }
  1386. ;
  1387. critical
  1388. : CRITICAL {
  1389. inCritical++;
  1390. STACK_PUSH(continueStack,NULL);
  1391. STACK_PUSH(breakStack,NULL);
  1392. $$ = NULL;
  1393. }
  1394. ;
  1395. critical_statement
  1396. : critical statement {
  1397. STACK_POP(breakStack);
  1398. STACK_POP(continueStack);
  1399. inCritical--;
  1400. $$ = newNode(CRITICAL,$2,NULL);
  1401. }
  1402. ;
  1403. labeled_statement
  1404. // : identifier ':' statement { $$ = createLabel($1,$3); }
  1405. : identifier ':' { $$ = createLabel($1,NULL);
  1406. $1->isitmp = 0; }
  1407. | CASE constant_expr ':'
  1408. {
  1409. if (STACK_EMPTY(swStk))
  1410. $$ = createCase(NULL,$2,NULL);
  1411. else
  1412. $$ = createCase(STACK_PEEK(swStk),$2,NULL);
  1413. }
  1414. | DEFAULT { $<asts>$ = newNode(DEFAULT,NULL,NULL); } ':'
  1415. {
  1416. if (STACK_EMPTY(swStk))
  1417. $$ = createDefault(NULL,$<asts>2,NULL);
  1418. else
  1419. $$ = createDefault(STACK_PEEK(swStk),$<asts>2,NULL);
  1420. }
  1421. ;
  1422. start_block
  1423. : '{'
  1424. {
  1425. NestLevel++;
  1426. STACK_PUSH(blockNum, currBlockno);
  1427. btree_add_child(currBlockno, ++blockNo);
  1428. currBlockno = blockNo;
  1429. ignoreTypedefType = 0;
  1430. }
  1431. ;
  1432. end_block
  1433. : '}'
  1434. {
  1435. NestLevel--;
  1436. currBlockno = STACK_POP(blockNum);
  1437. }
  1438. ;
  1439. compound_statement
  1440. : start_block end_block { $$ = createBlock(NULL, NULL); }
  1441. | start_block statement_list end_block { $$ = createBlock(NULL, $2); }
  1442. | start_block declaration_list end_block { $$ = createBlock($2, NULL); }
  1443. | start_block
  1444. declaration_list statement_list
  1445. end_block { $$ = createBlock($2, $3); }
  1446. | error ';' { $$ = NULL ; }
  1447. ;
  1448. declaration_list
  1449. : declaration
  1450. {
  1451. /* if this is typedef declare it immediately */
  1452. if ( $1 && IS_TYPEDEF($1->etype)) {
  1453. allocVariables

Large files files are truncated, but you can click here to view the full file