aboutsummaryrefslogtreecommitdiffstats
path: root/src/llvm.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/llvm.cpp')
-rw-r--r--src/llvm.cpp54
1 files changed, 26 insertions, 28 deletions
diff --git a/src/llvm.cpp b/src/llvm.cpp
index 36f639f..cd5314a 100644
--- a/src/llvm.cpp
+++ b/src/llvm.cpp
@@ -195,12 +195,6 @@ struct LLVMEngine : public Engine {
FunctionPassManager opt;
};
-static LLVMEngine*
-llEngine(CEnv& cenv)
-{
- return reinterpret_cast<LLVMEngine*>(cenv.engine());
-}
-
/// Create a new Engine (shared library entry point)
Engine*
tuplr_new_engine()
@@ -233,9 +227,10 @@ AFn::liftCall(CEnv& cenv, const AType& argsT)
{
TEnv::GenericTypes::const_iterator gt = cenv.tenv.genericTypes.find(this);
assert(gt != cenv.tenv.genericTypes.end());
- AType* genericType = new AType(*gt->second);
- AType* thisType = genericType;
- Subst argsSubst;
+ LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
+ AType* genericType = new AType(*gt->second);
+ AType* thisType = genericType;
+ Subst argsSubst;
if (!genericType->concrete()) {
// Build substitution to apply to generic type
@@ -317,7 +312,7 @@ AFn::liftCall(CEnv& cenv, const AType& argsT)
#ifdef EXPLICIT_STACK_FRAMES
- IRBuilder<> builder = llEngine(cenv)->builder;
+ IRBuilder<> builder = engine->builder;
// Scan out definitions
for (size_t i = 0; i < size(); ++i) {
@@ -334,7 +329,7 @@ AFn::liftCall(CEnv& cenv, const AType& argsT)
StructType* frameT = StructType::get(types, false);
Value* tag = ConstantInt::get(Type::Int8Ty, GC::TAG_FRAME);
Value* frameSize = ConstantInt::get(Type::Int32Ty, s / 8);
- Value* frame = builder.CreateCall2(llEngine(cenv)->alloc, frameSize, tag, "frame");
+ Value* frame = builder.CreateCall2(engine->alloc, frameSize, tag, "frame");
Value* framePtr = builder.CreateBitCast(frame, PointerType::get(frameT, 0));
// Bind parameter values in stack frame
@@ -373,11 +368,12 @@ AFn::compile(CEnv& cenv)
/*vector<const Type*> types;
types.push_back(PointerType::get(fnT, 0));
types.push_back(PointerType::get(Type::VoidTy, 0));
- IRBuilder<> builder = llEngine(cenv)->builder;
+ LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
+ IRBuilder<> builder = engine->builder;
Value* tag = ConstantInt::get(Type::Int8Ty, GC::TAG_FRAME);
StructType* tupT = StructType::get(types, false);
Value* tupSize = ConstantInt::get(Type::Int32Ty, sizeof(void*) * 2);
- Value* tup = builder.CreateCall2(llEngine(cenv)->alloc, tupSize, tag, "fn");
+ Value* tup = builder.CreateCall2(engine->alloc, tupSize, tag, "fn");
Value* tupPtr = builder.CreateBitCast(tup, PointerType::get(tupT, 0));
return tupPtr;*/
}
@@ -386,7 +382,8 @@ CValue
AIf::compile(CEnv& cenv)
{
typedef vector< pair<Value*, BasicBlock*> > Branches;
- Function* parent = llEngine(cenv)->builder.GetInsertBlock()->getParent();
+ LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
+ Function* parent = engine->builder.GetInsertBlock()->getParent();
BasicBlock* mergeBB = BasicBlock::Create("endif");
BasicBlock* nextBB = NULL;
Branches branches;
@@ -396,29 +393,29 @@ AIf::compile(CEnv& cenv)
nextBB = BasicBlock::Create((format("else%1%") % ((i+1)/2)).str());
- llEngine(cenv)->builder.CreateCondBr(condV, thenBB, nextBB);
+ engine->builder.CreateCondBr(condV, thenBB, nextBB);
// Emit then block for this condition
parent->getBasicBlockList().push_back(thenBB);
- llEngine(cenv)->builder.SetInsertPoint(thenBB);
+ engine->builder.SetInsertPoint(thenBB);
Value* thenV = llVal(cenv.compile(at(i+1)));
- llEngine(cenv)->builder.CreateBr(mergeBB);
- branches.push_back(make_pair(thenV, llEngine(cenv)->builder.GetInsertBlock()));
+ engine->builder.CreateBr(mergeBB);
+ branches.push_back(make_pair(thenV, engine->builder.GetInsertBlock()));
parent->getBasicBlockList().push_back(nextBB);
- llEngine(cenv)->builder.SetInsertPoint(nextBB);
+ engine->builder.SetInsertPoint(nextBB);
}
// Emit final else block
- llEngine(cenv)->builder.SetInsertPoint(nextBB);
+ engine->builder.SetInsertPoint(nextBB);
Value* elseV = llVal(cenv.compile(at(size() - 1)));
- llEngine(cenv)->builder.CreateBr(mergeBB);
- branches.push_back(make_pair(elseV, llEngine(cenv)->builder.GetInsertBlock()));
+ engine->builder.CreateBr(mergeBB);
+ branches.push_back(make_pair(elseV, engine->builder.GetInsertBlock()));
// Emit merge block (Phi node)
parent->getBasicBlockList().push_back(mergeBB);
- llEngine(cenv)->builder.SetInsertPoint(mergeBB);
- PHINode* pn = llEngine(cenv)->builder.CreatePHI(llType(cenv.type(this)), "ifval");
+ engine->builder.SetInsertPoint(mergeBB);
+ PHINode* pn = engine->builder.CreatePHI(llType(cenv.type(this)), "ifval");
FOREACH(Branches::iterator, i, branches)
pn->addIncoming(i->first, i->second);
@@ -429,6 +426,7 @@ AIf::compile(CEnv& cenv)
CValue
APrimitive::compile(CEnv& cenv)
{
+ LLVMEngine* engine = reinterpret_cast<LLVMEngine*>(cenv.engine());
Value* a = llVal(cenv.compile(at(1)));
Value* b = llVal(cenv.compile(at(2)));
bool isFloat = cenv.type(at(1))->str() == "Float";
@@ -445,9 +443,9 @@ APrimitive::compile(CEnv& cenv)
if (n == "/") op = isFloat ? Instruction::FDiv : Instruction::SDiv;
if (n == "%") op = isFloat ? Instruction::FRem : Instruction::SRem;
if (op != 0) {
- Value* val = llEngine(cenv)->builder.CreateBinOp(op, a, b);
+ Value* val = engine->builder.CreateBinOp(op, a, b);
for (size_t i = 3; i < size(); ++i)
- val = llEngine(cenv)->builder.CreateBinOp(op, val, llVal(cenv.compile(at(i))));
+ val = engine->builder.CreateBinOp(op, val, llVal(cenv.compile(at(i))));
return val;
}
@@ -461,9 +459,9 @@ APrimitive::compile(CEnv& cenv)
if (n == "<=") pred = isFloat ? CmpInst::FCMP_OLE : CmpInst::ICMP_SLE;
if (pred != 0) {
if (isFloat)
- return llEngine(cenv)->builder.CreateFCmp(pred, a, b);
+ return engine->builder.CreateFCmp(pred, a, b);
else
- return llEngine(cenv)->builder.CreateICmp(pred, a, b);
+ return engine->builder.CreateICmp(pred, a, b);
}
throw Error(loc, "unknown primitive");