Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame^] | 1 | // Copyright 2010 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 |
| 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 | #include "safepoint-table.h" |
| 29 | #include "disasm.h" |
| 30 | |
| 31 | namespace v8 { |
| 32 | namespace internal { |
| 33 | |
| 34 | SafepointTable::SafepointTable(Code* code) { |
| 35 | ASSERT(code->kind() == Code::OPTIMIZED_FUNCTION); |
| 36 | code_ = code; |
| 37 | Address header = code->instruction_start() + code->safepoint_table_start(); |
| 38 | length_ = Memory::uint32_at(header + kLengthOffset); |
| 39 | entry_size_ = Memory::uint32_at(header + kEntrySizeOffset); |
| 40 | pc_and_deoptimization_indexes_ = header + kHeaderSize; |
| 41 | entries_ = pc_and_deoptimization_indexes_ + |
| 42 | (length_ * kPcAndDeoptimizationIndexSize); |
| 43 | ASSERT(entry_size_ > 0); |
| 44 | ASSERT_EQ(DeoptimizationIndexField::max(), Safepoint::kNoDeoptimizationIndex); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | bool SafepointTable::HasRegisters(uint8_t* entry) { |
| 49 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 50 | const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 51 | for (int i = 0; i < num_reg_bytes; i++) { |
| 52 | if (entry[i] != kNoRegisters) return true; |
| 53 | } |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | |
| 58 | bool SafepointTable::HasRegisterAt(uint8_t* entry, int reg_index) { |
| 59 | ASSERT(reg_index >= 0 && reg_index < kNumSafepointRegisters); |
| 60 | int byte_index = reg_index >> kBitsPerByteLog2; |
| 61 | int bit_index = reg_index & (kBitsPerByte - 1); |
| 62 | return (entry[byte_index] & (1 << bit_index)) != 0; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | void SafepointTable::PrintEntry(unsigned index) const { |
| 67 | disasm::NameConverter converter; |
| 68 | uint8_t* entry = GetEntry(index); |
| 69 | |
| 70 | // Print the stack slot bits. |
| 71 | if (entry_size_ > 0) { |
| 72 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 73 | const int first = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 74 | int last = entry_size_ - 1; |
| 75 | for (int i = first; i < last; i++) PrintBits(entry[i], kBitsPerByte); |
| 76 | int last_bits = code_->stack_slots() - ((last - first) * kBitsPerByte); |
| 77 | PrintBits(entry[last], last_bits); |
| 78 | |
| 79 | // Print the registers (if any). |
| 80 | if (!HasRegisters(entry)) return; |
| 81 | for (int j = 0; j < kNumSafepointRegisters; j++) { |
| 82 | if (HasRegisterAt(entry, j)) { |
| 83 | PrintF(" | %s", converter.NameOfCPURegister(j)); |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | void SafepointTable::PrintBits(uint8_t byte, int digits) { |
| 91 | ASSERT(digits >= 0 && digits <= kBitsPerByte); |
| 92 | for (int i = 0; i < digits; i++) { |
| 93 | PrintF("%c", ((byte & (1 << i)) == 0) ? '0' : '1'); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | |
| 98 | Safepoint SafepointTableBuilder::DefineSafepoint(Assembler* assembler, |
| 99 | int deoptimization_index) { |
| 100 | ASSERT(deoptimization_index != -1); |
| 101 | DeoptimizationInfo pc_and_deoptimization_index; |
| 102 | pc_and_deoptimization_index.pc = assembler->pc_offset(); |
| 103 | pc_and_deoptimization_index.deoptimization_index = deoptimization_index; |
| 104 | pc_and_deoptimization_index.pc_after_gap = assembler->pc_offset(); |
| 105 | deoptimization_info_.Add(pc_and_deoptimization_index); |
| 106 | indexes_.Add(new ZoneList<int>(8)); |
| 107 | registers_.Add(NULL); |
| 108 | return Safepoint(indexes_.last(), registers_.last()); |
| 109 | } |
| 110 | |
| 111 | |
| 112 | Safepoint SafepointTableBuilder::DefineSafepointWithRegisters( |
| 113 | Assembler* assembler, int arguments, int deoptimization_index) { |
| 114 | ASSERT(deoptimization_index != -1); |
| 115 | ASSERT(arguments == 0); // Only case that works for now. |
| 116 | DeoptimizationInfo pc_and_deoptimization_index; |
| 117 | pc_and_deoptimization_index.pc = assembler->pc_offset(); |
| 118 | pc_and_deoptimization_index.deoptimization_index = deoptimization_index; |
| 119 | pc_and_deoptimization_index.pc_after_gap = assembler->pc_offset(); |
| 120 | deoptimization_info_.Add(pc_and_deoptimization_index); |
| 121 | indexes_.Add(new ZoneList<int>(8)); |
| 122 | registers_.Add(new ZoneList<int>(4)); |
| 123 | return Safepoint(indexes_.last(), registers_.last()); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | unsigned SafepointTableBuilder::GetCodeOffset() const { |
| 128 | ASSERT(emitted_); |
| 129 | return offset_; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | void SafepointTableBuilder::Emit(Assembler* assembler, int bits_per_entry) { |
| 134 | // Make sure the safepoint table is properly aligned. Pad with nops. |
| 135 | assembler->Align(kIntSize); |
| 136 | assembler->RecordComment(";;; Safepoint table."); |
| 137 | offset_ = assembler->pc_offset(); |
| 138 | |
| 139 | // Take the register bits into account. |
| 140 | bits_per_entry += kNumSafepointRegisters; |
| 141 | |
| 142 | // Compute the number of bytes per safepoint entry. |
| 143 | int bytes_per_entry = |
| 144 | RoundUp(bits_per_entry, kBitsPerByte) >> kBitsPerByteLog2; |
| 145 | |
| 146 | // Emit the table header. |
| 147 | int length = deoptimization_info_.length(); |
| 148 | assembler->dd(length); |
| 149 | assembler->dd(bytes_per_entry); |
| 150 | |
| 151 | // Emit sorted table of pc offsets together with deoptimization indexes and |
| 152 | // pc after gap information. |
| 153 | for (int i = 0; i < length; i++) { |
| 154 | assembler->dd(deoptimization_info_[i].pc); |
| 155 | assembler->dd(EncodeDeoptimizationIndexAndGap(deoptimization_info_[i])); |
| 156 | } |
| 157 | |
| 158 | // Emit table of bitmaps. |
| 159 | ZoneList<uint8_t> bits(bytes_per_entry); |
| 160 | for (int i = 0; i < length; i++) { |
| 161 | ZoneList<int>* indexes = indexes_[i]; |
| 162 | ZoneList<int>* registers = registers_[i]; |
| 163 | bits.Clear(); |
| 164 | bits.AddBlock(0, bytes_per_entry); |
| 165 | |
| 166 | // Run through the registers (if any). |
| 167 | ASSERT(IsAligned(kNumSafepointRegisters, kBitsPerByte)); |
| 168 | if (registers == NULL) { |
| 169 | const int num_reg_bytes = kNumSafepointRegisters >> kBitsPerByteLog2; |
| 170 | for (int j = 0; j < num_reg_bytes; j++) { |
| 171 | bits[j] = SafepointTable::kNoRegisters; |
| 172 | } |
| 173 | } else { |
| 174 | for (int j = 0; j < registers->length(); j++) { |
| 175 | int index = registers->at(j); |
| 176 | ASSERT(index >= 0 && index < kNumSafepointRegisters); |
| 177 | int byte_index = index >> kBitsPerByteLog2; |
| 178 | int bit_index = index & (kBitsPerByte - 1); |
| 179 | bits[byte_index] |= (1 << bit_index); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | // Run through the indexes and build a bitmap. |
| 184 | for (int j = 0; j < indexes->length(); j++) { |
| 185 | int index = bits_per_entry - 1 - indexes->at(j); |
| 186 | int byte_index = index >> kBitsPerByteLog2; |
| 187 | int bit_index = index & (kBitsPerByte - 1); |
| 188 | bits[byte_index] |= (1U << bit_index); |
| 189 | } |
| 190 | |
| 191 | // Emit the bitmap for the current entry. |
| 192 | for (int k = 0; k < bytes_per_entry; k++) { |
| 193 | assembler->db(bits[k]); |
| 194 | } |
| 195 | } |
| 196 | emitted_ = true; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | uint32_t SafepointTableBuilder::EncodeDeoptimizationIndexAndGap( |
| 201 | DeoptimizationInfo info) { |
| 202 | unsigned index = info.deoptimization_index; |
| 203 | unsigned gap_size = info.pc_after_gap - info.pc; |
| 204 | uint32_t encoding = SafepointTable::DeoptimizationIndexField::encode(index); |
| 205 | encoding |= SafepointTable::GapCodeSizeField::encode(gap_size); |
| 206 | return encoding; |
| 207 | } |
| 208 | |
| 209 | |
| 210 | } } // namespace v8::internal |