|
|
@ -26,23 +26,15 @@ namespace jit |
|
|
|
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): |
|
|
|
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
|
|
|
|
m_gasMeter(_gasMeter) |
|
|
|
{ |
|
|
|
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr}; |
|
|
|
m_resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule()); |
|
|
|
llvm::AttrBuilder attrBuilder; |
|
|
|
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly); |
|
|
|
m_resize->setAttributes(llvm::AttributeSet::get(m_resize->getContext(), 1, attrBuilder)); |
|
|
|
|
|
|
|
m_require = createRequireFunc(_gasMeter); |
|
|
|
m_loadWord = createFunc(false, Type::Word, _gasMeter); |
|
|
|
m_storeWord = createFunc(true, Type::Word, _gasMeter); |
|
|
|
m_storeByte = createFunc(true, Type::Byte, _gasMeter); |
|
|
|
} |
|
|
|
{} |
|
|
|
|
|
|
|
llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) |
|
|
|
llvm::Function* Memory::getRequireFunc() |
|
|
|
{ |
|
|
|
auto& func = m_require; |
|
|
|
if (!func) |
|
|
|
{ |
|
|
|
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word}; |
|
|
|
auto func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule()); |
|
|
|
func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule()); |
|
|
|
auto rt = func->arg_begin(); |
|
|
|
rt->setName("rt"); |
|
|
|
auto offset = rt->getNextNode(); |
|
|
@ -50,6 +42,12 @@ llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) |
|
|
|
auto size = offset->getNextNode(); |
|
|
|
size->setName("size"); |
|
|
|
|
|
|
|
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr}; |
|
|
|
auto resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule()); |
|
|
|
llvm::AttrBuilder attrBuilder; |
|
|
|
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly); |
|
|
|
resize->setAttributes(llvm::AttributeSet::get(resize->getContext(), 1, attrBuilder)); |
|
|
|
|
|
|
|
auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func); |
|
|
|
auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func); |
|
|
|
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func); |
|
|
@ -87,10 +85,10 @@ llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) |
|
|
|
sizeRequired = m_builder.CreateMul(wordsRequired, Constant::get(32), "roundedSizeReq"); |
|
|
|
auto words = m_builder.CreateUDiv(currSize, Constant::get(32), "words"); // size is always 32*k
|
|
|
|
auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords"); |
|
|
|
_gasMeter.countMemory(newWords); |
|
|
|
m_gasMeter.countMemory(newWords); |
|
|
|
// Resize
|
|
|
|
m_builder.CreateStore(sizeRequired, sizePtr); |
|
|
|
auto newData = m_builder.CreateCall2(m_resize, rt, sizePtr, "newData"); |
|
|
|
auto newData = m_builder.CreateCall2(resize, rt, sizePtr, "newData"); |
|
|
|
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|
|
|
m_builder.CreateStore(newData, dataPtr); |
|
|
|
m_builder.CreateBr(returnBB); |
|
|
@ -98,6 +96,7 @@ llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) |
|
|
|
// BB "Return"
|
|
|
|
m_builder.SetInsertPoint(returnBB); |
|
|
|
m_builder.CreateRetVoid(); |
|
|
|
} |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
@ -143,21 +142,45 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMet |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Function* Memory::getLoadWordFunc() |
|
|
|
{ |
|
|
|
auto& func = m_loadWord; |
|
|
|
if (!func) |
|
|
|
func = createFunc(false, Type::Word, m_gasMeter); |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Function* Memory::getStoreWordFunc() |
|
|
|
{ |
|
|
|
auto& func = m_storeWord; |
|
|
|
if (!func) |
|
|
|
func = createFunc(true, Type::Word, m_gasMeter); |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Function* Memory::getStoreByteFunc() |
|
|
|
{ |
|
|
|
auto& func = m_storeByte; |
|
|
|
if (!func) |
|
|
|
func = createFunc(true, Type::Byte, m_gasMeter); |
|
|
|
return func; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
llvm::Value* Memory::loadWord(llvm::Value* _addr) |
|
|
|
{ |
|
|
|
return createCall(m_loadWord, {getRuntimeManager().getRuntimePtr(), _addr}); |
|
|
|
return createCall(getLoadWordFunc(), {getRuntimeManager().getRuntimePtr(), _addr}); |
|
|
|
} |
|
|
|
|
|
|
|
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) |
|
|
|
{ |
|
|
|
createCall(m_storeWord, {getRuntimeManager().getRuntimePtr(), _addr, _word}); |
|
|
|
createCall(getStoreWordFunc(), {getRuntimeManager().getRuntimePtr(), _addr, _word}); |
|
|
|
} |
|
|
|
|
|
|
|
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) |
|
|
|
{ |
|
|
|
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); |
|
|
|
createCall(m_storeByte, {getRuntimeManager().getRuntimePtr(), _addr, byte}); |
|
|
|
createCall(getStoreByteFunc(), {getRuntimeManager().getRuntimePtr(), _addr, byte}); |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Value* Memory::getData() |
|
|
@ -187,7 +210,7 @@ void Memory::require(llvm::Value* _offset, llvm::Value* _size) |
|
|
|
if (!constant->getValue()) |
|
|
|
return; |
|
|
|
} |
|
|
|
createCall(m_require, {getRuntimeManager().getRuntimePtr(), _offset, _size}); |
|
|
|
createCall(getRequireFunc(), {getRuntimeManager().getRuntimePtr(), _offset, _size}); |
|
|
|
} |
|
|
|
|
|
|
|
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, |
|
|
|