aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/generator.c10
-rw-r--r--src/generator.h3
-rw-r--r--src/tpc.y7
3 files changed, 15 insertions, 5 deletions
diff --git a/src/generator.c b/src/generator.c
index e547a23..88f32d7 100644
--- a/src/generator.c
+++ b/src/generator.c
@@ -13,7 +13,11 @@ void gen_prologue() {
13 printf("extern printf\n"); 13 printf("extern printf\n");
14 printf("section .data\n"); 14 printf("section .data\n");
15 printf("format_int db \"%%d\",10,0\n\n"); 15 printf("format_int db \"%%d\",10,0\n\n");
16 printf("section .bss\nsection .text\n\nglobal _start\n"); 16 printf("section .bss\n");
17}
18void gen_prologue_continue(){
19 printf("globals: resq %d\n", nb_globals);
20 printf("section .text\n\nglobal _start\n");
17 printf("print: ;print needs an argument in rax\n"); 21 printf("print: ;print needs an argument in rax\n");
18 printf("push rbp\n"); 22 printf("push rbp\n");
19 printf("mov rbp, rsp\n"); 23 printf("mov rbp, rsp\n");
@@ -28,7 +32,6 @@ void gen_prologue() {
28 printf("\n_start:\n"); 32 printf("\n_start:\n");
29 printf("push rbp\nmov rbp, rsp\n\n"); 33 printf("push rbp\nmov rbp, rsp\n\n");
30} 34}
31
32void gen_const_declaration() { 35void gen_const_declaration() {
33 printf("mov rax,60 \n"); 36 printf("mov rax,60 \n");
34 printf("mov rdi,0 \n"); 37 printf("mov rdi,0 \n");
@@ -43,13 +46,14 @@ void gen_declaration(const char name[], int type, Scope scope) {
43 switch (scope) { 46 switch (scope) {
44 case GLOBAL: 47 case GLOBAL:
45 glo_addVar(name, type); 48 glo_addVar(name, type);
49 nb_globals++;
46 break; 50 break;
47 case LOCAL: 51 case LOCAL:
48 loc_addVar(name, type); 52 loc_addVar(name, type);
53 printf("push 0\n");
49 break; 54 break;
50 } 55 }
51 56
52 printf("push 0\n");
53} 57}
54 58
55// ----- READ AND PRINT FUNCTIONS ----- 59// ----- READ AND PRINT FUNCTIONS -----
diff --git a/src/generator.h b/src/generator.h
index c907f66..daa7339 100644
--- a/src/generator.h
+++ b/src/generator.h
@@ -11,7 +11,10 @@ typedef enum scope {
11 LOCAL 11 LOCAL
12} Scope; 12} Scope;
13 13
14extern int nb_globals;
15
14void gen_prologue(); 16void gen_prologue();
17void gen_prologue_continue();
15void gen_const_declaration(); 18void gen_const_declaration();
16void gen_declaration(const char name[], int type, Scope scope); 19void gen_declaration(const char name[], int type, Scope scope);
17 20
diff --git a/src/tpc.y b/src/tpc.y
index 17b8718..6da63ef 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -12,6 +12,7 @@
12 */ 12 */
13 13
14#include <stdio.h> 14#include <stdio.h>
15int nb_globals = 0;
15#include "symbol_table.h" 16#include "symbol_table.h"
16#include "generator.h" 17#include "generator.h"
17 18
@@ -89,9 +90,11 @@ DeclFonct:
89 Corps { scope = GLOBAL; } 90 Corps { scope = GLOBAL; }
90; 91;
91EnTeteFonct: 92EnTeteFonct:
92 TYPE IDENT '(' Parametres ')' 93 TYPE IDENT Prologue '(' Parametres ')'
93| VOID IDENT '(' Parametres ')' 94| VOID IDENT Prologue '(' Parametres ')'
94; 95;
96Prologue: {gen_prologue_continue();};
97
95Parametres: 98Parametres:
96 VOID 99 VOID
97| ListTypVar 100| ListTypVar