Acse.y 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. %{
  2. /*
  3. * Andrea Di Biagio
  4. * Politecnico di Milano, 2007
  5. *
  6. * Acse.y
  7. * Formal Languages & Compilers Machine, 2007/2008
  8. *
  9. */
  10. /*************************************************************************
  11. Compiler for the language LANCE
  12. ***************************************************************************/
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include "axe_struct.h"
  17. #include "axe_engine.h"
  18. #include "symbol_table.h"
  19. #include "axe_errors.h"
  20. #include "collections.h"
  21. #include "axe_expressions.h"
  22. #include "axe_gencode.h"
  23. #include "axe_utils.h"
  24. #include "axe_array.h"
  25. #include "axe_cflow_graph.h"
  26. #include "cflow_constants.h"
  27. #include "axe_transform.h"
  28. #include "axe_reg_alloc.h"
  29. #include "reg_alloc_constants.h"
  30. #include "axe_io_manager.h"
  31. #ifndef NDEBUG
  32. # include "axe_debug.h"
  33. #endif
  34. /* global variables */
  35. int line_num; /* this variable will keep track of the
  36. * source code line number. Every time that a newline
  37. * is encountered while parsing the input file, this
  38. * value is increased by 1. This value is then used
  39. * for error tracking: if the parser returns an error
  40. * or a warning, this value is used in order to notify
  41. * in which line of code the error has been found */
  42. int num_error; /* the number of errors found in the code. This value
  43. * is increased by 1 every time a new error is found
  44. * in the code. */
  45. int num_warning; /* As for the `num_error' global variable, this one
  46. * keeps track of all the warning messages displayed */
  47. /* errorcode is defined inside "axe_engine.c" */
  48. extern int errorcode; /* this variable is used to test if an error is found
  49. * while parsing the input file. It also is set
  50. * to notify if the compiler internal state is invalid.
  51. * When the parsing process is started, the value
  52. * of `errorcode' is set to the value of the macro
  53. * `AXE_OK' defined in "axe_constants.h".
  54. * As long as everything (the parsed source code and
  55. * the internal state of the compiler) is correct,
  56. * the value of `errorcode' is set to `AXE_OK'.
  57. * When an error occurs (because the input file contains
  58. * one or more syntax errors or because something went
  59. * wrong in the machine internal state), the errorcode
  60. * is set to a value that is different from `AXE_OK'. */
  61. extern int cflow_errorcode; /* As for `errorcode' this value is used to
  62. * test if an error occurs during the creation process of
  63. * a control flow graph. More informations can be found
  64. * analyzing the file `axe_cflow_graph.h'. */
  65. /* program informations */
  66. t_program_infos *program; /* The singleton instance of `program'.
  67. * An instance of `t_program_infos' holds in its
  68. * internal structure, all the useful informations
  69. * about a program. For example: the assembly
  70. * (code and directives); the symbol table;
  71. * the label manager (see axe_labels.h) etc. */
  72. t_cflow_Graph *graph; /* An instance of a control flow graph. This instance
  73. * will be generated starting from `program' and will
  74. * be used during the register allocation process */
  75. t_reg_allocator *RA; /* Register allocator. It implements the "Linear scan"
  76. * algorythm */
  77. t_io_infos *file_infos; /* input and output files used by the compiler */
  78. %}
  79. %expect 1
  80. /*=========================================================================
  81. SEMANTIC RECORDS
  82. =========================================================================*/
  83. %union {
  84. int intval;
  85. char *svalue;
  86. t_axe_expression expr;
  87. t_axe_declaration *decl;
  88. t_list *list;
  89. t_axe_label *label;
  90. t_while_statement while_stmt;
  91. }
  92. /*=========================================================================
  93. TOKENS
  94. =========================================================================*/
  95. %start program
  96. %token LBRACE RBRACE LPAR RPAR LSQUARE RSQUARE
  97. %token SEMI COLON PLUS MINUS MUL_OP DIV_OP MOD_OP
  98. %token AND_OP OR_OP NOT_OP
  99. %token ASSIGN LT GT SHL_OP SHR_OP EQ NOTEQ LTEQ GTEQ
  100. %token ANDAND OROR
  101. %token COMMA
  102. %token FOR
  103. %token RETURN
  104. %token READ
  105. %token WRITE
  106. %token AS
  107. %token LDSQUARE RDSQUARE
  108. %token <label> DO
  109. %token <while_stmt> WHILE
  110. %token <label> IF
  111. %token <label> ELSE
  112. %token <intval> TYPE
  113. %token <svalue> IDENTIFIER
  114. %token <intval> NUMBER
  115. %token <intval> MAP
  116. %token <label> ON REDUCE INTO
  117. %type <expr> exp
  118. %type <decl> declaration
  119. %type <list> declaration_list
  120. %type <label> if_stmt
  121. /*=========================================================================
  122. OPERATOR PRECEDENCES
  123. =========================================================================*/
  124. %left COMMA
  125. %left ASSIGN
  126. %left OROR
  127. %left ANDAND
  128. %left OR_OP
  129. %left AND_OP
  130. %left EQ NOTEQ
  131. %left LT GT LTEQ GTEQ
  132. %left SHL_OP SHR_OP
  133. %left MINUS PLUS
  134. %left MUL_OP DIV_OP
  135. %right NOT
  136. /*=========================================================================
  137. BISON GRAMMAR
  138. =========================================================================*/
  139. %%
  140. /* `program' is the starting non-terminal of the grammar.
  141. * A program is composed by:
  142. 1. declarations (zero or more);
  143. 2. A list of instructions. (at least one instruction!).
  144. * When the rule associated with the non-terminal `program' is executed,
  145. * the parser notify it to the `program' singleton instance. */
  146. program : var_declarations statements
  147. {
  148. /* Notify the end of the program. Once called
  149. * the function `set_end_Program' - if necessary -
  150. * introduces a `HALT' instruction into the
  151. * list of instructions. */
  152. set_end_Program(program);
  153. /* return from yyparse() */
  154. YYACCEPT;
  155. }
  156. ;
  157. var_declarations : var_declarations var_declaration { /* does nothing */ }
  158. | /* empty */ { /* does nothing */ }
  159. ;
  160. var_declaration : TYPE declaration_list SEMI
  161. {
  162. /* update the program infos by adding new variables */
  163. set_new_variables(program, $1, $2);
  164. }
  165. ;
  166. declaration_list : declaration_list COMMA declaration
  167. { /* add the new declaration to the list of declarations */
  168. $$ = addElement($1, $3, -1);
  169. }
  170. | declaration
  171. {
  172. /* add the new declaration to the list of declarations */
  173. $$ = addElement(NULL, $1, -1);
  174. }
  175. ;
  176. declaration : IDENTIFIER ASSIGN NUMBER
  177. {
  178. /* create a new instance of t_axe_declaration */
  179. $$ = alloc_declaration($1, 0, 0, $3);
  180. /* test if an `out of memory' occurred */
  181. if ($$ == NULL)
  182. notifyError(AXE_OUT_OF_MEMORY);
  183. }
  184. | IDENTIFIER LSQUARE NUMBER RSQUARE
  185. {
  186. /* create a new instance of t_axe_declaration */
  187. $$ = alloc_declaration($1, 1, $3, 0);
  188. /* test if an `out of memory' occurred */
  189. if ($$ == NULL)
  190. notifyError(AXE_OUT_OF_MEMORY);
  191. }
  192. | IDENTIFIER
  193. {
  194. /* create a new instance of t_axe_declaration */
  195. $$ = alloc_declaration($1, 0, 0, 0);
  196. /* test if an `out of memory' occurred */
  197. if ($$ == NULL)
  198. notifyError(AXE_OUT_OF_MEMORY);
  199. }
  200. ;
  201. /* A block of code can be either a single statement or
  202. * a set of statements enclosed between braces */
  203. code_block : statement { /* does nothing */ }
  204. | LBRACE statements RBRACE { /* does nothing */ }
  205. ;
  206. /* One or more code statements */
  207. statements : statements statement { /* does nothing */ }
  208. | statement { /* does nothing */ }
  209. ;
  210. /* A statement can be either an assignment statement or a control statement
  211. * or a read/write statement or a semicolon */
  212. statement : assign_statement SEMI { /* does nothing */ }
  213. | control_statement { /* does nothing */ }
  214. | read_write_statement SEMI { /* does nothing */ }
  215. | SEMI { gen_nop_instruction(program); }
  216. ;
  217. control_statement : if_statement { /* does nothing */ }
  218. | while_statement { /* does nothing */ }
  219. | do_while_statement SEMI { /* does nothing */ }
  220. | return_statement SEMI { /* does nothing */ }
  221. | map_statement
  222. | reduce_statement SEMI
  223. ;
  224. map_statement : MAP IDENTIFIER ON IDENTIFIER AS
  225. {
  226. t_axe_variable *var_elem = getVariable(program, $2);
  227. t_axe_variable *var_array = getVariable(program, $4);
  228. if (!var_elem || !var_array || var_elem->isArray ||
  229. !var_array->isArray)
  230. exit(-1);
  231. int elem_reg = get_symbol_location(program, $2, 0);
  232. $1 = gen_load_immediate(program, var_array->arraySize -1);
  233. $3 = assignNewLabel(program);
  234. t_axe_expression idx = create_expression($1, REGISTER);
  235. int tmp = loadArrayElement(program, $4, idx);
  236. gen_add_instruction(program, elem_reg, REG_0, tmp,
  237. CG_DIRECT_ALL);
  238. }
  239. code_block
  240. {
  241. int elem_reg = get_symbol_location(program, $2, 0);
  242. t_axe_expression idx = create_expression($1, REGISTER);
  243. t_axe_expression elem = create_expression(elem_reg, REGISTER);
  244. storeArrayElement(program, $4, idx, elem);
  245. gen_subi_instruction(program, $1, $1, 1);
  246. gen_bge_instruction(program, $3, 0);
  247. free($2);
  248. free($4);
  249. }
  250. ;
  251. reduce_statement : REDUCE IDENTIFIER INTO IDENTIFIER AS LDSQUARE
  252. {
  253. t_axe_variable *var_acc = getVariable(program, $2);
  254. t_axe_variable *var_elem = getVariable(program, $4);
  255. if (!var_elem || !var_acc || var_elem->isArray ||
  256. var_acc->isArray)
  257. exit(-1);
  258. $1 = newLabel(program);
  259. gen_bt_instruction(program, $1, 0);
  260. $3 = assignNewLabel(program);
  261. }
  262. exp RDSQUARE ON IDENTIFIER {
  263. t_axe_variable *var_arr = getVariable(program, $11);
  264. if (!var_arr || !var_arr->isArray) exit(-1);
  265. int acc_reg = get_symbol_location(program, $4, 0);
  266. t_axe_label *end_label = newLabel(program);
  267. if ($8.expression_type == IMMEDIATE) {
  268. gen_addi_instruction(program, acc_reg, REG_0,
  269. $8.value);
  270. gen_bt_instruction(program, end_label, 0);
  271. assignLabel(program, $1);
  272. gen_bt_instruction(program, $3, 0);
  273. } else {
  274. int iv_reg = getNewRegister(program);
  275. t_axe_expression ive = create_expression(iv_reg,
  276. REGISTER);
  277. int elem_reg = get_symbol_location(program, $2, 0);
  278. gen_addi_instruction(program, acc_reg, $8.value, 0);
  279. gen_subi_instruction(program, iv_reg, iv_reg, 1);
  280. gen_blt_instruction(program, end_label, 0);
  281. t_axe_label *begin_label = assignNewLabel(program);
  282. int tmp = loadArrayElement(program, $11, ive);
  283. gen_addi_instruction(program, elem_reg, tmp, 0);
  284. gen_bt_instruction(program, $3, 0);
  285. assignLabel(program, $1);
  286. gen_addi_instruction(program, iv_reg, REG_0,
  287. var_arr->arraySize -1);
  288. gen_bt_instruction(program, begin_label, 0);
  289. }
  290. assignLabel(program, end_label);
  291. free($2);
  292. free($4);
  293. free($11);
  294. }
  295. ;
  296. read_write_statement : read_statement { /* does nothing */ }
  297. | write_statement { /* does nothing */ }
  298. ;
  299. assign_statement : IDENTIFIER LSQUARE exp RSQUARE ASSIGN exp
  300. {
  301. /* Notify to `program' that the value $6
  302. * have to be assigned to the location
  303. * addressed by $1[$3]. Where $1 is obviously
  304. * the array/pointer identifier, $3 is an expression
  305. * that holds an integer value. That value will be
  306. * used as an index for the array $1 */
  307. storeArrayElement(program, $1, $3, $6);
  308. /* free the memory associated with the IDENTIFIER.
  309. * The use of the free instruction is required
  310. * because of the value associated with IDENTIFIER.
  311. * The value of IDENTIFIER is a string created
  312. * by a call to the function `strdup' (see Acse.lex) */
  313. free($1);
  314. }
  315. | IDENTIFIER ASSIGN exp
  316. {
  317. int location;
  318. t_axe_instruction *instr;
  319. /* in order to assign a value to a variable, we have to
  320. * know where the variable is located (i.e. in which register).
  321. * the function `get_symbol_location' is used in order
  322. * to retrieve the register location assigned to
  323. * a given identifier.
  324. * A symbol table keeps track of the location of every
  325. * declared variable.
  326. * `get_symbol_location' perform a query on the symbol table
  327. * in order to discover the correct location of
  328. * the variable with $1 as identifier */
  329. /* get the location of the symbol with the given ID. */
  330. location = get_symbol_location(program, $1, 0);
  331. /* update the value of location */
  332. if ($3.expression_type == IMMEDIATE)
  333. gen_move_immediate(program, location, $3.value);
  334. else
  335. instr = gen_add_instruction
  336. (program, location, REG_0, $3.value, CG_DIRECT_ALL);
  337. /* free the memory associated with the IDENTIFIER */
  338. free($1);
  339. }
  340. ;
  341. if_statement : if_stmt
  342. {
  343. /* fix the `label_else' */
  344. assignLabel(program, $1);
  345. }
  346. | if_stmt ELSE
  347. {
  348. /* reserve a new label that points to the address where to jump if
  349. * `exp' is verified */
  350. $2 = newLabel(program);
  351. /* exit from the if-else */
  352. gen_bt_instruction (program, $2, 0);
  353. /* fix the `label_else' */
  354. assignLabel(program, $1);
  355. }
  356. code_block
  357. {
  358. /* fix the `label_else' */
  359. assignLabel(program, $2);
  360. }
  361. ;
  362. if_stmt : IF
  363. {
  364. /* the label that points to the address where to jump if
  365. * `exp' is not verified */
  366. $1 = newLabel(program);
  367. }
  368. LPAR exp RPAR
  369. {
  370. if ($4.expression_type == IMMEDIATE)
  371. gen_load_immediate(program, $4.value);
  372. else
  373. gen_andb_instruction(program, $4.value,
  374. $4.value, $4.value, CG_DIRECT_ALL);
  375. /* if `exp' returns FALSE, jump to the label $1 */
  376. gen_beq_instruction (program, $1, 0);
  377. }
  378. code_block { $$ = $1; }
  379. ;
  380. while_statement : WHILE
  381. {
  382. /* initialize the value of the non-terminal */
  383. $1 = create_while_statement();
  384. /* reserve and fix a new label */
  385. $1.label_condition
  386. = assignNewLabel(program);
  387. }
  388. LPAR exp RPAR
  389. {
  390. if ($4.expression_type == IMMEDIATE)
  391. gen_load_immediate(program, $4.value);
  392. else
  393. gen_andb_instruction(program, $4.value,
  394. $4.value, $4.value, CG_DIRECT_ALL);
  395. /* reserve a new label. This new label will point
  396. * to the first instruction after the while code
  397. * block */
  398. $1.label_end = newLabel(program);
  399. /* if `exp' returns FALSE, jump to the label $1.label_end */
  400. gen_beq_instruction (program, $1.label_end, 0);
  401. }
  402. code_block
  403. {
  404. /* jump to the beginning of the loop */
  405. gen_bt_instruction
  406. (program, $1.label_condition, 0);
  407. /* fix the label `label_end' */
  408. assignLabel(program, $1.label_end);
  409. }
  410. ;
  411. do_while_statement : DO
  412. {
  413. /* the label that points to the address where to jump if
  414. * `exp' is not verified */
  415. $1 = newLabel(program);
  416. /* fix the label */
  417. assignLabel(program, $1);
  418. }
  419. code_block WHILE LPAR exp RPAR
  420. {
  421. if ($6.expression_type == IMMEDIATE)
  422. gen_load_immediate(program, $6.value);
  423. else
  424. gen_andb_instruction(program, $6.value,
  425. $6.value, $6.value, CG_DIRECT_ALL);
  426. /* if `exp' returns TRUE, jump to the label $1 */
  427. gen_bne_instruction (program, $1, 0);
  428. }
  429. ;
  430. return_statement : RETURN
  431. {
  432. /* insert an HALT instruction */
  433. gen_halt_instruction(program);
  434. }
  435. ;
  436. read_statement : READ LPAR IDENTIFIER RPAR
  437. {
  438. int location;
  439. /* read from standard input an integer value and assign
  440. * it to a variable associated with the given identifier */
  441. /* get the location of the symbol with the given ID */
  442. /* lookup the symbol table and fetch the register location
  443. * associated with the IDENTIFIER $3. */
  444. location = get_symbol_location(program, $3, 0);
  445. /* insert a read instruction */
  446. gen_read_instruction (program, location);
  447. /* free the memory associated with the IDENTIFIER */
  448. free($3);
  449. }
  450. ;
  451. write_statement : WRITE LPAR exp RPAR
  452. {
  453. int location;
  454. if ($3.expression_type == IMMEDIATE)
  455. {
  456. /* load `immediate' into a new register. Returns the new register
  457. * identifier or REG_INVALID if an error occurs */
  458. location = gen_load_immediate(program, $3.value);
  459. }
  460. else
  461. location = $3.value;
  462. /* write to standard output an integer value */
  463. gen_write_instruction (program, location);
  464. }
  465. ;
  466. exp: NUMBER { $$ = create_expression ($1, IMMEDIATE); }
  467. | IDENTIFIER {
  468. int location;
  469. /* get the location of the symbol with the given ID */
  470. location = get_symbol_location(program, $1, 0);
  471. /* return the register location of IDENTIFIER as
  472. * a value for `exp' */
  473. $$ = create_expression (location, REGISTER);
  474. /* free the memory associated with the IDENTIFIER */
  475. free($1);
  476. }
  477. | IDENTIFIER LSQUARE exp RSQUARE {
  478. int reg;
  479. /* load the value IDENTIFIER[exp]
  480. * into `arrayElement' */
  481. reg = loadArrayElement(program, $1, $3);
  482. /* create a new expression */
  483. $$ = create_expression (reg, REGISTER);
  484. /* free the memory associated with the IDENTIFIER */
  485. free($1);
  486. }
  487. | NOT_OP NUMBER { if ($2 == 0)
  488. $$ = create_expression (1, IMMEDIATE);
  489. else
  490. $$ = create_expression (0, IMMEDIATE);
  491. }
  492. | NOT_OP IDENTIFIER {
  493. int identifier_location;
  494. int output_register;
  495. /* get the location of the symbol with the given ID */
  496. identifier_location =
  497. get_symbol_location(program, $2, 0);
  498. /* generate a NOT instruction. In order to do this,
  499. * at first we have to ask for a free register where
  500. * to store the result of the NOT instruction. */
  501. output_register = getNewRegister(program);
  502. /* Now we are able to generate a NOT instruction */
  503. gen_notl_instruction (program, output_register
  504. , identifier_location);
  505. $$ = create_expression (output_register, REGISTER);
  506. /* free the memory associated with the IDENTIFIER */
  507. free($2);
  508. }
  509. | exp AND_OP exp {
  510. $$ = handle_bin_numeric_op(program, $1, $3, ANDB);
  511. }
  512. | exp OR_OP exp {
  513. $$ = handle_bin_numeric_op(program, $1, $3, ORB);
  514. }
  515. | exp PLUS exp {
  516. $$ = handle_bin_numeric_op(program, $1, $3, ADD);
  517. }
  518. | exp MINUS exp {
  519. $$ = handle_bin_numeric_op(program, $1, $3, SUB);
  520. }
  521. | exp MUL_OP exp {
  522. $$ = handle_bin_numeric_op(program, $1, $3, MUL);
  523. }
  524. | exp DIV_OP exp {
  525. $$ = handle_bin_numeric_op(program, $1, $3, DIV);
  526. }
  527. | exp LT exp {
  528. $$ = handle_binary_comparison (program, $1, $3, _LT_);
  529. }
  530. | exp GT exp {
  531. $$ = handle_binary_comparison (program, $1, $3, _GT_);
  532. }
  533. | exp EQ exp {
  534. $$ = handle_binary_comparison (program, $1, $3, _EQ_);
  535. }
  536. | exp NOTEQ exp {
  537. $$ = handle_binary_comparison (program, $1, $3, _NOTEQ_);
  538. }
  539. | exp LTEQ exp {
  540. $$ = handle_binary_comparison (program, $1, $3, _LTEQ_);
  541. }
  542. | exp GTEQ exp {
  543. $$ = handle_binary_comparison (program, $1, $3, _GTEQ_);
  544. }
  545. | exp SHL_OP exp { $$ = handle_bin_numeric_op(program, $1, $3, SHL); }
  546. | exp SHR_OP exp { $$ = handle_bin_numeric_op(program, $1, $3, SHR); }
  547. | exp ANDAND exp { $$ = handle_bin_numeric_op(program, $1, $3, ANDL); }
  548. | exp OROR exp { $$ = handle_bin_numeric_op(program, $1, $3, ORL); }
  549. | LPAR exp RPAR { $$ = $2; }
  550. | MINUS exp {
  551. if ($2.expression_type == IMMEDIATE)
  552. {
  553. $$ = $2;
  554. $$.value = - ($$.value);
  555. }
  556. else
  557. {
  558. t_axe_expression exp_r0;
  559. /* create an expression for regisrer REG_0 */
  560. exp_r0.value = REG_0;
  561. exp_r0.expression_type = REGISTER;
  562. $$ = handle_bin_numeric_op
  563. (program, exp_r0, $2, SUB);
  564. }
  565. }
  566. ;
  567. %%
  568. /*=========================================================================
  569. MAIN
  570. =========================================================================*/
  571. int main (int argc, char **argv)
  572. {
  573. /* initialize all the compiler data structures and global variables */
  574. init_compiler(argc, argv);
  575. /* start the parsing procedure */
  576. yyparse();
  577. #ifndef NDEBUG
  578. fprintf(stdout, "Parsing process completed. \n");
  579. #endif
  580. /* test if the parsing process completed succesfully */
  581. checkConsistency();
  582. #ifndef NDEBUG
  583. fprintf(stdout, "Creating a control flow graph. \n");
  584. #endif
  585. /* create the control flow graph */
  586. graph = createFlowGraph(program->instructions);
  587. checkConsistency();
  588. #ifndef NDEBUG
  589. assert(program != NULL);
  590. assert(program->sy_table != NULL);
  591. assert(file_infos != NULL);
  592. assert(file_infos->syTable_output != NULL);
  593. printSymbolTable(program->sy_table, file_infos->syTable_output);
  594. printGraphInfos(graph, file_infos->cfg_1, 0);
  595. fprintf(stdout, "Updating the basic blocks. \n");
  596. #endif
  597. /* update the control flow graph by inserting load and stores inside
  598. * every basic block */
  599. graph = insertLoadAndStoreInstr(program, graph);
  600. #ifndef NDEBUG
  601. fprintf(stdout, "Executing a liveness analysis on the intermediate code \n");
  602. #endif
  603. performLivenessAnalysis(graph);
  604. checkConsistency();
  605. #ifndef NDEBUG
  606. printGraphInfos(graph, file_infos->cfg_2, 1);
  607. #endif
  608. #ifndef NDEBUG
  609. fprintf(stdout, "Starting the register allocation process. \n");
  610. #endif
  611. /* initialize the register allocator by using the control flow
  612. * informations stored into the control flow graph */
  613. RA = initializeRegAlloc(graph);
  614. /* execute the linear scan algorythm */
  615. execute_linear_scan(RA);
  616. #ifndef NDEBUG
  617. printRegAllocInfos(RA, file_infos->reg_alloc_output);
  618. #endif
  619. #ifndef NDEBUG
  620. fprintf(stdout, "Updating the control flow informations. \n");
  621. #endif
  622. /* apply changes to the program informations by using the informations
  623. * of the register allocation process */
  624. updateProgramInfos(program, graph, RA);
  625. #ifndef NDEBUG
  626. fprintf(stdout, "Writing the assembly file... \n");
  627. #endif
  628. writeAssembly(program, file_infos->output_file_name);
  629. #ifndef NDEBUG
  630. fprintf(stdout, "Assembly written on file \"%s\".\n", file_infos->output_file_name);
  631. #endif
  632. /* shutdown the compiler */
  633. shutdownCompiler(0);
  634. return 0;
  635. }
  636. /*=========================================================================
  637. YYERROR
  638. =========================================================================*/
  639. int yyerror(const char* errmsg)
  640. {
  641. errorcode = AXE_SYNTAX_ERROR;
  642. return 0;
  643. }