1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
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
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
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 // OF THE POSSIBILITY OF SUCH DAMAGE.
33 // The original source code covered by the above license above has been modified
34 // significantly by Google Inc.
35 // Copyright 2014 the V8 project authors. All rights reserved.
37 #ifndef V8_PPC_ASSEMBLER_PPC_INL_H_
38 #define V8_PPC_ASSEMBLER_PPC_INL_H_
40 #include "src/ppc/assembler-ppc.h"
42 #include "src/assembler.h"
43 #include "src/debug.h"
50 bool CpuFeatures::SupportsCrankshaft() { return true; }
53 void RelocInfo::apply(intptr_t delta, ICacheFlushMode icache_flush_mode) {
54 if (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
55 // absolute code pointer inside code object moves with the code object.
56 Assembler::RelocateInternalReference(pc_, delta, 0, rmode_,
62 Address RelocInfo::target_address() {
63 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
64 return Assembler::target_address_at(pc_, host_);
68 Address RelocInfo::target_address_address() {
69 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) ||
70 rmode_ == EMBEDDED_OBJECT || rmode_ == EXTERNAL_REFERENCE);
72 #if V8_OOL_CONSTANT_POOL
73 if (Assembler::IsConstantPoolLoadStart(pc_)) {
74 // We return the PC for ool constant pool since this function is used by the
75 // serializerer and expects the address to reside within the code object.
76 return reinterpret_cast<Address>(pc_);
80 // Read the address of the word containing the target_address in an
81 // instruction stream.
82 // The only architecture-independent user of this function is the serializer.
83 // The serializer uses it to find out how many raw bytes of instruction to
84 // output before the next target.
85 // For an instruction like LIS/ORI where the target bits are mixed into the
86 // instruction bits, the size of the target will be zero, indicating that the
87 // serializer should not step forward in memory after a target is resolved
89 return reinterpret_cast<Address>(pc_);
93 Address RelocInfo::constant_pool_entry_address() {
94 #if V8_OOL_CONSTANT_POOL
95 return Assembler::target_constant_pool_address_at(pc_,
96 host_->constant_pool());
104 int RelocInfo::target_address_size() { return Assembler::kSpecialTargetSize; }
107 void RelocInfo::set_target_address(Address target,
108 WriteBarrierMode write_barrier_mode,
109 ICacheFlushMode icache_flush_mode) {
110 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
111 Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
112 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
113 IsCodeTarget(rmode_)) {
114 Object* target_code = Code::GetCodeFromTargetAddress(target);
115 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
116 host(), this, HeapObject::cast(target_code));
121 Address Assembler::break_address_from_return_address(Address pc) {
122 return target_address_from_return_address(pc);
126 Address Assembler::target_address_from_return_address(Address pc) {
127 // Returns the address of the call target from the return address that will
128 // be returned to after a call.
129 // Call sequence is :
130 // mov ip, @ call address
134 #if V8_OOL_CONSTANT_POOL
135 if (IsConstantPoolLoadEnd(pc - 3 * kInstrSize)) {
136 return pc - (kMovInstructionsConstantPool + 2) * kInstrSize;
139 return pc - (kMovInstructionsNoConstantPool + 2) * kInstrSize;
143 Address Assembler::return_address_from_call_start(Address pc) {
144 #if V8_OOL_CONSTANT_POOL
145 Address load_address = pc + (kMovInstructionsConstantPool - 1) * kInstrSize;
146 if (IsConstantPoolLoadEnd(load_address))
147 return pc + (kMovInstructionsConstantPool + 2) * kInstrSize;
149 return pc + (kMovInstructionsNoConstantPool + 2) * kInstrSize;
153 Object* RelocInfo::target_object() {
154 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
155 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
159 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
160 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
161 return Handle<Object>(
162 reinterpret_cast<Object**>(Assembler::target_address_at(pc_, host_)));
166 void RelocInfo::set_target_object(Object* target,
167 WriteBarrierMode write_barrier_mode,
168 ICacheFlushMode icache_flush_mode) {
169 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
170 Assembler::set_target_address_at(
171 pc_, host_, reinterpret_cast<Address>(target), icache_flush_mode);
172 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
173 target->IsHeapObject()) {
174 host()->GetHeap()->incremental_marking()->RecordWrite(
175 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
180 Address RelocInfo::target_reference() {
181 DCHECK(rmode_ == EXTERNAL_REFERENCE);
182 return Assembler::target_address_at(pc_, host_);
186 Address RelocInfo::target_runtime_entry(Assembler* origin) {
187 DCHECK(IsRuntimeEntry(rmode_));
188 return target_address();
192 void RelocInfo::set_target_runtime_entry(Address target,
193 WriteBarrierMode write_barrier_mode,
194 ICacheFlushMode icache_flush_mode) {
195 DCHECK(IsRuntimeEntry(rmode_));
196 if (target_address() != target)
197 set_target_address(target, write_barrier_mode, icache_flush_mode);
201 Handle<Cell> RelocInfo::target_cell_handle() {
202 DCHECK(rmode_ == RelocInfo::CELL);
203 Address address = Memory::Address_at(pc_);
204 return Handle<Cell>(reinterpret_cast<Cell**>(address));
208 Cell* RelocInfo::target_cell() {
209 DCHECK(rmode_ == RelocInfo::CELL);
210 return Cell::FromValueAddress(Memory::Address_at(pc_));
214 void RelocInfo::set_target_cell(Cell* cell, WriteBarrierMode write_barrier_mode,
215 ICacheFlushMode icache_flush_mode) {
216 DCHECK(rmode_ == RelocInfo::CELL);
217 Address address = cell->address() + Cell::kValueOffset;
218 Memory::Address_at(pc_) = address;
219 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
220 // TODO(1550) We are passing NULL as a slot because cell can never be on
221 // evacuation candidate.
222 host()->GetHeap()->incremental_marking()->RecordWrite(host(), NULL, cell);
227 #if V8_OOL_CONSTANT_POOL
228 static const int kNoCodeAgeInstructions = 7;
230 static const int kNoCodeAgeInstructions = 6;
232 static const int kCodeAgingInstructions =
233 Assembler::kMovInstructionsNoConstantPool + 3;
234 static const int kNoCodeAgeSequenceInstructions =
235 ((kNoCodeAgeInstructions >= kCodeAgingInstructions)
236 ? kNoCodeAgeInstructions
237 : kCodeAgingInstructions);
238 static const int kNoCodeAgeSequenceNops =
239 (kNoCodeAgeSequenceInstructions - kNoCodeAgeInstructions);
240 static const int kCodeAgingSequenceNops =
241 (kNoCodeAgeSequenceInstructions - kCodeAgingInstructions);
242 static const int kCodeAgingTargetDelta = 1 * Assembler::kInstrSize;
243 static const int kNoCodeAgeSequenceLength =
244 (kNoCodeAgeSequenceInstructions * Assembler::kInstrSize);
247 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
248 UNREACHABLE(); // This should never be reached on PPC.
249 return Handle<Object>();
253 Code* RelocInfo::code_age_stub() {
254 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
255 return Code::GetCodeFromTargetAddress(
256 Assembler::target_address_at(pc_ + kCodeAgingTargetDelta, host_));
260 void RelocInfo::set_code_age_stub(Code* stub,
261 ICacheFlushMode icache_flush_mode) {
262 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
263 Assembler::set_target_address_at(pc_ + kCodeAgingTargetDelta, host_,
264 stub->instruction_start(),
269 Address RelocInfo::call_address() {
270 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
271 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
272 // The pc_ offset of 0 assumes patched return sequence per
273 // BreakLocationIterator::SetDebugBreakAtReturn(), or debug break
274 // slot per BreakLocationIterator::SetDebugBreakAtSlot().
275 return Assembler::target_address_at(pc_, host_);
279 void RelocInfo::set_call_address(Address target) {
280 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
281 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
282 Assembler::set_target_address_at(pc_, host_, target);
283 if (host() != NULL) {
284 Object* target_code = Code::GetCodeFromTargetAddress(target);
285 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
286 host(), this, HeapObject::cast(target_code));
291 Object* RelocInfo::call_object() { return *call_object_address(); }
294 void RelocInfo::set_call_object(Object* target) {
295 *call_object_address() = target;
299 Object** RelocInfo::call_object_address() {
300 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
301 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
302 return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
306 void RelocInfo::WipeOut() {
307 DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
308 IsRuntimeEntry(rmode_) || IsExternalReference(rmode_));
309 Assembler::set_target_address_at(pc_, host_, NULL);
313 bool RelocInfo::IsPatchedReturnSequence() {
315 // The patched return sequence is defined by
316 // BreakLocationIterator::SetDebugBreakAtReturn()
319 Instr instr0 = Assembler::instr_at(pc_);
320 Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
321 #if V8_TARGET_ARCH_PPC64
322 Instr instr3 = Assembler::instr_at(pc_ + (3 * Assembler::kInstrSize));
323 Instr instr4 = Assembler::instr_at(pc_ + (4 * Assembler::kInstrSize));
324 Instr binstr = Assembler::instr_at(pc_ + (7 * Assembler::kInstrSize));
326 Instr binstr = Assembler::instr_at(pc_ + 4 * Assembler::kInstrSize);
328 bool patched_return =
329 ((instr0 & kOpcodeMask) == ADDIS && (instr1 & kOpcodeMask) == ORI &&
330 #if V8_TARGET_ARCH_PPC64
331 (instr3 & kOpcodeMask) == ORIS && (instr4 & kOpcodeMask) == ORI &&
333 (binstr == 0x7d821008)); // twge r2, r2
335 // printf("IsPatchedReturnSequence: %d\n", patched_return);
336 return patched_return;
340 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
341 Instr current_instr = Assembler::instr_at(pc_);
342 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
346 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
347 RelocInfo::Mode mode = rmode();
348 if (mode == RelocInfo::EMBEDDED_OBJECT) {
349 visitor->VisitEmbeddedPointer(this);
350 } else if (RelocInfo::IsCodeTarget(mode)) {
351 visitor->VisitCodeTarget(this);
352 } else if (mode == RelocInfo::CELL) {
353 visitor->VisitCell(this);
354 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
355 visitor->VisitExternalReference(this);
356 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
357 visitor->VisitCodeAgeSequence(this);
358 } else if (((RelocInfo::IsJSReturn(mode) && IsPatchedReturnSequence()) ||
359 (RelocInfo::IsDebugBreakSlot(mode) &&
360 IsPatchedDebugBreakSlotSequence())) &&
361 isolate->debug()->has_break_points()) {
362 visitor->VisitDebugTarget(this);
363 } else if (IsRuntimeEntry(mode)) {
364 visitor->VisitRuntimeEntry(this);
369 template <typename StaticVisitor>
370 void RelocInfo::Visit(Heap* heap) {
371 RelocInfo::Mode mode = rmode();
372 if (mode == RelocInfo::EMBEDDED_OBJECT) {
373 StaticVisitor::VisitEmbeddedPointer(heap, this);
374 } else if (RelocInfo::IsCodeTarget(mode)) {
375 StaticVisitor::VisitCodeTarget(heap, this);
376 } else if (mode == RelocInfo::CELL) {
377 StaticVisitor::VisitCell(heap, this);
378 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
379 StaticVisitor::VisitExternalReference(this);
380 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
381 StaticVisitor::VisitCodeAgeSequence(heap, this);
382 } else if (heap->isolate()->debug()->has_break_points() &&
383 ((RelocInfo::IsJSReturn(mode) && IsPatchedReturnSequence()) ||
384 (RelocInfo::IsDebugBreakSlot(mode) &&
385 IsPatchedDebugBreakSlotSequence()))) {
386 StaticVisitor::VisitDebugTarget(heap, this);
387 } else if (IsRuntimeEntry(mode)) {
388 StaticVisitor::VisitRuntimeEntry(this);
392 Operand::Operand(intptr_t immediate, RelocInfo::Mode rmode) {
398 Operand::Operand(const ExternalReference& f) {
400 imm_ = reinterpret_cast<intptr_t>(f.address());
401 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
404 Operand::Operand(Smi* value) {
406 imm_ = reinterpret_cast<intptr_t>(value);
407 rmode_ = kRelocInfo_NONEPTR;
410 Operand::Operand(Register rm) {
412 rmode_ = kRelocInfo_NONEPTR; // PPC -why doesn't ARM do this?
415 void Assembler::CheckBuffer() {
416 if (buffer_space() <= kGap) {
421 void Assembler::CheckTrampolinePoolQuick() {
422 if (pc_offset() >= next_buffer_check_) {
423 CheckTrampolinePool();
427 void Assembler::emit(Instr x) {
429 *reinterpret_cast<Instr*>(pc_) = x;
431 CheckTrampolinePoolQuick();
434 bool Operand::is_reg() const { return rm_.is_valid(); }
437 // Fetch the 32bit value from the FIXED_SEQUENCE lis/ori
438 Address Assembler::target_address_at(Address pc,
439 ConstantPoolArray* constant_pool) {
440 Instr instr1 = instr_at(pc);
441 Instr instr2 = instr_at(pc + kInstrSize);
442 // Interpret 2 instructions generated by lis/ori
443 if (IsLis(instr1) && IsOri(instr2)) {
444 #if V8_TARGET_ARCH_PPC64
445 Instr instr4 = instr_at(pc + (3 * kInstrSize));
446 Instr instr5 = instr_at(pc + (4 * kInstrSize));
447 // Assemble the 64 bit value.
448 uint64_t hi = (static_cast<uint32_t>((instr1 & kImm16Mask) << 16) |
449 static_cast<uint32_t>(instr2 & kImm16Mask));
450 uint64_t lo = (static_cast<uint32_t>((instr4 & kImm16Mask) << 16) |
451 static_cast<uint32_t>(instr5 & kImm16Mask));
452 return reinterpret_cast<Address>((hi << 32) | lo);
454 // Assemble the 32 bit value.
455 return reinterpret_cast<Address>(((instr1 & kImm16Mask) << 16) |
456 (instr2 & kImm16Mask));
459 #if V8_OOL_CONSTANT_POOL
460 return Memory::Address_at(target_constant_pool_address_at(pc, constant_pool));
468 #if V8_OOL_CONSTANT_POOL
469 bool Assembler::IsConstantPoolLoadStart(Address pc) {
470 #if V8_TARGET_ARCH_PPC64
471 if (!IsLi(instr_at(pc))) return false;
474 return GetRA(instr_at(pc)).is(kConstantPoolRegister);
478 bool Assembler::IsConstantPoolLoadEnd(Address pc) {
479 #if V8_TARGET_ARCH_PPC64
482 return IsConstantPoolLoadStart(pc);
486 int Assembler::GetConstantPoolOffset(Address pc) {
487 DCHECK(IsConstantPoolLoadStart(pc));
488 Instr instr = instr_at(pc);
489 int offset = SIGN_EXT_IMM16((instr & kImm16Mask));
494 void Assembler::SetConstantPoolOffset(Address pc, int offset) {
495 DCHECK(IsConstantPoolLoadStart(pc));
496 DCHECK(is_int16(offset));
497 Instr instr = instr_at(pc);
498 instr &= ~kImm16Mask;
499 instr |= (offset & kImm16Mask);
500 instr_at_put(pc, instr);
504 Address Assembler::target_constant_pool_address_at(
505 Address pc, ConstantPoolArray* constant_pool) {
506 Address addr = reinterpret_cast<Address>(constant_pool);
508 addr += GetConstantPoolOffset(pc);
514 // This sets the branch destination (which gets loaded at the call address).
515 // This is for calls and branches within generated code. The serializer
516 // has already deserialized the mov instructions etc.
517 // There is a FIXED_SEQUENCE assumption here
518 void Assembler::deserialization_set_special_target_at(
519 Address instruction_payload, Code* code, Address target) {
520 set_target_address_at(instruction_payload, code, target);
523 // This code assumes the FIXED_SEQUENCE of lis/ori
524 void Assembler::set_target_address_at(Address pc,
525 ConstantPoolArray* constant_pool,
527 ICacheFlushMode icache_flush_mode) {
528 Instr instr1 = instr_at(pc);
529 Instr instr2 = instr_at(pc + kInstrSize);
530 // Interpret 2 instructions generated by lis/ori
531 if (IsLis(instr1) && IsOri(instr2)) {
532 #if V8_TARGET_ARCH_PPC64
533 Instr instr4 = instr_at(pc + (3 * kInstrSize));
534 Instr instr5 = instr_at(pc + (4 * kInstrSize));
535 // Needs to be fixed up when mov changes to handle 64-bit values.
536 uint32_t* p = reinterpret_cast<uint32_t*>(pc);
537 uintptr_t itarget = reinterpret_cast<uintptr_t>(target);
539 instr5 &= ~kImm16Mask;
540 instr5 |= itarget & kImm16Mask;
541 itarget = itarget >> 16;
543 instr4 &= ~kImm16Mask;
544 instr4 |= itarget & kImm16Mask;
545 itarget = itarget >> 16;
547 instr2 &= ~kImm16Mask;
548 instr2 |= itarget & kImm16Mask;
549 itarget = itarget >> 16;
551 instr1 &= ~kImm16Mask;
552 instr1 |= itarget & kImm16Mask;
553 itarget = itarget >> 16;
559 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
560 CpuFeatures::FlushICache(p, 5 * kInstrSize);
563 uint32_t* p = reinterpret_cast<uint32_t*>(pc);
564 uint32_t itarget = reinterpret_cast<uint32_t>(target);
565 int lo_word = itarget & kImm16Mask;
566 int hi_word = itarget >> 16;
567 instr1 &= ~kImm16Mask;
569 instr2 &= ~kImm16Mask;
574 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
575 CpuFeatures::FlushICache(p, 2 * kInstrSize);
579 #if V8_OOL_CONSTANT_POOL
580 Memory::Address_at(target_constant_pool_address_at(pc, constant_pool)) =
588 } // namespace v8::internal
590 #endif // V8_PPC_ASSEMBLER_PPC_INL_H_