[presubmit] Enable readability/namespace linter checking.
[platform/upstream/v8.git] / src / ppc / assembler-ppc-inl.h
1 // Copyright (c) 1994-2006 Sun Microsystems Inc.
2 // All Rights Reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions
6 // are met:
7 //
8 // - Redistributions of source code must retain the above copyright notice,
9 // this list of conditions and the following disclaimer.
10 //
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
14 // distribution.
15 //
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.
19 //
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.
32
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.
36
37 #ifndef V8_PPC_ASSEMBLER_PPC_INL_H_
38 #define V8_PPC_ASSEMBLER_PPC_INL_H_
39
40 #include "src/ppc/assembler-ppc.h"
41
42 #include "src/assembler.h"
43 #include "src/debug/debug.h"
44
45
46 namespace v8 {
47 namespace internal {
48
49
50 bool CpuFeatures::SupportsCrankshaft() { return true; }
51
52
53 void RelocInfo::apply(intptr_t delta) {
54   // absolute code pointer inside code object moves with the code object.
55   if (IsInternalReference(rmode_)) {
56     // Jump table entry
57     Address target = Memory::Address_at(pc_);
58     Memory::Address_at(pc_) = target + delta;
59   } else {
60     // mov sequence
61     DCHECK(IsInternalReferenceEncoded(rmode_));
62     Address target = Assembler::target_address_at(pc_, host_);
63     Assembler::set_target_address_at(pc_, host_, target + delta,
64                                      SKIP_ICACHE_FLUSH);
65   }
66 }
67
68
69 Address RelocInfo::target_internal_reference() {
70   if (IsInternalReference(rmode_)) {
71     // Jump table entry
72     return Memory::Address_at(pc_);
73   } else {
74     // mov sequence
75     DCHECK(IsInternalReferenceEncoded(rmode_));
76     return Assembler::target_address_at(pc_, host_);
77   }
78 }
79
80
81 Address RelocInfo::target_internal_reference_address() {
82   DCHECK(IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
83   return reinterpret_cast<Address>(pc_);
84 }
85
86
87 Address RelocInfo::target_address() {
88   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
89   return Assembler::target_address_at(pc_, host_);
90 }
91
92
93 Address RelocInfo::target_address_address() {
94   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) ||
95          rmode_ == EMBEDDED_OBJECT || rmode_ == EXTERNAL_REFERENCE);
96
97   if (FLAG_enable_embedded_constant_pool &&
98       Assembler::IsConstantPoolLoadStart(pc_)) {
99     // We return the PC for embedded constant pool since this function is used
100     // by the serializer and expects the address to reside within the code
101     // object.
102     return reinterpret_cast<Address>(pc_);
103   }
104
105   // Read the address of the word containing the target_address in an
106   // instruction stream.
107   // The only architecture-independent user of this function is the serializer.
108   // The serializer uses it to find out how many raw bytes of instruction to
109   // output before the next target.
110   // For an instruction like LIS/ORI where the target bits are mixed into the
111   // instruction bits, the size of the target will be zero, indicating that the
112   // serializer should not step forward in memory after a target is resolved
113   // and written.
114   return reinterpret_cast<Address>(pc_);
115 }
116
117
118 Address RelocInfo::constant_pool_entry_address() {
119   if (FLAG_enable_embedded_constant_pool) {
120     Address constant_pool = host_->constant_pool();
121     DCHECK(constant_pool);
122     ConstantPoolEntry::Access access;
123     if (Assembler::IsConstantPoolLoadStart(pc_, &access))
124       return Assembler::target_constant_pool_address_at(
125           pc_, constant_pool, access, ConstantPoolEntry::INTPTR);
126   }
127   UNREACHABLE();
128   return NULL;
129 }
130
131
132 int RelocInfo::target_address_size() { return Assembler::kSpecialTargetSize; }
133
134
135 void RelocInfo::set_target_address(Address target,
136                                    WriteBarrierMode write_barrier_mode,
137                                    ICacheFlushMode icache_flush_mode) {
138   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
139   Assembler::set_target_address_at(pc_, host_, target, icache_flush_mode);
140   if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
141       IsCodeTarget(rmode_)) {
142     Object* target_code = Code::GetCodeFromTargetAddress(target);
143     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
144         host(), this, HeapObject::cast(target_code));
145   }
146 }
147
148
149 Address Assembler::target_address_from_return_address(Address pc) {
150 // Returns the address of the call target from the return address that will
151 // be returned to after a call.
152 // Call sequence is :
153 //  mov   ip, @ call address
154 //  mtlr  ip
155 //  blrl
156 //                      @ return address
157   int len;
158   ConstantPoolEntry::Access access;
159   if (FLAG_enable_embedded_constant_pool &&
160       IsConstantPoolLoadEnd(pc - 3 * kInstrSize, &access)) {
161     len = (access == ConstantPoolEntry::OVERFLOWED) ? 2 : 1;
162   } else {
163     len = kMovInstructionsNoConstantPool;
164   }
165   return pc - (len + 2) * kInstrSize;
166 }
167
168
169 Address Assembler::return_address_from_call_start(Address pc) {
170   int len;
171   ConstantPoolEntry::Access access;
172   if (FLAG_enable_embedded_constant_pool &&
173       IsConstantPoolLoadStart(pc, &access)) {
174     len = (access == ConstantPoolEntry::OVERFLOWED) ? 2 : 1;
175   } else {
176     len = kMovInstructionsNoConstantPool;
177   }
178   return pc + (len + 2) * kInstrSize;
179 }
180
181
182 Object* RelocInfo::target_object() {
183   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
184   return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
185 }
186
187
188 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
189   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
190   return Handle<Object>(
191       reinterpret_cast<Object**>(Assembler::target_address_at(pc_, host_)));
192 }
193
194
195 void RelocInfo::set_target_object(Object* target,
196                                   WriteBarrierMode write_barrier_mode,
197                                   ICacheFlushMode icache_flush_mode) {
198   DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
199   Assembler::set_target_address_at(
200       pc_, host_, reinterpret_cast<Address>(target), icache_flush_mode);
201   if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL &&
202       target->IsHeapObject()) {
203     host()->GetHeap()->incremental_marking()->RecordWrite(
204         host(), &Memory::Object_at(pc_), HeapObject::cast(target));
205   }
206 }
207
208
209 Address RelocInfo::target_external_reference() {
210   DCHECK(rmode_ == EXTERNAL_REFERENCE);
211   return Assembler::target_address_at(pc_, host_);
212 }
213
214
215 Address RelocInfo::target_runtime_entry(Assembler* origin) {
216   DCHECK(IsRuntimeEntry(rmode_));
217   return target_address();
218 }
219
220
221 void RelocInfo::set_target_runtime_entry(Address target,
222                                          WriteBarrierMode write_barrier_mode,
223                                          ICacheFlushMode icache_flush_mode) {
224   DCHECK(IsRuntimeEntry(rmode_));
225   if (target_address() != target)
226     set_target_address(target, write_barrier_mode, icache_flush_mode);
227 }
228
229
230 Handle<Cell> RelocInfo::target_cell_handle() {
231   DCHECK(rmode_ == RelocInfo::CELL);
232   Address address = Memory::Address_at(pc_);
233   return Handle<Cell>(reinterpret_cast<Cell**>(address));
234 }
235
236
237 Cell* RelocInfo::target_cell() {
238   DCHECK(rmode_ == RelocInfo::CELL);
239   return Cell::FromValueAddress(Memory::Address_at(pc_));
240 }
241
242
243 void RelocInfo::set_target_cell(Cell* cell, WriteBarrierMode write_barrier_mode,
244                                 ICacheFlushMode icache_flush_mode) {
245   DCHECK(rmode_ == RelocInfo::CELL);
246   Address address = cell->address() + Cell::kValueOffset;
247   Memory::Address_at(pc_) = address;
248   if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
249     // TODO(1550) We are passing NULL as a slot because cell can never be on
250     // evacuation candidate.
251     host()->GetHeap()->incremental_marking()->RecordWrite(host(), NULL, cell);
252   }
253 }
254
255
256 static const int kNoCodeAgeInstructions =
257     FLAG_enable_embedded_constant_pool ? 7 : 6;
258 static const int kCodeAgingInstructions =
259     Assembler::kMovInstructionsNoConstantPool + 3;
260 static const int kNoCodeAgeSequenceInstructions =
261     ((kNoCodeAgeInstructions >= kCodeAgingInstructions)
262          ? kNoCodeAgeInstructions
263          : kCodeAgingInstructions);
264 static const int kNoCodeAgeSequenceNops =
265     (kNoCodeAgeSequenceInstructions - kNoCodeAgeInstructions);
266 static const int kCodeAgingSequenceNops =
267     (kNoCodeAgeSequenceInstructions - kCodeAgingInstructions);
268 static const int kCodeAgingTargetDelta = 1 * Assembler::kInstrSize;
269 static const int kNoCodeAgeSequenceLength =
270     (kNoCodeAgeSequenceInstructions * Assembler::kInstrSize);
271
272
273 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
274   UNREACHABLE();  // This should never be reached on PPC.
275   return Handle<Object>();
276 }
277
278
279 Code* RelocInfo::code_age_stub() {
280   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
281   return Code::GetCodeFromTargetAddress(
282       Assembler::target_address_at(pc_ + kCodeAgingTargetDelta, host_));
283 }
284
285
286 void RelocInfo::set_code_age_stub(Code* stub,
287                                   ICacheFlushMode icache_flush_mode) {
288   DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
289   Assembler::set_target_address_at(pc_ + kCodeAgingTargetDelta, host_,
290                                    stub->instruction_start(),
291                                    icache_flush_mode);
292 }
293
294
295 Address RelocInfo::debug_call_address() {
296   DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
297   return Assembler::target_address_at(pc_, host_);
298 }
299
300
301 void RelocInfo::set_debug_call_address(Address target) {
302   DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
303   Assembler::set_target_address_at(pc_, host_, target);
304   if (host() != NULL) {
305     Object* target_code = Code::GetCodeFromTargetAddress(target);
306     host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
307         host(), this, HeapObject::cast(target_code));
308   }
309 }
310
311
312 void RelocInfo::WipeOut() {
313   DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
314          IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
315          IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
316   if (IsInternalReference(rmode_)) {
317     // Jump table entry
318     Memory::Address_at(pc_) = NULL;
319   } else if (IsInternalReferenceEncoded(rmode_)) {
320     // mov sequence
321     // Currently used only by deserializer, no need to flush.
322     Assembler::set_target_address_at(pc_, host_, NULL, SKIP_ICACHE_FLUSH);
323   } else {
324     Assembler::set_target_address_at(pc_, host_, NULL);
325   }
326 }
327
328
329 bool RelocInfo::IsPatchedReturnSequence() {
330   //
331   // The patched return sequence is defined by
332   // BreakLocation::SetDebugBreakAtReturn()
333   // FIXED_SEQUENCE
334
335   Instr instr0 = Assembler::instr_at(pc_);
336   Instr instr1 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
337 #if V8_TARGET_ARCH_PPC64
338   Instr instr3 = Assembler::instr_at(pc_ + (3 * Assembler::kInstrSize));
339   Instr instr4 = Assembler::instr_at(pc_ + (4 * Assembler::kInstrSize));
340   Instr binstr = Assembler::instr_at(pc_ + (7 * Assembler::kInstrSize));
341 #else
342   Instr binstr = Assembler::instr_at(pc_ + 4 * Assembler::kInstrSize);
343 #endif
344   bool patched_return =
345       ((instr0 & kOpcodeMask) == ADDIS && (instr1 & kOpcodeMask) == ORI &&
346 #if V8_TARGET_ARCH_PPC64
347        (instr3 & kOpcodeMask) == ORIS && (instr4 & kOpcodeMask) == ORI &&
348 #endif
349        (binstr == 0x7d821008));  // twge r2, r2
350
351   // printf("IsPatchedReturnSequence: %d\n", patched_return);
352   return patched_return;
353 }
354
355
356 bool RelocInfo::IsPatchedDebugBreakSlotSequence() {
357   Instr current_instr = Assembler::instr_at(pc_);
358   return !Assembler::IsNop(current_instr, Assembler::DEBUG_BREAK_NOP);
359 }
360
361
362 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
363   RelocInfo::Mode mode = rmode();
364   if (mode == RelocInfo::EMBEDDED_OBJECT) {
365     visitor->VisitEmbeddedPointer(this);
366   } else if (RelocInfo::IsCodeTarget(mode)) {
367     visitor->VisitCodeTarget(this);
368   } else if (mode == RelocInfo::CELL) {
369     visitor->VisitCell(this);
370   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
371     visitor->VisitExternalReference(this);
372   } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
373              mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
374     visitor->VisitInternalReference(this);
375   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
376     visitor->VisitCodeAgeSequence(this);
377   } else if (RelocInfo::IsDebugBreakSlot(mode) &&
378              IsPatchedDebugBreakSlotSequence()) {
379     visitor->VisitDebugTarget(this);
380   } else if (IsRuntimeEntry(mode)) {
381     visitor->VisitRuntimeEntry(this);
382   }
383 }
384
385
386 template <typename StaticVisitor>
387 void RelocInfo::Visit(Heap* heap) {
388   RelocInfo::Mode mode = rmode();
389   if (mode == RelocInfo::EMBEDDED_OBJECT) {
390     StaticVisitor::VisitEmbeddedPointer(heap, this);
391   } else if (RelocInfo::IsCodeTarget(mode)) {
392     StaticVisitor::VisitCodeTarget(heap, this);
393   } else if (mode == RelocInfo::CELL) {
394     StaticVisitor::VisitCell(heap, this);
395   } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
396     StaticVisitor::VisitExternalReference(this);
397   } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
398              mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
399     StaticVisitor::VisitInternalReference(this);
400   } else if (RelocInfo::IsCodeAgeSequence(mode)) {
401     StaticVisitor::VisitCodeAgeSequence(heap, this);
402   } else if (RelocInfo::IsDebugBreakSlot(mode) &&
403              IsPatchedDebugBreakSlotSequence()) {
404     StaticVisitor::VisitDebugTarget(heap, this);
405   } else if (IsRuntimeEntry(mode)) {
406     StaticVisitor::VisitRuntimeEntry(this);
407   }
408 }
409
410 Operand::Operand(intptr_t immediate, RelocInfo::Mode rmode) {
411   rm_ = no_reg;
412   imm_ = immediate;
413   rmode_ = rmode;
414 }
415
416 Operand::Operand(const ExternalReference& f) {
417   rm_ = no_reg;
418   imm_ = reinterpret_cast<intptr_t>(f.address());
419   rmode_ = RelocInfo::EXTERNAL_REFERENCE;
420 }
421
422 Operand::Operand(Smi* value) {
423   rm_ = no_reg;
424   imm_ = reinterpret_cast<intptr_t>(value);
425   rmode_ = kRelocInfo_NONEPTR;
426 }
427
428 Operand::Operand(Register rm) {
429   rm_ = rm;
430   rmode_ = kRelocInfo_NONEPTR;  // PPC -why doesn't ARM do this?
431 }
432
433 void Assembler::CheckBuffer() {
434   if (buffer_space() <= kGap) {
435     GrowBuffer();
436   }
437 }
438
439 void Assembler::TrackBranch() {
440   DCHECK(!trampoline_emitted_);
441   int count = tracked_branch_count_++;
442   if (count == 0) {
443     // We leave space (kMaxBlockTrampolineSectionSize)
444     // for BlockTrampolinePoolScope buffer.
445     next_trampoline_check_ =
446         pc_offset() + kMaxCondBranchReach - kMaxBlockTrampolineSectionSize;
447   } else {
448     next_trampoline_check_ -= kTrampolineSlotsSize;
449   }
450 }
451
452 void Assembler::UntrackBranch() {
453   DCHECK(!trampoline_emitted_);
454   DCHECK(tracked_branch_count_ > 0);
455   int count = --tracked_branch_count_;
456   if (count == 0) {
457     // Reset
458     next_trampoline_check_ = kMaxInt;
459   } else {
460     next_trampoline_check_ += kTrampolineSlotsSize;
461   }
462 }
463
464 void Assembler::CheckTrampolinePoolQuick() {
465   if (pc_offset() >= next_trampoline_check_) {
466     CheckTrampolinePool();
467   }
468 }
469
470 void Assembler::emit(Instr x) {
471   CheckBuffer();
472   *reinterpret_cast<Instr*>(pc_) = x;
473   pc_ += kInstrSize;
474   CheckTrampolinePoolQuick();
475 }
476
477 bool Operand::is_reg() const { return rm_.is_valid(); }
478
479
480 // Fetch the 32bit value from the FIXED_SEQUENCE lis/ori
481 Address Assembler::target_address_at(Address pc, Address constant_pool) {
482   if (FLAG_enable_embedded_constant_pool && constant_pool) {
483     ConstantPoolEntry::Access access;
484     if (IsConstantPoolLoadStart(pc, &access))
485       return Memory::Address_at(target_constant_pool_address_at(
486           pc, constant_pool, access, ConstantPoolEntry::INTPTR));
487   }
488
489   Instr instr1 = instr_at(pc);
490   Instr instr2 = instr_at(pc + kInstrSize);
491   // Interpret 2 instructions generated by lis/ori
492   if (IsLis(instr1) && IsOri(instr2)) {
493 #if V8_TARGET_ARCH_PPC64
494     Instr instr4 = instr_at(pc + (3 * kInstrSize));
495     Instr instr5 = instr_at(pc + (4 * kInstrSize));
496     // Assemble the 64 bit value.
497     uint64_t hi = (static_cast<uint32_t>((instr1 & kImm16Mask) << 16) |
498                    static_cast<uint32_t>(instr2 & kImm16Mask));
499     uint64_t lo = (static_cast<uint32_t>((instr4 & kImm16Mask) << 16) |
500                    static_cast<uint32_t>(instr5 & kImm16Mask));
501     return reinterpret_cast<Address>((hi << 32) | lo);
502 #else
503     // Assemble the 32 bit value.
504     return reinterpret_cast<Address>(((instr1 & kImm16Mask) << 16) |
505                                      (instr2 & kImm16Mask));
506 #endif
507   }
508
509   UNREACHABLE();
510   return NULL;
511 }
512
513
514 #if V8_TARGET_ARCH_PPC64
515 const int kLoadIntptrOpcode = LD;
516 #else
517 const int kLoadIntptrOpcode = LWZ;
518 #endif
519
520 // Constant pool load sequence detection:
521 // 1) REGULAR access:
522 //    load <dst>, kConstantPoolRegister + <offset>
523 //
524 // 2) OVERFLOWED access:
525 //    addis <scratch>, kConstantPoolRegister, <offset_high>
526 //    load <dst>, <scratch> + <offset_low>
527 bool Assembler::IsConstantPoolLoadStart(Address pc,
528                                         ConstantPoolEntry::Access* access) {
529   Instr instr = instr_at(pc);
530   int opcode = instr & kOpcodeMask;
531   if (!GetRA(instr).is(kConstantPoolRegister)) return false;
532   bool overflowed = (opcode == ADDIS);
533 #ifdef DEBUG
534   if (overflowed) {
535     opcode = instr_at(pc + kInstrSize) & kOpcodeMask;
536   }
537   DCHECK(opcode == kLoadIntptrOpcode || opcode == LFD);
538 #endif
539   if (access) {
540     *access = (overflowed ? ConstantPoolEntry::OVERFLOWED
541                           : ConstantPoolEntry::REGULAR);
542   }
543   return true;
544 }
545
546
547 bool Assembler::IsConstantPoolLoadEnd(Address pc,
548                                       ConstantPoolEntry::Access* access) {
549   Instr instr = instr_at(pc);
550   int opcode = instr & kOpcodeMask;
551   bool overflowed = false;
552   if (!(opcode == kLoadIntptrOpcode || opcode == LFD)) return false;
553   if (!GetRA(instr).is(kConstantPoolRegister)) {
554     instr = instr_at(pc - kInstrSize);
555     opcode = instr & kOpcodeMask;
556     if ((opcode != ADDIS) || !GetRA(instr).is(kConstantPoolRegister)) {
557       return false;
558     }
559     overflowed = true;
560   }
561   if (access) {
562     *access = (overflowed ? ConstantPoolEntry::OVERFLOWED
563                           : ConstantPoolEntry::REGULAR);
564   }
565   return true;
566 }
567
568
569 int Assembler::GetConstantPoolOffset(Address pc,
570                                      ConstantPoolEntry::Access access,
571                                      ConstantPoolEntry::Type type) {
572   bool overflowed = (access == ConstantPoolEntry::OVERFLOWED);
573 #ifdef DEBUG
574   ConstantPoolEntry::Access access_check =
575       static_cast<ConstantPoolEntry::Access>(-1);
576   DCHECK(IsConstantPoolLoadStart(pc, &access_check));
577   DCHECK(access_check == access);
578 #endif
579   int offset;
580   if (overflowed) {
581     offset = (instr_at(pc) & kImm16Mask) << 16;
582     offset += SIGN_EXT_IMM16(instr_at(pc + kInstrSize) & kImm16Mask);
583     DCHECK(!is_int16(offset));
584   } else {
585     offset = SIGN_EXT_IMM16((instr_at(pc) & kImm16Mask));
586   }
587   return offset;
588 }
589
590
591 void Assembler::PatchConstantPoolAccessInstruction(
592     int pc_offset, int offset, ConstantPoolEntry::Access access,
593     ConstantPoolEntry::Type type) {
594   Address pc = buffer_ + pc_offset;
595   bool overflowed = (access == ConstantPoolEntry::OVERFLOWED);
596   CHECK(overflowed != is_int16(offset));
597 #ifdef DEBUG
598   ConstantPoolEntry::Access access_check =
599       static_cast<ConstantPoolEntry::Access>(-1);
600   DCHECK(IsConstantPoolLoadStart(pc, &access_check));
601   DCHECK(access_check == access);
602 #endif
603   if (overflowed) {
604     int hi_word = static_cast<int>(offset >> 16);
605     int lo_word = static_cast<int>(offset & 0xffff);
606     if (lo_word & 0x8000) hi_word++;
607
608     Instr instr1 = instr_at(pc);
609     Instr instr2 = instr_at(pc + kInstrSize);
610     instr1 &= ~kImm16Mask;
611     instr1 |= (hi_word & kImm16Mask);
612     instr2 &= ~kImm16Mask;
613     instr2 |= (lo_word & kImm16Mask);
614     instr_at_put(pc, instr1);
615     instr_at_put(pc + kInstrSize, instr2);
616   } else {
617     Instr instr = instr_at(pc);
618     instr &= ~kImm16Mask;
619     instr |= (offset & kImm16Mask);
620     instr_at_put(pc, instr);
621   }
622 }
623
624
625 Address Assembler::target_constant_pool_address_at(
626     Address pc, Address constant_pool, ConstantPoolEntry::Access access,
627     ConstantPoolEntry::Type type) {
628   Address addr = constant_pool;
629   DCHECK(addr);
630   addr += GetConstantPoolOffset(pc, access, type);
631   return addr;
632 }
633
634
635 // This sets the branch destination (which gets loaded at the call address).
636 // This is for calls and branches within generated code.  The serializer
637 // has already deserialized the mov instructions etc.
638 // There is a FIXED_SEQUENCE assumption here
639 void Assembler::deserialization_set_special_target_at(
640     Address instruction_payload, Code* code, Address target) {
641   set_target_address_at(instruction_payload, code, target);
642 }
643
644
645 void Assembler::deserialization_set_target_internal_reference_at(
646     Address pc, Address target, RelocInfo::Mode mode) {
647   if (RelocInfo::IsInternalReferenceEncoded(mode)) {
648     Code* code = NULL;
649     set_target_address_at(pc, code, target, SKIP_ICACHE_FLUSH);
650   } else {
651     Memory::Address_at(pc) = target;
652   }
653 }
654
655
656 // This code assumes the FIXED_SEQUENCE of lis/ori
657 void Assembler::set_target_address_at(Address pc, Address constant_pool,
658                                       Address target,
659                                       ICacheFlushMode icache_flush_mode) {
660   if (FLAG_enable_embedded_constant_pool && constant_pool) {
661     ConstantPoolEntry::Access access;
662     if (IsConstantPoolLoadStart(pc, &access)) {
663       Memory::Address_at(target_constant_pool_address_at(
664           pc, constant_pool, access, ConstantPoolEntry::INTPTR)) = target;
665       return;
666     }
667   }
668
669   Instr instr1 = instr_at(pc);
670   Instr instr2 = instr_at(pc + kInstrSize);
671   // Interpret 2 instructions generated by lis/ori
672   if (IsLis(instr1) && IsOri(instr2)) {
673 #if V8_TARGET_ARCH_PPC64
674     Instr instr4 = instr_at(pc + (3 * kInstrSize));
675     Instr instr5 = instr_at(pc + (4 * kInstrSize));
676     // Needs to be fixed up when mov changes to handle 64-bit values.
677     uint32_t* p = reinterpret_cast<uint32_t*>(pc);
678     uintptr_t itarget = reinterpret_cast<uintptr_t>(target);
679
680     instr5 &= ~kImm16Mask;
681     instr5 |= itarget & kImm16Mask;
682     itarget = itarget >> 16;
683
684     instr4 &= ~kImm16Mask;
685     instr4 |= itarget & kImm16Mask;
686     itarget = itarget >> 16;
687
688     instr2 &= ~kImm16Mask;
689     instr2 |= itarget & kImm16Mask;
690     itarget = itarget >> 16;
691
692     instr1 &= ~kImm16Mask;
693     instr1 |= itarget & kImm16Mask;
694     itarget = itarget >> 16;
695
696     *p = instr1;
697     *(p + 1) = instr2;
698     *(p + 3) = instr4;
699     *(p + 4) = instr5;
700     if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
701       Assembler::FlushICacheWithoutIsolate(p, 5 * kInstrSize);
702     }
703 #else
704     uint32_t* p = reinterpret_cast<uint32_t*>(pc);
705     uint32_t itarget = reinterpret_cast<uint32_t>(target);
706     int lo_word = itarget & kImm16Mask;
707     int hi_word = itarget >> 16;
708     instr1 &= ~kImm16Mask;
709     instr1 |= hi_word;
710     instr2 &= ~kImm16Mask;
711     instr2 |= lo_word;
712
713     *p = instr1;
714     *(p + 1) = instr2;
715     if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
716       Assembler::FlushICacheWithoutIsolate(p, 2 * kInstrSize);
717     }
718 #endif
719     return;
720   }
721   UNREACHABLE();
722 }
723 }  // namespace internal
724 }  // namespace v8
725
726 #endif  // V8_PPC_ASSEMBLER_PPC_INL_H_