blob: 7a0d81a71aa1ddd33286f8a1f6801082c2cb86ee [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#if V8_TARGET_ARCH_MIPS64
6
7#include "src/codegen.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00008#include "src/debug/debug.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/deoptimizer.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/full-codegen/full-codegen.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040011#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000012
13namespace v8 {
14namespace internal {
15
16
17#define __ ACCESS_MASM(masm)
18
19
20void Builtins::Generate_Adaptor(MacroAssembler* masm,
21 CFunctionId id,
22 BuiltinExtraArguments extra_args) {
23 // ----------- S t a t e -------------
24 // -- a0 : number of arguments excluding receiver
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000025 // -- a1 : target
26 // -- a3 : new.target
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027 // -- sp[0] : last argument
28 // -- ...
29 // -- sp[8 * (argc - 1)] : first argument
30 // -- sp[8 * agrc] : receiver
31 // -----------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000032 __ AssertFunction(a1);
33
34 // Make sure we operate in the context of the called function (for example
35 // ConstructStubs implemented in C++ will be run in the context of the caller
36 // instead of the callee, due to the way that [[Construct]] is defined for
37 // ordinary functions).
38 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000039
40 // Insert extra arguments.
41 int num_extra_args = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000042 switch (extra_args) {
43 case BuiltinExtraArguments::kTarget:
44 __ Push(a1);
45 ++num_extra_args;
46 break;
47 case BuiltinExtraArguments::kNewTarget:
48 __ Push(a3);
49 ++num_extra_args;
50 break;
51 case BuiltinExtraArguments::kTargetAndNewTarget:
52 __ Push(a1, a3);
53 num_extra_args += 2;
54 break;
55 case BuiltinExtraArguments::kNone:
56 break;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057 }
58
Emily Bernierd0a1eb72015-03-24 16:35:39 -040059 // JumpToExternalReference expects a0 to contain the number of arguments
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 // including the receiver and the extra arguments.
Emily Bernierd0a1eb72015-03-24 16:35:39 -040061 __ Daddu(a0, a0, num_extra_args + 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000062
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
64}
65
66
67// Load the built-in InternalArray function from the current context.
68static void GenerateLoadInternalArrayFunction(MacroAssembler* masm,
69 Register result) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000070 // Load the InternalArray function from the native context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000071 __ LoadNativeContextSlot(Context::INTERNAL_ARRAY_FUNCTION_INDEX, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000072}
73
74
75// Load the built-in Array function from the current context.
76static void GenerateLoadArrayFunction(MacroAssembler* masm, Register result) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000077 // Load the Array function from the native context.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000078 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, result);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079}
80
81
82void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
83 // ----------- S t a t e -------------
84 // -- a0 : number of arguments
85 // -- ra : return address
86 // -- sp[...]: constructor arguments
87 // -----------------------------------
88 Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
89
90 // Get the InternalArray function.
91 GenerateLoadInternalArrayFunction(masm, a1);
92
93 if (FLAG_debug_code) {
94 // Initial map for the builtin InternalArray functions should be maps.
95 __ ld(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
96 __ SmiTst(a2, a4);
97 __ Assert(ne, kUnexpectedInitialMapForInternalArrayFunction,
98 a4, Operand(zero_reg));
99 __ GetObjectType(a2, a3, a4);
100 __ Assert(eq, kUnexpectedInitialMapForInternalArrayFunction,
101 a4, Operand(MAP_TYPE));
102 }
103
104 // Run the native code for the InternalArray function called as a normal
105 // function.
106 // Tail call a stub.
107 InternalArrayConstructorStub stub(masm->isolate());
108 __ TailCallStub(&stub);
109}
110
111
112void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
113 // ----------- S t a t e -------------
114 // -- a0 : number of arguments
115 // -- ra : return address
116 // -- sp[...]: constructor arguments
117 // -----------------------------------
118 Label generic_array_code;
119
120 // Get the Array function.
121 GenerateLoadArrayFunction(masm, a1);
122
123 if (FLAG_debug_code) {
124 // Initial map for the builtin Array functions should be maps.
125 __ ld(a2, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
126 __ SmiTst(a2, a4);
127 __ Assert(ne, kUnexpectedInitialMapForArrayFunction1,
128 a4, Operand(zero_reg));
129 __ GetObjectType(a2, a3, a4);
130 __ Assert(eq, kUnexpectedInitialMapForArrayFunction2,
131 a4, Operand(MAP_TYPE));
132 }
133
134 // Run the native code for the Array function called as a normal function.
135 // Tail call a stub.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000136 __ mov(a3, a1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000137 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
138 ArrayConstructorStub stub(masm->isolate());
139 __ TailCallStub(&stub);
140}
141
142
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000143// static
Ben Murdoch097c5b22016-05-18 11:27:45 +0100144void Builtins::Generate_MathMaxMin(MacroAssembler* masm, MathMaxMinKind kind) {
145 // ----------- S t a t e -------------
146 // -- a0 : number of arguments
147 // -- ra : return address
148 // -- sp[(argc - n) * 8] : arg[n] (zero-based)
149 // -- sp[(argc + 1) * 8] : receiver
150 // -----------------------------------
Ben Murdoch097c5b22016-05-18 11:27:45 +0100151 Heap::RootListIndex const root_index =
152 (kind == MathMaxMinKind::kMin) ? Heap::kInfinityValueRootIndex
153 : Heap::kMinusInfinityValueRootIndex;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100154
155 // Load the accumulator with the default return value (either -Infinity or
156 // +Infinity), with the tagged value in a1 and the double value in f0.
157 __ LoadRoot(a1, root_index);
158 __ ldc1(f0, FieldMemOperand(a1, HeapNumber::kValueOffset));
Ben Murdochda12d292016-06-02 14:46:10 +0100159 __ Addu(a3, a0, 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100160
161 Label done_loop, loop;
162 __ bind(&loop);
163 {
164 // Check if all parameters done.
165 __ Dsubu(a0, a0, Operand(1));
166 __ Branch(&done_loop, lt, a0, Operand(zero_reg));
167
168 // Load the next parameter tagged value into a2.
169 __ Dlsa(at, sp, a0, kPointerSizeLog2);
170 __ ld(a2, MemOperand(at));
171
172 // Load the double value of the parameter into f2, maybe converting the
173 // parameter to a number first using the ToNumberStub if necessary.
174 Label convert, convert_smi, convert_number, done_convert;
175 __ bind(&convert);
176 __ JumpIfSmi(a2, &convert_smi);
177 __ ld(a4, FieldMemOperand(a2, HeapObject::kMapOffset));
178 __ JumpIfRoot(a4, Heap::kHeapNumberMapRootIndex, &convert_number);
179 {
180 // Parameter is not a Number, use the ToNumberStub to convert it.
181 FrameScope scope(masm, StackFrame::INTERNAL);
182 __ SmiTag(a0);
183 __ SmiTag(a3);
184 __ Push(a0, a1, a3);
185 __ mov(a0, a2);
186 ToNumberStub stub(masm->isolate());
187 __ CallStub(&stub);
188 __ mov(a2, v0);
189 __ Pop(a0, a1, a3);
190 {
191 // Restore the double accumulator value (f0).
192 Label restore_smi, done_restore;
193 __ JumpIfSmi(a1, &restore_smi);
194 __ ldc1(f0, FieldMemOperand(a1, HeapNumber::kValueOffset));
195 __ jmp(&done_restore);
196 __ bind(&restore_smi);
197 __ SmiToDoubleFPURegister(a1, f0, a4);
198 __ bind(&done_restore);
199 }
200 __ SmiUntag(a3);
201 __ SmiUntag(a0);
202 }
203 __ jmp(&convert);
204 __ bind(&convert_number);
205 __ ldc1(f2, FieldMemOperand(a2, HeapNumber::kValueOffset));
206 __ jmp(&done_convert);
207 __ bind(&convert_smi);
208 __ SmiToDoubleFPURegister(a2, f2, a4);
209 __ bind(&done_convert);
210
Ben Murdochda12d292016-06-02 14:46:10 +0100211 // Perform the actual comparison with using Min/Max macro instructions the
212 // accumulator value on the left hand side (f0) and the next parameter value
213 // on the right hand side (f2).
214 // We need to work out which HeapNumber (or smi) the result came from.
215 Label compare_nan;
216 __ BranchF(nullptr, &compare_nan, eq, f0, f2);
217 __ Move(a4, f0);
218 if (kind == MathMaxMinKind::kMin) {
219 __ MinNaNCheck_d(f0, f0, f2);
220 } else {
221 DCHECK(kind == MathMaxMinKind::kMax);
222 __ MaxNaNCheck_d(f0, f0, f2);
223 }
224 __ Move(at, f0);
225 __ Branch(&loop, eq, a4, Operand(at));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100226 __ mov(a1, a2);
227 __ jmp(&loop);
228
229 // At least one side is NaN, which means that the result will be NaN too.
230 __ bind(&compare_nan);
231 __ LoadRoot(a1, Heap::kNanValueRootIndex);
232 __ ldc1(f0, FieldMemOperand(a1, HeapNumber::kValueOffset));
233 __ jmp(&loop);
234 }
235
236 __ bind(&done_loop);
237 __ Dlsa(sp, sp, a3, kPointerSizeLog2);
Ben Murdochda12d292016-06-02 14:46:10 +0100238 __ Ret(USE_DELAY_SLOT);
239 __ mov(v0, a1); // In delay slot.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100240}
241
242// static
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000243void Builtins::Generate_NumberConstructor(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000244 // ----------- S t a t e -------------
245 // -- a0 : number of arguments
246 // -- a1 : constructor function
247 // -- ra : return address
248 // -- sp[(argc - n - 1) * 8] : arg[n] (zero based)
249 // -- sp[argc * 8] : receiver
250 // -----------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000251
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000252 // 1. Load the first argument into a0 and get rid of the rest (including the
253 // receiver).
254 Label no_arguments;
255 {
256 __ Branch(USE_DELAY_SLOT, &no_arguments, eq, a0, Operand(zero_reg));
257 __ Dsubu(a0, a0, Operand(1));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100258 __ Dlsa(sp, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259 __ ld(a0, MemOperand(sp));
260 __ Drop(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000261 }
262
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 // 2a. Convert first argument to number.
264 ToNumberStub stub(masm->isolate());
265 __ TailCallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000266
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000267 // 2b. No arguments, return +0.
268 __ bind(&no_arguments);
269 __ Move(v0, Smi::FromInt(0));
270 __ DropAndRet(1);
271}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000272
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000273
274void Builtins::Generate_NumberConstructor_ConstructStub(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000276 // -- a0 : number of arguments
277 // -- a1 : constructor function
278 // -- a3 : new target
279 // -- ra : return address
280 // -- sp[(argc - n - 1) * 8] : arg[n] (zero based)
281 // -- sp[argc * 8] : receiver
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000282 // -----------------------------------
283
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000284 // 1. Make sure we operate in the context of the called function.
285 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000287 // 2. Load the first argument into a0 and get rid of the rest (including the
288 // receiver).
289 {
290 Label no_arguments, done;
291 __ Branch(USE_DELAY_SLOT, &no_arguments, eq, a0, Operand(zero_reg));
292 __ Dsubu(a0, a0, Operand(1));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100293 __ Dlsa(sp, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000294 __ ld(a0, MemOperand(sp));
295 __ Drop(2);
296 __ jmp(&done);
297 __ bind(&no_arguments);
298 __ Move(a0, Smi::FromInt(0));
299 __ Drop(1);
300 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000303 // 3. Make sure a0 is a number.
304 {
305 Label done_convert;
306 __ JumpIfSmi(a0, &done_convert);
307 __ GetObjectType(a0, a2, a2);
308 __ Branch(&done_convert, eq, t0, Operand(HEAP_NUMBER_TYPE));
309 {
310 FrameScope scope(masm, StackFrame::INTERNAL);
311 __ Push(a1, a3);
312 ToNumberStub stub(masm->isolate());
313 __ CallStub(&stub);
314 __ Move(a0, v0);
315 __ Pop(a1, a3);
316 }
317 __ bind(&done_convert);
318 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000319
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000320 // 4. Check if new target and constructor differ.
321 Label new_object;
322 __ Branch(&new_object, ne, a1, Operand(a3));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 // 5. Allocate a JSValue wrapper for the number.
325 __ AllocateJSValue(v0, a1, a0, a2, t0, &new_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000326 __ Ret();
327
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000328 // 6. Fallback to the runtime to create new object.
329 __ bind(&new_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000330 {
331 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100332 __ Push(a0);
333 FastNewObjectStub stub(masm->isolate());
334 __ CallStub(&stub);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000335 __ Pop(a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000337 __ Ret(USE_DELAY_SLOT);
338 __ sd(a0, FieldMemOperand(v0, JSValue::kValueOffset)); // In delay slot.
339}
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000341
342// static
343void Builtins::Generate_StringConstructor(MacroAssembler* masm) {
344 // ----------- S t a t e -------------
345 // -- a0 : number of arguments
346 // -- a1 : constructor function
347 // -- ra : return address
348 // -- sp[(argc - n - 1) * 8] : arg[n] (zero based)
349 // -- sp[argc * 8] : receiver
350 // -----------------------------------
351
352 // 1. Load the first argument into a0 and get rid of the rest (including the
353 // receiver).
354 Label no_arguments;
355 {
356 __ Branch(USE_DELAY_SLOT, &no_arguments, eq, a0, Operand(zero_reg));
357 __ Dsubu(a0, a0, Operand(1));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100358 __ Dlsa(sp, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000359 __ ld(a0, MemOperand(sp));
360 __ Drop(2);
361 }
362
363 // 2a. At least one argument, return a0 if it's a string, otherwise
364 // dispatch to appropriate conversion.
365 Label to_string, symbol_descriptive_string;
366 {
367 __ JumpIfSmi(a0, &to_string);
368 __ GetObjectType(a0, a1, a1);
369 STATIC_ASSERT(FIRST_NONSTRING_TYPE == SYMBOL_TYPE);
370 __ Subu(a1, a1, Operand(FIRST_NONSTRING_TYPE));
371 __ Branch(&symbol_descriptive_string, eq, a1, Operand(zero_reg));
372 __ Branch(&to_string, gt, a1, Operand(zero_reg));
373 __ Ret(USE_DELAY_SLOT);
374 __ mov(v0, a0);
375 }
376
377 // 2b. No arguments, return the empty string (and pop the receiver).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000378 __ bind(&no_arguments);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000379 {
380 __ LoadRoot(v0, Heap::kempty_stringRootIndex);
381 __ DropAndRet(1);
382 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000384 // 3a. Convert a0 to a string.
385 __ bind(&to_string);
386 {
387 ToStringStub stub(masm->isolate());
388 __ TailCallStub(&stub);
389 }
390
391 // 3b. Convert symbol in a0 to a string.
392 __ bind(&symbol_descriptive_string);
393 {
394 __ Push(a0);
395 __ TailCallRuntime(Runtime::kSymbolDescriptiveString);
396 }
397}
398
399
400void Builtins::Generate_StringConstructor_ConstructStub(MacroAssembler* masm) {
401 // ----------- S t a t e -------------
402 // -- a0 : number of arguments
403 // -- a1 : constructor function
404 // -- a3 : new target
405 // -- ra : return address
406 // -- sp[(argc - n - 1) * 8] : arg[n] (zero based)
407 // -- sp[argc * 8] : receiver
408 // -----------------------------------
409
410 // 1. Make sure we operate in the context of the called function.
411 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
412
413 // 2. Load the first argument into a0 and get rid of the rest (including the
414 // receiver).
415 {
416 Label no_arguments, done;
417 __ Branch(USE_DELAY_SLOT, &no_arguments, eq, a0, Operand(zero_reg));
418 __ Dsubu(a0, a0, Operand(1));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100419 __ Dlsa(sp, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420 __ ld(a0, MemOperand(sp));
421 __ Drop(2);
422 __ jmp(&done);
423 __ bind(&no_arguments);
424 __ LoadRoot(a0, Heap::kempty_stringRootIndex);
425 __ Drop(1);
426 __ bind(&done);
427 }
428
429 // 3. Make sure a0 is a string.
430 {
431 Label convert, done_convert;
432 __ JumpIfSmi(a0, &convert);
433 __ GetObjectType(a0, a2, a2);
434 __ And(t0, a2, Operand(kIsNotStringMask));
435 __ Branch(&done_convert, eq, t0, Operand(zero_reg));
436 __ bind(&convert);
437 {
438 FrameScope scope(masm, StackFrame::INTERNAL);
439 ToStringStub stub(masm->isolate());
440 __ Push(a1, a3);
441 __ CallStub(&stub);
442 __ Move(a0, v0);
443 __ Pop(a1, a3);
444 }
445 __ bind(&done_convert);
446 }
447
448 // 4. Check if new target and constructor differ.
449 Label new_object;
450 __ Branch(&new_object, ne, a1, Operand(a3));
451
452 // 5. Allocate a JSValue wrapper for the string.
453 __ AllocateJSValue(v0, a1, a0, a2, t0, &new_object);
454 __ Ret();
455
456 // 6. Fallback to the runtime to create new object.
457 __ bind(&new_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 {
459 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100460 __ Push(a0);
461 FastNewObjectStub stub(masm->isolate());
462 __ CallStub(&stub);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000463 __ Pop(a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000465 __ Ret(USE_DELAY_SLOT);
466 __ sd(a0, FieldMemOperand(v0, JSValue::kValueOffset)); // In delay slot.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467}
468
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
470 __ ld(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
471 __ ld(a2, FieldMemOperand(a2, SharedFunctionInfo::kCodeOffset));
472 __ Daddu(at, a2, Operand(Code::kHeaderSize - kHeapObjectTag));
473 __ Jump(at);
474}
475
Ben Murdoch097c5b22016-05-18 11:27:45 +0100476static void GenerateTailCallToReturnedCode(MacroAssembler* masm,
477 Runtime::FunctionId function_id) {
478 // ----------- S t a t e -------------
479 // -- a0 : argument count (preserved for callee)
480 // -- a1 : target function (preserved for callee)
481 // -- a3 : new target (preserved for callee)
482 // -----------------------------------
483 {
484 FrameScope scope(masm, StackFrame::INTERNAL);
485 // Push a copy of the function onto the stack.
486 // Push a copy of the target function and the new target.
487 __ SmiTag(a0);
488 __ Push(a0, a1, a3, a1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489
Ben Murdoch097c5b22016-05-18 11:27:45 +0100490 __ CallRuntime(function_id, 1);
491 // Restore target function and new target.
492 __ Pop(a0, a1, a3);
493 __ SmiUntag(a0);
494 }
495
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000496 __ Daddu(at, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
497 __ Jump(at);
498}
499
500
501void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
502 // Checking whether the queued function is ready for install is optional,
503 // since we come across interrupts and stack checks elsewhere. However,
504 // not checking may delay installing ready functions, and always checking
505 // would be quite expensive. A good compromise is to first check against
506 // stack limit as a cue for an interrupt signal.
507 Label ok;
508 __ LoadRoot(a4, Heap::kStackLimitRootIndex);
509 __ Branch(&ok, hs, sp, Operand(a4));
510
Ben Murdoch097c5b22016-05-18 11:27:45 +0100511 GenerateTailCallToReturnedCode(masm, Runtime::kTryInstallOptimizedCode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512
513 __ bind(&ok);
514 GenerateTailCallToSharedCode(masm);
515}
516
517
518static void Generate_JSConstructStubHelper(MacroAssembler* masm,
519 bool is_api_function,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100520 bool create_implicit_receiver,
521 bool check_derived_construct) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522 // ----------- S t a t e -------------
523 // -- a0 : number of arguments
524 // -- a1 : constructor function
525 // -- a2 : allocation site or undefined
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000526 // -- a3 : new target
Ben Murdochda12d292016-06-02 14:46:10 +0100527 // -- cp : context
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 // -- ra : return address
529 // -- sp[...]: constructor arguments
530 // -----------------------------------
531
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000532 Isolate* isolate = masm->isolate();
533
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534 // Enter a construct frame.
535 {
536 FrameScope scope(masm, StackFrame::CONSTRUCT);
537
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000538 // Preserve the incoming parameters on the stack.
539 __ AssertUndefinedOrAllocationSite(a2, t0);
540 __ SmiTag(a0);
Ben Murdochda12d292016-06-02 14:46:10 +0100541 __ Push(cp, a2, a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000543 if (create_implicit_receiver) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100544 __ Push(a1, a3);
545 FastNewObjectStub stub(masm->isolate());
546 __ CallStub(&stub);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000547 __ mov(t0, v0);
548 __ Pop(a1, a3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549
Ben Murdoch097c5b22016-05-18 11:27:45 +0100550 // ----------- S t a t e -------------
551 // -- a1: constructor function
552 // -- a3: new target
553 // -- t0: newly allocated object
554 // -----------------------------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000555 __ ld(a0, MemOperand(sp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000557 __ SmiUntag(a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000559 if (create_implicit_receiver) {
560 // Push the allocated receiver to the stack. We need two copies
561 // because we may have to return the original one and the calling
562 // conventions dictate that the called function pops the receiver.
563 __ Push(t0, t0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000564 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000565 __ PushRoot(Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000567
568 // Set up pointer to last argument.
569 __ Daddu(a2, fp, Operand(StandardFrameConstants::kCallerSPOffset));
570
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571 // Copy arguments and receiver to the expression stack.
572 // a0: number of arguments
573 // a1: constructor function
574 // a2: address of last argument (caller sp)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000575 // a3: new target
576 // t0: number of arguments (smi-tagged)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577 // sp[0]: receiver
578 // sp[1]: receiver
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000579 // sp[2]: number of arguments (smi-tagged)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 Label loop, entry;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000581 __ mov(t0, a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000582 __ jmp(&entry);
583 __ bind(&loop);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100584 __ Dlsa(a4, a2, t0, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000585 __ ld(a5, MemOperand(a4));
586 __ push(a5);
587 __ bind(&entry);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000588 __ Daddu(t0, t0, Operand(-1));
589 __ Branch(&loop, greater_equal, t0, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590
591 // Call the function.
592 // a0: number of arguments
593 // a1: constructor function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000594 // a3: new target
Ben Murdochc5610432016-08-08 18:44:38 +0100595 ParameterCount actual(a0);
596 __ InvokeFunction(a1, a3, actual, CALL_FUNCTION,
597 CheckDebugStepCallWrapper());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000598
599 // Store offset of return address for deoptimizer.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000600 if (create_implicit_receiver && !is_api_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
602 }
603
604 // Restore context from the frame.
Ben Murdochda12d292016-06-02 14:46:10 +0100605 __ ld(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000606
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000607 if (create_implicit_receiver) {
608 // If the result is an object (in the ECMA sense), we should get rid
609 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
610 // on page 74.
611 Label use_receiver, exit;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000613 // If the result is a smi, it is *not* an object in the ECMA sense.
614 // v0: result
615 // sp[0]: receiver (newly allocated object)
616 // sp[1]: number of arguments (smi-tagged)
617 __ JumpIfSmi(v0, &use_receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000619 // If the type of the result (stored in its map) is less than
620 // FIRST_JS_RECEIVER_TYPE, it is not an object in the ECMA sense.
621 __ GetObjectType(v0, a1, a3);
622 __ Branch(&exit, greater_equal, a3, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000623
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000624 // Throw away the result of the constructor invocation and use the
625 // on-stack receiver as the result.
626 __ bind(&use_receiver);
627 __ ld(v0, MemOperand(sp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000629 // Remove receiver from the stack, remove caller arguments, and
630 // return.
631 __ bind(&exit);
632 // v0: result
633 // sp[0]: receiver (newly allocated object)
634 // sp[1]: number of arguments (smi-tagged)
635 __ ld(a1, MemOperand(sp, 1 * kPointerSize));
636 } else {
637 __ ld(a1, MemOperand(sp));
638 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000639
640 // Leave construct frame.
641 }
642
Ben Murdoch097c5b22016-05-18 11:27:45 +0100643 // ES6 9.2.2. Step 13+
644 // Check that the result is not a Smi, indicating that the constructor result
645 // from a derived class is neither undefined nor an Object.
646 if (check_derived_construct) {
647 Label dont_throw;
648 __ JumpIfNotSmi(v0, &dont_throw);
649 {
650 FrameScope scope(masm, StackFrame::INTERNAL);
651 __ CallRuntime(Runtime::kThrowDerivedConstructorReturnedNonObject);
652 }
653 __ bind(&dont_throw);
654 }
655
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000656 __ SmiScale(a4, a1, kPointerSizeLog2);
657 __ Daddu(sp, sp, a4);
658 __ Daddu(sp, sp, kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000659 if (create_implicit_receiver) {
660 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, a1, a2);
661 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 __ Ret();
663}
664
665
666void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100667 Generate_JSConstructStubHelper(masm, false, true, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000668}
669
670
671void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100672 Generate_JSConstructStubHelper(masm, true, false, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000673}
674
675
676void Builtins::Generate_JSBuiltinsConstructStub(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100677 Generate_JSConstructStubHelper(masm, false, false, false);
678}
679
680
681void Builtins::Generate_JSBuiltinsConstructStubForDerived(
682 MacroAssembler* masm) {
683 Generate_JSConstructStubHelper(masm, false, false, true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000684}
685
Ben Murdochc5610432016-08-08 18:44:38 +0100686// static
687void Builtins::Generate_ResumeGeneratorTrampoline(MacroAssembler* masm) {
688 // ----------- S t a t e -------------
689 // -- v0 : the value to pass to the generator
690 // -- a1 : the JSGeneratorObject to resume
691 // -- a2 : the resume mode (tagged)
692 // -- ra : return address
693 // -----------------------------------
694 __ AssertGeneratorObject(a1);
695
696 // Store input value into generator object.
697 __ sd(v0, FieldMemOperand(a1, JSGeneratorObject::kInputOffset));
698 __ RecordWriteField(a1, JSGeneratorObject::kInputOffset, v0, a3,
699 kRAHasNotBeenSaved, kDontSaveFPRegs);
700
701 // Store resume mode into generator object.
702 __ sd(a2, FieldMemOperand(a1, JSGeneratorObject::kResumeModeOffset));
703
704 // Load suspended function and context.
705 __ ld(cp, FieldMemOperand(a1, JSGeneratorObject::kContextOffset));
706 __ ld(a4, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
707
708 // Flood function if we are stepping.
709 Label skip_flooding;
710 ExternalReference step_in_enabled =
711 ExternalReference::debug_step_in_enabled_address(masm->isolate());
712 __ li(t1, Operand(step_in_enabled));
713 __ lb(t1, MemOperand(t1));
714 __ Branch(&skip_flooding, eq, t1, Operand(zero_reg));
715 {
716 FrameScope scope(masm, StackFrame::INTERNAL);
717 __ Push(a1, a2, a4);
718 __ CallRuntime(Runtime::kDebugPrepareStepInIfStepping);
719 __ Pop(a1, a2);
720 __ ld(a4, FieldMemOperand(a1, JSGeneratorObject::kFunctionOffset));
721 }
722 __ bind(&skip_flooding);
723
724 // Push receiver.
725 __ ld(a5, FieldMemOperand(a1, JSGeneratorObject::kReceiverOffset));
726 __ Push(a5);
727
728 // ----------- S t a t e -------------
729 // -- a1 : the JSGeneratorObject to resume
730 // -- a2 : the resume mode (tagged)
731 // -- a4 : generator function
732 // -- cp : generator context
733 // -- ra : return address
734 // -- sp[0] : generator receiver
735 // -----------------------------------
736
737 // Push holes for arguments to generator function. Since the parser forced
738 // context allocation for any variables in generators, the actual argument
739 // values have already been copied into the context and these dummy values
740 // will never be used.
741 __ ld(a3, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
742 __ lw(a3,
743 FieldMemOperand(a3, SharedFunctionInfo::kFormalParameterCountOffset));
744 {
745 Label done_loop, loop;
746 __ bind(&loop);
747 __ Dsubu(a3, a3, Operand(1));
748 __ Branch(&done_loop, lt, a3, Operand(zero_reg));
749 __ PushRoot(Heap::kTheHoleValueRootIndex);
750 __ Branch(&loop);
751 __ bind(&done_loop);
752 }
753
754 // Dispatch on the kind of generator object.
755 Label old_generator;
756 __ ld(a3, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
757 __ ld(a3, FieldMemOperand(a3, SharedFunctionInfo::kFunctionDataOffset));
758 __ GetObjectType(a3, a3, a3);
759 __ Branch(&old_generator, ne, a3, Operand(BYTECODE_ARRAY_TYPE));
760
761 // New-style (ignition/turbofan) generator object.
762 {
763 __ ld(a0, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
764 __ lw(a0,
765 FieldMemOperand(a0, SharedFunctionInfo::kFormalParameterCountOffset));
766 __ SmiUntag(a0);
767 // We abuse new.target both to indicate that this is a resume call and to
768 // pass in the generator object. In ordinary calls, new.target is always
769 // undefined because generator functions are non-constructable.
770 __ Move(a3, a1);
771 __ Move(a1, a4);
772 __ ld(a2, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
773 __ Jump(a2);
774 }
775
776 // Old-style (full-codegen) generator object
777 __ bind(&old_generator);
778 {
779 // Enter a new JavaScript frame, and initialize its slots as they were when
780 // the generator was suspended.
781 FrameScope scope(masm, StackFrame::MANUAL);
782 __ Push(ra, fp);
783 __ Move(fp, sp);
784 __ Push(cp, a4);
785
786 // Restore the operand stack.
787 __ ld(a0, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
788 __ ld(a3, FieldMemOperand(a0, FixedArray::kLengthOffset));
789 __ SmiUntag(a3);
790 __ Daddu(a0, a0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
791 __ Dlsa(a3, a0, a3, kPointerSizeLog2);
792 {
793 Label done_loop, loop;
794 __ bind(&loop);
795 __ Branch(&done_loop, eq, a0, Operand(a3));
796 __ ld(a5, MemOperand(a0));
797 __ Push(a5);
798 __ Branch(USE_DELAY_SLOT, &loop);
799 __ daddiu(a0, a0, kPointerSize); // In delay slot.
800 __ bind(&done_loop);
801 }
802
803 // Reset operand stack so we don't leak.
804 __ LoadRoot(a5, Heap::kEmptyFixedArrayRootIndex);
805 __ sd(a5, FieldMemOperand(a1, JSGeneratorObject::kOperandStackOffset));
806
807 // Resume the generator function at the continuation.
808 __ ld(a3, FieldMemOperand(a4, JSFunction::kSharedFunctionInfoOffset));
809 __ ld(a3, FieldMemOperand(a3, SharedFunctionInfo::kCodeOffset));
810 __ Daddu(a3, a3, Operand(Code::kHeaderSize - kHeapObjectTag));
811 __ ld(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
812 __ SmiUntag(a2);
813 __ Daddu(a3, a3, Operand(a2));
814 __ li(a2, Operand(Smi::FromInt(JSGeneratorObject::kGeneratorExecuting)));
815 __ sd(a2, FieldMemOperand(a1, JSGeneratorObject::kContinuationOffset));
816 __ Move(v0, a1); // Continuation expects generator object in v0.
817 __ Jump(a3);
818 }
819}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000820
821void Builtins::Generate_ConstructedNonConstructable(MacroAssembler* masm) {
822 FrameScope scope(masm, StackFrame::INTERNAL);
823 __ Push(a1);
824 __ CallRuntime(Runtime::kThrowConstructedNonConstructable);
825}
826
827
828enum IsTagged { kArgcIsSmiTagged, kArgcIsUntaggedInt };
829
830
831// Clobbers a2; preserves all other registers.
832static void Generate_CheckStackOverflow(MacroAssembler* masm, Register argc,
833 IsTagged argc_is_tagged) {
834 // Check the stack for overflow. We are not trying to catch
835 // interruptions (e.g. debug break and preemption) here, so the "real stack
836 // limit" is checked.
837 Label okay;
838 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
839 // Make a2 the space we have left. The stack might already be overflowed
840 // here which will cause r2 to become negative.
841 __ dsubu(a2, sp, a2);
842 // Check if the arguments will overflow the stack.
843 if (argc_is_tagged == kArgcIsSmiTagged) {
844 __ SmiScale(a7, v0, kPointerSizeLog2);
845 } else {
846 DCHECK(argc_is_tagged == kArgcIsUntaggedInt);
847 __ dsll(a7, argc, kPointerSizeLog2);
848 }
849 __ Branch(&okay, gt, a2, Operand(a7)); // Signed comparison.
850
851 // Out of stack space.
852 __ CallRuntime(Runtime::kThrowStackOverflow);
853
854 __ bind(&okay);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855}
856
857
858static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
859 bool is_construct) {
860 // Called from JSEntryStub::GenerateBody
861
862 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000863 // -- a0: new.target
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000864 // -- a1: function
865 // -- a2: receiver_pointer
866 // -- a3: argc
867 // -- s0: argv
868 // -----------------------------------
869 ProfileEntryHookStub::MaybeCallEntryHook(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000870
871 // Enter an internal frame.
872 {
873 FrameScope scope(masm, StackFrame::INTERNAL);
874
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000875 // Setup the context (we need to use the caller context from the isolate).
876 ExternalReference context_address(Isolate::kContextAddress,
877 masm->isolate());
878 __ li(cp, Operand(context_address));
879 __ ld(cp, MemOperand(cp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000880
881 // Push the function and the receiver onto the stack.
882 __ Push(a1, a2);
883
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000884 // Check if we have enough stack space to push all arguments.
885 // Clobbers a2.
886 Generate_CheckStackOverflow(masm, a3, kArgcIsUntaggedInt);
887
888 // Remember new.target.
889 __ mov(a5, a0);
890
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 // Copy arguments to the stack in a loop.
892 // a3: argc
893 // s0: argv, i.e. points to first arg
894 Label loop, entry;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100895 __ Dlsa(a6, s0, a3, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 __ b(&entry);
897 __ nop(); // Branch delay slot nop.
898 // a6 points past last arg.
899 __ bind(&loop);
900 __ ld(a4, MemOperand(s0)); // Read next parameter.
901 __ daddiu(s0, s0, kPointerSize);
902 __ ld(a4, MemOperand(a4)); // Dereference handle.
903 __ push(a4); // Push parameter.
904 __ bind(&entry);
905 __ Branch(&loop, ne, s0, Operand(a6));
906
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000907 // Setup new.target and argc.
908 __ mov(a0, a3);
909 __ mov(a3, a5);
910
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000911 // Initialize all JavaScript callee-saved registers, since they will be seen
912 // by the garbage collector as part of handlers.
913 __ LoadRoot(a4, Heap::kUndefinedValueRootIndex);
914 __ mov(s1, a4);
915 __ mov(s2, a4);
916 __ mov(s3, a4);
917 __ mov(s4, a4);
918 __ mov(s5, a4);
919 // s6 holds the root address. Do not clobber.
920 // s7 is cp. Do not init.
921
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000922 // Invoke the code.
923 Handle<Code> builtin = is_construct
924 ? masm->isolate()->builtins()->Construct()
925 : masm->isolate()->builtins()->Call();
926 __ Call(builtin, RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000927
928 // Leave internal frame.
929 }
930 __ Jump(ra);
931}
932
933
934void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
935 Generate_JSEntryTrampolineHelper(masm, false);
936}
937
938
939void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
940 Generate_JSEntryTrampolineHelper(masm, true);
941}
942
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000943// Generate code for entering a JS function with the interpreter.
944// On entry to the function the receiver and arguments have been pushed on the
945// stack left to right. The actual argument count matches the formal parameter
946// count expected by the function.
947//
948// The live registers are:
949// o a1: the JS function object being called.
950// o a3: the new target
951// o cp: our context
952// o fp: the caller's frame pointer
953// o sp: stack pointer
954// o ra: return address
955//
Ben Murdoch097c5b22016-05-18 11:27:45 +0100956// The function builds an interpreter frame. See InterpreterFrameConstants in
957// frames.h for its layout.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000958void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
Ben Murdochc5610432016-08-08 18:44:38 +0100959 ProfileEntryHookStub::MaybeCallEntryHook(masm);
960
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000961 // Open a frame scope to indicate that there is a frame on the stack. The
962 // MANUAL indicates that the scope shouldn't actually generate code to set up
963 // the frame (that is done below).
964 FrameScope frame_scope(masm, StackFrame::MANUAL);
Ben Murdochda12d292016-06-02 14:46:10 +0100965 __ PushStandardFrame(a1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000966
Ben Murdochc5610432016-08-08 18:44:38 +0100967 // Get the bytecode array from the function object (or from the DebugInfo if
968 // it is present) and load it into kInterpreterBytecodeArrayRegister.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000969 __ ld(a0, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100970 Label load_debug_bytecode_array, bytecode_array_loaded;
971 Register debug_info = kInterpreterBytecodeArrayRegister;
972 DCHECK(!debug_info.is(a0));
973 __ ld(debug_info, FieldMemOperand(a0, SharedFunctionInfo::kDebugInfoOffset));
974 __ Branch(&load_debug_bytecode_array, ne, debug_info,
975 Operand(DebugInfo::uninitialized()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000976 __ ld(kInterpreterBytecodeArrayRegister,
977 FieldMemOperand(a0, SharedFunctionInfo::kFunctionDataOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100978 __ bind(&bytecode_array_loaded);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000979
Ben Murdochc5610432016-08-08 18:44:38 +0100980 // Check function data field is actually a BytecodeArray object.
981 Label bytecode_array_not_present;
982 __ JumpIfRoot(kInterpreterBytecodeArrayRegister,
983 Heap::kUndefinedValueRootIndex, &bytecode_array_not_present);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000984 if (FLAG_debug_code) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000985 __ SmiTst(kInterpreterBytecodeArrayRegister, a4);
986 __ Assert(ne, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, a4,
987 Operand(zero_reg));
988 __ GetObjectType(kInterpreterBytecodeArrayRegister, a4, a4);
989 __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, a4,
990 Operand(BYTECODE_ARRAY_TYPE));
991 }
992
Ben Murdochc5610432016-08-08 18:44:38 +0100993 // Load initial bytecode offset.
994 __ li(kInterpreterBytecodeOffsetRegister,
995 Operand(BytecodeArray::kHeaderSize - kHeapObjectTag));
996
997 // Push new.target, bytecode array and Smi tagged bytecode array offset.
998 __ SmiTag(a4, kInterpreterBytecodeOffsetRegister);
999 __ Push(a3, kInterpreterBytecodeArrayRegister, a4);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001000
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001001 // Allocate the local and temporary register file on the stack.
1002 {
1003 // Load frame size (word) from the BytecodeArray object.
1004 __ lw(a4, FieldMemOperand(kInterpreterBytecodeArrayRegister,
1005 BytecodeArray::kFrameSizeOffset));
1006
1007 // Do a stack check to ensure we don't go over the limit.
1008 Label ok;
1009 __ Dsubu(a5, sp, Operand(a4));
1010 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
1011 __ Branch(&ok, hs, a5, Operand(a2));
1012 __ CallRuntime(Runtime::kThrowStackOverflow);
1013 __ bind(&ok);
1014
1015 // If ok, push undefined as the initial value for all register file entries.
1016 Label loop_header;
1017 Label loop_check;
1018 __ LoadRoot(a5, Heap::kUndefinedValueRootIndex);
1019 __ Branch(&loop_check);
1020 __ bind(&loop_header);
1021 // TODO(rmcilroy): Consider doing more than one push per loop iteration.
1022 __ push(a5);
1023 // Continue loop if not done.
1024 __ bind(&loop_check);
1025 __ Dsubu(a4, a4, Operand(kPointerSize));
1026 __ Branch(&loop_header, ge, a4, Operand(zero_reg));
1027 }
1028
Ben Murdochc5610432016-08-08 18:44:38 +01001029 // Load accumulator and dispatch table into registers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001030 __ LoadRoot(kInterpreterAccumulatorRegister, Heap::kUndefinedValueRootIndex);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001031 __ li(kInterpreterDispatchTableRegister,
1032 Operand(ExternalReference::interpreter_dispatch_table_address(
1033 masm->isolate())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001034
1035 // Dispatch to the first bytecode handler for the function.
1036 __ Daddu(a0, kInterpreterBytecodeArrayRegister,
1037 kInterpreterBytecodeOffsetRegister);
1038 __ lbu(a0, MemOperand(a0));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001039 __ Dlsa(at, kInterpreterDispatchTableRegister, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001040 __ ld(at, MemOperand(at));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001041 __ Call(at);
Ben Murdochc5610432016-08-08 18:44:38 +01001042 masm->isolate()->heap()->SetInterpreterEntryReturnPCOffset(masm->pc_offset());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001043
Ben Murdochc5610432016-08-08 18:44:38 +01001044 // The return value is in v0.
1045
1046 // Get the arguments + reciever count.
1047 __ ld(t0, MemOperand(fp, InterpreterFrameConstants::kBytecodeArrayFromFp));
1048 __ lw(t0, FieldMemOperand(t0, BytecodeArray::kParameterSizeOffset));
1049
1050 // Leave the frame (also dropping the register file).
1051 __ LeaveFrame(StackFrame::JAVA_SCRIPT);
1052
1053 // Drop receiver + arguments and return.
1054 __ Daddu(sp, sp, t0);
1055 __ Jump(ra);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001056
1057 // Load debug copy of the bytecode array.
1058 __ bind(&load_debug_bytecode_array);
1059 __ ld(kInterpreterBytecodeArrayRegister,
1060 FieldMemOperand(debug_info, DebugInfo::kAbstractCodeIndex));
1061 __ Branch(&bytecode_array_loaded);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001062
Ben Murdochc5610432016-08-08 18:44:38 +01001063 // If the bytecode array is no longer present, then the underlying function
1064 // has been switched to a different kind of code and we heal the closure by
1065 // switching the code entry field over to the new code object as well.
1066 __ bind(&bytecode_array_not_present);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001067 __ LeaveFrame(StackFrame::JAVA_SCRIPT);
Ben Murdochc5610432016-08-08 18:44:38 +01001068 __ ld(a4, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1069 __ ld(a4, FieldMemOperand(a4, SharedFunctionInfo::kCodeOffset));
1070 __ Daddu(a4, a4, Operand(Code::kHeaderSize - kHeapObjectTag));
1071 __ sd(a4, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
1072 __ RecordWriteCodeEntryField(a1, a4, a5);
1073 __ Jump(a4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001074}
1075
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001076// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01001077void Builtins::Generate_InterpreterPushArgsAndCallImpl(
1078 MacroAssembler* masm, TailCallMode tail_call_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001079 // ----------- S t a t e -------------
1080 // -- a0 : the number of arguments (not including the receiver)
1081 // -- a2 : the address of the first argument to be pushed. Subsequent
1082 // arguments should be consecutive above this, in the same order as
1083 // they are to be pushed onto the stack.
1084 // -- a1 : the target to call (can be any Object).
1085 // -----------------------------------
1086
1087 // Find the address of the last argument.
1088 __ Daddu(a3, a0, Operand(1)); // Add one for receiver.
1089 __ dsll(a3, a3, kPointerSizeLog2);
1090 __ Dsubu(a3, a2, Operand(a3));
1091
1092 // Push the arguments.
1093 Label loop_header, loop_check;
1094 __ Branch(&loop_check);
1095 __ bind(&loop_header);
1096 __ ld(t0, MemOperand(a2));
1097 __ Daddu(a2, a2, Operand(-kPointerSize));
1098 __ push(t0);
1099 __ bind(&loop_check);
1100 __ Branch(&loop_header, gt, a2, Operand(a3));
1101
1102 // Call the target.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001103 __ Jump(masm->isolate()->builtins()->Call(ConvertReceiverMode::kAny,
1104 tail_call_mode),
1105 RelocInfo::CODE_TARGET);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001106}
1107
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001108// static
1109void Builtins::Generate_InterpreterPushArgsAndConstruct(MacroAssembler* masm) {
1110 // ----------- S t a t e -------------
1111 // -- a0 : argument count (not including receiver)
1112 // -- a3 : new target
1113 // -- a1 : constructor to call
1114 // -- a2 : address of the first argument
1115 // -----------------------------------
1116
1117 // Find the address of the last argument.
1118 __ dsll(t0, a0, kPointerSizeLog2);
1119 __ Dsubu(t0, a2, Operand(t0));
1120
1121 // Push a slot for the receiver.
1122 __ push(zero_reg);
1123
1124 // Push the arguments.
1125 Label loop_header, loop_check;
1126 __ Branch(&loop_check);
1127 __ bind(&loop_header);
1128 __ ld(t1, MemOperand(a2));
1129 __ Daddu(a2, a2, Operand(-kPointerSize));
1130 __ push(t1);
1131 __ bind(&loop_check);
1132 __ Branch(&loop_header, gt, a2, Operand(t0));
1133
1134 // Call the constructor with a0, a1, and a3 unmodified.
1135 __ Jump(masm->isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
1136}
1137
Ben Murdochc5610432016-08-08 18:44:38 +01001138void Builtins::Generate_InterpreterEnterBytecodeDispatch(MacroAssembler* masm) {
1139 // Set the return address to the correct point in the interpreter entry
1140 // trampoline.
1141 Smi* interpreter_entry_return_pc_offset(
1142 masm->isolate()->heap()->interpreter_entry_return_pc_offset());
1143 DCHECK_NE(interpreter_entry_return_pc_offset, Smi::FromInt(0));
1144 __ li(t0, Operand(masm->isolate()->builtins()->InterpreterEntryTrampoline()));
1145 __ Daddu(ra, t0, Operand(interpreter_entry_return_pc_offset->value() +
1146 Code::kHeaderSize - kHeapObjectTag));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001147
Ben Murdochc5610432016-08-08 18:44:38 +01001148 // Initialize the dispatch table register.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001149 __ li(kInterpreterDispatchTableRegister,
1150 Operand(ExternalReference::interpreter_dispatch_table_address(
1151 masm->isolate())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001152
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001153 // Get the bytecode array pointer from the frame.
Ben Murdochc5610432016-08-08 18:44:38 +01001154 __ ld(kInterpreterBytecodeArrayRegister,
1155 MemOperand(fp, InterpreterFrameConstants::kBytecodeArrayFromFp));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001156
1157 if (FLAG_debug_code) {
1158 // Check function data field is actually a BytecodeArray object.
1159 __ SmiTst(kInterpreterBytecodeArrayRegister, at);
1160 __ Assert(ne, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, at,
1161 Operand(zero_reg));
1162 __ GetObjectType(kInterpreterBytecodeArrayRegister, a1, a1);
1163 __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, a1,
1164 Operand(BYTECODE_ARRAY_TYPE));
1165 }
1166
1167 // Get the target bytecode offset from the frame.
1168 __ ld(kInterpreterBytecodeOffsetRegister,
Ben Murdochc5610432016-08-08 18:44:38 +01001169 MemOperand(fp, InterpreterFrameConstants::kBytecodeOffsetFromFp));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001170 __ SmiUntag(kInterpreterBytecodeOffsetRegister);
1171
1172 // Dispatch to the target bytecode.
1173 __ Daddu(a1, kInterpreterBytecodeArrayRegister,
1174 kInterpreterBytecodeOffsetRegister);
1175 __ lbu(a1, MemOperand(a1));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001176 __ Dlsa(a1, kInterpreterDispatchTableRegister, a1, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001177 __ ld(a1, MemOperand(a1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001178 __ Jump(a1);
1179}
1180
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001181void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
Ben Murdochc5610432016-08-08 18:44:38 +01001182 // ----------- S t a t e -------------
1183 // -- a0 : argument count (preserved for callee)
1184 // -- a3 : new target (preserved for callee)
1185 // -- a1 : target function (preserved for callee)
1186 // -----------------------------------
1187 // First lookup code, maybe we don't need to compile!
1188 Label gotta_call_runtime, gotta_call_runtime_no_stack;
1189 Label maybe_call_runtime;
1190 Label try_shared;
1191 Label loop_top, loop_bottom;
1192
1193 Register argument_count = a0;
1194 Register closure = a1;
1195 Register new_target = a3;
1196 __ push(argument_count);
1197 __ push(new_target);
1198 __ push(closure);
1199
1200 Register map = a0;
1201 Register index = a2;
1202 __ ld(map, FieldMemOperand(closure, JSFunction::kSharedFunctionInfoOffset));
1203 __ ld(map, FieldMemOperand(map, SharedFunctionInfo::kOptimizedCodeMapOffset));
1204 __ ld(index, FieldMemOperand(map, FixedArray::kLengthOffset));
1205 __ Branch(&gotta_call_runtime, lt, index, Operand(Smi::FromInt(2)));
1206
1207 // Find literals.
1208 // a3 : native context
1209 // a2 : length / index
1210 // a0 : optimized code map
1211 // stack[0] : new target
1212 // stack[4] : closure
1213 Register native_context = a3;
1214 __ ld(native_context, NativeContextMemOperand());
1215
1216 __ bind(&loop_top);
1217 Register temp = a1;
1218 Register array_pointer = a5;
1219
1220 // Does the native context match?
1221 __ SmiScale(at, index, kPointerSizeLog2);
1222 __ Daddu(array_pointer, map, Operand(at));
1223 __ ld(temp, FieldMemOperand(array_pointer,
1224 SharedFunctionInfo::kOffsetToPreviousContext));
1225 __ ld(temp, FieldMemOperand(temp, WeakCell::kValueOffset));
1226 __ Branch(&loop_bottom, ne, temp, Operand(native_context));
1227 // OSR id set to none?
1228 __ ld(temp, FieldMemOperand(array_pointer,
1229 SharedFunctionInfo::kOffsetToPreviousOsrAstId));
1230 const int bailout_id = BailoutId::None().ToInt();
1231 __ Branch(&loop_bottom, ne, temp, Operand(Smi::FromInt(bailout_id)));
1232 // Literals available?
1233 __ ld(temp, FieldMemOperand(array_pointer,
1234 SharedFunctionInfo::kOffsetToPreviousLiterals));
1235 __ ld(temp, FieldMemOperand(temp, WeakCell::kValueOffset));
1236 __ JumpIfSmi(temp, &gotta_call_runtime);
1237
1238 // Save the literals in the closure.
1239 __ ld(a4, MemOperand(sp, 0));
1240 __ sd(temp, FieldMemOperand(a4, JSFunction::kLiteralsOffset));
1241 __ push(index);
1242 __ RecordWriteField(a4, JSFunction::kLiteralsOffset, temp, index,
1243 kRAHasNotBeenSaved, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
1244 OMIT_SMI_CHECK);
1245 __ pop(index);
1246
1247 // Code available?
1248 Register entry = a4;
1249 __ ld(entry,
1250 FieldMemOperand(array_pointer,
1251 SharedFunctionInfo::kOffsetToPreviousCachedCode));
1252 __ ld(entry, FieldMemOperand(entry, WeakCell::kValueOffset));
1253 __ JumpIfSmi(entry, &maybe_call_runtime);
1254
1255 // Found literals and code. Get them into the closure and return.
1256 __ pop(closure);
1257 // Store code entry in the closure.
1258 __ Daddu(entry, entry, Operand(Code::kHeaderSize - kHeapObjectTag));
1259
1260 Label install_optimized_code_and_tailcall;
1261 __ bind(&install_optimized_code_and_tailcall);
1262 __ sd(entry, FieldMemOperand(closure, JSFunction::kCodeEntryOffset));
1263 __ RecordWriteCodeEntryField(closure, entry, a5);
1264
1265 // Link the closure into the optimized function list.
1266 // a4 : code entry
1267 // a3 : native context
1268 // a1 : closure
1269 __ ld(a5,
1270 ContextMemOperand(native_context, Context::OPTIMIZED_FUNCTIONS_LIST));
1271 __ sd(a5, FieldMemOperand(closure, JSFunction::kNextFunctionLinkOffset));
1272 __ RecordWriteField(closure, JSFunction::kNextFunctionLinkOffset, a5, a0,
1273 kRAHasNotBeenSaved, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
1274 OMIT_SMI_CHECK);
1275 const int function_list_offset =
1276 Context::SlotOffset(Context::OPTIMIZED_FUNCTIONS_LIST);
1277 __ sd(closure,
1278 ContextMemOperand(native_context, Context::OPTIMIZED_FUNCTIONS_LIST));
1279 // Save closure before the write barrier.
1280 __ mov(a5, closure);
1281 __ RecordWriteContextSlot(native_context, function_list_offset, closure, a0,
1282 kRAHasNotBeenSaved, kDontSaveFPRegs);
1283 __ mov(closure, a5);
1284 __ pop(new_target);
1285 __ pop(argument_count);
1286 __ Jump(entry);
1287
1288 __ bind(&loop_bottom);
1289 __ Dsubu(index, index,
1290 Operand(Smi::FromInt(SharedFunctionInfo::kEntryLength)));
1291 __ Branch(&loop_top, gt, index, Operand(Smi::FromInt(1)));
1292
1293 // We found neither literals nor code.
1294 __ jmp(&gotta_call_runtime);
1295
1296 __ bind(&maybe_call_runtime);
1297 __ pop(closure);
1298
1299 // Last possibility. Check the context free optimized code map entry.
1300 __ ld(entry, FieldMemOperand(map, FixedArray::kHeaderSize +
1301 SharedFunctionInfo::kSharedCodeIndex));
1302 __ ld(entry, FieldMemOperand(entry, WeakCell::kValueOffset));
1303 __ JumpIfSmi(entry, &try_shared);
1304
1305 // Store code entry in the closure.
1306 __ Daddu(entry, entry, Operand(Code::kHeaderSize - kHeapObjectTag));
1307 __ jmp(&install_optimized_code_and_tailcall);
1308
1309 __ bind(&try_shared);
1310 __ pop(new_target);
1311 __ pop(argument_count);
1312 // Is the full code valid?
1313 __ ld(entry, FieldMemOperand(closure, JSFunction::kSharedFunctionInfoOffset));
1314 __ ld(entry, FieldMemOperand(entry, SharedFunctionInfo::kCodeOffset));
1315 __ lw(a5, FieldMemOperand(entry, Code::kFlagsOffset));
1316 __ And(a5, a5, Operand(Code::KindField::kMask));
1317 __ dsrl(a5, a5, Code::KindField::kShift);
1318 __ Branch(&gotta_call_runtime_no_stack, eq, a5, Operand(Code::BUILTIN));
1319 // Yes, install the full code.
1320 __ Daddu(entry, entry, Operand(Code::kHeaderSize - kHeapObjectTag));
1321 __ sd(entry, FieldMemOperand(closure, JSFunction::kCodeEntryOffset));
1322 __ RecordWriteCodeEntryField(closure, entry, a5);
1323 __ Jump(entry);
1324
1325 __ bind(&gotta_call_runtime);
1326 __ pop(closure);
1327 __ pop(new_target);
1328 __ pop(argument_count);
1329 __ bind(&gotta_call_runtime_no_stack);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001330 GenerateTailCallToReturnedCode(masm, Runtime::kCompileLazy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001331}
1332
Ben Murdochc5610432016-08-08 18:44:38 +01001333void Builtins::Generate_CompileBaseline(MacroAssembler* masm) {
1334 GenerateTailCallToReturnedCode(masm, Runtime::kCompileBaseline);
1335}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001336
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001337void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001338 GenerateTailCallToReturnedCode(masm,
1339 Runtime::kCompileOptimized_NotConcurrent);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001340}
1341
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001342void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001343 GenerateTailCallToReturnedCode(masm, Runtime::kCompileOptimized_Concurrent);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344}
1345
1346
1347static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
1348 // For now, we are relying on the fact that make_code_young doesn't do any
1349 // garbage collection which allows us to save/restore the registers without
1350 // worrying about which of them contain pointers. We also don't build an
1351 // internal frame to make the code faster, since we shouldn't have to do stack
1352 // crawls in MakeCodeYoung. This seems a bit fragile.
1353
1354 // Set a0 to point to the head of the PlatformCodeAge sequence.
1355 __ Dsubu(a0, a0,
1356 Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
1357
1358 // The following registers must be saved and restored when calling through to
1359 // the runtime:
1360 // a0 - contains return address (beginning of patch sequence)
1361 // a1 - isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001362 // a3 - new target
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001363 RegList saved_regs =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001364 (a0.bit() | a1.bit() | a3.bit() | ra.bit() | fp.bit()) & ~sp.bit();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001365 FrameScope scope(masm, StackFrame::MANUAL);
1366 __ MultiPush(saved_regs);
1367 __ PrepareCallCFunction(2, 0, a2);
1368 __ li(a1, Operand(ExternalReference::isolate_address(masm->isolate())));
1369 __ CallCFunction(
1370 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
1371 __ MultiPop(saved_regs);
1372 __ Jump(a0);
1373}
1374
1375#define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
1376void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
1377 MacroAssembler* masm) { \
1378 GenerateMakeCodeYoungAgainCommon(masm); \
1379} \
1380void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
1381 MacroAssembler* masm) { \
1382 GenerateMakeCodeYoungAgainCommon(masm); \
1383}
1384CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
1385#undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
1386
1387
1388void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
1389 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
1390 // that make_code_young doesn't do any garbage collection which allows us to
1391 // save/restore the registers without worrying about which of them contain
1392 // pointers.
1393
1394 // Set a0 to point to the head of the PlatformCodeAge sequence.
1395 __ Dsubu(a0, a0,
1396 Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
1397
1398 // The following registers must be saved and restored when calling through to
1399 // the runtime:
1400 // a0 - contains return address (beginning of patch sequence)
1401 // a1 - isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001402 // a3 - new target
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001403 RegList saved_regs =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001404 (a0.bit() | a1.bit() | a3.bit() | ra.bit() | fp.bit()) & ~sp.bit();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001405 FrameScope scope(masm, StackFrame::MANUAL);
1406 __ MultiPush(saved_regs);
1407 __ PrepareCallCFunction(2, 0, a2);
1408 __ li(a1, Operand(ExternalReference::isolate_address(masm->isolate())));
1409 __ CallCFunction(
1410 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
1411 2);
1412 __ MultiPop(saved_regs);
1413
1414 // Perform prologue operations usually performed by the young code stub.
Ben Murdochda12d292016-06-02 14:46:10 +01001415 __ PushStandardFrame(a1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001416
1417 // Jump to point after the code-age stub.
1418 __ Daddu(a0, a0, Operand((kNoCodeAgeSequenceLength)));
1419 __ Jump(a0);
1420}
1421
1422
1423void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
1424 GenerateMakeCodeYoungAgainCommon(masm);
1425}
1426
1427
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001428void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
1429 Generate_MarkCodeAsExecutedOnce(masm);
1430}
1431
1432
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001433static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
1434 SaveFPRegsMode save_doubles) {
1435 {
1436 FrameScope scope(masm, StackFrame::INTERNAL);
1437
1438 // Preserve registers across notification, this is important for compiled
1439 // stubs that tail call the runtime on deopts passing their parameters in
1440 // registers.
1441 __ MultiPush(kJSCallerSaved | kCalleeSaved);
1442 // Pass the function and deoptimization type to the runtime system.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001443 __ CallRuntime(Runtime::kNotifyStubFailure, save_doubles);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001444 __ MultiPop(kJSCallerSaved | kCalleeSaved);
1445 }
1446
1447 __ Daddu(sp, sp, Operand(kPointerSize)); // Ignore state
1448 __ Jump(ra); // Jump to miss handler
1449}
1450
1451
1452void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
1453 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
1454}
1455
1456
1457void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
1458 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
1459}
1460
1461
1462static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1463 Deoptimizer::BailoutType type) {
1464 {
1465 FrameScope scope(masm, StackFrame::INTERNAL);
1466 // Pass the function and deoptimization type to the runtime system.
1467 __ li(a0, Operand(Smi::FromInt(static_cast<int>(type))));
1468 __ push(a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001469 __ CallRuntime(Runtime::kNotifyDeoptimized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001470 }
1471
1472 // Get the full codegen state from the stack and untag it -> a6.
1473 __ ld(a6, MemOperand(sp, 0 * kPointerSize));
1474 __ SmiUntag(a6);
1475 // Switch on the state.
1476 Label with_tos_register, unknown_state;
Ben Murdochc5610432016-08-08 18:44:38 +01001477 __ Branch(
1478 &with_tos_register, ne, a6,
1479 Operand(static_cast<int64_t>(Deoptimizer::BailoutState::NO_REGISTERS)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001480 __ Ret(USE_DELAY_SLOT);
1481 // Safe to fill delay slot Addu will emit one instruction.
1482 __ Daddu(sp, sp, Operand(1 * kPointerSize)); // Remove state.
1483
1484 __ bind(&with_tos_register);
Ben Murdochc5610432016-08-08 18:44:38 +01001485 DCHECK_EQ(kInterpreterAccumulatorRegister.code(), v0.code());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001486 __ ld(v0, MemOperand(sp, 1 * kPointerSize));
Ben Murdochc5610432016-08-08 18:44:38 +01001487 __ Branch(
1488 &unknown_state, ne, a6,
1489 Operand(static_cast<int64_t>(Deoptimizer::BailoutState::TOS_REGISTER)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001490
1491 __ Ret(USE_DELAY_SLOT);
1492 // Safe to fill delay slot Addu will emit one instruction.
1493 __ Daddu(sp, sp, Operand(2 * kPointerSize)); // Remove state.
1494
1495 __ bind(&unknown_state);
1496 __ stop("no cases left");
1497}
1498
1499
1500void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1501 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1502}
1503
1504
1505void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
1506 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1507}
1508
1509
1510void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1511 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1512}
1513
1514
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001515// Clobbers {t2, t3, a4, a5}.
1516static void CompatibleReceiverCheck(MacroAssembler* masm, Register receiver,
1517 Register function_template_info,
1518 Label* receiver_check_failed) {
1519 Register signature = t2;
1520 Register map = t3;
1521 Register constructor = a4;
1522 Register scratch = a5;
1523
1524 // If there is no signature, return the holder.
1525 __ ld(signature, FieldMemOperand(function_template_info,
1526 FunctionTemplateInfo::kSignatureOffset));
1527 Label receiver_check_passed;
1528 __ JumpIfRoot(signature, Heap::kUndefinedValueRootIndex,
1529 &receiver_check_passed);
1530
1531 // Walk the prototype chain.
1532 __ ld(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
1533 Label prototype_loop_start;
1534 __ bind(&prototype_loop_start);
1535
1536 // Get the constructor, if any.
1537 __ GetMapConstructor(constructor, map, scratch, scratch);
1538 Label next_prototype;
1539 __ Branch(&next_prototype, ne, scratch, Operand(JS_FUNCTION_TYPE));
1540 Register type = constructor;
1541 __ ld(type,
1542 FieldMemOperand(constructor, JSFunction::kSharedFunctionInfoOffset));
1543 __ ld(type, FieldMemOperand(type, SharedFunctionInfo::kFunctionDataOffset));
1544
1545 // Loop through the chain of inheriting function templates.
1546 Label function_template_loop;
1547 __ bind(&function_template_loop);
1548
1549 // If the signatures match, we have a compatible receiver.
1550 __ Branch(&receiver_check_passed, eq, signature, Operand(type),
1551 USE_DELAY_SLOT);
1552
1553 // If the current type is not a FunctionTemplateInfo, load the next prototype
1554 // in the chain.
1555 __ JumpIfSmi(type, &next_prototype);
1556 __ GetObjectType(type, scratch, scratch);
1557 __ Branch(&next_prototype, ne, scratch, Operand(FUNCTION_TEMPLATE_INFO_TYPE));
1558
1559 // Otherwise load the parent function template and iterate.
1560 __ ld(type,
1561 FieldMemOperand(type, FunctionTemplateInfo::kParentTemplateOffset));
1562 __ Branch(&function_template_loop);
1563
1564 // Load the next prototype.
1565 __ bind(&next_prototype);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001566 __ lwu(scratch, FieldMemOperand(map, Map::kBitField3Offset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001567 __ DecodeField<Map::HasHiddenPrototype>(scratch);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001568 __ Branch(receiver_check_failed, eq, scratch, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001569
1570 __ ld(receiver, FieldMemOperand(map, Map::kPrototypeOffset));
1571 __ ld(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001572 // Iterate.
1573 __ Branch(&prototype_loop_start);
1574
1575 __ bind(&receiver_check_passed);
1576}
1577
1578
1579void Builtins::Generate_HandleFastApiCall(MacroAssembler* masm) {
1580 // ----------- S t a t e -------------
1581 // -- a0 : number of arguments excluding receiver
1582 // -- a1 : callee
1583 // -- ra : return address
1584 // -- sp[0] : last argument
1585 // -- ...
1586 // -- sp[8 * (argc - 1)] : first argument
1587 // -- sp[8 * argc] : receiver
1588 // -----------------------------------
1589
1590 // Load the FunctionTemplateInfo.
1591 __ ld(t1, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1592 __ ld(t1, FieldMemOperand(t1, SharedFunctionInfo::kFunctionDataOffset));
1593
1594 // Do the compatible receiver check
1595 Label receiver_check_failed;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001596 __ Dlsa(t8, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001597 __ ld(t0, MemOperand(t8));
1598 CompatibleReceiverCheck(masm, t0, t1, &receiver_check_failed);
1599
1600 // Get the callback offset from the FunctionTemplateInfo, and jump to the
1601 // beginning of the code.
1602 __ ld(t2, FieldMemOperand(t1, FunctionTemplateInfo::kCallCodeOffset));
1603 __ ld(t2, FieldMemOperand(t2, CallHandlerInfo::kFastHandlerOffset));
1604 __ Daddu(t2, t2, Operand(Code::kHeaderSize - kHeapObjectTag));
1605 __ Jump(t2);
1606
1607 // Compatible receiver check failed: throw an Illegal Invocation exception.
1608 __ bind(&receiver_check_failed);
1609 // Drop the arguments (including the receiver);
1610 __ Daddu(t8, t8, Operand(kPointerSize));
1611 __ daddu(sp, t8, zero_reg);
1612 __ TailCallRuntime(Runtime::kThrowIllegalInvocation);
1613}
1614
1615
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001616void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1617 // Lookup the function in the JavaScript frame.
1618 __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1619 {
1620 FrameScope scope(masm, StackFrame::INTERNAL);
1621 // Pass function as argument.
1622 __ push(a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001623 __ CallRuntime(Runtime::kCompileForOnStackReplacement);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001624 }
1625
1626 // If the code object is null, just return to the unoptimized code.
1627 __ Ret(eq, v0, Operand(Smi::FromInt(0)));
1628
1629 // Load deoptimization data from the code object.
1630 // <deopt_data> = <code>[#deoptimization_data_offset]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001631 __ ld(a1, MemOperand(v0, Code::kDeoptimizationDataOffset - kHeapObjectTag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001632
1633 // Load the OSR entrypoint offset from the deoptimization data.
1634 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1635 __ ld(a1, MemOperand(a1, FixedArray::OffsetOfElementAt(
1636 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1637 __ SmiUntag(a1);
1638
1639 // Compute the target address = code_obj + header_size + osr_offset
1640 // <entry_addr> = <code_obj> + #header_size + <osr_offset>
1641 __ daddu(v0, v0, a1);
1642 __ daddiu(ra, v0, Code::kHeaderSize - kHeapObjectTag);
1643
1644 // And "return" to the OSR entry point of the function.
1645 __ Ret();
1646}
1647
1648
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001649// static
1650void Builtins::Generate_DatePrototype_GetField(MacroAssembler* masm,
1651 int field_index) {
1652 // ----------- S t a t e -------------
1653 // -- sp[0] : receiver
1654 // -----------------------------------
1655
1656 // 1. Pop receiver into a0 and check that it's actually a JSDate object.
1657 Label receiver_not_date;
1658 {
1659 __ Pop(a0);
1660 __ JumpIfSmi(a0, &receiver_not_date);
1661 __ GetObjectType(a0, t0, t0);
1662 __ Branch(&receiver_not_date, ne, t0, Operand(JS_DATE_TYPE));
1663 }
1664
1665 // 2. Load the specified date field, falling back to the runtime as necessary.
1666 if (field_index == JSDate::kDateValue) {
1667 __ Ret(USE_DELAY_SLOT);
1668 __ ld(v0, FieldMemOperand(a0, JSDate::kValueOffset)); // In delay slot.
1669 } else {
1670 if (field_index < JSDate::kFirstUncachedField) {
1671 Label stamp_mismatch;
1672 __ li(a1, Operand(ExternalReference::date_cache_stamp(masm->isolate())));
1673 __ ld(a1, MemOperand(a1));
1674 __ ld(t0, FieldMemOperand(a0, JSDate::kCacheStampOffset));
1675 __ Branch(&stamp_mismatch, ne, t0, Operand(a1));
1676 __ Ret(USE_DELAY_SLOT);
1677 __ ld(v0, FieldMemOperand(
1678 a0, JSDate::kValueOffset +
1679 field_index * kPointerSize)); // In delay slot.
1680 __ bind(&stamp_mismatch);
1681 }
1682 FrameScope scope(masm, StackFrame::INTERNAL);
1683 __ PrepareCallCFunction(2, t0);
1684 __ li(a1, Operand(Smi::FromInt(field_index)));
1685 __ CallCFunction(
1686 ExternalReference::get_date_field_function(masm->isolate()), 2);
1687 }
1688 __ Ret();
1689
1690 // 3. Raise a TypeError if the receiver is not a date.
1691 __ bind(&receiver_not_date);
1692 __ TailCallRuntime(Runtime::kThrowNotDateError);
1693}
1694
Ben Murdochda12d292016-06-02 14:46:10 +01001695// static
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001696void Builtins::Generate_FunctionPrototypeApply(MacroAssembler* masm) {
1697 // ----------- S t a t e -------------
1698 // -- a0 : argc
1699 // -- sp[0] : argArray
1700 // -- sp[4] : thisArg
1701 // -- sp[8] : receiver
1702 // -----------------------------------
1703
1704 // 1. Load receiver into a1, argArray into a0 (if present), remove all
1705 // arguments from the stack (including the receiver), and push thisArg (if
1706 // present) instead.
1707 {
1708 Label no_arg;
1709 Register scratch = a4;
1710 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1711 __ mov(a3, a2);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001712 // Dlsa() cannot be used hare as scratch value used later.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001713 __ dsll(scratch, a0, kPointerSizeLog2);
1714 __ Daddu(a0, sp, Operand(scratch));
1715 __ ld(a1, MemOperand(a0)); // receiver
1716 __ Dsubu(a0, a0, Operand(kPointerSize));
1717 __ Branch(&no_arg, lt, a0, Operand(sp));
1718 __ ld(a2, MemOperand(a0)); // thisArg
1719 __ Dsubu(a0, a0, Operand(kPointerSize));
1720 __ Branch(&no_arg, lt, a0, Operand(sp));
1721 __ ld(a3, MemOperand(a0)); // argArray
1722 __ bind(&no_arg);
1723 __ Daddu(sp, sp, Operand(scratch));
1724 __ sd(a2, MemOperand(sp));
1725 __ mov(a0, a3);
1726 }
1727
1728 // ----------- S t a t e -------------
1729 // -- a0 : argArray
1730 // -- a1 : receiver
1731 // -- sp[0] : thisArg
1732 // -----------------------------------
1733
1734 // 2. Make sure the receiver is actually callable.
1735 Label receiver_not_callable;
1736 __ JumpIfSmi(a1, &receiver_not_callable);
1737 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1738 __ lbu(a4, FieldMemOperand(a4, Map::kBitFieldOffset));
1739 __ And(a4, a4, Operand(1 << Map::kIsCallable));
1740 __ Branch(&receiver_not_callable, eq, a4, Operand(zero_reg));
1741
1742 // 3. Tail call with no arguments if argArray is null or undefined.
1743 Label no_arguments;
1744 __ JumpIfRoot(a0, Heap::kNullValueRootIndex, &no_arguments);
1745 __ JumpIfRoot(a0, Heap::kUndefinedValueRootIndex, &no_arguments);
1746
1747 // 4a. Apply the receiver to the given argArray (passing undefined for
1748 // new.target).
1749 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
1750 __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1751
1752 // 4b. The argArray is either null or undefined, so we tail call without any
1753 // arguments to the receiver.
1754 __ bind(&no_arguments);
1755 {
1756 __ mov(a0, zero_reg);
1757 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1758 }
1759
1760 // 4c. The receiver is not callable, throw an appropriate TypeError.
1761 __ bind(&receiver_not_callable);
1762 {
1763 __ sd(a1, MemOperand(sp));
1764 __ TailCallRuntime(Runtime::kThrowApplyNonFunction);
1765 }
1766}
1767
1768
1769// static
1770void Builtins::Generate_FunctionPrototypeCall(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001771 // 1. Make sure we have at least one argument.
1772 // a0: actual number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001773 {
1774 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001775 __ Branch(&done, ne, a0, Operand(zero_reg));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001776 __ PushRoot(Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001777 __ Daddu(a0, a0, Operand(1));
1778 __ bind(&done);
1779 }
1780
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001781 // 2. Get the function to call (passed as receiver) from the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001782 // a0: actual number of arguments
Ben Murdoch097c5b22016-05-18 11:27:45 +01001783 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001784 __ ld(a1, MemOperand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001785
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001786 // 3. Shift arguments and return address one slot down on the stack
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001787 // (overwriting the original receiver). Adjust argument count to make
1788 // the original first argument the new receiver.
1789 // a0: actual number of arguments
1790 // a1: function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001791 {
1792 Label loop;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001793 // Calculate the copy start address (destination). Copy end address is sp.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001794 __ Dlsa(a2, sp, a0, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001795
1796 __ bind(&loop);
1797 __ ld(at, MemOperand(a2, -kPointerSize));
1798 __ sd(at, MemOperand(a2));
1799 __ Dsubu(a2, a2, Operand(kPointerSize));
1800 __ Branch(&loop, ne, a2, Operand(sp));
1801 // Adjust the actual number of arguments and remove the top element
1802 // (which is a copy of the last argument).
1803 __ Dsubu(a0, a0, Operand(1));
1804 __ Pop();
1805 }
1806
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001807 // 4. Call the callable.
1808 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001809}
1810
1811
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001812void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1813 // ----------- S t a t e -------------
1814 // -- a0 : argc
1815 // -- sp[0] : argumentsList
1816 // -- sp[4] : thisArgument
1817 // -- sp[8] : target
1818 // -- sp[12] : receiver
1819 // -----------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001820
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001821 // 1. Load target into a1 (if present), argumentsList into a0 (if present),
1822 // remove all arguments from the stack (including the receiver), and push
1823 // thisArgument (if present) instead.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001824 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001825 Label no_arg;
1826 Register scratch = a4;
1827 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
1828 __ mov(a2, a1);
1829 __ mov(a3, a1);
1830 __ dsll(scratch, a0, kPointerSizeLog2);
1831 __ mov(a0, scratch);
1832 __ Dsubu(a0, a0, Operand(kPointerSize));
1833 __ Branch(&no_arg, lt, a0, Operand(zero_reg));
1834 __ Daddu(a0, sp, Operand(a0));
1835 __ ld(a1, MemOperand(a0)); // target
1836 __ Dsubu(a0, a0, Operand(kPointerSize));
1837 __ Branch(&no_arg, lt, a0, Operand(sp));
1838 __ ld(a2, MemOperand(a0)); // thisArgument
1839 __ Dsubu(a0, a0, Operand(kPointerSize));
1840 __ Branch(&no_arg, lt, a0, Operand(sp));
1841 __ ld(a3, MemOperand(a0)); // argumentsList
1842 __ bind(&no_arg);
1843 __ Daddu(sp, sp, Operand(scratch));
1844 __ sd(a2, MemOperand(sp));
1845 __ mov(a0, a3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001846 }
1847
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001848 // ----------- S t a t e -------------
1849 // -- a0 : argumentsList
1850 // -- a1 : target
1851 // -- sp[0] : thisArgument
1852 // -----------------------------------
1853
1854 // 2. Make sure the target is actually callable.
1855 Label target_not_callable;
1856 __ JumpIfSmi(a1, &target_not_callable);
1857 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1858 __ lbu(a4, FieldMemOperand(a4, Map::kBitFieldOffset));
1859 __ And(a4, a4, Operand(1 << Map::kIsCallable));
1860 __ Branch(&target_not_callable, eq, a4, Operand(zero_reg));
1861
1862 // 3a. Apply the target to the given argumentsList (passing undefined for
1863 // new.target).
1864 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
1865 __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1866
1867 // 3b. The target is not callable, throw an appropriate TypeError.
1868 __ bind(&target_not_callable);
1869 {
1870 __ sd(a1, MemOperand(sp));
1871 __ TailCallRuntime(Runtime::kThrowApplyNonFunction);
1872 }
1873}
1874
1875
1876void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1877 // ----------- S t a t e -------------
1878 // -- a0 : argc
1879 // -- sp[0] : new.target (optional)
1880 // -- sp[4] : argumentsList
1881 // -- sp[8] : target
1882 // -- sp[12] : receiver
1883 // -----------------------------------
1884
1885 // 1. Load target into a1 (if present), argumentsList into a0 (if present),
1886 // new.target into a3 (if present, otherwise use target), remove all
1887 // arguments from the stack (including the receiver), and push thisArgument
1888 // (if present) instead.
1889 {
1890 Label no_arg;
1891 Register scratch = a4;
1892 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
1893 __ mov(a2, a1);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001894 // Dlsa() cannot be used hare as scratch value used later.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001895 __ dsll(scratch, a0, kPointerSizeLog2);
1896 __ Daddu(a0, sp, Operand(scratch));
1897 __ sd(a2, MemOperand(a0)); // receiver
1898 __ Dsubu(a0, a0, Operand(kPointerSize));
1899 __ Branch(&no_arg, lt, a0, Operand(sp));
1900 __ ld(a1, MemOperand(a0)); // target
1901 __ mov(a3, a1); // new.target defaults to target
1902 __ Dsubu(a0, a0, Operand(kPointerSize));
1903 __ Branch(&no_arg, lt, a0, Operand(sp));
1904 __ ld(a2, MemOperand(a0)); // argumentsList
1905 __ Dsubu(a0, a0, Operand(kPointerSize));
1906 __ Branch(&no_arg, lt, a0, Operand(sp));
1907 __ ld(a3, MemOperand(a0)); // new.target
1908 __ bind(&no_arg);
1909 __ Daddu(sp, sp, Operand(scratch));
1910 __ mov(a0, a2);
1911 }
1912
1913 // ----------- S t a t e -------------
1914 // -- a0 : argumentsList
1915 // -- a3 : new.target
1916 // -- a1 : target
1917 // -- sp[0] : receiver (undefined)
1918 // -----------------------------------
1919
1920 // 2. Make sure the target is actually a constructor.
1921 Label target_not_constructor;
1922 __ JumpIfSmi(a1, &target_not_constructor);
1923 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1924 __ lbu(a4, FieldMemOperand(a4, Map::kBitFieldOffset));
1925 __ And(a4, a4, Operand(1 << Map::kIsConstructor));
1926 __ Branch(&target_not_constructor, eq, a4, Operand(zero_reg));
1927
1928 // 3. Make sure the target is actually a constructor.
1929 Label new_target_not_constructor;
1930 __ JumpIfSmi(a3, &new_target_not_constructor);
1931 __ ld(a4, FieldMemOperand(a3, HeapObject::kMapOffset));
1932 __ lbu(a4, FieldMemOperand(a4, Map::kBitFieldOffset));
1933 __ And(a4, a4, Operand(1 << Map::kIsConstructor));
1934 __ Branch(&new_target_not_constructor, eq, a4, Operand(zero_reg));
1935
1936 // 4a. Construct the target with the given new.target and argumentsList.
1937 __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1938
1939 // 4b. The target is not a constructor, throw an appropriate TypeError.
1940 __ bind(&target_not_constructor);
1941 {
1942 __ sd(a1, MemOperand(sp));
1943 __ TailCallRuntime(Runtime::kThrowCalledNonCallable);
1944 }
1945
1946 // 4c. The new.target is not a constructor, throw an appropriate TypeError.
1947 __ bind(&new_target_not_constructor);
1948 {
1949 __ sd(a3, MemOperand(sp));
1950 __ TailCallRuntime(Runtime::kThrowCalledNonCallable);
1951 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001952}
1953
1954
1955static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1956 Label* stack_overflow) {
1957 // ----------- S t a t e -------------
1958 // -- a0 : actual number of arguments
1959 // -- a1 : function (passed through to callee)
1960 // -- a2 : expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001961 // -- a3 : new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001962 // -----------------------------------
1963 // Check the stack for overflow. We are not trying to catch
1964 // interruptions (e.g. debug break and preemption) here, so the "real stack
1965 // limit" is checked.
1966 __ LoadRoot(a5, Heap::kRealStackLimitRootIndex);
1967 // Make a5 the space we have left. The stack might already be overflowed
1968 // here which will cause a5 to become negative.
1969 __ dsubu(a5, sp, a5);
1970 // Check if the arguments will overflow the stack.
1971 __ dsll(at, a2, kPointerSizeLog2);
1972 // Signed comparison.
1973 __ Branch(stack_overflow, le, a5, Operand(at));
1974}
1975
1976
1977static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1978 // __ sll(a0, a0, kSmiTagSize);
1979 __ dsll32(a0, a0, 0);
1980 __ li(a4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1981 __ MultiPush(a0.bit() | a1.bit() | a4.bit() | fp.bit() | ra.bit());
1982 __ Daddu(fp, sp,
1983 Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
1984}
1985
1986
1987static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1988 // ----------- S t a t e -------------
1989 // -- v0 : result being passed through
1990 // -----------------------------------
1991 // Get the number of arguments passed (as a smi), tear down the frame and
1992 // then tear down the parameters.
1993 __ ld(a1, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1994 kPointerSize)));
1995 __ mov(sp, fp);
1996 __ MultiPop(fp.bit() | ra.bit());
1997 __ SmiScale(a4, a1, kPointerSizeLog2);
1998 __ Daddu(sp, sp, a4);
1999 // Adjust for the receiver.
2000 __ Daddu(sp, sp, Operand(kPointerSize));
2001}
2002
2003
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002004// static
2005void Builtins::Generate_Apply(MacroAssembler* masm) {
2006 // ----------- S t a t e -------------
2007 // -- a0 : argumentsList
2008 // -- a1 : target
2009 // -- a3 : new.target (checked to be constructor or undefined)
2010 // -- sp[0] : thisArgument
2011 // -----------------------------------
2012
2013 // Create the list of arguments from the array-like argumentsList.
2014 {
2015 Label create_arguments, create_array, create_runtime, done_create;
2016 __ JumpIfSmi(a0, &create_runtime);
2017
2018 // Load the map of argumentsList into a2.
2019 __ ld(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
2020
2021 // Load native context into a4.
2022 __ ld(a4, NativeContextMemOperand());
2023
2024 // Check if argumentsList is an (unmodified) arguments object.
2025 __ ld(at, ContextMemOperand(a4, Context::SLOPPY_ARGUMENTS_MAP_INDEX));
2026 __ Branch(&create_arguments, eq, a2, Operand(at));
2027 __ ld(at, ContextMemOperand(a4, Context::STRICT_ARGUMENTS_MAP_INDEX));
2028 __ Branch(&create_arguments, eq, a2, Operand(at));
2029
2030 // Check if argumentsList is a fast JSArray.
2031 __ ld(v0, FieldMemOperand(a2, HeapObject::kMapOffset));
2032 __ lbu(v0, FieldMemOperand(v0, Map::kInstanceTypeOffset));
2033 __ Branch(&create_array, eq, v0, Operand(JS_ARRAY_TYPE));
2034
2035 // Ask the runtime to create the list (actually a FixedArray).
2036 __ bind(&create_runtime);
2037 {
2038 FrameScope scope(masm, StackFrame::INTERNAL);
2039 __ Push(a1, a3, a0);
2040 __ CallRuntime(Runtime::kCreateListFromArrayLike);
2041 __ mov(a0, v0);
2042 __ Pop(a1, a3);
2043 __ ld(a2, FieldMemOperand(v0, FixedArray::kLengthOffset));
2044 __ SmiUntag(a2);
2045 }
2046 __ Branch(&done_create);
2047
2048 // Try to create the list from an arguments object.
2049 __ bind(&create_arguments);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002050 __ ld(a2, FieldMemOperand(a0, JSArgumentsObject::kLengthOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002051 __ ld(a4, FieldMemOperand(a0, JSObject::kElementsOffset));
2052 __ ld(at, FieldMemOperand(a4, FixedArray::kLengthOffset));
2053 __ Branch(&create_runtime, ne, a2, Operand(at));
2054 __ SmiUntag(a2);
2055 __ mov(a0, a4);
2056 __ Branch(&done_create);
2057
2058 // Try to create the list from a JSArray object.
2059 __ bind(&create_array);
2060 __ ld(a2, FieldMemOperand(a2, Map::kBitField2Offset));
2061 __ DecodeField<Map::ElementsKindBits>(a2);
2062 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
2063 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
2064 STATIC_ASSERT(FAST_ELEMENTS == 2);
2065 __ Branch(&create_runtime, hi, a2, Operand(FAST_ELEMENTS));
2066 __ Branch(&create_runtime, eq, a2, Operand(FAST_HOLEY_SMI_ELEMENTS));
2067 __ ld(a2, FieldMemOperand(a0, JSArray::kLengthOffset));
2068 __ ld(a0, FieldMemOperand(a0, JSArray::kElementsOffset));
2069 __ SmiUntag(a2);
2070
2071 __ bind(&done_create);
2072 }
2073
2074 // Check for stack overflow.
2075 {
2076 // Check the stack for overflow. We are not trying to catch interruptions
2077 // (i.e. debug break and preemption) here, so check the "real stack limit".
2078 Label done;
2079 __ LoadRoot(a4, Heap::kRealStackLimitRootIndex);
2080 // Make ip the space we have left. The stack might already be overflowed
2081 // here which will cause ip to become negative.
2082 __ Dsubu(a4, sp, a4);
2083 // Check if the arguments will overflow the stack.
2084 __ dsll(at, a2, kPointerSizeLog2);
2085 __ Branch(&done, gt, a4, Operand(at)); // Signed comparison.
2086 __ TailCallRuntime(Runtime::kThrowStackOverflow);
2087 __ bind(&done);
2088 }
2089
2090 // ----------- S t a t e -------------
2091 // -- a1 : target
2092 // -- a0 : args (a FixedArray built from argumentsList)
2093 // -- a2 : len (number of elements to push from args)
2094 // -- a3 : new.target (checked to be constructor or undefined)
2095 // -- sp[0] : thisArgument
2096 // -----------------------------------
2097
2098 // Push arguments onto the stack (thisArgument is already on the stack).
2099 {
2100 __ mov(a4, zero_reg);
2101 Label done, loop;
2102 __ bind(&loop);
2103 __ Branch(&done, eq, a4, Operand(a2));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002104 __ Dlsa(at, a0, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002105 __ ld(at, FieldMemOperand(at, FixedArray::kHeaderSize));
2106 __ Push(at);
2107 __ Daddu(a4, a4, Operand(1));
2108 __ Branch(&loop);
2109 __ bind(&done);
2110 __ Move(a0, a4);
2111 }
2112
2113 // Dispatch to Call or Construct depending on whether new.target is undefined.
2114 {
2115 Label construct;
2116 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
2117 __ Branch(&construct, ne, a3, Operand(at));
2118 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
2119 __ bind(&construct);
2120 __ Jump(masm->isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
2121 }
2122}
2123
Ben Murdoch097c5b22016-05-18 11:27:45 +01002124namespace {
2125
2126// Drops top JavaScript frame and an arguments adaptor frame below it (if
2127// present) preserving all the arguments prepared for current call.
2128// Does nothing if debugger is currently active.
2129// ES6 14.6.3. PrepareForTailCall
2130//
2131// Stack structure for the function g() tail calling f():
2132//
2133// ------- Caller frame: -------
2134// | ...
2135// | g()'s arg M
2136// | ...
2137// | g()'s arg 1
2138// | g()'s receiver arg
2139// | g()'s caller pc
2140// ------- g()'s frame: -------
2141// | g()'s caller fp <- fp
2142// | g()'s context
2143// | function pointer: g
2144// | -------------------------
2145// | ...
2146// | ...
2147// | f()'s arg N
2148// | ...
2149// | f()'s arg 1
2150// | f()'s receiver arg <- sp (f()'s caller pc is not on the stack yet!)
2151// ----------------------
2152//
2153void PrepareForTailCall(MacroAssembler* masm, Register args_reg,
2154 Register scratch1, Register scratch2,
2155 Register scratch3) {
2156 DCHECK(!AreAliased(args_reg, scratch1, scratch2, scratch3));
2157 Comment cmnt(masm, "[ PrepareForTailCall");
2158
Ben Murdochda12d292016-06-02 14:46:10 +01002159 // Prepare for tail call only if ES2015 tail call elimination is enabled.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002160 Label done;
Ben Murdochda12d292016-06-02 14:46:10 +01002161 ExternalReference is_tail_call_elimination_enabled =
2162 ExternalReference::is_tail_call_elimination_enabled_address(
2163 masm->isolate());
2164 __ li(at, Operand(is_tail_call_elimination_enabled));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002165 __ lb(scratch1, MemOperand(at));
Ben Murdochda12d292016-06-02 14:46:10 +01002166 __ Branch(&done, eq, scratch1, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002167
2168 // Drop possible interpreter handler/stub frame.
2169 {
2170 Label no_interpreter_frame;
Ben Murdochda12d292016-06-02 14:46:10 +01002171 __ ld(scratch3,
2172 MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002173 __ Branch(&no_interpreter_frame, ne, scratch3,
2174 Operand(Smi::FromInt(StackFrame::STUB)));
2175 __ ld(fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
2176 __ bind(&no_interpreter_frame);
2177 }
2178
2179 // Check if next frame is an arguments adaptor frame.
Ben Murdochda12d292016-06-02 14:46:10 +01002180 Register caller_args_count_reg = scratch1;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002181 Label no_arguments_adaptor, formal_parameter_count_loaded;
2182 __ ld(scratch2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01002183 __ ld(scratch3,
2184 MemOperand(scratch2, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002185 __ Branch(&no_arguments_adaptor, ne, scratch3,
2186 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
2187
Ben Murdochda12d292016-06-02 14:46:10 +01002188 // Drop current frame and load arguments count from arguments adaptor frame.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002189 __ mov(fp, scratch2);
Ben Murdochda12d292016-06-02 14:46:10 +01002190 __ ld(caller_args_count_reg,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002191 MemOperand(fp, ArgumentsAdaptorFrameConstants::kLengthOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01002192 __ SmiUntag(caller_args_count_reg);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002193 __ Branch(&formal_parameter_count_loaded);
2194
2195 __ bind(&no_arguments_adaptor);
2196 // Load caller's formal parameter count
Ben Murdochda12d292016-06-02 14:46:10 +01002197 __ ld(scratch1,
2198 MemOperand(fp, ArgumentsAdaptorFrameConstants::kFunctionOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002199 __ ld(scratch1,
2200 FieldMemOperand(scratch1, JSFunction::kSharedFunctionInfoOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01002201 __ lw(caller_args_count_reg,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002202 FieldMemOperand(scratch1,
2203 SharedFunctionInfo::kFormalParameterCountOffset));
2204
2205 __ bind(&formal_parameter_count_loaded);
2206
Ben Murdochda12d292016-06-02 14:46:10 +01002207 ParameterCount callee_args_count(args_reg);
2208 __ PrepareForTailCall(callee_args_count, caller_args_count_reg, scratch2,
2209 scratch3);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002210 __ bind(&done);
2211}
2212} // namespace
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002213
2214// static
2215void Builtins::Generate_CallFunction(MacroAssembler* masm,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002216 ConvertReceiverMode mode,
2217 TailCallMode tail_call_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002218 // ----------- S t a t e -------------
2219 // -- a0 : the number of arguments (not including the receiver)
2220 // -- a1 : the function to call (checked to be a JSFunction)
2221 // -----------------------------------
2222 __ AssertFunction(a1);
2223
2224 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
2225 // Check that function is not a "classConstructor".
2226 Label class_constructor;
2227 __ ld(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2228 __ lbu(a3, FieldMemOperand(a2, SharedFunctionInfo::kFunctionKindByteOffset));
2229 __ And(at, a3, Operand(SharedFunctionInfo::kClassConstructorBitsWithinByte));
2230 __ Branch(&class_constructor, ne, at, Operand(zero_reg));
2231
2232 // Enter the context of the function; ToObject has to run in the function
2233 // context, and we also need to take the global proxy from the function
2234 // context in case of conversion.
2235 STATIC_ASSERT(SharedFunctionInfo::kNativeByteOffset ==
2236 SharedFunctionInfo::kStrictModeByteOffset);
2237 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
2238 // We need to convert the receiver for non-native sloppy mode functions.
2239 Label done_convert;
2240 __ lbu(a3, FieldMemOperand(a2, SharedFunctionInfo::kNativeByteOffset));
2241 __ And(at, a3, Operand((1 << SharedFunctionInfo::kNativeBitWithinByte) |
2242 (1 << SharedFunctionInfo::kStrictModeBitWithinByte)));
2243 __ Branch(&done_convert, ne, at, Operand(zero_reg));
2244 {
2245 // ----------- S t a t e -------------
2246 // -- a0 : the number of arguments (not including the receiver)
2247 // -- a1 : the function to call (checked to be a JSFunction)
2248 // -- a2 : the shared function info.
2249 // -- cp : the function context.
2250 // -----------------------------------
2251
2252 if (mode == ConvertReceiverMode::kNullOrUndefined) {
2253 // Patch receiver to global proxy.
2254 __ LoadGlobalProxy(a3);
2255 } else {
2256 Label convert_to_object, convert_receiver;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002257 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002258 __ ld(a3, MemOperand(at));
2259 __ JumpIfSmi(a3, &convert_to_object);
2260 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
2261 __ GetObjectType(a3, a4, a4);
2262 __ Branch(&done_convert, hs, a4, Operand(FIRST_JS_RECEIVER_TYPE));
2263 if (mode != ConvertReceiverMode::kNotNullOrUndefined) {
2264 Label convert_global_proxy;
2265 __ JumpIfRoot(a3, Heap::kUndefinedValueRootIndex,
2266 &convert_global_proxy);
2267 __ JumpIfNotRoot(a3, Heap::kNullValueRootIndex, &convert_to_object);
2268 __ bind(&convert_global_proxy);
2269 {
2270 // Patch receiver to global proxy.
2271 __ LoadGlobalProxy(a3);
2272 }
2273 __ Branch(&convert_receiver);
2274 }
2275 __ bind(&convert_to_object);
2276 {
2277 // Convert receiver using ToObject.
2278 // TODO(bmeurer): Inline the allocation here to avoid building the frame
2279 // in the fast case? (fall back to AllocateInNewSpace?)
2280 FrameScope scope(masm, StackFrame::INTERNAL);
2281 __ SmiTag(a0);
2282 __ Push(a0, a1);
2283 __ mov(a0, a3);
2284 ToObjectStub stub(masm->isolate());
2285 __ CallStub(&stub);
2286 __ mov(a3, v0);
2287 __ Pop(a0, a1);
2288 __ SmiUntag(a0);
2289 }
2290 __ ld(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2291 __ bind(&convert_receiver);
2292 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002293 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002294 __ sd(a3, MemOperand(at));
2295 }
2296 __ bind(&done_convert);
2297
2298 // ----------- S t a t e -------------
2299 // -- a0 : the number of arguments (not including the receiver)
2300 // -- a1 : the function to call (checked to be a JSFunction)
2301 // -- a2 : the shared function info.
2302 // -- cp : the function context.
2303 // -----------------------------------
2304
Ben Murdoch097c5b22016-05-18 11:27:45 +01002305 if (tail_call_mode == TailCallMode::kAllow) {
2306 PrepareForTailCall(masm, a0, t0, t1, t2);
2307 }
2308
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002309 __ lw(a2,
2310 FieldMemOperand(a2, SharedFunctionInfo::kFormalParameterCountOffset));
2311 ParameterCount actual(a0);
2312 ParameterCount expected(a2);
2313 __ InvokeFunctionCode(a1, no_reg, expected, actual, JUMP_FUNCTION,
2314 CheckDebugStepCallWrapper());
2315
2316 // The function is a "classConstructor", need to raise an exception.
2317 __ bind(&class_constructor);
2318 {
2319 FrameScope frame(masm, StackFrame::INTERNAL);
2320 __ Push(a1);
2321 __ CallRuntime(Runtime::kThrowConstructorNonCallableError);
2322 }
2323}
2324
2325
2326// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01002327void Builtins::Generate_CallBoundFunctionImpl(MacroAssembler* masm,
2328 TailCallMode tail_call_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002329 // ----------- S t a t e -------------
2330 // -- a0 : the number of arguments (not including the receiver)
2331 // -- a1 : the function to call (checked to be a JSBoundFunction)
2332 // -----------------------------------
2333 __ AssertBoundFunction(a1);
2334
Ben Murdoch097c5b22016-05-18 11:27:45 +01002335 if (tail_call_mode == TailCallMode::kAllow) {
2336 PrepareForTailCall(masm, a0, t0, t1, t2);
2337 }
2338
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002339 // Patch the receiver to [[BoundThis]].
2340 {
2341 __ ld(at, FieldMemOperand(a1, JSBoundFunction::kBoundThisOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002342 __ Dlsa(a4, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002343 __ sd(at, MemOperand(a4));
2344 }
2345
2346 // Load [[BoundArguments]] into a2 and length of that into a4.
2347 __ ld(a2, FieldMemOperand(a1, JSBoundFunction::kBoundArgumentsOffset));
2348 __ ld(a4, FieldMemOperand(a2, FixedArray::kLengthOffset));
2349 __ SmiUntag(a4);
2350
2351 // ----------- S t a t e -------------
2352 // -- a0 : the number of arguments (not including the receiver)
2353 // -- a1 : the function to call (checked to be a JSBoundFunction)
2354 // -- a2 : the [[BoundArguments]] (implemented as FixedArray)
2355 // -- a4 : the number of [[BoundArguments]]
2356 // -----------------------------------
2357
2358 // Reserve stack space for the [[BoundArguments]].
2359 {
2360 Label done;
2361 __ dsll(a5, a4, kPointerSizeLog2);
2362 __ Dsubu(sp, sp, Operand(a5));
2363 // Check the stack for overflow. We are not trying to catch interruptions
2364 // (i.e. debug break and preemption) here, so check the "real stack limit".
2365 __ LoadRoot(at, Heap::kRealStackLimitRootIndex);
2366 __ Branch(&done, gt, sp, Operand(at)); // Signed comparison.
2367 // Restore the stack pointer.
2368 __ Daddu(sp, sp, Operand(a5));
2369 {
2370 FrameScope scope(masm, StackFrame::MANUAL);
2371 __ EnterFrame(StackFrame::INTERNAL);
2372 __ CallRuntime(Runtime::kThrowStackOverflow);
2373 }
2374 __ bind(&done);
2375 }
2376
2377 // Relocate arguments down the stack.
2378 {
2379 Label loop, done_loop;
2380 __ mov(a5, zero_reg);
2381 __ bind(&loop);
2382 __ Branch(&done_loop, gt, a5, Operand(a0));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002383 __ Dlsa(a6, sp, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002384 __ ld(at, MemOperand(a6));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002385 __ Dlsa(a6, sp, a5, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002386 __ sd(at, MemOperand(a6));
2387 __ Daddu(a4, a4, Operand(1));
2388 __ Daddu(a5, a5, Operand(1));
2389 __ Branch(&loop);
2390 __ bind(&done_loop);
2391 }
2392
2393 // Copy [[BoundArguments]] to the stack (below the arguments).
2394 {
2395 Label loop, done_loop;
2396 __ ld(a4, FieldMemOperand(a2, FixedArray::kLengthOffset));
2397 __ SmiUntag(a4);
2398 __ Daddu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2399 __ bind(&loop);
2400 __ Dsubu(a4, a4, Operand(1));
2401 __ Branch(&done_loop, lt, a4, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002402 __ Dlsa(a5, a2, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002403 __ ld(at, MemOperand(a5));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002404 __ Dlsa(a5, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002405 __ sd(at, MemOperand(a5));
2406 __ Daddu(a0, a0, Operand(1));
2407 __ Branch(&loop);
2408 __ bind(&done_loop);
2409 }
2410
2411 // Call the [[BoundTargetFunction]] via the Call builtin.
2412 __ ld(a1, FieldMemOperand(a1, JSBoundFunction::kBoundTargetFunctionOffset));
2413 __ li(at, Operand(ExternalReference(Builtins::kCall_ReceiverIsAny,
2414 masm->isolate())));
2415 __ ld(at, MemOperand(at));
2416 __ Daddu(at, at, Operand(Code::kHeaderSize - kHeapObjectTag));
2417 __ Jump(at);
2418}
2419
2420
2421// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01002422void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode,
2423 TailCallMode tail_call_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002424 // ----------- S t a t e -------------
2425 // -- a0 : the number of arguments (not including the receiver)
2426 // -- a1 : the target to call (can be any Object).
2427 // -----------------------------------
2428
2429 Label non_callable, non_function, non_smi;
2430 __ JumpIfSmi(a1, &non_callable);
2431 __ bind(&non_smi);
2432 __ GetObjectType(a1, t1, t2);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002433 __ Jump(masm->isolate()->builtins()->CallFunction(mode, tail_call_mode),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002434 RelocInfo::CODE_TARGET, eq, t2, Operand(JS_FUNCTION_TYPE));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002435 __ Jump(masm->isolate()->builtins()->CallBoundFunction(tail_call_mode),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002436 RelocInfo::CODE_TARGET, eq, t2, Operand(JS_BOUND_FUNCTION_TYPE));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002437
2438 // Check if target has a [[Call]] internal method.
2439 __ lbu(t1, FieldMemOperand(t1, Map::kBitFieldOffset));
2440 __ And(t1, t1, Operand(1 << Map::kIsCallable));
2441 __ Branch(&non_callable, eq, t1, Operand(zero_reg));
2442
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002443 __ Branch(&non_function, ne, t2, Operand(JS_PROXY_TYPE));
2444
Ben Murdoch097c5b22016-05-18 11:27:45 +01002445 // 0. Prepare for tail call if necessary.
2446 if (tail_call_mode == TailCallMode::kAllow) {
2447 PrepareForTailCall(masm, a0, t0, t1, t2);
2448 }
2449
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002450 // 1. Runtime fallback for Proxy [[Call]].
2451 __ Push(a1);
2452 // Increase the arguments size to include the pushed function and the
2453 // existing receiver on the stack.
2454 __ Daddu(a0, a0, 2);
2455 // Tail-call to the runtime.
2456 __ JumpToExternalReference(
2457 ExternalReference(Runtime::kJSProxyCall, masm->isolate()));
2458
2459 // 2. Call to something else, which might have a [[Call]] internal method (if
2460 // not we raise an exception).
2461 __ bind(&non_function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002462 // Overwrite the original receiver with the (original) target.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002463 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002464 __ sd(a1, MemOperand(at));
2465 // Let the "call_as_function_delegate" take care of the rest.
2466 __ LoadNativeContextSlot(Context::CALL_AS_FUNCTION_DELEGATE_INDEX, a1);
2467 __ Jump(masm->isolate()->builtins()->CallFunction(
Ben Murdoch097c5b22016-05-18 11:27:45 +01002468 ConvertReceiverMode::kNotNullOrUndefined, tail_call_mode),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002469 RelocInfo::CODE_TARGET);
2470
2471 // 3. Call to something that is not callable.
2472 __ bind(&non_callable);
2473 {
2474 FrameScope scope(masm, StackFrame::INTERNAL);
2475 __ Push(a1);
2476 __ CallRuntime(Runtime::kThrowCalledNonCallable);
2477 }
2478}
2479
2480
2481void Builtins::Generate_ConstructFunction(MacroAssembler* masm) {
2482 // ----------- S t a t e -------------
2483 // -- a0 : the number of arguments (not including the receiver)
2484 // -- a1 : the constructor to call (checked to be a JSFunction)
2485 // -- a3 : the new target (checked to be a constructor)
2486 // -----------------------------------
2487 __ AssertFunction(a1);
2488
2489 // Calling convention for function specific ConstructStubs require
2490 // a2 to contain either an AllocationSite or undefined.
2491 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2492
2493 // Tail call to the function-specific construct stub (still in the caller
2494 // context at this point).
2495 __ ld(a4, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2496 __ ld(a4, FieldMemOperand(a4, SharedFunctionInfo::kConstructStubOffset));
2497 __ Daddu(at, a4, Operand(Code::kHeaderSize - kHeapObjectTag));
2498 __ Jump(at);
2499}
2500
2501
2502// static
2503void Builtins::Generate_ConstructBoundFunction(MacroAssembler* masm) {
2504 // ----------- S t a t e -------------
2505 // -- a0 : the number of arguments (not including the receiver)
2506 // -- a1 : the function to call (checked to be a JSBoundFunction)
2507 // -- a3 : the new target (checked to be a constructor)
2508 // -----------------------------------
2509 __ AssertBoundFunction(a1);
2510
2511 // Load [[BoundArguments]] into a2 and length of that into a4.
2512 __ ld(a2, FieldMemOperand(a1, JSBoundFunction::kBoundArgumentsOffset));
2513 __ ld(a4, FieldMemOperand(a2, FixedArray::kLengthOffset));
2514 __ SmiUntag(a4);
2515
2516 // ----------- S t a t e -------------
2517 // -- a0 : the number of arguments (not including the receiver)
2518 // -- a1 : the function to call (checked to be a JSBoundFunction)
2519 // -- a2 : the [[BoundArguments]] (implemented as FixedArray)
2520 // -- a3 : the new target (checked to be a constructor)
2521 // -- a4 : the number of [[BoundArguments]]
2522 // -----------------------------------
2523
2524 // Reserve stack space for the [[BoundArguments]].
2525 {
2526 Label done;
2527 __ dsll(a5, a4, kPointerSizeLog2);
2528 __ Dsubu(sp, sp, Operand(a5));
2529 // Check the stack for overflow. We are not trying to catch interruptions
2530 // (i.e. debug break and preemption) here, so check the "real stack limit".
2531 __ LoadRoot(at, Heap::kRealStackLimitRootIndex);
2532 __ Branch(&done, gt, sp, Operand(at)); // Signed comparison.
2533 // Restore the stack pointer.
2534 __ Daddu(sp, sp, Operand(a5));
2535 {
2536 FrameScope scope(masm, StackFrame::MANUAL);
2537 __ EnterFrame(StackFrame::INTERNAL);
2538 __ CallRuntime(Runtime::kThrowStackOverflow);
2539 }
2540 __ bind(&done);
2541 }
2542
2543 // Relocate arguments down the stack.
2544 {
2545 Label loop, done_loop;
2546 __ mov(a5, zero_reg);
2547 __ bind(&loop);
2548 __ Branch(&done_loop, ge, a5, Operand(a0));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002549 __ Dlsa(a6, sp, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002550 __ ld(at, MemOperand(a6));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002551 __ Dlsa(a6, sp, a5, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002552 __ sd(at, MemOperand(a6));
2553 __ Daddu(a4, a4, Operand(1));
2554 __ Daddu(a5, a5, Operand(1));
2555 __ Branch(&loop);
2556 __ bind(&done_loop);
2557 }
2558
2559 // Copy [[BoundArguments]] to the stack (below the arguments).
2560 {
2561 Label loop, done_loop;
2562 __ ld(a4, FieldMemOperand(a2, FixedArray::kLengthOffset));
2563 __ SmiUntag(a4);
2564 __ Daddu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2565 __ bind(&loop);
2566 __ Dsubu(a4, a4, Operand(1));
2567 __ Branch(&done_loop, lt, a4, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002568 __ Dlsa(a5, a2, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002569 __ ld(at, MemOperand(a5));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002570 __ Dlsa(a5, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002571 __ sd(at, MemOperand(a5));
2572 __ Daddu(a0, a0, Operand(1));
2573 __ Branch(&loop);
2574 __ bind(&done_loop);
2575 }
2576
2577 // Patch new.target to [[BoundTargetFunction]] if new.target equals target.
2578 {
2579 Label skip_load;
2580 __ Branch(&skip_load, ne, a1, Operand(a3));
2581 __ ld(a3, FieldMemOperand(a1, JSBoundFunction::kBoundTargetFunctionOffset));
2582 __ bind(&skip_load);
2583 }
2584
2585 // Construct the [[BoundTargetFunction]] via the Construct builtin.
2586 __ ld(a1, FieldMemOperand(a1, JSBoundFunction::kBoundTargetFunctionOffset));
2587 __ li(at, Operand(ExternalReference(Builtins::kConstruct, masm->isolate())));
2588 __ ld(at, MemOperand(at));
2589 __ Daddu(at, at, Operand(Code::kHeaderSize - kHeapObjectTag));
2590 __ Jump(at);
2591}
2592
2593
2594// static
2595void Builtins::Generate_ConstructProxy(MacroAssembler* masm) {
2596 // ----------- S t a t e -------------
2597 // -- a0 : the number of arguments (not including the receiver)
2598 // -- a1 : the constructor to call (checked to be a JSProxy)
2599 // -- a3 : the new target (either the same as the constructor or
2600 // the JSFunction on which new was invoked initially)
2601 // -----------------------------------
2602
2603 // Call into the Runtime for Proxy [[Construct]].
2604 __ Push(a1, a3);
2605 // Include the pushed new_target, constructor and the receiver.
2606 __ Daddu(a0, a0, Operand(3));
2607 // Tail-call to the runtime.
2608 __ JumpToExternalReference(
2609 ExternalReference(Runtime::kJSProxyConstruct, masm->isolate()));
2610}
2611
2612
2613// static
2614void Builtins::Generate_Construct(MacroAssembler* masm) {
2615 // ----------- S t a t e -------------
2616 // -- a0 : the number of arguments (not including the receiver)
2617 // -- a1 : the constructor to call (can be any Object)
2618 // -- a3 : the new target (either the same as the constructor or
2619 // the JSFunction on which new was invoked initially)
2620 // -----------------------------------
2621
2622 // Check if target is a Smi.
2623 Label non_constructor;
2624 __ JumpIfSmi(a1, &non_constructor);
2625
2626 // Dispatch based on instance type.
2627 __ ld(t1, FieldMemOperand(a1, HeapObject::kMapOffset));
2628 __ lbu(t2, FieldMemOperand(t1, Map::kInstanceTypeOffset));
2629 __ Jump(masm->isolate()->builtins()->ConstructFunction(),
2630 RelocInfo::CODE_TARGET, eq, t2, Operand(JS_FUNCTION_TYPE));
2631
2632 // Check if target has a [[Construct]] internal method.
2633 __ lbu(t3, FieldMemOperand(t1, Map::kBitFieldOffset));
2634 __ And(t3, t3, Operand(1 << Map::kIsConstructor));
2635 __ Branch(&non_constructor, eq, t3, Operand(zero_reg));
2636
2637 // Only dispatch to bound functions after checking whether they are
2638 // constructors.
2639 __ Jump(masm->isolate()->builtins()->ConstructBoundFunction(),
2640 RelocInfo::CODE_TARGET, eq, t2, Operand(JS_BOUND_FUNCTION_TYPE));
2641
2642 // Only dispatch to proxies after checking whether they are constructors.
2643 __ Jump(masm->isolate()->builtins()->ConstructProxy(), RelocInfo::CODE_TARGET,
2644 eq, t2, Operand(JS_PROXY_TYPE));
2645
2646 // Called Construct on an exotic Object with a [[Construct]] internal method.
2647 {
2648 // Overwrite the original receiver with the (original) target.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002649 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002650 __ sd(a1, MemOperand(at));
2651 // Let the "call_as_constructor_delegate" take care of the rest.
2652 __ LoadNativeContextSlot(Context::CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, a1);
2653 __ Jump(masm->isolate()->builtins()->CallFunction(),
2654 RelocInfo::CODE_TARGET);
2655 }
2656
2657 // Called Construct on an Object that doesn't have a [[Construct]] internal
2658 // method.
2659 __ bind(&non_constructor);
2660 __ Jump(masm->isolate()->builtins()->ConstructedNonConstructable(),
2661 RelocInfo::CODE_TARGET);
2662}
2663
Ben Murdochc5610432016-08-08 18:44:38 +01002664// static
2665void Builtins::Generate_AllocateInNewSpace(MacroAssembler* masm) {
2666 // ----------- S t a t e -------------
2667 // -- a0 : requested object size (untagged)
2668 // -- ra : return address
2669 // -----------------------------------
2670 __ SmiTag(a0);
2671 __ Push(a0);
2672 __ Move(cp, Smi::FromInt(0));
2673 __ TailCallRuntime(Runtime::kAllocateInNewSpace);
2674}
2675
2676// static
2677void Builtins::Generate_AllocateInOldSpace(MacroAssembler* masm) {
2678 // ----------- S t a t e -------------
2679 // -- a0 : requested object size (untagged)
2680 // -- ra : return address
2681 // -----------------------------------
2682 __ SmiTag(a0);
2683 __ Move(a1, Smi::FromInt(AllocateTargetSpace::encode(OLD_SPACE)));
2684 __ Push(a0, a1);
2685 __ Move(cp, Smi::FromInt(0));
2686 __ TailCallRuntime(Runtime::kAllocateInTargetSpace);
2687}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002688
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002689void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
2690 // State setup as expected by MacroAssembler::InvokePrologue.
2691 // ----------- S t a t e -------------
2692 // -- a0: actual arguments count
2693 // -- a1: function (passed through to callee)
2694 // -- a2: expected arguments count
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002695 // -- a3: new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002696 // -----------------------------------
2697
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002698 Label invoke, dont_adapt_arguments, stack_overflow;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002699
2700 Label enough, too_few;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002701 __ Branch(&dont_adapt_arguments, eq,
2702 a2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
2703 // We use Uless as the number of argument should always be greater than 0.
2704 __ Branch(&too_few, Uless, a0, Operand(a2));
2705
2706 { // Enough parameters: actual >= expected.
2707 // a0: actual number of arguments as a smi
2708 // a1: function
2709 // a2: expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002710 // a3: new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002711 __ bind(&enough);
2712 EnterArgumentsAdaptorFrame(masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002713 ArgumentAdaptorStackCheck(masm, &stack_overflow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002714
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002715 // Calculate copy start address into a0 and copy end address into a4.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002716 __ SmiScale(a0, a0, kPointerSizeLog2);
2717 __ Daddu(a0, fp, a0);
2718 // Adjust for return address and receiver.
2719 __ Daddu(a0, a0, Operand(2 * kPointerSize));
2720 // Compute copy end address.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002721 __ dsll(a4, a2, kPointerSizeLog2);
2722 __ dsubu(a4, a0, a4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002723
2724 // Copy the arguments (including the receiver) to the new stack frame.
2725 // a0: copy start address
2726 // a1: function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002727 // a2: expected number of arguments
2728 // a3: new target (passed through to callee)
2729 // a4: copy end address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002730
2731 Label copy;
2732 __ bind(&copy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002733 __ ld(a5, MemOperand(a0));
2734 __ push(a5);
2735 __ Branch(USE_DELAY_SLOT, &copy, ne, a0, Operand(a4));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002736 __ daddiu(a0, a0, -kPointerSize); // In delay slot.
2737
2738 __ jmp(&invoke);
2739 }
2740
2741 { // Too few parameters: Actual < expected.
2742 __ bind(&too_few);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002743 EnterArgumentsAdaptorFrame(masm);
2744 ArgumentAdaptorStackCheck(masm, &stack_overflow);
2745
2746 // Calculate copy start address into a0 and copy end address into a7.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002747 // a0: actual number of arguments as a smi
2748 // a1: function
2749 // a2: expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002750 // a3: new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002751 __ SmiScale(a0, a0, kPointerSizeLog2);
2752 __ Daddu(a0, fp, a0);
2753 // Adjust for return address and receiver.
2754 __ Daddu(a0, a0, Operand(2 * kPointerSize));
2755 // Compute copy end address. Also adjust for return address.
2756 __ Daddu(a7, fp, kPointerSize);
2757
2758 // Copy the arguments (including the receiver) to the new stack frame.
2759 // a0: copy start address
2760 // a1: function
2761 // a2: expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002762 // a3: new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002763 // a7: copy end address
2764 Label copy;
2765 __ bind(&copy);
2766 __ ld(a4, MemOperand(a0)); // Adjusted above for return addr and receiver.
2767 __ Dsubu(sp, sp, kPointerSize);
2768 __ Dsubu(a0, a0, kPointerSize);
2769 __ Branch(USE_DELAY_SLOT, &copy, ne, a0, Operand(a7));
2770 __ sd(a4, MemOperand(sp)); // In the delay slot.
2771
2772 // Fill the remaining expected arguments with undefined.
2773 // a1: function
2774 // a2: expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002775 // a3: new target (passed through to callee)
2776 __ LoadRoot(a5, Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002777 __ dsll(a6, a2, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002778 __ Dsubu(a4, fp, Operand(a6));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002779 // Adjust for frame.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002780 __ Dsubu(a4, a4, Operand(StandardFrameConstants::kFixedFrameSizeFromFp +
2781 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002782
2783 Label fill;
2784 __ bind(&fill);
2785 __ Dsubu(sp, sp, kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002786 __ Branch(USE_DELAY_SLOT, &fill, ne, sp, Operand(a4));
2787 __ sd(a5, MemOperand(sp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002788 }
2789
2790 // Call the entry point.
2791 __ bind(&invoke);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002792 __ mov(a0, a2);
2793 // a0 : expected number of arguments
2794 // a1 : function (passed through to callee)
2795 // a3: new target (passed through to callee)
2796 __ ld(a4, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
2797 __ Call(a4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002798
2799 // Store offset of return address for deoptimizer.
2800 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
2801
2802 // Exit frame and return.
2803 LeaveArgumentsAdaptorFrame(masm);
2804 __ Ret();
2805
2806
2807 // -------------------------------------------
2808 // Don't adapt arguments.
2809 // -------------------------------------------
2810 __ bind(&dont_adapt_arguments);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002811 __ ld(a4, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
2812 __ Jump(a4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002813
2814 __ bind(&stack_overflow);
2815 {
2816 FrameScope frame(masm, StackFrame::MANUAL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002817 __ CallRuntime(Runtime::kThrowStackOverflow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002818 __ break_(0xCC);
2819 }
2820}
2821
2822
2823#undef __
2824
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002825} // namespace internal
2826} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002827
2828#endif // V8_TARGET_ARCH_MIPS64