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.
5 #if V8_TARGET_ARCH_IA32
7 #include "src/code-factory.h"
8 #include "src/codegen.h"
9 #include "src/deoptimizer.h"
10 #include "src/full-codegen/full-codegen.h"
11 #include "src/ia32/frames-ia32.h"
17 #define __ ACCESS_MASM(masm)
20 void Builtins::Generate_Adaptor(MacroAssembler* masm,
22 BuiltinExtraArguments extra_args) {
23 // ----------- S t a t e -------------
24 // -- eax : number of arguments excluding receiver
25 // -- edi : called function (only guaranteed when
26 // extra_args requires it)
28 // -- esp[0] : return address
29 // -- esp[4] : last argument
31 // -- esp[4 * argc] : first argument (argc == eax)
32 // -- esp[4 * (argc +1)] : receiver
33 // -----------------------------------
35 // Insert extra arguments.
36 int num_extra_args = 0;
37 if (extra_args == NEEDS_CALLED_FUNCTION) {
39 Register scratch = ebx;
40 __ pop(scratch); // Save return address.
42 __ push(scratch); // Restore return address.
44 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
47 // JumpToExternalReference expects eax to contain the number of arguments
48 // including the receiver and the extra arguments.
49 __ add(eax, Immediate(num_extra_args + 1));
50 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
54 static void CallRuntimePassFunction(
55 MacroAssembler* masm, Runtime::FunctionId function_id) {
56 FrameScope scope(masm, StackFrame::INTERNAL);
57 // Push a copy of the function.
59 // Function is also the parameter to the runtime call.
62 __ CallRuntime(function_id, 1);
68 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
69 __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
70 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kCodeOffset));
71 __ lea(eax, FieldOperand(eax, Code::kHeaderSize));
76 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
77 __ lea(eax, FieldOperand(eax, 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 ExternalReference stack_limit =
90 ExternalReference::address_of_stack_limit(masm->isolate());
91 __ cmp(esp, Operand::StaticVariable(stack_limit));
92 __ j(above_equal, &ok, Label::kNear);
94 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
95 GenerateTailCallToReturnedCode(masm);
98 GenerateTailCallToSharedCode(masm);
102 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
103 bool is_api_function,
104 bool create_memento) {
105 // ----------- S t a t e -------------
106 // -- eax: number of arguments
107 // -- edi: constructor function
108 // -- ebx: allocation site or undefined
109 // -- edx: original constructor
110 // -----------------------------------
112 // Should never create mementos for api functions.
113 DCHECK(!is_api_function || !create_memento);
115 // Enter a construct frame.
117 FrameScope scope(masm, StackFrame::CONSTRUCT);
119 // Preserve the incoming parameters on the stack.
120 __ AssertUndefinedOrAllocationSite(ebx);
127 // Try to allocate the object without transitioning into C code. If any of
128 // the preconditions is not met, the code bails out to the runtime call.
129 Label rt_call, allocated;
130 if (FLAG_inline_new) {
131 ExternalReference debug_step_in_fp =
132 ExternalReference::debug_step_in_fp_address(masm->isolate());
133 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
134 __ j(not_equal, &rt_call);
136 // Fall back to runtime if the original constructor and function differ.
138 __ j(not_equal, &rt_call);
140 // Verified that the constructor is a JSFunction.
141 // Load the initial map and verify that it is in fact a map.
143 __ mov(eax, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
144 // Will both indicate a NULL and a Smi
145 __ JumpIfSmi(eax, &rt_call);
147 // eax: initial map (if proven valid below)
148 __ CmpObjectType(eax, MAP_TYPE, ebx);
149 __ j(not_equal, &rt_call);
151 // Check that the constructor is not constructing a JSFunction (see
152 // comments in Runtime_NewObject in runtime.cc). In which case the
153 // initial map's instance type would be JS_FUNCTION_TYPE.
156 __ CmpInstanceType(eax, JS_FUNCTION_TYPE);
157 __ j(equal, &rt_call);
159 if (!is_api_function) {
161 // The code below relies on these assumptions.
162 STATIC_ASSERT(Map::Counter::kShift + Map::Counter::kSize == 32);
163 // Check if slack tracking is enabled.
164 __ mov(esi, FieldOperand(eax, Map::kBitField3Offset));
165 __ shr(esi, Map::Counter::kShift);
166 __ cmp(esi, Map::kSlackTrackingCounterEnd);
167 __ j(less, &allocate);
168 // Decrease generous allocation count.
169 __ sub(FieldOperand(eax, Map::kBitField3Offset),
170 Immediate(1 << Map::Counter::kShift));
172 __ cmp(esi, Map::kSlackTrackingCounterEnd);
173 __ j(not_equal, &allocate);
179 __ push(edi); // constructor
180 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
185 __ mov(esi, Map::kSlackTrackingCounterEnd - 1);
190 // Now allocate the JSObject on the heap.
193 __ movzx_b(edi, FieldOperand(eax, Map::kInstanceSizeOffset));
194 __ shl(edi, kPointerSizeLog2);
195 if (create_memento) {
196 __ add(edi, Immediate(AllocationMemento::kSize));
199 __ Allocate(edi, ebx, edi, no_reg, &rt_call, NO_ALLOCATION_FLAGS);
201 Factory* factory = masm->isolate()->factory();
203 // Allocated the JSObject, now initialize the fields.
206 // edi: start of next object (including memento if create_memento)
207 __ mov(Operand(ebx, JSObject::kMapOffset), eax);
208 __ mov(ecx, factory->empty_fixed_array());
209 __ mov(Operand(ebx, JSObject::kPropertiesOffset), ecx);
210 __ mov(Operand(ebx, JSObject::kElementsOffset), ecx);
211 // Set extra fields in the newly allocated object.
214 // edi: start of next object (including memento if create_memento)
215 // esi: slack tracking counter (non-API function case)
216 __ mov(edx, factory->undefined_value());
217 __ lea(ecx, Operand(ebx, JSObject::kHeaderSize));
218 if (!is_api_function) {
219 Label no_inobject_slack_tracking;
221 // Check if slack tracking is enabled.
222 __ cmp(esi, Map::kSlackTrackingCounterEnd);
223 __ j(less, &no_inobject_slack_tracking);
225 // Allocate object with a slack.
229 eax, Map::kInObjectPropertiesOrConstructorFunctionIndexOffset));
230 __ movzx_b(eax, FieldOperand(eax, Map::kUnusedPropertyFieldsOffset));
233 Operand(ebx, esi, times_pointer_size, JSObject::kHeaderSize));
234 // esi: offset of first field after pre-allocated fields
235 if (FLAG_debug_code) {
237 __ Assert(less_equal,
238 kUnexpectedNumberOfPreAllocatedPropertyFields);
240 __ InitializeFieldsWithFiller(ecx, esi, edx);
241 __ mov(edx, factory->one_pointer_filler_map());
242 // Fill the remaining fields with one pointer filler map.
244 __ bind(&no_inobject_slack_tracking);
247 if (create_memento) {
248 __ lea(esi, Operand(edi, -AllocationMemento::kSize));
249 __ InitializeFieldsWithFiller(ecx, esi, edx);
251 // Fill in memento fields if necessary.
252 // esi: points to the allocated but uninitialized memento.
253 __ mov(Operand(esi, AllocationMemento::kMapOffset),
254 factory->allocation_memento_map());
255 // Get the cell or undefined.
256 __ mov(edx, Operand(esp, 3 * kPointerSize));
257 __ AssertUndefinedOrAllocationSite(edx);
258 __ mov(Operand(esi, AllocationMemento::kAllocationSiteOffset),
261 __ InitializeFieldsWithFiller(ecx, edi, edx);
264 // Add the object tag to make the JSObject real, so that we can continue
265 // and jump into the continuation code at any time from now on.
266 // ebx: JSObject (untagged)
267 __ or_(ebx, Immediate(kHeapObjectTag));
269 // Continue with JSObject being successfully allocated
270 // ebx: JSObject (tagged)
274 // Allocate the new receiver object using the runtime call.
275 // edx: original constructor
277 int offset = kPointerSize;
278 if (create_memento) {
279 // Get the cell or allocation site.
280 __ mov(edi, Operand(esp, kPointerSize * 3));
281 __ push(edi); // argument 1: allocation site
282 offset += kPointerSize;
285 // Must restore esi (context) and edi (constructor) before calling
287 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
288 __ mov(edi, Operand(esp, offset));
289 __ push(edi); // argument 2/1: constructor function
290 __ push(edx); // argument 3/2: original constructor
291 if (create_memento) {
292 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 3);
294 __ CallRuntime(Runtime::kNewObject, 2);
296 __ mov(ebx, eax); // store result in ebx
298 // Runtime_NewObjectWithAllocationSite increments allocation count.
299 // Skip the increment.
300 Label count_incremented;
301 if (create_memento) {
302 __ jmp(&count_incremented);
305 // New object allocated.
306 // ebx: newly allocated object
309 if (create_memento) {
310 __ mov(ecx, Operand(esp, 3 * kPointerSize));
311 __ cmp(ecx, masm->isolate()->factory()->undefined_value());
312 __ j(equal, &count_incremented);
313 // ecx is an AllocationSite. We are creating a memento from it, so we
314 // need to increment the memento create count.
315 __ add(FieldOperand(ecx, AllocationSite::kPretenureCreateCountOffset),
316 Immediate(Smi::FromInt(1)));
317 __ bind(&count_incremented);
320 // Restore the parameters.
321 __ pop(edx); // new.target
322 __ pop(edi); // Constructor function.
324 // Retrieve smi-tagged arguments count from the stack.
325 __ mov(eax, Operand(esp, 0));
328 // Push new.target onto the construct frame. This is stored just below the
329 // receiver on the stack.
332 // Push the allocated receiver to the stack. We need two copies
333 // because we may have to return the original one and the calling
334 // conventions dictate that the called function pops the receiver.
338 // Set up pointer to last argument.
339 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
341 // Copy arguments and receiver to the expression stack.
346 __ push(Operand(ebx, ecx, times_4, 0));
349 __ j(greater_equal, &loop);
351 // Call the function.
352 if (is_api_function) {
353 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
355 masm->isolate()->builtins()->HandleApiCallConstruct();
356 __ call(code, RelocInfo::CODE_TARGET);
358 ParameterCount actual(eax);
359 __ InvokeFunction(edi, actual, CALL_FUNCTION,
363 // Store offset of return address for deoptimizer.
364 if (!is_api_function) {
365 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
368 // Restore context from the frame.
369 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
371 // If the result is an object (in the ECMA sense), we should get rid
372 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
374 Label use_receiver, exit;
376 // If the result is a smi, it is *not* an object in the ECMA sense.
377 __ JumpIfSmi(eax, &use_receiver);
379 // If the type of the result (stored in its map) is less than
380 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
381 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
382 __ j(above_equal, &exit);
384 // Throw away the result of the constructor invocation and use the
385 // on-stack receiver as the result.
386 __ bind(&use_receiver);
387 __ mov(eax, Operand(esp, 0));
389 // Restore the arguments count and leave the construct frame. The arguments
390 // count is stored below the reciever and the new.target.
392 __ mov(ebx, Operand(esp, 2 * kPointerSize));
394 // Leave construct frame.
397 // Remove caller arguments from the stack and return.
398 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
400 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
402 __ IncrementCounter(masm->isolate()->counters()->constructed_objects(), 1);
407 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
408 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
412 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
413 Generate_JSConstructStubHelper(masm, true, false);
417 void Builtins::Generate_JSConstructStubForDerived(MacroAssembler* masm) {
418 // ----------- S t a t e -------------
419 // -- eax: number of arguments
420 // -- edi: constructor function
421 // -- ebx: allocation site or undefined
422 // -- edx: original constructor
423 // -----------------------------------
426 FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
428 // Preserve allocation site.
429 __ AssertUndefinedOrAllocationSite(ebx);
432 // Preserve actual arguments count.
440 // receiver is the hole.
441 __ push(Immediate(masm->isolate()->factory()->the_hole_value()));
443 // Set up pointer to last argument.
444 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
446 // Copy arguments and receiver to the expression stack.
451 __ push(Operand(ebx, ecx, times_4, 0));
454 __ j(greater_equal, &loop);
458 ExternalReference debug_step_in_fp =
459 ExternalReference::debug_step_in_fp_address(masm->isolate());
460 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
461 __ j(equal, &skip_step_in);
466 __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
470 __ bind(&skip_step_in);
473 ParameterCount actual(eax);
474 __ InvokeFunction(edi, actual, CALL_FUNCTION, NullCallWrapper());
476 // Restore context from the frame.
477 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
479 // Get arguments count, skipping over new.target.
480 __ mov(ebx, Operand(esp, kPointerSize));
483 __ pop(ecx); // Return address.
484 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize));
490 enum IsTagged { kEaxIsSmiTagged, kEaxIsUntaggedInt };
493 // Clobbers ecx, edx, edi; preserves all other registers.
494 static void Generate_CheckStackOverflow(MacroAssembler* masm,
495 const int calleeOffset,
496 IsTagged eax_is_tagged) {
497 // eax : the number of items to be pushed to the stack
499 // Check the stack for overflow. We are not trying to catch
500 // interruptions (e.g. debug break and preemption) here, so the "real stack
501 // limit" is checked.
503 ExternalReference real_stack_limit =
504 ExternalReference::address_of_real_stack_limit(masm->isolate());
505 __ mov(edi, Operand::StaticVariable(real_stack_limit));
506 // Make ecx the space we have left. The stack might already be overflowed
507 // here which will cause ecx to become negative.
510 // Make edx the space we need for the array when it is unrolled onto the
513 int smi_tag = eax_is_tagged == kEaxIsSmiTagged ? kSmiTagSize : 0;
514 __ shl(edx, kPointerSizeLog2 - smi_tag);
515 // Check if the arguments will overflow the stack.
517 __ j(greater, &okay); // Signed comparison.
519 // Out of stack space.
520 __ push(Operand(ebp, calleeOffset)); // push this
521 if (eax_is_tagged == kEaxIsUntaggedInt) {
525 __ InvokeBuiltin(Context::STACK_OVERFLOW_BUILTIN_INDEX, CALL_FUNCTION);
531 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
533 ProfileEntryHookStub::MaybeCallEntryHook(masm);
535 // Clear the context before we push it when entering the internal frame.
536 __ Move(esi, Immediate(0));
539 FrameScope scope(masm, StackFrame::INTERNAL);
541 // Load the previous frame pointer (ebx) to access C arguments
542 __ mov(ebx, Operand(ebp, 0));
544 // Get the function from the frame and setup the context.
545 __ mov(ecx, Operand(ebx, EntryFrameConstants::kFunctionArgOffset));
546 __ mov(esi, FieldOperand(ecx, JSFunction::kContextOffset));
548 // Push the function and the receiver onto the stack.
550 __ push(Operand(ebx, EntryFrameConstants::kReceiverArgOffset));
552 // Load the number of arguments and setup pointer to the arguments.
553 __ mov(eax, Operand(ebx, EntryFrameConstants::kArgcOffset));
554 __ mov(ebx, Operand(ebx, EntryFrameConstants::kArgvOffset));
556 // Check if we have enough stack space to push all arguments.
557 // The function is the first thing that was pushed above after entering
558 // the internal frame.
559 const int kFunctionOffset =
560 InternalFrameConstants::kCodeOffset - kPointerSize;
561 // Expects argument count in eax. Clobbers ecx, edx, edi.
562 Generate_CheckStackOverflow(masm, kFunctionOffset, kEaxIsUntaggedInt);
564 // Copy arguments to the stack in a loop.
566 __ Move(ecx, Immediate(0));
569 __ mov(edx, Operand(ebx, ecx, times_4, 0)); // push parameter from argv
570 __ push(Operand(edx, 0)); // dereference handle
574 __ j(not_equal, &loop);
576 // Get the function from the stack and call it.
577 // kPointerSize for the receiver.
578 __ mov(edi, Operand(esp, eax, times_4, kPointerSize));
582 // No type feedback cell is available
583 __ mov(ebx, masm->isolate()->factory()->undefined_value());
584 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
587 ParameterCount actual(eax);
588 __ InvokeFunction(edi, actual, CALL_FUNCTION,
592 // Exit the internal frame. Notice that this also removes the empty.
593 // context and the function left on the stack by the code
596 __ ret(kPointerSize); // Remove receiver.
600 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
601 Generate_JSEntryTrampolineHelper(masm, false);
605 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
606 Generate_JSEntryTrampolineHelper(masm, true);
610 // Generate code for entering a JS function with the interpreter.
611 // On entry to the function the receiver and arguments have been pushed on the
612 // stack left to right. The actual argument count matches the formal parameter
613 // count expected by the function.
615 // The live registers are:
616 // o edi: the JS function object being called
617 // o esi: our context
618 // o ebp: the caller's frame pointer
619 // o esp: stack pointer (pointing to return address)
621 // The function builds a JS frame. Please see JavaScriptFrameConstants in
622 // frames-ia32.h for its layout.
623 // TODO(rmcilroy): We will need to include the current bytecode pointer in the
625 void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
626 // Open a frame scope to indicate that there is a frame on the stack. The
627 // MANUAL indicates that the scope shouldn't actually generate code to set up
628 // the frame (that is done below).
629 FrameScope frame_scope(masm, StackFrame::MANUAL);
630 __ push(ebp); // Caller's frame pointer.
632 __ push(esi); // Callee's context.
633 __ push(edi); // Callee's JS function.
635 // Get the bytecode array from the function object and load the pointer to the
636 // first entry into edi (InterpreterBytecodeRegister).
637 __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
638 __ mov(kInterpreterBytecodeArrayRegister,
639 FieldOperand(eax, SharedFunctionInfo::kFunctionDataOffset));
641 if (FLAG_debug_code) {
642 // Check function data field is actually a BytecodeArray object.
643 __ AssertNotSmi(kInterpreterBytecodeArrayRegister);
644 __ CmpObjectType(kInterpreterBytecodeArrayRegister, BYTECODE_ARRAY_TYPE,
646 __ Assert(equal, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
649 // Allocate the local and temporary register file on the stack.
651 // Load frame size from the BytecodeArray object.
652 __ mov(ebx, FieldOperand(kInterpreterBytecodeArrayRegister,
653 BytecodeArray::kFrameSizeOffset));
655 // Do a stack check to ensure we don't go over the limit.
659 ExternalReference stack_limit =
660 ExternalReference::address_of_real_stack_limit(masm->isolate());
661 __ cmp(ecx, Operand::StaticVariable(stack_limit));
662 __ j(above_equal, &ok);
663 __ InvokeBuiltin(Context::STACK_OVERFLOW_BUILTIN_INDEX, CALL_FUNCTION);
666 // If ok, push undefined as the initial value for all register file entries.
669 __ mov(eax, Immediate(masm->isolate()->factory()->undefined_value()));
671 __ bind(&loop_header);
672 // TODO(rmcilroy): Consider doing more than one push per loop iteration.
674 // Continue loop if not done.
675 __ bind(&loop_check);
676 __ sub(ebx, Immediate(kPointerSize));
677 __ j(greater_equal, &loop_header);
680 // TODO(rmcilroy): List of things not currently dealt with here but done in
681 // fullcodegen's prologue:
682 // - Support profiler (specifically profiling_counter).
683 // - Call ProfileEntryHookStub when isolate has a function_entry_hook.
684 // - Allow simulator stop operations if FLAG_stop_at is set.
685 // - Deal with sloppy mode functions which need to replace the
686 // receiver with the global proxy when called as functions (without an
687 // explicit receiver object).
688 // - Code aging of the BytecodeArray object.
689 // - Supporting FLAG_trace.
691 // The following items are also not done here, and will probably be done using
692 // explicit bytecodes instead:
693 // - Allocating a new local context if applicable.
694 // - Setting up a local binding to the this function, which is used in
695 // derived constructors with super calls.
696 // - Setting new.target if required.
697 // - Dealing with REST parameters (only if
698 // https://codereview.chromium.org/1235153006 doesn't land by then).
699 // - Dealing with argument objects.
701 // Perform stack guard check.
704 ExternalReference stack_limit =
705 ExternalReference::address_of_stack_limit(masm->isolate());
706 __ cmp(esp, Operand::StaticVariable(stack_limit));
707 __ j(above_equal, &ok);
708 __ CallRuntime(Runtime::kStackGuard, 0);
712 // Load accumulator, register file, bytecode offset, dispatch table into
714 __ LoadRoot(kInterpreterAccumulatorRegister, Heap::kUndefinedValueRootIndex);
715 __ mov(kInterpreterRegisterFileRegister, ebp);
717 kInterpreterRegisterFileRegister,
718 Immediate(kPointerSize + StandardFrameConstants::kFixedFrameSizeFromFp));
719 __ mov(kInterpreterBytecodeOffsetRegister,
720 Immediate(BytecodeArray::kHeaderSize - kHeapObjectTag));
721 // Since the dispatch table root might be set after builtins are generated,
722 // load directly from the roots table.
723 __ LoadRoot(kInterpreterDispatchTableRegister,
724 Heap::kInterpreterTableRootIndex);
725 __ add(kInterpreterDispatchTableRegister,
726 Immediate(FixedArray::kHeaderSize - kHeapObjectTag));
728 // Push context as a stack located parameter to the bytecode handler.
729 DCHECK_EQ(-1, kInterpreterContextSpillSlot);
732 // Dispatch to the first bytecode handler for the function.
733 __ movzx_b(esi, Operand(kInterpreterBytecodeArrayRegister,
734 kInterpreterBytecodeOffsetRegister, times_1, 0));
735 __ mov(esi, Operand(kInterpreterDispatchTableRegister, esi,
736 times_pointer_size, 0));
737 // TODO(rmcilroy): Make dispatch table point to code entrys to avoid untagging
738 // and header removal.
739 __ add(esi, Immediate(Code::kHeaderSize - kHeapObjectTag));
744 void Builtins::Generate_InterpreterExitTrampoline(MacroAssembler* masm) {
745 // TODO(rmcilroy): List of things not currently dealt with here but done in
746 // fullcodegen's EmitReturnSequence.
747 // - Supporting FLAG_trace for Runtime::TraceExit.
748 // - Support profiler (specifically decrementing profiling_counter
749 // appropriately and calling out to HandleInterrupts if necessary).
751 // The return value is in accumulator, which is already in rax.
753 // Leave the frame (also dropping the register file).
756 // Drop receiver + arguments and return.
757 __ mov(ebx, FieldOperand(kInterpreterBytecodeArrayRegister,
758 BytecodeArray::kParameterSizeOffset));
766 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
767 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
768 GenerateTailCallToReturnedCode(masm);
773 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
774 FrameScope scope(masm, StackFrame::INTERNAL);
775 // Push a copy of the function.
777 // Function is also the parameter to the runtime call.
779 // Whether to compile in a background thread.
780 __ Push(masm->isolate()->factory()->ToBoolean(concurrent));
782 __ CallRuntime(Runtime::kCompileOptimized, 2);
788 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
789 CallCompileOptimized(masm, false);
790 GenerateTailCallToReturnedCode(masm);
794 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
795 CallCompileOptimized(masm, true);
796 GenerateTailCallToReturnedCode(masm);
800 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
801 // For now, we are relying on the fact that make_code_young doesn't do any
802 // garbage collection which allows us to save/restore the registers without
803 // worrying about which of them contain pointers. We also don't build an
804 // internal frame to make the code faster, since we shouldn't have to do stack
805 // crawls in MakeCodeYoung. This seems a bit fragile.
807 // Re-execute the code that was patched back to the young age when
809 __ sub(Operand(esp, 0), Immediate(5));
811 __ mov(eax, Operand(esp, 8 * kPointerSize));
813 FrameScope scope(masm, StackFrame::MANUAL);
814 __ PrepareCallCFunction(2, ebx);
815 __ mov(Operand(esp, 1 * kPointerSize),
816 Immediate(ExternalReference::isolate_address(masm->isolate())));
817 __ mov(Operand(esp, 0), eax);
819 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
825 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
826 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
827 MacroAssembler* masm) { \
828 GenerateMakeCodeYoungAgainCommon(masm); \
830 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
831 MacroAssembler* masm) { \
832 GenerateMakeCodeYoungAgainCommon(masm); \
834 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
835 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
838 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
839 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
840 // that make_code_young doesn't do any garbage collection which allows us to
841 // save/restore the registers without worrying about which of them contain
844 __ mov(eax, Operand(esp, 8 * kPointerSize));
845 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
847 FrameScope scope(masm, StackFrame::MANUAL);
848 __ PrepareCallCFunction(2, ebx);
849 __ mov(Operand(esp, 1 * kPointerSize),
850 Immediate(ExternalReference::isolate_address(masm->isolate())));
851 __ mov(Operand(esp, 0), eax);
853 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
858 // Perform prologue operations usually performed by the young code stub.
859 __ pop(eax); // Pop return address into scratch register.
860 __ push(ebp); // Caller's frame pointer.
862 __ push(esi); // Callee's context.
863 __ push(edi); // Callee's JS Function.
864 __ push(eax); // Push return address after frame prologue.
866 // Jump to point after the code-age stub.
871 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
872 GenerateMakeCodeYoungAgainCommon(masm);
876 void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
877 Generate_MarkCodeAsExecutedOnce(masm);
881 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
882 SaveFPRegsMode save_doubles) {
883 // Enter an internal frame.
885 FrameScope scope(masm, StackFrame::INTERNAL);
887 // Preserve registers across notification, this is important for compiled
888 // stubs that tail call the runtime on deopts passing their parameters in
891 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
893 // Tear down internal frame.
896 __ pop(MemOperand(esp, 0)); // Ignore state offset
897 __ ret(0); // Return to IC Miss stub, continuation still on stack.
901 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
902 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
906 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
907 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
911 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
912 Deoptimizer::BailoutType type) {
914 FrameScope scope(masm, StackFrame::INTERNAL);
916 // Pass deoptimization type to the runtime system.
917 __ push(Immediate(Smi::FromInt(static_cast<int>(type))));
918 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
920 // Tear down internal frame.
923 // Get the full codegen state from the stack and untag it.
924 __ mov(ecx, Operand(esp, 1 * kPointerSize));
927 // Switch on the state.
928 Label not_no_registers, not_tos_eax;
929 __ cmp(ecx, FullCodeGenerator::NO_REGISTERS);
930 __ j(not_equal, ¬_no_registers, Label::kNear);
931 __ ret(1 * kPointerSize); // Remove state.
933 __ bind(¬_no_registers);
934 __ mov(eax, Operand(esp, 2 * kPointerSize));
935 __ cmp(ecx, FullCodeGenerator::TOS_REG);
936 __ j(not_equal, ¬_tos_eax, Label::kNear);
937 __ ret(2 * kPointerSize); // Remove state, eax.
939 __ bind(¬_tos_eax);
940 __ Abort(kNoCasesLeft);
944 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
945 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
949 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
950 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
954 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
955 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
959 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
960 Factory* factory = masm->isolate()->factory();
962 // 1. Make sure we have at least one argument.
965 __ j(not_zero, &done);
967 __ push(Immediate(factory->undefined_value()));
973 // 2. Get the function to call (passed as receiver) from the stack, check
974 // if it is a function.
975 Label slow, non_function;
976 // 1 ~ return address.
977 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
978 __ JumpIfSmi(edi, &non_function);
979 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
980 __ j(not_equal, &slow);
983 // 3a. Patch the first argument if necessary when calling a function.
984 Label shift_arguments;
985 __ Move(edx, Immediate(0)); // indicate regular JS_FUNCTION
986 { Label convert_to_object, use_global_proxy, patch_receiver;
987 // Change context eagerly in case we need the global receiver.
988 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
990 // Do not transform the receiver for strict mode functions.
991 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
992 __ test_b(FieldOperand(ebx, SharedFunctionInfo::kStrictModeByteOffset),
993 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
994 __ j(not_equal, &shift_arguments);
996 // Do not transform the receiver for natives (shared already in ebx).
997 __ test_b(FieldOperand(ebx, SharedFunctionInfo::kNativeByteOffset),
998 1 << SharedFunctionInfo::kNativeBitWithinByte);
999 __ j(not_equal, &shift_arguments);
1001 // Compute the receiver in sloppy mode.
1002 __ mov(ebx, Operand(esp, eax, times_4, 0)); // First argument.
1004 // Call ToObject on the receiver if it is not an object, or use the
1005 // global object if it is null or undefined.
1006 __ JumpIfSmi(ebx, &convert_to_object);
1007 __ cmp(ebx, factory->null_value());
1008 __ j(equal, &use_global_proxy);
1009 __ cmp(ebx, factory->undefined_value());
1010 __ j(equal, &use_global_proxy);
1011 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1012 __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx);
1013 __ j(above_equal, &shift_arguments);
1015 __ bind(&convert_to_object);
1017 { // In order to preserve argument count.
1018 FrameScope scope(masm, StackFrame::INTERNAL);
1023 ToObjectStub stub(masm->isolate());
1026 __ Move(edx, Immediate(0)); // restore
1032 // Restore the function to edi.
1033 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
1034 __ jmp(&patch_receiver);
1036 __ bind(&use_global_proxy);
1038 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1039 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalProxyOffset));
1041 __ bind(&patch_receiver);
1042 __ mov(Operand(esp, eax, times_4, 0), ebx);
1044 __ jmp(&shift_arguments);
1047 // 3b. Check for function proxy.
1049 __ Move(edx, Immediate(1)); // indicate function proxy
1050 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
1051 __ j(equal, &shift_arguments);
1052 __ bind(&non_function);
1053 __ Move(edx, Immediate(2)); // indicate non-function
1055 // 3c. Patch the first argument when calling a non-function. The
1056 // CALL_NON_FUNCTION builtin expects the non-function callee as
1057 // receiver, so overwrite the first argument which will ultimately
1058 // become the receiver.
1059 __ mov(Operand(esp, eax, times_4, 0), edi);
1061 // 4. Shift arguments and return address one slot down on the stack
1062 // (overwriting the original receiver). Adjust argument count to make
1063 // the original first argument the new receiver.
1064 __ bind(&shift_arguments);
1068 __ mov(ebx, Operand(esp, ecx, times_4, 0));
1069 __ mov(Operand(esp, ecx, times_4, kPointerSize), ebx);
1071 __ j(not_sign, &loop); // While non-negative (to copy return address).
1072 __ pop(ebx); // Discard copy of return address.
1073 __ dec(eax); // One fewer argument (first argument is new receiver).
1076 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1077 // or a function proxy via CALL_FUNCTION_PROXY.
1078 { Label function, non_proxy;
1080 __ j(zero, &function);
1081 __ Move(ebx, Immediate(0));
1082 __ cmp(edx, Immediate(1));
1083 __ j(not_equal, &non_proxy);
1085 __ pop(edx); // return address
1086 __ push(edi); // re-add proxy object as additional argument
1089 __ GetBuiltinEntry(edx, Context::CALL_FUNCTION_PROXY_BUILTIN_INDEX);
1090 __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1091 RelocInfo::CODE_TARGET);
1093 __ bind(&non_proxy);
1094 __ GetBuiltinEntry(edx, Context::CALL_NON_FUNCTION_BUILTIN_INDEX);
1095 __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1096 RelocInfo::CODE_TARGET);
1100 // 5b. Get the code to call from the function and check that the number of
1101 // expected arguments matches what we're providing. If so, jump
1102 // (tail-call) to the code in register edx without checking arguments.
1103 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1105 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
1106 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
1110 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline());
1112 ParameterCount expected(0);
1113 __ InvokeCode(edx, expected, expected, JUMP_FUNCTION, NullCallWrapper());
1117 static void Generate_PushAppliedArguments(MacroAssembler* masm,
1118 const int argumentsOffset,
1119 const int indexOffset,
1120 const int limitOffset) {
1121 // Copy all arguments from the array to the stack.
1123 Register receiver = LoadDescriptor::ReceiverRegister();
1124 Register key = LoadDescriptor::NameRegister();
1125 Register slot = LoadDescriptor::SlotRegister();
1126 Register vector = LoadWithVectorDescriptor::VectorRegister();
1127 __ mov(key, Operand(ebp, indexOffset));
1130 __ mov(receiver, Operand(ebp, argumentsOffset)); // load arguments
1132 // Use inline caching to speed up access to arguments.
1133 Code::Kind kinds[] = {Code::KEYED_LOAD_IC};
1134 FeedbackVectorSpec spec(0, 1, kinds);
1135 Handle<TypeFeedbackVector> feedback_vector =
1136 masm->isolate()->factory()->NewTypeFeedbackVector(&spec);
1137 int index = feedback_vector->GetIndex(FeedbackVectorICSlot(0));
1138 __ mov(slot, Immediate(Smi::FromInt(index)));
1139 __ mov(vector, Immediate(feedback_vector));
1141 KeyedLoadICStub(masm->isolate(), LoadICState(kNoExtraICState)).GetCode();
1142 __ call(ic, RelocInfo::CODE_TARGET);
1143 // It is important that we do not have a test instruction after the
1144 // call. A test instruction after the call is used to indicate that
1145 // we have generated an inline version of the keyed load. In this
1146 // case, we know that we are not generating a test instruction next.
1148 // Push the nth argument.
1151 // Update the index on the stack and in register key.
1152 __ mov(key, Operand(ebp, indexOffset));
1153 __ add(key, Immediate(1 << kSmiTagSize));
1154 __ mov(Operand(ebp, indexOffset), key);
1157 __ cmp(key, Operand(ebp, limitOffset));
1158 __ j(not_equal, &loop);
1160 // On exit, the pushed arguments count is in eax, untagged
1166 // Used by FunctionApply and ReflectApply
1167 static void Generate_ApplyHelper(MacroAssembler* masm, bool targetIsArgument) {
1168 const int kFormalParameters = targetIsArgument ? 3 : 2;
1169 const int kStackSize = kFormalParameters + 1;
1172 // esp : return address
1173 // esp[4] : arguments
1174 // esp[8] : receiver ("this")
1175 // esp[12] : function
1177 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1179 // ebp : Old base pointer
1180 // ebp[4] : return address
1181 // ebp[8] : function arguments
1182 // ebp[12] : receiver
1183 // ebp[16] : function
1184 static const int kArgumentsOffset = kFPOnStackSize + kPCOnStackSize;
1185 static const int kReceiverOffset = kArgumentsOffset + kPointerSize;
1186 static const int kFunctionOffset = kReceiverOffset + kPointerSize;
1188 __ push(Operand(ebp, kFunctionOffset)); // push this
1189 __ push(Operand(ebp, kArgumentsOffset)); // push arguments
1190 if (targetIsArgument) {
1191 __ InvokeBuiltin(Context::REFLECT_APPLY_PREPARE_BUILTIN_INDEX,
1194 __ InvokeBuiltin(Context::APPLY_PREPARE_BUILTIN_INDEX, CALL_FUNCTION);
1197 Generate_CheckStackOverflow(masm, kFunctionOffset, kEaxIsSmiTagged);
1199 // Push current index and limit.
1200 const int kLimitOffset =
1201 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
1202 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
1203 __ push(eax); // limit
1204 __ push(Immediate(0)); // index
1206 // Get the receiver.
1207 __ mov(ebx, Operand(ebp, kReceiverOffset));
1209 // Check that the function is a JS function (otherwise it must be a proxy).
1210 Label push_receiver, use_global_proxy;
1211 __ mov(edi, Operand(ebp, kFunctionOffset));
1212 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1213 __ j(not_equal, &push_receiver);
1215 // Change context eagerly to get the right global object if necessary.
1216 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1218 // Compute the receiver.
1219 // Do not transform the receiver for strict mode functions.
1220 Label call_to_object;
1221 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1222 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset),
1223 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
1224 __ j(not_equal, &push_receiver);
1226 Factory* factory = masm->isolate()->factory();
1228 // Do not transform the receiver for natives (shared already in ecx).
1229 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset),
1230 1 << SharedFunctionInfo::kNativeBitWithinByte);
1231 __ j(not_equal, &push_receiver);
1233 // Compute the receiver in sloppy mode.
1234 // Call ToObject on the receiver if it is not an object, or use the
1235 // global object if it is null or undefined.
1236 __ JumpIfSmi(ebx, &call_to_object);
1237 __ cmp(ebx, factory->null_value());
1238 __ j(equal, &use_global_proxy);
1239 __ cmp(ebx, factory->undefined_value());
1240 __ j(equal, &use_global_proxy);
1241 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1242 __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx);
1243 __ j(above_equal, &push_receiver);
1245 __ bind(&call_to_object);
1247 ToObjectStub stub(masm->isolate());
1250 __ jmp(&push_receiver);
1252 __ bind(&use_global_proxy);
1254 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1255 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalProxyOffset));
1257 // Push the receiver.
1258 __ bind(&push_receiver);
1261 // Loop over the arguments array, pushing each value to the stack
1262 Generate_PushAppliedArguments(
1263 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1265 // Call the function.
1267 ParameterCount actual(eax);
1268 __ mov(edi, Operand(ebp, kFunctionOffset));
1269 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1270 __ j(not_equal, &call_proxy);
1271 __ InvokeFunction(edi, actual, CALL_FUNCTION, NullCallWrapper());
1273 frame_scope.GenerateLeaveFrame();
1274 __ ret(kStackSize * kPointerSize); // remove this, receiver, and arguments
1276 // Call the function proxy.
1277 __ bind(&call_proxy);
1278 __ push(edi); // add function proxy as last argument
1280 __ Move(ebx, Immediate(0));
1281 __ GetBuiltinEntry(edx, Context::CALL_FUNCTION_PROXY_BUILTIN_INDEX);
1282 __ call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1283 RelocInfo::CODE_TARGET);
1285 // Leave internal frame.
1287 __ ret(kStackSize * kPointerSize); // remove this, receiver, and arguments
1291 // Used by ReflectConstruct
1292 static void Generate_ConstructHelper(MacroAssembler* masm) {
1293 const int kFormalParameters = 3;
1294 const int kStackSize = kFormalParameters + 1;
1297 // esp : return address
1298 // esp[4] : original constructor (new.target)
1299 // esp[8] : arguments
1300 // esp[16] : constructor
1302 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1304 // ebp : Old base pointer
1305 // ebp[4] : return address
1306 // ebp[8] : original constructor (new.target)
1307 // ebp[12] : arguments
1308 // ebp[16] : constructor
1309 static const int kNewTargetOffset = kFPOnStackSize + kPCOnStackSize;
1310 static const int kArgumentsOffset = kNewTargetOffset + kPointerSize;
1311 static const int kFunctionOffset = kArgumentsOffset + kPointerSize;
1313 // If newTarget is not supplied, set it to constructor
1314 Label validate_arguments;
1315 __ mov(eax, Operand(ebp, kNewTargetOffset));
1316 __ CompareRoot(eax, Heap::kUndefinedValueRootIndex);
1317 __ j(not_equal, &validate_arguments, Label::kNear);
1318 __ mov(eax, Operand(ebp, kFunctionOffset));
1319 __ mov(Operand(ebp, kNewTargetOffset), eax);
1321 // Validate arguments
1322 __ bind(&validate_arguments);
1323 __ push(Operand(ebp, kFunctionOffset));
1324 __ push(Operand(ebp, kArgumentsOffset));
1325 __ push(Operand(ebp, kNewTargetOffset));
1326 __ InvokeBuiltin(Context::REFLECT_CONSTRUCT_PREPARE_BUILTIN_INDEX,
1329 Generate_CheckStackOverflow(masm, kFunctionOffset, kEaxIsSmiTagged);
1331 // Push current index and limit.
1332 const int kLimitOffset =
1333 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
1334 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
1335 __ Push(eax); // limit
1336 __ push(Immediate(0)); // index
1337 // Push the constructor function as callee.
1338 __ push(Operand(ebp, kFunctionOffset));
1340 // Loop over the arguments array, pushing each value to the stack
1341 Generate_PushAppliedArguments(
1342 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1344 // Use undefined feedback vector
1345 __ LoadRoot(ebx, Heap::kUndefinedValueRootIndex);
1346 __ mov(edi, Operand(ebp, kFunctionOffset));
1347 __ mov(ecx, Operand(ebp, kNewTargetOffset));
1349 // Call the function.
1350 CallConstructStub stub(masm->isolate(), SUPER_CONSTRUCTOR_CALL);
1351 __ call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
1353 // Leave internal frame.
1355 // remove this, target, arguments, and newTarget
1356 __ ret(kStackSize * kPointerSize);
1360 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1361 Generate_ApplyHelper(masm, false);
1365 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1366 Generate_ApplyHelper(masm, true);
1370 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1371 Generate_ConstructHelper(masm);
1375 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
1376 // ----------- S t a t e -------------
1378 // -- esp[0] : return address
1379 // -- esp[4] : last argument
1380 // -----------------------------------
1381 Label generic_array_code;
1383 // Get the InternalArray function.
1384 __ LoadGlobalFunction(Context::INTERNAL_ARRAY_FUNCTION_INDEX, edi);
1386 if (FLAG_debug_code) {
1387 // Initial map for the builtin InternalArray function should be a map.
1388 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1389 // Will both indicate a NULL and a Smi.
1390 __ test(ebx, Immediate(kSmiTagMask));
1391 __ Assert(not_zero, kUnexpectedInitialMapForInternalArrayFunction);
1392 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1393 __ Assert(equal, kUnexpectedInitialMapForInternalArrayFunction);
1396 // Run the native code for the InternalArray function called as a normal
1399 InternalArrayConstructorStub stub(masm->isolate());
1400 __ TailCallStub(&stub);
1404 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
1405 // ----------- S t a t e -------------
1407 // -- esp[0] : return address
1408 // -- esp[4] : last argument
1409 // -----------------------------------
1410 Label generic_array_code;
1412 // Get the Array function.
1413 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, edi);
1416 if (FLAG_debug_code) {
1417 // Initial map for the builtin Array function should be a map.
1418 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1419 // Will both indicate a NULL and a Smi.
1420 __ test(ebx, Immediate(kSmiTagMask));
1421 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
1422 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1423 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
1426 // Run the native code for the Array function called as a normal function.
1428 __ mov(ebx, masm->isolate()->factory()->undefined_value());
1429 ArrayConstructorStub stub(masm->isolate());
1430 __ TailCallStub(&stub);
1434 void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
1435 // ----------- S t a t e -------------
1436 // -- eax : number of arguments
1437 // -- edi : constructor function
1438 // -- esp[0] : return address
1439 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1440 // -- esp[(argc + 1) * 4] : receiver
1441 // -----------------------------------
1442 Counters* counters = masm->isolate()->counters();
1443 __ IncrementCounter(counters->string_ctor_calls(), 1);
1445 if (FLAG_debug_code) {
1446 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, ecx);
1448 __ Assert(equal, kUnexpectedStringFunction);
1451 // Load the first argument into eax and get rid of the rest
1452 // (including the receiver).
1455 __ j(zero, &no_arguments);
1456 __ mov(ebx, Operand(esp, eax, times_pointer_size, 0));
1458 __ lea(esp, Operand(esp, eax, times_pointer_size, kPointerSize));
1462 // Lookup the argument in the number to string cache.
1463 Label not_cached, argument_is_string;
1464 __ LookupNumberStringCache(eax, // Input.
1469 __ IncrementCounter(counters->string_ctor_cached_number(), 1);
1470 __ bind(&argument_is_string);
1471 // ----------- S t a t e -------------
1472 // -- ebx : argument converted to string
1473 // -- edi : constructor function
1474 // -- esp[0] : return address
1475 // -----------------------------------
1477 // Allocate a JSValue and put the tagged pointer into eax.
1479 __ Allocate(JSValue::kSize,
1481 ecx, // New allocation top (we ignore it).
1487 __ LoadGlobalFunctionInitialMap(edi, ecx);
1488 if (FLAG_debug_code) {
1489 __ cmpb(FieldOperand(ecx, Map::kInstanceSizeOffset),
1490 JSValue::kSize >> kPointerSizeLog2);
1491 __ Assert(equal, kUnexpectedStringWrapperInstanceSize);
1492 __ cmpb(FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset), 0);
1493 __ Assert(equal, kUnexpectedUnusedPropertiesOfStringWrapper);
1495 __ mov(FieldOperand(eax, HeapObject::kMapOffset), ecx);
1497 // Set properties and elements.
1498 Factory* factory = masm->isolate()->factory();
1499 __ Move(ecx, Immediate(factory->empty_fixed_array()));
1500 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
1501 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ecx);
1504 __ mov(FieldOperand(eax, JSValue::kValueOffset), ebx);
1506 // Ensure the object is fully initialized.
1507 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
1509 // We're done. Return.
1512 // The argument was not found in the number to string cache. Check
1513 // if it's a string already before calling the conversion builtin.
1514 Label convert_argument;
1515 __ bind(¬_cached);
1516 STATIC_ASSERT(kSmiTag == 0);
1517 __ JumpIfSmi(eax, &convert_argument);
1518 Condition is_string = masm->IsObjectStringType(eax, ebx, ecx);
1519 __ j(NegateCondition(is_string), &convert_argument);
1521 __ IncrementCounter(counters->string_ctor_string_value(), 1);
1522 __ jmp(&argument_is_string);
1524 // Invoke the conversion builtin and put the result into ebx.
1525 __ bind(&convert_argument);
1526 __ IncrementCounter(counters->string_ctor_conversions(), 1);
1528 FrameScope scope(masm, StackFrame::INTERNAL);
1529 __ push(edi); // Preserve the function.
1530 ToStringStub stub(masm->isolate());
1535 __ jmp(&argument_is_string);
1537 // Load the empty string into ebx, remove the receiver from the
1538 // stack, and jump back to the case where the argument is a string.
1539 __ bind(&no_arguments);
1540 __ Move(ebx, Immediate(factory->empty_string()));
1542 __ lea(esp, Operand(esp, kPointerSize));
1544 __ jmp(&argument_is_string);
1546 // At this point the argument is already a string. Call runtime to
1547 // create a string wrapper.
1548 __ bind(&gc_required);
1549 __ IncrementCounter(counters->string_ctor_gc_required(), 1);
1551 FrameScope scope(masm, StackFrame::INTERNAL);
1553 __ CallRuntime(Runtime::kNewStringWrapper, 1);
1559 static void ArgumentsAdaptorStackCheck(MacroAssembler* masm,
1560 Label* stack_overflow) {
1561 // ----------- S t a t e -------------
1562 // -- eax : actual number of arguments
1563 // -- ebx : expected number of arguments
1564 // -- edi : function (passed through to callee)
1565 // -----------------------------------
1566 // Check the stack for overflow. We are not trying to catch
1567 // interruptions (e.g. debug break and preemption) here, so the "real stack
1568 // limit" is checked.
1569 ExternalReference real_stack_limit =
1570 ExternalReference::address_of_real_stack_limit(masm->isolate());
1571 __ mov(edx, Operand::StaticVariable(real_stack_limit));
1572 // Make ecx the space we have left. The stack might already be overflowed
1573 // here which will cause ecx to become negative.
1576 // Make edx the space we need for the array when it is unrolled onto the
1579 __ shl(edx, kPointerSizeLog2);
1580 // Check if the arguments will overflow the stack.
1582 __ j(less_equal, stack_overflow); // Signed comparison.
1586 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1590 // Store the arguments adaptor context sentinel.
1591 __ push(Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1593 // Push the function on the stack.
1596 // Preserve the number of arguments on the stack. Must preserve eax,
1597 // ebx and ecx because these registers are used when copying the
1598 // arguments and the receiver.
1599 STATIC_ASSERT(kSmiTagSize == 1);
1600 __ lea(edi, Operand(eax, eax, times_1, kSmiTag));
1605 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1606 // Retrieve the number of arguments from the stack.
1607 __ mov(ebx, Operand(ebp, ArgumentsAdaptorFrameConstants::kLengthOffset));
1612 // Remove caller arguments from the stack.
1613 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
1615 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
1620 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1621 // ----------- S t a t e -------------
1622 // -- eax : actual number of arguments
1623 // -- ebx : expected number of arguments
1624 // -- edi : function (passed through to callee)
1625 // -----------------------------------
1627 Label invoke, dont_adapt_arguments;
1628 __ IncrementCounter(masm->isolate()->counters()->arguments_adaptors(), 1);
1630 Label stack_overflow;
1631 ArgumentsAdaptorStackCheck(masm, &stack_overflow);
1633 Label enough, too_few;
1634 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
1636 __ j(less, &too_few);
1637 __ cmp(ebx, SharedFunctionInfo::kDontAdaptArgumentsSentinel);
1638 __ j(equal, &dont_adapt_arguments);
1640 { // Enough parameters: Actual >= expected.
1642 EnterArgumentsAdaptorFrame(masm);
1644 // Copy receiver and all expected arguments.
1645 const int offset = StandardFrameConstants::kCallerSPOffset;
1646 __ lea(edi, Operand(ebp, eax, times_4, offset));
1647 __ mov(eax, -1); // account for receiver
1652 __ push(Operand(edi, 0));
1653 __ sub(edi, Immediate(kPointerSize));
1656 // eax now contains the expected number of arguments.
1660 { // Too few parameters: Actual < expected.
1663 // If the function is strong we need to throw an error.
1664 Label no_strong_error;
1665 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1666 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrongModeByteOffset),
1667 1 << SharedFunctionInfo::kStrongModeBitWithinByte);
1668 __ j(equal, &no_strong_error, Label::kNear);
1670 // What we really care about is the required number of arguments.
1671 __ mov(ecx, FieldOperand(ecx, SharedFunctionInfo::kLengthOffset));
1674 __ j(greater_equal, &no_strong_error, Label::kNear);
1677 FrameScope frame(masm, StackFrame::MANUAL);
1678 EnterArgumentsAdaptorFrame(masm);
1679 __ CallRuntime(Runtime::kThrowStrongModeTooFewArguments, 0);
1682 __ bind(&no_strong_error);
1683 EnterArgumentsAdaptorFrame(masm);
1685 // Remember expected arguments in ecx.
1688 // Copy receiver and all actual arguments.
1689 const int offset = StandardFrameConstants::kCallerSPOffset;
1690 __ lea(edi, Operand(ebp, eax, times_4, offset));
1691 // ebx = expected - actual.
1693 // eax = -actual - 1
1695 __ sub(eax, Immediate(1));
1700 __ push(Operand(edi, 0));
1701 __ sub(edi, Immediate(kPointerSize));
1703 __ j(not_zero, ©);
1705 // Fill remaining expected arguments with undefined values.
1709 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
1713 // Restore expected arguments.
1717 // Call the entry point.
1719 // Restore function pointer.
1720 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1721 // eax : expected number of arguments
1722 // edi : function (passed through to callee)
1725 // Store offset of return address for deoptimizer.
1726 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1728 // Leave frame and return.
1729 LeaveArgumentsAdaptorFrame(masm);
1732 // -------------------------------------------
1733 // Dont adapt arguments.
1734 // -------------------------------------------
1735 __ bind(&dont_adapt_arguments);
1738 __ bind(&stack_overflow);
1740 FrameScope frame(masm, StackFrame::MANUAL);
1741 EnterArgumentsAdaptorFrame(masm);
1742 __ InvokeBuiltin(Context::STACK_OVERFLOW_BUILTIN_INDEX, CALL_FUNCTION);
1748 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1749 // Lookup the function in the JavaScript frame.
1750 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1752 FrameScope scope(masm, StackFrame::INTERNAL);
1753 // Pass function as argument.
1755 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1759 // If the code object is null, just return to the unoptimized code.
1760 __ cmp(eax, Immediate(0));
1761 __ j(not_equal, &skip, Label::kNear);
1766 // Load deoptimization data from the code object.
1767 __ mov(ebx, Operand(eax, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1769 // Load the OSR entrypoint offset from the deoptimization data.
1770 __ mov(ebx, Operand(ebx, FixedArray::OffsetOfElementAt(
1771 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1774 // Compute the target address = code_obj + header_size + osr_offset
1775 __ lea(eax, Operand(eax, ebx, times_1, Code::kHeaderSize - kHeapObjectTag));
1777 // Overwrite the return address on the stack.
1778 __ mov(Operand(esp, 0), eax);
1780 // And "return" to the OSR entry point of the function.
1785 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1786 // We check the stack limit as indicator that recompilation might be done.
1788 ExternalReference stack_limit =
1789 ExternalReference::address_of_stack_limit(masm->isolate());
1790 __ cmp(esp, Operand::StaticVariable(stack_limit));
1791 __ j(above_equal, &ok, Label::kNear);
1793 FrameScope scope(masm, StackFrame::INTERNAL);
1794 __ CallRuntime(Runtime::kStackGuard, 0);
1796 __ jmp(masm->isolate()->builtins()->OnStackReplacement(),
1797 RelocInfo::CODE_TARGET);
1804 } // namespace internal
1807 #endif // V8_TARGET_ARCH_IA32