Browse Source

Change the way memory is passed to sha3 Env function

cl-refactor
Paweł Bylica 10 years ago
parent
commit
4fc4e76fa5
  1. 10
      libevmjit-cpp/Env.cpp
  2. 2
      libevmjit/Compiler.cpp
  3. 15
      libevmjit/Ext.cpp
  4. 6
      libevmjit/Ext.h
  5. 9
      libevmjit/Memory.cpp
  6. 1
      libevmjit/Memory.h

10
libevmjit-cpp/Env.cpp

@ -108,14 +108,10 @@ extern "C"
o_ret->a = ret ? 1 : 0;
}
EXPORT void ext_sha3(ExtVMFace* _env, i256* _inOff, i256* _inSize, i256* o_ret)
EXPORT void ext_sha3(ExtVMFace* _env, byte* _begin, uint64_t _size, h256* o_hash)
{
auto inOff = static_cast<size_t>(llvm2eth(*_inOff));
auto inSize = static_cast<size_t>(llvm2eth(*_inSize));
//auto dataRef = bytesConstRef(_rt->getMemory().data() + inOff, inSize);
auto dataRef = bytesConstRef(); // FIXME: Handle memory
auto hash = sha3(dataRef);
*o_ret = *reinterpret_cast<i256*>(&hash);
auto hash = sha3({_begin, _size});
*o_hash = hash;
}
EXPORT unsigned char* ext_codeAt(ExtVMFace* _env, h256* _addr256)

2
libevmjit/Compiler.cpp

@ -172,7 +172,7 @@ std::unique_ptr<llvm::Module> Compiler::compile(bytes const& _bytecode)
RuntimeManager runtimeManager(m_builder);
GasMeter gasMeter(m_builder, runtimeManager);
Memory memory(runtimeManager, gasMeter);
Ext ext(runtimeManager);
Ext ext(runtimeManager, memory);
Stack stack(m_builder, runtimeManager);
Arith256 arith(m_builder);

15
libevmjit/Ext.cpp

@ -9,6 +9,7 @@
//#include <libevm/FeeStructure.h>
#include "RuntimeManager.h"
#include "Memory.h"
#include "Type.h"
#include "Endianness.h"
@ -19,9 +20,9 @@ namespace eth
namespace jit
{
Ext::Ext(RuntimeManager& _runtimeManager):
Ext::Ext(RuntimeManager& _runtimeManager, Memory& _memoryMan):
RuntimeHelper(_runtimeManager),
m_data()
m_memoryMan(_memoryMan)
{
auto&& ctx = m_builder.getContext();
auto module = getModule();
@ -49,7 +50,6 @@ Ext::Ext(RuntimeManager& _runtimeManager):
m_suicide = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 2}, false), Linkage::ExternalLinkage, "ext_suicide", module);
m_create = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 5}, false), Linkage::ExternalLinkage, "ext_create", module);
m_call = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argsTypes, false), Linkage::ExternalLinkage, "ext_call", module);
m_sha3 = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 4}, false), Linkage::ExternalLinkage, "ext_sha3", module);
m_exp = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 4}, false), Linkage::ExternalLinkage, "ext_exp", module);
m_codeAt = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, {argsTypes, 2}, false), Linkage::ExternalLinkage, "ext_codeAt", module);
m_codesizeAt = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 3}, false), Linkage::ExternalLinkage, "ext_codesizeAt", module);
@ -58,6 +58,9 @@ Ext::Ext(RuntimeManager& _runtimeManager):
m_log2 = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 5}, false), Linkage::ExternalLinkage, "ext_log2", module);
m_log3 = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 6}, false), Linkage::ExternalLinkage, "ext_log3", module);
m_log4 = llvm::Function::Create(llvm::FunctionType::get(Type::Void, {argsTypes, 7}, false), Linkage::ExternalLinkage, "ext_log4", module);
llvm::Type* sha3ArgsTypes[] = { Type::EnvPtr, Type::BytePtr, Type::Size, Type::WordPtr };
m_sha3 = llvm::Function::Create(llvm::FunctionType::get(Type::Void, sha3ArgsTypes, false), Linkage::ExternalLinkage, "ext_sha3", module);
}
llvm::Value* Ext::store(llvm::Value* _index)
@ -127,9 +130,9 @@ llvm::Value* Ext::call(llvm::Value*& _gas, llvm::Value* _receiveAddress, llvm::V
llvm::Value* Ext::sha3(llvm::Value* _inOff, llvm::Value* _inSize)
{
m_builder.CreateStore(_inOff, m_args[0]);
m_builder.CreateStore(_inSize, m_arg2);
createCall(m_sha3, getRuntimeManager().getEnv(), m_args[0], m_arg2, m_args[1]);
auto begin = m_memoryMan.getBytePtr(_inOff);
auto size = m_builder.CreateTrunc(_inSize, Type::Size, "size");
createCall(m_sha3, getRuntimeManager().getEnv(), begin, size, m_args[1]);
llvm::Value* hash = m_builder.CreateLoad(m_args[1]);
hash = Endianness::toNative(m_builder, hash);
return hash;

6
libevmjit/Ext.h

@ -10,11 +10,12 @@ namespace eth
{
namespace jit
{
class Memory;
class Ext : public RuntimeHelper
{
public:
Ext(RuntimeManager& _runtimeManager);
Ext(RuntimeManager& _runtimeManager, Memory& _memoryMan);
llvm::Value* store(llvm::Value* _index);
void setStore(llvm::Value* _index, llvm::Value* _value);
@ -32,6 +33,7 @@ public:
void log(llvm::Value* _memIdx, llvm::Value* _numBytes, size_t _numTopics, std::array<llvm::Value*,4> const& _topics);
private:
Memory& m_memoryMan;
llvm::Value* m_args[2];
llvm::Value* m_arg2;
@ -41,7 +43,7 @@ private:
llvm::Value* m_arg6;
llvm::Value* m_arg7;
llvm::Value* m_arg8;
llvm::Value* m_data;
llvm::Value* m_data = nullptr;
llvm::Function* m_store;
llvm::Function* m_setStore;
llvm::Function* m_calldataload;

9
libevmjit/Memory.cpp

@ -118,8 +118,7 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMet
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8;
this->require(index, Constant::get(valueSize));
auto data = m_builder.CreateLoad(m_data, "data");
auto ptr = m_builder.CreateGEP(data, index, "ptr");
auto ptr = getBytePtr(index);
if (isWord)
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr");
if (_isStore)
@ -169,6 +168,12 @@ llvm::Value* Memory::getSize()
return m_builder.CreateLoad(m_size);
}
llvm::Value* Memory::getBytePtr(llvm::Value* _index)
{
auto data = m_builder.CreateLoad(m_data, "data");
return m_builder.CreateGEP(data, _index, "ptr");
}
void Memory::require(llvm::Value* _offset, llvm::Value* _size)
{
m_builder.CreateCall2(m_require, _offset, _size);

1
libevmjit/Memory.h

@ -19,6 +19,7 @@ public:
void storeByte(llvm::Value* _addr, llvm::Value* _byte);
llvm::Value* getData();
llvm::Value* getSize();
llvm::Value* getBytePtr(llvm::Value* _index);
void copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIndex,
llvm::Value* _destMemIdx, llvm::Value* _byteCount);

Loading…
Cancel
Save