blob: 2471d5eebdb807ff3c421b191a57892c424b0c43 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2//
3// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are
5// met:
6//
7// * Redistributions of source code must retain the above copyright
8// notice, this list of conditions and the following disclaimer.
9// * Redistributions in binary form must reproduce the above
10// copyright notice, this list of conditions and the following
11// disclaimer in the documentation and/or other materials provided
12// with the distribution.
13// * Neither the name of Google Inc. nor the names of its
14// contributors may be used to endorse or promote products derived
15// from this software without specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029#if V8_TARGET_ARCH_ARM64
30
31#define ARM64_DEFINE_REG_STATICS
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032#include "src/arm64/assembler-arm64.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000033
34#include "src/arm64/assembler-arm64-inl.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035#include "src/arm64/frames-arm64.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036#include "src/base/bits.h"
37#include "src/base/cpu.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038#include "src/register-configuration.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039
40namespace v8 {
41namespace internal {
42
43
44// -----------------------------------------------------------------------------
45// CpuFeatures implementation.
46
47void CpuFeatures::ProbeImpl(bool cross_compile) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -040048 // AArch64 has no configuration options, no further probing is required.
49 supported_ = 0;
50
51 // Only use statically determined features for cross compile (snapshot).
52 if (cross_compile) return;
53
54 // Probe for runtime features
55 base::CPU cpu;
56 if (cpu.implementer() == base::CPU::NVIDIA &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000057 cpu.variant() == base::CPU::NVIDIA_DENVER &&
58 cpu.part() <= base::CPU::NVIDIA_DENVER_V10) {
Ben Murdochda12d292016-06-02 14:46:10 +010059 // TODO(jkummerow): This is turned off as an experiment to see if it
60 // affects crash rates. Keep an eye on crash reports and either remove
61 // coherent cache support permanently, or re-enable it!
62 // supported_ |= 1u << COHERENT_CACHE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 }
64}
65
66
67void CpuFeatures::PrintTarget() { }
Emily Bernierd0a1eb72015-03-24 16:35:39 -040068
69
70void CpuFeatures::PrintFeatures() {
71 printf("COHERENT_CACHE=%d\n", CpuFeatures::IsSupported(COHERENT_CACHE));
72}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073
74
75// -----------------------------------------------------------------------------
76// CPURegList utilities.
77
78CPURegister CPURegList::PopLowestIndex() {
79 DCHECK(IsValid());
80 if (IsEmpty()) {
81 return NoCPUReg;
82 }
83 int index = CountTrailingZeros(list_, kRegListSizeInBits);
84 DCHECK((1 << index) & list_);
85 Remove(index);
86 return CPURegister::Create(index, size_, type_);
87}
88
89
90CPURegister CPURegList::PopHighestIndex() {
91 DCHECK(IsValid());
92 if (IsEmpty()) {
93 return NoCPUReg;
94 }
95 int index = CountLeadingZeros(list_, kRegListSizeInBits);
96 index = kRegListSizeInBits - 1 - index;
97 DCHECK((1 << index) & list_);
98 Remove(index);
99 return CPURegister::Create(index, size_, type_);
100}
101
102
103void CPURegList::RemoveCalleeSaved() {
104 if (type() == CPURegister::kRegister) {
105 Remove(GetCalleeSaved(RegisterSizeInBits()));
106 } else if (type() == CPURegister::kFPRegister) {
107 Remove(GetCalleeSavedFP(RegisterSizeInBits()));
108 } else {
109 DCHECK(type() == CPURegister::kNoRegister);
110 DCHECK(IsEmpty());
111 // The list must already be empty, so do nothing.
112 }
113}
114
115
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116CPURegList CPURegList::GetCalleeSaved(int size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000117 return CPURegList(CPURegister::kRegister, size, 19, 29);
118}
119
120
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000121CPURegList CPURegList::GetCalleeSavedFP(int size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000122 return CPURegList(CPURegister::kFPRegister, size, 8, 15);
123}
124
125
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000126CPURegList CPURegList::GetCallerSaved(int size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127 // Registers x0-x18 and lr (x30) are caller-saved.
128 CPURegList list = CPURegList(CPURegister::kRegister, size, 0, 18);
129 list.Combine(lr);
130 return list;
131}
132
133
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000134CPURegList CPURegList::GetCallerSavedFP(int size) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 // Registers d0-d7 and d16-d31 are caller-saved.
136 CPURegList list = CPURegList(CPURegister::kFPRegister, size, 0, 7);
137 list.Combine(CPURegList(CPURegister::kFPRegister, size, 16, 31));
138 return list;
139}
140
141
142// This function defines the list of registers which are associated with a
143// safepoint slot. Safepoint register slots are saved contiguously on the stack.
144// MacroAssembler::SafepointRegisterStackIndex handles mapping from register
145// code to index in the safepoint register slots. Any change here can affect
146// this mapping.
147CPURegList CPURegList::GetSafepointSavedRegisters() {
148 CPURegList list = CPURegList::GetCalleeSaved();
149 list.Combine(
150 CPURegList(CPURegister::kRegister, kXRegSizeInBits, kJSCallerSaved));
151
152 // Note that unfortunately we can't use symbolic names for registers and have
153 // to directly use register codes. This is because this function is used to
154 // initialize some static variables and we can't rely on register variables
155 // to be initialized due to static initialization order issues in C++.
156
157 // Drop ip0 and ip1 (i.e. x16 and x17), as they should not be expected to be
158 // preserved outside of the macro assembler.
159 list.Remove(16);
160 list.Remove(17);
161
162 // Add x18 to the safepoint list, as although it's not in kJSCallerSaved, it
163 // is a caller-saved register according to the procedure call standard.
164 list.Combine(18);
165
166 // Drop jssp as the stack pointer doesn't need to be included.
167 list.Remove(28);
168
169 // Add the link register (x30) to the safepoint list.
170 list.Combine(30);
171
172 return list;
173}
174
175
176// -----------------------------------------------------------------------------
177// Implementation of RelocInfo
178
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000179const int RelocInfo::kApplyMask = 1 << RelocInfo::INTERNAL_REFERENCE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180
181
182bool RelocInfo::IsCodedSpecially() {
183 // The deserializer needs to know whether a pointer is specially coded. Being
184 // specially coded on ARM64 means that it is a movz/movk sequence. We don't
185 // generate those for relocatable pointers.
186 return false;
187}
188
189
190bool RelocInfo::IsInConstantPool() {
191 Instruction* instr = reinterpret_cast<Instruction*>(pc_);
192 return instr->IsLdrLiteralX();
193}
194
195
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000196Register GetAllocatableRegisterThatIsNotOneOf(Register reg1, Register reg2,
197 Register reg3, Register reg4) {
198 CPURegList regs(reg1, reg2, reg3, reg4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000199 const RegisterConfiguration* config =
200 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT);
201 for (int i = 0; i < config->num_allocatable_double_registers(); ++i) {
202 int code = config->GetAllocatableDoubleCode(i);
203 Register candidate = Register::from_code(code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000204 if (regs.IncludesAliasOf(candidate)) continue;
205 return candidate;
206 }
207 UNREACHABLE();
208 return NoReg;
209}
210
211
212bool AreAliased(const CPURegister& reg1, const CPURegister& reg2,
213 const CPURegister& reg3, const CPURegister& reg4,
214 const CPURegister& reg5, const CPURegister& reg6,
215 const CPURegister& reg7, const CPURegister& reg8) {
216 int number_of_valid_regs = 0;
217 int number_of_valid_fpregs = 0;
218
219 RegList unique_regs = 0;
220 RegList unique_fpregs = 0;
221
222 const CPURegister regs[] = {reg1, reg2, reg3, reg4, reg5, reg6, reg7, reg8};
223
224 for (unsigned i = 0; i < arraysize(regs); i++) {
225 if (regs[i].IsRegister()) {
226 number_of_valid_regs++;
227 unique_regs |= regs[i].Bit();
228 } else if (regs[i].IsFPRegister()) {
229 number_of_valid_fpregs++;
230 unique_fpregs |= regs[i].Bit();
231 } else {
232 DCHECK(!regs[i].IsValid());
233 }
234 }
235
236 int number_of_unique_regs =
237 CountSetBits(unique_regs, sizeof(unique_regs) * kBitsPerByte);
238 int number_of_unique_fpregs =
239 CountSetBits(unique_fpregs, sizeof(unique_fpregs) * kBitsPerByte);
240
241 DCHECK(number_of_valid_regs >= number_of_unique_regs);
242 DCHECK(number_of_valid_fpregs >= number_of_unique_fpregs);
243
244 return (number_of_valid_regs != number_of_unique_regs) ||
245 (number_of_valid_fpregs != number_of_unique_fpregs);
246}
247
248
249bool AreSameSizeAndType(const CPURegister& reg1, const CPURegister& reg2,
250 const CPURegister& reg3, const CPURegister& reg4,
251 const CPURegister& reg5, const CPURegister& reg6,
252 const CPURegister& reg7, const CPURegister& reg8) {
253 DCHECK(reg1.IsValid());
254 bool match = true;
255 match &= !reg2.IsValid() || reg2.IsSameSizeAndType(reg1);
256 match &= !reg3.IsValid() || reg3.IsSameSizeAndType(reg1);
257 match &= !reg4.IsValid() || reg4.IsSameSizeAndType(reg1);
258 match &= !reg5.IsValid() || reg5.IsSameSizeAndType(reg1);
259 match &= !reg6.IsValid() || reg6.IsSameSizeAndType(reg1);
260 match &= !reg7.IsValid() || reg7.IsSameSizeAndType(reg1);
261 match &= !reg8.IsValid() || reg8.IsSameSizeAndType(reg1);
262 return match;
263}
264
265
266void Immediate::InitializeHandle(Handle<Object> handle) {
267 AllowDeferredHandleDereference using_raw_address;
268
269 // Verify all Objects referred by code are NOT in new space.
270 Object* obj = *handle;
271 if (obj->IsHeapObject()) {
272 DCHECK(!HeapObject::cast(obj)->GetHeap()->InNewSpace(obj));
273 value_ = reinterpret_cast<intptr_t>(handle.location());
274 rmode_ = RelocInfo::EMBEDDED_OBJECT;
275 } else {
276 STATIC_ASSERT(sizeof(intptr_t) == sizeof(int64_t));
277 value_ = reinterpret_cast<intptr_t>(obj);
278 rmode_ = RelocInfo::NONE64;
279 }
280}
281
282
283bool Operand::NeedsRelocation(const Assembler* assembler) const {
284 RelocInfo::Mode rmode = immediate_.rmode();
285
286 if (rmode == RelocInfo::EXTERNAL_REFERENCE) {
287 return assembler->serializer_enabled();
288 }
289
290 return !RelocInfo::IsNone(rmode);
291}
292
293
294// Constant Pool.
295void ConstPool::RecordEntry(intptr_t data,
296 RelocInfo::Mode mode) {
297 DCHECK(mode != RelocInfo::COMMENT &&
298 mode != RelocInfo::POSITION &&
299 mode != RelocInfo::STATEMENT_POSITION &&
300 mode != RelocInfo::CONST_POOL &&
301 mode != RelocInfo::VENEER_POOL &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000302 mode != RelocInfo::CODE_AGE_SEQUENCE &&
303 mode != RelocInfo::DEOPT_REASON);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000304 uint64_t raw_data = static_cast<uint64_t>(data);
305 int offset = assm_->pc_offset();
306 if (IsEmpty()) {
307 first_use_ = offset;
308 }
309
310 std::pair<uint64_t, int> entry = std::make_pair(raw_data, offset);
311 if (CanBeShared(mode)) {
312 shared_entries_.insert(entry);
313 if (shared_entries_.count(entry.first) == 1) {
314 shared_entries_count++;
315 }
316 } else {
317 unique_entries_.push_back(entry);
318 }
319
320 if (EntryCount() > Assembler::kApproxMaxPoolEntryCount) {
321 // Request constant pool emission after the next instruction.
322 assm_->SetNextConstPoolCheckIn(1);
323 }
324}
325
326
327int ConstPool::DistanceToFirstUse() {
328 DCHECK(first_use_ >= 0);
329 return assm_->pc_offset() - first_use_;
330}
331
332
333int ConstPool::MaxPcOffset() {
334 // There are no pending entries in the pool so we can never get out of
335 // range.
336 if (IsEmpty()) return kMaxInt;
337
338 // Entries are not necessarily emitted in the order they are added so in the
339 // worst case the first constant pool use will be accessing the last entry.
340 return first_use_ + kMaxLoadLiteralRange - WorstCaseSize();
341}
342
343
344int ConstPool::WorstCaseSize() {
345 if (IsEmpty()) return 0;
346
347 // Max size prologue:
348 // b over
349 // ldr xzr, #pool_size
350 // blr xzr
351 // nop
352 // All entries are 64-bit for now.
353 return 4 * kInstructionSize + EntryCount() * kPointerSize;
354}
355
356
357int ConstPool::SizeIfEmittedAtCurrentPc(bool require_jump) {
358 if (IsEmpty()) return 0;
359
360 // Prologue is:
361 // b over ;; if require_jump
362 // ldr xzr, #pool_size
363 // blr xzr
364 // nop ;; if not 64-bit aligned
365 int prologue_size = require_jump ? kInstructionSize : 0;
366 prologue_size += 2 * kInstructionSize;
367 prologue_size += IsAligned(assm_->pc_offset() + prologue_size, 8) ?
368 0 : kInstructionSize;
369
370 // All entries are 64-bit for now.
371 return prologue_size + EntryCount() * kPointerSize;
372}
373
374
375void ConstPool::Emit(bool require_jump) {
376 DCHECK(!assm_->is_const_pool_blocked());
377 // Prevent recursive pool emission and protect from veneer pools.
378 Assembler::BlockPoolsScope block_pools(assm_);
379
380 int size = SizeIfEmittedAtCurrentPc(require_jump);
381 Label size_check;
382 assm_->bind(&size_check);
383
384 assm_->RecordConstPool(size);
385 // Emit the constant pool. It is preceded by an optional branch if
386 // require_jump and a header which will:
387 // 1) Encode the size of the constant pool, for use by the disassembler.
388 // 2) Terminate the program, to try to prevent execution from accidentally
389 // flowing into the constant pool.
390 // 3) align the pool entries to 64-bit.
391 // The header is therefore made of up to three arm64 instructions:
392 // ldr xzr, #<size of the constant pool in 32-bit words>
393 // blr xzr
394 // nop
395 //
396 // If executed, the header will likely segfault and lr will point to the
397 // instruction following the offending blr.
398 // TODO(all): Make the alignment part less fragile. Currently code is
399 // allocated as a byte array so there are no guarantees the alignment will
400 // be preserved on compaction. Currently it works as allocation seems to be
401 // 64-bit aligned.
402
403 // Emit branch if required
404 Label after_pool;
405 if (require_jump) {
406 assm_->b(&after_pool);
407 }
408
409 // Emit the header.
410 assm_->RecordComment("[ Constant Pool");
411 EmitMarker();
412 EmitGuard();
413 assm_->Align(8);
414
415 // Emit constant pool entries.
416 // TODO(all): currently each relocated constant is 64 bits, consider adding
417 // support for 32-bit entries.
418 EmitEntries();
419 assm_->RecordComment("]");
420
421 if (after_pool.is_linked()) {
422 assm_->bind(&after_pool);
423 }
424
425 DCHECK(assm_->SizeOfCodeGeneratedSince(&size_check) ==
426 static_cast<unsigned>(size));
427}
428
429
430void ConstPool::Clear() {
431 shared_entries_.clear();
432 shared_entries_count = 0;
433 unique_entries_.clear();
434 first_use_ = -1;
435}
436
437
438bool ConstPool::CanBeShared(RelocInfo::Mode mode) {
439 // Constant pool currently does not support 32-bit entries.
440 DCHECK(mode != RelocInfo::NONE32);
441
442 return RelocInfo::IsNone(mode) ||
Ben Murdochda12d292016-06-02 14:46:10 +0100443 (!assm_->serializer_enabled() &&
444 (mode >= RelocInfo::FIRST_SHAREABLE_RELOC_MODE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445}
446
447
448void ConstPool::EmitMarker() {
449 // A constant pool size is expressed in number of 32-bits words.
450 // Currently all entries are 64-bit.
451 // + 1 is for the crash guard.
452 // + 0/1 for alignment.
453 int word_count = EntryCount() * 2 + 1 +
454 (IsAligned(assm_->pc_offset(), 8) ? 0 : 1);
455 assm_->Emit(LDR_x_lit |
456 Assembler::ImmLLiteral(word_count) |
457 Assembler::Rt(xzr));
458}
459
460
461MemOperand::PairResult MemOperand::AreConsistentForPair(
462 const MemOperand& operandA,
463 const MemOperand& operandB,
464 int access_size_log2) {
465 DCHECK(access_size_log2 >= 0);
466 DCHECK(access_size_log2 <= 3);
467 // Step one: check that they share the same base, that the mode is Offset
468 // and that the offset is a multiple of access size.
469 if (!operandA.base().Is(operandB.base()) ||
470 (operandA.addrmode() != Offset) ||
471 (operandB.addrmode() != Offset) ||
472 ((operandA.offset() & ((1 << access_size_log2) - 1)) != 0)) {
473 return kNotPair;
474 }
475 // Step two: check that the offsets are contiguous and that the range
476 // is OK for ldp/stp.
477 if ((operandB.offset() == operandA.offset() + (1 << access_size_log2)) &&
478 is_int7(operandA.offset() >> access_size_log2)) {
479 return kPairAB;
480 }
481 if ((operandA.offset() == operandB.offset() + (1 << access_size_log2)) &&
482 is_int7(operandB.offset() >> access_size_log2)) {
483 return kPairBA;
484 }
485 return kNotPair;
486}
487
488
489void ConstPool::EmitGuard() {
490#ifdef DEBUG
491 Instruction* instr = reinterpret_cast<Instruction*>(assm_->pc());
492 DCHECK(instr->preceding()->IsLdrLiteralX() &&
493 instr->preceding()->Rt() == xzr.code());
494#endif
495 assm_->EmitPoolGuard();
496}
497
498
499void ConstPool::EmitEntries() {
500 DCHECK(IsAligned(assm_->pc_offset(), 8));
501
502 typedef std::multimap<uint64_t, int>::const_iterator SharedEntriesIterator;
503 SharedEntriesIterator value_it;
504 // Iterate through the keys (constant pool values).
505 for (value_it = shared_entries_.begin();
506 value_it != shared_entries_.end();
507 value_it = shared_entries_.upper_bound(value_it->first)) {
508 std::pair<SharedEntriesIterator, SharedEntriesIterator> range;
509 uint64_t data = value_it->first;
510 range = shared_entries_.equal_range(data);
511 SharedEntriesIterator offset_it;
512 // Iterate through the offsets of a given key.
513 for (offset_it = range.first; offset_it != range.second; offset_it++) {
514 Instruction* instr = assm_->InstructionAt(offset_it->second);
515
516 // Instruction to patch must be 'ldr rd, [pc, #offset]' with offset == 0.
517 DCHECK(instr->IsLdrLiteral() && instr->ImmLLiteral() == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000518 instr->SetImmPCOffsetTarget(assm_->isolate(), assm_->pc());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519 }
520 assm_->dc64(data);
521 }
522 shared_entries_.clear();
523 shared_entries_count = 0;
524
525 // Emit unique entries.
526 std::vector<std::pair<uint64_t, int> >::const_iterator unique_it;
527 for (unique_it = unique_entries_.begin();
528 unique_it != unique_entries_.end();
529 unique_it++) {
530 Instruction* instr = assm_->InstructionAt(unique_it->second);
531
532 // Instruction to patch must be 'ldr rd, [pc, #offset]' with offset == 0.
533 DCHECK(instr->IsLdrLiteral() && instr->ImmLLiteral() == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000534 instr->SetImmPCOffsetTarget(assm_->isolate(), assm_->pc());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000535 assm_->dc64(unique_it->first);
536 }
537 unique_entries_.clear();
538 first_use_ = -1;
539}
540
541
542// Assembler
543Assembler::Assembler(Isolate* isolate, void* buffer, int buffer_size)
544 : AssemblerBase(isolate, buffer, buffer_size),
545 constpool_(this),
546 recorded_ast_id_(TypeFeedbackId::None()),
547 unresolved_branches_(),
548 positions_recorder_(this) {
549 const_pool_blocked_nesting_ = 0;
550 veneer_pool_blocked_nesting_ = 0;
551 Reset();
552}
553
554
555Assembler::~Assembler() {
556 DCHECK(constpool_.IsEmpty());
557 DCHECK(const_pool_blocked_nesting_ == 0);
558 DCHECK(veneer_pool_blocked_nesting_ == 0);
559}
560
561
562void Assembler::Reset() {
563#ifdef DEBUG
564 DCHECK((pc_ >= buffer_) && (pc_ < buffer_ + buffer_size_));
565 DCHECK(const_pool_blocked_nesting_ == 0);
566 DCHECK(veneer_pool_blocked_nesting_ == 0);
567 DCHECK(unresolved_branches_.empty());
568 memset(buffer_, 0, pc_ - buffer_);
569#endif
570 pc_ = buffer_;
571 reloc_info_writer.Reposition(reinterpret_cast<byte*>(buffer_ + buffer_size_),
572 reinterpret_cast<byte*>(pc_));
573 constpool_.Clear();
574 next_constant_pool_check_ = 0;
575 next_veneer_pool_check_ = kMaxInt;
576 no_const_pool_before_ = 0;
577 ClearRecordedAstId();
578}
579
580
581void Assembler::GetCode(CodeDesc* desc) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000582 reloc_info_writer.Finish();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000583 // Emit constant pool if necessary.
584 CheckConstPool(true, false);
585 DCHECK(constpool_.IsEmpty());
586
587 // Set up code descriptor.
588 if (desc) {
589 desc->buffer = reinterpret_cast<byte*>(buffer_);
590 desc->buffer_size = buffer_size_;
591 desc->instr_size = pc_offset();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000592 desc->reloc_size =
593 static_cast<int>((reinterpret_cast<byte*>(buffer_) + buffer_size_) -
594 reloc_info_writer.pos());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000595 desc->origin = this;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000596 desc->constant_pool_size = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000597 }
598}
599
600
601void Assembler::Align(int m) {
602 DCHECK(m >= 4 && base::bits::IsPowerOfTwo32(m));
603 while ((pc_offset() & (m - 1)) != 0) {
604 nop();
605 }
606}
607
608
609void Assembler::CheckLabelLinkChain(Label const * label) {
610#ifdef DEBUG
611 if (label->is_linked()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400612 static const int kMaxLinksToCheck = 64; // Avoid O(n2) behaviour.
613 int links_checked = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000614 int64_t linkoffset = label->pos();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000615 bool end_of_chain = false;
616 while (!end_of_chain) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400617 if (++links_checked > kMaxLinksToCheck) break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618 Instruction * link = InstructionAt(linkoffset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000619 int64_t linkpcoffset = link->ImmPCOffset();
620 int64_t prevlinkoffset = linkoffset + linkpcoffset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000621
622 end_of_chain = (linkoffset == prevlinkoffset);
623 linkoffset = linkoffset + linkpcoffset;
624 }
625 }
626#endif
627}
628
629
630void Assembler::RemoveBranchFromLabelLinkChain(Instruction* branch,
631 Label* label,
632 Instruction* label_veneer) {
633 DCHECK(label->is_linked());
634
635 CheckLabelLinkChain(label);
636
637 Instruction* link = InstructionAt(label->pos());
638 Instruction* prev_link = link;
639 Instruction* next_link;
640 bool end_of_chain = false;
641
642 while (link != branch && !end_of_chain) {
643 next_link = link->ImmPCOffsetTarget();
644 end_of_chain = (link == next_link);
645 prev_link = link;
646 link = next_link;
647 }
648
649 DCHECK(branch == link);
650 next_link = branch->ImmPCOffsetTarget();
651
652 if (branch == prev_link) {
653 // The branch is the first instruction in the chain.
654 if (branch == next_link) {
655 // It is also the last instruction in the chain, so it is the only branch
656 // currently referring to this label.
657 label->Unuse();
658 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000659 label->link_to(
660 static_cast<int>(reinterpret_cast<byte*>(next_link) - buffer_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000661 }
662
663 } else if (branch == next_link) {
664 // The branch is the last (but not also the first) instruction in the chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000665 prev_link->SetImmPCOffsetTarget(isolate(), prev_link);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000666
667 } else {
668 // The branch is in the middle of the chain.
669 if (prev_link->IsTargetInImmPCOffsetRange(next_link)) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000670 prev_link->SetImmPCOffsetTarget(isolate(), next_link);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000671 } else if (label_veneer != NULL) {
672 // Use the veneer for all previous links in the chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000673 prev_link->SetImmPCOffsetTarget(isolate(), prev_link);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674
675 end_of_chain = false;
676 link = next_link;
677 while (!end_of_chain) {
678 next_link = link->ImmPCOffsetTarget();
679 end_of_chain = (link == next_link);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000680 link->SetImmPCOffsetTarget(isolate(), label_veneer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000681 link = next_link;
682 }
683 } else {
684 // The assert below will fire.
685 // Some other work could be attempted to fix up the chain, but it would be
686 // rather complicated. If we crash here, we may want to consider using an
687 // other mechanism than a chain of branches.
688 //
689 // Note that this situation currently should not happen, as we always call
690 // this function with a veneer to the target label.
691 // However this could happen with a MacroAssembler in the following state:
692 // [previous code]
693 // B(label);
694 // [20KB code]
695 // Tbz(label); // First tbz. Pointing to unconditional branch.
696 // [20KB code]
697 // Tbz(label); // Second tbz. Pointing to the first tbz.
698 // [more code]
699 // and this function is called to remove the first tbz from the label link
700 // chain. Since tbz has a range of +-32KB, the second tbz cannot point to
701 // the unconditional branch.
702 CHECK(prev_link->IsTargetInImmPCOffsetRange(next_link));
703 UNREACHABLE();
704 }
705 }
706
707 CheckLabelLinkChain(label);
708}
709
710
711void Assembler::bind(Label* label) {
712 // Bind label to the address at pc_. All instructions (most likely branches)
713 // that are linked to this label will be updated to point to the newly-bound
714 // label.
715
716 DCHECK(!label->is_near_linked());
717 DCHECK(!label->is_bound());
718
719 DeleteUnresolvedBranchInfoForLabel(label);
720
721 // If the label is linked, the link chain looks something like this:
722 //
723 // |--I----I-------I-------L
724 // |---------------------->| pc_offset
725 // |-------------->| linkoffset = label->pos()
726 // |<------| link->ImmPCOffset()
727 // |------>| prevlinkoffset = linkoffset + link->ImmPCOffset()
728 //
729 // On each iteration, the last link is updated and then removed from the
730 // chain until only one remains. At that point, the label is bound.
731 //
732 // If the label is not linked, no preparation is required before binding.
733 while (label->is_linked()) {
734 int linkoffset = label->pos();
735 Instruction* link = InstructionAt(linkoffset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000736 int prevlinkoffset = linkoffset + static_cast<int>(link->ImmPCOffset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000737
738 CheckLabelLinkChain(label);
739
740 DCHECK(linkoffset >= 0);
741 DCHECK(linkoffset < pc_offset());
742 DCHECK((linkoffset > prevlinkoffset) ||
743 (linkoffset - prevlinkoffset == kStartOfLabelLinkChain));
744 DCHECK(prevlinkoffset >= 0);
745
746 // Update the link to point to the label.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000747 if (link->IsUnresolvedInternalReference()) {
748 // Internal references do not get patched to an instruction but directly
749 // to an address.
750 internal_reference_positions_.push_back(linkoffset);
751 PatchingAssembler patcher(isolate(), link, 2);
752 patcher.dc64(reinterpret_cast<uintptr_t>(pc_));
753 } else {
754 link->SetImmPCOffsetTarget(isolate(),
755 reinterpret_cast<Instruction*>(pc_));
756 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000757
758 // Link the label to the previous link in the chain.
759 if (linkoffset - prevlinkoffset == kStartOfLabelLinkChain) {
760 // We hit kStartOfLabelLinkChain, so the chain is fully processed.
761 label->Unuse();
762 } else {
763 // Update the label for the next iteration.
764 label->link_to(prevlinkoffset);
765 }
766 }
767 label->bind_to(pc_offset());
768
769 DCHECK(label->is_bound());
770 DCHECK(!label->is_linked());
771}
772
773
774int Assembler::LinkAndGetByteOffsetTo(Label* label) {
775 DCHECK(sizeof(*pc_) == 1);
776 CheckLabelLinkChain(label);
777
778 int offset;
779 if (label->is_bound()) {
780 // The label is bound, so it does not need to be updated. Referring
781 // instructions must link directly to the label as they will not be
782 // updated.
783 //
784 // In this case, label->pos() returns the offset of the label from the
785 // start of the buffer.
786 //
787 // Note that offset can be zero for self-referential instructions. (This
788 // could be useful for ADR, for example.)
789 offset = label->pos() - pc_offset();
790 DCHECK(offset <= 0);
791 } else {
792 if (label->is_linked()) {
793 // The label is linked, so the referring instruction should be added onto
794 // the end of the label's link chain.
795 //
796 // In this case, label->pos() returns the offset of the last linked
797 // instruction from the start of the buffer.
798 offset = label->pos() - pc_offset();
799 DCHECK(offset != kStartOfLabelLinkChain);
800 // Note that the offset here needs to be PC-relative only so that the
801 // first instruction in a buffer can link to an unbound label. Otherwise,
802 // the offset would be 0 for this case, and 0 is reserved for
803 // kStartOfLabelLinkChain.
804 } else {
805 // The label is unused, so it now becomes linked and the referring
806 // instruction is at the start of the new link chain.
807 offset = kStartOfLabelLinkChain;
808 }
809 // The instruction at pc is now the last link in the label's chain.
810 label->link_to(pc_offset());
811 }
812
813 return offset;
814}
815
816
817void Assembler::DeleteUnresolvedBranchInfoForLabelTraverse(Label* label) {
818 DCHECK(label->is_linked());
819 CheckLabelLinkChain(label);
820
821 int link_offset = label->pos();
822 int link_pcoffset;
823 bool end_of_chain = false;
824
825 while (!end_of_chain) {
826 Instruction * link = InstructionAt(link_offset);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000827 link_pcoffset = static_cast<int>(link->ImmPCOffset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000828
829 // ADR instructions are not handled by veneers.
830 if (link->IsImmBranch()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000831 int max_reachable_pc =
832 static_cast<int>(InstructionOffset(link) +
833 Instruction::ImmBranchRange(link->BranchType()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000834 typedef std::multimap<int, FarBranchInfo>::iterator unresolved_info_it;
835 std::pair<unresolved_info_it, unresolved_info_it> range;
836 range = unresolved_branches_.equal_range(max_reachable_pc);
837 unresolved_info_it it;
838 for (it = range.first; it != range.second; ++it) {
839 if (it->second.pc_offset_ == link_offset) {
840 unresolved_branches_.erase(it);
841 break;
842 }
843 }
844 }
845
846 end_of_chain = (link_pcoffset == 0);
847 link_offset = link_offset + link_pcoffset;
848 }
849}
850
851
852void Assembler::DeleteUnresolvedBranchInfoForLabel(Label* label) {
853 if (unresolved_branches_.empty()) {
854 DCHECK(next_veneer_pool_check_ == kMaxInt);
855 return;
856 }
857
858 if (label->is_linked()) {
859 // Branches to this label will be resolved when the label is bound, normally
860 // just after all the associated info has been deleted.
861 DeleteUnresolvedBranchInfoForLabelTraverse(label);
862 }
863 if (unresolved_branches_.empty()) {
864 next_veneer_pool_check_ = kMaxInt;
865 } else {
866 next_veneer_pool_check_ =
867 unresolved_branches_first_limit() - kVeneerDistanceCheckMargin;
868 }
869}
870
871
872void Assembler::StartBlockConstPool() {
873 if (const_pool_blocked_nesting_++ == 0) {
874 // Prevent constant pool checks happening by setting the next check to
875 // the biggest possible offset.
876 next_constant_pool_check_ = kMaxInt;
877 }
878}
879
880
881void Assembler::EndBlockConstPool() {
882 if (--const_pool_blocked_nesting_ == 0) {
883 // Check the constant pool hasn't been blocked for too long.
884 DCHECK(pc_offset() < constpool_.MaxPcOffset());
885 // Two cases:
886 // * no_const_pool_before_ >= next_constant_pool_check_ and the emission is
887 // still blocked
888 // * no_const_pool_before_ < next_constant_pool_check_ and the next emit
889 // will trigger a check.
890 next_constant_pool_check_ = no_const_pool_before_;
891 }
892}
893
894
895bool Assembler::is_const_pool_blocked() const {
896 return (const_pool_blocked_nesting_ > 0) ||
897 (pc_offset() < no_const_pool_before_);
898}
899
900
901bool Assembler::IsConstantPoolAt(Instruction* instr) {
902 // The constant pool marker is made of two instructions. These instructions
903 // will never be emitted by the JIT, so checking for the first one is enough:
904 // 0: ldr xzr, #<size of pool>
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000905 bool result = instr->IsLdrLiteralX() && (instr->Rt() == kZeroRegCode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000906
907 // It is still worth asserting the marker is complete.
908 // 4: blr xzr
909 DCHECK(!result || (instr->following()->IsBranchAndLinkToRegister() &&
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000910 instr->following()->Rn() == kZeroRegCode));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000911
912 return result;
913}
914
915
916int Assembler::ConstantPoolSizeAt(Instruction* instr) {
917#ifdef USE_SIMULATOR
918 // Assembler::debug() embeds constants directly into the instruction stream.
919 // Although this is not a genuine constant pool, treat it like one to avoid
920 // disassembling the constants.
921 if ((instr->Mask(ExceptionMask) == HLT) &&
922 (instr->ImmException() == kImmExceptionIsDebug)) {
923 const char* message =
924 reinterpret_cast<const char*>(
925 instr->InstructionAtOffset(kDebugMessageOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000926 int size = static_cast<int>(kDebugMessageOffset + strlen(message) + 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000927 return RoundUp(size, kInstructionSize) / kInstructionSize;
928 }
929 // Same for printf support, see MacroAssembler::CallPrintf().
930 if ((instr->Mask(ExceptionMask) == HLT) &&
931 (instr->ImmException() == kImmExceptionIsPrintf)) {
932 return kPrintfLength / kInstructionSize;
933 }
934#endif
935 if (IsConstantPoolAt(instr)) {
936 return instr->ImmLLiteral();
937 } else {
938 return -1;
939 }
940}
941
942
943void Assembler::EmitPoolGuard() {
944 // We must generate only one instruction as this is used in scopes that
945 // control the size of the code generated.
946 Emit(BLR | Rn(xzr));
947}
948
949
950void Assembler::StartBlockVeneerPool() {
951 ++veneer_pool_blocked_nesting_;
952}
953
954
955void Assembler::EndBlockVeneerPool() {
956 if (--veneer_pool_blocked_nesting_ == 0) {
957 // Check the veneer pool hasn't been blocked for too long.
958 DCHECK(unresolved_branches_.empty() ||
959 (pc_offset() < unresolved_branches_first_limit()));
960 }
961}
962
963
964void Assembler::br(const Register& xn) {
965 positions_recorder()->WriteRecordedPositions();
966 DCHECK(xn.Is64Bits());
967 Emit(BR | Rn(xn));
968}
969
970
971void Assembler::blr(const Register& xn) {
972 positions_recorder()->WriteRecordedPositions();
973 DCHECK(xn.Is64Bits());
974 // The pattern 'blr xzr' is used as a guard to detect when execution falls
975 // through the constant pool. It should not be emitted.
976 DCHECK(!xn.Is(xzr));
977 Emit(BLR | Rn(xn));
978}
979
980
981void Assembler::ret(const Register& xn) {
982 positions_recorder()->WriteRecordedPositions();
983 DCHECK(xn.Is64Bits());
984 Emit(RET | Rn(xn));
985}
986
987
988void Assembler::b(int imm26) {
989 Emit(B | ImmUncondBranch(imm26));
990}
991
992
993void Assembler::b(Label* label) {
994 positions_recorder()->WriteRecordedPositions();
995 b(LinkAndGetInstructionOffsetTo(label));
996}
997
998
999void Assembler::b(int imm19, Condition cond) {
1000 Emit(B_cond | ImmCondBranch(imm19) | cond);
1001}
1002
1003
1004void Assembler::b(Label* label, Condition cond) {
1005 positions_recorder()->WriteRecordedPositions();
1006 b(LinkAndGetInstructionOffsetTo(label), cond);
1007}
1008
1009
1010void Assembler::bl(int imm26) {
1011 positions_recorder()->WriteRecordedPositions();
1012 Emit(BL | ImmUncondBranch(imm26));
1013}
1014
1015
1016void Assembler::bl(Label* label) {
1017 positions_recorder()->WriteRecordedPositions();
1018 bl(LinkAndGetInstructionOffsetTo(label));
1019}
1020
1021
1022void Assembler::cbz(const Register& rt,
1023 int imm19) {
1024 positions_recorder()->WriteRecordedPositions();
1025 Emit(SF(rt) | CBZ | ImmCmpBranch(imm19) | Rt(rt));
1026}
1027
1028
1029void Assembler::cbz(const Register& rt,
1030 Label* label) {
1031 positions_recorder()->WriteRecordedPositions();
1032 cbz(rt, LinkAndGetInstructionOffsetTo(label));
1033}
1034
1035
1036void Assembler::cbnz(const Register& rt,
1037 int imm19) {
1038 positions_recorder()->WriteRecordedPositions();
1039 Emit(SF(rt) | CBNZ | ImmCmpBranch(imm19) | Rt(rt));
1040}
1041
1042
1043void Assembler::cbnz(const Register& rt,
1044 Label* label) {
1045 positions_recorder()->WriteRecordedPositions();
1046 cbnz(rt, LinkAndGetInstructionOffsetTo(label));
1047}
1048
1049
1050void Assembler::tbz(const Register& rt,
1051 unsigned bit_pos,
1052 int imm14) {
1053 positions_recorder()->WriteRecordedPositions();
1054 DCHECK(rt.Is64Bits() || (rt.Is32Bits() && (bit_pos < kWRegSizeInBits)));
1055 Emit(TBZ | ImmTestBranchBit(bit_pos) | ImmTestBranch(imm14) | Rt(rt));
1056}
1057
1058
1059void Assembler::tbz(const Register& rt,
1060 unsigned bit_pos,
1061 Label* label) {
1062 positions_recorder()->WriteRecordedPositions();
1063 tbz(rt, bit_pos, LinkAndGetInstructionOffsetTo(label));
1064}
1065
1066
1067void Assembler::tbnz(const Register& rt,
1068 unsigned bit_pos,
1069 int imm14) {
1070 positions_recorder()->WriteRecordedPositions();
1071 DCHECK(rt.Is64Bits() || (rt.Is32Bits() && (bit_pos < kWRegSizeInBits)));
1072 Emit(TBNZ | ImmTestBranchBit(bit_pos) | ImmTestBranch(imm14) | Rt(rt));
1073}
1074
1075
1076void Assembler::tbnz(const Register& rt,
1077 unsigned bit_pos,
1078 Label* label) {
1079 positions_recorder()->WriteRecordedPositions();
1080 tbnz(rt, bit_pos, LinkAndGetInstructionOffsetTo(label));
1081}
1082
1083
1084void Assembler::adr(const Register& rd, int imm21) {
1085 DCHECK(rd.Is64Bits());
1086 Emit(ADR | ImmPCRelAddress(imm21) | Rd(rd));
1087}
1088
1089
1090void Assembler::adr(const Register& rd, Label* label) {
1091 adr(rd, LinkAndGetByteOffsetTo(label));
1092}
1093
1094
1095void Assembler::add(const Register& rd,
1096 const Register& rn,
1097 const Operand& operand) {
1098 AddSub(rd, rn, operand, LeaveFlags, ADD);
1099}
1100
1101
1102void Assembler::adds(const Register& rd,
1103 const Register& rn,
1104 const Operand& operand) {
1105 AddSub(rd, rn, operand, SetFlags, ADD);
1106}
1107
1108
1109void Assembler::cmn(const Register& rn,
1110 const Operand& operand) {
1111 Register zr = AppropriateZeroRegFor(rn);
1112 adds(zr, rn, operand);
1113}
1114
1115
1116void Assembler::sub(const Register& rd,
1117 const Register& rn,
1118 const Operand& operand) {
1119 AddSub(rd, rn, operand, LeaveFlags, SUB);
1120}
1121
1122
1123void Assembler::subs(const Register& rd,
1124 const Register& rn,
1125 const Operand& operand) {
1126 AddSub(rd, rn, operand, SetFlags, SUB);
1127}
1128
1129
1130void Assembler::cmp(const Register& rn, const Operand& operand) {
1131 Register zr = AppropriateZeroRegFor(rn);
1132 subs(zr, rn, operand);
1133}
1134
1135
1136void Assembler::neg(const Register& rd, const Operand& operand) {
1137 Register zr = AppropriateZeroRegFor(rd);
1138 sub(rd, zr, operand);
1139}
1140
1141
1142void Assembler::negs(const Register& rd, const Operand& operand) {
1143 Register zr = AppropriateZeroRegFor(rd);
1144 subs(rd, zr, operand);
1145}
1146
1147
1148void Assembler::adc(const Register& rd,
1149 const Register& rn,
1150 const Operand& operand) {
1151 AddSubWithCarry(rd, rn, operand, LeaveFlags, ADC);
1152}
1153
1154
1155void Assembler::adcs(const Register& rd,
1156 const Register& rn,
1157 const Operand& operand) {
1158 AddSubWithCarry(rd, rn, operand, SetFlags, ADC);
1159}
1160
1161
1162void Assembler::sbc(const Register& rd,
1163 const Register& rn,
1164 const Operand& operand) {
1165 AddSubWithCarry(rd, rn, operand, LeaveFlags, SBC);
1166}
1167
1168
1169void Assembler::sbcs(const Register& rd,
1170 const Register& rn,
1171 const Operand& operand) {
1172 AddSubWithCarry(rd, rn, operand, SetFlags, SBC);
1173}
1174
1175
1176void Assembler::ngc(const Register& rd, const Operand& operand) {
1177 Register zr = AppropriateZeroRegFor(rd);
1178 sbc(rd, zr, operand);
1179}
1180
1181
1182void Assembler::ngcs(const Register& rd, const Operand& operand) {
1183 Register zr = AppropriateZeroRegFor(rd);
1184 sbcs(rd, zr, operand);
1185}
1186
1187
1188// Logical instructions.
1189void Assembler::and_(const Register& rd,
1190 const Register& rn,
1191 const Operand& operand) {
1192 Logical(rd, rn, operand, AND);
1193}
1194
1195
1196void Assembler::ands(const Register& rd,
1197 const Register& rn,
1198 const Operand& operand) {
1199 Logical(rd, rn, operand, ANDS);
1200}
1201
1202
1203void Assembler::tst(const Register& rn,
1204 const Operand& operand) {
1205 ands(AppropriateZeroRegFor(rn), rn, operand);
1206}
1207
1208
1209void Assembler::bic(const Register& rd,
1210 const Register& rn,
1211 const Operand& operand) {
1212 Logical(rd, rn, operand, BIC);
1213}
1214
1215
1216void Assembler::bics(const Register& rd,
1217 const Register& rn,
1218 const Operand& operand) {
1219 Logical(rd, rn, operand, BICS);
1220}
1221
1222
1223void Assembler::orr(const Register& rd,
1224 const Register& rn,
1225 const Operand& operand) {
1226 Logical(rd, rn, operand, ORR);
1227}
1228
1229
1230void Assembler::orn(const Register& rd,
1231 const Register& rn,
1232 const Operand& operand) {
1233 Logical(rd, rn, operand, ORN);
1234}
1235
1236
1237void Assembler::eor(const Register& rd,
1238 const Register& rn,
1239 const Operand& operand) {
1240 Logical(rd, rn, operand, EOR);
1241}
1242
1243
1244void Assembler::eon(const Register& rd,
1245 const Register& rn,
1246 const Operand& operand) {
1247 Logical(rd, rn, operand, EON);
1248}
1249
1250
1251void Assembler::lslv(const Register& rd,
1252 const Register& rn,
1253 const Register& rm) {
1254 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1255 DCHECK(rd.SizeInBits() == rm.SizeInBits());
1256 Emit(SF(rd) | LSLV | Rm(rm) | Rn(rn) | Rd(rd));
1257}
1258
1259
1260void Assembler::lsrv(const Register& rd,
1261 const Register& rn,
1262 const Register& rm) {
1263 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1264 DCHECK(rd.SizeInBits() == rm.SizeInBits());
1265 Emit(SF(rd) | LSRV | Rm(rm) | Rn(rn) | Rd(rd));
1266}
1267
1268
1269void Assembler::asrv(const Register& rd,
1270 const Register& rn,
1271 const Register& rm) {
1272 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1273 DCHECK(rd.SizeInBits() == rm.SizeInBits());
1274 Emit(SF(rd) | ASRV | Rm(rm) | Rn(rn) | Rd(rd));
1275}
1276
1277
1278void Assembler::rorv(const Register& rd,
1279 const Register& rn,
1280 const Register& rm) {
1281 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1282 DCHECK(rd.SizeInBits() == rm.SizeInBits());
1283 Emit(SF(rd) | RORV | Rm(rm) | Rn(rn) | Rd(rd));
1284}
1285
1286
1287// Bitfield operations.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001288void Assembler::bfm(const Register& rd, const Register& rn, int immr,
1289 int imms) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001290 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1291 Instr N = SF(rd) >> (kSFOffset - kBitfieldNOffset);
1292 Emit(SF(rd) | BFM | N |
1293 ImmR(immr, rd.SizeInBits()) |
1294 ImmS(imms, rn.SizeInBits()) |
1295 Rn(rn) | Rd(rd));
1296}
1297
1298
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001299void Assembler::sbfm(const Register& rd, const Register& rn, int immr,
1300 int imms) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001301 DCHECK(rd.Is64Bits() || rn.Is32Bits());
1302 Instr N = SF(rd) >> (kSFOffset - kBitfieldNOffset);
1303 Emit(SF(rd) | SBFM | N |
1304 ImmR(immr, rd.SizeInBits()) |
1305 ImmS(imms, rn.SizeInBits()) |
1306 Rn(rn) | Rd(rd));
1307}
1308
1309
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001310void Assembler::ubfm(const Register& rd, const Register& rn, int immr,
1311 int imms) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1313 Instr N = SF(rd) >> (kSFOffset - kBitfieldNOffset);
1314 Emit(SF(rd) | UBFM | N |
1315 ImmR(immr, rd.SizeInBits()) |
1316 ImmS(imms, rn.SizeInBits()) |
1317 Rn(rn) | Rd(rd));
1318}
1319
1320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001321void Assembler::extr(const Register& rd, const Register& rn, const Register& rm,
1322 int lsb) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001323 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1324 DCHECK(rd.SizeInBits() == rm.SizeInBits());
1325 Instr N = SF(rd) >> (kSFOffset - kBitfieldNOffset);
1326 Emit(SF(rd) | EXTR | N | Rm(rm) |
1327 ImmS(lsb, rn.SizeInBits()) | Rn(rn) | Rd(rd));
1328}
1329
1330
1331void Assembler::csel(const Register& rd,
1332 const Register& rn,
1333 const Register& rm,
1334 Condition cond) {
1335 ConditionalSelect(rd, rn, rm, cond, CSEL);
1336}
1337
1338
1339void Assembler::csinc(const Register& rd,
1340 const Register& rn,
1341 const Register& rm,
1342 Condition cond) {
1343 ConditionalSelect(rd, rn, rm, cond, CSINC);
1344}
1345
1346
1347void Assembler::csinv(const Register& rd,
1348 const Register& rn,
1349 const Register& rm,
1350 Condition cond) {
1351 ConditionalSelect(rd, rn, rm, cond, CSINV);
1352}
1353
1354
1355void Assembler::csneg(const Register& rd,
1356 const Register& rn,
1357 const Register& rm,
1358 Condition cond) {
1359 ConditionalSelect(rd, rn, rm, cond, CSNEG);
1360}
1361
1362
1363void Assembler::cset(const Register &rd, Condition cond) {
1364 DCHECK((cond != al) && (cond != nv));
1365 Register zr = AppropriateZeroRegFor(rd);
1366 csinc(rd, zr, zr, NegateCondition(cond));
1367}
1368
1369
1370void Assembler::csetm(const Register &rd, Condition cond) {
1371 DCHECK((cond != al) && (cond != nv));
1372 Register zr = AppropriateZeroRegFor(rd);
1373 csinv(rd, zr, zr, NegateCondition(cond));
1374}
1375
1376
1377void Assembler::cinc(const Register &rd, const Register &rn, Condition cond) {
1378 DCHECK((cond != al) && (cond != nv));
1379 csinc(rd, rn, rn, NegateCondition(cond));
1380}
1381
1382
1383void Assembler::cinv(const Register &rd, const Register &rn, Condition cond) {
1384 DCHECK((cond != al) && (cond != nv));
1385 csinv(rd, rn, rn, NegateCondition(cond));
1386}
1387
1388
1389void Assembler::cneg(const Register &rd, const Register &rn, Condition cond) {
1390 DCHECK((cond != al) && (cond != nv));
1391 csneg(rd, rn, rn, NegateCondition(cond));
1392}
1393
1394
1395void Assembler::ConditionalSelect(const Register& rd,
1396 const Register& rn,
1397 const Register& rm,
1398 Condition cond,
1399 ConditionalSelectOp op) {
1400 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1401 DCHECK(rd.SizeInBits() == rm.SizeInBits());
1402 Emit(SF(rd) | op | Rm(rm) | Cond(cond) | Rn(rn) | Rd(rd));
1403}
1404
1405
1406void Assembler::ccmn(const Register& rn,
1407 const Operand& operand,
1408 StatusFlags nzcv,
1409 Condition cond) {
1410 ConditionalCompare(rn, operand, nzcv, cond, CCMN);
1411}
1412
1413
1414void Assembler::ccmp(const Register& rn,
1415 const Operand& operand,
1416 StatusFlags nzcv,
1417 Condition cond) {
1418 ConditionalCompare(rn, operand, nzcv, cond, CCMP);
1419}
1420
1421
1422void Assembler::DataProcessing3Source(const Register& rd,
1423 const Register& rn,
1424 const Register& rm,
1425 const Register& ra,
1426 DataProcessing3SourceOp op) {
1427 Emit(SF(rd) | op | Rm(rm) | Ra(ra) | Rn(rn) | Rd(rd));
1428}
1429
1430
1431void Assembler::mul(const Register& rd,
1432 const Register& rn,
1433 const Register& rm) {
1434 DCHECK(AreSameSizeAndType(rd, rn, rm));
1435 Register zr = AppropriateZeroRegFor(rn);
1436 DataProcessing3Source(rd, rn, rm, zr, MADD);
1437}
1438
1439
1440void Assembler::madd(const Register& rd,
1441 const Register& rn,
1442 const Register& rm,
1443 const Register& ra) {
1444 DCHECK(AreSameSizeAndType(rd, rn, rm, ra));
1445 DataProcessing3Source(rd, rn, rm, ra, MADD);
1446}
1447
1448
1449void Assembler::mneg(const Register& rd,
1450 const Register& rn,
1451 const Register& rm) {
1452 DCHECK(AreSameSizeAndType(rd, rn, rm));
1453 Register zr = AppropriateZeroRegFor(rn);
1454 DataProcessing3Source(rd, rn, rm, zr, MSUB);
1455}
1456
1457
1458void Assembler::msub(const Register& rd,
1459 const Register& rn,
1460 const Register& rm,
1461 const Register& ra) {
1462 DCHECK(AreSameSizeAndType(rd, rn, rm, ra));
1463 DataProcessing3Source(rd, rn, rm, ra, MSUB);
1464}
1465
1466
1467void Assembler::smaddl(const Register& rd,
1468 const Register& rn,
1469 const Register& rm,
1470 const Register& ra) {
1471 DCHECK(rd.Is64Bits() && ra.Is64Bits());
1472 DCHECK(rn.Is32Bits() && rm.Is32Bits());
1473 DataProcessing3Source(rd, rn, rm, ra, SMADDL_x);
1474}
1475
1476
1477void Assembler::smsubl(const Register& rd,
1478 const Register& rn,
1479 const Register& rm,
1480 const Register& ra) {
1481 DCHECK(rd.Is64Bits() && ra.Is64Bits());
1482 DCHECK(rn.Is32Bits() && rm.Is32Bits());
1483 DataProcessing3Source(rd, rn, rm, ra, SMSUBL_x);
1484}
1485
1486
1487void Assembler::umaddl(const Register& rd,
1488 const Register& rn,
1489 const Register& rm,
1490 const Register& ra) {
1491 DCHECK(rd.Is64Bits() && ra.Is64Bits());
1492 DCHECK(rn.Is32Bits() && rm.Is32Bits());
1493 DataProcessing3Source(rd, rn, rm, ra, UMADDL_x);
1494}
1495
1496
1497void Assembler::umsubl(const Register& rd,
1498 const Register& rn,
1499 const Register& rm,
1500 const Register& ra) {
1501 DCHECK(rd.Is64Bits() && ra.Is64Bits());
1502 DCHECK(rn.Is32Bits() && rm.Is32Bits());
1503 DataProcessing3Source(rd, rn, rm, ra, UMSUBL_x);
1504}
1505
1506
1507void Assembler::smull(const Register& rd,
1508 const Register& rn,
1509 const Register& rm) {
1510 DCHECK(rd.Is64Bits());
1511 DCHECK(rn.Is32Bits() && rm.Is32Bits());
1512 DataProcessing3Source(rd, rn, rm, xzr, SMADDL_x);
1513}
1514
1515
1516void Assembler::smulh(const Register& rd,
1517 const Register& rn,
1518 const Register& rm) {
1519 DCHECK(AreSameSizeAndType(rd, rn, rm));
1520 DataProcessing3Source(rd, rn, rm, xzr, SMULH_x);
1521}
1522
1523
1524void Assembler::sdiv(const Register& rd,
1525 const Register& rn,
1526 const Register& rm) {
1527 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1528 DCHECK(rd.SizeInBits() == rm.SizeInBits());
1529 Emit(SF(rd) | SDIV | Rm(rm) | Rn(rn) | Rd(rd));
1530}
1531
1532
1533void Assembler::udiv(const Register& rd,
1534 const Register& rn,
1535 const Register& rm) {
1536 DCHECK(rd.SizeInBits() == rn.SizeInBits());
1537 DCHECK(rd.SizeInBits() == rm.SizeInBits());
1538 Emit(SF(rd) | UDIV | Rm(rm) | Rn(rn) | Rd(rd));
1539}
1540
1541
1542void Assembler::rbit(const Register& rd,
1543 const Register& rn) {
1544 DataProcessing1Source(rd, rn, RBIT);
1545}
1546
1547
1548void Assembler::rev16(const Register& rd,
1549 const Register& rn) {
1550 DataProcessing1Source(rd, rn, REV16);
1551}
1552
1553
1554void Assembler::rev32(const Register& rd,
1555 const Register& rn) {
1556 DCHECK(rd.Is64Bits());
1557 DataProcessing1Source(rd, rn, REV);
1558}
1559
1560
1561void Assembler::rev(const Register& rd,
1562 const Register& rn) {
1563 DataProcessing1Source(rd, rn, rd.Is64Bits() ? REV_x : REV_w);
1564}
1565
1566
1567void Assembler::clz(const Register& rd,
1568 const Register& rn) {
1569 DataProcessing1Source(rd, rn, CLZ);
1570}
1571
1572
1573void Assembler::cls(const Register& rd,
1574 const Register& rn) {
1575 DataProcessing1Source(rd, rn, CLS);
1576}
1577
1578
1579void Assembler::ldp(const CPURegister& rt,
1580 const CPURegister& rt2,
1581 const MemOperand& src) {
1582 LoadStorePair(rt, rt2, src, LoadPairOpFor(rt, rt2));
1583}
1584
1585
1586void Assembler::stp(const CPURegister& rt,
1587 const CPURegister& rt2,
1588 const MemOperand& dst) {
1589 LoadStorePair(rt, rt2, dst, StorePairOpFor(rt, rt2));
1590}
1591
1592
1593void Assembler::ldpsw(const Register& rt,
1594 const Register& rt2,
1595 const MemOperand& src) {
1596 DCHECK(rt.Is64Bits());
1597 LoadStorePair(rt, rt2, src, LDPSW_x);
1598}
1599
1600
1601void Assembler::LoadStorePair(const CPURegister& rt,
1602 const CPURegister& rt2,
1603 const MemOperand& addr,
1604 LoadStorePairOp op) {
1605 // 'rt' and 'rt2' can only be aliased for stores.
1606 DCHECK(((op & LoadStorePairLBit) == 0) || !rt.Is(rt2));
1607 DCHECK(AreSameSizeAndType(rt, rt2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001608 DCHECK(IsImmLSPair(addr.offset(), CalcLSPairDataSize(op)));
1609 int offset = static_cast<int>(addr.offset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001610
1611 Instr memop = op | Rt(rt) | Rt2(rt2) | RnSP(addr.base()) |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001612 ImmLSPair(offset, CalcLSPairDataSize(op));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001613
1614 Instr addrmodeop;
1615 if (addr.IsImmediateOffset()) {
1616 addrmodeop = LoadStorePairOffsetFixed;
1617 } else {
1618 // Pre-index and post-index modes.
1619 DCHECK(!rt.Is(addr.base()));
1620 DCHECK(!rt2.Is(addr.base()));
1621 DCHECK(addr.offset() != 0);
1622 if (addr.IsPreIndex()) {
1623 addrmodeop = LoadStorePairPreIndexFixed;
1624 } else {
1625 DCHECK(addr.IsPostIndex());
1626 addrmodeop = LoadStorePairPostIndexFixed;
1627 }
1628 }
1629 Emit(addrmodeop | memop);
1630}
1631
1632
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001633// Memory instructions.
1634void Assembler::ldrb(const Register& rt, const MemOperand& src) {
1635 LoadStore(rt, src, LDRB_w);
1636}
1637
1638
1639void Assembler::strb(const Register& rt, const MemOperand& dst) {
1640 LoadStore(rt, dst, STRB_w);
1641}
1642
1643
1644void Assembler::ldrsb(const Register& rt, const MemOperand& src) {
1645 LoadStore(rt, src, rt.Is64Bits() ? LDRSB_x : LDRSB_w);
1646}
1647
1648
1649void Assembler::ldrh(const Register& rt, const MemOperand& src) {
1650 LoadStore(rt, src, LDRH_w);
1651}
1652
1653
1654void Assembler::strh(const Register& rt, const MemOperand& dst) {
1655 LoadStore(rt, dst, STRH_w);
1656}
1657
1658
1659void Assembler::ldrsh(const Register& rt, const MemOperand& src) {
1660 LoadStore(rt, src, rt.Is64Bits() ? LDRSH_x : LDRSH_w);
1661}
1662
1663
1664void Assembler::ldr(const CPURegister& rt, const MemOperand& src) {
1665 LoadStore(rt, src, LoadOpFor(rt));
1666}
1667
1668
1669void Assembler::str(const CPURegister& rt, const MemOperand& src) {
1670 LoadStore(rt, src, StoreOpFor(rt));
1671}
1672
1673
1674void Assembler::ldrsw(const Register& rt, const MemOperand& src) {
1675 DCHECK(rt.Is64Bits());
1676 LoadStore(rt, src, LDRSW_x);
1677}
1678
1679
1680void Assembler::ldr_pcrel(const CPURegister& rt, int imm19) {
1681 // The pattern 'ldr xzr, #offset' is used to indicate the beginning of a
1682 // constant pool. It should not be emitted.
1683 DCHECK(!rt.IsZero());
1684 Emit(LoadLiteralOpFor(rt) | ImmLLiteral(imm19) | Rt(rt));
1685}
1686
1687
1688void Assembler::ldr(const CPURegister& rt, const Immediate& imm) {
1689 // Currently we only support 64-bit literals.
1690 DCHECK(rt.Is64Bits());
1691
1692 RecordRelocInfo(imm.rmode(), imm.value());
1693 BlockConstPoolFor(1);
1694 // The load will be patched when the constpool is emitted, patching code
1695 // expect a load literal with offset 0.
1696 ldr_pcrel(rt, 0);
1697}
1698
1699
1700void Assembler::mov(const Register& rd, const Register& rm) {
1701 // Moves involving the stack pointer are encoded as add immediate with
1702 // second operand of zero. Otherwise, orr with first operand zr is
1703 // used.
1704 if (rd.IsSP() || rm.IsSP()) {
1705 add(rd, rm, 0);
1706 } else {
1707 orr(rd, AppropriateZeroRegFor(rd), rm);
1708 }
1709}
1710
1711
1712void Assembler::mvn(const Register& rd, const Operand& operand) {
1713 orn(rd, AppropriateZeroRegFor(rd), operand);
1714}
1715
1716
1717void Assembler::mrs(const Register& rt, SystemRegister sysreg) {
1718 DCHECK(rt.Is64Bits());
1719 Emit(MRS | ImmSystemRegister(sysreg) | Rt(rt));
1720}
1721
1722
1723void Assembler::msr(SystemRegister sysreg, const Register& rt) {
1724 DCHECK(rt.Is64Bits());
1725 Emit(MSR | Rt(rt) | ImmSystemRegister(sysreg));
1726}
1727
1728
1729void Assembler::hint(SystemHint code) {
1730 Emit(HINT | ImmHint(code) | Rt(xzr));
1731}
1732
1733
1734void Assembler::dmb(BarrierDomain domain, BarrierType type) {
1735 Emit(DMB | ImmBarrierDomain(domain) | ImmBarrierType(type));
1736}
1737
1738
1739void Assembler::dsb(BarrierDomain domain, BarrierType type) {
1740 Emit(DSB | ImmBarrierDomain(domain) | ImmBarrierType(type));
1741}
1742
1743
1744void Assembler::isb() {
1745 Emit(ISB | ImmBarrierDomain(FullSystem) | ImmBarrierType(BarrierAll));
1746}
1747
1748
1749void Assembler::fmov(FPRegister fd, double imm) {
1750 DCHECK(fd.Is64Bits());
1751 DCHECK(IsImmFP64(imm));
1752 Emit(FMOV_d_imm | Rd(fd) | ImmFP64(imm));
1753}
1754
1755
1756void Assembler::fmov(FPRegister fd, float imm) {
1757 DCHECK(fd.Is32Bits());
1758 DCHECK(IsImmFP32(imm));
1759 Emit(FMOV_s_imm | Rd(fd) | ImmFP32(imm));
1760}
1761
1762
1763void Assembler::fmov(Register rd, FPRegister fn) {
1764 DCHECK(rd.SizeInBits() == fn.SizeInBits());
1765 FPIntegerConvertOp op = rd.Is32Bits() ? FMOV_ws : FMOV_xd;
1766 Emit(op | Rd(rd) | Rn(fn));
1767}
1768
1769
1770void Assembler::fmov(FPRegister fd, Register rn) {
1771 DCHECK(fd.SizeInBits() == rn.SizeInBits());
1772 FPIntegerConvertOp op = fd.Is32Bits() ? FMOV_sw : FMOV_dx;
1773 Emit(op | Rd(fd) | Rn(rn));
1774}
1775
1776
1777void Assembler::fmov(FPRegister fd, FPRegister fn) {
1778 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1779 Emit(FPType(fd) | FMOV | Rd(fd) | Rn(fn));
1780}
1781
1782
1783void Assembler::fadd(const FPRegister& fd,
1784 const FPRegister& fn,
1785 const FPRegister& fm) {
1786 FPDataProcessing2Source(fd, fn, fm, FADD);
1787}
1788
1789
1790void Assembler::fsub(const FPRegister& fd,
1791 const FPRegister& fn,
1792 const FPRegister& fm) {
1793 FPDataProcessing2Source(fd, fn, fm, FSUB);
1794}
1795
1796
1797void Assembler::fmul(const FPRegister& fd,
1798 const FPRegister& fn,
1799 const FPRegister& fm) {
1800 FPDataProcessing2Source(fd, fn, fm, FMUL);
1801}
1802
1803
1804void Assembler::fmadd(const FPRegister& fd,
1805 const FPRegister& fn,
1806 const FPRegister& fm,
1807 const FPRegister& fa) {
1808 FPDataProcessing3Source(fd, fn, fm, fa, fd.Is32Bits() ? FMADD_s : FMADD_d);
1809}
1810
1811
1812void Assembler::fmsub(const FPRegister& fd,
1813 const FPRegister& fn,
1814 const FPRegister& fm,
1815 const FPRegister& fa) {
1816 FPDataProcessing3Source(fd, fn, fm, fa, fd.Is32Bits() ? FMSUB_s : FMSUB_d);
1817}
1818
1819
1820void Assembler::fnmadd(const FPRegister& fd,
1821 const FPRegister& fn,
1822 const FPRegister& fm,
1823 const FPRegister& fa) {
1824 FPDataProcessing3Source(fd, fn, fm, fa, fd.Is32Bits() ? FNMADD_s : FNMADD_d);
1825}
1826
1827
1828void Assembler::fnmsub(const FPRegister& fd,
1829 const FPRegister& fn,
1830 const FPRegister& fm,
1831 const FPRegister& fa) {
1832 FPDataProcessing3Source(fd, fn, fm, fa, fd.Is32Bits() ? FNMSUB_s : FNMSUB_d);
1833}
1834
1835
1836void Assembler::fdiv(const FPRegister& fd,
1837 const FPRegister& fn,
1838 const FPRegister& fm) {
1839 FPDataProcessing2Source(fd, fn, fm, FDIV);
1840}
1841
1842
1843void Assembler::fmax(const FPRegister& fd,
1844 const FPRegister& fn,
1845 const FPRegister& fm) {
1846 FPDataProcessing2Source(fd, fn, fm, FMAX);
1847}
1848
1849
1850void Assembler::fmaxnm(const FPRegister& fd,
1851 const FPRegister& fn,
1852 const FPRegister& fm) {
1853 FPDataProcessing2Source(fd, fn, fm, FMAXNM);
1854}
1855
1856
1857void Assembler::fmin(const FPRegister& fd,
1858 const FPRegister& fn,
1859 const FPRegister& fm) {
1860 FPDataProcessing2Source(fd, fn, fm, FMIN);
1861}
1862
1863
1864void Assembler::fminnm(const FPRegister& fd,
1865 const FPRegister& fn,
1866 const FPRegister& fm) {
1867 FPDataProcessing2Source(fd, fn, fm, FMINNM);
1868}
1869
1870
1871void Assembler::fabs(const FPRegister& fd,
1872 const FPRegister& fn) {
1873 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1874 FPDataProcessing1Source(fd, fn, FABS);
1875}
1876
1877
1878void Assembler::fneg(const FPRegister& fd,
1879 const FPRegister& fn) {
1880 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1881 FPDataProcessing1Source(fd, fn, FNEG);
1882}
1883
1884
1885void Assembler::fsqrt(const FPRegister& fd,
1886 const FPRegister& fn) {
1887 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1888 FPDataProcessing1Source(fd, fn, FSQRT);
1889}
1890
1891
1892void Assembler::frinta(const FPRegister& fd,
1893 const FPRegister& fn) {
1894 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1895 FPDataProcessing1Source(fd, fn, FRINTA);
1896}
1897
1898
1899void Assembler::frintm(const FPRegister& fd,
1900 const FPRegister& fn) {
1901 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1902 FPDataProcessing1Source(fd, fn, FRINTM);
1903}
1904
1905
1906void Assembler::frintn(const FPRegister& fd,
1907 const FPRegister& fn) {
1908 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1909 FPDataProcessing1Source(fd, fn, FRINTN);
1910}
1911
1912
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001913void Assembler::frintp(const FPRegister& fd, const FPRegister& fn) {
1914 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1915 FPDataProcessing1Source(fd, fn, FRINTP);
1916}
1917
1918
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001919void Assembler::frintz(const FPRegister& fd,
1920 const FPRegister& fn) {
1921 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1922 FPDataProcessing1Source(fd, fn, FRINTZ);
1923}
1924
1925
1926void Assembler::fcmp(const FPRegister& fn,
1927 const FPRegister& fm) {
1928 DCHECK(fn.SizeInBits() == fm.SizeInBits());
1929 Emit(FPType(fn) | FCMP | Rm(fm) | Rn(fn));
1930}
1931
1932
1933void Assembler::fcmp(const FPRegister& fn,
1934 double value) {
1935 USE(value);
1936 // Although the fcmp instruction can strictly only take an immediate value of
1937 // +0.0, we don't need to check for -0.0 because the sign of 0.0 doesn't
1938 // affect the result of the comparison.
1939 DCHECK(value == 0.0);
1940 Emit(FPType(fn) | FCMP_zero | Rn(fn));
1941}
1942
1943
1944void Assembler::fccmp(const FPRegister& fn,
1945 const FPRegister& fm,
1946 StatusFlags nzcv,
1947 Condition cond) {
1948 DCHECK(fn.SizeInBits() == fm.SizeInBits());
1949 Emit(FPType(fn) | FCCMP | Rm(fm) | Cond(cond) | Rn(fn) | Nzcv(nzcv));
1950}
1951
1952
1953void Assembler::fcsel(const FPRegister& fd,
1954 const FPRegister& fn,
1955 const FPRegister& fm,
1956 Condition cond) {
1957 DCHECK(fd.SizeInBits() == fn.SizeInBits());
1958 DCHECK(fd.SizeInBits() == fm.SizeInBits());
1959 Emit(FPType(fd) | FCSEL | Rm(fm) | Cond(cond) | Rn(fn) | Rd(fd));
1960}
1961
1962
1963void Assembler::FPConvertToInt(const Register& rd,
1964 const FPRegister& fn,
1965 FPIntegerConvertOp op) {
1966 Emit(SF(rd) | FPType(fn) | op | Rn(fn) | Rd(rd));
1967}
1968
1969
1970void Assembler::fcvt(const FPRegister& fd,
1971 const FPRegister& fn) {
1972 if (fd.Is64Bits()) {
1973 // Convert float to double.
1974 DCHECK(fn.Is32Bits());
1975 FPDataProcessing1Source(fd, fn, FCVT_ds);
1976 } else {
1977 // Convert double to float.
1978 DCHECK(fn.Is64Bits());
1979 FPDataProcessing1Source(fd, fn, FCVT_sd);
1980 }
1981}
1982
1983
1984void Assembler::fcvtau(const Register& rd, const FPRegister& fn) {
1985 FPConvertToInt(rd, fn, FCVTAU);
1986}
1987
1988
1989void Assembler::fcvtas(const Register& rd, const FPRegister& fn) {
1990 FPConvertToInt(rd, fn, FCVTAS);
1991}
1992
1993
1994void Assembler::fcvtmu(const Register& rd, const FPRegister& fn) {
1995 FPConvertToInt(rd, fn, FCVTMU);
1996}
1997
1998
1999void Assembler::fcvtms(const Register& rd, const FPRegister& fn) {
2000 FPConvertToInt(rd, fn, FCVTMS);
2001}
2002
2003
2004void Assembler::fcvtnu(const Register& rd, const FPRegister& fn) {
2005 FPConvertToInt(rd, fn, FCVTNU);
2006}
2007
2008
2009void Assembler::fcvtns(const Register& rd, const FPRegister& fn) {
2010 FPConvertToInt(rd, fn, FCVTNS);
2011}
2012
2013
2014void Assembler::fcvtzu(const Register& rd, const FPRegister& fn) {
2015 FPConvertToInt(rd, fn, FCVTZU);
2016}
2017
2018
2019void Assembler::fcvtzs(const Register& rd, const FPRegister& fn) {
2020 FPConvertToInt(rd, fn, FCVTZS);
2021}
2022
2023
2024void Assembler::scvtf(const FPRegister& fd,
2025 const Register& rn,
2026 unsigned fbits) {
2027 if (fbits == 0) {
2028 Emit(SF(rn) | FPType(fd) | SCVTF | Rn(rn) | Rd(fd));
2029 } else {
2030 Emit(SF(rn) | FPType(fd) | SCVTF_fixed | FPScale(64 - fbits) | Rn(rn) |
2031 Rd(fd));
2032 }
2033}
2034
2035
2036void Assembler::ucvtf(const FPRegister& fd,
2037 const Register& rn,
2038 unsigned fbits) {
2039 if (fbits == 0) {
2040 Emit(SF(rn) | FPType(fd) | UCVTF | Rn(rn) | Rd(fd));
2041 } else {
2042 Emit(SF(rn) | FPType(fd) | UCVTF_fixed | FPScale(64 - fbits) | Rn(rn) |
2043 Rd(fd));
2044 }
2045}
2046
2047
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002048void Assembler::dcptr(Label* label) {
2049 RecordRelocInfo(RelocInfo::INTERNAL_REFERENCE);
2050 if (label->is_bound()) {
2051 // The label is bound, so it does not need to be updated and the internal
2052 // reference should be emitted.
2053 //
2054 // In this case, label->pos() returns the offset of the label from the
2055 // start of the buffer.
2056 internal_reference_positions_.push_back(pc_offset());
2057 dc64(reinterpret_cast<uintptr_t>(buffer_ + label->pos()));
2058 } else {
2059 int32_t offset;
2060 if (label->is_linked()) {
2061 // The label is linked, so the internal reference should be added
2062 // onto the end of the label's link chain.
2063 //
2064 // In this case, label->pos() returns the offset of the last linked
2065 // instruction from the start of the buffer.
2066 offset = label->pos() - pc_offset();
2067 DCHECK(offset != kStartOfLabelLinkChain);
2068 } else {
2069 // The label is unused, so it now becomes linked and the internal
2070 // reference is at the start of the new link chain.
2071 offset = kStartOfLabelLinkChain;
2072 }
2073 // The instruction at pc is now the last link in the label's chain.
2074 label->link_to(pc_offset());
2075
2076 // Traditionally the offset to the previous instruction in the chain is
2077 // encoded in the instruction payload (e.g. branch range) but internal
2078 // references are not instructions so while unbound they are encoded as
2079 // two consecutive brk instructions. The two 16-bit immediates are used
2080 // to encode the offset.
2081 offset >>= kInstructionSizeLog2;
2082 DCHECK(is_int32(offset));
2083 uint32_t high16 = unsigned_bitextract_32(31, 16, offset);
2084 uint32_t low16 = unsigned_bitextract_32(15, 0, offset);
2085
2086 brk(high16);
2087 brk(low16);
2088 }
2089}
2090
2091
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002092// Note:
2093// Below, a difference in case for the same letter indicates a
2094// negated bit.
2095// If b is 1, then B is 0.
2096Instr Assembler::ImmFP32(float imm) {
2097 DCHECK(IsImmFP32(imm));
2098 // bits: aBbb.bbbc.defg.h000.0000.0000.0000.0000
2099 uint32_t bits = float_to_rawbits(imm);
2100 // bit7: a000.0000
2101 uint32_t bit7 = ((bits >> 31) & 0x1) << 7;
2102 // bit6: 0b00.0000
2103 uint32_t bit6 = ((bits >> 29) & 0x1) << 6;
2104 // bit5_to_0: 00cd.efgh
2105 uint32_t bit5_to_0 = (bits >> 19) & 0x3f;
2106
2107 return (bit7 | bit6 | bit5_to_0) << ImmFP_offset;
2108}
2109
2110
2111Instr Assembler::ImmFP64(double imm) {
2112 DCHECK(IsImmFP64(imm));
2113 // bits: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
2114 // 0000.0000.0000.0000.0000.0000.0000.0000
2115 uint64_t bits = double_to_rawbits(imm);
2116 // bit7: a000.0000
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002117 uint64_t bit7 = ((bits >> 63) & 0x1) << 7;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002118 // bit6: 0b00.0000
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002119 uint64_t bit6 = ((bits >> 61) & 0x1) << 6;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002120 // bit5_to_0: 00cd.efgh
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002121 uint64_t bit5_to_0 = (bits >> 48) & 0x3f;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002122
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002123 return static_cast<Instr>((bit7 | bit6 | bit5_to_0) << ImmFP_offset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002124}
2125
2126
2127// Code generation helpers.
2128void Assembler::MoveWide(const Register& rd,
2129 uint64_t imm,
2130 int shift,
2131 MoveWideImmediateOp mov_op) {
2132 // Ignore the top 32 bits of an immediate if we're moving to a W register.
2133 if (rd.Is32Bits()) {
2134 // Check that the top 32 bits are zero (a positive 32-bit number) or top
2135 // 33 bits are one (a negative 32-bit number, sign extended to 64 bits).
2136 DCHECK(((imm >> kWRegSizeInBits) == 0) ||
2137 ((imm >> (kWRegSizeInBits - 1)) == 0x1ffffffff));
2138 imm &= kWRegMask;
2139 }
2140
2141 if (shift >= 0) {
2142 // Explicit shift specified.
2143 DCHECK((shift == 0) || (shift == 16) || (shift == 32) || (shift == 48));
2144 DCHECK(rd.Is64Bits() || (shift == 0) || (shift == 16));
2145 shift /= 16;
2146 } else {
2147 // Calculate a new immediate and shift combination to encode the immediate
2148 // argument.
2149 shift = 0;
2150 if ((imm & ~0xffffUL) == 0) {
2151 // Nothing to do.
2152 } else if ((imm & ~(0xffffUL << 16)) == 0) {
2153 imm >>= 16;
2154 shift = 1;
2155 } else if ((imm & ~(0xffffUL << 32)) == 0) {
2156 DCHECK(rd.Is64Bits());
2157 imm >>= 32;
2158 shift = 2;
2159 } else if ((imm & ~(0xffffUL << 48)) == 0) {
2160 DCHECK(rd.Is64Bits());
2161 imm >>= 48;
2162 shift = 3;
2163 }
2164 }
2165
2166 DCHECK(is_uint16(imm));
2167
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002168 Emit(SF(rd) | MoveWideImmediateFixed | mov_op | Rd(rd) |
2169 ImmMoveWide(static_cast<int>(imm)) | ShiftMoveWide(shift));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002170}
2171
2172
2173void Assembler::AddSub(const Register& rd,
2174 const Register& rn,
2175 const Operand& operand,
2176 FlagsUpdate S,
2177 AddSubOp op) {
2178 DCHECK(rd.SizeInBits() == rn.SizeInBits());
2179 DCHECK(!operand.NeedsRelocation(this));
2180 if (operand.IsImmediate()) {
2181 int64_t immediate = operand.ImmediateValue();
2182 DCHECK(IsImmAddSub(immediate));
2183 Instr dest_reg = (S == SetFlags) ? Rd(rd) : RdSP(rd);
2184 Emit(SF(rd) | AddSubImmediateFixed | op | Flags(S) |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002185 ImmAddSub(static_cast<int>(immediate)) | dest_reg | RnSP(rn));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002186 } else if (operand.IsShiftedRegister()) {
2187 DCHECK(operand.reg().SizeInBits() == rd.SizeInBits());
2188 DCHECK(operand.shift() != ROR);
2189
2190 // For instructions of the form:
2191 // add/sub wsp, <Wn>, <Wm> [, LSL #0-3 ]
2192 // add/sub <Wd>, wsp, <Wm> [, LSL #0-3 ]
2193 // add/sub wsp, wsp, <Wm> [, LSL #0-3 ]
2194 // adds/subs <Wd>, wsp, <Wm> [, LSL #0-3 ]
2195 // or their 64-bit register equivalents, convert the operand from shifted to
2196 // extended register mode, and emit an add/sub extended instruction.
2197 if (rn.IsSP() || rd.IsSP()) {
2198 DCHECK(!(rd.IsSP() && (S == SetFlags)));
2199 DataProcExtendedRegister(rd, rn, operand.ToExtendedRegister(), S,
2200 AddSubExtendedFixed | op);
2201 } else {
2202 DataProcShiftedRegister(rd, rn, operand, S, AddSubShiftedFixed | op);
2203 }
2204 } else {
2205 DCHECK(operand.IsExtendedRegister());
2206 DataProcExtendedRegister(rd, rn, operand, S, AddSubExtendedFixed | op);
2207 }
2208}
2209
2210
2211void Assembler::AddSubWithCarry(const Register& rd,
2212 const Register& rn,
2213 const Operand& operand,
2214 FlagsUpdate S,
2215 AddSubWithCarryOp op) {
2216 DCHECK(rd.SizeInBits() == rn.SizeInBits());
2217 DCHECK(rd.SizeInBits() == operand.reg().SizeInBits());
2218 DCHECK(operand.IsShiftedRegister() && (operand.shift_amount() == 0));
2219 DCHECK(!operand.NeedsRelocation(this));
2220 Emit(SF(rd) | op | Flags(S) | Rm(operand.reg()) | Rn(rn) | Rd(rd));
2221}
2222
2223
2224void Assembler::hlt(int code) {
2225 DCHECK(is_uint16(code));
2226 Emit(HLT | ImmException(code));
2227}
2228
2229
2230void Assembler::brk(int code) {
2231 DCHECK(is_uint16(code));
2232 Emit(BRK | ImmException(code));
2233}
2234
2235
2236void Assembler::EmitStringData(const char* string) {
2237 size_t len = strlen(string) + 1;
2238 DCHECK(RoundUp(len, kInstructionSize) <= static_cast<size_t>(kGap));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002239 EmitData(string, static_cast<int>(len));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002240 // Pad with NULL characters until pc_ is aligned.
2241 const char pad[] = {'\0', '\0', '\0', '\0'};
2242 STATIC_ASSERT(sizeof(pad) == kInstructionSize);
2243 EmitData(pad, RoundUp(pc_offset(), kInstructionSize) - pc_offset());
2244}
2245
2246
2247void Assembler::debug(const char* message, uint32_t code, Instr params) {
2248#ifdef USE_SIMULATOR
2249 // Don't generate simulator specific code if we are building a snapshot, which
2250 // might be run on real hardware.
2251 if (!serializer_enabled()) {
2252 // The arguments to the debug marker need to be contiguous in memory, so
2253 // make sure we don't try to emit pools.
2254 BlockPoolsScope scope(this);
2255
2256 Label start;
2257 bind(&start);
2258
2259 // Refer to instructions-arm64.h for a description of the marker and its
2260 // arguments.
2261 hlt(kImmExceptionIsDebug);
2262 DCHECK(SizeOfCodeGeneratedSince(&start) == kDebugCodeOffset);
2263 dc32(code);
2264 DCHECK(SizeOfCodeGeneratedSince(&start) == kDebugParamsOffset);
2265 dc32(params);
2266 DCHECK(SizeOfCodeGeneratedSince(&start) == kDebugMessageOffset);
2267 EmitStringData(message);
2268 hlt(kImmExceptionIsUnreachable);
2269
2270 return;
2271 }
2272 // Fall through if Serializer is enabled.
2273#endif
2274
2275 if (params & BREAK) {
2276 hlt(kImmExceptionIsDebug);
2277 }
2278}
2279
2280
2281void Assembler::Logical(const Register& rd,
2282 const Register& rn,
2283 const Operand& operand,
2284 LogicalOp op) {
2285 DCHECK(rd.SizeInBits() == rn.SizeInBits());
2286 DCHECK(!operand.NeedsRelocation(this));
2287 if (operand.IsImmediate()) {
2288 int64_t immediate = operand.ImmediateValue();
2289 unsigned reg_size = rd.SizeInBits();
2290
2291 DCHECK(immediate != 0);
2292 DCHECK(immediate != -1);
2293 DCHECK(rd.Is64Bits() || is_uint32(immediate));
2294
2295 // If the operation is NOT, invert the operation and immediate.
2296 if ((op & NOT) == NOT) {
2297 op = static_cast<LogicalOp>(op & ~NOT);
2298 immediate = rd.Is64Bits() ? ~immediate : (~immediate & kWRegMask);
2299 }
2300
2301 unsigned n, imm_s, imm_r;
2302 if (IsImmLogical(immediate, reg_size, &n, &imm_s, &imm_r)) {
2303 // Immediate can be encoded in the instruction.
2304 LogicalImmediate(rd, rn, n, imm_s, imm_r, op);
2305 } else {
2306 // This case is handled in the macro assembler.
2307 UNREACHABLE();
2308 }
2309 } else {
2310 DCHECK(operand.IsShiftedRegister());
2311 DCHECK(operand.reg().SizeInBits() == rd.SizeInBits());
2312 Instr dp_op = static_cast<Instr>(op | LogicalShiftedFixed);
2313 DataProcShiftedRegister(rd, rn, operand, LeaveFlags, dp_op);
2314 }
2315}
2316
2317
2318void Assembler::LogicalImmediate(const Register& rd,
2319 const Register& rn,
2320 unsigned n,
2321 unsigned imm_s,
2322 unsigned imm_r,
2323 LogicalOp op) {
2324 unsigned reg_size = rd.SizeInBits();
2325 Instr dest_reg = (op == ANDS) ? Rd(rd) : RdSP(rd);
2326 Emit(SF(rd) | LogicalImmediateFixed | op | BitN(n, reg_size) |
2327 ImmSetBits(imm_s, reg_size) | ImmRotate(imm_r, reg_size) | dest_reg |
2328 Rn(rn));
2329}
2330
2331
2332void Assembler::ConditionalCompare(const Register& rn,
2333 const Operand& operand,
2334 StatusFlags nzcv,
2335 Condition cond,
2336 ConditionalCompareOp op) {
2337 Instr ccmpop;
2338 DCHECK(!operand.NeedsRelocation(this));
2339 if (operand.IsImmediate()) {
2340 int64_t immediate = operand.ImmediateValue();
2341 DCHECK(IsImmConditionalCompare(immediate));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002342 ccmpop = ConditionalCompareImmediateFixed | op |
2343 ImmCondCmp(static_cast<unsigned>(immediate));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002344 } else {
2345 DCHECK(operand.IsShiftedRegister() && (operand.shift_amount() == 0));
2346 ccmpop = ConditionalCompareRegisterFixed | op | Rm(operand.reg());
2347 }
2348 Emit(SF(rn) | ccmpop | Cond(cond) | Rn(rn) | Nzcv(nzcv));
2349}
2350
2351
2352void Assembler::DataProcessing1Source(const Register& rd,
2353 const Register& rn,
2354 DataProcessing1SourceOp op) {
2355 DCHECK(rd.SizeInBits() == rn.SizeInBits());
2356 Emit(SF(rn) | op | Rn(rn) | Rd(rd));
2357}
2358
2359
2360void Assembler::FPDataProcessing1Source(const FPRegister& fd,
2361 const FPRegister& fn,
2362 FPDataProcessing1SourceOp op) {
2363 Emit(FPType(fn) | op | Rn(fn) | Rd(fd));
2364}
2365
2366
2367void Assembler::FPDataProcessing2Source(const FPRegister& fd,
2368 const FPRegister& fn,
2369 const FPRegister& fm,
2370 FPDataProcessing2SourceOp op) {
2371 DCHECK(fd.SizeInBits() == fn.SizeInBits());
2372 DCHECK(fd.SizeInBits() == fm.SizeInBits());
2373 Emit(FPType(fd) | op | Rm(fm) | Rn(fn) | Rd(fd));
2374}
2375
2376
2377void Assembler::FPDataProcessing3Source(const FPRegister& fd,
2378 const FPRegister& fn,
2379 const FPRegister& fm,
2380 const FPRegister& fa,
2381 FPDataProcessing3SourceOp op) {
2382 DCHECK(AreSameSizeAndType(fd, fn, fm, fa));
2383 Emit(FPType(fd) | op | Rm(fm) | Rn(fn) | Rd(fd) | Ra(fa));
2384}
2385
2386
2387void Assembler::EmitShift(const Register& rd,
2388 const Register& rn,
2389 Shift shift,
2390 unsigned shift_amount) {
2391 switch (shift) {
2392 case LSL:
2393 lsl(rd, rn, shift_amount);
2394 break;
2395 case LSR:
2396 lsr(rd, rn, shift_amount);
2397 break;
2398 case ASR:
2399 asr(rd, rn, shift_amount);
2400 break;
2401 case ROR:
2402 ror(rd, rn, shift_amount);
2403 break;
2404 default:
2405 UNREACHABLE();
2406 }
2407}
2408
2409
2410void Assembler::EmitExtendShift(const Register& rd,
2411 const Register& rn,
2412 Extend extend,
2413 unsigned left_shift) {
2414 DCHECK(rd.SizeInBits() >= rn.SizeInBits());
2415 unsigned reg_size = rd.SizeInBits();
2416 // Use the correct size of register.
2417 Register rn_ = Register::Create(rn.code(), rd.SizeInBits());
2418 // Bits extracted are high_bit:0.
2419 unsigned high_bit = (8 << (extend & 0x3)) - 1;
2420 // Number of bits left in the result that are not introduced by the shift.
2421 unsigned non_shift_bits = (reg_size - left_shift) & (reg_size - 1);
2422
2423 if ((non_shift_bits > high_bit) || (non_shift_bits == 0)) {
2424 switch (extend) {
2425 case UXTB:
2426 case UXTH:
2427 case UXTW: ubfm(rd, rn_, non_shift_bits, high_bit); break;
2428 case SXTB:
2429 case SXTH:
2430 case SXTW: sbfm(rd, rn_, non_shift_bits, high_bit); break;
2431 case UXTX:
2432 case SXTX: {
2433 DCHECK(rn.SizeInBits() == kXRegSizeInBits);
2434 // Nothing to extend. Just shift.
2435 lsl(rd, rn_, left_shift);
2436 break;
2437 }
2438 default: UNREACHABLE();
2439 }
2440 } else {
2441 // No need to extend as the extended bits would be shifted away.
2442 lsl(rd, rn_, left_shift);
2443 }
2444}
2445
2446
2447void Assembler::DataProcShiftedRegister(const Register& rd,
2448 const Register& rn,
2449 const Operand& operand,
2450 FlagsUpdate S,
2451 Instr op) {
2452 DCHECK(operand.IsShiftedRegister());
2453 DCHECK(rn.Is64Bits() || (rn.Is32Bits() && is_uint5(operand.shift_amount())));
2454 DCHECK(!operand.NeedsRelocation(this));
2455 Emit(SF(rd) | op | Flags(S) |
2456 ShiftDP(operand.shift()) | ImmDPShift(operand.shift_amount()) |
2457 Rm(operand.reg()) | Rn(rn) | Rd(rd));
2458}
2459
2460
2461void Assembler::DataProcExtendedRegister(const Register& rd,
2462 const Register& rn,
2463 const Operand& operand,
2464 FlagsUpdate S,
2465 Instr op) {
2466 DCHECK(!operand.NeedsRelocation(this));
2467 Instr dest_reg = (S == SetFlags) ? Rd(rd) : RdSP(rd);
2468 Emit(SF(rd) | op | Flags(S) | Rm(operand.reg()) |
2469 ExtendMode(operand.extend()) | ImmExtendShift(operand.shift_amount()) |
2470 dest_reg | RnSP(rn));
2471}
2472
2473
2474bool Assembler::IsImmAddSub(int64_t immediate) {
2475 return is_uint12(immediate) ||
2476 (is_uint12(immediate >> 12) && ((immediate & 0xfff) == 0));
2477}
2478
2479void Assembler::LoadStore(const CPURegister& rt,
2480 const MemOperand& addr,
2481 LoadStoreOp op) {
2482 Instr memop = op | Rt(rt) | RnSP(addr.base());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002483
2484 if (addr.IsImmediateOffset()) {
2485 LSDataSize size = CalcLSDataSize(op);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002486 if (IsImmLSScaled(addr.offset(), size)) {
2487 int offset = static_cast<int>(addr.offset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002488 // Use the scaled addressing mode.
2489 Emit(LoadStoreUnsignedOffsetFixed | memop |
2490 ImmLSUnsigned(offset >> size));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002491 } else if (IsImmLSUnscaled(addr.offset())) {
2492 int offset = static_cast<int>(addr.offset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002493 // Use the unscaled addressing mode.
2494 Emit(LoadStoreUnscaledOffsetFixed | memop | ImmLS(offset));
2495 } else {
2496 // This case is handled in the macro assembler.
2497 UNREACHABLE();
2498 }
2499 } else if (addr.IsRegisterOffset()) {
2500 Extend ext = addr.extend();
2501 Shift shift = addr.shift();
2502 unsigned shift_amount = addr.shift_amount();
2503
2504 // LSL is encoded in the option field as UXTX.
2505 if (shift == LSL) {
2506 ext = UXTX;
2507 }
2508
2509 // Shifts are encoded in one bit, indicating a left shift by the memory
2510 // access size.
2511 DCHECK((shift_amount == 0) ||
2512 (shift_amount == static_cast<unsigned>(CalcLSDataSize(op))));
2513 Emit(LoadStoreRegisterOffsetFixed | memop | Rm(addr.regoffset()) |
2514 ExtendMode(ext) | ImmShiftLS((shift_amount > 0) ? 1 : 0));
2515 } else {
2516 // Pre-index and post-index modes.
2517 DCHECK(!rt.Is(addr.base()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002518 if (IsImmLSUnscaled(addr.offset())) {
2519 int offset = static_cast<int>(addr.offset());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002520 if (addr.IsPreIndex()) {
2521 Emit(LoadStorePreIndexFixed | memop | ImmLS(offset));
2522 } else {
2523 DCHECK(addr.IsPostIndex());
2524 Emit(LoadStorePostIndexFixed | memop | ImmLS(offset));
2525 }
2526 } else {
2527 // This case is handled in the macro assembler.
2528 UNREACHABLE();
2529 }
2530 }
2531}
2532
2533
2534bool Assembler::IsImmLSUnscaled(int64_t offset) {
2535 return is_int9(offset);
2536}
2537
2538
2539bool Assembler::IsImmLSScaled(int64_t offset, LSDataSize size) {
2540 bool offset_is_size_multiple = (((offset >> size) << size) == offset);
2541 return offset_is_size_multiple && is_uint12(offset >> size);
2542}
2543
2544
2545bool Assembler::IsImmLSPair(int64_t offset, LSDataSize size) {
2546 bool offset_is_size_multiple = (((offset >> size) << size) == offset);
2547 return offset_is_size_multiple && is_int7(offset >> size);
2548}
2549
2550
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002551bool Assembler::IsImmLLiteral(int64_t offset) {
2552 int inst_size = static_cast<int>(kInstructionSizeLog2);
2553 bool offset_is_inst_multiple =
2554 (((offset >> inst_size) << inst_size) == offset);
2555 return offset_is_inst_multiple && is_intn(offset, ImmLLiteral_width);
2556}
2557
2558
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002559// Test if a given value can be encoded in the immediate field of a logical
2560// instruction.
2561// If it can be encoded, the function returns true, and values pointed to by n,
2562// imm_s and imm_r are updated with immediates encoded in the format required
2563// by the corresponding fields in the logical instruction.
2564// If it can not be encoded, the function returns false, and the values pointed
2565// to by n, imm_s and imm_r are undefined.
2566bool Assembler::IsImmLogical(uint64_t value,
2567 unsigned width,
2568 unsigned* n,
2569 unsigned* imm_s,
2570 unsigned* imm_r) {
2571 DCHECK((n != NULL) && (imm_s != NULL) && (imm_r != NULL));
2572 DCHECK((width == kWRegSizeInBits) || (width == kXRegSizeInBits));
2573
2574 bool negate = false;
2575
2576 // Logical immediates are encoded using parameters n, imm_s and imm_r using
2577 // the following table:
2578 //
2579 // N imms immr size S R
2580 // 1 ssssss rrrrrr 64 UInt(ssssss) UInt(rrrrrr)
2581 // 0 0sssss xrrrrr 32 UInt(sssss) UInt(rrrrr)
2582 // 0 10ssss xxrrrr 16 UInt(ssss) UInt(rrrr)
2583 // 0 110sss xxxrrr 8 UInt(sss) UInt(rrr)
2584 // 0 1110ss xxxxrr 4 UInt(ss) UInt(rr)
2585 // 0 11110s xxxxxr 2 UInt(s) UInt(r)
2586 // (s bits must not be all set)
2587 //
2588 // A pattern is constructed of size bits, where the least significant S+1 bits
2589 // are set. The pattern is rotated right by R, and repeated across a 32 or
2590 // 64-bit value, depending on destination register width.
2591 //
2592 // Put another way: the basic format of a logical immediate is a single
2593 // contiguous stretch of 1 bits, repeated across the whole word at intervals
2594 // given by a power of 2. To identify them quickly, we first locate the
2595 // lowest stretch of 1 bits, then the next 1 bit above that; that combination
2596 // is different for every logical immediate, so it gives us all the
2597 // information we need to identify the only logical immediate that our input
2598 // could be, and then we simply check if that's the value we actually have.
2599 //
2600 // (The rotation parameter does give the possibility of the stretch of 1 bits
2601 // going 'round the end' of the word. To deal with that, we observe that in
2602 // any situation where that happens the bitwise NOT of the value is also a
2603 // valid logical immediate. So we simply invert the input whenever its low bit
2604 // is set, and then we know that the rotated case can't arise.)
2605
2606 if (value & 1) {
2607 // If the low bit is 1, negate the value, and set a flag to remember that we
2608 // did (so that we can adjust the return values appropriately).
2609 negate = true;
2610 value = ~value;
2611 }
2612
2613 if (width == kWRegSizeInBits) {
2614 // To handle 32-bit logical immediates, the very easiest thing is to repeat
2615 // the input value twice to make a 64-bit word. The correct encoding of that
2616 // as a logical immediate will also be the correct encoding of the 32-bit
2617 // value.
2618
2619 // The most-significant 32 bits may not be zero (ie. negate is true) so
2620 // shift the value left before duplicating it.
2621 value <<= kWRegSizeInBits;
2622 value |= value >> kWRegSizeInBits;
2623 }
2624
2625 // The basic analysis idea: imagine our input word looks like this.
2626 //
2627 // 0011111000111110001111100011111000111110001111100011111000111110
2628 // c b a
2629 // |<--d-->|
2630 //
2631 // We find the lowest set bit (as an actual power-of-2 value, not its index)
2632 // and call it a. Then we add a to our original number, which wipes out the
2633 // bottommost stretch of set bits and replaces it with a 1 carried into the
2634 // next zero bit. Then we look for the new lowest set bit, which is in
2635 // position b, and subtract it, so now our number is just like the original
2636 // but with the lowest stretch of set bits completely gone. Now we find the
2637 // lowest set bit again, which is position c in the diagram above. Then we'll
2638 // measure the distance d between bit positions a and c (using CLZ), and that
2639 // tells us that the only valid logical immediate that could possibly be equal
2640 // to this number is the one in which a stretch of bits running from a to just
2641 // below b is replicated every d bits.
2642 uint64_t a = LargestPowerOf2Divisor(value);
2643 uint64_t value_plus_a = value + a;
2644 uint64_t b = LargestPowerOf2Divisor(value_plus_a);
2645 uint64_t value_plus_a_minus_b = value_plus_a - b;
2646 uint64_t c = LargestPowerOf2Divisor(value_plus_a_minus_b);
2647
2648 int d, clz_a, out_n;
2649 uint64_t mask;
2650
2651 if (c != 0) {
2652 // The general case, in which there is more than one stretch of set bits.
2653 // Compute the repeat distance d, and set up a bitmask covering the basic
2654 // unit of repetition (i.e. a word with the bottom d bits set). Also, in all
2655 // of these cases the N bit of the output will be zero.
2656 clz_a = CountLeadingZeros(a, kXRegSizeInBits);
2657 int clz_c = CountLeadingZeros(c, kXRegSizeInBits);
2658 d = clz_a - clz_c;
2659 mask = ((V8_UINT64_C(1) << d) - 1);
2660 out_n = 0;
2661 } else {
2662 // Handle degenerate cases.
2663 //
2664 // If any of those 'find lowest set bit' operations didn't find a set bit at
2665 // all, then the word will have been zero thereafter, so in particular the
2666 // last lowest_set_bit operation will have returned zero. So we can test for
2667 // all the special case conditions in one go by seeing if c is zero.
2668 if (a == 0) {
2669 // The input was zero (or all 1 bits, which will come to here too after we
2670 // inverted it at the start of the function), for which we just return
2671 // false.
2672 return false;
2673 } else {
2674 // Otherwise, if c was zero but a was not, then there's just one stretch
2675 // of set bits in our word, meaning that we have the trivial case of
2676 // d == 64 and only one 'repetition'. Set up all the same variables as in
2677 // the general case above, and set the N bit in the output.
2678 clz_a = CountLeadingZeros(a, kXRegSizeInBits);
2679 d = 64;
2680 mask = ~V8_UINT64_C(0);
2681 out_n = 1;
2682 }
2683 }
2684
2685 // If the repeat period d is not a power of two, it can't be encoded.
2686 if (!IS_POWER_OF_TWO(d)) {
2687 return false;
2688 }
2689
2690 if (((b - a) & ~mask) != 0) {
2691 // If the bit stretch (b - a) does not fit within the mask derived from the
2692 // repeat period, then fail.
2693 return false;
2694 }
2695
2696 // The only possible option is b - a repeated every d bits. Now we're going to
2697 // actually construct the valid logical immediate derived from that
2698 // specification, and see if it equals our original input.
2699 //
2700 // To repeat a value every d bits, we multiply it by a number of the form
2701 // (1 + 2^d + 2^(2d) + ...), i.e. 0x0001000100010001 or similar. These can
2702 // be derived using a table lookup on CLZ(d).
2703 static const uint64_t multipliers[] = {
2704 0x0000000000000001UL,
2705 0x0000000100000001UL,
2706 0x0001000100010001UL,
2707 0x0101010101010101UL,
2708 0x1111111111111111UL,
2709 0x5555555555555555UL,
2710 };
2711 int multiplier_idx = CountLeadingZeros(d, kXRegSizeInBits) - 57;
2712 // Ensure that the index to the multipliers array is within bounds.
2713 DCHECK((multiplier_idx >= 0) &&
2714 (static_cast<size_t>(multiplier_idx) < arraysize(multipliers)));
2715 uint64_t multiplier = multipliers[multiplier_idx];
2716 uint64_t candidate = (b - a) * multiplier;
2717
2718 if (value != candidate) {
2719 // The candidate pattern doesn't match our input value, so fail.
2720 return false;
2721 }
2722
2723 // We have a match! This is a valid logical immediate, so now we have to
2724 // construct the bits and pieces of the instruction encoding that generates
2725 // it.
2726
2727 // Count the set bits in our basic stretch. The special case of clz(0) == -1
2728 // makes the answer come out right for stretches that reach the very top of
2729 // the word (e.g. numbers like 0xffffc00000000000).
2730 int clz_b = (b == 0) ? -1 : CountLeadingZeros(b, kXRegSizeInBits);
2731 int s = clz_a - clz_b;
2732
2733 // Decide how many bits to rotate right by, to put the low bit of that basic
2734 // stretch in position a.
2735 int r;
2736 if (negate) {
2737 // If we inverted the input right at the start of this function, here's
2738 // where we compensate: the number of set bits becomes the number of clear
2739 // bits, and the rotation count is based on position b rather than position
2740 // a (since b is the location of the 'lowest' 1 bit after inversion).
2741 s = d - s;
2742 r = (clz_b + 1) & (d - 1);
2743 } else {
2744 r = (clz_a + 1) & (d - 1);
2745 }
2746
2747 // Now we're done, except for having to encode the S output in such a way that
2748 // it gives both the number of set bits and the length of the repeated
2749 // segment. The s field is encoded like this:
2750 //
2751 // imms size S
2752 // ssssss 64 UInt(ssssss)
2753 // 0sssss 32 UInt(sssss)
2754 // 10ssss 16 UInt(ssss)
2755 // 110sss 8 UInt(sss)
2756 // 1110ss 4 UInt(ss)
2757 // 11110s 2 UInt(s)
2758 //
2759 // So we 'or' (-d << 1) with our computed s to form imms.
2760 *n = out_n;
2761 *imm_s = ((-d << 1) | (s - 1)) & 0x3f;
2762 *imm_r = r;
2763
2764 return true;
2765}
2766
2767
2768bool Assembler::IsImmConditionalCompare(int64_t immediate) {
2769 return is_uint5(immediate);
2770}
2771
2772
2773bool Assembler::IsImmFP32(float imm) {
2774 // Valid values will have the form:
2775 // aBbb.bbbc.defg.h000.0000.0000.0000.0000
2776 uint32_t bits = float_to_rawbits(imm);
2777 // bits[19..0] are cleared.
2778 if ((bits & 0x7ffff) != 0) {
2779 return false;
2780 }
2781
2782 // bits[29..25] are all set or all cleared.
2783 uint32_t b_pattern = (bits >> 16) & 0x3e00;
2784 if (b_pattern != 0 && b_pattern != 0x3e00) {
2785 return false;
2786 }
2787
2788 // bit[30] and bit[29] are opposite.
2789 if (((bits ^ (bits << 1)) & 0x40000000) == 0) {
2790 return false;
2791 }
2792
2793 return true;
2794}
2795
2796
2797bool Assembler::IsImmFP64(double imm) {
2798 // Valid values will have the form:
2799 // aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
2800 // 0000.0000.0000.0000.0000.0000.0000.0000
2801 uint64_t bits = double_to_rawbits(imm);
2802 // bits[47..0] are cleared.
2803 if ((bits & 0xffffffffffffL) != 0) {
2804 return false;
2805 }
2806
2807 // bits[61..54] are all set or all cleared.
2808 uint32_t b_pattern = (bits >> 48) & 0x3fc0;
2809 if (b_pattern != 0 && b_pattern != 0x3fc0) {
2810 return false;
2811 }
2812
2813 // bit[62] and bit[61] are opposite.
2814 if (((bits ^ (bits << 1)) & 0x4000000000000000L) == 0) {
2815 return false;
2816 }
2817
2818 return true;
2819}
2820
2821
2822void Assembler::GrowBuffer() {
2823 if (!own_buffer_) FATAL("external code buffer is too small");
2824
2825 // Compute new buffer size.
2826 CodeDesc desc; // the new buffer
2827 if (buffer_size_ < 1 * MB) {
2828 desc.buffer_size = 2 * buffer_size_;
2829 } else {
2830 desc.buffer_size = buffer_size_ + 1 * MB;
2831 }
2832 CHECK_GT(desc.buffer_size, 0); // No overflow.
2833
2834 byte* buffer = reinterpret_cast<byte*>(buffer_);
2835
2836 // Set up new buffer.
2837 desc.buffer = NewArray<byte>(desc.buffer_size);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002838 desc.origin = this;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002839
2840 desc.instr_size = pc_offset();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002841 desc.reloc_size =
2842 static_cast<int>((buffer + buffer_size_) - reloc_info_writer.pos());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002843
2844 // Copy the data.
2845 intptr_t pc_delta = desc.buffer - buffer;
2846 intptr_t rc_delta = (desc.buffer + desc.buffer_size) -
2847 (buffer + buffer_size_);
2848 memmove(desc.buffer, buffer, desc.instr_size);
2849 memmove(reloc_info_writer.pos() + rc_delta,
2850 reloc_info_writer.pos(), desc.reloc_size);
2851
2852 // Switch buffers.
2853 DeleteArray(buffer_);
2854 buffer_ = desc.buffer;
2855 buffer_size_ = desc.buffer_size;
2856 pc_ = reinterpret_cast<byte*>(pc_) + pc_delta;
2857 reloc_info_writer.Reposition(reloc_info_writer.pos() + rc_delta,
2858 reloc_info_writer.last_pc() + pc_delta);
2859
2860 // None of our relocation types are pc relative pointing outside the code
2861 // buffer nor pc absolute pointing inside the code buffer, so there is no need
2862 // to relocate any emitted relocation entries.
2863
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002864 // Relocate internal references.
2865 for (auto pos : internal_reference_positions_) {
2866 intptr_t* p = reinterpret_cast<intptr_t*>(buffer_ + pos);
2867 *p += pc_delta;
2868 }
2869
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002870 // Pending relocation entries are also relative, no need to relocate.
2871}
2872
2873
2874void Assembler::RecordRelocInfo(RelocInfo::Mode rmode, intptr_t data) {
2875 // We do not try to reuse pool constants.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002876 RelocInfo rinfo(isolate(), reinterpret_cast<byte*>(pc_), rmode, data, NULL);
2877 if (((rmode >= RelocInfo::COMMENT) &&
Ben Murdochda12d292016-06-02 14:46:10 +01002878 (rmode <= RelocInfo::DEBUG_BREAK_SLOT_AT_TAIL_CALL)) ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002879 (rmode == RelocInfo::INTERNAL_REFERENCE) ||
2880 (rmode == RelocInfo::CONST_POOL) || (rmode == RelocInfo::VENEER_POOL) ||
2881 (rmode == RelocInfo::DEOPT_REASON) ||
2882 (rmode == RelocInfo::GENERATOR_CONTINUATION)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002883 // Adjust code for new modes.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002884 DCHECK(RelocInfo::IsDebugBreakSlot(rmode) || RelocInfo::IsComment(rmode) ||
2885 RelocInfo::IsDeoptReason(rmode) || RelocInfo::IsPosition(rmode) ||
2886 RelocInfo::IsInternalReference(rmode) ||
2887 RelocInfo::IsConstPool(rmode) || RelocInfo::IsVeneerPool(rmode) ||
2888 RelocInfo::IsGeneratorContinuation(rmode));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002889 // These modes do not need an entry in the constant pool.
2890 } else {
2891 constpool_.RecordEntry(data, rmode);
2892 // Make sure the constant pool is not emitted in place of the next
2893 // instruction for which we just recorded relocation info.
2894 BlockConstPoolFor(1);
2895 }
2896
2897 if (!RelocInfo::IsNone(rmode)) {
2898 // Don't record external references unless the heap will be serialized.
2899 if (rmode == RelocInfo::EXTERNAL_REFERENCE &&
2900 !serializer_enabled() && !emit_debug_code()) {
2901 return;
2902 }
2903 DCHECK(buffer_space() >= kMaxRelocSize); // too late to grow buffer here
2904 if (rmode == RelocInfo::CODE_TARGET_WITH_ID) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002905 RelocInfo reloc_info_with_ast_id(isolate(), reinterpret_cast<byte*>(pc_),
2906 rmode, RecordedAstId().ToInt(), NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002907 ClearRecordedAstId();
2908 reloc_info_writer.Write(&reloc_info_with_ast_id);
2909 } else {
2910 reloc_info_writer.Write(&rinfo);
2911 }
2912 }
2913}
2914
2915
2916void Assembler::BlockConstPoolFor(int instructions) {
2917 int pc_limit = pc_offset() + instructions * kInstructionSize;
2918 if (no_const_pool_before_ < pc_limit) {
2919 no_const_pool_before_ = pc_limit;
2920 // Make sure the pool won't be blocked for too long.
2921 DCHECK(pc_limit < constpool_.MaxPcOffset());
2922 }
2923
2924 if (next_constant_pool_check_ < no_const_pool_before_) {
2925 next_constant_pool_check_ = no_const_pool_before_;
2926 }
2927}
2928
2929
2930void Assembler::CheckConstPool(bool force_emit, bool require_jump) {
2931 // Some short sequence of instruction mustn't be broken up by constant pool
2932 // emission, such sequences are protected by calls to BlockConstPoolFor and
2933 // BlockConstPoolScope.
2934 if (is_const_pool_blocked()) {
2935 // Something is wrong if emission is forced and blocked at the same time.
2936 DCHECK(!force_emit);
2937 return;
2938 }
2939
2940 // There is nothing to do if there are no pending constant pool entries.
2941 if (constpool_.IsEmpty()) {
2942 // Calculate the offset of the next check.
2943 SetNextConstPoolCheckIn(kCheckConstPoolInterval);
2944 return;
2945 }
2946
2947 // We emit a constant pool when:
2948 // * requested to do so by parameter force_emit (e.g. after each function).
2949 // * the distance to the first instruction accessing the constant pool is
2950 // kApproxMaxDistToConstPool or more.
2951 // * the number of entries in the pool is kApproxMaxPoolEntryCount or more.
2952 int dist = constpool_.DistanceToFirstUse();
2953 int count = constpool_.EntryCount();
2954 if (!force_emit &&
2955 (dist < kApproxMaxDistToConstPool) &&
2956 (count < kApproxMaxPoolEntryCount)) {
2957 return;
2958 }
2959
2960
2961 // Emit veneers for branches that would go out of range during emission of the
2962 // constant pool.
2963 int worst_case_size = constpool_.WorstCaseSize();
2964 CheckVeneerPool(false, require_jump,
2965 kVeneerDistanceMargin + worst_case_size);
2966
2967 // Check that the code buffer is large enough before emitting the constant
2968 // pool (this includes the gap to the relocation information).
2969 int needed_space = worst_case_size + kGap + 1 * kInstructionSize;
2970 while (buffer_space() <= needed_space) {
2971 GrowBuffer();
2972 }
2973
2974 Label size_check;
2975 bind(&size_check);
2976 constpool_.Emit(require_jump);
2977 DCHECK(SizeOfCodeGeneratedSince(&size_check) <=
2978 static_cast<unsigned>(worst_case_size));
2979
2980 // Since a constant pool was just emitted, move the check offset forward by
2981 // the standard interval.
2982 SetNextConstPoolCheckIn(kCheckConstPoolInterval);
2983}
2984
2985
2986bool Assembler::ShouldEmitVeneer(int max_reachable_pc, int margin) {
2987 // Account for the branch around the veneers and the guard.
2988 int protection_offset = 2 * kInstructionSize;
2989 return pc_offset() > max_reachable_pc - margin - protection_offset -
2990 static_cast<int>(unresolved_branches_.size() * kMaxVeneerCodeSize);
2991}
2992
2993
2994void Assembler::RecordVeneerPool(int location_offset, int size) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002995 RelocInfo rinfo(isolate(), buffer_ + location_offset, RelocInfo::VENEER_POOL,
2996 static_cast<intptr_t>(size), NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002997 reloc_info_writer.Write(&rinfo);
2998}
2999
3000
3001void Assembler::EmitVeneers(bool force_emit, bool need_protection, int margin) {
3002 BlockPoolsScope scope(this);
3003 RecordComment("[ Veneers");
3004
3005 // The exact size of the veneer pool must be recorded (see the comment at the
3006 // declaration site of RecordConstPool()), but computing the number of
3007 // veneers that will be generated is not obvious. So instead we remember the
3008 // current position and will record the size after the pool has been
3009 // generated.
3010 Label size_check;
3011 bind(&size_check);
3012 int veneer_pool_relocinfo_loc = pc_offset();
3013
3014 Label end;
3015 if (need_protection) {
3016 b(&end);
3017 }
3018
3019 EmitVeneersGuard();
3020
3021 Label veneer_size_check;
3022
3023 std::multimap<int, FarBranchInfo>::iterator it, it_to_delete;
3024
3025 it = unresolved_branches_.begin();
3026 while (it != unresolved_branches_.end()) {
3027 if (force_emit || ShouldEmitVeneer(it->first, margin)) {
3028 Instruction* branch = InstructionAt(it->second.pc_offset_);
3029 Label* label = it->second.label_;
3030
3031#ifdef DEBUG
3032 bind(&veneer_size_check);
3033#endif
3034 // Patch the branch to point to the current position, and emit a branch
3035 // to the label.
3036 Instruction* veneer = reinterpret_cast<Instruction*>(pc_);
3037 RemoveBranchFromLabelLinkChain(branch, label, veneer);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003038 branch->SetImmPCOffsetTarget(isolate(), veneer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003039 b(label);
3040#ifdef DEBUG
3041 DCHECK(SizeOfCodeGeneratedSince(&veneer_size_check) <=
3042 static_cast<uint64_t>(kMaxVeneerCodeSize));
3043 veneer_size_check.Unuse();
3044#endif
3045
3046 it_to_delete = it++;
3047 unresolved_branches_.erase(it_to_delete);
3048 } else {
3049 ++it;
3050 }
3051 }
3052
3053 // Record the veneer pool size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003054 int pool_size = static_cast<int>(SizeOfCodeGeneratedSince(&size_check));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003055 RecordVeneerPool(veneer_pool_relocinfo_loc, pool_size);
3056
3057 if (unresolved_branches_.empty()) {
3058 next_veneer_pool_check_ = kMaxInt;
3059 } else {
3060 next_veneer_pool_check_ =
3061 unresolved_branches_first_limit() - kVeneerDistanceCheckMargin;
3062 }
3063
3064 bind(&end);
3065
3066 RecordComment("]");
3067}
3068
3069
3070void Assembler::CheckVeneerPool(bool force_emit, bool require_jump,
3071 int margin) {
3072 // There is nothing to do if there are no pending veneer pool entries.
3073 if (unresolved_branches_.empty()) {
3074 DCHECK(next_veneer_pool_check_ == kMaxInt);
3075 return;
3076 }
3077
3078 DCHECK(pc_offset() < unresolved_branches_first_limit());
3079
3080 // Some short sequence of instruction mustn't be broken up by veneer pool
3081 // emission, such sequences are protected by calls to BlockVeneerPoolFor and
3082 // BlockVeneerPoolScope.
3083 if (is_veneer_pool_blocked()) {
3084 DCHECK(!force_emit);
3085 return;
3086 }
3087
3088 if (!require_jump) {
3089 // Prefer emitting veneers protected by an existing instruction.
3090 margin *= kVeneerNoProtectionFactor;
3091 }
3092 if (force_emit || ShouldEmitVeneers(margin)) {
3093 EmitVeneers(force_emit, require_jump, margin);
3094 } else {
3095 next_veneer_pool_check_ =
3096 unresolved_branches_first_limit() - kVeneerDistanceCheckMargin;
3097 }
3098}
3099
3100
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003101int Assembler::buffer_space() const {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003102 return static_cast<int>(reloc_info_writer.pos() -
3103 reinterpret_cast<byte*>(pc_));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003104}
3105
3106
3107void Assembler::RecordConstPool(int size) {
3108 // We only need this for debugger support, to correctly compute offsets in the
3109 // code.
3110 RecordRelocInfo(RelocInfo::CONST_POOL, static_cast<intptr_t>(size));
3111}
3112
3113
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003114void PatchingAssembler::PatchAdrFar(int64_t target_offset) {
3115 // The code at the current instruction should be:
3116 // adr rd, 0
3117 // nop (adr_far)
3118 // nop (adr_far)
3119 // movz scratch, 0
3120
3121 // Verify the expected code.
3122 Instruction* expected_adr = InstructionAt(0);
3123 CHECK(expected_adr->IsAdr() && (expected_adr->ImmPCRel() == 0));
3124 int rd_code = expected_adr->Rd();
3125 for (int i = 0; i < kAdrFarPatchableNNops; ++i) {
3126 CHECK(InstructionAt((i + 1) * kInstructionSize)->IsNop(ADR_FAR_NOP));
3127 }
3128 Instruction* expected_movz =
3129 InstructionAt((kAdrFarPatchableNInstrs - 1) * kInstructionSize);
3130 CHECK(expected_movz->IsMovz() &&
3131 (expected_movz->ImmMoveWide() == 0) &&
3132 (expected_movz->ShiftMoveWide() == 0));
3133 int scratch_code = expected_movz->Rd();
3134
3135 // Patch to load the correct address.
3136 Register rd = Register::XRegFromCode(rd_code);
3137 Register scratch = Register::XRegFromCode(scratch_code);
3138 // Addresses are only 48 bits.
3139 adr(rd, target_offset & 0xFFFF);
3140 movz(scratch, (target_offset >> 16) & 0xFFFF, 16);
3141 movk(scratch, (target_offset >> 32) & 0xFFFF, 32);
3142 DCHECK((target_offset >> 48) == 0);
3143 add(rd, rd, scratch);
3144}
3145
3146
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003147} // namespace internal
3148} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003149
3150#endif // V8_TARGET_ARCH_ARM64