constant-array-builder.h 7.69 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/globals.h"
10 11
#include "src/identity-map.h"
#include "src/interpreter/bytecodes.h"
12
#include "src/zone/zone-containers.h"
13 14 15 16 17

namespace v8 {
namespace internal {

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

namespace interpreter {

23
// Constant array entries that represent singletons.
24
#define SINGLETON_CONSTANT_ENTRY_TYPES(V)       \
25
  V(NaN, nan_value)                             \
26 27 28
  V(IteratorSymbol, iterator_symbol)            \
  V(AsyncIteratorSymbol, async_iterator_symbol) \
  V(HomeObjectSymbol, home_object_symbol)       \
29 30
  V(EmptyFixedArray, empty_fixed_array)         \
  V(ClassFieldsSymbol, class_fields_symbol)
31

32 33 34 35
// 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.
36
class V8_EXPORT_PRIVATE ConstantArrayBuilder final BASE_EMBEDDED {
37 38
 public:
  // Capacity of the 8-bit operand slice.
39
  static const size_t k8BitCapacity = 1u << kBitsPerByte;
40 41

  // Capacity of the 16-bit operand slice.
42 43 44 45 46
  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;
47

48
  ConstantArrayBuilder(Zone* zone);
49

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

53 54 55 56
  // 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;
57 58 59 60

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

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

72 73 74 75
  // 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();
76

77 78 79 80 81
  // 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);

82 83
  // Sets the deferred value at |index| to |object|.
  void SetDeferredAt(size_t index, Handle<Object> object);
84

85 86 87 88
  // Sets the jump table entry at |index| to |smi|. Note that |index| is the
  // constant pool index, not the switch case value.
  void SetJumpTableSmi(size_t index, Smi* smi);

89 90 91 92 93 94
  // 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
95 96
  // SMI value.
  size_t CommitReservedEntry(OperandSize operand_size, Smi* value);
97 98 99 100 101

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

 private:
102
  typedef uint32_t index_t;
103

104 105
  struct ConstantArraySlice;

106 107 108 109 110 111
  class Entry {
   private:
    enum class Tag : uint8_t;

   public:
    explicit Entry(Smi* smi) : smi_(smi), tag_(Tag::kSmi) {}
112 113
    explicit Entry(double heap_number)
        : heap_number_(heap_number), tag_(Tag::kHeapNumber) {}
114 115
    explicit Entry(const AstRawString* raw_string)
        : raw_string_(raw_string), tag_(Tag::kRawString) {}
116
    explicit Entry(AstBigInt bigint) : bigint_(bigint), tag_(Tag::kBigInt) {}
117 118 119 120 121 122 123 124 125
    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); }

126 127 128 129
    static Entry UninitializedJumpTableSmi() {
      return Entry(Tag::kUninitializedJumpTableSmi);
    }

130 131
    bool IsDeferred() const { return tag_ == Tag::kDeferred; }

132 133 134 135 136
    bool IsJumpTableEntry() const {
      return tag_ == Tag::kUninitializedJumpTableSmi ||
             tag_ == Tag::kJumpTableSmi;
    }

137
    void SetDeferred(Handle<Object> handle) {
138
      DCHECK_EQ(tag_, Tag::kDeferred);
139 140 141 142
      tag_ = Tag::kHandle;
      handle_ = handle;
    }

143
    void SetJumpTableSmi(Smi* smi) {
144
      DCHECK_EQ(tag_, Tag::kUninitializedJumpTableSmi);
145 146 147 148
      tag_ = Tag::kJumpTableSmi;
      smi_ = smi;
    }

149 150 151 152 153 154 155 156
    Handle<Object> ToHandle(Isolate* isolate) const;

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

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

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

#if DEBUG
    // Required by CheckAllElementsAreUnique().
    friend struct ConstantArraySlice;
#endif
182 183 184
  };

  index_t AllocateIndex(Entry constant_entry);
185
  index_t AllocateIndexArray(Entry constant_entry, size_t size);
186
  index_t AllocateReservedEntry(Smi* value);
187

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

#if DEBUG
198
    void CheckAllElementsAreUnique(Isolate* isolate) const;
199
#endif
200 201 202 203 204 205

    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_; }
206 207
    inline size_t max_index() const { return start_index_ + capacity() - 1; }
    inline OperandSize operand_size() const { return operand_size_; }
208 209 210 211 212

   private:
    const size_t start_index_;
    const size_t capacity_;
    size_t reserved_;
213
    OperandSize operand_size_;
214
    ZoneVector<Entry> constants_;
215 216 217 218

    DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice);
  };

219
  ConstantArraySlice* IndexToSlice(size_t index) const;
220 221
  ConstantArraySlice* OperandSizeToSlice(OperandSize operand_size) const;

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

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

235
  Zone* zone_;
236 237 238 239 240 241 242
};

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

#endif  // V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_