PageRenderTime 62ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/V_2_2_0/sdcc/src/SDCC.y

#
Happy | 1333 lines | 1201 code | 132 blank | 0 comment | 0 complexity | af4559981d667caf5527ea90b09e3b37 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, LGPL-2.1, GPL-3.0
  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. extern int yyerror (char *);
  30. extern FILE *yyin;
  31. extern char srcLstFname[];
  32. int NestLevel = 0 ; /* current NestLevel */
  33. int stackPtr = 1 ; /* stack pointer */
  34. int xstackPtr = 0 ; /* xstack pointer */
  35. int reentrant = 0 ;
  36. int blockNo = 0 ; /* sequential block number */
  37. int currBlockno=0 ;
  38. extern int yylex();
  39. int yyparse(void);
  40. extern int noLineno ;
  41. char lbuff[1024]; /* local buffer */
  42. /* break & continue stacks */
  43. STACK_DCL(continueStack ,symbol *,MAX_NEST_LEVEL)
  44. STACK_DCL(breakStack ,symbol *,MAX_NEST_LEVEL)
  45. STACK_DCL(forStack ,symbol *,MAX_NEST_LEVEL)
  46. STACK_DCL(swStk ,ast *,MAX_NEST_LEVEL)
  47. STACK_DCL(blockNum,int,MAX_NEST_LEVEL*3)
  48. value *cenum = NULL ; /* current enumeration type chain*/
  49. %}
  50. %union {
  51. symbol *sym ; /* symbol table pointer */
  52. structdef *sdef; /* structure definition */
  53. char yychar[SDCC_NAME_MAX+1];
  54. link *lnk ; /* declarator or specifier */
  55. int yyint; /* integer value returned */
  56. value *val ; /* for integer constant */
  57. initList *ilist; /* initial list */
  58. char yyinline[MAX_INLINEASM]; /* inlined assembler code */
  59. ast *asts; /* expression tree */
  60. }
  61. %token <yychar> IDENTIFIER TYPE_NAME
  62. %token <val> CONSTANT STRING_LITERAL
  63. %token SIZEOF
  64. %token PTR_OP INC_OP DEC_OP LEFT_OP RIGHT_OP LE_OP GE_OP EQ_OP NE_OP
  65. %token AND_OP OR_OP
  66. %token <yyint> MUL_ASSIGN DIV_ASSIGN MOD_ASSIGN ADD_ASSIGN
  67. %token <yyint> SUB_ASSIGN LEFT_ASSIGN RIGHT_ASSIGN AND_ASSIGN
  68. %token <yyint> XOR_ASSIGN OR_ASSIGN
  69. %token TYPEDEF EXTERN STATIC AUTO REGISTER CODE EEPROM INTERRUPT SFR AT SBIT
  70. %token REENTRANT USING XDATA DATA IDATA PDATA VAR_ARGS CRITICAL
  71. %token CHAR SHORT INT LONG SIGNED UNSIGNED FLOAT DOUBLE CONST VOLATILE VOID BIT
  72. %token STRUCT UNION ENUM ELIPSIS RANGE FAR _XDATA _CODE _GENERIC _NEAR _PDATA _IDATA _EEPROM
  73. %token CASE DEFAULT IF ELSE SWITCH WHILE DO FOR GOTO CONTINUE BREAK RETURN
  74. %token <yyinline> INLINEASM
  75. %token IFX ADDRESS_OF GET_VALUE_AT_ADDRESS SPIL UNSPIL GETHBIT
  76. %token BITWISEAND UNARYMINUS IPUSH IPOP PCALL ENDFUNCTION JUMPTABLE
  77. %token RRC RLC
  78. %token CAST CALL PARAM NULLOP BLOCK LABEL RECEIVE SEND
  79. %type <yyint> Interrupt_storage
  80. %type <sym> identifier declarator declarator2 enumerator_list enumerator
  81. %type <sym> struct_declarator
  82. %type <sym> struct_declarator_list struct_declaration struct_declaration_list
  83. %type <sym> declaration init_declarator_list init_declarator
  84. %type <sym> declaration_list identifier_list parameter_identifier_list
  85. %type <sym> declarator2_using_reentrant while do for
  86. %type <lnk> pointer type_specifier_list type_specifier type_name
  87. %type <lnk> storage_class_specifier struct_or_union_specifier
  88. %type <lnk> declaration_specifiers sfr_reg_bit type_specifier2
  89. %type <lnk> using_reentrant using_reentrant_interrupt enum_specifier
  90. %type <lnk> abstract_declarator abstract_declarator2 far_near_pointer far_near
  91. %type <val> parameter_type_list parameter_list parameter_declaration opt_assign_expr
  92. %type <sdef> stag opt_stag
  93. %type <asts> primary_expr
  94. %type <asts> postfix_expr unary_expr cast_expr multiplicative_expr
  95. %type <asts> additive_expr shift_expr relational_expr equality_expr
  96. %type <asts> and_expr exclusive_or_expr inclusive_or_expr logical_or_expr
  97. %type <asts> logical_and_expr conditional_expr assignment_expr constant_expr
  98. %type <asts> expr argument_expr_list function_definition expr_opt
  99. %type <asts> statement_list statement labeled_statement compound_statement
  100. %type <asts> expression_statement selection_statement iteration_statement
  101. %type <asts> jump_statement function_body else_statement string_literal
  102. %type <ilist> initializer initializer_list
  103. %type <yyint> unary_operator assignment_operator struct_or_union
  104. %start file
  105. %%
  106. file
  107. : external_definition
  108. | file external_definition
  109. ;
  110. external_definition
  111. : function_definition { blockNo=0;}
  112. | declaration {
  113. addSymChain ($1);
  114. allocVariables ($1) ;
  115. cleanUpLevel (SymbolTab,1);
  116. }
  117. ;
  118. function_definition
  119. : declarator function_body { /* function type not specified */
  120. /* assume it to be 'int' */
  121. addDecl($1,0,newIntLink());
  122. $$ = createFunction($1,$2);
  123. }
  124. | declaration_specifiers declarator function_body
  125. {
  126. pointerTypes($2->type,copyLinkChain($1));
  127. addDecl($2,0,$1);
  128. $$ = createFunction($2,$3);
  129. }
  130. ;
  131. using_reentrant
  132. : using_reentrant_interrupt
  133. | using_reentrant_interrupt using_reentrant { $$ = mergeSpec($1,$2); }
  134. ;
  135. using_reentrant_interrupt
  136. : USING CONSTANT {
  137. $$ = newLink() ;
  138. $$->class = SPECIFIER ;
  139. SPEC_BNKF($$) = 1;
  140. SPEC_BANK($$) = (int) floatFromVal($2);
  141. }
  142. | REENTRANT { $$ = newLink ();
  143. $$->class = SPECIFIER ;
  144. SPEC_RENT($$) = 1;
  145. }
  146. | CRITICAL { $$ = newLink ();
  147. $$->class = SPECIFIER ;
  148. SPEC_CRTCL($$) = 1;
  149. }
  150. | Interrupt_storage
  151. {
  152. $$ = newLink () ;
  153. $$->class = SPECIFIER ;
  154. SPEC_INTN($$) = $1 ;
  155. SPEC_INTRTN($$) = 1;
  156. }
  157. ;
  158. function_body
  159. : compound_statement
  160. | declaration_list compound_statement
  161. {
  162. werror(E_OLD_STYLE,($1 ? $1->name: "")) ;
  163. exit(1);
  164. }
  165. ;
  166. primary_expr
  167. : identifier { $$ = newAst(EX_VALUE,symbolVal($1)); }
  168. | CONSTANT { $$ = newAst(EX_VALUE,$1); }
  169. | string_literal
  170. | '(' expr ')' { $$ = $2 ; }
  171. ;
  172. string_literal
  173. : STRING_LITERAL { $$ = newAst(EX_VALUE,$1); }
  174. ;
  175. postfix_expr
  176. : primary_expr
  177. | postfix_expr '[' expr ']' { $$ = newNode ('[', $1, $3) ; }
  178. | postfix_expr '(' ')' { $$ = newNode (CALL,$1,NULL);
  179. $$->left->funcName = 1;}
  180. | postfix_expr '(' argument_expr_list ')'
  181. {
  182. $$ = newNode (CALL,$1,$3) ; $$->left->funcName = 1;
  183. }
  184. | postfix_expr '.' identifier
  185. {
  186. $3 = newSymbol($3->name,NestLevel);
  187. $3->implicit = 1;
  188. $$ = newNode(PTR_OP,newNode('&',$1,NULL),newAst(EX_VALUE,symbolVal($3)));
  189. /* $$ = newNode('.',$1,newAst(EX_VALUE,symbolVal($3))) ; */
  190. }
  191. | postfix_expr PTR_OP identifier
  192. {
  193. $3 = newSymbol($3->name,NestLevel);
  194. $3->implicit = 1;
  195. $$ = newNode(PTR_OP,$1,newAst(EX_VALUE,symbolVal($3)));
  196. }
  197. | postfix_expr INC_OP
  198. { $$ = newNode(INC_OP,$1,NULL);}
  199. | postfix_expr DEC_OP
  200. { $$ = newNode(DEC_OP,$1,NULL); }
  201. ;
  202. argument_expr_list
  203. : assignment_expr
  204. | assignment_expr ',' argument_expr_list { $$ = newNode(PARAM,$1,$3); }
  205. ;
  206. unary_expr
  207. : postfix_expr
  208. | INC_OP unary_expr { $$ = newNode(INC_OP,NULL,$2); }
  209. | DEC_OP unary_expr { $$ = newNode(DEC_OP,NULL,$2); }
  210. | unary_operator cast_expr { $$ = newNode($1,$2,NULL) ; }
  211. | SIZEOF unary_expr { $$ = newNode(SIZEOF,NULL,$2); }
  212. | SIZEOF '(' type_name ')' { $$ = newAst(EX_VALUE,sizeofOp($3)); }
  213. ;
  214. unary_operator
  215. : '&' { $$ = '&' ;}
  216. | '*' { $$ = '*' ;}
  217. | '+' { $$ = '+' ;}
  218. | '-' { $$ = '-' ;}
  219. | '~' { $$ = '~' ;}
  220. | '!' { $$ = '!' ;}
  221. ;
  222. cast_expr
  223. : unary_expr
  224. | '(' type_name ')' cast_expr { $$ = newNode(CAST,newAst(EX_LINK,$2),$4); }
  225. ;
  226. multiplicative_expr
  227. : cast_expr
  228. | multiplicative_expr '*' cast_expr { $$ = newNode('*',$1,$3);}
  229. | multiplicative_expr '/' cast_expr { $$ = newNode('/',$1,$3);}
  230. | multiplicative_expr '%' cast_expr { $$ = newNode('%',$1,$3);}
  231. ;
  232. additive_expr
  233. : multiplicative_expr
  234. | additive_expr '+' multiplicative_expr { $$=newNode('+',$1,$3);}
  235. | additive_expr '-' multiplicative_expr { $$=newNode('-',$1,$3);}
  236. ;
  237. shift_expr
  238. : additive_expr
  239. | shift_expr LEFT_OP additive_expr { $$ = newNode(LEFT_OP,$1,$3); }
  240. | shift_expr RIGHT_OP additive_expr { $$ = newNode(RIGHT_OP,$1,$3); }
  241. ;
  242. relational_expr
  243. : shift_expr
  244. | relational_expr '<' shift_expr { $$ = newNode('<',$1,$3); }
  245. | relational_expr '>' shift_expr { $$ = newNode('>',$1,$3); }
  246. | relational_expr LE_OP shift_expr {
  247. /* $$ = newNode(LE_OP,$1,$3); */
  248. /* getting 8051 specific here : will change
  249. LE_OP operation to "not greater than" i.e.
  250. ( a <= b ) === ( ! ( a > b )) */
  251. $$ = newNode('!',
  252. newNode('>', $1 , $3 ),
  253. NULL);
  254. }
  255. | relational_expr GE_OP shift_expr {
  256. /* $$ = newNode(GE_OP,$1,$3) ; */
  257. /* getting 8051 specific here : will change
  258. GE_OP operation to "not less than" i.e.
  259. ( a >= b ) === ( ! ( a < b )) */
  260. $$ = newNode('!',
  261. newNode('<', $1 , $3 ),
  262. NULL);
  263. }
  264. ;
  265. equality_expr
  266. : relational_expr
  267. | equality_expr EQ_OP relational_expr { $$ = newNode(EQ_OP,$1,$3);}
  268. | equality_expr NE_OP relational_expr
  269. {
  270. /* $$ = newNode(NE_OP,$1,$3); */
  271. /* NE_OP changed :
  272. expr1 != expr2 is equivalent to
  273. (! expr1 == expr2) */
  274. $$ = newNode('!',
  275. newNode(EQ_OP,$1,$3),
  276. NULL);
  277. }
  278. ;
  279. and_expr
  280. : equality_expr
  281. | and_expr '&' equality_expr { $$ = newNode('&',$1,$3);}
  282. ;
  283. exclusive_or_expr
  284. : and_expr
  285. | exclusive_or_expr '^' and_expr { $$ = newNode('^',$1,$3);}
  286. ;
  287. inclusive_or_expr
  288. : exclusive_or_expr
  289. | inclusive_or_expr '|' exclusive_or_expr { $$ = newNode('|',$1,$3);}
  290. ;
  291. logical_and_expr
  292. : inclusive_or_expr
  293. | logical_and_expr AND_OP inclusive_or_expr
  294. { $$ = newNode(AND_OP,$1,$3);}
  295. ;
  296. logical_or_expr
  297. : logical_and_expr
  298. | logical_or_expr OR_OP logical_and_expr
  299. { $$ = newNode(OR_OP,$1,$3); }
  300. ;
  301. conditional_expr
  302. : logical_or_expr
  303. | logical_or_expr '?' logical_or_expr ':' conditional_expr
  304. {
  305. $$ = newNode(':',$3,$5) ;
  306. $$ = newNode('?',$1,$$) ;
  307. }
  308. ;
  309. assignment_expr
  310. : conditional_expr
  311. | unary_expr assignment_operator assignment_expr
  312. {
  313. switch ($2) {
  314. case '=':
  315. $$ = newNode($2,$1,$3);
  316. break;
  317. case MUL_ASSIGN:
  318. $$ = newNode('=',$1,newNode('*',copyAst($1),$3));
  319. break;
  320. case DIV_ASSIGN:
  321. $$ = newNode('=',$1,newNode('/',copyAst($1),$3));
  322. break;
  323. case ADD_ASSIGN:
  324. $$ = newNode('=',$1,newNode('+',copyAst($1),$3));
  325. break;
  326. case SUB_ASSIGN:
  327. $$ = newNode('=',$1,newNode('-',copyAst($1),$3));
  328. break;
  329. case LEFT_ASSIGN:
  330. $$ = newNode('=',$1,newNode(LEFT_OP,copyAst($1),$3));
  331. break;
  332. case RIGHT_ASSIGN:
  333. $$ = newNode('=',$1,newNode(RIGHT_OP,copyAst($1),$3));
  334. break;
  335. case AND_ASSIGN:
  336. $$ = newNode('=',$1,newNode('&',copyAst($1),$3));
  337. break;
  338. case XOR_ASSIGN:
  339. $$ = newNode('=',$1,newNode('^',copyAst($1),$3));
  340. break;
  341. case OR_ASSIGN:
  342. $$ = newNode('=',$1,newNode('|',copyAst($1),$3));
  343. break;
  344. default :
  345. $$ = NULL;
  346. }
  347. }
  348. ;
  349. assignment_operator
  350. : '=' { $$ = '=' ;}
  351. | MUL_ASSIGN
  352. | DIV_ASSIGN
  353. | MOD_ASSIGN
  354. | ADD_ASSIGN
  355. | SUB_ASSIGN
  356. | LEFT_ASSIGN
  357. | RIGHT_ASSIGN
  358. | AND_ASSIGN
  359. | XOR_ASSIGN
  360. | OR_ASSIGN
  361. ;
  362. expr
  363. : assignment_expr
  364. | expr ',' assignment_expr { $$ = newNode(',',$1,$3);}
  365. ;
  366. constant_expr
  367. : conditional_expr
  368. ;
  369. declaration
  370. : declaration_specifiers ';' { $$ = NULL ; }
  371. | declaration_specifiers init_declarator_list ';'
  372. {
  373. /* add the specifier list to the id */
  374. symbol *sym , *sym1;
  375. for (sym1 = sym = reverseSyms($2);sym != NULL;sym = sym->next) {
  376. link *lnk = copyLinkChain($1);
  377. /* do the pointer stuff */
  378. pointerTypes(sym->type,lnk);
  379. addDecl (sym,0,lnk) ;
  380. }
  381. $$ = sym1 ;
  382. }
  383. ;
  384. declaration_specifiers
  385. : storage_class_specifier { $$ = $1; }
  386. | storage_class_specifier declaration_specifiers {
  387. /* if the decl $2 is not a specifier */
  388. /* find the spec and replace it */
  389. if ( !IS_SPEC($2)) {
  390. link *lnk = $2 ;
  391. while (lnk && !IS_SPEC(lnk->next))
  392. lnk = lnk->next;
  393. lnk->next = mergeSpec($1,lnk->next);
  394. $$ = $2 ;
  395. }
  396. else
  397. $$ = mergeSpec($1,$2);
  398. }
  399. | type_specifier { $$ = $1; }
  400. | type_specifier declaration_specifiers {
  401. /* if the decl $2 is not a specifier */
  402. /* find the spec and replace it */
  403. if ( !IS_SPEC($2)) {
  404. link *lnk = $2 ;
  405. while (lnk && !IS_SPEC(lnk->next))
  406. lnk = lnk->next;
  407. lnk->next = mergeSpec($1,lnk->next);
  408. $$ = $2 ;
  409. }
  410. else
  411. $$ = mergeSpec($1,$2);
  412. }
  413. ;
  414. init_declarator_list
  415. : init_declarator
  416. | init_declarator_list ',' init_declarator { $3->next = $1 ; $$ = $3;}
  417. ;
  418. init_declarator
  419. : declarator { $1->ival = NULL ; }
  420. | declarator '=' initializer { $1->ival = $3 ; }
  421. ;
  422. storage_class_specifier
  423. : TYPEDEF {
  424. $$ = newLink () ;
  425. $$->class = SPECIFIER ;
  426. SPEC_TYPEDEF($$) = 1 ;
  427. }
  428. | EXTERN {
  429. $$ = newLink();
  430. $$->class = SPECIFIER ;
  431. SPEC_EXTR($$) = 1 ;
  432. }
  433. | STATIC {
  434. $$ = newLink ();
  435. $$->class = SPECIFIER ;
  436. SPEC_STAT($$) = 1 ;
  437. }
  438. | AUTO {
  439. $$ = newLink () ;
  440. $$->class = SPECIFIER ;
  441. SPEC_SCLS($$) = S_AUTO ;
  442. }
  443. | REGISTER {
  444. $$ = newLink ();
  445. $$->class = SPECIFIER ;
  446. SPEC_SCLS($$) = S_REGISTER ;
  447. }
  448. ;
  449. Interrupt_storage
  450. : INTERRUPT CONSTANT { $$ = (int) floatFromVal($2) ; }
  451. ;
  452. type_specifier
  453. : type_specifier2
  454. | type_specifier2 AT CONSTANT
  455. {
  456. /* add this to the storage class specifier */
  457. SPEC_ABSA($1) = 1; /* set the absolute addr flag */
  458. /* now get the abs addr from value */
  459. SPEC_ADDR($1) = (int) floatFromVal ($3) ;
  460. }
  461. ;
  462. type_specifier2
  463. : CHAR {
  464. $$=newLink();
  465. $$->class = SPECIFIER ;
  466. SPEC_NOUN($$) = V_CHAR ;
  467. }
  468. | SHORT {
  469. $$=newLink();
  470. $$->class = SPECIFIER ;
  471. SPEC_LONG($$) = 0 ;
  472. SPEC_SHORT($$) = 1 ;
  473. }
  474. | INT {
  475. $$=newLink();
  476. $$->class = SPECIFIER ;
  477. SPEC_NOUN($$) = V_INT ;
  478. }
  479. | LONG {
  480. $$=newLink();
  481. $$->class = SPECIFIER ;
  482. SPEC_LONG($$) = 1 ;
  483. SPEC_SHORT($$) = 0;
  484. }
  485. | SIGNED {
  486. $$=newLink();
  487. $$->class = SPECIFIER ;
  488. SPEC_USIGN($$) = 0 ;
  489. }
  490. | UNSIGNED {
  491. $$=newLink();
  492. $$->class = SPECIFIER ;
  493. SPEC_USIGN($$) = 1 ;
  494. }
  495. | VOID {
  496. $$=newLink();
  497. $$->class = SPECIFIER ;
  498. SPEC_NOUN($$) = V_VOID ;
  499. }
  500. | CONST {
  501. $$=newLink();
  502. $$->class = SPECIFIER ;
  503. SPEC_SCLS($$) = S_CONSTANT ;
  504. SPEC_CONST($$) = 1;
  505. }
  506. | VOLATILE {
  507. $$=newLink();
  508. $$->class = SPECIFIER ;
  509. SPEC_VOLATILE($$) = 1 ;
  510. }
  511. | FLOAT {
  512. $$=newLink();
  513. SPEC_NOUN($$) = V_FLOAT;
  514. $$->class = SPECIFIER ;
  515. }
  516. | XDATA {
  517. $$ = newLink ();
  518. $$->class = SPECIFIER ;
  519. SPEC_SCLS($$) = S_XDATA ;
  520. }
  521. | CODE {
  522. $$ = newLink () ;
  523. $$->class = SPECIFIER ;
  524. SPEC_SCLS($$) = S_CODE ;
  525. }
  526. | EEPROM {
  527. $$ = newLink () ;
  528. $$->class = SPECIFIER ;
  529. SPEC_SCLS($$) = S_EEPROM ;
  530. }
  531. | DATA {
  532. $$ = newLink ();
  533. $$->class = SPECIFIER ;
  534. SPEC_SCLS($$) = S_DATA ;
  535. }
  536. | IDATA {
  537. $$ = newLink ();
  538. $$->class = SPECIFIER ;
  539. SPEC_SCLS($$) = S_IDATA ;
  540. }
  541. | PDATA {
  542. $$ = newLink ();
  543. $$->class = SPECIFIER ;
  544. SPEC_SCLS($$) = S_PDATA ;
  545. }
  546. | BIT {
  547. $$=newLink();
  548. $$->class = SPECIFIER ;
  549. SPEC_NOUN($$) = V_BIT ;
  550. SPEC_SCLS($$) = S_BIT ;
  551. SPEC_BLEN($$) = 1;
  552. SPEC_BSTR($$) = 0;
  553. }
  554. | struct_or_union_specifier
  555. | enum_specifier {
  556. cenum = NULL ;
  557. $$ = $1 ;
  558. }
  559. | TYPE_NAME
  560. {
  561. symbol *sym;
  562. link *p ;
  563. sym = findSym(TypedefTab,NULL,$1) ;
  564. $$ = p = copyLinkChain(sym->type);
  565. SPEC_TYPEDEF(getSpec(p)) = 0;
  566. }
  567. | sfr_reg_bit
  568. ;
  569. sfr_reg_bit
  570. : SBIT {
  571. $$ = newLink() ;
  572. $$->class = SPECIFIER ;
  573. SPEC_NOUN($$) = V_SBIT;
  574. SPEC_SCLS($$) = S_SBIT;
  575. }
  576. | SFR {
  577. $$ = newLink() ;
  578. $$->class = SPECIFIER ;
  579. SPEC_NOUN($$) = V_CHAR;
  580. SPEC_SCLS($$) = S_SFR ;
  581. SPEC_USIGN($$) = 1 ;
  582. }
  583. ;
  584. struct_or_union_specifier
  585. : struct_or_union opt_stag '{' struct_declaration_list '}'
  586. {
  587. structdef *sdef ;
  588. /* Create a structdef */
  589. sdef = $2 ;
  590. sdef->fields = reverseSyms($4) ; /* link the fields */
  591. sdef->size = compStructSize($1,sdef); /* update size of */
  592. /* Create the specifier */
  593. $$ = newLink () ;
  594. $$->class = SPECIFIER ;
  595. SPEC_NOUN($$) = V_STRUCT;
  596. SPEC_STRUCT($$)= sdef ;
  597. }
  598. | struct_or_union stag
  599. {
  600. $$ = newLink() ;
  601. $$->class = SPECIFIER ;
  602. SPEC_NOUN($$) = V_STRUCT;
  603. SPEC_STRUCT($$) = $2 ;
  604. }
  605. ;
  606. struct_or_union
  607. : STRUCT { $$ = STRUCT ; }
  608. | UNION { $$ = UNION ; }
  609. ;
  610. opt_stag
  611. : stag
  612. | { /* synthesize a name add to structtable */
  613. $$ = newStruct(genSymName(NestLevel)) ;
  614. $$->level = NestLevel ;
  615. addSym (StructTab, $$, $$->tag,$$->level,currBlockno) ;
  616. }
  617. ;
  618. stag
  619. : identifier { /* add name to structure table */
  620. $$ = findSymWithBlock (StructTab,$1,currBlockno);
  621. if (! $$ ) {
  622. $$ = newStruct($1->name) ;
  623. $$->level = NestLevel ;
  624. addSym (StructTab, $$, $$->tag,$$->level,currBlockno) ;
  625. }
  626. }
  627. ;
  628. struct_declaration_list
  629. : struct_declaration
  630. | struct_declaration_list struct_declaration
  631. {
  632. symbol *sym = $2;
  633. /* go to the end of the chain */
  634. while (sym->next) sym = sym->next;
  635. sym->next = $1 ;
  636. $$ = $2;
  637. }
  638. ;
  639. struct_declaration
  640. : type_specifier_list struct_declarator_list ';'
  641. {
  642. /* add this type to all the symbols */
  643. symbol *sym ;
  644. for ( sym = $2 ; sym != NULL ; sym = sym->next ) {
  645. pointerTypes(sym->type,copyLinkChain($1));
  646. if (!sym->type) {
  647. sym->type = copyLinkChain($1);
  648. sym->etype = getSpec(sym->type);
  649. }
  650. else
  651. addDecl (sym,0,cloneSpec($1));
  652. }
  653. $$ = $2;
  654. }
  655. ;
  656. struct_declarator_list
  657. : struct_declarator
  658. | struct_declarator_list ',' struct_declarator
  659. {
  660. $3->next = $1 ;
  661. $$ = $3 ;
  662. }
  663. ;
  664. struct_declarator
  665. : declarator
  666. | ':' constant_expr {
  667. $$ = newSymbol (genSymName(NestLevel),NestLevel) ;
  668. $$->bitVar = (int) floatFromVal(constExprValue($2,TRUE));
  669. }
  670. | declarator ':' constant_expr
  671. {
  672. $1->bitVar = (int) floatFromVal(constExprValue($3,TRUE));
  673. }
  674. ;
  675. enum_specifier
  676. : ENUM '{' enumerator_list '}' {
  677. addSymChain ($3);
  678. allocVariables(reverseSyms($3)) ;
  679. $$ = copyLinkChain(cenum->type);
  680. }
  681. | ENUM identifier '{' enumerator_list '}' {
  682. symbol *csym ;
  683. $2->type = copyLinkChain(cenum->type);
  684. $2->etype = getSpec($2->type);
  685. /* add this to the enumerator table */
  686. if (!(csym=findSym(enumTab,$2,$2->name)) &&
  687. (csym && csym->level == $2->level))
  688. werror(E_DUPLICATE_TYPEDEF,csym->name);
  689. addSym ( enumTab,$2,$2->name,$2->level,$2->block);
  690. addSymChain ($4);
  691. allocVariables (reverseSyms($4));
  692. $$ = copyLinkChain(cenum->type);
  693. SPEC_SCLS(getSpec($$)) = 0 ;
  694. }
  695. | ENUM identifier {
  696. symbol *csym ;
  697. /* check the enumerator table */
  698. if ((csym = findSym(enumTab,$2,$2->name)))
  699. $$ = copyLinkChain(csym->type);
  700. else {
  701. $$ = newLink() ;
  702. $$->class = SPECIFIER ;
  703. SPEC_NOUN($$) = V_INT ;
  704. }
  705. SPEC_SCLS(getSpec($$)) = 0 ;
  706. }
  707. ;
  708. enumerator_list
  709. : enumerator
  710. | enumerator_list ',' enumerator {
  711. $3->next = $1 ;
  712. $$ = $3 ;
  713. }
  714. ;
  715. enumerator
  716. : identifier opt_assign_expr {
  717. /* make the symbol one level up */
  718. $1->level-- ;
  719. $1->type = copyLinkChain($2->type);
  720. $1->etype= getSpec($1->type);
  721. SPEC_ENUM($1->etype) = 1;
  722. $$ = $1 ;
  723. }
  724. ;
  725. opt_assign_expr
  726. : '=' constant_expr {
  727. value *val ;
  728. val = constExprValue($2,TRUE);
  729. $$ = cenum = val ;
  730. }
  731. | {
  732. if (cenum) {
  733. sprintf(lbuff,"%d",(int) floatFromVal(cenum)+1);
  734. $$ = cenum = constVal(lbuff);
  735. }
  736. else {
  737. sprintf(lbuff,"%d",0);
  738. $$ = cenum = constVal(lbuff);
  739. }
  740. }
  741. ;
  742. declarator
  743. : declarator2_using_reentrant { $$ = $1; }
  744. | pointer declarator2_using_reentrant
  745. {
  746. addDecl ($2,0,reverseLink($1));
  747. $$ = $2 ;
  748. }
  749. ;
  750. declarator2_using_reentrant
  751. : declarator2 { $$ = $1 ; }
  752. | declarator2 using_reentrant { addDecl ($1,0,$2); }
  753. ;
  754. declarator2
  755. : identifier
  756. | '(' declarator ')' { $$ = $2; }
  757. | declarator2 '[' ']'
  758. {
  759. link *p;
  760. p = newLink ();
  761. DCL_TYPE(p) = ARRAY ;
  762. DCL_ELEM(p) = 0 ;
  763. addDecl($1,0,p);
  764. }
  765. | declarator2 '[' constant_expr ']'
  766. {
  767. link *p ;
  768. value *tval;
  769. p = (tval = constExprValue($3,TRUE))->etype;
  770. /* if it is not a constant then Error */
  771. if ( SPEC_SCLS(p) != S_LITERAL)
  772. werror(E_CONST_EXPECTED) ;
  773. else {
  774. p = newLink ();
  775. DCL_TYPE(p) = ARRAY ;
  776. DCL_ELEM(p) = (int) floatFromVal(tval) ;
  777. addDecl($1,0,p);
  778. }
  779. }
  780. | declarator2 '(' ')' { addDecl ($1,FUNCTION,NULL) ; }
  781. | declarator2 '(' { NestLevel++ ; currBlockno++; } parameter_type_list ')'
  782. {
  783. addDecl ($1,FUNCTION,NULL) ;
  784. $1->hasVargs = IS_VARG($4);
  785. $1->args = reverseVal($4) ;
  786. /* nest level was incremented to take care of the parms */
  787. NestLevel-- ;
  788. currBlockno--;
  789. $$ = $1;
  790. }
  791. | declarator2 '(' parameter_identifier_list ')'
  792. {
  793. werror(E_OLD_STYLE,$1->name) ;
  794. /* assume it returns an it */
  795. $1->type = $1->etype = newIntLink();
  796. $$ = $1 ;
  797. }
  798. ;
  799. pointer
  800. : far_near_pointer { $$ = $1 ;}
  801. | far_near_pointer type_specifier_list
  802. {
  803. $$ = $1 ;
  804. DCL_TSPEC($1) = $2;
  805. }
  806. | far_near_pointer pointer
  807. {
  808. $$ = $1 ;
  809. $$->next = $2 ;
  810. }
  811. | far_near_pointer type_specifier_list pointer
  812. {
  813. $$ = $1 ;
  814. if (IS_SPEC($2) && DCL_TYPE($3) == UPOINTER) {
  815. DCL_PTR_CONST($1) = SPEC_CONST($2);
  816. DCL_PTR_VOLATILE($1) = SPEC_VOLATILE($2);
  817. switch (SPEC_SCLS($2)) {
  818. case S_XDATA:
  819. DCL_TYPE($3) = FPOINTER;
  820. break;
  821. case S_IDATA:
  822. DCL_TYPE($3) = IPOINTER ;
  823. break;
  824. case S_PDATA:
  825. DCL_TYPE($3) = PPOINTER ;
  826. break;
  827. case S_DATA:
  828. DCL_TYPE($3) = POINTER ;
  829. break;
  830. case S_CODE:
  831. DCL_PTR_CONST($3) = 1;
  832. DCL_TYPE($3) = CPOINTER ;
  833. case S_EEPROM:
  834. DCL_TYPE($3) = EEPPOINTER;
  835. break;
  836. default:
  837. werror(W_PTR_TYPE_INVALID);
  838. }
  839. }
  840. else
  841. werror (W_PTR_TYPE_INVALID);
  842. $$->next = $3 ;
  843. }
  844. ;
  845. far_near_pointer
  846. : far_near '*' {
  847. if ($1 == NULL) {
  848. $$ = newLink();
  849. DCL_TYPE($$) = POINTER ;
  850. }
  851. else
  852. $$ = $1 ;
  853. }
  854. ;
  855. far_near
  856. : _XDATA { $$ = newLink() ; DCL_TYPE($$) = FPOINTER ; }
  857. | _CODE { $$ = newLink() ; DCL_TYPE($$) = CPOINTER ; DCL_PTR_CONST($$) = 1;}
  858. | _PDATA { $$ = newLink() ; DCL_TYPE($$) = PPOINTER ; }
  859. | _IDATA { $$ = newLink() ; DCL_TYPE($$) = IPOINTER ; }
  860. | _NEAR { $$ = NULL ; }
  861. | _GENERIC { $$ = newLink() ; DCL_TYPE($$) = GPOINTER ; }
  862. | _EEPROM { $$ = newLink() ; DCL_TYPE($$) = EEPPOINTER ;}
  863. | { $$ = newLink() ; DCL_TYPE($$) = UPOINTER ; }
  864. ;
  865. type_specifier_list
  866. : type_specifier
  867. | type_specifier_list type_specifier { $$ = mergeSpec ($1,$2); }
  868. ;
  869. parameter_identifier_list
  870. : identifier_list
  871. | identifier_list ',' ELIPSIS
  872. ;
  873. identifier_list
  874. : identifier
  875. | identifier_list ',' identifier
  876. {
  877. $3->next = $1;
  878. $$ = $3 ;
  879. }
  880. ;
  881. parameter_type_list
  882. : parameter_list
  883. | parameter_list ',' VAR_ARGS { $1->vArgs = 1;}
  884. ;
  885. parameter_list
  886. : parameter_declaration
  887. | parameter_list ',' parameter_declaration
  888. {
  889. $3->next = $1 ;
  890. $$ = $3 ;
  891. }
  892. ;
  893. parameter_declaration
  894. : type_specifier_list declarator
  895. {
  896. symbol *loop ;
  897. pointerTypes($2->type,$1);
  898. addDecl ($2,0,$1);
  899. for (loop=$2;loop;loop->_isparm=1,loop=loop->next);
  900. addSymChain ($2);
  901. $$ = symbolVal($2);
  902. }
  903. | type_name {
  904. $$ = newValue() ;
  905. $$->type = $1;
  906. $$->etype = getSpec($$->type);
  907. }
  908. ;
  909. type_name
  910. : type_specifier_list { $$ = $1 ;}
  911. | type_specifier_list abstract_declarator
  912. {
  913. /* go to the end of the list */
  914. link *p;
  915. pointerTypes($2,$1);
  916. for ( p = $2 ; p->next ; p=p->next);
  917. p->next = $1 ;
  918. $$ = $2 ;
  919. }
  920. ;
  921. abstract_declarator
  922. : pointer { $$ = reverseLink($1); }
  923. | abstract_declarator2
  924. | pointer abstract_declarator2 { $1 = reverseLink($1); $1->next = $2 ; $$ = $1;}
  925. ;
  926. abstract_declarator2
  927. : '(' abstract_declarator ')' { $$ = $2 ; }
  928. | '[' ']' {
  929. $$ = newLink ();
  930. DCL_TYPE($$) = ARRAY ;
  931. DCL_ELEM($$) = 0 ;
  932. }
  933. | '[' constant_expr ']' {
  934. value *val ;
  935. $$ = newLink ();
  936. DCL_TYPE($$) = ARRAY ;
  937. DCL_ELEM($$) = (int) floatFromVal(val = constExprValue($2,TRUE));
  938. }
  939. | abstract_declarator2 '[' ']' {
  940. $$ = newLink ();
  941. DCL_TYPE($$) = ARRAY ;
  942. DCL_ELEM($$) = 0 ;
  943. $$->next = $1 ;
  944. }
  945. | abstract_declarator2 '[' constant_expr ']'
  946. {
  947. value *val ;
  948. $$ = newLink ();
  949. DCL_TYPE($$) = ARRAY ;
  950. DCL_ELEM($$) = (int) floatFromVal(val = constExprValue($3,TRUE));
  951. $$->next = $1 ;
  952. }
  953. | '(' ')' { $$ = NULL;}
  954. | '(' parameter_type_list ')' { $$ = NULL;}
  955. | abstract_declarator2 '(' ')'
  956. | abstract_declarator2 '(' parameter_type_list ')'
  957. ;
  958. initializer
  959. : assignment_expr { $$ = newiList(INIT_NODE,$1); }
  960. | '{' initializer_list '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
  961. | '{' initializer_list ',' '}' { $$ = newiList(INIT_DEEP,revinit($2)); }
  962. ;
  963. initializer_list
  964. : initializer
  965. | initializer_list ',' initializer { $3->next = $1; $$ = $3; }
  966. ;
  967. statement
  968. : labeled_statement
  969. | compound_statement
  970. | expression_statement
  971. | selection_statement
  972. | iteration_statement
  973. | jump_statement
  974. | INLINEASM ';' {
  975. ast *ex = newNode(INLINEASM,NULL,NULL);
  976. ALLOC_ATOMIC(ex->values.inlineasm,strlen($1));
  977. strcpy(ex->values.inlineasm,$1);
  978. $$ = ex;
  979. }
  980. ;
  981. labeled_statement
  982. : identifier ':' statement { $$ = createLabel($1,$3); }
  983. | CASE constant_expr ':' statement { $$ = createCase(STACK_PEEK(swStk),$2,$4); }
  984. | DEFAULT ':' statement { $$ = createDefault(STACK_PEEK(swStk),$3); }
  985. ;
  986. start_block : '{' { STACK_PUSH(blockNum,currBlockno); currBlockno = ++blockNo ; }
  987. ;
  988. end_block : '}' { currBlockno = STACK_POP(blockNum); }
  989. ;
  990. compound_statement
  991. : start_block end_block { $$ = createBlock(NULL,NULL); }
  992. | start_block statement_list end_block { $$ = createBlock(NULL,$2) ; }
  993. | start_block
  994. declaration_list { addSymChain($2); }
  995. end_block { $$ = createBlock($2,NULL) ; }
  996. | start_block
  997. declaration_list { addSymChain ($2); }
  998. statement_list
  999. end_block {$$ = createBlock($2,$4) ; }
  1000. | error ';' { $$ = NULL ; }
  1001. ;
  1002. declaration_list
  1003. : declaration
  1004. {
  1005. /* if this is typedef declare it immediately */
  1006. if ( $1 && IS_TYPEDEF($1->etype)) {
  1007. allocVariables ($1);
  1008. $$ = NULL ;
  1009. }
  1010. else
  1011. $$ = $1 ;
  1012. }
  1013. | declaration_list declaration
  1014. {
  1015. symbol *sym;
  1016. /* if this is a typedef */
  1017. if ($2 && IS_TYPEDEF($2->etype)) {
  1018. allocVariables ($2);
  1019. $$ = $1 ;
  1020. }
  1021. else {
  1022. /* get to the end of the previous decl */
  1023. if ( $1 ) {
  1024. $$ = sym = $1 ;
  1025. while (sym->next)
  1026. sym = sym->next ;
  1027. sym->next = $2;
  1028. }
  1029. else
  1030. $$ = $2 ;
  1031. }
  1032. }
  1033. ;
  1034. statement_list
  1035. : statement
  1036. | statement_list statement { $$ = newNode(NULLOP,$1,$2) ;}
  1037. ;
  1038. expression_statement
  1039. : ';' { $$ = NULL;}
  1040. | expr ';'
  1041. ;
  1042. else_statement
  1043. : ELSE statement { $$ = $2 ; }
  1044. | { $$ = NULL;}
  1045. ;
  1046. selection_statement
  1047. : IF '(' expr ')' statement else_statement { noLineno++ ; $$ = createIf ($3, $5, $6 ); noLineno--;}
  1048. | SWITCH '(' expr ')' {
  1049. ast *ex ;
  1050. static int swLabel = 0 ;
  1051. /* create a node for expression */
  1052. ex = newNode(SWITCH,$3,NULL);
  1053. STACK_PUSH(swStk,ex); /* save it in the stack */
  1054. ex->values.switchVals.swNum = swLabel ;
  1055. /* now create the label */
  1056. sprintf(lbuff,"_swBrk_%d",swLabel++);
  1057. $<sym>$ = newSymbol(lbuff,NestLevel);
  1058. /* put label in the break stack */
  1059. STACK_PUSH(breakStack,$<sym>$);
  1060. }
  1061. statement {
  1062. /* get back the switch form the stack */
  1063. $$ = STACK_POP(swStk) ;
  1064. $$->right = newNode (NULLOP,$6,createLabel($<sym>5,NULL));
  1065. STACK_POP(breakStack);
  1066. }
  1067. ;
  1068. while : WHILE { /* create and push the continue , break & body labels */
  1069. static int Lblnum = 0 ;
  1070. /* continue */
  1071. sprintf (lbuff,"_whilecontinue_%d",Lblnum);
  1072. STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
  1073. /* break */
  1074. sprintf (lbuff,"_whilebreak_%d",Lblnum);
  1075. STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
  1076. /* body */
  1077. sprintf (lbuff,"_whilebody_%d",Lblnum++);
  1078. $$ = newSymbol(lbuff,NestLevel);
  1079. }
  1080. do : DO { /* create and push the continue , break & body Labels */
  1081. static int Lblnum = 0 ;
  1082. /* continue */
  1083. sprintf(lbuff,"_docontinue_%d",Lblnum);
  1084. STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
  1085. /* break */
  1086. sprintf (lbuff,"_dobreak_%d",Lblnum);
  1087. STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
  1088. /* do body */
  1089. sprintf (lbuff,"_dobody_%d",Lblnum++);
  1090. $$ = newSymbol (lbuff,NestLevel);
  1091. }
  1092. for : FOR { /* create & push continue, break & body labels */
  1093. static int Lblnum = 0 ;
  1094. /* continue */
  1095. sprintf (lbuff,"_forcontinue_%d",Lblnum);
  1096. STACK_PUSH(continueStack,newSymbol(lbuff,NestLevel));
  1097. /* break */
  1098. sprintf (lbuff,"_forbreak_%d",Lblnum);
  1099. STACK_PUSH(breakStack,newSymbol(lbuff,NestLevel));
  1100. /* body */
  1101. sprintf (lbuff,"_forbody_%d",Lblnum);
  1102. $$ = newSymbol(lbuff,NestLevel);
  1103. /* condition */
  1104. sprintf (lbuff,"_forcond_%d",Lblnum++);
  1105. STACK_PUSH(forStack,newSymbol(lbuff,NestLevel));
  1106. }
  1107. iteration_statement
  1108. : while '(' expr ')' statement
  1109. {
  1110. noLineno++ ;
  1111. $$ = createWhile ( $1, STACK_POP(continueStack),
  1112. STACK_POP(breakStack), $3, $5 );
  1113. $$->lineno = $1->lineDef ;
  1114. noLineno-- ;
  1115. }
  1116. | do statement WHILE '(' expr ')' ';'
  1117. {
  1118. noLineno++ ;
  1119. $$ = createDo ( $1 , STACK_POP(continueStack),
  1120. STACK_POP(breakStack), $5, $2);
  1121. $$->lineno = $1->lineDef ;
  1122. noLineno-- ;
  1123. }
  1124. | for '(' expr_opt ';' expr_opt ';' expr_opt ')' statement
  1125. {
  1126. noLineno++ ;
  1127. /* if break or continue statement present
  1128. then create a general case loop */
  1129. if (STACK_PEEK(continueStack)->isref ||
  1130. STACK_PEEK(breakStack)->isref) {
  1131. $$ = createFor ($1, STACK_POP(continueStack),
  1132. STACK_POP(breakStack) ,
  1133. STACK_POP(forStack) ,
  1134. $3 , $5 , $7, $9 );
  1135. } else {
  1136. $$ = newNode(FOR,$9,NULL);
  1137. AST_FOR($$,trueLabel) = $1;
  1138. AST_FOR($$,continueLabel) = STACK_POP(continueStack);
  1139. AST_FOR($$,falseLabel) = STACK_POP(breakStack);
  1140. AST_FOR($$,condLabel) = STACK_POP(forStack) ;
  1141. AST_FOR($$,initExpr) = $3;
  1142. AST_FOR($$,condExpr) = $5;
  1143. AST_FOR($$,loopExpr) = $7;
  1144. }
  1145. noLineno-- ;
  1146. }
  1147. ;
  1148. expr_opt
  1149. : { $$ = NULL ; }
  1150. | expr
  1151. ;
  1152. jump_statement
  1153. : GOTO identifier ';' {
  1154. $2->islbl = 1;
  1155. $$ = newAst(EX_VALUE,symbolVal($2));
  1156. $$ = newNode(GOTO,$$,NULL);
  1157. }
  1158. | CONTINUE ';' {
  1159. /* make sure continue is in context */
  1160. if (STACK_PEEK(continueStack) == NULL) {
  1161. werror(E_BREAK_CONTEXT);
  1162. $$ = NULL;
  1163. }
  1164. else {
  1165. $$ = newAst(EX_VALUE,symbolVal(STACK_PEEK(continueStack)));
  1166. $$ = newNode(GOTO,$$,NULL);
  1167. /* mark the continue label as referenced */
  1168. STACK_PEEK(continueStack)->isref = 1;
  1169. }
  1170. }
  1171. | BREAK ';' {
  1172. if (STACK_PEEK(breakStack) == NULL) {
  1173. werror(E_BREAK_CONTEXT);
  1174. $$ = NULL;
  1175. } else {
  1176. $$ = newAst(EX_VALUE,symbolVal(STACK_PEEK(breakStack)));
  1177. $$ = newNode(GOTO,$$,NULL);
  1178. STACK_PEEK(breakStack)->isref = 1;
  1179. }
  1180. }
  1181. | RETURN ';' { $$ = newNode(RETURN,NULL,NULL) ; }
  1182. | RETURN expr ';' { $$ = newNode(RETURN,NULL,$2) ; }
  1183. ;
  1184. identifier
  1185. : IDENTIFIER { $$ = newSymbol ($1,NestLevel) ; }
  1186. ;
  1187. %%
  1188. extern unsigned char *yytext;
  1189. extern int column;
  1190. extern char *filename;
  1191. extern int fatalError;
  1192. int yyerror(char *s)
  1193. {
  1194. fflush(stdout);
  1195. if ( yylineno )
  1196. fprintf(stderr,"\n%s(%d) %s: token -> '%s' ; column %d\n",
  1197. filename,yylineno,
  1198. s,yytext,column);
  1199. fatalError++;
  1200. return 0;
  1201. }