|
|
@ -23,96 +23,6 @@ Stack::Stack(llvm::IRBuilder<>& _builder, RuntimeManager& _runtimeManager): |
|
|
|
m_stack(_builder, "stack") |
|
|
|
{} |
|
|
|
|
|
|
|
llvm::Function* Stack::getPushFunc() |
|
|
|
{ |
|
|
|
auto& func = m_push; |
|
|
|
if (!func) |
|
|
|
{ |
|
|
|
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word}; |
|
|
|
func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::ExternalLinkage, "stack.push", getModule()); |
|
|
|
llvm::Type* extArgTypes[] = {Type::RuntimePtr, Type::WordPtr}; |
|
|
|
auto extPushFunc = llvm::Function::Create(llvm::FunctionType::get(Type::Void, extArgTypes, false), llvm::Function::ExternalLinkage, "stack_push", getModule()); |
|
|
|
|
|
|
|
auto rt = &func->getArgumentList().front(); |
|
|
|
rt->setName("rt"); |
|
|
|
auto value = rt->getNextNode(); |
|
|
|
value->setName("value"); |
|
|
|
|
|
|
|
InsertPointGuard guard{m_builder}; |
|
|
|
auto entryBB = llvm::BasicBlock::Create(m_builder.getContext(), {}, func); |
|
|
|
m_builder.SetInsertPoint(entryBB); |
|
|
|
auto a = m_builder.CreateAlloca(Type::Word); |
|
|
|
m_builder.CreateStore(value, a); |
|
|
|
createCall(extPushFunc, {rt, a}); |
|
|
|
m_builder.CreateRetVoid(); |
|
|
|
} |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Function* Stack::getSetFunc() |
|
|
|
{ |
|
|
|
auto& func = m_set; |
|
|
|
if (!func) |
|
|
|
{ |
|
|
|
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Size, Type::Word}; |
|
|
|
func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::ExternalLinkage, "stack.set", getModule()); |
|
|
|
llvm::Type* extArgTypes[] = {Type::RuntimePtr, Type::Size, Type::WordPtr}; |
|
|
|
auto extSetFunc = llvm::Function::Create(llvm::FunctionType::get(Type::Void, extArgTypes, false), llvm::Function::ExternalLinkage, "stack_set", getModule()); |
|
|
|
|
|
|
|
auto rt = &func->getArgumentList().front(); |
|
|
|
rt->setName("rt"); |
|
|
|
auto index = rt->getNextNode(); |
|
|
|
index->setName("index"); |
|
|
|
auto value = index->getNextNode(); |
|
|
|
value->setName("value"); |
|
|
|
|
|
|
|
InsertPointGuard guard{m_builder}; |
|
|
|
auto entryBB = llvm::BasicBlock::Create(m_builder.getContext(), {}, func); |
|
|
|
m_builder.SetInsertPoint(entryBB); |
|
|
|
auto a = m_builder.CreateAlloca(Type::Word); |
|
|
|
m_builder.CreateStore(value, a); |
|
|
|
createCall(extSetFunc, {rt, index, a}); |
|
|
|
m_builder.CreateRetVoid(); |
|
|
|
} |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Function* Stack::getPopFunc() |
|
|
|
{ |
|
|
|
auto& func = m_pop; |
|
|
|
if (!func) |
|
|
|
{ |
|
|
|
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Size, Type::BytePtr}; |
|
|
|
func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::ExternalLinkage, "stack.pop", getModule()); |
|
|
|
llvm::Type* extArgTypes[] = {Type::RuntimePtr, Type::Size}; |
|
|
|
auto extPopFunc = llvm::Function::Create(llvm::FunctionType::get(Type::Bool, extArgTypes, false), llvm::Function::ExternalLinkage, "stack_pop", getModule()); |
|
|
|
|
|
|
|
auto rt = &func->getArgumentList().front(); |
|
|
|
rt->setName("rt"); |
|
|
|
auto index = rt->getNextNode(); |
|
|
|
index->setName("index"); |
|
|
|
auto jmpBuf = index->getNextNode(); |
|
|
|
jmpBuf->setName("jmpBuf"); |
|
|
|
|
|
|
|
InsertPointGuard guard{m_builder}; |
|
|
|
auto entryBB = llvm::BasicBlock::Create(m_builder.getContext(), {}, func); |
|
|
|
auto underflowBB = llvm::BasicBlock::Create(m_builder.getContext(), "Underflow", func); |
|
|
|
auto returnBB = llvm::BasicBlock::Create(m_builder.getContext(), "Return", func); |
|
|
|
|
|
|
|
m_builder.SetInsertPoint(entryBB); |
|
|
|
auto ok = createCall(extPopFunc, {rt, index}); |
|
|
|
m_builder.CreateCondBr(ok, returnBB, underflowBB); //TODO: Add branch weight
|
|
|
|
|
|
|
|
m_builder.SetInsertPoint(underflowBB); |
|
|
|
m_runtimeManager.abort(jmpBuf); |
|
|
|
m_builder.CreateUnreachable(); |
|
|
|
|
|
|
|
m_builder.SetInsertPoint(returnBB); |
|
|
|
m_builder.CreateRetVoid(); |
|
|
|
} |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Function* Stack::getGetFunc() |
|
|
|
{ |
|
|
|
auto& func = m_get; |
|
|
|