diff --git a/libevmjit/BasicBlock.cpp b/libevmjit/BasicBlock.cpp index d233ea7441..bae16392e5 100644 --- a/libevmjit/BasicBlock.cpp +++ b/libevmjit/BasicBlock.cpp @@ -20,17 +20,16 @@ namespace jit const char* BasicBlock::NamePrefix = "Instr."; -BasicBlock::BasicBlock(ProgramCounter _beginInstIdx, ProgramCounter _endInstIdx, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder) : - m_beginInstIdx(_beginInstIdx), - m_endInstIdx(_endInstIdx), - m_llvmBB(llvm::BasicBlock::Create(_mainFunc->getContext(), {NamePrefix, std::to_string(_beginInstIdx)}, _mainFunc)), +BasicBlock::BasicBlock(bytes::const_iterator _begin, bytes::const_iterator _end, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder) : + m_begin(_begin), + m_end(_end), + // TODO: Add begin index to name + m_llvmBB(llvm::BasicBlock::Create(_mainFunc->getContext(), NamePrefix, _mainFunc)), m_stack(*this), m_builder(_builder) {} BasicBlock::BasicBlock(std::string _name, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder) : - m_beginInstIdx(0), - m_endInstIdx(0), m_llvmBB(llvm::BasicBlock::Create(_mainFunc->getContext(), _name, _mainFunc)), m_stack(*this), m_builder(_builder) diff --git a/libevmjit/BasicBlock.h b/libevmjit/BasicBlock.h index 0a04e5939c..7a36090a51 100644 --- a/libevmjit/BasicBlock.h +++ b/libevmjit/BasicBlock.h @@ -1,9 +1,7 @@ #pragma once - #include - #include - +#include "Common.h" #include "Stack.h" namespace dev @@ -52,10 +50,10 @@ public: BasicBlock& m_bblock; }; - /// Basic block name prefix. The rest is beging instruction index. + /// Basic block name prefix. The rest is instruction index. static const char* NamePrefix; - explicit BasicBlock(ProgramCounter _beginInstIdx, ProgramCounter _endInstIdx, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder); + explicit BasicBlock(bytes::const_iterator _begin, bytes::const_iterator _end, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder); explicit BasicBlock(std::string _name, llvm::Function* _mainFunc, llvm::IRBuilder<>& _builder); BasicBlock(const BasicBlock&) = delete; @@ -64,8 +62,8 @@ public: operator llvm::BasicBlock*() { return m_llvmBB; } // TODO: Remove it llvm::BasicBlock* llvm() { return m_llvmBB; } - ProgramCounter begin() { return m_beginInstIdx; } - ProgramCounter end() { return m_endInstIdx; } + bytes::const_iterator begin() { return m_begin; } + bytes::const_iterator end() { return m_end; } bool isJumpDest() const { return m_isJumpDest; } void markAsJumpDest() { m_isJumpDest = true; } @@ -85,8 +83,8 @@ public: void dump(std::ostream& os, bool _dotOutput = false); private: - ProgramCounter const m_beginInstIdx; - ProgramCounter const m_endInstIdx; + bytes::const_iterator const m_begin; + bytes::const_iterator const m_end; llvm::BasicBlock* const m_llvmBB; diff --git a/libevmjit/Compiler.cpp b/libevmjit/Compiler.cpp index c2f670eef5..7e1851c8ad 100644 --- a/libevmjit/Compiler.cpp +++ b/libevmjit/Compiler.cpp @@ -51,9 +51,9 @@ void Compiler::createBasicBlocks(bytes const& _bytecode) return _curr + offset; }; - ProgramCounter beginIdx = 0; + auto begin = _bytecode.begin(); bool nextJumpDest = false; - for (auto curr = _bytecode.begin(), next = curr; curr != _bytecode.end(); curr = next) + for (auto curr = begin, next = begin; curr != _bytecode.end(); curr = next) { next = skipPushDataAndGetNext(curr, _bytecode.end()); @@ -82,12 +82,12 @@ void Compiler::createBasicBlocks(bytes const& _bytecode) if (isEnd) { - auto nextIdx = next - _bytecode.begin(); - auto p = m_basicBlocks.emplace(std::piecewise_construct, std::forward_as_tuple(beginIdx), std::forward_as_tuple(beginIdx, nextIdx, m_mainFunc, m_builder)); + auto beginIdx = begin - _bytecode.begin(); + auto p = m_basicBlocks.emplace(std::piecewise_construct, std::forward_as_tuple(beginIdx), std::forward_as_tuple(begin, next, m_mainFunc, m_builder)); if (nextJumpDest) p.first->second.markAsJumpDest(); nextJumpDest = false; - beginIdx = nextIdx; + begin = next; } } @@ -212,10 +212,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode m_builder.SetInsertPoint(_basicBlock.llvm()); auto& stack = _basicBlock.localStack(); - auto begin = _bytecode.begin() + _basicBlock.begin(); - auto end = _bytecode.begin() + _basicBlock.end(); - - for (auto it = begin; it != end; ++it) + for (auto it = _basicBlock.begin(); it != _basicBlock.end(); ++it) { auto inst = Instruction(*it); @@ -479,7 +476,7 @@ void Compiler::compileBasicBlock(BasicBlock& _basicBlock, bytes const& _bytecode case Instruction::ANY_PUSH: { - auto value = readPushData(it, end); + auto value = readPushData(it, _basicBlock.end()); stack.push(Constant::get(value)); break; }