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) {
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 void Assembler::set_target_internal_reference_encoded_at(Address pc,
194 // Encoded internal references are j/jal instructions.
195 Instr instr = Assembler::instr_at(pc + 0 * Assembler::kInstrSize);
198 (reinterpret_cast<uint64_t>(target) & static_cast<uint64_t>(kImm28Mask));
200 instr &= ~kImm26Mask;
201 uint64_t imm26 = imm28 >> 2;
202 DCHECK(is_uint26(imm26));
204 instr_at_put(pc, instr | (imm26 & kImm26Mask));
205 // Currently used only by deserializer, and all code will be flushed
206 // after complete deserialization, no need to flush on each reference.
210 void Assembler::deserialization_set_target_internal_reference_at(
211 Address pc, Address target, RelocInfo::Mode mode) {
212 if (mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
213 DCHECK(IsJ(instr_at(pc)));
214 set_target_internal_reference_encoded_at(pc, target);
216 DCHECK(mode == RelocInfo::INTERNAL_REFERENCE);
217 Memory::Address_at(pc) = target;
222 Object* RelocInfo::target_object() {
223 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
224 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
228 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
229 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
230 return Handle<Object>(reinterpret_cast<Object**>(
231 Assembler::target_address_at(pc_, host_)));
235 void RelocInfo::set_target_object(Object* target,
236 WriteBarrierMode write_barrier_mode,
237 ICacheFlushMode icache_flush_mode) {
238 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
239 Assembler::set_target_address_at(pc_, host_,
240 reinterpret_cast<Address>(target),
242 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
244 target->IsHeapObject()) {
245 host()->GetHeap()->incremental_marking()->RecordWrite(
246 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
251 Address RelocInfo::target_external_reference() {
252 DCHECK(rmode_ == EXTERNAL_REFERENCE);
253 return Assembler::target_address_at(pc_, host_);
257 Address RelocInfo::target_internal_reference() {
258 if (rmode_ == INTERNAL_REFERENCE) {
259 return Memory::Address_at(pc_);
261 // Encoded internal references are j/jal instructions.
262 DCHECK(rmode_ == INTERNAL_REFERENCE_ENCODED);
263 Instr instr = Assembler::instr_at(pc_ + 0 * Assembler::kInstrSize);
265 uint64_t imm28 = instr << 2;
267 (reinterpret_cast<uint64_t>(pc_) & ~static_cast<uint64_t>(kImm28Mask));
268 return reinterpret_cast<Address>(segment | imm28);
273 Address RelocInfo::target_internal_reference_address() {
274 DCHECK(rmode_ == INTERNAL_REFERENCE || rmode_ == INTERNAL_REFERENCE_ENCODED);
275 return reinterpret_cast<Address>(pc_);
279 Address RelocInfo::target_runtime_entry(Assembler* origin) {
280 DCHECK(IsRuntimeEntry(rmode_));
281 return target_address();
285 void RelocInfo::set_target_runtime_entry(Address target,
286 WriteBarrierMode write_barrier_mode,
287 ICacheFlushMode icache_flush_mode) {
288 DCHECK(IsRuntimeEntry(rmode_));
289 if (target_address() != target)
290 set_target_address(target, write_barrier_mode, icache_flush_mode);
294 Handle<Cell> RelocInfo::target_cell_handle() {
295 DCHECK(rmode_ == RelocInfo::CELL);
296 Address address = Memory::Address_at(pc_);
297 return Handle<Cell>(reinterpret_cast<Cell**>(address));
301 Cell* RelocInfo::target_cell() {
302 DCHECK(rmode_ == RelocInfo::CELL);
303 return Cell::FromValueAddress(Memory::Address_at(pc_));
307 void RelocInfo::set_target_cell(Cell* cell,
308 WriteBarrierMode write_barrier_mode,
309 ICacheFlushMode icache_flush_mode) {
310 DCHECK(rmode_ == RelocInfo::CELL);
311 Address address = cell->address() + Cell::kValueOffset;
312 Memory::Address_at(pc_) = address;
313 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
314 // TODO(1550) We are passing NULL as a slot because cell can never be on
315 // evacuation candidate.
316 host()->GetHeap()->incremental_marking()->RecordWrite(
322 static const int kNoCodeAgeSequenceLength = 9 * Assembler::kInstrSize;
325 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
326 UNREACHABLE(); // This should never be reached on Arm.
327 return Handle<Object>();
331 Code* RelocInfo::code_age_stub() {
332 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
333 return Code::GetCodeFromTargetAddress(
334 Assembler::target_address_at(pc_ + Assembler::kInstrSize, host_));
338 void RelocInfo::set_code_age_stub(Code* stub,
339 ICacheFlushMode icache_flush_mode) {
340 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
341 Assembler::set_target_address_at(pc_ + Assembler::kInstrSize,
343 stub->instruction_start());
347 Address RelocInfo::debug_call_address() {
348 // The pc_ offset of 0 assumes patched debug break slot or return
350 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
351 return Assembler::target_address_at(pc_, host_);
355 void RelocInfo::set_debug_call_address(Address target) {
356 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
357 // The pc_ offset of 0 assumes patched debug break slot or return
359 Assembler::set_target_address_at(pc_, host_, target);
360 if (host() != NULL) {
361 Object* target_code = Code::GetCodeFromTargetAddress(target);
362 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
363 host(), this, HeapObject::cast(target_code));
368 void RelocInfo::WipeOut() {
369 DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
370 IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
371 IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
372 if (IsInternalReference(rmode_)) {
373 Memory::Address_at(pc_) = NULL;
374 } else if (IsInternalReferenceEncoded(rmode_)) {
375 Assembler::set_target_internal_reference_encoded_at(pc_, nullptr);
377 Assembler::set_target_address_at(pc_, host_, NULL);
382 bool RelocInfo::IsPatchedReturnSequence() {
383 Instr instr0 = Assembler::instr_at(pc_); // lui.
384 Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize); // ori.
385 Instr instr2 = Assembler::instr_at(pc_ + 2 * Assembler::kInstrSize); // dsll.
386 Instr instr3 = Assembler::instr_at(pc_ + 3 * Assembler::kInstrSize); // ori.
387 Instr instr4 = Assembler::instr_at(pc_ + 4 * Assembler::kInstrSize); // jalr.
389 bool patched_return = ((instr0 & kOpcodeMask) == LUI &&
390 (instr1 & kOpcodeMask) == ORI &&
391 (instr2 & kFunctionFieldMask) == DSLL &&
392 (instr3 & kOpcodeMask) == ORI &&
393 (instr4 & kFunctionFieldMask) == JALR);
394 return patched_return;
398 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
399 Instr current_instr = Assembler::instr_at(pc_);
400 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
404 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
405 RelocInfo::Mode mode = rmode();
406 if (mode == RelocInfo::EMBEDDED_OBJECT) {
407 visitor->VisitEmbeddedPointer(this);
408 } else if (RelocInfo::IsCodeTarget(mode)) {
409 visitor->VisitCodeTarget(this);
410 } else if (mode == RelocInfo::CELL) {
411 visitor->VisitCell(this);
412 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
413 visitor->VisitExternalReference(this);
414 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
415 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
416 visitor->VisitInternalReference(this);
417 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
418 visitor->VisitCodeAgeSequence(this);
419 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
420 IsPatchedDebugBreakSlotSequence() &&
421 isolate->debug()->has_break_points()) {
422 visitor->VisitDebugTarget(this);
423 } else if (RelocInfo::IsRuntimeEntry(mode)) {
424 visitor->VisitRuntimeEntry(this);
429 template<typename StaticVisitor>
430 void RelocInfo::Visit(Heap* heap) {
431 RelocInfo::Mode mode = rmode();
432 if (mode == RelocInfo::EMBEDDED_OBJECT) {
433 StaticVisitor::VisitEmbeddedPointer(heap, this);
434 } else if (RelocInfo::IsCodeTarget(mode)) {
435 StaticVisitor::VisitCodeTarget(heap, this);
436 } else if (mode == RelocInfo::CELL) {
437 StaticVisitor::VisitCell(heap, this);
438 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
439 StaticVisitor::VisitExternalReference(this);
440 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
441 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
442 StaticVisitor::VisitInternalReference(this);
443 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
444 StaticVisitor::VisitCodeAgeSequence(heap, this);
445 } else if (heap->isolate()->debug()->has_break_points() &&
446 RelocInfo::IsDebugBreakSlot(mode) &&
447 IsPatchedDebugBreakSlotSequence()) {
448 StaticVisitor::VisitDebugTarget(heap, this);
449 } else if (RelocInfo::IsRuntimeEntry(mode)) {
450 StaticVisitor::VisitRuntimeEntry(this);
455 // -----------------------------------------------------------------------------
459 void Assembler::CheckBuffer() {
460 if (buffer_space() <= kGap) {
466 void Assembler::CheckTrampolinePoolQuick(int extra_instructions) {
467 if (pc_offset() >= next_buffer_check_ - extra_instructions * kInstrSize) {
468 CheckTrampolinePool();
473 void Assembler::emit(Instr x) {
474 if (!is_buffer_growth_blocked()) {
477 *reinterpret_cast<Instr*>(pc_) = x;
479 CheckTrampolinePoolQuick();
483 void Assembler::emit(uint64_t x) {
484 if (!is_buffer_growth_blocked()) {
487 *reinterpret_cast<uint64_t*>(pc_) = x;
488 pc_ += kInstrSize * 2;
489 CheckTrampolinePoolQuick();
493 } } // namespace v8::internal
495 #endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_