forked from petrv7/llvm2c
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathExpr.h
411 lines (309 loc) · 9.15 KB
/
Expr.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
#pragma once
#include <string>
#include <map>
#include <vector>
#include <memory>
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/Casting.h"
#include "../type/Type.h"
#include "ExprVisitor.h"
class Block;
/**
* @brief The Expr class is an abstract class for all expressions.
*/
class Expr {
public:
enum ExprKind {
EK_AggregateElement,
EK_ArrayElement,
EK_ExtractValueExpr,
EK_Value,
EK_GlobalValue,
EK_IfExpr,
EK_GotoExpr,
EK_SwitchExpr,
EK_AsmExpr,
EK_CallExpr,
EK_PointerShift,
EK_GepExpr,
EK_SelectExpr,
EK_StackAlloc,
EK_RefExpr,
EK_DerefExpr,
EK_RetExpr,
EK_CastExpr,
EK_AddExpr,
EK_SubExpr,
EK_AssignExpr,
EK_MulExpr,
EK_DivExpr,
EK_RemExpr,
EK_AndExpr,
EK_OrExpr,
EK_XorExpr,
EK_CmpExpr,
EK_AshrExpr,
EK_LshrExpr,
EK_ShlExpr,
EK_AggregateInitializer,
EK_ArrowExpr,
EK_LogicalAnd,
EK_LogicalOr,
EK_LogicalNot,
EK_ExprList,
EK_MinusExpr,
EK_DoWhile,
};
private:
const ExprKind kind;
public:
ExprKind getKind() const { return kind; }
Expr(ExprKind kind): kind(kind) {}
virtual ~Expr() = default;
virtual void accept(ExprVisitor& visitor) = 0;
virtual const Type* getType() const = 0;
virtual Type* getType() = 0;
virtual void setType(Type*) = 0;
virtual bool isZero() const = 0;
virtual bool isSimple() const = 0;
};
/**
* @brief The ExprBase class is a base class for all expressions. It contains type of the expression and function to get/set the type.
*/
class ExprBase : public Expr {
private:
Type* type;
public:
ExprBase(ExprKind kind) : Expr(kind) {}
const Type* getType() const override {
return type;
}
Type* getType() override {
return type;
}
void setType(Type* type) override {
this->type = type;
}
bool isZero() const override {
return false;
}
bool isSimple() const override {
return false;
}
};
class ExprList : public ExprBase {
public:
std::vector<Expr*> expressions;
ExprList(std::vector<Expr*>&& expressions);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The StructElement class represents access to element of a struct.
*/
class AggregateElement : public ExprBase {
public:
Expr* expr; //expression being accessed
unsigned element; //number of the element
AggregateElement(Expr*, unsigned);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The ArrayElement class represents access to element of an array.
*/
class ArrayElement : public ExprBase {
public:
Expr* expr; //expression being accessed
Expr* element; //expression representing index of the element
ArrayElement(Expr*, Expr*);
ArrayElement(Expr*, Expr*, Type*);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The ExtractValueExpr class represents extractvalue instruction in C. It is made of sequence of StructElement and ArrayElements expressions.
*/
class ExtractValueExpr : public ExprBase {
public:
std::vector<std::unique_ptr<Expr>> indices; //sequence of StructElement and ArrayElements expressions
ExtractValueExpr(std::vector<std::unique_ptr<Expr>>&&);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The Value class represents variable or constant value.
*/
class Value : public ExprBase {
public:
std::string valueName;
Value(const std::string&, Type*);
Value(const std::string&, Type*, ExprKind kind);
void accept(ExprVisitor& visitor) override;
bool isZero() const override;
bool isSimple() const override;
void setName(const std::string& name) { valueName = name; }
static bool classof(const Expr* expr);
};
/**
* @brief The GlobalValue class represents global variable.
*/
class GlobalValue : public Value {
public:
Expr* value;
bool isStatic = false;
GlobalValue(const std::string&, Expr*, Type*);
void accept(ExprVisitor& visitor) override;
bool isSimple() const override;
static bool classof(const Expr* expr);
};
/**
* @brief The IfExpr class represents br instruction in C as an if-else statement.
*/
class IfExpr : public ExprBase {
public:
Expr* cmp; //expression used as a condition
Expr* trueList;
Expr* falseList;
IfExpr(Expr*, Expr*, Expr*);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The GotoExpr class represents br instruction in C as a goto statement.
*/
class GotoExpr : public ExprBase {
public:
Block* target; //else goto falseBlock
GotoExpr(Block* target);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The SwitchExpr class represents switch.
*/
class SwitchExpr : public ExprBase {
public:
Expr* cmp; //expression used in switch
Expr* def; //default
std::map<int, Expr*> cases; //cases of switch
SwitchExpr(Expr*, Expr*, std::map<int, Expr*>);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The AsmExpr class represents calling of inline asm.
*/
class AsmExpr : public ExprBase {
public:
std::string inst; //asm string
std::vector<std::pair<std::string, Expr*>> output; //output constraints
std::vector<std::pair<std::string, Expr*>> input; //input constraints
std::string clobbers; //clobber
AsmExpr(const std::string&, const std::vector<std::pair<std::string, Expr*>>&, const std::vector<std::pair<std::string, Expr*>>&, const std::string&);
/**
* @brief addOutputExpr Adds Expr to the output vector
* @param expr Output expression
* @param pos Position in vector
*/
void addOutputExpr(Expr* expr, unsigned pos);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The CallExpr class represents function call.
*/
class CallExpr : public ExprBase {
public:
std::string funcName; //name of the called function
std::vector<Expr*> params; //parameters of the function call
Expr* funcValue; //expression in case of calling function pointer
CallExpr(Expr*, const std::string&, std::vector<Expr*>, Type*);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
bool isSimple() const override;
};
/**
* @brief The PointerShift class represents shifting a pointer.
*/
class PointerShift : public ExprBase {
public:
Type* ptrType; //type of the pointer
Expr* pointer; //expression being shifted
Expr* move; //expression representing number used for shifting
PointerShift(Type*, Expr*, Expr*);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The GepExpr class represents getelementptr instruction in C. It is made of sequence of StructElement, ArrayElement and PointerShift expressions.
*/
class GepExpr : public ExprBase {
public:
std::vector<Expr*> indices; //sequence of StructElement, ArrayElement and PointerShift expressions
GepExpr(std::vector<Expr*>&&);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The SelectExpr class represents select instruction in C (comp ? left : right).
*/
class SelectExpr : public ExprBase {
public:
Expr* left;
Expr* right;
Expr* comp;
SelectExpr(Expr*, Expr*, Expr*);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
class StackAlloc : public ExprBase {
public:
Value* value;
StackAlloc(Value*);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
class AggregateInitializer : public ExprBase {
public:
std::vector<Expr *> values;
AggregateInitializer(std::vector<Expr *> values);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
/**
* @brief The ArrowExpr represents an access to structure field via structure pointer
* struct->field
*/
class ArrowExpr : public ExprBase {
public:
Expr* expr; //expression being accessed
unsigned element; //number of the element
ArrowExpr(Expr*, unsigned);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
class LogicalAnd : public ExprBase {
public:
Expr* lhs;
Expr* rhs;
LogicalAnd(Expr*, Expr*);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
class LogicalOr : public ExprBase {
public:
Expr* lhs;
Expr* rhs;
LogicalOr(Expr*, Expr*);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};
class DoWhile : public Expr {
public:
Expr* body;
Expr* cond;
DoWhile(Expr* body, Expr* cond);
void accept(ExprVisitor& visitor) override;
static bool classof(const Expr* expr);
};