Implementing MonicelliType, refactoring LLVMType.

This commit is contained in:
Stefano Sanfilippo 2015-03-07 14:10:49 +01:00
parent 3968d937e0
commit 6725cbaf88

View File

@ -51,24 +51,6 @@ struct BitcodeEmitter::Private {
Scope<std::string, llvm::AllocaInst*> scope; Scope<std::string, llvm::AllocaInst*> scope;
}; };
static
llvm::Type *LLVMType(Type const& type) {
switch (type) {
case Type::INT:
return llvm::Type::getInt64Ty(getGlobalContext());
case Type::CHAR:
return llvm::Type::getInt8Ty(getGlobalContext());
case Type::FLOAT:
return llvm::Type::getFloatTy(getGlobalContext());
case Type::BOOL:
return llvm::Type::getInt1Ty(getGlobalContext());
case Type::DOUBLE:
return llvm::Type::getDoubleTy(getGlobalContext());
case Type::VOID:
return llvm::Type::getVoidTy(getGlobalContext());
}
}
static static
llvm::AllocaInst* allocateVar(llvm::Function *func, Id const& name, llvm::Type *type) { llvm::AllocaInst* allocateVar(llvm::Function *func, Id const& name, llvm::Type *type) {
llvm::IRBuilder<> builder(&func->getEntryBlock(), func->getEntryBlock().begin()); llvm::IRBuilder<> builder(&func->getEntryBlock(), func->getEntryBlock().begin());
@ -99,6 +81,7 @@ bool reportError(std::initializer_list<std::string> const& what) {
#define I1 llvm::Type::getInt1Ty(getGlobalContext()) #define I1 llvm::Type::getInt1Ty(getGlobalContext())
#define F llvm::Type::getFloatTy(getGlobalContext()) #define F llvm::Type::getFloatTy(getGlobalContext())
#define D llvm::Type::getDoubleTy(getGlobalContext()) #define D llvm::Type::getDoubleTy(getGlobalContext())
#define V llvm::Type::getVoidTy(getGlobalContext())
static const std::unordered_map<llvm::Type*, std::unordered_map<llvm::Type*, llvm::Type*>> TYPECAST_MAP = { static const std::unordered_map<llvm::Type*, std::unordered_map<llvm::Type*, llvm::Type*>> TYPECAST_MAP = {
{I64, { {I8, I64}, {I1, I64}, { F, D}, {D, D}}}, {I64, { {I8, I64}, {I1, I64}, { F, D}, {D, D}}},
@ -108,6 +91,45 @@ static const std::unordered_map<llvm::Type*, std::unordered_map<llvm::Type*, llv
{ D, {{I64, D}, {I8, D}, {I1, D}, { F, D} }} { D, {{I64, D}, {I8, D}, {I1, D}, { F, D} }}
}; };
static
Type MonicelliType(llvm::Type const* type) {
if (type == I64) {
return Type::INT;
} else if (type == I8) {
return Type::CHAR;
} else if (type == I1) {
return Type::BOOL;
} else if (type == D) {
return Type::DOUBLE;
} else if (type == F) {
return Type::FLOAT;
} else if (type == V) {
return Type::VOID;
}
return Type::UNKNOWN;
}
static
llvm::Type *LLVMType(Type const& type) {
switch (type) {
case Type::INT:
return llvm::Type::getInt64Ty(getGlobalContext());
case Type::CHAR:
return llvm::Type::getInt8Ty(getGlobalContext());
case Type::FLOAT:
return llvm::Type::getFloatTy(getGlobalContext());
case Type::BOOL:
return llvm::Type::getInt1Ty(getGlobalContext());
case Type::DOUBLE:
return llvm::Type::getDoubleTy(getGlobalContext());
case Type::VOID:
return llvm::Type::getVoidTy(getGlobalContext());
case Type::UNKNOWN:
return nullptr; // FIXME
}
}
static static
llvm::Type* deduceResultType(llvm::Value *left, llvm::Value *right) { llvm::Type* deduceResultType(llvm::Value *left, llvm::Value *right) {
llvm::Type *lt = left->getType(); llvm::Type *lt = left->getType();
@ -129,6 +151,7 @@ llvm::Type* deduceResultType(llvm::Value *left, llvm::Value *right) {
#undef I1 #undef I1
#undef F #undef F
#undef D #undef D
#undef V
static inline static inline
bool isFP(llvm::Type *type) { bool isFP(llvm::Type *type) {