aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/generator.c53
-rw-r--r--src/generator.h18
-rw-r--r--src/tpc.y108
3 files changed, 104 insertions, 75 deletions
diff --git a/src/generator.c b/src/generator.c
new file mode 100644
index 0000000..a7a7c72
--- /dev/null
+++ b/src/generator.c
@@ -0,0 +1,53 @@
1/**
2 * UPEM / Compilation / Projet
3 * Pacien TRAN-GIRARD, Adam NAILI
4 */
5
6#include "generator.h"
7#include <stdio.h>
8#include "symbol_table.h"
9
10void prologue() {
11 printf("extern printf\n");
12 printf("section .data\n");
13 printf("format_int db \"%%d\",10,0\n\n");
14 printf("section .bss\nsection .text\n\nglobal _start\n");
15 printf("print: ;print needs an argument in rax\n");
16 printf("push rbp\n");
17 printf("mov rbp, rsp\n");
18 printf("push rsi\n");
19 printf("mov rsi, rax\n");
20 printf("mov rdi, format_int\n");
21 printf("mov rax, 0\n");
22 printf("call printf WRT ..plt\n");
23 printf("pop rsi\n");
24 printf("pop rbp\n");
25 printf("ret\n");
26 printf("\n_start:\n");
27 printf("push rbp\nmov rbp, rsp\n\n");
28}
29
30void const_declaration() {
31 printf("mov rax,60 \n");
32 printf("mov rdi,0 \n");
33 printf("syscall \n");
34 printf(";global table\n");
35 glo_display_table();
36 printf(";local table\n");
37 loc_display_table();
38}
39
40void declaration(const char name[], int type, Scope scope) {
41 switch (scope) {
42 case GLOBAL:
43 glo_addVar(name, type);
44 break;
45 case LOCAL:
46 loc_addVar(name, type);
47 break;
48 }
49
50 printf("push 0\n");
51}
52
53
diff --git a/src/generator.h b/src/generator.h
new file mode 100644
index 0000000..53acdef
--- /dev/null
+++ b/src/generator.h
@@ -0,0 +1,18 @@
1/**
2 * UPEM / Compilation / Projet
3 * Pacien TRAN-GIRARD, Adam NAILI
4 */
5
6#ifndef __GENERATOR_H__
7#define __GENERATOR_H__
8
9typedef enum scope {
10 GLOBAL,
11 LOCAL
12} Scope;
13
14void prologue();
15void const_declaration();
16void declaration(const char name[], int type, Scope scope);
17
18#endif
diff --git a/src/tpc.y b/src/tpc.y
index b0d3633..14f3488 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -13,13 +13,12 @@
13 13
14#include <stdio.h> 14#include <stdio.h>
15#include "symbol_table.h" 15#include "symbol_table.h"
16#include "generator.h"
16 17
17extern int lineno; 18extern int lineno;
18int yylex(); 19int yylex();
19void yyerror(char *); 20void yyerror(char *);
20#define GLOBAL 0 21static Scope scope = GLOBAL;
21#define LOCAL 1
22static int status = GLOBAL;
23static int num_label = 0; 22static int num_label = 0;
24static int num_if = 0; 23static int num_if = 0;
25%} 24%}
@@ -50,100 +49,59 @@ static int num_if = 0;
50%precedence ELSE 49%precedence ELSE
51 50
52%% 51%%
53Prog:{printf("extern printf\n"); 52Prog: { prologue(); }
54 printf("section .data\n"); 53 DeclConsts DeclVars DeclFoncts { const_declaration(); }
55 printf("format_int db \"%%d\",10,0\n\n");
56 printf("section .bss\nsection .text\n\nglobal _start\n");
57 printf("print: ;print needs an argument in rax\n");
58 printf("push rbp\n");
59 printf("mov rbp, rsp\n");
60 printf("push rsi\n");
61 printf("mov rsi, rax\n");
62 printf("mov rdi, format_int\n");
63 printf("mov rax, 0\n");
64 printf("call printf WRT ..plt\n");
65 printf("pop rsi\n");
66 printf("pop rbp\n");
67 printf("ret\n");
68 printf("\n_start:\n");
69 printf("push rbp\nmov rbp, rsp\n\n");
70 }
71 DeclConsts DeclVars DeclFoncts
72 {
73 printf("mov rax,60 \n");
74 printf("mov rdi,0 \n");
75 printf("syscall \n");
76 printf(";global table\n");
77 glo_display_table();
78 printf(";local table\n");
79 loc_display_table();
80 }
81 ; 54 ;
82DeclConsts: 55DeclConsts:
83 DeclConsts CONST ListConst ';' 56 DeclConsts CONST ListConst ';'
84 | 57 |
85 ; 58 ;
86ListConst: 59ListConst:
87 ListConst ',' IDENT '=' Litteral 60 ListConst ',' IDENT '=' Litteral
88 | IDENT '=' Litteral 61 | IDENT '=' Litteral
89 ; 62 ;
90Litteral: 63Litteral:
91 NombreSigne 64 NombreSigne
92 | CARACTERE 65 | CARACTERE
93 ; 66 ;
94NombreSigne: 67NombreSigne:
95 NUM 68 NUM
96 | ADDSUB NUM 69 | ADDSUB NUM
97 ; 70 ;
98DeclVars: 71DeclVars:
99 DeclVars TYPE Declarateurs ';' 72 DeclVars TYPE Declarateurs ';'
100 | 73 |
101 ; 74 ;
102Declarateurs: 75Declarateurs:
103 Declarateurs ',' Declarateur { 76 Declarateurs ',' Declarateur { declaration($<ident>3, $<type>0, scope); }
104 if(status == GLOBAL) glo_addVar($<ident>3, $<type>0); 77 | Declarateur { declaration($<ident>1, $<type>0, scope); }
105 else loc_addVar($<ident>3, $<type>0);
106 printf("push 0\n");
107 }
108 | Declarateur {
109 if(status == GLOBAL) glo_addVar($<ident>1, $<type>0);
110 else loc_addVar($<ident>1, $<type>0);
111 printf("push 0\n");
112 }
113 ; 78 ;
114Declarateur: 79Declarateur:
115 IDENT 80 IDENT
116 | IDENT '[' NUM ']' 81 | IDENT '[' NUM ']'
117 ; 82 ;
118DeclFoncts: 83DeclFoncts:
119 DeclFoncts DeclFonct 84 DeclFoncts DeclFonct
120 | DeclFonct 85 | DeclFonct
121 ; 86 ;
122DeclFonct: 87DeclFonct:
123 EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;} 88 EnTeteFonct { scope = LOCAL; }
89 Corps { scope = GLOBAL; }
124 ; 90 ;
125EnTeteFonct: 91EnTeteFonct:
126 TYPE IDENT '(' Parametres ')' 92 TYPE IDENT '(' Parametres ')'
127 | VOID IDENT '(' Parametres ')' 93 | VOID IDENT '(' Parametres ')'
128 ; 94 ;
129Parametres: 95Parametres:
130 VOID 96 VOID
131 | ListTypVar 97 | ListTypVar
132 ; 98 ;
133ListTypVar: 99ListTypVar:
134 ListTypVar ',' TYPE IDENT { 100 ListTypVar ',' TYPE IDENT { declaration($<ident>4, $<type>3, scope); }
135 if(status == GLOBAL) glo_addVar($<ident>4, $<type>3); 101 | TYPE IDENT { declaration($<ident>2, $<type>1, scope); }
136 else loc_addVar($<ident>4, $<type>3);
137 printf("push 0\n");
138 }
139 | TYPE IDENT {
140 if(status == GLOBAL) glo_addVar($<ident>2, $<type>1);
141 else loc_addVar($<ident>2, $<type>1);
142 printf("push 0\n");
143 }
144 ; 102 ;
145Corps: 103Corps:
146 '{' DeclConsts DeclVars SuiteInstr '}' 104 '{' DeclConsts DeclVars SuiteInstr '}'
147 ; 105 ;
148SuiteInstr: 106SuiteInstr:
149 SuiteInstr Instr 107 SuiteInstr Instr
@@ -154,11 +112,11 @@ Instr:
154 | RETURN Exp ';' 112 | RETURN Exp ';'
155 | RETURN ';' 113 | RETURN ';'
156 | READE '(' IDENT ')' ';' { 114 | READE '(' IDENT ')' ';' {
157 if(status == GLOBAL) glo_lookup($<ident>3); 115 if(scope == GLOBAL) glo_lookup($<ident>3);
158 else loc_lookup($<ident>3); 116 else loc_lookup($<ident>3);
159 } 117 }
160 | READC '(' IDENT ')' ';' { 118 | READC '(' IDENT ')' ';' {
161 if(status == GLOBAL) glo_lookup($<ident>3); 119 if(scope == GLOBAL) glo_lookup($<ident>3);
162 else loc_lookup($<ident>3); 120 else loc_lookup($<ident>3);
163 } 121 }
164 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");} 122 | PRINT '(' Exp ')' ';' {printf("pop rax\ncall print\n");}
@@ -172,7 +130,7 @@ IfEndHandling: {printf("jmp .end_ifelse%d\n.end_if%d:\n",$<nu
172IfElseEndHandling: {printf(".end_ifelse%d:\n;ENDIF\n\n",$<num>-5);}; 130IfElseEndHandling: {printf(".end_ifelse%d:\n;ENDIF\n\n",$<num>-5);};
173Exp: 131Exp:
174 LValue '=' Exp { 132 LValue '=' Exp {
175 if(status == GLOBAL){ 133 if(scope == GLOBAL){
176 $$ = glo_lookup($<ident>1); 134 $$ = glo_lookup($<ident>1);
177 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1); 135 printf("pop QWORD [rbp - %d] ;%s\n",glo_get_addr($<ident>1),$<ident>1);
178 } 136 }