blob: b55b77c51158bf82d61d1faf8eafcdd073a4e6c8 [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 Murdochb8a8cc12014-11-26 15:28:44 +0000595 if (is_api_function) {
596 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
597 Handle<Code> code =
598 masm->isolate()->builtins()->HandleApiCallConstruct();
599 __ Call(code, RelocInfo::CODE_TARGET);
600 } else {
601 ParameterCount actual(a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000602 __ InvokeFunction(a1, a3, actual, CALL_FUNCTION,
603 CheckDebugStepCallWrapper());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000604 }
605
606 // Store offset of return address for deoptimizer.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000607 if (create_implicit_receiver && !is_api_function) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000608 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
609 }
610
611 // Restore context from the frame.
Ben Murdochda12d292016-06-02 14:46:10 +0100612 __ ld(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000613
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000614 if (create_implicit_receiver) {
615 // If the result is an object (in the ECMA sense), we should get rid
616 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
617 // on page 74.
618 Label use_receiver, exit;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000619
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000620 // If the result is a smi, it is *not* an object in the ECMA sense.
621 // v0: result
622 // sp[0]: receiver (newly allocated object)
623 // sp[1]: number of arguments (smi-tagged)
624 __ JumpIfSmi(v0, &use_receiver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000625
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000626 // If the type of the result (stored in its map) is less than
627 // FIRST_JS_RECEIVER_TYPE, it is not an object in the ECMA sense.
628 __ GetObjectType(v0, a1, a3);
629 __ Branch(&exit, greater_equal, a3, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000630
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000631 // Throw away the result of the constructor invocation and use the
632 // on-stack receiver as the result.
633 __ bind(&use_receiver);
634 __ ld(v0, MemOperand(sp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000636 // Remove receiver from the stack, remove caller arguments, and
637 // return.
638 __ bind(&exit);
639 // v0: result
640 // sp[0]: receiver (newly allocated object)
641 // sp[1]: number of arguments (smi-tagged)
642 __ ld(a1, MemOperand(sp, 1 * kPointerSize));
643 } else {
644 __ ld(a1, MemOperand(sp));
645 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646
647 // Leave construct frame.
648 }
649
Ben Murdoch097c5b22016-05-18 11:27:45 +0100650 // ES6 9.2.2. Step 13+
651 // Check that the result is not a Smi, indicating that the constructor result
652 // from a derived class is neither undefined nor an Object.
653 if (check_derived_construct) {
654 Label dont_throw;
655 __ JumpIfNotSmi(v0, &dont_throw);
656 {
657 FrameScope scope(masm, StackFrame::INTERNAL);
658 __ CallRuntime(Runtime::kThrowDerivedConstructorReturnedNonObject);
659 }
660 __ bind(&dont_throw);
661 }
662
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000663 __ SmiScale(a4, a1, kPointerSizeLog2);
664 __ Daddu(sp, sp, a4);
665 __ Daddu(sp, sp, kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000666 if (create_implicit_receiver) {
667 __ IncrementCounter(isolate->counters()->constructed_objects(), 1, a1, a2);
668 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000669 __ Ret();
670}
671
672
673void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100674 Generate_JSConstructStubHelper(masm, false, true, false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675}
676
677
678void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100679 Generate_JSConstructStubHelper(masm, true, false, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000680}
681
682
683void Builtins::Generate_JSBuiltinsConstructStub(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100684 Generate_JSConstructStubHelper(masm, false, false, false);
685}
686
687
688void Builtins::Generate_JSBuiltinsConstructStubForDerived(
689 MacroAssembler* masm) {
690 Generate_JSConstructStubHelper(masm, false, false, true);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000691}
692
693
694void Builtins::Generate_ConstructedNonConstructable(MacroAssembler* masm) {
695 FrameScope scope(masm, StackFrame::INTERNAL);
696 __ Push(a1);
697 __ CallRuntime(Runtime::kThrowConstructedNonConstructable);
698}
699
700
701enum IsTagged { kArgcIsSmiTagged, kArgcIsUntaggedInt };
702
703
704// Clobbers a2; preserves all other registers.
705static void Generate_CheckStackOverflow(MacroAssembler* masm, Register argc,
706 IsTagged argc_is_tagged) {
707 // Check the stack for overflow. We are not trying to catch
708 // interruptions (e.g. debug break and preemption) here, so the "real stack
709 // limit" is checked.
710 Label okay;
711 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
712 // Make a2 the space we have left. The stack might already be overflowed
713 // here which will cause r2 to become negative.
714 __ dsubu(a2, sp, a2);
715 // Check if the arguments will overflow the stack.
716 if (argc_is_tagged == kArgcIsSmiTagged) {
717 __ SmiScale(a7, v0, kPointerSizeLog2);
718 } else {
719 DCHECK(argc_is_tagged == kArgcIsUntaggedInt);
720 __ dsll(a7, argc, kPointerSizeLog2);
721 }
722 __ Branch(&okay, gt, a2, Operand(a7)); // Signed comparison.
723
724 // Out of stack space.
725 __ CallRuntime(Runtime::kThrowStackOverflow);
726
727 __ bind(&okay);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000728}
729
730
731static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
732 bool is_construct) {
733 // Called from JSEntryStub::GenerateBody
734
735 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000736 // -- a0: new.target
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000737 // -- a1: function
738 // -- a2: receiver_pointer
739 // -- a3: argc
740 // -- s0: argv
741 // -----------------------------------
742 ProfileEntryHookStub::MaybeCallEntryHook(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000743
744 // Enter an internal frame.
745 {
746 FrameScope scope(masm, StackFrame::INTERNAL);
747
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000748 // Setup the context (we need to use the caller context from the isolate).
749 ExternalReference context_address(Isolate::kContextAddress,
750 masm->isolate());
751 __ li(cp, Operand(context_address));
752 __ ld(cp, MemOperand(cp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000753
754 // Push the function and the receiver onto the stack.
755 __ Push(a1, a2);
756
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000757 // Check if we have enough stack space to push all arguments.
758 // Clobbers a2.
759 Generate_CheckStackOverflow(masm, a3, kArgcIsUntaggedInt);
760
761 // Remember new.target.
762 __ mov(a5, a0);
763
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000764 // Copy arguments to the stack in a loop.
765 // a3: argc
766 // s0: argv, i.e. points to first arg
767 Label loop, entry;
Ben Murdoch097c5b22016-05-18 11:27:45 +0100768 __ Dlsa(a6, s0, a3, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 __ b(&entry);
770 __ nop(); // Branch delay slot nop.
771 // a6 points past last arg.
772 __ bind(&loop);
773 __ ld(a4, MemOperand(s0)); // Read next parameter.
774 __ daddiu(s0, s0, kPointerSize);
775 __ ld(a4, MemOperand(a4)); // Dereference handle.
776 __ push(a4); // Push parameter.
777 __ bind(&entry);
778 __ Branch(&loop, ne, s0, Operand(a6));
779
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000780 // Setup new.target and argc.
781 __ mov(a0, a3);
782 __ mov(a3, a5);
783
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000784 // Initialize all JavaScript callee-saved registers, since they will be seen
785 // by the garbage collector as part of handlers.
786 __ LoadRoot(a4, Heap::kUndefinedValueRootIndex);
787 __ mov(s1, a4);
788 __ mov(s2, a4);
789 __ mov(s3, a4);
790 __ mov(s4, a4);
791 __ mov(s5, a4);
792 // s6 holds the root address. Do not clobber.
793 // s7 is cp. Do not init.
794
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000795 // Invoke the code.
796 Handle<Code> builtin = is_construct
797 ? masm->isolate()->builtins()->Construct()
798 : masm->isolate()->builtins()->Call();
799 __ Call(builtin, RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000800
801 // Leave internal frame.
802 }
803 __ Jump(ra);
804}
805
806
807void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
808 Generate_JSEntryTrampolineHelper(masm, false);
809}
810
811
812void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
813 Generate_JSEntryTrampolineHelper(masm, true);
814}
815
816
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000817// Generate code for entering a JS function with the interpreter.
818// On entry to the function the receiver and arguments have been pushed on the
819// stack left to right. The actual argument count matches the formal parameter
820// count expected by the function.
821//
822// The live registers are:
823// o a1: the JS function object being called.
824// o a3: the new target
825// o cp: our context
826// o fp: the caller's frame pointer
827// o sp: stack pointer
828// o ra: return address
829//
Ben Murdoch097c5b22016-05-18 11:27:45 +0100830// The function builds an interpreter frame. See InterpreterFrameConstants in
831// frames.h for its layout.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000832void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
833 // Open a frame scope to indicate that there is a frame on the stack. The
834 // MANUAL indicates that the scope shouldn't actually generate code to set up
835 // the frame (that is done below).
836 FrameScope frame_scope(masm, StackFrame::MANUAL);
Ben Murdochda12d292016-06-02 14:46:10 +0100837 __ PushStandardFrame(a1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000838
839 // Get the bytecode array from the function object and load the pointer to the
840 // first entry into kInterpreterBytecodeRegister.
841 __ ld(a0, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100842 Label load_debug_bytecode_array, bytecode_array_loaded;
843 Register debug_info = kInterpreterBytecodeArrayRegister;
844 DCHECK(!debug_info.is(a0));
845 __ ld(debug_info, FieldMemOperand(a0, SharedFunctionInfo::kDebugInfoOffset));
846 __ Branch(&load_debug_bytecode_array, ne, debug_info,
847 Operand(DebugInfo::uninitialized()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000848 __ ld(kInterpreterBytecodeArrayRegister,
849 FieldMemOperand(a0, SharedFunctionInfo::kFunctionDataOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100850 __ bind(&bytecode_array_loaded);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000851
852 if (FLAG_debug_code) {
853 // Check function data field is actually a BytecodeArray object.
854 __ SmiTst(kInterpreterBytecodeArrayRegister, a4);
855 __ Assert(ne, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, a4,
856 Operand(zero_reg));
857 __ GetObjectType(kInterpreterBytecodeArrayRegister, a4, a4);
858 __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, a4,
859 Operand(BYTECODE_ARRAY_TYPE));
860 }
861
Ben Murdoch097c5b22016-05-18 11:27:45 +0100862 // Push new.target, bytecode array and zero for bytecode array offset.
863 __ Push(a3, kInterpreterBytecodeArrayRegister, zero_reg);
864
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000865 // Allocate the local and temporary register file on the stack.
866 {
867 // Load frame size (word) from the BytecodeArray object.
868 __ lw(a4, FieldMemOperand(kInterpreterBytecodeArrayRegister,
869 BytecodeArray::kFrameSizeOffset));
870
871 // Do a stack check to ensure we don't go over the limit.
872 Label ok;
873 __ Dsubu(a5, sp, Operand(a4));
874 __ LoadRoot(a2, Heap::kRealStackLimitRootIndex);
875 __ Branch(&ok, hs, a5, Operand(a2));
876 __ CallRuntime(Runtime::kThrowStackOverflow);
877 __ bind(&ok);
878
879 // If ok, push undefined as the initial value for all register file entries.
880 Label loop_header;
881 Label loop_check;
882 __ LoadRoot(a5, Heap::kUndefinedValueRootIndex);
883 __ Branch(&loop_check);
884 __ bind(&loop_header);
885 // TODO(rmcilroy): Consider doing more than one push per loop iteration.
886 __ push(a5);
887 // Continue loop if not done.
888 __ bind(&loop_check);
889 __ Dsubu(a4, a4, Operand(kPointerSize));
890 __ Branch(&loop_header, ge, a4, Operand(zero_reg));
891 }
892
893 // TODO(rmcilroy): List of things not currently dealt with here but done in
894 // fullcodegen's prologue:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000895 // - Call ProfileEntryHookStub when isolate has a function_entry_hook.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000896 // - Code aging of the BytecodeArray object.
897
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000898 // Load bytecode offset and dispatch table into registers.
899 __ LoadRoot(kInterpreterAccumulatorRegister, Heap::kUndefinedValueRootIndex);
900 __ Daddu(kInterpreterRegisterFileRegister, fp,
901 Operand(InterpreterFrameConstants::kRegisterFilePointerFromFp));
902 __ li(kInterpreterBytecodeOffsetRegister,
903 Operand(BytecodeArray::kHeaderSize - kHeapObjectTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100904 __ li(kInterpreterDispatchTableRegister,
905 Operand(ExternalReference::interpreter_dispatch_table_address(
906 masm->isolate())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000907
908 // Dispatch to the first bytecode handler for the function.
909 __ Daddu(a0, kInterpreterBytecodeArrayRegister,
910 kInterpreterBytecodeOffsetRegister);
911 __ lbu(a0, MemOperand(a0));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100912 __ Dlsa(at, kInterpreterDispatchTableRegister, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000913 __ ld(at, MemOperand(at));
914 // TODO(rmcilroy): Make dispatch table point to code entrys to avoid untagging
915 // and header removal.
916 __ Daddu(at, at, Operand(Code::kHeaderSize - kHeapObjectTag));
917 __ Call(at);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100918
919 // Even though the first bytecode handler was called, we will never return.
920 __ Abort(kUnexpectedReturnFromBytecodeHandler);
921
922 // Load debug copy of the bytecode array.
923 __ bind(&load_debug_bytecode_array);
924 __ ld(kInterpreterBytecodeArrayRegister,
925 FieldMemOperand(debug_info, DebugInfo::kAbstractCodeIndex));
926 __ Branch(&bytecode_array_loaded);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000927}
928
929
930void Builtins::Generate_InterpreterExitTrampoline(MacroAssembler* masm) {
931 // TODO(rmcilroy): List of things not currently dealt with here but done in
932 // fullcodegen's EmitReturnSequence.
933 // - Supporting FLAG_trace for Runtime::TraceExit.
934 // - Support profiler (specifically decrementing profiling_counter
935 // appropriately and calling out to HandleInterrupts if necessary).
936
937 // The return value is in accumulator, which is already in v0.
938
939 // Leave the frame (also dropping the register file).
940 __ LeaveFrame(StackFrame::JAVA_SCRIPT);
941
942 // Drop receiver + arguments and return.
943 __ lw(at, FieldMemOperand(kInterpreterBytecodeArrayRegister,
944 BytecodeArray::kParameterSizeOffset));
945 __ Daddu(sp, sp, at);
946 __ Jump(ra);
947}
948
949
950// static
Ben Murdoch097c5b22016-05-18 11:27:45 +0100951void Builtins::Generate_InterpreterPushArgsAndCallImpl(
952 MacroAssembler* masm, TailCallMode tail_call_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000953 // ----------- S t a t e -------------
954 // -- a0 : the number of arguments (not including the receiver)
955 // -- a2 : the address of the first argument to be pushed. Subsequent
956 // arguments should be consecutive above this, in the same order as
957 // they are to be pushed onto the stack.
958 // -- a1 : the target to call (can be any Object).
959 // -----------------------------------
960
961 // Find the address of the last argument.
962 __ Daddu(a3, a0, Operand(1)); // Add one for receiver.
963 __ dsll(a3, a3, kPointerSizeLog2);
964 __ Dsubu(a3, a2, Operand(a3));
965
966 // Push the arguments.
967 Label loop_header, loop_check;
968 __ Branch(&loop_check);
969 __ bind(&loop_header);
970 __ ld(t0, MemOperand(a2));
971 __ Daddu(a2, a2, Operand(-kPointerSize));
972 __ push(t0);
973 __ bind(&loop_check);
974 __ Branch(&loop_header, gt, a2, Operand(a3));
975
976 // Call the target.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100977 __ Jump(masm->isolate()->builtins()->Call(ConvertReceiverMode::kAny,
978 tail_call_mode),
979 RelocInfo::CODE_TARGET);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000980}
981
982
983// static
984void Builtins::Generate_InterpreterPushArgsAndConstruct(MacroAssembler* masm) {
985 // ----------- S t a t e -------------
986 // -- a0 : argument count (not including receiver)
987 // -- a3 : new target
988 // -- a1 : constructor to call
989 // -- a2 : address of the first argument
990 // -----------------------------------
991
992 // Find the address of the last argument.
993 __ dsll(t0, a0, kPointerSizeLog2);
994 __ Dsubu(t0, a2, Operand(t0));
995
996 // Push a slot for the receiver.
997 __ push(zero_reg);
998
999 // Push the arguments.
1000 Label loop_header, loop_check;
1001 __ Branch(&loop_check);
1002 __ bind(&loop_header);
1003 __ ld(t1, MemOperand(a2));
1004 __ Daddu(a2, a2, Operand(-kPointerSize));
1005 __ push(t1);
1006 __ bind(&loop_check);
1007 __ Branch(&loop_header, gt, a2, Operand(t0));
1008
1009 // Call the constructor with a0, a1, and a3 unmodified.
1010 __ Jump(masm->isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
1011}
1012
1013
Ben Murdoch097c5b22016-05-18 11:27:45 +01001014static void Generate_EnterBytecodeDispatch(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001015 // Initialize register file register and dispatch table register.
1016 __ Daddu(kInterpreterRegisterFileRegister, fp,
1017 Operand(InterpreterFrameConstants::kRegisterFilePointerFromFp));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001018 __ li(kInterpreterDispatchTableRegister,
1019 Operand(ExternalReference::interpreter_dispatch_table_address(
1020 masm->isolate())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001021
1022 // Get the context from the frame.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001023 __ ld(kContextRegister,
1024 MemOperand(kInterpreterRegisterFileRegister,
1025 InterpreterFrameConstants::kContextFromRegisterPointer));
1026
1027 // Get the bytecode array pointer from the frame.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001028 __ ld(
1029 kInterpreterBytecodeArrayRegister,
1030 MemOperand(kInterpreterRegisterFileRegister,
1031 InterpreterFrameConstants::kBytecodeArrayFromRegisterPointer));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001032
1033 if (FLAG_debug_code) {
1034 // Check function data field is actually a BytecodeArray object.
1035 __ SmiTst(kInterpreterBytecodeArrayRegister, at);
1036 __ Assert(ne, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, at,
1037 Operand(zero_reg));
1038 __ GetObjectType(kInterpreterBytecodeArrayRegister, a1, a1);
1039 __ Assert(eq, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry, a1,
1040 Operand(BYTECODE_ARRAY_TYPE));
1041 }
1042
1043 // Get the target bytecode offset from the frame.
1044 __ ld(kInterpreterBytecodeOffsetRegister,
1045 MemOperand(
1046 kInterpreterRegisterFileRegister,
1047 InterpreterFrameConstants::kBytecodeOffsetFromRegisterPointer));
1048 __ SmiUntag(kInterpreterBytecodeOffsetRegister);
1049
1050 // Dispatch to the target bytecode.
1051 __ Daddu(a1, kInterpreterBytecodeArrayRegister,
1052 kInterpreterBytecodeOffsetRegister);
1053 __ lbu(a1, MemOperand(a1));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001054 __ Dlsa(a1, kInterpreterDispatchTableRegister, a1, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001055 __ ld(a1, MemOperand(a1));
1056 __ Daddu(a1, a1, Operand(Code::kHeaderSize - kHeapObjectTag));
1057 __ Jump(a1);
1058}
1059
1060
Ben Murdoch097c5b22016-05-18 11:27:45 +01001061static void Generate_InterpreterNotifyDeoptimizedHelper(
1062 MacroAssembler* masm, Deoptimizer::BailoutType type) {
1063 // Enter an internal frame.
1064 {
1065 FrameScope scope(masm, StackFrame::INTERNAL);
1066
1067 // Pass the deoptimization type to the runtime system.
1068 __ li(a1, Operand(Smi::FromInt(static_cast<int>(type))));
1069 __ push(a1);
1070 __ CallRuntime(Runtime::kNotifyDeoptimized);
1071 // Tear down internal frame.
1072 }
1073
1074 // Drop state (we don't use these for interpreter deopts) and and pop the
1075 // accumulator value into the accumulator register.
1076 __ Drop(1);
1077 __ Pop(kInterpreterAccumulatorRegister);
1078
1079 // Enter the bytecode dispatch.
1080 Generate_EnterBytecodeDispatch(masm);
1081}
1082
1083
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001084void Builtins::Generate_InterpreterNotifyDeoptimized(MacroAssembler* masm) {
1085 Generate_InterpreterNotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1086}
1087
1088
1089void Builtins::Generate_InterpreterNotifySoftDeoptimized(MacroAssembler* masm) {
1090 Generate_InterpreterNotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1091}
1092
1093
1094void Builtins::Generate_InterpreterNotifyLazyDeoptimized(MacroAssembler* masm) {
1095 Generate_InterpreterNotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1096}
1097
Ben Murdoch097c5b22016-05-18 11:27:45 +01001098void Builtins::Generate_InterpreterEnterBytecodeDispatch(MacroAssembler* masm) {
1099 // Set the address of the interpreter entry trampoline as a return address.
1100 // This simulates the initial call to bytecode handlers in interpreter entry
1101 // trampoline. The return will never actually be taken, but our stack walker
1102 // uses this address to determine whether a frame is interpreted.
1103 __ li(ra, Operand(masm->isolate()->builtins()->InterpreterEntryTrampoline()));
1104
1105 Generate_EnterBytecodeDispatch(masm);
1106}
1107
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001108
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001109void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001110 GenerateTailCallToReturnedCode(masm, Runtime::kCompileLazy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001111}
1112
1113
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001114void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001115 GenerateTailCallToReturnedCode(masm,
1116 Runtime::kCompileOptimized_NotConcurrent);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001117}
1118
1119
1120void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001121 GenerateTailCallToReturnedCode(masm, Runtime::kCompileOptimized_Concurrent);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001122}
1123
1124
1125static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
1126 // For now, we are relying on the fact that make_code_young doesn't do any
1127 // garbage collection which allows us to save/restore the registers without
1128 // worrying about which of them contain pointers. We also don't build an
1129 // internal frame to make the code faster, since we shouldn't have to do stack
1130 // crawls in MakeCodeYoung. This seems a bit fragile.
1131
1132 // Set a0 to point to the head of the PlatformCodeAge sequence.
1133 __ Dsubu(a0, a0,
1134 Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
1135
1136 // The following registers must be saved and restored when calling through to
1137 // the runtime:
1138 // a0 - contains return address (beginning of patch sequence)
1139 // a1 - isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001140 // a3 - new target
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001141 RegList saved_regs =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001142 (a0.bit() | a1.bit() | a3.bit() | ra.bit() | fp.bit()) & ~sp.bit();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001143 FrameScope scope(masm, StackFrame::MANUAL);
1144 __ MultiPush(saved_regs);
1145 __ PrepareCallCFunction(2, 0, a2);
1146 __ li(a1, Operand(ExternalReference::isolate_address(masm->isolate())));
1147 __ CallCFunction(
1148 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
1149 __ MultiPop(saved_regs);
1150 __ Jump(a0);
1151}
1152
1153#define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
1154void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
1155 MacroAssembler* masm) { \
1156 GenerateMakeCodeYoungAgainCommon(masm); \
1157} \
1158void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
1159 MacroAssembler* masm) { \
1160 GenerateMakeCodeYoungAgainCommon(masm); \
1161}
1162CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
1163#undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
1164
1165
1166void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
1167 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
1168 // that make_code_young doesn't do any garbage collection which allows us to
1169 // save/restore the registers without worrying about which of them contain
1170 // pointers.
1171
1172 // Set a0 to point to the head of the PlatformCodeAge sequence.
1173 __ Dsubu(a0, a0,
1174 Operand(kNoCodeAgeSequenceLength - Assembler::kInstrSize));
1175
1176 // The following registers must be saved and restored when calling through to
1177 // the runtime:
1178 // a0 - contains return address (beginning of patch sequence)
1179 // a1 - isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001180 // a3 - new target
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001181 RegList saved_regs =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001182 (a0.bit() | a1.bit() | a3.bit() | ra.bit() | fp.bit()) & ~sp.bit();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001183 FrameScope scope(masm, StackFrame::MANUAL);
1184 __ MultiPush(saved_regs);
1185 __ PrepareCallCFunction(2, 0, a2);
1186 __ li(a1, Operand(ExternalReference::isolate_address(masm->isolate())));
1187 __ CallCFunction(
1188 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
1189 2);
1190 __ MultiPop(saved_regs);
1191
1192 // Perform prologue operations usually performed by the young code stub.
Ben Murdochda12d292016-06-02 14:46:10 +01001193 __ PushStandardFrame(a1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001194
1195 // Jump to point after the code-age stub.
1196 __ Daddu(a0, a0, Operand((kNoCodeAgeSequenceLength)));
1197 __ Jump(a0);
1198}
1199
1200
1201void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
1202 GenerateMakeCodeYoungAgainCommon(masm);
1203}
1204
1205
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001206void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
1207 Generate_MarkCodeAsExecutedOnce(masm);
1208}
1209
1210
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001211static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
1212 SaveFPRegsMode save_doubles) {
1213 {
1214 FrameScope scope(masm, StackFrame::INTERNAL);
1215
1216 // Preserve registers across notification, this is important for compiled
1217 // stubs that tail call the runtime on deopts passing their parameters in
1218 // registers.
1219 __ MultiPush(kJSCallerSaved | kCalleeSaved);
1220 // Pass the function and deoptimization type to the runtime system.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001221 __ CallRuntime(Runtime::kNotifyStubFailure, save_doubles);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001222 __ MultiPop(kJSCallerSaved | kCalleeSaved);
1223 }
1224
1225 __ Daddu(sp, sp, Operand(kPointerSize)); // Ignore state
1226 __ Jump(ra); // Jump to miss handler
1227}
1228
1229
1230void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
1231 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
1232}
1233
1234
1235void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
1236 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
1237}
1238
1239
1240static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
1241 Deoptimizer::BailoutType type) {
1242 {
1243 FrameScope scope(masm, StackFrame::INTERNAL);
1244 // Pass the function and deoptimization type to the runtime system.
1245 __ li(a0, Operand(Smi::FromInt(static_cast<int>(type))));
1246 __ push(a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001247 __ CallRuntime(Runtime::kNotifyDeoptimized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001248 }
1249
1250 // Get the full codegen state from the stack and untag it -> a6.
1251 __ ld(a6, MemOperand(sp, 0 * kPointerSize));
1252 __ SmiUntag(a6);
1253 // Switch on the state.
1254 Label with_tos_register, unknown_state;
1255 __ Branch(&with_tos_register,
1256 ne, a6, Operand(FullCodeGenerator::NO_REGISTERS));
1257 __ Ret(USE_DELAY_SLOT);
1258 // Safe to fill delay slot Addu will emit one instruction.
1259 __ Daddu(sp, sp, Operand(1 * kPointerSize)); // Remove state.
1260
1261 __ bind(&with_tos_register);
1262 __ ld(v0, MemOperand(sp, 1 * kPointerSize));
1263 __ Branch(&unknown_state, ne, a6, Operand(FullCodeGenerator::TOS_REG));
1264
1265 __ Ret(USE_DELAY_SLOT);
1266 // Safe to fill delay slot Addu will emit one instruction.
1267 __ Daddu(sp, sp, Operand(2 * kPointerSize)); // Remove state.
1268
1269 __ bind(&unknown_state);
1270 __ stop("no cases left");
1271}
1272
1273
1274void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1275 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
1276}
1277
1278
1279void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
1280 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
1281}
1282
1283
1284void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
1285 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
1286}
1287
1288
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001289// Clobbers {t2, t3, a4, a5}.
1290static void CompatibleReceiverCheck(MacroAssembler* masm, Register receiver,
1291 Register function_template_info,
1292 Label* receiver_check_failed) {
1293 Register signature = t2;
1294 Register map = t3;
1295 Register constructor = a4;
1296 Register scratch = a5;
1297
1298 // If there is no signature, return the holder.
1299 __ ld(signature, FieldMemOperand(function_template_info,
1300 FunctionTemplateInfo::kSignatureOffset));
1301 Label receiver_check_passed;
1302 __ JumpIfRoot(signature, Heap::kUndefinedValueRootIndex,
1303 &receiver_check_passed);
1304
1305 // Walk the prototype chain.
1306 __ ld(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
1307 Label prototype_loop_start;
1308 __ bind(&prototype_loop_start);
1309
1310 // Get the constructor, if any.
1311 __ GetMapConstructor(constructor, map, scratch, scratch);
1312 Label next_prototype;
1313 __ Branch(&next_prototype, ne, scratch, Operand(JS_FUNCTION_TYPE));
1314 Register type = constructor;
1315 __ ld(type,
1316 FieldMemOperand(constructor, JSFunction::kSharedFunctionInfoOffset));
1317 __ ld(type, FieldMemOperand(type, SharedFunctionInfo::kFunctionDataOffset));
1318
1319 // Loop through the chain of inheriting function templates.
1320 Label function_template_loop;
1321 __ bind(&function_template_loop);
1322
1323 // If the signatures match, we have a compatible receiver.
1324 __ Branch(&receiver_check_passed, eq, signature, Operand(type),
1325 USE_DELAY_SLOT);
1326
1327 // If the current type is not a FunctionTemplateInfo, load the next prototype
1328 // in the chain.
1329 __ JumpIfSmi(type, &next_prototype);
1330 __ GetObjectType(type, scratch, scratch);
1331 __ Branch(&next_prototype, ne, scratch, Operand(FUNCTION_TEMPLATE_INFO_TYPE));
1332
1333 // Otherwise load the parent function template and iterate.
1334 __ ld(type,
1335 FieldMemOperand(type, FunctionTemplateInfo::kParentTemplateOffset));
1336 __ Branch(&function_template_loop);
1337
1338 // Load the next prototype.
1339 __ bind(&next_prototype);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001340 __ lwu(scratch, FieldMemOperand(map, Map::kBitField3Offset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001341 __ DecodeField<Map::HasHiddenPrototype>(scratch);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001342 __ Branch(receiver_check_failed, eq, scratch, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001343
1344 __ ld(receiver, FieldMemOperand(map, Map::kPrototypeOffset));
1345 __ ld(map, FieldMemOperand(receiver, HeapObject::kMapOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001346 // Iterate.
1347 __ Branch(&prototype_loop_start);
1348
1349 __ bind(&receiver_check_passed);
1350}
1351
1352
1353void Builtins::Generate_HandleFastApiCall(MacroAssembler* masm) {
1354 // ----------- S t a t e -------------
1355 // -- a0 : number of arguments excluding receiver
1356 // -- a1 : callee
1357 // -- ra : return address
1358 // -- sp[0] : last argument
1359 // -- ...
1360 // -- sp[8 * (argc - 1)] : first argument
1361 // -- sp[8 * argc] : receiver
1362 // -----------------------------------
1363
1364 // Load the FunctionTemplateInfo.
1365 __ ld(t1, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
1366 __ ld(t1, FieldMemOperand(t1, SharedFunctionInfo::kFunctionDataOffset));
1367
1368 // Do the compatible receiver check
1369 Label receiver_check_failed;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001370 __ Dlsa(t8, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001371 __ ld(t0, MemOperand(t8));
1372 CompatibleReceiverCheck(masm, t0, t1, &receiver_check_failed);
1373
1374 // Get the callback offset from the FunctionTemplateInfo, and jump to the
1375 // beginning of the code.
1376 __ ld(t2, FieldMemOperand(t1, FunctionTemplateInfo::kCallCodeOffset));
1377 __ ld(t2, FieldMemOperand(t2, CallHandlerInfo::kFastHandlerOffset));
1378 __ Daddu(t2, t2, Operand(Code::kHeaderSize - kHeapObjectTag));
1379 __ Jump(t2);
1380
1381 // Compatible receiver check failed: throw an Illegal Invocation exception.
1382 __ bind(&receiver_check_failed);
1383 // Drop the arguments (including the receiver);
1384 __ Daddu(t8, t8, Operand(kPointerSize));
1385 __ daddu(sp, t8, zero_reg);
1386 __ TailCallRuntime(Runtime::kThrowIllegalInvocation);
1387}
1388
1389
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001390void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1391 // Lookup the function in the JavaScript frame.
1392 __ ld(a0, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1393 {
1394 FrameScope scope(masm, StackFrame::INTERNAL);
1395 // Pass function as argument.
1396 __ push(a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001397 __ CallRuntime(Runtime::kCompileForOnStackReplacement);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001398 }
1399
1400 // If the code object is null, just return to the unoptimized code.
1401 __ Ret(eq, v0, Operand(Smi::FromInt(0)));
1402
1403 // Load deoptimization data from the code object.
1404 // <deopt_data> = <code>[#deoptimization_data_offset]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001405 __ ld(a1, MemOperand(v0, Code::kDeoptimizationDataOffset - kHeapObjectTag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001406
1407 // Load the OSR entrypoint offset from the deoptimization data.
1408 // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1409 __ ld(a1, MemOperand(a1, FixedArray::OffsetOfElementAt(
1410 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1411 __ SmiUntag(a1);
1412
1413 // Compute the target address = code_obj + header_size + osr_offset
1414 // <entry_addr> = <code_obj> + #header_size + <osr_offset>
1415 __ daddu(v0, v0, a1);
1416 __ daddiu(ra, v0, Code::kHeaderSize - kHeapObjectTag);
1417
1418 // And "return" to the OSR entry point of the function.
1419 __ Ret();
1420}
1421
1422
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001423// static
1424void Builtins::Generate_DatePrototype_GetField(MacroAssembler* masm,
1425 int field_index) {
1426 // ----------- S t a t e -------------
1427 // -- sp[0] : receiver
1428 // -----------------------------------
1429
1430 // 1. Pop receiver into a0 and check that it's actually a JSDate object.
1431 Label receiver_not_date;
1432 {
1433 __ Pop(a0);
1434 __ JumpIfSmi(a0, &receiver_not_date);
1435 __ GetObjectType(a0, t0, t0);
1436 __ Branch(&receiver_not_date, ne, t0, Operand(JS_DATE_TYPE));
1437 }
1438
1439 // 2. Load the specified date field, falling back to the runtime as necessary.
1440 if (field_index == JSDate::kDateValue) {
1441 __ Ret(USE_DELAY_SLOT);
1442 __ ld(v0, FieldMemOperand(a0, JSDate::kValueOffset)); // In delay slot.
1443 } else {
1444 if (field_index < JSDate::kFirstUncachedField) {
1445 Label stamp_mismatch;
1446 __ li(a1, Operand(ExternalReference::date_cache_stamp(masm->isolate())));
1447 __ ld(a1, MemOperand(a1));
1448 __ ld(t0, FieldMemOperand(a0, JSDate::kCacheStampOffset));
1449 __ Branch(&stamp_mismatch, ne, t0, Operand(a1));
1450 __ Ret(USE_DELAY_SLOT);
1451 __ ld(v0, FieldMemOperand(
1452 a0, JSDate::kValueOffset +
1453 field_index * kPointerSize)); // In delay slot.
1454 __ bind(&stamp_mismatch);
1455 }
1456 FrameScope scope(masm, StackFrame::INTERNAL);
1457 __ PrepareCallCFunction(2, t0);
1458 __ li(a1, Operand(Smi::FromInt(field_index)));
1459 __ CallCFunction(
1460 ExternalReference::get_date_field_function(masm->isolate()), 2);
1461 }
1462 __ Ret();
1463
1464 // 3. Raise a TypeError if the receiver is not a date.
1465 __ bind(&receiver_not_date);
1466 __ TailCallRuntime(Runtime::kThrowNotDateError);
1467}
1468
Ben Murdochda12d292016-06-02 14:46:10 +01001469// static
1470void Builtins::Generate_FunctionHasInstance(MacroAssembler* masm) {
1471 // ----------- S t a t e -------------
1472 // -- a0 : argc
1473 // -- sp[0] : first argument (left-hand side)
1474 // -- sp[8] : receiver (right-hand side)
1475 // -----------------------------------
1476
1477 {
1478 FrameScope scope(masm, StackFrame::INTERNAL);
1479 __ ld(InstanceOfDescriptor::LeftRegister(),
1480 MemOperand(fp, 2 * kPointerSize)); // Load left-hand side.
1481 __ ld(InstanceOfDescriptor::RightRegister(),
1482 MemOperand(fp, 3 * kPointerSize)); // Load right-hand side.
1483 InstanceOfStub stub(masm->isolate(), true);
1484 __ CallStub(&stub);
1485 }
1486
1487 // Pop the argument and the receiver.
1488 __ DropAndRet(2);
1489}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001490
1491// static
1492void Builtins::Generate_FunctionPrototypeApply(MacroAssembler* masm) {
1493 // ----------- S t a t e -------------
1494 // -- a0 : argc
1495 // -- sp[0] : argArray
1496 // -- sp[4] : thisArg
1497 // -- sp[8] : receiver
1498 // -----------------------------------
1499
1500 // 1. Load receiver into a1, argArray into a0 (if present), remove all
1501 // arguments from the stack (including the receiver), and push thisArg (if
1502 // present) instead.
1503 {
1504 Label no_arg;
1505 Register scratch = a4;
1506 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
1507 __ mov(a3, a2);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001508 // Dlsa() cannot be used hare as scratch value used later.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001509 __ dsll(scratch, a0, kPointerSizeLog2);
1510 __ Daddu(a0, sp, Operand(scratch));
1511 __ ld(a1, MemOperand(a0)); // receiver
1512 __ Dsubu(a0, a0, Operand(kPointerSize));
1513 __ Branch(&no_arg, lt, a0, Operand(sp));
1514 __ ld(a2, MemOperand(a0)); // thisArg
1515 __ Dsubu(a0, a0, Operand(kPointerSize));
1516 __ Branch(&no_arg, lt, a0, Operand(sp));
1517 __ ld(a3, MemOperand(a0)); // argArray
1518 __ bind(&no_arg);
1519 __ Daddu(sp, sp, Operand(scratch));
1520 __ sd(a2, MemOperand(sp));
1521 __ mov(a0, a3);
1522 }
1523
1524 // ----------- S t a t e -------------
1525 // -- a0 : argArray
1526 // -- a1 : receiver
1527 // -- sp[0] : thisArg
1528 // -----------------------------------
1529
1530 // 2. Make sure the receiver is actually callable.
1531 Label receiver_not_callable;
1532 __ JumpIfSmi(a1, &receiver_not_callable);
1533 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1534 __ lbu(a4, FieldMemOperand(a4, Map::kBitFieldOffset));
1535 __ And(a4, a4, Operand(1 << Map::kIsCallable));
1536 __ Branch(&receiver_not_callable, eq, a4, Operand(zero_reg));
1537
1538 // 3. Tail call with no arguments if argArray is null or undefined.
1539 Label no_arguments;
1540 __ JumpIfRoot(a0, Heap::kNullValueRootIndex, &no_arguments);
1541 __ JumpIfRoot(a0, Heap::kUndefinedValueRootIndex, &no_arguments);
1542
1543 // 4a. Apply the receiver to the given argArray (passing undefined for
1544 // new.target).
1545 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
1546 __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1547
1548 // 4b. The argArray is either null or undefined, so we tail call without any
1549 // arguments to the receiver.
1550 __ bind(&no_arguments);
1551 {
1552 __ mov(a0, zero_reg);
1553 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1554 }
1555
1556 // 4c. The receiver is not callable, throw an appropriate TypeError.
1557 __ bind(&receiver_not_callable);
1558 {
1559 __ sd(a1, MemOperand(sp));
1560 __ TailCallRuntime(Runtime::kThrowApplyNonFunction);
1561 }
1562}
1563
1564
1565// static
1566void Builtins::Generate_FunctionPrototypeCall(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001567 // 1. Make sure we have at least one argument.
1568 // a0: actual number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001569 {
1570 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001571 __ Branch(&done, ne, a0, Operand(zero_reg));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001572 __ PushRoot(Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001573 __ Daddu(a0, a0, Operand(1));
1574 __ bind(&done);
1575 }
1576
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001577 // 2. Get the function to call (passed as receiver) from the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001578 // a0: actual number of arguments
Ben Murdoch097c5b22016-05-18 11:27:45 +01001579 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001580 __ ld(a1, MemOperand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001581
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001582 // 3. Shift arguments and return address one slot down on the stack
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001583 // (overwriting the original receiver). Adjust argument count to make
1584 // the original first argument the new receiver.
1585 // a0: actual number of arguments
1586 // a1: function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001587 {
1588 Label loop;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001589 // Calculate the copy start address (destination). Copy end address is sp.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001590 __ Dlsa(a2, sp, a0, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001591
1592 __ bind(&loop);
1593 __ ld(at, MemOperand(a2, -kPointerSize));
1594 __ sd(at, MemOperand(a2));
1595 __ Dsubu(a2, a2, Operand(kPointerSize));
1596 __ Branch(&loop, ne, a2, Operand(sp));
1597 // Adjust the actual number of arguments and remove the top element
1598 // (which is a copy of the last argument).
1599 __ Dsubu(a0, a0, Operand(1));
1600 __ Pop();
1601 }
1602
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001603 // 4. Call the callable.
1604 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001605}
1606
1607
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001608void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1609 // ----------- S t a t e -------------
1610 // -- a0 : argc
1611 // -- sp[0] : argumentsList
1612 // -- sp[4] : thisArgument
1613 // -- sp[8] : target
1614 // -- sp[12] : receiver
1615 // -----------------------------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001616
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001617 // 1. Load target into a1 (if present), argumentsList into a0 (if present),
1618 // remove all arguments from the stack (including the receiver), and push
1619 // thisArgument (if present) instead.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001620 {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001621 Label no_arg;
1622 Register scratch = a4;
1623 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
1624 __ mov(a2, a1);
1625 __ mov(a3, a1);
1626 __ dsll(scratch, a0, kPointerSizeLog2);
1627 __ mov(a0, scratch);
1628 __ Dsubu(a0, a0, Operand(kPointerSize));
1629 __ Branch(&no_arg, lt, a0, Operand(zero_reg));
1630 __ Daddu(a0, sp, Operand(a0));
1631 __ ld(a1, MemOperand(a0)); // target
1632 __ Dsubu(a0, a0, Operand(kPointerSize));
1633 __ Branch(&no_arg, lt, a0, Operand(sp));
1634 __ ld(a2, MemOperand(a0)); // thisArgument
1635 __ Dsubu(a0, a0, Operand(kPointerSize));
1636 __ Branch(&no_arg, lt, a0, Operand(sp));
1637 __ ld(a3, MemOperand(a0)); // argumentsList
1638 __ bind(&no_arg);
1639 __ Daddu(sp, sp, Operand(scratch));
1640 __ sd(a2, MemOperand(sp));
1641 __ mov(a0, a3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642 }
1643
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001644 // ----------- S t a t e -------------
1645 // -- a0 : argumentsList
1646 // -- a1 : target
1647 // -- sp[0] : thisArgument
1648 // -----------------------------------
1649
1650 // 2. Make sure the target is actually callable.
1651 Label target_not_callable;
1652 __ JumpIfSmi(a1, &target_not_callable);
1653 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1654 __ lbu(a4, FieldMemOperand(a4, Map::kBitFieldOffset));
1655 __ And(a4, a4, Operand(1 << Map::kIsCallable));
1656 __ Branch(&target_not_callable, eq, a4, Operand(zero_reg));
1657
1658 // 3a. Apply the target to the given argumentsList (passing undefined for
1659 // new.target).
1660 __ LoadRoot(a3, Heap::kUndefinedValueRootIndex);
1661 __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1662
1663 // 3b. The target is not callable, throw an appropriate TypeError.
1664 __ bind(&target_not_callable);
1665 {
1666 __ sd(a1, MemOperand(sp));
1667 __ TailCallRuntime(Runtime::kThrowApplyNonFunction);
1668 }
1669}
1670
1671
1672void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1673 // ----------- S t a t e -------------
1674 // -- a0 : argc
1675 // -- sp[0] : new.target (optional)
1676 // -- sp[4] : argumentsList
1677 // -- sp[8] : target
1678 // -- sp[12] : receiver
1679 // -----------------------------------
1680
1681 // 1. Load target into a1 (if present), argumentsList into a0 (if present),
1682 // new.target into a3 (if present, otherwise use target), remove all
1683 // arguments from the stack (including the receiver), and push thisArgument
1684 // (if present) instead.
1685 {
1686 Label no_arg;
1687 Register scratch = a4;
1688 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
1689 __ mov(a2, a1);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001690 // Dlsa() cannot be used hare as scratch value used later.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001691 __ dsll(scratch, a0, kPointerSizeLog2);
1692 __ Daddu(a0, sp, Operand(scratch));
1693 __ sd(a2, MemOperand(a0)); // receiver
1694 __ Dsubu(a0, a0, Operand(kPointerSize));
1695 __ Branch(&no_arg, lt, a0, Operand(sp));
1696 __ ld(a1, MemOperand(a0)); // target
1697 __ mov(a3, a1); // new.target defaults to target
1698 __ Dsubu(a0, a0, Operand(kPointerSize));
1699 __ Branch(&no_arg, lt, a0, Operand(sp));
1700 __ ld(a2, MemOperand(a0)); // argumentsList
1701 __ Dsubu(a0, a0, Operand(kPointerSize));
1702 __ Branch(&no_arg, lt, a0, Operand(sp));
1703 __ ld(a3, MemOperand(a0)); // new.target
1704 __ bind(&no_arg);
1705 __ Daddu(sp, sp, Operand(scratch));
1706 __ mov(a0, a2);
1707 }
1708
1709 // ----------- S t a t e -------------
1710 // -- a0 : argumentsList
1711 // -- a3 : new.target
1712 // -- a1 : target
1713 // -- sp[0] : receiver (undefined)
1714 // -----------------------------------
1715
1716 // 2. Make sure the target is actually a constructor.
1717 Label target_not_constructor;
1718 __ JumpIfSmi(a1, &target_not_constructor);
1719 __ ld(a4, FieldMemOperand(a1, HeapObject::kMapOffset));
1720 __ lbu(a4, FieldMemOperand(a4, Map::kBitFieldOffset));
1721 __ And(a4, a4, Operand(1 << Map::kIsConstructor));
1722 __ Branch(&target_not_constructor, eq, a4, Operand(zero_reg));
1723
1724 // 3. Make sure the target is actually a constructor.
1725 Label new_target_not_constructor;
1726 __ JumpIfSmi(a3, &new_target_not_constructor);
1727 __ ld(a4, FieldMemOperand(a3, HeapObject::kMapOffset));
1728 __ lbu(a4, FieldMemOperand(a4, Map::kBitFieldOffset));
1729 __ And(a4, a4, Operand(1 << Map::kIsConstructor));
1730 __ Branch(&new_target_not_constructor, eq, a4, Operand(zero_reg));
1731
1732 // 4a. Construct the target with the given new.target and argumentsList.
1733 __ Jump(masm->isolate()->builtins()->Apply(), RelocInfo::CODE_TARGET);
1734
1735 // 4b. The target is not a constructor, throw an appropriate TypeError.
1736 __ bind(&target_not_constructor);
1737 {
1738 __ sd(a1, MemOperand(sp));
1739 __ TailCallRuntime(Runtime::kThrowCalledNonCallable);
1740 }
1741
1742 // 4c. The new.target is not a constructor, throw an appropriate TypeError.
1743 __ bind(&new_target_not_constructor);
1744 {
1745 __ sd(a3, MemOperand(sp));
1746 __ TailCallRuntime(Runtime::kThrowCalledNonCallable);
1747 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001748}
1749
1750
1751static void ArgumentAdaptorStackCheck(MacroAssembler* masm,
1752 Label* stack_overflow) {
1753 // ----------- S t a t e -------------
1754 // -- a0 : actual number of arguments
1755 // -- a1 : function (passed through to callee)
1756 // -- a2 : expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001757 // -- a3 : new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001758 // -----------------------------------
1759 // Check the stack for overflow. We are not trying to catch
1760 // interruptions (e.g. debug break and preemption) here, so the "real stack
1761 // limit" is checked.
1762 __ LoadRoot(a5, Heap::kRealStackLimitRootIndex);
1763 // Make a5 the space we have left. The stack might already be overflowed
1764 // here which will cause a5 to become negative.
1765 __ dsubu(a5, sp, a5);
1766 // Check if the arguments will overflow the stack.
1767 __ dsll(at, a2, kPointerSizeLog2);
1768 // Signed comparison.
1769 __ Branch(stack_overflow, le, a5, Operand(at));
1770}
1771
1772
1773static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1774 // __ sll(a0, a0, kSmiTagSize);
1775 __ dsll32(a0, a0, 0);
1776 __ li(a4, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1777 __ MultiPush(a0.bit() | a1.bit() | a4.bit() | fp.bit() | ra.bit());
1778 __ Daddu(fp, sp,
1779 Operand(StandardFrameConstants::kFixedFrameSizeFromFp + kPointerSize));
1780}
1781
1782
1783static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1784 // ----------- S t a t e -------------
1785 // -- v0 : result being passed through
1786 // -----------------------------------
1787 // Get the number of arguments passed (as a smi), tear down the frame and
1788 // then tear down the parameters.
1789 __ ld(a1, MemOperand(fp, -(StandardFrameConstants::kFixedFrameSizeFromFp +
1790 kPointerSize)));
1791 __ mov(sp, fp);
1792 __ MultiPop(fp.bit() | ra.bit());
1793 __ SmiScale(a4, a1, kPointerSizeLog2);
1794 __ Daddu(sp, sp, a4);
1795 // Adjust for the receiver.
1796 __ Daddu(sp, sp, Operand(kPointerSize));
1797}
1798
1799
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001800// static
1801void Builtins::Generate_Apply(MacroAssembler* masm) {
1802 // ----------- S t a t e -------------
1803 // -- a0 : argumentsList
1804 // -- a1 : target
1805 // -- a3 : new.target (checked to be constructor or undefined)
1806 // -- sp[0] : thisArgument
1807 // -----------------------------------
1808
1809 // Create the list of arguments from the array-like argumentsList.
1810 {
1811 Label create_arguments, create_array, create_runtime, done_create;
1812 __ JumpIfSmi(a0, &create_runtime);
1813
1814 // Load the map of argumentsList into a2.
1815 __ ld(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
1816
1817 // Load native context into a4.
1818 __ ld(a4, NativeContextMemOperand());
1819
1820 // Check if argumentsList is an (unmodified) arguments object.
1821 __ ld(at, ContextMemOperand(a4, Context::SLOPPY_ARGUMENTS_MAP_INDEX));
1822 __ Branch(&create_arguments, eq, a2, Operand(at));
1823 __ ld(at, ContextMemOperand(a4, Context::STRICT_ARGUMENTS_MAP_INDEX));
1824 __ Branch(&create_arguments, eq, a2, Operand(at));
1825
1826 // Check if argumentsList is a fast JSArray.
1827 __ ld(v0, FieldMemOperand(a2, HeapObject::kMapOffset));
1828 __ lbu(v0, FieldMemOperand(v0, Map::kInstanceTypeOffset));
1829 __ Branch(&create_array, eq, v0, Operand(JS_ARRAY_TYPE));
1830
1831 // Ask the runtime to create the list (actually a FixedArray).
1832 __ bind(&create_runtime);
1833 {
1834 FrameScope scope(masm, StackFrame::INTERNAL);
1835 __ Push(a1, a3, a0);
1836 __ CallRuntime(Runtime::kCreateListFromArrayLike);
1837 __ mov(a0, v0);
1838 __ Pop(a1, a3);
1839 __ ld(a2, FieldMemOperand(v0, FixedArray::kLengthOffset));
1840 __ SmiUntag(a2);
1841 }
1842 __ Branch(&done_create);
1843
1844 // Try to create the list from an arguments object.
1845 __ bind(&create_arguments);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001846 __ ld(a2, FieldMemOperand(a0, JSArgumentsObject::kLengthOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001847 __ ld(a4, FieldMemOperand(a0, JSObject::kElementsOffset));
1848 __ ld(at, FieldMemOperand(a4, FixedArray::kLengthOffset));
1849 __ Branch(&create_runtime, ne, a2, Operand(at));
1850 __ SmiUntag(a2);
1851 __ mov(a0, a4);
1852 __ Branch(&done_create);
1853
1854 // Try to create the list from a JSArray object.
1855 __ bind(&create_array);
1856 __ ld(a2, FieldMemOperand(a2, Map::kBitField2Offset));
1857 __ DecodeField<Map::ElementsKindBits>(a2);
1858 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
1859 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
1860 STATIC_ASSERT(FAST_ELEMENTS == 2);
1861 __ Branch(&create_runtime, hi, a2, Operand(FAST_ELEMENTS));
1862 __ Branch(&create_runtime, eq, a2, Operand(FAST_HOLEY_SMI_ELEMENTS));
1863 __ ld(a2, FieldMemOperand(a0, JSArray::kLengthOffset));
1864 __ ld(a0, FieldMemOperand(a0, JSArray::kElementsOffset));
1865 __ SmiUntag(a2);
1866
1867 __ bind(&done_create);
1868 }
1869
1870 // Check for stack overflow.
1871 {
1872 // Check the stack for overflow. We are not trying to catch interruptions
1873 // (i.e. debug break and preemption) here, so check the "real stack limit".
1874 Label done;
1875 __ LoadRoot(a4, Heap::kRealStackLimitRootIndex);
1876 // Make ip the space we have left. The stack might already be overflowed
1877 // here which will cause ip to become negative.
1878 __ Dsubu(a4, sp, a4);
1879 // Check if the arguments will overflow the stack.
1880 __ dsll(at, a2, kPointerSizeLog2);
1881 __ Branch(&done, gt, a4, Operand(at)); // Signed comparison.
1882 __ TailCallRuntime(Runtime::kThrowStackOverflow);
1883 __ bind(&done);
1884 }
1885
1886 // ----------- S t a t e -------------
1887 // -- a1 : target
1888 // -- a0 : args (a FixedArray built from argumentsList)
1889 // -- a2 : len (number of elements to push from args)
1890 // -- a3 : new.target (checked to be constructor or undefined)
1891 // -- sp[0] : thisArgument
1892 // -----------------------------------
1893
1894 // Push arguments onto the stack (thisArgument is already on the stack).
1895 {
1896 __ mov(a4, zero_reg);
1897 Label done, loop;
1898 __ bind(&loop);
1899 __ Branch(&done, eq, a4, Operand(a2));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001900 __ Dlsa(at, a0, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001901 __ ld(at, FieldMemOperand(at, FixedArray::kHeaderSize));
1902 __ Push(at);
1903 __ Daddu(a4, a4, Operand(1));
1904 __ Branch(&loop);
1905 __ bind(&done);
1906 __ Move(a0, a4);
1907 }
1908
1909 // Dispatch to Call or Construct depending on whether new.target is undefined.
1910 {
1911 Label construct;
1912 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
1913 __ Branch(&construct, ne, a3, Operand(at));
1914 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1915 __ bind(&construct);
1916 __ Jump(masm->isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
1917 }
1918}
1919
Ben Murdoch097c5b22016-05-18 11:27:45 +01001920namespace {
1921
1922// Drops top JavaScript frame and an arguments adaptor frame below it (if
1923// present) preserving all the arguments prepared for current call.
1924// Does nothing if debugger is currently active.
1925// ES6 14.6.3. PrepareForTailCall
1926//
1927// Stack structure for the function g() tail calling f():
1928//
1929// ------- Caller frame: -------
1930// | ...
1931// | g()'s arg M
1932// | ...
1933// | g()'s arg 1
1934// | g()'s receiver arg
1935// | g()'s caller pc
1936// ------- g()'s frame: -------
1937// | g()'s caller fp <- fp
1938// | g()'s context
1939// | function pointer: g
1940// | -------------------------
1941// | ...
1942// | ...
1943// | f()'s arg N
1944// | ...
1945// | f()'s arg 1
1946// | f()'s receiver arg <- sp (f()'s caller pc is not on the stack yet!)
1947// ----------------------
1948//
1949void PrepareForTailCall(MacroAssembler* masm, Register args_reg,
1950 Register scratch1, Register scratch2,
1951 Register scratch3) {
1952 DCHECK(!AreAliased(args_reg, scratch1, scratch2, scratch3));
1953 Comment cmnt(masm, "[ PrepareForTailCall");
1954
Ben Murdochda12d292016-06-02 14:46:10 +01001955 // Prepare for tail call only if ES2015 tail call elimination is enabled.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001956 Label done;
Ben Murdochda12d292016-06-02 14:46:10 +01001957 ExternalReference is_tail_call_elimination_enabled =
1958 ExternalReference::is_tail_call_elimination_enabled_address(
1959 masm->isolate());
1960 __ li(at, Operand(is_tail_call_elimination_enabled));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001961 __ lb(scratch1, MemOperand(at));
Ben Murdochda12d292016-06-02 14:46:10 +01001962 __ Branch(&done, eq, scratch1, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001963
1964 // Drop possible interpreter handler/stub frame.
1965 {
1966 Label no_interpreter_frame;
Ben Murdochda12d292016-06-02 14:46:10 +01001967 __ ld(scratch3,
1968 MemOperand(fp, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001969 __ Branch(&no_interpreter_frame, ne, scratch3,
1970 Operand(Smi::FromInt(StackFrame::STUB)));
1971 __ ld(fp, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1972 __ bind(&no_interpreter_frame);
1973 }
1974
1975 // Check if next frame is an arguments adaptor frame.
Ben Murdochda12d292016-06-02 14:46:10 +01001976 Register caller_args_count_reg = scratch1;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001977 Label no_arguments_adaptor, formal_parameter_count_loaded;
1978 __ ld(scratch2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01001979 __ ld(scratch3,
1980 MemOperand(scratch2, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001981 __ Branch(&no_arguments_adaptor, ne, scratch3,
1982 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1983
Ben Murdochda12d292016-06-02 14:46:10 +01001984 // Drop current frame and load arguments count from arguments adaptor frame.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001985 __ mov(fp, scratch2);
Ben Murdochda12d292016-06-02 14:46:10 +01001986 __ ld(caller_args_count_reg,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001987 MemOperand(fp, ArgumentsAdaptorFrameConstants::kLengthOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01001988 __ SmiUntag(caller_args_count_reg);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001989 __ Branch(&formal_parameter_count_loaded);
1990
1991 __ bind(&no_arguments_adaptor);
1992 // Load caller's formal parameter count
Ben Murdochda12d292016-06-02 14:46:10 +01001993 __ ld(scratch1,
1994 MemOperand(fp, ArgumentsAdaptorFrameConstants::kFunctionOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001995 __ ld(scratch1,
1996 FieldMemOperand(scratch1, JSFunction::kSharedFunctionInfoOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01001997 __ lw(caller_args_count_reg,
Ben Murdoch097c5b22016-05-18 11:27:45 +01001998 FieldMemOperand(scratch1,
1999 SharedFunctionInfo::kFormalParameterCountOffset));
2000
2001 __ bind(&formal_parameter_count_loaded);
2002
Ben Murdochda12d292016-06-02 14:46:10 +01002003 ParameterCount callee_args_count(args_reg);
2004 __ PrepareForTailCall(callee_args_count, caller_args_count_reg, scratch2,
2005 scratch3);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002006 __ bind(&done);
2007}
2008} // namespace
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002009
2010// static
2011void Builtins::Generate_CallFunction(MacroAssembler* masm,
Ben Murdoch097c5b22016-05-18 11:27:45 +01002012 ConvertReceiverMode mode,
2013 TailCallMode tail_call_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002014 // ----------- S t a t e -------------
2015 // -- a0 : the number of arguments (not including the receiver)
2016 // -- a1 : the function to call (checked to be a JSFunction)
2017 // -----------------------------------
2018 __ AssertFunction(a1);
2019
2020 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
2021 // Check that function is not a "classConstructor".
2022 Label class_constructor;
2023 __ ld(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2024 __ lbu(a3, FieldMemOperand(a2, SharedFunctionInfo::kFunctionKindByteOffset));
2025 __ And(at, a3, Operand(SharedFunctionInfo::kClassConstructorBitsWithinByte));
2026 __ Branch(&class_constructor, ne, at, Operand(zero_reg));
2027
2028 // Enter the context of the function; ToObject has to run in the function
2029 // context, and we also need to take the global proxy from the function
2030 // context in case of conversion.
2031 STATIC_ASSERT(SharedFunctionInfo::kNativeByteOffset ==
2032 SharedFunctionInfo::kStrictModeByteOffset);
2033 __ ld(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
2034 // We need to convert the receiver for non-native sloppy mode functions.
2035 Label done_convert;
2036 __ lbu(a3, FieldMemOperand(a2, SharedFunctionInfo::kNativeByteOffset));
2037 __ And(at, a3, Operand((1 << SharedFunctionInfo::kNativeBitWithinByte) |
2038 (1 << SharedFunctionInfo::kStrictModeBitWithinByte)));
2039 __ Branch(&done_convert, ne, at, Operand(zero_reg));
2040 {
2041 // ----------- S t a t e -------------
2042 // -- a0 : the number of arguments (not including the receiver)
2043 // -- a1 : the function to call (checked to be a JSFunction)
2044 // -- a2 : the shared function info.
2045 // -- cp : the function context.
2046 // -----------------------------------
2047
2048 if (mode == ConvertReceiverMode::kNullOrUndefined) {
2049 // Patch receiver to global proxy.
2050 __ LoadGlobalProxy(a3);
2051 } else {
2052 Label convert_to_object, convert_receiver;
Ben Murdoch097c5b22016-05-18 11:27:45 +01002053 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002054 __ ld(a3, MemOperand(at));
2055 __ JumpIfSmi(a3, &convert_to_object);
2056 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
2057 __ GetObjectType(a3, a4, a4);
2058 __ Branch(&done_convert, hs, a4, Operand(FIRST_JS_RECEIVER_TYPE));
2059 if (mode != ConvertReceiverMode::kNotNullOrUndefined) {
2060 Label convert_global_proxy;
2061 __ JumpIfRoot(a3, Heap::kUndefinedValueRootIndex,
2062 &convert_global_proxy);
2063 __ JumpIfNotRoot(a3, Heap::kNullValueRootIndex, &convert_to_object);
2064 __ bind(&convert_global_proxy);
2065 {
2066 // Patch receiver to global proxy.
2067 __ LoadGlobalProxy(a3);
2068 }
2069 __ Branch(&convert_receiver);
2070 }
2071 __ bind(&convert_to_object);
2072 {
2073 // Convert receiver using ToObject.
2074 // TODO(bmeurer): Inline the allocation here to avoid building the frame
2075 // in the fast case? (fall back to AllocateInNewSpace?)
2076 FrameScope scope(masm, StackFrame::INTERNAL);
2077 __ SmiTag(a0);
2078 __ Push(a0, a1);
2079 __ mov(a0, a3);
2080 ToObjectStub stub(masm->isolate());
2081 __ CallStub(&stub);
2082 __ mov(a3, v0);
2083 __ Pop(a0, a1);
2084 __ SmiUntag(a0);
2085 }
2086 __ ld(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2087 __ bind(&convert_receiver);
2088 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002089 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002090 __ sd(a3, MemOperand(at));
2091 }
2092 __ bind(&done_convert);
2093
2094 // ----------- S t a t e -------------
2095 // -- a0 : the number of arguments (not including the receiver)
2096 // -- a1 : the function to call (checked to be a JSFunction)
2097 // -- a2 : the shared function info.
2098 // -- cp : the function context.
2099 // -----------------------------------
2100
Ben Murdoch097c5b22016-05-18 11:27:45 +01002101 if (tail_call_mode == TailCallMode::kAllow) {
2102 PrepareForTailCall(masm, a0, t0, t1, t2);
2103 }
2104
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002105 __ lw(a2,
2106 FieldMemOperand(a2, SharedFunctionInfo::kFormalParameterCountOffset));
2107 ParameterCount actual(a0);
2108 ParameterCount expected(a2);
2109 __ InvokeFunctionCode(a1, no_reg, expected, actual, JUMP_FUNCTION,
2110 CheckDebugStepCallWrapper());
2111
2112 // The function is a "classConstructor", need to raise an exception.
2113 __ bind(&class_constructor);
2114 {
2115 FrameScope frame(masm, StackFrame::INTERNAL);
2116 __ Push(a1);
2117 __ CallRuntime(Runtime::kThrowConstructorNonCallableError);
2118 }
2119}
2120
2121
2122// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01002123void Builtins::Generate_CallBoundFunctionImpl(MacroAssembler* masm,
2124 TailCallMode tail_call_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002125 // ----------- S t a t e -------------
2126 // -- a0 : the number of arguments (not including the receiver)
2127 // -- a1 : the function to call (checked to be a JSBoundFunction)
2128 // -----------------------------------
2129 __ AssertBoundFunction(a1);
2130
Ben Murdoch097c5b22016-05-18 11:27:45 +01002131 if (tail_call_mode == TailCallMode::kAllow) {
2132 PrepareForTailCall(masm, a0, t0, t1, t2);
2133 }
2134
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002135 // Patch the receiver to [[BoundThis]].
2136 {
2137 __ ld(at, FieldMemOperand(a1, JSBoundFunction::kBoundThisOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002138 __ Dlsa(a4, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002139 __ sd(at, MemOperand(a4));
2140 }
2141
2142 // Load [[BoundArguments]] into a2 and length of that into a4.
2143 __ ld(a2, FieldMemOperand(a1, JSBoundFunction::kBoundArgumentsOffset));
2144 __ ld(a4, FieldMemOperand(a2, FixedArray::kLengthOffset));
2145 __ SmiUntag(a4);
2146
2147 // ----------- S t a t e -------------
2148 // -- a0 : the number of arguments (not including the receiver)
2149 // -- a1 : the function to call (checked to be a JSBoundFunction)
2150 // -- a2 : the [[BoundArguments]] (implemented as FixedArray)
2151 // -- a4 : the number of [[BoundArguments]]
2152 // -----------------------------------
2153
2154 // Reserve stack space for the [[BoundArguments]].
2155 {
2156 Label done;
2157 __ dsll(a5, a4, kPointerSizeLog2);
2158 __ Dsubu(sp, sp, Operand(a5));
2159 // Check the stack for overflow. We are not trying to catch interruptions
2160 // (i.e. debug break and preemption) here, so check the "real stack limit".
2161 __ LoadRoot(at, Heap::kRealStackLimitRootIndex);
2162 __ Branch(&done, gt, sp, Operand(at)); // Signed comparison.
2163 // Restore the stack pointer.
2164 __ Daddu(sp, sp, Operand(a5));
2165 {
2166 FrameScope scope(masm, StackFrame::MANUAL);
2167 __ EnterFrame(StackFrame::INTERNAL);
2168 __ CallRuntime(Runtime::kThrowStackOverflow);
2169 }
2170 __ bind(&done);
2171 }
2172
2173 // Relocate arguments down the stack.
2174 {
2175 Label loop, done_loop;
2176 __ mov(a5, zero_reg);
2177 __ bind(&loop);
2178 __ Branch(&done_loop, gt, a5, Operand(a0));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002179 __ Dlsa(a6, sp, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002180 __ ld(at, MemOperand(a6));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002181 __ Dlsa(a6, sp, a5, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002182 __ sd(at, MemOperand(a6));
2183 __ Daddu(a4, a4, Operand(1));
2184 __ Daddu(a5, a5, Operand(1));
2185 __ Branch(&loop);
2186 __ bind(&done_loop);
2187 }
2188
2189 // Copy [[BoundArguments]] to the stack (below the arguments).
2190 {
2191 Label loop, done_loop;
2192 __ ld(a4, FieldMemOperand(a2, FixedArray::kLengthOffset));
2193 __ SmiUntag(a4);
2194 __ Daddu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2195 __ bind(&loop);
2196 __ Dsubu(a4, a4, Operand(1));
2197 __ Branch(&done_loop, lt, a4, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002198 __ Dlsa(a5, a2, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002199 __ ld(at, MemOperand(a5));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002200 __ Dlsa(a5, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002201 __ sd(at, MemOperand(a5));
2202 __ Daddu(a0, a0, Operand(1));
2203 __ Branch(&loop);
2204 __ bind(&done_loop);
2205 }
2206
2207 // Call the [[BoundTargetFunction]] via the Call builtin.
2208 __ ld(a1, FieldMemOperand(a1, JSBoundFunction::kBoundTargetFunctionOffset));
2209 __ li(at, Operand(ExternalReference(Builtins::kCall_ReceiverIsAny,
2210 masm->isolate())));
2211 __ ld(at, MemOperand(at));
2212 __ Daddu(at, at, Operand(Code::kHeaderSize - kHeapObjectTag));
2213 __ Jump(at);
2214}
2215
2216
2217// static
Ben Murdoch097c5b22016-05-18 11:27:45 +01002218void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode,
2219 TailCallMode tail_call_mode) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002220 // ----------- S t a t e -------------
2221 // -- a0 : the number of arguments (not including the receiver)
2222 // -- a1 : the target to call (can be any Object).
2223 // -----------------------------------
2224
2225 Label non_callable, non_function, non_smi;
2226 __ JumpIfSmi(a1, &non_callable);
2227 __ bind(&non_smi);
2228 __ GetObjectType(a1, t1, t2);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002229 __ Jump(masm->isolate()->builtins()->CallFunction(mode, tail_call_mode),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002230 RelocInfo::CODE_TARGET, eq, t2, Operand(JS_FUNCTION_TYPE));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002231 __ Jump(masm->isolate()->builtins()->CallBoundFunction(tail_call_mode),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002232 RelocInfo::CODE_TARGET, eq, t2, Operand(JS_BOUND_FUNCTION_TYPE));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002233
2234 // Check if target has a [[Call]] internal method.
2235 __ lbu(t1, FieldMemOperand(t1, Map::kBitFieldOffset));
2236 __ And(t1, t1, Operand(1 << Map::kIsCallable));
2237 __ Branch(&non_callable, eq, t1, Operand(zero_reg));
2238
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002239 __ Branch(&non_function, ne, t2, Operand(JS_PROXY_TYPE));
2240
Ben Murdoch097c5b22016-05-18 11:27:45 +01002241 // 0. Prepare for tail call if necessary.
2242 if (tail_call_mode == TailCallMode::kAllow) {
2243 PrepareForTailCall(masm, a0, t0, t1, t2);
2244 }
2245
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002246 // 1. Runtime fallback for Proxy [[Call]].
2247 __ Push(a1);
2248 // Increase the arguments size to include the pushed function and the
2249 // existing receiver on the stack.
2250 __ Daddu(a0, a0, 2);
2251 // Tail-call to the runtime.
2252 __ JumpToExternalReference(
2253 ExternalReference(Runtime::kJSProxyCall, masm->isolate()));
2254
2255 // 2. Call to something else, which might have a [[Call]] internal method (if
2256 // not we raise an exception).
2257 __ bind(&non_function);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002258 // Overwrite the original receiver with the (original) target.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002259 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002260 __ sd(a1, MemOperand(at));
2261 // Let the "call_as_function_delegate" take care of the rest.
2262 __ LoadNativeContextSlot(Context::CALL_AS_FUNCTION_DELEGATE_INDEX, a1);
2263 __ Jump(masm->isolate()->builtins()->CallFunction(
Ben Murdoch097c5b22016-05-18 11:27:45 +01002264 ConvertReceiverMode::kNotNullOrUndefined, tail_call_mode),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002265 RelocInfo::CODE_TARGET);
2266
2267 // 3. Call to something that is not callable.
2268 __ bind(&non_callable);
2269 {
2270 FrameScope scope(masm, StackFrame::INTERNAL);
2271 __ Push(a1);
2272 __ CallRuntime(Runtime::kThrowCalledNonCallable);
2273 }
2274}
2275
2276
2277void Builtins::Generate_ConstructFunction(MacroAssembler* masm) {
2278 // ----------- S t a t e -------------
2279 // -- a0 : the number of arguments (not including the receiver)
2280 // -- a1 : the constructor to call (checked to be a JSFunction)
2281 // -- a3 : the new target (checked to be a constructor)
2282 // -----------------------------------
2283 __ AssertFunction(a1);
2284
2285 // Calling convention for function specific ConstructStubs require
2286 // a2 to contain either an AllocationSite or undefined.
2287 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2288
2289 // Tail call to the function-specific construct stub (still in the caller
2290 // context at this point).
2291 __ ld(a4, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2292 __ ld(a4, FieldMemOperand(a4, SharedFunctionInfo::kConstructStubOffset));
2293 __ Daddu(at, a4, Operand(Code::kHeaderSize - kHeapObjectTag));
2294 __ Jump(at);
2295}
2296
2297
2298// static
2299void Builtins::Generate_ConstructBoundFunction(MacroAssembler* masm) {
2300 // ----------- S t a t e -------------
2301 // -- a0 : the number of arguments (not including the receiver)
2302 // -- a1 : the function to call (checked to be a JSBoundFunction)
2303 // -- a3 : the new target (checked to be a constructor)
2304 // -----------------------------------
2305 __ AssertBoundFunction(a1);
2306
2307 // Load [[BoundArguments]] into a2 and length of that into a4.
2308 __ ld(a2, FieldMemOperand(a1, JSBoundFunction::kBoundArgumentsOffset));
2309 __ ld(a4, FieldMemOperand(a2, FixedArray::kLengthOffset));
2310 __ SmiUntag(a4);
2311
2312 // ----------- S t a t e -------------
2313 // -- a0 : the number of arguments (not including the receiver)
2314 // -- a1 : the function to call (checked to be a JSBoundFunction)
2315 // -- a2 : the [[BoundArguments]] (implemented as FixedArray)
2316 // -- a3 : the new target (checked to be a constructor)
2317 // -- a4 : the number of [[BoundArguments]]
2318 // -----------------------------------
2319
2320 // Reserve stack space for the [[BoundArguments]].
2321 {
2322 Label done;
2323 __ dsll(a5, a4, kPointerSizeLog2);
2324 __ Dsubu(sp, sp, Operand(a5));
2325 // Check the stack for overflow. We are not trying to catch interruptions
2326 // (i.e. debug break and preemption) here, so check the "real stack limit".
2327 __ LoadRoot(at, Heap::kRealStackLimitRootIndex);
2328 __ Branch(&done, gt, sp, Operand(at)); // Signed comparison.
2329 // Restore the stack pointer.
2330 __ Daddu(sp, sp, Operand(a5));
2331 {
2332 FrameScope scope(masm, StackFrame::MANUAL);
2333 __ EnterFrame(StackFrame::INTERNAL);
2334 __ CallRuntime(Runtime::kThrowStackOverflow);
2335 }
2336 __ bind(&done);
2337 }
2338
2339 // Relocate arguments down the stack.
2340 {
2341 Label loop, done_loop;
2342 __ mov(a5, zero_reg);
2343 __ bind(&loop);
2344 __ Branch(&done_loop, ge, a5, Operand(a0));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002345 __ Dlsa(a6, sp, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002346 __ ld(at, MemOperand(a6));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002347 __ Dlsa(a6, sp, a5, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002348 __ sd(at, MemOperand(a6));
2349 __ Daddu(a4, a4, Operand(1));
2350 __ Daddu(a5, a5, Operand(1));
2351 __ Branch(&loop);
2352 __ bind(&done_loop);
2353 }
2354
2355 // Copy [[BoundArguments]] to the stack (below the arguments).
2356 {
2357 Label loop, done_loop;
2358 __ ld(a4, FieldMemOperand(a2, FixedArray::kLengthOffset));
2359 __ SmiUntag(a4);
2360 __ Daddu(a2, a2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2361 __ bind(&loop);
2362 __ Dsubu(a4, a4, Operand(1));
2363 __ Branch(&done_loop, lt, a4, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002364 __ Dlsa(a5, a2, a4, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002365 __ ld(at, MemOperand(a5));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002366 __ Dlsa(a5, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002367 __ sd(at, MemOperand(a5));
2368 __ Daddu(a0, a0, Operand(1));
2369 __ Branch(&loop);
2370 __ bind(&done_loop);
2371 }
2372
2373 // Patch new.target to [[BoundTargetFunction]] if new.target equals target.
2374 {
2375 Label skip_load;
2376 __ Branch(&skip_load, ne, a1, Operand(a3));
2377 __ ld(a3, FieldMemOperand(a1, JSBoundFunction::kBoundTargetFunctionOffset));
2378 __ bind(&skip_load);
2379 }
2380
2381 // Construct the [[BoundTargetFunction]] via the Construct builtin.
2382 __ ld(a1, FieldMemOperand(a1, JSBoundFunction::kBoundTargetFunctionOffset));
2383 __ li(at, Operand(ExternalReference(Builtins::kConstruct, masm->isolate())));
2384 __ ld(at, MemOperand(at));
2385 __ Daddu(at, at, Operand(Code::kHeaderSize - kHeapObjectTag));
2386 __ Jump(at);
2387}
2388
2389
2390// static
2391void Builtins::Generate_ConstructProxy(MacroAssembler* masm) {
2392 // ----------- S t a t e -------------
2393 // -- a0 : the number of arguments (not including the receiver)
2394 // -- a1 : the constructor to call (checked to be a JSProxy)
2395 // -- a3 : the new target (either the same as the constructor or
2396 // the JSFunction on which new was invoked initially)
2397 // -----------------------------------
2398
2399 // Call into the Runtime for Proxy [[Construct]].
2400 __ Push(a1, a3);
2401 // Include the pushed new_target, constructor and the receiver.
2402 __ Daddu(a0, a0, Operand(3));
2403 // Tail-call to the runtime.
2404 __ JumpToExternalReference(
2405 ExternalReference(Runtime::kJSProxyConstruct, masm->isolate()));
2406}
2407
2408
2409// static
2410void Builtins::Generate_Construct(MacroAssembler* masm) {
2411 // ----------- S t a t e -------------
2412 // -- a0 : the number of arguments (not including the receiver)
2413 // -- a1 : the constructor to call (can be any Object)
2414 // -- a3 : the new target (either the same as the constructor or
2415 // the JSFunction on which new was invoked initially)
2416 // -----------------------------------
2417
2418 // Check if target is a Smi.
2419 Label non_constructor;
2420 __ JumpIfSmi(a1, &non_constructor);
2421
2422 // Dispatch based on instance type.
2423 __ ld(t1, FieldMemOperand(a1, HeapObject::kMapOffset));
2424 __ lbu(t2, FieldMemOperand(t1, Map::kInstanceTypeOffset));
2425 __ Jump(masm->isolate()->builtins()->ConstructFunction(),
2426 RelocInfo::CODE_TARGET, eq, t2, Operand(JS_FUNCTION_TYPE));
2427
2428 // Check if target has a [[Construct]] internal method.
2429 __ lbu(t3, FieldMemOperand(t1, Map::kBitFieldOffset));
2430 __ And(t3, t3, Operand(1 << Map::kIsConstructor));
2431 __ Branch(&non_constructor, eq, t3, Operand(zero_reg));
2432
2433 // Only dispatch to bound functions after checking whether they are
2434 // constructors.
2435 __ Jump(masm->isolate()->builtins()->ConstructBoundFunction(),
2436 RelocInfo::CODE_TARGET, eq, t2, Operand(JS_BOUND_FUNCTION_TYPE));
2437
2438 // Only dispatch to proxies after checking whether they are constructors.
2439 __ Jump(masm->isolate()->builtins()->ConstructProxy(), RelocInfo::CODE_TARGET,
2440 eq, t2, Operand(JS_PROXY_TYPE));
2441
2442 // Called Construct on an exotic Object with a [[Construct]] internal method.
2443 {
2444 // Overwrite the original receiver with the (original) target.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002445 __ Dlsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002446 __ sd(a1, MemOperand(at));
2447 // Let the "call_as_constructor_delegate" take care of the rest.
2448 __ LoadNativeContextSlot(Context::CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, a1);
2449 __ Jump(masm->isolate()->builtins()->CallFunction(),
2450 RelocInfo::CODE_TARGET);
2451 }
2452
2453 // Called Construct on an Object that doesn't have a [[Construct]] internal
2454 // method.
2455 __ bind(&non_constructor);
2456 __ Jump(masm->isolate()->builtins()->ConstructedNonConstructable(),
2457 RelocInfo::CODE_TARGET);
2458}
2459
2460
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002461void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
2462 // State setup as expected by MacroAssembler::InvokePrologue.
2463 // ----------- S t a t e -------------
2464 // -- a0: actual arguments count
2465 // -- a1: function (passed through to callee)
2466 // -- a2: expected arguments count
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002467 // -- a3: new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002468 // -----------------------------------
2469
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002470 Label invoke, dont_adapt_arguments, stack_overflow;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002471
2472 Label enough, too_few;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002473 __ Branch(&dont_adapt_arguments, eq,
2474 a2, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
2475 // We use Uless as the number of argument should always be greater than 0.
2476 __ Branch(&too_few, Uless, a0, Operand(a2));
2477
2478 { // Enough parameters: actual >= expected.
2479 // a0: actual number of arguments as a smi
2480 // a1: function
2481 // a2: expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002482 // a3: new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002483 __ bind(&enough);
2484 EnterArgumentsAdaptorFrame(masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002485 ArgumentAdaptorStackCheck(masm, &stack_overflow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002486
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002487 // Calculate copy start address into a0 and copy end address into a4.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002488 __ SmiScale(a0, a0, kPointerSizeLog2);
2489 __ Daddu(a0, fp, a0);
2490 // Adjust for return address and receiver.
2491 __ Daddu(a0, a0, Operand(2 * kPointerSize));
2492 // Compute copy end address.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002493 __ dsll(a4, a2, kPointerSizeLog2);
2494 __ dsubu(a4, a0, a4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002495
2496 // Copy the arguments (including the receiver) to the new stack frame.
2497 // a0: copy start address
2498 // a1: function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002499 // a2: expected number of arguments
2500 // a3: new target (passed through to callee)
2501 // a4: copy end address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002502
2503 Label copy;
2504 __ bind(&copy);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002505 __ ld(a5, MemOperand(a0));
2506 __ push(a5);
2507 __ Branch(USE_DELAY_SLOT, &copy, ne, a0, Operand(a4));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002508 __ daddiu(a0, a0, -kPointerSize); // In delay slot.
2509
2510 __ jmp(&invoke);
2511 }
2512
2513 { // Too few parameters: Actual < expected.
2514 __ bind(&too_few);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002515 EnterArgumentsAdaptorFrame(masm);
2516 ArgumentAdaptorStackCheck(masm, &stack_overflow);
2517
2518 // Calculate copy start address into a0 and copy end address into a7.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002519 // a0: actual number of arguments as a smi
2520 // a1: function
2521 // a2: expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002522 // a3: new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002523 __ SmiScale(a0, a0, kPointerSizeLog2);
2524 __ Daddu(a0, fp, a0);
2525 // Adjust for return address and receiver.
2526 __ Daddu(a0, a0, Operand(2 * kPointerSize));
2527 // Compute copy end address. Also adjust for return address.
2528 __ Daddu(a7, fp, kPointerSize);
2529
2530 // Copy the arguments (including the receiver) to the new stack frame.
2531 // a0: copy start address
2532 // a1: function
2533 // a2: expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002534 // a3: new target (passed through to callee)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002535 // a7: copy end address
2536 Label copy;
2537 __ bind(&copy);
2538 __ ld(a4, MemOperand(a0)); // Adjusted above for return addr and receiver.
2539 __ Dsubu(sp, sp, kPointerSize);
2540 __ Dsubu(a0, a0, kPointerSize);
2541 __ Branch(USE_DELAY_SLOT, &copy, ne, a0, Operand(a7));
2542 __ sd(a4, MemOperand(sp)); // In the delay slot.
2543
2544 // Fill the remaining expected arguments with undefined.
2545 // a1: function
2546 // a2: expected number of arguments
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002547 // a3: new target (passed through to callee)
2548 __ LoadRoot(a5, Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002549 __ dsll(a6, a2, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002550 __ Dsubu(a4, fp, Operand(a6));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002551 // Adjust for frame.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002552 __ Dsubu(a4, a4, Operand(StandardFrameConstants::kFixedFrameSizeFromFp +
2553 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002554
2555 Label fill;
2556 __ bind(&fill);
2557 __ Dsubu(sp, sp, kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002558 __ Branch(USE_DELAY_SLOT, &fill, ne, sp, Operand(a4));
2559 __ sd(a5, MemOperand(sp));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002560 }
2561
2562 // Call the entry point.
2563 __ bind(&invoke);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002564 __ mov(a0, a2);
2565 // a0 : expected number of arguments
2566 // a1 : function (passed through to callee)
2567 // a3: new target (passed through to callee)
2568 __ ld(a4, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
2569 __ Call(a4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002570
2571 // Store offset of return address for deoptimizer.
2572 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
2573
2574 // Exit frame and return.
2575 LeaveArgumentsAdaptorFrame(masm);
2576 __ Ret();
2577
2578
2579 // -------------------------------------------
2580 // Don't adapt arguments.
2581 // -------------------------------------------
2582 __ bind(&dont_adapt_arguments);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002583 __ ld(a4, FieldMemOperand(a1, JSFunction::kCodeEntryOffset));
2584 __ Jump(a4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002585
2586 __ bind(&stack_overflow);
2587 {
2588 FrameScope frame(masm, StackFrame::MANUAL);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002589 __ CallRuntime(Runtime::kThrowStackOverflow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002590 __ break_(0xCC);
2591 }
2592}
2593
2594
2595#undef __
2596
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002597} // namespace internal
2598} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002599
2600#endif // V8_TARGET_ARCH_MIPS64