v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
tagged-index.h
Go to the documentation of this file.
1// Copyright 2020 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_TAGGED_INDEX_H_
6#define V8_OBJECTS_TAGGED_INDEX_H_
7
11
12// Has to be the last include (doesn't have include guards):
14
15namespace v8 {
16namespace internal {
17
18// TaggedIndex represents integer values that can be stored in 31 bits.
19// The on 32-bit architectures ptr_ value has the following format:
20// [31 bit signed int] 0
21// The on 64-bit architectures ptr_ value has the following format:
22// [32 bits of sign-extended lower part][31 bit signed int] 0
23// Thus, on 32-bit architectures TaggedIndex is exactly the same as Smi but
24// on 64-bit architectures TaggedIndex differs from Smi in the following
25// aspects:
26// 1) TaggedIndex payload is always 31 bit independent of the Smi payload size
27// 2) TaggedIndex is always properly sign-extended independent of whether
28// pointer compression is enabled or not. In the former case, upper 32 bits
29// of a Smi value may contain 0 or sign or isolate root value.
30//
31// Given the above constraints TaggedIndex has the following properties:
32// 1) it still looks like a Smi from GC point of view and therefore it's safe
33// to pass TaggedIndex values to runtime functions or builtins on the stack
34// 2) since the TaggedIndex values are already properly sign-extended it's
35// safe to use them as indices in offset-computation functions.
36class TaggedIndex : public AllStatic {
37 public:
38 // Convert a value to a TaggedIndex object.
39 static inline Tagged<TaggedIndex> FromIntptr(intptr_t value) {
41 return Tagged<TaggedIndex>((static_cast<Address>(value) << kSmiTagSize) |
42 kSmiTag);
43 }
44
45 // Returns whether value can be represented in a TaggedIndex.
46 static inline bool constexpr IsValid(intptr_t value) {
47 return kMinValue <= value && value <= kMaxValue;
48 }
49
50 // Dispatched behavior.
52
53 static_assert(kSmiTagSize == 1);
54 static constexpr int kTaggedValueSize = 31;
55 static constexpr intptr_t kMinValue =
56 static_cast<intptr_t>(kUintptrAllBitsSet << (kTaggedValueSize - 1));
57 static constexpr intptr_t kMaxValue = -(kMinValue + 1);
58};
59
60template <>
62 static inline bool AllowFrom(Tagged<Object> value) {
63 return HAS_SMI_TAG(value.ptr());
64 }
65 static inline bool AllowFrom(Tagged<HeapObject> value) { return false; }
66};
67
68} // namespace internal
69} // namespace v8
70
72
73#endif // V8_OBJECTS_TAGGED_INDEX_H_
static Tagged< TaggedIndex > FromIntptr(intptr_t value)
static constexpr intptr_t kMaxValue
static constexpr intptr_t kMinValue
static constexpr int kTaggedValueSize
static bool constexpr IsValid(intptr_t value)
#define HAS_SMI_TAG(value)
Definition globals.h:1771
const int kSmiTagSize
Definition v8-internal.h:87
constexpr uintptr_t kUintptrAllBitsSet
Definition v8-internal.h:94
const int kSmiTag
Definition v8-internal.h:86
#define DECL_STATIC_VERIFIER(Name)
#define DCHECK(condition)
Definition logging.h:482
static bool AllowFrom(Tagged< HeapObject > value)
static bool AllowFrom(Tagged< Object > value)