diff options
-rw-r--r-- | makefile | 8 | ||||
-rw-r--r-- | src/generator.c | 53 | ||||
-rw-r--r-- | src/generator.h | 18 | ||||
-rw-r--r-- | src/tpc.y | 108 |
4 files changed, 110 insertions, 77 deletions
@@ -12,6 +12,7 @@ LEX_SRC := tpc.lex | |||
12 | YACC_SRC := tpc.y | 12 | YACC_SRC := tpc.y |
13 | PDF_SRC := rapport.md | 13 | PDF_SRC := rapport.md |
14 | ST_SRC := symbol_table | 14 | ST_SRC := symbol_table |
15 | GEN_SRC := generator | ||
15 | 16 | ||
16 | # INTERMEDIATE | 17 | # INTERMEDIATE |
17 | LEX_GEN := tpc.yy | 18 | LEX_GEN := tpc.yy |
@@ -47,7 +48,7 @@ $(OUT_DIR)/$(FILE_TEST).asm: $(RES_DIR)/$(FILE_TEST).tpc $(OUT_DIR)/$(COMPIL_BIN | |||
47 | $(OUT_DIR)/$(LEX_GEN).c: $(SRC_DIR)/$(LEX_SRC) | 48 | $(OUT_DIR)/$(LEX_GEN).c: $(SRC_DIR)/$(LEX_SRC) |
48 | $(LEX) -o $@ $^ | 49 | $(LEX) -o $@ $^ |
49 | 50 | ||
50 | $(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(YACC_GEN).h: $(SRC_DIR)/$(YACC_SRC) $(SRC_DIR)/$(ST_SRC).h | 51 | $(OUT_DIR)/$(YACC_GEN).c $(OUT_DIR)/$(YACC_GEN).h: $(SRC_DIR)/$(YACC_SRC) $(SRC_DIR)/$(ST_SRC).h $(SRC_DIR)/$(GEN_SRC).h |
51 | $(YACC) --output=$(OUT_DIR)/$(YACC_GEN).c --defines=$(OUT_DIR)/$(YACC_GEN).h -v $< | 52 | $(YACC) --output=$(OUT_DIR)/$(YACC_GEN).c --defines=$(OUT_DIR)/$(YACC_GEN).h -v $< |
52 | 53 | ||
53 | $(OUT_DIR)/$(LEX_GEN).o: $(OUT_DIR)/$(LEX_GEN).c $(OUT_DIR)/$(YACC_GEN).h | 54 | $(OUT_DIR)/$(LEX_GEN).o: $(OUT_DIR)/$(LEX_GEN).c $(OUT_DIR)/$(YACC_GEN).h |
@@ -59,7 +60,10 @@ $(OUT_DIR)/$(YACC_GEN).o: $(OUT_DIR)/$(YACC_GEN).c | |||
59 | $(OUT_DIR)/$(ST_SRC).o: $(SRC_DIR)/$(ST_SRC).c | 60 | $(OUT_DIR)/$(ST_SRC).o: $(SRC_DIR)/$(ST_SRC).c |
60 | $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) | 61 | $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) |
61 | 62 | ||
62 | $(OUT_DIR)/$(COMPIL_BIN): $(OUT_DIR)/$(LEX_GEN).o $(OUT_DIR)/$(YACC_GEN).o $(OUT_DIR)/$(ST_SRC).o | 63 | $(OUT_DIR)/$(GEN_SRC).o: $(SRC_DIR)/$(GEN_SRC).c |
64 | $(CC) -o $@ -c $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) | ||
65 | |||
66 | $(OUT_DIR)/$(COMPIL_BIN): $(OUT_DIR)/$(LEX_GEN).o $(OUT_DIR)/$(YACC_GEN).o $(OUT_DIR)/$(ST_SRC).o $(OUT_DIR)/$(GEN_SRC).o | ||
63 | $(CC) -o $@ $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) | 67 | $(CC) -o $@ $^ $(IFLAGS) $(LFLAGS) $(CFLAGS) |
64 | 68 | ||
65 | $(OUT_DIR)/$(REPORT_PDF): $(DOC_DIR)/$(PDF_SRC) | 69 | $(OUT_DIR)/$(REPORT_PDF): $(DOC_DIR)/$(PDF_SRC) |
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 | |||
10 | void 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 | |||
30 | void 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 | |||
40 | void 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 | |||
9 | typedef enum scope { | ||
10 | GLOBAL, | ||
11 | LOCAL | ||
12 | } Scope; | ||
13 | |||
14 | void prologue(); | ||
15 | void const_declaration(); | ||
16 | void declaration(const char name[], int type, Scope scope); | ||
17 | |||
18 | #endif | ||
@@ -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 | ||
17 | extern int lineno; | 18 | extern int lineno; |
18 | int yylex(); | 19 | int yylex(); |
19 | void yyerror(char *); | 20 | void yyerror(char *); |
20 | #define GLOBAL 0 | 21 | static Scope scope = GLOBAL; |
21 | #define LOCAL 1 | ||
22 | static int status = GLOBAL; | ||
23 | static int num_label = 0; | 22 | static int num_label = 0; |
24 | static int num_if = 0; | 23 | static 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 | %% |
53 | Prog:{printf("extern printf\n"); | 52 | Prog: { 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 | ; |
82 | DeclConsts: | 55 | DeclConsts: |
83 | DeclConsts CONST ListConst ';' | 56 | DeclConsts CONST ListConst ';' |
84 | | | 57 | | |
85 | ; | 58 | ; |
86 | ListConst: | 59 | ListConst: |
87 | ListConst ',' IDENT '=' Litteral | 60 | ListConst ',' IDENT '=' Litteral |
88 | | IDENT '=' Litteral | 61 | | IDENT '=' Litteral |
89 | ; | 62 | ; |
90 | Litteral: | 63 | Litteral: |
91 | NombreSigne | 64 | NombreSigne |
92 | | CARACTERE | 65 | | CARACTERE |
93 | ; | 66 | ; |
94 | NombreSigne: | 67 | NombreSigne: |
95 | NUM | 68 | NUM |
96 | | ADDSUB NUM | 69 | | ADDSUB NUM |
97 | ; | 70 | ; |
98 | DeclVars: | 71 | DeclVars: |
99 | DeclVars TYPE Declarateurs ';' | 72 | DeclVars TYPE Declarateurs ';' |
100 | | | 73 | | |
101 | ; | 74 | ; |
102 | Declarateurs: | 75 | Declarateurs: |
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 | ; |
114 | Declarateur: | 79 | Declarateur: |
115 | IDENT | 80 | IDENT |
116 | | IDENT '[' NUM ']' | 81 | | IDENT '[' NUM ']' |
117 | ; | 82 | ; |
118 | DeclFoncts: | 83 | DeclFoncts: |
119 | DeclFoncts DeclFonct | 84 | DeclFoncts DeclFonct |
120 | | DeclFonct | 85 | | DeclFonct |
121 | ; | 86 | ; |
122 | DeclFonct: | 87 | DeclFonct: |
123 | EnTeteFonct {status = LOCAL;} Corps {status = GLOBAL;} | 88 | EnTeteFonct { scope = LOCAL; } |
89 | Corps { scope = GLOBAL; } | ||
124 | ; | 90 | ; |
125 | EnTeteFonct: | 91 | EnTeteFonct: |
126 | TYPE IDENT '(' Parametres ')' | 92 | TYPE IDENT '(' Parametres ')' |
127 | | VOID IDENT '(' Parametres ')' | 93 | | VOID IDENT '(' Parametres ')' |
128 | ; | 94 | ; |
129 | Parametres: | 95 | Parametres: |
130 | VOID | 96 | VOID |
131 | | ListTypVar | 97 | | ListTypVar |
132 | ; | 98 | ; |
133 | ListTypVar: | 99 | ListTypVar: |
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 | ; |
145 | Corps: | 103 | Corps: |
146 | '{' DeclConsts DeclVars SuiteInstr '}' | 104 | '{' DeclConsts DeclVars SuiteInstr '}' |