constant-array-builder.h 8.09 KB
Newer Older
1 2 3 4 5 6 7
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_
#define V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_

8
#include "src/ast/ast-value-factory.h"
9
#include "src/common/globals.h"
10
#include "src/interpreter/bytecodes.h"
11
#include "src/objects/smi.h"
12
#include "src/utils/identity-map.h"
13
#include "src/zone/zone-containers.h"
14 15 16 17 18

namespace v8 {
namespace internal {

class Isolate;
19 20
class AstRawString;
class AstValue;
21 22 23

namespace interpreter {

24
// Constant array entries that represent singletons.
25 26 27 28
#define SINGLETON_CONSTANT_ENTRY_TYPES(V)                                    \
  V(AsyncIteratorSymbol, async_iterator_symbol)                              \
  V(ClassFieldsSymbol, class_fields_symbol)                                  \
  V(EmptyObjectBoilerplateDescription, empty_object_boilerplate_description) \
29
  V(EmptyArrayBoilerplateDescription, empty_array_boilerplate_description)   \
30 31 32 33
  V(EmptyFixedArray, empty_fixed_array)                                      \
  V(HomeObjectSymbol, home_object_symbol)                                    \
  V(IteratorSymbol, iterator_symbol)                                         \
  V(InterpreterTrampolineSymbol, interpreter_trampoline_symbol)              \
34
  V(NaN, nan_value)
35

36 37 38 39
// A helper class for constructing constant arrays for the
// interpreter. Each instance of this class is intended to be used to
// generate exactly one FixedArray of constants via the ToFixedArray
// method.
40
class V8_EXPORT_PRIVATE ConstantArrayBuilder final {
41 42
 public:
  // Capacity of the 8-bit operand slice.
43
  static const size_t k8BitCapacity = 1u << kBitsPerByte;
44 45

  // Capacity of the 16-bit operand slice.
46 47 48 49 50
  static const size_t k16BitCapacity = (1u << 2 * kBitsPerByte) - k8BitCapacity;

  // Capacity of the 32-bit operand slice.
  static const size_t k32BitCapacity =
      kMaxUInt32 - k16BitCapacity - k8BitCapacity + 1;
51

52
  explicit ConstantArrayBuilder(Zone* zone);
53

54
  // Generate a fixed array of constant handles based on inserted objects.
55
  Handle<FixedArray> ToFixedArray(Isolate* isolate);
56

57 58 59 60
  // Returns the object, as a handle in |isolate|, that is in the constant pool
  // array at index |index|. Returns null if there is no handle at this index.
  // Only expected to be used in tests.
  MaybeHandle<Object> At(size_t index, Isolate* isolate) const;
61 62 63 64

  // Returns the number of elements in the array.
  size_t size() const;

65 66
  // Insert an object into the constants array if it is not already present.
  // Returns the array index associated with the object.
67
  size_t Insert(Smi smi);
68
  size_t Insert(double number);
69
  size_t Insert(const AstRawString* raw_string);
70
  size_t Insert(AstBigInt bigint);
71 72 73 74
  size_t Insert(const Scope* scope);
#define INSERT_ENTRY(NAME, ...) size_t Insert##NAME();
  SINGLETON_CONSTANT_ENTRY_TYPES(INSERT_ENTRY)
#undef INSERT_ENTRY
75

76 77 78 79
  // Inserts an empty entry and returns the array index associated with the
  // reservation. The entry's handle value can be inserted by calling
  // SetDeferredAt().
  size_t InsertDeferred();
80

81 82 83 84 85
  // Inserts |size| consecutive empty entries and returns the array index
  // associated with the first reservation. Each entry's Smi value can be
  // inserted by calling SetJumpTableSmi().
  size_t InsertJumpTable(size_t size);

86 87
  // Sets the deferred value at |index| to |object|.
  void SetDeferredAt(size_t index, Handle<Object> object);
88

89 90
  // Sets the jump table entry at |index| to |smi|. Note that |index| is the
  // constant pool index, not the switch case value.
91
  void SetJumpTableSmi(size_t index, Smi smi);
92

93 94 95 96 97 98
  // Creates a reserved entry in the constant pool and returns
  // the size of the operand that'll be required to hold the entry
  // when committed.
  OperandSize CreateReservedEntry();

  // Commit reserved entry and returns the constant pool index for the
99
  // SMI value.
100
  size_t CommitReservedEntry(OperandSize operand_size, Smi value);
101 102 103 104 105

  // Discards constant pool reservation.
  void DiscardReservedEntry(OperandSize operand_size);

 private:
106
  using index_t = uint32_t;
107

108 109
  struct ConstantArraySlice;

110 111 112 113 114
  class Entry {
   private:
    enum class Tag : uint8_t;

