1 // Copyright 2011 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.
5 #ifndef V8_SAFEPOINT_TABLE_H_
6 #define V8_SAFEPOINT_TABLE_H_
8 #include "src/allocation.h"
9 #include "src/heap/heap.h"
10 #include "src/v8memory.h"
18 class SafepointEntry BASE_EMBEDDED {
20 SafepointEntry() : info_(0), bits_(NULL) {}
22 SafepointEntry(unsigned info, uint8_t* bits) : info_(info), bits_(bits) {
26 bool is_valid() const { return bits_ != NULL; }
28 bool Equals(const SafepointEntry& other) const {
29 return info_ == other.info_ && bits_ == other.bits_;
37 int deoptimization_index() const {
39 return DeoptimizationIndexField::decode(info_);
42 static const int kArgumentsFieldBits = 3;
43 static const int kSaveDoublesFieldBits = 1;
44 static const int kDeoptIndexBits =
45 32 - kArgumentsFieldBits - kSaveDoublesFieldBits;
46 class DeoptimizationIndexField:
47 public BitField<int, 0, kDeoptIndexBits> {}; // NOLINT
49 public BitField<unsigned,
51 kArgumentsFieldBits> {}; // NOLINT
52 class SaveDoublesField:
54 kDeoptIndexBits + kArgumentsFieldBits,
55 kSaveDoublesFieldBits> { }; // NOLINT
57 int argument_count() const {
59 return ArgumentsField::decode(info_);
62 bool has_doubles() const {
64 return SaveDoublesField::decode(info_);
72 bool HasRegisters() const;
73 bool HasRegisterAt(int reg_index) const;
81 class SafepointTable BASE_EMBEDDED {
83 explicit SafepointTable(Code* code);
87 (length_ * (kPcAndDeoptimizationIndexSize + entry_size_)); }
88 unsigned length() const { return length_; }
89 unsigned entry_size() const { return entry_size_; }
91 unsigned GetPcOffset(unsigned index) const {
92 DCHECK(index < length_);
93 return Memory::uint32_at(GetPcOffsetLocation(index));
96 SafepointEntry GetEntry(unsigned index) const {
97 DCHECK(index < length_);
98 unsigned info = Memory::uint32_at(GetInfoLocation(index));
99 uint8_t* bits = &Memory::uint8_at(entries_ + (index * entry_size_));
100 return SafepointEntry(info, bits);
103 // Returns the entry for the given pc.
104 SafepointEntry FindEntry(Address pc) const;
106 void PrintEntry(unsigned index, OStream& os) const; // NOLINT
109 static const uint8_t kNoRegisters = 0xFF;
111 static const int kLengthOffset = 0;
112 static const int kEntrySizeOffset = kLengthOffset + kIntSize;
113 static const int kHeaderSize = kEntrySizeOffset + kIntSize;
115 static const int kPcSize = kIntSize;
116 static const int kDeoptimizationIndexSize = kIntSize;
117 static const int kPcAndDeoptimizationIndexSize =
118 kPcSize + kDeoptimizationIndexSize;
120 Address GetPcOffsetLocation(unsigned index) const {
121 return pc_and_deoptimization_indexes_ +
122 (index * kPcAndDeoptimizationIndexSize);
125 Address GetInfoLocation(unsigned index) const {
126 return GetPcOffsetLocation(index) + kPcSize;
129 static void PrintBits(OStream& os, // NOLINT
130 uint8_t byte, int digits);
132 DisallowHeapAllocation no_allocation_;
135 unsigned entry_size_;
137 Address pc_and_deoptimization_indexes_;
140 friend class SafepointTableBuilder;
141 friend class SafepointEntry;
143 DISALLOW_COPY_AND_ASSIGN(SafepointTable);
147 class Safepoint BASE_EMBEDDED {
151 kWithRegisters = 1 << 0,
152 kWithDoubles = 1 << 1,
153 kWithRegistersAndDoubles = kWithRegisters | kWithDoubles
161 static const int kNoDeoptimizationIndex =
162 (1 << (SafepointEntry::kDeoptIndexBits)) - 1;
164 void DefinePointerSlot(int index, Zone* zone) { indexes_->Add(index, zone); }
165 void DefinePointerRegister(Register reg, Zone* zone);
168 Safepoint(ZoneList<int>* indexes, ZoneList<int>* registers) :
169 indexes_(indexes), registers_(registers) { }
170 ZoneList<int>* indexes_;
171 ZoneList<int>* registers_;
173 friend class SafepointTableBuilder;
177 class SafepointTableBuilder BASE_EMBEDDED {
179 explicit SafepointTableBuilder(Zone* zone)
180 : deoptimization_info_(32, zone),
181 deopt_index_list_(32, zone),
183 registers_(32, zone),
185 last_lazy_safepoint_(0),
188 // Get the offset of the emitted safepoint table in the code.
189 unsigned GetCodeOffset() const;
191 // Define a new safepoint for the current position in the body.
192 Safepoint DefineSafepoint(Assembler* assembler,
193 Safepoint::Kind kind,
195 Safepoint::DeoptMode mode);
197 // Record deoptimization index for lazy deoptimization for the last
198 // outstanding safepoints.
199 void RecordLazyDeoptimizationIndex(int index);
200 void BumpLastLazySafepointIndex() {
201 last_lazy_safepoint_ = deopt_index_list_.length();
204 // Emit the safepoint table after the body. The number of bits per
205 // entry must be enough to hold all the pointer indexes.
206 void Emit(Assembler* assembler, int bits_per_entry);
210 struct DeoptimizationInfo {
216 uint32_t EncodeExceptPC(const DeoptimizationInfo& info, unsigned index);
218 ZoneList<DeoptimizationInfo> deoptimization_info_;
219 ZoneList<unsigned> deopt_index_list_;
220 ZoneList<ZoneList<int>*> indexes_;
221 ZoneList<ZoneList<int>*> registers_;
225 int last_lazy_safepoint_;
229 DISALLOW_COPY_AND_ASSIGN(SafepointTableBuilder);
232 } } // namespace v8::internal
234 #endif // V8_SAFEPOINT_TABLE_H_