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_MIPS64
11 #include "src/codegen.h"
12 #include "src/debug/debug.h"
13 #include "src/deoptimizer.h"
14 #include "src/full-codegen/full-codegen.h"
15 #include "src/runtime/runtime.h"
21 #define __ ACCESS_MASM(masm)
24 void Builtins::Generate_Adaptor(MacroAssembler* masm,
26 BuiltinExtraArguments extra_args) {
27 // ----------- S t a t e -------------
28 // -- a0 : number of arguments excluding receiver
29 // -- a1 : called function (only guaranteed when
30 // -- extra_args requires it)
32 // -- sp[0] : last argument
34 // -- sp[8 * (argc - 1)] : first argument
35 // -- sp[8 * agrc] : receiver
36 // -----------------------------------
38 // Insert extra arguments.
39 int num_extra_args = 0;
40 if (extra_args == NEEDS_CALLED_FUNCTION) {
44 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
47 // JumpToExternalReference expects a0 to contain the number of arguments
48 // including the receiver and the extra arguments.
49 __ Daddu(a0, a0, num_extra_args + 1);
50 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
54 // Load the built-in InternalArray function from the current context.
55 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
57 // Load the native context.
60 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
62 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
63 // Load the InternalArray function from the native context.
67 Context::INTERNAL_ARRAY_FUNCTION_INDEX)));
71 // Load the built-in Array function from the current context.
72 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
73 // Load the native context.
76 MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
78 FieldMemOperand(result, GlobalObject::kNativeContextOffset));
79 // Load the Array function from the native context.
82 Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
86 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
87 // ----------- S t a t e -------------
88 // -- a0 : number of arguments
89 // -- ra : return address
90 // -- sp[...]: constructor arguments
91 // -----------------------------------
92 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
94 // Get the InternalArray function.
95 GenerateLoadInternalArrayFunction(masm, a1);
97 if (FLAG_debug_code) {
98 // Initial map for the builtin InternalArray functions should be maps.
99 __ ld(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
101 __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction,
102 a4, Operand(zero_reg));
103 __ GetObjectType(a2, a3, a4);
104 __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction,
105 a4, Operand(MAP_TYPE));
108 // Run the native code for the InternalArray function called as a normal
111 InternalArrayConstructorStub stub(masm->isolate());
112 __ TailCallStub(&stub);
116 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
117 // ----------- S t a t e -------------
118 // -- a0 : number of arguments
119 // -- ra : return address
120 // -- sp[...]: constructor arguments
121 // -----------------------------------
122 Label generic_array_code;
124 // Get the Array function.
125 GenerateLoadArrayFunction(masm, a1);
127 if (FLAG_debug_code) {
128 // Initial map for the builtin Array functions should be maps.
129 __ ld(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
131 __ Assert(ne, kUnexpectedInitialMapForArrayFunction1,
132 a4, Operand(zero_reg));
133 __ GetObjectType(a2, a3, a4);
134 __ Assert(eq, kUnexpectedInitialMapForArrayFunction2,
135 a4, Operand(MAP_TYPE));
138 // Run the native code for the Array function called as a normal function.
141 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
142 ArrayConstructorStub stub(masm->isolate());
143 __ TailCallStub(&stub);
147 void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
148 // ----------- S t a t e -------------
149 // -- a0 : number of arguments
150 // -- a1 : constructor function
151 // -- ra : return address
152 // -- sp[(argc - n - 1) * 8] : arg[n] (zero based)
153 // -- sp[argc * 8] : receiver
154 // -----------------------------------
155 Counters* counters = masm->isolate()->counters();
156 __ IncrementCounter(counters->string_ctor_calls(), 1, a2, a3);
158 Register function = a1;
159 if (FLAG_debug_code) {
160 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, a2);
161 __ Assert(eq, kUnexpectedStringFunction, function, Operand(a2));
164 // Load the first arguments in a0 and get rid of the rest.
166 __ Branch(&no_arguments, eq, a0, Operand(zero_reg));
167 // First args = sp[(argc - 1) * 8].
168 __ Dsubu(a0, a0, Operand(1));
169 __ dsll(a0, a0, kPointerSizeLog2);
170 __ Daddu(sp, a0, sp);
171 __ ld(a0, MemOperand(sp));
172 // sp now point to args[0], drop args[0] + receiver.
175 Register argument = a2;
176 Label not_cached, argument_is_string;
177 __ LookupNumberStringCache(a0, // Input.
183 __ IncrementCounter(counters->string_ctor_cached_number(), 1, a3, a4);
184 __ bind(&argument_is_string);
186 // ----------- S t a t e -------------
187 // -- a2 : argument converted to string
188 // -- a1 : constructor function
189 // -- ra : return address
190 // -----------------------------------
193 __ Allocate(JSValue::kSize,
200 // Initialising the String Object.
202 __ LoadGlobalFunctionInitialMap(function, map, a4);
203 if (FLAG_debug_code) {
204 __ lbu(a4, FieldMemOperand(map, Map::kInstanceSizeOffset));
205 __ Assert(eq, kUnexpectedStringWrapperInstanceSize,
206 a4, Operand(JSValue::kSize >> kPointerSizeLog2));
207 __ lbu(a4, FieldMemOperand(map, Map::kUnusedPropertyFieldsOffset));
208 __ Assert(eq, kUnexpectedUnusedPropertiesOfStringWrapper,
209 a4, Operand(zero_reg));
211 __ sd(map, FieldMemOperand(v0, HeapObject::kMapOffset));
213 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
214 __ sd(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
215 __ sd(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
217 __ sd(argument, FieldMemOperand(v0, JSValue::kValueOffset));
219 // Ensure the object is fully initialized.
220 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
224 // The argument was not found in the number to string cache. Check
225 // if it's a string already before calling the conversion builtin.
226 Label convert_argument;
227 __ bind(¬_cached);
228 __ JumpIfSmi(a0, &convert_argument);
231 __ ld(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
232 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
233 STATIC_ASSERT(kNotStringTag != 0);
234 __ And(a4, a3, Operand(kIsNotStringMask));
235 __ Branch(&convert_argument, ne, a4, Operand(zero_reg));
236 __ mov(argument, a0);
237 __ IncrementCounter(counters->string_ctor_conversions(), 1, a3, a4);
238 __ Branch(&argument_is_string);
240 // Invoke the conversion builtin and put the result into a2.
241 __ bind(&convert_argument);
242 __ push(function); // Preserve the function.
243 __ IncrementCounter(counters->string_ctor_conversions(), 1, a3, a4);
245 FrameScope scope(masm, StackFrame::INTERNAL);
247 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
250 __ mov(argument, v0);
251 __ Branch(&argument_is_string);
253 // Load the empty string into a2, remove the receiver from the
254 // stack, and jump back to the case where the argument is a string.
255 __ bind(&no_arguments);
256 __ LoadRoot(argument, Heap::kempty_stringRootIndex);
258 __ Branch(&argument_is_string);
260 // At this point the argument is already a string. Call runtime to
261 // create a string wrapper.
262 __ bind(&gc_required);
263 __ IncrementCounter(counters->string_ctor_gc_required(), 1, a3, a4);
265 FrameScope scope(masm, StackFrame::INTERNAL);
267 __ CallRuntime(Runtime::kNewStringWrapper, 1);
273 static void CallRuntimePassFunction(
274 MacroAssembler* masm, Runtime::FunctionId function_id) {
275 FrameScope scope(masm, StackFrame::INTERNAL);
276 // Push a copy of the function onto the stack.
277 // Push call kind information and function as parameter to the runtime call.
280 __ CallRuntime(function_id, 1);
281 // Restore call kind information and receiver.
286 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
287 __ ld(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
288 __ ld(a2, FieldMemOperand(a2, SharedFunctionInfo::kCodeOffset));
289 __ Daddu(at, a2, Operand(Code::kHeaderSize - kHeapObjectTag));
294 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
295 __ Daddu(at, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
300 void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
301 // Checking whether the queued function is ready for install is optional,
302 // since we come across interrupts and stack checks elsewhere. However,
303 // not checking may delay installing ready functions, and always checking
304 // would be quite expensive. A good compromise is to first check against
305 // stack limit as a cue for an interrupt signal.
307 __ LoadRoot(a4, Heap::kStackLimitRootIndex);
308 __ Branch(&ok, hs, sp, Operand(a4));
310 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
311 GenerateTailCallToReturnedCode(masm);
314 GenerateTailCallToSharedCode(masm);
318 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
319 bool is_api_function,
320 bool create_memento) {
321 // ----------- S t a t e -------------
322 // -- a0 : number of arguments
323 // -- a1 : constructor function
324 // -- a2 : allocation site or undefined
325 // -- a3 : original constructor
326 // -- ra : return address
327 // -- sp[...]: constructor arguments
328 // -----------------------------------
330 // Should never create mementos for api functions.
331 DCHECK(!is_api_function || !create_memento);
333 Isolate* isolate = masm->isolate();
335 // Enter a construct frame.
337 FrameScope scope(masm, StackFrame::CONSTRUCT);
339 // Preserve the incoming parameters on the stack.
340 __ AssertUndefinedOrAllocationSite(a2, t0);
342 __ Push(a2, a0, a1, a3);
344 // Try to allocate the object without transitioning into C code. If any of
345 // the preconditions is not met, the code bails out to the runtime call.
346 Label rt_call, allocated;
347 if (FLAG_inline_new) {
348 ExternalReference debug_step_in_fp =
349 ExternalReference::debug_step_in_fp_address(isolate);
350 __ li(a2, Operand(debug_step_in_fp));
351 __ ld(a2, MemOperand(a2));
352 __ Branch(&rt_call, ne, a2, Operand(zero_reg));
354 // Fall back to runtime if the original constructor and function differ.
355 __ Branch(&rt_call, ne, a1, Operand(a3));
357 // Load the initial map and verify that it is in fact a map.
358 // a1: constructor function
359 __ ld(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
360 __ JumpIfSmi(a2, &rt_call);
361 __ GetObjectType(a2, t1, t0);
362 __ Branch(&rt_call, ne, t0, Operand(MAP_TYPE));
364 // Check that the constructor is not constructing a JSFunction (see
365 // comments in Runtime_NewObject in runtime.cc). In which case the
366 // initial map's instance type would be JS_FUNCTION_TYPE.
367 // a1: constructor function
369 __ lbu(t1, FieldMemOperand(a2, Map::kInstanceTypeOffset));
370 __ Branch(&rt_call, eq, t1, Operand(JS_FUNCTION_TYPE));
372 if (!is_api_function) {
374 MemOperand bit_field3 = FieldMemOperand(a2, Map::kBitField3Offset);
375 // Check if slack tracking is enabled.
376 __ lwu(a4, bit_field3);
377 __ DecodeField<Map::Counter>(a6, a4);
378 __ Branch(&allocate, lt, a6,
379 Operand(static_cast<int64_t>(Map::kSlackTrackingCounterEnd)));
380 // Decrease generous allocation count.
381 __ Dsubu(a4, a4, Operand(1 << Map::Counter::kShift));
382 __ Branch(USE_DELAY_SLOT, &allocate, ne, a6,
383 Operand(Map::kSlackTrackingCounterEnd));
384 __ sw(a4, bit_field3); // In delay slot.
386 __ Push(a1, a2, a1); // a1 = Constructor.
387 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
390 __ li(a6, Operand(Map::kSlackTrackingCounterEnd - 1));
395 // Now allocate the JSObject on the heap.
396 // a1: constructor function
398 Label rt_call_reload_new_target;
399 __ lbu(a3, FieldMemOperand(a2, Map::kInstanceSizeOffset));
400 if (create_memento) {
401 __ Daddu(a3, a3, Operand(AllocationMemento::kSize / kPointerSize));
404 __ Allocate(a3, t0, t1, t2, &rt_call_reload_new_target, SIZE_IN_WORDS);
406 // Allocated the JSObject, now initialize the fields. Map is set to
407 // initial map and properties and elements are set to empty fixed array.
408 // a1: constructor function
410 // a3: object size (including memento if create_memento)
411 // t0: JSObject (not tagged)
412 __ LoadRoot(t2, Heap::kEmptyFixedArrayRootIndex);
414 __ sd(a2, MemOperand(t1, JSObject::kMapOffset));
415 __ sd(t2, MemOperand(t1, JSObject::kPropertiesOffset));
416 __ sd(t2, MemOperand(t1, JSObject::kElementsOffset));
417 __ Daddu(t1, t1, Operand(3*kPointerSize));
418 DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
419 DCHECK_EQ(1 * kPointerSize, JSObject::kPropertiesOffset);
420 DCHECK_EQ(2 * kPointerSize, JSObject::kElementsOffset);
422 // Fill all the in-object properties with appropriate filler.
423 // a1: constructor function
425 // a3: object size (in words, including memento if create_memento)
426 // t0: JSObject (not tagged)
427 // t1: First in-object property of JSObject (not tagged)
428 // a6: slack tracking counter (non-API function case)
429 DCHECK_EQ(3 * kPointerSize, JSObject::kHeaderSize);
431 // Use t3 to hold undefined, which is used in several places below.
432 __ LoadRoot(t3, Heap::kUndefinedValueRootIndex);
434 if (!is_api_function) {
435 Label no_inobject_slack_tracking;
437 // Check if slack tracking is enabled.
438 __ Branch(&no_inobject_slack_tracking, lt, a6,
439 Operand(static_cast<int64_t>(Map::kSlackTrackingCounterEnd)));
441 // Allocate object with a slack.
442 __ lbu(a0, FieldMemOperand(a2, Map::kInObjectPropertiesOffset));
443 __ lbu(a2, FieldMemOperand(a2, Map::kUnusedPropertyFieldsOffset));
444 __ dsubu(a0, a0, a2);
445 __ dsll(at, a0, kPointerSizeLog2);
446 __ daddu(a0, t1, at);
447 // a0: offset of first field after pre-allocated fields
448 if (FLAG_debug_code) {
449 __ dsll(at, a3, kPointerSizeLog2);
450 __ Daddu(t2, t0, Operand(at)); // End of object.
451 __ Assert(le, kUnexpectedNumberOfPreAllocatedPropertyFields,
454 __ InitializeFieldsWithFiller(t1, a0, t3);
455 // To allow for truncation.
456 __ LoadRoot(t3, Heap::kOnePointerFillerMapRootIndex);
457 // Fill the remaining fields with one pointer filler map.
459 __ bind(&no_inobject_slack_tracking);
462 if (create_memento) {
463 __ Dsubu(a0, a3, Operand(AllocationMemento::kSize / kPointerSize));
464 __ dsll(a0, a0, kPointerSizeLog2);
465 __ Daddu(a0, t0, Operand(a0)); // End of object.
466 __ InitializeFieldsWithFiller(t1, a0, t3);
468 // Fill in memento fields.
469 // t1: points to the allocated but uninitialized memento.
470 __ LoadRoot(t3, Heap::kAllocationMementoMapRootIndex);
471 DCHECK_EQ(0 * kPointerSize, AllocationMemento::kMapOffset);
472 __ sd(t3, MemOperand(t1));
473 __ Daddu(t1, t1, kPointerSize);
474 // Load the AllocationSite.
475 __ ld(t3, MemOperand(sp, 3 * kPointerSize));
476 __ AssertUndefinedOrAllocationSite(t3, a0);
477 DCHECK_EQ(1 * kPointerSize, AllocationMemento::kAllocationSiteOffset);
478 __ sd(t3, MemOperand(t1));
479 __ Daddu(t1, t1, kPointerSize);
481 __ dsll(at, a3, kPointerSizeLog2);
482 __ Daddu(a0, t0, Operand(at)); // End of object.
483 __ InitializeFieldsWithFiller(t1, a0, t3);
486 // Add the object tag to make the JSObject real, so that we can continue
487 // and jump into the continuation code at any time from now on.
488 __ Daddu(t0, t0, Operand(kHeapObjectTag));
490 // Continue with JSObject being successfully allocated.
494 // Reload the original constructor and fall-through.
495 __ bind(&rt_call_reload_new_target);
496 __ ld(a3, MemOperand(sp, 0 * kPointerSize));
499 // Allocate the new receiver object using the runtime call.
500 // a1: constructor function
501 // a3: original constructor
503 if (create_memento) {
504 // Get the cell or allocation site.
505 __ ld(a2, MemOperand(sp, 3 * kPointerSize));
506 __ push(a2); // argument 1: allocation site
509 __ Push(a1, a3); // arguments 2-3 / 1-2
510 if (create_memento) {
511 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 3);
513 __ CallRuntime(Runtime::kNewObject, 2);
517 // Runtime_NewObjectWithAllocationSite increments allocation count.
518 // Skip the increment.
519 Label count_incremented;
520 if (create_memento) {
521 __ jmp(&count_incremented);
524 // Receiver for constructor call allocated.
528 if (create_memento) {
529 __ ld(a2, MemOperand(sp, 3 * kPointerSize));
530 __ LoadRoot(t1, Heap::kUndefinedValueRootIndex);
531 __ Branch(&count_incremented, eq, a2, Operand(t1));
532 // a2 is an AllocationSite. We are creating a memento from it, so we
533 // need to increment the memento create count.
534 __ ld(a3, FieldMemOperand(a2,
535 AllocationSite::kPretenureCreateCountOffset));
536 __ Daddu(a3, a3, Operand(Smi::FromInt(1)));
537 __ sd(a3, FieldMemOperand(a2,
538 AllocationSite::kPretenureCreateCountOffset));
539 __ bind(&count_incremented);
542 // Restore the parameters.
543 __ Pop(a3); // new.target
546 __ ld(a0, MemOperand(sp));
551 // Set up pointer to last argument.
552 __ Daddu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
554 // Copy arguments and receiver to the expression stack.
555 // a0: number of arguments
556 // a1: constructor function
557 // a2: address of last argument (caller sp)
558 // a3: number of arguments (smi-tagged)
562 // sp[3]: number of arguments (smi-tagged)
567 __ dsll(a4, a3, kPointerSizeLog2);
568 __ Daddu(a4, a2, Operand(a4));
569 __ ld(a5, MemOperand(a4));
572 __ Daddu(a3, a3, Operand(-1));
573 __ Branch(&loop, greater_equal, a3, Operand(zero_reg));
575 // Call the function.
576 // a0: number of arguments
577 // a1: constructor function
578 if (is_api_function) {
579 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
581 masm->isolate()->builtins()->HandleApiCallConstruct();
582 __ Call(code, RelocInfo::CODE_TARGET);
584 ParameterCount actual(a0);
585 __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
588 // Store offset of return address for deoptimizer.
589 if (!is_api_function) {
590 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
593 // Restore context from the frame.
594 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
596 // If the result is an object (in the ECMA sense), we should get rid
597 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
599 Label use_receiver, exit;
601 // If the result is a smi, it is *not* an object in the ECMA sense.
603 // sp[0]: receiver (newly allocated object)
605 // sp[2]: number of arguments (smi-tagged)
606 __ JumpIfSmi(v0, &use_receiver);
608 // If the type of the result (stored in its map) is less than
609 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
610 __ GetObjectType(v0, a1, a3);
611 __ Branch(&exit, greater_equal, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
613 // Throw away the result of the constructor invocation and use the
614 // on-stack receiver as the result.
615 __ bind(&use_receiver);
616 __ ld(v0, MemOperand(sp));
618 // Remove receiver from the stack, remove caller arguments, and
622 // sp[0]: receiver (newly allocated object)
623 // sp[1]: new.target (original constructor)
624 // sp[2]: number of arguments (smi-tagged)
625 __ ld(a1, MemOperand(sp, 2 * kPointerSize));
627 // Leave construct frame.
630 __ SmiScale(a4, a1, kPointerSizeLog2);
631 __ Daddu(sp, sp, a4);
632 __ Daddu(sp, sp, kPointerSize);
633 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, a1, a2);
638 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
639 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
643 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
644 Generate_JSConstructStubHelper(masm, true, false);
648 void Builtins::Generate_JSConstructStubForDerived(MacroAssembler* masm) {
649 // ----------- S t a t e -------------
650 // -- a0 : number of arguments
651 // -- a1 : constructor function
652 // -- a2 : allocation site or undefined
653 // -- a3 : original constructor
654 // -- ra : return address
655 // -- sp[...]: constructor arguments
656 // -----------------------------------
659 FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
661 __ AssertUndefinedOrAllocationSite(a2, t0);
666 __ push(a4); // Smi-tagged arguments count.
671 // receiver is the hole.
672 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
675 // Set up pointer to last argument.
676 __ Daddu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
678 // Copy arguments and receiver to the expression stack.
679 // a0: number of arguments
680 // a1: constructor function
681 // a2: address of last argument (caller sp)
682 // a4: number of arguments (smi-tagged)
685 // sp[2]: number of arguments (smi-tagged)
690 __ dsll(at, a4, kPointerSizeLog2);
691 __ Daddu(at, a2, Operand(at));
692 __ ld(at, MemOperand(at));
695 __ Daddu(a4, a4, Operand(-1));
696 __ Branch(&loop, ge, a4, Operand(zero_reg));
700 ExternalReference debug_step_in_fp =
701 ExternalReference::debug_step_in_fp_address(masm->isolate());
702 __ li(a2, Operand(debug_step_in_fp));
703 __ ld(a2, MemOperand(a2));
704 __ Branch(&skip_step_in, eq, a2, Operand(zero_reg));
707 __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
710 __ bind(&skip_step_in);
713 // Call the function.
714 // a0: number of arguments
715 // a1: constructor function
716 ParameterCount actual(a0);
717 __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
719 // Restore context from the frame.
722 // sp[1]: number of arguments (smi-tagged)
723 __ ld(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
724 __ ld(a1, MemOperand(sp, kPointerSize));
726 // Leave construct frame.
729 __ SmiScale(at, a1, kPointerSizeLog2);
730 __ Daddu(sp, sp, Operand(at));
731 __ Daddu(sp, sp, Operand(kPointerSize));
736 enum IsTagged { kArgcIsSmiTagged, kArgcIsUntaggedInt };
739 // Clobbers a2; preserves all other registers.
740 static void Generate_CheckStackOverflow(MacroAssembler* masm,
741 const int calleeOffset, Register argc,
742 IsTagged argc_is_tagged) {
743 // Check the stack for overflow. We are not trying to catch
744 // interruptions (e.g. debug break and preemption) here, so the "real stack
745 // limit" is checked.
747 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
748 // Make a2 the space we have left. The stack might already be overflowed
749 // here which will cause r2 to become negative.
750 __ dsubu(a2, sp, a2);
751 // Check if the arguments will overflow the stack.
752 if (argc_is_tagged == kArgcIsSmiTagged) {
753 __ SmiScale(a7, v0, kPointerSizeLog2);
755 DCHECK(argc_is_tagged == kArgcIsUntaggedInt);
756 __ dsll(a7, argc, kPointerSizeLog2);
758 __ Branch(&okay, gt, a2, Operand(a7)); // Signed comparison.
760 // Out of stack space.
761 __ ld(a1, MemOperand(fp, calleeOffset));
762 if (argc_is_tagged == kArgcIsUntaggedInt) {
766 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
772 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
774 // Called from JSEntryStub::GenerateBody
776 // ----------- S t a t e -------------
779 // -- a2: receiver_pointer
782 // -----------------------------------
783 ProfileEntryHookStub::MaybeCallEntryHook(masm);
784 // Clear the context before we push it when entering the JS frame.
785 __ mov(cp, zero_reg);
787 // Enter an internal frame.
789 FrameScope scope(masm, StackFrame::INTERNAL);
791 // Set up the context from the function argument.
792 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
794 // Push the function and the receiver onto the stack.
797 // Check if we have enough stack space to push all arguments.
798 // The function is the first thing that was pushed above after entering
799 // the internal frame.
800 const int kFunctionOffset =
801 InternalFrameConstants::kCodeOffset - kPointerSize;
803 Generate_CheckStackOverflow(masm, kFunctionOffset, a3, kArgcIsUntaggedInt);
805 // Copy arguments to the stack in a loop.
807 // s0: argv, i.e. points to first arg
809 __ dsll(a4, a3, kPointerSizeLog2);
810 __ daddu(a6, s0, a4);
812 __ nop(); // Branch delay slot nop.
813 // a6 points past last arg.
815 __ ld(a4, MemOperand(s0)); // Read next parameter.
816 __ daddiu(s0, s0, kPointerSize);
817 __ ld(a4, MemOperand(a4)); // Dereference handle.
818 __ push(a4); // Push parameter.
820 __ Branch(&loop, ne, s0, Operand(a6));
822 // Initialize all JavaScript callee-saved registers, since they will be seen
823 // by the garbage collector as part of handlers.
824 __ LoadRoot(a4, Heap::kUndefinedValueRootIndex);
830 // s6 holds the root address. Do not clobber.
831 // s7 is cp. Do not init.
833 // Invoke the code and pass argc as a0.
836 // No type feedback cell is available
837 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
838 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
841 ParameterCount actual(a0);
842 __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
845 // Leave internal frame.
851 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
852 Generate_JSEntryTrampolineHelper(masm, false);
856 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
857 Generate_JSEntryTrampolineHelper(masm, true);
861 // Generate code for entering a JS function with the interpreter.
862 // On entry to the function the receiver and arguments have been pushed on the
863 // stack left to right. The actual argument count matches the formal parameter
864 // count expected by the function.
866 // The live registers are:
867 // o a1: the JS function object being called.
869 // o fp: the caller's frame pointer
870 // o sp: stack pointer
871 // o ra: return address
873 // The function builds a JS frame. Please see JavaScriptFrameConstants in
874 // frames-mips.h for its layout.
875 // TODO(rmcilroy): We will need to include the current bytecode pointer in the
877 void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
878 // Open a frame scope to indicate that there is a frame on the stack. The
879 // MANUAL indicates that the scope shouldn't actually generate code to set up
880 // the frame (that is done below).
881 FrameScope frame_scope(masm, StackFrame::MANUAL);
883 __ Push(ra, fp, cp, a1);
884 __ Daddu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
886 // Get the bytecode array from the function object and load the pointer to the
887 // first entry into kInterpreterBytecodeRegister.
888 __ ld(a0, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
889 __ ld(kInterpreterBytecodeArrayRegister,
890 FieldMemOperand(a0, SharedFunctionInfo::kFunctionDataOffset));
892 if (FLAG_debug_code) {
893 // Check function data field is actually a BytecodeArray object.
894 __ SmiTst(kInterpreterBytecodeArrayRegister, a4);
895 __ Assert(ne, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, a4,
897 __ GetObjectType(kInterpreterBytecodeArrayRegister, a4, a4);
898 __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, a4,
899 Operand(BYTECODE_ARRAY_TYPE));
902 // Allocate the local and temporary register file on the stack.
904 // Load frame size from the BytecodeArray object.
905 __ ld(a4, FieldMemOperand(kInterpreterBytecodeArrayRegister,
906 BytecodeArray::kFrameSizeOffset));
908 // Do a stack check to ensure we don't go over the limit.
910 __ Dsubu(a5, sp, Operand(a4));
911 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
912 __ Branch(&ok, hs, a5, Operand(a2));
913 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
916 // If ok, push undefined as the initial value for all register file entries.
917 // Note: there should always be at least one stack slot for the return
918 // register in the register file.
920 __ LoadRoot(a5, Heap::kUndefinedValueRootIndex);
921 __ bind(&loop_header);
922 // TODO(rmcilroy): Consider doing more than one push per loop iteration.
924 // Continue loop if not done.
925 __ Dsubu(a4, a4, Operand(kPointerSize));
926 __ Branch(&loop_header, ge, a4, Operand(zero_reg));
929 // TODO(rmcilroy): List of things not currently dealt with here but done in
930 // fullcodegen's prologue:
931 // - Support profiler (specifically profiling_counter).
932 // - Call ProfileEntryHookStub when isolate has a function_entry_hook.
933 // - Allow simulator stop operations if FLAG_stop_at is set.
934 // - Deal with sloppy mode functions which need to replace the
935 // receiver with the global proxy when called as functions (without an
936 // explicit receiver object).
937 // - Code aging of the BytecodeArray object.
938 // - Supporting FLAG_trace.
940 // The following items are also not done here, and will probably be done using
941 // explicit bytecodes instead:
942 // - Allocating a new local context if applicable.
943 // - Setting up a local binding to the this function, which is used in
944 // derived constructors with super calls.
945 // - Setting new.target if required.
946 // - Dealing with REST parameters (only if
947 // https://codereview.chromium.org/1235153006 doesn't land by then).
948 // - Dealing with argument objects.
950 // Perform stack guard check.
953 __ LoadRoot(at, Heap::kStackLimitRootIndex);
954 __ Branch(&ok, hs, sp, Operand(at));
955 __ CallRuntime(Runtime::kStackGuard, 0);
959 // Load bytecode offset and dispatch table into registers.
960 __ li(kInterpreterBytecodeOffsetRegister,
961 Operand(BytecodeArray::kHeaderSize - kHeapObjectTag));
962 __ LoadRoot(kInterpreterDispatchTableRegister,
963 Heap::kInterpreterTableRootIndex);
964 __ Daddu(kInterpreterDispatchTableRegister, kInterpreterDispatchTableRegister,
965 Operand(FixedArray::kHeaderSize - kHeapObjectTag));
967 // Dispatch to the first bytecode handler for the function.
968 __ Daddu(a0, kInterpreterBytecodeArrayRegister,
969 kInterpreterBytecodeOffsetRegister);
970 __ lbu(a0, MemOperand(a0));
971 __ dsll(at, a0, kPointerSizeLog2);
972 __ Daddu(at, kInterpreterDispatchTableRegister, at);
973 __ ld(at, MemOperand(at));
974 // TODO(rmcilroy): Make dispatch table point to code entrys to avoid untagging
975 // and header removal.
976 __ Daddu(at, at, Operand(Code::kHeaderSize - kHeapObjectTag));
981 void Builtins::Generate_InterpreterExitTrampoline(MacroAssembler* masm) {
982 // TODO(rmcilroy): List of things not currently dealt with here but done in
983 // fullcodegen's EmitReturnSequence.
984 // - Supporting FLAG_trace for Runtime::TraceExit.
985 // - Support profiler (specifically decrementing profiling_counter
986 // appropriately and calling out to HandleInterrupts if necessary).
988 // Load return value into v0.
989 __ ld(v0, MemOperand(fp, -kPointerSize -
990 StandardFrameConstants::kFixedFrameSizeFromFp));
991 // Leave the frame (also dropping the register file).
992 __ LeaveFrame(StackFrame::JAVA_SCRIPT);
993 // Drop receiver + arguments.
994 __ Drop(1); // TODO(rmcilroy): Get number of arguments from BytecodeArray.
999 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
1000 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
1001 GenerateTailCallToReturnedCode(masm);
1005 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
1006 FrameScope scope(masm, StackFrame::INTERNAL);
1007 // Push a copy of the function onto the stack.
1008 // Push function as parameter to the runtime call.
1010 // Whether to compile in a background thread.
1012 at, concurrent ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
1015 __ CallRuntime(Runtime::kCompileOptimized, 2);
1016 // Restore receiver.
1021 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
1022 CallCompileOptimized(masm, false);
1023 GenerateTailCallToReturnedCode(masm);
1027 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
1028 CallCompileOptimized(masm, true);
1029 GenerateTailCallToReturnedCode(masm);
1033 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
1034 // For now, we are relying on the fact that make_code_young doesn't do any
1035 // garbage collection which allows us to save/restore the registers without
1036 // worrying about which of them contain pointers. We also don't build an
1037 // internal frame to make the code faster, since we shouldn't have to do stack
1038 // crawls in MakeCodeYoung. This seems a bit fragile.
1040 // Set a0 to point to the head of the PlatformCodeAge sequence.
1042 Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
1044 // The following registers must be saved and restored when calling through to
1046 // a0 - contains return address (beginning of patch sequence)
1048 RegList saved_regs =
1049 (a0.bit() | a1.bit() | ra.bit() | fp.bit()) & ~sp.bit();
1050 FrameScope scope(masm, StackFrame::MANUAL);
1051 __ MultiPush(saved_regs);
1052 __ PrepareCallCFunction(2, 0, a2);
1053 __ li(a1, Operand(ExternalReference::isolate_address(masm->isolate())));
1055 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
1056 __ MultiPop(saved_regs);
1060 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
1061 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
1062 MacroAssembler* masm) { \
1063 GenerateMakeCodeYoungAgainCommon(masm); \
1065 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
1066 MacroAssembler* masm) { \
1067 GenerateMakeCodeYoungAgainCommon(masm); \
1069 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
1070 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
1073 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
1074 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
1075 // that make_code_young doesn't do any garbage collection which allows us to
1076 // save/restore the registers without worrying about which of them contain
1079 // Set a0 to point to the head of the PlatformCodeAge sequence.
1081 Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
1083 // The following registers must be saved and restored when calling through to
1085 // a0 - contains return address (beginning of patch sequence)
1087 RegList saved_regs =
1088 (a0.bit() | a1.bit() | ra.bit() | fp.bit()) & ~sp.bit();
1089 FrameScope scope(masm, StackFrame::MANUAL);
1090 __ MultiPush(saved_regs);
1091 __ PrepareCallCFunction(2, 0, a2);
1092 __ li(a1, Operand(ExternalReference::isolate_address(masm->isolate())));
1094 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
1096 __ MultiPop(saved_regs);
1098 // Perform prologue operations usually performed by the young code stub.
1099 __ Push(ra, fp, cp, a1);
1100 __ Daddu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
1102 // Jump to point after the code-age stub.
1103 __ Daddu(a0, a0, Operand((kNoCodeAgeSequenceLength)));
1108 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
1109 GenerateMakeCodeYoungAgainCommon(masm);
1113 void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
1114 Generate_MarkCodeAsExecutedOnce(masm);
1118 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
1119 SaveFPRegsMode save_doubles) {
1121 FrameScope scope(masm, StackFrame::INTERNAL);
1123 // Preserve registers across notification, this is important for compiled
1124 // stubs that tail call the runtime on deopts passing their parameters in
1126 __ MultiPush(kJSCallerSaved | kCalleeSaved);
1127 // Pass the function and deoptimization type to the runtime system.
1128 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
1129 __ MultiPop(kJSCallerSaved | kCalleeSaved);
1132 __ Daddu(sp, sp, Operand(kPointerSize)); // Ignore state
1133 __ Jump(ra); // Jump to miss handler
1137 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
1138 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
1142 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
1143 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
1147 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1148 Deoptimizer::BailoutType type) {
1150 FrameScope scope(masm, StackFrame::INTERNAL);
1151 // Pass the function and deoptimization type to the runtime system.
1152 __ li(a0, Operand(Smi::FromInt(static_cast<int>(type))));
1154 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
1157 // Get the full codegen state from the stack and untag it -> a6.
1158 __ ld(a6, MemOperand(sp, 0 * kPointerSize));
1160 // Switch on the state.
1161 Label with_tos_register, unknown_state;
1162 __ Branch(&with_tos_register,
1163 ne, a6, Operand(FullCodeGenerator::NO_REGISTERS));
1164 __ Ret(USE_DELAY_SLOT);
1165 // Safe to fill delay slot Addu will emit one instruction.
1166 __ Daddu(sp, sp, Operand(1 * kPointerSize)); // Remove state.
1168 __ bind(&with_tos_register);
1169 __ ld(v0, MemOperand(sp, 1 * kPointerSize));
1170 __ Branch(&unknown_state, ne, a6, Operand(FullCodeGenerator::TOS_REG));
1172 __ Ret(USE_DELAY_SLOT);
1173 // Safe to fill delay slot Addu will emit one instruction.
1174 __ Daddu(sp, sp, Operand(2 * kPointerSize)); // Remove state.
1176 __ bind(&unknown_state);
1177 __ stop("no cases left");
1181 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1182 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1186 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
1187 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1191 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1192 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1196 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1197 // Lookup the function in the JavaScript frame.
1198 __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1200 FrameScope scope(masm, StackFrame::INTERNAL);
1201 // Pass function as argument.
1203 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1206 // If the code object is null, just return to the unoptimized code.
1207 __ Ret(eq, v0, Operand(Smi::FromInt(0)));
1209 // Load deoptimization data from the code object.
1210 // <deopt_data> = <code>[#deoptimization_data_offset]
1211 __ ld(a1, MemOperand(v0, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1213 // Load the OSR entrypoint offset from the deoptimization data.
1214 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1215 __ ld(a1, MemOperand(a1, FixedArray::OffsetOfElementAt(
1216 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1219 // Compute the target address = code_obj + header_size + osr_offset
1220 // <entry_addr> = <code_obj> + #header_size + <osr_offset>
1221 __ daddu(v0, v0, a1);
1222 __ daddiu(ra, v0, Code::kHeaderSize - kHeapObjectTag);
1224 // And "return" to the OSR entry point of the function.
1229 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1230 // We check the stack limit as indicator that recompilation might be done.
1232 __ LoadRoot(at, Heap::kStackLimitRootIndex);
1233 __ Branch(&ok, hs, sp, Operand(at));
1235 FrameScope scope(masm, StackFrame::INTERNAL);
1236 __ CallRuntime(Runtime::kStackGuard, 0);
1238 __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
1239 RelocInfo::CODE_TARGET);
1246 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1247 // 1. Make sure we have at least one argument.
1248 // a0: actual number of arguments
1250 __ Branch(&done, ne, a0, Operand(zero_reg));
1251 __ LoadRoot(a6, Heap::kUndefinedValueRootIndex);
1253 __ Daddu(a0, a0, Operand(1));
1257 // 2. Get the function to call (passed as receiver) from the stack, check
1258 // if it is a function.
1259 // a0: actual number of arguments
1260 Label slow, non_function;
1261 __ dsll(at, a0, kPointerSizeLog2);
1262 __ daddu(at, sp, at);
1263 __ ld(a1, MemOperand(at));
1264 __ JumpIfSmi(a1, &non_function);
1265 __ GetObjectType(a1, a2, a2);
1266 __ Branch(&slow, ne, a2, Operand(JS_FUNCTION_TYPE));
1268 // 3a. Patch the first argument if necessary when calling a function.
1269 // a0: actual number of arguments
1271 Label shift_arguments;
1272 __ li(a4, Operand(0, RelocInfo::NONE32)); // Indicate regular JS_FUNCTION.
1273 { Label convert_to_object, use_global_proxy, patch_receiver;
1274 // Change context eagerly in case we need the global receiver.
1275 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
1277 // Do not transform the receiver for strict mode functions.
1278 __ ld(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1279 __ lbu(a3, FieldMemOperand(a2, SharedFunctionInfo::kStrictModeByteOffset));
1280 __ And(a7, a3, Operand(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
1281 __ Branch(&shift_arguments, ne, a7, Operand(zero_reg));
1283 // Do not transform the receiver for native (Compilerhints already in a3).
1284 __ lbu(a3, FieldMemOperand(a2, SharedFunctionInfo::kNativeByteOffset));
1285 __ And(a7, a3, Operand(1 << SharedFunctionInfo::kNativeBitWithinByte));
1286 __ Branch(&shift_arguments, ne, a7, Operand(zero_reg));
1288 // Compute the receiver in sloppy mode.
1289 // Load first argument in a2. a2 = -kPointerSize(sp + n_args << 2).
1290 __ dsll(at, a0, kPointerSizeLog2);
1291 __ daddu(a2, sp, at);
1292 __ ld(a2, MemOperand(a2, -kPointerSize));
1293 // a0: actual number of arguments
1295 // a2: first argument
1296 __ JumpIfSmi(a2, &convert_to_object, a6);
1298 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
1299 __ Branch(&use_global_proxy, eq, a2, Operand(a3));
1300 __ LoadRoot(a3, Heap::kNullValueRootIndex);
1301 __ Branch(&use_global_proxy, eq, a2, Operand(a3));
1303 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1304 __ GetObjectType(a2, a3, a3);
1305 __ Branch(&shift_arguments, ge, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
1307 __ bind(&convert_to_object);
1308 // Enter an internal frame in order to preserve argument count.
1310 FrameScope scope(masm, StackFrame::INTERNAL);
1314 ToObjectStub stub(masm->isolate());
1320 // Leave internal frame.
1322 // Restore the function to a1, and the flag to a4.
1323 __ dsll(at, a0, kPointerSizeLog2);
1324 __ daddu(at, sp, at);
1325 __ ld(a1, MemOperand(at));
1326 __ Branch(USE_DELAY_SLOT, &patch_receiver);
1327 __ li(a4, Operand(0, RelocInfo::NONE32));
1329 __ bind(&use_global_proxy);
1330 __ ld(a2, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1331 __ ld(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
1333 __ bind(&patch_receiver);
1334 __ dsll(at, a0, kPointerSizeLog2);
1335 __ daddu(a3, sp, at);
1336 __ sd(a2, MemOperand(a3, -kPointerSize));
1338 __ Branch(&shift_arguments);
1341 // 3b. Check for function proxy.
1343 __ li(a4, Operand(1, RelocInfo::NONE32)); // Indicate function proxy.
1344 __ Branch(&shift_arguments, eq, a2, Operand(JS_FUNCTION_PROXY_TYPE));
1346 __ bind(&non_function);
1347 __ li(a4, Operand(2, RelocInfo::NONE32)); // Indicate non-function.
1349 // 3c. Patch the first argument when calling a non-function. The
1350 // CALL_NON_FUNCTION builtin expects the non-function callee as
1351 // receiver, so overwrite the first argument which will ultimately
1352 // become the receiver.
1353 // a0: actual number of arguments
1355 // a4: call type (0: JS function, 1: function proxy, 2: non-function)
1356 __ dsll(at, a0, kPointerSizeLog2);
1357 __ daddu(a2, sp, at);
1358 __ sd(a1, MemOperand(a2, -kPointerSize));
1360 // 4. Shift arguments and return address one slot down on the stack
1361 // (overwriting the original receiver). Adjust argument count to make
1362 // the original first argument the new receiver.
1363 // a0: actual number of arguments
1365 // a4: call type (0: JS function, 1: function proxy, 2: non-function)
1366 __ bind(&shift_arguments);
1368 // Calculate the copy start address (destination). Copy end address is sp.
1369 __ dsll(at, a0, kPointerSizeLog2);
1370 __ daddu(a2, sp, at);
1373 __ ld(at, MemOperand(a2, -kPointerSize));
1374 __ sd(at, MemOperand(a2));
1375 __ Dsubu(a2, a2, Operand(kPointerSize));
1376 __ Branch(&loop, ne, a2, Operand(sp));
1377 // Adjust the actual number of arguments and remove the top element
1378 // (which is a copy of the last argument).
1379 __ Dsubu(a0, a0, Operand(1));
1383 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1384 // or a function proxy via CALL_FUNCTION_PROXY.
1385 // a0: actual number of arguments
1387 // a4: call type (0: JS function, 1: function proxy, 2: non-function)
1388 { Label function, non_proxy;
1389 __ Branch(&function, eq, a4, Operand(zero_reg));
1390 // Expected number of arguments is 0 for CALL_NON_FUNCTION.
1391 __ mov(a2, zero_reg);
1392 __ Branch(&non_proxy, ne, a4, Operand(1));
1394 __ push(a1); // Re-add proxy object as additional argument.
1395 __ Daddu(a0, a0, Operand(1));
1396 __ GetBuiltinFunction(a1, Builtins::CALL_FUNCTION_PROXY);
1397 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1398 RelocInfo::CODE_TARGET);
1400 __ bind(&non_proxy);
1401 __ GetBuiltinFunction(a1, Builtins::CALL_NON_FUNCTION);
1402 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1403 RelocInfo::CODE_TARGET);
1407 // 5b. Get the code to call from the function and check that the number of
1408 // expected arguments matches what we're providing. If so, jump
1409 // (tail-call) to the code in register edx without checking arguments.
1410 // a0: actual number of arguments
1412 __ ld(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1413 // The argument count is stored as int32_t on 64-bit platforms.
1414 // TODO(plind): Smi on 32-bit platforms.
1416 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
1417 // Check formal and actual parameter counts.
1418 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1419 RelocInfo::CODE_TARGET, ne, a2, Operand(a0));
1421 __ ld(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
1422 ParameterCount expected(0);
1423 __ InvokeCode(a3, expected, expected, JUMP_FUNCTION, NullCallWrapper());
1427 static void Generate_PushAppliedArguments(MacroAssembler* masm,
1428 const int argumentsOffset,
1429 const int indexOffset,
1430 const int limitOffset) {
1432 Register receiver = LoadDescriptor::ReceiverRegister();
1433 Register key = LoadDescriptor::NameRegister();
1434 Register slot = LoadDescriptor::SlotRegister();
1435 Register vector = LoadWithVectorDescriptor::VectorRegister();
1437 __ ld(key, MemOperand(fp, indexOffset));
1440 // Load the current argument from the arguments array.
1442 __ ld(receiver, MemOperand(fp, argumentsOffset));
1444 // Use inline caching to speed up access to arguments.
1445 Code::Kind kinds[] = {Code::KEYED_LOAD_IC};
1446 FeedbackVectorSpec spec(0, 1, kinds);
1447 Handle<TypeFeedbackVector> feedback_vector =
1448 masm->isolate()->factory()->NewTypeFeedbackVector(&spec);
1449 int index = feedback_vector->GetIndex(FeedbackVectorICSlot(0));
1450 __ li(slot, Operand(Smi::FromInt(index)));
1451 __ li(vector, feedback_vector);
1453 KeyedLoadICStub(masm->isolate(), LoadICState(kNoExtraICState)).GetCode();
1454 __ Call(ic, RelocInfo::CODE_TARGET);
1458 // Use inline caching to access the arguments.
1459 __ ld(key, MemOperand(fp, indexOffset));
1460 __ Daddu(key, key, Operand(Smi::FromInt(1)));
1461 __ sd(key, MemOperand(fp, indexOffset));
1463 // Test if the copy loop has finished copying all the elements from the
1464 // arguments object.
1466 __ ld(a1, MemOperand(fp, limitOffset));
1467 __ Branch(&loop, ne, key, Operand(a1));
1469 // On exit, the pushed arguments count is in a0, untagged
1475 // Used by FunctionApply and ReflectApply
1476 static void Generate_ApplyHelper(MacroAssembler* masm, bool targetIsArgument) {
1477 const int kFormalParameters = targetIsArgument ? 3 : 2;
1478 const int kStackSize = kFormalParameters + 1;
1481 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1482 const int kArgumentsOffset = kFPOnStackSize + kPCOnStackSize;
1483 const int kReceiverOffset = kArgumentsOffset + kPointerSize;
1484 const int kFunctionOffset = kReceiverOffset + kPointerSize;
1486 __ ld(a0, MemOperand(fp, kFunctionOffset)); // Get the function.
1488 __ ld(a0, MemOperand(fp, kArgumentsOffset)); // Get the args array.
1491 // Returns (in v0) number of arguments to copy to stack as Smi.
1492 if (targetIsArgument) {
1493 __ InvokeBuiltin(Builtins::REFLECT_APPLY_PREPARE, CALL_FUNCTION);
1495 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1498 // Returns the result in v0.
1499 Generate_CheckStackOverflow(masm, kFunctionOffset, v0, kArgcIsSmiTagged);
1501 // Push current limit and index.
1502 const int kIndexOffset =
1503 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1504 const int kLimitOffset =
1505 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1506 __ mov(a1, zero_reg);
1507 __ Push(v0, a1); // Limit and initial index.
1509 // Get the receiver.
1510 __ ld(a0, MemOperand(fp, kReceiverOffset));
1512 // Check that the function is a JS function (otherwise it must be a proxy).
1513 Label push_receiver;
1514 __ ld(a1, MemOperand(fp, kFunctionOffset));
1515 __ GetObjectType(a1, a2, a2);
1516 __ Branch(&push_receiver, ne, a2, Operand(JS_FUNCTION_TYPE));
1518 // Change context eagerly to get the right global object if necessary.
1519 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
1520 // Load the shared function info while the function is still in a1.
1521 __ ld(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1523 // Compute the receiver.
1524 // Do not transform the receiver for strict mode functions.
1525 Label call_to_object, use_global_proxy;
1526 __ lbu(a7, FieldMemOperand(a2, SharedFunctionInfo::kStrictModeByteOffset));
1527 __ And(a7, a7, Operand(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
1528 __ Branch(&push_receiver, ne, a7, Operand(zero_reg));
1530 // Do not transform the receiver for native (Compilerhints already in a2).
1531 __ lbu(a7, FieldMemOperand(a2, SharedFunctionInfo::kNativeByteOffset));
1532 __ And(a7, a7, Operand(1 << SharedFunctionInfo::kNativeBitWithinByte));
1533 __ Branch(&push_receiver, ne, a7, Operand(zero_reg));
1535 // Compute the receiver in sloppy mode.
1536 __ JumpIfSmi(a0, &call_to_object);
1537 __ LoadRoot(a1, Heap::kNullValueRootIndex);
1538 __ Branch(&use_global_proxy, eq, a0, Operand(a1));
1539 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1540 __ Branch(&use_global_proxy, eq, a0, Operand(a2));
1542 // Check if the receiver is already a JavaScript object.
1544 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1545 __ GetObjectType(a0, a1, a1);
1546 __ Branch(&push_receiver, ge, a1, Operand(FIRST_SPEC_OBJECT_TYPE));
1548 // Convert the receiver to a regular object.
1550 __ bind(&call_to_object);
1551 ToObjectStub stub(masm->isolate());
1553 __ mov(a0, v0); // Put object in a0 to match other paths to push_receiver.
1554 __ Branch(&push_receiver);
1556 __ bind(&use_global_proxy);
1557 __ ld(a0, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1558 __ ld(a0, FieldMemOperand(a0, GlobalObject::kGlobalProxyOffset));
1560 // Push the receiver.
1562 __ bind(&push_receiver);
1565 // Copy all arguments from the array to the stack.
1566 Generate_PushAppliedArguments(
1567 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1569 // Call the function.
1571 ParameterCount actual(a0);
1572 __ ld(a1, MemOperand(fp, kFunctionOffset));
1573 __ GetObjectType(a1, a2, a2);
1574 __ Branch(&call_proxy, ne, a2, Operand(JS_FUNCTION_TYPE));
1576 __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
1578 frame_scope.GenerateLeaveFrame();
1579 __ Ret(USE_DELAY_SLOT);
1580 __ Daddu(sp, sp, Operand(kStackSize * kPointerSize)); // In delay slot.
1582 // Call the function proxy.
1583 __ bind(&call_proxy);
1584 __ push(a1); // Add function proxy as last argument.
1585 __ Daddu(a0, a0, Operand(1));
1586 __ li(a2, Operand(0, RelocInfo::NONE32));
1587 __ GetBuiltinFunction(a1, Builtins::CALL_FUNCTION_PROXY);
1588 __ Call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1589 RelocInfo::CODE_TARGET);
1590 // Tear down the internal frame and remove function, receiver and args.
1593 __ Ret(USE_DELAY_SLOT);
1594 __ Daddu(sp, sp, Operand(kStackSize * kPointerSize)); // In delay slot.
1598 static void Generate_ConstructHelper(MacroAssembler* masm) {
1599 const int kFormalParameters = 3;
1600 const int kStackSize = kFormalParameters + 1;
1603 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1604 const int kNewTargetOffset = kFPOnStackSize + kPCOnStackSize;
1605 const int kArgumentsOffset = kNewTargetOffset + kPointerSize;
1606 const int kFunctionOffset = kArgumentsOffset + kPointerSize;
1608 // If newTarget is not supplied, set it to constructor
1609 Label validate_arguments;
1610 __ ld(a0, MemOperand(fp, kNewTargetOffset));
1611 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1612 __ Branch(&validate_arguments, ne, a0, Operand(at));
1613 __ ld(a0, MemOperand(fp, kFunctionOffset));
1614 __ sd(a0, MemOperand(fp, kNewTargetOffset));
1616 // Validate arguments
1617 __ bind(&validate_arguments);
1618 __ ld(a0, MemOperand(fp, kFunctionOffset)); // get the function
1620 __ ld(a0, MemOperand(fp, kArgumentsOffset)); // get the args array
1622 __ ld(a0, MemOperand(fp, kNewTargetOffset)); // get the new.target
1624 // Returns argument count in v0.
1625 __ InvokeBuiltin(Builtins::REFLECT_CONSTRUCT_PREPARE, CALL_FUNCTION);
1627 // Returns result in v0.
1628 Generate_CheckStackOverflow(masm, kFunctionOffset, v0, kArgcIsSmiTagged);
1630 // Push current limit and index.
1631 const int kIndexOffset =
1632 StandardFrameConstants::kExpressionsOffset - (2 * kPointerSize);
1633 const int kLimitOffset =
1634 StandardFrameConstants::kExpressionsOffset - (1 * kPointerSize);
1635 __ push(v0); // limit
1636 __ mov(a1, zero_reg); // initial index
1638 // Push the constructor function as callee.
1639 __ ld(a0, MemOperand(fp, kFunctionOffset));
1642 // Copy all arguments from the array to the stack.
1643 Generate_PushAppliedArguments(
1644 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1646 // Use undefined feedback vector
1647 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1648 __ ld(a1, MemOperand(fp, kFunctionOffset));
1649 __ ld(a4, MemOperand(fp, kNewTargetOffset));
1651 // Call the function.
1652 CallConstructStub stub(masm->isolate(), SUPER_CONSTRUCTOR_CALL);
1653 __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
1655 // Leave internal frame.
1658 __ Daddu(sp, sp, Operand(kStackSize * kPointerSize)); // In delay slot.
1662 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1663 Generate_ApplyHelper(masm, false);
1667 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1668 Generate_ApplyHelper(masm, true);
1672 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1673 Generate_ConstructHelper(masm);
1677 static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1678 Label* stack_overflow) {
1679 // ----------- S t a t e -------------
1680 // -- a0 : actual number of arguments
1681 // -- a1 : function (passed through to callee)
1682 // -- a2 : expected number of arguments
1683 // -----------------------------------
1684 // Check the stack for overflow. We are not trying to catch
1685 // interruptions (e.g. debug break and preemption) here, so the "real stack
1686 // limit" is checked.
1687 __ LoadRoot(a5, Heap::kRealStackLimitRootIndex);
1688 // Make a5 the space we have left. The stack might already be overflowed
1689 // here which will cause a5 to become negative.
1690 __ dsubu(a5, sp, a5);
1691 // Check if the arguments will overflow the stack.
1692 __ dsll(at, a2, kPointerSizeLog2);
1693 // Signed comparison.
1694 __ Branch(stack_overflow, le, a5, Operand(at));
1698 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1699 // __ sll(a0, a0, kSmiTagSize);
1700 __ dsll32(a0, a0, 0);
1701 __ li(a4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1702 __ MultiPush(a0.bit() | a1.bit() | a4.bit() | fp.bit() | ra.bit());
1704 Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
1708 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1709 // ----------- S t a t e -------------
1710 // -- v0 : result being passed through
1711 // -----------------------------------
1712 // Get the number of arguments passed (as a smi), tear down the frame and
1713 // then tear down the parameters.
1714 __ ld(a1, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1717 __ MultiPop(fp.bit() | ra.bit());
1718 __ SmiScale(a4, a1, kPointerSizeLog2);
1719 __ Daddu(sp, sp, a4);
1720 // Adjust for the receiver.
1721 __ Daddu(sp, sp, Operand(kPointerSize));
1725 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1726 // State setup as expected by MacroAssembler::InvokePrologue.
1727 // ----------- S t a t e -------------
1728 // -- a0: actual arguments count
1729 // -- a1: function (passed through to callee)
1730 // -- a2: expected arguments count
1731 // -----------------------------------
1733 Label stack_overflow;
1734 ArgumentAdaptorStackCheck(masm, &stack_overflow);
1735 Label invoke, dont_adapt_arguments;
1737 Label enough, too_few;
1738 __ ld(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
1739 __ Branch(&dont_adapt_arguments, eq,
1740 a2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1741 // We use Uless as the number of argument should always be greater than 0.
1742 __ Branch(&too_few, Uless, a0, Operand(a2));
1744 { // Enough parameters: actual >= expected.
1745 // a0: actual number of arguments as a smi
1747 // a2: expected number of arguments
1748 // a3: code entry to call
1750 EnterArgumentsAdaptorFrame(masm);
1752 // Calculate copy start address into a0 and copy end address into a2.
1753 __ SmiScale(a0, a0, kPointerSizeLog2);
1754 __ Daddu(a0, fp, a0);
1755 // Adjust for return address and receiver.
1756 __ Daddu(a0, a0, Operand(2 * kPointerSize));
1757 // Compute copy end address.
1758 __ dsll(a2, a2, kPointerSizeLog2);
1759 __ dsubu(a2, a0, a2);
1761 // Copy the arguments (including the receiver) to the new stack frame.
1762 // a0: copy start address
1764 // a2: copy end address
1765 // a3: code entry to call
1769 __ ld(a4, MemOperand(a0));
1771 __ Branch(USE_DELAY_SLOT, ©, ne, a0, Operand(a2));
1772 __ daddiu(a0, a0, -kPointerSize); // In delay slot.
1777 { // Too few parameters: Actual < expected.
1780 // If the function is strong we need to throw an error.
1781 Label no_strong_error;
1782 __ ld(a4, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1783 __ lbu(a5, FieldMemOperand(a4, SharedFunctionInfo::kStrongModeByteOffset));
1784 __ And(a5, a5, Operand(1 << SharedFunctionInfo::kStrongModeBitWithinByte));
1785 __ Branch(&no_strong_error, eq, a5, Operand(zero_reg));
1787 // What we really care about is the required number of arguments.
1788 DCHECK_EQ(kPointerSize, kInt64Size);
1789 __ lw(a5, FieldMemOperand(a4, SharedFunctionInfo::kLengthOffset));
1791 __ Branch(&no_strong_error, ge, a0, Operand(a5));
1794 FrameScope frame(masm, StackFrame::MANUAL);
1795 EnterArgumentsAdaptorFrame(masm);
1796 __ CallRuntime(Runtime::kThrowStrongModeTooFewArguments, 0);
1799 __ bind(&no_strong_error);
1800 EnterArgumentsAdaptorFrame(masm);
1802 // Calculate copy start address into a0 and copy end address is fp.
1803 // a0: actual number of arguments as a smi
1805 // a2: expected number of arguments
1806 // a3: code entry to call
1807 __ SmiScale(a0, a0, kPointerSizeLog2);
1808 __ Daddu(a0, fp, a0);
1809 // Adjust for return address and receiver.
1810 __ Daddu(a0, a0, Operand(2 * kPointerSize));
1811 // Compute copy end address. Also adjust for return address.
1812 __ Daddu(a7, fp, kPointerSize);
1814 // Copy the arguments (including the receiver) to the new stack frame.
1815 // a0: copy start address
1817 // a2: expected number of arguments
1818 // a3: code entry to call
1819 // a7: copy end address
1822 __ ld(a4, MemOperand(a0)); // Adjusted above for return addr and receiver.
1823 __ Dsubu(sp, sp, kPointerSize);
1824 __ Dsubu(a0, a0, kPointerSize);
1825 __ Branch(USE_DELAY_SLOT, ©, ne, a0, Operand(a7));
1826 __ sd(a4, MemOperand(sp)); // In the delay slot.
1828 // Fill the remaining expected arguments with undefined.
1830 // a2: expected number of arguments
1831 // a3: code entry to call
1832 __ LoadRoot(a4, Heap::kUndefinedValueRootIndex);
1833 __ dsll(a6, a2, kPointerSizeLog2);
1834 __ Dsubu(a2, fp, Operand(a6));
1835 // Adjust for frame.
1836 __ Dsubu(a2, a2, Operand(StandardFrameConstants::kFixedFrameSizeFromFp +
1841 __ Dsubu(sp, sp, kPointerSize);
1842 __ Branch(USE_DELAY_SLOT, &fill, ne, sp, Operand(a2));
1843 __ sd(a4, MemOperand(sp));
1846 // Call the entry point.
1851 // Store offset of return address for deoptimizer.
1852 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1854 // Exit frame and return.
1855 LeaveArgumentsAdaptorFrame(masm);
1859 // -------------------------------------------
1860 // Don't adapt arguments.
1861 // -------------------------------------------
1862 __ bind(&dont_adapt_arguments);
1865 __ bind(&stack_overflow);
1867 FrameScope frame(masm, StackFrame::MANUAL);
1868 EnterArgumentsAdaptorFrame(masm);
1869 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1877 } // namespace internal
1880 #endif // V8_TARGET_ARCH_MIPS64