V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
assembler-mips-inl.h
1 
2 // Copyright (c) 1994-2006 Sun Microsystems Inc.
3 // All Rights Reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // - Redistributions of source code must retain the above copyright notice,
10 // this list of conditions and the following disclaimer.
11 //
12 // - Redistribution in binary form must reproduce the above copyright
13 // notice, this list of conditions and the following disclaimer in the
14 // documentation and/or other materials provided with the 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 "AS
21 // IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 
32 // The original source code covered by the above license above has been
33 // modified significantly by Google Inc.
34 // Copyright 2012 the V8 project authors. All rights reserved.
35 
36 
37 #ifndef V8_MIPS_ASSEMBLER_MIPS_INL_H_
38 #define V8_MIPS_ASSEMBLER_MIPS_INL_H_
39 
40 #include "src/mips/assembler-mips.h"
41 
42 #include "src/assembler.h"
43 #include "src/debug/debug.h"
44 #include "src/objects-inl.h"
45 
46 namespace v8 {
47 namespace internal {
48 
49 bool CpuFeatures::SupportsOptimizer() { return IsSupported(FPU); }
50 
51 bool CpuFeatures::SupportsWasmSimd128() { return IsSupported(MIPS_SIMD); }
52 
53 // -----------------------------------------------------------------------------
54 // Operand and MemOperand.
55 
56 bool Operand::is_reg() const {
57  return rm_.is_valid();
58 }
59 
60 int32_t Operand::immediate() const {
61  DCHECK(!is_reg());
62  DCHECK(!IsHeapObjectRequest());
63  return value_.immediate;
64 }
65 
66 // -----------------------------------------------------------------------------
67 // RelocInfo.
68 
69 void RelocInfo::apply(intptr_t delta) {
70  if (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
71  // Absolute code pointer inside code object moves with the code object.
72  Assembler::RelocateInternalReference(rmode_, pc_, delta);
73  } else if (IsRelativeCodeTarget(rmode_)) {
74  Assembler::RelocateRelativeReference(rmode_, pc_, delta);
75  }
76 }
77 
78 
79 Address RelocInfo::target_address() {
80  DCHECK(IsCodeTargetMode(rmode_) || IsRuntimeEntry(rmode_) ||
81  IsWasmCall(rmode_));
82  return Assembler::target_address_at(pc_, constant_pool_);
83 }
84 
85 Address RelocInfo::target_address_address() {
86  DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_) || IsWasmCall(rmode_) ||
87  IsEmbeddedObject(rmode_) || IsExternalReference(rmode_) ||
88  IsOffHeapTarget(rmode_));
89  // Read the address of the word containing the target_address in an
90  // instruction stream.
91  // The only architecture-independent user of this function is the serializer.
92  // The serializer uses it to find out how many raw bytes of instruction to
93  // output before the next target.
94  // For an instruction like LUI/ORI where the target bits are mixed into the
95  // instruction bits, the size of the target will be zero, indicating that the
96  // serializer should not step forward in memory after a target is resolved
97  // and written. In this case the target_address_address function should
98  // return the end of the instructions to be patched, allowing the
99  // deserializer to deserialize the instructions as raw bytes and put them in
100  // place, ready to be patched with the target. After jump optimization,
101  // that is the address of the instruction that follows J/JAL/JR/JALR
102  // instruction.
103  if (IsMipsArchVariant(kMips32r6)) {
104  // On R6 we don't move to the end of the instructions to be patched, but one
105  // instruction before, because if these instructions are at the end of the
106  // code object it can cause errors in the deserializer.
107  return pc_ + (Assembler::kInstructionsFor32BitConstant - 1) * kInstrSize;
108  } else {
109  return pc_ + Assembler::kInstructionsFor32BitConstant * kInstrSize;
110  }
111 }
112 
113 Address RelocInfo::constant_pool_entry_address() {
114  UNREACHABLE();
115 }
116 
117 
118 int RelocInfo::target_address_size() {
119  return Assembler::kSpecialTargetSize;
120 }
121 
122 Address Assembler::target_address_from_return_address(Address pc) {
123  return pc - kCallTargetAddressOffset;
124 }
125 
126 void Assembler::deserialization_set_special_target_at(
127  Address instruction_payload, Code code, Address target) {
128  if (IsMipsArchVariant(kMips32r6)) {
129  // On R6 the address location is shifted by one instruction
130  set_target_address_at(
131  instruction_payload - (kInstructionsFor32BitConstant - 1) * kInstrSize,
132  code ? code->constant_pool() : kNullAddress, target);
133  } else {
134  set_target_address_at(
135  instruction_payload - kInstructionsFor32BitConstant * kInstrSize,
136  code ? code->constant_pool() : kNullAddress, target);
137  }
138 }
139 
140 int Assembler::deserialization_special_target_size(
141  Address instruction_payload) {
142  return kSpecialTargetSize;
143 }
144 
145 void Assembler::set_target_internal_reference_encoded_at(Address pc,
146  Address target) {
147  Instr instr1 = Assembler::instr_at(pc + 0 * kInstrSize);
148  Instr instr2 = Assembler::instr_at(pc + 1 * kInstrSize);
149  DCHECK(Assembler::IsLui(instr1));
150  DCHECK(Assembler::IsOri(instr2) || Assembler::IsJicOrJialc(instr2));
151  instr1 &= ~kImm16Mask;
152  instr2 &= ~kImm16Mask;
153  int32_t imm = static_cast<int32_t>(target);
154  DCHECK_EQ(imm & 3, 0);
155  if (Assembler::IsJicOrJialc(instr2)) {
156  // Encoded internal references are lui/jic load of 32-bit absolute address.
157  uint32_t lui_offset_u, jic_offset_u;
158  Assembler::UnpackTargetAddressUnsigned(imm, lui_offset_u, jic_offset_u);
159 
160  Assembler::instr_at_put(pc + 0 * kInstrSize, instr1 | lui_offset_u);
161  Assembler::instr_at_put(pc + 1 * kInstrSize, instr2 | jic_offset_u);
162  } else {
163  // Encoded internal references are lui/ori load of 32-bit absolute address.
164  PatchLuiOriImmediate(pc, imm, instr1, 0 * kInstrSize, instr2,
165  1 * kInstrSize);
166  }
167 
168  // Currently used only by deserializer, and all code will be flushed
169  // after complete deserialization, no need to flush on each reference.
170 }
171 
172 void Assembler::deserialization_set_target_internal_reference_at(
173  Address pc, Address target, RelocInfo::Mode mode) {
174  if (RelocInfo::IsInternalReferenceEncoded(mode)) {
175  DCHECK(IsLui(instr_at(pc)));
176  set_target_internal_reference_encoded_at(pc, target);
177  } else {
178  DCHECK(RelocInfo::IsInternalReference(mode));
179  Memory<Address>(pc) = target;
180  }
181 }
182 
183 HeapObject* RelocInfo::target_object() {
184  DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObject(rmode_));
185  return HeapObject::cast(reinterpret_cast<Object*>(
186  Assembler::target_address_at(pc_, constant_pool_)));
187 }
188 
189 Handle<HeapObject> RelocInfo::target_object_handle(Assembler* origin) {
190  if (IsCodeTarget(rmode_) || IsEmbeddedObject(rmode_)) {
191  return Handle<HeapObject>(reinterpret_cast<Address*>(
192  Assembler::target_address_at(pc_, constant_pool_)));
193  }
194  DCHECK(IsRelativeCodeTarget(rmode_));
195  return origin->relative_code_target_object_handle_at(pc_);
196 }
197 
198 void RelocInfo::set_target_object(Heap* heap, HeapObject* target,
199  WriteBarrierMode write_barrier_mode,
200  ICacheFlushMode icache_flush_mode) {
201  DCHECK(IsCodeTarget(rmode_) || IsEmbeddedObject(rmode_));
202  Assembler::set_target_address_at(pc_, constant_pool_,
203  reinterpret_cast<Address>(target),
204  icache_flush_mode);
205  if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != nullptr) {
206  WriteBarrierForCode(host(), this, target);
207  }
208 }
209 
210 
211 Address RelocInfo::target_external_reference() {
212  DCHECK(IsExternalReference(rmode_));
213  return Assembler::target_address_at(pc_, constant_pool_);
214 }
215 
216 void RelocInfo::set_target_external_reference(
217  Address target, ICacheFlushMode icache_flush_mode) {
218  DCHECK(IsExternalReference(rmode_));
219  Assembler::set_target_address_at(pc_, constant_pool_, target,
220  icache_flush_mode);
221 }
222 
223 Address RelocInfo::target_internal_reference() {
224  if (IsInternalReference(rmode_)) {
225  return Memory<Address>(pc_);
226  } else {
227  // Encoded internal references are lui/ori or lui/jic load of 32-bit
228  // absolute address.
229  DCHECK(IsInternalReferenceEncoded(rmode_));
230  Instr instr1 = Assembler::instr_at(pc_ + 0 * kInstrSize);
231  Instr instr2 = Assembler::instr_at(pc_ + 1 * kInstrSize);
232  DCHECK(Assembler::IsLui(instr1));
233  DCHECK(Assembler::IsOri(instr2) || Assembler::IsJicOrJialc(instr2));
234  if (Assembler::IsJicOrJialc(instr2)) {
235  return static_cast<Address>(
236  Assembler::CreateTargetAddress(instr1, instr2));
237  }
238  return static_cast<Address>(Assembler::GetLuiOriImmediate(instr1, instr2));
239  }
240 }
241 
242 
243 Address RelocInfo::target_internal_reference_address() {
244  DCHECK(IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
245  return pc_;
246 }
247 
248 Address RelocInfo::target_runtime_entry(Assembler* origin) {
249  DCHECK(IsRuntimeEntry(rmode_));
250  return target_address();
251 }
252 
253 void RelocInfo::set_target_runtime_entry(Address target,
254  WriteBarrierMode write_barrier_mode,
255  ICacheFlushMode icache_flush_mode) {
256  DCHECK(IsRuntimeEntry(rmode_));
257  if (target_address() != target)
258  set_target_address(target, write_barrier_mode, icache_flush_mode);
259 }
260 
261 Address RelocInfo::target_off_heap_target() {
262  DCHECK(IsOffHeapTarget(rmode_));
263  return Assembler::target_address_at(pc_, constant_pool_);
264 }
265 
266 void RelocInfo::WipeOut() {
267  DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
268  IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
269  IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_) ||
270  IsOffHeapTarget(rmode_));
271  if (IsInternalReference(rmode_)) {
272  Memory<Address>(pc_) = kNullAddress;
273  } else if (IsInternalReferenceEncoded(rmode_)) {
274  Assembler::set_target_internal_reference_encoded_at(pc_, kNullAddress);
275  } else {
276  Assembler::set_target_address_at(pc_, constant_pool_, kNullAddress);
277  }
278 }
279 
280 Handle<Code> Assembler::relative_code_target_object_handle_at(
281  Address pc) const {
282  Instr instr1 = instr_at(pc);
283  Instr instr2 = instr_at(pc + kInstrSize);
284  DCHECK(IsLui(instr1));
285  DCHECK(IsOri(instr2) || IsNal(instr2));
286  DCHECK(IsNal(instr2) || IsNal(instr_at(pc - kInstrSize)));
287  if (IsNal(instr2)) {
288  instr2 = instr_at(pc + 2 * kInstrSize);
289  }
290  // Interpret 2 instructions generated by li (lui/ori).
291  int code_target_index = GetLuiOriImmediate(instr1, instr2);
292  return GetCodeTarget(code_target_index);
293 }
294 
295 template <typename ObjectVisitor>
296 void RelocInfo::Visit(ObjectVisitor* visitor) {
297  Mode mode = rmode();
298  if (IsEmbeddedObject(mode)) {
299  visitor->VisitEmbeddedPointer(host(), this);
300  } else if (IsCodeTargetMode(mode)) {
301  visitor->VisitCodeTarget(host(), this);
302  } else if (IsExternalReference(mode)) {
303  visitor->VisitExternalReference(host(), this);
304  } else if (IsInternalReference(mode) || IsInternalReferenceEncoded(mode)) {
305  visitor->VisitInternalReference(host(), this);
306  } else if (IsRuntimeEntry(mode)) {
307  visitor->VisitRuntimeEntry(host(), this);
308  } else if (IsOffHeapTarget(mode)) {
309  visitor->VisitOffHeapTarget(host(), this);
310  }
311 }
312 
313 // -----------------------------------------------------------------------------
314 // Assembler.
315 
316 
317 void Assembler::CheckBuffer() {
318  if (buffer_space() <= kGap) {
319  GrowBuffer();
320  }
321 }
322 
323 
324 void Assembler::CheckForEmitInForbiddenSlot() {
325  if (!is_buffer_growth_blocked()) {
326  CheckBuffer();
327  }
328  if (IsPrevInstrCompactBranch()) {
329  // Nop instruction to precede a CTI in forbidden slot:
330  Instr nop = SPECIAL | SLL;
331  *reinterpret_cast<Instr*>(pc_) = nop;
332  pc_ += kInstrSize;
333 
334  ClearCompactBranchState();
335  }
336 }
337 
338 
339 void Assembler::EmitHelper(Instr x, CompactBranchType is_compact_branch) {
340  if (IsPrevInstrCompactBranch()) {
341  if (Instruction::IsForbiddenAfterBranchInstr(x)) {
342  // Nop instruction to precede a CTI in forbidden slot:
343  Instr nop = SPECIAL | SLL;
344  *reinterpret_cast<Instr*>(pc_) = nop;
345  pc_ += kInstrSize;
346  }
347  ClearCompactBranchState();
348  }
349  *reinterpret_cast<Instr*>(pc_) = x;
350  pc_ += kInstrSize;
351  if (is_compact_branch == CompactBranchType::COMPACT_BRANCH) {
352  EmittedCompactBranchInstruction();
353  }
354  CheckTrampolinePoolQuick();
355 }
356 
357 template <>
358 inline void Assembler::EmitHelper(uint8_t x);
359 
360 template <typename T>
361 void Assembler::EmitHelper(T x) {
362  *reinterpret_cast<T*>(pc_) = x;
363  pc_ += sizeof(x);
364  CheckTrampolinePoolQuick();
365 }
366 
367 template <>
368 void Assembler::EmitHelper(uint8_t x) {
369  *reinterpret_cast<uint8_t*>(pc_) = x;
370  pc_ += sizeof(x);
371  if (reinterpret_cast<intptr_t>(pc_) % kInstrSize == 0) {
372  CheckTrampolinePoolQuick();
373  }
374 }
375 
376 void Assembler::emit(Instr x, CompactBranchType is_compact_branch) {
377  if (!is_buffer_growth_blocked()) {
378  CheckBuffer();
379  }
380  EmitHelper(x, is_compact_branch);
381 }
382 
383 EnsureSpace::EnsureSpace(Assembler* assembler) { assembler->CheckBuffer(); }
384 
385 } // namespace internal
386 } // namespace v8
387 
388 #endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_
Definition: libplatform.h:13