blob: 37ee3a680722b07b1874872e7e5461f5fe925fb5 [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
105
106Address 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 return reinterpret_cast<Address>(
128 pc_ + Assembler::kInstructionsFor64BitConstant * Assembler::kInstrSize);
129}
130
131
132Address RelocInfo::constant_pool_entry_address() {
133 UNREACHABLE();
134 return NULL;
135}
136
137
138int RelocInfo::target_address_size() {
139 return Assembler::kSpecialTargetSize;
140}
141
142
143void RelocInfo::set_target_address(Address target,
144 WriteBarrierMode write_barrier_mode,
145 ICacheFlushMode icache_flush_mode) {
146 DCHECK(IsCodeTarget(rmode_) || IsRuntimeEntry(rmode_));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147 Assembler::set_target_address_at(isolate_, pc_, host_, target,
148 icache_flush_mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000149 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
150 host() != NULL && IsCodeTarget(rmode_)) {
151 Object* target_code = Code::GetCodeFromTargetAddress(target);
152 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
153 host(), this, HeapObject::cast(target_code));
154 }
155}
156
157
158Address Assembler::target_address_from_return_address(Address pc) {
159 return pc - kCallTargetAddressOffset;
160}
161
162
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000163void Assembler::set_target_internal_reference_encoded_at(Address pc,
164 Address target) {
165 // Encoded internal references are j/jal instructions.
166 Instr instr = Assembler::instr_at(pc + 0 * Assembler::kInstrSize);
167
168 uint64_t imm28 =
169 (reinterpret_cast<uint64_t>(target) & static_cast<uint64_t>(kImm28Mask));
170
171 instr &= ~kImm26Mask;
172 uint64_t imm26 = imm28 >> 2;
173 DCHECK(is_uint26(imm26));
174
175 instr_at_put(pc, instr | (imm26 & kImm26Mask));
176 // Currently used only by deserializer, and all code will be flushed
177 // after complete deserialization, no need to flush on each reference.
178}
179
180
181void Assembler::deserialization_set_target_internal_reference_at(
182 Isolate* isolate, Address pc, Address target, RelocInfo::Mode mode) {
183 if (mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
184 DCHECK(IsJ(instr_at(pc)));
185 set_target_internal_reference_encoded_at(pc, target);
186 } else {
187 DCHECK(mode == RelocInfo::INTERNAL_REFERENCE);
188 Memory::Address_at(pc) = target;
189 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000190}
191
192
193Object* RelocInfo::target_object() {
194 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
195 return reinterpret_cast<Object*>(Assembler::target_address_at(pc_, host_));
196}
197
198
199Handle<Object> RelocInfo::target_object_handle(Assembler* origin) {
200 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
201 return Handle<Object>(reinterpret_cast<Object**>(
202 Assembler::target_address_at(pc_, host_)));
203}
204
205
206void RelocInfo::set_target_object(Object* target,
207 WriteBarrierMode write_barrier_mode,
208 ICacheFlushMode icache_flush_mode) {
209 DCHECK(IsCodeTarget(rmode_) || rmode_ == EMBEDDED_OBJECT);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000210 Assembler::set_target_address_at(isolate_, pc_, host_,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000211 reinterpret_cast<Address>(target),
212 icache_flush_mode);
213 if (write_barrier_mode == UPDATE_WRITE_BARRIER &&
214 host() != NULL &&
215 target->IsHeapObject()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100216 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
217 host(), this, HeapObject::cast(target));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 }
219}
220
221
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000222Address RelocInfo::target_external_reference() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000223 DCHECK(rmode_ == EXTERNAL_REFERENCE);
224 return Assembler::target_address_at(pc_, host_);
225}
226
227
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000228Address RelocInfo::target_internal_reference() {
229 if (rmode_ == INTERNAL_REFERENCE) {
230 return Memory::Address_at(pc_);
231 } else {
232 // Encoded internal references are j/jal instructions.
233 DCHECK(rmode_ == INTERNAL_REFERENCE_ENCODED);
234 Instr instr = Assembler::instr_at(pc_ + 0 * Assembler::kInstrSize);
235 instr &= kImm26Mask;
236 uint64_t imm28 = instr << 2;
237 uint64_t segment =
238 (reinterpret_cast<uint64_t>(pc_) & ~static_cast<uint64_t>(kImm28Mask));
239 return reinterpret_cast<Address>(segment | imm28);
240 }
241}
242
243
244Address RelocInfo::target_internal_reference_address() {
245 DCHECK(rmode_ == INTERNAL_REFERENCE || rmode_ == INTERNAL_REFERENCE_ENCODED);
246 return reinterpret_cast<Address>(pc_);
247}
248
249
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250Address RelocInfo::target_runtime_entry(Assembler* origin) {
251 DCHECK(IsRuntimeEntry(rmode_));
252 return target_address();
253}
254
255
256void RelocInfo::set_target_runtime_entry(Address target,
257 WriteBarrierMode write_barrier_mode,
258 ICacheFlushMode icache_flush_mode) {
259 DCHECK(IsRuntimeEntry(rmode_));
260 if (target_address() != target)
261 set_target_address(target, write_barrier_mode, icache_flush_mode);
262}
263
264
265Handle<Cell> RelocInfo::target_cell_handle() {
266 DCHECK(rmode_ == RelocInfo::CELL);
267 Address address = Memory::Address_at(pc_);
268 return Handle<Cell>(reinterpret_cast<Cell**>(address));
269}
270
271
272Cell* RelocInfo::target_cell() {
273 DCHECK(rmode_ == RelocInfo::CELL);
274 return Cell::FromValueAddress(Memory::Address_at(pc_));
275}
276
277
278void RelocInfo::set_target_cell(Cell* cell,
279 WriteBarrierMode write_barrier_mode,
280 ICacheFlushMode icache_flush_mode) {
281 DCHECK(rmode_ == RelocInfo::CELL);
282 Address address = cell->address() + Cell::kValueOffset;
283 Memory::Address_at(pc_) = address;
284 if (write_barrier_mode == UPDATE_WRITE_BARRIER && host() != NULL) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100285 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(host(), this,
286 cell);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 }
288}
289
290
291static const int kNoCodeAgeSequenceLength = 9 * Assembler::kInstrSize;
292
293
294Handle<Object> RelocInfo::code_age_stub_handle(Assembler* origin) {
295 UNREACHABLE(); // This should never be reached on Arm.
296 return Handle<Object>();
297}
298
299
300Code* RelocInfo::code_age_stub() {
301 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
302 return Code::GetCodeFromTargetAddress(
303 Assembler::target_address_at(pc_ + Assembler::kInstrSize, host_));
304}
305
306
307void RelocInfo::set_code_age_stub(Code* stub,
308 ICacheFlushMode icache_flush_mode) {
309 DCHECK(rmode_ == RelocInfo::CODE_AGE_SEQUENCE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000310 Assembler::set_target_address_at(isolate_, pc_ + Assembler::kInstrSize, host_,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000311 stub->instruction_start());
312}
313
314
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315Address RelocInfo::debug_call_address() {
316 // The pc_ offset of 0 assumes patched debug break slot or return
317 // sequence.
318 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319 return Assembler::target_address_at(pc_, host_);
320}
321
322
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000323void RelocInfo::set_debug_call_address(Address target) {
324 DCHECK(IsDebugBreakSlot(rmode()) && IsPatchedDebugBreakSlotSequence());
325 // The pc_ offset of 0 assumes patched debug break slot or return
326 // sequence.
327 Assembler::set_target_address_at(isolate_, pc_, host_, target);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 if (host() != NULL) {
329 Object* target_code = Code::GetCodeFromTargetAddress(target);
330 host()->GetHeap()->incremental_marking()->RecordWriteIntoCode(
331 host(), this, HeapObject::cast(target_code));
332 }
333}
334
335
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336void RelocInfo::WipeOut() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000337 DCHECK(IsEmbeddedObject(rmode_) || IsCodeTarget(rmode_) ||
338 IsRuntimeEntry(rmode_) || IsExternalReference(rmode_) ||
339 IsInternalReference(rmode_) || IsInternalReferenceEncoded(rmode_));
340 if (IsInternalReference(rmode_)) {
341 Memory::Address_at(pc_) = NULL;
342 } else if (IsInternalReferenceEncoded(rmode_)) {
343 Assembler::set_target_internal_reference_encoded_at(pc_, nullptr);
344 } else {
345 Assembler::set_target_address_at(isolate_, pc_, host_, NULL);
346 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000347}
348
349
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000350void RelocInfo::Visit(Isolate* isolate, ObjectVisitor* visitor) {
351 RelocInfo::Mode mode = rmode();
352 if (mode == RelocInfo::EMBEDDED_OBJECT) {
353 visitor->VisitEmbeddedPointer(this);
354 } else if (RelocInfo::IsCodeTarget(mode)) {
355 visitor->VisitCodeTarget(this);
356 } else if (mode == RelocInfo::CELL) {
357 visitor->VisitCell(this);
358 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
359 visitor->VisitExternalReference(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000360 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
361 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
362 visitor->VisitInternalReference(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
364 visitor->VisitCodeAgeSequence(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000365 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
366 IsPatchedDebugBreakSlotSequence()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367 visitor->VisitDebugTarget(this);
368 } else if (RelocInfo::IsRuntimeEntry(mode)) {
369 visitor->VisitRuntimeEntry(this);
370 }
371}
372
373
374template<typename StaticVisitor>
375void RelocInfo::Visit(Heap* heap) {
376 RelocInfo::Mode mode = rmode();
377 if (mode == RelocInfo::EMBEDDED_OBJECT) {
378 StaticVisitor::VisitEmbeddedPointer(heap, this);
379 } else if (RelocInfo::IsCodeTarget(mode)) {
380 StaticVisitor::VisitCodeTarget(heap, this);
381 } else if (mode == RelocInfo::CELL) {
382 StaticVisitor::VisitCell(heap, this);
383 } else if (mode == RelocInfo::EXTERNAL_REFERENCE) {
384 StaticVisitor::VisitExternalReference(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000385 } else if (mode == RelocInfo::INTERNAL_REFERENCE ||
386 mode == RelocInfo::INTERNAL_REFERENCE_ENCODED) {
387 StaticVisitor::VisitInternalReference(this);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000388 } else if (RelocInfo::IsCodeAgeSequence(mode)) {
389 StaticVisitor::VisitCodeAgeSequence(heap, this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000390 } else if (RelocInfo::IsDebugBreakSlot(mode) &&
391 IsPatchedDebugBreakSlotSequence()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000392 StaticVisitor::VisitDebugTarget(heap, this);
393 } else if (RelocInfo::IsRuntimeEntry(mode)) {
394 StaticVisitor::VisitRuntimeEntry(this);
395 }
396}
397
398
399// -----------------------------------------------------------------------------
400// Assembler.
401
402
403void Assembler::CheckBuffer() {
404 if (buffer_space() <= kGap) {
405 GrowBuffer();
406 }
407}
408
409
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000410void Assembler::CheckTrampolinePoolQuick(int extra_instructions) {
411 if (pc_offset() >= next_buffer_check_ - extra_instructions * kInstrSize) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 CheckTrampolinePool();
413 }
414}
415
416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000417void Assembler::CheckForEmitInForbiddenSlot() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000418 if (!is_buffer_growth_blocked()) {
419 CheckBuffer();
420 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000421 if (IsPrevInstrCompactBranch()) {
422 // Nop instruction to preceed a CTI in forbidden slot:
423 Instr nop = SPECIAL | SLL;
424 *reinterpret_cast<Instr*>(pc_) = nop;
425 pc_ += kInstrSize;
426
427 ClearCompactBranchState();
428 }
429}
430
431
432void Assembler::EmitHelper(Instr x, CompactBranchType is_compact_branch) {
433 if (IsPrevInstrCompactBranch()) {
434 if (Instruction::IsForbiddenAfterBranchInstr(x)) {
435 // Nop instruction to preceed a CTI in forbidden slot:
436 Instr nop = SPECIAL | SLL;
437 *reinterpret_cast<Instr*>(pc_) = nop;
438 pc_ += kInstrSize;
439 }
440 ClearCompactBranchState();
441 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 *reinterpret_cast<Instr*>(pc_) = x;
443 pc_ += kInstrSize;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000444 if (is_compact_branch == CompactBranchType::COMPACT_BRANCH) {
445 EmittedCompactBranchInstruction();
446 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000447 CheckTrampolinePoolQuick();
448}
449
450
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
458
459void Assembler::emit(Instr x, CompactBranchType is_compact_branch) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000460 if (!is_buffer_growth_blocked()) {
461 CheckBuffer();
462 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 EmitHelper(x, is_compact_branch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464}
465
466
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000467void Assembler::emit(uint64_t data) {
468 CheckForEmitInForbiddenSlot();
469 EmitHelper(data);
470}
471
472
473} // namespace internal
474} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000475
476#endif // V8_MIPS_ASSEMBLER_MIPS_INL_H_