v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
machine-type.h
Go to the documentation of this file.
1// Copyright 2014 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_CODEGEN_MACHINE_TYPE_H_
6#define V8_CODEGEN_MACHINE_TYPE_H_
7
8#include <iosfwd>
9#include <limits>
10
12#include "src/base/bits.h"
13#include "src/common/globals.h"
14#include "src/flags/flags.h"
15
16namespace v8 {
17namespace internal {
18
19enum class MachineRepresentation : uint8_t {
20 kNone,
21 kBit,
22 // Integral representations must be consecutive, in order of increasing order.
23 kWord8,
24 kWord16,
25 kWord32,
26 kWord64,
27 // (uncompressed) MapWord
28 // kMapWord is the representation of a map word, i.e. a map in the header
29 // of a HeapObject.
30 // If V8_MAP_PACKING is disabled, a map word is just the map itself. Hence
31 // kMapWord is equivalent to kTaggedPointer -- in fact it will be
32 // translated to kTaggedPointer during memory lowering.
33 // If V8_MAP_PACKING is enabled, a map word is a Smi-like encoding of a map
34 // and some meta data. Memory lowering of kMapWord loads/stores
35 // produces low-level kTagged loads/stores plus the necessary
36 // decode/encode operations.
37 // In either case, the kMapWord representation is not used after memory
38 // lowering.
40 kTaggedSigned, // (uncompressed) Smi
41 kTaggedPointer, // (uncompressed) HeapObject
42 kTagged, // (uncompressed) Object (Smi or HeapObject)
43 kCompressedPointer, // (compressed) HeapObject
44 kCompressed, // (compressed) Object (Smi or HeapObject)
45 kProtectedPointer, // (uncompressed) TrustedObject
46 kIndirectPointer, // (indirect) HeapObject
47 // A 64-bit pointer encoded in a way (e.g. as offset) that guarantees it will
48 // point into the sandbox.
50 // kFloat16RawBits is not a real FP representation! It's representation of a
51 // float16 bitcast to a word16, and is used for conversions to and from
52 // Float16Array elements. It is not used after machine lowering.
54 // FP and SIMD representations must be last, and in order of increasing size.
62};
63
65
66#define ASSERT_CONSECUTIVE(rep1, rep2) \
67 static_assert(static_cast<uint8_t>(MachineRepresentation::k##rep1) + 1 == \
68 static_cast<uint8_t>(MachineRepresentation::k##rep2), \
69 #rep1 " and " #rep2 " must be consecutive.");
70
71ASSERT_CONSECUTIVE(Word8, Word16)
78#undef ASSERT_CONSECUTIVE
79
82 "FP and SIMD representations must be last.");
83
84static_assert(static_cast<int>(MachineRepresentation::kLastRepresentation) <
86 "Bit masks of MachineRepresentation should fit in an int");
87
89
90enum class MachineSemantic : uint8_t {
91 kNone,
92 kBool,
93 kInt32,
94 kUint32,
95 kInt64,
96 kUint64,
99 kNumber,
101 kAny
102};
103
105
106V8_EXPORT_PRIVATE inline constexpr int ElementSizeInBytes(
108
110 public:
117
118 constexpr bool operator==(MachineType other) const {
119 return representation() == other.representation() &&
120 semantic() == other.semantic();
121 }
122
123 constexpr bool operator!=(MachineType other) const {
124 return !(*this == other);
125 }
126
128 return representation_;
129 }
130 constexpr MachineSemantic semantic() const { return semantic_; }
131
132 constexpr bool IsNone() const {
134 }
135
136 constexpr bool IsMapWord() const {
138 }
139
140 constexpr bool IsSigned() const {
141 return semantic() == MachineSemantic::kInt32 ||
143 }
144 constexpr bool IsUnsigned() const {
147 }
153 constexpr bool IsTaggedSigned() const {
155 }
156 constexpr bool IsTaggedPointer() const {
158 }
163 constexpr bool IsCompressedPointer() const {
165 }
166 constexpr bool IsIndirectPointer() const {
168 }
173 constexpr static MachineType UintPtr() {
174 return (kSystemPointerSize == 4) ? Uint32() : Uint64();
175 }
176 constexpr static MachineType IntPtr() {
177 return (kSystemPointerSize == 4) ? Int32() : Int64();
178 }
279
281 bool isSigned = true) {
282 switch (rep) {
284 return MachineType::None();
286 return MachineType::Bool();
288 return isSigned ? MachineType::Int8() : MachineType::Uint8();
290 return isSigned ? MachineType::Int16() : MachineType::Uint16();
292 return isSigned ? MachineType::Int32() : MachineType::Uint32();
294 return isSigned ? MachineType::Int64() : MachineType::Uint64();
296 return MachineType::Float16();
298 return MachineType::Float32();
300 return MachineType::Float64();
302 return MachineType::Simd128();
304 return MachineType::Simd256();
306 return MachineType::AnyTagged();
319 default:
320 UNREACHABLE();
321 }
322 }
323
324 static MachineType TypeForCType(const CTypeInfo& type) {
325 switch (type.GetType()) {
327 return MachineType::AnyTagged();
329 return MachineType::Bool();
331 return MachineType::Uint8();
333 return MachineType::Int32();
335 return MachineType::Uint32();
337 return MachineType::Int64();
339 static_assert(
340 sizeof(AnyCType) == kInt64Size,
341 "CTypeInfo::Type::kAny is assumed to be of size 64 bits.");
342 return MachineType::Int64();
344 return MachineType::Uint64();
346 return MachineType::Float32();
348 return MachineType::Float64();
350 return MachineType::Pointer();
354 return MachineType::AnyTagged();
355 }
356 }
357
358 constexpr bool LessThanOrEqualPointerSize() const {
360 }
361
362 constexpr uint8_t MemSize() const {
363 return 1 << i::ElementSizeLog2Of(this->representation());
364 }
365
366 private:
369};
370
372 return static_cast<size_t>(rep);
373}
374
376 return static_cast<size_t>(type.representation()) +
377 static_cast<size_t>(type.semantic()) * 16;
378}
379
380V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os,
382std::ostream& operator<<(std::ostream& os, MachineSemantic type);
383V8_EXPORT_PRIVATE std::ostream& operator<<(std::ostream& os, MachineType type);
384
385constexpr inline bool IsIntegral(MachineRepresentation rep) {
386 return rep >= MachineRepresentation::kWord8 &&
388}
389
393
394constexpr inline bool IsSimd128(MachineRepresentation rep) {
396}
397
403
408
409constexpr inline bool IsAnyTagged(MachineRepresentation rep) {
411}
412
417
418constexpr inline bool CanBeIndirectPointer(MachineRepresentation rep) {
420}
421
422// Note: this is used in particular to decide which spill slots need
423// to be visited by the GC.
429
435
436constexpr inline bool IsAnyCompressed(MachineRepresentation rep) {
437 return CanBeCompressedPointer(rep);
438}
439
440// Gets the log2 of the element size in bytes of the machine type.
475
476constexpr int kMaximumReprSizeLog2 =
479
480static_assert(kMaximumReprSizeLog2 >=
482static_assert(kMaximumReprSizeLog2 >=
484
487 return 1 << ElementSizeLog2Of(rep);
488}
489
490inline constexpr int ElementSizeInBits(MachineRepresentation rep) {
491 return 8 * ElementSizeInBytes(rep);
492}
493
494inline constexpr uint64_t MaxUnsignedValue(MachineRepresentation rep) {
495 switch (rep) {
497 return std::numeric_limits<uint8_t>::max();
499 return std::numeric_limits<uint16_t>::max();
501 return std::numeric_limits<uint32_t>::max();
503 return std::numeric_limits<uint64_t>::max();
504 default:
505 UNREACHABLE();
506 }
507}
508
514
515// Converts representation to bit for representation masks.
518 return 1 << static_cast<int>(rep);
519}
520
521} // namespace internal
522} // namespace v8
523
524#endif // V8_CODEGEN_MACHINE_TYPE_H_
static constexpr MachineType Simd256()
constexpr bool IsUnsigned() const
static MachineType TypeForRepresentation(const MachineRepresentation &rep, bool isSigned=true)
constexpr bool IsTagged() const
constexpr MachineSemantic semantic() const
static constexpr MachineType MapInHeader()
static constexpr MachineType Float16()
constexpr MachineType(MachineRepresentation representation, MachineSemantic semantic)
constexpr bool IsSigned() const
static constexpr MachineType Float64()
static constexpr MachineType Pointer()
static constexpr MachineType Uint8()
static constexpr MachineType SignedBigInt64()
MachineSemantic semantic_
constexpr MachineRepresentation representation() const
constexpr bool IsIndirectPointer() const
constexpr bool LessThanOrEqualPointerSize() const
constexpr bool IsNone() const
constexpr bool IsCompressedPointer() const
static constexpr MachineType Int32()
static constexpr MachineType Simd128()
constexpr bool operator!=(MachineType other) const
static constexpr MachineType AnyTagged()
constexpr bool IsTaggedPointer() const
static constexpr MachineType Uint64()
static constexpr MachineType Uint32()
static constexpr MachineType AnyCompressed()
static MachineType TypeForCType(const CTypeInfo &type)
static constexpr MachineType SandboxedPointer()
constexpr uint8_t MemSize() const
static constexpr MachineType TaggedSigned()
MachineRepresentation representation_
static constexpr MachineType IndirectPointer()
constexpr bool operator==(MachineType other) const
constexpr bool IsMapWord() const
static constexpr MachineType UnsignedBigInt64()
static constexpr MachineType Uint16()
static constexpr MachineType Int16()
constexpr bool IsTaggedSigned() const
static constexpr MachineType Float32()
constexpr bool IsCompressed() const
static constexpr MachineType Bool()
static constexpr MachineType Int64()
static constexpr MachineType None()
static constexpr MachineType HoleyFloat64()
static constexpr MachineType TaggedPointer()
static constexpr MachineType ProtectedPointer()
static constexpr MachineType UintPtr()
static constexpr MachineType Int8()
static constexpr MachineRepresentation PointerRepresentation()
static constexpr MachineType IntPtr()
static constexpr MachineType CompressedPointer()
#define ASSERT_CONSECUTIVE(rep1, rep2)
WordWithBits< 64 > Word64
Definition index.h:224
FloatWithBits< 32 > Float32
Definition index.h:233
WordWithBits< 32 > Word32
Definition index.h:223
WordWithBits< 128 > Simd128
Definition index.h:236
WordWithBits< 256 > Simd256
Definition index.h:237
FloatWithBits< 64 > Float64
Definition index.h:234
constexpr int kIntSize
Definition globals.h:400
constexpr int ElementSizeInBits(MachineRepresentation rep)
constexpr int kInt64Size
Definition globals.h:402
constexpr int kMaximumReprSizeInBytes
constexpr int kBitsPerByte
Definition globals.h:682
constexpr bool CanBeTaggedOrCompressedOrIndirectPointer(MachineRepresentation rep)
V8_EXPORT_PRIVATE constexpr int ElementSizeInPointers(MachineRepresentation rep)
size_t hash_value(AtomicMemoryOrder order)
constexpr bool CanBeTaggedOrCompressedPointer(MachineRepresentation rep)
V8_EXPORT_PRIVATE constexpr int RepresentationBit(MachineRepresentation rep)
constexpr int kSystemPointerSizeLog2
Definition globals.h:494
constexpr bool IsAnyTagged(MachineRepresentation rep)
std::ostream & operator<<(std::ostream &os, AtomicMemoryOrder order)
constexpr bool IsAnyCompressed(MachineRepresentation rep)
constexpr int kSystemPointerSize
Definition globals.h:410
constexpr int kMaximumReprSizeLog2
constexpr int kTaggedSizeLog2
Definition globals.h:543
constexpr bool IsFloatingPoint(MachineRepresentation rep)
constexpr uint64_t MaxUnsignedValue(MachineRepresentation rep)
constexpr bool CanBeTaggedPointer(MachineRepresentation rep)
const char * MachineReprToString(MachineRepresentation rep)
constexpr bool CanBeIndirectPointer(MachineRepresentation rep)
bool IsSubtype(MachineRepresentation rep1, MachineRepresentation rep2)
V8_EXPORT_PRIVATE constexpr int ElementSizeLog2Of(MachineRepresentation)
constexpr bool IsSimd128(MachineRepresentation rep)
constexpr bool CanBeTaggedSigned(MachineRepresentation rep)
V8_EXPORT_PRIVATE constexpr int ElementSizeInBytes(MachineRepresentation)
constexpr bool CanBeCompressedPointer(MachineRepresentation rep)
constexpr bool IsIntegral(MachineRepresentation rep)
#define V8_EXPORT_PRIVATE
Definition macros.h:460
#define V8_INLINE
Definition v8config.h:500