PageRenderTime 65ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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

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