-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.h
249 lines (230 loc) · 6.62 KB
/
tree.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
#ifndef TREE_H
#define TREE_H
#include<iostream>
#include<map>
#include<vector>
#include<stdlib.h>
#include "llvm/Analysis/Passes.h"
#include "llvm/IR/Verifier.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/PassManager.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Transforms/Scalar.h"
using namespace std;
using namespace llvm;
extern map<string,string> typeTab;
class ExprAST
{
public:
virtual ~ExprAST() {}
virtual string getType() = 0;
virtual string getName() = 0;
virtual Value *Codegen() = 0;
};
class LetExprAST : public ExprAST
{
string Name;
ExprAST* RHS;
public:
LetExprAST(const string& name, ExprAST* rhs) : Name(name), RHS(rhs) {}
virtual string getType() { return typeTab[Name]; }
virtual string getName() { return Name; }
virtual Value* Codegen();
};
class IfExprAST : public ExprAST
{
ExprAST *Cond;
ExprAST* Then;
public:
IfExprAST(ExprAST *cond, ExprAST* then) : Cond(cond), Then(then) {}
virtual string getType() { return Cond->getType(); }
virtual string getName() { return Cond->getName(); }
virtual Value *Codegen();
};
class ForExprAST : public ExprAST
{
ExprAST *Start, *End, *Step;
vector<ExprAST*> Body;
public:
ForExprAST(ExprAST *start, ExprAST *end, vector<ExprAST*> body) : Start(start), End(end), Body(body) {}
virtual string getType() { return Start->getName(); }
virtual string getName() { return Start->getType(); }
virtual Value* Codegen();
};
class IntExprAST : public ExprAST
{
public:
int Val;
IntExprAST(double val) : Val(val) {}
virtual string getType() { return "int"; }
virtual string getName() { return "int"; }
virtual Value *Codegen();
};
class DoubleExprAST : public ExprAST
{
public:
double Val;
DoubleExprAST(double val) : Val(val) {}
virtual string getType() { return "double"; }
virtual string getName() { return "double"; }
virtual Value *Codegen();
};
class stringExprAST : public ExprAST
{
public:
const char* Val;
double Size;
stringExprAST(const char* val, double size, bool isPString = false) : Val(val), Size(size) {}
virtual string getType() { return "string"; }
virtual string getName() { string str(Val); return str; }
virtual Value* Codegen();
};
class CharExprAST : public ExprAST
{
public:
char Val;
CharExprAST(char val) : Val(val) {}
virtual string getType() { return "char"; }
virtual string getName() { return "char"; }
virtual Value* Codegen();
};
class VariableRefAST : public ExprAST
{
string Name;
public:
VariableRefAST(const string &name) : Name(name) {}
virtual string getName() { return Name; }
virtual string getType() { return typeTab[Name]; }
virtual Value *Codegen();
};
class VarInitExprAST : public ExprAST
{
string Name;
string Type;
public:
VarInitExprAST(const string &name, const string &type) : Name(name), Type(type) {}
virtual string getName() { return Name; }
virtual string getType() { return Type; }
virtual Value* Codegen();
};
class BinaryExprAST : public ExprAST
{
char Op;
ExprAST *LHS, *RHS;
Value* L, *R;
string lty, rty;
string Var;
public:
BinaryExprAST(char op, ExprAST *lhs, ExprAST *rhs) : Op(op), LHS(lhs), RHS(rhs) {}
BinaryExprAST(char op, const string& var, ExprAST *rhs) : Op(op), Var(var), RHS(rhs) {}
virtual string getType() { return LHS->getType(); }
string getLHSVar() { return dynamic_cast<VariableRefAST*>(LHS)->getName(); }
string getRHSVar() { return dynamic_cast<VariableRefAST*>(RHS)->getName(); }
virtual string getName() { return Var; }
virtual Value *Codegen();
private:
bool checkTypes(); //Checks if the types match
void convertTypes(); //Will up/downconvert types as nessicary
bool isPtrOp(); //Returns true if the operation involves pointers
bool isArrayOp();
Value* doOp();
Value* doPtrOp();
Value* doAssignmentOp();
};
class CallExprAST : public ExprAST
{
string Callee;
vector<ExprAST*> Args;
public:
CallExprAST(const string &callee, vector<ExprAST*> &args) : Callee(callee), Args(args) {}
virtual string getType() { return "None"; }
virtual string getName() { return Callee; }
virtual Value *Codegen();
};
class PrototypeAST : public ExprAST
{
string Name;
vector<VarInitExprAST*> Args;
string Ty;
public:
PrototypeAST(const string &name, const vector<VarInitExprAST*> &args) : Name(name), Args(args) {}
virtual string getType() { return Ty; }
virtual string getName() { return Name; }
Function *Codegen();
void CreateArgumentAllocas(Function *F);
};
class FunctionAST : public ExprAST
{
PrototypeAST *Proto;
vector<ExprAST*> Body;
public:
FunctionAST(PrototypeAST *proto, vector<ExprAST*> body) : Proto(proto), Body(body) {}
virtual string getType() { return Proto->getType(); }
virtual string getName() { return Proto->getName(); }
Function *Codegen();
};
template <typename T, typename U>
class SymbolTable
{
private:
map<T,U> _NamedValues;
map<T,U> _GlobalValues;
public:
SymbolTable() {}
~SymbolTable() {}
typedef typename std::map<T,U>::iterator iterator;
typedef typename std::map<T,U>::const_iterator const_iterator;
iterator begin() { return _NamedValues.begin(); }
const_iterator begin() const { return _NamedValues.begin(); }
iterator end() { return _GlobalValues.end(); }
const_iterator end() const { return _GlobalValues.end(); }
iterator find(T key)
{
if (_NamedValues.find(key) == _NamedValues.end())
return _GlobalValues.find(key);
else
return _NamedValues.find(key);
}
void clear() { _NamedValues.clear(); }
void clearAll() { this->clear(); _GlobalValues.clear(); }
void addGlobal(T key, U val) { _GlobalValues[key] = val; }
U& operator[](T key)
{
if (_NamedValues[key])
return _NamedValues[key];
else
return _GlobalValues[key];
}
U operator[] (T key) const
{
if (_NamedValues[key])
return _NamedValues[key];
else
return _GlobalValues[key];
}
void dump()
{
//THIS IS HACKY. For some reason clang won't allow map<T,U>::iterator here. Gives a missing semicolon error
map<string,Value*>::iterator it;
cout << "\nDumping vars: \n";
for(it=_NamedValues.begin();it!=_NamedValues.end(); it++)
{
cout << it->first << ": " << it->second;
cout << "\tType: " << typeTab[it->first] << endl;
}
cout << "\nDumping global vars: \n";
for(it=_GlobalValues.begin();it!=_GlobalValues.end(); it++)
{
cout << it->first << ": " << it->second;
cout << "\tType: " << typeTab[it->first] << endl;
}
}
};
void createExtern(PrototypeAST* P);
void ERROR(string err);
#endif