PageRenderTime 60ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/sdcc/src/SDCC.y

https://bitbucket.org/mirrorbot/sdcc
Happy | 1942 lines | 1774 code | 168 blank | 0 comment | 0 complexity | 8d1d95e0bd617ff808156d34326dded4 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, GPL-3.0, LGPL-2.1
  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 *yystr; /* pointer to dynamicaly allocated string */
  69. ast *asts; /* expression tree */
  70. }
  71. %token <yychar> IDENTIFIER TYPE_NAME ADDRSPACE_NAME
  72. %token <val> CONSTANT
  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 <yystr> STRING_LITERAL 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. %token ASM
  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_val
  117. %type <asts> critical_statement asm_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. %type <yystr> asm_string_literal
  122. %start file
  123. %%
  124. file
  125. : /* empty */
  126. {
  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. }
  140. | declaration
  141. {
  142. ignoreTypedefType = 0;
  143. if ($1 && $1->type && IS_FUNC($1->type))
  144. {
  145. /* The only legal storage classes for
  146. * a function prototype (declaration)
  147. * are extern and static. extern is the
  148. * default. Thus, if this function isn't
  149. * explicitly marked static, mark it
  150. * extern.
  151. */
  152. if ($1->etype && IS_SPEC($1->etype) && !SPEC_STAT($1->etype))
  153. {
  154. SPEC_EXTR($1->etype) = 1;
  155. }
  156. }
  157. addSymChain (&$1);
  158. allocVariables ($1);
  159. cleanUpLevel (SymbolTab, 1);
  160. }
  161. | addressmod
  162. ;
  163. function_definition
  164. : function_declarator function_body { /* function type not specified */
  165. /* assume it to be 'int' */
  166. addDecl($1,0,newIntLink());
  167. $$ = createFunction($1,$2);
  168. }
  169. | declaration_specifiers function_declarator function_body
  170. {
  171. pointerTypes($2->type,copyLinkChain($1));
  172. addDecl($2,0,$1);
  173. $$ = createFunction($2,$3);
  174. }
  175. ;
  176. function_attribute
  177. : function_attributes
  178. | function_attributes function_attribute { $$ = mergeSpec($1,$2,"function_attribute"); }
  179. ;
  180. function_attributes
  181. : USING constant_expr {
  182. $$ = newLink(SPECIFIER);
  183. FUNC_REGBANK($$) = (int) ulFromVal(constExprValue($2,TRUE));
  184. }
  185. | REENTRANT { $$ = newLink (SPECIFIER);
  186. FUNC_ISREENT($$)=1;
  187. }
  188. | CRITICAL { $$ = newLink (SPECIFIER);
  189. FUNC_ISCRITICAL($$) = 1;
  190. }
  191. | NAKED { $$ = newLink (SPECIFIER);
  192. FUNC_ISNAKED($$)=1;
  193. }
  194. | JAVANATIVE { $$ = newLink (SPECIFIER);
  195. FUNC_ISJAVANATIVE($$)=1;
  196. }
  197. | OVERLAY { $$ = newLink (SPECIFIER);
  198. FUNC_ISOVERLAY($$)=1;
  199. }
  200. | NONBANKED {$$ = newLink (SPECIFIER);
  201. FUNC_NONBANKED($$) = 1;
  202. if (FUNC_BANKED($$)) {
  203. werror(W_BANKED_WITH_NONBANKED);
  204. }
  205. }
  206. | SHADOWREGS {$$ = newLink (SPECIFIER);
  207. FUNC_ISSHADOWREGS($$) = 1;
  208. }
  209. | SD_WPARAM {$$ = newLink (SPECIFIER);
  210. FUNC_ISWPARAM($$) = 1;
  211. }
  212. | BANKED {$$ = newLink (SPECIFIER);
  213. FUNC_BANKED($$) = 1;
  214. if (FUNC_NONBANKED($$)) {
  215. werror(W_BANKED_WITH_NONBANKED);
  216. }
  217. }
  218. | Interrupt_storage
  219. {
  220. $$ = newLink (SPECIFIER);
  221. FUNC_INTNO($$) = $1;
  222. FUNC_ISISR($$) = 1;
  223. }
  224. | SMALLC { $$ = newLink (SPECIFIER);
  225. FUNC_ISSMALLC($$)=1;
  226. }
  227. ;
  228. function_body
  229. : compound_statement
  230. | declaration_list compound_statement
  231. {
  232. werror (E_OLD_STYLE, ($1 ? $1->name: ""));
  233. exit (1);
  234. }
  235. ;
  236. offsetof_member_designator
  237. : identifier { $$ = newAst_VALUE (symbolVal ($1)); }
  238. | offsetof_member_designator '.' { ignoreTypedefType = 1; } identifier
  239. {
  240. ignoreTypedefType = 0;
  241. $4 = newSymbol ($4->name, NestLevel);
  242. $4->implicit = 1;
  243. $$ = newNode ('.', $1, newAst_VALUE (symbolVal ($4)));
  244. }
  245. | offsetof_member_designator '[' expr ']'
  246. {
  247. $$ = newNode ('[', $1, $3);
  248. }
  249. ;
  250. primary_expr
  251. : identifier { $$ = newAst_VALUE (symbolVal ($1)); }
  252. | CONSTANT { $$ = newAst_VALUE ($1); }
  253. | string_literal_val
  254. | '(' expr ')' { $$ = $2; }
  255. ;
  256. string_literal_val
  257. : STRING_LITERAL { $$ = newAst_VALUE (strVal ($1)); }
  258. ;
  259. postfix_expr
  260. : primary_expr
  261. | postfix_expr '[' expr ']' { $$ = newNode ('[', $1, $3); }
  262. | postfix_expr '(' ')' { $$ = newNode (CALL,$1,NULL);
  263. $$->left->funcName = 1;}
  264. | postfix_expr '(' argument_expr_list ')'
  265. {
  266. $$ = newNode (CALL,$1,$3); $$->left->funcName = 1;
  267. }
  268. | postfix_expr '.' { ignoreTypedefType = 1; } identifier
  269. {
  270. ignoreTypedefType = 0;
  271. $4 = newSymbol($4->name,NestLevel);
  272. $4->implicit = 1;
  273. $$ = newNode(PTR_OP,newNode('&',$1,NULL),newAst_VALUE(symbolVal($4)));
  274. }
  275. | postfix_expr PTR_OP { ignoreTypedefType = 1; } identifier
  276. {
  277. ignoreTypedefType = 0;
  278. $4 = newSymbol($4->name,NestLevel);
  279. $4->implicit = 1;
  280. $$ = newNode(PTR_OP,$1,newAst_VALUE(symbolVal($4)));
  281. }
  282. | postfix_expr INC_OP
  283. { $$ = newNode(INC_OP,$1,NULL);}
  284. | postfix_expr DEC_OP
  285. { $$ = newNode(DEC_OP,$1,NULL); }
  286. ;
  287. argument_expr_list
  288. : assignment_expr
  289. | assignment_expr ',' argument_expr_list { $$ = newNode(PARAM,$1,$3); }
  290. ;
  291. unary_expr
  292. : postfix_expr
  293. | INC_OP unary_expr { $$ = newNode (INC_OP, NULL, $2); }
  294. | DEC_OP unary_expr { $$ = newNode (DEC_OP, NULL, $2); }
  295. | unary_operator cast_expr { $$ = newNode ($1, $2, NULL); }
  296. | SIZEOF unary_expr { $$ = newNode (SIZEOF, NULL, $2); }
  297. | SIZEOF '(' type_name ')' { $$ = newAst_VALUE (sizeofOp ($3)); }
  298. | TYPEOF unary_expr { $$ = newNode (TYPEOF, NULL, $2); }
  299. | OFFSETOF '(' type_name ',' offsetof_member_designator ')' { $$ = offsetofOp($3, $5); }
  300. ;
  301. unary_operator
  302. : '&' { $$ = '&';}
  303. | '*' { $$ = '*';}
  304. | '+' { $$ = '+';}
  305. | '-' { $$ = '-';}
  306. | '~' { $$ = '~';}
  307. | '!' { $$ = '!';}
  308. ;
  309. cast_expr
  310. : unary_expr
  311. | '(' type_name ')' cast_expr { $$ = newNode(CAST,newAst_LINK($2),$4); }
  312. ;
  313. multiplicative_expr
  314. : cast_expr
  315. | multiplicative_expr '*' cast_expr { $$ = newNode('*',$1,$3);}
  316. | multiplicative_expr '/' cast_expr { $$ = newNode('/',$1,$3);}
  317. | multiplicative_expr '%' cast_expr { $$ = newNode('%',$1,$3);}
  318. ;
  319. additive_expr
  320. : multiplicative_expr
  321. | additive_expr '+' multiplicative_expr { $$=newNode('+',$1,$3);}
  322. | additive_expr '-' multiplicative_expr { $$=newNode('-',$1,$3);}
  323. ;
  324. shift_expr
  325. : additive_expr
  326. | shift_expr LEFT_OP additive_expr { $$ = newNode(LEFT_OP,$1,$3); }
  327. | shift_expr RIGHT_OP additive_expr { $$ = newNode(RIGHT_OP,$1,$3); }
  328. ;
  329. relational_expr
  330. : shift_expr
  331. | relational_expr '<' shift_expr { $$ = newNode('<', $1,$3);}
  332. | relational_expr '>' shift_expr { $$ = newNode('>', $1,$3);}
  333. | relational_expr LE_OP shift_expr { $$ = newNode(LE_OP,$1,$3);}
  334. | relational_expr GE_OP shift_expr { $$ = newNode(GE_OP,$1,$3);}
  335. ;
  336. equality_expr
  337. : relational_expr
  338. | equality_expr EQ_OP relational_expr { $$ = newNode(EQ_OP,$1,$3);}
  339. | equality_expr NE_OP relational_expr { $$ = newNode(NE_OP,$1,$3);}
  340. ;
  341. and_expr
  342. : equality_expr
  343. | and_expr '&' equality_expr { $$ = newNode('&',$1,$3);}
  344. ;
  345. exclusive_or_expr
  346. : and_expr
  347. | exclusive_or_expr '^' and_expr { $$ = newNode('^',$1,$3);}
  348. ;
  349. inclusive_or_expr
  350. : exclusive_or_expr
  351. | inclusive_or_expr '|' exclusive_or_expr { $$ = newNode('|',$1,$3);}
  352. ;
  353. logical_and_expr
  354. : inclusive_or_expr
  355. | logical_and_expr AND_OP { seqPointNo++;} inclusive_or_expr
  356. { $$ = newNode(AND_OP,$1,$4);}
  357. ;
  358. logical_or_expr
  359. : logical_and_expr
  360. | logical_or_expr OR_OP { seqPointNo++;} logical_and_expr
  361. { $$ = newNode(OR_OP,$1,$4); }
  362. ;
  363. conditional_expr
  364. : logical_or_expr
  365. | logical_or_expr '?' { seqPointNo++;} logical_or_expr ':' conditional_expr
  366. {
  367. $$ = newNode(':',$4,$6);
  368. $$ = newNode('?',$1,$$);
  369. }
  370. ;
  371. assignment_expr
  372. : conditional_expr
  373. | cast_expr assignment_operator assignment_expr
  374. {
  375. switch ($2) {
  376. case '=':
  377. $$ = newNode($2,$1,$3);
  378. break;
  379. case MUL_ASSIGN:
  380. $$ = createRMW($1, '*', $3);
  381. break;
  382. case DIV_ASSIGN:
  383. $$ = createRMW($1, '/', $3);
  384. break;
  385. case MOD_ASSIGN:
  386. $$ = createRMW($1, '%', $3);
  387. break;
  388. case ADD_ASSIGN:
  389. $$ = createRMW($1, '+', $3);
  390. break;
  391. case SUB_ASSIGN:
  392. $$ = createRMW($1, '-', $3);
  393. break;
  394. case LEFT_ASSIGN:
  395. $$ = createRMW($1, LEFT_OP, $3);
  396. break;
  397. case RIGHT_ASSIGN:
  398. $$ = createRMW($1, RIGHT_OP, $3);
  399. break;
  400. case AND_ASSIGN:
  401. $$ = createRMW($1, '&', $3);
  402. break;
  403. case XOR_ASSIGN:
  404. $$ = createRMW($1, '^', $3);
  405. break;
  406. case OR_ASSIGN:
  407. $$ = createRMW($1, '|', $3);
  408. break;
  409. default :
  410. $$ = NULL;
  411. }
  412. }
  413. ;
  414. assignment_operator
  415. : '=' { $$ = '=';}
  416. | MUL_ASSIGN
  417. | DIV_ASSIGN
  418. | MOD_ASSIGN
  419. | ADD_ASSIGN
  420. | SUB_ASSIGN
  421. | LEFT_ASSIGN
  422. | RIGHT_ASSIGN
  423. | AND_ASSIGN
  424. | XOR_ASSIGN
  425. | OR_ASSIGN
  426. ;
  427. expr
  428. : assignment_expr
  429. | expr ',' { seqPointNo++;} assignment_expr { $$ = newNode(',',$1,$4);}
  430. ;
  431. constant_expr
  432. : conditional_expr
  433. ;
  434. declaration
  435. : declaration_specifiers ';'
  436. {
  437. /* Special case: if incomplete struct/union declared without name, */
  438. /* make sure an incomplete type for it exists in the current scope */
  439. if (IS_STRUCT($1))
  440. {
  441. structdef *sdef = SPEC_STRUCT($1);
  442. structdef *osdef;
  443. osdef = findSymWithBlock (StructTab, sdef->tagsym, currBlockno);
  444. if (osdef && osdef->block != currBlockno)
  445. {
  446. sdef = newStruct(osdef->tagsym->name);
  447. sdef->level = NestLevel;
  448. sdef->block = currBlockno;
  449. sdef->tagsym = newSymbol (osdef->tagsym->name, NestLevel);
  450. addSym (StructTab, sdef, sdef->tag, sdef->level, currBlockno, 0);
  451. uselessDecl = FALSE;
  452. }
  453. }
  454. if (uselessDecl)
  455. werror(W_USELESS_DECL);
  456. uselessDecl = TRUE;
  457. $$ = NULL;
  458. }
  459. | declaration_specifiers init_declarator_list ';'
  460. {
  461. /* add the specifier list to the id */
  462. symbol *sym , *sym1;
  463. for (sym1 = sym = reverseSyms($2);sym != NULL;sym = sym->next) {
  464. sym_link *lnk = copyLinkChain($1);
  465. /* do the pointer stuff */
  466. pointerTypes(sym->type,lnk);
  467. addDecl (sym,0,lnk);
  468. }
  469. uselessDecl = TRUE;
  470. $$ = sym1;
  471. }
  472. ;
  473. declaration_specifiers : declaration_specifiers_ { $$ = finalizeSpec($1); };
  474. declaration_specifiers_
  475. : storage_class_specifier { $$ = $1; }
  476. | storage_class_specifier declaration_specifiers_ {
  477. /* if the decl $2 is not a specifier */
  478. /* find the spec and replace it */
  479. $$ = mergeDeclSpec($1, $2, "storage_class_specifier declaration_specifiers - skipped");
  480. }
  481. | type_specifier { $$ = $1; }
  482. | type_specifier declaration_specifiers_ {
  483. /* if the decl $2 is not a specifier */
  484. /* find the spec and replace it */
  485. $$ = mergeDeclSpec($1, $2, "type_specifier declaration_specifiers - skipped");
  486. }
  487. | function_specifier { $$ = $1; }
  488. | function_specifier declaration_specifiers_ {
  489. /* if the decl $2 is not a specifier */
  490. /* find the spec and replace it */
  491. $$ = mergeDeclSpec($1, $2, "function_specifier declaration_specifiers - skipped");
  492. }
  493. ;
  494. init_declarator_list
  495. : init_declarator
  496. | init_declarator_list ',' init_declarator { $3->next = $1; $$ = $3;}
  497. ;
  498. init_declarator
  499. : declarator { $1->ival = NULL; }
  500. | declarator '=' initializer { $1->ival = $3; }
  501. ;
  502. designation_opt
  503. : { $$ = NULL; }
  504. | designation
  505. ;
  506. designation
  507. : designator_list '=' { $$ = revDesignation($1); }
  508. ;
  509. designator_list
  510. : designator
  511. | designator_list designator { $2->next = $1; $$ = $2; }
  512. ;
  513. designator
  514. : '[' constant_expr ']'
  515. {
  516. value *tval;
  517. int elemno;
  518. tval = constExprValue($2, TRUE);
  519. /* if it is not a constant then Error */
  520. if (!tval || (SPEC_SCLS(tval->etype) != S_LITERAL))
  521. {
  522. werror (E_CONST_EXPECTED);
  523. elemno = 0; /* arbitrary fixup */
  524. }
  525. else
  526. {
  527. if ((elemno = (int) ulFromVal(tval)) < 0)
  528. {
  529. werror (E_BAD_DESIGNATOR);
  530. elemno = 0; /* arbitrary fixup */
  531. }
  532. }
  533. $$ = newDesignation(DESIGNATOR_ARRAY, &elemno);
  534. }
  535. | '.' identifier { $$ = newDesignation(DESIGNATOR_STRUCT,$2); }
  536. ;
  537. storage_class_specifier
  538. : TYPEDEF {
  539. $$ = newLink (SPECIFIER);
  540. SPEC_TYPEDEF($$) = 1;
  541. }
  542. | EXTERN {
  543. $$ = newLink(SPECIFIER);
  544. SPEC_EXTR($$) = 1;
  545. }
  546. | STATIC {
  547. $$ = newLink (SPECIFIER);
  548. SPEC_STAT($$) = 1;
  549. }
  550. | AUTO {
  551. $$ = newLink (SPECIFIER);
  552. SPEC_SCLS($$) = S_AUTO;
  553. }
  554. | REGISTER {
  555. $$ = newLink (SPECIFIER);
  556. SPEC_SCLS($$) = S_REGISTER;
  557. }
  558. ;
  559. function_specifier
  560. : INLINE {
  561. $$ = newLink (SPECIFIER);
  562. SPEC_INLINE($$) = 1;
  563. }
  564. | NORETURN {
  565. $$ = newLink (SPECIFIER);
  566. SPEC_NORETURN($$) = 1;
  567. }
  568. ;
  569. Interrupt_storage
  570. : INTERRUPT { $$ = INTNO_UNSPEC; }
  571. | INTERRUPT constant_expr
  572. { int intno = (int) ulFromVal(constExprValue($2,TRUE));
  573. if ((intno >= 0) && (intno <= INTNO_MAX))
  574. $$ = intno;
  575. else
  576. {
  577. werror(E_INT_BAD_INTNO, intno);
  578. $$ = INTNO_UNSPEC;
  579. }
  580. }
  581. ;
  582. type_specifier
  583. : SD_BOOL {
  584. $$=newLink(SPECIFIER);
  585. SPEC_NOUN($$) = V_BOOL;
  586. ignoreTypedefType = 1;
  587. }
  588. | SD_CHAR {
  589. $$=newLink(SPECIFIER);
  590. SPEC_NOUN($$) = V_CHAR;
  591. ignoreTypedefType = 1;
  592. }
  593. | SD_SHORT {
  594. $$=newLink(SPECIFIER);
  595. SPEC_SHORT($$) = 1;
  596. ignoreTypedefType = 1;
  597. }
  598. | SD_INT {
  599. $$=newLink(SPECIFIER);
  600. SPEC_NOUN($$) = V_INT;
  601. ignoreTypedefType = 1;
  602. }
  603. | SD_LONG {
  604. $$=newLink(SPECIFIER);
  605. SPEC_LONG($$) = 1;
  606. ignoreTypedefType = 1;
  607. }
  608. | SIGNED {
  609. $$=newLink(SPECIFIER);
  610. $$->select.s.b_signed = 1;
  611. ignoreTypedefType = 1;
  612. }
  613. | UNSIGNED {
  614. $$=newLink(SPECIFIER);
  615. SPEC_USIGN($$) = 1;
  616. ignoreTypedefType = 1;
  617. }
  618. | SD_VOID {
  619. $$=newLink(SPECIFIER);
  620. SPEC_NOUN($$) = V_VOID;
  621. ignoreTypedefType = 1;
  622. }
  623. | SD_CONST {
  624. $$=newLink(SPECIFIER);
  625. SPEC_CONST($$) = 1;
  626. }
  627. | VOLATILE {
  628. $$=newLink(SPECIFIER);
  629. SPEC_VOLATILE($$) = 1;
  630. }
  631. | RESTRICT {
  632. $$=newLink(SPECIFIER);
  633. SPEC_RESTRICT($$) = 1;
  634. }
  635. | ADDRSPACE_NAME {
  636. $$=newLink(SPECIFIER);
  637. SPEC_ADDRSPACE($$) = findSym (AddrspaceTab, 0, $1);
  638. }
  639. | SD_FLOAT {
  640. $$=newLink(SPECIFIER);
  641. SPEC_NOUN($$) = V_FLOAT;
  642. ignoreTypedefType = 1;
  643. }
  644. | FIXED16X16 {
  645. $$=newLink(SPECIFIER);
  646. SPEC_NOUN($$) = V_FIXED16X16;
  647. ignoreTypedefType = 1;
  648. }
  649. | XDATA {
  650. $$ = newLink (SPECIFIER);
  651. SPEC_SCLS($$) = S_XDATA;
  652. }
  653. | CODE {
  654. $$ = newLink (SPECIFIER);
  655. SPEC_SCLS($$) = S_CODE;
  656. }
  657. | EEPROM {
  658. $$ = newLink (SPECIFIER);
  659. SPEC_SCLS($$) = S_EEPROM;
  660. }
  661. | DATA {
  662. $$ = newLink (SPECIFIER);
  663. SPEC_SCLS($$) = S_DATA;
  664. }
  665. | IDATA {
  666. $$ = newLink (SPECIFIER);
  667. SPEC_SCLS($$) = S_IDATA;
  668. }
  669. | PDATA {
  670. $$ = newLink (SPECIFIER);
  671. SPEC_SCLS($$) = S_PDATA;
  672. }
  673. | BIT {
  674. $$=newLink(SPECIFIER);
  675. SPEC_NOUN($$) = V_BIT;
  676. SPEC_SCLS($$) = S_BIT;
  677. SPEC_BLEN($$) = 1;
  678. SPEC_BSTR($$) = 0;
  679. ignoreTypedefType = 1;
  680. }
  681. | AT constant_expr {
  682. $$=newLink(SPECIFIER);
  683. /* add this to the storage class specifier */
  684. SPEC_ABSA($$) = 1; /* set the absolute addr flag */
  685. /* now get the abs addr from value */
  686. SPEC_ADDR($$) = (unsigned int) ulFromVal(constExprValue($2,TRUE));
  687. }
  688. | struct_or_union_specifier {
  689. uselessDecl = FALSE;
  690. $$ = $1;
  691. ignoreTypedefType = 1;
  692. }
  693. | enum_specifier {
  694. cenum = NULL;
  695. uselessDecl = FALSE;
  696. ignoreTypedefType = 1;
  697. $$ = $1;
  698. }
  699. | TYPE_NAME
  700. {
  701. symbol *sym;
  702. sym_link *p;
  703. sym = findSym(TypedefTab,NULL,$1);
  704. $$ = p = copyLinkChain(sym ? sym->type : NULL);
  705. SPEC_TYPEDEF(getSpec(p)) = 0;
  706. ignoreTypedefType = 1;
  707. }
  708. | sfr_reg_bit
  709. ;
  710. sfr_reg_bit
  711. : SBIT {
  712. $$ = newLink(SPECIFIER);
  713. SPEC_NOUN($$) = V_SBIT;
  714. SPEC_SCLS($$) = S_SBIT;
  715. SPEC_BLEN($$) = 1;
  716. SPEC_BSTR($$) = 0;
  717. ignoreTypedefType = 1;
  718. }
  719. | sfr_attributes
  720. ;
  721. sfr_attributes
  722. : SFR {
  723. $$ = newLink(SPECIFIER);
  724. FUNC_REGBANK($$) = 0;
  725. SPEC_NOUN($$) = V_CHAR;
  726. SPEC_SCLS($$) = S_SFR;
  727. SPEC_USIGN($$) = 1;
  728. ignoreTypedefType = 1;
  729. }
  730. | SFR BANKED {
  731. $$ = newLink(SPECIFIER);
  732. FUNC_REGBANK($$) = 1;
  733. SPEC_NOUN($$) = V_CHAR;
  734. SPEC_SCLS($$) = S_SFR;
  735. SPEC_USIGN($$) = 1;
  736. ignoreTypedefType = 1;
  737. }
  738. ;
  739. sfr_attributes
  740. : SFR16 {
  741. $$ = newLink(SPECIFIER);
  742. FUNC_REGBANK($$) = 0;
  743. SPEC_NOUN($$) = V_INT;
  744. SPEC_SCLS($$) = S_SFR;
  745. SPEC_USIGN($$) = 1;
  746. ignoreTypedefType = 1;
  747. }
  748. ;
  749. sfr_attributes
  750. : SFR32 {
  751. $$ = newLink(SPECIFIER);
  752. FUNC_REGBANK($$) = 0;
  753. SPEC_NOUN($$) = V_INT;
  754. SPEC_SCLS($$) = S_SFR;
  755. SPEC_LONG($$) = 1;
  756. SPEC_USIGN($$) = 1;
  757. ignoreTypedefType = 1;
  758. }
  759. ;
  760. struct_or_union_specifier
  761. : struct_or_union opt_stag
  762. {
  763. structdef *sdef;
  764. if (! $2->tagsym)
  765. {
  766. /* no tag given, so new struct def for current scope */
  767. addSym (StructTab, $2, $2->tag, $2->level, currBlockno, 0);
  768. }
  769. else
  770. {
  771. sdef = findSymWithBlock (StructTab, $2->tagsym, currBlockno);
  772. if (sdef)
  773. {
  774. /* Error if a complete type already defined in this scope */
  775. if (sdef->block == currBlockno)
  776. {
  777. if (sdef->fields)
  778. {
  779. werror(E_STRUCT_REDEF, $2->tag);
  780. werrorfl(sdef->tagsym->fileDef, sdef->tagsym->lineDef, E_PREVIOUS_DEF);
  781. }
  782. else
  783. {
  784. $2 = sdef; /* We are completing an incomplete type */
  785. }
  786. }
  787. else
  788. {
  789. /* There is an existing struct def in an outer scope. */
  790. /* Create new struct def for current scope */
  791. addSym (StructTab, $2, $2->tag, $2->level, currBlockno, 0);
  792. }
  793. }
  794. else
  795. {
  796. /* There is no existing struct def at all. */
  797. /* Create new struct def for current scope */
  798. addSym (StructTab, $2, $2->tag, $2->level, currBlockno, 0);
  799. }
  800. }
  801. if (!$2->type)
  802. {
  803. $2->type = $1;
  804. }
  805. else
  806. {
  807. if ($2->type != $1)
  808. werror(E_BAD_TAG, $2->tag, $1==STRUCT ? "struct" : "union");
  809. }
  810. }
  811. '{' struct_declaration_list '}'
  812. {
  813. structdef *sdef;
  814. symbol *sym, *dsym;
  815. // check for errors in structure members
  816. for (sym=$5; sym; sym=sym->next)
  817. {
  818. if (IS_ABSOLUTE(sym->etype))
  819. {
  820. werrorfl(sym->fileDef, sym->lineDef, E_NOT_ALLOWED, "'at'");
  821. SPEC_ABSA(sym->etype) = 0;
  822. }
  823. if (IS_SPEC(sym->etype) && SPEC_SCLS(sym->etype))
  824. {
  825. werrorfl(sym->fileDef, sym->lineDef, E_NOT_ALLOWED, "storage class");
  826. printTypeChainRaw (sym->type, NULL);
  827. SPEC_SCLS(sym->etype) = 0;
  828. }
  829. for (dsym=sym->next; dsym; dsym=dsym->next)
  830. {
  831. if (*dsym->name && strcmp(sym->name, dsym->name)==0)
  832. {
  833. werrorfl(sym->fileDef, sym->lineDef, E_DUPLICATE_MEMBER,
  834. $1==STRUCT ? "struct" : "union", sym->name);
  835. werrorfl(dsym->fileDef, dsym->lineDef, E_PREVIOUS_DEF);
  836. }
  837. }
  838. }
  839. /* Create a structdef */
  840. sdef = $2;
  841. sdef->fields = reverseSyms($5); /* link the fields */
  842. sdef->size = compStructSize($1, sdef); /* update size of */
  843. promoteAnonStructs ($1, sdef);
  844. /* Create the specifier */
  845. $$ = newLink (SPECIFIER);
  846. SPEC_NOUN($$) = V_STRUCT;
  847. SPEC_STRUCT($$)= sdef;
  848. }
  849. | struct_or_union stag
  850. {
  851. structdef *sdef;
  852. sdef = findSymWithBlock (StructTab, $2->tagsym, currBlockno);
  853. if (sdef)
  854. $2 = sdef;
  855. else
  856. {
  857. /* new struct def for current scope */
  858. addSym (StructTab, $2, $2->tag, $2->level, currBlockno, 0);
  859. }
  860. $$ = newLink(SPECIFIER);
  861. SPEC_NOUN($$) = V_STRUCT;
  862. SPEC_STRUCT($$) = $2;
  863. if (!$2->type)
  864. {
  865. $2->type = $1;
  866. }
  867. else
  868. {
  869. if ($2->type != $1)
  870. werror(E_BAD_TAG, $2->tag, $1==STRUCT ? "struct" : "union");
  871. }
  872. }
  873. ;
  874. struct_or_union
  875. : STRUCT { $$ = STRUCT; ignoreTypedefType = 1; }
  876. | UNION { $$ = UNION; ignoreTypedefType = 1; }
  877. ;
  878. opt_stag
  879. : stag
  880. | { /* synthesize a name add to structtable */
  881. ignoreTypedefType = 0;
  882. $$ = newStruct(genSymName(NestLevel));
  883. $$->level = NestLevel;
  884. $$->block = currBlockno;
  885. $$->tagsym = NULL;
  886. //addSym (StructTab, $$, $$->tag, $$->level, currBlockno, 0);
  887. }
  888. ;
  889. stag
  890. : identifier
  891. { /* add name to structure table */
  892. ignoreTypedefType = 0;
  893. $$ = newStruct($1->name);
  894. $$->level = NestLevel;
  895. $$->block = currBlockno;
  896. $$->tagsym = $1;
  897. //$$ = findSymWithBlock (StructTab, $1, currBlockno);
  898. //if (! $$ )
  899. // {
  900. // $$ = newStruct($1->name);
  901. // $$->level = NestLevel;
  902. // $$->tagsym = $1;
  903. // //addSym (StructTab, $$, $$->tag, $$->level, currBlockno, 0);
  904. // }
  905. }
  906. ;
  907. struct_declaration_list
  908. : struct_declaration
  909. | struct_declaration_list struct_declaration
  910. {
  911. symbol *sym = $2;
  912. /* go to the end of the chain */
  913. while (sym->next) sym = sym->next;
  914. sym->next = $1;
  915. $$ = $2;
  916. }
  917. ;
  918. struct_declaration
  919. : type_specifier_list struct_declarator_list ';'
  920. {
  921. /* add this type to all the symbols */
  922. symbol *sym;
  923. for ( sym = $2; sym != NULL; sym = sym->next )
  924. {
  925. sym_link *btype = copyLinkChain($1);
  926. pointerTypes(sym->type, btype);
  927. if (!sym->type)
  928. {
  929. sym->type = btype;
  930. sym->etype = getSpec(sym->type);
  931. }
  932. else
  933. addDecl (sym, 0, btype);
  934. /* make sure the type is complete and sane */
  935. checkTypeSanity(sym->etype, sym->name);
  936. }
  937. ignoreTypedefType = 0;
  938. $$ = $2;
  939. }
  940. ;
  941. struct_declarator_list
  942. : struct_declarator
  943. | struct_declarator_list ',' struct_declarator
  944. {
  945. $3->next = $1;
  946. $$ = $3;
  947. }
  948. ;
  949. struct_declarator
  950. : declarator
  951. | ':' constant_expr
  952. {
  953. unsigned int bitsize;
  954. $$ = newSymbol (genSymName(NestLevel), NestLevel);
  955. bitsize = (unsigned int) ulFromVal(constExprValue($2, TRUE));
  956. if (bitsize > (port->s.int_size * 8))
  957. {
  958. bitsize = port->s.int_size * 8;
  959. werror(E_BITFLD_SIZE, bitsize);
  960. }
  961. if (!bitsize)
  962. bitsize = BITVAR_PAD;
  963. $$->bitVar = bitsize;
  964. $$->bitUnnamed = 1;
  965. }
  966. | declarator ':' constant_expr
  967. {
  968. unsigned int bitsize;
  969. bitsize = (unsigned int) ulFromVal(constExprValue($3, TRUE));
  970. if (bitsize > (port->s.int_size * 8))
  971. {
  972. bitsize = port->s.int_size * 8;
  973. werror(E_BITFLD_SIZE, bitsize);
  974. }
  975. if (!bitsize)
  976. {
  977. $$ = newSymbol (genSymName(NestLevel), NestLevel);
  978. $$->bitVar = BITVAR_PAD;
  979. werror(W_BITFLD_NAMED);
  980. }
  981. else
  982. $1->bitVar = bitsize;
  983. }
  984. | { $$ = newSymbol ("", NestLevel); }
  985. ;
  986. enum_specifier
  987. : ENUM '{' enumerator_list '}'
  988. {
  989. $$ = newEnumType ($3);
  990. SPEC_SCLS(getSpec($$)) = 0;
  991. }
  992. | ENUM identifier '{' enumerator_list '}'
  993. {
  994. symbol *csym;
  995. sym_link *enumtype;
  996. csym = findSymWithLevel(enumTab, $2);
  997. if ((csym && csym->level == $2->level))
  998. {
  999. werrorfl($2->fileDef, $2->lineDef, E_DUPLICATE_TYPEDEF, csym->name);
  1000. werrorfl(csym->fileDef, csym->lineDef, E_PREVIOUS_DEF);
  1001. }
  1002. enumtype = newEnumType ($4);
  1003. SPEC_SCLS(getSpec(enumtype)) = 0;
  1004. $2->type = enumtype;
  1005. /* add this to the enumerator table */
  1006. if (!csym)
  1007. addSym (enumTab, $2, $2->name, $2->level, $2->block, 0);
  1008. $$ = copyLinkChain(enumtype);
  1009. }
  1010. | ENUM identifier
  1011. {
  1012. symbol *csym;
  1013. /* check the enumerator table */
  1014. if ((csym = findSymWithLevel(enumTab, $2)))
  1015. $$ = copyLinkChain(csym->type);
  1016. else
  1017. {
  1018. $$ = newLink(SPECIFIER);
  1019. SPEC_NOUN($$) = V_INT;
  1020. }
  1021. }
  1022. ;
  1023. enumerator_list
  1024. : enumerator
  1025. | enumerator_list ','
  1026. | enumerator_list ',' enumerator
  1027. {
  1028. $3->next = $1;
  1029. $$ = $3;
  1030. }
  1031. ;
  1032. enumerator
  1033. : identifier opt_assign_expr
  1034. {
  1035. symbol *sym;
  1036. // check if the symbol at the same level already exists
  1037. if ((sym = findSymWithLevel (SymbolTab, $1)) && sym->level == $1->level)
  1038. {
  1039. werrorfl ($1->fileDef, $1->lineDef, E_DUPLICATE_MEMBER, "enum", $1->name);
  1040. werrorfl (sym->fileDef, sym->lineDef, E_PREVIOUS_DEF);
  1041. }
  1042. $1->type = copyLinkChain ($2->type);
  1043. $1->etype = getSpec ($1->type);
  1044. SPEC_ENUM ($1->etype) = 1;
  1045. $$ = $1;
  1046. // do this now, so we can use it for the next enums in the list
  1047. addSymChain (&$1);
  1048. }
  1049. ;
  1050. opt_assign_expr
  1051. : '=' constant_expr
  1052. {
  1053. value *val;
  1054. val = constExprValue($2, TRUE);
  1055. if (!IS_INT(val->type) && !IS_CHAR(val->type) && !IS_BOOL(val->type))
  1056. {
  1057. werror(E_ENUM_NON_INTEGER);
  1058. SNPRINTF(lbuff, sizeof(lbuff), "%d", (int) ulFromVal(val));
  1059. val = constVal(lbuff);
  1060. }
  1061. $$ = cenum = val;
  1062. }
  1063. | {
  1064. if (cenum)
  1065. {
  1066. SNPRINTF(lbuff, sizeof(lbuff), "%d", (int) ulFromVal(cenum)+1);
  1067. $$ = cenum = constVal(lbuff);
  1068. }
  1069. else
  1070. {
  1071. $$ = cenum = constCharVal(0);
  1072. }
  1073. }
  1074. ;
  1075. declarator
  1076. : declarator3 { $$ = $1; }
  1077. | pointer declarator3
  1078. {
  1079. addDecl ($2,0,reverseLink($1));
  1080. $$ = $2;
  1081. }
  1082. ;
  1083. declarator3
  1084. : declarator2_function_attributes { $$ = $1; }
  1085. | declarator2 { $$ = $1; }
  1086. ;
  1087. function_declarator
  1088. : declarator2_function_attributes { $$ = $1; }
  1089. | pointer declarator2_function_attributes
  1090. {
  1091. addDecl ($2,0,reverseLink($1));
  1092. $$ = $2;
  1093. }
  1094. ;
  1095. declarator2_function_attributes
  1096. : function_declarator2 { $$ = $1; }
  1097. | function_declarator2 function_attribute {
  1098. // copy the functionAttributes (not the args and hasVargs !!)
  1099. struct value *args;
  1100. unsigned hasVargs;
  1101. sym_link *funcType=$1->type;
  1102. while (funcType && !IS_FUNC(funcType))
  1103. funcType = funcType->next;
  1104. if (!funcType)
  1105. werror (E_FUNC_ATTR);
  1106. else
  1107. {
  1108. args=FUNC_ARGS(funcType);
  1109. hasVargs=FUNC_HASVARARGS(funcType);
  1110. memcpy (&funcType->funcAttrs, &$2->funcAttrs,
  1111. sizeof($2->funcAttrs));
  1112. FUNC_ARGS(funcType)=args;
  1113. FUNC_HASVARARGS(funcType)=hasVargs;
  1114. // just to be sure
  1115. memset (&$2->funcAttrs, 0,
  1116. sizeof($2->funcAttrs));
  1117. addDecl ($1,0,$2);
  1118. }
  1119. }
  1120. ;
  1121. declarator2
  1122. : identifier
  1123. | '(' declarator ')' { $$ = $2; }
  1124. | declarator3 '[' ']'
  1125. {
  1126. sym_link *p;
  1127. p = newLink (DECLARATOR);
  1128. DCL_TYPE(p) = ARRAY;
  1129. DCL_ELEM(p) = 0;
  1130. addDecl($1,0,p);
  1131. }
  1132. | declarator3 '[' constant_expr ']'
  1133. {
  1134. sym_link *p;
  1135. value *tval;
  1136. int size;
  1137. tval = constExprValue($3, TRUE);
  1138. /* if it is not a constant then Error */
  1139. p = newLink (DECLARATOR);
  1140. DCL_TYPE(p) = ARRAY;
  1141. if (!tval || (SPEC_SCLS(tval->etype) != S_LITERAL))
  1142. {
  1143. werror(E_CONST_EXPECTED);
  1144. /* Assume a single item array to limit the cascade */
  1145. /* of additional errors. */
  1146. size = 1;
  1147. }
  1148. else
  1149. {
  1150. if ((size = (int) ulFromVal(tval)) < 0)
  1151. {
  1152. werror(E_NEGATIVE_ARRAY_SIZE, $1->name);
  1153. size = 1;
  1154. }
  1155. }
  1156. DCL_ELEM(p) = size;
  1157. addDecl($1, 0, p);
  1158. }
  1159. ;
  1160. function_declarator2
  1161. : declarator2 '(' ')'
  1162. {
  1163. addDecl ($1, FUNCTION, NULL);
  1164. }
  1165. | declarator2 '('
  1166. {
  1167. NestLevel++;
  1168. STACK_PUSH(blockNum, currBlockno);
  1169. currBlockno = ++blockNo;
  1170. }
  1171. parameter_type_list ')'
  1172. {
  1173. sym_link *funcType;
  1174. addDecl ($1, FUNCTION, NULL);
  1175. funcType = $1->type;
  1176. while (funcType && !IS_FUNC(funcType))
  1177. funcType = funcType->next;
  1178. assert (funcType);
  1179. FUNC_HASVARARGS(funcType) = IS_VARG($4);
  1180. FUNC_ARGS(funcType) = reverseVal($4);
  1181. /* nest level was incremented to take care of the parms */
  1182. NestLevel--;
  1183. currBlockno = STACK_POP(blockNum);
  1184. // if this was a pointer (to a function)
  1185. if (!IS_FUNC($1->type))
  1186. cleanUpLevel(SymbolTab, NestLevel + 1);
  1187. $$ = $1;
  1188. }
  1189. | declarator2 '(' identifier_list ')'
  1190. {
  1191. werror(E_OLD_STYLE,$1->name);
  1192. /* assume it returns an int */
  1193. $1->type = $1->etype = newIntLink();
  1194. $$ = $1;
  1195. }
  1196. ;
  1197. pointer
  1198. : unqualified_pointer { $$ = $1;}
  1199. | unqualified_pointer type_specifier_list
  1200. {
  1201. $$ = $1;
  1202. if (IS_SPEC($2)) {
  1203. DCL_TSPEC($1) = $2;
  1204. DCL_PTR_CONST($1) = SPEC_CONST($2);
  1205. DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
  1206. DCL_PTR_RESTRICT($1) = SPEC_RESTRICT($2);
  1207. DCL_PTR_ADDRSPACE($1) = SPEC_ADDRSPACE($2);
  1208. }
  1209. else
  1210. werror (W_PTR_TYPE_INVALID);
  1211. }
  1212. | unqualified_pointer pointer
  1213. {
  1214. $$ = $1;
  1215. $$->next = $2;
  1216. DCL_TYPE($2)=port->unqualified_pointer;
  1217. }
  1218. | unqualified_pointer type_specifier_list pointer
  1219. {
  1220. $$ = $1;
  1221. if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
  1222. DCL_PTR_CONST($1) = SPEC_CONST($2);
  1223. DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
  1224. DCL_PTR_RESTRICT($1) = SPEC_RESTRICT($2);
  1225. DCL_PTR_ADDRSPACE($1) = SPEC_ADDRSPACE($2);
  1226. switch (SPEC_SCLS($2)) {
  1227. case S_XDATA:
  1228. DCL_TYPE($3) = FPOINTER;
  1229. break;
  1230. case S_IDATA:
  1231. DCL_TYPE($3) = IPOINTER;
  1232. break;
  1233. case S_PDATA:
  1234. DCL_TYPE($3) = PPOINTER;
  1235. break;
  1236. case S_DATA:
  1237. DCL_TYPE($3) = POINTER;
  1238. break;
  1239. case S_CODE:
  1240. DCL_TYPE($3) = CPOINTER;
  1241. break;
  1242. case S_EEPROM:
  1243. DCL_TYPE($3) = EEPPOINTER;
  1244. break;
  1245. default:
  1246. // this could be just "constant"
  1247. // werror(W_PTR_TYPE_INVALID);
  1248. ;
  1249. }
  1250. }
  1251. else
  1252. werror (W_PTR_TYPE_INVALID);
  1253. $$->next = $3;
  1254. }
  1255. ;
  1256. unqualified_pointer
  1257. : '*'
  1258. {
  1259. $$ = newLink(DECLARATOR);
  1260. DCL_TYPE($$)=UPOINTER;
  1261. }
  1262. ;
  1263. type_specifier_list : type_specifier_list_ { $$ = finalizeSpec($1); };
  1264. type_specifier_list_
  1265. : type_specifier
  1266. //| type_specifier_list_ type_specifier { $$ = mergeSpec ($1,$2, "type_specifier_list"); }
  1267. | type_specifier_list_ type_specifier {
  1268. /* if the decl $2 is not a specifier */
  1269. /* find the spec and replace it */
  1270. $$ = mergeDeclSpec($1, $2, "type_specifier_list type_specifier skipped");
  1271. }
  1272. ;
  1273. identifier_list
  1274. : identifier
  1275. | identifier_list ',' identifier
  1276. {
  1277. $3->next = $1;
  1278. $$ = $3;
  1279. }
  1280. ;
  1281. parameter_type_list
  1282. : parameter_list
  1283. | parameter_list ',' VAR_ARGS { $1->vArgs = 1;}
  1284. ;
  1285. parameter_list
  1286. : parameter_declaration
  1287. | parameter_list ',' parameter_declaration
  1288. {
  1289. $3->next = $1;
  1290. $$ = $3;
  1291. }
  1292. ;
  1293. parameter_declaration
  1294. : declaration_specifiers declarator
  1295. {
  1296. symbol *loop;
  1297. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1298. {
  1299. werror (E_STORAGE_CLASS_FOR_PARAMETER, $2->name);
  1300. }
  1301. pointerTypes ($2->type, $1);
  1302. addDecl ($2, 0, $1);
  1303. for (loop = $2; loop; loop->_isparm = 1, loop = loop->next)
  1304. ;
  1305. addSymChain (&$2);
  1306. $$ = symbolVal ($2);
  1307. ignoreTypedefType = 0;
  1308. }
  1309. | type_name
  1310. {
  1311. $$ = newValue ();
  1312. $$->type = $1;
  1313. $$->etype = getSpec ($$->type);
  1314. ignoreTypedefType = 0;
  1315. }
  1316. ;
  1317. type_name
  1318. : declaration_specifiers
  1319. {
  1320. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1321. {
  1322. werror (E_STORAGE_CLASS_FOR_PARAMETER, "type name");
  1323. }
  1324. $$ = $1; ignoreTypedefType = 0;
  1325. }
  1326. | declaration_specifiers abstract_declarator
  1327. {
  1328. /* go to the end of the list */
  1329. sym_link *p;
  1330. if (IS_SPEC ($1) && !IS_VALID_PARAMETER_STORAGE_CLASS_SPEC ($1))
  1331. {
  1332. werror (E_STORAGE_CLASS_FOR_PARAMETER, "type name");
  1333. }
  1334. pointerTypes ($2,$1);
  1335. for (p = $2; p && p->next; p = p->next)
  1336. ;
  1337. if (!p)
  1338. {
  1339. werror(E_SYNTAX_ERROR, yytext);
  1340. }
  1341. else
  1342. {
  1343. p->next = $1;
  1344. }
  1345. $$ = $2;
  1346. ignoreTypedefType = 0;
  1347. }
  1348. ;
  1349. abstract_declarator
  1350. : pointer { $$ = reverseLink($1); }
  1351. | abstract_declarator2
  1352. | pointer abstract_declarator2 { $1 = reverseLink($1); $1->next = $2; $$ = $1;
  1353. if (IS_PTR($1) && IS_FUNC($2))
  1354. DCL_TYPE($1) = CPOINTER;
  1355. }
  1356. ;
  1357. abstract_declarator2
  1358. : '(' abstract_declarator ')' { $$ = $2; }
  1359. | '[' ']' {
  1360. $$ = newLink (DECLARATOR);
  1361. DCL_TYPE($$) = ARRAY;
  1362. DCL_ELEM($$) = 0;
  1363. }
  1364. | '[' constant_expr ']' {
  1365. value *val;
  1366. $$ = newLink (DECLARATOR);
  1367. DCL_TYPE($$) = ARRAY;
  1368. DCL_ELEM($$) = (int) ulFromVal(val = constExprValue($2,TRUE));
  1369. }
  1370. | abstract_declarator2 '[' ']' {
  1371. $$ = newLink (DECLARATOR);
  1372. DCL_TYPE($$) = ARRAY;
  1373. DCL_ELEM($$) = 0;
  1374. $$->next = $1;
  1375. }
  1376. | abstract_declarator2 '[' constant_expr ']'
  1377. {
  1378. value *val;
  1379. $$ = newLink (DECLARATOR);
  1380. DCL_TYPE($$) = ARRAY;
  1381. DCL_ELEM($$) = (int) ulFromVal(val = constExprValue($3,TRUE));
  1382. $$->next = $1;
  1383. }
  1384. | '(' ')' { $$ = NULL;}
  1385. | '(' parameter_type_list ')' { $$ = NULL;}
  1386. | abstract_declarator2 '(' ')' {
  1387. // $1 must be a pointer to a function
  1388. sym_link *p=newLink(DECLARATOR);
  1389. DCL_TYPE(p) = FUNCTION;
  1390. if (!$1) {
  1391. // ((void (code *) ()) 0) ()
  1392. $1=newLink(DECLARATOR);
  1393. DCL_TYPE($1)=CPOINTER;
  1394. $$ = $1;
  1395. }
  1396. $1->next=p;
  1397. }
  1398. | abstract_declarator2 '('
  1399. {
  1400. NestLevel++;
  1401. STACK_PUSH(blockNum, currBlockno);
  1402. currBlockno = ++blockNo;
  1403. }
  1404. parameter_type_list ')'
  1405. {
  1406. sym_link *p = newLink(DECLARATOR);
  1407. DCL_TYPE(p) = FUNCTION;
  1408. FUNC_HASVARARGS(p) = IS_VARG($4);
  1409. FUNC_ARGS(p) = reverseVal($4);
  1410. /* nest level was incremented to take care of the parms */
  1411. NestLevel--;
  1412. currBlockno = STACK_POP(blockNum);
  1413. if (!$1)
  1414. {
  1415. /* ((void (code *) (void)) 0) () */
  1416. $1 = newLink(DECLARATOR);
  1417. DCL_TYPE($1) = CPOINTER;
  1418. $$ = $1;
  1419. }
  1420. $1->next = p;
  1421. // disabled to fix bug 3190029
  1422. // // remove the symbol args (if any)
  1423. // cleanUpLevel(SymbolTab, NestLevel + 1);
  1424. }
  1425. ;
  1426. initializer
  1427. : assignment_expr { $$ = newiList(INIT_NODE,$1); }
  1428. | '{' initializer_list '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
  1429. | '{' initializer_list ',' '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
  1430. ;
  1431. initializer_list
  1432. : designation_opt initializer { $2->designation = $1; $$ = $2; }
  1433. | initializer_list ',' designation_opt initializer
  1434. {
  1435. $4->designation = $3;
  1436. $4->next = $1;
  1437. $$ = $4;
  1438. }
  1439. ;
  1440. statement
  1441. : labeled_statement
  1442. | compound_statement
  1443. | expression_statement
  1444. | selection_statement
  1445. | iteration_statement
  1446. | jump_statement
  1447. | critical_statement
  1448. | asm_statement
  1449. ;
  1450. critical
  1451. : CRITICAL {
  1452. inCritical++;
  1453. STACK_PUSH(continueStack,NULL);
  1454. STACK_PUSH(breakStack,NULL);
  1455. $$ = NULL;
  1456. }
  1457. ;
  1458. critical_statement
  1459. : critical statement {
  1460. STACK_POP(breakStack);
  1461. STACK_POP(continueStack);
  1462. inCritical--;
  1463. $$ = newNode(CRITICAL,$2,NULL);
  1464. }
  1465. ;
  1466. labeled_statement
  1467. // : identifier ':' statement { $$ = createLabel($1,$3); }
  1468. : identifier ':' { $$ = createLabel($1,NULL);
  1469. $1->isitmp = 0; }
  1470. | CASE constant_expr ':'
  1471. {
  1472. if (STACK_EMPTY(swStk))
  1473. $$ = createCase(NULL,$2,NULL);
  1474. else
  1475. $$ = createCase(STACK_PEEK(swStk),$2,NULL);
  1476. }
  1477. | DEFAULT { $<asts>$ = newNode(DEFAULT,NULL,NULL); } ':'
  1478. {
  1479. if (STACK_EMPTY(swStk))
  1480. $$ = createDefault(NULL,$<asts>2,NULL);
  1481. else
  1482. $$ = createDefault(STACK_PEEK(swStk),$<asts>2,NULL);
  1483. }
  1484. ;
  1485. start_block
  1486. : '{'
  1487. {
  1488. NestLevel++;
  1489. STACK_PUSH(blockNum, currBlockno);
  1490. currBlockno = ++blockNo;
  1491. ignoreTypedefType = 0;
  1492. }
  1493. ;
  1494. end_block
  1495. : '}'
  1496. {
  1497. NestLevel--;
  1498. currBlockno = STACK_POP(blockNum);
  1499. }
  1500. ;
  1501. compound_statement
  1502. : start_block end_block { $$ = createBlock(NULL, NULL); }
  1503. | start_block statement_list end_block { $$ = createBlock(NULL, $2); }
  1504. | start_block declaration_list end_block
  1505. {
  1506. $$ = createBlock($2, NULL);
  1507. cleanUpLevel(StructTab, NestLevel + 1);
  1508. }
  1509. | start_block
  1510. declaration_list statement_list
  1511. end_block
  1512. {
  1513. $$ = createBlock($2, $3);
  1514. cleanUpLevel(StructTab, NestLevel + 1);
  1515. }
  1516. | error ';' { $$ = NULL; }
  1517. ;
  1518. declaration_list
  1519. : declaration
  1520. {
  1521. /* if this is typedef declare it immediately */
  1522. if ( $1 && IS_TYPEDEF($1->etype)) {
  1523. allocVariables ($1);
  1524. $$ = NULL;
  1525. }
  1526. else
  1527. $$ = $1;
  1528. ignoreTypedefType = 0;
  1529. addSymChain(&$1);
  1530. }
  1531. | declaration_list declaration
  1532. {
  1533. symbol *sym;
  1534. /* if this is a typedef */
  1535. if ($2 && IS_TYPEDEF($2->etype)) {
  1536. allocVariables ($2);
  1537. $$ = $1;
  1538. }
  1539. else {
  1540. /* get to the end of the previous decl */
  1541. if ( $1 ) {
  1542. $$ = sym = $1;
  1543. while (sym->next)
  1544. sym = sym->next;
  1545. sym->next = $2;
  1546. }
  1547. else
  1548. $$ = $2;
  1549. }
  1550. ignoreTypedefType = 0;
  1551. addSymChain(&$2);
  1552. }
  1553. ;
  1554. statement_list
  1555. : statement
  1556. | statement_list statement { $$ = newNode(NULLOP,$1,$2);}
  1557. ;
  1558. expression_statement
  1559. : ';' { $$ = NULL;}
  1560. | expr ';' { $$ = $1; seqPointNo++;}
  1561. ;
  1562. else_statement
  1563. : ELSE statement { $$ = $2; }
  1564. | { $$ = NULL; }
  1565. ;
  1566. selection_statement
  1567. : IF '(' expr ')' { seqPointNo++;} statement else_statement
  1568. {
  1569. noLineno++;
  1570. $$ = createIf ($3, $6, $7 );
  1571. $$->lineno = $3->lineno;
  1572. $$->filename = $3->filename;
  1573. noLineno--;
  1574. }
  1575. | SWITCH '(' expr ')' {
  1576. ast *ex;
  1577. static int swLabel = 0;
  1578. seqPointNo++;
  1579. /* create a node for expression */
  1580. ex = newNode(SWITCH,$3,NULL);
  1581. STACK_PUSH(swStk,ex); /* save it in the stack */
  1582. ex->values.switchVals.swNum = swLabel;
  1583. /* now create the label */
  1584. SNPRINTF(lbuff, sizeof(lbuff),
  1585. "_swBrk_%d",swLabel++);
  1586. $<sym>$ = newSymbol(lbuff,NestLevel);
  1587. /* put label in the break stack */
  1588. STACK_PUSH(breakStack,$<sym>$);
  1589. }
  1590. statement {
  1591. /* get back the switch form the stack */
  1592. $$ = STACK_POP(swStk);
  1593. $$->right = newNode (NULLOP,$6,createLabel($<sym>5,NULL));
  1594. STACK_POP(breakStack);
  1595. }
  1596. ;
  1597. while : WHILE { /* create and push the continue , break & body labels */
  1598. static int Lblnum = 0;
  1599. /* continue */
  1600. SNPRINTF (lbuff, sizeof(lbuff), "_whilecontinue_%d",Lblnum);
  1601. STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
  1602. /* break */
  1603. SNPRINTF (lbuff, sizeof(lbuff), "_whilebreak_%d",Lblnum);
  1604. STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
  1605. /* body */
  1606. SNPRINTF (lbuff, sizeof(lbuff), "_whilebody_%d",Lblnum++);
  1607. $$ = newSymbol(lbuff,NestLevel);
  1608. }
  1609. ;
  1610. do : DO { /* create and push the continue , break & body Labels */
  1611. static int Lblnum = 0;
  1612. /* continue */
  1613. SNPRINTF(lbuff, sizeof(lbuff), "_docontinue_%d",Lblnum);
  1614. STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
  1615. /* break */
  1616. SNPRINTF(lbuff, sizeof(lbuff), "_dobreak_%d",Lblnum);
  1617. STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
  1618. /* do body */
  1619. SNPRINTF(lbuff, sizeof(lbuff), "_dobody_%d",Lblnum++);
  1620. $$ = newSymbol (lbuff,NestLevel);
  1621. }
  1622. ;
  1623. for : FOR { /* create & push continue, break & body labels */
  1624. static int Lblnum = 0;
  1625. /* continue */
  1626. SNPRINTF(lbuff, sizeof(lbuff), "_forcontinue_%d",Lblnum);
  1627. STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
  1628. /* break */
  1629. SNPRINTF(lbuff, sizeof(lbuff), "_forbreak_%d",Lblnum);
  1630. STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
  1631. /* body */
  1632. SNPRINTF(lbuff, sizeof(lbuff), "_forbody_%d",Lblnum);
  1633. $$ = newSymbol(lbuff,NestLevel);
  1634. /* condition */
  1635. SNPRINTF(lbuff, sizeof(lbuff), "_forcond_%d",Lblnum++);
  1636. STACK_PUSH(forStack,newSymbol(lbuff,NestLevel));
  1637. }
  1638. ;
  1639. iteration_statement
  1640. : while '(' expr ')' { seqPointNo++;} statement
  1641. {
  1642. noLineno++;
  1643. $$ = createWhile ( $1, STACK_POP(continueStack),
  1644. STACK_POP(breakStack), $3, $6 );
  1645. $$->lineno = $1->lineDef;
  1646. $$->filename = $1->fileDef;
  1647. noLineno--;
  1648. }
  1649. | do statement WHILE '(' expr ')' ';'
  1650. {
  1651. seqPointNo++;
  1652. noLineno++;
  1653. $$ = createDo ( $1 , STACK_POP(continueStack),
  1654. STACK_POP(breakStack), $5, $2);
  1655. $$->lineno = $1->lineDef;
  1656. $$->filename = $1->fileDef;
  1657. noLineno--;
  1658. }
  1659. | for '(' expr_opt ';' expr_opt ';' expr_opt ')' statement
  1660. {
  1661. noLineno++;
  1662. /* if break or continue statement present
  1663. then create a general case loop */
  1664. if (STACK_PEEK(continueStack)->isref ||
  1665. STACK_PEEK(breakStack)->isref) {
  1666. $$ = createFor ($1, STACK_POP(continueStack),
  1667. STACK_POP(breakStack) ,
  1668. STACK_POP(forStack) ,
  1669. $3 , $5 , $7, $9 );
  1670. } else {
  1671. $$ = newNode(FOR,$9,NULL);
  1672. AST_FOR($$,trueLabel) = $1;
  1673. AST_FOR($$,continueLabel) = STACK_POP(continueStack);
  1674. AST_FOR($$,falseLabel) = STACK_POP(breakStack);
  1675. AST_FOR($$,condLabel) = STACK_POP(forStack);
  1676. AST_FOR($$,initExpr) = $3;
  1677. AST_FOR($$,condExpr) = $5;
  1678. AST_FOR($$,loopExpr) = $7;
  1679. }
  1680. noLineno--;
  1681. }
  1682. ;
  1683. expr_opt
  1684. : { $$ = NULL; seqPointNo++; }
  1685. | expr { $$ = $1; seqPointNo++; }
  1686. ;
  1687. jump_statement
  1688. : GOTO identifier ';' {
  1689. $2->islbl = 1;
  1690. $$ = newAst_VALUE(symbolVal($2));
  1691. $$ = newNode(GOTO,$$,NULL);
  1692. }
  1693. | CONTINUE ';' {
  1694. /* make sure continue is in context */
  1695. if (STACK_EMPTY(continueStack) || STACK_PEEK(continueStack) == NULL) {
  1696. werror(E_BREAK_CONTEXT);
  1697. $$ = NULL;
  1698. }
  1699. else {
  1700. $$ = newAst_VALUE(symbolVal(STACK_PEEK(continueStack)));
  1701. $$ = newNode(GOTO,$$,NULL);
  1702. /* mark the continue label as referenced */
  1703. STACK_PEEK(continueStack)->isref = 1;
  1704. }
  1705. }
  1706. | BREAK ';' {
  1707. if (STACK_EMPTY(breakStack) || STACK_PEEK(breakStack) == NULL) {
  1708. werror(E_BREAK_CONTEXT);
  1709. $$ = NULL;
  1710. } else {
  1711. $$ = newAst_VALUE(symbolVal(STACK_PEEK(breakStack)));
  1712. $$ = newNode(GOTO,$$,NULL);
  1713. STACK_PEEK(breakStack)->isref = 1;
  1714. }
  1715. }
  1716. | RETURN ';' {
  1717. seqPointNo++;
  1718. if (inCritical) {
  1719. werror(E_INVALID_CRITICAL);
  1720. $$ = NULL;
  1721. } else {
  1722. $$ = newNode(RETURN,NULL,NULL);
  1723. }
  1724. }
  1725. | RETURN expr ';' {
  1726. seqPointNo++;
  1727. if (inCritical) {
  1728. werror(E_INVALID_CRITICAL);
  1729. $$ = NULL;
  1730. } else {
  1731. $$ = newNode(RETURN,NULL,$2);
  1732. }
  1733. }
  1734. ;
  1735. asm_string_literal
  1736. : STRING_LITERAL
  1737. ;
  1738. asm_statement
  1739. : ASM '(' asm_string_literal ')' ';'
  1740. {
  1741. ast *ex;
  1742. seqPointNo++;
  1743. ex = newNode(INLINEASM, NULL, NULL);
  1744. ex->values.inlineasm = copyStr ($3, NULL);
  1745. seqPointNo++;
  1746. $$ = ex;
  1747. }
  1748. | INLINEASM ';'
  1749. {
  1750. ast *ex;
  1751. seqPointNo++;
  1752. ex = newNode(INLINEASM, NULL, NULL);
  1753. ex->values.inlineasm = strdup($1);
  1754. seqPointNo++;
  1755. $$ = ex;
  1756. }
  1757. ;
  1758. addressmod
  1759. : ADDRESSMOD identifier identifier ';' {
  1760. symbol *sym;
  1761. if ((sym = findSymWithLevel (AddrspaceTab, $3)) && sym->level == $3->level)
  1762. werrorfl (sym->fileDef, sym->lineDef, E_PREVIOUS_DEF);
  1763. if (!findSymWithLevel (SymbolTab, $2))
  1764. werror (E_ID_UNDEF, $2->name);
  1765. addSym (AddrspaceTab, $3, $3->name, $3->level, $3->block, 0);
  1766. sym = findSymWithLevel (AddrspaceTab, $3);
  1767. sym->addressmod[0] = findSymWithLevel (SymbolTab, $2);
  1768. sym->addressmod[1] = 0;
  1769. }
  1770. ;
  1771. identifier
  1772. : IDENTIFIER { $$ = newSymbol ($1, NestLevel); }
  1773. ;
  1774. %%