-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode.h
executable file
·480 lines (415 loc) · 12 KB
/
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#ifndef Node_H
#define Node_H
#include "SyntaxTree.h"
#include <vector>
#include <map>
#include <llvm/IR/Value.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/LLVMContext.h>
#include <llvm/IR/Type.h>
#include <llvm/IR/DerivedTypes.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/BasicBlock.h>
#include <llvm/IR/Argument.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/GlobalVariable.h>
#include <llvm/IR/GlobalValue.h>
class constValue;
class Node;
class ExprAST;
class TypeAST;
class NumberExprAST;
class RealExprAST;
class BoolExprAST;
class CharExprAST;
class StringExprAST;
class BasicTypeAST;
class ConstAST;
class VariableDeclAST;
class VariableExprAST;
class UnaryExprAST;
class BinaryExprAST;
class CallExprAST;
class FunctionAST;
class CodeGenContext;
using namespace std;
using namespace llvm;
extern LLVMContext &context;
extern IRBuilder<> builder;
extern Module module;
extern Function *startFunc;
extern string errorMsg;
extern FunctionAST *program;
extern Value* createCast(Value *value,Type *type);
extern Constant* getInitial(Type *type);
extern void throwError(Node *node);
extern void throwWarning(Node *node,string msg);
extern string getOperatorName(int op);
extern string getTypeName(Type *type);
//二元表达式集合
typedef enum {
plusKind, // plus
minusKind, // minus
orKind, // or
mulKind, // muliply
divKind, // div
modKind, // mod
andKind, // and
geKind, // greater equal
gtKind, // greater than
leKind, // less equal
ltKind, // less than
eqKind, // equal
ueqKind, // unequal
assignmentKind // assignment
} BinaryOpKind;
//一元表达式集合
typedef enum {
notKind, // not
negKind // negative
} UnaryOpKind;
//const值类型
class constValue {
public:
int i;
double d;
char c;
string s;
bool b;
constValue() {}
constValue(const constValue &f):i(f.i), d(f.d), c(f.c), s(f.s), b(f.b){}
};
//TypeAST中用来标记是哪种类型
typedef enum {
basic_kind,
array_kind,
self_kind
} TypeName;
//SelfdefineTypeAST中用来标记类型
typedef enum {
record,
changeName
} SelfType;
//BasicTypeAST中的物种基础类型
typedef enum {
Integer,
String,
Real,
Char,
Bool
} BasicType;
typedef enum {
NumberExpr,
RealExpr,
BoolExpr,
StringExpr,
CharExpr,
Variable,
ArrayVariable,
RecordVariable,
UnaryExpr,
BinaryExpr,
CallExpr,
IfExpr,
ForExpr,
WhileExpr,
RepeatExpr
} ExprType;
class Node {
public:
Node(){}
virtual ~Node(){}
virtual Value* Codegen(CodeGenContext& astcontext){};
virtual void print(int i) {};
};
//所有表达式类的基类
class ExprAST : public Node{
public:
ExprType expr_type;
ExprAST(){}
virtual Value* Codegen(CodeGenContext& astcontext){};
};
//所有类型类的基类
class TypeAST : public Node{
public:
TypeAST(){}
virtual void print(int i){};
};
//整数常数表达式,如“1”
class NumberExprAST : public ExprAST {
public:
int val;
NumberExprAST(int value):val(value){}
NumberExprAST(const NumberExprAST &f):val(f.val){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//实数常数表达式,如“1.0”
class RealExprAST : public ExprAST {
public:
double val;
RealExprAST(double value):val(value){}
RealExprAST(const RealExprAST &f):val(f.val){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//布尔常数表达式,如“true”
class BoolExprAST : public ExprAST {
public:
bool val;
BoolExprAST(bool value):val(value){}
BoolExprAST(const BoolExprAST &f):val(f.val){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//字符串常数表达式,如“wasd”
class StringExprAST : public ExprAST {
public:
string val;
StringExprAST(const string value):val(value){}
StringExprAST(const StringExprAST &f):val(f.val){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//字符常数表达式,如“a”
class CharExprAST : public ExprAST {
public:
char val;
CharExprAST(char value):val(value){}
CharExprAST(const CharExprAST &f):val(f.val){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//基础类型如string、integer等
class BasicTypeAST : public TypeAST {
public:
BasicType type;
BasicTypeAST(BasicType type):type(type){}
BasicTypeAST(const BasicTypeAST &f):type(f.type){}
void print(int n) override;
};
//const类
class ConstAST : public Node{
public:
string variableName;
BasicTypeAST* type;
constValue value;
ConstAST(string name, BasicTypeAST* ty, constValue val)
:variableName(name), type(ty), value(val){}
ConstAST(const ConstAST &f): variableName(f.variableName), type(f.type), value(f.value){}
~ConstAST(){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
// ConstAST* ErrorC(const char *str){fprintf(stderr, "%s\n", str); return 0;}
};
//数组类型
class ArrayTypeAST : public TypeAST {
public:
TypeAST *type;
ConstAST *startIndex, *endIndex;
ArrayTypeAST(TypeAST *ty, ConstAST *s, ConstAST *e):type(ty), startIndex(s), endIndex(e){}
};
//自定义类型
class SelfdefineTypeAST : public TypeAST {
public:
//SelfType type;
SelfdefineTypeAST(){}
//virtual ~SelfDefineAST(){}
//virtual Value* Codegen(CodeGenContext& context) override;
//SelfdefineTypeAST* ErrorS(const char *str){Error(str); return 0;}
};
//变量声明
class VariableDeclAST : public Node{
public:
BasicTypeAST *type;
vector<string> variableName;
int isGlobal;
VariableDeclAST(BasicTypeAST *t, vector<string> v):type(t), variableName(v){}
VariableDeclAST(const VariableDeclAST &f):type(f.type), variableName(f.variableName){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//record类型
class RecordTypeAST : public SelfdefineTypeAST {
public:
vector<VariableDeclAST*> declList;
RecordTypeAST(vector<VariableDeclAST*> v):declList(v){}
Value* Codegen(CodeGenContext& context) override;
};
//type A=integer;这种自定义类型
class ChangeNameTypeAST : public SelfdefineTypeAST {
public:
string newName;
TypeAST* originalType;
ChangeNameTypeAST(string name, TypeAST* old):newName(name), originalType(old){}
Value* Codegen(CodeGenContext& context) override;
};
//普通变量表达式,如“i”
class VariableExprAST : public ExprAST {
public:
string name;
VariableExprAST(const string variableName):name(variableName){}
VariableExprAST(const VariableExprAST &f):name(f.name){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//数组变量表达式,如"a[i]"
class ArrayVariableExprAST : public ExprAST {
public:
string name;
ExprAST *index;
ArrayVariableExprAST(const string variableName, ExprAST *i)
:name(variableName), index(i){}
Value* Codegen(CodeGenContext& context) override;
void print(int n) override;
};
//记录变量表达式,如"a.p"
class RecordVariableExprAST : public ExprAST {
public:
string name;
ExprAST *sub;
RecordVariableExprAST(const string variableName, ExprAST *s)
:name(variableName), sub(s){}
Value* Codegen(CodeGenContext& context) override;
void print(int n) override;
};
//一元表达式如"!"、“-”
class UnaryExprAST : public ExprAST {
public:
UnaryOpKind op;
ExprAST* expr;
UnaryExprAST(UnaryOpKind o, ExprAST *e):op(o), expr(e){}
UnaryExprAST(const UnaryExprAST &f):op(f.op), expr(f.expr){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//二元表达式如“+”、“-”、“*”、“/”、“=”等
class BinaryExprAST : public ExprAST {
public:
BinaryOpKind op;
ExprAST *LExpr, *RExpr;
BinaryExprAST(BinaryOpKind binaryOp, ExprAST *left, ExprAST *right)
:op(binaryOp), LExpr(left), RExpr(right){}
BinaryExprAST(const BinaryExprAST &f)
:op(f.op), LExpr(f.LExpr), RExpr(f.RExpr){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//调用函数表达式,包含函数名称和参数
class CallFunctionExprAST : public ExprAST {
public:
string callee;
std::vector<ExprAST*> args;
bool isSystemCall;
CallFunctionExprAST(const string name, std::vector<ExprAST*> vec, bool is)
:callee(name), args(vec), isSystemCall(is){}
CallFunctionExprAST(const CallFunctionExprAST &f): callee(f.callee), args(f.args), isSystemCall(f.isSystemCall){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//调用procedure, 包含procedure名称和参数
class CallProcedureExprAST : public ExprAST {
public:
string callee;
std::vector<ExprAST*> args;
bool isSystemCall;
CallProcedureExprAST(const string name, std::vector<ExprAST*> vec, bool is)
:callee(name), args(vec), isSystemCall(is){}
CallProcedureExprAST(const CallProcedureExprAST &f): callee(f.callee), args(f.args), isSystemCall(f.isSystemCall){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
//if else表达式
class IfExprAST : public ExprAST {
public:
ExprAST *ifCond;
std::vector<ExprAST*> thenComponent, elseComponent;
IfExprAST(ExprAST *cond, std::vector<ExprAST*> thenCo, std::vector<ExprAST*> elseCo)
:ifCond(cond), thenComponent(thenCo), elseComponent(elseCo){}
Value* Codegen(CodeGenContext& context) override;
void print(int n) override;
};
//for表达式
class ForExprAST : public ExprAST {
public:
string varName;
ExprAST *start, *end;
std::vector<ExprAST*> body;
bool increaseDirection;
ForExprAST(const string name, ExprAST *st, ExprAST *en, std::vector<ExprAST*> bo, bool inc)
:varName(name), start(st), end(en), body(bo), increaseDirection(inc){}
Value* Codegen(CodeGenContext& astcontext) override;
void print(int n) override;
};
//while表达式
class WhileExprAST : public ExprAST {
public:
ExprAST *whileCond;
std::vector<ExprAST*> body;
WhileExprAST(ExprAST *cond, std::vector<ExprAST*> bo)
:whileCond(cond), body(bo){}
Value* Codegen(CodeGenContext& context) override;
void print(int n) override;
};
//repeat until表达式
class RepeatExprAST : public ExprAST {
public:
ExprAST* untilCond;
std::vector<ExprAST*> body;
RepeatExprAST(ExprAST *cond, std::vector<ExprAST*> bo)
:untilCond(cond), body(bo){}
Value* Codegen(CodeGenContext& context) override;
void print(int n) override;
};
//函数,包含原型和函数体表达式
typedef enum { type_function, type_procedure} functionType;
class FunctionAST : public Node{
public:
string name;
std::vector<VariableDeclAST*> headerDecl;
BasicTypeAST* returnType;
std::vector<ConstAST*> consts;
std::vector<SelfdefineTypeAST*> selfdefineType;
std::vector<VariableDeclAST*> bodyDecl;
std::vector<FunctionAST*> functions;
std::vector<ExprAST*> body;
functionType type;//区分是procedure还是function
bool isFunction() { return type == type_function; }
bool isProcedure() { return type == type_procedure; }
FunctionAST(string name, vector<VariableDeclAST *>args, BasicTypeAST *returnType,
vector<ConstAST*> con, vector<SelfdefineTypeAST*> de,
vector<VariableDeclAST*> funcDecl, vector<FunctionAST *> subFun,
vector<ExprAST*> funcBody, functionType t)
:name(name), headerDecl(args), returnType(returnType),
consts(con), selfdefineType(de), bodyDecl(funcDecl), functions(subFun),
body(funcBody), type(t){}
FunctionAST(const FunctionAST &f)
:name(f.name), headerDecl(f.headerDecl), returnType(f.returnType),consts(f.consts),
selfdefineType(f.selfdefineType), bodyDecl(f.bodyDecl), functions(f.functions), body(f.body), type(f.type){}
Value* Codegen(CodeGenContext &astcontext) override;
void print(int n) override;
};
class CodeGenContext{
public:
CodeGenContext *parent;
map<string, Type*> typeTable;
map<string, FunctionAST*> functionTable;
map<string, Value*> locals;
vector<string> loopVar;
FunctionAST *currentFunc;
CodeGenContext(CodeGenContext *parent=NULL):parent(parent){
if(parent != NULL){
currentFunc = parent->currentFunc;
}else{
currentFunc = NULL;
}
}
// Type* getType(string name);
// FunctionAST* getFunction(string name);
// Value* getVar(string name);
// bool addFunction(string name, FunctionAST *FunctionAST);
// bool addVar(string name, Value *var);
// bool addType(string name, Type *type);
};
#endif