|
|
@ -24,113 +24,132 @@ Ext::Ext(RuntimeManager& _runtimeManager, Memory& _memoryMan): |
|
|
|
RuntimeHelper(_runtimeManager), |
|
|
|
m_memoryMan(_memoryMan) |
|
|
|
{ |
|
|
|
auto module = getModule(); |
|
|
|
|
|
|
|
m_args[0] = m_builder.CreateAlloca(Type::Word, nullptr, "ext.index"); |
|
|
|
m_args[1] = m_builder.CreateAlloca(Type::Word, nullptr, "ext.value"); |
|
|
|
m_arg2 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg2"); |
|
|
|
m_arg3 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg3"); |
|
|
|
m_arg4 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg4"); |
|
|
|
m_arg5 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg5"); |
|
|
|
m_arg6 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg6"); |
|
|
|
m_arg7 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg7"); |
|
|
|
m_arg8 = m_builder.CreateAlloca(Type::Word, nullptr, "ext.arg8"); |
|
|
|
m_size = m_builder.CreateAlloca(Type::Size, nullptr, "env.size"); |
|
|
|
} |
|
|
|
|
|
|
|
using Linkage = llvm::GlobalValue::LinkageTypes; |
|
|
|
|
|
|
|
llvm::Type* argsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr}; |
|
|
|
|
|
|
|
m_sload = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "env_sload", module); |
|
|
|
m_sstore = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "env_sstore", module); |
|
|
|
|
|
|
|
llvm::Type* sha3ArgsTypes[] = {Type::BytePtr, Type::Size, Type::WordPtr}; |
|
|
|
m_sha3 = llvm::Function::Create(llvm::FunctionType::get(Type::Void, sha3ArgsTypes, false), Linkage::ExternalLinkage, "env_sha3", module); |
|
|
|
|
|
|
|
llvm::Type* createArgsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::BytePtr, Type::Size, Type::WordPtr}; |
|
|
|
m_create = llvm::Function::Create(llvm::FunctionType::get(Type::Void, createArgsTypes, false), Linkage::ExternalLinkage, "env_create", module); |
|
|
|
|
|
|
|
llvm::Type* callArgsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::BytePtr, Type::Size, Type::BytePtr, Type::Size, Type::WordPtr}; |
|
|
|
m_call = llvm::Function::Create(llvm::FunctionType::get(Type::Bool, callArgsTypes, false), Linkage::ExternalLinkage, "env_call", module); |
|
|
|
using FuncDesc = std::tuple<char const*, llvm::FunctionType*>; |
|
|
|
|
|
|
|
llvm::Type* logArgsTypes[] = {Type::EnvPtr, Type::BytePtr, Type::Size, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr}; |
|
|
|
m_log = llvm::Function::Create(llvm::FunctionType::get(Type::Void, logArgsTypes, false), Linkage::ExternalLinkage, "env_log", module); |
|
|
|
llvm::FunctionType* getFunctionType(llvm::Type* _returnType, std::initializer_list<llvm::Type*> const& _argsTypes) |
|
|
|
{ |
|
|
|
return llvm::FunctionType::get(_returnType, llvm::ArrayRef<llvm::Type*>{_argsTypes.begin(), _argsTypes.size()}, false); |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Type* getExtCodeArgsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::Size->getPointerTo()}; |
|
|
|
m_getExtCode = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, getExtCodeArgsTypes, false), Linkage::ExternalLinkage, "env_getExtCode", module); |
|
|
|
std::array<FuncDesc, sizeOf<EnvFunc>::value> const& getEnvFuncDescs() |
|
|
|
{ |
|
|
|
static std::array<FuncDesc, sizeOf<EnvFunc>::value> descs{{ |
|
|
|
FuncDesc{"env_sload", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr})}, |
|
|
|
FuncDesc{"env_sstore", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr})}, |
|
|
|
FuncDesc{"env_sha3", getFunctionType(Type::Void, {Type::BytePtr, Type::Size, Type::WordPtr})}, |
|
|
|
FuncDesc{"env_balance", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr})}, |
|
|
|
FuncDesc{"env_create", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::BytePtr, Type::Size, Type::WordPtr})}, |
|
|
|
FuncDesc{"env_call", getFunctionType(Type::Bool, {Type::EnvPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::BytePtr, Type::Size, Type::BytePtr, Type::Size, Type::WordPtr})}, |
|
|
|
FuncDesc{"env_log", getFunctionType(Type::Void, {Type::EnvPtr, Type::BytePtr, Type::Size, Type::WordPtr, Type::WordPtr, Type::WordPtr, Type::WordPtr})}, |
|
|
|
FuncDesc{"env_blockhash", getFunctionType(Type::Void, {Type::EnvPtr, Type::WordPtr, Type::WordPtr})}, |
|
|
|
FuncDesc{"env_getExtCode", getFunctionType(Type::BytePtr, {Type::EnvPtr, Type::WordPtr, Type::Size->getPointerTo()})}, |
|
|
|
FuncDesc{"ext_calldataload", getFunctionType(Type::Void, {Type::RuntimeDataPtr, Type::WordPtr, Type::WordPtr})}, |
|
|
|
}}; |
|
|
|
|
|
|
|
return descs; |
|
|
|
} |
|
|
|
|
|
|
|
// Helper function, not client Env interface
|
|
|
|
llvm::Type* callDataLoadArgsTypes[] = {Type::RuntimeDataPtr, Type::WordPtr, Type::WordPtr}; |
|
|
|
m_calldataload = llvm::Function::Create(llvm::FunctionType::get(Type::Void, callDataLoadArgsTypes, false), Linkage::ExternalLinkage, "ext_calldataload", module); |
|
|
|
llvm::Function* createFunc(EnvFunc _id, llvm::Module* _module) |
|
|
|
{ |
|
|
|
auto&& desc = getEnvFuncDescs()[static_cast<size_t>(_id)]; |
|
|
|
return llvm::Function::Create(std::get<1>(desc), llvm::Function::ExternalLinkage, std::get<0>(desc), _module); |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Function* Ext::getBalanceFunc() |
|
|
|
llvm::Value* Ext::getArgAlloca() |
|
|
|
{ |
|
|
|
if (!m_balance) |
|
|
|
auto& a = m_argAllocas[m_argCounter++]; |
|
|
|
if (!a) |
|
|
|
{ |
|
|
|
llvm::Type* argsTypes[] = {Type::EnvPtr, Type::WordPtr, Type::WordPtr}; |
|
|
|
m_balance = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argsTypes, false), llvm::Function::ExternalLinkage, "env_balance", getModule()); |
|
|
|
// FIXME: Improve order and names
|
|
|
|
InsertPointGuard g{getBuilder()}; |
|
|
|
getBuilder().SetInsertPoint(getMainFunction()->front().getFirstNonPHI()); |
|
|
|
a = getBuilder().CreateAlloca(Type::Word, nullptr, "arg"); |
|
|
|
} |
|
|
|
return m_balance; |
|
|
|
|
|
|
|
return a; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Value* Ext::byPtr(llvm::Value* _value) |
|
|
|
{ |
|
|
|
auto a = getArgAlloca(); |
|
|
|
getBuilder().CreateStore(_value, a); |
|
|
|
return a; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::CallInst* Ext::createCall(EnvFunc _funcId, std::initializer_list<llvm::Value*> const& _args) |
|
|
|
{ |
|
|
|
auto& func = m_funcs[static_cast<size_t>(_funcId)]; |
|
|
|
if (!func) |
|
|
|
func = createFunc(_funcId, getModule()); |
|
|
|
|
|
|
|
m_argCounter = 0; |
|
|
|
return getBuilder().CreateCall(func, {_args.begin(), _args.size()}); |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Value* Ext::sload(llvm::Value* _index) |
|
|
|
{ |
|
|
|
m_builder.CreateStore(_index, m_args[0]); |
|
|
|
m_builder.CreateCall3(m_sload, getRuntimeManager().getEnvPtr(), m_args[0], m_args[1]); // Uses native endianness
|
|
|
|
return m_builder.CreateLoad(m_args[1]); |
|
|
|
auto ret = getArgAlloca(); |
|
|
|
createCall(EnvFunc::sload, {getRuntimeManager().getEnvPtr(), byPtr(_index), ret}); // Uses native endianness
|
|
|
|
return m_builder.CreateLoad(ret); |
|
|
|
} |
|
|
|
|
|
|
|
void Ext::sstore(llvm::Value* _index, llvm::Value* _value) |
|
|
|
{ |
|
|
|
m_builder.CreateStore(_index, m_args[0]); |
|
|
|
m_builder.CreateStore(_value, m_args[1]); |
|
|
|
m_builder.CreateCall3(m_sstore, getRuntimeManager().getEnvPtr(), m_args[0], m_args[1]); // Uses native endianness
|
|
|
|
createCall(EnvFunc::sstore, {getRuntimeManager().getEnvPtr(), byPtr(_index), byPtr(_value)}); // Uses native endianness
|
|
|
|
} |
|
|
|
|
|
|
|
llvm::Value* Ext::calldataload(llvm::Value* _index) |
|
|
|
{ |
|
|
|
m_builder.CreateStore(_index, m_args[0]); |
|
|
|
createCall(m_calldataload, getRuntimeManager().getDataPtr(), m_args[0], m_args[1]); |
|
|
|
auto ret = m_builder.CreateLoad(m_args[1]); |
|
|
|
auto ret = getArgAlloca(); |
|
|
|
createCall(EnvFunc::calldataload, {getRuntimeManager().getDataPtr(), byPtr(_index), ret}); |
|
|
|
ret = m_builder.CreateLoad(ret); |
|
|
|
return Endianness::toNative(m_builder, ret); |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Value* Ext::balance(llvm::Value* _address) |
|
|
|
{ |
|
|
|
auto address = Endianness::toBE(m_builder, _address); |
|
|
|
m_builder.CreateStore(address, m_args[0]); |
|
|
|
createCall(getBalanceFunc(), getRuntimeManager().getEnvPtr(), m_args[0], m_args[1]); |
|
|
|
return m_builder.CreateLoad(m_args[1]); |
|
|
|
auto ret = getArgAlloca(); |
|
|
|
createCall(EnvFunc::balance, {getRuntimeManager().getEnvPtr(), byPtr(address), ret}); |
|
|
|
return m_builder.CreateLoad(ret); |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Value* Ext::blockhash(llvm::Value* _number) |
|
|
|
{ |
|
|
|
auto hash = getArgAlloca(); |
|
|
|
createCall(EnvFunc::blockhash, {getRuntimeManager().getEnvPtr(), byPtr(_number), hash}); |
|
|
|
hash = m_builder.CreateLoad(hash); |
|
|
|
return Endianness::toNative(getBuilder(), hash); |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Value* Ext::create(llvm::Value*& _gas, llvm::Value* _endowment, llvm::Value* _initOff, llvm::Value* _initSize) |
|
|
|
{ |
|
|
|
m_builder.CreateStore(_gas, m_args[0]); |
|
|
|
m_builder.CreateStore(_endowment, m_arg2); |
|
|
|
auto gas = byPtr(_gas); |
|
|
|
auto ret = getArgAlloca(); |
|
|
|
auto begin = m_memoryMan.getBytePtr(_initOff); |
|
|
|
auto size = m_builder.CreateTrunc(_initSize, Type::Size, "size"); |
|
|
|
createCall(m_create, getRuntimeManager().getEnvPtr(), m_args[0], m_arg2, begin, size, m_args[1]); |
|
|
|
_gas = m_builder.CreateLoad(m_args[0]); // Return gas
|
|
|
|
llvm::Value* address = m_builder.CreateLoad(m_args[1]); |
|
|
|
createCall(EnvFunc::create, {getRuntimeManager().getEnvPtr(), gas, byPtr(_endowment), begin, size, ret}); |
|
|
|
_gas = m_builder.CreateLoad(gas); // Return gas
|
|
|
|
llvm::Value* address = m_builder.CreateLoad(ret); |
|
|
|
address = Endianness::toNative(m_builder, address); |
|
|
|
return address; |
|
|
|
} |
|
|
|
|
|
|
|
llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::Value* _value, llvm::Value* _inOff, llvm::Value* _inSize, llvm::Value* _outOff, llvm::Value* _outSize, llvm::Value* _codeAddress) |
|
|
|
{ |
|
|
|
m_builder.CreateStore(_gas, m_args[0]); |
|
|
|
auto gas = byPtr(_gas); |
|
|
|
auto receiveAddress = Endianness::toBE(m_builder, _receiveAddress); |
|
|
|
m_builder.CreateStore(receiveAddress, m_arg2); |
|
|
|
m_builder.CreateStore(_value, m_arg3); |
|
|
|
auto inBeg = m_memoryMan.getBytePtr(_inOff); |
|
|
|
auto inSize = m_builder.CreateTrunc(_inSize, Type::Size, "in.size"); |
|
|
|
auto outBeg = m_memoryMan.getBytePtr(_outOff); |
|
|
|
auto outSize = m_builder.CreateTrunc(_outSize, Type::Size, "out.size"); |
|
|
|
auto codeAddress = Endianness::toBE(m_builder, _codeAddress); |
|
|
|
m_builder.CreateStore(codeAddress, m_arg8); |
|
|
|
auto ret = createCall(m_call, getRuntimeManager().getEnvPtr(), m_args[0], m_arg2, m_arg3, inBeg, inSize, outBeg, outSize, m_arg8); |
|
|
|
_gas = m_builder.CreateLoad(m_args[0]); // Return gas
|
|
|
|
auto ret = createCall(EnvFunc::call, {getRuntimeManager().getEnvPtr(), gas, byPtr(receiveAddress), byPtr(_value), inBeg, inSize, outBeg, outSize, byPtr(codeAddress)}); |
|
|
|
_gas = m_builder.CreateLoad(gas); // Return gas
|
|
|
|
return m_builder.CreateZExt(ret, Type::Word, "ret"); |
|
|
|
} |
|
|
|
|
|
|
@ -138,8 +157,9 @@ llvm::Value* Ext::sha3(llvm::Value* _inOff, llvm::Value* _inSize) |
|
|
|
{ |
|
|
|
auto begin = m_memoryMan.getBytePtr(_inOff); |
|
|
|
auto size = m_builder.CreateTrunc(_inSize, Type::Size, "size"); |
|
|
|
createCall(m_sha3, begin, size, m_args[1]); |
|
|
|
llvm::Value* hash = m_builder.CreateLoad(m_args[1]); |
|
|
|
auto ret = getArgAlloca(); |
|
|
|
createCall(EnvFunc::sha3, {begin, size, ret}); |
|
|
|
llvm::Value* hash = m_builder.CreateLoad(ret); |
|
|
|
hash = Endianness::toNative(m_builder, hash); |
|
|
|
return hash; |
|
|
|
} |
|
|
@ -147,8 +167,7 @@ llvm::Value* Ext::sha3(llvm::Value* _inOff, llvm::Value* _inSize) |
|
|
|
MemoryRef Ext::getExtCode(llvm::Value* _addr) |
|
|
|
{ |
|
|
|
auto addr = Endianness::toBE(m_builder, _addr); |
|
|
|
m_builder.CreateStore(addr, m_args[0]); |
|
|
|
auto code = createCall(m_getExtCode, getRuntimeManager().getEnvPtr(), m_args[0], m_size); |
|
|
|
auto code = createCall(EnvFunc::getExtCode, {getRuntimeManager().getEnvPtr(), byPtr(addr), m_size}); |
|
|
|
auto codeSize = m_builder.CreateLoad(m_size); |
|
|
|
auto codeSize256 = m_builder.CreateZExt(codeSize, Type::Word); |
|
|
|
return {code, codeSize256}; |
|
|
@ -158,7 +177,7 @@ void Ext::log(llvm::Value* _memIdx, llvm::Value* _numBytes, std::array<llvm::Val |
|
|
|
{ |
|
|
|
auto begin = m_memoryMan.getBytePtr(_memIdx); |
|
|
|
auto size = m_builder.CreateTrunc(_numBytes, Type::Size, "size"); |
|
|
|
llvm::Value* args[] = {getRuntimeManager().getEnvPtr(), begin, size, m_arg2, m_arg3, m_arg4, m_arg5}; |
|
|
|
llvm::Value* args[] = {getRuntimeManager().getEnvPtr(), begin, size, getArgAlloca(), getArgAlloca(), getArgAlloca(), getArgAlloca()}; |
|
|
|
|
|
|
|
auto topicArgPtr = &args[3]; |
|
|
|
for (auto&& topic : _topics) |
|
|
@ -170,7 +189,7 @@ void Ext::log(llvm::Value* _memIdx, llvm::Value* _numBytes, std::array<llvm::Val |
|
|
|
++topicArgPtr; |
|
|
|
} |
|
|
|
|
|
|
|
m_builder.CreateCall(m_log, args); |
|
|
|
createCall(EnvFunc::log, {args[0], args[1], args[2], args[3], args[4], args[5], args[6]}); // TODO: use std::initializer_list<>
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|