aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/symbol_table.c (renamed from src/symboltable.c)28
-rw-r--r--src/symbol_table.h (renamed from src/symboltable.h)26
-rw-r--r--src/tpc.lex7
-rw-r--r--src/tpc.y40
4 files changed, 55 insertions, 46 deletions
diff --git a/src/symboltable.c b/src/symbol_table.c
index d20b0dc..6f84ce4 100644
--- a/src/symboltable.c
+++ b/src/symbol_table.c
@@ -1,4 +1,9 @@
1#include "symboltable.h" 1/**
2 * UPEM / Compilation / Projet
3 * Pacien TRAN-GIRARD, Adam NAILI
4 */
5
6#include "symbol_table.h"
2 7
3extern int lineno; /* from lexical analyser */ 8extern int lineno; /* from lexical analyser */
4 9
@@ -22,7 +27,7 @@ void glo_addVar(const char name[], int type) {
22 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name); 27 strcpy(glo_symbol_table.entries[glo_symbol_table.size - 1].name, name);
23 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type; 28 glo_symbol_table.entries[glo_symbol_table.size - 1].type = type;
24 glo_symbol_table.entries[glo_symbol_table.size - 1].addr = 29 glo_symbol_table.entries[glo_symbol_table.size - 1].addr =
25 (glo_symbol_table.size - 1) * 8; 30 (glo_symbol_table.size - 1) * 8;
26} 31}
27 32
28// Verifies that the variable exists and returns the type 33// Verifies that the variable exists and returns the type
@@ -38,6 +43,7 @@ int glo_lookup(const char name[]) {
38 lineno); 43 lineno);
39 return -1; 44 return -1;
40} 45}
46
41int glo_get_addr(const char name[]) { 47int glo_get_addr(const char name[]) {
42 int count; 48 int count;
43 49
@@ -83,7 +89,7 @@ void loc_addVar(const char name[], int type) {
83 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name); 89 strcpy(loc_symbol_table.entries[loc_symbol_table.size - 1].name, name);
84 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type; 90 loc_symbol_table.entries[loc_symbol_table.size - 1].type = type;
85 loc_symbol_table.entries[loc_symbol_table.size - 1].addr = 91 loc_symbol_table.entries[loc_symbol_table.size - 1].addr =
86 (loc_symbol_table.size - 1) * 8; 92 (loc_symbol_table.size - 1) * 8;
87} 93}
88 94
89int loc_lookup(const char name[]) { 95int loc_lookup(const char name[]) {
@@ -140,12 +146,18 @@ void loc_clean_table() {
140 loc_symbol_table.size = 0; 146 loc_symbol_table.size = 0;
141} 147}
142 148
149static char *string_of_type(int type) {
150 switch (type) {
151 case INT: return "INT";
152 case CHAR: return "CHAR";
153 default: return "UNEXPECTED";
154 }
155}
156
143void check_expected_type(int type_to_check, int type_expected) { 157void check_expected_type(int type_to_check, int type_expected) {
144 if (type_to_check != type_expected) 158 if (type_to_check != type_expected)
145 fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n", 159 fprintf(stderr, "Expected type : %s -> Got type : %s (near line %d)\n",
146 type_expected == INT ? "INT" : type_expected == CHAR ? "CHAR" 160 string_of_type(type_to_check),
147 : "UNDEFINED", 161 string_of_type(type_to_check),
148 type_to_check == INT ? "INT" : type_to_check == CHAR ? "CHAR"
149 : "UNDEFINED",
150 lineno); 162 lineno);
151} \ No newline at end of file 163}
diff --git a/src/symboltable.h b/src/symbol_table.h
index ac6db36..9926fec 100644
--- a/src/symboltable.h
+++ b/src/symbol_table.h
@@ -1,4 +1,10 @@
1#ifndef __SYMBOLTABLE_H__ 1/**
2 * UPEM / Compilation / Projet
3 * Pacien TRAN-GIRARD, Adam NAILI
4 */
5
6#ifndef __SYMBOL_TABLE_H__
7#define __SYMBOL_TABLE_H__
2 8
3#include <stdio.h> 9#include <stdio.h>
4#include <stdlib.h> 10#include <stdlib.h>
@@ -9,21 +15,18 @@
9#define CHAR 1 15#define CHAR 1
10#define MAXSYMBOLS 256 16#define MAXSYMBOLS 256
11 17
12
13typedef struct { 18typedef struct {
14 char name[MAXNAME]; 19 char name[MAXNAME];
15 int type; 20 int type;
16 int addr; 21 int addr;
17} STentry; 22} STentry;
18 23
19
20typedef struct { 24typedef struct {
21 STentry entries[MAXSYMBOLS]; 25 STentry entries[MAXSYMBOLS];
22 int maxsize; 26 int maxsize;
23 int size; 27 int size;
24} SymbolTable; 28} SymbolTable;
25 29
26
27void glo_addVar(const char name[], int type); 30void glo_addVar(const char name[], int type);
28int glo_lookup(const char name[]); 31int glo_lookup(const char name[]);
29int glo_get_addr(const char name[]); 32int glo_get_addr(const char name[]);
@@ -33,4 +36,5 @@ int loc_lookup(const char name[]);
33int loc_get_addr(const char name[]); 36int loc_get_addr(const char name[]);
34void loc_display_table(); 37void loc_display_table();
35void check_expected_type(int type_to_check, int type_expected); 38void check_expected_type(int type_to_check, int type_expected);
36#endif \ No newline at end of file 39
40#endif
diff --git a/src/tpc.lex b/src/tpc.lex
index 7a0d6b6..bdf6bb5 100644
--- a/src/tpc.lex
+++ b/src/tpc.lex
@@ -1,8 +1,8 @@
1%{ 1%{
2/** 2/**
3 * UPEM / Compilation / Projet 3 * UPEM / Compilation / Projet
4 * Pacien TRAN-GIRARD, Adam NAILI 4 * Pacien TRAN-GIRARD, Adam NAILI
5 */ 5 */
6 6
7#include "tpc.tab.h" 7#include "tpc.tab.h"
8#define INT 0 8#define INT 0
@@ -49,4 +49,3 @@ reade { return READE; }
49<COMMENT>\n { lineno++; } 49<COMMENT>\n { lineno++; }
50<COMMENT>. ; 50<COMMENT>. ;
51%% 51%%
52
diff --git a/src/tpc.y b/src/tpc.y
index 94d89fe..b0d3633 100644
--- a/src/tpc.y
+++ b/src/tpc.y
@@ -3,21 +3,16 @@
3 * UPEM / Compilation / Projet 3 * UPEM / Compilation / Projet
4 * Pacien TRAN-GIRARD, Adam NAILI 4 * Pacien TRAN-GIRARD, Adam NAILI
5 * 5 *
6 * 6 * TODO :
7 *
8 *
9 *
10 *
11 * TODO :
12 * ------ 7 * ------
13 * - Gérer les globales avec .bss (Il faut donc décaler le début du programme après l'analyse des globales pour savoir combien de place réserver.) 8 * - Gérer les globales avec .bss (Il faut donc décaler le début du programme après l'analyse des globales pour savoir combien de place réserver.)
14 * - Gestion des tableaux 9 * - Gestion des tableaux
15 * - Tableau des fonctions 10 * - Tableau des fonctions
16 * 11 *
17 */ 12 */
18 13
19#include <stdio.h> 14#include <stdio.h>
20#include "symboltable.h" 15#include "symbol_table.h"
21 16
22extern int lineno; 17extern int lineno;
23int yylex(); 18int yylex();
@@ -30,13 +25,13 @@ static int num_if = 0;
30%} 25%}
31 26
32%union { 27%union {
33 char caractere; 28 char caractere;
34 int num; 29 int num;
35 char ident[64]; 30 char ident[64];
36 int type; 31 int type;
37 char comp[3]; 32 char comp[3];
38 char addsub; 33 char addsub;
39 char divstar; 34 char divstar;
40} 35}
41%token <caractere> CARACTERE 36%token <caractere> CARACTERE
42%token <num> NUM 37%token <num> NUM
@@ -47,7 +42,7 @@ static int num_if = 0;
47%token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE 42%token OR AND CONST IF ELSE WHILE RETURN VOID PRINT READC READE
48%token <type> TYPE 43%token <type> TYPE
49 44
50%type <num> Exp EB TB FB M E T F 45%type <num> Exp EB TB FB M E T F
51%type <ident> LValue 46%type <ident> LValue
52 47
53%left ',' 48%left ','
@@ -66,17 +61,17 @@ Prog:{printf("extern printf\n");
66 printf("mov rsi, rax\n"); 61 printf("mov rsi, rax\n");
67 printf("mov rdi, format_int\n"); 62 printf("mov rdi, format_int\n");
68 printf("mov rax, 0\n"); 63 printf("mov rax, 0\n");
69 printf("call printf WRT ..plt\n"); 64 printf("call printf WRT ..plt\n");
70 printf("pop rsi\n"); 65 printf("pop rsi\n");
71 printf("pop rbp\n"); 66 printf("pop rbp\n");
72 printf("ret\n"); 67 printf("ret\n");
73 printf("\n_start:\n"); 68 printf("\n_start:\n");
74 printf("push rbp\nmov rbp, rsp\n\n"); 69 printf("push rbp\nmov rbp, rsp\n\n");
75 } 70 }
76 DeclConsts DeclVars DeclFoncts 71 DeclConsts DeclVars DeclFoncts
77 { 72 {
78 printf("mov rax,60 \n"); 73 printf("mov rax,60 \n");
79 printf("mov rdi,0 \n"); 74 printf("mov rdi,0 \n");
80 printf("syscall \n"); 75 printf("syscall \n");
81 printf(";global table\n"); 76 printf(";global table\n");
82 glo_display_table(); 77 glo_display_table();
@@ -278,8 +273,8 @@ F:
278 else{ 273 else{
279 printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n"); 274 printf(";-F\npop rdx\nxor rax,rax\nsub rax,rdx\npush rax\n");
280 } 275 }
281 } 276 }
282 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");} 277 | '!' F {$$ = $2;printf(";!F\npop rax\nxor rax,1\npush rax\n");}
283 | '(' Exp ')' {$$ = $2;} 278 | '(' Exp ')' {$$ = $2;}
284 | LValue { 279 | LValue {
285 if(status == GLOBAL) { 280 if(status == GLOBAL) {
@@ -330,4 +325,3 @@ void yyerror(char *msg){
330int main(int argc, char **argv) { 325int main(int argc, char **argv) {
331 return yyparse(); 326 return yyparse();
332} 327}
333