1 // Copyright 2015 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_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_ 6 #define V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_ 7 8 #include "src/globals.h" 9 #include "src/identity-map.h" 10 #include "src/interpreter/bytecodes.h" 11 #include "src/zone/zone-containers.h" 12 13 namespace v8 { 14 namespace internal { 15 16 class Isolate; 17 18 namespace interpreter { 19 20 // A helper class for constructing constant arrays for the 21 // interpreter. Each instance of this class is intended to be used to 22 // generate exactly one FixedArray of constants via the ToFixedArray 23 // method. 24 class V8_EXPORT_PRIVATE ConstantArrayBuilder final BASE_EMBEDDED { 25 public: 26 // Capacity of the 8-bit operand slice. 27 static const size_t k8BitCapacity = 1u << kBitsPerByte; 28 29 // Capacity of the 16-bit operand slice. 30 static const size_t k16BitCapacity = (1u << 2 * kBitsPerByte) - k8BitCapacity; 31 32 // Capacity of the 32-bit operand slice. 33 static const size_t k32BitCapacity = 34 kMaxUInt32 - k16BitCapacity - k8BitCapacity + 1; 35 36 ConstantArrayBuilder(Zone* zone, Handle<Object> the_hole_value); 37 38 // Generate a fixed array of constants based on inserted objects. 39 Handle<FixedArray> ToFixedArray(Isolate* isolate); 40 41 // Returns the object in the constant pool array that at index 42 // |index|. 43 Handle<Object> At(size_t index) const; 44 45 // Returns the number of elements in the array. 46 size_t size() const; 47 48 // Insert an object into the constants array if it is not already 49 // present. Returns the array index associated with the object. 50 size_t Insert(Handle<Object> object); 51 52 // Allocates an empty entry and returns the array index associated with the 53 // reservation. Entry can be inserted by calling InsertReservedEntry(). 54 size_t AllocateEntry(); 55 56 // Inserts the given object into an allocated entry. 57 void InsertAllocatedEntry(size_t index, Handle<Object> object); 58 59 // Creates a reserved entry in the constant pool and returns 60 // the size of the operand that'll be required to hold the entry 61 // when committed. 62 OperandSize CreateReservedEntry(); 63 64 // Commit reserved entry and returns the constant pool index for the 65 // SMI value. 66 size_t CommitReservedEntry(OperandSize operand_size, Smi* value); 67 68 // Discards constant pool reservation. 69 void DiscardReservedEntry(OperandSize operand_size); 70 71 private: 72 typedef uint32_t index_t; 73 74 index_t AllocateIndex(Handle<Object> object); 75 index_t AllocateReservedEntry(Smi* value); 76 77 struct ConstantArraySlice final : public ZoneObject { 78 ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity, 79 OperandSize operand_size); 80 void Reserve(); 81 void Unreserve(); 82 size_t Allocate(Handle<Object> object); 83 Handle<Object> At(size_t index) const; 84 void InsertAt(size_t index, Handle<Object> object); 85 bool AllElementsAreUnique() const; 86 availablefinal87 inline size_t available() const { return capacity() - reserved() - size(); } reservedfinal88 inline size_t reserved() const { return reserved_; } capacityfinal89 inline size_t capacity() const { return capacity_; } sizefinal90 inline size_t size() const { return constants_.size(); } start_indexfinal91 inline size_t start_index() const { return start_index_; } max_indexfinal92 inline size_t max_index() const { return start_index_ + capacity() - 1; } operand_sizefinal93 inline OperandSize operand_size() const { return operand_size_; } 94 95 private: 96 const size_t start_index_; 97 const size_t capacity_; 98 size_t reserved_; 99 OperandSize operand_size_; 100 ZoneVector<Handle<Object>> constants_; 101 102 DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice); 103 }; 104 105 ConstantArraySlice* IndexToSlice(size_t index) const; 106 ConstantArraySlice* OperandSizeToSlice(OperandSize operand_size) const; 107 the_hole_value()108 Handle<Object> the_hole_value() const { return the_hole_value_; } 109 110 ConstantArraySlice* idx_slice_[3]; 111 base::TemplateHashMapImpl<Address, index_t, base::KeyEqualityMatcher<Address>, 112 ZoneAllocationPolicy> 113 constants_map_; 114 ZoneMap<Smi*, index_t> smi_map_; 115 ZoneVector<std::pair<Smi*, index_t>> smi_pairs_; 116 Zone* zone_; 117 Handle<Object> the_hole_value_; 118 }; 119 120 } // namespace interpreter 121 } // namespace internal 122 } // namespace v8 123 124 #endif // V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_ 125