diff --git a/evmjit/libevmjit/BasicBlock.cpp b/evmjit/libevmjit/BasicBlock.cpp index 53bbe8b12..b9534e40f 100644 --- a/evmjit/libevmjit/BasicBlock.cpp +++ b/evmjit/libevmjit/BasicBlock.cpp @@ -38,17 +38,17 @@ LocalStack::LocalStack(Stack& _globalStack): void LocalStack::push(llvm::Value* _value) { assert(_value->getType() == Type::Word); - m_currentStack.push_back(_value); + m_local.push_back(_value); m_maxSize = std::max(m_maxSize, size()); } llvm::Value* LocalStack::pop() { auto item = get(0); - assert(!m_currentStack.empty() || !m_initialStack.empty()); + assert(!m_local.empty() || !m_input.empty()); - if (m_currentStack.size() > 0) - m_currentStack.pop_back(); + if (m_local.size() > 0) + m_local.pop_back(); else ++m_globalPops; @@ -80,13 +80,13 @@ void LocalStack::swap(size_t _index) llvm::Value* LocalStack::get(size_t _index) { - if (_index < m_currentStack.size()) - return *(m_currentStack.rbegin() + _index); // count from back + if (_index < m_local.size()) + return *(m_local.rbegin() + _index); // count from back - auto idx = _index - m_currentStack.size() + m_globalPops; - if (idx >= m_initialStack.size()) - m_initialStack.resize(idx + 1); - auto& item = m_initialStack[idx]; + auto idx = _index - m_local.size() + m_globalPops; + if (idx >= m_input.size()) + m_input.resize(idx + 1); + auto& item = m_input[idx]; if (!item) item = m_global.get(idx); @@ -96,15 +96,15 @@ llvm::Value* LocalStack::get(size_t _index) void LocalStack::set(size_t _index, llvm::Value* _word) { - if (_index < m_currentStack.size()) + if (_index < m_local.size()) { - *(m_currentStack.rbegin() + _index) = _word; + *(m_local.rbegin() + _index) = _word; return; } - auto idx = _index - m_currentStack.size() + m_globalPops; - assert(idx < m_initialStack.size()); - m_initialStack[idx] = _word; + auto idx = _index - m_local.size() + m_globalPops; + assert(idx < m_input.size()); + m_input[idx] = _word; } @@ -118,15 +118,15 @@ void LocalStack::finalize(llvm::IRBuilder<>& _builder, llvm::BasicBlock& _bb) _builder.SetInsertPoint(blockTerminator); // Update items fetched from global stack ignoring the poped ones - assert(m_globalPops <= m_initialStack.size()); // pop() always does get() - for (auto i = m_globalPops; i < m_initialStack.size(); ++i) + assert(m_globalPops <= m_input.size()); // pop() always does get() + for (auto i = m_globalPops; i < m_input.size(); ++i) { - if (m_initialStack[i]) - m_global.set(i, m_initialStack[i]); + if (m_input[i]) + m_global.set(i, m_input[i]); } // Add new items - for (auto& item: m_currentStack) + for (auto& item: m_local) { if (m_globalPops) // Override poped global items m_global.set(--m_globalPops, item); // using pops counter as the index diff --git a/evmjit/libevmjit/BasicBlock.h b/evmjit/libevmjit/BasicBlock.h index c79088ffc..14c7111f8 100644 --- a/evmjit/libevmjit/BasicBlock.h +++ b/evmjit/libevmjit/BasicBlock.h @@ -14,8 +14,6 @@ namespace jit using namespace evmjit; using instr_idx = uint64_t; -class BasicBlock; - class LocalStack { public: @@ -36,7 +34,7 @@ public: /// @param _index Index of value to be swaped. Must be > 0. void swap(size_t _index); - ssize_t size() const { return static_cast(m_currentStack.size()) - static_cast(m_globalPops); } + ssize_t size() const { return static_cast(m_local.size()) - static_cast(m_globalPops); } ssize_t minSize() const { return m_minSize; } ssize_t maxSize() const { return m_maxSize; } @@ -50,18 +48,12 @@ private: /// Sets _index'th value from top (counting from 0) void set(size_t _index, llvm::Value* _value); - /// This stack contains LLVM values that correspond to items found at - /// the EVM stack when the current basic block starts executing. - /// Location 0 corresponds to the top of the EVM stack, location 1 is - /// the item below the top and so on. The stack grows as the code - /// accesses more items on the EVM stack but once a value is put on - /// the stack, it will never be replaced. - std::vector m_initialStack; - - /// This stack tracks the contents of the EVM stack as the basic block - /// executes. It may grow on both sides, as the code pushes items on - /// top of the stack or changes existing items. - std::vector m_currentStack; + /// Items fetched from global stack. First element matches the top of the global stack. + /// Can contain nulls if some items has been skipped. + std::vector m_input; + + /// Local stack items that has not been pushed to global stack. First item is just above global stack. + std::vector m_local; Stack& m_global; ///< Reference to global stack.