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_
42 class SafepointEntry BASE_EMBEDDED {
44 SafepointEntry() : info_(0), bits_(NULL) {}
46 SafepointEntry(unsigned info, uint8_t* bits) : info_(info), bits_(bits) {
50 bool is_valid() const { return bits_ != NULL; }
52 bool Equals(const SafepointEntry& other) const {
53 return info_ == other.info_ && bits_ == other.bits_;
61 int deoptimization_index() const {
63 return DeoptimizationIndexField::decode(info_);
66 int gap_code_size() const {
68 return GapCodeSizeField::decode(info_);
71 int argument_count() const {
73 return ArgumentsField::decode(info_);
76 bool has_doubles() const {
78 return SaveDoublesField::decode(info_);
86 bool HasRegisters() const;
87 bool HasRegisterAt(int reg_index) const;
89 // Reserve 13 bits for the gap code size. On ARM a constant pool can be
90 // emitted when generating the gap code. The size of the const pool is less
91 // than what can be represented in 12 bits, so 13 bits gives room for having
92 // instructions before potentially emitting a constant pool.
93 static const int kGapCodeSizeBits = 13;
94 static const int kArgumentsFieldBits = 3;
95 static const int kSaveDoublesFieldBits = 1;
96 static const int kDeoptIndexBits =
97 32 - kGapCodeSizeBits - kArgumentsFieldBits - kSaveDoublesFieldBits;
98 class GapCodeSizeField: public BitField<unsigned, 0, kGapCodeSizeBits> {};
99 class DeoptimizationIndexField: public BitField<int,
101 kDeoptIndexBits> {}; // NOLINT
102 class ArgumentsField: public BitField<unsigned,
103 kGapCodeSizeBits + kDeoptIndexBits,
104 kArgumentsFieldBits> {}; // NOLINT
105 class SaveDoublesField: public BitField<bool,
106 kGapCodeSizeBits + kDeoptIndexBits +
108 kSaveDoublesFieldBits> { }; // NOLINT
116 class SafepointTable BASE_EMBEDDED {
118 explicit SafepointTable(Code* code);
122 (length_ * (kPcAndDeoptimizationIndexSize + entry_size_)); }
123 unsigned length() const { return length_; }
124 unsigned entry_size() const { return entry_size_; }
126 unsigned GetPcOffset(unsigned index) const {
127 ASSERT(index < length_);
128 return Memory::uint32_at(GetPcOffsetLocation(index));
131 SafepointEntry GetEntry(unsigned index) const {
132 ASSERT(index < length_);
133 unsigned info = Memory::uint32_at(GetInfoLocation(index));
134 uint8_t* bits = &Memory::uint8_at(entries_ + (index * entry_size_));
135 return SafepointEntry(info, bits);
138 // Returns the entry for the given pc.
139 SafepointEntry FindEntry(Address pc) const;
141 void PrintEntry(unsigned index) const;
144 static const uint8_t kNoRegisters = 0xFF;
146 static const int kLengthOffset = 0;
147 static const int kEntrySizeOffset = kLengthOffset + kIntSize;
148 static const int kHeaderSize = kEntrySizeOffset + kIntSize;
150 static const int kPcSize = kIntSize;
151 static const int kDeoptimizationIndexSize = kIntSize;
152 static const int kPcAndDeoptimizationIndexSize =
153 kPcSize + kDeoptimizationIndexSize;
155 Address GetPcOffsetLocation(unsigned index) const {
156 return pc_and_deoptimization_indexes_ +
157 (index * kPcAndDeoptimizationIndexSize);
160 Address GetInfoLocation(unsigned index) const {
161 return GetPcOffsetLocation(index) + kPcSize;
164 static void PrintBits(uint8_t byte, int digits);
166 AssertNoAllocation no_allocation_;
169 unsigned entry_size_;
171 Address pc_and_deoptimization_indexes_;
174 friend class SafepointTableBuilder;
175 friend class SafepointEntry;
177 DISALLOW_COPY_AND_ASSIGN(SafepointTable);
181 class Safepoint BASE_EMBEDDED {
185 kWithRegisters = 1 << 0,
186 kWithDoubles = 1 << 1,
187 kWithRegistersAndDoubles = kWithRegisters | kWithDoubles
190 static const int kNoDeoptimizationIndex =
191 (1 << (SafepointEntry::kDeoptIndexBits)) - 1;
193 void DefinePointerSlot(int index) { indexes_->Add(index); }
194 void DefinePointerRegister(Register reg);
197 Safepoint(ZoneList<int>* indexes, ZoneList<int>* registers) :
198 indexes_(indexes), registers_(registers) { }
199 ZoneList<int>* indexes_;
200 ZoneList<int>* registers_;
202 friend class SafepointTableBuilder;
206 class SafepointTableBuilder BASE_EMBEDDED {
208 SafepointTableBuilder()
209 : deoptimization_info_(32),
214 // Get the offset of the emitted safepoint table in the code.
215 unsigned GetCodeOffset() const;
217 // Define a new safepoint for the current position in the body.
218 Safepoint DefineSafepoint(Assembler* assembler,
219 Safepoint::Kind kind,
221 int deoptimization_index);
223 // Update the last safepoint with the size of the code generated for the gap
225 void SetPcAfterGap(int pc) {
226 ASSERT(!deoptimization_info_.is_empty());
227 int index = deoptimization_info_.length() - 1;
228 deoptimization_info_[index].pc_after_gap = pc;
231 // Emit the safepoint table after the body. The number of bits per
232 // entry must be enough to hold all the pointer indexes.
233 void Emit(Assembler* assembler, int bits_per_entry);
236 struct DeoptimizationInfo {
238 unsigned deoptimization_index;
239 unsigned pc_after_gap;
244 uint32_t EncodeExceptPC(const DeoptimizationInfo& info);
246 ZoneList<DeoptimizationInfo> deoptimization_info_;
247 ZoneList<ZoneList<int>*> indexes_;
248 ZoneList<ZoneList<int>*> registers_;
253 DISALLOW_COPY_AND_ASSIGN(SafepointTableBuilder);
256 } } // namespace v8::internal
258 #endif // V8_SAFEPOINT_TABLE_H_