V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
builtins-s390.cc
1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #if V8_TARGET_ARCH_S390
6 
7 #include "src/assembler-inl.h"
8 #include "src/code-factory.h"
9 #include "src/code-stubs.h"
10 #include "src/counters.h"
11 #include "src/debug/debug.h"
12 #include "src/deoptimizer.h"
13 #include "src/frame-constants.h"
14 #include "src/frames.h"
15 #include "src/objects/js-generator.h"
16 #include "src/objects/smi.h"
17 #include "src/register-configuration.h"
18 #include "src/runtime/runtime.h"
19 #include "src/wasm/wasm-objects.h"
20 
21 namespace v8 {
22 namespace internal {
23 
24 #define __ ACCESS_MASM(masm)
25 
26 void Builtins::Generate_Adaptor(MacroAssembler* masm, Address address,
27  ExitFrameType exit_frame_type) {
28  __ Move(kJavaScriptCallExtraArg1Register, ExternalReference::Create(address));
29  if (exit_frame_type == BUILTIN_EXIT) {
30  __ Jump(BUILTIN_CODE(masm->isolate(), AdaptorWithBuiltinExitFrame),
31  RelocInfo::CODE_TARGET);
32  } else {
33  DCHECK(exit_frame_type == EXIT);
34  __ Jump(BUILTIN_CODE(masm->isolate(), AdaptorWithExitFrame),
35  RelocInfo::CODE_TARGET);
36  }
37 }
38 
39 void Builtins::Generate_InternalArrayConstructor(MacroAssembler* masm) {
40  // ----------- S t a t e -------------
41  // -- r2 : number of arguments
42  // -- lr : return address
43  // -- sp[...]: constructor arguments
44  // -----------------------------------
45  Label generic_array_code, one_or_more_arguments, two_or_more_arguments;
46 
47  if (FLAG_debug_code) {
48  // Initial map for the builtin InternalArray functions should be maps.
49  __ LoadP(r4, FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
50  __ TestIfSmi(r4);
51  __ Assert(ne, AbortReason::kUnexpectedInitialMapForInternalArrayFunction,
52  cr0);
53  __ CompareObjectType(r4, r5, r6, MAP_TYPE);
54  __ Assert(eq, AbortReason::kUnexpectedInitialMapForInternalArrayFunction);
55  }
56 
57  // Run the native code for the InternalArray function called as a normal
58  // function.
59  __ Jump(BUILTIN_CODE(masm->isolate(), InternalArrayConstructorImpl),
60  RelocInfo::CODE_TARGET);
61 }
62 
63 static void GenerateTailCallToReturnedCode(MacroAssembler* masm,
64  Runtime::FunctionId function_id) {
65  // ----------- S t a t e -------------
66  // -- r2 : argument count (preserved for callee)
67  // -- r3 : target function (preserved for callee)
68  // -- r5 : new target (preserved for callee)
69  // -----------------------------------
70  {
71  FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
72  // Push the number of arguments to the callee.
73  // Push a copy of the target function and the new target.
74  // Push function as parameter to the runtime call.
75  __ SmiTag(r2);
76  __ Push(r2, r3, r5, r3);
77 
78  __ CallRuntime(function_id, 1);
79  __ LoadRR(r4, r2);
80 
81  // Restore target function and new target.
82  __ Pop(r2, r3, r5);
83  __ SmiUntag(r2);
84  }
85  static_assert(kJavaScriptCallCodeStartRegister == r4, "ABI mismatch");
86  __ AddP(r4, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
87  __ JumpToJSEntry(r4);
88 }
89 
90 namespace {
91 
92 void Generate_JSBuiltinsConstructStubHelper(MacroAssembler* masm) {
93  Label post_instantiation_deopt_entry;
94  // ----------- S t a t e -------------
95  // -- r2 : number of arguments
96  // -- r3 : constructor function
97  // -- r5 : new target
98  // -- cp : context
99  // -- lr : return address
100  // -- sp[...]: constructor arguments
101  // -----------------------------------
102 
103  // Enter a construct frame.
104  {
105  FrameAndConstantPoolScope scope(masm, StackFrame::CONSTRUCT);
106 
107  // Preserve the incoming parameters on the stack.
108  __ SmiTag(r2);
109  __ Push(cp, r2);
110  __ SmiUntag(r2);
111  // The receiver for the builtin/api call.
112  __ PushRoot(RootIndex::kTheHoleValue);
113  // Set up pointer to last argument.
114  __ la(r6, MemOperand(fp, StandardFrameConstants::kCallerSPOffset));
115 
116  // Copy arguments and receiver to the expression stack.
117  // r2: number of arguments
118  // r3: constructor function
119  // r4: address of last argument (caller sp)
120  // r5: new target
121  // cr0: condition indicating whether r2 is zero
122  // sp[0]: receiver
123  // sp[1]: receiver
124  // sp[2]: number of arguments (smi-tagged)
125  Label loop, no_args;
126  __ beq(&no_args);
127  __ ShiftLeftP(ip, r2, Operand(kPointerSizeLog2));
128  __ SubP(sp, sp, ip);
129  __ LoadRR(r1, r2);
130  __ bind(&loop);
131  __ lay(ip, MemOperand(ip, -kPointerSize));
132  __ LoadP(r0, MemOperand(ip, r6));
133  __ StoreP(r0, MemOperand(ip, sp));
134  __ BranchOnCount(r1, &loop);
135  __ bind(&no_args);
136 
137  // Call the function.
138  // r2: number of arguments
139  // r3: constructor function
140  // r5: new target
141 
142  ParameterCount actual(r2);
143  __ InvokeFunction(r3, r5, actual, CALL_FUNCTION);
144 
145  // Restore context from the frame.
146  __ LoadP(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
147  // Restore smi-tagged arguments count from the frame.
148  __ LoadP(r3, MemOperand(fp, ConstructFrameConstants::kLengthOffset));
149 
150  // Leave construct frame.
151  }
152  // Remove caller arguments from the stack and return.
153  STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
154 
155  __ SmiToPtrArrayOffset(r3, r3);
156  __ AddP(sp, sp, r3);
157  __ AddP(sp, sp, Operand(kPointerSize));
158  __ Ret();
159 }
160 
161 void Generate_StackOverflowCheck(MacroAssembler* masm, Register num_args,
162  Register scratch, Label* stack_overflow) {
163  // Check the stack for overflow. We are not trying to catch
164  // interruptions (e.g. debug break and preemption) here, so the "real stack
165  // limit" is checked.
166  __ LoadRoot(scratch, RootIndex::kRealStackLimit);
167  // Make scratch the space we have left. The stack might already be overflowed
168  // here which will cause scratch to become negative.
169  __ SubP(scratch, sp, scratch);
170  // Check if the arguments will overflow the stack.
171  __ ShiftLeftP(r0, num_args, Operand(kPointerSizeLog2));
172  __ CmpP(scratch, r0);
173  __ ble(stack_overflow); // Signed comparison.
174 }
175 
176 } // namespace
177 
178 // The construct stub for ES5 constructor functions and ES6 class constructors.
179 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
180  // ----------- S t a t e -------------
181  // -- r2: number of arguments (untagged)
182  // -- r3: constructor function
183  // -- r5: new target
184  // -- cp: context
185  // -- lr: return address
186  // -- sp[...]: constructor arguments
187  // -----------------------------------
188 
189  // Enter a construct frame.
190  {
191  FrameAndConstantPoolScope scope(masm, StackFrame::CONSTRUCT);
192  Label post_instantiation_deopt_entry, not_create_implicit_receiver;
193 
194  // Preserve the incoming parameters on the stack.
195  __ SmiTag(r2);
196  __ Push(cp, r2, r3);
197  __ PushRoot(RootIndex::kUndefinedValue);
198  __ Push(r5);
199 
200  // ----------- S t a t e -------------
201  // -- sp[0*kPointerSize]: new target
202  // -- sp[1*kPointerSize]: padding
203  // -- r3 and sp[2*kPointerSize]: constructor function
204  // -- sp[3*kPointerSize]: number of arguments (tagged)
205  // -- sp[4*kPointerSize]: context
206  // -----------------------------------
207 
208  __ LoadP(r6, FieldMemOperand(r3, JSFunction::kSharedFunctionInfoOffset));
209  __ LoadlW(r6, FieldMemOperand(r6, SharedFunctionInfo::kFlagsOffset));
210  __ TestBitMask(r6, SharedFunctionInfo::IsDerivedConstructorBit::kMask, r0);
211  __ bne(&not_create_implicit_receiver);
212 
213  // If not derived class constructor: Allocate the new receiver object.
214  __ IncrementCounter(masm->isolate()->counters()->constructed_objects(), 1,
215  r6, r7);
216  __ Call(BUILTIN_CODE(masm->isolate(), FastNewObject),
217  RelocInfo::CODE_TARGET);
218  __ b(&post_instantiation_deopt_entry);
219 
220  // Else: use TheHoleValue as receiver for constructor call
221  __ bind(&not_create_implicit_receiver);
222  __ LoadRoot(r2, RootIndex::kTheHoleValue);
223 
224  // ----------- S t a t e -------------
225  // -- r2: receiver
226  // -- Slot 4 / sp[0*kPointerSize]: new target
227  // -- Slot 3 / sp[1*kPointerSize]: padding
228  // -- Slot 2 / sp[2*kPointerSize]: constructor function
229  // -- Slot 1 / sp[3*kPointerSize]: number of arguments (tagged)
230  // -- Slot 0 / sp[4*kPointerSize]: context
231  // -----------------------------------
232  // Deoptimizer enters here.
233  masm->isolate()->heap()->SetConstructStubCreateDeoptPCOffset(
234  masm->pc_offset());
235  __ bind(&post_instantiation_deopt_entry);
236 
237  // Restore new target.
238  __ Pop(r5);
239  // Push the allocated receiver to the stack. We need two copies
240  // because we may have to return the original one and the calling
241  // conventions dictate that the called function pops the receiver.
242  __ Push(r2, r2);
243 
244  // ----------- S t a t e -------------
245  // -- r5: new target
246  // -- sp[0*kPointerSize]: implicit receiver
247  // -- sp[1*kPointerSize]: implicit receiver
248  // -- sp[2*kPointerSize]: padding
249  // -- sp[3*kPointerSize]: constructor function
250  // -- sp[4*kPointerSize]: number of arguments (tagged)
251  // -- sp[5*kPointerSize]: context
252  // -----------------------------------
253 
254  // Restore constructor function and argument count.
255  __ LoadP(r3, MemOperand(fp, ConstructFrameConstants::kConstructorOffset));
256  __ LoadP(r2, MemOperand(fp, ConstructFrameConstants::kLengthOffset));
257  __ SmiUntag(r2);
258 
259  // Set up pointer to last argument.
260  __ la(r6, MemOperand(fp, StandardFrameConstants::kCallerSPOffset));
261 
262  Label enough_stack_space, stack_overflow;
263  Generate_StackOverflowCheck(masm, r2, r7, &stack_overflow);
264  __ b(&enough_stack_space);
265 
266  __ bind(&stack_overflow);
267  // Restore the context from the frame.
268  __ LoadP(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
269  __ CallRuntime(Runtime::kThrowStackOverflow);
270  // Unreachable code.
271  __ bkpt(0);
272 
273  __ bind(&enough_stack_space);
274 
275  // Copy arguments and receiver to the expression stack.
276  Label loop, no_args;
277  // ----------- S t a t e -------------
278  // -- r2: number of arguments (untagged)
279  // -- r5: new target
280  // -- r6: pointer to last argument
281  // -- cr0: condition indicating whether r2 is zero
282  // -- sp[0*kPointerSize]: implicit receiver
283  // -- sp[1*kPointerSize]: implicit receiver
284  // -- sp[2*kPointerSize]: padding
285  // -- r3 and sp[3*kPointerSize]: constructor function
286  // -- sp[4*kPointerSize]: number of arguments (tagged)
287  // -- sp[5*kPointerSize]: context
288  // -----------------------------------
289 
290  __ ltgr(r2, r2);
291  __ beq(&no_args);
292  __ ShiftLeftP(ip, r2, Operand(kPointerSizeLog2));
293  __ SubP(sp, sp, ip);
294  __ LoadRR(r1, r2);
295  __ bind(&loop);
296  __ lay(ip, MemOperand(ip, -kPointerSize));
297  __ LoadP(r0, MemOperand(ip, r6));
298  __ StoreP(r0, MemOperand(ip, sp));
299  __ BranchOnCount(r1, &loop);
300  __ bind(&no_args);
301 
302  // Call the function.
303  ParameterCount actual(r2);
304  __ InvokeFunction(r3, r5, actual, CALL_FUNCTION);
305 
306  // ----------- S t a t e -------------
307  // -- r0: constructor result
308  // -- sp[0*kPointerSize]: implicit receiver
309  // -- sp[1*kPointerSize]: padding
310  // -- sp[2*kPointerSize]: constructor function
311  // -- sp[3*kPointerSize]: number of arguments
312  // -- sp[4*kPointerSize]: context
313  // -----------------------------------
314 
315  // Store offset of return address for deoptimizer.
316  masm->isolate()->heap()->SetConstructStubInvokeDeoptPCOffset(
317  masm->pc_offset());
318 
319  // Restore the context from the frame.
320  __ LoadP(cp, MemOperand(fp, ConstructFrameConstants::kContextOffset));
321 
322  // If the result is an object (in the ECMA sense), we should get rid
323  // of the receiver and use the result; see ECMA-262 section 13.2.2-7
324  // on page 74.
325  Label use_receiver, do_throw, leave_frame;
326 
327  // If the result is undefined, we jump out to using the implicit receiver.
328  __ JumpIfRoot(r2, RootIndex::kUndefinedValue, &use_receiver);
329 
330  // Otherwise we do a smi check and fall through to check if the return value
331  // is a valid receiver.
332 
333  // If the result is a smi, it is *not* an object in the ECMA sense.
334  __ JumpIfSmi(r2, &use_receiver);
335 
336  // If the type of the result (stored in its map) is less than
337  // FIRST_JS_RECEIVER_TYPE, it is not an object in the ECMA sense.
338  STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
339  __ CompareObjectType(r2, r6, r6, FIRST_JS_RECEIVER_TYPE);
340  __ bge(&leave_frame);
341  __ b(&use_receiver);
342 
343  __ bind(&do_throw);
344  __ CallRuntime(Runtime::kThrowConstructorReturnedNonObject);
345 
346  // Throw away the result of the constructor invocation and use the
347  // on-stack receiver as the result.
348  __ bind(&use_receiver);
349  __ LoadP(r2, MemOperand(sp));
350  __ JumpIfRoot(r2, RootIndex::kTheHoleValue, &do_throw);
351 
352  __ bind(&leave_frame);
353  // Restore smi-tagged arguments count from the frame.
354  __ LoadP(r3, MemOperand(fp, ConstructFrameConstants::kLengthOffset));
355  // Leave construct frame.
356  }
357 
358  // Remove caller arguments from the stack and return.
359  STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
360 
361  __ SmiToPtrArrayOffset(r3, r3);
362  __ AddP(sp, sp, r3);
363  __ AddP(sp, sp, Operand(kPointerSize));
364  __ Ret();
365 }
366 
367 void Builtins::Generate_JSBuiltinsConstructStub(MacroAssembler* masm) {
368  Generate_JSBuiltinsConstructStubHelper(masm);
369 }
370 
371 static void GetSharedFunctionInfoBytecode(MacroAssembler* masm,
372  Register sfi_data,
373  Register scratch1) {
374  Label done;
375 
376  __ CompareObjectType(sfi_data, scratch1, scratch1, INTERPRETER_DATA_TYPE);
377  __ bne(&done, Label::kNear);
378  __ LoadP(sfi_data,
379  FieldMemOperand(sfi_data, InterpreterData::kBytecodeArrayOffset));
380  __ bind(&done);
381 }
382 
383 // static
384 void Builtins::Generate_ResumeGeneratorTrampoline(MacroAssembler* masm) {
385  // ----------- S t a t e -------------
386  // -- r2 : the value to pass to the generator
387  // -- r3 : the JSGeneratorObject to resume
388  // -- lr : return address
389  // -----------------------------------
390  __ AssertGeneratorObject(r3);
391 
392  // Store input value into generator object.
393  __ StoreP(r2, FieldMemOperand(r3, JSGeneratorObject::kInputOrDebugPosOffset),
394  r0);
395  __ RecordWriteField(r3, JSGeneratorObject::kInputOrDebugPosOffset, r2, r5,
396  kLRHasNotBeenSaved, kDontSaveFPRegs);
397 
398  // Load suspended function and context.
399  __ LoadP(r6, FieldMemOperand(r3, JSGeneratorObject::kFunctionOffset));
400  __ LoadP(cp, FieldMemOperand(r6, JSFunction::kContextOffset));
401 
402  // Flood function if we are stepping.
403  Label prepare_step_in_if_stepping, prepare_step_in_suspended_generator;
404  Label stepping_prepared;
405  ExternalReference debug_hook =
406  ExternalReference::debug_hook_on_function_call_address(masm->isolate());
407  __ Move(ip, debug_hook);
408  __ LoadB(ip, MemOperand(ip));
409  __ CmpSmiLiteral(ip, Smi::zero(), r0);
410  __ bne(&prepare_step_in_if_stepping);
411 
412  // Flood function if we need to continue stepping in the suspended generator.
413 
414  ExternalReference debug_suspended_generator =
415  ExternalReference::debug_suspended_generator_address(masm->isolate());
416 
417  __ Move(ip, debug_suspended_generator);
418  __ LoadP(ip, MemOperand(ip));
419  __ CmpP(ip, r3);
420  __ beq(&prepare_step_in_suspended_generator);
421  __ bind(&stepping_prepared);
422 
423  // Check the stack for overflow. We are not trying to catch interruptions
424  // (i.e. debug break and preemption) here, so check the "real stack limit".
425  Label stack_overflow;
426  __ CompareRoot(sp, RootIndex::kRealStackLimit);
427  __ blt(&stack_overflow);
428 
429  // Push receiver.
430  __ LoadP(ip, FieldMemOperand(r3, JSGeneratorObject::kReceiverOffset));
431  __ Push(ip);
432 
433  // ----------- S t a t e -------------
434  // -- r3 : the JSGeneratorObject to resume
435  // -- r6 : generator function
436  // -- cp : generator context
437  // -- lr : return address
438  // -- sp[0] : generator receiver
439  // -----------------------------------
440 
441  // Copy the function arguments from the generator object's register file.
442  __ LoadP(r5, FieldMemOperand(r6, JSFunction::kSharedFunctionInfoOffset));
443  __ LoadLogicalHalfWordP(
444  r5, FieldMemOperand(r5, SharedFunctionInfo::kFormalParameterCountOffset));
445  __ LoadP(r4, FieldMemOperand(
446  r3, JSGeneratorObject::kParametersAndRegistersOffset));
447  {
448  Label loop, done_loop;
449  __ ShiftLeftP(r5, r5, Operand(kPointerSizeLog2));
450  __ SubP(sp, r5);
451 
452  // ip = stack offset
453  // r5 = parameter array offset
454  __ LoadImmP(ip, Operand::Zero());
455  __ SubP(r5, Operand(kPointerSize));
456  __ blt(&done_loop);
457 
458  __ lgfi(r1, Operand(-kPointerSize));
459 
460  __ bind(&loop);
461 
462  // parameter copy loop
463  __ LoadP(r0, FieldMemOperand(r4, r5, FixedArray::kHeaderSize));
464  __ StoreP(r0, MemOperand(sp, ip));
465 
466  // update offsets
467  __ lay(ip, MemOperand(ip, kPointerSize));
468 
469  __ BranchRelativeOnIdxHighP(r5, r1, &loop);
470 
471  __ bind(&done_loop);
472  }
473 
474  // Underlying function needs to have bytecode available.
475  if (FLAG_debug_code) {
476  __ LoadP(r5, FieldMemOperand(r6, JSFunction::kSharedFunctionInfoOffset));
477  __ LoadP(r5, FieldMemOperand(r5, SharedFunctionInfo::kFunctionDataOffset));
478  GetSharedFunctionInfoBytecode(masm, r5, ip);
479  __ CompareObjectType(r5, r5, r5, BYTECODE_ARRAY_TYPE);
480  __ Assert(eq, AbortReason::kMissingBytecodeArray);
481  }
482 
483  // Resume (Ignition/TurboFan) generator object.
484  {
485  // We abuse new.target both to indicate that this is a resume call and to
486  // pass in the generator object. In ordinary calls, new.target is always
487  // undefined because generator functions are non-constructable.
488  __ LoadRR(r5, r3);
489  __ LoadRR(r3, r6);
490  static_assert(kJavaScriptCallCodeStartRegister == r4, "ABI mismatch");
491  __ LoadP(r4, FieldMemOperand(r3, JSFunction::kCodeOffset));
492  __ AddP(r4, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
493  __ JumpToJSEntry(r4);
494  }
495 
496  __ bind(&prepare_step_in_if_stepping);
497  {
498  FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
499  __ Push(r3, r6);
500  // Push hole as receiver since we do not use it for stepping.
501  __ PushRoot(RootIndex::kTheHoleValue);
502  __ CallRuntime(Runtime::kDebugOnFunctionCall);
503  __ Pop(r3);
504  __ LoadP(r6, FieldMemOperand(r3, JSGeneratorObject::kFunctionOffset));
505  }
506  __ b(&stepping_prepared);
507 
508  __ bind(&prepare_step_in_suspended_generator);
509  {
510  FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
511  __ Push(r3);
512  __ CallRuntime(Runtime::kDebugPrepareStepInSuspendedGenerator);
513  __ Pop(r3);
514  __ LoadP(r6, FieldMemOperand(r3, JSGeneratorObject::kFunctionOffset));
515  }
516  __ b(&stepping_prepared);
517 
518  __ bind(&stack_overflow);
519  {
520  FrameScope scope(masm, StackFrame::INTERNAL);
521  __ CallRuntime(Runtime::kThrowStackOverflow);
522  __ bkpt(0); // This should be unreachable.
523  }
524 }
525 
526 void Builtins::Generate_ConstructedNonConstructable(MacroAssembler* masm) {
527  FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
528  __ push(r3);
529  __ CallRuntime(Runtime::kThrowConstructedNonConstructable);
530 }
531 
532 // Clobbers r4; preserves all other registers.
533 static void Generate_CheckStackOverflow(MacroAssembler* masm, Register argc) {
534  // Check the stack for overflow. We are not trying to catch
535  // interruptions (e.g. debug break and preemption) here, so the "real stack
536  // limit" is checked.
537  Label okay;
538  __ LoadRoot(r4, RootIndex::kRealStackLimit);
539  // Make r4 the space we have left. The stack might already be overflowed
540  // here which will cause r4 to become negative.
541  __ SubP(r4, sp, r4);
542  // Check if the arguments will overflow the stack.
543  __ ShiftLeftP(r0, argc, Operand(kPointerSizeLog2));
544  __ CmpP(r4, r0);
545  __ bgt(&okay); // Signed comparison.
546 
547  // Out of stack space.
548  __ CallRuntime(Runtime::kThrowStackOverflow);
549 
550  __ bind(&okay);
551 }
552 
553 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
554  bool is_construct) {
555  // Called from Generate_JS_Entry
556  // r2: new.target
557  // r3: function
558  // r4: receiver
559  // r5: argc
560  // r6: argv
561  // r0,r7-r9, cp may be clobbered
562 
563  // Enter an internal frame.
564  {
565  // FrameScope ends up calling MacroAssembler::EnterFrame here
566  FrameScope scope(masm, StackFrame::INTERNAL);
567 
568  // Setup the context (we need to use the caller context from the isolate).
569  ExternalReference context_address = ExternalReference::Create(
570  IsolateAddressId::kContextAddress, masm->isolate());
571  __ Move(cp, context_address);
572  __ LoadP(cp, MemOperand(cp));
573 
574  // Push the function and the receiver onto the stack.
575  __ Push(r3, r4);
576 
577  // Check if we have enough stack space to push all arguments.
578  // Clobbers r4.
579  Generate_CheckStackOverflow(masm, r5);
580 
581  // Copy arguments to the stack in a loop from argv to sp.
582  // The arguments are actually placed in reverse order on sp
583  // compared to argv (i.e. arg1 is highest memory in sp).
584  // r3: function
585  // r5: argc
586  // r6: argv, i.e. points to first arg
587  // r7: scratch reg to hold scaled argc
588  // r8: scratch reg to hold arg handle
589  // r9: scratch reg to hold index into argv
590  Label argLoop, argExit;
591  intptr_t zero = 0;
592  __ ShiftLeftP(r7, r5, Operand(kPointerSizeLog2));
593  __ SubRR(sp, r7); // Buy the stack frame to fit args
594  __ LoadImmP(r9, Operand(zero)); // Initialize argv index
595  __ bind(&argLoop);
596  __ CmpPH(r7, Operand(zero));
597  __ beq(&argExit, Label::kNear);
598  __ lay(r7, MemOperand(r7, -kPointerSize));
599  __ LoadP(r8, MemOperand(r9, r6)); // read next parameter
600  __ la(r9, MemOperand(r9, kPointerSize)); // r9++;
601  __ LoadP(r0, MemOperand(r8)); // dereference handle
602  __ StoreP(r0, MemOperand(r7, sp)); // push parameter
603  __ b(&argLoop);
604  __ bind(&argExit);
605 
606  // Setup new.target and argc.
607  __ LoadRR(r6, r2);
608  __ LoadRR(r2, r5);
609  __ LoadRR(r5, r6);
610 
611  // Initialize all JavaScript callee-saved registers, since they will be seen
612  // by the garbage collector as part of handlers.
613  __ LoadRoot(r6, RootIndex::kUndefinedValue);
614  __ LoadRR(r7, r6);
615  __ LoadRR(r8, r6);
616  __ LoadRR(r9, r6);
617 
618  // Invoke the code.
619  Handle<Code> builtin = is_construct
620  ? BUILTIN_CODE(masm->isolate(), Construct)
621  : masm->isolate()->builtins()->Call();
622  __ Call(builtin, RelocInfo::CODE_TARGET);
623 
624  // Exit the JS frame and remove the parameters (except function), and
625  // return.
626  }
627  __ b(r14);
628 
629  // r2: result
630 }
631 
632 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
633  Generate_JSEntryTrampolineHelper(masm, false);
634 }
635 
636 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
637  Generate_JSEntryTrampolineHelper(masm, true);
638 }
639 
640 static void ReplaceClosureCodeWithOptimizedCode(
641  MacroAssembler* masm, Register optimized_code, Register closure,
642  Register scratch1, Register scratch2, Register scratch3) {
643  // Store code entry in the closure.
644  __ StoreP(optimized_code, FieldMemOperand(closure, JSFunction::kCodeOffset),
645  r0);
646  __ LoadRR(scratch1,
647  optimized_code); // Write barrier clobbers scratch1 below.
648  __ RecordWriteField(closure, JSFunction::kCodeOffset, scratch1, scratch2,
649  kLRHasNotBeenSaved, kDontSaveFPRegs, OMIT_REMEMBERED_SET,
650  OMIT_SMI_CHECK);
651 }
652 
653 static void LeaveInterpreterFrame(MacroAssembler* masm, Register scratch) {
654  Register args_count = scratch;
655 
656  // Get the arguments + receiver count.
657  __ LoadP(args_count,
658  MemOperand(fp, InterpreterFrameConstants::kBytecodeArrayFromFp));
659  __ LoadlW(args_count,
660  FieldMemOperand(args_count, BytecodeArray::kParameterSizeOffset));
661 
662  // Leave the frame (also dropping the register file).
663  __ LeaveFrame(StackFrame::INTERPRETED);
664 
665  __ AddP(sp, sp, args_count);
666 }
667 
668 // Tail-call |function_id| if |smi_entry| == |marker|
669 static void TailCallRuntimeIfMarkerEquals(MacroAssembler* masm,
670  Register smi_entry,
671  OptimizationMarker marker,
672  Runtime::FunctionId function_id) {
673  Label no_match;
674  __ CmpSmiLiteral(smi_entry, Smi::FromEnum(marker), r0);
675  __ bne(&no_match);
676  GenerateTailCallToReturnedCode(masm, function_id);
677  __ bind(&no_match);
678 }
679 
680 static void MaybeTailCallOptimizedCodeSlot(MacroAssembler* masm,
681  Register feedback_vector,
682  Register scratch1, Register scratch2,
683  Register scratch3) {
684  // ----------- S t a t e -------------
685  // -- r0 : argument count (preserved for callee if needed, and caller)
686  // -- r3 : new target (preserved for callee if needed, and caller)
687  // -- r1 : target function (preserved for callee if needed, and caller)
688  // -- feedback vector (preserved for caller if needed)
689  // -----------------------------------
690  DCHECK(
691  !AreAliased(feedback_vector, r2, r3, r5, scratch1, scratch2, scratch3));
692 
693  Label optimized_code_slot_is_weak_ref, fallthrough;
694 
695  Register closure = r3;
696  Register optimized_code_entry = scratch1;
697 
698  __ LoadP(
699  optimized_code_entry,
700  FieldMemOperand(feedback_vector, FeedbackVector::kOptimizedCodeOffset));
701 
702  // Check if the code entry is a Smi. If yes, we interpret it as an
703  // optimisation marker. Otherwise, interpret it as a weak reference to a code
704  // object.
705  __ JumpIfNotSmi(optimized_code_entry, &optimized_code_slot_is_weak_ref);
706 
707  {
708  // Optimized code slot is a Smi optimization marker.
709 
710  // Fall through if no optimization trigger.
711  __ CmpSmiLiteral(optimized_code_entry,
712  Smi::FromEnum(OptimizationMarker::kNone), r0);
713  __ beq(&fallthrough);
714 
715  TailCallRuntimeIfMarkerEquals(masm, optimized_code_entry,
716  OptimizationMarker::kLogFirstExecution,
717  Runtime::kFunctionFirstExecution);
718  TailCallRuntimeIfMarkerEquals(masm, optimized_code_entry,
719  OptimizationMarker::kCompileOptimized,
720  Runtime::kCompileOptimized_NotConcurrent);
721  TailCallRuntimeIfMarkerEquals(
722  masm, optimized_code_entry,
723  OptimizationMarker::kCompileOptimizedConcurrent,
724  Runtime::kCompileOptimized_Concurrent);
725 
726  {
727  // Otherwise, the marker is InOptimizationQueue, so fall through hoping
728  // that an interrupt will eventually update the slot with optimized code.
729  if (FLAG_debug_code) {
730  __ CmpSmiLiteral(
731  optimized_code_entry,
732  Smi::FromEnum(OptimizationMarker::kInOptimizationQueue), r0);
733  __ Assert(eq, AbortReason::kExpectedOptimizationSentinel);
734  }
735  __ b(&fallthrough, Label::kNear);
736  }
737  }
738 
739  {
740  // Optimized code slot is a weak reference.
741  __ bind(&optimized_code_slot_is_weak_ref);
742 
743  __ LoadWeakValue(optimized_code_entry, optimized_code_entry, &fallthrough);
744 
745  // Check if the optimized code is marked for deopt. If it is, call the
746  // runtime to clear it.
747  Label found_deoptimized_code;
748  __ LoadP(scratch2, FieldMemOperand(optimized_code_entry,
749  Code::kCodeDataContainerOffset));
750  __ LoadW(
751  scratch2,
752  FieldMemOperand(scratch2, CodeDataContainer::kKindSpecificFlagsOffset));
753  __ TestBit(scratch2, Code::kMarkedForDeoptimizationBit, r0);
754  __ bne(&found_deoptimized_code);
755 
756  // Optimized code is good, get it into the closure and link the closure into
757  // the optimized functions list, then tail call the optimized code.
758  // The feedback vector is no longer used, so re-use it as a scratch
759  // register.
760  ReplaceClosureCodeWithOptimizedCode(masm, optimized_code_entry, closure,
761  scratch2, scratch3, feedback_vector);
762  static_assert(kJavaScriptCallCodeStartRegister == r4, "ABI mismatch");
763  __ AddP(r4, optimized_code_entry,
764  Operand(Code::kHeaderSize - kHeapObjectTag));
765  __ Jump(r4);
766 
767  // Optimized code slot contains deoptimized code, evict it and re-enter the
768  // closure's code.
769  __ bind(&found_deoptimized_code);
770  GenerateTailCallToReturnedCode(masm, Runtime::kEvictOptimizedCodeSlot);
771  }
772 
773  // Fall-through if the optimized code cell is clear and there is no
774  // optimization marker.
775  __ bind(&fallthrough);
776 }
777 
778 // Advance the current bytecode offset. This simulates what all bytecode
779 // handlers do upon completion of the underlying operation. Will bail out to a
780 // label if the bytecode (without prefix) is a return bytecode.
781 static void AdvanceBytecodeOffsetOrReturn(MacroAssembler* masm,
782  Register bytecode_array,
783  Register bytecode_offset,
784  Register bytecode, Register scratch1,
785  Label* if_return) {
786  Register bytecode_size_table = scratch1;
787  Register scratch2 = bytecode;
788  DCHECK(!AreAliased(bytecode_array, bytecode_offset, bytecode_size_table,
789  bytecode));
790  __ Move(bytecode_size_table,
791  ExternalReference::bytecode_size_table_address());
792 
793  // Check if the bytecode is a Wide or ExtraWide prefix bytecode.
794  Label process_bytecode, extra_wide;
795  STATIC_ASSERT(0 == static_cast<int>(interpreter::Bytecode::kWide));
796  STATIC_ASSERT(1 == static_cast<int>(interpreter::Bytecode::kExtraWide));
797  STATIC_ASSERT(2 == static_cast<int>(interpreter::Bytecode::kDebugBreakWide));
798  STATIC_ASSERT(3 ==
799  static_cast<int>(interpreter::Bytecode::kDebugBreakExtraWide));
800  __ CmpP(bytecode, Operand(0x3));
801  __ bgt(&process_bytecode);
802  __ tmll(bytecode, Operand(0x1));
803  __ bne(&extra_wide);
804 
805  // Load the next bytecode and update table to the wide scaled table.
806  __ AddP(bytecode_offset, bytecode_offset, Operand(1));
807  __ LoadlB(bytecode, MemOperand(bytecode_array, bytecode_offset));
808  __ AddP(bytecode_size_table, bytecode_size_table,
809  Operand(kIntSize * interpreter::Bytecodes::kBytecodeCount));
810  __ b(&process_bytecode);
811 
812  __ bind(&extra_wide);
813  // Load the next bytecode and update table to the extra wide scaled table.
814  __ AddP(bytecode_offset, bytecode_offset, Operand(1));
815  __ LoadlB(bytecode, MemOperand(bytecode_array, bytecode_offset));
816  __ AddP(bytecode_size_table, bytecode_size_table,
817  Operand(2 * kIntSize * interpreter::Bytecodes::kBytecodeCount));
818 
819  // Load the size of the current bytecode.
820  __ bind(&process_bytecode);
821 
822 // Bailout to the return label if this is a return bytecode.
823 #define JUMP_IF_EQUAL(NAME) \
824  __ CmpP(bytecode, \
825  Operand(static_cast<int>(interpreter::Bytecode::k##NAME))); \
826  __ beq(if_return);
827  RETURN_BYTECODE_LIST(JUMP_IF_EQUAL)
828 #undef JUMP_IF_EQUAL
829 
830  // Otherwise, load the size of the current bytecode and advance the offset.
831  __ ShiftLeftP(scratch2, bytecode, Operand(2));
832  __ LoadlW(scratch2, MemOperand(bytecode_size_table, scratch2));
833  __ AddP(bytecode_offset, bytecode_offset, scratch2);
834 }
835 
836 // Generate code for entering a JS function with the interpreter.
837 // On entry to the function the receiver and arguments have been pushed on the
838 // stack left to right. The actual argument count matches the formal parameter
839 // count expected by the function.
840 //
841 // The live registers are:
842 // o r3: the JS function object being called.
843 // o r5: the incoming new target or generator object
844 // o cp: our context
845 // o pp: the caller's constant pool pointer (if enabled)
846 // o fp: the caller's frame pointer
847 // o sp: stack pointer
848 // o lr: return address
849 //
850 // The function builds an interpreter frame. See InterpreterFrameConstants in
851 // frames.h for its layout.
852 void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
853  Register closure = r3;
854  Register feedback_vector = r4;
855 
856  // Load the feedback vector from the closure.
857  __ LoadP(feedback_vector,
858  FieldMemOperand(closure, JSFunction::kFeedbackCellOffset));
859  __ LoadP(feedback_vector,
860  FieldMemOperand(feedback_vector, Cell::kValueOffset));
861  // Read off the optimized code slot in the feedback vector, and if there
862  // is optimized code or an optimization marker, call that instead.
863  MaybeTailCallOptimizedCodeSlot(masm, feedback_vector, r6, r8, r7);
864 
865  // Open a frame scope to indicate that there is a frame on the stack. The
866  // MANUAL indicates that the scope shouldn't actually generate code to set up
867  // the frame (that is done below).
868  FrameScope frame_scope(masm, StackFrame::MANUAL);
869  __ PushStandardFrame(closure);
870 
871  // Get the bytecode array from the function object and load it into
872  // kInterpreterBytecodeArrayRegister.
873  __ LoadP(r2, FieldMemOperand(closure, JSFunction::kSharedFunctionInfoOffset));
874  // Load original bytecode array or the debug copy.
875  __ LoadP(kInterpreterBytecodeArrayRegister,
876  FieldMemOperand(r2, SharedFunctionInfo::kFunctionDataOffset));
877  GetSharedFunctionInfoBytecode(masm, kInterpreterBytecodeArrayRegister, r6);
878 
879  // Increment invocation count for the function.
880  __ LoadW(r1, FieldMemOperand(feedback_vector,
881  FeedbackVector::kInvocationCountOffset));
882  __ AddP(r1, r1, Operand(1));
883  __ StoreW(r1, FieldMemOperand(feedback_vector,
884  FeedbackVector::kInvocationCountOffset));
885 
886  // Check function data field is actually a BytecodeArray object.
887  if (FLAG_debug_code) {
888  __ TestIfSmi(kInterpreterBytecodeArrayRegister);
889  __ Assert(
890  ne, AbortReason::kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
891  __ CompareObjectType(kInterpreterBytecodeArrayRegister, r2, no_reg,
892  BYTECODE_ARRAY_TYPE);
893  __ Assert(
894  eq, AbortReason::kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
895  }
896 
897  // Reset code age.
898  __ mov(r1, Operand(BytecodeArray::kNoAgeBytecodeAge));
899  __ StoreByte(r1, FieldMemOperand(kInterpreterBytecodeArrayRegister,
900  BytecodeArray::kBytecodeAgeOffset),
901  r0);
902 
903  // Load the initial bytecode offset.
904  __ mov(kInterpreterBytecodeOffsetRegister,
905  Operand(BytecodeArray::kHeaderSize - kHeapObjectTag));
906 
907  // Push bytecode array and Smi tagged bytecode array offset.
908  __ SmiTag(r4, kInterpreterBytecodeOffsetRegister);
909  __ Push(kInterpreterBytecodeArrayRegister, r4);
910 
911  // Allocate the local and temporary register file on the stack.
912  {
913  // Load frame size (word) from the BytecodeArray object.
914  __ LoadlW(r4, FieldMemOperand(kInterpreterBytecodeArrayRegister,
915  BytecodeArray::kFrameSizeOffset));
916 
917  // Do a stack check to ensure we don't go over the limit.
918  Label ok;
919  __ SubP(r8, sp, r4);
920  __ LoadRoot(r0, RootIndex::kRealStackLimit);
921  __ CmpLogicalP(r8, r0);
922  __ bge(&ok);
923  __ CallRuntime(Runtime::kThrowStackOverflow);
924  __ bind(&ok);
925 
926  // If ok, push undefined as the initial value for all register file entries.
927  // TODO(rmcilroy): Consider doing more than one push per loop iteration.
928  Label loop, no_args;
929  __ LoadRoot(r8, RootIndex::kUndefinedValue);
930  __ ShiftRightP(r4, r4, Operand(kPointerSizeLog2));
931  __ LoadAndTestP(r4, r4);
932  __ beq(&no_args);
933  __ LoadRR(r1, r4);
934  __ bind(&loop);
935  __ push(r8);
936  __ SubP(r1, Operand(1));
937  __ bne(&loop);
938  __ bind(&no_args);
939  }
940 
941  // If the bytecode array has a valid incoming new target or generator object
942  // register, initialize it with incoming value which was passed in r6.
943  Label no_incoming_new_target_or_generator_register;
944  __ LoadW(r8, FieldMemOperand(
945  kInterpreterBytecodeArrayRegister,
946  BytecodeArray::kIncomingNewTargetOrGeneratorRegisterOffset));
947  __ CmpP(r8, Operand::Zero());
948  __ beq(&no_incoming_new_target_or_generator_register);
949  __ ShiftLeftP(r8, r8, Operand(kPointerSizeLog2));
950  __ StoreP(r5, MemOperand(fp, r8));
951  __ bind(&no_incoming_new_target_or_generator_register);
952 
953  // Load accumulator with undefined.
954  __ LoadRoot(kInterpreterAccumulatorRegister, RootIndex::kUndefinedValue);
955  // Load the dispatch table into a register and dispatch to the bytecode
956  // handler at the current bytecode offset.
957  Label do_dispatch;
958  __ bind(&do_dispatch);
959  __ Move(
960  kInterpreterDispatchTableRegister,
961  ExternalReference::interpreter_dispatch_table_address(masm->isolate()));
962 
963  __ LoadlB(r5, MemOperand(kInterpreterBytecodeArrayRegister,
964  kInterpreterBytecodeOffsetRegister));
965  __ ShiftLeftP(r5, r5, Operand(kPointerSizeLog2));
966  __ LoadP(kJavaScriptCallCodeStartRegister,
967  MemOperand(kInterpreterDispatchTableRegister, r5));
968  __ Call(kJavaScriptCallCodeStartRegister);
969 
970  masm->isolate()->heap()->SetInterpreterEntryReturnPCOffset(masm->pc_offset());
971 
972  // Any returns to the entry trampoline are either due to the return bytecode
973  // or the interpreter tail calling a builtin and then a dispatch.
974 
975  // Get bytecode array and bytecode offset from the stack frame.
976  __ LoadP(kInterpreterBytecodeArrayRegister,
977  MemOperand(fp, InterpreterFrameConstants::kBytecodeArrayFromFp));
978  __ LoadP(kInterpreterBytecodeOffsetRegister,
979  MemOperand(fp, InterpreterFrameConstants::kBytecodeOffsetFromFp));
980  __ SmiUntag(kInterpreterBytecodeOffsetRegister);
981 
982  // Either return, or advance to the next bytecode and dispatch.
983  Label do_return;
984  __ LoadlB(r3, MemOperand(kInterpreterBytecodeArrayRegister,
985  kInterpreterBytecodeOffsetRegister));
986  AdvanceBytecodeOffsetOrReturn(masm, kInterpreterBytecodeArrayRegister,
987  kInterpreterBytecodeOffsetRegister, r3, r4,
988  &do_return);
989  __ b(&do_dispatch);
990 
991  __ bind(&do_return);
992  // The return value is in r2.
993  LeaveInterpreterFrame(masm, r4);
994  __ Ret();
995 }
996 
997 static void Generate_InterpreterPushArgs(MacroAssembler* masm,
998  Register num_args, Register index,
999  Register count, Register scratch) {
1000  Label loop, skip;
1001  __ CmpP(count, Operand::Zero());
1002  __ beq(&skip);
1003  __ AddP(index, index, Operand(kPointerSize)); // Bias up for LoadPU
1004  __ LoadRR(r0, count);
1005  __ bind(&loop);
1006  __ LoadP(scratch, MemOperand(index, -kPointerSize));
1007  __ lay(index, MemOperand(index, -kPointerSize));
1008  __ push(scratch);
1009  __ SubP(r0, Operand(1));
1010  __ bne(&loop);
1011  __ bind(&skip);
1012 }
1013 
1014 // static
1015 void Builtins::Generate_InterpreterPushArgsThenCallImpl(
1016  MacroAssembler* masm, ConvertReceiverMode receiver_mode,
1017  InterpreterPushArgsMode mode) {
1018  DCHECK(mode != InterpreterPushArgsMode::kArrayFunction);
1019  // ----------- S t a t e -------------
1020  // -- r2 : the number of arguments (not including the receiver)
1021  // -- r4 : the address of the first argument to be pushed. Subsequent
1022  // arguments should be consecutive above this, in the same order as
1023  // they are to be pushed onto the stack.
1024  // -- r3 : the target to call (can be any Object).
1025  // -----------------------------------
1026  Label stack_overflow;
1027 
1028  // Calculate number of arguments (AddP one for receiver).
1029  __ AddP(r5, r2, Operand(1));
1030  Generate_StackOverflowCheck(masm, r5, ip, &stack_overflow);
1031 
1032  // Push "undefined" as the receiver arg if we need to.
1033  if (receiver_mode == ConvertReceiverMode::kNullOrUndefined) {
1034  __ PushRoot(RootIndex::kUndefinedValue);
1035  __ LoadRR(r5, r2); // Argument count is correct.
1036  }
1037 
1038  // Push the arguments.
1039  Generate_InterpreterPushArgs(masm, r5, r4, r5, r6);
1040  if (mode == InterpreterPushArgsMode::kWithFinalSpread) {
1041  __ Pop(r4); // Pass the spread in a register
1042  __ SubP(r2, r2, Operand(1)); // Subtract one for spread
1043  }
1044 
1045  // Call the target.
1046  if (mode == InterpreterPushArgsMode::kWithFinalSpread) {
1047  __ Jump(BUILTIN_CODE(masm->isolate(), CallWithSpread),
1048  RelocInfo::CODE_TARGET);
1049  } else {
1050  __ Jump(masm->isolate()->builtins()->Call(ConvertReceiverMode::kAny),
1051  RelocInfo::CODE_TARGET);
1052  }
1053 
1054  __ bind(&stack_overflow);
1055  {
1056  __ TailCallRuntime(Runtime::kThrowStackOverflow);
1057  // Unreachable Code.
1058  __ bkpt(0);
1059  }
1060 }
1061 
1062 // static
1063 void Builtins::Generate_InterpreterPushArgsThenConstructImpl(
1064  MacroAssembler* masm, InterpreterPushArgsMode mode) {
1065  // ----------- S t a t e -------------
1066  // -- r2 : argument count (not including receiver)
1067  // -- r5 : new target
1068  // -- r3 : constructor to call
1069  // -- r4 : allocation site feedback if available, undefined otherwise.
1070  // -- r6 : address of the first argument
1071  // -----------------------------------
1072  Label stack_overflow;
1073 
1074  // Push a slot for the receiver to be constructed.
1075  __ LoadImmP(r0, Operand::Zero());
1076  __ push(r0);
1077 
1078  // Push the arguments (skip if none).
1079  Label skip;
1080  __ CmpP(r2, Operand::Zero());
1081  __ beq(&skip);
1082  Generate_StackOverflowCheck(masm, r2, ip, &stack_overflow);
1083  Generate_InterpreterPushArgs(masm, r2, r6, r2, r7);
1084  __ bind(&skip);
1085 
1086  if (mode == InterpreterPushArgsMode::kWithFinalSpread) {
1087  __ Pop(r4); // Pass the spread in a register
1088  __ SubP(r2, r2, Operand(1)); // Subtract one for spread
1089  } else {
1090  __ AssertUndefinedOrAllocationSite(r4, r7);
1091  }
1092  if (mode == InterpreterPushArgsMode::kArrayFunction) {
1093  __ AssertFunction(r3);
1094 
1095  // Tail call to the array construct stub (still in the caller
1096  // context at this point).
1097  Handle<Code> code = BUILTIN_CODE(masm->isolate(), ArrayConstructorImpl);
1098  __ Jump(code, RelocInfo::CODE_TARGET);
1099  } else if (mode == InterpreterPushArgsMode::kWithFinalSpread) {
1100  // Call the constructor with r2, r3, and r5 unmodified.
1101  __ Jump(BUILTIN_CODE(masm->isolate(), ConstructWithSpread),
1102  RelocInfo::CODE_TARGET);
1103  } else {
1104  DCHECK_EQ(InterpreterPushArgsMode::kOther, mode);
1105  // Call the constructor with r2, r3, and r5 unmodified.
1106  __ Jump(BUILTIN_CODE(masm->isolate(), Construct), RelocInfo::CODE_TARGET);
1107  }
1108 
1109  __ bind(&stack_overflow);
1110  {
1111  __ TailCallRuntime(Runtime::kThrowStackOverflow);
1112  // Unreachable Code.
1113  __ bkpt(0);
1114  }
1115 }
1116 
1117 static void Generate_InterpreterEnterBytecode(MacroAssembler* masm) {
1118  // Set the return address to the correct point in the interpreter entry
1119  // trampoline.
1120  Label builtin_trampoline, trampoline_loaded;
1121  Smi interpreter_entry_return_pc_offset(
1122  masm->isolate()->heap()->interpreter_entry_return_pc_offset());
1123  DCHECK_NE(interpreter_entry_return_pc_offset, Smi::zero());
1124 
1125  // If the SFI function_data is an InterpreterData, the function will have a
1126  // custom copy of the interpreter entry trampoline for profiling. If so,
1127  // get the custom trampoline, otherwise grab the entry address of the global
1128  // trampoline.
1129  __ LoadP(r4, MemOperand(fp, StandardFrameConstants::kFunctionOffset));
1130  __ LoadP(r4, FieldMemOperand(r4, JSFunction::kSharedFunctionInfoOffset));
1131  __ LoadP(r4, FieldMemOperand(r4, SharedFunctionInfo::kFunctionDataOffset));
1132  __ CompareObjectType(r4, kInterpreterDispatchTableRegister,
1133  kInterpreterDispatchTableRegister,
1134  INTERPRETER_DATA_TYPE);
1135  __ bne(&builtin_trampoline);
1136 
1137  __ LoadP(r4,
1138  FieldMemOperand(r4, InterpreterData::kInterpreterTrampolineOffset));
1139  __ AddP(r4, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
1140  __ b(&trampoline_loaded);
1141 
1142  __ bind(&builtin_trampoline);
1143  __ Move(r4, ExternalReference::
1144  address_of_interpreter_entry_trampoline_instruction_start(
1145  masm->isolate()));
1146  __ LoadP(r4, MemOperand(r4));
1147 
1148  __ bind(&trampoline_loaded);
1149  __ AddP(r14, r4, Operand(interpreter_entry_return_pc_offset->value()));
1150 
1151  // Initialize the dispatch table register.
1152  __ Move(
1153  kInterpreterDispatchTableRegister,
1154  ExternalReference::interpreter_dispatch_table_address(masm->isolate()));
1155 
1156  // Get the bytecode array pointer from the frame.
1157  __ LoadP(kInterpreterBytecodeArrayRegister,
1158  MemOperand(fp, InterpreterFrameConstants::kBytecodeArrayFromFp));
1159 
1160  if (FLAG_debug_code) {
1161  // Check function data field is actually a BytecodeArray object.
1162  __ TestIfSmi(kInterpreterBytecodeArrayRegister);
1163  __ Assert(
1164  ne, AbortReason::kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
1165  __ CompareObjectType(kInterpreterBytecodeArrayRegister, r3, no_reg,
1166  BYTECODE_ARRAY_TYPE);
1167  __ Assert(
1168  eq, AbortReason::kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
1169  }
1170 
1171  // Get the target bytecode offset from the frame.
1172  __ LoadP(kInterpreterBytecodeOffsetRegister,
1173  MemOperand(fp, InterpreterFrameConstants::kBytecodeOffsetFromFp));
1174  __ SmiUntag(kInterpreterBytecodeOffsetRegister);
1175 
1176  // Dispatch to the target bytecode.
1177  __ LoadlB(ip, MemOperand(kInterpreterBytecodeArrayRegister,
1178  kInterpreterBytecodeOffsetRegister));
1179  __ ShiftLeftP(ip, ip, Operand(kPointerSizeLog2));
1180  __ LoadP(kJavaScriptCallCodeStartRegister,
1181  MemOperand(kInterpreterDispatchTableRegister, ip));
1182  __ Jump(kJavaScriptCallCodeStartRegister);
1183 }
1184 
1185 void Builtins::Generate_InterpreterEnterBytecodeAdvance(MacroAssembler* masm) {
1186  // Get bytecode array and bytecode offset from the stack frame.
1187  __ LoadP(kInterpreterBytecodeArrayRegister,
1188  MemOperand(fp, InterpreterFrameConstants::kBytecodeArrayFromFp));
1189  __ LoadP(kInterpreterBytecodeOffsetRegister,
1190  MemOperand(fp, InterpreterFrameConstants::kBytecodeOffsetFromFp));
1191  __ SmiUntag(kInterpreterBytecodeOffsetRegister);
1192 
1193  // Load the current bytecode.
1194  __ LoadlB(r3, MemOperand(kInterpreterBytecodeArrayRegister,
1195  kInterpreterBytecodeOffsetRegister));
1196 
1197  // Advance to the next bytecode.
1198  Label if_return;
1199  AdvanceBytecodeOffsetOrReturn(masm, kInterpreterBytecodeArrayRegister,
1200  kInterpreterBytecodeOffsetRegister, r3, r4,
1201  &if_return);
1202 
1203  // Convert new bytecode offset to a Smi and save in the stackframe.
1204  __ SmiTag(r4, kInterpreterBytecodeOffsetRegister);
1205  __ StoreP(r4,
1206  MemOperand(fp, InterpreterFrameConstants::kBytecodeOffsetFromFp));
1207 
1208  Generate_InterpreterEnterBytecode(masm);
1209 
1210  // We should never take the if_return path.
1211  __ bind(&if_return);
1212  __ Abort(AbortReason::kInvalidBytecodeAdvance);
1213 }
1214 
1215 void Builtins::Generate_InterpreterEnterBytecodeDispatch(MacroAssembler* masm) {
1216  Generate_InterpreterEnterBytecode(masm);
1217 }
1218 
1219 void Builtins::Generate_InstantiateAsmJs(MacroAssembler* masm) {
1220  // ----------- S t a t e -------------
1221  // -- r2 : argument count (preserved for callee)
1222  // -- r3 : new target (preserved for callee)
1223  // -- r5 : target function (preserved for callee)
1224  // -----------------------------------
1225  Label failed;
1226  {
1227  FrameScope scope(masm, StackFrame::INTERNAL);
1228  // Preserve argument count for later compare.
1229  __ Move(r6, r2);
1230  // Push a copy of the target function and the new target.
1231  __ SmiTag(r2);
1232  // Push another copy as a parameter to the runtime call.
1233  __ Push(r2, r3, r5, r3);
1234 
1235  // Copy arguments from caller (stdlib, foreign, heap).
1236  Label args_done;
1237  for (int j = 0; j < 4; ++j) {
1238  Label over;
1239  if (j < 3) {
1240  __ CmpP(r6, Operand(j));
1241  __ b(ne, &over);
1242  }
1243  for (int i = j - 1; i >= 0; --i) {
1244  __ LoadP(r6, MemOperand(fp, StandardFrameConstants::kCallerSPOffset +
1245  i * kPointerSize));
1246  __ push(r6);
1247  }
1248  for (int i = 0; i < 3 - j; ++i) {
1249  __ PushRoot(RootIndex::kUndefinedValue);
1250  }
1251  if (j < 3) {
1252  __ jmp(&args_done);
1253  __ bind(&over);
1254  }
1255  }
1256  __ bind(&args_done);
1257 
1258  // Call runtime, on success unwind frame, and parent frame.
1259  __ CallRuntime(Runtime::kInstantiateAsmJs, 4);
1260  // A smi 0 is returned on failure, an object on success.
1261  __ JumpIfSmi(r2, &failed);
1262 
1263  __ Drop(2);
1264  __ pop(r6);
1265  __ SmiUntag(r6);
1266  scope.GenerateLeaveFrame();
1267 
1268  __ AddP(r6, r6, Operand(1));
1269  __ Drop(r6);
1270  __ Ret();
1271 
1272  __ bind(&failed);
1273  // Restore target function and new target.
1274  __ Pop(r2, r3, r5);
1275  __ SmiUntag(r2);
1276  }
1277  // On failure, tail call back to regular js by re-calling the function
1278  // which has be reset to the compile lazy builtin.
1279  static_assert(kJavaScriptCallCodeStartRegister == r4, "ABI mismatch");
1280  __ LoadP(r4, FieldMemOperand(r3, JSFunction::kCodeOffset));
1281  __ AddP(r4, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
1282  __ JumpToJSEntry(r4);
1283 }
1284 
1285 namespace {
1286 void Generate_ContinueToBuiltinHelper(MacroAssembler* masm,
1287  bool java_script_builtin,
1288  bool with_result) {
1289  const RegisterConfiguration* config(RegisterConfiguration::Default());
1290  int allocatable_register_count = config->num_allocatable_general_registers();
1291  if (with_result) {
1292  // Overwrite the hole inserted by the deoptimizer with the return value from
1293  // the LAZY deopt point.
1294  __ StoreP(
1295  r2, MemOperand(
1296  sp, config->num_allocatable_general_registers() * kPointerSize +
1297  BuiltinContinuationFrameConstants::kFixedFrameSize));
1298  }
1299  for (int i = allocatable_register_count - 1; i >= 0; --i) {
1300  int code = config->GetAllocatableGeneralCode(i);
1301  __ Pop(Register::from_code(code));
1302  if (java_script_builtin && code == kJavaScriptCallArgCountRegister.code()) {
1303  __ SmiUntag(Register::from_code(code));
1304  }
1305  }
1306  __ LoadP(
1307  fp,
1308  MemOperand(sp, BuiltinContinuationFrameConstants::kFixedFrameSizeFromFp));
1309  __ Pop(ip);
1310  __ AddP(sp, sp,
1311  Operand(BuiltinContinuationFrameConstants::kFixedFrameSizeFromFp));
1312  __ Pop(r0);
1313  __ LoadRR(r14, r0);
1314  __ AddP(ip, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
1315  __ Jump(ip);
1316 }
1317 } // namespace
1318 
1319 void Builtins::Generate_ContinueToCodeStubBuiltin(MacroAssembler* masm) {
1320  Generate_ContinueToBuiltinHelper(masm, false, false);
1321 }
1322 
1323 void Builtins::Generate_ContinueToCodeStubBuiltinWithResult(
1324  MacroAssembler* masm) {
1325  Generate_ContinueToBuiltinHelper(masm, false, true);
1326 }
1327 
1328 void Builtins::Generate_ContinueToJavaScriptBuiltin(MacroAssembler* masm) {
1329  Generate_ContinueToBuiltinHelper(masm, true, false);
1330 }
1331 
1332 void Builtins::Generate_ContinueToJavaScriptBuiltinWithResult(
1333  MacroAssembler* masm) {
1334  Generate_ContinueToBuiltinHelper(masm, true, true);
1335 }
1336 
1337 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
1338  {
1339  FrameScope scope(masm, StackFrame::INTERNAL);
1340  __ CallRuntime(Runtime::kNotifyDeoptimized);
1341  }
1342 
1343  DCHECK_EQ(kInterpreterAccumulatorRegister.code(), r2.code());
1344  __ pop(r2);
1345  __ Ret();
1346 }
1347 
1348 void Builtins::Generate_InterpreterOnStackReplacement(MacroAssembler* masm) {
1349  // Lookup the function in the JavaScript frame.
1350  __ LoadP(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1351  __ LoadP(r2, MemOperand(r2, JavaScriptFrameConstants::kFunctionOffset));
1352 
1353  {
1354  FrameScope scope(masm, StackFrame::INTERNAL);
1355  // Pass function as argument.
1356  __ push(r2);
1357  __ CallRuntime(Runtime::kCompileForOnStackReplacement);
1358  }
1359 
1360  // If the code object is null, just return to the caller.
1361  Label skip;
1362  __ CmpSmiLiteral(r2, Smi::zero(), r0);
1363  __ bne(&skip);
1364  __ Ret();
1365 
1366  __ bind(&skip);
1367 
1368  // Drop the handler frame that is be sitting on top of the actual
1369  // JavaScript frame. This is the case then OSR is triggered from bytecode.
1370  __ LeaveFrame(StackFrame::STUB);
1371 
1372  // Load deoptimization data from the code object.
1373  // <deopt_data> = <code>[#deoptimization_data_offset]
1374  __ LoadP(r3, FieldMemOperand(r2, Code::kDeoptimizationDataOffset));
1375 
1376  // Load the OSR entrypoint offset from the deoptimization data.
1377  // <osr_offset> = <deopt_data>[#header_size + #osr_pc_offset]
1378  __ LoadP(r3, FieldMemOperand(r3, FixedArray::OffsetOfElementAt(
1379  DeoptimizationData::kOsrPcOffsetIndex)));
1380  __ SmiUntag(r3);
1381 
1382  // Compute the target address = code_obj + header_size + osr_offset
1383  // <entry_addr> = <code_obj> + #header_size + <osr_offset>
1384  __ AddP(r2, r3);
1385  __ AddP(r0, r2, Operand(Code::kHeaderSize - kHeapObjectTag));
1386  __ LoadRR(r14, r0);
1387 
1388  // And "return" to the OSR entry point of the function.
1389  __ Ret();
1390 }
1391 
1392 // static
1393 void Builtins::Generate_FunctionPrototypeApply(MacroAssembler* masm) {
1394  // ----------- S t a t e -------------
1395  // -- r2 : argc
1396  // -- sp[0] : argArray
1397  // -- sp[4] : thisArg
1398  // -- sp[8] : receiver
1399  // -----------------------------------
1400 
1401  // 1. Load receiver into r3, argArray into r4 (if present), remove all
1402  // arguments from the stack (including the receiver), and push thisArg (if
1403  // present) instead.
1404  {
1405  Label skip;
1406  Register arg_size = r7;
1407  Register new_sp = r5;
1408  Register scratch = r6;
1409  __ ShiftLeftP(arg_size, r2, Operand(kPointerSizeLog2));
1410  __ AddP(new_sp, sp, arg_size);
1411  __ LoadRoot(scratch, RootIndex::kUndefinedValue);
1412  __ LoadRR(r4, scratch);
1413  __ LoadP(r3, MemOperand(new_sp, 0)); // receiver
1414  __ CmpP(arg_size, Operand(kPointerSize));
1415  __ blt(&skip);
1416  __ LoadP(scratch, MemOperand(new_sp, 1 * -kPointerSize)); // thisArg
1417  __ beq(&skip);
1418  __ LoadP(r4, MemOperand(new_sp, 2 * -kPointerSize)); // argArray
1419  __ bind(&skip);
1420  __ LoadRR(sp, new_sp);
1421  __ StoreP(scratch, MemOperand(sp, 0));
1422  }
1423 
1424  // ----------- S t a t e -------------
1425  // -- r4 : argArray
1426  // -- r3 : receiver
1427  // -- sp[0] : thisArg
1428  // -----------------------------------
1429 
1430  // 2. We don't need to check explicitly for callable receiver here,
1431  // since that's the first thing the Call/CallWithArrayLike builtins
1432  // will do.
1433 
1434  // 3. Tail call with no arguments if argArray is null or undefined.
1435  Label no_arguments;
1436  __ JumpIfRoot(r4, RootIndex::kNullValue, &no_arguments);
1437  __ JumpIfRoot(r4, RootIndex::kUndefinedValue, &no_arguments);
1438 
1439  // 4a. Apply the receiver to the given argArray.
1440  __ Jump(BUILTIN_CODE(masm->isolate(), CallWithArrayLike),
1441  RelocInfo::CODE_TARGET);
1442 
1443  // 4b. The argArray is either null or undefined, so we tail call without any
1444  // arguments to the receiver.
1445  __ bind(&no_arguments);
1446  {
1447  __ LoadImmP(r2, Operand::Zero());
1448  __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1449  }
1450 }
1451 
1452 // static
1453 void Builtins::Generate_FunctionPrototypeCall(MacroAssembler* masm) {
1454  // 1. Make sure we have at least one argument.
1455  // r2: actual number of arguments
1456  {
1457  Label done;
1458  __ CmpP(r2, Operand::Zero());
1459  __ bne(&done, Label::kNear);
1460  __ PushRoot(RootIndex::kUndefinedValue);
1461  __ AddP(r2, Operand(1));
1462  __ bind(&done);
1463  }
1464 
1465  // r2: actual number of arguments
1466  // 2. Get the callable to call (passed as receiver) from the stack.
1467  __ ShiftLeftP(r4, r2, Operand(kPointerSizeLog2));
1468  __ LoadP(r3, MemOperand(sp, r4));
1469 
1470  // 3. Shift arguments and return address one slot down on the stack
1471  // (overwriting the original receiver). Adjust argument count to make
1472  // the original first argument the new receiver.
1473  // r2: actual number of arguments
1474  // r3: callable
1475  {
1476  Label loop;
1477  // Calculate the copy start address (destination). Copy end address is sp.
1478  __ AddP(r4, sp, r4);
1479 
1480  __ bind(&loop);
1481  __ LoadP(ip, MemOperand(r4, -kPointerSize));
1482  __ StoreP(ip, MemOperand(r4));
1483  __ SubP(r4, Operand(kPointerSize));
1484  __ CmpP(r4, sp);
1485  __ bne(&loop);
1486  // Adjust the actual number of arguments and remove the top element
1487  // (which is a copy of the last argument).
1488  __ SubP(r2, Operand(1));
1489  __ pop();
1490  }
1491 
1492  // 4. Call the callable.
1493  __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1494 }
1495 
1496 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1497  // ----------- S t a t e -------------
1498  // -- r2 : argc
1499  // -- sp[0] : argumentsList
1500  // -- sp[4] : thisArgument
1501  // -- sp[8] : target
1502  // -- sp[12] : receiver
1503  // -----------------------------------
1504 
1505  // 1. Load target into r3 (if present), argumentsList into r4 (if present),
1506  // remove all arguments from the stack (including the receiver), and push
1507  // thisArgument (if present) instead.
1508  {
1509  Label skip;
1510  Register arg_size = r7;
1511  Register new_sp = r5;
1512  Register scratch = r6;
1513  __ ShiftLeftP(arg_size, r2, Operand(kPointerSizeLog2));
1514  __ AddP(new_sp, sp, arg_size);
1515  __ LoadRoot(r3, RootIndex::kUndefinedValue);
1516  __ LoadRR(scratch, r3);
1517  __ LoadRR(r4, r3);
1518  __ CmpP(arg_size, Operand(kPointerSize));
1519  __ blt(&skip);
1520  __ LoadP(r3, MemOperand(new_sp, 1 * -kPointerSize)); // target
1521  __ beq(&skip);
1522  __ LoadP(scratch, MemOperand(new_sp, 2 * -kPointerSize)); // thisArgument
1523  __ CmpP(arg_size, Operand(2 * kPointerSize));
1524  __ beq(&skip);
1525  __ LoadP(r4, MemOperand(new_sp, 3 * -kPointerSize)); // argumentsList
1526  __ bind(&skip);
1527  __ LoadRR(sp, new_sp);
1528  __ StoreP(scratch, MemOperand(sp, 0));
1529  }
1530 
1531  // ----------- S t a t e -------------
1532  // -- r4 : argumentsList
1533  // -- r3 : target
1534  // -- sp[0] : thisArgument
1535  // -----------------------------------
1536 
1537  // 2. We don't need to check explicitly for callable target here,
1538  // since that's the first thing the Call/CallWithArrayLike builtins
1539  // will do.
1540 
1541  // 3 Apply the target to the given argumentsList.
1542  __ Jump(BUILTIN_CODE(masm->isolate(), CallWithArrayLike),
1543  RelocInfo::CODE_TARGET);
1544 }
1545 
1546 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1547  // ----------- S t a t e -------------
1548  // -- r2 : argc
1549  // -- sp[0] : new.target (optional)
1550  // -- sp[4] : argumentsList
1551  // -- sp[8] : target
1552  // -- sp[12] : receiver
1553  // -----------------------------------
1554 
1555  // 1. Load target into r3 (if present), argumentsList into r4 (if present),
1556  // new.target into r5 (if present, otherwise use target), remove all
1557  // arguments from the stack (including the receiver), and push thisArgument
1558  // (if present) instead.
1559  {
1560  Label skip;
1561  Register arg_size = r7;
1562  Register new_sp = r6;
1563  __ ShiftLeftP(arg_size, r2, Operand(kPointerSizeLog2));
1564  __ AddP(new_sp, sp, arg_size);
1565  __ LoadRoot(r3, RootIndex::kUndefinedValue);
1566  __ LoadRR(r4, r3);
1567  __ LoadRR(r5, r3);
1568  __ StoreP(r3, MemOperand(new_sp, 0)); // receiver (undefined)
1569  __ CmpP(arg_size, Operand(kPointerSize));
1570  __ blt(&skip);
1571  __ LoadP(r3, MemOperand(new_sp, 1 * -kPointerSize)); // target
1572  __ LoadRR(r5, r3); // new.target defaults to target
1573  __ beq(&skip);
1574  __ LoadP(r4, MemOperand(new_sp, 2 * -kPointerSize)); // argumentsList
1575  __ CmpP(arg_size, Operand(2 * kPointerSize));
1576  __ beq(&skip);
1577  __ LoadP(r5, MemOperand(new_sp, 3 * -kPointerSize)); // new.target
1578  __ bind(&skip);
1579  __ LoadRR(sp, new_sp);
1580  }
1581 
1582  // ----------- S t a t e -------------
1583  // -- r4 : argumentsList
1584  // -- r5 : new.target
1585  // -- r3 : target
1586  // -- sp[0] : receiver (undefined)
1587  // -----------------------------------
1588 
1589  // 2. We don't need to check explicitly for constructor target here,
1590  // since that's the first thing the Construct/ConstructWithArrayLike
1591  // builtins will do.
1592 
1593  // 3. We don't need to check explicitly for constructor new.target here,
1594  // since that's the second thing the Construct/ConstructWithArrayLike
1595  // builtins will do.
1596 
1597  // 4. Construct the target with the given new.target and argumentsList.
1598  __ Jump(BUILTIN_CODE(masm->isolate(), ConstructWithArrayLike),
1599  RelocInfo::CODE_TARGET);
1600 }
1601 
1602 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1603  __ SmiTag(r2);
1604  __ Load(r6, Operand(StackFrame::TypeToMarker(StackFrame::ARGUMENTS_ADAPTOR)));
1605  // Stack updated as such:
1606  // old SP --->
1607  // R14 Return Addr
1608  // Old FP <--- New FP
1609  // Argument Adapter SMI
1610  // Function
1611  // ArgC as SMI
1612  // Padding <--- New SP
1613  __ lay(sp, MemOperand(sp, -5 * kPointerSize));
1614 
1615  // Cleanse the top nibble of 31-bit pointers.
1616  __ CleanseP(r14);
1617  __ StoreP(r14, MemOperand(sp, 4 * kPointerSize));
1618  __ StoreP(fp, MemOperand(sp, 3 * kPointerSize));
1619  __ StoreP(r6, MemOperand(sp, 2 * kPointerSize));
1620  __ StoreP(r3, MemOperand(sp, 1 * kPointerSize));
1621  __ StoreP(r2, MemOperand(sp, 0 * kPointerSize));
1622  __ Push(Smi::zero()); // Padding.
1623  __ la(fp,
1624  MemOperand(sp, ArgumentsAdaptorFrameConstants::kFixedFrameSizeFromFp));
1625 }
1626 
1627 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1628  // ----------- S t a t e -------------
1629  // -- r2 : result being passed through
1630  // -----------------------------------
1631  // Get the number of arguments passed (as a smi), tear down the frame and
1632  // then tear down the parameters.
1633  __ LoadP(r3, MemOperand(fp, ArgumentsAdaptorFrameConstants::kLengthOffset));
1634  int stack_adjustment = kPointerSize; // adjust for receiver
1635  __ LeaveFrame(StackFrame::ARGUMENTS_ADAPTOR, stack_adjustment);
1636  __ SmiToPtrArrayOffset(r3, r3);
1637  __ lay(sp, MemOperand(sp, r3));
1638 }
1639 
1640 // static
1641 void Builtins::Generate_CallOrConstructVarargs(MacroAssembler* masm,
1642  Handle<Code> code) {
1643  // ----------- S t a t e -------------
1644  // -- r3 : target
1645  // -- r2 : number of parameters on the stack (not including the receiver)
1646  // -- r4 : arguments list (a FixedArray)
1647  // -- r6 : len (number of elements to push from args)
1648  // -- r5 : new.target (for [[Construct]])
1649  // -----------------------------------
1650 
1651  Register scratch = ip;
1652 
1653  if (masm->emit_debug_code()) {
1654  // Allow r4 to be a FixedArray, or a FixedDoubleArray if r6 == 0.
1655  Label ok, fail;
1656  __ AssertNotSmi(r4);
1657  __ LoadP(scratch, FieldMemOperand(r4, HeapObject::kMapOffset));
1658  __ LoadHalfWordP(scratch,
1659  FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1660  __ CmpP(scratch, Operand(FIXED_ARRAY_TYPE));
1661  __ beq(&ok);
1662  __ CmpP(scratch, Operand(FIXED_DOUBLE_ARRAY_TYPE));
1663  __ bne(&fail);
1664  __ CmpP(r6, Operand::Zero());
1665  __ beq(&ok);
1666  // Fall through.
1667  __ bind(&fail);
1668  __ Abort(AbortReason::kOperandIsNotAFixedArray);
1669 
1670  __ bind(&ok);
1671  }
1672 
1673  // Check for stack overflow.
1674  Label stack_overflow;
1675  Generate_StackOverflowCheck(masm, r6, ip, &stack_overflow);
1676 
1677  // Push arguments onto the stack (thisArgument is already on the stack).
1678  {
1679  Label loop, no_args, skip;
1680  __ CmpP(r6, Operand::Zero());
1681  __ beq(&no_args);
1682  __ AddP(r4, r4,
1683  Operand(FixedArray::kHeaderSize - kHeapObjectTag - kPointerSize));
1684  __ LoadRR(r1, r6);
1685  __ bind(&loop);
1686  __ LoadP(ip, MemOperand(r4, kPointerSize));
1687  __ la(r4, MemOperand(r4, kPointerSize));
1688  __ CompareRoot(ip, RootIndex::kTheHoleValue);
1689  __ bne(&skip, Label::kNear);
1690  __ LoadRoot(ip, RootIndex::kUndefinedValue);
1691  __ bind(&skip);
1692  __ push(ip);
1693  __ BranchOnCount(r1, &loop);
1694  __ bind(&no_args);
1695  __ AddP(r2, r2, r6);
1696  }
1697 
1698  // Tail-call to the actual Call or Construct builtin.
1699  __ Jump(code, RelocInfo::CODE_TARGET);
1700 
1701  __ bind(&stack_overflow);
1702  __ TailCallRuntime(Runtime::kThrowStackOverflow);
1703 }
1704 
1705 // static
1706 void Builtins::Generate_CallOrConstructForwardVarargs(MacroAssembler* masm,
1707  CallOrConstructMode mode,
1708  Handle<Code> code) {
1709  // ----------- S t a t e -------------
1710  // -- r2 : the number of arguments (not including the receiver)
1711  // -- r5 : the new.target (for [[Construct]] calls)
1712  // -- r3 : the target to call (can be any Object)
1713  // -- r4 : start index (to support rest parameters)
1714  // -----------------------------------
1715 
1716  Register scratch = r8;
1717 
1718  if (mode == CallOrConstructMode::kConstruct) {
1719  Label new_target_constructor, new_target_not_constructor;
1720  __ JumpIfSmi(r5, &new_target_not_constructor);
1721  __ LoadP(scratch, FieldMemOperand(r5, HeapObject::kMapOffset));
1722  __ LoadlB(scratch, FieldMemOperand(scratch, Map::kBitFieldOffset));
1723  __ tmll(scratch, Operand(Map::IsConstructorBit::kShift));
1724  __ bne(&new_target_constructor);
1725  __ bind(&new_target_not_constructor);
1726  {
1727  FrameScope scope(masm, StackFrame::MANUAL);
1728  __ EnterFrame(StackFrame::INTERNAL);
1729  __ Push(r5);
1730  __ CallRuntime(Runtime::kThrowNotConstructor);
1731  }
1732  __ bind(&new_target_constructor);
1733  }
1734 
1735  // Check if we have an arguments adaptor frame below the function frame.
1736  Label arguments_adaptor, arguments_done;
1737  __ LoadP(r6, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1738  __ LoadP(ip, MemOperand(r6, CommonFrameConstants::kContextOrFrameTypeOffset));
1739  __ CmpP(ip, Operand(StackFrame::TypeToMarker(StackFrame::ARGUMENTS_ADAPTOR)));
1740  __ beq(&arguments_adaptor);
1741  {
1742  __ LoadP(r7, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
1743  __ LoadP(r7, FieldMemOperand(r7, JSFunction::kSharedFunctionInfoOffset));
1744  __ LoadLogicalHalfWordP(
1745  r7,
1746  FieldMemOperand(r7, SharedFunctionInfo::kFormalParameterCountOffset));
1747  __ LoadRR(r6, fp);
1748  }
1749  __ b(&arguments_done);
1750  __ bind(&arguments_adaptor);
1751  {
1752  // Load the length from the ArgumentsAdaptorFrame.
1753  __ LoadP(r7, MemOperand(r6, ArgumentsAdaptorFrameConstants::kLengthOffset));
1754  __ SmiUntag(r7);
1755  }
1756  __ bind(&arguments_done);
1757 
1758  Label stack_done, stack_overflow;
1759  __ SubP(r7, r7, r4);
1760  __ CmpP(r7, Operand::Zero());
1761  __ ble(&stack_done);
1762  {
1763  // Check for stack overflow.
1764  Generate_StackOverflowCheck(masm, r7, r4, &stack_overflow);
1765 
1766  // Forward the arguments from the caller frame.
1767  {
1768  Label loop;
1769  __ AddP(r6, r6, Operand(kPointerSize));
1770  __ AddP(r2, r2, r7);
1771  __ bind(&loop);
1772  {
1773  __ ShiftLeftP(ip, r7, Operand(kPointerSizeLog2));
1774  __ LoadP(ip, MemOperand(r6, ip));
1775  __ push(ip);
1776  __ SubP(r7, r7, Operand(1));
1777  __ CmpP(r7, Operand::Zero());
1778  __ bne(&loop);
1779  }
1780  }
1781  }
1782  __ b(&stack_done);
1783  __ bind(&stack_overflow);
1784  __ TailCallRuntime(Runtime::kThrowStackOverflow);
1785  __ bind(&stack_done);
1786 
1787  // Tail-call to the {code} handler.
1788  __ Jump(code, RelocInfo::CODE_TARGET);
1789 }
1790 
1791 // static
1792 void Builtins::Generate_CallFunction(MacroAssembler* masm,
1793  ConvertReceiverMode mode) {
1794  // ----------- S t a t e -------------
1795  // -- r2 : the number of arguments (not including the receiver)
1796  // -- r3 : the function to call (checked to be a JSFunction)
1797  // -----------------------------------
1798  __ AssertFunction(r3);
1799 
1800  // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
1801  // Check that the function is not a "classConstructor".
1802  Label class_constructor;
1803  __ LoadP(r4, FieldMemOperand(r3, JSFunction::kSharedFunctionInfoOffset));
1804  __ LoadlW(r5, FieldMemOperand(r4, SharedFunctionInfo::kFlagsOffset));
1805  __ TestBitMask(r5, SharedFunctionInfo::IsClassConstructorBit::kMask, r0);
1806  __ bne(&class_constructor);
1807 
1808  // Enter the context of the function; ToObject has to run in the function
1809  // context, and we also need to take the global proxy from the function
1810  // context in case of conversion.
1811  __ LoadP(cp, FieldMemOperand(r3, JSFunction::kContextOffset));
1812  // We need to convert the receiver for non-native sloppy mode functions.
1813  Label done_convert;
1814  __ AndP(r0, r5,
1815  Operand(SharedFunctionInfo::IsStrictBit::kMask |
1816  SharedFunctionInfo::IsNativeBit::kMask));
1817  __ bne(&done_convert);
1818  {
1819  // ----------- S t a t e -------------
1820  // -- r2 : the number of arguments (not including the receiver)
1821  // -- r3 : the function to call (checked to be a JSFunction)
1822  // -- r4 : the shared function info.
1823  // -- cp : the function context.
1824  // -----------------------------------
1825 
1826  if (mode == ConvertReceiverMode::kNullOrUndefined) {
1827  // Patch receiver to global proxy.
1828  __ LoadGlobalProxy(r5);
1829  } else {
1830  Label convert_to_object, convert_receiver;
1831  __ ShiftLeftP(r5, r2, Operand(kPointerSizeLog2));
1832  __ LoadP(r5, MemOperand(sp, r5));
1833  __ JumpIfSmi(r5, &convert_to_object);
1834  STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
1835  __ CompareObjectType(r5, r6, r6, FIRST_JS_RECEIVER_TYPE);
1836  __ bge(&done_convert);
1837  if (mode != ConvertReceiverMode::kNotNullOrUndefined) {
1838  Label convert_global_proxy;
1839  __ JumpIfRoot(r5, RootIndex::kUndefinedValue, &convert_global_proxy);
1840  __ JumpIfNotRoot(r5, RootIndex::kNullValue, &convert_to_object);
1841  __ bind(&convert_global_proxy);
1842  {
1843  // Patch receiver to global proxy.
1844  __ LoadGlobalProxy(r5);
1845  }
1846  __ b(&convert_receiver);
1847  }
1848  __ bind(&convert_to_object);
1849  {
1850  // Convert receiver using ToObject.
1851  // TODO(bmeurer): Inline the allocation here to avoid building the frame
1852  // in the fast case? (fall back to AllocateInNewSpace?)
1853  FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1854  __ SmiTag(r2);
1855  __ Push(r2, r3);
1856  __ LoadRR(r2, r5);
1857  __ Push(cp);
1858  __ Call(BUILTIN_CODE(masm->isolate(), ToObject),
1859  RelocInfo::CODE_TARGET);
1860  __ Pop(cp);
1861  __ LoadRR(r5, r2);
1862  __ Pop(r2, r3);
1863  __ SmiUntag(r2);
1864  }
1865  __ LoadP(r4, FieldMemOperand(r3, JSFunction::kSharedFunctionInfoOffset));
1866  __ bind(&convert_receiver);
1867  }
1868  __ ShiftLeftP(r6, r2, Operand(kPointerSizeLog2));
1869  __ StoreP(r5, MemOperand(sp, r6));
1870  }
1871  __ bind(&done_convert);
1872 
1873  // ----------- S t a t e -------------
1874  // -- r2 : the number of arguments (not including the receiver)
1875  // -- r3 : the function to call (checked to be a JSFunction)
1876  // -- r4 : the shared function info.
1877  // -- cp : the function context.
1878  // -----------------------------------
1879 
1880  __ LoadLogicalHalfWordP(
1881  r4, FieldMemOperand(r4, SharedFunctionInfo::kFormalParameterCountOffset));
1882  ParameterCount actual(r2);
1883  ParameterCount expected(r4);
1884  __ InvokeFunctionCode(r3, no_reg, expected, actual, JUMP_FUNCTION);
1885 
1886  // The function is a "classConstructor", need to raise an exception.
1887  __ bind(&class_constructor);
1888  {
1889  FrameAndConstantPoolScope frame(masm, StackFrame::INTERNAL);
1890  __ push(r3);
1891  __ CallRuntime(Runtime::kThrowConstructorNonCallableError);
1892  }
1893 }
1894 
1895 namespace {
1896 
1897 void Generate_PushBoundArguments(MacroAssembler* masm) {
1898  // ----------- S t a t e -------------
1899  // -- r2 : the number of arguments (not including the receiver)
1900  // -- r3 : target (checked to be a JSBoundFunction)
1901  // -- r5 : new.target (only in case of [[Construct]])
1902  // -----------------------------------
1903 
1904  // Load [[BoundArguments]] into r4 and length of that into r6.
1905  Label no_bound_arguments;
1906  __ LoadP(r4, FieldMemOperand(r3, JSBoundFunction::kBoundArgumentsOffset));
1907  __ LoadP(r6, FieldMemOperand(r4, FixedArray::kLengthOffset));
1908  __ SmiUntag(r6);
1909  __ LoadAndTestP(r6, r6);
1910  __ beq(&no_bound_arguments);
1911  {
1912  // ----------- S t a t e -------------
1913  // -- r2 : the number of arguments (not including the receiver)
1914  // -- r3 : target (checked to be a JSBoundFunction)
1915  // -- r4 : the [[BoundArguments]] (implemented as FixedArray)
1916  // -- r5 : new.target (only in case of [[Construct]])
1917  // -- r6 : the number of [[BoundArguments]]
1918  // -----------------------------------
1919 
1920  // Reserve stack space for the [[BoundArguments]].
1921  {
1922  Label done;
1923  __ LoadRR(r8, sp); // preserve previous stack pointer
1924  __ ShiftLeftP(r9, r6, Operand(kPointerSizeLog2));
1925  __ SubP(sp, sp, r9);
1926  // Check the stack for overflow. We are not trying to catch interruptions
1927  // (i.e. debug break and preemption) here, so check the "real stack
1928  // limit".
1929  __ CompareRoot(sp, RootIndex::kRealStackLimit);
1930  __ bgt(&done); // Signed comparison.
1931  // Restore the stack pointer.
1932  __ LoadRR(sp, r8);
1933  {
1934  FrameScope scope(masm, StackFrame::MANUAL);
1935  __ EnterFrame(StackFrame::INTERNAL);
1936  __ CallRuntime(Runtime::kThrowStackOverflow);
1937  }
1938  __ bind(&done);
1939  }
1940 
1941  // Relocate arguments down the stack.
1942  // -- r2 : the number of arguments (not including the receiver)
1943  // -- r8 : the previous stack pointer
1944  // -- r9: the size of the [[BoundArguments]]
1945  {
1946  Label skip, loop;
1947  __ LoadImmP(r7, Operand::Zero());
1948  __ CmpP(r2, Operand::Zero());
1949  __ beq(&skip);
1950  __ LoadRR(r1, r2);
1951  __ bind(&loop);
1952  __ LoadP(r0, MemOperand(r8, r7));
1953  __ StoreP(r0, MemOperand(sp, r7));
1954  __ AddP(r7, r7, Operand(kPointerSize));
1955  __ BranchOnCount(r1, &loop);
1956  __ bind(&skip);
1957  }
1958 
1959  // Copy [[BoundArguments]] to the stack (below the arguments).
1960  {
1961  Label loop;
1962  __ AddP(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1963  __ AddP(r4, r4, r9);
1964  __ LoadRR(r1, r6);
1965  __ bind(&loop);
1966  __ LoadP(r0, MemOperand(r4, -kPointerSize));
1967  __ lay(r4, MemOperand(r4, -kPointerSize));
1968  __ StoreP(r0, MemOperand(sp, r7));
1969  __ AddP(r7, r7, Operand(kPointerSize));
1970  __ BranchOnCount(r1, &loop);
1971  __ AddP(r2, r2, r6);
1972  }
1973  }
1974  __ bind(&no_bound_arguments);
1975 }
1976 
1977 } // namespace
1978 
1979 // static
1980 void Builtins::Generate_CallBoundFunctionImpl(MacroAssembler* masm) {
1981  // ----------- S t a t e -------------
1982  // -- r2 : the number of arguments (not including the receiver)
1983  // -- r3 : the function to call (checked to be a JSBoundFunction)
1984  // -----------------------------------
1985  __ AssertBoundFunction(r3);
1986 
1987  // Patch the receiver to [[BoundThis]].
1988  __ LoadP(ip, FieldMemOperand(r3, JSBoundFunction::kBoundThisOffset));
1989  __ ShiftLeftP(r1, r2, Operand(kPointerSizeLog2));
1990  __ StoreP(ip, MemOperand(sp, r1));
1991 
1992  // Push the [[BoundArguments]] onto the stack.
1993  Generate_PushBoundArguments(masm);
1994 
1995  // Call the [[BoundTargetFunction]] via the Call builtin.
1996  __ LoadP(r3,
1997  FieldMemOperand(r3, JSBoundFunction::kBoundTargetFunctionOffset));
1998  __ Jump(BUILTIN_CODE(masm->isolate(), Call_ReceiverIsAny),
1999  RelocInfo::CODE_TARGET);
2000 }
2001 
2002 // static
2003 void Builtins::Generate_Call(MacroAssembler* masm, ConvertReceiverMode mode) {
2004  // ----------- S t a t e -------------
2005  // -- r2 : the number of arguments (not including the receiver)
2006  // -- r3 : the target to call (can be any Object).
2007  // -----------------------------------
2008 
2009  Label non_callable, non_function, non_smi;
2010  __ JumpIfSmi(r3, &non_callable);
2011  __ bind(&non_smi);
2012  __ CompareObjectType(r3, r6, r7, JS_FUNCTION_TYPE);
2013  __ Jump(masm->isolate()->builtins()->CallFunction(mode),
2014  RelocInfo::CODE_TARGET, eq);
2015  __ CmpP(r7, Operand(JS_BOUND_FUNCTION_TYPE));
2016  __ Jump(BUILTIN_CODE(masm->isolate(), CallBoundFunction),
2017  RelocInfo::CODE_TARGET, eq);
2018 
2019  // Check if target has a [[Call]] internal method.
2020  __ LoadlB(r6, FieldMemOperand(r6, Map::kBitFieldOffset));
2021  __ TestBit(r6, Map::IsCallableBit::kShift);
2022  __ beq(&non_callable);
2023 
2024  // Check if target is a proxy and call CallProxy external builtin
2025  __ CmpP(r7, Operand(JS_PROXY_TYPE));
2026  __ bne(&non_function);
2027  __ Jump(BUILTIN_CODE(masm->isolate(), CallProxy), RelocInfo::CODE_TARGET);
2028 
2029  // 2. Call to something else, which might have a [[Call]] internal method (if
2030  // not we raise an exception).
2031  __ bind(&non_function);
2032  // Overwrite the original receiver the (original) target.
2033  __ ShiftLeftP(r7, r2, Operand(kPointerSizeLog2));
2034  __ StoreP(r3, MemOperand(sp, r7));
2035  // Let the "call_as_function_delegate" take care of the rest.
2036  __ LoadNativeContextSlot(Context::CALL_AS_FUNCTION_DELEGATE_INDEX, r3);
2037  __ Jump(masm->isolate()->builtins()->CallFunction(
2038  ConvertReceiverMode::kNotNullOrUndefined),
2039  RelocInfo::CODE_TARGET);
2040 
2041  // 3. Call to something that is not callable.
2042  __ bind(&non_callable);
2043  {
2044  FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
2045  __ Push(r3);
2046  __ CallRuntime(Runtime::kThrowCalledNonCallable);
2047  }
2048 }
2049 
2050 // static
2051 void Builtins::Generate_ConstructFunction(MacroAssembler* masm) {
2052  // ----------- S t a t e -------------
2053  // -- r2 : the number of arguments (not including the receiver)
2054  // -- r3 : the constructor to call (checked to be a JSFunction)
2055  // -- r5 : the new target (checked to be a constructor)
2056  // -----------------------------------
2057  __ AssertConstructor(r3, r1);
2058  __ AssertFunction(r3);
2059 
2060  // Calling convention for function specific ConstructStubs require
2061  // r4 to contain either an AllocationSite or undefined.
2062  __ LoadRoot(r4, RootIndex::kUndefinedValue);
2063 
2064  Label call_generic_stub;
2065 
2066  // Jump to JSBuiltinsConstructStub or JSConstructStubGeneric.
2067  __ LoadP(r6, FieldMemOperand(r3, JSFunction::kSharedFunctionInfoOffset));
2068  __ LoadlW(r6, FieldMemOperand(r6, SharedFunctionInfo::kFlagsOffset));
2069  __ AndP(r6, Operand(SharedFunctionInfo::ConstructAsBuiltinBit::kMask));
2070  __ beq(&call_generic_stub);
2071 
2072  __ Jump(BUILTIN_CODE(masm->isolate(), JSBuiltinsConstructStub),
2073  RelocInfo::CODE_TARGET);
2074 
2075  __ bind(&call_generic_stub);
2076  __ Jump(BUILTIN_CODE(masm->isolate(), JSConstructStubGeneric),
2077  RelocInfo::CODE_TARGET);
2078 }
2079 
2080 // static
2081 void Builtins::Generate_ConstructBoundFunction(MacroAssembler* masm) {
2082  // ----------- S t a t e -------------
2083  // -- r2 : the number of arguments (not including the receiver)
2084  // -- r3 : the function to call (checked to be a JSBoundFunction)
2085  // -- r5 : the new target (checked to be a constructor)
2086  // -----------------------------------
2087  __ AssertConstructor(r3, r1);
2088  __ AssertBoundFunction(r3);
2089 
2090  // Push the [[BoundArguments]] onto the stack.
2091  Generate_PushBoundArguments(masm);
2092 
2093  // Patch new.target to [[BoundTargetFunction]] if new.target equals target.
2094  Label skip;
2095  __ CmpP(r3, r5);
2096  __ bne(&skip);
2097  __ LoadP(r5,
2098  FieldMemOperand(r3, JSBoundFunction::kBoundTargetFunctionOffset));
2099  __ bind(&skip);
2100 
2101  // Construct the [[BoundTargetFunction]] via the Construct builtin.
2102  __ LoadP(r3,
2103  FieldMemOperand(r3, JSBoundFunction::kBoundTargetFunctionOffset));
2104  __ Jump(BUILTIN_CODE(masm->isolate(), Construct), RelocInfo::CODE_TARGET);
2105 }
2106 
2107 // static
2108 void Builtins::Generate_Construct(MacroAssembler* masm) {
2109  // ----------- S t a t e -------------
2110  // -- r2 : the number of arguments (not including the receiver)
2111  // -- r3 : the constructor to call (can be any Object)
2112  // -- r5 : the new target (either the same as the constructor or
2113  // the JSFunction on which new was invoked initially)
2114  // -----------------------------------
2115 
2116  // Check if target is a Smi.
2117  Label non_constructor, non_proxy;
2118  __ JumpIfSmi(r3, &non_constructor);
2119 
2120  // Check if target has a [[Construct]] internal method.
2121  __ LoadP(r6, FieldMemOperand(r3, HeapObject::kMapOffset));
2122  __ LoadlB(r4, FieldMemOperand(r6, Map::kBitFieldOffset));
2123  __ TestBit(r4, Map::IsConstructorBit::kShift);
2124  __ beq(&non_constructor);
2125 
2126  // Dispatch based on instance type.
2127  __ CompareInstanceType(r6, r7, JS_FUNCTION_TYPE);
2128  __ Jump(BUILTIN_CODE(masm->isolate(), ConstructFunction),
2129  RelocInfo::CODE_TARGET, eq);
2130 
2131  // Only dispatch to bound functions after checking whether they are
2132  // constructors.
2133  __ CmpP(r7, Operand(JS_BOUND_FUNCTION_TYPE));
2134  __ Jump(BUILTIN_CODE(masm->isolate(), ConstructBoundFunction),
2135  RelocInfo::CODE_TARGET, eq);
2136 
2137  // Only dispatch to proxies after checking whether they are constructors.
2138  __ CmpP(r7, Operand(JS_PROXY_TYPE));
2139  __ bne(&non_proxy);
2140  __ Jump(BUILTIN_CODE(masm->isolate(), ConstructProxy),
2141  RelocInfo::CODE_TARGET);
2142 
2143  // Called Construct on an exotic Object with a [[Construct]] internal method.
2144  __ bind(&non_proxy);
2145  {
2146  // Overwrite the original receiver with the (original) target.
2147  __ ShiftLeftP(r7, r2, Operand(kPointerSizeLog2));
2148  __ StoreP(r3, MemOperand(sp, r7));
2149  // Let the "call_as_constructor_delegate" take care of the rest.
2150  __ LoadNativeContextSlot(Context::CALL_AS_CONSTRUCTOR_DELEGATE_INDEX, r3);
2151  __ Jump(masm->isolate()->builtins()->CallFunction(),
2152  RelocInfo::CODE_TARGET);
2153  }
2154 
2155  // Called Construct on an Object that doesn't have a [[Construct]] internal
2156  // method.
2157  __ bind(&non_constructor);
2158  __ Jump(BUILTIN_CODE(masm->isolate(), ConstructedNonConstructable),
2159  RelocInfo::CODE_TARGET);
2160 }
2161 
2162 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
2163  // ----------- S t a t e -------------
2164  // -- r2 : actual number of arguments
2165  // -- r3 : function (passed through to callee)
2166  // -- r4 : expected number of arguments
2167  // -- r5 : new target (passed through to callee)
2168  // -----------------------------------
2169 
2170  Label invoke, dont_adapt_arguments, stack_overflow;
2171 
2172  Label enough, too_few;
2173  __ tmll(r4, Operand(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
2174  __ b(Condition(1), &dont_adapt_arguments);
2175  __ CmpLogicalP(r2, r4);
2176  __ blt(&too_few);
2177 
2178  { // Enough parameters: actual >= expected
2179  __ bind(&enough);
2180  EnterArgumentsAdaptorFrame(masm);
2181  Generate_StackOverflowCheck(masm, r4, r7, &stack_overflow);
2182 
2183  // Calculate copy start address into r2 and copy end address into r6.
2184  // r2: actual number of arguments as a smi
2185  // r3: function
2186  // r4: expected number of arguments
2187  // r5: new target (passed through to callee)
2188  __ SmiToPtrArrayOffset(r2, r2);
2189  __ AddP(r2, fp);
2190  // adjust for return address and receiver
2191  __ AddP(r2, r2, Operand(2 * kPointerSize));
2192  __ ShiftLeftP(r6, r4, Operand(kPointerSizeLog2));
2193  __ SubP(r6, r2, r6);
2194 
2195  // Copy the arguments (including the receiver) to the new stack frame.
2196  // r2: copy start address
2197  // r3: function
2198  // r4: expected number of arguments
2199  // r5: new target (passed through to callee)
2200  // r6: copy end address
2201 
2202  Label copy;
2203  __ bind(&copy);
2204  __ LoadP(r0, MemOperand(r2, 0));
2205  __ push(r0);
2206  __ CmpP(r2, r6); // Compare before moving to next argument.
2207  __ lay(r2, MemOperand(r2, -kPointerSize));
2208  __ bne(&copy);
2209 
2210  __ b(&invoke);
2211  }
2212 
2213  { // Too few parameters: Actual < expected
2214  __ bind(&too_few);
2215 
2216  EnterArgumentsAdaptorFrame(masm);
2217  Generate_StackOverflowCheck(masm, r4, r7, &stack_overflow);
2218 
2219  // Calculate copy start address into r0 and copy end address is fp.
2220  // r2: actual number of arguments as a smi
2221  // r3: function
2222  // r4: expected number of arguments
2223  // r5: new target (passed through to callee)
2224  __ SmiToPtrArrayOffset(r2, r2);
2225  __ lay(r2, MemOperand(r2, fp));
2226 
2227  // Copy the arguments (including the receiver) to the new stack frame.
2228  // r2: copy start address
2229  // r3: function
2230  // r4: expected number of arguments
2231  // r5: new target (passed through to callee)
2232  Label copy;
2233  __ bind(&copy);
2234  // Adjust load for return address and receiver.
2235  __ LoadP(r0, MemOperand(r2, 2 * kPointerSize));
2236  __ push(r0);
2237  __ CmpP(r2, fp); // Compare before moving to next argument.
2238  __ lay(r2, MemOperand(r2, -kPointerSize));
2239  __ bne(&copy);
2240 
2241  // Fill the remaining expected arguments with undefined.
2242  // r3: function
2243  // r4: expected number of argumentus
2244  __ LoadRoot(r0, RootIndex::kUndefinedValue);
2245  __ ShiftLeftP(r6, r4, Operand(kPointerSizeLog2));
2246  __ SubP(r6, fp, r6);
2247  // Adjust for frame.
2248  __ SubP(r6, r6,
2249  Operand(ArgumentsAdaptorFrameConstants::kFixedFrameSizeFromFp +
2250  kPointerSize));
2251 
2252  Label fill;
2253  __ bind(&fill);
2254  __ push(r0);
2255  __ CmpP(sp, r6);
2256  __ bne(&fill);
2257  }
2258 
2259  // Call the entry point.
2260  __ bind(&invoke);
2261  __ LoadRR(r2, r4);
2262  // r2 : expected number of arguments
2263  // r3 : function (passed through to callee)
2264  // r5 : new target (passed through to callee)
2265  static_assert(kJavaScriptCallCodeStartRegister == r4, "ABI mismatch");
2266  __ LoadP(r4, FieldMemOperand(r3, JSFunction::kCodeOffset));
2267  __ AddP(r4, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
2268  __ CallJSEntry(r4);
2269 
2270  // Store offset of return address for deoptimizer.
2271  masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
2272 
2273  // Exit frame and return.
2274  LeaveArgumentsAdaptorFrame(masm);
2275  __ Ret();
2276 
2277  // -------------------------------------------
2278  // Dont adapt arguments.
2279  // -------------------------------------------
2280  __ bind(&dont_adapt_arguments);
2281  static_assert(kJavaScriptCallCodeStartRegister == r4, "ABI mismatch");
2282  __ LoadP(r4, FieldMemOperand(r3, JSFunction::kCodeOffset));
2283  __ AddP(r4, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
2284  __ JumpToJSEntry(r4);
2285 
2286  __ bind(&stack_overflow);
2287  {
2288  FrameScope frame(masm, StackFrame::MANUAL);
2289  __ CallRuntime(Runtime::kThrowStackOverflow);
2290  __ bkpt(0);
2291  }
2292 }
2293 
2294 void Builtins::Generate_WasmCompileLazy(MacroAssembler* masm) {
2295  // The function index was put in a register by the jump table trampoline.
2296  // Convert to Smi for the runtime call.
2297  __ SmiTag(kWasmCompileLazyFuncIndexRegister,
2298  kWasmCompileLazyFuncIndexRegister);
2299  {
2300  HardAbortScope hard_abort(masm); // Avoid calls to Abort.
2301  FrameAndConstantPoolScope scope(masm, StackFrame::WASM_COMPILE_LAZY);
2302 
2303  // Save all parameter registers (see wasm-linkage.cc). They might be
2304  // overwritten in the runtime call below. We don't have any callee-saved
2305  // registers in wasm, so no need to store anything else.
2306  constexpr RegList gp_regs = Register::ListOf<r2, r3, r4, r5, r6>();
2307 #if V8_TARGET_ARCH_S390X
2308  constexpr RegList fp_regs = DoubleRegister::ListOf<d0, d2, d4, d6>();
2309 #else
2310  constexpr RegList fp_regs = DoubleRegister::ListOf<d0, d2>();
2311 #endif
2312  __ MultiPush(gp_regs);
2313  __ MultiPushDoubles(fp_regs);
2314 
2315  // Pass instance and function index as explicit arguments to the runtime
2316  // function.
2317  __ Push(kWasmInstanceRegister, r7);
2318  // Load the correct CEntry builtin from the instance object.
2319  __ LoadP(r4, FieldMemOperand(kWasmInstanceRegister,
2320  WasmInstanceObject::kCEntryStubOffset));
2321  // Initialize the JavaScript context with 0. CEntry will use it to
2322  // set the current context on the isolate.
2323  __ LoadSmiLiteral(cp, Smi::zero());
2324  __ CallRuntimeWithCEntry(Runtime::kWasmCompileLazy, r4);
2325  // The entrypoint address is the return value.
2326  __ LoadRR(ip, r2);
2327 
2328  // Restore registers.
2329  __ MultiPopDoubles(fp_regs);
2330  __ MultiPop(gp_regs);
2331  }
2332  // Finally, jump to the entrypoint.
2333  __ Jump(ip);
2334 }
2335 
2336 void Builtins::Generate_CEntry(MacroAssembler* masm, int result_size,
2337  SaveFPRegsMode save_doubles, ArgvMode argv_mode,
2338  bool builtin_exit_frame) {
2339  // Called from JavaScript; parameters are on stack as if calling JS function.
2340  // r2: number of arguments including receiver
2341  // r3: pointer to builtin function
2342  // fp: frame pointer (restored after C call)
2343  // sp: stack pointer (restored as callee's sp after C call)
2344  // cp: current context (C callee-saved)
2345  //
2346  // If argv_mode == kArgvInRegister:
2347  // r4: pointer to the first argument
2348 
2349  __ LoadRR(r7, r3);
2350 
2351  if (argv_mode == kArgvInRegister) {
2352  // Move argv into the correct register.
2353  __ LoadRR(r3, r4);
2354  } else {
2355  // Compute the argv pointer.
2356  __ ShiftLeftP(r3, r2, Operand(kPointerSizeLog2));
2357  __ lay(r3, MemOperand(r3, sp, -kPointerSize));
2358  }
2359 
2360  // Enter the exit frame that transitions from JavaScript to C++.
2361  FrameScope scope(masm, StackFrame::MANUAL);
2362 
2363  // Need at least one extra slot for return address location.
2364  int arg_stack_space = 1;
2365 
2366  // Pass buffer for return value on stack if necessary
2367  bool needs_return_buffer =
2368  result_size == 2 && !ABI_RETURNS_OBJECTPAIR_IN_REGS;
2369  if (needs_return_buffer) {
2370  arg_stack_space += result_size;
2371  }
2372 
2373 #if V8_TARGET_ARCH_S390X
2374  // 64-bit linux pass Argument object by reference not value
2375  arg_stack_space += 2;
2376 #endif
2377 
2378  __ EnterExitFrame(
2379  save_doubles, arg_stack_space,
2380  builtin_exit_frame ? StackFrame::BUILTIN_EXIT : StackFrame::EXIT);
2381 
2382  // Store a copy of argc, argv in callee-saved registers for later.
2383  __ LoadRR(r6, r2);
2384  __ LoadRR(r8, r3);
2385  // r2, r6: number of arguments including receiver (C callee-saved)
2386  // r3, r8: pointer to the first argument
2387  // r7: pointer to builtin function (C callee-saved)
2388 
2389  // Result returned in registers or stack, depending on result size and ABI.
2390 
2391  Register isolate_reg = r4;
2392  if (needs_return_buffer) {
2393  // The return value is 16-byte non-scalar value.
2394  // Use frame storage reserved by calling function to pass return
2395  // buffer as implicit first argument in R2. Shfit original parameters
2396  // by one register each.
2397  __ LoadRR(r4, r3);
2398  __ LoadRR(r3, r2);
2399  __ la(r2, MemOperand(sp, (kStackFrameExtraParamSlot + 1) * kPointerSize));
2400  isolate_reg = r5;
2401  // Clang doesn't preserve r2 (result buffer)
2402  // write to r8 (preserved) before entry
2403  __ LoadRR(r8, r2);
2404  }
2405  // Call C built-in.
2406  __ Move(isolate_reg, ExternalReference::isolate_address(masm->isolate()));
2407 
2408  Register target = r7;
2409 
2410  // To let the GC traverse the return address of the exit frames, we need to
2411  // know where the return address is. The CEntryStub is unmovable, so
2412  // we can store the address on the stack to be able to find it again and
2413  // we never have to restore it, because it will not change.
2414  {
2415  Label return_label;
2416  __ larl(r14, &return_label); // Generate the return addr of call later.
2417  __ StoreP(r14, MemOperand(sp, kStackFrameRASlot * kPointerSize));
2418 
2419  // zLinux ABI requires caller's frame to have sufficient space for callee
2420  // preserved regsiter save area.
2421  // __ lay(sp, MemOperand(sp, -kCalleeRegisterSaveAreaSize));
2422  __ b(target);
2423  __ bind(&return_label);
2424  // __ la(sp, MemOperand(sp, +kCalleeRegisterSaveAreaSize));
2425  }
2426 
2427  // If return value is on the stack, pop it to registers.
2428  if (needs_return_buffer) {
2429  __ LoadRR(r2, r8);
2430  __ LoadP(r3, MemOperand(r2, kPointerSize));
2431  __ LoadP(r2, MemOperand(r2));
2432  }
2433 
2434  // Check result for exception sentinel.
2435  Label exception_returned;
2436  __ CompareRoot(r2, RootIndex::kException);
2437  __ beq(&exception_returned, Label::kNear);
2438 
2439  // Check that there is no pending exception, otherwise we
2440  // should have returned the exception sentinel.
2441  if (FLAG_debug_code) {
2442  Label okay;
2443  ExternalReference pending_exception_address = ExternalReference::Create(
2444  IsolateAddressId::kPendingExceptionAddress, masm->isolate());
2445  __ Move(r1, pending_exception_address);
2446  __ LoadP(r1, MemOperand(r1));
2447  __ CompareRoot(r1, RootIndex::kTheHoleValue);
2448  // Cannot use check here as it attempts to generate call into runtime.
2449  __ beq(&okay, Label::kNear);
2450  __ stop("Unexpected pending exception");
2451  __ bind(&okay);
2452  }
2453 
2454  // Exit C frame and return.
2455  // r2:r3: result
2456  // sp: stack pointer
2457  // fp: frame pointer
2458  Register argc = argv_mode == kArgvInRegister
2459  // We don't want to pop arguments so set argc to no_reg.
2460  ? no_reg
2461  // r6: still holds argc (callee-saved).
2462  : r6;
2463  __ LeaveExitFrame(save_doubles, argc);
2464  __ b(r14);
2465 
2466  // Handling of exception.
2467  __ bind(&exception_returned);
2468 
2469  ExternalReference pending_handler_context_address = ExternalReference::Create(
2470  IsolateAddressId::kPendingHandlerContextAddress, masm->isolate());
2471  ExternalReference pending_handler_entrypoint_address =
2472  ExternalReference::Create(
2473  IsolateAddressId::kPendingHandlerEntrypointAddress, masm->isolate());
2474  ExternalReference pending_handler_fp_address = ExternalReference::Create(
2475  IsolateAddressId::kPendingHandlerFPAddress, masm->isolate());
2476  ExternalReference pending_handler_sp_address = ExternalReference::Create(
2477  IsolateAddressId::kPendingHandlerSPAddress, masm->isolate());
2478 
2479  // Ask the runtime for help to determine the handler. This will set r3 to
2480  // contain the current pending exception, don't clobber it.
2481  ExternalReference find_handler =
2482  ExternalReference::Create(Runtime::kUnwindAndFindExceptionHandler);
2483  {
2484  FrameScope scope(masm, StackFrame::MANUAL);
2485  __ PrepareCallCFunction(3, 0, r2);
2486  __ LoadImmP(r2, Operand::Zero());
2487  __ LoadImmP(r3, Operand::Zero());
2488  __ Move(r4, ExternalReference::isolate_address(masm->isolate()));
2489  __ CallCFunction(find_handler, 3);
2490  }
2491 
2492  // Retrieve the handler context, SP and FP.
2493  __ Move(cp, pending_handler_context_address);
2494  __ LoadP(cp, MemOperand(cp));
2495  __ Move(sp, pending_handler_sp_address);
2496  __ LoadP(sp, MemOperand(sp));
2497  __ Move(fp, pending_handler_fp_address);
2498  __ LoadP(fp, MemOperand(fp));
2499 
2500  // If the handler is a JS frame, restore the context to the frame. Note that
2501  // the context will be set to (cp == 0) for non-JS frames.
2502  Label skip;
2503  __ CmpP(cp, Operand::Zero());
2504  __ beq(&skip, Label::kNear);
2505  __ StoreP(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
2506  __ bind(&skip);
2507 
2508  // Reset the masking register. This is done independent of the underlying
2509  // feature flag {FLAG_untrusted_code_mitigations} to make the snapshot work
2510  // with both configurations. It is safe to always do this, because the
2511  // underlying register is caller-saved and can be arbitrarily clobbered.
2512  __ ResetSpeculationPoisonRegister();
2513 
2514  // Compute the handler entry address and jump to it.
2515  __ Move(r3, pending_handler_entrypoint_address);
2516  __ LoadP(r3, MemOperand(r3));
2517  __ Jump(r3);
2518 }
2519 
2520 void Builtins::Generate_DoubleToI(MacroAssembler* masm) {
2521  Label out_of_range, only_low, negate, done, fastpath_done;
2522  Register result_reg = r2;
2523 
2524  HardAbortScope hard_abort(masm); // Avoid calls to Abort.
2525 
2526  // Immediate values for this stub fit in instructions, so it's safe to use ip.
2527  Register scratch = GetRegisterThatIsNotOneOf(result_reg);
2528  Register scratch_low = GetRegisterThatIsNotOneOf(result_reg, scratch);
2529  Register scratch_high =
2530  GetRegisterThatIsNotOneOf(result_reg, scratch, scratch_low);
2531  DoubleRegister double_scratch = kScratchDoubleReg;
2532 
2533  __ Push(result_reg, scratch);
2534  // Account for saved regs.
2535  int argument_offset = 2 * kPointerSize;
2536 
2537  // Load double input.
2538  __ LoadDouble(double_scratch, MemOperand(sp, argument_offset));
2539 
2540  // Do fast-path convert from double to int.
2541  __ ConvertDoubleToInt64(result_reg, double_scratch);
2542 
2543  // Test for overflow
2544  __ TestIfInt32(result_reg);
2545  __ beq(&fastpath_done, Label::kNear);
2546 
2547  __ Push(scratch_high, scratch_low);
2548  // Account for saved regs.
2549  argument_offset += 2 * kPointerSize;
2550 
2551  __ LoadlW(scratch_high,
2552  MemOperand(sp, argument_offset + Register::kExponentOffset));
2553  __ LoadlW(scratch_low,
2554  MemOperand(sp, argument_offset + Register::kMantissaOffset));
2555 
2556  __ ExtractBitMask(scratch, scratch_high, HeapNumber::kExponentMask);
2557  // Load scratch with exponent - 1. This is faster than loading
2558  // with exponent because Bias + 1 = 1024 which is a *S390* immediate value.
2559  STATIC_ASSERT(HeapNumber::kExponentBias + 1 == 1024);
2560  __ SubP(scratch, Operand(HeapNumber::kExponentBias + 1));
2561  // If exponent is greater than or equal to 84, the 32 less significant
2562  // bits are 0s (2^84 = 1, 52 significant bits, 32 uncoded bits),
2563  // the result is 0.
2564  // Compare exponent with 84 (compare exponent - 1 with 83).
2565  __ CmpP(scratch, Operand(83));
2566  __ bge(&out_of_range, Label::kNear);
2567 
2568  // If we reach this code, 31 <= exponent <= 83.
2569  // So, we don't have to handle cases where 0 <= exponent <= 20 for
2570  // which we would need to shift right the high part of the mantissa.
2571  // Scratch contains exponent - 1.
2572  // Load scratch with 52 - exponent (load with 51 - (exponent - 1)).
2573  __ Load(r0, Operand(51));
2574  __ SubP(scratch, r0, scratch);
2575  __ CmpP(scratch, Operand::Zero());
2576  __ ble(&only_low, Label::kNear);
2577  // 21 <= exponent <= 51, shift scratch_low and scratch_high
2578  // to generate the result.
2579  __ ShiftRight(scratch_low, scratch_low, scratch);
2580  // Scratch contains: 52 - exponent.
2581  // We needs: exponent - 20.
2582  // So we use: 32 - scratch = 32 - 52 + exponent = exponent - 20.
2583  __ Load(r0, Operand(32));
2584  __ SubP(scratch, r0, scratch);
2585  __ ExtractBitMask(result_reg, scratch_high, HeapNumber::kMantissaMask);
2586  // Set the implicit 1 before the mantissa part in scratch_high.
2587  STATIC_ASSERT(HeapNumber::kMantissaBitsInTopWord >= 16);
2588  __ Load(r0, Operand(1 << ((HeapNumber::kMantissaBitsInTopWord)-16)));
2589  __ ShiftLeftP(r0, r0, Operand(16));
2590  __ OrP(result_reg, result_reg, r0);
2591  __ ShiftLeft(r0, result_reg, scratch);
2592  __ OrP(result_reg, scratch_low, r0);
2593  __ b(&negate, Label::kNear);
2594 
2595  __ bind(&out_of_range);
2596  __ mov(result_reg, Operand::Zero());
2597  __ b(&done, Label::kNear);
2598 
2599  __ bind(&only_low);
2600  // 52 <= exponent <= 83, shift only scratch_low.
2601  // On entry, scratch contains: 52 - exponent.
2602  __ LoadComplementRR(scratch, scratch);
2603  __ ShiftLeft(result_reg, scratch_low, scratch);
2604 
2605  __ bind(&negate);
2606  // If input was positive, scratch_high ASR 31 equals 0 and
2607  // scratch_high LSR 31 equals zero.
2608  // New result = (result eor 0) + 0 = result.
2609  // If the input was negative, we have to negate the result.
2610  // Input_high ASR 31 equals 0xFFFFFFFF and scratch_high LSR 31 equals 1.
2611  // New result = (result eor 0xFFFFFFFF) + 1 = 0 - result.
2612  __ ShiftRightArith(r0, scratch_high, Operand(31));
2613 #if V8_TARGET_ARCH_S390X
2614  __ lgfr(r0, r0);
2615  __ ShiftRightP(r0, r0, Operand(32));
2616 #endif
2617  __ XorP(result_reg, r0);
2618  __ ShiftRight(r0, scratch_high, Operand(31));
2619  __ AddP(result_reg, r0);
2620 
2621  __ bind(&done);
2622  __ Pop(scratch_high, scratch_low);
2623  argument_offset -= 2 * kPointerSize;
2624 
2625  __ bind(&fastpath_done);
2626  __ StoreP(result_reg, MemOperand(sp, argument_offset));
2627  __ Pop(result_reg, scratch);
2628 
2629  __ Ret();
2630 }
2631 
2632 void Builtins::Generate_MathPowInternal(MacroAssembler* masm) {
2633  const Register exponent = r4;
2634  const DoubleRegister double_base = d1;
2635  const DoubleRegister double_exponent = d2;
2636  const DoubleRegister double_result = d3;
2637  const DoubleRegister double_scratch = d0;
2638  const Register scratch = r1;
2639  const Register scratch2 = r9;
2640 
2641  Label call_runtime, done, int_exponent;
2642 
2643  // Detect integer exponents stored as double.
2644  __ TryDoubleToInt32Exact(scratch, double_exponent, scratch2, double_scratch);
2645  __ beq(&int_exponent, Label::kNear);
2646 
2647  __ push(r14);
2648  {
2649  AllowExternalCallThatCantCauseGC scope(masm);
2650  __ PrepareCallCFunction(0, 2, scratch);
2651  __ MovToFloatParameters(double_base, double_exponent);
2652  __ CallCFunction(ExternalReference::power_double_double_function(), 0, 2);
2653  }
2654  __ pop(r14);
2655  __ MovFromFloatResult(double_result);
2656  __ b(&done);
2657 
2658  // Calculate power with integer exponent.
2659  __ bind(&int_exponent);
2660 
2661  // Get two copies of exponent in the registers scratch and exponent.
2662  // Exponent has previously been stored into scratch as untagged integer.
2663  __ LoadRR(exponent, scratch);
2664 
2665  __ ldr(double_scratch, double_base); // Back up base.
2666  __ LoadImmP(scratch2, Operand(1));
2667  __ ConvertIntToDouble(double_result, scratch2);
2668 
2669  // Get absolute value of exponent.
2670  Label positive_exponent;
2671  __ CmpP(scratch, Operand::Zero());
2672  __ bge(&positive_exponent, Label::kNear);
2673  __ LoadComplementRR(scratch, scratch);
2674  __ bind(&positive_exponent);
2675 
2676  Label while_true, no_carry, loop_end;
2677  __ bind(&while_true);
2678  __ mov(scratch2, Operand(1));
2679  __ AndP(scratch2, scratch);
2680  __ beq(&no_carry, Label::kNear);
2681  __ mdbr(double_result, double_scratch);
2682  __ bind(&no_carry);
2683  __ ShiftRightP(scratch, scratch, Operand(1));
2684  __ LoadAndTestP(scratch, scratch);
2685  __ beq(&loop_end, Label::kNear);
2686  __ mdbr(double_scratch, double_scratch);
2687  __ b(&while_true);
2688  __ bind(&loop_end);
2689 
2690  __ CmpP(exponent, Operand::Zero());
2691  __ bge(&done);
2692 
2693  // get 1/double_result:
2694  __ ldr(double_scratch, double_result);
2695  __ LoadImmP(scratch2, Operand(1));
2696  __ ConvertIntToDouble(double_result, scratch2);
2697  __ ddbr(double_result, double_scratch);
2698 
2699  // Test whether result is zero. Bail out to check for subnormal result.
2700  // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
2701  __ lzdr(kDoubleRegZero);
2702  __ cdbr(double_result, kDoubleRegZero);
2703  __ bne(&done, Label::kNear);
2704  // double_exponent may not containe the exponent value if the input was a
2705  // smi. We set it with exponent value before bailing out.
2706  __ ConvertIntToDouble(double_exponent, exponent);
2707 
2708  // Returning or bailing out.
2709  __ push(r14);
2710  {
2711  AllowExternalCallThatCantCauseGC scope(masm);
2712  __ PrepareCallCFunction(0, 2, scratch);
2713  __ MovToFloatParameters(double_base, double_exponent);
2714  __ CallCFunction(ExternalReference::power_double_double_function(), 0, 2);
2715  }
2716  __ pop(r14);
2717  __ MovFromFloatResult(double_result);
2718 
2719  __ bind(&done);
2720  __ Ret();
2721 }
2722 
2723 namespace {
2724 
2725 void GenerateInternalArrayConstructorCase(MacroAssembler* masm,
2726  ElementsKind kind) {
2727  // Load undefined into the allocation site parameter as required by
2728  // ArrayNArgumentsConstructor.
2729  __ LoadRoot(kJavaScriptCallExtraArg1Register, RootIndex::kUndefinedValue);
2730 
2731  __ CmpLogicalP(r2, Operand(1));
2732 
2733  __ Jump(CodeFactory::InternalArrayNoArgumentConstructor(masm->isolate(), kind)
2734  .code(),
2735  RelocInfo::CODE_TARGET, lt);
2736 
2737  __ Jump(BUILTIN_CODE(masm->isolate(), ArrayNArgumentsConstructor),
2738  RelocInfo::CODE_TARGET, gt);
2739 
2740  if (IsFastPackedElementsKind(kind)) {
2741  // We might need to create a holey array
2742  // look at the first argument
2743  __ LoadP(r5, MemOperand(sp, 0));
2744  __ CmpP(r5, Operand::Zero());
2745 
2746  __ Jump(CodeFactory::InternalArraySingleArgumentConstructor(
2747  masm->isolate(), GetHoleyElementsKind(kind))
2748  .code(),
2749  RelocInfo::CODE_TARGET, ne);
2750  }
2751 
2752  __ Jump(
2753  CodeFactory::InternalArraySingleArgumentConstructor(masm->isolate(), kind)
2754  .code(),
2755  RelocInfo::CODE_TARGET);
2756 }
2757 
2758 } // namespace
2759 
2760 void Builtins::Generate_InternalArrayConstructorImpl(MacroAssembler* masm) {
2761  // ----------- S t a t e -------------
2762  // -- r2 : argc
2763  // -- r3 : constructor
2764  // -- sp[0] : return address
2765  // -- sp[4] : last argument
2766  // -----------------------------------
2767 
2768  if (FLAG_debug_code) {
2769  // The array construct code is only set for the global and natives
2770  // builtin Array functions which always have maps.
2771 
2772  // Initial map for the builtin Array function should be a map.
2773  __ LoadP(r5, FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
2774  // Will both indicate a nullptr and a Smi.
2775  __ TestIfSmi(r5);
2776  __ Assert(ne, AbortReason::kUnexpectedInitialMapForArrayFunction, cr0);
2777  __ CompareObjectType(r5, r5, r6, MAP_TYPE);
2778  __ Assert(eq, AbortReason::kUnexpectedInitialMapForArrayFunction);
2779  }
2780 
2781  // Figure out the right elements kind
2782  __ LoadP(r5, FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
2783  // Load the map's "bit field 2" into |result|.
2784  __ LoadlB(r5, FieldMemOperand(r5, Map::kBitField2Offset));
2785  // Retrieve elements_kind from bit field 2.
2786  __ DecodeField<Map::ElementsKindBits>(r5);
2787 
2788  if (FLAG_debug_code) {
2789  Label done;
2790  __ CmpP(r5, Operand(PACKED_ELEMENTS));
2791  __ beq(&done);
2792  __ CmpP(r5, Operand(HOLEY_ELEMENTS));
2793  __ Assert(
2794  eq,
2795  AbortReason::kInvalidElementsKindForInternalArrayOrInternalPackedArray);
2796  __ bind(&done);
2797  }
2798 
2799  Label fast_elements_case;
2800  __ CmpP(r5, Operand(PACKED_ELEMENTS));
2801  __ beq(&fast_elements_case);
2802  GenerateInternalArrayConstructorCase(masm, HOLEY_ELEMENTS);
2803 
2804  __ bind(&fast_elements_case);
2805  GenerateInternalArrayConstructorCase(masm, PACKED_ELEMENTS);
2806 }
2807 
2808 #undef __
2809 
2810 } // namespace internal
2811 } // namespace v8
2812 
2813 #endif // V8_TARGET_ARCH_S390
Definition: libplatform.h:13