1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #include "src/code-factory.h"
8 #include "src/codegen.h"
9 #include "src/deoptimizer.h"
10 #include "src/full-codegen/full-codegen.h"
16 #define __ ACCESS_MASM(masm)
19 void Builtins::Generate_Adaptor(MacroAssembler* masm,
21 BuiltinExtraArguments extra_args) {
22 // ----------- S t a t e -------------
23 // -- rax : number of arguments excluding receiver
24 // -- rdi : called function (only guaranteed when
25 // extra_args requires it)
27 // -- rsp[0] : return address
28 // -- rsp[8] : last argument
30 // -- rsp[8 * argc] : first argument (argc == rax)
31 // -- rsp[8 * (argc + 1)] : receiver
32 // -----------------------------------
34 // Insert extra arguments.
35 int num_extra_args = 0;
36 if (extra_args == NEEDS_CALLED_FUNCTION) {
38 __ PopReturnAddressTo(kScratchRegister);
40 __ PushReturnAddressFrom(kScratchRegister);
42 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
45 // JumpToExternalReference expects rax to contain the number of arguments
46 // including the receiver and the extra arguments.
47 __ addp(rax, Immediate(num_extra_args + 1));
48 __ JumpToExternalReference(ExternalReference(id, masm->isolate()), 1);
52 static void CallRuntimePassFunction(
53 MacroAssembler* masm, Runtime::FunctionId function_id) {
54 FrameScope scope(masm, StackFrame::INTERNAL);
55 // Push a copy of the function onto the stack.
57 // Function is also the parameter to the runtime call.
60 __ CallRuntime(function_id, 1);
66 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
67 __ movp(kScratchRegister,
68 FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
69 __ movp(kScratchRegister,
70 FieldOperand(kScratchRegister, SharedFunctionInfo::kCodeOffset));
71 __ leap(kScratchRegister, FieldOperand(kScratchRegister, Code::kHeaderSize));
72 __ jmp(kScratchRegister);
76 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
77 __ leap(rax, FieldOperand(rax, Code::kHeaderSize));
82 void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
83 // Checking whether the queued function is ready for install is optional,
84 // since we come across interrupts and stack checks elsewhere. However,
85 // not checking may delay installing ready functions, and always checking
86 // would be quite expensive. A good compromise is to first check against
87 // stack limit as a cue for an interrupt signal.
89 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
90 __ j(above_equal, &ok);
92 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
93 GenerateTailCallToReturnedCode(masm);
96 GenerateTailCallToSharedCode(masm);
100 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
101 bool is_api_function) {
102 // ----------- S t a t e -------------
103 // -- rax: number of arguments
104 // -- rdi: constructor function
105 // -- rbx: allocation site or undefined
106 // -- rdx: original constructor
107 // -----------------------------------
109 // Enter a construct frame.
111 FrameScope scope(masm, StackFrame::CONSTRUCT);
113 // Preserve the incoming parameters on the stack.
114 __ AssertUndefinedOrAllocationSite(rbx);
116 __ Integer32ToSmi(rax, rax);
121 // Try to allocate the object without transitioning into C code. If any of
122 // the preconditions is not met, the code bails out to the runtime call.
123 Label rt_call, allocated;
124 if (FLAG_inline_new) {
125 ExternalReference debug_step_in_fp =
126 ExternalReference::debug_step_in_fp_address(masm->isolate());
127 __ Move(kScratchRegister, debug_step_in_fp);
128 __ cmpp(Operand(kScratchRegister, 0), Immediate(0));
129 __ j(not_equal, &rt_call);
131 // Fall back to runtime if the original constructor and function differ.
133 __ j(not_equal, &rt_call);
135 // Verified that the constructor is a JSFunction.
136 // Load the initial map and verify that it is in fact a map.
138 __ movp(rax, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
139 // Will both indicate a NULL and a Smi
140 DCHECK(kSmiTag == 0);
141 __ JumpIfSmi(rax, &rt_call);
143 // rax: initial map (if proven valid below)
144 __ CmpObjectType(rax, MAP_TYPE, rbx);
145 __ j(not_equal, &rt_call);
147 // Check that the constructor is not constructing a JSFunction (see
148 // comments in Runtime_NewObject in runtime.cc). In which case the
149 // initial map's instance type would be JS_FUNCTION_TYPE.
152 __ CmpInstanceType(rax, JS_FUNCTION_TYPE);
153 __ j(equal, &rt_call);
154 if (!is_api_function) {
156 // The code below relies on these assumptions.
157 STATIC_ASSERT(Map::Counter::kShift + Map::Counter::kSize == 32);
158 // Check if slack tracking is enabled.
159 __ movl(rsi, FieldOperand(rax, Map::kBitField3Offset));
160 __ shrl(rsi, Immediate(Map::Counter::kShift));
161 __ cmpl(rsi, Immediate(Map::kSlackTrackingCounterEnd));
162 __ j(less, &allocate);
163 // Decrease generous allocation count.
164 __ subl(FieldOperand(rax, Map::kBitField3Offset),
165 Immediate(1 << Map::Counter::kShift));
167 __ cmpl(rsi, Immediate(Map::kSlackTrackingCounterEnd));
168 __ j(not_equal, &allocate);
174 __ Push(rdi); // constructor
175 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
180 __ movl(rsi, Immediate(Map::kSlackTrackingCounterEnd - 1));
185 // Now allocate the JSObject on the heap.
186 __ movzxbp(rdi, FieldOperand(rax, Map::kInstanceSizeOffset));
187 __ shlp(rdi, Immediate(kPointerSizeLog2));
188 // rdi: size of new object
194 NO_ALLOCATION_FLAGS);
195 // Allocated the JSObject, now initialize the fields.
197 // rbx: JSObject (not HeapObject tagged - the actual address).
198 // rdi: start of next object
199 __ movp(Operand(rbx, JSObject::kMapOffset), rax);
200 __ LoadRoot(rcx, Heap::kEmptyFixedArrayRootIndex);
201 __ movp(Operand(rbx, JSObject::kPropertiesOffset), rcx);
202 __ movp(Operand(rbx, JSObject::kElementsOffset), rcx);
203 // Set extra fields in the newly allocated object.
206 // rdi: start of next object
207 // rsi: slack tracking counter (non-API function case)
208 __ leap(rcx, Operand(rbx, JSObject::kHeaderSize));
209 __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
210 if (!is_api_function) {
211 Label no_inobject_slack_tracking;
213 // Check if slack tracking is enabled.
214 __ cmpl(rsi, Immediate(Map::kSlackTrackingCounterEnd));
215 __ j(less, &no_inobject_slack_tracking);
217 // Allocate object with a slack.
221 rax, Map::kInObjectPropertiesOrConstructorFunctionIndexOffset));
222 __ movzxbp(rax, FieldOperand(rax, Map::kUnusedPropertyFieldsOffset));
225 Operand(rbx, rsi, times_pointer_size, JSObject::kHeaderSize));
226 // rsi: offset of first field after pre-allocated fields
227 if (FLAG_debug_code) {
229 __ Assert(less_equal,
230 kUnexpectedNumberOfPreAllocatedPropertyFields);
232 __ InitializeFieldsWithFiller(rcx, rsi, rdx);
233 __ LoadRoot(rdx, Heap::kOnePointerFillerMapRootIndex);
234 // Fill the remaining fields with one pointer filler map.
236 __ bind(&no_inobject_slack_tracking);
239 __ InitializeFieldsWithFiller(rcx, rdi, rdx);
241 // Add the object tag to make the JSObject real, so that we can continue
242 // and jump into the continuation code at any time from now on.
243 // rbx: JSObject (untagged)
244 __ orp(rbx, Immediate(kHeapObjectTag));
246 // Continue with JSObject being successfully allocated
247 // rbx: JSObject (tagged)
251 // Allocate the new receiver object using the runtime call.
252 // rdx: original constructor
254 int offset = kPointerSize;
256 // Must restore rsi (context) and rdi (constructor) before calling runtime.
257 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
258 __ movp(rdi, Operand(rsp, offset));
259 __ Push(rdi); // argument 2/1: constructor function
260 __ Push(rdx); // argument 3/2: original constructor
261 __ CallRuntime(Runtime::kNewObject, 2);
262 __ movp(rbx, rax); // store result in rbx
264 // New object allocated.
265 // rbx: newly allocated object
268 // Restore the parameters.
272 // Retrieve smi-tagged arguments count from the stack.
273 __ movp(rax, Operand(rsp, 0));
274 __ SmiToInteger32(rax, rax);
276 // Push new.target onto the construct frame. This is stored just below the
277 // receiver on the stack.
280 // Push the allocated receiver to the stack. We need two copies
281 // because we may have to return the original one and the calling
282 // conventions dictate that the called function pops the receiver.
286 // Set up pointer to last argument.
287 __ leap(rbx, Operand(rbp, StandardFrameConstants::kCallerSPOffset));
289 // Copy arguments and receiver to the expression stack.
294 __ Push(Operand(rbx, rcx, times_pointer_size, 0));
297 __ j(greater_equal, &loop);
299 // Call the function.
300 if (is_api_function) {
301 __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
303 masm->isolate()->builtins()->HandleApiCallConstruct();
304 __ Call(code, RelocInfo::CODE_TARGET);
306 ParameterCount actual(rax);
307 __ InvokeFunction(rdi, actual, CALL_FUNCTION, NullCallWrapper());
310 // Store offset of return address for deoptimizer.
311 if (!is_api_function) {
312 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
315 // Restore context from the frame.
316 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
318 // If the result is an object (in the ECMA sense), we should get rid
319 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
321 Label use_receiver, exit;
322 // If the result is a smi, it is *not* an object in the ECMA sense.
323 __ JumpIfSmi(rax, &use_receiver);
325 // If the type of the result (stored in its map) is less than
326 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
327 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
328 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
329 __ j(above_equal, &exit);
331 // Throw away the result of the constructor invocation and use the
332 // on-stack receiver as the result.
333 __ bind(&use_receiver);
334 __ movp(rax, Operand(rsp, 0));
336 // Restore the arguments count and leave the construct frame. The arguments
337 // count is stored below the reciever and the new.target.
339 __ movp(rbx, Operand(rsp, 2 * kPointerSize));
341 // Leave construct frame.
344 // Remove caller arguments from the stack and return.
345 __ PopReturnAddressTo(rcx);
346 SmiIndex index = masm->SmiToIndex(rbx, rbx, kPointerSizeLog2);
347 __ leap(rsp, Operand(rsp, index.reg, index.scale, 1 * kPointerSize));
348 __ PushReturnAddressFrom(rcx);
349 Counters* counters = masm->isolate()->counters();
350 __ IncrementCounter(counters->constructed_objects(), 1);
355 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
356 Generate_JSConstructStubHelper(masm, false);
360 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
361 Generate_JSConstructStubHelper(masm, true);
365 void Builtins::Generate_JSConstructStubForDerived(MacroAssembler* masm) {
366 // ----------- S t a t e -------------
367 // -- rax: number of arguments
368 // -- rdi: constructor function
369 // -- rbx: allocation site or undefined
370 // -- rdx: original constructor
371 // -----------------------------------
374 FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
376 // Preserve allocation site.
377 __ AssertUndefinedOrAllocationSite(rbx);
380 // Store a smi-tagged arguments count on the stack.
381 __ Integer32ToSmi(rax, rax);
383 __ SmiToInteger32(rax, rax);
388 // receiver is the hole.
389 __ Push(masm->isolate()->factory()->the_hole_value());
391 // Set up pointer to last argument.
392 __ leap(rbx, Operand(rbp, StandardFrameConstants::kCallerSPOffset));
394 // Copy arguments and receiver to the expression stack.
399 __ Push(Operand(rbx, rcx, times_pointer_size, 0));
402 __ j(greater_equal, &loop);
406 ExternalReference debug_step_in_fp =
407 ExternalReference::debug_step_in_fp_address(masm->isolate());
408 __ Move(kScratchRegister, debug_step_in_fp);
409 __ cmpp(Operand(kScratchRegister, 0), Immediate(0));
410 __ j(equal, &skip_step_in);
415 __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
419 __ bind(&skip_step_in);
421 // Call the function.
422 ParameterCount actual(rax);
423 __ InvokeFunction(rdi, actual, CALL_FUNCTION, NullCallWrapper());
425 // Restore context from the frame.
426 __ movp(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
428 // Get arguments count, skipping over new.target.
429 __ movp(rbx, Operand(rsp, kPointerSize)); // Get arguments count.
430 } // Leave construct frame.
432 // Remove caller arguments from the stack and return.
433 __ PopReturnAddressTo(rcx);
434 SmiIndex index = masm->SmiToIndex(rbx, rbx, kPointerSizeLog2);
435 __ leap(rsp, Operand(rsp, index.reg, index.scale, 1 * kPointerSize));
436 __ PushReturnAddressFrom(rcx);
441 enum IsTagged { kRaxIsSmiTagged, kRaxIsUntaggedInt };
444 // Clobbers rcx, rdx, kScratchRegister; preserves all other registers.
445 static void Generate_CheckStackOverflow(MacroAssembler* masm,
446 const int calleeOffset,
447 IsTagged rax_is_tagged) {
448 // rax : the number of items to be pushed to the stack
450 // Check the stack for overflow. We are not trying to catch
451 // interruptions (e.g. debug break and preemption) here, so the "real stack
452 // limit" is checked.
454 __ LoadRoot(kScratchRegister, Heap::kRealStackLimitRootIndex);
456 // Make rcx the space we have left. The stack might already be overflowed
457 // here which will cause rcx to become negative.
458 __ subp(rcx, kScratchRegister);
459 // Make rdx the space we need for the array when it is unrolled onto the
461 if (rax_is_tagged == kRaxIsSmiTagged) {
462 __ PositiveSmiTimesPowerOfTwoToInteger64(rdx, rax, kPointerSizeLog2);
464 DCHECK(rax_is_tagged == kRaxIsUntaggedInt);
466 __ shlq(rdx, Immediate(kPointerSizeLog2));
468 // Check if the arguments will overflow the stack.
470 __ j(greater, &okay); // Signed comparison.
472 // Out of stack space.
473 __ Push(Operand(rbp, calleeOffset));
474 if (rax_is_tagged == kRaxIsUntaggedInt) {
475 __ Integer32ToSmi(rax, rax);
478 __ CallRuntime(Runtime::kThrowStackOverflow, 0);
484 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
486 ProfileEntryHookStub::MaybeCallEntryHook(masm);
488 // Expects five C++ function parameters.
489 // - Address entry (ignored)
490 // - JSFunction* function (
491 // - Object* receiver
494 // (see Handle::Invoke in execution.cc).
496 // Open a C++ scope for the FrameScope.
498 // Platform specific argument handling. After this, the stack contains
499 // an internal frame and the pushed function and receiver, and
500 // register rax and rbx holds the argument count and argument array,
501 // while rdi holds the function pointer and rsi the context.
504 // MSVC parameters in:
505 // rcx : entry (ignored)
511 // Clear the context before we push it when entering the internal frame.
513 // Enter an internal frame.
514 FrameScope scope(masm, StackFrame::INTERNAL);
516 // Load the function context into rsi.
517 __ movp(rsi, FieldOperand(rdx, JSFunction::kContextOffset));
519 // Push the function and the receiver onto the stack.
523 // Load the number of arguments and setup pointer to the arguments.
525 // Load the previous frame pointer to access C argument on stack
526 __ movp(kScratchRegister, Operand(rbp, 0));
527 __ movp(rbx, Operand(kScratchRegister, EntryFrameConstants::kArgvOffset));
528 // Load the function pointer into rdi.
531 // GCC parameters in:
532 // rdi : entry (ignored)
541 // Clear the context before we push it when entering the internal frame.
543 // Enter an internal frame.
544 FrameScope scope(masm, StackFrame::INTERNAL);
546 // Push the function and receiver and setup the context.
549 __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
551 // Load the number of arguments and setup pointer to the arguments.
556 // Current stack contents:
557 // [rsp + 2 * kPointerSize ... ] : Internal frame
558 // [rsp + kPointerSize] : function
560 // Current register contents:
566 // Check if we have enough stack space to push all arguments.
567 // The function is the first thing that was pushed above after entering
568 // the internal frame.
569 const int kFunctionOffset =
570 InternalFrameConstants::kCodeOffset - kRegisterSize;
571 // Expects argument count in rax. Clobbers rcx, rdx.
572 Generate_CheckStackOverflow(masm, kFunctionOffset, kRaxIsUntaggedInt);
574 // Copy arguments to the stack in a loop.
575 // Register rbx points to array of pointers to handle locations.
576 // Push the values of these handles.
578 __ Set(rcx, 0); // Set loop variable to 0.
581 __ movp(kScratchRegister, Operand(rbx, rcx, times_pointer_size, 0));
582 __ Push(Operand(kScratchRegister, 0)); // dereference handle
583 __ addp(rcx, Immediate(1));
586 __ j(not_equal, &loop);
590 // No type feedback cell is available
591 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
592 // Expects rdi to hold function pointer.
593 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
596 __ Call(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
598 // Exit the internal frame. Notice that this also removes the empty
599 // context and the function left on the stack by the code
603 // TODO(X64): Is argument correct? Is there a receiver to remove?
604 __ ret(1 * kPointerSize); // Remove receiver.
608 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
609 Generate_JSEntryTrampolineHelper(masm, false);
613 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
614 Generate_JSEntryTrampolineHelper(masm, true);
618 // Generate code for entering a JS function with the interpreter.
619 // On entry to the function the receiver and arguments have been pushed on the
620 // stack left to right. The actual argument count matches the formal parameter
621 // count expected by the function.
623 // The live registers are:
624 // o rdi: the JS function object being called
625 // o rsi: our context
626 // o rbp: the caller's frame pointer
627 // o rsp: stack pointer (pointing to return address)
629 // The function builds a JS frame. Please see JavaScriptFrameConstants in
630 // frames-x64.h for its layout.
631 // TODO(rmcilroy): We will need to include the current bytecode pointer in the
633 void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
634 // Open a frame scope to indicate that there is a frame on the stack. The
635 // MANUAL indicates that the scope shouldn't actually generate code to set up
636 // the frame (that is done below).
637 FrameScope frame_scope(masm, StackFrame::MANUAL);
638 __ pushq(rbp); // Caller's frame pointer.
640 __ Push(rsi); // Callee's context.
641 __ Push(rdi); // Callee's JS function.
643 // Get the bytecode array from the function object and load the pointer to the
644 // first entry into edi (InterpreterBytecodeRegister).
645 __ movp(rax, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
646 __ movp(kInterpreterBytecodeArrayRegister,
647 FieldOperand(rax, SharedFunctionInfo::kFunctionDataOffset));
649 if (FLAG_debug_code) {
650 // Check function data field is actually a BytecodeArray object.
651 __ AssertNotSmi(kInterpreterBytecodeArrayRegister);
652 __ CmpObjectType(kInterpreterBytecodeArrayRegister, BYTECODE_ARRAY_TYPE,
654 __ Assert(equal, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
657 // Allocate the local and temporary register file on the stack.
659 // Load frame size from the BytecodeArray object.
660 __ movl(rcx, FieldOperand(kInterpreterBytecodeArrayRegister,
661 BytecodeArray::kFrameSizeOffset));
663 // Do a stack check to ensure we don't go over the limit.
667 __ CompareRoot(rdx, Heap::kRealStackLimitRootIndex);
668 __ j(above_equal, &ok, Label::kNear);
669 __ CallRuntime(Runtime::kThrowStackOverflow, 0);
672 // If ok, push undefined as the initial value for all register file entries.
675 __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);
676 __ j(always, &loop_check);
677 __ bind(&loop_header);
678 // TODO(rmcilroy): Consider doing more than one push per loop iteration.
680 // Continue loop if not done.
681 __ bind(&loop_check);
682 __ subp(rcx, Immediate(kPointerSize));
683 __ j(greater_equal, &loop_header, Label::kNear);
686 // TODO(rmcilroy): List of things not currently dealt with here but done in
687 // fullcodegen's prologue:
688 // - Support profiler (specifically profiling_counter).
689 // - Call ProfileEntryHookStub when isolate has a function_entry_hook.
690 // - Allow simulator stop operations if FLAG_stop_at is set.
691 // - Deal with sloppy mode functions which need to replace the
692 // receiver with the global proxy when called as functions (without an
693 // explicit receiver object).
694 // - Code aging of the BytecodeArray object.
695 // - Supporting FLAG_trace.
697 // The following items are also not done here, and will probably be done using
698 // explicit bytecodes instead:
699 // - Allocating a new local context if applicable.
700 // - Setting up a local binding to the this function, which is used in
701 // derived constructors with super calls.
702 // - Setting new.target if required.
703 // - Dealing with REST parameters (only if
704 // https://codereview.chromium.org/1235153006 doesn't land by then).
705 // - Dealing with argument objects.
707 // Perform stack guard check.
710 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
711 __ j(above_equal, &ok, Label::kNear);
712 __ CallRuntime(Runtime::kStackGuard, 0);
716 // Load accumulator, register file, bytecode offset, dispatch table into
718 __ LoadRoot(kInterpreterAccumulatorRegister, Heap::kUndefinedValueRootIndex);
719 __ movp(kInterpreterRegisterFileRegister, rbp);
721 kInterpreterRegisterFileRegister,
722 Immediate(kPointerSize + StandardFrameConstants::kFixedFrameSizeFromFp));
723 __ movp(kInterpreterBytecodeOffsetRegister,
724 Immediate(BytecodeArray::kHeaderSize - kHeapObjectTag));
725 __ LoadRoot(kInterpreterDispatchTableRegister,
726 Heap::kInterpreterTableRootIndex);
727 __ addp(kInterpreterDispatchTableRegister,
728 Immediate(FixedArray::kHeaderSize - kHeapObjectTag));
730 // Dispatch to the first bytecode handler for the function.
731 __ movzxbp(rbx, Operand(kInterpreterBytecodeArrayRegister,
732 kInterpreterBytecodeOffsetRegister, times_1, 0));
733 __ movp(rbx, Operand(kInterpreterDispatchTableRegister, rbx,
734 times_pointer_size, 0));
735 // TODO(rmcilroy): Make dispatch table point to code entrys to avoid untagging
736 // and header removal.
737 __ addp(rbx, Immediate(Code::kHeaderSize - kHeapObjectTag));
742 void Builtins::Generate_InterpreterExitTrampoline(MacroAssembler* masm) {
743 // TODO(rmcilroy): List of things not currently dealt with here but done in
744 // fullcodegen's EmitReturnSequence.
745 // - Supporting FLAG_trace for Runtime::TraceExit.
746 // - Support profiler (specifically decrementing profiling_counter
747 // appropriately and calling out to HandleInterrupts if necessary).
749 // The return value is in accumulator, which is already in rax.
751 // Leave the frame (also dropping the register file).
754 // Drop receiver + arguments and return.
755 __ movl(rbx, FieldOperand(kInterpreterBytecodeArrayRegister,
756 BytecodeArray::kParameterSizeOffset));
757 __ PopReturnAddressTo(rcx);
759 __ PushReturnAddressFrom(rcx);
764 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
765 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
766 GenerateTailCallToReturnedCode(masm);
770 static void CallCompileOptimized(MacroAssembler* masm,
772 FrameScope scope(masm, StackFrame::INTERNAL);
773 // Push a copy of the function onto the stack.
775 // Function is also the parameter to the runtime call.
777 // Whether to compile in a background thread.
778 __ Push(masm->isolate()->factory()->ToBoolean(concurrent));
780 __ CallRuntime(Runtime::kCompileOptimized, 2);
786 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
787 CallCompileOptimized(masm, false);
788 GenerateTailCallToReturnedCode(masm);
792 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
793 CallCompileOptimized(masm, true);
794 GenerateTailCallToReturnedCode(masm);
798 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
799 // For now, we are relying on the fact that make_code_young doesn't do any
800 // garbage collection which allows us to save/restore the registers without
801 // worrying about which of them contain pointers. We also don't build an
802 // internal frame to make the code faster, since we shouldn't have to do stack
803 // crawls in MakeCodeYoung. This seems a bit fragile.
805 // Re-execute the code that was patched back to the young age when
807 __ subp(Operand(rsp, 0), Immediate(5));
809 __ Move(arg_reg_2, ExternalReference::isolate_address(masm->isolate()));
810 __ movp(arg_reg_1, Operand(rsp, kNumSafepointRegisters * kPointerSize));
812 FrameScope scope(masm, StackFrame::MANUAL);
813 __ PrepareCallCFunction(2);
815 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
822 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
823 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
824 MacroAssembler* masm) { \
825 GenerateMakeCodeYoungAgainCommon(masm); \
827 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
828 MacroAssembler* masm) { \
829 GenerateMakeCodeYoungAgainCommon(masm); \
831 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
832 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
835 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
836 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
837 // that make_code_young doesn't do any garbage collection which allows us to
838 // save/restore the registers without worrying about which of them contain
841 __ Move(arg_reg_2, ExternalReference::isolate_address(masm->isolate()));
842 __ movp(arg_reg_1, Operand(rsp, kNumSafepointRegisters * kPointerSize));
843 __ subp(arg_reg_1, Immediate(Assembler::kShortCallInstructionLength));
845 FrameScope scope(masm, StackFrame::MANUAL);
846 __ PrepareCallCFunction(2);
848 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
853 // Perform prologue operations usually performed by the young code stub.
854 __ PopReturnAddressTo(kScratchRegister);
855 __ pushq(rbp); // Caller's frame pointer.
857 __ Push(rsi); // Callee's context.
858 __ Push(rdi); // Callee's JS Function.
859 __ PushReturnAddressFrom(kScratchRegister);
861 // Jump to point after the code-age stub.
866 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
867 GenerateMakeCodeYoungAgainCommon(masm);
871 void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
872 Generate_MarkCodeAsExecutedOnce(masm);
876 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
877 SaveFPRegsMode save_doubles) {
878 // Enter an internal frame.
880 FrameScope scope(masm, StackFrame::INTERNAL);
882 // Preserve registers across notification, this is important for compiled
883 // stubs that tail call the runtime on deopts passing their parameters in
886 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
888 // Tear down internal frame.
891 __ DropUnderReturnAddress(1); // Ignore state offset
892 __ ret(0); // Return to IC Miss stub, continuation still on stack.
896 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
897 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
901 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
902 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
906 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
907 Deoptimizer::BailoutType type) {
908 // Enter an internal frame.
910 FrameScope scope(masm, StackFrame::INTERNAL);
912 // Pass the deoptimization type to the runtime system.
913 __ Push(Smi::FromInt(static_cast<int>(type)));
915 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
916 // Tear down internal frame.
919 // Get the full codegen state from the stack and untag it.
920 __ SmiToInteger32(kScratchRegister, Operand(rsp, kPCOnStackSize));
922 // Switch on the state.
923 Label not_no_registers, not_tos_rax;
924 __ cmpp(kScratchRegister, Immediate(FullCodeGenerator::NO_REGISTERS));
925 __ j(not_equal, ¬_no_registers, Label::kNear);
926 __ ret(1 * kPointerSize); // Remove state.
928 __ bind(¬_no_registers);
929 __ movp(rax, Operand(rsp, kPCOnStackSize + kPointerSize));
930 __ cmpp(kScratchRegister, Immediate(FullCodeGenerator::TOS_REG));
931 __ j(not_equal, ¬_tos_rax, Label::kNear);
932 __ ret(2 * kPointerSize); // Remove state, rax.
934 __ bind(¬_tos_rax);
935 __ Abort(kNoCasesLeft);
939 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
940 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
944 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
945 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
949 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
950 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
955 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
957 // rsp[0] : Return address
958 // rsp[8] : Argument n
959 // rsp[16] : Argument n-1
961 // rsp[8 * n] : Argument 1
962 // rsp[8 * (n + 1)] : Receiver (callable to call)
964 // rax contains the number of arguments, n, not counting the receiver.
966 // 1. Make sure we have at least one argument.
970 __ j(not_zero, &done, Label::kNear);
971 __ PopReturnAddressTo(rbx);
972 __ PushRoot(Heap::kUndefinedValueRootIndex);
973 __ PushReturnAddressFrom(rbx);
978 // 2. Get the callable to call (passed as receiver) from the stack.
980 StackArgumentsAccessor args(rsp, rax);
981 __ movp(rdi, args.GetReceiverOperand());
984 // 3. Shift arguments and return address one slot down on the stack
985 // (overwriting the original receiver). Adjust argument count to make
986 // the original first argument the new receiver.
990 StackArgumentsAccessor args(rsp, rcx);
992 __ movp(rbx, args.GetArgumentOperand(1));
993 __ movp(args.GetArgumentOperand(0), rbx);
995 __ j(not_zero, &loop); // While non-zero.
996 __ DropUnderReturnAddress(1, rbx); // Drop one slot under return address.
997 __ decp(rax); // One fewer argument (first argument is new receiver).
1000 // 4. Call the callable.
1001 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1005 static void Generate_PushAppliedArguments(MacroAssembler* masm,
1006 const int vectorOffset,
1007 const int argumentsOffset,
1008 const int indexOffset,
1009 const int limitOffset) {
1010 Register receiver = LoadDescriptor::ReceiverRegister();
1011 Register key = LoadDescriptor::NameRegister();
1012 Register slot = LoadDescriptor::SlotRegister();
1013 Register vector = LoadWithVectorDescriptor::VectorRegister();
1015 // Copy all arguments from the array to the stack.
1017 __ movp(key, Operand(rbp, indexOffset));
1020 __ movp(receiver, Operand(rbp, argumentsOffset)); // load arguments
1022 // Use inline caching to speed up access to arguments.
1023 int slot_index = TypeFeedbackVector::PushAppliedArgumentsIndex();
1024 __ Move(slot, Smi::FromInt(slot_index));
1025 __ movp(vector, Operand(rbp, vectorOffset));
1027 KeyedLoadICStub(masm->isolate(), LoadICState(kNoExtraICState)).GetCode();
1028 __ Call(ic, RelocInfo::CODE_TARGET);
1029 // It is important that we do not have a test instruction after the
1030 // call. A test instruction after the call is used to indicate that
1031 // we have generated an inline version of the keyed load. In this
1032 // case, we know that we are not generating a test instruction next.
1034 // Push the nth argument.
1037 // Update the index on the stack and in register key.
1038 __ movp(key, Operand(rbp, indexOffset));
1039 __ SmiAddConstant(key, key, Smi::FromInt(1));
1040 __ movp(Operand(rbp, indexOffset), key);
1043 __ cmpp(key, Operand(rbp, limitOffset));
1044 __ j(not_equal, &loop);
1046 // On exit, the pushed arguments count is in rax, untagged
1047 __ SmiToInteger64(rax, key);
1051 // Used by FunctionApply and ReflectApply
1052 static void Generate_ApplyHelper(MacroAssembler* masm, bool targetIsArgument) {
1053 const int kFormalParameters = targetIsArgument ? 3 : 2;
1054 const int kStackSize = kFormalParameters + 1;
1057 // rsp : return address
1058 // rsp[8] : arguments
1059 // rsp[16] : receiver ("this")
1060 // rsp[24] : function
1062 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1064 // rbp : Old base pointer
1065 // rbp[8] : return address
1066 // rbp[16] : function arguments
1067 // rbp[24] : receiver
1068 // rbp[32] : function
1069 static const int kArgumentsOffset = kFPOnStackSize + kPCOnStackSize;
1070 static const int kReceiverOffset = kArgumentsOffset + kPointerSize;
1071 static const int kFunctionOffset = kReceiverOffset + kPointerSize;
1072 static const int kVectorOffset =
1073 InternalFrameConstants::kCodeOffset - 1 * kPointerSize;
1076 __ movp(rdi, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1077 __ movp(rdi, FieldOperand(rdi, SharedFunctionInfo::kFeedbackVectorOffset));
1080 __ Push(Operand(rbp, kFunctionOffset));
1081 __ Push(Operand(rbp, kArgumentsOffset));
1082 if (targetIsArgument) {
1083 __ InvokeBuiltin(Context::REFLECT_APPLY_PREPARE_BUILTIN_INDEX,
1086 __ InvokeBuiltin(Context::APPLY_PREPARE_BUILTIN_INDEX, CALL_FUNCTION);
1089 Generate_CheckStackOverflow(masm, kFunctionOffset, kRaxIsSmiTagged);
1091 // Push current index and limit, and receiver.
1092 const int kLimitOffset = kVectorOffset - 1 * kPointerSize;
1093 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
1094 __ Push(rax); // limit
1095 __ Push(Immediate(0)); // index
1096 __ Push(Operand(rbp, kReceiverOffset)); // receiver
1098 // Loop over the arguments array, pushing each value to the stack
1099 Generate_PushAppliedArguments(masm, kVectorOffset, kArgumentsOffset,
1100 kIndexOffset, kLimitOffset);
1102 // Call the callable.
1103 // TODO(bmeurer): This should be a tail call according to ES6.
1104 __ movp(rdi, Operand(rbp, kFunctionOffset));
1105 __ Call(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1107 // Leave internal frame.
1109 __ ret(kStackSize * kPointerSize); // remove this, receiver, and arguments
1113 // Used by ReflectConstruct
1114 static void Generate_ConstructHelper(MacroAssembler* masm) {
1115 const int kFormalParameters = 3;
1116 const int kStackSize = kFormalParameters + 1;
1119 // rsp : return address
1120 // rsp[8] : original constructor (new.target)
1121 // rsp[16] : arguments
1122 // rsp[24] : constructor
1124 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1126 // rbp : Old base pointer
1127 // rbp[8] : return address
1128 // rbp[16] : original constructor (new.target)
1129 // rbp[24] : arguments
1130 // rbp[32] : constructor
1131 static const int kNewTargetOffset = kFPOnStackSize + kPCOnStackSize;
1132 static const int kArgumentsOffset = kNewTargetOffset + kPointerSize;
1133 static const int kFunctionOffset = kArgumentsOffset + kPointerSize;
1135 static const int kVectorOffset =
1136 InternalFrameConstants::kCodeOffset - 1 * kPointerSize;
1139 __ movp(rdi, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1140 __ movp(rdi, FieldOperand(rdi, SharedFunctionInfo::kFeedbackVectorOffset));
1143 // If newTarget is not supplied, set it to constructor
1144 Label validate_arguments;
1145 __ movp(rax, Operand(rbp, kNewTargetOffset));
1146 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
1147 __ j(not_equal, &validate_arguments, Label::kNear);
1148 __ movp(rax, Operand(rbp, kFunctionOffset));
1149 __ movp(Operand(rbp, kNewTargetOffset), rax);
1151 // Validate arguments
1152 __ bind(&validate_arguments);
1153 __ Push(Operand(rbp, kFunctionOffset));
1154 __ Push(Operand(rbp, kArgumentsOffset));
1155 __ Push(Operand(rbp, kNewTargetOffset));
1156 __ InvokeBuiltin(Context::REFLECT_CONSTRUCT_PREPARE_BUILTIN_INDEX,
1159 Generate_CheckStackOverflow(masm, kFunctionOffset, kRaxIsSmiTagged);
1161 // Push current index and limit.
1162 const int kLimitOffset = kVectorOffset - 1 * kPointerSize;
1163 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
1164 __ Push(rax); // limit
1165 __ Push(Immediate(0)); // index
1166 // Push the constructor function as callee.
1167 __ Push(Operand(rbp, kFunctionOffset));
1169 // Loop over the arguments array, pushing each value to the stack
1170 Generate_PushAppliedArguments(masm, kVectorOffset, kArgumentsOffset,
1171 kIndexOffset, kLimitOffset);
1173 // Use undefined feedback vector
1174 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
1175 __ movp(rdi, Operand(rbp, kFunctionOffset));
1176 __ movp(rcx, Operand(rbp, kNewTargetOffset));
1178 // Call the function.
1179 CallConstructStub stub(masm->isolate(), SUPER_CONSTRUCTOR_CALL);
1180 __ call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
1182 // Leave internal frame.
1184 // remove this, target, arguments and newTarget
1185 __ ret(kStackSize * kPointerSize);
1189 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1190 Generate_ApplyHelper(masm, false);
1194 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1195 Generate_ApplyHelper(masm, true);
1199 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1200 Generate_ConstructHelper(masm);
1204 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
1205 // ----------- S t a t e -------------
1207 // -- rsp[0] : return address
1208 // -- rsp[8] : last argument
1209 // -----------------------------------
1210 Label generic_array_code;
1212 // Get the InternalArray function.
1213 __ LoadGlobalFunction(Context::INTERNAL_ARRAY_FUNCTION_INDEX, rdi);
1215 if (FLAG_debug_code) {
1216 // Initial map for the builtin InternalArray functions should be maps.
1217 __ movp(rbx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
1218 // Will both indicate a NULL and a Smi.
1219 STATIC_ASSERT(kSmiTag == 0);
1220 Condition not_smi = NegateCondition(masm->CheckSmi(rbx));
1221 __ Check(not_smi, kUnexpectedInitialMapForInternalArrayFunction);
1222 __ CmpObjectType(rbx, MAP_TYPE, rcx);
1223 __ Check(equal, kUnexpectedInitialMapForInternalArrayFunction);
1226 // Run the native code for the InternalArray function called as a normal
1229 InternalArrayConstructorStub stub(masm->isolate());
1230 __ TailCallStub(&stub);
1234 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
1235 // ----------- S t a t e -------------
1237 // -- rsp[0] : return address
1238 // -- rsp[8] : last argument
1239 // -----------------------------------
1240 Label generic_array_code;
1242 // Get the Array function.
1243 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, rdi);
1245 if (FLAG_debug_code) {
1246 // Initial map for the builtin Array functions should be maps.
1247 __ movp(rbx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
1248 // Will both indicate a NULL and a Smi.
1249 STATIC_ASSERT(kSmiTag == 0);
1250 Condition not_smi = NegateCondition(masm->CheckSmi(rbx));
1251 __ Check(not_smi, kUnexpectedInitialMapForArrayFunction);
1252 __ CmpObjectType(rbx, MAP_TYPE, rcx);
1253 __ Check(equal, kUnexpectedInitialMapForArrayFunction);
1257 // Run the native code for the Array function called as a normal function.
1259 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
1260 ArrayConstructorStub stub(masm->isolate());
1261 __ TailCallStub(&stub);
1266 void Builtins::Generate_StringConstructor(MacroAssembler* masm) {
1267 // ----------- S t a t e -------------
1268 // -- rax : number of arguments
1269 // -- rdi : constructor function
1270 // -- rsp[0] : return address
1271 // -- rsp[(argc - n) * 8] : arg[n] (zero-based)
1272 // -- rsp[(argc + 1) * 8] : receiver
1273 // -----------------------------------
1275 // 1. Load the first argument into rax and get rid of the rest (including the
1279 StackArgumentsAccessor args(rsp, rax);
1281 __ j(zero, &no_arguments, Label::kNear);
1282 __ movp(rbx, args.GetArgumentOperand(1));
1283 __ PopReturnAddressTo(rcx);
1284 __ leap(rsp, Operand(rsp, rax, times_pointer_size, kPointerSize));
1285 __ PushReturnAddressFrom(rcx);
1289 // 2a. At least one argument, return rax if it's a string, otherwise
1290 // dispatch to appropriate conversion.
1291 Label to_string, symbol_descriptive_string;
1293 __ JumpIfSmi(rax, &to_string, Label::kNear);
1294 STATIC_ASSERT(FIRST_NONSTRING_TYPE == SYMBOL_TYPE);
1295 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, rdx);
1296 __ j(above, &to_string, Label::kNear);
1297 __ j(equal, &symbol_descriptive_string, Label::kNear);
1301 // 2b. No arguments, return the empty string (and pop the receiver).
1302 __ bind(&no_arguments);
1304 __ LoadRoot(rax, Heap::kempty_stringRootIndex);
1305 __ ret(1 * kPointerSize);
1308 // 3a. Convert rax to a string.
1309 __ bind(&to_string);
1311 ToStringStub stub(masm->isolate());
1312 __ TailCallStub(&stub);
1315 // 3b. Convert symbol in rax to a string.
1316 __ bind(&symbol_descriptive_string);
1318 __ PopReturnAddressTo(rcx);
1320 __ PushReturnAddressFrom(rcx);
1321 __ TailCallRuntime(Runtime::kSymbolDescriptiveString, 1, 1);
1327 void Builtins::Generate_StringConstructor_ConstructStub(MacroAssembler* masm) {
1328 // ----------- S t a t e -------------
1329 // -- rax : number of arguments
1330 // -- rdi : constructor function
1331 // -- rsp[0] : return address
1332 // -- rsp[(argc - n) * 8] : arg[n] (zero-based)
1333 // -- rsp[(argc + 1) * 8] : receiver
1334 // -----------------------------------
1336 // 1. Load the first argument into rbx and get rid of the rest (including the
1339 StackArgumentsAccessor args(rsp, rax);
1340 Label no_arguments, done;
1342 __ j(zero, &no_arguments, Label::kNear);
1343 __ movp(rbx, args.GetArgumentOperand(1));
1344 __ jmp(&done, Label::kNear);
1345 __ bind(&no_arguments);
1346 __ LoadRoot(rbx, Heap::kempty_stringRootIndex);
1348 __ PopReturnAddressTo(rcx);
1349 __ leap(rsp, Operand(rsp, rax, times_pointer_size, kPointerSize));
1350 __ PushReturnAddressFrom(rcx);
1353 // 2. Make sure rbx is a string.
1355 Label convert, done_convert;
1356 __ JumpIfSmi(rbx, &convert, Label::kNear);
1357 __ CmpObjectType(rbx, FIRST_NONSTRING_TYPE, rdx);
1358 __ j(below, &done_convert);
1361 FrameScope scope(masm, StackFrame::INTERNAL);
1362 ToStringStub stub(masm->isolate());
1369 __ bind(&done_convert);
1372 // 3. Allocate a JSValue wrapper for the string.
1374 // ----------- S t a t e -------------
1375 // -- rbx : the first argument
1376 // -- rdi : constructor function
1377 // -----------------------------------
1379 Label allocate, done_allocate;
1380 __ Allocate(JSValue::kSize, rax, rcx, no_reg, &allocate, TAG_OBJECT);
1381 __ bind(&done_allocate);
1383 // Initialize the JSValue in rax.
1384 __ LoadGlobalFunctionInitialMap(rdi, rcx);
1385 __ movp(FieldOperand(rax, HeapObject::kMapOffset), rcx);
1386 __ LoadRoot(rcx, Heap::kEmptyFixedArrayRootIndex);
1387 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), rcx);
1388 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rcx);
1389 __ movp(FieldOperand(rax, JSValue::kValueOffset), rbx);
1390 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
1393 // Fallback to the runtime to allocate in new space.
1396 FrameScope scope(masm, StackFrame::INTERNAL);
1399 __ Push(Smi::FromInt(JSValue::kSize));
1400 __ CallRuntime(Runtime::kAllocateInNewSpace, 1);
1404 __ jmp(&done_allocate);
1409 static void ArgumentsAdaptorStackCheck(MacroAssembler* masm,
1410 Label* stack_overflow) {
1411 // ----------- S t a t e -------------
1412 // -- rax : actual number of arguments
1413 // -- rbx : expected number of arguments
1414 // -- rdi: function (passed through to callee)
1415 // -----------------------------------
1416 // Check the stack for overflow. We are not trying to catch
1417 // interruptions (e.g. debug break and preemption) here, so the "real stack
1418 // limit" is checked.
1420 __ LoadRoot(rdx, Heap::kRealStackLimitRootIndex);
1422 // Make rcx the space we have left. The stack might already be overflowed
1423 // here which will cause rcx to become negative.
1425 // Make rdx the space we need for the array when it is unrolled onto the
1428 __ shlp(rdx, Immediate(kPointerSizeLog2));
1429 // Check if the arguments will overflow the stack.
1431 __ j(less_equal, stack_overflow); // Signed comparison.
1435 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1439 // Store the arguments adaptor context sentinel.
1440 __ Push(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
1442 // Push the function on the stack.
1445 // Preserve the number of arguments on the stack. Must preserve rax,
1446 // rbx and rcx because these registers are used when copying the
1447 // arguments and the receiver.
1448 __ Integer32ToSmi(r8, rax);
1453 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1454 // Retrieve the number of arguments from the stack. Number is a Smi.
1455 __ movp(rbx, Operand(rbp, ArgumentsAdaptorFrameConstants::kLengthOffset));
1461 // Remove caller arguments from the stack.
1462 __ PopReturnAddressTo(rcx);
1463 SmiIndex index = masm->SmiToIndex(rbx, rbx, kPointerSizeLog2);
1464 __ leap(rsp, Operand(rsp, index.reg, index.scale, 1 * kPointerSize));
1465 __ PushReturnAddressFrom(rcx);
1469 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1470 // ----------- S t a t e -------------
1471 // -- rax : actual number of arguments
1472 // -- rbx : expected number of arguments
1473 // -- rdi: function (passed through to callee)
1474 // -----------------------------------
1476 Label invoke, dont_adapt_arguments;
1477 Counters* counters = masm->isolate()->counters();
1478 __ IncrementCounter(counters->arguments_adaptors(), 1);
1480 Label stack_overflow;
1481 ArgumentsAdaptorStackCheck(masm, &stack_overflow);
1483 Label enough, too_few;
1484 __ movp(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
1486 __ j(less, &too_few);
1487 __ cmpp(rbx, Immediate(SharedFunctionInfo::kDontAdaptArgumentsSentinel));
1488 __ j(equal, &dont_adapt_arguments);
1490 { // Enough parameters: Actual >= expected.
1492 EnterArgumentsAdaptorFrame(masm);
1494 // Copy receiver and all expected arguments.
1495 const int offset = StandardFrameConstants::kCallerSPOffset;
1496 __ leap(rax, Operand(rbp, rax, times_pointer_size, offset));
1497 __ Set(r8, -1); // account for receiver
1502 __ Push(Operand(rax, 0));
1503 __ subp(rax, Immediate(kPointerSize));
1509 { // Too few parameters: Actual < expected.
1512 // If the function is strong we need to throw an error.
1513 Label no_strong_error;
1514 __ movp(kScratchRegister,
1515 FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1516 __ testb(FieldOperand(kScratchRegister,
1517 SharedFunctionInfo::kStrongModeByteOffset),
1518 Immediate(1 << SharedFunctionInfo::kStrongModeBitWithinByte));
1519 __ j(equal, &no_strong_error, Label::kNear);
1521 // What we really care about is the required number of arguments.
1523 if (kPointerSize == kInt32Size) {
1526 FieldOperand(kScratchRegister, SharedFunctionInfo::kLengthOffset));
1527 __ SmiToInteger32(kScratchRegister, kScratchRegister);
1529 // See comment near kLengthOffset in src/objects.h
1532 FieldOperand(kScratchRegister, SharedFunctionInfo::kLengthOffset));
1533 __ shrq(kScratchRegister, Immediate(1));
1536 __ cmpp(rax, kScratchRegister);
1537 __ j(greater_equal, &no_strong_error, Label::kNear);
1540 FrameScope frame(masm, StackFrame::MANUAL);
1541 EnterArgumentsAdaptorFrame(masm);
1542 __ CallRuntime(Runtime::kThrowStrongModeTooFewArguments, 0);
1545 __ bind(&no_strong_error);
1546 EnterArgumentsAdaptorFrame(masm);
1548 // Copy receiver and all actual arguments.
1549 const int offset = StandardFrameConstants::kCallerSPOffset;
1550 __ leap(rdi, Operand(rbp, rax, times_pointer_size, offset));
1551 __ Set(r8, -1); // account for receiver
1556 __ Push(Operand(rdi, 0));
1557 __ subp(rdi, Immediate(kPointerSize));
1561 // Fill remaining expected arguments with undefined values.
1563 __ LoadRoot(kScratchRegister, Heap::kUndefinedValueRootIndex);
1566 __ Push(kScratchRegister);
1570 // Restore function pointer.
1571 __ movp(rdi, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1574 // Call the entry point.
1577 // rax : expected number of arguments
1578 // rdi: function (passed through to callee)
1581 // Store offset of return address for deoptimizer.
1582 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1584 // Leave frame and return.
1585 LeaveArgumentsAdaptorFrame(masm);
1588 // -------------------------------------------
1589 // Dont adapt arguments.
1590 // -------------------------------------------
1591 __ bind(&dont_adapt_arguments);
1594 __ bind(&stack_overflow);
1596 FrameScope frame(masm, StackFrame::MANUAL);
1597 EnterArgumentsAdaptorFrame(masm);
1598 __ CallRuntime(Runtime::kThrowStackOverflow, 0);
1605 void Builtins::Generate_CallFunction(MacroAssembler* masm) {
1606 // ----------- S t a t e -------------
1607 // -- rax : the number of arguments (not including the receiver)
1608 // -- rdi : the function to call (checked to be a JSFunction)
1609 // -----------------------------------
1611 Label convert, convert_global_proxy, convert_to_object, done_convert;
1612 StackArgumentsAccessor args(rsp, rax);
1613 __ AssertFunction(rdi);
1614 // TODO(bmeurer): Throw a TypeError if function's [[FunctionKind]] internal
1615 // slot is "classConstructor".
1616 // Enter the context of the function; ToObject has to run in the function
1617 // context, and we also need to take the global proxy from the function
1618 // context in case of conversion.
1619 // See ES6 section 9.2.1 [[Call]] ( thisArgument, argumentsList)
1620 STATIC_ASSERT(SharedFunctionInfo::kNativeByteOffset ==
1621 SharedFunctionInfo::kStrictModeByteOffset);
1622 __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
1623 __ movp(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1624 // We need to convert the receiver for non-native sloppy mode functions.
1625 __ testb(FieldOperand(rdx, SharedFunctionInfo::kNativeByteOffset),
1626 Immediate((1 << SharedFunctionInfo::kNativeBitWithinByte) |
1627 (1 << SharedFunctionInfo::kStrictModeBitWithinByte)));
1628 __ j(not_zero, &done_convert);
1630 __ movp(rcx, args.GetReceiverOperand());
1632 // ----------- S t a t e -------------
1633 // -- rax : the number of arguments (not including the receiver)
1634 // -- rcx : the receiver
1635 // -- rdx : the shared function info.
1636 // -- rdi : the function to call (checked to be a JSFunction)
1637 // -- rsi : the function context.
1638 // -----------------------------------
1640 Label convert_receiver;
1641 __ JumpIfSmi(rcx, &convert_to_object, Label::kNear);
1642 STATIC_ASSERT(LAST_JS_RECEIVER_TYPE == LAST_TYPE);
1643 __ CmpObjectType(rcx, FIRST_JS_RECEIVER_TYPE, rbx);
1644 __ j(above_equal, &done_convert);
1645 __ JumpIfRoot(rcx, Heap::kUndefinedValueRootIndex, &convert_global_proxy,
1647 __ JumpIfNotRoot(rcx, Heap::kNullValueRootIndex, &convert_to_object,
1649 __ bind(&convert_global_proxy);
1651 // Patch receiver to global proxy.
1652 __ LoadGlobalProxy(rcx);
1654 __ jmp(&convert_receiver);
1655 __ bind(&convert_to_object);
1657 // Convert receiver using ToObject.
1658 // TODO(bmeurer): Inline the allocation here to avoid building the frame
1659 // in the fast case? (fall back to AllocateInNewSpace?)
1660 FrameScope scope(masm, StackFrame::INTERNAL);
1661 __ Integer32ToSmi(rax, rax);
1665 ToObjectStub stub(masm->isolate());
1670 __ SmiToInteger32(rax, rax);
1672 __ movp(rdx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1673 __ bind(&convert_receiver);
1674 __ movp(args.GetReceiverOperand(), rcx);
1676 __ bind(&done_convert);
1678 // ----------- S t a t e -------------
1679 // -- rax : the number of arguments (not including the receiver)
1680 // -- rdx : the shared function info.
1681 // -- rdi : the function to call (checked to be a JSFunction)
1682 // -- rsi : the function context.
1683 // -----------------------------------
1685 __ LoadSharedFunctionInfoSpecialField(
1686 rbx, rdx, SharedFunctionInfo::kFormalParameterCountOffset);
1687 __ movp(rdx, FieldOperand(rdi, JSFunction::kCodeEntryOffset));
1688 ParameterCount actual(rax);
1689 ParameterCount expected(rbx);
1690 __ InvokeCode(rdx, expected, actual, JUMP_FUNCTION, NullCallWrapper());
1695 void Builtins::Generate_Call(MacroAssembler* masm) {
1696 // ----------- S t a t e -------------
1697 // -- rax : the number of arguments (not including the receiver)
1698 // -- rdi : the target to call (can be any Object).
1699 // -----------------------------------
1701 Label non_smi, non_function;
1702 __ JumpIfSmi(rdi, &non_function);
1704 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rdx);
1705 __ j(equal, masm->isolate()->builtins()->CallFunction(),
1706 RelocInfo::CODE_TARGET);
1707 __ CmpInstanceType(rdx, JS_FUNCTION_PROXY_TYPE);
1708 __ j(not_equal, &non_function);
1710 // 1. Call to function proxy.
1711 // TODO(neis): This doesn't match the ES6 spec for [[Call]] on proxies.
1712 __ movp(rdi, FieldOperand(rdi, JSFunctionProxy::kCallTrapOffset));
1713 __ AssertNotSmi(rdi);
1716 // 2. Call to something else, which might have a [[Call]] internal method (if
1717 // not we raise an exception).
1718 __ bind(&non_function);
1719 // TODO(bmeurer): I wonder why we prefer to have slow API calls? This could
1720 // be awesome instead; i.e. a trivial improvement would be to call into the
1721 // runtime and just deal with the API function there instead of returning a
1722 // delegate from a runtime call that just jumps back to the runtime once
1723 // called. Or, bonus points, call directly into the C API function here, as
1724 // we do in some Crankshaft fast cases.
1725 StackArgumentsAccessor args(rsp, rax);
1726 // Overwrite the original receiver with the (original) target.
1727 __ movp(args.GetReceiverOperand(), rdi);
1729 // Determine the delegate for the target (if any).
1730 FrameScope scope(masm, StackFrame::INTERNAL);
1731 __ Integer32ToSmi(rax, rax);
1734 __ CallRuntime(Runtime::kGetFunctionDelegate, 1);
1737 __ SmiToInteger32(rax, rax);
1739 // The delegate is always a regular function.
1740 __ AssertFunction(rdi);
1741 __ Jump(masm->isolate()->builtins()->CallFunction(), RelocInfo::CODE_TARGET);
1746 void Builtins::Generate_PushArgsAndCall(MacroAssembler* masm) {
1747 // ----------- S t a t e -------------
1748 // -- rax : the number of arguments (not including the receiver)
1749 // -- rbx : the address of the first argument to be pushed. Subsequent
1750 // arguments should be consecutive above this, in the same order as
1751 // they are to be pushed onto the stack.
1752 // -- rdi : the target to call (can be any Object).
1754 // Pop return address to allow tail-call after pushing arguments.
1757 // Find the address of the last argument.
1759 __ addp(rcx, Immediate(1)); // Add one for receiver.
1760 __ shlp(rcx, Immediate(kPointerSizeLog2));
1764 // Push the arguments.
1765 Label loop_header, loop_check;
1766 __ j(always, &loop_check);
1767 __ bind(&loop_header);
1768 __ Push(Operand(rbx, 0));
1769 __ subp(rbx, Immediate(kPointerSize));
1770 __ bind(&loop_check);
1772 __ j(greater, &loop_header, Label::kNear);
1775 __ Push(rdx); // Re-push return address.
1776 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
1780 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1781 // Lookup the function in the JavaScript frame.
1782 __ movp(rax, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
1784 FrameScope scope(masm, StackFrame::INTERNAL);
1785 // Pass function as argument.
1787 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1791 // If the code object is null, just return to the unoptimized code.
1792 __ cmpp(rax, Immediate(0));
1793 __ j(not_equal, &skip, Label::kNear);
1798 // Load deoptimization data from the code object.
1799 __ movp(rbx, Operand(rax, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1801 // Load the OSR entrypoint offset from the deoptimization data.
1802 __ SmiToInteger32(rbx, Operand(rbx, FixedArray::OffsetOfElementAt(
1803 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1805 // Compute the target address = code_obj + header_size + osr_offset
1806 __ leap(rax, Operand(rax, rbx, times_1, Code::kHeaderSize - kHeapObjectTag));
1808 // Overwrite the return address on the stack.
1809 __ movq(StackOperandForReturnAddress(0), rax);
1811 // And "return" to the OSR entry point of the function.
1816 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1817 // We check the stack limit as indicator that recompilation might be done.
1819 __ CompareRoot(rsp, Heap::kStackLimitRootIndex);
1820 __ j(above_equal, &ok);
1822 FrameScope scope(masm, StackFrame::INTERNAL);
1823 __ CallRuntime(Runtime::kStackGuard, 0);
1825 __ jmp(masm->isolate()->builtins()->OnStackReplacement(),
1826 RelocInfo::CODE_TARGET);
1835 } // namespace internal
1838 #endif // V8_TARGET_ARCH_X64