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