| 1 | /* |
| 2 | * Copyright (C) 2013, 2015 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
| 14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
| 17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | * THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #pragma once |
| 27 | |
| 28 | #include "InstructionStream.h" |
| 29 | #include <limits.h> |
| 30 | #include <wtf/FastBitVector.h> |
| 31 | #include <wtf/Vector.h> |
| 32 | |
| 33 | namespace JSC { |
| 34 | |
| 35 | class CodeBlock; |
| 36 | class UnlinkedCodeBlock; |
| 37 | struct Instruction; |
| 38 | |
| 39 | class BytecodeBasicBlock { |
| 40 | WTF_MAKE_FAST_ALLOCATED; |
| 41 | public: |
| 42 | enum SpecialBlockType { EntryBlock, ExitBlock }; |
| 43 | BytecodeBasicBlock(const InstructionStream::Ref&); |
| 44 | BytecodeBasicBlock(SpecialBlockType); |
| 45 | void shrinkToFit(); |
| 46 | |
| 47 | bool isEntryBlock() { return !m_leaderOffset && !m_totalLength; } |
| 48 | bool isExitBlock() { return m_leaderOffset == UINT_MAX && m_totalLength == UINT_MAX; } |
| 49 | |
| 50 | unsigned leaderOffset() const { return m_leaderOffset; } |
| 51 | unsigned totalLength() const { return m_totalLength; } |
| 52 | |
| 53 | const Vector<InstructionStream::Offset>& offsets() const { return m_offsets; } |
| 54 | |
| 55 | const Vector<BytecodeBasicBlock*>& successors() const { return m_successors; } |
| 56 | |
| 57 | FastBitVector& in() { return m_in; } |
| 58 | FastBitVector& out() { return m_out; } |
| 59 | |
| 60 | unsigned index() const { return m_index; } |
| 61 | |
| 62 | static void compute(CodeBlock*, const InstructionStream& instructions, Vector<std::unique_ptr<BytecodeBasicBlock>>&); |
| 63 | static void compute(UnlinkedCodeBlock*, const InstructionStream& instructions, Vector<std::unique_ptr<BytecodeBasicBlock>>&); |
| 64 | |
| 65 | private: |
| 66 | template<typename Block> static void computeImpl(Block* codeBlock, const InstructionStream& instructions, Vector<std::unique_ptr<BytecodeBasicBlock>>& basicBlocks); |
| 67 | |
| 68 | void addSuccessor(BytecodeBasicBlock* block) { m_successors.append(block); } |
| 69 | |
| 70 | void addLength(unsigned); |
| 71 | |
| 72 | InstructionStream::Offset m_leaderOffset; |
| 73 | unsigned m_totalLength; |
| 74 | unsigned m_index; |
| 75 | |
| 76 | Vector<InstructionStream::Offset> m_offsets; |
| 77 | Vector<BytecodeBasicBlock*> m_successors; |
| 78 | |
| 79 | FastBitVector m_in; |
| 80 | FastBitVector m_out; |
| 81 | }; |
| 82 | |
| 83 | inline BytecodeBasicBlock::BytecodeBasicBlock(const InstructionStream::Ref& instruction) |
| 84 | : m_leaderOffset(instruction.offset()) |
| 85 | , m_totalLength(instruction->size()) |
| 86 | { |
| 87 | m_offsets.append(m_leaderOffset); |
| 88 | } |
| 89 | |
| 90 | inline BytecodeBasicBlock::BytecodeBasicBlock(BytecodeBasicBlock::SpecialBlockType blockType) |
| 91 | : m_leaderOffset(blockType == BytecodeBasicBlock::EntryBlock ? 0 : UINT_MAX) |
| 92 | , m_totalLength(blockType == BytecodeBasicBlock::EntryBlock ? 0 : UINT_MAX) |
| 93 | { |
| 94 | } |
| 95 | |
| 96 | inline void BytecodeBasicBlock::addLength(unsigned bytecodeLength) |
| 97 | { |
| 98 | m_offsets.append(m_leaderOffset + m_totalLength); |
| 99 | m_totalLength += bytecodeLength; |
| 100 | } |
| 101 | |
| 102 | } // namespace JSC |
| 103 | |