-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode_ast.hpp
373 lines (286 loc) · 9.55 KB
/
code_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
#ifndef AST_H_GUARD
#define AST_H_GUARD
#include <string>
#include <vector>
#include <map>
#include <utility>
#include <list>
#include "llvm/LLVMContext.h"
#include "llvm/Module.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Constants.h"
#include "llvm/Instructions.h"
#include "llvm/Analysis/Verifier.h"
#include "llvm/Support/IRBuilder.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetSelect.h"
#include "llvmGen.hpp"
enum NodeType {OP = 0, VAR, CONST, TYPE, RET, WHILE, IF, FUNC, ARRAY, ARRAY_ELEM, IO, FUNC_DEF, FUNC_CALL, LOOK_DEF, ELSE, END_IF, ROOT, COMMENT, ARG};
enum OPType {NONE = 0, ADD, OR, XOR, AND, SUB, MUL, DIV, MOD, UNR, NEG, G, GOE, S, SOE, E, BOOL_OR, BOOL_AND, NOT, NE};
enum VarType {STRING = 0, NUMBER, LETTER, T_ARRAY, T_NONE};
static llvm::IRBuilder<> Builder( llvm::getGlobalContext() );
static llvm::Function * Main;
static llvm::BasicBlock * BB;
class SimpleNode : public node_struct{
public:
SimpleNode();
SimpleNode( node_struct & );
SimpleNode( std::string, std::string, std::string, std::string );
NodeType getType();
OPType getOP();
int getId();
std::string getData();
void debug();
llvm::IRBuilder<> * builder;
std::vector<SimpleNode* > children;
std::string value;
int uniqueId;
protected:
//general
NodeType type;
OPType op;
//data left to parse
std::string data;
};
class Node : public SimpleNode{
public:
Node(){};
virtual ~Node(){};
Node( SimpleNode& );
virtual llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *) = 0;
void addChild( Node* );
static Node * createAST( node_struct & ); //Factory
static Node * createNode( SimpleNode & );
VarType getVarType();
void setVarType( VarType );
std::string getVarId();
void setVarId( std::string );
std::string getValueString();
void setValueString( std::string );
char getValueLetter();
void setValueLetter( char );
int getValueNumber();
void setValueNumber( int );
llvm::Value * getValue();
void setValue( llvm::Value * );
llvm::AllocaInst * alloca;
//children
std::vector<Node*> children;
Node * varNodeWithValue;
bool allocated;
bool wasGenerated();
//printf
//virtual void say();
//protected:
static Node * createOPNode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createVARNode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createCONSTNode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createTYPENode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createRETNode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createIFNode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createWHILENode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createARRAYNode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createARRAYELNode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createELSENode( SimpleNode&, std::list<std::pair<int, int> >& );
static Node * createENDIFNode( SimpleNode&, std::list<std::pair<int, int> >& );
//TODO: decide which fields to leave here, and which to move down
std::string varId;
VarType varType;
//values
llvm::Value * valueLlvm;
int valueNumber;
std::string valueString;
char valueLetter;
bool generated;
int set;
Node * tn;
int arrayType, arraySize;
};
static std::map<std::string, VarType> mapOfTypes;
class OPNode : public Node{
public:
OPNode(){};
OPNode( SimpleNode& );
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
protected:
llvm::Value *lhs;
llvm::Value *rhs;
static llvm::Value *codeGenADD( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenOR( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenXOR( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenAND( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenSUB( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenMUL( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenDIV( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenMOD( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenNEG( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenS( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenG( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenE( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenGOE( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenSOE( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenNOT( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenNE( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenBOR( llvm::IRBuilder<> &, OPNode& );
static llvm::Value *codeGenBAND( llvm::IRBuilder<> &, OPNode& );
};
class TYPENode : public Node{
public:
TYPENode();
TYPENode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
llvm::Type * getLlvmType();
llvm::Type * getLlvmArgType();
std::string getArgName();
VarType getArgType();
void setArrayLength(int);
protected:
int arrayLength;
static llvm::Value *codeGenSTRING( TYPENode & , llvm::IRBuilder<> & );
static llvm::Value *codeGenNUMBER( TYPENode & , llvm::IRBuilder<> & );
static llvm::Value *codeGenLETTER( TYPENode & , llvm::IRBuilder<> & );
};
class VARNode : public Node{
public:
VARNode();
VARNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
void setAlloca( llvm::AllocaInst * );
protected:
llvm::Value *lhs;
};
class RETNode : public Node{
public:
RETNode();
RETNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
};
class CONSTNode : public Node{
public:
CONSTNode();
CONSTNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
protected:
TYPENode * typeNode;
llvm::Value *val;
static llvm::Value *codeGenSTRING( CONSTNode& );
static llvm::Value *codeGenNUMBER( CONSTNode& );
static llvm::Value *codeGenLETTER( CONSTNode& );
};
class ARRAYELEMNode : public Node{
public:
ARRAYELEMNode();
ARRAYELEMNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
protected:
VarType arrayType;
};
class IFNode : public Node{
public:
IFNode();
IFNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
protected:
};
class ELSENode : public Node{
public:
ELSENode();
ELSENode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
protected:
};
class ENDIFNode : public Node{
public:
ENDIFNode();
ENDIFNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
protected:
};
class WHILENode : public Node {
public:
WHILENode();
WHILENode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
};
class ARGNode : public Node{
public:
ARGNode(){type= ARG;};
ARGNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
};
class FUNCTIONNode : public Node {
public:
FUNCTIONNode();
FUNCTIONNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
std::string getFunName();
std::vector<const llvm::Type*> getArgsTypes();
std::vector<std::string> getArgs();
std::string funName;
bool argsAdded;
//protected:
std::vector<const llvm::Type*> args;
std::vector<std::string> argsNames;
std::vector<llvm::Function::arg_iterator> argsValues;
};
class FUNCTIONDEFNode : public Node {
public:
FUNCTIONDEFNode();
FUNCTIONDEFNode( SimpleNode& s);
llvm::Function *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *);
llvm::FunctionType * FT;
llvm::Function * F;
FUNCTIONNode * Fn;
std::string funName;
Environment<Node> * Fenv;
protected:
};
class FUNCTIONCALLNode : public Node {
public:
FUNCTIONCALLNode();
FUNCTIONCALLNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module * );
llvm::Value *callWas(llvm::IRBuilder<> &, Environment<Node>&);
llvm::Value *callBecame(llvm::IRBuilder<> &, Environment<Node>&);
llvm::Value *callDrank(llvm::IRBuilder<> &, Environment<Node>&);
llvm::Value *callAte(llvm::IRBuilder<> &, Environment<Node>&);
llvm::Value *callHad(llvm::IRBuilder<> &, Environment<Node>&);
std::string funName;
std::string getFunName();
std::vector<const llvm::Type*> getArgs();
protected:
std::vector<const llvm::Type*> args;
};
class IONode : public Node{
public:
IONode();
IONode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module * );
std::string funName;
protected:
};
class ARRAYELNode : public Node{
public:
ARRAYELNode();
ARRAYELNode( SimpleNode& s);
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module * );
llvm::Value *codeGenS(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module *, llvm::Value * );
std::string arrName;
llvm::Value * alloca;
protected:
};
class ROOTNode : public Node{
public:
ROOTNode();
ROOTNode( SimpleNode& );
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module * ){return 0;};
protected:
};
class COMMENTNode : public Node{
public:
COMMENTNode();
COMMENTNode( SimpleNode& );
llvm::Value *codeGen(llvm::IRBuilder<> &, Environment<Node>&, llvm::Module * );
protected:
};
#endif