blob: d7e41e377102ced2e9d38341a5d6467770c31cda [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.
26 static const size_t kLowCapacity = 1u << kBitsPerByte;
27
28 // Capacity of the combined 8-bit and 16-bit operand slices.
29 static const size_t kMaxCapacity = 1u << (2 * kBitsPerByte);
30
31 // Capacity of the 16-bit operand slice.
32 static const size_t kHighCapacity = kMaxCapacity - kLowCapacity;
33
34 ConstantArrayBuilder(Isolate* isolate, Zone* zone);
35
36 // Generate a fixed array of constants based on inserted objects.
Ben Murdoch097c5b22016-05-18 11:27:45 +010037 Handle<FixedArray> ToFixedArray();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038
39 // Returns the object in the constant pool array that at index
40 // |index|.
41 Handle<Object> At(size_t index) const;
42
43 // Returns the number of elements in the array.
44 size_t size() const;
45
46 // Insert an object into the constants array if it is not already
47 // present. Returns the array index associated with the object.
48 size_t Insert(Handle<Object> object);
49
50 // Creates a reserved entry in the constant pool and returns
51 // the size of the operand that'll be required to hold the entry
52 // when committed.
53 OperandSize CreateReservedEntry();
54
55 // Commit reserved entry and returns the constant pool index for the
56 // object.
57 size_t CommitReservedEntry(OperandSize operand_size, Handle<Object> object);
58
59 // Discards constant pool reservation.
60 void DiscardReservedEntry(OperandSize operand_size);
61
62 private:
63 typedef uint16_t index_t;
64
65 index_t AllocateEntry(Handle<Object> object);
66
67 struct ConstantArraySlice final {
68 ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity);
69 void Reserve();
70 void Unreserve();
71 size_t Allocate(Handle<Object> object);
72 Handle<Object> At(size_t index) const;
73
74 inline size_t available() const { return capacity() - reserved() - size(); }
75 inline size_t reserved() const { return reserved_; }
76 inline size_t capacity() const { return capacity_; }
77 inline size_t size() const { return constants_.size(); }
78 inline size_t start_index() const { return start_index_; }
79
80 private:
81 const size_t start_index_;
82 const size_t capacity_;
83 size_t reserved_;
84 ZoneVector<Handle<Object>> constants_;
85
86 DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice);
87 };
88
Ben Murdoch097c5b22016-05-18 11:27:45 +010089 IdentityMap<index_t>* constants_map() { return &constants_map_; }
90
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000091 Isolate* isolate_;
92 ConstantArraySlice idx8_slice_;
93 ConstantArraySlice idx16_slice_;
94 IdentityMap<index_t> constants_map_;
95};
96
97} // namespace interpreter
98} // namespace internal
99} // namespace v8
100
101#endif // V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_