mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 04:06:44 +00:00
parent
d3f59f6de4
commit
28d6dd7930
3 changed files with 42 additions and 1 deletions
|
|
@ -8,6 +8,7 @@
|
||||||
|
|
||||||
#include "Memory.h"
|
#include "Memory.h"
|
||||||
#include "Ext.h"
|
#include "Ext.h"
|
||||||
|
#include "GasMeter.h"
|
||||||
|
|
||||||
namespace evmcc
|
namespace evmcc
|
||||||
{
|
{
|
||||||
|
|
@ -182,6 +183,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(const dev::bytes& bytecode)
|
||||||
// Init runtime structures.
|
// Init runtime structures.
|
||||||
auto memory = Memory(builder, module.get());
|
auto memory = Memory(builder, module.get());
|
||||||
auto ext = Ext(builder, module.get());
|
auto ext = Ext(builder, module.get());
|
||||||
|
GasMeter gasMeter(builder, module.get());
|
||||||
|
|
||||||
// Jump to first instruction
|
// Jump to first instruction
|
||||||
builder.CreateBr(basicBlocks.begin()->second);
|
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)
|
for (auto currentPC = basicBlock.begin(); currentPC != basicBlock.end(); ++currentPC)
|
||||||
{
|
{
|
||||||
auto inst = static_cast<Instruction>(bytecode[currentPC]);
|
auto inst = static_cast<Instruction>(bytecode[currentPC]);
|
||||||
|
gasMeter.check(inst);
|
||||||
|
|
||||||
switch (inst)
|
switch (inst)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
|
|
||||||
#include "GasMeter.h"
|
#include "GasMeter.h"
|
||||||
|
|
||||||
|
#include <llvm/IR/GlobalVariable.h>
|
||||||
|
#include <llvm/IR/Function.h>
|
||||||
|
|
||||||
#include <libevmface/Instruction.h>
|
#include <libevmface/Instruction.h>
|
||||||
#include <libevm/FeeStructure.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
|
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
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
@ -1,9 +1,26 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <llvm/IR/IRBuilder.h>
|
||||||
|
|
||||||
#include <libevmface/Instruction.h>
|
#include <libevmface/Instruction.h>
|
||||||
|
|
||||||
namespace evmcc
|
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…
Reference in a new issue