Browse Source

Create Array for memory

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

24
libevmjit/Array.cpp

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

3
libevmjit/Array.h

@ -31,6 +31,7 @@ class Array : public CompilerHelper
{ {
public: public:
Array(llvm::IRBuilder<>& _builder, char const* _name); 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 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}); } 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; } llvm::Value* getPointerTo() const { return m_array; }
static llvm::Type* getType();
private: private:
llvm::Value* m_array = nullptr; llvm::Value* m_array = nullptr;

7
libevmjit/Memory.cpp

@ -19,6 +19,7 @@ namespace jit
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter):
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
m_memory{getBuilder(), _runtimeManager.getMem()},
m_gasMeter(_gasMeter) m_gasMeter(_gasMeter)
{} {}
@ -27,7 +28,7 @@ llvm::Function* Memory::getRequireFunc()
auto& func = m_require; auto& func = m_require;
if (!func) 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()); func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule());
auto rt = func->arg_begin(); auto rt = func->arg_begin();
rt->setName("rt"); rt->setName("rt");
@ -37,6 +38,8 @@ llvm::Function* Memory::getRequireFunc()
size->setName("size"); size->setName("size");
auto jmpBuf = size->getNextNode(); auto jmpBuf = size->getNextNode();
jmpBuf->setName("jmpBuf"); jmpBuf->setName("jmpBuf");
auto mem = jmpBuf->getNextNode();
mem->setName("mem");
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr}; 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()); 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()) if (!constant->getValue())
return; 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, void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx,

4
libevmjit/Memory.h

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

3
libevmjit/Runtime.h

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

9
libevmjit/RuntimeManager.cpp

@ -52,6 +52,7 @@ llvm::StructType* RuntimeManager::getRuntimeType()
Type::EnvPtr, // Env* Type::EnvPtr, // Env*
Type::BytePtr, // memory data Type::BytePtr, // memory data
Type::Word, // memory size Type::Word, // memory size
Array::getType()
}; };
type = llvm::StructType::create(elems, "Runtime"); type = llvm::StructType::create(elems, "Runtime");
} }
@ -122,6 +123,14 @@ llvm::Value* RuntimeManager::getDataPtr()
return dataPtr; 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() llvm::Value* RuntimeManager::getEnvPtr()
{ {
assert(getMainFunction()); // Available only in main function assert(getMainFunction()); // Available only in main function

2
libevmjit/RuntimeManager.h

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

Loading…
Cancel
Save