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);
45 static void InitializeInternalArrayConstructorDescriptor(
46 Isolate* isolate, CodeStubDescriptor* descriptor,
47 int constant_stack_parameter_count) {
49 // eax -- number of arguments
50 // edi -- constructor function
51 Address deopt_handler = Runtime::FunctionForId(
52 Runtime::kInternalArrayConstructor)->entry;
54 if (constant_stack_parameter_count == 0) {
55 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
56 JS_FUNCTION_STUB_MODE);
58 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
59 JS_FUNCTION_STUB_MODE);
64 void ArrayNoArgumentConstructorStub::InitializeDescriptor(
65 CodeStubDescriptor* descriptor) {
66 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
70 void ArraySingleArgumentConstructorStub::InitializeDescriptor(
71 CodeStubDescriptor* descriptor) {
72 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
76 void ArrayNArgumentsConstructorStub::InitializeDescriptor(
77 CodeStubDescriptor* descriptor) {
78 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
82 void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
83 CodeStubDescriptor* descriptor) {
84 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
88 void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
89 CodeStubDescriptor* descriptor) {
90 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
94 void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
95 CodeStubDescriptor* descriptor) {
96 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
100 #define __ ACCESS_MASM(masm)
103 void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
104 ExternalReference miss) {
105 // Update the static counter each time a new code stub is generated.
106 isolate()->counters()->code_stubs()->Increment();
108 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
109 int param_count = descriptor.GetRegisterParameterCount();
111 // Call the runtime system in a fresh internal frame.
112 FrameScope scope(masm, StackFrame::INTERNAL);
113 DCHECK(param_count == 0 ||
114 eax.is(descriptor.GetRegisterParameter(param_count - 1)));
116 for (int i = 0; i < param_count; ++i) {
117 __ push(descriptor.GetRegisterParameter(i));
119 __ CallExternalReference(miss, param_count);
126 void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
127 // We don't allow a GC during a store buffer overflow so there is no need to
128 // store the registers in any particular way, but we do have to store and
131 if (save_doubles()) {
132 __ 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 __ TailCallRuntime(Runtime::kLoadElementWithInterceptor, 2, 1);
694 PropertyAccessCompiler::TailCallBuiltin(
695 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
699 void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
700 // Return address is on the stack.
703 Register receiver = LoadDescriptor::ReceiverRegister();
704 Register index = LoadDescriptor::NameRegister();
705 Register scratch = edi;
706 DCHECK(!scratch.is(receiver) && !scratch.is(index));
707 Register result = eax;
708 DCHECK(!result.is(scratch));
709 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
710 result.is(LoadDescriptor::SlotRegister()));
712 // StringCharAtGenerator doesn't use the result register until it's passed
713 // the different miss possibilities. If it did, we would have a conflict
714 // when FLAG_vector_ics is true.
715 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
716 &miss, // When not a string.
717 &miss, // When not a number.
718 &miss, // When index out of range.
719 STRING_INDEX_IS_ARRAY_INDEX,
721 char_at_generator.GenerateFast(masm);
724 StubRuntimeCallHelper call_helper;
725 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
728 PropertyAccessCompiler::TailCallBuiltin(
729 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
733 void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
734 // The key is in edx and the parameter count is in eax.
735 DCHECK(edx.is(ArgumentsAccessReadDescriptor::index()));
736 DCHECK(eax.is(ArgumentsAccessReadDescriptor::parameter_count()));
738 // The displacement is used for skipping the frame pointer on the
739 // stack. It is the offset of the last parameter (if any) relative
740 // to the frame pointer.
741 static const int kDisplacement = 1 * kPointerSize;
743 // Check that the key is a smi.
745 __ JumpIfNotSmi(edx, &slow, Label::kNear);
747 // Check if the calling frame is an arguments adaptor frame.
749 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
750 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
751 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
752 __ j(equal, &adaptor, Label::kNear);
754 // Check index against formal parameters count limit passed in
755 // through register eax. Use unsigned comparison to get negative
758 __ j(above_equal, &slow, Label::kNear);
760 // Read the argument from the stack and return it.
761 STATIC_ASSERT(kSmiTagSize == 1);
762 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
763 __ lea(ebx, Operand(ebp, eax, times_2, 0));
765 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
768 // Arguments adaptor case: Check index against actual arguments
769 // limit found in the arguments adaptor frame. Use unsigned
770 // comparison to get negative check for free.
772 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
774 __ j(above_equal, &slow, Label::kNear);
776 // Read the argument from the stack and return it.
777 STATIC_ASSERT(kSmiTagSize == 1);
778 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
779 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
781 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
784 // Slow-case: Handle non-smi or out-of-bounds access to arguments
785 // by calling the runtime system.
787 __ pop(ebx); // Return address.
790 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
794 void ArgumentsAccessStub::GenerateNewSloppySlow(MacroAssembler* masm) {
795 // esp[0] : return address
796 // esp[4] : number of parameters
797 // esp[8] : receiver displacement
798 // esp[12] : function
800 // Check if the calling frame is an arguments adaptor frame.
802 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
803 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
804 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
805 __ j(not_equal, &runtime, Label::kNear);
807 // Patch the arguments.length and the parameters pointer.
808 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
809 __ mov(Operand(esp, 1 * kPointerSize), ecx);
810 __ lea(edx, Operand(edx, ecx, times_2,
811 StandardFrameConstants::kCallerSPOffset));
812 __ mov(Operand(esp, 2 * kPointerSize), edx);
815 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
819 void ArgumentsAccessStub::GenerateNewSloppyFast(MacroAssembler* masm) {
820 // esp[0] : return address
821 // esp[4] : number of parameters (tagged)
822 // esp[8] : receiver displacement
823 // esp[12] : function
825 // ebx = parameter count (tagged)
826 __ mov(ebx, Operand(esp, 1 * kPointerSize));
828 // Check if the calling frame is an arguments adaptor frame.
829 // TODO(rossberg): Factor out some of the bits that are shared with the other
830 // Generate* functions.
832 Label adaptor_frame, try_allocate;
833 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
834 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
835 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
836 __ j(equal, &adaptor_frame, Label::kNear);
838 // No adaptor, parameter count = argument count.
840 __ jmp(&try_allocate, Label::kNear);
842 // We have an adaptor frame. Patch the parameters pointer.
843 __ bind(&adaptor_frame);
844 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
845 __ lea(edx, Operand(edx, ecx, times_2,
846 StandardFrameConstants::kCallerSPOffset));
847 __ mov(Operand(esp, 2 * kPointerSize), edx);
849 // ebx = parameter count (tagged)
850 // ecx = argument count (smi-tagged)
851 // esp[4] = parameter count (tagged)
852 // esp[8] = address of receiver argument
853 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
855 __ j(less_equal, &try_allocate, Label::kNear);
858 __ bind(&try_allocate);
860 // Save mapped parameter count.
863 // Compute the sizes of backing store, parameter map, and arguments object.
864 // 1. Parameter map, has 2 extra words containing context and backing store.
865 const int kParameterMapHeaderSize =
866 FixedArray::kHeaderSize + 2 * kPointerSize;
867 Label no_parameter_map;
869 __ j(zero, &no_parameter_map, Label::kNear);
870 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
871 __ bind(&no_parameter_map);
874 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
876 // 3. Arguments object.
877 __ add(ebx, Immediate(Heap::kSloppyArgumentsObjectSize));
879 // Do the allocation of all three objects in one go.
880 __ Allocate(ebx, eax, edx, edi, &runtime, TAG_OBJECT);
882 // eax = address of new object(s) (tagged)
883 // ecx = argument count (smi-tagged)
884 // esp[0] = mapped parameter count (tagged)
885 // esp[8] = parameter count (tagged)
886 // esp[12] = address of receiver argument
887 // Get the arguments map from the current native context into edi.
888 Label has_mapped_parameters, instantiate;
889 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
890 __ mov(edi, FieldOperand(edi, GlobalObject::kNativeContextOffset));
891 __ mov(ebx, Operand(esp, 0 * kPointerSize));
893 __ j(not_zero, &has_mapped_parameters, Label::kNear);
896 Operand(edi, Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX)));
897 __ jmp(&instantiate, Label::kNear);
899 __ bind(&has_mapped_parameters);
900 __ mov(edi, Operand(edi, Context::SlotOffset(
901 Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX)));
902 __ bind(&instantiate);
904 // eax = address of new object (tagged)
905 // ebx = mapped parameter count (tagged)
906 // ecx = argument count (smi-tagged)
907 // edi = address of arguments map (tagged)
908 // esp[0] = mapped parameter count (tagged)
909 // esp[8] = parameter count (tagged)
910 // esp[12] = address of receiver argument
911 // Copy the JS object part.
912 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
913 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
914 masm->isolate()->factory()->empty_fixed_array());
915 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
916 masm->isolate()->factory()->empty_fixed_array());
918 // Set up the callee in-object property.
919 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
920 __ mov(edx, Operand(esp, 4 * kPointerSize));
921 __ AssertNotSmi(edx);
922 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
923 Heap::kArgumentsCalleeIndex * kPointerSize),
926 // Use the length (smi tagged) and set that as an in-object property too.
928 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
929 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
930 Heap::kArgumentsLengthIndex * kPointerSize),
933 // Set up the elements pointer in the allocated arguments object.
934 // If we allocated a parameter map, edi will point there, otherwise to the
936 __ lea(edi, Operand(eax, Heap::kSloppyArgumentsObjectSize));
937 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
939 // eax = address of new object (tagged)
940 // ebx = mapped parameter count (tagged)
941 // ecx = argument count (tagged)
942 // edi = address of parameter map or backing store (tagged)
943 // esp[0] = mapped parameter count (tagged)
944 // esp[8] = parameter count (tagged)
945 // esp[12] = address of receiver argument
949 // Initialize parameter map. If there are no mapped arguments, we're done.
950 Label skip_parameter_map;
952 __ j(zero, &skip_parameter_map);
954 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
955 Immediate(isolate()->factory()->sloppy_arguments_elements_map()));
956 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
957 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
958 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
959 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
960 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
962 // Copy the parameter slots and the holes in the arguments.
963 // We need to fill in mapped_parameter_count slots. They index the context,
964 // where parameters are stored in reverse order, at
965 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
966 // The mapped parameter thus need to get indices
967 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
968 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
969 // We loop from right to left.
970 Label parameters_loop, parameters_test;
972 __ mov(eax, Operand(esp, 2 * kPointerSize));
973 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
974 __ add(ebx, Operand(esp, 4 * kPointerSize));
976 __ mov(ecx, isolate()->factory()->the_hole_value());
978 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
979 // eax = loop variable (tagged)
980 // ebx = mapping index (tagged)
981 // ecx = the hole value
982 // edx = address of parameter map (tagged)
983 // edi = address of backing store (tagged)
984 // esp[0] = argument count (tagged)
985 // esp[4] = address of new object (tagged)
986 // esp[8] = mapped parameter count (tagged)
987 // esp[16] = parameter count (tagged)
988 // esp[20] = address of receiver argument
989 __ jmp(¶meters_test, Label::kNear);
991 __ bind(¶meters_loop);
992 __ sub(eax, Immediate(Smi::FromInt(1)));
993 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
994 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
995 __ add(ebx, Immediate(Smi::FromInt(1)));
996 __ bind(¶meters_test);
998 __ j(not_zero, ¶meters_loop, Label::kNear);
1001 __ bind(&skip_parameter_map);
1003 // ecx = argument count (tagged)
1004 // edi = address of backing store (tagged)
1005 // esp[0] = address of new object (tagged)
1006 // esp[4] = mapped parameter count (tagged)
1007 // esp[12] = parameter count (tagged)
1008 // esp[16] = address of receiver argument
1009 // Copy arguments header and remaining slots (if there are any).
1010 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
1011 Immediate(isolate()->factory()->fixed_array_map()));
1012 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
1014 Label arguments_loop, arguments_test;
1015 __ mov(ebx, Operand(esp, 1 * kPointerSize));
1016 __ mov(edx, Operand(esp, 4 * kPointerSize));
1017 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
1019 __ jmp(&arguments_test, Label::kNear);
1021 __ bind(&arguments_loop);
1022 __ sub(edx, Immediate(kPointerSize));
1023 __ mov(eax, Operand(edx, 0));
1024 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
1025 __ add(ebx, Immediate(Smi::FromInt(1)));
1027 __ bind(&arguments_test);
1029 __ j(less, &arguments_loop, Label::kNear);
1032 __ pop(eax); // Address of arguments object.
1033 __ pop(ebx); // Parameter count.
1035 // Return and remove the on-stack parameters.
1036 __ ret(3 * kPointerSize);
1038 // Do the runtime call to allocate the arguments object.
1040 __ pop(eax); // Remove saved parameter count.
1041 __ mov(Operand(esp, 1 * kPointerSize), ecx); // Patch argument count.
1042 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
1046 void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
1047 // esp[0] : return address
1048 // esp[4] : number of parameters
1049 // esp[8] : receiver displacement
1050 // esp[12] : function
1052 // Check if the calling frame is an arguments adaptor frame.
1053 Label adaptor_frame, try_allocate, runtime;
1054 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1055 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
1056 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1057 __ j(equal, &adaptor_frame, Label::kNear);
1059 // Get the length from the frame.
1060 __ mov(ecx, Operand(esp, 1 * kPointerSize));
1061 __ jmp(&try_allocate, Label::kNear);
1063 // Patch the arguments.length and the parameters pointer.
1064 __ bind(&adaptor_frame);
1065 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1067 __ lea(edx, Operand(edx, ecx, times_2,
1068 StandardFrameConstants::kCallerSPOffset));
1069 __ mov(Operand(esp, 1 * kPointerSize), ecx);
1070 __ mov(Operand(esp, 2 * kPointerSize), edx);
1072 // Try the new space allocation. Start out with computing the size of
1073 // the arguments object and the elements array.
1074 Label add_arguments_object;
1075 __ bind(&try_allocate);
1077 __ j(zero, &add_arguments_object, Label::kNear);
1078 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
1079 __ bind(&add_arguments_object);
1080 __ add(ecx, Immediate(Heap::kStrictArgumentsObjectSize));
1082 // Do the allocation of both objects in one go.
1083 __ Allocate(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
1085 // Get the arguments map from the current native context.
1086 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1087 __ mov(edi, FieldOperand(edi, GlobalObject::kNativeContextOffset));
1088 const int offset = Context::SlotOffset(Context::STRICT_ARGUMENTS_MAP_INDEX);
1089 __ mov(edi, Operand(edi, offset));
1091 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
1092 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
1093 masm->isolate()->factory()->empty_fixed_array());
1094 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
1095 masm->isolate()->factory()->empty_fixed_array());
1097 // Get the length (smi tagged) and set that as an in-object property too.
1098 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
1099 __ mov(ecx, Operand(esp, 1 * kPointerSize));
1101 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
1102 Heap::kArgumentsLengthIndex * kPointerSize),
1105 // If there are no actual arguments, we're done.
1108 __ j(zero, &done, Label::kNear);
1110 // Get the parameters pointer from the stack.
1111 __ mov(edx, Operand(esp, 2 * kPointerSize));
1113 // Set up the elements pointer in the allocated arguments object and
1114 // initialize the header in the elements fixed array.
1115 __ lea(edi, Operand(eax, Heap::kStrictArgumentsObjectSize));
1116 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
1117 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
1118 Immediate(isolate()->factory()->fixed_array_map()));
1120 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
1121 // Untag the length for the loop below.
1124 // Copy the fixed array slots.
1127 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
1128 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
1129 __ add(edi, Immediate(kPointerSize));
1130 __ sub(edx, Immediate(kPointerSize));
1132 __ j(not_zero, &loop);
1134 // Return and remove the on-stack parameters.
1136 __ ret(3 * kPointerSize);
1138 // Do the runtime call to allocate the arguments object.
1140 __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
1144 void RestParamAccessStub::GenerateNew(MacroAssembler* masm) {
1145 // esp[0] : return address
1146 // esp[4] : language mode
1147 // esp[8] : index of rest parameter
1148 // esp[12] : number of parameters
1149 // esp[16] : receiver displacement
1151 // Check if the calling frame is an arguments adaptor frame.
1153 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
1154 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
1155 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1156 __ j(not_equal, &runtime);
1158 // Patch the arguments.length and the parameters pointer.
1159 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1160 __ mov(Operand(esp, 3 * kPointerSize), ecx);
1161 __ lea(edx, Operand(edx, ecx, times_2,
1162 StandardFrameConstants::kCallerSPOffset));
1163 __ mov(Operand(esp, 4 * kPointerSize), edx);
1166 __ TailCallRuntime(Runtime::kNewRestParam, 4, 1);
1170 void RegExpExecStub::Generate(MacroAssembler* masm) {
1171 // Just jump directly to runtime if native RegExp is not selected at compile
1172 // time or if regexp entry in generated code is turned off runtime switch or
1174 #ifdef V8_INTERPRETED_REGEXP
1175 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1176 #else // V8_INTERPRETED_REGEXP
1178 // Stack frame on entry.
1179 // esp[0]: return address
1180 // esp[4]: last_match_info (expected JSArray)
1181 // esp[8]: previous index
1182 // esp[12]: subject string
1183 // esp[16]: JSRegExp object
1185 static const int kLastMatchInfoOffset = 1 * kPointerSize;
1186 static const int kPreviousIndexOffset = 2 * kPointerSize;
1187 static const int kSubjectOffset = 3 * kPointerSize;
1188 static const int kJSRegExpOffset = 4 * kPointerSize;
1191 Factory* factory = isolate()->factory();
1193 // Ensure that a RegExp stack is allocated.
1194 ExternalReference address_of_regexp_stack_memory_address =
1195 ExternalReference::address_of_regexp_stack_memory_address(isolate());
1196 ExternalReference address_of_regexp_stack_memory_size =
1197 ExternalReference::address_of_regexp_stack_memory_size(isolate());
1198 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
1200 __ j(zero, &runtime);
1202 // Check that the first argument is a JSRegExp object.
1203 __ mov(eax, Operand(esp, kJSRegExpOffset));
1204 STATIC_ASSERT(kSmiTag == 0);
1205 __ JumpIfSmi(eax, &runtime);
1206 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
1207 __ j(not_equal, &runtime);
1209 // Check that the RegExp has been compiled (data contains a fixed array).
1210 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1211 if (FLAG_debug_code) {
1212 __ test(ecx, Immediate(kSmiTagMask));
1213 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
1214 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
1215 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
1218 // ecx: RegExp data (FixedArray)
1219 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1220 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
1221 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
1222 __ j(not_equal, &runtime);
1224 // ecx: RegExp data (FixedArray)
1225 // Check that the number of captures fit in the static offsets vector buffer.
1226 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
1227 // Check (number_of_captures + 1) * 2 <= offsets vector size
1228 // Or number_of_captures * 2 <= offsets vector size - 2
1229 // Multiplying by 2 comes for free since edx is smi-tagged.
1230 STATIC_ASSERT(kSmiTag == 0);
1231 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1232 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1233 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
1234 __ j(above, &runtime);
1236 // Reset offset for possibly sliced string.
1237 __ Move(edi, Immediate(0));
1238 __ mov(eax, Operand(esp, kSubjectOffset));
1239 __ JumpIfSmi(eax, &runtime);
1240 __ mov(edx, eax); // Make a copy of the original subject string.
1241 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1242 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1244 // eax: subject string
1245 // edx: subject string
1246 // ebx: subject string instance type
1247 // ecx: RegExp data (FixedArray)
1248 // Handle subject string according to its encoding and representation:
1249 // (1) Sequential two byte? If yes, go to (9).
1250 // (2) Sequential one byte? If yes, go to (6).
1251 // (3) Anything but sequential or cons? If yes, go to (7).
1252 // (4) Cons string. If the string is flat, replace subject with first string.
1253 // Otherwise bailout.
1254 // (5a) Is subject sequential two byte? If yes, go to (9).
1255 // (5b) Is subject external? If yes, go to (8).
1256 // (6) One byte sequential. Load regexp code for one byte.
1260 // Deferred code at the end of the stub:
1261 // (7) Not a long external string? If yes, go to (10).
1262 // (8) External string. Make it, offset-wise, look like a sequential string.
1263 // (8a) Is the external string one byte? If yes, go to (6).
1264 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1265 // (10) Short external string or not a string? If yes, bail out to runtime.
1266 // (11) Sliced string. Replace subject with parent. Go to (5a).
1268 Label seq_one_byte_string /* 6 */, seq_two_byte_string /* 9 */,
1269 external_string /* 8 */, check_underlying /* 5a */,
1270 not_seq_nor_cons /* 7 */, check_code /* E */,
1271 not_long_external /* 10 */;
1273 // (1) Sequential two byte? If yes, go to (9).
1274 __ and_(ebx, kIsNotStringMask |
1275 kStringRepresentationMask |
1276 kStringEncodingMask |
1277 kShortExternalStringMask);
1278 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
1279 __ j(zero, &seq_two_byte_string); // Go to (9).
1281 // (2) Sequential one byte? If yes, go to (6).
1282 // Any other sequential string must be one byte.
1283 __ and_(ebx, Immediate(kIsNotStringMask |
1284 kStringRepresentationMask |
1285 kShortExternalStringMask));
1286 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (6).
1288 // (3) Anything but sequential or cons? If yes, go to (7).
1289 // We check whether the subject string is a cons, since sequential strings
1290 // have already been covered.
1291 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1292 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
1293 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1294 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
1295 __ cmp(ebx, Immediate(kExternalStringTag));
1296 __ j(greater_equal, ¬_seq_nor_cons); // Go to (7).
1298 // (4) Cons string. Check that it's flat.
1299 // Replace subject with first string and reload instance type.
1300 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
1301 __ j(not_equal, &runtime);
1302 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
1303 __ bind(&check_underlying);
1304 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1305 __ mov(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1307 // (5a) Is subject sequential two byte? If yes, go to (9).
1308 __ test_b(ebx, kStringRepresentationMask | kStringEncodingMask);
1309 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
1310 __ j(zero, &seq_two_byte_string); // Go to (9).
1311 // (5b) Is subject external? If yes, go to (8).
1312 __ test_b(ebx, kStringRepresentationMask);
1313 // The underlying external string is never a short external string.
1314 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
1315 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
1316 __ j(not_zero, &external_string); // Go to (8).
1318 // eax: sequential subject string (or look-alike, external string)
1319 // edx: original subject string
1320 // ecx: RegExp data (FixedArray)
1321 // (6) One byte sequential. Load regexp code for one byte.
1322 __ bind(&seq_one_byte_string);
1323 // Load previous index and check range before edx is overwritten. We have
1324 // to use edx instead of eax here because it might have been only made to
1325 // look like a sequential string when it actually is an external string.
1326 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1327 __ JumpIfNotSmi(ebx, &runtime);
1328 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1329 __ j(above_equal, &runtime);
1330 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataOneByteCodeOffset));
1331 __ Move(ecx, Immediate(1)); // Type is one byte.
1333 // (E) Carry on. String handling is done.
1334 __ bind(&check_code);
1335 // edx: irregexp code
1336 // Check that the irregexp code has been generated for the actual string
1337 // encoding. If it has, the field contains a code object otherwise it contains
1338 // a smi (code flushing support).
1339 __ JumpIfSmi(edx, &runtime);
1341 // eax: subject string
1342 // ebx: previous index (smi)
1344 // ecx: encoding of subject string (1 if one_byte, 0 if two_byte);
1345 // All checks done. Now push arguments for native regexp code.
1346 Counters* counters = isolate()->counters();
1347 __ IncrementCounter(counters->regexp_entry_native(), 1);
1349 // Isolates: note we add an additional parameter here (isolate pointer).
1350 static const int kRegExpExecuteArguments = 9;
1351 __ EnterApiExitFrame(kRegExpExecuteArguments);
1353 // Argument 9: Pass current isolate address.
1354 __ mov(Operand(esp, 8 * kPointerSize),
1355 Immediate(ExternalReference::isolate_address(isolate())));
1357 // Argument 8: Indicate that this is a direct call from JavaScript.
1358 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
1360 // Argument 7: Start (high end) of backtracking stack memory area.
1361 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
1362 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
1363 __ mov(Operand(esp, 6 * kPointerSize), esi);
1365 // Argument 6: Set the number of capture registers to zero to force global
1366 // regexps to behave as non-global. This does not affect non-global regexps.
1367 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
1369 // Argument 5: static offsets vector buffer.
1370 __ mov(Operand(esp, 4 * kPointerSize),
1371 Immediate(ExternalReference::address_of_static_offsets_vector(
1374 // Argument 2: Previous index.
1376 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1378 // Argument 1: Original subject string.
1379 // The original subject is in the previous stack frame. Therefore we have to
1380 // use ebp, which points exactly to one pointer size below the previous esp.
1381 // (Because creating a new stack frame pushes the previous ebp onto the stack
1382 // and thereby moves up esp by one kPointerSize.)
1383 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
1384 __ mov(Operand(esp, 0 * kPointerSize), esi);
1386 // esi: original subject string
1387 // eax: underlying subject string
1388 // ebx: previous index
1389 // ecx: encoding of subject string (1 if one_byte 0 if two_byte);
1391 // Argument 4: End of string data
1392 // Argument 3: Start of string data
1393 // Prepare start and end index of the input.
1394 // Load the length from the original sliced string if that is the case.
1395 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
1396 __ add(esi, edi); // Calculate input end wrt offset.
1398 __ add(ebx, edi); // Calculate input start wrt offset.
1400 // ebx: start index of the input string
1401 // esi: end index of the input string
1402 Label setup_two_byte, setup_rest;
1404 __ j(zero, &setup_two_byte, Label::kNear);
1406 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
1407 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1408 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
1409 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1410 __ jmp(&setup_rest, Label::kNear);
1412 __ bind(&setup_two_byte);
1413 STATIC_ASSERT(kSmiTag == 0);
1414 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
1415 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
1416 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1417 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
1418 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1420 __ bind(&setup_rest);
1422 // Locate the code entry and call it.
1423 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1426 // Drop arguments and come back to JS mode.
1427 __ LeaveApiExitFrame(true);
1429 // Check the result.
1432 // We expect exactly one result since we force the called regexp to behave
1434 __ j(equal, &success);
1436 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
1437 __ j(equal, &failure);
1438 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
1439 // If not exception it can only be retry. Handle that in the runtime system.
1440 __ j(not_equal, &runtime);
1441 // Result must now be exception. If there is no pending exception already a
1442 // stack overflow (on the backtrack stack) was detected in RegExp code but
1443 // haven't created the exception yet. Handle that in the runtime system.
1444 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
1445 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
1447 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1448 __ mov(eax, Operand::StaticVariable(pending_exception));
1450 __ j(equal, &runtime);
1452 // For exception, throw the exception again.
1453 __ TailCallRuntime(Runtime::kRegExpExecReThrow, 4, 1);
1456 // For failure to match, return null.
1457 __ mov(eax, factory->null_value());
1458 __ ret(4 * kPointerSize);
1460 // Load RegExp data.
1462 __ mov(eax, Operand(esp, kJSRegExpOffset));
1463 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1464 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
1465 // Calculate number of capture registers (number_of_captures + 1) * 2.
1466 STATIC_ASSERT(kSmiTag == 0);
1467 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1468 __ add(edx, Immediate(2)); // edx was a smi.
1470 // edx: Number of capture registers
1471 // Load last_match_info which is still known to be a fast case JSArray.
1472 // Check that the fourth object is a JSArray object.
1473 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1474 __ JumpIfSmi(eax, &runtime);
1475 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
1476 __ j(not_equal, &runtime);
1477 // Check that the JSArray is in fast case.
1478 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
1479 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
1480 __ cmp(eax, factory->fixed_array_map());
1481 __ j(not_equal, &runtime);
1482 // Check that the last match info has space for the capture registers and the
1483 // additional information.
1484 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
1486 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
1488 __ j(greater, &runtime);
1490 // ebx: last_match_info backing store (FixedArray)
1491 // edx: number of capture registers
1492 // Store the capture count.
1493 __ SmiTag(edx); // Number of capture registers to smi.
1494 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
1495 __ SmiUntag(edx); // Number of capture registers back from smi.
1496 // Store last subject and last input.
1497 __ mov(eax, Operand(esp, kSubjectOffset));
1499 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
1500 __ RecordWriteField(ebx,
1501 RegExpImpl::kLastSubjectOffset,
1506 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
1507 __ RecordWriteField(ebx,
1508 RegExpImpl::kLastInputOffset,
1513 // Get the static offsets vector filled by the native regexp code.
1514 ExternalReference address_of_static_offsets_vector =
1515 ExternalReference::address_of_static_offsets_vector(isolate());
1516 __ mov(ecx, Immediate(address_of_static_offsets_vector));
1518 // ebx: last_match_info backing store (FixedArray)
1519 // ecx: offsets vector
1520 // edx: number of capture registers
1521 Label next_capture, done;
1522 // Capture register counter starts from number of capture registers and
1523 // counts down until wraping after zero.
1524 __ bind(&next_capture);
1525 __ sub(edx, Immediate(1));
1526 __ j(negative, &done, Label::kNear);
1527 // Read the value from the static offsets vector buffer.
1528 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
1530 // Store the smi value in the last match info.
1531 __ mov(FieldOperand(ebx,
1534 RegExpImpl::kFirstCaptureOffset),
1536 __ jmp(&next_capture);
1539 // Return last match info.
1540 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1541 __ ret(4 * kPointerSize);
1543 // Do the runtime call to execute the regexp.
1545 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1547 // Deferred code for string handling.
1548 // (7) Not a long external string? If yes, go to (10).
1549 __ bind(¬_seq_nor_cons);
1550 // Compare flags are still set from (3).
1551 __ j(greater, ¬_long_external, Label::kNear); // Go to (10).
1553 // (8) External string. Short external strings have been ruled out.
1554 __ bind(&external_string);
1555 // Reload instance type.
1556 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1557 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1558 if (FLAG_debug_code) {
1559 // Assert that we do not have a cons or slice (indirect strings) here.
1560 // Sequential strings have already been ruled out.
1561 __ test_b(ebx, kIsIndirectStringMask);
1562 __ Assert(zero, kExternalStringExpectedButNotFound);
1564 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
1565 // Move the pointer so that offset-wise, it looks like a sequential string.
1566 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
1567 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
1568 STATIC_ASSERT(kTwoByteStringTag == 0);
1569 // (8a) Is the external string one byte? If yes, go to (6).
1570 __ test_b(ebx, kStringEncodingMask);
1571 __ j(not_zero, &seq_one_byte_string); // Goto (6).
1573 // eax: sequential subject string (or look-alike, external string)
1574 // edx: original subject string
1575 // ecx: RegExp data (FixedArray)
1576 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1577 __ bind(&seq_two_byte_string);
1578 // Load previous index and check range before edx is overwritten. We have
1579 // to use edx instead of eax here because it might have been only made to
1580 // look like a sequential string when it actually is an external string.
1581 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1582 __ JumpIfNotSmi(ebx, &runtime);
1583 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1584 __ j(above_equal, &runtime);
1585 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
1586 __ Move(ecx, Immediate(0)); // Type is two byte.
1587 __ jmp(&check_code); // Go to (E).
1589 // (10) Not a string or a short external string? If yes, bail out to runtime.
1590 __ bind(¬_long_external);
1591 // Catch non-string subject or short external string.
1592 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1593 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
1594 __ j(not_zero, &runtime);
1596 // (11) Sliced string. Replace subject with parent. Go to (5a).
1597 // Load offset into edi and replace subject string with parent.
1598 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
1599 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
1600 __ jmp(&check_underlying); // Go to (5a).
1601 #endif // V8_INTERPRETED_REGEXP
1605 static int NegativeComparisonResult(Condition cc) {
1606 DCHECK(cc != equal);
1607 DCHECK((cc == less) || (cc == less_equal)
1608 || (cc == greater) || (cc == greater_equal));
1609 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
1613 static void CheckInputType(MacroAssembler* masm, Register input,
1614 CompareICState::State expected, Label* fail) {
1616 if (expected == CompareICState::SMI) {
1617 __ JumpIfNotSmi(input, fail);
1618 } else if (expected == CompareICState::NUMBER) {
1619 __ JumpIfSmi(input, &ok);
1620 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
1621 Immediate(masm->isolate()->factory()->heap_number_map()));
1622 __ j(not_equal, fail);
1624 // We could be strict about internalized/non-internalized here, but as long as
1625 // hydrogen doesn't care, the stub doesn't have to care either.
1630 static void BranchIfNotInternalizedString(MacroAssembler* masm,
1634 __ JumpIfSmi(object, label);
1635 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
1636 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
1637 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
1638 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
1639 __ j(not_zero, label);
1643 void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
1644 Label runtime_call, check_unequal_objects;
1645 Condition cc = GetCondition();
1648 CheckInputType(masm, edx, left(), &miss);
1649 CheckInputType(masm, eax, right(), &miss);
1651 // Compare two smis.
1652 Label non_smi, smi_done;
1655 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
1656 __ sub(edx, eax); // Return on the result of the subtraction.
1657 __ j(no_overflow, &smi_done, Label::kNear);
1658 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
1664 // NOTICE! This code is only reached after a smi-fast-case check, so
1665 // it is certain that at least one operand isn't a smi.
1667 // Identical objects can be compared fast, but there are some tricky cases
1668 // for NaN and undefined.
1669 Label generic_heap_number_comparison;
1671 Label not_identical;
1673 __ j(not_equal, ¬_identical);
1676 // Check for undefined. undefined OP undefined is false even though
1677 // undefined == undefined.
1678 __ cmp(edx, isolate()->factory()->undefined_value());
1679 if (is_strong(strength())) {
1680 // In strong mode, this comparison must throw, so call the runtime.
1681 __ j(equal, &runtime_call, Label::kFar);
1683 Label check_for_nan;
1684 __ j(not_equal, &check_for_nan, Label::kNear);
1685 __ Move(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1687 __ bind(&check_for_nan);
1691 // Test for NaN. Compare heap numbers in a general way,
1692 // to hanlde NaNs correctly.
1693 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
1694 Immediate(isolate()->factory()->heap_number_map()));
1695 __ j(equal, &generic_heap_number_comparison, Label::kNear);
1697 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1698 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
1699 // Call runtime on identical JSObjects. Otherwise return equal.
1700 __ cmpb(ecx, static_cast<uint8_t>(FIRST_SPEC_OBJECT_TYPE));
1701 __ j(above_equal, &runtime_call, Label::kFar);
1702 // Call runtime on identical symbols since we need to throw a TypeError.
1703 __ cmpb(ecx, static_cast<uint8_t>(SYMBOL_TYPE));
1704 __ j(equal, &runtime_call, Label::kFar);
1705 // Call runtime on identical SIMD values since we must throw a TypeError.
1706 __ cmpb(ecx, static_cast<uint8_t>(FLOAT32X4_TYPE));
1707 __ j(equal, &runtime_call, Label::kFar);
1708 if (is_strong(strength())) {
1709 // We have already tested for smis and heap numbers, so if both
1710 // arguments are not strings we must proceed to the slow case.
1711 __ test(ecx, Immediate(kIsNotStringMask));
1712 __ j(not_zero, &runtime_call, Label::kFar);
1715 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
1719 __ bind(¬_identical);
1722 // Strict equality can quickly decide whether objects are equal.
1723 // Non-strict object equality is slower, so it is handled later in the stub.
1724 if (cc == equal && strict()) {
1725 Label slow; // Fallthrough label.
1727 // If we're doing a strict equality comparison, we don't have to do
1728 // type conversion, so we generate code to do fast comparison for objects
1729 // and oddballs. Non-smi numbers and strings still go through the usual
1731 // If either is a Smi (we know that not both are), then they can only
1732 // be equal if the other is a HeapNumber. If so, use the slow case.
1733 STATIC_ASSERT(kSmiTag == 0);
1734 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1735 __ mov(ecx, Immediate(kSmiTagMask));
1738 __ j(not_zero, ¬_smis, Label::kNear);
1739 // One operand is a smi.
1741 // Check whether the non-smi is a heap number.
1742 STATIC_ASSERT(kSmiTagMask == 1);
1743 // ecx still holds eax & kSmiTag, which is either zero or one.
1744 __ sub(ecx, Immediate(0x01));
1747 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
1749 // if eax was smi, ebx is now edx, else eax.
1751 // Check if the non-smi operand is a heap number.
1752 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1753 Immediate(isolate()->factory()->heap_number_map()));
1754 // If heap number, handle it in the slow case.
1755 __ j(equal, &slow, Label::kNear);
1756 // Return non-equal (ebx is not zero)
1761 // If either operand is a JSObject or an oddball value, then they are not
1762 // equal since their pointers are different
1763 // There is no test for undetectability in strict equality.
1765 // Get the type of the first operand.
1766 // If the first object is a JS object, we have done pointer comparison.
1767 Label first_non_object;
1768 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
1769 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1770 __ j(below, &first_non_object, Label::kNear);
1772 // Return non-zero (eax is not zero)
1773 Label return_not_equal;
1774 STATIC_ASSERT(kHeapObjectTag != 0);
1775 __ bind(&return_not_equal);
1778 __ bind(&first_non_object);
1779 // Check for oddballs: true, false, null, undefined.
1780 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1781 __ j(equal, &return_not_equal);
1783 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ecx);
1784 __ j(above_equal, &return_not_equal);
1786 // Check for oddballs: true, false, null, undefined.
1787 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1788 __ j(equal, &return_not_equal);
1790 // Fall through to the general case.
1794 // Generate the number comparison code.
1795 Label non_number_comparison;
1797 __ bind(&generic_heap_number_comparison);
1799 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
1800 __ ucomisd(xmm0, xmm1);
1801 // Don't base result on EFLAGS when a NaN is involved.
1802 __ j(parity_even, &unordered, Label::kNear);
1804 __ mov(eax, 0); // equal
1805 __ mov(ecx, Immediate(Smi::FromInt(1)));
1806 __ cmov(above, eax, ecx);
1807 __ mov(ecx, Immediate(Smi::FromInt(-1)));
1808 __ cmov(below, eax, ecx);
1811 // If one of the numbers was NaN, then the result is always false.
1812 // The cc is never not-equal.
1813 __ bind(&unordered);
1814 DCHECK(cc != not_equal);
1815 if (cc == less || cc == less_equal) {
1816 __ mov(eax, Immediate(Smi::FromInt(1)));
1818 __ mov(eax, Immediate(Smi::FromInt(-1)));
1822 // The number comparison code did not provide a valid result.
1823 __ bind(&non_number_comparison);
1825 // Fast negative check for internalized-to-internalized equality.
1826 Label check_for_strings;
1828 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
1829 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
1831 // We've already checked for object identity, so if both operands
1832 // are internalized they aren't equal. Register eax already holds a
1833 // non-zero value, which indicates not equal, so just return.
1837 __ bind(&check_for_strings);
1839 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx,
1840 &check_unequal_objects);
1842 // Inline comparison of one-byte strings.
1844 StringHelper::GenerateFlatOneByteStringEquals(masm, edx, eax, ecx, ebx);
1846 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
1850 __ Abort(kUnexpectedFallThroughFromStringComparison);
1853 __ bind(&check_unequal_objects);
1854 if (cc == equal && !strict()) {
1855 // Non-strict equality. Objects are unequal if
1856 // they are both JSObjects and not undetectable,
1857 // and their pointers are different.
1858 Label return_unequal;
1859 // At most one is a smi, so we can test for smi by adding the two.
1860 // A smi plus a heap object has the low bit set, a heap object plus
1861 // a heap object has the low bit clear.
1862 STATIC_ASSERT(kSmiTag == 0);
1863 STATIC_ASSERT(kSmiTagMask == 1);
1864 __ lea(ecx, Operand(eax, edx, times_1, 0));
1865 __ test(ecx, Immediate(kSmiTagMask));
1866 __ j(not_zero, &runtime_call, Label::kNear);
1867 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1868 __ j(below, &runtime_call, Label::kNear);
1869 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ebx);
1870 __ j(below, &runtime_call, Label::kNear);
1871 // We do not bail out after this point. Both are JSObjects, and
1872 // they are equal if and only if both are undetectable.
1873 // The and of the undetectable flags is 1 if and only if they are equal.
1874 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
1875 1 << Map::kIsUndetectable);
1876 __ j(zero, &return_unequal, Label::kNear);
1877 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
1878 1 << Map::kIsUndetectable);
1879 __ j(zero, &return_unequal, Label::kNear);
1880 // The objects are both undetectable, so they both compare as the value
1881 // undefined, and are equal.
1882 __ Move(eax, Immediate(EQUAL));
1883 __ bind(&return_unequal);
1884 // Return non-equal by returning the non-zero object pointer in eax,
1885 // or return equal if we fell through to here.
1886 __ ret(0); // rax, rdx were pushed
1888 __ bind(&runtime_call);
1890 // Push arguments below the return address.
1895 // Figure out which native to call and setup the arguments.
1896 Builtins::JavaScript builtin;
1898 builtin = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1901 is_strong(strength()) ? Builtins::COMPARE_STRONG : Builtins::COMPARE;
1902 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1905 // Restore return address on the stack.
1908 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1909 // tagged as a small integer.
1910 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
1917 static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub,
1919 // eax : number of arguments to the construct function
1920 // ebx : feedback vector
1921 // edx : slot in feedback vector (Smi)
1922 // edi : the function to call
1923 // esp[0]: original receiver (for IsSuperConstructorCall)
1929 FrameScope scope(masm, StackFrame::INTERNAL);
1931 // Number-of-arguments register must be smi-tagged to call out.
1959 static void GenerateRecordCallTarget(MacroAssembler* masm, bool is_super) {
1960 // Cache the called function in a feedback vector slot. Cache states
1961 // are uninitialized, monomorphic (indicated by a JSFunction), and
1963 // eax : number of arguments to the construct function
1964 // ebx : feedback vector
1965 // edx : slot in feedback vector (Smi)
1966 // edi : the function to call
1967 // esp[0]: original receiver (for IsSuperConstructorCall)
1968 Isolate* isolate = masm->isolate();
1969 Label initialize, done, miss, megamorphic, not_array_function;
1971 // Load the cache state into ecx.
1972 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1973 FixedArray::kHeaderSize));
1975 // A monomorphic cache hit or an already megamorphic state: invoke the
1976 // function without changing the state.
1977 // We don't know if ecx is a WeakCell or a Symbol, but it's harmless to read
1978 // at this position in a symbol (see static asserts in
1979 // type-feedback-vector.h).
1980 Label check_allocation_site;
1981 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
1982 __ j(equal, &done, Label::kFar);
1983 __ CompareRoot(ecx, Heap::kmegamorphic_symbolRootIndex);
1984 __ j(equal, &done, Label::kFar);
1985 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1986 Heap::kWeakCellMapRootIndex);
1987 __ j(not_equal, FLAG_pretenuring_call_new ? &miss : &check_allocation_site);
1989 // If the weak cell is cleared, we have a new chance to become monomorphic.
1990 __ JumpIfSmi(FieldOperand(ecx, WeakCell::kValueOffset), &initialize);
1991 __ jmp(&megamorphic);
1993 if (!FLAG_pretenuring_call_new) {
1994 __ bind(&check_allocation_site);
1995 // If we came here, we need to see if we are the array function.
1996 // If we didn't have a matching function, and we didn't find the megamorph
1997 // sentinel, then we have in the slot either some other function or an
1999 __ CompareRoot(FieldOperand(ecx, 0), Heap::kAllocationSiteMapRootIndex);
2000 __ j(not_equal, &miss);
2002 // Make sure the function is the Array() function
2003 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2005 __ j(not_equal, &megamorphic);
2006 __ jmp(&done, Label::kFar);
2011 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
2013 __ CompareRoot(ecx, Heap::kuninitialized_symbolRootIndex);
2014 __ j(equal, &initialize);
2015 // MegamorphicSentinel is an immortal immovable object (undefined) so no
2016 // write-barrier is needed.
2017 __ bind(&megamorphic);
2019 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
2020 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2021 __ jmp(&done, Label::kFar);
2023 // An uninitialized cache is patched with the function or sentinel to
2024 // indicate the ElementsKind if function is the Array constructor.
2025 __ bind(&initialize);
2026 if (!FLAG_pretenuring_call_new) {
2027 // Make sure the function is the Array() function
2028 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2030 __ j(not_equal, ¬_array_function);
2032 // The target function is the Array constructor,
2033 // Create an AllocationSite if we don't already have it, store it in the
2035 CreateAllocationSiteStub create_stub(isolate);
2036 CallStubInRecordCallTarget(masm, &create_stub, is_super);
2039 __ bind(¬_array_function);
2042 CreateWeakCellStub create_stub(isolate);
2043 CallStubInRecordCallTarget(masm, &create_stub, is_super);
2048 static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
2049 // Do not transform the receiver for strict mode functions.
2050 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2051 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset),
2052 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
2053 __ j(not_equal, cont);
2055 // Do not transform the receiver for natives (shared already in ecx).
2056 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset),
2057 1 << SharedFunctionInfo::kNativeBitWithinByte);
2058 __ j(not_equal, cont);
2062 static void EmitSlowCase(Isolate* isolate,
2063 MacroAssembler* masm,
2065 Label* non_function) {
2066 // Check for function proxy.
2067 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
2068 __ j(not_equal, non_function);
2070 __ push(edi); // put proxy as additional argument under return address
2072 __ Move(eax, Immediate(argc + 1));
2073 __ Move(ebx, Immediate(0));
2074 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
2076 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
2077 __ jmp(adaptor, RelocInfo::CODE_TARGET);
2080 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
2081 // of the original receiver from the call site).
2082 __ bind(non_function);
2083 __ mov(Operand(esp, (argc + 1) * kPointerSize), edi);
2084 __ Move(eax, Immediate(argc));
2085 __ Move(ebx, Immediate(0));
2086 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
2087 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
2088 __ jmp(adaptor, RelocInfo::CODE_TARGET);
2092 static void EmitWrapCase(MacroAssembler* masm, int argc, Label* cont) {
2093 // Wrap the receiver and patch it back onto the stack.
2094 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
2097 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
2100 __ mov(Operand(esp, (argc + 1) * kPointerSize), eax);
2105 static void CallFunctionNoFeedback(MacroAssembler* masm,
2106 int argc, bool needs_checks,
2107 bool call_as_method) {
2108 // edi : the function to call
2109 Label slow, non_function, wrap, cont;
2112 // Check that the function really is a JavaScript function.
2113 __ JumpIfSmi(edi, &non_function);
2115 // Goto slow case if we do not have a function.
2116 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2117 __ j(not_equal, &slow);
2120 // Fast-case: Just invoke the function.
2121 ParameterCount actual(argc);
2123 if (call_as_method) {
2125 EmitContinueIfStrictOrNative(masm, &cont);
2128 // Load the receiver from the stack.
2129 __ mov(eax, Operand(esp, (argc + 1) * kPointerSize));
2132 __ JumpIfSmi(eax, &wrap);
2134 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
2143 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
2146 // Slow-case: Non-function called.
2148 // (non_function is bound in EmitSlowCase)
2149 EmitSlowCase(masm->isolate(), masm, argc, &non_function);
2152 if (call_as_method) {
2154 EmitWrapCase(masm, argc, &cont);
2159 void CallFunctionStub::Generate(MacroAssembler* masm) {
2160 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
2164 void CallConstructStub::Generate(MacroAssembler* masm) {
2165 // eax : number of arguments
2166 // ebx : feedback vector
2167 // ecx : original constructor (for IsSuperConstructorCall)
2168 // edx : slot in feedback vector (Smi, for RecordCallTarget)
2169 // edi : constructor function
2170 Label slow, non_function_call;
2172 if (IsSuperConstructorCall()) {
2176 // Check that function is not a smi.
2177 __ JumpIfSmi(edi, &non_function_call);
2178 // Check that function is a JSFunction.
2179 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2180 __ j(not_equal, &slow);
2182 if (RecordCallTarget()) {
2183 GenerateRecordCallTarget(masm, IsSuperConstructorCall());
2185 if (FLAG_pretenuring_call_new) {
2186 // Put the AllocationSite from the feedback vector into ebx.
2187 // By adding kPointerSize we encode that we know the AllocationSite
2188 // entry is at the feedback vector slot given by edx + 1.
2189 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
2190 FixedArray::kHeaderSize + kPointerSize));
2192 Label feedback_register_initialized;
2193 // Put the AllocationSite from the feedback vector into ebx, or undefined.
2194 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
2195 FixedArray::kHeaderSize));
2196 Handle<Map> allocation_site_map =
2197 isolate()->factory()->allocation_site_map();
2198 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
2199 __ j(equal, &feedback_register_initialized);
2200 __ mov(ebx, isolate()->factory()->undefined_value());
2201 __ bind(&feedback_register_initialized);
2204 __ AssertUndefinedOrAllocationSite(ebx);
2207 if (IsSuperConstructorCall()) {
2210 // Pass original constructor to construct stub.
2214 // Jump to the function-specific construct stub.
2215 Register jmp_reg = ecx;
2216 __ mov(jmp_reg, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2217 __ mov(jmp_reg, FieldOperand(jmp_reg,
2218 SharedFunctionInfo::kConstructStubOffset));
2219 __ lea(jmp_reg, FieldOperand(jmp_reg, Code::kHeaderSize));
2222 // edi: called object
2223 // eax: number of arguments
2225 // esp[0]: original receiver (for IsSuperConstructorCall)
2228 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
2229 __ j(not_equal, &non_function_call);
2230 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
2233 __ bind(&non_function_call);
2234 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
2236 if (IsSuperConstructorCall()) {
2239 // Set expected number of arguments to zero (not changing eax).
2240 __ Move(ebx, Immediate(0));
2241 Handle<Code> arguments_adaptor =
2242 isolate()->builtins()->ArgumentsAdaptorTrampoline();
2243 __ jmp(arguments_adaptor, RelocInfo::CODE_TARGET);
2247 static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
2248 __ mov(vector, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
2249 __ mov(vector, FieldOperand(vector, JSFunction::kSharedFunctionInfoOffset));
2250 __ mov(vector, FieldOperand(vector,
2251 SharedFunctionInfo::kFeedbackVectorOffset));
2255 void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
2260 int argc = arg_count();
2261 ParameterCount actual(argc);
2263 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2265 __ j(not_equal, &miss);
2267 __ mov(eax, arg_count());
2268 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
2269 FixedArray::kHeaderSize));
2271 // Verify that ecx contains an AllocationSite
2272 Factory* factory = masm->isolate()->factory();
2273 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
2274 factory->allocation_site_map());
2275 __ j(not_equal, &miss);
2277 // Increment the call count for monomorphic function calls.
2278 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
2279 FixedArray::kHeaderSize + kPointerSize),
2280 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2284 ArrayConstructorStub stub(masm->isolate(), arg_count());
2285 __ TailCallStub(&stub);
2290 // The slow case, we need this no matter what to complete a call after a miss.
2291 CallFunctionNoFeedback(masm,
2301 void CallICStub::Generate(MacroAssembler* masm) {
2305 Isolate* isolate = masm->isolate();
2306 const int with_types_offset =
2307 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kWithTypesIndex);
2308 const int generic_offset =
2309 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kGenericCountIndex);
2310 Label extra_checks_or_miss, slow_start;
2311 Label slow, non_function, wrap, cont;
2312 Label have_js_function;
2313 int argc = arg_count();
2314 ParameterCount actual(argc);
2316 // The checks. First, does edi match the recorded monomorphic target?
2317 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
2318 FixedArray::kHeaderSize));
2320 // We don't know that we have a weak cell. We might have a private symbol
2321 // or an AllocationSite, but the memory is safe to examine.
2322 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
2324 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
2325 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
2326 // computed, meaning that it can't appear to be a pointer. If the low bit is
2327 // 0, then hash is computed, but the 0 bit prevents the field from appearing
2329 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
2330 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
2331 WeakCell::kValueOffset &&
2332 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
2334 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
2335 __ j(not_equal, &extra_checks_or_miss);
2337 // The compare above could have been a SMI/SMI comparison. Guard against this
2338 // convincing us that we have a monomorphic JSFunction.
2339 __ JumpIfSmi(edi, &extra_checks_or_miss);
2341 // Increment the call count for monomorphic function calls.
2342 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
2343 FixedArray::kHeaderSize + kPointerSize),
2344 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2346 __ bind(&have_js_function);
2347 if (CallAsMethod()) {
2348 EmitContinueIfStrictOrNative(masm, &cont);
2350 // Load the receiver from the stack.
2351 __ mov(eax, Operand(esp, (argc + 1) * kPointerSize));
2353 __ JumpIfSmi(eax, &wrap);
2355 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
2361 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
2364 EmitSlowCase(isolate, masm, argc, &non_function);
2366 if (CallAsMethod()) {
2368 EmitWrapCase(masm, argc, &cont);
2371 __ bind(&extra_checks_or_miss);
2372 Label uninitialized, miss;
2374 __ cmp(ecx, Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2375 __ j(equal, &slow_start);
2377 // The following cases attempt to handle MISS cases without going to the
2379 if (FLAG_trace_ic) {
2383 __ cmp(ecx, Immediate(TypeFeedbackVector::UninitializedSentinel(isolate)));
2384 __ j(equal, &uninitialized);
2386 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2387 // to handle it here. More complex cases are dealt with in the runtime.
2388 __ AssertNotSmi(ecx);
2389 __ CmpObjectType(ecx, JS_FUNCTION_TYPE, ecx);
2390 __ j(not_equal, &miss);
2392 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
2393 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2394 // We have to update statistics for runtime profiling.
2395 __ sub(FieldOperand(ebx, with_types_offset), Immediate(Smi::FromInt(1)));
2396 __ add(FieldOperand(ebx, generic_offset), Immediate(Smi::FromInt(1)));
2397 __ jmp(&slow_start);
2399 __ bind(&uninitialized);
2401 // We are going monomorphic, provided we actually have a JSFunction.
2402 __ JumpIfSmi(edi, &miss);
2404 // Goto miss case if we do not have a function.
2405 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2406 __ j(not_equal, &miss);
2408 // Make sure the function is not the Array() function, which requires special
2409 // behavior on MISS.
2410 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2415 __ add(FieldOperand(ebx, with_types_offset), Immediate(Smi::FromInt(1)));
2417 // Initialize the call counter.
2418 __ mov(FieldOperand(ebx, edx, times_half_pointer_size,
2419 FixedArray::kHeaderSize + kPointerSize),
2420 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2422 // Store the function. Use a stub since we need a frame for allocation.
2427 FrameScope scope(masm, StackFrame::INTERNAL);
2428 CreateWeakCellStub create_stub(isolate);
2430 __ CallStub(&create_stub);
2434 __ jmp(&have_js_function);
2436 // We are here because tracing is on or we encountered a MISS case we can't
2442 __ bind(&slow_start);
2444 // Check that the function really is a JavaScript function.
2445 __ JumpIfSmi(edi, &non_function);
2447 // Goto slow case if we do not have a function.
2448 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2449 __ j(not_equal, &slow);
2450 __ jmp(&have_js_function);
2457 void CallICStub::GenerateMiss(MacroAssembler* masm) {
2458 FrameScope scope(masm, StackFrame::INTERNAL);
2460 // Push the function and feedback info.
2466 Runtime::FunctionId id = GetICState() == DEFAULT
2467 ? Runtime::kCallIC_Miss
2468 : Runtime::kCallIC_Customization_Miss;
2469 __ CallRuntime(id, 3);
2471 // Move result to edi and exit the internal frame.
2476 bool CEntryStub::NeedsImmovableCode() {
2481 void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
2482 CEntryStub::GenerateAheadOfTime(isolate);
2483 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
2484 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
2485 // It is important that the store buffer overflow stubs are generated first.
2486 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
2487 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
2488 CreateWeakCellStub::GenerateAheadOfTime(isolate);
2489 BinaryOpICStub::GenerateAheadOfTime(isolate);
2490 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
2491 StoreFastElementStub::GenerateAheadOfTime(isolate);
2492 TypeofStub::GenerateAheadOfTime(isolate);
2496 void CodeStub::GenerateFPStubs(Isolate* isolate) {
2497 // Generate if not already in cache.
2498 CEntryStub(isolate, 1, kSaveFPRegs).GetCode();
2499 isolate->set_fp_stubs_generated(true);
2503 void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
2504 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
2509 void CEntryStub::Generate(MacroAssembler* masm) {
2510 // eax: number of arguments including receiver
2511 // ebx: pointer to C function (C callee-saved)
2512 // ebp: frame pointer (restored after C call)
2513 // esp: stack pointer (restored after C call)
2514 // esi: current context (C callee-saved)
2515 // edi: JS function of the caller (C callee-saved)
2517 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2519 // Enter the exit frame that transitions from JavaScript to C++.
2520 __ EnterExitFrame(save_doubles());
2522 // ebx: pointer to C function (C callee-saved)
2523 // ebp: frame pointer (restored after C call)
2524 // esp: stack pointer (restored after C call)
2525 // edi: number of arguments including receiver (C callee-saved)
2526 // esi: pointer to the first argument (C callee-saved)
2528 // Result returned in eax, or eax+edx if result size is 2.
2530 // Check stack alignment.
2531 if (FLAG_debug_code) {
2532 __ CheckStackAlignment();
2536 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
2537 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
2538 __ mov(Operand(esp, 2 * kPointerSize),
2539 Immediate(ExternalReference::isolate_address(isolate())));
2541 // Result is in eax or edx:eax - do not destroy these registers!
2543 // Check result for exception sentinel.
2544 Label exception_returned;
2545 __ cmp(eax, isolate()->factory()->exception());
2546 __ j(equal, &exception_returned);
2548 // Check that there is no pending exception, otherwise we
2549 // should have returned the exception sentinel.
2550 if (FLAG_debug_code) {
2552 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
2554 ExternalReference pending_exception_address(
2555 Isolate::kPendingExceptionAddress, isolate());
2556 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
2557 // Cannot use check here as it attempts to generate call into runtime.
2558 __ j(equal, &okay, Label::kNear);
2564 // Exit the JavaScript to C++ exit frame.
2565 __ LeaveExitFrame(save_doubles());
2568 // Handling of exception.
2569 __ bind(&exception_returned);
2571 ExternalReference pending_handler_context_address(
2572 Isolate::kPendingHandlerContextAddress, isolate());
2573 ExternalReference pending_handler_code_address(
2574 Isolate::kPendingHandlerCodeAddress, isolate());
2575 ExternalReference pending_handler_offset_address(
2576 Isolate::kPendingHandlerOffsetAddress, isolate());
2577 ExternalReference pending_handler_fp_address(
2578 Isolate::kPendingHandlerFPAddress, isolate());
2579 ExternalReference pending_handler_sp_address(
2580 Isolate::kPendingHandlerSPAddress, isolate());
2582 // Ask the runtime for help to determine the handler. This will set eax to
2583 // contain the current pending exception, don't clobber it.
2584 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
2587 FrameScope scope(masm, StackFrame::MANUAL);
2588 __ PrepareCallCFunction(3, eax);
2589 __ mov(Operand(esp, 0 * kPointerSize), Immediate(0)); // argc.
2590 __ mov(Operand(esp, 1 * kPointerSize), Immediate(0)); // argv.
2591 __ mov(Operand(esp, 2 * kPointerSize),
2592 Immediate(ExternalReference::isolate_address(isolate())));
2593 __ CallCFunction(find_handler, 3);
2596 // Retrieve the handler context, SP and FP.
2597 __ mov(esi, Operand::StaticVariable(pending_handler_context_address));
2598 __ mov(esp, Operand::StaticVariable(pending_handler_sp_address));
2599 __ mov(ebp, Operand::StaticVariable(pending_handler_fp_address));
2601 // If the handler is a JS frame, restore the context to the frame. Note that
2602 // the context will be set to (esi == 0) for non-JS frames.
2605 __ j(zero, &skip, Label::kNear);
2606 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
2609 // Compute the handler entry address and jump to it.
2610 __ mov(edi, Operand::StaticVariable(pending_handler_code_address));
2611 __ mov(edx, Operand::StaticVariable(pending_handler_offset_address));
2612 __ lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
2617 void JSEntryStub::Generate(MacroAssembler* masm) {
2618 Label invoke, handler_entry, exit;
2619 Label not_outermost_js, not_outermost_js_2;
2621 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2627 // Push marker in two places.
2628 int marker = type();
2629 __ push(Immediate(Smi::FromInt(marker))); // context slot
2630 __ push(Immediate(Smi::FromInt(marker))); // function slot
2631 // Save callee-saved registers (C calling conventions).
2636 // Save copies of the top frame descriptor on the stack.
2637 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
2638 __ push(Operand::StaticVariable(c_entry_fp));
2640 // If this is the outermost JS call, set js_entry_sp value.
2641 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
2642 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
2643 __ j(not_equal, ¬_outermost_js, Label::kNear);
2644 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
2645 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
2646 __ jmp(&invoke, Label::kNear);
2647 __ bind(¬_outermost_js);
2648 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
2650 // Jump to a faked try block that does the invoke, with a faked catch
2651 // block that sets the pending exception.
2653 __ bind(&handler_entry);
2654 handler_offset_ = handler_entry.pos();
2655 // Caught exception: Store result (exception) in the pending exception
2656 // field in the JSEnv and return a failure sentinel.
2657 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
2659 __ mov(Operand::StaticVariable(pending_exception), eax);
2660 __ mov(eax, Immediate(isolate()->factory()->exception()));
2663 // Invoke: Link this frame into the handler chain.
2665 __ PushStackHandler();
2667 // Clear any pending exceptions.
2668 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
2669 __ mov(Operand::StaticVariable(pending_exception), edx);
2671 // Fake a receiver (NULL).
2672 __ push(Immediate(0)); // receiver
2674 // Invoke the function by calling through JS entry trampoline builtin and
2675 // pop the faked function when we return. Notice that we cannot store a
2676 // reference to the trampoline code directly in this stub, because the
2677 // builtin stubs may not have been generated yet.
2678 if (type() == StackFrame::ENTRY_CONSTRUCT) {
2679 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
2681 __ mov(edx, Immediate(construct_entry));
2683 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
2684 __ mov(edx, Immediate(entry));
2686 __ mov(edx, Operand(edx, 0)); // deref address
2687 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
2690 // Unlink this frame from the handler chain.
2691 __ PopStackHandler();
2694 // Check if the current stack frame is marked as the outermost JS frame.
2696 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
2697 __ j(not_equal, ¬_outermost_js_2);
2698 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
2699 __ bind(¬_outermost_js_2);
2701 // Restore the top frame descriptor from the stack.
2702 __ pop(Operand::StaticVariable(ExternalReference(
2703 Isolate::kCEntryFPAddress, isolate())));
2705 // Restore callee-saved registers (C calling conventions).
2709 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
2711 // Restore frame pointer and return.
2717 // Generate stub code for instanceof.
2718 // This code can patch a call site inlined cache of the instance of check,
2719 // which looks like this.
2721 // 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
2722 // 75 0a jne <some near label>
2723 // b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
2725 // If call site patching is requested the stack will have the delta from the
2726 // return address to the cmp instruction just below the return address. This
2727 // also means that call site patching can only take place with arguments in
2728 // registers. TOS looks like this when call site patching is requested
2730 // esp[0] : return address
2731 // esp[4] : delta from return address to cmp instruction
2733 void InstanceofStub::Generate(MacroAssembler* masm) {
2734 // Call site inlining and patching implies arguments in registers.
2735 DCHECK(HasArgsInRegisters() || !HasCallSiteInlineCheck());
2737 // Fixed register usage throughout the stub.
2738 Register object = eax; // Object (lhs).
2739 Register map = ebx; // Map of the object.
2740 Register function = edx; // Function (rhs).
2741 Register prototype = edi; // Prototype of the function.
2742 Register scratch = ecx;
2744 // Constants describing the call site code to patch.
2745 static const int kDeltaToCmpImmediate = 2;
2746 static const int kDeltaToMov = 8;
2747 static const int kDeltaToMovImmediate = 9;
2748 static const int8_t kCmpEdiOperandByte1 = bit_cast<int8_t, uint8_t>(0x3b);
2749 static const int8_t kCmpEdiOperandByte2 = bit_cast<int8_t, uint8_t>(0x3d);
2750 static const int8_t kMovEaxImmediateByte = bit_cast<int8_t, uint8_t>(0xb8);
2752 DCHECK_EQ(object.code(), InstanceofStub::left().code());
2753 DCHECK_EQ(function.code(), InstanceofStub::right().code());
2755 // Get the object and function - they are always both needed.
2756 Label slow, not_js_object;
2757 if (!HasArgsInRegisters()) {
2758 __ mov(object, Operand(esp, 2 * kPointerSize));
2759 __ mov(function, Operand(esp, 1 * kPointerSize));
2762 // Check that the left hand is a JS object.
2763 __ JumpIfSmi(object, ¬_js_object);
2764 __ IsObjectJSObjectType(object, map, scratch, ¬_js_object);
2766 // If there is a call site cache don't look in the global cache, but do the
2767 // real lookup and update the call site cache.
2768 if (!HasCallSiteInlineCheck() && !ReturnTrueFalseObject()) {
2769 // Look up the function and the map in the instanceof cache.
2771 __ CompareRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2772 __ j(not_equal, &miss, Label::kNear);
2773 __ CompareRoot(map, scratch, Heap::kInstanceofCacheMapRootIndex);
2774 __ j(not_equal, &miss, Label::kNear);
2775 __ LoadRoot(eax, Heap::kInstanceofCacheAnswerRootIndex);
2776 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2780 // Get the prototype of the function.
2781 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
2783 // Check that the function prototype is a JS object.
2784 __ JumpIfSmi(prototype, &slow);
2785 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
2787 // Update the global instanceof or call site inlined cache with the current
2788 // map and function. The cached answer will be set when it is known below.
2789 if (!HasCallSiteInlineCheck()) {
2790 __ StoreRoot(map, scratch, Heap::kInstanceofCacheMapRootIndex);
2791 __ StoreRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2793 // The constants for the code patching are based on no push instructions
2794 // at the call site.
2795 DCHECK(HasArgsInRegisters());
2796 // Get return address and delta to inlined map check.
2797 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2798 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2799 if (FLAG_debug_code) {
2800 __ cmpb(Operand(scratch, 0), kCmpEdiOperandByte1);
2801 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCmp1);
2802 __ cmpb(Operand(scratch, 1), kCmpEdiOperandByte2);
2803 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCmp2);
2805 __ mov(scratch, Operand(scratch, kDeltaToCmpImmediate));
2806 __ mov(Operand(scratch, 0), map);
2808 // Scratch points at the cell payload. Calculate the start of the object.
2809 __ sub(scratch, Immediate(Cell::kValueOffset - 1));
2810 __ RecordWriteField(scratch, Cell::kValueOffset, map, function,
2811 kDontSaveFPRegs, OMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2815 // Loop through the prototype chain of the object looking for the function
2817 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
2818 Label loop, is_instance, is_not_instance;
2820 __ cmp(scratch, prototype);
2821 __ j(equal, &is_instance, Label::kNear);
2822 Factory* factory = isolate()->factory();
2823 __ cmp(scratch, Immediate(factory->null_value()));
2824 __ j(equal, &is_not_instance, Label::kNear);
2825 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
2826 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
2829 __ bind(&is_instance);
2830 if (!HasCallSiteInlineCheck()) {
2831 __ mov(eax, Immediate(0));
2832 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
2833 if (ReturnTrueFalseObject()) {
2834 __ mov(eax, factory->true_value());
2837 // Get return address and delta to inlined map check.
2838 __ mov(eax, factory->true_value());
2839 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2840 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2841 if (FLAG_debug_code) {
2842 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
2843 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2845 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
2846 if (!ReturnTrueFalseObject()) {
2847 __ Move(eax, Immediate(0));
2850 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2852 __ bind(&is_not_instance);
2853 if (!HasCallSiteInlineCheck()) {
2854 __ mov(eax, Immediate(Smi::FromInt(1)));
2855 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
2856 if (ReturnTrueFalseObject()) {
2857 __ mov(eax, factory->false_value());
2860 // Get return address and delta to inlined map check.
2861 __ mov(eax, factory->false_value());
2862 __ mov(scratch, Operand(esp, 0 * kPointerSize));
2863 __ sub(scratch, Operand(esp, 1 * kPointerSize));
2864 if (FLAG_debug_code) {
2865 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
2866 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2868 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
2869 if (!ReturnTrueFalseObject()) {
2870 __ Move(eax, Immediate(Smi::FromInt(1)));
2873 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2875 Label object_not_null, object_not_null_or_smi;
2876 __ bind(¬_js_object);
2877 // Before null, smi and string value checks, check that the rhs is a function
2878 // as for a non-function rhs an exception needs to be thrown.
2879 __ JumpIfSmi(function, &slow, Label::kNear);
2880 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
2881 __ j(not_equal, &slow, Label::kNear);
2883 // Null is not instance of anything.
2884 __ cmp(object, factory->null_value());
2885 __ j(not_equal, &object_not_null, Label::kNear);
2886 if (ReturnTrueFalseObject()) {
2887 __ mov(eax, factory->false_value());
2889 __ Move(eax, Immediate(Smi::FromInt(1)));
2891 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2893 __ bind(&object_not_null);
2894 // Smi values is not instance of anything.
2895 __ JumpIfNotSmi(object, &object_not_null_or_smi, Label::kNear);
2896 if (ReturnTrueFalseObject()) {
2897 __ mov(eax, factory->false_value());
2899 __ Move(eax, Immediate(Smi::FromInt(1)));
2901 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2903 __ bind(&object_not_null_or_smi);
2904 // String values is not instance of anything.
2905 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
2906 __ j(NegateCondition(is_string), &slow, Label::kNear);
2907 if (ReturnTrueFalseObject()) {
2908 __ mov(eax, factory->false_value());
2910 __ Move(eax, Immediate(Smi::FromInt(1)));
2912 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2914 // Slow-case: Go through the JavaScript implementation.
2916 if (!ReturnTrueFalseObject()) {
2917 // Tail call the builtin which returns 0 or 1.
2918 if (HasArgsInRegisters()) {
2919 // Push arguments below return address.
2925 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
2927 // Call the builtin and convert 0/1 to true/false.
2929 FrameScope scope(masm, StackFrame::INTERNAL);
2932 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
2934 Label true_value, done;
2936 __ j(zero, &true_value, Label::kNear);
2937 __ mov(eax, factory->false_value());
2938 __ jmp(&done, Label::kNear);
2939 __ bind(&true_value);
2940 __ mov(eax, factory->true_value());
2942 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2947 // -------------------------------------------------------------------------
2948 // StringCharCodeAtGenerator
2950 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
2951 // If the receiver is a smi trigger the non-string case.
2952 STATIC_ASSERT(kSmiTag == 0);
2953 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2954 __ JumpIfSmi(object_, receiver_not_string_);
2956 // Fetch the instance type of the receiver into result register.
2957 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
2958 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2959 // If the receiver is not a string trigger the non-string case.
2960 __ test(result_, Immediate(kIsNotStringMask));
2961 __ j(not_zero, receiver_not_string_);
2964 // If the index is non-smi trigger the non-smi case.
2965 STATIC_ASSERT(kSmiTag == 0);
2966 __ JumpIfNotSmi(index_, &index_not_smi_);
2967 __ bind(&got_smi_index_);
2969 // Check for index out of range.
2970 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
2971 __ j(above_equal, index_out_of_range_);
2973 __ SmiUntag(index_);
2975 Factory* factory = masm->isolate()->factory();
2976 StringCharLoadGenerator::Generate(
2977 masm, factory, object_, index_, result_, &call_runtime_);
2984 void StringCharCodeAtGenerator::GenerateSlow(
2985 MacroAssembler* masm, EmbedMode embed_mode,
2986 const RuntimeCallHelper& call_helper) {
2987 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
2989 // Index is not a smi.
2990 __ bind(&index_not_smi_);
2991 // If index is a heap number, try converting it to an integer.
2993 masm->isolate()->factory()->heap_number_map(),
2996 call_helper.BeforeCall(masm);
2997 if (embed_mode == PART_OF_IC_HANDLER) {
2998 __ push(LoadWithVectorDescriptor::VectorRegister());
2999 __ push(LoadDescriptor::SlotRegister());
3002 __ push(index_); // Consumed by runtime conversion function.
3003 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3004 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3006 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3007 // NumberToSmi discards numbers that are not exact integers.
3008 __ CallRuntime(Runtime::kNumberToSmi, 1);
3010 if (!index_.is(eax)) {
3011 // Save the conversion result before the pop instructions below
3012 // have a chance to overwrite it.
3013 __ mov(index_, eax);
3016 if (embed_mode == PART_OF_IC_HANDLER) {
3017 __ pop(LoadDescriptor::SlotRegister());
3018 __ pop(LoadWithVectorDescriptor::VectorRegister());
3020 // Reload the instance type.
3021 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
3022 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3023 call_helper.AfterCall(masm);
3024 // If index is still not a smi, it must be out of range.
3025 STATIC_ASSERT(kSmiTag == 0);
3026 __ JumpIfNotSmi(index_, index_out_of_range_);
3027 // Otherwise, return to the fast path.
3028 __ jmp(&got_smi_index_);
3030 // Call runtime. We get here when the receiver is a string and the
3031 // index is a number, but the code of getting the actual character
3032 // is too complex (e.g., when the string needs to be flattened).
3033 __ bind(&call_runtime_);
3034 call_helper.BeforeCall(masm);
3038 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
3039 if (!result_.is(eax)) {
3040 __ mov(result_, eax);
3042 call_helper.AfterCall(masm);
3045 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
3049 // -------------------------------------------------------------------------
3050 // StringCharFromCodeGenerator
3052 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3053 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3054 STATIC_ASSERT(kSmiTag == 0);
3055 STATIC_ASSERT(kSmiShiftSize == 0);
3056 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
3057 __ test(code_, Immediate(kSmiTagMask |
3058 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
3059 __ j(not_zero, &slow_case_);
3061 Factory* factory = masm->isolate()->factory();
3062 __ Move(result_, Immediate(factory->single_character_string_cache()));
3063 STATIC_ASSERT(kSmiTag == 0);
3064 STATIC_ASSERT(kSmiTagSize == 1);
3065 STATIC_ASSERT(kSmiShiftSize == 0);
3066 // At this point code register contains smi tagged one byte char code.
3067 __ mov(result_, FieldOperand(result_,
3068 code_, times_half_pointer_size,
3069 FixedArray::kHeaderSize));
3070 __ cmp(result_, factory->undefined_value());
3071 __ j(equal, &slow_case_);
3076 void StringCharFromCodeGenerator::GenerateSlow(
3077 MacroAssembler* masm,
3078 const RuntimeCallHelper& call_helper) {
3079 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
3081 __ bind(&slow_case_);
3082 call_helper.BeforeCall(masm);
3084 __ CallRuntime(Runtime::kCharFromCode, 1);
3085 if (!result_.is(eax)) {
3086 __ mov(result_, eax);
3088 call_helper.AfterCall(masm);
3091 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
3095 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3100 String::Encoding encoding) {
3101 DCHECK(!scratch.is(dest));
3102 DCHECK(!scratch.is(src));
3103 DCHECK(!scratch.is(count));
3105 // Nothing to do for zero characters.
3107 __ test(count, count);
3110 // Make count the number of bytes to copy.
3111 if (encoding == String::TWO_BYTE_ENCODING) {
3117 __ mov_b(scratch, Operand(src, 0));
3118 __ mov_b(Operand(dest, 0), scratch);
3122 __ j(not_zero, &loop);
3128 void SubStringStub::Generate(MacroAssembler* masm) {
3131 // Stack frame on entry.
3132 // esp[0]: return address
3137 // Make sure first argument is a string.
3138 __ mov(eax, Operand(esp, 3 * kPointerSize));
3139 STATIC_ASSERT(kSmiTag == 0);
3140 __ JumpIfSmi(eax, &runtime);
3141 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3142 __ j(NegateCondition(is_string), &runtime);
3145 // ebx: instance type
3147 // Calculate length of sub string using the smi values.
3148 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
3149 __ JumpIfNotSmi(ecx, &runtime);
3150 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
3151 __ JumpIfNotSmi(edx, &runtime);
3153 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
3154 Label not_original_string;
3155 // Shorter than original string's length: an actual substring.
3156 __ j(below, ¬_original_string, Label::kNear);
3157 // Longer than original string's length or negative: unsafe arguments.
3158 __ j(above, &runtime);
3159 // Return original string.
3160 Counters* counters = isolate()->counters();
3161 __ IncrementCounter(counters->sub_string_native(), 1);
3162 __ ret(3 * kPointerSize);
3163 __ bind(¬_original_string);
3166 __ cmp(ecx, Immediate(Smi::FromInt(1)));
3167 __ j(equal, &single_char);
3170 // ebx: instance type
3171 // ecx: sub string length (smi)
3172 // edx: from index (smi)
3173 // Deal with different string types: update the index if necessary
3174 // and put the underlying string into edi.
3175 Label underlying_unpacked, sliced_string, seq_or_external_string;
3176 // If the string is not indirect, it can only be sequential or external.
3177 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3178 STATIC_ASSERT(kIsIndirectStringMask != 0);
3179 __ test(ebx, Immediate(kIsIndirectStringMask));
3180 __ j(zero, &seq_or_external_string, Label::kNear);
3182 Factory* factory = isolate()->factory();
3183 __ test(ebx, Immediate(kSlicedNotConsMask));
3184 __ j(not_zero, &sliced_string, Label::kNear);
3185 // Cons string. Check whether it is flat, then fetch first part.
3186 // Flat cons strings have an empty second part.
3187 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
3188 factory->empty_string());
3189 __ j(not_equal, &runtime);
3190 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
3191 // Update instance type.
3192 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
3193 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3194 __ jmp(&underlying_unpacked, Label::kNear);
3196 __ bind(&sliced_string);
3197 // Sliced string. Fetch parent and adjust start index by offset.
3198 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
3199 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
3200 // Update instance type.
3201 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
3202 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3203 __ jmp(&underlying_unpacked, Label::kNear);
3205 __ bind(&seq_or_external_string);
3206 // Sequential or external string. Just move string to the expected register.
3209 __ bind(&underlying_unpacked);
3211 if (FLAG_string_slices) {
3213 // edi: underlying subject string
3214 // ebx: instance type of underlying subject string
3215 // edx: adjusted start index (smi)
3216 // ecx: length (smi)
3217 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
3218 // Short slice. Copy instead of slicing.
3219 __ j(less, ©_routine);
3220 // Allocate new sliced string. At this point we do not reload the instance
3221 // type including the string encoding because we simply rely on the info
3222 // provided by the original string. It does not matter if the original
3223 // string's encoding is wrong because we always have to recheck encoding of
3224 // the newly created string's parent anyways due to externalized strings.
3225 Label two_byte_slice, set_slice_header;
3226 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
3227 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3228 __ test(ebx, Immediate(kStringEncodingMask));
3229 __ j(zero, &two_byte_slice, Label::kNear);
3230 __ AllocateOneByteSlicedString(eax, ebx, no_reg, &runtime);
3231 __ jmp(&set_slice_header, Label::kNear);
3232 __ bind(&two_byte_slice);
3233 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
3234 __ bind(&set_slice_header);
3235 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
3236 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
3237 Immediate(String::kEmptyHashField));
3238 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
3239 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
3240 __ IncrementCounter(counters->sub_string_native(), 1);
3241 __ ret(3 * kPointerSize);
3243 __ bind(©_routine);
3246 // edi: underlying subject string
3247 // ebx: instance type of underlying subject string
3248 // edx: adjusted start index (smi)
3249 // ecx: length (smi)
3250 // The subject string can only be external or sequential string of either
3251 // encoding at this point.
3252 Label two_byte_sequential, runtime_drop_two, sequential_string;
3253 STATIC_ASSERT(kExternalStringTag != 0);
3254 STATIC_ASSERT(kSeqStringTag == 0);
3255 __ test_b(ebx, kExternalStringTag);
3256 __ j(zero, &sequential_string);
3258 // Handle external string.
3259 // Rule out short external strings.
3260 STATIC_ASSERT(kShortExternalStringTag != 0);
3261 __ test_b(ebx, kShortExternalStringMask);
3262 __ j(not_zero, &runtime);
3263 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
3264 // Move the pointer so that offset-wise, it looks like a sequential string.
3265 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3266 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3268 __ bind(&sequential_string);
3269 // Stash away (adjusted) index and (underlying) string.
3273 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
3274 __ test_b(ebx, kStringEncodingMask);
3275 __ j(zero, &two_byte_sequential);
3277 // Sequential one byte string. Allocate the result.
3278 __ AllocateOneByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
3280 // eax: result string
3281 // ecx: result string length
3282 // Locate first character of result.
3284 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3285 // Load string argument and locate character of sub string start.
3289 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqOneByteString::kHeaderSize));
3291 // eax: result string
3292 // ecx: result length
3293 // edi: first character of result
3294 // edx: character of sub string start
3295 StringHelper::GenerateCopyCharacters(
3296 masm, edi, edx, ecx, ebx, String::ONE_BYTE_ENCODING);
3297 __ IncrementCounter(counters->sub_string_native(), 1);
3298 __ ret(3 * kPointerSize);
3300 __ bind(&two_byte_sequential);
3301 // Sequential two-byte string. Allocate the result.
3302 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
3304 // eax: result string
3305 // ecx: result string length
3306 // Locate first character of result.
3309 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3310 // Load string argument and locate character of sub string start.
3313 // As from is a smi it is 2 times the value which matches the size of a two
3315 STATIC_ASSERT(kSmiTag == 0);
3316 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3317 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqTwoByteString::kHeaderSize));
3319 // eax: result string
3320 // ecx: result length
3321 // edi: first character of result
3322 // edx: character of sub string start
3323 StringHelper::GenerateCopyCharacters(
3324 masm, edi, edx, ecx, ebx, String::TWO_BYTE_ENCODING);
3325 __ IncrementCounter(counters->sub_string_native(), 1);
3326 __ ret(3 * kPointerSize);
3328 // Drop pushed values on the stack before tail call.
3329 __ bind(&runtime_drop_two);
3332 // Just jump to runtime to create the sub string.
3334 __ TailCallRuntime(Runtime::kSubStringRT, 3, 1);
3336 __ bind(&single_char);
3338 // ebx: instance type
3339 // ecx: sub string length (smi)
3340 // edx: from index (smi)
3341 StringCharAtGenerator generator(eax, edx, ecx, eax, &runtime, &runtime,
3342 &runtime, STRING_INDEX_IS_NUMBER,
3343 RECEIVER_IS_STRING);
3344 generator.GenerateFast(masm);
3345 __ ret(3 * kPointerSize);
3346 generator.SkipSlow(masm, &runtime);
3350 void ToNumberStub::Generate(MacroAssembler* masm) {
3351 // The ToNumber stub takes one argument in eax.
3353 __ JumpIfNotSmi(eax, ¬_smi, Label::kNear);
3357 Label not_heap_number;
3358 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
3359 __ j(not_equal, ¬_heap_number, Label::kNear);
3361 __ bind(¬_heap_number);
3363 Label not_string, slow_string;
3364 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
3367 __ j(above_equal, ¬_string, Label::kNear);
3368 // Check if string has a cached array index.
3369 __ test(FieldOperand(eax, String::kHashFieldOffset),
3370 Immediate(String::kContainsCachedArrayIndexMask));
3371 __ j(not_zero, &slow_string, Label::kNear);
3372 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3373 __ IndexFromHash(eax, eax);
3375 __ bind(&slow_string);
3376 __ pop(ecx); // Pop return address.
3377 __ push(eax); // Push argument.
3378 __ push(ecx); // Push return address.
3379 __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
3380 __ bind(¬_string);
3383 __ CmpInstanceType(edi, ODDBALL_TYPE);
3384 __ j(not_equal, ¬_oddball, Label::kNear);
3385 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
3387 __ bind(¬_oddball);
3389 __ pop(ecx); // Pop return address.
3390 __ push(eax); // Push argument.
3391 __ push(ecx); // Push return address.
3392 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
3396 void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
3400 Register scratch2) {
3401 Register length = scratch1;
3404 Label strings_not_equal, check_zero_length;
3405 __ mov(length, FieldOperand(left, String::kLengthOffset));
3406 __ cmp(length, FieldOperand(right, String::kLengthOffset));
3407 __ j(equal, &check_zero_length, Label::kNear);
3408 __ bind(&strings_not_equal);
3409 __ Move(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
3412 // Check if the length is zero.
3413 Label compare_chars;
3414 __ bind(&check_zero_length);
3415 STATIC_ASSERT(kSmiTag == 0);
3416 __ test(length, length);
3417 __ j(not_zero, &compare_chars, Label::kNear);
3418 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3421 // Compare characters.
3422 __ bind(&compare_chars);
3423 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
3424 &strings_not_equal, Label::kNear);
3426 // Characters are equal.
3427 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3432 void StringHelper::GenerateCompareFlatOneByteStrings(
3433 MacroAssembler* masm, Register left, Register right, Register scratch1,
3434 Register scratch2, Register scratch3) {
3435 Counters* counters = masm->isolate()->counters();
3436 __ IncrementCounter(counters->string_compare_native(), 1);
3438 // Find minimum length.
3440 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
3441 __ mov(scratch3, scratch1);
3442 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
3444 Register length_delta = scratch3;
3446 __ j(less_equal, &left_shorter, Label::kNear);
3447 // Right string is shorter. Change scratch1 to be length of right string.
3448 __ sub(scratch1, length_delta);
3449 __ bind(&left_shorter);
3451 Register min_length = scratch1;
3453 // If either length is zero, just compare lengths.
3454 Label compare_lengths;
3455 __ test(min_length, min_length);
3456 __ j(zero, &compare_lengths, Label::kNear);
3458 // Compare characters.
3459 Label result_not_equal;
3460 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
3461 &result_not_equal, Label::kNear);
3463 // Compare lengths - strings up to min-length are equal.
3464 __ bind(&compare_lengths);
3465 __ test(length_delta, length_delta);
3466 Label length_not_equal;
3467 __ j(not_zero, &length_not_equal, Label::kNear);
3470 STATIC_ASSERT(EQUAL == 0);
3471 STATIC_ASSERT(kSmiTag == 0);
3472 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3475 Label result_greater;
3477 __ bind(&length_not_equal);
3478 __ j(greater, &result_greater, Label::kNear);
3479 __ jmp(&result_less, Label::kNear);
3480 __ bind(&result_not_equal);
3481 __ j(above, &result_greater, Label::kNear);
3482 __ bind(&result_less);
3485 __ Move(eax, Immediate(Smi::FromInt(LESS)));
3488 // Result is GREATER.
3489 __ bind(&result_greater);
3490 __ Move(eax, Immediate(Smi::FromInt(GREATER)));
3495 void StringHelper::GenerateOneByteCharsCompareLoop(
3496 MacroAssembler* masm, Register left, Register right, Register length,
3497 Register scratch, Label* chars_not_equal,
3498 Label::Distance chars_not_equal_near) {
3499 // Change index to run from -length to -1 by adding length to string
3500 // start. This means that loop ends when index reaches zero, which
3501 // doesn't need an additional compare.
3502 __ SmiUntag(length);
3504 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
3506 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
3508 Register index = length; // index = -length;
3513 __ mov_b(scratch, Operand(left, index, times_1, 0));
3514 __ cmpb(scratch, Operand(right, index, times_1, 0));
3515 __ j(not_equal, chars_not_equal, chars_not_equal_near);
3517 __ j(not_zero, &loop);
3521 void StringCompareStub::Generate(MacroAssembler* masm) {
3524 // Stack frame on entry.
3525 // esp[0]: return address
3526 // esp[4]: right string
3527 // esp[8]: left string
3529 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
3530 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
3534 __ j(not_equal, ¬_same, Label::kNear);
3535 STATIC_ASSERT(EQUAL == 0);
3536 STATIC_ASSERT(kSmiTag == 0);
3537 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3538 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1);
3539 __ ret(2 * kPointerSize);
3543 // Check that both objects are sequential one-byte strings.
3544 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx, &runtime);
3546 // Compare flat one-byte strings.
3547 // Drop arguments from the stack.
3549 __ add(esp, Immediate(2 * kPointerSize));
3551 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
3554 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
3555 // tagged as a small integer.
3557 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3561 void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3562 // ----------- S t a t e -------------
3565 // -- esp[0] : return address
3566 // -----------------------------------
3568 // Load ecx with the allocation site. We stick an undefined dummy value here
3569 // and replace it with the real allocation site later when we instantiate this
3570 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3571 __ mov(ecx, handle(isolate()->heap()->undefined_value()));
3573 // Make sure that we actually patched the allocation site.
3574 if (FLAG_debug_code) {
3575 __ test(ecx, Immediate(kSmiTagMask));
3576 __ Assert(not_equal, kExpectedAllocationSite);
3577 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
3578 isolate()->factory()->allocation_site_map());
3579 __ Assert(equal, kExpectedAllocationSite);
3582 // Tail call into the stub that handles binary operations with allocation
3584 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3585 __ TailCallStub(&stub);
3589 void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3590 DCHECK(state() == CompareICState::SMI);
3594 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
3596 if (GetCondition() == equal) {
3597 // For equality we do not care about the sign of the result.
3602 __ j(no_overflow, &done, Label::kNear);
3603 // Correct sign of result in case of overflow.
3615 void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3616 DCHECK(state() == CompareICState::NUMBER);
3619 Label unordered, maybe_undefined1, maybe_undefined2;
3622 if (left() == CompareICState::SMI) {
3623 __ JumpIfNotSmi(edx, &miss);
3625 if (right() == CompareICState::SMI) {
3626 __ JumpIfNotSmi(eax, &miss);
3629 // Load left and right operand.
3630 Label done, left, left_smi, right_smi;
3631 __ JumpIfSmi(eax, &right_smi, Label::kNear);
3632 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
3633 isolate()->factory()->heap_number_map());
3634 __ j(not_equal, &maybe_undefined1, Label::kNear);
3635 __ movsd(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3636 __ jmp(&left, Label::kNear);
3637 __ bind(&right_smi);
3638 __ mov(ecx, eax); // Can't clobber eax because we can still jump away.
3640 __ Cvtsi2sd(xmm1, ecx);
3643 __ JumpIfSmi(edx, &left_smi, Label::kNear);
3644 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
3645 isolate()->factory()->heap_number_map());
3646 __ j(not_equal, &maybe_undefined2, Label::kNear);
3647 __ movsd(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3650 __ mov(ecx, edx); // Can't clobber edx because we can still jump away.
3652 __ Cvtsi2sd(xmm0, ecx);
3655 // Compare operands.
3656 __ ucomisd(xmm0, xmm1);
3658 // Don't base result on EFLAGS when a NaN is involved.
3659 __ j(parity_even, &unordered, Label::kNear);
3661 // Return a result of -1, 0, or 1, based on EFLAGS.
3662 // Performing mov, because xor would destroy the flag register.
3663 __ mov(eax, 0); // equal
3664 __ mov(ecx, Immediate(Smi::FromInt(1)));
3665 __ cmov(above, eax, ecx);
3666 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3667 __ cmov(below, eax, ecx);
3670 __ bind(&unordered);
3671 __ bind(&generic_stub);
3672 CompareICStub stub(isolate(), op(), strength(), CompareICState::GENERIC,
3673 CompareICState::GENERIC, CompareICState::GENERIC);
3674 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3676 __ bind(&maybe_undefined1);
3677 if (Token::IsOrderedRelationalCompareOp(op())) {
3678 __ cmp(eax, Immediate(isolate()->factory()->undefined_value()));
3679 __ j(not_equal, &miss);
3680 __ JumpIfSmi(edx, &unordered);
3681 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
3682 __ j(not_equal, &maybe_undefined2, Label::kNear);
3686 __ bind(&maybe_undefined2);
3687 if (Token::IsOrderedRelationalCompareOp(op())) {
3688 __ cmp(edx, Immediate(isolate()->factory()->undefined_value()));
3689 __ j(equal, &unordered);
3697 void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3698 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3699 DCHECK(GetCondition() == equal);
3701 // Registers containing left and right operands respectively.
3702 Register left = edx;
3703 Register right = eax;
3704 Register tmp1 = ecx;
3705 Register tmp2 = ebx;
3707 // Check that both operands are heap objects.
3710 STATIC_ASSERT(kSmiTag == 0);
3711 __ and_(tmp1, right);
3712 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3714 // Check that both operands are internalized strings.
3715 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3716 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3717 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3718 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3719 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3721 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
3722 __ j(not_zero, &miss, Label::kNear);
3724 // Internalized strings are compared by identity.
3726 __ cmp(left, right);
3727 // Make sure eax is non-zero. At this point input operands are
3728 // guaranteed to be non-zero.
3729 DCHECK(right.is(eax));
3730 __ j(not_equal, &done, Label::kNear);
3731 STATIC_ASSERT(EQUAL == 0);
3732 STATIC_ASSERT(kSmiTag == 0);
3733 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3742 void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3743 DCHECK(state() == CompareICState::UNIQUE_NAME);
3744 DCHECK(GetCondition() == equal);
3746 // Registers containing left and right operands respectively.
3747 Register left = edx;
3748 Register right = eax;
3749 Register tmp1 = ecx;
3750 Register tmp2 = ebx;
3752 // Check that both operands are heap objects.
3755 STATIC_ASSERT(kSmiTag == 0);
3756 __ and_(tmp1, right);
3757 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3759 // Check that both operands are unique names. This leaves the instance
3760 // types loaded in tmp1 and tmp2.
3761 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3762 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3763 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3764 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3766 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
3767 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
3769 // Unique names are compared by identity.
3771 __ cmp(left, right);
3772 // Make sure eax is non-zero. At this point input operands are
3773 // guaranteed to be non-zero.
3774 DCHECK(right.is(eax));
3775 __ j(not_equal, &done, Label::kNear);
3776 STATIC_ASSERT(EQUAL == 0);
3777 STATIC_ASSERT(kSmiTag == 0);
3778 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3787 void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3788 DCHECK(state() == CompareICState::STRING);
3791 bool equality = Token::IsEqualityOp(op());
3793 // Registers containing left and right operands respectively.
3794 Register left = edx;
3795 Register right = eax;
3796 Register tmp1 = ecx;
3797 Register tmp2 = ebx;
3798 Register tmp3 = edi;
3800 // Check that both operands are heap objects.
3802 STATIC_ASSERT(kSmiTag == 0);
3803 __ and_(tmp1, right);
3804 __ JumpIfSmi(tmp1, &miss);
3806 // Check that both operands are strings. This leaves the instance
3807 // types loaded in tmp1 and tmp2.
3808 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3809 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3810 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3811 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3813 STATIC_ASSERT(kNotStringTag != 0);
3815 __ test(tmp3, Immediate(kIsNotStringMask));
3816 __ j(not_zero, &miss);
3818 // Fast check for identical strings.
3820 __ cmp(left, right);
3821 __ j(not_equal, ¬_same, Label::kNear);
3822 STATIC_ASSERT(EQUAL == 0);
3823 STATIC_ASSERT(kSmiTag == 0);
3824 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3827 // Handle not identical strings.
3830 // Check that both strings are internalized. If they are, we're done
3831 // because we already know they are not identical. But in the case of
3832 // non-equality compare, we still need to determine the order. We
3833 // also know they are both strings.
3836 STATIC_ASSERT(kInternalizedTag == 0);
3838 __ test(tmp1, Immediate(kIsNotInternalizedMask));
3839 __ j(not_zero, &do_compare, Label::kNear);
3840 // Make sure eax is non-zero. At this point input operands are
3841 // guaranteed to be non-zero.
3842 DCHECK(right.is(eax));
3844 __ bind(&do_compare);
3847 // Check that both strings are sequential one-byte.
3849 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
3851 // Compare flat one byte strings. Returns when done.
3853 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
3856 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3860 // Handle more complex cases in runtime.
3862 __ pop(tmp1); // Return address.
3867 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3869 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3877 void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3878 DCHECK(state() == CompareICState::OBJECT);
3882 __ JumpIfSmi(ecx, &miss, Label::kNear);
3884 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
3885 __ j(not_equal, &miss, Label::kNear);
3886 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
3887 __ j(not_equal, &miss, Label::kNear);
3889 DCHECK(GetCondition() == equal);
3898 void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
3900 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
3903 __ JumpIfSmi(ecx, &miss, Label::kNear);
3905 __ GetWeakValue(edi, cell);
3906 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3907 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
3909 __ j(not_equal, &miss, Label::kNear);
3911 __ j(not_equal, &miss, Label::kNear);
3921 void CompareICStub::GenerateMiss(MacroAssembler* masm) {
3923 // Call the runtime system in a fresh internal frame.
3924 FrameScope scope(masm, StackFrame::INTERNAL);
3925 __ push(edx); // Preserve edx and eax.
3927 __ push(edx); // And also use them as the arguments.
3929 __ push(Immediate(Smi::FromInt(op())));
3930 __ CallRuntime(Runtime::kCompareIC_Miss, 3);
3931 // Compute the entry point of the rewritten stub.
3932 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
3937 // Do a tail call to the rewritten stub.
3942 // Helper function used to check that the dictionary doesn't contain
3943 // the property. This function may return false negatives, so miss_label
3944 // must always call a backup property check that is complete.
3945 // This function is safe to call if the receiver has fast properties.
3946 // Name must be a unique name and receiver must be a heap object.
3947 void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3950 Register properties,
3953 DCHECK(name->IsUniqueName());
3955 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3956 // not equal to the name and kProbes-th slot is not used (its name is the
3957 // undefined value), it guarantees the hash table doesn't contain the
3958 // property. It's true even if some slots represent deleted properties
3959 // (their names are the hole value).
3960 for (int i = 0; i < kInlinedProbes; i++) {
3961 // Compute the masked index: (hash + i + i * i) & mask.
3962 Register index = r0;
3963 // Capacity is smi 2^n.
3964 __ mov(index, FieldOperand(properties, kCapacityOffset));
3967 Immediate(Smi::FromInt(name->Hash() +
3968 NameDictionary::GetProbeOffset(i))));
3970 // Scale the index by multiplying by the entry size.
3971 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3972 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
3973 Register entity_name = r0;
3974 // Having undefined at this place means the name is not contained.
3975 STATIC_ASSERT(kSmiTagSize == 1);
3976 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
3977 kElementsStartOffset - kHeapObjectTag));
3978 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
3981 // Stop if found the property.
3982 __ cmp(entity_name, Handle<Name>(name));
3986 // Check for the hole and skip.
3987 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
3988 __ j(equal, &good, Label::kNear);
3990 // Check if the entry name is not a unique name.
3991 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3992 __ JumpIfNotUniqueNameInstanceType(
3993 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3997 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3999 __ push(Immediate(Handle<Object>(name)));
4000 __ push(Immediate(name->Hash()));
4003 __ j(not_zero, miss);
4008 // Probe the name dictionary in the |elements| register. Jump to the
4009 // |done| label if a property with the given name is found leaving the
4010 // index into the dictionary in |r0|. Jump to the |miss| label
4012 void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
4019 DCHECK(!elements.is(r0));
4020 DCHECK(!elements.is(r1));
4021 DCHECK(!name.is(r0));
4022 DCHECK(!name.is(r1));
4024 __ AssertName(name);
4026 __ mov(r1, FieldOperand(elements, kCapacityOffset));
4027 __ shr(r1, kSmiTagSize); // convert smi to int
4030 // Generate an unrolled loop that performs a few probes before
4031 // giving up. Measurements done on Gmail indicate that 2 probes
4032 // cover ~93% of loads from dictionaries.
4033 for (int i = 0; i < kInlinedProbes; i++) {
4034 // Compute the masked index: (hash + i + i * i) & mask.
4035 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
4036 __ shr(r0, Name::kHashShift);
4038 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
4042 // Scale the index by multiplying by the entry size.
4043 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
4044 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
4046 // Check if the key is identical to the name.
4047 __ cmp(name, Operand(elements,
4050 kElementsStartOffset - kHeapObjectTag));
4054 NameDictionaryLookupStub stub(masm->isolate(), elements, r1, r0,
4057 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
4058 __ shr(r0, Name::kHashShift);
4068 void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
4069 // This stub overrides SometimesSetsUpAFrame() to return false. That means
4070 // we cannot call anything that could cause a GC from this stub.
4071 // Stack frame on entry:
4072 // esp[0 * kPointerSize]: return address.
4073 // esp[1 * kPointerSize]: key's hash.
4074 // esp[2 * kPointerSize]: key.
4076 // dictionary_: NameDictionary to probe.
4077 // result_: used as scratch.
4078 // index_: will hold an index of entry if lookup is successful.
4079 // might alias with result_.
4081 // result_ is zero if lookup failed, non zero otherwise.
4083 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
4085 Register scratch = result();
4087 __ mov(scratch, FieldOperand(dictionary(), kCapacityOffset));
4089 __ SmiUntag(scratch);
4092 // If names of slots in range from 1 to kProbes - 1 for the hash value are
4093 // not equal to the name and kProbes-th slot is not used (its name is the
4094 // undefined value), it guarantees the hash table doesn't contain the
4095 // property. It's true even if some slots represent deleted properties
4096 // (their names are the null value).
4097 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
4098 // Compute the masked index: (hash + i + i * i) & mask.
4099 __ mov(scratch, Operand(esp, 2 * kPointerSize));
4101 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
4103 __ and_(scratch, Operand(esp, 0));
4105 // Scale the index by multiplying by the entry size.
4106 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
4107 __ lea(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
4109 // Having undefined at this place means the name is not contained.
4110 STATIC_ASSERT(kSmiTagSize == 1);
4111 __ mov(scratch, Operand(dictionary(), index(), times_pointer_size,
4112 kElementsStartOffset - kHeapObjectTag));
4113 __ cmp(scratch, isolate()->factory()->undefined_value());
4114 __ j(equal, ¬_in_dictionary);
4116 // Stop if found the property.
4117 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
4118 __ j(equal, &in_dictionary);
4120 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
4121 // If we hit a key that is not a unique name during negative
4122 // lookup we have to bailout as this key might be equal to the
4123 // key we are looking for.
4125 // Check if the entry name is not a unique name.
4126 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4127 __ JumpIfNotUniqueNameInstanceType(
4128 FieldOperand(scratch, Map::kInstanceTypeOffset),
4129 &maybe_in_dictionary);
4133 __ bind(&maybe_in_dictionary);
4134 // If we are doing negative lookup then probing failure should be
4135 // treated as a lookup success. For positive lookup probing failure
4136 // should be treated as lookup failure.
4137 if (mode() == POSITIVE_LOOKUP) {
4138 __ mov(result(), Immediate(0));
4140 __ ret(2 * kPointerSize);
4143 __ bind(&in_dictionary);
4144 __ mov(result(), Immediate(1));
4146 __ ret(2 * kPointerSize);
4148 __ bind(¬_in_dictionary);
4149 __ mov(result(), Immediate(0));
4151 __ ret(2 * kPointerSize);
4155 void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
4157 StoreBufferOverflowStub stub(isolate, kDontSaveFPRegs);
4159 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
4164 // Takes the input in 3 registers: address_ value_ and object_. A pointer to
4165 // the value has just been written into the object, now this stub makes sure
4166 // we keep the GC informed. The word in the object where the value has been
4167 // written is in the address register.
4168 void RecordWriteStub::Generate(MacroAssembler* masm) {
4169 Label skip_to_incremental_noncompacting;
4170 Label skip_to_incremental_compacting;
4172 // The first two instructions are generated with labels so as to get the
4173 // offset fixed up correctly by the bind(Label*) call. We patch it back and
4174 // forth between a compare instructions (a nop in this position) and the
4175 // real branch when we start and stop incremental heap marking.
4176 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
4177 __ jmp(&skip_to_incremental_compacting, Label::kFar);
4179 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4180 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4181 MacroAssembler::kReturnAtEnd);
4186 __ bind(&skip_to_incremental_noncompacting);
4187 GenerateIncremental(masm, INCREMENTAL);
4189 __ bind(&skip_to_incremental_compacting);
4190 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4192 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
4193 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
4194 masm->set_byte_at(0, kTwoByteNopInstruction);
4195 masm->set_byte_at(2, kFiveByteNopInstruction);
4199 void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4202 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4203 Label dont_need_remembered_set;
4205 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
4206 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
4208 &dont_need_remembered_set);
4210 __ CheckPageFlag(regs_.object(),
4212 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4214 &dont_need_remembered_set);
4216 // First notify the incremental marker if necessary, then update the
4218 CheckNeedsToInformIncrementalMarker(
4220 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
4222 InformIncrementalMarker(masm);
4223 regs_.Restore(masm);
4224 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4225 MacroAssembler::kReturnAtEnd);
4227 __ bind(&dont_need_remembered_set);
4230 CheckNeedsToInformIncrementalMarker(
4232 kReturnOnNoNeedToInformIncrementalMarker,
4234 InformIncrementalMarker(masm);
4235 regs_.Restore(masm);
4240 void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
4241 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
4242 int argument_count = 3;
4243 __ PrepareCallCFunction(argument_count, regs_.scratch0());
4244 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
4245 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
4246 __ mov(Operand(esp, 2 * kPointerSize),
4247 Immediate(ExternalReference::isolate_address(isolate())));
4249 AllowExternalCallThatCantCauseGC scope(masm);
4251 ExternalReference::incremental_marking_record_write_function(isolate()),
4254 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
4258 void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4259 MacroAssembler* masm,
4260 OnNoNeedToInformIncrementalMarker on_no_need,
4262 Label object_is_black, need_incremental, need_incremental_pop_object;
4264 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
4265 __ and_(regs_.scratch0(), regs_.object());
4266 __ mov(regs_.scratch1(),
4267 Operand(regs_.scratch0(),
4268 MemoryChunk::kWriteBarrierCounterOffset));
4269 __ sub(regs_.scratch1(), Immediate(1));
4270 __ mov(Operand(regs_.scratch0(),
4271 MemoryChunk::kWriteBarrierCounterOffset),
4273 __ j(negative, &need_incremental);
4275 // Let's look at the color of the object: If it is not black we don't have
4276 // to inform the incremental marker.
4277 __ JumpIfBlack(regs_.object(),
4283 regs_.Restore(masm);
4284 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4285 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4286 MacroAssembler::kReturnAtEnd);
4291 __ bind(&object_is_black);
4293 // Get the value from the slot.
4294 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
4296 if (mode == INCREMENTAL_COMPACTION) {
4297 Label ensure_not_white;
4299 __ CheckPageFlag(regs_.scratch0(), // Contains value.
4300 regs_.scratch1(), // Scratch.
4301 MemoryChunk::kEvacuationCandidateMask,
4306 __ CheckPageFlag(regs_.object(),
4307 regs_.scratch1(), // Scratch.
4308 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4313 __ jmp(&need_incremental);
4315 __ bind(&ensure_not_white);
4318 // We need an extra register for this, so we push the object register
4320 __ push(regs_.object());
4321 __ EnsureNotWhite(regs_.scratch0(), // The value.
4322 regs_.scratch1(), // Scratch.
4323 regs_.object(), // Scratch.
4324 &need_incremental_pop_object,
4326 __ pop(regs_.object());
4328 regs_.Restore(masm);
4329 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4330 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4331 MacroAssembler::kReturnAtEnd);
4336 __ bind(&need_incremental_pop_object);
4337 __ pop(regs_.object());
4339 __ bind(&need_incremental);
4341 // Fall through when we need to inform the incremental marker.
4345 void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4346 // ----------- S t a t e -------------
4347 // -- eax : element value to store
4348 // -- ecx : element index as smi
4349 // -- esp[0] : return address
4350 // -- esp[4] : array literal index in function
4351 // -- esp[8] : array literal
4352 // clobbers ebx, edx, edi
4353 // -----------------------------------
4356 Label double_elements;
4358 Label slow_elements;
4359 Label slow_elements_from_double;
4360 Label fast_elements;
4362 // Get array literal index, array literal and its map.
4363 __ mov(edx, Operand(esp, 1 * kPointerSize));
4364 __ mov(ebx, Operand(esp, 2 * kPointerSize));
4365 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
4367 __ CheckFastElements(edi, &double_elements);
4369 // Check for FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS elements
4370 __ JumpIfSmi(eax, &smi_element);
4371 __ CheckFastSmiElements(edi, &fast_elements, Label::kNear);
4373 // Store into the array literal requires a elements transition. Call into
4376 __ bind(&slow_elements);
4377 __ pop(edi); // Pop return address and remember to put back later for tail
4382 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4383 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
4385 __ push(edi); // Return return address so that tail call returns to right
4387 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4389 __ bind(&slow_elements_from_double);
4391 __ jmp(&slow_elements);
4393 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
4394 __ bind(&fast_elements);
4395 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4396 __ lea(ecx, FieldOperand(ebx, ecx, times_half_pointer_size,
4397 FixedArrayBase::kHeaderSize));
4398 __ mov(Operand(ecx, 0), eax);
4399 // Update the write barrier for the array store.
4400 __ RecordWrite(ebx, ecx, eax,
4402 EMIT_REMEMBERED_SET,
4406 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS,
4407 // and value is Smi.
4408 __ bind(&smi_element);
4409 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4410 __ mov(FieldOperand(ebx, ecx, times_half_pointer_size,
4411 FixedArrayBase::kHeaderSize), eax);
4414 // Array literal has ElementsKind of FAST_*_DOUBLE_ELEMENTS.
4415 __ bind(&double_elements);
4418 __ mov(edx, FieldOperand(ebx, JSObject::kElementsOffset));
4419 __ StoreNumberToDoubleElements(eax,
4424 &slow_elements_from_double);
4430 void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4431 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4432 __ call(ces.GetCode(), RelocInfo::CODE_TARGET);
4433 int parameter_count_offset =
4434 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4435 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
4436 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4438 int additional_offset =
4439 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
4440 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
4441 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
4445 void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4446 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4447 LoadICStub stub(isolate(), state());
4448 stub.GenerateForTrampoline(masm);
4452 void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4453 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4454 KeyedLoadICStub stub(isolate(), state());
4455 stub.GenerateForTrampoline(masm);
4459 static void HandleArrayCases(MacroAssembler* masm, Register receiver,
4460 Register key, Register vector, Register slot,
4461 Register feedback, bool is_polymorphic,
4463 // feedback initially contains the feedback array
4464 Label next, next_loop, prepare_next;
4465 Label load_smi_map, compare_map;
4466 Label start_polymorphic;
4471 Register receiver_map = receiver;
4472 Register cached_map = vector;
4474 // Receiver might not be a heap object.
4475 __ JumpIfSmi(receiver, &load_smi_map);
4476 __ mov(receiver_map, FieldOperand(receiver, 0));
4477 __ bind(&compare_map);
4478 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
4480 // A named keyed load might have a 2 element array, all other cases can count
4481 // on an array with at least 2 {map, handler} pairs, so they can go right
4482 // into polymorphic array handling.
4483 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4484 __ j(not_equal, is_polymorphic ? &start_polymorphic : &next);
4486 // found, now call handler.
4487 Register handler = feedback;
4488 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
4491 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4494 if (!is_polymorphic) {
4496 __ cmp(FieldOperand(feedback, FixedArray::kLengthOffset),
4497 Immediate(Smi::FromInt(2)));
4498 __ j(not_equal, &start_polymorphic);
4504 // Polymorphic, we have to loop from 2 to N
4505 __ bind(&start_polymorphic);
4507 Register counter = key;
4508 __ mov(counter, Immediate(Smi::FromInt(2)));
4509 __ bind(&next_loop);
4510 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4511 FixedArray::kHeaderSize));
4512 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4513 __ j(not_equal, &prepare_next);
4514 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
4515 FixedArray::kHeaderSize + kPointerSize));
4519 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4522 __ bind(&prepare_next);
4523 __ add(counter, Immediate(Smi::FromInt(2)));
4524 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
4525 __ j(less, &next_loop);
4527 // We exhausted our array of map handler pairs.
4533 __ bind(&load_smi_map);
4534 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4535 __ jmp(&compare_map);
4539 static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
4540 Register key, Register vector, Register slot,
4541 Register weak_cell, Label* miss) {
4542 // feedback initially contains the feedback array
4543 Label compare_smi_map;
4545 // Move the weak map into the weak_cell register.
4546 Register ic_map = weak_cell;
4547 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
4549 // Receiver might not be a heap object.
4550 __ JumpIfSmi(receiver, &compare_smi_map);
4551 __ cmp(ic_map, FieldOperand(receiver, 0));
4552 __ j(not_equal, miss);
4553 Register handler = weak_cell;
4554 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
4555 FixedArray::kHeaderSize + kPointerSize));
4556 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4559 // In microbenchmarks, it made sense to unroll this code so that the call to
4560 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
4561 __ bind(&compare_smi_map);
4562 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
4563 __ j(not_equal, miss);
4564 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
4565 FixedArray::kHeaderSize + kPointerSize));
4566 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4571 void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
4574 void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4575 GenerateImpl(masm, true);
4579 void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4580 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
4581 Register name = LoadWithVectorDescriptor::NameRegister(); // ecx
4582 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
4583 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
4584 Register scratch = edi;
4585 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4586 FixedArray::kHeaderSize));
4588 // Is it a weak cell?
4590 Label not_array, smi_key, key_okay, miss;
4591 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
4592 __ j(not_equal, &try_array);
4593 HandleMonomorphicCase(masm, receiver, name, vector, slot, scratch, &miss);
4595 // Is it a fixed array?
4596 __ bind(&try_array);
4597 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
4598 __ j(not_equal, ¬_array);
4599 HandleArrayCases(masm, receiver, name, vector, slot, scratch, true, &miss);
4601 __ bind(¬_array);
4602 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4603 __ j(not_equal, &miss);
4606 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4607 Code::ComputeHandlerFlags(Code::LOAD_IC));
4608 masm->isolate()->stub_cache()->GenerateProbe(
4609 masm, Code::LOAD_IC, code_flags, false, receiver, name, vector, scratch);
4614 LoadIC::GenerateMiss(masm);
4618 void KeyedLoadICStub::Generate(MacroAssembler* masm) {
4619 GenerateImpl(masm, false);
4623 void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4624 GenerateImpl(masm, true);
4628 void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4629 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
4630 Register key = LoadWithVectorDescriptor::NameRegister(); // ecx
4631 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
4632 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
4633 Register feedback = edi;
4634 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
4635 FixedArray::kHeaderSize));
4636 // Is it a weak cell?
4638 Label not_array, smi_key, key_okay, miss;
4639 __ CompareRoot(FieldOperand(feedback, 0), Heap::kWeakCellMapRootIndex);
4640 __ j(not_equal, &try_array);
4641 HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, &miss);
4643 __ bind(&try_array);
4644 // Is it a fixed array?
4645 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
4646 __ j(not_equal, ¬_array);
4648 // We have a polymorphic element handler.
4649 Label polymorphic, try_poly_name;
4650 __ bind(&polymorphic);
4651 HandleArrayCases(masm, receiver, key, vector, slot, feedback, true, &miss);
4653 __ bind(¬_array);
4655 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
4656 __ j(not_equal, &try_poly_name);
4657 Handle<Code> megamorphic_stub =
4658 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4659 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4661 __ bind(&try_poly_name);
4662 // We might have a name in feedback, and a fixed array in the next slot.
4663 __ cmp(key, feedback);
4664 __ j(not_equal, &miss);
4665 // If the name comparison succeeded, we know we have a fixed array with
4666 // at least one map/handler pair.
4667 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
4668 FixedArray::kHeaderSize + kPointerSize));
4669 HandleArrayCases(masm, receiver, key, vector, slot, feedback, false, &miss);
4672 KeyedLoadIC::GenerateMiss(masm);
4676 void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4677 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4678 VectorStoreICStub stub(isolate(), state());
4679 stub.GenerateForTrampoline(masm);
4683 void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4684 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4685 VectorKeyedStoreICStub stub(isolate(), state());
4686 stub.GenerateForTrampoline(masm);
4690 void VectorStoreICStub::Generate(MacroAssembler* masm) {
4691 GenerateImpl(masm, false);
4695 void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4696 GenerateImpl(masm, true);
4700 void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4703 // TODO(mvstanton): Implement.
4705 StoreIC::GenerateMiss(masm);
4709 void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4710 GenerateImpl(masm, false);
4714 void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4715 GenerateImpl(masm, true);
4719 void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4722 // TODO(mvstanton): Implement.
4724 KeyedStoreIC::GenerateMiss(masm);
4728 void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4729 EmitLoadTypeFeedbackVector(masm, ebx);
4730 CallICStub stub(isolate(), state());
4731 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4735 void CallIC_ArrayTrampolineStub::Generate(MacroAssembler* masm) {
4736 EmitLoadTypeFeedbackVector(masm, ebx);
4737 CallIC_ArrayStub stub(isolate(), state());
4738 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4742 void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4743 if (masm->isolate()->function_entry_hook() != NULL) {
4744 ProfileEntryHookStub stub(masm->isolate());
4745 masm->CallStub(&stub);
4750 void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4751 // Save volatile registers.
4752 const int kNumSavedRegisters = 3;
4757 // Calculate and push the original stack pointer.
4758 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4761 // Retrieve our return address and use it to calculate the calling
4762 // function's address.
4763 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4764 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
4767 // Call the entry hook.
4768 DCHECK(isolate()->function_entry_hook() != NULL);
4769 __ call(FUNCTION_ADDR(isolate()->function_entry_hook()),
4770 RelocInfo::RUNTIME_ENTRY);
4771 __ add(esp, Immediate(2 * kPointerSize));
4783 static void CreateArrayDispatch(MacroAssembler* masm,
4784 AllocationSiteOverrideMode mode) {
4785 if (mode == DISABLE_ALLOCATION_SITES) {
4786 T stub(masm->isolate(),
4787 GetInitialFastElementsKind(),
4789 __ TailCallStub(&stub);
4790 } else if (mode == DONT_OVERRIDE) {
4791 int last_index = GetSequenceIndexFromFastElementsKind(
4792 TERMINAL_FAST_ELEMENTS_KIND);
4793 for (int i = 0; i <= last_index; ++i) {
4795 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4797 __ j(not_equal, &next);
4798 T stub(masm->isolate(), kind);
4799 __ TailCallStub(&stub);
4803 // If we reached this point there is a problem.
4804 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4811 static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4812 AllocationSiteOverrideMode mode) {
4813 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4814 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
4815 // eax - number of arguments
4816 // edi - constructor?
4817 // esp[0] - return address
4818 // esp[4] - last argument
4819 Label normal_sequence;
4820 if (mode == DONT_OVERRIDE) {
4821 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4822 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4823 STATIC_ASSERT(FAST_ELEMENTS == 2);
4824 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4825 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4826 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4828 // is the low bit set? If so, we are holey and that is good.
4830 __ j(not_zero, &normal_sequence);
4833 // look at the first argument
4834 __ mov(ecx, Operand(esp, kPointerSize));
4836 __ j(zero, &normal_sequence);
4838 if (mode == DISABLE_ALLOCATION_SITES) {
4839 ElementsKind initial = GetInitialFastElementsKind();
4840 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4842 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4844 DISABLE_ALLOCATION_SITES);
4845 __ TailCallStub(&stub_holey);
4847 __ bind(&normal_sequence);
4848 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4850 DISABLE_ALLOCATION_SITES);
4851 __ TailCallStub(&stub);
4852 } else if (mode == DONT_OVERRIDE) {
4853 // We are going to create a holey array, but our kind is non-holey.
4854 // Fix kind and retry.
4857 if (FLAG_debug_code) {
4858 Handle<Map> allocation_site_map =
4859 masm->isolate()->factory()->allocation_site_map();
4860 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
4861 __ Assert(equal, kExpectedAllocationSite);
4864 // Save the resulting elements kind in type info. We can't just store r3
4865 // in the AllocationSite::transition_info field because elements kind is
4866 // restricted to a portion of the field...upper bits need to be left alone.
4867 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4868 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
4869 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
4871 __ bind(&normal_sequence);
4872 int last_index = GetSequenceIndexFromFastElementsKind(
4873 TERMINAL_FAST_ELEMENTS_KIND);
4874 for (int i = 0; i <= last_index; ++i) {
4876 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4878 __ j(not_equal, &next);
4879 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4880 __ TailCallStub(&stub);
4884 // If we reached this point there is a problem.
4885 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4893 static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4894 int to_index = GetSequenceIndexFromFastElementsKind(
4895 TERMINAL_FAST_ELEMENTS_KIND);
4896 for (int i = 0; i <= to_index; ++i) {
4897 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4898 T stub(isolate, kind);
4900 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4901 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4908 void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4909 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4911 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4913 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4918 void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4920 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4921 for (int i = 0; i < 2; i++) {
4922 // For internal arrays we only need a few things
4923 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4925 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4927 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4933 void ArrayConstructorStub::GenerateDispatchToArrayStub(
4934 MacroAssembler* masm,
4935 AllocationSiteOverrideMode mode) {
4936 if (argument_count() == ANY) {
4937 Label not_zero_case, not_one_case;
4939 __ j(not_zero, ¬_zero_case);
4940 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4942 __ bind(¬_zero_case);
4944 __ j(greater, ¬_one_case);
4945 CreateArrayDispatchOneArgument(masm, mode);
4947 __ bind(¬_one_case);
4948 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4949 } else if (argument_count() == NONE) {
4950 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4951 } else if (argument_count() == ONE) {
4952 CreateArrayDispatchOneArgument(masm, mode);
4953 } else if (argument_count() == MORE_THAN_ONE) {
4954 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4961 void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4962 // ----------- S t a t e -------------
4963 // -- eax : argc (only if argument_count() is ANY or MORE_THAN_ONE)
4964 // -- ebx : AllocationSite or undefined
4965 // -- edi : constructor
4966 // -- edx : Original constructor
4967 // -- esp[0] : return address
4968 // -- esp[4] : last argument
4969 // -----------------------------------
4970 if (FLAG_debug_code) {
4971 // The array construct code is only set for the global and natives
4972 // builtin Array functions which always have maps.
4974 // Initial map for the builtin Array function should be a map.
4975 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4976 // Will both indicate a NULL and a Smi.
4977 __ test(ecx, Immediate(kSmiTagMask));
4978 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4979 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4980 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4982 // We should either have undefined in ebx or a valid AllocationSite
4983 __ AssertUndefinedOrAllocationSite(ebx);
4989 __ j(not_equal, &subclassing);
4992 // If the feedback vector is the undefined value call an array constructor
4993 // that doesn't use AllocationSites.
4994 __ cmp(ebx, isolate()->factory()->undefined_value());
4995 __ j(equal, &no_info);
4997 // Only look at the lower 16 bits of the transition info.
4998 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
5000 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
5001 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
5002 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
5005 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
5008 __ bind(&subclassing);
5009 __ pop(ecx); // return address.
5014 switch (argument_count()) {
5017 __ add(eax, Immediate(2));
5020 __ mov(eax, Immediate(2));
5023 __ mov(eax, Immediate(3));
5028 __ JumpToExternalReference(
5029 ExternalReference(Runtime::kArrayConstructorWithSubclassing, isolate()));
5033 void InternalArrayConstructorStub::GenerateCase(
5034 MacroAssembler* masm, ElementsKind kind) {
5035 Label not_zero_case, not_one_case;
5036 Label normal_sequence;
5039 __ j(not_zero, ¬_zero_case);
5040 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
5041 __ TailCallStub(&stub0);
5043 __ bind(¬_zero_case);
5045 __ j(greater, ¬_one_case);
5047 if (IsFastPackedElementsKind(kind)) {
5048 // We might need to create a holey array
5049 // look at the first argument
5050 __ mov(ecx, Operand(esp, kPointerSize));
5052 __ j(zero, &normal_sequence);
5054 InternalArraySingleArgumentConstructorStub
5055 stub1_holey(isolate(), GetHoleyElementsKind(kind));
5056 __ TailCallStub(&stub1_holey);
5059 __ bind(&normal_sequence);
5060 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
5061 __ TailCallStub(&stub1);
5063 __ bind(¬_one_case);
5064 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
5065 __ TailCallStub(&stubN);
5069 void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
5070 // ----------- S t a t e -------------
5072 // -- edi : constructor
5073 // -- esp[0] : return address
5074 // -- esp[4] : last argument
5075 // -----------------------------------
5077 if (FLAG_debug_code) {
5078 // The array construct code is only set for the global and natives
5079 // builtin Array functions which always have maps.
5081 // Initial map for the builtin Array function should be a map.
5082 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5083 // Will both indicate a NULL and a Smi.
5084 __ test(ecx, Immediate(kSmiTagMask));
5085 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
5086 __ CmpObjectType(ecx, MAP_TYPE, ecx);
5087 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
5090 // Figure out the right elements kind
5091 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5093 // Load the map's "bit field 2" into |result|. We only need the first byte,
5094 // but the following masking takes care of that anyway.
5095 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
5096 // Retrieve elements_kind from bit field 2.
5097 __ DecodeField<Map::ElementsKindBits>(ecx);
5099 if (FLAG_debug_code) {
5101 __ cmp(ecx, Immediate(FAST_ELEMENTS));
5103 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
5105 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
5109 Label fast_elements_case;
5110 __ cmp(ecx, Immediate(FAST_ELEMENTS));
5111 __ j(equal, &fast_elements_case);
5112 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
5114 __ bind(&fast_elements_case);
5115 GenerateCase(masm, FAST_ELEMENTS);
5119 void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
5120 Register context_reg = esi;
5121 Register slot_reg = ebx;
5122 Register result_reg = eax;
5125 // Go up context chain to the script context.
5126 for (int i = 0; i < depth(); ++i) {
5127 __ mov(result_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5128 context_reg = result_reg;
5131 // Load the PropertyCell value at the specified slot.
5132 __ mov(result_reg, ContextOperand(context_reg, slot_reg));
5133 __ mov(result_reg, FieldOperand(result_reg, PropertyCell::kValueOffset));
5135 // Check that value is not the_hole.
5136 __ CompareRoot(result_reg, Heap::kTheHoleValueRootIndex);
5137 __ j(equal, &slow_case, Label::kNear);
5140 // Fallback to the runtime.
5141 __ bind(&slow_case);
5142 __ SmiTag(slot_reg);
5143 __ Pop(result_reg); // Pop return address.
5145 __ Push(result_reg); // Push return address.
5146 __ TailCallRuntime(Runtime::kLoadGlobalViaContext, 1, 1);
5150 void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
5151 Register context_reg = esi;
5152 Register slot_reg = ebx;
5153 Register value_reg = eax;
5154 Register cell_reg = edi;
5155 Register cell_details_reg = edx;
5156 Register cell_value_reg = ecx;
5157 Label fast_heapobject_case, fast_smi_case, slow_case;
5159 if (FLAG_debug_code) {
5160 __ CompareRoot(value_reg, Heap::kTheHoleValueRootIndex);
5161 __ Check(not_equal, kUnexpectedValue);
5164 // Go up context chain to the script context.
5165 for (int i = 0; i < depth(); ++i) {
5166 __ mov(cell_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5167 context_reg = cell_reg;
5170 // Load the PropertyCell at the specified slot.
5171 __ mov(cell_reg, ContextOperand(context_reg, slot_reg));
5173 // Load PropertyDetails for the cell (actually only the cell_type and kind).
5174 __ mov(cell_details_reg,
5175 FieldOperand(cell_reg, PropertyCell::kDetailsOffset));
5176 __ SmiUntag(cell_details_reg);
5177 __ and_(cell_details_reg,
5178 Immediate(PropertyDetails::PropertyCellTypeField::kMask |
5179 PropertyDetails::KindField::kMask |
5180 PropertyDetails::kAttributesReadOnlyMask));
5182 // Check if PropertyCell holds mutable data.
5183 Label not_mutable_data;
5184 __ cmp(cell_details_reg,
5185 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5186 PropertyCellType::kMutable) |
5187 PropertyDetails::KindField::encode(kData)));
5188 __ j(not_equal, ¬_mutable_data);
5189 __ JumpIfSmi(value_reg, &fast_smi_case);
5190 __ bind(&fast_heapobject_case);
5191 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5192 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
5193 cell_details_reg, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
5195 // RecordWriteField clobbers the value register, so we need to reload.
5196 __ mov(value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5198 __ bind(¬_mutable_data);
5200 // Check if PropertyCell value matches the new value (relevant for Constant,
5201 // ConstantType and Undefined cells).
5202 Label not_same_value;
5203 __ mov(cell_value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5204 __ cmp(cell_value_reg, value_reg);
5205 __ j(not_equal, ¬_same_value,
5206 FLAG_debug_code ? Label::kFar : Label::kNear);
5207 // Make sure the PropertyCell is not marked READ_ONLY.
5208 __ test(cell_details_reg,
5209 Immediate(PropertyDetails::kAttributesReadOnlyMask));
5210 __ j(not_zero, &slow_case);
5211 if (FLAG_debug_code) {
5213 // This can only be true for Constant, ConstantType and Undefined cells,
5214 // because we never store the_hole via this stub.
5215 __ cmp(cell_details_reg,
5216 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5217 PropertyCellType::kConstant) |
5218 PropertyDetails::KindField::encode(kData)));
5220 __ cmp(cell_details_reg,
5221 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5222 PropertyCellType::kConstantType) |
5223 PropertyDetails::KindField::encode(kData)));
5225 __ cmp(cell_details_reg,
5226 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5227 PropertyCellType::kUndefined) |
5228 PropertyDetails::KindField::encode(kData)));
5229 __ Check(equal, kUnexpectedValue);
5233 __ bind(¬_same_value);
5235 // Check if PropertyCell contains data with constant type (and is not
5237 __ cmp(cell_details_reg,
5238 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5239 PropertyCellType::kConstantType) |
5240 PropertyDetails::KindField::encode(kData)));
5241 __ j(not_equal, &slow_case, Label::kNear);
5243 // Now either both old and new values must be SMIs or both must be heap
5244 // objects with same map.
5245 Label value_is_heap_object;
5246 __ JumpIfNotSmi(value_reg, &value_is_heap_object, Label::kNear);
5247 __ JumpIfNotSmi(cell_value_reg, &slow_case, Label::kNear);
5248 // Old and new values are SMIs, no need for a write barrier here.
5249 __ bind(&fast_smi_case);
5250 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5252 __ bind(&value_is_heap_object);
5253 __ JumpIfSmi(cell_value_reg, &slow_case, Label::kNear);
5254 Register cell_value_map_reg = cell_value_reg;
5255 __ mov(cell_value_map_reg,
5256 FieldOperand(cell_value_reg, HeapObject::kMapOffset));
5257 __ cmp(cell_value_map_reg, FieldOperand(value_reg, HeapObject::kMapOffset));
5258 __ j(equal, &fast_heapobject_case);
5260 // Fallback to the runtime.
5261 __ bind(&slow_case);
5262 __ SmiTag(slot_reg);
5263 __ Pop(cell_reg); // Pop return address.
5266 __ Push(cell_reg); // Push return address.
5267 __ TailCallRuntime(is_strict(language_mode())
5268 ? Runtime::kStoreGlobalViaContext_Strict
5269 : Runtime::kStoreGlobalViaContext_Sloppy,
5274 // Generates an Operand for saving parameters after PrepareCallApiFunction.
5275 static Operand ApiParameterOperand(int index) {
5276 return Operand(esp, index * kPointerSize);
5280 // Prepares stack to put arguments (aligns and so on). Reserves
5281 // space for return value if needed (assumes the return value is a handle).
5282 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
5283 // etc. Saves context (esi). If space was reserved for return value then
5284 // stores the pointer to the reserved slot into esi.
5285 static void PrepareCallApiFunction(MacroAssembler* masm, int argc) {
5286 __ EnterApiExitFrame(argc);
5287 if (__ emit_debug_code()) {
5288 __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
5293 // Calls an API function. Allocates HandleScope, extracts returned value
5294 // from handle and propagates exceptions. Clobbers ebx, edi and
5295 // caller-save registers. Restores context. On return removes
5296 // stack_space * kPointerSize (GCed).
5297 static void CallApiFunctionAndReturn(MacroAssembler* masm,
5298 Register function_address,
5299 ExternalReference thunk_ref,
5300 Operand thunk_last_arg, int stack_space,
5301 Operand* stack_space_operand,
5302 Operand return_value_operand,
5303 Operand* context_restore_operand) {
5304 Isolate* isolate = masm->isolate();
5306 ExternalReference next_address =
5307 ExternalReference::handle_scope_next_address(isolate);
5308 ExternalReference limit_address =
5309 ExternalReference::handle_scope_limit_address(isolate);
5310 ExternalReference level_address =
5311 ExternalReference::handle_scope_level_address(isolate);
5313 DCHECK(edx.is(function_address));
5314 // Allocate HandleScope in callee-save registers.
5315 __ mov(ebx, Operand::StaticVariable(next_address));
5316 __ mov(edi, Operand::StaticVariable(limit_address));
5317 __ add(Operand::StaticVariable(level_address), Immediate(1));
5319 if (FLAG_log_timer_events) {
5320 FrameScope frame(masm, StackFrame::MANUAL);
5321 __ PushSafepointRegisters();
5322 __ PrepareCallCFunction(1, eax);
5323 __ mov(Operand(esp, 0),
5324 Immediate(ExternalReference::isolate_address(isolate)));
5325 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5327 __ PopSafepointRegisters();
5331 Label profiler_disabled;
5332 Label end_profiler_check;
5333 __ mov(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
5334 __ cmpb(Operand(eax, 0), 0);
5335 __ j(zero, &profiler_disabled);
5337 // Additional parameter is the address of the actual getter function.
5338 __ mov(thunk_last_arg, function_address);
5339 // Call the api function.
5340 __ mov(eax, Immediate(thunk_ref));
5342 __ jmp(&end_profiler_check);
5344 __ bind(&profiler_disabled);
5345 // Call the api function.
5346 __ call(function_address);
5347 __ bind(&end_profiler_check);
5349 if (FLAG_log_timer_events) {
5350 FrameScope frame(masm, StackFrame::MANUAL);
5351 __ PushSafepointRegisters();
5352 __ PrepareCallCFunction(1, eax);
5353 __ mov(Operand(esp, 0),
5354 Immediate(ExternalReference::isolate_address(isolate)));
5355 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5357 __ PopSafepointRegisters();
5361 // Load the value from ReturnValue
5362 __ mov(eax, return_value_operand);
5364 Label promote_scheduled_exception;
5365 Label delete_allocated_handles;
5366 Label leave_exit_frame;
5369 // No more valid handles (the result handle was the last one). Restore
5370 // previous handle scope.
5371 __ mov(Operand::StaticVariable(next_address), ebx);
5372 __ sub(Operand::StaticVariable(level_address), Immediate(1));
5373 __ Assert(above_equal, kInvalidHandleScopeLevel);
5374 __ cmp(edi, Operand::StaticVariable(limit_address));
5375 __ j(not_equal, &delete_allocated_handles);
5377 // Leave the API exit frame.
5378 __ bind(&leave_exit_frame);
5379 bool restore_context = context_restore_operand != NULL;
5380 if (restore_context) {
5381 __ mov(esi, *context_restore_operand);
5383 if (stack_space_operand != nullptr) {
5384 __ mov(ebx, *stack_space_operand);
5386 __ LeaveApiExitFrame(!restore_context);
5388 // Check if the function scheduled an exception.
5389 ExternalReference scheduled_exception_address =
5390 ExternalReference::scheduled_exception_address(isolate);
5391 __ cmp(Operand::StaticVariable(scheduled_exception_address),
5392 Immediate(isolate->factory()->the_hole_value()));
5393 __ j(not_equal, &promote_scheduled_exception);
5396 // Check if the function returned a valid JavaScript value.
5398 Register return_value = eax;
5401 __ JumpIfSmi(return_value, &ok, Label::kNear);
5402 __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
5404 __ CmpInstanceType(map, LAST_NAME_TYPE);
5405 __ j(below_equal, &ok, Label::kNear);
5407 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
5408 __ j(above_equal, &ok, Label::kNear);
5410 __ cmp(map, isolate->factory()->heap_number_map());
5411 __ j(equal, &ok, Label::kNear);
5413 __ cmp(return_value, isolate->factory()->undefined_value());
5414 __ j(equal, &ok, Label::kNear);
5416 __ cmp(return_value, isolate->factory()->true_value());
5417 __ j(equal, &ok, Label::kNear);
5419 __ cmp(return_value, isolate->factory()->false_value());
5420 __ j(equal, &ok, Label::kNear);
5422 __ cmp(return_value, isolate->factory()->null_value());
5423 __ j(equal, &ok, Label::kNear);
5425 __ Abort(kAPICallReturnedInvalidObject);
5430 if (stack_space_operand != nullptr) {
5431 DCHECK_EQ(0, stack_space);
5436 __ ret(stack_space * kPointerSize);
5439 // Re-throw by promoting a scheduled exception.
5440 __ bind(&promote_scheduled_exception);
5441 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
5443 // HandleScope limit has changed. Delete allocated extensions.
5444 ExternalReference delete_extensions =
5445 ExternalReference::delete_handle_scope_extensions(isolate);
5446 __ bind(&delete_allocated_handles);
5447 __ mov(Operand::StaticVariable(limit_address), edi);
5449 __ mov(Operand(esp, 0),
5450 Immediate(ExternalReference::isolate_address(isolate)));
5451 __ mov(eax, Immediate(delete_extensions));
5454 __ jmp(&leave_exit_frame);
5458 static void CallApiFunctionStubHelper(MacroAssembler* masm,
5459 const ParameterCount& argc,
5460 bool return_first_arg,
5461 bool call_data_undefined) {
5462 // ----------- S t a t e -------------
5464 // -- ebx : call_data
5466 // -- edx : api_function_address
5468 // -- eax : number of arguments if argc is a register
5470 // -- esp[0] : return address
5471 // -- esp[4] : last argument
5473 // -- esp[argc * 4] : first argument
5474 // -- esp[(argc + 1) * 4] : receiver
5475 // -----------------------------------
5477 Register callee = edi;
5478 Register call_data = ebx;
5479 Register holder = ecx;
5480 Register api_function_address = edx;
5481 Register context = esi;
5482 Register return_address = eax;
5484 typedef FunctionCallbackArguments FCA;
5486 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5487 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5488 STATIC_ASSERT(FCA::kDataIndex == 4);
5489 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5490 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5491 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5492 STATIC_ASSERT(FCA::kHolderIndex == 0);
5493 STATIC_ASSERT(FCA::kArgsLength == 7);
5495 DCHECK(argc.is_immediate() || eax.is(argc.reg()));
5497 if (argc.is_immediate()) {
5498 __ pop(return_address);
5502 // pop return address and save context
5503 __ xchg(context, Operand(esp, 0));
5504 return_address = context;
5513 Register scratch = call_data;
5514 if (!call_data_undefined) {
5516 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
5517 // return value default
5518 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
5522 // return value default
5526 __ push(Immediate(reinterpret_cast<int>(masm->isolate())));
5530 __ mov(scratch, esp);
5532 // push return address
5533 __ push(return_address);
5535 // load context from callee
5536 __ mov(context, FieldOperand(callee, JSFunction::kContextOffset));
5538 // API function gets reference to the v8::Arguments. If CPU profiler
5539 // is enabled wrapper function will be called and we need to pass
5540 // address of the callback as additional parameter, always allocate
5542 const int kApiArgc = 1 + 1;
5544 // Allocate the v8::Arguments structure in the arguments' space since
5545 // it's not controlled by GC.
5546 const int kApiStackSpace = 4;
5548 PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace);
5550 // FunctionCallbackInfo::implicit_args_.
5551 __ mov(ApiParameterOperand(2), scratch);
5552 if (argc.is_immediate()) {
5554 Immediate((argc.immediate() + FCA::kArgsLength - 1) * kPointerSize));
5555 // FunctionCallbackInfo::values_.
5556 __ mov(ApiParameterOperand(3), scratch);
5557 // FunctionCallbackInfo::length_.
5558 __ Move(ApiParameterOperand(4), Immediate(argc.immediate()));
5559 // FunctionCallbackInfo::is_construct_call_.
5560 __ Move(ApiParameterOperand(5), Immediate(0));
5562 __ lea(scratch, Operand(scratch, argc.reg(), times_pointer_size,
5563 (FCA::kArgsLength - 1) * kPointerSize));
5564 // FunctionCallbackInfo::values_.
5565 __ mov(ApiParameterOperand(3), scratch);
5566 // FunctionCallbackInfo::length_.
5567 __ mov(ApiParameterOperand(4), argc.reg());
5568 // FunctionCallbackInfo::is_construct_call_.
5569 __ lea(argc.reg(), Operand(argc.reg(), times_pointer_size,
5570 (FCA::kArgsLength + 1) * kPointerSize));
5571 __ mov(ApiParameterOperand(5), argc.reg());
5574 // v8::InvocationCallback's argument.
5575 __ lea(scratch, ApiParameterOperand(2));
5576 __ mov(ApiParameterOperand(0), scratch);
5578 ExternalReference thunk_ref =
5579 ExternalReference::invoke_function_callback(masm->isolate());
5581 Operand context_restore_operand(ebp,
5582 (2 + FCA::kContextSaveIndex) * kPointerSize);
5583 // Stores return the first js argument
5584 int return_value_offset = 0;
5585 if (return_first_arg) {
5586 return_value_offset = 2 + FCA::kArgsLength;
5588 return_value_offset = 2 + FCA::kReturnValueOffset;
5590 Operand return_value_operand(ebp, return_value_offset * kPointerSize);
5591 int stack_space = 0;
5592 Operand is_construct_call_operand = ApiParameterOperand(5);
5593 Operand* stack_space_operand = &is_construct_call_operand;
5594 if (argc.is_immediate()) {
5595 stack_space = argc.immediate() + FCA::kArgsLength + 1;
5596 stack_space_operand = nullptr;
5598 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5599 ApiParameterOperand(1), stack_space,
5600 stack_space_operand, return_value_operand,
5601 &context_restore_operand);
5605 void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5606 bool call_data_undefined = this->call_data_undefined();
5607 CallApiFunctionStubHelper(masm, ParameterCount(eax), false,
5608 call_data_undefined);
5612 void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5613 bool is_store = this->is_store();
5614 int argc = this->argc();
5615 bool call_data_undefined = this->call_data_undefined();
5616 CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
5617 call_data_undefined);
5621 void CallApiGetterStub::Generate(MacroAssembler* masm) {
5622 // ----------- S t a t e -------------
5623 // -- esp[0] : return address
5625 // -- esp[8 - kArgsLength*4] : PropertyCallbackArguments object
5627 // -- edx : api_function_address
5628 // -----------------------------------
5629 DCHECK(edx.is(ApiGetterDescriptor::function_address()));
5631 // array for v8::Arguments::values_, handler for name and pointer
5632 // to the values (it considered as smi in GC).
5633 const int kStackSpace = PropertyCallbackArguments::kArgsLength + 2;
5634 // Allocate space for opional callback address parameter in case
5635 // CPU profiler is active.
5636 const int kApiArgc = 2 + 1;
5638 Register api_function_address = edx;
5639 Register scratch = ebx;
5641 // load address of name
5642 __ lea(scratch, Operand(esp, 1 * kPointerSize));
5644 PrepareCallApiFunction(masm, kApiArgc);
5645 __ mov(ApiParameterOperand(0), scratch); // name.
5646 __ add(scratch, Immediate(kPointerSize));
5647 __ mov(ApiParameterOperand(1), scratch); // arguments pointer.
5649 ExternalReference thunk_ref =
5650 ExternalReference::invoke_accessor_getter_callback(isolate());
5652 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5653 ApiParameterOperand(2), kStackSpace, nullptr,
5654 Operand(ebp, 7 * kPointerSize), NULL);
5660 } // namespace internal
5663 #endif // V8_TARGET_ARCH_IA32