Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 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. |
| 15 | // |
| 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. |
| 27 | |
| 28 | #ifndef V8_SAFEPOINT_TABLE_H_ |
| 29 | #define V8_SAFEPOINT_TABLE_H_ |
| 30 | |
| 31 | #include "v8.h" |
| 32 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 33 | #include "heap.h" |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 34 | #include "zone.h" |
| 35 | #include "zone-inl.h" |
| 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 40 | struct Register; |
| 41 | |
| 42 | class SafepointEntry BASE_EMBEDDED { |
| 43 | public: |
| 44 | SafepointEntry() : info_(0), bits_(NULL) {} |
| 45 | |
| 46 | SafepointEntry(unsigned info, uint8_t* bits) : info_(info), bits_(bits) { |
| 47 | ASSERT(is_valid()); |
| 48 | } |
| 49 | |
| 50 | bool is_valid() const { return bits_ != NULL; } |
| 51 | |
| 52 | bool Equals(const SafepointEntry& other) const { |
| 53 | return info_ == other.info_ && bits_ == other.bits_; |
| 54 | } |
| 55 | |
| 56 | void Reset() { |
| 57 | info_ = 0; |
| 58 | bits_ = NULL; |
| 59 | } |
| 60 | |
| 61 | int deoptimization_index() const { |
| 62 | ASSERT(is_valid()); |
| 63 | return DeoptimizationIndexField::decode(info_); |
| 64 | } |
| 65 | |
| 66 | int gap_code_size() const { |
| 67 | ASSERT(is_valid()); |
| 68 | return GapCodeSizeField::decode(info_); |
| 69 | } |
| 70 | |
| 71 | int argument_count() const { |
| 72 | ASSERT(is_valid()); |
| 73 | return ArgumentsField::decode(info_); |
| 74 | } |
| 75 | |
| 76 | bool has_doubles() const { |
| 77 | ASSERT(is_valid()); |
| 78 | return SaveDoublesField::decode(info_); |
| 79 | } |
| 80 | |
| 81 | uint8_t* bits() { |
| 82 | ASSERT(is_valid()); |
| 83 | return bits_; |
| 84 | } |
| 85 | |
| 86 | bool HasRegisters() const; |
| 87 | bool HasRegisterAt(int reg_index) const; |
| 88 | |
| 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, |
| 100 | kGapCodeSizeBits, |
| 101 | kDeoptIndexBits> {}; // NOLINT |
| 102 | class ArgumentsField: public BitField<unsigned, |
| 103 | kGapCodeSizeBits + kDeoptIndexBits, |
| 104 | kArgumentsFieldBits> {}; // NOLINT |
| 105 | class SaveDoublesField: public BitField<bool, |
| 106 | kGapCodeSizeBits + kDeoptIndexBits + |
| 107 | kArgumentsFieldBits, |
| 108 | kSaveDoublesFieldBits> { }; // NOLINT |
| 109 | |
| 110 | private: |
| 111 | unsigned info_; |
| 112 | uint8_t* bits_; |
| 113 | }; |
| 114 | |
| 115 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 116 | class SafepointTable BASE_EMBEDDED { |
| 117 | public: |
| 118 | explicit SafepointTable(Code* code); |
| 119 | |
| 120 | int size() const { |
| 121 | return kHeaderSize + |
| 122 | (length_ * (kPcAndDeoptimizationIndexSize + entry_size_)); } |
| 123 | unsigned length() const { return length_; } |
| 124 | unsigned entry_size() const { return entry_size_; } |
| 125 | |
| 126 | unsigned GetPcOffset(unsigned index) const { |
| 127 | ASSERT(index < length_); |
| 128 | return Memory::uint32_at(GetPcOffsetLocation(index)); |
| 129 | } |
| 130 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 131 | SafepointEntry GetEntry(unsigned index) const { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 132 | ASSERT(index < length_); |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 133 | unsigned info = Memory::uint32_at(GetInfoLocation(index)); |
| 134 | uint8_t* bits = &Memory::uint8_at(entries_ + (index * entry_size_)); |
| 135 | return SafepointEntry(info, bits); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 136 | } |
| 137 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 138 | // Returns the entry for the given pc. |
| 139 | SafepointEntry FindEntry(Address pc) const; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 140 | |
| 141 | void PrintEntry(unsigned index) const; |
| 142 | |
| 143 | private: |
| 144 | static const uint8_t kNoRegisters = 0xFF; |
| 145 | |
| 146 | static const int kLengthOffset = 0; |
| 147 | static const int kEntrySizeOffset = kLengthOffset + kIntSize; |
| 148 | static const int kHeaderSize = kEntrySizeOffset + kIntSize; |
| 149 | |
| 150 | static const int kPcSize = kIntSize; |
| 151 | static const int kDeoptimizationIndexSize = kIntSize; |
| 152 | static const int kPcAndDeoptimizationIndexSize = |
| 153 | kPcSize + kDeoptimizationIndexSize; |
| 154 | |
| 155 | Address GetPcOffsetLocation(unsigned index) const { |
| 156 | return pc_and_deoptimization_indexes_ + |
| 157 | (index * kPcAndDeoptimizationIndexSize); |
| 158 | } |
| 159 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 160 | Address GetInfoLocation(unsigned index) const { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 161 | return GetPcOffsetLocation(index) + kPcSize; |
| 162 | } |
| 163 | |
| 164 | static void PrintBits(uint8_t byte, int digits); |
| 165 | |
| 166 | AssertNoAllocation no_allocation_; |
| 167 | Code* code_; |
| 168 | unsigned length_; |
| 169 | unsigned entry_size_; |
| 170 | |
| 171 | Address pc_and_deoptimization_indexes_; |
| 172 | Address entries_; |
| 173 | |
| 174 | friend class SafepointTableBuilder; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 175 | friend class SafepointEntry; |
| 176 | |
| 177 | DISALLOW_COPY_AND_ASSIGN(SafepointTable); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 178 | }; |
| 179 | |
| 180 | |
| 181 | class Safepoint BASE_EMBEDDED { |
| 182 | public: |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 183 | typedef enum { |
| 184 | kSimple = 0, |
| 185 | kWithRegisters = 1 << 0, |
| 186 | kWithDoubles = 1 << 1, |
| 187 | kWithRegistersAndDoubles = kWithRegisters | kWithDoubles |
| 188 | } Kind; |
| 189 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 190 | static const int kNoDeoptimizationIndex = |
| 191 | (1 << (SafepointEntry::kDeoptIndexBits)) - 1; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 192 | |
| 193 | void DefinePointerSlot(int index) { indexes_->Add(index); } |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 194 | void DefinePointerRegister(Register reg); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 195 | |
| 196 | private: |
| 197 | Safepoint(ZoneList<int>* indexes, ZoneList<int>* registers) : |
| 198 | indexes_(indexes), registers_(registers) { } |
| 199 | ZoneList<int>* indexes_; |
| 200 | ZoneList<int>* registers_; |
| 201 | |
| 202 | friend class SafepointTableBuilder; |
| 203 | }; |
| 204 | |
| 205 | |
| 206 | class SafepointTableBuilder BASE_EMBEDDED { |
| 207 | public: |
| 208 | SafepointTableBuilder() |
| 209 | : deoptimization_info_(32), |
| 210 | indexes_(32), |
| 211 | registers_(32), |
| 212 | emitted_(false) { } |
| 213 | |
| 214 | // Get the offset of the emitted safepoint table in the code. |
| 215 | unsigned GetCodeOffset() const; |
| 216 | |
| 217 | // Define a new safepoint for the current position in the body. |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 218 | Safepoint DefineSafepoint(Assembler* assembler, |
| 219 | Safepoint::Kind kind, |
| 220 | int arguments, |
| 221 | int deoptimization_index); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 222 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 223 | // Update the last safepoint with the size of the code generated until the |
| 224 | // end of the gap following it. |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 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; |
| 229 | } |
| 230 | |
| 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); |
| 234 | |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 235 | // Count the number of deoptimization points where the next |
| 236 | // following deoptimization point comes less than limit bytes |
| 237 | // after the end of this point's gap. |
| 238 | int CountShortDeoptimizationIntervals(unsigned limit); |
| 239 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 240 | private: |
| 241 | struct DeoptimizationInfo { |
| 242 | unsigned pc; |
| 243 | unsigned deoptimization_index; |
| 244 | unsigned pc_after_gap; |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 245 | unsigned arguments; |
| 246 | bool has_doubles; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 247 | }; |
| 248 | |
Ben Murdoch | b8e0da2 | 2011-05-16 14:20:40 +0100 | [diff] [blame] | 249 | uint32_t EncodeExceptPC(const DeoptimizationInfo& info); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 250 | |
| 251 | ZoneList<DeoptimizationInfo> deoptimization_info_; |
| 252 | ZoneList<ZoneList<int>*> indexes_; |
| 253 | ZoneList<ZoneList<int>*> registers_; |
| 254 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 255 | unsigned offset_; |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 256 | bool emitted_; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 257 | |
| 258 | DISALLOW_COPY_AND_ASSIGN(SafepointTableBuilder); |
| 259 | }; |
| 260 | |
| 261 | } } // namespace v8::internal |
| 262 | |
| 263 | #endif // V8_SAFEPOINT_TABLE_H_ |