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