Gav Wood
10 years ago
267 changed files with 9474 additions and 2804 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,44 @@ |
|||||
|
#pragma once |
||||
|
|
||||
|
#include <string> |
||||
|
#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 "Memory.h" |
||||
|
|
||||
#include <vector> |
#include "preprocessor/llvm_includes_start.h" |
||||
#include <iostream> |
#include <llvm/IR/IntrinsicInst.h> |
||||
#include <iomanip> |
#include "preprocessor/llvm_includes_end.h" |
||||
#include <cstdint> |
|
||||
#include <cassert> |
#include "Type.h" |
||||
|
#include "Runtime.h" |
||||
#include <llvm/IR/GlobalVariable.h> |
#include "GasMeter.h" |
||||
#include <llvm/IR/Function.h> |
#include "Endianness.h" |
||||
#include <llvm/IR/IntrinsicInst.h> |
#include "RuntimeManager.h" |
||||
|
|
||||
#include "Type.h" |
namespace dev |
||||
#include "Runtime.h" |
{ |
||||
#include "GasMeter.h" |
namespace eth |
||||
#include "Endianness.h" |
{ |
||||
#include "RuntimeManager.h" |
namespace jit |
||||
|
{ |
||||
namespace dev |
|
||||
{ |
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): |
||||
namespace eth |
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
|
||||
{ |
m_gasMeter(_gasMeter) |
||||
namespace jit |
{} |
||||
{ |
|
||||
|
llvm::Function* Memory::getRequireFunc() |
||||
Memory::Memory(RuntimeManager& _runtimeManager, GasMeter& _gasMeter): |
{ |
||||
RuntimeHelper(_runtimeManager), // TODO: RuntimeHelper not needed
|
auto& func = m_require; |
||||
m_gasMeter(_gasMeter) |
if (!func) |
||||
{ |
{ |
||||
llvm::Type* resizeArgs[] = {Type::RuntimePtr, Type::WordPtr}; |
llvm::Type* argTypes[] = {Type::RuntimePtr, Type::Word, Type::Word}; |
||||
m_resize = llvm::Function::Create(llvm::FunctionType::get(Type::BytePtr, resizeArgs, false), llvm::Function::ExternalLinkage, "mem_resize", getModule()); |
func = llvm::Function::Create(llvm::FunctionType::get(Type::Void, argTypes, false), llvm::Function::PrivateLinkage, "mem.require", getModule()); |
||||
llvm::AttrBuilder attrBuilder; |
auto rt = func->arg_begin(); |
||||
attrBuilder.addAttribute(llvm::Attribute::NoAlias).addAttribute(llvm::Attribute::NoCapture).addAttribute(llvm::Attribute::NonNull).addAttribute(llvm::Attribute::ReadOnly); |
rt->setName("rt"); |
||||
m_resize->setAttributes(llvm::AttributeSet::get(m_resize->getContext(), 1, attrBuilder)); |
auto offset = rt->getNextNode(); |
||||
|
offset->setName("offset"); |
||||
m_require = createRequireFunc(_gasMeter); |
auto size = offset->getNextNode(); |
||||
m_loadWord = createFunc(false, Type::Word, _gasMeter); |
size->setName("size"); |
||||
m_storeWord = createFunc(true, Type::Word, _gasMeter); |
|
||||
m_storeByte = createFunc(true, Type::Byte, _gasMeter); |
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; |
||||
llvm::Function* Memory::createRequireFunc(GasMeter& _gasMeter) |
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)); |
||||
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 preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func); |
||||
auto rt = func->arg_begin(); |
auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func); |
||||
rt->setName("rt"); |
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func); |
||||
auto offset = rt->getNextNode(); |
auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func); |
||||
offset->setName("offset"); |
|
||||
auto size = offset->getNextNode(); |
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
||||
size->setName("size"); |
|
||||
|
// BB "Pre": Ignore checks with size 0
|
||||
auto preBB = llvm::BasicBlock::Create(func->getContext(), "Pre", func); |
m_builder.SetInsertPoint(preBB); |
||||
auto checkBB = llvm::BasicBlock::Create(func->getContext(), "Check", func); |
auto sizeIsZero = m_builder.CreateICmpEQ(size, Constant::get(0)); |
||||
auto resizeBB = llvm::BasicBlock::Create(func->getContext(), "Resize", func); |
m_builder.CreateCondBr(sizeIsZero, returnBB, checkBB); |
||||
auto returnBB = llvm::BasicBlock::Create(func->getContext(), "Return", func); |
|
||||
|
// BB "Check"
|
||||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
m_builder.SetInsertPoint(checkBB); |
||||
|
auto uaddWO = llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::uadd_with_overflow, Type::Word); |
||||
// BB "Pre": Ignore checks with size 0
|
auto uaddRes = m_builder.CreateCall2(uaddWO, offset, size, "res"); |
||||
m_builder.SetInsertPoint(preBB); |
auto sizeRequired = m_builder.CreateExtractValue(uaddRes, 0, "sizeReq"); |
||||
auto sizeIsZero = m_builder.CreateICmpEQ(size, Constant::get(0)); |
auto overflow1 = m_builder.CreateExtractValue(uaddRes, 1, "overflow1"); |
||||
m_builder.CreateCondBr(sizeIsZero, returnBB, checkBB); |
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
||||
|
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
||||
// BB "Check"
|
auto currSize = m_builder.CreateLoad(sizePtr, "currSize"); |
||||
m_builder.SetInsertPoint(checkBB); |
auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall"); |
||||
auto uaddWO = llvm::Intrinsic::getDeclaration(getModule(), llvm::Intrinsic::uadd_with_overflow, Type::Word); |
auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded"); |
||||
auto uaddRes = m_builder.CreateCall2(uaddWO, offset, size, "res"); |
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
|
||||
auto sizeRequired = m_builder.CreateExtractValue(uaddRes, 0, "sizeReq"); |
|
||||
auto overflow1 = m_builder.CreateExtractValue(uaddRes, 1, "overflow1"); |
// BB "Resize"
|
||||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
m_builder.SetInsertPoint(resizeBB); |
||||
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
// Check gas first
|
||||
auto currSize = m_builder.CreateLoad(sizePtr, "currSize"); |
uaddRes = m_builder.CreateCall2(uaddWO, sizeRequired, Constant::get(31), "res"); |
||||
auto tooSmall = m_builder.CreateICmpULE(currSize, sizeRequired, "tooSmall"); |
auto wordsRequired = m_builder.CreateExtractValue(uaddRes, 0); |
||||
auto resizeNeeded = m_builder.CreateOr(tooSmall, overflow1, "resizeNeeded"); |
auto overflow2 = m_builder.CreateExtractValue(uaddRes, 1, "overflow2"); |
||||
m_builder.CreateCondBr(resizeNeeded, resizeBB, returnBB); // OPT branch weights?
|
auto overflow = m_builder.CreateOr(overflow1, overflow2, "overflow"); |
||||
|
wordsRequired = m_builder.CreateSelect(overflow, Constant::get(-1), wordsRequired); |
||||
// BB "Resize"
|
wordsRequired = m_builder.CreateUDiv(wordsRequired, Constant::get(32), "wordsReq"); |
||||
m_builder.SetInsertPoint(resizeBB); |
sizeRequired = m_builder.CreateMul(wordsRequired, Constant::get(32), "roundedSizeReq"); |
||||
// Check gas first
|
auto words = m_builder.CreateUDiv(currSize, Constant::get(32), "words"); // size is always 32*k
|
||||
uaddRes = m_builder.CreateCall2(uaddWO, sizeRequired, Constant::get(31), "res"); |
auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords"); |
||||
auto wordsRequired = m_builder.CreateExtractValue(uaddRes, 0); |
m_gasMeter.countMemory(newWords); |
||||
auto overflow2 = m_builder.CreateExtractValue(uaddRes, 1, "overflow2"); |
// Resize
|
||||
auto overflow = m_builder.CreateOr(overflow1, overflow2, "overflow"); |
m_builder.CreateStore(sizeRequired, sizePtr); |
||||
wordsRequired = m_builder.CreateSelect(overflow, Constant::get(-1), wordsRequired); |
auto newData = m_builder.CreateCall2(resize, rt, sizePtr, "newData"); |
||||
wordsRequired = m_builder.CreateUDiv(wordsRequired, Constant::get(32), "wordsReq"); |
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
||||
sizeRequired = m_builder.CreateMul(wordsRequired, Constant::get(32), "roundedSizeReq"); |
m_builder.CreateStore(newData, dataPtr); |
||||
auto words = m_builder.CreateUDiv(currSize, Constant::get(32), "words"); // size is always 32*k
|
m_builder.CreateBr(returnBB); |
||||
auto newWords = m_builder.CreateSub(wordsRequired, words, "addtionalWords"); |
|
||||
_gasMeter.countMemory(newWords); |
// BB "Return"
|
||||
// Resize
|
m_builder.SetInsertPoint(returnBB); |
||||
m_builder.CreateStore(sizeRequired, sizePtr); |
m_builder.CreateRetVoid(); |
||||
auto newData = m_builder.CreateCall2(m_resize, rt, sizePtr, "newData"); |
} |
||||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
return func; |
||||
m_builder.CreateStore(newData, dataPtr); |
} |
||||
m_builder.CreateBr(returnBB); |
|
||||
|
llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMeter&) |
||||
// BB "Return"
|
{ |
||||
m_builder.SetInsertPoint(returnBB); |
auto isWord = _valueType == Type::Word; |
||||
m_builder.CreateRetVoid(); |
|
||||
return func; |
llvm::Type* storeArgs[] = {Type::RuntimePtr, Type::Word, _valueType}; |
||||
} |
llvm::Type* loadArgs[] = {Type::RuntimePtr, Type::Word}; |
||||
|
auto name = _isStore ? isWord ? "mstore" : "mstore8" : "mload"; |
||||
llvm::Function* Memory::createFunc(bool _isStore, llvm::Type* _valueType, GasMeter&) |
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()); |
||||
auto isWord = _valueType == Type::Word; |
|
||||
|
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
||||
llvm::Type* storeArgs[] = {Type::RuntimePtr, Type::Word, _valueType}; |
|
||||
llvm::Type* loadArgs[] = {Type::RuntimePtr, Type::Word}; |
m_builder.SetInsertPoint(llvm::BasicBlock::Create(func->getContext(), {}, func)); |
||||
auto name = _isStore ? isWord ? "mstore" : "mstore8" : "mload"; |
auto rt = func->arg_begin(); |
||||
auto funcType = _isStore ? llvm::FunctionType::get(Type::Void, storeArgs, false) : llvm::FunctionType::get(Type::Word, loadArgs, false); |
rt->setName("rt"); |
||||
auto func = llvm::Function::Create(funcType, llvm::Function::PrivateLinkage, name, getModule()); |
auto index = rt->getNextNode(); |
||||
|
index->setName("index"); |
||||
InsertPointGuard guard(m_builder); // Restores insert point at function exit
|
|
||||
|
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8; |
||||
m_builder.SetInsertPoint(llvm::BasicBlock::Create(func->getContext(), {}, func)); |
this->require(index, Constant::get(valueSize)); |
||||
auto rt = func->arg_begin(); |
auto ptr = getBytePtr(index); |
||||
rt->setName("rt"); |
if (isWord) |
||||
auto index = rt->getNextNode(); |
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr"); |
||||
index->setName("index"); |
if (_isStore) |
||||
|
{ |
||||
auto valueSize = _valueType->getPrimitiveSizeInBits() / 8; |
llvm::Value* value = index->getNextNode(); |
||||
this->require(index, Constant::get(valueSize)); |
value->setName("value"); |
||||
auto ptr = getBytePtr(index); |
if (isWord) |
||||
if (isWord) |
value = Endianness::toBE(m_builder, value); |
||||
ptr = m_builder.CreateBitCast(ptr, Type::WordPtr, "wordPtr"); |
m_builder.CreateStore(value, ptr); |
||||
if (_isStore) |
m_builder.CreateRetVoid(); |
||||
{ |
} |
||||
llvm::Value* value = index->getNextNode(); |
else |
||||
value->setName("value"); |
{ |
||||
if (isWord) |
llvm::Value* ret = m_builder.CreateLoad(ptr); |
||||
value = Endianness::toBE(m_builder, value); |
ret = Endianness::toNative(m_builder, ret); |
||||
m_builder.CreateStore(value, ptr); |
m_builder.CreateRet(ret); |
||||
m_builder.CreateRetVoid(); |
} |
||||
} |
|
||||
else |
return func; |
||||
{ |
} |
||||
llvm::Value* ret = m_builder.CreateLoad(ptr); |
|
||||
ret = Endianness::toNative(m_builder, ret); |
llvm::Function* Memory::getLoadWordFunc() |
||||
m_builder.CreateRet(ret); |
{ |
||||
} |
auto& func = m_loadWord; |
||||
|
if (!func) |
||||
return func; |
func = createFunc(false, Type::Word, m_gasMeter); |
||||
} |
return func; |
||||
|
} |
||||
|
|
||||
llvm::Value* Memory::loadWord(llvm::Value* _addr) |
llvm::Function* Memory::getStoreWordFunc() |
||||
{ |
{ |
||||
return createCall(m_loadWord, {getRuntimeManager().getRuntimePtr(), _addr}); |
auto& func = m_storeWord; |
||||
} |
if (!func) |
||||
|
func = createFunc(true, Type::Word, m_gasMeter); |
||||
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) |
return func; |
||||
{ |
} |
||||
createCall(m_storeWord, {getRuntimeManager().getRuntimePtr(), _addr, _word}); |
|
||||
} |
llvm::Function* Memory::getStoreByteFunc() |
||||
|
{ |
||||
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) |
auto& func = m_storeByte; |
||||
{ |
if (!func) |
||||
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); |
func = createFunc(true, Type::Byte, m_gasMeter); |
||||
createCall(m_storeByte, {getRuntimeManager().getRuntimePtr(), _addr, byte}); |
return func; |
||||
} |
} |
||||
|
|
||||
llvm::Value* Memory::getData() |
|
||||
{ |
llvm::Value* Memory::loadWord(llvm::Value* _addr) |
||||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
{ |
||||
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
return createCall(getLoadWordFunc(), {getRuntimeManager().getRuntimePtr(), _addr}); |
||||
return m_builder.CreateLoad(dataPtr, "data"); |
} |
||||
} |
|
||||
|
void Memory::storeWord(llvm::Value* _addr, llvm::Value* _word) |
||||
llvm::Value* Memory::getSize() |
{ |
||||
{ |
createCall(getStoreWordFunc(), {getRuntimeManager().getRuntimePtr(), _addr, _word}); |
||||
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
} |
||||
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
|
||||
return m_builder.CreateLoad(sizePtr, "size"); |
void Memory::storeByte(llvm::Value* _addr, llvm::Value* _word) |
||||
} |
{ |
||||
|
auto byte = m_builder.CreateTrunc(_word, Type::Byte, "byte"); |
||||
llvm::Value* Memory::getBytePtr(llvm::Value* _index) |
createCall(getStoreByteFunc(), {getRuntimeManager().getRuntimePtr(), _addr, byte}); |
||||
{ |
} |
||||
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"); |
llvm::Value* Memory::getData() |
||||
} |
{ |
||||
|
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
||||
void Memory::require(llvm::Value* _offset, llvm::Value* _size) |
auto dataPtr = m_builder.CreateStructGEP(rtPtr, 3); |
||||
{ |
return m_builder.CreateLoad(dataPtr, "data"); |
||||
createCall(m_require, {getRuntimeManager().getRuntimePtr(), _offset, _size}); |
} |
||||
} |
|
||||
|
llvm::Value* Memory::getSize() |
||||
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, |
{ |
||||
llvm::Value* _destMemIdx, llvm::Value* _reqBytes) |
auto rtPtr = getRuntimeManager().getRuntimePtr(); |
||||
{ |
auto sizePtr = m_builder.CreateStructGEP(rtPtr, 4); |
||||
require(_destMemIdx, _reqBytes); |
return m_builder.CreateLoad(sizePtr, "size"); |
||||
|
} |
||||
// Additional copy cost
|
|
||||
// TODO: This round ups to 32 happens in many places
|
llvm::Value* Memory::getBytePtr(llvm::Value* _index) |
||||
auto copyWords = m_builder.CreateUDiv(m_builder.CreateAdd(_reqBytes, Constant::get(31)), Constant::get(32)); |
{ |
||||
m_gasMeter.countCopy(copyWords); |
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"); |
||||
// Algorithm:
|
} |
||||
// isOutsideData = idx256 >= size256
|
|
||||
// idx64 = trunc idx256
|
void Memory::require(llvm::Value* _offset, llvm::Value* _size) |
||||
// size64 = trunc size256
|
{ |
||||
// dataLeftSize = size64 - idx64 // safe if not isOutsideData
|
if (auto constant = llvm::dyn_cast<llvm::ConstantInt>(_size)) |
||||
// reqBytes64 = trunc _reqBytes // require() handles large values
|
{ |
||||
// bytesToCopy0 = select(reqBytes64 > dataLeftSize, dataSizeLeft, reqBytes64) // min
|
if (!constant->getValue()) |
||||
// bytesToCopy = select(isOutsideData, 0, bytesToCopy0)
|
return; |
||||
|
} |
||||
auto isOutsideData = m_builder.CreateICmpUGE(_srcIdx, _srcSize); |
createCall(getRequireFunc(), {getRuntimeManager().getRuntimePtr(), _offset, _size}); |
||||
auto idx64 = m_builder.CreateTrunc(_srcIdx, Type::lowPrecision); |
} |
||||
auto size64 = m_builder.CreateTrunc(_srcSize, Type::lowPrecision); |
|
||||
auto dataLeftSize = m_builder.CreateNUWSub(size64, idx64); |
void Memory::copyBytes(llvm::Value* _srcPtr, llvm::Value* _srcSize, llvm::Value* _srcIdx, |
||||
auto reqBytes64 = m_builder.CreateTrunc(_reqBytes, Type::lowPrecision); |
llvm::Value* _destMemIdx, llvm::Value* _reqBytes) |
||||
auto outOfBound = m_builder.CreateICmpUGT(reqBytes64, dataLeftSize); |
{ |
||||
auto bytesToCopyInner = m_builder.CreateSelect(outOfBound, dataLeftSize, reqBytes64); |
require(_destMemIdx, _reqBytes); |
||||
auto zero64 = llvm::ConstantInt::get(Type::lowPrecision, 0); // TODO: Cache common constants
|
|
||||
auto bytesToCopy = m_builder.CreateSelect(isOutsideData, zero64, bytesToCopyInner); |
// Additional copy cost
|
||||
|
// TODO: This round ups to 32 happens in many places
|
||||
auto src = m_builder.CreateGEP(_srcPtr, idx64, "src"); |
auto reqBytes = m_builder.CreateTrunc(_reqBytes, Type::Gas); |
||||
auto dstIdx = m_builder.CreateTrunc(_destMemIdx, Type::Size, "dstIdx"); // Never allow memory index be a type bigger than i64
|
auto copyWords = m_builder.CreateUDiv(m_builder.CreateNUWAdd(reqBytes, m_builder.getInt64(31)), m_builder.getInt64(32)); |
||||
auto dst = m_builder.CreateGEP(getData(), dstIdx, "dst"); |
m_gasMeter.countCopy(copyWords); |
||||
m_builder.CreateMemCpy(dst, src, bytesToCopy, 0); |
|
||||
} |
// 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
|
||||
extern "C" |
// bytesToCopy = select(isOutsideData, 0, bytesToCopy0)
|
||||
{ |
|
||||
using namespace dev::eth::jit; |
auto isOutsideData = m_builder.CreateICmpUGE(_srcIdx, _srcSize); |
||||
|
auto idx64 = m_builder.CreateTrunc(_srcIdx, Type::Size); |
||||
EXPORT byte* mem_resize(Runtime* _rt, i256* _size) // TODO: Use uint64 as size OR use realloc in LLVM IR
|
auto size64 = m_builder.CreateTrunc(_srcSize, Type::Size); |
||||
{ |
auto dataLeftSize = m_builder.CreateNUWSub(size64, idx64); |
||||
auto size = _size->a; // Trunc to 64-bit
|
auto outOfBound = m_builder.CreateICmpUGT(reqBytes, dataLeftSize); |
||||
auto& memory = _rt->getMemory(); |
auto bytesToCopyInner = m_builder.CreateSelect(outOfBound, dataLeftSize, reqBytes); |
||||
memory.resize(size); |
auto bytesToCopy = m_builder.CreateSelect(isOutsideData, m_builder.getInt64(0), bytesToCopyInner); |
||||
return memory.data(); |
|
||||
} |
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 |
||||
#pragma once |
|
||||
|
#include "RuntimeData.h" |
||||
#include <csetjmp> |
|
||||
#include "RuntimeData.h" |
namespace dev |
||||
|
{ |
||||
namespace dev |
namespace eth |
||||
{ |
{ |
||||
namespace eth |
namespace jit |
||||
{ |
{ |
||||
namespace jit |
|
||||
{ |
using StackImpl = std::vector<i256>; |
||||
|
using MemoryImpl = bytes; |
||||
using StackImpl = std::vector<i256>; |
|
||||
using MemoryImpl = bytes; |
class Runtime |
||||
using jmp_buf_ref = decltype(&std::jmp_buf{}[0]); |
{ |
||||
|
public: |
||||
class Runtime |
Runtime(RuntimeData* _data, Env* _env); |
||||
{ |
|
||||
public: |
Runtime(const Runtime&) = delete; |
||||
Runtime(RuntimeData* _data, Env* _env); |
Runtime& operator=(const Runtime&) = delete; |
||||
|
|
||||
Runtime(const Runtime&) = delete; |
StackImpl& getStack() { return m_stack; } |
||||
Runtime& operator=(const Runtime&) = delete; |
MemoryImpl& getMemory() { return m_memory; } |
||||
|
|
||||
StackImpl& getStack() { return m_stack; } |
bytes_ref getReturnData() const; |
||||
MemoryImpl& getMemory() { return m_memory; } |
|
||||
Env* getEnvPtr() { return &m_env; } |
private: |
||||
|
RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract.
|
||||
bytes_ref getReturnData() const; |
Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract.
|
||||
jmp_buf_ref getJmpBuf() { return m_jmpBuf; } |
void* m_currJmpBuf = nullptr; ///< Pointer to jump buffer. Expected by compiled contract.
|
||||
|
byte* m_memoryData = nullptr; |
||||
private: |
i256 m_memorySize; |
||||
RuntimeData& m_data; ///< Pointer to data. Expected by compiled contract.
|
StackImpl m_stack; |
||||
Env& m_env; ///< Pointer to environment proxy. Expected by compiled contract.
|
MemoryImpl m_memory; |
||||
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; |
|
||||
}; |
|
||||
|
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
@ -0,0 +1,7 @@ |
|||||
|
#if defined(_MSC_VER) |
||||
|
#pragma warning(pop) |
||||
|
#elif defined(__clang__) |
||||
|
#pragma clang diagnostic pop |
||||
|
#else |
||||
|
#pragma GCC diagnostic pop |
||||
|
#endif |
@ -0,0 +1,12 @@ |
|||||
|
#if defined(_MSC_VER) |
||||
|
#pragma warning(push) |
||||
|
#pragma warning(disable: 4267 4244 4800) |
||||
|
#elif defined(__clang__) |
||||
|
#pragma clang diagnostic push |
||||
|
#pragma clang diagnostic ignored "-Wunused-parameter" |
||||
|
#pragma clang diagnostic ignored "-Wconversion" |
||||
|
#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; |
||||
|
|
||||
|
} |
||||
|
} |
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue