-
Notifications
You must be signed in to change notification settings - Fork 3
/
AST_node.h
366 lines (333 loc) · 10.6 KB
/
AST_node.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
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
#ifndef AST_NODE_H
#define AST_NODE_H
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include <stack>
#include <set>
#include <string>
#include <algorithm>
#include <typeinfo>
#include <llvm/IR/Value.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/CallingConv.h>
#include <llvm/IR/IRPrintingPasses.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/PassManager.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/Constants.h>
#include <llvm/IR/GlobalVariable.h>
#include <llvm/IR/InlineAsm.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/MathExtras.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/ExecutionEngine/GenericValue.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/ADT/SmallVector.h>
#include <llvm/Pass.h>
using namespace llvm;
using namespace std;
enum E_TYPE {
E_UNKNOWN = -1,
E_VOID = 0,
E_CHAR,
E_INT,
E_DOUBLE,
E_PTR,
E_FUNC,
};
class GenBlock {
public:
BasicBlock *block;
map<string, Value*> locals;
};
class GenBlockP {
public:
GenBlockP();
set<string> locals;
set<string> declared_globals;
bool isFunction;
};
class BlockExprNode;
class GenContext {
public:
stack<GenBlock *> blocks;
map<string, Value*> globalVariables;
Function *mainFunction;
Function *tempFunction;
string code;
bool funcDeclaring;
Module *module;
public:
GenContext() {
funcDeclaring = false;
module = new Module("main", getGlobalContext());
}
void CodeGen(BlockExprNode& root);
void OutputCode(ostream &out);
GenericValue run();
map<string, Value*>& locals() { return blocks.top()->locals; }
map<string, Value*>& globals() { return globalVariables; }
BasicBlock *context() { return blocks.top()->block; }
Function* currentFunction() { return tempFunction; }
void currentFunction(Function *function) { tempFunction = function; }
void ret(BasicBlock* block) { blocks.top()->block = block; }
void push(BasicBlock *block, bool copy_locals = true) {
GenBlock* new_block = new GenBlock();
new_block->block = block;
if(copy_locals) {
map<string, Value*> prev_locals = map<string, Value*>(blocks.top()->locals);
new_block->locals = prev_locals;
}
blocks.push(new_block);
}
void pop() {
GenBlock *top = blocks.top();
blocks.pop();
delete top;
}
};
class GenContextP {
public:
stack<GenBlockP *> blocks;
set<string> globalVariables;
stringstream code;
stringstream code_buf;
int indent_num;
bool funcDeclaring;
public:
GenContextP();
~GenContextP();
void CodeGen(BlockExprNode &root);
void OutputCode(ostream &out);
void clearBuf();
void applyBuf();
set<string>& locals();
set<string>& declared_globals();
set<string>& globals();
void declare_global(string name);
GenBlockP *currentBlock();
void PushBlock(bool copy_locals = true, bool isFunction = false);
void popBlock();
void indent(bool use_buf = true);
void nextLine(bool use_buf = true);
};
class ASTNode {
public:
ASTNode() {};
virtual ~ASTNode(){};
virtual Value *CodeGen(GenContext&) = 0;
virtual void CodeGenP(GenContextP&) = 0;
};
class ExprNode: public ASTNode {
public:
E_TYPE _type;
};
class StatementNode: public ASTNode {};
class IntExprNode: public ExprNode {
public:
long long val;
public:
IntExprNode(long long val): val(val) {
_type = E_INT;
}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class CharExprNode: public ExprNode {
public:
char val;
public:
CharExprNode(char val): val(val) {
_type = E_CHAR;
}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class DoubleExprNode: public ExprNode {
double val;
public:
DoubleExprNode(double val): val(val) {
_type = E_DOUBLE;
}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class VariableExprNode: public ExprNode {
public:
string name;
public:
VariableExprNode(const string &name, E_TYPE type = E_UNKNOWN): name(name) {
_type = type;
}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class OperatorExprNode: public ExprNode {
public:
int op;
ExprNode *left, *right;
public:
OperatorExprNode(ExprNode *left, int op, ExprNode *right): left(left), right(right), op(op) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class BlockExprNode: public ExprNode {
public:
vector<StatementNode*> *statements;
public:
BlockExprNode(): statements(new vector<StatementNode*>()) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class AssignExprNode: public ExprNode {
public:
VariableExprNode *left;
ExprNode *right;
public:
AssignExprNode(VariableExprNode *left, ExprNode *right): left(left), right(right) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class FuncExprNode: public ExprNode {
public:
VariableExprNode *functor;
vector<ExprNode*> *args;
public:
FuncExprNode(VariableExprNode *functor): functor(functor), args(new vector<ExprNode*>()) {}
FuncExprNode(VariableExprNode *functor, vector<ExprNode*> *args): functor(functor), args(args) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class CastExprNode: public ExprNode {
public:
VariableExprNode *type;
ExprNode *expr;
public:
CastExprNode(VariableExprNode *type, ExprNode *expr): type(type), expr(expr) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class ExprStatementNode : public StatementNode {
public:
ExprNode *expr;
public:
ExprStatementNode(ExprNode *expr): expr(expr) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class ReturnStatementNode : public StatementNode {
public:
ExprNode *expr;
public:
ReturnStatementNode(ExprNode *expr): expr(expr) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class VarDecStatementNode : public StatementNode {
public:
VariableExprNode *type;
VariableExprNode *name;
ExprNode *expr;
public:
VarDecStatementNode(VariableExprNode *type, VariableExprNode *name): type(type), name(name), expr(NULL) {}
VarDecStatementNode(VariableExprNode *type, VariableExprNode *name, ExprNode *expr): type(type), name(name), expr(expr) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class ArrayDecStatementNode : public StatementNode {
public:
VariableExprNode *type;
VariableExprNode *name;
vector<ExprNode*> *init;
long long size;
bool isString;
public:
ArrayDecStatementNode(VariableExprNode *type, VariableExprNode *name, long long size): type(type), name(name), init(new vector<ExprNode*>()), size(size), isString(false) {}
ArrayDecStatementNode(VariableExprNode *type, VariableExprNode *name, vector<ExprNode*> *init): type(type), name(name), init(init), size(init->size()), isString(false) {}
ArrayDecStatementNode(VariableExprNode *type, VariableExprNode *name, const string &str): type(type), name(name), init(new vector<ExprNode*>()), isString(true) {
for(auto it = str.begin(); it != str.end(); it++)
init->push_back((ExprNode*)(new CharExprNode(*it)));
init->push_back((ExprNode*)(new CharExprNode(0)));
size = init->size() + 1;
}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class IndexExprNode : public ExprNode {
public:
VariableExprNode *name;
ExprNode *expr;
ExprNode *assign;
public:
IndexExprNode(VariableExprNode *name, ExprNode *expr): name(name), expr(expr), assign(NULL) {}
IndexExprNode(VariableExprNode *name, ExprNode *expr, ExprNode *assign): name(name), expr(expr), assign(assign) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class FuncDecStatementNode : public StatementNode {
public:
VariableExprNode *type;
VariableExprNode *name;
vector<VarDecStatementNode*> *args;
BlockExprNode *block;
public:
FuncDecStatementNode(VariableExprNode *type, VariableExprNode *name, vector<VarDecStatementNode*> *args, BlockExprNode *block): type(type), name(name), args(args), block(block) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class ExternFuncDecStatementNode : public StatementNode {
public:
VariableExprNode *type;
VariableExprNode *name;
vector<VarDecStatementNode*> *args;
public:
ExternFuncDecStatementNode(VariableExprNode *type, VariableExprNode *name, vector<VarDecStatementNode*> *_args): type(type), name(name), args(_args) {
vector<VarDecStatementNode*>::const_iterator it;
}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class IfStatementNode : public StatementNode {
public:
ExprNode *condExpr;
BlockExprNode *trueBlock;
BlockExprNode *falseBlock;
public:
IfStatementNode(ExprNode *condExpr, BlockExprNode *trueBlock): condExpr(condExpr), trueBlock(trueBlock), falseBlock(new BlockExprNode()) {}
IfStatementNode(ExprNode *condExpr, BlockExprNode *trueBlock, BlockExprNode *falseBlock): condExpr(condExpr), trueBlock(trueBlock), falseBlock(falseBlock) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class ForStatementNode : public StatementNode {
public:
ExprNode *initExpr;
ExprNode *condExpr;
ExprNode *loopExpr;
BlockExprNode *block;
public:
ForStatementNode(ExprNode *initExpr, ExprNode *condExpr, ExprNode *loopExpr, BlockExprNode *block): initExpr(initExpr), condExpr(condExpr), loopExpr(loopExpr), block(block) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
class WhileStatementNode : public StatementNode {
public:
ExprNode *whileExpr;
BlockExprNode *block;
public:
WhileStatementNode(ExprNode *whileExpr, BlockExprNode *block): whileExpr(whileExpr), block(block) {}
virtual Value *CodeGen(GenContext&);
virtual void CodeGenP(GenContextP&);
};
#endif