PageRenderTime 51ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/sdcc-300-pre1/sdcc/src/SDCC.y

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

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