blob: 82267edd0a8c0969a92fe18726775b91ad001e4a [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001
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"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000044
45
46namespace v8 {
47namespace internal {
48
49
50bool CpuFeatures::SupportsCrankshaft() { return IsSupported(FPU); }
51
52
53// -----------------------------------------------------------------------------
54// Operand and MemOperand.
55
56Operand::Operand(int64_t immediate, RelocInfo::Mode rmode) {
57 rm_ = no_reg;
58 imm64_ = immediate;
59 rmode_ = rmode;
60}
61
62
63Operand::Operand(const ExternalReference& f) {
64 rm_ = no_reg;
65 imm64_ = reinterpret_cast<int64_t>(f.address());
66 rmode_ = RelocInfo::EXTERNAL_REFERENCE;
67}
68
69
70Operand::Operand(Smi* value) {
71 rm_ = no_reg;
72 imm64_ = reinterpret_cast<intptr_t>(value);
73 rmode_ = RelocInfo::NONE32;
74}
75
76
77Operand::Operand(Register rm) {
78 rm_ = rm;
79}
80
81
82bool Operand::is_reg() const {
83 return rm_.is_valid();
84}
85
86
Ben Murdochb8a8cc12014-11-26 15:28:44 +000087// -----------------------------------------------------------------------------
88// RelocInfo.
89
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000090void RelocInfo::apply(intptr_t delta) {
91 if (IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092 // Absolute code pointer inside code object moves with the code object.
93 byte* p = reinterpret_cast<byte*>(pc_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094 int count = Assembler::RelocateInternalReference(rmode_, p, delta);
95 Assembler::FlushICache(isolate_, p, count * sizeof(uint32_t));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000096 }
97}
98
99
100Address RelocInfo::target_address() {
101 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
102 return Assembler::target_address_at(pc_, host_);
103}
104
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000105Address 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
131Address RelocInfo::constant_pool_entry_address() {
132 UNREACHABLE();
133 return NULL;
134}
135
136
137int RelocInfo::target_address_size() {
138 return Assembler::kSpecialTargetSize;
139}
140
141
142void RelocInfo::set_target_address(Address target,
143 WriteBarrierMode write_barrier_mode,
144 ICacheFlushMode icache_flush_mode) {
145 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000146 Assembler::set_target_address_at(isolate_, pc_, host_, target,
147 icache_flush_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156Address Assembler::target_address_from_return_address(Address pc) {
157 return pc - kCallTargetAddressOffset;
158}
159
160
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161void 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
179void 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 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188}
189
190
191Object* RelocInfo::target_object() {
192 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
193 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
194}
195
196
197Handle<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
204void RelocInfo::set_target_object(Object* target,
205 WriteBarrierMode write_barrier_mode,
206 ICacheFlushMode icache_flush_mode) {
207 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000208 Assembler::set_target_address_at(isolate_, pc_, host_,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000209 reinterpret_cast<Address>(target),
210 icache_flush_mode);
211 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
212 host() != NULL &&
213 target->IsHeapObject()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100214 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
215 host(), this, HeapObject::cast(target));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 }
217}
218
219
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000220Address RelocInfo::target_external_reference() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 DCHECK(rmode_ == EXTERNAL_REFERENCE);
222 return Assembler::target_address_at(pc_, host_);
223}
224
225
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000226Address 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
242Address RelocInfo::target_internal_reference_address() {
243 DCHECK(rmode_ == INTERNAL_REFERENCE || rmode_ == INTERNAL_REFERENCE_ENCODED);
244 return reinterpret_cast<Address>(pc_);
245}
246
247
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248Address RelocInfo::target_runtime_entry(Assembler* origin) {
249 DCHECK(IsRuntimeEntry(rmode_));
250 return target_address();
251}
252
253
254void 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
263Handle<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
270Cell* RelocInfo::target_cell() {
271 DCHECK(rmode_ == RelocInfo::CELL);
272 return Cell::FromValueAddress(Memory::Address_at(pc_));
273}
274
275
276void 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) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100283 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(host(), this,
284 cell);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 }
286}
287
288
289static const int kNoCodeAgeSequenceLength = 9 * Assembler::kInstrSize;
290
291
292Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
293 UNREACHABLE(); // This should never be reached on Arm.
294 return Handle<Object>();
295}
296
297
298Code* 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
305void RelocInfo::set_code_age_stub(Code* stub,
306 ICacheFlushMode icache_flush_mode) {
307 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000308 Assembler::set_target_address_at(isolate_, pc_ + Assembler::kInstrSize, host_,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000309 stub->instruction_start());
310}
311
312
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313Address RelocInfo::debug_call_address() {
314 // The pc_ offset of 0 assumes patched debug break slot or return
315 // sequence.
316 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 return Assembler::target_address_at(pc_, host_);
318}
319
320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000321void 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);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334void RelocInfo::WipeOut() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000335 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 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345}
346
Ben Murdochc5610432016-08-08 18:44:38 +0100347template <typename ObjectVisitor>
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348void 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000358 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
359 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
360 visitor->VisitInternalReference(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000361 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
362 visitor->VisitCodeAgeSequence(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000363 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
364 IsPatchedDebugBreakSlotSequence()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000365 visitor->VisitDebugTarget(this);
366 } else if (RelocInfo::IsRuntimeEntry(mode)) {
367 visitor->VisitRuntimeEntry(this);
368 }
369}
370
371
372template<typename StaticVisitor>
373void 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);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
384 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
385 StaticVisitor::VisitInternalReference(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
387 StaticVisitor::VisitCodeAgeSequence(heap, this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000388 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
389 IsPatchedDebugBreakSlotSequence()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390 StaticVisitor::VisitDebugTarget(heap, this);
391 } else if (RelocInfo::IsRuntimeEntry(mode)) {
392 StaticVisitor::VisitRuntimeEntry(this);
393 }
394}
395
396
397// -----------------------------------------------------------------------------
398// Assembler.
399
400
401void Assembler::CheckBuffer() {
402 if (buffer_space() <= kGap) {
403 GrowBuffer();
404 }
405}
406
407
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000408void Assembler::CheckTrampolinePoolQuick(int extra_instructions) {
409 if (pc_offset() >= next_buffer_check_ - extra_instructions * kInstrSize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000410 CheckTrampolinePool();
411 }
412}
413
414
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000415void Assembler::CheckForEmitInForbiddenSlot() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000416 if (!is_buffer_growth_blocked()) {
417 CheckBuffer();
418 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000419 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
430void 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 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000440 *reinterpret_cast<Instr*>(pc_) = x;
441 pc_ += kInstrSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000442 if (is_compact_branch == CompactBranchType::COMPACT_BRANCH) {
443 EmittedCompactBranchInstruction();
444 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445 CheckTrampolinePoolQuick();
446}
447
Ben Murdoch61f157c2016-09-16 13:49:30 +0100448template <>
449inline void Assembler::EmitHelper(uint8_t x);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000450
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000451template <typename T>
452void Assembler::EmitHelper(T x) {
453 *reinterpret_cast<T*>(pc_) = x;
454 pc_ += sizeof(x);
455 CheckTrampolinePoolQuick();
456}
457
Ben Murdoch61f157c2016-09-16 13:49:30 +0100458template <>
459void 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}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000466
467void Assembler::emit(Instr x, CompactBranchType is_compact_branch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 if (!is_buffer_growth_blocked()) {
469 CheckBuffer();
470 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000471 EmitHelper(x, is_compact_branch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472}
473
474
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000475void Assembler::emit(uint64_t data) {
476 CheckForEmitInForbiddenSlot();
477 EmitHelper(data);
478}
479
480
481} // namespace internal
482} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483
484#endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_