blob: 12ddd8145e38decd00e98c6902a084151f71dfdb [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2013 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#if V8_TARGET_ARCH_ARM64
6
7#include "src/base/bits.h"
8#include "src/base/division-by-constant.h"
9#include "src/bootstrapper.h"
10#include "src/codegen.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000011#include "src/debug/debug.h"
12#include "src/register-configuration.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040013#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015#include "src/arm64/frames-arm64.h"
16#include "src/arm64/macro-assembler-arm64.h"
17
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018namespace v8 {
19namespace internal {
20
21// Define a fake double underscore to use with the ASM_UNIMPLEMENTED macros.
22#define __
23
24
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000025MacroAssembler::MacroAssembler(Isolate* arg_isolate, byte* buffer,
26 unsigned buffer_size,
27 CodeObjectRequired create_code_object)
Ben Murdochb8a8cc12014-11-26 15:28:44 +000028 : Assembler(arg_isolate, buffer, buffer_size),
29 generating_stub_(false),
30#if DEBUG
31 allow_macro_instructions_(true),
32#endif
33 has_frame_(false),
34 use_real_aborts_(true),
35 sp_(jssp),
36 tmp_list_(DefaultTmpList()),
37 fptmp_list_(DefaultFPTmpList()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038 if (create_code_object == CodeObjectRequired::kYes) {
39 code_object_ =
40 Handle<Object>::New(isolate()->heap()->undefined_value(), isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 }
42}
43
44
45CPURegList MacroAssembler::DefaultTmpList() {
46 return CPURegList(ip0, ip1);
47}
48
49
50CPURegList MacroAssembler::DefaultFPTmpList() {
51 return CPURegList(fp_scratch1, fp_scratch2);
52}
53
54
55void MacroAssembler::LogicalMacro(const Register& rd,
56 const Register& rn,
57 const Operand& operand,
58 LogicalOp op) {
59 UseScratchRegisterScope temps(this);
60
61 if (operand.NeedsRelocation(this)) {
62 Register temp = temps.AcquireX();
63 Ldr(temp, operand.immediate());
64 Logical(rd, rn, temp, op);
65
66 } else if (operand.IsImmediate()) {
67 int64_t immediate = operand.ImmediateValue();
68 unsigned reg_size = rd.SizeInBits();
69
70 // If the operation is NOT, invert the operation and immediate.
71 if ((op & NOT) == NOT) {
72 op = static_cast<LogicalOp>(op & ~NOT);
73 immediate = ~immediate;
74 }
75
76 // Ignore the top 32 bits of an immediate if we're moving to a W register.
77 if (rd.Is32Bits()) {
78 // Check that the top 32 bits are consistent.
79 DCHECK(((immediate >> kWRegSizeInBits) == 0) ||
80 ((immediate >> kWRegSizeInBits) == -1));
81 immediate &= kWRegMask;
82 }
83
84 DCHECK(rd.Is64Bits() || is_uint32(immediate));
85
86 // Special cases for all set or all clear immediates.
87 if (immediate == 0) {
88 switch (op) {
89 case AND:
90 Mov(rd, 0);
91 return;
92 case ORR: // Fall through.
93 case EOR:
94 Mov(rd, rn);
95 return;
96 case ANDS: // Fall through.
97 case BICS:
98 break;
99 default:
100 UNREACHABLE();
101 }
102 } else if ((rd.Is64Bits() && (immediate == -1L)) ||
103 (rd.Is32Bits() && (immediate == 0xffffffffL))) {
104 switch (op) {
105 case AND:
106 Mov(rd, rn);
107 return;
108 case ORR:
109 Mov(rd, immediate);
110 return;
111 case EOR:
112 Mvn(rd, rn);
113 return;
114 case ANDS: // Fall through.
115 case BICS:
116 break;
117 default:
118 UNREACHABLE();
119 }
120 }
121
122 unsigned n, imm_s, imm_r;
123 if (IsImmLogical(immediate, reg_size, &n, &imm_s, &imm_r)) {
124 // Immediate can be encoded in the instruction.
125 LogicalImmediate(rd, rn, n, imm_s, imm_r, op);
126 } else {
127 // Immediate can't be encoded: synthesize using move immediate.
128 Register temp = temps.AcquireSameSizeAs(rn);
129 Operand imm_operand = MoveImmediateForShiftedOp(temp, immediate);
130 if (rd.Is(csp)) {
131 // If rd is the stack pointer we cannot use it as the destination
132 // register so we use the temp register as an intermediate again.
133 Logical(temp, rn, imm_operand, op);
134 Mov(csp, temp);
135 AssertStackConsistency();
136 } else {
137 Logical(rd, rn, imm_operand, op);
138 }
139 }
140
141 } else if (operand.IsExtendedRegister()) {
142 DCHECK(operand.reg().SizeInBits() <= rd.SizeInBits());
143 // Add/sub extended supports shift <= 4. We want to support exactly the
144 // same modes here.
145 DCHECK(operand.shift_amount() <= 4);
146 DCHECK(operand.reg().Is64Bits() ||
147 ((operand.extend() != UXTX) && (operand.extend() != SXTX)));
148 Register temp = temps.AcquireSameSizeAs(rn);
149 EmitExtendShift(temp, operand.reg(), operand.extend(),
150 operand.shift_amount());
151 Logical(rd, rn, temp, op);
152
153 } else {
154 // The operand can be encoded in the instruction.
155 DCHECK(operand.IsShiftedRegister());
156 Logical(rd, rn, operand, op);
157 }
158}
159
160
161void MacroAssembler::Mov(const Register& rd, uint64_t imm) {
162 DCHECK(allow_macro_instructions_);
163 DCHECK(is_uint32(imm) || is_int32(imm) || rd.Is64Bits());
164 DCHECK(!rd.IsZero());
165
166 // TODO(all) extend to support more immediates.
167 //
168 // Immediates on Aarch64 can be produced using an initial value, and zero to
169 // three move keep operations.
170 //
171 // Initial values can be generated with:
172 // 1. 64-bit move zero (movz).
173 // 2. 32-bit move inverted (movn).
174 // 3. 64-bit move inverted.
175 // 4. 32-bit orr immediate.
176 // 5. 64-bit orr immediate.
177 // Move-keep may then be used to modify each of the 16-bit half-words.
178 //
179 // The code below supports all five initial value generators, and
180 // applying move-keep operations to move-zero and move-inverted initial
181 // values.
182
183 // Try to move the immediate in one instruction, and if that fails, switch to
184 // using multiple instructions.
185 if (!TryOneInstrMoveImmediate(rd, imm)) {
186 unsigned reg_size = rd.SizeInBits();
187
188 // Generic immediate case. Imm will be represented by
189 // [imm3, imm2, imm1, imm0], where each imm is 16 bits.
190 // A move-zero or move-inverted is generated for the first non-zero or
191 // non-0xffff immX, and a move-keep for subsequent non-zero immX.
192
193 uint64_t ignored_halfword = 0;
194 bool invert_move = false;
195 // If the number of 0xffff halfwords is greater than the number of 0x0000
196 // halfwords, it's more efficient to use move-inverted.
197 if (CountClearHalfWords(~imm, reg_size) >
198 CountClearHalfWords(imm, reg_size)) {
199 ignored_halfword = 0xffffL;
200 invert_move = true;
201 }
202
203 // Mov instructions can't move immediate values into the stack pointer, so
204 // set up a temporary register, if needed.
205 UseScratchRegisterScope temps(this);
206 Register temp = rd.IsSP() ? temps.AcquireSameSizeAs(rd) : rd;
207
208 // Iterate through the halfwords. Use movn/movz for the first non-ignored
209 // halfword, and movk for subsequent halfwords.
210 DCHECK((reg_size % 16) == 0);
211 bool first_mov_done = false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000212 for (int i = 0; i < (rd.SizeInBits() / 16); i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 uint64_t imm16 = (imm >> (16 * i)) & 0xffffL;
214 if (imm16 != ignored_halfword) {
215 if (!first_mov_done) {
216 if (invert_move) {
217 movn(temp, (~imm16) & 0xffffL, 16 * i);
218 } else {
219 movz(temp, imm16, 16 * i);
220 }
221 first_mov_done = true;
222 } else {
223 // Construct a wider constant.
224 movk(temp, imm16, 16 * i);
225 }
226 }
227 }
228 DCHECK(first_mov_done);
229
230 // Move the temporary if the original destination register was the stack
231 // pointer.
232 if (rd.IsSP()) {
233 mov(rd, temp);
234 AssertStackConsistency();
235 }
236 }
237}
238
239
240void MacroAssembler::Mov(const Register& rd,
241 const Operand& operand,
242 DiscardMoveMode discard_mode) {
243 DCHECK(allow_macro_instructions_);
244 DCHECK(!rd.IsZero());
245
246 // Provide a swap register for instructions that need to write into the
247 // system stack pointer (and can't do this inherently).
248 UseScratchRegisterScope temps(this);
249 Register dst = (rd.IsSP()) ? temps.AcquireSameSizeAs(rd) : rd;
250
251 if (operand.NeedsRelocation(this)) {
252 Ldr(dst, operand.immediate());
253
254 } else if (operand.IsImmediate()) {
255 // Call the macro assembler for generic immediates.
256 Mov(dst, operand.ImmediateValue());
257
258 } else if (operand.IsShiftedRegister() && (operand.shift_amount() != 0)) {
259 // Emit a shift instruction if moving a shifted register. This operation
260 // could also be achieved using an orr instruction (like orn used by Mvn),
261 // but using a shift instruction makes the disassembly clearer.
262 EmitShift(dst, operand.reg(), operand.shift(), operand.shift_amount());
263
264 } else if (operand.IsExtendedRegister()) {
265 // Emit an extend instruction if moving an extended register. This handles
266 // extend with post-shift operations, too.
267 EmitExtendShift(dst, operand.reg(), operand.extend(),
268 operand.shift_amount());
269
270 } else {
271 // Otherwise, emit a register move only if the registers are distinct, or
272 // if they are not X registers.
273 //
274 // Note that mov(w0, w0) is not a no-op because it clears the top word of
275 // x0. A flag is provided (kDiscardForSameWReg) if a move between the same W
276 // registers is not required to clear the top word of the X register. In
277 // this case, the instruction is discarded.
278 //
279 // If csp is an operand, add #0 is emitted, otherwise, orr #0.
280 if (!rd.Is(operand.reg()) || (rd.Is32Bits() &&
281 (discard_mode == kDontDiscardForSameWReg))) {
282 Assembler::mov(rd, operand.reg());
283 }
284 // This case can handle writes into the system stack pointer directly.
285 dst = rd;
286 }
287
288 // Copy the result to the system stack pointer.
289 if (!dst.Is(rd)) {
290 DCHECK(rd.IsSP());
291 Assembler::mov(rd, dst);
292 }
293}
294
295
296void MacroAssembler::Mvn(const Register& rd, const Operand& operand) {
297 DCHECK(allow_macro_instructions_);
298
299 if (operand.NeedsRelocation(this)) {
300 Ldr(rd, operand.immediate());
301 mvn(rd, rd);
302
303 } else if (operand.IsImmediate()) {
304 // Call the macro assembler for generic immediates.
305 Mov(rd, ~operand.ImmediateValue());
306
307 } else if (operand.IsExtendedRegister()) {
308 // Emit two instructions for the extend case. This differs from Mov, as
309 // the extend and invert can't be achieved in one instruction.
310 EmitExtendShift(rd, operand.reg(), operand.extend(),
311 operand.shift_amount());
312 mvn(rd, rd);
313
314 } else {
315 mvn(rd, operand);
316 }
317}
318
319
320unsigned MacroAssembler::CountClearHalfWords(uint64_t imm, unsigned reg_size) {
321 DCHECK((reg_size % 8) == 0);
322 int count = 0;
323 for (unsigned i = 0; i < (reg_size / 16); i++) {
324 if ((imm & 0xffff) == 0) {
325 count++;
326 }
327 imm >>= 16;
328 }
329 return count;
330}
331
332
333// The movz instruction can generate immediates containing an arbitrary 16-bit
334// half-word, with remaining bits clear, eg. 0x00001234, 0x0000123400000000.
335bool MacroAssembler::IsImmMovz(uint64_t imm, unsigned reg_size) {
336 DCHECK((reg_size == kXRegSizeInBits) || (reg_size == kWRegSizeInBits));
337 return CountClearHalfWords(imm, reg_size) >= ((reg_size / 16) - 1);
338}
339
340
341// The movn instruction can generate immediates containing an arbitrary 16-bit
342// half-word, with remaining bits set, eg. 0xffff1234, 0xffff1234ffffffff.
343bool MacroAssembler::IsImmMovn(uint64_t imm, unsigned reg_size) {
344 return IsImmMovz(~imm, reg_size);
345}
346
347
348void MacroAssembler::ConditionalCompareMacro(const Register& rn,
349 const Operand& operand,
350 StatusFlags nzcv,
351 Condition cond,
352 ConditionalCompareOp op) {
353 DCHECK((cond != al) && (cond != nv));
354 if (operand.NeedsRelocation(this)) {
355 UseScratchRegisterScope temps(this);
356 Register temp = temps.AcquireX();
357 Ldr(temp, operand.immediate());
358 ConditionalCompareMacro(rn, temp, nzcv, cond, op);
359
360 } else if ((operand.IsShiftedRegister() && (operand.shift_amount() == 0)) ||
361 (operand.IsImmediate() &&
362 IsImmConditionalCompare(operand.ImmediateValue()))) {
363 // The immediate can be encoded in the instruction, or the operand is an
364 // unshifted register: call the assembler.
365 ConditionalCompare(rn, operand, nzcv, cond, op);
366
367 } else {
368 // The operand isn't directly supported by the instruction: perform the
369 // operation on a temporary register.
370 UseScratchRegisterScope temps(this);
371 Register temp = temps.AcquireSameSizeAs(rn);
372 Mov(temp, operand);
373 ConditionalCompare(rn, temp, nzcv, cond, op);
374 }
375}
376
377
378void MacroAssembler::Csel(const Register& rd,
379 const Register& rn,
380 const Operand& operand,
381 Condition cond) {
382 DCHECK(allow_macro_instructions_);
383 DCHECK(!rd.IsZero());
384 DCHECK((cond != al) && (cond != nv));
385 if (operand.IsImmediate()) {
386 // Immediate argument. Handle special cases of 0, 1 and -1 using zero
387 // register.
388 int64_t imm = operand.ImmediateValue();
389 Register zr = AppropriateZeroRegFor(rn);
390 if (imm == 0) {
391 csel(rd, rn, zr, cond);
392 } else if (imm == 1) {
393 csinc(rd, rn, zr, cond);
394 } else if (imm == -1) {
395 csinv(rd, rn, zr, cond);
396 } else {
397 UseScratchRegisterScope temps(this);
398 Register temp = temps.AcquireSameSizeAs(rn);
399 Mov(temp, imm);
400 csel(rd, rn, temp, cond);
401 }
402 } else if (operand.IsShiftedRegister() && (operand.shift_amount() == 0)) {
403 // Unshifted register argument.
404 csel(rd, rn, operand.reg(), cond);
405 } else {
406 // All other arguments.
407 UseScratchRegisterScope temps(this);
408 Register temp = temps.AcquireSameSizeAs(rn);
409 Mov(temp, operand);
410 csel(rd, rn, temp, cond);
411 }
412}
413
414
415bool MacroAssembler::TryOneInstrMoveImmediate(const Register& dst,
416 int64_t imm) {
417 unsigned n, imm_s, imm_r;
418 int reg_size = dst.SizeInBits();
419 if (IsImmMovz(imm, reg_size) && !dst.IsSP()) {
420 // Immediate can be represented in a move zero instruction. Movz can't write
421 // to the stack pointer.
422 movz(dst, imm);
423 return true;
424 } else if (IsImmMovn(imm, reg_size) && !dst.IsSP()) {
425 // Immediate can be represented in a move not instruction. Movn can't write
426 // to the stack pointer.
427 movn(dst, dst.Is64Bits() ? ~imm : (~imm & kWRegMask));
428 return true;
429 } else if (IsImmLogical(imm, reg_size, &n, &imm_s, &imm_r)) {
430 // Immediate can be represented in a logical orr instruction.
431 LogicalImmediate(dst, AppropriateZeroRegFor(dst), n, imm_s, imm_r, ORR);
432 return true;
433 }
434 return false;
435}
436
437
438Operand MacroAssembler::MoveImmediateForShiftedOp(const Register& dst,
439 int64_t imm) {
440 int reg_size = dst.SizeInBits();
441
442 // Encode the immediate in a single move instruction, if possible.
443 if (TryOneInstrMoveImmediate(dst, imm)) {
444 // The move was successful; nothing to do here.
445 } else {
446 // Pre-shift the immediate to the least-significant bits of the register.
447 int shift_low = CountTrailingZeros(imm, reg_size);
448 int64_t imm_low = imm >> shift_low;
449
450 // Pre-shift the immediate to the most-significant bits of the register. We
451 // insert set bits in the least-significant bits, as this creates a
452 // different immediate that may be encodable using movn or orr-immediate.
453 // If this new immediate is encodable, the set bits will be eliminated by
454 // the post shift on the following instruction.
455 int shift_high = CountLeadingZeros(imm, reg_size);
456 int64_t imm_high = (imm << shift_high) | ((1 << shift_high) - 1);
457
458 if (TryOneInstrMoveImmediate(dst, imm_low)) {
459 // The new immediate has been moved into the destination's low bits:
460 // return a new leftward-shifting operand.
461 return Operand(dst, LSL, shift_low);
462 } else if (TryOneInstrMoveImmediate(dst, imm_high)) {
463 // The new immediate has been moved into the destination's high bits:
464 // return a new rightward-shifting operand.
465 return Operand(dst, LSR, shift_high);
466 } else {
467 // Use the generic move operation to set up the immediate.
468 Mov(dst, imm);
469 }
470 }
471 return Operand(dst);
472}
473
474
475void MacroAssembler::AddSubMacro(const Register& rd,
476 const Register& rn,
477 const Operand& operand,
478 FlagsUpdate S,
479 AddSubOp op) {
480 if (operand.IsZero() && rd.Is(rn) && rd.Is64Bits() && rn.Is64Bits() &&
481 !operand.NeedsRelocation(this) && (S == LeaveFlags)) {
482 // The instruction would be a nop. Avoid generating useless code.
483 return;
484 }
485
486 if (operand.NeedsRelocation(this)) {
487 UseScratchRegisterScope temps(this);
488 Register temp = temps.AcquireX();
489 Ldr(temp, operand.immediate());
490 AddSubMacro(rd, rn, temp, S, op);
491 } else if ((operand.IsImmediate() &&
492 !IsImmAddSub(operand.ImmediateValue())) ||
493 (rn.IsZero() && !operand.IsShiftedRegister()) ||
494 (operand.IsShiftedRegister() && (operand.shift() == ROR))) {
495 UseScratchRegisterScope temps(this);
496 Register temp = temps.AcquireSameSizeAs(rn);
497 if (operand.IsImmediate()) {
498 Operand imm_operand =
499 MoveImmediateForShiftedOp(temp, operand.ImmediateValue());
500 AddSub(rd, rn, imm_operand, S, op);
501 } else {
502 Mov(temp, operand);
503 AddSub(rd, rn, temp, S, op);
504 }
505 } else {
506 AddSub(rd, rn, operand, S, op);
507 }
508}
509
510
511void MacroAssembler::AddSubWithCarryMacro(const Register& rd,
512 const Register& rn,
513 const Operand& operand,
514 FlagsUpdate S,
515 AddSubWithCarryOp op) {
516 DCHECK(rd.SizeInBits() == rn.SizeInBits());
517 UseScratchRegisterScope temps(this);
518
519 if (operand.NeedsRelocation(this)) {
520 Register temp = temps.AcquireX();
521 Ldr(temp, operand.immediate());
522 AddSubWithCarryMacro(rd, rn, temp, S, op);
523
524 } else if (operand.IsImmediate() ||
525 (operand.IsShiftedRegister() && (operand.shift() == ROR))) {
526 // Add/sub with carry (immediate or ROR shifted register.)
527 Register temp = temps.AcquireSameSizeAs(rn);
528 Mov(temp, operand);
529 AddSubWithCarry(rd, rn, temp, S, op);
530
531 } else if (operand.IsShiftedRegister() && (operand.shift_amount() != 0)) {
532 // Add/sub with carry (shifted register).
533 DCHECK(operand.reg().SizeInBits() == rd.SizeInBits());
534 DCHECK(operand.shift() != ROR);
535 DCHECK(is_uintn(operand.shift_amount(),
536 rd.SizeInBits() == kXRegSizeInBits ? kXRegSizeInBitsLog2
537 : kWRegSizeInBitsLog2));
538 Register temp = temps.AcquireSameSizeAs(rn);
539 EmitShift(temp, operand.reg(), operand.shift(), operand.shift_amount());
540 AddSubWithCarry(rd, rn, temp, S, op);
541
542 } else if (operand.IsExtendedRegister()) {
543 // Add/sub with carry (extended register).
544 DCHECK(operand.reg().SizeInBits() <= rd.SizeInBits());
545 // Add/sub extended supports a shift <= 4. We want to support exactly the
546 // same modes.
547 DCHECK(operand.shift_amount() <= 4);
548 DCHECK(operand.reg().Is64Bits() ||
549 ((operand.extend() != UXTX) && (operand.extend() != SXTX)));
550 Register temp = temps.AcquireSameSizeAs(rn);
551 EmitExtendShift(temp, operand.reg(), operand.extend(),
552 operand.shift_amount());
553 AddSubWithCarry(rd, rn, temp, S, op);
554
555 } else {
556 // The addressing mode is directly supported by the instruction.
557 AddSubWithCarry(rd, rn, operand, S, op);
558 }
559}
560
561
562void MacroAssembler::LoadStoreMacro(const CPURegister& rt,
563 const MemOperand& addr,
564 LoadStoreOp op) {
565 int64_t offset = addr.offset();
566 LSDataSize size = CalcLSDataSize(op);
567
568 // Check if an immediate offset fits in the immediate field of the
569 // appropriate instruction. If not, emit two instructions to perform
570 // the operation.
571 if (addr.IsImmediateOffset() && !IsImmLSScaled(offset, size) &&
572 !IsImmLSUnscaled(offset)) {
573 // Immediate offset that can't be encoded using unsigned or unscaled
574 // addressing modes.
575 UseScratchRegisterScope temps(this);
576 Register temp = temps.AcquireSameSizeAs(addr.base());
577 Mov(temp, addr.offset());
578 LoadStore(rt, MemOperand(addr.base(), temp), op);
579 } else if (addr.IsPostIndex() && !IsImmLSUnscaled(offset)) {
580 // Post-index beyond unscaled addressing range.
581 LoadStore(rt, MemOperand(addr.base()), op);
582 add(addr.base(), addr.base(), offset);
583 } else if (addr.IsPreIndex() && !IsImmLSUnscaled(offset)) {
584 // Pre-index beyond unscaled addressing range.
585 add(addr.base(), addr.base(), offset);
586 LoadStore(rt, MemOperand(addr.base()), op);
587 } else {
588 // Encodable in one load/store instruction.
589 LoadStore(rt, addr, op);
590 }
591}
592
593void MacroAssembler::LoadStorePairMacro(const CPURegister& rt,
594 const CPURegister& rt2,
595 const MemOperand& addr,
596 LoadStorePairOp op) {
597 // TODO(all): Should we support register offset for load-store-pair?
598 DCHECK(!addr.IsRegisterOffset());
599
600 int64_t offset = addr.offset();
601 LSDataSize size = CalcLSPairDataSize(op);
602
603 // Check if the offset fits in the immediate field of the appropriate
604 // instruction. If not, emit two instructions to perform the operation.
605 if (IsImmLSPair(offset, size)) {
606 // Encodable in one load/store pair instruction.
607 LoadStorePair(rt, rt2, addr, op);
608 } else {
609 Register base = addr.base();
610 if (addr.IsImmediateOffset()) {
611 UseScratchRegisterScope temps(this);
612 Register temp = temps.AcquireSameSizeAs(base);
613 Add(temp, base, offset);
614 LoadStorePair(rt, rt2, MemOperand(temp), op);
615 } else if (addr.IsPostIndex()) {
616 LoadStorePair(rt, rt2, MemOperand(base), op);
617 Add(base, base, offset);
618 } else {
619 DCHECK(addr.IsPreIndex());
620 Add(base, base, offset);
621 LoadStorePair(rt, rt2, MemOperand(base), op);
622 }
623 }
624}
625
626
627void MacroAssembler::Load(const Register& rt,
628 const MemOperand& addr,
629 Representation r) {
630 DCHECK(!r.IsDouble());
631
632 if (r.IsInteger8()) {
633 Ldrsb(rt, addr);
634 } else if (r.IsUInteger8()) {
635 Ldrb(rt, addr);
636 } else if (r.IsInteger16()) {
637 Ldrsh(rt, addr);
638 } else if (r.IsUInteger16()) {
639 Ldrh(rt, addr);
640 } else if (r.IsInteger32()) {
641 Ldr(rt.W(), addr);
642 } else {
643 DCHECK(rt.Is64Bits());
644 Ldr(rt, addr);
645 }
646}
647
648
649void MacroAssembler::Store(const Register& rt,
650 const MemOperand& addr,
651 Representation r) {
652 DCHECK(!r.IsDouble());
653
654 if (r.IsInteger8() || r.IsUInteger8()) {
655 Strb(rt, addr);
656 } else if (r.IsInteger16() || r.IsUInteger16()) {
657 Strh(rt, addr);
658 } else if (r.IsInteger32()) {
659 Str(rt.W(), addr);
660 } else {
661 DCHECK(rt.Is64Bits());
662 if (r.IsHeapObject()) {
663 AssertNotSmi(rt);
664 } else if (r.IsSmi()) {
665 AssertSmi(rt);
666 }
667 Str(rt, addr);
668 }
669}
670
671
672bool MacroAssembler::NeedExtraInstructionsOrRegisterBranch(
673 Label *label, ImmBranchType b_type) {
674 bool need_longer_range = false;
675 // There are two situations in which we care about the offset being out of
676 // range:
677 // - The label is bound but too far away.
678 // - The label is not bound but linked, and the previous branch
679 // instruction in the chain is too far away.
680 if (label->is_bound() || label->is_linked()) {
681 need_longer_range =
682 !Instruction::IsValidImmPCOffset(b_type, label->pos() - pc_offset());
683 }
684 if (!need_longer_range && !label->is_bound()) {
685 int max_reachable_pc = pc_offset() + Instruction::ImmBranchRange(b_type);
686 unresolved_branches_.insert(
687 std::pair<int, FarBranchInfo>(max_reachable_pc,
688 FarBranchInfo(pc_offset(), label)));
689 // Also maintain the next pool check.
690 next_veneer_pool_check_ =
691 Min(next_veneer_pool_check_,
692 max_reachable_pc - kVeneerDistanceCheckMargin);
693 }
694 return need_longer_range;
695}
696
697
698void MacroAssembler::Adr(const Register& rd, Label* label, AdrHint hint) {
699 DCHECK(allow_macro_instructions_);
700 DCHECK(!rd.IsZero());
701
702 if (hint == kAdrNear) {
703 adr(rd, label);
704 return;
705 }
706
707 DCHECK(hint == kAdrFar);
708 if (label->is_bound()) {
709 int label_offset = label->pos() - pc_offset();
710 if (Instruction::IsValidPCRelOffset(label_offset)) {
711 adr(rd, label);
712 } else {
713 DCHECK(label_offset <= 0);
714 int min_adr_offset = -(1 << (Instruction::ImmPCRelRangeBitwidth - 1));
715 adr(rd, min_adr_offset);
716 Add(rd, rd, label_offset - min_adr_offset);
717 }
718 } else {
719 UseScratchRegisterScope temps(this);
720 Register scratch = temps.AcquireX();
721
722 InstructionAccurateScope scope(
723 this, PatchingAssembler::kAdrFarPatchableNInstrs);
724 adr(rd, label);
725 for (int i = 0; i < PatchingAssembler::kAdrFarPatchableNNops; ++i) {
726 nop(ADR_FAR_NOP);
727 }
728 movz(scratch, 0);
729 }
730}
731
732
733void MacroAssembler::B(Label* label, BranchType type, Register reg, int bit) {
734 DCHECK((reg.Is(NoReg) || type >= kBranchTypeFirstUsingReg) &&
735 (bit == -1 || type >= kBranchTypeFirstUsingBit));
736 if (kBranchTypeFirstCondition <= type && type <= kBranchTypeLastCondition) {
737 B(static_cast<Condition>(type), label);
738 } else {
739 switch (type) {
740 case always: B(label); break;
741 case never: break;
742 case reg_zero: Cbz(reg, label); break;
743 case reg_not_zero: Cbnz(reg, label); break;
744 case reg_bit_clear: Tbz(reg, bit, label); break;
745 case reg_bit_set: Tbnz(reg, bit, label); break;
746 default:
747 UNREACHABLE();
748 }
749 }
750}
751
752
753void MacroAssembler::B(Label* label, Condition cond) {
754 DCHECK(allow_macro_instructions_);
755 DCHECK((cond != al) && (cond != nv));
756
757 Label done;
758 bool need_extra_instructions =
759 NeedExtraInstructionsOrRegisterBranch(label, CondBranchType);
760
761 if (need_extra_instructions) {
762 b(&done, NegateCondition(cond));
763 B(label);
764 } else {
765 b(label, cond);
766 }
767 bind(&done);
768}
769
770
771void MacroAssembler::Tbnz(const Register& rt, unsigned bit_pos, Label* label) {
772 DCHECK(allow_macro_instructions_);
773
774 Label done;
775 bool need_extra_instructions =
776 NeedExtraInstructionsOrRegisterBranch(label, TestBranchType);
777
778 if (need_extra_instructions) {
779 tbz(rt, bit_pos, &done);
780 B(label);
781 } else {
782 tbnz(rt, bit_pos, label);
783 }
784 bind(&done);
785}
786
787
788void MacroAssembler::Tbz(const Register& rt, unsigned bit_pos, Label* label) {
789 DCHECK(allow_macro_instructions_);
790
791 Label done;
792 bool need_extra_instructions =
793 NeedExtraInstructionsOrRegisterBranch(label, TestBranchType);
794
795 if (need_extra_instructions) {
796 tbnz(rt, bit_pos, &done);
797 B(label);
798 } else {
799 tbz(rt, bit_pos, label);
800 }
801 bind(&done);
802}
803
804
805void MacroAssembler::Cbnz(const Register& rt, Label* label) {
806 DCHECK(allow_macro_instructions_);
807
808 Label done;
809 bool need_extra_instructions =
810 NeedExtraInstructionsOrRegisterBranch(label, CompareBranchType);
811
812 if (need_extra_instructions) {
813 cbz(rt, &done);
814 B(label);
815 } else {
816 cbnz(rt, label);
817 }
818 bind(&done);
819}
820
821
822void MacroAssembler::Cbz(const Register& rt, Label* label) {
823 DCHECK(allow_macro_instructions_);
824
825 Label done;
826 bool need_extra_instructions =
827 NeedExtraInstructionsOrRegisterBranch(label, CompareBranchType);
828
829 if (need_extra_instructions) {
830 cbnz(rt, &done);
831 B(label);
832 } else {
833 cbz(rt, label);
834 }
835 bind(&done);
836}
837
838
839// Pseudo-instructions.
840
841
842void MacroAssembler::Abs(const Register& rd, const Register& rm,
843 Label* is_not_representable,
844 Label* is_representable) {
845 DCHECK(allow_macro_instructions_);
846 DCHECK(AreSameSizeAndType(rd, rm));
847
848 Cmp(rm, 1);
849 Cneg(rd, rm, lt);
850
851 // If the comparison sets the v flag, the input was the smallest value
852 // representable by rm, and the mathematical result of abs(rm) is not
853 // representable using two's complement.
854 if ((is_not_representable != NULL) && (is_representable != NULL)) {
855 B(is_not_representable, vs);
856 B(is_representable);
857 } else if (is_not_representable != NULL) {
858 B(is_not_representable, vs);
859 } else if (is_representable != NULL) {
860 B(is_representable, vc);
861 }
862}
863
864
865// Abstracted stack operations.
866
867
868void MacroAssembler::Push(const CPURegister& src0, const CPURegister& src1,
869 const CPURegister& src2, const CPURegister& src3) {
870 DCHECK(AreSameSizeAndType(src0, src1, src2, src3));
871
872 int count = 1 + src1.IsValid() + src2.IsValid() + src3.IsValid();
873 int size = src0.SizeInBytes();
874
875 PushPreamble(count, size);
876 PushHelper(count, size, src0, src1, src2, src3);
877}
878
879
880void MacroAssembler::Push(const CPURegister& src0, const CPURegister& src1,
881 const CPURegister& src2, const CPURegister& src3,
882 const CPURegister& src4, const CPURegister& src5,
883 const CPURegister& src6, const CPURegister& src7) {
884 DCHECK(AreSameSizeAndType(src0, src1, src2, src3, src4, src5, src6, src7));
885
886 int count = 5 + src5.IsValid() + src6.IsValid() + src6.IsValid();
887 int size = src0.SizeInBytes();
888
889 PushPreamble(count, size);
890 PushHelper(4, size, src0, src1, src2, src3);
891 PushHelper(count - 4, size, src4, src5, src6, src7);
892}
893
894
895void MacroAssembler::Pop(const CPURegister& dst0, const CPURegister& dst1,
896 const CPURegister& dst2, const CPURegister& dst3) {
897 // It is not valid to pop into the same register more than once in one
898 // instruction, not even into the zero register.
899 DCHECK(!AreAliased(dst0, dst1, dst2, dst3));
900 DCHECK(AreSameSizeAndType(dst0, dst1, dst2, dst3));
901 DCHECK(dst0.IsValid());
902
903 int count = 1 + dst1.IsValid() + dst2.IsValid() + dst3.IsValid();
904 int size = dst0.SizeInBytes();
905
906 PopHelper(count, size, dst0, dst1, dst2, dst3);
907 PopPostamble(count, size);
908}
909
910
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000911void MacroAssembler::Pop(const CPURegister& dst0, const CPURegister& dst1,
912 const CPURegister& dst2, const CPURegister& dst3,
913 const CPURegister& dst4, const CPURegister& dst5,
914 const CPURegister& dst6, const CPURegister& dst7) {
915 // It is not valid to pop into the same register more than once in one
916 // instruction, not even into the zero register.
917 DCHECK(!AreAliased(dst0, dst1, dst2, dst3, dst4, dst5, dst6, dst7));
918 DCHECK(AreSameSizeAndType(dst0, dst1, dst2, dst3, dst4, dst5, dst6, dst7));
919 DCHECK(dst0.IsValid());
920
921 int count = 5 + dst5.IsValid() + dst6.IsValid() + dst7.IsValid();
922 int size = dst0.SizeInBytes();
923
924 PopHelper(4, size, dst0, dst1, dst2, dst3);
925 PopHelper(count - 4, size, dst4, dst5, dst6, dst7);
926 PopPostamble(count, size);
927}
928
929
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000930void MacroAssembler::Push(const Register& src0, const FPRegister& src1) {
931 int size = src0.SizeInBytes() + src1.SizeInBytes();
932
933 PushPreamble(size);
934 // Reserve room for src0 and push src1.
935 str(src1, MemOperand(StackPointer(), -size, PreIndex));
936 // Fill the gap with src0.
937 str(src0, MemOperand(StackPointer(), src1.SizeInBytes()));
938}
939
940
941void MacroAssembler::PushPopQueue::PushQueued(
942 PreambleDirective preamble_directive) {
943 if (queued_.empty()) return;
944
945 if (preamble_directive == WITH_PREAMBLE) {
946 masm_->PushPreamble(size_);
947 }
948
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000949 size_t count = queued_.size();
950 size_t index = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000951 while (index < count) {
952 // PushHelper can only handle registers with the same size and type, and it
953 // can handle only four at a time. Batch them up accordingly.
954 CPURegister batch[4] = {NoReg, NoReg, NoReg, NoReg};
955 int batch_index = 0;
956 do {
957 batch[batch_index++] = queued_[index++];
958 } while ((batch_index < 4) && (index < count) &&
959 batch[0].IsSameSizeAndType(queued_[index]));
960
961 masm_->PushHelper(batch_index, batch[0].SizeInBytes(),
962 batch[0], batch[1], batch[2], batch[3]);
963 }
964
965 queued_.clear();
966}
967
968
969void MacroAssembler::PushPopQueue::PopQueued() {
970 if (queued_.empty()) return;
971
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000972 size_t count = queued_.size();
973 size_t index = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000974 while (index < count) {
975 // PopHelper can only handle registers with the same size and type, and it
976 // can handle only four at a time. Batch them up accordingly.
977 CPURegister batch[4] = {NoReg, NoReg, NoReg, NoReg};
978 int batch_index = 0;
979 do {
980 batch[batch_index++] = queued_[index++];
981 } while ((batch_index < 4) && (index < count) &&
982 batch[0].IsSameSizeAndType(queued_[index]));
983
984 masm_->PopHelper(batch_index, batch[0].SizeInBytes(),
985 batch[0], batch[1], batch[2], batch[3]);
986 }
987
988 masm_->PopPostamble(size_);
989 queued_.clear();
990}
991
992
993void MacroAssembler::PushCPURegList(CPURegList registers) {
994 int size = registers.RegisterSizeInBytes();
995
996 PushPreamble(registers.Count(), size);
997 // Push up to four registers at a time because if the current stack pointer is
998 // csp and reg_size is 32, registers must be pushed in blocks of four in order
999 // to maintain the 16-byte alignment for csp.
1000 while (!registers.IsEmpty()) {
1001 int count_before = registers.Count();
1002 const CPURegister& src0 = registers.PopHighestIndex();
1003 const CPURegister& src1 = registers.PopHighestIndex();
1004 const CPURegister& src2 = registers.PopHighestIndex();
1005 const CPURegister& src3 = registers.PopHighestIndex();
1006 int count = count_before - registers.Count();
1007 PushHelper(count, size, src0, src1, src2, src3);
1008 }
1009}
1010
1011
1012void MacroAssembler::PopCPURegList(CPURegList registers) {
1013 int size = registers.RegisterSizeInBytes();
1014
1015 // Pop up to four registers at a time because if the current stack pointer is
1016 // csp and reg_size is 32, registers must be pushed in blocks of four in
1017 // order to maintain the 16-byte alignment for csp.
1018 while (!registers.IsEmpty()) {
1019 int count_before = registers.Count();
1020 const CPURegister& dst0 = registers.PopLowestIndex();
1021 const CPURegister& dst1 = registers.PopLowestIndex();
1022 const CPURegister& dst2 = registers.PopLowestIndex();
1023 const CPURegister& dst3 = registers.PopLowestIndex();
1024 int count = count_before - registers.Count();
1025 PopHelper(count, size, dst0, dst1, dst2, dst3);
1026 }
1027 PopPostamble(registers.Count(), size);
1028}
1029
1030
1031void MacroAssembler::PushMultipleTimes(CPURegister src, int count) {
1032 int size = src.SizeInBytes();
1033
1034 PushPreamble(count, size);
1035
1036 if (FLAG_optimize_for_size && count > 8) {
1037 UseScratchRegisterScope temps(this);
1038 Register temp = temps.AcquireX();
1039
1040 Label loop;
1041 __ Mov(temp, count / 2);
1042 __ Bind(&loop);
1043 PushHelper(2, size, src, src, NoReg, NoReg);
1044 __ Subs(temp, temp, 1);
1045 __ B(ne, &loop);
1046
1047 count %= 2;
1048 }
1049
1050 // Push up to four registers at a time if possible because if the current
1051 // stack pointer is csp and the register size is 32, registers must be pushed
1052 // in blocks of four in order to maintain the 16-byte alignment for csp.
1053 while (count >= 4) {
1054 PushHelper(4, size, src, src, src, src);
1055 count -= 4;
1056 }
1057 if (count >= 2) {
1058 PushHelper(2, size, src, src, NoReg, NoReg);
1059 count -= 2;
1060 }
1061 if (count == 1) {
1062 PushHelper(1, size, src, NoReg, NoReg, NoReg);
1063 count -= 1;
1064 }
1065 DCHECK(count == 0);
1066}
1067
1068
1069void MacroAssembler::PushMultipleTimes(CPURegister src, Register count) {
1070 PushPreamble(Operand(count, UXTW, WhichPowerOf2(src.SizeInBytes())));
1071
1072 UseScratchRegisterScope temps(this);
1073 Register temp = temps.AcquireSameSizeAs(count);
1074
1075 if (FLAG_optimize_for_size) {
1076 Label loop, done;
1077
1078 Subs(temp, count, 1);
1079 B(mi, &done);
1080
1081 // Push all registers individually, to save code size.
1082 Bind(&loop);
1083 Subs(temp, temp, 1);
1084 PushHelper(1, src.SizeInBytes(), src, NoReg, NoReg, NoReg);
1085 B(pl, &loop);
1086
1087 Bind(&done);
1088 } else {
1089 Label loop, leftover2, leftover1, done;
1090
1091 Subs(temp, count, 4);
1092 B(mi, &leftover2);
1093
1094 // Push groups of four first.
1095 Bind(&loop);
1096 Subs(temp, temp, 4);
1097 PushHelper(4, src.SizeInBytes(), src, src, src, src);
1098 B(pl, &loop);
1099
1100 // Push groups of two.
1101 Bind(&leftover2);
1102 Tbz(count, 1, &leftover1);
1103 PushHelper(2, src.SizeInBytes(), src, src, NoReg, NoReg);
1104
1105 // Push the last one (if required).
1106 Bind(&leftover1);
1107 Tbz(count, 0, &done);
1108 PushHelper(1, src.SizeInBytes(), src, NoReg, NoReg, NoReg);
1109
1110 Bind(&done);
1111 }
1112}
1113
1114
1115void MacroAssembler::PushHelper(int count, int size,
1116 const CPURegister& src0,
1117 const CPURegister& src1,
1118 const CPURegister& src2,
1119 const CPURegister& src3) {
1120 // Ensure that we don't unintentially modify scratch or debug registers.
1121 InstructionAccurateScope scope(this);
1122
1123 DCHECK(AreSameSizeAndType(src0, src1, src2, src3));
1124 DCHECK(size == src0.SizeInBytes());
1125
1126 // When pushing multiple registers, the store order is chosen such that
1127 // Push(a, b) is equivalent to Push(a) followed by Push(b).
1128 switch (count) {
1129 case 1:
1130 DCHECK(src1.IsNone() && src2.IsNone() && src3.IsNone());
1131 str(src0, MemOperand(StackPointer(), -1 * size, PreIndex));
1132 break;
1133 case 2:
1134 DCHECK(src2.IsNone() && src3.IsNone());
1135 stp(src1, src0, MemOperand(StackPointer(), -2 * size, PreIndex));
1136 break;
1137 case 3:
1138 DCHECK(src3.IsNone());
1139 stp(src2, src1, MemOperand(StackPointer(), -3 * size, PreIndex));
1140 str(src0, MemOperand(StackPointer(), 2 * size));
1141 break;
1142 case 4:
1143 // Skip over 4 * size, then fill in the gap. This allows four W registers
1144 // to be pushed using csp, whilst maintaining 16-byte alignment for csp
1145 // at all times.
1146 stp(src3, src2, MemOperand(StackPointer(), -4 * size, PreIndex));
1147 stp(src1, src0, MemOperand(StackPointer(), 2 * size));
1148 break;
1149 default:
1150 UNREACHABLE();
1151 }
1152}
1153
1154
1155void MacroAssembler::PopHelper(int count, int size,
1156 const CPURegister& dst0,
1157 const CPURegister& dst1,
1158 const CPURegister& dst2,
1159 const CPURegister& dst3) {
1160 // Ensure that we don't unintentially modify scratch or debug registers.
1161 InstructionAccurateScope scope(this);
1162
1163 DCHECK(AreSameSizeAndType(dst0, dst1, dst2, dst3));
1164 DCHECK(size == dst0.SizeInBytes());
1165
1166 // When popping multiple registers, the load order is chosen such that
1167 // Pop(a, b) is equivalent to Pop(a) followed by Pop(b).
1168 switch (count) {
1169 case 1:
1170 DCHECK(dst1.IsNone() && dst2.IsNone() && dst3.IsNone());
1171 ldr(dst0, MemOperand(StackPointer(), 1 * size, PostIndex));
1172 break;
1173 case 2:
1174 DCHECK(dst2.IsNone() && dst3.IsNone());
1175 ldp(dst0, dst1, MemOperand(StackPointer(), 2 * size, PostIndex));
1176 break;
1177 case 3:
1178 DCHECK(dst3.IsNone());
1179 ldr(dst2, MemOperand(StackPointer(), 2 * size));
1180 ldp(dst0, dst1, MemOperand(StackPointer(), 3 * size, PostIndex));
1181 break;
1182 case 4:
1183 // Load the higher addresses first, then load the lower addresses and
1184 // skip the whole block in the second instruction. This allows four W
1185 // registers to be popped using csp, whilst maintaining 16-byte alignment
1186 // for csp at all times.
1187 ldp(dst2, dst3, MemOperand(StackPointer(), 2 * size));
1188 ldp(dst0, dst1, MemOperand(StackPointer(), 4 * size, PostIndex));
1189 break;
1190 default:
1191 UNREACHABLE();
1192 }
1193}
1194
1195
1196void MacroAssembler::PushPreamble(Operand total_size) {
1197 if (csp.Is(StackPointer())) {
1198 // If the current stack pointer is csp, then it must be aligned to 16 bytes
1199 // on entry and the total size of the specified registers must also be a
1200 // multiple of 16 bytes.
1201 if (total_size.IsImmediate()) {
1202 DCHECK((total_size.ImmediateValue() % 16) == 0);
1203 }
1204
1205 // Don't check access size for non-immediate sizes. It's difficult to do
1206 // well, and it will be caught by hardware (or the simulator) anyway.
1207 } else {
1208 // Even if the current stack pointer is not the system stack pointer (csp),
1209 // the system stack pointer will still be modified in order to comply with
1210 // ABI rules about accessing memory below the system stack pointer.
1211 BumpSystemStackPointer(total_size);
1212 }
1213}
1214
1215
1216void MacroAssembler::PopPostamble(Operand total_size) {
1217 if (csp.Is(StackPointer())) {
1218 // If the current stack pointer is csp, then it must be aligned to 16 bytes
1219 // on entry and the total size of the specified registers must also be a
1220 // multiple of 16 bytes.
1221 if (total_size.IsImmediate()) {
1222 DCHECK((total_size.ImmediateValue() % 16) == 0);
1223 }
1224
1225 // Don't check access size for non-immediate sizes. It's difficult to do
1226 // well, and it will be caught by hardware (or the simulator) anyway.
1227 } else if (emit_debug_code()) {
1228 // It is safe to leave csp where it is when unwinding the JavaScript stack,
1229 // but if we keep it matching StackPointer, the simulator can detect memory
1230 // accesses in the now-free part of the stack.
1231 SyncSystemStackPointer();
1232 }
1233}
1234
1235
1236void MacroAssembler::Poke(const CPURegister& src, const Operand& offset) {
1237 if (offset.IsImmediate()) {
1238 DCHECK(offset.ImmediateValue() >= 0);
1239 } else if (emit_debug_code()) {
1240 Cmp(xzr, offset);
1241 Check(le, kStackAccessBelowStackPointer);
1242 }
1243
1244 Str(src, MemOperand(StackPointer(), offset));
1245}
1246
1247
1248void MacroAssembler::Peek(const CPURegister& dst, const Operand& offset) {
1249 if (offset.IsImmediate()) {
1250 DCHECK(offset.ImmediateValue() >= 0);
1251 } else if (emit_debug_code()) {
1252 Cmp(xzr, offset);
1253 Check(le, kStackAccessBelowStackPointer);
1254 }
1255
1256 Ldr(dst, MemOperand(StackPointer(), offset));
1257}
1258
1259
1260void MacroAssembler::PokePair(const CPURegister& src1,
1261 const CPURegister& src2,
1262 int offset) {
1263 DCHECK(AreSameSizeAndType(src1, src2));
1264 DCHECK((offset >= 0) && ((offset % src1.SizeInBytes()) == 0));
1265 Stp(src1, src2, MemOperand(StackPointer(), offset));
1266}
1267
1268
1269void MacroAssembler::PeekPair(const CPURegister& dst1,
1270 const CPURegister& dst2,
1271 int offset) {
1272 DCHECK(AreSameSizeAndType(dst1, dst2));
1273 DCHECK((offset >= 0) && ((offset % dst1.SizeInBytes()) == 0));
1274 Ldp(dst1, dst2, MemOperand(StackPointer(), offset));
1275}
1276
1277
1278void MacroAssembler::PushCalleeSavedRegisters() {
1279 // Ensure that the macro-assembler doesn't use any scratch registers.
1280 InstructionAccurateScope scope(this);
1281
1282 // This method must not be called unless the current stack pointer is the
1283 // system stack pointer (csp).
1284 DCHECK(csp.Is(StackPointer()));
1285
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001286 MemOperand tos(csp, -2 * static_cast<int>(kXRegSize), PreIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001287
1288 stp(d14, d15, tos);
1289 stp(d12, d13, tos);
1290 stp(d10, d11, tos);
1291 stp(d8, d9, tos);
1292
1293 stp(x29, x30, tos);
1294 stp(x27, x28, tos); // x28 = jssp
1295 stp(x25, x26, tos);
1296 stp(x23, x24, tos);
1297 stp(x21, x22, tos);
1298 stp(x19, x20, tos);
1299}
1300
1301
1302void MacroAssembler::PopCalleeSavedRegisters() {
1303 // Ensure that the macro-assembler doesn't use any scratch registers.
1304 InstructionAccurateScope scope(this);
1305
1306 // This method must not be called unless the current stack pointer is the
1307 // system stack pointer (csp).
1308 DCHECK(csp.Is(StackPointer()));
1309
1310 MemOperand tos(csp, 2 * kXRegSize, PostIndex);
1311
1312 ldp(x19, x20, tos);
1313 ldp(x21, x22, tos);
1314 ldp(x23, x24, tos);
1315 ldp(x25, x26, tos);
1316 ldp(x27, x28, tos); // x28 = jssp
1317 ldp(x29, x30, tos);
1318
1319 ldp(d8, d9, tos);
1320 ldp(d10, d11, tos);
1321 ldp(d12, d13, tos);
1322 ldp(d14, d15, tos);
1323}
1324
1325
1326void MacroAssembler::AssertStackConsistency() {
1327 // Avoid emitting code when !use_real_abort() since non-real aborts cause too
1328 // much code to be generated.
1329 if (emit_debug_code() && use_real_aborts()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001330 if (csp.Is(StackPointer())) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001331 // Always check the alignment of csp if ALWAYS_ALIGN_CSP is true. We
1332 // can't check the alignment of csp without using a scratch register (or
1333 // clobbering the flags), but the processor (or simulator) will abort if
1334 // it is not properly aligned during a load.
1335 ldr(xzr, MemOperand(csp, 0));
1336 }
1337 if (FLAG_enable_slow_asserts && !csp.Is(StackPointer())) {
1338 Label ok;
1339 // Check that csp <= StackPointer(), preserving all registers and NZCV.
1340 sub(StackPointer(), csp, StackPointer());
1341 cbz(StackPointer(), &ok); // Ok if csp == StackPointer().
1342 tbnz(StackPointer(), kXSignBit, &ok); // Ok if csp < StackPointer().
1343
1344 // Avoid generating AssertStackConsistency checks for the Push in Abort.
1345 { DontEmitDebugCodeScope dont_emit_debug_code_scope(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001346 // Restore StackPointer().
1347 sub(StackPointer(), csp, StackPointer());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001348 Abort(kTheCurrentStackPointerIsBelowCsp);
1349 }
1350
1351 bind(&ok);
1352 // Restore StackPointer().
1353 sub(StackPointer(), csp, StackPointer());
1354 }
1355 }
1356}
1357
Ben Murdochda12d292016-06-02 14:46:10 +01001358void MacroAssembler::AssertCspAligned() {
1359 if (emit_debug_code() && use_real_aborts()) {
1360 // TODO(titzer): use a real assert for alignment check?
1361 UseScratchRegisterScope scope(this);
1362 Register temp = scope.AcquireX();
1363 ldr(temp, MemOperand(csp));
1364 }
1365}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001366
1367void MacroAssembler::AssertFPCRState(Register fpcr) {
1368 if (emit_debug_code()) {
1369 Label unexpected_mode, done;
1370 UseScratchRegisterScope temps(this);
1371 if (fpcr.IsNone()) {
1372 fpcr = temps.AcquireX();
1373 Mrs(fpcr, FPCR);
1374 }
1375
1376 // Settings overridden by ConfiugreFPCR():
1377 // - Assert that default-NaN mode is set.
1378 Tbz(fpcr, DN_offset, &unexpected_mode);
1379
1380 // Settings left to their default values:
1381 // - Assert that flush-to-zero is not set.
1382 Tbnz(fpcr, FZ_offset, &unexpected_mode);
1383 // - Assert that the rounding mode is nearest-with-ties-to-even.
1384 STATIC_ASSERT(FPTieEven == 0);
1385 Tst(fpcr, RMode_mask);
1386 B(eq, &done);
1387
1388 Bind(&unexpected_mode);
1389 Abort(kUnexpectedFPCRMode);
1390
1391 Bind(&done);
1392 }
1393}
1394
1395
1396void MacroAssembler::ConfigureFPCR() {
1397 UseScratchRegisterScope temps(this);
1398 Register fpcr = temps.AcquireX();
1399 Mrs(fpcr, FPCR);
1400
1401 // If necessary, enable default-NaN mode. The default values of the other FPCR
1402 // options should be suitable, and AssertFPCRState will verify that.
1403 Label no_write_required;
1404 Tbnz(fpcr, DN_offset, &no_write_required);
1405
1406 Orr(fpcr, fpcr, DN_mask);
1407 Msr(FPCR, fpcr);
1408
1409 Bind(&no_write_required);
1410 AssertFPCRState(fpcr);
1411}
1412
1413
1414void MacroAssembler::CanonicalizeNaN(const FPRegister& dst,
1415 const FPRegister& src) {
1416 AssertFPCRState();
1417
1418 // With DN=1 and RMode=FPTieEven, subtracting 0.0 preserves all inputs except
1419 // for NaNs, which become the default NaN. We use fsub rather than fadd
1420 // because sub preserves -0.0 inputs: -0.0 + 0.0 = 0.0, but -0.0 - 0.0 = -0.0.
1421 Fsub(dst, src, fp_zero);
1422}
1423
1424
1425void MacroAssembler::LoadRoot(CPURegister destination,
1426 Heap::RootListIndex index) {
1427 // TODO(jbramley): Most root values are constants, and can be synthesized
1428 // without a load. Refer to the ARM back end for details.
1429 Ldr(destination, MemOperand(root, index << kPointerSizeLog2));
1430}
1431
1432
1433void MacroAssembler::StoreRoot(Register source,
1434 Heap::RootListIndex index) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001435 DCHECK(Heap::RootCanBeWrittenAfterInitialization(index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001436 Str(source, MemOperand(root, index << kPointerSizeLog2));
1437}
1438
1439
1440void MacroAssembler::LoadTrueFalseRoots(Register true_root,
1441 Register false_root) {
1442 STATIC_ASSERT((Heap::kTrueValueRootIndex + 1) == Heap::kFalseValueRootIndex);
1443 Ldp(true_root, false_root,
1444 MemOperand(root, Heap::kTrueValueRootIndex << kPointerSizeLog2));
1445}
1446
1447
1448void MacroAssembler::LoadHeapObject(Register result,
1449 Handle<HeapObject> object) {
1450 AllowDeferredHandleDereference using_raw_address;
1451 if (isolate()->heap()->InNewSpace(*object)) {
1452 Handle<Cell> cell = isolate()->factory()->NewCell(object);
1453 Mov(result, Operand(cell));
1454 Ldr(result, FieldMemOperand(result, Cell::kValueOffset));
1455 } else {
1456 Mov(result, Operand(object));
1457 }
1458}
1459
1460
1461void MacroAssembler::LoadInstanceDescriptors(Register map,
1462 Register descriptors) {
1463 Ldr(descriptors, FieldMemOperand(map, Map::kDescriptorsOffset));
1464}
1465
1466
1467void MacroAssembler::NumberOfOwnDescriptors(Register dst, Register map) {
1468 Ldr(dst, FieldMemOperand(map, Map::kBitField3Offset));
1469 DecodeField<Map::NumberOfOwnDescriptorsBits>(dst);
1470}
1471
1472
1473void MacroAssembler::EnumLengthUntagged(Register dst, Register map) {
1474 STATIC_ASSERT(Map::EnumLengthBits::kShift == 0);
1475 Ldrsw(dst, FieldMemOperand(map, Map::kBitField3Offset));
1476 And(dst, dst, Map::EnumLengthBits::kMask);
1477}
1478
1479
1480void MacroAssembler::EnumLengthSmi(Register dst, Register map) {
1481 EnumLengthUntagged(dst, map);
1482 SmiTag(dst, dst);
1483}
1484
1485
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001486void MacroAssembler::LoadAccessor(Register dst, Register holder,
1487 int accessor_index,
1488 AccessorComponent accessor) {
1489 Ldr(dst, FieldMemOperand(holder, HeapObject::kMapOffset));
1490 LoadInstanceDescriptors(dst, dst);
1491 Ldr(dst,
1492 FieldMemOperand(dst, DescriptorArray::GetValueOffset(accessor_index)));
1493 int offset = accessor == ACCESSOR_GETTER ? AccessorPair::kGetterOffset
1494 : AccessorPair::kSetterOffset;
1495 Ldr(dst, FieldMemOperand(dst, offset));
1496}
1497
1498
Ben Murdoch097c5b22016-05-18 11:27:45 +01001499void MacroAssembler::CheckEnumCache(Register object, Register scratch0,
1500 Register scratch1, Register scratch2,
1501 Register scratch3, Register scratch4,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001502 Label* call_runtime) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001503 DCHECK(!AreAliased(object, scratch0, scratch1, scratch2, scratch3, scratch4));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001504
1505 Register empty_fixed_array_value = scratch0;
1506 Register current_object = scratch1;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001507 Register null_value = scratch4;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001508
1509 LoadRoot(empty_fixed_array_value, Heap::kEmptyFixedArrayRootIndex);
1510 Label next, start;
1511
1512 Mov(current_object, object);
1513
1514 // Check if the enum length field is properly initialized, indicating that
1515 // there is an enum cache.
1516 Register map = scratch2;
1517 Register enum_length = scratch3;
1518 Ldr(map, FieldMemOperand(current_object, HeapObject::kMapOffset));
1519
1520 EnumLengthUntagged(enum_length, map);
1521 Cmp(enum_length, kInvalidEnumCacheSentinel);
1522 B(eq, call_runtime);
1523
Ben Murdoch097c5b22016-05-18 11:27:45 +01001524 LoadRoot(null_value, Heap::kNullValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001525 B(&start);
1526
1527 Bind(&next);
1528 Ldr(map, FieldMemOperand(current_object, HeapObject::kMapOffset));
1529
1530 // For all objects but the receiver, check that the cache is empty.
1531 EnumLengthUntagged(enum_length, map);
1532 Cbnz(enum_length, call_runtime);
1533
1534 Bind(&start);
1535
1536 // Check that there are no elements. Register current_object contains the
1537 // current JS object we've reached through the prototype chain.
1538 Label no_elements;
1539 Ldr(current_object, FieldMemOperand(current_object,
1540 JSObject::kElementsOffset));
1541 Cmp(current_object, empty_fixed_array_value);
1542 B(eq, &no_elements);
1543
1544 // Second chance, the object may be using the empty slow element dictionary.
1545 CompareRoot(current_object, Heap::kEmptySlowElementDictionaryRootIndex);
1546 B(ne, call_runtime);
1547
1548 Bind(&no_elements);
1549 Ldr(current_object, FieldMemOperand(map, Map::kPrototypeOffset));
1550 Cmp(current_object, null_value);
1551 B(ne, &next);
1552}
1553
1554
1555void MacroAssembler::TestJSArrayForAllocationMemento(Register receiver,
1556 Register scratch1,
1557 Register scratch2,
1558 Label* no_memento_found) {
Ben Murdochda12d292016-06-02 14:46:10 +01001559 Label map_check;
1560 Label top_check;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001561 ExternalReference new_space_allocation_top =
1562 ExternalReference::new_space_allocation_top_address(isolate());
Ben Murdochda12d292016-06-02 14:46:10 +01001563 const int kMementoMapOffset = JSArray::kSize - kHeapObjectTag;
1564 const int kMementoEndOffset = kMementoMapOffset + AllocationMemento::kSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565
Ben Murdochda12d292016-06-02 14:46:10 +01001566 // Bail out if the object is not in new space.
1567 JumpIfNotInNewSpace(receiver, no_memento_found);
1568 Add(scratch1, receiver, kMementoEndOffset);
1569 // If the object is in new space, we need to check whether it is on the same
1570 // page as the current top.
1571 Eor(scratch2, scratch1, new_space_allocation_top);
1572 Tst(scratch2, ~Page::kPageAlignmentMask);
1573 B(eq, &top_check);
1574 // The object is on a different page than allocation top. Bail out if the
1575 // object sits on the page boundary as no memento can follow and we cannot
1576 // touch the memory following it.
1577 Eor(scratch2, scratch1, receiver);
1578 Tst(scratch2, ~Page::kPageAlignmentMask);
1579 B(ne, no_memento_found);
1580 // Continue with the actual map check.
1581 jmp(&map_check);
1582 // If top is on the same page as the current object, we need to check whether
1583 // we are below top.
1584 bind(&top_check);
1585 Cmp(scratch1, new_space_allocation_top);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001586 B(gt, no_memento_found);
Ben Murdochda12d292016-06-02 14:46:10 +01001587 // Memento map check.
1588 bind(&map_check);
1589 Ldr(scratch1, MemOperand(receiver, kMementoMapOffset));
1590 Cmp(scratch1, Operand(isolate()->factory()->allocation_memento_map()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001591}
1592
1593
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001594void MacroAssembler::InNewSpace(Register object,
1595 Condition cond,
1596 Label* branch) {
1597 DCHECK(cond == eq || cond == ne);
1598 UseScratchRegisterScope temps(this);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001599 const int mask =
1600 (1 << MemoryChunk::IN_FROM_SPACE) | (1 << MemoryChunk::IN_TO_SPACE);
1601 CheckPageFlag(object, temps.AcquireSameSizeAs(object), mask, cond, branch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001602}
1603
1604
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001605void MacroAssembler::AssertSmi(Register object, BailoutReason reason) {
1606 if (emit_debug_code()) {
1607 STATIC_ASSERT(kSmiTag == 0);
1608 Tst(object, kSmiTagMask);
1609 Check(eq, reason);
1610 }
1611}
1612
1613
1614void MacroAssembler::AssertNotSmi(Register object, BailoutReason reason) {
1615 if (emit_debug_code()) {
1616 STATIC_ASSERT(kSmiTag == 0);
1617 Tst(object, kSmiTagMask);
1618 Check(ne, reason);
1619 }
1620}
1621
1622
1623void MacroAssembler::AssertName(Register object) {
1624 if (emit_debug_code()) {
1625 AssertNotSmi(object, kOperandIsASmiAndNotAName);
1626
1627 UseScratchRegisterScope temps(this);
1628 Register temp = temps.AcquireX();
1629
1630 Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
1631 CompareInstanceType(temp, temp, LAST_NAME_TYPE);
1632 Check(ls, kOperandIsNotAName);
1633 }
1634}
1635
1636
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001637void MacroAssembler::AssertFunction(Register object) {
1638 if (emit_debug_code()) {
1639 AssertNotSmi(object, kOperandIsASmiAndNotAFunction);
1640
1641 UseScratchRegisterScope temps(this);
1642 Register temp = temps.AcquireX();
1643
1644 CompareObjectType(object, temp, temp, JS_FUNCTION_TYPE);
1645 Check(eq, kOperandIsNotAFunction);
1646 }
1647}
1648
1649
1650void MacroAssembler::AssertBoundFunction(Register object) {
1651 if (emit_debug_code()) {
1652 AssertNotSmi(object, kOperandIsASmiAndNotABoundFunction);
1653
1654 UseScratchRegisterScope temps(this);
1655 Register temp = temps.AcquireX();
1656
1657 CompareObjectType(object, temp, temp, JS_BOUND_FUNCTION_TYPE);
1658 Check(eq, kOperandIsNotABoundFunction);
1659 }
1660}
1661
1662
Ben Murdoch097c5b22016-05-18 11:27:45 +01001663void MacroAssembler::AssertReceiver(Register object) {
1664 if (emit_debug_code()) {
1665 AssertNotSmi(object, kOperandIsASmiAndNotAReceiver);
1666
1667 UseScratchRegisterScope temps(this);
1668 Register temp = temps.AcquireX();
1669
1670 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
1671 CompareObjectType(object, temp, temp, FIRST_JS_RECEIVER_TYPE);
1672 Check(hs, kOperandIsNotAReceiver);
1673 }
1674}
1675
1676
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001677void MacroAssembler::AssertUndefinedOrAllocationSite(Register object,
1678 Register scratch) {
1679 if (emit_debug_code()) {
1680 Label done_checking;
1681 AssertNotSmi(object);
1682 JumpIfRoot(object, Heap::kUndefinedValueRootIndex, &done_checking);
1683 Ldr(scratch, FieldMemOperand(object, HeapObject::kMapOffset));
1684 CompareRoot(scratch, Heap::kAllocationSiteMapRootIndex);
1685 Assert(eq, kExpectedUndefinedOrCell);
1686 Bind(&done_checking);
1687 }
1688}
1689
1690
1691void MacroAssembler::AssertString(Register object) {
1692 if (emit_debug_code()) {
1693 UseScratchRegisterScope temps(this);
1694 Register temp = temps.AcquireX();
1695 STATIC_ASSERT(kSmiTag == 0);
1696 Tst(object, kSmiTagMask);
1697 Check(ne, kOperandIsASmiAndNotAString);
1698 Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
1699 CompareInstanceType(temp, temp, FIRST_NONSTRING_TYPE);
1700 Check(lo, kOperandIsNotAString);
1701 }
1702}
1703
1704
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001705void MacroAssembler::AssertPositiveOrZero(Register value) {
1706 if (emit_debug_code()) {
1707 Label done;
1708 int sign_bit = value.Is64Bits() ? kXSignBit : kWSignBit;
1709 Tbz(value, sign_bit, &done);
1710 Abort(kUnexpectedNegativeValue);
1711 Bind(&done);
1712 }
1713}
1714
Ben Murdochda12d292016-06-02 14:46:10 +01001715void MacroAssembler::AssertNotNumber(Register value) {
1716 if (emit_debug_code()) {
1717 STATIC_ASSERT(kSmiTag == 0);
1718 Tst(value, kSmiTagMask);
1719 Check(ne, kOperandIsANumber);
1720 Label done;
1721 JumpIfNotHeapNumber(value, &done);
1722 Abort(kOperandIsANumber);
1723 Bind(&done);
1724 }
1725}
1726
Ben Murdoch097c5b22016-05-18 11:27:45 +01001727void MacroAssembler::AssertNumber(Register value) {
1728 if (emit_debug_code()) {
1729 Label done;
1730 JumpIfSmi(value, &done);
1731 JumpIfHeapNumber(value, &done);
1732 Abort(kOperandIsNotANumber);
1733 Bind(&done);
1734 }
1735}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001736
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001737void MacroAssembler::CallStub(CodeStub* stub, TypeFeedbackId ast_id) {
1738 DCHECK(AllowThisStubCall(stub)); // Stub calls are not allowed in some stubs.
1739 Call(stub->GetCode(), RelocInfo::CODE_TARGET, ast_id);
1740}
1741
1742
1743void MacroAssembler::TailCallStub(CodeStub* stub) {
1744 Jump(stub->GetCode(), RelocInfo::CODE_TARGET);
1745}
1746
1747
1748void MacroAssembler::CallRuntime(const Runtime::Function* f,
1749 int num_arguments,
1750 SaveFPRegsMode save_doubles) {
1751 // All arguments must be on the stack before this function is called.
1752 // x0 holds the return value after the call.
1753
1754 // Check that the number of arguments matches what the function expects.
1755 // If f->nargs is -1, the function can accept a variable number of arguments.
1756 CHECK(f->nargs < 0 || f->nargs == num_arguments);
1757
1758 // Place the necessary arguments.
1759 Mov(x0, num_arguments);
1760 Mov(x1, ExternalReference(f, isolate()));
1761
1762 CEntryStub stub(isolate(), 1, save_doubles);
1763 CallStub(&stub);
1764}
1765
1766
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001767void MacroAssembler::CallExternalReference(const ExternalReference& ext,
1768 int num_arguments) {
1769 Mov(x0, num_arguments);
1770 Mov(x1, ext);
1771
1772 CEntryStub stub(isolate(), 1);
1773 CallStub(&stub);
1774}
1775
1776
1777void MacroAssembler::JumpToExternalReference(const ExternalReference& builtin) {
1778 Mov(x1, builtin);
1779 CEntryStub stub(isolate(), 1);
1780 Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1781}
1782
1783
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001784void MacroAssembler::TailCallRuntime(Runtime::FunctionId fid) {
1785 const Runtime::Function* function = Runtime::FunctionForId(fid);
1786 DCHECK_EQ(1, function->result_size);
1787 if (function->nargs >= 0) {
1788 // TODO(1236192): Most runtime routines don't need the number of
1789 // arguments passed in because it is constant. At some point we
1790 // should remove this need and make the runtime routine entry code
1791 // smarter.
1792 Mov(x0, function->nargs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001793 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001794 JumpToExternalReference(ExternalReference(fid, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001795}
1796
1797
1798void MacroAssembler::InitializeNewString(Register string,
1799 Register length,
1800 Heap::RootListIndex map_index,
1801 Register scratch1,
1802 Register scratch2) {
1803 DCHECK(!AreAliased(string, length, scratch1, scratch2));
1804 LoadRoot(scratch2, map_index);
1805 SmiTag(scratch1, length);
1806 Str(scratch2, FieldMemOperand(string, HeapObject::kMapOffset));
1807
1808 Mov(scratch2, String::kEmptyHashField);
1809 Str(scratch1, FieldMemOperand(string, String::kLengthOffset));
1810 Str(scratch2, FieldMemOperand(string, String::kHashFieldOffset));
1811}
1812
1813
1814int MacroAssembler::ActivationFrameAlignment() {
1815#if V8_HOST_ARCH_ARM64
1816 // Running on the real platform. Use the alignment as mandated by the local
1817 // environment.
1818 // Note: This will break if we ever start generating snapshots on one ARM
1819 // platform for another ARM platform with a different alignment.
1820 return base::OS::ActivationFrameAlignment();
1821#else // V8_HOST_ARCH_ARM64
1822 // If we are using the simulator then we should always align to the expected
1823 // alignment. As the simulator is used to generate snapshots we do not know
1824 // if the target platform will need alignment, so this is controlled from a
1825 // flag.
1826 return FLAG_sim_stack_alignment;
1827#endif // V8_HOST_ARCH_ARM64
1828}
1829
1830
1831void MacroAssembler::CallCFunction(ExternalReference function,
1832 int num_of_reg_args) {
1833 CallCFunction(function, num_of_reg_args, 0);
1834}
1835
1836
1837void MacroAssembler::CallCFunction(ExternalReference function,
1838 int num_of_reg_args,
1839 int num_of_double_args) {
1840 UseScratchRegisterScope temps(this);
1841 Register temp = temps.AcquireX();
1842 Mov(temp, function);
1843 CallCFunction(temp, num_of_reg_args, num_of_double_args);
1844}
1845
1846
1847void MacroAssembler::CallCFunction(Register function,
1848 int num_of_reg_args,
1849 int num_of_double_args) {
1850 DCHECK(has_frame());
1851 // We can pass 8 integer arguments in registers. If we need to pass more than
1852 // that, we'll need to implement support for passing them on the stack.
1853 DCHECK(num_of_reg_args <= 8);
1854
1855 // If we're passing doubles, we're limited to the following prototypes
1856 // (defined by ExternalReference::Type):
1857 // BUILTIN_COMPARE_CALL: int f(double, double)
1858 // BUILTIN_FP_FP_CALL: double f(double, double)
1859 // BUILTIN_FP_CALL: double f(double)
1860 // BUILTIN_FP_INT_CALL: double f(double, int)
1861 if (num_of_double_args > 0) {
1862 DCHECK(num_of_reg_args <= 1);
1863 DCHECK((num_of_double_args + num_of_reg_args) <= 2);
1864 }
1865
1866
1867 // If the stack pointer is not csp, we need to derive an aligned csp from the
1868 // current stack pointer.
1869 const Register old_stack_pointer = StackPointer();
1870 if (!csp.Is(old_stack_pointer)) {
1871 AssertStackConsistency();
1872
1873 int sp_alignment = ActivationFrameAlignment();
1874 // The ABI mandates at least 16-byte alignment.
1875 DCHECK(sp_alignment >= 16);
1876 DCHECK(base::bits::IsPowerOfTwo32(sp_alignment));
1877
1878 // The current stack pointer is a callee saved register, and is preserved
1879 // across the call.
1880 DCHECK(kCalleeSaved.IncludesAliasOf(old_stack_pointer));
1881
1882 // Align and synchronize the system stack pointer with jssp.
1883 Bic(csp, old_stack_pointer, sp_alignment - 1);
1884 SetStackPointer(csp);
1885 }
1886
1887 // Call directly. The function called cannot cause a GC, or allow preemption,
1888 // so the return address in the link register stays correct.
1889 Call(function);
1890
1891 if (!csp.Is(old_stack_pointer)) {
1892 if (emit_debug_code()) {
1893 // Because the stack pointer must be aligned on a 16-byte boundary, the
1894 // aligned csp can be up to 12 bytes below the jssp. This is the case
1895 // where we only pushed one W register on top of an aligned jssp.
1896 UseScratchRegisterScope temps(this);
1897 Register temp = temps.AcquireX();
1898 DCHECK(ActivationFrameAlignment() == 16);
1899 Sub(temp, csp, old_stack_pointer);
1900 // We want temp <= 0 && temp >= -12.
1901 Cmp(temp, 0);
1902 Ccmp(temp, -12, NFlag, le);
1903 Check(ge, kTheStackWasCorruptedByMacroAssemblerCall);
1904 }
1905 SetStackPointer(old_stack_pointer);
1906 }
1907}
1908
1909
1910void MacroAssembler::Jump(Register target) {
1911 Br(target);
1912}
1913
1914
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001915void MacroAssembler::Jump(intptr_t target, RelocInfo::Mode rmode,
1916 Condition cond) {
1917 if (cond == nv) return;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001918 UseScratchRegisterScope temps(this);
1919 Register temp = temps.AcquireX();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001920 Label done;
1921 if (cond != al) B(NegateCondition(cond), &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001922 Mov(temp, Operand(target, rmode));
1923 Br(temp);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001924 Bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001925}
1926
1927
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001928void MacroAssembler::Jump(Address target, RelocInfo::Mode rmode,
1929 Condition cond) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001930 DCHECK(!RelocInfo::IsCodeTarget(rmode));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001931 Jump(reinterpret_cast<intptr_t>(target), rmode, cond);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001932}
1933
1934
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001935void MacroAssembler::Jump(Handle<Code> code, RelocInfo::Mode rmode,
1936 Condition cond) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001937 DCHECK(RelocInfo::IsCodeTarget(rmode));
1938 AllowDeferredHandleDereference embedding_raw_address;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001939 Jump(reinterpret_cast<intptr_t>(code.location()), rmode, cond);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001940}
1941
1942
1943void MacroAssembler::Call(Register target) {
1944 BlockPoolsScope scope(this);
1945#ifdef DEBUG
1946 Label start_call;
1947 Bind(&start_call);
1948#endif
1949
1950 Blr(target);
1951
1952#ifdef DEBUG
1953 AssertSizeOfCodeGeneratedSince(&start_call, CallSize(target));
1954#endif
1955}
1956
1957
1958void MacroAssembler::Call(Label* target) {
1959 BlockPoolsScope scope(this);
1960#ifdef DEBUG
1961 Label start_call;
1962 Bind(&start_call);
1963#endif
1964
1965 Bl(target);
1966
1967#ifdef DEBUG
1968 AssertSizeOfCodeGeneratedSince(&start_call, CallSize(target));
1969#endif
1970}
1971
1972
1973// MacroAssembler::CallSize is sensitive to changes in this function, as it
1974// requires to know how many instructions are used to branch to the target.
1975void MacroAssembler::Call(Address target, RelocInfo::Mode rmode) {
1976 BlockPoolsScope scope(this);
1977#ifdef DEBUG
1978 Label start_call;
1979 Bind(&start_call);
1980#endif
1981 // Statement positions are expected to be recorded when the target
1982 // address is loaded.
1983 positions_recorder()->WriteRecordedPositions();
1984
1985 // Addresses always have 64 bits, so we shouldn't encounter NONE32.
1986 DCHECK(rmode != RelocInfo::NONE32);
1987
1988 UseScratchRegisterScope temps(this);
1989 Register temp = temps.AcquireX();
1990
1991 if (rmode == RelocInfo::NONE64) {
1992 // Addresses are 48 bits so we never need to load the upper 16 bits.
1993 uint64_t imm = reinterpret_cast<uint64_t>(target);
1994 // If we don't use ARM tagged addresses, the 16 higher bits must be 0.
1995 DCHECK(((imm >> 48) & 0xffff) == 0);
1996 movz(temp, (imm >> 0) & 0xffff, 0);
1997 movk(temp, (imm >> 16) & 0xffff, 16);
1998 movk(temp, (imm >> 32) & 0xffff, 32);
1999 } else {
2000 Ldr(temp, Immediate(reinterpret_cast<intptr_t>(target), rmode));
2001 }
2002 Blr(temp);
2003#ifdef DEBUG
2004 AssertSizeOfCodeGeneratedSince(&start_call, CallSize(target, rmode));
2005#endif
2006}
2007
2008
2009void MacroAssembler::Call(Handle<Code> code,
2010 RelocInfo::Mode rmode,
2011 TypeFeedbackId ast_id) {
2012#ifdef DEBUG
2013 Label start_call;
2014 Bind(&start_call);
2015#endif
2016
2017 if ((rmode == RelocInfo::CODE_TARGET) && (!ast_id.IsNone())) {
2018 SetRecordedAstId(ast_id);
2019 rmode = RelocInfo::CODE_TARGET_WITH_ID;
2020 }
2021
2022 AllowDeferredHandleDereference embedding_raw_address;
2023 Call(reinterpret_cast<Address>(code.location()), rmode);
2024
2025#ifdef DEBUG
2026 // Check the size of the code generated.
2027 AssertSizeOfCodeGeneratedSince(&start_call, CallSize(code, rmode, ast_id));
2028#endif
2029}
2030
2031
2032int MacroAssembler::CallSize(Register target) {
2033 USE(target);
2034 return kInstructionSize;
2035}
2036
2037
2038int MacroAssembler::CallSize(Label* target) {
2039 USE(target);
2040 return kInstructionSize;
2041}
2042
2043
2044int MacroAssembler::CallSize(Address target, RelocInfo::Mode rmode) {
2045 USE(target);
2046
2047 // Addresses always have 64 bits, so we shouldn't encounter NONE32.
2048 DCHECK(rmode != RelocInfo::NONE32);
2049
2050 if (rmode == RelocInfo::NONE64) {
2051 return kCallSizeWithoutRelocation;
2052 } else {
2053 return kCallSizeWithRelocation;
2054 }
2055}
2056
2057
2058int MacroAssembler::CallSize(Handle<Code> code,
2059 RelocInfo::Mode rmode,
2060 TypeFeedbackId ast_id) {
2061 USE(code);
2062 USE(ast_id);
2063
2064 // Addresses always have 64 bits, so we shouldn't encounter NONE32.
2065 DCHECK(rmode != RelocInfo::NONE32);
2066
2067 if (rmode == RelocInfo::NONE64) {
2068 return kCallSizeWithoutRelocation;
2069 } else {
2070 return kCallSizeWithRelocation;
2071 }
2072}
2073
2074
2075void MacroAssembler::JumpIfHeapNumber(Register object, Label* on_heap_number,
2076 SmiCheckType smi_check_type) {
2077 Label on_not_heap_number;
2078
2079 if (smi_check_type == DO_SMI_CHECK) {
2080 JumpIfSmi(object, &on_not_heap_number);
2081 }
2082
2083 AssertNotSmi(object);
2084
2085 UseScratchRegisterScope temps(this);
2086 Register temp = temps.AcquireX();
2087 Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
2088 JumpIfRoot(temp, Heap::kHeapNumberMapRootIndex, on_heap_number);
2089
2090 Bind(&on_not_heap_number);
2091}
2092
2093
2094void MacroAssembler::JumpIfNotHeapNumber(Register object,
2095 Label* on_not_heap_number,
2096 SmiCheckType smi_check_type) {
2097 if (smi_check_type == DO_SMI_CHECK) {
2098 JumpIfSmi(object, on_not_heap_number);
2099 }
2100
2101 AssertNotSmi(object);
2102
2103 UseScratchRegisterScope temps(this);
2104 Register temp = temps.AcquireX();
2105 Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
2106 JumpIfNotRoot(temp, Heap::kHeapNumberMapRootIndex, on_not_heap_number);
2107}
2108
2109
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002110void MacroAssembler::TryRepresentDoubleAsInt(Register as_int,
2111 FPRegister value,
2112 FPRegister scratch_d,
2113 Label* on_successful_conversion,
2114 Label* on_failed_conversion) {
2115 // Convert to an int and back again, then compare with the original value.
2116 Fcvtzs(as_int, value);
2117 Scvtf(scratch_d, as_int);
2118 Fcmp(value, scratch_d);
2119
2120 if (on_successful_conversion) {
2121 B(on_successful_conversion, eq);
2122 }
2123 if (on_failed_conversion) {
2124 B(on_failed_conversion, ne);
2125 }
2126}
2127
2128
2129void MacroAssembler::TestForMinusZero(DoubleRegister input) {
2130 UseScratchRegisterScope temps(this);
2131 Register temp = temps.AcquireX();
2132 // Floating point -0.0 is kMinInt as an integer, so subtracting 1 (cmp) will
2133 // cause overflow.
2134 Fmov(temp, input);
2135 Cmp(temp, 1);
2136}
2137
2138
2139void MacroAssembler::JumpIfMinusZero(DoubleRegister input,
2140 Label* on_negative_zero) {
2141 TestForMinusZero(input);
2142 B(vs, on_negative_zero);
2143}
2144
2145
2146void MacroAssembler::JumpIfMinusZero(Register input,
2147 Label* on_negative_zero) {
2148 DCHECK(input.Is64Bits());
2149 // Floating point value is in an integer register. Detect -0.0 by subtracting
2150 // 1 (cmp), which will cause overflow.
2151 Cmp(input, 1);
2152 B(vs, on_negative_zero);
2153}
2154
2155
2156void MacroAssembler::ClampInt32ToUint8(Register output, Register input) {
2157 // Clamp the value to [0..255].
2158 Cmp(input.W(), Operand(input.W(), UXTB));
2159 // If input < input & 0xff, it must be < 0, so saturate to 0.
2160 Csel(output.W(), wzr, input.W(), lt);
2161 // If input <= input & 0xff, it must be <= 255. Otherwise, saturate to 255.
2162 Csel(output.W(), output.W(), 255, le);
2163}
2164
2165
2166void MacroAssembler::ClampInt32ToUint8(Register in_out) {
2167 ClampInt32ToUint8(in_out, in_out);
2168}
2169
2170
2171void MacroAssembler::ClampDoubleToUint8(Register output,
2172 DoubleRegister input,
2173 DoubleRegister dbl_scratch) {
2174 // This conversion follows the WebIDL "[Clamp]" rules for PIXEL types:
2175 // - Inputs lower than 0 (including -infinity) produce 0.
2176 // - Inputs higher than 255 (including +infinity) produce 255.
2177 // Also, it seems that PIXEL types use round-to-nearest rather than
2178 // round-towards-zero.
2179
2180 // Squash +infinity before the conversion, since Fcvtnu will normally
2181 // convert it to 0.
2182 Fmov(dbl_scratch, 255);
2183 Fmin(dbl_scratch, dbl_scratch, input);
2184
2185 // Convert double to unsigned integer. Values less than zero become zero.
2186 // Values greater than 255 have already been clamped to 255.
2187 Fcvtnu(output, dbl_scratch);
2188}
2189
2190
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002191void MacroAssembler::CopyBytes(Register dst,
2192 Register src,
2193 Register length,
2194 Register scratch,
2195 CopyHint hint) {
2196 UseScratchRegisterScope temps(this);
2197 Register tmp1 = temps.AcquireX();
2198 Register tmp2 = temps.AcquireX();
2199 DCHECK(!AreAliased(src, dst, length, scratch, tmp1, tmp2));
2200 DCHECK(!AreAliased(src, dst, csp));
2201
2202 if (emit_debug_code()) {
2203 // Check copy length.
2204 Cmp(length, 0);
2205 Assert(ge, kUnexpectedNegativeValue);
2206
2207 // Check src and dst buffers don't overlap.
2208 Add(scratch, src, length); // Calculate end of src buffer.
2209 Cmp(scratch, dst);
2210 Add(scratch, dst, length); // Calculate end of dst buffer.
2211 Ccmp(scratch, src, ZFlag, gt);
2212 Assert(le, kCopyBuffersOverlap);
2213 }
2214
2215 Label short_copy, short_loop, bulk_loop, done;
2216
2217 if ((hint == kCopyLong || hint == kCopyUnknown) && !FLAG_optimize_for_size) {
2218 Register bulk_length = scratch;
2219 int pair_size = 2 * kXRegSize;
2220 int pair_mask = pair_size - 1;
2221
2222 Bic(bulk_length, length, pair_mask);
2223 Cbz(bulk_length, &short_copy);
2224 Bind(&bulk_loop);
2225 Sub(bulk_length, bulk_length, pair_size);
2226 Ldp(tmp1, tmp2, MemOperand(src, pair_size, PostIndex));
2227 Stp(tmp1, tmp2, MemOperand(dst, pair_size, PostIndex));
2228 Cbnz(bulk_length, &bulk_loop);
2229
2230 And(length, length, pair_mask);
2231 }
2232
2233 Bind(&short_copy);
2234 Cbz(length, &done);
2235 Bind(&short_loop);
2236 Sub(length, length, 1);
2237 Ldrb(tmp1, MemOperand(src, 1, PostIndex));
2238 Strb(tmp1, MemOperand(dst, 1, PostIndex));
2239 Cbnz(length, &short_loop);
2240
2241
2242 Bind(&done);
2243}
2244
2245
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002246void MacroAssembler::InitializeFieldsWithFiller(Register current_address,
2247 Register end_address,
2248 Register filler) {
2249 DCHECK(!current_address.Is(csp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002250 UseScratchRegisterScope temps(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002251 Register distance_in_words = temps.AcquireX();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002252 Label done;
2253
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002254 // Calculate the distance. If it's <= zero then there's nothing to do.
2255 Subs(distance_in_words, end_address, current_address);
2256 B(le, &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002257
2258 // There's at least one field to fill, so do this unconditionally.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002259 Str(filler, MemOperand(current_address));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002260
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002261 // If the distance_in_words consists of odd number of words we advance
2262 // start_address by one word, otherwise the pairs loop will ovwerite the
2263 // field that was stored above.
2264 And(distance_in_words, distance_in_words, kPointerSize);
2265 Add(current_address, current_address, distance_in_words);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002266
2267 // Store filler to memory in pairs.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002268 Label loop, entry;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002269 B(&entry);
2270 Bind(&loop);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002271 Stp(filler, filler, MemOperand(current_address, 2 * kPointerSize, PostIndex));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002272 Bind(&entry);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002273 Cmp(current_address, end_address);
2274 B(lo, &loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002275
2276 Bind(&done);
2277}
2278
2279
2280void MacroAssembler::JumpIfEitherIsNotSequentialOneByteStrings(
2281 Register first, Register second, Register scratch1, Register scratch2,
2282 Label* failure, SmiCheckType smi_check) {
2283 if (smi_check == DO_SMI_CHECK) {
2284 JumpIfEitherSmi(first, second, failure);
2285 } else if (emit_debug_code()) {
2286 DCHECK(smi_check == DONT_DO_SMI_CHECK);
2287 Label not_smi;
2288 JumpIfEitherSmi(first, second, NULL, &not_smi);
2289
2290 // At least one input is a smi, but the flags indicated a smi check wasn't
2291 // needed.
2292 Abort(kUnexpectedSmi);
2293
2294 Bind(&not_smi);
2295 }
2296
2297 // Test that both first and second are sequential one-byte strings.
2298 Ldr(scratch1, FieldMemOperand(first, HeapObject::kMapOffset));
2299 Ldr(scratch2, FieldMemOperand(second, HeapObject::kMapOffset));
2300 Ldrb(scratch1, FieldMemOperand(scratch1, Map::kInstanceTypeOffset));
2301 Ldrb(scratch2, FieldMemOperand(scratch2, Map::kInstanceTypeOffset));
2302
2303 JumpIfEitherInstanceTypeIsNotSequentialOneByte(scratch1, scratch2, scratch1,
2304 scratch2, failure);
2305}
2306
2307
2308void MacroAssembler::JumpIfEitherInstanceTypeIsNotSequentialOneByte(
2309 Register first, Register second, Register scratch1, Register scratch2,
2310 Label* failure) {
2311 DCHECK(!AreAliased(scratch1, second));
2312 DCHECK(!AreAliased(scratch1, scratch2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002313 const int kFlatOneByteStringMask =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002314 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002315 const int kFlatOneByteStringTag =
2316 kStringTag | kOneByteStringTag | kSeqStringTag;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002317 And(scratch1, first, kFlatOneByteStringMask);
2318 And(scratch2, second, kFlatOneByteStringMask);
2319 Cmp(scratch1, kFlatOneByteStringTag);
2320 Ccmp(scratch2, kFlatOneByteStringTag, NoFlag, eq);
2321 B(ne, failure);
2322}
2323
2324
2325void MacroAssembler::JumpIfInstanceTypeIsNotSequentialOneByte(Register type,
2326 Register scratch,
2327 Label* failure) {
2328 const int kFlatOneByteStringMask =
2329 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2330 const int kFlatOneByteStringTag =
2331 kStringTag | kOneByteStringTag | kSeqStringTag;
2332 And(scratch, type, kFlatOneByteStringMask);
2333 Cmp(scratch, kFlatOneByteStringTag);
2334 B(ne, failure);
2335}
2336
2337
2338void MacroAssembler::JumpIfBothInstanceTypesAreNotSequentialOneByte(
2339 Register first, Register second, Register scratch1, Register scratch2,
2340 Label* failure) {
2341 DCHECK(!AreAliased(first, second, scratch1, scratch2));
2342 const int kFlatOneByteStringMask =
2343 kIsNotStringMask | kStringEncodingMask | kStringRepresentationMask;
2344 const int kFlatOneByteStringTag =
2345 kStringTag | kOneByteStringTag | kSeqStringTag;
2346 And(scratch1, first, kFlatOneByteStringMask);
2347 And(scratch2, second, kFlatOneByteStringMask);
2348 Cmp(scratch1, kFlatOneByteStringTag);
2349 Ccmp(scratch2, kFlatOneByteStringTag, NoFlag, eq);
2350 B(ne, failure);
2351}
2352
2353
2354void MacroAssembler::JumpIfNotUniqueNameInstanceType(Register type,
2355 Label* not_unique_name) {
2356 STATIC_ASSERT((kInternalizedTag == 0) && (kStringTag == 0));
2357 // if ((type is string && type is internalized) || type == SYMBOL_TYPE) {
2358 // continue
2359 // } else {
2360 // goto not_unique_name
2361 // }
2362 Tst(type, kIsNotStringMask | kIsNotInternalizedMask);
2363 Ccmp(type, SYMBOL_TYPE, ZFlag, ne);
2364 B(ne, not_unique_name);
2365}
2366
Ben Murdochda12d292016-06-02 14:46:10 +01002367void MacroAssembler::PrepareForTailCall(const ParameterCount& callee_args_count,
2368 Register caller_args_count_reg,
2369 Register scratch0, Register scratch1) {
2370#if DEBUG
2371 if (callee_args_count.is_reg()) {
2372 DCHECK(!AreAliased(callee_args_count.reg(), caller_args_count_reg, scratch0,
2373 scratch1));
2374 } else {
2375 DCHECK(!AreAliased(caller_args_count_reg, scratch0, scratch1));
2376 }
2377#endif
2378
2379 // Calculate the end of destination area where we will put the arguments
2380 // after we drop current frame. We add kPointerSize to count the receiver
2381 // argument which is not included into formal parameters count.
2382 Register dst_reg = scratch0;
2383 __ add(dst_reg, fp, Operand(caller_args_count_reg, LSL, kPointerSizeLog2));
2384 __ add(dst_reg, dst_reg,
2385 Operand(StandardFrameConstants::kCallerSPOffset + kPointerSize));
2386
2387 Register src_reg = caller_args_count_reg;
2388 // Calculate the end of source area. +kPointerSize is for the receiver.
2389 if (callee_args_count.is_reg()) {
2390 add(src_reg, jssp, Operand(callee_args_count.reg(), LSL, kPointerSizeLog2));
2391 add(src_reg, src_reg, Operand(kPointerSize));
2392 } else {
2393 add(src_reg, jssp,
2394 Operand((callee_args_count.immediate() + 1) * kPointerSize));
2395 }
2396
2397 if (FLAG_debug_code) {
2398 __ Cmp(src_reg, dst_reg);
2399 __ Check(lo, kStackAccessBelowStackPointer);
2400 }
2401
2402 // Restore caller's frame pointer and return address now as they will be
2403 // overwritten by the copying loop.
2404 __ Ldr(lr, MemOperand(fp, StandardFrameConstants::kCallerPCOffset));
2405 __ Ldr(fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2406
2407 // Now copy callee arguments to the caller frame going backwards to avoid
2408 // callee arguments corruption (source and destination areas could overlap).
2409
2410 // Both src_reg and dst_reg are pointing to the word after the one to copy,
2411 // so they must be pre-decremented in the loop.
2412 Register tmp_reg = scratch1;
2413 Label loop, entry;
2414 __ B(&entry);
2415 __ bind(&loop);
2416 __ Ldr(tmp_reg, MemOperand(src_reg, -kPointerSize, PreIndex));
2417 __ Str(tmp_reg, MemOperand(dst_reg, -kPointerSize, PreIndex));
2418 __ bind(&entry);
2419 __ Cmp(jssp, src_reg);
2420 __ B(ne, &loop);
2421
2422 // Leave current frame.
2423 __ Mov(jssp, dst_reg);
2424 __ SetStackPointer(jssp);
2425 __ AssertStackConsistency();
2426}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002427
2428void MacroAssembler::InvokePrologue(const ParameterCount& expected,
2429 const ParameterCount& actual,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002430 Label* done,
2431 InvokeFlag flag,
2432 bool* definitely_mismatches,
2433 const CallWrapper& call_wrapper) {
2434 bool definitely_matches = false;
2435 *definitely_mismatches = false;
2436 Label regular_invoke;
2437
2438 // Check whether the expected and actual arguments count match. If not,
2439 // setup registers according to contract with ArgumentsAdaptorTrampoline:
2440 // x0: actual arguments count.
2441 // x1: function (passed through to callee).
2442 // x2: expected arguments count.
2443
2444 // The code below is made a lot easier because the calling code already sets
2445 // up actual and expected registers according to the contract if values are
2446 // passed in registers.
2447 DCHECK(actual.is_immediate() || actual.reg().is(x0));
2448 DCHECK(expected.is_immediate() || expected.reg().is(x2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002449
2450 if (expected.is_immediate()) {
2451 DCHECK(actual.is_immediate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002452 Mov(x0, actual.immediate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002453 if (expected.immediate() == actual.immediate()) {
2454 definitely_matches = true;
2455
2456 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002457 if (expected.immediate() ==
2458 SharedFunctionInfo::kDontAdaptArgumentsSentinel) {
2459 // Don't worry about adapting arguments for builtins that
2460 // don't want that done. Skip adaption code by making it look
2461 // like we have a match between expected and actual number of
2462 // arguments.
2463 definitely_matches = true;
2464 } else {
2465 *definitely_mismatches = true;
2466 // Set up x2 for the argument adaptor.
2467 Mov(x2, expected.immediate());
2468 }
2469 }
2470
2471 } else { // expected is a register.
2472 Operand actual_op = actual.is_immediate() ? Operand(actual.immediate())
2473 : Operand(actual.reg());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002474 Mov(x0, actual_op);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002475 // If actual == expected perform a regular invocation.
2476 Cmp(expected.reg(), actual_op);
2477 B(eq, &regular_invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002478 }
2479
2480 // If the argument counts may mismatch, generate a call to the argument
2481 // adaptor.
2482 if (!definitely_matches) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002483 Handle<Code> adaptor =
2484 isolate()->builtins()->ArgumentsAdaptorTrampoline();
2485 if (flag == CALL_FUNCTION) {
2486 call_wrapper.BeforeCall(CallSize(adaptor));
2487 Call(adaptor);
2488 call_wrapper.AfterCall();
2489 if (!*definitely_mismatches) {
2490 // If the arg counts don't match, no extra code is emitted by
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002491 // MAsm::InvokeFunctionCode and we can just fall through.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002492 B(done);
2493 }
2494 } else {
2495 Jump(adaptor, RelocInfo::CODE_TARGET);
2496 }
2497 }
2498 Bind(&regular_invoke);
2499}
2500
2501
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002502void MacroAssembler::FloodFunctionIfStepping(Register fun, Register new_target,
2503 const ParameterCount& expected,
2504 const ParameterCount& actual) {
2505 Label skip_flooding;
2506 ExternalReference step_in_enabled =
2507 ExternalReference::debug_step_in_enabled_address(isolate());
2508 Mov(x4, Operand(step_in_enabled));
2509 ldrb(x4, MemOperand(x4));
2510 CompareAndBranch(x4, Operand(0), eq, &skip_flooding);
2511 {
2512 FrameScope frame(this,
2513 has_frame() ? StackFrame::NONE : StackFrame::INTERNAL);
2514 if (expected.is_reg()) {
2515 SmiTag(expected.reg());
2516 Push(expected.reg());
2517 }
2518 if (actual.is_reg()) {
2519 SmiTag(actual.reg());
2520 Push(actual.reg());
2521 }
2522 if (new_target.is_valid()) {
2523 Push(new_target);
2524 }
2525 Push(fun);
2526 Push(fun);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002527 CallRuntime(Runtime::kDebugPrepareStepInIfStepping);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002528 Pop(fun);
2529 if (new_target.is_valid()) {
2530 Pop(new_target);
2531 }
2532 if (actual.is_reg()) {
2533 Pop(actual.reg());
2534 SmiUntag(actual.reg());
2535 }
2536 if (expected.is_reg()) {
2537 Pop(expected.reg());
2538 SmiUntag(expected.reg());
2539 }
2540 }
2541 bind(&skip_flooding);
2542}
2543
2544
2545void MacroAssembler::InvokeFunctionCode(Register function, Register new_target,
2546 const ParameterCount& expected,
2547 const ParameterCount& actual,
2548 InvokeFlag flag,
2549 const CallWrapper& call_wrapper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002550 // You can't call a function without a valid frame.
2551 DCHECK(flag == JUMP_FUNCTION || has_frame());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002552 DCHECK(function.is(x1));
2553 DCHECK_IMPLIES(new_target.is_valid(), new_target.is(x3));
2554
2555 FloodFunctionIfStepping(function, new_target, expected, actual);
2556
2557 // Clear the new.target register if not given.
2558 if (!new_target.is_valid()) {
2559 LoadRoot(x3, Heap::kUndefinedValueRootIndex);
2560 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002561
2562 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002563 bool definitely_mismatches = false;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002564 InvokePrologue(expected, actual, &done, flag, &definitely_mismatches,
2565 call_wrapper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002566
2567 // If we are certain that actual != expected, then we know InvokePrologue will
2568 // have handled the call through the argument adaptor mechanism.
2569 // The called function expects the call kind in x5.
2570 if (!definitely_mismatches) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002571 // We call indirectly through the code field in the function to
2572 // allow recompilation to take effect without changing any of the
2573 // call sites.
2574 Register code = x4;
2575 Ldr(code, FieldMemOperand(function, JSFunction::kCodeEntryOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002576 if (flag == CALL_FUNCTION) {
2577 call_wrapper.BeforeCall(CallSize(code));
2578 Call(code);
2579 call_wrapper.AfterCall();
2580 } else {
2581 DCHECK(flag == JUMP_FUNCTION);
2582 Jump(code);
2583 }
2584 }
2585
2586 // Continue here if InvokePrologue does handle the invocation due to
2587 // mismatched parameter counts.
2588 Bind(&done);
2589}
2590
2591
2592void MacroAssembler::InvokeFunction(Register function,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002593 Register new_target,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002594 const ParameterCount& actual,
2595 InvokeFlag flag,
2596 const CallWrapper& call_wrapper) {
2597 // You can't call a function without a valid frame.
2598 DCHECK(flag == JUMP_FUNCTION || has_frame());
2599
2600 // Contract with called JS functions requires that function is passed in x1.
2601 // (See FullCodeGenerator::Generate().)
2602 DCHECK(function.is(x1));
2603
2604 Register expected_reg = x2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002605
2606 Ldr(cp, FieldMemOperand(function, JSFunction::kContextOffset));
2607 // The number of arguments is stored as an int32_t, and -1 is a marker
2608 // (SharedFunctionInfo::kDontAdaptArgumentsSentinel), so we need sign
2609 // extension to correctly handle it.
2610 Ldr(expected_reg, FieldMemOperand(function,
2611 JSFunction::kSharedFunctionInfoOffset));
2612 Ldrsw(expected_reg,
2613 FieldMemOperand(expected_reg,
2614 SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002615
2616 ParameterCount expected(expected_reg);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002617 InvokeFunctionCode(function, new_target, expected, actual, flag,
2618 call_wrapper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002619}
2620
2621
2622void MacroAssembler::InvokeFunction(Register function,
2623 const ParameterCount& expected,
2624 const ParameterCount& actual,
2625 InvokeFlag flag,
2626 const CallWrapper& call_wrapper) {
2627 // You can't call a function without a valid frame.
2628 DCHECK(flag == JUMP_FUNCTION || has_frame());
2629
2630 // Contract with called JS functions requires that function is passed in x1.
2631 // (See FullCodeGenerator::Generate().)
2632 DCHECK(function.Is(x1));
2633
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002634 // Set up the context.
2635 Ldr(cp, FieldMemOperand(function, JSFunction::kContextOffset));
2636
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002637 InvokeFunctionCode(function, no_reg, expected, actual, flag, call_wrapper);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002638}
2639
2640
2641void MacroAssembler::InvokeFunction(Handle<JSFunction> function,
2642 const ParameterCount& expected,
2643 const ParameterCount& actual,
2644 InvokeFlag flag,
2645 const CallWrapper& call_wrapper) {
2646 // Contract with called JS functions requires that function is passed in x1.
2647 // (See FullCodeGenerator::Generate().)
2648 __ LoadObject(x1, function);
2649 InvokeFunction(x1, expected, actual, flag, call_wrapper);
2650}
2651
2652
2653void MacroAssembler::TryConvertDoubleToInt64(Register result,
2654 DoubleRegister double_input,
2655 Label* done) {
2656 // Try to convert with an FPU convert instruction. It's trivial to compute
2657 // the modulo operation on an integer register so we convert to a 64-bit
2658 // integer.
2659 //
2660 // Fcvtzs will saturate to INT64_MIN (0x800...00) or INT64_MAX (0x7ff...ff)
2661 // when the double is out of range. NaNs and infinities will be converted to 0
2662 // (as ECMA-262 requires).
2663 Fcvtzs(result.X(), double_input);
2664
2665 // The values INT64_MIN (0x800...00) or INT64_MAX (0x7ff...ff) are not
2666 // representable using a double, so if the result is one of those then we know
2667 // that saturation occured, and we need to manually handle the conversion.
2668 //
2669 // It is easy to detect INT64_MIN and INT64_MAX because adding or subtracting
2670 // 1 will cause signed overflow.
2671 Cmp(result.X(), 1);
2672 Ccmp(result.X(), -1, VFlag, vc);
2673
2674 B(vc, done);
2675}
2676
2677
2678void MacroAssembler::TruncateDoubleToI(Register result,
2679 DoubleRegister double_input) {
2680 Label done;
2681
2682 // Try to convert the double to an int64. If successful, the bottom 32 bits
2683 // contain our truncated int32 result.
2684 TryConvertDoubleToInt64(result, double_input, &done);
2685
2686 const Register old_stack_pointer = StackPointer();
2687 if (csp.Is(old_stack_pointer)) {
2688 // This currently only happens during compiler-unittest. If it arises
2689 // during regular code generation the DoubleToI stub should be updated to
2690 // cope with csp and have an extra parameter indicating which stack pointer
2691 // it should use.
2692 Push(jssp, xzr); // Push xzr to maintain csp required 16-bytes alignment.
2693 Mov(jssp, csp);
2694 SetStackPointer(jssp);
2695 }
2696
2697 // If we fell through then inline version didn't succeed - call stub instead.
2698 Push(lr, double_input);
2699
2700 DoubleToIStub stub(isolate(),
2701 jssp,
2702 result,
2703 0,
2704 true, // is_truncating
2705 true); // skip_fastpath
2706 CallStub(&stub); // DoubleToIStub preserves any registers it needs to clobber
2707
2708 DCHECK_EQ(xzr.SizeInBytes(), double_input.SizeInBytes());
2709 Pop(xzr, lr); // xzr to drop the double input on the stack.
2710
2711 if (csp.Is(old_stack_pointer)) {
2712 Mov(csp, jssp);
2713 SetStackPointer(csp);
2714 AssertStackConsistency();
2715 Pop(xzr, jssp);
2716 }
2717
2718 Bind(&done);
2719}
2720
2721
2722void MacroAssembler::TruncateHeapNumberToI(Register result,
2723 Register object) {
2724 Label done;
2725 DCHECK(!result.is(object));
2726 DCHECK(jssp.Is(StackPointer()));
2727
2728 Ldr(fp_scratch, FieldMemOperand(object, HeapNumber::kValueOffset));
2729
2730 // Try to convert the double to an int64. If successful, the bottom 32 bits
2731 // contain our truncated int32 result.
2732 TryConvertDoubleToInt64(result, fp_scratch, &done);
2733
2734 // If we fell through then inline version didn't succeed - call stub instead.
2735 Push(lr);
2736 DoubleToIStub stub(isolate(),
2737 object,
2738 result,
2739 HeapNumber::kValueOffset - kHeapObjectTag,
2740 true, // is_truncating
2741 true); // skip_fastpath
2742 CallStub(&stub); // DoubleToIStub preserves any registers it needs to clobber
2743 Pop(lr);
2744
2745 Bind(&done);
2746}
2747
Ben Murdochda12d292016-06-02 14:46:10 +01002748void MacroAssembler::StubPrologue(StackFrame::Type type, int frame_slots) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002749 UseScratchRegisterScope temps(this);
Ben Murdochda12d292016-06-02 14:46:10 +01002750 frame_slots -= TypedFrameConstants::kFixedSlotCountAboveFp;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002751 Register temp = temps.AcquireX();
Ben Murdochda12d292016-06-02 14:46:10 +01002752 Mov(temp, Smi::FromInt(type));
2753 Push(lr, fp);
2754 Mov(fp, StackPointer());
2755 Claim(frame_slots);
2756 str(temp, MemOperand(fp, TypedFrameConstants::kFrameTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002757}
2758
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002759void MacroAssembler::Prologue(bool code_pre_aging) {
2760 if (code_pre_aging) {
2761 Code* stub = Code::GetPreAgedCodeAgeStub(isolate());
2762 __ EmitCodeAgeSequence(stub);
2763 } else {
2764 __ EmitFrameSetupForCodeAgePatching();
2765 }
2766}
2767
2768
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002769void MacroAssembler::EmitLoadTypeFeedbackVector(Register vector) {
2770 Ldr(vector, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
2771 Ldr(vector, FieldMemOperand(vector, JSFunction::kSharedFunctionInfoOffset));
2772 Ldr(vector,
2773 FieldMemOperand(vector, SharedFunctionInfo::kFeedbackVectorOffset));
2774}
2775
2776
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002777void MacroAssembler::EnterFrame(StackFrame::Type type,
2778 bool load_constant_pool_pointer_reg) {
2779 // Out-of-line constant pool not implemented on arm64.
2780 UNREACHABLE();
2781}
2782
2783
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002784void MacroAssembler::EnterFrame(StackFrame::Type type) {
2785 DCHECK(jssp.Is(StackPointer()));
2786 UseScratchRegisterScope temps(this);
2787 Register type_reg = temps.AcquireX();
2788 Register code_reg = temps.AcquireX();
2789
Ben Murdochda12d292016-06-02 14:46:10 +01002790 if (type == StackFrame::INTERNAL) {
2791 Mov(type_reg, Smi::FromInt(type));
2792 Push(lr, fp);
2793 Push(type_reg);
2794 Mov(code_reg, Operand(CodeObject()));
2795 Push(code_reg);
2796 Add(fp, jssp, InternalFrameConstants::kFixedFrameSizeFromFp);
2797 // jssp[4] : lr
2798 // jssp[3] : fp
2799 // jssp[1] : type
2800 // jssp[0] : [code object]
2801 } else {
2802 Mov(type_reg, Smi::FromInt(type));
2803 Push(lr, fp);
2804 Push(type_reg);
2805 Add(fp, jssp, TypedFrameConstants::kFixedFrameSizeFromFp);
2806 // jssp[2] : lr
2807 // jssp[1] : fp
2808 // jssp[0] : type
2809 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002810}
2811
2812
2813void MacroAssembler::LeaveFrame(StackFrame::Type type) {
2814 DCHECK(jssp.Is(StackPointer()));
2815 // Drop the execution stack down to the frame pointer and restore
2816 // the caller frame pointer and return address.
2817 Mov(jssp, fp);
2818 AssertStackConsistency();
2819 Pop(fp, lr);
2820}
2821
2822
2823void MacroAssembler::ExitFramePreserveFPRegs() {
2824 PushCPURegList(kCallerSavedFP);
2825}
2826
2827
2828void MacroAssembler::ExitFrameRestoreFPRegs() {
2829 // Read the registers from the stack without popping them. The stack pointer
2830 // will be reset as part of the unwinding process.
2831 CPURegList saved_fp_regs = kCallerSavedFP;
2832 DCHECK(saved_fp_regs.Count() % 2 == 0);
2833
2834 int offset = ExitFrameConstants::kLastExitFrameField;
2835 while (!saved_fp_regs.IsEmpty()) {
2836 const CPURegister& dst0 = saved_fp_regs.PopHighestIndex();
2837 const CPURegister& dst1 = saved_fp_regs.PopHighestIndex();
2838 offset -= 2 * kDRegSize;
2839 Ldp(dst1, dst0, MemOperand(fp, offset));
2840 }
2841}
2842
2843
2844void MacroAssembler::EnterExitFrame(bool save_doubles,
2845 const Register& scratch,
2846 int extra_space) {
2847 DCHECK(jssp.Is(StackPointer()));
2848
2849 // Set up the new stack frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002850 Push(lr, fp);
2851 Mov(fp, StackPointer());
Ben Murdochda12d292016-06-02 14:46:10 +01002852 Mov(scratch, Smi::FromInt(StackFrame::EXIT));
2853 Push(scratch);
2854 Push(xzr);
2855 Mov(scratch, Operand(CodeObject()));
2856 Push(scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002857 // fp[8]: CallerPC (lr)
2858 // fp -> fp[0]: CallerFP (old fp)
Ben Murdochda12d292016-06-02 14:46:10 +01002859 // fp[-8]: STUB marker
2860 // fp[-16]: Space reserved for SPOffset.
2861 // jssp -> fp[-24]: CodeObject()
2862 STATIC_ASSERT((2 * kPointerSize) == ExitFrameConstants::kCallerSPOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002863 STATIC_ASSERT((1 * kPointerSize) == ExitFrameConstants::kCallerPCOffset);
2864 STATIC_ASSERT((0 * kPointerSize) == ExitFrameConstants::kCallerFPOffset);
Ben Murdochda12d292016-06-02 14:46:10 +01002865 STATIC_ASSERT((-2 * kPointerSize) == ExitFrameConstants::kSPOffset);
2866 STATIC_ASSERT((-3 * kPointerSize) == ExitFrameConstants::kCodeOffset);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002867
2868 // Save the frame pointer and context pointer in the top frame.
2869 Mov(scratch, Operand(ExternalReference(Isolate::kCEntryFPAddress,
2870 isolate())));
2871 Str(fp, MemOperand(scratch));
2872 Mov(scratch, Operand(ExternalReference(Isolate::kContextAddress,
2873 isolate())));
2874 Str(cp, MemOperand(scratch));
2875
Ben Murdochda12d292016-06-02 14:46:10 +01002876 STATIC_ASSERT((-3 * kPointerSize) == ExitFrameConstants::kLastExitFrameField);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002877 if (save_doubles) {
2878 ExitFramePreserveFPRegs();
2879 }
2880
2881 // Reserve space for the return address and for user requested memory.
2882 // We do this before aligning to make sure that we end up correctly
2883 // aligned with the minimum of wasted space.
2884 Claim(extra_space + 1, kXRegSize);
2885 // fp[8]: CallerPC (lr)
2886 // fp -> fp[0]: CallerFP (old fp)
Ben Murdochda12d292016-06-02 14:46:10 +01002887 // fp[-8]: STUB marker
2888 // fp[-16]: Space reserved for SPOffset.
2889 // fp[-24]: CodeObject()
2890 // fp[-24 - fp_size]: Saved doubles (if save_doubles is true).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002891 // jssp[8]: Extra space reserved for caller (if extra_space != 0).
2892 // jssp -> jssp[0]: Space reserved for the return address.
2893
2894 // Align and synchronize the system stack pointer with jssp.
2895 AlignAndSetCSPForFrame();
2896 DCHECK(csp.Is(StackPointer()));
2897
2898 // fp[8]: CallerPC (lr)
2899 // fp -> fp[0]: CallerFP (old fp)
Ben Murdochda12d292016-06-02 14:46:10 +01002900 // fp[-8]: STUB marker
2901 // fp[-16]: Space reserved for SPOffset.
2902 // fp[-24]: CodeObject()
2903 // fp[-24 - fp_size]: Saved doubles (if save_doubles is true).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002904 // csp[8]: Memory reserved for the caller if extra_space != 0.
2905 // Alignment padding, if necessary.
2906 // csp -> csp[0]: Space reserved for the return address.
2907
2908 // ExitFrame::GetStateForFramePointer expects to find the return address at
2909 // the memory address immediately below the pointer stored in SPOffset.
2910 // It is not safe to derive much else from SPOffset, because the size of the
2911 // padding can vary.
2912 Add(scratch, csp, kXRegSize);
2913 Str(scratch, MemOperand(fp, ExitFrameConstants::kSPOffset));
2914}
2915
2916
2917// Leave the current exit frame.
2918void MacroAssembler::LeaveExitFrame(bool restore_doubles,
2919 const Register& scratch,
2920 bool restore_context) {
2921 DCHECK(csp.Is(StackPointer()));
2922
2923 if (restore_doubles) {
2924 ExitFrameRestoreFPRegs();
2925 }
2926
2927 // Restore the context pointer from the top frame.
2928 if (restore_context) {
2929 Mov(scratch, Operand(ExternalReference(Isolate::kContextAddress,
2930 isolate())));
2931 Ldr(cp, MemOperand(scratch));
2932 }
2933
2934 if (emit_debug_code()) {
2935 // Also emit debug code to clear the cp in the top frame.
2936 Mov(scratch, Operand(ExternalReference(Isolate::kContextAddress,
2937 isolate())));
2938 Str(xzr, MemOperand(scratch));
2939 }
2940 // Clear the frame pointer from the top frame.
2941 Mov(scratch, Operand(ExternalReference(Isolate::kCEntryFPAddress,
2942 isolate())));
2943 Str(xzr, MemOperand(scratch));
2944
2945 // Pop the exit frame.
2946 // fp[8]: CallerPC (lr)
2947 // fp -> fp[0]: CallerFP (old fp)
2948 // fp[...]: The rest of the frame.
2949 Mov(jssp, fp);
2950 SetStackPointer(jssp);
2951 AssertStackConsistency();
2952 Pop(fp, lr);
2953}
2954
2955
2956void MacroAssembler::SetCounter(StatsCounter* counter, int value,
2957 Register scratch1, Register scratch2) {
2958 if (FLAG_native_code_counters && counter->Enabled()) {
2959 Mov(scratch1, value);
2960 Mov(scratch2, ExternalReference(counter));
2961 Str(scratch1, MemOperand(scratch2));
2962 }
2963}
2964
2965
2966void MacroAssembler::IncrementCounter(StatsCounter* counter, int value,
2967 Register scratch1, Register scratch2) {
2968 DCHECK(value != 0);
2969 if (FLAG_native_code_counters && counter->Enabled()) {
2970 Mov(scratch2, ExternalReference(counter));
2971 Ldr(scratch1, MemOperand(scratch2));
2972 Add(scratch1, scratch1, value);
2973 Str(scratch1, MemOperand(scratch2));
2974 }
2975}
2976
2977
2978void MacroAssembler::DecrementCounter(StatsCounter* counter, int value,
2979 Register scratch1, Register scratch2) {
2980 IncrementCounter(counter, -value, scratch1, scratch2);
2981}
2982
2983
2984void MacroAssembler::LoadContext(Register dst, int context_chain_length) {
2985 if (context_chain_length > 0) {
2986 // Move up the chain of contexts to the context containing the slot.
2987 Ldr(dst, MemOperand(cp, Context::SlotOffset(Context::PREVIOUS_INDEX)));
2988 for (int i = 1; i < context_chain_length; i++) {
2989 Ldr(dst, MemOperand(dst, Context::SlotOffset(Context::PREVIOUS_INDEX)));
2990 }
2991 } else {
2992 // Slot is in the current function context. Move it into the
2993 // destination register in case we store into it (the write barrier
2994 // cannot be allowed to destroy the context in cp).
2995 Mov(dst, cp);
2996 }
2997}
2998
2999
3000void MacroAssembler::DebugBreak() {
3001 Mov(x0, 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003002 Mov(x1, ExternalReference(Runtime::kHandleDebuggerStatement, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003003 CEntryStub ces(isolate(), 1);
3004 DCHECK(AllowThisStubCall(&ces));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003005 Call(ces.GetCode(), RelocInfo::DEBUGGER_STATEMENT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003006}
3007
3008
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003009void MacroAssembler::PushStackHandler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003010 DCHECK(jssp.Is(StackPointer()));
3011 // Adjust this code if the asserts don't hold.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003012 STATIC_ASSERT(StackHandlerConstants::kSize == 1 * kPointerSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003013 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0 * kPointerSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003014
3015 // For the JSEntry handler, we must preserve the live registers x0-x4.
3016 // (See JSEntryStub::GenerateBody().)
3017
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003018 // Link the current handler as the next handler.
3019 Mov(x11, ExternalReference(Isolate::kHandlerAddress, isolate()));
3020 Ldr(x10, MemOperand(x11));
3021 Push(x10);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003022
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003023 // Set this new handler as the current one.
3024 Str(jssp, MemOperand(x11));
3025}
3026
3027
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003028void MacroAssembler::PopStackHandler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003029 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
3030 Pop(x10);
3031 Mov(x11, ExternalReference(Isolate::kHandlerAddress, isolate()));
3032 Drop(StackHandlerConstants::kSize - kXRegSize, kByteSizeInBytes);
3033 Str(x10, MemOperand(x11));
3034}
3035
3036
3037void MacroAssembler::Allocate(int object_size,
3038 Register result,
3039 Register scratch1,
3040 Register scratch2,
3041 Label* gc_required,
3042 AllocationFlags flags) {
3043 DCHECK(object_size <= Page::kMaxRegularHeapObjectSize);
3044 if (!FLAG_inline_new) {
3045 if (emit_debug_code()) {
3046 // Trash the registers to simulate an allocation failure.
3047 // We apply salt to the original zap value to easily spot the values.
3048 Mov(result, (kDebugZapValue & ~0xffL) | 0x11L);
3049 Mov(scratch1, (kDebugZapValue & ~0xffL) | 0x21L);
3050 Mov(scratch2, (kDebugZapValue & ~0xffL) | 0x21L);
3051 }
3052 B(gc_required);
3053 return;
3054 }
3055
3056 UseScratchRegisterScope temps(this);
3057 Register scratch3 = temps.AcquireX();
3058
3059 DCHECK(!AreAliased(result, scratch1, scratch2, scratch3));
3060 DCHECK(result.Is64Bits() && scratch1.Is64Bits() && scratch2.Is64Bits());
3061
3062 // Make object size into bytes.
3063 if ((flags & SIZE_IN_WORDS) != 0) {
3064 object_size *= kPointerSize;
3065 }
3066 DCHECK(0 == (object_size & kObjectAlignmentMask));
3067
3068 // Check relative positions of allocation top and limit addresses.
3069 // The values must be adjacent in memory to allow the use of LDP.
3070 ExternalReference heap_allocation_top =
3071 AllocationUtils::GetAllocationTopReference(isolate(), flags);
3072 ExternalReference heap_allocation_limit =
3073 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
3074 intptr_t top = reinterpret_cast<intptr_t>(heap_allocation_top.address());
3075 intptr_t limit = reinterpret_cast<intptr_t>(heap_allocation_limit.address());
3076 DCHECK((limit - top) == kPointerSize);
3077
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003078 // Set up allocation top address and allocation limit registers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003079 Register top_address = scratch1;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003080 Register alloc_limit = scratch2;
3081 Register result_end = scratch3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003082 Mov(top_address, Operand(heap_allocation_top));
3083
3084 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003085 // Load allocation top into result and allocation limit into alloc_limit.
3086 Ldp(result, alloc_limit, MemOperand(top_address));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003087 } else {
3088 if (emit_debug_code()) {
3089 // Assert that result actually contains top on entry.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003090 Ldr(alloc_limit, MemOperand(top_address));
3091 Cmp(result, alloc_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003092 Check(eq, kUnexpectedAllocationTop);
3093 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003094 // Load allocation limit. Result already contains allocation top.
3095 Ldr(alloc_limit, MemOperand(top_address, limit - top));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003096 }
3097
3098 // We can ignore DOUBLE_ALIGNMENT flags here because doubles and pointers have
3099 // the same alignment on ARM64.
3100 STATIC_ASSERT(kPointerAlignment == kDoubleAlignment);
3101
3102 // Calculate new top and bail out if new space is exhausted.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003103 Adds(result_end, result, object_size);
3104 Ccmp(result_end, alloc_limit, CFlag, cc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003105 B(hi, gc_required);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003106 Str(result_end, MemOperand(top_address));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003107
3108 // Tag the object if requested.
3109 if ((flags & TAG_OBJECT) != 0) {
3110 ObjectTag(result, result);
3111 }
3112}
3113
3114
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003115void MacroAssembler::Allocate(Register object_size, Register result,
3116 Register result_end, Register scratch,
3117 Label* gc_required, AllocationFlags flags) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003118 if (!FLAG_inline_new) {
3119 if (emit_debug_code()) {
3120 // Trash the registers to simulate an allocation failure.
3121 // We apply salt to the original zap value to easily spot the values.
3122 Mov(result, (kDebugZapValue & ~0xffL) | 0x11L);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003123 Mov(scratch, (kDebugZapValue & ~0xffL) | 0x21L);
3124 Mov(result_end, (kDebugZapValue & ~0xffL) | 0x21L);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003125 }
3126 B(gc_required);
3127 return;
3128 }
3129
3130 UseScratchRegisterScope temps(this);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003131 Register scratch2 = temps.AcquireX();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003132
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003133 // |object_size| and |result_end| may overlap, other registers must not.
3134 DCHECK(!AreAliased(object_size, result, scratch, scratch2));
3135 DCHECK(!AreAliased(result_end, result, scratch, scratch2));
3136 DCHECK(object_size.Is64Bits() && result.Is64Bits() && scratch.Is64Bits() &&
3137 result_end.Is64Bits());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003138
3139 // Check relative positions of allocation top and limit addresses.
3140 // The values must be adjacent in memory to allow the use of LDP.
3141 ExternalReference heap_allocation_top =
3142 AllocationUtils::GetAllocationTopReference(isolate(), flags);
3143 ExternalReference heap_allocation_limit =
3144 AllocationUtils::GetAllocationLimitReference(isolate(), flags);
3145 intptr_t top = reinterpret_cast<intptr_t>(heap_allocation_top.address());
3146 intptr_t limit = reinterpret_cast<intptr_t>(heap_allocation_limit.address());
3147 DCHECK((limit - top) == kPointerSize);
3148
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003149 // Set up allocation top address and allocation limit registers.
3150 Register top_address = scratch;
3151 Register alloc_limit = scratch2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003152 Mov(top_address, heap_allocation_top);
3153
3154 if ((flags & RESULT_CONTAINS_TOP) == 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003155 // Load allocation top into result and allocation limit into alloc_limit.
3156 Ldp(result, alloc_limit, MemOperand(top_address));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003157 } else {
3158 if (emit_debug_code()) {
3159 // Assert that result actually contains top on entry.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003160 Ldr(alloc_limit, MemOperand(top_address));
3161 Cmp(result, alloc_limit);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003162 Check(eq, kUnexpectedAllocationTop);
3163 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003164 // Load allocation limit. Result already contains allocation top.
3165 Ldr(alloc_limit, MemOperand(top_address, limit - top));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003166 }
3167
3168 // We can ignore DOUBLE_ALIGNMENT flags here because doubles and pointers have
3169 // the same alignment on ARM64.
3170 STATIC_ASSERT(kPointerAlignment == kDoubleAlignment);
3171
3172 // Calculate new top and bail out if new space is exhausted
3173 if ((flags & SIZE_IN_WORDS) != 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003174 Adds(result_end, result, Operand(object_size, LSL, kPointerSizeLog2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003175 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003176 Adds(result_end, result, object_size);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003177 }
3178
3179 if (emit_debug_code()) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003180 Tst(result_end, kObjectAlignmentMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003181 Check(eq, kUnalignedAllocationInNewSpace);
3182 }
3183
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003184 Ccmp(result_end, alloc_limit, CFlag, cc);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003185 B(hi, gc_required);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003186 Str(result_end, MemOperand(top_address));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003187
3188 // Tag the object if requested.
3189 if ((flags & TAG_OBJECT) != 0) {
3190 ObjectTag(result, result);
3191 }
3192}
3193
3194
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003195void MacroAssembler::AllocateTwoByteString(Register result,
3196 Register length,
3197 Register scratch1,
3198 Register scratch2,
3199 Register scratch3,
3200 Label* gc_required) {
3201 DCHECK(!AreAliased(result, length, scratch1, scratch2, scratch3));
3202 // Calculate the number of bytes needed for the characters in the string while
3203 // observing object alignment.
3204 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3205 Add(scratch1, length, length); // Length in bytes, not chars.
3206 Add(scratch1, scratch1, kObjectAlignmentMask + SeqTwoByteString::kHeaderSize);
3207 Bic(scratch1, scratch1, kObjectAlignmentMask);
3208
3209 // Allocate two-byte string in new space.
3210 Allocate(scratch1,
3211 result,
3212 scratch2,
3213 scratch3,
3214 gc_required,
3215 TAG_OBJECT);
3216
3217 // Set the map, length and hash field.
3218 InitializeNewString(result,
3219 length,
3220 Heap::kStringMapRootIndex,
3221 scratch1,
3222 scratch2);
3223}
3224
3225
3226void MacroAssembler::AllocateOneByteString(Register result, Register length,
3227 Register scratch1, Register scratch2,
3228 Register scratch3,
3229 Label* gc_required) {
3230 DCHECK(!AreAliased(result, length, scratch1, scratch2, scratch3));
3231 // Calculate the number of bytes needed for the characters in the string while
3232 // observing object alignment.
3233 STATIC_ASSERT((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3234 STATIC_ASSERT(kCharSize == 1);
3235 Add(scratch1, length, kObjectAlignmentMask + SeqOneByteString::kHeaderSize);
3236 Bic(scratch1, scratch1, kObjectAlignmentMask);
3237
3238 // Allocate one-byte string in new space.
3239 Allocate(scratch1,
3240 result,
3241 scratch2,
3242 scratch3,
3243 gc_required,
3244 TAG_OBJECT);
3245
3246 // Set the map, length and hash field.
3247 InitializeNewString(result, length, Heap::kOneByteStringMapRootIndex,
3248 scratch1, scratch2);
3249}
3250
3251
3252void MacroAssembler::AllocateTwoByteConsString(Register result,
3253 Register length,
3254 Register scratch1,
3255 Register scratch2,
3256 Label* gc_required) {
3257 Allocate(ConsString::kSize, result, scratch1, scratch2, gc_required,
3258 TAG_OBJECT);
3259
3260 InitializeNewString(result,
3261 length,
3262 Heap::kConsStringMapRootIndex,
3263 scratch1,
3264 scratch2);
3265}
3266
3267
3268void MacroAssembler::AllocateOneByteConsString(Register result, Register length,
3269 Register scratch1,
3270 Register scratch2,
3271 Label* gc_required) {
3272 Allocate(ConsString::kSize,
3273 result,
3274 scratch1,
3275 scratch2,
3276 gc_required,
3277 TAG_OBJECT);
3278
3279 InitializeNewString(result, length, Heap::kConsOneByteStringMapRootIndex,
3280 scratch1, scratch2);
3281}
3282
3283
3284void MacroAssembler::AllocateTwoByteSlicedString(Register result,
3285 Register length,
3286 Register scratch1,
3287 Register scratch2,
3288 Label* gc_required) {
3289 DCHECK(!AreAliased(result, length, scratch1, scratch2));
3290 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
3291 TAG_OBJECT);
3292
3293 InitializeNewString(result,
3294 length,
3295 Heap::kSlicedStringMapRootIndex,
3296 scratch1,
3297 scratch2);
3298}
3299
3300
3301void MacroAssembler::AllocateOneByteSlicedString(Register result,
3302 Register length,
3303 Register scratch1,
3304 Register scratch2,
3305 Label* gc_required) {
3306 DCHECK(!AreAliased(result, length, scratch1, scratch2));
3307 Allocate(SlicedString::kSize, result, scratch1, scratch2, gc_required,
3308 TAG_OBJECT);
3309
3310 InitializeNewString(result, length, Heap::kSlicedOneByteStringMapRootIndex,
3311 scratch1, scratch2);
3312}
3313
3314
3315// Allocates a heap number or jumps to the need_gc label if the young space
3316// is full and a scavenge is needed.
3317void MacroAssembler::AllocateHeapNumber(Register result,
3318 Label* gc_required,
3319 Register scratch1,
3320 Register scratch2,
3321 CPURegister value,
3322 CPURegister heap_number_map,
3323 MutableMode mode) {
3324 DCHECK(!value.IsValid() || value.Is64Bits());
3325 UseScratchRegisterScope temps(this);
3326
3327 // Allocate an object in the heap for the heap number and tag it as a heap
3328 // object.
3329 Allocate(HeapNumber::kSize, result, scratch1, scratch2, gc_required,
3330 NO_ALLOCATION_FLAGS);
3331
3332 Heap::RootListIndex map_index = mode == MUTABLE
3333 ? Heap::kMutableHeapNumberMapRootIndex
3334 : Heap::kHeapNumberMapRootIndex;
3335
3336 // Prepare the heap number map.
3337 if (!heap_number_map.IsValid()) {
3338 // If we have a valid value register, use the same type of register to store
3339 // the map so we can use STP to store both in one instruction.
3340 if (value.IsValid() && value.IsFPRegister()) {
3341 heap_number_map = temps.AcquireD();
3342 } else {
3343 heap_number_map = scratch1;
3344 }
3345 LoadRoot(heap_number_map, map_index);
3346 }
3347 if (emit_debug_code()) {
3348 Register map;
3349 if (heap_number_map.IsFPRegister()) {
3350 map = scratch1;
3351 Fmov(map, DoubleRegister(heap_number_map));
3352 } else {
3353 map = Register(heap_number_map);
3354 }
3355 AssertRegisterIsRoot(map, map_index);
3356 }
3357
3358 // Store the heap number map and the value in the allocated object.
3359 if (value.IsSameSizeAndType(heap_number_map)) {
3360 STATIC_ASSERT(HeapObject::kMapOffset + kPointerSize ==
3361 HeapNumber::kValueOffset);
3362 Stp(heap_number_map, value, MemOperand(result, HeapObject::kMapOffset));
3363 } else {
3364 Str(heap_number_map, MemOperand(result, HeapObject::kMapOffset));
3365 if (value.IsValid()) {
3366 Str(value, MemOperand(result, HeapNumber::kValueOffset));
3367 }
3368 }
3369 ObjectTag(result, result);
3370}
3371
3372
3373void MacroAssembler::JumpIfObjectType(Register object,
3374 Register map,
3375 Register type_reg,
3376 InstanceType type,
3377 Label* if_cond_pass,
3378 Condition cond) {
3379 CompareObjectType(object, map, type_reg, type);
3380 B(cond, if_cond_pass);
3381}
3382
3383
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003384void MacroAssembler::AllocateJSValue(Register result, Register constructor,
3385 Register value, Register scratch1,
3386 Register scratch2, Label* gc_required) {
3387 DCHECK(!result.is(constructor));
3388 DCHECK(!result.is(scratch1));
3389 DCHECK(!result.is(scratch2));
3390 DCHECK(!result.is(value));
3391
3392 // Allocate JSValue in new space.
3393 Allocate(JSValue::kSize, result, scratch1, scratch2, gc_required, TAG_OBJECT);
3394
3395 // Initialize the JSValue.
3396 LoadGlobalFunctionInitialMap(constructor, scratch1, scratch2);
3397 Str(scratch1, FieldMemOperand(result, HeapObject::kMapOffset));
3398 LoadRoot(scratch1, Heap::kEmptyFixedArrayRootIndex);
3399 Str(scratch1, FieldMemOperand(result, JSObject::kPropertiesOffset));
3400 Str(scratch1, FieldMemOperand(result, JSObject::kElementsOffset));
3401 Str(value, FieldMemOperand(result, JSValue::kValueOffset));
3402 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
3403}
3404
3405
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003406void MacroAssembler::JumpIfNotObjectType(Register object,
3407 Register map,
3408 Register type_reg,
3409 InstanceType type,
3410 Label* if_not_object) {
3411 JumpIfObjectType(object, map, type_reg, type, if_not_object, ne);
3412}
3413
3414
3415// Sets condition flags based on comparison, and returns type in type_reg.
3416void MacroAssembler::CompareObjectType(Register object,
3417 Register map,
3418 Register type_reg,
3419 InstanceType type) {
3420 Ldr(map, FieldMemOperand(object, HeapObject::kMapOffset));
3421 CompareInstanceType(map, type_reg, type);
3422}
3423
3424
3425// Sets condition flags based on comparison, and returns type in type_reg.
3426void MacroAssembler::CompareInstanceType(Register map,
3427 Register type_reg,
3428 InstanceType type) {
3429 Ldrb(type_reg, FieldMemOperand(map, Map::kInstanceTypeOffset));
3430 Cmp(type_reg, type);
3431}
3432
3433
3434void MacroAssembler::CompareObjectMap(Register obj, Heap::RootListIndex index) {
3435 UseScratchRegisterScope temps(this);
3436 Register obj_map = temps.AcquireX();
3437 Ldr(obj_map, FieldMemOperand(obj, HeapObject::kMapOffset));
3438 CompareRoot(obj_map, index);
3439}
3440
3441
3442void MacroAssembler::CompareObjectMap(Register obj, Register scratch,
3443 Handle<Map> map) {
3444 Ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
3445 CompareMap(scratch, map);
3446}
3447
3448
3449void MacroAssembler::CompareMap(Register obj_map,
3450 Handle<Map> map) {
3451 Cmp(obj_map, Operand(map));
3452}
3453
3454
3455void MacroAssembler::CheckMap(Register obj,
3456 Register scratch,
3457 Handle<Map> map,
3458 Label* fail,
3459 SmiCheckType smi_check_type) {
3460 if (smi_check_type == DO_SMI_CHECK) {
3461 JumpIfSmi(obj, fail);
3462 }
3463
3464 CompareObjectMap(obj, scratch, map);
3465 B(ne, fail);
3466}
3467
3468
3469void MacroAssembler::CheckMap(Register obj,
3470 Register scratch,
3471 Heap::RootListIndex index,
3472 Label* fail,
3473 SmiCheckType smi_check_type) {
3474 if (smi_check_type == DO_SMI_CHECK) {
3475 JumpIfSmi(obj, fail);
3476 }
3477 Ldr(scratch, FieldMemOperand(obj, HeapObject::kMapOffset));
3478 JumpIfNotRoot(scratch, index, fail);
3479}
3480
3481
3482void MacroAssembler::CheckMap(Register obj_map,
3483 Handle<Map> map,
3484 Label* fail,
3485 SmiCheckType smi_check_type) {
3486 if (smi_check_type == DO_SMI_CHECK) {
3487 JumpIfSmi(obj_map, fail);
3488 }
3489
3490 CompareMap(obj_map, map);
3491 B(ne, fail);
3492}
3493
3494
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003495void MacroAssembler::DispatchWeakMap(Register obj, Register scratch1,
3496 Register scratch2, Handle<WeakCell> cell,
3497 Handle<Code> success,
3498 SmiCheckType smi_check_type) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003499 Label fail;
3500 if (smi_check_type == DO_SMI_CHECK) {
3501 JumpIfSmi(obj, &fail);
3502 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003503 Ldr(scratch1, FieldMemOperand(obj, HeapObject::kMapOffset));
3504 CmpWeakValue(scratch1, cell, scratch2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003505 B(ne, &fail);
3506 Jump(success, RelocInfo::CODE_TARGET);
3507 Bind(&fail);
3508}
3509
3510
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003511void MacroAssembler::CmpWeakValue(Register value, Handle<WeakCell> cell,
3512 Register scratch) {
3513 Mov(scratch, Operand(cell));
3514 Ldr(scratch, FieldMemOperand(scratch, WeakCell::kValueOffset));
3515 Cmp(value, scratch);
3516}
3517
3518
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003519void MacroAssembler::GetWeakValue(Register value, Handle<WeakCell> cell) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003520 Mov(value, Operand(cell));
3521 Ldr(value, FieldMemOperand(value, WeakCell::kValueOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003522}
3523
3524
3525void MacroAssembler::LoadWeakValue(Register value, Handle<WeakCell> cell,
3526 Label* miss) {
3527 GetWeakValue(value, cell);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003528 JumpIfSmi(value, miss);
3529}
3530
3531
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003532void MacroAssembler::TestMapBitfield(Register object, uint64_t mask) {
3533 UseScratchRegisterScope temps(this);
3534 Register temp = temps.AcquireX();
3535 Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
3536 Ldrb(temp, FieldMemOperand(temp, Map::kBitFieldOffset));
3537 Tst(temp, mask);
3538}
3539
3540
3541void MacroAssembler::LoadElementsKindFromMap(Register result, Register map) {
3542 // Load the map's "bit field 2".
3543 __ Ldrb(result, FieldMemOperand(map, Map::kBitField2Offset));
3544 // Retrieve elements_kind from bit field 2.
3545 DecodeField<Map::ElementsKindBits>(result);
3546}
3547
3548
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003549void MacroAssembler::GetMapConstructor(Register result, Register map,
3550 Register temp, Register temp2) {
3551 Label done, loop;
3552 Ldr(result, FieldMemOperand(map, Map::kConstructorOrBackPointerOffset));
3553 Bind(&loop);
3554 JumpIfSmi(result, &done);
3555 CompareObjectType(result, temp, temp2, MAP_TYPE);
3556 B(ne, &done);
3557 Ldr(result, FieldMemOperand(result, Map::kConstructorOrBackPointerOffset));
3558 B(&loop);
3559 Bind(&done);
3560}
3561
3562
3563void MacroAssembler::TryGetFunctionPrototype(Register function, Register result,
3564 Register scratch, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003565 DCHECK(!AreAliased(function, result, scratch));
3566
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003567 // Get the prototype or initial map from the function.
3568 Ldr(result,
3569 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
3570
3571 // If the prototype or initial map is the hole, don't return it and simply
3572 // miss the cache instead. This will allow us to allocate a prototype object
3573 // on-demand in the runtime system.
3574 JumpIfRoot(result, Heap::kTheHoleValueRootIndex, miss);
3575
3576 // If the function does not have an initial map, we're done.
3577 Label done;
3578 JumpIfNotObjectType(result, scratch, scratch, MAP_TYPE, &done);
3579
3580 // Get the prototype from the initial map.
3581 Ldr(result, FieldMemOperand(result, Map::kPrototypeOffset));
3582
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003583 // All done.
3584 Bind(&done);
3585}
3586
3587
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003588void MacroAssembler::PushRoot(Heap::RootListIndex index) {
3589 UseScratchRegisterScope temps(this);
3590 Register temp = temps.AcquireX();
3591 LoadRoot(temp, index);
3592 Push(temp);
3593}
3594
3595
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003596void MacroAssembler::CompareRoot(const Register& obj,
3597 Heap::RootListIndex index) {
3598 UseScratchRegisterScope temps(this);
3599 Register temp = temps.AcquireX();
3600 DCHECK(!AreAliased(obj, temp));
3601 LoadRoot(temp, index);
3602 Cmp(obj, temp);
3603}
3604
3605
3606void MacroAssembler::JumpIfRoot(const Register& obj,
3607 Heap::RootListIndex index,
3608 Label* if_equal) {
3609 CompareRoot(obj, index);
3610 B(eq, if_equal);
3611}
3612
3613
3614void MacroAssembler::JumpIfNotRoot(const Register& obj,
3615 Heap::RootListIndex index,
3616 Label* if_not_equal) {
3617 CompareRoot(obj, index);
3618 B(ne, if_not_equal);
3619}
3620
3621
3622void MacroAssembler::CompareAndSplit(const Register& lhs,
3623 const Operand& rhs,
3624 Condition cond,
3625 Label* if_true,
3626 Label* if_false,
3627 Label* fall_through) {
3628 if ((if_true == if_false) && (if_false == fall_through)) {
3629 // Fall through.
3630 } else if (if_true == if_false) {
3631 B(if_true);
3632 } else if (if_false == fall_through) {
3633 CompareAndBranch(lhs, rhs, cond, if_true);
3634 } else if (if_true == fall_through) {
3635 CompareAndBranch(lhs, rhs, NegateCondition(cond), if_false);
3636 } else {
3637 CompareAndBranch(lhs, rhs, cond, if_true);
3638 B(if_false);
3639 }
3640}
3641
3642
3643void MacroAssembler::TestAndSplit(const Register& reg,
3644 uint64_t bit_pattern,
3645 Label* if_all_clear,
3646 Label* if_any_set,
3647 Label* fall_through) {
3648 if ((if_all_clear == if_any_set) && (if_any_set == fall_through)) {
3649 // Fall through.
3650 } else if (if_all_clear == if_any_set) {
3651 B(if_all_clear);
3652 } else if (if_all_clear == fall_through) {
3653 TestAndBranchIfAnySet(reg, bit_pattern, if_any_set);
3654 } else if (if_any_set == fall_through) {
3655 TestAndBranchIfAllClear(reg, bit_pattern, if_all_clear);
3656 } else {
3657 TestAndBranchIfAnySet(reg, bit_pattern, if_any_set);
3658 B(if_all_clear);
3659 }
3660}
3661
3662
3663void MacroAssembler::CheckFastElements(Register map,
3664 Register scratch,
3665 Label* fail) {
3666 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3667 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3668 STATIC_ASSERT(FAST_ELEMENTS == 2);
3669 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3670 Ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
3671 Cmp(scratch, Map::kMaximumBitField2FastHoleyElementValue);
3672 B(hi, fail);
3673}
3674
3675
3676void MacroAssembler::CheckFastObjectElements(Register map,
3677 Register scratch,
3678 Label* fail) {
3679 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3680 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3681 STATIC_ASSERT(FAST_ELEMENTS == 2);
3682 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3683 Ldrb(scratch, FieldMemOperand(map, Map::kBitField2Offset));
3684 Cmp(scratch, Operand(Map::kMaximumBitField2FastHoleySmiElementValue));
3685 // If cond==ls, set cond=hi, otherwise compare.
3686 Ccmp(scratch,
3687 Operand(Map::kMaximumBitField2FastHoleyElementValue), CFlag, hi);
3688 B(hi, fail);
3689}
3690
3691
3692// Note: The ARM version of this clobbers elements_reg, but this version does
3693// not. Some uses of this in ARM64 assume that elements_reg will be preserved.
3694void MacroAssembler::StoreNumberToDoubleElements(Register value_reg,
3695 Register key_reg,
3696 Register elements_reg,
3697 Register scratch1,
3698 FPRegister fpscratch1,
3699 Label* fail,
3700 int elements_offset) {
3701 DCHECK(!AreAliased(value_reg, key_reg, elements_reg, scratch1));
3702 Label store_num;
3703
3704 // Speculatively convert the smi to a double - all smis can be exactly
3705 // represented as a double.
3706 SmiUntagToDouble(fpscratch1, value_reg, kSpeculativeUntag);
3707
3708 // If value_reg is a smi, we're done.
3709 JumpIfSmi(value_reg, &store_num);
3710
3711 // Ensure that the object is a heap number.
3712 JumpIfNotHeapNumber(value_reg, fail);
3713
3714 Ldr(fpscratch1, FieldMemOperand(value_reg, HeapNumber::kValueOffset));
3715
3716 // Canonicalize NaNs.
3717 CanonicalizeNaN(fpscratch1);
3718
3719 // Store the result.
3720 Bind(&store_num);
3721 Add(scratch1, elements_reg,
3722 Operand::UntagSmiAndScale(key_reg, kDoubleSizeLog2));
3723 Str(fpscratch1,
3724 FieldMemOperand(scratch1,
3725 FixedDoubleArray::kHeaderSize - elements_offset));
3726}
3727
3728
3729bool MacroAssembler::AllowThisStubCall(CodeStub* stub) {
3730 return has_frame_ || !stub->SometimesSetsUpAFrame();
3731}
3732
3733
3734void MacroAssembler::IndexFromHash(Register hash, Register index) {
3735 // If the hash field contains an array index pick it out. The assert checks
3736 // that the constants for the maximum number of digits for an array index
3737 // cached in the hash field and the number of bits reserved for it does not
3738 // conflict.
3739 DCHECK(TenToThe(String::kMaxCachedArrayIndexLength) <
3740 (1 << String::kArrayIndexValueBits));
3741 DecodeField<String::ArrayIndexValueBits>(index, hash);
3742 SmiTag(index, index);
3743}
3744
3745
3746void MacroAssembler::EmitSeqStringSetCharCheck(
3747 Register string,
3748 Register index,
3749 SeqStringSetCharCheckIndexType index_type,
3750 Register scratch,
3751 uint32_t encoding_mask) {
3752 DCHECK(!AreAliased(string, index, scratch));
3753
3754 if (index_type == kIndexIsSmi) {
3755 AssertSmi(index);
3756 }
3757
3758 // Check that string is an object.
3759 AssertNotSmi(string, kNonObject);
3760
3761 // Check that string has an appropriate map.
3762 Ldr(scratch, FieldMemOperand(string, HeapObject::kMapOffset));
3763 Ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
3764
3765 And(scratch, scratch, kStringRepresentationMask | kStringEncodingMask);
3766 Cmp(scratch, encoding_mask);
3767 Check(eq, kUnexpectedStringType);
3768
3769 Ldr(scratch, FieldMemOperand(string, String::kLengthOffset));
3770 Cmp(index, index_type == kIndexIsSmi ? scratch : Operand::UntagSmi(scratch));
3771 Check(lt, kIndexIsTooLarge);
3772
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003773 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003774 Cmp(index, 0);
3775 Check(ge, kIndexIsNegative);
3776}
3777
3778
3779void MacroAssembler::CheckAccessGlobalProxy(Register holder_reg,
3780 Register scratch1,
3781 Register scratch2,
3782 Label* miss) {
3783 DCHECK(!AreAliased(holder_reg, scratch1, scratch2));
3784 Label same_contexts;
3785
Ben Murdochda12d292016-06-02 14:46:10 +01003786 // Load current lexical context from the active StandardFrame, which
3787 // may require crawling past STUB frames.
3788 Label load_context;
3789 Label has_context;
3790 Mov(scratch2, fp);
3791 bind(&load_context);
3792 Ldr(scratch1,
3793 MemOperand(scratch2, CommonFrameConstants::kContextOrFrameTypeOffset));
3794 JumpIfNotSmi(scratch1, &has_context);
3795 Ldr(scratch2, MemOperand(scratch2, CommonFrameConstants::kCallerFPOffset));
3796 B(&load_context);
3797 bind(&has_context);
3798
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003799 // In debug mode, make sure the lexical context is set.
3800#ifdef DEBUG
3801 Cmp(scratch1, 0);
3802 Check(ne, kWeShouldNotHaveAnEmptyLexicalContext);
3803#endif
3804
3805 // Load the native context of the current context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003806 Ldr(scratch1, ContextMemOperand(scratch1, Context::NATIVE_CONTEXT_INDEX));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003807
3808 // Check the context is a native context.
3809 if (emit_debug_code()) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003810 // Read the first word and compare to the native_context_map.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003811 Ldr(scratch2, FieldMemOperand(scratch1, HeapObject::kMapOffset));
3812 CompareRoot(scratch2, Heap::kNativeContextMapRootIndex);
3813 Check(eq, kExpectedNativeContext);
3814 }
3815
3816 // Check if both contexts are the same.
3817 Ldr(scratch2, FieldMemOperand(holder_reg,
3818 JSGlobalProxy::kNativeContextOffset));
3819 Cmp(scratch1, scratch2);
3820 B(&same_contexts, eq);
3821
3822 // Check the context is a native context.
3823 if (emit_debug_code()) {
3824 // We're short on scratch registers here, so use holder_reg as a scratch.
3825 Push(holder_reg);
3826 Register scratch3 = holder_reg;
3827
3828 CompareRoot(scratch2, Heap::kNullValueRootIndex);
3829 Check(ne, kExpectedNonNullContext);
3830
3831 Ldr(scratch3, FieldMemOperand(scratch2, HeapObject::kMapOffset));
3832 CompareRoot(scratch3, Heap::kNativeContextMapRootIndex);
3833 Check(eq, kExpectedNativeContext);
3834 Pop(holder_reg);
3835 }
3836
3837 // Check that the security token in the calling global object is
3838 // compatible with the security token in the receiving global
3839 // object.
3840 int token_offset = Context::kHeaderSize +
3841 Context::SECURITY_TOKEN_INDEX * kPointerSize;
3842
3843 Ldr(scratch1, FieldMemOperand(scratch1, token_offset));
3844 Ldr(scratch2, FieldMemOperand(scratch2, token_offset));
3845 Cmp(scratch1, scratch2);
3846 B(miss, ne);
3847
3848 Bind(&same_contexts);
3849}
3850
3851
3852// Compute the hash code from the untagged key. This must be kept in sync with
3853// ComputeIntegerHash in utils.h and KeyedLoadGenericStub in
3854// code-stub-hydrogen.cc
3855void MacroAssembler::GetNumberHash(Register key, Register scratch) {
3856 DCHECK(!AreAliased(key, scratch));
3857
3858 // Xor original key with a seed.
3859 LoadRoot(scratch, Heap::kHashSeedRootIndex);
3860 Eor(key, key, Operand::UntagSmi(scratch));
3861
3862 // The algorithm uses 32-bit integer values.
3863 key = key.W();
3864 scratch = scratch.W();
3865
3866 // Compute the hash code from the untagged key. This must be kept in sync
3867 // with ComputeIntegerHash in utils.h.
3868 //
3869 // hash = ~hash + (hash <<1 15);
3870 Mvn(scratch, key);
3871 Add(key, scratch, Operand(key, LSL, 15));
3872 // hash = hash ^ (hash >> 12);
3873 Eor(key, key, Operand(key, LSR, 12));
3874 // hash = hash + (hash << 2);
3875 Add(key, key, Operand(key, LSL, 2));
3876 // hash = hash ^ (hash >> 4);
3877 Eor(key, key, Operand(key, LSR, 4));
3878 // hash = hash * 2057;
3879 Mov(scratch, Operand(key, LSL, 11));
3880 Add(key, key, Operand(key, LSL, 3));
3881 Add(key, key, scratch);
3882 // hash = hash ^ (hash >> 16);
3883 Eor(key, key, Operand(key, LSR, 16));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003884 Bic(key, key, Operand(0xc0000000u));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003885}
3886
3887
3888void MacroAssembler::LoadFromNumberDictionary(Label* miss,
3889 Register elements,
3890 Register key,
3891 Register result,
3892 Register scratch0,
3893 Register scratch1,
3894 Register scratch2,
3895 Register scratch3) {
3896 DCHECK(!AreAliased(elements, key, scratch0, scratch1, scratch2, scratch3));
3897
3898 Label done;
3899
3900 SmiUntag(scratch0, key);
3901 GetNumberHash(scratch0, scratch1);
3902
3903 // Compute the capacity mask.
3904 Ldrsw(scratch1,
3905 UntagSmiFieldMemOperand(elements,
3906 SeededNumberDictionary::kCapacityOffset));
3907 Sub(scratch1, scratch1, 1);
3908
3909 // Generate an unrolled loop that performs a few probes before giving up.
3910 for (int i = 0; i < kNumberDictionaryProbes; i++) {
3911 // Compute the masked index: (hash + i + i * i) & mask.
3912 if (i > 0) {
3913 Add(scratch2, scratch0, SeededNumberDictionary::GetProbeOffset(i));
3914 } else {
3915 Mov(scratch2, scratch0);
3916 }
3917 And(scratch2, scratch2, scratch1);
3918
3919 // Scale the index by multiplying by the element size.
3920 DCHECK(SeededNumberDictionary::kEntrySize == 3);
3921 Add(scratch2, scratch2, Operand(scratch2, LSL, 1));
3922
3923 // Check if the key is identical to the name.
3924 Add(scratch2, elements, Operand(scratch2, LSL, kPointerSizeLog2));
3925 Ldr(scratch3,
3926 FieldMemOperand(scratch2,
3927 SeededNumberDictionary::kElementsStartOffset));
3928 Cmp(key, scratch3);
3929 if (i != (kNumberDictionaryProbes - 1)) {
3930 B(eq, &done);
3931 } else {
3932 B(ne, miss);
3933 }
3934 }
3935
3936 Bind(&done);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003937 // Check that the value is a field property.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003938 const int kDetailsOffset =
3939 SeededNumberDictionary::kElementsStartOffset + 2 * kPointerSize;
3940 Ldrsw(scratch1, UntagSmiFieldMemOperand(scratch2, kDetailsOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003941 DCHECK_EQ(DATA, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003942 TestAndBranchIfAnySet(scratch1, PropertyDetails::TypeField::kMask, miss);
3943
3944 // Get the value at the masked, scaled index and return.
3945 const int kValueOffset =
3946 SeededNumberDictionary::kElementsStartOffset + kPointerSize;
3947 Ldr(result, FieldMemOperand(scratch2, kValueOffset));
3948}
3949
Ben Murdoch097c5b22016-05-18 11:27:45 +01003950void MacroAssembler::RecordWriteCodeEntryField(Register js_function,
3951 Register code_entry,
3952 Register scratch) {
3953 const int offset = JSFunction::kCodeEntryOffset;
3954
3955 // Since a code entry (value) is always in old space, we don't need to update
3956 // remembered set. If incremental marking is off, there is nothing for us to
3957 // do.
3958 if (!FLAG_incremental_marking) return;
3959
3960 DCHECK(js_function.is(x1));
3961 DCHECK(code_entry.is(x7));
3962 DCHECK(scratch.is(x5));
3963 AssertNotSmi(js_function);
3964
3965 if (emit_debug_code()) {
3966 UseScratchRegisterScope temps(this);
3967 Register temp = temps.AcquireX();
3968 Add(scratch, js_function, offset - kHeapObjectTag);
3969 Ldr(temp, MemOperand(scratch));
3970 Cmp(temp, code_entry);
3971 Check(eq, kWrongAddressOrValuePassedToRecordWrite);
3972 }
3973
3974 // First, check if a write barrier is even needed. The tests below
3975 // catch stores of Smis and stores into young gen.
3976 Label done;
3977
3978 CheckPageFlagClear(code_entry, scratch,
3979 MemoryChunk::kPointersToHereAreInterestingMask, &done);
3980 CheckPageFlagClear(js_function, scratch,
3981 MemoryChunk::kPointersFromHereAreInterestingMask, &done);
3982
3983 const Register dst = scratch;
3984 Add(dst, js_function, offset - kHeapObjectTag);
3985
3986 // Save caller-saved registers.Both input registers (x1 and x7) are caller
3987 // saved, so there is no need to push them.
3988 PushCPURegList(kCallerSaved);
3989
3990 int argument_count = 3;
3991
3992 Mov(x0, js_function);
3993 Mov(x1, dst);
3994 Mov(x2, ExternalReference::isolate_address(isolate()));
3995
3996 {
3997 AllowExternalCallThatCantCauseGC scope(this);
3998 CallCFunction(
3999 ExternalReference::incremental_marking_record_write_code_entry_function(
4000 isolate()),
4001 argument_count);
4002 }
4003
4004 // Restore caller-saved registers.
4005 PopCPURegList(kCallerSaved);
4006
4007 Bind(&done);
4008}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004009
4010void MacroAssembler::RememberedSetHelper(Register object, // For debug tests.
4011 Register address,
4012 Register scratch1,
4013 SaveFPRegsMode fp_mode,
4014 RememberedSetFinalAction and_then) {
4015 DCHECK(!AreAliased(object, address, scratch1));
4016 Label done, store_buffer_overflow;
4017 if (emit_debug_code()) {
4018 Label ok;
4019 JumpIfNotInNewSpace(object, &ok);
4020 Abort(kRememberedSetPointerInNewSpace);
4021 bind(&ok);
4022 }
4023 UseScratchRegisterScope temps(this);
4024 Register scratch2 = temps.AcquireX();
4025
4026 // Load store buffer top.
4027 Mov(scratch2, ExternalReference::store_buffer_top(isolate()));
4028 Ldr(scratch1, MemOperand(scratch2));
4029 // Store pointer to buffer and increment buffer top.
4030 Str(address, MemOperand(scratch1, kPointerSize, PostIndex));
4031 // Write back new top of buffer.
4032 Str(scratch1, MemOperand(scratch2));
4033 // Call stub on end of buffer.
4034 // Check for end of buffer.
Ben Murdochda12d292016-06-02 14:46:10 +01004035 Tst(scratch1, StoreBuffer::kStoreBufferMask);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004036 if (and_then == kFallThroughAtEnd) {
Ben Murdochda12d292016-06-02 14:46:10 +01004037 B(ne, &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004038 } else {
4039 DCHECK(and_then == kReturnAtEnd);
Ben Murdochda12d292016-06-02 14:46:10 +01004040 B(eq, &store_buffer_overflow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004041 Ret();
4042 }
4043
4044 Bind(&store_buffer_overflow);
4045 Push(lr);
4046 StoreBufferOverflowStub store_buffer_overflow_stub(isolate(), fp_mode);
4047 CallStub(&store_buffer_overflow_stub);
4048 Pop(lr);
4049
4050 Bind(&done);
4051 if (and_then == kReturnAtEnd) {
4052 Ret();
4053 }
4054}
4055
4056
4057void MacroAssembler::PopSafepointRegisters() {
4058 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
4059 PopXRegList(kSafepointSavedRegisters);
4060 Drop(num_unsaved);
4061}
4062
4063
4064void MacroAssembler::PushSafepointRegisters() {
4065 // Safepoints expect a block of kNumSafepointRegisters values on the stack, so
4066 // adjust the stack for unsaved registers.
4067 const int num_unsaved = kNumSafepointRegisters - kNumSafepointSavedRegisters;
4068 DCHECK(num_unsaved >= 0);
4069 Claim(num_unsaved);
4070 PushXRegList(kSafepointSavedRegisters);
4071}
4072
4073
4074void MacroAssembler::PushSafepointRegistersAndDoubles() {
4075 PushSafepointRegisters();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004076 PushCPURegList(CPURegList(
4077 CPURegister::kFPRegister, kDRegSizeInBits,
4078 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
4079 ->allocatable_double_codes_mask()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004080}
4081
4082
4083void MacroAssembler::PopSafepointRegistersAndDoubles() {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004084 PopCPURegList(CPURegList(
4085 CPURegister::kFPRegister, kDRegSizeInBits,
4086 RegisterConfiguration::ArchDefault(RegisterConfiguration::CRANKSHAFT)
4087 ->allocatable_double_codes_mask()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004088 PopSafepointRegisters();
4089}
4090
4091
4092int MacroAssembler::SafepointRegisterStackIndex(int reg_code) {
4093 // Make sure the safepoint registers list is what we expect.
4094 DCHECK(CPURegList::GetSafepointSavedRegisters().list() == 0x6ffcffff);
4095
4096 // Safepoint registers are stored contiguously on the stack, but not all the
4097 // registers are saved. The following registers are excluded:
4098 // - x16 and x17 (ip0 and ip1) because they shouldn't be preserved outside of
4099 // the macro assembler.
4100 // - x28 (jssp) because JS stack pointer doesn't need to be included in
4101 // safepoint registers.
4102 // - x31 (csp) because the system stack pointer doesn't need to be included
4103 // in safepoint registers.
4104 //
4105 // This function implements the mapping of register code to index into the
4106 // safepoint register slots.
4107 if ((reg_code >= 0) && (reg_code <= 15)) {
4108 return reg_code;
4109 } else if ((reg_code >= 18) && (reg_code <= 27)) {
4110 // Skip ip0 and ip1.
4111 return reg_code - 2;
4112 } else if ((reg_code == 29) || (reg_code == 30)) {
4113 // Also skip jssp.
4114 return reg_code - 3;
4115 } else {
4116 // This register has no safepoint register slot.
4117 UNREACHABLE();
4118 return -1;
4119 }
4120}
4121
Ben Murdoch097c5b22016-05-18 11:27:45 +01004122void MacroAssembler::CheckPageFlag(const Register& object,
4123 const Register& scratch, int mask,
4124 Condition cc, Label* condition_met) {
4125 And(scratch, object, ~Page::kPageAlignmentMask);
4126 Ldr(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
4127 if (cc == eq) {
4128 TestAndBranchIfAnySet(scratch, mask, condition_met);
4129 } else {
4130 TestAndBranchIfAllClear(scratch, mask, condition_met);
4131 }
4132}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004133
4134void MacroAssembler::CheckPageFlagSet(const Register& object,
4135 const Register& scratch,
4136 int mask,
4137 Label* if_any_set) {
4138 And(scratch, object, ~Page::kPageAlignmentMask);
4139 Ldr(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
4140 TestAndBranchIfAnySet(scratch, mask, if_any_set);
4141}
4142
4143
4144void MacroAssembler::CheckPageFlagClear(const Register& object,
4145 const Register& scratch,
4146 int mask,
4147 Label* if_all_clear) {
4148 And(scratch, object, ~Page::kPageAlignmentMask);
4149 Ldr(scratch, MemOperand(scratch, MemoryChunk::kFlagsOffset));
4150 TestAndBranchIfAllClear(scratch, mask, if_all_clear);
4151}
4152
4153
4154void MacroAssembler::RecordWriteField(
4155 Register object,
4156 int offset,
4157 Register value,
4158 Register scratch,
4159 LinkRegisterStatus lr_status,
4160 SaveFPRegsMode save_fp,
4161 RememberedSetAction remembered_set_action,
4162 SmiCheck smi_check,
4163 PointersToHereCheck pointers_to_here_check_for_value) {
4164 // First, check if a write barrier is even needed. The tests below
4165 // catch stores of Smis.
4166 Label done;
4167
4168 // Skip the barrier if writing a smi.
4169 if (smi_check == INLINE_SMI_CHECK) {
4170 JumpIfSmi(value, &done);
4171 }
4172
4173 // Although the object register is tagged, the offset is relative to the start
4174 // of the object, so offset must be a multiple of kPointerSize.
4175 DCHECK(IsAligned(offset, kPointerSize));
4176
4177 Add(scratch, object, offset - kHeapObjectTag);
4178 if (emit_debug_code()) {
4179 Label ok;
4180 Tst(scratch, (1 << kPointerSizeLog2) - 1);
4181 B(eq, &ok);
4182 Abort(kUnalignedCellInWriteBarrier);
4183 Bind(&ok);
4184 }
4185
4186 RecordWrite(object,
4187 scratch,
4188 value,
4189 lr_status,
4190 save_fp,
4191 remembered_set_action,
4192 OMIT_SMI_CHECK,
4193 pointers_to_here_check_for_value);
4194
4195 Bind(&done);
4196
4197 // Clobber clobbered input registers when running with the debug-code flag
4198 // turned on to provoke errors.
4199 if (emit_debug_code()) {
4200 Mov(value, Operand(bit_cast<int64_t>(kZapValue + 4)));
4201 Mov(scratch, Operand(bit_cast<int64_t>(kZapValue + 8)));
4202 }
4203}
4204
4205
4206// Will clobber: object, map, dst.
4207// If lr_status is kLRHasBeenSaved, lr will also be clobbered.
4208void MacroAssembler::RecordWriteForMap(Register object,
4209 Register map,
4210 Register dst,
4211 LinkRegisterStatus lr_status,
4212 SaveFPRegsMode fp_mode) {
4213 ASM_LOCATION("MacroAssembler::RecordWrite");
4214 DCHECK(!AreAliased(object, map));
4215
4216 if (emit_debug_code()) {
4217 UseScratchRegisterScope temps(this);
4218 Register temp = temps.AcquireX();
4219
4220 CompareObjectMap(map, temp, isolate()->factory()->meta_map());
4221 Check(eq, kWrongAddressOrValuePassedToRecordWrite);
4222 }
4223
4224 if (!FLAG_incremental_marking) {
4225 return;
4226 }
4227
4228 if (emit_debug_code()) {
4229 UseScratchRegisterScope temps(this);
4230 Register temp = temps.AcquireX();
4231
4232 Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
4233 Cmp(temp, map);
4234 Check(eq, kWrongAddressOrValuePassedToRecordWrite);
4235 }
4236
4237 // First, check if a write barrier is even needed. The tests below
4238 // catch stores of smis and stores into the young generation.
4239 Label done;
4240
4241 // A single check of the map's pages interesting flag suffices, since it is
4242 // only set during incremental collection, and then it's also guaranteed that
4243 // the from object's page's interesting flag is also set. This optimization
4244 // relies on the fact that maps can never be in new space.
4245 CheckPageFlagClear(map,
4246 map, // Used as scratch.
4247 MemoryChunk::kPointersToHereAreInterestingMask,
4248 &done);
4249
4250 // Record the actual write.
4251 if (lr_status == kLRHasNotBeenSaved) {
4252 Push(lr);
4253 }
4254 Add(dst, object, HeapObject::kMapOffset - kHeapObjectTag);
4255 RecordWriteStub stub(isolate(), object, map, dst, OMIT_REMEMBERED_SET,
4256 fp_mode);
4257 CallStub(&stub);
4258 if (lr_status == kLRHasNotBeenSaved) {
4259 Pop(lr);
4260 }
4261
4262 Bind(&done);
4263
4264 // Count number of write barriers in generated code.
4265 isolate()->counters()->write_barriers_static()->Increment();
4266 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1, map,
4267 dst);
4268
4269 // Clobber clobbered registers when running with the debug-code flag
4270 // turned on to provoke errors.
4271 if (emit_debug_code()) {
4272 Mov(dst, Operand(bit_cast<int64_t>(kZapValue + 12)));
4273 Mov(map, Operand(bit_cast<int64_t>(kZapValue + 16)));
4274 }
4275}
4276
4277
4278// Will clobber: object, address, value.
4279// If lr_status is kLRHasBeenSaved, lr will also be clobbered.
4280//
4281// The register 'object' contains a heap object pointer. The heap object tag is
4282// shifted away.
4283void MacroAssembler::RecordWrite(
4284 Register object,
4285 Register address,
4286 Register value,
4287 LinkRegisterStatus lr_status,
4288 SaveFPRegsMode fp_mode,
4289 RememberedSetAction remembered_set_action,
4290 SmiCheck smi_check,
4291 PointersToHereCheck pointers_to_here_check_for_value) {
4292 ASM_LOCATION("MacroAssembler::RecordWrite");
4293 DCHECK(!AreAliased(object, value));
4294
4295 if (emit_debug_code()) {
4296 UseScratchRegisterScope temps(this);
4297 Register temp = temps.AcquireX();
4298
4299 Ldr(temp, MemOperand(address));
4300 Cmp(temp, value);
4301 Check(eq, kWrongAddressOrValuePassedToRecordWrite);
4302 }
4303
4304 // First, check if a write barrier is even needed. The tests below
4305 // catch stores of smis and stores into the young generation.
4306 Label done;
4307
4308 if (smi_check == INLINE_SMI_CHECK) {
4309 DCHECK_EQ(0, kSmiTag);
4310 JumpIfSmi(value, &done);
4311 }
4312
4313 if (pointers_to_here_check_for_value != kPointersToHereAreAlwaysInteresting) {
4314 CheckPageFlagClear(value,
4315 value, // Used as scratch.
4316 MemoryChunk::kPointersToHereAreInterestingMask,
4317 &done);
4318 }
4319 CheckPageFlagClear(object,
4320 value, // Used as scratch.
4321 MemoryChunk::kPointersFromHereAreInterestingMask,
4322 &done);
4323
4324 // Record the actual write.
4325 if (lr_status == kLRHasNotBeenSaved) {
4326 Push(lr);
4327 }
4328 RecordWriteStub stub(isolate(), object, value, address, remembered_set_action,
4329 fp_mode);
4330 CallStub(&stub);
4331 if (lr_status == kLRHasNotBeenSaved) {
4332 Pop(lr);
4333 }
4334
4335 Bind(&done);
4336
4337 // Count number of write barriers in generated code.
4338 isolate()->counters()->write_barriers_static()->Increment();
4339 IncrementCounter(isolate()->counters()->write_barriers_dynamic(), 1, address,
4340 value);
4341
4342 // Clobber clobbered registers when running with the debug-code flag
4343 // turned on to provoke errors.
4344 if (emit_debug_code()) {
4345 Mov(address, Operand(bit_cast<int64_t>(kZapValue + 12)));
4346 Mov(value, Operand(bit_cast<int64_t>(kZapValue + 16)));
4347 }
4348}
4349
4350
4351void MacroAssembler::AssertHasValidColor(const Register& reg) {
4352 if (emit_debug_code()) {
4353 // The bit sequence is backward. The first character in the string
4354 // represents the least significant bit.
4355 DCHECK(strcmp(Marking::kImpossibleBitPattern, "01") == 0);
4356
4357 Label color_is_valid;
4358 Tbnz(reg, 0, &color_is_valid);
4359 Tbz(reg, 1, &color_is_valid);
4360 Abort(kUnexpectedColorFound);
4361 Bind(&color_is_valid);
4362 }
4363}
4364
4365
4366void MacroAssembler::GetMarkBits(Register addr_reg,
4367 Register bitmap_reg,
4368 Register shift_reg) {
4369 DCHECK(!AreAliased(addr_reg, bitmap_reg, shift_reg));
4370 DCHECK(addr_reg.Is64Bits() && bitmap_reg.Is64Bits() && shift_reg.Is64Bits());
4371 // addr_reg is divided into fields:
4372 // |63 page base 20|19 high 8|7 shift 3|2 0|
4373 // 'high' gives the index of the cell holding color bits for the object.
4374 // 'shift' gives the offset in the cell for this object's color.
4375 const int kShiftBits = kPointerSizeLog2 + Bitmap::kBitsPerCellLog2;
4376 UseScratchRegisterScope temps(this);
4377 Register temp = temps.AcquireX();
4378 Ubfx(temp, addr_reg, kShiftBits, kPageSizeBits - kShiftBits);
4379 Bic(bitmap_reg, addr_reg, Page::kPageAlignmentMask);
4380 Add(bitmap_reg, bitmap_reg, Operand(temp, LSL, Bitmap::kBytesPerCellLog2));
4381 // bitmap_reg:
4382 // |63 page base 20|19 zeros 15|14 high 3|2 0|
4383 Ubfx(shift_reg, addr_reg, kPointerSizeLog2, Bitmap::kBitsPerCellLog2);
4384}
4385
4386
4387void MacroAssembler::HasColor(Register object,
4388 Register bitmap_scratch,
4389 Register shift_scratch,
4390 Label* has_color,
4391 int first_bit,
4392 int second_bit) {
4393 // See mark-compact.h for color definitions.
4394 DCHECK(!AreAliased(object, bitmap_scratch, shift_scratch));
4395
4396 GetMarkBits(object, bitmap_scratch, shift_scratch);
4397 Ldr(bitmap_scratch, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
4398 // Shift the bitmap down to get the color of the object in bits [1:0].
4399 Lsr(bitmap_scratch, bitmap_scratch, shift_scratch);
4400
4401 AssertHasValidColor(bitmap_scratch);
4402
4403 // These bit sequences are backwards. The first character in the string
4404 // represents the least significant bit.
4405 DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004406 DCHECK(strcmp(Marking::kBlackBitPattern, "11") == 0);
4407 DCHECK(strcmp(Marking::kGreyBitPattern, "10") == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004408
4409 // Check for the color.
4410 if (first_bit == 0) {
4411 // Checking for white.
4412 DCHECK(second_bit == 0);
4413 // We only need to test the first bit.
4414 Tbz(bitmap_scratch, 0, has_color);
4415 } else {
4416 Label other_color;
4417 // Checking for grey or black.
4418 Tbz(bitmap_scratch, 0, &other_color);
4419 if (second_bit == 0) {
4420 Tbz(bitmap_scratch, 1, has_color);
4421 } else {
4422 Tbnz(bitmap_scratch, 1, has_color);
4423 }
4424 Bind(&other_color);
4425 }
4426
4427 // Fall through if it does not have the right color.
4428}
4429
4430
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004431void MacroAssembler::JumpIfBlack(Register object,
4432 Register scratch0,
4433 Register scratch1,
4434 Label* on_black) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004435 DCHECK(strcmp(Marking::kBlackBitPattern, "11") == 0);
4436 HasColor(object, scratch0, scratch1, on_black, 1, 1); // kBlackBitPattern.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004437}
4438
4439
4440void MacroAssembler::JumpIfDictionaryInPrototypeChain(
4441 Register object,
4442 Register scratch0,
4443 Register scratch1,
4444 Label* found) {
4445 DCHECK(!AreAliased(object, scratch0, scratch1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004446 Register current = scratch0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004447 Label loop_again, end;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004448
4449 // Scratch contains elements pointer.
4450 Mov(current, object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004451 Ldr(current, FieldMemOperand(current, HeapObject::kMapOffset));
4452 Ldr(current, FieldMemOperand(current, Map::kPrototypeOffset));
4453 CompareAndBranch(current, Heap::kNullValueRootIndex, eq, &end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004454
4455 // Loop based on the map going up the prototype chain.
4456 Bind(&loop_again);
4457 Ldr(current, FieldMemOperand(current, HeapObject::kMapOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004458 STATIC_ASSERT(JS_PROXY_TYPE < JS_OBJECT_TYPE);
4459 STATIC_ASSERT(JS_VALUE_TYPE < JS_OBJECT_TYPE);
4460 CompareInstanceType(current, scratch1, JS_OBJECT_TYPE);
4461 B(lo, found);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004462 Ldrb(scratch1, FieldMemOperand(current, Map::kBitField2Offset));
4463 DecodeField<Map::ElementsKindBits>(scratch1);
4464 CompareAndBranch(scratch1, DICTIONARY_ELEMENTS, eq, found);
4465 Ldr(current, FieldMemOperand(current, Map::kPrototypeOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004466 CompareAndBranch(current, Heap::kNullValueRootIndex, ne, &loop_again);
4467
4468 Bind(&end);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004469}
4470
4471
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004472void MacroAssembler::JumpIfWhite(Register value, Register bitmap_scratch,
4473 Register shift_scratch, Register load_scratch,
4474 Register length_scratch,
4475 Label* value_is_white) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004476 DCHECK(!AreAliased(
4477 value, bitmap_scratch, shift_scratch, load_scratch, length_scratch));
4478
4479 // These bit sequences are backwards. The first character in the string
4480 // represents the least significant bit.
4481 DCHECK(strcmp(Marking::kWhiteBitPattern, "00") == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004482 DCHECK(strcmp(Marking::kBlackBitPattern, "11") == 0);
4483 DCHECK(strcmp(Marking::kGreyBitPattern, "10") == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004484
4485 GetMarkBits(value, bitmap_scratch, shift_scratch);
4486 Ldr(load_scratch, MemOperand(bitmap_scratch, MemoryChunk::kHeaderSize));
4487 Lsr(load_scratch, load_scratch, shift_scratch);
4488
4489 AssertHasValidColor(load_scratch);
4490
4491 // If the value is black or grey we don't need to do anything.
4492 // Since both black and grey have a 1 in the first position and white does
4493 // not have a 1 there we only need to check one bit.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004494 Tbz(load_scratch, 0, value_is_white);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004495}
4496
4497
4498void MacroAssembler::Assert(Condition cond, BailoutReason reason) {
4499 if (emit_debug_code()) {
4500 Check(cond, reason);
4501 }
4502}
4503
4504
4505
4506void MacroAssembler::AssertRegisterIsClear(Register reg, BailoutReason reason) {
4507 if (emit_debug_code()) {
4508 CheckRegisterIsClear(reg, reason);
4509 }
4510}
4511
4512
4513void MacroAssembler::AssertRegisterIsRoot(Register reg,
4514 Heap::RootListIndex index,
4515 BailoutReason reason) {
4516 if (emit_debug_code()) {
4517 CompareRoot(reg, index);
4518 Check(eq, reason);
4519 }
4520}
4521
4522
4523void MacroAssembler::AssertFastElements(Register elements) {
4524 if (emit_debug_code()) {
4525 UseScratchRegisterScope temps(this);
4526 Register temp = temps.AcquireX();
4527 Label ok;
4528 Ldr(temp, FieldMemOperand(elements, HeapObject::kMapOffset));
4529 JumpIfRoot(temp, Heap::kFixedArrayMapRootIndex, &ok);
4530 JumpIfRoot(temp, Heap::kFixedDoubleArrayMapRootIndex, &ok);
4531 JumpIfRoot(temp, Heap::kFixedCOWArrayMapRootIndex, &ok);
4532 Abort(kJSObjectWithFastElementsMapHasSlowElements);
4533 Bind(&ok);
4534 }
4535}
4536
4537
4538void MacroAssembler::AssertIsString(const Register& object) {
4539 if (emit_debug_code()) {
4540 UseScratchRegisterScope temps(this);
4541 Register temp = temps.AcquireX();
4542 STATIC_ASSERT(kSmiTag == 0);
4543 Tst(object, kSmiTagMask);
4544 Check(ne, kOperandIsNotAString);
4545 Ldr(temp, FieldMemOperand(object, HeapObject::kMapOffset));
4546 CompareInstanceType(temp, temp, FIRST_NONSTRING_TYPE);
4547 Check(lo, kOperandIsNotAString);
4548 }
4549}
4550
4551
4552void MacroAssembler::Check(Condition cond, BailoutReason reason) {
4553 Label ok;
4554 B(cond, &ok);
4555 Abort(reason);
4556 // Will not return here.
4557 Bind(&ok);
4558}
4559
4560
4561void MacroAssembler::CheckRegisterIsClear(Register reg, BailoutReason reason) {
4562 Label ok;
4563 Cbz(reg, &ok);
4564 Abort(reason);
4565 // Will not return here.
4566 Bind(&ok);
4567}
4568
4569
4570void MacroAssembler::Abort(BailoutReason reason) {
4571#ifdef DEBUG
4572 RecordComment("Abort message: ");
4573 RecordComment(GetBailoutReason(reason));
4574
4575 if (FLAG_trap_on_abort) {
4576 Brk(0);
4577 return;
4578 }
4579#endif
4580
4581 // Abort is used in some contexts where csp is the stack pointer. In order to
4582 // simplify the CallRuntime code, make sure that jssp is the stack pointer.
4583 // There is no risk of register corruption here because Abort doesn't return.
4584 Register old_stack_pointer = StackPointer();
4585 SetStackPointer(jssp);
4586 Mov(jssp, old_stack_pointer);
4587
4588 // We need some scratch registers for the MacroAssembler, so make sure we have
4589 // some. This is safe here because Abort never returns.
4590 RegList old_tmp_list = TmpList()->list();
4591 TmpList()->Combine(MacroAssembler::DefaultTmpList());
4592
4593 if (use_real_aborts()) {
4594 // Avoid infinite recursion; Push contains some assertions that use Abort.
4595 NoUseRealAbortsScope no_real_aborts(this);
4596
4597 Mov(x0, Smi::FromInt(reason));
4598 Push(x0);
4599
4600 if (!has_frame_) {
4601 // We don't actually want to generate a pile of code for this, so just
4602 // claim there is a stack frame, without generating one.
4603 FrameScope scope(this, StackFrame::NONE);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004604 CallRuntime(Runtime::kAbort);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004605 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004606 CallRuntime(Runtime::kAbort);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004607 }
4608 } else {
4609 // Load the string to pass to Printf.
4610 Label msg_address;
4611 Adr(x0, &msg_address);
4612
4613 // Call Printf directly to report the error.
4614 CallPrintf();
4615
4616 // We need a way to stop execution on both the simulator and real hardware,
4617 // and Unreachable() is the best option.
4618 Unreachable();
4619
4620 // Emit the message string directly in the instruction stream.
4621 {
4622 BlockPoolsScope scope(this);
4623 Bind(&msg_address);
4624 EmitStringData(GetBailoutReason(reason));
4625 }
4626 }
4627
4628 SetStackPointer(old_stack_pointer);
4629 TmpList()->set_list(old_tmp_list);
4630}
4631
4632
4633void MacroAssembler::LoadTransitionedArrayMapConditional(
4634 ElementsKind expected_kind,
4635 ElementsKind transitioned_kind,
4636 Register map_in_out,
4637 Register scratch1,
4638 Register scratch2,
4639 Label* no_map_match) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004640 DCHECK(IsFastElementsKind(expected_kind));
4641 DCHECK(IsFastElementsKind(transitioned_kind));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004642
4643 // Check that the function's map is the same as the expected cached map.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004644 Ldr(scratch1, NativeContextMemOperand());
4645 Ldr(scratch2,
4646 ContextMemOperand(scratch1, Context::ArrayMapIndex(expected_kind)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004647 Cmp(map_in_out, scratch2);
4648 B(ne, no_map_match);
4649
4650 // Use the transitioned cached map.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004651 Ldr(map_in_out,
4652 ContextMemOperand(scratch1, Context::ArrayMapIndex(transitioned_kind)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004653}
4654
4655
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004656void MacroAssembler::LoadNativeContextSlot(int index, Register dst) {
4657 Ldr(dst, NativeContextMemOperand());
4658 Ldr(dst, ContextMemOperand(dst, index));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004659}
4660
4661
4662void MacroAssembler::LoadGlobalFunctionInitialMap(Register function,
4663 Register map,
4664 Register scratch) {
4665 // Load the initial map. The global functions all have initial maps.
4666 Ldr(map, FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
4667 if (emit_debug_code()) {
4668 Label ok, fail;
4669 CheckMap(map, scratch, Heap::kMetaMapRootIndex, &fail, DO_SMI_CHECK);
4670 B(&ok);
4671 Bind(&fail);
4672 Abort(kGlobalFunctionsMustHaveInitialMap);
4673 Bind(&ok);
4674 }
4675}
4676
4677
4678// This is the main Printf implementation. All other Printf variants call
4679// PrintfNoPreserve after setting up one or more PreserveRegisterScopes.
4680void MacroAssembler::PrintfNoPreserve(const char * format,
4681 const CPURegister& arg0,
4682 const CPURegister& arg1,
4683 const CPURegister& arg2,
4684 const CPURegister& arg3) {
4685 // We cannot handle a caller-saved stack pointer. It doesn't make much sense
4686 // in most cases anyway, so this restriction shouldn't be too serious.
4687 DCHECK(!kCallerSaved.IncludesAliasOf(__ StackPointer()));
4688
4689 // The provided arguments, and their proper procedure-call standard registers.
4690 CPURegister args[kPrintfMaxArgCount] = {arg0, arg1, arg2, arg3};
4691 CPURegister pcs[kPrintfMaxArgCount] = {NoReg, NoReg, NoReg, NoReg};
4692
4693 int arg_count = kPrintfMaxArgCount;
4694
4695 // The PCS varargs registers for printf. Note that x0 is used for the printf
4696 // format string.
4697 static const CPURegList kPCSVarargs =
4698 CPURegList(CPURegister::kRegister, kXRegSizeInBits, 1, arg_count);
4699 static const CPURegList kPCSVarargsFP =
4700 CPURegList(CPURegister::kFPRegister, kDRegSizeInBits, 0, arg_count - 1);
4701
4702 // We can use caller-saved registers as scratch values, except for the
4703 // arguments and the PCS registers where they might need to go.
4704 CPURegList tmp_list = kCallerSaved;
4705 tmp_list.Remove(x0); // Used to pass the format string.
4706 tmp_list.Remove(kPCSVarargs);
4707 tmp_list.Remove(arg0, arg1, arg2, arg3);
4708
4709 CPURegList fp_tmp_list = kCallerSavedFP;
4710 fp_tmp_list.Remove(kPCSVarargsFP);
4711 fp_tmp_list.Remove(arg0, arg1, arg2, arg3);
4712
4713 // Override the MacroAssembler's scratch register list. The lists will be
4714 // reset automatically at the end of the UseScratchRegisterScope.
4715 UseScratchRegisterScope temps(this);
4716 TmpList()->set_list(tmp_list.list());
4717 FPTmpList()->set_list(fp_tmp_list.list());
4718
4719 // Copies of the printf vararg registers that we can pop from.
4720 CPURegList pcs_varargs = kPCSVarargs;
4721 CPURegList pcs_varargs_fp = kPCSVarargsFP;
4722
4723 // Place the arguments. There are lots of clever tricks and optimizations we
4724 // could use here, but Printf is a debug tool so instead we just try to keep
4725 // it simple: Move each input that isn't already in the right place to a
4726 // scratch register, then move everything back.
4727 for (unsigned i = 0; i < kPrintfMaxArgCount; i++) {
4728 // Work out the proper PCS register for this argument.
4729 if (args[i].IsRegister()) {
4730 pcs[i] = pcs_varargs.PopLowestIndex().X();
4731 // We might only need a W register here. We need to know the size of the
4732 // argument so we can properly encode it for the simulator call.
4733 if (args[i].Is32Bits()) pcs[i] = pcs[i].W();
4734 } else if (args[i].IsFPRegister()) {
4735 // In C, floats are always cast to doubles for varargs calls.
4736 pcs[i] = pcs_varargs_fp.PopLowestIndex().D();
4737 } else {
4738 DCHECK(args[i].IsNone());
4739 arg_count = i;
4740 break;
4741 }
4742
4743 // If the argument is already in the right place, leave it where it is.
4744 if (args[i].Aliases(pcs[i])) continue;
4745
4746 // Otherwise, if the argument is in a PCS argument register, allocate an
4747 // appropriate scratch register and then move it out of the way.
4748 if (kPCSVarargs.IncludesAliasOf(args[i]) ||
4749 kPCSVarargsFP.IncludesAliasOf(args[i])) {
4750 if (args[i].IsRegister()) {
4751 Register old_arg = Register(args[i]);
4752 Register new_arg = temps.AcquireSameSizeAs(old_arg);
4753 Mov(new_arg, old_arg);
4754 args[i] = new_arg;
4755 } else {
4756 FPRegister old_arg = FPRegister(args[i]);
4757 FPRegister new_arg = temps.AcquireSameSizeAs(old_arg);
4758 Fmov(new_arg, old_arg);
4759 args[i] = new_arg;
4760 }
4761 }
4762 }
4763
4764 // Do a second pass to move values into their final positions and perform any
4765 // conversions that may be required.
4766 for (int i = 0; i < arg_count; i++) {
4767 DCHECK(pcs[i].type() == args[i].type());
4768 if (pcs[i].IsRegister()) {
4769 Mov(Register(pcs[i]), Register(args[i]), kDiscardForSameWReg);
4770 } else {
4771 DCHECK(pcs[i].IsFPRegister());
4772 if (pcs[i].SizeInBytes() == args[i].SizeInBytes()) {
4773 Fmov(FPRegister(pcs[i]), FPRegister(args[i]));
4774 } else {
4775 Fcvt(FPRegister(pcs[i]), FPRegister(args[i]));
4776 }
4777 }
4778 }
4779
4780 // Load the format string into x0, as per the procedure-call standard.
4781 //
4782 // To make the code as portable as possible, the format string is encoded
4783 // directly in the instruction stream. It might be cleaner to encode it in a
4784 // literal pool, but since Printf is usually used for debugging, it is
4785 // beneficial for it to be minimally dependent on other features.
4786 Label format_address;
4787 Adr(x0, &format_address);
4788
4789 // Emit the format string directly in the instruction stream.
4790 { BlockPoolsScope scope(this);
4791 Label after_data;
4792 B(&after_data);
4793 Bind(&format_address);
4794 EmitStringData(format);
4795 Unreachable();
4796 Bind(&after_data);
4797 }
4798
4799 // We don't pass any arguments on the stack, but we still need to align the C
4800 // stack pointer to a 16-byte boundary for PCS compliance.
4801 if (!csp.Is(StackPointer())) {
4802 Bic(csp, StackPointer(), 0xf);
4803 }
4804
4805 CallPrintf(arg_count, pcs);
4806}
4807
4808
4809void MacroAssembler::CallPrintf(int arg_count, const CPURegister * args) {
4810 // A call to printf needs special handling for the simulator, since the system
4811 // printf function will use a different instruction set and the procedure-call
4812 // standard will not be compatible.
4813#ifdef USE_SIMULATOR
4814 { InstructionAccurateScope scope(this, kPrintfLength / kInstructionSize);
4815 hlt(kImmExceptionIsPrintf);
4816 dc32(arg_count); // kPrintfArgCountOffset
4817
4818 // Determine the argument pattern.
4819 uint32_t arg_pattern_list = 0;
4820 for (int i = 0; i < arg_count; i++) {
4821 uint32_t arg_pattern;
4822 if (args[i].IsRegister()) {
4823 arg_pattern = args[i].Is32Bits() ? kPrintfArgW : kPrintfArgX;
4824 } else {
4825 DCHECK(args[i].Is64Bits());
4826 arg_pattern = kPrintfArgD;
4827 }
4828 DCHECK(arg_pattern < (1 << kPrintfArgPatternBits));
4829 arg_pattern_list |= (arg_pattern << (kPrintfArgPatternBits * i));
4830 }
4831 dc32(arg_pattern_list); // kPrintfArgPatternListOffset
4832 }
4833#else
4834 Call(FUNCTION_ADDR(printf), RelocInfo::EXTERNAL_REFERENCE);
4835#endif
4836}
4837
4838
4839void MacroAssembler::Printf(const char * format,
4840 CPURegister arg0,
4841 CPURegister arg1,
4842 CPURegister arg2,
4843 CPURegister arg3) {
4844 // We can only print sp if it is the current stack pointer.
4845 if (!csp.Is(StackPointer())) {
4846 DCHECK(!csp.Aliases(arg0));
4847 DCHECK(!csp.Aliases(arg1));
4848 DCHECK(!csp.Aliases(arg2));
4849 DCHECK(!csp.Aliases(arg3));
4850 }
4851
4852 // Printf is expected to preserve all registers, so make sure that none are
4853 // available as scratch registers until we've preserved them.
4854 RegList old_tmp_list = TmpList()->list();
4855 RegList old_fp_tmp_list = FPTmpList()->list();
4856 TmpList()->set_list(0);
4857 FPTmpList()->set_list(0);
4858
4859 // Preserve all caller-saved registers as well as NZCV.
4860 // If csp is the stack pointer, PushCPURegList asserts that the size of each
4861 // list is a multiple of 16 bytes.
4862 PushCPURegList(kCallerSaved);
4863 PushCPURegList(kCallerSavedFP);
4864
4865 // We can use caller-saved registers as scratch values (except for argN).
4866 CPURegList tmp_list = kCallerSaved;
4867 CPURegList fp_tmp_list = kCallerSavedFP;
4868 tmp_list.Remove(arg0, arg1, arg2, arg3);
4869 fp_tmp_list.Remove(arg0, arg1, arg2, arg3);
4870 TmpList()->set_list(tmp_list.list());
4871 FPTmpList()->set_list(fp_tmp_list.list());
4872
4873 { UseScratchRegisterScope temps(this);
4874 // If any of the arguments are the current stack pointer, allocate a new
4875 // register for them, and adjust the value to compensate for pushing the
4876 // caller-saved registers.
4877 bool arg0_sp = StackPointer().Aliases(arg0);
4878 bool arg1_sp = StackPointer().Aliases(arg1);
4879 bool arg2_sp = StackPointer().Aliases(arg2);
4880 bool arg3_sp = StackPointer().Aliases(arg3);
4881 if (arg0_sp || arg1_sp || arg2_sp || arg3_sp) {
4882 // Allocate a register to hold the original stack pointer value, to pass
4883 // to PrintfNoPreserve as an argument.
4884 Register arg_sp = temps.AcquireX();
4885 Add(arg_sp, StackPointer(),
4886 kCallerSaved.TotalSizeInBytes() + kCallerSavedFP.TotalSizeInBytes());
4887 if (arg0_sp) arg0 = Register::Create(arg_sp.code(), arg0.SizeInBits());
4888 if (arg1_sp) arg1 = Register::Create(arg_sp.code(), arg1.SizeInBits());
4889 if (arg2_sp) arg2 = Register::Create(arg_sp.code(), arg2.SizeInBits());
4890 if (arg3_sp) arg3 = Register::Create(arg_sp.code(), arg3.SizeInBits());
4891 }
4892
4893 // Preserve NZCV.
4894 { UseScratchRegisterScope temps(this);
4895 Register tmp = temps.AcquireX();
4896 Mrs(tmp, NZCV);
4897 Push(tmp, xzr);
4898 }
4899
4900 PrintfNoPreserve(format, arg0, arg1, arg2, arg3);
4901
4902 // Restore NZCV.
4903 { UseScratchRegisterScope temps(this);
4904 Register tmp = temps.AcquireX();
4905 Pop(xzr, tmp);
4906 Msr(NZCV, tmp);
4907 }
4908 }
4909
4910 PopCPURegList(kCallerSavedFP);
4911 PopCPURegList(kCallerSaved);
4912
4913 TmpList()->set_list(old_tmp_list);
4914 FPTmpList()->set_list(old_fp_tmp_list);
4915}
4916
4917
4918void MacroAssembler::EmitFrameSetupForCodeAgePatching() {
4919 // TODO(jbramley): Other architectures use the internal memcpy to copy the
4920 // sequence. If this is a performance bottleneck, we should consider caching
4921 // the sequence and copying it in the same way.
4922 InstructionAccurateScope scope(this,
4923 kNoCodeAgeSequenceLength / kInstructionSize);
4924 DCHECK(jssp.Is(StackPointer()));
4925 EmitFrameSetupForCodeAgePatching(this);
4926}
4927
4928
4929
4930void MacroAssembler::EmitCodeAgeSequence(Code* stub) {
4931 InstructionAccurateScope scope(this,
4932 kNoCodeAgeSequenceLength / kInstructionSize);
4933 DCHECK(jssp.Is(StackPointer()));
4934 EmitCodeAgeSequence(this, stub);
4935}
4936
4937
4938#undef __
4939#define __ assm->
4940
4941
4942void MacroAssembler::EmitFrameSetupForCodeAgePatching(Assembler * assm) {
4943 Label start;
4944 __ bind(&start);
4945
4946 // We can do this sequence using four instructions, but the code ageing
4947 // sequence that patches it needs five, so we use the extra space to try to
4948 // simplify some addressing modes and remove some dependencies (compared to
4949 // using two stp instructions with write-back).
4950 __ sub(jssp, jssp, 4 * kXRegSize);
4951 __ sub(csp, csp, 4 * kXRegSize);
4952 __ stp(x1, cp, MemOperand(jssp, 0 * kXRegSize));
4953 __ stp(fp, lr, MemOperand(jssp, 2 * kXRegSize));
4954 __ add(fp, jssp, StandardFrameConstants::kFixedFrameSizeFromFp);
4955
4956 __ AssertSizeOfCodeGeneratedSince(&start, kNoCodeAgeSequenceLength);
4957}
4958
4959
4960void MacroAssembler::EmitCodeAgeSequence(Assembler * assm,
4961 Code * stub) {
4962 Label start;
4963 __ bind(&start);
4964 // When the stub is called, the sequence is replaced with the young sequence
4965 // (as in EmitFrameSetupForCodeAgePatching). After the code is replaced, the
4966 // stub jumps to &start, stored in x0. The young sequence does not call the
4967 // stub so there is no infinite loop here.
4968 //
4969 // A branch (br) is used rather than a call (blr) because this code replaces
4970 // the frame setup code that would normally preserve lr.
4971 __ ldr_pcrel(ip0, kCodeAgeStubEntryOffset >> kLoadLiteralScaleLog2);
4972 __ adr(x0, &start);
4973 __ br(ip0);
4974 // IsCodeAgeSequence in codegen-arm64.cc assumes that the code generated up
4975 // until now (kCodeAgeStubEntryOffset) is the same for all code age sequences.
4976 __ AssertSizeOfCodeGeneratedSince(&start, kCodeAgeStubEntryOffset);
4977 if (stub) {
4978 __ dc64(reinterpret_cast<uint64_t>(stub->instruction_start()));
4979 __ AssertSizeOfCodeGeneratedSince(&start, kNoCodeAgeSequenceLength);
4980 }
4981}
4982
4983
4984bool MacroAssembler::IsYoungSequence(Isolate* isolate, byte* sequence) {
4985 bool is_young = isolate->code_aging_helper()->IsYoung(sequence);
4986 DCHECK(is_young ||
4987 isolate->code_aging_helper()->IsOld(sequence));
4988 return is_young;
4989}
4990
4991
4992void MacroAssembler::TruncatingDiv(Register result,
4993 Register dividend,
4994 int32_t divisor) {
4995 DCHECK(!AreAliased(result, dividend));
4996 DCHECK(result.Is32Bits() && dividend.Is32Bits());
4997 base::MagicNumbersForDivision<uint32_t> mag =
4998 base::SignedDivisionByConstant(static_cast<uint32_t>(divisor));
4999 Mov(result, mag.multiplier);
5000 Smull(result.X(), dividend, result);
5001 Asr(result.X(), result.X(), 32);
5002 bool neg = (mag.multiplier & (static_cast<uint32_t>(1) << 31)) != 0;
5003 if (divisor > 0 && neg) Add(result, result, dividend);
5004 if (divisor < 0 && !neg && mag.multiplier > 0) Sub(result, result, dividend);
5005 if (mag.shift > 0) Asr(result, result, mag.shift);
5006 Add(result, result, Operand(dividend, LSR, 31));
5007}
5008
5009
5010#undef __
5011
5012
5013UseScratchRegisterScope::~UseScratchRegisterScope() {
5014 available_->set_list(old_available_);
5015 availablefp_->set_list(old_availablefp_);
5016}
5017
5018
5019Register UseScratchRegisterScope::AcquireSameSizeAs(const Register& reg) {
5020 int code = AcquireNextAvailable(available_).code();
5021 return Register::Create(code, reg.SizeInBits());
5022}
5023
5024
5025FPRegister UseScratchRegisterScope::AcquireSameSizeAs(const FPRegister& reg) {
5026 int code = AcquireNextAvailable(availablefp_).code();
5027 return FPRegister::Create(code, reg.SizeInBits());
5028}
5029
5030
5031CPURegister UseScratchRegisterScope::AcquireNextAvailable(
5032 CPURegList* available) {
5033 CHECK(!available->IsEmpty());
5034 CPURegister result = available->PopLowestIndex();
5035 DCHECK(!AreAliased(result, xzr, csp));
5036 return result;
5037}
5038
5039
5040CPURegister UseScratchRegisterScope::UnsafeAcquire(CPURegList* available,
5041 const CPURegister& reg) {
5042 DCHECK(available->IncludesAliasOf(reg));
5043 available->Remove(reg);
5044 return reg;
5045}
5046
5047
5048#define __ masm->
5049
5050
5051void InlineSmiCheckInfo::Emit(MacroAssembler* masm, const Register& reg,
5052 const Label* smi_check) {
5053 Assembler::BlockPoolsScope scope(masm);
5054 if (reg.IsValid()) {
5055 DCHECK(smi_check->is_bound());
5056 DCHECK(reg.Is64Bits());
5057
5058 // Encode the register (x0-x30) in the lowest 5 bits, then the offset to
5059 // 'check' in the other bits. The possible offset is limited in that we
5060 // use BitField to pack the data, and the underlying data type is a
5061 // uint32_t.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005062 uint32_t delta =
5063 static_cast<uint32_t>(__ InstructionsGeneratedSince(smi_check));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005064 __ InlineData(RegisterBits::encode(reg.code()) | DeltaBits::encode(delta));
5065 } else {
5066 DCHECK(!smi_check->is_bound());
5067
5068 // An offset of 0 indicates that there is no patch site.
5069 __ InlineData(0);
5070 }
5071}
5072
5073
5074InlineSmiCheckInfo::InlineSmiCheckInfo(Address info)
5075 : reg_(NoReg), smi_check_(NULL) {
5076 InstructionSequence* inline_data = InstructionSequence::At(info);
5077 DCHECK(inline_data->IsInlineData());
5078 if (inline_data->IsInlineData()) {
5079 uint64_t payload = inline_data->InlineData();
5080 // We use BitField to decode the payload, and BitField can only handle
5081 // 32-bit values.
5082 DCHECK(is_uint32(payload));
5083 if (payload != 0) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005084 uint32_t payload32 = static_cast<uint32_t>(payload);
5085 int reg_code = RegisterBits::decode(payload32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005086 reg_ = Register::XRegFromCode(reg_code);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005087 int smi_check_delta = DeltaBits::decode(payload32);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005088 DCHECK(smi_check_delta != 0);
5089 smi_check_ = inline_data->preceding(smi_check_delta);
5090 }
5091 }
5092}
5093
5094
5095#undef __
5096
5097
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005098} // namespace internal
5099} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005100
5101#endif // V8_TARGET_ARCH_ARM64