constant-array-builder.h 4.09 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_
#define V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_

#include "src/identity-map.h"
#include "src/interpreter/bytecodes.h"
#include "src/zone-containers.h"

namespace v8 {
namespace internal {

class Isolate;

namespace interpreter {

19 20 21 22 23
// A helper class for constructing constant arrays for the
// interpreter. Each instance of this class is intended to be used to
// generate exactly one FixedArray of constants via the ToFixedArray
// method.
class ConstantArrayBuilder final BASE_EMBEDDED {
24 25
 public:
  // 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 30 31 32 33
  static const size_t k16BitCapacity = (1u << 2 * kBitsPerByte) - k8BitCapacity;

  // Capacity of the 32-bit operand slice.
  static const size_t k32BitCapacity =
      kMaxUInt32 - k16BitCapacity - k8BitCapacity + 1;
34

35
  ConstantArrayBuilder(Zone* zone, Handle<Object> the_hole_value);
36 37

  // Generate a fixed array of constants based on inserted objects.
38
  Handle<FixedArray> ToFixedArray(Isolate* isolate);
39 40 41 42 43 44 45 46 47 48 49 50

  // Returns the object in the constant pool array that at index
  // |index|.
  Handle<Object> At(size_t index) const;

  // Returns the number of elements in the array.
  size_t size() const;

  // Insert an object into the constants array if it is not already
  // present. Returns the array index associated with the object.
  size_t Insert(Handle<Object> object);

51 52 53 54 55 56 57
  // Allocates an empty entry and returns the array index associated with the
  // reservation. Entry can be inserted by calling InsertReservedEntry().
  size_t AllocateEntry();

  // Inserts the given object into an allocated entry.
  void InsertAllocatedEntry(size_t index, Handle<Object> object);

58 59 60 61 62 63
  // Creates a reserved entry in the constant pool and returns
  // the size of the operand that'll be required to hold the entry
  // when committed.
  OperandSize CreateReservedEntry();

  // Commit reserved entry and returns the constant pool index for the
64 65
  // SMI value.
  size_t CommitReservedEntry(OperandSize operand_size, Smi* value);
66 67 68 69 70

  // Discards constant pool reservation.
  void DiscardReservedEntry(OperandSize operand_size);

 private:
71
  typedef uint32_t index_t;
72 73

  index_t AllocateEntry(Handle<Object> object);
74
  index_t AllocateIndex(Handle<Object> object);
75
  index_t AllocateReservedEntry(Smi* value);
76

77 78 79
  struct ConstantArraySlice final : public ZoneObject {
    ConstantArraySlice(Zone* zone, size_t start_index, size_t capacity,
                       OperandSize operand_size);
80 81 82 83
    void Reserve();
    void Unreserve();
    size_t Allocate(Handle<Object> object);
    Handle<Object> At(size_t index) const;
84
    void InsertAt(size_t index, Handle<Object> object);
85
    bool AllElementsAreUnique() const;
86 87 88 89 90 91

    inline size_t available() const { return capacity() - reserved() - size(); }
    inline size_t reserved() const { return reserved_; }
    inline size_t capacity() const { return capacity_; }
    inline size_t size() const { return constants_.size(); }
    inline size_t start_index() const { return start_index_; }
92 93
    inline size_t max_index() const { return start_index_ + capacity() - 1; }
    inline OperandSize operand_size() const { return operand_size_; }
94 95 96 97 98

   private:
    const size_t start_index_;
    const size_t capacity_;
    size_t reserved_;
99
    OperandSize operand_size_;
100 101 102 103 104
    ZoneVector<Handle<Object>> constants_;

    DISALLOW_COPY_AND_ASSIGN(ConstantArraySlice);
  };

105
  ConstantArraySlice* IndexToSlice(size_t index) const;
106 107
  ConstantArraySlice* OperandSizeToSlice(OperandSize operand_size) const;

108 109
  Handle<Object> the_hole_value() const { return the_hole_value_; }

110
  ConstantArraySlice* idx_slice_[3];
111
  ZoneMap<Address, index_t> constants_map_;
112 113
  ZoneMap<Smi*, index_t> smi_map_;
  ZoneVector<std::pair<Smi*, index_t>> smi_pairs_;
114
  Handle<Object> the_hole_value_;
115 116 117 118 119 120 121
};

}  // namespace interpreter
}  // namespace internal
}  // namespace v8

#endif  // V8_INTERPRETER_CONSTANT_ARRAY_BUILDER_H_