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.
9 #include "src/codegen.h"
10 #include "src/debug.h"
11 #include "src/deoptimizer.h"
12 #include "src/full-codegen.h"
13 #include "src/runtime.h"
19 #define __ ACCESS_MASM(masm)
22 void Builtins::Generate_Adaptor(MacroAssembler* masm,
24 BuiltinExtraArguments extra_args) {
25 // ----------- S t a t e -------------
26 // -- r0 : number of arguments excluding receiver
27 // -- r1 : called function (only guaranteed when
28 // extra_args requires it)
30 // -- sp[0] : last argument
32 // -- sp[4 * (argc - 1)] : first argument (argc == r0)
33 // -- sp[4 * argc] : receiver
34 // -----------------------------------
36 // Insert extra arguments.
37 int num_extra_args = 0;
38 if (extra_args == NEEDS_CALLED_FUNCTION) {
42 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
45 // JumpToExternalReference expects r0 to contain the number of arguments
46 // including the receiver and the extra arguments.
47 __ add(r0, r0, Operand(num_extra_args + 1));
48 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
52 // Load the built-in InternalArray function from the current context.
53 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
55 // Load the native context.
58 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
60 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
61 // Load the InternalArray function from the native context.
65 Context::INTERNAL_ARRAY_FUNCTION_INDEX)));
69 // Load the built-in Array function from the current context.
70 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
71 // Load the native context.
74 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
76 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
77 // Load the Array function from the native context.
80 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
84 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
85 // ----------- S t a t e -------------
86 // -- r0 : number of arguments
87 // -- lr : return address
88 // -- sp[...]: constructor arguments
89 // -----------------------------------
90 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
92 // Get the InternalArray function.
93 GenerateLoadInternalArrayFunction(masm, r1);
95 if (FLAG_debug_code) {
96 // Initial map for the builtin InternalArray functions should be maps.
97 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
99 __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction);
100 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
101 __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction);
104 // Run the native code for the InternalArray function called as a normal
107 InternalArrayConstructorStub stub(masm->isolate());
108 __ TailCallStub(&stub);
112 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
113 // ----------- S t a t e -------------
114 // -- r0 : number of arguments
115 // -- lr : return address
116 // -- sp[...]: constructor arguments
117 // -----------------------------------
118 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
120 // Get the Array function.
121 GenerateLoadArrayFunction(masm, r1);
123 if (FLAG_debug_code) {
124 // Initial map for the builtin Array functions should be maps.
125 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
127 __ Assert(ne, kUnexpectedInitialMapForArrayFunction);
128 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
129 __ Assert(eq, kUnexpectedInitialMapForArrayFunction);
132 // Run the native code for the Array function called as a normal function.
134 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
135 ArrayConstructorStub stub(masm->isolate());
136 __ TailCallStub(&stub);
140 void Builtins::Generate_StringConstructCode(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 // -----------------------------------
148 Counters* counters = masm->isolate()->counters();
149 __ IncrementCounter(counters->string_ctor_calls(), 1, r2, r3);
151 Register function = r1;
152 if (FLAG_debug_code) {
153 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, r2);
154 __ cmp(function, Operand(r2));
155 __ Assert(eq, kUnexpectedStringFunction);
158 // Load the first arguments in r0 and get rid of the rest.
160 __ cmp(r0, Operand::Zero());
161 __ b(eq, &no_arguments);
162 // First args = sp[(argc - 1) * 4].
163 __ sub(r0, r0, Operand(1));
164 __ ldr(r0, MemOperand(sp, r0, LSL, kPointerSizeLog2, PreIndex));
165 // sp now point to args[0], drop args[0] + receiver.
168 Register argument = r2;
169 Label not_cached, argument_is_string;
170 __ LookupNumberStringCache(r0, // Input.
176 __ IncrementCounter(counters->string_ctor_cached_number(), 1, r3, r4);
177 __ bind(&argument_is_string);
179 // ----------- S t a t e -------------
180 // -- r2 : argument converted to string
181 // -- r1 : constructor function
182 // -- lr : return address
183 // -----------------------------------
186 __ Allocate(JSValue::kSize,
193 // Initialising the String Object.
195 __ LoadGlobalFunctionInitialMap(function, map, r4);
196 if (FLAG_debug_code) {
197 __ ldrb(r4, FieldMemOperand(map, Map::kInstanceSizeOffset));
198 __ cmp(r4, Operand(JSValue::kSize >> kPointerSizeLog2));
199 __ Assert(eq, kUnexpectedStringWrapperInstanceSize);
200 __ ldrb(r4, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
201 __ cmp(r4, Operand::Zero());
202 __ Assert(eq, kUnexpectedUnusedPropertiesOfStringWrapper);
204 __ str(map, FieldMemOperand(r0, HeapObject::kMapOffset));
206 __ LoadRoot(r3, Heap::kEmptyFixedArrayRootIndex);
207 __ str(r3, FieldMemOperand(r0, JSObject::kPropertiesOffset));
208 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
210 __ str(argument, FieldMemOperand(r0, JSValue::kValueOffset));
212 // Ensure the object is fully initialized.
213 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
217 // The argument was not found in the number to string cache. Check
218 // if it's a string already before calling the conversion builtin.
219 Label convert_argument;
220 __ bind(¬_cached);
221 __ JumpIfSmi(r0, &convert_argument);
224 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
225 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceTypeOffset));
226 STATIC_ASSERT(kNotStringTag != 0);
227 __ tst(r3, Operand(kIsNotStringMask));
228 __ b(ne, &convert_argument);
229 __ mov(argument, r0);
230 __ IncrementCounter(counters->string_ctor_conversions(), 1, r3, r4);
231 __ b(&argument_is_string);
233 // Invoke the conversion builtin and put the result into r2.
234 __ bind(&convert_argument);
235 __ push(function); // Preserve the function.
236 __ IncrementCounter(counters->string_ctor_conversions(), 1, r3, r4);
238 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
240 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
243 __ mov(argument, r0);
244 __ b(&argument_is_string);
246 // Load the empty string into r2, remove the receiver from the
247 // stack, and jump back to the case where the argument is a string.
248 __ bind(&no_arguments);
249 __ LoadRoot(argument, Heap::kempty_stringRootIndex);
251 __ b(&argument_is_string);
253 // At this point the argument is already a string. Call runtime to
254 // create a string wrapper.
255 __ bind(&gc_required);
256 __ IncrementCounter(counters->string_ctor_gc_required(), 1, r3, r4);
258 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
260 __ CallRuntime(Runtime::kNewStringWrapper, 1);
266 static void CallRuntimePassFunction(
267 MacroAssembler* masm, Runtime::FunctionId function_id) {
268 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
269 // Push a copy of the function onto the stack.
271 // Push function as parameter to the runtime call.
274 __ CallRuntime(function_id, 1);
280 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
281 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
282 __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kCodeOffset));
283 __ add(r2, r2, Operand(Code::kHeaderSize - kHeapObjectTag));
288 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
289 __ add(r0, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
294 void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
295 // Checking whether the queued function is ready for install is optional,
296 // since we come across interrupts and stack checks elsewhere. However,
297 // not checking may delay installing ready functions, and always checking
298 // would be quite expensive. A good compromise is to first check against
299 // stack limit as a cue for an interrupt signal.
301 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
302 __ cmp(sp, Operand(ip));
305 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
306 GenerateTailCallToReturnedCode(masm);
309 GenerateTailCallToSharedCode(masm);
313 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
314 bool is_api_function,
315 bool create_memento) {
316 // ----------- S t a t e -------------
317 // -- r0 : number of arguments
318 // -- r1 : constructor function
319 // -- r2 : allocation site or undefined
320 // -- lr : return address
321 // -- sp[...]: constructor arguments
322 // -----------------------------------
324 // Should never create mementos for api functions.
325 DCHECK(!is_api_function || !create_memento);
327 Isolate* isolate = masm->isolate();
329 // Enter a construct frame.
331 FrameAndConstantPoolScope scope(masm, StackFrame::CONSTRUCT);
333 if (create_memento) {
334 __ AssertUndefinedOrAllocationSite(r2, r3);
338 // Preserve the two incoming parameters on the stack.
340 __ push(r0); // Smi-tagged arguments count.
341 __ push(r1); // Constructor function.
343 // Try to allocate the object without transitioning into C code. If any of
344 // the preconditions is not met, the code bails out to the runtime call.
345 Label rt_call, allocated;
346 if (FLAG_inline_new) {
347 Label undo_allocation;
348 ExternalReference debug_step_in_fp =
349 ExternalReference::debug_step_in_fp_address(isolate);
350 __ mov(r2, Operand(debug_step_in_fp));
351 __ ldr(r2, MemOperand(r2));
355 // Load the initial map and verify that it is in fact a map.
356 // r1: constructor function
357 __ ldr(r2, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
358 __ JumpIfSmi(r2, &rt_call);
359 __ CompareObjectType(r2, r3, r4, MAP_TYPE);
362 // Check that the constructor is not constructing a JSFunction (see
363 // comments in Runtime_NewObject in runtime.cc). In which case the
364 // initial map's instance type would be JS_FUNCTION_TYPE.
365 // r1: constructor function
367 __ CompareInstanceType(r2, r3, JS_FUNCTION_TYPE);
370 if (!is_api_function) {
372 MemOperand bit_field3 = FieldMemOperand(r2, Map::kBitField3Offset);
373 // Check if slack tracking is enabled.
374 __ ldr(r4, bit_field3);
375 __ DecodeField<Map::ConstructionCount>(r3, r4);
376 __ cmp(r3, Operand(JSFunction::kNoSlackTracking));
378 // Decrease generous allocation count.
379 __ sub(r4, r4, Operand(1 << Map::ConstructionCount::kShift));
380 __ str(r4, bit_field3);
381 __ cmp(r3, Operand(JSFunction::kFinishSlackTracking));
386 __ Push(r2, r1); // r1 = constructor
387 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
395 // Now allocate the JSObject on the heap.
396 // r1: constructor function
398 __ ldrb(r3, FieldMemOperand(r2, Map::kInstanceSizeOffset));
399 if (create_memento) {
400 __ add(r3, r3, Operand(AllocationMemento::kSize / kPointerSize));
403 __ Allocate(r3, r4, r5, r6, &rt_call, SIZE_IN_WORDS);
405 // Allocated the JSObject, now initialize the fields. Map is set to
406 // initial map and properties and elements are set to empty fixed array.
407 // r1: constructor function
409 // r3: object size (not including memento if create_memento)
410 // r4: JSObject (not tagged)
411 __ LoadRoot(r6, Heap::kEmptyFixedArrayRootIndex);
413 DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
414 __ str(r2, MemOperand(r5, kPointerSize, PostIndex));
415 DCHECK_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
416 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
417 DCHECK_EQ(2 * kPointerSize, JSObject::kElementsOffset);
418 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
420 // Fill all the in-object properties with the appropriate filler.
421 // r1: constructor function
423 // r3: object size (in words, including memento if create_memento)
424 // r4: JSObject (not tagged)
425 // r5: First in-object property of JSObject (not tagged)
426 DCHECK_EQ(3 * kPointerSize, JSObject::kHeaderSize);
427 __ LoadRoot(r6, Heap::kUndefinedValueRootIndex);
429 if (!is_api_function) {
430 Label no_inobject_slack_tracking;
432 // Check if slack tracking is enabled.
433 __ ldr(ip, FieldMemOperand(r2, Map::kBitField3Offset));
434 __ DecodeField<Map::ConstructionCount>(ip);
435 __ cmp(ip, Operand(JSFunction::kNoSlackTracking));
436 __ b(eq, &no_inobject_slack_tracking);
438 // Allocate object with a slack.
439 __ ldr(r0, FieldMemOperand(r2, Map::kInstanceSizesOffset));
440 __ Ubfx(r0, r0, Map::kPreAllocatedPropertyFieldsByte * kBitsPerByte,
442 __ add(r0, r5, Operand(r0, LSL, kPointerSizeLog2));
443 // r0: offset of first field after pre-allocated fields
444 if (FLAG_debug_code) {
445 __ add(ip, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
447 __ Assert(le, kUnexpectedNumberOfPreAllocatedPropertyFields);
449 __ InitializeFieldsWithFiller(r5, r0, r6);
450 // To allow for truncation.
451 __ LoadRoot(r6, Heap::kOnePointerFillerMapRootIndex);
452 // Fill the remaining fields with one pointer filler map.
454 __ bind(&no_inobject_slack_tracking);
457 if (create_memento) {
458 __ sub(ip, r3, Operand(AllocationMemento::kSize / kPointerSize));
459 __ add(r0, r4, Operand(ip, LSL, kPointerSizeLog2)); // End of object.
460 __ InitializeFieldsWithFiller(r5, r0, r6);
462 // Fill in memento fields.
463 // r5: points to the allocated but uninitialized memento.
464 __ LoadRoot(r6, Heap::kAllocationMementoMapRootIndex);
465 DCHECK_EQ(0 * kPointerSize, AllocationMemento::kMapOffset);
466 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
467 // Load the AllocationSite
468 __ ldr(r6, MemOperand(sp, 2 * kPointerSize));
469 DCHECK_EQ(1 * kPointerSize, AllocationMemento::kAllocationSiteOffset);
470 __ str(r6, MemOperand(r5, kPointerSize, PostIndex));
472 __ add(r0, r4, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
473 __ InitializeFieldsWithFiller(r5, r0, r6);
476 // Add the object tag to make the JSObject real, so that we can continue
477 // and jump into the continuation code at any time from now on. Any
478 // failures need to undo the allocation, so that the heap is in a
479 // consistent state and verifiable.
480 __ add(r4, r4, Operand(kHeapObjectTag));
482 // Check if a non-empty properties array is needed. Continue with
483 // allocated object if not fall through to runtime call if it is.
484 // r1: constructor function
486 // r5: start of next object (not tagged)
487 __ ldrb(r3, FieldMemOperand(r2, Map::kUnusedPropertyFieldsOffset));
488 // The field instance sizes contains both pre-allocated property fields
489 // and in-object properties.
490 __ ldr(r0, FieldMemOperand(r2, Map::kInstanceSizesOffset));
491 __ Ubfx(r6, r0, Map::kPreAllocatedPropertyFieldsByte * kBitsPerByte,
493 __ add(r3, r3, Operand(r6));
494 __ Ubfx(r6, r0, Map::kInObjectPropertiesByte * kBitsPerByte,
496 __ sub(r3, r3, Operand(r6), SetCC);
498 // Done if no extra properties are to be allocated.
499 __ b(eq, &allocated);
500 __ Assert(pl, kPropertyAllocationCountFailed);
502 // Scale the number of elements by pointer size and add the header for
503 // FixedArrays to the start of the next object calculation from above.
505 // r3: number of elements in properties array
507 // r5: start of next object
508 __ add(r0, r3, Operand(FixedArray::kHeaderSize / kPointerSize));
515 static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
517 // Initialize the FixedArray.
519 // r3: number of elements in properties array
521 // r5: FixedArray (not tagged)
522 __ LoadRoot(r6, Heap::kFixedArrayMapRootIndex);
524 DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
525 __ str(r6, MemOperand(r2, kPointerSize, PostIndex));
526 DCHECK_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
528 __ str(r0, MemOperand(r2, kPointerSize, PostIndex));
530 // Initialize the fields to undefined.
531 // r1: constructor function
532 // r2: First element of FixedArray (not tagged)
533 // r3: number of elements in properties array
535 // r5: FixedArray (not tagged)
536 __ add(r6, r2, Operand(r3, LSL, kPointerSizeLog2)); // End of object.
537 DCHECK_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
539 __ LoadRoot(r0, Heap::kUndefinedValueRootIndex);
542 __ str(r0, MemOperand(r2, kPointerSize, PostIndex));
548 // Store the initialized FixedArray into the properties field of
550 // r1: constructor function
552 // r5: FixedArray (not tagged)
553 __ add(r5, r5, Operand(kHeapObjectTag)); // Add the heap tag.
554 __ str(r5, FieldMemOperand(r4, JSObject::kPropertiesOffset));
556 // Continue with JSObject being successfully allocated
557 // r1: constructor function
561 // Undo the setting of the new top so that the heap is verifiable. For
562 // example, the map's unused properties potentially do not match the
563 // allocated objects unused properties.
564 // r4: JSObject (previous new top)
565 __ bind(&undo_allocation);
566 __ UndoAllocationInNewSpace(r4, r5);
569 // Allocate the new receiver object using the runtime call.
570 // r1: constructor function
572 if (create_memento) {
573 // Get the cell or allocation site.
574 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
578 __ push(r1); // argument for Runtime_NewObject
579 if (create_memento) {
580 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 2);
582 __ CallRuntime(Runtime::kNewObject, 1);
586 // If we ended up using the runtime, and we want a memento, then the
587 // runtime call made it for us, and we shouldn't do create count
589 Label count_incremented;
590 if (create_memento) {
591 __ jmp(&count_incremented);
594 // Receiver for constructor call allocated.
598 if (create_memento) {
599 __ ldr(r2, MemOperand(sp, kPointerSize * 2));
600 __ LoadRoot(r5, Heap::kUndefinedValueRootIndex);
602 __ b(eq, &count_incremented);
603 // r2 is an AllocationSite. We are creating a memento from it, so we
604 // need to increment the memento create count.
605 __ ldr(r3, FieldMemOperand(r2,
606 AllocationSite::kPretenureCreateCountOffset));
607 __ add(r3, r3, Operand(Smi::FromInt(1)));
608 __ str(r3, FieldMemOperand(r2,
609 AllocationSite::kPretenureCreateCountOffset));
610 __ bind(&count_incremented);
616 // Reload the number of arguments and the constructor from the stack.
619 // sp[2]: constructor function
620 // sp[3]: number of arguments (smi-tagged)
621 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
622 __ ldr(r3, MemOperand(sp, 3 * kPointerSize));
624 // Set up pointer to last argument.
625 __ add(r2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
627 // Set up number of arguments for function call below
630 // Copy arguments and receiver to the expression stack.
631 // r0: number of arguments
632 // r1: constructor function
633 // r2: address of last argument (caller sp)
634 // r3: number of arguments (smi-tagged)
637 // sp[2]: constructor function
638 // sp[3]: number of arguments (smi-tagged)
642 __ ldr(ip, MemOperand(r2, r3, LSL, kPointerSizeLog2 - 1));
645 __ sub(r3, r3, Operand(2), SetCC);
648 // Call the function.
649 // r0: number of arguments
650 // r1: constructor function
651 if (is_api_function) {
652 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
654 masm->isolate()->builtins()->HandleApiCallConstruct();
655 __ Call(code, RelocInfo::CODE_TARGET);
657 ParameterCount actual(r0);
658 __ InvokeFunction(r1, actual, CALL_FUNCTION, NullCallWrapper());
661 // Store offset of return address for deoptimizer.
662 if (!is_api_function) {
663 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
666 // Restore context from the frame.
669 // sp[1]: constructor function
670 // sp[2]: number of arguments (smi-tagged)
671 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
673 // If the result is an object (in the ECMA sense), we should get rid
674 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
676 Label use_receiver, exit;
678 // If the result is a smi, it is *not* an object in the ECMA sense.
680 // sp[0]: receiver (newly allocated object)
681 // sp[1]: constructor function
682 // sp[2]: number of arguments (smi-tagged)
683 __ JumpIfSmi(r0, &use_receiver);
685 // If the type of the result (stored in its map) is less than
686 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
687 __ CompareObjectType(r0, r1, r3, FIRST_SPEC_OBJECT_TYPE);
690 // Throw away the result of the constructor invocation and use the
691 // on-stack receiver as the result.
692 __ bind(&use_receiver);
693 __ ldr(r0, MemOperand(sp));
695 // Remove receiver from the stack, remove caller arguments, and
699 // sp[0]: receiver (newly allocated object)
700 // sp[1]: constructor function
701 // sp[2]: number of arguments (smi-tagged)
702 __ ldr(r1, MemOperand(sp, 2 * kPointerSize));
704 // Leave construct frame.
707 __ add(sp, sp, Operand(r1, LSL, kPointerSizeLog2 - 1));
708 __ add(sp, sp, Operand(kPointerSize));
709 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, r1, r2);
714 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
715 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
719 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
720 Generate_JSConstructStubHelper(masm, true, false);
724 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
726 // Called from Generate_JS_Entry
732 // r5-r6, r8 (if not FLAG_enable_ool_constant_pool) and cp may be clobbered
733 ProfileEntryHookStub::MaybeCallEntryHook(masm);
735 // Clear the context before we push it when entering the internal frame.
736 __ mov(cp, Operand::Zero());
738 // Enter an internal frame.
740 FrameScope scope(masm, StackFrame::INTERNAL);
742 // Set up the context from the function argument.
743 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
745 __ InitializeRootRegister();
747 // Push the function and the receiver onto the stack.
751 // Copy arguments to the stack in a loop.
754 // r4: argv, i.e. points to first arg
756 __ add(r2, r4, Operand(r3, LSL, kPointerSizeLog2));
757 // r2 points past last arg.
760 __ ldr(r0, MemOperand(r4, kPointerSize, PostIndex)); // read next parameter
761 __ ldr(r0, MemOperand(r0)); // dereference handle
762 __ push(r0); // push parameter
767 // Initialize all JavaScript callee-saved registers, since they will be seen
768 // by the garbage collector as part of handlers.
769 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
770 __ mov(r5, Operand(r4));
771 __ mov(r6, Operand(r4));
772 if (!FLAG_enable_ool_constant_pool) {
773 __ mov(r8, Operand(r4));
775 if (kR9Available == 1) {
776 __ mov(r9, Operand(r4));
779 // Invoke the code and pass argc as r0.
780 __ mov(r0, Operand(r3));
782 // No type feedback cell is available
783 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
784 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
787 ParameterCount actual(r0);
788 __ InvokeFunction(r1, actual, CALL_FUNCTION, NullCallWrapper());
790 // Exit the JS frame and remove the parameters (except function), and
792 // Respect ABI stack constraint.
800 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
801 Generate_JSEntryTrampolineHelper(masm, false);
805 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
806 Generate_JSEntryTrampolineHelper(masm, true);
810 void Builtins::Generate_CompileUnoptimized(MacroAssembler* masm) {
811 CallRuntimePassFunction(masm, Runtime::kCompileUnoptimized);
812 GenerateTailCallToReturnedCode(masm);
816 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
817 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
818 // Push a copy of the function onto the stack.
820 // Push function as parameter to the runtime call.
822 // Whether to compile in a background thread.
823 __ Push(masm->isolate()->factory()->ToBoolean(concurrent));
825 __ CallRuntime(Runtime::kCompileOptimized, 2);
831 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
832 CallCompileOptimized(masm, false);
833 GenerateTailCallToReturnedCode(masm);
837 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
838 CallCompileOptimized(masm, true);
839 GenerateTailCallToReturnedCode(masm);
843 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
844 // For now, we are relying on the fact that make_code_young doesn't do any
845 // garbage collection which allows us to save/restore the registers without
846 // worrying about which of them contain pointers. We also don't build an
847 // internal frame to make the code faster, since we shouldn't have to do stack
848 // crawls in MakeCodeYoung. This seems a bit fragile.
850 // The following registers must be saved and restored when calling through to
852 // r0 - contains return address (beginning of patch sequence)
854 FrameScope scope(masm, StackFrame::MANUAL);
855 __ stm(db_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
856 __ PrepareCallCFunction(2, 0, r2);
857 __ mov(r1, Operand(ExternalReference::isolate_address(masm->isolate())));
859 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
860 __ ldm(ia_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
864 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
865 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
866 MacroAssembler* masm) { \
867 GenerateMakeCodeYoungAgainCommon(masm); \
869 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
870 MacroAssembler* masm) { \
871 GenerateMakeCodeYoungAgainCommon(masm); \
873 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
874 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
877 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
878 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
879 // that make_code_young doesn't do any garbage collection which allows us to
880 // save/restore the registers without worrying about which of them contain
883 // The following registers must be saved and restored when calling through to
885 // r0 - contains return address (beginning of patch sequence)
887 FrameScope scope(masm, StackFrame::MANUAL);
888 __ stm(db_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
889 __ PrepareCallCFunction(2, 0, r2);
890 __ mov(r1, Operand(ExternalReference::isolate_address(masm->isolate())));
891 __ CallCFunction(ExternalReference::get_mark_code_as_executed_function(
892 masm->isolate()), 2);
893 __ ldm(ia_w, sp, r0.bit() | r1.bit() | fp.bit() | lr.bit());
895 // Perform prologue operations usually performed by the young code stub.
896 __ PushFixedFrame(r1);
897 __ add(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
899 // Jump to point after the code-age stub.
900 __ add(r0, r0, Operand(kNoCodeAgeSequenceLength));
905 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
906 GenerateMakeCodeYoungAgainCommon(masm);
910 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
911 SaveFPRegsMode save_doubles) {
913 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
915 // Preserve registers across notification, this is important for compiled
916 // stubs that tail call the runtime on deopts passing their parameters in
918 __ stm(db_w, sp, kJSCallerSaved | kCalleeSaved);
919 // Pass the function and deoptimization type to the runtime system.
920 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
921 __ ldm(ia_w, sp, kJSCallerSaved | kCalleeSaved);
924 __ add(sp, sp, Operand(kPointerSize)); // Ignore state
925 __ mov(pc, lr); // Jump to miss handler
929 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
930 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
934 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
935 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
939 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
940 Deoptimizer::BailoutType type) {
942 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
943 // Pass the function and deoptimization type to the runtime system.
944 __ mov(r0, Operand(Smi::FromInt(static_cast<int>(type))));
946 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
949 // Get the full codegen state from the stack and untag it -> r6.
950 __ ldr(r6, MemOperand(sp, 0 * kPointerSize));
952 // Switch on the state.
953 Label with_tos_register, unknown_state;
954 __ cmp(r6, Operand(FullCodeGenerator::NO_REGISTERS));
955 __ b(ne, &with_tos_register);
956 __ add(sp, sp, Operand(1 * kPointerSize)); // Remove state.
959 __ bind(&with_tos_register);
960 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
961 __ cmp(r6, Operand(FullCodeGenerator::TOS_REG));
962 __ b(ne, &unknown_state);
963 __ add(sp, sp, Operand(2 * kPointerSize)); // Remove state.
966 __ bind(&unknown_state);
967 __ stop("no cases left");
971 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
972 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
976 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
977 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
981 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
982 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
986 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
987 // Lookup the function in the JavaScript frame.
988 __ ldr(r0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
990 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
991 // Pass function as argument.
993 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
996 // If the code object is null, just return to the unoptimized code.
998 __ cmp(r0, Operand(Smi::FromInt(0)));
1004 // Load deoptimization data from the code object.
1005 // <deopt_data> = <code>[#deoptimization_data_offset]
1006 __ ldr(r1, FieldMemOperand(r0, Code::kDeoptimizationDataOffset));
1008 { ConstantPoolUnavailableScope constant_pool_unavailable(masm);
1009 if (FLAG_enable_ool_constant_pool) {
1010 __ ldr(pp, FieldMemOperand(r0, Code::kConstantPoolOffset));
1013 // Load the OSR entrypoint offset from the deoptimization data.
1014 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1015 __ ldr(r1, FieldMemOperand(r1, FixedArray::OffsetOfElementAt(
1016 DeoptimizationInputData::kOsrPcOffsetIndex)));
1018 // Compute the target address = code_obj + header_size + osr_offset
1019 // <entry_addr> = <code_obj> + #header_size + <osr_offset>
1020 __ add(r0, r0, Operand::SmiUntag(r1));
1021 __ add(lr, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
1023 // And "return" to the OSR entry point of the function.
1029 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1030 // We check the stack limit as indicator that recompilation might be done.
1032 __ LoadRoot(ip, Heap::kStackLimitRootIndex);
1033 __ cmp(sp, Operand(ip));
1036 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1037 __ CallRuntime(Runtime::kStackGuard, 0);
1039 __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
1040 RelocInfo::CODE_TARGET);
1047 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1048 // 1. Make sure we have at least one argument.
1049 // r0: actual number of arguments
1051 __ cmp(r0, Operand::Zero());
1053 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1055 __ add(r0, r0, Operand(1));
1059 // 2. Get the function to call (passed as receiver) from the stack, check
1060 // if it is a function.
1061 // r0: actual number of arguments
1062 Label slow, non_function;
1063 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1064 __ JumpIfSmi(r1, &non_function);
1065 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1068 // 3a. Patch the first argument if necessary when calling a function.
1069 // r0: actual number of arguments
1071 Label shift_arguments;
1072 __ mov(r4, Operand::Zero()); // indicate regular JS_FUNCTION
1073 { Label convert_to_object, use_global_proxy, patch_receiver;
1074 // Change context eagerly in case we need the global receiver.
1075 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1077 // Do not transform the receiver for strict mode functions.
1078 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1079 __ ldr(r3, FieldMemOperand(r2, SharedFunctionInfo::kCompilerHintsOffset));
1080 __ tst(r3, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1082 __ b(ne, &shift_arguments);
1084 // Do not transform the receiver for native (Compilerhints already in r3).
1085 __ tst(r3, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1086 __ b(ne, &shift_arguments);
1088 // Compute the receiver in sloppy mode.
1089 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1090 __ ldr(r2, MemOperand(r2, -kPointerSize));
1091 // r0: actual number of arguments
1093 // r2: first argument
1094 __ JumpIfSmi(r2, &convert_to_object);
1096 __ LoadRoot(r3, Heap::kUndefinedValueRootIndex);
1098 __ b(eq, &use_global_proxy);
1099 __ LoadRoot(r3, Heap::kNullValueRootIndex);
1101 __ b(eq, &use_global_proxy);
1103 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1104 __ CompareObjectType(r2, r3, r3, FIRST_SPEC_OBJECT_TYPE);
1105 __ b(ge, &shift_arguments);
1107 __ bind(&convert_to_object);
1110 // Enter an internal frame in order to preserve argument count.
1111 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1116 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1122 // Exit the internal frame.
1125 // Restore the function to r1, and the flag to r4.
1126 __ ldr(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
1127 __ mov(r4, Operand::Zero());
1128 __ jmp(&patch_receiver);
1130 __ bind(&use_global_proxy);
1131 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1132 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalProxyOffset));
1134 __ bind(&patch_receiver);
1135 __ add(r3, sp, Operand(r0, LSL, kPointerSizeLog2));
1136 __ str(r2, MemOperand(r3, -kPointerSize));
1138 __ jmp(&shift_arguments);
1141 // 3b. Check for function proxy.
1143 __ mov(r4, Operand(1, RelocInfo::NONE32)); // indicate function proxy
1144 __ cmp(r2, Operand(JS_FUNCTION_PROXY_TYPE));
1145 __ b(eq, &shift_arguments);
1146 __ bind(&non_function);
1147 __ mov(r4, Operand(2, RelocInfo::NONE32)); // indicate non-function
1149 // 3c. Patch the first argument when calling a non-function. The
1150 // CALL_NON_FUNCTION builtin expects the non-function callee as
1151 // receiver, so overwrite the first argument which will ultimately
1152 // become the receiver.
1153 // r0: actual number of arguments
1155 // r4: call type (0: JS function, 1: function proxy, 2: non-function)
1156 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1157 __ str(r1, MemOperand(r2, -kPointerSize));
1159 // 4. Shift arguments and return address one slot down on the stack
1160 // (overwriting the original receiver). Adjust argument count to make
1161 // the original first argument the new receiver.
1162 // r0: actual number of arguments
1164 // r4: call type (0: JS function, 1: function proxy, 2: non-function)
1165 __ bind(&shift_arguments);
1167 // Calculate the copy start address (destination). Copy end address is sp.
1168 __ add(r2, sp, Operand(r0, LSL, kPointerSizeLog2));
1171 __ ldr(ip, MemOperand(r2, -kPointerSize));
1172 __ str(ip, MemOperand(r2));
1173 __ sub(r2, r2, Operand(kPointerSize));
1176 // Adjust the actual number of arguments and remove the top element
1177 // (which is a copy of the last argument).
1178 __ sub(r0, r0, Operand(1));
1182 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1183 // or a function proxy via CALL_FUNCTION_PROXY.
1184 // r0: actual number of arguments
1186 // r4: call type (0: JS function, 1: function proxy, 2: non-function)
1187 { Label function, non_proxy;
1189 __ b(eq, &function);
1190 // Expected number of arguments is 0 for CALL_NON_FUNCTION.
1191 __ mov(r2, Operand::Zero());
1192 __ cmp(r4, Operand(1));
1193 __ b(ne, &non_proxy);
1195 __ push(r1); // re-add proxy object as additional argument
1196 __ add(r0, r0, Operand(1));
1197 __ GetBuiltinFunction(r1, Builtins::CALL_FUNCTION_PROXY);
1198 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1199 RelocInfo::CODE_TARGET);
1201 __ bind(&non_proxy);
1202 __ GetBuiltinFunction(r1, Builtins::CALL_NON_FUNCTION);
1203 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1204 RelocInfo::CODE_TARGET);
1208 // 5b. Get the code to call from the function and check that the number of
1209 // expected arguments matches what we're providing. If so, jump
1210 // (tail-call) to the code in register edx without checking arguments.
1211 // r0: actual number of arguments
1213 __ ldr(r3, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1215 FieldMemOperand(r3, SharedFunctionInfo::kFormalParameterCountOffset));
1217 __ cmp(r2, r0); // Check formal and actual parameter counts.
1218 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1219 RelocInfo::CODE_TARGET,
1222 __ ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
1223 ParameterCount expected(0);
1224 __ InvokeCode(r3, expected, expected, JUMP_FUNCTION, NullCallWrapper());
1228 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1229 const int kIndexOffset =
1230 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1231 const int kLimitOffset =
1232 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1233 const int kArgsOffset = 2 * kPointerSize;
1234 const int kRecvOffset = 3 * kPointerSize;
1235 const int kFunctionOffset = 4 * kPointerSize;
1238 FrameAndConstantPoolScope frame_scope(masm, StackFrame::INTERNAL);
1240 __ ldr(r0, MemOperand(fp, kFunctionOffset)); // get the function
1242 __ ldr(r0, MemOperand(fp, kArgsOffset)); // get the args array
1244 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1246 // Check the stack for overflow. We are not trying to catch
1247 // interruptions (e.g. debug break and preemption) here, so the "real stack
1248 // limit" is checked.
1250 __ LoadRoot(r2, Heap::kRealStackLimitRootIndex);
1251 // Make r2 the space we have left. The stack might already be overflowed
1252 // here which will cause r2 to become negative.
1254 // Check if the arguments will overflow the stack.
1255 __ cmp(r2, Operand::PointerOffsetFromSmiKey(r0));
1256 __ b(gt, &okay); // Signed comparison.
1258 // Out of stack space.
1259 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1261 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1262 // End of stack check.
1264 // Push current limit and index.
1266 __ push(r0); // limit
1267 __ mov(r1, Operand::Zero()); // initial index
1270 // Get the receiver.
1271 __ ldr(r0, MemOperand(fp, kRecvOffset));
1273 // Check that the function is a JS function (otherwise it must be a proxy).
1274 Label push_receiver;
1275 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1276 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1277 __ b(ne, &push_receiver);
1279 // Change context eagerly to get the right global object if necessary.
1280 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
1281 // Load the shared function info while the function is still in r1.
1282 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1284 // Compute the receiver.
1285 // Do not transform the receiver for strict mode functions.
1286 Label call_to_object, use_global_proxy;
1287 __ ldr(r2, FieldMemOperand(r2, SharedFunctionInfo::kCompilerHintsOffset));
1288 __ tst(r2, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1290 __ b(ne, &push_receiver);
1292 // Do not transform the receiver for strict mode functions.
1293 __ tst(r2, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1294 __ b(ne, &push_receiver);
1296 // Compute the receiver in sloppy mode.
1297 __ JumpIfSmi(r0, &call_to_object);
1298 __ LoadRoot(r1, Heap::kNullValueRootIndex);
1300 __ b(eq, &use_global_proxy);
1301 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
1303 __ b(eq, &use_global_proxy);
1305 // Check if the receiver is already a JavaScript object.
1307 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1308 __ CompareObjectType(r0, r1, r1, FIRST_SPEC_OBJECT_TYPE);
1309 __ b(ge, &push_receiver);
1311 // Convert the receiver to a regular object.
1313 __ bind(&call_to_object);
1315 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1316 __ b(&push_receiver);
1318 __ bind(&use_global_proxy);
1319 __ ldr(r0, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1320 __ ldr(r0, FieldMemOperand(r0, GlobalObject::kGlobalProxyOffset));
1322 // Push the receiver.
1324 __ bind(&push_receiver);
1327 // Copy all arguments from the array to the stack.
1329 __ ldr(r0, MemOperand(fp, kIndexOffset));
1332 // Load the current argument from the arguments array and push it to the
1334 // r0: current argument index
1336 __ ldr(r1, MemOperand(fp, kArgsOffset));
1339 // Call the runtime to access the property in the arguments array.
1340 __ CallRuntime(Runtime::kGetProperty, 2);
1343 // Use inline caching to access the arguments.
1344 __ ldr(r0, MemOperand(fp, kIndexOffset));
1345 __ add(r0, r0, Operand(1 << kSmiTagSize));
1346 __ str(r0, MemOperand(fp, kIndexOffset));
1348 // Test if the copy loop has finished copying all the elements from the
1349 // arguments object.
1351 __ ldr(r1, MemOperand(fp, kLimitOffset));
1355 // Call the function.
1357 ParameterCount actual(r0);
1359 __ ldr(r1, MemOperand(fp, kFunctionOffset));
1360 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
1361 __ b(ne, &call_proxy);
1362 __ InvokeFunction(r1, actual, CALL_FUNCTION, NullCallWrapper());
1364 frame_scope.GenerateLeaveFrame();
1365 __ add(sp, sp, Operand(3 * kPointerSize));
1368 // Call the function proxy.
1369 __ bind(&call_proxy);
1370 __ push(r1); // add function proxy as last argument
1371 __ add(r0, r0, Operand(1));
1372 __ mov(r2, Operand::Zero());
1373 __ GetBuiltinFunction(r1, Builtins::CALL_FUNCTION_PROXY);
1374 __ Call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1375 RelocInfo::CODE_TARGET);
1377 // Tear down the internal frame and remove function, receiver and args.
1379 __ add(sp, sp, Operand(3 * kPointerSize));
1384 static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1385 Label* stack_overflow) {
1386 // ----------- S t a t e -------------
1387 // -- r0 : actual number of arguments
1388 // -- r1 : function (passed through to callee)
1389 // -- r2 : expected number of arguments
1390 // -----------------------------------
1391 // Check the stack for overflow. We are not trying to catch
1392 // interruptions (e.g. debug break and preemption) here, so the "real stack
1393 // limit" is checked.
1394 __ LoadRoot(r5, Heap::kRealStackLimitRootIndex);
1395 // Make r5 the space we have left. The stack might already be overflowed
1396 // here which will cause r5 to become negative.
1398 // Check if the arguments will overflow the stack.
1399 __ cmp(r5, Operand(r2, LSL, kPointerSizeLog2));
1400 __ b(le, stack_overflow); // Signed comparison.
1404 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1406 __ mov(r4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1407 __ stm(db_w, sp, r0.bit() | r1.bit() | r4.bit() |
1408 (FLAG_enable_ool_constant_pool ? pp.bit() : 0) |
1409 fp.bit() | lr.bit());
1411 Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
1415 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1416 // ----------- S t a t e -------------
1417 // -- r0 : result being passed through
1418 // -----------------------------------
1419 // Get the number of arguments passed (as a smi), tear down the frame and
1420 // then tear down the parameters.
1421 __ ldr(r1, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1424 __ LeaveFrame(StackFrame::ARGUMENTS_ADAPTOR);
1425 __ add(sp, sp, Operand::PointerOffsetFromSmiKey(r1));
1426 __ add(sp, sp, Operand(kPointerSize)); // adjust for receiver
1430 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1431 // ----------- S t a t e -------------
1432 // -- r0 : actual number of arguments
1433 // -- r1 : function (passed through to callee)
1434 // -- r2 : expected number of arguments
1435 // -----------------------------------
1437 Label stack_overflow;
1438 ArgumentAdaptorStackCheck(masm, &stack_overflow);
1439 Label invoke, dont_adapt_arguments;
1441 Label enough, too_few;
1442 __ ldr(r3, FieldMemOperand(r1, JSFunction::kCodeEntryOffset));
1445 __ cmp(r2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1446 __ b(eq, &dont_adapt_arguments);
1448 { // Enough parameters: actual >= expected
1450 EnterArgumentsAdaptorFrame(masm);
1452 // Calculate copy start address into r0 and copy end address into r2.
1453 // r0: actual number of arguments as a smi
1455 // r2: expected number of arguments
1456 // r3: code entry to call
1457 __ add(r0, fp, Operand::PointerOffsetFromSmiKey(r0));
1458 // adjust for return address and receiver
1459 __ add(r0, r0, Operand(2 * kPointerSize));
1460 __ sub(r2, r0, Operand(r2, LSL, kPointerSizeLog2));
1462 // Copy the arguments (including the receiver) to the new stack frame.
1463 // r0: copy start address
1465 // r2: copy end address
1466 // r3: code entry to call
1470 __ ldr(ip, MemOperand(r0, 0));
1472 __ cmp(r0, r2); // Compare before moving to next argument.
1473 __ sub(r0, r0, Operand(kPointerSize));
1479 { // Too few parameters: Actual < expected
1481 EnterArgumentsAdaptorFrame(masm);
1483 // Calculate copy start address into r0 and copy end address is fp.
1484 // r0: actual number of arguments as a smi
1486 // r2: expected number of arguments
1487 // r3: code entry to call
1488 __ add(r0, fp, Operand::PointerOffsetFromSmiKey(r0));
1490 // Copy the arguments (including the receiver) to the new stack frame.
1491 // r0: copy start address
1493 // r2: expected number of arguments
1494 // r3: code entry to call
1497 // Adjust load for return address and receiver.
1498 __ ldr(ip, MemOperand(r0, 2 * kPointerSize));
1500 __ cmp(r0, fp); // Compare before moving to next argument.
1501 __ sub(r0, r0, Operand(kPointerSize));
1504 // Fill the remaining expected arguments with undefined.
1506 // r2: expected number of arguments
1507 // r3: code entry to call
1508 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1509 __ sub(r2, fp, Operand(r2, LSL, kPointerSizeLog2));
1510 // Adjust for frame.
1511 __ sub(r2, r2, Operand(StandardFrameConstants::kFixedFrameSizeFromFp +
1521 // Call the entry point.
1525 // Store offset of return address for deoptimizer.
1526 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1528 // Exit frame and return.
1529 LeaveArgumentsAdaptorFrame(masm);
1533 // -------------------------------------------
1534 // Dont adapt arguments.
1535 // -------------------------------------------
1536 __ bind(&dont_adapt_arguments);
1539 __ bind(&stack_overflow);
1541 FrameScope frame(masm, StackFrame::MANUAL);
1542 EnterArgumentsAdaptorFrame(masm);
1543 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1551 } } // namespace v8::internal
1553 #endif // V8_TARGET_ARCH_ARM