aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/generator.c40
-rw-r--r--src/generator.h14
-rw-r--r--src/tpc.y132
3 files changed, 92 insertions, 94 deletions
diff --git a/src/generator.c b/src/generator.c
index bbb2af5..553161a 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -12,9 +12,8 @@ void gen_prologue() {
12 fprintf(output, "section .data\n"); 12 fprintf(output, "section .data\n");
13} 13}
14 14
15void gen_prologue_continue(int *bss_done) { 15void gen_prologue_continue(bool *bss_done) {
16 if (*bss_done != 0) 16 if (!*bss_done) return;
17 return;
18 17
19 fprintf(output, "format_int db \"%%d\",10,0\n"); 18 fprintf(output, "format_int db \"%%d\",10,0\n");
20 fprintf(output, "format_char db \"%%c\",10,0\n"); 19 fprintf(output, "format_char db \"%%c\",10,0\n");
@@ -115,8 +114,7 @@ Type gen_function_declaration(const char name[], int return_type) {
115 return return_type; 114 return return_type;
116} 115}
117 116
118void gen_function_end_declaration(const char name[], int return_type, 117void gen_function_end_declaration(const char name[], Type return_type, int nb_param) {
119 int nb_param) {
120 fun_add(name, return_type, nb_param); 118 fun_add(name, return_type, nb_param);
121 fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n"); 119 fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n");
122} 120}
@@ -139,7 +137,7 @@ Type gen_function_call(const char name[], int nb_param) {
139 return return_type; 137 return return_type;
140} 138}
141 139
142void gen_declaration(const char name[], int type, Scope scope) { 140void gen_declaration(const char name[], Type type, Scope scope) {
143 switch (scope) { 141 switch (scope) {
144 case GLOBAL: 142 case GLOBAL:
145 glo_addVar(name, type); 143 glo_addVar(name, type);
@@ -220,7 +218,7 @@ void gen_readc(const char name[], Scope scope) {
220 fprintf(output, "mov rax,globals\nadd rax,%d\ncall readc\n", g_addr); 218 fprintf(output, "mov rax,globals\nadd rax,%d\ncall readc\n", g_addr);
221} 219}
222 220
223void gen_print(int type) { 221void gen_print(Type type) {
224 // check if the name exists in both tables 222 // check if the name exists in both tables
225 fprintf(output, "pop rax\n"); 223 fprintf(output, "pop rax\n");
226 switch (type) { 224 switch (type) {
@@ -264,7 +262,7 @@ void gen_ifelse_end(int idx) {
264 262
265// ----- OPERATORS ----- 263// ----- OPERATORS -----
266 264
267int gen_assign(const char ident[], Scope scope) { 265static int gen_assign_simple(const char ident[], Scope scope) {
268 int l_addr = loc_get_addr(ident); 266 int l_addr = loc_get_addr(ident);
269 int g_addr = glo_get_addr(ident); 267 int g_addr = glo_get_addr(ident);
270 268
@@ -290,7 +288,7 @@ int gen_assign(const char ident[], Scope scope) {
290 } 288 }
291} 289}
292 290
293int gen_assign_tab(const char ident[], Scope scope) { 291static int gen_assign_tab(const char ident[], Scope scope) {
294 int l_addr = loc_get_addr(ident); 292 int l_addr = loc_get_addr(ident);
295 int g_addr = glo_get_addr(ident); 293 int g_addr = glo_get_addr(ident);
296 294
@@ -322,6 +320,13 @@ int gen_assign_tab(const char ident[], Scope scope) {
322 } 320 }
323} 321}
324 322
323int gen_assign(const char ident[], Scope scope) {
324 switch (loc_lookup(ident)) {
325 case TAB: return gen_assign_tab(ident, scope);
326 default: return gen_assign_simple(ident, scope);
327 }
328}
329
325void gen_or(int left, int right, int idx) { 330void gen_or(int left, int right, int idx) {
326 check_expected_types(left, INT, TAB); 331 check_expected_types(left, INT, TAB);
327 check_expected_types(right, INT, TAB); 332 check_expected_types(right, INT, TAB);
@@ -430,7 +435,7 @@ void gen_divstar(char op, int left, int right) {
430 } 435 }
431} 436}
432 437
433int gen_signed_expr(char op, int type) { 438int gen_signed_expr(char op, Type type) {
434 check_expected_types(type, INT, TAB); 439 check_expected_types(type, INT, TAB);
435 switch (op) { 440 switch (op) {
436 case '+': 441 case '+':
@@ -446,13 +451,13 @@ int gen_signed_expr(char op, int type) {
446 return type; 451 return type;
447} 452}
448 453
449int gen_negate_expr(int type) { 454int gen_negate_expr(Type type) {
450 check_expected_types(type, INT, TAB); 455 check_expected_types(type, INT, TAB);
451 fprintf(output, ";!F\npop rax\nxor rax,1\npush rax\n"); 456 fprintf(output, ";!F\npop rax\nxor rax,1\npush rax\n");
452 return type; 457 return type;
453} 458}
454 459
455int gen_value(const char ident[], Scope scope) { 460static int gen_value_simple(const char ident[], Scope scope) {
456 int l_addr = loc_get_addr(ident); 461 int l_addr = loc_get_addr(ident);
457 462
458 switch (scope) { 463 switch (scope) {
@@ -475,7 +480,8 @@ int gen_value(const char ident[], Scope scope) {
475 exit(1); 480 exit(1);
476 } 481 }
477} 482}
478int gen_value_tab(const char ident[], Scope scope) { 483
484static int gen_value_tab(const char ident[], Scope scope) {
479 int l_addr = loc_get_addr(ident); 485 int l_addr = loc_get_addr(ident);
480 int g_addr = glo_get_addr(ident); 486 int g_addr = glo_get_addr(ident);
481 switch (scope) { 487 switch (scope) {
@@ -500,6 +506,14 @@ int gen_value_tab(const char ident[], Scope scope) {
500 exit(1); 506 exit(1);
501 } 507 }
502} 508}
509
510int gen_value(const char ident[], Scope scope) {
511 switch (loc_lookup(ident)) {
512 case TAB: return gen_value_tab(ident, scope);
513 default: return gen_value_simple(ident, scope);
514 }
515}
516
503int gen_num(int value, Scope scope) { 517int gen_num(int value, Scope scope) {
504 fprintf(output, "push %d\n", value); 518 fprintf(output, "push %d\n", value);
505 return INT; 519 return INT;
diff --git a/src/generator.h b/src/generator.h
index 9d6dd65..e1fd714 100644
--- a/src/generator.h
+++ b/src/generator.h
@@ -15,23 +15,23 @@ extern int lineno;
15FILE *output; 15FILE *output;
16 16
17void gen_prologue(); 17void gen_prologue();
18void gen_prologue_continue(int *bss_done); 18void gen_prologue_continue(bool *bss_done);
19void gen_epilogue(); 19void gen_epilogue();
20void gen_const(const char name[], int value, Scope scope); 20void gen_const(const char name[], int value, Scope scope);
21 21
22Type gen_function_declaration(const char name[], int return_type); 22Type gen_function_declaration(const char name[], int return_type);
23void gen_tab_declaration(const char name[], Scope scope, int size); 23void gen_tab_declaration(const char name[], Scope scope, int size);
24void gen_function_end_declaration(const char name[], int return_type, int nb_param); 24void gen_function_end_declaration(const char name[], Type return_type, int nb_param);
25void gen_function_return(Type expect, Type actual); 25void gen_function_return(Type expect, Type actual);
26Type gen_function_call(const char name[], int nb_param); 26Type gen_function_call(const char name[], int nb_param);
27 27
28void gen_declaration(const char name[], int type, Scope scope); 28void gen_declaration(const char name[], Type type, Scope scope);
29void gen_check(const char name[], Scope scope); 29void gen_check(const char name[], Scope scope);
30 30
31void gen_reade(const char name[], Scope scope); 31void gen_reade(const char name[], Scope scope);
32void gen_readc(const char name[], Scope scope); 32void gen_readc(const char name[], Scope scope);
33 33
34void gen_print(int type); 34void gen_print(Type type);
35 35
36void gen_if_label(int idx); 36void gen_if_label(int idx);
37void gen_if_start(int idx); 37void gen_if_start(int idx);
@@ -39,7 +39,6 @@ void gen_if_end(int idx);
39void gen_ifelse_end(int idx); 39void gen_ifelse_end(int idx);
40 40
41int gen_assign(const char ident[], Scope scope); 41int gen_assign(const char ident[], Scope scope);
42int gen_assign_tab(const char ident[], Scope scope);
43 42
44void gen_or(int left, int right, int idx); 43void gen_or(int left, int right, int idx);
45void gen_and(int left, int right, int idx); 44void gen_and(int left, int right, int idx);
@@ -48,10 +47,9 @@ void gen_order(const char op[], int left, int right, int idx);
48void gen_addsub(char op, int left, int right); 47void gen_addsub(char op, int left, int right);
49void gen_divstar(char op, int left, int right); 48void gen_divstar(char op, int left, int right);
50 49
51int gen_signed_expr(char op, int type); 50int gen_signed_expr(char op, Type type);
52int gen_negate_expr(int type); 51int gen_negate_expr(Type type);
53int gen_value(const char ident[], Scope scope); 52int gen_value(const char ident[], Scope scope);
54int gen_value_tab(const char ident[], Scope scope);
55 53
56int gen_num(int value, Scope scope); 54int gen_num(int value, Scope scope);
57int gen_char(int value, Scope scope); 55int gen_char(int value, Scope scope);
diff --git a/src/tpc.y b/src/tpc.y
index 74d31c6..a21100d 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -2,11 +2,6 @@
2/* 2/*
3 * UPEM / Compilation / Projet 3 * UPEM / Compilation / Projet
4 * Pacien TRAN-GIRARD, Adam NAILI 4 * Pacien TRAN-GIRARD, Adam NAILI
5 *
6 * TODO :
7 * ------
8 * - arrays
9 *
10 */ 5 */
11 6
12int nb_globals = 0; 7int nb_globals = 0;
@@ -20,14 +15,14 @@ int yylex();
20void yyerror(char *); 15void yyerror(char *);
21static Scope scope = GLOBAL; 16static Scope scope = GLOBAL;
22static Type return_type = VOID_T; 17static Type return_type = VOID_T;
23static int bss_done = 0; 18static bool bss_done = false;
24static int num_label = 0; 19static int num_label = 0;
25static int num_if = 0; 20static int num_if = 0;
26static int num_while = 0; 21static int num_while = 0;
27static int nb_param[255]; 22static int nb_param[255];
28static int num_scope = -1; 23static int num_scope = -1;
29static int offset = 0; 24static int offset = 0;
30static int is_array = 0; 25static bool is_array = false;
31static char fname[64]; 26static char fname[64];
32%} 27%}
33 28
@@ -81,50 +76,49 @@ DeclVars:
81| 76|
82; 77;
83Declarateurs: 78Declarateurs:
84 Declarateurs ',' Declarateur { 79 Declarateurs ',' Declarateur { if (is_array) gen_tab_declaration($<ident>3, scope, offset);