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
|
#ifndef PCC_TYPE_H
#define PCC_TYPE_H
#include <memory>
#include <string>
#include <vector>
#include <llvm/IR/IRBuilder.h>
#include <llvm/IR/Module.h>
#include <llvm/IR/Type.h>
namespace pcc {
using Value = llvm::Value*;
class Context;
enum class BinaryOperator {
ADD,
SUB,
MUL,
DIV,
MOD,
LT,
LE,
GT,
GE,
EQ,
NE,
AND,
OR,
XOR,
SHL,
SHR,
BINARYOP_NUMBER,
REAL_DIV // the difference between REAL_DIV and DIV is handled in
// TypeManager, thus we don't need to create instruction for this.
};
enum class UnaryOperator { POS, NEG, NOT, UNARYOP_NUMEBR };
const char* GetOperatorName(BinaryOperator op);
const char* GetOperatorName(UnaryOperator op);
template <typename T>
constexpr std::underlying_type_t<T> ToUnderlying(T value) {
return static_cast<std::underlying_type_t<T>>(value);
}
class Type {
public:
Type(std::string&& name, llvm::Type* LLVMType);
virtual const char* GetCommonName();
llvm::Type* GetLLVMType();
// return nullptr if the operation is not supported
virtual Value CreateBinaryOperation(BinaryOperator op, Value v0, Value v1,
Context* context);
virtual Value CreateUnaryOperation(UnaryOperator op, Value v,
Context* context);
virtual llvm::Constant* GetZeroInitializer();
protected:
Value NotAllowed(Value v0, Value v1, Context* context);
Value NotAllowed(Value v, Context* context);
Value CreateAdd(Value v0, Value v1, Context* context);
Value CreateSub(Value v0, Value v1, Context* context);
Value CreateMul(Value v0, Value v1, Context* context);
Value CreateDiv(Value v0, Value v1, Context* context);
Value CreateMod(Value v0, Value v1, Context* context);
Value CreateLt(Value v0, Value v1, Context* context);
Value CreateLe(Value v0, Value v1, Context* context);
Value CreateGt(Value v0, Value v1, Context* context);
Value CreateGe(Value v0, Value v1, Context* context);
Value CreateEq(Value v0, Value v1, Context* context);
Value CreateNe(Value v0, Value v1, Context* context);
Value CreateAnd(Value v0, Value v1, Context* context);
Value CreateOr(Value v0, Value v1, Context* context);
Value CreateXor(Value v0, Value v1, Context* context);
Value CreateShl(Value v0, Value v1, Context* context);
Value CreateShr(Value v0, Value v1, Context* context);
Value CreatePos(Value v, Context* context);
Value CreateNeg(Value v, Context* context);
Value CreateNot(Value v, Context* context);
Value CreateFAdd(Value v0, Value v1, Context* context);
Value CreateFSub(Value v0, Value v1, Context* context);
Value CreateFMul(Value v0, Value v1, Context* context);
Value CreateFDiv(Value v0, Value v1, Context* context);
Value CreateFMod(Value v0, Value v1, Context* context);
Value CreateFLt(Value v0, Value v1, Context* context);
Value CreateFLe(Value v0, Value v1, Context* context);
Value CreateFGt(Value v0, Value v1, Context* context);
Value CreateFGe(Value v0, Value v1, Context* context);
Value CreateFEq(Value v0, Value v1, Context* context);
Value CreateFNe(Value v0, Value v1, Context* context);
Value CreateFPos(Value v, Context* context);
Value CreateFNeg(Value v, Context* context);
std::string name_;
llvm::Type* LLVMType_;
};
class VoidType : public Type {
public:
VoidType();
};
class IntegerBaseType : public Type {
public:
IntegerBaseType(std::string&& name, int width, llvm::Type* LLVMType);
int GetWidth();
protected:
int width_;
};
class BooleanType : public IntegerBaseType {
public:
BooleanType();
virtual Value CreateBinaryOperation(BinaryOperator op, Value v0, Value v1,
Context* context) override;
virtual Value CreateUnaryOperation(UnaryOperator op, Value v,
Context* context) override;
};
class CharType : public IntegerBaseType {
public:
CharType();
virtual Value CreateBinaryOperation(BinaryOperator op, Value v0, Value v1,
Context* context) override;
virtual Value CreateUnaryOperation(UnaryOperator op, Value v,
Context* context) override;
};
class IntegerType : public IntegerBaseType {
public:
IntegerType();
virtual Value CreateBinaryOperation(BinaryOperator op, Value v0, Value v1,
Context* context) override;
virtual Value CreateUnaryOperation(UnaryOperator op, Value v,
Context* context) override;
};
class RealType : public Type {
public:
RealType();
virtual Value CreateBinaryOperation(BinaryOperator op, Value v0, Value v1,
Context* context) override;
virtual Value CreateUnaryOperation(UnaryOperator op, Value v,
Context* context) override;
};
class ArrayType : public Type {
public:
ArrayType(std::shared_ptr<Type> ElementType, uint64_t ElementNumber,
std::shared_ptr<Type> IndexType, Value start, Value end,
std::string&& name, bool IsZeroStarted = false);
std::shared_ptr<Type> GetElementType();
std::shared_ptr<Type> GetIndexType();
bool GetIsZeroStarted();
Value GetIndexStart();
protected:
std::shared_ptr<Type> ElementType_;
std::shared_ptr<Type> IndexType_;
bool IsZeroStarted_;
Value start_, end_;
};
class PointerType : public Type {
public:
PointerType(std::shared_ptr<Type> ElementType,
std::shared_ptr<Type> DifferenceType,
std::shared_ptr<Type> IndexType, std::string&& name);
std::shared_ptr<Type> GetElementType();
std::shared_ptr<Type> GetIndexType();
std::shared_ptr<Type> GeDiifferenceType();
virtual Value CreateBinaryOperation(BinaryOperator op, Value v0, Value v1,
Context* context) override;
protected:
Value CreatePointerSub(Value v0, Value v1, Context* context);
std::shared_ptr<Type> ElementType_;
std::shared_ptr<Type> DifferenceType_;
std::shared_ptr<Type> IndexType_;
};
class FunctionType : public Type {
public:
FunctionType(std::shared_ptr<Type> ReturnType,
std::vector<std::shared_ptr<Type>> ArgTypes,
std::string&& name, bool IsVariadic = false);
std::shared_ptr<Type> GetReturnType();
std::vector<std::shared_ptr<Type>> GetArgTypes();
bool IsVariadic();
protected:
std::shared_ptr<Type> ReturnType_;
std::vector<std::shared_ptr<Type>> ArgTypes_;
bool IsVariadic_;
};
using RecordMember = std::pair<std::string, std::shared_ptr<Type>>;
class RecordType : public Type {
public:
RecordType(const std::vector<RecordMember>& members, std::string&& name);
std::pair<int, std::shared_ptr<Type>> GetMember(const std::string& name);
protected:
std::map<std::string, std::pair<int, std::shared_ptr<Type>>> members_;
};
} // namespace pcc
#endif // PCC_TYPE_H
|