V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
constant-pool.h
1 // Copyright 2018 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_CONSTANT_POOL_H_
6 #define V8_CONSTANT_POOL_H_
7 
8 #include <map>
9 
10 #include "src/double.h"
11 #include "src/globals.h"
12 #include "src/label.h"
13 #include "src/reloc-info.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 // -----------------------------------------------------------------------------
19 // Constant pool support
20 
22  public:
23  ConstantPoolEntry() = default;
24  ConstantPoolEntry(int position, intptr_t value, bool sharing_ok,
25  RelocInfo::Mode rmode = RelocInfo::NONE)
26  : position_(position),
27  merged_index_(sharing_ok ? SHARING_ALLOWED : SHARING_PROHIBITED),
28  value_(value),
29  rmode_(rmode) {}
30  ConstantPoolEntry(int position, Double value,
31  RelocInfo::Mode rmode = RelocInfo::NONE)
32  : position_(position),
33  merged_index_(SHARING_ALLOWED),
34  value64_(value.AsUint64()),
35  rmode_(rmode) {}
36 
37  int position() const { return position_; }
38  bool sharing_ok() const { return merged_index_ != SHARING_PROHIBITED; }
39  bool is_merged() const { return merged_index_ >= 0; }
40  int merged_index() const {
41  DCHECK(is_merged());
42  return merged_index_;
43  }
44  void set_merged_index(int index) {
45  DCHECK(sharing_ok());
46  merged_index_ = index;
47  DCHECK(is_merged());
48  }
49  int offset() const {
50  DCHECK_GE(merged_index_, 0);
51  return merged_index_;
52  }
53  void set_offset(int offset) {
54  DCHECK_GE(offset, 0);
55  merged_index_ = offset;
56  }
57  intptr_t value() const { return value_; }
58  uint64_t value64() const { return value64_; }
59  RelocInfo::Mode rmode() const { return rmode_; }
60 
61  enum Type { INTPTR, DOUBLE, NUMBER_OF_TYPES };
62 
63  static int size(Type type) {
64  return (type == INTPTR) ? kPointerSize : kDoubleSize;
65  }
66 
67  enum Access { REGULAR, OVERFLOWED };
68 
69  private:
70  int position_;
71  int merged_index_;
72  union {
73  intptr_t value_;
74  uint64_t value64_;
75  };
76  // TODO(leszeks): The way we use this, it could probably be packed into
77  // merged_index_ if size is a concern.
78  RelocInfo::Mode rmode_;
79  enum { SHARING_PROHIBITED = -2, SHARING_ALLOWED = -1 };
80 };
81 
82 #if defined(V8_TARGET_ARCH_PPC)
83 
84 // -----------------------------------------------------------------------------
85 // Embedded constant pool support
86 
87 class ConstantPoolBuilder {
88  public:
89  ConstantPoolBuilder(int ptr_reach_bits, int double_reach_bits);
90 
91  // Add pointer-sized constant to the embedded constant pool
92  ConstantPoolEntry::Access AddEntry(int position, intptr_t value,
93  bool sharing_ok) {
94  ConstantPoolEntry entry(position, value, sharing_ok);
95  return AddEntry(entry, ConstantPoolEntry::INTPTR);
96  }
97 
98  // Add double constant to the embedded constant pool
99  ConstantPoolEntry::Access AddEntry(int position, Double value) {
100  ConstantPoolEntry entry(position, value);
101  return AddEntry(entry, ConstantPoolEntry::DOUBLE);
102  }
103 
104  // Add double constant to the embedded constant pool
105  ConstantPoolEntry::Access AddEntry(int position, double value) {
106  return AddEntry(position, Double(value));
107  }
108 
109  // Previews the access type required for the next new entry to be added.
110  ConstantPoolEntry::Access NextAccess(ConstantPoolEntry::Type type) const;
111 
112  bool IsEmpty() {
113  return info_[ConstantPoolEntry::INTPTR].entries.empty() &&
114  info_[ConstantPoolEntry::INTPTR].shared_entries.empty() &&
115  info_[ConstantPoolEntry::DOUBLE].entries.empty() &&
116  info_[ConstantPoolEntry::DOUBLE].shared_entries.empty();
117  }
118 
119  // Emit the constant pool. Invoke only after all entries have been
120  // added and all instructions have been emitted.
121  // Returns position of the emitted pool (zero implies no constant pool).
122  int Emit(Assembler* assm);
123 
124  // Returns the label associated with the start of the constant pool.
125  // Linking to this label in the function prologue may provide an
126  // efficient means of constant pool pointer register initialization
127  // on some architectures.
128  inline Label* EmittedPosition() { return &emitted_label_; }
129 
130  private:
131  ConstantPoolEntry::Access AddEntry(ConstantPoolEntry& entry,
132  ConstantPoolEntry::Type type);
133  void EmitSharedEntries(Assembler* assm, ConstantPoolEntry::Type type);
134  void EmitGroup(Assembler* assm, ConstantPoolEntry::Access access,
135  ConstantPoolEntry::Type type);
136 
137  struct PerTypeEntryInfo {
138  PerTypeEntryInfo() : regular_count(0), overflow_start(-1) {}
139  bool overflow() const {
140  return (overflow_start >= 0 &&
141  overflow_start < static_cast<int>(entries.size()));
142  }
143  int regular_reach_bits;
144  int regular_count;
145  int overflow_start;
146  std::vector<ConstantPoolEntry> entries;
147  std::vector<ConstantPoolEntry> shared_entries;
148  };
149 
150  Label emitted_label_; // Records pc_offset of emitted pool
151  PerTypeEntryInfo info_[ConstantPoolEntry::NUMBER_OF_TYPES];
152 };
153 
154 #endif // defined(V8_TARGET_ARCH_PPC)
155 
156 } // namespace internal
157 } // namespace v8
158 
159 #endif // V8_CONSTANT_POOL_H_
Definition: libplatform.h:13