Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 15 | class Isolate; |
| 16 | |
| 17 | namespace interpreter { |
| 18 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 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 { |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 24 | public: |
| 25 | // Capacity of the 8-bit operand slice. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 26 | static const size_t k8BitCapacity = 1u << kBitsPerByte; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 27 | |
| 28 | // Capacity of the 16-bit operand slice. |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 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; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 34 | |
| 35 | ConstantArrayBuilder(Isolate* isolate, Zone* zone); |
| 36 | |
| 37 | // Generate a fixed array of constants based on inserted objects. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 38 | Handle<FixedArray> ToFixedArray(); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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: |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 64 | typedef uint32_t index_t; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 65 | |
| 66 | index_t AllocateEntry(Handle<Object> object); |
| 67 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 68 | struct ConstantArraySlice final : public ZoneObject { |
| 69 | ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity, |
| 70 | OperandSize operand_size); |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 71 | void Reserve(); |
| 72 | void Unreserve(); |
| 73 | size_t Allocate(Handle<Object> object); |
| 74 | Handle<Object> At(size_t index) const; |
| 75 | |
| 76 | inline size_t available() const { return capacity() - reserved() - size(); } |
| 77 | inline size_t reserved() const { return reserved_; } |
| 78 | inline size_t capacity() const { return capacity_; } |
| 79 | inline size_t size() const { return constants_.size(); } |
| 80 | inline size_t start_index() const { return start_index_; } |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 81 | inline size_t max_index() const { return start_index_ + capacity() - 1; } |
| 82 | inline OperandSize operand_size() const { return operand_size_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 83 | |
| 84 | private: |
| 85 | const size_t start_index_; |
| 86 | const size_t capacity_; |
| 87 | size_t reserved_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 88 | OperandSize operand_size_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 89 | ZoneVector<Handle<Object>> constants_; |
| 90 | |
| 91 | DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice); |
| 92 | }; |
| 93 | |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 94 | const ConstantArraySlice* IndexToSlice(size_t index) const; |
| 95 | ConstantArraySlice* OperandSizeToSlice(OperandSize operand_size) const; |
| 96 | |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 97 | IdentityMap<index_t>* constants_map() { return &constants_map_; } |
| 98 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | Isolate* isolate_; |
Ben Murdoch | da12d29 | 2016-06-02 14:46:10 +0100 | [diff] [blame] | 100 | ConstantArraySlice* idx_slice_[3]; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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_ |