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