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.
9 #include "src/code-factory.h"
10 #include "src/codegen.h"
11 #include "src/deoptimizer.h"
12 #include "src/full-codegen.h"
18 #define __ ACCESS_MASM(masm)
21 void Builtins::Generate_Adaptor(MacroAssembler* masm,
23 BuiltinExtraArguments extra_args) {
24 // ----------- S t a t e -------------
25 // -- eax : number of arguments excluding receiver
26 // -- edi : called function (only guaranteed when
27 // extra_args requires it)
29 // -- esp[0] : return address
30 // -- esp[4] : last argument
32 // -- esp[4 * argc] : first argument (argc == eax)
33 // -- esp[4 * (argc +1)] : receiver
34 // -----------------------------------
36 // Insert extra arguments.
37 int num_extra_args = 0;
38 if (extra_args == NEEDS_CALLED_FUNCTION) {
40 Register scratch = ebx;
41 __ pop(scratch); // Save return address.
43 __ push(scratch); // Restore return address.
45 DCHECK(extra_args == NO_EXTRA_ARGUMENTS);
48 // JumpToExternalReference expects eax to contain the number of arguments
49 // including the receiver and the extra arguments.
50 __ add(eax, Immediate(num_extra_args + 1));
51 __ JumpToExternalReference(ExternalReference(id, masm->isolate()));
55 static void CallRuntimePassFunction(
56 MacroAssembler* masm, Runtime::FunctionId function_id) {
57 FrameScope scope(masm, StackFrame::INTERNAL);
58 // Push a copy of the function.
60 // Function is also the parameter to the runtime call.
63 __ CallRuntime(function_id, 1);
69 static void GenerateTailCallToSharedCode(MacroAssembler* masm) {
70 __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
71 __ mov(eax, FieldOperand(eax, SharedFunctionInfo::kCodeOffset));
72 __ lea(eax, FieldOperand(eax, Code::kHeaderSize));
77 static void GenerateTailCallToReturnedCode(MacroAssembler* masm) {
78 __ lea(eax, FieldOperand(eax, Code::kHeaderSize));
83 void Builtins::Generate_InOptimizationQueue(MacroAssembler* masm) {
84 // Checking whether the queued function is ready for install is optional,
85 // since we come across interrupts and stack checks elsewhere. However,
86 // not checking may delay installing ready functions, and always checking
87 // would be quite expensive. A good compromise is to first check against
88 // stack limit as a cue for an interrupt signal.
90 ExternalReference stack_limit =
91 ExternalReference::address_of_stack_limit(masm->isolate());
92 __ cmp(esp, Operand::StaticVariable(stack_limit));
93 __ j(above_equal, &ok, Label::kNear);
95 CallRuntimePassFunction(masm, Runtime::kTryInstallOptimizedCode);
96 GenerateTailCallToReturnedCode(masm);
99 GenerateTailCallToSharedCode(masm);
103 static void Generate_Runtime_NewObject(MacroAssembler* masm,
105 Register original_constructor,
106 Label* count_incremented,
109 if (create_memento) {
110 // Get the cell or allocation site.
111 __ mov(edi, Operand(esp, kPointerSize * 2));
113 offset = kPointerSize;
116 // Must restore esi (context) and edi (constructor) before calling
118 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
119 __ mov(edi, Operand(esp, offset));
121 __ push(original_constructor);
122 if (create_memento) {
123 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 3);
125 __ CallRuntime(Runtime::kNewObject, 2);
127 __ mov(ebx, eax); // store result in ebx
129 // Runtime_NewObjectWithAllocationSite increments allocation count.
130 // Skip the increment.
131 if (create_memento) {
132 __ jmp(count_incremented);
139 static void Generate_JSConstructStubHelper(MacroAssembler* masm,
140 bool is_api_function,
141 bool create_memento) {
142 // ----------- S t a t e -------------
143 // -- eax: number of arguments
144 // -- edi: constructor function
145 // -- ebx: allocation site or undefined
146 // -- edx: original constructor
147 // -----------------------------------
149 // Should never create mementos for api functions.
150 DCHECK(!is_api_function || !create_memento);
152 // Enter a construct frame.
154 FrameScope scope(masm, StackFrame::CONSTRUCT);
156 if (create_memento) {
157 __ AssertUndefinedOrAllocationSite(ebx);
161 // Store a smi-tagged arguments count on the stack.
165 // Push the function to invoke on the stack.
170 Label count_incremented;
172 __ j(equal, &normal_new);
174 // Original constructor and function are different.
175 Generate_Runtime_NewObject(masm, create_memento, edx, &count_incremented,
177 __ bind(&normal_new);
179 // Try to allocate the object without transitioning into C code. If any of
180 // the preconditions is not met, the code bails out to the runtime call.
182 if (FLAG_inline_new) {
183 Label undo_allocation;
184 ExternalReference debug_step_in_fp =
185 ExternalReference::debug_step_in_fp_address(masm->isolate());
186 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
187 __ j(not_equal, &rt_call);
189 // Verified that the constructor is a JSFunction.
190 // Load the initial map and verify that it is in fact a map.
192 __ mov(eax, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
193 // Will both indicate a NULL and a Smi
194 __ JumpIfSmi(eax, &rt_call);
196 // eax: initial map (if proven valid below)
197 __ CmpObjectType(eax, MAP_TYPE, ebx);
198 __ j(not_equal, &rt_call);
200 // Check that the constructor is not constructing a JSFunction (see
201 // comments in Runtime_NewObject in runtime.cc). In which case the
202 // initial map's instance type would be JS_FUNCTION_TYPE.
205 __ CmpInstanceType(eax, JS_FUNCTION_TYPE);
206 __ j(equal, &rt_call);
208 if (!is_api_function) {
210 // The code below relies on these assumptions.
211 STATIC_ASSERT(Map::Counter::kShift + Map::Counter::kSize == 32);
212 // Check if slack tracking is enabled.
213 __ mov(esi, FieldOperand(eax, Map::kBitField3Offset));
214 __ shr(esi, Map::Counter::kShift);
215 __ cmp(esi, Map::kSlackTrackingCounterEnd);
216 __ j(less, &allocate);
217 // Decrease generous allocation count.
218 __ sub(FieldOperand(eax, Map::kBitField3Offset),
219 Immediate(1 << Map::Counter::kShift));
221 __ cmp(esi, Map::kSlackTrackingCounterEnd);
222 __ j(not_equal, &allocate);
227 __ push(edi); // constructor
228 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
232 __ mov(esi, Map::kSlackTrackingCounterEnd - 1);
237 // Now allocate the JSObject on the heap.
240 __ movzx_b(edi, FieldOperand(eax, Map::kInstanceSizeOffset));
241 __ shl(edi, kPointerSizeLog2);
242 if (create_memento) {
243 __ add(edi, Immediate(AllocationMemento::kSize));
246 __ Allocate(edi, ebx, edi, no_reg, &rt_call, NO_ALLOCATION_FLAGS);
248 Factory* factory = masm->isolate()->factory();
250 // Allocated the JSObject, now initialize the fields.
253 // edi: start of next object (including memento if create_memento)
254 __ mov(Operand(ebx, JSObject::kMapOffset), eax);
255 __ mov(ecx, factory->empty_fixed_array());
256 __ mov(Operand(ebx, JSObject::kPropertiesOffset), ecx);
257 __ mov(Operand(ebx, JSObject::kElementsOffset), ecx);
258 // Set extra fields in the newly allocated object.
261 // edi: start of next object (including memento if create_memento)
262 // esi: slack tracking counter (non-API function case)
263 __ mov(edx, factory->undefined_value());
264 __ lea(ecx, Operand(ebx, JSObject::kHeaderSize));
265 if (!is_api_function) {
266 Label no_inobject_slack_tracking;
268 // Check if slack tracking is enabled.
269 __ cmp(esi, Map::kSlackTrackingCounterEnd);
270 __ j(less, &no_inobject_slack_tracking);
272 // Allocate object with a slack.
274 FieldOperand(eax, Map::kPreAllocatedPropertyFieldsOffset));
276 Operand(ebx, esi, times_pointer_size, JSObject::kHeaderSize));
277 // esi: offset of first field after pre-allocated fields
278 if (FLAG_debug_code) {
280 __ Assert(less_equal,
281 kUnexpectedNumberOfPreAllocatedPropertyFields);
283 __ InitializeFieldsWithFiller(ecx, esi, edx);
284 __ mov(edx, factory->one_pointer_filler_map());
285 // Fill the remaining fields with one pointer filler map.
287 __ bind(&no_inobject_slack_tracking);
290 if (create_memento) {
291 __ lea(esi, Operand(edi, -AllocationMemento::kSize));
292 __ InitializeFieldsWithFiller(ecx, esi, edx);
294 // Fill in memento fields if necessary.
295 // esi: points to the allocated but uninitialized memento.
296 __ mov(Operand(esi, AllocationMemento::kMapOffset),
297 factory->allocation_memento_map());
298 // Get the cell or undefined.
299 __ mov(edx, Operand(esp, kPointerSize*2));
300 __ mov(Operand(esi, AllocationMemento::kAllocationSiteOffset),
303 __ InitializeFieldsWithFiller(ecx, edi, edx);
306 // Add the object tag to make the JSObject real, so that we can continue
307 // and jump into the continuation code at any time from now on. Any
308 // failures need to undo the allocation, so that the heap is in a
309 // consistent state and verifiable.
312 // edi: start of next object
313 __ or_(ebx, Immediate(kHeapObjectTag));
315 // Check if a non-empty properties array is needed.
316 // Allocate and initialize a FixedArray if it is.
319 // edi: start of next object
320 // Calculate the total number of properties described by the map.
321 __ movzx_b(edx, FieldOperand(eax, Map::kUnusedPropertyFieldsOffset));
323 FieldOperand(eax, Map::kPreAllocatedPropertyFieldsOffset));
325 // Calculate unused properties past the end of the in-object properties.
326 __ movzx_b(ecx, FieldOperand(eax, Map::kInObjectPropertiesOffset));
328 // Done if no extra properties are to be allocated.
329 __ j(zero, &allocated);
330 __ Assert(positive, kPropertyAllocationCountFailed);
332 // Scale the number of elements by pointer size and add the header for
333 // FixedArrays to the start of the next object calculation from above.
335 // edi: start of next object (will be start of FixedArray)
336 // edx: number of elements in properties array
337 __ Allocate(FixedArray::kHeaderSize,
340 REGISTER_VALUE_IS_INT32,
345 RESULT_CONTAINS_TOP);
347 // Initialize the FixedArray.
350 // edx: number of elements
351 // ecx: start of next object
352 __ mov(eax, factory->fixed_array_map());
353 __ mov(Operand(edi, FixedArray::kMapOffset), eax); // setup the map
355 __ mov(Operand(edi, FixedArray::kLengthOffset), edx); // and length
357 // Initialize the fields to undefined.
360 // ecx: start of next object
362 __ mov(edx, factory->undefined_value());
363 __ lea(eax, Operand(edi, FixedArray::kHeaderSize));
366 __ mov(Operand(eax, 0), edx);
367 __ add(eax, Immediate(kPointerSize));
373 // Store the initialized FixedArray into the properties field of
377 __ or_(edi, Immediate(kHeapObjectTag)); // add the heap tag
378 __ mov(FieldOperand(ebx, JSObject::kPropertiesOffset), edi);
381 // Continue with JSObject being successfully allocated
385 // Undo the setting of the new top so that the heap is verifiable. For
386 // example, the map's unused properties potentially do not match the
387 // allocated objects unused properties.
388 // ebx: JSObject (previous new top)
389 __ bind(&undo_allocation);
390 __ UndoAllocationInNewSpace(ebx);
393 // Allocate the new receiver object using the runtime call.
395 Generate_Runtime_NewObject(masm, create_memento, edi, &count_incremented,
397 // New object allocated.
398 // ebx: newly allocated object
401 if (create_memento) {
402 __ mov(ecx, Operand(esp, kPointerSize * 2));
403 __ cmp(ecx, masm->isolate()->factory()->undefined_value());
404 __ j(equal, &count_incremented);
405 // ecx is an AllocationSite. We are creating a memento from it, so we
406 // need to increment the memento create count.
407 __ add(FieldOperand(ecx, AllocationSite::kPretenureCreateCountOffset),
408 Immediate(Smi::FromInt(1)));
409 __ bind(&count_incremented);
412 // Retrieve the function from the stack.
415 // Retrieve smi-tagged arguments count from the stack.
416 __ mov(eax, Operand(esp, 0));
419 // Push the allocated receiver to the stack. We need two copies
420 // because we may have to return the original one and the calling
421 // conventions dictate that the called function pops the receiver.
425 // Set up pointer to last argument.
426 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
428 // Copy arguments and receiver to the expression stack.
433 __ push(Operand(ebx, ecx, times_4, 0));
436 __ j(greater_equal, &loop);
438 // Call the function.
439 if (is_api_function) {
440 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
442 masm->isolate()->builtins()->HandleApiCallConstruct();
443 __ call(code, RelocInfo::CODE_TARGET);
445 ParameterCount actual(eax);
446 __ InvokeFunction(edi, actual, CALL_FUNCTION,
450 // Store offset of return address for deoptimizer.
451 if (!is_api_function) {
452 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
455 // Restore context from the frame.
456 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
458 // If the result is an object (in the ECMA sense), we should get rid
459 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
461 Label use_receiver, exit;
463 // If the result is a smi, it is *not* an object in the ECMA sense.
464 __ JumpIfSmi(eax, &use_receiver);
466 // If the type of the result (stored in its map) is less than
467 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
468 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
469 __ j(above_equal, &exit);
471 // Throw away the result of the constructor invocation and use the
472 // on-stack receiver as the result.
473 __ bind(&use_receiver);
474 __ mov(eax, Operand(esp, 0));
476 // Restore the arguments count and leave the construct frame.
478 __ mov(ebx, Operand(esp, kPointerSize)); // Get arguments count.
480 // Leave construct frame.
483 // Remove caller arguments from the stack and return.
484 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
486 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
488 __ IncrementCounter(masm->isolate()->counters()->constructed_objects(), 1);
493 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
494 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
498 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
499 Generate_JSConstructStubHelper(masm, true, false);
503 void Builtins::Generate_JSConstructStubForDerived(MacroAssembler* masm) {
504 // ----------- S t a t e -------------
505 // -- eax: number of arguments
506 // -- edi: constructor function
507 // -- ebx: allocation site or undefined
508 // -- edx: original constructor
509 // -----------------------------------
511 // TODO(dslomov): support pretenuring
512 CHECK(!FLAG_pretenuring_call_new);
515 FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
517 // Preserve actual arguments count.
525 // receiver is the hole.
526 __ push(Immediate(masm->isolate()->factory()->the_hole_value()));
528 // Set up pointer to last argument.
529 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
531 // Copy arguments and receiver to the expression stack.
536 __ push(Operand(ebx, ecx, times_4, 0));
539 __ j(greater_equal, &loop);
541 __ inc(eax); // Pushed new.target.
546 ExternalReference debug_step_in_fp =
547 ExternalReference::debug_step_in_fp_address(masm->isolate());
548 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
549 __ j(equal, &skip_step_in);
554 __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
558 __ bind(&skip_step_in);
561 ParameterCount actual(eax);
562 __ InvokeFunction(edi, actual, CALL_FUNCTION, NullCallWrapper());
564 // Restore context from the frame.
565 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
567 __ mov(ebx, Operand(esp, 0));
570 __ pop(ecx); // Return address.
571 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize));
577 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
579 ProfileEntryHookStub::MaybeCallEntryHook(masm);
581 // Clear the context before we push it when entering the internal frame.
582 __ Move(esi, Immediate(0));
585 FrameScope scope(masm, StackFrame::INTERNAL);
587 // Load the previous frame pointer (ebx) to access C arguments
588 __ mov(ebx, Operand(ebp, 0));
590 // Get the function from the frame and setup the context.
591 __ mov(ecx, Operand(ebx, EntryFrameConstants::kFunctionArgOffset));
592 __ mov(esi, FieldOperand(ecx, JSFunction::kContextOffset));
594 // Push the function and the receiver onto the stack.
596 __ push(Operand(ebx, EntryFrameConstants::kReceiverArgOffset));
598 // Load the number of arguments and setup pointer to the arguments.
599 __ mov(eax, Operand(ebx, EntryFrameConstants::kArgcOffset));
600 __ mov(ebx, Operand(ebx, EntryFrameConstants::kArgvOffset));
602 // Copy arguments to the stack in a loop.
604 __ Move(ecx, Immediate(0));
607 __ mov(edx, Operand(ebx, ecx, times_4, 0)); // push parameter from argv
608 __ push(Operand(edx, 0)); // dereference handle
612 __ j(not_equal, &loop);
614 // Get the function from the stack and call it.
615 // kPointerSize for the receiver.
616 __ mov(edi, Operand(esp, eax, times_4, kPointerSize));
620 // No type feedback cell is available
621 __ mov(ebx, masm->isolate()->factory()->undefined_value());
622 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
625 ParameterCount actual(eax);
626 __ InvokeFunction(edi, actual, CALL_FUNCTION,
630 // Exit the internal frame. Notice that this also removes the empty.
631 // context and the function left on the stack by the code
634 __ ret(kPointerSize); // Remove receiver.
638 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
639 Generate_JSEntryTrampolineHelper(masm, false);
643 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
644 Generate_JSEntryTrampolineHelper(masm, true);
648 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
649 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
650 GenerateTailCallToReturnedCode(masm);
655 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
656 FrameScope scope(masm, StackFrame::INTERNAL);
657 // Push a copy of the function.
659 // Function is also the parameter to the runtime call.
661 // Whether to compile in a background thread.
662 __ Push(masm->isolate()->factory()->ToBoolean(concurrent));
664 __ CallRuntime(Runtime::kCompileOptimized, 2);
670 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
671 CallCompileOptimized(masm, false);
672 GenerateTailCallToReturnedCode(masm);
676 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
677 CallCompileOptimized(masm, true);
678 GenerateTailCallToReturnedCode(masm);
682 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
683 // For now, we are relying on the fact that make_code_young doesn't do any
684 // garbage collection which allows us to save/restore the registers without
685 // worrying about which of them contain pointers. We also don't build an
686 // internal frame to make the code faster, since we shouldn't have to do stack
687 // crawls in MakeCodeYoung. This seems a bit fragile.
689 // Re-execute the code that was patched back to the young age when
691 __ sub(Operand(esp, 0), Immediate(5));
693 __ mov(eax, Operand(esp, 8 * kPointerSize));
695 FrameScope scope(masm, StackFrame::MANUAL);
696 __ PrepareCallCFunction(2, ebx);
697 __ mov(Operand(esp, 1 * kPointerSize),
698 Immediate(ExternalReference::isolate_address(masm->isolate())));
699 __ mov(Operand(esp, 0), eax);
701 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
707 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
708 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
709 MacroAssembler* masm) { \
710 GenerateMakeCodeYoungAgainCommon(masm); \
712 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
713 MacroAssembler* masm) { \
714 GenerateMakeCodeYoungAgainCommon(masm); \
716 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
717 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
720 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
721 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
722 // that make_code_young doesn't do any garbage collection which allows us to
723 // save/restore the registers without worrying about which of them contain
726 __ mov(eax, Operand(esp, 8 * kPointerSize));
727 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
729 FrameScope scope(masm, StackFrame::MANUAL);
730 __ PrepareCallCFunction(2, ebx);
731 __ mov(Operand(esp, 1 * kPointerSize),
732 Immediate(ExternalReference::isolate_address(masm->isolate())));
733 __ mov(Operand(esp, 0), eax);
735 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
740 // Perform prologue operations usually performed by the young code stub.
741 __ pop(eax); // Pop return address into scratch register.
742 __ push(ebp); // Caller's frame pointer.
744 __ push(esi); // Callee's context.
745 __ push(edi); // Callee's JS Function.
746 __ push(eax); // Push return address after frame prologue.
748 // Jump to point after the code-age stub.
753 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
754 GenerateMakeCodeYoungAgainCommon(masm);
758 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
759 SaveFPRegsMode save_doubles) {
760 // Enter an internal frame.
762 FrameScope scope(masm, StackFrame::INTERNAL);
764 // Preserve registers across notification, this is important for compiled
765 // stubs that tail call the runtime on deopts passing their parameters in
768 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
770 // Tear down internal frame.
773 __ pop(MemOperand(esp, 0)); // Ignore state offset
774 __ ret(0); // Return to IC Miss stub, continuation still on stack.
778 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
779 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
783 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
784 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
788 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
789 Deoptimizer::BailoutType type) {
791 FrameScope scope(masm, StackFrame::INTERNAL);
793 // Pass deoptimization type to the runtime system.
794 __ push(Immediate(Smi::FromInt(static_cast<int>(type))));
795 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
797 // Tear down internal frame.
800 // Get the full codegen state from the stack and untag it.
801 __ mov(ecx, Operand(esp, 1 * kPointerSize));
804 // Switch on the state.
805 Label not_no_registers, not_tos_eax;
806 __ cmp(ecx, FullCodeGenerator::NO_REGISTERS);
807 __ j(not_equal, ¬_no_registers, Label::kNear);
808 __ ret(1 * kPointerSize); // Remove state.
810 __ bind(¬_no_registers);
811 __ mov(eax, Operand(esp, 2 * kPointerSize));
812 __ cmp(ecx, FullCodeGenerator::TOS_REG);
813 __ j(not_equal, ¬_tos_eax, Label::kNear);
814 __ ret(2 * kPointerSize); // Remove state, eax.
816 __ bind(¬_tos_eax);
817 __ Abort(kNoCasesLeft);
821 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
822 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
826 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
827 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
831 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
832 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
836 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
837 Factory* factory = masm->isolate()->factory();
839 // 1. Make sure we have at least one argument.
842 __ j(not_zero, &done);
844 __ push(Immediate(factory->undefined_value()));
850 // 2. Get the function to call (passed as receiver) from the stack, check
851 // if it is a function.
852 Label slow, non_function;
853 // 1 ~ return address.
854 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
855 __ JumpIfSmi(edi, &non_function);
856 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
857 __ j(not_equal, &slow);
860 // 3a. Patch the first argument if necessary when calling a function.
861 Label shift_arguments;
862 __ Move(edx, Immediate(0)); // indicate regular JS_FUNCTION
863 { Label convert_to_object, use_global_proxy, patch_receiver;
864 // Change context eagerly in case we need the global receiver.
865 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
867 // Do not transform the receiver for strict mode functions.
868 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
869 __ test_b(FieldOperand(ebx, SharedFunctionInfo::kStrictModeByteOffset),
870 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
871 __ j(not_equal, &shift_arguments);
873 // Do not transform the receiver for natives (shared already in ebx).
874 __ test_b(FieldOperand(ebx, SharedFunctionInfo::kNativeByteOffset),
875 1 << SharedFunctionInfo::kNativeBitWithinByte);
876 __ j(not_equal, &shift_arguments);
878 // Compute the receiver in sloppy mode.
879 __ mov(ebx, Operand(esp, eax, times_4, 0)); // First argument.
881 // Call ToObject on the receiver if it is not an object, or use the
882 // global object if it is null or undefined.
883 __ JumpIfSmi(ebx, &convert_to_object);
884 __ cmp(ebx, factory->null_value());
885 __ j(equal, &use_global_proxy);
886 __ cmp(ebx, factory->undefined_value());
887 __ j(equal, &use_global_proxy);
888 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
889 __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx);
890 __ j(above_equal, &shift_arguments);
892 __ bind(&convert_to_object);
894 { // In order to preserve argument count.
895 FrameScope scope(masm, StackFrame::INTERNAL);
900 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
902 __ Move(edx, Immediate(0)); // restore
908 // Restore the function to edi.
909 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
910 __ jmp(&patch_receiver);
912 __ bind(&use_global_proxy);
914 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
915 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalProxyOffset));
917 __ bind(&patch_receiver);
918 __ mov(Operand(esp, eax, times_4, 0), ebx);
920 __ jmp(&shift_arguments);
923 // 3b. Check for function proxy.
925 __ Move(edx, Immediate(1)); // indicate function proxy
926 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
927 __ j(equal, &shift_arguments);
928 __ bind(&non_function);
929 __ Move(edx, Immediate(2)); // indicate non-function
931 // 3c. Patch the first argument when calling a non-function. The
932 // CALL_NON_FUNCTION builtin expects the non-function callee as
933 // receiver, so overwrite the first argument which will ultimately
934 // become the receiver.
935 __ mov(Operand(esp, eax, times_4, 0), edi);
937 // 4. Shift arguments and return address one slot down on the stack
938 // (overwriting the original receiver). Adjust argument count to make
939 // the original first argument the new receiver.
940 __ bind(&shift_arguments);
944 __ mov(ebx, Operand(esp, ecx, times_4, 0));
945 __ mov(Operand(esp, ecx, times_4, kPointerSize), ebx);
947 __ j(not_sign, &loop); // While non-negative (to copy return address).
948 __ pop(ebx); // Discard copy of return address.
949 __ dec(eax); // One fewer argument (first argument is new receiver).
952 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
953 // or a function proxy via CALL_FUNCTION_PROXY.
954 { Label function, non_proxy;
956 __ j(zero, &function);
957 __ Move(ebx, Immediate(0));
958 __ cmp(edx, Immediate(1));
959 __ j(not_equal, &non_proxy);
961 __ pop(edx); // return address
962 __ push(edi); // re-add proxy object as additional argument
965 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
966 __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
967 RelocInfo::CODE_TARGET);
970 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
971 __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
972 RelocInfo::CODE_TARGET);
976 // 5b. Get the code to call from the function and check that the number of
977 // expected arguments matches what we're providing. If so, jump
978 // (tail-call) to the code in register edx without checking arguments.
979 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
981 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
982 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
986 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline());
988 ParameterCount expected(0);
989 __ InvokeCode(edx, expected, expected, JUMP_FUNCTION, NullCallWrapper());
993 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
994 static const int kArgumentsOffset = 2 * kPointerSize;
995 static const int kReceiverOffset = 3 * kPointerSize;
996 static const int kFunctionOffset = 4 * kPointerSize;
998 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1000 __ push(Operand(ebp, kFunctionOffset)); // push this
1001 __ push(Operand(ebp, kArgumentsOffset)); // push arguments
1002 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1004 // Check the stack for overflow. We are not trying to catch
1005 // interruptions (e.g. debug break and preemption) here, so the "real stack
1006 // limit" is checked.
1008 ExternalReference real_stack_limit =
1009 ExternalReference::address_of_real_stack_limit(masm->isolate());
1010 __ mov(edi, Operand::StaticVariable(real_stack_limit));
1011 // Make ecx the space we have left. The stack might already be overflowed
1012 // here which will cause ecx to become negative.
1015 // Make edx the space we need for the array when it is unrolled onto the
1018 __ shl(edx, kPointerSizeLog2 - kSmiTagSize);
1019 // Check if the arguments will overflow the stack.
1021 __ j(greater, &okay); // Signed comparison.
1023 // Out of stack space.
1024 __ push(Operand(ebp, 4 * kPointerSize)); // push this
1026 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1028 // End of stack check.
1030 // Push current index and limit.
1031 const int kLimitOffset =
1032 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
1033 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
1034 __ push(eax); // limit
1035 __ push(Immediate(0)); // index
1037 // Get the receiver.
1038 __ mov(ebx, Operand(ebp, kReceiverOffset));
1040 // Check that the function is a JS function (otherwise it must be a proxy).
1041 Label push_receiver, use_global_proxy;
1042 __ mov(edi, Operand(ebp, kFunctionOffset));
1043 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1044 __ j(not_equal, &push_receiver);
1046 // Change context eagerly to get the right global object if necessary.
1047 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1049 // Compute the receiver.
1050 // Do not transform the receiver for strict mode functions.
1051 Label call_to_object;
1052 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1053 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset),
1054 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
1055 __ j(not_equal, &push_receiver);
1057 Factory* factory = masm->isolate()->factory();
1059 // Do not transform the receiver for natives (shared already in ecx).
1060 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset),
1061 1 << SharedFunctionInfo::kNativeBitWithinByte);
1062 __ j(not_equal, &push_receiver);
1064 // Compute the receiver in sloppy mode.
1065 // Call ToObject on the receiver if it is not an object, or use the
1066 // global object if it is null or undefined.
1067 __ JumpIfSmi(ebx, &call_to_object);
1068 __ cmp(ebx, factory->null_value());
1069 __ j(equal, &use_global_proxy);
1070 __ cmp(ebx, factory->undefined_value());
1071 __ j(equal, &use_global_proxy);
1072 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1073 __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx);
1074 __ j(above_equal, &push_receiver);
1076 __ bind(&call_to_object);
1078 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1080 __ jmp(&push_receiver);
1082 __ bind(&use_global_proxy);
1084 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1085 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalProxyOffset));
1087 // Push the receiver.
1088 __ bind(&push_receiver);
1091 // Copy all arguments from the array to the stack.
1093 Register receiver = LoadDescriptor::ReceiverRegister();
1094 Register key = LoadDescriptor::NameRegister();
1095 __ mov(key, Operand(ebp, kIndexOffset));
1098 __ mov(receiver, Operand(ebp, kArgumentsOffset)); // load arguments
1100 if (FLAG_vector_ics) {
1101 // TODO(mvstanton): Vector-based ics need additional infrastructure to
1102 // be embedded here. For now, just call the runtime.
1105 __ CallRuntime(Runtime::kGetProperty, 2);
1107 // Use inline caching to speed up access to arguments.
1108 Handle<Code> ic = CodeFactory::KeyedLoadIC(masm->isolate()).code();
1109 __ call(ic, RelocInfo::CODE_TARGET);
1110 // It is important that we do not have a test instruction after the
1111 // call. A test instruction after the call is used to indicate that
1112 // we have generated an inline version of the keyed load. In this
1113 // case, we know that we are not generating a test instruction next.
1116 // Push the nth argument.
1119 // Update the index on the stack and in register key.
1120 __ mov(key, Operand(ebp, kIndexOffset));
1121 __ add(key, Immediate(1 << kSmiTagSize));
1122 __ mov(Operand(ebp, kIndexOffset), key);
1125 __ cmp(key, Operand(ebp, kLimitOffset));
1126 __ j(not_equal, &loop);
1128 // Call the function.
1130 ParameterCount actual(eax);
1133 __ mov(edi, Operand(ebp, kFunctionOffset));
1134 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1135 __ j(not_equal, &call_proxy);
1136 __ InvokeFunction(edi, actual, CALL_FUNCTION, NullCallWrapper());
1138 frame_scope.GenerateLeaveFrame();
1139 __ ret(3 * kPointerSize); // remove this, receiver, and arguments
1141 // Call the function proxy.
1142 __ bind(&call_proxy);
1143 __ push(edi); // add function proxy as last argument
1145 __ Move(ebx, Immediate(0));
1146 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
1147 __ call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1148 RelocInfo::CODE_TARGET);
1150 // Leave internal frame.
1152 __ ret(3 * kPointerSize); // remove this, receiver, and arguments
1156 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
1157 // ----------- S t a t e -------------
1159 // -- esp[0] : return address
1160 // -- esp[4] : last argument
1161 // -----------------------------------
1162 Label generic_array_code;
1164 // Get the InternalArray function.
1165 __ LoadGlobalFunction(Context::INTERNAL_ARRAY_FUNCTION_INDEX, edi);
1167 if (FLAG_debug_code) {
1168 // Initial map for the builtin InternalArray function should be a map.
1169 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1170 // Will both indicate a NULL and a Smi.
1171 __ test(ebx, Immediate(kSmiTagMask));
1172 __ Assert(not_zero, kUnexpectedInitialMapForInternalArrayFunction);
1173 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1174 __ Assert(equal, kUnexpectedInitialMapForInternalArrayFunction);
1177 // Run the native code for the InternalArray function called as a normal
1180 InternalArrayConstructorStub stub(masm->isolate());
1181 __ TailCallStub(&stub);
1185 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
1186 // ----------- S t a t e -------------
1188 // -- esp[0] : return address
1189 // -- esp[4] : last argument
1190 // -----------------------------------
1191 Label generic_array_code;
1193 // Get the Array function.
1194 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, edi);
1197 if (FLAG_debug_code) {
1198 // Initial map for the builtin Array function should be a map.
1199 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1200 // Will both indicate a NULL and a Smi.
1201 __ test(ebx, Immediate(kSmiTagMask));
1202 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
1203 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1204 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
1207 // Run the native code for the Array function called as a normal function.
1209 __ mov(ebx, masm->isolate()->factory()->undefined_value());
1210 ArrayConstructorStub stub(masm->isolate());
1211 __ TailCallStub(&stub);
1215 void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
1216 // ----------- S t a t e -------------
1217 // -- eax : number of arguments
1218 // -- edi : constructor function
1219 // -- esp[0] : return address
1220 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1221 // -- esp[(argc + 1) * 4] : receiver
1222 // -----------------------------------
1223 Counters* counters = masm->isolate()->counters();
1224 __ IncrementCounter(counters->string_ctor_calls(), 1);
1226 if (FLAG_debug_code) {
1227 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, ecx);
1229 __ Assert(equal, kUnexpectedStringFunction);
1232 // Load the first argument into eax and get rid of the rest
1233 // (including the receiver).
1236 __ j(zero, &no_arguments);
1237 __ mov(ebx, Operand(esp, eax, times_pointer_size, 0));
1239 __ lea(esp, Operand(esp, eax, times_pointer_size, kPointerSize));
1243 // Lookup the argument in the number to string cache.
1244 Label not_cached, argument_is_string;
1245 __ LookupNumberStringCache(eax, // Input.
1250 __ IncrementCounter(counters->string_ctor_cached_number(), 1);
1251 __ bind(&argument_is_string);
1252 // ----------- S t a t e -------------
1253 // -- ebx : argument converted to string
1254 // -- edi : constructor function
1255 // -- esp[0] : return address
1256 // -----------------------------------
1258 // Allocate a JSValue and put the tagged pointer into eax.
1260 __ Allocate(JSValue::kSize,
1262 ecx, // New allocation top (we ignore it).
1268 __ LoadGlobalFunctionInitialMap(edi, ecx);
1269 if (FLAG_debug_code) {
1270 __ cmpb(FieldOperand(ecx, Map::kInstanceSizeOffset),
1271 JSValue::kSize >> kPointerSizeLog2);
1272 __ Assert(equal, kUnexpectedStringWrapperInstanceSize);
1273 __ cmpb(FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset), 0);
1274 __ Assert(equal, kUnexpectedUnusedPropertiesOfStringWrapper);
1276 __ mov(FieldOperand(eax, HeapObject::kMapOffset), ecx);
1278 // Set properties and elements.
1279 Factory* factory = masm->isolate()->factory();
1280 __ Move(ecx, Immediate(factory->empty_fixed_array()));
1281 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
1282 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ecx);
1285 __ mov(FieldOperand(eax, JSValue::kValueOffset), ebx);
1287 // Ensure the object is fully initialized.
1288 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
1290 // We're done. Return.
1293 // The argument was not found in the number to string cache. Check
1294 // if it's a string already before calling the conversion builtin.
1295 Label convert_argument;
1296 __ bind(¬_cached);
1297 STATIC_ASSERT(kSmiTag == 0);
1298 __ JumpIfSmi(eax, &convert_argument);
1299 Condition is_string = masm->IsObjectStringType(eax, ebx, ecx);
1300 __ j(NegateCondition(is_string), &convert_argument);
1302 __ IncrementCounter(counters->string_ctor_string_value(), 1);
1303 __ jmp(&argument_is_string);
1305 // Invoke the conversion builtin and put the result into ebx.
1306 __ bind(&convert_argument);
1307 __ IncrementCounter(counters->string_ctor_conversions(), 1);
1309 FrameScope scope(masm, StackFrame::INTERNAL);
1310 __ push(edi); // Preserve the function.
1312 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
1316 __ jmp(&argument_is_string);
1318 // Load the empty string into ebx, remove the receiver from the
1319 // stack, and jump back to the case where the argument is a string.
1320 __ bind(&no_arguments);
1321 __ Move(ebx, Immediate(factory->empty_string()));
1323 __ lea(esp, Operand(esp, kPointerSize));
1325 __ jmp(&argument_is_string);
1327 // At this point the argument is already a string. Call runtime to
1328 // create a string wrapper.
1329 __ bind(&gc_required);
1330 __ IncrementCounter(counters->string_ctor_gc_required(), 1);
1332 FrameScope scope(masm, StackFrame::INTERNAL);
1334 __ CallRuntime(Runtime::kNewStringWrapper, 1);
1340 static void ArgumentsAdaptorStackCheck(MacroAssembler* masm,
1341 Label* stack_overflow) {
1342 // ----------- S t a t e -------------
1343 // -- eax : actual number of arguments
1344 // -- ebx : expected number of arguments
1345 // -- edi : function (passed through to callee)
1346 // -----------------------------------
1347 // Check the stack for overflow. We are not trying to catch
1348 // interruptions (e.g. debug break and preemption) here, so the "real stack
1349 // limit" is checked.
1350 ExternalReference real_stack_limit =
1351 ExternalReference::address_of_real_stack_limit(masm->isolate());
1352 __ mov(edx, Operand::StaticVariable(real_stack_limit));
1353 // Make ecx the space we have left. The stack might already be overflowed
1354 // here which will cause ecx to become negative.
1357 // Make edx the space we need for the array when it is unrolled onto the
1360 __ shl(edx, kPointerSizeLog2);
1361 // Check if the arguments will overflow the stack.
1363 __ j(less_equal, stack_overflow); // Signed comparison.
1367 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1371 // Store the arguments adaptor context sentinel.
1372 __ push(Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1374 // Push the function on the stack.
1377 // Preserve the number of arguments on the stack. Must preserve eax,
1378 // ebx and ecx because these registers are used when copying the
1379 // arguments and the receiver.
1380 STATIC_ASSERT(kSmiTagSize == 1);
1381 __ lea(edi, Operand(eax, eax, times_1, kSmiTag));
1386 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1387 // Retrieve the number of arguments from the stack.
1388 __ mov(ebx, Operand(ebp, ArgumentsAdaptorFrameConstants::kLengthOffset));
1393 // Remove caller arguments from the stack.
1394 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
1396 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
1401 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1402 // ----------- S t a t e -------------
1403 // -- eax : actual number of arguments
1404 // -- ebx : expected number of arguments
1405 // -- edi : function (passed through to callee)
1406 // -----------------------------------
1408 Label invoke, dont_adapt_arguments;
1409 __ IncrementCounter(masm->isolate()->counters()->arguments_adaptors(), 1);
1411 Label stack_overflow;
1412 ArgumentsAdaptorStackCheck(masm, &stack_overflow);
1414 Label enough, too_few;
1415 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
1417 __ j(less, &too_few);
1418 __ cmp(ebx, SharedFunctionInfo::kDontAdaptArgumentsSentinel);
1419 __ j(equal, &dont_adapt_arguments);
1421 { // Enough parameters: Actual >= expected.
1423 EnterArgumentsAdaptorFrame(masm);
1425 // Copy receiver and all expected arguments.
1426 const int offset = StandardFrameConstants::kCallerSPOffset;
1427 __ lea(eax, Operand(ebp, eax, times_4, offset));
1428 __ mov(edi, -1); // account for receiver
1433 __ push(Operand(eax, 0));
1434 __ sub(eax, Immediate(kPointerSize));
1440 { // Too few parameters: Actual < expected.
1442 EnterArgumentsAdaptorFrame(masm);
1444 // Copy receiver and all actual arguments.
1445 const int offset = StandardFrameConstants::kCallerSPOffset;
1446 __ lea(edi, Operand(ebp, eax, times_4, offset));
1447 // ebx = expected - actual.
1449 // eax = -actual - 1
1451 __ sub(eax, Immediate(1));
1456 __ push(Operand(edi, 0));
1457 __ sub(edi, Immediate(kPointerSize));
1459 __ j(not_zero, ©);
1461 // Fill remaining expected arguments with undefined values.
1465 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
1470 // Call the entry point.
1472 // Restore function pointer.
1473 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1476 // Store offset of return address for deoptimizer.
1477 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1479 // Leave frame and return.
1480 LeaveArgumentsAdaptorFrame(masm);
1483 // -------------------------------------------
1484 // Dont adapt arguments.
1485 // -------------------------------------------
1486 __ bind(&dont_adapt_arguments);
1489 __ bind(&stack_overflow);
1491 FrameScope frame(masm, StackFrame::MANUAL);
1492 EnterArgumentsAdaptorFrame(masm);
1493 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1499 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1500 // Lookup the function in the JavaScript frame.
1501 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1503 FrameScope scope(masm, StackFrame::INTERNAL);
1504 // Pass function as argument.
1506 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1510 // If the code object is null, just return to the unoptimized code.
1511 __ cmp(eax, Immediate(0));
1512 __ j(not_equal, &skip, Label::kNear);
1517 // Load deoptimization data from the code object.
1518 __ mov(ebx, Operand(eax, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1520 // Load the OSR entrypoint offset from the deoptimization data.
1521 __ mov(ebx, Operand(ebx, FixedArray::OffsetOfElementAt(
1522 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1525 // Compute the target address = code_obj + header_size + osr_offset
1526 __ lea(eax, Operand(eax, ebx, times_1, Code::kHeaderSize - kHeapObjectTag));
1528 // Overwrite the return address on the stack.
1529 __ mov(Operand(esp, 0), eax);
1531 // And "return" to the OSR entry point of the function.
1536 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1537 // We check the stack limit as indicator that recompilation might be done.
1539 ExternalReference stack_limit =
1540 ExternalReference::address_of_stack_limit(masm->isolate());
1541 __ cmp(esp, Operand::StaticVariable(stack_limit));
1542 __ j(above_equal, &ok, Label::kNear);
1544 FrameScope scope(masm, StackFrame::INTERNAL);
1545 __ CallRuntime(Runtime::kStackGuard, 0);
1547 __ jmp(masm->isolate()->builtins()->OnStackReplacement(),
1548 RelocInfo::CODE_TARGET);
1556 } // namespace v8::internal
1558 #endif // V8_TARGET_ARCH_X87