1// Copyright 2019 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@abstract
6extern class Name extends PrimitiveHeapObject {
7  raw_hash_field: NameHash;
8}
9
10bitfield struct NameHash extends uint32 {
11  hash_not_computed: bool: 1 bit;
12  is_not_integer_index_mask: bool: 1 bit;
13  array_index_value: uint32: 24 bit;
14  array_index_length: uint32: 6 bit;
15}
16
17// This is the same as Name, but with the information that there are no other
18// kinds of names.
19type AnyName = PrivateSymbol|PublicSymbol|String;
20
21bitfield struct SymbolFlags extends uint32 {
22  is_private: bool: 1 bit;
23  is_well_known_symbol: bool: 1 bit;
24  is_in_public_symbol_table: bool: 1 bit;
25  is_interesting_symbol: bool: 1 bit;
26  is_private_name: bool: 1 bit;
27  is_private_brand: bool: 1 bit;
28}
29
30extern class Symbol extends Name {
31  flags: SymbolFlags;
32  description: String|Undefined;
33}
34
35type PublicSymbol extends Symbol;
36type PrivateSymbol extends Symbol;
37
38const kNameEmptyHashField: NameHash = NameHash{
39  hash_not_computed: true,
40  is_not_integer_index_mask: true,
41  array_index_value: 0,
42  array_index_length: 0
43};
44
45const kMaxCachedArrayIndexLength: constexpr uint32
46    generates 'Name::kMaxCachedArrayIndexLength';
47const kMaxArrayIndexSize: constexpr uint32
48    generates 'Name::kMaxArrayIndexSize';
49const kNofHashBitFields: constexpr int31
50    generates 'Name::kNofHashBitFields';
51const kArrayIndexValueBits: constexpr int31
52    generates 'Name::kArrayIndexValueBits';
53const kDoesNotContainCachedArrayIndexMask: constexpr uint32
54    generates 'Name::kDoesNotContainCachedArrayIndexMask';
55const kIsNotIntegerIndexMask: constexpr uint32
56    generates 'Name::kIsNotIntegerIndexMask';
57
58macro ContainsCachedArrayIndex(hash: uint32): bool {
59  return (hash & kDoesNotContainCachedArrayIndexMask) == 0;
60}
61
62const kArrayIndexValueBitsShift: uint32 = kNofHashBitFields;
63const kArrayIndexLengthBitsShift: uint32 =
64    kNofHashBitFields + kArrayIndexValueBits;
65
66macro TenToThe(exponent: uint32): uint32 {
67  dcheck(exponent <= 9);
68  let answer: int32 = 1;
69  for (let i: int32 = 0; i < Signed(exponent); i++) {
70    answer = answer * 10;
71  }
72  return Unsigned(answer);
73}
74
75macro MakeArrayIndexHash(value: uint32, length: uint32): NameHash {
76  // This is in sync with StringHasher::MakeArrayIndexHash.
77  dcheck(length <= kMaxArrayIndexSize);
78  const one: uint32 = 1;
79  dcheck(TenToThe(kMaxCachedArrayIndexLength) < (one << kArrayIndexValueBits));
80  let hash: uint32 = value;
81  hash = (hash << kArrayIndexValueBitsShift) |
82      (length << kArrayIndexLengthBitsShift);
83  dcheck((hash & kIsNotIntegerIndexMask) == 0);
84  dcheck(
85      (length <= kMaxCachedArrayIndexLength) == ContainsCachedArrayIndex(hash));
86  return %RawDownCast<NameHash>(hash);
87}
88