blob: 1a68646251f1f4360f72668bb91ff9fb48593de6 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// 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
12namespace v8 {
13namespace internal {
14
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015class Isolate;
16
17namespace interpreter {
18
Ben Murdoch097c5b22016-05-18 11:27:45 +010019// 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.
23class ConstantArrayBuilder final BASE_EMBEDDED {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000024 public:
25 // Capacity of the 8-bit operand slice.
Ben Murdochda12d292016-06-02 14:46:10 +010026 static const size_t k8BitCapacity = 1u << kBitsPerByte;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000027
28 // Capacity of the 16-bit operand slice.
Ben Murdochda12d292016-06-02 14:46:10 +010029 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 Murdoch4a90d5f2016-03-22 12:00:34 +000034
35 ConstantArrayBuilder(Isolate* isolate, Zone* zone);
36
37 // Generate a fixed array of constants based on inserted objects.
Ben Murdoch097c5b22016-05-18 11:27:45 +010038 Handle<FixedArray> ToFixedArray();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000039
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 Murdochda12d292016-06-02 14:46:10 +010064 typedef uint32_t index_t;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000065
66 index_t AllocateEntry(Handle<Object> object);
67
Ben Murdochda12d292016-06-02 14:46:10 +010068 struct ConstantArraySlice final : public ZoneObject {
69 ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity,
70 OperandSize operand_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071 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 Murdochda12d292016-06-02 14:46:10 +010081 inline size_t max_index() const { return start_index_ + capacity() - 1; }
82 inline OperandSize operand_size() const { return operand_size_; }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000083
84 private:
85 const size_t start_index_;
86 const size_t capacity_;
87 size_t reserved_;
Ben Murdochda12d292016-06-02 14:46:10 +010088 OperandSize operand_size_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000089 ZoneVector<Handle<Object>> constants_;
90
91 DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice);
92 };
93
Ben Murdochda12d292016-06-02 14:46:10 +010094 const ConstantArraySlice* IndexToSlice(size_t index) const;
95 ConstantArraySlice* OperandSizeToSlice(OperandSize operand_size) const;
96
Ben Murdoch097c5b22016-05-18 11:27:45 +010097 IdentityMap<index_t>* constants_map() { return &constants_map_; }
98
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000099 Isolate* isolate_;
Ben Murdochda12d292016-06-02 14:46:10 +0100100 ConstantArraySlice* idx_slice_[3];
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000101 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_