winsvega
10 years ago
136 changed files with 2243 additions and 1280 deletions
@ -0,0 +1,21 @@ |
|||
The MIT License (MIT) |
|||
|
|||
Copyright (c) 2015 Paweł Bylica <chfast@gmail.com> |
|||
|
|||
Permission is hereby granted, free of charge, to any person obtaining a copy |
|||
of this software and associated documentation files (the "Software"), to deal |
|||
in the Software without restriction, including without limitation the rights |
|||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|||
copies of the Software, and to permit persons to whom the Software is |
|||
furnished to do so, subject to the following conditions: |
|||
|
|||
The above copyright notice and this permission notice shall be included in all |
|||
copies or substantial portions of the Software. |
|||
|
|||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|||
SOFTWARE. |
@ -0,0 +1,43 @@ |
|||
# The Ethereum EVM JIT |
|||
|
|||
EVM JIT is a library for just-in-time compilation of Ethereum EVM code. |
|||
It can be used to substitute classic interpreter-like EVM Virtual Machine in Ethereum client. |
|||
|
|||
## Build |
|||
|
|||
### Linux / Ubuntu |
|||
|
|||
1. Install llvm-3.5-dev package |
|||
1. For Ubuntu 14.04 using LLVM deb packages source: http://llvm.org/apt |
|||
2. For Ubuntu 14.10 using Ubuntu packages |
|||
2. Build library with cmake |
|||
1. `mkdir build && cd $_` |
|||
2. `cmake .. && make` |
|||
3. Install library |
|||
1. `sudo make install` |
|||
2. `sudo ldconfig` |
|||
|
|||
### OSX |
|||
|
|||
1. Install llvm35 |
|||
1. `brew install llvm35 --disable-shared --HEAD` |
|||
2. Build library with cmake |
|||
1. `mkdir build && cd $_` |
|||
2. `cmake -DLLVM_DIR=/usr/local/lib/llvm-3.5/share/llvm/cmake .. && make` |
|||
3. Install library |
|||
1. `make install` (with admin rights?) |
|||
|
|||
### Windows |
|||
|
|||
Ask me. |
|||
|
|||
## Options |
|||
|
|||
Options to evmjit library can be passed by environmental variables, e.g. `EVMJIT_CACHE=0 testeth --jit`. |
|||
|
|||
Option | Default value | Description |
|||
------------- | ------------- | ---------------------------------------------- |
|||
EVMJIT_CACHE | 1 | Enables on disk cache for compiled EVM objects |
|||
EVMJIT_DUMP | 0 | Dumps generated LLVM module to standard output |
|||
|
|||
|
@ -0,0 +1,10 @@ |
|||
|
|||
#define EVMJIT_VERSION "${EVMJIT_VERSION}" |
|||
#define EVMJIT_VERSION_MAJOR ${EVMJIT_VERSION_MAJOR} |
|||
#define EVMJIT_VERSION_MINOR ${EVMJIT_VERSION_MINOR} |
|||
#define EVMJIT_VERSION_PATCH ${EVMJIT_VERSION_PATCH} |
|||
#define EVMJIT_VERSION_PRERELEASE "${EVMJIT_VERSION_PRERELEASE}" |
|||
#define EVMJIT_VERSION_FULL "${EVMJIT_VERSION_FULL}" |
|||
|
|||
#define LLVM_VERSION "${LLVM_PACKAGE_VERSION}" |
|||
#define LLVM_ASSERTIONS "${LLVM_ENABLE_ASSERTIONS}" |
@ -0,0 +1,97 @@ |
|||
#include "ExecStats.h" |
|||
|
|||
#include <iostream> |
|||
#include <iomanip> |
|||
#include <cassert> |
|||
|
|||
#include "Utils.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
void ExecStats::stateChanged(ExecState _state) |
|||
{ |
|||
if (!CHECK(m_state != ExecState::Finished)) |
|||
return; |
|||
|
|||
auto now = clock::now(); |
|||
if (_state != ExecState::Started) |
|||
{ |
|||
assert(time[(int)m_state] == ExecStats::duration::zero()); |
|||
time[(int)m_state] = now - m_tp; |
|||
} |
|||
m_state = _state; |
|||
m_tp = now; |
|||
} |
|||
|
|||
namespace |
|||
{ |
|||
struct StatsAgg |
|||
{ |
|||
using unit = std::chrono::microseconds; |
|||
ExecStats::duration tot = ExecStats::duration::zero(); |
|||
ExecStats::duration min = ExecStats::duration::max(); |
|||
ExecStats::duration max = ExecStats::duration::zero(); |
|||
size_t count = 0; |
|||
|
|||
void update(ExecStats::duration _d) |
|||
{ |
|||
++count; |
|||
tot += _d; |
|||
min = _d < min ? _d : min; |
|||
max = _d > max ? _d : max; |
|||
} |
|||
|
|||
void output(char const* _name, std::ostream& _os) |
|||
{ |
|||
auto avg = tot / count; |
|||
_os << std::setfill(' ') |
|||
<< std::setw(12) << std::left << _name |
|||
<< std::setw(10) << std::right << std::chrono::duration_cast<unit>(tot).count() |
|||
<< std::setw(10) << std::right << std::chrono::duration_cast<unit>(avg).count() |
|||
<< std::setw(10) << std::right << std::chrono::duration_cast<unit>(min).count() |
|||
<< std::setw(10) << std::right << std::chrono::duration_cast<unit>(max).count() |
|||
<< std::endl; |
|||
} |
|||
}; |
|||
|
|||
char const* getExecStateName(ExecState _state) |
|||
{ |
|||
switch (_state) |
|||
{ |
|||
case ExecState::Started: return "Start"; |
|||
case ExecState::CacheLoad: return "CacheLoad"; |
|||
case ExecState::CacheWrite: return "CacheWrite"; |
|||
case ExecState::Compilation: return "Compilation"; |
|||
case ExecState::CodeGen: return "CodeGen"; |
|||
case ExecState::Execution: return "Execution"; |
|||
case ExecState::Return: return "Return"; |
|||
case ExecState::Finished: return "Finish"; |
|||
} |
|||
return nullptr; |
|||
} |
|||
} |
|||
|
|||
StatsCollector::~StatsCollector() |
|||
{ |
|||
if (stats.empty()) |
|||
return; |
|||
|
|||
std::cout << " [us] total avg min max\n"; |
|||
for (int i = 0; i < (int)ExecState::Finished; ++i) |
|||
{ |
|||
StatsAgg agg; |
|||
for (auto&& s : stats) |
|||
agg.update(s->time[i]); |
|||
|
|||
agg.output(getExecStateName(ExecState(i)), std::cout); |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
@ -0,0 +1,43 @@ |
|||
#pragma once |
|||
|
|||
#include <chrono> |
|||
|
|||
#include "ExecutionEngine.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
class ExecStats : public ExecutionEngineListener |
|||
{ |
|||
public: |
|||
using clock = std::chrono::high_resolution_clock; |
|||
using duration = clock::duration; |
|||
using time_point = clock::time_point; |
|||
|
|||
std::string id; |
|||
duration time[(int)ExecState::Finished] = {}; |
|||
|
|||
void stateChanged(ExecState _state) override; |
|||
|
|||
private: |
|||
ExecState m_state = {}; |
|||
time_point m_tp = {}; |
|||
|
|||
}; |
|||
|
|||
|
|||
class StatsCollector |
|||
{ |
|||
public: |
|||
std::vector<std::unique_ptr<ExecStats>> stats; |
|||
|
|||
~StatsCollector(); |
|||
}; |
|||
|
|||
} |
|||
} |
|||
} |
@ -1,239 +1,260 @@ |
|||
#include "Memory.h" |
|||
|
|||
#include <vector> |
|||
#include <iostream> |
|||
#include <iomanip> |
|||
#include <cstdint> |
|||
#include <cassert> |
|||
|
|||
#include <llvm/IR/GlobalVariable.h> |
|||
#include <llvm/IR/Function.h> |
|||
#include <llvm/IR/IntrinsicInst.h> |
|||
|
|||
#include "Type.h" |
|||
#include "Runtime.h" |
|||
#include "GasMeter.h" |
|||
#include "Endianness.h" |
|||
#include "RuntimeManager.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): |
|||
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
|
|||
m_gasMeter(_gasMeter) |
|||
{ |
|||
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr}; |
|||
m_resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule()); |
|||
llvm::AttrBuilder attrBuilder; |
|||
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly); |
|||
m_resize->setAttributes(llvm::AttributeSet::get(m_resize->getContext(), 1, attrBuilder)); |
|||
|
|||
m_require = createRequireFunc(_gasMeter); |
|||
m_loadWord = createFunc(false, Type::Word, _gasMeter); |
|||
m_storeWord = createFunc(true, Type::Word, _gasMeter); |
|||
m_storeByte = createFunc(true, Type::Byte, _gasMeter); |
|||
} |
|||
|
|||
llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) |
|||
{ |
|||
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word}; |
|||
auto 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"); |
|||
auto offset = rt->getNextNode(); |
|||
offset->setName("offset"); |
|||
auto size = offset->getNextNode(); |
|||
size->setName("size"); |
|||
|
|||
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); |
|||
auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func); |
|||
|
|||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|||
|
|||
// BB "Pre": Ignore checks with size 0
|
|||
m_builder.SetInsertPoint(preBB); |
|||
auto sizeIsZero = m_builder.CreateICmpEQ(size, Constant::get(0)); |
|||
m_builder.CreateCondBr(sizeIsZero, returnBB, checkBB); |
|||
|
|||
// BB "Check"
|
|||
m_builder.SetInsertPoint(checkBB); |
|||
auto uaddWO = llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::uadd_with_overflow, Type::Word); |
|||
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, 4); |
|||
auto currSize = m_builder.CreateLoad(sizePtr, "currSize"); |
|||
auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall"); |
|||
auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded"); |
|||
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
|
|||
|
|||
// BB "Resize"
|
|||
m_builder.SetInsertPoint(resizeBB); |
|||
// Check gas first
|
|||
uaddRes = m_builder.CreateCall2(uaddWO, sizeRequired, Constant::get(31), "res"); |
|||
auto wordsRequired = m_builder.CreateExtractValue(uaddRes, 0); |
|||
auto overflow2 = m_builder.CreateExtractValue(uaddRes, 1, "overflow2"); |
|||
auto overflow = m_builder.CreateOr(overflow1, overflow2, "overflow"); |
|||
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"); |
|||
_gasMeter.countMemory(newWords); |
|||
// Resize
|
|||
m_builder.CreateStore(sizeRequired, sizePtr); |
|||
auto newData = m_builder.CreateCall2(m_resize, rt, sizePtr, "newData"); |
|||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|||
m_builder.CreateStore(newData, dataPtr); |
|||
m_builder.CreateBr(returnBB); |
|||
|
|||
// BB "Return"
|
|||
m_builder.SetInsertPoint(returnBB); |
|||
m_builder.CreateRetVoid(); |
|||
return func; |
|||
} |
|||
|
|||
llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMeter&) |
|||
{ |
|||
auto isWord = _valueType == Type::Word; |
|||
|
|||
llvm::Type* storeArgs[] = {Type::RuntimePtr, Type::Word, _valueType}; |
|||
llvm::Type* loadArgs[] = {Type::RuntimePtr, Type::Word}; |
|||
auto name = _isStore ? isWord ? "mstore" : "mstore8" : "mload"; |
|||
auto funcType = _isStore ? llvm::FunctionType::get(Type::Void, storeArgs, false) : llvm::FunctionType::get(Type::Word, loadArgs, false); |
|||
auto func = llvm::Function::Create(funcType, llvm::Function::PrivateLinkage, name, getModule()); |
|||
|
|||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|||
|
|||
m_builder.SetInsertPoint(llvm::BasicBlock::Create(func->getContext(), {}, func)); |
|||
auto rt = func->arg_begin(); |
|||
rt->setName("rt"); |
|||
auto index = rt->getNextNode(); |
|||
index->setName("index"); |
|||
|
|||
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8; |
|||
this->require(index, Constant::get(valueSize)); |
|||
auto ptr = getBytePtr(index); |
|||
if (isWord) |
|||
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr"); |
|||
if (_isStore) |
|||
{ |
|||
llvm::Value* value = index->getNextNode(); |
|||
value->setName("value"); |
|||
if (isWord) |
|||
value = Endianness::toBE(m_builder, value); |
|||
m_builder.CreateStore(value, ptr); |
|||
m_builder.CreateRetVoid(); |
|||
} |
|||
else |
|||
{ |
|||
llvm::Value* ret = m_builder.CreateLoad(ptr); |
|||
ret = Endianness::toNative(m_builder, ret); |
|||
m_builder.CreateRet(ret); |
|||
} |
|||
|
|||
return func; |
|||
} |
|||
|
|||
|
|||
llvm::Value* Memory::loadWord(llvm::Value* _addr) |
|||
{ |
|||
return createCall(m_loadWord, {getRuntimeManager().getRuntimePtr(), _addr}); |
|||
} |
|||
|
|||
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) |
|||
{ |
|||
createCall(m_storeWord, {getRuntimeManager().getRuntimePtr(), _addr, _word}); |
|||
} |
|||
|
|||
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) |
|||
{ |
|||
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); |
|||
createCall(m_storeByte, {getRuntimeManager().getRuntimePtr(), _addr, byte}); |
|||
} |
|||
|
|||
llvm::Value* Memory::getData() |
|||
{ |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|||
return m_builder.CreateLoad(dataPtr, "data"); |
|||
} |
|||
|
|||
llvm::Value* Memory::getSize() |
|||
{ |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
|||
return m_builder.CreateLoad(sizePtr, "size"); |
|||
} |
|||
|
|||
llvm::Value* Memory::getBytePtr(llvm::Value* _index) |
|||
{ |
|||
auto idx = m_builder.CreateTrunc(_index, Type::Size, "idx"); // Never allow memory index be a type bigger than i64
|
|||
return m_builder.CreateGEP(getData(), idx, "ptr"); |
|||
} |
|||
|
|||
void Memory::require(llvm::Value* _offset, llvm::Value* _size) |
|||
{ |
|||
createCall(m_require, {getRuntimeManager().getRuntimePtr(), _offset, _size}); |
|||
} |
|||
|
|||
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, |
|||
llvm::Value* _destMemIdx, llvm::Value* _reqBytes) |
|||
{ |
|||
require(_destMemIdx, _reqBytes); |
|||
|
|||
// Additional copy cost
|
|||
// TODO: This round ups to 32 happens in many places
|
|||
auto copyWords = m_builder.CreateUDiv(m_builder.CreateAdd(_reqBytes, Constant::get(31)), Constant::get(32)); |
|||
m_gasMeter.countCopy(copyWords); |
|||
|
|||
// Algorithm:
|
|||
// isOutsideData = idx256 >= size256
|
|||
// idx64 = trunc idx256
|
|||
// size64 = trunc size256
|
|||
// dataLeftSize = size64 - idx64 // safe if not isOutsideData
|
|||
// reqBytes64 = trunc _reqBytes // require() handles large values
|
|||
// bytesToCopy0 = select(reqBytes64 > dataLeftSize, dataSizeLeft, reqBytes64) // min
|
|||
// bytesToCopy = select(isOutsideData, 0, bytesToCopy0)
|
|||
|
|||
auto isOutsideData = m_builder.CreateICmpUGE(_srcIdx, _srcSize); |
|||
auto idx64 = m_builder.CreateTrunc(_srcIdx, Type::lowPrecision); |
|||
auto size64 = m_builder.CreateTrunc(_srcSize, Type::lowPrecision); |
|||
auto dataLeftSize = m_builder.CreateNUWSub(size64, idx64); |
|||
auto reqBytes64 = m_builder.CreateTrunc(_reqBytes, Type::lowPrecision); |
|||
auto outOfBound = m_builder.CreateICmpUGT(reqBytes64, dataLeftSize); |
|||
auto bytesToCopyInner = m_builder.CreateSelect(outOfBound, dataLeftSize, reqBytes64); |
|||
auto zero64 = llvm::ConstantInt::get(Type::lowPrecision, 0); // TODO: Cache common constants
|
|||
auto bytesToCopy = m_builder.CreateSelect(isOutsideData, zero64, bytesToCopyInner); |
|||
|
|||
auto src = m_builder.CreateGEP(_srcPtr, idx64, "src"); |
|||
auto dstIdx = m_builder.CreateTrunc(_destMemIdx, Type::Size, "dstIdx"); // Never allow memory index be a type bigger than i64
|
|||
auto dst = m_builder.CreateGEP(getData(), dstIdx, "dst"); |
|||
m_builder.CreateMemCpy(dst, src, bytesToCopy, 0); |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
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(); |
|||
} |
|||
} |
|||
#include "Memory.h" |
|||
|
|||
#include "preprocessor/llvm_includes_start.h" |
|||
#include <llvm/IR/IntrinsicInst.h> |
|||
#include "preprocessor/llvm_includes_end.h" |
|||
|
|||
#include "Type.h" |
|||
#include "Runtime.h" |
|||
#include "GasMeter.h" |
|||
#include "Endianness.h" |
|||
#include "RuntimeManager.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): |
|||
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
|
|||
m_gasMeter(_gasMeter) |
|||
{} |
|||
|
|||
llvm::Function* Memory::getRequireFunc() |
|||
{ |
|||
auto& func = m_require; |
|||
if (!func) |
|||
{ |
|||
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word}; |
|||
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"); |
|||
auto offset = rt->getNextNode(); |
|||
offset->setName("offset"); |
|||
auto size = offset->getNextNode(); |
|||
size->setName("size"); |
|||
|
|||
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()); |
|||
llvm::AttrBuilder attrBuilder; |
|||
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly); |
|||
resize->setAttributes(llvm::AttributeSet::get(resize->getContext(), 1, attrBuilder)); |
|||
|
|||
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); |
|||
auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func); |
|||
|
|||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|||
|
|||
// BB "Pre": Ignore checks with size 0
|
|||
m_builder.SetInsertPoint(preBB); |
|||
auto sizeIsZero = m_builder.CreateICmpEQ(size, Constant::get(0)); |
|||
m_builder.CreateCondBr(sizeIsZero, returnBB, checkBB); |
|||
|
|||
// BB "Check"
|
|||
m_builder.SetInsertPoint(checkBB); |
|||
auto uaddWO = llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::uadd_with_overflow, Type::Word); |
|||
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, 4); |
|||
auto currSize = m_builder.CreateLoad(sizePtr, "currSize"); |
|||
auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall"); |
|||
auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded"); |
|||
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
|
|||
|
|||
// BB "Resize"
|
|||
m_builder.SetInsertPoint(resizeBB); |
|||
// Check gas first
|
|||
uaddRes = m_builder.CreateCall2(uaddWO, sizeRequired, Constant::get(31), "res"); |
|||
auto wordsRequired = m_builder.CreateExtractValue(uaddRes, 0); |
|||
auto overflow2 = m_builder.CreateExtractValue(uaddRes, 1, "overflow2"); |
|||
auto overflow = m_builder.CreateOr(overflow1, overflow2, "overflow"); |
|||
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"); |
|||
m_gasMeter.countMemory(newWords); |
|||
// Resize
|
|||
m_builder.CreateStore(sizeRequired, sizePtr); |
|||
auto newData = m_builder.CreateCall2(resize, rt, sizePtr, "newData"); |
|||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|||
m_builder.CreateStore(newData, dataPtr); |
|||
m_builder.CreateBr(returnBB); |
|||
|
|||
// BB "Return"
|
|||
m_builder.SetInsertPoint(returnBB); |
|||
m_builder.CreateRetVoid(); |
|||
} |
|||
return func; |
|||
} |
|||
|
|||
llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMeter&) |
|||
{ |
|||
auto isWord = _valueType == Type::Word; |
|||
|
|||
llvm::Type* storeArgs[] = {Type::RuntimePtr, Type::Word, _valueType}; |
|||
llvm::Type* loadArgs[] = {Type::RuntimePtr, Type::Word}; |
|||
auto name = _isStore ? isWord ? "mstore" : "mstore8" : "mload"; |
|||
auto funcType = _isStore ? llvm::FunctionType::get(Type::Void, storeArgs, false) : llvm::FunctionType::get(Type::Word, loadArgs, false); |
|||
auto func = llvm::Function::Create(funcType, llvm::Function::PrivateLinkage, name, getModule()); |
|||
|
|||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|||
|
|||
m_builder.SetInsertPoint(llvm::BasicBlock::Create(func->getContext(), {}, func)); |
|||
auto rt = func->arg_begin(); |
|||
rt->setName("rt"); |
|||
auto index = rt->getNextNode(); |
|||
index->setName("index"); |
|||
|
|||
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8; |
|||
this->require(index, Constant::get(valueSize)); |
|||
auto ptr = getBytePtr(index); |
|||
if (isWord) |
|||
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr"); |
|||
if (_isStore) |
|||
{ |
|||
llvm::Value* value = index->getNextNode(); |
|||
value->setName("value"); |
|||
if (isWord) |
|||
value = Endianness::toBE(m_builder, value); |
|||
m_builder.CreateStore(value, ptr); |
|||
m_builder.CreateRetVoid(); |
|||
} |
|||
else |
|||
{ |
|||
llvm::Value* ret = m_builder.CreateLoad(ptr); |
|||
ret = Endianness::toNative(m_builder, ret); |
|||
m_builder.CreateRet(ret); |
|||
} |
|||
|
|||
return func; |
|||
} |
|||
|
|||
llvm::Function* Memory::getLoadWordFunc() |
|||
{ |
|||
auto& func = m_loadWord; |
|||
if (!func) |
|||
func = createFunc(false, Type::Word, m_gasMeter); |
|||
return func; |
|||
} |
|||
|
|||
llvm::Function* Memory::getStoreWordFunc() |
|||
{ |
|||
auto& func = m_storeWord; |
|||
if (!func) |
|||
func = createFunc(true, Type::Word, m_gasMeter); |
|||
return func; |
|||
} |
|||
|
|||
llvm::Function* Memory::getStoreByteFunc() |
|||
{ |
|||
auto& func = m_storeByte; |
|||
if (!func) |
|||
func = createFunc(true, Type::Byte, m_gasMeter); |
|||
return func; |
|||
} |
|||
|
|||
|
|||
llvm::Value* Memory::loadWord(llvm::Value* _addr) |
|||
{ |
|||
return createCall(getLoadWordFunc(), {getRuntimeManager().getRuntimePtr(), _addr}); |
|||
} |
|||
|
|||
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) |
|||
{ |
|||
createCall(getStoreWordFunc(), {getRuntimeManager().getRuntimePtr(), _addr, _word}); |
|||
} |
|||
|
|||
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) |
|||
{ |
|||
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); |
|||
createCall(getStoreByteFunc(), {getRuntimeManager().getRuntimePtr(), _addr, byte}); |
|||
} |
|||
|
|||
llvm::Value* Memory::getData() |
|||
{ |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
|||
return m_builder.CreateLoad(dataPtr, "data"); |
|||
} |
|||
|
|||
llvm::Value* Memory::getSize() |
|||
{ |
|||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
|||
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
|||
return m_builder.CreateLoad(sizePtr, "size"); |
|||
} |
|||
|
|||
llvm::Value* Memory::getBytePtr(llvm::Value* _index) |
|||
{ |
|||
auto idx = m_builder.CreateTrunc(_index, Type::Size, "idx"); // Never allow memory index be a type bigger than i64
|
|||
return m_builder.CreateGEP(getData(), idx, "ptr"); |
|||
} |
|||
|
|||
void Memory::require(llvm::Value* _offset, llvm::Value* _size) |
|||
{ |
|||
if (auto constant = llvm::dyn_cast<llvm::ConstantInt>(_size)) |
|||
{ |
|||
if (!constant->getValue()) |
|||
return; |
|||
} |
|||
createCall(getRequireFunc(), {getRuntimeManager().getRuntimePtr(), _offset, _size}); |
|||
} |
|||
|
|||
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, |
|||
llvm::Value* _destMemIdx, llvm::Value* _reqBytes) |
|||
{ |
|||
require(_destMemIdx, _reqBytes); |
|||
|
|||
// Additional copy cost
|
|||
// TODO: This round ups to 32 happens in many places
|
|||
auto reqBytes = m_builder.CreateTrunc(_reqBytes, Type::Gas); |
|||
auto copyWords = m_builder.CreateUDiv(m_builder.CreateNUWAdd(reqBytes, m_builder.getInt64(31)), m_builder.getInt64(32)); |
|||
m_gasMeter.countCopy(copyWords); |
|||
|
|||
// Algorithm:
|
|||
// isOutsideData = idx256 >= size256
|
|||
// idx64 = trunc idx256
|
|||
// size64 = trunc size256
|
|||
// dataLeftSize = size64 - idx64 // safe if not isOutsideData
|
|||
// reqBytes64 = trunc _reqBytes // require() handles large values
|
|||
// bytesToCopy0 = select(reqBytes64 > dataLeftSize, dataSizeLeft, reqBytes64) // min
|
|||
// bytesToCopy = select(isOutsideData, 0, bytesToCopy0)
|
|||
|
|||
auto isOutsideData = m_builder.CreateICmpUGE(_srcIdx, _srcSize); |
|||
auto idx64 = m_builder.CreateTrunc(_srcIdx, Type::Size); |
|||
auto size64 = m_builder.CreateTrunc(_srcSize, Type::Size); |
|||
auto dataLeftSize = m_builder.CreateNUWSub(size64, idx64); |
|||
auto outOfBound = m_builder.CreateICmpUGT(reqBytes, dataLeftSize); |
|||
auto bytesToCopyInner = m_builder.CreateSelect(outOfBound, dataLeftSize, reqBytes); |
|||
auto bytesToCopy = m_builder.CreateSelect(isOutsideData, m_builder.getInt64(0), bytesToCopyInner); |
|||
|
|||
auto src = m_builder.CreateGEP(_srcPtr, idx64, "src"); |
|||
auto dstIdx = m_builder.CreateTrunc(_destMemIdx, Type::Size, "dstIdx"); // Never allow memory index be a type bigger than i64
|
|||
auto dst = m_builder.CreateGEP(getData(), dstIdx, "dst"); |
|||
m_builder.CreateMemCpy(dst, src, bytesToCopy, 0); |
|||
} |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
|||
|
|||
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(); |
|||
} |
|||
} |
|||
|
@ -1,46 +1,40 @@ |
|||
|
|||
#pragma once |
|||
|
|||
#include <csetjmp> |
|||
#include "RuntimeData.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
using StackImpl = std::vector<i256>; |
|||
using MemoryImpl = bytes; |
|||
using jmp_buf_ref = decltype(&std::jmp_buf{}[0]); |
|||
|
|||
class Runtime |
|||
{ |
|||
public: |
|||
Runtime(RuntimeData* _data, Env* _env); |
|||
|
|||
Runtime(const Runtime&) = delete; |
|||
Runtime& operator=(const Runtime&) = delete; |
|||
|
|||
StackImpl& getStack() { return m_stack; } |
|||
MemoryImpl& getMemory() { return m_memory; } |
|||
Env* getEnvPtr() { return &m_env; } |
|||
|
|||
bytes_ref getReturnData() const; |
|||
jmp_buf_ref getJmpBuf() { return m_jmpBuf; } |
|||
|
|||
private: |
|||
RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract.
|
|||
Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract.
|
|||
jmp_buf_ref m_currJmpBuf; ///< Pointer to jump buffer. Expected by compiled contract.
|
|||
byte* m_memoryData = nullptr; |
|||
i256 m_memorySize; |
|||
std::jmp_buf m_jmpBuf; |
|||
StackImpl m_stack; |
|||
MemoryImpl m_memory; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
} |
|||
#pragma once |
|||
|
|||
#include "RuntimeData.h" |
|||
|
|||
namespace dev |
|||
{ |
|||
namespace eth |
|||
{ |
|||
namespace jit |
|||
{ |
|||
|
|||
using StackImpl = std::vector<i256>; |
|||
using MemoryImpl = bytes; |
|||
|
|||
class Runtime |
|||
{ |
|||
public: |
|||
Runtime(RuntimeData* _data, Env* _env); |
|||
|
|||
Runtime(const Runtime&) = delete; |
|||
Runtime& operator=(const Runtime&) = delete; |
|||
|
|||
StackImpl& getStack() { return m_stack; } |
|||
MemoryImpl& getMemory() { return m_memory; } |
|||
|
|||
bytes_ref getReturnData() const; |
|||
|
|||
private: |
|||
RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract.
|
|||
Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract.
|
|||
void* m_currJmpBuf = nullptr; ///< Pointer to jump buffer. Expected by compiled contract.
|
|||
byte* m_memoryData = nullptr; |
|||
i256 m_memorySize; |
|||
StackImpl m_stack; |
|||
MemoryImpl m_memory; |
|||
}; |
|||
|
|||
} |
|||
} |
|||
} |
|||
|
@ -0,0 +1,5 @@ |
|||
#if defined(_MSC_VER) |
|||
#pragma warning(pop) |
|||
#else |
|||
#pragma GCC diagnostic pop |
|||
#endif |
@ -0,0 +1,8 @@ |
|||
#if defined(_MSC_VER) |
|||
#pragma warning(push) |
|||
#pragma warning(disable: 4267 4244 4800) |
|||
#else |
|||
#pragma GCC diagnostic push |
|||
#pragma GCC diagnostic ignored "-Wunused-parameter" |
|||
#pragma GCC diagnostic ignored "-Wconversion" |
|||
#endif |
@ -0,0 +1,40 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file GenesisInfo.cpp
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#include "GenesisInfo.h" |
|||
|
|||
std::string const dev::eth::c_genesisInfo = |
|||
R"ETHEREUM( |
|||
{ |
|||
"dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, |
|||
"e6716f9544a56c530d868e4bfbacb172315bdead": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, |
|||
"b9c015918bdaba24b4ff057a92a3873d6eb201be": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, |
|||
"1a26338f0d905e295fccb71fa9ea849ffa12aaf4": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, |
|||
"2ef47100e0787b915105fd5e3f4ff6752079d5cb": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, |
|||
"cd2a3d9f938e13cd947ec05abc7fe734df8dd826": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, |
|||
"6c386a4b26f73c802f34673f7248bb118f97424a": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, |
|||
"e4157b34ea9615cfbde6b4fda419828124b70c78": { "wei": "1606938044258990275541962092341162602522202993782792835301376" }, |
|||
"b0afc46d9ce366d06ab4952ca27db1d9557ae9fd": { "finney": "154162184" }, |
|||
"f6b1e9dc460d4d62cc22ec5f987d726929c0f9f0": { "finney": "102774789" }, |
|||
"cc45122d8b7fa0b1eaa6b29e0fb561422a9239d0": { "finney": "51387394" }, |
|||
"b7576e9d314df41ec5506494293afb1bd5d3f65d": { "finney": "69423399" }, |
|||
} |
|||
)ETHEREUM"; |
@ -0,0 +1,32 @@ |
|||
/*
|
|||
This file is part of cpp-ethereum. |
|||
|
|||
cpp-ethereum is free software: you can redistribute it and/or modify |
|||
it under the terms of the GNU General Public License as published by |
|||
the Free Software Foundation, either version 3 of the License, or |
|||
(at your option) any later version. |
|||
|
|||
cpp-ethereum is distributed in the hope that it will be useful, |
|||
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|||
GNU General Public License for more details. |
|||
|
|||
You should have received a copy of the GNU General Public License |
|||
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|||
*/ |
|||
/** @file GenesisInfo.h
|
|||
* @author Gav Wood <i@gavwood.com> |
|||
* @date 2014 |
|||
*/ |
|||
|
|||
#pragma once |
|||
|
|||
#include <string> |
|||
|
|||
namespace dev { |
|||
namespace eth { |
|||
|
|||
extern std::string const c_genesisInfo; |
|||
|
|||
} |
|||
} |
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue