]> git.saurik.com Git - apple/javascriptcore.git/blob - dfg/DFGLazyNode.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / dfg / DFGLazyNode.h
1 /*
2 * Copyright (C) 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. ``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 DFGLazyNode_h
27 #define DFGLazyNode_h
28
29 #if ENABLE(DFG_JIT)
30
31 #include "DFGCommon.h"
32 #include "DFGInsertionSet.h"
33 #include <wtf/PrintStream.h>
34
35 namespace JSC { namespace DFG {
36
37 class LazyNode {
38 public:
39 static const size_t jsConstantTag = 0;
40 static const size_t doubleConstantTag = 1;
41 static const size_t int52ConstantTag = 2;
42
43 static const uintptr_t tagMask = 0x3;
44 static const uintptr_t pointerMask = ~tagMask;
45
46 explicit LazyNode(Node* node = nullptr)
47 : m_node(node)
48 , m_value(reinterpret_cast<uintptr_t>(nullptr))
49 {
50 if (node && node->isConstant())
51 setFrozenValue(node->constant(), node->op());
52 }
53
54 explicit LazyNode(FrozenValue* value, NodeType op = JSConstant)
55 : m_node(nullptr)
56 , m_value(reinterpret_cast<uintptr_t>(nullptr))
57 {
58 setFrozenValue(value, op);
59 }
60
61 LazyNode(std::nullptr_t)
62 : m_node(nullptr)
63 , m_value(reinterpret_cast<uintptr_t>(nullptr))
64 {
65 }
66
67 LazyNode(WTF::HashTableDeletedValueType)
68 : m_node(reinterpret_cast<Node*>(-1))
69 {
70 }
71
72 void setNode(Node* node)
73 {
74 m_node = node;
75 if (node && node->isConstant())
76 setFrozenValue(node->constant(), node->op());
77 }
78
79 bool isHashTableDeletedValue() const { return m_node == reinterpret_cast<Node*>(-1); }
80
81 bool isNode() const { return m_node; }
82
83 NodeType op() const
84 {
85 if (m_node)
86 return m_node->op();
87
88 switch (m_value & tagMask) {
89 case jsConstantTag:
90 return JSConstant;
91 case doubleConstantTag:
92 return DoubleConstant;
93 case int52ConstantTag:
94 return Int52Constant;
95 default:
96 RELEASE_ASSERT_NOT_REACHED();
97 }
98 }
99
100 Node* asNode() const
101 {
102 ASSERT(m_node || !asValue());
103 return m_node;
104 }
105
106 FrozenValue* asValue() const
107 {
108 return reinterpret_cast<FrozenValue*>(m_value & pointerMask);
109 }
110
111 unsigned hash() const
112 {
113 if (asValue())
114 return WTF::PtrHash<FrozenValue*>::hash(asValue());
115 return WTF::PtrHash<Node*>::hash(m_node);
116 }
117
118 bool operator==(const LazyNode& other) const
119 {
120 if (asValue() || other.asValue())
121 return m_value == other.m_value;
122 return m_node == other.m_node;
123 }
124
125 bool operator!=(const LazyNode& other) const
126 {
127 return !(*this == other);
128 }
129
130 Node* ensureIsNode(InsertionSet& insertionSet, BasicBlock* block, unsigned nodeIndex)
131 {
132 if (!m_node)
133 m_node = insertionSet.insertConstant(nodeIndex, block->at(nodeIndex)->origin, asValue(), op());
134
135 return asNode();
136 }
137
138 Node* operator->() const { return asNode(); }
139
140 Node& operator*() const { return *asNode(); }
141
142 bool operator!() const { return !asValue() && !asNode(); }
143
144 explicit operator bool() const { return !!*this; }
145
146 void dump(PrintStream& out) const;
147
148 private:
149 void setFrozenValue(FrozenValue* value, NodeType op)
150 {
151 ASSERT(value);
152 m_value = reinterpret_cast<uintptr_t>(value);
153 ASSERT(m_value == (m_value & pointerMask));
154 switch (op) {
155 case JSConstant:
156 m_value |= jsConstantTag;
157 break;
158 case DoubleConstant:
159 m_value |= doubleConstantTag;
160 break;
161 case Int52Constant:
162 m_value |= int52ConstantTag;
163 break;
164 default:
165 RELEASE_ASSERT_NOT_REACHED();
166 break;
167 }
168 }
169
170 Node* m_node;
171 uintptr_t m_value;
172 };
173
174 } } // namespace JSC::DFG
175
176 namespace WTF {
177
178 template<typename T> struct HashTraits;
179 template<> struct HashTraits<JSC::DFG::LazyNode> : SimpleClassHashTraits<JSC::DFG::LazyNode> {
180 static const bool emptyValueIsZero = true;
181 };
182
183 } // namespace WTF
184
185 #endif // ENABLE(DFG_JIT)
186
187 #endif // DFGLazyNode_h