Browse Source

Remove old memory backend

cl-refactor
Paweł Bylica 10 years ago
parent
commit
3197dca8a5
  1. 4
      libevmjit/Array.cpp
  2. 2
      libevmjit/Array.h
  3. 1
      libevmjit/ExecutionEngine.cpp
  4. 5
      libevmjit/ExecutionEngine.h
  5. 43
      libevmjit/Memory.cpp
  6. 5
      libevmjit/Runtime.h

4
libevmjit/Array.cpp

@ -250,9 +250,9 @@ void Array::pop(llvm::Value* _count)
m_builder.CreateStore(newSize, sizePtr);
}
llvm::Value* Array::size()
llvm::Value* Array::size(llvm::Value* _array)
{
auto sizePtr = m_builder.CreateStructGEP(m_array, 1, "sizePtr");
auto sizePtr = m_builder.CreateStructGEP(_array ? _array : m_array, 1, "sizePtr");
return m_builder.CreateLoad(sizePtr, "array.size");
}

2
libevmjit/Array.h

@ -37,7 +37,7 @@ public:
void set(llvm::Value* _index, llvm::Value* _value) { m_setFunc.call(m_builder, {m_array, _index, _value}); }
llvm::Value* get(llvm::Value* _index) { return m_getFunc.call(m_builder, {m_array, _index}); }
void pop(llvm::Value* _count);
llvm::Value* size();
llvm::Value* size(llvm::Value* _array = nullptr);
void free() { m_freeFunc.call(m_builder, {m_array}); }
void extend(llvm::Value* _arrayPtr, llvm::Value* _size);

1
libevmjit/ExecutionEngine.cpp

@ -153,7 +153,6 @@ ReturnCode ExecutionEngine::run(RuntimeData* _data, Env* _env)
if (returnCode == ReturnCode::Return)
{
returnData = runtime.getReturnData(); // Save reference to return data
std::swap(m_memory, runtime.getMemory()); // Take ownership of memory
}
listener->stateChanged(ExecState::Finished);

5
libevmjit/ExecutionEngine.h

@ -49,11 +49,6 @@ public:
/// Reference to returned data (RETURN opcode used)
bytes_ref returnData;
private:
/// After execution, if RETURN used, memory is moved there
/// to allow client copy the returned data
bytes m_memory;
};
}

43
libevmjit/Memory.cpp

@ -41,9 +41,6 @@ llvm::Function* Memory::getRequireFunc()
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());
auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func);
auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func);
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func);
@ -62,10 +59,8 @@ llvm::Function* Memory::getRequireFunc()
auto uaddRes = m_builder.CreateCall2(uaddWO, offset, size, "res");
auto sizeRequired = m_builder.CreateExtractValue(uaddRes, 0, "sizeReq");
auto overflow1 = m_builder.CreateExtractValue(uaddRes, 1, "overflow1");
auto rtPtr = getRuntimeManager().getRuntimePtr();
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 3);
auto currSize = m_builder.CreateLoad(sizePtr, "currSize");
auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall");
auto currSize = m_memory.size(mem);
auto tooSmall = m_builder.CreateICmpULE(m_builder.CreateZExt(currSize, Type::Word), sizeRequired, "tooSmall");
auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded");
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
@ -79,14 +74,10 @@ llvm::Function* Memory::getRequireFunc()
wordsRequired = m_builder.CreateSelect(overflow, Constant::get(-1), wordsRequired);
wordsRequired = m_builder.CreateUDiv(wordsRequired, Constant::get(32), "wordsReq");
sizeRequired = m_builder.CreateMul(wordsRequired, Constant::get(32), "roundedSizeReq");
auto words = m_builder.CreateUDiv(currSize, Constant::get(32), "words"); // size is always 32*k
auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords");
auto words = m_builder.CreateUDiv(currSize, m_builder.getInt64(32), "words"); // size is always 32*k
auto newWords = m_builder.CreateSub(wordsRequired, m_builder.CreateZExt(words, Type::Word), "addtionalWords");
m_gasMeter.countMemory(newWords, jmpBuf);
// Resize
m_builder.CreateStore(sizeRequired, sizePtr);
auto newData = m_builder.CreateCall2(resize, rt, sizePtr, "newData");
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 2);
m_builder.CreateStore(newData, dataPtr);
auto extendSize = m_builder.CreateTrunc(sizeRequired, Type::Size, "extendSize");
m_memory.extend(mem, extendSize);
m_builder.CreateBr(returnBB);
@ -116,9 +107,6 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType)
auto index = rt->getNextNode();
index->setName("index");
auto ptr = getBytePtr(index);
if (isWord)
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr");
if (_isStore)
{
auto valueArg = index->getNextNode();
@ -126,7 +114,6 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType)
auto mem = valueArg->getNextNode();
mem->setName("mem");
auto value = isWord ? Endianness::toBE(m_builder, valueArg) : valueArg;
m_builder.CreateStore(value, ptr);
auto memPtr = m_memory.getPtr(mem, m_builder.CreateTrunc(index, Type::Size));
auto valuePtr = m_builder.CreateBitCast(memPtr, _valueType->getPointerTo(), "valuePtr");
m_builder.CreateStore(value, valuePtr);
@ -137,8 +124,6 @@ llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType)
auto mem = index->getNextNode();
mem->setName("mem");
auto memPtr = m_memory.getPtr(mem, m_builder.CreateTrunc(index, Type::Size));
llvm::Value* ret = m_builder.CreateLoad(memPtr);
ret = Endianness::toNative(m_builder, ret);
m_builder.CreateRet(ret);
@ -201,11 +186,7 @@ llvm::Value* Memory::getData()
llvm::Value* Memory::getSize()
{
auto rtPtr = getRuntimeManager().getRuntimePtr();
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 3);
auto size = m_builder.CreateLoad(sizePtr, "size");
assert(size->getType() == Type::Word);
return size;
return m_builder.CreateZExt(m_memory.size(), Type::Word, "msize"); // TODO: Allow placing i64 on stack
}
llvm::Value* Memory::getBytePtr(llvm::Value* _index)
@ -263,17 +244,3 @@ void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value*
}
}
}
extern "C"
{
using namespace dev::eth::jit;
EXPORT byte* mem_resize(Runtime* _rt, i256* _size) // TODO: Use uint64 as size OR use realloc in LLVM IR
{
auto size = _size->a; // Trunc to 64-bit
auto& memory = _rt->getMemory();
memory.resize(size);
return memory.data();
}
}

5
libevmjit/Runtime.h

@ -18,8 +18,6 @@ public:
Runtime(const Runtime&) = delete;
Runtime& operator=(const Runtime&) = delete;
MemoryImpl& getMemory() { return m_memory; }
bytes_ref getReturnData() const;
private:
@ -28,10 +26,9 @@ private:
byte* m_memoryData = nullptr;
i256 m_memorySize;
public:
byte* m_memData = nullptr;
byte* m_memData = nullptr; // FIXME: Remember to free memory
uint64_t m_memSize = 0;
uint64_t m_memCap = 0;
MemoryImpl m_memory;
};
}

Loading…
Cancel
Save