mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-07-21 20:26:41 +00:00
Moving ADDRESS data from Ext to Runtime [#81470252]
This commit is contained in:
parent
2b898fc06a
commit
399cf2845e
5 changed files with 46 additions and 25 deletions
|
|
@ -180,7 +180,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytesConstRef bytecode)
|
||||||
RuntimeManager runtimeManager(m_builder);
|
RuntimeManager runtimeManager(m_builder);
|
||||||
GasMeter gasMeter(m_builder, runtimeManager);
|
GasMeter gasMeter(m_builder, runtimeManager);
|
||||||
Memory memory(m_builder, gasMeter, runtimeManager);
|
Memory memory(m_builder, gasMeter, runtimeManager);
|
||||||
Ext ext(m_builder);
|
Ext ext(runtimeManager);
|
||||||
Stack stack(m_builder, runtimeManager);
|
Stack stack(m_builder, runtimeManager);
|
||||||
|
|
||||||
m_builder.CreateBr(basicBlocks.begin()->second);
|
m_builder.CreateBr(basicBlocks.begin()->second);
|
||||||
|
|
@ -686,7 +686,7 @@ void Compiler::compileBasicBlock(BasicBlock& basicBlock, bytesConstRef bytecode,
|
||||||
|
|
||||||
case Instruction::ADDRESS:
|
case Instruction::ADDRESS:
|
||||||
{
|
{
|
||||||
auto value = ext.address();
|
auto value = _runtimeManager.get(RuntimeData::Address);
|
||||||
stack.push(value);
|
stack.push(value);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
@ -873,7 +873,7 @@ void Compiler::compileBasicBlock(BasicBlock& basicBlock, bytesConstRef bytecode,
|
||||||
|
|
||||||
auto receiveAddress = codeAddress;
|
auto receiveAddress = codeAddress;
|
||||||
if (inst == Instruction::CALLCODE)
|
if (inst == Instruction::CALLCODE)
|
||||||
receiveAddress = ext.address();
|
receiveAddress = _runtimeManager.get(RuntimeData::Address);
|
||||||
|
|
||||||
auto ret = ext.call(gas, receiveAddress, value, inOff, inSize, outOff, outSize, codeAddress);
|
auto ret = ext.call(gas, receiveAddress, value, inOff, inSize, outOff, outSize, codeAddress);
|
||||||
gasMeter.giveBack(gas);
|
gasMeter.giveBack(gas);
|
||||||
|
|
|
||||||
|
|
@ -44,10 +44,10 @@ struct ExtData
|
||||||
const byte* code;
|
const byte* code;
|
||||||
};
|
};
|
||||||
|
|
||||||
Ext::Ext(llvm::IRBuilder<>& _builder):
|
Ext::Ext(RuntimeManager& _runtimeManager):
|
||||||
CompilerHelper(_builder)
|
RuntimeHelper(_runtimeManager)
|
||||||
{
|
{
|
||||||
auto&& ctx = _builder.getContext();
|
auto&& ctx = m_builder.getContext();
|
||||||
auto module = getModule();
|
auto module = getModule();
|
||||||
|
|
||||||
auto i256Ty = m_builder.getIntNTy(256);
|
auto i256Ty = m_builder.getIntNTy(256);
|
||||||
|
|
@ -126,7 +126,6 @@ Value* Ext::getDataElem(unsigned _index, const Twine& _name)
|
||||||
return m_builder.CreateLoad(valuePtr);
|
return m_builder.CreateLoad(valuePtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
Value* Ext::address() { return getDataElem(0, "address"); }
|
|
||||||
Value* Ext::caller() { return getDataElem(1, "caller"); }
|
Value* Ext::caller() { return getDataElem(1, "caller"); }
|
||||||
Value* Ext::origin() { return getDataElem(2, "origin"); }
|
Value* Ext::origin() { return getDataElem(2, "origin"); }
|
||||||
Value* Ext::callvalue() { return getDataElem(3, "callvalue"); }
|
Value* Ext::callvalue() { return getDataElem(3, "callvalue"); }
|
||||||
|
|
@ -243,7 +242,6 @@ using namespace dev::eth::jit;
|
||||||
EXPORT void ext_init(ExtData* _extData)
|
EXPORT void ext_init(ExtData* _extData)
|
||||||
{
|
{
|
||||||
auto&& ext = Runtime::getExt();
|
auto&& ext = Runtime::getExt();
|
||||||
_extData->address = eth2llvm(fromAddress(ext.myAddress));
|
|
||||||
_extData->caller = eth2llvm(fromAddress(ext.caller));
|
_extData->caller = eth2llvm(fromAddress(ext.caller));
|
||||||
_extData->origin = eth2llvm(fromAddress(ext.origin));
|
_extData->origin = eth2llvm(fromAddress(ext.origin));
|
||||||
_extData->callvalue = eth2llvm(ext.value);
|
_extData->callvalue = eth2llvm(ext.value);
|
||||||
|
|
|
||||||
|
|
@ -12,15 +12,14 @@ namespace eth
|
||||||
namespace jit
|
namespace jit
|
||||||
{
|
{
|
||||||
|
|
||||||
class Ext : public CompilerHelper
|
class Ext : public RuntimeHelper
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Ext(llvm::IRBuilder<>& _builder);
|
Ext(RuntimeManager& _runtimeManager);
|
||||||
|
|
||||||
llvm::Value* store(llvm::Value* _index);
|
llvm::Value* store(llvm::Value* _index);
|
||||||
void setStore(llvm::Value* _index, llvm::Value* _value);
|
void setStore(llvm::Value* _index, llvm::Value* _value);
|
||||||
|
|
||||||
llvm::Value* address();
|
|
||||||
llvm::Value* caller();
|
llvm::Value* caller();
|
||||||
llvm::Value* origin();
|
llvm::Value* origin();
|
||||||
llvm::Value* callvalue();
|
llvm::Value* callvalue();
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ llvm::StructType* RuntimeData::getType()
|
||||||
{
|
{
|
||||||
llvm::Type* elems[] =
|
llvm::Type* elems[] =
|
||||||
{
|
{
|
||||||
Type::i256,
|
llvm::ArrayType::get(Type::i256, _size)
|
||||||
};
|
};
|
||||||
type = llvm::StructType::create(elems, "RuntimeData");
|
type = llvm::StructType::create(elems, "RuntimeData");
|
||||||
}
|
}
|
||||||
|
|
@ -36,7 +36,8 @@ Runtime::Runtime(u256 _gas, ExtVMFace& _ext):
|
||||||
{
|
{
|
||||||
assert(!g_runtime);
|
assert(!g_runtime);
|
||||||
g_runtime = this;
|
g_runtime = this;
|
||||||
m_data.gas = eth2llvm(_gas);
|
set(RuntimeData::Gas, _gas);
|
||||||
|
set(RuntimeData::Address, fromAddress(_ext.myAddress));
|
||||||
}
|
}
|
||||||
|
|
||||||
Runtime::~Runtime()
|
Runtime::~Runtime()
|
||||||
|
|
@ -44,15 +45,20 @@ Runtime::~Runtime()
|
||||||
g_runtime = nullptr;
|
g_runtime = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Runtime::set(RuntimeData::Index _index, u256 _value)
|
||||||
|
{
|
||||||
|
m_data.elems[_index] = eth2llvm(_value);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
ExtVMFace& Runtime::getExt()
|
ExtVMFace& Runtime::getExt()
|
||||||
{
|
{
|
||||||
return g_runtime->m_ext;
|
return g_runtime->m_ext;
|
||||||
}
|
}
|
||||||
|
|
||||||
u256 Runtime::getGas()
|
u256 Runtime::getGas() const
|
||||||
{
|
{
|
||||||
return llvm2eth(m_data.gas);
|
return llvm2eth(m_data.elems[RuntimeData::Gas]);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
@ -60,12 +66,12 @@ extern "C" {
|
||||||
EXPORT i256 mem_returnDataSize;
|
EXPORT i256 mem_returnDataSize;
|
||||||
}
|
}
|
||||||
|
|
||||||
bytesConstRef Runtime::getReturnData()
|
bytesConstRef Runtime::getReturnData() const
|
||||||
{
|
{
|
||||||
// TODO: Handle large indexes
|
// TODO: Handle large indexes
|
||||||
auto offset = static_cast<size_t>(llvm2eth(mem_returnDataOffset));
|
auto offset = static_cast<size_t>(llvm2eth(mem_returnDataOffset));
|
||||||
auto size = static_cast<size_t>(llvm2eth(mem_returnDataSize));
|
auto size = static_cast<size_t>(llvm2eth(mem_returnDataSize));
|
||||||
return{getMemory().data() + offset, size};
|
return {m_memory.data() + offset, size};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -85,16 +91,23 @@ llvm::Value* RuntimeManager::getRuntimePtr()
|
||||||
return m_builder.CreateLoad(m_dataPtr);
|
return m_builder.CreateLoad(m_dataPtr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
llvm::Value* RuntimeManager::get(RuntimeData::Index _index)
|
||||||
|
{
|
||||||
|
llvm::Value* idxList[] = {m_builder.getInt32(0), m_builder.getInt32(0), m_builder.getInt32(_index)};
|
||||||
|
auto ptr = m_builder.CreateInBoundsGEP(getRuntimePtr(), idxList, "dataElemPtr");
|
||||||
|
return m_builder.CreateLoad(ptr);
|
||||||
|
}
|
||||||
|
|
||||||
llvm::Value* RuntimeManager::getGas()
|
llvm::Value* RuntimeManager::getGas()
|
||||||
{
|
{
|
||||||
auto gasPtr = m_builder.CreateStructGEP(getRuntimePtr(), 0);
|
return get(RuntimeData::Gas);
|
||||||
return m_builder.CreateLoad(gasPtr, "gas");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RuntimeManager::setGas(llvm::Value* _gas)
|
void RuntimeManager::setGas(llvm::Value* _gas)
|
||||||
{
|
{
|
||||||
auto gasPtr = m_builder.CreateStructGEP(getRuntimePtr(), 0);
|
llvm::Value* idxList[] = {m_builder.getInt32(0), m_builder.getInt32(0), m_builder.getInt32(RuntimeData::Gas)};
|
||||||
m_builder.CreateStore(_gas, gasPtr);
|
auto ptr = m_builder.CreateInBoundsGEP(getRuntimePtr(), idxList, "gasPtr");
|
||||||
|
m_builder.CreateStore(_gas, ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -24,9 +24,17 @@ namespace jit
|
||||||
|
|
||||||
struct RuntimeData
|
struct RuntimeData
|
||||||
{
|
{
|
||||||
static llvm::StructType* getType();
|
enum Index: unsigned
|
||||||
|
{
|
||||||
|
Gas,
|
||||||
|
Address,
|
||||||
|
|
||||||
i256 gas;
|
_size
|
||||||
|
};
|
||||||
|
|
||||||
|
i256 elems[_size];
|
||||||
|
|
||||||
|
static llvm::StructType* getType();
|
||||||
};
|
};
|
||||||
|
|
||||||
using StackImpl = std::vector<i256>;
|
using StackImpl = std::vector<i256>;
|
||||||
|
|
@ -46,10 +54,12 @@ public:
|
||||||
StackImpl& getStack() { return m_stack; }
|
StackImpl& getStack() { return m_stack; }
|
||||||
MemoryImpl& getMemory() { return m_memory; }
|
MemoryImpl& getMemory() { return m_memory; }
|
||||||
static ExtVMFace& getExt();
|
static ExtVMFace& getExt();
|
||||||
u256 getGas();
|
|
||||||
bytesConstRef getReturnData();
|
u256 getGas() const;
|
||||||
|
bytesConstRef getReturnData() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
void set(RuntimeData::Index _index, u256 _value);
|
||||||
|
|
||||||
/// @internal Must be the first element to asure Runtime* === RuntimeData*
|
/// @internal Must be the first element to asure Runtime* === RuntimeData*
|
||||||
RuntimeData m_data;
|
RuntimeData m_data;
|
||||||
|
|
@ -65,6 +75,7 @@ public:
|
||||||
|
|
||||||
llvm::Value* getRuntimePtr();
|
llvm::Value* getRuntimePtr();
|
||||||
|
|
||||||
|
llvm::Value* get(RuntimeData::Index _index);
|
||||||
llvm::Value* getGas();
|
llvm::Value* getGas();
|
||||||
void setGas(llvm::Value* _gas);
|
void setGas(llvm::Value* _gas);
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue