1/*
2 * Copyright (C) 2013 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#ifndef BytecodeBasicBlock_h
27#define BytecodeBasicBlock_h
28
29#include <limits.h>
30#include <wtf/FastBitVector.h>
31#include <wtf/HashMap.h>
32#include <wtf/RefCounted.h>
33#include <wtf/Vector.h>
34
35namespace JSC {
36
37class CodeBlock;
38
39class BytecodeBasicBlock : public RefCounted<BytecodeBasicBlock> {
40public:
41    enum SpecialBlockType { EntryBlock, ExitBlock };
42    BytecodeBasicBlock(unsigned start, unsigned length);
43    BytecodeBasicBlock(SpecialBlockType);
44
45    bool isEntryBlock() { return !m_leaderBytecodeOffset && !m_totalBytecodeLength; }
46    bool isExitBlock() { return m_leaderBytecodeOffset == UINT_MAX && m_totalBytecodeLength == UINT_MAX; }
47
48    unsigned leaderBytecodeOffset() { return m_leaderBytecodeOffset; }
49    unsigned totalBytecodeLength() { return m_totalBytecodeLength; }
50
51    Vector<unsigned>& bytecodeOffsets() { return m_bytecodeOffsets; }
52    void addBytecodeLength(unsigned);
53
54    void addPredecessor(BytecodeBasicBlock* block) { m_predecessors.append(block); }
55    void addSuccessor(BytecodeBasicBlock* block) { m_successors.append(block); }
56
57    Vector<BytecodeBasicBlock*>& predecessors() { return m_predecessors; }
58    Vector<BytecodeBasicBlock*>& successors() { return m_successors; }
59
60    FastBitVector& in() { return m_in; }
61    FastBitVector& out() { return m_out; }
62
63private:
64    unsigned m_leaderBytecodeOffset;
65    unsigned m_totalBytecodeLength;
66
67    Vector<unsigned> m_bytecodeOffsets;
68
69    Vector<BytecodeBasicBlock*> m_predecessors;
70    Vector<BytecodeBasicBlock*> m_successors;
71
72    FastBitVector m_in;
73    FastBitVector m_out;
74};
75
76void computeBytecodeBasicBlocks(CodeBlock*, Vector<RefPtr<BytecodeBasicBlock> >&);
77
78inline BytecodeBasicBlock::BytecodeBasicBlock(unsigned start, unsigned length)
79    : m_leaderBytecodeOffset(start)
80    , m_totalBytecodeLength(length)
81{
82    m_bytecodeOffsets.append(m_leaderBytecodeOffset);
83}
84
85inline BytecodeBasicBlock::BytecodeBasicBlock(BytecodeBasicBlock::SpecialBlockType blockType)
86    : m_leaderBytecodeOffset(blockType == BytecodeBasicBlock::EntryBlock ? 0 : UINT_MAX)
87    , m_totalBytecodeLength(blockType == BytecodeBasicBlock::EntryBlock ? 0 : UINT_MAX)
88{
89}
90
91inline void BytecodeBasicBlock::addBytecodeLength(unsigned bytecodeLength)
92{
93    m_bytecodeOffsets.append(m_leaderBytecodeOffset + m_totalBytecodeLength);
94    m_totalBytecodeLength += bytecodeLength;
95}
96
97} // namespace JSC
98
99#endif // BytecodeBasicBlock_h
100