📄 jsimmediate.h
字号:
/* * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. * Copyright (C) 2006 Alexey Proskuryakov (ap@webkit.org) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. * */#ifndef JSImmediate_h#define JSImmediate_h#include <wtf/Assertions.h>#include <wtf/AlwaysInline.h>#include <wtf/MathExtras.h>#include <wtf/StdLibExtras.h>#include "JSValue.h"#include <limits>#include <limits.h>#include <stdarg.h>#include <stdint.h>#include <stdlib.h>namespace JSC { class ExecState; class JSCell; class JSFastMath; class JSGlobalData; class JSObject; class UString; JSValuePtr js0(); JSValuePtr jsNull(); JSValuePtr jsBoolean(bool b); JSValuePtr jsUndefined(); JSValuePtr jsImpossibleValue(); JSValuePtr jsNumber(ExecState* exec, double d); JSValuePtr jsNumber(ExecState*, char i); JSValuePtr jsNumber(ExecState*, unsigned char i); JSValuePtr jsNumber(ExecState*, short i); JSValuePtr jsNumber(ExecState*, unsigned short i); JSValuePtr jsNumber(ExecState* exec, int i); JSValuePtr jsNumber(ExecState* exec, unsigned i); JSValuePtr jsNumber(ExecState* exec, long i); JSValuePtr jsNumber(ExecState* exec, unsigned long i); JSValuePtr jsNumber(ExecState* exec, long long i); JSValuePtr jsNumber(ExecState* exec, unsigned long long i); JSValuePtr jsNumber(JSGlobalData* globalData, double d); JSValuePtr jsNumber(JSGlobalData* globalData, short i); JSValuePtr jsNumber(JSGlobalData* globalData, unsigned short i); JSValuePtr jsNumber(JSGlobalData* globalData, int i); JSValuePtr jsNumber(JSGlobalData* globalData, unsigned i); JSValuePtr jsNumber(JSGlobalData* globalData, long i); JSValuePtr jsNumber(JSGlobalData* globalData, unsigned long i); JSValuePtr jsNumber(JSGlobalData* globalData, long long i); JSValuePtr jsNumber(JSGlobalData* globalData, unsigned long long i);#if USE(ALTERNATE_JSIMMEDIATE) inline intptr_t reinterpretDoubleToIntptr(double value) { return WTF::bitwise_cast<intptr_t>(value); } inline double reinterpretIntptrToDouble(intptr_t value) { return WTF::bitwise_cast<double>(value); }#endif /* * A JSValue* is either a pointer to a cell (a heap-allocated object) or an immediate (a type-tagged * value masquerading as a pointer). The low two bits in a JSValue* are available for type tagging * because allocator alignment guarantees they will be 00 in cell pointers. * * For example, on a 32 bit system: * * JSCell*: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 00 * [ high 30 bits: pointer address ] [ low 2 bits -- always 0 ] * JSImmediate: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX TT * [ high 30 bits: 'payload' ] [ low 2 bits -- tag ] * * Where the bottom two bits are non-zero they either indicate that the immediate is a 31 bit signed * integer, or they mark the value as being an immediate of a type other than integer, with a secondary * tag used to indicate the exact type. * * Where the lowest bit is set (TT is equal to 01 or 11) the high 31 bits form a 31 bit signed int value. * Where TT is equal to 10 this indicates this is a type of immediate other than an integer, and the next * two bits will form an extended tag. * * 31 bit signed int: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX X1 * [ high 30 bits of the value ] [ high bit part of value ] * Other: YYYYYYYYYYYYYYYYYYYYYYYYYYYY ZZ 10 * [ extended 'payload' ] [ extended tag ] [ tag 'other' ] * * Where the first bit of the extended tag is set this flags the value as being a boolean, and the following * bit would flag the value as undefined. If neither bits are set, the value is null. * * Other: YYYYYYYYYYYYYYYYYYYYYYYYYYYY UB 10 * [ extended 'payload' ] [ undefined | bool ] [ tag 'other' ] * * For boolean value the lowest bit in the payload holds the value of the bool, all remaining bits are zero. * For undefined or null immediates the payload is zero. * * Boolean: 000000000000000000000000000V 01 10 * [ boolean value ] [ bool ] [ tag 'other' ] * Undefined: 0000000000000000000000000000 10 10 * [ zero ] [ undefined ] [ tag 'other' ] * Null: 0000000000000000000000000000 00 10 * [ zero ] [ zero ] [ tag 'other' ] */ /* * On 64-bit platforms, we support an alternative encoding form for immediates, if * USE(ALTERNATE_JSIMMEDIATE) is defined. When this format is used, double precision * floating point values may also be encoded as JSImmediates. * * The encoding makes use of unused NaN space in the IEEE754 representation. Any value * with the top 13 bits set represents a QNaN (with the sign bit set). QNaN values * can encode a 51-bit payload. Hardware produced and C-library payloads typically * have a payload of zero. We assume that non-zero payloads are available to encode * pointer and integer values. Since any 64-bit bit pattern where the top 15 bits are * all set represents a NaN with a non-zero payload, we can use this space in the NaN * ranges to encode other values (however there are also other ranges of NaN space that * could have been selected). This range of NaN space is represented by 64-bit numbers * begining with the 16-bit hex patterns 0xFFFE and 0xFFFF - we rely on the fact that no * valid double-precision numbers will begin fall in these ranges. * * The scheme we have implemented encodes double precision values by adding 2^48 to the * 64-bit integer representation of the number. After this manipulation, no encoded * double-precision value will begin with the pattern 0x0000 or 0xFFFF. * * The top 16-bits denote the type of the encoded JSImmediate: * * Pointer: 0000:PPPP:PPPP:PPPP * 0001:****:****:**** * Double:{ ... * FFFE:****:****:**** * Integer: FFFF:0000:IIII:IIII * * 32-bit signed integers are marked with the 16-bit tag 0xFFFF. The tag 0x0000 * denotes a pointer, or another form of tagged immediate. Boolean, null and undefined * values are encoded in the same manner as the default format. */ class JSImmediate { private: friend class JIT; friend class JSValuePtr; friend class JSFastMath; friend JSValuePtr js0(); friend JSValuePtr jsNull(); friend JSValuePtr jsBoolean(bool b); friend JSValuePtr jsUndefined(); friend JSValuePtr jsImpossibleValue(); friend JSValuePtr jsNumber(ExecState* exec, double d); friend JSValuePtr jsNumber(ExecState*, char i); friend JSValuePtr jsNumber(ExecState*, unsigned char i); friend JSValuePtr jsNumber(ExecState*, short i); friend JSValuePtr jsNumber(ExecState*, unsigned short i); friend JSValuePtr jsNumber(ExecState* exec, int i); friend JSValuePtr jsNumber(ExecState* exec, unsigned i); friend JSValuePtr jsNumber(ExecState* exec, long i); friend JSValuePtr jsNumber(ExecState* exec, unsigned long i); friend JSValuePtr jsNumber(ExecState* exec, long long i); friend JSValuePtr jsNumber(ExecState* exec, unsigned long long i); friend JSValuePtr jsNumber(JSGlobalData* globalData, double d); friend JSValuePtr jsNumber(JSGlobalData* globalData, short i); friend JSValuePtr jsNumber(JSGlobalData* globalData, unsigned short i); friend JSValuePtr jsNumber(JSGlobalData* globalData, int i); friend JSValuePtr jsNumber(JSGlobalData* globalData, unsigned i); friend JSValuePtr jsNumber(JSGlobalData* globalData, long i); friend JSValuePtr jsNumber(JSGlobalData* globalData, unsigned long i); friend JSValuePtr jsNumber(JSGlobalData* globalData, long long i); friend JSValuePtr jsNumber(JSGlobalData* globalData, unsigned long long i);#if USE(ALTERNATE_JSIMMEDIATE) // If all bits in the mask are set, this indicates an integer number, // if any but not all are set this value is a double precision number. static const intptr_t TagTypeNumber = 0xffff000000000000ll; // This value is 2^48, used to encode doubles such that the encoded value will begin // with a 16-bit pattern within the range 0x0001..0xFFFE. static const intptr_t DoubleEncodeOffset = 0x1000000000000ll;#else static const intptr_t TagTypeNumber = 0x1; // bottom bit set indicates integer, this dominates the following bit#endif static const intptr_t TagBitTypeOther = 0x2; // second bit set indicates immediate other than an integer static const intptr_t TagMask = TagTypeNumber | TagBitTypeOther; static const intptr_t ExtendedTagMask = 0xC; // extended tag holds a further two bits static const intptr_t ExtendedTagBitBool = 0x4; static const intptr_t ExtendedTagBitUndefined = 0x8; static const intptr_t FullTagTypeMask = TagMask | ExtendedTagMask; static const intptr_t FullTagTypeBool = TagBitTypeOther | ExtendedTagBitBool; static const intptr_t FullTagTypeUndefined = TagBitTypeOther | ExtendedTagBitUndefined; static const intptr_t FullTagTypeNull = TagBitTypeOther;#if USE(ALTERNATE_JSIMMEDIATE) static const int32_t IntegerPayloadShift = 0;#else static const int32_t IntegerPayloadShift = 1;#endif static const int32_t ExtendedPayloadShift = 4; static const intptr_t ExtendedPayloadBitBoolValue = 1 << ExtendedPayloadShift; static const int32_t signBit = 0x80000000; static ALWAYS_INLINE bool isImmediate(JSValuePtr v) { return rawValue(v) & TagMask; } static ALWAYS_INLINE bool isNumber(JSValuePtr v) { return rawValue(v) & TagTypeNumber; } static ALWAYS_INLINE bool isIntegerNumber(JSValuePtr v) {#if USE(ALTERNATE_JSIMMEDIATE) return (rawValue(v) & TagTypeNumber) == TagTypeNumber;#else return isNumber(v);#endif }#if USE(ALTERNATE_JSIMMEDIATE) static ALWAYS_INLINE bool isDoubleNumber(JSValuePtr v) { return isNumber(v) && !isIntegerNumber(v); }#endif static ALWAYS_INLINE bool isPositiveIntegerNumber(JSValuePtr v) { // A single mask to check for the sign bit and the number tag all at once. return (rawValue(v) & (signBit | TagTypeNumber)) == TagTypeNumber; } static ALWAYS_INLINE bool isBoolean(JSValuePtr v) { return (rawValue(v) & FullTagTypeMask) == FullTagTypeBool; } static ALWAYS_INLINE bool isUndefinedOrNull(JSValuePtr v) { // Undefined and null share the same value, bar the 'undefined' bit in the extended tag. return (rawValue(v) & ~ExtendedTagBitUndefined) == FullTagTypeNull; } static JSValuePtr from(char); static JSValuePtr from(signed char); static JSValuePtr from(unsigned char); static JSValuePtr from(short); static JSValuePtr from(unsigned short); static JSValuePtr from(int); static JSValuePtr from(unsigned); static JSValuePtr from(long); static JSValuePtr from(unsigned long); static JSValuePtr from(long long); static JSValuePtr from(unsigned long long); static JSValuePtr from(double); static ALWAYS_INLINE bool isEitherImmediate(JSValuePtr v1, JSValuePtr v2) { return (rawValue(v1) | rawValue(v2)) & TagMask; } static ALWAYS_INLINE bool areBothImmediate(JSValuePtr v1, JSValuePtr v2) { return isImmediate(v1) & isImmediate(v2); } static ALWAYS_INLINE bool areBothImmediateIntegerNumbers(JSValuePtr v1, JSValuePtr v2) {#if USE(ALTERNATE_JSIMMEDIATE) return (rawValue(v1) & rawValue(v2) & TagTypeNumber) == TagTypeNumber;#else return rawValue(v1) & rawValue(v2) & TagTypeNumber;#endif } static double toDouble(JSValuePtr); static bool toBoolean(JSValuePtr); static JSObject* toObject(JSValuePtr, ExecState*); static JSObject* toThisObject(JSValuePtr, ExecState*); static UString toString(JSValuePtr); static bool getUInt32(JSValuePtr, uint32_t&); static bool getTruncatedInt32(JSValuePtr, int32_t&); static bool getTruncatedUInt32(JSValuePtr, uint32_t&); static int32_t getTruncatedInt32(JSValuePtr); static uint32_t getTruncatedUInt32(JSValuePtr); static JSValuePtr trueImmediate(); static JSValuePtr falseImmediate(); static JSValuePtr undefinedImmediate(); static JSValuePtr nullImmediate(); static JSValuePtr zeroImmediate(); static JSValuePtr oneImmediate(); static JSValuePtr impossibleValue(); static JSObject* prototype(JSValuePtr, ExecState*); private:#if USE(ALTERNATE_JSIMMEDIATE) static const int minImmediateInt = ((-INT_MAX) - 1); static const int maxImmediateInt = INT_MAX;#else static const int minImmediateInt = ((-INT_MAX) - 1) >> IntegerPayloadShift; static const int maxImmediateInt = INT_MAX >> IntegerPayloadShift;#endif static const unsigned maxImmediateUInt = maxImmediateInt; static ALWAYS_INLINE JSValuePtr makeValue(intptr_t integer) { return JSValuePtr::makeImmediate(integer); } // With USE(ALTERNATE_JSIMMEDIATE) we want the argument to be zero extended, so the // integer doesn't interfere with the tag bits in the upper word. In the default encoding, // if intptr_t id larger then int32_t we sign extend the value through the upper word.#if USE(ALTERNATE_JSIMMEDIATE) static ALWAYS_INLINE JSValuePtr makeInt(uint32_t value)#else static ALWAYS_INLINE JSValuePtr makeInt(int32_t value)#endif { return makeValue((static_cast<intptr_t>(value) << IntegerPayloadShift) | TagTypeNumber); } #if USE(ALTERNATE_JSIMMEDIATE) static ALWAYS_INLINE JSValuePtr makeDouble(double value) { return makeValue(reinterpretDoubleToIntptr(value) + DoubleEncodeOffset); }#endif static ALWAYS_INLINE JSValuePtr makeBool(bool b) { return makeValue((static_cast<intptr_t>(b) << ExtendedPayloadShift) | FullTagTypeBool); } static ALWAYS_INLINE JSValuePtr makeUndefined() { return makeValue(FullTagTypeUndefined); } static ALWAYS_INLINE JSValuePtr makeNull() { return makeValue(FullTagTypeNull); } template<typename T> static JSValuePtr fromNumberOutsideIntegerRange(T);#if USE(ALTERNATE_JSIMMEDIATE) static ALWAYS_INLINE double doubleValue(JSValuePtr v) { return reinterpretIntptrToDouble(rawValue(v) - DoubleEncodeOffset); }#endif static ALWAYS_INLINE int32_t intValue(JSValuePtr v) { return static_cast<int32_t>(rawValue(v) >> IntegerPayloadShift); } static ALWAYS_INLINE uint32_t uintValue(JSValuePtr v) { return static_cast<uint32_t>(rawValue(v) >> IntegerPayloadShift); } static ALWAYS_INLINE bool boolValue(JSValuePtr v) { return rawValue(v) & ExtendedPayloadBitBoolValue; } static ALWAYS_INLINE intptr_t rawValue(JSValuePtr v) { return v.immediateValue(); } static double nonInlineNaN(); }; ALWAYS_INLINE JSValuePtr JSImmediate::trueImmediate() { return makeBool(true); } ALWAYS_INLINE JSValuePtr JSImmediate::falseImmediate() { return makeBool(false); } ALWAYS_INLINE JSValuePtr JSImmediate::undefinedImmediate() { return makeUndefined(); } ALWAYS_INLINE JSValuePtr JSImmediate::nullImmediate() { return makeNull(); } ALWAYS_INLINE JSValuePtr JSImmediate::zeroImmediate() { return makeInt(0); } ALWAYS_INLINE JSValuePtr JSImmediate::oneImmediate() { return makeInt(1); } // This value is impossible because 0x4 is not a valid pointer but a tag of 0 would indicate non-immediate ALWAYS_INLINE JSValuePtr JSImmediate::impossibleValue() { return makeValue(0x4); }#if USE(ALTERNATE_JSIMMEDIATE) inline bool doubleToBoolean(double value) { return value < 0.0 || value > 0.0; } ALWAYS_INLINE bool JSImmediate::toBoolean(JSValuePtr v)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -