PageRenderTime 65ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

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

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