-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeStream.cpp
More file actions
111 lines (99 loc) · 2.62 KB
/
Copy pathTypeStream.cpp
File metadata and controls
111 lines (99 loc) · 2.62 KB
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
#include "TypeStream.h"
#include "codegen.h"
TypeStream::TypeStream(LLVMContext& context):context(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);
}
Type* TypeStream::getVarType(string typeStr){
if( typeStr.compare("int") == 0 ){
return this->intTy;
}
else if( typeStr.compare("double") == 0 ){
return this->doubleTy;
}
else if( typeStr.compare("float") == 0 ){
return this->floatTy;
}
else if( typeStr.compare("char") == 0 ){
return this->charTy;
}
else if( typeStr.compare("bool") == 0 ){
return this->boolTy;
}
else if( typeStr.compare("void") == 0 ){
return this->voidTy;
}
else if( typeStr.compare("string") == 0 ){
return this->stringTy;
}
else
return nullptr;
}
Type* TypeStream::getVarType(const Identifier& value){
assert(value.isType);
if(value.isArray){
return PointerType::get(getVarType(value.name),0);
}
return getVarType(value.name);
}
Value* TypeStream::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 TypeStream::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* TypeStream::cast(Value* value,Type* type,BasicBlock* block){
Type* from = value->getType();
if(from == type) return value;
if (_castTable.find(from) == _castTable.end()){
return value;
}
if(_castTable[from].find(type) == _castTable[from].end()){
return value;
}
return CastInst::Create(_castTable[from][type], value, type, "cast", block);
}
string TypeStream::llvmTypeToString(Type* value){
Type::TypeID typeID;
if (value){
typeID = value->getTypeID();
}
else
return "Value is nullptr";
switch(typeID){
case Type::VoidTyID:
return "VoidTyID";
case Type::IntegerTyID:
return "IntegerTyID";
case Type::FloatTyID:
return "FloatTyID";
case Type::DoubleTyID:
return "DoubleTyID";
case Type::ArrayTyID:
return "ArrayTyID";
case Type::PointerTyID:
return "PointerTyID";
default:
return "Unknow Type";
}
}
string TypeStream::llvmTypeToString(Value* value){
if(value)
return llvmTypeToString(value->getType());
else
return "Value is nullptr";
}