Browse Source

Use Constant::get() interface to create LLVM constants

cl-refactor
Paweł Bylica 10 years ago
parent
commit
7494fdba7a
  1. 12
      evmcc/Compiler.cpp
  2. 4
      evmcc/GasMeter.cpp
  3. 8
      evmcc/Memory.cpp

12
evmcc/Compiler.cpp

@ -420,12 +420,12 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
// TODO: Shifting by 0 gives wrong results as of this bug http://llvm.org/bugs/show_bug.cgi?id=16439
auto shbits = builder.CreateShl(byteNum, builder.getIntN(256, 3));
auto shbits = builder.CreateShl(byteNum, Constant::get(3));
value = builder.CreateShl(value, shbits);
value = builder.CreateLShr(value, builder.getIntN(256, 31 * 8));
value = builder.CreateLShr(value, Constant::get(31 * 8));
auto byteNumValid = builder.CreateICmpULT(byteNum, builder.getIntN(256, 32));
value = builder.CreateSelect(byteNumValid, value, builder.getIntN(256, 0));
auto byteNumValid = builder.CreateICmpULT(byteNum, Constant::get(32));
value = builder.CreateSelect(byteNumValid, value, Constant::get(0));
stack.push(value);
break;
@ -651,7 +651,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
case Instruction::PC:
{
auto value = builder.getIntN(256, currentPC);
auto value = Constant::get(currentPC);
stack.push(value);
break;
}
@ -716,7 +716,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
case Instruction::CODESIZE:
{
auto value = builder.getIntN(256, bytecode.size());
auto value = Constant::get(bytecode.size());
stack.push(value);
break;
}

4
evmcc/GasMeter.cpp

@ -113,7 +113,7 @@ void GasMeter::commitCostBlock()
if (m_checkCall)
{
if (m_blockCost > 0) // If any cost
m_checkCall->setArgOperand(0, m_builder.getIntN(256, m_blockCost)); // Update block cost in gas check call
m_checkCall->setArgOperand(0, Constant::get(m_blockCost)); // Update block cost in gas check call
else
m_checkCall->eraseFromParent(); // Remove the gas check call
@ -126,7 +126,7 @@ void GasMeter::commitCostBlock()
void GasMeter::checkMemory(llvm::Value* _additionalMemoryInWords, llvm::IRBuilder<>& _builder)
{
// Memory uses other builder, but that can be changes later
auto cost = _builder.CreateMul(_additionalMemoryInWords, _builder.getIntN(256, static_cast<uint64_t>(c_memoryGas)), "memcost");
auto cost = _builder.CreateMul(_additionalMemoryInWords, Constant::get(static_cast<uint64_t>(c_memoryGas)), "memcost");
_builder.CreateCall(m_gasCheckFunc, cost);
}

8
evmcc/Memory.cpp

@ -30,7 +30,7 @@ Memory::Memory(llvm::IRBuilder<>& _builder, llvm::Module* _module, GasMeter& _ga
m_data = new llvm::GlobalVariable(*_module, Type::BytePtr, false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(Type::BytePtr), "mem.data");
m_data->setUnnamedAddr(true); // Address is not important
m_size = new llvm::GlobalVariable(*_module, Type::i256, false, llvm::GlobalVariable::PrivateLinkage, m_builder.getIntN(256, 0), "mem.size");
m_size = new llvm::GlobalVariable(*_module, Type::i256, false, llvm::GlobalVariable::PrivateLinkage, Constant::get(0), "mem.size");
m_size->setUnnamedAddr(true); // Address is not important
m_returnDataOffset = new llvm::GlobalVariable(*_module, Type::i256, false, llvm::GlobalVariable::ExternalLinkage, nullptr, "mem_returnDataOffset");
@ -63,7 +63,7 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, llvm::
llvm::Value* index = func->arg_begin();
index->setName("index");
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8;
auto sizeRequired = builder.CreateAdd(index, builder.getIntN(256, valueSize), "sizeRequired");
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?
@ -71,8 +71,8 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, llvm::
// BB "resize"
builder.SetInsertPoint(resizeBB);
// Check gas first
auto wordsRequired = builder.CreateUDiv(builder.CreateAdd(sizeRequired, builder.getIntN(256, 31)), builder.getIntN(256, 32), "wordsRequired");
auto words = builder.CreateUDiv(builder.CreateAdd(size, builder.getIntN(256, 31)), builder.getIntN(256, 32), "words");
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

Loading…
Cancel
Save