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/mips64/assembler-mips64.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(int64_t immediate, RelocInfo::Mode rmode) {
63 Operand::Operand(const ExternalReference& f) {
65 imm64_ = reinterpret_cast<int64_t>(f.address());
66 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
70 Operand::Operand(Smi* value) {
72 imm64_ = 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 (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
122 // Absolute code pointer inside code object moves with the code object.
123 byte* p = reinterpret_cast<byte*>(pc_);
124 int count = Assembler::RelocateInternalReference(rmode_, p, delta);
125 CpuFeatures::FlushICache(p, count * sizeof(uint32_t));
130 Address RelocInfo::target_address() {
131 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
132 return Assembler::target_address_at(pc_, host_);
136 Address RelocInfo::target_address_address() {
137 DCHECK(IsCodeTarget(rmode_) ||
138 IsRuntimeEntry(rmode_) ||
139 rmode_ == EMBEDDED_OBJECT ||
140 rmode_ == EXTERNAL_REFERENCE);
141 // Read the address of the word containing the target_address in an
142 // instruction stream.
143 // The only architecture-independent user of this function is the serializer.
144 // The serializer uses it to find out how many raw bytes of instruction to
145 // output before the next target.
146 // For an instruction like LUI/ORI where the target bits are mixed into the
147 // instruction bits, the size of the target will be zero, indicating that the
148 // serializer should not step forward in memory after a target is resolved
149 // and written. In this case the target_address_address function should
150 // return the end of the instructions to be patched, allowing the
151 // deserializer to deserialize the instructions as raw bytes and put them in
152 // place, ready to be patched with the target. After jump optimization,
153 // that is the address of the instruction that follows J/JAL/JR/JALR
155 // return reinterpret_cast<Address>(
156 // pc_ + Assembler::kInstructionsFor32BitConstant * Assembler::kInstrSize);
157 return reinterpret_cast<Address>(
158 pc_ + Assembler::kInstructionsFor64BitConstant * Assembler::kInstrSize);
162 Address RelocInfo::constant_pool_entry_address() {
168 int RelocInfo::target_address_size() {
169 return Assembler::kSpecialTargetSize;
173 void RelocInfo::set_target_address(Address target,
174 WriteBarrierMode write_barrier_mode,
175 ICacheFlushMode icache_flush_mode) {
176 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
177 Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
178 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
179 host() != NULL && IsCodeTarget(rmode_)) {
180 Object* target_code = Code::GetCodeFromTargetAddress(target);
181 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
182 host(), this, HeapObject::cast(target_code));
187 Address Assembler::target_address_from_return_address(Address pc) {
188 return pc - kCallTargetAddressOffset;
192 Address Assembler::break_address_from_return_address(Address pc) {
193 return pc - Assembler::kPatchDebugBreakSlotReturnOffset;
197 Object* RelocInfo::target_object() {
198 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
199 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
203 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
204 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
205 return Handle<Object>(reinterpret_cast<Object**>(
206 Assembler::target_address_at(pc_, host_)));
210 void RelocInfo::set_target_object(Object* target,
211 WriteBarrierMode write_barrier_mode,
212 ICacheFlushMode icache_flush_mode) {
213 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
214 Assembler::set_target_address_at(pc_, host_,
215 reinterpret_cast<Address>(target),
217 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
219 target->IsHeapObject()) {
220 host()->GetHeap()->incremental_marking()->RecordWrite(
221 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
226 Address RelocInfo::target_external_reference() {
227 DCHECK(rmode_ == EXTERNAL_REFERENCE);
228 return Assembler::target_address_at(pc_, host_);
232 Address RelocInfo::target_internal_reference() {
233 if (rmode_ == INTERNAL_REFERENCE) {
234 return Memory::Address_at(pc_);
236 DCHECK(rmode_ == INTERNAL_REFERENCE_ENCODED);
237 Instr instr_lui = Assembler::instr_at(pc_ + 0 * Assembler::kInstrSize);
238 Instr instr_ori = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
239 Instr instr_ori2 = Assembler::instr_at(pc_ + 3 * Assembler::kInstrSize);
240 DCHECK(Assembler::IsLui(instr_lui));
241 DCHECK(Assembler::IsOri(instr_ori));
242 DCHECK(Assembler::IsOri(instr_ori2));
243 int64_t imm = (instr_lui & static_cast<int64_t>(kImm16Mask)) << 32;
244 imm |= (instr_ori & static_cast<int64_t>(kImm16Mask)) << 16;
245 imm |= (instr_ori2 & static_cast<int64_t>(kImm16Mask));
246 return reinterpret_cast<Address>(imm);
251 void RelocInfo::set_target_internal_reference(Address target) {
252 if (rmode_ == INTERNAL_REFERENCE) {
253 Memory::Address_at(pc_) = target;
255 // Encoded internal references are lui/ori load of 48-bit abolute address.
256 DCHECK(rmode_ == INTERNAL_REFERENCE_ENCODED);
257 Instr instr_lui = Assembler::instr_at(pc_ + 0 * Assembler::kInstrSize);
258 Instr instr_ori = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
259 Instr instr_ori2 = Assembler::instr_at(pc_ + 3 * Assembler::kInstrSize);
260 DCHECK(Assembler::IsLui(instr_lui));
261 DCHECK(Assembler::IsOri(instr_ori));
262 DCHECK(Assembler::IsOri(instr_ori2));
263 instr_lui &= ~kImm16Mask;
264 instr_ori &= ~kImm16Mask;
265 instr_ori2 &= ~kImm16Mask;
266 int64_t imm = reinterpret_cast<int64_t>(target);
267 DCHECK((imm & 3) == 0);
268 Assembler::instr_at_put(pc_ + 0 * Assembler::kInstrSize,
269 instr_lui | ((imm >> 32) & kImm16Mask));
270 Assembler::instr_at_put(pc_ + 1 * Assembler::kInstrSize,
271 instr_ori | ((imm >> 16) & kImm16Mask));
272 Assembler::instr_at_put(pc_ + 3 * Assembler::kInstrSize,
273 instr_ori | (imm & kImm16Mask));
274 // Currently used only by deserializer, and all code will be flushed
275 // after complete deserialization, no need to flush on each reference.
280 Address RelocInfo::target_runtime_entry(Assembler* origin) {
281 DCHECK(IsRuntimeEntry(rmode_));
282 return target_address();
286 void RelocInfo::set_target_runtime_entry(Address target,
287 WriteBarrierMode write_barrier_mode,
288 ICacheFlushMode icache_flush_mode) {
289 DCHECK(IsRuntimeEntry(rmode_));
290 if (target_address() != target)
291 set_target_address(target, write_barrier_mode, icache_flush_mode);
295 Handle<Cell> RelocInfo::target_cell_handle() {
296 DCHECK(rmode_ == RelocInfo::CELL);
297 Address address = Memory::Address_at(pc_);
298 return Handle<Cell>(reinterpret_cast<Cell**>(address));
302 Cell* RelocInfo::target_cell() {
303 DCHECK(rmode_ == RelocInfo::CELL);
304 return Cell::FromValueAddress(Memory::Address_at(pc_));
308 void RelocInfo::set_target_cell(Cell* cell,
309 WriteBarrierMode write_barrier_mode,
310 ICacheFlushMode icache_flush_mode) {
311 DCHECK(rmode_ == RelocInfo::CELL);
312 Address address = cell->address() + Cell::kValueOffset;
313 Memory::Address_at(pc_) = address;
314 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
315 // TODO(1550) We are passing NULL as a slot because cell can never be on
316 // evacuation candidate.
317 host()->GetHeap()->incremental_marking()->RecordWrite(
323 static const int kNoCodeAgeSequenceLength = 9 * Assembler::kInstrSize;
326 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
327 UNREACHABLE(); // This should never be reached on Arm.
328 return Handle<Object>();
332 Code* RelocInfo::code_age_stub() {
333 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
334 return Code::GetCodeFromTargetAddress(
335 Assembler::target_address_at(pc_ + Assembler::kInstrSize, host_));
339 void RelocInfo::set_code_age_stub(Code* stub,
340 ICacheFlushMode icache_flush_mode) {
341 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
342 Assembler::set_target_address_at(pc_ + Assembler::kInstrSize,
344 stub->instruction_start());
348 Address RelocInfo::call_address() {
349 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
350 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
351 // The pc_ offset of 0 assumes mips patched return sequence per
352 // debug-mips.cc BreakLocation::SetDebugBreakAtReturn(), or
353 // debug break slot per BreakLocation::SetDebugBreakAtSlot().
354 return Assembler::target_address_at(pc_, host_);
358 void RelocInfo::set_call_address(Address target) {
359 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
360 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
361 // The pc_ offset of 0 assumes mips patched return sequence per
362 // debug-mips.cc BreakLocation::SetDebugBreakAtReturn(), or
363 // debug break slot per BreakLocation::SetDebugBreakAtSlot().
364 Assembler::set_target_address_at(pc_, host_, target);
365 if (host() != NULL) {
366 Object* target_code = Code::GetCodeFromTargetAddress(target);
367 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
368 host(), this, HeapObject::cast(target_code));
373 Object* RelocInfo::call_object() {
374 return *call_object_address();
378 Object** RelocInfo::call_object_address() {
379 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
380 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
381 return reinterpret_cast<Object**>(pc_ + 6 * Assembler::kInstrSize);
385 void RelocInfo::set_call_object(Object* target) {
386 *call_object_address() = target;
390 void RelocInfo::WipeOut() {
391 DCHECK(IsEmbeddedObject(rmode_) ||
392 IsCodeTarget(rmode_) ||
393 IsRuntimeEntry(rmode_) ||
394 IsExternalReference(rmode_));
395 Assembler::set_target_address_at(pc_, host_, NULL);
399 bool RelocInfo::IsPatchedReturnSequence() {
400 Instr instr0 = Assembler::instr_at(pc_); // lui.
401 Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize); // ori.
402 Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize); // dsll.
403 Instr instr3 = Assembler::instr_at(pc_ + 3 * Assembler::kInstrSize); // ori.
404 Instr instr4 = Assembler::instr_at(pc_ + 4 * Assembler::kInstrSize); // jalr.
406 bool patched_return = ((instr0 & kOpcodeMask) == LUI &&
407 (instr1 & kOpcodeMask) == ORI &&
408 (instr2 & kFunctionFieldMask) == DSLL &&
409 (instr3 & kOpcodeMask) == ORI &&
410 (instr4 & kFunctionFieldMask) == JALR);
411 return patched_return;
415 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
416 Instr current_instr = Assembler::instr_at(pc_);
417 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
421 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
422 RelocInfo::Mode mode = rmode();
423 if (mode == RelocInfo::EMBEDDED_OBJECT) {
424 visitor->VisitEmbeddedPointer(this);
425 } else if (RelocInfo::IsCodeTarget(mode)) {
426 visitor->VisitCodeTarget(this);
427 } else if (mode == RelocInfo::CELL) {
428 visitor->VisitCell(this);
429 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
430 visitor->VisitExternalReference(this);
431 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
432 visitor->VisitCodeAgeSequence(this);
433 } else if (((RelocInfo::IsJSReturn(mode) &&
434 IsPatchedReturnSequence()) ||
435 (RelocInfo::IsDebugBreakSlot(mode) &&
436 IsPatchedDebugBreakSlotSequence())) &&
437 isolate->debug()->has_break_points()) {
438 visitor->VisitDebugTarget(this);
439 } else if (RelocInfo::IsRuntimeEntry(mode)) {
440 visitor->VisitRuntimeEntry(this);
445 template<typename StaticVisitor>
446 void RelocInfo::Visit(Heap* heap) {
447 RelocInfo::Mode mode = rmode();
448 if (mode == RelocInfo::EMBEDDED_OBJECT) {
449 StaticVisitor::VisitEmbeddedPointer(heap, this);
450 } else if (RelocInfo::IsCodeTarget(mode)) {
451 StaticVisitor::VisitCodeTarget(heap, this);
452 } else if (mode == RelocInfo::CELL) {
453 StaticVisitor::VisitCell(heap, this);
454 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
455 StaticVisitor::VisitExternalReference(this);
456 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
457 StaticVisitor::VisitCodeAgeSequence(heap, this);
458 } else if (heap->isolate()->debug()->has_break_points() &&
459 ((RelocInfo::IsJSReturn(mode) &&
460 IsPatchedReturnSequence()) ||
461 (RelocInfo::IsDebugBreakSlot(mode) &&
462 IsPatchedDebugBreakSlotSequence()))) {
463 StaticVisitor::VisitDebugTarget(heap, this);
464 } else if (RelocInfo::IsRuntimeEntry(mode)) {
465 StaticVisitor::VisitRuntimeEntry(this);
470 // -----------------------------------------------------------------------------
474 void Assembler::CheckBuffer() {
475 if (buffer_space() <= kGap) {
481 void Assembler::CheckTrampolinePoolQuick() {
482 if (pc_offset() >= next_buffer_check_) {
483 CheckTrampolinePool();
488 void Assembler::emit(Instr x) {
489 if (!is_buffer_growth_blocked()) {
492 *reinterpret_cast<Instr*>(pc_) = x;
494 CheckTrampolinePoolQuick();
498 void Assembler::emit(uint64_t x) {
499 if (!is_buffer_growth_blocked()) {
502 *reinterpret_cast<uint64_t*>(pc_) = x;
503 pc_ += kInstrSize * 2;
504 CheckTrampolinePoolQuick();
508 } } // namespace v8::internal
510 #endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_