-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.hpp
386 lines (327 loc) · 12.6 KB
/
ast.hpp
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
#ifndef __ast_h
#define __ast_h
#include <llvm/ADT/STLExtras.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Verifier.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <iostream>
#include <string>
#include <vector>
#include "global.hpp"
#include "scan.hpp"
class Main_block;
class FunctionAST;
class BasicAST {
public:
virtual void print()=0;
virtual llvm::Value* codegen(Main_block*)=0;
};
class FloatAST : public BasicAST {
float value;
public:
void print() { Debug("(print)Float ", std::to_string(this->value).c_str()); }
FloatAST(float v) : value(v) {}
llvm::Value* codegen(Main_block*);
};
class IntegerAST : public BasicAST {
int value;
public:
void print() { Debug("(print) Integer ", std::to_string(this->value).c_str()); }
IntegerAST(int v) : value(v) {}
llvm::Value* codegen(Main_block* mblock);
};
class StringAST : public BasicAST {
std::string value;
public:
std::string get_value() { return value; }
void print() { Debug("(print) String ",this->value.c_str()); }
StringAST(std::string v) : value(v) {}
llvm::Value* codegen(Main_block* mblock);
};
class CallFuncAST : public BasicAST {
std::string callee;
std::string message;
std::string string_message;
std::string g_var;
std::string var;
std::vector<std::string> args;
std::vector<BasicAST*> real_args;
std::vector<FunctionAST*> functions;
int array_pos;
bool isarray;
bool has_string;
int external;
public:
void print() { Debug("(print) CallFunc", this->callee.c_str()); }
void add_arg(std::string arg) { args.push_back(arg); }
void add_rarg(BasicAST* arg) { real_args.push_back(arg); }
int get_external() { return external; }
void set_external() { external = 1; }
void set_message(int);
void set_name(std::string name) { this->callee = name; }
void set_var(std::string var) { this->var = var; }
void set_array(bool val) { isarray = val; }
void set_array_pos(int n) { array_pos = n; }
void set_string_message(std::string s) { string_message = s; has_string = true; }
bool get_array() { return isarray; }
bool get_array_pos() { return array_pos; }
std::string get_name() { return callee; }
std::string get_message() { return message; }
std::string get_gvar() { return g_var; }
std::string get_var() { return var; }
std::vector<std::string> get_args() { return this->args; }
FunctionAST* get_func(Main_block*);
std::string get_string_message() { return string_message; }
CallFuncAST() { external = 0; set_array(false); has_string = false; }
CallFuncAST(std::string name) : callee(name) { external = 0; set_array(false); has_string = false; }
llvm::Value* codegen(Main_block*);
};
class VariableAST : public BasicAST {
int var_type;
bool is_global;
std::string name;
bool isarray;
int array_size;
public:
bool get_global() { return is_global; }
int get_array() { return isarray; }
int get_array_pos() { return array_size; }
void set_global() { is_global = true; }
void set_array(bool v) { isarray = v; }
void set_array_pos(int n) { array_size = n; }
void print() { Debug("(print) Variable", get_name().c_str()); }
std::string get_name() { return this->name; }
VariableAST(std::string name, int var_type) : name(name), var_type(var_type), isarray(false) {}
llvm::Value* codegen(Main_block*);
};
class AssignAST : public BasicAST {
std::string var;
std::vector<BasicAST*> rhs;
bool isarray;
int array_pos;
bool isstring;
StringAST* var_string;
public:
StringAST* get_string() { return var_string; }
bool get_array() { return isarray; }
int get_array_pos() { return array_pos; }
bool is_string() { return isstring; }
void set_array(bool val) { isarray = val; }
void set_string(bool val) { isstring = val; }
void print() { Debug("(print)Assigning var", this->get_name().c_str()); }
llvm::Value* string_var(Main_block*, std::string);
AssignAST(std::string var_name, std::vector<BasicAST*> rhs) : var(var_name), rhs(rhs) { set_array(false); set_string(false); }
AssignAST(std::string var_name, int pos, std::vector<BasicAST*> rhs) : var(var_name), rhs(rhs), array_pos(pos) { set_array(true); set_string(false); }
AssignAST(std::string var_name, StringAST* s) : var(var_name), var_string(s) { set_string(true); }
std::string get_name() { return this->var; }
llvm::Value* codegen(Main_block*);
};
class CallVarAST : public BasicAST {
std::string var;
bool isarray;
int array_pos;
public:
std::string get_var() { return this->var; }
bool get_array() { return isarray; }
int get_array_pos() { return array_pos; }
void set_array(bool val) { isarray = val; }
void set_array_pos(int val) { array_pos = val; }
void print() { Debug("(print)Calling var", this->var.c_str()); }
CallVarAST(std::string var_name) : var(var_name) { set_array(false); }
llvm::Value* codegen(Main_block*);
};
class BinopAST : public BasicAST {
char op;
BasicAST* op1;
BasicAST* op2;
int type;
std::string var;
public:
void set_type(int t, std::string v) { type = t; var = v; }
int get_type() { return type; }
std::string get_var() { return var; }
void print() { Debug("(print) bin op"); }
BinopAST(char op, BasicAST* op1, BasicAST* op2) : op(op),op1(op1),op2(op2) { set_type(0, ""); }
llvm::Value* codegen(Main_block*);
};
#define COND_EQUAL 0
#define COND_NOT_EQUAL 1
#define COND_LESS 2
#define COND_LESS_EQUAL 3
#define COND_GREATER 4
#define COND_GREATER_EQUAL 5
class FlowBlock : public BasicAST {
int type;
public:
void set_type(int t) { this->type = t; }
int get_type() { return this->type; }
virtual void add_statement(BasicAST*)=0;
virtual void change_state()=0; //used in the control flow to go to the else state
};
class CondAST : public FlowBlock {
llvm::Function* func; //functio this condition belongs to
int state; //shows if cond is true or in false state
bool in_for;
llvm::BasicBlock* cond_true; //block when the cond is true
llvm::BasicBlock* cond_false; //block when the cond is false
std::vector<BasicAST*> true_statements; //statements of the true cond block
std::vector<BasicAST*> false_statements; //statements of the false cond block
std::vector<BasicAST*> lhs; //condition lhs
std::vector<int> operand; //operand of condition
std::vector<BasicAST*> rhs; //condition rhs
public:
std::vector<BasicAST*> get_true_statement() { return this->true_statements; }
std::vector<BasicAST*> get_false_statement() { return this->false_statements; }
void set_state(int s) { this->state = s; }
void set_in_for(bool s) { this->in_for = s; }
void print() { Debug("(print) if block"); }
void add_condition(BasicAST* lhs, int operand, BasicAST* rhs);
void add_statement(BasicAST*);
void change_state() { state++; }
int get_state() { return this->state; }
bool get_in_for() { return this->in_for; }
CondAST() { set_state(0); set_in_for(false); };
llvm::Value* codegen(Main_block*);
};
class ForAST : public FlowBlock {
std::string incr_var;
CondAST* cond;
int state;
public:
void print() { Debug("(print) for block"); }
void add_cond(CondAST* c) { this->cond = c;}
void add_statement(BasicAST* b) { cond->add_statement(b); };
void add_incr_var(std::string v) { incr_var = v; }
void change_state() { state++; }
int get_state() { return this->state; }
std::string get_incr_var() { return incr_var; }
ForAST() {};
llvm::Value* codegen(Main_block*);
};
class BranchAST : public BasicAST {
llvm::BasicBlock* branch;
public:
BranchAST(llvm::BasicBlock* bb) { branch = bb; Debug("Branch instr"); }
void print() { Debug("(print) Branch instr"); }
llvm::Value* codegen(Main_block *);
};
class EndFunctionAST : public BasicAST {
bool return_statement;
public:
EndFunctionAST() { return_statement = false; Debug("End function"); }
EndFunctionAST(bool val) { return_statement = val; Debug("End function using return"); }
bool get_return() { return return_statement; }
void print() { Debug("(print) end function"); }
llvm::Value* codegen(Main_block *);
};
class SpecialBlock {
std::vector<FlowBlock*> blocks;
FlowBlock* get_block();
public:
void add_statement(BasicAST*);
void add_block(FlowBlock*, int);
void change_state();
FlowBlock* pop_block();
};
class FunctionAST : public BasicAST {
llvm::FunctionType* llvm_func_type;
llvm::Function* func;
int state; //show if there is an if or a loop (1 == if, 2 == loop)
std::string name; //head
int func_type; //head
std::map<std::string, llvm::Type*> args; //head
std::vector<int> arg_mask;
std::vector<BasicAST*> list_statement; //body
SpecialBlock* sblocks; //body
std::map<std::string, llvm::Value*> local_var; //body
llvm::BasicBlock* bb;
public:
void print() { Debug("(print) function", get_name().c_str()); }
FunctionAST(std::string name) : name(name) { this->state = 0; this->sblocks = new SpecialBlock; }
void add_arg(std::string, llvm::Type*); //add argument to this function
void add_var(VariableAST* var); //add var to local variables of this function
void add_statement(BasicAST*);
void add_special(FlowBlock*, int);
void add_return(Main_block*);
void add_mask(int m) { this->arg_mask.push_back(m); }
void pop_special();
void inc_state() { this->state = this->state + 1; }
void dec_state() { this->state = this->state - 1; }
int change_var_value(std::string, llvm::Value*);
void change_state() { sblocks->change_state(); };
void change_bb(llvm::BasicBlock* b) { bb = b; }
void set_func(llvm::Function* f) { this->func = f; }
std::string get_name() { return this->name; }
std::vector<llvm::Type *> get_args_type();
llvm::Value* get_var(std::string var) { return this->local_var[var]; }
llvm::Function* get_func() { return this->func; }
int get_state() { return this->state; }
std::vector<int> get_mask() { return this->arg_mask; }
llvm::Value* codegen(Main_block *);
void show_dump() { func->dump(); }
};
struct structure {
std::string var_name;
llvm::Type* type;
int state;
};
class Structure {
std::vector<struct structure> structures;
int state;
public:
Structure() { state = 0; }
void print() { Debug("(print) structures"); }
void add_structure(std::string, llvm::Type*);
std::vector<std::string> get_vars(Main_block*,std::string);
void inc_state() { state++; }
void codegen(Main_block* );
};
class Main_block {
std::string name;
std::unique_ptr<llvm::Module> mod;
std::vector<llvm::BasicBlock*> block;
std::map<std::string, llvm::Value*> local_var;
Structure* s;
llvm::Function *main;
std::vector<FunctionAST*> functions;
int function_position;
public:
Main_block();
void set_name(std::string name) { this->name = name; }
void codegen();
void show_dump() { mod->dump(); }
void add_var(VariableAST* var); //add variable to last function inserted
void add_gvar(VariableAST* var);
void add_array();
void add_func(FunctionAST* func) { functions.push_back(func); }; //add new function to main block
void add_arg(std::string, llvm::Type*);
void add_block(llvm::BasicBlock *bb) { block.push_back(bb); }
void add_statement(BasicAST*); //add a statement to the current block
void add_special(FlowBlock*, int); //add a special block
void add_structure(std::string v,llvm::Type* t) { s->add_structure(v,t); }
void add_mask(int);
void pop_special(); //pop the last special block in the current function
void pop_block() { block.pop_back(); }
void pop_function();
void inc_structure() { s->inc_state(); }
void inc_function() { function_position++; }
void change_state(); //used in the control flow
llvm::BasicBlock* get_block() { return this->block.back(); }
FunctionAST* get_func();
FunctionAST* get_func(int);
FunctionAST* get_main_func() { return this->functions.front(); }
std::vector<FunctionAST*> get_functions() { return this->functions; }
llvm::Module* get_module() { return mod.get(); }
std::vector<std::string> get_struct_vars(std::string func_name) { return s->get_vars(this,func_name); }
void reset();
llvm::GenericValue runCode();
};
///TODO
/*
Add function semi done to Main_block in the cosntructor
Always pass the Main_block to the codegen of the other classes
Classes can get the current BasicBlock, module, etc...
*/
#endif // __ast_h