Browse Source

Use mem.require in mload, mstore & mstore8

cl-refactor
Paweł Bylica 10 years ago
parent
commit
07882137e3
  1. 50
      evmcc/Memory.cpp

50
evmcc/Memory.cpp

@ -40,11 +40,10 @@ Memory::Memory(llvm::IRBuilder<>& _builder, llvm::Module* _module, GasMeter& _ga
m_returnDataSize->setUnnamedAddr(true); // Address is not important
m_resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, Type::WordPtr, false), llvm::Function::ExternalLinkage, "mem_resize", _module);
m_require = createRequireFunc(_module, _gasMeter);
m_loadWord = createFunc(false, Type::i256, _module, _gasMeter);
m_storeWord = createFunc(true, Type::i256, _module, _gasMeter);
m_storeByte = createFunc(true, Type::Byte, _module, _gasMeter);
m_require = createRequireFunc(_module, _gasMeter);
}
llvm::Function* Memory::createRequireFunc(llvm::Module* _module, GasMeter& _gasMeter)
@ -91,51 +90,34 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, llvm::
auto funcType = _isStore ? llvm::FunctionType::get(Type::Void, storeArgs, false) : llvm::FunctionType::get(Type::i256, Type::i256, false);
auto func = llvm::Function::Create(funcType, llvm::Function::PrivateLinkage, name, _module);
auto checkBB = llvm::BasicBlock::Create(func->getContext(), "check", func);
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "resize", func);
auto accessBB = llvm::BasicBlock::Create(func->getContext(), "access", func);
auto origBB = m_builder.GetInsertBlock();
auto origPt = m_builder.GetInsertPoint();
// BB "check"
llvm::IRBuilder<> builder(checkBB);
m_builder.SetInsertPoint(llvm::BasicBlock::Create(func->getContext(), {}, func));
llvm::Value* index = func->arg_begin();
index->setName("index");
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8;
auto sizeRequired = builder.CreateAdd(index, Constant::get(valueSize), "sizeRequired");
auto size = builder.CreateLoad(m_size, "size");
auto resizeNeeded = builder.CreateICmpULE(size, sizeRequired, "resizeNeeded");
builder.CreateCondBr(resizeNeeded, resizeBB, accessBB); // OPT branch weights?
// BB "resize"
builder.SetInsertPoint(resizeBB);
// Check gas first
auto wordsRequired = builder.CreateUDiv(builder.CreateAdd(sizeRequired, Constant::get(31)), Constant::get(32), "wordsRequired");
auto words = builder.CreateUDiv(builder.CreateAdd(size, Constant::get(31)), Constant::get(32), "words");
auto newWords = builder.CreateSub(wordsRequired, words, "addtionalWords");
_gasMeter.checkMemory(newWords, builder);
// Resize
builder.CreateStore(sizeRequired, m_size);
auto newData = builder.CreateCall(m_resize, m_size, "newData");
builder.CreateStore(newData, m_data);
builder.CreateBr(accessBB);
// BB "access"
builder.SetInsertPoint(accessBB);
auto data = builder.CreateLoad(m_data, "data");
auto ptr = builder.CreateGEP(data, index, "ptr");
this->require(index, Constant::get(valueSize));
auto data = m_builder.CreateLoad(m_data, "data");
auto ptr = m_builder.CreateGEP(data, index, "ptr");
if (isWord)
ptr = builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr");
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr");
if (_isStore)
{
llvm::Value* value = ++func->arg_begin();
value->setName("value");
builder.CreateStore(value, ptr);
builder.CreateRetVoid();
m_builder.CreateStore(value, ptr);
m_builder.CreateRetVoid();
}
else
{
auto ret = builder.CreateLoad(ptr);
builder.CreateRet(ret);
auto ret = m_builder.CreateLoad(ptr);
m_builder.CreateRet(ret);
}
m_builder.SetInsertPoint(origBB, origPt);
return func;
}

Loading…
Cancel
Save