From 0f294ec820a87605ab5181d33ced0c81a414e49b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Bylica?= Date: Mon, 26 Jan 2015 15:21:23 +0100 Subject: [PATCH] Reorder JIT runtime data fields --- libevmjit-cpp/JitVM.cpp | 26 +++++++++++++---------- libevmjit/Compiler.cpp | 14 +++++-------- libevmjit/RuntimeData.h | 38 +++++++++++++++++----------------- libevmjit/RuntimeManager.cpp | 40 +++++++++++++----------------------- libevmjit/RuntimeManager.h | 2 -- 5 files changed, 53 insertions(+), 67 deletions(-) diff --git a/libevmjit-cpp/JitVM.cpp b/libevmjit-cpp/JitVM.cpp index f08534bed..613b5031c 100644 --- a/libevmjit-cpp/JitVM.cpp +++ b/libevmjit-cpp/JitVM.cpp @@ -14,29 +14,33 @@ bytesConstRef JitVM::go(ExtVMFace& _ext, OnOpFunc const&, uint64_t) using namespace jit; if (m_gas > std::numeric_limits::max()) - BOOST_THROW_EXCEPTION(OutOfGas()); // Do not accept requests with gas > 2^63 (int64 max) + BOOST_THROW_EXCEPTION(OutOfGas()); // Do not accept requests with gas > 2^63 (int64 max) // TODO: Return "not accepted" exception to allow interpreted handle that - if (_ext.currentBlock.number > std::numeric_limits::max()) + if (_ext.gasPrice > std::numeric_limits::max()) BOOST_THROW_EXCEPTION(OutOfGas()); - if (_ext.currentBlock.timestamp > std::numeric_limits::max()) + if (_ext.currentBlock.number > std::numeric_limits::max()) BOOST_THROW_EXCEPTION(OutOfGas()); + if (_ext.currentBlock.timestamp > std::numeric_limits::max()) + BOOST_THROW_EXCEPTION(OutOfGas()); + + + m_data.gas = static_cast(m_gas); + m_data.gasPrice = static_cast(_ext.gasPrice); + m_data.callData = _ext.data.data(); + m_data.callDataSize = _ext.data.size(); m_data.address = eth2llvm(fromAddress(_ext.myAddress)); m_data.caller = eth2llvm(fromAddress(_ext.caller)); m_data.origin = eth2llvm(fromAddress(_ext.origin)); m_data.callValue = eth2llvm(_ext.value); - m_data.gasPrice = eth2llvm(_ext.gasPrice); m_data.coinBase = eth2llvm(fromAddress(_ext.currentBlock.coinbaseAddress)); m_data.difficulty = eth2llvm(_ext.currentBlock.difficulty); m_data.gasLimit = eth2llvm(_ext.currentBlock.gasLimit); - m_data.callData = _ext.data.data(); - m_data.code = _ext.code.data(); - m_data.codeSize = _ext.code.size(); - m_data.callDataSize = _ext.data.size(); - m_data.gas = static_cast(m_gas); - m_data.blockNumber = static_cast(_ext.currentBlock.number); - m_data.blockTimestamp = static_cast(_ext.currentBlock.timestamp); + m_data.number = static_cast(_ext.currentBlock.number); + m_data.timestamp = static_cast(_ext.currentBlock.timestamp); + m_data.code = _ext.code.data(); + m_data.codeSize = _ext.code.size(); auto env = reinterpret_cast(&_ext); auto exitCode = m_engine.run(_ext.code, &m_data, env); diff --git a/libevmjit/Compiler.cpp b/libevmjit/Compiler.cpp index d109cadfc..439ff065c 100644 --- a/libevmjit/Compiler.cpp +++ b/libevmjit/Compiler.cpp @@ -641,9 +641,13 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode case Instruction::COINBASE: case Instruction::DIFFICULTY: case Instruction::GASLIMIT: + case Instruction::NUMBER: + case Instruction::TIMESTAMP: { // Pushes an element of runtime data on stack - stack.push(_runtimeManager.get(inst)); + auto value = _runtimeManager.get(inst); + value = m_builder.CreateZExt(value, Type::Word); + stack.push(value); break; } @@ -656,14 +660,6 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode stack.push(_runtimeManager.getCallDataSize()); break; - case Instruction::NUMBER: - stack.push(_runtimeManager.getBlockNumber()); - break; - - case Instruction::TIMESTAMP: - stack.push(_runtimeManager.getBlockTimestamp()); - break; - case Instruction::BLOCKHASH: { auto number = stack.pop(); diff --git a/libevmjit/RuntimeData.h b/libevmjit/RuntimeData.h index 357750078..846f7a238 100644 --- a/libevmjit/RuntimeData.h +++ b/libevmjit/RuntimeData.h @@ -14,42 +14,42 @@ struct RuntimeData { enum Index { + Gas, + GasPrice, + CallData, + CallDataSize, Address, Caller, Origin, CallValue, - GasPrice, CoinBase, Difficulty, GasLimit, - CallData, + Number, + Timestamp, Code, CodeSize, - CallDataSize, - Gas, - BlockNumber, - BlockTimestamp, SuicideDestAddress = Address, ///< Suicide balance destination address ReturnData = CallData, ///< Return data pointer (set only in case of RETURN) ReturnDataSize = CallDataSize, ///< Return data size (set only in case of RETURN) }; - i256 address; - i256 caller; - i256 origin; - i256 callValue; - i256 gasPrice; - i256 coinBase; - i256 difficulty; - i256 gasLimit; + int64_t gas = 0; + int64_t gasPrice = 0; byte const* callData = nullptr; + uint64_t callDataSize = 0; + i256 address; + i256 caller; + i256 origin; + i256 callValue; + i256 coinBase; + i256 difficulty; + i256 gasLimit; + uint64_t number = 0; + uint64_t timestamp = 0; byte const* code = nullptr; - uint64_t codeSize = 0; - uint64_t callDataSize = 0; - int64_t gas = 0; - uint64_t blockNumber = 0; - uint64_t blockTimestamp = 0; + uint64_t codeSize = 0; }; /// VM Environment (ExtVM) opaque type diff --git a/libevmjit/RuntimeManager.cpp b/libevmjit/RuntimeManager.cpp index 8e779805a..4021ad46f 100644 --- a/libevmjit/RuntimeManager.cpp +++ b/libevmjit/RuntimeManager.cpp @@ -22,21 +22,21 @@ llvm::StructType* RuntimeManager::getRuntimeDataType() { llvm::Type* elems[] = { + Type::Size, // gas + Type::Size, // gasPrice + Type::BytePtr, // callData + Type::Size, // callDataSize Type::Word, // address Type::Word, // caller Type::Word, // origin - Type::Word, // address - Type::Word, // address - Type::Word, // address - Type::Word, // address - Type::Word, // address - Type::BytePtr, // callData - Type::BytePtr, // code - Type::Size, // codeSize - Type::Size, // callDataSize - Type::Size, // gas + Type::Word, // callValue + Type::Word, // coinBase + Type::Word, // difficulty + Type::Word, // gasLimit Type::Size, // blockNumber Type::Size, // blockTimestamp + Type::BytePtr, // code + Type::Size, // codeSize }; type = llvm::StructType::create(elems, "RuntimeData"); } @@ -81,8 +81,8 @@ llvm::Twine getName(RuntimeData::Index _index) case RuntimeData::CodeSize: return "code"; case RuntimeData::CallDataSize: return "callDataSize"; case RuntimeData::Gas: return "gas"; - case RuntimeData::BlockNumber: return "number"; - case RuntimeData::BlockTimestamp: return "timestamp"; + case RuntimeData::Number: return "number"; + case RuntimeData::Timestamp: return "timestamp"; } } } @@ -178,6 +178,8 @@ llvm::Value* RuntimeManager::get(Instruction _inst) case Instruction::COINBASE: return get(RuntimeData::CoinBase); case Instruction::DIFFICULTY: return get(RuntimeData::Difficulty); case Instruction::GASLIMIT: return get(RuntimeData::GasLimit); + case Instruction::NUMBER: return get(RuntimeData::Number); + case Instruction::TIMESTAMP: return get(RuntimeData::Timestamp); } } @@ -205,20 +207,6 @@ llvm::Value* RuntimeManager::getCallDataSize() return getBuilder().CreateZExt(value, Type::Word); } -llvm::Value* RuntimeManager::getBlockNumber() -{ - auto value = get(RuntimeData::BlockNumber); - assert(value->getType() == Type::Size); - return getBuilder().CreateZExt(value, Type::Word); -} - -llvm::Value* RuntimeManager::getBlockTimestamp() -{ - auto value = get(RuntimeData::BlockTimestamp); - assert(value->getType() == Type::Size); - return getBuilder().CreateZExt(value, Type::Word); -} - llvm::Value* RuntimeManager::getJmpBuf() { auto ptr = getBuilder().CreateStructGEP(getRuntimePtr(), 2, "jmpbufPtr"); diff --git a/libevmjit/RuntimeManager.h b/libevmjit/RuntimeManager.h index 5e2eb7550..b5f3ca657 100644 --- a/libevmjit/RuntimeManager.h +++ b/libevmjit/RuntimeManager.h @@ -28,8 +28,6 @@ public: llvm::Value* getCode(); llvm::Value* getCodeSize(); llvm::Value* getCallDataSize(); - llvm::Value* getBlockNumber(); - llvm::Value* getBlockTimestamp(); void setGas(llvm::Value* _gas); void registerReturnData(llvm::Value* _index, llvm::Value* _size);