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

/branches/sdcc-240/sdcc/src/SDCC.y

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

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