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

/tags/sdcc-260-pre2/sdcc/src/SDCC.y

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

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