1 // Copyright 2012 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.
7 #include "src/codegen.h"
8 #include "src/debug/debug.h"
9 #include "src/deoptimizer.h"
10 #include "src/full-codegen/full-codegen.h"
11 #include "src/runtime/runtime.h"
17 #define __ ACCESS_MASM(masm)
20 void Builtins::Generate_Adaptor(MacroAssembler* masm,
22 BuiltinExtraArguments extra_args) {
23 // ----------- S t a t e -------------
24 // -- r0 : number of arguments excluding receiver
25 // -- r1 : called function (only guaranteed when
26 // extra_args requires it)
28 // -- sp[0] : last argument
30 // -- sp[4 * (argc - 1)] : first argument (argc == r0)
31 // -- sp[4 * argc] : receiver
32 // -----------------------------------
34 // Insert extra arguments.
35 int num_extra_args = 0;
36 if (extra_args == NEEDS_CALLED_FUNCTION) {
40 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
43 // JumpToExternalReference expects r0 to contain the number of arguments
44 // including the receiver and the extra arguments.
45 __ add(r0, r0, Operand(num_extra_args + 1));
46 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
50 // Load the built-in InternalArray function from the current context.
51 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
53 // Load the native context.
56 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
58 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
59 // Load the InternalArray function from the native context.
63 Context::INTERNAL_ARRAY_FUNCTION_INDEX)));
67 // Load the built-in Array function from the current context.
68 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
69 // Load the native context.
72 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
74 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
75 // Load the Array function from the native context.
78 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
82 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
83 // ----------- S t a t e -------------
84 // -- r0 : number of arguments
85 // -- lr : return address
86 // -- sp[...]: constructor arguments
87 // -----------------------------------
88 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
90 // Get the InternalArray function.
91 GenerateLoadInternalArrayFunction(masm, r1);
93 if (FLAG_debug_code) {
94 // Initial map for the builtin InternalArray functions should be maps.
95 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
97 __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction);
98 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
99 __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction);
102 // Run the native code for the InternalArray function called as a normal
105 InternalArrayConstructorStub stub(masm->isolate());
106 __ TailCallStub(&stub);
110 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
111 // ----------- S t a t e -------------
112 // -- r0 : number of arguments
113 // -- lr : return address
114 // -- sp[...]: constructor arguments
115 // -----------------------------------
116 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
118 // Get the Array function.
119 GenerateLoadArrayFunction(masm, r1);
121 if (FLAG_debug_code) {
122 // Initial map for the builtin Array functions should be maps.
123 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
125 __ Assert(ne, kUnexpectedInitialMapForArrayFunction);
126 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
127 __ Assert(eq, kUnexpectedInitialMapForArrayFunction);
131 // Run the native code for the Array function called as a normal function.
133 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
134 ArrayConstructorStub stub(masm->isolate());
135 __ TailCallStub(&stub);
140 void Builtins::Generate_StringConstructor(MacroAssembler* masm) {
141 // ----------- S t a t e -------------
142 // -- r0 : number of arguments
143 // -- r1 : constructor function
144 // -- lr : return address
145 // -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
146 // -- sp[argc * 4] : receiver
147 // -----------------------------------
149 // 1. Load the first argument into r0 and get rid of the rest (including the
153 __ sub(r0, r0, Operand(1), SetCC);
154 __ b(lo, &no_arguments);
155 __ ldr(r0, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
159 // 2a. At least one argument, return r0 if it's a string, otherwise
160 // dispatch to appropriate conversion.
161 Label to_string, symbol_descriptive_string;
163 __ JumpIfSmi(r0, &to_string);
164 STATIC_ASSERT(FIRST_NONSTRING_TYPE == SYMBOL_TYPE);
165 __ CompareObjectType(r0, r1, r1, FIRST_NONSTRING_TYPE);
166 __ b(hi, &to_string);
167 __ b(eq, &symbol_descriptive_string);
171 // 2b. No arguments, return the empty string (and pop the receiver).
172 __ bind(&no_arguments);
174 __ LoadRoot(r0, Heap::kempty_stringRootIndex);
178 // 3a. Convert r0 to a string.
181 ToStringStub stub(masm->isolate());
182 __ TailCallStub(&stub);
185 // 3b. Convert symbol in r0 to a string.
186 __ bind(&symbol_descriptive_string);
189 __ TailCallRuntime(Runtime::kSymbolDescriptiveString, 1, 1);
195 void Builtins::Generate_StringConstructor_ConstructStub(MacroAssembler* masm) {
196 // ----------- S t a t e -------------
197 // -- r0 : number of arguments
198 // -- r1 : constructor function
199 // -- lr : return address
200 // -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
201 // -- sp[argc * 4] : receiver
202 // -----------------------------------
204 // 1. Load the first argument into r0 and get rid of the rest (including the
207 Label no_arguments, done;
208 __ sub(r0, r0, Operand(1), SetCC);
209 __ b(lo, &no_arguments);
210 __ ldr(r0, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
213 __ bind(&no_arguments);
214 __ LoadRoot(r0, Heap::kempty_stringRootIndex);
219 // 2. Make sure r0 is a string.
221 Label convert, done_convert;
222 __ JumpIfSmi(r0, &convert);
223 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
224 __ b(lo, &done_convert);
227 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
228 ToStringStub stub(masm->isolate());
233 __ bind(&done_convert);
236 // 3. Allocate a JSValue wrapper for the string.
238 // ----------- S t a t e -------------
239 // -- r0 : the first argument
240 // -- r1 : constructor function
241 // -- lr : return address
242 // -----------------------------------
244 Label allocate, done_allocate;
246 __ Allocate(JSValue::kSize, r0, r3, r4, &allocate, TAG_OBJECT);
247 __ bind(&done_allocate);
249 // Initialize the JSValue in r0.
250 __ LoadGlobalFunctionInitialMap(r1, r3, r4);
251 __ str(r3, FieldMemOperand(r0, HeapObject::kMapOffset));
252 __ LoadRoot(r3, Heap::kEmptyFixedArrayRootIndex);
253 __ str(r3, FieldMemOperand(r0, JSObject::kPropertiesOffset));
254 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
255 __ str(r2, FieldMemOperand(r0, JSValue::kValueOffset));
256 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
259 // Fallback to the runtime to allocate in new space.
262 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
263 __ Move(r3, Smi::FromInt(JSValue::kSize));
265 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
268 __ b(&done_allocate);
273 static void CallRuntimePassFunction(
274 MacroAssembler* masm, Runtime::FunctionId function_id) {
275 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
276 // Push a copy of the function onto the stack.
278 // Push function as parameter to the runtime call.
281 __ CallRuntime(function_id, 1);
287 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
288 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
289 __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kCodeOffset));
290 __ add(r2, r2, Operand(Code::kHeaderSize - kHeapObjectTag));
295 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
296 __ add(r0, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
301 void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
302 // Checking whether the queued function is ready for install is optional,
303 // since we come across interrupts and stack checks elsewhere. However,
304 // not checking may delay installing ready functions, and always checking
305 // would be quite expensive. A good compromise is to first check against
306 // stack limit as a cue for an interrupt signal.
308 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
309 __ cmp(sp, Operand(ip));
312 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
313 GenerateTailCallToReturnedCode(masm);
316 GenerateTailCallToSharedCode(masm);
320 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
321 bool is_api_function) {
322 // ----------- S t a t e -------------
323 // -- r0 : number of arguments
324 // -- r1 : constructor function
325 // -- r2 : allocation site or undefined
326 // -- r3 : original constructor
327 // -- lr : return address
328 // -- sp[...]: constructor arguments
329 // -----------------------------------
331 Isolate* isolate = masm->isolate();
333 // Enter a construct frame.
335 FrameAndConstantPoolScope scope(masm, StackFrame::CONSTRUCT);
337 // Preserve the incoming parameters on the stack.
338 __ AssertUndefinedOrAllocationSite(r2, r4);
345 // Try to allocate the object without transitioning into C code. If any of
346 // the preconditions is not met, the code bails out to the runtime call.
347 Label rt_call, allocated;
348 if (FLAG_inline_new) {
349 ExternalReference debug_step_in_fp =
350 ExternalReference::debug_step_in_fp_address(isolate);
351 __ mov(r2, Operand(debug_step_in_fp));
352 __ ldr(r2, MemOperand(r2));
356 // Fall back to runtime if the original constructor and function differ.
360 // Load the initial map and verify that it is in fact a map.
361 // r1: constructor function
362 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
363 __ JumpIfSmi(r2, &rt_call);
364 __ CompareObjectType(r2, r5, r4, MAP_TYPE);
367 // Check that the constructor is not constructing a JSFunction (see
368 // comments in Runtime_NewObject in runtime.cc). In which case the
369 // initial map's instance type would be JS_FUNCTION_TYPE.
370 // r1: constructor function
372 __ CompareInstanceType(r2, r5, JS_FUNCTION_TYPE);
375 if (!is_api_function) {
377 MemOperand bit_field3 = FieldMemOperand(r2, Map::kBitField3Offset);
378 // Check if slack tracking is enabled.
379 __ ldr(r4, bit_field3);
380 __ DecodeField<Map::Counter>(r3, r4);
381 __ cmp(r3, Operand(Map::kSlackTrackingCounterEnd));
383 // Decrease generous allocation count.
384 __ sub(r4, r4, Operand(1 << Map::Counter::kShift));
385 __ str(r4, bit_field3);
386 __ cmp(r3, Operand(Map::kSlackTrackingCounterEnd));
391 __ Push(r2, r1); // r1 = constructor
392 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
400 // Now allocate the JSObject on the heap.
401 // r1: constructor function
403 Label rt_call_reload_new_target;
404 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceSizeOffset));
406 __ Allocate(r3, r4, r5, r6, &rt_call_reload_new_target, SIZE_IN_WORDS);
408 // Allocated the JSObject, now initialize the fields. Map is set to
409 // initial map and properties and elements are set to empty fixed array.
410 // r1: constructor function
413 // r4: JSObject (not tagged)
414 __ LoadRoot(r6, Heap::kEmptyFixedArrayRootIndex);
416 DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
417 __ str(r2, MemOperand(r5, kPointerSize, PostIndex));
418 DCHECK_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
419 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
420 DCHECK_EQ(2 * kPointerSize, JSObject::kElementsOffset);
421 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
423 // Fill all the in-object properties with the appropriate filler.
424 // r1: constructor function
427 // r4: JSObject (not tagged)
428 // r5: First in-object property of JSObject (not tagged)
429 DCHECK_EQ(3 * kPointerSize, JSObject::kHeaderSize);
430 __ LoadRoot(r6, Heap::kUndefinedValueRootIndex);
432 if (!is_api_function) {
433 Label no_inobject_slack_tracking;
435 // Check if slack tracking is enabled.
436 __ ldr(ip, FieldMemOperand(r2, Map::kBitField3Offset));
437 __ DecodeField<Map::Counter>(ip);
438 __ cmp(ip, Operand(Map::kSlackTrackingCounterEnd));
439 __ b(lt, &no_inobject_slack_tracking);
441 // Allocate object with a slack.
442 __ ldr(r0, FieldMemOperand(r2, Map::kInstanceSizesOffset));
443 __ Ubfx(r0, r0, Map::kInObjectPropertiesOrConstructorFunctionIndexByte *
446 __ ldr(r2, FieldMemOperand(r2, Map::kInstanceAttributesOffset));
447 __ Ubfx(r2, r2, Map::kUnusedPropertyFieldsByte * kBitsPerByte,
449 __ sub(r0, r0, Operand(r2));
450 __ add(r0, r5, Operand(r0, LSL, kPointerSizeLog2));
451 // r0: offset of first field after pre-allocated fields
452 if (FLAG_debug_code) {
453 __ add(ip, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
455 __ Assert(le, kUnexpectedNumberOfPreAllocatedPropertyFields);
457 __ InitializeFieldsWithFiller(r5, r0, r6);
458 // To allow for truncation.
459 __ LoadRoot(r6, Heap::kOnePointerFillerMapRootIndex);
460 // Fill the remaining fields with one pointer filler map.
462 __ bind(&no_inobject_slack_tracking);
465 __ add(r0, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
466 __ InitializeFieldsWithFiller(r5, r0, r6);
468 // Add the object tag to make the JSObject real, so that we can continue
469 // and jump into the continuation code at any time from now on.
470 __ add(r4, r4, Operand(kHeapObjectTag));
472 // Continue with JSObject being successfully allocated
476 // Reload the original constructor and fall-through.
477 __ bind(&rt_call_reload_new_target);
478 __ ldr(r3, MemOperand(sp, 0 * kPointerSize));
481 // Allocate the new receiver object using the runtime call.
482 // r1: constructor function
483 // r3: original constructor
486 __ push(r1); // argument 2/1: constructor function
487 __ push(r3); // argument 3/2: original constructor
488 __ CallRuntime(Runtime::kNewObject, 2);
491 // Receiver for constructor call allocated.
495 // Restore the parameters.
499 // Retrieve smi-tagged arguments count from the stack.
500 __ ldr(r0, MemOperand(sp));
503 // Push new.target onto the construct frame. This is stored just below the
504 // receiver on the stack.
509 // Set up pointer to last argument.
510 __ add(r2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
512 // Copy arguments and receiver to the expression stack.
513 // r0: number of arguments
514 // r1: constructor function
515 // r2: address of last argument (caller sp)
516 // r3: number of arguments (smi-tagged)
520 // sp[3]: number of arguments (smi-tagged)
525 __ ldr(ip, MemOperand(r2, r3, LSL, kPointerSizeLog2 - 1));
528 __ sub(r3, r3, Operand(2), SetCC);
531 // Call the function.
532 // r0: number of arguments
533 // r1: constructor function
534 if (is_api_function) {
535 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
537 masm->isolate()->builtins()->HandleApiCallConstruct();
538 __ Call(code, RelocInfo::CODE_TARGET);
540 ParameterCount actual(r0);
541 __ InvokeFunction(r1, actual, CALL_FUNCTION, NullCallWrapper());
544 // Store offset of return address for deoptimizer.
545 if (!is_api_function) {
546 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
549 // Restore context from the frame.
553 // sp[2]: number of arguments (smi-tagged)
554 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
556 // If the result is an object (in the ECMA sense), we should get rid
557 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
559 Label use_receiver, exit;
561 // If the result is a smi, it is *not* an object in the ECMA sense.
565 // sp[2]: number of arguments (smi-tagged)
566 __ JumpIfSmi(r0, &use_receiver);
568 // If the type of the result (stored in its map) is less than
569 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
570 __ CompareObjectType(r0, r1, r3, FIRST_SPEC_OBJECT_TYPE);
573 // Throw away the result of the constructor invocation and use the
574 // on-stack receiver as the result.
575 __ bind(&use_receiver);
576 __ ldr(r0, MemOperand(sp));
578 // Remove receiver from the stack, remove caller arguments, and
582 // sp[0]: receiver (newly allocated object)
583 // sp[1]: new.target (original constructor)
584 // sp[2]: number of arguments (smi-tagged)
585 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
587 // Leave construct frame.
590 __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - 1));
591 __ add(sp, sp, Operand(kPointerSize));
592 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, r1, r2);
597 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
598 Generate_JSConstructStubHelper(masm, false);
602 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
603 Generate_JSConstructStubHelper(masm, true);
607 void Builtins::Generate_JSConstructStubForDerived(MacroAssembler* masm) {
608 // ----------- S t a t e -------------
609 // -- r0 : number of arguments
610 // -- r1 : constructor function
611 // -- r2 : allocation site or undefined
612 // -- r3 : original constructor
613 // -- lr : return address
614 // -- sp[...]: constructor arguments
615 // -----------------------------------
618 FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
620 __ AssertUndefinedOrAllocationSite(r2, r4);
625 __ push(r4); // Smi-tagged arguments count.
630 // receiver is the hole.
631 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
634 // Set up pointer to last argument.
635 __ add(r2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
637 // Copy arguments and receiver to the expression stack.
638 // r0: number of arguments
639 // r1: constructor function
640 // r2: address of last argument (caller sp)
641 // r4: number of arguments (smi-tagged)
644 // sp[2]: number of arguments (smi-tagged)
648 __ ldr(ip, MemOperand(r2, r4, LSL, kPointerSizeLog2 - 1));
651 __ sub(r4, r4, Operand(2), SetCC);
656 ExternalReference debug_step_in_fp =
657 ExternalReference::debug_step_in_fp_address(masm->isolate());
658 __ mov(r2, Operand(debug_step_in_fp));
659 __ ldr(r2, MemOperand(r2));
661 __ b(eq, &skip_step_in);
666 __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
670 __ bind(&skip_step_in);
672 // Call the function.
673 // r0: number of arguments
674 // r1: constructor function
675 ParameterCount actual(r0);
676 __ InvokeFunction(r1, actual, CALL_FUNCTION, NullCallWrapper());
678 // Restore context from the frame.
680 // sp[0]: number of arguments (smi-tagged)
681 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
682 // Get arguments count, skipping over new.target.
683 __ ldr(r1, MemOperand(sp, kPointerSize));
685 // Leave construct frame.
688 __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - 1));
689 __ add(sp, sp, Operand(kPointerSize));
694 enum IsTagged { kArgcIsSmiTagged, kArgcIsUntaggedInt };
697 // Clobbers r2; preserves all other registers.
698 static void Generate_CheckStackOverflow(MacroAssembler* masm,
699 const int calleeOffset, Register argc,
700 IsTagged argc_is_tagged) {
701 // Check the stack for overflow. We are not trying to catch
702 // interruptions (e.g. debug break and preemption) here, so the "real stack
703 // limit" is checked.
705 __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
706 // Make r2 the space we have left. The stack might already be overflowed
707 // here which will cause r2 to become negative.
709 // Check if the arguments will overflow the stack.
710 if (argc_is_tagged == kArgcIsSmiTagged) {
711 __ cmp(r2, Operand::PointerOffsetFromSmiKey(argc));
713 DCHECK(argc_is_tagged == kArgcIsUntaggedInt);
714 __ cmp(r2, Operand(argc, LSL, kPointerSizeLog2));
716 __ b(gt, &okay); // Signed comparison.
718 // Out of stack space.
719 __ ldr(r1, MemOperand(fp, calleeOffset));
720 if (argc_is_tagged == kArgcIsUntaggedInt) {
724 __ CallRuntime(Runtime::kThrowStackOverflow, 0);
730 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
732 // Called from Generate_JS_Entry
738 // r5-r6, r8 (if !FLAG_enable_embedded_constant_pool) and cp may be clobbered
739 ProfileEntryHookStub::MaybeCallEntryHook(masm);
741 // Clear the context before we push it when entering the internal frame.
742 __ mov(cp, Operand::Zero());
744 // Enter an internal frame.
746 FrameScope scope(masm, StackFrame::INTERNAL);
748 // Set up the context from the function argument.
749 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
751 __ InitializeRootRegister();
753 // Push the function and the receiver onto the stack.
757 // Check if we have enough stack space to push all arguments.
758 // The function is the first thing that was pushed above after entering
759 // the internal frame.
760 const int kFunctionOffset =
761 InternalFrameConstants::kCodeOffset - kPointerSize;
763 Generate_CheckStackOverflow(masm, kFunctionOffset, r3, kArgcIsUntaggedInt);
765 // Copy arguments to the stack in a loop.
768 // r4: argv, i.e. points to first arg
770 __ add(r2, r4, Operand(r3, LSL, kPointerSizeLog2));
771 // r2 points past last arg.
774 __ ldr(r0, MemOperand(r4, kPointerSize, PostIndex)); // read next parameter
775 __ ldr(r0, MemOperand(r0)); // dereference handle
776 __ push(r0); // push parameter
781 // Initialize all JavaScript callee-saved registers, since they will be seen
782 // by the garbage collector as part of handlers.
783 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
784 __ mov(r5, Operand(r4));
785 __ mov(r6, Operand(r4));
786 if (!FLAG_enable_embedded_constant_pool) {
787 __ mov(r8, Operand(r4));
789 if (kR9Available == 1) {
790 __ mov(r9, Operand(r4));
793 // Invoke the code and pass argc as r0.
794 __ mov(r0, Operand(r3));
796 // No type feedback cell is available
797 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
798 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
801 __ Call(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
803 // Exit the JS frame and remove the parameters (except function), and
805 // Respect ABI stack constraint.
813 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
814 Generate_JSEntryTrampolineHelper(masm, false);
818 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
819 Generate_JSEntryTrampolineHelper(masm, true);
823 // Generate code for entering a JS function with the interpreter.
824 // On entry to the function the receiver and arguments have been pushed on the
825 // stack left to right. The actual argument count matches the formal parameter
826 // count expected by the function.
828 // The live registers are:
829 // o r1: the JS function object being called.
831 // o pp: the caller's constant pool pointer (if enabled)
832 // o fp: the caller's frame pointer
833 // o sp: stack pointer
834 // o lr: return address
836 // The function builds a JS frame. Please see JavaScriptFrameConstants in
837 // frames-arm.h for its layout.
838 // TODO(rmcilroy): We will need to include the current bytecode pointer in the
840 void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
841 // Open a frame scope to indicate that there is a frame on the stack. The
842 // MANUAL indicates that the scope shouldn't actually generate code to set up
843 // the frame (that is done below).
844 FrameScope frame_scope(masm, StackFrame::MANUAL);
845 __ PushFixedFrame(r1);
846 __ add(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
848 // Get the bytecode array from the function object and load the pointer to the
849 // first entry into kInterpreterBytecodeRegister.
850 __ ldr(r0, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
851 __ ldr(kInterpreterBytecodeArrayRegister,
852 FieldMemOperand(r0, SharedFunctionInfo::kFunctionDataOffset));
854 if (FLAG_debug_code) {
855 // Check function data field is actually a BytecodeArray object.
856 __ SmiTst(kInterpreterBytecodeArrayRegister);
857 __ Assert(ne, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
858 __ CompareObjectType(kInterpreterBytecodeArrayRegister, r0, no_reg,
859 BYTECODE_ARRAY_TYPE);
860 __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
863 // Allocate the local and temporary register file on the stack.
865 // Load frame size from the BytecodeArray object.
866 __ ldr(r4, FieldMemOperand(kInterpreterBytecodeArrayRegister,
867 BytecodeArray::kFrameSizeOffset));
869 // Do a stack check to ensure we don't go over the limit.
871 __ sub(r9, sp, Operand(r4));
872 __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
873 __ cmp(r9, Operand(r2));
875 __ CallRuntime(Runtime::kThrowStackOverflow, 0);
878 // If ok, push undefined as the initial value for all register file entries.
881 __ LoadRoot(r9, Heap::kUndefinedValueRootIndex);
882 __ b(&loop_check, al);
883 __ bind(&loop_header);
884 // TODO(rmcilroy): Consider doing more than one push per loop iteration.
886 // Continue loop if not done.
887 __ bind(&loop_check);
888 __ sub(r4, r4, Operand(kPointerSize), SetCC);
889 __ b(&loop_header, ge);
892 // TODO(rmcilroy): List of things not currently dealt with here but done in
893 // fullcodegen's prologue:
894 // - Support profiler (specifically profiling_counter).
895 // - Call ProfileEntryHookStub when isolate has a function_entry_hook.
896 // - Allow simulator stop operations if FLAG_stop_at is set.
897 // - Deal with sloppy mode functions which need to replace the
898 // receiver with the global proxy when called as functions (without an
899 // explicit receiver object).
900 // - Code aging of the BytecodeArray object.
901 // - Supporting FLAG_trace.
903 // The following items are also not done here, and will probably be done using
904 // explicit bytecodes instead:
905 // - Allocating a new local context if applicable.
906 // - Setting up a local binding to the this function, which is used in
907 // derived constructors with super calls.
908 // - Setting new.target if required.
909 // - Dealing with REST parameters (only if
910 // https://codereview.chromium.org/1235153006 doesn't land by then).
911 // - Dealing with argument objects.
913 // Perform stack guard check.
916 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
917 __ cmp(sp, Operand(ip));
919 __ CallRuntime(Runtime::kStackGuard, 0);
923 // Load accumulator, register file, bytecode offset, dispatch table into
925 __ LoadRoot(kInterpreterAccumulatorRegister, Heap::kUndefinedValueRootIndex);
926 __ sub(kInterpreterRegisterFileRegister, fp,
927 Operand(kPointerSize + StandardFrameConstants::kFixedFrameSizeFromFp));
928 __ mov(kInterpreterBytecodeOffsetRegister,
929 Operand(BytecodeArray::kHeaderSize - kHeapObjectTag));
930 __ LoadRoot(kInterpreterDispatchTableRegister,
931 Heap::kInterpreterTableRootIndex);
932 __ add(kInterpreterDispatchTableRegister, kInterpreterDispatchTableRegister,
933 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
935 // Dispatch to the first bytecode handler for the function.
936 __ ldrb(r1, MemOperand(kInterpreterBytecodeArrayRegister,
937 kInterpreterBytecodeOffsetRegister));
938 __ ldr(ip, MemOperand(kInterpreterDispatchTableRegister, r1, LSL,
940 // TODO(rmcilroy): Make dispatch table point to code entrys to avoid untagging
941 // and header removal.
942 __ add(ip, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
947 void Builtins::Generate_InterpreterExitTrampoline(MacroAssembler* masm) {
948 // TODO(rmcilroy): List of things not currently dealt with here but done in
949 // fullcodegen's EmitReturnSequence.
950 // - Supporting FLAG_trace for Runtime::TraceExit.
951 // - Support profiler (specifically decrementing profiling_counter
952 // appropriately and calling out to HandleInterrupts if necessary).
954 // The return value is in accumulator, which is already in r0.
956 // Leave the frame (also dropping the register file).
957 __ LeaveFrame(StackFrame::JAVA_SCRIPT);
959 // Drop receiver + arguments and return.
960 __ ldr(ip, FieldMemOperand(kInterpreterBytecodeArrayRegister,
961 BytecodeArray::kParameterSizeOffset));
962 __ add(sp, sp, ip, LeaveCC);
967 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
968 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
969 GenerateTailCallToReturnedCode(masm);
973 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
974 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
975 // Push a copy of the function onto the stack.
977 // Push function as parameter to the runtime call.
979 // Whether to compile in a background thread.
981 ip, concurrent ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
984 __ CallRuntime(Runtime::kCompileOptimized, 2);
990 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
991 CallCompileOptimized(masm, false);
992 GenerateTailCallToReturnedCode(masm);
996 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
997 CallCompileOptimized(masm, true);
998 GenerateTailCallToReturnedCode(masm);
1002 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
1003 // For now, we are relying on the fact that make_code_young doesn't do any
1004 // garbage collection which allows us to save/restore the registers without
1005 // worrying about which of them contain pointers. We also don't build an
1006 // internal frame to make the code faster, since we shouldn't have to do stack
1007 // crawls in MakeCodeYoung. This seems a bit fragile.
1009 // The following registers must be saved and restored when calling through to
1011 // r0 - contains return address (beginning of patch sequence)
1013 FrameScope scope(masm, StackFrame::MANUAL);
1014 __ stm(db_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
1015 __ PrepareCallCFunction(2, 0, r2);
1016 __ mov(r1, Operand(ExternalReference::isolate_address(masm->isolate())));
1018 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
1019 __ ldm(ia_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
1023 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
1024 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
1025 MacroAssembler* masm) { \
1026 GenerateMakeCodeYoungAgainCommon(masm); \
1028 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
1029 MacroAssembler* masm) { \
1030 GenerateMakeCodeYoungAgainCommon(masm); \
1032 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
1033 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
1036 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
1037 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
1038 // that make_code_young doesn't do any garbage collection which allows us to
1039 // save/restore the registers without worrying about which of them contain
1042 // The following registers must be saved and restored when calling through to
1044 // r0 - contains return address (beginning of patch sequence)
1046 FrameScope scope(masm, StackFrame::MANUAL);
1047 __ stm(db_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
1048 __ PrepareCallCFunction(2, 0, r2);
1049 __ mov(r1, Operand(ExternalReference::isolate_address(masm->isolate())));
1050 __ CallCFunction(ExternalReference::get_mark_code_as_executed_function(
1051 masm->isolate()), 2);
1052 __ ldm(ia_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
1054 // Perform prologue operations usually performed by the young code stub.
1055 __ PushFixedFrame(r1);
1056 __ add(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
1058 // Jump to point after the code-age stub.
1059 __ add(r0, r0, Operand(kNoCodeAgeSequenceLength));
1064 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
1065 GenerateMakeCodeYoungAgainCommon(masm);
1069 void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
1070 Generate_MarkCodeAsExecutedOnce(masm);
1074 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
1075 SaveFPRegsMode save_doubles) {
1077 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1079 // Preserve registers across notification, this is important for compiled
1080 // stubs that tail call the runtime on deopts passing their parameters in
1082 __ stm(db_w, sp, kJSCallerSaved | kCalleeSaved);
1083 // Pass the function and deoptimization type to the runtime system.
1084 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
1085 __ ldm(ia_w, sp, kJSCallerSaved | kCalleeSaved);
1088 __ add(sp, sp, Operand(kPointerSize)); // Ignore state
1089 __ mov(pc, lr); // Jump to miss handler
1093 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
1094 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
1098 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
1099 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
1103 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1104 Deoptimizer::BailoutType type) {
1106 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1107 // Pass the function and deoptimization type to the runtime system.
1108 __ mov(r0, Operand(Smi::FromInt(static_cast<int>(type))));
1110 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
1113 // Get the full codegen state from the stack and untag it -> r6.
1114 __ ldr(r6, MemOperand(sp, 0 * kPointerSize));
1116 // Switch on the state.
1117 Label with_tos_register, unknown_state;
1118 __ cmp(r6, Operand(FullCodeGenerator::NO_REGISTERS));
1119 __ b(ne, &with_tos_register);
1120 __ add(sp, sp, Operand(1 * kPointerSize)); // Remove state.
1123 __ bind(&with_tos_register);
1124 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
1125 __ cmp(r6, Operand(FullCodeGenerator::TOS_REG));
1126 __ b(ne, &unknown_state);
1127 __ add(sp, sp, Operand(2 * kPointerSize)); // Remove state.
1130 __ bind(&unknown_state);
1131 __ stop("no cases left");
1135 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1136 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1140 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
1141 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1145 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1146 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1150 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1151 // Lookup the function in the JavaScript frame.
1152 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1154 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1155 // Pass function as argument.
1157 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1160 // If the code object is null, just return to the unoptimized code.
1162 __ cmp(r0, Operand(Smi::FromInt(0)));
1168 // Load deoptimization data from the code object.
1169 // <deopt_data> = <code>[#deoptimization_data_offset]
1170 __ ldr(r1, FieldMemOperand(r0, Code::kDeoptimizationDataOffset));
1172 { ConstantPoolUnavailableScope constant_pool_unavailable(masm);
1173 __ add(r0, r0, Operand(Code::kHeaderSize - kHeapObjectTag)); // Code start
1175 if (FLAG_enable_embedded_constant_pool) {
1176 __ LoadConstantPoolPointerRegisterFromCodeTargetAddress(r0);
1179 // Load the OSR entrypoint offset from the deoptimization data.
1180 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1181 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(
1182 DeoptimizationInputData::kOsrPcOffsetIndex)));
1184 // Compute the target address = code start + osr_offset
1185 __ add(lr, r0, Operand::SmiUntag(r1));
1187 // And "return" to the OSR entry point of the function.
1193 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1194 // We check the stack limit as indicator that recompilation might be done.
1196 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1197 __ cmp(sp, Operand(ip));
1200 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1201 __ CallRuntime(Runtime::kStackGuard, 0);
1203 __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
1204 RelocInfo::CODE_TARGET);
1212 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1213 // 1. Make sure we have at least one argument.
1214 // r0: actual number of arguments
1217 __ cmp(r0, Operand::Zero());
1219 __ PushRoot(Heap::kUndefinedValueRootIndex);
1220 __ add(r0, r0, Operand(1));
1224 // 2. Get the callable to call (passed as receiver) from the stack.
1225 // r0: actual number of arguments
1226 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1228 // 3. Shift arguments and return address one slot down on the stack
1229 // (overwriting the original receiver). Adjust argument count to make
1230 // the original first argument the new receiver.
1231 // r0: actual number of arguments
1235 // Calculate the copy start address (destination). Copy end address is sp.
1236 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1239 __ ldr(ip, MemOperand(r2, -kPointerSize));
1240 __ str(ip, MemOperand(r2));
1241 __ sub(r2, r2, Operand(kPointerSize));
1244 // Adjust the actual number of arguments and remove the top element
1245 // (which is a copy of the last argument).
1246 __ sub(r0, r0, Operand(1));
1250 // 4. Call the callable.
1251 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1255 static void Generate_PushAppliedArguments(MacroAssembler* masm,
1256 const int vectorOffset,
1257 const int argumentsOffset,
1258 const int indexOffset,
1259 const int limitOffset) {
1261 Register receiver = LoadDescriptor::ReceiverRegister();
1262 Register key = LoadDescriptor::NameRegister();
1263 Register slot = LoadDescriptor::SlotRegister();
1264 Register vector = LoadWithVectorDescriptor::VectorRegister();
1266 __ ldr(key, MemOperand(fp, indexOffset));
1269 // Load the current argument from the arguments array.
1271 __ ldr(receiver, MemOperand(fp, argumentsOffset));
1273 // Use inline caching to speed up access to arguments.
1274 int slot_index = TypeFeedbackVector::PushAppliedArgumentsIndex();
1275 __ mov(slot, Operand(Smi::FromInt(slot_index)));
1276 __ ldr(vector, MemOperand(fp, vectorOffset));
1278 KeyedLoadICStub(masm->isolate(), LoadICState(kNoExtraICState)).GetCode();
1279 __ Call(ic, RelocInfo::CODE_TARGET);
1281 // Push the nth argument.
1284 __ ldr(key, MemOperand(fp, indexOffset));
1285 __ add(key, key, Operand(1 << kSmiTagSize));
1286 __ str(key, MemOperand(fp, indexOffset));
1288 // Test if the copy loop has finished copying all the elements from the
1289 // arguments object.
1291 __ ldr(r1, MemOperand(fp, limitOffset));
1295 // On exit, the pushed arguments count is in r0, untagged
1301 // Used by FunctionApply and ReflectApply
1302 static void Generate_ApplyHelper(MacroAssembler* masm, bool targetIsArgument) {
1303 const int kFormalParameters = targetIsArgument ? 3 : 2;
1304 const int kStackSize = kFormalParameters + 1;
1307 FrameAndConstantPoolScope frame_scope(masm, StackFrame::INTERNAL);
1308 const int kArgumentsOffset = kFPOnStackSize + kPCOnStackSize;
1309 const int kReceiverOffset = kArgumentsOffset + kPointerSize;
1310 const int kFunctionOffset = kReceiverOffset + kPointerSize;
1311 const int kVectorOffset =
1312 InternalFrameConstants::kCodeOffset - 1 * kPointerSize;
1315 __ ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1316 __ ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kFeedbackVectorOffset));
1319 __ ldr(r0, MemOperand(fp, kFunctionOffset)); // get the function
1320 __ ldr(r1, MemOperand(fp, kArgumentsOffset)); // get the args array
1322 if (targetIsArgument) {
1323 __ InvokeBuiltin(Context::REFLECT_APPLY_PREPARE_BUILTIN_INDEX,
1326 __ InvokeBuiltin(Context::APPLY_PREPARE_BUILTIN_INDEX, CALL_FUNCTION);
1329 Generate_CheckStackOverflow(masm, kFunctionOffset, r0, kArgcIsSmiTagged);
1331 // Push current limit and index.
1332 const int kIndexOffset = kVectorOffset - (2 * kPointerSize);
1333 const int kLimitOffset = kVectorOffset - (1 * kPointerSize);
1334 __ mov(r1, Operand::Zero());
1335 __ ldr(r2, MemOperand(fp, kReceiverOffset));
1336 __ Push(r0, r1, r2); // limit, initial index and receiver.
1338 // Copy all arguments from the array to the stack.
1339 Generate_PushAppliedArguments(masm, kVectorOffset, kArgumentsOffset,
1340 kIndexOffset, kLimitOffset);
1342 // Call the callable.
1343 // TODO(bmeurer): This should be a tail call according to ES6.
1344 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1345 __ Call(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1347 // Tear down the internal frame and remove function, receiver and args.
1349 __ add(sp, sp, Operand(kStackSize * kPointerSize));
1354 static void Generate_ConstructHelper(MacroAssembler* masm) {
1355 const int kFormalParameters = 3;
1356 const int kStackSize = kFormalParameters + 1;
1359 FrameAndConstantPoolScope frame_scope(masm, StackFrame::INTERNAL);
1360 const int kNewTargetOffset = kFPOnStackSize + kPCOnStackSize;
1361 const int kArgumentsOffset = kNewTargetOffset + kPointerSize;
1362 const int kFunctionOffset = kArgumentsOffset + kPointerSize;
1363 static const int kVectorOffset =
1364 InternalFrameConstants::kCodeOffset - 1 * kPointerSize;
1367 __ ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1368 __ ldr(r1, FieldMemOperand(r1, SharedFunctionInfo::kFeedbackVectorOffset));
1371 // If newTarget is not supplied, set it to constructor
1372 Label validate_arguments;
1373 __ ldr(r0, MemOperand(fp, kNewTargetOffset));
1374 __ CompareRoot(r0, Heap::kUndefinedValueRootIndex);
1375 __ b(ne, &validate_arguments);
1376 __ ldr(r0, MemOperand(fp, kFunctionOffset));
1377 __ str(r0, MemOperand(fp, kNewTargetOffset));
1379 // Validate arguments
1380 __ bind(&validate_arguments);
1381 __ ldr(r0, MemOperand(fp, kFunctionOffset)); // get the function
1383 __ ldr(r0, MemOperand(fp, kArgumentsOffset)); // get the args array
1385 __ ldr(r0, MemOperand(fp, kNewTargetOffset)); // get the new.target
1387 __ InvokeBuiltin(Context::REFLECT_CONSTRUCT_PREPARE_BUILTIN_INDEX,
1390 Generate_CheckStackOverflow(masm, kFunctionOffset, r0, kArgcIsSmiTagged);
1392 // Push current limit and index.
1393 const int kIndexOffset = kVectorOffset - (2 * kPointerSize);
1394 const int kLimitOffset = kVectorOffset - (1 * kPointerSize);
1395 __ push(r0); // limit
1396 __ mov(r1, Operand::Zero()); // initial index
1398 // Push the constructor function as callee.
1399 __ ldr(r0, MemOperand(fp, kFunctionOffset));
1402 // Copy all arguments from the array to the stack.
1403 Generate_PushAppliedArguments(masm, kVectorOffset, kArgumentsOffset,
1404 kIndexOffset, kLimitOffset);
1406 // Use undefined feedback vector
1407 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1408 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1409 __ ldr(r4, MemOperand(fp, kNewTargetOffset));
1411 // Call the function.
1412 CallConstructStub stub(masm->isolate(), SUPER_CONSTRUCTOR_CALL);
1413 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
1415 // Leave internal frame.
1417 __ add(sp, sp, Operand(kStackSize * kPointerSize));
1422 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1423 Generate_ApplyHelper(masm, false);
1427 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1428 Generate_ApplyHelper(masm, true);
1432 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1433 Generate_ConstructHelper(masm);
1437 static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1438 Label* stack_overflow) {
1439 // ----------- S t a t e -------------
1440 // -- r0 : actual number of arguments
1441 // -- r1 : function (passed through to callee)
1442 // -- r2 : expected number of arguments
1443 // -----------------------------------
1444 // Check the stack for overflow. We are not trying to catch
1445 // interruptions (e.g. debug break and preemption) here, so the "real stack
1446 // limit" is checked.
1447 __ LoadRoot(r5, Heap::kRealStackLimitRootIndex);
1448 // Make r5 the space we have left. The stack might already be overflowed
1449 // here which will cause r5 to become negative.
1451 // Check if the arguments will overflow the stack.
1452 __ cmp(r5, Operand(r2, LSL, kPointerSizeLog2));
1453 __ b(le, stack_overflow); // Signed comparison.
1457 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1459 __ mov(r4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1460 __ stm(db_w, sp, r0.bit() | r1.bit() | r4.bit() |
1461 (FLAG_enable_embedded_constant_pool ? pp.bit() : 0) |
1462 fp.bit() | lr.bit());
1464 Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
1468 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1469 // ----------- S t a t e -------------
1470 // -- r0 : result being passed through
1471 // -----------------------------------
1472 // Get the number of arguments passed (as a smi), tear down the frame and
1473 // then tear down the parameters.
1474 __ ldr(r1, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1477 __ LeaveFrame(StackFrame::ARGUMENTS_ADAPTOR);
1478 __ add(sp, sp, Operand::PointerOffsetFromSmiKey(r1));
1479 __ add(sp, sp, Operand(kPointerSize)); // adjust for receiver
1484 void Builtins::Generate_CallFunction(MacroAssembler* masm) {
1485 // ----------- S t a t e -------------
1486 // -- r0 : the number of arguments (not including the receiver)
1487 // -- r1 : the function to call (checked to be a JSFunction)
1488 // -----------------------------------
1490 Label convert, convert_global_proxy, convert_to_object, done_convert;
1491 __ AssertFunction(r1);
1492 // TODO(bmeurer): Throw a TypeError if function's [[FunctionKind]] internal
1493 // slot is "classConstructor".
1494 // Enter the context of the function; ToObject has to run in the function
1495 // context, and we also need to take the global proxy from the function
1496 // context in case of conversion.
1497 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
1498 STATIC_ASSERT(SharedFunctionInfo::kNativeByteOffset ==
1499 SharedFunctionInfo::kStrictModeByteOffset);
1500 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1501 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1502 // We need to convert the receiver for non-native sloppy mode functions.
1503 __ ldrb(r3, FieldMemOperand(r2, SharedFunctionInfo::kNativeByteOffset));
1504 __ tst(r3, Operand((1 << SharedFunctionInfo::kNativeBitWithinByte) |
1505 (1 << SharedFunctionInfo::kStrictModeBitWithinByte)));
1506 __ b(ne, &done_convert);
1508 __ ldr(r3, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1510 // ----------- S t a t e -------------
1511 // -- r0 : the number of arguments (not including the receiver)
1512 // -- r1 : the function to call (checked to be a JSFunction)
1513 // -- r2 : the shared function info.
1514 // -- r3 : the receiver
1515 // -- cp : the function context.
1516 // -----------------------------------
1518 Label convert_receiver;
1519 __ JumpIfSmi(r3, &convert_to_object);
1520 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
1521 __ CompareObjectType(r3, r4, r4, FIRST_JS_RECEIVER_TYPE);
1522 __ b(hs, &done_convert);
1523 __ JumpIfRoot(r3, Heap::kUndefinedValueRootIndex, &convert_global_proxy);
1524 __ JumpIfNotRoot(r3, Heap::kNullValueRootIndex, &convert_to_object);
1525 __ bind(&convert_global_proxy);
1527 // Patch receiver to global proxy.
1528 __ LoadGlobalProxy(r3);
1530 __ b(&convert_receiver);
1531 __ bind(&convert_to_object);
1533 // Convert receiver using ToObject.
1534 // TODO(bmeurer): Inline the allocation here to avoid building the frame
1535 // in the fast case? (fall back to AllocateInNewSpace?)
1536 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1540 ToObjectStub stub(masm->isolate());
1546 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1547 __ bind(&convert_receiver);
1548 __ str(r3, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1550 __ bind(&done_convert);
1552 // ----------- S t a t e -------------
1553 // -- r0 : the number of arguments (not including the receiver)
1554 // -- r1 : the function to call (checked to be a JSFunction)
1555 // -- r2 : the shared function info.
1556 // -- cp : the function context.
1557 // -----------------------------------
1560 FieldMemOperand(r2, SharedFunctionInfo::kFormalParameterCountOffset));
1562 __ ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
1563 ParameterCount actual(r0);
1564 ParameterCount expected(r2);
1565 __ InvokeCode(r3, expected, actual, JUMP_FUNCTION, NullCallWrapper());
1570 void Builtins::Generate_Call(MacroAssembler* masm) {
1571 // ----------- S t a t e -------------
1572 // -- r0 : the number of arguments (not including the receiver)
1573 // -- r1 : the target to call (can be any Object).
1574 // -----------------------------------
1576 Label non_smi, non_function;
1577 __ JumpIfSmi(r1, &non_function);
1579 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1580 __ Jump(masm->isolate()->builtins()->CallFunction(), RelocInfo::CODE_TARGET,
1582 __ cmp(r2, Operand(JS_FUNCTION_PROXY_TYPE));
1583 __ b(ne, &non_function);
1585 // 1. Call to function proxy.
1586 // TODO(neis): This doesn't match the ES6 spec for [[Call]] on proxies.
1587 __ ldr(r1, FieldMemOperand(r1, JSFunctionProxy::kCallTrapOffset));
1588 __ AssertNotSmi(r1);
1591 // 2. Call to something else, which might have a [[Call]] internal method (if
1592 // not we raise an exception).
1593 __ bind(&non_function);
1594 // TODO(bmeurer): I wonder why we prefer to have slow API calls? This could
1595 // be awesome instead; i.e. a trivial improvement would be to call into the
1596 // runtime and just deal with the API function there instead of returning a
1597 // delegate from a runtime call that just jumps back to the runtime once
1598 // called. Or, bonus points, call directly into the C API function here, as
1599 // we do in some Crankshaft fast cases.
1600 // Overwrite the original receiver with the (original) target.
1601 __ str(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1603 // Determine the delegate for the target (if any).
1604 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1607 __ CallRuntime(Runtime::kGetFunctionDelegate, 1);
1612 // The delegate is always a regular function.
1613 __ AssertFunction(r1);
1614 __ Jump(masm->isolate()->builtins()->CallFunction(), RelocInfo::CODE_TARGET);
1619 void Builtins::Generate_PushArgsAndCall(MacroAssembler* masm) {
1620 // ----------- S t a t e -------------
1621 // -- r0 : the number of arguments (not including the receiver)
1622 // -- r2 : the address of the first argument to be pushed. Subsequent
1623 // arguments should be consecutive above this, in the same order as
1624 // they are to be pushed onto the stack.
1625 // -- r1 : the target to call (can be any Object).
1627 // Find the address of the last argument.
1628 __ add(r3, r0, Operand(1)); // Add one for receiver.
1629 __ mov(r3, Operand(r3, LSL, kPointerSizeLog2));
1632 // Push the arguments.
1633 Label loop_header, loop_check;
1634 __ b(al, &loop_check);
1635 __ bind(&loop_header);
1636 __ ldr(r4, MemOperand(r2, -kPointerSize, PostIndex));
1638 __ bind(&loop_check);
1640 __ b(gt, &loop_header);
1643 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1647 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1648 // ----------- S t a t e -------------
1649 // -- r0 : actual number of arguments
1650 // -- r1 : function (passed through to callee)
1651 // -- r2 : expected number of arguments
1652 // -----------------------------------
1654 Label stack_overflow;
1655 ArgumentAdaptorStackCheck(masm, &stack_overflow);
1656 Label invoke, dont_adapt_arguments;
1658 Label enough, too_few;
1659 __ ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
1662 __ cmp(r2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1663 __ b(eq, &dont_adapt_arguments);
1665 { // Enough parameters: actual >= expected
1667 EnterArgumentsAdaptorFrame(masm);
1669 // Calculate copy start address into r0 and copy end address into r4.
1670 // r0: actual number of arguments as a smi
1672 // r2: expected number of arguments
1673 // r3: code entry to call
1674 __ add(r0, fp, Operand::PointerOffsetFromSmiKey(r0));
1675 // adjust for return address and receiver
1676 __ add(r0, r0, Operand(2 * kPointerSize));
1677 __ sub(r4, r0, Operand(r2, LSL, kPointerSizeLog2));
1679 // Copy the arguments (including the receiver) to the new stack frame.
1680 // r0: copy start address
1682 // r2: expected number of arguments
1683 // r3: code entry to call
1684 // r4: copy end address
1688 __ ldr(ip, MemOperand(r0, 0));
1690 __ cmp(r0, r4); // Compare before moving to next argument.
1691 __ sub(r0, r0, Operand(kPointerSize));
1697 { // Too few parameters: Actual < expected
1700 // If the function is strong we need to throw an error.
1701 Label no_strong_error;
1702 __ ldr(r4, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1703 __ ldr(r5, FieldMemOperand(r4, SharedFunctionInfo::kCompilerHintsOffset));
1704 __ tst(r5, Operand(1 << (SharedFunctionInfo::kStrongModeFunction +
1706 __ b(eq, &no_strong_error);
1708 // What we really care about is the required number of arguments.
1709 __ ldr(r4, FieldMemOperand(r4, SharedFunctionInfo::kLengthOffset));
1710 __ cmp(r0, Operand::SmiUntag(r4));
1711 __ b(ge, &no_strong_error);
1714 FrameScope frame(masm, StackFrame::MANUAL);
1715 EnterArgumentsAdaptorFrame(masm);
1716 __ CallRuntime(Runtime::kThrowStrongModeTooFewArguments, 0);
1719 __ bind(&no_strong_error);
1720 EnterArgumentsAdaptorFrame(masm);
1722 // Calculate copy start address into r0 and copy end address is fp.
1723 // r0: actual number of arguments as a smi
1725 // r2: expected number of arguments
1726 // r3: code entry to call
1727 __ add(r0, fp, Operand::PointerOffsetFromSmiKey(r0));
1729 // Copy the arguments (including the receiver) to the new stack frame.
1730 // r0: copy start address
1732 // r2: expected number of arguments
1733 // r3: code entry to call
1736 // Adjust load for return address and receiver.
1737 __ ldr(ip, MemOperand(r0, 2 * kPointerSize));
1739 __ cmp(r0, fp); // Compare before moving to next argument.
1740 __ sub(r0, r0, Operand(kPointerSize));
1743 // Fill the remaining expected arguments with undefined.
1745 // r2: expected number of arguments
1746 // r3: code entry to call
1747 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1748 __ sub(r4, fp, Operand(r2, LSL, kPointerSizeLog2));
1749 // Adjust for frame.
1750 __ sub(r4, r4, Operand(StandardFrameConstants::kFixedFrameSizeFromFp +
1760 // Call the entry point.
1763 // r0 : expected number of arguments
1764 // r1 : function (passed through to callee)
1767 // Store offset of return address for deoptimizer.
1768 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1770 // Exit frame and return.
1771 LeaveArgumentsAdaptorFrame(masm);
1775 // -------------------------------------------
1776 // Dont adapt arguments.
1777 // -------------------------------------------
1778 __ bind(&dont_adapt_arguments);
1781 __ bind(&stack_overflow);
1783 FrameScope frame(masm, StackFrame::MANUAL);
1784 EnterArgumentsAdaptorFrame(masm);
1785 __ CallRuntime(Runtime::kThrowStackOverflow, 0);
1793 } // namespace internal
1796 #endif // V8_TARGET_ARCH_ARM