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/mips64/assembler-mips64.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(int64_t immediate,RelocInfo::Mode rmode)56 Operand::Operand(int64_t immediate, RelocInfo::Mode rmode) {
57 rm_ = no_reg;
58 imm64_ = immediate;
59 rmode_ = rmode;
60 }
61
62
Operand(const ExternalReference & f)63 Operand::Operand(const ExternalReference& f) {
64 rm_ = no_reg;
65 imm64_ = reinterpret_cast<int64_t>(f.address());
66 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
67 }
68
69
Operand(Smi * value)70 Operand::Operand(Smi* value) {
71 rm_ = no_reg;
72 imm64_ = 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 return reinterpret_cast<Address>(
127 pc_ + Assembler::kInstructionsFor64BitConstant * Assembler::kInstrSize);
128 }
129
130
constant_pool_entry_address()131 Address RelocInfo::constant_pool_entry_address() {
132 UNREACHABLE();
133 return NULL;
134 }
135
136
target_address_size()137 int RelocInfo::target_address_size() {
138 return Assembler::kSpecialTargetSize;
139 }
140
141
set_target_address(Address target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)142 void RelocInfo::set_target_address(Address target,
143 WriteBarrierMode write_barrier_mode,
144 ICacheFlushMode icache_flush_mode) {
145 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
146 Assembler::set_target_address_at(isolate_, pc_, host_, target,
147 icache_flush_mode);
148 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
149 host() != NULL && IsCodeTarget(rmode_)) {
150 Object* target_code = Code::GetCodeFromTargetAddress(target);
151 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
152 host(), this, HeapObject::cast(target_code));
153 }
154 }
155
target_address_from_return_address(Address pc)156 Address Assembler::target_address_from_return_address(Address pc) {
157 return pc - kCallTargetAddressOffset;
158 }
159
160
set_target_internal_reference_encoded_at(Address pc,Address target)161 void Assembler::set_target_internal_reference_encoded_at(Address pc,
162 Address target) {
163 // Encoded internal references are j/jal instructions.
164 Instr instr = Assembler::instr_at(pc + 0 * Assembler::kInstrSize);
165
166 uint64_t imm28 =
167 (reinterpret_cast<uint64_t>(target) & static_cast<uint64_t>(kImm28Mask));
168
169 instr &= ~kImm26Mask;
170 uint64_t imm26 = imm28 >> 2;
171 DCHECK(is_uint26(imm26));
172
173 instr_at_put(pc, instr | (imm26 & kImm26Mask));
174 // Currently used only by deserializer, and all code will be flushed
175 // after complete deserialization, no need to flush on each reference.
176 }
177
178
deserialization_set_target_internal_reference_at(Isolate * isolate,Address pc,Address target,RelocInfo::Mode mode)179 void Assembler::deserialization_set_target_internal_reference_at(
180 Isolate* isolate, Address pc, Address target, RelocInfo::Mode mode) {
181 if (mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
182 DCHECK(IsJ(instr_at(pc)));
183 set_target_internal_reference_encoded_at(pc, target);
184 } else {
185 DCHECK(mode == RelocInfo::INTERNAL_REFERENCE);
186 Memory::Address_at(pc) = target;
187 }
188 }
189
190
target_object()191 Object* RelocInfo::target_object() {
192 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
193 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
194 }
195
196
target_object_handle(Assembler * origin)197 Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
198 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
199 return Handle<Object>(reinterpret_cast<Object**>(
200 Assembler::target_address_at(pc_, host_)));
201 }
202
203
set_target_object(Object * target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)204 void RelocInfo::set_target_object(Object* target,
205 WriteBarrierMode write_barrier_mode,
206 ICacheFlushMode icache_flush_mode) {
207 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
208 Assembler::set_target_address_at(isolate_, pc_, host_,
209 reinterpret_cast<Address>(target),
210 icache_flush_mode);
211 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
212 host() != NULL &&
213 target->IsHeapObject()) {
214 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
215 host(), this, HeapObject::cast(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 j/jal instructions.
231 DCHECK(rmode_ == INTERNAL_REFERENCE_ENCODED);
232 Instr instr = Assembler::instr_at(pc_ + 0 * Assembler::kInstrSize);
233 instr &= kImm26Mask;
234 uint64_t imm28 = instr << 2;
235 uint64_t segment =
236 (reinterpret_cast<uint64_t>(pc_) & ~static_cast<uint64_t>(kImm28Mask));
237 return reinterpret_cast<Address>(segment | imm28);
238 }
239 }
240
241
target_internal_reference_address()242 Address RelocInfo::target_internal_reference_address() {
243 DCHECK(rmode_ == INTERNAL_REFERENCE || rmode_ == INTERNAL_REFERENCE_ENCODED);
244 return reinterpret_cast<Address>(pc_);
245 }
246
247
target_runtime_entry(Assembler * origin)248 Address RelocInfo::target_runtime_entry(Assembler* origin) {
249 DCHECK(IsRuntimeEntry(rmode_));
250 return target_address();
251 }
252
253
set_target_runtime_entry(Address target,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)254 void RelocInfo::set_target_runtime_entry(Address target,
255 WriteBarrierMode write_barrier_mode,
256 ICacheFlushMode icache_flush_mode) {
257 DCHECK(IsRuntimeEntry(rmode_));
258 if (target_address() != target)
259 set_target_address(target, write_barrier_mode, icache_flush_mode);
260 }
261
262
target_cell_handle()263 Handle<Cell> RelocInfo::target_cell_handle() {
264 DCHECK(rmode_ == RelocInfo::CELL);
265 Address address = Memory::Address_at(pc_);
266 return Handle<Cell>(reinterpret_cast<Cell**>(address));
267 }
268
269
target_cell()270 Cell* RelocInfo::target_cell() {
271 DCHECK(rmode_ == RelocInfo::CELL);
272 return Cell::FromValueAddress(Memory::Address_at(pc_));
273 }
274
275
set_target_cell(Cell * cell,WriteBarrierMode write_barrier_mode,ICacheFlushMode icache_flush_mode)276 void RelocInfo::set_target_cell(Cell* cell,
277 WriteBarrierMode write_barrier_mode,
278 ICacheFlushMode icache_flush_mode) {
279 DCHECK(rmode_ == RelocInfo::CELL);
280 Address address = cell->address() + Cell::kValueOffset;
281 Memory::Address_at(pc_) = address;
282 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
283 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(host(), this,
284 cell);
285 }
286 }
287
288
289 static const int kNoCodeAgeSequenceLength = 9 * Assembler::kInstrSize;
290
291
code_age_stub_handle(Assembler * origin)292 Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
293 UNREACHABLE(); // This should never be reached on Arm.
294 return Handle<Object>();
295 }
296
297
code_age_stub()298 Code* RelocInfo::code_age_stub() {
299 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
300 return Code::GetCodeFromTargetAddress(
301 Assembler::target_address_at(pc_ + Assembler::kInstrSize, host_));
302 }
303
304
set_code_age_stub(Code * stub,ICacheFlushMode icache_flush_mode)305 void RelocInfo::set_code_age_stub(Code* stub,
306 ICacheFlushMode icache_flush_mode) {
307 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
308 Assembler::set_target_address_at(isolate_, pc_ + Assembler::kInstrSize, host_,
309 stub->instruction_start());
310 }
311
312
debug_call_address()313 Address RelocInfo::debug_call_address() {
314 // The pc_ offset of 0 assumes patched debug break slot or return
315 // sequence.
316 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
317 return Assembler::target_address_at(pc_, host_);
318 }
319
320
set_debug_call_address(Address target)321 void RelocInfo::set_debug_call_address(Address target) {
322 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
323 // The pc_ offset of 0 assumes patched debug break slot or return
324 // sequence.
325 Assembler::set_target_address_at(isolate_, pc_, host_, target);
326 if (host() != NULL) {
327 Object* target_code = Code::GetCodeFromTargetAddress(target);
328 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
329 host(), this, HeapObject::cast(target_code));
330 }
331 }
332
333
WipeOut()334 void RelocInfo::WipeOut() {
335 DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
336 IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
337 IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
338 if (IsInternalReference(rmode_)) {
339 Memory::Address_at(pc_) = NULL;
340 } else if (IsInternalReferenceEncoded(rmode_)) {
341 Assembler::set_target_internal_reference_encoded_at(pc_, nullptr);
342 } else {
343 Assembler::set_target_address_at(isolate_, pc_, host_, NULL);
344 }
345 }
346
347 template <typename ObjectVisitor>
Visit(Isolate * isolate,ObjectVisitor * visitor)348 void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
349 RelocInfo::Mode mode = rmode();
350 if (mode == RelocInfo::EMBEDDED_OBJECT) {
351 visitor->VisitEmbeddedPointer(this);
352 } else if (RelocInfo::IsCodeTarget(mode)) {
353 visitor->VisitCodeTarget(this);
354 } else if (mode == RelocInfo::CELL) {
355 visitor->VisitCell(this);
356 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
357 visitor->VisitExternalReference(this);
358 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
359 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
360 visitor->VisitInternalReference(this);
361 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
362 visitor->VisitCodeAgeSequence(this);
363 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
364 IsPatchedDebugBreakSlotSequence()) {
365 visitor->VisitDebugTarget(this);
366 } else if (RelocInfo::IsRuntimeEntry(mode)) {
367 visitor->VisitRuntimeEntry(this);
368 }
369 }
370
371
372 template<typename StaticVisitor>
Visit(Heap * heap)373 void RelocInfo::Visit(Heap* heap) {
374 RelocInfo::Mode mode = rmode();
375 if (mode == RelocInfo::EMBEDDED_OBJECT) {
376 StaticVisitor::VisitEmbeddedPointer(heap, this);
377 } else if (RelocInfo::IsCodeTarget(mode)) {
378 StaticVisitor::VisitCodeTarget(heap, this);
379 } else if (mode == RelocInfo::CELL) {
380 StaticVisitor::VisitCell(heap, this);
381 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
382 StaticVisitor::VisitExternalReference(this);
383 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
384 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
385 StaticVisitor::VisitInternalReference(this);
386 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
387 StaticVisitor::VisitCodeAgeSequence(heap, this);
388 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
389 IsPatchedDebugBreakSlotSequence()) {
390 StaticVisitor::VisitDebugTarget(heap, this);
391 } else if (RelocInfo::IsRuntimeEntry(mode)) {
392 StaticVisitor::VisitRuntimeEntry(this);
393 }
394 }
395
396
397 // -----------------------------------------------------------------------------
398 // Assembler.
399
400
CheckBuffer()401 void Assembler::CheckBuffer() {
402 if (buffer_space() <= kGap) {
403 GrowBuffer();
404 }
405 }
406
407
CheckTrampolinePoolQuick(int extra_instructions)408 void Assembler::CheckTrampolinePoolQuick(int extra_instructions) {
409 if (pc_offset() >= next_buffer_check_ - extra_instructions * kInstrSize) {
410 CheckTrampolinePool();
411 }
412 }
413
414
CheckForEmitInForbiddenSlot()415 void Assembler::CheckForEmitInForbiddenSlot() {
416 if (!is_buffer_growth_blocked()) {
417 CheckBuffer();
418 }
419 if (IsPrevInstrCompactBranch()) {
420 // Nop instruction to preceed a CTI in forbidden slot:
421 Instr nop = SPECIAL | SLL;
422 *reinterpret_cast<Instr*>(pc_) = nop;
423 pc_ += kInstrSize;
424
425 ClearCompactBranchState();
426 }
427 }
428
429
EmitHelper(Instr x,CompactBranchType is_compact_branch)430 void Assembler::EmitHelper(Instr x, CompactBranchType is_compact_branch) {
431 if (IsPrevInstrCompactBranch()) {
432 if (Instruction::IsForbiddenAfterBranchInstr(x)) {
433 // Nop instruction to preceed a CTI in forbidden slot:
434 Instr nop = SPECIAL | SLL;
435 *reinterpret_cast<Instr*>(pc_) = nop;
436 pc_ += kInstrSize;
437 }
438 ClearCompactBranchState();
439 }
440 *reinterpret_cast<Instr*>(pc_) = x;
441 pc_ += kInstrSize;
442 if (is_compact_branch == CompactBranchType::COMPACT_BRANCH) {
443 EmittedCompactBranchInstruction();
444 }
445 CheckTrampolinePoolQuick();
446 }
447
448 template <>
449 inline void Assembler::EmitHelper(uint8_t x);
450
451 template <typename T>
EmitHelper(T x)452 void Assembler::EmitHelper(T x) {
453 *reinterpret_cast<T*>(pc_) = x;
454 pc_ += sizeof(x);
455 CheckTrampolinePoolQuick();
456 }
457
458 template <>
EmitHelper(uint8_t x)459 void Assembler::EmitHelper(uint8_t x) {
460 *reinterpret_cast<uint8_t*>(pc_) = x;
461 pc_ += sizeof(x);
462 if (reinterpret_cast<intptr_t>(pc_) % kInstrSize == 0) {
463 CheckTrampolinePoolQuick();
464 }
465 }
466
emit(Instr x,CompactBranchType is_compact_branch)467 void Assembler::emit(Instr x, CompactBranchType is_compact_branch) {
468 if (!is_buffer_growth_blocked()) {
469 CheckBuffer();
470 }
471 EmitHelper(x, is_compact_branch);
472 }
473
474
emit(uint64_t data)475 void Assembler::emit(uint64_t data) {
476 CheckForEmitInForbiddenSlot();
477 EmitHelper(data);
478 }
479
480
481 } // namespace internal
482 } // namespace v8
483
484 #endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_
485