1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef V8_SAFEPOINT_TABLE_H_
29 #define V8_SAFEPOINT_TABLE_H_
31 #include "allocation.h"
41 class SafepointEntry BASE_EMBEDDED {
43 SafepointEntry() : info_(0), bits_(NULL) {}
45 SafepointEntry(unsigned info, uint8_t* bits) : info_(info), bits_(bits) {
49 bool is_valid() const { return bits_ != NULL; }
51 bool Equals(const SafepointEntry& other) const {
52 return info_ == other.info_ && bits_ == other.bits_;
60 int deoptimization_index() const {
62 return DeoptimizationIndexField::decode(info_);
65 static const int kArgumentsFieldBits = 3;
66 static const int kSaveDoublesFieldBits = 1;
67 static const int kDeoptIndexBits =
68 32 - kArgumentsFieldBits - kSaveDoublesFieldBits;
69 class DeoptimizationIndexField:
70 public BitField<int, 0, kDeoptIndexBits> {}; // NOLINT
72 public BitField<unsigned,
74 kArgumentsFieldBits> {}; // NOLINT
75 class SaveDoublesField:
77 kDeoptIndexBits + kArgumentsFieldBits,
78 kSaveDoublesFieldBits> { }; // NOLINT
80 int argument_count() const {
82 return ArgumentsField::decode(info_);
85 bool has_doubles() const {
87 return SaveDoublesField::decode(info_);
95 bool HasRegisters() const;
96 bool HasRegisterAt(int reg_index) const;
104 class SafepointTable BASE_EMBEDDED {
106 explicit SafepointTable(Code* code);
110 (length_ * (kPcAndDeoptimizationIndexSize + entry_size_)); }
111 unsigned length() const { return length_; }
112 unsigned entry_size() const { return entry_size_; }
114 unsigned GetPcOffset(unsigned index) const {
115 ASSERT(index < length_);
116 return Memory::uint32_at(GetPcOffsetLocation(index));
119 SafepointEntry GetEntry(unsigned index) const {
120 ASSERT(index < length_);
121 unsigned info = Memory::uint32_at(GetInfoLocation(index));
122 uint8_t* bits = &Memory::uint8_at(entries_ + (index * entry_size_));
123 return SafepointEntry(info, bits);
126 // Returns the entry for the given pc.
127 SafepointEntry FindEntry(Address pc) const;
129 void PrintEntry(unsigned index, FILE* out = stdout) const;
132 static const uint8_t kNoRegisters = 0xFF;
134 static const int kLengthOffset = 0;
135 static const int kEntrySizeOffset = kLengthOffset + kIntSize;
136 static const int kHeaderSize = kEntrySizeOffset + kIntSize;
138 static const int kPcSize = kIntSize;
139 static const int kDeoptimizationIndexSize = kIntSize;
140 static const int kPcAndDeoptimizationIndexSize =
141 kPcSize + kDeoptimizationIndexSize;
143 Address GetPcOffsetLocation(unsigned index) const {
144 return pc_and_deoptimization_indexes_ +
145 (index * kPcAndDeoptimizationIndexSize);
148 Address GetInfoLocation(unsigned index) const {
149 return GetPcOffsetLocation(index) + kPcSize;
152 static void PrintBits(FILE* out, uint8_t byte, int digits);
154 DisallowHeapAllocation no_allocation_;
157 unsigned entry_size_;
159 Address pc_and_deoptimization_indexes_;
162 friend class SafepointTableBuilder;
163 friend class SafepointEntry;
165 DISALLOW_COPY_AND_ASSIGN(SafepointTable);
169 class Safepoint BASE_EMBEDDED {
173 kWithRegisters = 1 << 0,
174 kWithDoubles = 1 << 1,
175 kWithRegistersAndDoubles = kWithRegisters | kWithDoubles
183 static const int kNoDeoptimizationIndex =
184 (1 << (SafepointEntry::kDeoptIndexBits)) - 1;
186 void DefinePointerSlot(int index, Zone* zone) { indexes_->Add(index, zone); }
187 void DefinePointerRegister(Register reg, Zone* zone);
190 Safepoint(ZoneList<int>* indexes, ZoneList<int>* registers) :
191 indexes_(indexes), registers_(registers) { }
192 ZoneList<int>* indexes_;
193 ZoneList<int>* registers_;
195 friend class SafepointTableBuilder;
199 class SafepointTableBuilder BASE_EMBEDDED {
201 explicit SafepointTableBuilder(Zone* zone)
202 : deoptimization_info_(32, zone),
203 deopt_index_list_(32, zone),
205 registers_(32, zone),
207 last_lazy_safepoint_(0),
210 // Get the offset of the emitted safepoint table in the code.
211 unsigned GetCodeOffset() const;
213 // Define a new safepoint for the current position in the body.
214 Safepoint DefineSafepoint(Assembler* assembler,
215 Safepoint::Kind kind,
217 Safepoint::DeoptMode mode);
219 // Record deoptimization index for lazy deoptimization for the last
220 // outstanding safepoints.
221 void RecordLazyDeoptimizationIndex(int index);
223 // Emit the safepoint table after the body. The number of bits per
224 // entry must be enough to hold all the pointer indexes.
225 void Emit(Assembler* assembler, int bits_per_entry);
229 struct DeoptimizationInfo {
235 uint32_t EncodeExceptPC(const DeoptimizationInfo& info, unsigned index);
237 ZoneList<DeoptimizationInfo> deoptimization_info_;
238 ZoneList<unsigned> deopt_index_list_;
239 ZoneList<ZoneList<int>*> indexes_;
240 ZoneList<ZoneList<int>*> registers_;
244 int last_lazy_safepoint_;
248 DISALLOW_COPY_AND_ASSIGN(SafepointTableBuilder);
251 } } // namespace v8::internal
253 #endif // V8_SAFEPOINT_TABLE_H_