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