1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #if V8_TARGET_ARCH_IA32
9 #include "src/code-factory.h"
10 #include "src/codegen.h"
11 #include "src/deoptimizer.h"
12 #include "src/full-codegen/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_JSConstructStubHelper(MacroAssembler* masm,
104 bool is_api_function,
105 bool create_memento) {
106 // ----------- S t a t e -------------
107 // -- eax: number of arguments
108 // -- edi: constructor function
109 // -- ebx: allocation site or undefined
110 // -- edx: original constructor
111 // -----------------------------------
113 // Should never create mementos for api functions.
114 DCHECK(!is_api_function || !create_memento);
116 // Enter a construct frame.
118 FrameScope scope(masm, StackFrame::CONSTRUCT);
120 // Preserve the incoming parameters on the stack.
121 __ AssertUndefinedOrAllocationSite(ebx);
128 // Try to allocate the object without transitioning into C code. If any of
129 // the preconditions is not met, the code bails out to the runtime call.
130 Label rt_call, allocated;
131 if (FLAG_inline_new) {
132 ExternalReference debug_step_in_fp =
133 ExternalReference::debug_step_in_fp_address(masm->isolate());
134 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
135 __ j(not_equal, &rt_call);
137 // Fall back to runtime if the original constructor and function differ.
139 __ j(not_equal, &rt_call);
141 // Verified that the constructor is a JSFunction.
142 // Load the initial map and verify that it is in fact a map.
144 __ mov(eax, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
145 // Will both indicate a NULL and a Smi
146 __ JumpIfSmi(eax, &rt_call);
148 // eax: initial map (if proven valid below)
149 __ CmpObjectType(eax, MAP_TYPE, ebx);
150 __ j(not_equal, &rt_call);
152 // Check that the constructor is not constructing a JSFunction (see
153 // comments in Runtime_NewObject in runtime.cc). In which case the
154 // initial map's instance type would be JS_FUNCTION_TYPE.
157 __ CmpInstanceType(eax, JS_FUNCTION_TYPE);
158 __ j(equal, &rt_call);
160 if (!is_api_function) {
162 // The code below relies on these assumptions.
163 STATIC_ASSERT(Map::Counter::kShift + Map::Counter::kSize == 32);
164 // Check if slack tracking is enabled.
165 __ mov(esi, FieldOperand(eax, Map::kBitField3Offset));
166 __ shr(esi, Map::Counter::kShift);
167 __ cmp(esi, Map::kSlackTrackingCounterEnd);
168 __ j(less, &allocate);
169 // Decrease generous allocation count.
170 __ sub(FieldOperand(eax, Map::kBitField3Offset),
171 Immediate(1 << Map::Counter::kShift));
173 __ cmp(esi, Map::kSlackTrackingCounterEnd);
174 __ j(not_equal, &allocate);
180 __ push(edi); // constructor
181 __ CallRuntime(Runtime::kFinalizeInstanceSize, 1);
186 __ mov(esi, Map::kSlackTrackingCounterEnd - 1);
191 // Now allocate the JSObject on the heap.
194 __ movzx_b(edi, FieldOperand(eax, Map::kInstanceSizeOffset));
195 __ shl(edi, kPointerSizeLog2);
196 if (create_memento) {
197 __ add(edi, Immediate(AllocationMemento::kSize));
200 __ Allocate(edi, ebx, edi, no_reg, &rt_call, NO_ALLOCATION_FLAGS);
202 Factory* factory = masm->isolate()->factory();
204 // Allocated the JSObject, now initialize the fields.
207 // edi: start of next object (including memento if create_memento)
208 __ mov(Operand(ebx, JSObject::kMapOffset), eax);
209 __ mov(ecx, factory->empty_fixed_array());
210 __ mov(Operand(ebx, JSObject::kPropertiesOffset), ecx);
211 __ mov(Operand(ebx, JSObject::kElementsOffset), ecx);
212 // Set extra fields in the newly allocated object.
215 // edi: start of next object (including memento if create_memento)
216 // esi: slack tracking counter (non-API function case)
217 __ mov(edx, factory->undefined_value());
218 __ lea(ecx, Operand(ebx, JSObject::kHeaderSize));
219 if (!is_api_function) {
220 Label no_inobject_slack_tracking;
222 // Check if slack tracking is enabled.
223 __ cmp(esi, Map::kSlackTrackingCounterEnd);
224 __ j(less, &no_inobject_slack_tracking);
226 // Allocate object with a slack.
227 __ movzx_b(esi, FieldOperand(eax, Map::kInObjectPropertiesOffset));
228 __ movzx_b(eax, FieldOperand(eax, Map::kUnusedPropertyFieldsOffset));
231 Operand(ebx, esi, times_pointer_size, JSObject::kHeaderSize));
232 // esi: offset of first field after pre-allocated fields
233 if (FLAG_debug_code) {
235 __ Assert(less_equal,
236 kUnexpectedNumberOfPreAllocatedPropertyFields);
238 __ InitializeFieldsWithFiller(ecx, esi, edx);
239 __ mov(edx, factory->one_pointer_filler_map());
240 // Fill the remaining fields with one pointer filler map.
242 __ bind(&no_inobject_slack_tracking);
245 if (create_memento) {
246 __ lea(esi, Operand(edi, -AllocationMemento::kSize));
247 __ InitializeFieldsWithFiller(ecx, esi, edx);
249 // Fill in memento fields if necessary.
250 // esi: points to the allocated but uninitialized memento.
251 __ mov(Operand(esi, AllocationMemento::kMapOffset),
252 factory->allocation_memento_map());
253 // Get the cell or undefined.
254 __ mov(edx, Operand(esp, 3 * kPointerSize));
255 __ AssertUndefinedOrAllocationSite(edx);
256 __ mov(Operand(esi, AllocationMemento::kAllocationSiteOffset),
259 __ InitializeFieldsWithFiller(ecx, edi, edx);
262 // Add the object tag to make the JSObject real, so that we can continue
263 // and jump into the continuation code at any time from now on.
264 // ebx: JSObject (untagged)
265 __ or_(ebx, Immediate(kHeapObjectTag));
267 // Continue with JSObject being successfully allocated
268 // ebx: JSObject (tagged)
272 // Allocate the new receiver object using the runtime call.
273 // edx: original constructor
275 int offset = kPointerSize;
276 if (create_memento) {
277 // Get the cell or allocation site.
278 __ mov(edi, Operand(esp, kPointerSize * 3));
279 __ push(edi); // argument 1: allocation site
280 offset += kPointerSize;
283 // Must restore esi (context) and edi (constructor) before calling
285 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
286 __ mov(edi, Operand(esp, offset));
287 __ push(edi); // argument 2/1: constructor function
288 __ push(edx); // argument 3/2: original constructor
289 if (create_memento) {
290 __ CallRuntime(Runtime::kNewObjectWithAllocationSite, 3);
292 __ CallRuntime(Runtime::kNewObject, 2);
294 __ mov(ebx, eax); // store result in ebx
296 // Runtime_NewObjectWithAllocationSite increments allocation count.
297 // Skip the increment.
298 Label count_incremented;
299 if (create_memento) {
300 __ jmp(&count_incremented);
303 // New object allocated.
304 // ebx: newly allocated object
307 if (create_memento) {
308 __ mov(ecx, Operand(esp, 3 * kPointerSize));
309 __ cmp(ecx, masm->isolate()->factory()->undefined_value());
310 __ j(equal, &count_incremented);
311 // ecx is an AllocationSite. We are creating a memento from it, so we
312 // need to increment the memento create count.
313 __ add(FieldOperand(ecx, AllocationSite::kPretenureCreateCountOffset),
314 Immediate(Smi::FromInt(1)));
315 __ bind(&count_incremented);
318 // Restore the parameters.
319 __ pop(edx); // new.target
320 __ pop(edi); // Constructor function.
322 // Retrieve smi-tagged arguments count from the stack.
323 __ mov(eax, Operand(esp, 0));
326 // Push new.target onto the construct frame. This is stored just below the
327 // receiver on the stack.
330 // Push the allocated receiver to the stack. We need two copies
331 // because we may have to return the original one and the calling
332 // conventions dictate that the called function pops the receiver.
336 // Set up pointer to last argument.
337 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
339 // Copy arguments and receiver to the expression stack.
344 __ push(Operand(ebx, ecx, times_4, 0));
347 __ j(greater_equal, &loop);
349 // Call the function.
350 if (is_api_function) {
351 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
353 masm->isolate()->builtins()->HandleApiCallConstruct();
354 __ call(code, RelocInfo::CODE_TARGET);
356 ParameterCount actual(eax);
357 __ InvokeFunction(edi, actual, CALL_FUNCTION,
361 // Store offset of return address for deoptimizer.
362 if (!is_api_function) {
363 masm->isolate()->heap()->SetConstructStubDeoptPCOffset(masm->pc_offset());
366 // Restore context from the frame.
367 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
369 // If the result is an object (in the ECMA sense), we should get rid
370 // of the receiver and use the result; see ECMA-262 section 13.2.2-7
372 Label use_receiver, exit;
374 // If the result is a smi, it is *not* an object in the ECMA sense.
375 __ JumpIfSmi(eax, &use_receiver);
377 // If the type of the result (stored in its map) is less than
378 // FIRST_SPEC_OBJECT_TYPE, it is not an object in the ECMA sense.
379 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
380 __ j(above_equal, &exit);
382 // Throw away the result of the constructor invocation and use the
383 // on-stack receiver as the result.
384 __ bind(&use_receiver);
385 __ mov(eax, Operand(esp, 0));
387 // Restore the arguments count and leave the construct frame. The arguments
388 // count is stored below the reciever and the new.target.
390 __ mov(ebx, Operand(esp, 2 * kPointerSize));
392 // Leave construct frame.
395 // Remove caller arguments from the stack and return.
396 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
398 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
400 __ IncrementCounter(masm->isolate()->counters()->constructed_objects(), 1);
405 void Builtins::Generate_JSConstructStubGeneric(MacroAssembler* masm) {
406 Generate_JSConstructStubHelper(masm, false, FLAG_pretenuring_call_new);
410 void Builtins::Generate_JSConstructStubApi(MacroAssembler* masm) {
411 Generate_JSConstructStubHelper(masm, true, false);
415 void Builtins::Generate_JSConstructStubForDerived(MacroAssembler* masm) {
416 // ----------- S t a t e -------------
417 // -- eax: number of arguments
418 // -- edi: constructor function
419 // -- ebx: allocation site or undefined
420 // -- edx: original constructor
421 // -----------------------------------
424 FrameScope frame_scope(masm, StackFrame::CONSTRUCT);
426 // Preserve allocation site.
427 __ AssertUndefinedOrAllocationSite(ebx);
430 // Preserve actual arguments count.
438 // receiver is the hole.
439 __ push(Immediate(masm->isolate()->factory()->the_hole_value()));
441 // Set up pointer to last argument.
442 __ lea(ebx, Operand(ebp, StandardFrameConstants::kCallerSPOffset));
444 // Copy arguments and receiver to the expression stack.
449 __ push(Operand(ebx, ecx, times_4, 0));
452 __ j(greater_equal, &loop);
456 ExternalReference debug_step_in_fp =
457 ExternalReference::debug_step_in_fp_address(masm->isolate());
458 __ cmp(Operand::StaticVariable(debug_step_in_fp), Immediate(0));
459 __ j(equal, &skip_step_in);
464 __ CallRuntime(Runtime::kHandleStepInForDerivedConstructors, 1);
468 __ bind(&skip_step_in);
471 ParameterCount actual(eax);
472 __ InvokeFunction(edi, actual, CALL_FUNCTION, NullCallWrapper());
474 // Restore context from the frame.
475 __ mov(esi, Operand(ebp, StandardFrameConstants::kContextOffset));
477 // Get arguments count, skipping over new.target.
478 __ mov(ebx, Operand(esp, kPointerSize));
481 __ pop(ecx); // Return address.
482 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize));
488 enum IsTagged { kEaxIsSmiTagged, kEaxIsUntaggedInt };
491 // Clobbers ecx, edx, edi; preserves all other registers.
492 static void Generate_CheckStackOverflow(MacroAssembler* masm,
493 const int calleeOffset,
494 IsTagged eax_is_tagged) {
495 // eax : the number of items to be pushed to the stack
497 // Check the stack for overflow. We are not trying to catch
498 // interruptions (e.g. debug break and preemption) here, so the "real stack
499 // limit" is checked.
501 ExternalReference real_stack_limit =
502 ExternalReference::address_of_real_stack_limit(masm->isolate());
503 __ mov(edi, Operand::StaticVariable(real_stack_limit));
504 // Make ecx the space we have left. The stack might already be overflowed
505 // here which will cause ecx to become negative.
508 // Make edx the space we need for the array when it is unrolled onto the
511 int smi_tag = eax_is_tagged == kEaxIsSmiTagged ? kSmiTagSize : 0;
512 __ shl(edx, kPointerSizeLog2 - smi_tag);
513 // Check if the arguments will overflow the stack.
515 __ j(greater, &okay); // Signed comparison.
517 // Out of stack space.
518 __ push(Operand(ebp, calleeOffset)); // push this
519 if (eax_is_tagged == kEaxIsUntaggedInt) {
523 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
529 static void Generate_JSEntryTrampolineHelper(MacroAssembler* masm,
531 ProfileEntryHookStub::MaybeCallEntryHook(masm);
533 // Clear the context before we push it when entering the internal frame.
534 __ Move(esi, Immediate(0));
537 FrameScope scope(masm, StackFrame::INTERNAL);
539 // Load the previous frame pointer (ebx) to access C arguments
540 __ mov(ebx, Operand(ebp, 0));
542 // Get the function from the frame and setup the context.
543 __ mov(ecx, Operand(ebx, EntryFrameConstants::kFunctionArgOffset));
544 __ mov(esi, FieldOperand(ecx, JSFunction::kContextOffset));
546 // Push the function and the receiver onto the stack.
548 __ push(Operand(ebx, EntryFrameConstants::kReceiverArgOffset));
550 // Load the number of arguments and setup pointer to the arguments.
551 __ mov(eax, Operand(ebx, EntryFrameConstants::kArgcOffset));
552 __ mov(ebx, Operand(ebx, EntryFrameConstants::kArgvOffset));
554 // Check if we have enough stack space to push all arguments.
555 // The function is the first thing that was pushed above after entering
556 // the internal frame.
557 const int kFunctionOffset =
558 InternalFrameConstants::kCodeOffset - kPointerSize;
559 // Expects argument count in eax. Clobbers ecx, edx, edi.
560 Generate_CheckStackOverflow(masm, kFunctionOffset, kEaxIsUntaggedInt);
562 // Copy arguments to the stack in a loop.
564 __ Move(ecx, Immediate(0));
567 __ mov(edx, Operand(ebx, ecx, times_4, 0)); // push parameter from argv
568 __ push(Operand(edx, 0)); // dereference handle
572 __ j(not_equal, &loop);
574 // Get the function from the stack and call it.
575 // kPointerSize for the receiver.
576 __ mov(edi, Operand(esp, eax, times_4, kPointerSize));
580 // No type feedback cell is available
581 __ mov(ebx, masm->isolate()->factory()->undefined_value());
582 CallConstructStub stub(masm->isolate(), NO_CALL_CONSTRUCTOR_FLAGS);
585 ParameterCount actual(eax);
586 __ InvokeFunction(edi, actual, CALL_FUNCTION,
590 // Exit the internal frame. Notice that this also removes the empty.
591 // context and the function left on the stack by the code
594 __ ret(kPointerSize); // Remove receiver.
598 void Builtins::Generate_JSEntryTrampoline(MacroAssembler* masm) {
599 Generate_JSEntryTrampolineHelper(masm, false);
603 void Builtins::Generate_JSConstructEntryTrampoline(MacroAssembler* masm) {
604 Generate_JSEntryTrampolineHelper(masm, true);
608 // Generate code for entering a JS function with the interpreter.
609 // On entry to the function the receiver and arguments have been pushed on the
610 // stack left to right. The actual argument count matches the formal parameter
611 // count expected by the function.
613 // The live registers are:
614 // o edi: the JS function object being called
615 // o esi: our context
616 // o ebp: the caller's frame pointer
617 // o esp: stack pointer (pointing to return address)
619 // The function builds a JS frame. Please see JavaScriptFrameConstants in
620 // frames-ia32.h for its layout.
621 // TODO(rmcilroy): We will need to include the current bytecode pointer in the
623 void Builtins::Generate_InterpreterEntryTrampoline(MacroAssembler* masm) {
624 // Open a frame scope to indicate that there is a frame on the stack. The
625 // MANUAL indicates that the scope shouldn't actually generate code to set up
626 // the frame (that is done below).
627 FrameScope frame_scope(masm, StackFrame::MANUAL);
628 __ push(ebp); // Caller's frame pointer.
630 __ push(esi); // Callee's context.
631 __ push(edi); // Callee's JS function.
633 // Get the bytecode array from the function object and load the pointer to the
634 // first entry into edi (InterpreterBytecodeRegister).
635 __ mov(edi, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
636 __ mov(edi, FieldOperand(edi, SharedFunctionInfo::kFunctionDataOffset));
638 if (FLAG_debug_code) {
639 // Check function data field is actually a BytecodeArray object.
640 __ AssertNotSmi(edi);
641 __ CmpObjectType(edi, BYTECODE_ARRAY_TYPE, eax);
642 __ Assert(equal, kFunctionDataShouldBeBytecodeArrayOnInterpreterEntry);
645 // Allocate the local and temporary register file on the stack.
647 // Load frame size from the BytecodeArray object.
648 __ mov(ebx, FieldOperand(edi, BytecodeArray::kFrameSizeOffset));
650 // Do a stack check to ensure we don't go over the limit.
654 ExternalReference stack_limit =
655 ExternalReference::address_of_real_stack_limit(masm->isolate());
656 __ cmp(ecx, Operand::StaticVariable(stack_limit));
657 __ j(above_equal, &ok, Label::kNear);
658 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
661 // If ok, push undefined as the initial value for all register file entries.
662 // Note: there should always be at least one stack slot for the return
663 // register in the register file.
665 __ mov(eax, Immediate(masm->isolate()->factory()->undefined_value()));
666 __ bind(&loop_header);
667 // TODO(rmcilroy): Consider doing more than one push per loop iteration.
669 // Continue loop if not done.
670 __ sub(ebx, Immediate(kPointerSize));
671 __ j(not_equal, &loop_header, Label::kNear);
674 // TODO(rmcilroy): List of things not currently dealt with here but done in
675 // fullcodegen's prologue:
676 // - Support profiler (specifically profiling_counter).
677 // - Call ProfileEntryHookStub when isolate has a function_entry_hook.
678 // - Allow simulator stop operations if FLAG_stop_at is set.
679 // - Deal with sloppy mode functions which need to replace the
680 // receiver with the global proxy when called as functions (without an
681 // explicit receiver object).
682 // - Code aging of the BytecodeArray object.
683 // - Supporting FLAG_trace.
685 // The following items are also not done here, and will probably be done using
686 // explicit bytecodes instead:
687 // - Allocating a new local context if applicable.
688 // - Setting up a local binding to the this function, which is used in
689 // derived constructors with super calls.
690 // - Setting new.target if required.
691 // - Dealing with REST parameters (only if
692 // https://codereview.chromium.org/1235153006 doesn't land by then).
693 // - Dealing with argument objects.
695 // Perform stack guard check.
698 ExternalReference stack_limit =
699 ExternalReference::address_of_stack_limit(masm->isolate());
700 __ cmp(esp, Operand::StaticVariable(stack_limit));
701 __ j(above_equal, &ok, Label::kNear);
702 __ CallRuntime(Runtime::kStackGuard, 0);
706 // Load bytecode offset and dispatch table into registers.
707 __ mov(ecx, Immediate(BytecodeArray::kHeaderSize - kHeapObjectTag));
708 // Since the dispatch table root might be set after builtins are generated,
709 // load directly from the roots table.
710 __ LoadRoot(ebx, Heap::kInterpreterTableRootIndex);
711 __ add(ebx, Immediate(FixedArray::kHeaderSize - kHeapObjectTag));
713 // Dispatch to the first bytecode handler for the function.
714 __ movzx_b(eax, Operand(edi, ecx, times_1, 0));
715 __ mov(eax, Operand(ebx, eax, times_pointer_size, 0));
716 // TODO(rmcilroy): Make dispatch table point to code entrys to avoid untagging
717 // and header removal.
718 __ add(eax, Immediate(Code::kHeaderSize - kHeapObjectTag));
723 void Builtins::Generate_InterpreterExitTrampoline(MacroAssembler* masm) {
724 // TODO(rmcilroy): List of things not currently dealt with here but done in
725 // fullcodegen's EmitReturnSequence.
726 // - Supporting FLAG_trace for Runtime::TraceExit.
727 // - Support profiler (specifically decrementing profiling_counter
728 // appropriately and calling out to HandleInterrupts if necessary).
730 // Load return value into r0.
731 __ mov(eax, Operand(ebp, -kPointerSize -
732 StandardFrameConstants::kFixedFrameSizeFromFp));
733 // Leave the frame (also dropping the register file).
735 // Return droping receiver + arguments.
736 // TODO(rmcilroy): Get number of arguments from BytecodeArray.
737 __ Ret(1 * kPointerSize, ecx);
741 void Builtins::Generate_CompileLazy(MacroAssembler* masm) {
742 CallRuntimePassFunction(masm, Runtime::kCompileLazy);
743 GenerateTailCallToReturnedCode(masm);
748 static void CallCompileOptimized(MacroAssembler* masm, bool concurrent) {
749 FrameScope scope(masm, StackFrame::INTERNAL);
750 // Push a copy of the function.
752 // Function is also the parameter to the runtime call.
754 // Whether to compile in a background thread.
755 __ Push(masm->isolate()->factory()->ToBoolean(concurrent));
757 __ CallRuntime(Runtime::kCompileOptimized, 2);
763 void Builtins::Generate_CompileOptimized(MacroAssembler* masm) {
764 CallCompileOptimized(masm, false);
765 GenerateTailCallToReturnedCode(masm);
769 void Builtins::Generate_CompileOptimizedConcurrent(MacroAssembler* masm) {
770 CallCompileOptimized(masm, true);
771 GenerateTailCallToReturnedCode(masm);
775 static void GenerateMakeCodeYoungAgainCommon(MacroAssembler* masm) {
776 // For now, we are relying on the fact that make_code_young doesn't do any
777 // garbage collection which allows us to save/restore the registers without
778 // worrying about which of them contain pointers. We also don't build an
779 // internal frame to make the code faster, since we shouldn't have to do stack
780 // crawls in MakeCodeYoung. This seems a bit fragile.
782 // Re-execute the code that was patched back to the young age when
784 __ sub(Operand(esp, 0), Immediate(5));
786 __ mov(eax, Operand(esp, 8 * kPointerSize));
788 FrameScope scope(masm, StackFrame::MANUAL);
789 __ PrepareCallCFunction(2, ebx);
790 __ mov(Operand(esp, 1 * kPointerSize),
791 Immediate(ExternalReference::isolate_address(masm->isolate())));
792 __ mov(Operand(esp, 0), eax);
794 ExternalReference::get_make_code_young_function(masm->isolate()), 2);
800 #define DEFINE_CODE_AGE_BUILTIN_GENERATOR(C) \
801 void Builtins::Generate_Make##C##CodeYoungAgainEvenMarking( \
802 MacroAssembler* masm) { \
803 GenerateMakeCodeYoungAgainCommon(masm); \
805 void Builtins::Generate_Make##C##CodeYoungAgainOddMarking( \
806 MacroAssembler* masm) { \
807 GenerateMakeCodeYoungAgainCommon(masm); \
809 CODE_AGE_LIST(DEFINE_CODE_AGE_BUILTIN_GENERATOR)
810 #undef DEFINE_CODE_AGE_BUILTIN_GENERATOR
813 void Builtins::Generate_MarkCodeAsExecutedOnce(MacroAssembler* masm) {
814 // For now, as in GenerateMakeCodeYoungAgainCommon, we are relying on the fact
815 // that make_code_young doesn't do any garbage collection which allows us to
816 // save/restore the registers without worrying about which of them contain
819 __ mov(eax, Operand(esp, 8 * kPointerSize));
820 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
822 FrameScope scope(masm, StackFrame::MANUAL);
823 __ PrepareCallCFunction(2, ebx);
824 __ mov(Operand(esp, 1 * kPointerSize),
825 Immediate(ExternalReference::isolate_address(masm->isolate())));
826 __ mov(Operand(esp, 0), eax);
828 ExternalReference::get_mark_code_as_executed_function(masm->isolate()),
833 // Perform prologue operations usually performed by the young code stub.
834 __ pop(eax); // Pop return address into scratch register.
835 __ push(ebp); // Caller's frame pointer.
837 __ push(esi); // Callee's context.
838 __ push(edi); // Callee's JS Function.
839 __ push(eax); // Push return address after frame prologue.
841 // Jump to point after the code-age stub.
846 void Builtins::Generate_MarkCodeAsExecutedTwice(MacroAssembler* masm) {
847 GenerateMakeCodeYoungAgainCommon(masm);
851 void Builtins::Generate_MarkCodeAsToBeExecutedOnce(MacroAssembler* masm) {
852 Generate_MarkCodeAsExecutedOnce(masm);
856 static void Generate_NotifyStubFailureHelper(MacroAssembler* masm,
857 SaveFPRegsMode save_doubles) {
858 // Enter an internal frame.
860 FrameScope scope(masm, StackFrame::INTERNAL);
862 // Preserve registers across notification, this is important for compiled
863 // stubs that tail call the runtime on deopts passing their parameters in
866 __ CallRuntime(Runtime::kNotifyStubFailure, 0, save_doubles);
868 // Tear down internal frame.
871 __ pop(MemOperand(esp, 0)); // Ignore state offset
872 __ ret(0); // Return to IC Miss stub, continuation still on stack.
876 void Builtins::Generate_NotifyStubFailure(MacroAssembler* masm) {
877 Generate_NotifyStubFailureHelper(masm, kDontSaveFPRegs);
881 void Builtins::Generate_NotifyStubFailureSaveDoubles(MacroAssembler* masm) {
882 Generate_NotifyStubFailureHelper(masm, kSaveFPRegs);
886 static void Generate_NotifyDeoptimizedHelper(MacroAssembler* masm,
887 Deoptimizer::BailoutType type) {
889 FrameScope scope(masm, StackFrame::INTERNAL);
891 // Pass deoptimization type to the runtime system.
892 __ push(Immediate(Smi::FromInt(static_cast<int>(type))));
893 __ CallRuntime(Runtime::kNotifyDeoptimized, 1);
895 // Tear down internal frame.
898 // Get the full codegen state from the stack and untag it.
899 __ mov(ecx, Operand(esp, 1 * kPointerSize));
902 // Switch on the state.
903 Label not_no_registers, not_tos_eax;
904 __ cmp(ecx, FullCodeGenerator::NO_REGISTERS);
905 __ j(not_equal, ¬_no_registers, Label::kNear);
906 __ ret(1 * kPointerSize); // Remove state.
908 __ bind(¬_no_registers);
909 __ mov(eax, Operand(esp, 2 * kPointerSize));
910 __ cmp(ecx, FullCodeGenerator::TOS_REG);
911 __ j(not_equal, ¬_tos_eax, Label::kNear);
912 __ ret(2 * kPointerSize); // Remove state, eax.
914 __ bind(¬_tos_eax);
915 __ Abort(kNoCasesLeft);
919 void Builtins::Generate_NotifyDeoptimized(MacroAssembler* masm) {
920 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::EAGER);
924 void Builtins::Generate_NotifySoftDeoptimized(MacroAssembler* masm) {
925 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::SOFT);
929 void Builtins::Generate_NotifyLazyDeoptimized(MacroAssembler* masm) {
930 Generate_NotifyDeoptimizedHelper(masm, Deoptimizer::LAZY);
934 void Builtins::Generate_FunctionCall(MacroAssembler* masm) {
935 Factory* factory = masm->isolate()->factory();
937 // 1. Make sure we have at least one argument.
940 __ j(not_zero, &done);
942 __ push(Immediate(factory->undefined_value()));
948 // 2. Get the function to call (passed as receiver) from the stack, check
949 // if it is a function.
950 Label slow, non_function;
951 // 1 ~ return address.
952 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
953 __ JumpIfSmi(edi, &non_function);
954 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
955 __ j(not_equal, &slow);
958 // 3a. Patch the first argument if necessary when calling a function.
959 Label shift_arguments;
960 __ Move(edx, Immediate(0)); // indicate regular JS_FUNCTION
961 { Label convert_to_object, use_global_proxy, patch_receiver;
962 // Change context eagerly in case we need the global receiver.
963 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
965 // Do not transform the receiver for strict mode functions.
966 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
967 __ test_b(FieldOperand(ebx, SharedFunctionInfo::kStrictModeByteOffset),
968 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
969 __ j(not_equal, &shift_arguments);
971 // Do not transform the receiver for natives (shared already in ebx).
972 __ test_b(FieldOperand(ebx, SharedFunctionInfo::kNativeByteOffset),
973 1 << SharedFunctionInfo::kNativeBitWithinByte);
974 __ j(not_equal, &shift_arguments);
976 // Compute the receiver in sloppy mode.
977 __ mov(ebx, Operand(esp, eax, times_4, 0)); // First argument.
979 // Call ToObject on the receiver if it is not an object, or use the
980 // global object if it is null or undefined.
981 __ JumpIfSmi(ebx, &convert_to_object);
982 __ cmp(ebx, factory->null_value());
983 __ j(equal, &use_global_proxy);
984 __ cmp(ebx, factory->undefined_value());
985 __ j(equal, &use_global_proxy);
986 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
987 __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx);
988 __ j(above_equal, &shift_arguments);
990 __ bind(&convert_to_object);
992 { // In order to preserve argument count.
993 FrameScope scope(masm, StackFrame::INTERNAL);
998 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1000 __ Move(edx, Immediate(0)); // restore
1006 // Restore the function to edi.
1007 __ mov(edi, Operand(esp, eax, times_4, 1 * kPointerSize));
1008 __ jmp(&patch_receiver);
1010 __ bind(&use_global_proxy);
1012 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1013 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalProxyOffset));
1015 __ bind(&patch_receiver);
1016 __ mov(Operand(esp, eax, times_4, 0), ebx);
1018 __ jmp(&shift_arguments);
1021 // 3b. Check for function proxy.
1023 __ Move(edx, Immediate(1)); // indicate function proxy
1024 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
1025 __ j(equal, &shift_arguments);
1026 __ bind(&non_function);
1027 __ Move(edx, Immediate(2)); // indicate non-function
1029 // 3c. Patch the first argument when calling a non-function. The
1030 // CALL_NON_FUNCTION builtin expects the non-function callee as
1031 // receiver, so overwrite the first argument which will ultimately
1032 // become the receiver.
1033 __ mov(Operand(esp, eax, times_4, 0), edi);
1035 // 4. Shift arguments and return address one slot down on the stack
1036 // (overwriting the original receiver). Adjust argument count to make
1037 // the original first argument the new receiver.
1038 __ bind(&shift_arguments);
1042 __ mov(ebx, Operand(esp, ecx, times_4, 0));
1043 __ mov(Operand(esp, ecx, times_4, kPointerSize), ebx);
1045 __ j(not_sign, &loop); // While non-negative (to copy return address).
1046 __ pop(ebx); // Discard copy of return address.
1047 __ dec(eax); // One fewer argument (first argument is new receiver).
1050 // 5a. Call non-function via tail call to CALL_NON_FUNCTION builtin,
1051 // or a function proxy via CALL_FUNCTION_PROXY.
1052 { Label function, non_proxy;
1054 __ j(zero, &function);
1055 __ Move(ebx, Immediate(0));
1056 __ cmp(edx, Immediate(1));
1057 __ j(not_equal, &non_proxy);
1059 __ pop(edx); // return address
1060 __ push(edi); // re-add proxy object as additional argument
1063 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
1064 __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1065 RelocInfo::CODE_TARGET);
1067 __ bind(&non_proxy);
1068 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
1069 __ jmp(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1070 RelocInfo::CODE_TARGET);
1074 // 5b. Get the code to call from the function and check that the number of
1075 // expected arguments matches what we're providing. If so, jump
1076 // (tail-call) to the code in register edx without checking arguments.
1077 __ mov(edx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1079 FieldOperand(edx, SharedFunctionInfo::kFormalParameterCountOffset));
1080 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
1084 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline());
1086 ParameterCount expected(0);
1087 __ InvokeCode(edx, expected, expected, JUMP_FUNCTION, NullCallWrapper());
1091 static void Generate_PushAppliedArguments(MacroAssembler* masm,
1092 const int argumentsOffset,
1093 const int indexOffset,
1094 const int limitOffset) {
1095 // Copy all arguments from the array to the stack.
1097 Register receiver = LoadDescriptor::ReceiverRegister();
1098 Register key = LoadDescriptor::NameRegister();
1099 Register slot = LoadDescriptor::SlotRegister();
1100 Register vector = LoadWithVectorDescriptor::VectorRegister();
1101 __ mov(key, Operand(ebp, indexOffset));
1104 __ mov(receiver, Operand(ebp, argumentsOffset)); // load arguments
1106 // Use inline caching to speed up access to arguments.
1107 FeedbackVectorSpec spec(0, Code::KEYED_LOAD_IC);
1108 Handle<TypeFeedbackVector> feedback_vector =
1109 masm->isolate()->factory()->NewTypeFeedbackVector(&spec);
1110 int index = feedback_vector->GetIndex(FeedbackVectorICSlot(0));
1111 __ mov(slot, Immediate(Smi::FromInt(index)));
1112 __ mov(vector, Immediate(feedback_vector));
1114 KeyedLoadICStub(masm->isolate(), LoadICState(kNoExtraICState)).GetCode();
1115 __ call(ic, RelocInfo::CODE_TARGET);
1116 // It is important that we do not have a test instruction after the
1117 // call. A test instruction after the call is used to indicate that
1118 // we have generated an inline version of the keyed load. In this
1119 // case, we know that we are not generating a test instruction next.
1121 // Push the nth argument.
1124 // Update the index on the stack and in register key.
1125 __ mov(key, Operand(ebp, indexOffset));
1126 __ add(key, Immediate(1 << kSmiTagSize));
1127 __ mov(Operand(ebp, indexOffset), key);
1130 __ cmp(key, Operand(ebp, limitOffset));
1131 __ j(not_equal, &loop);
1133 // On exit, the pushed arguments count is in eax, untagged
1139 // Used by FunctionApply and ReflectApply
1140 static void Generate_ApplyHelper(MacroAssembler* masm, bool targetIsArgument) {
1141 const int kFormalParameters = targetIsArgument ? 3 : 2;
1142 const int kStackSize = kFormalParameters + 1;
1145 // esp : return address
1146 // esp[4] : arguments
1147 // esp[8] : receiver ("this")
1148 // esp[12] : function
1150 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1152 // ebp : Old base pointer
1153 // ebp[4] : return address
1154 // ebp[8] : function arguments
1155 // ebp[12] : receiver
1156 // ebp[16] : function
1157 static const int kArgumentsOffset = kFPOnStackSize + kPCOnStackSize;
1158 static const int kReceiverOffset = kArgumentsOffset + kPointerSize;
1159 static const int kFunctionOffset = kReceiverOffset + kPointerSize;
1161 __ push(Operand(ebp, kFunctionOffset)); // push this
1162 __ push(Operand(ebp, kArgumentsOffset)); // push arguments
1163 if (targetIsArgument) {
1164 __ InvokeBuiltin(Builtins::REFLECT_APPLY_PREPARE, CALL_FUNCTION);
1166 __ InvokeBuiltin(Builtins::APPLY_PREPARE, CALL_FUNCTION);
1169 Generate_CheckStackOverflow(masm, kFunctionOffset, kEaxIsSmiTagged);
1171 // Push current index and limit.
1172 const int kLimitOffset =
1173 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
1174 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
1175 __ push(eax); // limit
1176 __ push(Immediate(0)); // index
1178 // Get the receiver.
1179 __ mov(ebx, Operand(ebp, kReceiverOffset));
1181 // Check that the function is a JS function (otherwise it must be a proxy).
1182 Label push_receiver, use_global_proxy;
1183 __ mov(edi, Operand(ebp, kFunctionOffset));
1184 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1185 __ j(not_equal, &push_receiver);
1187 // Change context eagerly to get the right global object if necessary.
1188 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
1190 // Compute the receiver.
1191 // Do not transform the receiver for strict mode functions.
1192 Label call_to_object;
1193 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1194 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset),
1195 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
1196 __ j(not_equal, &push_receiver);
1198 Factory* factory = masm->isolate()->factory();
1200 // Do not transform the receiver for natives (shared already in ecx).
1201 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset),
1202 1 << SharedFunctionInfo::kNativeBitWithinByte);
1203 __ j(not_equal, &push_receiver);
1205 // Compute the receiver in sloppy mode.
1206 // Call ToObject on the receiver if it is not an object, or use the
1207 // global object if it is null or undefined.
1208 __ JumpIfSmi(ebx, &call_to_object);
1209 __ cmp(ebx, factory->null_value());
1210 __ j(equal, &use_global_proxy);
1211 __ cmp(ebx, factory->undefined_value());
1212 __ j(equal, &use_global_proxy);
1213 STATIC_ASSERT(LAST_SPEC_OBJECT_TYPE == LAST_TYPE);
1214 __ CmpObjectType(ebx, FIRST_SPEC_OBJECT_TYPE, ecx);
1215 __ j(above_equal, &push_receiver);
1217 __ bind(&call_to_object);
1219 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1221 __ jmp(&push_receiver);
1223 __ bind(&use_global_proxy);
1225 Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1226 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalProxyOffset));
1228 // Push the receiver.
1229 __ bind(&push_receiver);
1232 // Loop over the arguments array, pushing each value to the stack
1233 Generate_PushAppliedArguments(
1234 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1236 // Call the function.
1238 ParameterCount actual(eax);
1239 __ mov(edi, Operand(ebp, kFunctionOffset));
1240 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1241 __ j(not_equal, &call_proxy);
1242 __ InvokeFunction(edi, actual, CALL_FUNCTION, NullCallWrapper());
1244 frame_scope.GenerateLeaveFrame();
1245 __ ret(kStackSize * kPointerSize); // remove this, receiver, and arguments
1247 // Call the function proxy.
1248 __ bind(&call_proxy);
1249 __ push(edi); // add function proxy as last argument
1251 __ Move(ebx, Immediate(0));
1252 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
1253 __ call(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
1254 RelocInfo::CODE_TARGET);
1256 // Leave internal frame.
1258 __ ret(kStackSize * kPointerSize); // remove this, receiver, and arguments
1262 // Used by ReflectConstruct
1263 static void Generate_ConstructHelper(MacroAssembler* masm) {
1264 const int kFormalParameters = 3;
1265 const int kStackSize = kFormalParameters + 1;
1268 // esp : return address
1269 // esp[4] : original constructor (new.target)
1270 // esp[8] : arguments
1271 // esp[16] : constructor
1273 FrameScope frame_scope(masm, StackFrame::INTERNAL);
1275 // ebp : Old base pointer
1276 // ebp[4] : return address
1277 // ebp[8] : original constructor (new.target)
1278 // ebp[12] : arguments
1279 // ebp[16] : constructor
1280 static const int kNewTargetOffset = kFPOnStackSize + kPCOnStackSize;
1281 static const int kArgumentsOffset = kNewTargetOffset + kPointerSize;
1282 static const int kFunctionOffset = kArgumentsOffset + kPointerSize;
1284 // If newTarget is not supplied, set it to constructor
1285 Label validate_arguments;
1286 __ mov(eax, Operand(ebp, kNewTargetOffset));
1287 __ CompareRoot(eax, Heap::kUndefinedValueRootIndex);
1288 __ j(not_equal, &validate_arguments, Label::kNear);
1289 __ mov(eax, Operand(ebp, kFunctionOffset));
1290 __ mov(Operand(ebp, kNewTargetOffset), eax);
1292 // Validate arguments
1293 __ bind(&validate_arguments);
1294 __ push(Operand(ebp, kFunctionOffset));
1295 __ push(Operand(ebp, kArgumentsOffset));
1296 __ push(Operand(ebp, kNewTargetOffset));
1297 __ InvokeBuiltin(Builtins::REFLECT_CONSTRUCT_PREPARE, CALL_FUNCTION);
1299 Generate_CheckStackOverflow(masm, kFunctionOffset, kEaxIsSmiTagged);
1301 // Push current index and limit.
1302 const int kLimitOffset =
1303 StandardFrameConstants::kExpressionsOffset - 1 * kPointerSize;
1304 const int kIndexOffset = kLimitOffset - 1 * kPointerSize;
1305 __ Push(eax); // limit
1306 __ push(Immediate(0)); // index
1307 // Push the constructor function as callee.
1308 __ push(Operand(ebp, kFunctionOffset));
1310 // Loop over the arguments array, pushing each value to the stack
1311 Generate_PushAppliedArguments(
1312 masm, kArgumentsOffset, kIndexOffset, kLimitOffset);
1314 // Use undefined feedback vector
1315 __ LoadRoot(ebx, Heap::kUndefinedValueRootIndex);
1316 __ mov(edi, Operand(ebp, kFunctionOffset));
1317 __ mov(ecx, Operand(ebp, kNewTargetOffset));
1319 // Call the function.
1320 CallConstructStub stub(masm->isolate(), SUPER_CONSTRUCTOR_CALL);
1321 __ call(stub.GetCode(), RelocInfo::CONSTRUCT_CALL);
1323 // Leave internal frame.
1325 // remove this, target, arguments, and newTarget
1326 __ ret(kStackSize * kPointerSize);
1330 void Builtins::Generate_FunctionApply(MacroAssembler* masm) {
1331 Generate_ApplyHelper(masm, false);
1335 void Builtins::Generate_ReflectApply(MacroAssembler* masm) {
1336 Generate_ApplyHelper(masm, true);
1340 void Builtins::Generate_ReflectConstruct(MacroAssembler* masm) {
1341 Generate_ConstructHelper(masm);
1345 void Builtins::Generate_InternalArrayCode(MacroAssembler* masm) {
1346 // ----------- S t a t e -------------
1348 // -- esp[0] : return address
1349 // -- esp[4] : last argument
1350 // -----------------------------------
1351 Label generic_array_code;
1353 // Get the InternalArray function.
1354 __ LoadGlobalFunction(Context::INTERNAL_ARRAY_FUNCTION_INDEX, edi);
1356 if (FLAG_debug_code) {
1357 // Initial map for the builtin InternalArray function should be a map.
1358 __ mov(ebx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
1359 // Will both indicate a NULL and a Smi.
1360 __ test(ebx, Immediate(kSmiTagMask));
1361 __ Assert(not_zero, kUnexpectedInitialMapForInternalArrayFunction);
1362 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1363 __ Assert(equal, kUnexpectedInitialMapForInternalArrayFunction);
1366 // Run the native code for the InternalArray function called as a normal
1369 InternalArrayConstructorStub stub(masm->isolate());
1370 __ TailCallStub(&stub);
1374 void Builtins::Generate_ArrayCode(MacroAssembler* masm) {
1375 // ----------- S t a t e -------------
1377 // -- esp[0] : return address
1378 // -- esp[4] : last argument
1379 // -----------------------------------
1380 Label generic_array_code;
1382 // Get the Array function.
1383 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, edi);
1386 if (FLAG_debug_code) {
1387 // Initial map for the builtin Array 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, kUnexpectedInitialMapForArrayFunction);
1392 __ CmpObjectType(ebx, MAP_TYPE, ecx);
1393 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
1396 // Run the native code for the Array function called as a normal function.
1398 __ mov(ebx, masm->isolate()->factory()->undefined_value());
1399 ArrayConstructorStub stub(masm->isolate());
1400 __ TailCallStub(&stub);
1404 void Builtins::Generate_StringConstructCode(MacroAssembler* masm) {
1405 // ----------- S t a t e -------------
1406 // -- eax : number of arguments
1407 // -- edi : constructor function
1408 // -- esp[0] : return address
1409 // -- esp[(argc - n) * 4] : arg[n] (zero-based)
1410 // -- esp[(argc + 1) * 4] : receiver
1411 // -----------------------------------
1412 Counters* counters = masm->isolate()->counters();
1413 __ IncrementCounter(counters->string_ctor_calls(), 1);
1415 if (FLAG_debug_code) {
1416 __ LoadGlobalFunction(Context::STRING_FUNCTION_INDEX, ecx);
1418 __ Assert(equal, kUnexpectedStringFunction);
1421 // Load the first argument into eax and get rid of the rest
1422 // (including the receiver).
1425 __ j(zero, &no_arguments);
1426 __ mov(ebx, Operand(esp, eax, times_pointer_size, 0));
1428 __ lea(esp, Operand(esp, eax, times_pointer_size, kPointerSize));
1432 // Lookup the argument in the number to string cache.
1433 Label not_cached, argument_is_string;
1434 __ LookupNumberStringCache(eax, // Input.
1439 __ IncrementCounter(counters->string_ctor_cached_number(), 1);
1440 __ bind(&argument_is_string);
1441 // ----------- S t a t e -------------
1442 // -- ebx : argument converted to string
1443 // -- edi : constructor function
1444 // -- esp[0] : return address
1445 // -----------------------------------
1447 // Allocate a JSValue and put the tagged pointer into eax.
1449 __ Allocate(JSValue::kSize,
1451 ecx, // New allocation top (we ignore it).
1457 __ LoadGlobalFunctionInitialMap(edi, ecx);
1458 if (FLAG_debug_code) {
1459 __ cmpb(FieldOperand(ecx, Map::kInstanceSizeOffset),
1460 JSValue::kSize >> kPointerSizeLog2);
1461 __ Assert(equal, kUnexpectedStringWrapperInstanceSize);
1462 __ cmpb(FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset), 0);
1463 __ Assert(equal, kUnexpectedUnusedPropertiesOfStringWrapper);
1465 __ mov(FieldOperand(eax, HeapObject::kMapOffset), ecx);
1467 // Set properties and elements.
1468 Factory* factory = masm->isolate()->factory();
1469 __ Move(ecx, Immediate(factory->empty_fixed_array()));
1470 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
1471 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ecx);
1474 __ mov(FieldOperand(eax, JSValue::kValueOffset), ebx);
1476 // Ensure the object is fully initialized.
1477 STATIC_ASSERT(JSValue::kSize == 4 * kPointerSize);
1479 // We're done. Return.
1482 // The argument was not found in the number to string cache. Check
1483 // if it's a string already before calling the conversion builtin.
1484 Label convert_argument;
1485 __ bind(¬_cached);
1486 STATIC_ASSERT(kSmiTag == 0);
1487 __ JumpIfSmi(eax, &convert_argument);
1488 Condition is_string = masm->IsObjectStringType(eax, ebx, ecx);
1489 __ j(NegateCondition(is_string), &convert_argument);
1491 __ IncrementCounter(counters->string_ctor_string_value(), 1);
1492 __ jmp(&argument_is_string);
1494 // Invoke the conversion builtin and put the result into ebx.
1495 __ bind(&convert_argument);
1496 __ IncrementCounter(counters->string_ctor_conversions(), 1);
1498 FrameScope scope(masm, StackFrame::INTERNAL);
1499 __ push(edi); // Preserve the function.
1501 __ InvokeBuiltin(Builtins::TO_STRING, CALL_FUNCTION);
1505 __ jmp(&argument_is_string);
1507 // Load the empty string into ebx, remove the receiver from the
1508 // stack, and jump back to the case where the argument is a string.
1509 __ bind(&no_arguments);
1510 __ Move(ebx, Immediate(factory->empty_string()));
1512 __ lea(esp, Operand(esp, kPointerSize));
1514 __ jmp(&argument_is_string);
1516 // At this point the argument is already a string. Call runtime to
1517 // create a string wrapper.
1518 __ bind(&gc_required);
1519 __ IncrementCounter(counters->string_ctor_gc_required(), 1);
1521 FrameScope scope(masm, StackFrame::INTERNAL);
1523 __ CallRuntime(Runtime::kNewStringWrapper, 1);
1529 static void ArgumentsAdaptorStackCheck(MacroAssembler* masm,
1530 Label* stack_overflow) {
1531 // ----------- S t a t e -------------
1532 // -- eax : actual number of arguments
1533 // -- ebx : expected number of arguments
1534 // -- edi : function (passed through to callee)
1535 // -----------------------------------
1536 // Check the stack for overflow. We are not trying to catch
1537 // interruptions (e.g. debug break and preemption) here, so the "real stack
1538 // limit" is checked.
1539 ExternalReference real_stack_limit =
1540 ExternalReference::address_of_real_stack_limit(masm->isolate());
1541 __ mov(edx, Operand::StaticVariable(real_stack_limit));
1542 // Make ecx the space we have left. The stack might already be overflowed
1543 // here which will cause ecx to become negative.
1546 // Make edx the space we need for the array when it is unrolled onto the
1549 __ shl(edx, kPointerSizeLog2);
1550 // Check if the arguments will overflow the stack.
1552 __ j(less_equal, stack_overflow); // Signed comparison.
1556 static void EnterArgumentsAdaptorFrame(MacroAssembler* masm) {
1560 // Store the arguments adaptor context sentinel.
1561 __ push(Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1563 // Push the function on the stack.
1566 // Preserve the number of arguments on the stack. Must preserve eax,
1567 // ebx and ecx because these registers are used when copying the
1568 // arguments and the receiver.
1569 STATIC_ASSERT(kSmiTagSize == 1);
1570 __ lea(edi, Operand(eax, eax, times_1, kSmiTag));
1575 static void LeaveArgumentsAdaptorFrame(MacroAssembler* masm) {
1576 // Retrieve the number of arguments from the stack.
1577 __ mov(ebx, Operand(ebp, ArgumentsAdaptorFrameConstants::kLengthOffset));
1582 // Remove caller arguments from the stack.
1583 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
1585 __ lea(esp, Operand(esp, ebx, times_2, 1 * kPointerSize)); // 1 ~ receiver
1590 void Builtins::Generate_ArgumentsAdaptorTrampoline(MacroAssembler* masm) {
1591 // ----------- S t a t e -------------
1592 // -- eax : actual number of arguments
1593 // -- ebx : expected number of arguments
1594 // -- edi : function (passed through to callee)
1595 // -----------------------------------
1597 Label invoke, dont_adapt_arguments;
1598 __ IncrementCounter(masm->isolate()->counters()->arguments_adaptors(), 1);
1600 Label stack_overflow;
1601 ArgumentsAdaptorStackCheck(masm, &stack_overflow);
1603 Label enough, too_few;
1604 __ mov(edx, FieldOperand(edi, JSFunction::kCodeEntryOffset));
1606 __ j(less, &too_few);
1607 __ cmp(ebx, SharedFunctionInfo::kDontAdaptArgumentsSentinel);
1608 __ j(equal, &dont_adapt_arguments);
1610 { // Enough parameters: Actual >= expected.
1612 EnterArgumentsAdaptorFrame(masm);
1614 // Copy receiver and all expected arguments.
1615 const int offset = StandardFrameConstants::kCallerSPOffset;
1616 __ lea(eax, Operand(ebp, eax, times_4, offset));
1617 __ mov(edi, -1); // account for receiver
1622 __ push(Operand(eax, 0));
1623 __ sub(eax, Immediate(kPointerSize));
1629 { // Too few parameters: Actual < expected.
1632 // If the function is strong we need to throw an error.
1633 Label no_strong_error;
1634 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1635 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrongModeByteOffset),
1636 1 << SharedFunctionInfo::kStrongModeBitWithinByte);
1637 __ j(equal, &no_strong_error, Label::kNear);
1639 // What we really care about is the required number of arguments.
1640 __ mov(ecx, FieldOperand(ecx, SharedFunctionInfo::kLengthOffset));
1643 __ j(greater_equal, &no_strong_error, Label::kNear);
1646 FrameScope frame(masm, StackFrame::MANUAL);
1647 EnterArgumentsAdaptorFrame(masm);
1648 __ CallRuntime(Runtime::kThrowStrongModeTooFewArguments, 0);
1651 __ bind(&no_strong_error);
1652 EnterArgumentsAdaptorFrame(masm);
1654 // Copy receiver and all actual arguments.
1655 const int offset = StandardFrameConstants::kCallerSPOffset;
1656 __ lea(edi, Operand(ebp, eax, times_4, offset));
1657 // ebx = expected - actual.
1659 // eax = -actual - 1
1661 __ sub(eax, Immediate(1));
1666 __ push(Operand(edi, 0));
1667 __ sub(edi, Immediate(kPointerSize));
1669 __ j(not_zero, ©);
1671 // Fill remaining expected arguments with undefined values.
1675 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
1680 // Call the entry point.
1682 // Restore function pointer.
1683 __ mov(edi, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1686 // Store offset of return address for deoptimizer.
1687 masm->isolate()->heap()->SetArgumentsAdaptorDeoptPCOffset(masm->pc_offset());
1689 // Leave frame and return.
1690 LeaveArgumentsAdaptorFrame(masm);
1693 // -------------------------------------------
1694 // Dont adapt arguments.
1695 // -------------------------------------------
1696 __ bind(&dont_adapt_arguments);
1699 __ bind(&stack_overflow);
1701 FrameScope frame(masm, StackFrame::MANUAL);
1702 EnterArgumentsAdaptorFrame(masm);
1703 __ InvokeBuiltin(Builtins::STACK_OVERFLOW, CALL_FUNCTION);
1709 void Builtins::Generate_OnStackReplacement(MacroAssembler* masm) {
1710 // Lookup the function in the JavaScript frame.
1711 __ mov(eax, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1713 FrameScope scope(masm, StackFrame::INTERNAL);
1714 // Pass function as argument.
1716 __ CallRuntime(Runtime::kCompileForOnStackReplacement, 1);
1720 // If the code object is null, just return to the unoptimized code.
1721 __ cmp(eax, Immediate(0));
1722 __ j(not_equal, &skip, Label::kNear);
1727 // Load deoptimization data from the code object.
1728 __ mov(ebx, Operand(eax, Code::kDeoptimizationDataOffset - kHeapObjectTag));
1730 // Load the OSR entrypoint offset from the deoptimization data.
1731 __ mov(ebx, Operand(ebx, FixedArray::OffsetOfElementAt(
1732 DeoptimizationInputData::kOsrPcOffsetIndex) - kHeapObjectTag));
1735 // Compute the target address = code_obj + header_size + osr_offset
1736 __ lea(eax, Operand(eax, ebx, times_1, Code::kHeaderSize - kHeapObjectTag));
1738 // Overwrite the return address on the stack.
1739 __ mov(Operand(esp, 0), eax);
1741 // And "return" to the OSR entry point of the function.
1746 void Builtins::Generate_OsrAfterStackCheck(MacroAssembler* masm) {
1747 // We check the stack limit as indicator that recompilation might be done.
1749 ExternalReference stack_limit =
1750 ExternalReference::address_of_stack_limit(masm->isolate());
1751 __ cmp(esp, Operand::StaticVariable(stack_limit));
1752 __ j(above_equal, &ok, Label::kNear);
1754 FrameScope scope(masm, StackFrame::INTERNAL);
1755 __ CallRuntime(Runtime::kStackGuard, 0);
1757 __ jmp(masm->isolate()->builtins()->OnStackReplacement(),
1758 RelocInfo::CODE_TARGET);
1765 } // namespace internal
1768 #endif // V8_TARGET_ARCH_IA32