Browse Source

Use external counter for gas (external linkage global variable)

[#79942174]
cl-refactor
Paweł Bylica 10 years ago
parent
commit
f6eef07d37
  1. 4
      evmcc/ExecutionEngine.cpp
  2. 6
      evmcc/Ext.cpp
  3. 2
      evmcc/GasMeter.cpp
  4. 6
      evmcc/Memory.cpp
  5. 12
      evmcc/Runtime.cpp
  6. 10
      evmcc/Runtime.h

4
evmcc/ExecutionEngine.cpp

@ -85,7 +85,8 @@ int ExecutionEngine::run(std::unique_ptr<llvm::Module> _module)
ext->data = calldata;
// Init runtime
Runtime runtime(std::move(ext));
uint64_t gas = 1000000;
Runtime runtime(gas, std::move(ext));
auto entryFunc = module->getFunction("main");
if (!entryFunc)
@ -95,6 +96,7 @@ int ExecutionEngine::run(std::unique_ptr<llvm::Module> _module)
}
auto result = exec->runFunction(entryFunc, {});
gas = static_cast<decltype(gas)>(Runtime::getGas());
if (auto intResult = result.IntVal.getZExtValue())
{
auto index = intResult >> 32;

6
evmcc/Ext.cpp

@ -9,12 +9,6 @@
#include "Runtime.h"
#ifdef _MSC_VER
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
using namespace llvm;
using llvm::types::i;
using Linkage = llvm::GlobalValue::LinkageTypes;

2
evmcc/GasMeter.cpp

@ -81,7 +81,7 @@ bool isCostBlockEnd(Instruction _inst)
GasMeter::GasMeter(llvm::IRBuilder<>& _builder, llvm::Module* _module):
m_builder(_builder)
{
m_gas = new llvm::GlobalVariable(*_module, Type::i256, false, llvm::GlobalVariable::PrivateLinkage, llvm::UndefValue::get(Type::i256), "gas");
m_gas = new llvm::GlobalVariable(*_module, Type::i256, false, llvm::GlobalVariable::ExternalLinkage, nullptr, "gas");
m_gas->setUnnamedAddr(true); // Address is not important
auto pt = m_builder.GetInsertPoint();

6
evmcc/Memory.cpp

@ -15,12 +15,6 @@
#include "Runtime.h"
#include "GasMeter.h"
#ifdef _MSC_VER
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
namespace evmcc
{

12
evmcc/Runtime.cpp

@ -6,11 +6,14 @@ namespace evmcc
static Runtime* g_runtime;
Runtime::Runtime(std::unique_ptr<dev::eth::ExtVMFace> _ext)
: m_ext(std::move(_ext))
extern "C" { EXPORT i256 gas; }
Runtime::Runtime(dev::u256 _gas, std::unique_ptr<dev::eth::ExtVMFace> _ext):
m_ext(std::move(_ext))
{
assert(!g_runtime);
g_runtime = this;
gas = eth2llvm(_gas);
}
Runtime::~Runtime()
@ -33,4 +36,9 @@ dev::eth::ExtVMFace& Runtime::getExt()
return *g_runtime->m_ext;
}
dev::u256 Runtime::getGas()
{
return llvm2eth(gas);
}
}

10
evmcc/Runtime.h

@ -7,6 +7,13 @@
#include "Utils.h"
#ifdef _MSC_VER
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
namespace evmcc
{
@ -16,7 +23,7 @@ using MemoryImpl = dev::bytes;
class Runtime
{
public:
Runtime(std::unique_ptr<dev::eth::ExtVMFace> _ext);
Runtime(dev::u256 _gas, std::unique_ptr<dev::eth::ExtVMFace> _ext);
~Runtime();
Runtime(const Runtime&) = delete;
@ -25,6 +32,7 @@ public:
static StackImpl& getStack();
static MemoryImpl& getMemory();
static dev::eth::ExtVMFace& getExt();
static dev::u256 getGas();
private:
StackImpl m_stack;

Loading…
Cancel
Save