PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/sdcc-310/src/SDCC.y

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

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