PageRenderTime 64ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/branches/optaddr/sdcc/src/SDCC.y

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

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