diff --git a/evmjit/libevmjit-cpp/Env.cpp b/evmjit/libevmjit-cpp/Env.cpp index 90474c7e4..f89897792 100644 --- a/evmjit/libevmjit-cpp/Env.cpp +++ b/evmjit/libevmjit-cpp/Env.cpp @@ -3,7 +3,7 @@ #include #include -#include +#include "Utils.h" extern "C" { @@ -16,7 +16,6 @@ extern "C" using namespace dev; using namespace dev::eth; using jit::i256; - using jit::eth2llvm; EXPORT void env_sload(ExtVMFace* _env, i256* _index, i256* o_value) { diff --git a/evmjit/libevmjit-cpp/JitVM.cpp b/evmjit/libevmjit-cpp/JitVM.cpp index f1bba9e7f..ededbff52 100644 --- a/evmjit/libevmjit-cpp/JitVM.cpp +++ b/evmjit/libevmjit-cpp/JitVM.cpp @@ -2,7 +2,7 @@ #include "JitVM.h" #include #include -#include +#include "Utils.h" namespace dev { @@ -13,28 +13,28 @@ bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const&, uint64_t) { using namespace jit; - m_data.set(RuntimeData::Gas, m_gas); - m_data.set(RuntimeData::Address, fromAddress(_ext.myAddress)); - m_data.set(RuntimeData::Caller, fromAddress(_ext.caller)); - m_data.set(RuntimeData::Origin, fromAddress(_ext.origin)); - m_data.set(RuntimeData::CallValue, _ext.value); - m_data.set(RuntimeData::CallDataSize, _ext.data.size()); - m_data.set(RuntimeData::GasPrice, _ext.gasPrice); - m_data.set(RuntimeData::CoinBase, fromAddress(_ext.currentBlock.coinbaseAddress)); - m_data.set(RuntimeData::TimeStamp, _ext.currentBlock.timestamp); - m_data.set(RuntimeData::Number, _ext.currentBlock.number); - m_data.set(RuntimeData::Difficulty, _ext.currentBlock.difficulty); - m_data.set(RuntimeData::GasLimit, _ext.currentBlock.gasLimit); - m_data.set(RuntimeData::CodeSize, _ext.code.size()); + m_data.elems[RuntimeData::Gas] = eth2llvm(m_gas); + m_data.elems[RuntimeData::Address] = eth2llvm(fromAddress(_ext.myAddress)); + m_data.elems[RuntimeData::Caller] = eth2llvm(fromAddress(_ext.caller)); + m_data.elems[RuntimeData::Origin] = eth2llvm(fromAddress(_ext.origin)); + m_data.elems[RuntimeData::CallValue] = eth2llvm(_ext.value); + m_data.elems[RuntimeData::CallDataSize] = eth2llvm(_ext.data.size()); + m_data.elems[RuntimeData::GasPrice] = eth2llvm(_ext.gasPrice); + m_data.elems[RuntimeData::CoinBase] = eth2llvm(fromAddress(_ext.currentBlock.coinbaseAddress)); + m_data.elems[RuntimeData::TimeStamp] = eth2llvm(_ext.currentBlock.timestamp); + m_data.elems[RuntimeData::Number] = eth2llvm(_ext.currentBlock.number); + m_data.elems[RuntimeData::Difficulty] = eth2llvm(_ext.currentBlock.difficulty); + m_data.elems[RuntimeData::GasLimit] = eth2llvm(_ext.currentBlock.gasLimit); + m_data.elems[RuntimeData::CodeSize] = eth2llvm(_ext.code.size()); m_data.callData = _ext.data.data(); - m_data.code = _ext.code.data(); + m_data.code = _ext.code.data(); auto env = reinterpret_cast(&_ext); auto exitCode = m_engine.run(_ext.code, &m_data, env); switch (exitCode) { case ReturnCode::Suicide: - _ext.suicide(right160(m_data.get(RuntimeData::SuicideDestAddress))); + _ext.suicide(right160(llvm2eth(m_data.elems[RuntimeData::SuicideDestAddress]))); break; case ReturnCode::BadJumpDestination: diff --git a/evmjit/libevmjit-cpp/Utils.h b/evmjit/libevmjit-cpp/Utils.h new file mode 100644 index 000000000..ac796920b --- /dev/null +++ b/evmjit/libevmjit-cpp/Utils.h @@ -0,0 +1,38 @@ +#pragma once + +#include + +namespace dev +{ +namespace eth +{ + +inline u256 llvm2eth(jit::i256 _i) +{ + u256 u = 0; + u |= _i.d; + u <<= 64; + u |= _i.c; + u <<= 64; + u |= _i.b; + u <<= 64; + u |= _i.a; + return u; +} + +inline jit::i256 eth2llvm(u256 _u) +{ + jit::i256 i; + u256 mask = 0xFFFFFFFFFFFFFFFF; + i.a = static_cast(_u & mask); + _u >>= 64; + i.b = static_cast(_u & mask); + _u >>= 64; + i.c = static_cast(_u & mask); + _u >>= 64; + i.d = static_cast(_u & mask); + return i; +} + +} +} diff --git a/evmjit/libevmjit/Arith256.cpp b/evmjit/libevmjit/Arith256.cpp index c33a3cf3a..adb6cf9d5 100644 --- a/evmjit/libevmjit/Arith256.cpp +++ b/evmjit/libevmjit/Arith256.cpp @@ -102,27 +102,6 @@ llvm::Value* Arith256::mulmod(llvm::Value* _arg1, llvm::Value* _arg2, llvm::Valu namespace { - using s256 = boost::multiprecision::int256_t; - - inline s256 u2s(u256 _u) - { - static const bigint c_end = (bigint)1 << 256; - static const u256 c_send = (u256)1 << 255; - if (_u < c_send) - return (s256)_u; - else - return (s256)-(c_end - _u); - } - - inline u256 s2u(s256 _u) - { - static const bigint c_end = (bigint)1 << 256; - if (_u >= 0) - return (u256)_u; - else - return (u256)(c_end + _u); - } - using uint128 = __uint128_t; // uint128 add(uint128 a, uint128 b) { return a + b; } diff --git a/evmjit/libevmjit/Common.h b/evmjit/libevmjit/Common.h index 2d35b21ac..179c7ab38 100644 --- a/evmjit/libevmjit/Common.h +++ b/evmjit/libevmjit/Common.h @@ -35,7 +35,6 @@ enum class ReturnCode }; /// Representation of 256-bit value binary compatible with LLVM i256 -// TODO: Replace with h256 struct i256 { uint64_t a = 0; diff --git a/evmjit/libevmjit/Runtime.cpp b/evmjit/libevmjit/Runtime.cpp index 443c19ba6..989c053dc 100644 --- a/evmjit/libevmjit/Runtime.cpp +++ b/evmjit/libevmjit/Runtime.cpp @@ -21,8 +21,8 @@ Runtime::Runtime(RuntimeData* _data, Env* _env) : bytes_ref Runtime::getReturnData() const { // TODO: Handle large indexes - auto offset = static_cast(llvm2eth(m_data.elems[RuntimeData::ReturnDataOffset])); - auto size = static_cast(llvm2eth(m_data.elems[RuntimeData::ReturnDataSize])); + auto offset = static_cast(m_data.elems[RuntimeData::ReturnDataOffset].a); + auto size = static_cast(m_data.elems[RuntimeData::ReturnDataSize].a); assert(offset + size <= m_memory.size() || size == 0); if (offset + size > m_memory.size()) diff --git a/evmjit/libevmjit/RuntimeData.h b/evmjit/libevmjit/RuntimeData.h index bb52f7864..a71cd468b 100644 --- a/evmjit/libevmjit/RuntimeData.h +++ b/evmjit/libevmjit/RuntimeData.h @@ -38,9 +38,6 @@ struct RuntimeData i256 elems[_size] = {}; byte const* callData = nullptr; byte const* code = nullptr; - - void set(Index _index, u256 _value) { elems[_index] = eth2llvm(_value); } - u256 get(Index _index) { return llvm2eth(elems[_index]); } }; /// VM Environment (ExtVM) opaque type diff --git a/evmjit/libevmjit/Utils.cpp b/evmjit/libevmjit/Utils.cpp index 0fd9c0e41..5f7f75bfd 100644 --- a/evmjit/libevmjit/Utils.cpp +++ b/evmjit/libevmjit/Utils.cpp @@ -8,32 +8,6 @@ namespace eth namespace jit { -u256 llvm2eth(i256 _i) -{ - u256 u = 0; - u |= _i.d; - u <<= 64; - u |= _i.c; - u <<= 64; - u |= _i.b; - u <<= 64; - u |= _i.a; - return u; -} - -i256 eth2llvm(u256 _u) -{ - i256 i; - u256 mask = 0xFFFFFFFFFFFFFFFF; - i.a = static_cast(_u & mask); - _u >>= 64; - i.b = static_cast(_u & mask); - _u >>= 64; - i.c = static_cast(_u & mask); - _u >>= 64; - i.d = static_cast(_u & mask); - return i; -} } } diff --git a/evmjit/libevmjit/Utils.h b/evmjit/libevmjit/Utils.h index f672365c6..1e6705667 100644 --- a/evmjit/libevmjit/Utils.h +++ b/evmjit/libevmjit/Utils.h @@ -14,9 +14,6 @@ struct JIT: public NoteChannel { static const char* name() { return "JIT"; } }; //#define clog(CHANNEL) std::cerr #define clog(CHANNEL) std::ostream(nullptr) -u256 llvm2eth(i256); -i256 eth2llvm(u256); - } } }