5 changed files with 150 additions and 50 deletions
@ -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; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
@ -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
|
||||
|
}; |
||||
|
|
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue