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