Browse Source

Decrement global gas value by instruction step cost

[#79942174]
cl-refactor
Paweł Bylica 10 years ago
parent
commit
28d6dd7930
  1. 4
      evmcc/Compiler.cpp
  2. 22
      evmcc/GasMeter.cpp
  3. 17
      evmcc/GasMeter.h

4
evmcc/Compiler.cpp

@ -8,6 +8,7 @@
#include "Memory.h"
#include "Ext.h"
#include "GasMeter.h"
namespace evmcc
{
@ -182,6 +183,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
// Init runtime structures.
auto memory = Memory(builder, module.get());
auto ext = Ext(builder, module.get());
GasMeter gasMeter(builder, module.get());
// Jump to first instruction
builder.CreateBr(basicBlocks.begin()->second);
@ -195,6 +197,8 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
for (auto currentPC = basicBlock.begin(); currentPC != basicBlock.end(); ++currentPC)
{
auto inst = static_cast<Instruction>(bytecode[currentPC]);
gasMeter.check(inst);
switch (inst)
{

22
evmcc/GasMeter.cpp

@ -1,6 +1,9 @@
#include "GasMeter.h"
#include <llvm/IR/GlobalVariable.h>
#include <llvm/IR/Function.h>
#include <libevmface/Instruction.h>
#include <libevm/FeeStructure.h>
@ -9,7 +12,7 @@ namespace evmcc
using namespace dev::eth; // We should move all the JIT code into dev::eth namespace
namespace
namespace // Helper functions
{
uint64_t getStepCost(dev::eth::Instruction inst) // TODO: Add this function to FeeSructure
@ -46,4 +49,21 @@ uint64_t getStepCost(dev::eth::Instruction inst) // TODO: Add this function to F
}
GasMeter::GasMeter(llvm::IRBuilder<>& _builder, llvm::Module* _module):
m_builder(_builder)
{
m_gas = new llvm::GlobalVariable(*_module, m_builder.getIntNTy(256), false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(m_builder.getIntNTy(256)), "gas");
m_gas->setUnnamedAddr(true); // Address is not important
//llvm::Function::Create()
}
void GasMeter::check(Instruction _inst)
{
auto stepCost = getStepCost(_inst);
auto before = m_builder.CreateLoad(m_gas, "gas.before");
auto after = m_builder.CreateSub(before, m_builder.getIntN(256, stepCost));
m_builder.CreateStore(after, m_gas);
}
}

17
evmcc/GasMeter.h

@ -1,9 +1,26 @@
#pragma once
#include <llvm/IR/IRBuilder.h>
#include <libevmface/Instruction.h>
namespace evmcc
{
class GasMeter
{
public:
GasMeter(llvm::IRBuilder<>& _builder, llvm::Module* module);
GasMeter(const GasMeter&) = delete;
void operator=(GasMeter) = delete;
void check(dev::eth::Instruction _inst);
private:
llvm::IRBuilder<>& m_builder;
llvm::GlobalVariable* m_gas;
};
}
Loading…
Cancel
Save