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/identity-map.h" 9 #include "src/interpreter/bytecodes.h" 10 #include "src/zone-containers.h" 11 12 namespace v8 { 13 namespace internal { 14 15 class Isolate; 16 17 namespace interpreter { 18 19 // A helper class for constructing constant arrays for the 20 // interpreter. Each instance of this class is intended to be used to 21 // generate exactly one FixedArray of constants via the ToFixedArray 22 // method. 23 class ConstantArrayBuilder final BASE_EMBEDDED { 24 public: 25 // Capacity of the 8-bit operand slice. 26 static const size_t k8BitCapacity = 1u << kBitsPerByte; 27 28 // Capacity of the 16-bit operand slice. 29 static const size_t k16BitCapacity = (1u << 2 * kBitsPerByte) - k8BitCapacity; 30 31 // Capacity of the 32-bit operand slice. 32 static const size_t k32BitCapacity = 33 kMaxUInt32 - k16BitCapacity - k8BitCapacity + 1; 34 35 ConstantArrayBuilder(Isolate* isolate, Zone* zone); 36 37 // Generate a fixed array of constants based on inserted objects. 38 Handle<FixedArray> ToFixedArray(); 39 40 // Returns the object in the constant pool array that at index 41 // |index|. 42 Handle<Object> At(size_t index) const; 43 44 // Returns the number of elements in the array. 45 size_t size() const; 46 47 // Insert an object into the constants array if it is not already 48 // present. Returns the array index associated with the object. 49 size_t Insert(Handle<Object> object); 50 51 // Creates a reserved entry in the constant pool and returns 52 // the size of the operand that'll be required to hold the entry 53 // when committed. 54 OperandSize CreateReservedEntry(); 55 56 // Commit reserved entry and returns the constant pool index for the 57 // object. 58 size_t CommitReservedEntry(OperandSize operand_size, Handle<Object> object); 59 60 // Discards constant pool reservation. 61 void DiscardReservedEntry(OperandSize operand_size); 62 63 private: 64 typedef uint32_t index_t; 65 66 index_t AllocateEntry(Handle<Object> object); 67 68 struct ConstantArraySlice final : public ZoneObject { 69 ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity, 70 OperandSize operand_size); 71 void Reserve(); 72 void Unreserve(); 73 size_t Allocate(Handle<Object> object); 74 Handle<Object> At(size_t index) const; 75 availablefinal76 inline size_t available() const { return capacity() - reserved() - size(); } reservedfinal77 inline size_t reserved() const { return reserved_; } capacityfinal78 inline size_t capacity() const { return capacity_; } sizefinal79 inline size_t size() const { return constants_.size(); } start_indexfinal80 inline size_t start_index() const { return start_index_; } max_indexfinal81 inline size_t max_index() const { return start_index_ + capacity() - 1; } operand_sizefinal82 inline OperandSize operand_size() const { return operand_size_; } 83 84 private: 85 const size_t start_index_; 86 const size_t capacity_; 87 size_t reserved_; 88 OperandSize operand_size_; 89 ZoneVector<Handle<Object>> constants_; 90 91 DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice); 92 }; 93 94 const ConstantArraySlice* IndexToSlice(size_t index) const; 95 ConstantArraySlice* OperandSizeToSlice(OperandSize operand_size) const; 96 constants_map()97 IdentityMap<index_t>* constants_map() { return &constants_map_; } 98 99 Isolate* isolate_; 100 ConstantArraySlice* idx_slice_[3]; 101 IdentityMap<index_t> constants_map_; 102 }; 103 104 } // namespace interpreter 105 } // namespace internal 106 } // namespace v8 107 108 #endif // V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_ 109