-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_checker.h
106 lines (63 loc) · 2.15 KB
/
type_checker.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
#pragma once
#ifndef TYPE_CHECKER_H
#define TYPE_CHECKER_H
#include "llvm.h"
namespace rift {
/** The point of the typechecker is to check that the program conforms to the very basic set of rules. These are by far not exhaustive, but they will catch at least something.
The only types you should worry about are D for double vectors, C for character vectors and F for functions. T is the top element of the lattice, meaning that the type of the RVal is uknown.
There is some example code in the implementation of runOnFunction() method, we have prefilled the fromDoubleVector, doubleVectorLiteral and genericSub runtime calls. Your task is to extend this functionality further, including the implementation of phi nodes, where two values merge into one. For this, you will need to fill in proper ordering in the method lessThan.
*/
class TypeChecker : public llvm::FunctionPass {
public:
enum Type {
B,
D,
C,
F,
T,
};
class MachineState {
public:
Type get(llvm::Value * v) {
if (type.count(v))
return type.at(v);
else
return Type::B;
}
void update(llvm::Value * v, Type t) {
Type prev = get(v);
if (prev < t) {
type[v] = t;
changed = true;
}
}
void clear() {
type.clear();
}
void iterationStart() {
changed = false;
}
bool hasReachedFixpoint() {
return !changed;
}
MachineState() : changed(false) {}
friend std::ostream & operator << (std::ostream & s, MachineState & m);
private:
bool changed;
std::map<llvm::Value *, Type> type;
};
static char ID;
char const * getPassName() const override {
return "TypeChecker";
}
TypeChecker() : llvm::FunctionPass(ID) {}
bool runOnFunction(llvm::Function & f);
private:
MachineState state;
};
inline bool operator < (TypeChecker::Type t1, TypeChecker::Type t2) {
// fill me in
return false;
}
} // namespace rift
#endif // TYPE_CHECKER_H