2 // Copyright (c) 1994-2006 Sun Microsystems Inc.
3 // All Rights Reserved.
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // - Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
12 // - Redistribution in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the distribution.
16 // - Neither the name of Sun Microsystems or the names of contributors may
17 // be used to endorse or promote products derived from this software without
18 // specific prior written permission.
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
21 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 // The original source code covered by the above license above has been
33 // modified significantly by Google Inc.
34 // Copyright 2012 the V8 project authors. All rights reserved.
37 #ifndef V8_MIPS_ASSEMBLER_MIPS_INL_H_
38 #define V8_MIPS_ASSEMBLER_MIPS_INL_H_
40 #include "src/mips/assembler-mips.h"
42 #include "src/assembler.h"
43 #include "src/debug.h"
50 bool CpuFeatures::SupportsCrankshaft() { return IsSupported(FPU); }
53 // -----------------------------------------------------------------------------
54 // Operand and MemOperand.
56 Operand::Operand(int32_t immediate, RelocInfo::Mode rmode) {
63 Operand::Operand(const ExternalReference& f) {
65 imm32_ = reinterpret_cast<int32_t>(f.address());
66 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
70 Operand::Operand(Smi* value) {
72 imm32_ = reinterpret_cast<intptr_t>(value);
73 rmode_ = RelocInfo::NONE32;
77 Operand::Operand(Register rm) {
82 bool Operand::is_reg() const {
83 return rm_.is_valid();
87 int Register::NumAllocatableRegisters() {
88 return kMaxNumAllocatableRegisters;
92 int DoubleRegister::NumRegisters() {
93 return FPURegister::kMaxNumRegisters;
97 int DoubleRegister::NumAllocatableRegisters() {
98 return FPURegister::kMaxNumAllocatableRegisters;
102 int DoubleRegister::NumAllocatableAliasedRegisters() {
103 return NumAllocatableRegisters();
107 int FPURegister::ToAllocationIndex(FPURegister reg) {
108 DCHECK(reg.code() % 2 == 0);
109 DCHECK(reg.code() / 2 < kMaxNumAllocatableRegisters);
110 DCHECK(reg.is_valid());
111 DCHECK(!reg.is(kDoubleRegZero));
112 DCHECK(!reg.is(kLithiumScratchDouble));
113 return (reg.code() / 2);
117 // -----------------------------------------------------------------------------
120 void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) {
121 if (IsCodeTarget(rmode_)) {
122 uint32_t scope1 = (uint32_t) target_address() & ~kImm28Mask;
123 uint32_t scope2 = reinterpret_cast<uint32_t>(pc_) & ~kImm28Mask;
125 if (scope1 != scope2) {
126 Assembler::JumpLabelToJumpRegister(pc_);
129 if (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
130 // Absolute code pointer inside code object moves with the code object.
131 byte* p = reinterpret_cast<byte*>(pc_);
132 int count = Assembler::RelocateInternalReference(rmode_, p, delta);
133 CpuFeatures::FlushICache(p, count * sizeof(uint32_t));
138 Address RelocInfo::target_address() {
139 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
140 return Assembler::target_address_at(pc_, host_);
144 Address RelocInfo::target_address_address() {
145 DCHECK(IsCodeTarget(rmode_) ||
146 IsRuntimeEntry(rmode_) ||
147 rmode_ == EMBEDDED_OBJECT ||
148 rmode_ == EXTERNAL_REFERENCE);
149 // Read the address of the word containing the target_address in an
150 // instruction stream.
151 // The only architecture-independent user of this function is the serializer.
152 // The serializer uses it to find out how many raw bytes of instruction to
153 // output before the next target.
154 // For an instruction like LUI/ORI where the target bits are mixed into the
155 // instruction bits, the size of the target will be zero, indicating that the
156 // serializer should not step forward in memory after a target is resolved
157 // and written. In this case the target_address_address function should
158 // return the end of the instructions to be patched, allowing the
159 // deserializer to deserialize the instructions as raw bytes and put them in
160 // place, ready to be patched with the target. After jump optimization,
161 // that is the address of the instruction that follows J/JAL/JR/JALR
163 return reinterpret_cast<Address>(
164 pc_ + Assembler::kInstructionsFor32BitConstant * Assembler::kInstrSize);
168 Address RelocInfo::constant_pool_entry_address() {
174 int RelocInfo::target_address_size() {
175 return Assembler::kSpecialTargetSize;
179 void RelocInfo::set_target_address(Address target,
180 WriteBarrierMode write_barrier_mode,
181 ICacheFlushMode icache_flush_mode) {
182 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
183 Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
184 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
185 host() != NULL && IsCodeTarget(rmode_)) {
186 Object* target_code = Code::GetCodeFromTargetAddress(target);
187 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
188 host(), this, HeapObject::cast(target_code));
193 Address Assembler::target_address_from_return_address(Address pc) {
194 return pc - kCallTargetAddressOffset;
198 Address Assembler::break_address_from_return_address(Address pc) {
199 return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
203 void Assembler::set_target_internal_reference_encoded_at(Address pc,
205 // Encoded internal references are lui/ori load of 32-bit abolute address.
206 Instr instr_lui = Assembler::instr_at(pc + 0 * Assembler::kInstrSize);
207 Instr instr_ori = Assembler::instr_at(pc + 1 * Assembler::kInstrSize);
208 DCHECK(Assembler::IsLui(instr_lui));
209 DCHECK(Assembler::IsOri(instr_ori));
210 instr_lui &= ~kImm16Mask;
211 instr_ori &= ~kImm16Mask;
212 int32_t imm = reinterpret_cast<int32_t>(target);
213 DCHECK((imm & 3) == 0);
214 Assembler::instr_at_put(pc + 0 * Assembler::kInstrSize,
215 instr_lui | ((imm >> kLuiShift) & kImm16Mask));
216 Assembler::instr_at_put(pc + 1 * Assembler::kInstrSize,
217 instr_ori | (imm & kImm16Mask));
219 // Currently used only by deserializer, and all code will be flushed
220 // after complete deserialization, no need to flush on each reference.
224 void Assembler::deserialization_set_target_internal_reference_at(
225 Address pc, Address target, RelocInfo::Mode mode) {
226 if (mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
227 DCHECK(IsLui(instr_at(pc)));
228 set_target_internal_reference_encoded_at(pc, target);
230 DCHECK(mode == RelocInfo::INTERNAL_REFERENCE);
231 Memory::Address_at(pc) = target;
236 Object* RelocInfo::target_object() {
237 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
238 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
242 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
243 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
244 return Handle<Object>(reinterpret_cast<Object**>(
245 Assembler::target_address_at(pc_, host_)));
249 void RelocInfo::set_target_object(Object* target,
250 WriteBarrierMode write_barrier_mode,
251 ICacheFlushMode icache_flush_mode) {
252 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
253 Assembler::set_target_address_at(pc_, host_,
254 reinterpret_cast<Address>(target),
256 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
258 target->IsHeapObject()) {
259 host()->GetHeap()->incremental_marking()->RecordWrite(
260 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
265 Address RelocInfo::target_external_reference() {
266 DCHECK(rmode_ == EXTERNAL_REFERENCE);
267 return Assembler::target_address_at(pc_, host_);
271 Address RelocInfo::target_internal_reference() {
272 if (rmode_ == INTERNAL_REFERENCE) {
273 return Memory::Address_at(pc_);
275 // Encoded internal references are lui/ori load of 32-bit abolute address.
276 DCHECK(rmode_ == INTERNAL_REFERENCE_ENCODED);
277 Instr instr_lui = Assembler::instr_at(pc_ + 0 * Assembler::kInstrSize);
278 Instr instr_ori = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
279 DCHECK(Assembler::IsLui(instr_lui));
280 DCHECK(Assembler::IsOri(instr_ori));
281 int32_t imm = (instr_lui & static_cast<int32_t>(kImm16Mask)) << kLuiShift;
282 imm |= (instr_ori & static_cast<int32_t>(kImm16Mask));
283 return reinterpret_cast<Address>(imm);
288 Address RelocInfo::target_internal_reference_address() {
289 DCHECK(rmode_ == INTERNAL_REFERENCE || rmode_ == INTERNAL_REFERENCE_ENCODED);
290 return reinterpret_cast<Address>(pc_);
294 Address RelocInfo::target_runtime_entry(Assembler* origin) {
295 DCHECK(IsRuntimeEntry(rmode_));
296 return target_address();
300 void RelocInfo::set_target_runtime_entry(Address target,
301 WriteBarrierMode write_barrier_mode,
302 ICacheFlushMode icache_flush_mode) {
303 DCHECK(IsRuntimeEntry(rmode_));
304 if (target_address() != target)
305 set_target_address(target, write_barrier_mode, icache_flush_mode);
309 Handle<Cell> RelocInfo::target_cell_handle() {
310 DCHECK(rmode_ == RelocInfo::CELL);
311 Address address = Memory::Address_at(pc_);
312 return Handle<Cell>(reinterpret_cast<Cell**>(address));
316 Cell* RelocInfo::target_cell() {
317 DCHECK(rmode_ == RelocInfo::CELL);
318 return Cell::FromValueAddress(Memory::Address_at(pc_));
322 void RelocInfo::set_target_cell(Cell* cell,
323 WriteBarrierMode write_barrier_mode,
324 ICacheFlushMode icache_flush_mode) {
325 DCHECK(rmode_ == RelocInfo::CELL);
326 Address address = cell->address() + Cell::kValueOffset;
327 Memory::Address_at(pc_) = address;
328 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
329 // TODO(1550) We are passing NULL as a slot because cell can never be on
330 // evacuation candidate.
331 host()->GetHeap()->incremental_marking()->RecordWrite(
337 static const int kNoCodeAgeSequenceLength = 7 * Assembler::kInstrSize;
340 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
341 UNREACHABLE(); // This should never be reached on Arm.
342 return Handle<Object>();
346 Code* RelocInfo::code_age_stub() {
347 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
348 return Code::GetCodeFromTargetAddress(
349 Assembler::target_address_at(pc_ + Assembler::kInstrSize, host_));
353 void RelocInfo::set_code_age_stub(Code* stub,
354 ICacheFlushMode icache_flush_mode) {
355 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
356 Assembler::set_target_address_at(pc_ + Assembler::kInstrSize,
358 stub->instruction_start());
362 Address RelocInfo::call_address() {
363 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
364 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
365 // The pc_ offset of 0 assumes mips patched return sequence per
366 // debug-mips.cc BreakLocation::SetDebugBreakAtReturn(), or
367 // debug break slot per BreakLocation::SetDebugBreakAtSlot().
368 return Assembler::target_address_at(pc_, host_);
372 void RelocInfo::set_call_address(Address target) {
373 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
374 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
375 // The pc_ offset of 0 assumes mips patched return sequence per
376 // debug-mips.cc BreakLocation::SetDebugBreakAtReturn(), or
377 // debug break slot per BreakLocation::SetDebugBreakAtSlot().
378 Assembler::set_target_address_at(pc_, host_, target);
379 if (host() != NULL) {
380 Object* target_code = Code::GetCodeFromTargetAddress(target);
381 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
382 host(), this, HeapObject::cast(target_code));
387 Object* RelocInfo::call_object() {
388 return *call_object_address();
392 Object** RelocInfo::call_object_address() {
393 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
394 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
395 return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
399 void RelocInfo::set_call_object(Object* target) {
400 *call_object_address() = target;
404 void RelocInfo::WipeOut() {
405 DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
406 IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
407 IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
408 if (IsInternalReference(rmode_)) {
409 Memory::Address_at(pc_) = NULL;
410 } else if (IsInternalReferenceEncoded(rmode_)) {
411 Assembler::set_target_internal_reference_encoded_at(pc_, nullptr);
413 Assembler::set_target_address_at(pc_, host_, NULL);
418 bool RelocInfo::IsPatchedReturnSequence() {
419 Instr instr0 = Assembler::instr_at(pc_);
420 Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
421 Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize);
422 bool patched_return = ((instr0 & kOpcodeMask) == LUI &&
423 (instr1 & kOpcodeMask) == ORI &&
424 ((instr2 & kOpcodeMask) == JAL ||
425 ((instr2 & kOpcodeMask) == SPECIAL &&
426 (instr2 & kFunctionFieldMask) == JALR)));
427 return patched_return;
431 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
432 Instr current_instr = Assembler::instr_at(pc_);
433 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
437 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
438 RelocInfo::Mode mode = rmode();
439 if (mode == RelocInfo::EMBEDDED_OBJECT) {
440 visitor->VisitEmbeddedPointer(this);
441 } else if (RelocInfo::IsCodeTarget(mode)) {
442 visitor->VisitCodeTarget(this);
443 } else if (mode == RelocInfo::CELL) {
444 visitor->VisitCell(this);
445 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
446 visitor->VisitExternalReference(this);
447 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
448 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
449 visitor->VisitInternalReference(this);
450 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
451 visitor->VisitCodeAgeSequence(this);
452 } else if (((RelocInfo::IsJSReturn(mode) &&
453 IsPatchedReturnSequence()) ||
454 (RelocInfo::IsDebugBreakSlot(mode) &&
455 IsPatchedDebugBreakSlotSequence())) &&
456 isolate->debug()->has_break_points()) {
457 visitor->VisitDebugTarget(this);
458 } else if (RelocInfo::IsRuntimeEntry(mode)) {
459 visitor->VisitRuntimeEntry(this);
464 template<typename StaticVisitor>
465 void RelocInfo::Visit(Heap* heap) {
466 RelocInfo::Mode mode = rmode();
467 if (mode == RelocInfo::EMBEDDED_OBJECT) {
468 StaticVisitor::VisitEmbeddedPointer(heap, this);
469 } else if (RelocInfo::IsCodeTarget(mode)) {
470 StaticVisitor::VisitCodeTarget(heap, this);
471 } else if (mode == RelocInfo::CELL) {
472 StaticVisitor::VisitCell(heap, this);
473 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
474 StaticVisitor::VisitExternalReference(this);
475 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
476 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
477 StaticVisitor::VisitInternalReference(this);
478 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
479 StaticVisitor::VisitCodeAgeSequence(heap, this);
480 } else if (heap->isolate()->debug()->has_break_points() &&
481 ((RelocInfo::IsJSReturn(mode) &&
482 IsPatchedReturnSequence()) ||
483 (RelocInfo::IsDebugBreakSlot(mode) &&
484 IsPatchedDebugBreakSlotSequence()))) {
485 StaticVisitor::VisitDebugTarget(heap, this);
486 } else if (RelocInfo::IsRuntimeEntry(mode)) {
487 StaticVisitor::VisitRuntimeEntry(this);
492 // -----------------------------------------------------------------------------
496 void Assembler::CheckBuffer() {
497 if (buffer_space() <= kGap) {
503 void Assembler::CheckTrampolinePoolQuick() {
504 if (pc_offset() >= next_buffer_check_) {
505 CheckTrampolinePool();
510 void Assembler::emit(Instr x) {
511 if (!is_buffer_growth_blocked()) {
514 *reinterpret_cast<Instr*>(pc_) = x;
516 CheckTrampolinePoolQuick();
520 } } // namespace v8::internal
522 #endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_