| 1 |
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
| 2 |
* vim: set ts=8 sts=2 et sw=2 tw=80: |
| 3 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 6 |
|
| 7 |
#ifndef vm_ConstantCompareOperand_h |
| 8 |
#define vm_ConstantCompareOperand_h |
| 9 |
|
| 10 |
#include <stdint.h> |
| 11 |
|
| 12 |
#include "js/Value.h" |
| 13 |
|
| 14 |
namespace js { |
| 15 |
|
| 16 |
/* |
| 17 |
* Simple struct for encoding comparison ops with constant values ( |
| 18 |
* that can be extracted during parsing) presently used with the |
| 19 |
* StrictConstantEq and StrictConstantNe opcodes. The operand encodes |
| 20 |
* the type of the constant and the payload if applicable within a |
| 21 |
* uint16_t. The type is encoded in the range of the top 8 bits, and the |
| 22 |
* payload in the bottom 8 bits. |
| 23 |
* |
| 24 |
* TODO (Bug 1958722): Investigate if larger payloads can be supported |
| 25 |
* in the empty bits. |
| 26 |
*/ |
| 27 |
struct ConstantCompareOperand { |
| 28 |
public: |
| 29 |
enum class EncodedType : uint16_t { |
| 30 |
Int32 = JSVAL_TYPE_INT32, |
| 31 |
Boolean = JSVAL_TYPE_BOOLEAN, |
| 32 |
Null = JSVAL_TYPE_NULL, |
| 33 |
Undefined = JSVAL_TYPE_UNDEFINED, |
| 34 |
}; |
| 35 |
static constexpr uint16_t OFFSET_OF_TYPE = |
| 36 |
sizeof(jsbytecode) + sizeof(JSValueType); |
| 37 |
static constexpr uint16_t OFFSET_OF_VALUE = sizeof(jsbytecode); |
| 38 |
|
| 39 |
private: |
| 40 |
uint16_t value_; |
| 41 |
|
| 42 |
static constexpr uint8_t SHIFT_TYPE = 8; |
| 43 |
static constexpr uint16_t MASK_TYPE = 0xFF00; |
| 44 |
static constexpr uint16_t MASK_VALUE = 0x00FF; |
| 45 |
|
| 46 |
static uint16_t encodeType(EncodedType type) { |
| 47 |
return static_cast<uint16_t>(type) << SHIFT_TYPE; |
| 48 |
} |
| 49 |
|
| 50 |
explicit ConstantCompareOperand(uint16_t value) : value_(value) {} |
| 51 |
|
| 52 |
public: |
| 53 |
explicit ConstantCompareOperand(int8_t value) |
| 54 |
: value_(encodeType(EncodedType::Int32) | static_cast<uint8_t>(value)) { |
| 55 |
MOZ_ASSERT(this->toInt32() == value); |
| 56 |
} |
| 57 |
explicit ConstantCompareOperand(bool value) |
| 58 |
: value_(encodeType(EncodedType::Boolean) | value) { |
| 59 |
MOZ_ASSERT(this->toBoolean() == value); |
| 60 |
} |
| 61 |
explicit ConstantCompareOperand(EncodedType type) : value_(encodeType(type)) { |
| 62 |
MOZ_ASSERT(type == EncodedType::Undefined || type == EncodedType::Null); |
| 63 |
MOZ_ASSERT_IF(type == EncodedType::Undefined, this->isUndefined()); |
| 64 |
MOZ_ASSERT_IF(type == EncodedType::Null, this->isNull()); |
| 65 |
} |
| 66 |
|
| 67 |
static ConstantCompareOperand fromRawValue(uint16_t value) { |
| 68 |
return ConstantCompareOperand(value); |
| 69 |
} |
| 70 |
|
| 71 |
static bool CanEncodeInt32ValueAsOperand(int32_t value) { |
| 72 |
return value >= INT8_MIN && value <= INT8_MAX; |
| 73 |
} |
| 74 |
|
| 75 |
EncodedType type() const { |
| 76 |
return static_cast<EncodedType>((value_ & MASK_TYPE) >> SHIFT_TYPE); |
| 77 |
} |
| 78 |
|
| 79 |
bool isNumber() const { return type() == EncodedType::Int32; } |
| 80 |
bool isBoolean() const { return type() == EncodedType::Boolean; } |
| 81 |
bool isNullOrUndefined() const { |
| 82 |
return type() == EncodedType::Undefined || type() == EncodedType::Null; |
| 83 |
} |
| 84 |
bool isUndefined() const { return type() == EncodedType::Undefined; } |
| 85 |
bool isNull() const { return type() == EncodedType::Null; } |
| 86 |
|
| 87 |
int32_t toInt32() const { |
| 88 |
MOZ_ASSERT(type() == EncodedType::Int32); |
| 89 |
return static_cast<int8_t>(value_ & MASK_VALUE); |
| 90 |
} |
| 91 |
|
| 92 |
double toNumber() const { |
| 93 |
MOZ_ASSERT(type() == EncodedType::Int32); |
| 94 |
return static_cast<int8_t>(value_ & MASK_VALUE); |
| 95 |
} |
| 96 |
|
| 97 |
bool toBoolean() const { |
| 98 |
MOZ_ASSERT(type() == EncodedType::Boolean); |
| 99 |
return bool(value_ & MASK_VALUE); |
| 100 |
} |
| 101 |
|
| 102 |
uint16_t rawValue() const { return value_; } |
| 103 |
}; |
| 104 |
|
| 105 |
} // namespace js |
| 106 |
|
| 107 |
#endif /* vm_ConstantCompareOperand_h */ |