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_INSTANCE_TYPE_INL_H_
6 #define V8_OBJECTS_INSTANCE_TYPE_INL_H_
7 
8 #include "src/base/bounds.h"
9 #include "src/execution/isolate-utils-inl.h"
10 #include "src/objects/instance-type.h"
11 #include "src/objects/map-inl.h"
12 
13 // Has to be the last include (doesn't have include guards):
14 #include "src/objects/object-macros.h"
15 
16 namespace v8 {
17 namespace internal {
18 
19 namespace InstanceTypeChecker {
20 
21 // Define type checkers for classes with single instance type.
22 INSTANCE_TYPE_CHECKERS_SINGLE(INSTANCE_TYPE_CHECKER)
23 
24 // Checks if value is in range [lower_limit, higher_limit] using a single
25 // branch. Assumes that the input instance type is valid.
26 template <InstanceType lower_limit, InstanceType upper_limit>
27 struct InstanceRangeChecker {
CheckInstanceRangeChecker28   static constexpr bool Check(InstanceType value) {
29     return base::IsInRange(value, lower_limit, upper_limit);
30   }
31 };
32 template <InstanceType upper_limit>
33 struct InstanceRangeChecker<FIRST_TYPE, upper_limit> {
34   static constexpr bool Check(InstanceType value) {
35     DCHECK_LE(FIRST_TYPE, value);
36     return value <= upper_limit;
37   }
38 };
39 template <InstanceType lower_limit>
40 struct InstanceRangeChecker<lower_limit, LAST_TYPE> {
41   static constexpr bool Check(InstanceType value) {
42     DCHECK_GE(LAST_TYPE, value);
43     return value >= lower_limit;
44   }
45 };
46 
47 // Define type checkers for classes with ranges of instance types.
48 #define INSTANCE_TYPE_CHECKER_RANGE(type, first_instance_type,             \
49                                     last_instance_type)                    \
50   V8_INLINE bool Is##type(InstanceType instance_type) {                    \
51     return InstanceRangeChecker<first_instance_type,                       \
52                                 last_instance_type>::Check(instance_type); \
53   }
54 INSTANCE_TYPE_CHECKERS_RANGE(INSTANCE_TYPE_CHECKER_RANGE)
55 #undef INSTANCE_TYPE_CHECKER_RANGE
56 
57 V8_INLINE bool IsHeapObject(InstanceType instance_type) { return true; }
58 
59 V8_INLINE bool IsInternalizedString(InstanceType instance_type) {
60   STATIC_ASSERT(kNotInternalizedTag != 0);
61   return (instance_type & (kIsNotStringMask | kIsNotInternalizedMask)) ==
62          (kStringTag | kInternalizedTag);
63 }
64 
65 V8_INLINE bool IsExternalString(InstanceType instance_type) {
66   return (instance_type & (kIsNotStringMask | kStringRepresentationMask)) ==
67          kExternalStringTag;
68 }
69 
70 V8_INLINE bool IsThinString(InstanceType instance_type) {
71   return (instance_type & kStringRepresentationMask) == kThinStringTag;
72 }
73 
74 }  // namespace InstanceTypeChecker
75 
76 // TODO(v8:7786): For instance types that have a single map instance on the
77 // roots, and when that map is a embedded in the binary, compare against the map
78 // pointer rather than looking up the instance type.
79 INSTANCE_TYPE_CHECKERS(TYPE_CHECKER)
80 
81 }  // namespace internal
82 }  // namespace v8
83 
84 #include "src/objects/object-macros-undef.h"
85 
86 #endif  // V8_OBJECTS_INSTANCE_TYPE_INL_H_
87