• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_CODEGEN_PPC_ASSEMBLER_PPC_INL_H_
38 #define V8_CODEGEN_PPC_ASSEMBLER_PPC_INL_H_
39 
40 #include "src/codegen/ppc/assembler-ppc.h"
41 
42 #include "src/codegen/assembler.h"
43 #include "src/debug/debug.h"
44 #include "src/objects/objects-inl.h"
45 
46 namespace v8 {
47 namespace internal {
48 
SupportsOptimizer()49 bool CpuFeatures::SupportsOptimizer() { return true; }
50 
apply(intptr_t delta)51 void RelocInfo::apply(intptr_t delta) {
52   // absolute code pointer inside code object moves with the code object.
53   if (IsInternalReference(rmode_)) {
54     // Jump table entry
55     Address target = Memory<Address>(pc_);
56     Memory<Address>(pc_) = target + delta;
57   } else {
58     // mov sequence
59     DCHECK(IsInternalReferenceEncoded(rmode_));
60     Address target = Assembler::target_address_at(pc_, constant_pool_);
61     Assembler::set_target_address_at(pc_, constant_pool_, target + delta,
62                                      SKIP_ICACHE_FLUSH);
63   }
64 }
65 
target_internal_reference()66 Address RelocInfo::target_internal_reference() {
67   if (IsInternalReference(rmode_)) {
68     // Jump table entry
69     return Memory<Address>(pc_);
70   } else {
71     // mov sequence
72     DCHECK(IsInternalReferenceEncoded(rmode_));
73     return Assembler::target_address_at(pc_, constant_pool_);
74   }
75 }
76 
target_internal_reference_address()77 Address RelocInfo::target_internal_reference_address() {
78   DCHECK(IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
79   return pc_;
80 }
81 
target_address()82 Address RelocInfo::target_address() {
83   DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) || IsWasmCall(rmode_));
84   return Assembler::target_address_at(pc_, constant_pool_);
85 }
86 
target_address_address()87 Address RelocInfo::target_address_address() {
88   DCHECK(HasTargetAddressAddress());
89 
90   if (FLAG_enable_embedded_constant_pool &&
91       Assembler::IsConstantPoolLoadStart(pc_)) {
92     // We return the PC for embedded constant pool since this function is used
93     // by the serializer and expects the address to reside within the code
94     // object.
95     return pc_;
96   }
97 
98   // Read the address of the word containing the target_address in an
99   // instruction stream.
100   // The only architecture-independent user of this function is the serializer.
101   // The serializer uses it to find out how many raw bytes of instruction to
102   // output before the next target.
103   // For an instruction like LIS/ORI where the target bits are mixed into the
104   // instruction bits, the size of the target will be zero, indicating that the
105   // serializer should not step forward in memory after a target is resolved
106   // and written.
107   return pc_;
108 }
109 
constant_pool_entry_address()110 Address RelocInfo::constant_pool_entry_address() {
111   if (FLAG_enable_embedded_constant_pool) {
112     DCHECK(constant_pool_);
113     ConstantPoolEntry::Access access;
114     if (Assembler::IsConstantPoolLoadStart(pc_, &access))
115       return Assembler::target_constant_pool_address_at(
116           pc_, constant_pool_, access, ConstantPoolEntry::INTPTR);
117   }
118   UNREACHABLE();
119 }
120 
set_target_compressed_address_at(Address pc,Address constant_pool,Tagged_t target,ICacheFlushMode icache_flush_mode)121 void Assembler::set_target_compressed_address_at(
122     Address pc, Address constant_pool, Tagged_t target,
123     ICacheFlushMode icache_flush_mode) {
124   Assembler::set_target_address_at(
125       pc, constant_pool, static_cast<Address>(target), icache_flush_mode);
126 }
127 
target_address_size()128 int RelocInfo::target_address_size() {
129   if (IsCodedSpecially()) {
130     return Assembler::kSpecialTargetSize;
131   } else {
132     return kSystemPointerSize;
133   }
134 }
135 
target_compressed_address_at(Address pc,Address constant_pool)136 Tagged_t Assembler::target_compressed_address_at(Address pc,
137                                                  Address constant_pool) {
138   return static_cast<Tagged_t>(target_address_at(pc, constant_pool));
139 }
140 
code_target_object_handle_at(Address pc,Address constant_pool)141 Handle<Object> Assembler::code_target_object_handle_at(Address pc,
142                                                        Address constant_pool) {
143   int index =
144       static_cast<int>(target_address_at(pc, constant_pool)) & 0xFFFFFFFF;
145   return GetCodeTarget(index);
146 }
147 
target_object(PtrComprCageBase cage_base)148 HeapObject RelocInfo::target_object(PtrComprCageBase cage_base) {
149   DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObjectMode(rmode_));
150   if (IsDataEmbeddedObject(rmode_)) {
151     return HeapObject::cast(Object(ReadUnalignedValue<Address>(pc_)));
152   } else if (IsCompressedEmbeddedObject(rmode_)) {
153     return HeapObject::cast(Object(DecompressTaggedAny(
154         cage_base,
155         Assembler::target_compressed_address_at(pc_, constant_pool_))));
156   } else {
157     return HeapObject::cast(
158         Object(Assembler::target_address_at(pc_, constant_pool_)));
159   }
160 }
161 
compressed_embedded_object_handle_at(Address pc,Address const_pool)162 Handle<HeapObject> Assembler::compressed_embedded_object_handle_at(
163     Address pc, Address const_pool) {
164   return GetEmbeddedObject(target_compressed_address_at(pc, const_pool));
165 }
166 
target_object_handle(Assembler * origin)167 Handle<HeapObject> RelocInfo::target_object_handle(Assembler* origin) {
168   DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObjectMode(rmode_));
169   if (IsDataEmbeddedObject(rmode_)) {
170     return Handle<HeapObject>::cast(ReadUnalignedValue<Handle<Object>>(pc_));
171   } else if (IsCodeTarget(rmode_)) {
172     return Handle<HeapObject>::cast(
173         origin->code_target_object_handle_at(pc_, constant_pool_));
174   } else {
175     if (IsCompressedEmbeddedObject(rmode_)) {
176       return origin->compressed_embedded_object_handle_at(pc_, constant_pool_);
177     }
178     return Handle<HeapObject>(reinterpret_cast<Address*>(
179         Assembler::target_address_at(pc_, constant_pool_)));
180   }
181 }
182 
set_target_object(Heap * heap,HeapObject target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)183 void RelocInfo::set_target_object(Heap* heap, HeapObject target,
184                                   WriteBarrierMode write_barrier_mode,
185                                   ICacheFlushMode icache_flush_mode) {
186   DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObjectMode(rmode_));
187   if (IsDataEmbeddedObject(rmode_)) {
188     WriteUnalignedValue(pc_, target.ptr());
189     // No need to flush icache since no instructions were changed.
190   } else if (IsCompressedEmbeddedObject(rmode_)) {
191     Assembler::set_target_compressed_address_at(
192         pc_, constant_pool_, CompressTagged(target.ptr()), icache_flush_mode);
193   } else {
194     DCHECK(IsFullEmbeddedObject(rmode_));
195     Assembler::set_target_address_at(pc_, constant_pool_, target.ptr(),
196                                      icache_flush_mode);
197   }
198   if (write_barrier_mode == UPDATE_WRITE_BARRIER && !host().is_null() &&
199       !FLAG_disable_write_barriers) {
200     WriteBarrierForCode(host(), this, target);
201   }
202 }
203 
target_external_reference()204 Address RelocInfo::target_external_reference() {
205   DCHECK(rmode_ == EXTERNAL_REFERENCE);
206   return Assembler::target_address_at(pc_, constant_pool_);
207 }
208 
set_target_external_reference(Address target,ICacheFlushMode icache_flush_mode)209 void RelocInfo::set_target_external_reference(
210     Address target, ICacheFlushMode icache_flush_mode) {
211   DCHECK(rmode_ == RelocInfo::EXTERNAL_REFERENCE);
212   Assembler::set_target_address_at(pc_, constant_pool_, target,
213                                    icache_flush_mode);
214 }
215 
target_runtime_entry(Assembler * origin)216 Address RelocInfo::target_runtime_entry(Assembler* origin) {
217   DCHECK(IsRuntimeEntry(rmode_));
218   return target_address();
219 }
220 
set_target_runtime_entry(Address target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)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 
target_off_heap_target()229 Address RelocInfo::target_off_heap_target() {
230   DCHECK(IsOffHeapTarget(rmode_));
231   return Assembler::target_address_at(pc_, constant_pool_);
232 }
233 
WipeOut()234 void RelocInfo::WipeOut() {
235   DCHECK(IsEmbeddedObjectMode(rmode_) || IsCodeTarget(rmode_) ||
236          IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
237          IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_) ||
238          IsOffHeapTarget(rmode_));
239   if (IsInternalReference(rmode_)) {
240     // Jump table entry
241     Memory<Address>(pc_) = kNullAddress;
242   } else if (IsCompressedEmbeddedObject(rmode_)) {
243     Assembler::set_target_compressed_address_at(pc_, constant_pool_,
244                                                 kNullAddress);
245   } else if (IsInternalReferenceEncoded(rmode_) || IsOffHeapTarget(rmode_)) {
246     // mov sequence
247     // Currently used only by deserializer, no need to flush.
248     Assembler::set_target_address_at(pc_, constant_pool_, kNullAddress,
249                                      SKIP_ICACHE_FLUSH);
250   } else {
251     Assembler::set_target_address_at(pc_, constant_pool_, kNullAddress);
252   }
253 }
254 
Operand(Register rm)255 Operand::Operand(Register rm) : rm_(rm), rmode_(RelocInfo::NO_INFO) {}
256 
UntrackBranch()257 void Assembler::UntrackBranch() {
258   DCHECK(!trampoline_emitted_);
259   DCHECK_GT(tracked_branch_count_, 0);
260   int count = --tracked_branch_count_;
261   if (count == 0) {
262     // Reset
263     next_trampoline_check_ = kMaxInt;
264   } else {
265     next_trampoline_check_ += kTrampolineSlotsSize;
266   }
267 }
268 
269 // Fetch the 32bit value from the FIXED_SEQUENCE lis/ori
target_address_at(Address pc,Address constant_pool)270 Address Assembler::target_address_at(Address pc, Address constant_pool) {
271   if (FLAG_enable_embedded_constant_pool && constant_pool) {
272     ConstantPoolEntry::Access access;
273     if (IsConstantPoolLoadStart(pc, &access))
274       return Memory<Address>(target_constant_pool_address_at(
275           pc, constant_pool, access, ConstantPoolEntry::INTPTR));
276   }
277 
278   Instr instr1 = instr_at(pc);
279   Instr instr2 = instr_at(pc + kInstrSize);
280   // Interpret 2 instructions generated by lis/ori
281   if (IsLis(instr1) && IsOri(instr2)) {
282 #if V8_TARGET_ARCH_PPC64
283     Instr instr4 = instr_at(pc + (3 * kInstrSize));
284     Instr instr5 = instr_at(pc + (4 * kInstrSize));
285     // Assemble the 64 bit value.
286     uint64_t hi = (static_cast<uint32_t>((instr1 & kImm16Mask) << 16) |
287                    static_cast<uint32_t>(instr2 & kImm16Mask));
288     uint64_t lo = (static_cast<uint32_t>((instr4 & kImm16Mask) << 16) |
289                    static_cast<uint32_t>(instr5 & kImm16Mask));
290     return static_cast<Address>((hi << 32) | lo);
291 #else
292     // Assemble the 32 bit value.
293     return static_cast<Address>(((instr1 & kImm16Mask) << 16) |
294                                 (instr2 & kImm16Mask));
295 #endif
296   }
297 
298   UNREACHABLE();
299 }
300 
301 #if V8_TARGET_ARCH_PPC64
302 const uint32_t kLoadIntptrOpcode = LD;
303 #else
304 const uint32_t kLoadIntptrOpcode = LWZ;
305 #endif
306 
307 // Constant pool load sequence detection:
308 // 1) REGULAR access:
309 //    load <dst>, kConstantPoolRegister + <offset>
310 //
311 // 2) OVERFLOWED access:
312 //    addis <scratch>, kConstantPoolRegister, <offset_high>
313 //    load <dst>, <scratch> + <offset_low>
IsConstantPoolLoadStart(Address pc,ConstantPoolEntry::Access * access)314 bool Assembler::IsConstantPoolLoadStart(Address pc,
315                                         ConstantPoolEntry::Access* access) {
316   Instr instr = instr_at(pc);
317   uint32_t opcode = instr & kOpcodeMask;
318   if (GetRA(instr) != kConstantPoolRegister) return false;
319   bool overflowed = (opcode == ADDIS);
320 #ifdef DEBUG
321   if (overflowed) {
322     opcode = instr_at(pc + kInstrSize) & kOpcodeMask;
323   }
324   DCHECK(opcode == kLoadIntptrOpcode || opcode == LFD);
325 #endif
326   if (access) {
327     *access = (overflowed ? ConstantPoolEntry::OVERFLOWED
328                           : ConstantPoolEntry::REGULAR);
329   }
330   return true;
331 }
332 
IsConstantPoolLoadEnd(Address pc,ConstantPoolEntry::Access * access)333 bool Assembler::IsConstantPoolLoadEnd(Address pc,
334                                       ConstantPoolEntry::Access* access) {
335   Instr instr = instr_at(pc);
336   uint32_t opcode = instr & kOpcodeMask;
337   bool overflowed = false;
338   if (!(opcode == kLoadIntptrOpcode || opcode == LFD)) return false;
339   if (GetRA(instr) != kConstantPoolRegister) {
340     instr = instr_at(pc - kInstrSize);
341     opcode = instr & kOpcodeMask;
342     if ((opcode != ADDIS) || GetRA(instr) != kConstantPoolRegister) {
343       return false;
344     }
345     overflowed = true;
346   }
347   if (access) {
348     *access = (overflowed ? ConstantPoolEntry::OVERFLOWED
349                           : ConstantPoolEntry::REGULAR);
350   }
351   return true;
352 }
353 
GetConstantPoolOffset(Address pc,ConstantPoolEntry::Access access,ConstantPoolEntry::Type type)354 int Assembler::GetConstantPoolOffset(Address pc,
355                                      ConstantPoolEntry::Access access,
356                                      ConstantPoolEntry::Type type) {
357   bool overflowed = (access == ConstantPoolEntry::OVERFLOWED);
358 #ifdef DEBUG
359   ConstantPoolEntry::Access access_check =
360       static_cast<ConstantPoolEntry::Access>(-1);
361   DCHECK(IsConstantPoolLoadStart(pc, &access_check));
362   DCHECK(access_check == access);
363 #endif
364   int offset;
365   if (overflowed) {
366     offset = (instr_at(pc) & kImm16Mask) << 16;
367     offset += SIGN_EXT_IMM16(instr_at(pc + kInstrSize) & kImm16Mask);
368     DCHECK(!is_int16(offset));
369   } else {
370     offset = SIGN_EXT_IMM16((instr_at(pc) & kImm16Mask));
371   }
372   return offset;
373 }
374 
PatchConstantPoolAccessInstruction(int pc_offset,int offset,ConstantPoolEntry::Access access,ConstantPoolEntry::Type type)375 void Assembler::PatchConstantPoolAccessInstruction(
376     int pc_offset, int offset, ConstantPoolEntry::Access access,
377     ConstantPoolEntry::Type type) {
378   Address pc = reinterpret_cast<Address>(buffer_start_) + pc_offset;
379   bool overflowed = (access == ConstantPoolEntry::OVERFLOWED);
380   CHECK(overflowed != is_int16(offset));
381 #ifdef DEBUG
382   ConstantPoolEntry::Access access_check =
383       static_cast<ConstantPoolEntry::Access>(-1);
384   DCHECK(IsConstantPoolLoadStart(pc, &access_check));
385   DCHECK(access_check == access);
386 #endif
387   if (overflowed) {
388     int hi_word = static_cast<int>(offset >> 16);
389     int lo_word = static_cast<int>(offset & 0xffff);
390     if (lo_word & 0x8000) hi_word++;
391 
392     Instr instr1 = instr_at(pc);
393     Instr instr2 = instr_at(pc + kInstrSize);
394     instr1 &= ~kImm16Mask;
395     instr1 |= (hi_word & kImm16Mask);
396     instr2 &= ~kImm16Mask;
397     instr2 |= (lo_word & kImm16Mask);
398     instr_at_put(pc, instr1);
399     instr_at_put(pc + kInstrSize, instr2);
400   } else {
401     Instr instr = instr_at(pc);
402     instr &= ~kImm16Mask;
403     instr |= (offset & kImm16Mask);
404     instr_at_put(pc, instr);
405   }
406 }
407 
target_constant_pool_address_at(Address pc,Address constant_pool,ConstantPoolEntry::Access access,ConstantPoolEntry::Type type)408 Address Assembler::target_constant_pool_address_at(
409     Address pc, Address constant_pool, ConstantPoolEntry::Access access,
410     ConstantPoolEntry::Type type) {
411   Address addr = constant_pool;
412   DCHECK(addr);
413   addr += GetConstantPoolOffset(pc, access, type);
414   return addr;
415 }
416 
417 // This sets the branch destination (which gets loaded at the call address).
418 // This is for calls and branches within generated code.  The serializer
419 // has already deserialized the mov instructions etc.
420 // There is a FIXED_SEQUENCE assumption here
deserialization_set_special_target_at(Address instruction_payload,Code code,Address target)421 void Assembler::deserialization_set_special_target_at(
422     Address instruction_payload, Code code, Address target) {
423   set_target_address_at(instruction_payload,
424                         !code.is_null() ? code.constant_pool() : kNullAddress,
425                         target);
426 }
427 
deserialization_special_target_size(Address instruction_payload)428 int Assembler::deserialization_special_target_size(
429     Address instruction_payload) {
430   return kSpecialTargetSize;
431 }
432 
deserialization_set_target_internal_reference_at(Address pc,Address target,RelocInfo::Mode mode)433 void Assembler::deserialization_set_target_internal_reference_at(
434     Address pc, Address target, RelocInfo::Mode mode) {
435   if (RelocInfo::IsInternalReferenceEncoded(mode)) {
436     set_target_address_at(pc, kNullAddress, target, SKIP_ICACHE_FLUSH);
437   } else {
438     Memory<Address>(pc) = target;
439   }
440 }
441 
442 // This code assumes the FIXED_SEQUENCE of lis/ori
set_target_address_at(Address pc,Address constant_pool,Address target,ICacheFlushMode icache_flush_mode)443 void Assembler::set_target_address_at(Address pc, Address constant_pool,
444                                       Address target,
445                                       ICacheFlushMode icache_flush_mode) {
446   if (FLAG_enable_embedded_constant_pool && constant_pool) {
447     ConstantPoolEntry::Access access;
448     if (IsConstantPoolLoadStart(pc, &access)) {
449       Memory<Address>(target_constant_pool_address_at(
450           pc, constant_pool, access, ConstantPoolEntry::INTPTR)) = target;
451       return;
452     }
453   }
454 
455   Instr instr1 = instr_at(pc);
456   Instr instr2 = instr_at(pc + kInstrSize);
457   // Interpret 2 instructions generated by lis/ori
458   if (IsLis(instr1) && IsOri(instr2)) {
459 #if V8_TARGET_ARCH_PPC64
460     Instr instr4 = instr_at(pc + (3 * kInstrSize));
461     Instr instr5 = instr_at(pc + (4 * kInstrSize));
462     // Needs to be fixed up when mov changes to handle 64-bit values.
463     uint32_t* p = reinterpret_cast<uint32_t*>(pc);
464     uintptr_t itarget = static_cast<uintptr_t>(target);
465 
466     instr5 &= ~kImm16Mask;
467     instr5 |= itarget & kImm16Mask;
468     itarget = itarget >> 16;
469 
470     instr4 &= ~kImm16Mask;
471     instr4 |= itarget & kImm16Mask;
472     itarget = itarget >> 16;
473 
474     instr2 &= ~kImm16Mask;
475     instr2 |= itarget & kImm16Mask;
476     itarget = itarget >> 16;
477 
478     instr1 &= ~kImm16Mask;
479     instr1 |= itarget & kImm16Mask;
480     itarget = itarget >> 16;
481 
482     *p = instr1;
483     *(p + 1) = instr2;
484     *(p + 3) = instr4;
485     *(p + 4) = instr5;
486     if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
487       FlushInstructionCache(p, 5 * kInstrSize);
488     }
489 #else
490     uint32_t* p = reinterpret_cast<uint32_t*>(pc);
491     uint32_t itarget = static_cast<uint32_t>(target);
492     int lo_word = itarget & kImm16Mask;
493     int hi_word = itarget >> 16;
494     instr1 &= ~kImm16Mask;
495     instr1 |= hi_word;
496     instr2 &= ~kImm16Mask;
497     instr2 |= lo_word;
498 
499     *p = instr1;
500     *(p + 1) = instr2;
501     if (icache_flush_mode != SKIP_ICACHE_FLUSH) {
502       FlushInstructionCache(p, 2 * kInstrSize);
503     }
504 #endif
505     return;
506   }
507   UNREACHABLE();
508 }
509 }  // namespace internal
510 }  // namespace v8
511 
512 #endif  // V8_CODEGEN_PPC_ASSEMBLER_PPC_INL_H_
513