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 // absolute code pointer inside code object moves with the code object.
55 if (IsInternalReference(rmode_)) {
57 Address target = Memory::Address_at(pc_);
58 Memory::Address_at(pc_) = target + delta;
61 DCHECK(IsInternalReferenceEncoded(rmode_));
62 Address target = Assembler::target_address_at(pc_, host_);
63 Assembler::set_target_address_at(pc_, host_, target + delta,
69 Address RelocInfo::target_internal_reference() {
70 if (IsInternalReference(rmode_)) {
72 return Memory::Address_at(pc_);
75 DCHECK(IsInternalReferenceEncoded(rmode_));
76 return Assembler::target_address_at(pc_, host_);
81 Address RelocInfo::target_internal_reference_address() {
82 DCHECK(IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
83 return reinterpret_cast<Address>(pc_);
87 Address RelocInfo::target_address() {
88 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
89 return Assembler::target_address_at(pc_, host_);
93 Address RelocInfo::target_address_address() {
94 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) ||
95 rmode_ == EMBEDDED_OBJECT || rmode_ == EXTERNAL_REFERENCE);
97 // Read the address of the word containing the target_address in an
98 // instruction stream.
99 // The only architecture-independent user of this function is the serializer.
100 // The serializer uses it to find out how many raw bytes of instruction to
101 // output before the next target.
102 // For an instruction like LIS/ORI where the target bits are mixed into the
103 // instruction bits, the size of the target will be zero, indicating that the
104 // serializer should not step forward in memory after a target is resolved
106 return reinterpret_cast<Address>(pc_);
110 Address RelocInfo::constant_pool_entry_address() {
116 int RelocInfo::target_address_size() { return Assembler::kSpecialTargetSize; }
119 void RelocInfo::set_target_address(Address target,
120 WriteBarrierMode write_barrier_mode,
121 ICacheFlushMode icache_flush_mode) {
122 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
123 Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
124 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
125 IsCodeTarget(rmode_)) {
126 Object* target_code = Code::GetCodeFromTargetAddress(target);
127 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
128 host(), this, HeapObject::cast(target_code));
133 Address Assembler::break_address_from_return_address(Address pc) {
134 return target_address_from_return_address(pc);
138 Address Assembler::target_address_from_return_address(Address pc) {
139 // Returns the address of the call target from the return address that will
140 // be returned to after a call.
141 // Call sequence is :
142 // mov ip, @ call address
146 return pc - (kMovInstructions + 2) * kInstrSize;
150 Address Assembler::return_address_from_call_start(Address pc) {
151 return pc + (kMovInstructions + 2) * kInstrSize;
155 Object* RelocInfo::target_object() {
156 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
157 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
161 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
162 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
163 return Handle<Object>(
164 reinterpret_cast<Object**>(Assembler::target_address_at(pc_, host_)));
168 void RelocInfo::set_target_object(Object* target,
169 WriteBarrierMode write_barrier_mode,
170 ICacheFlushMode icache_flush_mode) {
171 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
172 Assembler::set_target_address_at(
173 pc_, host_, reinterpret_cast<Address>(target), icache_flush_mode);
174 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
175 target->IsHeapObject()) {
176 host()->GetHeap()->incremental_marking()->RecordWrite(
177 host(), &Memory::Object_at(pc_), HeapObject::cast(target));
182 Address RelocInfo::target_external_reference() {
183 DCHECK(rmode_ == EXTERNAL_REFERENCE);
184 return Assembler::target_address_at(pc_, host_);
188 Address RelocInfo::target_runtime_entry(Assembler* origin) {
189 DCHECK(IsRuntimeEntry(rmode_));
190 return target_address();
194 void RelocInfo::set_target_runtime_entry(Address target,
195 WriteBarrierMode write_barrier_mode,
196 ICacheFlushMode icache_flush_mode) {
197 DCHECK(IsRuntimeEntry(rmode_));
198 if (target_address() != target)
199 set_target_address(target, write_barrier_mode, icache_flush_mode);
203 Handle<Cell> RelocInfo::target_cell_handle() {
204 DCHECK(rmode_ == RelocInfo::CELL);
205 Address address = Memory::Address_at(pc_);
206 return Handle<Cell>(reinterpret_cast<Cell**>(address));
210 Cell* RelocInfo::target_cell() {
211 DCHECK(rmode_ == RelocInfo::CELL);
212 return Cell::FromValueAddress(Memory::Address_at(pc_));
216 void RelocInfo::set_target_cell(Cell* cell, WriteBarrierMode write_barrier_mode,
217 ICacheFlushMode icache_flush_mode) {
218 DCHECK(rmode_ == RelocInfo::CELL);
219 Address address = cell->address() + Cell::kValueOffset;
220 Memory::Address_at(pc_) = address;
221 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
222 // TODO(1550) We are passing NULL as a slot because cell can never be on
223 // evacuation candidate.
224 host()->GetHeap()->incremental_marking()->RecordWrite(host(), NULL, cell);
229 static const int kNoCodeAgeInstructions = 6;
230 static const int kCodeAgingInstructions = Assembler::kMovInstructions + 3;
231 static const int kNoCodeAgeSequenceInstructions =
232 ((kNoCodeAgeInstructions >= kCodeAgingInstructions)
233 ? kNoCodeAgeInstructions
234 : kCodeAgingInstructions);
235 static const int kNoCodeAgeSequenceNops =
236 (kNoCodeAgeSequenceInstructions - kNoCodeAgeInstructions);
237 static const int kCodeAgingSequenceNops =
238 (kNoCodeAgeSequenceInstructions - kCodeAgingInstructions);
239 static const int kCodeAgingTargetDelta = 1 * Assembler::kInstrSize;
240 static const int kNoCodeAgeSequenceLength =
241 (kNoCodeAgeSequenceInstructions * Assembler::kInstrSize);
244 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
245 UNREACHABLE(); // This should never be reached on PPC.
246 return Handle<Object>();
250 Code* RelocInfo::code_age_stub() {
251 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
252 return Code::GetCodeFromTargetAddress(
253 Assembler::target_address_at(pc_ + kCodeAgingTargetDelta, host_));
257 void RelocInfo::set_code_age_stub(Code* stub,
258 ICacheFlushMode icache_flush_mode) {
259 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
260 Assembler::set_target_address_at(pc_ + kCodeAgingTargetDelta, host_,
261 stub->instruction_start(),
266 Address RelocInfo::call_address() {
267 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
268 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
269 // The pc_ offset of 0 assumes patched return sequence per
270 // BreakLocation::SetDebugBreakAtReturn(), or debug break
271 // slot per BreakLocation::SetDebugBreakAtSlot().
272 return Assembler::target_address_at(pc_, host_);
276 void RelocInfo::set_call_address(Address target) {
277 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
278 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
279 Assembler::set_target_address_at(pc_, host_, target);
280 if (host() != NULL) {
281 Object* target_code = Code::GetCodeFromTargetAddress(target);
282 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
283 host(), this, HeapObject::cast(target_code));
288 Object* RelocInfo::call_object() { return *call_object_address(); }
291 void RelocInfo::set_call_object(Object* target) {
292 *call_object_address() = target;
296 Object** RelocInfo::call_object_address() {
297 DCHECK((IsJSReturn(rmode()) && IsPatchedReturnSequence()) ||
298 (IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence()));
299 return reinterpret_cast<Object**>(pc_ + 2 * Assembler::kInstrSize);
303 void RelocInfo::WipeOut() {
304 DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
305 IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
306 IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
307 if (IsInternalReference(rmode_)) {
309 Memory::Address_at(pc_) = NULL;
310 } else if (IsInternalReferenceEncoded(rmode_)) {
312 // Currently used only by deserializer, no need to flush.
313 Assembler::set_target_address_at(pc_, host_, NULL, SKIP_ICACHE_FLUSH);
315 Assembler::set_target_address_at(pc_, host_, NULL);
320 bool RelocInfo::IsPatchedReturnSequence() {
322 // The patched return sequence is defined by
323 // BreakLocation::SetDebugBreakAtReturn()
326 Instr instr0 = Assembler::instr_at(pc_);
327 Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
328 #if V8_TARGET_ARCH_PPC64
329 Instr instr3 = Assembler::instr_at(pc_ + (3 * Assembler::kInstrSize));
330 Instr instr4 = Assembler::instr_at(pc_ + (4 * Assembler::kInstrSize));
331 Instr binstr = Assembler::instr_at(pc_ + (7 * Assembler::kInstrSize));
333 Instr binstr = Assembler::instr_at(pc_ + 4 * Assembler::kInstrSize);
335 bool patched_return =
336 ((instr0 & kOpcodeMask) == ADDIS && (instr1 & kOpcodeMask) == ORI &&
337 #if V8_TARGET_ARCH_PPC64
338 (instr3 & kOpcodeMask) == ORIS && (instr4 & kOpcodeMask) == ORI &&
340 (binstr == 0x7d821008)); // twge r2, r2
342 // printf("IsPatchedReturnSequence: %d\n", patched_return);
343 return patched_return;
347 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
348 Instr current_instr = Assembler::instr_at(pc_);
349 return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
353 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
354 RelocInfo::Mode mode = rmode();
355 if (mode == RelocInfo::EMBEDDED_OBJECT) {
356 visitor->VisitEmbeddedPointer(this);
357 } else if (RelocInfo::IsCodeTarget(mode)) {
358 visitor->VisitCodeTarget(this);
359 } else if (mode == RelocInfo::CELL) {
360 visitor->VisitCell(this);
361 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
362 visitor->VisitExternalReference(this);
363 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
364 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
365 visitor->VisitInternalReference(this);
366 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
367 visitor->VisitCodeAgeSequence(this);
368 } else if (((RelocInfo::IsJSReturn(mode) && IsPatchedReturnSequence()) ||
369 (RelocInfo::IsDebugBreakSlot(mode) &&
370 IsPatchedDebugBreakSlotSequence())) &&
371 isolate->debug()->has_break_points()) {
372 visitor->VisitDebugTarget(this);
373 } else if (IsRuntimeEntry(mode)) {
374 visitor->VisitRuntimeEntry(this);
379 template <typename StaticVisitor>
380 void RelocInfo::Visit(Heap* heap) {
381 RelocInfo::Mode mode = rmode();
382 if (mode == RelocInfo::EMBEDDED_OBJECT) {
383 StaticVisitor::VisitEmbeddedPointer(heap, this);
384 } else if (RelocInfo::IsCodeTarget(mode)) {
385 StaticVisitor::VisitCodeTarget(heap, this);
386 } else if (mode == RelocInfo::CELL) {
387 StaticVisitor::VisitCell(heap, this);
388 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
389 StaticVisitor::VisitExternalReference(this);
390 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
391 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
392 StaticVisitor::VisitInternalReference(this);
393 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
394 StaticVisitor::VisitCodeAgeSequence(heap, this);
395 } else if (heap->isolate()->debug()->has_break_points() &&
396 ((RelocInfo::IsJSReturn(mode) && IsPatchedReturnSequence()) ||
397 (RelocInfo::IsDebugBreakSlot(mode) &&
398 IsPatchedDebugBreakSlotSequence()))) {
399 StaticVisitor::VisitDebugTarget(heap, this);
400 } else if (IsRuntimeEntry(mode)) {
401 StaticVisitor::VisitRuntimeEntry(this);
405 Operand::Operand(intptr_t immediate, RelocInfo::Mode rmode) {
411 Operand::Operand(const ExternalReference& f) {
413 imm_ = reinterpret_cast<intptr_t>(f.address());
414 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
417 Operand::Operand(Smi* value) {
419 imm_ = reinterpret_cast<intptr_t>(value);
420 rmode_ = kRelocInfo_NONEPTR;
423 Operand::Operand(Register rm) {
425 rmode_ = kRelocInfo_NONEPTR; // PPC -why doesn't ARM do this?
428 void Assembler::CheckBuffer() {
429 if (buffer_space() <= kGap) {
434 void Assembler::CheckTrampolinePoolQuick() {
435 if (pc_offset() >= next_buffer_check_) {
436 CheckTrampolinePool();
440 void Assembler::emit(Instr x) {
442 *reinterpret_cast<Instr*>(pc_) = x;
444 CheckTrampolinePoolQuick();
447 bool Operand::is_reg() const { return rm_.is_valid(); }
450 // Fetch the 32bit value from the FIXED_SEQUENCE lis/ori
451 Address Assembler::target_address_at(Address pc,
452 ConstantPoolArray* constant_pool) {
453 Instr instr1 = instr_at(pc);
454 Instr instr2 = instr_at(pc + kInstrSize);
455 // Interpret 2 instructions generated by lis/ori
456 if (IsLis(instr1) && IsOri(instr2)) {
457 #if V8_TARGET_ARCH_PPC64
458 Instr instr4 = instr_at(pc + (3 * kInstrSize));
459 Instr instr5 = instr_at(pc + (4 * kInstrSize));
460 // Assemble the 64 bit value.
461 uint64_t hi = (static_cast<uint32_t>((instr1 & kImm16Mask) << 16) |
462 static_cast<uint32_t>(instr2 & kImm16Mask));
463 uint64_t lo = (static_cast<uint32_t>((instr4 & kImm16Mask) << 16) |
464 static_cast<uint32_t>(instr5 & kImm16Mask));
465 return reinterpret_cast<Address>((hi << 32) | lo);
467 // Assemble the 32 bit value.
468 return reinterpret_cast<Address>(((instr1 & kImm16Mask) << 16) |
469 (instr2 & kImm16Mask));
478 // This sets the branch destination (which gets loaded at the call address).
479 // This is for calls and branches within generated code. The serializer
480 // has already deserialized the mov instructions etc.
481 // There is a FIXED_SEQUENCE assumption here
482 void Assembler::deserialization_set_special_target_at(
483 Address instruction_payload, Code* code, Address target) {
484 set_target_address_at(instruction_payload, code, target);
488 void Assembler::deserialization_set_target_internal_reference_at(
489 Address pc, Address target, RelocInfo::Mode mode) {
490 if (RelocInfo::IsInternalReferenceEncoded(mode)) {
492 set_target_address_at(pc, code, target, SKIP_ICACHE_FLUSH);
494 Memory::Address_at(pc) = target;
499 // This code assumes the FIXED_SEQUENCE of lis/ori
500 void Assembler::set_target_address_at(Address pc,
501 ConstantPoolArray* constant_pool,
503 ICacheFlushMode icache_flush_mode) {
504 Instr instr1 = instr_at(pc);
505 Instr instr2 = instr_at(pc + kInstrSize);
506 // Interpret 2 instructions generated by lis/ori
507 if (IsLis(instr1) && IsOri(instr2)) {
508 #if V8_TARGET_ARCH_PPC64
509 Instr instr4 = instr_at(pc + (3 * kInstrSize));
510 Instr instr5 = instr_at(pc + (4 * kInstrSize));
511 // Needs to be fixed up when mov changes to handle 64-bit values.
512 uint32_t* p = reinterpret_cast<uint32_t*>(pc);
513 uintptr_t itarget = reinterpret_cast<uintptr_t>(target);
515 instr5 &= ~kImm16Mask;
516 instr5 |= itarget & kImm16Mask;
517 itarget = itarget >> 16;
519 instr4 &= ~kImm16Mask;
520 instr4 |= itarget & kImm16Mask;
521 itarget = itarget >> 16;
523 instr2 &= ~kImm16Mask;
524 instr2 |= itarget & kImm16Mask;
525 itarget = itarget >> 16;
527 instr1 &= ~kImm16Mask;
528 instr1 |= itarget & kImm16Mask;
529 itarget = itarget >> 16;
535 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
536 CpuFeatures::FlushICache(p, 5 * kInstrSize);
539 uint32_t* p = reinterpret_cast<uint32_t*>(pc);
540 uint32_t itarget = reinterpret_cast<uint32_t>(target);
541 int lo_word = itarget & kImm16Mask;
542 int hi_word = itarget >> 16;
543 instr1 &= ~kImm16Mask;
545 instr2 &= ~kImm16Mask;
550 if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
551 CpuFeatures::FlushICache(p, 2 * kInstrSize);
559 } // namespace v8::internal
561 #endif // V8_PPC_ASSEMBLER_PPC_INL_H_