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 #ifndef V8_CODEGEN_MIPS_ASSEMBLER_MIPS_INL_H_
37 #define V8_CODEGEN_MIPS_ASSEMBLER_MIPS_INL_H_
38
39 #include "src/codegen/mips/assembler-mips.h"
40
41 #include "src/codegen/assembler.h"
42 #include "src/debug/debug.h"
43 #include "src/objects/objects-inl.h"
44
45 namespace v8 {
46 namespace internal {
47
SupportsOptimizer()48 bool CpuFeatures::SupportsOptimizer() { return IsSupported(FPU); }
49
SupportsWasmSimd128()50 bool CpuFeatures::SupportsWasmSimd128() { return IsSupported(MIPS_SIMD); }
51
52 // -----------------------------------------------------------------------------
53 // Operand and MemOperand.
54
is_reg()55 bool Operand::is_reg() const { return rm_.is_valid(); }
56
immediate()57 int32_t Operand::immediate() const {
58 DCHECK(!is_reg());
59 DCHECK(!IsHeapObjectRequest());
60 return value_.immediate;
61 }
62
63 // -----------------------------------------------------------------------------
64 // RelocInfo.
65
apply(intptr_t delta)66 void RelocInfo::apply(intptr_t delta) {
67 if (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
68 // Absolute code pointer inside code object moves with the code object.
69 Assembler::RelocateInternalReference(rmode_, pc_, delta);
70 } else if (IsRelativeCodeTarget(rmode_)) {
71 Assembler::RelocateRelativeReference(rmode_, pc_, delta);
72 }
73 }
74
target_address()75 Address RelocInfo::target_address() {
76 DCHECK(IsCodeTargetMode(rmode_) || IsRuntimeEntry(rmode_) ||
77 IsWasmCall(rmode_));
78 return Assembler::target_address_at(pc_, constant_pool_);
79 }
80
target_address_address()81 Address RelocInfo::target_address_address() {
82 DCHECK(HasTargetAddressAddress());
83 // Read the address of the word containing the target_address in an
84 // instruction stream.
85 // The only architecture-independent user of this function is the serializer.
86 // The serializer uses it to find out how many raw bytes of instruction to
87 // output before the next target.
88 // For an instruction like LUI/ORI where the target bits are mixed into the
89 // instruction bits, the size of the target will be zero, indicating that the
90 // serializer should not step forward in memory after a target is resolved
91 // and written. In this case the target_address_address function should
92 // return the end of the instructions to be patched, allowing the
93 // deserializer to deserialize the instructions as raw bytes and put them in
94 // place, ready to be patched with the target. After jump optimization,
95 // that is the address of the instruction that follows J/JAL/JR/JALR
96 // instruction.
97 if (IsMipsArchVariant(kMips32r6)) {
98 // On R6 we don't move to the end of the instructions to be patched, but one
99 // instruction before, because if these instructions are at the end of the
100 // code object it can cause errors in the deserializer.
101 return pc_ + (Assembler::kInstructionsFor32BitConstant - 1) * kInstrSize;
102 } else {
103 return pc_ + Assembler::kInstructionsFor32BitConstant * kInstrSize;
104 }
105 }
106
constant_pool_entry_address()107 Address RelocInfo::constant_pool_entry_address() { UNREACHABLE(); }
108
target_address_size()109 int RelocInfo::target_address_size() { return Assembler::kSpecialTargetSize; }
110
deserialization_set_special_target_at(Address instruction_payload,Code code,Address target)111 void Assembler::deserialization_set_special_target_at(
112 Address instruction_payload, Code code, Address target) {
113 set_target_address_at(instruction_payload,
114 !code.is_null() ? code.constant_pool() : kNullAddress,
115 target);
116 }
117
deserialization_special_target_size(Address instruction_payload)118 int Assembler::deserialization_special_target_size(
119 Address instruction_payload) {
120 return kSpecialTargetSize;
121 }
122
set_target_internal_reference_encoded_at(Address pc,Address target)123 void Assembler::set_target_internal_reference_encoded_at(Address pc,
124 Address target) {
125 Instr instr1 = Assembler::instr_at(pc + 0 * kInstrSize);
126 Instr instr2 = Assembler::instr_at(pc + 1 * kInstrSize);
127 DCHECK(Assembler::IsLui(instr1));
128 DCHECK(Assembler::IsOri(instr2) || Assembler::IsJicOrJialc(instr2));
129 instr1 &= ~kImm16Mask;
130 instr2 &= ~kImm16Mask;
131 int32_t imm = static_cast<int32_t>(target);
132 DCHECK_EQ(imm & 3, 0);
133 if (Assembler::IsJicOrJialc(instr2)) {
134 // Encoded internal references are lui/jic load of 32-bit absolute address.
135 uint32_t lui_offset_u, jic_offset_u;
136 Assembler::UnpackTargetAddressUnsigned(imm, &lui_offset_u, &jic_offset_u);
137
138 Assembler::instr_at_put(pc + 0 * kInstrSize, instr1 | lui_offset_u);
139 Assembler::instr_at_put(pc + 1 * kInstrSize, instr2 | jic_offset_u);
140 } else {
141 // Encoded internal references are lui/ori load of 32-bit absolute address.
142 PatchLuiOriImmediate(pc, imm, instr1, 0 * kInstrSize, instr2,
143 1 * kInstrSize);
144 }
145
146 // Currently used only by deserializer, and all code will be flushed
147 // after complete deserialization, no need to flush on each reference.
148 }
149
deserialization_set_target_internal_reference_at(Address pc,Address target,RelocInfo::Mode mode)150 void Assembler::deserialization_set_target_internal_reference_at(
151 Address pc, Address target, RelocInfo::Mode mode) {
152 if (RelocInfo::IsInternalReferenceEncoded(mode)) {
153 DCHECK(IsLui(instr_at(pc)));
154 set_target_internal_reference_encoded_at(pc, target);
155 } else {
156 DCHECK(RelocInfo::IsInternalReference(mode));
157 Memory<Address>(pc) = target;
158 }
159 }
160
target_object()161 HeapObject RelocInfo::target_object() {
162 DCHECK(IsCodeTarget(rmode_) || IsFullEmbeddedObject(rmode_));
163 return HeapObject::cast(
164 Object(Assembler::target_address_at(pc_, constant_pool_)));
165 }
166
target_object_no_host(Isolate * isolate)167 HeapObject RelocInfo::target_object_no_host(Isolate* isolate) {
168 return target_object();
169 }
170
target_object_handle(Assembler * origin)171 Handle<HeapObject> RelocInfo::target_object_handle(Assembler* origin) {
172 if (IsCodeTarget(rmode_) || IsFullEmbeddedObject(rmode_)) {
173 return Handle<HeapObject>(reinterpret_cast<Address*>(
174 Assembler::target_address_at(pc_, constant_pool_)));
175 }
176 DCHECK(IsRelativeCodeTarget(rmode_));
177 return origin->relative_code_target_object_handle_at(pc_);
178 }
179
set_target_object(Heap * heap,HeapObject target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)180 void RelocInfo::set_target_object(Heap* heap, HeapObject target,
181 WriteBarrierMode write_barrier_mode,
182 ICacheFlushMode icache_flush_mode) {
183 DCHECK(IsCodeTarget(rmode_) || IsFullEmbeddedObject(rmode_));
184 Assembler::set_target_address_at(pc_, constant_pool_, target.ptr(),
185 icache_flush_mode);
186 if (write_barrier_mode == UPDATE_WRITE_BARRIER && !host().is_null() &&
187 !FLAG_disable_write_barriers) {
188 WriteBarrierForCode(host(), this, target);
189 }
190 }
191
target_external_reference()192 Address RelocInfo::target_external_reference() {
193 DCHECK(IsExternalReference(rmode_));
194 return Assembler::target_address_at(pc_, constant_pool_);
195 }
196
set_target_external_reference(Address target,ICacheFlushMode icache_flush_mode)197 void RelocInfo::set_target_external_reference(
198 Address target, ICacheFlushMode icache_flush_mode) {
199 DCHECK(IsExternalReference(rmode_));
200 Assembler::set_target_address_at(pc_, constant_pool_, target,
201 icache_flush_mode);
202 }
203
target_internal_reference()204 Address RelocInfo::target_internal_reference() {
205 if (IsInternalReference(rmode_)) {
206 return Memory<Address>(pc_);
207 } else {
208 // Encoded internal references are lui/ori or lui/jic load of 32-bit
209 // absolute address.
210 DCHECK(IsInternalReferenceEncoded(rmode_));
211 Instr instr1 = Assembler::instr_at(pc_ + 0 * kInstrSize);
212 Instr instr2 = Assembler::instr_at(pc_ + 1 * kInstrSize);
213 DCHECK(Assembler::IsLui(instr1));
214 DCHECK(Assembler::IsOri(instr2) || Assembler::IsJicOrJialc(instr2));
215 if (Assembler::IsJicOrJialc(instr2)) {
216 return static_cast<Address>(
217 Assembler::CreateTargetAddress(instr1, instr2));
218 }
219 return static_cast<Address>(Assembler::GetLuiOriImmediate(instr1, instr2));
220 }
221 }
222
target_internal_reference_address()223 Address RelocInfo::target_internal_reference_address() {
224 DCHECK(IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
225 return pc_;
226 }
227
target_runtime_entry(Assembler * origin)228 Address RelocInfo::target_runtime_entry(Assembler* origin) {
229 DCHECK(IsRuntimeEntry(rmode_));
230 return target_address();
231 }
232
set_target_runtime_entry(Address target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)233 void RelocInfo::set_target_runtime_entry(Address target,
234 WriteBarrierMode write_barrier_mode,
235 ICacheFlushMode icache_flush_mode) {
236 DCHECK(IsRuntimeEntry(rmode_));
237 if (target_address() != target)
238 set_target_address(target, write_barrier_mode, icache_flush_mode);
239 }
240
target_off_heap_target()241 Address RelocInfo::target_off_heap_target() {
242 DCHECK(IsOffHeapTarget(rmode_));
243 return Assembler::target_address_at(pc_, constant_pool_);
244 }
245
WipeOut()246 void RelocInfo::WipeOut() {
247 DCHECK(IsFullEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
248 IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
249 IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_) ||
250 IsOffHeapTarget(rmode_));
251 if (IsInternalReference(rmode_)) {
252 Memory<Address>(pc_) = kNullAddress;
253 } else if (IsInternalReferenceEncoded(rmode_)) {
254 Assembler::set_target_internal_reference_encoded_at(pc_, kNullAddress);
255 } else {
256 Assembler::set_target_address_at(pc_, constant_pool_, kNullAddress);
257 }
258 }
259
relative_code_target_object_handle_at(Address pc)260 Handle<Code> Assembler::relative_code_target_object_handle_at(
261 Address pc) const {
262 Instr instr1 = instr_at(pc);
263 Instr instr2 = instr_at(pc + kInstrSize);
264 DCHECK(IsLui(instr1));
265 DCHECK(IsOri(instr2) || IsNal(instr2));
266 DCHECK(IsNal(instr2) || IsNal(instr_at(pc - kInstrSize)));
267 if (IsNal(instr2)) {
268 instr2 = instr_at(pc + 2 * kInstrSize);
269 }
270 // Interpret 2 instructions generated by li (lui/ori).
271 int code_target_index = GetLuiOriImmediate(instr1, instr2);
272 return GetCodeTarget(code_target_index);
273 }
274
275 // -----------------------------------------------------------------------------
276 // Assembler.
277
CheckBuffer()278 void Assembler::CheckBuffer() {
279 if (buffer_space() <= kGap) {
280 GrowBuffer();
281 }
282 }
283
CheckForEmitInForbiddenSlot()284 void Assembler::CheckForEmitInForbiddenSlot() {
285 if (!is_buffer_growth_blocked()) {
286 CheckBuffer();
287 }
288 if (IsPrevInstrCompactBranch()) {
289 // Nop instruction to precede a CTI in forbidden slot:
290 Instr nop = SPECIAL | SLL;
291 *reinterpret_cast<Instr*>(pc_) = nop;
292 pc_ += kInstrSize;
293
294 ClearCompactBranchState();
295 }
296 }
297
EmitHelper(Instr x,CompactBranchType is_compact_branch)298 void Assembler::EmitHelper(Instr x, CompactBranchType is_compact_branch) {
299 if (IsPrevInstrCompactBranch()) {
300 if (Instruction::IsForbiddenAfterBranchInstr(x)) {
301 // Nop instruction to precede a CTI in forbidden slot:
302 Instr nop = SPECIAL | SLL;
303 *reinterpret_cast<Instr*>(pc_) = nop;
304 pc_ += kInstrSize;
305 }
306 ClearCompactBranchState();
307 }
308 *reinterpret_cast<Instr*>(pc_) = x;
309 pc_ += kInstrSize;
310 if (is_compact_branch == CompactBranchType::COMPACT_BRANCH) {
311 EmittedCompactBranchInstruction();
312 }
313 CheckTrampolinePoolQuick();
314 }
315
316 template <>
317 inline void Assembler::EmitHelper(uint8_t x);
318
319 template <typename T>
EmitHelper(T x)320 void Assembler::EmitHelper(T x) {
321 *reinterpret_cast<T*>(pc_) = x;
322 pc_ += sizeof(x);
323 CheckTrampolinePoolQuick();
324 }
325
326 template <>
EmitHelper(uint8_t x)327 void Assembler::EmitHelper(uint8_t x) {
328 *reinterpret_cast<uint8_t*>(pc_) = x;
329 pc_ += sizeof(x);
330 if (reinterpret_cast<intptr_t>(pc_) % kInstrSize == 0) {
331 CheckTrampolinePoolQuick();
332 }
333 }
334
emit(Instr x,CompactBranchType is_compact_branch)335 void Assembler::emit(Instr x, CompactBranchType is_compact_branch) {
336 if (!is_buffer_growth_blocked()) {
337 CheckBuffer();
338 }
339 EmitHelper(x, is_compact_branch);
340 }
341
EnsureSpace(Assembler * assembler)342 EnsureSpace::EnsureSpace(Assembler* assembler) { assembler->CheckBuffer(); }
343
344 } // namespace internal
345 } // namespace v8
346
347 #endif // V8_CODEGEN_MIPS_ASSEMBLER_MIPS_INL_H_
348