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