deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / arm64 / code-stubs-arm64.cc
1 // 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
5 #include "src/v8.h"
6
7 #if V8_TARGET_ARCH_ARM64
8
9 #include "src/bootstrapper.h"
10 #include "src/code-stubs.h"
11 #include "src/codegen.h"
12 #include "src/ic/handler-compiler.h"
13 #include "src/ic/ic.h"
14 #include "src/ic/stub-cache.h"
15 #include "src/isolate.h"
16 #include "src/jsregexp.h"
17 #include "src/regexp-macro-assembler.h"
18 #include "src/runtime/runtime.h"
19
20 namespace v8 {
21 namespace internal {
22
23
24 static void InitializeArrayConstructorDescriptor(
25     Isolate* isolate, CodeStubDescriptor* descriptor,
26     int constant_stack_parameter_count) {
27   // cp: context
28   // x1: function
29   // x2: allocation site with elements kind
30   // x0: number of arguments to the constructor function
31   Address deopt_handler = Runtime::FunctionForId(
32       Runtime::kArrayConstructor)->entry;
33
34   if (constant_stack_parameter_count == 0) {
35     descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
36                            JS_FUNCTION_STUB_MODE);
37   } else {
38     descriptor->Initialize(x0, deopt_handler, constant_stack_parameter_count,
39                            JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
40   }
41 }
42
43
44 void ArrayNoArgumentConstructorStub::InitializeDescriptor(
45     CodeStubDescriptor* descriptor) {
46   InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
47 }
48
49
50 void ArraySingleArgumentConstructorStub::InitializeDescriptor(
51     CodeStubDescriptor* descriptor) {
52   InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
53 }
54
55
56 void ArrayNArgumentsConstructorStub::InitializeDescriptor(
57     CodeStubDescriptor* descriptor) {
58   InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
59 }
60
61
62 static void InitializeInternalArrayConstructorDescriptor(
63     Isolate* isolate, CodeStubDescriptor* descriptor,
64     int constant_stack_parameter_count) {
65   Address deopt_handler = Runtime::FunctionForId(
66       Runtime::kInternalArrayConstructor)->entry;
67
68   if (constant_stack_parameter_count == 0) {
69     descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
70                            JS_FUNCTION_STUB_MODE);
71   } else {
72     descriptor->Initialize(x0, deopt_handler, constant_stack_parameter_count,
73                            JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
74   }
75 }
76
77
78 void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
79     CodeStubDescriptor* descriptor) {
80   InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
81 }
82
83
84 void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
85     CodeStubDescriptor* descriptor) {
86   InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
87 }
88
89
90 void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
91     CodeStubDescriptor* descriptor) {
92   InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
93 }
94
95
96 #define __ ACCESS_MASM(masm)
97
98
99 void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
100                                                ExternalReference miss) {
101   // Update the static counter each time a new code stub is generated.
102   isolate()->counters()->code_stubs()->Increment();
103
104   CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
105   int param_count = descriptor.GetEnvironmentParameterCount();
106   {
107     // Call the runtime system in a fresh internal frame.
108     FrameScope scope(masm, StackFrame::INTERNAL);
109     DCHECK((param_count == 0) ||
110            x0.Is(descriptor.GetEnvironmentParameterRegister(param_count - 1)));
111
112     // Push arguments
113     MacroAssembler::PushPopQueue queue(masm);
114     for (int i = 0; i < param_count; ++i) {
115       queue.Queue(descriptor.GetEnvironmentParameterRegister(i));
116     }
117     queue.PushQueued();
118
119     __ CallExternalReference(miss, param_count);
120   }
121
122   __ Ret();
123 }
124
125
126 void DoubleToIStub::Generate(MacroAssembler* masm) {
127   Label done;
128   Register input = source();
129   Register result = destination();
130   DCHECK(is_truncating());
131
132   DCHECK(result.Is64Bits());
133   DCHECK(jssp.Is(masm->StackPointer()));
134
135   int double_offset = offset();
136
137   DoubleRegister double_scratch = d0;  // only used if !skip_fastpath()
138   Register scratch1 = GetAllocatableRegisterThatIsNotOneOf(input, result);
139   Register scratch2 =
140       GetAllocatableRegisterThatIsNotOneOf(input, result, scratch1);
141
142   __ Push(scratch1, scratch2);
143   // Account for saved regs if input is jssp.
144   if (input.is(jssp)) double_offset += 2 * kPointerSize;
145
146   if (!skip_fastpath()) {
147     __ Push(double_scratch);
148     if (input.is(jssp)) double_offset += 1 * kDoubleSize;
149     __ Ldr(double_scratch, MemOperand(input, double_offset));
150     // Try to convert with a FPU convert instruction.  This handles all
151     // non-saturating cases.
152     __ TryConvertDoubleToInt64(result, double_scratch, &done);
153     __ Fmov(result, double_scratch);
154   } else {
155     __ Ldr(result, MemOperand(input, double_offset));
156   }
157
158   // If we reach here we need to manually convert the input to an int32.
159
160   // Extract the exponent.
161   Register exponent = scratch1;
162   __ Ubfx(exponent, result, HeapNumber::kMantissaBits,
163           HeapNumber::kExponentBits);
164
165   // It the exponent is >= 84 (kMantissaBits + 32), the result is always 0 since
166   // the mantissa gets shifted completely out of the int32_t result.
167   __ Cmp(exponent, HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 32);
168   __ CzeroX(result, ge);
169   __ B(ge, &done);
170
171   // The Fcvtzs sequence handles all cases except where the conversion causes
172   // signed overflow in the int64_t target. Since we've already handled
173   // exponents >= 84, we can guarantee that 63 <= exponent < 84.
174
175   if (masm->emit_debug_code()) {
176     __ Cmp(exponent, HeapNumber::kExponentBias + 63);
177     // Exponents less than this should have been handled by the Fcvt case.
178     __ Check(ge, kUnexpectedValue);
179   }
180
181   // Isolate the mantissa bits, and set the implicit '1'.
182   Register mantissa = scratch2;
183   __ Ubfx(mantissa, result, 0, HeapNumber::kMantissaBits);
184   __ Orr(mantissa, mantissa, 1UL << HeapNumber::kMantissaBits);
185
186   // Negate the mantissa if necessary.
187   __ Tst(result, kXSignMask);
188   __ Cneg(mantissa, mantissa, ne);
189
190   // Shift the mantissa bits in the correct place. We know that we have to shift
191   // it left here, because exponent >= 63 >= kMantissaBits.
192   __ Sub(exponent, exponent,
193          HeapNumber::kExponentBias + HeapNumber::kMantissaBits);
194   __ Lsl(result, mantissa, exponent);
195
196   __ Bind(&done);
197   if (!skip_fastpath()) {
198     __ Pop(double_scratch);
199   }
200   __ Pop(scratch2, scratch1);
201   __ Ret();
202 }
203
204
205 // See call site for description.
206 static void EmitIdenticalObjectComparison(MacroAssembler* masm,
207                                           Register left,
208                                           Register right,
209                                           Register scratch,
210                                           FPRegister double_scratch,
211                                           Label* slow,
212                                           Condition cond) {
213   DCHECK(!AreAliased(left, right, scratch));
214   Label not_identical, return_equal, heap_number;
215   Register result = x0;
216
217   __ Cmp(right, left);
218   __ B(ne, &not_identical);
219
220   // Test for NaN. Sadly, we can't just compare to factory::nan_value(),
221   // so we do the second best thing - test it ourselves.
222   // They are both equal and they are not both Smis so both of them are not
223   // Smis.  If it's not a heap number, then return equal.
224   if ((cond == lt) || (cond == gt)) {
225     __ JumpIfObjectType(right, scratch, scratch, FIRST_SPEC_OBJECT_TYPE, slow,
226                         ge);
227   } else if (cond == eq) {
228     __ JumpIfHeapNumber(right, &heap_number);
229   } else {
230     Register right_type = scratch;
231     __ JumpIfObjectType(right, right_type, right_type, HEAP_NUMBER_TYPE,
232                         &heap_number);
233     // Comparing JS objects with <=, >= is complicated.
234     __ Cmp(right_type, FIRST_SPEC_OBJECT_TYPE);
235     __ B(ge, slow);
236     // Normally here we fall through to return_equal, but undefined is
237     // special: (undefined == undefined) == true, but
238     // (undefined <= undefined) == false!  See ECMAScript 11.8.5.
239     if ((cond == le) || (cond == ge)) {
240       __ Cmp(right_type, ODDBALL_TYPE);
241       __ B(ne, &return_equal);
242       __ JumpIfNotRoot(right, Heap::kUndefinedValueRootIndex, &return_equal);
243       if (cond == le) {
244         // undefined <= undefined should fail.
245         __ Mov(result, GREATER);
246       } else {
247         // undefined >= undefined should fail.
248         __ Mov(result, LESS);
249       }
250       __ Ret();
251     }
252   }
253
254   __ Bind(&return_equal);
255   if (cond == lt) {
256     __ Mov(result, GREATER);  // Things aren't less than themselves.
257   } else if (cond == gt) {
258     __ Mov(result, LESS);     // Things aren't greater than themselves.
259   } else {
260     __ Mov(result, EQUAL);    // Things are <=, >=, ==, === themselves.
261   }
262   __ Ret();
263
264   // Cases lt and gt have been handled earlier, and case ne is never seen, as
265   // it is handled in the parser (see Parser::ParseBinaryExpression). We are
266   // only concerned with cases ge, le and eq here.
267   if ((cond != lt) && (cond != gt)) {
268     DCHECK((cond == ge) || (cond == le) || (cond == eq));
269     __ Bind(&heap_number);
270     // Left and right are identical pointers to a heap number object. Return
271     // non-equal if the heap number is a NaN, and equal otherwise. Comparing
272     // the number to itself will set the overflow flag iff the number is NaN.
273     __ Ldr(double_scratch, FieldMemOperand(right, HeapNumber::kValueOffset));
274     __ Fcmp(double_scratch, double_scratch);
275     __ B(vc, &return_equal);  // Not NaN, so treat as normal heap number.
276
277     if (cond == le) {
278       __ Mov(result, GREATER);
279     } else {
280       __ Mov(result, LESS);
281     }
282     __ Ret();
283   }
284
285   // No fall through here.
286   if (FLAG_debug_code) {
287     __ Unreachable();
288   }
289
290   __ Bind(&not_identical);
291 }
292
293
294 // See call site for description.
295 static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
296                                            Register left,
297                                            Register right,
298                                            Register left_type,
299                                            Register right_type,
300                                            Register scratch) {
301   DCHECK(!AreAliased(left, right, left_type, right_type, scratch));
302
303   if (masm->emit_debug_code()) {
304     // We assume that the arguments are not identical.
305     __ Cmp(left, right);
306     __ Assert(ne, kExpectedNonIdenticalObjects);
307   }
308
309   // If either operand is a JS object or an oddball value, then they are not
310   // equal since their pointers are different.
311   // There is no test for undetectability in strict equality.
312   STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
313   Label right_non_object;
314
315   __ Cmp(right_type, FIRST_SPEC_OBJECT_TYPE);
316   __ B(lt, &right_non_object);
317
318   // Return non-zero - x0 already contains a non-zero pointer.
319   DCHECK(left.is(x0) || right.is(x0));
320   Label return_not_equal;
321   __ Bind(&return_not_equal);
322   __ Ret();
323
324   __ Bind(&right_non_object);
325
326   // Check for oddballs: true, false, null, undefined.
327   __ Cmp(right_type, ODDBALL_TYPE);
328
329   // If right is not ODDBALL, test left. Otherwise, set eq condition.
330   __ Ccmp(left_type, ODDBALL_TYPE, ZFlag, ne);
331
332   // If right or left is not ODDBALL, test left >= FIRST_SPEC_OBJECT_TYPE.
333   // Otherwise, right or left is ODDBALL, so set a ge condition.
334   __ Ccmp(left_type, FIRST_SPEC_OBJECT_TYPE, NVFlag, ne);
335
336   __ B(ge, &return_not_equal);
337
338   // Internalized strings are unique, so they can only be equal if they are the
339   // same object. We have already tested that case, so if left and right are
340   // both internalized strings, they cannot be equal.
341   STATIC_ASSERT((kInternalizedTag == 0) && (kStringTag == 0));
342   __ Orr(scratch, left_type, right_type);
343   __ TestAndBranchIfAllClear(
344       scratch, kIsNotStringMask | kIsNotInternalizedMask, &return_not_equal);
345 }
346
347
348 // See call site for description.
349 static void EmitSmiNonsmiComparison(MacroAssembler* masm,
350                                     Register left,
351                                     Register right,
352                                     FPRegister left_d,
353                                     FPRegister right_d,
354                                     Label* slow,
355                                     bool strict) {
356   DCHECK(!AreAliased(left_d, right_d));
357   DCHECK((left.is(x0) && right.is(x1)) ||
358          (right.is(x0) && left.is(x1)));
359   Register result = x0;
360
361   Label right_is_smi, done;
362   __ JumpIfSmi(right, &right_is_smi);
363
364   // Left is the smi. Check whether right is a heap number.
365   if (strict) {
366     // If right is not a number and left is a smi, then strict equality cannot
367     // succeed. Return non-equal.
368     Label is_heap_number;
369     __ JumpIfHeapNumber(right, &is_heap_number);
370     // Register right is a non-zero pointer, which is a valid NOT_EQUAL result.
371     if (!right.is(result)) {
372       __ Mov(result, NOT_EQUAL);
373     }
374     __ Ret();
375     __ Bind(&is_heap_number);
376   } else {
377     // Smi compared non-strictly with a non-smi, non-heap-number. Call the
378     // runtime.
379     __ JumpIfNotHeapNumber(right, slow);
380   }
381
382   // Left is the smi. Right is a heap number. Load right value into right_d, and
383   // convert left smi into double in left_d.
384   __ Ldr(right_d, FieldMemOperand(right, HeapNumber::kValueOffset));
385   __ SmiUntagToDouble(left_d, left);
386   __ B(&done);
387
388   __ Bind(&right_is_smi);
389   // Right is a smi. Check whether the non-smi left is a heap number.
390   if (strict) {
391     // If left is not a number and right is a smi then strict equality cannot
392     // succeed. Return non-equal.
393     Label is_heap_number;
394     __ JumpIfHeapNumber(left, &is_heap_number);
395     // Register left is a non-zero pointer, which is a valid NOT_EQUAL result.
396     if (!left.is(result)) {
397       __ Mov(result, NOT_EQUAL);
398     }
399     __ Ret();
400     __ Bind(&is_heap_number);
401   } else {
402     // Smi compared non-strictly with a non-smi, non-heap-number. Call the
403     // runtime.
404     __ JumpIfNotHeapNumber(left, slow);
405   }
406
407   // Right is the smi. Left is a heap number. Load left value into left_d, and
408   // convert right smi into double in right_d.
409   __ Ldr(left_d, FieldMemOperand(left, HeapNumber::kValueOffset));
410   __ SmiUntagToDouble(right_d, right);
411
412   // Fall through to both_loaded_as_doubles.
413   __ Bind(&done);
414 }
415
416
417 // Fast negative check for internalized-to-internalized equality.
418 // See call site for description.
419 static void EmitCheckForInternalizedStringsOrObjects(MacroAssembler* masm,
420                                                      Register left,
421                                                      Register right,
422                                                      Register left_map,
423                                                      Register right_map,
424                                                      Register left_type,
425                                                      Register right_type,
426                                                      Label* possible_strings,
427                                                      Label* not_both_strings) {
428   DCHECK(!AreAliased(left, right, left_map, right_map, left_type, right_type));
429   Register result = x0;
430
431   Label object_test;
432   STATIC_ASSERT((kInternalizedTag == 0) && (kStringTag == 0));
433   // TODO(all): reexamine this branch sequence for optimisation wrt branch
434   // prediction.
435   __ Tbnz(right_type, MaskToBit(kIsNotStringMask), &object_test);
436   __ Tbnz(right_type, MaskToBit(kIsNotInternalizedMask), possible_strings);
437   __ Tbnz(left_type, MaskToBit(kIsNotStringMask), not_both_strings);
438   __ Tbnz(left_type, MaskToBit(kIsNotInternalizedMask), possible_strings);
439
440   // Both are internalized. We already checked that they weren't the same
441   // pointer, so they are not equal.
442   __ Mov(result, NOT_EQUAL);
443   __ Ret();
444
445   __ Bind(&object_test);
446
447   __ Cmp(right_type, FIRST_SPEC_OBJECT_TYPE);
448
449   // If right >= FIRST_SPEC_OBJECT_TYPE, test left.
450   // Otherwise, right < FIRST_SPEC_OBJECT_TYPE, so set lt condition.
451   __ Ccmp(left_type, FIRST_SPEC_OBJECT_TYPE, NFlag, ge);
452
453   __ B(lt, not_both_strings);
454
455   // If both objects are undetectable, they are equal. Otherwise, they are not
456   // equal, since they are different objects and an object is not equal to
457   // undefined.
458
459   // Returning here, so we can corrupt right_type and left_type.
460   Register right_bitfield = right_type;
461   Register left_bitfield = left_type;
462   __ Ldrb(right_bitfield, FieldMemOperand(right_map, Map::kBitFieldOffset));
463   __ Ldrb(left_bitfield, FieldMemOperand(left_map, Map::kBitFieldOffset));
464   __ And(result, right_bitfield, left_bitfield);
465   __ And(result, result, 1 << Map::kIsUndetectable);
466   __ Eor(result, result, 1 << Map::kIsUndetectable);
467   __ Ret();
468 }
469
470
471 static void CompareICStub_CheckInputType(MacroAssembler* masm, Register input,
472                                          CompareICState::State expected,
473                                          Label* fail) {
474   Label ok;
475   if (expected == CompareICState::SMI) {
476     __ JumpIfNotSmi(input, fail);
477   } else if (expected == CompareICState::NUMBER) {
478     __ JumpIfSmi(input, &ok);
479     __ JumpIfNotHeapNumber(input, fail);
480   }
481   // We could be strict about internalized/non-internalized here, but as long as
482   // hydrogen doesn't care, the stub doesn't have to care either.
483   __ Bind(&ok);
484 }
485
486
487 void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
488   Register lhs = x1;
489   Register rhs = x0;
490   Register result = x0;
491   Condition cond = GetCondition();
492
493   Label miss;
494   CompareICStub_CheckInputType(masm, lhs, left(), &miss);
495   CompareICStub_CheckInputType(masm, rhs, right(), &miss);
496
497   Label slow;  // Call builtin.
498   Label not_smis, both_loaded_as_doubles;
499   Label not_two_smis, smi_done;
500   __ JumpIfEitherNotSmi(lhs, rhs, &not_two_smis);
501   __ SmiUntag(lhs);
502   __ Sub(result, lhs, Operand::UntagSmi(rhs));
503   __ Ret();
504
505   __ Bind(&not_two_smis);
506
507   // NOTICE! This code is only reached after a smi-fast-case check, so it is
508   // certain that at least one operand isn't a smi.
509
510   // Handle the case where the objects are identical. Either returns the answer
511   // or goes to slow. Only falls through if the objects were not identical.
512   EmitIdenticalObjectComparison(masm, lhs, rhs, x10, d0, &slow, cond);
513
514   // If either is a smi (we know that at least one is not a smi), then they can
515   // only be strictly equal if the other is a HeapNumber.
516   __ JumpIfBothNotSmi(lhs, rhs, &not_smis);
517
518   // Exactly one operand is a smi. EmitSmiNonsmiComparison generates code that
519   // can:
520   //  1) Return the answer.
521   //  2) Branch to the slow case.
522   //  3) Fall through to both_loaded_as_doubles.
523   // In case 3, we have found out that we were dealing with a number-number
524   // comparison. The double values of the numbers have been loaded, right into
525   // rhs_d, left into lhs_d.
526   FPRegister rhs_d = d0;
527   FPRegister lhs_d = d1;
528   EmitSmiNonsmiComparison(masm, lhs, rhs, lhs_d, rhs_d, &slow, strict());
529
530   __ Bind(&both_loaded_as_doubles);
531   // The arguments have been converted to doubles and stored in rhs_d and
532   // lhs_d.
533   Label nan;
534   __ Fcmp(lhs_d, rhs_d);
535   __ B(vs, &nan);  // Overflow flag set if either is NaN.
536   STATIC_ASSERT((LESS == -1) && (EQUAL == 0) && (GREATER == 1));
537   __ Cset(result, gt);  // gt => 1, otherwise (lt, eq) => 0 (EQUAL).
538   __ Csinv(result, result, xzr, ge);  // lt => -1, gt => 1, eq => 0.
539   __ Ret();
540
541   __ Bind(&nan);
542   // Left and/or right is a NaN. Load the result register with whatever makes
543   // the comparison fail, since comparisons with NaN always fail (except ne,
544   // which is filtered out at a higher level.)
545   DCHECK(cond != ne);
546   if ((cond == lt) || (cond == le)) {
547     __ Mov(result, GREATER);
548   } else {
549     __ Mov(result, LESS);
550   }
551   __ Ret();
552
553   __ Bind(&not_smis);
554   // At this point we know we are dealing with two different objects, and
555   // neither of them is a smi. The objects are in rhs_ and lhs_.
556
557   // Load the maps and types of the objects.
558   Register rhs_map = x10;
559   Register rhs_type = x11;
560   Register lhs_map = x12;
561   Register lhs_type = x13;
562   __ Ldr(rhs_map, FieldMemOperand(rhs, HeapObject::kMapOffset));
563   __ Ldr(lhs_map, FieldMemOperand(lhs, HeapObject::kMapOffset));
564   __ Ldrb(rhs_type, FieldMemOperand(rhs_map, Map::kInstanceTypeOffset));
565   __ Ldrb(lhs_type, FieldMemOperand(lhs_map, Map::kInstanceTypeOffset));
566
567   if (strict()) {
568     // This emits a non-equal return sequence for some object types, or falls
569     // through if it was not lucky.
570     EmitStrictTwoHeapObjectCompare(masm, lhs, rhs, lhs_type, rhs_type, x14);
571   }
572
573   Label check_for_internalized_strings;
574   Label flat_string_check;
575   // Check for heap number comparison. Branch to earlier double comparison code
576   // if they are heap numbers, otherwise, branch to internalized string check.
577   __ Cmp(rhs_type, HEAP_NUMBER_TYPE);
578   __ B(ne, &check_for_internalized_strings);
579   __ Cmp(lhs_map, rhs_map);
580
581   // If maps aren't equal, lhs_ and rhs_ are not heap numbers. Branch to flat
582   // string check.
583   __ B(ne, &flat_string_check);
584
585   // Both lhs_ and rhs_ are heap numbers. Load them and branch to the double
586   // comparison code.
587   __ Ldr(lhs_d, FieldMemOperand(lhs, HeapNumber::kValueOffset));
588   __ Ldr(rhs_d, FieldMemOperand(rhs, HeapNumber::kValueOffset));
589   __ B(&both_loaded_as_doubles);
590
591   __ Bind(&check_for_internalized_strings);
592   // In the strict case, the EmitStrictTwoHeapObjectCompare already took care
593   // of internalized strings.
594   if ((cond == eq) && !strict()) {
595     // Returns an answer for two internalized strings or two detectable objects.
596     // Otherwise branches to the string case or not both strings case.
597     EmitCheckForInternalizedStringsOrObjects(masm, lhs, rhs, lhs_map, rhs_map,
598                                              lhs_type, rhs_type,
599                                              &flat_string_check, &slow);
600   }
601
602   // Check for both being sequential one-byte strings,
603   // and inline if that is the case.
604   __ Bind(&flat_string_check);
605   __ JumpIfBothInstanceTypesAreNotSequentialOneByte(lhs_type, rhs_type, x14,
606                                                     x15, &slow);
607
608   __ IncrementCounter(isolate()->counters()->string_compare_native(), 1, x10,
609                       x11);
610   if (cond == eq) {
611     StringHelper::GenerateFlatOneByteStringEquals(masm, lhs, rhs, x10, x11,
612                                                   x12);
613   } else {
614     StringHelper::GenerateCompareFlatOneByteStrings(masm, lhs, rhs, x10, x11,
615                                                     x12, x13);
616   }
617
618   // Never fall through to here.
619   if (FLAG_debug_code) {
620     __ Unreachable();
621   }
622
623   __ Bind(&slow);
624
625   __ Push(lhs, rhs);
626   // Figure out which native to call and setup the arguments.
627   Builtins::JavaScript native;
628   if (cond == eq) {
629     native = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
630   } else {
631     native = Builtins::COMPARE;
632     int ncr;  // NaN compare result
633     if ((cond == lt) || (cond == le)) {
634       ncr = GREATER;
635     } else {
636       DCHECK((cond == gt) || (cond == ge));  // remaining cases
637       ncr = LESS;
638     }
639     __ Mov(x10, Smi::FromInt(ncr));
640     __ Push(x10);
641   }
642
643   // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
644   // tagged as a small integer.
645   __ InvokeBuiltin(native, JUMP_FUNCTION);
646
647   __ Bind(&miss);
648   GenerateMiss(masm);
649 }
650
651
652 void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
653   CPURegList saved_regs = kCallerSaved;
654   CPURegList saved_fp_regs = kCallerSavedFP;
655
656   // We don't allow a GC during a store buffer overflow so there is no need to
657   // store the registers in any particular way, but we do have to store and
658   // restore them.
659
660   // We don't care if MacroAssembler scratch registers are corrupted.
661   saved_regs.Remove(*(masm->TmpList()));
662   saved_fp_regs.Remove(*(masm->FPTmpList()));
663
664   __ PushCPURegList(saved_regs);
665   if (save_doubles()) {
666     __ PushCPURegList(saved_fp_regs);
667   }
668
669   AllowExternalCallThatCantCauseGC scope(masm);
670   __ Mov(x0, ExternalReference::isolate_address(isolate()));
671   __ CallCFunction(
672       ExternalReference::store_buffer_overflow_function(isolate()), 1, 0);
673
674   if (save_doubles()) {
675     __ PopCPURegList(saved_fp_regs);
676   }
677   __ PopCPURegList(saved_regs);
678   __ Ret();
679 }
680
681
682 void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
683     Isolate* isolate) {
684   StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
685   stub1.GetCode();
686   StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
687   stub2.GetCode();
688 }
689
690
691 void StoreRegistersStateStub::Generate(MacroAssembler* masm) {
692   MacroAssembler::NoUseRealAbortsScope no_use_real_aborts(masm);
693   UseScratchRegisterScope temps(masm);
694   Register saved_lr = temps.UnsafeAcquire(to_be_pushed_lr());
695   Register return_address = temps.AcquireX();
696   __ Mov(return_address, lr);
697   // Restore lr with the value it had before the call to this stub (the value
698   // which must be pushed).
699   __ Mov(lr, saved_lr);
700   __ PushSafepointRegisters();
701   __ Ret(return_address);
702 }
703
704
705 void RestoreRegistersStateStub::Generate(MacroAssembler* masm) {
706   MacroAssembler::NoUseRealAbortsScope no_use_real_aborts(masm);
707   UseScratchRegisterScope temps(masm);
708   Register return_address = temps.AcquireX();
709   // Preserve the return address (lr will be clobbered by the pop).
710   __ Mov(return_address, lr);
711   __ PopSafepointRegisters();
712   __ Ret(return_address);
713 }
714
715
716 void MathPowStub::Generate(MacroAssembler* masm) {
717   // Stack on entry:
718   // jssp[0]: Exponent (as a tagged value).
719   // jssp[1]: Base (as a tagged value).
720   //
721   // The (tagged) result will be returned in x0, as a heap number.
722
723   Register result_tagged = x0;
724   Register base_tagged = x10;
725   Register exponent_tagged = MathPowTaggedDescriptor::exponent();
726   DCHECK(exponent_tagged.is(x11));
727   Register exponent_integer = MathPowIntegerDescriptor::exponent();
728   DCHECK(exponent_integer.is(x12));
729   Register scratch1 = x14;
730   Register scratch0 = x15;
731   Register saved_lr = x19;
732   FPRegister result_double = d0;
733   FPRegister base_double = d0;
734   FPRegister exponent_double = d1;
735   FPRegister base_double_copy = d2;
736   FPRegister scratch1_double = d6;
737   FPRegister scratch0_double = d7;
738
739   // A fast-path for integer exponents.
740   Label exponent_is_smi, exponent_is_integer;
741   // Bail out to runtime.
742   Label call_runtime;
743   // Allocate a heap number for the result, and return it.
744   Label done;
745
746   // Unpack the inputs.
747   if (exponent_type() == ON_STACK) {
748     Label base_is_smi;
749     Label unpack_exponent;
750
751     __ Pop(exponent_tagged, base_tagged);
752
753     __ JumpIfSmi(base_tagged, &base_is_smi);
754     __ JumpIfNotHeapNumber(base_tagged, &call_runtime);
755     // base_tagged is a heap number, so load its double value.
756     __ Ldr(base_double, FieldMemOperand(base_tagged, HeapNumber::kValueOffset));
757     __ B(&unpack_exponent);
758     __ Bind(&base_is_smi);
759     // base_tagged is a SMI, so untag it and convert it to a double.
760     __ SmiUntagToDouble(base_double, base_tagged);
761
762     __ Bind(&unpack_exponent);
763     //  x10   base_tagged       The tagged base (input).
764     //  x11   exponent_tagged   The tagged exponent (input).
765     //  d1    base_double       The base as a double.
766     __ JumpIfSmi(exponent_tagged, &exponent_is_smi);
767     __ JumpIfNotHeapNumber(exponent_tagged, &call_runtime);
768     // exponent_tagged is a heap number, so load its double value.
769     __ Ldr(exponent_double,
770            FieldMemOperand(exponent_tagged, HeapNumber::kValueOffset));
771   } else if (exponent_type() == TAGGED) {
772     __ JumpIfSmi(exponent_tagged, &exponent_is_smi);
773     __ Ldr(exponent_double,
774            FieldMemOperand(exponent_tagged, HeapNumber::kValueOffset));
775   }
776
777   // Handle double (heap number) exponents.
778   if (exponent_type() != INTEGER) {
779     // Detect integer exponents stored as doubles and handle those in the
780     // integer fast-path.
781     __ TryRepresentDoubleAsInt64(exponent_integer, exponent_double,
782                                  scratch0_double, &exponent_is_integer);
783
784     if (exponent_type() == ON_STACK) {
785       FPRegister  half_double = d3;
786       FPRegister  minus_half_double = d4;
787       // Detect square root case. Crankshaft detects constant +/-0.5 at compile
788       // time and uses DoMathPowHalf instead. We then skip this check for
789       // non-constant cases of +/-0.5 as these hardly occur.
790
791       __ Fmov(minus_half_double, -0.5);
792       __ Fmov(half_double, 0.5);
793       __ Fcmp(minus_half_double, exponent_double);
794       __ Fccmp(half_double, exponent_double, NZFlag, ne);
795       // Condition flags at this point:
796       //    0.5;  nZCv    // Identified by eq && pl
797       //   -0.5:  NZcv    // Identified by eq && mi
798       //  other:  ?z??    // Identified by ne
799       __ B(ne, &call_runtime);
800
801       // The exponent is 0.5 or -0.5.
802
803       // Given that exponent is known to be either 0.5 or -0.5, the following
804       // special cases could apply (according to ECMA-262 15.8.2.13):
805       //
806       //  base.isNaN():                   The result is NaN.
807       //  (base == +INFINITY) || (base == -INFINITY)
808       //    exponent == 0.5:              The result is +INFINITY.
809       //    exponent == -0.5:             The result is +0.
810       //  (base == +0) || (base == -0)
811       //    exponent == 0.5:              The result is +0.
812       //    exponent == -0.5:             The result is +INFINITY.
813       //  (base < 0) && base.isFinite():  The result is NaN.
814       //
815       // Fsqrt (and Fdiv for the -0.5 case) can handle all of those except
816       // where base is -INFINITY or -0.
817
818       // Add +0 to base. This has no effect other than turning -0 into +0.
819       __ Fadd(base_double, base_double, fp_zero);
820       // The operation -0+0 results in +0 in all cases except where the
821       // FPCR rounding mode is 'round towards minus infinity' (RM). The
822       // ARM64 simulator does not currently simulate FPCR (where the rounding
823       // mode is set), so test the operation with some debug code.
824       if (masm->emit_debug_code()) {
825         UseScratchRegisterScope temps(masm);
826         Register temp = temps.AcquireX();
827         __ Fneg(scratch0_double, fp_zero);
828         // Verify that we correctly generated +0.0 and -0.0.
829         //  bits(+0.0) = 0x0000000000000000
830         //  bits(-0.0) = 0x8000000000000000
831         __ Fmov(temp, fp_zero);
832         __ CheckRegisterIsClear(temp, kCouldNotGenerateZero);
833         __ Fmov(temp, scratch0_double);
834         __ Eor(temp, temp, kDSignMask);
835         __ CheckRegisterIsClear(temp, kCouldNotGenerateNegativeZero);
836         // Check that -0.0 + 0.0 == +0.0.
837         __ Fadd(scratch0_double, scratch0_double, fp_zero);
838         __ Fmov(temp, scratch0_double);
839         __ CheckRegisterIsClear(temp, kExpectedPositiveZero);
840       }
841
842       // If base is -INFINITY, make it +INFINITY.
843       //  * Calculate base - base: All infinities will become NaNs since both
844       //    -INFINITY+INFINITY and +INFINITY-INFINITY are NaN in ARM64.
845       //  * If the result is NaN, calculate abs(base).
846       __ Fsub(scratch0_double, base_double, base_double);
847       __ Fcmp(scratch0_double, 0.0);
848       __ Fabs(scratch1_double, base_double);
849       __ Fcsel(base_double, scratch1_double, base_double, vs);
850
851       // Calculate the square root of base.
852       __ Fsqrt(result_double, base_double);
853       __ Fcmp(exponent_double, 0.0);
854       __ B(ge, &done);  // Finish now for exponents of 0.5.
855       // Find the inverse for exponents of -0.5.
856       __ Fmov(scratch0_double, 1.0);
857       __ Fdiv(result_double, scratch0_double, result_double);
858       __ B(&done);
859     }
860
861     {
862       AllowExternalCallThatCantCauseGC scope(masm);
863       __ Mov(saved_lr, lr);
864       __ CallCFunction(
865           ExternalReference::power_double_double_function(isolate()),
866           0, 2);
867       __ Mov(lr, saved_lr);
868       __ B(&done);
869     }
870
871     // Handle SMI exponents.
872     __ Bind(&exponent_is_smi);
873     //  x10   base_tagged       The tagged base (input).
874     //  x11   exponent_tagged   The tagged exponent (input).
875     //  d1    base_double       The base as a double.
876     __ SmiUntag(exponent_integer, exponent_tagged);
877   }
878
879   __ Bind(&exponent_is_integer);
880   //  x10   base_tagged       The tagged base (input).
881   //  x11   exponent_tagged   The tagged exponent (input).
882   //  x12   exponent_integer  The exponent as an integer.
883   //  d1    base_double       The base as a double.
884
885   // Find abs(exponent). For negative exponents, we can find the inverse later.
886   Register exponent_abs = x13;
887   __ Cmp(exponent_integer, 0);
888   __ Cneg(exponent_abs, exponent_integer, mi);
889   //  x13   exponent_abs      The value of abs(exponent_integer).
890
891   // Repeatedly multiply to calculate the power.
892   //  result = 1.0;
893   //  For each bit n (exponent_integer{n}) {
894   //    if (exponent_integer{n}) {
895   //      result *= base;
896   //    }
897   //    base *= base;
898   //    if (remaining bits in exponent_integer are all zero) {
899   //      break;
900   //    }
901   //  }
902   Label power_loop, power_loop_entry, power_loop_exit;
903   __ Fmov(scratch1_double, base_double);
904   __ Fmov(base_double_copy, base_double);
905   __ Fmov(result_double, 1.0);
906   __ B(&power_loop_entry);
907
908   __ Bind(&power_loop);
909   __ Fmul(scratch1_double, scratch1_double, scratch1_double);
910   __ Lsr(exponent_abs, exponent_abs, 1);
911   __ Cbz(exponent_abs, &power_loop_exit);
912
913   __ Bind(&power_loop_entry);
914   __ Tbz(exponent_abs, 0, &power_loop);
915   __ Fmul(result_double, result_double, scratch1_double);
916   __ B(&power_loop);
917
918   __ Bind(&power_loop_exit);
919
920   // If the exponent was positive, result_double holds the result.
921   __ Tbz(exponent_integer, kXSignBit, &done);
922
923   // The exponent was negative, so find the inverse.
924   __ Fmov(scratch0_double, 1.0);
925   __ Fdiv(result_double, scratch0_double, result_double);
926   // ECMA-262 only requires Math.pow to return an 'implementation-dependent
927   // approximation' of base^exponent. However, mjsunit/math-pow uses Math.pow
928   // to calculate the subnormal value 2^-1074. This method of calculating
929   // negative powers doesn't work because 2^1074 overflows to infinity. To
930   // catch this corner-case, we bail out if the result was 0. (This can only
931   // occur if the divisor is infinity or the base is zero.)
932   __ Fcmp(result_double, 0.0);
933   __ B(&done, ne);
934
935   if (exponent_type() == ON_STACK) {
936     // Bail out to runtime code.
937     __ Bind(&call_runtime);
938     // Put the arguments back on the stack.
939     __ Push(base_tagged, exponent_tagged);
940     __ TailCallRuntime(Runtime::kMathPowRT, 2, 1);
941
942     // Return.
943     __ Bind(&done);
944     __ AllocateHeapNumber(result_tagged, &call_runtime, scratch0, scratch1,
945                           result_double);
946     DCHECK(result_tagged.is(x0));
947     __ IncrementCounter(
948         isolate()->counters()->math_pow(), 1, scratch0, scratch1);
949     __ Ret();
950   } else {
951     AllowExternalCallThatCantCauseGC scope(masm);
952     __ Mov(saved_lr, lr);
953     __ Fmov(base_double, base_double_copy);
954     __ Scvtf(exponent_double, exponent_integer);
955     __ CallCFunction(
956         ExternalReference::power_double_double_function(isolate()),
957         0, 2);
958     __ Mov(lr, saved_lr);
959     __ Bind(&done);
960     __ IncrementCounter(
961         isolate()->counters()->math_pow(), 1, scratch0, scratch1);
962     __ Ret();
963   }
964 }
965
966
967 void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
968   // It is important that the following stubs are generated in this order
969   // because pregenerated stubs can only call other pregenerated stubs.
970   // RecordWriteStub uses StoreBufferOverflowStub, which in turn uses
971   // CEntryStub.
972   CEntryStub::GenerateAheadOfTime(isolate);
973   StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
974   StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
975   ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
976   CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
977   CreateWeakCellStub::GenerateAheadOfTime(isolate);
978   BinaryOpICStub::GenerateAheadOfTime(isolate);
979   StoreRegistersStateStub::GenerateAheadOfTime(isolate);
980   RestoreRegistersStateStub::GenerateAheadOfTime(isolate);
981   BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
982 }
983
984
985 void StoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
986   StoreRegistersStateStub stub(isolate);
987   stub.GetCode();
988 }
989
990
991 void RestoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
992   RestoreRegistersStateStub stub(isolate);
993   stub.GetCode();
994 }
995
996
997 void CodeStub::GenerateFPStubs(Isolate* isolate) {
998   // Floating-point code doesn't get special handling in ARM64, so there's
999   // nothing to do here.
1000   USE(isolate);
1001 }
1002
1003
1004 bool CEntryStub::NeedsImmovableCode() {
1005   // CEntryStub stores the return address on the stack before calling into
1006   // C++ code. In some cases, the VM accesses this address, but it is not used
1007   // when the C++ code returns to the stub because LR holds the return address
1008   // in AAPCS64. If the stub is moved (perhaps during a GC), we could end up
1009   // returning to dead code.
1010   // TODO(jbramley): Whilst this is the only analysis that makes sense, I can't
1011   // find any comment to confirm this, and I don't hit any crashes whatever
1012   // this function returns. The anaylsis should be properly confirmed.
1013   return true;
1014 }
1015
1016
1017 void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1018   CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1019   stub.GetCode();
1020   CEntryStub stub_fp(isolate, 1, kSaveFPRegs);
1021   stub_fp.GetCode();
1022 }
1023
1024
1025 void CEntryStub::Generate(MacroAssembler* masm) {
1026   // The Abort mechanism relies on CallRuntime, which in turn relies on
1027   // CEntryStub, so until this stub has been generated, we have to use a
1028   // fall-back Abort mechanism.
1029   //
1030   // Note that this stub must be generated before any use of Abort.
1031   MacroAssembler::NoUseRealAbortsScope no_use_real_aborts(masm);
1032
1033   ASM_LOCATION("CEntryStub::Generate entry");
1034   ProfileEntryHookStub::MaybeCallEntryHook(masm);
1035
1036   // Register parameters:
1037   //    x0: argc (including receiver, untagged)
1038   //    x1: target
1039   //
1040   // The stack on entry holds the arguments and the receiver, with the receiver
1041   // at the highest address:
1042   //
1043   //    jssp]argc-1]: receiver
1044   //    jssp[argc-2]: arg[argc-2]
1045   //    ...           ...
1046   //    jssp[1]:      arg[1]
1047   //    jssp[0]:      arg[0]
1048   //
1049   // The arguments are in reverse order, so that arg[argc-2] is actually the
1050   // first argument to the target function and arg[0] is the last.
1051   DCHECK(jssp.Is(__ StackPointer()));
1052   const Register& argc_input = x0;
1053   const Register& target_input = x1;
1054
1055   // Calculate argv, argc and the target address, and store them in
1056   // callee-saved registers so we can retry the call without having to reload
1057   // these arguments.
1058   // TODO(jbramley): If the first call attempt succeeds in the common case (as
1059   // it should), then we might be better off putting these parameters directly
1060   // into their argument registers, rather than using callee-saved registers and
1061   // preserving them on the stack.
1062   const Register& argv = x21;
1063   const Register& argc = x22;
1064   const Register& target = x23;
1065
1066   // Derive argv from the stack pointer so that it points to the first argument
1067   // (arg[argc-2]), or just below the receiver in case there are no arguments.
1068   //  - Adjust for the arg[] array.
1069   Register temp_argv = x11;
1070   __ Add(temp_argv, jssp, Operand(x0, LSL, kPointerSizeLog2));
1071   //  - Adjust for the receiver.
1072   __ Sub(temp_argv, temp_argv, 1 * kPointerSize);
1073
1074   // Enter the exit frame. Reserve three slots to preserve x21-x23 callee-saved
1075   // registers.
1076   FrameScope scope(masm, StackFrame::MANUAL);
1077   __ EnterExitFrame(save_doubles(), x10, 3);
1078   DCHECK(csp.Is(__ StackPointer()));
1079
1080   // Poke callee-saved registers into reserved space.
1081   __ Poke(argv, 1 * kPointerSize);
1082   __ Poke(argc, 2 * kPointerSize);
1083   __ Poke(target, 3 * kPointerSize);
1084
1085   // We normally only keep tagged values in callee-saved registers, as they
1086   // could be pushed onto the stack by called stubs and functions, and on the
1087   // stack they can confuse the GC. However, we're only calling C functions
1088   // which can push arbitrary data onto the stack anyway, and so the GC won't
1089   // examine that part of the stack.
1090   __ Mov(argc, argc_input);
1091   __ Mov(target, target_input);
1092   __ Mov(argv, temp_argv);
1093
1094   // x21 : argv
1095   // x22 : argc
1096   // x23 : call target
1097   //
1098   // The stack (on entry) holds the arguments and the receiver, with the
1099   // receiver at the highest address:
1100   //
1101   //         argv[8]:     receiver
1102   // argv -> argv[0]:     arg[argc-2]
1103   //         ...          ...
1104   //         argv[...]:   arg[1]
1105   //         argv[...]:   arg[0]
1106   //
1107   // Immediately below (after) this is the exit frame, as constructed by
1108   // EnterExitFrame:
1109   //         fp[8]:    CallerPC (lr)
1110   //   fp -> fp[0]:    CallerFP (old fp)
1111   //         fp[-8]:   Space reserved for SPOffset.
1112   //         fp[-16]:  CodeObject()
1113   //         csp[...]: Saved doubles, if saved_doubles is true.
1114   //         csp[32]:  Alignment padding, if necessary.
1115   //         csp[24]:  Preserved x23 (used for target).
1116   //         csp[16]:  Preserved x22 (used for argc).
1117   //         csp[8]:   Preserved x21 (used for argv).
1118   //  csp -> csp[0]:   Space reserved for the return address.
1119   //
1120   // After a successful call, the exit frame, preserved registers (x21-x23) and
1121   // the arguments (including the receiver) are dropped or popped as
1122   // appropriate. The stub then returns.
1123   //
1124   // After an unsuccessful call, the exit frame and suchlike are left
1125   // untouched, and the stub either throws an exception by jumping to one of
1126   // the exception_returned label.
1127
1128   DCHECK(csp.Is(__ StackPointer()));
1129
1130   // Prepare AAPCS64 arguments to pass to the builtin.
1131   __ Mov(x0, argc);
1132   __ Mov(x1, argv);
1133   __ Mov(x2, ExternalReference::isolate_address(isolate()));
1134
1135   Label return_location;
1136   __ Adr(x12, &return_location);
1137   __ Poke(x12, 0);
1138
1139   if (__ emit_debug_code()) {
1140     // Verify that the slot below fp[kSPOffset]-8 points to the return location
1141     // (currently in x12).
1142     UseScratchRegisterScope temps(masm);
1143     Register temp = temps.AcquireX();
1144     __ Ldr(temp, MemOperand(fp, ExitFrameConstants::kSPOffset));
1145     __ Ldr(temp, MemOperand(temp, -static_cast<int64_t>(kXRegSize)));
1146     __ Cmp(temp, x12);
1147     __ Check(eq, kReturnAddressNotFoundInFrame);
1148   }
1149
1150   // Call the builtin.
1151   __ Blr(target);
1152   __ Bind(&return_location);
1153
1154   //  x0    result      The return code from the call.
1155   //  x21   argv
1156   //  x22   argc
1157   //  x23   target
1158   const Register& result = x0;
1159
1160   // Check result for exception sentinel.
1161   Label exception_returned;
1162   __ CompareRoot(result, Heap::kExceptionRootIndex);
1163   __ B(eq, &exception_returned);
1164
1165   // The call succeeded, so unwind the stack and return.
1166
1167   // Restore callee-saved registers x21-x23.
1168   __ Mov(x11, argc);
1169
1170   __ Peek(argv, 1 * kPointerSize);
1171   __ Peek(argc, 2 * kPointerSize);
1172   __ Peek(target, 3 * kPointerSize);
1173
1174   __ LeaveExitFrame(save_doubles(), x10, true);
1175   DCHECK(jssp.Is(__ StackPointer()));
1176   // Pop or drop the remaining stack slots and return from the stub.
1177   //         jssp[24]:    Arguments array (of size argc), including receiver.
1178   //         jssp[16]:    Preserved x23 (used for target).
1179   //         jssp[8]:     Preserved x22 (used for argc).
1180   //         jssp[0]:     Preserved x21 (used for argv).
1181   __ Drop(x11);
1182   __ AssertFPCRState();
1183   __ Ret();
1184
1185   // The stack pointer is still csp if we aren't returning, and the frame
1186   // hasn't changed (except for the return address).
1187   __ SetStackPointer(csp);
1188
1189   // Handling of exception.
1190   __ Bind(&exception_returned);
1191
1192   ExternalReference pending_handler_context_address(
1193       Isolate::kPendingHandlerContextAddress, isolate());
1194   ExternalReference pending_handler_code_address(
1195       Isolate::kPendingHandlerCodeAddress, isolate());
1196   ExternalReference pending_handler_offset_address(
1197       Isolate::kPendingHandlerOffsetAddress, isolate());
1198   ExternalReference pending_handler_fp_address(
1199       Isolate::kPendingHandlerFPAddress, isolate());
1200   ExternalReference pending_handler_sp_address(
1201       Isolate::kPendingHandlerSPAddress, isolate());
1202
1203   // Ask the runtime for help to determine the handler. This will set x0 to
1204   // contain the current pending exception, don't clobber it.
1205   ExternalReference find_handler(Runtime::kFindExceptionHandler, isolate());
1206   DCHECK(csp.Is(masm->StackPointer()));
1207   {
1208     FrameScope scope(masm, StackFrame::MANUAL);
1209     __ Mov(x0, 0);  // argc.
1210     __ Mov(x1, 0);  // argv.
1211     __ Mov(x2, ExternalReference::isolate_address(isolate()));
1212     __ CallCFunction(find_handler, 3);
1213   }
1214
1215   // We didn't execute a return case, so the stack frame hasn't been updated
1216   // (except for the return address slot). However, we don't need to initialize
1217   // jssp because the throw method will immediately overwrite it when it
1218   // unwinds the stack.
1219   __ SetStackPointer(jssp);
1220
1221   // Retrieve the handler context, SP and FP.
1222   __ Mov(cp, Operand(pending_handler_context_address));
1223   __ Ldr(cp, MemOperand(cp));
1224   __ Mov(jssp, Operand(pending_handler_sp_address));
1225   __ Ldr(jssp, MemOperand(jssp));
1226   __ Mov(fp, Operand(pending_handler_fp_address));
1227   __ Ldr(fp, MemOperand(fp));
1228
1229   // If the handler is a JS frame, restore the context to the frame. Note that
1230   // the context will be set to (cp == 0) for non-JS frames.
1231   Label not_js_frame;
1232   __ Cbz(cp, &not_js_frame);
1233   __ Str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1234   __ Bind(&not_js_frame);
1235
1236   // Compute the handler entry address and jump to it.
1237   __ Mov(x10, Operand(pending_handler_code_address));
1238   __ Ldr(x10, MemOperand(x10));
1239   __ Mov(x11, Operand(pending_handler_offset_address));
1240   __ Ldr(x11, MemOperand(x11));
1241   __ Add(x10, x10, Code::kHeaderSize - kHeapObjectTag);
1242   __ Add(x10, x10, x11);
1243   __ Br(x10);
1244 }
1245
1246
1247 // This is the entry point from C++. 5 arguments are provided in x0-x4.
1248 // See use of the CALL_GENERATED_CODE macro for example in src/execution.cc.
1249 // Input:
1250 //   x0: code entry.
1251 //   x1: function.
1252 //   x2: receiver.
1253 //   x3: argc.
1254 //   x4: argv.
1255 // Output:
1256 //   x0: result.
1257 void JSEntryStub::Generate(MacroAssembler* masm) {
1258   DCHECK(jssp.Is(__ StackPointer()));
1259   Register code_entry = x0;
1260
1261   // Enable instruction instrumentation. This only works on the simulator, and
1262   // will have no effect on the model or real hardware.
1263   __ EnableInstrumentation();
1264
1265   Label invoke, handler_entry, exit;
1266
1267   // Push callee-saved registers and synchronize the system stack pointer (csp)
1268   // and the JavaScript stack pointer (jssp).
1269   //
1270   // We must not write to jssp until after the PushCalleeSavedRegisters()
1271   // call, since jssp is itself a callee-saved register.
1272   __ SetStackPointer(csp);
1273   __ PushCalleeSavedRegisters();
1274   __ Mov(jssp, csp);
1275   __ SetStackPointer(jssp);
1276
1277   // Configure the FPCR. We don't restore it, so this is technically not allowed
1278   // according to AAPCS64. However, we only set default-NaN mode and this will
1279   // be harmless for most C code. Also, it works for ARM.
1280   __ ConfigureFPCR();
1281
1282   ProfileEntryHookStub::MaybeCallEntryHook(masm);
1283
1284   // Set up the reserved register for 0.0.
1285   __ Fmov(fp_zero, 0.0);
1286
1287   // Build an entry frame (see layout below).
1288   int marker = type();
1289   int64_t bad_frame_pointer = -1L;  // Bad frame pointer to fail if it is used.
1290   __ Mov(x13, bad_frame_pointer);
1291   __ Mov(x12, Smi::FromInt(marker));
1292   __ Mov(x11, ExternalReference(Isolate::kCEntryFPAddress, isolate()));
1293   __ Ldr(x10, MemOperand(x11));
1294
1295   __ Push(x13, xzr, x12, x10);
1296   // Set up fp.
1297   __ Sub(fp, jssp, EntryFrameConstants::kCallerFPOffset);
1298
1299   // Push the JS entry frame marker. Also set js_entry_sp if this is the
1300   // outermost JS call.
1301   Label non_outermost_js, done;
1302   ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
1303   __ Mov(x10, ExternalReference(js_entry_sp));
1304   __ Ldr(x11, MemOperand(x10));
1305   __ Cbnz(x11, &non_outermost_js);
1306   __ Str(fp, MemOperand(x10));
1307   __ Mov(x12, Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
1308   __ Push(x12);
1309   __ B(&done);
1310   __ Bind(&non_outermost_js);
1311   // We spare one instruction by pushing xzr since the marker is 0.
1312   DCHECK(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME) == NULL);
1313   __ Push(xzr);
1314   __ Bind(&done);
1315
1316   // The frame set up looks like this:
1317   // jssp[0] : JS entry frame marker.
1318   // jssp[1] : C entry FP.
1319   // jssp[2] : stack frame marker.
1320   // jssp[3] : stack frmae marker.
1321   // jssp[4] : bad frame pointer 0xfff...ff   <- fp points here.
1322
1323
1324   // Jump to a faked try block that does the invoke, with a faked catch
1325   // block that sets the pending exception.
1326   __ B(&invoke);
1327
1328   // Prevent the constant pool from being emitted between the record of the
1329   // handler_entry position and the first instruction of the sequence here.
1330   // There is no risk because Assembler::Emit() emits the instruction before
1331   // checking for constant pool emission, but we do not want to depend on
1332   // that.
1333   {
1334     Assembler::BlockPoolsScope block_pools(masm);
1335     __ bind(&handler_entry);
1336     handler_offset_ = handler_entry.pos();
1337     // Caught exception: Store result (exception) in the pending exception
1338     // field in the JSEnv and return a failure sentinel. Coming in here the
1339     // fp will be invalid because the PushTryHandler below sets it to 0 to
1340     // signal the existence of the JSEntry frame.
1341     __ Mov(x10, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
1342                                           isolate())));
1343   }
1344   __ Str(code_entry, MemOperand(x10));
1345   __ LoadRoot(x0, Heap::kExceptionRootIndex);
1346   __ B(&exit);
1347
1348   // Invoke: Link this frame into the handler chain.
1349   __ Bind(&invoke);
1350   __ PushStackHandler();
1351   // If an exception not caught by another handler occurs, this handler
1352   // returns control to the code after the B(&invoke) above, which
1353   // restores all callee-saved registers (including cp and fp) to their
1354   // saved values before returning a failure to C.
1355
1356   // Clear any pending exceptions.
1357   __ Mov(x10, Operand(isolate()->factory()->the_hole_value()));
1358   __ Mov(x11, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
1359                                         isolate())));
1360   __ Str(x10, MemOperand(x11));
1361
1362   // Invoke the function by calling through the JS entry trampoline builtin.
1363   // Notice that we cannot store a reference to the trampoline code directly in
1364   // this stub, because runtime stubs are not traversed when doing GC.
1365
1366   // Expected registers by Builtins::JSEntryTrampoline
1367   // x0: code entry.
1368   // x1: function.
1369   // x2: receiver.
1370   // x3: argc.
1371   // x4: argv.
1372   ExternalReference entry(type() == StackFrame::ENTRY_CONSTRUCT
1373                               ? Builtins::kJSConstructEntryTrampoline
1374                               : Builtins::kJSEntryTrampoline,
1375                           isolate());
1376   __ Mov(x10, entry);
1377
1378   // Call the JSEntryTrampoline.
1379   __ Ldr(x11, MemOperand(x10));  // Dereference the address.
1380   __ Add(x12, x11, Code::kHeaderSize - kHeapObjectTag);
1381   __ Blr(x12);
1382
1383   // Unlink this frame from the handler chain.
1384   __ PopStackHandler();
1385
1386
1387   __ Bind(&exit);
1388   // x0 holds the result.
1389   // The stack pointer points to the top of the entry frame pushed on entry from
1390   // C++ (at the beginning of this stub):
1391   // jssp[0] : JS entry frame marker.
1392   // jssp[1] : C entry FP.
1393   // jssp[2] : stack frame marker.
1394   // jssp[3] : stack frmae marker.
1395   // jssp[4] : bad frame pointer 0xfff...ff   <- fp points here.
1396
1397   // Check if the current stack frame is marked as the outermost JS frame.
1398   Label non_outermost_js_2;
1399   __ Pop(x10);
1400   __ Cmp(x10, Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
1401   __ B(ne, &non_outermost_js_2);
1402   __ Mov(x11, ExternalReference(js_entry_sp));
1403   __ Str(xzr, MemOperand(x11));
1404   __ Bind(&non_outermost_js_2);
1405
1406   // Restore the top frame descriptors from the stack.
1407   __ Pop(x10);
1408   __ Mov(x11, ExternalReference(Isolate::kCEntryFPAddress, isolate()));
1409   __ Str(x10, MemOperand(x11));
1410
1411   // Reset the stack to the callee saved registers.
1412   __ Drop(-EntryFrameConstants::kCallerFPOffset, kByteSizeInBytes);
1413   // Restore the callee-saved registers and return.
1414   DCHECK(jssp.Is(__ StackPointer()));
1415   __ Mov(csp, jssp);
1416   __ SetStackPointer(csp);
1417   __ PopCalleeSavedRegisters();
1418   // After this point, we must not modify jssp because it is a callee-saved
1419   // register which we have just restored.
1420   __ Ret();
1421 }
1422
1423
1424 void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
1425   Label miss;
1426   Register receiver = LoadDescriptor::ReceiverRegister();
1427   // Ensure that the vector and slot registers won't be clobbered before
1428   // calling the miss handler.
1429   DCHECK(!FLAG_vector_ics ||
1430          !AreAliased(x10, x11, VectorLoadICDescriptor::VectorRegister(),
1431                      VectorLoadICDescriptor::SlotRegister()));
1432
1433   NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(masm, receiver, x10,
1434                                                           x11, &miss);
1435
1436   __ Bind(&miss);
1437   PropertyAccessCompiler::TailCallBuiltin(
1438       masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
1439 }
1440
1441
1442 void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
1443   // Return address is in lr.
1444   Label miss;
1445
1446   Register receiver = LoadDescriptor::ReceiverRegister();
1447   Register index = LoadDescriptor::NameRegister();
1448   Register result = x0;
1449   Register scratch = x10;
1450   DCHECK(!scratch.is(receiver) && !scratch.is(index));
1451   DCHECK(!FLAG_vector_ics ||
1452          (!scratch.is(VectorLoadICDescriptor::VectorRegister()) &&
1453           result.is(VectorLoadICDescriptor::SlotRegister())));
1454
1455   // StringCharAtGenerator doesn't use the result register until it's passed
1456   // the different miss possibilities. If it did, we would have a conflict
1457   // when FLAG_vector_ics is true.
1458   StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
1459                                           &miss,  // When not a string.
1460                                           &miss,  // When not a number.
1461                                           &miss,  // When index out of range.
1462                                           STRING_INDEX_IS_ARRAY_INDEX,
1463                                           RECEIVER_IS_STRING);
1464   char_at_generator.GenerateFast(masm);
1465   __ Ret();
1466
1467   StubRuntimeCallHelper call_helper;
1468   char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
1469
1470   __ Bind(&miss);
1471   PropertyAccessCompiler::TailCallBuiltin(
1472       masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
1473 }
1474
1475
1476 void InstanceofStub::Generate(MacroAssembler* masm) {
1477   // Stack on entry:
1478   // jssp[0]: function.
1479   // jssp[8]: object.
1480   //
1481   // Returns result in x0. Zero indicates instanceof, smi 1 indicates not
1482   // instanceof.
1483
1484   Register result = x0;
1485   Register function = right();
1486   Register object = left();
1487   Register scratch1 = x6;
1488   Register scratch2 = x7;
1489   Register res_true = x8;
1490   Register res_false = x9;
1491   // Only used if there was an inline map check site. (See
1492   // LCodeGen::DoInstanceOfKnownGlobal().)
1493   Register map_check_site = x4;
1494   // Delta for the instructions generated between the inline map check and the
1495   // instruction setting the result.
1496   const int32_t kDeltaToLoadBoolResult = 4 * kInstructionSize;
1497
1498   Label not_js_object, slow;
1499
1500   if (!HasArgsInRegisters()) {
1501     __ Pop(function, object);
1502   }
1503
1504   if (ReturnTrueFalseObject()) {
1505     __ LoadTrueFalseRoots(res_true, res_false);
1506   } else {
1507     // This is counter-intuitive, but correct.
1508     __ Mov(res_true, Smi::FromInt(0));
1509     __ Mov(res_false, Smi::FromInt(1));
1510   }
1511
1512   // Check that the left hand side is a JS object and load its map as a side
1513   // effect.
1514   Register map = x12;
1515   __ JumpIfSmi(object, &not_js_object);
1516   __ IsObjectJSObjectType(object, map, scratch2, &not_js_object);
1517
1518   // If there is a call site cache, don't look in the global cache, but do the
1519   // real lookup and update the call site cache.
1520   if (!HasCallSiteInlineCheck() && !ReturnTrueFalseObject()) {
1521     Label miss;
1522     __ JumpIfNotRoot(function, Heap::kInstanceofCacheFunctionRootIndex, &miss);
1523     __ JumpIfNotRoot(map, Heap::kInstanceofCacheMapRootIndex, &miss);
1524     __ LoadRoot(result, Heap::kInstanceofCacheAnswerRootIndex);
1525     __ Ret();
1526     __ Bind(&miss);
1527   }
1528
1529   // Get the prototype of the function.
1530   Register prototype = x13;
1531   __ TryGetFunctionPrototype(function, prototype, scratch2, &slow,
1532                              MacroAssembler::kMissOnBoundFunction);
1533
1534   // Check that the function prototype is a JS object.
1535   __ JumpIfSmi(prototype, &slow);
1536   __ IsObjectJSObjectType(prototype, scratch1, scratch2, &slow);
1537
1538   // Update the global instanceof or call site inlined cache with the current
1539   // map and function. The cached answer will be set when it is known below.
1540   if (HasCallSiteInlineCheck()) {
1541     // Patch the (relocated) inlined map check.
1542     __ GetRelocatedValueLocation(map_check_site, scratch1);
1543     // We have a cell, so need another level of dereferencing.
1544     __ Ldr(scratch1, MemOperand(scratch1));
1545     __ Str(map, FieldMemOperand(scratch1, Cell::kValueOffset));
1546   } else {
1547     __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
1548     __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
1549   }
1550
1551   Label return_true, return_result;
1552   Register smi_value = scratch1;
1553   {
1554     // Loop through the prototype chain looking for the function prototype.
1555     Register chain_map = x1;
1556     Register chain_prototype = x14;
1557     Register null_value = x15;
1558     Label loop;
1559     __ Ldr(chain_prototype, FieldMemOperand(map, Map::kPrototypeOffset));
1560     __ LoadRoot(null_value, Heap::kNullValueRootIndex);
1561     // Speculatively set a result.
1562     __ Mov(result, res_false);
1563     if (!HasCallSiteInlineCheck() && ReturnTrueFalseObject()) {
1564       // Value to store in the cache cannot be an object.
1565       __ Mov(smi_value, Smi::FromInt(1));
1566     }
1567
1568     __ Bind(&loop);
1569
1570     // If the chain prototype is the object prototype, return true.
1571     __ Cmp(chain_prototype, prototype);
1572     __ B(eq, &return_true);
1573
1574     // If the chain prototype is null, we've reached the end of the chain, so
1575     // return false.
1576     __ Cmp(chain_prototype, null_value);
1577     __ B(eq, &return_result);
1578
1579     // Otherwise, load the next prototype in the chain, and loop.
1580     __ Ldr(chain_map, FieldMemOperand(chain_prototype, HeapObject::kMapOffset));
1581     __ Ldr(chain_prototype, FieldMemOperand(chain_map, Map::kPrototypeOffset));
1582     __ B(&loop);
1583   }
1584
1585   // Return sequence when no arguments are on the stack.
1586   // We cannot fall through to here.
1587   __ Bind(&return_true);
1588   __ Mov(result, res_true);
1589   if (!HasCallSiteInlineCheck() && ReturnTrueFalseObject()) {
1590     // Value to store in the cache cannot be an object.
1591     __ Mov(smi_value, Smi::FromInt(0));
1592   }
1593   __ Bind(&return_result);
1594   if (HasCallSiteInlineCheck()) {
1595     DCHECK(ReturnTrueFalseObject());
1596     __ Add(map_check_site, map_check_site, kDeltaToLoadBoolResult);
1597     __ GetRelocatedValueLocation(map_check_site, scratch2);
1598     __ Str(result, MemOperand(scratch2));
1599   } else {
1600     Register cached_value = ReturnTrueFalseObject() ? smi_value : result;
1601     __ StoreRoot(cached_value, Heap::kInstanceofCacheAnswerRootIndex);
1602   }
1603   __ Ret();
1604
1605   Label object_not_null, object_not_null_or_smi;
1606
1607   __ Bind(&not_js_object);
1608   Register object_type = x14;
1609   //   x0   result        result return register (uninit)
1610   //   x10  function      pointer to function
1611   //   x11  object        pointer to object
1612   //   x14  object_type   type of object (uninit)
1613
1614   // Before null, smi and string checks, check that the rhs is a function.
1615   // For a non-function rhs, an exception must be thrown.
1616   __ JumpIfSmi(function, &slow);
1617   __ JumpIfNotObjectType(
1618       function, scratch1, object_type, JS_FUNCTION_TYPE, &slow);
1619
1620   __ Mov(result, res_false);
1621
1622   // Null is not instance of anything.
1623   __ Cmp(object, Operand(isolate()->factory()->null_value()));
1624   __ B(ne, &object_not_null);
1625   __ Ret();
1626
1627   __ Bind(&object_not_null);
1628   // Smi values are not instances of anything.
1629   __ JumpIfNotSmi(object, &object_not_null_or_smi);
1630   __ Ret();
1631
1632   __ Bind(&object_not_null_or_smi);
1633   // String values are not instances of anything.
1634   __ IsObjectJSStringType(object, scratch2, &slow);
1635   __ Ret();
1636
1637   // Slow-case. Tail call builtin.
1638   __ Bind(&slow);
1639   {
1640     FrameScope scope(masm, StackFrame::INTERNAL);
1641     // Arguments have either been passed into registers or have been previously
1642     // popped. We need to push them before calling builtin.
1643     __ Push(object, function);
1644     __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
1645   }
1646   if (ReturnTrueFalseObject()) {
1647     // Reload true/false because they were clobbered in the builtin call.
1648     __ LoadTrueFalseRoots(res_true, res_false);
1649     __ Cmp(result, 0);
1650     __ Csel(result, res_true, res_false, eq);
1651   }
1652   __ Ret();
1653 }
1654
1655
1656 void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
1657   CHECK(!has_new_target());
1658   Register arg_count = ArgumentsAccessReadDescriptor::parameter_count();
1659   Register key = ArgumentsAccessReadDescriptor::index();
1660   DCHECK(arg_count.is(x0));
1661   DCHECK(key.is(x1));
1662
1663   // The displacement is the offset of the last parameter (if any) relative
1664   // to the frame pointer.
1665   static const int kDisplacement =
1666       StandardFrameConstants::kCallerSPOffset - kPointerSize;
1667
1668   // Check that the key is a smi.
1669   Label slow;
1670   __ JumpIfNotSmi(key, &slow);
1671
1672   // Check if the calling frame is an arguments adaptor frame.
1673   Register local_fp = x11;
1674   Register caller_fp = x11;
1675   Register caller_ctx = x12;
1676   Label skip_adaptor;
1677   __ Ldr(caller_fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1678   __ Ldr(caller_ctx, MemOperand(caller_fp,
1679                                 StandardFrameConstants::kContextOffset));
1680   __ Cmp(caller_ctx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
1681   __ Csel(local_fp, fp, caller_fp, ne);
1682   __ B(ne, &skip_adaptor);
1683
1684   // Load the actual arguments limit found in the arguments adaptor frame.
1685   __ Ldr(arg_count, MemOperand(caller_fp,
1686                                ArgumentsAdaptorFrameConstants::kLengthOffset));
1687   __ Bind(&skip_adaptor);
1688
1689   // Check index against formal parameters count limit. Use unsigned comparison
1690   // to get negative check for free: branch if key < 0 or key >= arg_count.
1691   __ Cmp(key, arg_count);
1692   __ B(hs, &slow);
1693
1694   // Read the argument from the stack and return it.
1695   __ Sub(x10, arg_count, key);
1696   __ Add(x10, local_fp, Operand::UntagSmiAndScale(x10, kPointerSizeLog2));
1697   __ Ldr(x0, MemOperand(x10, kDisplacement));
1698   __ Ret();
1699
1700   // Slow case: handle non-smi or out-of-bounds access to arguments by calling
1701   // the runtime system.
1702   __ Bind(&slow);
1703   __ Push(key);
1704   __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
1705 }
1706
1707
1708 void ArgumentsAccessStub::GenerateNewSloppySlow(MacroAssembler* masm) {
1709   // Stack layout on entry.
1710   //  jssp[0]:  number of parameters (tagged)
1711   //  jssp[8]:  address of receiver argument
1712   //  jssp[16]: function
1713
1714   CHECK(!has_new_target());
1715
1716   // Check if the calling frame is an arguments adaptor frame.
1717   Label runtime;
1718   Register caller_fp = x10;
1719   __ Ldr(caller_fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1720   // Load and untag the context.
1721   __ Ldr(w11, UntagSmiMemOperand(caller_fp,
1722                                  StandardFrameConstants::kContextOffset));
1723   __ Cmp(w11, StackFrame::ARGUMENTS_ADAPTOR);
1724   __ B(ne, &runtime);
1725
1726   // Patch the arguments.length and parameters pointer in the current frame.
1727   __ Ldr(x11, MemOperand(caller_fp,
1728                          ArgumentsAdaptorFrameConstants::kLengthOffset));
1729   __ Poke(x11, 0 * kXRegSize);
1730   __ Add(x10, caller_fp, Operand::UntagSmiAndScale(x11, kPointerSizeLog2));
1731   __ Add(x10, x10, StandardFrameConstants::kCallerSPOffset);
1732   __ Poke(x10, 1 * kXRegSize);
1733
1734   __ Bind(&runtime);
1735   __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
1736 }
1737
1738
1739 void ArgumentsAccessStub::GenerateNewSloppyFast(MacroAssembler* masm) {
1740   // Stack layout on entry.
1741   //  jssp[0]:  number of parameters (tagged)
1742   //  jssp[8]:  address of receiver argument
1743   //  jssp[16]: function
1744   //
1745   // Returns pointer to result object in x0.
1746
1747   CHECK(!has_new_target());
1748
1749   // Note: arg_count_smi is an alias of param_count_smi.
1750   Register arg_count_smi = x3;
1751   Register param_count_smi = x3;
1752   Register param_count = x7;
1753   Register recv_arg = x14;
1754   Register function = x4;
1755   __ Pop(param_count_smi, recv_arg, function);
1756   __ SmiUntag(param_count, param_count_smi);
1757
1758   // Check if the calling frame is an arguments adaptor frame.
1759   Register caller_fp = x11;
1760   Register caller_ctx = x12;
1761   Label runtime;
1762   Label adaptor_frame, try_allocate;
1763   __ Ldr(caller_fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1764   __ Ldr(caller_ctx, MemOperand(caller_fp,
1765                                 StandardFrameConstants::kContextOffset));
1766   __ Cmp(caller_ctx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
1767   __ B(eq, &adaptor_frame);
1768
1769   // No adaptor, parameter count = argument count.
1770
1771   //   x1   mapped_params number of mapped params, min(params, args) (uninit)
1772   //   x2   arg_count     number of function arguments (uninit)
1773   //   x3   arg_count_smi number of function arguments (smi)
1774   //   x4   function      function pointer
1775   //   x7   param_count   number of function parameters
1776   //   x11  caller_fp     caller's frame pointer
1777   //   x14  recv_arg      pointer to receiver arguments
1778
1779   Register arg_count = x2;
1780   __ Mov(arg_count, param_count);
1781   __ B(&try_allocate);
1782
1783   // We have an adaptor frame. Patch the parameters pointer.
1784   __ Bind(&adaptor_frame);
1785   __ Ldr(arg_count_smi,
1786          MemOperand(caller_fp,
1787                     ArgumentsAdaptorFrameConstants::kLengthOffset));
1788   __ SmiUntag(arg_count, arg_count_smi);
1789   __ Add(x10, caller_fp, Operand(arg_count, LSL, kPointerSizeLog2));
1790   __ Add(recv_arg, x10, StandardFrameConstants::kCallerSPOffset);
1791
1792   // Compute the mapped parameter count = min(param_count, arg_count)
1793   Register mapped_params = x1;
1794   __ Cmp(param_count, arg_count);
1795   __ Csel(mapped_params, param_count, arg_count, lt);
1796
1797   __ Bind(&try_allocate);
1798
1799   //   x0   alloc_obj     pointer to allocated objects: param map, backing
1800   //                      store, arguments (uninit)
1801   //   x1   mapped_params number of mapped parameters, min(params, args)
1802   //   x2   arg_count     number of function arguments
1803   //   x3   arg_count_smi number of function arguments (smi)
1804   //   x4   function      function pointer
1805   //   x7   param_count   number of function parameters
1806   //   x10  size          size of objects to allocate (uninit)
1807   //   x14  recv_arg      pointer to receiver arguments
1808
1809   // Compute the size of backing store, parameter map, and arguments object.
1810   // 1. Parameter map, has two extra words containing context and backing
1811   // store.
1812   const int kParameterMapHeaderSize =
1813       FixedArray::kHeaderSize + 2 * kPointerSize;
1814
1815   // Calculate the parameter map size, assuming it exists.
1816   Register size = x10;
1817   __ Mov(size, Operand(mapped_params, LSL, kPointerSizeLog2));
1818   __ Add(size, size, kParameterMapHeaderSize);
1819
1820   // If there are no mapped parameters, set the running size total to zero.
1821   // Otherwise, use the parameter map size calculated earlier.
1822   __ Cmp(mapped_params, 0);
1823   __ CzeroX(size, eq);
1824
1825   // 2. Add the size of the backing store and arguments object.
1826   __ Add(size, size, Operand(arg_count, LSL, kPointerSizeLog2));
1827   __ Add(size, size,
1828          FixedArray::kHeaderSize + Heap::kSloppyArgumentsObjectSize);
1829
1830   // Do the allocation of all three objects in one go. Assign this to x0, as it
1831   // will be returned to the caller.
1832   Register alloc_obj = x0;
1833   __ Allocate(size, alloc_obj, x11, x12, &runtime, TAG_OBJECT);
1834
1835   // Get the arguments boilerplate from the current (global) context.
1836
1837   //   x0   alloc_obj       pointer to allocated objects (param map, backing
1838   //                        store, arguments)
1839   //   x1   mapped_params   number of mapped parameters, min(params, args)
1840   //   x2   arg_count       number of function arguments
1841   //   x3   arg_count_smi   number of function arguments (smi)
1842   //   x4   function        function pointer
1843   //   x7   param_count     number of function parameters
1844   //   x11  sloppy_args_map offset to args (or aliased args) map (uninit)
1845   //   x14  recv_arg        pointer to receiver arguments
1846
1847   Register global_object = x10;
1848   Register global_ctx = x10;
1849   Register sloppy_args_map = x11;
1850   Register aliased_args_map = x10;
1851   __ Ldr(global_object, GlobalObjectMemOperand());
1852   __ Ldr(global_ctx, FieldMemOperand(global_object,
1853                                      GlobalObject::kNativeContextOffset));
1854
1855   __ Ldr(sloppy_args_map,
1856          ContextMemOperand(global_ctx, Context::SLOPPY_ARGUMENTS_MAP_INDEX));
1857   __ Ldr(aliased_args_map,
1858          ContextMemOperand(global_ctx, Context::ALIASED_ARGUMENTS_MAP_INDEX));
1859   __ Cmp(mapped_params, 0);
1860   __ CmovX(sloppy_args_map, aliased_args_map, ne);
1861
1862   // Copy the JS object part.
1863   __ Str(sloppy_args_map, FieldMemOperand(alloc_obj, JSObject::kMapOffset));
1864   __ LoadRoot(x10, Heap::kEmptyFixedArrayRootIndex);
1865   __ Str(x10, FieldMemOperand(alloc_obj, JSObject::kPropertiesOffset));
1866   __ Str(x10, FieldMemOperand(alloc_obj, JSObject::kElementsOffset));
1867
1868   // Set up the callee in-object property.
1869   STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
1870   const int kCalleeOffset = JSObject::kHeaderSize +
1871                             Heap::kArgumentsCalleeIndex * kPointerSize;
1872   __ AssertNotSmi(function);
1873   __ Str(function, FieldMemOperand(alloc_obj, kCalleeOffset));
1874
1875   // Use the length and set that as an in-object property.
1876   STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
1877   const int kLengthOffset = JSObject::kHeaderSize +
1878                             Heap::kArgumentsLengthIndex * kPointerSize;
1879   __ Str(arg_count_smi, FieldMemOperand(alloc_obj, kLengthOffset));
1880
1881   // Set up the elements pointer in the allocated arguments object.
1882   // If we allocated a parameter map, "elements" will point there, otherwise
1883   // it will point to the backing store.
1884
1885   //   x0   alloc_obj     pointer to allocated objects (param map, backing
1886   //                      store, arguments)
1887   //   x1   mapped_params number of mapped parameters, min(params, args)
1888   //   x2   arg_count     number of function arguments
1889   //   x3   arg_count_smi number of function arguments (smi)
1890   //   x4   function      function pointer
1891   //   x5   elements      pointer to parameter map or backing store (uninit)
1892   //   x6   backing_store pointer to backing store (uninit)
1893   //   x7   param_count   number of function parameters
1894   //   x14  recv_arg      pointer to receiver arguments
1895
1896   Register elements = x5;
1897   __ Add(elements, alloc_obj, Heap::kSloppyArgumentsObjectSize);
1898   __ Str(elements, FieldMemOperand(alloc_obj, JSObject::kElementsOffset));
1899
1900   // Initialize parameter map. If there are no mapped arguments, we're done.
1901   Label skip_parameter_map;
1902   __ Cmp(mapped_params, 0);
1903   // Set up backing store address, because it is needed later for filling in
1904   // the unmapped arguments.
1905   Register backing_store = x6;
1906   __ CmovX(backing_store, elements, eq);
1907   __ B(eq, &skip_parameter_map);
1908
1909   __ LoadRoot(x10, Heap::kSloppyArgumentsElementsMapRootIndex);
1910   __ Str(x10, FieldMemOperand(elements, FixedArray::kMapOffset));
1911   __ Add(x10, mapped_params, 2);
1912   __ SmiTag(x10);
1913   __ Str(x10, FieldMemOperand(elements, FixedArray::kLengthOffset));
1914   __ Str(cp, FieldMemOperand(elements,
1915                              FixedArray::kHeaderSize + 0 * kPointerSize));
1916   __ Add(x10, elements, Operand(mapped_params, LSL, kPointerSizeLog2));
1917   __ Add(x10, x10, kParameterMapHeaderSize);
1918   __ Str(x10, FieldMemOperand(elements,
1919                               FixedArray::kHeaderSize + 1 * kPointerSize));
1920
1921   // Copy the parameter slots and the holes in the arguments.
1922   // We need to fill in mapped_parameter_count slots. Then index the context,
1923   // where parameters are stored in reverse order, at:
1924   //
1925   //   MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS + parameter_count - 1
1926   //
1927   // The mapped parameter thus needs to get indices:
1928   //
1929   //   MIN_CONTEXT_SLOTS + parameter_count - 1 ..
1930   //     MIN_CONTEXT_SLOTS + parameter_count - mapped_parameter_count
1931   //
1932   // We loop from right to left.
1933
1934   //   x0   alloc_obj     pointer to allocated objects (param map, backing
1935   //                      store, arguments)
1936   //   x1   mapped_params number of mapped parameters, min(params, args)
1937   //   x2   arg_count     number of function arguments
1938   //   x3   arg_count_smi number of function arguments (smi)
1939   //   x4   function      function pointer
1940   //   x5   elements      pointer to parameter map or backing store (uninit)
1941   //   x6   backing_store pointer to backing store (uninit)
1942   //   x7   param_count   number of function parameters
1943   //   x11  loop_count    parameter loop counter (uninit)
1944   //   x12  index         parameter index (smi, uninit)
1945   //   x13  the_hole      hole value (uninit)
1946   //   x14  recv_arg      pointer to receiver arguments
1947
1948   Register loop_count = x11;
1949   Register index = x12;
1950   Register the_hole = x13;
1951   Label parameters_loop, parameters_test;
1952   __ Mov(loop_count, mapped_params);
1953   __ Add(index, param_count, static_cast<int>(Context::MIN_CONTEXT_SLOTS));
1954   __ Sub(index, index, mapped_params);
1955   __ SmiTag(index);
1956   __ LoadRoot(the_hole, Heap::kTheHoleValueRootIndex);
1957   __ Add(backing_store, elements, Operand(loop_count, LSL, kPointerSizeLog2));
1958   __ Add(backing_store, backing_store, kParameterMapHeaderSize);
1959
1960   __ B(&parameters_test);
1961
1962   __ Bind(&parameters_loop);
1963   __ Sub(loop_count, loop_count, 1);
1964   __ Mov(x10, Operand(loop_count, LSL, kPointerSizeLog2));
1965   __ Add(x10, x10, kParameterMapHeaderSize - kHeapObjectTag);
1966   __ Str(index, MemOperand(elements, x10));
1967   __ Sub(x10, x10, kParameterMapHeaderSize - FixedArray::kHeaderSize);
1968   __ Str(the_hole, MemOperand(backing_store, x10));
1969   __ Add(index, index, Smi::FromInt(1));
1970   __ Bind(&parameters_test);
1971   __ Cbnz(loop_count, &parameters_loop);
1972
1973   __ Bind(&skip_parameter_map);
1974   // Copy arguments header and remaining slots (if there are any.)
1975   __ LoadRoot(x10, Heap::kFixedArrayMapRootIndex);
1976   __ Str(x10, FieldMemOperand(backing_store, FixedArray::kMapOffset));
1977   __ Str(arg_count_smi, FieldMemOperand(backing_store,
1978                                         FixedArray::kLengthOffset));
1979
1980   //   x0   alloc_obj     pointer to allocated objects (param map, backing
1981   //                      store, arguments)
1982   //   x1   mapped_params number of mapped parameters, min(params, args)
1983   //   x2   arg_count     number of function arguments
1984   //   x4   function      function pointer
1985   //   x3   arg_count_smi number of function arguments (smi)
1986   //   x6   backing_store pointer to backing store (uninit)
1987   //   x14  recv_arg      pointer to receiver arguments
1988
1989   Label arguments_loop, arguments_test;
1990   __ Mov(x10, mapped_params);
1991   __ Sub(recv_arg, recv_arg, Operand(x10, LSL, kPointerSizeLog2));
1992   __ B(&arguments_test);
1993
1994   __ Bind(&arguments_loop);
1995   __ Sub(recv_arg, recv_arg, kPointerSize);
1996   __ Ldr(x11, MemOperand(recv_arg));
1997   __ Add(x12, backing_store, Operand(x10, LSL, kPointerSizeLog2));
1998   __ Str(x11, FieldMemOperand(x12, FixedArray::kHeaderSize));
1999   __ Add(x10, x10, 1);
2000
2001   __ Bind(&arguments_test);
2002   __ Cmp(x10, arg_count);
2003   __ B(lt, &arguments_loop);
2004
2005   __ Ret();
2006
2007   // Do the runtime call to allocate the arguments object.
2008   __ Bind(&runtime);
2009   __ Push(function, recv_arg, arg_count_smi);
2010   __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
2011 }
2012
2013
2014 void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
2015   // Return address is in lr.
2016   Label slow;
2017
2018   Register receiver = LoadDescriptor::ReceiverRegister();
2019   Register key = LoadDescriptor::NameRegister();
2020
2021   // Check that the key is an array index, that is Uint32.
2022   __ TestAndBranchIfAnySet(key, kSmiTagMask | kSmiSignMask, &slow);
2023
2024   // Everything is fine, call runtime.
2025   __ Push(receiver, key);
2026   __ TailCallExternalReference(
2027       ExternalReference(IC_Utility(IC::kLoadElementWithInterceptor),
2028                         masm->isolate()),
2029       2, 1);
2030
2031   __ Bind(&slow);
2032   PropertyAccessCompiler::TailCallBuiltin(
2033       masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
2034 }
2035
2036
2037 void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
2038   // Stack layout on entry.
2039   //  jssp[0]:  number of parameters (tagged)
2040   //  jssp[8]:  address of receiver argument
2041   //  jssp[16]: function
2042   //
2043   // Returns pointer to result object in x0.
2044
2045   // Get the stub arguments from the frame, and make an untagged copy of the
2046   // parameter count.
2047   Register param_count_smi = x1;
2048   Register params = x2;
2049   Register function = x3;
2050   Register param_count = x13;
2051   __ Pop(param_count_smi, params, function);
2052   __ SmiUntag(param_count, param_count_smi);
2053
2054   // Test if arguments adaptor needed.
2055   Register caller_fp = x11;
2056   Register caller_ctx = x12;
2057   Label try_allocate, runtime;
2058   __ Ldr(caller_fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2059   __ Ldr(caller_ctx, MemOperand(caller_fp,
2060                                 StandardFrameConstants::kContextOffset));
2061   __ Cmp(caller_ctx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2062   __ B(ne, &try_allocate);
2063
2064   //   x1   param_count_smi   number of parameters passed to function (smi)
2065   //   x2   params            pointer to parameters
2066   //   x3   function          function pointer
2067   //   x11  caller_fp         caller's frame pointer
2068   //   x13  param_count       number of parameters passed to function
2069
2070   // Patch the argument length and parameters pointer.
2071   __ Ldr(param_count_smi,
2072          MemOperand(caller_fp,
2073                     ArgumentsAdaptorFrameConstants::kLengthOffset));
2074   __ SmiUntag(param_count, param_count_smi);
2075   if (has_new_target()) {
2076     __ Cmp(param_count, Operand(0));
2077     Label skip_decrement;
2078     __ B(eq, &skip_decrement);
2079     // Skip new.target: it is not a part of arguments.
2080     __ Sub(param_count, param_count, Operand(1));
2081     __ SmiTag(param_count_smi, param_count);
2082     __ Bind(&skip_decrement);
2083   }
2084   __ Add(x10, caller_fp, Operand(param_count, LSL, kPointerSizeLog2));
2085   __ Add(params, x10, StandardFrameConstants::kCallerSPOffset);
2086
2087   // Try the new space allocation. Start out with computing the size of the
2088   // arguments object and the elements array in words.
2089   Register size = x10;
2090   __ Bind(&try_allocate);
2091   __ Add(size, param_count, FixedArray::kHeaderSize / kPointerSize);
2092   __ Cmp(param_count, 0);
2093   __ CzeroX(size, eq);
2094   __ Add(size, size, Heap::kStrictArgumentsObjectSize / kPointerSize);
2095
2096   // Do the allocation of both objects in one go. Assign this to x0, as it will
2097   // be returned to the caller.
2098   Register alloc_obj = x0;
2099   __ Allocate(size, alloc_obj, x11, x12, &runtime,
2100               static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
2101
2102   // Get the arguments boilerplate from the current (native) context.
2103   Register global_object = x10;
2104   Register global_ctx = x10;
2105   Register strict_args_map = x4;
2106   __ Ldr(global_object, GlobalObjectMemOperand());
2107   __ Ldr(global_ctx, FieldMemOperand(global_object,
2108                                      GlobalObject::kNativeContextOffset));
2109   __ Ldr(strict_args_map,
2110          ContextMemOperand(global_ctx, Context::STRICT_ARGUMENTS_MAP_INDEX));
2111
2112   //   x0   alloc_obj         pointer to allocated objects: parameter array and
2113   //                          arguments object
2114   //   x1   param_count_smi   number of parameters passed to function (smi)
2115   //   x2   params            pointer to parameters
2116   //   x3   function          function pointer
2117   //   x4   strict_args_map   offset to arguments map
2118   //   x13  param_count       number of parameters passed to function
2119   __ Str(strict_args_map, FieldMemOperand(alloc_obj, JSObject::kMapOffset));
2120   __ LoadRoot(x5, Heap::kEmptyFixedArrayRootIndex);
2121   __ Str(x5, FieldMemOperand(alloc_obj, JSObject::kPropertiesOffset));
2122   __ Str(x5, FieldMemOperand(alloc_obj, JSObject::kElementsOffset));
2123
2124   // Set the smi-tagged length as an in-object property.
2125   STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
2126   const int kLengthOffset = JSObject::kHeaderSize +
2127                             Heap::kArgumentsLengthIndex * kPointerSize;
2128   __ Str(param_count_smi, FieldMemOperand(alloc_obj, kLengthOffset));
2129
2130   // If there are no actual arguments, we're done.
2131   Label done;
2132   __ Cbz(param_count, &done);
2133
2134   // Set up the elements pointer in the allocated arguments object and
2135   // initialize the header in the elements fixed array.
2136   Register elements = x5;
2137   __ Add(elements, alloc_obj, Heap::kStrictArgumentsObjectSize);
2138   __ Str(elements, FieldMemOperand(alloc_obj, JSObject::kElementsOffset));
2139   __ LoadRoot(x10, Heap::kFixedArrayMapRootIndex);
2140   __ Str(x10, FieldMemOperand(elements, FixedArray::kMapOffset));
2141   __ Str(param_count_smi, FieldMemOperand(elements, FixedArray::kLengthOffset));
2142
2143   //   x0   alloc_obj         pointer to allocated objects: parameter array and
2144   //                          arguments object
2145   //   x1   param_count_smi   number of parameters passed to function (smi)
2146   //   x2   params            pointer to parameters
2147   //   x3   function          function pointer
2148   //   x4   array             pointer to array slot (uninit)
2149   //   x5   elements          pointer to elements array of alloc_obj
2150   //   x13  param_count       number of parameters passed to function
2151
2152   // Copy the fixed array slots.
2153   Label loop;
2154   Register array = x4;
2155   // Set up pointer to first array slot.
2156   __ Add(array, elements, FixedArray::kHeaderSize - kHeapObjectTag);
2157
2158   __ Bind(&loop);
2159   // Pre-decrement the parameters pointer by kPointerSize on each iteration.
2160   // Pre-decrement in order to skip receiver.
2161   __ Ldr(x10, MemOperand(params, -kPointerSize, PreIndex));
2162   // Post-increment elements by kPointerSize on each iteration.
2163   __ Str(x10, MemOperand(array, kPointerSize, PostIndex));
2164   __ Sub(param_count, param_count, 1);
2165   __ Cbnz(param_count, &loop);
2166
2167   // Return from stub.
2168   __ Bind(&done);
2169   __ Ret();
2170
2171   // Do the runtime call to allocate the arguments object.
2172   __ Bind(&runtime);
2173   __ Push(function, params, param_count_smi);
2174   __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
2175 }
2176
2177
2178 void RestParamAccessStub::GenerateNew(MacroAssembler* masm) {
2179   // Stack layout on entry.
2180   //  jssp[0]:  index of rest parameter (tagged)
2181   //  jssp[8]:  number of parameters (tagged)
2182   //  jssp[16]: address of receiver argument
2183   //
2184   // Returns pointer to result object in x0.
2185
2186   // Get the stub arguments from the frame, and make an untagged copy of the
2187   // parameter count.
2188   Register rest_index_smi = x1;
2189   Register param_count_smi = x2;
2190   Register params = x3;
2191   Register param_count = x13;
2192   __ Pop(rest_index_smi, param_count_smi, params);
2193   __ SmiUntag(param_count, param_count_smi);
2194
2195   // Test if arguments adaptor needed.
2196   Register caller_fp = x11;
2197   Register caller_ctx = x12;
2198   Label runtime;
2199   __ Ldr(caller_fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2200   __ Ldr(caller_ctx, MemOperand(caller_fp,
2201                                 StandardFrameConstants::kContextOffset));
2202   __ Cmp(caller_ctx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2203   __ B(ne, &runtime);
2204
2205   //   x1   rest_index_smi    index of rest parameter
2206   //   x2   param_count_smi   number of parameters passed to function (smi)
2207   //   x3   params            pointer to parameters
2208   //   x11  caller_fp         caller's frame pointer
2209   //   x13  param_count       number of parameters passed to function
2210
2211   // Patch the argument length and parameters pointer.
2212   __ Ldr(param_count_smi,
2213          MemOperand(caller_fp,
2214                     ArgumentsAdaptorFrameConstants::kLengthOffset));
2215   __ SmiUntag(param_count, param_count_smi);
2216   __ Add(x10, caller_fp, Operand(param_count, LSL, kPointerSizeLog2));
2217   __ Add(params, x10, StandardFrameConstants::kCallerSPOffset);
2218
2219   __ Bind(&runtime);
2220   __ Push(params, param_count_smi, rest_index_smi);
2221   __ TailCallRuntime(Runtime::kNewRestParam, 3, 1);
2222 }
2223
2224
2225 void RegExpExecStub::Generate(MacroAssembler* masm) {
2226 #ifdef V8_INTERPRETED_REGEXP
2227   __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2228 #else  // V8_INTERPRETED_REGEXP
2229
2230   // Stack frame on entry.
2231   //  jssp[0]: last_match_info (expected JSArray)
2232   //  jssp[8]: previous index
2233   //  jssp[16]: subject string
2234   //  jssp[24]: JSRegExp object
2235   Label runtime;
2236
2237   // Use of registers for this function.
2238
2239   // Variable registers:
2240   //   x10-x13                                  used as scratch registers
2241   //   w0       string_type                     type of subject string
2242   //   x2       jsstring_length                 subject string length
2243   //   x3       jsregexp_object                 JSRegExp object
2244   //   w4       string_encoding                 Latin1 or UC16
2245   //   w5       sliced_string_offset            if the string is a SlicedString
2246   //                                            offset to the underlying string
2247   //   w6       string_representation           groups attributes of the string:
2248   //                                              - is a string
2249   //                                              - type of the string
2250   //                                              - is a short external string
2251   Register string_type = w0;
2252   Register jsstring_length = x2;
2253   Register jsregexp_object = x3;
2254   Register string_encoding = w4;
2255   Register sliced_string_offset = w5;
2256   Register string_representation = w6;
2257
2258   // These are in callee save registers and will be preserved by the call
2259   // to the native RegExp code, as this code is called using the normal
2260   // C calling convention. When calling directly from generated code the
2261   // native RegExp code will not do a GC and therefore the content of
2262   // these registers are safe to use after the call.
2263
2264   //   x19       subject                        subject string
2265   //   x20       regexp_data                    RegExp data (FixedArray)
2266   //   x21       last_match_info_elements       info relative to the last match
2267   //                                            (FixedArray)
2268   //   x22       code_object                    generated regexp code
2269   Register subject = x19;
2270   Register regexp_data = x20;
2271   Register last_match_info_elements = x21;
2272   Register code_object = x22;
2273
2274   // TODO(jbramley): Is it necessary to preserve these? I don't think ARM does.
2275   CPURegList used_callee_saved_registers(subject,
2276                                          regexp_data,
2277                                          last_match_info_elements,
2278                                          code_object);
2279   __ PushCPURegList(used_callee_saved_registers);
2280
2281   // Stack frame.
2282   //  jssp[0] : x19
2283   //  jssp[8] : x20
2284   //  jssp[16]: x21
2285   //  jssp[24]: x22
2286   //  jssp[32]: last_match_info (JSArray)
2287   //  jssp[40]: previous index
2288   //  jssp[48]: subject string
2289   //  jssp[56]: JSRegExp object
2290
2291   const int kLastMatchInfoOffset = 4 * kPointerSize;
2292   const int kPreviousIndexOffset = 5 * kPointerSize;
2293   const int kSubjectOffset = 6 * kPointerSize;
2294   const int kJSRegExpOffset = 7 * kPointerSize;
2295
2296   // Ensure that a RegExp stack is allocated.
2297   ExternalReference address_of_regexp_stack_memory_address =
2298       ExternalReference::address_of_regexp_stack_memory_address(isolate());
2299   ExternalReference address_of_regexp_stack_memory_size =
2300       ExternalReference::address_of_regexp_stack_memory_size(isolate());
2301   __ Mov(x10, address_of_regexp_stack_memory_size);
2302   __ Ldr(x10, MemOperand(x10));
2303   __ Cbz(x10, &runtime);
2304
2305   // Check that the first argument is a JSRegExp object.
2306   DCHECK(jssp.Is(__ StackPointer()));
2307   __ Peek(jsregexp_object, kJSRegExpOffset);
2308   __ JumpIfSmi(jsregexp_object, &runtime);
2309   __ JumpIfNotObjectType(jsregexp_object, x10, x10, JS_REGEXP_TYPE, &runtime);
2310
2311   // Check that the RegExp has been compiled (data contains a fixed array).
2312   __ Ldr(regexp_data, FieldMemOperand(jsregexp_object, JSRegExp::kDataOffset));
2313   if (FLAG_debug_code) {
2314     STATIC_ASSERT(kSmiTag == 0);
2315     __ Tst(regexp_data, kSmiTagMask);
2316     __ Check(ne, kUnexpectedTypeForRegExpDataFixedArrayExpected);
2317     __ CompareObjectType(regexp_data, x10, x10, FIXED_ARRAY_TYPE);
2318     __ Check(eq, kUnexpectedTypeForRegExpDataFixedArrayExpected);
2319   }
2320
2321   // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
2322   __ Ldr(x10, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
2323   __ Cmp(x10, Smi::FromInt(JSRegExp::IRREGEXP));
2324   __ B(ne, &runtime);
2325
2326   // Check that the number of captures fit in the static offsets vector buffer.
2327   // We have always at least one capture for the whole match, plus additional
2328   // ones due to capturing parentheses. A capture takes 2 registers.
2329   // The number of capture registers then is (number_of_captures + 1) * 2.
2330   __ Ldrsw(x10,
2331            UntagSmiFieldMemOperand(regexp_data,
2332                                    JSRegExp::kIrregexpCaptureCountOffset));
2333   // Check (number_of_captures + 1) * 2 <= offsets vector size
2334   //             number_of_captures * 2 <= offsets vector size - 2
2335   STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
2336   __ Add(x10, x10, x10);
2337   __ Cmp(x10, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
2338   __ B(hi, &runtime);
2339
2340   // Initialize offset for possibly sliced string.
2341   __ Mov(sliced_string_offset, 0);
2342
2343   DCHECK(jssp.Is(__ StackPointer()));
2344   __ Peek(subject, kSubjectOffset);
2345   __ JumpIfSmi(subject, &runtime);
2346
2347   __ Ldr(x10, FieldMemOperand(subject, HeapObject::kMapOffset));
2348   __ Ldrb(string_type, FieldMemOperand(x10, Map::kInstanceTypeOffset));
2349
2350   __ Ldr(jsstring_length, FieldMemOperand(subject, String::kLengthOffset));
2351
2352   // Handle subject string according to its encoding and representation:
2353   // (1) Sequential string?  If yes, go to (5).
2354   // (2) Anything but sequential or cons?  If yes, go to (6).
2355   // (3) Cons string.  If the string is flat, replace subject with first string.
2356   //     Otherwise bailout.
2357   // (4) Is subject external?  If yes, go to (7).
2358   // (5) Sequential string.  Load regexp code according to encoding.
2359   // (E) Carry on.
2360   /// [...]
2361
2362   // Deferred code at the end of the stub:
2363   // (6) Not a long external string?  If yes, go to (8).
2364   // (7) External string.  Make it, offset-wise, look like a sequential string.
2365   //     Go to (5).
2366   // (8) Short external string or not a string?  If yes, bail out to runtime.
2367   // (9) Sliced string.  Replace subject with parent.  Go to (4).
2368
2369   Label check_underlying;   // (4)
2370   Label seq_string;         // (5)
2371   Label not_seq_nor_cons;   // (6)
2372   Label external_string;    // (7)
2373   Label not_long_external;  // (8)
2374
2375   // (1) Sequential string?  If yes, go to (5).
2376   __ And(string_representation,
2377          string_type,
2378          kIsNotStringMask |
2379              kStringRepresentationMask |
2380              kShortExternalStringMask);
2381   // We depend on the fact that Strings of type
2382   // SeqString and not ShortExternalString are defined
2383   // by the following pattern:
2384   //   string_type: 0XX0 XX00
2385   //                ^  ^   ^^
2386   //                |  |   ||
2387   //                |  |   is a SeqString
2388   //                |  is not a short external String
2389   //                is a String
2390   STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
2391   STATIC_ASSERT(kShortExternalStringTag != 0);
2392   __ Cbz(string_representation, &seq_string);  // Go to (5).
2393
2394   // (2) Anything but sequential or cons?  If yes, go to (6).
2395   STATIC_ASSERT(kConsStringTag < kExternalStringTag);
2396   STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
2397   STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
2398   STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
2399   __ Cmp(string_representation, kExternalStringTag);
2400   __ B(ge, &not_seq_nor_cons);  // Go to (6).
2401
2402   // (3) Cons string.  Check that it's flat.
2403   __ Ldr(x10, FieldMemOperand(subject, ConsString::kSecondOffset));
2404   __ JumpIfNotRoot(x10, Heap::kempty_stringRootIndex, &runtime);
2405   // Replace subject with first string.
2406   __ Ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
2407
2408   // (4) Is subject external?  If yes, go to (7).
2409   __ Bind(&check_underlying);
2410   // Reload the string type.
2411   __ Ldr(x10, FieldMemOperand(subject, HeapObject::kMapOffset));
2412   __ Ldrb(string_type, FieldMemOperand(x10, Map::kInstanceTypeOffset));
2413   STATIC_ASSERT(kSeqStringTag == 0);
2414   // The underlying external string is never a short external string.
2415   STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
2416   STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
2417   __ TestAndBranchIfAnySet(string_type.X(),
2418                            kStringRepresentationMask,
2419                            &external_string);  // Go to (7).
2420
2421   // (5) Sequential string.  Load regexp code according to encoding.
2422   __ Bind(&seq_string);
2423
2424   // Check that the third argument is a positive smi less than the subject
2425   // string length. A negative value will be greater (unsigned comparison).
2426   DCHECK(jssp.Is(__ StackPointer()));
2427   __ Peek(x10, kPreviousIndexOffset);
2428   __ JumpIfNotSmi(x10, &runtime);
2429   __ Cmp(jsstring_length, x10);
2430   __ B(ls, &runtime);
2431
2432   // Argument 2 (x1): We need to load argument 2 (the previous index) into x1
2433   // before entering the exit frame.
2434   __ SmiUntag(x1, x10);
2435
2436   // The third bit determines the string encoding in string_type.
2437   STATIC_ASSERT(kOneByteStringTag == 0x04);
2438   STATIC_ASSERT(kTwoByteStringTag == 0x00);
2439   STATIC_ASSERT(kStringEncodingMask == 0x04);
2440
2441   // Find the code object based on the assumptions above.
2442   // kDataOneByteCodeOffset and kDataUC16CodeOffset are adjacent, adds an offset
2443   // of kPointerSize to reach the latter.
2444   DCHECK_EQ(JSRegExp::kDataOneByteCodeOffset + kPointerSize,
2445             JSRegExp::kDataUC16CodeOffset);
2446   __ Mov(x10, kPointerSize);
2447   // We will need the encoding later: Latin1 = 0x04
2448   //                                  UC16   = 0x00
2449   __ Ands(string_encoding, string_type, kStringEncodingMask);
2450   __ CzeroX(x10, ne);
2451   __ Add(x10, regexp_data, x10);
2452   __ Ldr(code_object, FieldMemOperand(x10, JSRegExp::kDataOneByteCodeOffset));
2453
2454   // (E) Carry on.  String handling is done.
2455
2456   // Check that the irregexp code has been generated for the actual string
2457   // encoding. If it has, the field contains a code object otherwise it contains
2458   // a smi (code flushing support).
2459   __ JumpIfSmi(code_object, &runtime);
2460
2461   // All checks done. Now push arguments for native regexp code.
2462   __ IncrementCounter(isolate()->counters()->regexp_entry_native(), 1,
2463                       x10,
2464                       x11);
2465
2466   // Isolates: note we add an additional parameter here (isolate pointer).
2467   __ EnterExitFrame(false, x10, 1);
2468   DCHECK(csp.Is(__ StackPointer()));
2469
2470   // We have 9 arguments to pass to the regexp code, therefore we have to pass
2471   // one on the stack and the rest as registers.
2472
2473   // Note that the placement of the argument on the stack isn't standard
2474   // AAPCS64:
2475   // csp[0]: Space for the return address placed by DirectCEntryStub.
2476   // csp[8]: Argument 9, the current isolate address.
2477
2478   __ Mov(x10, ExternalReference::isolate_address(isolate()));
2479   __ Poke(x10, kPointerSize);
2480
2481   Register length = w11;
2482   Register previous_index_in_bytes = w12;
2483   Register start = x13;
2484
2485   // Load start of the subject string.
2486   __ Add(start, subject, SeqString::kHeaderSize - kHeapObjectTag);
2487   // Load the length from the original subject string from the previous stack
2488   // frame. Therefore we have to use fp, which points exactly to two pointer
2489   // sizes below the previous sp. (Because creating a new stack frame pushes
2490   // the previous fp onto the stack and decrements sp by 2 * kPointerSize.)
2491   __ Ldr(subject, MemOperand(fp, kSubjectOffset + 2 * kPointerSize));
2492   __ Ldr(length, UntagSmiFieldMemOperand(subject, String::kLengthOffset));
2493
2494   // Handle UC16 encoding, two bytes make one character.
2495   //   string_encoding: if Latin1: 0x04
2496   //                    if UC16:   0x00
2497   STATIC_ASSERT(kStringEncodingMask == 0x04);
2498   __ Ubfx(string_encoding, string_encoding, 2, 1);
2499   __ Eor(string_encoding, string_encoding, 1);
2500   //   string_encoding: if Latin1: 0
2501   //                    if UC16:   1
2502
2503   // Convert string positions from characters to bytes.
2504   // Previous index is in x1.
2505   __ Lsl(previous_index_in_bytes, w1, string_encoding);
2506   __ Lsl(length, length, string_encoding);
2507   __ Lsl(sliced_string_offset, sliced_string_offset, string_encoding);
2508
2509   // Argument 1 (x0): Subject string.
2510   __ Mov(x0, subject);
2511
2512   // Argument 2 (x1): Previous index, already there.
2513
2514   // Argument 3 (x2): Get the start of input.
2515   // Start of input = start of string + previous index + substring offset
2516   //                                                     (0 if the string
2517   //                                                      is not sliced).
2518   __ Add(w10, previous_index_in_bytes, sliced_string_offset);
2519   __ Add(x2, start, Operand(w10, UXTW));
2520
2521   // Argument 4 (x3):
2522   // End of input = start of input + (length of input - previous index)
2523   __ Sub(w10, length, previous_index_in_bytes);
2524   __ Add(x3, x2, Operand(w10, UXTW));
2525
2526   // Argument 5 (x4): static offsets vector buffer.
2527   __ Mov(x4, ExternalReference::address_of_static_offsets_vector(isolate()));
2528
2529   // Argument 6 (x5): Set the number of capture registers to zero to force
2530   // global regexps to behave as non-global. This stub is not used for global
2531   // regexps.
2532   __ Mov(x5, 0);
2533
2534   // Argument 7 (x6): Start (high end) of backtracking stack memory area.
2535   __ Mov(x10, address_of_regexp_stack_memory_address);
2536   __ Ldr(x10, MemOperand(x10));
2537   __ Mov(x11, address_of_regexp_stack_memory_size);
2538   __ Ldr(x11, MemOperand(x11));
2539   __ Add(x6, x10, x11);
2540
2541   // Argument 8 (x7): Indicate that this is a direct call from JavaScript.
2542   __ Mov(x7, 1);
2543
2544   // Locate the code entry and call it.
2545   __ Add(code_object, code_object, Code::kHeaderSize - kHeapObjectTag);
2546   DirectCEntryStub stub(isolate());
2547   stub.GenerateCall(masm, code_object);
2548
2549   __ LeaveExitFrame(false, x10, true);
2550
2551   // The generated regexp code returns an int32 in w0.
2552   Label failure, exception;
2553   __ CompareAndBranch(w0, NativeRegExpMacroAssembler::FAILURE, eq, &failure);
2554   __ CompareAndBranch(w0,
2555                       NativeRegExpMacroAssembler::EXCEPTION,
2556                       eq,
2557                       &exception);
2558   __ CompareAndBranch(w0, NativeRegExpMacroAssembler::RETRY, eq, &runtime);
2559
2560   // Success: process the result from the native regexp code.
2561   Register number_of_capture_registers = x12;
2562
2563   // Calculate number of capture registers (number_of_captures + 1) * 2
2564   // and store it in the last match info.
2565   __ Ldrsw(x10,
2566            UntagSmiFieldMemOperand(regexp_data,
2567                                    JSRegExp::kIrregexpCaptureCountOffset));
2568   __ Add(x10, x10, x10);
2569   __ Add(number_of_capture_registers, x10, 2);
2570
2571   // Check that the fourth object is a JSArray object.
2572   DCHECK(jssp.Is(__ StackPointer()));
2573   __ Peek(x10, kLastMatchInfoOffset);
2574   __ JumpIfSmi(x10, &runtime);
2575   __ JumpIfNotObjectType(x10, x11, x11, JS_ARRAY_TYPE, &runtime);
2576
2577   // Check that the JSArray is the fast case.
2578   __ Ldr(last_match_info_elements,
2579          FieldMemOperand(x10, JSArray::kElementsOffset));
2580   __ Ldr(x10,
2581          FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
2582   __ JumpIfNotRoot(x10, Heap::kFixedArrayMapRootIndex, &runtime);
2583
2584   // Check that the last match info has space for the capture registers and the
2585   // additional information (overhead).
2586   //     (number_of_captures + 1) * 2 + overhead <= last match info size
2587   //     (number_of_captures * 2) + 2 + overhead <= last match info size
2588   //      number_of_capture_registers + overhead <= last match info size
2589   __ Ldrsw(x10,
2590            UntagSmiFieldMemOperand(last_match_info_elements,
2591                                    FixedArray::kLengthOffset));
2592   __ Add(x11, number_of_capture_registers, RegExpImpl::kLastMatchOverhead);
2593   __ Cmp(x11, x10);
2594   __ B(gt, &runtime);
2595
2596   // Store the capture count.
2597   __ SmiTag(x10, number_of_capture_registers);
2598   __ Str(x10,
2599          FieldMemOperand(last_match_info_elements,
2600                          RegExpImpl::kLastCaptureCountOffset));
2601   // Store last subject and last input.
2602   __ Str(subject,
2603          FieldMemOperand(last_match_info_elements,
2604                          RegExpImpl::kLastSubjectOffset));
2605   // Use x10 as the subject string in order to only need
2606   // one RecordWriteStub.
2607   __ Mov(x10, subject);
2608   __ RecordWriteField(last_match_info_elements,
2609                       RegExpImpl::kLastSubjectOffset,
2610                       x10,
2611                       x11,
2612                       kLRHasNotBeenSaved,
2613                       kDontSaveFPRegs);
2614   __ Str(subject,
2615          FieldMemOperand(last_match_info_elements,
2616                          RegExpImpl::kLastInputOffset));
2617   __ Mov(x10, subject);
2618   __ RecordWriteField(last_match_info_elements,
2619                       RegExpImpl::kLastInputOffset,
2620                       x10,
2621                       x11,
2622                       kLRHasNotBeenSaved,
2623                       kDontSaveFPRegs);
2624
2625   Register last_match_offsets = x13;
2626   Register offsets_vector_index = x14;
2627   Register current_offset = x15;
2628
2629   // Get the static offsets vector filled by the native regexp code
2630   // and fill the last match info.
2631   ExternalReference address_of_static_offsets_vector =
2632       ExternalReference::address_of_static_offsets_vector(isolate());
2633   __ Mov(offsets_vector_index, address_of_static_offsets_vector);
2634
2635   Label next_capture, done;
2636   // Capture register counter starts from number of capture registers and
2637   // iterates down to zero (inclusive).
2638   __ Add(last_match_offsets,
2639          last_match_info_elements,
2640          RegExpImpl::kFirstCaptureOffset - kHeapObjectTag);
2641   __ Bind(&next_capture);
2642   __ Subs(number_of_capture_registers, number_of_capture_registers, 2);
2643   __ B(mi, &done);
2644   // Read two 32 bit values from the static offsets vector buffer into
2645   // an X register
2646   __ Ldr(current_offset,
2647          MemOperand(offsets_vector_index, kWRegSize * 2, PostIndex));
2648   // Store the smi values in the last match info.
2649   __ SmiTag(x10, current_offset);
2650   // Clearing the 32 bottom bits gives us a Smi.
2651   STATIC_ASSERT(kSmiTag == 0);
2652   __ Bic(x11, current_offset, kSmiShiftMask);
2653   __ Stp(x10,
2654          x11,
2655          MemOperand(last_match_offsets, kXRegSize * 2, PostIndex));
2656   __ B(&next_capture);
2657   __ Bind(&done);
2658
2659   // Return last match info.
2660   __ Peek(x0, kLastMatchInfoOffset);
2661   __ PopCPURegList(used_callee_saved_registers);
2662   // Drop the 4 arguments of the stub from the stack.
2663   __ Drop(4);
2664   __ Ret();
2665
2666   __ Bind(&exception);
2667   Register exception_value = x0;
2668   // A stack overflow (on the backtrack stack) may have occured
2669   // in the RegExp code but no exception has been created yet.
2670   // If there is no pending exception, handle that in the runtime system.
2671   __ Mov(x10, Operand(isolate()->factory()->the_hole_value()));
2672   __ Mov(x11,
2673          Operand(ExternalReference(Isolate::kPendingExceptionAddress,
2674                                    isolate())));
2675   __ Ldr(exception_value, MemOperand(x11));
2676   __ Cmp(x10, exception_value);
2677   __ B(eq, &runtime);
2678
2679   // For exception, throw the exception again.
2680   __ TailCallRuntime(Runtime::kRegExpExecReThrow, 4, 1);
2681
2682   __ Bind(&failure);
2683   __ Mov(x0, Operand(isolate()->factory()->null_value()));
2684   __ PopCPURegList(used_callee_saved_registers);
2685   // Drop the 4 arguments of the stub from the stack.
2686   __ Drop(4);
2687   __ Ret();
2688
2689   __ Bind(&runtime);
2690   __ PopCPURegList(used_callee_saved_registers);
2691   __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2692
2693   // Deferred code for string handling.
2694   // (6) Not a long external string?  If yes, go to (8).
2695   __ Bind(&not_seq_nor_cons);
2696   // Compare flags are still set.
2697   __ B(ne, &not_long_external);  // Go to (8).
2698
2699   // (7) External string. Make it, offset-wise, look like a sequential string.
2700   __ Bind(&external_string);
2701   if (masm->emit_debug_code()) {
2702     // Assert that we do not have a cons or slice (indirect strings) here.
2703     // Sequential strings have already been ruled out.
2704     __ Ldr(x10, FieldMemOperand(subject, HeapObject::kMapOffset));
2705     __ Ldrb(x10, FieldMemOperand(x10, Map::kInstanceTypeOffset));
2706     __ Tst(x10, kIsIndirectStringMask);
2707     __ Check(eq, kExternalStringExpectedButNotFound);
2708     __ And(x10, x10, kStringRepresentationMask);
2709     __ Cmp(x10, 0);
2710     __ Check(ne, kExternalStringExpectedButNotFound);
2711   }
2712   __ Ldr(subject,
2713          FieldMemOperand(subject, ExternalString::kResourceDataOffset));
2714   // Move the pointer so that offset-wise, it looks like a sequential string.
2715   STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2716   __ Sub(subject, subject, SeqTwoByteString::kHeaderSize - kHeapObjectTag);
2717   __ B(&seq_string);    // Go to (5).
2718
2719   // (8) If this is a short external string or not a string, bail out to
2720   // runtime.
2721   __ Bind(&not_long_external);
2722   STATIC_ASSERT(kShortExternalStringTag != 0);
2723   __ TestAndBranchIfAnySet(string_representation,
2724                            kShortExternalStringMask | kIsNotStringMask,
2725                            &runtime);
2726
2727   // (9) Sliced string. Replace subject with parent.
2728   __ Ldr(sliced_string_offset,
2729          UntagSmiFieldMemOperand(subject, SlicedString::kOffsetOffset));
2730   __ Ldr(subject, FieldMemOperand(subject, SlicedString::kParentOffset));
2731   __ B(&check_underlying);    // Go to (4).
2732 #endif
2733 }
2734
2735
2736 static void GenerateRecordCallTarget(MacroAssembler* masm,
2737                                      Register argc,
2738                                      Register function,
2739                                      Register feedback_vector,
2740                                      Register index,
2741                                      Register scratch1,
2742                                      Register scratch2) {
2743   ASM_LOCATION("GenerateRecordCallTarget");
2744   DCHECK(!AreAliased(scratch1, scratch2,
2745                      argc, function, feedback_vector, index));
2746   // Cache the called function in a feedback vector slot. Cache states are
2747   // uninitialized, monomorphic (indicated by a JSFunction), and megamorphic.
2748   //  argc :            number of arguments to the construct function
2749   //  function :        the function to call
2750   //  feedback_vector : the feedback vector
2751   //  index :           slot in feedback vector (smi)
2752   Label initialize, done, miss, megamorphic, not_array_function;
2753
2754   DCHECK_EQ(*TypeFeedbackVector::MegamorphicSentinel(masm->isolate()),
2755             masm->isolate()->heap()->megamorphic_symbol());
2756   DCHECK_EQ(*TypeFeedbackVector::UninitializedSentinel(masm->isolate()),
2757             masm->isolate()->heap()->uninitialized_symbol());
2758
2759   // Load the cache state.
2760   __ Add(scratch1, feedback_vector,
2761          Operand::UntagSmiAndScale(index, kPointerSizeLog2));
2762   __ Ldr(scratch1, FieldMemOperand(scratch1, FixedArray::kHeaderSize));
2763
2764   // A monomorphic cache hit or an already megamorphic state: invoke the
2765   // function without changing the state.
2766   __ Cmp(scratch1, function);
2767   __ B(eq, &done);
2768
2769   if (!FLAG_pretenuring_call_new) {
2770     // If we came here, we need to see if we are the array function.
2771     // If we didn't have a matching function, and we didn't find the megamorph
2772     // sentinel, then we have in the slot either some other function or an
2773     // AllocationSite. Do a map check on the object in scratch1 register.
2774     __ Ldr(scratch2, FieldMemOperand(scratch1, AllocationSite::kMapOffset));
2775     __ JumpIfNotRoot(scratch2, Heap::kAllocationSiteMapRootIndex, &miss);
2776
2777     // Make sure the function is the Array() function
2778     __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, scratch1);
2779     __ Cmp(function, scratch1);
2780     __ B(ne, &megamorphic);
2781     __ B(&done);
2782   }
2783
2784   __ Bind(&miss);
2785
2786   // A monomorphic miss (i.e, here the cache is not uninitialized) goes
2787   // megamorphic.
2788   __ JumpIfRoot(scratch1, Heap::kuninitialized_symbolRootIndex, &initialize);
2789   // MegamorphicSentinel is an immortal immovable object (undefined) so no
2790   // write-barrier is needed.
2791   __ Bind(&megamorphic);
2792   __ Add(scratch1, feedback_vector,
2793          Operand::UntagSmiAndScale(index, kPointerSizeLog2));
2794   __ LoadRoot(scratch2, Heap::kmegamorphic_symbolRootIndex);
2795   __ Str(scratch2, FieldMemOperand(scratch1, FixedArray::kHeaderSize));
2796   __ B(&done);
2797
2798   // An uninitialized cache is patched with the function or sentinel to
2799   // indicate the ElementsKind if function is the Array constructor.
2800   __ Bind(&initialize);
2801
2802   if (!FLAG_pretenuring_call_new) {
2803     // Make sure the function is the Array() function
2804     __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, scratch1);
2805     __ Cmp(function, scratch1);
2806     __ B(ne, &not_array_function);
2807
2808     // The target function is the Array constructor,
2809     // Create an AllocationSite if we don't already have it, store it in the
2810     // slot.
2811     {
2812       FrameScope scope(masm, StackFrame::INTERNAL);
2813       CreateAllocationSiteStub create_stub(masm->isolate());
2814
2815       // Arguments register must be smi-tagged to call out.
2816       __ SmiTag(argc);
2817       __ Push(argc, function, feedback_vector, index);
2818
2819       // CreateAllocationSiteStub expect the feedback vector in x2 and the slot
2820       // index in x3.
2821       DCHECK(feedback_vector.Is(x2) && index.Is(x3));
2822       __ CallStub(&create_stub);
2823
2824       __ Pop(index, feedback_vector, function, argc);
2825       __ SmiUntag(argc);
2826     }
2827     __ B(&done);
2828
2829     __ Bind(&not_array_function);
2830   }
2831
2832   // An uninitialized cache is patched with the function.
2833
2834   __ Add(scratch1, feedback_vector,
2835          Operand::UntagSmiAndScale(index, kPointerSizeLog2));
2836   __ Add(scratch1, scratch1, FixedArray::kHeaderSize - kHeapObjectTag);
2837   __ Str(function, MemOperand(scratch1, 0));
2838
2839   __ Push(function);
2840   __ RecordWrite(feedback_vector, scratch1, function, kLRHasNotBeenSaved,
2841                  kDontSaveFPRegs, EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2842   __ Pop(function);
2843
2844   __ Bind(&done);
2845 }
2846
2847
2848 static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
2849   // Do not transform the receiver for strict mode functions.
2850   __ Ldr(x3, FieldMemOperand(x1, JSFunction::kSharedFunctionInfoOffset));
2851   __ Ldr(w4, FieldMemOperand(x3, SharedFunctionInfo::kCompilerHintsOffset));
2852   __ Tbnz(w4, SharedFunctionInfo::kStrictModeFunction, cont);
2853
2854   // Do not transform the receiver for native (Compilerhints already in x3).
2855   __ Tbnz(w4, SharedFunctionInfo::kNative, cont);
2856 }
2857
2858
2859 static void EmitSlowCase(MacroAssembler* masm,
2860                          int argc,
2861                          Register function,
2862                          Register type,
2863                          Label* non_function) {
2864   // Check for function proxy.
2865   // x10 : function type.
2866   __ CompareAndBranch(type, JS_FUNCTION_PROXY_TYPE, ne, non_function);
2867   __ Push(function);  // put proxy as additional argument
2868   __ Mov(x0, argc + 1);
2869   __ Mov(x2, 0);
2870   __ GetBuiltinFunction(x1, Builtins::CALL_FUNCTION_PROXY);
2871   {
2872     Handle<Code> adaptor =
2873         masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
2874     __ Jump(adaptor, RelocInfo::CODE_TARGET);
2875   }
2876
2877   // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
2878   // of the original receiver from the call site).
2879   __ Bind(non_function);
2880   __ Poke(function, argc * kXRegSize);
2881   __ Mov(x0, argc);  // Set up the number of arguments.
2882   __ Mov(x2, 0);
2883   __ GetBuiltinFunction(function, Builtins::CALL_NON_FUNCTION);
2884   __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
2885           RelocInfo::CODE_TARGET);
2886 }
2887
2888
2889 static void EmitWrapCase(MacroAssembler* masm, int argc, Label* cont) {
2890   // Wrap the receiver and patch it back onto the stack.
2891   { FrameScope frame_scope(masm, StackFrame::INTERNAL);
2892     __ Push(x1, x3);
2893     __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
2894     __ Pop(x1);
2895   }
2896   __ Poke(x0, argc * kPointerSize);
2897   __ B(cont);
2898 }
2899
2900
2901 static void CallFunctionNoFeedback(MacroAssembler* masm,
2902                                    int argc, bool needs_checks,
2903                                    bool call_as_method) {
2904   // x1  function    the function to call
2905   Register function = x1;
2906   Register type = x4;
2907   Label slow, non_function, wrap, cont;
2908
2909   // TODO(jbramley): This function has a lot of unnamed registers. Name them,
2910   // and tidy things up a bit.
2911
2912   if (needs_checks) {
2913     // Check that the function is really a JavaScript function.
2914     __ JumpIfSmi(function, &non_function);
2915
2916     // Goto slow case if we do not have a function.
2917     __ JumpIfNotObjectType(function, x10, type, JS_FUNCTION_TYPE, &slow);
2918   }
2919
2920   // Fast-case: Invoke the function now.
2921   // x1  function  pushed function
2922   ParameterCount actual(argc);
2923
2924   if (call_as_method) {
2925     if (needs_checks) {
2926       EmitContinueIfStrictOrNative(masm, &cont);
2927     }
2928
2929     // Compute the receiver in sloppy mode.
2930     __ Peek(x3, argc * kPointerSize);
2931
2932     if (needs_checks) {
2933       __ JumpIfSmi(x3, &wrap);
2934       __ JumpIfObjectType(x3, x10, type, FIRST_SPEC_OBJECT_TYPE, &wrap, lt);
2935     } else {
2936       __ B(&wrap);
2937     }
2938
2939     __ Bind(&cont);
2940   }
2941
2942   __ InvokeFunction(function,
2943                     actual,
2944                     JUMP_FUNCTION,
2945                     NullCallWrapper());
2946   if (needs_checks) {
2947     // Slow-case: Non-function called.
2948     __ Bind(&slow);
2949     EmitSlowCase(masm, argc, function, type, &non_function);
2950   }
2951
2952   if (call_as_method) {
2953     __ Bind(&wrap);
2954     EmitWrapCase(masm, argc, &cont);
2955   }
2956 }
2957
2958
2959 void CallFunctionStub::Generate(MacroAssembler* masm) {
2960   ASM_LOCATION("CallFunctionStub::Generate");
2961   CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
2962 }
2963
2964
2965 void CallConstructStub::Generate(MacroAssembler* masm) {
2966   ASM_LOCATION("CallConstructStub::Generate");
2967   // x0 : number of arguments
2968   // x1 : the function to call
2969   // x2 : feedback vector
2970   // x3 : slot in feedback vector (smi) (if r2 is not the megamorphic symbol)
2971   Register function = x1;
2972   Label slow, non_function_call;
2973
2974   // Check that the function is not a smi.
2975   __ JumpIfSmi(function, &non_function_call);
2976   // Check that the function is a JSFunction.
2977   Register object_type = x10;
2978   __ JumpIfNotObjectType(function, object_type, object_type, JS_FUNCTION_TYPE,
2979                          &slow);
2980
2981   if (RecordCallTarget()) {
2982     GenerateRecordCallTarget(masm, x0, function, x2, x3, x4, x5);
2983
2984     __ Add(x5, x2, Operand::UntagSmiAndScale(x3, kPointerSizeLog2));
2985     if (FLAG_pretenuring_call_new) {
2986       // Put the AllocationSite from the feedback vector into x2.
2987       // By adding kPointerSize we encode that we know the AllocationSite
2988       // entry is at the feedback vector slot given by x3 + 1.
2989       __ Ldr(x2, FieldMemOperand(x5, FixedArray::kHeaderSize + kPointerSize));
2990     } else {
2991     Label feedback_register_initialized;
2992       // Put the AllocationSite from the feedback vector into x2, or undefined.
2993       __ Ldr(x2, FieldMemOperand(x5, FixedArray::kHeaderSize));
2994       __ Ldr(x5, FieldMemOperand(x2, AllocationSite::kMapOffset));
2995       __ JumpIfRoot(x5, Heap::kAllocationSiteMapRootIndex,
2996                     &feedback_register_initialized);
2997       __ LoadRoot(x2, Heap::kUndefinedValueRootIndex);
2998       __ bind(&feedback_register_initialized);
2999     }
3000
3001     __ AssertUndefinedOrAllocationSite(x2, x5);
3002   }
3003
3004   if (IsSuperConstructorCall()) {
3005     __ Mov(x4, Operand(1 * kPointerSize));
3006     __ Add(x4, x4, Operand(x0, LSL, kPointerSizeLog2));
3007     __ Peek(x3, x4);
3008   } else {
3009     __ Mov(x3, function);
3010   }
3011
3012   // Jump to the function-specific construct stub.
3013   Register jump_reg = x4;
3014   Register shared_func_info = jump_reg;
3015   Register cons_stub = jump_reg;
3016   Register cons_stub_code = jump_reg;
3017   __ Ldr(shared_func_info,
3018          FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
3019   __ Ldr(cons_stub,
3020          FieldMemOperand(shared_func_info,
3021                          SharedFunctionInfo::kConstructStubOffset));
3022   __ Add(cons_stub_code, cons_stub, Code::kHeaderSize - kHeapObjectTag);
3023   __ Br(cons_stub_code);
3024
3025   Label do_call;
3026   __ Bind(&slow);
3027   __ Cmp(object_type, JS_FUNCTION_PROXY_TYPE);
3028   __ B(ne, &non_function_call);
3029   __ GetBuiltinFunction(x1, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
3030   __ B(&do_call);
3031
3032   __ Bind(&non_function_call);
3033   __ GetBuiltinFunction(x1, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
3034
3035   __ Bind(&do_call);
3036   // Set expected number of arguments to zero (not changing x0).
3037   __ Mov(x2, 0);
3038   __ Jump(isolate()->builtins()->ArgumentsAdaptorTrampoline(),
3039           RelocInfo::CODE_TARGET);
3040 }
3041
3042
3043 static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
3044   __ Ldr(vector, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
3045   __ Ldr(vector, FieldMemOperand(vector,
3046                                  JSFunction::kSharedFunctionInfoOffset));
3047   __ Ldr(vector, FieldMemOperand(vector,
3048                                  SharedFunctionInfo::kFeedbackVectorOffset));
3049 }
3050
3051
3052 void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
3053   // x1 - function
3054   // x3 - slot id
3055   // x2 - vector
3056   Label miss;
3057   Register function = x1;
3058   Register feedback_vector = x2;
3059   Register index = x3;
3060   Register scratch = x4;
3061
3062   __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, scratch);
3063   __ Cmp(function, scratch);
3064   __ B(ne, &miss);
3065
3066   __ Mov(x0, Operand(arg_count()));
3067
3068   __ Add(scratch, feedback_vector,
3069          Operand::UntagSmiAndScale(index, kPointerSizeLog2));
3070   __ Ldr(scratch, FieldMemOperand(scratch, FixedArray::kHeaderSize));
3071
3072   // Verify that scratch contains an AllocationSite
3073   Register map = x5;
3074   __ Ldr(map, FieldMemOperand(scratch, HeapObject::kMapOffset));
3075   __ JumpIfNotRoot(map, Heap::kAllocationSiteMapRootIndex, &miss);
3076
3077   Register allocation_site = feedback_vector;
3078   __ Mov(allocation_site, scratch);
3079
3080   Register original_constructor = x3;
3081   __ Mov(original_constructor, function);
3082   ArrayConstructorStub stub(masm->isolate(), arg_count());
3083   __ TailCallStub(&stub);
3084
3085   __ bind(&miss);
3086   GenerateMiss(masm);
3087
3088   // The slow case, we need this no matter what to complete a call after a miss.
3089   CallFunctionNoFeedback(masm,
3090                          arg_count(),
3091                          true,
3092                          CallAsMethod());
3093
3094   __ Unreachable();
3095 }
3096
3097
3098 void CallICStub::Generate(MacroAssembler* masm) {
3099   ASM_LOCATION("CallICStub");
3100
3101   // x1 - function
3102   // x3 - slot id (Smi)
3103   // x2 - vector
3104   const int with_types_offset =
3105       FixedArray::OffsetOfElementAt(TypeFeedbackVector::kWithTypesIndex);
3106   const int generic_offset =
3107       FixedArray::OffsetOfElementAt(TypeFeedbackVector::kGenericCountIndex);
3108   Label extra_checks_or_miss, slow_start;
3109   Label slow, non_function, wrap, cont;
3110   Label have_js_function;
3111   int argc = arg_count();
3112   ParameterCount actual(argc);
3113
3114   Register function = x1;
3115   Register feedback_vector = x2;
3116   Register index = x3;
3117   Register type = x4;
3118
3119   // The checks. First, does x1 match the recorded monomorphic target?
3120   __ Add(x4, feedback_vector,
3121          Operand::UntagSmiAndScale(index, kPointerSizeLog2));
3122   __ Ldr(x4, FieldMemOperand(x4, FixedArray::kHeaderSize));
3123
3124   // We don't know that we have a weak cell. We might have a private symbol
3125   // or an AllocationSite, but the memory is safe to examine.
3126   // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
3127   // FixedArray.
3128   // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
3129   // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
3130   // computed, meaning that it can't appear to be a pointer. If the low bit is
3131   // 0, then hash is computed, but the 0 bit prevents the field from appearing
3132   // to be a pointer.
3133   STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
3134   STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
3135                     WeakCell::kValueOffset &&
3136                 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
3137
3138   __ Ldr(x5, FieldMemOperand(x4, WeakCell::kValueOffset));
3139   __ Cmp(x5, function);
3140   __ B(ne, &extra_checks_or_miss);
3141
3142   // The compare above could have been a SMI/SMI comparison. Guard against this
3143   // convincing us that we have a monomorphic JSFunction.
3144   __ JumpIfSmi(function, &extra_checks_or_miss);
3145
3146   __ bind(&have_js_function);
3147   if (CallAsMethod()) {
3148     EmitContinueIfStrictOrNative(masm, &cont);
3149
3150     // Compute the receiver in sloppy mode.
3151     __ Peek(x3, argc * kPointerSize);
3152
3153     __ JumpIfSmi(x3, &wrap);
3154     __ JumpIfObjectType(x3, x10, type, FIRST_SPEC_OBJECT_TYPE, &wrap, lt);
3155
3156     __ Bind(&cont);
3157   }
3158
3159   __ InvokeFunction(function,
3160                     actual,
3161                     JUMP_FUNCTION,
3162                     NullCallWrapper());
3163
3164   __ bind(&slow);
3165   EmitSlowCase(masm, argc, function, type, &non_function);
3166
3167   if (CallAsMethod()) {
3168     __ bind(&wrap);
3169     EmitWrapCase(masm, argc, &cont);
3170   }
3171
3172   __ bind(&extra_checks_or_miss);
3173   Label uninitialized, miss;
3174
3175   __ JumpIfRoot(x4, Heap::kmegamorphic_symbolRootIndex, &slow_start);
3176
3177   // The following cases attempt to handle MISS cases without going to the
3178   // runtime.
3179   if (FLAG_trace_ic) {
3180     __ jmp(&miss);
3181   }
3182
3183   __ JumpIfRoot(x4, Heap::kuninitialized_symbolRootIndex, &miss);
3184
3185   // We are going megamorphic. If the feedback is a JSFunction, it is fine
3186   // to handle it here. More complex cases are dealt with in the runtime.
3187   __ AssertNotSmi(x4);
3188   __ JumpIfNotObjectType(x4, x5, x5, JS_FUNCTION_TYPE, &miss);
3189   __ Add(x4, feedback_vector,
3190          Operand::UntagSmiAndScale(index, kPointerSizeLog2));
3191   __ LoadRoot(x5, Heap::kmegamorphic_symbolRootIndex);
3192   __ Str(x5, FieldMemOperand(x4, FixedArray::kHeaderSize));
3193   // We have to update statistics for runtime profiling.
3194   __ Ldr(x4, FieldMemOperand(feedback_vector, with_types_offset));
3195   __ Subs(x4, x4, Operand(Smi::FromInt(1)));
3196   __ Str(x4, FieldMemOperand(feedback_vector, with_types_offset));
3197   __ Ldr(x4, FieldMemOperand(feedback_vector, generic_offset));
3198   __ Adds(x4, x4, Operand(Smi::FromInt(1)));
3199   __ Str(x4, FieldMemOperand(feedback_vector, generic_offset));
3200   __ B(&slow_start);
3201
3202   __ bind(&uninitialized);
3203
3204   // We are going monomorphic, provided we actually have a JSFunction.
3205   __ JumpIfSmi(function, &miss);
3206
3207   // Goto miss case if we do not have a function.
3208   __ JumpIfNotObjectType(function, x5, x5, JS_FUNCTION_TYPE, &miss);
3209
3210   // Make sure the function is not the Array() function, which requires special
3211   // behavior on MISS.
3212   __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, x5);
3213   __ Cmp(function, x5);
3214   __ B(eq, &miss);
3215
3216   // Update stats.
3217   __ Ldr(x4, FieldMemOperand(feedback_vector, with_types_offset));
3218   __ Adds(x4, x4, Operand(Smi::FromInt(1)));
3219   __ Str(x4, FieldMemOperand(feedback_vector, with_types_offset));
3220
3221   // Store the function. Use a stub since we need a frame for allocation.
3222   // x2 - vector
3223   // x3 - slot
3224   // x1 - function
3225   {
3226     FrameScope scope(masm, StackFrame::INTERNAL);
3227     CreateWeakCellStub create_stub(masm->isolate());
3228     __ Push(function);
3229     __ CallStub(&create_stub);
3230     __ Pop(function);
3231   }
3232
3233   __ B(&have_js_function);
3234
3235   // We are here because tracing is on or we encountered a MISS case we can't
3236   // handle here.
3237   __ bind(&miss);
3238   GenerateMiss(masm);
3239
3240   // the slow case
3241   __ bind(&slow_start);
3242
3243   // Check that the function is really a JavaScript function.
3244   __ JumpIfSmi(function, &non_function);
3245
3246   // Goto slow case if we do not have a function.
3247   __ JumpIfNotObjectType(function, x10, type, JS_FUNCTION_TYPE, &slow);
3248   __ B(&have_js_function);
3249 }
3250
3251
3252 void CallICStub::GenerateMiss(MacroAssembler* masm) {
3253   ASM_LOCATION("CallICStub[Miss]");
3254
3255   FrameScope scope(masm, StackFrame::INTERNAL);
3256
3257   // Push the receiver and the function and feedback info.
3258   __ Push(x1, x2, x3);
3259
3260   // Call the entry.
3261   IC::UtilityId id = GetICState() == DEFAULT ? IC::kCallIC_Miss
3262                                              : IC::kCallIC_Customization_Miss;
3263
3264   ExternalReference miss = ExternalReference(IC_Utility(id), masm->isolate());
3265   __ CallExternalReference(miss, 3);
3266
3267   // Move result to edi and exit the internal frame.
3268   __ Mov(x1, x0);
3269 }
3270
3271
3272 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
3273   // If the receiver is a smi trigger the non-string case.
3274   if (check_mode_ == RECEIVER_IS_UNKNOWN) {
3275     __ JumpIfSmi(object_, receiver_not_string_);
3276
3277     // Fetch the instance type of the receiver into result register.
3278     __ Ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3279     __ Ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3280
3281     // If the receiver is not a string trigger the non-string case.
3282     __ TestAndBranchIfAnySet(result_, kIsNotStringMask, receiver_not_string_);
3283   }
3284
3285   // If the index is non-smi trigger the non-smi case.
3286   __ JumpIfNotSmi(index_, &index_not_smi_);
3287
3288   __ Bind(&got_smi_index_);
3289   // Check for index out of range.
3290   __ Ldrsw(result_, UntagSmiFieldMemOperand(object_, String::kLengthOffset));
3291   __ Cmp(result_, Operand::UntagSmi(index_));
3292   __ B(ls, index_out_of_range_);
3293
3294   __ SmiUntag(index_);
3295
3296   StringCharLoadGenerator::Generate(masm,
3297                                     object_,
3298                                     index_.W(),
3299                                     result_,
3300                                     &call_runtime_);
3301   __ SmiTag(result_);
3302   __ Bind(&exit_);
3303 }
3304
3305
3306 void StringCharCodeAtGenerator::GenerateSlow(
3307     MacroAssembler* masm, EmbedMode embed_mode,
3308     const RuntimeCallHelper& call_helper) {
3309   __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
3310
3311   __ Bind(&index_not_smi_);
3312   // If index is a heap number, try converting it to an integer.
3313   __ JumpIfNotHeapNumber(index_, index_not_number_);
3314   call_helper.BeforeCall(masm);
3315   if (FLAG_vector_ics && embed_mode == PART_OF_IC_HANDLER) {
3316     __ Push(VectorLoadICDescriptor::VectorRegister(),
3317             VectorLoadICDescriptor::SlotRegister(), object_, index_);
3318   } else {
3319     // Save object_ on the stack and pass index_ as argument for runtime call.
3320     __ Push(object_, index_);
3321   }
3322   if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3323     __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3324   } else {
3325     DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3326     // NumberToSmi discards numbers that are not exact integers.
3327     __ CallRuntime(Runtime::kNumberToSmi, 1);
3328   }
3329   // Save the conversion result before the pop instructions below
3330   // have a chance to overwrite it.
3331   __ Mov(index_, x0);
3332   if (FLAG_vector_ics && embed_mode == PART_OF_IC_HANDLER) {
3333     __ Pop(object_, VectorLoadICDescriptor::SlotRegister(),
3334            VectorLoadICDescriptor::VectorRegister());
3335   } else {
3336     __ Pop(object_);
3337   }
3338   // Reload the instance type.
3339   __ Ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3340   __ Ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3341   call_helper.AfterCall(masm);
3342
3343   // If index is still not a smi, it must be out of range.
3344   __ JumpIfNotSmi(index_, index_out_of_range_);
3345   // Otherwise, return to the fast path.
3346   __ B(&got_smi_index_);
3347
3348   // Call runtime. We get here when the receiver is a string and the
3349   // index is a number, but the code of getting the actual character
3350   // is too complex (e.g., when the string needs to be flattened).
3351   __ Bind(&call_runtime_);
3352   call_helper.BeforeCall(masm);
3353   __ SmiTag(index_);
3354   __ Push(object_, index_);
3355   __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
3356   __ Mov(result_, x0);
3357   call_helper.AfterCall(masm);
3358   __ B(&exit_);
3359
3360   __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
3361 }
3362
3363
3364 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3365   __ JumpIfNotSmi(code_, &slow_case_);
3366   __ Cmp(code_, Smi::FromInt(String::kMaxOneByteCharCode));
3367   __ B(hi, &slow_case_);
3368
3369   __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
3370   // At this point code register contains smi tagged one-byte char code.
3371   __ Add(result_, result_, Operand::UntagSmiAndScale(code_, kPointerSizeLog2));
3372   __ Ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
3373   __ JumpIfRoot(result_, Heap::kUndefinedValueRootIndex, &slow_case_);
3374   __ Bind(&exit_);
3375 }
3376
3377
3378 void StringCharFromCodeGenerator::GenerateSlow(
3379     MacroAssembler* masm,
3380     const RuntimeCallHelper& call_helper) {
3381   __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
3382
3383   __ Bind(&slow_case_);
3384   call_helper.BeforeCall(masm);
3385   __ Push(code_);
3386   __ CallRuntime(Runtime::kCharFromCode, 1);
3387   __ Mov(result_, x0);
3388   call_helper.AfterCall(masm);
3389   __ B(&exit_);
3390
3391   __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
3392 }
3393
3394
3395 void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3396   // Inputs are in x0 (lhs) and x1 (rhs).
3397   DCHECK(state() == CompareICState::SMI);
3398   ASM_LOCATION("CompareICStub[Smis]");
3399   Label miss;
3400   // Bail out (to 'miss') unless both x0 and x1 are smis.
3401   __ JumpIfEitherNotSmi(x0, x1, &miss);
3402
3403   if (GetCondition() == eq) {
3404     // For equality we do not care about the sign of the result.
3405     __ Sub(x0, x0, x1);
3406   } else {
3407     // Untag before subtracting to avoid handling overflow.
3408     __ SmiUntag(x1);
3409     __ Sub(x0, x1, Operand::UntagSmi(x0));
3410   }
3411   __ Ret();
3412
3413   __ Bind(&miss);
3414   GenerateMiss(masm);
3415 }
3416
3417
3418 void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3419   DCHECK(state() == CompareICState::NUMBER);
3420   ASM_LOCATION("CompareICStub[HeapNumbers]");
3421
3422   Label unordered, maybe_undefined1, maybe_undefined2;
3423   Label miss, handle_lhs, values_in_d_regs;
3424   Label untag_rhs, untag_lhs;
3425
3426   Register result = x0;
3427   Register rhs = x0;
3428   Register lhs = x1;
3429   FPRegister rhs_d = d0;
3430   FPRegister lhs_d = d1;
3431
3432   if (left() == CompareICState::SMI) {
3433     __ JumpIfNotSmi(lhs, &miss);
3434   }
3435   if (right() == CompareICState::SMI) {
3436     __ JumpIfNotSmi(rhs, &miss);
3437   }
3438
3439   __ SmiUntagToDouble(rhs_d, rhs, kSpeculativeUntag);
3440   __ SmiUntagToDouble(lhs_d, lhs, kSpeculativeUntag);
3441
3442   // Load rhs if it's a heap number.
3443   __ JumpIfSmi(rhs, &handle_lhs);
3444   __ JumpIfNotHeapNumber(rhs, &maybe_undefined1);
3445   __ Ldr(rhs_d, FieldMemOperand(rhs, HeapNumber::kValueOffset));
3446
3447   // Load lhs if it's a heap number.
3448   __ Bind(&handle_lhs);
3449   __ JumpIfSmi(lhs, &values_in_d_regs);
3450   __ JumpIfNotHeapNumber(lhs, &maybe_undefined2);
3451   __ Ldr(lhs_d, FieldMemOperand(lhs, HeapNumber::kValueOffset));
3452
3453   __ Bind(&values_in_d_regs);
3454   __ Fcmp(lhs_d, rhs_d);
3455   __ B(vs, &unordered);  // Overflow flag set if either is NaN.
3456   STATIC_ASSERT((LESS == -1) && (EQUAL == 0) && (GREATER == 1));
3457   __ Cset(result, gt);  // gt => 1, otherwise (lt, eq) => 0 (EQUAL).
3458   __ Csinv(result, result, xzr, ge);  // lt => -1, gt => 1, eq => 0.
3459   __ Ret();
3460
3461   __ Bind(&unordered);
3462   CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
3463                      CompareICState::GENERIC, CompareICState::GENERIC);
3464   __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
3465
3466   __ Bind(&maybe_undefined1);
3467   if (Token::IsOrderedRelationalCompareOp(op())) {
3468     __ JumpIfNotRoot(rhs, Heap::kUndefinedValueRootIndex, &miss);
3469     __ JumpIfSmi(lhs, &unordered);
3470     __ JumpIfNotHeapNumber(lhs, &maybe_undefined2);
3471     __ B(&unordered);
3472   }
3473
3474   __ Bind(&maybe_undefined2);
3475   if (Token::IsOrderedRelationalCompareOp(op())) {
3476     __ JumpIfRoot(lhs, Heap::kUndefinedValueRootIndex, &unordered);
3477   }
3478
3479   __ Bind(&miss);
3480   GenerateMiss(masm);
3481 }
3482
3483
3484 void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3485   DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3486   ASM_LOCATION("CompareICStub[InternalizedStrings]");
3487   Label miss;
3488
3489   Register result = x0;
3490   Register rhs = x0;
3491   Register lhs = x1;
3492
3493   // Check that both operands are heap objects.
3494   __ JumpIfEitherSmi(lhs, rhs, &miss);
3495
3496   // Check that both operands are internalized strings.
3497   Register rhs_map = x10;
3498   Register lhs_map = x11;
3499   Register rhs_type = x10;
3500   Register lhs_type = x11;
3501   __ Ldr(lhs_map, FieldMemOperand(lhs, HeapObject::kMapOffset));
3502   __ Ldr(rhs_map, FieldMemOperand(rhs, HeapObject::kMapOffset));
3503   __ Ldrb(lhs_type, FieldMemOperand(lhs_map, Map::kInstanceTypeOffset));
3504   __ Ldrb(rhs_type, FieldMemOperand(rhs_map, Map::kInstanceTypeOffset));
3505
3506   STATIC_ASSERT((kInternalizedTag == 0) && (kStringTag == 0));
3507   __ Orr(x12, lhs_type, rhs_type);
3508   __ TestAndBranchIfAnySet(
3509       x12, kIsNotStringMask | kIsNotInternalizedMask, &miss);
3510
3511   // Internalized strings are compared by identity.
3512   STATIC_ASSERT(EQUAL == 0);
3513   __ Cmp(lhs, rhs);
3514   __ Cset(result, ne);
3515   __ Ret();
3516
3517   __ Bind(&miss);
3518   GenerateMiss(masm);
3519 }
3520
3521
3522 void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3523   DCHECK(state() == CompareICState::UNIQUE_NAME);
3524   ASM_LOCATION("CompareICStub[UniqueNames]");
3525   DCHECK(GetCondition() == eq);
3526   Label miss;
3527
3528   Register result = x0;
3529   Register rhs = x0;
3530   Register lhs = x1;
3531
3532   Register lhs_instance_type = w2;
3533   Register rhs_instance_type = w3;
3534
3535   // Check that both operands are heap objects.
3536   __ JumpIfEitherSmi(lhs, rhs, &miss);
3537
3538   // Check that both operands are unique names. This leaves the instance
3539   // types loaded in tmp1 and tmp2.
3540   __ Ldr(x10, FieldMemOperand(lhs, HeapObject::kMapOffset));
3541   __ Ldr(x11, FieldMemOperand(rhs, HeapObject::kMapOffset));
3542   __ Ldrb(lhs_instance_type, FieldMemOperand(x10, Map::kInstanceTypeOffset));
3543   __ Ldrb(rhs_instance_type, FieldMemOperand(x11, Map::kInstanceTypeOffset));
3544
3545   // To avoid a miss, each instance type should be either SYMBOL_TYPE or it
3546   // should have kInternalizedTag set.
3547   __ JumpIfNotUniqueNameInstanceType(lhs_instance_type, &miss);
3548   __ JumpIfNotUniqueNameInstanceType(rhs_instance_type, &miss);
3549
3550   // Unique names are compared by identity.
3551   STATIC_ASSERT(EQUAL == 0);
3552   __ Cmp(lhs, rhs);
3553   __ Cset(result, ne);
3554   __ Ret();
3555
3556   __ Bind(&miss);
3557   GenerateMiss(masm);
3558 }
3559
3560
3561 void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3562   DCHECK(state() == CompareICState::STRING);
3563   ASM_LOCATION("CompareICStub[Strings]");
3564
3565   Label miss;
3566
3567   bool equality = Token::IsEqualityOp(op());
3568
3569   Register result = x0;
3570   Register rhs = x0;
3571   Register lhs = x1;
3572
3573   // Check that both operands are heap objects.
3574   __ JumpIfEitherSmi(rhs, lhs, &miss);
3575
3576   // Check that both operands are strings.
3577   Register rhs_map = x10;
3578   Register lhs_map = x11;
3579   Register rhs_type = x10;
3580   Register lhs_type = x11;
3581   __ Ldr(lhs_map, FieldMemOperand(lhs, HeapObject::kMapOffset));
3582   __ Ldr(rhs_map, FieldMemOperand(rhs, HeapObject::kMapOffset));
3583   __ Ldrb(lhs_type, FieldMemOperand(lhs_map, Map::kInstanceTypeOffset));
3584   __ Ldrb(rhs_type, FieldMemOperand(rhs_map, Map::kInstanceTypeOffset));
3585   STATIC_ASSERT(kNotStringTag != 0);
3586   __ Orr(x12, lhs_type, rhs_type);
3587   __ Tbnz(x12, MaskToBit(kIsNotStringMask), &miss);
3588
3589   // Fast check for identical strings.
3590   Label not_equal;
3591   __ Cmp(lhs, rhs);
3592   __ B(ne, &not_equal);
3593   __ Mov(result, EQUAL);
3594   __ Ret();
3595
3596   __ Bind(&not_equal);
3597   // Handle not identical strings
3598
3599   // Check that both strings are internalized strings. If they are, we're done
3600   // because we already know they are not identical. We know they are both
3601   // strings.
3602   if (equality) {
3603     DCHECK(GetCondition() == eq);
3604     STATIC_ASSERT(kInternalizedTag == 0);
3605     Label not_internalized_strings;
3606     __ Orr(x12, lhs_type, rhs_type);
3607     __ TestAndBranchIfAnySet(
3608         x12, kIsNotInternalizedMask, &not_internalized_strings);
3609     // Result is in rhs (x0), and not EQUAL, as rhs is not a smi.
3610     __ Ret();
3611     __ Bind(&not_internalized_strings);
3612   }
3613
3614   // Check that both strings are sequential one-byte.
3615   Label runtime;
3616   __ JumpIfBothInstanceTypesAreNotSequentialOneByte(lhs_type, rhs_type, x12,
3617                                                     x13, &runtime);
3618
3619   // Compare flat one-byte strings. Returns when done.
3620   if (equality) {
3621     StringHelper::GenerateFlatOneByteStringEquals(masm, lhs, rhs, x10, x11,
3622                                                   x12);
3623   } else {
3624     StringHelper::GenerateCompareFlatOneByteStrings(masm, lhs, rhs, x10, x11,
3625                                                     x12, x13);
3626   }
3627
3628   // Handle more complex cases in runtime.
3629   __ Bind(&runtime);
3630   __ Push(lhs, rhs);
3631   if (equality) {
3632     __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3633   } else {
3634     __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3635   }
3636
3637   __ Bind(&miss);
3638   GenerateMiss(masm);
3639 }
3640
3641
3642 void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3643   DCHECK(state() == CompareICState::OBJECT);
3644   ASM_LOCATION("CompareICStub[Objects]");
3645
3646   Label miss;
3647
3648   Register result = x0;
3649   Register rhs = x0;
3650   Register lhs = x1;
3651
3652   __ JumpIfEitherSmi(rhs, lhs, &miss);
3653
3654   __ JumpIfNotObjectType(rhs, x10, x10, JS_OBJECT_TYPE, &miss);
3655   __ JumpIfNotObjectType(lhs, x10, x10, JS_OBJECT_TYPE, &miss);
3656
3657   DCHECK(GetCondition() == eq);
3658   __ Sub(result, rhs, lhs);
3659   __ Ret();
3660
3661   __ Bind(&miss);
3662   GenerateMiss(masm);
3663 }
3664
3665
3666 void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
3667   ASM_LOCATION("CompareICStub[KnownObjects]");
3668
3669   Label miss;
3670   Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
3671
3672   Register result = x0;
3673   Register rhs = x0;
3674   Register lhs = x1;
3675
3676   __ JumpIfEitherSmi(rhs, lhs, &miss);
3677
3678   Register rhs_map = x10;
3679   Register lhs_map = x11;
3680   Register map = x12;
3681   __ GetWeakValue(map, cell);
3682   __ Ldr(rhs_map, FieldMemOperand(rhs, HeapObject::kMapOffset));
3683   __ Ldr(lhs_map, FieldMemOperand(lhs, HeapObject::kMapOffset));
3684   __ Cmp(rhs_map, map);
3685   __ B(ne, &miss);
3686   __ Cmp(lhs_map, map);
3687   __ B(ne, &miss);
3688
3689   __ Sub(result, rhs, lhs);
3690   __ Ret();
3691
3692   __ Bind(&miss);
3693   GenerateMiss(masm);
3694 }
3695
3696
3697 // This method handles the case where a compare stub had the wrong
3698 // implementation. It calls a miss handler, which re-writes the stub. All other
3699 // CompareICStub::Generate* methods should fall back into this one if their
3700 // operands were not the expected types.
3701 void CompareICStub::GenerateMiss(MacroAssembler* masm) {
3702   ASM_LOCATION("CompareICStub[Miss]");
3703
3704   Register stub_entry = x11;
3705   {
3706     ExternalReference miss =
3707       ExternalReference(IC_Utility(IC::kCompareIC_Miss), isolate());
3708
3709     FrameScope scope(masm, StackFrame::INTERNAL);
3710     Register op = x10;
3711     Register left = x1;
3712     Register right = x0;
3713     // Preserve some caller-saved registers.
3714     __ Push(x1, x0, lr);
3715     // Push the arguments.
3716     __ Mov(op, Smi::FromInt(this->op()));
3717     __ Push(left, right, op);
3718
3719     // Call the miss handler. This also pops the arguments.
3720     __ CallExternalReference(miss, 3);
3721
3722     // Compute the entry point of the rewritten stub.
3723     __ Add(stub_entry, x0, Code::kHeaderSize - kHeapObjectTag);
3724     // Restore caller-saved registers.
3725     __ Pop(lr, x0, x1);
3726   }
3727
3728   // Tail-call to the new stub.
3729   __ Jump(stub_entry);
3730 }
3731
3732
3733 void SubStringStub::Generate(MacroAssembler* masm) {
3734   ASM_LOCATION("SubStringStub::Generate");
3735   Label runtime;
3736
3737   // Stack frame on entry.
3738   //  lr: return address
3739   //  jssp[0]:  substring "to" offset
3740   //  jssp[8]:  substring "from" offset
3741   //  jssp[16]: pointer to string object
3742
3743   // This stub is called from the native-call %_SubString(...), so
3744   // nothing can be assumed about the arguments. It is tested that:
3745   //  "string" is a sequential string,
3746   //  both "from" and "to" are smis, and
3747   //  0 <= from <= to <= string.length (in debug mode.)
3748   // If any of these assumptions fail, we call the runtime system.
3749
3750   static const int kToOffset = 0 * kPointerSize;
3751   static const int kFromOffset = 1 * kPointerSize;
3752   static const int kStringOffset = 2 * kPointerSize;
3753
3754   Register to = x0;
3755   Register from = x15;
3756   Register input_string = x10;
3757   Register input_length = x11;
3758   Register input_type = x12;
3759   Register result_string = x0;
3760   Register result_length = x1;
3761   Register temp = x3;
3762
3763   __ Peek(to, kToOffset);
3764   __ Peek(from, kFromOffset);
3765
3766   // Check that both from and to are smis. If not, jump to runtime.
3767   __ JumpIfEitherNotSmi(from, to, &runtime);
3768   __ SmiUntag(from);
3769   __ SmiUntag(to);
3770
3771   // Calculate difference between from and to. If to < from, branch to runtime.
3772   __ Subs(result_length, to, from);
3773   __ B(mi, &runtime);
3774
3775   // Check from is positive.
3776   __ Tbnz(from, kWSignBit, &runtime);
3777
3778   // Make sure first argument is a string.
3779   __ Peek(input_string, kStringOffset);
3780   __ JumpIfSmi(input_string, &runtime);
3781   __ IsObjectJSStringType(input_string, input_type, &runtime);
3782
3783   Label single_char;
3784   __ Cmp(result_length, 1);
3785   __ B(eq, &single_char);
3786
3787   // Short-cut for the case of trivial substring.
3788   Label return_x0;
3789   __ Ldrsw(input_length,
3790            UntagSmiFieldMemOperand(input_string, String::kLengthOffset));
3791
3792   __ Cmp(result_length, input_length);
3793   __ CmovX(x0, input_string, eq);
3794   // Return original string.
3795   __ B(eq, &return_x0);
3796
3797   // Longer than original string's length or negative: unsafe arguments.
3798   __ B(hi, &runtime);
3799
3800   // Shorter than original string's length: an actual substring.
3801
3802   //   x0   to               substring end character offset
3803   //   x1   result_length    length of substring result
3804   //   x10  input_string     pointer to input string object
3805   //   x10  unpacked_string  pointer to unpacked string object
3806   //   x11  input_length     length of input string
3807   //   x12  input_type       instance type of input string
3808   //   x15  from             substring start character offset
3809
3810   // Deal with different string types: update the index if necessary and put
3811   // the underlying string into register unpacked_string.
3812   Label underlying_unpacked, sliced_string, seq_or_external_string;
3813   Label update_instance_type;
3814   // If the string is not indirect, it can only be sequential or external.
3815   STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3816   STATIC_ASSERT(kIsIndirectStringMask != 0);
3817
3818   // Test for string types, and branch/fall through to appropriate unpacking
3819   // code.
3820   __ Tst(input_type, kIsIndirectStringMask);
3821   __ B(eq, &seq_or_external_string);
3822   __ Tst(input_type, kSlicedNotConsMask);
3823   __ B(ne, &sliced_string);
3824
3825   Register unpacked_string = input_string;
3826
3827   // Cons string. Check whether it is flat, then fetch first part.
3828   __ Ldr(temp, FieldMemOperand(input_string, ConsString::kSecondOffset));
3829   __ JumpIfNotRoot(temp, Heap::kempty_stringRootIndex, &runtime);
3830   __ Ldr(unpacked_string,
3831          FieldMemOperand(input_string, ConsString::kFirstOffset));
3832   __ B(&update_instance_type);
3833
3834   __ Bind(&sliced_string);
3835   // Sliced string. Fetch parent and correct start index by offset.
3836   __ Ldrsw(temp,
3837            UntagSmiFieldMemOperand(input_string, SlicedString::kOffsetOffset));
3838   __ Add(from, from, temp);
3839   __ Ldr(unpacked_string,
3840          FieldMemOperand(input_string, SlicedString::kParentOffset));
3841
3842   __ Bind(&update_instance_type);
3843   __ Ldr(temp, FieldMemOperand(unpacked_string, HeapObject::kMapOffset));
3844   __ Ldrb(input_type, FieldMemOperand(temp, Map::kInstanceTypeOffset));
3845   // Now control must go to &underlying_unpacked. Since the no code is generated
3846   // before then we fall through instead of generating a useless branch.
3847
3848   __ Bind(&seq_or_external_string);
3849   // Sequential or external string. Registers unpacked_string and input_string
3850   // alias, so there's nothing to do here.
3851   // Note that if code is added here, the above code must be updated.
3852
3853   //   x0   result_string    pointer to result string object (uninit)
3854   //   x1   result_length    length of substring result
3855   //   x10  unpacked_string  pointer to unpacked string object
3856   //   x11  input_length     length of input string
3857   //   x12  input_type       instance type of input string
3858   //   x15  from             substring start character offset
3859   __ Bind(&underlying_unpacked);
3860
3861   if (FLAG_string_slices) {
3862     Label copy_routine;
3863     __ Cmp(result_length, SlicedString::kMinLength);
3864     // Short slice. Copy instead of slicing.
3865     __ B(lt, &copy_routine);
3866     // Allocate new sliced string. At this point we do not reload the instance
3867     // type including the string encoding because we simply rely on the info
3868     // provided by the original string. It does not matter if the original
3869     // string's encoding is wrong because we always have to recheck encoding of
3870     // the newly created string's parent anyway due to externalized strings.
3871     Label two_byte_slice, set_slice_header;
3872     STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
3873     STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3874     __ Tbz(input_type, MaskToBit(kStringEncodingMask), &two_byte_slice);
3875     __ AllocateOneByteSlicedString(result_string, result_length, x3, x4,
3876                                    &runtime);
3877     __ B(&set_slice_header);
3878
3879     __ Bind(&two_byte_slice);
3880     __ AllocateTwoByteSlicedString(result_string, result_length, x3, x4,
3881                                    &runtime);
3882
3883     __ Bind(&set_slice_header);
3884     __ SmiTag(from);
3885     __ Str(from, FieldMemOperand(result_string, SlicedString::kOffsetOffset));
3886     __ Str(unpacked_string,
3887            FieldMemOperand(result_string, SlicedString::kParentOffset));
3888     __ B(&return_x0);
3889
3890     __ Bind(&copy_routine);
3891   }
3892
3893   //   x0   result_string    pointer to result string object (uninit)
3894   //   x1   result_length    length of substring result
3895   //   x10  unpacked_string  pointer to unpacked string object
3896   //   x11  input_length     length of input string
3897   //   x12  input_type       instance type of input string
3898   //   x13  unpacked_char0   pointer to first char of unpacked string (uninit)
3899   //   x13  substring_char0  pointer to first char of substring (uninit)
3900   //   x14  result_char0     pointer to first char of result (uninit)
3901   //   x15  from             substring start character offset
3902   Register unpacked_char0 = x13;
3903   Register substring_char0 = x13;
3904   Register result_char0 = x14;
3905   Label two_byte_sequential, sequential_string, allocate_result;
3906   STATIC_ASSERT(kExternalStringTag != 0);
3907   STATIC_ASSERT(kSeqStringTag == 0);
3908
3909   __ Tst(input_type, kExternalStringTag);
3910   __ B(eq, &sequential_string);
3911
3912   __ Tst(input_type, kShortExternalStringTag);
3913   __ B(ne, &runtime);
3914   __ Ldr(unpacked_char0,
3915          FieldMemOperand(unpacked_string, ExternalString::kResourceDataOffset));
3916   // unpacked_char0 points to the first character of the underlying string.
3917   __ B(&allocate_result);
3918
3919   __ Bind(&sequential_string);
3920   // Locate first character of underlying subject string.
3921   STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3922   __ Add(unpacked_char0, unpacked_string,
3923          SeqOneByteString::kHeaderSize - kHeapObjectTag);
3924
3925   __ Bind(&allocate_result);
3926   // Sequential one-byte string. Allocate the result.
3927   STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
3928   __ Tbz(input_type, MaskToBit(kStringEncodingMask), &two_byte_sequential);
3929
3930   // Allocate and copy the resulting one-byte string.
3931   __ AllocateOneByteString(result_string, result_length, x3, x4, x5, &runtime);
3932
3933   // Locate first character of substring to copy.
3934   __ Add(substring_char0, unpacked_char0, from);
3935
3936   // Locate first character of result.
3937   __ Add(result_char0, result_string,
3938          SeqOneByteString::kHeaderSize - kHeapObjectTag);
3939
3940   STATIC_ASSERT((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3941   __ CopyBytes(result_char0, substring_char0, result_length, x3, kCopyLong);
3942   __ B(&return_x0);
3943
3944   // Allocate and copy the resulting two-byte string.
3945   __ Bind(&two_byte_sequential);
3946   __ AllocateTwoByteString(result_string, result_length, x3, x4, x5, &runtime);
3947
3948   // Locate first character of substring to copy.
3949   __ Add(substring_char0, unpacked_char0, Operand(from, LSL, 1));
3950
3951   // Locate first character of result.
3952   __ Add(result_char0, result_string,
3953          SeqTwoByteString::kHeaderSize - kHeapObjectTag);
3954
3955   STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3956   __ Add(result_length, result_length, result_length);
3957   __ CopyBytes(result_char0, substring_char0, result_length, x3, kCopyLong);
3958
3959   __ Bind(&return_x0);
3960   Counters* counters = isolate()->counters();
3961   __ IncrementCounter(counters->sub_string_native(), 1, x3, x4);
3962   __ Drop(3);
3963   __ Ret();
3964
3965   __ Bind(&runtime);
3966   __ TailCallRuntime(Runtime::kSubStringRT, 3, 1);
3967
3968   __ bind(&single_char);
3969   // x1: result_length
3970   // x10: input_string
3971   // x12: input_type
3972   // x15: from (untagged)
3973   __ SmiTag(from);
3974   StringCharAtGenerator generator(input_string, from, result_length, x0,
3975                                   &runtime, &runtime, &runtime,
3976                                   STRING_INDEX_IS_NUMBER, RECEIVER_IS_STRING);
3977   generator.GenerateFast(masm);
3978   __ Drop(3);
3979   __ Ret();
3980   generator.SkipSlow(masm, &runtime);
3981 }
3982
3983
3984 void ToNumberStub::Generate(MacroAssembler* masm) {
3985   // The ToNumber stub takes one argument in x0.
3986   Label not_smi;
3987   __ JumpIfNotSmi(x0, &not_smi);
3988   __ Ret();
3989   __ Bind(&not_smi);
3990
3991   Label not_heap_number;
3992   __ Ldr(x1, FieldMemOperand(x0, HeapObject::kMapOffset));
3993   __ Ldrb(x1, FieldMemOperand(x1, Map::kInstanceTypeOffset));
3994   // x0: object
3995   // x1: instance type
3996   __ Cmp(x1, HEAP_NUMBER_TYPE);
3997   __ B(ne, &not_heap_number);
3998   __ Ret();
3999   __ Bind(&not_heap_number);
4000
4001   Label not_string, slow_string;
4002   __ Cmp(x1, FIRST_NONSTRING_TYPE);
4003   __ B(hs, &not_string);
4004   // Check if string has a cached array index.
4005   __ Ldr(x2, FieldMemOperand(x0, String::kHashFieldOffset));
4006   __ Tst(x2, Operand(String::kContainsCachedArrayIndexMask));
4007   __ B(ne, &slow_string);
4008   __ IndexFromHash(x2, x0);
4009   __ Ret();
4010   __ Bind(&slow_string);
4011   __ Push(x0);  // Push argument.
4012   __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
4013   __ Bind(&not_string);
4014
4015   Label not_oddball;
4016   __ Cmp(x1, ODDBALL_TYPE);
4017   __ B(ne, &not_oddball);
4018   __ Ldr(x0, FieldMemOperand(x0, Oddball::kToNumberOffset));
4019   __ Ret();
4020   __ Bind(&not_oddball);
4021
4022   __ Push(x0);  // Push argument.
4023   __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
4024 }
4025
4026
4027 void StringHelper::GenerateFlatOneByteStringEquals(
4028     MacroAssembler* masm, Register left, Register right, Register scratch1,
4029     Register scratch2, Register scratch3) {
4030   DCHECK(!AreAliased(left, right, scratch1, scratch2, scratch3));
4031   Register result = x0;
4032   Register left_length = scratch1;
4033   Register right_length = scratch2;
4034
4035   // Compare lengths. If lengths differ, strings can't be equal. Lengths are
4036   // smis, and don't need to be untagged.
4037   Label strings_not_equal, check_zero_length;
4038   __ Ldr(left_length, FieldMemOperand(left, String::kLengthOffset));
4039   __ Ldr(right_length, FieldMemOperand(right, String::kLengthOffset));
4040   __ Cmp(left_length, right_length);
4041   __ B(eq, &check_zero_length);
4042
4043   __ Bind(&strings_not_equal);
4044   __ Mov(result, Smi::FromInt(NOT_EQUAL));
4045   __ Ret();
4046
4047   // Check if the length is zero. If so, the strings must be equal (and empty.)
4048   Label compare_chars;
4049   __ Bind(&check_zero_length);
4050   STATIC_ASSERT(kSmiTag == 0);
4051   __ Cbnz(left_length, &compare_chars);
4052   __ Mov(result, Smi::FromInt(EQUAL));
4053   __ Ret();
4054
4055   // Compare characters. Falls through if all characters are equal.
4056   __ Bind(&compare_chars);
4057   GenerateOneByteCharsCompareLoop(masm, left, right, left_length, scratch2,
4058                                   scratch3, &strings_not_equal);
4059
4060   // Characters in strings are equal.
4061   __ Mov(result, Smi::FromInt(EQUAL));
4062   __ Ret();
4063 }
4064
4065
4066 void StringHelper::GenerateCompareFlatOneByteStrings(
4067     MacroAssembler* masm, Register left, Register right, Register scratch1,
4068     Register scratch2, Register scratch3, Register scratch4) {
4069   DCHECK(!AreAliased(left, right, scratch1, scratch2, scratch3, scratch4));
4070   Label result_not_equal, compare_lengths;
4071
4072   // Find minimum length and length difference.
4073   Register length_delta = scratch3;
4074   __ Ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
4075   __ Ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
4076   __ Subs(length_delta, scratch1, scratch2);
4077
4078   Register min_length = scratch1;
4079   __ Csel(min_length, scratch2, scratch1, gt);
4080   __ Cbz(min_length, &compare_lengths);
4081
4082   // Compare loop.
4083   GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
4084                                   scratch4, &result_not_equal);
4085
4086   // Compare lengths - strings up to min-length are equal.
4087   __ Bind(&compare_lengths);
4088
4089   DCHECK(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
4090
4091   // Use length_delta as result if it's zero.
4092   Register result = x0;
4093   __ Subs(result, length_delta, 0);
4094
4095   __ Bind(&result_not_equal);
4096   Register greater = x10;
4097   Register less = x11;
4098   __ Mov(greater, Smi::FromInt(GREATER));
4099   __ Mov(less, Smi::FromInt(LESS));
4100   __ CmovX(result, greater, gt);
4101   __ CmovX(result, less, lt);
4102   __ Ret();
4103 }
4104
4105
4106 void StringHelper::GenerateOneByteCharsCompareLoop(
4107     MacroAssembler* masm, Register left, Register right, Register length,
4108     Register scratch1, Register scratch2, Label* chars_not_equal) {
4109   DCHECK(!AreAliased(left, right, length, scratch1, scratch2));
4110
4111   // Change index to run from -length to -1 by adding length to string
4112   // start. This means that loop ends when index reaches zero, which
4113   // doesn't need an additional compare.
4114   __ SmiUntag(length);
4115   __ Add(scratch1, length, SeqOneByteString::kHeaderSize - kHeapObjectTag);
4116   __ Add(left, left, scratch1);
4117   __ Add(right, right, scratch1);
4118
4119   Register index = length;
4120   __ Neg(index, length);  // index = -length;
4121
4122   // Compare loop
4123   Label loop;
4124   __ Bind(&loop);
4125   __ Ldrb(scratch1, MemOperand(left, index));
4126   __ Ldrb(scratch2, MemOperand(right, index));
4127   __ Cmp(scratch1, scratch2);
4128   __ B(ne, chars_not_equal);
4129   __ Add(index, index, 1);
4130   __ Cbnz(index, &loop);
4131 }
4132
4133
4134 void StringCompareStub::Generate(MacroAssembler* masm) {
4135   Label runtime;
4136
4137   Counters* counters = isolate()->counters();
4138
4139   // Stack frame on entry.
4140   //  sp[0]: right string
4141   //  sp[8]: left string
4142   Register right = x10;
4143   Register left = x11;
4144   Register result = x0;
4145   __ Pop(right, left);
4146
4147   Label not_same;
4148   __ Subs(result, right, left);
4149   __ B(ne, &not_same);
4150   STATIC_ASSERT(EQUAL == 0);
4151   __ IncrementCounter(counters->string_compare_native(), 1, x3, x4);
4152   __ Ret();
4153
4154   __ Bind(&not_same);
4155
4156   // Check that both objects are sequential one-byte strings.
4157   __ JumpIfEitherIsNotSequentialOneByteStrings(left, right, x12, x13, &runtime);
4158
4159   // Compare flat one-byte strings natively. Remove arguments from stack first,
4160   // as this function will generate a return.
4161   __ IncrementCounter(counters->string_compare_native(), 1, x3, x4);
4162   StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, x12, x13,
4163                                                   x14, x15);
4164
4165   __ Bind(&runtime);
4166
4167   // Push arguments back on to the stack.
4168   //  sp[0] = right string
4169   //  sp[8] = left string.
4170   __ Push(left, right);
4171
4172   // Call the runtime.
4173   // Returns -1 (less), 0 (equal), or 1 (greater) tagged as a small integer.
4174   __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
4175 }
4176
4177
4178 void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
4179   // ----------- S t a t e -------------
4180   //  -- x1    : left
4181   //  -- x0    : right
4182   //  -- lr    : return address
4183   // -----------------------------------
4184
4185   // Load x2 with the allocation site.  We stick an undefined dummy value here
4186   // and replace it with the real allocation site later when we instantiate this
4187   // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
4188   __ LoadObject(x2, handle(isolate()->heap()->undefined_value()));
4189
4190   // Make sure that we actually patched the allocation site.
4191   if (FLAG_debug_code) {
4192     __ AssertNotSmi(x2, kExpectedAllocationSite);
4193     __ Ldr(x10, FieldMemOperand(x2, HeapObject::kMapOffset));
4194     __ AssertRegisterIsRoot(x10, Heap::kAllocationSiteMapRootIndex,
4195                             kExpectedAllocationSite);
4196   }
4197
4198   // Tail call into the stub that handles binary operations with allocation
4199   // sites.
4200   BinaryOpWithAllocationSiteStub stub(isolate(), state());
4201   __ TailCallStub(&stub);
4202 }
4203
4204
4205 void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4206   // We need some extra registers for this stub, they have been allocated
4207   // but we need to save them before using them.
4208   regs_.Save(masm);
4209
4210   if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4211     Label dont_need_remembered_set;
4212
4213     Register val = regs_.scratch0();
4214     __ Ldr(val, MemOperand(regs_.address()));
4215     __ JumpIfNotInNewSpace(val, &dont_need_remembered_set);
4216
4217     __ CheckPageFlagSet(regs_.object(), val, 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4218                         &dont_need_remembered_set);
4219
4220     // First notify the incremental marker if necessary, then update the
4221     // remembered set.
4222     CheckNeedsToInformIncrementalMarker(
4223         masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
4224     InformIncrementalMarker(masm);
4225     regs_.Restore(masm);  // Restore the extra scratch registers we used.
4226
4227     __ RememberedSetHelper(object(), address(),
4228                            value(),  // scratch1
4229                            save_fp_regs_mode(), MacroAssembler::kReturnAtEnd);
4230
4231     __ Bind(&dont_need_remembered_set);
4232   }
4233
4234   CheckNeedsToInformIncrementalMarker(
4235       masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
4236   InformIncrementalMarker(masm);
4237   regs_.Restore(masm);  // Restore the extra scratch registers we used.
4238   __ Ret();
4239 }
4240
4241
4242 void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
4243   regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
4244   Register address =
4245     x0.Is(regs_.address()) ? regs_.scratch0() : regs_.address();
4246   DCHECK(!address.Is(regs_.object()));
4247   DCHECK(!address.Is(x0));
4248   __ Mov(address, regs_.address());
4249   __ Mov(x0, regs_.object());
4250   __ Mov(x1, address);
4251   __ Mov(x2, ExternalReference::isolate_address(isolate()));
4252
4253   AllowExternalCallThatCantCauseGC scope(masm);
4254   ExternalReference function =
4255       ExternalReference::incremental_marking_record_write_function(
4256           isolate());
4257   __ CallCFunction(function, 3, 0);
4258
4259   regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
4260 }
4261
4262
4263 void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4264     MacroAssembler* masm,
4265     OnNoNeedToInformIncrementalMarker on_no_need,
4266     Mode mode) {
4267   Label on_black;
4268   Label need_incremental;
4269   Label need_incremental_pop_scratch;
4270
4271   Register mem_chunk = regs_.scratch0();
4272   Register counter = regs_.scratch1();
4273   __ Bic(mem_chunk, regs_.object(), Page::kPageAlignmentMask);
4274   __ Ldr(counter,
4275          MemOperand(mem_chunk, MemoryChunk::kWriteBarrierCounterOffset));
4276   __ Subs(counter, counter, 1);
4277   __ Str(counter,
4278          MemOperand(mem_chunk, MemoryChunk::kWriteBarrierCounterOffset));
4279   __ B(mi, &need_incremental);
4280
4281   // If the object is not black we don't have to inform the incremental marker.
4282   __ JumpIfBlack(regs_.object(), regs_.scratch0(), regs_.scratch1(), &on_black);
4283
4284   regs_.Restore(masm);  // Restore the extra scratch registers we used.
4285   if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4286     __ RememberedSetHelper(object(), address(),
4287                            value(),  // scratch1
4288                            save_fp_regs_mode(), MacroAssembler::kReturnAtEnd);
4289   } else {
4290     __ Ret();
4291   }
4292
4293   __ Bind(&on_black);
4294   // Get the value from the slot.
4295   Register val = regs_.scratch0();
4296   __ Ldr(val, MemOperand(regs_.address()));
4297
4298   if (mode == INCREMENTAL_COMPACTION) {
4299     Label ensure_not_white;
4300
4301     __ CheckPageFlagClear(val, regs_.scratch1(),
4302                           MemoryChunk::kEvacuationCandidateMask,
4303                           &ensure_not_white);
4304
4305     __ CheckPageFlagClear(regs_.object(),
4306                           regs_.scratch1(),
4307                           MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4308                           &need_incremental);
4309
4310     __ Bind(&ensure_not_white);
4311   }
4312
4313   // We need extra registers for this, so we push the object and the address
4314   // register temporarily.
4315   __ Push(regs_.address(), regs_.object());
4316   __ EnsureNotWhite(val,
4317                     regs_.scratch1(),  // Scratch.
4318                     regs_.object(),    // Scratch.
4319                     regs_.address(),   // Scratch.
4320                     regs_.scratch2(),  // Scratch.
4321                     &need_incremental_pop_scratch);
4322   __ Pop(regs_.object(), regs_.address());
4323
4324   regs_.Restore(masm);  // Restore the extra scratch registers we used.
4325   if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4326     __ RememberedSetHelper(object(), address(),
4327                            value(),  // scratch1
4328                            save_fp_regs_mode(), MacroAssembler::kReturnAtEnd);
4329   } else {
4330     __ Ret();
4331   }
4332
4333   __ Bind(&need_incremental_pop_scratch);
4334   __ Pop(regs_.object(), regs_.address());
4335
4336   __ Bind(&need_incremental);
4337   // Fall through when we need to inform the incremental marker.
4338 }
4339
4340
4341 void RecordWriteStub::Generate(MacroAssembler* masm) {
4342   Label skip_to_incremental_noncompacting;
4343   Label skip_to_incremental_compacting;
4344
4345   // We patch these two first instructions back and forth between a nop and
4346   // real branch when we start and stop incremental heap marking.
4347   // Initially the stub is expected to be in STORE_BUFFER_ONLY mode, so 2 nops
4348   // are generated.
4349   // See RecordWriteStub::Patch for details.
4350   {
4351     InstructionAccurateScope scope(masm, 2);
4352     __ adr(xzr, &skip_to_incremental_noncompacting);
4353     __ adr(xzr, &skip_to_incremental_compacting);
4354   }
4355
4356   if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4357     __ RememberedSetHelper(object(), address(),
4358                            value(),  // scratch1
4359                            save_fp_regs_mode(), MacroAssembler::kReturnAtEnd);
4360   }
4361   __ Ret();
4362
4363   __ Bind(&skip_to_incremental_noncompacting);
4364   GenerateIncremental(masm, INCREMENTAL);
4365
4366   __ Bind(&skip_to_incremental_compacting);
4367   GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4368 }
4369
4370
4371 void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4372   // x0     value            element value to store
4373   // x3     index_smi        element index as smi
4374   // sp[0]  array_index_smi  array literal index in function as smi
4375   // sp[1]  array            array literal
4376
4377   Register value = x0;
4378   Register index_smi = x3;
4379
4380   Register array = x1;
4381   Register array_map = x2;
4382   Register array_index_smi = x4;
4383   __ PeekPair(array_index_smi, array, 0);
4384   __ Ldr(array_map, FieldMemOperand(array, JSObject::kMapOffset));
4385
4386   Label double_elements, smi_element, fast_elements, slow_elements;
4387   Register bitfield2 = x10;
4388   __ Ldrb(bitfield2, FieldMemOperand(array_map, Map::kBitField2Offset));
4389
4390   // Jump if array's ElementsKind is not FAST*_SMI_ELEMENTS, FAST_ELEMENTS or
4391   // FAST_HOLEY_ELEMENTS.
4392   STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4393   STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4394   STATIC_ASSERT(FAST_ELEMENTS == 2);
4395   STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4396   __ Cmp(bitfield2, Map::kMaximumBitField2FastHoleyElementValue);
4397   __ B(hi, &double_elements);
4398
4399   __ JumpIfSmi(value, &smi_element);
4400
4401   // Jump if array's ElementsKind is not FAST_ELEMENTS or FAST_HOLEY_ELEMENTS.
4402   __ Tbnz(bitfield2, MaskToBit(FAST_ELEMENTS << Map::ElementsKindBits::kShift),
4403           &fast_elements);
4404
4405   // Store into the array literal requires an elements transition. Call into
4406   // the runtime.
4407   __ Bind(&slow_elements);
4408   __ Push(array, index_smi, value);
4409   __ Ldr(x10, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4410   __ Ldr(x11, FieldMemOperand(x10, JSFunction::kLiteralsOffset));
4411   __ Push(x11, array_index_smi);
4412   __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4413
4414   // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
4415   __ Bind(&fast_elements);
4416   __ Ldr(x10, FieldMemOperand(array, JSObject::kElementsOffset));
4417   __ Add(x11, x10, Operand::UntagSmiAndScale(index_smi, kPointerSizeLog2));
4418   __ Add(x11, x11, FixedArray::kHeaderSize - kHeapObjectTag);
4419   __ Str(value, MemOperand(x11));
4420   // Update the write barrier for the array store.
4421   __ RecordWrite(x10, x11, value, kLRHasNotBeenSaved, kDontSaveFPRegs,
4422                  EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
4423   __ Ret();
4424
4425   // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS,
4426   // and value is Smi.
4427   __ Bind(&smi_element);
4428   __ Ldr(x10, FieldMemOperand(array, JSObject::kElementsOffset));
4429   __ Add(x11, x10, Operand::UntagSmiAndScale(index_smi, kPointerSizeLog2));
4430   __ Str(value, FieldMemOperand(x11, FixedArray::kHeaderSize));
4431   __ Ret();
4432
4433   __ Bind(&double_elements);
4434   __ Ldr(x10, FieldMemOperand(array, JSObject::kElementsOffset));
4435   __ StoreNumberToDoubleElements(value, index_smi, x10, x11, d0,
4436                                  &slow_elements);
4437   __ Ret();
4438 }
4439
4440
4441 void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4442   CEntryStub ces(isolate(), 1, kSaveFPRegs);
4443   __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
4444   int parameter_count_offset =
4445       StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4446   __ Ldr(x1, MemOperand(fp, parameter_count_offset));
4447   if (function_mode() == JS_FUNCTION_STUB_MODE) {
4448     __ Add(x1, x1, 1);
4449   }
4450   masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4451   __ Drop(x1);
4452   // Return to IC Miss stub, continuation still on stack.
4453   __ Ret();
4454 }
4455
4456
4457 void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4458   EmitLoadTypeFeedbackVector(masm, VectorLoadICDescriptor::VectorRegister());
4459   VectorRawLoadStub stub(isolate(), state());
4460   stub.GenerateForTrampoline(masm);
4461 }
4462
4463
4464 void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4465   EmitLoadTypeFeedbackVector(masm, VectorLoadICDescriptor::VectorRegister());
4466   VectorRawKeyedLoadStub stub(isolate());
4467   stub.GenerateForTrampoline(masm);
4468 }
4469
4470
4471 void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4472   EmitLoadTypeFeedbackVector(masm, x2);
4473   CallICStub stub(isolate(), state());
4474   __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4475 }
4476
4477
4478 void CallIC_ArrayTrampolineStub::Generate(MacroAssembler* masm) {
4479   EmitLoadTypeFeedbackVector(masm, x2);
4480   CallIC_ArrayStub stub(isolate(), state());
4481   __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4482 }
4483
4484
4485 void VectorRawLoadStub::Generate(MacroAssembler* masm) {
4486   GenerateImpl(masm, false);
4487 }
4488
4489
4490 void VectorRawLoadStub::GenerateForTrampoline(MacroAssembler* masm) {
4491   GenerateImpl(masm, true);
4492 }
4493
4494
4495 static void HandleArrayCases(MacroAssembler* masm, Register receiver,
4496                              Register key, Register vector, Register slot,
4497                              Register feedback, Register scratch1,
4498                              Register scratch2, Register scratch3,
4499                              bool is_polymorphic, Label* miss) {
4500   // feedback initially contains the feedback array
4501   Label next_loop, prepare_next;
4502   Label load_smi_map, compare_map;
4503   Label start_polymorphic;
4504
4505   Register receiver_map = scratch1;
4506   Register cached_map = scratch2;
4507
4508   // Receiver might not be a heap object.
4509   __ JumpIfSmi(receiver, &load_smi_map);
4510   __ Ldr(receiver_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
4511   __ Bind(&compare_map);
4512   __ Ldr(cached_map,
4513          FieldMemOperand(feedback, FixedArray::OffsetOfElementAt(0)));
4514   __ Ldr(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
4515   __ Cmp(receiver_map, cached_map);
4516   __ B(ne, &start_polymorphic);
4517   // found, now call handler.
4518   Register handler = feedback;
4519   __ Ldr(handler, FieldMemOperand(feedback, FixedArray::OffsetOfElementAt(1)));
4520   __ Add(handler, handler, Code::kHeaderSize - kHeapObjectTag);
4521   __ Jump(feedback);
4522
4523   Register length = scratch3;
4524   __ Bind(&start_polymorphic);
4525   __ Ldr(length, FieldMemOperand(feedback, FixedArray::kLengthOffset));
4526   if (!is_polymorphic) {
4527     __ Cmp(length, Operand(Smi::FromInt(2)));
4528     __ B(eq, miss);
4529   }
4530
4531   Register too_far = length;
4532   Register pointer_reg = feedback;
4533
4534   // +-----+------+------+-----+-----+ ... ----+
4535   // | map | len  | wm0  | h0  | wm1 |      hN |
4536   // +-----+------+------+-----+-----+ ... ----+
4537   //                 0      1     2        len-1
4538   //                              ^              ^
4539   //                              |              |
4540   //                         pointer_reg      too_far
4541   //                         aka feedback     scratch3
4542   // also need receiver_map (aka scratch1)
4543   // use cached_map (scratch2) to look in the weak map values.
4544   __ Add(too_far, feedback,
4545          Operand::UntagSmiAndScale(length, kPointerSizeLog2));
4546   __ Add(too_far, too_far, FixedArray::kHeaderSize - kHeapObjectTag);
4547   __ Add(pointer_reg, feedback,
4548          FixedArray::OffsetOfElementAt(2) - kHeapObjectTag);
4549
4550   __ Bind(&next_loop);
4551   __ Ldr(cached_map, MemOperand(pointer_reg));
4552   __ Ldr(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
4553   __ Cmp(receiver_map, cached_map);
4554   __ B(ne, &prepare_next);
4555   __ Ldr(handler, MemOperand(pointer_reg, kPointerSize));
4556   __ Add(handler, handler, Code::kHeaderSize - kHeapObjectTag);
4557   __ Jump(handler);
4558
4559   __ Bind(&prepare_next);
4560   __ Add(pointer_reg, pointer_reg, kPointerSize * 2);
4561   __ Cmp(pointer_reg, too_far);
4562   __ B(lt, &next_loop);
4563
4564   // We exhausted our array of map handler pairs.
4565   __ jmp(miss);
4566
4567   __ Bind(&load_smi_map);
4568   __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4569   __ jmp(&compare_map);
4570 }
4571
4572
4573 static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
4574                                   Register key, Register vector, Register slot,
4575                                   Register weak_cell, Register scratch,
4576                                   Label* miss) {
4577   // feedback initially contains the feedback array
4578   Label compare_smi_map;
4579   Register receiver_map = scratch;
4580   Register cached_map = weak_cell;
4581
4582   // Move the weak map into the weak_cell register.
4583   __ Ldr(cached_map, FieldMemOperand(weak_cell, WeakCell::kValueOffset));
4584
4585   // Receiver might not be a heap object.
4586   __ JumpIfSmi(receiver, &compare_smi_map);
4587   __ Ldr(receiver_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
4588   __ Cmp(cached_map, receiver_map);
4589   __ B(ne, miss);
4590
4591   Register handler = weak_cell;
4592   __ Add(handler, vector, Operand::UntagSmiAndScale(slot, kPointerSizeLog2));
4593   __ Ldr(handler,
4594          FieldMemOperand(handler, FixedArray::kHeaderSize + kPointerSize));
4595   __ Add(handler, handler, Code::kHeaderSize - kHeapObjectTag);
4596   __ Jump(weak_cell);
4597
4598   // In microbenchmarks, it made sense to unroll this code so that the call to
4599   // the handler is duplicated for a HeapObject receiver and a Smi receiver.
4600   // TODO(mvstanton): does this hold on ARM?
4601   __ Bind(&compare_smi_map);
4602   __ JumpIfNotRoot(weak_cell, Heap::kHeapNumberMapRootIndex, miss);
4603   __ Add(handler, vector, Operand::UntagSmiAndScale(slot, kPointerSizeLog2));
4604   __ Ldr(handler,
4605          FieldMemOperand(handler, FixedArray::kHeaderSize + kPointerSize));
4606   __ Add(handler, handler, Code::kHeaderSize - kHeapObjectTag);
4607   __ Jump(handler);
4608 }
4609
4610
4611 void VectorRawLoadStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4612   Register receiver = VectorLoadICDescriptor::ReceiverRegister();  // x1
4613   Register name = VectorLoadICDescriptor::NameRegister();          // x2
4614   Register vector = VectorLoadICDescriptor::VectorRegister();      // x3
4615   Register slot = VectorLoadICDescriptor::SlotRegister();          // x0
4616   Register feedback = x4;
4617   Register scratch1 = x5;
4618
4619   __ Add(feedback, vector, Operand::UntagSmiAndScale(slot, kPointerSizeLog2));
4620   __ Ldr(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
4621
4622   // Is it a weak cell?
4623   Label try_array;
4624   Label not_array, smi_key, key_okay, miss;
4625   __ Ldr(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
4626   __ JumpIfNotRoot(scratch1, Heap::kWeakCellMapRootIndex, &try_array);
4627   HandleMonomorphicCase(masm, receiver, name, vector, slot, feedback, scratch1,
4628                         &miss);
4629
4630   // Is it a fixed array?
4631   __ Bind(&try_array);
4632   __ JumpIfNotRoot(scratch1, Heap::kFixedArrayMapRootIndex, &not_array);
4633   HandleArrayCases(masm, receiver, name, vector, slot, feedback, scratch1, x6,
4634                    x7, true, &miss);
4635
4636   __ Bind(&not_array);
4637   __ JumpIfNotRoot(feedback, Heap::kmegamorphic_symbolRootIndex, &miss);
4638   Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4639       Code::ComputeHandlerFlags(Code::LOAD_IC));
4640   masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
4641                                                false, receiver, name, feedback,
4642                                                scratch1, x6, x7);
4643
4644   __ Bind(&miss);
4645   LoadIC::GenerateMiss(masm);
4646 }
4647
4648
4649 void VectorRawKeyedLoadStub::Generate(MacroAssembler* masm) {
4650   GenerateImpl(masm, false);
4651 }
4652
4653
4654 void VectorRawKeyedLoadStub::GenerateForTrampoline(MacroAssembler* masm) {
4655   GenerateImpl(masm, true);
4656 }
4657
4658
4659 void VectorRawKeyedLoadStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4660   Register receiver = VectorLoadICDescriptor::ReceiverRegister();  // x1
4661   Register key = VectorLoadICDescriptor::NameRegister();           // x2
4662   Register vector = VectorLoadICDescriptor::VectorRegister();      // x3
4663   Register slot = VectorLoadICDescriptor::SlotRegister();          // x0
4664   Register feedback = x4;
4665   Register scratch1 = x5;
4666
4667   __ Add(feedback, vector, Operand::UntagSmiAndScale(slot, kPointerSizeLog2));
4668   __ Ldr(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
4669
4670   // Is it a weak cell?
4671   Label try_array;
4672   Label not_array, smi_key, key_okay, miss;
4673   __ Ldr(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
4674   __ JumpIfNotRoot(scratch1, Heap::kWeakCellMapRootIndex, &try_array);
4675   HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, scratch1,
4676                         &miss);
4677
4678   __ Bind(&try_array);
4679   // Is it a fixed array?
4680   __ JumpIfNotRoot(scratch1, Heap::kFixedArrayMapRootIndex, &not_array);
4681
4682   // We have a polymorphic element handler.
4683   Label polymorphic, try_poly_name;
4684   __ Bind(&polymorphic);
4685   HandleArrayCases(masm, receiver, key, vector, slot, feedback, scratch1, x6,
4686                    x7, true, &miss);
4687
4688   __ Bind(&not_array);
4689   // Is it generic?
4690   __ JumpIfNotRoot(feedback, Heap::kmegamorphic_symbolRootIndex,
4691                    &try_poly_name);
4692   Handle<Code> megamorphic_stub =
4693       KeyedLoadIC::ChooseMegamorphicStub(masm->isolate());
4694   __ Jump(megamorphic_stub, RelocInfo::CODE_TARGET);
4695
4696   __ Bind(&try_poly_name);
4697   // We might have a name in feedback, and a fixed array in the next slot.
4698   __ Cmp(key, feedback);
4699   __ B(ne, &miss);
4700   // If the name comparison succeeded, we know we have a fixed array with
4701   // at least one map/handler pair.
4702   __ Add(feedback, vector, Operand::UntagSmiAndScale(slot, kPointerSizeLog2));
4703   __ Ldr(feedback,
4704          FieldMemOperand(feedback, FixedArray::kHeaderSize + kPointerSize));
4705   HandleArrayCases(masm, receiver, key, vector, slot, feedback, scratch1, x6,
4706                    x7, false, &miss);
4707
4708   __ Bind(&miss);
4709   KeyedLoadIC::GenerateMiss(masm);
4710 }
4711
4712
4713 // The entry hook is a "BumpSystemStackPointer" instruction (sub), followed by
4714 // a "Push lr" instruction, followed by a call.
4715 static const unsigned int kProfileEntryHookCallSize =
4716     Assembler::kCallSizeWithRelocation + (2 * kInstructionSize);
4717
4718
4719 void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4720   if (masm->isolate()->function_entry_hook() != NULL) {
4721     ProfileEntryHookStub stub(masm->isolate());
4722     Assembler::BlockConstPoolScope no_const_pools(masm);
4723     DontEmitDebugCodeScope no_debug_code(masm);
4724     Label entry_hook_call_start;
4725     __ Bind(&entry_hook_call_start);
4726     __ Push(lr);
4727     __ CallStub(&stub);
4728     DCHECK(masm->SizeOfCodeGeneratedSince(&entry_hook_call_start) ==
4729            kProfileEntryHookCallSize);
4730
4731     __ Pop(lr);
4732   }
4733 }
4734
4735
4736 void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4737   MacroAssembler::NoUseRealAbortsScope no_use_real_aborts(masm);
4738
4739   // Save all kCallerSaved registers (including lr), since this can be called
4740   // from anywhere.
4741   // TODO(jbramley): What about FP registers?
4742   __ PushCPURegList(kCallerSaved);
4743   DCHECK(kCallerSaved.IncludesAliasOf(lr));
4744   const int kNumSavedRegs = kCallerSaved.Count();
4745
4746   // Compute the function's address as the first argument.
4747   __ Sub(x0, lr, kProfileEntryHookCallSize);
4748
4749 #if V8_HOST_ARCH_ARM64
4750   uintptr_t entry_hook =
4751       reinterpret_cast<uintptr_t>(isolate()->function_entry_hook());
4752   __ Mov(x10, entry_hook);
4753 #else
4754   // Under the simulator we need to indirect the entry hook through a trampoline
4755   // function at a known address.
4756   ApiFunction dispatcher(FUNCTION_ADDR(EntryHookTrampoline));
4757   __ Mov(x10, Operand(ExternalReference(&dispatcher,
4758                                         ExternalReference::BUILTIN_CALL,
4759                                         isolate())));
4760   // It additionally takes an isolate as a third parameter
4761   __ Mov(x2, ExternalReference::isolate_address(isolate()));
4762 #endif
4763
4764   // The caller's return address is above the saved temporaries.
4765   // Grab its location for the second argument to the hook.
4766   __ Add(x1, __ StackPointer(), kNumSavedRegs * kPointerSize);
4767
4768   {
4769     // Create a dummy frame, as CallCFunction requires this.
4770     FrameScope frame(masm, StackFrame::MANUAL);
4771     __ CallCFunction(x10, 2, 0);
4772   }
4773
4774   __ PopCPURegList(kCallerSaved);
4775   __ Ret();
4776 }
4777
4778
4779 void DirectCEntryStub::Generate(MacroAssembler* masm) {
4780   // When calling into C++ code the stack pointer must be csp.
4781   // Therefore this code must use csp for peek/poke operations when the
4782   // stub is generated. When the stub is called
4783   // (via DirectCEntryStub::GenerateCall), the caller must setup an ExitFrame
4784   // and configure the stack pointer *before* doing the call.
4785   const Register old_stack_pointer = __ StackPointer();
4786   __ SetStackPointer(csp);
4787
4788   // Put return address on the stack (accessible to GC through exit frame pc).
4789   __ Poke(lr, 0);
4790   // Call the C++ function.
4791   __ Blr(x10);
4792   // Return to calling code.
4793   __ Peek(lr, 0);
4794   __ AssertFPCRState();
4795   __ Ret();
4796
4797   __ SetStackPointer(old_stack_pointer);
4798 }
4799
4800 void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
4801                                     Register target) {
4802   // Make sure the caller configured the stack pointer (see comment in
4803   // DirectCEntryStub::Generate).
4804   DCHECK(csp.Is(__ StackPointer()));
4805
4806   intptr_t code =
4807       reinterpret_cast<intptr_t>(GetCode().location());
4808   __ Mov(lr, Operand(code, RelocInfo::CODE_TARGET));
4809   __ Mov(x10, target);
4810   // Branch to the stub.
4811   __ Blr(lr);
4812 }
4813
4814
4815 // Probe the name dictionary in the 'elements' register.
4816 // Jump to the 'done' label if a property with the given name is found.
4817 // Jump to the 'miss' label otherwise.
4818 //
4819 // If lookup was successful 'scratch2' will be equal to elements + 4 * index.
4820 // 'elements' and 'name' registers are preserved on miss.
4821 void NameDictionaryLookupStub::GeneratePositiveLookup(
4822     MacroAssembler* masm,
4823     Label* miss,
4824     Label* done,
4825     Register elements,
4826     Register name,
4827     Register scratch1,
4828     Register scratch2) {
4829   DCHECK(!AreAliased(elements, name, scratch1, scratch2));
4830
4831   // Assert that name contains a string.
4832   __ AssertName(name);
4833
4834   // Compute the capacity mask.
4835   __ Ldrsw(scratch1, UntagSmiFieldMemOperand(elements, kCapacityOffset));
4836   __ Sub(scratch1, scratch1, 1);
4837
4838   // Generate an unrolled loop that performs a few probes before giving up.
4839   for (int i = 0; i < kInlinedProbes; i++) {
4840     // Compute the masked index: (hash + i + i * i) & mask.
4841     __ Ldr(scratch2, FieldMemOperand(name, Name::kHashFieldOffset));
4842     if (i > 0) {
4843       // Add the probe offset (i + i * i) left shifted to avoid right shifting
4844       // the hash in a separate instruction. The value hash + i + i * i is right
4845       // shifted in the following and instruction.
4846       DCHECK(NameDictionary::GetProbeOffset(i) <
4847           1 << (32 - Name::kHashFieldOffset));
4848       __ Add(scratch2, scratch2, Operand(
4849           NameDictionary::GetProbeOffset(i) << Name::kHashShift));
4850     }
4851     __ And(scratch2, scratch1, Operand(scratch2, LSR, Name::kHashShift));
4852
4853     // Scale the index by multiplying by the element size.
4854     DCHECK(NameDictionary::kEntrySize == 3);
4855     __ Add(scratch2, scratch2, Operand(scratch2, LSL, 1));
4856
4857     // Check if the key is identical to the name.
4858     UseScratchRegisterScope temps(masm);
4859     Register scratch3 = temps.AcquireX();
4860     __ Add(scratch2, elements, Operand(scratch2, LSL, kPointerSizeLog2));
4861     __ Ldr(scratch3, FieldMemOperand(scratch2, kElementsStartOffset));
4862     __ Cmp(name, scratch3);
4863     __ B(eq, done);
4864   }
4865
4866   // The inlined probes didn't find the entry.
4867   // Call the complete stub to scan the whole dictionary.
4868
4869   CPURegList spill_list(CPURegister::kRegister, kXRegSizeInBits, 0, 6);
4870   spill_list.Combine(lr);
4871   spill_list.Remove(scratch1);
4872   spill_list.Remove(scratch2);
4873
4874   __ PushCPURegList(spill_list);
4875
4876   if (name.is(x0)) {
4877     DCHECK(!elements.is(x1));
4878     __ Mov(x1, name);
4879     __ Mov(x0, elements);
4880   } else {
4881     __ Mov(x0, elements);
4882     __ Mov(x1, name);
4883   }
4884
4885   Label not_found;
4886   NameDictionaryLookupStub stub(masm->isolate(), POSITIVE_LOOKUP);
4887   __ CallStub(&stub);
4888   __ Cbz(x0, &not_found);
4889   __ Mov(scratch2, x2);  // Move entry index into scratch2.
4890   __ PopCPURegList(spill_list);
4891   __ B(done);
4892
4893   __ Bind(&not_found);
4894   __ PopCPURegList(spill_list);
4895   __ B(miss);
4896 }
4897
4898
4899 void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
4900                                                       Label* miss,
4901                                                       Label* done,
4902                                                       Register receiver,
4903                                                       Register properties,
4904                                                       Handle<Name> name,
4905                                                       Register scratch0) {
4906   DCHECK(!AreAliased(receiver, properties, scratch0));
4907   DCHECK(name->IsUniqueName());
4908   // If names of slots in range from 1 to kProbes - 1 for the hash value are
4909   // not equal to the name and kProbes-th slot is not used (its name is the
4910   // undefined value), it guarantees the hash table doesn't contain the
4911   // property. It's true even if some slots represent deleted properties
4912   // (their names are the hole value).
4913   for (int i = 0; i < kInlinedProbes; i++) {
4914     // scratch0 points to properties hash.
4915     // Compute the masked index: (hash + i + i * i) & mask.
4916     Register index = scratch0;
4917     // Capacity is smi 2^n.
4918     __ Ldrsw(index, UntagSmiFieldMemOperand(properties, kCapacityOffset));
4919     __ Sub(index, index, 1);
4920     __ And(index, index, name->Hash() + NameDictionary::GetProbeOffset(i));
4921
4922     // Scale the index by multiplying by the entry size.
4923     DCHECK(NameDictionary::kEntrySize == 3);
4924     __ Add(index, index, Operand(index, LSL, 1));  // index *= 3.
4925
4926     Register entity_name = scratch0;
4927     // Having undefined at this place means the name is not contained.
4928     Register tmp = index;
4929     __ Add(tmp, properties, Operand(index, LSL, kPointerSizeLog2));
4930     __ Ldr(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
4931
4932     __ JumpIfRoot(entity_name, Heap::kUndefinedValueRootIndex, done);
4933
4934     // Stop if found the property.
4935     __ Cmp(entity_name, Operand(name));
4936     __ B(eq, miss);
4937
4938     Label good;
4939     __ JumpIfRoot(entity_name, Heap::kTheHoleValueRootIndex, &good);
4940
4941     // Check if the entry name is not a unique name.
4942     __ Ldr(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
4943     __ Ldrb(entity_name,
4944             FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
4945     __ JumpIfNotUniqueNameInstanceType(entity_name, miss);
4946     __ Bind(&good);
4947   }
4948
4949   CPURegList spill_list(CPURegister::kRegister, kXRegSizeInBits, 0, 6);
4950   spill_list.Combine(lr);
4951   spill_list.Remove(scratch0);  // Scratch registers don't need to be preserved.
4952
4953   __ PushCPURegList(spill_list);
4954
4955   __ Ldr(x0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
4956   __ Mov(x1, Operand(name));
4957   NameDictionaryLookupStub stub(masm->isolate(), NEGATIVE_LOOKUP);
4958   __ CallStub(&stub);
4959   // Move stub return value to scratch0. Note that scratch0 is not included in
4960   // spill_list and won't be clobbered by PopCPURegList.
4961   __ Mov(scratch0, x0);
4962   __ PopCPURegList(spill_list);
4963
4964   __ Cbz(scratch0, done);
4965   __ B(miss);
4966 }
4967
4968
4969 void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
4970   // This stub overrides SometimesSetsUpAFrame() to return false. That means
4971   // we cannot call anything that could cause a GC from this stub.
4972   //
4973   // Arguments are in x0 and x1:
4974   //   x0: property dictionary.
4975   //   x1: the name of the property we are looking for.
4976   //
4977   // Return value is in x0 and is zero if lookup failed, non zero otherwise.
4978   // If the lookup is successful, x2 will contains the index of the entry.
4979
4980   Register result = x0;
4981   Register dictionary = x0;
4982   Register key = x1;
4983   Register index = x2;
4984   Register mask = x3;
4985   Register hash = x4;
4986   Register undefined = x5;
4987   Register entry_key = x6;
4988
4989   Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
4990
4991   __ Ldrsw(mask, UntagSmiFieldMemOperand(dictionary, kCapacityOffset));
4992   __ Sub(mask, mask, 1);
4993
4994   __ Ldr(hash, FieldMemOperand(key, Name::kHashFieldOffset));
4995   __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
4996
4997   for (int i = kInlinedProbes; i < kTotalProbes; i++) {
4998     // Compute the masked index: (hash + i + i * i) & mask.
4999     // Capacity is smi 2^n.
5000     if (i > 0) {
5001       // Add the probe offset (i + i * i) left shifted to avoid right shifting
5002       // the hash in a separate instruction. The value hash + i + i * i is right
5003       // shifted in the following and instruction.
5004       DCHECK(NameDictionary::GetProbeOffset(i) <
5005              1 << (32 - Name::kHashFieldOffset));
5006       __ Add(index, hash,
5007              NameDictionary::GetProbeOffset(i) << Name::kHashShift);
5008     } else {
5009       __ Mov(index, hash);
5010     }
5011     __ And(index, mask, Operand(index, LSR, Name::kHashShift));
5012
5013     // Scale the index by multiplying by the entry size.
5014     DCHECK(NameDictionary::kEntrySize == 3);
5015     __ Add(index, index, Operand(index, LSL, 1));  // index *= 3.
5016
5017     __ Add(index, dictionary, Operand(index, LSL, kPointerSizeLog2));
5018     __ Ldr(entry_key, FieldMemOperand(index, kElementsStartOffset));
5019
5020     // Having undefined at this place means the name is not contained.
5021     __ Cmp(entry_key, undefined);
5022     __ B(eq, &not_in_dictionary);
5023
5024     // Stop if found the property.
5025     __ Cmp(entry_key, key);
5026     __ B(eq, &in_dictionary);
5027
5028     if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
5029       // Check if the entry name is not a unique name.
5030       __ Ldr(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
5031       __ Ldrb(entry_key, FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
5032       __ JumpIfNotUniqueNameInstanceType(entry_key, &maybe_in_dictionary);
5033     }
5034   }
5035
5036   __ Bind(&maybe_in_dictionary);
5037   // If we are doing negative lookup then probing failure should be
5038   // treated as a lookup success. For positive lookup, probing failure
5039   // should be treated as lookup failure.
5040   if (mode() == POSITIVE_LOOKUP) {
5041     __ Mov(result, 0);
5042     __ Ret();
5043   }
5044
5045   __ Bind(&in_dictionary);
5046   __ Mov(result, 1);
5047   __ Ret();
5048
5049   __ Bind(&not_in_dictionary);
5050   __ Mov(result, 0);
5051   __ Ret();
5052 }
5053
5054
5055 template<class T>
5056 static void CreateArrayDispatch(MacroAssembler* masm,
5057                                 AllocationSiteOverrideMode mode) {
5058   ASM_LOCATION("CreateArrayDispatch");
5059   if (mode == DISABLE_ALLOCATION_SITES) {
5060     T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
5061      __ TailCallStub(&stub);
5062
5063   } else if (mode == DONT_OVERRIDE) {
5064     Register kind = x3;
5065     int last_index =
5066         GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
5067     for (int i = 0; i <= last_index; ++i) {
5068       Label next;
5069       ElementsKind candidate_kind = GetFastElementsKindFromSequenceIndex(i);
5070       // TODO(jbramley): Is this the best way to handle this? Can we make the
5071       // tail calls conditional, rather than hopping over each one?
5072       __ CompareAndBranch(kind, candidate_kind, ne, &next);
5073       T stub(masm->isolate(), candidate_kind);
5074       __ TailCallStub(&stub);
5075       __ Bind(&next);
5076     }
5077
5078     // If we reached this point there is a problem.
5079     __ Abort(kUnexpectedElementsKindInArrayConstructor);
5080
5081   } else {
5082     UNREACHABLE();
5083   }
5084 }
5085
5086
5087 // TODO(jbramley): If this needs to be a special case, make it a proper template
5088 // specialization, and not a separate function.
5089 static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
5090                                            AllocationSiteOverrideMode mode) {
5091   ASM_LOCATION("CreateArrayDispatchOneArgument");
5092   // x0 - argc
5093   // x1 - constructor?
5094   // x2 - allocation site (if mode != DISABLE_ALLOCATION_SITES)
5095   // x3 - kind (if mode != DISABLE_ALLOCATION_SITES)
5096   // sp[0] - last argument
5097
5098   Register allocation_site = x2;
5099   Register kind = x3;
5100
5101   Label normal_sequence;
5102   if (mode == DONT_OVERRIDE) {
5103     STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
5104     STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
5105     STATIC_ASSERT(FAST_ELEMENTS == 2);
5106     STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
5107     STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
5108     STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
5109
5110     // Is the low bit set? If so, the array is holey.
5111     __ Tbnz(kind, 0, &normal_sequence);
5112   }
5113
5114   // Look at the last argument.
5115   // TODO(jbramley): What does a 0 argument represent?
5116   __ Peek(x10, 0);
5117   __ Cbz(x10, &normal_sequence);
5118
5119   if (mode == DISABLE_ALLOCATION_SITES) {
5120     ElementsKind initial = GetInitialFastElementsKind();
5121     ElementsKind holey_initial = GetHoleyElementsKind(initial);
5122
5123     ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
5124                                                   holey_initial,
5125                                                   DISABLE_ALLOCATION_SITES);
5126     __ TailCallStub(&stub_holey);
5127
5128     __ Bind(&normal_sequence);
5129     ArraySingleArgumentConstructorStub stub(masm->isolate(),
5130                                             initial,
5131                                             DISABLE_ALLOCATION_SITES);
5132     __ TailCallStub(&stub);
5133   } else if (mode == DONT_OVERRIDE) {
5134     // We are going to create a holey array, but our kind is non-holey.
5135     // Fix kind and retry (only if we have an allocation site in the slot).
5136     __ Orr(kind, kind, 1);
5137
5138     if (FLAG_debug_code) {
5139       __ Ldr(x10, FieldMemOperand(allocation_site, 0));
5140       __ JumpIfNotRoot(x10, Heap::kAllocationSiteMapRootIndex,
5141                        &normal_sequence);
5142       __ Assert(eq, kExpectedAllocationSite);
5143     }
5144
5145     // Save the resulting elements kind in type info. We can't just store 'kind'
5146     // in the AllocationSite::transition_info field because elements kind is
5147     // restricted to a portion of the field; upper bits need to be left alone.
5148     STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
5149     __ Ldr(x11, FieldMemOperand(allocation_site,
5150                                 AllocationSite::kTransitionInfoOffset));
5151     __ Add(x11, x11, Smi::FromInt(kFastElementsKindPackedToHoley));
5152     __ Str(x11, FieldMemOperand(allocation_site,
5153                                 AllocationSite::kTransitionInfoOffset));
5154
5155     __ Bind(&normal_sequence);
5156     int last_index =
5157         GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
5158     for (int i = 0; i <= last_index; ++i) {
5159       Label next;
5160       ElementsKind candidate_kind = GetFastElementsKindFromSequenceIndex(i);
5161       __ CompareAndBranch(kind, candidate_kind, ne, &next);
5162       ArraySingleArgumentConstructorStub stub(masm->isolate(), candidate_kind);
5163       __ TailCallStub(&stub);
5164       __ Bind(&next);
5165     }
5166
5167     // If we reached this point there is a problem.
5168     __ Abort(kUnexpectedElementsKindInArrayConstructor);
5169   } else {
5170     UNREACHABLE();
5171   }
5172 }
5173
5174
5175 template<class T>
5176 static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
5177   int to_index = GetSequenceIndexFromFastElementsKind(
5178       TERMINAL_FAST_ELEMENTS_KIND);
5179   for (int i = 0; i <= to_index; ++i) {
5180     ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
5181     T stub(isolate, kind);
5182     stub.GetCode();
5183     if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
5184       T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
5185       stub1.GetCode();
5186     }
5187   }
5188 }
5189
5190
5191 void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
5192   ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
5193       isolate);
5194   ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
5195       isolate);
5196   ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
5197       isolate);
5198 }
5199
5200
5201 void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
5202     Isolate* isolate) {
5203   ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
5204   for (int i = 0; i < 2; i++) {
5205     // For internal arrays we only need a few things
5206     InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
5207     stubh1.GetCode();
5208     InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
5209     stubh2.GetCode();
5210     InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
5211     stubh3.GetCode();
5212   }
5213 }
5214
5215
5216 void ArrayConstructorStub::GenerateDispatchToArrayStub(
5217     MacroAssembler* masm,
5218     AllocationSiteOverrideMode mode) {
5219   Register argc = x0;
5220   if (argument_count() == ANY) {
5221     Label zero_case, n_case;
5222     __ Cbz(argc, &zero_case);
5223     __ Cmp(argc, 1);
5224     __ B(ne, &n_case);
5225
5226     // One argument.
5227     CreateArrayDispatchOneArgument(masm, mode);
5228
5229     __ Bind(&zero_case);
5230     // No arguments.
5231     CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
5232
5233     __ Bind(&n_case);
5234     // N arguments.
5235     CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
5236
5237   } else if (argument_count() == NONE) {
5238     CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
5239   } else if (argument_count() == ONE) {
5240     CreateArrayDispatchOneArgument(masm, mode);
5241   } else if (argument_count() == MORE_THAN_ONE) {
5242     CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
5243   } else {
5244     UNREACHABLE();
5245   }
5246 }
5247
5248
5249 void ArrayConstructorStub::Generate(MacroAssembler* masm) {
5250   ASM_LOCATION("ArrayConstructorStub::Generate");
5251   // ----------- S t a t e -------------
5252   //  -- x0 : argc (only if argument_count() is ANY or MORE_THAN_ONE)
5253   //  -- x1 : constructor
5254   //  -- x2 : AllocationSite or undefined
5255   //  -- x3 : original constructor
5256   //  -- sp[0] : last argument
5257   // -----------------------------------
5258   Register constructor = x1;
5259   Register allocation_site = x2;
5260   Register original_constructor = x3;
5261
5262   if (FLAG_debug_code) {
5263     // The array construct code is only set for the global and natives
5264     // builtin Array functions which always have maps.
5265
5266     Label unexpected_map, map_ok;
5267     // Initial map for the builtin Array function should be a map.
5268     __ Ldr(x10, FieldMemOperand(constructor,
5269                                 JSFunction::kPrototypeOrInitialMapOffset));
5270     // Will both indicate a NULL and a Smi.
5271     __ JumpIfSmi(x10, &unexpected_map);
5272     __ JumpIfObjectType(x10, x10, x11, MAP_TYPE, &map_ok);
5273     __ Bind(&unexpected_map);
5274     __ Abort(kUnexpectedInitialMapForArrayFunction);
5275     __ Bind(&map_ok);
5276
5277     // We should either have undefined in the allocation_site register or a
5278     // valid AllocationSite.
5279     __ AssertUndefinedOrAllocationSite(allocation_site, x10);
5280   }
5281
5282   Label subclassing;
5283   __ Cmp(original_constructor, constructor);
5284   __ B(ne, &subclassing);
5285
5286   Register kind = x3;
5287   Label no_info;
5288   // Get the elements kind and case on that.
5289   __ JumpIfRoot(allocation_site, Heap::kUndefinedValueRootIndex, &no_info);
5290
5291   __ Ldrsw(kind,
5292            UntagSmiFieldMemOperand(allocation_site,
5293                                    AllocationSite::kTransitionInfoOffset));
5294   __ And(kind, kind, AllocationSite::ElementsKindBits::kMask);
5295   GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
5296
5297   __ Bind(&no_info);
5298   GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
5299
5300   // Subclassing support.
5301   __ Bind(&subclassing);
5302   __ Push(constructor, original_constructor);
5303   // Adjust argc.
5304   switch (argument_count()) {
5305     case ANY:
5306     case MORE_THAN_ONE:
5307       __ add(x0, x0, Operand(2));
5308       break;
5309     case NONE:
5310       __ Mov(x0, Operand(2));
5311       break;
5312     case ONE:
5313       __ Mov(x0, Operand(3));
5314       break;
5315   }
5316   __ JumpToExternalReference(
5317       ExternalReference(Runtime::kArrayConstructorWithSubclassing, isolate()));
5318 }
5319
5320
5321 void InternalArrayConstructorStub::GenerateCase(
5322     MacroAssembler* masm, ElementsKind kind) {
5323   Label zero_case, n_case;
5324   Register argc = x0;
5325
5326   __ Cbz(argc, &zero_case);
5327   __ CompareAndBranch(argc, 1, ne, &n_case);
5328
5329   // One argument.
5330   if (IsFastPackedElementsKind(kind)) {
5331     Label packed_case;
5332
5333     // We might need to create a holey array; look at the first argument.
5334     __ Peek(x10, 0);
5335     __ Cbz(x10, &packed_case);
5336
5337     InternalArraySingleArgumentConstructorStub
5338         stub1_holey(isolate(), GetHoleyElementsKind(kind));
5339     __ TailCallStub(&stub1_holey);
5340
5341     __ Bind(&packed_case);
5342   }
5343   InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
5344   __ TailCallStub(&stub1);
5345
5346   __ Bind(&zero_case);
5347   // No arguments.
5348   InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
5349   __ TailCallStub(&stub0);
5350
5351   __ Bind(&n_case);
5352   // N arguments.
5353   InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
5354   __ TailCallStub(&stubN);
5355 }
5356
5357
5358 void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
5359   // ----------- S t a t e -------------
5360   //  -- x0 : argc
5361   //  -- x1 : constructor
5362   //  -- sp[0] : return address
5363   //  -- sp[4] : last argument
5364   // -----------------------------------
5365
5366   Register constructor = x1;
5367
5368   if (FLAG_debug_code) {
5369     // The array construct code is only set for the global and natives
5370     // builtin Array functions which always have maps.
5371
5372     Label unexpected_map, map_ok;
5373     // Initial map for the builtin Array function should be a map.
5374     __ Ldr(x10, FieldMemOperand(constructor,
5375                                 JSFunction::kPrototypeOrInitialMapOffset));
5376     // Will both indicate a NULL and a Smi.
5377     __ JumpIfSmi(x10, &unexpected_map);
5378     __ JumpIfObjectType(x10, x10, x11, MAP_TYPE, &map_ok);
5379     __ Bind(&unexpected_map);
5380     __ Abort(kUnexpectedInitialMapForArrayFunction);
5381     __ Bind(&map_ok);
5382   }
5383
5384   Register kind = w3;
5385   // Figure out the right elements kind
5386   __ Ldr(x10, FieldMemOperand(constructor,
5387                               JSFunction::kPrototypeOrInitialMapOffset));
5388
5389   // Retrieve elements_kind from map.
5390   __ LoadElementsKindFromMap(kind, x10);
5391
5392   if (FLAG_debug_code) {
5393     Label done;
5394     __ Cmp(x3, FAST_ELEMENTS);
5395     __ Ccmp(x3, FAST_HOLEY_ELEMENTS, ZFlag, ne);
5396     __ Assert(eq, kInvalidElementsKindForInternalArrayOrInternalPackedArray);
5397   }
5398
5399   Label fast_elements_case;
5400   __ CompareAndBranch(kind, FAST_ELEMENTS, eq, &fast_elements_case);
5401   GenerateCase(masm, FAST_HOLEY_ELEMENTS);
5402
5403   __ Bind(&fast_elements_case);
5404   GenerateCase(masm, FAST_ELEMENTS);
5405 }
5406
5407
5408 // The number of register that CallApiFunctionAndReturn will need to save on
5409 // the stack. The space for these registers need to be allocated in the
5410 // ExitFrame before calling CallApiFunctionAndReturn.
5411 static const int kCallApiFunctionSpillSpace = 4;
5412
5413
5414 static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
5415   return ref0.address() - ref1.address();
5416 }
5417
5418
5419 // Calls an API function. Allocates HandleScope, extracts returned value
5420 // from handle and propagates exceptions.
5421 // 'stack_space' is the space to be unwound on exit (includes the call JS
5422 // arguments space and the additional space allocated for the fast call).
5423 // 'spill_offset' is the offset from the stack pointer where
5424 // CallApiFunctionAndReturn can spill registers.
5425 static void CallApiFunctionAndReturn(
5426     MacroAssembler* masm, Register function_address,
5427     ExternalReference thunk_ref, int stack_space,
5428     MemOperand* stack_space_operand, int spill_offset,
5429     MemOperand return_value_operand, MemOperand* context_restore_operand) {
5430   ASM_LOCATION("CallApiFunctionAndReturn");
5431   Isolate* isolate = masm->isolate();
5432   ExternalReference next_address =
5433       ExternalReference::handle_scope_next_address(isolate);
5434   const int kNextOffset = 0;
5435   const int kLimitOffset = AddressOffset(
5436       ExternalReference::handle_scope_limit_address(isolate), next_address);
5437   const int kLevelOffset = AddressOffset(
5438       ExternalReference::handle_scope_level_address(isolate), next_address);
5439
5440   DCHECK(function_address.is(x1) || function_address.is(x2));
5441
5442   Label profiler_disabled;
5443   Label end_profiler_check;
5444   __ Mov(x10, ExternalReference::is_profiling_address(isolate));
5445   __ Ldrb(w10, MemOperand(x10));
5446   __ Cbz(w10, &profiler_disabled);
5447   __ Mov(x3, thunk_ref);
5448   __ B(&end_profiler_check);
5449
5450   __ Bind(&profiler_disabled);
5451   __ Mov(x3, function_address);
5452   __ Bind(&end_profiler_check);
5453
5454   // Save the callee-save registers we are going to use.
5455   // TODO(all): Is this necessary? ARM doesn't do it.
5456   STATIC_ASSERT(kCallApiFunctionSpillSpace == 4);
5457   __ Poke(x19, (spill_offset + 0) * kXRegSize);
5458   __ Poke(x20, (spill_offset + 1) * kXRegSize);
5459   __ Poke(x21, (spill_offset + 2) * kXRegSize);
5460   __ Poke(x22, (spill_offset + 3) * kXRegSize);
5461
5462   // Allocate HandleScope in callee-save registers.
5463   // We will need to restore the HandleScope after the call to the API function,
5464   // by allocating it in callee-save registers they will be preserved by C code.
5465   Register handle_scope_base = x22;
5466   Register next_address_reg = x19;
5467   Register limit_reg = x20;
5468   Register level_reg = w21;
5469
5470   __ Mov(handle_scope_base, next_address);
5471   __ Ldr(next_address_reg, MemOperand(handle_scope_base, kNextOffset));
5472   __ Ldr(limit_reg, MemOperand(handle_scope_base, kLimitOffset));
5473   __ Ldr(level_reg, MemOperand(handle_scope_base, kLevelOffset));
5474   __ Add(level_reg, level_reg, 1);
5475   __ Str(level_reg, MemOperand(handle_scope_base, kLevelOffset));
5476
5477   if (FLAG_log_timer_events) {
5478     FrameScope frame(masm, StackFrame::MANUAL);
5479     __ PushSafepointRegisters();
5480     __ Mov(x0, ExternalReference::isolate_address(isolate));
5481     __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5482                      1);
5483     __ PopSafepointRegisters();
5484   }
5485
5486   // Native call returns to the DirectCEntry stub which redirects to the
5487   // return address pushed on stack (could have moved after GC).
5488   // DirectCEntry stub itself is generated early and never moves.
5489   DirectCEntryStub stub(isolate);
5490   stub.GenerateCall(masm, x3);
5491
5492   if (FLAG_log_timer_events) {
5493     FrameScope frame(masm, StackFrame::MANUAL);
5494     __ PushSafepointRegisters();
5495     __ Mov(x0, ExternalReference::isolate_address(isolate));
5496     __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5497                      1);
5498     __ PopSafepointRegisters();
5499   }
5500
5501   Label promote_scheduled_exception;
5502   Label delete_allocated_handles;
5503   Label leave_exit_frame;
5504   Label return_value_loaded;
5505
5506   // Load value from ReturnValue.
5507   __ Ldr(x0, return_value_operand);
5508   __ Bind(&return_value_loaded);
5509   // No more valid handles (the result handle was the last one). Restore
5510   // previous handle scope.
5511   __ Str(next_address_reg, MemOperand(handle_scope_base, kNextOffset));
5512   if (__ emit_debug_code()) {
5513     __ Ldr(w1, MemOperand(handle_scope_base, kLevelOffset));
5514     __ Cmp(w1, level_reg);
5515     __ Check(eq, kUnexpectedLevelAfterReturnFromApiCall);
5516   }
5517   __ Sub(level_reg, level_reg, 1);
5518   __ Str(level_reg, MemOperand(handle_scope_base, kLevelOffset));
5519   __ Ldr(x1, MemOperand(handle_scope_base, kLimitOffset));
5520   __ Cmp(limit_reg, x1);
5521   __ B(ne, &delete_allocated_handles);
5522
5523   // Leave the API exit frame.
5524   __ Bind(&leave_exit_frame);
5525   // Restore callee-saved registers.
5526   __ Peek(x19, (spill_offset + 0) * kXRegSize);
5527   __ Peek(x20, (spill_offset + 1) * kXRegSize);
5528   __ Peek(x21, (spill_offset + 2) * kXRegSize);
5529   __ Peek(x22, (spill_offset + 3) * kXRegSize);
5530
5531   bool restore_context = context_restore_operand != NULL;
5532   if (restore_context) {
5533     __ Ldr(cp, *context_restore_operand);
5534   }
5535
5536   if (stack_space_operand != NULL) {
5537     __ Ldr(w2, *stack_space_operand);
5538   }
5539
5540   __ LeaveExitFrame(false, x1, !restore_context);
5541
5542   // Check if the function scheduled an exception.
5543   __ Mov(x5, ExternalReference::scheduled_exception_address(isolate));
5544   __ Ldr(x5, MemOperand(x5));
5545   __ JumpIfNotRoot(x5, Heap::kTheHoleValueRootIndex,
5546                    &promote_scheduled_exception);
5547
5548   if (stack_space_operand != NULL) {
5549     __ Drop(x2, 1);
5550   } else {
5551     __ Drop(stack_space);
5552   }
5553   __ Ret();
5554
5555   // Re-throw by promoting a scheduled exception.
5556   __ Bind(&promote_scheduled_exception);
5557   __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
5558
5559   // HandleScope limit has changed. Delete allocated extensions.
5560   __ Bind(&delete_allocated_handles);
5561   __ Str(limit_reg, MemOperand(handle_scope_base, kLimitOffset));
5562   // Save the return value in a callee-save register.
5563   Register saved_result = x19;
5564   __ Mov(saved_result, x0);
5565   __ Mov(x0, ExternalReference::isolate_address(isolate));
5566   __ CallCFunction(ExternalReference::delete_handle_scope_extensions(isolate),
5567                    1);
5568   __ Mov(x0, saved_result);
5569   __ B(&leave_exit_frame);
5570 }
5571
5572
5573 static void CallApiFunctionStubHelper(MacroAssembler* masm,
5574                                       const ParameterCount& argc,
5575                                       bool return_first_arg,
5576                                       bool call_data_undefined) {
5577   // ----------- S t a t e -------------
5578   //  -- x0                  : callee
5579   //  -- x4                  : call_data
5580   //  -- x2                  : holder
5581   //  -- x1                  : api_function_address
5582   //  -- x3                  : number of arguments if argc is a register
5583   //  -- cp                  : context
5584   //  --
5585   //  -- sp[0]               : last argument
5586   //  -- ...
5587   //  -- sp[(argc - 1) * 8]  : first argument
5588   //  -- sp[argc * 8]        : receiver
5589   // -----------------------------------
5590
5591   Register callee = x0;
5592   Register call_data = x4;
5593   Register holder = x2;
5594   Register api_function_address = x1;
5595   Register context = cp;
5596
5597   typedef FunctionCallbackArguments FCA;
5598
5599   STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5600   STATIC_ASSERT(FCA::kCalleeIndex == 5);
5601   STATIC_ASSERT(FCA::kDataIndex == 4);
5602   STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5603   STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5604   STATIC_ASSERT(FCA::kIsolateIndex == 1);
5605   STATIC_ASSERT(FCA::kHolderIndex == 0);
5606   STATIC_ASSERT(FCA::kArgsLength == 7);
5607
5608   DCHECK(argc.is_immediate() || x3.is(argc.reg()));
5609
5610   // FunctionCallbackArguments: context, callee and call data.
5611   __ Push(context, callee, call_data);
5612
5613   // Load context from callee
5614   __ Ldr(context, FieldMemOperand(callee, JSFunction::kContextOffset));
5615
5616   if (!call_data_undefined) {
5617     __ LoadRoot(call_data, Heap::kUndefinedValueRootIndex);
5618   }
5619   Register isolate_reg = x5;
5620   __ Mov(isolate_reg, ExternalReference::isolate_address(masm->isolate()));
5621
5622   // FunctionCallbackArguments:
5623   //    return value, return value default, isolate, holder.
5624   __ Push(call_data, call_data, isolate_reg, holder);
5625
5626   // Prepare arguments.
5627   Register args = x6;
5628   __ Mov(args, masm->StackPointer());
5629
5630   // Allocate the v8::Arguments structure in the arguments' space, since it's
5631   // not controlled by GC.
5632   const int kApiStackSpace = 4;
5633
5634   // Allocate space for CallApiFunctionAndReturn can store some scratch
5635   // registeres on the stack.
5636   const int kCallApiFunctionSpillSpace = 4;
5637
5638   FrameScope frame_scope(masm, StackFrame::MANUAL);
5639   __ EnterExitFrame(false, x10, kApiStackSpace + kCallApiFunctionSpillSpace);
5640
5641   DCHECK(!AreAliased(x0, api_function_address));
5642   // x0 = FunctionCallbackInfo&
5643   // Arguments is after the return address.
5644   __ Add(x0, masm->StackPointer(), 1 * kPointerSize);
5645   if (argc.is_immediate()) {
5646     // FunctionCallbackInfo::implicit_args_ and FunctionCallbackInfo::values_
5647     __ Add(x10, args,
5648            Operand((FCA::kArgsLength - 1 + argc.immediate()) * kPointerSize));
5649     __ Stp(args, x10, MemOperand(x0, 0 * kPointerSize));
5650     // FunctionCallbackInfo::length_ = argc and
5651     // FunctionCallbackInfo::is_construct_call = 0
5652     __ Mov(x10, argc.immediate());
5653     __ Stp(x10, xzr, MemOperand(x0, 2 * kPointerSize));
5654   } else {
5655     // FunctionCallbackInfo::implicit_args_ and FunctionCallbackInfo::values_
5656     __ Add(x10, args, Operand(argc.reg(), LSL, kPointerSizeLog2));
5657     __ Add(x10, x10, (FCA::kArgsLength - 1) * kPointerSize);
5658     __ Stp(args, x10, MemOperand(x0, 0 * kPointerSize));
5659     // FunctionCallbackInfo::length_ = argc and
5660     // FunctionCallbackInfo::is_construct_call
5661     __ Add(x10, argc.reg(), FCA::kArgsLength + 1);
5662     __ Mov(x10, Operand(x10, LSL, kPointerSizeLog2));
5663     __ Stp(argc.reg(), x10, MemOperand(x0, 2 * kPointerSize));
5664   }
5665
5666   ExternalReference thunk_ref =
5667       ExternalReference::invoke_function_callback(masm->isolate());
5668
5669   AllowExternalCallThatCantCauseGC scope(masm);
5670   MemOperand context_restore_operand(
5671       fp, (2 + FCA::kContextSaveIndex) * kPointerSize);
5672   // Stores return the first js argument
5673   int return_value_offset = 0;
5674   if (return_first_arg) {
5675     return_value_offset = 2 + FCA::kArgsLength;
5676   } else {
5677     return_value_offset = 2 + FCA::kReturnValueOffset;
5678   }
5679   MemOperand return_value_operand(fp, return_value_offset * kPointerSize);
5680   int stack_space = 0;
5681   MemOperand is_construct_call_operand =
5682       MemOperand(masm->StackPointer(), 4 * kPointerSize);
5683   MemOperand* stack_space_operand = &is_construct_call_operand;
5684   if (argc.is_immediate()) {
5685     stack_space = argc.immediate() + FCA::kArgsLength + 1;
5686     stack_space_operand = NULL;
5687   }
5688
5689   const int spill_offset = 1 + kApiStackSpace;
5690   CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, stack_space,
5691                            stack_space_operand, spill_offset,
5692                            return_value_operand, &context_restore_operand);
5693 }
5694
5695
5696 void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5697   bool call_data_undefined = this->call_data_undefined();
5698   CallApiFunctionStubHelper(masm, ParameterCount(x3), false,
5699                             call_data_undefined);
5700 }
5701
5702
5703 void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5704   bool is_store = this->is_store();
5705   int argc = this->argc();
5706   bool call_data_undefined = this->call_data_undefined();
5707   CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
5708                             call_data_undefined);
5709 }
5710
5711
5712 void CallApiGetterStub::Generate(MacroAssembler* masm) {
5713   // ----------- S t a t e -------------
5714   //  -- sp[0]                  : name
5715   //  -- sp[8 - kArgsLength*8]  : PropertyCallbackArguments object
5716   //  -- ...
5717   //  -- x2                     : api_function_address
5718   // -----------------------------------
5719
5720   Register api_function_address = ApiGetterDescriptor::function_address();
5721   DCHECK(api_function_address.is(x2));
5722
5723   __ Mov(x0, masm->StackPointer());  // x0 = Handle<Name>
5724   __ Add(x1, x0, 1 * kPointerSize);  // x1 = PCA
5725
5726   const int kApiStackSpace = 1;
5727
5728   // Allocate space for CallApiFunctionAndReturn can store some scratch
5729   // registeres on the stack.
5730   const int kCallApiFunctionSpillSpace = 4;
5731
5732   FrameScope frame_scope(masm, StackFrame::MANUAL);
5733   __ EnterExitFrame(false, x10, kApiStackSpace + kCallApiFunctionSpillSpace);
5734
5735   // Create PropertyAccessorInfo instance on the stack above the exit frame with
5736   // x1 (internal::Object** args_) as the data.
5737   __ Poke(x1, 1 * kPointerSize);
5738   __ Add(x1, masm->StackPointer(), 1 * kPointerSize);  // x1 = AccessorInfo&
5739
5740   const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5741
5742   ExternalReference thunk_ref =
5743       ExternalReference::invoke_accessor_getter_callback(isolate());
5744
5745   const int spill_offset = 1 + kApiStackSpace;
5746   CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5747                            kStackUnwindSpace, NULL, spill_offset,
5748                            MemOperand(fp, 6 * kPointerSize), NULL);
5749 }
5750
5751
5752 #undef __
5753
5754 } }  // namespace v8::internal
5755
5756 #endif  // V8_TARGET_ARCH_ARM64