deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / mips / builtins-mips.cc
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.
4
5
6
7 #include "src/v8.h"
8
9 #if V8_TARGET_ARCH_MIPS
10
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"
16
17
18 namespace v8 {
19 namespace internal {
20
21
22 #define __ ACCESS_MASM(masm)
23
24
25 void Builtins::Generate_Adaptor(MacroAssembler* masm,
26                                 CFunctionId id,
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)
32   //  -- cp                 : context
33   //  -- sp[0]              : last argument
34   //  -- ...
35   //  -- sp[4 * (argc - 1)] : first argument
36   //  -- sp[4 * agrc]       : receiver
37   // -----------------------------------
38
39   // Insert extra arguments.
40   int num_extra_args = 0;
41   if (extra_args == NEEDS_CALLED_FUNCTION) {
42     num_extra_args = 1;
43     __ push(a1);
44   } else {
45     DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
46   }
47
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()));
52 }
53
54
55 // Load the built-in InternalArray function from the current context.
56 static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
57                                               Register result) {
58   // Load the native context.
59
60   __ lw(result,
61         MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
62   __ lw(result,
63         FieldMemOperand(result, GlobalObject::kNativeContextOffset));
64   // Load the InternalArray function from the native context.
65   __ lw(result,
66          MemOperand(result,
67                     Context::SlotOffset(
68                         Context::INTERNAL_ARRAY_FUNCTION_INDEX)));
69 }
70
71
72 // Load the built-in Array function from the current context.
73 static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
74   // Load the native context.
75
76   __ lw(result,
77         MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
78   __ lw(result,
79         FieldMemOperand(result, GlobalObject::kNativeContextOffset));
80   // Load the Array function from the native context.
81   __ lw(result,
82         MemOperand(result,
83                    Context::SlotOffset(Context::ARRAY_FUNCTION_INDEX)));
84 }
85
86
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;
94
95   // Get the InternalArray function.
96   GenerateLoadInternalArrayFunction(masm, a1);
97
98   if (FLAG_debug_code) {
99     // Initial map for the builtin InternalArray functions should be maps.
100     __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
101     __ SmiTst(a2, t0);
102     __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction,
103               t0, Operand(zero_reg));
104     __ GetObjectType(a2, a3, t0);
105     __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction,
106               t0, Operand(MAP_TYPE));
107   }
108
109   // Run the native code for the InternalArray function called as a normal
110   // function.
111   // Tail call a stub.
112   InternalArrayConstructorStub stub(masm->isolate());
113   __ TailCallStub(&stub);
114 }
115
116
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;
124
125   // Get the Array function.
126   GenerateLoadArrayFunction(masm, a1);
127
128   if (FLAG_debug_code) {
129     // Initial map for the builtin Array functions should be maps.
130     __ lw(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
131     __ SmiTst(a2, t0);
132     __ Assert(ne, kUnexpectedInitialMapForArrayFunction1,
133               t0, Operand(zero_reg));
134     __ GetObjectType(a2, a3, t0);
135     __ Assert(eq, kUnexpectedInitialMapForArrayFunction2,
136               t0, Operand(MAP_TYPE));
137   }
138
139   // Run the native code for the Array function called as a normal function.
140   // Tail call a stub.
141   __ mov(a3, a1);
142   __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
143   ArrayConstructorStub stub(masm->isolate());
144   __ TailCallStub(&stub);
145 }
146
147
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);
158
159   Register function = a1;
160   if (FLAG_debug_code) {
161     __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, a2);
162     __ Assert(eq, kUnexpectedStringFunction, function, Operand(a2));
163   }
164
165   // Load the first arguments in a0 and get rid of the rest.
166   Label no_arguments;
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);
171   __ Addu(sp, a0, sp);
172   __ lw(a0, MemOperand(sp));
173   // sp now point to args[0], drop args[0] + receiver.
174   __ Drop(2);
175
176   Register argument = a2;
177   Label not_cached, argument_is_string;
178   __ LookupNumberStringCache(a0,        // Input.
179                              argument,  // Result.
180                              a3,        // Scratch.
181                              t0,        // Scratch.
182                              t1,        // Scratch.
183                              &not_cached);
184   __ IncrementCounter(counters->string_ctor_cached_number(), 1, a3, t0);
185   __ bind(&argument_is_string);
186
187   // ----------- S t a t e -------------
188   //  -- a2     : argument converted to string
189   //  -- a1     : constructor function
190   //  -- ra     : return address
191   // -----------------------------------
192
193   Label gc_required;
194   __ Allocate(JSValue::kSize,
195               v0,  // Result.
196               a3,  // Scratch.
197               t0,  // Scratch.
198               &gc_required,
199               TAG_OBJECT);
200
201   // Initialising the String Object.
202   Register map = a3;
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));
211   }
212   __ sw(map, FieldMemOperand(v0, HeapObject::kMapOffset));
213
214   __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
215   __ sw(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
216   __ sw(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
217
218   __ sw(argument, FieldMemOperand(v0, JSValue::kValueOffset));
219
220   // Ensure the object is fully initialized.
221   STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
222
223   __ Ret();
224
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(&not_cached);
229   __ JumpIfSmi(a0, &convert_argument);
230
231   // Is it a String?
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);
240
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);
245   {
246     FrameScope scope(masm, StackFrame::INTERNAL);
247     __ push(a0);
248     __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
249   }
250   __ pop(function);
251   __ mov(argument, v0);
252   __ Branch(&argument_is_string);
253
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);
258   __ Drop(1);
259   __ Branch(&argument_is_string);
260
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);
265   {
266     FrameScope scope(masm, StackFrame::INTERNAL);
267     __ push(argument);
268     __ CallRuntime(Runtime::kNewStringWrapper, 1);
269   }
270   __ Ret();
271 }
272
273
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.
279   __ Push(a1, a1);
280
281   __ CallRuntime(function_id, 1);
282   // Restore call kind information and receiver.
283   __ Pop(a1);
284 }
285
286
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));
291   __ Jump(at);
292 }
293
294
295 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
296   __ Addu(at, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
297   __ Jump(at);
298 }
299
300
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.
307   Label ok;
308   __ LoadRoot(t0, Heap::kStackLimitRootIndex);
309   __ Branch(&ok, hs, sp, Operand(t0));
310
311   CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
312   GenerateTailCallToReturnedCode(masm);
313
314   __ bind(&ok);
315   GenerateTailCallToSharedCode(masm);
316 }
317
318
319 static void Generate_Runtime_NewObject(MacroAssembler* masm,
320                                        bool create_memento,
321                                        Register original_constructor,
322                                        Label* count_incremented,
323                                        Label* allocated) {
324   if (create_memento) {
325     // Get the cell or allocation site.
326     __ lw(a2, MemOperand(sp, 2 * kPointerSize));
327     __ push(a2);
328   }
329
330   __ push(a1);                    // argument for Runtime_NewObject
331   __ push(original_constructor);  // original constructor
332   if (create_memento) {
333     __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 3);
334   } else {
335     __ CallRuntime(Runtime::kNewObject, 2);
336   }
337   __ mov(t4, v0);
338
339   // Runtime_NewObjectWithAllocationSite increments allocation count.
340   // Skip the increment.
341   if (create_memento) {
342     __ jmp(count_incremented);
343   } else {
344     __ jmp(allocated);
345   }
346 }
347
348
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   // -----------------------------------
360
361   // Should never create mementos for api functions.
362   DCHECK(!is_api_function || !create_memento);
363
364   Isolate* isolate = masm->isolate();
365
366   // ----------- S t a t e -------------
367   //  -- a0     : number of arguments
368   //  -- a1     : constructor function
369   //  -- ra     : return address
370   //  -- sp[...]: constructor arguments
371   // -----------------------------------
372
373   // Enter a construct frame.
374   {
375     FrameScope scope(masm, StackFrame::CONSTRUCT);
376
377     if (create_memento) {
378       __ AssertUndefinedOrAllocationSite(a2, t0);
379       __ push(a2);
380     }
381
382     // Preserve the two incoming parameters on the stack.
383     __ sll(a0, a0, kSmiTagSize);  // Tag arguments count.
384     __ MultiPushReversed(a0.bit() | a1.bit());
385
386     Label rt_call, allocated, normal_new, count_incremented;
387     __ Branch(&normal_new, eq, a1, Operand(a3));
388
389     // Original constructor and function are different.
390     Generate_Runtime_NewObject(masm, create_memento, a3, &count_incremented,
391                                &allocated);
392     __ bind(&normal_new);
393
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));
403
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));
410
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
415       // a2: initial map
416       __ lbu(a3, FieldMemOperand(a2, Map::kInstanceTypeOffset));
417       __ Branch(&rt_call, eq, a3, Operand(JS_FUNCTION_TYPE));
418
419       if (!is_api_function) {
420         Label allocate;
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.
431
432         __ Push(a1, a2, a1);  // a1 = Constructor.
433         __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
434
435         __ Pop(a1, a2);
436         // Slack tracking counter is Map::kSlackTrackingCounterEnd after runtime
437         // call.
438         __ li(t2, Map::kSlackTrackingCounterEnd);
439
440         __ bind(&allocate);
441       }
442
443       // Now allocate the JSObject on the heap.
444       // a1: constructor function
445       // a2: initial map
446       __ lbu(a3, FieldMemOperand(a2, Map::kInstanceSizeOffset));
447       if (create_memento) {
448         __ Addu(a3, a3, Operand(AllocationMemento::kSize / kPointerSize));
449       }
450
451       __ Allocate(a3, t4, t5, t6, &rt_call, SIZE_IN_WORDS);
452
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
456       // a2: initial map
457       // a3: object size (not including memento if create_memento)
458       // t4: JSObject (not tagged)
459       __ LoadRoot(t6, Heap::kEmptyFixedArrayRootIndex);
460       __ mov(t5, t4);
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);
468
469       // Fill all the in-object properties with appropriate filler.
470       // a1: constructor function
471       // a2: initial map
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);
477
478       // Use t7 to hold undefined, which is used in several places below.
479       __ LoadRoot(t7, Heap::kUndefinedValueRootIndex);
480
481       if (!is_api_function) {
482         Label no_inobject_slack_tracking;
483
484         // Check if slack tracking is enabled.
485         __ Branch(&no_inobject_slack_tracking, lt, t2,
486                   Operand(Map::kSlackTrackingCounterEnd));
487
488         // Allocate object with a slack.
489         __ lbu(a0, FieldMemOperand(a2, Map::kPreAllocatedPropertyFieldsOffset));
490         __ sll(at, a0, kPointerSizeLog2);
491         __ addu(a0, t5, at);
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,
497               a0, Operand(t6));
498         }
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.
503
504         __ bind(&no_inobject_slack_tracking);
505       }
506
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);
512
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);
524       } else {
525         __ sll(at, a3, kPointerSizeLog2);
526         __ Addu(a0, t4, Operand(at));  // End of object.
527         __ InitializeFieldsWithFiller(t5, a0, t7);
528       }
529
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));
535
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
539       // t4: JSObject
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));
547       __ subu(a3, a3, t6);
548
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));
553
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.
556       // a1: constructor
557       // a3: number of elements in properties array
558       // t4: JSObject
559       // t5: start of next object
560       __ Addu(a0, a3, Operand(FixedArray::kHeaderSize / kPointerSize));
561       __ Allocate(
562           a0,
563           t5,
564           t6,
565           a2,
566           &undo_allocation,
567           static_cast<AllocationFlags>(RESULT_CONTAINS_TOP | SIZE_IN_WORDS));
568
569       // Initialize the FixedArray.
570       // a1: constructor
571       // a3: number of elements in properties array (untagged)
572       // t4: JSObject
573       // t5: start of next object
574       __ LoadRoot(t6, Heap::kFixedArrayMapRootIndex);
575       __ mov(a2, t5);
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));
580
581       DCHECK_EQ(0 * kPointerSize, JSObject::kMapOffset);
582       DCHECK_EQ(1 * kPointerSize, FixedArray::kLengthOffset);
583
584       // Initialize the fields to undefined.
585       // a1: constructor
586       // a2: First element of FixedArray (not tagged)
587       // a3: number of elements in properties array
588       // t4: JSObject
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);
593       { Label loop, entry;
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));
599         }
600         __ jmp(&entry);
601         __ bind(&loop);
602         __ sw(t7, MemOperand(a2));
603         __ addiu(a2, a2, kPointerSize);
604         __ bind(&entry);
605         __ Branch(&loop, less, a2, Operand(t6));
606       }
607
608       // Store the initialized FixedArray into the properties field of
609       // the JSObject.
610       // a1: constructor function
611       // t4: JSObject
612       // t5: FixedArray (not tagged)
613       __ Addu(t5, t5, Operand(kHeapObjectTag));  // Add the heap tag.
614       __ sw(t5, FieldMemOperand(t4, JSObject::kPropertiesOffset));
615
616       // Continue with JSObject being successfully allocated.
617       // a1: constructor function
618       // a4: JSObject
619       __ jmp(&allocated);
620
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);
627     }
628
629     // Allocate the new receiver object using the runtime call.
630     // a1: constructor function
631     __ bind(&rt_call);
632     Generate_Runtime_NewObject(masm, create_memento, a1, &count_incremented,
633                                &allocated);
634
635     // Receiver for constructor call allocated.
636     // t4: JSObject
637     __ bind(&allocated);
638
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);
651     }
652
653     __ Push(t4, t4);
654
655     // Reload the number of arguments from the stack.
656     // sp[0]: receiver
657     // sp[1]: receiver
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));
662
663     // Set up pointer to last argument.
664     __ Addu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
665
666     // Set up number of arguments for function call below.
667     __ srl(a0, a3, kSmiTagSize);
668
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)
674     // sp[0]: receiver
675     // sp[1]: receiver
676     // sp[2]: constructor function
677     // sp[3]: number of arguments (smi-tagged)
678     Label loop, entry;
679     __ jmp(&entry);
680     __ bind(&loop);
681     __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
682     __ Addu(t0, a2, Operand(t0));
683     __ lw(t1, MemOperand(t0));
684     __ push(t1);
685     __ bind(&entry);
686     __ Addu(a3, a3, Operand(-2));
687     __ Branch(&loop, greater_equal, a3, Operand(zero_reg));
688
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));
694       Handle<Code> code =
695           masm->isolate()->builtins()->HandleApiCallConstruct();
696       __ Call(code, RelocInfo::CODE_TARGET);
697     } else {
698       ParameterCount actual(a0);
699       __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
700     }
701
702     // Store offset of return address for deoptimizer.
703     if (!is_api_function) {
704       masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
705     }
706
707     // Restore context from the frame.
708     __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
709
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
712     // on page 74.
713     Label use_receiver, exit;
714
715     // If the result is a smi, it is *not* an object in the ECMA sense.
716     // v0: result
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);
721
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));
726
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));
731
732     // Remove receiver from the stack, remove caller arguments, and
733     // return.
734     __ bind(&exit);
735     // v0: result
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));
740
741     // Leave construct frame.
742   }
743
744   __ sll(t0, a1, kPointerSizeLog2 - 1);
745   __ Addu(sp, sp, t0);
746   __ Addu(sp, sp, kPointerSize);
747   __ IncrementCounter(isolate->counters()->constructed_objects(), 1, a1, a2);
748   __ Ret();
749 }
750
751
752 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
753   Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
754 }
755
756
757 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
758   Generate_JSConstructStubHelper(masm, true, false);
759 }
760
761
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   // -----------------------------------
771
772   // TODO(dslomov): support pretenuring
773   CHECK(!FLAG_pretenuring_call_new);
774
775   {
776     FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
777
778     __ mov(t0, a0);
779     __ SmiTag(t0);
780     __ push(t0);  // Smi-tagged arguments count.
781
782     // Push new.target.
783     __ push(a3);
784
785     // receiver is the hole.
786     __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
787     __ push(at);
788
789     // Set up pointer to last argument.
790     __ Addu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
791
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)
797     // sp[0]: receiver
798     // sp[1]: new.target
799     // sp[2]: number of arguments (smi-tagged)
800     Label loop, entry;
801     __ Branch(&entry);
802     __ bind(&loop);
803     __ sll(at, t0, kPointerSizeLog2 - 1);
804     __ Addu(at, a2, Operand(at));
805     __ lw(at, MemOperand(at));
806     __ push(at);
807     __ bind(&entry);
808     __ Subu(t0, t0, Operand(2));
809     __ Branch(&loop, ge, t0, Operand(zero_reg));
810
811     __ Addu(a0, a0, Operand(1));
812
813     // Handle step in.
814     Label skip_step_in;
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));
820
821     __ Push(a0, a1, a1);
822     __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
823     __ Pop(a0, a1);
824
825     __ bind(&skip_step_in);
826
827     // Call the function.
828     // a0: number of arguments
829     // a1: constructor function
830     ParameterCount actual(a0);
831     __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
832
833     // Restore context from the frame.
834     // v0: result
835     // sp[0]: number of arguments (smi-tagged)
836     __ lw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
837     __ lw(a1, MemOperand(sp, 0));
838
839     // Leave construct frame.
840   }
841
842   __ sll(at, a1, kPointerSizeLog2 - 1);
843   __ Addu(sp, sp, Operand(at));
844   __ Addu(sp, sp, Operand(kPointerSize));
845   __ Jump(ra);
846 }
847
848
849 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
850                                              bool is_construct) {
851   // Called from JSEntryStub::GenerateBody
852
853   // ----------- S t a t e -------------
854   //  -- a0: code entry
855   //  -- a1: function
856   //  -- a2: receiver_pointer
857   //  -- a3: argc
858   //  -- s0: argv
859   // -----------------------------------
860   ProfileEntryHookStub::MaybeCallEntryHook(masm);
861
862   // Clear the context before we push it when entering the JS frame.
863   __ mov(cp, zero_reg);
864
865   // Enter an internal frame.
866   {
867     FrameScope scope(masm, StackFrame::INTERNAL);
868
869     // Set up the context from the function argument.
870     __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
871
872     // Push the function and the receiver onto the stack.
873     __ Push(a1, a2);
874
875     // Copy arguments to the stack in a loop.
876     // a3: argc
877     // s0: argv, i.e. points to first arg
878     Label loop, entry;
879     __ sll(t0, a3, kPointerSizeLog2);
880     __ addu(t2, s0, t0);
881     __ b(&entry);
882     __ nop();   // Branch delay slot nop.
883     // t2 points past last arg.
884     __ bind(&loop);
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.
889     __ bind(&entry);
890     __ Branch(&loop, ne, s0, Operand(t2));
891
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);
895     __ mov(s1, t0);
896     __ mov(s2, t0);
897     __ mov(s3, t0);
898     __ mov(s4, t0);
899     __ mov(s5, t0);
900     // s6 holds the root address. Do not clobber.
901     // s7 is cp. Do not init.
902
903     // Invoke the code and pass argc as a0.
904     __ mov(a0, a3);
905     if (is_construct) {
906       // No type feedback cell is available
907       __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
908       CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
909       __ CallStub(&stub);
910     } else {
911       ParameterCount actual(a0);
912       __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
913     }
914
915     // Leave internal frame.
916   }
917
918   __ Jump(ra);
919 }
920
921
922 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
923   Generate_JSEntryTrampolineHelper(masm, false);
924 }
925
926
927 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
928   Generate_JSEntryTrampolineHelper(masm, true);
929 }
930
931
932 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
933   CallRuntimePassFunction(masm, Runtime::kCompileLazy);
934   GenerateTailCallToReturnedCode(masm);
935 }
936
937
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.
942   __ Push(a1, a1);
943   // Whether to compile in a background thread.
944   __ LoadRoot(
945       at, concurrent ? Heap::kTrueValueRootIndex : Heap::kFalseValueRootIndex);
946   __ push(at);
947
948   __ CallRuntime(Runtime::kCompileOptimized, 2);
949   // Restore receiver.
950   __ Pop(a1);
951 }
952
953
954 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
955   CallCompileOptimized(masm, false);
956   GenerateTailCallToReturnedCode(masm);
957 }
958
959
960 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
961   CallCompileOptimized(masm, true);
962   GenerateTailCallToReturnedCode(masm);
963 }
964
965
966
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.
973
974   // Set a0 to point to the head of the PlatformCodeAge sequence.
975   __ Subu(a0, a0,
976       Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
977
978   // The following registers must be saved and restored when calling through to
979   // the runtime:
980   //   a0 - contains return address (beginning of patch sequence)
981   //   a1 - isolate
982   RegList saved_regs =
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())));
988   __ CallCFunction(
989       ExternalReference::get_make_code_young_function(masm->isolate()), 2);
990   __ MultiPop(saved_regs);
991   __ Jump(a0);
992 }
993
994 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C)                 \
995 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking(  \
996     MacroAssembler* masm) {                                  \
997   GenerateMakeCodeYoungAgainCommon(masm);                    \
998 }                                                            \
999 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking(   \
1000     MacroAssembler* masm) {                                  \
1001   GenerateMakeCodeYoungAgainCommon(masm);                    \
1002 }
1003 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
1004 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
1005
1006
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
1011   // pointers.
1012
1013   // Set a0 to point to the head of the PlatformCodeAge sequence.
1014   __ Subu(a0, a0,
1015       Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
1016
1017   // The following registers must be saved and restored when calling through to
1018   // the runtime:
1019   //   a0 - contains return address (beginning of patch sequence)
1020   //   a1 - isolate
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())));
1027   __ CallCFunction(
1028       ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
1029       2);
1030   __ MultiPop(saved_regs);
1031
1032   // Perform prologue operations usually performed by the young code stub.
1033   __ Push(ra, fp, cp, a1);
1034   __ Addu(fp, sp, Operand(StandardFrameConstants::kFixedFrameSizeFromFp));
1035
1036   // Jump to point after the code-age stub.
1037   __ Addu(a0, a0, Operand(kNoCodeAgeSequenceLength));
1038   __ Jump(a0);
1039 }
1040
1041
1042 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
1043   GenerateMakeCodeYoungAgainCommon(masm);
1044 }
1045
1046
1047 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
1048                                              SaveFPRegsMode save_doubles) {
1049   {
1050     FrameScope scope(masm, StackFrame::INTERNAL);
1051
1052     // Preserve registers across notification, this is important for compiled
1053     // stubs that tail call the runtime on deopts passing their parameters in
1054     // registers.
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);
1059   }
1060
1061   __ Addu(sp, sp, Operand(kPointerSize));  // Ignore state
1062   __ Jump(ra);  // Jump to miss handler
1063 }
1064
1065
1066 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
1067   Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
1068 }
1069
1070
1071 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
1072   Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
1073 }
1074
1075
1076 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1077                                              Deoptimizer::BailoutType type) {
1078   {
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))));
1082     __ push(a0);
1083     __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
1084   }
1085
1086   // Get the full codegen state from the stack and untag it -> t2.
1087   __ lw(t2, MemOperand(sp, 0 * kPointerSize));
1088   __ SmiUntag(t2);
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.
1096
1097   __ bind(&with_tos_register);
1098   __ lw(v0, MemOperand(sp, 1 * kPointerSize));
1099   __ Branch(&unknown_state, ne, t2, Operand(FullCodeGenerator::TOS_REG));
1100
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.
1104
1105   __ bind(&unknown_state);
1106   __ stop("no cases left");
1107 }
1108
1109
1110 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1111   Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1112 }
1113
1114
1115 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
1116   Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1117 }
1118
1119
1120 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1121   Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1122 }
1123
1124
1125 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1126   // Lookup the function in the JavaScript frame.
1127   __ lw(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1128   {
1129     FrameScope scope(masm, StackFrame::INTERNAL);
1130     // Pass function as argument.
1131     __ push(a0);
1132     __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1133   }
1134
1135   // If the code object is null, just return to the unoptimized code.
1136   __ Ret(eq, v0, Operand(Smi::FromInt(0)));
1137
1138   // Load deoptimization data from the code object.
1139   // <deopt_data> = <code>[#deoptimization_data_offset]
1140   __ lw(a1, MemOperand(v0, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1141
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));
1146   __ SmiUntag(a1);
1147
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);
1152
1153   // And "return" to the OSR entry point of the function.
1154   __ Ret();
1155 }
1156
1157
1158 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1159   // We check the stack limit as indicator that recompilation might be done.
1160   Label ok;
1161   __ LoadRoot(at, Heap::kStackLimitRootIndex);
1162   __ Branch(&ok, hs, sp, Operand(at));
1163   {
1164     FrameScope scope(masm, StackFrame::INTERNAL);
1165     __ CallRuntime(Runtime::kStackGuard, 0);
1166   }
1167   __ Jump(masm->isolate()->builtins()->OnStackReplacement(),
1168           RelocInfo::CODE_TARGET);
1169
1170   __ bind(&ok);
1171   __ Ret();
1172 }
1173
1174
1175 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
1176   // 1. Make sure we have at least one argument.
1177   // a0: actual number of arguments
1178   { Label done;
1179     __ Branch(&done, ne, a0, Operand(zero_reg));
1180     __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
1181     __ push(t2);
1182     __ Addu(a0, a0, Operand(1));
1183     __ bind(&done);
1184   }
1185
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));
1196
1197   // 3a. Patch the first argument if necessary when calling a function.
1198   // a0: actual number of arguments
1199   // a1: function
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));
1205
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 +
1210                                  kSmiTagSize)));
1211     __ Branch(&shift_arguments, ne, t3, Operand(zero_reg));
1212
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));
1216
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
1223     // a1: function
1224     // a2: first argument
1225     __ JumpIfSmi(a2, &convert_to_object, t2);
1226
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));
1231
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));
1235
1236     __ bind(&convert_to_object);
1237     // Enter an internal frame in order to preserve argument count.
1238     {
1239       FrameScope scope(masm, StackFrame::INTERNAL);
1240       __ sll(a0, a0, kSmiTagSize);  // Smi tagged.
1241       __ Push(a0, a2);
1242       __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1243       __ mov(a2, v0);
1244
1245       __ pop(a0);
1246       __ sra(a0, a0, kSmiTagSize);  // Un-tag.
1247       // Leave internal frame.
1248     }
1249
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.
1256
1257     __ bind(&use_global_proxy);
1258     __ lw(a2, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1259     __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalProxyOffset));
1260
1261     __ bind(&patch_receiver);
1262     __ sll(at, a0, kPointerSizeLog2);
1263     __ addu(a3, sp, at);
1264     __ sw(a2, MemOperand(a3, -kPointerSize));
1265
1266     __ Branch(&shift_arguments);
1267   }
1268
1269   // 3b. Check for function proxy.
1270   __ bind(&slow);
1271   __ li(t0, Operand(1, RelocInfo::NONE32));  // Indicate function proxy.
1272   __ Branch(&shift_arguments, eq, a2, Operand(JS_FUNCTION_PROXY_TYPE));
1273
1274   __ bind(&non_function);
1275   __ li(t0, Operand(2, RelocInfo::NONE32));  // Indicate non-function.
1276
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
1282   // a1: function
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));
1287
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
1292   // a1: function
1293   // t0: call type (0: JS function, 1: function proxy, 2: non-function)
1294   __ bind(&shift_arguments);
1295   { Label loop;
1296     // Calculate the copy start address (destination). Copy end address is sp.
1297     __ sll(at, a0, kPointerSizeLog2);
1298     __ addu(a2, sp, at);
1299
1300     __ bind(&loop);
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));
1308     __ Pop();
1309   }
1310
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
1314   // a1: function
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));
1321
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);
1327
1328     __ bind(&non_proxy);
1329     __ GetBuiltinFunction(a1, Builtins::CALL_NON_FUNCTION);
1330     __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1331             RelocInfo::CODE_TARGET);
1332     __ bind(&function);
1333   }
1334
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
1339   // a1: function
1340   __ lw(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1341   __ lw(a2,
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));
1347
1348   __ lw(a3, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
1349   ParameterCount expected(0);
1350   __ InvokeCode(a3, expected, expected, JUMP_FUNCTION, NullCallWrapper());
1351 }
1352
1353
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.
1359   Label okay;
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));
1368
1369   // Out of stack space.
1370   __ lw(a1, MemOperand(fp, calleeOffset));
1371   __ Push(a1, v0);
1372   __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1373
1374   __ bind(&okay);
1375 }
1376
1377
1378 static void Generate_PushAppliedArguments(MacroAssembler* masm,
1379                                           const int argumentsOffset,
1380                                           const int indexOffset,
1381                                           const int limitOffset) {
1382   Label entry, loop;
1383   __ lw(a0, MemOperand(fp, indexOffset));
1384   __ Branch(&entry);
1385
1386   // Load the current argument from the arguments array and push it to the
1387   // stack.
1388   // a0: current argument index
1389   __ bind(&loop);
1390   __ lw(a1, MemOperand(fp, argumentsOffset));
1391   __ Push(a1, a0);
1392
1393   // Call the runtime to access the property in the arguments array.
1394   __ CallRuntime(Runtime::kGetProperty, 2);
1395   __ push(v0);
1396
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));
1401
1402   // Test if the copy loop has finished copying all the elements from the
1403   // arguments object.
1404   __ bind(&entry);
1405   __ lw(a1, MemOperand(fp, limitOffset));
1406   __ Branch(&loop, ne, a0, Operand(a1));
1407
1408   // On exit, the pushed arguments count is in a0, untagged
1409   __ SmiUntag(a0);
1410 }
1411
1412
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;
1417
1418   {
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;
1423
1424     __ lw(a0, MemOperand(fp, kFunctionOffset));  // Get the function.
1425     __ push(a0);
1426     __ lw(a0, MemOperand(fp, kArgumentsOffset));  // Get the args array.
1427     __ push(a0);
1428     // Returns (in v0) number of arguments to copy to stack as Smi.
1429     if (targetIsArgument) {
1430       __ InvokeBuiltin(Builtins::REFLECT_APPLY_PREPARE, CALL_FUNCTION);
1431     } else {
1432       __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1433     }
1434
1435     // Returns the result in v0.
1436     Generate_CheckStackOverflow(masm, kFunctionOffset);
1437
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.
1445
1446     // Get the receiver.
1447     __ lw(a0, MemOperand(fp, kReceiverOffset));
1448
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));
1454
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));
1459
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 +
1465                                  kSmiTagSize)));
1466     __ Branch(&push_receiver, ne, t3, Operand(zero_reg));
1467
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));
1471
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));
1478
1479     // Check if the receiver is already a JavaScript object.
1480     // a0: receiver
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));
1484
1485     // Convert the receiver to a regular object.
1486     // a0: receiver
1487     __ bind(&call_to_object);
1488     __ push(a0);
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);
1492
1493     __ bind(&use_global_proxy);
1494     __ lw(a0, ContextOperand(cp, Context::GLOBAL_OBJECT_INDEX));
1495     __ lw(a0, FieldMemOperand(a0, GlobalObject::kGlobalProxyOffset));
1496
1497     // Push the receiver.
1498     // a0: receiver
1499     __ bind(&push_receiver);
1500     __ push(a0);
1501
1502     // Copy all arguments from the array to the stack.
1503     Generate_PushAppliedArguments(
1504         masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1505
1506     // Call the function.
1507     Label call_proxy;
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));
1512
1513     __ InvokeFunction(a1, actual, CALL_FUNCTION, NullCallWrapper());
1514
1515     frame_scope.GenerateLeaveFrame();
1516     __ Ret(USE_DELAY_SLOT);
1517     __ Addu(sp, sp, Operand(kStackSize * kPointerSize));  // In delay slot.
1518
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.
1528   }
1529
1530   __ Ret(USE_DELAY_SLOT);
1531   __ Addu(sp, sp, Operand(kStackSize * kPointerSize));  // In delay slot.
1532 }
1533
1534
1535 static void Generate_ConstructHelper(MacroAssembler* masm) {
1536   const int kFormalParameters = 3;
1537   const int kStackSize = kFormalParameters + 1;
1538
1539   {
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;
1544
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));
1552
1553     // Validate arguments
1554     __ bind(&validate_arguments);
1555     __ lw(a0, MemOperand(fp, kFunctionOffset));  // get the function
1556     __ push(a0);
1557     __ lw(a0, MemOperand(fp, kArgumentsOffset));  // get the args array
1558     __ push(a0);
1559     __ lw(a0, MemOperand(fp, kNewTargetOffset));  // get the new.target
1560     __ push(a0);
1561     // Returns argument count in v0.
1562     __ InvokeBuiltin(Builtins::REFLECT_CONSTRUCT_PREPARE, CALL_FUNCTION);
1563
1564     // Returns result in v0.
1565     Generate_CheckStackOverflow(masm, kFunctionOffset);
1566
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
1574     __ push(a1);
1575     // Push newTarget and callee functions
1576     __ lw(a0, MemOperand(fp, kNewTargetOffset));
1577     __ push(a0);
1578     __ lw(a0, MemOperand(fp, kFunctionOffset));
1579     __ push(a0);
1580
1581     // Copy all arguments from the array to the stack.
1582     Generate_PushAppliedArguments(
1583         masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1584
1585     // Use undefined feedback vector
1586     __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1587     __ lw(a1, MemOperand(fp, kFunctionOffset));
1588
1589     // Call the function.
1590     CallConstructStub stub(masm->isolate(), SUPER_CONSTRUCTOR_CALL);
1591     __ Call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
1592
1593     __ Drop(1);
1594
1595     // Leave internal frame.
1596   }
1597   __ jr(ra);
1598   __ Addu(sp, sp, Operand(kStackSize * kPointerSize));  // In delay slot.
1599 }
1600
1601
1602 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1603   Generate_ApplyHelper(masm, false);
1604 }
1605
1606
1607 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1608   Generate_ApplyHelper(masm, true);
1609 }
1610
1611
1612 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1613   Generate_ConstructHelper(masm);
1614 }
1615
1616
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));
1635 }
1636
1637
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());
1642   __ Addu(fp, sp,
1643       Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
1644 }
1645
1646
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 +
1654                              kPointerSize)));
1655   __ mov(sp, fp);
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));
1661 }
1662
1663
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   // -----------------------------------
1671
1672   Label stack_overflow;
1673   ArgumentAdaptorStackCheck(masm, &stack_overflow);
1674   Label invoke, dont_adapt_arguments;
1675
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));
1682
1683   {  // Enough parameters: actual >= expected.
1684     // a0: actual number of arguments as a smi
1685     // a1: function
1686     // a2: expected number of arguments
1687     // a3: code entry to call
1688     __ bind(&enough);
1689     EnterArgumentsAdaptorFrame(masm);
1690
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);
1699
1700     // Copy the arguments (including the receiver) to the new stack frame.
1701     // a0: copy start address
1702     // a1: function
1703     // a2: copy end address
1704     // a3: code entry to call
1705
1706     Label copy;
1707     __ bind(&copy);
1708     __ lw(t0, MemOperand(a0));
1709     __ push(t0);
1710     __ Branch(USE_DELAY_SLOT, &copy, ne, a0, Operand(a2));
1711     __ addiu(a0, a0, -kPointerSize);  // In delay slot.
1712
1713     __ jmp(&invoke);
1714   }
1715
1716   {  // Too few parameters: Actual < expected.
1717     __ bind(&too_few);
1718     EnterArgumentsAdaptorFrame(masm);
1719
1720     // Calculate copy start address into a0 and copy end address is fp.
1721     // a0: actual number of arguments as a smi
1722     // a1: function
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);
1731
1732     // Copy the arguments (including the receiver) to the new stack frame.
1733     // a0: copy start address
1734     // a1: function
1735     // a2: expected number of arguments
1736     // a3: code entry to call
1737     // t3: copy end address
1738     Label copy;
1739     __ bind(&copy);
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, &copy, ne, a0, Operand(t3));
1744     __ sw(t0, MemOperand(sp));  // In the delay slot.
1745
1746     // Fill the remaining expected arguments with undefined.
1747     // a1: function
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 +
1755                             2 * kPointerSize));
1756
1757     Label fill;
1758     __ bind(&fill);
1759     __ Subu(sp, sp, kPointerSize);
1760     __ Branch(USE_DELAY_SLOT, &fill, ne, sp, Operand(a2));
1761     __ sw(t0, MemOperand(sp));
1762   }
1763
1764   // Call the entry point.
1765   __ bind(&invoke);
1766
1767   __ Call(a3);
1768
1769   // Store offset of return address for deoptimizer.
1770   masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1771
1772   // Exit frame and return.
1773   LeaveArgumentsAdaptorFrame(masm);
1774   __ Ret();
1775
1776
1777   // -------------------------------------------
1778   // Don't adapt arguments.
1779   // -------------------------------------------
1780   __ bind(&dont_adapt_arguments);
1781   __ Jump(a3);
1782
1783   __ bind(&stack_overflow);
1784   {
1785     FrameScope frame(masm, StackFrame::MANUAL);
1786     EnterArgumentsAdaptorFrame(masm);
1787     __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1788     __ break_(0xCC);
1789   }
1790 }
1791
1792
1793 #undef __
1794
1795 } }  // namespace v8::internal
1796
1797 #endif  // V8_TARGET_ARCH_MIPS