Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 1 | // Copyright (c) 1994-2006 Sun Microsystems Inc. |
| 2 | // All Rights Reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // - Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // |
| 11 | // - Redistribution in binary form must reproduce the above copyright |
| 12 | // notice, this list of conditions and the following disclaimer in the |
| 13 | // documentation and/or other materials provided with the distribution. |
| 14 | // |
| 15 | // - Neither the name of Sun Microsystems or the names of contributors may |
| 16 | // be used to endorse or promote products derived from this software without |
| 17 | // specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 20 | // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 21 | // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 22 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 24 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 26 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 27 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 28 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 29 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // The original source code covered by the above license above has been |
| 32 | // modified significantly by Google Inc. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 33 | // Copyright 2011 the V8 project authors. All rights reserved. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 34 | |
| 35 | |
| 36 | #ifndef V8_MIPS_ASSEMBLER_MIPS_INL_H_ |
| 37 | #define V8_MIPS_ASSEMBLER_MIPS_INL_H_ |
| 38 | |
| 39 | #include "mips/assembler-mips.h" |
| 40 | #include "cpu.h" |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 41 | #include "debug.h" |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 42 | |
| 43 | |
| 44 | namespace v8 { |
| 45 | namespace internal { |
| 46 | |
| 47 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 48 | // Operand and MemOperand. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 49 | |
| 50 | Operand::Operand(int32_t immediate, RelocInfo::Mode rmode) { |
| 51 | rm_ = no_reg; |
| 52 | imm32_ = immediate; |
| 53 | rmode_ = rmode; |
| 54 | } |
| 55 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 56 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 57 | Operand::Operand(const ExternalReference& f) { |
| 58 | rm_ = no_reg; |
| 59 | imm32_ = reinterpret_cast<int32_t>(f.address()); |
| 60 | rmode_ = RelocInfo::EXTERNAL_REFERENCE; |
| 61 | } |
| 62 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 63 | |
| 64 | Operand::Operand(Smi* value) { |
| 65 | rm_ = no_reg; |
| 66 | imm32_ = reinterpret_cast<intptr_t>(value); |
| 67 | rmode_ = RelocInfo::NONE; |
| 68 | } |
| 69 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 70 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 71 | Operand::Operand(Register rm) { |
| 72 | rm_ = rm; |
| 73 | } |
| 74 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 75 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 76 | bool Operand::is_reg() const { |
| 77 | return rm_.is_valid(); |
| 78 | } |
| 79 | |
| 80 | |
| 81 | |
| 82 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 83 | // RelocInfo. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 84 | |
| 85 | void RelocInfo::apply(intptr_t delta) { |
| 86 | // On MIPS we do not use pc relative addressing, so we don't need to patch the |
| 87 | // code here. |
| 88 | } |
| 89 | |
| 90 | |
| 91 | Address RelocInfo::target_address() { |
| 92 | ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); |
| 93 | return Assembler::target_address_at(pc_); |
| 94 | } |
| 95 | |
| 96 | |
| 97 | Address RelocInfo::target_address_address() { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 98 | ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); |
| 99 | return reinterpret_cast<Address>(pc_); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
| 103 | int RelocInfo::target_address_size() { |
| 104 | return Assembler::kExternalTargetSize; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | |
| 108 | void RelocInfo::set_target_address(Address target) { |
| 109 | ASSERT(IsCodeTarget(rmode_) || rmode_ == RUNTIME_ENTRY); |
| 110 | Assembler::set_target_address_at(pc_, target); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | Object* RelocInfo::target_object() { |
| 115 | ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); |
| 116 | return reinterpret_cast<Object*>(Assembler::target_address_at(pc_)); |
| 117 | } |
| 118 | |
| 119 | |
| 120 | Handle<Object> RelocInfo::target_object_handle(Assembler *origin) { |
| 121 | ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); |
| 122 | return Handle<Object>(reinterpret_cast<Object**>( |
| 123 | Assembler::target_address_at(pc_))); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | Object** RelocInfo::target_object_address() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 128 | // Provide a "natural pointer" to the embedded object, |
| 129 | // which can be de-referenced during heap iteration. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 130 | ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 131 | reconstructed_obj_ptr_ = |
| 132 | reinterpret_cast<Object*>(Assembler::target_address_at(pc_)); |
| 133 | return &reconstructed_obj_ptr_; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
| 137 | void RelocInfo::set_target_object(Object* target) { |
| 138 | ASSERT(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT); |
| 139 | Assembler::set_target_address_at(pc_, reinterpret_cast<Address>(target)); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | Address* RelocInfo::target_reference_address() { |
| 144 | ASSERT(rmode_ == EXTERNAL_REFERENCE); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 145 | reconstructed_adr_ptr_ = Assembler::target_address_at(pc_); |
| 146 | return &reconstructed_adr_ptr_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | Handle<JSGlobalPropertyCell> RelocInfo::target_cell_handle() { |
| 151 | ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL); |
| 152 | Address address = Memory::Address_at(pc_); |
| 153 | return Handle<JSGlobalPropertyCell>( |
| 154 | reinterpret_cast<JSGlobalPropertyCell**>(address)); |
| 155 | } |
| 156 | |
| 157 | |
| 158 | JSGlobalPropertyCell* RelocInfo::target_cell() { |
| 159 | ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL); |
| 160 | Address address = Memory::Address_at(pc_); |
| 161 | Object* object = HeapObject::FromAddress( |
| 162 | address - JSGlobalPropertyCell::kValueOffset); |
| 163 | return reinterpret_cast<JSGlobalPropertyCell*>(object); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void RelocInfo::set_target_cell(JSGlobalPropertyCell* cell) { |
| 168 | ASSERT(rmode_ == RelocInfo::GLOBAL_PROPERTY_CELL); |
| 169 | Address address = cell->address() + JSGlobalPropertyCell::kValueOffset; |
| 170 | Memory::Address_at(pc_) = address; |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
| 174 | Address RelocInfo::call_address() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 175 | ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || |
| 176 | (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); |
| 177 | // The pc_ offset of 0 assumes mips patched return sequence per |
| 178 | // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or |
| 179 | // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot(). |
| 180 | return Assembler::target_address_at(pc_); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | |
| 184 | void RelocInfo::set_call_address(Address target) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 185 | ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || |
| 186 | (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); |
| 187 | // The pc_ offset of 0 assumes mips patched return sequence per |
| 188 | // debug-mips.cc BreakLocationIterator::SetDebugBreakAtReturn(), or |
| 189 | // debug break slot per BreakLocationIterator::SetDebugBreakAtSlot(). |
| 190 | Assembler::set_target_address_at(pc_, target); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | |
| 194 | Object* RelocInfo::call_object() { |
| 195 | return *call_object_address(); |
| 196 | } |
| 197 | |
| 198 | |
| 199 | Object** RelocInfo::call_object_address() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 200 | ASSERT((IsJSReturn(rmode()) && IsPatchedReturnSequence()) || |
| 201 | (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence())); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 202 | return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | void RelocInfo::set_call_object(Object* target) { |
| 207 | *call_object_address() = target; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | bool RelocInfo::IsPatchedReturnSequence() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 212 | Instr instr0 = Assembler::instr_at(pc_); |
| 213 | Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize); |
| 214 | Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize); |
| 215 | bool patched_return = ((instr0 & kOpcodeMask) == LUI && |
| 216 | (instr1 & kOpcodeMask) == ORI && |
| 217 | (instr2 & kOpcodeMask) == SPECIAL && |
| 218 | (instr2 & kFunctionFieldMask) == JALR); |
| 219 | return patched_return; |
| 220 | } |
| 221 | |
| 222 | |
| 223 | bool RelocInfo::IsPatchedDebugBreakSlotSequence() { |
| 224 | Instr current_instr = Assembler::instr_at(pc_); |
| 225 | return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | void RelocInfo::Visit(ObjectVisitor* visitor) { |
| 230 | RelocInfo::Mode mode = rmode(); |
| 231 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 232 | Object** p = target_object_address(); |
| 233 | Object* orig = *p; |
| 234 | visitor->VisitPointer(p); |
| 235 | if (*p != orig) { |
| 236 | set_target_object(*p); |
| 237 | } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 238 | } else if (RelocInfo::IsCodeTarget(mode)) { |
| 239 | visitor->VisitCodeTarget(this); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 240 | } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) { |
| 241 | visitor->VisitGlobalPropertyCell(this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 242 | } else if (mode == RelocInfo::EXTERNAL_REFERENCE) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 243 | visitor->VisitExternalReference(target_reference_address()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 244 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 245 | // TODO(isolates): Get a cached isolate below. |
| 246 | } else if (((RelocInfo::IsJSReturn(mode) && |
| 247 | IsPatchedReturnSequence()) || |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 248 | (RelocInfo::IsDebugBreakSlot(mode) && |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 249 | IsPatchedDebugBreakSlotSequence())) && |
| 250 | Isolate::Current()->debug()->has_break_points()) { |
| 251 | visitor->VisitDebugTarget(this); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 252 | #endif |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 253 | } else if (mode == RelocInfo::RUNTIME_ENTRY) { |
| 254 | visitor->VisitRuntimeEntry(this); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | |
| 259 | template<typename StaticVisitor> |
| 260 | void RelocInfo::Visit(Heap* heap) { |
| 261 | RelocInfo::Mode mode = rmode(); |
| 262 | if (mode == RelocInfo::EMBEDDED_OBJECT) { |
| 263 | StaticVisitor::VisitPointer(heap, target_object_address()); |
| 264 | } else if (RelocInfo::IsCodeTarget(mode)) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 265 | StaticVisitor::VisitCodeTarget(heap, this); |
| 266 | } else if (mode == RelocInfo::GLOBAL_PROPERTY_CELL) { |
| 267 | StaticVisitor::VisitGlobalPropertyCell(heap, this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 268 | } else if (mode == RelocInfo::EXTERNAL_REFERENCE) { |
| 269 | StaticVisitor::VisitExternalReference(target_reference_address()); |
| 270 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 271 | } else if (heap->isolate()->debug()->has_break_points() && |
| 272 | ((RelocInfo::IsJSReturn(mode) && |
| 273 | IsPatchedReturnSequence()) || |
| 274 | (RelocInfo::IsDebugBreakSlot(mode) && |
| 275 | IsPatchedDebugBreakSlotSequence()))) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 276 | StaticVisitor::VisitDebugTarget(heap, this); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 277 | #endif |
| 278 | } else if (mode == RelocInfo::RUNTIME_ENTRY) { |
| 279 | StaticVisitor::VisitRuntimeEntry(this); |
| 280 | } |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | |
| 284 | // ----------------------------------------------------------------------------- |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame^] | 285 | // Assembler. |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 286 | |
| 287 | |
| 288 | void Assembler::CheckBuffer() { |
| 289 | if (buffer_space() <= kGap) { |
| 290 | GrowBuffer(); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 295 | void Assembler::CheckTrampolinePoolQuick() { |
| 296 | if (pc_offset() >= next_buffer_check_) { |
| 297 | CheckTrampolinePool(); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 302 | void Assembler::emit(Instr x) { |
| 303 | CheckBuffer(); |
| 304 | *reinterpret_cast<Instr*>(pc_) = x; |
| 305 | pc_ += kInstrSize; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 306 | CheckTrampolinePoolQuick(); |
Andrei Popescu | 3100271 | 2010-02-23 13:46:05 +0000 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | |
| 310 | } } // namespace v8::internal |
| 311 | |
| 312 | #endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_ |