aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAdam NAILI2018-06-05 19:53:58 +0200
committerAdam NAILI2018-06-05 19:53:58 +0200
commita67cd40065f790383776ec3fffa364f6443f7ee7 (patch)
tree420f183d70f4278cebe2e670964c064598337bea /src
parent7138ea2f185c6963cfd09a13cfc289d4d1452858 (diff)
downloadtpc-compiler-a67cd40065f790383776ec3fffa364f6443f7ee7.tar.gz
Parameters handling (without checking types on parameters)
Diffstat (limited to 'src')
-rw-r--r--src/generator.c6
-rw-r--r--src/generator.h4
-rw-r--r--src/tpc.y20
3 files changed, 16 insertions, 14 deletions
diff --git a/src/generator.c b/src/generator.c
index 37581e0..b8a0b8f 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -90,13 +90,13 @@ void gen_const_declaration() {
90 fun_display_table(); 90 fun_display_table();
91} 91}
92 92
93Type gen_function_declaration(const char name[], int return_type, int nb_param) { 93Type gen_function_declaration(const char name[], int return_type) {
94 fun_add(name, return_type, nb_param);
95 fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name); 94 fprintf(output, "\n%s:\npush rbp\nmov rbp,rsp\n", name);
96 return return_type; 95 return return_type;
97} 96}
98 97
99void gen_function_end_declaration() { 98void gen_function_end_declaration(const char name[], int return_type, int nb_param) {
99 fun_add(name, return_type, nb_param);
100 fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n"); 100 fprintf(output, "mov rax,-1\nmov rsp, rbp\npop rbp\nret\n");
101} 101}
102 102
diff --git a/src/generator.h b/src/generator.h
index 082e359..41a769a 100644
--- a/src/generator.h
+++ b/src/generator.h
@@ -21,8 +21,8 @@ FILE *output;
21void gen_prologue(); 21void gen_prologue();
22void gen_prologue_continue(int *bss_done); 22void gen_prologue_continue(int *bss_done);
23void gen_const_declaration(); 23void gen_const_declaration();
24Type gen_function_declaration(const char name[], int return_type, int nb_param); 24Type gen_function_declaration(const char name[], int return_type);
25void gen_function_end_declaration(); 25void gen_function_end_declaration(const char name[], int return_type, int nb_param);
26void gen_function_return(Type expect, Type actual); 26void gen_function_return(Type expect, Type actual);
27Type gen_function_call(const char name[], int nb_param); 27Type gen_function_call(const char name[], int nb_param);
28void gen_declaration(const char name[], int type, Scope scope); 28void gen_declaration(const char name[], int type, Scope scope);
diff --git a/src/tpc.y b/src/tpc.y
index 979e696..fedd193 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -23,6 +23,8 @@ static Type return_type = VOID_T;
23static int bss_done = 0; 23static int bss_done = 0;
24static int num_label = 0; 24static int num_label = 0;
25static int num_if = 0; 25static int num_if = 0;
26static int nb_param = 0;
27static char fname[64];
26%} 28%}
27 29
28%union { 30%union {
@@ -88,14 +90,14 @@ DeclFoncts:
88; 90;
89DeclFonct: 91DeclFonct:
90 EnTeteFonct { scope = LOCAL; } 92 EnTeteFonct { scope = LOCAL; }
91 Corps { gen_function_end_declaration(); scope = GLOBAL; return_type = VOID_T; } 93 Corps { gen_function_end_declaration(fname,return_type,nb_param); scope = GLOBAL; return_type = VOID_T; }
92; 94;
93EnTeteFonct: 95EnTeteFonct:
94 TYPE IDENT PrologueCont '(' Parametres ')' { return_type = gen_function_declaration($<ident>2, $<type>1, $5); } 96 TYPE IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, $<type>1);} '(' Parametres ')' { nb_param = $<num>6 ; scope = GLOBAL;}
95| VOID IDENT PrologueCont '(' Parametres ')' { return_type = gen_function_declaration($<ident>2, VOID_T, $5); } 97| VOID IDENT PrologueCont {strcpy(fname,$<ident>2); return_type = gen_function_declaration($<ident>2, VOID_T);} '(' Parametres ')' { nb_param = $<num>6 ; scope = GLOBAL; }
96; 98;
97 99
98PrologueCont: {gen_prologue_continue(&bss_done);}; 100PrologueCont: {scope = LOCAL;gen_prologue_continue(&bss_done);};
99 101
100Parametres: 102Parametres:
101 VOID {$$ = 0;} 103 VOID {$$ = 0;}
@@ -106,7 +108,7 @@ ListTypVar:
106| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; } 108| TYPE IDENT { gen_declaration($<ident>2, $<type>1, scope); $<num>$ = 1; }
107; 109;
108Corps: 110Corps:
109 '{' DeclConsts DeclVars SuiteInstr '}' 111 '{' {int i; for(i=1;i<=nb_param;i++){fprintf(output, "mov r8, [rbp + %d]\nmov [rbp - %d], r8\n", (nb_param-i+2)*8, i*8);} } DeclConsts DeclVars SuiteInstr '}'
110; 112;
111SuiteInstr: 113SuiteInstr:
112 SuiteInstr Instr 114 SuiteInstr Instr
@@ -115,8 +117,8 @@ SuiteInstr:
115Instr: 117Instr:
116 Exp ';' 118 Exp ';'
117| ';' 119| ';'
118| RETURN Exp ';' { gen_function_return(return_type, $<type>2); scope = GLOBAL; return_type = VOID_T; } 120| RETURN Exp ';' { gen_function_return(return_type, $<type>2); }
119| RETURN ';' { gen_function_return(return_type, VOID_T); scope = GLOBAL; return_type = VOID_T; } 121| RETURN ';' { gen_function_return(return_type, VOID_T);}
120| READE '(' IDENT ')' ';' { gen_reade($<ident>3); } 122| READE '(' IDENT ')' ';' { gen_reade($<ident>3); }
121| READC '(' IDENT ')' ';' { gen_readc($<ident>3); } 123| READC '(' IDENT ')' ';' { gen_readc($<ident>3); }
122| PRINT '(' Exp ')' ';' { gen_print($<type>3);} 124| PRINT '(' Exp ')' ';' { gen_print($<type>3);}
@@ -166,8 +168,8 @@ F:
166| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); } 168| IDENT '(' Arguments ')' { $$ = gen_function_call($<ident>1,$<num>3); }
167; 169;
168LValue: 170LValue:
169 IDENT { $$ = $1; gen_check($<ident>1, scope); } 171 IDENT { gen_check($<ident>1, scope); }
170| IDENT '[' Exp ']' { $$ = $1; gen_check($<ident>1, scope); } 172| IDENT '[' Exp ']' { gen_check($<ident>1, scope); }
171; 173;
172Arguments: 174Arguments:
173 ListExp 175 ListExp