|
|
@ -9,6 +9,7 @@ |
|
|
|
|
|
|
|
#include "Type.h" |
|
|
|
#include "Utils.h" |
|
|
|
#include "Ext.h" |
|
|
|
|
|
|
|
namespace evmcc |
|
|
|
{ |
|
|
@ -100,12 +101,34 @@ void GasMeter::count(Instruction _inst) |
|
|
|
m_checkCall = m_builder.CreateCall(m_gasCheckFunc, llvm::UndefValue::get(Type::i256)); |
|
|
|
} |
|
|
|
|
|
|
|
m_blockCost += getStepCost(_inst); |
|
|
|
if (_inst != Instruction::SSTORE) // Handle cost of SSTORE separately in countSStore()
|
|
|
|
m_blockCost += getStepCost(_inst); |
|
|
|
|
|
|
|
if (isCostBlockEnd(_inst)) |
|
|
|
commitCostBlock(); |
|
|
|
} |
|
|
|
|
|
|
|
void GasMeter::countSStore(Ext& _ext, llvm::Value* _index, llvm::Value* _newValue) |
|
|
|
{ |
|
|
|
assert(!m_checkCall); // Everything should've been commited before
|
|
|
|
|
|
|
|
static const auto sstoreCost = static_cast<uint64_t>(c_sstoreGas); |
|
|
|
|
|
|
|
// [ADD] if oldValue == 0 and newValue != 0 => 2*cost
|
|
|
|
// [DEL] if oldValue != 0 and newValue == 0 => 0
|
|
|
|
|
|
|
|
auto oldValue = _ext.store(_index); |
|
|
|
auto oldValueIsZero = m_builder.CreateICmpEQ(oldValue, Constant::get(0), "oldValueIsZero"); |
|
|
|
auto newValueIsZero = m_builder.CreateICmpEQ(_newValue, Constant::get(0), "newValueIsZero"); |
|
|
|
auto oldValueIsntZero = m_builder.CreateICmpNE(oldValue, Constant::get(0), "oldValueIsntZero"); |
|
|
|
auto newValueIsntZero = m_builder.CreateICmpNE(_newValue, Constant::get(0), "newValueIsntZero"); |
|
|
|
auto isAdd = m_builder.CreateAnd(oldValueIsZero, newValueIsntZero, "isAdd"); |
|
|
|
auto isDel = m_builder.CreateAnd(oldValueIsntZero, newValueIsZero, "isDel"); |
|
|
|
auto cost = m_builder.CreateSelect(isAdd, Constant::get(2 * sstoreCost), Constant::get(sstoreCost), "cost"); |
|
|
|
cost = m_builder.CreateSelect(isDel, Constant::get(0), cost, "cost"); |
|
|
|
m_builder.CreateCall(m_gasCheckFunc, cost); |
|
|
|
} |
|
|
|
|
|
|
|
void GasMeter::giveBack(llvm::Value* _gas) |
|
|
|
{ |
|
|
|
llvm::Value* gasCounter = m_builder.CreateLoad(m_gas, "gas"); |
|
|
|