diff options
author | Adam NAILI | 2018-06-06 15:00:54 +0200 |
---|---|---|
committer | Adam NAILI | 2018-06-06 15:00:54 +0200 |
commit | cca10e12f433446d22c19d6326768cc227f18d2a (patch) | |
tree | 8d671f548484758f43190ee9a52be230bb8ef567 /src | |
parent | 8b93ef6b8eabfdfa6dd0c61ebcad0101b5127d5e (diff) | |
parent | 00b8cb6be126d6cfeacf932b5595df9824cdf412 (diff) | |
download | tpc-compiler-cca10e12f433446d22c19d6326768cc227f18d2a.tar.gz |
Merge branch 'master' of https://github.com/pacien/upem-compil-tpc
Diffstat (limited to 'src')
-rw-r--r-- | src/generator.c | 40 | ||||
-rw-r--r-- | src/generator.h | 14 | ||||
-rw-r--r-- | src/tpc.y | 132 |
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 | ||
15 | void gen_prologue_continue(int *bss_done) { | 15 | void 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 | ||
118 | void gen_function_end_declaration(const char name[], int return_type, | 117 | void 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 | ||
142 | void gen_declaration(const char name[], int type, Scope scope) { | 140 | void 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 | ||
223 | void gen_print(int type) { | 221 | void 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 | ||
267 | int gen_assign(const char ident[], Scope scope) { | 265 | static 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 | ||
293 | int gen_assign_tab(const char ident[], Scope scope) { | 291 | static 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 | ||
323 | int 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 | |||
325 | void gen_or(int left, int right, int idx) { | 330 | void 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 | ||
433 | int gen_signed_expr(char op, int type) { | 438 | int 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 | ||
449 | int gen_negate_expr(int type) { | 454 | int 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 | ||
455 | int gen_value(const char ident[], Scope scope) { | 460 | static 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 | } |
478 | int gen_value_tab(const char ident[], Scope scope) { | 483 | |
484 | static 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 | |||
510 | int 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 | |||
503 | int gen_num(int value, Scope scope) { | 517 | int 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; | |||
15 | FILE *output; | 15 | FILE *output; |
16 | 16 | ||
17 | void gen_prologue(); | 17 | void gen_prologue(); |
18 | void gen_prologue_continue(int *bss_done); | 18 | void gen_prologue_continue(bool *bss_done); |
19 | void gen_epilogue(); | 19 | void gen_epilogue(); |
20 | void gen_const(const char name[], int value, Scope scope); | 20 | void gen_const(const char name[], int value, Scope scope); |
21 | 21 | ||
22 | Type gen_function_declaration(const char name[], int return_type); | 22 | Type gen_function_declaration(const char name[], int return_type); |
23 | void gen_tab_declaration(const char name[], Scope scope, int size); | 23 | void gen_tab_declaration(const char name[], Scope scope, int size); |
24 | void gen_function_end_declaration(const char name[], int return_type, int nb_param); | 24 | void gen_function_end_declaration(const char name[], Type return_type, int nb_param); |
25 | void gen_function_return(Type expect, Type actual); | 25 | void gen_function_return(Type expect, Type actual); |
26 | Type gen_function_call(const char name[], int nb_param); | 26 | Type gen_function_call(const char name[], int nb_param); |
27 | 27 | ||
28 | void gen_declaration(const char name[], int type, Scope scope); | 28 | void gen_declaration(const char name[], Type type, Scope scope); |
29 | void gen_check(const char name[], Scope scope); | 29 | void gen_check(const char name[], Scope scope); |
30 | 30 | ||
31 | void gen_reade(const char name[], Scope scope); | 31 | void gen_reade(const char name[], Scope scope); |
32 | void gen_readc(const char name[], Scope scope); | 32 | void gen_readc(const char name[], Scope scope); |
33 | 33 | ||
34 | void gen_print(int type); | 34 | void gen_print(Type type); |
35 | 35 | ||
36 | void gen_if_label(int idx); | 36 | void gen_if_label(int idx); |
37 | void gen_if_start(int idx); | 37 | void gen_if_start(int idx); |
@@ -39,7 +39,6 @@ void gen_if_end(int idx); | |||
39 | void gen_ifelse_end(int idx); | 39 | void gen_ifelse_end(int idx); |
40 | 40 | ||
41 | int gen_assign(const char ident[], Scope scope); | 41 | int gen_assign(const char ident[], Scope scope); |
42 | int gen_assign_tab(const char ident[], Scope scope); | ||
43 | 42 | ||
44 | void gen_or(int left, int right, int idx); | 43 | void gen_or(int left, int right, int idx); |
45 | void gen_and(int left, int right, int idx); | 44 | void gen_and(int left, int right, int idx); |
@@ -48,10 +47,9 @@ void gen_order(const char op[], int left, int right, int idx); | |||
48 | void gen_addsub(char op, int left, int right); | 47 | void gen_addsub(char op, int left, int right); |
49 | void gen_divstar(char op, int left, int right); | 48 | void gen_divstar(char op, int left, int right); |
50 | 49 | ||
51 | int gen_signed_expr(char op, int type); | 50 | int gen_signed_expr(char op, Type type); |
52 | int gen_negate_expr(int type); | 51 | int gen_negate_expr(Type type); |
53 | int gen_value(const char ident[], Scope scope); | 52 | int gen_value(const char ident[], Scope scope); |
54 | int gen_value_tab(const char ident[], Scope scope); | ||
55 | 53 | ||
56 | int gen_num(int value, Scope scope); | 54 | int gen_num(int value, Scope scope); |
57 | int gen_char(int value, Scope scope); | 55 | int gen_char(int value, Scope scope); |
@@ -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 | ||
12 | int nb_globals = 0; | 7 | int nb_globals = 0; |
@@ -20,14 +15,14 @@ int yylex(); | |||
20 | void yyerror(char *); | 15 | void yyerror(char *); |
21 | static Scope scope = GLOBAL; | 16 | static Scope scope = GLOBAL; |
22 | static Type return_type = VOID_T; | 17 | static Type return_type = VOID_T; |
23 | static int bss_done = 0; | 18 | static bool bss_done = false; |
24 | static int num_label = 0; | 19 | static int num_label = 0; |
25 | static int num_if = 0; | 20 | static int num_if = 0; |
26 | static int num_while = 0; | 21 | static int num_while = 0; |
27 | static int nb_param[255]; | 22 | static int nb_param[255]; |
28 | static int num_scope = -1; | 23 | static int num_scope = -1; |
29 | static int offset = 0; | 24 | static int offset = 0; |
30 | static int is_array = 0; | 25 | static bool is_array = false; |
31 | static char fname[64]; | 26 | static char fname[64]; |
32 | %} | 27 | %} |
33 |