Browse Source

Changing Runtime interface

cl-refactor
Paweł Bylica 10 years ago
parent
commit
0509b3bddd
  1. 70
      libevmjit-cpp/Runtime.cpp
  2. 64
      libevmjit-cpp/Runtime.h
  3. 38
      libevmjit/Runtime.cpp
  4. 23
      libevmjit/Runtime.h
  5. 5
      libevmjit/RuntimeData.h

70
libevmjit-cpp/Runtime.cpp

@ -0,0 +1,70 @@
#include "Runtime.h"
#include <llvm/IR/GlobalVariable.h>
#include <llvm/IR/Function.h>
#include <llvm/IR/IntrinsicInst.h>
//#include <libevm/VM.h>
namespace dev
{
namespace eth
{
namespace jit
{
Runtime::Runtime(u256 _gas, ExtVMFace& _ext, jmp_buf _jmpBuf, bool _outputLogs):
m_ext(_ext),
m_outputLogs(_outputLogs)
{
set(RuntimeData::Gas, _gas);
set(RuntimeData::Address, fromAddress(_ext.myAddress));
set(RuntimeData::Caller, fromAddress(_ext.caller));
set(RuntimeData::Origin, fromAddress(_ext.origin));
set(RuntimeData::CallValue, _ext.value);
set(RuntimeData::CallDataSize, _ext.data.size());
set(RuntimeData::GasPrice, _ext.gasPrice);
set(RuntimeData::PrevHash, _ext.previousBlock.hash);
set(RuntimeData::CoinBase, fromAddress(_ext.currentBlock.coinbaseAddress));
set(RuntimeData::TimeStamp, _ext.currentBlock.timestamp);
set(RuntimeData::Number, _ext.currentBlock.number);
set(RuntimeData::Difficulty, _ext.currentBlock.difficulty);
set(RuntimeData::GasLimit, _ext.currentBlock.gasLimit);
set(RuntimeData::CodeSize, _ext.code.size()); // TODO: Use constant
m_data.callData = _ext.data.data();
m_data.code = _ext.code.data();
m_data.jmpBuf = _jmpBuf;
}
void Runtime::set(RuntimeData::Index _index, u256 _value)
{
m_data.elems[_index] = eth2llvm(_value);
}
u256 Runtime::getGas() const
{
return llvm2eth(m_data.elems[RuntimeData::Gas]);
}
bytes Runtime::getReturnData() const // FIXME: Reconsider returning by copy
{
// TODO: Handle large indexes
auto offset = static_cast<size_t>(llvm2eth(m_data.elems[RuntimeData::ReturnDataOffset]));
auto size = static_cast<size_t>(llvm2eth(m_data.elems[RuntimeData::ReturnDataSize]));
assert(offset + size <= m_memory.size());
// TODO: Handle invalid data access by returning empty ref
auto dataBeg = m_memory.begin() + offset;
return {dataBeg, dataBeg + size};
}
bool Runtime::outputLogs() const
{
return m_outputLogs;
}
}
}
}

64
libevmjit-cpp/Runtime.h

@ -0,0 +1,64 @@
#pragma once
#include <vector>
#include <csetjmp>
//#include <libevm/ExtVMFace.h>
#include "Instruction.h"
#include "CompilerHelper.h"
#include "Utils.h"
#include "Type.h"
#include "RuntimeData.h"
#ifdef _MSC_VER
#define EXPORT __declspec(dllexport)
#else
#define EXPORT
#endif
namespace dev
{
namespace eth
{
namespace jit
{
using StackImpl = std::vector<i256>;
using MemoryImpl = bytes;
class Runtime
{
public:
Runtime(u256 _gas, ExtVMFace& _ext, jmp_buf _jmpBuf, bool _outputLogs);
Runtime(const Runtime&) = delete;
void operator=(const Runtime&) = delete;
RuntimeData* getDataPtr() { return &m_data; }
StackImpl& getStack() { return m_stack; }
MemoryImpl& getMemory() { return m_memory; }
ExtVMFace& getExt() { return m_ext; }
u256 getGas() const;
bytes getReturnData() const;
decltype(&jmp_buf{}[0]) getJmpBuf() { return m_data.jmpBuf; }
bool outputLogs() const;
private:
void set(RuntimeData::Index _index, u256 _value);
/// @internal Must be the first element to asure Runtime* === RuntimeData*
RuntimeData m_data;
StackImpl m_stack;
MemoryImpl m_memory;
ExtVMFace& m_ext;
bool m_outputLogs; ///< write LOG statements to console
};
}
}
}

