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.
5 #if V8_TARGET_ARCH_ARM64
7 #include "src/arm64/frames-arm64.h"
8 #include "src/codegen.h"
9 #include "src/debug/debug.h"
10 #include "src/deoptimizer.h"
11 #include "src/full-codegen/full-codegen.h"
12 #include "src/runtime/runtime.h"
18 #define __ ACCESS_MASM(masm)
21 // Load the built-in Array function from the current context.
22 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
23 // Load the native context.
24 __ Ldr(result, GlobalObjectMemOperand());
26 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
27 // Load the InternalArray function from the native context.
30 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
34 // Load the built-in InternalArray function from the current context.
35 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
37 // Load the native context.
38 __ Ldr(result, GlobalObjectMemOperand());
40 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
41 // Load the InternalArray function from the native context.
42 __ Ldr(result, ContextMemOperand(result,
43 Context::INTERNAL_ARRAY_FUNCTION_INDEX));
47 void Builtins::Generate_Adaptor(MacroAssembler* masm,
49 BuiltinExtraArguments extra_args) {
50 // ----------- S t a t e -------------
51 // -- x0 : number of arguments excluding receiver
52 // -- x1 : called function (only guaranteed when
53 // extra_args requires it)
55 // -- sp[0] : last argument
57 // -- sp[4 * (argc - 1)] : first argument (argc == x0)
58 // -- sp[4 * argc] : receiver
59 // -----------------------------------
61 // Insert extra arguments.
62 int num_extra_args = 0;
63 if (extra_args == NEEDS_CALLED_FUNCTION) {
67 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
70 // JumpToExternalReference expects x0 to contain the number of arguments
71 // including the receiver and the extra arguments.
72 __ Add(x0, x0, num_extra_args + 1);
73 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
77 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
78 // ----------- S t a t e -------------
79 // -- x0 : number of arguments
80 // -- lr : return address
81 // -- sp[...]: constructor arguments
82 // -----------------------------------
83 ASM_LOCATION("Builtins::Generate_InternalArrayCode");
84 Label generic_array_code;
86 // Get the InternalArray function.
87 GenerateLoadInternalArrayFunction(masm, x1);
89 if (FLAG_debug_code) {
90 // Initial map for the builtin InternalArray functions should be maps.
91 __ Ldr(x10, FieldMemOperand(x1, JSFunction::kPrototypeOrInitialMapOffset));
92 __ Tst(x10, kSmiTagMask);
93 __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction);
94 __ CompareObjectType(x10, x11, x12, MAP_TYPE);
95 __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction);
98 // Run the native code for the InternalArray function called as a normal
100 InternalArrayConstructorStub stub(masm->isolate());
101 __ TailCallStub(&stub);
105 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
106 // ----------- S t a t e -------------
107 // -- x0 : number of arguments
108 // -- lr : return address
109 // -- sp[...]: constructor arguments
110 // -----------------------------------
111 ASM_LOCATION("Builtins::Generate_ArrayCode");
112 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
114 // Get the Array function.
115 GenerateLoadArrayFunction(masm, x1);
117 if (FLAG_debug_code) {
118 // Initial map for the builtin Array functions should be maps.
119 __ Ldr(x10, FieldMemOperand(x1, JSFunction::kPrototypeOrInitialMapOffset));
120 __ Tst(x10, kSmiTagMask);
121 __ Assert(ne, kUnexpectedInitialMapForArrayFunction);
122 __ CompareObjectType(x10, x11, x12, MAP_TYPE);
123 __ Assert(eq, kUnexpectedInitialMapForArrayFunction);
126 // Run the native code for the Array function called as a normal function.
127 __ LoadRoot(x2, Heap::kUndefinedValueRootIndex);
129 ArrayConstructorStub stub(masm->isolate());
130 __ TailCallStub(&stub);
134 void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
135 // ----------- S t a t e -------------
136 // -- x0 : number of arguments
137 // -- x1 : constructor function
138 // -- lr : return address
139 // -- sp[(argc - n - 1) * 8] : arg[n] (zero based)
140 // -- sp[argc * 8] : receiver
141 // -----------------------------------
142 ASM_LOCATION("Builtins::Generate_StringConstructCode");
143 Counters* counters = masm->isolate()->counters();
144 __ IncrementCounter(counters->string_ctor_calls(), 1, x10, x11);
147 Register function = x1;
148 if (FLAG_debug_code) {
149 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, x10);
150 __ Cmp(function, x10);
151 __ Assert(eq, kUnexpectedStringFunction);
154 // Load the first arguments in x0 and get rid of the rest.
156 __ Cbz(argc, &no_arguments);
157 // First args = sp[(argc - 1) * 8].
158 __ Sub(argc, argc, 1);
159 __ Drop(argc, kXRegSize);
160 // jssp now point to args[0], load and drop args[0] + receiver.
162 __ Ldr(arg, MemOperand(jssp, 2 * kPointerSize, PostIndex));
165 Register argument = x2;
166 Label not_cached, argument_is_string;
167 __ LookupNumberStringCache(arg, // Input.
173 __ IncrementCounter(counters->string_ctor_cached_number(), 1, x10, x11);
174 __ Bind(&argument_is_string);
176 // ----------- S t a t e -------------
177 // -- x2 : argument converted to string
178 // -- x1 : constructor function
179 // -- lr : return address
180 // -----------------------------------
183 Register new_obj = x0;
184 __ Allocate(JSValue::kSize, new_obj, x10, x11, &gc_required, TAG_OBJECT);
186 // Initialize the String object.
188 __ LoadGlobalFunctionInitialMap(function, map, x10);
189 if (FLAG_debug_code) {
190 __ Ldrb(x4, FieldMemOperand(map, Map::kInstanceSizeOffset));
191 __ Cmp(x4, JSValue::kSize >> kPointerSizeLog2);
192 __ Assert(eq, kUnexpectedStringWrapperInstanceSize);
193 __ Ldrb(x4, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
195 __ Assert(eq, kUnexpectedUnusedPropertiesOfStringWrapper);
197 __ Str(map, FieldMemOperand(new_obj, HeapObject::kMapOffset));
200 __ LoadRoot(empty, Heap::kEmptyFixedArrayRootIndex);
201 __ Str(empty, FieldMemOperand(new_obj, JSObject::kPropertiesOffset));
202 __ Str(empty, FieldMemOperand(new_obj, JSObject::kElementsOffset));
204 __ Str(argument, FieldMemOperand(new_obj, JSValue::kValueOffset));
206 // Ensure the object is fully initialized.
207 STATIC_ASSERT(JSValue::kSize == (4 * kPointerSize));
211 // The argument was not found in the number to string cache. Check
212 // if it's a string already before calling the conversion builtin.
213 Label convert_argument;
214 __ Bind(¬_cached);
215 __ JumpIfSmi(arg, &convert_argument);
218 __ Ldr(x10, FieldMemOperand(x0, HeapObject::kMapOffset));
219 __ Ldrb(x11, FieldMemOperand(x10, Map::kInstanceTypeOffset));
220 __ Tbnz(x11, MaskToBit(kIsNotStringMask), &convert_argument);
221 __ Mov(argument, arg);
222 __ IncrementCounter(counters->string_ctor_string_value(), 1, x10, x11);
223 __ B(&argument_is_string);
225 // Invoke the conversion builtin and put the result into x2.
226 __ Bind(&convert_argument);
227 __ Push(function); // Preserve the function.
228 __ IncrementCounter(counters->string_ctor_conversions(), 1, x10, x11);
230 FrameScope scope(masm, StackFrame::INTERNAL);
231 ToStringStub stub(masm->isolate());
235 __ Mov(argument, x0);
236 __ B(&argument_is_string);
238 // Load the empty string into x2, remove the receiver from the
239 // stack, and jump back to the case where the argument is a string.
240 __ Bind(&no_arguments);
241 __ LoadRoot(argument, Heap::kempty_stringRootIndex);
243 __ B(&argument_is_string);
245 // At this point the argument is already a string. Call runtime to create a
247 __ Bind(&gc_required);
248 __ IncrementCounter(counters->string_ctor_gc_required(), 1, x10, x11);
250 FrameScope scope(masm, StackFrame::INTERNAL);
252 __ CallRuntime(Runtime::kNewStringWrapper, 1);
258 static void CallRuntimePassFunction(MacroAssembler* masm,
259 Runtime::FunctionId function_id) {
260 FrameScope scope(masm, StackFrame::INTERNAL);
261 // - Push a copy of the function onto the stack.
262 // - Push another copy as a parameter to the runtime call.
265 __ CallRuntime(function_id, 1);
267 // - Restore receiver.
272 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
273 __ Ldr(x2, FieldMemOperand(x1, JSFunction::kSharedFunctionInfoOffset));
274 __ Ldr(x2, FieldMemOperand(x2, SharedFunctionInfo::kCodeOffset));
275 __ Add(x2, x2, Code::kHeaderSize - kHeapObjectTag);
280 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
281 __ Add(x0, x0, Code::kHeaderSize - kHeapObjectTag);
286 void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
287 // Checking whether the queued function is ready for install is optional,
288 // since we come across interrupts and stack checks elsewhere. However, not
289 // checking may delay installing ready functions, and always checking would be
290 // quite expensive. A good compromise is to first check against stack limit as
291 // a cue for an interrupt signal.
293 __ CompareRoot(masm->StackPointer(), Heap::kStackLimitRootIndex);
296 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
297 GenerateTailCallToReturnedCode(masm);
300 GenerateTailCallToSharedCode(masm);
304 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
305 bool is_api_function,
306 bool create_memento) {
307 // ----------- S t a t e -------------
308 // -- x0 : number of arguments
309 // -- x1 : constructor function
310 // -- x2 : allocation site or undefined
311 // -- x3 : original constructor
312 // -- lr : return address
313 // -- sp[...]: constructor arguments
314 // -----------------------------------
316 ASM_LOCATION("Builtins::Generate_JSConstructStubHelper");
317 // Should never create mementos for api functions.
318 DCHECK(!is_api_function || !create_memento);
320 Isolate* isolate = masm->isolate();
322 // Enter a construct frame.
324 FrameScope scope(masm, StackFrame::CONSTRUCT);
326 // Preserve the four incoming parameters on the stack.
328 Register constructor = x1;
329 Register allocation_site = x2;
330 Register original_constructor = x3;
332 // Preserve the incoming parameters on the stack.
333 __ AssertUndefinedOrAllocationSite(allocation_site, x10);
335 __ Push(allocation_site, argc, constructor, original_constructor);
337 // sp[1]: Constructor function.
338 // sp[2]: number of arguments (smi-tagged)
339 // sp[3]: allocation site
341 // Try to allocate the object without transitioning into C code. If any of
342 // the preconditions is not met, the code bails out to the runtime call.
343 Label rt_call, allocated;
344 if (FLAG_inline_new) {
345 ExternalReference debug_step_in_fp =
346 ExternalReference::debug_step_in_fp_address(isolate);
347 __ Mov(x2, Operand(debug_step_in_fp));
348 __ Ldr(x2, MemOperand(x2));
349 __ Cbnz(x2, &rt_call);
351 // Fall back to runtime if the original constructor and function differ.
352 __ Cmp(constructor, original_constructor);
355 // Load the initial map and verify that it is in fact a map.
356 Register init_map = x2;
358 FieldMemOperand(constructor,
359 JSFunction::kPrototypeOrInitialMapOffset));
360 __ JumpIfSmi(init_map, &rt_call);
361 __ JumpIfNotObjectType(init_map, x10, x11, MAP_TYPE, &rt_call);
363 // Check that the constructor is not constructing a JSFunction (see
364 // comments in Runtime_NewObject in runtime.cc). In which case the initial
365 // map's instance type would be JS_FUNCTION_TYPE.
366 __ CompareInstanceType(init_map, x10, JS_FUNCTION_TYPE);
369 Register constructon_count = x14;
370 if (!is_api_function) {
372 MemOperand bit_field3 =
373 FieldMemOperand(init_map, Map::kBitField3Offset);
374 // Check if slack tracking is enabled.
375 __ Ldr(x4, bit_field3);
376 __ DecodeField<Map::Counter>(constructon_count, x4);
377 __ Cmp(constructon_count, Operand(Map::kSlackTrackingCounterEnd));
379 // Decrease generous allocation count.
380 __ Subs(x4, x4, Operand(1 << Map::Counter::kShift));
381 __ Str(x4, bit_field3);
382 __ Cmp(constructon_count, Operand(Map::kSlackTrackingCounterEnd));
385 // Push the constructor and map to the stack, and the constructor again
386 // as argument to the runtime call.
387 __ Push(constructor, init_map, constructor);
388 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
389 __ Pop(init_map, constructor);
390 __ Mov(constructon_count, Operand(Map::kSlackTrackingCounterEnd - 1));
394 // Now allocate the JSObject on the heap.
395 Label rt_call_reload_new_target;
396 Register obj_size = x3;
397 Register new_obj = x4;
398 __ Ldrb(obj_size, FieldMemOperand(init_map, Map::kInstanceSizeOffset));
399 if (create_memento) {
401 Operand(AllocationMemento::kSize / kPointerSize));
402 __ Allocate(x7, new_obj, x10, x11, &rt_call_reload_new_target,
405 __ Allocate(obj_size, new_obj, x10, x11, &rt_call_reload_new_target,
409 // Allocated the JSObject, now initialize the fields. Map is set to
410 // initial map and properties and elements are set to empty fixed array.
411 // NB. the object pointer is not tagged, so MemOperand is used.
413 __ LoadRoot(empty, Heap::kEmptyFixedArrayRootIndex);
414 __ Str(init_map, MemOperand(new_obj, JSObject::kMapOffset));
415 STATIC_ASSERT(JSObject::kElementsOffset ==
416 (JSObject::kPropertiesOffset + kPointerSize));
417 __ Stp(empty, empty, MemOperand(new_obj, JSObject::kPropertiesOffset));
419 Register first_prop = x5;
420 __ Add(first_prop, new_obj, JSObject::kHeaderSize);
422 // Fill all of the in-object properties with the appropriate filler.
423 Register filler = x7;
424 __ LoadRoot(filler, Heap::kUndefinedValueRootIndex);
426 // Obtain number of pre-allocated property fields and in-object
428 Register unused_props = x10;
429 Register inobject_props = x11;
430 Register inst_sizes_or_attrs = x11;
431 Register prealloc_fields = x10;
432 __ Ldr(inst_sizes_or_attrs,
433 FieldMemOperand(init_map, Map::kInstanceAttributesOffset));
434 __ Ubfx(unused_props, inst_sizes_or_attrs,
435 Map::kUnusedPropertyFieldsByte * kBitsPerByte, kBitsPerByte);
436 __ Ldr(inst_sizes_or_attrs,
437 FieldMemOperand(init_map, Map::kInstanceSizesOffset));
439 inobject_props, inst_sizes_or_attrs,
440 Map::kInObjectPropertiesOrConstructorFunctionIndexByte * kBitsPerByte,
442 __ Sub(prealloc_fields, inobject_props, unused_props);
444 // Calculate number of property fields in the object.
445 Register prop_fields = x6;
446 __ Sub(prop_fields, obj_size, JSObject::kHeaderSize / kPointerSize);
448 if (!is_api_function) {
449 Label no_inobject_slack_tracking;
451 // Check if slack tracking is enabled.
452 __ Cmp(constructon_count, Operand(Map::kSlackTrackingCounterEnd));
453 __ B(lt, &no_inobject_slack_tracking);
454 constructon_count = NoReg;
456 // Fill the pre-allocated fields with undef.
457 __ FillFields(first_prop, prealloc_fields, filler);
459 // Update first_prop register to be the offset of the first field after
460 // pre-allocated fields.
461 __ Add(first_prop, first_prop,
462 Operand(prealloc_fields, LSL, kPointerSizeLog2));
464 if (FLAG_debug_code) {
465 Register obj_end = x14;
466 __ Add(obj_end, new_obj, Operand(obj_size, LSL, kPointerSizeLog2));
467 __ Cmp(first_prop, obj_end);
468 __ Assert(le, kUnexpectedNumberOfPreAllocatedPropertyFields);
471 // Fill the remaining fields with one pointer filler map.
472 __ LoadRoot(filler, Heap::kOnePointerFillerMapRootIndex);
473 __ Sub(prop_fields, prop_fields, prealloc_fields);
475 __ bind(&no_inobject_slack_tracking);
477 if (create_memento) {
478 // Fill the pre-allocated fields with undef.
479 __ FillFields(first_prop, prop_fields, filler);
480 __ Add(first_prop, new_obj, Operand(obj_size, LSL, kPointerSizeLog2));
481 __ LoadRoot(x14, Heap::kAllocationMementoMapRootIndex);
482 DCHECK_EQ(0 * kPointerSize, AllocationMemento::kMapOffset);
483 __ Str(x14, MemOperand(first_prop, kPointerSize, PostIndex));
484 // Load the AllocationSite
485 __ Peek(x14, 3 * kXRegSize);
486 __ AssertUndefinedOrAllocationSite(x14, x10);
487 DCHECK_EQ(1 * kPointerSize, AllocationMemento::kAllocationSiteOffset);
488 __ Str(x14, MemOperand(first_prop, kPointerSize, PostIndex));
491 // Fill all of the property fields with undef.
492 __ FillFields(first_prop, prop_fields, filler);
497 // Add the object tag to make the JSObject real, so that we can continue
498 // and jump into the continuation code at any time from now on.
499 __ Add(new_obj, new_obj, kHeapObjectTag);
501 // Continue with JSObject being successfully allocated.
504 // Reload the original constructor and fall-through.
505 __ Bind(&rt_call_reload_new_target);
506 __ Peek(x3, 0 * kXRegSize);
509 // Allocate the new receiver object using the runtime call.
510 // x1: constructor function
511 // x3: original constructor
513 Label count_incremented;
514 if (create_memento) {
515 // Get the cell or allocation site.
516 __ Peek(x4, 3 * kXRegSize);
517 __ Push(x4, constructor, original_constructor); // arguments 1-3
518 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 3);
520 // If we ended up using the runtime, and we want a memento, then the
521 // runtime call made it for us, and we shouldn't do create count
523 __ B(&count_incremented);
525 __ Push(constructor, original_constructor); // arguments 1-2
526 __ CallRuntime(Runtime::kNewObject, 2);
530 // Receiver for constructor call allocated.
534 if (create_memento) {
535 __ Peek(x10, 3 * kXRegSize);
536 __ JumpIfRoot(x10, Heap::kUndefinedValueRootIndex, &count_incremented);
537 // r2 is an AllocationSite. We are creating a memento from it, so we
538 // need to increment the memento create count.
539 __ Ldr(x5, FieldMemOperand(x10,
540 AllocationSite::kPretenureCreateCountOffset));
541 __ Add(x5, x5, Operand(Smi::FromInt(1)));
542 __ Str(x5, FieldMemOperand(x10,
543 AllocationSite::kPretenureCreateCountOffset));
544 __ bind(&count_incremented);
547 // Restore the parameters.
548 __ Pop(original_constructor);
551 // Reload the number of arguments from the stack.
552 // Set it up in x0 for the function call below.
553 // jssp[0]: number of arguments (smi-tagged)
554 __ Peek(argc, 0); // Load number of arguments.
557 __ Push(original_constructor, x4, x4);
559 // Set up pointer to last argument.
560 __ Add(x2, fp, StandardFrameConstants::kCallerSPOffset);
562 // Copy arguments and receiver to the expression stack.
563 // Copy 2 values every loop to use ldp/stp.
564 // x0: number of arguments
565 // x1: constructor function
566 // x2: address of last argument (caller sp)
569 // jssp[2]: new.target
570 // jssp[3]: number of arguments (smi-tagged)
571 // Compute the start address of the copy in x3.
572 __ Add(x3, x2, Operand(argc, LSL, kPointerSizeLog2));
573 Label loop, entry, done_copying_arguments;
576 __ Ldp(x10, x11, MemOperand(x3, -2 * kPointerSize, PreIndex));
581 // Because we copied values 2 by 2 we may have copied one extra value.
582 // Drop it if that is the case.
583 __ B(eq, &done_copying_arguments);
585 __ Bind(&done_copying_arguments);
587 // Call the function.
588 // x0: number of arguments
589 // x1: constructor function
590 if (is_api_function) {
591 __ Ldr(cp, FieldMemOperand(constructor, JSFunction::kContextOffset));
593 masm->isolate()->builtins()->HandleApiCallConstruct();
594 __ Call(code, RelocInfo::CODE_TARGET);
596 ParameterCount actual(argc);
597 __ InvokeFunction(constructor, actual, CALL_FUNCTION, NullCallWrapper());
600 // Store offset of return address for deoptimizer.
601 if (!is_api_function) {
602 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
605 // Restore the context from the frame.
608 // jssp[1]: new.target
609 // jssp[2]: number of arguments (smi-tagged)
610 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
612 // If the result is an object (in the ECMA sense), we should get rid
613 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
615 Label use_receiver, exit;
617 // If the result is a smi, it is *not* an object in the ECMA sense.
619 // jssp[0]: receiver (newly allocated object)
620 // jssp[1]: number of arguments (smi-tagged)
621 __ JumpIfSmi(x0, &use_receiver);
623 // If the type of the result (stored in its map) is less than
624 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
625 __ JumpIfObjectType(x0, x1, x3, FIRST_SPEC_OBJECT_TYPE, &exit, ge);
627 // Throw away the result of the constructor invocation and use the
628 // on-stack receiver as the result.
629 __ Bind(&use_receiver);
632 // Remove the receiver from the stack, remove caller arguments, and
636 // jssp[0]: receiver (newly allocated object)
637 // jssp[1]: new.target (original constructor)
638 // jssp[2]: number of arguments (smi-tagged)
639 __ Peek(x1, 2 * kXRegSize);
641 // Leave construct frame.
646 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, x1, x2);
651 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
652 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
656 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
657 Generate_JSConstructStubHelper(masm, true, false);
661 void Builtins::Generate_JSConstructStubForDerived(MacroAssembler* masm) {
662 // ----------- S t a t e -------------
663 // -- x0 : number of arguments
664 // -- x1 : constructor function
665 // -- x2 : allocation site or undefined
666 // -- x3 : original constructor
667 // -- lr : return address
668 // -- sp[...]: constructor arguments
669 // -----------------------------------
670 ASM_LOCATION("Builtins::Generate_JSConstructStubForDerived");
673 FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
675 __ AssertUndefinedOrAllocationSite(x2, x10);
678 __ LoadRoot(x10, Heap::kTheHoleValueRootIndex);
679 __ Push(x2, x4, x3, x10);
680 // sp[0]: receiver (the hole)
682 // sp[2]: number of arguments
683 // sp[3]: allocation site
685 // Set up pointer to last argument.
686 __ Add(x2, fp, StandardFrameConstants::kCallerSPOffset);
688 // Copy arguments and receiver to the expression stack.
689 // Copy 2 values every loop to use ldp/stp.
690 // x0: number of arguments
691 // x1: constructor function
692 // x2: address of last argument (caller sp)
694 // jssp[1]: new.target
695 // jssp[2]: number of arguments (smi-tagged)
696 // Compute the start address of the copy in x4.
697 __ Add(x4, x2, Operand(x0, LSL, kPointerSizeLog2));
698 Label loop, entry, done_copying_arguments;
701 __ Ldp(x10, x11, MemOperand(x4, -2 * kPointerSize, PreIndex));
706 // Because we copied values 2 by 2 we may have copied one extra value.
707 // Drop it if that is the case.
708 __ B(eq, &done_copying_arguments);
710 __ Bind(&done_copying_arguments);
714 ExternalReference debug_step_in_fp =
715 ExternalReference::debug_step_in_fp_address(masm->isolate());
716 __ Mov(x2, Operand(debug_step_in_fp));
717 __ Ldr(x2, MemOperand(x2));
718 __ Cbz(x2, &skip_step_in);
721 __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
724 __ bind(&skip_step_in);
726 // Call the function.
727 // x0: number of arguments
728 // x1: constructor function
729 ParameterCount actual(x0);
730 __ InvokeFunction(x1, actual, CALL_FUNCTION, NullCallWrapper());
733 // Restore the context from the frame.
735 // jssp[0]: number of arguments (smi-tagged)
736 __ Ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
738 // Load number of arguments (smi), skipping over new.target.
739 __ Peek(x1, kPointerSize);
741 // Leave construct frame
750 enum IsTagged { kArgcIsSmiTagged, kArgcIsUntaggedInt };
753 // Clobbers x10, x15; preserves all other registers.
754 static void Generate_CheckStackOverflow(MacroAssembler* masm,
755 const int calleeOffset, Register argc,
756 IsTagged argc_is_tagged) {
757 Register function = x15;
759 // Check the stack for overflow.
760 // We are not trying to catch interruptions (e.g. debug break and
761 // preemption) here, so the "real stack limit" is checked.
762 Label enough_stack_space;
763 __ LoadRoot(x10, Heap::kRealStackLimitRootIndex);
764 __ Ldr(function, MemOperand(fp, calleeOffset));
765 // Make x10 the space we have left. The stack might already be overflowed
766 // here which will cause x10 to become negative.
767 // TODO(jbramley): Check that the stack usage here is safe.
768 __ Sub(x10, jssp, x10);
769 // Check if the arguments will overflow the stack.
770 if (argc_is_tagged == kArgcIsSmiTagged) {
771 __ Cmp(x10, Operand::UntagSmiAndScale(argc, kPointerSizeLog2));
773 DCHECK(argc_is_tagged == kArgcIsUntaggedInt);
774 __ Cmp(x10, Operand(argc, LSL, kPointerSizeLog2));
776 __ B(gt, &enough_stack_space);
777 // There is not enough stack space, so use a builtin to throw an appropriate
779 if (argc_is_tagged == kArgcIsUntaggedInt) {
782 __ Push(function, argc);
783 __ InvokeBuiltin(Context::STACK_OVERFLOW_BUILTIN_INDEX, CALL_FUNCTION);
784 // We should never return from the APPLY_OVERFLOW builtin.
785 if (__ emit_debug_code()) {
789 __ Bind(&enough_stack_space);
801 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
803 // Called from JSEntryStub::GenerateBody().
804 Register function = x1;
805 Register receiver = x2;
809 ProfileEntryHookStub::MaybeCallEntryHook(masm);
811 // Clear the context before we push it when entering the internal frame.
815 // Enter an internal frame.
816 FrameScope scope(masm, StackFrame::INTERNAL);
818 // Set up the context from the function argument.
819 __ Ldr(cp, FieldMemOperand(function, JSFunction::kContextOffset));
821 __ InitializeRootRegister();
823 // Push the function and the receiver onto the stack.
824 __ Push(function, receiver);
826 // Check if we have enough stack space to push all arguments.
827 // The function is the first thing that was pushed above after entering
828 // the internal frame.
829 const int kFunctionOffset =
830 InternalFrameConstants::kCodeOffset - kPointerSize;
831 // Expects argument count in eax. Clobbers ecx, edx, edi.
832 Generate_CheckStackOverflow(masm, kFunctionOffset, argc,
835 // Copy arguments to the stack in a loop, in reverse order.
839 // Compute the copy end address.
840 __ Add(x10, argv, Operand(argc, LSL, kPointerSizeLog2));
844 __ Ldr(x11, MemOperand(argv, kPointerSize, PostIndex));
845 __ Ldr(x12, MemOperand(x11)); // Dereference the handle.
846 __ Push(x12); // Push the argument.
851 // Initialize all JavaScript callee-saved registers, since they will be seen
852 // by the garbage collector as part of handlers.
853 // The original values have been saved in JSEntryStub::GenerateBody().
854 __ LoadRoot(x19, Heap::kUndefinedValueRootIndex);
861 // Don't initialize the reserved registers.
862 // x26 : root register (root).
863 // x27 : context pointer (cp).
864 // x28 : JS stack pointer (jssp).
865 // x29 : frame pointer (fp).
869 // No type feedback cell is available.
870 __ LoadRoot(x2, Heap::kUndefinedValueRootIndex);
872 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
875 ParameterCount actual(x0);
876 __ InvokeFunction(function, actual, CALL_FUNCTION, NullCallWrapper());
878 // Exit the JS internal frame and remove the parameters (except function),
882 // Result is in x0. Return.
887 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
888 Generate_JSEntryTrampolineHelper(masm, false);
892 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
893 Generate_JSEntryTrampolineHelper(masm, true);
897 // Generate code for entering a JS function with the interpreter.
898 // On entry to the function the receiver and arguments have been pushed on the
899 // stack left to right. The actual argument count matches the formal parameter
900 // count expected by the function.
902 // The live registers are:
903 // - x1: the JS function object being called.
904 // - cp: our context.
905 // - fp: our caller's frame pointer.
906 // - jssp: stack pointer.
907 // - lr: return address.
909 // The function builds a JS frame. Please see JavaScriptFrameConstants in
910 // frames-arm64.h for its layout.
911 // TODO(rmcilroy): We will need to include the current bytecode pointer in the
913 void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
914 // Open a frame scope to indicate that there is a frame on the stack. The
915 // MANUAL indicates that the scope shouldn't actually generate code to set up
916 // the frame (that is done below).
917 FrameScope frame_scope(masm, StackFrame::MANUAL);
918 __ Push(lr, fp, cp, x1);
919 __ Add(fp, jssp, StandardFrameConstants::kFixedFrameSizeFromFp);
921 // Get the bytecode array from the function object and load the pointer to the
922 // first entry into kInterpreterBytecodeRegister.
923 __ Ldr(x0, FieldMemOperand(x1, JSFunction::kSharedFunctionInfoOffset));
924 __ Ldr(kInterpreterBytecodeArrayRegister,
925 FieldMemOperand(x0, SharedFunctionInfo::kFunctionDataOffset));
927 if (FLAG_debug_code) {
928 // Check function data field is actually a BytecodeArray object.
929 __ AssertNotSmi(kInterpreterBytecodeArrayRegister,
930 kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
931 __ CompareObjectType(kInterpreterBytecodeArrayRegister, x0, x0,
932 BYTECODE_ARRAY_TYPE);
933 __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
936 // Allocate the local and temporary register file on the stack.
938 // Load frame size from the BytecodeArray object.
939 __ Ldr(w11, FieldMemOperand(kInterpreterBytecodeArrayRegister,
940 BytecodeArray::kFrameSizeOffset));
942 // Do a stack check to ensure we don't go over the limit.
944 DCHECK(jssp.Is(__ StackPointer()));
945 __ Sub(x10, jssp, Operand(x11));
946 __ CompareRoot(x10, Heap::kRealStackLimitRootIndex);
948 __ InvokeBuiltin(Context::STACK_OVERFLOW_BUILTIN_INDEX, CALL_FUNCTION);
951 // If ok, push undefined as the initial value for all register file entries.
952 // Note: there should always be at least one stack slot for the return
953 // register in the register file.
955 __ LoadRoot(x10, Heap::kUndefinedValueRootIndex);
956 // TODO(rmcilroy): Ensure we always have an even number of registers to
957 // allow stack to be 16 bit aligned (and remove need for jssp).
958 __ Lsr(x11, x11, kPointerSizeLog2);
959 __ PushMultipleTimes(x10, x11);
960 __ Bind(&loop_header);
963 // TODO(rmcilroy): List of things not currently dealt with here but done in
964 // fullcodegen's prologue:
965 // - Support profiler (specifically profiling_counter).
966 // - Call ProfileEntryHookStub when isolate has a function_entry_hook.
967 // - Allow simulator stop operations if FLAG_stop_at is set.
968 // - Deal with sloppy mode functions which need to replace the
969 // receiver with the global proxy when called as functions (without an
970 // explicit receiver object).
971 // - Code aging of the BytecodeArray object.
972 // - Supporting FLAG_trace.
974 // The following items are also not done here, and will probably be done using
975 // explicit bytecodes instead:
976 // - Allocating a new local context if applicable.
977 // - Setting up a local binding to the this function, which is used in
978 // derived constructors with super calls.
979 // - Setting new.target if required.
980 // - Dealing with REST parameters (only if
981 // https://codereview.chromium.org/1235153006 doesn't land by then).
982 // - Dealing with argument objects.
984 // Perform stack guard check.
987 __ CompareRoot(jssp, Heap::kStackLimitRootIndex);
989 __ CallRuntime(Runtime::kStackGuard, 0);
993 // Load accumulator, register file, bytecode offset, dispatch table into
995 __ LoadRoot(kInterpreterAccumulatorRegister, Heap::kUndefinedValueRootIndex);
996 __ Sub(kInterpreterRegisterFileRegister, fp,
997 Operand(kPointerSize + StandardFrameConstants::kFixedFrameSizeFromFp));
998 __ Mov(kInterpreterBytecodeOffsetRegister,
999 Operand(BytecodeArray::kHeaderSize - kHeapObjectTag));
1000 __ LoadRoot(kInterpreterDispatchTableRegister,
1001 Heap::kInterpreterTableRootIndex);
1002 __ Add(kInterpreterDispatchTableRegister, kInterpreterDispatchTableRegister,
1003 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1005 // Dispatch to the first bytecode handler for the function.
1006 __ Ldrb(x1, MemOperand(kInterpreterBytecodeArrayRegister,
1007 kInterpreterBytecodeOffsetRegister));
1008 __ Mov(x1, Operand(x1, LSL, kPointerSizeLog2));
1009 __ Ldr(ip0, MemOperand(kInterpreterDispatchTableRegister, x1));
1010 // TODO(rmcilroy): Make dispatch table point to code entrys to avoid untagging
1011 // and header removal.
1012 __ Add(ip0, ip0, Operand(Code::kHeaderSize - kHeapObjectTag));
1017 void Builtins::Generate_InterpreterExitTrampoline(MacroAssembler* masm) {
1018 // TODO(rmcilroy): List of things not currently dealt with here but done in
1019 // fullcodegen's EmitReturnSequence.
1020 // - Supporting FLAG_trace for Runtime::TraceExit.
1021 // - Support profiler (specifically decrementing profiling_counter
1022 // appropriately and calling out to HandleInterrupts if necessary).
1024 // The return value is in accumulator, which is already in x0.
1026 // Leave the frame (also dropping the register file).
1027 __ LeaveFrame(StackFrame::JAVA_SCRIPT);
1029 // Drop receiver + arguments and return.
1030 __ Ldr(w1, FieldMemOperand(kInterpreterBytecodeArrayRegister,
1031 BytecodeArray::kParameterSizeOffset));
1037 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
1038 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
1039 GenerateTailCallToReturnedCode(masm);
1043 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
1044 FrameScope scope(masm, StackFrame::INTERNAL);
1045 Register function = x1;
1047 // Preserve function. At the same time, push arguments for
1048 // kCompileOptimized.
1049 __ LoadObject(x10, masm->isolate()->factory()->ToBoolean(concurrent));
1050 __ Push(function, function, x10);
1052 __ CallRuntime(Runtime::kCompileOptimized, 2);
1054 // Restore receiver.
1059 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
1060 CallCompileOptimized(masm, false);
1061 GenerateTailCallToReturnedCode(masm);
1065 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
1066 CallCompileOptimized(masm, true);
1067 GenerateTailCallToReturnedCode(masm);
1071 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
1072 // For now, we are relying on the fact that make_code_young doesn't do any
1073 // garbage collection which allows us to save/restore the registers without
1074 // worrying about which of them contain pointers. We also don't build an
1075 // internal frame to make the code fast, since we shouldn't have to do stack
1076 // crawls in MakeCodeYoung. This seems a bit fragile.
1078 // The following caller-saved registers must be saved and restored when
1079 // calling through to the runtime:
1080 // x0 - The address from which to resume execution.
1082 // lr - The return address for the JSFunction itself. It has not yet been
1083 // preserved on the stack because the frame setup code was replaced
1084 // with a call to this stub, to handle code ageing.
1086 FrameScope scope(masm, StackFrame::MANUAL);
1087 __ Push(x0, x1, fp, lr);
1088 __ Mov(x1, ExternalReference::isolate_address(masm->isolate()));
1090 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
1091 __ Pop(lr, fp, x1, x0);
1094 // The calling function has been made young again, so return to execute the
1095 // real frame set-up code.
1099 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
1100 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
1101 MacroAssembler* masm) { \
1102 GenerateMakeCodeYoungAgainCommon(masm); \
1104 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
1105 MacroAssembler* masm) { \
1106 GenerateMakeCodeYoungAgainCommon(masm); \
1108 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
1109 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
1112 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
1113 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
1114 // that make_code_young doesn't do any garbage collection which allows us to
1115 // save/restore the registers without worrying about which of them contain
1118 // The following caller-saved registers must be saved and restored when
1119 // calling through to the runtime:
1120 // x0 - The address from which to resume execution.
1122 // lr - The return address for the JSFunction itself. It has not yet been
1123 // preserved on the stack because the frame setup code was replaced
1124 // with a call to this stub, to handle code ageing.
1126 FrameScope scope(masm, StackFrame::MANUAL);
1127 __ Push(x0, x1, fp, lr);
1128 __ Mov(x1, ExternalReference::isolate_address(masm->isolate()));
1130 ExternalReference::get_mark_code_as_executed_function(
1131 masm->isolate()), 2);
1132 __ Pop(lr, fp, x1, x0);
1134 // Perform prologue operations usually performed by the young code stub.
1135 __ EmitFrameSetupForCodeAgePatching(masm);
1138 // Jump to point after the code-age stub.
1139 __ Add(x0, x0, kNoCodeAgeSequenceLength);
1144 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
1145 GenerateMakeCodeYoungAgainCommon(masm);
1149 void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
1150 Generate_MarkCodeAsExecutedOnce(masm);
1154 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
1155 SaveFPRegsMode save_doubles) {
1157 FrameScope scope(masm, StackFrame::INTERNAL);
1159 // Preserve registers across notification, this is important for compiled
1160 // stubs that tail call the runtime on deopts passing their parameters in
1162 // TODO(jbramley): Is it correct (and appropriate) to use safepoint
1163 // registers here? According to the comment above, we should only need to
1164 // preserve the registers with parameters.
1165 __ PushXRegList(kSafepointSavedRegisters);
1166 // Pass the function and deoptimization type to the runtime system.
1167 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
1168 __ PopXRegList(kSafepointSavedRegisters);
1171 // Ignore state (pushed by Deoptimizer::EntryGenerator::Generate).
1174 // Jump to the miss handler. Deoptimizer::EntryGenerator::Generate loads this
1175 // into lr before it jumps here.
1180 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
1181 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
1185 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
1186 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
1190 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1191 Deoptimizer::BailoutType type) {
1193 FrameScope scope(masm, StackFrame::INTERNAL);
1194 // Pass the deoptimization type to the runtime system.
1195 __ Mov(x0, Smi::FromInt(static_cast<int>(type)));
1197 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
1200 // Get the full codegen state from the stack and untag it.
1201 Register state = x6;
1205 // Switch on the state.
1206 Label with_tos_register, unknown_state;
1207 __ CompareAndBranch(
1208 state, FullCodeGenerator::NO_REGISTERS, ne, &with_tos_register);
1209 __ Drop(1); // Remove state.
1212 __ Bind(&with_tos_register);
1213 // Reload TOS register.
1214 __ Peek(x0, kPointerSize);
1215 __ CompareAndBranch(state, FullCodeGenerator::TOS_REG, ne, &unknown_state);
1216 __ Drop(2); // Remove state and TOS.
1219 __ Bind(&unknown_state);
1220 __ Abort(kInvalidFullCodegenState);
1224 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1225 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1229 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1230 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1234 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
1235 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1239 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1240 // Lookup the function in the JavaScript frame.
1241 __ Ldr(x0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1243 FrameScope scope(masm, StackFrame::INTERNAL);
1244 // Pass function as argument.
1246 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1249 // If the code object is null, just return to the unoptimized code.
1251 __ CompareAndBranch(x0, Smi::FromInt(0), ne, &skip);
1256 // Load deoptimization data from the code object.
1257 // <deopt_data> = <code>[#deoptimization_data_offset]
1258 __ Ldr(x1, MemOperand(x0, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1260 // Load the OSR entrypoint offset from the deoptimization data.
1261 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1262 __ Ldrsw(w1, UntagSmiFieldMemOperand(x1, FixedArray::OffsetOfElementAt(
1263 DeoptimizationInputData::kOsrPcOffsetIndex)));
1265 // Compute the target address = code_obj + header_size + osr_offset
1266 // <entry_addr> = <code_obj> + #header_size + <osr_offset>
1268 __ Add(lr, x0, Code::kHeaderSize - kHeapObjectTag);
1270 // And "return" to the OSR entry point of the function.
1275 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1276 // We check the stack limit as indicator that recompilation might be done.
1278 __ CompareRoot(jssp, Heap::kStackLimitRootIndex);
1281 FrameScope scope(masm, StackFrame::INTERNAL);
1282 __ CallRuntime(Runtime::kStackGuard, 0);
1284 __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
1285 RelocInfo::CODE_TARGET);
1292 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1294 Register function = x1;
1295 Register scratch1 = x10;
1296 Register scratch2 = x11;
1298 ASM_LOCATION("Builtins::Generate_FunctionCall");
1299 // 1. Make sure we have at least one argument.
1302 __ Cbnz(argc, &done);
1303 __ LoadRoot(scratch1, Heap::kUndefinedValueRootIndex);
1309 // 2. Get the callable to call (passed as receiver) from the stack.
1310 __ Peek(function, Operand(argc, LSL, kXRegSizeLog2));
1312 // 3. Shift arguments and return address one slot down on the stack
1313 // (overwriting the original receiver). Adjust argument count to make
1314 // the original first argument the new receiver.
1317 // Calculate the copy start address (destination). Copy end address is jssp.
1318 __ Add(scratch2, jssp, Operand(argc, LSL, kPointerSizeLog2));
1319 __ Sub(scratch1, scratch2, kPointerSize);
1322 __ Ldr(x12, MemOperand(scratch1, -kPointerSize, PostIndex));
1323 __ Str(x12, MemOperand(scratch2, -kPointerSize, PostIndex));
1324 __ Cmp(scratch1, jssp);
1326 // Adjust the actual number of arguments and remove the top element
1327 // (which is a copy of the last argument).
1328 __ Sub(argc, argc, 1);
1332 // 4. Call the callable.
1333 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1337 static void Generate_PushAppliedArguments(MacroAssembler* masm,
1338 const int argumentsOffset,
1339 const int indexOffset,
1340 const int limitOffset) {
1342 Register receiver = LoadDescriptor::ReceiverRegister();
1343 Register key = LoadDescriptor::NameRegister();
1344 Register slot = LoadDescriptor::SlotRegister();
1345 Register vector = LoadWithVectorDescriptor::VectorRegister();
1347 __ Ldr(key, MemOperand(fp, indexOffset));
1350 // Load the current argument from the arguments array.
1352 __ Ldr(receiver, MemOperand(fp, argumentsOffset));
1354 // Use inline caching to speed up access to arguments.
1355 Code::Kind kinds[] = {Code::KEYED_LOAD_IC};
1356 FeedbackVectorSpec spec(0, 1, kinds);
1357 Handle<TypeFeedbackVector> feedback_vector =
1358 masm->isolate()->factory()->NewTypeFeedbackVector(&spec);
1359 int index = feedback_vector->GetIndex(FeedbackVectorICSlot(0));
1360 __ Mov(slot, Smi::FromInt(index));
1361 __ Mov(vector, feedback_vector);
1363 KeyedLoadICStub(masm->isolate(), LoadICState(kNoExtraICState)).GetCode();
1364 __ Call(ic, RelocInfo::CODE_TARGET);
1366 // Push the nth argument.
1369 __ Ldr(key, MemOperand(fp, indexOffset));
1370 __ Add(key, key, Smi::FromInt(1));
1371 __ Str(key, MemOperand(fp, indexOffset));
1373 // Test if the copy loop has finished copying all the elements from the
1374 // arguments object.
1376 __ Ldr(x1, MemOperand(fp, limitOffset));
1380 // On exit, the pushed arguments count is in x0, untagged
1386 static void Generate_ApplyHelper(MacroAssembler* masm, bool targetIsArgument) {
1387 const int kFormalParameters = targetIsArgument ? 3 : 2;
1388 const int kStackSize = kFormalParameters + 1;
1391 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1393 const int kArgumentsOffset = kFPOnStackSize + kPCOnStackSize;
1394 const int kReceiverOffset = kArgumentsOffset + kPointerSize;
1395 const int kFunctionOffset = kReceiverOffset + kPointerSize;
1396 const int kIndexOffset =
1397 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1398 const int kLimitOffset =
1399 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1401 Register args = x12;
1402 Register receiver = x14;
1403 Register function = x15;
1405 // Get the length of the arguments via a builtin call.
1406 __ Ldr(function, MemOperand(fp, kFunctionOffset));
1407 __ Ldr(args, MemOperand(fp, kArgumentsOffset));
1408 __ Push(function, args);
1409 if (targetIsArgument) {
1410 __ InvokeBuiltin(Context::REFLECT_APPLY_PREPARE_BUILTIN_INDEX,
1413 __ InvokeBuiltin(Context::APPLY_PREPARE_BUILTIN_INDEX, CALL_FUNCTION);
1417 Generate_CheckStackOverflow(masm, kFunctionOffset, argc, kArgcIsSmiTagged);
1419 // Push current limit, index and receiver.
1420 __ Mov(x1, 0); // Initial index.
1421 __ Ldr(receiver, MemOperand(fp, kReceiverOffset));
1422 __ Push(argc, x1, receiver);
1424 // Copy all arguments from the array to the stack.
1425 Generate_PushAppliedArguments(masm, kArgumentsOffset, kIndexOffset,
1428 // At the end of the loop, the number of arguments is stored in x0, untagged
1430 // Call the callable.
1431 // TODO(bmeurer): This should be a tail call according to ES6.
1432 __ Ldr(x1, MemOperand(fp, kFunctionOffset));
1433 __ Call(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1435 __ Drop(kStackSize);
1440 static void Generate_ConstructHelper(MacroAssembler* masm) {
1441 const int kFormalParameters = 3;
1442 const int kStackSize = kFormalParameters + 1;
1445 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1447 const int kNewTargetOffset = kFPOnStackSize + kPCOnStackSize;
1448 const int kArgumentsOffset = kNewTargetOffset + kPointerSize;
1449 const int kFunctionOffset = kArgumentsOffset + kPointerSize;
1451 const int kIndexOffset =
1452 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1453 const int kLimitOffset =
1454 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1456 // Is x11 safe to use?
1457 Register newTarget = x11;
1458 Register args = x12;
1459 Register function = x15;
1461 // If newTarget is not supplied, set it to constructor
1462 Label validate_arguments;
1463 __ Ldr(x0, MemOperand(fp, kNewTargetOffset));
1464 __ CompareRoot(x0, Heap::kUndefinedValueRootIndex);
1465 __ B(ne, &validate_arguments);
1466 __ Ldr(x0, MemOperand(fp, kFunctionOffset));
1467 __ Str(x0, MemOperand(fp, kNewTargetOffset));
1469 // Validate arguments
1470 __ Bind(&validate_arguments);
1471 __ Ldr(function, MemOperand(fp, kFunctionOffset));
1472 __ Ldr(args, MemOperand(fp, kArgumentsOffset));
1473 __ Ldr(newTarget, MemOperand(fp, kNewTargetOffset));
1474 __ Push(function, args, newTarget);
1475 __ InvokeBuiltin(Context::REFLECT_CONSTRUCT_PREPARE_BUILTIN_INDEX,
1479 Generate_CheckStackOverflow(masm, kFunctionOffset, argc, kArgcIsSmiTagged);
1481 // Push current limit and index & constructor function as callee.
1482 __ Mov(x1, 0); // Initial index.
1483 __ Push(argc, x1, function);
1485 // Copy all arguments from the array to the stack.
1486 Generate_PushAppliedArguments(
1487 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1489 // Use undefined feedback vector
1490 __ LoadRoot(x2, Heap::kUndefinedValueRootIndex);
1491 __ Ldr(x1, MemOperand(fp, kFunctionOffset));
1492 __ Ldr(x4, MemOperand(fp, kNewTargetOffset));
1494 // Call the function.
1495 CallConstructStub stub(masm->isolate(), SUPER_CONSTRUCTOR_CALL);
1496 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
1498 // Leave internal frame.
1500 __ Drop(kStackSize);
1505 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1506 ASM_LOCATION("Builtins::Generate_FunctionApply");
1507 Generate_ApplyHelper(masm, false);
1511 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1512 ASM_LOCATION("Builtins::Generate_ReflectApply");
1513 Generate_ApplyHelper(masm, true);
1517 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1518 ASM_LOCATION("Builtins::Generate_ReflectConstruct");
1519 Generate_ConstructHelper(masm);
1523 static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1524 Label* stack_overflow) {
1525 // ----------- S t a t e -------------
1526 // -- x0 : actual number of arguments
1527 // -- x1 : function (passed through to callee)
1528 // -- x2 : expected number of arguments
1529 // -----------------------------------
1530 // Check the stack for overflow.
1531 // We are not trying to catch interruptions (e.g. debug break and
1532 // preemption) here, so the "real stack limit" is checked.
1533 Label enough_stack_space;
1534 __ LoadRoot(x10, Heap::kRealStackLimitRootIndex);
1535 // Make x10 the space we have left. The stack might already be overflowed
1536 // here which will cause x10 to become negative.
1537 __ Sub(x10, jssp, x10);
1538 // Check if the arguments will overflow the stack.
1539 __ Cmp(x10, Operand(x2, LSL, kPointerSizeLog2));
1540 __ B(le, stack_overflow);
1544 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1546 __ Mov(x11, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
1548 __ Push(x11, x1, x10);
1550 StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize);
1554 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1555 // ----------- S t a t e -------------
1556 // -- x0 : result being passed through
1557 // -----------------------------------
1558 // Get the number of arguments passed (as a smi), tear down the frame and
1559 // then drop the parameters and the receiver.
1560 __ Ldr(x10, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1564 __ DropBySMI(x10, kXRegSize);
1570 void Builtins::Generate_CallFunction(MacroAssembler* masm) {
1571 // ----------- S t a t e -------------
1572 // -- x0 : the number of arguments (not including the receiver)
1573 // -- x1 : the function to call (checked to be a JSFunction)
1574 // -----------------------------------
1576 Label convert, convert_global_proxy, convert_to_object, done_convert;
1577 __ AssertFunction(x1);
1578 // TODO(bmeurer): Throw a TypeError if function's [[FunctionKind]] internal
1579 // slot is "classConstructor".
1580 // Enter the context of the function; ToObject has to run in the function
1581 // context, and we also need to take the global proxy from the function
1582 // context in case of conversion.
1583 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
1584 __ Ldr(cp, FieldMemOperand(x1, JSFunction::kContextOffset));
1585 __ Ldr(x2, FieldMemOperand(x1, JSFunction::kSharedFunctionInfoOffset));
1586 // We need to convert the receiver for non-native sloppy mode functions.
1587 __ Ldr(w3, FieldMemOperand(x2, SharedFunctionInfo::kCompilerHintsOffset));
1588 __ TestAndBranchIfAnySet(w3,
1589 (1 << SharedFunctionInfo::kNative) |
1590 (1 << SharedFunctionInfo::kStrictModeFunction),
1593 __ Peek(x3, Operand(x0, LSL, kXRegSizeLog2));
1595 // ----------- S t a t e -------------
1596 // -- x0 : the number of arguments (not including the receiver)
1597 // -- x1 : the function to call (checked to be a JSFunction)
1598 // -- x2 : the shared function info.
1599 // -- x3 : the receiver
1600 // -- cp : the function context.
1601 // -----------------------------------
1603 Label convert_receiver;
1604 __ JumpIfSmi(x3, &convert_to_object);
1605 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
1606 __ CompareObjectType(x3, x4, x4, FIRST_JS_RECEIVER_TYPE);
1607 __ B(hs, &done_convert);
1608 __ JumpIfRoot(x3, Heap::kUndefinedValueRootIndex, &convert_global_proxy);
1609 __ JumpIfNotRoot(x3, Heap::kNullValueRootIndex, &convert_to_object);
1610 __ Bind(&convert_global_proxy);
1612 // Patch receiver to global proxy.
1613 __ LoadGlobalProxy(x3);
1615 __ B(&convert_receiver);
1616 __ Bind(&convert_to_object);
1618 // Convert receiver using ToObject.
1619 // TODO(bmeurer): Inline the allocation here to avoid building the frame
1620 // in the fast case? (fall back to AllocateInNewSpace?)
1621 FrameScope scope(masm, StackFrame::INTERNAL);
1625 ToObjectStub stub(masm->isolate());
1631 __ Ldr(x2, FieldMemOperand(x1, JSFunction::kSharedFunctionInfoOffset));
1632 __ Bind(&convert_receiver);
1633 __ Poke(x3, Operand(x0, LSL, kXRegSizeLog2));
1635 __ Bind(&done_convert);
1637 // ----------- S t a t e -------------
1638 // -- x0 : the number of arguments (not including the receiver)
1639 // -- x1 : the function to call (checked to be a JSFunction)
1640 // -- x2 : the shared function info.
1641 // -- cp : the function context.
1642 // -----------------------------------
1645 x2, FieldMemOperand(x2, SharedFunctionInfo::kFormalParameterCountOffset));
1646 __ Ldr(x3, FieldMemOperand(x1, JSFunction::kCodeEntryOffset));
1647 ParameterCount actual(x0);
1648 ParameterCount expected(x2);
1649 __ InvokeCode(x3, expected, actual, JUMP_FUNCTION, NullCallWrapper());
1654 void Builtins::Generate_Call(MacroAssembler* masm) {
1655 // ----------- S t a t e -------------
1656 // -- x0 : the number of arguments (not including the receiver)
1657 // -- x1 : the target to call (can be any Object).
1658 // -----------------------------------
1660 Label non_smi, non_jsfunction, non_function;
1661 __ JumpIfSmi(x1, &non_function);
1663 __ CompareObjectType(x1, x2, x2, JS_FUNCTION_TYPE);
1664 __ B(ne, &non_jsfunction);
1665 __ Jump(masm->isolate()->builtins()->CallFunction(), RelocInfo::CODE_TARGET);
1666 __ Bind(&non_jsfunction);
1667 __ Cmp(x2, JS_FUNCTION_PROXY_TYPE);
1668 __ B(ne, &non_function);
1670 // 1. Call to function proxy.
1671 // TODO(neis): This doesn't match the ES6 spec for [[Call]] on proxies.
1672 __ Ldr(x1, FieldMemOperand(x1, JSFunctionProxy::kCallTrapOffset));
1673 __ AssertNotSmi(x1);
1676 // 2. Call to something else, which might have a [[Call]] internal method (if
1677 // not we raise an exception).
1678 __ Bind(&non_function);
1679 // TODO(bmeurer): I wonder why we prefer to have slow API calls? This could
1680 // be awesome instead; i.e. a trivial improvement would be to call into the
1681 // runtime and just deal with the API function there instead of returning a
1682 // delegate from a runtime call that just jumps back to the runtime once
1683 // called. Or, bonus points, call directly into the C API function here, as
1684 // we do in some Crankshaft fast cases.
1685 // Overwrite the original receiver with the (original) target.
1686 __ Poke(x1, Operand(x0, LSL, kXRegSizeLog2));
1688 // Determine the delegate for the target (if any).
1689 FrameScope scope(masm, StackFrame::INTERNAL);
1692 __ CallRuntime(Runtime::kGetFunctionDelegate, 1);
1697 // The delegate is always a regular function.
1698 __ AssertFunction(x1);
1699 __ Jump(masm->isolate()->builtins()->CallFunction(), RelocInfo::CODE_TARGET);
1703 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1704 ASM_LOCATION("Builtins::Generate_ArgumentsAdaptorTrampoline");
1705 // ----------- S t a t e -------------
1706 // -- x0 : actual number of arguments
1707 // -- x1 : function (passed through to callee)
1708 // -- x2 : expected number of arguments
1709 // -----------------------------------
1711 Label stack_overflow;
1712 ArgumentAdaptorStackCheck(masm, &stack_overflow);
1714 Register argc_actual = x0; // Excluding the receiver.
1715 Register argc_expected = x2; // Excluding the receiver.
1716 Register function = x1;
1717 Register code_entry = x3;
1719 Label invoke, dont_adapt_arguments;
1721 Label enough, too_few;
1722 __ Ldr(code_entry, FieldMemOperand(function, JSFunction::kCodeEntryOffset));
1723 __ Cmp(argc_actual, argc_expected);
1725 __ Cmp(argc_expected, SharedFunctionInfo::kDontAdaptArgumentsSentinel);
1726 __ B(eq, &dont_adapt_arguments);
1728 { // Enough parameters: actual >= expected
1729 EnterArgumentsAdaptorFrame(masm);
1731 Register copy_start = x10;
1732 Register copy_end = x11;
1733 Register copy_to = x12;
1734 Register scratch1 = x13, scratch2 = x14;
1736 __ Lsl(scratch2, argc_expected, kPointerSizeLog2);
1738 // Adjust for fp, lr, and the receiver.
1739 __ Add(copy_start, fp, 3 * kPointerSize);
1740 __ Add(copy_start, copy_start, Operand(argc_actual, LSL, kPointerSizeLog2));
1741 __ Sub(copy_end, copy_start, scratch2);
1742 __ Sub(copy_end, copy_end, kPointerSize);
1743 __ Mov(copy_to, jssp);
1745 // Claim space for the arguments, the receiver, and one extra slot.
1746 // The extra slot ensures we do not write under jssp. It will be popped
1748 __ Add(scratch1, scratch2, 2 * kPointerSize);
1749 __ Claim(scratch1, 1);
1751 // Copy the arguments (including the receiver) to the new stack frame.
1753 __ Bind(©_2_by_2);
1754 __ Ldp(scratch1, scratch2,
1755 MemOperand(copy_start, - 2 * kPointerSize, PreIndex));
1756 __ Stp(scratch1, scratch2,
1757 MemOperand(copy_to, - 2 * kPointerSize, PreIndex));
1758 __ Cmp(copy_start, copy_end);
1759 __ B(hi, ©_2_by_2);
1761 // Correct the space allocated for the extra slot.
1767 { // Too few parameters: Actual < expected
1770 Register copy_from = x10;
1771 Register copy_end = x11;
1772 Register copy_to = x12;
1773 Register scratch1 = x13, scratch2 = x14;
1775 // If the function is strong we need to throw an error.
1776 Label no_strong_error;
1778 FieldMemOperand(function, JSFunction::kSharedFunctionInfoOffset));
1779 __ Ldr(scratch2.W(),
1780 FieldMemOperand(scratch1, SharedFunctionInfo::kCompilerHintsOffset));
1781 __ TestAndBranchIfAllClear(scratch2.W(),
1782 (1 << SharedFunctionInfo::kStrongModeFunction),
1785 // What we really care about is the required number of arguments.
1786 DCHECK_EQ(kPointerSize, kInt64Size);
1787 __ Ldr(scratch2.W(),
1788 FieldMemOperand(scratch1, SharedFunctionInfo::kLengthOffset));
1789 __ Cmp(argc_actual, Operand(scratch2, LSR, 1));
1790 __ B(ge, &no_strong_error);
1793 FrameScope frame(masm, StackFrame::MANUAL);
1794 EnterArgumentsAdaptorFrame(masm);
1795 __ CallRuntime(Runtime::kThrowStrongModeTooFewArguments, 0);
1798 __ Bind(&no_strong_error);
1799 EnterArgumentsAdaptorFrame(masm);
1801 __ Lsl(scratch2, argc_expected, kPointerSizeLog2);
1802 __ Lsl(argc_actual, argc_actual, kPointerSizeLog2);
1804 // Adjust for fp, lr, and the receiver.
1805 __ Add(copy_from, fp, 3 * kPointerSize);
1806 __ Add(copy_from, copy_from, argc_actual);
1807 __ Mov(copy_to, jssp);
1808 __ Sub(copy_end, copy_to, 1 * kPointerSize); // Adjust for the receiver.
1809 __ Sub(copy_end, copy_end, argc_actual);
1811 // Claim space for the arguments, the receiver, and one extra slot.
1812 // The extra slot ensures we do not write under jssp. It will be popped
1814 __ Add(scratch1, scratch2, 2 * kPointerSize);
1815 __ Claim(scratch1, 1);
1817 // Copy the arguments (including the receiver) to the new stack frame.
1819 __ Bind(©_2_by_2);
1820 __ Ldp(scratch1, scratch2,
1821 MemOperand(copy_from, - 2 * kPointerSize, PreIndex));
1822 __ Stp(scratch1, scratch2,
1823 MemOperand(copy_to, - 2 * kPointerSize, PreIndex));
1824 __ Cmp(copy_to, copy_end);
1825 __ B(hi, ©_2_by_2);
1827 __ Mov(copy_to, copy_end);
1829 // Fill the remaining expected arguments with undefined.
1830 __ LoadRoot(scratch1, Heap::kUndefinedValueRootIndex);
1831 __ Add(copy_end, jssp, kPointerSize);
1835 __ Stp(scratch1, scratch1,
1836 MemOperand(copy_to, - 2 * kPointerSize, PreIndex));
1837 __ Cmp(copy_to, copy_end);
1840 // Correct the space allocated for the extra slot.
1844 // Arguments have been adapted. Now call the entry point.
1846 __ Mov(argc_actual, argc_expected);
1847 // x0 : expected number of arguments
1848 // x1 : function (passed through to callee)
1849 __ Call(code_entry);
1851 // Store offset of return address for deoptimizer.
1852 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1854 // Exit frame and return.
1855 LeaveArgumentsAdaptorFrame(masm);
1858 // Call the entry point without adapting the arguments.
1859 __ Bind(&dont_adapt_arguments);
1860 __ Jump(code_entry);
1862 __ Bind(&stack_overflow);
1864 FrameScope frame(masm, StackFrame::MANUAL);
1865 EnterArgumentsAdaptorFrame(masm);
1866 __ InvokeBuiltin(Context::STACK_OVERFLOW_BUILTIN_INDEX, CALL_FUNCTION);
1874 } // namespace internal
1877 #endif // V8_TARGET_ARCH_ARM