Browse Source

Moving ext_calldataload helper function to evmjit library (does not depend on Env)

cl-refactor
Paweł Bylica 10 years ago
parent
commit
bd40486c7b
  1. 10
      libevmjit-cpp/Env.cpp
  2. 7
      libevmjit/Ext.cpp
  3. 22
      libevmjit/Stack.cpp

10
libevmjit-cpp/Env.cpp

@ -36,16 +36,6 @@ extern "C"
_env->setStore(index, value); // Interface uses native endianness
}
// TODO: Move to Memory/Runtime
EXPORT void ext_calldataload(ExtVMFace* _env, i256* _index, ::byte* o_value)
{
auto index = static_cast<size_t>(llvm2eth(*_index));
assert(index + 31 > index); // TODO: Handle large index
for (size_t i = index, j = 0; i <= index + 31; ++i, ++j)
o_value[j] = i < _env->data.size() ? _env->data[i] : 0; // Keep Big Endian
// TODO: It all can be done by adding padding to data or by using min() algorithm without branch
}
EXPORT void ext_balance(ExtVMFace* _env, h256* _address, i256* o_value)
{
auto u = _env->balance(right160(*_address));

7
libevmjit/Ext.cpp

@ -46,7 +46,6 @@ Ext::Ext(RuntimeManager& _runtimeManager, Memory& _memoryMan):
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);
m_calldataload = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "ext_calldataload", module);
m_balance = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "ext_balance", module);
m_suicide = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 2}, false), Linkage::ExternalLinkage, "ext_suicide", module);
@ -64,6 +63,10 @@ Ext::Ext(RuntimeManager& _runtimeManager, Memory& _memoryMan):
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);
// Helper function, not client Env interface
llvm::Type* callDataLoadArgsTypes[] = {Type::RuntimePtr, Type::WordPtr, Type::WordPtr};
m_calldataload = llvm::Function::Create(llvm::FunctionType::get(Type::Void, callDataLoadArgsTypes, false), Linkage::ExternalLinkage, "ext_calldataload", module);
}
llvm::Value* Ext::sload(llvm::Value* _index)
@ -83,7 +86,7 @@ void Ext::sstore(llvm::Value* _index, llvm::Value* _value)
llvm::Value* Ext::calldataload(llvm::Value* _index)
{
m_builder.CreateStore(_index, m_args[0]);
m_builder.CreateCall3(m_calldataload, getRuntimeManager().getEnv(), m_args[0], m_args[1]);
createCall(m_calldataload, getRuntimeManager().getRuntimePtr(), m_args[0], m_args[1]);
auto ret = m_builder.CreateLoad(m_args[1]);
return Endianness::toNative(m_builder, ret);
}

22
libevmjit/Stack.cpp

@ -109,5 +109,27 @@ extern "C"
*(stack.rbegin() + _index) = *_word;
}
EXPORT void ext_calldataload(Runtime* _rt, i256* _index, byte* o_value)
{
// It asumes all indexes are less than 2^64
auto index = _index->a;
if (_index->b || _index->c || _index->d) // if bigger that 2^64
index = std::numeric_limits<decltype(index)>::max(); // set max to fill with 0 leter
auto data = _rt->getDataPtr()->callData;
auto size = _rt->getDataPtr()->elems[RuntimeData::CallDataSize].a;
for (auto i = 0; i < 32; ++i)
{
if (index < size)
{
o_value[i] = data[index];
++index; // increment only if in range
}
else
o_value[i] = 0;
}
}
} // extern "C"

Loading…
Cancel
Save