kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [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 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 28 | #include "v8.h" |
| 29 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 30 | #include "safepoint-table.h" |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 31 | |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 32 | #include "deoptimizer.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 33 | #include "disasm.h" |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 34 | #include "macro-assembler.h" |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 35 | #include "zone-inl.h" |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 36 | |
| 37 | namespace v8 { |
| 38 | namespace internal { |
| 39 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 40 | |
| 41 | bool SafepointEntry::HasRegisters() const { |
| 42 | ASSERT(is_valid()); |
| 43 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 44 | const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 45 | for (int i = 0; i < num_reg_bytes; i++) { |
| 46 | if (bits_[i] != SafepointTable::kNoRegisters) return true; |
| 47 | } |
| 48 | return false; |
| 49 | } |
| 50 | |
| 51 | |
| 52 | bool SafepointEntry::HasRegisterAt(int reg_index) const { |
| 53 | ASSERT(is_valid()); |
| 54 | ASSERT(reg_index >= 0 && reg_index < kNumSafepointRegisters); |
| 55 | int byte_index = reg_index >> kBitsPerByteLog2; |
| 56 | int bit_index = reg_index & (kBitsPerByte - 1); |
| 57 | return (bits_[byte_index] & (1 << bit_index)) != 0; |
| 58 | } |
| 59 | |
| 60 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 61 | SafepointTable::SafepointTable(Code* code) { |
mstarzinger@chromium.org | b228be0 | 2013-04-18 14:56:59 +0000 | [diff] [blame] | 62 | ASSERT(code->is_crankshafted()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 63 | code_ = code; |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 64 | Address header = code->instruction_start() + code->safepoint_table_offset(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 65 | length_ = Memory::uint32_at(header + kLengthOffset); |
| 66 | entry_size_ = Memory::uint32_at(header + kEntrySizeOffset); |
| 67 | pc_and_deoptimization_indexes_ = header + kHeaderSize; |
| 68 | entries_ = pc_and_deoptimization_indexes_ + |
| 69 | (length_ * kPcAndDeoptimizationIndexSize); |
| 70 | ASSERT(entry_size_ > 0); |
kmillikin@chromium.org | 83e1682 | 2011-09-13 08:21:47 +0000 | [diff] [blame] | 71 | STATIC_ASSERT(SafepointEntry::DeoptimizationIndexField::kMax == |
| 72 | Safepoint::kNoDeoptimizationIndex); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 76 | SafepointEntry SafepointTable::FindEntry(Address pc) const { |
| 77 | unsigned pc_offset = static_cast<unsigned>(pc - code_->instruction_start()); |
| 78 | for (unsigned i = 0; i < length(); i++) { |
| 79 | // TODO(kasperl): Replace the linear search with binary search. |
| 80 | if (GetPcOffset(i) == pc_offset) return GetEntry(i); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 81 | } |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 82 | return SafepointEntry(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | |
| 86 | void SafepointTable::PrintEntry(unsigned index) const { |
| 87 | disasm::NameConverter converter; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 88 | SafepointEntry entry = GetEntry(index); |
| 89 | uint8_t* bits = entry.bits(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 90 | |
| 91 | // Print the stack slot bits. |
| 92 | if (entry_size_ > 0) { |
| 93 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 94 | const int first = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 95 | int last = entry_size_ - 1; |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 96 | for (int i = first; i < last; i++) PrintBits(bits[i], kBitsPerByte); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 97 | int last_bits = code_->stack_slots() - ((last - first) * kBitsPerByte); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 98 | PrintBits(bits[last], last_bits); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 99 | |
| 100 | // Print the registers (if any). |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 101 | if (!entry.HasRegisters()) return; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 102 | for (int j = 0; j < kNumSafepointRegisters; j++) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 103 | if (entry.HasRegisterAt(j)) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 104 | PrintF(" | %s", converter.NameOfCPURegister(j)); |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | |
| 111 | void SafepointTable::PrintBits(uint8_t byte, int digits) { |
| 112 | ASSERT(digits >= 0 && digits <= kBitsPerByte); |
| 113 | for (int i = 0; i < digits; i++) { |
| 114 | PrintF("%c", ((byte & (1 << i)) == 0) ? '0' : '1'); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 119 | void Safepoint::DefinePointerRegister(Register reg, Zone* zone) { |
| 120 | registers_->Add(reg.code(), zone); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 124 | Safepoint SafepointTableBuilder::DefineSafepoint( |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 125 | Assembler* assembler, |
| 126 | Safepoint::Kind kind, |
| 127 | int arguments, |
| 128 | Safepoint::DeoptMode deopt_mode) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 129 | ASSERT(arguments >= 0); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 130 | DeoptimizationInfo info; |
| 131 | info.pc = assembler->pc_offset(); |
| 132 | info.arguments = arguments; |
| 133 | info.has_doubles = (kind & Safepoint::kWithDoubles); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 134 | deoptimization_info_.Add(info, zone_); |
| 135 | deopt_index_list_.Add(Safepoint::kNoDeoptimizationIndex, zone_); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 136 | if (deopt_mode == Safepoint::kNoLazyDeopt) { |
| 137 | last_lazy_safepoint_ = deopt_index_list_.length(); |
| 138 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 139 | indexes_.Add(new(zone_) ZoneList<int>(8, zone_), zone_); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 140 | registers_.Add((kind & Safepoint::kWithRegisters) |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 141 | ? new(zone_) ZoneList<int>(4, zone_) |
| 142 | : NULL, |
| 143 | zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 144 | return Safepoint(indexes_.last(), registers_.last()); |
| 145 | } |
| 146 | |
| 147 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 148 | void SafepointTableBuilder::RecordLazyDeoptimizationIndex(int index) { |
| 149 | while (last_lazy_safepoint_ < deopt_index_list_.length()) { |
| 150 | deopt_index_list_[last_lazy_safepoint_++] = index; |
| 151 | } |
| 152 | } |
| 153 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 154 | unsigned SafepointTableBuilder::GetCodeOffset() const { |
| 155 | ASSERT(emitted_); |
| 156 | return offset_; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) { |
| 161 | // Make sure the safepoint table is properly aligned. Pad with nops. |
| 162 | assembler->Align(kIntSize); |
| 163 | assembler->RecordComment(";;; Safepoint table."); |
| 164 | offset_ = assembler->pc_offset(); |
| 165 | |
| 166 | // Take the register bits into account. |
| 167 | bits_per_entry += kNumSafepointRegisters; |
| 168 | |
| 169 | // Compute the number of bytes per safepoint entry. |
| 170 | int bytes_per_entry = |
| 171 | RoundUp(bits_per_entry, kBitsPerByte) >> kBitsPerByteLog2; |
| 172 | |
| 173 | // Emit the table header. |
| 174 | int length = deoptimization_info_.length(); |
| 175 | assembler->dd(length); |
| 176 | assembler->dd(bytes_per_entry); |
| 177 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 178 | // Emit sorted table of pc offsets together with deoptimization indexes. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 179 | for (int i = 0; i < length; i++) { |
| 180 | assembler->dd(deoptimization_info_[i].pc); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 181 | assembler->dd(EncodeExceptPC(deoptimization_info_[i], |
| 182 | deopt_index_list_[i])); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Emit table of bitmaps. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 186 | ZoneList<uint8_t> bits(bytes_per_entry, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 187 | for (int i = 0; i < length; i++) { |
| 188 | ZoneList<int>* indexes = indexes_[i]; |
| 189 | ZoneList<int>* registers = registers_[i]; |
| 190 | bits.Clear(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 191 | bits.AddBlock(0, bytes_per_entry, zone_); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 192 | |
| 193 | // Run through the registers (if any). |
| 194 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 195 | if (registers == NULL) { |
| 196 | const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 197 | for (int j = 0; j < num_reg_bytes; j++) { |
| 198 | bits[j] = SafepointTable::kNoRegisters; |
| 199 | } |
| 200 | } else { |
| 201 | for (int j = 0; j < registers->length(); j++) { |
| 202 | int index = registers->at(j); |
| 203 | ASSERT(index >= 0 && index < kNumSafepointRegisters); |
| 204 | int byte_index = index >> kBitsPerByteLog2; |
| 205 | int bit_index = index & (kBitsPerByte - 1); |
| 206 | bits[byte_index] |= (1 << bit_index); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Run through the indexes and build a bitmap. |
| 211 | for (int j = 0; j < indexes->length(); j++) { |
| 212 | int index = bits_per_entry - 1 - indexes->at(j); |
| 213 | int byte_index = index >> kBitsPerByteLog2; |
| 214 | int bit_index = index & (kBitsPerByte - 1); |
| 215 | bits[byte_index] |= (1U << bit_index); |
| 216 | } |
| 217 | |
| 218 | // Emit the bitmap for the current entry. |
| 219 | for (int k = 0; k < bytes_per_entry; k++) { |
| 220 | assembler->db(bits[k]); |
| 221 | } |
| 222 | } |
| 223 | emitted_ = true; |
| 224 | } |
| 225 | |
| 226 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 227 | uint32_t SafepointTableBuilder::EncodeExceptPC(const DeoptimizationInfo& info, |
| 228 | unsigned index) { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 229 | uint32_t encoding = SafepointEntry::DeoptimizationIndexField::encode(index); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 230 | encoding |= SafepointEntry::ArgumentsField::encode(info.arguments); |
| 231 | encoding |= SafepointEntry::SaveDoublesField::encode(info.has_doubles); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 232 | return encoding; |
| 233 | } |
| 234 | |
| 235 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 236 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 237 | } } // namespace v8::internal |