1/*
2 * Copyright (C) 2008, 2009 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 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef Register_h
30#define Register_h
31
32#include "JSCJSValue.h"
33#include <wtf/Assertions.h>
34#include <wtf/VectorTraits.h>
35
36namespace JSC {
37
38 class CodeBlock;
39 class ExecState;
40 class JSLexicalEnvironment;
41 class JSObject;
42 class JSScope;
43
44 typedef ExecState CallFrame;
45
46 class Register {
47 WTF_MAKE_FAST_ALLOCATED;
48 public:
49 Register();
50
51 Register(const JSValue&);
52 Register& operator=(const JSValue&);
53 JSValue jsValue() const;
54 JSValue asanUnsafeJSValue() const;
55 EncodedJSValue encodedJSValue() const;
56
57 Register& operator=(CallFrame*);
58 Register& operator=(CodeBlock*);
59 Register& operator=(JSScope*);
60 Register& operator=(JSObject*);
61
62 int32_t i() const;
63 CallFrame* callFrame() const;
64 CodeBlock* codeBlock() const;
65 CodeBlock* asanUnsafeCodeBlock() const;
66 JSObject* object() const;
67 JSScope* scope() const;
68 int32_t unboxedInt32() const;
69 int64_t unboxedInt52() const;
70 int64_t unboxedStrictInt52() const;
71 bool unboxedBoolean() const;
72 double unboxedDouble() const;
73 JSCell* unboxedCell() const;
74 int32_t payload() const;
75 int32_t tag() const;
76 int32_t unsafeTag() const;
77 int32_t& payload();
78 int32_t& tag();
79
80 static Register withInt(int32_t i)
81 {
82 Register r = jsNumber(i);
83 return r;
84 }
85
86 private:
87 union {
88 EncodedJSValue value;
89 CallFrame* callFrame;
90 CodeBlock* codeBlock;
91 EncodedValueDescriptor encodedValue;
92 double number;
93 int64_t integer;
94 } u;
95 };
96
97 ALWAYS_INLINE Register::Register()
98 {
99#ifndef NDEBUG
100 *this = JSValue();
101#endif
102 }
103
104 ALWAYS_INLINE Register::Register(const JSValue& v)
105 {
106 u.value = JSValue::encode(v);
107 }
108
109 ALWAYS_INLINE Register& Register::operator=(const JSValue& v)
110 {
111 u.value = JSValue::encode(v);
112 return *this;
113 }
114
115 // FIXME (rdar://problem/19379214): ASan only needs to be suppressed for Register::jsValue() when called from prepareOSREntry(), but there is currently no way to express this short of adding a separate copy of the function.
116 SUPPRESS_ASAN ALWAYS_INLINE JSValue Register::asanUnsafeJSValue() const
117 {
118 return JSValue::decode(u.value);
119 }
120
121 ALWAYS_INLINE JSValue Register::jsValue() const
122 {
123 return JSValue::decode(u.value);
124 }
125
126 ALWAYS_INLINE EncodedJSValue Register::encodedJSValue() const
127 {
128 return u.value;
129 }
130
131 // Interpreter functions
132
133 ALWAYS_INLINE Register& Register::operator=(CallFrame* callFrame)
134 {
135 u.callFrame = callFrame;
136 return *this;
137 }
138
139 ALWAYS_INLINE Register& Register::operator=(CodeBlock* codeBlock)
140 {
141 u.codeBlock = codeBlock;
142 return *this;
143 }
144
145 ALWAYS_INLINE int32_t Register::i() const
146 {
147 return jsValue().asInt32();
148 }
149
150 ALWAYS_INLINE CallFrame* Register::callFrame() const
151 {
152 return u.callFrame;
153 }
154
155 ALWAYS_INLINE CodeBlock* Register::codeBlock() const
156 {
157 return u.codeBlock;
158 }
159
160 SUPPRESS_ASAN ALWAYS_INLINE CodeBlock* Register::asanUnsafeCodeBlock() const
161 {
162 return u.codeBlock;
163 }
164
165 ALWAYS_INLINE int32_t Register::unboxedInt32() const
166 {
167 return payload();
168 }
169
170 ALWAYS_INLINE int64_t Register::unboxedInt52() const
171 {
172 return u.integer >> JSValue::int52ShiftAmount;
173 }
174
175 ALWAYS_INLINE int64_t Register::unboxedStrictInt52() const
176 {
177 return u.integer;
178 }
179
180 ALWAYS_INLINE bool Register::unboxedBoolean() const
181 {
182 return !!payload();
183 }
184
185 ALWAYS_INLINE double Register::unboxedDouble() const
186 {
187 return u.number;
188 }
189
190 ALWAYS_INLINE JSCell* Register::unboxedCell() const
191 {
192#if USE(JSVALUE64)
193 return u.encodedValue.ptr;
194#else
195 return bitwise_cast<JSCell*>(payload());
196#endif
197 }
198
199 ALWAYS_INLINE int32_t Register::payload() const
200 {
201 return u.encodedValue.asBits.payload;
202 }
203
204 ALWAYS_INLINE int32_t Register::tag() const
205 {
206 return u.encodedValue.asBits.tag;
207 }
208
209 SUPPRESS_ASAN ALWAYS_INLINE int32_t Register::unsafeTag() const
210 {
211 return u.encodedValue.asBits.tag;
212 }
213
214 ALWAYS_INLINE int32_t& Register::payload()
215 {
216 return u.encodedValue.asBits.payload;
217 }
218
219 ALWAYS_INLINE int32_t& Register::tag()
220 {
221 return u.encodedValue.asBits.tag;
222 }
223
224} // namespace JSC
225
226namespace WTF {
227
228 template<> struct VectorTraits<JSC::Register> : VectorTraitsBase<true, JSC::Register> { };
229
230} // namespace WTF
231
232#endif // Register_h
233