1/*
2 * Copyright (C) 2011, 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. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#ifndef DFGAdjacencyList_h
27#define DFGAdjacencyList_h
28
29#if ENABLE(DFG_JIT)
30
31#include "DFGCommon.h"
32#include "DFGEdge.h"
33
34namespace JSC { namespace DFG {
35
36class AdjacencyList {
37public:
38    enum Kind {
39        Fixed,
40        Variable
41    };
42
43    enum { Size = 3 };
44
45    AdjacencyList() { }
46
47    AdjacencyList(Kind kind)
48    {
49        if (kind == Variable) {
50            m_words[0].m_encodedWord = UINT_MAX;
51            m_words[1].m_encodedWord = UINT_MAX;
52        }
53    }
54
55    AdjacencyList(Kind kind, Edge child1, Edge child2, Edge child3)
56    {
57        ASSERT_UNUSED(kind, kind == Fixed);
58        initialize(child1, child2, child3);
59    }
60
61    AdjacencyList(Kind kind, unsigned firstChild, unsigned numChildren)
62    {
63        ASSERT_UNUSED(kind, kind == Variable);
64        setFirstChild(firstChild);
65        setNumChildren(numChildren);
66    }
67
68    const Edge& child(unsigned i) const
69    {
70        ASSERT(i < Size);
71        return m_words[i];
72    }
73
74    Edge& child(unsigned i)
75    {
76        ASSERT(i < Size);
77        return m_words[i];
78    }
79
80    void setChild(unsigned i, Edge nodeUse)
81    {
82        ASSERT(i < Size);
83        m_words[i] = nodeUse;
84    }
85
86    Edge child1() const { return child(0); }
87    Edge child2() const { return child(1); }
88    Edge child3() const { return child(2); }
89
90    Edge& child1() { return child(0); }
91    Edge& child2() { return child(1); }
92    Edge& child3() { return child(2); }
93
94    void setChild1(Edge nodeUse) { setChild(0, nodeUse); }
95    void setChild2(Edge nodeUse) { setChild(1, nodeUse); }
96    void setChild3(Edge nodeUse) { setChild(2, nodeUse); }
97
98    Edge child1Unchecked() const { return m_words[0]; }
99
100    Edge justOneChild() const
101    {
102        if (!!child1() && !child2()) {
103            ASSERT(!child3());
104            return child1();
105        }
106        return Edge();
107    }
108
109    void initialize(Edge child1, Edge child2, Edge child3)
110    {
111        child(0) = child1;
112        child(1) = child2;
113        child(2) = child3;
114    }
115
116    void initialize(Node* child1 = 0, Node* child2 = 0, Node* child3 = 0)
117    {
118        initialize(Edge(child1), Edge(child2), Edge(child3));
119    }
120
121    void reset()
122    {
123        initialize();
124    }
125
126    // Call this if you wish to remove an edge and the node treats the list of children.
127    void removeEdge(unsigned edgeIndex)
128    {
129        for (unsigned i = edgeIndex; i < Size - 1; ++i)
130            setChild(i, child(i + 1));
131        setChild(Size - 1, Edge());
132    }
133
134    unsigned firstChild() const
135    {
136        return m_words[0].m_encodedWord;
137    }
138    void setFirstChild(unsigned firstChild)
139    {
140        m_words[0].m_encodedWord = firstChild;
141    }
142
143    unsigned numChildren() const
144    {
145        return m_words[1].m_encodedWord;
146    }
147    void setNumChildren(unsigned numChildren)
148    {
149        m_words[1].m_encodedWord = numChildren;
150    }
151
152private:
153    Edge m_words[Size];
154};
155
156} } // namespace JSC::DFG
157
158#endif // ENABLE(DFG_JIT)
159
160#endif // DFGAdjacencyList_h
161