   public:
115
    explicit Entry(Smi smi) : smi_(smi), tag_(Tag::kSmi) {}
116 117
    explicit Entry(double heap_number)
        : heap_number_(heap_number), tag_(Tag::kHeapNumber) {}
118 119
    explicit Entry(const AstRawString* raw_string)
        : raw_string_(raw_string), tag_(Tag::kRawString) {}
120
    explicit Entry(AstBigInt bigint) : bigint_(bigint), tag_(Tag::kBigInt) {}
121 122 123 124 125 126 127 128 129
    explicit Entry(const Scope* scope) : scope_(scope), tag_(Tag::kScope) {}

#define CONSTRUCT_ENTRY(NAME, LOWER_NAME) \
  static Entry NAME() { return Entry(Tag::k##NAME); }
    SINGLETON_CONSTANT_ENTRY_TYPES(CONSTRUCT_ENTRY)
#undef CONSTRUCT_ENTRY

    static Entry Deferred() { return Entry(Tag::kDeferred); }

130 131 132 133
    static Entry UninitializedJumpTableSmi() {
      return Entry(Tag::kUninitializedJumpTableSmi);
    }

134 135
    bool IsDeferred() const { return tag_ == Tag::kDeferred; }

136 137 138 139 140
    bool IsJumpTableEntry() const {
      return tag_ == Tag::kUninitializedJumpTableSmi ||
             tag_ == Tag::kJumpTableSmi;
    }

141
    void SetDeferred(Handle<Object> handle) {
142
      DCHECK_EQ(tag_, Tag::kDeferred);
143 144 145 146
      tag_ = Tag::kHandle;
      handle_ = handle;
    }

147
    void SetJumpTableSmi(Smi smi) {
148
      DCHECK_EQ(tag_, Tag::kUninitializedJumpTableSmi);
149 150 151 152
      tag_ = Tag::kJumpTableSmi;
      smi_ = smi;
    }

153 154 155 156 157 158 159
    Handle<Object> ToHandle(Isolate* isolate) const;

   private:
    explicit Entry(Tag tag) : tag_(tag) {}

    union {
      Handle<Object> handle_;
160
      Smi smi_;
161
      double heap_number_;
162
      const AstRawString* raw_string_;
163
      AstBigInt bigint_;
164 165 166 167 168 169 170 171 172
      const Scope* scope_;
    };

    enum class Tag : uint8_t {
      kDeferred,
      kHandle,
      kSmi,
      kRawString,
      kHeapNumber,
173
      kBigInt,
174
      kScope,
175 176
      kUninitializedJumpTableSmi,
      kJumpTableSmi,
177 178 179 180
#define ENTRY_TAG(NAME, ...) k##NAME,
      SINGLETON_CONSTANT_ENTRY_TYPES(ENTRY_TAG)
#undef ENTRY_TAG
    } tag_;
181 182 183 184 185

#if DEBUG
    // Required by CheckAllElementsAreUnique().
    friend struct ConstantArraySlice;
#endif
186 187 188
  };

  index_t AllocateIndex(Entry constant_entry);
189
  index_t AllocateIndexArray(Entry constant_entry, size_t size);
190
  index_t AllocateReservedEntry(Smi value);
191

192 193 194
  struct ConstantArraySlice final : public ZoneObject {
    ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity,
                       OperandSize operand_size);
195 196
    void Reserve();
    void Unreserve();
197
    size_t Allocate(Entry entry, size_t count = 1);
198 199
    Entry& At(size_t index);
    const Entry& At(size_t index) const;
200 201

#if DEBUG
202
    void CheckAllElementsAreUnique(Isolate* isolate) const;
203
#endif
204 205 206 207 208 209

    inline size_t available() const { return capacity() - reserved() - size(); }
    inline size_t reserved() const { return reserved_; }
    inline size_t capacity() const { return capacity_; }
    inline size_t size() const { return constants_.size(); }
    inline size_t start_index() const { return start_index_; }
210 211
    inline size_t max_index() const { return start_index_ + capacity() - 1; }
    inline OperandSize operand_size() const { return operand_size_; }
212 213 214 215 216

   private:
    const size_t start_index_;
    const size_t capacity_;
    size_t reserved_;
217
    OperandSize operand_size_;
218
    ZoneVector<Entry> constants_;
219 220 221 222

    DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice);
  };

223
  ConstantArraySlice* IndexToSlice(size_t index) const;
224 225
  ConstantArraySlice* OperandSizeToSlice(OperandSize operand_size) const;

226
  ConstantArraySlice* idx_slice_[3];
227 228
  base::TemplateHashMapImpl<intptr_t, index_t,
                            base::KeyEqualityMatcher<intptr_t>,
229 230
                            ZoneAllocationPolicy>
      constants_map_;
231 232
  ZoneMap<Smi, index_t> smi_map_;
  ZoneVector<std::pair<Smi, index_t>> smi_pairs_;
233
  ZoneMap<double, index_t> heap_number_map_;
234 235 236 237 238

#define SINGLETON_ENTRY_FIELD(NAME, LOWER_NAME) int LOWER_NAME##_;
  SINGLETON_CONSTANT_ENTRY_TYPES(SINGLETON_ENTRY_FIELD)
#undef SINGLETON_ENTRY_FIELD

239
  Zone* zone_;
240 241 242 243 244 245 246
};

}  // namespace interpreter
}  // namespace internal
}  // namespace v8

#endif  // V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_