Browse Source

Create Array for memory

cl-refactor
Paweł Bylica 10 years ago
parent
commit
8e6d368902
  1. 24
      evmjit/libevmjit/Array.cpp
  2. 3
      evmjit/libevmjit/Array.h
  3. 7
      evmjit/libevmjit/Memory.cpp
  4. 4
      evmjit/libevmjit/Memory.h
  5. 3
      evmjit/libevmjit/Runtime.h
  6. 9
      evmjit/libevmjit/RuntimeManager.cpp
  7. 2
      evmjit/libevmjit/RuntimeManager.h

24
evmjit/libevmjit/Array.cpp

@ -154,6 +154,13 @@ llvm::Function* Array::createFreeFunc()
return func;
}
llvm::Type* Array::getType()
{
llvm::Type* elementTys[] = {Type::WordPtr, Type::Size, Type::Size};
static auto arrayTy = llvm::StructType::create(elementTys, "Array");
return arrayTy;
}
Array::Array(llvm::IRBuilder<>& _builder, char const* _name) :
CompilerHelper(_builder),
m_pushFunc([this](){ return createArrayPushFunc(); }),
@ -161,13 +168,20 @@ Array::Array(llvm::IRBuilder<>& _builder, char const* _name) :
m_getFunc([this](){ return createArrayGetFunc(); }),
m_freeFunc([this](){ return createFreeFunc(); })
{
llvm::Type* elementTys[] = {Type::WordPtr, Type::Size, Type::Size};
static auto arrayTy = llvm::StructType::create(elementTys, "Array");
m_array = m_builder.CreateAlloca(arrayTy, nullptr, _name);
m_builder.CreateStore(llvm::ConstantAggregateZero::get(arrayTy), m_array);
m_array = m_builder.CreateAlloca(getType(), nullptr, _name);
m_builder.CreateStore(llvm::ConstantAggregateZero::get(getType()), m_array);
}
Array::Array(llvm::IRBuilder<>& _builder, llvm::Value* _array) :
CompilerHelper(_builder),
m_array(_array),
m_pushFunc([this](){ return createArrayPushFunc(); }),
m_setFunc([this](){ return createArraySetFunc(); }),
m_getFunc([this](){ return createArrayGetFunc(); }),
m_freeFunc([this](){ return createFreeFunc(); })
{}
void Array::pop(llvm::Value* _count)
{
auto sizePtr = m_builder.CreateStructGEP(m_array, 1, "sizePtr");

3
evmjit/libevmjit/Array.h

@ -31,6 +31,7 @@ class Array : public CompilerHelper
{
public:
Array(llvm::IRBuilder<>& _builder, char const* _name);
Array(llvm::IRBuilder<>& _builder, llvm::Value* _array);
void push(llvm::Value* _value) { m_pushFunc.call(m_builder, {m_array, _value}); }
void set(llvm::Value* _index, llvm::Value* _value) { m_setFunc.call(m_builder, {m_array, _index, _value}); }
@ -41,6 +42,8 @@ public:
llvm::Value* getPointerTo() const { return m_array; }
static llvm::Type* getType();
private:
llvm::Value* m_array = nullptr;

7
evmjit/libevmjit/Memory.cpp

@ -19,6 +19,7 @@ namespace jit
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter):
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
m_memory{getBuilder(), _runtimeManager.getMem()},
m_gasMeter(_gasMeter)
{}
@ -27,7 +28,7 @@ llvm::Function* Memory::getRequireFunc()
auto& func = m_require;
if (!func)
{
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word, Type::BytePtr};
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word, Type::BytePtr, Array::getType()->getPointerTo()};
func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule());
auto rt = func->arg_begin();
rt->setName("rt");
@ -37,6 +38,8 @@ llvm::Function* Memory::getRequireFunc()
size->setName("size");
auto jmpBuf = size->getNextNode();
jmpBuf->setName("jmpBuf");
auto mem = jmpBuf->getNextNode();
mem->setName("mem");
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr};
auto resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule());
@ -208,7 +211,7 @@ void Memory::require(llvm::Value* _offset, llvm::Value* _size)
if (!constant->getValue())
return;
}
createCall(getRequireFunc(), {getRuntimeManager().getRuntimePtr(), _offset, _size, getRuntimeManager().getJmpBuf()});
createCall(getRequireFunc(), {getRuntimeManager().getRuntimePtr(), _offset, _size, getRuntimeManager().getJmpBuf(), getRuntimeManager().getMem()});
}
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx,

4
evmjit/libevmjit/Memory.h

@ -1,6 +1,6 @@
#pragma once
#include "CompilerHelper.h"
#include "Array.h"
namespace dev
{
@ -28,6 +28,8 @@ public:
void require(llvm::Value* _offset, llvm::Value* _size);
private:
Array m_memory;
GasMeter& m_gasMeter;
llvm::Function* createFunc(bool _isStore, llvm::Type* _type);

3
evmjit/libevmjit/Runtime.h

@ -27,6 +27,9 @@ private:
Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract.
byte* m_memoryData = nullptr;
i256 m_memorySize;
byte* m_memData = nullptr;
uint64_t m_memSize = 0;
uint64_t m_memCap = 0;
MemoryImpl m_memory;
};

9
evmjit/libevmjit/RuntimeManager.cpp

@ -52,6 +52,7 @@ llvm::StructType* RuntimeManager::getRuntimeType()
Type::EnvPtr, // Env*
Type::BytePtr, // memory data
Type::Word, // memory size
Array::getType()
};
type = llvm::StructType::create(elems, "Runtime");
}
@ -122,6 +123,14 @@ llvm::Value* RuntimeManager::getDataPtr()
return dataPtr;
}
llvm::Value* RuntimeManager::getMem()
{
auto rtPtr = getRuntimePtr();
auto memPtr = m_builder.CreateStructGEP(rtPtr, 4, "mem");
assert(memPtr->getType() == Array::getType()->getPointerTo());
return memPtr;
}
llvm::Value* RuntimeManager::getEnvPtr()
{
assert(getMainFunction()); // Available only in main function

2
evmjit/libevmjit/RuntimeManager.h

@ -33,6 +33,8 @@ public:
llvm::Value* getJmpBuf() { return m_jmpBuf; }
void setGas(llvm::Value* _gas);
llvm::Value* getMem();
void registerReturnData(llvm::Value* _index, llvm::Value* _size);
void registerSuicide(llvm::Value* _balanceAddress);

Loading…
Cancel
Save