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
45
46 namespace v8 {
47 namespace internal {
48
49
SupportsCrankshaft()50 bool CpuFeatures::SupportsCrankshaft() { return IsSupported(FPU); }
51
SupportsSimd128()52 bool CpuFeatures::SupportsSimd128() { return false; }
53
54 // -----------------------------------------------------------------------------
55 // Operand and MemOperand.
56
Operand(int32_t immediate,RelocInfo::Mode rmode)57 Operand::Operand(int32_t immediate, RelocInfo::Mode rmode) {
58 rm_ = no_reg;
59 imm32_ = immediate;
60 rmode_ = rmode;
61 }
62
63
Operand(const ExternalReference & f)64 Operand::Operand(const ExternalReference& f) {
65 rm_ = no_reg;
66 imm32_ = reinterpret_cast<int32_t>(f.address());
67 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
68 }
69
70
Operand(Smi * value)71 Operand::Operand(Smi* value) {
72 rm_ = no_reg;
73 imm32_ = reinterpret_cast<intptr_t>(value);
74 rmode_ = RelocInfo::NONE32;
75 }
76
77
Operand(Register rm)78 Operand::Operand(Register rm) {
79 rm_ = rm;
80 }
81
82
is_reg()83 bool Operand::is_reg() const {
84 return rm_.is_valid();
85 }
86
87
88 // -----------------------------------------------------------------------------
89 // RelocInfo.
90
apply(intptr_t delta)91 void RelocInfo::apply(intptr_t delta) {
92 if (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
93 // Absolute code pointer inside code object moves with the code object.
94 byte* p = reinterpret_cast<byte*>(pc_);
95 int count = Assembler::RelocateInternalReference(rmode_, p, delta);
96 Assembler::FlushICache(isolate_, p, count * sizeof(uint32_t));
97 }
98 }
99
100
target_address()101 Address RelocInfo::target_address() {
102 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
103 return Assembler::target_address_at(pc_, host_);
104 }
105
target_address_address()106 Address RelocInfo::target_address_address() {
107 DCHECK(IsCodeTarget(rmode_) ||
108 IsRuntimeEntry(rmode_) ||
109 rmode_ == EMBEDDED_OBJECT ||
110 rmode_ == EXTERNAL_REFERENCE);
111 // Read the address of the word containing the target_address in an
112 // instruction stream.
113 // The only architecture-independent user of this function is the serializer.
114 // The serializer uses it to find out how many raw bytes of instruction to
115 // output before the next target.
116 // For an instruction like LUI/ORI where the target bits are mixed into the
117 // instruction bits, the size of the target will be zero, indicating that the
118 // serializer should not step forward in memory after a target is resolved
119 // and written. In this case the target_address_address function should
120 // return the end of the instructions to be patched, allowing the
121 // deserializer to deserialize the instructions as raw bytes and put them in
122 // place, ready to be patched with the target. After jump optimization,
123 // that is the address of the instruction that follows J/JAL/JR/JALR
124 // instruction.
125 return reinterpret_cast<Address>(
126 pc_ + Assembler::kInstructionsFor32BitConstant * Assembler::kInstrSize);
127 }
128
129
constant_pool_entry_address()130 Address RelocInfo::constant_pool_entry_address() {
131 UNREACHABLE();
132 return NULL;
133 }
134
135
target_address_size()136 int RelocInfo::target_address_size() {
137 return Assembler::kSpecialTargetSize;
138 }
139
140
target_address_from_return_address(Address pc)141 Address Assembler::target_address_from_return_address(Address pc) {
142 return pc - kCallTargetAddressOffset;
143 }
144
145
set_target_internal_reference_encoded_at(Address pc,Address target)146 void Assembler::set_target_internal_reference_encoded_at(Address pc,
147 Address target) {
148 Instr instr1 = Assembler::instr_at(pc + 0 * Assembler::kInstrSize);
149 Instr instr2 = Assembler::instr_at(pc + 1 * Assembler::kInstrSize);
150 DCHECK(Assembler::IsLui(instr1));
151 DCHECK(Assembler::IsOri(instr2) || Assembler::IsJicOrJialc(instr2));
152 instr1 &= ~kImm16Mask;
153 instr2 &= ~kImm16Mask;
154 int32_t imm = reinterpret_cast<int32_t>(target);
155 DCHECK((imm & 3) == 0);
156 if (Assembler::IsJicOrJialc(instr2)) {
157 // Encoded internal references are lui/jic load of 32-bit absolute address.
158 uint32_t lui_offset_u, jic_offset_u;
159 Assembler::UnpackTargetAddressUnsigned(imm, lui_offset_u, jic_offset_u);
160
161 Assembler::instr_at_put(pc + 0 * Assembler::kInstrSize,
162 instr1 | lui_offset_u);
163 Assembler::instr_at_put(pc + 1 * Assembler::kInstrSize,
164 instr2 | jic_offset_u);
165 } else {
166 // Encoded internal references are lui/ori load of 32-bit absolute address.
167 Assembler::instr_at_put(pc + 0 * Assembler::kInstrSize,
168 instr1 | ((imm >> kLuiShift) & kImm16Mask));
169 Assembler::instr_at_put(pc + 1 * Assembler::kInstrSize,
170 instr2 | (imm & kImm16Mask));
171 }
172
173 // Currently used only by deserializer, and all code will be flushed
174 // after complete deserialization, no need to flush on each reference.
175 }
176
177
deserialization_set_target_internal_reference_at(Isolate * isolate,Address pc,Address target,RelocInfo::Mode mode)178 void Assembler::deserialization_set_target_internal_reference_at(
179 Isolate* isolate, Address pc, Address target, RelocInfo::Mode mode) {
180 if (mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
181 DCHECK(IsLui(instr_at(pc)));
182 set_target_internal_reference_encoded_at(pc, target);
183 } else {
184 DCHECK(mode == RelocInfo::INTERNAL_REFERENCE);
185 Memory::Address_at(pc) = target;
186 }
187 }
188
189
target_object()190 Object* RelocInfo::target_object() {
191 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
192 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
193 }
194
195
target_object_handle(Assembler * origin)196 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
197 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
198 return Handle<Object>(reinterpret_cast<Object**>(
199 Assembler::target_address_at(pc_, host_)));
200 }
201
202
set_target_object(Object * target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)203 void RelocInfo::set_target_object(Object* target,
204 WriteBarrierMode write_barrier_mode,
205 ICacheFlushMode icache_flush_mode) {
206 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
207 Assembler::set_target_address_at(isolate_, pc_, host_,
208 reinterpret_cast<Address>(target),
209 icache_flush_mode);
210 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
211 host() != NULL &&
212 target->IsHeapObject()) {
213 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
214 host(), this, HeapObject::cast(target));
215 host()->GetHeap()->RecordWriteIntoCode(host(), this, target);
216 }
217 }
218
219
target_external_reference()220 Address RelocInfo::target_external_reference() {
221 DCHECK(rmode_ == EXTERNAL_REFERENCE);
222 return Assembler::target_address_at(pc_, host_);
223 }
224
225
target_internal_reference()226 Address RelocInfo::target_internal_reference() {
227 if (rmode_ == INTERNAL_REFERENCE) {
228 return Memory::Address_at(pc_);
229 } else {
230 // Encoded internal references are lui/ori or lui/jic load of 32-bit
231 // absolute address.
232 DCHECK(rmode_ == INTERNAL_REFERENCE_ENCODED);
233 Instr instr1 = Assembler::instr_at(pc_ + 0 * Assembler::kInstrSize);
234 Instr instr2 = Assembler::instr_at(pc_ + 1 * Assembler::kInstrSize);
235 DCHECK(Assembler::IsLui(instr1));
236 DCHECK(Assembler::IsOri(instr2) || Assembler::IsJicOrJialc(instr2));
237 if (Assembler::IsJicOrJialc(instr2)) {
238 return reinterpret_cast<Address>(
239 Assembler::CreateTargetAddress(instr1, instr2));
240 }
241 int32_t imm = (instr1 & static_cast<int32_t>(kImm16Mask)) << kLuiShift;
242 imm |= (instr2 & static_cast<int32_t>(kImm16Mask));
243 return reinterpret_cast<Address>(imm);
244 }
245 }
246
247
target_internal_reference_address()248 Address RelocInfo::target_internal_reference_address() {
249 DCHECK(rmode_ == INTERNAL_REFERENCE || rmode_ == INTERNAL_REFERENCE_ENCODED);
250 return reinterpret_cast<Address>(pc_);
251 }
252
253
target_runtime_entry(Assembler * origin)254 Address RelocInfo::target_runtime_entry(Assembler* origin) {
255 DCHECK(IsRuntimeEntry(rmode_));
256 return target_address();
257 }
258
259
set_target_runtime_entry(Address target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)260 void RelocInfo::set_target_runtime_entry(Address target,
261 WriteBarrierMode write_barrier_mode,
262 ICacheFlushMode icache_flush_mode) {
263 DCHECK(IsRuntimeEntry(rmode_));
264 if (target_address() != target)
265 set_target_address(target, write_barrier_mode, icache_flush_mode);
266 }
267
268
target_cell_handle()269 Handle<Cell> RelocInfo::target_cell_handle() {
270 DCHECK(rmode_ == RelocInfo::CELL);
271 Address address = Memory::Address_at(pc_);
272 return Handle<Cell>(reinterpret_cast<Cell**>(address));
273 }
274
275
target_cell()276 Cell* RelocInfo::target_cell() {
277 DCHECK(rmode_ == RelocInfo::CELL);
278 return Cell::FromValueAddress(Memory::Address_at(pc_));
279 }
280
281
set_target_cell(Cell * cell,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)282 void RelocInfo::set_target_cell(Cell* cell,
283 WriteBarrierMode write_barrier_mode,
284 ICacheFlushMode icache_flush_mode) {
285 DCHECK(rmode_ == RelocInfo::CELL);
286 Address address = cell->address() + Cell::kValueOffset;
287 Memory::Address_at(pc_) = address;
288 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
289 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(host(), this,
290 cell);
291 }
292 }
293
294
295 static const int kNoCodeAgeSequenceLength = 7 * Assembler::kInstrSize;
296
297
code_age_stub_handle(Assembler * origin)298 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
299 UNREACHABLE(); // This should never be reached on Arm.
300 return Handle<Object>();
301 }
302
303
code_age_stub()304 Code* RelocInfo::code_age_stub() {
305 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
306 return Code::GetCodeFromTargetAddress(
307 Assembler::target_address_at(pc_ + Assembler::kInstrSize, host_));
308 }
309
310
set_code_age_stub(Code * stub,ICacheFlushMode icache_flush_mode)311 void RelocInfo::set_code_age_stub(Code* stub,
312 ICacheFlushMode icache_flush_mode) {
313 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
314 Assembler::set_target_address_at(isolate_, pc_ + Assembler::kInstrSize, host_,
315 stub->instruction_start());
316 }
317
318
debug_call_address()319 Address RelocInfo::debug_call_address() {
320 // The pc_ offset of 0 assumes patched debug break slot or return
321 // sequence.
322 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
323 return Assembler::target_address_at(pc_, host_);
324 }
325
326
set_debug_call_address(Address target)327 void RelocInfo::set_debug_call_address(Address target) {
328 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
329 // The pc_ offset of 0 assumes patched debug break slot or return
330 // sequence.
331 Assembler::set_target_address_at(isolate_, pc_, host_, target);
332 if (host() != NULL) {
333 Object* target_code = Code::GetCodeFromTargetAddress(target);
334 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
335 host(), this, HeapObject::cast(target_code));
336 }
337 }
338
339
WipeOut()340 void RelocInfo::WipeOut() {
341 DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
342 IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
343 IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
344 if (IsInternalReference(rmode_)) {
345 Memory::Address_at(pc_) = NULL;
346 } else if (IsInternalReferenceEncoded(rmode_)) {
347 Assembler::set_target_internal_reference_encoded_at(pc_, nullptr);
348 } else {
349 Assembler::set_target_address_at(isolate_, pc_, host_, NULL);
350 }
351 }
352
353 template <typename ObjectVisitor>
Visit(Isolate * isolate,ObjectVisitor * visitor)354 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
355 RelocInfo::Mode mode = rmode();
356 if (mode == RelocInfo::EMBEDDED_OBJECT) {
357 visitor->VisitEmbeddedPointer(this);
358 } else if (RelocInfo::IsCodeTarget(mode)) {
359 visitor->VisitCodeTarget(this);
360 } else if (mode == RelocInfo::CELL) {
361 visitor->VisitCell(this);
362 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
363 visitor->VisitExternalReference(this);
364 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
365 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
366 visitor->VisitInternalReference(this);
367 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
368 visitor->VisitCodeAgeSequence(this);
369 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
370 IsPatchedDebugBreakSlotSequence()) {
371 visitor->VisitDebugTarget(this);
372 } else if (RelocInfo::IsRuntimeEntry(mode)) {
373 visitor->VisitRuntimeEntry(this);
374 }
375 }
376
377
378 template<typename StaticVisitor>
Visit(Heap * heap)379 void RelocInfo::Visit(Heap* heap) {
380 RelocInfo::Mode mode = rmode();
381 if (mode == RelocInfo::EMBEDDED_OBJECT) {
382 StaticVisitor::VisitEmbeddedPointer(heap, this);
383 } else if (RelocInfo::IsCodeTarget(mode)) {
384 StaticVisitor::VisitCodeTarget(heap, this);
385 } else if (mode == RelocInfo::CELL) {
386 StaticVisitor::VisitCell(heap, this);
387 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
388 StaticVisitor::VisitExternalReference(this);
389 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
390 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
391 StaticVisitor::VisitInternalReference(this);
392 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
393 StaticVisitor::VisitCodeAgeSequence(heap, this);
394 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
395 IsPatchedDebugBreakSlotSequence()) {
396 StaticVisitor::VisitDebugTarget(heap, this);
397 } else if (RelocInfo::IsRuntimeEntry(mode)) {
398 StaticVisitor::VisitRuntimeEntry(this);
399 }
400 }
401
402
403 // -----------------------------------------------------------------------------
404 // Assembler.
405
406
CheckBuffer()407 void Assembler::CheckBuffer() {
408 if (buffer_space() <= kGap) {
409 GrowBuffer();
410 }
411 }
412
413
CheckTrampolinePoolQuick(int extra_instructions)414 void Assembler::CheckTrampolinePoolQuick(int extra_instructions) {
415 if (pc_offset() >= next_buffer_check_ - extra_instructions * kInstrSize) {
416 CheckTrampolinePool();
417 }
418 }
419
420
CheckForEmitInForbiddenSlot()421 void Assembler::CheckForEmitInForbiddenSlot() {
422 if (!is_buffer_growth_blocked()) {
423 CheckBuffer();
424 }
425 if (IsPrevInstrCompactBranch()) {
426 // Nop instruction to preceed a CTI in forbidden slot:
427 Instr nop = SPECIAL | SLL;
428 *reinterpret_cast<Instr*>(pc_) = nop;
429 pc_ += kInstrSize;
430
431 ClearCompactBranchState();
432 }
433 }
434
435
EmitHelper(Instr x,CompactBranchType is_compact_branch)436 void Assembler::EmitHelper(Instr x, CompactBranchType is_compact_branch) {
437 if (IsPrevInstrCompactBranch()) {
438 if (Instruction::IsForbiddenAfterBranchInstr(x)) {
439 // Nop instruction to preceed a CTI in forbidden slot:
440 Instr nop = SPECIAL | SLL;
441 *reinterpret_cast<Instr*>(pc_) = nop;
442 pc_ += kInstrSize;
443 }
444 ClearCompactBranchState();
445 }
446 *reinterpret_cast<Instr*>(pc_) = x;
447 pc_ += kInstrSize;
448 if (is_compact_branch == CompactBranchType::COMPACT_BRANCH) {
449 EmittedCompactBranchInstruction();
450 }
451 CheckTrampolinePoolQuick();
452 }
453
454 template <>
455 inline void Assembler::EmitHelper(uint8_t x);
456
457 template <typename T>
EmitHelper(T x)458 void Assembler::EmitHelper(T x) {
459 *reinterpret_cast<T*>(pc_) = x;
460 pc_ += sizeof(x);
461 CheckTrampolinePoolQuick();
462 }
463
464 template <>
EmitHelper(uint8_t x)465 void Assembler::EmitHelper(uint8_t x) {
466 *reinterpret_cast<uint8_t*>(pc_) = x;
467 pc_ += sizeof(x);
468 if (reinterpret_cast<intptr_t>(pc_) % kInstrSize == 0) {
469 CheckTrampolinePoolQuick();
470 }
471 }
472
emit(Instr x,CompactBranchType is_compact_branch)473 void Assembler::emit(Instr x, CompactBranchType is_compact_branch) {
474 if (!is_buffer_growth_blocked()) {
475 CheckBuffer();
476 }
477 EmitHelper(x, is_compact_branch);
478 }
479
480
481 } // namespace internal
482 } // namespace v8
483
484 #endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_
485