diff options
author | Adam NAILI | 2018-05-04 01:03:21 +0200 |
---|---|---|
committer | Adam NAILI | 2018-05-04 01:03:21 +0200 |
commit | c9c4fbbde6e4f4f3942777cd9930cf40375e0ea9 (patch) | |
tree | dc6c860b2934dadd75a000bd797934371a651f64 /src/tpc.y | |
parent | 2793b31c330396956e4fbeef454e26a37b055ebd (diff) | |
download | tpc-compiler-c9c4fbbde6e4f4f3942777cd9930cf40375e0ea9.tar.gz |
Add sub working (local/global not fully implemented, checking type function available
Diffstat (limited to 'src/tpc.y')
-rw-r--r-- | src/tpc.y | 92 |
1 files changed, 66 insertions, 26 deletions
@@ -10,10 +10,10 @@ | |||
10 | extern int lineno; | 10 | extern int lineno; |
11 | int yylex(); | 11 | int yylex(); |
12 | void yyerror(char *); | 12 | void yyerror(char *); |
13 | |||
14 | #define GLOBAL 0 | 13 | #define GLOBAL 0 |
15 | #define LOCAL 1 | 14 | #define LOCAL 1 |
16 | static int status = GLOBAL; | 15 | static int status = GLOBAL; |
16 | |||
17 | %} | 17 | %} |
18 | 18 | ||
19 | %union { | 19 | %union { |
@@ -44,9 +44,9 @@ static int status = GLOBAL; | |||
44 | %% | 44 | %% |
45 | Prog:{printf("extern printf\n"); | 45 | Prog:{printf("extern printf\n"); |
46 | printf("section .data\n"); | 46 | printf("section .data\n"); |
47 | printf("format_int db \"%%d \\n\",10,0\n\n"); | 47 | printf("format_int db \"%%d\",10,0\n\n"); |
48 | printf("section .bss\nsection .text\n\nglobal _start\n"); | 48 | printf("section .bss\nsection .text\n\nglobal _start\n"); |
49 | printf("print: ;print needs an argument in rax\n\n"); | 49 | printf("print: ;print needs an argument in rax\n"); |
50 | printf("push rbp\n"); | 50 | printf("push rbp\n"); |
51 | printf("mov rbp, rsp\n"); | 51 | printf("mov rbp, rsp\n"); |
52 | printf("push rsi\n"); | 52 | printf("push rsi\n"); |
@@ -58,12 +58,17 @@ Prog:{printf("extern printf\n"); | |||
58 | printf("pop rbp\n"); | 58 | printf("pop rbp\n"); |
59 | printf("ret\n"); | 59 | printf("ret\n"); |
60 | printf("\n_start:\n"); | 60 | printf("\n_start:\n"); |
61 | printf("push rbp\nmov rbp, rsp\n\n"); | ||
61 | } | 62 | } |
62 | DeclConsts DeclVars DeclFoncts | 63 | DeclConsts DeclVars DeclFoncts |
63 | { | 64 | { |
64 | printf("mov rax,60 \n"); | 65 | printf("mov rax,60 \n"); |
65 | printf("mov rdi,0 \n"); | 66 | printf("mov rdi,0 \n"); |
66 | printf("syscall \n"); | 67 | printf("syscall \n"); |
68 | printf(";global table\n"); | ||
69 | glo_display_table(); | ||
70 | printf(";local table\n"); | ||
71 | loc_display_table(); | ||
67 | } | 72 | } |
68 | ; | 73 | ; |
69 | DeclConsts: | 74 | DeclConsts: |
@@ -87,8 +92,12 @@ DeclVars: | |||
87 | | | 92 | | |
88 | ; | 93 | ; |
89 | Declarateurs: | 94 | Declarateurs: |
90 | Declarateurs ',' Declarateur {glo_addVar($<ident>3, $<type>0);} | 95 | Declarateurs ',' Declarateur {if(status == GLOBAL) glo_addVar($<ident>3, $<type>0); |
91 | | Declarateur {glo_addVar($<ident>1, $<type>0);} | 96 | else loc_addVar($<ident>3, $<type>0); |
97 | printf("push 0\n");} | ||
98 | | Declarateur {if(status == GLOBAL) glo_addVar($<ident>1, $<type>0); | ||
99 | else loc_addVar($<ident>1, $<type>0); | ||
100 | printf("push 0\n");} | ||
92 | ; | 101 | ; |
93 | Declarateur: | 102 | Declarateur: |
94 | IDENT | 103 | IDENT |
@@ -110,8 +119,12 @@ Parametres: | |||
110 | | ListTypVar | 119 | | ListTypVar |
111 | ; | 120 | ; |
112 | ListTypVar: | 121 | ListTypVar: |
113 | ListTypVar ',' TYPE IDENT {glo_addVar($<ident>4, $<type>3);} | 122 | ListTypVar ',' TYPE IDENT {if(status == GLOBAL) glo_addVar($<ident>4, $<type>3); |
114 | | TYPE IDENT {glo_addVar($<ident>2, $<type>1);} | 123 | else loc_addVar($<ident>4, $<type>3); |
124 | printf("push 0\n");} | ||
125 | | TYPE IDENT {if(status == GLOBAL) glo_addVar($<ident>2, $<type>1); | ||
126 | else loc_addVar($<ident>2, $<type>1); | ||
127 | printf("push 0\n");} | ||
115 | ; | 128 | ; |
116 | Corps: | 129 | Corps: |
117 | '{' DeclConsts DeclVars SuiteInstr '}' | 130 | '{' DeclConsts DeclVars SuiteInstr '}' |
@@ -124,54 +137,81 @@ Instr: | |||
124 | | ';' | 137 | | ';' |
125 | | RETURN Exp ';' | 138 | | RETURN Exp ';' |
126 | | RETURN ';' | 139 | | RETURN ';' |
127 | | READE '(' IDENT ')' ';' {glo_lookup($<ident>3);} | 140 | | READE '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3); |
128 | | READC '(' IDENT ')' ';' {glo_lookup($<ident>3);} | 141 | else loc_lookup($<ident>3);} |
129 | | PRINT '(' Exp ')' ';' | 142 | | READC '(' IDENT ')' ';' {if(status == GLOBAL) glo_lookup($<ident>3); |
143 | else loc_lookup($<ident>3);} | ||
144 | | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} | ||
130 | | IF '(' Exp ')' Instr | 145 | | IF '(' Exp ')' Instr |
131 | | IF '(' Exp ')' Instr ELSE Instr | 146 | | IF '(' Exp ')' Instr ELSE Instr |
132 | | WHILE '(' Exp ')' Instr | 147 | | WHILE '(' Exp ')' Instr |
133 | | '{' SuiteInstr '}' | 148 | | '{' SuiteInstr '}' |
134 | ; | 149 | ; |
135 | Exp: | 150 | Exp: |
136 | LValue '=' Exp {$$ = glo_lookup($<ident>1);} | 151 | LValue '=' Exp {if(status == GLOBAL){ |
152 | $$ = glo_lookup($<ident>1); | ||
153 | printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1); | ||
154 | } | ||
155 | else { | ||
156 | $$ = loc_lookup($<ident>1); | ||
157 | printf("pop QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1); | ||
158 | } | ||
159 | } | ||
137 | | EB | 160 | | EB |
138 | ; | 161 | ; |
139 | EB: | 162 | EB: |
140 | EB OR TB | 163 | EB OR TB {check_expected_type($1,INT);check_expected_type($3,INT);} |
141 | | TB | 164 | | TB |
142 | ; | 165 | ; |
143 | TB: | 166 | TB: |
144 | TB AND FB | 167 | TB AND FB {check_expected_type($1,INT);check_expected_type($3,INT);} |
145 | | FB | 168 | | FB |
146 | ; | 169 | ; |
147 | FB: | 170 | FB: |
148 | FB EQ M | 171 | FB EQ M {check_expected_type($1,INT);check_expected_type($3,INT);} |
149 | | M | 172 | | M |
150 | ; | 173 | ; |
151 | M: | 174 | M: |
152 | M ORDER E | 175 | M ORDER E {check_expected_type($1,INT);check_expected_type($3,INT);} |
153 | | E | 176 | | E |
154 | ; | 177 | ; |
155 | E: | 178 | E: |
156 | E ADDSUB T | 179 | E ADDSUB T {check_expected_type($1,INT);check_expected_type($3,INT); |
180 | if($2 == '+'){ | ||
181 | printf(";E + T\npop rcx\npop rax\nadd rax,rcx\npush rax\n"); | ||
182 | } | ||
183 | else{ | ||
184 | printf(";E - T\npop rcx\npop rax\nsub rax,rcx\npush rax\n"); | ||
185 | } | ||
186 | } | ||
157 | | T | 187 | | T |
158 | ; | 188 | ; |
159 | T: | 189 | T: |
160 | T DIVSTAR F | 190 | T DIVSTAR F {check_expected_type($1,INT);check_expected_type($3,INT);} |
161 | | F | 191 | | F |
162 | ; | 192 | ; |
163 | F: | 193 | F: |
164 | ADDSUB F {$$ = $2;} //on fait remonter le type | 194 | ADDSUB F {$$ = $2;//on fait remonter le type |
165 | | '!' F {$$ = $2;} | 195 | if($1 == '+') { |
166 | | '(' Exp ')' {$$ = $2;} | 196 | printf(";+F\n"); |
167 | | LValue {$$ = glo_lookup($<ident>1);} | 197 | } |
168 | | NUM {$$ = INT;} // on stocke les types pour l'analyse sémantique | 198 | else{ |
169 | | CARACTERE {$$ = CHAR;} | 199 | printf(";-F\npop rdx\nxor eax,eax\nsub eax,edx\npush rax\n"); |
170 | | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement | 200 | } |
201 | } | ||
202 | | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} | ||
203 | | '(' Exp ')' {$$ = $2;} | ||
204 | | LValue {if(status == GLOBAL) {$$ = glo_lookup($<ident>1);printf("push QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);} | ||
205 | else {$$ = loc_lookup($<ident>1); printf("push QWORD [rbp - %d] ;%s\n",loc_get_addr($<ident>1),$<ident>1);}} | ||
206 | | NUM {$$ = INT; printf("push %d\n",$1);} // on stocke les types pour l'analyse sémantique | ||
207 | | CARACTERE {$$ = CHAR; printf("push %d\n",$1);} | ||
208 | | IDENT '(' Arguments ')' {$$ = INT;} //tableau d'entiers uniquement | ||
171 | ; | 209 | ; |
172 | LValue: | 210 | LValue: |
173 | IDENT {glo_lookup($<ident>1);} | 211 | IDENT {if(status == GLOBAL) {glo_lookup($<ident>1);} |
174 | | IDENT '[' Exp ']' {glo_lookup($<ident>1);} | 212 | else {loc_lookup($<ident>1);}} |
213 | | IDENT '[' Exp ']' {if(status == GLOBAL) {glo_lookup($<ident>1);} | ||
214 | else {loc_lookup($<ident>1);}} | ||
175 | ; | 215 | ; |
176 | Arguments: | 216 | Arguments: |
177 | ListExp | 217 | ListExp |