From 41f1e0d03581309351686659e5ca2fa185a319ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Wed, 25 Feb 2015 21:02:10 +0100 Subject: [PATCH] Create Array for memory --- libevmjit/Array.cpp | 24 +++++++++++++++++++----- libevmjit/Array.h | 3 +++ libevmjit/Memory.cpp | 7 +++++-- libevmjit/Memory.h | 4 +++- libevmjit/Runtime.h | 3 +++ libevmjit/RuntimeManager.cpp | 9 +++++++++ libevmjit/RuntimeManager.h | 2 ++ 7 files changed, 44 insertions(+), 8 deletions(-) diff --git a/libevmjit/Array.cpp b/libevmjit/Array.cpp index bd874a9dc..b44e4e174 100644 --- a/libevmjit/Array.cpp +++ b/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"); diff --git a/libevmjit/Array.h b/libevmjit/Array.h index cadeab7c9..0cd7abb17 100644 --- a/libevmjit/Array.h +++ b/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; diff --git a/libevmjit/Memory.cpp b/libevmjit/Memory.cpp index 4afe84997..db4c36c70 100644 --- a/libevmjit/Memory.cpp +++ b/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, diff --git a/libevmjit/Memory.h b/libevmjit/Memory.h index 92a8ce126..beb535226 100644 --- a/libevmjit/Memory.h +++ b/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); diff --git a/libevmjit/Runtime.h b/libevmjit/Runtime.h index ac2e0acd7..55b84d575 100644 --- a/libevmjit/Runtime.h +++ b/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; }; diff --git a/libevmjit/RuntimeManager.cpp b/libevmjit/RuntimeManager.cpp index b267a1c93..b6f945c97 100644 --- a/libevmjit/RuntimeManager.cpp +++ b/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 diff --git a/libevmjit/RuntimeManager.h b/libevmjit/RuntimeManager.h index 94e1eb303..0462c7d6a 100644 --- a/libevmjit/RuntimeManager.h +++ b/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);