1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
7 #if V8_TARGET_ARCH_IA32
9 #include "src/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, PASS_ARGUMENTS);
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, PASS_ARGUMENTS);
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 __ sub(esp, Immediate(kDoubleSize * XMMRegister::kMaxNumRegisters));
133 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
134 XMMRegister reg = XMMRegister::from_code(i);
135 __ movsd(Operand(esp, i * kDoubleSize), reg);
138 const int argument_count = 1;
140 AllowExternalCallThatCantCauseGC scope(masm);
141 __ PrepareCallCFunction(argument_count, ecx);
142 __ mov(Operand(esp, 0 * kPointerSize),
143 Immediate(ExternalReference::isolate_address(isolate())));
145 ExternalReference::store_buffer_overflow_function(isolate()),
147 if (save_doubles()) {
148 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
149 XMMRegister reg = XMMRegister::from_code(i);
150 __ movsd(reg, Operand(esp, i * kDoubleSize));
152 __ add(esp, Immediate(kDoubleSize * XMMRegister::kMaxNumRegisters));
159 class FloatingPointHelper : public AllStatic {
166 // Code pattern for loading a floating point value. Input value must
167 // be either a smi or a heap number object (fp value). Requirements:
168 // operand in register number. Returns operand as floating point number
170 static void LoadFloatOperand(MacroAssembler* masm, Register number);
172 // Test if operands are smi or number objects (fp). Requirements:
173 // operand_1 in eax, operand_2 in edx; falls through on float
174 // operands, jumps to the non_float label otherwise.
175 static void CheckFloatOperands(MacroAssembler* masm,
179 // Test if operands are numbers (smi or HeapNumber objects), and load
180 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
181 // either operand is not a number. Operands are in edx and eax.
182 // Leaves operands unchanged.
183 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
187 void DoubleToIStub::Generate(MacroAssembler* masm) {
188 Register input_reg = this->source();
189 Register final_result_reg = this->destination();
190 DCHECK(is_truncating());
192 Label check_negative, process_64_bits, done, done_no_stash;
194 int double_offset = offset();
196 // Account for return address and saved regs if input is esp.
197 if (input_reg.is(esp)) double_offset += 3 * kPointerSize;
199 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
200 MemOperand exponent_operand(MemOperand(input_reg,
201 double_offset + kDoubleSize / 2));
205 Register scratch_candidates[3] = { ebx, edx, edi };
206 for (int i = 0; i < 3; i++) {
207 scratch1 = scratch_candidates[i];
208 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
211 // Since we must use ecx for shifts below, use some other register (eax)
212 // to calculate the result if ecx is the requested return register.
213 Register result_reg = final_result_reg.is(ecx) ? eax : final_result_reg;
214 // Save ecx if it isn't the return register and therefore volatile, or if it
215 // is the return register, then save the temp register we use in its stead for
217 Register save_reg = final_result_reg.is(ecx) ? eax : ecx;
221 bool stash_exponent_copy = !input_reg.is(esp);
222 __ mov(scratch1, mantissa_operand);
223 if (CpuFeatures::IsSupported(SSE3)) {
224 CpuFeatureScope scope(masm, SSE3);
225 // Load x87 register with heap number.
226 __ fld_d(mantissa_operand);
228 __ mov(ecx, exponent_operand);
229 if (stash_exponent_copy) __ push(ecx);
231 __ and_(ecx, HeapNumber::kExponentMask);
232 __ shr(ecx, HeapNumber::kExponentShift);
233 __ lea(result_reg, MemOperand(ecx, -HeapNumber::kExponentBias));
234 __ cmp(result_reg, Immediate(HeapNumber::kMantissaBits));
235 __ j(below, &process_64_bits);
237 // Result is entirely in lower 32-bits of mantissa
238 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
239 if (CpuFeatures::IsSupported(SSE3)) {
242 __ sub(ecx, Immediate(delta));
243 __ xor_(result_reg, result_reg);
244 __ cmp(ecx, Immediate(31));
247 __ jmp(&check_negative);
249 __ bind(&process_64_bits);
250 if (CpuFeatures::IsSupported(SSE3)) {
251 CpuFeatureScope scope(masm, SSE3);
252 if (stash_exponent_copy) {
253 // Already a copy of the exponent on the stack, overwrite it.
254 STATIC_ASSERT(kDoubleSize == 2 * kPointerSize);
255 __ sub(esp, Immediate(kDoubleSize / 2));
257 // Reserve space for 64 bit answer.
258 __ sub(esp, Immediate(kDoubleSize)); // Nolint.
260 // Do conversion, which cannot fail because we checked the exponent.
261 __ fisttp_d(Operand(esp, 0));
262 __ mov(result_reg, Operand(esp, 0)); // Load low word of answer as result
263 __ add(esp, Immediate(kDoubleSize));
264 __ jmp(&done_no_stash);
266 // Result must be extracted from shifted 32-bit mantissa
267 __ sub(ecx, Immediate(delta));
269 if (stash_exponent_copy) {
270 __ mov(result_reg, MemOperand(esp, 0));
272 __ mov(result_reg, exponent_operand);
275 Immediate(static_cast<uint32_t>(Double::kSignificandMask >> 32)));
277 Immediate(static_cast<uint32_t>(Double::kHiddenBit >> 32)));
278 __ shrd(result_reg, scratch1);
279 __ shr_cl(result_reg);
280 __ test(ecx, Immediate(32));
281 __ cmov(not_equal, scratch1, result_reg);
284 // If the double was negative, negate the integer result.
285 __ bind(&check_negative);
286 __ mov(result_reg, scratch1);
288 if (stash_exponent_copy) {
289 __ cmp(MemOperand(esp, 0), Immediate(0));
291 __ cmp(exponent_operand, Immediate(0));
293 __ cmov(greater, result_reg, scratch1);
297 if (stash_exponent_copy) {
298 __ add(esp, Immediate(kDoubleSize / 2));
300 __ bind(&done_no_stash);
301 if (!final_result_reg.is(result_reg)) {
302 DCHECK(final_result_reg.is(ecx));
303 __ mov(final_result_reg, result_reg);
311 void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
313 Label load_smi, done;
315 __ JumpIfSmi(number, &load_smi, Label::kNear);
316 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
317 __ jmp(&done, Label::kNear);
322 __ fild_s(Operand(esp, 0));
329 void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
330 Label* not_numbers) {
331 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
332 // Load operand in edx into xmm0, or branch to not_numbers.
333 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
334 Factory* factory = masm->isolate()->factory();
335 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
336 __ j(not_equal, not_numbers); // Argument in edx is not a number.
337 __ movsd(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
339 // Load operand in eax into xmm1, or branch to not_numbers.
340 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
341 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
342 __ j(equal, &load_float_eax, Label::kNear);
343 __ jmp(not_numbers); // Argument in eax is not a number.
344 __ bind(&load_smi_edx);
345 __ SmiUntag(edx); // Untag smi before converting to float.
346 __ Cvtsi2sd(xmm0, edx);
347 __ SmiTag(edx); // Retag smi for heap number overwriting test.
349 __ bind(&load_smi_eax);
350 __ SmiUntag(eax); // Untag smi before converting to float.
351 __ Cvtsi2sd(xmm1, eax);
352 __ SmiTag(eax); // Retag smi for heap number overwriting test.
353 __ jmp(&done, Label::kNear);
354 __ bind(&load_float_eax);
355 __ movsd(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
360 void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
363 Label test_other, done;
364 // Test if both operands are floats or smi -> scratch=k_is_float;
365 // Otherwise scratch = k_not_float.
366 __ JumpIfSmi(edx, &test_other, Label::kNear);
367 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
368 Factory* factory = masm->isolate()->factory();
369 __ cmp(scratch, factory->heap_number_map());
370 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
372 __ bind(&test_other);
373 __ JumpIfSmi(eax, &done, Label::kNear);
374 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
375 __ cmp(scratch, factory->heap_number_map());
376 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
378 // Fall-through: Both operands are numbers.
383 void MathPowStub::Generate(MacroAssembler* masm) {
384 Factory* factory = isolate()->factory();
385 const Register exponent = MathPowTaggedDescriptor::exponent();
386 DCHECK(exponent.is(eax));
387 const Register base = edx;
388 const Register scratch = ecx;
389 const XMMRegister double_result = xmm3;
390 const XMMRegister double_base = xmm2;
391 const XMMRegister double_exponent = xmm1;
392 const XMMRegister double_scratch = xmm4;
394 Label call_runtime, done, exponent_not_smi, int_exponent;
396 // Save 1 in double_result - we need this several times later on.
397 __ mov(scratch, Immediate(1));
398 __ Cvtsi2sd(double_result, scratch);
400 if (exponent_type() == ON_STACK) {
401 Label base_is_smi, unpack_exponent;
402 // The exponent and base are supplied as arguments on the stack.
403 // This can only happen if the stub is called from non-optimized code.
404 // Load input parameters from stack.
405 __ mov(base, Operand(esp, 2 * kPointerSize));
406 __ mov(exponent, Operand(esp, 1 * kPointerSize));
408 __ JumpIfSmi(base, &base_is_smi, Label::kNear);
409 __ cmp(FieldOperand(base, HeapObject::kMapOffset),
410 factory->heap_number_map());
411 __ j(not_equal, &call_runtime);
413 __ movsd(double_base, FieldOperand(base, HeapNumber::kValueOffset));
414 __ jmp(&unpack_exponent, Label::kNear);
416 __ bind(&base_is_smi);
418 __ Cvtsi2sd(double_base, base);
420 __ bind(&unpack_exponent);
421 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
422 __ SmiUntag(exponent);
423 __ jmp(&int_exponent);
425 __ bind(&exponent_not_smi);
426 __ cmp(FieldOperand(exponent, HeapObject::kMapOffset),
427 factory->heap_number_map());
428 __ j(not_equal, &call_runtime);
429 __ movsd(double_exponent,
430 FieldOperand(exponent, HeapNumber::kValueOffset));
431 } else if (exponent_type() == TAGGED) {
432 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
433 __ SmiUntag(exponent);
434 __ jmp(&int_exponent);
436 __ bind(&exponent_not_smi);
437 __ movsd(double_exponent,
438 FieldOperand(exponent, HeapNumber::kValueOffset));
441 if (exponent_type() != INTEGER) {
442 Label fast_power, try_arithmetic_simplification;
443 __ DoubleToI(exponent, double_exponent, double_scratch,
444 TREAT_MINUS_ZERO_AS_ZERO, &try_arithmetic_simplification,
445 &try_arithmetic_simplification,
446 &try_arithmetic_simplification);
447 __ jmp(&int_exponent);
449 __ bind(&try_arithmetic_simplification);
450 // Skip to runtime if possibly NaN (indicated by the indefinite integer).
451 __ cvttsd2si(exponent, Operand(double_exponent));
452 __ cmp(exponent, Immediate(0x1));
453 __ j(overflow, &call_runtime);
455 if (exponent_type() == ON_STACK) {
456 // Detect square root case. Crankshaft detects constant +/-0.5 at
457 // compile time and uses DoMathPowHalf instead. We then skip this check
458 // for non-constant cases of +/-0.5 as these hardly occur.
459 Label continue_sqrt, continue_rsqrt, not_plus_half;
461 // Load double_scratch with 0.5.
462 __ mov(scratch, Immediate(0x3F000000u));
463 __ movd(double_scratch, scratch);
464 __ cvtss2sd(double_scratch, double_scratch);
465 // Already ruled out NaNs for exponent.
466 __ ucomisd(double_scratch, double_exponent);
467 __ j(not_equal, ¬_plus_half, Label::kNear);
469 // Calculates square root of base. Check for the special case of
470 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
471 // According to IEEE-754, single-precision -Infinity has the highest
472 // 9 bits set and the lowest 23 bits cleared.
473 __ mov(scratch, 0xFF800000u);
474 __ movd(double_scratch, scratch);
475 __ cvtss2sd(double_scratch, double_scratch);
476 __ ucomisd(double_base, double_scratch);
477 // Comparing -Infinity with NaN results in "unordered", which sets the
478 // zero flag as if both were equal. However, it also sets the carry flag.
479 __ j(not_equal, &continue_sqrt, Label::kNear);
480 __ j(carry, &continue_sqrt, Label::kNear);
482 // Set result to Infinity in the special case.
483 __ xorps(double_result, double_result);
484 __ subsd(double_result, double_scratch);
487 __ bind(&continue_sqrt);
488 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
489 __ xorps(double_scratch, double_scratch);
490 __ addsd(double_scratch, double_base); // Convert -0 to +0.
491 __ sqrtsd(double_result, double_scratch);
495 __ bind(¬_plus_half);
496 // Load double_exponent with -0.5 by substracting 1.
497 __ subsd(double_scratch, double_result);
498 // Already ruled out NaNs for exponent.
499 __ ucomisd(double_scratch, double_exponent);
500 __ j(not_equal, &fast_power, Label::kNear);
502 // Calculates reciprocal of square root of base. Check for the special
503 // case of Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
504 // According to IEEE-754, single-precision -Infinity has the highest
505 // 9 bits set and the lowest 23 bits cleared.
506 __ mov(scratch, 0xFF800000u);
507 __ movd(double_scratch, scratch);
508 __ cvtss2sd(double_scratch, double_scratch);
509 __ ucomisd(double_base, double_scratch);
510 // Comparing -Infinity with NaN results in "unordered", which sets the
511 // zero flag as if both were equal. However, it also sets the carry flag.
512 __ j(not_equal, &continue_rsqrt, Label::kNear);
513 __ j(carry, &continue_rsqrt, Label::kNear);
515 // Set result to 0 in the special case.
516 __ xorps(double_result, double_result);
519 __ bind(&continue_rsqrt);
520 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
521 __ xorps(double_exponent, double_exponent);
522 __ addsd(double_exponent, double_base); // Convert -0 to +0.
523 __ sqrtsd(double_exponent, double_exponent);
524 __ divsd(double_result, double_exponent);
528 // Using FPU instructions to calculate power.
529 Label fast_power_failed;
530 __ bind(&fast_power);
531 __ fnclex(); // Clear flags to catch exceptions later.
532 // Transfer (B)ase and (E)xponent onto the FPU register stack.
533 __ sub(esp, Immediate(kDoubleSize));
534 __ movsd(Operand(esp, 0), double_exponent);
535 __ fld_d(Operand(esp, 0)); // E
536 __ movsd(Operand(esp, 0), double_base);
537 __ fld_d(Operand(esp, 0)); // B, E
539 // Exponent is in st(1) and base is in st(0)
540 // B ^ E = (2^(E * log2(B)) - 1) + 1 = (2^X - 1) + 1 for X = E * log2(B)
541 // FYL2X calculates st(1) * log2(st(0))
544 __ frndint(); // rnd(X), X
545 __ fsub(1); // rnd(X), X-rnd(X)
546 __ fxch(1); // X - rnd(X), rnd(X)
547 // F2XM1 calculates 2^st(0) - 1 for -1 < st(0) < 1
548 __ f2xm1(); // 2^(X-rnd(X)) - 1, rnd(X)
549 __ fld1(); // 1, 2^(X-rnd(X)) - 1, rnd(X)
550 __ faddp(1); // 2^(X-rnd(X)), rnd(X)
551 // FSCALE calculates st(0) * 2^st(1)
552 __ fscale(); // 2^X, rnd(X)
554 // Bail out to runtime in case of exceptions in the status word.
556 __ test_b(eax, 0x5F); // We check for all but precision exception.
557 __ j(not_zero, &fast_power_failed, Label::kNear);
558 __ fstp_d(Operand(esp, 0));
559 __ movsd(double_result, Operand(esp, 0));
560 __ add(esp, Immediate(kDoubleSize));
563 __ bind(&fast_power_failed);
565 __ add(esp, Immediate(kDoubleSize));
566 __ jmp(&call_runtime);
569 // Calculate power with integer exponent.
570 __ bind(&int_exponent);
571 const XMMRegister double_scratch2 = double_exponent;
572 __ mov(scratch, exponent); // Back up exponent.
573 __ movsd(double_scratch, double_base); // Back up base.
574 __ movsd(double_scratch2, double_result); // Load double_exponent with 1.
576 // Get absolute value of exponent.
577 Label no_neg, while_true, while_false;
578 __ test(scratch, scratch);
579 __ j(positive, &no_neg, Label::kNear);
583 __ j(zero, &while_false, Label::kNear);
585 // Above condition means CF==0 && ZF==0. This means that the
586 // bit that has been shifted out is 0 and the result is not 0.
587 __ j(above, &while_true, Label::kNear);
588 __ movsd(double_result, double_scratch);
589 __ j(zero, &while_false, Label::kNear);
591 __ bind(&while_true);
593 __ mulsd(double_scratch, double_scratch);
594 __ j(above, &while_true, Label::kNear);
595 __ mulsd(double_result, double_scratch);
596 __ j(not_zero, &while_true);
598 __ bind(&while_false);
599 // scratch has the original value of the exponent - if the exponent is
600 // negative, return 1/result.
601 __ test(exponent, exponent);
602 __ j(positive, &done);
603 __ divsd(double_scratch2, double_result);
604 __ movsd(double_result, double_scratch2);
605 // Test whether result is zero. Bail out to check for subnormal result.
606 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
607 __ xorps(double_scratch2, double_scratch2);
608 __ ucomisd(double_scratch2, double_result); // Result cannot be NaN.
609 // double_exponent aliased as double_scratch2 has already been overwritten
610 // and may not have contained the exponent value in the first place when the
611 // exponent is a smi. We reset it with exponent value before bailing out.
612 __ j(not_equal, &done);
613 __ Cvtsi2sd(double_exponent, exponent);
615 // Returning or bailing out.
616 Counters* counters = isolate()->counters();
617 if (exponent_type() == ON_STACK) {
618 // The arguments are still on the stack.
619 __ bind(&call_runtime);
620 __ TailCallRuntime(Runtime::kMathPowRT, 2, 1);
622 // The stub is called from non-optimized code, which expects the result
623 // as heap number in exponent.
625 __ AllocateHeapNumber(eax, scratch, base, &call_runtime);
626 __ movsd(FieldOperand(eax, HeapNumber::kValueOffset), double_result);
627 __ IncrementCounter(counters->math_pow(), 1);
628 __ ret(2 * kPointerSize);
630 __ bind(&call_runtime);
632 AllowExternalCallThatCantCauseGC scope(masm);
633 __ PrepareCallCFunction(4, scratch);
634 __ movsd(Operand(esp, 0 * kDoubleSize), double_base);
635 __ movsd(Operand(esp, 1 * kDoubleSize), double_exponent);
637 ExternalReference::power_double_double_function(isolate()), 4);
639 // Return value is in st(0) on ia32.
640 // Store it into the (fixed) result register.
641 __ sub(esp, Immediate(kDoubleSize));
642 __ fstp_d(Operand(esp, 0));
643 __ movsd(double_result, Operand(esp, 0));
644 __ add(esp, Immediate(kDoubleSize));
647 __ IncrementCounter(counters->math_pow(), 1);
653 void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
655 Register receiver = LoadDescriptor::ReceiverRegister();
656 // With careful management, we won't have to save slot and vector on
657 // the stack. Simply handle the possibly missing case first.
658 // TODO(mvstanton): this code can be more efficient.
659 __ cmp(FieldOperand(receiver, JSFunction::kPrototypeOrInitialMapOffset),
660 Immediate(isolate()->factory()->the_hole_value()));
662 __ TryGetFunctionPrototype(receiver, eax, ebx, &miss);
666 PropertyAccessCompiler::TailCallBuiltin(
667 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
671 void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
672 // Return address is on the stack.
675 Register receiver = LoadDescriptor::ReceiverRegister();
676 Register key = LoadDescriptor::NameRegister();
677 Register scratch = eax;
678 DCHECK(!scratch.is(receiver) && !scratch.is(key));
680 // Check that the key is an array index, that is Uint32.
681 __ test(key, Immediate(kSmiTagMask | kSmiSignMask));
682 __ j(not_zero, &slow);
684 // Everything is fine, call runtime.
686 __ push(receiver); // receiver
688 __ push(scratch); // return address
690 // Perform tail call to the entry.
691 ExternalReference ref = ExternalReference(
692 IC_Utility(IC::kLoadElementWithInterceptor), masm->isolate());
693 __ TailCallExternalReference(ref, 2, 1);
696 PropertyAccessCompiler::TailCallBuiltin(
697 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
701 void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
702 // Return address is on the stack.
705 Register receiver = LoadDescriptor::ReceiverRegister();
706 Register index = LoadDescriptor::NameRegister();
707 Register scratch = edi;
708 DCHECK(!scratch.is(receiver) && !scratch.is(index));
709 Register result = eax;
710 DCHECK(!result.is(scratch));
711 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
712 result.is(LoadDescriptor::SlotRegister()));
714 // StringCharAtGenerator doesn't use the result register until it's passed
715 // the different miss possibilities. If it did, we would have a conflict
716 // when FLAG_vector_ics is true.
717 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
718 &miss, // When not a string.
719 &miss, // When not a number.
720 &miss, // When index out of range.
721 STRING_INDEX_IS_ARRAY_INDEX,
723 char_at_generator.GenerateFast(masm);
726 StubRuntimeCallHelper call_helper;
727 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
730 PropertyAccessCompiler::TailCallBuiltin(
731 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
735 void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
736 // The key is in edx and the parameter count is in eax.
737 DCHECK(edx.is(ArgumentsAccessReadDescriptor::index()));
738 DCHECK(eax.is(ArgumentsAccessReadDescriptor::parameter_count()));
740 // The displacement is used for skipping the frame pointer on the
741 // stack. It is the offset of the last parameter (if any) relative
742 // to the frame pointer.
743 static const int kDisplacement = 1 * kPointerSize;
745 // Check that the key is a smi.
747 __ JumpIfNotSmi(edx, &slow, Label::kNear);
749 // Check if the calling frame is an arguments adaptor frame.
751 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
752 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
753 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
754 __ j(equal, &adaptor, Label::kNear);
756 // Check index against formal parameters count limit passed in
757 // through register eax. Use unsigned comparison to get negative
760 __ j(above_equal, &slow, Label::kNear);
762 // Read the argument from the stack and return it.
763 STATIC_ASSERT(kSmiTagSize == 1);
764 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
765 __ lea(ebx, Operand(ebp, eax, times_2, 0));
767 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
770 // Arguments adaptor case: Check index against actual arguments
771 // limit found in the arguments adaptor frame. Use unsigned
772 // comparison to get negative check for free.
774 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
776 __ j(above_equal, &slow, Label::kNear);
778 // Read the argument from the stack and return it.
779 STATIC_ASSERT(kSmiTagSize == 1);
780 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
781 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
783 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
786 // Slow-case: Handle non-smi or out-of-bounds access to arguments
787 // by calling the runtime system.
789 __ pop(ebx); // Return address.
792 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
796 void ArgumentsAccessStub::GenerateNewSloppySlow(MacroAssembler* masm) {
797 // esp[0] : return address
798 // esp[4] : number of parameters
799 // esp[8] : receiver displacement
800 // esp[12] : function
802 // Check if the calling frame is an arguments adaptor frame.
804 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
805 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
806 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
807 __ j(not_equal, &runtime, Label::kNear);
809 // Patch the arguments.length and the parameters pointer.
810 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
811 __ mov(Operand(esp, 1 * kPointerSize), ecx);
812 __ lea(edx, Operand(edx, ecx, times_2,
813 StandardFrameConstants::kCallerSPOffset));
814 __ mov(Operand(esp, 2 * kPointerSize), edx);
817 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
821 void ArgumentsAccessStub::GenerateNewSloppyFast(MacroAssembler* masm) {
822 // esp[0] : return address
823 // esp[4] : number of parameters (tagged)
824 // esp[8] : receiver displacement
825 // esp[12] : function
827 // ebx = parameter count (tagged)
828 __ mov(ebx, Operand(esp, 1 * kPointerSize));
830 // Check if the calling frame is an arguments adaptor frame.
831 // TODO(rossberg): Factor out some of the bits that are shared with the other
832 // Generate* functions.
834 Label adaptor_frame, try_allocate;
835 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
836 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
837 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
838 __ j(equal, &adaptor_frame, Label::kNear);
840 // No adaptor, parameter count = argument count.
842 __ jmp(&try_allocate, Label::kNear);
844 // We have an adaptor frame. Patch the parameters pointer.
845 __ bind(&adaptor_frame);
846 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
847 __ lea(edx, Operand(edx, ecx, times_2,
848 StandardFrameConstants::kCallerSPOffset));
849 __ mov(Operand(esp, 2 * kPointerSize), edx);
851 // ebx = parameter count (tagged)
852 // ecx = argument count (smi-tagged)
853 // esp[4] = parameter count (tagged)
854 // esp[8] = address of receiver argument
855 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
857 __ j(less_equal, &try_allocate, Label::kNear);
860 __ bind(&try_allocate);
862 // Save mapped parameter count.
865 // Compute the sizes of backing store, parameter map, and arguments object.
866 // 1. Parameter map, has 2 extra words containing context and backing store.
867 const int kParameterMapHeaderSize =
868 FixedArray::kHeaderSize + 2 * kPointerSize;
869 Label no_parameter_map;
871 __ j(zero, &no_parameter_map, Label::kNear);
872 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
873 __ bind(&no_parameter_map);
876 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
878 // 3. Arguments object.
879 __ add(ebx, Immediate(Heap::kSloppyArgumentsObjectSize));
881 // Do the allocation of all three objects in one go.
882 __ Allocate(ebx, eax, edx, edi, &runtime, TAG_OBJECT);
884 // eax = address of new object(s) (tagged)
885 // ecx = argument count (smi-tagged)
886 // esp[0] = mapped parameter count (tagged)
887 // esp[8] = parameter count (tagged)
888 // esp[12] = address of receiver argument
889 // Get the arguments map from the current native context into edi.
890 Label has_mapped_parameters, instantiate;
891 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
892 __ mov(edi, FieldOperand(edi, GlobalObject::kNativeContextOffset));
893 __ mov(ebx, Operand(esp, 0 * kPointerSize));
895 __ j(not_zero, &has_mapped_parameters, Label::kNear);
898 Operand(edi, Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX)));
899 __ jmp(&instantiate, Label::kNear);
901 __ bind(&has_mapped_parameters);
902 __ mov(edi, Operand(edi, Context::SlotOffset(
903 Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX)));
904 __ bind(&instantiate);
906 // eax = address of new object (tagged)
907 // ebx = mapped parameter count (tagged)
908 // ecx = argument count (smi-tagged)
909 // edi = address of arguments map (tagged)
910 // esp[0] = mapped parameter count (tagged)
911 // esp[8] = parameter count (tagged)
912 // esp[12] = address of receiver argument
913 // Copy the JS object part.
914 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
915 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
916 masm->isolate()->factory()->empty_fixed_array());
917 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
918 masm->isolate()->factory()->empty_fixed_array());
920 // Set up the callee in-object property.
921 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
922 __ mov(edx, Operand(esp, 4 * kPointerSize));
923 __ AssertNotSmi(edx);
924 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
925 Heap::kArgumentsCalleeIndex * kPointerSize),
928 // Use the length (smi tagged) and set that as an in-object property too.
930 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
931 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
932 Heap::kArgumentsLengthIndex * kPointerSize),
935 // Set up the elements pointer in the allocated arguments object.
936 // If we allocated a parameter map, edi will point there, otherwise to the
938 __ lea(edi, Operand(eax, Heap::kSloppyArgumentsObjectSize));
939 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
941 // eax = address of new object (tagged)
942 // ebx = mapped parameter count (tagged)
943 // ecx = argument count (tagged)
944 // edi = address of parameter map or backing store (tagged)
945 // esp[0] = mapped parameter count (tagged)
946 // esp[8] = parameter count (tagged)
947 // esp[12] = address of receiver argument
951 // Initialize parameter map. If there are no mapped arguments, we're done.
952 Label skip_parameter_map;
954 __ j(zero, &skip_parameter_map);
956 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
957 Immediate(isolate()->factory()->sloppy_arguments_elements_map()));
958 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
959 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
960 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
961 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
962 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
964 // Copy the parameter slots and the holes in the arguments.
965 // We need to fill in mapped_parameter_count slots. They index the context,
966 // where parameters are stored in reverse order, at
967 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
968 // The mapped parameter thus need to get indices
969 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
970 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
971 // We loop from right to left.
972 Label parameters_loop, parameters_test;
974 __ mov(eax, Operand(esp, 2 * kPointerSize));
975 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
976 __ add(ebx, Operand(esp, 4 * kPointerSize));
978 __ mov(ecx, isolate()->factory()->the_hole_value());
980 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
981 // eax = loop variable (tagged)
982 // ebx = mapping index (tagged)
983 // ecx = the hole value
984 // edx = address of parameter map (tagged)
985 // edi = address of backing store (tagged)
986 // esp[0] = argument count (tagged)
987 // esp[4] = address of new object (tagged)
988 // esp[8] = mapped parameter count (tagged)
989 // esp[16] = parameter count (tagged)
990 // esp[20] = address of receiver argument
991 __ jmp(¶meters_test, Label::kNear);
993 __ bind(¶meters_loop);
994 __ sub(eax, Immediate(Smi::FromInt(1)));
995 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
996 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
997 __ add(ebx, Immediate(Smi::FromInt(1)));
998 __ bind(¶meters_test);
1000 __ j(not_zero, ¶meters_loop, Label::kNear);
1003 __ bind(&skip_parameter_map);
1005 // ecx = argument count (tagged)
1006 // edi = address of backing store (tagged)
1007 // esp[0] = address of new object (tagged)
1008 // esp[4] = mapped parameter count (tagged)
1009 // esp[12] = parameter count (tagged)
1010 // esp[16] = address of receiver argument
1011 // Copy arguments header and remaining slots (if there are any).
1012 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
1013 Immediate(isolate()->factory()->fixed_array_map()));
1014 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
1016 Label arguments_loop, arguments_test;
1017 __ mov(ebx, Operand(esp, 1 * kPointerSize));
1018 __ mov(edx, Operand(esp, 4 * kPointerSize));
1019 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
1021 __ jmp(&arguments_test, Label::kNear);
1023 __ bind(&arguments_loop);
1024 __ sub(edx, Immediate(kPointerSize));
1025 __ mov(eax, Operand(edx, 0));
1026 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
1027 __ add(ebx, Immediate(Smi::FromInt(1)));
1029 __ bind(&arguments_test);
1031 __ j(less, &arguments_loop, Label::kNear);
1034 __ pop(eax); // Address of arguments object.
1035 __ pop(ebx); // Parameter count.
1037 // Return and remove the on-stack parameters.
1038 __ ret(3 * kPointerSize);
1040 // Do the runtime call to allocate the arguments object.
1042 __ pop(eax); // Remove saved parameter count.
1043 __ mov(Operand(esp, 1 * kPointerSize), ecx); // Patch argument count.
1044 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
1048 void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
1049 // esp[0] : return address
1050 // esp[4] : number of parameters
1051 // esp[8] : receiver displacement
1052 // esp[12] : function
1054 // Check if the calling frame is an arguments adaptor frame.
1055 Label adaptor_frame, try_allocate, runtime;
1056 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1057 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
1058 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1059 __ j(equal, &adaptor_frame, Label::kNear);
1061 // Get the length from the frame.
1062 __ mov(ecx, Operand(esp, 1 * kPointerSize));
1063 __ jmp(&try_allocate, Label::kNear);
1065 // Patch the arguments.length and the parameters pointer.
1066 __ bind(&adaptor_frame);
1067 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1069 __ lea(edx, Operand(edx, ecx, times_2,
1070 StandardFrameConstants::kCallerSPOffset));
1071 __ mov(Operand(esp, 1 * kPointerSize), ecx);
1072 __ mov(Operand(esp, 2 * kPointerSize), edx);
1074 // Try the new space allocation. Start out with computing the size of
1075 // the arguments object and the elements array.
1076 Label add_arguments_object;
1077 __ bind(&try_allocate);
1079 __ j(zero, &add_arguments_object, Label::kNear);
1080 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
1081 __ bind(&add_arguments_object);
1082 __ add(ecx, Immediate(Heap::kStrictArgumentsObjectSize));
1084 // Do the allocation of both objects in one go.
1085 __ Allocate(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
1087 // Get the arguments map from the current native context.
1088 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1089 __ mov(edi, FieldOperand(edi, GlobalObject::kNativeContextOffset));
1090 const int offset = Context::SlotOffset(Context::STRICT_ARGUMENTS_MAP_INDEX);
1091 __ mov(edi, Operand(edi, offset));
1093 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
1094 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
1095 masm->isolate()->factory()->empty_fixed_array());
1096 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
1097 masm->isolate()->factory()->empty_fixed_array());
1099 // Get the length (smi tagged) and set that as an in-object property too.
1100 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
1101 __ mov(ecx, Operand(esp, 1 * kPointerSize));
1103 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
1104 Heap::kArgumentsLengthIndex * kPointerSize),
1107 // If there are no actual arguments, we're done.
1110 __ j(zero, &done, Label::kNear);
1112 // Get the parameters pointer from the stack.
1113 __ mov(edx, Operand(esp, 2 * kPointerSize));
1115 // Set up the elements pointer in the allocated arguments object and
1116 // initialize the header in the elements fixed array.
1117 __ lea(edi, Operand(eax, Heap::kStrictArgumentsObjectSize));
1118 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
1119 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
1120 Immediate(isolate()->factory()->fixed_array_map()));
1122 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
1123 // Untag the length for the loop below.
1126 // Copy the fixed array slots.
1129 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
1130 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
1131 __ add(edi, Immediate(kPointerSize));
1132 __ sub(edx, Immediate(kPointerSize));
1134 __ j(not_zero, &loop);
1136 // Return and remove the on-stack parameters.
1138 __ ret(3 * kPointerSize);
1140 // Do the runtime call to allocate the arguments object.
1142 __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
1146 void RestParamAccessStub::GenerateNew(MacroAssembler* masm) {
1147 // esp[0] : return address
1148 // esp[4] : language mode
1149 // esp[8] : index of rest parameter
1150 // esp[12] : number of parameters
1151 // esp[16] : receiver displacement
1153 // Check if the calling frame is an arguments adaptor frame.
1155 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1156 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
1157 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1158 __ j(not_equal, &runtime);
1160 // Patch the arguments.length and the parameters pointer.
1161 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1162 __ mov(Operand(esp, 3 * kPointerSize), ecx);
1163 __ lea(edx, Operand(edx, ecx, times_2,
1164 StandardFrameConstants::kCallerSPOffset));
1165 __ mov(Operand(esp, 4 * kPointerSize), edx);
1168 __ TailCallRuntime(Runtime::kNewRestParam, 4, 1);
1172 void RegExpExecStub::Generate(MacroAssembler* masm) {
1173 // Just jump directly to runtime if native RegExp is not selected at compile
1174 // time or if regexp entry in generated code is turned off runtime switch or
1176 #ifdef V8_INTERPRETED_REGEXP
1177 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1178 #else // V8_INTERPRETED_REGEXP
1180 // Stack frame on entry.
1181 // esp[0]: return address
1182 // esp[4]: last_match_info (expected JSArray)
1183 // esp[8]: previous index
1184 // esp[12]: subject string
1185 // esp[16]: JSRegExp object
1187 static const int kLastMatchInfoOffset = 1 * kPointerSize;
1188 static const int kPreviousIndexOffset = 2 * kPointerSize;
1189 static const int kSubjectOffset = 3 * kPointerSize;
1190 static const int kJSRegExpOffset = 4 * kPointerSize;
1193 Factory* factory = isolate()->factory();
1195 // Ensure that a RegExp stack is allocated.
1196 ExternalReference address_of_regexp_stack_memory_address =
1197 ExternalReference::address_of_regexp_stack_memory_address(isolate());
1198 ExternalReference address_of_regexp_stack_memory_size =
1199 ExternalReference::address_of_regexp_stack_memory_size(isolate());
1200 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
1202 __ j(zero, &runtime);
1204 // Check that the first argument is a JSRegExp object.
1205 __ mov(eax, Operand(esp, kJSRegExpOffset));
1206 STATIC_ASSERT(kSmiTag == 0);
1207 __ JumpIfSmi(eax, &runtime);
1208 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
1209 __ j(not_equal, &runtime);
1211 // Check that the RegExp has been compiled (data contains a fixed array).
1212 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1213 if (FLAG_debug_code) {
1214 __ test(ecx, Immediate(kSmiTagMask));
1215 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
1216 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
1217 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
1220 // ecx: RegExp data (FixedArray)
1221 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1222 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
1223 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
1224 __ j(not_equal, &runtime);
1226 // ecx: RegExp data (FixedArray)
1227 // Check that the number of captures fit in the static offsets vector buffer.
1228 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
1229 // Check (number_of_captures + 1) * 2 <= offsets vector size
1230 // Or number_of_captures * 2 <= offsets vector size - 2
1231 // Multiplying by 2 comes for free since edx is smi-tagged.
1232 STATIC_ASSERT(kSmiTag == 0);
1233 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1234 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1235 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
1236 __ j(above, &runtime);
1238 // Reset offset for possibly sliced string.
1239 __ Move(edi, Immediate(0));
1240 __ mov(eax, Operand(esp, kSubjectOffset));
1241 __ JumpIfSmi(eax, &runtime);
1242 __ mov(edx, eax); // Make a copy of the original subject string.
1243 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1244 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1246 // eax: subject string
1247 // edx: subject string
1248 // ebx: subject string instance type
1249 // ecx: RegExp data (FixedArray)
1250 // Handle subject string according to its encoding and representation:
1251 // (1) Sequential two byte? If yes, go to (9).
1252 // (2) Sequential one byte? If yes, go to (6).
1253 // (3) Anything but sequential or cons? If yes, go to (7).
1254 // (4) Cons string. If the string is flat, replace subject with first string.
1255 // Otherwise bailout.
1256 // (5a) Is subject sequential two byte? If yes, go to (9).
1257 // (5b) Is subject external? If yes, go to (8).
1258 // (6) One byte sequential. Load regexp code for one byte.
1262 // Deferred code at the end of the stub:
1263 // (7) Not a long external string? If yes, go to (10).
1264 // (8) External string. Make it, offset-wise, look like a sequential string.
1265 // (8a) Is the external string one byte? If yes, go to (6).
1266 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1267 // (10) Short external string or not a string? If yes, bail out to runtime.
1268 // (11) Sliced string. Replace subject with parent. Go to (5a).
1270 Label seq_one_byte_string /* 6 */, seq_two_byte_string /* 9 */,
1271 external_string /* 8 */, check_underlying /* 5a */,
1272 not_seq_nor_cons /* 7 */, check_code /* E */,
1273 not_long_external /* 10 */;
1275 // (1) Sequential two byte? If yes, go to (9).
1276 __ and_(ebx, kIsNotStringMask |
1277 kStringRepresentationMask |
1278 kStringEncodingMask |
1279 kShortExternalStringMask);
1280 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
1281 __ j(zero, &seq_two_byte_string); // Go to (9).
1283 // (2) Sequential one byte? If yes, go to (6).
1284 // Any other sequential string must be one byte.
1285 __ and_(ebx, Immediate(kIsNotStringMask |
1286 kStringRepresentationMask |
1287 kShortExternalStringMask));
1288 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (6).
1290 // (3) Anything but sequential or cons? If yes, go to (7).
1291 // We check whether the subject string is a cons, since sequential strings
1292 // have already been covered.
1293 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1294 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
1295 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1296 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
1297 __ cmp(ebx, Immediate(kExternalStringTag));
1298 __ j(greater_equal, ¬_seq_nor_cons); // Go to (7).
1300 // (4) Cons string. Check that it's flat.
1301 // Replace subject with first string and reload instance type.
1302 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
1303 __ j(not_equal, &runtime);
1304 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
1305 __ bind(&check_underlying);
1306 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1307 __ mov(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1309 // (5a) Is subject sequential two byte? If yes, go to (9).
1310 __ test_b(ebx, kStringRepresentationMask | kStringEncodingMask);
1311 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
1312 __ j(zero, &seq_two_byte_string); // Go to (9).
1313 // (5b) Is subject external? If yes, go to (8).
1314 __ test_b(ebx, kStringRepresentationMask);
1315 // The underlying external string is never a short external string.
1316 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
1317 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
1318 __ j(not_zero, &external_string); // Go to (8).
1320 // eax: sequential subject string (or look-alike, external string)
1321 // edx: original subject string
1322 // ecx: RegExp data (FixedArray)
1323 // (6) One byte sequential. Load regexp code for one byte.
1324 __ bind(&seq_one_byte_string);
1325 // Load previous index and check range before edx is overwritten. We have
1326 // to use edx instead of eax here because it might have been only made to
1327 // look like a sequential string when it actually is an external string.
1328 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1329 __ JumpIfNotSmi(ebx, &runtime);
1330 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1331 __ j(above_equal, &runtime);
1332 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataOneByteCodeOffset));
1333 __ Move(ecx, Immediate(1)); // Type is one byte.
1335 // (E) Carry on. String handling is done.
1336 __ bind(&check_code);
1337 // edx: irregexp code
1338 // Check that the irregexp code has been generated for the actual string
1339 // encoding. If it has, the field contains a code object otherwise it contains
1340 // a smi (code flushing support).
1341 __ JumpIfSmi(edx, &runtime);
1343 // eax: subject string
1344 // ebx: previous index (smi)
1346 // ecx: encoding of subject string (1 if one_byte, 0 if two_byte);
1347 // All checks done. Now push arguments for native regexp code.
1348 Counters* counters = isolate()->counters();
1349 __ IncrementCounter(counters->regexp_entry_native(), 1);
1351 // Isolates: note we add an additional parameter here (isolate pointer).
1352 static const int kRegExpExecuteArguments = 9;
1353 __ EnterApiExitFrame(kRegExpExecuteArguments);
1355 // Argument 9: Pass current isolate address.
1356 __ mov(Operand(esp, 8 * kPointerSize),
1357 Immediate(ExternalReference::isolate_address(isolate())));
1359 // Argument 8: Indicate that this is a direct call from JavaScript.
1360 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
1362 // Argument 7: Start (high end) of backtracking stack memory area.
1363 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
1364 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
1365 __ mov(Operand(esp, 6 * kPointerSize), esi);
1367 // Argument 6: Set the number of capture registers to zero to force global
1368 // regexps to behave as non-global. This does not affect non-global regexps.
1369 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
1371 // Argument 5: static offsets vector buffer.
1372 __ mov(Operand(esp, 4 * kPointerSize),
1373 Immediate(ExternalReference::address_of_static_offsets_vector(
1376 // Argument 2: Previous index.
1378 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1380 // Argument 1: Original subject string.
1381 // The original subject is in the previous stack frame. Therefore we have to
1382 // use ebp, which points exactly to one pointer size below the previous esp.
1383 // (Because creating a new stack frame pushes the previous ebp onto the stack
1384 // and thereby moves up esp by one kPointerSize.)
1385 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
1386 __ mov(Operand(esp, 0 * kPointerSize), esi);
1388 // esi: original subject string
1389 // eax: underlying subject string
1390 // ebx: previous index
1391 // ecx: encoding of subject string (1 if one_byte 0 if two_byte);
1393 // Argument 4: End of string data
1394 // Argument 3: Start of string data
1395 // Prepare start and end index of the input.
1396 // Load the length from the original sliced string if that is the case.
1397 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
1398 __ add(esi, edi); // Calculate input end wrt offset.
1400 __ add(ebx, edi); // Calculate input start wrt offset.
1402 // ebx: start index of the input string
1403 // esi: end index of the input string
1404 Label setup_two_byte, setup_rest;
1406 __ j(zero, &setup_two_byte, Label::kNear);
1408 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
1409 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1410 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
1411 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1412 __ jmp(&setup_rest, Label::kNear);
1414 __ bind(&setup_two_byte);
1415 STATIC_ASSERT(kSmiTag == 0);
1416 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
1417 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
1418 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1419 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
1420 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1422 __ bind(&setup_rest);
1424 // Locate the code entry and call it.
1425 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1428 // Drop arguments and come back to JS mode.
1429 __ LeaveApiExitFrame(true);
1431 // Check the result.
1434 // We expect exactly one result since we force the called regexp to behave
1436 __ j(equal, &success);
1438 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
1439 __ j(equal, &failure);
1440 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
1441 // If not exception it can only be retry. Handle that in the runtime system.
1442 __ j(not_equal, &runtime);
1443 // Result must now be exception. If there is no pending exception already a
1444 // stack overflow (on the backtrack stack) was detected in RegExp code but
1445 // haven't created the exception yet. Handle that in the runtime system.
1446 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
1447 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
1449 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1450 __ mov(eax, Operand::StaticVariable(pending_exception));
1452 __ j(equal, &runtime);
1454 // For exception, throw the exception again.
1455 __ TailCallRuntime(Runtime::kRegExpExecReThrow, 4, 1);
1458 // For failure to match, return null.
1459 __ mov(eax, factory->null_value());
1460 __ ret(4 * kPointerSize);
1462 // Load RegExp data.
1464 __ mov(eax, Operand(esp, kJSRegExpOffset));
1465 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1466 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
1467 // Calculate number of capture registers (number_of_captures + 1) * 2.
1468 STATIC_ASSERT(kSmiTag == 0);
1469 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1470 __ add(edx, Immediate(2)); // edx was a smi.
1472 // edx: Number of capture registers
1473 // Load last_match_info which is still known to be a fast case JSArray.
1474 // Check that the fourth object is a JSArray object.
1475 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1476 __ JumpIfSmi(eax, &runtime);
1477 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
1478 __ j(not_equal, &runtime);
1479 // Check that the JSArray is in fast case.
1480 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
1481 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
1482 __ cmp(eax, factory->fixed_array_map());
1483 __ j(not_equal, &runtime);
1484 // Check that the last match info has space for the capture registers and the
1485 // additional information.
1486 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
1488 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
1490 __ j(greater, &runtime);
1492 // ebx: last_match_info backing store (FixedArray)
1493 // edx: number of capture registers
1494 // Store the capture count.
1495 __ SmiTag(edx); // Number of capture registers to smi.
1496 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
1497 __ SmiUntag(edx); // Number of capture registers back from smi.
1498 // Store last subject and last input.
1499 __ mov(eax, Operand(esp, kSubjectOffset));
1501 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
1502 __ RecordWriteField(ebx,
1503 RegExpImpl::kLastSubjectOffset,
1508 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
1509 __ RecordWriteField(ebx,
1510 RegExpImpl::kLastInputOffset,
1515 // Get the static offsets vector filled by the native regexp code.
1516 ExternalReference address_of_static_offsets_vector =
1517 ExternalReference::address_of_static_offsets_vector(isolate());
1518 __ mov(ecx, Immediate(address_of_static_offsets_vector));
1520 // ebx: last_match_info backing store (FixedArray)
1521 // ecx: offsets vector
1522 // edx: number of capture registers
1523 Label next_capture, done;
1524 // Capture register counter starts from number of capture registers and
1525 // counts down until wraping after zero.
1526 __ bind(&next_capture);
1527 __ sub(edx, Immediate(1));
1528 __ j(negative, &done, Label::kNear);
1529 // Read the value from the static offsets vector buffer.
1530 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
1532 // Store the smi value in the last match info.
1533 __ mov(FieldOperand(ebx,
1536 RegExpImpl::kFirstCaptureOffset),
1538 __ jmp(&next_capture);
1541 // Return last match info.
1542 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1543 __ ret(4 * kPointerSize);
1545 // Do the runtime call to execute the regexp.
1547 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1549 // Deferred code for string handling.
1550 // (7) Not a long external string? If yes, go to (10).
1551 __ bind(¬_seq_nor_cons);
1552 // Compare flags are still set from (3).
1553 __ j(greater, ¬_long_external, Label::kNear); // Go to (10).
1555 // (8) External string. Short external strings have been ruled out.
1556 __ bind(&external_string);
1557 // Reload instance type.
1558 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1559 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1560 if (FLAG_debug_code) {
1561 // Assert that we do not have a cons or slice (indirect strings) here.
1562 // Sequential strings have already been ruled out.
1563 __ test_b(ebx, kIsIndirectStringMask);
1564 __ Assert(zero, kExternalStringExpectedButNotFound);
1566 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
1567 // Move the pointer so that offset-wise, it looks like a sequential string.
1568 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
1569 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
1570 STATIC_ASSERT(kTwoByteStringTag == 0);
1571 // (8a) Is the external string one byte? If yes, go to (6).
1572 __ test_b(ebx, kStringEncodingMask);
1573 __ j(not_zero, &seq_one_byte_string); // Goto (6).
1575 // eax: sequential subject string (or look-alike, external string)
1576 // edx: original subject string
1577 // ecx: RegExp data (FixedArray)
1578 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1579 __ bind(&seq_two_byte_string);
1580 // Load previous index and check range before edx is overwritten. We have
1581 // to use edx instead of eax here because it might have been only made to
1582 // look like a sequential string when it actually is an external string.
1583 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1584 __ JumpIfNotSmi(ebx, &runtime);
1585 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1586 __ j(above_equal, &runtime);
1587 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
1588 __ Move(ecx, Immediate(0)); // Type is two byte.
1589 __ jmp(&check_code); // Go to (E).
1591 // (10) Not a string or a short external string? If yes, bail out to runtime.
1592 __ bind(¬_long_external);
1593 // Catch non-string subject or short external string.
1594 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1595 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
1596 __ j(not_zero, &runtime);
1598 // (11) Sliced string. Replace subject with parent. Go to (5a).
1599 // Load offset into edi and replace subject string with parent.
1600 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
1601 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
1602 __ jmp(&check_underlying); // Go to (5a).
1603 #endif // V8_INTERPRETED_REGEXP
1607 static int NegativeComparisonResult(Condition cc) {
1608 DCHECK(cc != equal);
1609 DCHECK((cc == less) || (cc == less_equal)
1610 || (cc == greater) || (cc == greater_equal));
1611 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
1615 static void CheckInputType(MacroAssembler* masm, Register input,
1616 CompareICState::State expected, Label* fail) {
1618 if (expected == CompareICState::SMI) {
1619 __ JumpIfNotSmi(input, fail);
1620 } else if (expected == CompareICState::NUMBER) {
1621 __ JumpIfSmi(input, &ok);
1622 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
1623 Immediate(masm->isolate()->factory()->heap_number_map()));
1624 __ j(not_equal, fail);
1626 // We could be strict about internalized/non-internalized here, but as long as
1627 // hydrogen doesn't care, the stub doesn't have to care either.
1632 static void BranchIfNotInternalizedString(MacroAssembler* masm,
1636 __ JumpIfSmi(object, label);
1637 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
1638 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
1639 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
1640 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
1641 __ j(not_zero, label);
1645 void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
1646 Label runtime_call, check_unequal_objects;
1647 Condition cc = GetCondition();
1650 CheckInputType(masm, edx, left(), &miss);
1651 CheckInputType(masm, eax, right(), &miss);
1653 // Compare two smis.
1654 Label non_smi, smi_done;
1657 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
1658 __ sub(edx, eax); // Return on the result of the subtraction.
1659 __ j(no_overflow, &smi_done, Label::kNear);
1660 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
1666 // NOTICE! This code is only reached after a smi-fast-case check, so
1667 // it is certain that at least one operand isn't a smi.
1669 // Identical objects can be compared fast, but there are some tricky cases
1670 // for NaN and undefined.
1671 Label generic_heap_number_comparison;
1673 Label not_identical;
1675 __ j(not_equal, ¬_identical);
1678 // Check for undefined. undefined OP undefined is false even though
1679 // undefined == undefined.
1680 __ cmp(edx, isolate()->factory()->undefined_value());
1681 if (is_strong(strength())) {
1682 // In strong mode, this comparison must throw, so call the runtime.
1683 __ j(equal, &runtime_call, Label::kFar);
1685 Label check_for_nan;
1686 __ j(not_equal, &check_for_nan, Label::kNear);
1687 __ Move(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1689 __ bind(&check_for_nan);
1693 // Test for NaN. Compare heap numbers in a general way,
1694 // to hanlde NaNs correctly.
1695 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
1696 Immediate(isolate()->factory()->heap_number_map()));
1697 __ j(equal, &generic_heap_number_comparison, Label::kNear);
1699 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1700 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
1701 // Call runtime on identical JSObjects. Otherwise return equal.
1702 __ cmpb(ecx, static_cast<uint8_t>(FIRST_SPEC_OBJECT_TYPE));
1703 __ j(above_equal, &runtime_call, Label::kFar);
1704 // Call runtime on identical symbols since we need to throw a TypeError.
1705 __ cmpb(ecx, static_cast<uint8_t>(SYMBOL_TYPE));
1706 __ j(equal, &runtime_call, Label::kFar);
1707 if (is_strong(strength())) {
1708 // We have already tested for smis and heap numbers, so if both
1709 // arguments are not strings we must proceed to the slow case.
1710 __ test(ecx, Immediate(kIsNotStringMask));
1711 __ j(not_zero, &runtime_call, Label::kFar);
1714 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
1718 __ bind(¬_identical);
1721 // Strict equality can quickly decide whether objects are equal.
1722 // Non-strict object equality is slower, so it is handled later in the stub.
1723 if (cc == equal && strict()) {
1724 Label slow; // Fallthrough label.
1726 // If we're doing a strict equality comparison, we don't have to do
1727 // type conversion, so we generate code to do fast comparison for objects
1728 // and oddballs. Non-smi numbers and strings still go through the usual
1730 // If either is a Smi (we know that not both are), then they can only
1731 // be equal if the other is a HeapNumber. If so, use the slow case.
1732 STATIC_ASSERT(kSmiTag == 0);
1733 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1734 __ mov(ecx, Immediate(kSmiTagMask));
1737 __ j(not_zero, ¬_smis, Label::kNear);
1738 // One operand is a smi.
1740 // Check whether the non-smi is a heap number.
1741 STATIC_ASSERT(kSmiTagMask == 1);
1742 // ecx still holds eax & kSmiTag, which is either zero or one.
1743 __ sub(ecx, Immediate(0x01));
1746 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
1748 // if eax was smi, ebx is now edx, else eax.
1750 // Check if the non-smi operand is a heap number.
1751 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1752 Immediate(isolate()->factory()->heap_number_map()));
1753 // If heap number, handle it in the slow case.
1754 __ j(equal, &slow, Label::kNear);
1755 // Return non-equal (ebx is not zero)
1760 // If either operand is a JSObject or an oddball value, then they are not
1761 // equal since their pointers are different
1762 // There is no test for undetectability in strict equality.
1764 // Get the type of the first operand.
1765 // If the first object is a JS object, we have done pointer comparison.
1766 Label first_non_object;
1767 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
1768 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1769 __ j(below, &first_non_object, Label::kNear);
1771 // Return non-zero (eax is not zero)
1772 Label return_not_equal;
1773 STATIC_ASSERT(kHeapObjectTag != 0);
1774 __ bind(&return_not_equal);
1777 __ bind(&first_non_object);
1778 // Check for oddballs: true, false, null, undefined.
1779 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1780 __ j(equal, &return_not_equal);
1782 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ecx);
1783 __ j(above_equal, &return_not_equal);
1785 // Check for oddballs: true, false, null, undefined.
1786 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1787 __ j(equal, &return_not_equal);
1789 // Fall through to the general case.
1793 // Generate the number comparison code.
1794 Label non_number_comparison;
1796 __ bind(&generic_heap_number_comparison);
1798 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
1799 __ ucomisd(xmm0, xmm1);
1800 // Don't base result on EFLAGS when a NaN is involved.
1801 __ j(parity_even, &unordered, Label::kNear);
1803 __ mov(eax, 0); // equal
1804 __ mov(ecx, Immediate(Smi::FromInt(1)));
1805 __ cmov(above, eax, ecx);
1806 __ mov(ecx, Immediate(Smi::FromInt(-1)));
1807 __ cmov(below, eax, ecx);
1810 // If one of the numbers was NaN, then the result is always false.
1811 // The cc is never not-equal.
1812 __ bind(&unordered);
1813 DCHECK(cc != not_equal);
1814 if (cc == less || cc == less_equal) {
1815 __ mov(eax, Immediate(Smi::FromInt(1)));
1817 __ mov(eax, Immediate(Smi::FromInt(-1)));
1821 // The number comparison code did not provide a valid result.
1822 __ bind(&non_number_comparison);
1824 // Fast negative check for internalized-to-internalized equality.
1825 Label check_for_strings;
1827 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
1828 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
1830 // We've already checked for object identity, so if both operands
1831 // are internalized they aren't equal. Register eax already holds a
1832 // non-zero value, which indicates not equal, so just return.
1836 __ bind(&check_for_strings);
1838 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx,
1839 &check_unequal_objects);
1841 // Inline comparison of one-byte strings.
1843 StringHelper::GenerateFlatOneByteStringEquals(masm, edx, eax, ecx, ebx);
1845 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
1849 __ Abort(kUnexpectedFallThroughFromStringComparison);
1852 __ bind(&check_unequal_objects);
1853 if (cc == equal && !strict()) {
1854 // Non-strict equality. Objects are unequal if
1855 // they are both JSObjects and not undetectable,
1856 // and their pointers are different.
1857 Label return_unequal;
1858 // At most one is a smi, so we can test for smi by adding the two.
1859 // A smi plus a heap object has the low bit set, a heap object plus
1860 // a heap object has the low bit clear.
1861 STATIC_ASSERT(kSmiTag == 0);
1862 STATIC_ASSERT(kSmiTagMask == 1);
1863 __ lea(ecx, Operand(eax, edx, times_1, 0));
1864 __ test(ecx, Immediate(kSmiTagMask));
1865 __ j(not_zero, &runtime_call, Label::kNear);
1866 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1867 __ j(below, &runtime_call, Label::kNear);
1868 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ebx);
1869 __ j(below, &runtime_call, Label::kNear);
1870 // We do not bail out after this point. Both are JSObjects, and
1871 // they are equal if and only if both are undetectable.
1872 // The and of the undetectable flags is 1 if and only if they are equal.
1873 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
1874 1 << Map::kIsUndetectable);
1875 __ j(zero, &return_unequal, Label::kNear);
1876 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
1877 1 << Map::kIsUndetectable);
1878 __ j(zero, &return_unequal, Label::kNear);
1879 // The objects are both undetectable, so they both compare as the value
1880 // undefined, and are equal.
1881 __ Move(eax, Immediate(EQUAL));
1882 __ bind(&return_unequal);
1883 // Return non-equal by returning the non-zero object pointer in eax,
1884 // or return equal if we fell through to here.
1885 __ ret(0); // rax, rdx were pushed
1887 __ bind(&runtime_call);
1889 // Push arguments below the return address.
1894 // Figure out which native to call and setup the arguments.
1895 Builtins::JavaScript builtin;
1897 builtin = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1900 is_strong(strength()) ? Builtins::COMPARE_STRONG : Builtins::COMPARE;
1901 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1904 // Restore return address on the stack.
1907 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1908 // tagged as a small integer.
1909 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
1916 static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
1917 // eax : number of arguments to the construct function
1918 // ebx : Feedback vector
1919 // edx : slot in feedback vector (Smi)
1920 // edi : the function to call
1921 FrameScope scope(masm, StackFrame::INTERNAL);
1923 // Number-of-arguments register must be smi-tagged to call out.
1940 static void GenerateRecordCallTarget(MacroAssembler* masm) {
1941 // Cache the called function in a feedback vector slot. Cache states
1942 // are uninitialized, monomorphic (indicated by a JSFunction), and
1944 // eax : number of arguments to the construct function
1945 // ebx : Feedback vector
1946 // edx : slot in feedback vector (Smi)
1947 // edi : the function to call
1948 Isolate* isolate = masm->isolate();
1949 Label initialize, done, miss, megamorphic, not_array_function;
1951 // Load the cache state into ecx.
1952 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1953 FixedArray::kHeaderSize));
1955 // A monomorphic cache hit or an already megamorphic state: invoke the
1956 // function without changing the state.
1957 // We don't know if ecx is a WeakCell or a Symbol, but it's harmless to read
1958 // at this position in a symbol (see static asserts in
1959 // type-feedback-vector.h).
1960 Label check_allocation_site;
1961 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
1962 __ j(equal, &done, Label::kFar);
1963 __ CompareRoot(ecx, Heap::kmegamorphic_symbolRootIndex);
1964 __ j(equal, &done, Label::kFar);
1965 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1966 Heap::kWeakCellMapRootIndex);
1967 __ j(not_equal, FLAG_pretenuring_call_new ? &miss : &check_allocation_site);
1969 // If the weak cell is cleared, we have a new chance to become monomorphic.
1970 __ JumpIfSmi(FieldOperand(ecx, WeakCell::kValueOffset), &initialize);
1971 __ jmp(&megamorphic);
1973 if (!FLAG_pretenuring_call_new) {
1974 __ bind(&check_allocation_site);
1975 // If we came here, we need to see if we are the array function.
1976 // If we didn't have a matching function, and we didn't find the megamorph
1977 // sentinel, then we have in the slot either some other function or an
1979 __ CompareRoot(FieldOperand(ecx, 0), Heap::kAllocationSiteMapRootIndex);
1980 __ j(not_equal, &miss);
1982 // Make sure the function is the Array() function
1983 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1985 __ j(not_equal, &megamorphic);
1986 __ jmp(&done, Label::kFar);
1991 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1993 __ CompareRoot(ecx, Heap::kuninitialized_symbolRootIndex);
1994 __ j(equal, &initialize);
1995 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1996 // write-barrier is needed.
1997 __ bind(&megamorphic);
1999 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
2000 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2001 __ jmp(&done, Label::kFar);
2003 // An uninitialized cache is patched with the function or sentinel to
2004 // indicate the ElementsKind if function is the Array constructor.
2005 __ bind(&initialize);
2006 if (!FLAG_pretenuring_call_new) {
2007 // Make sure the function is the Array() function
2008 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2010 __ j(not_equal, ¬_array_function);
2012 // The target function is the Array constructor,
2013 // Create an AllocationSite if we don't already have it, store it in the
2015 CreateAllocationSiteStub create_stub(isolate);
2016 CallStubInRecordCallTarget(masm, &create_stub);
2019 __ bind(¬_array_function);
2022 CreateWeakCellStub create_stub(isolate);
2023 CallStubInRecordCallTarget(masm, &create_stub);
2028 static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
2029 // Do not transform the receiver for strict mode functions.
2030 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2031 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset),
2032 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
2033 __ j(not_equal, cont);
2035 // Do not transform the receiver for natives (shared already in ecx).
2036 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset),
2037 1 << SharedFunctionInfo::kNativeBitWithinByte);
2038 __ j(not_equal, cont);
2042 static void EmitSlowCase(Isolate* isolate,
2043 MacroAssembler* masm,
2045 Label* non_function) {
2046 // Check for function proxy.
2047 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
2048 __ j(not_equal, non_function);
2050 __ push(edi); // put proxy as additional argument under return address
2052 __ Move(eax, Immediate(argc + 1));
2053 __ Move(ebx, Immediate(0));
2054 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
2056 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
2057 __ jmp(adaptor, RelocInfo::CODE_TARGET);
2060 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
2061 // of the original receiver from the call site).
2062 __ bind(non_function);
2063 __ mov(Operand(esp, (argc + 1) * kPointerSize), edi);
2064 __ Move(eax, Immediate(argc));
2065 __ Move(ebx, Immediate(0));
2066 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
2067 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
2068 __ jmp(adaptor, RelocInfo::CODE_TARGET);
2072 static void EmitWrapCase(MacroAssembler* masm, int argc, Label* cont) {
2073 // Wrap the receiver and patch it back onto the stack.
2074 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
2077 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
2080 __ mov(Operand(esp, (argc + 1) * kPointerSize), eax);
2085 static void CallFunctionNoFeedback(MacroAssembler* masm,
2086 int argc, bool needs_checks,
2087 bool call_as_method) {
2088 // edi : the function to call
2089 Label slow, non_function, wrap, cont;
2092 // Check that the function really is a JavaScript function.
2093 __ JumpIfSmi(edi, &non_function);
2095 // Goto slow case if we do not have a function.
2096 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2097 __ j(not_equal, &slow);
2100 // Fast-case: Just invoke the function.
2101 ParameterCount actual(argc);
2103 if (call_as_method) {
2105 EmitContinueIfStrictOrNative(masm, &cont);
2108 // Load the receiver from the stack.
2109 __ mov(eax, Operand(esp, (argc + 1) * kPointerSize));
2112 __ JumpIfSmi(eax, &wrap);
2114 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
2123 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
2126 // Slow-case: Non-function called.
2128 // (non_function is bound in EmitSlowCase)
2129 EmitSlowCase(masm->isolate(), masm, argc, &non_function);
2132 if (call_as_method) {
2134 EmitWrapCase(masm, argc, &cont);
2139 void CallFunctionStub::Generate(MacroAssembler* masm) {
2140 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
2144 void CallConstructStub::Generate(MacroAssembler* masm) {
2145 // eax : number of arguments
2146 // ebx : feedback vector
2147 // ecx : original constructor (for IsSuperConstructorCall)
2148 // edx : slot in feedback vector (Smi, for RecordCallTarget)
2149 // edi : constructor function
2150 Label slow, non_function_call;
2152 if (IsSuperConstructorCall()) {
2156 // Check that function is not a smi.
2157 __ JumpIfSmi(edi, &non_function_call);
2158 // Check that function is a JSFunction.
2159 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2160 __ j(not_equal, &slow);
2162 if (RecordCallTarget()) {
2163 GenerateRecordCallTarget(masm);
2165 if (FLAG_pretenuring_call_new) {
2166 // Put the AllocationSite from the feedback vector into ebx.
2167 // By adding kPointerSize we encode that we know the AllocationSite
2168 // entry is at the feedback vector slot given by edx + 1.
2169 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
2170 FixedArray::kHeaderSize + kPointerSize));
2172 Label feedback_register_initialized;
2173 // Put the AllocationSite from the feedback vector into ebx, or undefined.
2174 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
2175 FixedArray::kHeaderSize));
2176 Handle<Map> allocation_site_map =
2177 isolate()->factory()->allocation_site_map();
2178 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
2179 __ j(equal, &feedback_register_initialized);
2180 __ mov(ebx, isolate()->factory()->undefined_value());
2181 __ bind(&feedback_register_initialized);
2184 __ AssertUndefinedOrAllocationSite(ebx);
2187 if (IsSuperConstructorCall()) {
2190 // Pass original constructor to construct stub.
2194 // Jump to the function-specific construct stub.
2195 Register jmp_reg = ecx;
2196 __ mov(jmp_reg, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2197 __ mov(jmp_reg, FieldOperand(jmp_reg,
2198 SharedFunctionInfo::kConstructStubOffset));
2199 __ lea(jmp_reg, FieldOperand(jmp_reg, Code::kHeaderSize));
2202 // edi: called object
2203 // eax: number of arguments
2205 // esp[0]: original receiver
2208 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
2209 __ j(not_equal, &non_function_call);
2210 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
2213 __ bind(&non_function_call);
2214 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
2216 if (IsSuperConstructorCall()) {
2219 // Set expected number of arguments to zero (not changing eax).
2220 __ Move(ebx, Immediate(0));
2221 Handle<Code> arguments_adaptor =
2222 isolate()->builtins()->ArgumentsAdaptorTrampoline();
2223 __ jmp(arguments_adaptor, RelocInfo::CODE_TARGET);
2227 static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
2228 __ mov(vector, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
2229 __ mov(vector, FieldOperand(vector, JSFunction::kSharedFunctionInfoOffset));
2230 __ mov(vector, FieldOperand(vector,
2231 SharedFunctionInfo::kFeedbackVectorOffset));
2235 void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
2240 int argc = arg_count();
2241 ParameterCount actual(argc);
2243 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2245 __ j(not_equal, &miss);
2247 __ mov(eax, arg_count());
2248 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
2249 FixedArray::kHeaderSize));
2251 // Verify that ecx contains an AllocationSite
2252 Factory* factory = masm->isolate()->factory();
2253 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
2254 factory->allocation_site_map());
2255 __ j(not_equal, &miss);
2257 // Increment the call count for monomorphic function calls.
2258 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
2259 FixedArray::kHeaderSize + kPointerSize),
2260 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2264 ArrayConstructorStub stub(masm->isolate(), arg_count());
2265 __ TailCallStub(&stub);
2270 // The slow case, we need this no matter what to complete a call after a miss.
2271 CallFunctionNoFeedback(masm,
2281 void CallICStub::Generate(MacroAssembler* masm) {
2285 Isolate* isolate = masm->isolate();
2286 const int with_types_offset =
2287 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kWithTypesIndex);
2288 const int generic_offset =
2289 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kGenericCountIndex);
2290 Label extra_checks_or_miss, slow_start;
2291 Label slow, non_function, wrap, cont;
2292 Label have_js_function;
2293 int argc = arg_count();
2294 ParameterCount actual(argc);
2296 // The checks. First, does edi match the recorded monomorphic target?
2297 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
2298 FixedArray::kHeaderSize));
2300 // We don't know that we have a weak cell. We might have a private symbol
2301 // or an AllocationSite, but the memory is safe to examine.
2302 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
2304 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
2305 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
2306 // computed, meaning that it can't appear to be a pointer. If the low bit is
2307 // 0, then hash is computed, but the 0 bit prevents the field from appearing
2309 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
2310 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
2311 WeakCell::kValueOffset &&
2312 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
2314 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
2315 __ j(not_equal, &extra_checks_or_miss);
2317 // The compare above could have been a SMI/SMI comparison. Guard against this
2318 // convincing us that we have a monomorphic JSFunction.
2319 __ JumpIfSmi(edi, &extra_checks_or_miss);
2321 // Increment the call count for monomorphic function calls.
2322 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
2323 FixedArray::kHeaderSize + kPointerSize),
2324 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2326 __ bind(&have_js_function);
2327 if (CallAsMethod()) {
2328 EmitContinueIfStrictOrNative(masm, &cont);
2330 // Load the receiver from the stack.
2331 __ mov(eax, Operand(esp, (argc + 1) * kPointerSize));
2333 __ JumpIfSmi(eax, &wrap);
2335 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
2341 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
2344 EmitSlowCase(isolate, masm, argc, &non_function);
2346 if (CallAsMethod()) {
2348 EmitWrapCase(masm, argc, &cont);
2351 __ bind(&extra_checks_or_miss);
2352 Label uninitialized, miss;
2354 __ cmp(ecx, Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2355 __ j(equal, &slow_start);
2357 // The following cases attempt to handle MISS cases without going to the
2359 if (FLAG_trace_ic) {
2363 __ cmp(ecx, Immediate(TypeFeedbackVector::UninitializedSentinel(isolate)));
2364 __ j(equal, &uninitialized);
2366 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2367 // to handle it here. More complex cases are dealt with in the runtime.
2368 __ AssertNotSmi(ecx);
2369 __ CmpObjectType(ecx, JS_FUNCTION_TYPE, ecx);
2370 __ j(not_equal, &miss);
2372 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
2373 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2374 // We have to update statistics for runtime profiling.
2375 __ sub(FieldOperand(ebx, with_types_offset), Immediate(Smi::FromInt(1)));
2376 __ add(FieldOperand(ebx, generic_offset), Immediate(Smi::FromInt(1)));
2377 __ jmp(&slow_start);
2379 __ bind(&uninitialized);
2381 // We are going monomorphic, provided we actually have a JSFunction.
2382 __ JumpIfSmi(edi, &miss);
2384 // Goto miss case if we do not have a function.
2385 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2386 __ j(not_equal, &miss);
2388 // Make sure the function is not the Array() function, which requires special
2389 // behavior on MISS.
2390 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2395 __ add(FieldOperand(ebx, with_types_offset), Immediate(Smi::FromInt(1)));
2397 // Initialize the call counter.
2398 __ mov(FieldOperand(ebx, edx, times_half_pointer_size,
2399 FixedArray::kHeaderSize + kPointerSize),
2400 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2402 // Store the function. Use a stub since we need a frame for allocation.
2407 FrameScope scope(masm, StackFrame::INTERNAL);
2408 CreateWeakCellStub create_stub(isolate);
2410 __ CallStub(&create_stub);
2414 __ jmp(&have_js_function);
2416 // We are here because tracing is on or we encountered a MISS case we can't
2422 __ bind(&slow_start);
2424 // Check that the function really is a JavaScript function.
2425 __ JumpIfSmi(edi, &non_function);
2427 // Goto slow case if we do not have a function.
2428 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2429 __ j(not_equal, &slow);
2430 __ jmp(&have_js_function);
2437 void CallICStub::GenerateMiss(MacroAssembler* masm) {
2438 FrameScope scope(masm, StackFrame::INTERNAL);
2440 // Push the function and feedback info.
2446 IC::UtilityId id = GetICState() == DEFAULT ? IC::kCallIC_Miss
2447 : IC::kCallIC_Customization_Miss;
2449 ExternalReference miss = ExternalReference(IC_Utility(id), masm->isolate());
2450 __ CallExternalReference(miss, 3);
2452 // Move result to edi and exit the internal frame.
2457 bool CEntryStub::NeedsImmovableCode() {
2462 void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
2463 CEntryStub::GenerateAheadOfTime(isolate);
2464 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
2465 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
2466 // It is important that the store buffer overflow stubs are generated first.
2467 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
2468 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
2469 CreateWeakCellStub::GenerateAheadOfTime(isolate);
2470 BinaryOpICStub::GenerateAheadOfTime(isolate);
2471 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
2472 StoreFastElementStub::GenerateAheadOfTime(isolate);
2473 TypeofStub::GenerateAheadOfTime(isolate);
2477 void CodeStub::GenerateFPStubs(Isolate* isolate) {
2478 // Generate if not already in cache.
2479 CEntryStub(isolate, 1, kSaveFPRegs).GetCode();
2480 isolate->set_fp_stubs_generated(true);
2484 void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
2485 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
2490 void CEntryStub::Generate(MacroAssembler* masm) {
2491 // eax: number of arguments including receiver
2492 // ebx: pointer to C function (C callee-saved)
2493 // ebp: frame pointer (restored after C call)
2494 // esp: stack pointer (restored after C call)
2495 // esi: current context (C callee-saved)
2496 // edi: JS function of the caller (C callee-saved)
2498 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2500 // Enter the exit frame that transitions from JavaScript to C++.
2501 __ EnterExitFrame(save_doubles());
2503 // ebx: pointer to C function (C callee-saved)
2504 // ebp: frame pointer (restored after C call)
2505 // esp: stack pointer (restored after C call)
2506 // edi: number of arguments including receiver (C callee-saved)
2507 // esi: pointer to the first argument (C callee-saved)
2509 // Result returned in eax, or eax+edx if result size is 2.
2511 // Check stack alignment.
2512 if (FLAG_debug_code) {
2513 __ CheckStackAlignment();
2517 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
2518 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
2519 __ mov(Operand(esp, 2 * kPointerSize),
2520 Immediate(ExternalReference::isolate_address(isolate())));
2522 // Result is in eax or edx:eax - do not destroy these registers!
2524 // Check result for exception sentinel.
2525 Label exception_returned;
2526 __ cmp(eax, isolate()->factory()->exception());
2527 __ j(equal, &exception_returned);
2529 // Check that there is no pending exception, otherwise we
2530 // should have returned the exception sentinel.
2531 if (FLAG_debug_code) {
2533 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
2535 ExternalReference pending_exception_address(
2536 Isolate::kPendingExceptionAddress, isolate());
2537 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
2538 // Cannot use check here as it attempts to generate call into runtime.
2539 __ j(equal, &okay, Label::kNear);
2545 // Exit the JavaScript to C++ exit frame.
2546 __ LeaveExitFrame(save_doubles());
2549 // Handling of exception.
2550 __ bind(&exception_returned);
2552 ExternalReference pending_handler_context_address(
2553 Isolate::kPendingHandlerContextAddress, isolate());
2554 ExternalReference pending_handler_code_address(
2555 Isolate::kPendingHandlerCodeAddress, isolate());
2556 ExternalReference pending_handler_offset_address(
2557 Isolate::kPendingHandlerOffsetAddress, isolate());
2558 ExternalReference pending_handler_fp_address(
2559 Isolate::kPendingHandlerFPAddress, isolate());
2560 ExternalReference pending_handler_sp_address(
2561 Isolate::kPendingHandlerSPAddress, isolate());
2563 // Ask the runtime for help to determine the handler. This will set eax to
2564 // contain the current pending exception, don't clobber it.
2565 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
2568 FrameScope scope(masm, StackFrame::MANUAL);
2569 __ PrepareCallCFunction(3, eax);
2570 __ mov(Operand(esp, 0 * kPointerSize), Immediate(0)); // argc.
2571 __ mov(Operand(esp, 1 * kPointerSize), Immediate(0)); // argv.
2572 __ mov(Operand(esp, 2 * kPointerSize),
2573 Immediate(ExternalReference::isolate_address(isolate())));
2574 __ CallCFunction(find_handler, 3);
2577 // Retrieve the handler context, SP and FP.
2578 __ mov(esi, Operand::StaticVariable(pending_handler_context_address));
2579 __ mov(esp, Operand::StaticVariable(pending_handler_sp_address));
2580 __ mov(ebp, Operand::StaticVariable(pending_handler_fp_address));
2582 // If the handler is a JS frame, restore the context to the frame. Note that
2583 // the context will be set to (esi == 0) for non-JS frames.
2586 __ j(zero, &skip, Label::kNear);
2587 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
2590 // Compute the handler entry address and jump to it.
2591 __ mov(edi, Operand::StaticVariable(pending_handler_code_address));
2592 __ mov(edx, Operand::StaticVariable(pending_handler_offset_address));
2593 __ lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
2598 void JSEntryStub::Generate(MacroAssembler* masm) {
2599 Label invoke, handler_entry, exit;
2600 Label not_outermost_js, not_outermost_js_2;
2602 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2608 // Push marker in two places.
2609 int marker = type();
2610 __ push(Immediate(Smi::FromInt(marker))); // context slot
2611 __ push(Immediate(Smi::FromInt(marker))); // function slot
2612 // Save callee-saved registers (C calling conventions).
2617 // Save copies of the top frame descriptor on the stack.
2618 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
2619 __ push(Operand::StaticVariable(c_entry_fp));
2621 // If this is the outermost JS call, set js_entry_sp value.
2622 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
2623 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
2624 __ j(not_equal, ¬_outermost_js, Label::kNear);
2625 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
2626 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
2627 __ jmp(&invoke, Label::kNear);
2628 __ bind(¬_outermost_js);
2629 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
2631 // Jump to a faked try block that does the invoke, with a faked catch
2632 // block that sets the pending exception.
2634 __ bind(&handler_entry);
2635 handler_offset_ = handler_entry.pos();
2636 // Caught exception: Store result (exception) in the pending exception
2637 // field in the JSEnv and return a failure sentinel.
2638 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
2640 __ mov(Operand::StaticVariable(pending_exception), eax);
2641 __ mov(eax, Immediate(isolate()->factory()->exception()));
2644 // Invoke: Link this frame into the handler chain.
2646 __ PushStackHandler();
2648 // Clear any pending exceptions.
2649 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
2650 __ mov(Operand::StaticVariable(pending_exception), edx);
2652 // Fake a receiver (NULL).
2653 __ push(Immediate(0)); // receiver
2655 // Invoke the function by calling through JS entry trampoline builtin and
2656 // pop the faked function when we return. Notice that we cannot store a
2657 // reference to the trampoline code directly in this stub, because the
2658 // builtin stubs may not have been generated yet.
2659 if (type() == StackFrame::ENTRY_CONSTRUCT) {
2660 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
2662 __ mov(edx, Immediate(construct_entry));
2664 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
2665 __ mov(edx, Immediate(entry));
2667 __ mov(edx, Operand(edx, 0)); // deref address
2668 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
2671 // Unlink this frame from the handler chain.
2672 __ PopStackHandler();
2675 // Check if the current stack frame is marked as the outermost JS frame.
2677 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
2678 __ j(not_equal, ¬_outermost_js_2);
2679 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
2680 __ bind(¬_outermost_js_2);
2682 // Restore the top frame descriptor from the stack.
2683 __ pop(Operand::StaticVariable(ExternalReference(
2684 Isolate::kCEntryFPAddress, isolate())));
2686 // Restore callee-saved registers (C calling conventions).
2690 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
2692 // Restore frame pointer and return.
2698 // Generate stub code for instanceof.
2699 // This code can patch a call site inlined cache of the instance of check,
2700 // which looks like this.
2702 // 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
2703 // 75 0a jne <some near label>
2704 // b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
2706 // If call site patching is requested the stack will have the delta from the
2707 // return address to the cmp instruction just below the return address. This
2708 // also means that call site patching can only take place with arguments in
2709 // registers. TOS looks like this when call site patching is requested
2711 // esp[0] : return address
2712 // esp[4] : delta from return address to cmp instruction
2714 void InstanceofStub::Generate(MacroAssembler* masm) {
2715 // Call site inlining and patching implies arguments in registers.
2716 DCHECK(HasArgsInRegisters() || !HasCallSiteInlineCheck());
2718 // Fixed register usage throughout the stub.
2719 Register object = eax; // Object (lhs).
2720 Register map = ebx; // Map of the object.
2721 Register function = edx; // Function (rhs).
2722 Register prototype = edi; // Prototype of the function.
2723 Register scratch = ecx;
2725 // Constants describing the call site code to patch.
2726 static const int kDeltaToCmpImmediate = 2;
2727 static const int kDeltaToMov = 8;
2728 static const int kDeltaToMovImmediate = 9;
2729 static const int8_t kCmpEdiOperandByte1 = bit_cast<int8_t, uint8_t>(0x3b);
2730 static const int8_t kCmpEdiOperandByte2 = bit_cast<int8_t, uint8_t>(0x3d);
2731 static const int8_t kMovEaxImmediateByte = bit_cast<int8_t, uint8_t>(0xb8);
2733 DCHECK_EQ(object.code(), InstanceofStub::left().code());
2734 DCHECK_EQ(function.code(), InstanceofStub::right().code());
2736 // Get the object and function - they are always both needed.
2737 Label slow, not_js_object;
2738 if (!HasArgsInRegisters()) {
2739 __ mov(object, Operand(esp, 2 * kPointerSize));
2740 __ mov(function, Operand(esp, 1 * kPointerSize));
2743 // Check that the left hand is a JS object.
2744 __ JumpIfSmi(object, ¬_js_object);
2745 __ IsObjectJSObjectType(object, map, scratch, ¬_js_object);
2747 // If there is a call site cache don't look in the global cache, but do the
2748 // real lookup and update the call site cache.
2749 if (!HasCallSiteInlineCheck() && !ReturnTrueFalseObject()) {
2750 // Look up the function and the map in the instanceof cache.
2752 __ CompareRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2753 __ j(not_equal, &miss, Label::kNear);
2754 __ CompareRoot(map, scratch, Heap::kInstanceofCacheMapRootIndex);
2755 __ j(not_equal, &miss, Label::kNear);
2756 __ LoadRoot(eax, Heap::kInstanceofCacheAnswerRootIndex);
2757 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2761 // Get the prototype of the function.
2762 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
2764 // Check that the function prototype is a JS object.
2765 __ JumpIfSmi(prototype, &slow);
2766 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
2768 // Update the global instanceof or call site inlined cache with the current
2769 // map and function. The cached answer will be set when it is known below.
2770 if (!HasCallSiteInlineCheck()) {
2771 __ StoreRoot(map, scratch, Heap::kInstanceofCacheMapRootIndex);
2772 __ StoreRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2774 // The constants for the code patching are based on no push instructions
2775 // at the call site.
2776 DCHECK(HasArgsInRegisters());
2777 // Get return address and delta to inlined map check.
2778 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2779 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2780 if (FLAG_debug_code) {
2781 __ cmpb(Operand(scratch, 0), kCmpEdiOperandByte1);
2782 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCmp1);
2783 __ cmpb(Operand(scratch, 1), kCmpEdiOperandByte2);
2784 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCmp2);
2786 __ mov(scratch, Operand(scratch, kDeltaToCmpImmediate));
2787 __ mov(Operand(scratch, 0), map);
2789 // Scratch points at the cell payload. Calculate the start of the object.
2790 __ sub(scratch, Immediate(Cell::kValueOffset - 1));
2791 __ RecordWriteField(scratch, Cell::kValueOffset, map, function,
2792 kDontSaveFPRegs, OMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2796 // Loop through the prototype chain of the object looking for the function
2798 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
2799 Label loop, is_instance, is_not_instance;
2801 __ cmp(scratch, prototype);
2802 __ j(equal, &is_instance, Label::kNear);
2803 Factory* factory = isolate()->factory();
2804 __ cmp(scratch, Immediate(factory->null_value()));
2805 __ j(equal, &is_not_instance, Label::kNear);
2806 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
2807 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
2810 __ bind(&is_instance);
2811 if (!HasCallSiteInlineCheck()) {
2812 __ mov(eax, Immediate(0));
2813 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
2814 if (ReturnTrueFalseObject()) {
2815 __ mov(eax, factory->true_value());
2818 // Get return address and delta to inlined map check.
2819 __ mov(eax, factory->true_value());
2820 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2821 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2822 if (FLAG_debug_code) {
2823 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
2824 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2826 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
2827 if (!ReturnTrueFalseObject()) {
2828 __ Move(eax, Immediate(0));
2831 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2833 __ bind(&is_not_instance);
2834 if (!HasCallSiteInlineCheck()) {
2835 __ mov(eax, Immediate(Smi::FromInt(1)));
2836 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
2837 if (ReturnTrueFalseObject()) {
2838 __ mov(eax, factory->false_value());
2841 // Get return address and delta to inlined map check.
2842 __ mov(eax, factory->false_value());
2843 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2844 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2845 if (FLAG_debug_code) {
2846 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
2847 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2849 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
2850 if (!ReturnTrueFalseObject()) {
2851 __ Move(eax, Immediate(Smi::FromInt(1)));
2854 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2856 Label object_not_null, object_not_null_or_smi;
2857 __ bind(¬_js_object);
2858 // Before null, smi and string value checks, check that the rhs is a function
2859 // as for a non-function rhs an exception needs to be thrown.
2860 __ JumpIfSmi(function, &slow, Label::kNear);
2861 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
2862 __ j(not_equal, &slow, Label::kNear);
2864 // Null is not instance of anything.
2865 __ cmp(object, factory->null_value());
2866 __ j(not_equal, &object_not_null, Label::kNear);
2867 if (ReturnTrueFalseObject()) {
2868 __ mov(eax, factory->false_value());
2870 __ Move(eax, Immediate(Smi::FromInt(1)));
2872 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2874 __ bind(&object_not_null);
2875 // Smi values is not instance of anything.
2876 __ JumpIfNotSmi(object, &object_not_null_or_smi, Label::kNear);
2877 if (ReturnTrueFalseObject()) {
2878 __ mov(eax, factory->false_value());
2880 __ Move(eax, Immediate(Smi::FromInt(1)));
2882 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2884 __ bind(&object_not_null_or_smi);
2885 // String values is not instance of anything.
2886 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
2887 __ j(NegateCondition(is_string), &slow, Label::kNear);
2888 if (ReturnTrueFalseObject()) {
2889 __ mov(eax, factory->false_value());
2891 __ Move(eax, Immediate(Smi::FromInt(1)));
2893 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2895 // Slow-case: Go through the JavaScript implementation.
2897 if (!ReturnTrueFalseObject()) {
2898 // Tail call the builtin which returns 0 or 1.
2899 if (HasArgsInRegisters()) {
2900 // Push arguments below return address.
2906 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
2908 // Call the builtin and convert 0/1 to true/false.
2910 FrameScope scope(masm, StackFrame::INTERNAL);
2913 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
2915 Label true_value, done;
2917 __ j(zero, &true_value, Label::kNear);
2918 __ mov(eax, factory->false_value());
2919 __ jmp(&done, Label::kNear);
2920 __ bind(&true_value);
2921 __ mov(eax, factory->true_value());
2923 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2928 // -------------------------------------------------------------------------
2929 // StringCharCodeAtGenerator
2931 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
2932 // If the receiver is a smi trigger the non-string case.
2933 STATIC_ASSERT(kSmiTag == 0);
2934 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2935 __ JumpIfSmi(object_, receiver_not_string_);
2937 // Fetch the instance type of the receiver into result register.
2938 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
2939 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2940 // If the receiver is not a string trigger the non-string case.
2941 __ test(result_, Immediate(kIsNotStringMask));
2942 __ j(not_zero, receiver_not_string_);
2945 // If the index is non-smi trigger the non-smi case.
2946 STATIC_ASSERT(kSmiTag == 0);
2947 __ JumpIfNotSmi(index_, &index_not_smi_);
2948 __ bind(&got_smi_index_);
2950 // Check for index out of range.
2951 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
2952 __ j(above_equal, index_out_of_range_);
2954 __ SmiUntag(index_);
2956 Factory* factory = masm->isolate()->factory();
2957 StringCharLoadGenerator::Generate(
2958 masm, factory, object_, index_, result_, &call_runtime_);
2965 void StringCharCodeAtGenerator::GenerateSlow(
2966 MacroAssembler* masm, EmbedMode embed_mode,
2967 const RuntimeCallHelper& call_helper) {
2968 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
2970 // Index is not a smi.
2971 __ bind(&index_not_smi_);
2972 // If index is a heap number, try converting it to an integer.
2974 masm->isolate()->factory()->heap_number_map(),
2977 call_helper.BeforeCall(masm);
2978 if (embed_mode == PART_OF_IC_HANDLER) {
2979 __ push(LoadWithVectorDescriptor::VectorRegister());
2980 __ push(LoadDescriptor::SlotRegister());
2983 __ push(index_); // Consumed by runtime conversion function.
2984 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
2985 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
2987 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
2988 // NumberToSmi discards numbers that are not exact integers.
2989 __ CallRuntime(Runtime::kNumberToSmi, 1);
2991 if (!index_.is(eax)) {
2992 // Save the conversion result before the pop instructions below
2993 // have a chance to overwrite it.
2994 __ mov(index_, eax);
2997 if (embed_mode == PART_OF_IC_HANDLER) {
2998 __ pop(LoadDescriptor::SlotRegister());
2999 __ pop(LoadWithVectorDescriptor::VectorRegister());
3001 // Reload the instance type.
3002 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3003 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3004 call_helper.AfterCall(masm);
3005 // If index is still not a smi, it must be out of range.
3006 STATIC_ASSERT(kSmiTag == 0);
3007 __ JumpIfNotSmi(index_, index_out_of_range_);
3008 // Otherwise, return to the fast path.
3009 __ jmp(&got_smi_index_);
3011 // Call runtime. We get here when the receiver is a string and the
3012 // index is a number, but the code of getting the actual character
3013 // is too complex (e.g., when the string needs to be flattened).
3014 __ bind(&call_runtime_);
3015 call_helper.BeforeCall(masm);
3019 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
3020 if (!result_.is(eax)) {
3021 __ mov(result_, eax);
3023 call_helper.AfterCall(masm);
3026 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
3030 // -------------------------------------------------------------------------
3031 // StringCharFromCodeGenerator
3033 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3034 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3035 STATIC_ASSERT(kSmiTag == 0);
3036 STATIC_ASSERT(kSmiShiftSize == 0);
3037 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
3038 __ test(code_, Immediate(kSmiTagMask |
3039 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
3040 __ j(not_zero, &slow_case_);
3042 Factory* factory = masm->isolate()->factory();
3043 __ Move(result_, Immediate(factory->single_character_string_cache()));
3044 STATIC_ASSERT(kSmiTag == 0);
3045 STATIC_ASSERT(kSmiTagSize == 1);
3046 STATIC_ASSERT(kSmiShiftSize == 0);
3047 // At this point code register contains smi tagged one byte char code.
3048 __ mov(result_, FieldOperand(result_,
3049 code_, times_half_pointer_size,
3050 FixedArray::kHeaderSize));
3051 __ cmp(result_, factory->undefined_value());
3052 __ j(equal, &slow_case_);
3057 void StringCharFromCodeGenerator::GenerateSlow(
3058 MacroAssembler* masm,
3059 const RuntimeCallHelper& call_helper) {
3060 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
3062 __ bind(&slow_case_);
3063 call_helper.BeforeCall(masm);
3065 __ CallRuntime(Runtime::kCharFromCode, 1);
3066 if (!result_.is(eax)) {
3067 __ mov(result_, eax);
3069 call_helper.AfterCall(masm);
3072 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
3076 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3081 String::Encoding encoding) {
3082 DCHECK(!scratch.is(dest));
3083 DCHECK(!scratch.is(src));
3084 DCHECK(!scratch.is(count));
3086 // Nothing to do for zero characters.
3088 __ test(count, count);
3091 // Make count the number of bytes to copy.
3092 if (encoding == String::TWO_BYTE_ENCODING) {
3098 __ mov_b(scratch, Operand(src, 0));
3099 __ mov_b(Operand(dest, 0), scratch);
3103 __ j(not_zero, &loop);
3109 void SubStringStub::Generate(MacroAssembler* masm) {
3112 // Stack frame on entry.
3113 // esp[0]: return address
3118 // Make sure first argument is a string.
3119 __ mov(eax, Operand(esp, 3 * kPointerSize));
3120 STATIC_ASSERT(kSmiTag == 0);
3121 __ JumpIfSmi(eax, &runtime);
3122 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3123 __ j(NegateCondition(is_string), &runtime);
3126 // ebx: instance type
3128 // Calculate length of sub string using the smi values.
3129 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
3130 __ JumpIfNotSmi(ecx, &runtime);
3131 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
3132 __ JumpIfNotSmi(edx, &runtime);
3134 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
3135 Label not_original_string;
3136 // Shorter than original string's length: an actual substring.
3137 __ j(below, ¬_original_string, Label::kNear);
3138 // Longer than original string's length or negative: unsafe arguments.
3139 __ j(above, &runtime);
3140 // Return original string.
3141 Counters* counters = isolate()->counters();
3142 __ IncrementCounter(counters->sub_string_native(), 1);
3143 __ ret(3 * kPointerSize);
3144 __ bind(¬_original_string);
3147 __ cmp(ecx, Immediate(Smi::FromInt(1)));
3148 __ j(equal, &single_char);
3151 // ebx: instance type
3152 // ecx: sub string length (smi)
3153 // edx: from index (smi)
3154 // Deal with different string types: update the index if necessary
3155 // and put the underlying string into edi.
3156 Label underlying_unpacked, sliced_string, seq_or_external_string;
3157 // If the string is not indirect, it can only be sequential or external.
3158 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3159 STATIC_ASSERT(kIsIndirectStringMask != 0);
3160 __ test(ebx, Immediate(kIsIndirectStringMask));
3161 __ j(zero, &seq_or_external_string, Label::kNear);
3163 Factory* factory = isolate()->factory();
3164 __ test(ebx, Immediate(kSlicedNotConsMask));
3165 __ j(not_zero, &sliced_string, Label::kNear);
3166 // Cons string. Check whether it is flat, then fetch first part.
3167 // Flat cons strings have an empty second part.
3168 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
3169 factory->empty_string());
3170 __ j(not_equal, &runtime);
3171 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
3172 // Update instance type.
3173 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
3174 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3175 __ jmp(&underlying_unpacked, Label::kNear);
3177 __ bind(&sliced_string);
3178 // Sliced string. Fetch parent and adjust start index by offset.
3179 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
3180 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
3181 // Update instance type.
3182 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
3183 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3184 __ jmp(&underlying_unpacked, Label::kNear);
3186 __ bind(&seq_or_external_string);
3187 // Sequential or external string. Just move string to the expected register.
3190 __ bind(&underlying_unpacked);
3192 if (FLAG_string_slices) {
3194 // edi: underlying subject string
3195 // ebx: instance type of underlying subject string
3196 // edx: adjusted start index (smi)
3197 // ecx: length (smi)
3198 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
3199 // Short slice. Copy instead of slicing.
3200 __ j(less, ©_routine);
3201 // Allocate new sliced string. At this point we do not reload the instance
3202 // type including the string encoding because we simply rely on the info
3203 // provided by the original string. It does not matter if the original
3204 // string's encoding is wrong because we always have to recheck encoding of
3205 // the newly created string's parent anyways due to externalized strings.
3206 Label two_byte_slice, set_slice_header;
3207 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
3208 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3209 __ test(ebx, Immediate(kStringEncodingMask));
3210 __ j(zero, &two_byte_slice, Label::kNear);
3211 __ AllocateOneByteSlicedString(eax, ebx, no_reg, &runtime);
3212 __ jmp(&set_slice_header, Label::kNear);
3213 __ bind(&two_byte_slice);
3214 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
3215 __ bind(&set_slice_header);
3216 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
3217 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
3218 Immediate(String::kEmptyHashField));
3219 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
3220 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
3221 __ IncrementCounter(counters->sub_string_native(), 1);
3222 __ ret(3 * kPointerSize);
3224 __ bind(©_routine);
3227 // edi: underlying subject string
3228 // ebx: instance type of underlying subject string
3229 // edx: adjusted start index (smi)
3230 // ecx: length (smi)
3231 // The subject string can only be external or sequential string of either
3232 // encoding at this point.
3233 Label two_byte_sequential, runtime_drop_two, sequential_string;
3234 STATIC_ASSERT(kExternalStringTag != 0);
3235 STATIC_ASSERT(kSeqStringTag == 0);
3236 __ test_b(ebx, kExternalStringTag);
3237 __ j(zero, &sequential_string);
3239 // Handle external string.
3240 // Rule out short external strings.
3241 STATIC_ASSERT(kShortExternalStringTag != 0);
3242 __ test_b(ebx, kShortExternalStringMask);
3243 __ j(not_zero, &runtime);
3244 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
3245 // Move the pointer so that offset-wise, it looks like a sequential string.
3246 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3247 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3249 __ bind(&sequential_string);
3250 // Stash away (adjusted) index and (underlying) string.
3254 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
3255 __ test_b(ebx, kStringEncodingMask);
3256 __ j(zero, &two_byte_sequential);
3258 // Sequential one byte string. Allocate the result.
3259 __ AllocateOneByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
3261 // eax: result string
3262 // ecx: result string length
3263 // Locate first character of result.
3265 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3266 // Load string argument and locate character of sub string start.
3270 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqOneByteString::kHeaderSize));
3272 // eax: result string
3273 // ecx: result length
3274 // edi: first character of result
3275 // edx: character of sub string start
3276 StringHelper::GenerateCopyCharacters(
3277 masm, edi, edx, ecx, ebx, String::ONE_BYTE_ENCODING);
3278 __ IncrementCounter(counters->sub_string_native(), 1);
3279 __ ret(3 * kPointerSize);
3281 __ bind(&two_byte_sequential);
3282 // Sequential two-byte string. Allocate the result.
3283 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
3285 // eax: result string
3286 // ecx: result string length
3287 // Locate first character of result.
3290 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3291 // Load string argument and locate character of sub string start.
3294 // As from is a smi it is 2 times the value which matches the size of a two
3296 STATIC_ASSERT(kSmiTag == 0);
3297 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3298 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqTwoByteString::kHeaderSize));
3300 // eax: result string
3301 // ecx: result length
3302 // edi: first character of result
3303 // edx: character of sub string start
3304 StringHelper::GenerateCopyCharacters(
3305 masm, edi, edx, ecx, ebx, String::TWO_BYTE_ENCODING);
3306 __ IncrementCounter(counters->sub_string_native(), 1);
3307 __ ret(3 * kPointerSize);
3309 // Drop pushed values on the stack before tail call.
3310 __ bind(&runtime_drop_two);
3313 // Just jump to runtime to create the sub string.
3315 __ TailCallRuntime(Runtime::kSubStringRT, 3, 1);
3317 __ bind(&single_char);
3319 // ebx: instance type
3320 // ecx: sub string length (smi)
3321 // edx: from index (smi)
3322 StringCharAtGenerator generator(eax, edx, ecx, eax, &runtime, &runtime,
3323 &runtime, STRING_INDEX_IS_NUMBER,
3324 RECEIVER_IS_STRING);
3325 generator.GenerateFast(masm);
3326 __ ret(3 * kPointerSize);
3327 generator.SkipSlow(masm, &runtime);
3331 void ToNumberStub::Generate(MacroAssembler* masm) {
3332 // The ToNumber stub takes one argument in eax.
3334 __ JumpIfNotSmi(eax, ¬_smi, Label::kNear);
3338 Label not_heap_number;
3339 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
3340 __ j(not_equal, ¬_heap_number, Label::kNear);
3342 __ bind(¬_heap_number);
3344 Label not_string, slow_string;
3345 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
3348 __ j(above_equal, ¬_string, Label::kNear);
3349 // Check if string has a cached array index.
3350 __ test(FieldOperand(eax, String::kHashFieldOffset),
3351 Immediate(String::kContainsCachedArrayIndexMask));
3352 __ j(not_zero, &slow_string, Label::kNear);
3353 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3354 __ IndexFromHash(eax, eax);
3356 __ bind(&slow_string);
3357 __ pop(ecx); // Pop return address.
3358 __ push(eax); // Push argument.
3359 __ push(ecx); // Push return address.
3360 __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
3361 __ bind(¬_string);
3364 __ CmpInstanceType(edi, ODDBALL_TYPE);
3365 __ j(not_equal, ¬_oddball, Label::kNear);
3366 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
3368 __ bind(¬_oddball);
3370 __ pop(ecx); // Pop return address.
3371 __ push(eax); // Push argument.
3372 __ push(ecx); // Push return address.
3373 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
3377 void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
3381 Register scratch2) {
3382 Register length = scratch1;
3385 Label strings_not_equal, check_zero_length;
3386 __ mov(length, FieldOperand(left, String::kLengthOffset));
3387 __ cmp(length, FieldOperand(right, String::kLengthOffset));
3388 __ j(equal, &check_zero_length, Label::kNear);
3389 __ bind(&strings_not_equal);
3390 __ Move(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
3393 // Check if the length is zero.
3394 Label compare_chars;
3395 __ bind(&check_zero_length);
3396 STATIC_ASSERT(kSmiTag == 0);
3397 __ test(length, length);
3398 __ j(not_zero, &compare_chars, Label::kNear);
3399 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3402 // Compare characters.
3403 __ bind(&compare_chars);
3404 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
3405 &strings_not_equal, Label::kNear);
3407 // Characters are equal.
3408 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3413 void StringHelper::GenerateCompareFlatOneByteStrings(
3414 MacroAssembler* masm, Register left, Register right, Register scratch1,
3415 Register scratch2, Register scratch3) {
3416 Counters* counters = masm->isolate()->counters();
3417 __ IncrementCounter(counters->string_compare_native(), 1);
3419 // Find minimum length.
3421 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
3422 __ mov(scratch3, scratch1);
3423 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
3425 Register length_delta = scratch3;
3427 __ j(less_equal, &left_shorter, Label::kNear);
3428 // Right string is shorter. Change scratch1 to be length of right string.
3429 __ sub(scratch1, length_delta);
3430 __ bind(&left_shorter);
3432 Register min_length = scratch1;
3434 // If either length is zero, just compare lengths.
3435 Label compare_lengths;
3436 __ test(min_length, min_length);
3437 __ j(zero, &compare_lengths, Label::kNear);
3439 // Compare characters.
3440 Label result_not_equal;
3441 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
3442 &result_not_equal, Label::kNear);
3444 // Compare lengths - strings up to min-length are equal.
3445 __ bind(&compare_lengths);
3446 __ test(length_delta, length_delta);
3447 Label length_not_equal;
3448 __ j(not_zero, &length_not_equal, Label::kNear);
3451 STATIC_ASSERT(EQUAL == 0);
3452 STATIC_ASSERT(kSmiTag == 0);
3453 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3456 Label result_greater;
3458 __ bind(&length_not_equal);
3459 __ j(greater, &result_greater, Label::kNear);
3460 __ jmp(&result_less, Label::kNear);
3461 __ bind(&result_not_equal);
3462 __ j(above, &result_greater, Label::kNear);
3463 __ bind(&result_less);
3466 __ Move(eax, Immediate(Smi::FromInt(LESS)));
3469 // Result is GREATER.
3470 __ bind(&result_greater);
3471 __ Move(eax, Immediate(Smi::FromInt(GREATER)));
3476 void StringHelper::GenerateOneByteCharsCompareLoop(
3477 MacroAssembler* masm, Register left, Register right, Register length,
3478 Register scratch, Label* chars_not_equal,
3479 Label::Distance chars_not_equal_near) {
3480 // Change index to run from -length to -1 by adding length to string
3481 // start. This means that loop ends when index reaches zero, which
3482 // doesn't need an additional compare.
3483 __ SmiUntag(length);
3485 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
3487 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
3489 Register index = length; // index = -length;
3494 __ mov_b(scratch, Operand(left, index, times_1, 0));
3495 __ cmpb(scratch, Operand(right, index, times_1, 0));
3496 __ j(not_equal, chars_not_equal, chars_not_equal_near);
3498 __ j(not_zero, &loop);
3502 void StringCompareStub::Generate(MacroAssembler* masm) {
3505 // Stack frame on entry.
3506 // esp[0]: return address
3507 // esp[4]: right string
3508 // esp[8]: left string
3510 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
3511 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
3515 __ j(not_equal, ¬_same, Label::kNear);
3516 STATIC_ASSERT(EQUAL == 0);
3517 STATIC_ASSERT(kSmiTag == 0);
3518 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3519 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1);
3520 __ ret(2 * kPointerSize);
3524 // Check that both objects are sequential one-byte strings.
3525 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx, &runtime);
3527 // Compare flat one-byte strings.
3528 // Drop arguments from the stack.
3530 __ add(esp, Immediate(2 * kPointerSize));
3532 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
3535 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
3536 // tagged as a small integer.
3538 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3542 void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3543 // ----------- S t a t e -------------
3546 // -- esp[0] : return address
3547 // -----------------------------------
3549 // Load ecx with the allocation site. We stick an undefined dummy value here
3550 // and replace it with the real allocation site later when we instantiate this
3551 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3552 __ mov(ecx, handle(isolate()->heap()->undefined_value()));
3554 // Make sure that we actually patched the allocation site.
3555 if (FLAG_debug_code) {
3556 __ test(ecx, Immediate(kSmiTagMask));
3557 __ Assert(not_equal, kExpectedAllocationSite);
3558 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
3559 isolate()->factory()->allocation_site_map());
3560 __ Assert(equal, kExpectedAllocationSite);
3563 // Tail call into the stub that handles binary operations with allocation
3565 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3566 __ TailCallStub(&stub);
3570 void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3571 DCHECK(state() == CompareICState::SMI);
3575 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
3577 if (GetCondition() == equal) {
3578 // For equality we do not care about the sign of the result.
3583 __ j(no_overflow, &done, Label::kNear);
3584 // Correct sign of result in case of overflow.
3596 void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3597 DCHECK(state() == CompareICState::NUMBER);
3600 Label unordered, maybe_undefined1, maybe_undefined2;
3603 if (left() == CompareICState::SMI) {
3604 __ JumpIfNotSmi(edx, &miss);
3606 if (right() == CompareICState::SMI) {
3607 __ JumpIfNotSmi(eax, &miss);
3610 // Load left and right operand.
3611 Label done, left, left_smi, right_smi;
3612 __ JumpIfSmi(eax, &right_smi, Label::kNear);
3613 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
3614 isolate()->factory()->heap_number_map());
3615 __ j(not_equal, &maybe_undefined1, Label::kNear);
3616 __ movsd(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3617 __ jmp(&left, Label::kNear);
3618 __ bind(&right_smi);
3619 __ mov(ecx, eax); // Can't clobber eax because we can still jump away.
3621 __ Cvtsi2sd(xmm1, ecx);
3624 __ JumpIfSmi(edx, &left_smi, Label::kNear);
3625 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
3626 isolate()->factory()->heap_number_map());
3627 __ j(not_equal, &maybe_undefined2, Label::kNear);
3628 __ movsd(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3631 __ mov(ecx, edx); // Can't clobber edx because we can still jump away.
3633 __ Cvtsi2sd(xmm0, ecx);
3636 // Compare operands.
3637 __ ucomisd(xmm0, xmm1);
3639 // Don't base result on EFLAGS when a NaN is involved.
3640 __ j(parity_even, &unordered, Label::kNear);
3642 // Return a result of -1, 0, or 1, based on EFLAGS.
3643 // Performing mov, because xor would destroy the flag register.
3644 __ mov(eax, 0); // equal
3645 __ mov(ecx, Immediate(Smi::FromInt(1)));
3646 __ cmov(above, eax, ecx);
3647 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3648 __ cmov(below, eax, ecx);
3651 __ bind(&unordered);
3652 __ bind(&generic_stub);
3653 CompareICStub stub(isolate(), op(), strength(), CompareICState::GENERIC,
3654 CompareICState::GENERIC, CompareICState::GENERIC);
3655 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3657 __ bind(&maybe_undefined1);
3658 if (Token::IsOrderedRelationalCompareOp(op())) {
3659 __ cmp(eax, Immediate(isolate()->factory()->undefined_value()));
3660 __ j(not_equal, &miss);
3661 __ JumpIfSmi(edx, &unordered);
3662 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
3663 __ j(not_equal, &maybe_undefined2, Label::kNear);
3667 __ bind(&maybe_undefined2);
3668 if (Token::IsOrderedRelationalCompareOp(op())) {
3669 __ cmp(edx, Immediate(isolate()->factory()->undefined_value()));
3670 __ j(equal, &unordered);
3678 void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3679 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3680 DCHECK(GetCondition() == equal);
3682 // Registers containing left and right operands respectively.
3683 Register left = edx;
3684 Register right = eax;
3685 Register tmp1 = ecx;
3686 Register tmp2 = ebx;
3688 // Check that both operands are heap objects.
3691 STATIC_ASSERT(kSmiTag == 0);
3692 __ and_(tmp1, right);
3693 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3695 // Check that both operands are internalized strings.
3696 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3697 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3698 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3699 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3700 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3702 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
3703 __ j(not_zero, &miss, Label::kNear);
3705 // Internalized strings are compared by identity.
3707 __ cmp(left, right);
3708 // Make sure eax is non-zero. At this point input operands are
3709 // guaranteed to be non-zero.
3710 DCHECK(right.is(eax));
3711 __ j(not_equal, &done, Label::kNear);
3712 STATIC_ASSERT(EQUAL == 0);
3713 STATIC_ASSERT(kSmiTag == 0);
3714 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3723 void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3724 DCHECK(state() == CompareICState::UNIQUE_NAME);
3725 DCHECK(GetCondition() == equal);
3727 // Registers containing left and right operands respectively.
3728 Register left = edx;
3729 Register right = eax;
3730 Register tmp1 = ecx;
3731 Register tmp2 = ebx;
3733 // Check that both operands are heap objects.
3736 STATIC_ASSERT(kSmiTag == 0);
3737 __ and_(tmp1, right);
3738 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3740 // Check that both operands are unique names. This leaves the instance
3741 // types loaded in tmp1 and tmp2.
3742 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3743 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3744 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3745 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3747 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
3748 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
3750 // Unique names are compared by identity.
3752 __ cmp(left, right);
3753 // Make sure eax is non-zero. At this point input operands are
3754 // guaranteed to be non-zero.
3755 DCHECK(right.is(eax));
3756 __ j(not_equal, &done, Label::kNear);
3757 STATIC_ASSERT(EQUAL == 0);
3758 STATIC_ASSERT(kSmiTag == 0);
3759 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3768 void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3769 DCHECK(state() == CompareICState::STRING);
3772 bool equality = Token::IsEqualityOp(op());
3774 // Registers containing left and right operands respectively.
3775 Register left = edx;
3776 Register right = eax;
3777 Register tmp1 = ecx;
3778 Register tmp2 = ebx;
3779 Register tmp3 = edi;
3781 // Check that both operands are heap objects.
3783 STATIC_ASSERT(kSmiTag == 0);
3784 __ and_(tmp1, right);
3785 __ JumpIfSmi(tmp1, &miss);
3787 // Check that both operands are strings. This leaves the instance
3788 // types loaded in tmp1 and tmp2.
3789 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3790 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3791 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3792 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3794 STATIC_ASSERT(kNotStringTag != 0);
3796 __ test(tmp3, Immediate(kIsNotStringMask));
3797 __ j(not_zero, &miss);
3799 // Fast check for identical strings.
3801 __ cmp(left, right);
3802 __ j(not_equal, ¬_same, Label::kNear);
3803 STATIC_ASSERT(EQUAL == 0);
3804 STATIC_ASSERT(kSmiTag == 0);
3805 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3808 // Handle not identical strings.
3811 // Check that both strings are internalized. If they are, we're done
3812 // because we already know they are not identical. But in the case of
3813 // non-equality compare, we still need to determine the order. We
3814 // also know they are both strings.
3817 STATIC_ASSERT(kInternalizedTag == 0);
3819 __ test(tmp1, Immediate(kIsNotInternalizedMask));
3820 __ j(not_zero, &do_compare, Label::kNear);
3821 // Make sure eax is non-zero. At this point input operands are
3822 // guaranteed to be non-zero.
3823 DCHECK(right.is(eax));
3825 __ bind(&do_compare);
3828 // Check that both strings are sequential one-byte.
3830 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
3832 // Compare flat one byte strings. Returns when done.
3834 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
3837 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3841 // Handle more complex cases in runtime.
3843 __ pop(tmp1); // Return address.
3848 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3850 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3858 void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3859 DCHECK(state() == CompareICState::OBJECT);
3863 __ JumpIfSmi(ecx, &miss, Label::kNear);
3865 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
3866 __ j(not_equal, &miss, Label::kNear);
3867 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
3868 __ j(not_equal, &miss, Label::kNear);
3870 DCHECK(GetCondition() == equal);
3879 void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
3881 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
3884 __ JumpIfSmi(ecx, &miss, Label::kNear);
3886 __ GetWeakValue(edi, cell);
3887 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3888 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
3890 __ j(not_equal, &miss, Label::kNear);
3892 __ j(not_equal, &miss, Label::kNear);
3902 void CompareICStub::GenerateMiss(MacroAssembler* masm) {
3904 // Call the runtime system in a fresh internal frame.
3905 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
3907 FrameScope scope(masm, StackFrame::INTERNAL);
3908 __ push(edx); // Preserve edx and eax.
3910 __ push(edx); // And also use them as the arguments.
3912 __ push(Immediate(Smi::FromInt(op())));
3913 __ CallExternalReference(miss, 3);
3914 // Compute the entry point of the rewritten stub.
3915 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
3920 // Do a tail call to the rewritten stub.
3925 // Helper function used to check that the dictionary doesn't contain
3926 // the property. This function may return false negatives, so miss_label
3927 // must always call a backup property check that is complete.
3928 // This function is safe to call if the receiver has fast properties.
3929 // Name must be a unique name and receiver must be a heap object.
3930 void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3933 Register properties,
3936 DCHECK(name->IsUniqueName());
3938 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3939 // not equal to the name and kProbes-th slot is not used (its name is the
3940 // undefined value), it guarantees the hash table doesn't contain the
3941 // property. It's true even if some slots represent deleted properties
3942 // (their names are the hole value).
3943 for (int i = 0; i < kInlinedProbes; i++) {
3944 // Compute the masked index: (hash + i + i * i) & mask.
3945 Register index = r0;
3946 // Capacity is smi 2^n.
3947 __ mov(index, FieldOperand(properties, kCapacityOffset));
3950 Immediate(Smi::FromInt(name->Hash() +
3951 NameDictionary::GetProbeOffset(i))));
3953 // Scale the index by multiplying by the entry size.
3954 DCHECK(NameDictionary::kEntrySize == 3);
3955 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
3956 Register entity_name = r0;
3957 // Having undefined at this place means the name is not contained.
3958 DCHECK_EQ(kSmiTagSize, 1);
3959 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
3960 kElementsStartOffset - kHeapObjectTag));
3961 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
3964 // Stop if found the property.
3965 __ cmp(entity_name, Handle<Name>(name));
3969 // Check for the hole and skip.
3970 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
3971 __ j(equal, &good, Label::kNear);
3973 // Check if the entry name is not a unique name.
3974 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3975 __ JumpIfNotUniqueNameInstanceType(
3976 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3980 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3982 __ push(Immediate(Handle<Object>(name)));
3983 __ push(Immediate(name->Hash()));
3986 __ j(not_zero, miss);
3991 // Probe the name dictionary in the |elements| register. Jump to the
3992 // |done| label if a property with the given name is found leaving the
3993 // index into the dictionary in |r0|. Jump to the |miss| label
3995 void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
4002 DCHECK(!elements.is(r0));
4003 DCHECK(!elements.is(r1));
4004 DCHECK(!name.is(r0));
4005 DCHECK(!name.is(r1));
4007 __ AssertName(name);
4009 __ mov(r1, FieldOperand(elements, kCapacityOffset));
4010 __ shr(r1, kSmiTagSize); // convert smi to int
4013 // Generate an unrolled loop that performs a few probes before
4014 // giving up. Measurements done on Gmail indicate that 2 probes
4015 // cover ~93% of loads from dictionaries.
4016 for (int i = 0; i < kInlinedProbes; i++) {
4017 // Compute the masked index: (hash + i + i * i) & mask.
4018 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
4019 __ shr(r0, Name::kHashShift);
4021 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
4025 // Scale the index by multiplying by the entry size.
4026 DCHECK(NameDictionary::kEntrySize == 3);
4027 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
4029 // Check if the key is identical to the name.
4030 __ cmp(name, Operand(elements,
4033 kElementsStartOffset - kHeapObjectTag));
4037 NameDictionaryLookupStub stub(masm->isolate(), elements, r1, r0,
4040 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
4041 __ shr(r0, Name::kHashShift);
4051 void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
4052 // This stub overrides SometimesSetsUpAFrame() to return false. That means
4053 // we cannot call anything that could cause a GC from this stub.
4054 // Stack frame on entry:
4055 // esp[0 * kPointerSize]: return address.
4056 // esp[1 * kPointerSize]: key's hash.
4057 // esp[2 * kPointerSize]: key.
4059 // dictionary_: NameDictionary to probe.
4060 // result_: used as scratch.
4061 // index_: will hold an index of entry if lookup is successful.
4062 // might alias with result_.
4064 // result_ is zero if lookup failed, non zero otherwise.
4066 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
4068 Register scratch = result();
4070 __ mov(scratch, FieldOperand(dictionary(), kCapacityOffset));
4072 __ SmiUntag(scratch);
4075 // If names of slots in range from 1 to kProbes - 1 for the hash value are
4076 // not equal to the name and kProbes-th slot is not used (its name is the
4077 // undefined value), it guarantees the hash table doesn't contain the
4078 // property. It's true even if some slots represent deleted properties
4079 // (their names are the null value).
4080 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
4081 // Compute the masked index: (hash + i + i * i) & mask.
4082 __ mov(scratch, Operand(esp, 2 * kPointerSize));
4084 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
4086 __ and_(scratch, Operand(esp, 0));
4088 // Scale the index by multiplying by the entry size.
4089 DCHECK(NameDictionary::kEntrySize == 3);
4090 __ lea(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
4092 // Having undefined at this place means the name is not contained.
4093 DCHECK_EQ(kSmiTagSize, 1);
4094 __ mov(scratch, Operand(dictionary(), index(), times_pointer_size,
4095 kElementsStartOffset - kHeapObjectTag));
4096 __ cmp(scratch, isolate()->factory()->undefined_value());
4097 __ j(equal, ¬_in_dictionary);
4099 // Stop if found the property.
4100 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
4101 __ j(equal, &in_dictionary);
4103 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
4104 // If we hit a key that is not a unique name during negative
4105 // lookup we have to bailout as this key might be equal to the
4106 // key we are looking for.
4108 // Check if the entry name is not a unique name.
4109 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4110 __ JumpIfNotUniqueNameInstanceType(
4111 FieldOperand(scratch, Map::kInstanceTypeOffset),
4112 &maybe_in_dictionary);
4116 __ bind(&maybe_in_dictionary);
4117 // If we are doing negative lookup then probing failure should be
4118 // treated as a lookup success. For positive lookup probing failure
4119 // should be treated as lookup failure.
4120 if (mode() == POSITIVE_LOOKUP) {
4121 __ mov(result(), Immediate(0));
4123 __ ret(2 * kPointerSize);
4126 __ bind(&in_dictionary);
4127 __ mov(result(), Immediate(1));
4129 __ ret(2 * kPointerSize);
4131 __ bind(¬_in_dictionary);
4132 __ mov(result(), Immediate(0));
4134 __ ret(2 * kPointerSize);
4138 void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
4140 StoreBufferOverflowStub stub(isolate, kDontSaveFPRegs);
4142 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
4147 // Takes the input in 3 registers: address_ value_ and object_. A pointer to
4148 // the value has just been written into the object, now this stub makes sure
4149 // we keep the GC informed. The word in the object where the value has been
4150 // written is in the address register.
4151 void RecordWriteStub::Generate(MacroAssembler* masm) {
4152 Label skip_to_incremental_noncompacting;
4153 Label skip_to_incremental_compacting;
4155 // The first two instructions are generated with labels so as to get the
4156 // offset fixed up correctly by the bind(Label*) call. We patch it back and
4157 // forth between a compare instructions (a nop in this position) and the
4158 // real branch when we start and stop incremental heap marking.
4159 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
4160 __ jmp(&skip_to_incremental_compacting, Label::kFar);
4162 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4163 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4164 MacroAssembler::kReturnAtEnd);
4169 __ bind(&skip_to_incremental_noncompacting);
4170 GenerateIncremental(masm, INCREMENTAL);
4172 __ bind(&skip_to_incremental_compacting);
4173 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4175 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
4176 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
4177 masm->set_byte_at(0, kTwoByteNopInstruction);
4178 masm->set_byte_at(2, kFiveByteNopInstruction);
4182 void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4185 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4186 Label dont_need_remembered_set;
4188 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
4189 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
4191 &dont_need_remembered_set);
4193 __ CheckPageFlag(regs_.object(),
4195 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4197 &dont_need_remembered_set);
4199 // First notify the incremental marker if necessary, then update the
4201 CheckNeedsToInformIncrementalMarker(
4203 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
4205 InformIncrementalMarker(masm);
4206 regs_.Restore(masm);
4207 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4208 MacroAssembler::kReturnAtEnd);
4210 __ bind(&dont_need_remembered_set);
4213 CheckNeedsToInformIncrementalMarker(
4215 kReturnOnNoNeedToInformIncrementalMarker,
4217 InformIncrementalMarker(masm);
4218 regs_.Restore(masm);
4223 void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
4224 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
4225 int argument_count = 3;
4226 __ PrepareCallCFunction(argument_count, regs_.scratch0());
4227 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
4228 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
4229 __ mov(Operand(esp, 2 * kPointerSize),
4230 Immediate(ExternalReference::isolate_address(isolate())));
4232 AllowExternalCallThatCantCauseGC scope(masm);
4234 ExternalReference::incremental_marking_record_write_function(isolate()),
4237 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
4241 void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4242 MacroAssembler* masm,
4243 OnNoNeedToInformIncrementalMarker on_no_need,
4245 Label object_is_black, need_incremental, need_incremental_pop_object;
4247 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
4248 __ and_(regs_.scratch0(), regs_.object());
4249 __ mov(regs_.scratch1(),
4250 Operand(regs_.scratch0(),
4251 MemoryChunk::kWriteBarrierCounterOffset));
4252 __ sub(regs_.scratch1(), Immediate(1));
4253 __ mov(Operand(regs_.scratch0(),
4254 MemoryChunk::kWriteBarrierCounterOffset),
4256 __ j(negative, &need_incremental);
4258 // Let's look at the color of the object: If it is not black we don't have
4259 // to inform the incremental marker.
4260 __ JumpIfBlack(regs_.object(),
4266 regs_.Restore(masm);
4267 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4268 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4269 MacroAssembler::kReturnAtEnd);
4274 __ bind(&object_is_black);
4276 // Get the value from the slot.
4277 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
4279 if (mode == INCREMENTAL_COMPACTION) {
4280 Label ensure_not_white;
4282 __ CheckPageFlag(regs_.scratch0(), // Contains value.
4283 regs_.scratch1(), // Scratch.
4284 MemoryChunk::kEvacuationCandidateMask,
4289 __ CheckPageFlag(regs_.object(),
4290 regs_.scratch1(), // Scratch.
4291 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4296 __ jmp(&need_incremental);
4298 __ bind(&ensure_not_white);
4301 // We need an extra register for this, so we push the object register
4303 __ push(regs_.object());
4304 __ EnsureNotWhite(regs_.scratch0(), // The value.
4305 regs_.scratch1(), // Scratch.
4306 regs_.object(), // Scratch.
4307 &need_incremental_pop_object,
4309 __ pop(regs_.object());
4311 regs_.Restore(masm);
4312 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4313 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4314 MacroAssembler::kReturnAtEnd);
4319 __ bind(&need_incremental_pop_object);
4320 __ pop(regs_.object());
4322 __ bind(&need_incremental);
4324 // Fall through when we need to inform the incremental marker.
4328 void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4329 // ----------- S t a t e -------------
4330 // -- eax : element value to store
4331 // -- ecx : element index as smi
4332 // -- esp[0] : return address
4333 // -- esp[4] : array literal index in function
4334 // -- esp[8] : array literal
4335 // clobbers ebx, edx, edi
4336 // -----------------------------------
4339 Label double_elements;
4341 Label slow_elements;
4342 Label slow_elements_from_double;
4343 Label fast_elements;
4345 // Get array literal index, array literal and its map.
4346 __ mov(edx, Operand(esp, 1 * kPointerSize));
4347 __ mov(ebx, Operand(esp, 2 * kPointerSize));
4348 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
4350 __ CheckFastElements(edi, &double_elements);
4352 // Check for FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS elements
4353 __ JumpIfSmi(eax, &smi_element);
4354 __ CheckFastSmiElements(edi, &fast_elements, Label::kNear);
4356 // Store into the array literal requires a elements transition. Call into
4359 __ bind(&slow_elements);
4360 __ pop(edi); // Pop return address and remember to put back later for tail
4365 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4366 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
4368 __ push(edi); // Return return address so that tail call returns to right
4370 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4372 __ bind(&slow_elements_from_double);
4374 __ jmp(&slow_elements);
4376 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
4377 __ bind(&fast_elements);
4378 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4379 __ lea(ecx, FieldOperand(ebx, ecx, times_half_pointer_size,
4380 FixedArrayBase::kHeaderSize));
4381 __ mov(Operand(ecx, 0), eax);
4382 // Update the write barrier for the array store.
4383 __ RecordWrite(ebx, ecx, eax,
4385 EMIT_REMEMBERED_SET,
4389 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS,
4390 // and value is Smi.
4391 __ bind(&smi_element);
4392 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4393 __ mov(FieldOperand(ebx, ecx, times_half_pointer_size,
4394 FixedArrayBase::kHeaderSize), eax);
4397 // Array literal has ElementsKind of FAST_*_DOUBLE_ELEMENTS.
4398 __ bind(&double_elements);
4401 __ mov(edx, FieldOperand(ebx, JSObject::kElementsOffset));
4402 __ StoreNumberToDoubleElements(eax,
4407 &slow_elements_from_double);
4413 void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4414 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4415 __ call(ces.GetCode(), RelocInfo::CODE_TARGET);
4416 int parameter_count_offset =
4417 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4418 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
4419 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4421 int additional_offset =
4422 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
4423 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
4424 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
4428 void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4429 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4430 LoadICStub stub(isolate(), state());
4431 stub.GenerateForTrampoline(masm);
4435 void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4436 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4437 KeyedLoadICStub stub(isolate(), state());
4438 stub.GenerateForTrampoline(masm);
4442 static void HandleArrayCases(MacroAssembler* masm, Register receiver,
4443 Register key, Register vector, Register slot,
4444 Register feedback, bool is_polymorphic,
4446 // feedback initially contains the feedback array
4447 Label next, next_loop, prepare_next;
4448 Label load_smi_map, compare_map;
4449 Label start_polymorphic;
4454 Register receiver_map = receiver;
4455 Register cached_map = vector;
4457 // Receiver might not be a heap object.
4458 __ JumpIfSmi(receiver, &load_smi_map);
4459 __ mov(receiver_map, FieldOperand(receiver, 0));
4460 __ bind(&compare_map);
4461 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
4463 // A named keyed load might have a 2 element array, all other cases can count
4464 // on an array with at least 2 {map, handler} pairs, so they can go right
4465 // into polymorphic array handling.
4466 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4467 __ j(not_equal, is_polymorphic ? &start_polymorphic : &next);
4469 // found, now call handler.
4470 Register handler = feedback;
4471 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
4474 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4477 if (!is_polymorphic) {
4479 __ cmp(FieldOperand(feedback, FixedArray::kLengthOffset),
4480 Immediate(Smi::FromInt(2)));
4481 __ j(not_equal, &start_polymorphic);
4487 // Polymorphic, we have to loop from 2 to N
4488 __ bind(&start_polymorphic);
4490 Register counter = key;
4491 __ mov(counter, Immediate(Smi::FromInt(2)));
4492 __ bind(&next_loop);
4493 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4494 FixedArray::kHeaderSize));
4495 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4496 __ j(not_equal, &prepare_next);
4497 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
4498 FixedArray::kHeaderSize + kPointerSize));
4502 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4505 __ bind(&prepare_next);
4506 __ add(counter, Immediate(Smi::FromInt(2)));
4507 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
4508 __ j(less, &next_loop);
4510 // We exhausted our array of map handler pairs.
4516 __ bind(&load_smi_map);
4517 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4518 __ jmp(&compare_map);
4522 static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
4523 Register key, Register vector, Register slot,
4524 Register weak_cell, Label* miss) {
4525 // feedback initially contains the feedback array
4526 Label compare_smi_map;
4528 // Move the weak map into the weak_cell register.
4529 Register ic_map = weak_cell;
4530 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
4532 // Receiver might not be a heap object.
4533 __ JumpIfSmi(receiver, &compare_smi_map);
4534 __ cmp(ic_map, FieldOperand(receiver, 0));
4535 __ j(not_equal, miss);
4536 Register handler = weak_cell;
4537 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
4538 FixedArray::kHeaderSize + kPointerSize));
4539 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4542 // In microbenchmarks, it made sense to unroll this code so that the call to
4543 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
4544 __ bind(&compare_smi_map);
4545 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
4546 __ j(not_equal, miss);
4547 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
4548 FixedArray::kHeaderSize + kPointerSize));
4549 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4554 void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
4557 void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4558 GenerateImpl(masm, true);
4562 void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4563 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
4564 Register name = LoadWithVectorDescriptor::NameRegister(); // ecx
4565 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
4566 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
4567 Register scratch = edi;
4568 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4569 FixedArray::kHeaderSize));
4571 // Is it a weak cell?
4573 Label not_array, smi_key, key_okay, miss;
4574 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
4575 __ j(not_equal, &try_array);
4576 HandleMonomorphicCase(masm, receiver, name, vector, slot, scratch, &miss);
4578 // Is it a fixed array?
4579 __ bind(&try_array);
4580 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
4581 __ j(not_equal, ¬_array);
4582 HandleArrayCases(masm, receiver, name, vector, slot, scratch, true, &miss);
4584 __ bind(¬_array);
4585 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4586 __ j(not_equal, &miss);
4589 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4590 Code::ComputeHandlerFlags(Code::LOAD_IC));
4591 masm->isolate()->stub_cache()->GenerateProbe(
4592 masm, Code::LOAD_IC, code_flags, false, receiver, name, vector, scratch);
4597 LoadIC::GenerateMiss(masm);
4601 void KeyedLoadICStub::Generate(MacroAssembler* masm) {
4602 GenerateImpl(masm, false);
4606 void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4607 GenerateImpl(masm, true);
4611 void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4612 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
4613 Register key = LoadWithVectorDescriptor::NameRegister(); // ecx
4614 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
4615 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
4616 Register feedback = edi;
4617 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
4618 FixedArray::kHeaderSize));
4619 // Is it a weak cell?
4621 Label not_array, smi_key, key_okay, miss;
4622 __ CompareRoot(FieldOperand(feedback, 0), Heap::kWeakCellMapRootIndex);
4623 __ j(not_equal, &try_array);
4624 HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, &miss);
4626 __ bind(&try_array);
4627 // Is it a fixed array?
4628 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
4629 __ j(not_equal, ¬_array);
4631 // We have a polymorphic element handler.
4632 Label polymorphic, try_poly_name;
4633 __ bind(&polymorphic);
4634 HandleArrayCases(masm, receiver, key, vector, slot, feedback, true, &miss);
4636 __ bind(¬_array);
4638 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
4639 __ j(not_equal, &try_poly_name);
4640 Handle<Code> megamorphic_stub =
4641 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4642 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4644 __ bind(&try_poly_name);
4645 // We might have a name in feedback, and a fixed array in the next slot.
4646 __ cmp(key, feedback);
4647 __ j(not_equal, &miss);
4648 // If the name comparison succeeded, we know we have a fixed array with
4649 // at least one map/handler pair.
4650 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
4651 FixedArray::kHeaderSize + kPointerSize));
4652 HandleArrayCases(masm, receiver, key, vector, slot, feedback, false, &miss);
4655 KeyedLoadIC::GenerateMiss(masm);
4659 void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4660 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4661 VectorStoreICStub stub(isolate(), state());
4662 stub.GenerateForTrampoline(masm);
4666 void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4667 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4668 VectorKeyedStoreICStub stub(isolate(), state());
4669 stub.GenerateForTrampoline(masm);
4673 void VectorStoreICStub::Generate(MacroAssembler* masm) {
4674 GenerateImpl(masm, false);
4678 void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4679 GenerateImpl(masm, true);
4683 void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4686 // TODO(mvstanton): Implement.
4688 StoreIC::GenerateMiss(masm);
4692 void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4693 GenerateImpl(masm, false);
4697 void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4698 GenerateImpl(masm, true);
4702 void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4705 // TODO(mvstanton): Implement.
4707 KeyedStoreIC::GenerateMiss(masm);
4711 void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4712 EmitLoadTypeFeedbackVector(masm, ebx);
4713 CallICStub stub(isolate(), state());
4714 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4718 void CallIC_ArrayTrampolineStub::Generate(MacroAssembler* masm) {
4719 EmitLoadTypeFeedbackVector(masm, ebx);
4720 CallIC_ArrayStub stub(isolate(), state());
4721 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4725 void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4726 if (masm->isolate()->function_entry_hook() != NULL) {
4727 ProfileEntryHookStub stub(masm->isolate());
4728 masm->CallStub(&stub);
4733 void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4734 // Save volatile registers.
4735 const int kNumSavedRegisters = 3;
4740 // Calculate and push the original stack pointer.
4741 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4744 // Retrieve our return address and use it to calculate the calling
4745 // function's address.
4746 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4747 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
4750 // Call the entry hook.
4751 DCHECK(isolate()->function_entry_hook() != NULL);
4752 __ call(FUNCTION_ADDR(isolate()->function_entry_hook()),
4753 RelocInfo::RUNTIME_ENTRY);
4754 __ add(esp, Immediate(2 * kPointerSize));
4766 static void CreateArrayDispatch(MacroAssembler* masm,
4767 AllocationSiteOverrideMode mode) {
4768 if (mode == DISABLE_ALLOCATION_SITES) {
4769 T stub(masm->isolate(),
4770 GetInitialFastElementsKind(),
4772 __ TailCallStub(&stub);
4773 } else if (mode == DONT_OVERRIDE) {
4774 int last_index = GetSequenceIndexFromFastElementsKind(
4775 TERMINAL_FAST_ELEMENTS_KIND);
4776 for (int i = 0; i <= last_index; ++i) {
4778 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4780 __ j(not_equal, &next);
4781 T stub(masm->isolate(), kind);
4782 __ TailCallStub(&stub);
4786 // If we reached this point there is a problem.
4787 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4794 static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4795 AllocationSiteOverrideMode mode) {
4796 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4797 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
4798 // eax - number of arguments
4799 // edi - constructor?
4800 // esp[0] - return address
4801 // esp[4] - last argument
4802 Label normal_sequence;
4803 if (mode == DONT_OVERRIDE) {
4804 DCHECK(FAST_SMI_ELEMENTS == 0);
4805 DCHECK(FAST_HOLEY_SMI_ELEMENTS == 1);
4806 DCHECK(FAST_ELEMENTS == 2);
4807 DCHECK(FAST_HOLEY_ELEMENTS == 3);
4808 DCHECK(FAST_DOUBLE_ELEMENTS == 4);
4809 DCHECK(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4811 // is the low bit set? If so, we are holey and that is good.
4813 __ j(not_zero, &normal_sequence);
4816 // look at the first argument
4817 __ mov(ecx, Operand(esp, kPointerSize));
4819 __ j(zero, &normal_sequence);
4821 if (mode == DISABLE_ALLOCATION_SITES) {
4822 ElementsKind initial = GetInitialFastElementsKind();
4823 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4825 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4827 DISABLE_ALLOCATION_SITES);
4828 __ TailCallStub(&stub_holey);
4830 __ bind(&normal_sequence);
4831 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4833 DISABLE_ALLOCATION_SITES);
4834 __ TailCallStub(&stub);
4835 } else if (mode == DONT_OVERRIDE) {
4836 // We are going to create a holey array, but our kind is non-holey.
4837 // Fix kind and retry.
4840 if (FLAG_debug_code) {
4841 Handle<Map> allocation_site_map =
4842 masm->isolate()->factory()->allocation_site_map();
4843 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
4844 __ Assert(equal, kExpectedAllocationSite);
4847 // Save the resulting elements kind in type info. We can't just store r3
4848 // in the AllocationSite::transition_info field because elements kind is
4849 // restricted to a portion of the field...upper bits need to be left alone.
4850 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4851 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
4852 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
4854 __ bind(&normal_sequence);
4855 int last_index = GetSequenceIndexFromFastElementsKind(
4856 TERMINAL_FAST_ELEMENTS_KIND);
4857 for (int i = 0; i <= last_index; ++i) {
4859 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4861 __ j(not_equal, &next);
4862 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4863 __ TailCallStub(&stub);
4867 // If we reached this point there is a problem.
4868 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4876 static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4877 int to_index = GetSequenceIndexFromFastElementsKind(
4878 TERMINAL_FAST_ELEMENTS_KIND);
4879 for (int i = 0; i <= to_index; ++i) {
4880 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4881 T stub(isolate, kind);
4883 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4884 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4891 void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4892 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4894 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4896 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4901 void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4903 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4904 for (int i = 0; i < 2; i++) {
4905 // For internal arrays we only need a few things
4906 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4908 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4910 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4916 void ArrayConstructorStub::GenerateDispatchToArrayStub(
4917 MacroAssembler* masm,
4918 AllocationSiteOverrideMode mode) {
4919 if (argument_count() == ANY) {
4920 Label not_zero_case, not_one_case;
4922 __ j(not_zero, ¬_zero_case);
4923 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4925 __ bind(¬_zero_case);
4927 __ j(greater, ¬_one_case);
4928 CreateArrayDispatchOneArgument(masm, mode);
4930 __ bind(¬_one_case);
4931 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4932 } else if (argument_count() == NONE) {
4933 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4934 } else if (argument_count() == ONE) {
4935 CreateArrayDispatchOneArgument(masm, mode);
4936 } else if (argument_count() == MORE_THAN_ONE) {
4937 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4944 void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4945 // ----------- S t a t e -------------
4946 // -- eax : argc (only if argument_count() is ANY or MORE_THAN_ONE)
4947 // -- ebx : AllocationSite or undefined
4948 // -- edi : constructor
4949 // -- edx : Original constructor
4950 // -- esp[0] : return address
4951 // -- esp[4] : last argument
4952 // -----------------------------------
4953 if (FLAG_debug_code) {
4954 // The array construct code is only set for the global and natives
4955 // builtin Array functions which always have maps.
4957 // Initial map for the builtin Array function should be a map.
4958 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4959 // Will both indicate a NULL and a Smi.
4960 __ test(ecx, Immediate(kSmiTagMask));
4961 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4962 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4963 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4965 // We should either have undefined in ebx or a valid AllocationSite
4966 __ AssertUndefinedOrAllocationSite(ebx);
4972 __ j(not_equal, &subclassing);
4975 // If the feedback vector is the undefined value call an array constructor
4976 // that doesn't use AllocationSites.
4977 __ cmp(ebx, isolate()->factory()->undefined_value());
4978 __ j(equal, &no_info);
4980 // Only look at the lower 16 bits of the transition info.
4981 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
4983 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4984 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
4985 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4988 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
4991 __ bind(&subclassing);
4992 __ pop(ecx); // return address.
4997 switch (argument_count()) {
5000 __ add(eax, Immediate(2));
5003 __ mov(eax, Immediate(2));
5006 __ mov(eax, Immediate(3));
5011 __ JumpToExternalReference(
5012 ExternalReference(Runtime::kArrayConstructorWithSubclassing, isolate()));
5016 void InternalArrayConstructorStub::GenerateCase(
5017 MacroAssembler* masm, ElementsKind kind) {
5018 Label not_zero_case, not_one_case;
5019 Label normal_sequence;
5022 __ j(not_zero, ¬_zero_case);
5023 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
5024 __ TailCallStub(&stub0);
5026 __ bind(¬_zero_case);
5028 __ j(greater, ¬_one_case);
5030 if (IsFastPackedElementsKind(kind)) {
5031 // We might need to create a holey array
5032 // look at the first argument
5033 __ mov(ecx, Operand(esp, kPointerSize));
5035 __ j(zero, &normal_sequence);
5037 InternalArraySingleArgumentConstructorStub
5038 stub1_holey(isolate(), GetHoleyElementsKind(kind));
5039 __ TailCallStub(&stub1_holey);
5042 __ bind(&normal_sequence);
5043 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
5044 __ TailCallStub(&stub1);
5046 __ bind(¬_one_case);
5047 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
5048 __ TailCallStub(&stubN);
5052 void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
5053 // ----------- S t a t e -------------
5055 // -- edi : constructor
5056 // -- esp[0] : return address
5057 // -- esp[4] : last argument
5058 // -----------------------------------
5060 if (FLAG_debug_code) {
5061 // The array construct code is only set for the global and natives
5062 // builtin Array functions which always have maps.
5064 // Initial map for the builtin Array function should be a map.
5065 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5066 // Will both indicate a NULL and a Smi.
5067 __ test(ecx, Immediate(kSmiTagMask));
5068 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
5069 __ CmpObjectType(ecx, MAP_TYPE, ecx);
5070 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
5073 // Figure out the right elements kind
5074 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5076 // Load the map's "bit field 2" into |result|. We only need the first byte,
5077 // but the following masking takes care of that anyway.
5078 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
5079 // Retrieve elements_kind from bit field 2.
5080 __ DecodeField<Map::ElementsKindBits>(ecx);
5082 if (FLAG_debug_code) {
5084 __ cmp(ecx, Immediate(FAST_ELEMENTS));
5086 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
5088 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
5092 Label fast_elements_case;
5093 __ cmp(ecx, Immediate(FAST_ELEMENTS));
5094 __ j(equal, &fast_elements_case);
5095 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
5097 __ bind(&fast_elements_case);
5098 GenerateCase(masm, FAST_ELEMENTS);
5102 // Generates an Operand for saving parameters after PrepareCallApiFunction.
5103 static Operand ApiParameterOperand(int index) {
5104 return Operand(esp, index * kPointerSize);
5108 // Prepares stack to put arguments (aligns and so on). Reserves
5109 // space for return value if needed (assumes the return value is a handle).
5110 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
5111 // etc. Saves context (esi). If space was reserved for return value then
5112 // stores the pointer to the reserved slot into esi.
5113 static void PrepareCallApiFunction(MacroAssembler* masm, int argc) {
5114 __ EnterApiExitFrame(argc);
5115 if (__ emit_debug_code()) {
5116 __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
5121 // Calls an API function. Allocates HandleScope, extracts returned value
5122 // from handle and propagates exceptions. Clobbers ebx, edi and
5123 // caller-save registers. Restores context. On return removes
5124 // stack_space * kPointerSize (GCed).
5125 static void CallApiFunctionAndReturn(MacroAssembler* masm,
5126 Register function_address,
5127 ExternalReference thunk_ref,
5128 Operand thunk_last_arg, int stack_space,
5129 Operand* stack_space_operand,
5130 Operand return_value_operand,
5131 Operand* context_restore_operand) {
5132 Isolate* isolate = masm->isolate();
5134 ExternalReference next_address =
5135 ExternalReference::handle_scope_next_address(isolate);
5136 ExternalReference limit_address =
5137 ExternalReference::handle_scope_limit_address(isolate);
5138 ExternalReference level_address =
5139 ExternalReference::handle_scope_level_address(isolate);
5141 DCHECK(edx.is(function_address));
5142 // Allocate HandleScope in callee-save registers.
5143 __ mov(ebx, Operand::StaticVariable(next_address));
5144 __ mov(edi, Operand::StaticVariable(limit_address));
5145 __ add(Operand::StaticVariable(level_address), Immediate(1));
5147 if (FLAG_log_timer_events) {
5148 FrameScope frame(masm, StackFrame::MANUAL);
5149 __ PushSafepointRegisters();
5150 __ PrepareCallCFunction(1, eax);
5151 __ mov(Operand(esp, 0),
5152 Immediate(ExternalReference::isolate_address(isolate)));
5153 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5155 __ PopSafepointRegisters();
5159 Label profiler_disabled;
5160 Label end_profiler_check;
5161 __ mov(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
5162 __ cmpb(Operand(eax, 0), 0);
5163 __ j(zero, &profiler_disabled);
5165 // Additional parameter is the address of the actual getter function.
5166 __ mov(thunk_last_arg, function_address);
5167 // Call the api function.
5168 __ mov(eax, Immediate(thunk_ref));
5170 __ jmp(&end_profiler_check);
5172 __ bind(&profiler_disabled);
5173 // Call the api function.
5174 __ call(function_address);
5175 __ bind(&end_profiler_check);
5177 if (FLAG_log_timer_events) {
5178 FrameScope frame(masm, StackFrame::MANUAL);
5179 __ PushSafepointRegisters();
5180 __ PrepareCallCFunction(1, eax);
5181 __ mov(Operand(esp, 0),
5182 Immediate(ExternalReference::isolate_address(isolate)));
5183 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5185 __ PopSafepointRegisters();
5189 // Load the value from ReturnValue
5190 __ mov(eax, return_value_operand);
5192 Label promote_scheduled_exception;
5193 Label delete_allocated_handles;
5194 Label leave_exit_frame;
5197 // No more valid handles (the result handle was the last one). Restore
5198 // previous handle scope.
5199 __ mov(Operand::StaticVariable(next_address), ebx);
5200 __ sub(Operand::StaticVariable(level_address), Immediate(1));
5201 __ Assert(above_equal, kInvalidHandleScopeLevel);
5202 __ cmp(edi, Operand::StaticVariable(limit_address));
5203 __ j(not_equal, &delete_allocated_handles);
5205 // Leave the API exit frame.
5206 __ bind(&leave_exit_frame);
5207 bool restore_context = context_restore_operand != NULL;
5208 if (restore_context) {
5209 __ mov(esi, *context_restore_operand);
5211 if (stack_space_operand != nullptr) {
5212 __ mov(ebx, *stack_space_operand);
5214 __ LeaveApiExitFrame(!restore_context);
5216 // Check if the function scheduled an exception.
5217 ExternalReference scheduled_exception_address =
5218 ExternalReference::scheduled_exception_address(isolate);
5219 __ cmp(Operand::StaticVariable(scheduled_exception_address),
5220 Immediate(isolate->factory()->the_hole_value()));
5221 __ j(not_equal, &promote_scheduled_exception);
5224 // Check if the function returned a valid JavaScript value.
5226 Register return_value = eax;
5229 __ JumpIfSmi(return_value, &ok, Label::kNear);
5230 __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
5232 __ CmpInstanceType(map, LAST_NAME_TYPE);
5233 __ j(below_equal, &ok, Label::kNear);
5235 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
5236 __ j(above_equal, &ok, Label::kNear);
5238 __ cmp(map, isolate->factory()->heap_number_map());
5239 __ j(equal, &ok, Label::kNear);
5241 __ cmp(return_value, isolate->factory()->undefined_value());
5242 __ j(equal, &ok, Label::kNear);
5244 __ cmp(return_value, isolate->factory()->true_value());
5245 __ j(equal, &ok, Label::kNear);
5247 __ cmp(return_value, isolate->factory()->false_value());
5248 __ j(equal, &ok, Label::kNear);
5250 __ cmp(return_value, isolate->factory()->null_value());
5251 __ j(equal, &ok, Label::kNear);
5253 __ Abort(kAPICallReturnedInvalidObject);
5258 if (stack_space_operand != nullptr) {
5259 DCHECK_EQ(0, stack_space);
5264 __ ret(stack_space * kPointerSize);
5267 // Re-throw by promoting a scheduled exception.
5268 __ bind(&promote_scheduled_exception);
5269 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
5271 // HandleScope limit has changed. Delete allocated extensions.
5272 ExternalReference delete_extensions =
5273 ExternalReference::delete_handle_scope_extensions(isolate);
5274 __ bind(&delete_allocated_handles);
5275 __ mov(Operand::StaticVariable(limit_address), edi);
5277 __ mov(Operand(esp, 0),
5278 Immediate(ExternalReference::isolate_address(isolate)));
5279 __ mov(eax, Immediate(delete_extensions));
5282 __ jmp(&leave_exit_frame);
5286 static void CallApiFunctionStubHelper(MacroAssembler* masm,
5287 const ParameterCount& argc,
5288 bool return_first_arg,
5289 bool call_data_undefined) {
5290 // ----------- S t a t e -------------
5292 // -- ebx : call_data
5294 // -- edx : api_function_address
5296 // -- eax : number of arguments if argc is a register
5298 // -- esp[0] : return address
5299 // -- esp[4] : last argument
5301 // -- esp[argc * 4] : first argument
5302 // -- esp[(argc + 1) * 4] : receiver
5303 // -----------------------------------
5305 Register callee = edi;
5306 Register call_data = ebx;
5307 Register holder = ecx;
5308 Register api_function_address = edx;
5309 Register context = esi;
5310 Register return_address = eax;
5312 typedef FunctionCallbackArguments FCA;
5314 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5315 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5316 STATIC_ASSERT(FCA::kDataIndex == 4);
5317 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5318 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5319 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5320 STATIC_ASSERT(FCA::kHolderIndex == 0);
5321 STATIC_ASSERT(FCA::kArgsLength == 7);
5323 DCHECK(argc.is_immediate() || eax.is(argc.reg()));
5325 if (argc.is_immediate()) {
5326 __ pop(return_address);
5330 // pop return address and save context
5331 __ xchg(context, Operand(esp, 0));
5332 return_address = context;
5341 Register scratch = call_data;
5342 if (!call_data_undefined) {
5344 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
5345 // return value default
5346 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
5350 // return value default
5354 __ push(Immediate(reinterpret_cast<int>(masm->isolate())));
5358 __ mov(scratch, esp);
5360 // push return address
5361 __ push(return_address);
5363 // load context from callee
5364 __ mov(context, FieldOperand(callee, JSFunction::kContextOffset));
5366 // API function gets reference to the v8::Arguments. If CPU profiler
5367 // is enabled wrapper function will be called and we need to pass
5368 // address of the callback as additional parameter, always allocate
5370 const int kApiArgc = 1 + 1;
5372 // Allocate the v8::Arguments structure in the arguments' space since
5373 // it's not controlled by GC.
5374 const int kApiStackSpace = 4;
5376 PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace);
5378 // FunctionCallbackInfo::implicit_args_.
5379 __ mov(ApiParameterOperand(2), scratch);
5380 if (argc.is_immediate()) {
5382 Immediate((argc.immediate() + FCA::kArgsLength - 1) * kPointerSize));
5383 // FunctionCallbackInfo::values_.
5384 __ mov(ApiParameterOperand(3), scratch);
5385 // FunctionCallbackInfo::length_.
5386 __ Move(ApiParameterOperand(4), Immediate(argc.immediate()));
5387 // FunctionCallbackInfo::is_construct_call_.
5388 __ Move(ApiParameterOperand(5), Immediate(0));
5390 __ lea(scratch, Operand(scratch, argc.reg(), times_pointer_size,
5391 (FCA::kArgsLength - 1) * kPointerSize));
5392 // FunctionCallbackInfo::values_.
5393 __ mov(ApiParameterOperand(3), scratch);
5394 // FunctionCallbackInfo::length_.
5395 __ mov(ApiParameterOperand(4), argc.reg());
5396 // FunctionCallbackInfo::is_construct_call_.
5397 __ lea(argc.reg(), Operand(argc.reg(), times_pointer_size,
5398 (FCA::kArgsLength + 1) * kPointerSize));
5399 __ mov(ApiParameterOperand(5), argc.reg());
5402 // v8::InvocationCallback's argument.
5403 __ lea(scratch, ApiParameterOperand(2));
5404 __ mov(ApiParameterOperand(0), scratch);
5406 ExternalReference thunk_ref =
5407 ExternalReference::invoke_function_callback(masm->isolate());
5409 Operand context_restore_operand(ebp,
5410 (2 + FCA::kContextSaveIndex) * kPointerSize);
5411 // Stores return the first js argument
5412 int return_value_offset = 0;
5413 if (return_first_arg) {
5414 return_value_offset = 2 + FCA::kArgsLength;
5416 return_value_offset = 2 + FCA::kReturnValueOffset;
5418 Operand return_value_operand(ebp, return_value_offset * kPointerSize);
5419 int stack_space = 0;
5420 Operand is_construct_call_operand = ApiParameterOperand(5);
5421 Operand* stack_space_operand = &is_construct_call_operand;
5422 if (argc.is_immediate()) {
5423 stack_space = argc.immediate() + FCA::kArgsLength + 1;
5424 stack_space_operand = nullptr;
5426 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5427 ApiParameterOperand(1), stack_space,
5428 stack_space_operand, return_value_operand,
5429 &context_restore_operand);
5433 void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5434 bool call_data_undefined = this->call_data_undefined();
5435 CallApiFunctionStubHelper(masm, ParameterCount(eax), false,
5436 call_data_undefined);
5440 void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5441 bool is_store = this->is_store();
5442 int argc = this->argc();
5443 bool call_data_undefined = this->call_data_undefined();
5444 CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
5445 call_data_undefined);
5449 void CallApiGetterStub::Generate(MacroAssembler* masm) {
5450 // ----------- S t a t e -------------
5451 // -- esp[0] : return address
5453 // -- esp[8 - kArgsLength*4] : PropertyCallbackArguments object
5455 // -- edx : api_function_address
5456 // -----------------------------------
5457 DCHECK(edx.is(ApiGetterDescriptor::function_address()));
5459 // array for v8::Arguments::values_, handler for name and pointer
5460 // to the values (it considered as smi in GC).
5461 const int kStackSpace = PropertyCallbackArguments::kArgsLength + 2;
5462 // Allocate space for opional callback address parameter in case
5463 // CPU profiler is active.
5464 const int kApiArgc = 2 + 1;
5466 Register api_function_address = edx;
5467 Register scratch = ebx;
5469 // load address of name
5470 __ lea(scratch, Operand(esp, 1 * kPointerSize));
5472 PrepareCallApiFunction(masm, kApiArgc);
5473 __ mov(ApiParameterOperand(0), scratch); // name.
5474 __ add(scratch, Immediate(kPointerSize));
5475 __ mov(ApiParameterOperand(1), scratch); // arguments pointer.
5477 ExternalReference thunk_ref =
5478 ExternalReference::invoke_accessor_getter_callback(isolate());
5480 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5481 ApiParameterOperand(2), kStackSpace, nullptr,
5482 Operand(ebp, 7 * kPointerSize), NULL);
5488 } // namespace internal
5491 #endif // V8_TARGET_ARCH_IA32