From 58e0712bb4f624e02bffc877bf39f5fd45acc2e4 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 6 Jun 2018 14:36:25 +0200 Subject: extract assign and val-retrieval --- src/generator.c | 24 ++++++++++++++++++++---- src/generator.h | 2 -- src/tpc.y | 20 ++------------------ 3 files changed, 22 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/generator.c b/src/generator.c index 44fdeab..b75f7a8 100644 --- a/src/generator.c +++ b/src/generator.c @@ -265,7 +265,7 @@ void gen_ifelse_end(int idx) { // ----- OPERATORS ----- -int gen_assign(const char ident[], Scope scope) { +static int gen_assign_simple(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); @@ -291,7 +291,7 @@ int gen_assign(const char ident[], Scope scope) { } } -int gen_assign_tab(const char ident[], Scope scope) { +static int gen_assign_tab(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); @@ -317,6 +317,13 @@ int gen_assign_tab(const char ident[], Scope scope) { } } +int gen_assign(const char ident[], Scope scope) { + switch (loc_lookup(ident)) { + case TAB: return gen_assign_tab(ident, scope); + default: return gen_assign_simple(ident, scope); + } +} + void gen_or(int left, int right, int idx) { check_expected_types(left, INT,TAB); check_expected_types(right, INT,TAB); @@ -447,7 +454,7 @@ int gen_negate_expr(int type) { return type; } -int gen_value(const char ident[], Scope scope) { +static int gen_value_simple(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); switch (scope) { @@ -470,7 +477,8 @@ int gen_value(const char ident[], Scope scope) { exit(1); } } -int gen_value_tab(const char ident[], Scope scope) { + +static int gen_value_tab(const char ident[], Scope scope) { int l_addr = loc_get_addr(ident); int g_addr = glo_get_addr(ident); switch (scope) { @@ -489,6 +497,14 @@ int gen_value_tab(const char ident[], Scope scope) { exit(1); } } + +int gen_value(const char ident[], Scope scope) { + switch (loc_lookup(ident)) { + case TAB: return gen_value_tab(ident, scope); + default: return gen_value_simple(ident, scope); + } +} + int gen_num(int value, Scope scope) { fprintf(output, "push %d\n", value); return INT; diff --git a/src/generator.h b/src/generator.h index 9d6dd65..e1e47df 100644 --- a/src/generator.h +++ b/src/generator.h @@ -39,7 +39,6 @@ void gen_if_end(int idx); void gen_ifelse_end(int idx); int gen_assign(const char ident[], Scope scope); -int gen_assign_tab(const char ident[], Scope scope); void gen_or(int left, int right, int idx); void gen_and(int left, int right, int idx); @@ -51,7 +50,6 @@ void gen_divstar(char op, int left, int right); int gen_signed_expr(char op, int type); int gen_negate_expr(int type); int gen_value(const char ident[], Scope scope); -int gen_value_tab(const char ident[], Scope scope); int gen_num(int value, Scope scope); int gen_char(int value, Scope scope); diff --git a/src/tpc.y b/src/tpc.y index 74d31c6..6e733d0 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -2,11 +2,6 @@ /* * UPEM / Compilation / Projet * Pacien TRAN-GIRARD, Adam NAILI - * - * TODO : - * ------ - * - arrays - * */ int nb_globals = 0; @@ -147,13 +142,7 @@ IfHandling: { gen_if_start($$ = num_if++); }; IfEndHandling: { gen_if_end($-3); }; IfElseEndHandling: { gen_ifelse_end($-5); }; Exp: - LValue '=' Exp { - if(loc_lookup($1) != TAB){ - $$ = gen_assign($1, scope); - }else{ - $$ = gen_assign_tab($1,scope); - } - } + LValue '=' Exp { $$ = gen_assign($1, scope); } | EB ; EB: @@ -184,12 +173,7 @@ F: ADDSUB F { $$ = gen_signed_expr($1, $2); } | '!' F { $$ = gen_negate_expr($2); } | '(' Exp ')' { $$ = $2; } -| LValue { if(loc_lookup($1) != TAB){ - $$ = gen_value($1, scope); - }else{ - $$ = gen_value_tab($1,scope); - } - } +| LValue { $$ = gen_value($1, scope); } | NUM { $$ = gen_num($1, scope); } | CARACTERE { $$ = gen_char($1, scope); } | IDENT '(' Arguments ')' { $$ = gen_function_call($1,$3); } -- cgit v1.2.3 From 00b8cb6be126d6cfeacf932b5595df9824cdf412 Mon Sep 17 00:00:00 2001 From: pacien Date: Wed, 6 Jun 2018 15:00:35 +0200 Subject: reformat and unify types --- src/generator.c | 16 ++++---- src/generator.h | 12 +++--- src/tpc.y | 116 ++++++++++++++++++++++++++++---------------------------- 3 files changed, 72 insertions(+), 72 deletions(-) (limited to 'src') diff --git a/src/generator.c b/src/generator.c index 1b11b67..553161a 100644 --- a/src/generator.c +++ b/src/generator.c @@ -12,9 +12,8 @@ void gen_prologue() { fprintf(output, "section .data\n"); } -void gen_prologue_continue(int *bss_done) { - if (*bss_done != 0) - return; +void gen_prologue_continue(bool *bss_done) { + if (!*bss_done) return; fprintf(output, "format_int db \"%%d\",10,0\n"); fprintf(output, "format_char db \"%%c\",10,0\n"); @@ -115,8 +114,7 @@ Type gen_function_declaration(const char name[], int return_type) { return return_type; } -void gen_function_end_declaration(const char name[], int return_type, - int nb_param) { +void gen_function_end_declaration(const char name[], Type return_type, int nb_param) { fun_add(name, return_type, nb_param); fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n"); } @@ -139,7 +137,7 @@ Type gen_function_call(const char name[], int nb_param) { return return_type; } -void gen_declaration(const char name[], int type, Scope scope) { +void gen_declaration(const char name[], Type type, Scope scope) { switch (scope) { case GLOBAL: glo_addVar(name, type); @@ -220,7 +218,7 @@ void gen_readc(const char name[], Scope scope) { fprintf(output, "mov rax,globals\nadd rax,%d\ncall readc\n", g_addr); } -void gen_print(int type) { +void gen_print(Type type) { // check if the name exists in both tables fprintf(output, "pop rax\n"); switch (type) { @@ -437,7 +435,7 @@ void gen_divstar(char op, int left, int right) { } } -int gen_signed_expr(char op, int type) { +int gen_signed_expr(char op, Type type) { check_expected_types(type, INT, TAB); switch (op) { case '+': @@ -453,7 +451,7 @@ int gen_signed_expr(char op, int type) { return type; } -int gen_negate_expr(int type) { +int gen_negate_expr(Type type) { check_expected_types(type, INT, TAB); fprintf(output, ";!F\npop rax\nxor rax,1\npush rax\n"); return type; diff --git a/src/generator.h b/src/generator.h index e1e47df..e1fd714 100644 --- a/src/generator.h +++ b/src/generator.h @@ -15,23 +15,23 @@ extern int lineno; FILE *output; void gen_prologue(); -void gen_prologue_continue(int *bss_done); +void gen_prologue_continue(bool *bss_done); void gen_epilogue(); void gen_const(const char name[], int value, Scope scope); Type gen_function_declaration(const char name[], int return_type); void gen_tab_declaration(const char name[], Scope scope, int size); -void gen_function_end_declaration(const char name[], int return_type, int nb_param); +void gen_function_end_declaration(const char name[], Type return_type, int nb_param); void gen_function_return(Type expect, Type actual); Type gen_function_call(const char name[], int nb_param); -void gen_declaration(const char name[], int type, Scope scope); +void gen_declaration(const char name[], Type type, Scope scope); void gen_check(const char name[], Scope scope); void gen_reade(const char name[], Scope scope); void gen_readc(const char name[], Scope scope); -void gen_print(int type); +void gen_print(Type type); void gen_if_label(int idx); void gen_if_start(int idx); @@ -47,8 +47,8 @@ void gen_order(const char op[], int left, int right, int idx); void gen_addsub(char op, int left, int right); void gen_divstar(char op, int left, int right); -int gen_signed_expr(char op, int type); -int gen_negate_expr(int type); +int gen_signed_expr(char op, Type type); +int gen_negate_expr(Type type); int gen_value(const char ident[], Scope scope); int gen_num(int value, Scope scope); diff --git a/src/tpc.y b/src/tpc.y index 6e733d0..a21100d 100644 --- a/src/tpc.y +++ b/src/tpc.y @@ -15,14 +15,14 @@ int yylex(); void yyerror(char *); static Scope scope = GLOBAL; static Type return_type = VOID_T; -static int bss_done = 0; +static bool bss_done = false; static int num_label = 0; static int num_if = 0; static int num_while = 0; static int nb_param[255]; static int num_scope = -1; static int offset = 0; -static int is_array = 0; +static bool is_array = false; static char fname[64]; %} @@ -76,50 +76,49 @@ DeclVars: | ; Declarateurs: - Declarateurs ',' Declarateur { - if(!is_array){ - gen_declaration($3, $0, scope); - }else{ - gen_tab_declaration($3, scope, offset); - } - } -| Declarateur { - if(!is_array){ - gen_declaration($1, $0, scope); - }else{ - gen_tab_declaration($1, scope, offset); - } - } + Declarateurs ',' Declarateur { if (is_array) gen_tab_declaration($3, scope, offset); + else gen_declaration($3, $0, scope); } +| Declarateur { if (is_array) gen_tab_declaration($1, scope, offset); + else gen_declaration($1, $0, scope); } ; Declarateur: - IDENT {strcpy($$,$1);is_array = 0;} -| IDENT '[' NUM ']' {offset = $3;strcpy($$,$1);is_array=1;} + IDENT { strcpy($$, $1); is_array = false; } +| IDENT '[' NUM ']' { offset = $3; strcpy($$, $1); is_array = true; } ; DeclFoncts: DeclFoncts DeclFonct | DeclFonct ; DeclFonct: - EnTeteFonct { scope = LOCAL; } - Corps { gen_function_end_declaration(fname,return_type,nb_param[num_scope]); scope = GLOBAL; return_type = VOID_T; num_scope--; loc_clean_table(); } + EnTeteFonct { scope = LOCAL; } + Corps { gen_function_end_declaration(fname, return_type, nb_param[num_scope]); + scope = GLOBAL; return_type = VOID_T; num_scope--; loc_clean_table(); } ; EnTeteFonct: - TYPE IDENT PrologueCont {strcpy(fname,$2); return_type = gen_function_declaration($2, $1);} '(' Parametres ')' { nb_param[++num_scope] = $6 ; scope = GLOBAL;} -| VOID IDENT PrologueCont {strcpy(fname,$2); return_type = gen_function_declaration($2, VOID_T);} '(' Parametres ')' { nb_param[++num_scope] = $6 ; scope = GLOBAL; } + TYPE IDENT PrologueCont { strcpy(fname, $2); + return_type = gen_function_declaration($2, $1); } + '(' Parametres ')' { nb_param[++num_scope] = $6; scope = GLOBAL;} +| VOID IDENT PrologueCont { strcpy(fname, $2); + return_type = gen_function_declaration($2, VOID_T); } + '(' Parametres ')' { nb_param[++num_scope] = $6; scope = GLOBAL; } ; -PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);}; +PrologueCont: { scope = LOCAL; gen_prologue_continue(&bss_done); }; Parametres: - VOID {$$ = 0;} -| ListTypVar { $$ = $1;} + VOID { $$ = 0; } +| ListTypVar { $$ = $1; } ; ListTypVar: - ListTypVar ',' TYPE IDENT { gen_declaration($4, $3, scope); $$ = $1+1; } -| TYPE IDENT { gen_declaration($2, $1, scope); $$ = 1; } + ListTypVar ',' TYPE IDENT { gen_declaration($4, $3, scope); $$ = $1+1; } +| TYPE IDENT { gen_declaration($2, $1, scope); $$ = 1; } ; Corps: - '{' {int i; for(i=1;i<=nb_param[num_scope];i++){fprintf(output, "mov r8, [rbp + %d]\nmov [rbp - %d], r8\n", (nb_param[num_scope]-i+2)*8, i*8);} } DeclConsts DeclVars SuiteInstr '}' + '{' { int i; + for(i=1;i<=nb_param[num_scope];i++){ + fprintf(output, "mov r8, [rbp + %d]\nmov [rbp - %d], r8\n", (nb_param[num_scope]-i+2)*8, i*8); + } } + DeclConsts DeclVars SuiteInstr '}' ; SuiteInstr: SuiteInstr Instr @@ -128,67 +127,70 @@ SuiteInstr: Instr: Exp ';' | ';' -| RETURN Exp ';' { gen_function_return(return_type, $2); } -| RETURN ';' { gen_function_return(return_type, VOID_T);} -| READE '(' IDENT ')' ';' { gen_reade($3, scope); } -| READC '(' IDENT ')' ';' { gen_readc($3, scope); } -| PRINT '(' Exp ')' ';' { gen_print($3);} -| IF '(' Exp IfHandling')' Instr { gen_if_label($4); } -| IF '(' Exp IfHandling')' Instr ELSE IfEndHandling Instr IfElseEndHandling -| WHILE {fprintf(output,".upwhile%d:\n",num_while);}'(' Exp {fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n",num_while);}')' Instr {fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n",num_while,num_while);num_while++;} +| RETURN Exp ';' { gen_function_return(return_type, $2); } +| RETURN ';' { gen_function_return(return_type, VOID_T); } +| READE '(' IDENT ')' ';' { gen_reade($3, scope); } +| READC '(' IDENT ')' ';' { gen_readc($3, scope); } +| PRINT '(' Exp ')' ';' { gen_print($3);} +| IF '(' Exp IfHandling')' Instr { gen_if_label($4); } +| IF '(' Exp IfHandling')' Instr + ELSE IfEndHandling Instr IfElseEndHandling +| WHILE { fprintf(output,".upwhile%d:\n", num_while); } + '(' Exp { fprintf(output,"pop rax\ncmp rax,0\njz .downwhile%d\n", num_while); } + ')' Instr { fprintf(output,"jmp .upwhile%d\n.downwhile%d:\n", num_while, num_while); num_while++; } | '{' SuiteInstr '}' ; -IfHandling: { gen_if_start($$ = num_if++); }; -IfEndHandling: { gen_if_end($-3); }; -IfElseEndHandling: { gen_ifelse_end($-5); }; +IfHandling: { gen_if_start($$ = num_if++); }; +IfEndHandling: { gen_if_end($-3); }; +IfElseEndHandling: { gen_ifelse_end($-5); }; Exp: - LValue '=' Exp { $$ = gen_assign($1, scope); } + LValue '=' Exp { $$ = gen_assign($1, scope); } | EB ; EB: - EB OR TB { gen_or($1, $3, num_label++); } + EB OR TB { gen_or($1, $3, num_label++); } | TB ; TB: - TB AND FB { gen_and($1, $3, num_label++); } + TB AND FB { gen_and($1, $3, num_label++); } | FB ; FB: - FB EQ M { gen_eq($2, $1, $3, num_label++); } + FB EQ M { gen_eq($2, $1, $3, num_label++); } | M ; M: - M ORDER E { gen_order($2, $1, $3, num_label++); } + M ORDER E { gen_order($2, $1, $3, num_label++); } | E ; E: - E ADDSUB T { gen_addsub($2, $1, $3); } + E ADDSUB T { gen_addsub($2, $1, $3); } | T ; T: - T DIVSTAR F { gen_divstar($2, $1, $3); } + T DIVSTAR F { gen_divstar($2, $1, $3); } | F ; F: - ADDSUB F { $$ = gen_signed_expr($1, $2); } -| '!' F { $$ = gen_negate_expr($2); } -| '(' Exp ')' { $$ = $2; } -| LValue { $$ = gen_value($1, scope); } -| NUM { $$ = gen_num($1, scope); } -| CARACTERE { $$ = gen_char($1, scope); } -| IDENT '(' Arguments ')' { $$ = gen_function_call($1,$3); } + ADDSUB F { $$ = gen_signed_expr($1, $2); } +| '!' F { $$ = gen_negate_expr($2); } +| '(' Exp ')' { $$ = $2; } +| LValue { $$ = gen_value($1, scope); } +| NUM { $$ = gen_num($1, scope); } +| CARACTERE { $$ = gen_char($1, scope); } +| IDENT '(' Arguments ')' { $$ = gen_function_call($1, $3); } ; LValue: - IDENT { gen_check($1, scope); strcpy($$, $1);} -| IDENT '[' Exp ']' { gen_check($1, scope); strcpy($$, $1);} + IDENT { gen_check($1, scope); strcpy($$, $1);} +| IDENT '[' Exp ']' { gen_check($1, scope); strcpy($$, $1);} ; Arguments: ListExp | {$$ = 0;} ; ListExp: - ListExp ',' Exp {$$ = $1 + 1;} -| Exp {$$ = 1;} + ListExp ',' Exp { $$ = $1 + 1; } +| Exp { $$ = 1; } ; %% -- cgit v1.2.3