38
libevmjit/Runtime.cpp

@ -14,33 +14,11 @@ namespace eth
namespace jit
{
Runtime::Runtime(u256 _gas, ExtVMFace& _ext, jmp_buf _jmpBuf, bool _outputLogs):
m_ext(_ext),
m_outputLogs(_outputLogs)
{
set(RuntimeData::Gas, _gas);
set(RuntimeData::Address, fromAddress(_ext.myAddress));
set(RuntimeData::Caller, fromAddress(_ext.caller));
set(RuntimeData::Origin, fromAddress(_ext.origin));
set(RuntimeData::CallValue, _ext.value);
set(RuntimeData::CallDataSize, _ext.data.size());
set(RuntimeData::GasPrice, _ext.gasPrice);
set(RuntimeData::PrevHash, _ext.previousBlock.hash);
set(RuntimeData::CoinBase, fromAddress(_ext.currentBlock.coinbaseAddress));
set(RuntimeData::TimeStamp, _ext.currentBlock.timestamp);
set(RuntimeData::Number, _ext.currentBlock.number);
set(RuntimeData::Difficulty, _ext.currentBlock.difficulty);
set(RuntimeData::GasLimit, _ext.currentBlock.gasLimit);
set(RuntimeData::CodeSize, _ext.code.size()); // TODO: Use constant
m_data.callData = _ext.data.data();
m_data.code = _ext.code.data();
m_data.jmpBuf = _jmpBuf;
}
void Runtime::set(RuntimeData::Index _index, u256 _value)
{
m_data.elems[_index] = eth2llvm(_value);
}
Runtime::Runtime(RuntimeData* _data, Env* _env, JmpBufRef _jmpBuf):
m_data(*_data),
m_env(*_env),
m_jmpBuf(_jmpBuf)
{}
u256 Runtime::getGas() const
{
@ -59,12 +37,6 @@ bytes Runtime::getReturnData() const // FIXME: Reconsider returning by copy
return {dataBeg, dataBeg + size};
}
bool Runtime::outputLogs() const
{
return m_outputLogs;
}
}
}
}

23
libevmjit/Runtime.h

@ -2,9 +2,6 @@
#pragma once
#include <vector>
#include <csetjmp>
//#include <libevm/ExtVMFace.h>
#include "Instruction.h"
#include "CompilerHelper.h"
@ -28,11 +25,15 @@ namespace jit
using StackImpl = std::vector<i256>;
using MemoryImpl = bytes;
using JmpBufRef = decltype(&jmp_buf{}[0]);
/// VM Environment (ExtVM) opaque type
struct Env;
class Runtime
{
public:
Runtime(u256 _gas, ExtVMFace& _ext, jmp_buf _jmpBuf, bool _outputLogs);
Runtime(RuntimeData* _data, Env* _env, JmpBufRef _jmpBuf);
Runtime(const Runtime&) = delete;
void operator=(const Runtime&) = delete;
@ -41,22 +42,18 @@ public:
StackImpl& getStack() { return m_stack; }
MemoryImpl& getMemory() { return m_memory; }
ExtVMFace& getExt() { return m_ext; }
Env* getEnvPtr() { return &m_env; }
u256 getGas() const;
bytes getReturnData() const;
decltype(&jmp_buf{}[0]) getJmpBuf() { return m_data.jmpBuf; }
bool outputLogs() const;
JmpBufRef getJmpBuf() { return m_jmpBuf; }
private:
void set(RuntimeData::Index _index, u256 _value);
/// @internal Must be the first element to asure Runtime* === RuntimeData*
RuntimeData m_data;
RuntimeData& m_data;
Env& m_env;
JmpBufRef m_jmpBuf;
StackImpl m_stack;
MemoryImpl m_memory;
ExtVMFace& m_ext;
bool m_outputLogs; ///< write LOG statements to console
};
}

5
libevmjit/RuntimeData.h

@ -12,9 +12,7 @@ namespace eth
{
namespace jit
{
using jmpBufRef = decltype(&jmp_buf{}[0]);
struct RuntimeData
{
enum Index
@ -43,7 +41,6 @@ struct RuntimeData
i256 elems[_size];
byte const* callData;
byte const* code;
jmpBufRef jmpBuf;
};
}

Loading…
Cancel
Save