| 1 | /* |
| 2 | * Copyright (C) 2008-2019 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 | #pragma once |
| 30 | |
| 31 | #include "JSCJSValue.h" |
| 32 | #include <wtf/Assertions.h> |
| 33 | #include <wtf/VectorTraits.h> |
| 34 | |
| 35 | namespace JSC { |
| 36 | |
| 37 | class CallFrame; |
| 38 | class CodeBlock; |
| 39 | class JSLexicalEnvironment; |
| 40 | class JSObject; |
| 41 | class JSScope; |
| 42 | |
| 43 | class Register { |
| 44 | WTF_MAKE_FAST_ALLOCATED; |
| 45 | public: |
| 46 | Register(); |
| 47 | |
| 48 | Register(const JSValue&); |
| 49 | JSValue jsValue() const; |
| 50 | JSValue asanUnsafeJSValue() const; |
| 51 | EncodedJSValue encodedJSValue() const; |
| 52 | |
| 53 | ALWAYS_INLINE Register& operator=(CallFrame*); |
| 54 | ALWAYS_INLINE Register& operator=(CodeBlock*); |
| 55 | ALWAYS_INLINE Register& operator=(JSScope*); |
| 56 | ALWAYS_INLINE Register& operator=(JSObject*); |
| 57 | ALWAYS_INLINE Register& operator=(EncodedJSValue); |
| 58 | |
| 59 | int32_t i() const; |
| 60 | ALWAYS_INLINE CallFrame* callFrame() const; |
| 61 | ALWAYS_INLINE CodeBlock* codeBlock() const; |
| 62 | ALWAYS_INLINE CodeBlock* asanUnsafeCodeBlock() const; |
| 63 | ALWAYS_INLINE JSObject* object() const; |
| 64 | ALWAYS_INLINE JSScope* scope() const; |
| 65 | int32_t unboxedInt32() const; |
| 66 | int32_t asanUnsafeUnboxedInt32() const; |
| 67 | int64_t unboxedInt52() const; |
| 68 | int64_t asanUnsafeUnboxedInt52() const; |
| 69 | int64_t unboxedStrictInt52() const; |
| 70 | int64_t asanUnsafeUnboxedStrictInt52() const; |
| 71 | bool unboxedBoolean() const; |
| 72 | double unboxedDouble() const; |
| 73 | double asanUnsafeUnboxedDouble() const; |
| 74 | JSCell* unboxedCell() const; |
| 75 | JSCell* asanUnsafeUnboxedCell() const; |
| 76 | int32_t payload() const; |
| 77 | int32_t tag() const; |
| 78 | int32_t unsafePayload() const; |
| 79 | int32_t unsafeTag() const; |
| 80 | int32_t& payload(); |
| 81 | int32_t& tag(); |
| 82 | |
| 83 | void* pointer() const; |
| 84 | void* asanUnsafePointer() const; |
| 85 | |
| 86 | static Register withInt(int32_t i) |
| 87 | { |
| 88 | Register r = jsNumber(i); |
| 89 | return r; |
| 90 | } |
| 91 | |
| 92 | private: |
| 93 | union { |
| 94 | EncodedJSValue value; |
| 95 | CallFrame* callFrame; |
| 96 | CodeBlock* codeBlock; |
| 97 | EncodedValueDescriptor encodedValue; |
| 98 | double number; |
| 99 | int64_t integer; |
| 100 | } u; |
| 101 | }; |
| 102 | |
| 103 | ALWAYS_INLINE Register::Register() |
| 104 | { |
| 105 | #ifndef NDEBUG |
| 106 | *this = JSValue(); |
| 107 | #endif |
| 108 | } |
| 109 | |
| 110 | ALWAYS_INLINE Register::Register(const JSValue& v) |
| 111 | { |
| 112 | u.value = JSValue::encode(v); |
| 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 int32_t Register::i() const |
| 134 | { |
| 135 | return jsValue().asInt32(); |
| 136 | } |
| 137 | |
| 138 | ALWAYS_INLINE int32_t Register::unboxedInt32() const |
| 139 | { |
| 140 | return payload(); |
| 141 | } |
| 142 | |
| 143 | SUPPRESS_ASAN ALWAYS_INLINE int32_t Register::asanUnsafeUnboxedInt32() const |
| 144 | { |
| 145 | return unsafePayload(); |
| 146 | } |
| 147 | |
| 148 | ALWAYS_INLINE int64_t Register::unboxedInt52() const |
| 149 | { |
| 150 | return u.integer >> JSValue::int52ShiftAmount; |
| 151 | } |
| 152 | |
| 153 | SUPPRESS_ASAN ALWAYS_INLINE int64_t Register::asanUnsafeUnboxedInt52() const |
| 154 | { |
| 155 | return u.integer >> JSValue::int52ShiftAmount; |
| 156 | } |
| 157 | |
| 158 | ALWAYS_INLINE int64_t Register::unboxedStrictInt52() const |
| 159 | { |
| 160 | return u.integer; |
| 161 | } |
| 162 | |
| 163 | SUPPRESS_ASAN ALWAYS_INLINE int64_t Register::asanUnsafeUnboxedStrictInt52() const |
| 164 | { |
| 165 | return u.integer; |
| 166 | } |
| 167 | |
| 168 | ALWAYS_INLINE bool Register::unboxedBoolean() const |
| 169 | { |
| 170 | return !!payload(); |
| 171 | } |
| 172 | |
| 173 | ALWAYS_INLINE double Register::unboxedDouble() const |
| 174 | { |
| 175 | return u.number; |
| 176 | } |
| 177 | |
| 178 | SUPPRESS_ASAN ALWAYS_INLINE double Register::asanUnsafeUnboxedDouble() const |
| 179 | { |
| 180 | return u.number; |
| 181 | } |
| 182 | |
| 183 | ALWAYS_INLINE JSCell* Register::unboxedCell() const |
| 184 | { |
| 185 | #if USE(JSVALUE64) |
| 186 | return u.encodedValue.ptr; |
| 187 | #else |
| 188 | return bitwise_cast<JSCell*>(payload()); |
| 189 | #endif |
| 190 | } |
| 191 | |
| 192 | SUPPRESS_ASAN ALWAYS_INLINE JSCell* Register::asanUnsafeUnboxedCell() const |
| 193 | { |
| 194 | #if USE(JSVALUE64) |
| 195 | return u.encodedValue.ptr; |
| 196 | #else |
| 197 | return bitwise_cast<JSCell*>(payload()); |
| 198 | #endif |
| 199 | } |
| 200 | |
| 201 | ALWAYS_INLINE void* Register::pointer() const |
| 202 | { |
| 203 | #if USE(JSVALUE64) |
| 204 | return u.encodedValue.ptr; |
| 205 | #else |
| 206 | return bitwise_cast<void*>(payload()); |
| 207 | #endif |
| 208 | } |
| 209 | |
| 210 | SUPPRESS_ASAN ALWAYS_INLINE void* Register::asanUnsafePointer() const |
| 211 | { |
| 212 | #if USE(JSVALUE64) |
| 213 | return u.encodedValue.ptr; |
| 214 | #else |
| 215 | return bitwise_cast<void*>(unsafePayload()); |
| 216 | #endif |
| 217 | } |
| 218 | |
| 219 | ALWAYS_INLINE int32_t Register::payload() const |
| 220 | { |
| 221 | return u.encodedValue.asBits.payload; |
| 222 | } |
| 223 | |
| 224 | ALWAYS_INLINE int32_t Register::tag() const |
| 225 | { |
| 226 | return u.encodedValue.asBits.tag; |
| 227 | } |
| 228 | |
| 229 | SUPPRESS_ASAN ALWAYS_INLINE int32_t Register::unsafePayload() const |
| 230 | { |
| 231 | return u.encodedValue.asBits.payload; |
| 232 | } |
| 233 | |
| 234 | SUPPRESS_ASAN ALWAYS_INLINE int32_t Register::unsafeTag() const |
| 235 | { |
| 236 | return u.encodedValue.asBits.tag; |
| 237 | } |
| 238 | |
| 239 | ALWAYS_INLINE int32_t& Register::payload() |
| 240 | { |
| 241 | return u.encodedValue.asBits.payload; |
| 242 | } |
| 243 | |
| 244 | ALWAYS_INLINE int32_t& Register::tag() |
| 245 | { |
| 246 | return u.encodedValue.asBits.tag; |
| 247 | } |
| 248 | |
| 249 | } // namespace JSC |
| 250 | |
| 251 | namespace WTF { |
| 252 | |
| 253 | template<> struct VectorTraits<JSC::Register> : VectorTraitsBase<true, JSC::Register> { }; |
| 254 | |
| 255 | } // namespace WTF |
| 256 | |