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/base/bits.h"
10 #include "src/bootstrapper.h"
11 #include "src/code-stubs.h"
12 #include "src/codegen.h"
13 #include "src/ic/handler-compiler.h"
14 #include "src/ic/ic.h"
15 #include "src/ic/stub-cache.h"
16 #include "src/isolate.h"
17 #include "src/jsregexp.h"
18 #include "src/regexp-macro-assembler.h"
19 #include "src/runtime/runtime.h"
25 static void InitializeArrayConstructorDescriptor(
26 Isolate* isolate, CodeStubDescriptor* descriptor,
27 int constant_stack_parameter_count) {
29 // eax -- number of arguments
31 // ebx -- allocation site with elements kind
32 Address deopt_handler = Runtime::FunctionForId(
33 Runtime::kArrayConstructor)->entry;
35 if (constant_stack_parameter_count == 0) {
36 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
37 JS_FUNCTION_STUB_MODE);
39 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
40 JS_FUNCTION_STUB_MODE);
45 static void InitializeInternalArrayConstructorDescriptor(
46 Isolate* isolate, CodeStubDescriptor* descriptor,
47 int constant_stack_parameter_count) {
49 // eax -- number of arguments
50 // edi -- constructor function
51 Address deopt_handler = Runtime::FunctionForId(
52 Runtime::kInternalArrayConstructor)->entry;
54 if (constant_stack_parameter_count == 0) {
55 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
56 JS_FUNCTION_STUB_MODE);
58 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
59 JS_FUNCTION_STUB_MODE);
64 void ArrayNoArgumentConstructorStub::InitializeDescriptor(
65 CodeStubDescriptor* descriptor) {
66 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
70 void ArraySingleArgumentConstructorStub::InitializeDescriptor(
71 CodeStubDescriptor* descriptor) {
72 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
76 void ArrayNArgumentsConstructorStub::InitializeDescriptor(
77 CodeStubDescriptor* descriptor) {
78 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
82 void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
83 CodeStubDescriptor* descriptor) {
84 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
88 void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
89 CodeStubDescriptor* descriptor) {
90 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
94 void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
95 CodeStubDescriptor* descriptor) {
96 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
100 #define __ ACCESS_MASM(masm)
103 void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
104 ExternalReference miss) {
105 // Update the static counter each time a new code stub is generated.
106 isolate()->counters()->code_stubs()->Increment();
108 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
109 int param_count = descriptor.GetRegisterParameterCount();
111 // Call the runtime system in a fresh internal frame.
112 FrameScope scope(masm, StackFrame::INTERNAL);
113 DCHECK(param_count == 0 ||
114 eax.is(descriptor.GetRegisterParameter(param_count - 1)));
116 for (int i = 0; i < param_count; ++i) {
117 __ push(descriptor.GetRegisterParameter(i));
119 __ CallExternalReference(miss, param_count);
126 void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
127 // We don't allow a GC during a store buffer overflow so there is no need to
128 // store the registers in any particular way, but we do have to store and
131 if (save_doubles()) {
132 // Save FPU stat in m108byte.
133 __ sub(esp, Immediate(108));
134 __ fnsave(Operand(esp, 0));
136 const int argument_count = 1;
138 AllowExternalCallThatCantCauseGC scope(masm);
139 __ PrepareCallCFunction(argument_count, ecx);
140 __ mov(Operand(esp, 0 * kPointerSize),
141 Immediate(ExternalReference::isolate_address(isolate())));
143 ExternalReference::store_buffer_overflow_function(isolate()),
145 if (save_doubles()) {
146 // Restore FPU stat in m108byte.
147 __ frstor(Operand(esp, 0));
148 __ add(esp, Immediate(108));
155 class FloatingPointHelper : public AllStatic {
162 // Code pattern for loading a floating point value. Input value must
163 // be either a smi or a heap number object (fp value). Requirements:
164 // operand in register number. Returns operand as floating point number
166 static void LoadFloatOperand(MacroAssembler* masm, Register number);
168 // Test if operands are smi or number objects (fp). Requirements:
169 // operand_1 in eax, operand_2 in edx; falls through on float
170 // operands, jumps to the non_float label otherwise.
171 static void CheckFloatOperands(MacroAssembler* masm,
177 void DoubleToIStub::Generate(MacroAssembler* masm) {
178 Register input_reg = this->source();
179 Register final_result_reg = this->destination();
180 DCHECK(is_truncating());
182 Label check_negative, process_64_bits, done, done_no_stash;
184 int double_offset = offset();
186 // Account for return address and saved regs if input is esp.
187 if (input_reg.is(esp)) double_offset += 3 * kPointerSize;
189 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
190 MemOperand exponent_operand(MemOperand(input_reg,
191 double_offset + kDoubleSize / 2));
195 Register scratch_candidates[3] = { ebx, edx, edi };
196 for (int i = 0; i < 3; i++) {
197 scratch1 = scratch_candidates[i];
198 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
201 // Since we must use ecx for shifts below, use some other register (eax)
202 // to calculate the result if ecx is the requested return register.
203 Register result_reg = final_result_reg.is(ecx) ? eax : final_result_reg;
204 // Save ecx if it isn't the return register and therefore volatile, or if it
205 // is the return register, then save the temp register we use in its stead for
207 Register save_reg = final_result_reg.is(ecx) ? eax : ecx;
211 bool stash_exponent_copy = !input_reg.is(esp);
212 __ mov(scratch1, mantissa_operand);
213 __ mov(ecx, exponent_operand);
214 if (stash_exponent_copy) __ push(ecx);
216 __ and_(ecx, HeapNumber::kExponentMask);
217 __ shr(ecx, HeapNumber::kExponentShift);
218 __ lea(result_reg, MemOperand(ecx, -HeapNumber::kExponentBias));
219 __ cmp(result_reg, Immediate(HeapNumber::kMantissaBits));
220 __ j(below, &process_64_bits);
222 // Result is entirely in lower 32-bits of mantissa
223 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
224 __ sub(ecx, Immediate(delta));
225 __ xor_(result_reg, result_reg);
226 __ cmp(ecx, Immediate(31));
229 __ jmp(&check_negative);
231 __ bind(&process_64_bits);
232 // Result must be extracted from shifted 32-bit mantissa
233 __ sub(ecx, Immediate(delta));
235 if (stash_exponent_copy) {
236 __ mov(result_reg, MemOperand(esp, 0));
238 __ mov(result_reg, exponent_operand);
241 Immediate(static_cast<uint32_t>(Double::kSignificandMask >> 32)));
243 Immediate(static_cast<uint32_t>(Double::kHiddenBit >> 32)));
244 __ shrd(result_reg, scratch1);
245 __ shr_cl(result_reg);
246 __ test(ecx, Immediate(32));
249 __ j(equal, &skip_mov, Label::kNear);
250 __ mov(scratch1, result_reg);
254 // If the double was negative, negate the integer result.
255 __ bind(&check_negative);
256 __ mov(result_reg, scratch1);
258 if (stash_exponent_copy) {
259 __ cmp(MemOperand(esp, 0), Immediate(0));
261 __ cmp(exponent_operand, Immediate(0));
265 __ j(less_equal, &skip_mov, Label::kNear);
266 __ mov(result_reg, scratch1);
272 if (stash_exponent_copy) {
273 __ add(esp, Immediate(kDoubleSize / 2));
275 __ bind(&done_no_stash);
276 if (!final_result_reg.is(result_reg)) {
277 DCHECK(final_result_reg.is(ecx));
278 __ mov(final_result_reg, result_reg);
286 void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
288 Label load_smi, done;
290 __ JumpIfSmi(number, &load_smi, Label::kNear);
291 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
292 __ jmp(&done, Label::kNear);
297 __ fild_s(Operand(esp, 0));
304 void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
307 Label test_other, done;
308 // Test if both operands are floats or smi -> scratch=k_is_float;
309 // Otherwise scratch = k_not_float.
310 __ JumpIfSmi(edx, &test_other, Label::kNear);
311 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
312 Factory* factory = masm->isolate()->factory();
313 __ cmp(scratch, factory->heap_number_map());
314 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
316 __ bind(&test_other);
317 __ JumpIfSmi(eax, &done, Label::kNear);
318 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
319 __ cmp(scratch, factory->heap_number_map());
320 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
322 // Fall-through: Both operands are numbers.
327 void MathPowStub::Generate(MacroAssembler* masm) {
328 const Register base = edx;
329 const Register scratch = ecx;
330 Counters* counters = isolate()->counters();
333 // We will call runtime helper function directly.
334 if (exponent_type() == ON_STACK) {
335 // The arguments are still on the stack.
336 __ bind(&call_runtime);
337 __ TailCallRuntime(Runtime::kMathPowRT, 2, 1);
339 // The stub is called from non-optimized code, which expects the result
340 // as heap number in exponent.
341 __ AllocateHeapNumber(eax, scratch, base, &call_runtime);
342 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
343 __ IncrementCounter(counters->math_pow(), 1);
344 __ ret(2 * kPointerSize);
346 // Currently it's only called from full-compiler and exponent type is
353 void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
355 Register receiver = LoadDescriptor::ReceiverRegister();
356 // With careful management, we won't have to save slot and vector on
357 // the stack. Simply handle the possibly missing case first.
358 // TODO(mvstanton): this code can be more efficient.
359 __ cmp(FieldOperand(receiver, JSFunction::kPrototypeOrInitialMapOffset),
360 Immediate(isolate()->factory()->the_hole_value()));
362 __ TryGetFunctionPrototype(receiver, eax, ebx, &miss);
366 PropertyAccessCompiler::TailCallBuiltin(
367 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
371 void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
372 // Return address is on the stack.
375 Register receiver = LoadDescriptor::ReceiverRegister();
376 Register key = LoadDescriptor::NameRegister();
377 Register scratch = eax;
378 DCHECK(!scratch.is(receiver) && !scratch.is(key));
380 // Check that the key is an array index, that is Uint32.
381 __ test(key, Immediate(kSmiTagMask | kSmiSignMask));
382 __ j(not_zero, &slow);
384 // Everything is fine, call runtime.
386 __ push(receiver); // receiver
388 __ push(scratch); // return address
390 // Perform tail call to the entry.
391 __ TailCallRuntime(Runtime::kLoadElementWithInterceptor, 2, 1);
394 PropertyAccessCompiler::TailCallBuiltin(
395 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
399 void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
400 // Return address is on the stack.
403 Register receiver = LoadDescriptor::ReceiverRegister();
404 Register index = LoadDescriptor::NameRegister();
405 Register scratch = edi;
406 DCHECK(!scratch.is(receiver) && !scratch.is(index));
407 Register result = eax;
408 DCHECK(!result.is(scratch));
409 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
410 result.is(LoadDescriptor::SlotRegister()));
412 // StringCharAtGenerator doesn't use the result register until it's passed
413 // the different miss possibilities. If it did, we would have a conflict
414 // when FLAG_vector_ics is true.
416 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
417 &miss, // When not a string.
418 &miss, // When not a number.
419 &miss, // When index out of range.
420 STRING_INDEX_IS_ARRAY_INDEX,
422 char_at_generator.GenerateFast(masm);
425 StubRuntimeCallHelper call_helper;
426 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
429 PropertyAccessCompiler::TailCallBuiltin(
430 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
434 void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
435 // The key is in edx and the parameter count is in eax.
436 DCHECK(edx.is(ArgumentsAccessReadDescriptor::index()));
437 DCHECK(eax.is(ArgumentsAccessReadDescriptor::parameter_count()));
439 // The displacement is used for skipping the frame pointer on the
440 // stack. It is the offset of the last parameter (if any) relative
441 // to the frame pointer.
442 static const int kDisplacement = 1 * kPointerSize;
444 // Check that the key is a smi.
446 __ JumpIfNotSmi(edx, &slow, Label::kNear);
448 // Check if the calling frame is an arguments adaptor frame.
450 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
451 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
452 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
453 __ j(equal, &adaptor, Label::kNear);
455 // Check index against formal parameters count limit passed in
456 // through register eax. Use unsigned comparison to get negative
459 __ j(above_equal, &slow, Label::kNear);
461 // Read the argument from the stack and return it.
462 STATIC_ASSERT(kSmiTagSize == 1);
463 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
464 __ lea(ebx, Operand(ebp, eax, times_2, 0));
466 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
469 // Arguments adaptor case: Check index against actual arguments
470 // limit found in the arguments adaptor frame. Use unsigned
471 // comparison to get negative check for free.
473 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
475 __ j(above_equal, &slow, Label::kNear);
477 // Read the argument from the stack and return it.
478 STATIC_ASSERT(kSmiTagSize == 1);
479 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
480 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
482 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
485 // Slow-case: Handle non-smi or out-of-bounds access to arguments
486 // by calling the runtime system.
488 __ pop(ebx); // Return address.
491 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
495 void ArgumentsAccessStub::GenerateNewSloppySlow(MacroAssembler* masm) {
496 // esp[0] : return address
497 // esp[4] : number of parameters
498 // esp[8] : receiver displacement
499 // esp[12] : function
501 // Check if the calling frame is an arguments adaptor frame.
503 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
504 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
505 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
506 __ j(not_equal, &runtime, Label::kNear);
508 // Patch the arguments.length and the parameters pointer.
509 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
510 __ mov(Operand(esp, 1 * kPointerSize), ecx);
511 __ lea(edx, Operand(edx, ecx, times_2,
512 StandardFrameConstants::kCallerSPOffset));
513 __ mov(Operand(esp, 2 * kPointerSize), edx);
516 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
520 void ArgumentsAccessStub::GenerateNewSloppyFast(MacroAssembler* masm) {
521 // esp[0] : return address
522 // esp[4] : number of parameters (tagged)
523 // esp[8] : receiver displacement
524 // esp[12] : function
526 // ebx = parameter count (tagged)
527 __ mov(ebx, Operand(esp, 1 * kPointerSize));
529 // Check if the calling frame is an arguments adaptor frame.
530 // TODO(rossberg): Factor out some of the bits that are shared with the other
531 // Generate* functions.
533 Label adaptor_frame, try_allocate;
534 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
535 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
536 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
537 __ j(equal, &adaptor_frame, Label::kNear);
539 // No adaptor, parameter count = argument count.
541 __ jmp(&try_allocate, Label::kNear);
543 // We have an adaptor frame. Patch the parameters pointer.
544 __ bind(&adaptor_frame);
545 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
546 __ lea(edx, Operand(edx, ecx, times_2,
547 StandardFrameConstants::kCallerSPOffset));
548 __ mov(Operand(esp, 2 * kPointerSize), edx);
550 // ebx = parameter count (tagged)
551 // ecx = argument count (smi-tagged)
552 // esp[4] = parameter count (tagged)
553 // esp[8] = address of receiver argument
554 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
556 __ j(less_equal, &try_allocate, Label::kNear);
559 __ bind(&try_allocate);
561 // Save mapped parameter count.
564 // Compute the sizes of backing store, parameter map, and arguments object.
565 // 1. Parameter map, has 2 extra words containing context and backing store.
566 const int kParameterMapHeaderSize =
567 FixedArray::kHeaderSize + 2 * kPointerSize;
568 Label no_parameter_map;
570 __ j(zero, &no_parameter_map, Label::kNear);
571 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
572 __ bind(&no_parameter_map);
575 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
577 // 3. Arguments object.
578 __ add(ebx, Immediate(Heap::kSloppyArgumentsObjectSize));
580 // Do the allocation of all three objects in one go.
581 __ Allocate(ebx, eax, edx, edi, &runtime, TAG_OBJECT);
583 // eax = address of new object(s) (tagged)
584 // ecx = argument count (smi-tagged)
585 // esp[0] = mapped parameter count (tagged)
586 // esp[8] = parameter count (tagged)
587 // esp[12] = address of receiver argument
588 // Get the arguments map from the current native context into edi.
589 Label has_mapped_parameters, instantiate;
590 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
591 __ mov(edi, FieldOperand(edi, GlobalObject::kNativeContextOffset));
592 __ mov(ebx, Operand(esp, 0 * kPointerSize));
594 __ j(not_zero, &has_mapped_parameters, Label::kNear);
597 Operand(edi, Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX)));
598 __ jmp(&instantiate, Label::kNear);
600 __ bind(&has_mapped_parameters);
601 __ mov(edi, Operand(edi, Context::SlotOffset(
602 Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX)));
603 __ bind(&instantiate);
605 // eax = address of new object (tagged)
606 // ebx = mapped parameter count (tagged)
607 // ecx = argument count (smi-tagged)
608 // edi = address of arguments map (tagged)
609 // esp[0] = mapped parameter count (tagged)
610 // esp[8] = parameter count (tagged)
611 // esp[12] = address of receiver argument
612 // Copy the JS object part.
613 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
614 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
615 masm->isolate()->factory()->empty_fixed_array());
616 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
617 masm->isolate()->factory()->empty_fixed_array());
619 // Set up the callee in-object property.
620 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
621 __ mov(edx, Operand(esp, 4 * kPointerSize));
622 __ AssertNotSmi(edx);
623 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
624 Heap::kArgumentsCalleeIndex * kPointerSize),
627 // Use the length (smi tagged) and set that as an in-object property too.
629 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
630 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
631 Heap::kArgumentsLengthIndex * kPointerSize),
634 // Set up the elements pointer in the allocated arguments object.
635 // If we allocated a parameter map, edi will point there, otherwise to the
637 __ lea(edi, Operand(eax, Heap::kSloppyArgumentsObjectSize));
638 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
640 // eax = address of new object (tagged)
641 // ebx = mapped parameter count (tagged)
642 // ecx = argument count (tagged)
643 // edi = address of parameter map or backing store (tagged)
644 // esp[0] = mapped parameter count (tagged)
645 // esp[8] = parameter count (tagged)
646 // esp[12] = address of receiver argument
650 // Initialize parameter map. If there are no mapped arguments, we're done.
651 Label skip_parameter_map;
653 __ j(zero, &skip_parameter_map);
655 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
656 Immediate(isolate()->factory()->sloppy_arguments_elements_map()));
657 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
658 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
659 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
660 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
661 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
663 // Copy the parameter slots and the holes in the arguments.
664 // We need to fill in mapped_parameter_count slots. They index the context,
665 // where parameters are stored in reverse order, at
666 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
667 // The mapped parameter thus need to get indices
668 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
669 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
670 // We loop from right to left.
671 Label parameters_loop, parameters_test;
673 __ mov(eax, Operand(esp, 2 * kPointerSize));
674 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
675 __ add(ebx, Operand(esp, 4 * kPointerSize));
677 __ mov(ecx, isolate()->factory()->the_hole_value());
679 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
680 // eax = loop variable (tagged)
681 // ebx = mapping index (tagged)
682 // ecx = the hole value
683 // edx = address of parameter map (tagged)
684 // edi = address of backing store (tagged)
685 // esp[0] = argument count (tagged)
686 // esp[4] = address of new object (tagged)
687 // esp[8] = mapped parameter count (tagged)
688 // esp[16] = parameter count (tagged)
689 // esp[20] = address of receiver argument
690 __ jmp(¶meters_test, Label::kNear);
692 __ bind(¶meters_loop);
693 __ sub(eax, Immediate(Smi::FromInt(1)));
694 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
695 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
696 __ add(ebx, Immediate(Smi::FromInt(1)));
697 __ bind(¶meters_test);
699 __ j(not_zero, ¶meters_loop, Label::kNear);
702 __ bind(&skip_parameter_map);
704 // ecx = argument count (tagged)
705 // edi = address of backing store (tagged)
706 // esp[0] = address of new object (tagged)
707 // esp[4] = mapped parameter count (tagged)
708 // esp[12] = parameter count (tagged)
709 // esp[16] = address of receiver argument
710 // Copy arguments header and remaining slots (if there are any).
711 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
712 Immediate(isolate()->factory()->fixed_array_map()));
713 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
715 Label arguments_loop, arguments_test;
716 __ mov(ebx, Operand(esp, 1 * kPointerSize));
717 __ mov(edx, Operand(esp, 4 * kPointerSize));
718 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
720 __ jmp(&arguments_test, Label::kNear);
722 __ bind(&arguments_loop);
723 __ sub(edx, Immediate(kPointerSize));
724 __ mov(eax, Operand(edx, 0));
725 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
726 __ add(ebx, Immediate(Smi::FromInt(1)));
728 __ bind(&arguments_test);
730 __ j(less, &arguments_loop, Label::kNear);
733 __ pop(eax); // Address of arguments object.
734 __ pop(ebx); // Parameter count.
736 // Return and remove the on-stack parameters.
737 __ ret(3 * kPointerSize);
739 // Do the runtime call to allocate the arguments object.
741 __ pop(eax); // Remove saved parameter count.
742 __ mov(Operand(esp, 1 * kPointerSize), ecx); // Patch argument count.
743 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
747 void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
748 // esp[0] : return address
749 // esp[4] : number of parameters
750 // esp[8] : receiver displacement
751 // esp[12] : function
753 // Check if the calling frame is an arguments adaptor frame.
754 Label adaptor_frame, try_allocate, runtime;
755 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
756 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
757 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
758 __ j(equal, &adaptor_frame, Label::kNear);
760 // Get the length from the frame.
761 __ mov(ecx, Operand(esp, 1 * kPointerSize));
762 __ jmp(&try_allocate, Label::kNear);
764 // Patch the arguments.length and the parameters pointer.
765 __ bind(&adaptor_frame);
766 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
768 __ lea(edx, Operand(edx, ecx, times_2,
769 StandardFrameConstants::kCallerSPOffset));
770 __ mov(Operand(esp, 1 * kPointerSize), ecx);
771 __ mov(Operand(esp, 2 * kPointerSize), edx);
773 // Try the new space allocation. Start out with computing the size of
774 // the arguments object and the elements array.
775 Label add_arguments_object;
776 __ bind(&try_allocate);
778 __ j(zero, &add_arguments_object, Label::kNear);
779 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
780 __ bind(&add_arguments_object);
781 __ add(ecx, Immediate(Heap::kStrictArgumentsObjectSize));
783 // Do the allocation of both objects in one go.
784 __ Allocate(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
786 // Get the arguments map from the current native context.
787 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
788 __ mov(edi, FieldOperand(edi, GlobalObject::kNativeContextOffset));
789 const int offset = Context::SlotOffset(Context::STRICT_ARGUMENTS_MAP_INDEX);
790 __ mov(edi, Operand(edi, offset));
792 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
793 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
794 masm->isolate()->factory()->empty_fixed_array());
795 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
796 masm->isolate()->factory()->empty_fixed_array());
798 // Get the length (smi tagged) and set that as an in-object property too.
799 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
800 __ mov(ecx, Operand(esp, 1 * kPointerSize));
802 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
803 Heap::kArgumentsLengthIndex * kPointerSize),
806 // If there are no actual arguments, we're done.
809 __ j(zero, &done, Label::kNear);
811 // Get the parameters pointer from the stack.
812 __ mov(edx, Operand(esp, 2 * kPointerSize));
814 // Set up the elements pointer in the allocated arguments object and
815 // initialize the header in the elements fixed array.
816 __ lea(edi, Operand(eax, Heap::kStrictArgumentsObjectSize));
817 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
818 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
819 Immediate(isolate()->factory()->fixed_array_map()));
821 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
822 // Untag the length for the loop below.
825 // Copy the fixed array slots.
828 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
829 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
830 __ add(edi, Immediate(kPointerSize));
831 __ sub(edx, Immediate(kPointerSize));
833 __ j(not_zero, &loop);
835 // Return and remove the on-stack parameters.
837 __ ret(3 * kPointerSize);
839 // Do the runtime call to allocate the arguments object.
841 __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
845 void RestParamAccessStub::GenerateNew(MacroAssembler* masm) {
846 // esp[0] : return address
847 // esp[4] : language mode
848 // esp[8] : index of rest parameter
849 // esp[12] : number of parameters
850 // esp[16] : receiver displacement
852 // Check if the calling frame is an arguments adaptor frame.
854 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
855 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
856 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
857 __ j(not_equal, &runtime);
859 // Patch the arguments.length and the parameters pointer.
860 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
861 __ mov(Operand(esp, 3 * kPointerSize), ecx);
862 __ lea(edx, Operand(edx, ecx, times_2,
863 StandardFrameConstants::kCallerSPOffset));
864 __ mov(Operand(esp, 4 * kPointerSize), edx);
867 __ TailCallRuntime(Runtime::kNewRestParam, 4, 1);
871 void RegExpExecStub::Generate(MacroAssembler* masm) {
872 // Just jump directly to runtime if native RegExp is not selected at compile
873 // time or if regexp entry in generated code is turned off runtime switch or
875 #ifdef V8_INTERPRETED_REGEXP
876 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
877 #else // V8_INTERPRETED_REGEXP
879 // Stack frame on entry.
880 // esp[0]: return address
881 // esp[4]: last_match_info (expected JSArray)
882 // esp[8]: previous index
883 // esp[12]: subject string
884 // esp[16]: JSRegExp object
886 static const int kLastMatchInfoOffset = 1 * kPointerSize;
887 static const int kPreviousIndexOffset = 2 * kPointerSize;
888 static const int kSubjectOffset = 3 * kPointerSize;
889 static const int kJSRegExpOffset = 4 * kPointerSize;
892 Factory* factory = isolate()->factory();
894 // Ensure that a RegExp stack is allocated.
895 ExternalReference address_of_regexp_stack_memory_address =
896 ExternalReference::address_of_regexp_stack_memory_address(isolate());
897 ExternalReference address_of_regexp_stack_memory_size =
898 ExternalReference::address_of_regexp_stack_memory_size(isolate());
899 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
901 __ j(zero, &runtime);
903 // Check that the first argument is a JSRegExp object.
904 __ mov(eax, Operand(esp, kJSRegExpOffset));
905 STATIC_ASSERT(kSmiTag == 0);
906 __ JumpIfSmi(eax, &runtime);
907 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
908 __ j(not_equal, &runtime);
910 // Check that the RegExp has been compiled (data contains a fixed array).
911 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
912 if (FLAG_debug_code) {
913 __ test(ecx, Immediate(kSmiTagMask));
914 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
915 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
916 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
919 // ecx: RegExp data (FixedArray)
920 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
921 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
922 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
923 __ j(not_equal, &runtime);
925 // ecx: RegExp data (FixedArray)
926 // Check that the number of captures fit in the static offsets vector buffer.
927 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
928 // Check (number_of_captures + 1) * 2 <= offsets vector size
929 // Or number_of_captures * 2 <= offsets vector size - 2
930 // Multiplying by 2 comes for free since edx is smi-tagged.
931 STATIC_ASSERT(kSmiTag == 0);
932 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
933 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
934 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
935 __ j(above, &runtime);
937 // Reset offset for possibly sliced string.
938 __ Move(edi, Immediate(0));
939 __ mov(eax, Operand(esp, kSubjectOffset));
940 __ JumpIfSmi(eax, &runtime);
941 __ mov(edx, eax); // Make a copy of the original subject string.
942 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
943 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
945 // eax: subject string
946 // edx: subject string
947 // ebx: subject string instance type
948 // ecx: RegExp data (FixedArray)
949 // Handle subject string according to its encoding and representation:
950 // (1) Sequential two byte? If yes, go to (9).
951 // (2) Sequential one byte? If yes, go to (6).
952 // (3) Anything but sequential or cons? If yes, go to (7).
953 // (4) Cons string. If the string is flat, replace subject with first string.
954 // Otherwise bailout.
955 // (5a) Is subject sequential two byte? If yes, go to (9).
956 // (5b) Is subject external? If yes, go to (8).
957 // (6) One byte sequential. Load regexp code for one byte.
961 // Deferred code at the end of the stub:
962 // (7) Not a long external string? If yes, go to (10).
963 // (8) External string. Make it, offset-wise, look like a sequential string.
964 // (8a) Is the external string one byte? If yes, go to (6).
965 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
966 // (10) Short external string or not a string? If yes, bail out to runtime.
967 // (11) Sliced string. Replace subject with parent. Go to (5a).
969 Label seq_one_byte_string /* 6 */, seq_two_byte_string /* 9 */,
970 external_string /* 8 */, check_underlying /* 5a */,
971 not_seq_nor_cons /* 7 */, check_code /* E */,
972 not_long_external /* 10 */;
974 // (1) Sequential two byte? If yes, go to (9).
975 __ and_(ebx, kIsNotStringMask |
976 kStringRepresentationMask |
977 kStringEncodingMask |
978 kShortExternalStringMask);
979 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
980 __ j(zero, &seq_two_byte_string); // Go to (9).
982 // (2) Sequential one byte? If yes, go to (6).
983 // Any other sequential string must be one byte.
984 __ and_(ebx, Immediate(kIsNotStringMask |
985 kStringRepresentationMask |
986 kShortExternalStringMask));
987 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (6).
989 // (3) Anything but sequential or cons? If yes, go to (7).
990 // We check whether the subject string is a cons, since sequential strings
991 // have already been covered.
992 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
993 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
994 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
995 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
996 __ cmp(ebx, Immediate(kExternalStringTag));
997 __ j(greater_equal, ¬_seq_nor_cons); // Go to (7).
999 // (4) Cons string. Check that it's flat.
1000 // Replace subject with first string and reload instance type.
1001 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
1002 __ j(not_equal, &runtime);
1003 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
1004 __ bind(&check_underlying);
1005 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1006 __ mov(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1008 // (5a) Is subject sequential two byte? If yes, go to (9).
1009 __ test_b(ebx, kStringRepresentationMask | kStringEncodingMask);
1010 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
1011 __ j(zero, &seq_two_byte_string); // Go to (9).
1012 // (5b) Is subject external? If yes, go to (8).
1013 __ test_b(ebx, kStringRepresentationMask);
1014 // The underlying external string is never a short external string.
1015 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
1016 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
1017 __ j(not_zero, &external_string); // Go to (8).
1019 // eax: sequential subject string (or look-alike, external string)
1020 // edx: original subject string
1021 // ecx: RegExp data (FixedArray)
1022 // (6) One byte sequential. Load regexp code for one byte.
1023 __ bind(&seq_one_byte_string);
1024 // Load previous index and check range before edx is overwritten. We have
1025 // to use edx instead of eax here because it might have been only made to
1026 // look like a sequential string when it actually is an external string.
1027 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1028 __ JumpIfNotSmi(ebx, &runtime);
1029 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1030 __ j(above_equal, &runtime);
1031 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataOneByteCodeOffset));
1032 __ Move(ecx, Immediate(1)); // Type is one byte.
1034 // (E) Carry on. String handling is done.
1035 __ bind(&check_code);
1036 // edx: irregexp code
1037 // Check that the irregexp code has been generated for the actual string
1038 // encoding. If it has, the field contains a code object otherwise it contains
1039 // a smi (code flushing support).
1040 __ JumpIfSmi(edx, &runtime);
1042 // eax: subject string
1043 // ebx: previous index (smi)
1045 // ecx: encoding of subject string (1 if one_byte, 0 if two_byte);
1046 // All checks done. Now push arguments for native regexp code.
1047 Counters* counters = isolate()->counters();
1048 __ IncrementCounter(counters->regexp_entry_native(), 1);
1050 // Isolates: note we add an additional parameter here (isolate pointer).
1051 static const int kRegExpExecuteArguments = 9;
1052 __ EnterApiExitFrame(kRegExpExecuteArguments);
1054 // Argument 9: Pass current isolate address.
1055 __ mov(Operand(esp, 8 * kPointerSize),
1056 Immediate(ExternalReference::isolate_address(isolate())));
1058 // Argument 8: Indicate that this is a direct call from JavaScript.
1059 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
1061 // Argument 7: Start (high end) of backtracking stack memory area.
1062 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
1063 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
1064 __ mov(Operand(esp, 6 * kPointerSize), esi);
1066 // Argument 6: Set the number of capture registers to zero to force global
1067 // regexps to behave as non-global. This does not affect non-global regexps.
1068 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
1070 // Argument 5: static offsets vector buffer.
1071 __ mov(Operand(esp, 4 * kPointerSize),
1072 Immediate(ExternalReference::address_of_static_offsets_vector(
1075 // Argument 2: Previous index.
1077 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1079 // Argument 1: Original subject string.
1080 // The original subject is in the previous stack frame. Therefore we have to
1081 // use ebp, which points exactly to one pointer size below the previous esp.
1082 // (Because creating a new stack frame pushes the previous ebp onto the stack
1083 // and thereby moves up esp by one kPointerSize.)
1084 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
1085 __ mov(Operand(esp, 0 * kPointerSize), esi);
1087 // esi: original subject string
1088 // eax: underlying subject string
1089 // ebx: previous index
1090 // ecx: encoding of subject string (1 if one_byte 0 if two_byte);
1092 // Argument 4: End of string data
1093 // Argument 3: Start of string data
1094 // Prepare start and end index of the input.
1095 // Load the length from the original sliced string if that is the case.
1096 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
1097 __ add(esi, edi); // Calculate input end wrt offset.
1099 __ add(ebx, edi); // Calculate input start wrt offset.
1101 // ebx: start index of the input string
1102 // esi: end index of the input string
1103 Label setup_two_byte, setup_rest;
1105 __ j(zero, &setup_two_byte, Label::kNear);
1107 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
1108 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1109 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
1110 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1111 __ jmp(&setup_rest, Label::kNear);
1113 __ bind(&setup_two_byte);
1114 STATIC_ASSERT(kSmiTag == 0);
1115 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
1116 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
1117 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1118 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
1119 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1121 __ bind(&setup_rest);
1123 // Locate the code entry and call it.
1124 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1127 // Drop arguments and come back to JS mode.
1128 __ LeaveApiExitFrame(true);
1130 // Check the result.
1133 // We expect exactly one result since we force the called regexp to behave
1135 __ j(equal, &success);
1137 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
1138 __ j(equal, &failure);
1139 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
1140 // If not exception it can only be retry. Handle that in the runtime system.
1141 __ j(not_equal, &runtime);
1142 // Result must now be exception. If there is no pending exception already a
1143 // stack overflow (on the backtrack stack) was detected in RegExp code but
1144 // haven't created the exception yet. Handle that in the runtime system.
1145 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
1146 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
1148 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1149 __ mov(eax, Operand::StaticVariable(pending_exception));
1151 __ j(equal, &runtime);
1153 // For exception, throw the exception again.
1154 __ TailCallRuntime(Runtime::kRegExpExecReThrow, 4, 1);
1157 // For failure to match, return null.
1158 __ mov(eax, factory->null_value());
1159 __ ret(4 * kPointerSize);
1161 // Load RegExp data.
1163 __ mov(eax, Operand(esp, kJSRegExpOffset));
1164 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1165 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
1166 // Calculate number of capture registers (number_of_captures + 1) * 2.
1167 STATIC_ASSERT(kSmiTag == 0);
1168 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1169 __ add(edx, Immediate(2)); // edx was a smi.
1171 // edx: Number of capture registers
1172 // Load last_match_info which is still known to be a fast case JSArray.
1173 // Check that the fourth object is a JSArray object.
1174 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1175 __ JumpIfSmi(eax, &runtime);
1176 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
1177 __ j(not_equal, &runtime);
1178 // Check that the JSArray is in fast case.
1179 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
1180 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
1181 __ cmp(eax, factory->fixed_array_map());
1182 __ j(not_equal, &runtime);
1183 // Check that the last match info has space for the capture registers and the
1184 // additional information.
1185 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
1187 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
1189 __ j(greater, &runtime);
1191 // ebx: last_match_info backing store (FixedArray)
1192 // edx: number of capture registers
1193 // Store the capture count.
1194 __ SmiTag(edx); // Number of capture registers to smi.
1195 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
1196 __ SmiUntag(edx); // Number of capture registers back from smi.
1197 // Store last subject and last input.
1198 __ mov(eax, Operand(esp, kSubjectOffset));
1200 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
1201 __ RecordWriteField(ebx, RegExpImpl::kLastSubjectOffset, eax, edi,
1204 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
1205 __ RecordWriteField(ebx, RegExpImpl::kLastInputOffset, eax, edi,
1208 // Get the static offsets vector filled by the native regexp code.
1209 ExternalReference address_of_static_offsets_vector =
1210 ExternalReference::address_of_static_offsets_vector(isolate());
1211 __ mov(ecx, Immediate(address_of_static_offsets_vector));
1213 // ebx: last_match_info backing store (FixedArray)
1214 // ecx: offsets vector
1215 // edx: number of capture registers
1216 Label next_capture, done;
1217 // Capture register counter starts from number of capture registers and
1218 // counts down until wraping after zero.
1219 __ bind(&next_capture);
1220 __ sub(edx, Immediate(1));
1221 __ j(negative, &done, Label::kNear);
1222 // Read the value from the static offsets vector buffer.
1223 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
1225 // Store the smi value in the last match info.
1226 __ mov(FieldOperand(ebx,
1229 RegExpImpl::kFirstCaptureOffset),
1231 __ jmp(&next_capture);
1234 // Return last match info.
1235 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1236 __ ret(4 * kPointerSize);
1238 // Do the runtime call to execute the regexp.
1240 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1242 // Deferred code for string handling.
1243 // (7) Not a long external string? If yes, go to (10).
1244 __ bind(¬_seq_nor_cons);
1245 // Compare flags are still set from (3).
1246 __ j(greater, ¬_long_external, Label::kNear); // Go to (10).
1248 // (8) External string. Short external strings have been ruled out.
1249 __ bind(&external_string);
1250 // Reload instance type.
1251 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1252 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1253 if (FLAG_debug_code) {
1254 // Assert that we do not have a cons or slice (indirect strings) here.
1255 // Sequential strings have already been ruled out.
1256 __ test_b(ebx, kIsIndirectStringMask);
1257 __ Assert(zero, kExternalStringExpectedButNotFound);
1259 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
1260 // Move the pointer so that offset-wise, it looks like a sequential string.
1261 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
1262 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
1263 STATIC_ASSERT(kTwoByteStringTag == 0);
1264 // (8a) Is the external string one byte? If yes, go to (6).
1265 __ test_b(ebx, kStringEncodingMask);
1266 __ j(not_zero, &seq_one_byte_string); // Goto (6).
1268 // eax: sequential subject string (or look-alike, external string)
1269 // edx: original subject string
1270 // ecx: RegExp data (FixedArray)
1271 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1272 __ bind(&seq_two_byte_string);
1273 // Load previous index and check range before edx is overwritten. We have
1274 // to use edx instead of eax here because it might have been only made to
1275 // look like a sequential string when it actually is an external string.
1276 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1277 __ JumpIfNotSmi(ebx, &runtime);
1278 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1279 __ j(above_equal, &runtime);
1280 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
1281 __ Move(ecx, Immediate(0)); // Type is two byte.
1282 __ jmp(&check_code); // Go to (E).
1284 // (10) Not a string or a short external string? If yes, bail out to runtime.
1285 __ bind(¬_long_external);
1286 // Catch non-string subject or short external string.
1287 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1288 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
1289 __ j(not_zero, &runtime);
1291 // (11) Sliced string. Replace subject with parent. Go to (5a).
1292 // Load offset into edi and replace subject string with parent.
1293 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
1294 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
1295 __ jmp(&check_underlying); // Go to (5a).
1296 #endif // V8_INTERPRETED_REGEXP
1300 static int NegativeComparisonResult(Condition cc) {
1301 DCHECK(cc != equal);
1302 DCHECK((cc == less) || (cc == less_equal)
1303 || (cc == greater) || (cc == greater_equal));
1304 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
1308 static void CheckInputType(MacroAssembler* masm, Register input,
1309 CompareICState::State expected, Label* fail) {
1311 if (expected == CompareICState::SMI) {
1312 __ JumpIfNotSmi(input, fail);
1313 } else if (expected == CompareICState::NUMBER) {
1314 __ JumpIfSmi(input, &ok);
1315 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
1316 Immediate(masm->isolate()->factory()->heap_number_map()));
1317 __ j(not_equal, fail);
1319 // We could be strict about internalized/non-internalized here, but as long as
1320 // hydrogen doesn't care, the stub doesn't have to care either.
1325 static void BranchIfNotInternalizedString(MacroAssembler* masm,
1329 __ JumpIfSmi(object, label);
1330 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
1331 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
1332 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
1333 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
1334 __ j(not_zero, label);
1338 void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
1339 Label runtime_call, check_unequal_objects;
1340 Condition cc = GetCondition();
1343 CheckInputType(masm, edx, left(), &miss);
1344 CheckInputType(masm, eax, right(), &miss);
1346 // Compare two smis.
1347 Label non_smi, smi_done;
1350 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
1351 __ sub(edx, eax); // Return on the result of the subtraction.
1352 __ j(no_overflow, &smi_done, Label::kNear);
1353 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
1359 // NOTICE! This code is only reached after a smi-fast-case check, so
1360 // it is certain that at least one operand isn't a smi.
1362 // Identical objects can be compared fast, but there are some tricky cases
1363 // for NaN and undefined.
1364 Label generic_heap_number_comparison;
1366 Label not_identical;
1368 __ j(not_equal, ¬_identical);
1371 // Check for undefined. undefined OP undefined is false even though
1372 // undefined == undefined.
1373 __ cmp(edx, isolate()->factory()->undefined_value());
1374 if (is_strong(strength())) {
1375 // In strong mode, this comparison must throw, so call the runtime.
1376 __ j(equal, &runtime_call, Label::kFar);
1378 Label check_for_nan;
1379 __ j(not_equal, &check_for_nan, Label::kNear);
1380 __ Move(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1382 __ bind(&check_for_nan);
1386 // Test for NaN. Compare heap numbers in a general way,
1387 // to handle NaNs correctly.
1388 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
1389 Immediate(isolate()->factory()->heap_number_map()));
1390 __ j(equal, &generic_heap_number_comparison, Label::kNear);
1392 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1393 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
1394 // Call runtime on identical JSObjects. Otherwise return equal.
1395 __ cmpb(ecx, static_cast<uint8_t>(FIRST_SPEC_OBJECT_TYPE));
1396 __ j(above_equal, &runtime_call, Label::kFar);
1397 // Call runtime on identical symbols since we need to throw a TypeError.
1398 __ cmpb(ecx, static_cast<uint8_t>(SYMBOL_TYPE));
1399 __ j(equal, &runtime_call, Label::kFar);
1400 // Call runtime on identical SIMD values since we must throw a TypeError.
1401 __ cmpb(ecx, static_cast<uint8_t>(FLOAT32X4_TYPE));
1402 __ j(equal, &runtime_call, Label::kFar);
1403 if (is_strong(strength())) {
1404 // We have already tested for smis and heap numbers, so if both
1405 // arguments are not strings we must proceed to the slow case.
1406 __ test(ecx, Immediate(kIsNotStringMask));
1407 __ j(not_zero, &runtime_call, Label::kFar);
1410 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
1414 __ bind(¬_identical);
1417 // Strict equality can quickly decide whether objects are equal.
1418 // Non-strict object equality is slower, so it is handled later in the stub.
1419 if (cc == equal && strict()) {
1420 Label slow; // Fallthrough label.
1422 // If we're doing a strict equality comparison, we don't have to do
1423 // type conversion, so we generate code to do fast comparison for objects
1424 // and oddballs. Non-smi numbers and strings still go through the usual
1426 // If either is a Smi (we know that not both are), then they can only
1427 // be equal if the other is a HeapNumber. If so, use the slow case.
1428 STATIC_ASSERT(kSmiTag == 0);
1429 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1430 __ mov(ecx, Immediate(kSmiTagMask));
1433 __ j(not_zero, ¬_smis, Label::kNear);
1434 // One operand is a smi.
1436 // Check whether the non-smi is a heap number.
1437 STATIC_ASSERT(kSmiTagMask == 1);
1438 // ecx still holds eax & kSmiTag, which is either zero or one.
1439 __ sub(ecx, Immediate(0x01));
1442 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
1444 // if eax was smi, ebx is now edx, else eax.
1446 // Check if the non-smi operand is a heap number.
1447 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1448 Immediate(isolate()->factory()->heap_number_map()));
1449 // If heap number, handle it in the slow case.
1450 __ j(equal, &slow, Label::kNear);
1451 // Return non-equal (ebx is not zero)
1456 // If either operand is a JSObject or an oddball value, then they are not
1457 // equal since their pointers are different
1458 // There is no test for undetectability in strict equality.
1460 // Get the type of the first operand.
1461 // If the first object is a JS object, we have done pointer comparison.
1462 Label first_non_object;
1463 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
1464 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1465 __ j(below, &first_non_object, Label::kNear);
1467 // Return non-zero (eax is not zero)
1468 Label return_not_equal;
1469 STATIC_ASSERT(kHeapObjectTag != 0);
1470 __ bind(&return_not_equal);
1473 __ bind(&first_non_object);
1474 // Check for oddballs: true, false, null, undefined.
1475 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1476 __ j(equal, &return_not_equal);
1478 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ecx);
1479 __ j(above_equal, &return_not_equal);
1481 // Check for oddballs: true, false, null, undefined.
1482 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1483 __ j(equal, &return_not_equal);
1485 // Fall through to the general case.
1489 // Generate the number comparison code.
1490 Label non_number_comparison;
1492 __ bind(&generic_heap_number_comparison);
1493 FloatingPointHelper::CheckFloatOperands(
1494 masm, &non_number_comparison, ebx);
1495 FloatingPointHelper::LoadFloatOperand(masm, eax);
1496 FloatingPointHelper::LoadFloatOperand(masm, edx);
1499 // Don't base result on EFLAGS when a NaN is involved.
1500 __ j(parity_even, &unordered, Label::kNear);
1502 Label below_label, above_label;
1503 // Return a result of -1, 0, or 1, based on EFLAGS.
1504 __ j(below, &below_label, Label::kNear);
1505 __ j(above, &above_label, Label::kNear);
1507 __ Move(eax, Immediate(0));
1510 __ bind(&below_label);
1511 __ mov(eax, Immediate(Smi::FromInt(-1)));
1514 __ bind(&above_label);
1515 __ mov(eax, Immediate(Smi::FromInt(1)));
1518 // If one of the numbers was NaN, then the result is always false.
1519 // The cc is never not-equal.
1520 __ bind(&unordered);
1521 DCHECK(cc != not_equal);
1522 if (cc == less || cc == less_equal) {
1523 __ mov(eax, Immediate(Smi::FromInt(1)));
1525 __ mov(eax, Immediate(Smi::FromInt(-1)));
1529 // The number comparison code did not provide a valid result.
1530 __ bind(&non_number_comparison);
1532 // Fast negative check for internalized-to-internalized equality.
1533 Label check_for_strings;
1535 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
1536 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
1538 // We've already checked for object identity, so if both operands
1539 // are internalized they aren't equal. Register eax already holds a
1540 // non-zero value, which indicates not equal, so just return.
1544 __ bind(&check_for_strings);
1546 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx,
1547 &check_unequal_objects);
1549 // Inline comparison of one-byte strings.
1551 StringHelper::GenerateFlatOneByteStringEquals(masm, edx, eax, ecx, ebx);
1553 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
1557 __ Abort(kUnexpectedFallThroughFromStringComparison);
1560 __ bind(&check_unequal_objects);
1561 if (cc == equal && !strict()) {
1562 // Non-strict equality. Objects are unequal if
1563 // they are both JSObjects and not undetectable,
1564 // and their pointers are different.
1565 Label return_unequal;
1566 // At most one is a smi, so we can test for smi by adding the two.
1567 // A smi plus a heap object has the low bit set, a heap object plus
1568 // a heap object has the low bit clear.
1569 STATIC_ASSERT(kSmiTag == 0);
1570 STATIC_ASSERT(kSmiTagMask == 1);
1571 __ lea(ecx, Operand(eax, edx, times_1, 0));
1572 __ test(ecx, Immediate(kSmiTagMask));
1573 __ j(not_zero, &runtime_call, Label::kNear);
1574 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1575 __ j(below, &runtime_call, Label::kNear);
1576 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ebx);
1577 __ j(below, &runtime_call, Label::kNear);
1578 // We do not bail out after this point. Both are JSObjects, and
1579 // they are equal if and only if both are undetectable.
1580 // The and of the undetectable flags is 1 if and only if they are equal.
1581 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
1582 1 << Map::kIsUndetectable);
1583 __ j(zero, &return_unequal, Label::kNear);
1584 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
1585 1 << Map::kIsUndetectable);
1586 __ j(zero, &return_unequal, Label::kNear);
1587 // The objects are both undetectable, so they both compare as the value
1588 // undefined, and are equal.
1589 __ Move(eax, Immediate(EQUAL));
1590 __ bind(&return_unequal);
1591 // Return non-equal by returning the non-zero object pointer in eax,
1592 // or return equal if we fell through to here.
1593 __ ret(0); // rax, rdx were pushed
1595 __ bind(&runtime_call);
1597 // Push arguments below the return address.
1602 // Figure out which native to call and setup the arguments.
1603 Builtins::JavaScript builtin;
1605 builtin = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1608 is_strong(strength()) ? Builtins::COMPARE_STRONG : Builtins::COMPARE;
1609 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1612 // Restore return address on the stack.
1615 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1616 // tagged as a small integer.
1617 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
1624 static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub,
1626 // eax : number of arguments to the construct function
1627 // ebx : feedback vector
1628 // edx : slot in feedback vector (Smi)
1629 // edi : the function to call
1630 // esp[0]: original receiver (for IsSuperConstructorCall)
1636 FrameScope scope(masm, StackFrame::INTERNAL);
1638 // Number-of-arguments register must be smi-tagged to call out.
1666 static void GenerateRecordCallTarget(MacroAssembler* masm, bool is_super) {
1667 // Cache the called function in a feedback vector slot. Cache states
1668 // are uninitialized, monomorphic (indicated by a JSFunction), and
1670 // eax : number of arguments to the construct function
1671 // ebx : feedback vector
1672 // edx : slot in feedback vector (Smi)
1673 // edi : the function to call
1674 // esp[0]: original receiver (for IsSuperConstructorCall)
1675 Isolate* isolate = masm->isolate();
1676 Label initialize, done, miss, megamorphic, not_array_function;
1678 // Load the cache state into ecx.
1679 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1680 FixedArray::kHeaderSize));
1682 // A monomorphic cache hit or an already megamorphic state: invoke the
1683 // function without changing the state.
1684 // We don't know if ecx is a WeakCell or a Symbol, but it's harmless to read
1685 // at this position in a symbol (see static asserts in
1686 // type-feedback-vector.h).
1687 Label check_allocation_site;
1688 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
1689 __ j(equal, &done, Label::kFar);
1690 __ CompareRoot(ecx, Heap::kmegamorphic_symbolRootIndex);
1691 __ j(equal, &done, Label::kFar);
1692 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1693 Heap::kWeakCellMapRootIndex);
1694 __ j(not_equal, FLAG_pretenuring_call_new ? &miss : &check_allocation_site);
1696 // If the weak cell is cleared, we have a new chance to become monomorphic.
1697 __ JumpIfSmi(FieldOperand(ecx, WeakCell::kValueOffset), &initialize);
1698 __ jmp(&megamorphic);
1700 if (!FLAG_pretenuring_call_new) {
1701 __ bind(&check_allocation_site);
1702 // If we came here, we need to see if we are the array function.
1703 // If we didn't have a matching function, and we didn't find the megamorph
1704 // sentinel, then we have in the slot either some other function or an
1706 __ CompareRoot(FieldOperand(ecx, 0), Heap::kAllocationSiteMapRootIndex);
1707 __ j(not_equal, &miss);
1709 // Make sure the function is the Array() function
1710 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1712 __ j(not_equal, &megamorphic);
1713 __ jmp(&done, Label::kFar);
1718 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1720 __ CompareRoot(ecx, Heap::kuninitialized_symbolRootIndex);
1721 __ j(equal, &initialize);
1722 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1723 // write-barrier is needed.
1724 __ bind(&megamorphic);
1726 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1727 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
1728 __ jmp(&done, Label::kFar);
1730 // An uninitialized cache is patched with the function or sentinel to
1731 // indicate the ElementsKind if function is the Array constructor.
1732 __ bind(&initialize);
1733 if (!FLAG_pretenuring_call_new) {
1734 // Make sure the function is the Array() function
1735 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1737 __ j(not_equal, ¬_array_function);
1739 // The target function is the Array constructor,
1740 // Create an AllocationSite if we don't already have it, store it in the
1742 CreateAllocationSiteStub create_stub(isolate);
1743 CallStubInRecordCallTarget(masm, &create_stub, is_super);
1746 __ bind(¬_array_function);
1749 CreateWeakCellStub create_stub(isolate);
1750 CallStubInRecordCallTarget(masm, &create_stub, is_super);
1755 static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
1756 // Do not transform the receiver for strict mode functions.
1757 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1758 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset),
1759 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
1760 __ j(not_equal, cont);
1762 // Do not transform the receiver for natives (shared already in ecx).
1763 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset),
1764 1 << SharedFunctionInfo::kNativeBitWithinByte);
1765 __ j(not_equal, cont);
1769 static void EmitSlowCase(Isolate* isolate,
1770 MacroAssembler* masm,
1772 Label* non_function) {
1773 // Check for function proxy.
1774 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
1775 __ j(not_equal, non_function);
1777 __ push(edi); // put proxy as additional argument under return address
1779 __ Move(eax, Immediate(argc + 1));
1780 __ Move(ebx, Immediate(0));
1781 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
1783 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
1784 __ jmp(adaptor, RelocInfo::CODE_TARGET);
1787 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
1788 // of the original receiver from the call site).
1789 __ bind(non_function);
1790 __ mov(Operand(esp, (argc + 1) * kPointerSize), edi);
1791 __ Move(eax, Immediate(argc));
1792 __ Move(ebx, Immediate(0));
1793 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
1794 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
1795 __ jmp(adaptor, RelocInfo::CODE_TARGET);
1799 static void EmitWrapCase(MacroAssembler* masm, int argc, Label* cont) {
1800 // Wrap the receiver and patch it back onto the stack.
1801 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
1803 ToObjectStub stub(masm->isolate());
1807 __ mov(Operand(esp, (argc + 1) * kPointerSize), eax);
1812 static void CallFunctionNoFeedback(MacroAssembler* masm,
1813 int argc, bool needs_checks,
1814 bool call_as_method) {
1815 // edi : the function to call
1816 Label slow, non_function, wrap, cont;
1819 // Check that the function really is a JavaScript function.
1820 __ JumpIfSmi(edi, &non_function);
1822 // Goto slow case if we do not have a function.
1823 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1824 __ j(not_equal, &slow);
1827 // Fast-case: Just invoke the function.
1828 ParameterCount actual(argc);
1830 if (call_as_method) {
1832 EmitContinueIfStrictOrNative(masm, &cont);
1835 // Load the receiver from the stack.
1836 __ mov(eax, Operand(esp, (argc + 1) * kPointerSize));
1839 __ JumpIfSmi(eax, &wrap);
1841 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1850 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
1853 // Slow-case: Non-function called.
1855 // (non_function is bound in EmitSlowCase)
1856 EmitSlowCase(masm->isolate(), masm, argc, &non_function);
1859 if (call_as_method) {
1861 EmitWrapCase(masm, argc, &cont);
1866 void CallFunctionStub::Generate(MacroAssembler* masm) {
1867 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
1871 void CallConstructStub::Generate(MacroAssembler* masm) {
1872 // eax : number of arguments
1873 // ebx : feedback vector
1874 // ecx : original constructor (for IsSuperConstructorCall)
1875 // edx : slot in feedback vector (Smi, for RecordCallTarget)
1876 // edi : constructor function
1877 Label slow, non_function_call;
1879 if (IsSuperConstructorCall()) {
1883 // Check that function is not a smi.
1884 __ JumpIfSmi(edi, &non_function_call);
1885 // Check that function is a JSFunction.
1886 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1887 __ j(not_equal, &slow);
1889 if (RecordCallTarget()) {
1890 GenerateRecordCallTarget(masm, IsSuperConstructorCall());
1892 if (FLAG_pretenuring_call_new) {
1893 // Put the AllocationSite from the feedback vector into ebx.
1894 // By adding kPointerSize we encode that we know the AllocationSite
1895 // entry is at the feedback vector slot given by edx + 1.
1896 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
1897 FixedArray::kHeaderSize + kPointerSize));
1899 Label feedback_register_initialized;
1900 // Put the AllocationSite from the feedback vector into ebx, or undefined.
1901 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
1902 FixedArray::kHeaderSize));
1903 Handle<Map> allocation_site_map =
1904 isolate()->factory()->allocation_site_map();
1905 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
1906 __ j(equal, &feedback_register_initialized);
1907 __ mov(ebx, isolate()->factory()->undefined_value());
1908 __ bind(&feedback_register_initialized);
1911 __ AssertUndefinedOrAllocationSite(ebx);
1914 if (IsSuperConstructorCall()) {
1917 // Pass original constructor to construct stub.
1921 // Jump to the function-specific construct stub.
1922 Register jmp_reg = ecx;
1923 __ mov(jmp_reg, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1924 __ mov(jmp_reg, FieldOperand(jmp_reg,
1925 SharedFunctionInfo::kConstructStubOffset));
1926 __ lea(jmp_reg, FieldOperand(jmp_reg, Code::kHeaderSize));
1929 // edi: called object
1930 // eax: number of arguments
1932 // esp[0]: original receiver (for IsSuperConstructorCall)
1935 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
1936 __ j(not_equal, &non_function_call);
1937 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
1940 __ bind(&non_function_call);
1941 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
1943 if (IsSuperConstructorCall()) {
1946 // Set expected number of arguments to zero (not changing eax).
1947 __ Move(ebx, Immediate(0));
1948 Handle<Code> arguments_adaptor =
1949 isolate()->builtins()->ArgumentsAdaptorTrampoline();
1950 __ jmp(arguments_adaptor, RelocInfo::CODE_TARGET);
1954 static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
1955 __ mov(vector, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
1956 __ mov(vector, FieldOperand(vector, JSFunction::kSharedFunctionInfoOffset));
1957 __ mov(vector, FieldOperand(vector,
1958 SharedFunctionInfo::kFeedbackVectorOffset));
1962 void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
1967 int argc = arg_count();
1968 ParameterCount actual(argc);
1970 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1972 __ j(not_equal, &miss);
1974 __ mov(eax, arg_count());
1975 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1976 FixedArray::kHeaderSize));
1978 // Verify that ecx contains an AllocationSite
1979 Factory* factory = masm->isolate()->factory();
1980 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
1981 factory->allocation_site_map());
1982 __ j(not_equal, &miss);
1984 // Increment the call count for monomorphic function calls.
1985 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1986 FixedArray::kHeaderSize + kPointerSize),
1987 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
1991 ArrayConstructorStub stub(masm->isolate(), arg_count());
1992 __ TailCallStub(&stub);
1997 // The slow case, we need this no matter what to complete a call after a miss.
1998 CallFunctionNoFeedback(masm,
2008 void CallICStub::Generate(MacroAssembler* masm) {
2012 Isolate* isolate = masm->isolate();
2013 const int with_types_offset =
2014 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kWithTypesIndex);
2015 const int generic_offset =
2016 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kGenericCountIndex);
2017 Label extra_checks_or_miss, slow_start;
2018 Label slow, non_function, wrap, cont;
2019 Label have_js_function;
2020 int argc = arg_count();
2021 ParameterCount actual(argc);
2023 // The checks. First, does edi match the recorded monomorphic target?
2024 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
2025 FixedArray::kHeaderSize));
2027 // We don't know that we have a weak cell. We might have a private symbol
2028 // or an AllocationSite, but the memory is safe to examine.
2029 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
2031 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
2032 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
2033 // computed, meaning that it can't appear to be a pointer. If the low bit is
2034 // 0, then hash is computed, but the 0 bit prevents the field from appearing
2036 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
2037 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
2038 WeakCell::kValueOffset &&
2039 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
2041 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
2042 __ j(not_equal, &extra_checks_or_miss);
2044 // The compare above could have been a SMI/SMI comparison. Guard against this
2045 // convincing us that we have a monomorphic JSFunction.
2046 __ JumpIfSmi(edi, &extra_checks_or_miss);
2048 // Increment the call count for monomorphic function calls.
2049 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
2050 FixedArray::kHeaderSize + kPointerSize),
2051 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2053 __ bind(&have_js_function);
2054 if (CallAsMethod()) {
2055 EmitContinueIfStrictOrNative(masm, &cont);
2057 // Load the receiver from the stack.
2058 __ mov(eax, Operand(esp, (argc + 1) * kPointerSize));
2060 __ JumpIfSmi(eax, &wrap);
2062 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
2068 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
2071 EmitSlowCase(isolate, masm, argc, &non_function);
2073 if (CallAsMethod()) {
2075 EmitWrapCase(masm, argc, &cont);
2078 __ bind(&extra_checks_or_miss);
2079 Label uninitialized, miss;
2081 __ cmp(ecx, Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2082 __ j(equal, &slow_start);
2084 // The following cases attempt to handle MISS cases without going to the
2086 if (FLAG_trace_ic) {
2090 __ cmp(ecx, Immediate(TypeFeedbackVector::UninitializedSentinel(isolate)));
2091 __ j(equal, &uninitialized);
2093 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2094 // to handle it here. More complex cases are dealt with in the runtime.
2095 __ AssertNotSmi(ecx);
2096 __ CmpObjectType(ecx, JS_FUNCTION_TYPE, ecx);
2097 __ j(not_equal, &miss);
2099 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
2100 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2101 // We have to update statistics for runtime profiling.
2102 __ sub(FieldOperand(ebx, with_types_offset), Immediate(Smi::FromInt(1)));
2103 __ add(FieldOperand(ebx, generic_offset), Immediate(Smi::FromInt(1)));
2104 __ jmp(&slow_start);
2106 __ bind(&uninitialized);
2108 // We are going monomorphic, provided we actually have a JSFunction.
2109 __ JumpIfSmi(edi, &miss);
2111 // Goto miss case if we do not have a function.
2112 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2113 __ j(not_equal, &miss);
2115 // Make sure the function is not the Array() function, which requires special
2116 // behavior on MISS.
2117 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2122 __ add(FieldOperand(ebx, with_types_offset), Immediate(Smi::FromInt(1)));
2124 // Initialize the call counter.
2125 __ mov(FieldOperand(ebx, edx, times_half_pointer_size,
2126 FixedArray::kHeaderSize + kPointerSize),
2127 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2129 // Store the function. Use a stub since we need a frame for allocation.
2134 FrameScope scope(masm, StackFrame::INTERNAL);
2135 CreateWeakCellStub create_stub(isolate);
2137 __ CallStub(&create_stub);
2141 __ jmp(&have_js_function);
2143 // We are here because tracing is on or we encountered a MISS case we can't
2149 __ bind(&slow_start);
2151 // Check that the function really is a JavaScript function.
2152 __ JumpIfSmi(edi, &non_function);
2154 // Goto slow case if we do not have a function.
2155 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2156 __ j(not_equal, &slow);
2157 __ jmp(&have_js_function);
2164 void CallICStub::GenerateMiss(MacroAssembler* masm) {
2165 FrameScope scope(masm, StackFrame::INTERNAL);
2167 // Push the receiver and the function and feedback info.
2173 Runtime::FunctionId id = GetICState() == DEFAULT
2174 ? Runtime::kCallIC_Miss
2175 : Runtime::kCallIC_Customization_Miss;
2176 __ CallRuntime(id, 3);
2178 // Move result to edi and exit the internal frame.
2183 bool CEntryStub::NeedsImmovableCode() {
2188 void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
2189 CEntryStub::GenerateAheadOfTime(isolate);
2190 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
2191 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
2192 // It is important that the store buffer overflow stubs are generated first.
2193 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
2194 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
2195 CreateWeakCellStub::GenerateAheadOfTime(isolate);
2196 BinaryOpICStub::GenerateAheadOfTime(isolate);
2197 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
2198 StoreFastElementStub::GenerateAheadOfTime(isolate);
2199 TypeofStub::GenerateAheadOfTime(isolate);
2203 void CodeStub::GenerateFPStubs(Isolate* isolate) {
2204 CEntryStub save_doubles(isolate, 1, kSaveFPRegs);
2205 // Stubs might already be in the snapshot, detect that and don't regenerate,
2206 // which would lead to code stub initialization state being messed up.
2207 Code* save_doubles_code;
2208 if (!save_doubles.FindCodeInCache(&save_doubles_code)) {
2209 save_doubles_code = *(save_doubles.GetCode());
2211 isolate->set_fp_stubs_generated(true);
2215 void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
2216 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
2221 void CEntryStub::Generate(MacroAssembler* masm) {
2222 // eax: number of arguments including receiver
2223 // ebx: pointer to C function (C callee-saved)
2224 // ebp: frame pointer (restored after C call)
2225 // esp: stack pointer (restored after C call)
2226 // esi: current context (C callee-saved)
2227 // edi: JS function of the caller (C callee-saved)
2229 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2231 // Enter the exit frame that transitions from JavaScript to C++.
2232 __ EnterExitFrame(save_doubles());
2234 // ebx: pointer to C function (C callee-saved)
2235 // ebp: frame pointer (restored after C call)
2236 // esp: stack pointer (restored after C call)
2237 // edi: number of arguments including receiver (C callee-saved)
2238 // esi: pointer to the first argument (C callee-saved)
2240 // Result returned in eax, or eax+edx if result size is 2.
2242 // Check stack alignment.
2243 if (FLAG_debug_code) {
2244 __ CheckStackAlignment();
2248 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
2249 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
2250 __ mov(Operand(esp, 2 * kPointerSize),
2251 Immediate(ExternalReference::isolate_address(isolate())));
2253 // Result is in eax or edx:eax - do not destroy these registers!
2255 // Check result for exception sentinel.
2256 Label exception_returned;
2257 __ cmp(eax, isolate()->factory()->exception());
2258 __ j(equal, &exception_returned);
2260 // Check that there is no pending exception, otherwise we
2261 // should have returned the exception sentinel.
2262 if (FLAG_debug_code) {
2264 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
2266 ExternalReference pending_exception_address(
2267 Isolate::kPendingExceptionAddress, isolate());
2268 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
2269 // Cannot use check here as it attempts to generate call into runtime.
2270 __ j(equal, &okay, Label::kNear);
2276 // Exit the JavaScript to C++ exit frame.
2277 __ LeaveExitFrame(save_doubles());
2280 // Handling of exception.
2281 __ bind(&exception_returned);
2283 ExternalReference pending_handler_context_address(
2284 Isolate::kPendingHandlerContextAddress, isolate());
2285 ExternalReference pending_handler_code_address(
2286 Isolate::kPendingHandlerCodeAddress, isolate());
2287 ExternalReference pending_handler_offset_address(
2288 Isolate::kPendingHandlerOffsetAddress, isolate());
2289 ExternalReference pending_handler_fp_address(
2290 Isolate::kPendingHandlerFPAddress, isolate());
2291 ExternalReference pending_handler_sp_address(
2292 Isolate::kPendingHandlerSPAddress, isolate());
2294 // Ask the runtime for help to determine the handler. This will set eax to
2295 // contain the current pending exception, don't clobber it.
2296 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
2299 FrameScope scope(masm, StackFrame::MANUAL);
2300 __ PrepareCallCFunction(3, eax);
2301 __ mov(Operand(esp, 0 * kPointerSize), Immediate(0)); // argc.
2302 __ mov(Operand(esp, 1 * kPointerSize), Immediate(0)); // argv.
2303 __ mov(Operand(esp, 2 * kPointerSize),
2304 Immediate(ExternalReference::isolate_address(isolate())));
2305 __ CallCFunction(find_handler, 3);
2308 // Retrieve the handler context, SP and FP.
2309 __ mov(esi, Operand::StaticVariable(pending_handler_context_address));
2310 __ mov(esp, Operand::StaticVariable(pending_handler_sp_address));
2311 __ mov(ebp, Operand::StaticVariable(pending_handler_fp_address));
2313 // If the handler is a JS frame, restore the context to the frame. Note that
2314 // the context will be set to (esi == 0) for non-JS frames.
2317 __ j(zero, &skip, Label::kNear);
2318 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
2321 // Compute the handler entry address and jump to it.
2322 __ mov(edi, Operand::StaticVariable(pending_handler_code_address));
2323 __ mov(edx, Operand::StaticVariable(pending_handler_offset_address));
2324 __ lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
2329 void JSEntryStub::Generate(MacroAssembler* masm) {
2330 Label invoke, handler_entry, exit;
2331 Label not_outermost_js, not_outermost_js_2;
2333 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2339 // Push marker in two places.
2340 int marker = type();
2341 __ push(Immediate(Smi::FromInt(marker))); // context slot
2342 __ push(Immediate(Smi::FromInt(marker))); // function slot
2343 // Save callee-saved registers (C calling conventions).
2348 // Save copies of the top frame descriptor on the stack.
2349 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
2350 __ push(Operand::StaticVariable(c_entry_fp));
2352 // If this is the outermost JS call, set js_entry_sp value.
2353 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
2354 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
2355 __ j(not_equal, ¬_outermost_js, Label::kNear);
2356 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
2357 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
2358 __ jmp(&invoke, Label::kNear);
2359 __ bind(¬_outermost_js);
2360 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
2362 // Jump to a faked try block that does the invoke, with a faked catch
2363 // block that sets the pending exception.
2365 __ bind(&handler_entry);
2366 handler_offset_ = handler_entry.pos();
2367 // Caught exception: Store result (exception) in the pending exception
2368 // field in the JSEnv and return a failure sentinel.
2369 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
2371 __ mov(Operand::StaticVariable(pending_exception), eax);
2372 __ mov(eax, Immediate(isolate()->factory()->exception()));
2375 // Invoke: Link this frame into the handler chain.
2377 __ PushStackHandler();
2379 // Clear any pending exceptions.
2380 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
2381 __ mov(Operand::StaticVariable(pending_exception), edx);
2383 // Fake a receiver (NULL).
2384 __ push(Immediate(0)); // receiver
2386 // Invoke the function by calling through JS entry trampoline builtin and
2387 // pop the faked function when we return. Notice that we cannot store a
2388 // reference to the trampoline code directly in this stub, because the
2389 // builtin stubs may not have been generated yet.
2390 if (type() == StackFrame::ENTRY_CONSTRUCT) {
2391 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
2393 __ mov(edx, Immediate(construct_entry));
2395 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
2396 __ mov(edx, Immediate(entry));
2398 __ mov(edx, Operand(edx, 0)); // deref address
2399 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
2402 // Unlink this frame from the handler chain.
2403 __ PopStackHandler();
2406 // Check if the current stack frame is marked as the outermost JS frame.
2408 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
2409 __ j(not_equal, ¬_outermost_js_2);
2410 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
2411 __ bind(¬_outermost_js_2);
2413 // Restore the top frame descriptor from the stack.
2414 __ pop(Operand::StaticVariable(ExternalReference(
2415 Isolate::kCEntryFPAddress, isolate())));
2417 // Restore callee-saved registers (C calling conventions).
2421 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
2423 // Restore frame pointer and return.
2429 // Generate stub code for instanceof.
2430 // This code can patch a call site inlined cache of the instance of check,
2431 // which looks like this.
2433 // 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
2434 // 75 0a jne <some near label>
2435 // b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
2437 // If call site patching is requested the stack will have the delta from the
2438 // return address to the cmp instruction just below the return address. This
2439 // also means that call site patching can only take place with arguments in
2440 // registers. TOS looks like this when call site patching is requested
2442 // esp[0] : return address
2443 // esp[4] : delta from return address to cmp instruction
2445 void InstanceofStub::Generate(MacroAssembler* masm) {
2446 // Call site inlining and patching implies arguments in registers.
2447 DCHECK(HasArgsInRegisters() || !HasCallSiteInlineCheck());
2449 // Fixed register usage throughout the stub.
2450 Register object = eax; // Object (lhs).
2451 Register map = ebx; // Map of the object.
2452 Register function = edx; // Function (rhs).
2453 Register prototype = edi; // Prototype of the function.
2454 Register scratch = ecx;
2456 // Constants describing the call site code to patch.
2457 static const int kDeltaToCmpImmediate = 2;
2458 static const int kDeltaToMov = 8;
2459 static const int kDeltaToMovImmediate = 9;
2460 static const int8_t kCmpEdiOperandByte1 = bit_cast<int8_t, uint8_t>(0x3b);
2461 static const int8_t kCmpEdiOperandByte2 = bit_cast<int8_t, uint8_t>(0x3d);
2462 static const int8_t kMovEaxImmediateByte = bit_cast<int8_t, uint8_t>(0xb8);
2464 DCHECK_EQ(object.code(), InstanceofStub::left().code());
2465 DCHECK_EQ(function.code(), InstanceofStub::right().code());
2467 // Get the object and function - they are always both needed.
2468 Label slow, not_js_object;
2469 if (!HasArgsInRegisters()) {
2470 __ mov(object, Operand(esp, 2 * kPointerSize));
2471 __ mov(function, Operand(esp, 1 * kPointerSize));
2474 // Check that the left hand is a JS object.
2475 __ JumpIfSmi(object, ¬_js_object);
2476 __ IsObjectJSObjectType(object, map, scratch, ¬_js_object);
2478 // If there is a call site cache don't look in the global cache, but do the
2479 // real lookup and update the call site cache.
2480 if (!HasCallSiteInlineCheck() && !ReturnTrueFalseObject()) {
2481 // Look up the function and the map in the instanceof cache.
2483 __ CompareRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2484 __ j(not_equal, &miss, Label::kNear);
2485 __ CompareRoot(map, scratch, Heap::kInstanceofCacheMapRootIndex);
2486 __ j(not_equal, &miss, Label::kNear);
2487 __ LoadRoot(eax, Heap::kInstanceofCacheAnswerRootIndex);
2488 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2492 // Get the prototype of the function.
2493 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
2495 // Check that the function prototype is a JS object.
2496 __ JumpIfSmi(prototype, &slow);
2497 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
2499 // Update the global instanceof or call site inlined cache with the current
2500 // map and function. The cached answer will be set when it is known below.
2501 if (!HasCallSiteInlineCheck()) {
2502 __ StoreRoot(map, scratch, Heap::kInstanceofCacheMapRootIndex);
2503 __ StoreRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2505 // The constants for the code patching are based on no push instructions
2506 // at the call site.
2507 DCHECK(HasArgsInRegisters());
2508 // Get return address and delta to inlined map check.
2509 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2510 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2511 if (FLAG_debug_code) {
2512 __ cmpb(Operand(scratch, 0), kCmpEdiOperandByte1);
2513 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCmp1);
2514 __ cmpb(Operand(scratch, 1), kCmpEdiOperandByte2);
2515 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCmp2);
2517 __ mov(scratch, Operand(scratch, kDeltaToCmpImmediate));
2518 __ mov(Operand(scratch, 0), map);
2520 // Scratch points at the cell payload. Calculate the start of the object.
2521 __ sub(scratch, Immediate(Cell::kValueOffset - 1));
2522 __ RecordWriteField(scratch, Cell::kValueOffset, map, function,
2523 kDontSaveFPRegs, OMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2527 // Loop through the prototype chain of the object looking for the function
2529 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
2530 Label loop, is_instance, is_not_instance;
2532 __ cmp(scratch, prototype);
2533 __ j(equal, &is_instance, Label::kNear);
2534 Factory* factory = isolate()->factory();
2535 __ cmp(scratch, Immediate(factory->null_value()));
2536 __ j(equal, &is_not_instance, Label::kNear);
2537 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
2538 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
2541 __ bind(&is_instance);
2542 if (!HasCallSiteInlineCheck()) {
2543 __ mov(eax, Immediate(0));
2544 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
2545 if (ReturnTrueFalseObject()) {
2546 __ mov(eax, factory->true_value());
2549 // Get return address and delta to inlined map check.
2550 __ mov(eax, factory->true_value());
2551 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2552 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2553 if (FLAG_debug_code) {
2554 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
2555 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2557 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
2558 if (!ReturnTrueFalseObject()) {
2559 __ Move(eax, Immediate(0));
2562 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2564 __ bind(&is_not_instance);
2565 if (!HasCallSiteInlineCheck()) {
2566 __ mov(eax, Immediate(Smi::FromInt(1)));
2567 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
2568 if (ReturnTrueFalseObject()) {
2569 __ mov(eax, factory->false_value());
2572 // Get return address and delta to inlined map check.
2573 __ mov(eax, factory->false_value());
2574 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2575 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2576 if (FLAG_debug_code) {
2577 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
2578 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2580 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
2581 if (!ReturnTrueFalseObject()) {
2582 __ Move(eax, Immediate(Smi::FromInt(1)));
2585 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2587 Label object_not_null, object_not_null_or_smi;
2588 __ bind(¬_js_object);
2589 // Before null, smi and string value checks, check that the rhs is a function
2590 // as for a non-function rhs an exception needs to be thrown.
2591 __ JumpIfSmi(function, &slow, Label::kNear);
2592 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
2593 __ j(not_equal, &slow, Label::kNear);
2595 // Null is not instance of anything.
2596 __ cmp(object, factory->null_value());
2597 __ j(not_equal, &object_not_null, Label::kNear);
2598 if (ReturnTrueFalseObject()) {
2599 __ mov(eax, factory->false_value());
2601 __ Move(eax, Immediate(Smi::FromInt(1)));
2603 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2605 __ bind(&object_not_null);
2606 // Smi values is not instance of anything.
2607 __ JumpIfNotSmi(object, &object_not_null_or_smi, Label::kNear);
2608 if (ReturnTrueFalseObject()) {
2609 __ mov(eax, factory->false_value());
2611 __ Move(eax, Immediate(Smi::FromInt(1)));
2613 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2615 __ bind(&object_not_null_or_smi);
2616 // String values is not instance of anything.
2617 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
2618 __ j(NegateCondition(is_string), &slow, Label::kNear);
2619 if (ReturnTrueFalseObject()) {
2620 __ mov(eax, factory->false_value());
2622 __ Move(eax, Immediate(Smi::FromInt(1)));
2624 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2626 // Slow-case: Go through the JavaScript implementation.
2628 if (!ReturnTrueFalseObject()) {
2629 // Tail call the builtin which returns 0 or 1.
2630 if (HasArgsInRegisters()) {
2631 // Push arguments below return address.
2637 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
2639 // Call the builtin and convert 0/1 to true/false.
2641 FrameScope scope(masm, StackFrame::INTERNAL);
2644 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
2646 Label true_value, done;
2648 __ j(zero, &true_value, Label::kNear);
2649 __ mov(eax, factory->false_value());
2650 __ jmp(&done, Label::kNear);
2651 __ bind(&true_value);
2652 __ mov(eax, factory->true_value());
2654 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2659 // -------------------------------------------------------------------------
2660 // StringCharCodeAtGenerator
2662 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
2663 // If the receiver is a smi trigger the non-string case.
2664 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2665 __ JumpIfSmi(object_, receiver_not_string_);
2667 // Fetch the instance type of the receiver into result register.
2668 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
2669 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2670 // If the receiver is not a string trigger the non-string case.
2671 __ test(result_, Immediate(kIsNotStringMask));
2672 __ j(not_zero, receiver_not_string_);
2675 // If the index is non-smi trigger the non-smi case.
2676 __ JumpIfNotSmi(index_, &index_not_smi_);
2677 __ bind(&got_smi_index_);
2679 // Check for index out of range.
2680 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
2681 __ j(above_equal, index_out_of_range_);
2683 __ SmiUntag(index_);
2685 Factory* factory = masm->isolate()->factory();
2686 StringCharLoadGenerator::Generate(
2687 masm, factory, object_, index_, result_, &call_runtime_);
2694 void StringCharCodeAtGenerator::GenerateSlow(
2695 MacroAssembler* masm, EmbedMode embed_mode,
2696 const RuntimeCallHelper& call_helper) {
2697 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
2699 // Index is not a smi.
2700 __ bind(&index_not_smi_);
2701 // If index is a heap number, try converting it to an integer.
2703 masm->isolate()->factory()->heap_number_map(),
2706 call_helper.BeforeCall(masm);
2707 if (embed_mode == PART_OF_IC_HANDLER) {
2708 __ push(LoadWithVectorDescriptor::VectorRegister());
2709 __ push(LoadDescriptor::SlotRegister());
2712 __ push(index_); // Consumed by runtime conversion function.
2713 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
2714 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
2716 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
2717 // NumberToSmi discards numbers that are not exact integers.
2718 __ CallRuntime(Runtime::kNumberToSmi, 1);
2720 if (!index_.is(eax)) {
2721 // Save the conversion result before the pop instructions below
2722 // have a chance to overwrite it.
2723 __ mov(index_, eax);
2726 if (embed_mode == PART_OF_IC_HANDLER) {
2727 __ pop(LoadDescriptor::SlotRegister());
2728 __ pop(LoadWithVectorDescriptor::VectorRegister());
2730 // Reload the instance type.
2731 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
2732 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2733 call_helper.AfterCall(masm);
2734 // If index is still not a smi, it must be out of range.
2735 STATIC_ASSERT(kSmiTag == 0);
2736 __ JumpIfNotSmi(index_, index_out_of_range_);
2737 // Otherwise, return to the fast path.
2738 __ jmp(&got_smi_index_);
2740 // Call runtime. We get here when the receiver is a string and the
2741 // index is a number, but the code of getting the actual character
2742 // is too complex (e.g., when the string needs to be flattened).
2743 __ bind(&call_runtime_);
2744 call_helper.BeforeCall(masm);
2748 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
2749 if (!result_.is(eax)) {
2750 __ mov(result_, eax);
2752 call_helper.AfterCall(masm);
2755 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
2759 // -------------------------------------------------------------------------
2760 // StringCharFromCodeGenerator
2762 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
2763 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2764 STATIC_ASSERT(kSmiTag == 0);
2765 STATIC_ASSERT(kSmiShiftSize == 0);
2766 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
2767 __ test(code_, Immediate(kSmiTagMask |
2768 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
2769 __ j(not_zero, &slow_case_);
2771 Factory* factory = masm->isolate()->factory();
2772 __ Move(result_, Immediate(factory->single_character_string_cache()));
2773 STATIC_ASSERT(kSmiTag == 0);
2774 STATIC_ASSERT(kSmiTagSize == 1);
2775 STATIC_ASSERT(kSmiShiftSize == 0);
2776 // At this point code register contains smi tagged one byte char code.
2777 __ mov(result_, FieldOperand(result_,
2778 code_, times_half_pointer_size,
2779 FixedArray::kHeaderSize));
2780 __ cmp(result_, factory->undefined_value());
2781 __ j(equal, &slow_case_);
2786 void StringCharFromCodeGenerator::GenerateSlow(
2787 MacroAssembler* masm,
2788 const RuntimeCallHelper& call_helper) {
2789 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
2791 __ bind(&slow_case_);
2792 call_helper.BeforeCall(masm);
2794 __ CallRuntime(Runtime::kCharFromCode, 1);
2795 if (!result_.is(eax)) {
2796 __ mov(result_, eax);
2798 call_helper.AfterCall(masm);
2801 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
2805 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2810 String::Encoding encoding) {
2811 DCHECK(!scratch.is(dest));
2812 DCHECK(!scratch.is(src));
2813 DCHECK(!scratch.is(count));
2815 // Nothing to do for zero characters.
2817 __ test(count, count);
2820 // Make count the number of bytes to copy.
2821 if (encoding == String::TWO_BYTE_ENCODING) {
2827 __ mov_b(scratch, Operand(src, 0));
2828 __ mov_b(Operand(dest, 0), scratch);
2832 __ j(not_zero, &loop);
2838 void SubStringStub::Generate(MacroAssembler* masm) {
2841 // Stack frame on entry.
2842 // esp[0]: return address
2847 // Make sure first argument is a string.
2848 __ mov(eax, Operand(esp, 3 * kPointerSize));
2849 STATIC_ASSERT(kSmiTag == 0);
2850 __ JumpIfSmi(eax, &runtime);
2851 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
2852 __ j(NegateCondition(is_string), &runtime);
2855 // ebx: instance type
2857 // Calculate length of sub string using the smi values.
2858 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
2859 __ JumpIfNotSmi(ecx, &runtime);
2860 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
2861 __ JumpIfNotSmi(edx, &runtime);
2863 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
2864 Label not_original_string;
2865 // Shorter than original string's length: an actual substring.
2866 __ j(below, ¬_original_string, Label::kNear);
2867 // Longer than original string's length or negative: unsafe arguments.
2868 __ j(above, &runtime);
2869 // Return original string.
2870 Counters* counters = isolate()->counters();
2871 __ IncrementCounter(counters->sub_string_native(), 1);
2872 __ ret(3 * kPointerSize);
2873 __ bind(¬_original_string);
2876 __ cmp(ecx, Immediate(Smi::FromInt(1)));
2877 __ j(equal, &single_char);
2880 // ebx: instance type
2881 // ecx: sub string length (smi)
2882 // edx: from index (smi)
2883 // Deal with different string types: update the index if necessary
2884 // and put the underlying string into edi.
2885 Label underlying_unpacked, sliced_string, seq_or_external_string;
2886 // If the string is not indirect, it can only be sequential or external.
2887 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2888 STATIC_ASSERT(kIsIndirectStringMask != 0);
2889 __ test(ebx, Immediate(kIsIndirectStringMask));
2890 __ j(zero, &seq_or_external_string, Label::kNear);
2892 Factory* factory = isolate()->factory();
2893 __ test(ebx, Immediate(kSlicedNotConsMask));
2894 __ j(not_zero, &sliced_string, Label::kNear);
2895 // Cons string. Check whether it is flat, then fetch first part.
2896 // Flat cons strings have an empty second part.
2897 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
2898 factory->empty_string());
2899 __ j(not_equal, &runtime);
2900 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
2901 // Update instance type.
2902 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2903 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2904 __ jmp(&underlying_unpacked, Label::kNear);
2906 __ bind(&sliced_string);
2907 // Sliced string. Fetch parent and adjust start index by offset.
2908 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
2909 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
2910 // Update instance type.
2911 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2912 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2913 __ jmp(&underlying_unpacked, Label::kNear);
2915 __ bind(&seq_or_external_string);
2916 // Sequential or external string. Just move string to the expected register.
2919 __ bind(&underlying_unpacked);
2921 if (FLAG_string_slices) {
2923 // edi: underlying subject string
2924 // ebx: instance type of underlying subject string
2925 // edx: adjusted start index (smi)
2926 // ecx: length (smi)
2927 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
2928 // Short slice. Copy instead of slicing.
2929 __ j(less, ©_routine);
2930 // Allocate new sliced string. At this point we do not reload the instance
2931 // type including the string encoding because we simply rely on the info
2932 // provided by the original string. It does not matter if the original
2933 // string's encoding is wrong because we always have to recheck encoding of
2934 // the newly created string's parent anyways due to externalized strings.
2935 Label two_byte_slice, set_slice_header;
2936 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
2937 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2938 __ test(ebx, Immediate(kStringEncodingMask));
2939 __ j(zero, &two_byte_slice, Label::kNear);
2940 __ AllocateOneByteSlicedString(eax, ebx, no_reg, &runtime);
2941 __ jmp(&set_slice_header, Label::kNear);
2942 __ bind(&two_byte_slice);
2943 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
2944 __ bind(&set_slice_header);
2945 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
2946 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
2947 Immediate(String::kEmptyHashField));
2948 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
2949 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
2950 __ IncrementCounter(counters->sub_string_native(), 1);
2951 __ ret(3 * kPointerSize);
2953 __ bind(©_routine);
2956 // edi: underlying subject string
2957 // ebx: instance type of underlying subject string
2958 // edx: adjusted start index (smi)
2959 // ecx: length (smi)
2960 // The subject string can only be external or sequential string of either
2961 // encoding at this point.
2962 Label two_byte_sequential, runtime_drop_two, sequential_string;
2963 STATIC_ASSERT(kExternalStringTag != 0);
2964 STATIC_ASSERT(kSeqStringTag == 0);
2965 __ test_b(ebx, kExternalStringTag);
2966 __ j(zero, &sequential_string);
2968 // Handle external string.
2969 // Rule out short external strings.
2970 STATIC_ASSERT(kShortExternalStringTag != 0);
2971 __ test_b(ebx, kShortExternalStringMask);
2972 __ j(not_zero, &runtime);
2973 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
2974 // Move the pointer so that offset-wise, it looks like a sequential string.
2975 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2976 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2978 __ bind(&sequential_string);
2979 // Stash away (adjusted) index and (underlying) string.
2983 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
2984 __ test_b(ebx, kStringEncodingMask);
2985 __ j(zero, &two_byte_sequential);
2987 // Sequential one byte string. Allocate the result.
2988 __ AllocateOneByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
2990 // eax: result string
2991 // ecx: result string length
2992 // Locate first character of result.
2994 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
2995 // Load string argument and locate character of sub string start.
2999 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqOneByteString::kHeaderSize));
3001 // eax: result string
3002 // ecx: result length
3003 // edi: first character of result
3004 // edx: character of sub string start
3005 StringHelper::GenerateCopyCharacters(
3006 masm, edi, edx, ecx, ebx, String::ONE_BYTE_ENCODING);
3007 __ IncrementCounter(counters->sub_string_native(), 1);
3008 __ ret(3 * kPointerSize);
3010 __ bind(&two_byte_sequential);
3011 // Sequential two-byte string. Allocate the result.
3012 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
3014 // eax: result string
3015 // ecx: result string length
3016 // Locate first character of result.
3019 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3020 // Load string argument and locate character of sub string start.
3023 // As from is a smi it is 2 times the value which matches the size of a two
3025 STATIC_ASSERT(kSmiTag == 0);
3026 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3027 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqTwoByteString::kHeaderSize));
3029 // eax: result string
3030 // ecx: result length
3031 // edi: first character of result
3032 // edx: character of sub string start
3033 StringHelper::GenerateCopyCharacters(
3034 masm, edi, edx, ecx, ebx, String::TWO_BYTE_ENCODING);
3035 __ IncrementCounter(counters->sub_string_native(), 1);
3036 __ ret(3 * kPointerSize);
3038 // Drop pushed values on the stack before tail call.
3039 __ bind(&runtime_drop_two);
3042 // Just jump to runtime to create the sub string.
3044 __ TailCallRuntime(Runtime::kSubStringRT, 3, 1);
3046 __ bind(&single_char);
3048 // ebx: instance type
3049 // ecx: sub string length (smi)
3050 // edx: from index (smi)
3051 StringCharAtGenerator generator(eax, edx, ecx, eax, &runtime, &runtime,
3052 &runtime, STRING_INDEX_IS_NUMBER,
3053 RECEIVER_IS_STRING);
3054 generator.GenerateFast(masm);
3055 __ ret(3 * kPointerSize);
3056 generator.SkipSlow(masm, &runtime);
3060 void ToNumberStub::Generate(MacroAssembler* masm) {
3061 // The ToNumber stub takes one argument in eax.
3063 __ JumpIfNotSmi(eax, ¬_smi, Label::kNear);
3067 Label not_heap_number;
3068 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
3069 __ j(not_equal, ¬_heap_number, Label::kNear);
3071 __ bind(¬_heap_number);
3073 Label not_string, slow_string;
3074 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
3077 __ j(above_equal, ¬_string, Label::kNear);
3078 // Check if string has a cached array index.
3079 __ test(FieldOperand(eax, String::kHashFieldOffset),
3080 Immediate(String::kContainsCachedArrayIndexMask));
3081 __ j(not_zero, &slow_string, Label::kNear);
3082 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3083 __ IndexFromHash(eax, eax);
3085 __ bind(&slow_string);
3086 __ pop(ecx); // Pop return address.
3087 __ push(eax); // Push argument.
3088 __ push(ecx); // Push return address.
3089 __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
3090 __ bind(¬_string);
3093 __ CmpInstanceType(edi, ODDBALL_TYPE);
3094 __ j(not_equal, ¬_oddball, Label::kNear);
3095 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
3097 __ bind(¬_oddball);
3099 __ pop(ecx); // Pop return address.
3100 __ push(eax); // Push argument.
3101 __ push(ecx); // Push return address.
3102 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
3106 void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
3110 Register scratch2) {
3111 Register length = scratch1;
3114 Label strings_not_equal, check_zero_length;
3115 __ mov(length, FieldOperand(left, String::kLengthOffset));
3116 __ cmp(length, FieldOperand(right, String::kLengthOffset));
3117 __ j(equal, &check_zero_length, Label::kNear);
3118 __ bind(&strings_not_equal);
3119 __ Move(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
3122 // Check if the length is zero.
3123 Label compare_chars;
3124 __ bind(&check_zero_length);
3125 STATIC_ASSERT(kSmiTag == 0);
3126 __ test(length, length);
3127 __ j(not_zero, &compare_chars, Label::kNear);
3128 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3131 // Compare characters.
3132 __ bind(&compare_chars);
3133 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
3134 &strings_not_equal, Label::kNear);
3136 // Characters are equal.
3137 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3142 void StringHelper::GenerateCompareFlatOneByteStrings(
3143 MacroAssembler* masm, Register left, Register right, Register scratch1,
3144 Register scratch2, Register scratch3) {
3145 Counters* counters = masm->isolate()->counters();
3146 __ IncrementCounter(counters->string_compare_native(), 1);
3148 // Find minimum length.
3150 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
3151 __ mov(scratch3, scratch1);
3152 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
3154 Register length_delta = scratch3;
3156 __ j(less_equal, &left_shorter, Label::kNear);
3157 // Right string is shorter. Change scratch1 to be length of right string.
3158 __ sub(scratch1, length_delta);
3159 __ bind(&left_shorter);
3161 Register min_length = scratch1;
3163 // If either length is zero, just compare lengths.
3164 Label compare_lengths;
3165 __ test(min_length, min_length);
3166 __ j(zero, &compare_lengths, Label::kNear);
3168 // Compare characters.
3169 Label result_not_equal;
3170 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
3171 &result_not_equal, Label::kNear);
3173 // Compare lengths - strings up to min-length are equal.
3174 __ bind(&compare_lengths);
3175 __ test(length_delta, length_delta);
3176 Label length_not_equal;
3177 __ j(not_zero, &length_not_equal, Label::kNear);
3180 STATIC_ASSERT(EQUAL == 0);
3181 STATIC_ASSERT(kSmiTag == 0);
3182 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3185 Label result_greater;
3187 __ bind(&length_not_equal);
3188 __ j(greater, &result_greater, Label::kNear);
3189 __ jmp(&result_less, Label::kNear);
3190 __ bind(&result_not_equal);
3191 __ j(above, &result_greater, Label::kNear);
3192 __ bind(&result_less);
3195 __ Move(eax, Immediate(Smi::FromInt(LESS)));
3198 // Result is GREATER.
3199 __ bind(&result_greater);
3200 __ Move(eax, Immediate(Smi::FromInt(GREATER)));
3205 void StringHelper::GenerateOneByteCharsCompareLoop(
3206 MacroAssembler* masm, Register left, Register right, Register length,
3207 Register scratch, Label* chars_not_equal,
3208 Label::Distance chars_not_equal_near) {
3209 // Change index to run from -length to -1 by adding length to string
3210 // start. This means that loop ends when index reaches zero, which
3211 // doesn't need an additional compare.
3212 __ SmiUntag(length);
3214 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
3216 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
3218 Register index = length; // index = -length;
3223 __ mov_b(scratch, Operand(left, index, times_1, 0));
3224 __ cmpb(scratch, Operand(right, index, times_1, 0));
3225 __ j(not_equal, chars_not_equal, chars_not_equal_near);
3227 __ j(not_zero, &loop);
3231 void StringCompareStub::Generate(MacroAssembler* masm) {
3234 // Stack frame on entry.
3235 // esp[0]: return address
3236 // esp[4]: right string
3237 // esp[8]: left string
3239 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
3240 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
3244 __ j(not_equal, ¬_same, Label::kNear);
3245 STATIC_ASSERT(EQUAL == 0);
3246 STATIC_ASSERT(kSmiTag == 0);
3247 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3248 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1);
3249 __ ret(2 * kPointerSize);
3253 // Check that both objects are sequential one-byte strings.
3254 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx, &runtime);
3256 // Compare flat one-byte strings.
3257 // Drop arguments from the stack.
3259 __ add(esp, Immediate(2 * kPointerSize));
3261 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
3264 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
3265 // tagged as a small integer.
3267 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3271 void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3272 // ----------- S t a t e -------------
3275 // -- esp[0] : return address
3276 // -----------------------------------
3278 // Load ecx with the allocation site. We stick an undefined dummy value here
3279 // and replace it with the real allocation site later when we instantiate this
3280 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3281 __ mov(ecx, handle(isolate()->heap()->undefined_value()));
3283 // Make sure that we actually patched the allocation site.
3284 if (FLAG_debug_code) {
3285 __ test(ecx, Immediate(kSmiTagMask));
3286 __ Assert(not_equal, kExpectedAllocationSite);
3287 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
3288 isolate()->factory()->allocation_site_map());
3289 __ Assert(equal, kExpectedAllocationSite);
3292 // Tail call into the stub that handles binary operations with allocation
3294 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3295 __ TailCallStub(&stub);
3299 void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3300 DCHECK(state() == CompareICState::SMI);
3304 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
3306 if (GetCondition() == equal) {
3307 // For equality we do not care about the sign of the result.
3312 __ j(no_overflow, &done, Label::kNear);
3313 // Correct sign of result in case of overflow.
3325 void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3326 DCHECK(state() == CompareICState::NUMBER);
3328 Label generic_stub, check_left;
3329 Label unordered, maybe_undefined1, maybe_undefined2;
3332 if (left() == CompareICState::SMI) {
3333 __ JumpIfNotSmi(edx, &miss);
3335 if (right() == CompareICState::SMI) {
3336 __ JumpIfNotSmi(eax, &miss);
3339 // Inlining the double comparison and falling back to the general compare
3340 // stub if NaN is involved or SSE2 or CMOV is unsupported.
3341 __ JumpIfSmi(eax, &check_left, Label::kNear);
3342 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
3343 isolate()->factory()->heap_number_map());
3344 __ j(not_equal, &maybe_undefined1, Label::kNear);
3346 __ bind(&check_left);
3347 __ JumpIfSmi(edx, &generic_stub, Label::kNear);
3348 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
3349 isolate()->factory()->heap_number_map());
3350 __ j(not_equal, &maybe_undefined2, Label::kNear);
3352 __ bind(&unordered);
3353 __ bind(&generic_stub);
3354 CompareICStub stub(isolate(), op(), strength(), CompareICState::GENERIC,
3355 CompareICState::GENERIC, CompareICState::GENERIC);
3356 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3358 __ bind(&maybe_undefined1);
3359 if (Token::IsOrderedRelationalCompareOp(op())) {
3360 __ cmp(eax, Immediate(isolate()->factory()->undefined_value()));
3361 __ j(not_equal, &miss);
3362 __ JumpIfSmi(edx, &unordered);
3363 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
3364 __ j(not_equal, &maybe_undefined2, Label::kNear);
3368 __ bind(&maybe_undefined2);
3369 if (Token::IsOrderedRelationalCompareOp(op())) {
3370 __ cmp(edx, Immediate(isolate()->factory()->undefined_value()));
3371 __ j(equal, &unordered);
3379 void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3380 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3381 DCHECK(GetCondition() == equal);
3383 // Registers containing left and right operands respectively.
3384 Register left = edx;
3385 Register right = eax;
3386 Register tmp1 = ecx;
3387 Register tmp2 = ebx;
3389 // Check that both operands are heap objects.
3392 STATIC_ASSERT(kSmiTag == 0);
3393 __ and_(tmp1, right);
3394 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3396 // Check that both operands are internalized strings.
3397 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3398 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3399 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3400 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3401 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3403 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
3404 __ j(not_zero, &miss, Label::kNear);
3406 // Internalized strings are compared by identity.
3408 __ cmp(left, right);
3409 // Make sure eax is non-zero. At this point input operands are
3410 // guaranteed to be non-zero.
3411 DCHECK(right.is(eax));
3412 __ j(not_equal, &done, Label::kNear);
3413 STATIC_ASSERT(EQUAL == 0);
3414 STATIC_ASSERT(kSmiTag == 0);
3415 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3424 void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3425 DCHECK(state() == CompareICState::UNIQUE_NAME);
3426 DCHECK(GetCondition() == equal);
3428 // Registers containing left and right operands respectively.
3429 Register left = edx;
3430 Register right = eax;
3431 Register tmp1 = ecx;
3432 Register tmp2 = ebx;
3434 // Check that both operands are heap objects.
3437 STATIC_ASSERT(kSmiTag == 0);
3438 __ and_(tmp1, right);
3439 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3441 // Check that both operands are unique names. This leaves the instance
3442 // types loaded in tmp1 and tmp2.
3443 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3444 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3445 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3446 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3448 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
3449 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
3451 // Unique names are compared by identity.
3453 __ cmp(left, right);
3454 // Make sure eax is non-zero. At this point input operands are
3455 // guaranteed to be non-zero.
3456 DCHECK(right.is(eax));
3457 __ j(not_equal, &done, Label::kNear);
3458 STATIC_ASSERT(EQUAL == 0);
3459 STATIC_ASSERT(kSmiTag == 0);
3460 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3469 void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3470 DCHECK(state() == CompareICState::STRING);
3473 bool equality = Token::IsEqualityOp(op());
3475 // Registers containing left and right operands respectively.
3476 Register left = edx;
3477 Register right = eax;
3478 Register tmp1 = ecx;
3479 Register tmp2 = ebx;
3480 Register tmp3 = edi;
3482 // Check that both operands are heap objects.
3484 STATIC_ASSERT(kSmiTag == 0);
3485 __ and_(tmp1, right);
3486 __ JumpIfSmi(tmp1, &miss);
3488 // Check that both operands are strings. This leaves the instance
3489 // types loaded in tmp1 and tmp2.
3490 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3491 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3492 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3493 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3495 STATIC_ASSERT(kNotStringTag != 0);
3497 __ test(tmp3, Immediate(kIsNotStringMask));
3498 __ j(not_zero, &miss);
3500 // Fast check for identical strings.
3502 __ cmp(left, right);
3503 __ j(not_equal, ¬_same, Label::kNear);
3504 STATIC_ASSERT(EQUAL == 0);
3505 STATIC_ASSERT(kSmiTag == 0);
3506 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3509 // Handle not identical strings.
3512 // Check that both strings are internalized. If they are, we're done
3513 // because we already know they are not identical. But in the case of
3514 // non-equality compare, we still need to determine the order. We
3515 // also know they are both strings.
3518 STATIC_ASSERT(kInternalizedTag == 0);
3520 __ test(tmp1, Immediate(kIsNotInternalizedMask));
3521 __ j(not_zero, &do_compare, Label::kNear);
3522 // Make sure eax is non-zero. At this point input operands are
3523 // guaranteed to be non-zero.
3524 DCHECK(right.is(eax));
3526 __ bind(&do_compare);
3529 // Check that both strings are sequential one-byte.
3531 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
3533 // Compare flat one byte strings. Returns when done.
3535 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
3538 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3542 // Handle more complex cases in runtime.
3544 __ pop(tmp1); // Return address.
3549 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3551 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3559 void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3560 DCHECK(state() == CompareICState::OBJECT);
3564 __ JumpIfSmi(ecx, &miss, Label::kNear);
3566 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
3567 __ j(not_equal, &miss, Label::kNear);
3568 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
3569 __ j(not_equal, &miss, Label::kNear);
3571 DCHECK(GetCondition() == equal);
3580 void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
3582 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
3585 __ JumpIfSmi(ecx, &miss, Label::kNear);
3587 __ GetWeakValue(edi, cell);
3588 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3589 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
3591 __ j(not_equal, &miss, Label::kNear);
3593 __ j(not_equal, &miss, Label::kNear);
3603 void CompareICStub::GenerateMiss(MacroAssembler* masm) {
3605 // Call the runtime system in a fresh internal frame.
3606 FrameScope scope(masm, StackFrame::INTERNAL);
3607 __ push(edx); // Preserve edx and eax.
3609 __ push(edx); // And also use them as the arguments.
3611 __ push(Immediate(Smi::FromInt(op())));
3612 __ CallRuntime(Runtime::kCompareIC_Miss, 3);
3613 // Compute the entry point of the rewritten stub.
3614 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
3619 // Do a tail call to the rewritten stub.
3624 // Helper function used to check that the dictionary doesn't contain
3625 // the property. This function may return false negatives, so miss_label
3626 // must always call a backup property check that is complete.
3627 // This function is safe to call if the receiver has fast properties.
3628 // Name must be a unique name and receiver must be a heap object.
3629 void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3632 Register properties,
3635 DCHECK(name->IsUniqueName());
3637 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3638 // not equal to the name and kProbes-th slot is not used (its name is the
3639 // undefined value), it guarantees the hash table doesn't contain the
3640 // property. It's true even if some slots represent deleted properties
3641 // (their names are the hole value).
3642 for (int i = 0; i < kInlinedProbes; i++) {
3643 // Compute the masked index: (hash + i + i * i) & mask.
3644 Register index = r0;
3645 // Capacity is smi 2^n.
3646 __ mov(index, FieldOperand(properties, kCapacityOffset));
3649 Immediate(Smi::FromInt(name->Hash() +
3650 NameDictionary::GetProbeOffset(i))));
3652 // Scale the index by multiplying by the entry size.
3653 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3654 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
3655 Register entity_name = r0;
3656 // Having undefined at this place means the name is not contained.
3657 STATIC_ASSERT(kSmiTagSize == 1);
3658 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
3659 kElementsStartOffset - kHeapObjectTag));
3660 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
3663 // Stop if found the property.
3664 __ cmp(entity_name, Handle<Name>(name));
3668 // Check for the hole and skip.
3669 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
3670 __ j(equal, &good, Label::kNear);
3672 // Check if the entry name is not a unique name.
3673 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3674 __ JumpIfNotUniqueNameInstanceType(
3675 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3679 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3681 __ push(Immediate(Handle<Object>(name)));
3682 __ push(Immediate(name->Hash()));
3685 __ j(not_zero, miss);
3690 // Probe the name dictionary in the |elements| register. Jump to the
3691 // |done| label if a property with the given name is found leaving the
3692 // index into the dictionary in |r0|. Jump to the |miss| label
3694 void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3701 DCHECK(!elements.is(r0));
3702 DCHECK(!elements.is(r1));
3703 DCHECK(!name.is(r0));
3704 DCHECK(!name.is(r1));
3706 __ AssertName(name);
3708 __ mov(r1, FieldOperand(elements, kCapacityOffset));
3709 __ shr(r1, kSmiTagSize); // convert smi to int
3712 // Generate an unrolled loop that performs a few probes before
3713 // giving up. Measurements done on Gmail indicate that 2 probes
3714 // cover ~93% of loads from dictionaries.
3715 for (int i = 0; i < kInlinedProbes; i++) {
3716 // Compute the masked index: (hash + i + i * i) & mask.
3717 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3718 __ shr(r0, Name::kHashShift);
3720 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
3724 // Scale the index by multiplying by the entry size.
3725 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3726 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
3728 // Check if the key is identical to the name.
3729 __ cmp(name, Operand(elements,
3732 kElementsStartOffset - kHeapObjectTag));
3736 NameDictionaryLookupStub stub(masm->isolate(), elements, r1, r0,
3739 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3740 __ shr(r0, Name::kHashShift);
3750 void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
3751 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3752 // we cannot call anything that could cause a GC from this stub.
3753 // Stack frame on entry:
3754 // esp[0 * kPointerSize]: return address.
3755 // esp[1 * kPointerSize]: key's hash.
3756 // esp[2 * kPointerSize]: key.
3758 // dictionary_: NameDictionary to probe.
3759 // result_: used as scratch.
3760 // index_: will hold an index of entry if lookup is successful.
3761 // might alias with result_.
3763 // result_ is zero if lookup failed, non zero otherwise.
3765 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3767 Register scratch = result();
3769 __ mov(scratch, FieldOperand(dictionary(), kCapacityOffset));
3771 __ SmiUntag(scratch);
3774 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3775 // not equal to the name and kProbes-th slot is not used (its name is the
3776 // undefined value), it guarantees the hash table doesn't contain the
3777 // property. It's true even if some slots represent deleted properties
3778 // (their names are the null value).
3779 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3780 // Compute the masked index: (hash + i + i * i) & mask.
3781 __ mov(scratch, Operand(esp, 2 * kPointerSize));
3783 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
3785 __ and_(scratch, Operand(esp, 0));
3787 // Scale the index by multiplying by the entry size.
3788 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3789 __ lea(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
3791 // Having undefined at this place means the name is not contained.
3792 STATIC_ASSERT(kSmiTagSize == 1);
3793 __ mov(scratch, Operand(dictionary(), index(), times_pointer_size,
3794 kElementsStartOffset - kHeapObjectTag));
3795 __ cmp(scratch, isolate()->factory()->undefined_value());
3796 __ j(equal, ¬_in_dictionary);
3798 // Stop if found the property.
3799 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
3800 __ j(equal, &in_dictionary);
3802 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3803 // If we hit a key that is not a unique name during negative
3804 // lookup we have to bailout as this key might be equal to the
3805 // key we are looking for.
3807 // Check if the entry name is not a unique name.
3808 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
3809 __ JumpIfNotUniqueNameInstanceType(
3810 FieldOperand(scratch, Map::kInstanceTypeOffset),
3811 &maybe_in_dictionary);
3815 __ bind(&maybe_in_dictionary);
3816 // If we are doing negative lookup then probing failure should be
3817 // treated as a lookup success. For positive lookup probing failure
3818 // should be treated as lookup failure.
3819 if (mode() == POSITIVE_LOOKUP) {
3820 __ mov(result(), Immediate(0));
3822 __ ret(2 * kPointerSize);
3825 __ bind(&in_dictionary);
3826 __ mov(result(), Immediate(1));
3828 __ ret(2 * kPointerSize);
3830 __ bind(¬_in_dictionary);
3831 __ mov(result(), Immediate(0));
3833 __ ret(2 * kPointerSize);
3837 void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3839 StoreBufferOverflowStub stub(isolate, kDontSaveFPRegs);
3841 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3846 // Takes the input in 3 registers: address_ value_ and object_. A pointer to
3847 // the value has just been written into the object, now this stub makes sure
3848 // we keep the GC informed. The word in the object where the value has been
3849 // written is in the address register.
3850 void RecordWriteStub::Generate(MacroAssembler* masm) {
3851 Label skip_to_incremental_noncompacting;
3852 Label skip_to_incremental_compacting;
3854 // The first two instructions are generated with labels so as to get the
3855 // offset fixed up correctly by the bind(Label*) call. We patch it back and
3856 // forth between a compare instructions (a nop in this position) and the
3857 // real branch when we start and stop incremental heap marking.
3858 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
3859 __ jmp(&skip_to_incremental_compacting, Label::kFar);
3861 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3862 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3863 MacroAssembler::kReturnAtEnd);
3868 __ bind(&skip_to_incremental_noncompacting);
3869 GenerateIncremental(masm, INCREMENTAL);
3871 __ bind(&skip_to_incremental_compacting);
3872 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3874 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3875 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
3876 masm->set_byte_at(0, kTwoByteNopInstruction);
3877 masm->set_byte_at(2, kFiveByteNopInstruction);
3881 void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3884 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3885 Label dont_need_remembered_set;
3887 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3888 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
3890 &dont_need_remembered_set);
3892 __ CheckPageFlag(regs_.object(),
3894 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3896 &dont_need_remembered_set);
3898 // First notify the incremental marker if necessary, then update the
3900 CheckNeedsToInformIncrementalMarker(
3902 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
3904 InformIncrementalMarker(masm);
3905 regs_.Restore(masm);
3906 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3907 MacroAssembler::kReturnAtEnd);
3909 __ bind(&dont_need_remembered_set);
3912 CheckNeedsToInformIncrementalMarker(
3914 kReturnOnNoNeedToInformIncrementalMarker,
3916 InformIncrementalMarker(masm);
3917 regs_.Restore(masm);
3922 void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3923 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
3924 int argument_count = 3;
3925 __ PrepareCallCFunction(argument_count, regs_.scratch0());
3926 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
3927 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
3928 __ mov(Operand(esp, 2 * kPointerSize),
3929 Immediate(ExternalReference::isolate_address(isolate())));
3931 AllowExternalCallThatCantCauseGC scope(masm);
3933 ExternalReference::incremental_marking_record_write_function(isolate()),
3936 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
3940 void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3941 MacroAssembler* masm,
3942 OnNoNeedToInformIncrementalMarker on_no_need,
3944 Label object_is_black, need_incremental, need_incremental_pop_object;
3946 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
3947 __ and_(regs_.scratch0(), regs_.object());
3948 __ mov(regs_.scratch1(),
3949 Operand(regs_.scratch0(),
3950 MemoryChunk::kWriteBarrierCounterOffset));
3951 __ sub(regs_.scratch1(), Immediate(1));
3952 __ mov(Operand(regs_.scratch0(),
3953 MemoryChunk::kWriteBarrierCounterOffset),
3955 __ j(negative, &need_incremental);
3957 // Let's look at the color of the object: If it is not black we don't have
3958 // to inform the incremental marker.
3959 __ JumpIfBlack(regs_.object(),
3965 regs_.Restore(masm);
3966 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
3967 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3968 MacroAssembler::kReturnAtEnd);
3973 __ bind(&object_is_black);
3975 // Get the value from the slot.
3976 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3978 if (mode == INCREMENTAL_COMPACTION) {
3979 Label ensure_not_white;
3981 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3982 regs_.scratch1(), // Scratch.
3983 MemoryChunk::kEvacuationCandidateMask,
3988 __ CheckPageFlag(regs_.object(),
3989 regs_.scratch1(), // Scratch.
3990 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3995 __ jmp(&need_incremental);
3997 __ bind(&ensure_not_white);
4000 // We need an extra register for this, so we push the object register
4002 __ push(regs_.object());
4003 __ EnsureNotWhite(regs_.scratch0(), // The value.
4004 regs_.scratch1(), // Scratch.
4005 regs_.object(), // Scratch.
4006 &need_incremental_pop_object,
4008 __ pop(regs_.object());
4010 regs_.Restore(masm);
4011 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4012 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4013 MacroAssembler::kReturnAtEnd);
4018 __ bind(&need_incremental_pop_object);
4019 __ pop(regs_.object());
4021 __ bind(&need_incremental);
4023 // Fall through when we need to inform the incremental marker.
4027 void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4028 // ----------- S t a t e -------------
4029 // -- eax : element value to store
4030 // -- ecx : element index as smi
4031 // -- esp[0] : return address
4032 // -- esp[4] : array literal index in function
4033 // -- esp[8] : array literal
4034 // clobbers ebx, edx, edi
4035 // -----------------------------------
4038 Label double_elements;
4040 Label slow_elements;
4041 Label slow_elements_from_double;
4042 Label fast_elements;
4044 // Get array literal index, array literal and its map.
4045 __ mov(edx, Operand(esp, 1 * kPointerSize));
4046 __ mov(ebx, Operand(esp, 2 * kPointerSize));
4047 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
4049 __ CheckFastElements(edi, &double_elements);
4051 // Check for FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS elements
4052 __ JumpIfSmi(eax, &smi_element);
4053 __ CheckFastSmiElements(edi, &fast_elements, Label::kNear);
4055 // Store into the array literal requires a elements transition. Call into
4058 __ bind(&slow_elements);
4059 __ pop(edi); // Pop return address and remember to put back later for tail
4064 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4065 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
4067 __ push(edi); // Return return address so that tail call returns to right
4069 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4071 __ bind(&slow_elements_from_double);
4073 __ jmp(&slow_elements);
4075 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
4076 __ bind(&fast_elements);
4077 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4078 __ lea(ecx, FieldOperand(ebx, ecx, times_half_pointer_size,
4079 FixedArrayBase::kHeaderSize));
4080 __ mov(Operand(ecx, 0), eax);
4081 // Update the write barrier for the array store.
4082 __ RecordWrite(ebx, ecx, eax, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
4086 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS,
4087 // and value is Smi.
4088 __ bind(&smi_element);
4089 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4090 __ mov(FieldOperand(ebx, ecx, times_half_pointer_size,
4091 FixedArrayBase::kHeaderSize), eax);
4094 // Array literal has ElementsKind of FAST_*_DOUBLE_ELEMENTS.
4095 __ bind(&double_elements);
4098 __ mov(edx, FieldOperand(ebx, JSObject::kElementsOffset));
4099 __ StoreNumberToDoubleElements(eax,
4103 &slow_elements_from_double,
4110 void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4111 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4112 __ call(ces.GetCode(), RelocInfo::CODE_TARGET);
4113 int parameter_count_offset =
4114 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4115 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
4116 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4118 int additional_offset =
4119 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
4120 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
4121 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
4125 void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4126 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4127 LoadICStub stub(isolate(), state());
4128 stub.GenerateForTrampoline(masm);
4132 void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4133 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4134 KeyedLoadICStub stub(isolate(), state());
4135 stub.GenerateForTrampoline(masm);
4139 static void HandleArrayCases(MacroAssembler* masm, Register receiver,
4140 Register key, Register vector, Register slot,
4141 Register feedback, bool is_polymorphic,
4143 // feedback initially contains the feedback array
4144 Label next, next_loop, prepare_next;
4145 Label load_smi_map, compare_map;
4146 Label start_polymorphic;
4151 Register receiver_map = receiver;
4152 Register cached_map = vector;
4154 // Receiver might not be a heap object.
4155 __ JumpIfSmi(receiver, &load_smi_map);
4156 __ mov(receiver_map, FieldOperand(receiver, 0));
4157 __ bind(&compare_map);
4158 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
4160 // A named keyed load might have a 2 element array, all other cases can count
4161 // on an array with at least 2 {map, handler} pairs, so they can go right
4162 // into polymorphic array handling.
4163 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4164 __ j(not_equal, is_polymorphic ? &start_polymorphic : &next);
4166 // found, now call handler.
4167 Register handler = feedback;
4168 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
4171 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4174 if (!is_polymorphic) {
4176 __ cmp(FieldOperand(feedback, FixedArray::kLengthOffset),
4177 Immediate(Smi::FromInt(2)));
4178 __ j(not_equal, &start_polymorphic);
4184 // Polymorphic, we have to loop from 2 to N
4185 __ bind(&start_polymorphic);
4187 Register counter = key;
4188 __ mov(counter, Immediate(Smi::FromInt(2)));
4189 __ bind(&next_loop);
4190 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4191 FixedArray::kHeaderSize));
4192 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4193 __ j(not_equal, &prepare_next);
4194 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
4195 FixedArray::kHeaderSize + kPointerSize));
4199 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4202 __ bind(&prepare_next);
4203 __ add(counter, Immediate(Smi::FromInt(2)));
4204 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
4205 __ j(less, &next_loop);
4207 // We exhausted our array of map handler pairs.
4213 __ bind(&load_smi_map);
4214 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4215 __ jmp(&compare_map);
4219 static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
4220 Register key, Register vector, Register slot,
4221 Register weak_cell, Label* miss) {
4222 // feedback initially contains the feedback array
4223 Label compare_smi_map;
4225 // Move the weak map into the weak_cell register.
4226 Register ic_map = weak_cell;
4227 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
4229 // Receiver might not be a heap object.
4230 __ JumpIfSmi(receiver, &compare_smi_map);
4231 __ cmp(ic_map, FieldOperand(receiver, 0));
4232 __ j(not_equal, miss);
4233 Register handler = weak_cell;
4234 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
4235 FixedArray::kHeaderSize + kPointerSize));
4236 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4239 // In microbenchmarks, it made sense to unroll this code so that the call to
4240 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
4241 __ bind(&compare_smi_map);
4242 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
4243 __ j(not_equal, miss);
4244 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
4245 FixedArray::kHeaderSize + kPointerSize));
4246 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4251 void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
4254 void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4255 GenerateImpl(masm, true);
4259 void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4260 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
4261 Register name = LoadWithVectorDescriptor::NameRegister(); // ecx
4262 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
4263 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
4264 Register scratch = edi;
4265 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4266 FixedArray::kHeaderSize));
4268 // Is it a weak cell?
4270 Label not_array, smi_key, key_okay, miss;
4271 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
4272 __ j(not_equal, &try_array);
4273 HandleMonomorphicCase(masm, receiver, name, vector, slot, scratch, &miss);
4275 // Is it a fixed array?
4276 __ bind(&try_array);
4277 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
4278 __ j(not_equal, ¬_array);
4279 HandleArrayCases(masm, receiver, name, vector, slot, scratch, true, &miss);
4281 __ bind(¬_array);
4282 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4283 __ j(not_equal, &miss);
4286 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4287 Code::ComputeHandlerFlags(Code::LOAD_IC));
4288 masm->isolate()->stub_cache()->GenerateProbe(
4289 masm, Code::LOAD_IC, code_flags, false, receiver, name, vector, scratch);
4294 LoadIC::GenerateMiss(masm);
4298 void KeyedLoadICStub::Generate(MacroAssembler* masm) {
4299 GenerateImpl(masm, false);
4303 void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4304 GenerateImpl(masm, true);
4308 void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4309 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
4310 Register key = LoadWithVectorDescriptor::NameRegister(); // ecx
4311 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
4312 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
4313 Register feedback = edi;
4314 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
4315 FixedArray::kHeaderSize));
4316 // Is it a weak cell?
4318 Label not_array, smi_key, key_okay, miss;
4319 __ CompareRoot(FieldOperand(feedback, 0), Heap::kWeakCellMapRootIndex);
4320 __ j(not_equal, &try_array);
4321 HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, &miss);
4323 __ bind(&try_array);
4324 // Is it a fixed array?
4325 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
4326 __ j(not_equal, ¬_array);
4328 // We have a polymorphic element handler.
4329 Label polymorphic, try_poly_name;
4330 __ bind(&polymorphic);
4331 HandleArrayCases(masm, receiver, key, vector, slot, feedback, true, &miss);
4333 __ bind(¬_array);
4335 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
4336 __ j(not_equal, &try_poly_name);
4337 Handle<Code> megamorphic_stub =
4338 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4339 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4341 __ bind(&try_poly_name);
4342 // We might have a name in feedback, and a fixed array in the next slot.
4343 __ cmp(key, feedback);
4344 __ j(not_equal, &miss);
4345 // If the name comparison succeeded, we know we have a fixed array with
4346 // at least one map/handler pair.
4347 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
4348 FixedArray::kHeaderSize + kPointerSize));
4349 HandleArrayCases(masm, receiver, key, vector, slot, feedback, false, &miss);
4352 KeyedLoadIC::GenerateMiss(masm);
4356 void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4357 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4358 VectorStoreICStub stub(isolate(), state());
4359 stub.GenerateForTrampoline(masm);
4363 void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4364 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4365 VectorKeyedStoreICStub stub(isolate(), state());
4366 stub.GenerateForTrampoline(masm);
4370 void VectorStoreICStub::Generate(MacroAssembler* masm) {
4371 GenerateImpl(masm, false);
4375 void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4376 GenerateImpl(masm, true);
4380 void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4383 // TODO(mvstanton): Implement.
4385 StoreIC::GenerateMiss(masm);
4389 void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4390 GenerateImpl(masm, false);
4394 void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4395 GenerateImpl(masm, true);
4399 void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4402 // TODO(mvstanton): Implement.
4404 KeyedStoreIC::GenerateMiss(masm);
4408 void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4409 EmitLoadTypeFeedbackVector(masm, ebx);
4410 CallICStub stub(isolate(), state());
4411 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4415 void CallIC_ArrayTrampolineStub::Generate(MacroAssembler* masm) {
4416 EmitLoadTypeFeedbackVector(masm, ebx);
4417 CallIC_ArrayStub stub(isolate(), state());
4418 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4422 void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4423 if (masm->isolate()->function_entry_hook() != NULL) {
4424 ProfileEntryHookStub stub(masm->isolate());
4425 masm->CallStub(&stub);
4430 void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4431 // Save volatile registers.
4432 const int kNumSavedRegisters = 3;
4437 // Calculate and push the original stack pointer.
4438 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4441 // Retrieve our return address and use it to calculate the calling
4442 // function's address.
4443 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4444 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
4447 // Call the entry hook.
4448 DCHECK(isolate()->function_entry_hook() != NULL);
4449 __ call(FUNCTION_ADDR(isolate()->function_entry_hook()),
4450 RelocInfo::RUNTIME_ENTRY);
4451 __ add(esp, Immediate(2 * kPointerSize));
4463 static void CreateArrayDispatch(MacroAssembler* masm,
4464 AllocationSiteOverrideMode mode) {
4465 if (mode == DISABLE_ALLOCATION_SITES) {
4466 T stub(masm->isolate(),
4467 GetInitialFastElementsKind(),
4469 __ TailCallStub(&stub);
4470 } else if (mode == DONT_OVERRIDE) {
4471 int last_index = GetSequenceIndexFromFastElementsKind(
4472 TERMINAL_FAST_ELEMENTS_KIND);
4473 for (int i = 0; i <= last_index; ++i) {
4475 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4477 __ j(not_equal, &next);
4478 T stub(masm->isolate(), kind);
4479 __ TailCallStub(&stub);
4483 // If we reached this point there is a problem.
4484 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4491 static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4492 AllocationSiteOverrideMode mode) {
4493 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4494 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
4495 // eax - number of arguments
4496 // edi - constructor?
4497 // esp[0] - return address
4498 // esp[4] - last argument
4499 Label normal_sequence;
4500 if (mode == DONT_OVERRIDE) {
4501 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4502 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4503 STATIC_ASSERT(FAST_ELEMENTS == 2);
4504 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4505 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4506 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4508 // is the low bit set? If so, we are holey and that is good.
4510 __ j(not_zero, &normal_sequence);
4513 // look at the first argument
4514 __ mov(ecx, Operand(esp, kPointerSize));
4516 __ j(zero, &normal_sequence);
4518 if (mode == DISABLE_ALLOCATION_SITES) {
4519 ElementsKind initial = GetInitialFastElementsKind();
4520 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4522 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4524 DISABLE_ALLOCATION_SITES);
4525 __ TailCallStub(&stub_holey);
4527 __ bind(&normal_sequence);
4528 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4530 DISABLE_ALLOCATION_SITES);
4531 __ TailCallStub(&stub);
4532 } else if (mode == DONT_OVERRIDE) {
4533 // We are going to create a holey array, but our kind is non-holey.
4534 // Fix kind and retry.
4537 if (FLAG_debug_code) {
4538 Handle<Map> allocation_site_map =
4539 masm->isolate()->factory()->allocation_site_map();
4540 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
4541 __ Assert(equal, kExpectedAllocationSite);
4544 // Save the resulting elements kind in type info. We can't just store r3
4545 // in the AllocationSite::transition_info field because elements kind is
4546 // restricted to a portion of the field...upper bits need to be left alone.
4547 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4548 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
4549 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
4551 __ bind(&normal_sequence);
4552 int last_index = GetSequenceIndexFromFastElementsKind(
4553 TERMINAL_FAST_ELEMENTS_KIND);
4554 for (int i = 0; i <= last_index; ++i) {
4556 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4558 __ j(not_equal, &next);
4559 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4560 __ TailCallStub(&stub);
4564 // If we reached this point there is a problem.
4565 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4573 static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4574 int to_index = GetSequenceIndexFromFastElementsKind(
4575 TERMINAL_FAST_ELEMENTS_KIND);
4576 for (int i = 0; i <= to_index; ++i) {
4577 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4578 T stub(isolate, kind);
4580 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4581 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4588 void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4589 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4591 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4593 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4598 void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4600 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4601 for (int i = 0; i < 2; i++) {
4602 // For internal arrays we only need a few things
4603 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4605 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4607 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4613 void ArrayConstructorStub::GenerateDispatchToArrayStub(
4614 MacroAssembler* masm,
4615 AllocationSiteOverrideMode mode) {
4616 if (argument_count() == ANY) {
4617 Label not_zero_case, not_one_case;
4619 __ j(not_zero, ¬_zero_case);
4620 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4622 __ bind(¬_zero_case);
4624 __ j(greater, ¬_one_case);
4625 CreateArrayDispatchOneArgument(masm, mode);
4627 __ bind(¬_one_case);
4628 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4629 } else if (argument_count() == NONE) {
4630 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4631 } else if (argument_count() == ONE) {
4632 CreateArrayDispatchOneArgument(masm, mode);
4633 } else if (argument_count() == MORE_THAN_ONE) {
4634 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4641 void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4642 // ----------- S t a t e -------------
4643 // -- eax : argc (only if argument_count() is ANY or MORE_THAN_ONE)
4644 // -- ebx : AllocationSite or undefined
4645 // -- edi : constructor
4646 // -- edx : Original constructor
4647 // -- esp[0] : return address
4648 // -- esp[4] : last argument
4649 // -----------------------------------
4650 if (FLAG_debug_code) {
4651 // The array construct code is only set for the global and natives
4652 // builtin Array functions which always have maps.
4654 // Initial map for the builtin Array function should be a map.
4655 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4656 // Will both indicate a NULL and a Smi.
4657 __ test(ecx, Immediate(kSmiTagMask));
4658 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4659 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4660 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4662 // We should either have undefined in ebx or a valid AllocationSite
4663 __ AssertUndefinedOrAllocationSite(ebx);
4669 __ j(not_equal, &subclassing);
4672 // If the feedback vector is the undefined value call an array constructor
4673 // that doesn't use AllocationSites.
4674 __ cmp(ebx, isolate()->factory()->undefined_value());
4675 __ j(equal, &no_info);
4677 // Only look at the lower 16 bits of the transition info.
4678 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
4680 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4681 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
4682 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4685 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
4688 __ bind(&subclassing);
4689 __ pop(ecx); // return address.
4694 switch (argument_count()) {
4697 __ add(eax, Immediate(2));
4700 __ mov(eax, Immediate(2));
4703 __ mov(eax, Immediate(3));
4708 __ JumpToExternalReference(
4709 ExternalReference(Runtime::kArrayConstructorWithSubclassing, isolate()));
4713 void InternalArrayConstructorStub::GenerateCase(
4714 MacroAssembler* masm, ElementsKind kind) {
4715 Label not_zero_case, not_one_case;
4716 Label normal_sequence;
4719 __ j(not_zero, ¬_zero_case);
4720 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4721 __ TailCallStub(&stub0);
4723 __ bind(¬_zero_case);
4725 __ j(greater, ¬_one_case);
4727 if (IsFastPackedElementsKind(kind)) {
4728 // We might need to create a holey array
4729 // look at the first argument
4730 __ mov(ecx, Operand(esp, kPointerSize));
4732 __ j(zero, &normal_sequence);
4734 InternalArraySingleArgumentConstructorStub
4735 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4736 __ TailCallStub(&stub1_holey);
4739 __ bind(&normal_sequence);
4740 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4741 __ TailCallStub(&stub1);
4743 __ bind(¬_one_case);
4744 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4745 __ TailCallStub(&stubN);
4749 void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4750 // ----------- S t a t e -------------
4752 // -- edi : constructor
4753 // -- esp[0] : return address
4754 // -- esp[4] : last argument
4755 // -----------------------------------
4757 if (FLAG_debug_code) {
4758 // The array construct code is only set for the global and natives
4759 // builtin Array functions which always have maps.
4761 // Initial map for the builtin Array function should be a map.
4762 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4763 // Will both indicate a NULL and a Smi.
4764 __ test(ecx, Immediate(kSmiTagMask));
4765 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4766 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4767 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4770 // Figure out the right elements kind
4771 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4773 // Load the map's "bit field 2" into |result|. We only need the first byte,
4774 // but the following masking takes care of that anyway.
4775 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
4776 // Retrieve elements_kind from bit field 2.
4777 __ DecodeField<Map::ElementsKindBits>(ecx);
4779 if (FLAG_debug_code) {
4781 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4783 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
4785 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
4789 Label fast_elements_case;
4790 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4791 __ j(equal, &fast_elements_case);
4792 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4794 __ bind(&fast_elements_case);
4795 GenerateCase(masm, FAST_ELEMENTS);
4799 void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
4800 Register context_reg = esi;
4801 Register slot_reg = ebx;
4802 Register result_reg = eax;
4805 // Go up context chain to the script context.
4806 for (int i = 0; i < depth(); ++i) {
4807 __ mov(result_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
4808 context_reg = result_reg;
4811 // Load the PropertyCell value at the specified slot.
4812 __ mov(result_reg, ContextOperand(context_reg, slot_reg));
4813 __ mov(result_reg, FieldOperand(result_reg, PropertyCell::kValueOffset));
4815 // Check that value is not the_hole.
4816 __ CompareRoot(result_reg, Heap::kTheHoleValueRootIndex);
4817 __ j(equal, &slow_case, Label::kNear);
4820 // Fallback to the runtime.
4821 __ bind(&slow_case);
4822 __ SmiTag(slot_reg);
4823 __ Pop(result_reg); // Pop return address.
4825 __ Push(result_reg); // Push return address.
4826 __ TailCallRuntime(Runtime::kLoadGlobalViaContext, 1, 1);
4830 void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
4831 Register context_reg = esi;
4832 Register slot_reg = ebx;
4833 Register value_reg = eax;
4834 Register cell_reg = edi;
4835 Register cell_details_reg = edx;
4836 Register cell_value_reg = ecx;
4837 Label fast_heapobject_case, fast_smi_case, slow_case;
4839 if (FLAG_debug_code) {
4840 __ CompareRoot(value_reg, Heap::kTheHoleValueRootIndex);
4841 __ Check(not_equal, kUnexpectedValue);
4844 // Go up context chain to the script context.
4845 for (int i = 0; i < depth(); ++i) {
4846 __ mov(cell_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
4847 context_reg = cell_reg;
4850 // Load the PropertyCell at the specified slot.
4851 __ mov(cell_reg, ContextOperand(context_reg, slot_reg));
4853 // Load PropertyDetails for the cell (actually only the cell_type and kind).
4854 __ mov(cell_details_reg,
4855 FieldOperand(cell_reg, PropertyCell::kDetailsOffset));
4856 __ SmiUntag(cell_details_reg);
4857 __ and_(cell_details_reg,
4858 Immediate(PropertyDetails::PropertyCellTypeField::kMask |
4859 PropertyDetails::KindField::kMask |
4860 PropertyDetails::kAttributesReadOnlyMask));
4862 // Check if PropertyCell holds mutable data.
4863 Label not_mutable_data;
4864 __ cmp(cell_details_reg,
4865 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4866 PropertyCellType::kMutable) |
4867 PropertyDetails::KindField::encode(kData)));
4868 __ j(not_equal, ¬_mutable_data);
4869 __ JumpIfSmi(value_reg, &fast_smi_case);
4870 __ bind(&fast_heapobject_case);
4871 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
4872 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
4873 cell_details_reg, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
4875 // RecordWriteField clobbers the value register, so we need to reload.
4876 __ mov(value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
4878 __ bind(¬_mutable_data);
4880 // Check if PropertyCell value matches the new value (relevant for Constant,
4881 // ConstantType and Undefined cells).
4882 Label not_same_value;
4883 __ mov(cell_value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
4884 __ cmp(cell_value_reg, value_reg);
4885 __ j(not_equal, ¬_same_value,
4886 FLAG_debug_code ? Label::kFar : Label::kNear);
4887 // Make sure the PropertyCell is not marked READ_ONLY.
4888 __ test(cell_details_reg,
4889 Immediate(PropertyDetails::kAttributesReadOnlyMask));
4890 __ j(not_zero, &slow_case);
4891 if (FLAG_debug_code) {
4893 // This can only be true for Constant, ConstantType and Undefined cells,
4894 // because we never store the_hole via this stub.
4895 __ cmp(cell_details_reg,
4896 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4897 PropertyCellType::kConstant) |
4898 PropertyDetails::KindField::encode(kData)));
4900 __ cmp(cell_details_reg,
4901 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4902 PropertyCellType::kConstantType) |
4903 PropertyDetails::KindField::encode(kData)));
4905 __ cmp(cell_details_reg,
4906 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4907 PropertyCellType::kUndefined) |
4908 PropertyDetails::KindField::encode(kData)));
4909 __ Check(equal, kUnexpectedValue);
4913 __ bind(¬_same_value);
4915 // Check if PropertyCell contains data with constant type (and is not
4917 __ cmp(cell_details_reg,
4918 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4919 PropertyCellType::kConstantType) |
4920 PropertyDetails::KindField::encode(kData)));
4921 __ j(not_equal, &slow_case, Label::kNear);
4923 // Now either both old and new values must be SMIs or both must be heap
4924 // objects with same map.
4925 Label value_is_heap_object;
4926 __ JumpIfNotSmi(value_reg, &value_is_heap_object, Label::kNear);
4927 __ JumpIfNotSmi(cell_value_reg, &slow_case, Label::kNear);
4928 // Old and new values are SMIs, no need for a write barrier here.
4929 __ bind(&fast_smi_case);
4930 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
4932 __ bind(&value_is_heap_object);
4933 __ JumpIfSmi(cell_value_reg, &slow_case, Label::kNear);
4934 Register cell_value_map_reg = cell_value_reg;
4935 __ mov(cell_value_map_reg,
4936 FieldOperand(cell_value_reg, HeapObject::kMapOffset));
4937 __ cmp(cell_value_map_reg, FieldOperand(value_reg, HeapObject::kMapOffset));
4938 __ j(equal, &fast_heapobject_case);
4940 // Fallback to the runtime.
4941 __ bind(&slow_case);
4942 __ SmiTag(slot_reg);
4943 __ Pop(cell_reg); // Pop return address.
4946 __ Push(cell_reg); // Push return address.
4947 __ TailCallRuntime(is_strict(language_mode())
4948 ? Runtime::kStoreGlobalViaContext_Strict
4949 : Runtime::kStoreGlobalViaContext_Sloppy,
4954 // Generates an Operand for saving parameters after PrepareCallApiFunction.
4955 static Operand ApiParameterOperand(int index) {
4956 return Operand(esp, index * kPointerSize);
4960 // Prepares stack to put arguments (aligns and so on). Reserves
4961 // space for return value if needed (assumes the return value is a handle).
4962 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
4963 // etc. Saves context (esi). If space was reserved for return value then
4964 // stores the pointer to the reserved slot into esi.
4965 static void PrepareCallApiFunction(MacroAssembler* masm, int argc) {
4966 __ EnterApiExitFrame(argc);
4967 if (__ emit_debug_code()) {
4968 __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
4973 // Calls an API function. Allocates HandleScope, extracts returned value
4974 // from handle and propagates exceptions. Clobbers ebx, edi and
4975 // caller-save registers. Restores context. On return removes
4976 // stack_space * kPointerSize (GCed).
4977 static void CallApiFunctionAndReturn(MacroAssembler* masm,
4978 Register function_address,
4979 ExternalReference thunk_ref,
4980 Operand thunk_last_arg, int stack_space,
4981 Operand* stack_space_operand,
4982 Operand return_value_operand,
4983 Operand* context_restore_operand) {
4984 Isolate* isolate = masm->isolate();
4986 ExternalReference next_address =
4987 ExternalReference::handle_scope_next_address(isolate);
4988 ExternalReference limit_address =
4989 ExternalReference::handle_scope_limit_address(isolate);
4990 ExternalReference level_address =
4991 ExternalReference::handle_scope_level_address(isolate);
4993 DCHECK(edx.is(function_address));
4994 // Allocate HandleScope in callee-save registers.
4995 __ mov(ebx, Operand::StaticVariable(next_address));
4996 __ mov(edi, Operand::StaticVariable(limit_address));
4997 __ add(Operand::StaticVariable(level_address), Immediate(1));
4999 if (FLAG_log_timer_events) {
5000 FrameScope frame(masm, StackFrame::MANUAL);
5001 __ PushSafepointRegisters();
5002 __ PrepareCallCFunction(1, eax);
5003 __ mov(Operand(esp, 0),
5004 Immediate(ExternalReference::isolate_address(isolate)));
5005 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5007 __ PopSafepointRegisters();
5011 Label profiler_disabled;
5012 Label end_profiler_check;
5013 __ mov(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
5014 __ cmpb(Operand(eax, 0), 0);
5015 __ j(zero, &profiler_disabled);
5017 // Additional parameter is the address of the actual getter function.
5018 __ mov(thunk_last_arg, function_address);
5019 // Call the api function.
5020 __ mov(eax, Immediate(thunk_ref));
5022 __ jmp(&end_profiler_check);
5024 __ bind(&profiler_disabled);
5025 // Call the api function.
5026 __ call(function_address);
5027 __ bind(&end_profiler_check);
5029 if (FLAG_log_timer_events) {
5030 FrameScope frame(masm, StackFrame::MANUAL);
5031 __ PushSafepointRegisters();
5032 __ PrepareCallCFunction(1, eax);
5033 __ mov(Operand(esp, 0),
5034 Immediate(ExternalReference::isolate_address(isolate)));
5035 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5037 __ PopSafepointRegisters();
5041 // Load the value from ReturnValue
5042 __ mov(eax, return_value_operand);
5044 Label promote_scheduled_exception;
5045 Label delete_allocated_handles;
5046 Label leave_exit_frame;
5049 // No more valid handles (the result handle was the last one). Restore
5050 // previous handle scope.
5051 __ mov(Operand::StaticVariable(next_address), ebx);
5052 __ sub(Operand::StaticVariable(level_address), Immediate(1));
5053 __ Assert(above_equal, kInvalidHandleScopeLevel);
5054 __ cmp(edi, Operand::StaticVariable(limit_address));
5055 __ j(not_equal, &delete_allocated_handles);
5057 // Leave the API exit frame.
5058 __ bind(&leave_exit_frame);
5059 bool restore_context = context_restore_operand != NULL;
5060 if (restore_context) {
5061 __ mov(esi, *context_restore_operand);
5063 if (stack_space_operand != nullptr) {
5064 __ mov(ebx, *stack_space_operand);
5066 __ LeaveApiExitFrame(!restore_context);
5068 // Check if the function scheduled an exception.
5069 ExternalReference scheduled_exception_address =
5070 ExternalReference::scheduled_exception_address(isolate);
5071 __ cmp(Operand::StaticVariable(scheduled_exception_address),
5072 Immediate(isolate->factory()->the_hole_value()));
5073 __ j(not_equal, &promote_scheduled_exception);
5076 // Check if the function returned a valid JavaScript value.
5078 Register return_value = eax;
5081 __ JumpIfSmi(return_value, &ok, Label::kNear);
5082 __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
5084 __ CmpInstanceType(map, LAST_NAME_TYPE);
5085 __ j(below_equal, &ok, Label::kNear);
5087 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
5088 __ j(above_equal, &ok, Label::kNear);
5090 __ cmp(map, isolate->factory()->heap_number_map());
5091 __ j(equal, &ok, Label::kNear);
5093 __ cmp(return_value, isolate->factory()->undefined_value());
5094 __ j(equal, &ok, Label::kNear);
5096 __ cmp(return_value, isolate->factory()->true_value());
5097 __ j(equal, &ok, Label::kNear);
5099 __ cmp(return_value, isolate->factory()->false_value());
5100 __ j(equal, &ok, Label::kNear);
5102 __ cmp(return_value, isolate->factory()->null_value());
5103 __ j(equal, &ok, Label::kNear);
5105 __ Abort(kAPICallReturnedInvalidObject);
5110 if (stack_space_operand != nullptr) {
5111 DCHECK_EQ(0, stack_space);
5116 __ ret(stack_space * kPointerSize);
5119 // Re-throw by promoting a scheduled exception.
5120 __ bind(&promote_scheduled_exception);
5121 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
5123 // HandleScope limit has changed. Delete allocated extensions.
5124 ExternalReference delete_extensions =
5125 ExternalReference::delete_handle_scope_extensions(isolate);
5126 __ bind(&delete_allocated_handles);
5127 __ mov(Operand::StaticVariable(limit_address), edi);
5129 __ mov(Operand(esp, 0),
5130 Immediate(ExternalReference::isolate_address(isolate)));
5131 __ mov(eax, Immediate(delete_extensions));
5134 __ jmp(&leave_exit_frame);
5138 static void CallApiFunctionStubHelper(MacroAssembler* masm,
5139 const ParameterCount& argc,
5140 bool return_first_arg,
5141 bool call_data_undefined) {
5142 // ----------- S t a t e -------------
5144 // -- ebx : call_data
5146 // -- edx : api_function_address
5148 // -- eax : number of arguments if argc is a register
5150 // -- esp[0] : return address
5151 // -- esp[4] : last argument
5153 // -- esp[argc * 4] : first argument
5154 // -- esp[(argc + 1) * 4] : receiver
5155 // -----------------------------------
5157 Register callee = edi;
5158 Register call_data = ebx;
5159 Register holder = ecx;
5160 Register api_function_address = edx;
5161 Register context = esi;
5162 Register return_address = eax;
5164 typedef FunctionCallbackArguments FCA;
5166 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5167 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5168 STATIC_ASSERT(FCA::kDataIndex == 4);
5169 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5170 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5171 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5172 STATIC_ASSERT(FCA::kHolderIndex == 0);
5173 STATIC_ASSERT(FCA::kArgsLength == 7);
5175 DCHECK(argc.is_immediate() || eax.is(argc.reg()));
5177 if (argc.is_immediate()) {
5178 __ pop(return_address);
5182 // pop return address and save context
5183 __ xchg(context, Operand(esp, 0));
5184 return_address = context;
5193 Register scratch = call_data;
5194 if (!call_data_undefined) {
5196 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
5197 // return value default
5198 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
5202 // return value default
5206 __ push(Immediate(reinterpret_cast<int>(masm->isolate())));
5210 __ mov(scratch, esp);
5212 // push return address
5213 __ push(return_address);
5215 // load context from callee
5216 __ mov(context, FieldOperand(callee, JSFunction::kContextOffset));
5218 // API function gets reference to the v8::Arguments. If CPU profiler
5219 // is enabled wrapper function will be called and we need to pass
5220 // address of the callback as additional parameter, always allocate
5222 const int kApiArgc = 1 + 1;
5224 // Allocate the v8::Arguments structure in the arguments' space since
5225 // it's not controlled by GC.
5226 const int kApiStackSpace = 4;
5228 PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace);
5230 // FunctionCallbackInfo::implicit_args_.
5231 __ mov(ApiParameterOperand(2), scratch);
5232 if (argc.is_immediate()) {
5234 Immediate((argc.immediate() + FCA::kArgsLength - 1) * kPointerSize));
5235 // FunctionCallbackInfo::values_.
5236 __ mov(ApiParameterOperand(3), scratch);
5237 // FunctionCallbackInfo::length_.
5238 __ Move(ApiParameterOperand(4), Immediate(argc.immediate()));
5239 // FunctionCallbackInfo::is_construct_call_.
5240 __ Move(ApiParameterOperand(5), Immediate(0));
5242 __ lea(scratch, Operand(scratch, argc.reg(), times_pointer_size,
5243 (FCA::kArgsLength - 1) * kPointerSize));
5244 // FunctionCallbackInfo::values_.
5245 __ mov(ApiParameterOperand(3), scratch);
5246 // FunctionCallbackInfo::length_.
5247 __ mov(ApiParameterOperand(4), argc.reg());
5248 // FunctionCallbackInfo::is_construct_call_.
5249 __ lea(argc.reg(), Operand(argc.reg(), times_pointer_size,
5250 (FCA::kArgsLength + 1) * kPointerSize));
5251 __ mov(ApiParameterOperand(5), argc.reg());
5254 // v8::InvocationCallback's argument.
5255 __ lea(scratch, ApiParameterOperand(2));
5256 __ mov(ApiParameterOperand(0), scratch);
5258 ExternalReference thunk_ref =
5259 ExternalReference::invoke_function_callback(masm->isolate());
5261 Operand context_restore_operand(ebp,
5262 (2 + FCA::kContextSaveIndex) * kPointerSize);
5263 // Stores return the first js argument
5264 int return_value_offset = 0;
5265 if (return_first_arg) {
5266 return_value_offset = 2 + FCA::kArgsLength;
5268 return_value_offset = 2 + FCA::kReturnValueOffset;
5270 Operand return_value_operand(ebp, return_value_offset * kPointerSize);
5271 int stack_space = 0;
5272 Operand is_construct_call_operand = ApiParameterOperand(5);
5273 Operand* stack_space_operand = &is_construct_call_operand;
5274 if (argc.is_immediate()) {
5275 stack_space = argc.immediate() + FCA::kArgsLength + 1;
5276 stack_space_operand = nullptr;
5278 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5279 ApiParameterOperand(1), stack_space,
5280 stack_space_operand, return_value_operand,
5281 &context_restore_operand);
5285 void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5286 bool call_data_undefined = this->call_data_undefined();
5287 CallApiFunctionStubHelper(masm, ParameterCount(eax), false,
5288 call_data_undefined);
5292 void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5293 bool is_store = this->is_store();
5294 int argc = this->argc();
5295 bool call_data_undefined = this->call_data_undefined();
5296 CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
5297 call_data_undefined);
5301 void CallApiGetterStub::Generate(MacroAssembler* masm) {
5302 // ----------- S t a t e -------------
5303 // -- esp[0] : return address
5305 // -- esp[8 - kArgsLength*4] : PropertyCallbackArguments object
5307 // -- edx : api_function_address
5308 // -----------------------------------
5309 DCHECK(edx.is(ApiGetterDescriptor::function_address()));
5311 // array for v8::Arguments::values_, handler for name and pointer
5312 // to the values (it considered as smi in GC).
5313 const int kStackSpace = PropertyCallbackArguments::kArgsLength + 2;
5314 // Allocate space for opional callback address parameter in case
5315 // CPU profiler is active.
5316 const int kApiArgc = 2 + 1;
5318 Register api_function_address = edx;
5319 Register scratch = ebx;
5321 // load address of name
5322 __ lea(scratch, Operand(esp, 1 * kPointerSize));
5324 PrepareCallApiFunction(masm, kApiArgc);
5325 __ mov(ApiParameterOperand(0), scratch); // name.
5326 __ add(scratch, Immediate(kPointerSize));
5327 __ mov(ApiParameterOperand(1), scratch); // arguments pointer.
5329 ExternalReference thunk_ref =
5330 ExternalReference::invoke_accessor_getter_callback(isolate());
5332 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5333 ApiParameterOperand(2), kStackSpace, nullptr,
5334 Operand(ebp, 7 * kPointerSize), NULL);
5340 } // namespace internal
5343 #endif // V8_TARGET_ARCH_X87