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 int gap_code_size() const {
67 return GapCodeSizeField::decode(info_);
70 int argument_count() const {
72 return ArgumentsField::decode(info_);
75 bool has_doubles() const {
77 return SaveDoublesField::decode(info_);
85 bool HasRegisters() const;
86 bool HasRegisterAt(int reg_index) const;
88 // Reserve 13 bits for the gap code size. On ARM a constant pool can be
89 // emitted when generating the gap code. The size of the const pool is less
90 // than what can be represented in 12 bits, so 13 bits gives room for having
91 // instructions before potentially emitting a constant pool.
92 static const int kGapCodeSizeBits = 13;
93 static const int kArgumentsFieldBits = 3;
94 static const int kSaveDoublesFieldBits = 1;
95 static const int kDeoptIndexBits =
96 32 - kGapCodeSizeBits - kArgumentsFieldBits - kSaveDoublesFieldBits;
97 class GapCodeSizeField: public BitField<unsigned, 0, kGapCodeSizeBits> {};
98 class DeoptimizationIndexField: public BitField<int,
100 kDeoptIndexBits> {}; // NOLINT
101 class ArgumentsField: public BitField<unsigned,
102 kGapCodeSizeBits + kDeoptIndexBits,
103 kArgumentsFieldBits> {}; // NOLINT
104 class SaveDoublesField: public BitField<bool,
105 kGapCodeSizeBits + kDeoptIndexBits +
107 kSaveDoublesFieldBits> { }; // NOLINT
115 class SafepointTable BASE_EMBEDDED {
117 explicit SafepointTable(Code* code);
121 (length_ * (kPcAndDeoptimizationIndexSize + entry_size_)); }
122 unsigned length() const { return length_; }
123 unsigned entry_size() const { return entry_size_; }
125 unsigned GetPcOffset(unsigned index) const {
126 ASSERT(index < length_);
127 return Memory::uint32_at(GetPcOffsetLocation(index));
130 SafepointEntry GetEntry(unsigned index) const {
131 ASSERT(index < length_);
132 unsigned info = Memory::uint32_at(GetInfoLocation(index));
133 uint8_t* bits = &Memory::uint8_at(entries_ + (index * entry_size_));
134 return SafepointEntry(info, bits);
137 // Returns the entry for the given pc.
138 SafepointEntry FindEntry(Address pc) const;
140 void PrintEntry(unsigned index) const;
143 static const uint8_t kNoRegisters = 0xFF;
145 static const int kLengthOffset = 0;
146 static const int kEntrySizeOffset = kLengthOffset + kIntSize;
147 static const int kHeaderSize = kEntrySizeOffset + kIntSize;
149 static const int kPcSize = kIntSize;
150 static const int kDeoptimizationIndexSize = kIntSize;
151 static const int kPcAndDeoptimizationIndexSize =
152 kPcSize + kDeoptimizationIndexSize;
154 Address GetPcOffsetLocation(unsigned index) const {
155 return pc_and_deoptimization_indexes_ +
156 (index * kPcAndDeoptimizationIndexSize);
159 Address GetInfoLocation(unsigned index) const {
160 return GetPcOffsetLocation(index) + kPcSize;
163 static void PrintBits(uint8_t byte, int digits);
165 AssertNoAllocation no_allocation_;
168 unsigned entry_size_;
170 Address pc_and_deoptimization_indexes_;
173 friend class SafepointTableBuilder;
174 friend class SafepointEntry;
176 DISALLOW_COPY_AND_ASSIGN(SafepointTable);
180 class Safepoint BASE_EMBEDDED {
184 kWithRegisters = 1 << 0,
185 kWithDoubles = 1 << 1,
186 kWithRegistersAndDoubles = kWithRegisters | kWithDoubles
189 static const int kNoDeoptimizationIndex =
190 (1 << (SafepointEntry::kDeoptIndexBits)) - 1;
192 void DefinePointerSlot(int index) { indexes_->Add(index); }
193 void DefinePointerRegister(Register reg);
196 Safepoint(ZoneList<int>* indexes, ZoneList<int>* registers) :
197 indexes_(indexes), registers_(registers) { }
198 ZoneList<int>* indexes_;
199 ZoneList<int>* registers_;
201 friend class SafepointTableBuilder;
205 class SafepointTableBuilder BASE_EMBEDDED {
207 SafepointTableBuilder()
208 : deoptimization_info_(32),
213 // Get the offset of the emitted safepoint table in the code.
214 unsigned GetCodeOffset() const;
216 // Define a new safepoint for the current position in the body.
217 Safepoint DefineSafepoint(Assembler* assembler,
218 Safepoint::Kind kind,
220 int deoptimization_index);
222 // Update the last safepoint with the size of the code generated until the
223 // end of the gap following it.
224 void SetPcAfterGap(int pc) {
225 ASSERT(!deoptimization_info_.is_empty());
226 int index = deoptimization_info_.length() - 1;
227 deoptimization_info_[index].pc_after_gap = pc;
230 // Get the end pc offset of the last safepoint, including the code generated
231 // until the end of the gap following it.
232 unsigned GetPcAfterGap() {
233 int index = deoptimization_info_.length();
234 if (index == 0) return 0;
235 return deoptimization_info_[index - 1].pc_after_gap;
238 // Emit the safepoint table after the body. The number of bits per
239 // entry must be enough to hold all the pointer indexes.
240 void Emit(Assembler* assembler, int bits_per_entry);
242 // Count the number of deoptimization points where the next
243 // following deoptimization point comes less than limit bytes
244 // after the end of this point's gap.
245 int CountShortDeoptimizationIntervals(unsigned limit);
248 struct DeoptimizationInfo {
250 unsigned deoptimization_index;
251 unsigned pc_after_gap;
256 uint32_t EncodeExceptPC(const DeoptimizationInfo& info);
258 ZoneList<DeoptimizationInfo> deoptimization_info_;
259 ZoneList<ZoneList<int>*> indexes_;
260 ZoneList<ZoneList<int>*> registers_;
265 DISALLOW_COPY_AND_ASSIGN(SafepointTableBuilder);
268 } } // namespace v8::internal
270 #endif // V8_SAFEPOINT_TABLE_H_