-
Notifications
You must be signed in to change notification settings - Fork 58
/
TypeSystem.cpp
184 lines (158 loc) · 5.28 KB
/
TypeSystem.cpp
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
//
// Created by cs on 2017/5/31.
//
#include "TypeSystem.h"
#include "CodeGen.h"
//
//Value* VarType::getDefaultValue(LLVMContext &context) const {
// if( this->name == "int" ){
// return ConstantInt::get(Type::getInt32Ty(context), 0, true);
// }else if( this->name == "double" ){
// return ConstantFP::get(Type::getDoubleTy(context), 0);
// }
// return nullptr;
//}
//
//Value* VarArrayType::getDefaultValue(LLVMContext &context) const {
//
//}
string TypeSystem::llvmTypeToStr(Type *value) {
Type::TypeID typeID;
if( value )
typeID = value->getTypeID();
else
return "Value is nullptr";
switch (typeID){
case Type::VoidTyID:
return "VoidTyID";
case Type::HalfTyID:
return "HalfTyID";
case Type::FloatTyID:
return "FloatTyID";
case Type::DoubleTyID:
return "DoubleTyID";
case Type::IntegerTyID:
return "IntegerTyID";
case Type::FunctionTyID:
return "FunctionTyID";
case Type::StructTyID:
return "StructTyID";
case Type::ArrayTyID:
return "ArrayTyID";
case Type::PointerTyID:
return "PointerTyID";
case Type::VectorTyID:
return "VectorTyID";
default:
return "Unknown";
}
}
string TypeSystem::llvmTypeToStr(Value *value) {
if( value )
return llvmTypeToStr(value->getType());
else
return "Value is nullptr";
}
TypeSystem::TypeSystem(LLVMContext &context): llvmContext(context){
addCast(intTy, floatTy, llvm::CastInst::SIToFP);
addCast(intTy, doubleTy, llvm::CastInst::SIToFP);
addCast(boolTy, doubleTy, llvm::CastInst::SIToFP);
addCast(floatTy, doubleTy, llvm::CastInst::FPExt);
addCast(floatTy, intTy, llvm::CastInst::FPToSI);
addCast(doubleTy, intTy, llvm::CastInst::FPToSI);
addCast(intTy, intTy, llvm::CastInst::SExt);
}
void TypeSystem::addStructMember(string structName, string memType, string memName) {
if( this->_structTypes.find(structName) == this->_structTypes.end() ){
LogError("Unknown struct name");
}
this->_structMembers[structName].push_back(std::make_pair(memType, memName));
}
void TypeSystem::addStructType(string name, llvm::StructType *type) {
this->_structTypes[name] = type;
this->_structMembers[name] = std::vector<TypeNamePair>();
}
Type *TypeSystem::getVarType(const NIdentifier& type) {
assert(type.isType);
if( type.isArray ){ // array type when allocation, pointer type when pass parameters
// return ArrayType::get(getVarType(type.name), type.arraySize->value);
return PointerType::get(getVarType(type.name), 0);
// PointerType::getUnqual(ArrayType::get(getVarType(type.name), type.arraySize->value));
}
return getVarType(type.name);
return 0;
}
Value* TypeSystem::getDefaultValue(string typeStr, LLVMContext &context) {
Type* type = this->getVarType(typeStr);
if( type == this->intTy ){
return ConstantInt::get(type, 0, true);
}else if( type == this->doubleTy || type == this->floatTy ){
return ConstantFP::get(type, 0);
}
return nullptr;
}
void TypeSystem::addCast(Type *from, Type *to, CastInst::CastOps op) {
if( _castTable.find(from) == _castTable.end() ){
_castTable[from] = std::map<Type*, CastInst::CastOps>();
}
_castTable[from][to] = op;
}
Value* TypeSystem::cast(Value *value, Type *type, BasicBlock *block) {
Type* from = value->getType();
if( from == type )
return value;
if( _castTable.find(from) == _castTable.end() ){
LogError("Type has no cast");
return value;
}
if( _castTable[from].find(type) == _castTable[from].end() ){
string error = "Unable to cast from ";
error += llvmTypeToStr(from) + " to " + llvmTypeToStr(type);
LogError(error.c_str());
return value;
}
return CastInst::Create(_castTable[from][type], value, type, "cast", block);
}
bool TypeSystem::isStruct(string typeStr) const {
return this->_structTypes.find(typeStr) != this->_structTypes.end();
}
long TypeSystem::getStructMemberIndex(string structName, string memberName) {
if( this->_structTypes.find(structName) == this->_structTypes.end() ){
LogError("Unknown struct name");
return 0;
}
auto& members = this->_structMembers[structName];
for(auto it=members.begin(); it!=members.end(); it++){
if( it->second == memberName ){
return std::distance(members.begin(), it);
}
}
LogError("Unknown struct member");
return 0;
}
Type *TypeSystem::getVarType(string typeStr) {
if( typeStr.compare("int") == 0 ){
return this->intTy;
}
if( typeStr.compare("float") == 0 ){
return this->floatTy;
}
if( typeStr.compare("double") == 0 ){
return this->doubleTy;
}
if( typeStr.compare("bool") == 0 ){
return this->boolTy;
}
if( typeStr.compare("char") == 0 ){
return this->charTy;
}
if( typeStr.compare("void") == 0 ){
return this->voidTy;
}
if( typeStr.compare("string") == 0 ){
return this->stringTy;
}
if( this->_structTypes.find(typeStr) != this->_structTypes.end() )
return this->_structTypes[typeStr];
return nullptr;
}