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 #if V8_TARGET_ARCH_MIPS
11 #include "src/codegen.h"
12 #include "src/debug.h"
13 #include "src/deoptimizer.h"
14 #include "src/full-codegen.h"
15 #include "src/runtime/runtime.h"
22 #define __ ACCESS_MASM(masm)
25 void Builtins::Generate_Adaptor(MacroAssembler* masm,
27 BuiltinExtraArguments extra_args) {
28 // ----------- S t a t e -------------
29 // -- a0 : number of arguments excluding receiver
30 // -- a1 : called function (only guaranteed when
31 // -- extra_args requires it)
33 // -- sp[0] : last argument
35 // -- sp[4 * (argc - 1)] : first argument
36 // -- sp[4 * agrc] : receiver
37 // -----------------------------------
39 // Insert extra arguments.
40 int num_extra_args = 0;
41 if (extra_args == NEEDS_CALLED_FUNCTION) {
45 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
48 // JumpToExternalReference expects a0 to contain the number of arguments
49 // including the receiver and the extra arguments.
50 __ Addu(a0, a0, num_extra_args + 1);
51 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
55 // Load the built-in InternalArray function from the current context.
56 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
58 // Load the native context.
61 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
63 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
64 // Load the InternalArray function from the native context.
68 Context::INTERNAL_ARRAY_FUNCTION_INDEX)));
72 // Load the built-in Array function from the current context.
73 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
74 // Load the native context.
77 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
79 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
80 // Load the Array function from the native context.
83 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
87 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
88 // ----------- S t a t e -------------
89 // -- a0 : number of arguments
90 // -- ra : return address
91 // -- sp[...]: constructor arguments
92 // -----------------------------------
93 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
95 // Get the InternalArray function.
96 GenerateLoadInternalArrayFunction(masm, a1);
98 if (FLAG_debug_code) {
99 // Initial map for the builtin InternalArray functions should be maps.
100 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
102 __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction,
103 t0, Operand(zero_reg));
104 __ GetObjectType(a2, a3, t0);
105 __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction,
106 t0, Operand(MAP_TYPE));
109 // Run the native code for the InternalArray function called as a normal
112 InternalArrayConstructorStub stub(masm->isolate());
113 __ TailCallStub(&stub);
117 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
118 // ----------- S t a t e -------------
119 // -- a0 : number of arguments
120 // -- ra : return address
121 // -- sp[...]: constructor arguments
122 // -----------------------------------
123 Label generic_array_code;
125 // Get the Array function.
126 GenerateLoadArrayFunction(masm, a1);
128 if (FLAG_debug_code) {
129 // Initial map for the builtin Array functions should be maps.
130 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
132 __ Assert(ne, kUnexpectedInitialMapForArrayFunction1,
133 t0, Operand(zero_reg));
134 __ GetObjectType(a2, a3, t0);
135 __ Assert(eq, kUnexpectedInitialMapForArrayFunction2,
136 t0, Operand(MAP_TYPE));
139 // Run the native code for the Array function called as a normal function.
142 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
143 ArrayConstructorStub stub(masm->isolate());
144 __ TailCallStub(&stub);
148 void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
149 // ----------- S t a t e -------------
150 // -- a0 : number of arguments
151 // -- a1 : constructor function
152 // -- ra : return address
153 // -- sp[(argc - n - 1) * 4] : arg[n] (zero based)
154 // -- sp[argc * 4] : receiver
155 // -----------------------------------
156 Counters* counters = masm->isolate()->counters();
157 __ IncrementCounter(counters->string_ctor_calls(), 1, a2, a3);
159 Register function = a1;
160 if (FLAG_debug_code) {
161 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, a2);
162 __ Assert(eq, kUnexpectedStringFunction, function, Operand(a2));
165 // Load the first arguments in a0 and get rid of the rest.
167 __ Branch(&no_arguments, eq, a0, Operand(zero_reg));
168 // First args = sp[(argc - 1) * 4].
169 __ Subu(a0, a0, Operand(1));
170 __ sll(a0, a0, kPointerSizeLog2);
172 __ lw(a0, MemOperand(sp));
173 // sp now point to args[0], drop args[0] + receiver.
176 Register argument = a2;
177 Label not_cached, argument_is_string;
178 __ LookupNumberStringCache(a0, // Input.
184 __ IncrementCounter(counters->string_ctor_cached_number(), 1, a3, t0);
185 __ bind(&argument_is_string);
187 // ----------- S t a t e -------------
188 // -- a2 : argument converted to string
189 // -- a1 : constructor function
190 // -- ra : return address
191 // -----------------------------------
194 __ Allocate(JSValue::kSize,
201 // Initialising the String Object.
203 __ LoadGlobalFunctionInitialMap(function, map, t0);
204 if (FLAG_debug_code) {
205 __ lbu(t0, FieldMemOperand(map, Map::kInstanceSizeOffset));
206 __ Assert(eq, kUnexpectedStringWrapperInstanceSize,
207 t0, Operand(JSValue::kSize >> kPointerSizeLog2));
208 __ lbu(t0, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
209 __ Assert(eq, kUnexpectedUnusedPropertiesOfStringWrapper,
210 t0, Operand(zero_reg));
212 __ sw(map, FieldMemOperand(v0, HeapObject::kMapOffset));
214 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
215 __ sw(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
216 __ sw(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
218 __ sw(argument, FieldMemOperand(v0, JSValue::kValueOffset));
220 // Ensure the object is fully initialized.
221 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
225 // The argument was not found in the number to string cache. Check
226 // if it's a string already before calling the conversion builtin.
227 Label convert_argument;
228 __ bind(¬_cached);
229 __ JumpIfSmi(a0, &convert_argument);
232 __ lw(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
233 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
234 STATIC_ASSERT(kNotStringTag != 0);
235 __ And(t0, a3, Operand(kIsNotStringMask));
236 __ Branch(&convert_argument, ne, t0, Operand(zero_reg));
237 __ mov(argument, a0);
238 __ IncrementCounter(counters->string_ctor_conversions(), 1, a3, t0);
239 __ Branch(&argument_is_string);
241 // Invoke the conversion builtin and put the result into a2.
242 __ bind(&convert_argument);
243 __ push(function); // Preserve the function.
244 __ IncrementCounter(counters->string_ctor_conversions(), 1, a3, t0);
246 FrameScope scope(masm, StackFrame::INTERNAL);
248 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
251 __ mov(argument, v0);
252 __ Branch(&argument_is_string);
254 // Load the empty string into a2, remove the receiver from the
255 // stack, and jump back to the case where the argument is a string.
256 __ bind(&no_arguments);
257 __ LoadRoot(argument, Heap::kempty_stringRootIndex);
259 __ Branch(&argument_is_string);
261 // At this point the argument is already a string. Call runtime to
262 // create a string wrapper.
263 __ bind(&gc_required);
264 __ IncrementCounter(counters->string_ctor_gc_required(), 1, a3, t0);
266 FrameScope scope(masm, StackFrame::INTERNAL);
268 __ CallRuntime(Runtime::kNewStringWrapper, 1);
274 static void CallRuntimePassFunction(
275 MacroAssembler* masm, Runtime::FunctionId function_id) {
276 FrameScope scope(masm, StackFrame::INTERNAL);
277 // Push a copy of the function onto the stack.
278 // Push call kind information and function as parameter to the runtime call.
281 __ CallRuntime(function_id, 1);
282 // Restore call kind information and receiver.
287 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
288 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
289 __ lw(a2, FieldMemOperand(a2, SharedFunctionInfo::kCodeOffset));
290 __ Addu(at, a2, Operand(Code::kHeaderSize - kHeapObjectTag));
295 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
296 __ Addu(at, v0, 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(t0, Heap::kStackLimitRootIndex);
309 __ Branch(&ok, hs, sp, Operand(t0));
311 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
312 GenerateTailCallToReturnedCode(masm);
315 GenerateTailCallToSharedCode(masm);
319 static void Generate_Runtime_NewObject(MacroAssembler* masm,
321 Register original_constructor,
322 Label* count_incremented,
324 if (create_memento) {
325 // Get the cell or allocation site.
326 __ lw(a2, MemOperand(sp, 2 * kPointerSize));
330 __ push(a1); // argument for Runtime_NewObject
331 __ push(original_constructor); // original constructor
332 if (create_memento) {
333 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 3);
335 __ CallRuntime(Runtime::kNewObject, 2);
339 // Runtime_NewObjectWithAllocationSite increments allocation count.
340 // Skip the increment.
341 if (create_memento) {
342 __ jmp(count_incremented);
349 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
350 bool is_api_function,
351 bool create_memento) {
352 // ----------- S t a t e -------------
353 // -- a0 : number of arguments
354 // -- a1 : constructor function
355 // -- a2 : allocation site or undefined
356 // -- a3 : original constructor
357 // -- ra : return address
358 // -- sp[...]: constructor arguments
359 // -----------------------------------
361 // Should never create mementos for api functions.
362 DCHECK(!is_api_function || !create_memento);
364 Isolate* isolate = masm->isolate();
366 // ----------- S t a t e -------------
367 // -- a0 : number of arguments
368 // -- a1 : constructor function
369 // -- ra : return address
370 // -- sp[...]: constructor arguments
371 // -----------------------------------
373 // Enter a construct frame.
375 FrameScope scope(masm, StackFrame::CONSTRUCT);
377 if (create_memento) {
378 __ AssertUndefinedOrAllocationSite(a2, t0);
382 // Preserve the two incoming parameters on the stack.
383 __ sll(a0, a0, kSmiTagSize); // Tag arguments count.
384 __ MultiPushReversed(a0.bit() | a1.bit());
386 Label rt_call, allocated, normal_new, count_incremented;
387 __ Branch(&normal_new, eq, a1, Operand(a3));
389 // Original constructor and function are different.
390 Generate_Runtime_NewObject(masm, create_memento, a3, &count_incremented,
392 __ bind(&normal_new);
394 // Try to allocate the object without transitioning into C code. If any of
395 // the preconditions is not met, the code bails out to the runtime call.
396 if (FLAG_inline_new) {
397 Label undo_allocation;
398 ExternalReference debug_step_in_fp =
399 ExternalReference::debug_step_in_fp_address(isolate);
400 __ li(a2, Operand(debug_step_in_fp));
401 __ lw(a2, MemOperand(a2));
402 __ Branch(&rt_call, ne, a2, Operand(zero_reg));
404 // Load the initial map and verify that it is in fact a map.
405 // a1: constructor function
406 __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
407 __ JumpIfSmi(a2, &rt_call);
408 __ GetObjectType(a2, a3, t4);
409 __ Branch(&rt_call, ne, t4, Operand(MAP_TYPE));
411 // Check that the constructor is not constructing a JSFunction (see
412 // comments in Runtime_NewObject in runtime.cc). In which case the
413 // initial map's instance type would be JS_FUNCTION_TYPE.
414 // a1: constructor function
416 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
417 __ Branch(&rt_call, eq, a3, Operand(JS_FUNCTION_TYPE));
419 if (!is_api_function) {
421 MemOperand bit_field3 = FieldMemOperand(a2, Map::kBitField3Offset);
422 // Check if slack tracking is enabled.
423 __ lw(t0, bit_field3);
424 __ DecodeField<Map::Counter>(t2, t0);
425 __ Branch(&allocate, lt, t2, Operand(Map::kSlackTrackingCounterEnd));
426 // Decrease generous allocation count.
427 __ Subu(t0, t0, Operand(1 << Map::Counter::kShift));
428 __ Branch(USE_DELAY_SLOT, &allocate, ne, t2,
429 Operand(Map::kSlackTrackingCounterEnd));
430 __ sw(t0, bit_field3); // In delay slot.
432 __ Push(a1, a2, a1); // a1 = Constructor.
433 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
436 // Slack tracking counter is Map::kSlackTrackingCounterEnd after runtime
438 __ li(t2, Map::kSlackTrackingCounterEnd);
443 // Now allocate the JSObject on the heap.
444 // a1: constructor function
446 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceSizeOffset));
447 if (create_memento) {
448 __ Addu(a3, a3, Operand(AllocationMemento::kSize / kPointerSize));
451 __ Allocate(a3, t4, t5, t6, &rt_call, SIZE_IN_WORDS);
453 // Allocated the JSObject, now initialize the fields. Map is set to
454 // initial map and properties and elements are set to empty fixed array.
455 // a1: constructor function
457 // a3: object size (not including memento if create_memento)
458 // t4: JSObject (not tagged)
459 __ LoadRoot(t6, Heap::kEmptyFixedArrayRootIndex);
461 __ sw(a2, MemOperand(t5, JSObject::kMapOffset));
462 __ sw(t6, MemOperand(t5, JSObject::kPropertiesOffset));
463 __ sw(t6, MemOperand(t5, JSObject::kElementsOffset));
464 __ Addu(t5, t5, Operand(3*kPointerSize));
465 DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
466 DCHECK_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
467 DCHECK_EQ(2 * kPointerSize, JSObject::kElementsOffset);
469 // Fill all the in-object properties with appropriate filler.
470 // a1: constructor function
472 // a3: object size (in words, including memento if create_memento)
473 // t4: JSObject (not tagged)
474 // t5: First in-object property of JSObject (not tagged)
475 // t2: slack tracking counter (non-API function case)
476 DCHECK_EQ(3 * kPointerSize, JSObject::kHeaderSize);
478 // Use t7 to hold undefined, which is used in several places below.
479 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
481 if (!is_api_function) {
482 Label no_inobject_slack_tracking;
484 // Check if slack tracking is enabled.
485 __ Branch(&no_inobject_slack_tracking, lt, t2,
486 Operand(Map::kSlackTrackingCounterEnd));
488 // Allocate object with a slack.
489 __ lbu(a0, FieldMemOperand(a2, Map::kPreAllocatedPropertyFieldsOffset));
490 __ sll(at, a0, kPointerSizeLog2);
492 // a0: offset of first field after pre-allocated fields
493 if (FLAG_debug_code) {
494 __ sll(at, a3, kPointerSizeLog2);
495 __ Addu(t6, t4, Operand(at)); // End of object.
496 __ Assert(le, kUnexpectedNumberOfPreAllocatedPropertyFields,
499 __ InitializeFieldsWithFiller(t5, a0, t7);
500 // To allow for truncation.
501 __ LoadRoot(t7, Heap::kOnePointerFillerMapRootIndex);
502 // Fill the remaining fields with one pointer filler map.
504 __ bind(&no_inobject_slack_tracking);
507 if (create_memento) {
508 __ Subu(a0, a3, Operand(AllocationMemento::kSize / kPointerSize));
509 __ sll(a0, a0, kPointerSizeLog2);
510 __ Addu(a0, t4, Operand(a0)); // End of object.
511 __ InitializeFieldsWithFiller(t5, a0, t7);
513 // Fill in memento fields.
514 // t5: points to the allocated but uninitialized memento.
515 __ LoadRoot(t7, Heap::kAllocationMementoMapRootIndex);
516 DCHECK_EQ(0 * kPointerSize, AllocationMemento::kMapOffset);
517 __ sw(t7, MemOperand(t5));
518 __ Addu(t5, t5, kPointerSize);
519 // Load the AllocationSite.
520 __ lw(t7, MemOperand(sp, 2 * kPointerSize));
521 DCHECK_EQ(1 * kPointerSize, AllocationMemento::kAllocationSiteOffset);
522 __ sw(t7, MemOperand(t5));
523 __ Addu(t5, t5, kPointerSize);
525 __ sll(at, a3, kPointerSizeLog2);
526 __ Addu(a0, t4, Operand(at)); // End of object.
527 __ InitializeFieldsWithFiller(t5, a0, t7);
530 // Add the object tag to make the JSObject real, so that we can continue
531 // and jump into the continuation code at any time from now on. Any
532 // failures need to undo the allocation, so that the heap is in a
533 // consistent state and verifiable.
534 __ Addu(t4, t4, Operand(kHeapObjectTag));
536 // Check if a non-empty properties array is needed. Continue with
537 // allocated object if not fall through to runtime call if it is.
538 // a1: constructor function
540 // t5: start of next object (not tagged)
541 __ lbu(a3, FieldMemOperand(a2, Map::kUnusedPropertyFieldsOffset));
542 // The field instance sizes contains both pre-allocated property fields
543 // and in-object properties.
544 __ lbu(t6, FieldMemOperand(a2, Map::kPreAllocatedPropertyFieldsOffset));
545 __ Addu(a3, a3, Operand(t6));
546 __ lbu(t6, FieldMemOperand(a2, Map::kInObjectPropertiesOffset));
549 // Done if no extra properties are to be allocated.
550 __ Branch(&allocated, eq, a3, Operand(zero_reg));
551 __ Assert(greater_equal, kPropertyAllocationCountFailed,
552 a3, Operand(zero_reg));
554 // Scale the number of elements by pointer size and add the header for
555 // FixedArrays to the start of the next object calculation from above.
557 // a3: number of elements in properties array
559 // t5: start of next object
560 __ Addu(a0, a3, Operand(FixedArray::kHeaderSize / kPointerSize));
567 static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
569 // Initialize the FixedArray.
571 // a3: number of elements in properties array (untagged)
573 // t5: start of next object
574 __ LoadRoot(t6, Heap::kFixedArrayMapRootIndex);
576 __ sw(t6, MemOperand(a2, JSObject::kMapOffset));
577 __ sll(a0, a3, kSmiTagSize);
578 __ sw(a0, MemOperand(a2, FixedArray::kLengthOffset));
579 __ Addu(a2, a2, Operand(2 * kPointerSize));
581 DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
582 DCHECK_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
584 // Initialize the fields to undefined.
586 // a2: First element of FixedArray (not tagged)
587 // a3: number of elements in properties array
589 // t5: FixedArray (not tagged)
590 __ sll(t3, a3, kPointerSizeLog2);
591 __ addu(t6, a2, t3); // End of object.
592 DCHECK_EQ(2 * kPointerSize, FixedArray::kHeaderSize);
594 if (!is_api_function || create_memento) {
595 __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
596 } else if (FLAG_debug_code) {
597 __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
598 __ Assert(eq, kUndefinedValueNotLoaded, t7, Operand(t2));
602 __ sw(t7, MemOperand(a2));
603 __ addiu(a2, a2, kPointerSize);
605 __ Branch(&loop, less, a2, Operand(t6));
608 // Store the initialized FixedArray into the properties field of
610 // a1: constructor function
612 // t5: FixedArray (not tagged)
613 __ Addu(t5, t5, Operand(kHeapObjectTag)); // Add the heap tag.
614 __ sw(t5, FieldMemOperand(t4, JSObject::kPropertiesOffset));
616 // Continue with JSObject being successfully allocated.
617 // a1: constructor function
621 // Undo the setting of the new top so that the heap is verifiable. For
622 // example, the map's unused properties potentially do not match the
623 // allocated objects unused properties.
624 // t4: JSObject (previous new top)
625 __ bind(&undo_allocation);
626 __ UndoAllocationInNewSpace(t4, t5);
629 // Allocate the new receiver object using the runtime call.
630 // a1: constructor function
632 Generate_Runtime_NewObject(masm, create_memento, a1, &count_incremented,
635 // Receiver for constructor call allocated.
639 if (create_memento) {
640 __ lw(a2, MemOperand(sp, kPointerSize * 2));
641 __ LoadRoot(t5, Heap::kUndefinedValueRootIndex);
642 __ Branch(&count_incremented, eq, a2, Operand(t5));
643 // a2 is an AllocationSite. We are creating a memento from it, so we
644 // need to increment the memento create count.
645 __ lw(a3, FieldMemOperand(a2,
646 AllocationSite::kPretenureCreateCountOffset));
647 __ Addu(a3, a3, Operand(Smi::FromInt(1)));
648 __ sw(a3, FieldMemOperand(a2,
649 AllocationSite::kPretenureCreateCountOffset));
650 __ bind(&count_incremented);
655 // Reload the number of arguments from the stack.
658 // sp[2]: constructor function
659 // sp[3]: number of arguments (smi-tagged)
660 __ lw(a1, MemOperand(sp, 2 * kPointerSize));
661 __ lw(a3, MemOperand(sp, 3 * kPointerSize));
663 // Set up pointer to last argument.
664 __ Addu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
666 // Set up number of arguments for function call below.
667 __ srl(a0, a3, kSmiTagSize);
669 // Copy arguments and receiver to the expression stack.
670 // a0: number of arguments
671 // a1: constructor function
672 // a2: address of last argument (caller sp)
673 // a3: number of arguments (smi-tagged)
676 // sp[2]: constructor function
677 // sp[3]: number of arguments (smi-tagged)
681 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
682 __ Addu(t0, a2, Operand(t0));
683 __ lw(t1, MemOperand(t0));
686 __ Addu(a3, a3, Operand(-2));
687 __ Branch(&loop, greater_equal, a3, Operand(zero_reg));
689 // Call the function.
690 // a0: number of arguments
691 // a1: constructor function
692 if (is_api_function) {
693 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
695 masm->isolate()->builtins()->HandleApiCallConstruct();
696 __ Call(code, RelocInfo::CODE_TARGET);
698 ParameterCount actual(a0);
699 __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
702 // Store offset of return address for deoptimizer.
703 if (!is_api_function) {
704 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
707 // Restore context from the frame.
708 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
710 // If the result is an object (in the ECMA sense), we should get rid
711 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
713 Label use_receiver, exit;
715 // If the result is a smi, it is *not* an object in the ECMA sense.
717 // sp[0]: receiver (newly allocated object)
718 // sp[1]: constructor function
719 // sp[2]: number of arguments (smi-tagged)
720 __ JumpIfSmi(v0, &use_receiver);
722 // If the type of the result (stored in its map) is less than
723 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
724 __ GetObjectType(v0, a1, a3);
725 __ Branch(&exit, greater_equal, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
727 // Throw away the result of the constructor invocation and use the
728 // on-stack receiver as the result.
729 __ bind(&use_receiver);
730 __ lw(v0, MemOperand(sp));
732 // Remove receiver from the stack, remove caller arguments, and
736 // sp[0]: receiver (newly allocated object)
737 // sp[1]: constructor function
738 // sp[2]: number of arguments (smi-tagged)
739 __ lw(a1, MemOperand(sp, 2 * kPointerSize));
741 // Leave construct frame.
744 __ sll(t0, a1, kPointerSizeLog2 - 1);
746 __ Addu(sp, sp, kPointerSize);
747 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, a1, a2);
752 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
753 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
757 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
758 Generate_JSConstructStubHelper(masm, true, false);
762 void Builtins::Generate_JSConstructStubForDerived(MacroAssembler* masm) {
763 // ----------- S t a t e -------------
764 // -- a0 : number of arguments
765 // -- a1 : constructor function
766 // -- a2 : allocation site or undefined
767 // -- a3 : original constructor
768 // -- ra : return address
769 // -- sp[...]: constructor arguments
770 // -----------------------------------
772 // TODO(dslomov): support pretenuring
773 CHECK(!FLAG_pretenuring_call_new);
776 FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
780 __ push(t0); // Smi-tagged arguments count.
785 // receiver is the hole.
786 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
789 // Set up pointer to last argument.
790 __ Addu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
792 // Copy arguments and receiver to the expression stack.
793 // a0: number of arguments
794 // a1: constructor function
795 // a2: address of last argument (caller sp)
796 // t0: number of arguments (smi-tagged)
799 // sp[2]: number of arguments (smi-tagged)
803 __ sll(at, t0, kPointerSizeLog2 - 1);
804 __ Addu(at, a2, Operand(at));
805 __ lw(at, MemOperand(at));
808 __ Subu(t0, t0, Operand(2));
809 __ Branch(&loop, ge, t0, Operand(zero_reg));
811 __ Addu(a0, a0, Operand(1));
815 ExternalReference debug_step_in_fp =
816 ExternalReference::debug_step_in_fp_address(masm->isolate());
817 __ li(a2, Operand(debug_step_in_fp));
818 __ lw(a2, MemOperand(a2));
819 __ Branch(&skip_step_in, eq, a2, Operand(zero_reg));
822 __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
825 __ bind(&skip_step_in);
827 // Call the function.
828 // a0: number of arguments
829 // a1: constructor function
830 ParameterCount actual(a0);
831 __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
833 // Restore context from the frame.
835 // sp[0]: number of arguments (smi-tagged)
836 __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
837 __ lw(a1, MemOperand(sp, 0));
839 // Leave construct frame.
842 __ sll(at, a1, kPointerSizeLog2 - 1);
843 __ Addu(sp, sp, Operand(at));
844 __ Addu(sp, sp, Operand(kPointerSize));
849 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
851 // Called from JSEntryStub::GenerateBody
853 // ----------- S t a t e -------------
856 // -- a2: receiver_pointer
859 // -----------------------------------
860 ProfileEntryHookStub::MaybeCallEntryHook(masm);
862 // Clear the context before we push it when entering the JS frame.
863 __ mov(cp, zero_reg);
865 // Enter an internal frame.
867 FrameScope scope(masm, StackFrame::INTERNAL);
869 // Set up the context from the function argument.
870 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
872 // Push the function and the receiver onto the stack.
875 // Copy arguments to the stack in a loop.
877 // s0: argv, i.e. points to first arg
879 __ sll(t0, a3, kPointerSizeLog2);
882 __ nop(); // Branch delay slot nop.
883 // t2 points past last arg.
885 __ lw(t0, MemOperand(s0)); // Read next parameter.
886 __ addiu(s0, s0, kPointerSize);
887 __ lw(t0, MemOperand(t0)); // Dereference handle.
888 __ push(t0); // Push parameter.
890 __ Branch(&loop, ne, s0, Operand(t2));
892 // Initialize all JavaScript callee-saved registers, since they will be seen
893 // by the garbage collector as part of handlers.
894 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
900 // s6 holds the root address. Do not clobber.
901 // s7 is cp. Do not init.
903 // Invoke the code and pass argc as a0.
906 // No type feedback cell is available
907 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
908 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
911 ParameterCount actual(a0);
912 __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
915 // Leave internal frame.
922 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
923 Generate_JSEntryTrampolineHelper(masm, false);
927 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
928 Generate_JSEntryTrampolineHelper(masm, true);
932 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
933 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
934 GenerateTailCallToReturnedCode(masm);
938 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
939 FrameScope scope(masm, StackFrame::INTERNAL);
940 // Push a copy of the function onto the stack.
941 // Push function as parameter to the runtime call.
943 // Whether to compile in a background thread.
945 at, concurrent ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
948 __ CallRuntime(Runtime::kCompileOptimized, 2);
954 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
955 CallCompileOptimized(masm, false);
956 GenerateTailCallToReturnedCode(masm);
960 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
961 CallCompileOptimized(masm, true);
962 GenerateTailCallToReturnedCode(masm);
967 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
968 // For now, we are relying on the fact that make_code_young doesn't do any
969 // garbage collection which allows us to save/restore the registers without
970 // worrying about which of them contain pointers. We also don't build an
971 // internal frame to make the code faster, since we shouldn't have to do stack
972 // crawls in MakeCodeYoung. This seems a bit fragile.
974 // Set a0 to point to the head of the PlatformCodeAge sequence.
976 Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
978 // The following registers must be saved and restored when calling through to
980 // a0 - contains return address (beginning of patch sequence)
983 (a0.bit() | a1.bit() | ra.bit() | fp.bit()) & ~sp.bit();
984 FrameScope scope(masm, StackFrame::MANUAL);
985 __ MultiPush(saved_regs);
986 __ PrepareCallCFunction(2, 0, a2);
987 __ li(a1, Operand(ExternalReference::isolate_address(masm->isolate())));
989 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
990 __ MultiPop(saved_regs);
994 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
995 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
996 MacroAssembler* masm) { \
997 GenerateMakeCodeYoungAgainCommon(masm); \
999 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
1000 MacroAssembler* masm) { \
1001 GenerateMakeCodeYoungAgainCommon(masm); \
1003 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
1004 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
1007 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
1008 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
1009 // that make_code_young doesn't do any garbage collection which allows us to
1010 // save/restore the registers without worrying about which of them contain
1013 // Set a0 to point to the head of the PlatformCodeAge sequence.
1015 Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
1017 // The following registers must be saved and restored when calling through to
1019 // a0 - contains return address (beginning of patch sequence)
1021 RegList saved_regs =
1022 (a0.bit() | a1.bit() | ra.bit() | fp.bit()) & ~sp.bit();
1023 FrameScope scope(masm, StackFrame::MANUAL);
1024 __ MultiPush(saved_regs);
1025 __ PrepareCallCFunction(2, 0, a2);
1026 __ li(a1, Operand(ExternalReference::isolate_address(masm->isolate())));
1028 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
1030 __ MultiPop(saved_regs);
1032 // Perform prologue operations usually performed by the young code stub.
1033 __ Push(ra, fp, cp, a1);
1034 __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
1036 // Jump to point after the code-age stub.
1037 __ Addu(a0, a0, Operand(kNoCodeAgeSequenceLength));
1042 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
1043 GenerateMakeCodeYoungAgainCommon(masm);
1047 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
1048 SaveFPRegsMode save_doubles) {
1050 FrameScope scope(masm, StackFrame::INTERNAL);
1052 // Preserve registers across notification, this is important for compiled
1053 // stubs that tail call the runtime on deopts passing their parameters in
1055 __ MultiPush(kJSCallerSaved | kCalleeSaved);
1056 // Pass the function and deoptimization type to the runtime system.
1057 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
1058 __ MultiPop(kJSCallerSaved | kCalleeSaved);
1061 __ Addu(sp, sp, Operand(kPointerSize)); // Ignore state
1062 __ Jump(ra); // Jump to miss handler
1066 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
1067 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
1071 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
1072 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
1076 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1077 Deoptimizer::BailoutType type) {
1079 FrameScope scope(masm, StackFrame::INTERNAL);
1080 // Pass the function and deoptimization type to the runtime system.
1081 __ li(a0, Operand(Smi::FromInt(static_cast<int>(type))));
1083 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
1086 // Get the full codegen state from the stack and untag it -> t2.
1087 __ lw(t2, MemOperand(sp, 0 * kPointerSize));
1089 // Switch on the state.
1090 Label with_tos_register, unknown_state;
1091 __ Branch(&with_tos_register,
1092 ne, t2, Operand(FullCodeGenerator::NO_REGISTERS));
1093 __ Ret(USE_DELAY_SLOT);
1094 // Safe to fill delay slot Addu will emit one instruction.
1095 __ Addu(sp, sp, Operand(1 * kPointerSize)); // Remove state.
1097 __ bind(&with_tos_register);
1098 __ lw(v0, MemOperand(sp, 1 * kPointerSize));
1099 __ Branch(&unknown_state, ne, t2, Operand(FullCodeGenerator::TOS_REG));
1101 __ Ret(USE_DELAY_SLOT);
1102 // Safe to fill delay slot Addu will emit one instruction.
1103 __ Addu(sp, sp, Operand(2 * kPointerSize)); // Remove state.
1105 __ bind(&unknown_state);
1106 __ stop("no cases left");
1110 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1111 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1115 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
1116 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1120 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1121 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1125 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1126 // Lookup the function in the JavaScript frame.
1127 __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1129 FrameScope scope(masm, StackFrame::INTERNAL);
1130 // Pass function as argument.
1132 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1135 // If the code object is null, just return to the unoptimized code.
1136 __ Ret(eq, v0, Operand(Smi::FromInt(0)));
1138 // Load deoptimization data from the code object.
1139 // <deopt_data> = <code>[#deoptimization_data_offset]
1140 __ lw(a1, MemOperand(v0, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1142 // Load the OSR entrypoint offset from the deoptimization data.
1143 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1144 __ lw(a1, MemOperand(a1, FixedArray::OffsetOfElementAt(
1145 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1148 // Compute the target address = code_obj + header_size + osr_offset
1149 // <entry_addr> = <code_obj> + #header_size + <osr_offset>
1150 __ addu(v0, v0, a1);
1151 __ addiu(ra, v0, Code::kHeaderSize - kHeapObjectTag);
1153 // And "return" to the OSR entry point of the function.
1158 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1159 // We check the stack limit as indicator that recompilation might be done.
1161 __ LoadRoot(at, Heap::kStackLimitRootIndex);
1162 __ Branch(&ok, hs, sp, Operand(at));
1164 FrameScope scope(masm, StackFrame::INTERNAL);
1165 __ CallRuntime(Runtime::kStackGuard, 0);
1167 __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
1168 RelocInfo::CODE_TARGET);
1175 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1176 // 1. Make sure we have at least one argument.
1177 // a0: actual number of arguments
1179 __ Branch(&done, ne, a0, Operand(zero_reg));
1180 __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
1182 __ Addu(a0, a0, Operand(1));
1186 // 2. Get the function to call (passed as receiver) from the stack, check
1187 // if it is a function.
1188 // a0: actual number of arguments
1189 Label slow, non_function;
1190 __ sll(at, a0, kPointerSizeLog2);
1191 __ addu(at, sp, at);
1192 __ lw(a1, MemOperand(at));
1193 __ JumpIfSmi(a1, &non_function);
1194 __ GetObjectType(a1, a2, a2);
1195 __ Branch(&slow, ne, a2, Operand(JS_FUNCTION_TYPE));
1197 // 3a. Patch the first argument if necessary when calling a function.
1198 // a0: actual number of arguments
1200 Label shift_arguments;
1201 __ li(t0, Operand(0, RelocInfo::NONE32)); // Indicate regular JS_FUNCTION.
1202 { Label convert_to_object, use_global_proxy, patch_receiver;
1203 // Change context eagerly in case we need the global receiver.
1204 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
1206 // Do not transform the receiver for strict mode functions.
1207 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1208 __ lw(a3, FieldMemOperand(a2, SharedFunctionInfo::kCompilerHintsOffset));
1209 __ And(t3, a3, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1211 __ Branch(&shift_arguments, ne, t3, Operand(zero_reg));
1213 // Do not transform the receiver for native (Compilerhints already in a3).
1214 __ And(t3, a3, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1215 __ Branch(&shift_arguments, ne, t3, Operand(zero_reg));
1217 // Compute the receiver in sloppy mode.
1218 // Load first argument in a2. a2 = -kPointerSize(sp + n_args << 2).
1219 __ sll(at, a0, kPointerSizeLog2);
1220 __ addu(a2, sp, at);
1221 __ lw(a2, MemOperand(a2, -kPointerSize));
1222 // a0: actual number of arguments
1224 // a2: first argument
1225 __ JumpIfSmi(a2, &convert_to_object, t2);
1227 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
1228 __ Branch(&use_global_proxy, eq, a2, Operand(a3));
1229 __ LoadRoot(a3, Heap::kNullValueRootIndex);
1230 __ Branch(&use_global_proxy, eq, a2, Operand(a3));
1232 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1233 __ GetObjectType(a2, a3, a3);
1234 __ Branch(&shift_arguments, ge, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
1236 __ bind(&convert_to_object);
1237 // Enter an internal frame in order to preserve argument count.
1239 FrameScope scope(masm, StackFrame::INTERNAL);
1240 __ sll(a0, a0, kSmiTagSize); // Smi tagged.
1242 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1246 __ sra(a0, a0, kSmiTagSize); // Un-tag.
1247 // Leave internal frame.
1250 // Restore the function to a1, and the flag to t0.
1251 __ sll(at, a0, kPointerSizeLog2);
1252 __ addu(at, sp, at);
1253 __ lw(a1, MemOperand(at));
1254 __ Branch(USE_DELAY_SLOT, &patch_receiver);
1255 __ li(t0, Operand(0, RelocInfo::NONE32)); // In delay slot.
1257 __ bind(&use_global_proxy);
1258 __ lw(a2, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1259 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
1261 __ bind(&patch_receiver);
1262 __ sll(at, a0, kPointerSizeLog2);
1263 __ addu(a3, sp, at);
1264 __ sw(a2, MemOperand(a3, -kPointerSize));
1266 __ Branch(&shift_arguments);
1269 // 3b. Check for function proxy.
1271 __ li(t0, Operand(1, RelocInfo::NONE32)); // Indicate function proxy.
1272 __ Branch(&shift_arguments, eq, a2, Operand(JS_FUNCTION_PROXY_TYPE));
1274 __ bind(&non_function);
1275 __ li(t0, Operand(2, RelocInfo::NONE32)); // Indicate non-function.
1277 // 3c. Patch the first argument when calling a non-function. The
1278 // CALL_NON_FUNCTION builtin expects the non-function callee as
1279 // receiver, so overwrite the first argument which will ultimately
1280 // become the receiver.
1281 // a0: actual number of arguments
1283 // t0: call type (0: JS function, 1: function proxy, 2: non-function)
1284 __ sll(at, a0, kPointerSizeLog2);
1285 __ addu(a2, sp, at);
1286 __ sw(a1, MemOperand(a2, -kPointerSize));
1288 // 4. Shift arguments and return address one slot down on the stack
1289 // (overwriting the original receiver). Adjust argument count to make
1290 // the original first argument the new receiver.
1291 // a0: actual number of arguments
1293 // t0: call type (0: JS function, 1: function proxy, 2: non-function)
1294 __ bind(&shift_arguments);
1296 // Calculate the copy start address (destination). Copy end address is sp.
1297 __ sll(at, a0, kPointerSizeLog2);
1298 __ addu(a2, sp, at);
1301 __ lw(at, MemOperand(a2, -kPointerSize));
1302 __ sw(at, MemOperand(a2));
1303 __ Subu(a2, a2, Operand(kPointerSize));
1304 __ Branch(&loop, ne, a2, Operand(sp));
1305 // Adjust the actual number of arguments and remove the top element
1306 // (which is a copy of the last argument).
1307 __ Subu(a0, a0, Operand(1));
1311 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1312 // or a function proxy via CALL_FUNCTION_PROXY.
1313 // a0: actual number of arguments
1315 // t0: call type (0: JS function, 1: function proxy, 2: non-function)
1316 { Label function, non_proxy;
1317 __ Branch(&function, eq, t0, Operand(zero_reg));
1318 // Expected number of arguments is 0 for CALL_NON_FUNCTION.
1319 __ mov(a2, zero_reg);
1320 __ Branch(&non_proxy, ne, t0, Operand(1));
1322 __ push(a1); // Re-add proxy object as additional argument.
1323 __ Addu(a0, a0, Operand(1));
1324 __ GetBuiltinFunction(a1, Builtins::CALL_FUNCTION_PROXY);
1325 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1326 RelocInfo::CODE_TARGET);
1328 __ bind(&non_proxy);
1329 __ GetBuiltinFunction(a1, Builtins::CALL_NON_FUNCTION);
1330 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1331 RelocInfo::CODE_TARGET);
1335 // 5b. Get the code to call from the function and check that the number of
1336 // expected arguments matches what we're providing. If so, jump
1337 // (tail-call) to the code in register edx without checking arguments.
1338 // a0: actual number of arguments
1340 __ lw(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1342 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
1343 __ sra(a2, a2, kSmiTagSize);
1344 // Check formal and actual parameter counts.
1345 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1346 RelocInfo::CODE_TARGET, ne, a2, Operand(a0));
1348 __ lw(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
1349 ParameterCount expected(0);
1350 __ InvokeCode(a3, expected, expected, JUMP_FUNCTION, NullCallWrapper());
1354 static void Generate_CheckStackOverflow(MacroAssembler* masm,
1355 const int calleeOffset) {
1356 // Check the stack for overflow. We are not trying to catch
1357 // interruptions (e.g. debug break and preemption) here, so the "real stack
1358 // limit" is checked.
1360 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
1361 // Make a2 the space we have left. The stack might already be overflowed
1362 // here which will cause a2 to become negative.
1363 __ Subu(a2, sp, a2);
1364 // Check if the arguments will overflow the stack.
1365 __ sll(t3, v0, kPointerSizeLog2 - kSmiTagSize);
1366 // Signed comparison.
1367 __ Branch(&okay, gt, a2, Operand(t3));
1369 // Out of stack space.
1370 __ lw(a1, MemOperand(fp, calleeOffset));
1372 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1378 static void Generate_PushAppliedArguments(MacroAssembler* masm,
1379 const int argumentsOffset,
1380 const int indexOffset,
1381 const int limitOffset) {
1383 __ lw(a0, MemOperand(fp, indexOffset));
1386 // Load the current argument from the arguments array and push it to the
1388 // a0: current argument index
1390 __ lw(a1, MemOperand(fp, argumentsOffset));
1393 // Call the runtime to access the property in the arguments array.
1394 __ CallRuntime(Runtime::kGetProperty, 2);
1397 // Use inline caching to access the arguments.
1398 __ lw(a0, MemOperand(fp, indexOffset));
1399 __ Addu(a0, a0, Operand(1 << kSmiTagSize));
1400 __ sw(a0, MemOperand(fp, indexOffset));
1402 // Test if the copy loop has finished copying all the elements from the
1403 // arguments object.
1405 __ lw(a1, MemOperand(fp, limitOffset));
1406 __ Branch(&loop, ne, a0, Operand(a1));
1408 // On exit, the pushed arguments count is in a0, untagged
1413 // Used by FunctionApply and ReflectApply
1414 static void Generate_ApplyHelper(MacroAssembler* masm, bool targetIsArgument) {
1415 const int kFormalParameters = targetIsArgument ? 3 : 2;
1416 const int kStackSize = kFormalParameters + 1;
1419 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1420 const int kArgumentsOffset = kFPOnStackSize + kPCOnStackSize;
1421 const int kReceiverOffset = kArgumentsOffset + kPointerSize;
1422 const int kFunctionOffset = kReceiverOffset + kPointerSize;
1424 __ lw(a0, MemOperand(fp, kFunctionOffset)); // Get the function.
1426 __ lw(a0, MemOperand(fp, kArgumentsOffset)); // Get the args array.
1428 // Returns (in v0) number of arguments to copy to stack as Smi.
1429 if (targetIsArgument) {
1430 __ InvokeBuiltin(Builtins::REFLECT_APPLY_PREPARE, CALL_FUNCTION);
1432 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1435 // Returns the result in v0.
1436 Generate_CheckStackOverflow(masm, kFunctionOffset);
1438 // Push current limit and index.
1439 const int kIndexOffset =
1440 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1441 const int kLimitOffset =
1442 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1443 __ mov(a1, zero_reg);
1444 __ Push(v0, a1); // Limit and initial index.
1446 // Get the receiver.
1447 __ lw(a0, MemOperand(fp, kReceiverOffset));
1449 // Check that the function is a JS function (otherwise it must be a proxy).
1450 Label push_receiver;
1451 __ lw(a1, MemOperand(fp, kFunctionOffset));
1452 __ GetObjectType(a1, a2, a2);
1453 __ Branch(&push_receiver, ne, a2, Operand(JS_FUNCTION_TYPE));
1455 // Change context eagerly to get the right global object if necessary.
1456 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
1457 // Load the shared function info while the function is still in a1.
1458 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1460 // Compute the receiver.
1461 // Do not transform the receiver for strict mode functions.
1462 Label call_to_object, use_global_proxy;
1463 __ lw(a2, FieldMemOperand(a2, SharedFunctionInfo::kCompilerHintsOffset));
1464 __ And(t3, a2, Operand(1 << (SharedFunctionInfo::kStrictModeFunction +
1466 __ Branch(&push_receiver, ne, t3, Operand(zero_reg));
1468 // Do not transform the receiver for native (Compilerhints already in a2).
1469 __ And(t3, a2, Operand(1 << (SharedFunctionInfo::kNative + kSmiTagSize)));
1470 __ Branch(&push_receiver, ne, t3, Operand(zero_reg));
1472 // Compute the receiver in sloppy mode.
1473 __ JumpIfSmi(a0, &call_to_object);
1474 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1475 __ Branch(&use_global_proxy, eq, a0, Operand(a1));
1476 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1477 __ Branch(&use_global_proxy, eq, a0, Operand(a2));
1479 // Check if the receiver is already a JavaScript object.
1481 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1482 __ GetObjectType(a0, a1, a1);
1483 __ Branch(&push_receiver, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
1485 // Convert the receiver to a regular object.
1487 __ bind(&call_to_object);
1489 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1490 __ mov(a0, v0); // Put object in a0 to match other paths to push_receiver.
1491 __ Branch(&push_receiver);
1493 __ bind(&use_global_proxy);
1494 __ lw(a0, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1495 __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalProxyOffset));
1497 // Push the receiver.
1499 __ bind(&push_receiver);
1502 // Copy all arguments from the array to the stack.
1503 Generate_PushAppliedArguments(
1504 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1506 // Call the function.
1508 ParameterCount actual(a0);
1509 __ lw(a1, MemOperand(fp, kFunctionOffset));
1510 __ GetObjectType(a1, a2, a2);
1511 __ Branch(&call_proxy, ne, a2, Operand(JS_FUNCTION_TYPE));
1513 __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
1515 frame_scope.GenerateLeaveFrame();
1516 __ Ret(USE_DELAY_SLOT);
1517 __ Addu(sp, sp, Operand(kStackSize * kPointerSize)); // In delay slot.
1519 // Call the function proxy.
1520 __ bind(&call_proxy);
1521 __ push(a1); // Add function proxy as last argument.
1522 __ Addu(a0, a0, Operand(1));
1523 __ li(a2, Operand(0, RelocInfo::NONE32));
1524 __ GetBuiltinFunction(a1, Builtins::CALL_FUNCTION_PROXY);
1525 __ Call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1526 RelocInfo::CODE_TARGET);
1527 // Tear down the internal frame and remove function, receiver and args.
1530 __ Ret(USE_DELAY_SLOT);
1531 __ Addu(sp, sp, Operand(kStackSize * kPointerSize)); // In delay slot.
1535 static void Generate_ConstructHelper(MacroAssembler* masm) {
1536 const int kFormalParameters = 3;
1537 const int kStackSize = kFormalParameters + 1;
1540 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1541 const int kNewTargetOffset = kFPOnStackSize + kPCOnStackSize;
1542 const int kArgumentsOffset = kNewTargetOffset + kPointerSize;
1543 const int kFunctionOffset = kArgumentsOffset + kPointerSize;
1545 // If newTarget is not supplied, set it to constructor
1546 Label validate_arguments;
1547 __ lw(a0, MemOperand(fp, kNewTargetOffset));
1548 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1549 __ Branch(&validate_arguments, ne, a0, Operand(at));
1550 __ lw(a0, MemOperand(fp, kFunctionOffset));
1551 __ sw(a0, MemOperand(fp, kNewTargetOffset));
1553 // Validate arguments
1554 __ bind(&validate_arguments);
1555 __ lw(a0, MemOperand(fp, kFunctionOffset)); // get the function
1557 __ lw(a0, MemOperand(fp, kArgumentsOffset)); // get the args array
1559 __ lw(a0, MemOperand(fp, kNewTargetOffset)); // get the new.target
1561 // Returns argument count in v0.
1562 __ InvokeBuiltin(Builtins::REFLECT_CONSTRUCT_PREPARE, CALL_FUNCTION);
1564 // Returns result in v0.
1565 Generate_CheckStackOverflow(masm, kFunctionOffset);
1567 // Push current limit and index.
1568 const int kIndexOffset =
1569 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1570 const int kLimitOffset =
1571 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1572 __ push(v0); // limit
1573 __ mov(a1, zero_reg); // initial index
1575 // Push newTarget and callee functions
1576 __ lw(a0, MemOperand(fp, kNewTargetOffset));
1578 __ lw(a0, MemOperand(fp, kFunctionOffset));
1581 // Copy all arguments from the array to the stack.
1582 Generate_PushAppliedArguments(
1583 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1585 // Use undefined feedback vector
1586 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1587 __ lw(a1, MemOperand(fp, kFunctionOffset));
1589 // Call the function.
1590 CallConstructStub stub(masm->isolate(), SUPER_CONSTRUCTOR_CALL);
1591 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
1595 // Leave internal frame.
1598 __ Addu(sp, sp, Operand(kStackSize * kPointerSize)); // In delay slot.
1602 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1603 Generate_ApplyHelper(masm, false);
1607 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1608 Generate_ApplyHelper(masm, true);
1612 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1613 Generate_ConstructHelper(masm);
1617 static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1618 Label* stack_overflow) {
1619 // ----------- S t a t e -------------
1620 // -- a0 : actual number of arguments
1621 // -- a1 : function (passed through to callee)
1622 // -- a2 : expected number of arguments
1623 // -----------------------------------
1624 // Check the stack for overflow. We are not trying to catch
1625 // interruptions (e.g. debug break and preemption) here, so the "real stack
1626 // limit" is checked.
1627 __ LoadRoot(t1, Heap::kRealStackLimitRootIndex);
1628 // Make t1 the space we have left. The stack might already be overflowed
1629 // here which will cause t1 to become negative.
1630 __ subu(t1, sp, t1);
1631 // Check if the arguments will overflow the stack.
1632 __ sll(at, a2, kPointerSizeLog2);
1633 // Signed comparison.
1634 __ Branch(stack_overflow, le, t1, Operand(at));
1638 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1639 __ sll(a0, a0, kSmiTagSize);
1640 __ li(t0, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1641 __ MultiPush(a0.bit() | a1.bit() | t0.bit() | fp.bit() | ra.bit());
1643 Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
1647 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1648 // ----------- S t a t e -------------
1649 // -- v0 : result being passed through
1650 // -----------------------------------
1651 // Get the number of arguments passed (as a smi), tear down the frame and
1652 // then tear down the parameters.
1653 __ lw(a1, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1656 __ MultiPop(fp.bit() | ra.bit());
1657 __ sll(t0, a1, kPointerSizeLog2 - kSmiTagSize);
1658 __ Addu(sp, sp, t0);
1659 // Adjust for the receiver.
1660 __ Addu(sp, sp, Operand(kPointerSize));
1664 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1665 // State setup as expected by MacroAssembler::InvokePrologue.
1666 // ----------- S t a t e -------------
1667 // -- a0: actual arguments count
1668 // -- a1: function (passed through to callee)
1669 // -- a2: expected arguments count
1670 // -----------------------------------
1672 Label stack_overflow;
1673 ArgumentAdaptorStackCheck(masm, &stack_overflow);
1674 Label invoke, dont_adapt_arguments;
1676 Label enough, too_few;
1677 __ lw(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
1678 __ Branch(&dont_adapt_arguments, eq,
1679 a2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1680 // We use Uless as the number of argument should always be greater than 0.
1681 __ Branch(&too_few, Uless, a0, Operand(a2));
1683 { // Enough parameters: actual >= expected.
1684 // a0: actual number of arguments as a smi
1686 // a2: expected number of arguments
1687 // a3: code entry to call
1689 EnterArgumentsAdaptorFrame(masm);
1691 // Calculate copy start address into a0 and copy end address into a2.
1692 __ sll(a0, a0, kPointerSizeLog2 - kSmiTagSize);
1693 __ Addu(a0, fp, a0);
1694 // Adjust for return address and receiver.
1695 __ Addu(a0, a0, Operand(2 * kPointerSize));
1696 // Compute copy end address.
1697 __ sll(a2, a2, kPointerSizeLog2);
1698 __ subu(a2, a0, a2);
1700 // Copy the arguments (including the receiver) to the new stack frame.
1701 // a0: copy start address
1703 // a2: copy end address
1704 // a3: code entry to call
1708 __ lw(t0, MemOperand(a0));
1710 __ Branch(USE_DELAY_SLOT, ©, ne, a0, Operand(a2));
1711 __ addiu(a0, a0, -kPointerSize); // In delay slot.
1716 { // Too few parameters: Actual < expected.
1718 EnterArgumentsAdaptorFrame(masm);
1720 // Calculate copy start address into a0 and copy end address is fp.
1721 // a0: actual number of arguments as a smi
1723 // a2: expected number of arguments
1724 // a3: code entry to call
1725 __ sll(a0, a0, kPointerSizeLog2 - kSmiTagSize);
1726 __ Addu(a0, fp, a0);
1727 // Adjust for return address and receiver.
1728 __ Addu(a0, a0, Operand(2 * kPointerSize));
1729 // Compute copy end address. Also adjust for return address.
1730 __ Addu(t3, fp, kPointerSize);
1732 // Copy the arguments (including the receiver) to the new stack frame.
1733 // a0: copy start address
1735 // a2: expected number of arguments
1736 // a3: code entry to call
1737 // t3: copy end address
1740 __ lw(t0, MemOperand(a0)); // Adjusted above for return addr and receiver.
1741 __ Subu(sp, sp, kPointerSize);
1742 __ Subu(a0, a0, kPointerSize);
1743 __ Branch(USE_DELAY_SLOT, ©, ne, a0, Operand(t3));
1744 __ sw(t0, MemOperand(sp)); // In the delay slot.
1746 // Fill the remaining expected arguments with undefined.
1748 // a2: expected number of arguments
1749 // a3: code entry to call
1750 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
1751 __ sll(t2, a2, kPointerSizeLog2);
1752 __ Subu(a2, fp, Operand(t2));
1753 // Adjust for frame.
1754 __ Subu(a2, a2, Operand(StandardFrameConstants::kFixedFrameSizeFromFp +
1759 __ Subu(sp, sp, kPointerSize);
1760 __ Branch(USE_DELAY_SLOT, &fill, ne, sp, Operand(a2));
1761 __ sw(t0, MemOperand(sp));
1764 // Call the entry point.
1769 // Store offset of return address for deoptimizer.
1770 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1772 // Exit frame and return.
1773 LeaveArgumentsAdaptorFrame(masm);
1777 // -------------------------------------------
1778 // Don't adapt arguments.
1779 // -------------------------------------------
1780 __ bind(&dont_adapt_arguments);
1783 __ bind(&stack_overflow);
1785 FrameScope frame(masm, StackFrame::MANUAL);
1786 EnterArgumentsAdaptorFrame(masm);
1787 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1795 } } // namespace v8::internal
1797 #endif // V8_TARGET_ARCH_MIPS