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