Line data Source code
1 : // Copyright 2018 the V8 project authors. All rights reserved.
2 : // Use of this source code is governed by a BSD-style license that can be
3 : // found in the LICENSE file.
4 :
5 : #ifndef V8_OBJECTS_SMI_H_
6 : #define V8_OBJECTS_SMI_H_
7 :
8 : #include "src/globals.h"
9 : #include "src/objects/heap-object.h"
10 :
11 : // Has to be the last include (doesn't have include guards):
12 : #include "src/objects/object-macros.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 : // Smi represents integer Numbers that can be stored in 31 bits.
18 : // Smis are immediate which means they are NOT allocated in the heap.
19 : // The ptr_ value has the following format: [31 bit signed int] 0
20 : // For long smis it has the following format:
21 : // [32 bit signed int] [31 bits zero padding] 0
22 : // Smi stands for small integer.
23 : class Smi : public Object {
24 : public:
25 : // This replaces the OBJECT_CONSTRUCTORS macro, because Smis are special
26 : // in that we want them to be constexprs.
27 : constexpr Smi() : Object() {}
28 7659872960 : explicit constexpr Smi(Address ptr) : Object(ptr) {
29 : #if V8_CAN_HAVE_DCHECK_IN_CONSTEXPR
30 : DCHECK(HAS_SMI_TAG(ptr));
31 : #endif
32 7659872960 : }
33 7426233 : Smi* operator->() { return this; }
34 : const Smi* operator->() const { return this; }
35 :
36 : // Returns the integer value.
37 14852464 : inline int value() const { return Internals::SmiValue(ptr()); }
38 : inline Smi ToUint32Smi() {
39 436725 : if (value() <= 0) return Smi::FromInt(0);
40 : return Smi::FromInt(static_cast<uint32_t>(value()));
41 : }
42 :
43 : // Convert a Smi object to an int.
44 : static inline int ToInt(const Object object);
45 :
46 : // Convert a value to a Smi object.
47 12608894 : static inline constexpr Smi FromInt(int value) {
48 : #if V8_CAN_HAVE_DCHECK_IN_CONSTEXPR
49 : DCHECK(Smi::IsValid(value));
50 : #endif
51 12608894 : return Smi(Internals::IntToSmi(value));
52 : }
53 :
54 : static inline Smi FromIntptr(intptr_t value) {
55 : DCHECK(Smi::IsValid(value));
56 : int smi_shift_bits = kSmiTagSize + kSmiShiftSize;
57 99434 : return Smi((value << smi_shift_bits) | kSmiTag);
58 : }
59 :
60 : template <typename E,
61 : typename = typename std::enable_if<std::is_enum<E>::value>::type>
62 : static inline Smi FromEnum(E value) {
63 : STATIC_ASSERT(sizeof(E) <= sizeof(int));
64 627324 : return FromInt(static_cast<int>(value));
65 : }
66 :
67 : // Returns whether value can be represented in a Smi.
68 5 : static inline bool constexpr IsValid(intptr_t value) {
69 : #if V8_CAN_HAVE_DCHECK_IN_CONSTEXPR
70 : DCHECK(Internals::IsValidSmi(value) ==
71 : (value >= kMinValue && value <= kMaxValue));
72 : #endif
73 5 : return Internals::IsValidSmi(value);
74 : }
75 :
76 : // Compare two Smis x, y as if they were converted to strings and then
77 : // compared lexicographically. Returns:
78 : // -1 if x < y.
79 : // 0 if x == y.
80 : // 1 if x > y.
81 : // Returns the result (a tagged Smi) as a raw Address for ExternalReference
82 : // usage.
83 : static Address LexicographicCompare(Isolate* isolate, Smi x, Smi y);
84 :
85 : DECL_CAST(Smi)
86 :
87 : // Dispatched behavior.
88 : V8_EXPORT_PRIVATE void SmiPrint(std::ostream& os) const; // NOLINT
89 : DECL_VERIFIER(Smi)
90 :
91 : // C++ does not allow us to have an object of type Smi within class Smi,
92 : // so the kZero value has type Object. Consider it deprecated; new code
93 : // should use zero() instead.
94 : V8_EXPORT_PRIVATE static constexpr Object kZero = Object(0);
95 : // If you need something with type Smi, call zero() instead. Since it is
96 : // a constexpr, "calling" it is just as efficient as reading kZero.
97 130426498 : static inline constexpr Smi zero() { return Smi::FromInt(0); }
98 : static constexpr int kMinValue = kSmiMinValue;
99 : static constexpr int kMaxValue = kSmiMaxValue;
100 : };
101 :
102 : } // namespace internal
103 : } // namespace v8
104 :
105 : #include "src/objects/object-macros-undef.h"
106 :
107 : #endif // V8_OBJECTS_SMI_H_
|