diff options
author | Adam NAILI | 2018-05-14 15:48:01 +0200 |
---|---|---|
committer | Adam NAILI | 2018-05-14 15:48:01 +0200 |
commit | 67fc7933df394848eeb86a45f37e35ded032e5e9 (patch) | |
tree | f5453f99cbbea8267f765120389372d7f5d1a32d | |
parent | 337ca2a847e6c5f7555c90807f13412c349b753f (diff) | |
download | tpc-compiler-67fc7933df394848eeb86a45f37e35ded032e5e9.tar.gz |
Not working properly: Beginning of the implementation of globals -> need to modify if(global) else
-rw-r--r-- | src/generator.c | 10 | ||||
-rw-r--r-- | src/generator.h | 3 | ||||
-rw-r--r-- | src/tpc.y | 7 |
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 | } | ||
18 | void 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 | |||
32 | void gen_const_declaration() { | 35 | void 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 | ||
14 | extern int nb_globals; | ||
15 | |||
14 | void gen_prologue(); | 16 | void gen_prologue(); |
17 | void gen_prologue_continue(); | ||
15 | void gen_const_declaration(); | 18 | void gen_const_declaration(); |
16 | void gen_declaration(const char name[], int type, Scope scope); | 19 | void gen_declaration(const char name[], int type, Scope scope); |
17 | 20 | ||
@@ -12,6 +12,7 @@ | |||
12 | */ | 12 | */ |
13 | 13 | ||
14 | #include <stdio.h> | 14 | #include <stdio.h> |
15 | int 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 | ; |
91 | EnTeteFonct: | 92 | EnTeteFonct: |
92 | TYPE IDENT '(' Parametres ')' | 93 | TYPE IDENT Prologue '(' Parametres ')' |
93 | | VOID IDENT '(' Parametres ')' | 94 | | VOID IDENT Prologue '(' Parametres ')' |
94 | ; | 95 | ; |
96 | Prologue: {gen_prologue_continue();}; | ||
97 | |||
95 | Parametres: | 98 | Parametres: |
96 | VOID | 99 | VOID |
97 | | ListTypVar | 100 | | ListTypVar |