-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.h
318 lines (274 loc) · 6.98 KB
/
driver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
/* Authored by Charlie Keaney */
/* driver.h - Responsible for driving the kcc
compiler, taking in arguments. */
#ifndef DRIVER_H
#define DRIVER_H 1
#include "lexema-pool.h"
#include "preprocessor.h"
#include "lexer.h"
#include "parser.h"
#include "codegen.h"
#include "semantic-annotator.h"
#include "annotated-ast-node.h"
#include "kcc-tester.h"
#define NUM_INPUT_CHARACTERS 1028
#define NUM_PREPROCESSING_TOKENS 1028
#define NUM_ASSEMBLY_TOKENS 1028
#define DEBUG_DISPLAY_LEXEMA_POOL 0
/*****************************************************//**
* Declarations *
/********************************************************/
enum class KccExitCode {
SUCCESS,
FAIL
};
enum class CompilerFlag {
DISPLAY_PPTOKENS,
DISPLAY_TOKENS,
DISPLAY_TREE,
DISPLAY_ANNO_TREE,
DISPLAY_X86,
DISPLAY_NOTHING
};
struct CompilerFlags {
bool help : 1;
bool display_pptokens : 1;
bool display_tokens : 1;
bool display_tree : 1;
bool display_annotree : 1;
bool display_x86 : 1;
bool display_nothing : 1;
};
struct CompilerArgs {
CompilerFlags flags;
const char* dest;
};
const char* help_msg
= "Keaney's C Compiler (c) 2022\n"
"Designed by Charlie Keaney of Queen's "
"University Belfast during his MEng "
"Computer Science Program.\n"
"This compiler is designed for the "
"output of x86-64 Bit.\n"
"Run it with the command :\n"
"\tkcc -<options> <args>\n"
"\n"
"\t\t\tOptions\n"
"\t-h : Display help menu.\n"
"\t-p : Display produced preprocessing tokens.\n"
"\t-l : Display produced lexer tokens.\n"
"\t-t : Display produced abstract syntax tree.\n"
"\t-s : Display produced abstract syntax tree"
" with semantic annotations.\n"
"\t-p : Display produced x86 code.\n"
"\t-m : Mute (display nothing) (default).\n";
struct CompilationEnvironment {
PreprocessingToken* ppts;
LexemaPool* lexema;
Token* tokens;
AstNode* ast_root;
AnnotatedAstNode* anno_ast_root;
x86_Asm_IR* instrs;
AlertList bkl;
};
static inline
void free_compilation_environment(
CompilationEnvironment& environment);
static inline
void print_help();
static inline
KccExitCode drive(
const char* const& filename,
const char* const& input,
const CompilerFlags const& flags);
static inline
void interpret_args(
const char** const& argv,
int const& argc);
/*****************************************************//**
* Definitions *
/********************************************************/
static inline
void free_compilation_environment(
CompilationEnvironment& environment)
{
if (environment.ppts) {
delete[] environment.ppts;
environment.ppts = NULL;
}
if (environment.lexema) {
delete environment.lexema;
environment.lexema = NULL;
}
if (environment.tokens) {
delete[] environment.tokens;
environment.tokens = NULL;
}
if (environment.ast_root) {
delete environment.ast_root;
environment.ast_root = NULL;
}
if (environment.anno_ast_root) {
delete environment.anno_ast_root;
environment.anno_ast_root = NULL;
}
if (environment.instrs) {
delete[] environment.instrs;
environment.instrs = NULL;
}
}
static inline
void print_help() {
cout << help_msg;
}
static inline
KccExitCode drive(
const char* const& filename,
const char* const& input,
const CompilerFlags const& flags)
{
if (flags.help) {
print_help();
}
KccExitCode exitcode
= KccExitCode::FAIL;
CompilationEnvironment environment
= CompilationEnvironment({ 0 });
environment.bkl
= AlertList();
/* Preprocessing. */
environment.ppts
= new PreprocessingToken[NUM_PREPROCESSING_TOKENS] { };
environment.lexema
= new LexemaPool();
PreprocessingToken* ppts_ptr
= environment.ppts;
const char* input_ptr = input;
if (preprocess(
input_ptr,
*environment.lexema,
filename,
environment.bkl,
ppts_ptr)
== PreprocessorExitCode::SUCCESS) {
if (DEBUG_DISPLAY_LEXEMA_POOL) {
print_lexema_pool(*(environment.lexema));
}
int count_pptokens
= ppts_ptr - environment.ppts;
if (flags.display_pptokens) {
print_preprocessing_token_table(
environment.ppts,
count_pptokens);
}
/* Lexing. */
environment.tokens
= new Token[count_pptokens + 1] { };
Token* tokens_lex_ptr
= environment.tokens;
if (lex(
environment.ppts,
tokens_lex_ptr,
count_pptokens,
environment.bkl)
== LexerExitCode::SUCCESS) {
int count_tokens
= tokens_lex_ptr - environment.tokens;
if (flags.display_tokens) {
for (Token* t = environment.tokens;
t < environment.tokens + count_tokens;
t++) {
t->print();
cout << endl;
}
}
/* Parsing. */
const Token* tokens_parse_ptr
= environment.tokens;
if (parse(
tokens_parse_ptr,
environment.ast_root)
== ParserExitCode::SUCCESS) {
if (flags.display_tree) {
environment.ast_root->print();
}
/* Semantic Analysis. */
if (annotate(
environment.ast_root,
environment.anno_ast_root)
== SemanticAnnotatorExitCode::SUCCESS) {
if (flags.display_annotree) {
environment.anno_ast_root->print();
}
/* Code Generation. */
environment.instrs
= new x86_Asm_IR[NUM_ASSEMBLY_TOKENS]{ };
x86_Asm_IR* instrs_ptr
= environment.instrs;
int num_labels = 0;
gen(num_labels,
environment.anno_ast_root,
instrs_ptr);
if (flags.display_x86) {
for (x86_Asm_IR* i = environment.instrs;
i != instrs_ptr;
i++) {
i->print();
}
}
}
}
}
}
environment.bkl.print(input);
/* Memory Management. */
free_compilation_environment(environment);
/* Returning*/
return exitcode;
}
/*
kcc-directive:
kcc <flags> <filepath>
flags:
<flags> <flag>
| <flag>
flag:
-<flag-mnemonics>
flag-mnemonic:
h
| p
| l
| t
| s
| x
| m
-<arg>
*/
static inline
void interpret_args(
const char** const& argv,
int const& argc)
{
CompilerArgs args = CompilerArgs({ 0 });
CompilerFlags flags = CompilerFlags({ 0 });
/* Fill args.flags */
const char* chr = argv[1];
if (*chr == '-') {
while (*chr) {
switch (*chr++) {
case 'h': flags.help = true; break;
case 'p': flags.display_pptokens = true; break;
case 'l': flags.display_tokens = true; break;
case 't': flags.display_tree = true; break;
case 's': flags.display_annotree = true; break;
case 'x': flags.display_x86 = true; break;
case 'm': flags.display_nothing = true; break;
default: break;
}
}
}
args.flags = flags;
/* Fill args.dest */
args.dest = argv[2];
}
#endif