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.
5 #if V8_TARGET_ARCH_IA32
7 #include "src/base/bits.h"
8 #include "src/bootstrapper.h"
9 #include "src/code-stubs.h"
10 #include "src/codegen.h"
11 #include "src/ia32/code-stubs-ia32.h"
12 #include "src/ia32/frames-ia32.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/regexp/jsregexp.h"
18 #include "src/regexp/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::kArguments, 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 RegExpExecStub::Generate(MacroAssembler* masm) {
1145 // Just jump directly to runtime if native RegExp is not selected at compile
1146 // time or if regexp entry in generated code is turned off runtime switch or
1148 #ifdef V8_INTERPRETED_REGEXP
1149 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1150 #else // V8_INTERPRETED_REGEXP
1152 // Stack frame on entry.
1153 // esp[0]: return address
1154 // esp[4]: last_match_info (expected JSArray)
1155 // esp[8]: previous index
1156 // esp[12]: subject string
1157 // esp[16]: JSRegExp object
1159 static const int kLastMatchInfoOffset = 1 * kPointerSize;
1160 static const int kPreviousIndexOffset = 2 * kPointerSize;
1161 static const int kSubjectOffset = 3 * kPointerSize;
1162 static const int kJSRegExpOffset = 4 * kPointerSize;
1165 Factory* factory = isolate()->factory();
1167 // Ensure that a RegExp stack is allocated.
1168 ExternalReference address_of_regexp_stack_memory_address =
1169 ExternalReference::address_of_regexp_stack_memory_address(isolate());
1170 ExternalReference address_of_regexp_stack_memory_size =
1171 ExternalReference::address_of_regexp_stack_memory_size(isolate());
1172 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
1174 __ j(zero, &runtime);
1176 // Check that the first argument is a JSRegExp object.
1177 __ mov(eax, Operand(esp, kJSRegExpOffset));
1178 STATIC_ASSERT(kSmiTag == 0);
1179 __ JumpIfSmi(eax, &runtime);
1180 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
1181 __ j(not_equal, &runtime);
1183 // Check that the RegExp has been compiled (data contains a fixed array).
1184 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1185 if (FLAG_debug_code) {
1186 __ test(ecx, Immediate(kSmiTagMask));
1187 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
1188 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
1189 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
1192 // ecx: RegExp data (FixedArray)
1193 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1194 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
1195 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
1196 __ j(not_equal, &runtime);
1198 // ecx: RegExp data (FixedArray)
1199 // Check that the number of captures fit in the static offsets vector buffer.
1200 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
1201 // Check (number_of_captures + 1) * 2 <= offsets vector size
1202 // Or number_of_captures * 2 <= offsets vector size - 2
1203 // Multiplying by 2 comes for free since edx is smi-tagged.
1204 STATIC_ASSERT(kSmiTag == 0);
1205 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1206 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1207 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
1208 __ j(above, &runtime);
1210 // Reset offset for possibly sliced string.
1211 __ Move(edi, Immediate(0));
1212 __ mov(eax, Operand(esp, kSubjectOffset));
1213 __ JumpIfSmi(eax, &runtime);
1214 __ mov(edx, eax); // Make a copy of the original subject string.
1215 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1216 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1218 // eax: subject string
1219 // edx: subject string
1220 // ebx: subject string instance type
1221 // ecx: RegExp data (FixedArray)
1222 // Handle subject string according to its encoding and representation:
1223 // (1) Sequential two byte? If yes, go to (9).
1224 // (2) Sequential one byte? If yes, go to (6).
1225 // (3) Anything but sequential or cons? If yes, go to (7).
1226 // (4) Cons string. If the string is flat, replace subject with first string.
1227 // Otherwise bailout.
1228 // (5a) Is subject sequential two byte? If yes, go to (9).
1229 // (5b) Is subject external? If yes, go to (8).
1230 // (6) One byte sequential. Load regexp code for one byte.
1234 // Deferred code at the end of the stub:
1235 // (7) Not a long external string? If yes, go to (10).
1236 // (8) External string. Make it, offset-wise, look like a sequential string.
1237 // (8a) Is the external string one byte? If yes, go to (6).
1238 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1239 // (10) Short external string or not a string? If yes, bail out to runtime.
1240 // (11) Sliced string. Replace subject with parent. Go to (5a).
1242 Label seq_one_byte_string /* 6 */, seq_two_byte_string /* 9 */,
1243 external_string /* 8 */, check_underlying /* 5a */,
1244 not_seq_nor_cons /* 7 */, check_code /* E */,
1245 not_long_external /* 10 */;
1247 // (1) Sequential two byte? If yes, go to (9).
1248 __ and_(ebx, kIsNotStringMask |
1249 kStringRepresentationMask |
1250 kStringEncodingMask |
1251 kShortExternalStringMask);
1252 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
1253 __ j(zero, &seq_two_byte_string); // Go to (9).
1255 // (2) Sequential one byte? If yes, go to (6).
1256 // Any other sequential string must be one byte.
1257 __ and_(ebx, Immediate(kIsNotStringMask |
1258 kStringRepresentationMask |
1259 kShortExternalStringMask));
1260 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (6).
1262 // (3) Anything but sequential or cons? If yes, go to (7).
1263 // We check whether the subject string is a cons, since sequential strings
1264 // have already been covered.
1265 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1266 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
1267 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1268 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
1269 __ cmp(ebx, Immediate(kExternalStringTag));
1270 __ j(greater_equal, ¬_seq_nor_cons); // Go to (7).
1272 // (4) Cons string. Check that it's flat.
1273 // Replace subject with first string and reload instance type.
1274 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
1275 __ j(not_equal, &runtime);
1276 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
1277 __ bind(&check_underlying);
1278 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1279 __ mov(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1281 // (5a) Is subject sequential two byte? If yes, go to (9).
1282 __ test_b(ebx, kStringRepresentationMask | kStringEncodingMask);
1283 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
1284 __ j(zero, &seq_two_byte_string); // Go to (9).
1285 // (5b) Is subject external? If yes, go to (8).
1286 __ test_b(ebx, kStringRepresentationMask);
1287 // The underlying external string is never a short external string.
1288 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
1289 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
1290 __ j(not_zero, &external_string); // Go to (8).
1292 // eax: sequential subject string (or look-alike, external string)
1293 // edx: original subject string
1294 // ecx: RegExp data (FixedArray)
1295 // (6) One byte sequential. Load regexp code for one byte.
1296 __ bind(&seq_one_byte_string);
1297 // Load previous index and check range before edx is overwritten. We have
1298 // to use edx instead of eax here because it might have been only made to
1299 // look like a sequential string when it actually is an external string.
1300 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1301 __ JumpIfNotSmi(ebx, &runtime);
1302 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1303 __ j(above_equal, &runtime);
1304 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataOneByteCodeOffset));
1305 __ Move(ecx, Immediate(1)); // Type is one byte.
1307 // (E) Carry on. String handling is done.
1308 __ bind(&check_code);
1309 // edx: irregexp code
1310 // Check that the irregexp code has been generated for the actual string
1311 // encoding. If it has, the field contains a code object otherwise it contains
1312 // a smi (code flushing support).
1313 __ JumpIfSmi(edx, &runtime);
1315 // eax: subject string
1316 // ebx: previous index (smi)
1318 // ecx: encoding of subject string (1 if one_byte, 0 if two_byte);
1319 // All checks done. Now push arguments for native regexp code.
1320 Counters* counters = isolate()->counters();
1321 __ IncrementCounter(counters->regexp_entry_native(), 1);
1323 // Isolates: note we add an additional parameter here (isolate pointer).
1324 static const int kRegExpExecuteArguments = 9;
1325 __ EnterApiExitFrame(kRegExpExecuteArguments);
1327 // Argument 9: Pass current isolate address.
1328 __ mov(Operand(esp, 8 * kPointerSize),
1329 Immediate(ExternalReference::isolate_address(isolate())));
1331 // Argument 8: Indicate that this is a direct call from JavaScript.
1332 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
1334 // Argument 7: Start (high end) of backtracking stack memory area.
1335 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
1336 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
1337 __ mov(Operand(esp, 6 * kPointerSize), esi);
1339 // Argument 6: Set the number of capture registers to zero to force global
1340 // regexps to behave as non-global. This does not affect non-global regexps.
1341 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
1343 // Argument 5: static offsets vector buffer.
1344 __ mov(Operand(esp, 4 * kPointerSize),
1345 Immediate(ExternalReference::address_of_static_offsets_vector(
1348 // Argument 2: Previous index.
1350 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1352 // Argument 1: Original subject string.
1353 // The original subject is in the previous stack frame. Therefore we have to
1354 // use ebp, which points exactly to one pointer size below the previous esp.
1355 // (Because creating a new stack frame pushes the previous ebp onto the stack
1356 // and thereby moves up esp by one kPointerSize.)
1357 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
1358 __ mov(Operand(esp, 0 * kPointerSize), esi);
1360 // esi: original subject string
1361 // eax: underlying subject string
1362 // ebx: previous index
1363 // ecx: encoding of subject string (1 if one_byte 0 if two_byte);
1365 // Argument 4: End of string data
1366 // Argument 3: Start of string data
1367 // Prepare start and end index of the input.
1368 // Load the length from the original sliced string if that is the case.
1369 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
1370 __ add(esi, edi); // Calculate input end wrt offset.
1372 __ add(ebx, edi); // Calculate input start wrt offset.
1374 // ebx: start index of the input string
1375 // esi: end index of the input string
1376 Label setup_two_byte, setup_rest;
1378 __ j(zero, &setup_two_byte, Label::kNear);
1380 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
1381 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1382 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
1383 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1384 __ jmp(&setup_rest, Label::kNear);
1386 __ bind(&setup_two_byte);
1387 STATIC_ASSERT(kSmiTag == 0);
1388 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
1389 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
1390 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
1391 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
1392 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
1394 __ bind(&setup_rest);
1396 // Locate the code entry and call it.
1397 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
1400 // Drop arguments and come back to JS mode.
1401 __ LeaveApiExitFrame(true);
1403 // Check the result.
1406 // We expect exactly one result since we force the called regexp to behave
1408 __ j(equal, &success);
1410 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
1411 __ j(equal, &failure);
1412 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
1413 // If not exception it can only be retry. Handle that in the runtime system.
1414 __ j(not_equal, &runtime);
1415 // Result must now be exception. If there is no pending exception already a
1416 // stack overflow (on the backtrack stack) was detected in RegExp code but
1417 // haven't created the exception yet. Handle that in the runtime system.
1418 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
1419 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
1421 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1422 __ mov(eax, Operand::StaticVariable(pending_exception));
1424 __ j(equal, &runtime);
1426 // For exception, throw the exception again.
1427 __ TailCallRuntime(Runtime::kRegExpExecReThrow, 4, 1);
1430 // For failure to match, return null.
1431 __ mov(eax, factory->null_value());
1432 __ ret(4 * kPointerSize);
1434 // Load RegExp data.
1436 __ mov(eax, Operand(esp, kJSRegExpOffset));
1437 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
1438 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
1439 // Calculate number of capture registers (number_of_captures + 1) * 2.
1440 STATIC_ASSERT(kSmiTag == 0);
1441 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1442 __ add(edx, Immediate(2)); // edx was a smi.
1444 // edx: Number of capture registers
1445 // Load last_match_info which is still known to be a fast case JSArray.
1446 // Check that the fourth object is a JSArray object.
1447 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1448 __ JumpIfSmi(eax, &runtime);
1449 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
1450 __ j(not_equal, &runtime);
1451 // Check that the JSArray is in fast case.
1452 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
1453 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
1454 __ cmp(eax, factory->fixed_array_map());
1455 __ j(not_equal, &runtime);
1456 // Check that the last match info has space for the capture registers and the
1457 // additional information.
1458 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
1460 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
1462 __ j(greater, &runtime);
1464 // ebx: last_match_info backing store (FixedArray)
1465 // edx: number of capture registers
1466 // Store the capture count.
1467 __ SmiTag(edx); // Number of capture registers to smi.
1468 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
1469 __ SmiUntag(edx); // Number of capture registers back from smi.
1470 // Store last subject and last input.
1471 __ mov(eax, Operand(esp, kSubjectOffset));
1473 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
1474 __ RecordWriteField(ebx,
1475 RegExpImpl::kLastSubjectOffset,
1480 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
1481 __ RecordWriteField(ebx,
1482 RegExpImpl::kLastInputOffset,
1487 // Get the static offsets vector filled by the native regexp code.
1488 ExternalReference address_of_static_offsets_vector =
1489 ExternalReference::address_of_static_offsets_vector(isolate());
1490 __ mov(ecx, Immediate(address_of_static_offsets_vector));
1492 // ebx: last_match_info backing store (FixedArray)
1493 // ecx: offsets vector
1494 // edx: number of capture registers
1495 Label next_capture, done;
1496 // Capture register counter starts from number of capture registers and
1497 // counts down until wraping after zero.
1498 __ bind(&next_capture);
1499 __ sub(edx, Immediate(1));
1500 __ j(negative, &done, Label::kNear);
1501 // Read the value from the static offsets vector buffer.
1502 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
1504 // Store the smi value in the last match info.
1505 __ mov(FieldOperand(ebx,
1508 RegExpImpl::kFirstCaptureOffset),
1510 __ jmp(&next_capture);
1513 // Return last match info.
1514 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1515 __ ret(4 * kPointerSize);
1517 // Do the runtime call to execute the regexp.
1519 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1521 // Deferred code for string handling.
1522 // (7) Not a long external string? If yes, go to (10).
1523 __ bind(¬_seq_nor_cons);
1524 // Compare flags are still set from (3).
1525 __ j(greater, ¬_long_external, Label::kNear); // Go to (10).
1527 // (8) External string. Short external strings have been ruled out.
1528 __ bind(&external_string);
1529 // Reload instance type.
1530 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1531 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1532 if (FLAG_debug_code) {
1533 // Assert that we do not have a cons or slice (indirect strings) here.
1534 // Sequential strings have already been ruled out.
1535 __ test_b(ebx, kIsIndirectStringMask);
1536 __ Assert(zero, kExternalStringExpectedButNotFound);
1538 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
1539 // Move the pointer so that offset-wise, it looks like a sequential string.
1540 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
1541 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
1542 STATIC_ASSERT(kTwoByteStringTag == 0);
1543 // (8a) Is the external string one byte? If yes, go to (6).
1544 __ test_b(ebx, kStringEncodingMask);
1545 __ j(not_zero, &seq_one_byte_string); // Goto (6).
1547 // eax: sequential subject string (or look-alike, external string)
1548 // edx: original subject string
1549 // ecx: RegExp data (FixedArray)
1550 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1551 __ bind(&seq_two_byte_string);
1552 // Load previous index and check range before edx is overwritten. We have
1553 // to use edx instead of eax here because it might have been only made to
1554 // look like a sequential string when it actually is an external string.
1555 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1556 __ JumpIfNotSmi(ebx, &runtime);
1557 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1558 __ j(above_equal, &runtime);
1559 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
1560 __ Move(ecx, Immediate(0)); // Type is two byte.
1561 __ jmp(&check_code); // Go to (E).
1563 // (10) Not a string or a short external string? If yes, bail out to runtime.
1564 __ bind(¬_long_external);
1565 // Catch non-string subject or short external string.
1566 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1567 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
1568 __ j(not_zero, &runtime);
1570 // (11) Sliced string. Replace subject with parent. Go to (5a).
1571 // Load offset into edi and replace subject string with parent.
1572 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
1573 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
1574 __ jmp(&check_underlying); // Go to (5a).
1575 #endif // V8_INTERPRETED_REGEXP
1579 static int NegativeComparisonResult(Condition cc) {
1580 DCHECK(cc != equal);
1581 DCHECK((cc == less) || (cc == less_equal)
1582 || (cc == greater) || (cc == greater_equal));
1583 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
1587 static void CheckInputType(MacroAssembler* masm, Register input,
1588 CompareICState::State expected, Label* fail) {
1590 if (expected == CompareICState::SMI) {
1591 __ JumpIfNotSmi(input, fail);
1592 } else if (expected == CompareICState::NUMBER) {
1593 __ JumpIfSmi(input, &ok);
1594 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
1595 Immediate(masm->isolate()->factory()->heap_number_map()));
1596 __ j(not_equal, fail);
1598 // We could be strict about internalized/non-internalized here, but as long as
1599 // hydrogen doesn't care, the stub doesn't have to care either.
1604 static void BranchIfNotInternalizedString(MacroAssembler* masm,
1608 __ JumpIfSmi(object, label);
1609 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
1610 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
1611 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
1612 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
1613 __ j(not_zero, label);
1617 void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
1618 Label runtime_call, check_unequal_objects;
1619 Condition cc = GetCondition();
1622 CheckInputType(masm, edx, left(), &miss);
1623 CheckInputType(masm, eax, right(), &miss);
1625 // Compare two smis.
1626 Label non_smi, smi_done;
1629 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
1630 __ sub(edx, eax); // Return on the result of the subtraction.
1631 __ j(no_overflow, &smi_done, Label::kNear);
1632 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
1638 // NOTICE! This code is only reached after a smi-fast-case check, so
1639 // it is certain that at least one operand isn't a smi.
1641 // Identical objects can be compared fast, but there are some tricky cases
1642 // for NaN and undefined.
1643 Label generic_heap_number_comparison;
1645 Label not_identical;
1647 __ j(not_equal, ¬_identical);
1650 // Check for undefined. undefined OP undefined is false even though
1651 // undefined == undefined.
1652 __ cmp(edx, isolate()->factory()->undefined_value());
1653 if (is_strong(strength())) {
1654 // In strong mode, this comparison must throw, so call the runtime.
1655 __ j(equal, &runtime_call, Label::kFar);
1657 Label check_for_nan;
1658 __ j(not_equal, &check_for_nan, Label::kNear);
1659 __ Move(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1661 __ bind(&check_for_nan);
1665 // Test for NaN. Compare heap numbers in a general way,
1666 // to handle NaNs correctly.
1667 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
1668 Immediate(isolate()->factory()->heap_number_map()));
1669 __ j(equal, &generic_heap_number_comparison, Label::kNear);
1671 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1672 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
1673 // Call runtime on identical JSObjects. Otherwise return equal.
1674 __ cmpb(ecx, static_cast<uint8_t>(FIRST_SPEC_OBJECT_TYPE));
1675 __ j(above_equal, &runtime_call, Label::kFar);
1676 // Call runtime on identical symbols since we need to throw a TypeError.
1677 __ cmpb(ecx, static_cast<uint8_t>(SYMBOL_TYPE));
1678 __ j(equal, &runtime_call, Label::kFar);
1679 // Call runtime on identical SIMD values since we must throw a TypeError.
1680 __ cmpb(ecx, static_cast<uint8_t>(SIMD128_VALUE_TYPE));
1681 __ j(equal, &runtime_call, Label::kFar);
1682 if (is_strong(strength())) {
1683 // We have already tested for smis and heap numbers, so if both
1684 // arguments are not strings we must proceed to the slow case.
1685 __ test(ecx, Immediate(kIsNotStringMask));
1686 __ j(not_zero, &runtime_call, Label::kFar);
1689 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
1693 __ bind(¬_identical);
1696 // Strict equality can quickly decide whether objects are equal.
1697 // Non-strict object equality is slower, so it is handled later in the stub.
1698 if (cc == equal && strict()) {
1699 Label slow; // Fallthrough label.
1701 // If we're doing a strict equality comparison, we don't have to do
1702 // type conversion, so we generate code to do fast comparison for objects
1703 // and oddballs. Non-smi numbers and strings still go through the usual
1705 // If either is a Smi (we know that not both are), then they can only
1706 // be equal if the other is a HeapNumber. If so, use the slow case.
1707 STATIC_ASSERT(kSmiTag == 0);
1708 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
1709 __ mov(ecx, Immediate(kSmiTagMask));
1712 __ j(not_zero, ¬_smis, Label::kNear);
1713 // One operand is a smi.
1715 // Check whether the non-smi is a heap number.
1716 STATIC_ASSERT(kSmiTagMask == 1);
1717 // ecx still holds eax & kSmiTag, which is either zero or one.
1718 __ sub(ecx, Immediate(0x01));
1721 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
1723 // if eax was smi, ebx is now edx, else eax.
1725 // Check if the non-smi operand is a heap number.
1726 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
1727 Immediate(isolate()->factory()->heap_number_map()));
1728 // If heap number, handle it in the slow case.
1729 __ j(equal, &slow, Label::kNear);
1730 // Return non-equal (ebx is not zero)
1735 // If either operand is a JSObject or an oddball value, then they are not
1736 // equal since their pointers are different
1737 // There is no test for undetectability in strict equality.
1739 // Get the type of the first operand.
1740 // If the first object is a JS object, we have done pointer comparison.
1741 Label first_non_object;
1742 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
1743 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1744 __ j(below, &first_non_object, Label::kNear);
1746 // Return non-zero (eax is not zero)
1747 Label return_not_equal;
1748 STATIC_ASSERT(kHeapObjectTag != 0);
1749 __ bind(&return_not_equal);
1752 __ bind(&first_non_object);
1753 // Check for oddballs: true, false, null, undefined.
1754 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1755 __ j(equal, &return_not_equal);
1757 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ecx);
1758 __ j(above_equal, &return_not_equal);
1760 // Check for oddballs: true, false, null, undefined.
1761 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1762 __ j(equal, &return_not_equal);
1764 // Fall through to the general case.
1768 // Generate the number comparison code.
1769 Label non_number_comparison;
1771 __ bind(&generic_heap_number_comparison);
1773 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
1774 __ ucomisd(xmm0, xmm1);
1775 // Don't base result on EFLAGS when a NaN is involved.
1776 __ j(parity_even, &unordered, Label::kNear);
1778 __ mov(eax, 0); // equal
1779 __ mov(ecx, Immediate(Smi::FromInt(1)));
1780 __ cmov(above, eax, ecx);
1781 __ mov(ecx, Immediate(Smi::FromInt(-1)));
1782 __ cmov(below, eax, ecx);
1785 // If one of the numbers was NaN, then the result is always false.
1786 // The cc is never not-equal.
1787 __ bind(&unordered);
1788 DCHECK(cc != not_equal);
1789 if (cc == less || cc == less_equal) {
1790 __ mov(eax, Immediate(Smi::FromInt(1)));
1792 __ mov(eax, Immediate(Smi::FromInt(-1)));
1796 // The number comparison code did not provide a valid result.
1797 __ bind(&non_number_comparison);
1799 // Fast negative check for internalized-to-internalized equality.
1800 Label check_for_strings;
1802 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
1803 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
1805 // We've already checked for object identity, so if both operands
1806 // are internalized they aren't equal. Register eax already holds a
1807 // non-zero value, which indicates not equal, so just return.
1811 __ bind(&check_for_strings);
1813 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx,
1814 &check_unequal_objects);
1816 // Inline comparison of one-byte strings.
1818 StringHelper::GenerateFlatOneByteStringEquals(masm, edx, eax, ecx, ebx);
1820 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
1824 __ Abort(kUnexpectedFallThroughFromStringComparison);
1827 __ bind(&check_unequal_objects);
1828 if (cc == equal && !strict()) {
1829 // Non-strict equality. Objects are unequal if
1830 // they are both JSObjects and not undetectable,
1831 // and their pointers are different.
1832 Label return_unequal;
1833 // At most one is a smi, so we can test for smi by adding the two.
1834 // A smi plus a heap object has the low bit set, a heap object plus
1835 // a heap object has the low bit clear.
1836 STATIC_ASSERT(kSmiTag == 0);
1837 STATIC_ASSERT(kSmiTagMask == 1);
1838 __ lea(ecx, Operand(eax, edx, times_1, 0));
1839 __ test(ecx, Immediate(kSmiTagMask));
1840 __ j(not_zero, &runtime_call, Label::kNear);
1841 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
1842 __ j(below, &runtime_call, Label::kNear);
1843 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ebx);
1844 __ j(below, &runtime_call, Label::kNear);
1845 // We do not bail out after this point. Both are JSObjects, and
1846 // they are equal if and only if both are undetectable.
1847 // The and of the undetectable flags is 1 if and only if they are equal.
1848 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
1849 1 << Map::kIsUndetectable);
1850 __ j(zero, &return_unequal, Label::kNear);
1851 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
1852 1 << Map::kIsUndetectable);
1853 __ j(zero, &return_unequal, Label::kNear);
1854 // The objects are both undetectable, so they both compare as the value
1855 // undefined, and are equal.
1856 __ Move(eax, Immediate(EQUAL));
1857 __ bind(&return_unequal);
1858 // Return non-equal by returning the non-zero object pointer in eax,
1859 // or return equal if we fell through to here.
1860 __ ret(0); // rax, rdx were pushed
1862 __ bind(&runtime_call);
1864 // Push arguments below the return address.
1869 // Figure out which native to call and setup the arguments.
1870 if (cc == equal && strict()) {
1872 __ TailCallRuntime(Runtime::kStrictEquals, 2, 1);
1874 int native_context_index;
1876 native_context_index = Context::EQUALS_BUILTIN_INDEX;
1878 native_context_index = is_strong(strength())
1879 ? Context::COMPARE_STRONG_BUILTIN_INDEX
1880 : Context::COMPARE_BUILTIN_INDEX;
1881 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1884 // Restore return address on the stack.
1887 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1888 // tagged as a small integer.
1889 __ InvokeBuiltin(native_context_index, JUMP_FUNCTION);
1897 static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub,
1899 // eax : number of arguments to the construct function
1900 // ebx : feedback vector
1901 // edx : slot in feedback vector (Smi)
1902 // edi : the function to call
1903 // esp[0]: original receiver (for IsSuperConstructorCall)
1909 FrameScope scope(masm, StackFrame::INTERNAL);
1911 // Number-of-arguments register must be smi-tagged to call out.
1939 static void GenerateRecordCallTarget(MacroAssembler* masm, bool is_super) {
1940 // Cache the called function in a feedback vector slot. Cache states
1941 // are uninitialized, monomorphic (indicated by a JSFunction), and
1943 // eax : number of arguments to the construct function
1944 // ebx : feedback vector
1945 // edx : slot in feedback vector (Smi)
1946 // edi : the function to call
1947 // esp[0]: original receiver (for IsSuperConstructorCall)
1948 Isolate* isolate = masm->isolate();
1949 Label initialize, done, miss, megamorphic, not_array_function;
1951 // Load the cache state into ecx.
1952 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1953 FixedArray::kHeaderSize));
1955 // A monomorphic cache hit or an already megamorphic state: invoke the
1956 // function without changing the state.
1957 // We don't know if ecx is a WeakCell or a Symbol, but it's harmless to read
1958 // at this position in a symbol (see static asserts in
1959 // type-feedback-vector.h).
1960 Label check_allocation_site;
1961 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
1962 __ j(equal, &done, Label::kFar);
1963 __ CompareRoot(ecx, Heap::kmegamorphic_symbolRootIndex);
1964 __ j(equal, &done, Label::kFar);
1965 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1966 Heap::kWeakCellMapRootIndex);
1967 __ j(not_equal, FLAG_pretenuring_call_new ? &miss : &check_allocation_site);
1969 // If the weak cell is cleared, we have a new chance to become monomorphic.
1970 __ JumpIfSmi(FieldOperand(ecx, WeakCell::kValueOffset), &initialize);
1971 __ jmp(&megamorphic);
1973 if (!FLAG_pretenuring_call_new) {
1974 __ bind(&check_allocation_site);
1975 // If we came here, we need to see if we are the array function.
1976 // If we didn't have a matching function, and we didn't find the megamorph
1977 // sentinel, then we have in the slot either some other function or an
1979 __ CompareRoot(FieldOperand(ecx, 0), Heap::kAllocationSiteMapRootIndex);
1980 __ j(not_equal, &miss);
1982 // Make sure the function is the Array() function
1983 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1985 __ j(not_equal, &megamorphic);
1986 __ jmp(&done, Label::kFar);
1991 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1993 __ CompareRoot(ecx, Heap::kuninitialized_symbolRootIndex);
1994 __ j(equal, &initialize);
1995 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1996 // write-barrier is needed.
1997 __ bind(&megamorphic);
1999 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
2000 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2001 __ jmp(&done, Label::kFar);
2003 // An uninitialized cache is patched with the function or sentinel to
2004 // indicate the ElementsKind if function is the Array constructor.
2005 __ bind(&initialize);
2006 if (!FLAG_pretenuring_call_new) {
2007 // Make sure the function is the Array() function
2008 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2010 __ j(not_equal, ¬_array_function);
2012 // The target function is the Array constructor,
2013 // Create an AllocationSite if we don't already have it, store it in the
2015 CreateAllocationSiteStub create_stub(isolate);
2016 CallStubInRecordCallTarget(masm, &create_stub, is_super);
2019 __ bind(¬_array_function);
2022 CreateWeakCellStub create_stub(isolate);
2023 CallStubInRecordCallTarget(masm, &create_stub, is_super);
2028 static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
2029 // Do not transform the receiver for strict mode functions.
2030 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2031 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kStrictModeByteOffset),
2032 1 << SharedFunctionInfo::kStrictModeBitWithinByte);
2033 __ j(not_equal, cont);
2035 // Do not transform the receiver for natives (shared already in ecx).
2036 __ test_b(FieldOperand(ecx, SharedFunctionInfo::kNativeByteOffset),
2037 1 << SharedFunctionInfo::kNativeBitWithinByte);
2038 __ j(not_equal, cont);
2042 static void EmitSlowCase(Isolate* isolate, MacroAssembler* masm, int argc) {
2044 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
2048 static void EmitWrapCase(MacroAssembler* masm, int argc, Label* cont) {
2049 // Wrap the receiver and patch it back onto the stack.
2050 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
2052 ToObjectStub stub(masm->isolate());
2056 __ mov(Operand(esp, (argc + 1) * kPointerSize), eax);
2061 static void CallFunctionNoFeedback(MacroAssembler* masm,
2062 int argc, bool needs_checks,
2063 bool call_as_method) {
2064 // edi : the function to call
2065 Label slow, wrap, cont;
2068 // Check that the function really is a JavaScript function.
2069 __ JumpIfSmi(edi, &slow);
2071 // Goto slow case if we do not have a function.
2072 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2073 __ j(not_equal, &slow);
2076 // Fast-case: Just invoke the function.
2077 ParameterCount actual(argc);
2079 if (call_as_method) {
2081 EmitContinueIfStrictOrNative(masm, &cont);
2084 // Load the receiver from the stack.
2085 __ mov(eax, Operand(esp, (argc + 1) * kPointerSize));
2088 __ JumpIfSmi(eax, &wrap);
2090 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
2099 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
2102 // Slow-case: Non-function called.
2104 EmitSlowCase(masm->isolate(), masm, argc);
2107 if (call_as_method) {
2109 EmitWrapCase(masm, argc, &cont);
2114 void CallFunctionStub::Generate(MacroAssembler* masm) {
2115 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
2119 void CallConstructStub::Generate(MacroAssembler* masm) {
2120 // eax : number of arguments
2121 // ebx : feedback vector
2122 // ecx : original constructor (for IsSuperConstructorCall)
2123 // edx : slot in feedback vector (Smi, for RecordCallTarget)
2124 // edi : constructor function
2125 Label slow, non_function_call;
2127 if (IsSuperConstructorCall()) {
2131 // Check that function is not a smi.
2132 __ JumpIfSmi(edi, &non_function_call);
2133 // Check that function is a JSFunction.
2134 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2135 __ j(not_equal, &slow);
2137 if (RecordCallTarget()) {
2138 GenerateRecordCallTarget(masm, IsSuperConstructorCall());
2140 if (FLAG_pretenuring_call_new) {
2141 // Put the AllocationSite from the feedback vector into ebx.
2142 // By adding kPointerSize we encode that we know the AllocationSite
2143 // entry is at the feedback vector slot given by edx + 1.
2144 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
2145 FixedArray::kHeaderSize + kPointerSize));
2147 Label feedback_register_initialized;
2148 // Put the AllocationSite from the feedback vector into ebx, or undefined.
2149 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
2150 FixedArray::kHeaderSize));
2151 Handle<Map> allocation_site_map =
2152 isolate()->factory()->allocation_site_map();
2153 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
2154 __ j(equal, &feedback_register_initialized);
2155 __ mov(ebx, isolate()->factory()->undefined_value());
2156 __ bind(&feedback_register_initialized);
2159 __ AssertUndefinedOrAllocationSite(ebx);
2162 if (IsSuperConstructorCall()) {
2165 // Pass original constructor to construct stub.
2169 // Jump to the function-specific construct stub.
2170 Register jmp_reg = ecx;
2171 __ mov(jmp_reg, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
2172 __ mov(jmp_reg, FieldOperand(jmp_reg,
2173 SharedFunctionInfo::kConstructStubOffset));
2174 __ lea(jmp_reg, FieldOperand(jmp_reg, Code::kHeaderSize));
2177 // edi: called object
2178 // eax: number of arguments
2180 // esp[0]: original receiver (for IsSuperConstructorCall)
2183 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
2184 __ j(not_equal, &non_function_call);
2185 __ GetBuiltinEntry(edx,
2186 Context::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR_BUILTIN_INDEX);
2189 __ bind(&non_function_call);
2190 __ GetBuiltinEntry(edx,
2191 Context::CALL_NON_FUNCTION_AS_CONSTRUCTOR_BUILTIN_INDEX);
2193 if (IsSuperConstructorCall()) {
2196 // Set expected number of arguments to zero (not changing eax).
2197 __ Move(ebx, Immediate(0));
2198 Handle<Code> arguments_adaptor =
2199 isolate()->builtins()->ArgumentsAdaptorTrampoline();
2200 __ jmp(arguments_adaptor, RelocInfo::CODE_TARGET);
2204 static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
2205 __ mov(vector, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
2206 __ mov(vector, FieldOperand(vector, JSFunction::kSharedFunctionInfoOffset));
2207 __ mov(vector, FieldOperand(vector,
2208 SharedFunctionInfo::kFeedbackVectorOffset));
2212 void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
2217 int argc = arg_count();
2218 ParameterCount actual(argc);
2220 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2222 __ j(not_equal, &miss);
2224 __ mov(eax, arg_count());
2225 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
2226 FixedArray::kHeaderSize));
2228 // Verify that ecx contains an AllocationSite
2229 Factory* factory = masm->isolate()->factory();
2230 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
2231 factory->allocation_site_map());
2232 __ j(not_equal, &miss);
2234 // Increment the call count for monomorphic function calls.
2235 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
2236 FixedArray::kHeaderSize + kPointerSize),
2237 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2241 ArrayConstructorStub stub(masm->isolate(), arg_count());
2242 __ TailCallStub(&stub);
2247 // The slow case, we need this no matter what to complete a call after a miss.
2248 __ Set(eax, arg_count());
2249 __ Jump(masm->isolate()->builtins()->Call(), RelocInfo::CODE_TARGET);
2253 void CallICStub::Generate(MacroAssembler* masm) {
2257 Isolate* isolate = masm->isolate();
2258 const int with_types_offset =
2259 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kWithTypesIndex);
2260 const int generic_offset =
2261 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kGenericCountIndex);
2262 Label extra_checks_or_miss, slow_start;
2263 Label slow, wrap, cont;
2264 Label have_js_function;
2265 int argc = arg_count();
2266 ParameterCount actual(argc);
2268 // The checks. First, does edi match the recorded monomorphic target?
2269 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
2270 FixedArray::kHeaderSize));
2272 // We don't know that we have a weak cell. We might have a private symbol
2273 // or an AllocationSite, but the memory is safe to examine.
2274 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
2276 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
2277 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
2278 // computed, meaning that it can't appear to be a pointer. If the low bit is
2279 // 0, then hash is computed, but the 0 bit prevents the field from appearing
2281 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
2282 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
2283 WeakCell::kValueOffset &&
2284 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
2286 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
2287 __ j(not_equal, &extra_checks_or_miss);
2289 // The compare above could have been a SMI/SMI comparison. Guard against this
2290 // convincing us that we have a monomorphic JSFunction.
2291 __ JumpIfSmi(edi, &extra_checks_or_miss);
2293 // Increment the call count for monomorphic function calls.
2294 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
2295 FixedArray::kHeaderSize + kPointerSize),
2296 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2298 __ bind(&have_js_function);
2299 if (CallAsMethod()) {
2300 EmitContinueIfStrictOrNative(masm, &cont);
2302 // Load the receiver from the stack.
2303 __ mov(eax, Operand(esp, (argc + 1) * kPointerSize));
2305 __ JumpIfSmi(eax, &wrap);
2307 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
2313 __ InvokeFunction(edi, actual, JUMP_FUNCTION, NullCallWrapper());
2316 EmitSlowCase(isolate, masm, argc);
2318 if (CallAsMethod()) {
2320 EmitWrapCase(masm, argc, &cont);
2323 __ bind(&extra_checks_or_miss);
2324 Label uninitialized, miss;
2326 __ cmp(ecx, Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2327 __ j(equal, &slow_start);
2329 // The following cases attempt to handle MISS cases without going to the
2331 if (FLAG_trace_ic) {
2335 __ cmp(ecx, Immediate(TypeFeedbackVector::UninitializedSentinel(isolate)));
2336 __ j(equal, &uninitialized);
2338 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2339 // to handle it here. More complex cases are dealt with in the runtime.
2340 __ AssertNotSmi(ecx);
2341 __ CmpObjectType(ecx, JS_FUNCTION_TYPE, ecx);
2342 __ j(not_equal, &miss);
2344 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
2345 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
2346 // We have to update statistics for runtime profiling.
2347 __ sub(FieldOperand(ebx, with_types_offset), Immediate(Smi::FromInt(1)));
2348 __ add(FieldOperand(ebx, generic_offset), Immediate(Smi::FromInt(1)));
2349 __ jmp(&slow_start);
2351 __ bind(&uninitialized);
2353 // We are going monomorphic, provided we actually have a JSFunction.
2354 __ JumpIfSmi(edi, &miss);
2356 // Goto miss case if we do not have a function.
2357 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2358 __ j(not_equal, &miss);
2360 // Make sure the function is not the Array() function, which requires special
2361 // behavior on MISS.
2362 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
2367 __ add(FieldOperand(ebx, with_types_offset), Immediate(Smi::FromInt(1)));
2369 // Initialize the call counter.
2370 __ mov(FieldOperand(ebx, edx, times_half_pointer_size,
2371 FixedArray::kHeaderSize + kPointerSize),
2372 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2374 // Store the function. Use a stub since we need a frame for allocation.
2379 FrameScope scope(masm, StackFrame::INTERNAL);
2380 CreateWeakCellStub create_stub(isolate);
2382 __ CallStub(&create_stub);
2386 __ jmp(&have_js_function);
2388 // We are here because tracing is on or we encountered a MISS case we can't
2394 __ bind(&slow_start);
2396 // Check that the function really is a JavaScript function.
2397 __ JumpIfSmi(edi, &slow);
2399 // Goto slow case if we do not have a function.
2400 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
2401 __ j(not_equal, &slow);
2402 __ jmp(&have_js_function);
2409 void CallICStub::GenerateMiss(MacroAssembler* masm) {
2410 FrameScope scope(masm, StackFrame::INTERNAL);
2412 // Push the function and feedback info.
2418 Runtime::FunctionId id = GetICState() == DEFAULT
2419 ? Runtime::kCallIC_Miss
2420 : Runtime::kCallIC_Customization_Miss;
2421 __ CallRuntime(id, 3);
2423 // Move result to edi and exit the internal frame.
2428 bool CEntryStub::NeedsImmovableCode() {
2433 void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
2434 CEntryStub::GenerateAheadOfTime(isolate);
2435 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
2436 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
2437 // It is important that the store buffer overflow stubs are generated first.
2438 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
2439 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
2440 CreateWeakCellStub::GenerateAheadOfTime(isolate);
2441 BinaryOpICStub::GenerateAheadOfTime(isolate);
2442 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
2443 StoreFastElementStub::GenerateAheadOfTime(isolate);
2444 TypeofStub::GenerateAheadOfTime(isolate);
2448 void CodeStub::GenerateFPStubs(Isolate* isolate) {
2449 // Generate if not already in cache.
2450 CEntryStub(isolate, 1, kSaveFPRegs).GetCode();
2451 isolate->set_fp_stubs_generated(true);
2455 void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
2456 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
2461 void CEntryStub::Generate(MacroAssembler* masm) {
2462 // eax: number of arguments including receiver
2463 // ebx: pointer to C function (C callee-saved)
2464 // ebp: frame pointer (restored after C call)
2465 // esp: stack pointer (restored after C call)
2466 // esi: current context (C callee-saved)
2467 // edi: JS function of the caller (C callee-saved)
2469 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2471 // Enter the exit frame that transitions from JavaScript to C++.
2472 __ EnterExitFrame(save_doubles());
2474 // ebx: pointer to C function (C callee-saved)
2475 // ebp: frame pointer (restored after C call)
2476 // esp: stack pointer (restored after C call)
2477 // edi: number of arguments including receiver (C callee-saved)
2478 // esi: pointer to the first argument (C callee-saved)
2480 // Result returned in eax, or eax+edx if result size is 2.
2482 // Check stack alignment.
2483 if (FLAG_debug_code) {
2484 __ CheckStackAlignment();
2488 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
2489 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
2490 __ mov(Operand(esp, 2 * kPointerSize),
2491 Immediate(ExternalReference::isolate_address(isolate())));
2493 // Result is in eax or edx:eax - do not destroy these registers!
2495 // Check result for exception sentinel.
2496 Label exception_returned;
2497 __ cmp(eax, isolate()->factory()->exception());
2498 __ j(equal, &exception_returned);
2500 // Check that there is no pending exception, otherwise we
2501 // should have returned the exception sentinel.
2502 if (FLAG_debug_code) {
2504 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
2506 ExternalReference pending_exception_address(
2507 Isolate::kPendingExceptionAddress, isolate());
2508 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
2509 // Cannot use check here as it attempts to generate call into runtime.
2510 __ j(equal, &okay, Label::kNear);
2516 // Exit the JavaScript to C++ exit frame.
2517 __ LeaveExitFrame(save_doubles());
2520 // Handling of exception.
2521 __ bind(&exception_returned);
2523 ExternalReference pending_handler_context_address(
2524 Isolate::kPendingHandlerContextAddress, isolate());
2525 ExternalReference pending_handler_code_address(
2526 Isolate::kPendingHandlerCodeAddress, isolate());
2527 ExternalReference pending_handler_offset_address(
2528 Isolate::kPendingHandlerOffsetAddress, isolate());
2529 ExternalReference pending_handler_fp_address(
2530 Isolate::kPendingHandlerFPAddress, isolate());
2531 ExternalReference pending_handler_sp_address(
2532 Isolate::kPendingHandlerSPAddress, isolate());
2534 // Ask the runtime for help to determine the handler. This will set eax to
2535 // contain the current pending exception, don't clobber it.
2536 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
2539 FrameScope scope(masm, StackFrame::MANUAL);
2540 __ PrepareCallCFunction(3, eax);
2541 __ mov(Operand(esp, 0 * kPointerSize), Immediate(0)); // argc.
2542 __ mov(Operand(esp, 1 * kPointerSize), Immediate(0)); // argv.
2543 __ mov(Operand(esp, 2 * kPointerSize),
2544 Immediate(ExternalReference::isolate_address(isolate())));
2545 __ CallCFunction(find_handler, 3);
2548 // Retrieve the handler context, SP and FP.
2549 __ mov(esi, Operand::StaticVariable(pending_handler_context_address));
2550 __ mov(esp, Operand::StaticVariable(pending_handler_sp_address));
2551 __ mov(ebp, Operand::StaticVariable(pending_handler_fp_address));
2553 // If the handler is a JS frame, restore the context to the frame. Note that
2554 // the context will be set to (esi == 0) for non-JS frames.
2557 __ j(zero, &skip, Label::kNear);
2558 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
2561 // Compute the handler entry address and jump to it.
2562 __ mov(edi, Operand::StaticVariable(pending_handler_code_address));
2563 __ mov(edx, Operand::StaticVariable(pending_handler_offset_address));
2564 __ lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
2569 void JSEntryStub::Generate(MacroAssembler* masm) {
2570 Label invoke, handler_entry, exit;
2571 Label not_outermost_js, not_outermost_js_2;
2573 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2579 // Push marker in two places.
2580 int marker = type();
2581 __ push(Immediate(Smi::FromInt(marker))); // context slot
2582 __ push(Immediate(Smi::FromInt(marker))); // function slot
2583 // Save callee-saved registers (C calling conventions).
2588 // Save copies of the top frame descriptor on the stack.
2589 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
2590 __ push(Operand::StaticVariable(c_entry_fp));
2592 // If this is the outermost JS call, set js_entry_sp value.
2593 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
2594 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
2595 __ j(not_equal, ¬_outermost_js, Label::kNear);
2596 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
2597 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
2598 __ jmp(&invoke, Label::kNear);
2599 __ bind(¬_outermost_js);
2600 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
2602 // Jump to a faked try block that does the invoke, with a faked catch
2603 // block that sets the pending exception.
2605 __ bind(&handler_entry);
2606 handler_offset_ = handler_entry.pos();
2607 // Caught exception: Store result (exception) in the pending exception
2608 // field in the JSEnv and return a failure sentinel.
2609 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
2611 __ mov(Operand::StaticVariable(pending_exception), eax);
2612 __ mov(eax, Immediate(isolate()->factory()->exception()));
2615 // Invoke: Link this frame into the handler chain.
2617 __ PushStackHandler();
2619 // Clear any pending exceptions.
2620 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
2621 __ mov(Operand::StaticVariable(pending_exception), edx);
2623 // Fake a receiver (NULL).
2624 __ push(Immediate(0)); // receiver
2626 // Invoke the function by calling through JS entry trampoline builtin and
2627 // pop the faked function when we return. Notice that we cannot store a
2628 // reference to the trampoline code directly in this stub, because the
2629 // builtin stubs may not have been generated yet.
2630 if (type() == StackFrame::ENTRY_CONSTRUCT) {
2631 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
2633 __ mov(edx, Immediate(construct_entry));
2635 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
2636 __ mov(edx, Immediate(entry));
2638 __ mov(edx, Operand(edx, 0)); // deref address
2639 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
2642 // Unlink this frame from the handler chain.
2643 __ PopStackHandler();
2646 // Check if the current stack frame is marked as the outermost JS frame.
2648 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
2649 __ j(not_equal, ¬_outermost_js_2);
2650 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
2651 __ bind(¬_outermost_js_2);
2653 // Restore the top frame descriptor from the stack.
2654 __ pop(Operand::StaticVariable(ExternalReference(
2655 Isolate::kCEntryFPAddress, isolate())));
2657 // Restore callee-saved registers (C calling conventions).
2661 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
2663 // Restore frame pointer and return.
2669 void InstanceOfStub::Generate(MacroAssembler* masm) {
2670 Register const object = edx; // Object (lhs).
2671 Register const function = eax; // Function (rhs).
2672 Register const object_map = ecx; // Map of {object}.
2673 Register const function_map = ebx; // Map of {function}.
2674 Register const function_prototype = function_map; // Prototype of {function}.
2675 Register const scratch = edi;
2677 DCHECK(object.is(InstanceOfDescriptor::LeftRegister()));
2678 DCHECK(function.is(InstanceOfDescriptor::RightRegister()));
2680 // Check if {object} is a smi.
2681 Label object_is_smi;
2682 __ JumpIfSmi(object, &object_is_smi, Label::kNear);
2684 // Lookup the {function} and the {object} map in the global instanceof cache.
2685 // Note: This is safe because we clear the global instanceof cache whenever
2686 // we change the prototype of any object.
2687 Label fast_case, slow_case;
2688 __ mov(object_map, FieldOperand(object, HeapObject::kMapOffset));
2689 __ CompareRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2690 __ j(not_equal, &fast_case, Label::kNear);
2691 __ CompareRoot(object_map, scratch, Heap::kInstanceofCacheMapRootIndex);
2692 __ j(not_equal, &fast_case, Label::kNear);
2693 __ LoadRoot(eax, Heap::kInstanceofCacheAnswerRootIndex);
2696 // If {object} is a smi we can safely return false if {function} is a JS
2697 // function, otherwise we have to miss to the runtime and throw an exception.
2698 __ bind(&object_is_smi);
2699 __ JumpIfSmi(function, &slow_case);
2700 __ CmpObjectType(function, JS_FUNCTION_TYPE, function_map);
2701 __ j(not_equal, &slow_case);
2702 __ LoadRoot(eax, Heap::kFalseValueRootIndex);
2705 // Fast-case: The {function} must be a valid JSFunction.
2706 __ bind(&fast_case);
2707 __ JumpIfSmi(function, &slow_case);
2708 __ CmpObjectType(function, JS_FUNCTION_TYPE, function_map);
2709 __ j(not_equal, &slow_case);
2711 // Ensure that {function} has an instance prototype.
2712 __ test_b(FieldOperand(function_map, Map::kBitFieldOffset),
2713 static_cast<uint8_t>(1 << Map::kHasNonInstancePrototype));
2714 __ j(not_zero, &slow_case);
2716 // Ensure that {function} is not bound.
2717 Register const shared_info = scratch;
2719 FieldOperand(function, JSFunction::kSharedFunctionInfoOffset));
2720 __ BooleanBitTest(shared_info, SharedFunctionInfo::kCompilerHintsOffset,
2721 SharedFunctionInfo::kBoundFunction);
2722 __ j(not_zero, &slow_case);
2724 // Get the "prototype" (or initial map) of the {function}.
2725 __ mov(function_prototype,
2726 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2727 __ AssertNotSmi(function_prototype);
2729 // Resolve the prototype if the {function} has an initial map. Afterwards the
2730 // {function_prototype} will be either the JSReceiver prototype object or the
2731 // hole value, which means that no instances of the {function} were created so
2732 // far and hence we should return false.
2733 Label function_prototype_valid;
2734 Register const function_prototype_map = scratch;
2735 __ CmpObjectType(function_prototype, MAP_TYPE, function_prototype_map);
2736 __ j(not_equal, &function_prototype_valid, Label::kNear);
2737 __ mov(function_prototype,
2738 FieldOperand(function_prototype, Map::kPrototypeOffset));
2739 __ bind(&function_prototype_valid);
2740 __ AssertNotSmi(function_prototype);
2742 // Update the global instanceof cache with the current {object} map and
2743 // {function}. The cached answer will be set when it is known below.
2744 __ StoreRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2745 __ StoreRoot(object_map, scratch, Heap::kInstanceofCacheMapRootIndex);
2747 // Loop through the prototype chain looking for the {function} prototype.
2748 // Assume true, and change to false if not found.
2749 Register const object_prototype = object_map;
2751 __ mov(eax, isolate()->factory()->true_value());
2753 __ mov(object_prototype, FieldOperand(object_map, Map::kPrototypeOffset));
2754 __ cmp(object_prototype, function_prototype);
2755 __ j(equal, &done, Label::kNear);
2756 __ cmp(object_prototype, isolate()->factory()->null_value());
2757 __ mov(object_map, FieldOperand(object_prototype, HeapObject::kMapOffset));
2758 __ j(not_equal, &loop);
2759 __ mov(eax, isolate()->factory()->false_value());
2761 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
2764 // Slow-case: Call the runtime function.
2765 __ bind(&slow_case);
2766 __ pop(scratch); // Pop return address.
2767 __ push(object); // Push {object}.
2768 __ push(function); // Push {function}.
2769 __ push(scratch); // Push return address.
2770 __ TailCallRuntime(Runtime::kInstanceOf, 2, 1);
2774 // -------------------------------------------------------------------------
2775 // StringCharCodeAtGenerator
2777 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
2778 // If the receiver is a smi trigger the non-string case.
2779 STATIC_ASSERT(kSmiTag == 0);
2780 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2781 __ JumpIfSmi(object_, receiver_not_string_);
2783 // Fetch the instance type of the receiver into result register.
2784 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
2785 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2786 // If the receiver is not a string trigger the non-string case.
2787 __ test(result_, Immediate(kIsNotStringMask));
2788 __ j(not_zero, receiver_not_string_);
2791 // If the index is non-smi trigger the non-smi case.
2792 STATIC_ASSERT(kSmiTag == 0);
2793 __ JumpIfNotSmi(index_, &index_not_smi_);
2794 __ bind(&got_smi_index_);
2796 // Check for index out of range.
2797 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
2798 __ j(above_equal, index_out_of_range_);
2800 __ SmiUntag(index_);
2802 Factory* factory = masm->isolate()->factory();
2803 StringCharLoadGenerator::Generate(
2804 masm, factory, object_, index_, result_, &call_runtime_);
2811 void StringCharCodeAtGenerator::GenerateSlow(
2812 MacroAssembler* masm, EmbedMode embed_mode,
2813 const RuntimeCallHelper& call_helper) {
2814 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
2816 // Index is not a smi.
2817 __ bind(&index_not_smi_);
2818 // If index is a heap number, try converting it to an integer.
2820 masm->isolate()->factory()->heap_number_map(),
2823 call_helper.BeforeCall(masm);
2824 if (embed_mode == PART_OF_IC_HANDLER) {
2825 __ push(LoadWithVectorDescriptor::VectorRegister());
2826 __ push(LoadDescriptor::SlotRegister());
2829 __ push(index_); // Consumed by runtime conversion function.
2830 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
2831 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
2833 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
2834 // NumberToSmi discards numbers that are not exact integers.
2835 __ CallRuntime(Runtime::kNumberToSmi, 1);
2837 if (!index_.is(eax)) {
2838 // Save the conversion result before the pop instructions below
2839 // have a chance to overwrite it.
2840 __ mov(index_, eax);
2843 if (embed_mode == PART_OF_IC_HANDLER) {
2844 __ pop(LoadDescriptor::SlotRegister());
2845 __ pop(LoadWithVectorDescriptor::VectorRegister());
2847 // Reload the instance type.
2848 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
2849 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2850 call_helper.AfterCall(masm);
2851 // If index is still not a smi, it must be out of range.
2852 STATIC_ASSERT(kSmiTag == 0);
2853 __ JumpIfNotSmi(index_, index_out_of_range_);
2854 // Otherwise, return to the fast path.
2855 __ jmp(&got_smi_index_);
2857 // Call runtime. We get here when the receiver is a string and the
2858 // index is a number, but the code of getting the actual character
2859 // is too complex (e.g., when the string needs to be flattened).
2860 __ bind(&call_runtime_);
2861 call_helper.BeforeCall(masm);
2865 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
2866 if (!result_.is(eax)) {
2867 __ mov(result_, eax);
2869 call_helper.AfterCall(masm);
2872 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
2876 // -------------------------------------------------------------------------
2877 // StringCharFromCodeGenerator
2879 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
2880 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2881 STATIC_ASSERT(kSmiTag == 0);
2882 STATIC_ASSERT(kSmiShiftSize == 0);
2883 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
2884 __ test(code_, Immediate(kSmiTagMask |
2885 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
2886 __ j(not_zero, &slow_case_);
2888 Factory* factory = masm->isolate()->factory();
2889 __ Move(result_, Immediate(factory->single_character_string_cache()));
2890 STATIC_ASSERT(kSmiTag == 0);
2891 STATIC_ASSERT(kSmiTagSize == 1);
2892 STATIC_ASSERT(kSmiShiftSize == 0);
2893 // At this point code register contains smi tagged one byte char code.
2894 __ mov(result_, FieldOperand(result_,
2895 code_, times_half_pointer_size,
2896 FixedArray::kHeaderSize));
2897 __ cmp(result_, factory->undefined_value());
2898 __ j(equal, &slow_case_);
2903 void StringCharFromCodeGenerator::GenerateSlow(
2904 MacroAssembler* masm,
2905 const RuntimeCallHelper& call_helper) {
2906 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
2908 __ bind(&slow_case_);
2909 call_helper.BeforeCall(masm);
2911 __ CallRuntime(Runtime::kCharFromCode, 1);
2912 if (!result_.is(eax)) {
2913 __ mov(result_, eax);
2915 call_helper.AfterCall(masm);
2918 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
2922 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2927 String::Encoding encoding) {
2928 DCHECK(!scratch.is(dest));
2929 DCHECK(!scratch.is(src));
2930 DCHECK(!scratch.is(count));
2932 // Nothing to do for zero characters.
2934 __ test(count, count);
2937 // Make count the number of bytes to copy.
2938 if (encoding == String::TWO_BYTE_ENCODING) {
2944 __ mov_b(scratch, Operand(src, 0));
2945 __ mov_b(Operand(dest, 0), scratch);
2949 __ j(not_zero, &loop);
2955 void SubStringStub::Generate(MacroAssembler* masm) {
2958 // Stack frame on entry.
2959 // esp[0]: return address
2964 // Make sure first argument is a string.
2965 __ mov(eax, Operand(esp, 3 * kPointerSize));
2966 STATIC_ASSERT(kSmiTag == 0);
2967 __ JumpIfSmi(eax, &runtime);
2968 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
2969 __ j(NegateCondition(is_string), &runtime);
2972 // ebx: instance type
2974 // Calculate length of sub string using the smi values.
2975 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
2976 __ JumpIfNotSmi(ecx, &runtime);
2977 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
2978 __ JumpIfNotSmi(edx, &runtime);
2980 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
2981 Label not_original_string;
2982 // Shorter than original string's length: an actual substring.
2983 __ j(below, ¬_original_string, Label::kNear);
2984 // Longer than original string's length or negative: unsafe arguments.
2985 __ j(above, &runtime);
2986 // Return original string.
2987 Counters* counters = isolate()->counters();
2988 __ IncrementCounter(counters->sub_string_native(), 1);
2989 __ ret(3 * kPointerSize);
2990 __ bind(¬_original_string);
2993 __ cmp(ecx, Immediate(Smi::FromInt(1)));
2994 __ j(equal, &single_char);
2997 // ebx: instance type
2998 // ecx: sub string length (smi)
2999 // edx: from index (smi)
3000 // Deal with different string types: update the index if necessary
3001 // and put the underlying string into edi.
3002 Label underlying_unpacked, sliced_string, seq_or_external_string;
3003 // If the string is not indirect, it can only be sequential or external.
3004 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3005 STATIC_ASSERT(kIsIndirectStringMask != 0);
3006 __ test(ebx, Immediate(kIsIndirectStringMask));
3007 __ j(zero, &seq_or_external_string, Label::kNear);
3009 Factory* factory = isolate()->factory();
3010 __ test(ebx, Immediate(kSlicedNotConsMask));
3011 __ j(not_zero, &sliced_string, Label::kNear);
3012 // Cons string. Check whether it is flat, then fetch first part.
3013 // Flat cons strings have an empty second part.
3014 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
3015 factory->empty_string());
3016 __ j(not_equal, &runtime);
3017 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
3018 // Update instance type.
3019 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
3020 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3021 __ jmp(&underlying_unpacked, Label::kNear);
3023 __ bind(&sliced_string);
3024 // Sliced string. Fetch parent and adjust start index by offset.
3025 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
3026 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
3027 // Update instance type.
3028 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
3029 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3030 __ jmp(&underlying_unpacked, Label::kNear);
3032 __ bind(&seq_or_external_string);
3033 // Sequential or external string. Just move string to the expected register.
3036 __ bind(&underlying_unpacked);
3038 if (FLAG_string_slices) {
3040 // edi: underlying subject string
3041 // ebx: instance type of underlying subject string
3042 // edx: adjusted start index (smi)
3043 // ecx: length (smi)
3044 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
3045 // Short slice. Copy instead of slicing.
3046 __ j(less, ©_routine);
3047 // Allocate new sliced string. At this point we do not reload the instance
3048 // type including the string encoding because we simply rely on the info
3049 // provided by the original string. It does not matter if the original
3050 // string's encoding is wrong because we always have to recheck encoding of
3051 // the newly created string's parent anyways due to externalized strings.
3052 Label two_byte_slice, set_slice_header;
3053 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
3054 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3055 __ test(ebx, Immediate(kStringEncodingMask));
3056 __ j(zero, &two_byte_slice, Label::kNear);
3057 __ AllocateOneByteSlicedString(eax, ebx, no_reg, &runtime);
3058 __ jmp(&set_slice_header, Label::kNear);
3059 __ bind(&two_byte_slice);
3060 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
3061 __ bind(&set_slice_header);
3062 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
3063 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
3064 Immediate(String::kEmptyHashField));
3065 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
3066 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
3067 __ IncrementCounter(counters->sub_string_native(), 1);
3068 __ ret(3 * kPointerSize);
3070 __ bind(©_routine);
3073 // edi: underlying subject string
3074 // ebx: instance type of underlying subject string
3075 // edx: adjusted start index (smi)
3076 // ecx: length (smi)
3077 // The subject string can only be external or sequential string of either
3078 // encoding at this point.
3079 Label two_byte_sequential, runtime_drop_two, sequential_string;
3080 STATIC_ASSERT(kExternalStringTag != 0);
3081 STATIC_ASSERT(kSeqStringTag == 0);
3082 __ test_b(ebx, kExternalStringTag);
3083 __ j(zero, &sequential_string);
3085 // Handle external string.
3086 // Rule out short external strings.
3087 STATIC_ASSERT(kShortExternalStringTag != 0);
3088 __ test_b(ebx, kShortExternalStringMask);
3089 __ j(not_zero, &runtime);
3090 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
3091 // Move the pointer so that offset-wise, it looks like a sequential string.
3092 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3093 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3095 __ bind(&sequential_string);
3096 // Stash away (adjusted) index and (underlying) string.
3100 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
3101 __ test_b(ebx, kStringEncodingMask);
3102 __ j(zero, &two_byte_sequential);
3104 // Sequential one byte string. Allocate the result.
3105 __ AllocateOneByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
3107 // eax: result string
3108 // ecx: result string length
3109 // Locate first character of result.
3111 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3112 // Load string argument and locate character of sub string start.
3116 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqOneByteString::kHeaderSize));
3118 // eax: result string
3119 // ecx: result length
3120 // edi: first character of result
3121 // edx: character of sub string start
3122 StringHelper::GenerateCopyCharacters(
3123 masm, edi, edx, ecx, ebx, String::ONE_BYTE_ENCODING);
3124 __ IncrementCounter(counters->sub_string_native(), 1);
3125 __ ret(3 * kPointerSize);
3127 __ bind(&two_byte_sequential);
3128 // Sequential two-byte string. Allocate the result.
3129 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
3131 // eax: result string
3132 // ecx: result string length
3133 // Locate first character of result.
3136 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3137 // Load string argument and locate character of sub string start.
3140 // As from is a smi it is 2 times the value which matches the size of a two
3142 STATIC_ASSERT(kSmiTag == 0);
3143 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3144 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqTwoByteString::kHeaderSize));
3146 // eax: result string
3147 // ecx: result length
3148 // edi: first character of result
3149 // edx: character of sub string start
3150 StringHelper::GenerateCopyCharacters(
3151 masm, edi, edx, ecx, ebx, String::TWO_BYTE_ENCODING);
3152 __ IncrementCounter(counters->sub_string_native(), 1);
3153 __ ret(3 * kPointerSize);
3155 // Drop pushed values on the stack before tail call.
3156 __ bind(&runtime_drop_two);
3159 // Just jump to runtime to create the sub string.
3161 __ TailCallRuntime(Runtime::kSubString, 3, 1);
3163 __ bind(&single_char);
3165 // ebx: instance type
3166 // ecx: sub string length (smi)
3167 // edx: from index (smi)
3168 StringCharAtGenerator generator(eax, edx, ecx, eax, &runtime, &runtime,
3169 &runtime, STRING_INDEX_IS_NUMBER,
3170 RECEIVER_IS_STRING);
3171 generator.GenerateFast(masm);
3172 __ ret(3 * kPointerSize);
3173 generator.SkipSlow(masm, &runtime);
3177 void ToNumberStub::Generate(MacroAssembler* masm) {
3178 // The ToNumber stub takes one argument in eax.
3180 __ JumpIfNotSmi(eax, ¬_smi, Label::kNear);
3184 Label not_heap_number;
3185 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
3186 __ j(not_equal, ¬_heap_number, Label::kNear);
3188 __ bind(¬_heap_number);
3190 Label not_string, slow_string;
3191 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
3194 __ j(above_equal, ¬_string, Label::kNear);
3195 // Check if string has a cached array index.
3196 __ test(FieldOperand(eax, String::kHashFieldOffset),
3197 Immediate(String::kContainsCachedArrayIndexMask));
3198 __ j(not_zero, &slow_string, Label::kNear);
3199 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
3200 __ IndexFromHash(eax, eax);
3202 __ bind(&slow_string);
3203 __ pop(ecx); // Pop return address.
3204 __ push(eax); // Push argument.
3205 __ push(ecx); // Push return address.
3206 __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
3207 __ bind(¬_string);
3210 __ CmpInstanceType(edi, ODDBALL_TYPE);
3211 __ j(not_equal, ¬_oddball, Label::kNear);
3212 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
3214 __ bind(¬_oddball);
3216 __ pop(ecx); // Pop return address.
3217 __ push(eax); // Push argument.
3218 __ push(ecx); // Push return address.
3219 __ TailCallRuntime(Runtime::kToNumber, 1, 1);
3223 void ToStringStub::Generate(MacroAssembler* masm) {
3224 // The ToString stub takes one argument in eax.
3226 __ JumpIfSmi(eax, &is_number, Label::kNear);
3229 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
3231 // edi: receiver map
3232 __ j(above_equal, ¬_string, Label::kNear);
3234 __ bind(¬_string);
3236 Label not_heap_number;
3237 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
3238 __ j(not_equal, ¬_heap_number, Label::kNear);
3239 __ bind(&is_number);
3240 NumberToStringStub stub(isolate());
3241 __ TailCallStub(&stub);
3242 __ bind(¬_heap_number);
3245 __ CmpInstanceType(edi, ODDBALL_TYPE);
3246 __ j(not_equal, ¬_oddball, Label::kNear);
3247 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
3249 __ bind(¬_oddball);
3251 __ pop(ecx); // Pop return address.
3252 __ push(eax); // Push argument.
3253 __ push(ecx); // Push return address.
3254 __ TailCallRuntime(Runtime::kToString, 1, 1);
3258 void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
3262 Register scratch2) {
3263 Register length = scratch1;
3266 Label strings_not_equal, check_zero_length;
3267 __ mov(length, FieldOperand(left, String::kLengthOffset));
3268 __ cmp(length, FieldOperand(right, String::kLengthOffset));
3269 __ j(equal, &check_zero_length, Label::kNear);
3270 __ bind(&strings_not_equal);
3271 __ Move(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
3274 // Check if the length is zero.
3275 Label compare_chars;
3276 __ bind(&check_zero_length);
3277 STATIC_ASSERT(kSmiTag == 0);
3278 __ test(length, length);
3279 __ j(not_zero, &compare_chars, Label::kNear);
3280 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3283 // Compare characters.
3284 __ bind(&compare_chars);
3285 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
3286 &strings_not_equal, Label::kNear);
3288 // Characters are equal.
3289 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3294 void StringHelper::GenerateCompareFlatOneByteStrings(
3295 MacroAssembler* masm, Register left, Register right, Register scratch1,
3296 Register scratch2, Register scratch3) {
3297 Counters* counters = masm->isolate()->counters();
3298 __ IncrementCounter(counters->string_compare_native(), 1);
3300 // Find minimum length.
3302 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
3303 __ mov(scratch3, scratch1);
3304 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
3306 Register length_delta = scratch3;
3308 __ j(less_equal, &left_shorter, Label::kNear);
3309 // Right string is shorter. Change scratch1 to be length of right string.
3310 __ sub(scratch1, length_delta);
3311 __ bind(&left_shorter);
3313 Register min_length = scratch1;
3315 // If either length is zero, just compare lengths.
3316 Label compare_lengths;
3317 __ test(min_length, min_length);
3318 __ j(zero, &compare_lengths, Label::kNear);
3320 // Compare characters.
3321 Label result_not_equal;
3322 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
3323 &result_not_equal, Label::kNear);
3325 // Compare lengths - strings up to min-length are equal.
3326 __ bind(&compare_lengths);
3327 __ test(length_delta, length_delta);
3328 Label length_not_equal;
3329 __ j(not_zero, &length_not_equal, Label::kNear);
3332 STATIC_ASSERT(EQUAL == 0);
3333 STATIC_ASSERT(kSmiTag == 0);
3334 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3337 Label result_greater;
3339 __ bind(&length_not_equal);
3340 __ j(greater, &result_greater, Label::kNear);
3341 __ jmp(&result_less, Label::kNear);
3342 __ bind(&result_not_equal);
3343 __ j(above, &result_greater, Label::kNear);
3344 __ bind(&result_less);
3347 __ Move(eax, Immediate(Smi::FromInt(LESS)));
3350 // Result is GREATER.
3351 __ bind(&result_greater);
3352 __ Move(eax, Immediate(Smi::FromInt(GREATER)));
3357 void StringHelper::GenerateOneByteCharsCompareLoop(
3358 MacroAssembler* masm, Register left, Register right, Register length,
3359 Register scratch, Label* chars_not_equal,
3360 Label::Distance chars_not_equal_near) {
3361 // Change index to run from -length to -1 by adding length to string
3362 // start. This means that loop ends when index reaches zero, which
3363 // doesn't need an additional compare.
3364 __ SmiUntag(length);
3366 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
3368 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
3370 Register index = length; // index = -length;
3375 __ mov_b(scratch, Operand(left, index, times_1, 0));
3376 __ cmpb(scratch, Operand(right, index, times_1, 0));
3377 __ j(not_equal, chars_not_equal, chars_not_equal_near);
3379 __ j(not_zero, &loop);
3383 void StringCompareStub::Generate(MacroAssembler* masm) {
3386 // Stack frame on entry.
3387 // esp[0]: return address
3388 // esp[4]: right string
3389 // esp[8]: left string
3391 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
3392 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
3396 __ j(not_equal, ¬_same, Label::kNear);
3397 STATIC_ASSERT(EQUAL == 0);
3398 STATIC_ASSERT(kSmiTag == 0);
3399 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3400 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1);
3401 __ ret(2 * kPointerSize);
3405 // Check that both objects are sequential one-byte strings.
3406 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx, &runtime);
3408 // Compare flat one-byte strings.
3409 // Drop arguments from the stack.
3411 __ add(esp, Immediate(2 * kPointerSize));
3413 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
3416 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
3417 // tagged as a small integer.
3419 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
3423 void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3424 // ----------- S t a t e -------------
3427 // -- esp[0] : return address
3428 // -----------------------------------
3430 // Load ecx with the allocation site. We stick an undefined dummy value here
3431 // and replace it with the real allocation site later when we instantiate this
3432 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3433 __ mov(ecx, handle(isolate()->heap()->undefined_value()));
3435 // Make sure that we actually patched the allocation site.
3436 if (FLAG_debug_code) {
3437 __ test(ecx, Immediate(kSmiTagMask));
3438 __ Assert(not_equal, kExpectedAllocationSite);
3439 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
3440 isolate()->factory()->allocation_site_map());
3441 __ Assert(equal, kExpectedAllocationSite);
3444 // Tail call into the stub that handles binary operations with allocation
3446 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3447 __ TailCallStub(&stub);
3451 void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3452 DCHECK(state() == CompareICState::SMI);
3456 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
3458 if (GetCondition() == equal) {
3459 // For equality we do not care about the sign of the result.
3464 __ j(no_overflow, &done, Label::kNear);
3465 // Correct sign of result in case of overflow.
3477 void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3478 DCHECK(state() == CompareICState::NUMBER);
3481 Label unordered, maybe_undefined1, maybe_undefined2;
3484 if (left() == CompareICState::SMI) {
3485 __ JumpIfNotSmi(edx, &miss);
3487 if (right() == CompareICState::SMI) {
3488 __ JumpIfNotSmi(eax, &miss);
3491 // Load left and right operand.
3492 Label done, left, left_smi, right_smi;
3493 __ JumpIfSmi(eax, &right_smi, Label::kNear);
3494 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
3495 isolate()->factory()->heap_number_map());
3496 __ j(not_equal, &maybe_undefined1, Label::kNear);
3497 __ movsd(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
3498 __ jmp(&left, Label::kNear);
3499 __ bind(&right_smi);
3500 __ mov(ecx, eax); // Can't clobber eax because we can still jump away.
3502 __ Cvtsi2sd(xmm1, ecx);
3505 __ JumpIfSmi(edx, &left_smi, Label::kNear);
3506 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
3507 isolate()->factory()->heap_number_map());
3508 __ j(not_equal, &maybe_undefined2, Label::kNear);
3509 __ movsd(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
3512 __ mov(ecx, edx); // Can't clobber edx because we can still jump away.
3514 __ Cvtsi2sd(xmm0, ecx);
3517 // Compare operands.
3518 __ ucomisd(xmm0, xmm1);
3520 // Don't base result on EFLAGS when a NaN is involved.
3521 __ j(parity_even, &unordered, Label::kNear);
3523 // Return a result of -1, 0, or 1, based on EFLAGS.
3524 // Performing mov, because xor would destroy the flag register.
3525 __ mov(eax, 0); // equal
3526 __ mov(ecx, Immediate(Smi::FromInt(1)));
3527 __ cmov(above, eax, ecx);
3528 __ mov(ecx, Immediate(Smi::FromInt(-1)));
3529 __ cmov(below, eax, ecx);
3532 __ bind(&unordered);
3533 __ bind(&generic_stub);
3534 CompareICStub stub(isolate(), op(), strength(), CompareICState::GENERIC,
3535 CompareICState::GENERIC, CompareICState::GENERIC);
3536 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3538 __ bind(&maybe_undefined1);
3539 if (Token::IsOrderedRelationalCompareOp(op())) {
3540 __ cmp(eax, Immediate(isolate()->factory()->undefined_value()));
3541 __ j(not_equal, &miss);
3542 __ JumpIfSmi(edx, &unordered);
3543 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
3544 __ j(not_equal, &maybe_undefined2, Label::kNear);
3548 __ bind(&maybe_undefined2);
3549 if (Token::IsOrderedRelationalCompareOp(op())) {
3550 __ cmp(edx, Immediate(isolate()->factory()->undefined_value()));
3551 __ j(equal, &unordered);
3559 void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3560 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3561 DCHECK(GetCondition() == equal);
3563 // Registers containing left and right operands respectively.
3564 Register left = edx;
3565 Register right = eax;
3566 Register tmp1 = ecx;
3567 Register tmp2 = ebx;
3569 // Check that both operands are heap objects.
3572 STATIC_ASSERT(kSmiTag == 0);
3573 __ and_(tmp1, right);
3574 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3576 // Check that both operands are internalized strings.
3577 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3578 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3579 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3580 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3581 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3583 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
3584 __ j(not_zero, &miss, Label::kNear);
3586 // Internalized strings are compared by identity.
3588 __ cmp(left, right);
3589 // Make sure eax is non-zero. At this point input operands are
3590 // guaranteed to be non-zero.
3591 DCHECK(right.is(eax));
3592 __ j(not_equal, &done, Label::kNear);
3593 STATIC_ASSERT(EQUAL == 0);
3594 STATIC_ASSERT(kSmiTag == 0);
3595 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3604 void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3605 DCHECK(state() == CompareICState::UNIQUE_NAME);
3606 DCHECK(GetCondition() == equal);
3608 // Registers containing left and right operands respectively.
3609 Register left = edx;
3610 Register right = eax;
3611 Register tmp1 = ecx;
3612 Register tmp2 = ebx;
3614 // Check that both operands are heap objects.
3617 STATIC_ASSERT(kSmiTag == 0);
3618 __ and_(tmp1, right);
3619 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3621 // Check that both operands are unique names. This leaves the instance
3622 // types loaded in tmp1 and tmp2.
3623 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3624 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3625 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3626 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3628 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
3629 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
3631 // Unique names are compared by identity.
3633 __ cmp(left, right);
3634 // Make sure eax is non-zero. At this point input operands are
3635 // guaranteed to be non-zero.
3636 DCHECK(right.is(eax));
3637 __ j(not_equal, &done, Label::kNear);
3638 STATIC_ASSERT(EQUAL == 0);
3639 STATIC_ASSERT(kSmiTag == 0);
3640 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3649 void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3650 DCHECK(state() == CompareICState::STRING);
3653 bool equality = Token::IsEqualityOp(op());
3655 // Registers containing left and right operands respectively.
3656 Register left = edx;
3657 Register right = eax;
3658 Register tmp1 = ecx;
3659 Register tmp2 = ebx;
3660 Register tmp3 = edi;
3662 // Check that both operands are heap objects.
3664 STATIC_ASSERT(kSmiTag == 0);
3665 __ and_(tmp1, right);
3666 __ JumpIfSmi(tmp1, &miss);
3668 // Check that both operands are strings. This leaves the instance
3669 // types loaded in tmp1 and tmp2.
3670 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3671 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3672 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3673 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3675 STATIC_ASSERT(kNotStringTag != 0);
3677 __ test(tmp3, Immediate(kIsNotStringMask));
3678 __ j(not_zero, &miss);
3680 // Fast check for identical strings.
3682 __ cmp(left, right);
3683 __ j(not_equal, ¬_same, Label::kNear);
3684 STATIC_ASSERT(EQUAL == 0);
3685 STATIC_ASSERT(kSmiTag == 0);
3686 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3689 // Handle not identical strings.
3692 // Check that both strings are internalized. If they are, we're done
3693 // because we already know they are not identical. But in the case of
3694 // non-equality compare, we still need to determine the order. We
3695 // also know they are both strings.
3698 STATIC_ASSERT(kInternalizedTag == 0);
3700 __ test(tmp1, Immediate(kIsNotInternalizedMask));
3701 __ j(not_zero, &do_compare, Label::kNear);
3702 // Make sure eax is non-zero. At this point input operands are
3703 // guaranteed to be non-zero.
3704 DCHECK(right.is(eax));
3706 __ bind(&do_compare);
3709 // Check that both strings are sequential one-byte.
3711 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
3713 // Compare flat one byte strings. Returns when done.
3715 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
3718 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3722 // Handle more complex cases in runtime.
3724 __ pop(tmp1); // Return address.
3729 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3731 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
3739 void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3740 DCHECK(state() == CompareICState::OBJECT);
3744 __ JumpIfSmi(ecx, &miss, Label::kNear);
3746 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
3747 __ j(not_equal, &miss, Label::kNear);
3748 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
3749 __ j(not_equal, &miss, Label::kNear);
3751 DCHECK(GetCondition() == equal);
3760 void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
3762 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
3765 __ JumpIfSmi(ecx, &miss, Label::kNear);
3767 __ GetWeakValue(edi, cell);
3768 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
3769 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
3771 __ j(not_equal, &miss, Label::kNear);
3773 __ j(not_equal, &miss, Label::kNear);
3783 void CompareICStub::GenerateMiss(MacroAssembler* masm) {
3785 // Call the runtime system in a fresh internal frame.
3786 FrameScope scope(masm, StackFrame::INTERNAL);
3787 __ push(edx); // Preserve edx and eax.
3789 __ push(edx); // And also use them as the arguments.
3791 __ push(Immediate(Smi::FromInt(op())));
3792 __ CallRuntime(Runtime::kCompareIC_Miss, 3);
3793 // Compute the entry point of the rewritten stub.
3794 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
3799 // Do a tail call to the rewritten stub.
3804 // Helper function used to check that the dictionary doesn't contain
3805 // the property. This function may return false negatives, so miss_label
3806 // must always call a backup property check that is complete.
3807 // This function is safe to call if the receiver has fast properties.
3808 // Name must be a unique name and receiver must be a heap object.
3809 void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3812 Register properties,
3815 DCHECK(name->IsUniqueName());
3817 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3818 // not equal to the name and kProbes-th slot is not used (its name is the
3819 // undefined value), it guarantees the hash table doesn't contain the
3820 // property. It's true even if some slots represent deleted properties
3821 // (their names are the hole value).
3822 for (int i = 0; i < kInlinedProbes; i++) {
3823 // Compute the masked index: (hash + i + i * i) & mask.
3824 Register index = r0;
3825 // Capacity is smi 2^n.
3826 __ mov(index, FieldOperand(properties, kCapacityOffset));
3829 Immediate(Smi::FromInt(name->Hash() +
3830 NameDictionary::GetProbeOffset(i))));
3832 // Scale the index by multiplying by the entry size.
3833 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3834 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
3835 Register entity_name = r0;
3836 // Having undefined at this place means the name is not contained.
3837 STATIC_ASSERT(kSmiTagSize == 1);
3838 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
3839 kElementsStartOffset - kHeapObjectTag));
3840 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
3843 // Stop if found the property.
3844 __ cmp(entity_name, Handle<Name>(name));
3848 // Check for the hole and skip.
3849 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
3850 __ j(equal, &good, Label::kNear);
3852 // Check if the entry name is not a unique name.
3853 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3854 __ JumpIfNotUniqueNameInstanceType(
3855 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3859 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3861 __ push(Immediate(Handle<Object>(name)));
3862 __ push(Immediate(name->Hash()));
3865 __ j(not_zero, miss);
3870 // Probe the name dictionary in the |elements| register. Jump to the
3871 // |done| label if a property with the given name is found leaving the
3872 // index into the dictionary in |r0|. Jump to the |miss| label
3874 void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3881 DCHECK(!elements.is(r0));
3882 DCHECK(!elements.is(r1));
3883 DCHECK(!name.is(r0));
3884 DCHECK(!name.is(r1));
3886 __ AssertName(name);
3888 __ mov(r1, FieldOperand(elements, kCapacityOffset));
3889 __ shr(r1, kSmiTagSize); // convert smi to int
3892 // Generate an unrolled loop that performs a few probes before
3893 // giving up. Measurements done on Gmail indicate that 2 probes
3894 // cover ~93% of loads from dictionaries.
3895 for (int i = 0; i < kInlinedProbes; i++) {
3896 // Compute the masked index: (hash + i + i * i) & mask.
3897 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3898 __ shr(r0, Name::kHashShift);
3900 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
3904 // Scale the index by multiplying by the entry size.
3905 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3906 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
3908 // Check if the key is identical to the name.
3909 __ cmp(name, Operand(elements,
3912 kElementsStartOffset - kHeapObjectTag));
3916 NameDictionaryLookupStub stub(masm->isolate(), elements, r1, r0,
3919 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3920 __ shr(r0, Name::kHashShift);
3930 void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
3931 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3932 // we cannot call anything that could cause a GC from this stub.
3933 // Stack frame on entry:
3934 // esp[0 * kPointerSize]: return address.
3935 // esp[1 * kPointerSize]: key's hash.
3936 // esp[2 * kPointerSize]: key.
3938 // dictionary_: NameDictionary to probe.
3939 // result_: used as scratch.
3940 // index_: will hold an index of entry if lookup is successful.
3941 // might alias with result_.
3943 // result_ is zero if lookup failed, non zero otherwise.
3945 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3947 Register scratch = result();
3949 __ mov(scratch, FieldOperand(dictionary(), kCapacityOffset));
3951 __ SmiUntag(scratch);
3954 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3955 // not equal to the name and kProbes-th slot is not used (its name is the
3956 // undefined value), it guarantees the hash table doesn't contain the
3957 // property. It's true even if some slots represent deleted properties
3958 // (their names are the null value).
3959 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3960 // Compute the masked index: (hash + i + i * i) & mask.
3961 __ mov(scratch, Operand(esp, 2 * kPointerSize));
3963 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
3965 __ and_(scratch, Operand(esp, 0));
3967 // Scale the index by multiplying by the entry size.
3968 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3969 __ lea(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
3971 // Having undefined at this place means the name is not contained.
3972 STATIC_ASSERT(kSmiTagSize == 1);
3973 __ mov(scratch, Operand(dictionary(), index(), times_pointer_size,
3974 kElementsStartOffset - kHeapObjectTag));
3975 __ cmp(scratch, isolate()->factory()->undefined_value());
3976 __ j(equal, ¬_in_dictionary);
3978 // Stop if found the property.
3979 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
3980 __ j(equal, &in_dictionary);
3982 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3983 // If we hit a key that is not a unique name during negative
3984 // lookup we have to bailout as this key might be equal to the
3985 // key we are looking for.
3987 // Check if the entry name is not a unique name.
3988 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
3989 __ JumpIfNotUniqueNameInstanceType(
3990 FieldOperand(scratch, Map::kInstanceTypeOffset),
3991 &maybe_in_dictionary);
3995 __ bind(&maybe_in_dictionary);
3996 // If we are doing negative lookup then probing failure should be
3997 // treated as a lookup success. For positive lookup probing failure
3998 // should be treated as lookup failure.
3999 if (mode() == POSITIVE_LOOKUP) {
4000 __ mov(result(), Immediate(0));
4002 __ ret(2 * kPointerSize);
4005 __ bind(&in_dictionary);
4006 __ mov(result(), Immediate(1));
4008 __ ret(2 * kPointerSize);
4010 __ bind(¬_in_dictionary);
4011 __ mov(result(), Immediate(0));
4013 __ ret(2 * kPointerSize);
4017 void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
4019 StoreBufferOverflowStub stub(isolate, kDontSaveFPRegs);
4021 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
4026 // Takes the input in 3 registers: address_ value_ and object_. A pointer to
4027 // the value has just been written into the object, now this stub makes sure
4028 // we keep the GC informed. The word in the object where the value has been
4029 // written is in the address register.
4030 void RecordWriteStub::Generate(MacroAssembler* masm) {
4031 Label skip_to_incremental_noncompacting;
4032 Label skip_to_incremental_compacting;
4034 // The first two instructions are generated with labels so as to get the
4035 // offset fixed up correctly by the bind(Label*) call. We patch it back and
4036 // forth between a compare instructions (a nop in this position) and the
4037 // real branch when we start and stop incremental heap marking.
4038 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
4039 __ jmp(&skip_to_incremental_compacting, Label::kFar);
4041 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4042 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4043 MacroAssembler::kReturnAtEnd);
4048 __ bind(&skip_to_incremental_noncompacting);
4049 GenerateIncremental(masm, INCREMENTAL);
4051 __ bind(&skip_to_incremental_compacting);
4052 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4054 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
4055 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
4056 masm->set_byte_at(0, kTwoByteNopInstruction);
4057 masm->set_byte_at(2, kFiveByteNopInstruction);
4061 void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4064 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4065 Label dont_need_remembered_set;
4067 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
4068 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
4070 &dont_need_remembered_set);
4072 __ CheckPageFlag(regs_.object(),
4074 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4076 &dont_need_remembered_set);
4078 // First notify the incremental marker if necessary, then update the
4080 CheckNeedsToInformIncrementalMarker(
4082 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
4084 InformIncrementalMarker(masm);
4085 regs_.Restore(masm);
4086 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4087 MacroAssembler::kReturnAtEnd);
4089 __ bind(&dont_need_remembered_set);
4092 CheckNeedsToInformIncrementalMarker(
4094 kReturnOnNoNeedToInformIncrementalMarker,
4096 InformIncrementalMarker(masm);
4097 regs_.Restore(masm);
4102 void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
4103 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
4104 int argument_count = 3;
4105 __ PrepareCallCFunction(argument_count, regs_.scratch0());
4106 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
4107 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
4108 __ mov(Operand(esp, 2 * kPointerSize),
4109 Immediate(ExternalReference::isolate_address(isolate())));
4111 AllowExternalCallThatCantCauseGC scope(masm);
4113 ExternalReference::incremental_marking_record_write_function(isolate()),
4116 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
4120 void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4121 MacroAssembler* masm,
4122 OnNoNeedToInformIncrementalMarker on_no_need,
4124 Label object_is_black, need_incremental, need_incremental_pop_object;
4126 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
4127 __ and_(regs_.scratch0(), regs_.object());
4128 __ mov(regs_.scratch1(),
4129 Operand(regs_.scratch0(),
4130 MemoryChunk::kWriteBarrierCounterOffset));
4131 __ sub(regs_.scratch1(), Immediate(1));
4132 __ mov(Operand(regs_.scratch0(),
4133 MemoryChunk::kWriteBarrierCounterOffset),
4135 __ j(negative, &need_incremental);
4137 // Let's look at the color of the object: If it is not black we don't have
4138 // to inform the incremental marker.
4139 __ JumpIfBlack(regs_.object(),
4145 regs_.Restore(masm);
4146 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4147 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4148 MacroAssembler::kReturnAtEnd);
4153 __ bind(&object_is_black);
4155 // Get the value from the slot.
4156 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
4158 if (mode == INCREMENTAL_COMPACTION) {
4159 Label ensure_not_white;
4161 __ CheckPageFlag(regs_.scratch0(), // Contains value.
4162 regs_.scratch1(), // Scratch.
4163 MemoryChunk::kEvacuationCandidateMask,
4168 __ CheckPageFlag(regs_.object(),
4169 regs_.scratch1(), // Scratch.
4170 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4175 __ jmp(&need_incremental);
4177 __ bind(&ensure_not_white);
4180 // We need an extra register for this, so we push the object register
4182 __ push(regs_.object());
4183 __ EnsureNotWhite(regs_.scratch0(), // The value.
4184 regs_.scratch1(), // Scratch.
4185 regs_.object(), // Scratch.
4186 &need_incremental_pop_object,
4188 __ pop(regs_.object());
4190 regs_.Restore(masm);
4191 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4192 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4193 MacroAssembler::kReturnAtEnd);
4198 __ bind(&need_incremental_pop_object);
4199 __ pop(regs_.object());
4201 __ bind(&need_incremental);
4203 // Fall through when we need to inform the incremental marker.
4207 void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4208 // ----------- S t a t e -------------
4209 // -- eax : element value to store
4210 // -- ecx : element index as smi
4211 // -- esp[0] : return address
4212 // -- esp[4] : array literal index in function
4213 // -- esp[8] : array literal
4214 // clobbers ebx, edx, edi
4215 // -----------------------------------
4218 Label double_elements;
4220 Label slow_elements;
4221 Label slow_elements_from_double;
4222 Label fast_elements;
4224 // Get array literal index, array literal and its map.
4225 __ mov(edx, Operand(esp, 1 * kPointerSize));
4226 __ mov(ebx, Operand(esp, 2 * kPointerSize));
4227 __ mov(edi, FieldOperand(ebx, JSObject::kMapOffset));
4229 __ CheckFastElements(edi, &double_elements);
4231 // Check for FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS elements
4232 __ JumpIfSmi(eax, &smi_element);
4233 __ CheckFastSmiElements(edi, &fast_elements, Label::kNear);
4235 // Store into the array literal requires a elements transition. Call into
4238 __ bind(&slow_elements);
4239 __ pop(edi); // Pop return address and remember to put back later for tail
4244 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
4245 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
4247 __ push(edi); // Return return address so that tail call returns to right
4249 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4251 __ bind(&slow_elements_from_double);
4253 __ jmp(&slow_elements);
4255 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
4256 __ bind(&fast_elements);
4257 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4258 __ lea(ecx, FieldOperand(ebx, ecx, times_half_pointer_size,
4259 FixedArrayBase::kHeaderSize));
4260 __ mov(Operand(ecx, 0), eax);
4261 // Update the write barrier for the array store.
4262 __ RecordWrite(ebx, ecx, eax,
4264 EMIT_REMEMBERED_SET,
4268 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS,
4269 // and value is Smi.
4270 __ bind(&smi_element);
4271 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
4272 __ mov(FieldOperand(ebx, ecx, times_half_pointer_size,
4273 FixedArrayBase::kHeaderSize), eax);
4276 // Array literal has ElementsKind of FAST_*_DOUBLE_ELEMENTS.
4277 __ bind(&double_elements);
4280 __ mov(edx, FieldOperand(ebx, JSObject::kElementsOffset));
4281 __ StoreNumberToDoubleElements(eax,
4286 &slow_elements_from_double);
4292 void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4293 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4294 __ call(ces.GetCode(), RelocInfo::CODE_TARGET);
4295 int parameter_count_offset =
4296 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4297 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
4298 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4300 int additional_offset =
4301 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
4302 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
4303 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
4307 void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4308 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4309 LoadICStub stub(isolate(), state());
4310 stub.GenerateForTrampoline(masm);
4314 void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4315 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4316 KeyedLoadICStub stub(isolate(), state());
4317 stub.GenerateForTrampoline(masm);
4321 static void HandleArrayCases(MacroAssembler* masm, Register receiver,
4322 Register key, Register vector, Register slot,
4323 Register feedback, bool is_polymorphic,
4325 // feedback initially contains the feedback array
4326 Label next, next_loop, prepare_next;
4327 Label load_smi_map, compare_map;
4328 Label start_polymorphic;
4333 Register receiver_map = receiver;
4334 Register cached_map = vector;
4336 // Receiver might not be a heap object.
4337 __ JumpIfSmi(receiver, &load_smi_map);
4338 __ mov(receiver_map, FieldOperand(receiver, 0));
4339 __ bind(&compare_map);
4340 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
4342 // A named keyed load might have a 2 element array, all other cases can count
4343 // on an array with at least 2 {map, handler} pairs, so they can go right
4344 // into polymorphic array handling.
4345 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4346 __ j(not_equal, is_polymorphic ? &start_polymorphic : &next);
4348 // found, now call handler.
4349 Register handler = feedback;
4350 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
4353 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4356 if (!is_polymorphic) {
4358 __ cmp(FieldOperand(feedback, FixedArray::kLengthOffset),
4359 Immediate(Smi::FromInt(2)));
4360 __ j(not_equal, &start_polymorphic);
4366 // Polymorphic, we have to loop from 2 to N
4367 __ bind(&start_polymorphic);
4369 Register counter = key;
4370 __ mov(counter, Immediate(Smi::FromInt(2)));
4371 __ bind(&next_loop);
4372 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4373 FixedArray::kHeaderSize));
4374 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4375 __ j(not_equal, &prepare_next);
4376 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
4377 FixedArray::kHeaderSize + kPointerSize));
4381 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4384 __ bind(&prepare_next);
4385 __ add(counter, Immediate(Smi::FromInt(2)));
4386 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
4387 __ j(less, &next_loop);
4389 // We exhausted our array of map handler pairs.
4395 __ bind(&load_smi_map);
4396 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4397 __ jmp(&compare_map);
4401 static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
4402 Register key, Register vector, Register slot,
4403 Register weak_cell, Label* miss) {
4404 // feedback initially contains the feedback array
4405 Label compare_smi_map;
4407 // Move the weak map into the weak_cell register.
4408 Register ic_map = weak_cell;
4409 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
4411 // Receiver might not be a heap object.
4412 __ JumpIfSmi(receiver, &compare_smi_map);
4413 __ cmp(ic_map, FieldOperand(receiver, 0));
4414 __ j(not_equal, miss);
4415 Register handler = weak_cell;
4416 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
4417 FixedArray::kHeaderSize + kPointerSize));
4418 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4421 // In microbenchmarks, it made sense to unroll this code so that the call to
4422 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
4423 __ bind(&compare_smi_map);
4424 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
4425 __ j(not_equal, miss);
4426 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
4427 FixedArray::kHeaderSize + kPointerSize));
4428 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4433 void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
4436 void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4437 GenerateImpl(masm, true);
4441 void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4442 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
4443 Register name = LoadWithVectorDescriptor::NameRegister(); // ecx
4444 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
4445 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
4446 Register scratch = edi;
4447 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4448 FixedArray::kHeaderSize));
4450 // Is it a weak cell?
4452 Label not_array, smi_key, key_okay, miss;
4453 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
4454 __ j(not_equal, &try_array);
4455 HandleMonomorphicCase(masm, receiver, name, vector, slot, scratch, &miss);
4457 // Is it a fixed array?
4458 __ bind(&try_array);
4459 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
4460 __ j(not_equal, ¬_array);
4461 HandleArrayCases(masm, receiver, name, vector, slot, scratch, true, &miss);
4463 __ bind(¬_array);
4464 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4465 __ j(not_equal, &miss);
4468 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4469 Code::ComputeHandlerFlags(Code::LOAD_IC));
4470 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
4471 receiver, name, vector, scratch);
4476 LoadIC::GenerateMiss(masm);
4480 void KeyedLoadICStub::Generate(MacroAssembler* masm) {
4481 GenerateImpl(masm, false);
4485 void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4486 GenerateImpl(masm, true);
4490 void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4491 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
4492 Register key = LoadWithVectorDescriptor::NameRegister(); // ecx
4493 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
4494 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
4495 Register feedback = edi;
4496 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
4497 FixedArray::kHeaderSize));
4498 // Is it a weak cell?
4500 Label not_array, smi_key, key_okay, miss;
4501 __ CompareRoot(FieldOperand(feedback, 0), Heap::kWeakCellMapRootIndex);
4502 __ j(not_equal, &try_array);
4503 HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, &miss);
4505 __ bind(&try_array);
4506 // Is it a fixed array?
4507 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
4508 __ j(not_equal, ¬_array);
4510 // We have a polymorphic element handler.
4511 Label polymorphic, try_poly_name;
4512 __ bind(&polymorphic);
4513 HandleArrayCases(masm, receiver, key, vector, slot, feedback, true, &miss);
4515 __ bind(¬_array);
4517 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
4518 __ j(not_equal, &try_poly_name);
4519 Handle<Code> megamorphic_stub =
4520 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4521 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4523 __ bind(&try_poly_name);
4524 // We might have a name in feedback, and a fixed array in the next slot.
4525 __ cmp(key, feedback);
4526 __ j(not_equal, &miss);
4527 // If the name comparison succeeded, we know we have a fixed array with
4528 // at least one map/handler pair.
4529 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
4530 FixedArray::kHeaderSize + kPointerSize));
4531 HandleArrayCases(masm, receiver, key, vector, slot, feedback, false, &miss);
4534 KeyedLoadIC::GenerateMiss(masm);
4538 void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4539 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4540 VectorStoreICStub stub(isolate(), state());
4541 stub.GenerateForTrampoline(masm);
4545 void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4546 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4547 VectorKeyedStoreICStub stub(isolate(), state());
4548 stub.GenerateForTrampoline(masm);
4552 void VectorStoreICStub::Generate(MacroAssembler* masm) {
4553 GenerateImpl(masm, false);
4557 void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4558 GenerateImpl(masm, true);
4562 // value is on the stack already.
4563 static void HandlePolymorphicStoreCase(MacroAssembler* masm, Register receiver,
4564 Register key, Register vector,
4565 Register slot, Register feedback,
4567 // feedback initially contains the feedback array
4568 Label next, next_loop, prepare_next;
4569 Label load_smi_map, compare_map;
4570 Label start_polymorphic;
4575 Register receiver_map = receiver;
4576 Register cached_map = vector;
4578 // Receiver might not be a heap object.
4579 __ JumpIfSmi(receiver, &load_smi_map);
4580 __ mov(receiver_map, FieldOperand(receiver, 0));
4581 __ bind(&compare_map);
4582 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
4584 // A named keyed store might have a 2 element array, all other cases can count
4585 // on an array with at least 2 {map, handler} pairs, so they can go right
4586 // into polymorphic array handling.
4587 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4588 __ j(not_equal, &start_polymorphic);
4590 // found, now call handler.
4591 Register handler = feedback;
4592 DCHECK(handler.is(VectorStoreICDescriptor::ValueRegister()));
4593 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
4596 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4597 __ xchg(handler, Operand(esp, 0));
4600 // Polymorphic, we have to loop from 2 to N
4602 // TODO(mvstanton): I think there is a bug here, we are assuming the
4603 // array has more than one map/handler pair, but we call this function in the
4604 // keyed store with a string key case, where it might be just an array of two
4607 __ bind(&start_polymorphic);
4609 Register counter = key;
4610 __ mov(counter, Immediate(Smi::FromInt(2)));
4611 __ bind(&next_loop);
4612 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4613 FixedArray::kHeaderSize));
4614 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4615 __ j(not_equal, &prepare_next);
4616 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
4617 FixedArray::kHeaderSize + kPointerSize));
4621 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4622 __ xchg(handler, Operand(esp, 0));
4625 __ bind(&prepare_next);
4626 __ add(counter, Immediate(Smi::FromInt(2)));
4627 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
4628 __ j(less, &next_loop);
4630 // We exhausted our array of map handler pairs.
4636 __ bind(&load_smi_map);
4637 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4638 __ jmp(&compare_map);
4642 static void HandleMonomorphicStoreCase(MacroAssembler* masm, Register receiver,
4643 Register key, Register vector,
4644 Register slot, Register weak_cell,
4646 // The store ic value is on the stack.
4647 DCHECK(weak_cell.is(VectorStoreICDescriptor::ValueRegister()));
4649 // feedback initially contains the feedback array
4650 Label compare_smi_map;
4652 // Move the weak map into the weak_cell register.
4653 Register ic_map = weak_cell;
4654 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
4656 // Receiver might not be a heap object.
4657 __ JumpIfSmi(receiver, &compare_smi_map);
4658 __ cmp(ic_map, FieldOperand(receiver, 0));
4659 __ j(not_equal, miss);
4660 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
4661 FixedArray::kHeaderSize + kPointerSize));
4662 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
4663 // Put the store ic value back in it's register.
4664 __ xchg(weak_cell, Operand(esp, 0));
4665 // "return" to the handler.
4668 // In microbenchmarks, it made sense to unroll this code so that the call to
4669 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
4670 __ bind(&compare_smi_map);
4671 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
4672 __ j(not_equal, miss);
4673 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
4674 FixedArray::kHeaderSize + kPointerSize));
4675 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
4676 // Put the store ic value back in it's register.
4677 __ xchg(weak_cell, Operand(esp, 0));
4678 // "return" to the handler.
4683 void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4684 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
4685 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
4686 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
4687 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
4688 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
4693 Register scratch = value;
4694 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4695 FixedArray::kHeaderSize));
4697 // Is it a weak cell?
4699 Label not_array, smi_key, key_okay;
4700 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
4701 __ j(not_equal, &try_array);
4702 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
4704 // Is it a fixed array?
4705 __ bind(&try_array);
4706 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
4707 __ j(not_equal, ¬_array);
4708 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
4710 __ bind(¬_array);
4711 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4712 __ j(not_equal, &miss);
4717 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4718 Code::ComputeHandlerFlags(Code::STORE_IC));
4719 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::STORE_IC, code_flags,
4720 receiver, key, slot, no_reg);
4724 __ jmp(&no_pop_miss);
4728 __ bind(&no_pop_miss);
4729 StoreIC::GenerateMiss(masm);
4733 void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4734 GenerateImpl(masm, false);
4738 void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4739 GenerateImpl(masm, true);
4743 static void HandlePolymorphicKeyedStoreCase(MacroAssembler* masm,
4744 Register receiver, Register key,
4745 Register vector, Register slot,
4746 Register feedback, Label* miss) {
4747 // feedback initially contains the feedback array
4748 Label next, next_loop, prepare_next;
4749 Label load_smi_map, compare_map;
4750 Label transition_call;
4756 Register receiver_map = receiver;
4757 Register cached_map = vector;
4759 // Receiver might not be a heap object.
4760 __ JumpIfSmi(receiver, &load_smi_map);
4761 __ mov(receiver_map, FieldOperand(receiver, 0));
4762 __ bind(&compare_map);
4764 // Polymorphic, we have to loop from 0 to N - 1
4766 // On the stack we have:
4771 Register counter = key;
4772 __ mov(counter, Immediate(Smi::FromInt(0)));
4773 __ bind(&next_loop);
4774 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4775 FixedArray::kHeaderSize));
4776 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4777 __ j(not_equal, &prepare_next);
4778 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4779 FixedArray::kHeaderSize + kPointerSize));
4780 __ CompareRoot(cached_map, Heap::kUndefinedValueRootIndex);
4781 __ j(not_equal, &transition_call);
4782 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
4783 FixedArray::kHeaderSize + 2 * kPointerSize));
4787 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
4788 __ xchg(feedback, Operand(esp, 0));
4791 __ bind(&transition_call);
4792 // Oh holy hell this will be tough.
4793 // The map goes in vector register.
4794 __ mov(receiver, FieldOperand(cached_map, WeakCell::kValueOffset));
4795 // The weak cell may have been cleared.
4796 __ JumpIfSmi(receiver, &pop_and_miss);
4797 // slot goes on the stack, and holds return address.
4798 __ xchg(slot, Operand(esp, 4 * kPointerSize));
4799 // Get the handler in value.
4800 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
4801 FixedArray::kHeaderSize + 2 * kPointerSize));
4802 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
4803 // Pop key into place.
4805 // Put the return address on top of stack, vector goes in slot.
4806 __ xchg(slot, Operand(esp, 0));
4807 // put the map on the stack, receiver holds receiver.
4808 __ xchg(receiver, Operand(esp, 1 * kPointerSize));
4809 // put the vector on the stack, slot holds value.
4810 __ xchg(slot, Operand(esp, 2 * kPointerSize));
4811 // feedback (value) = value, slot = handler.
4812 __ xchg(feedback, slot);
4815 __ bind(&prepare_next);
4816 __ add(counter, Immediate(Smi::FromInt(3)));
4817 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
4818 __ j(less, &next_loop);
4820 // We exhausted our array of map handler pairs.
4821 __ bind(&pop_and_miss);
4827 __ bind(&load_smi_map);
4828 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4829 __ jmp(&compare_map);
4833 void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4834 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
4835 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
4836 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
4837 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
4838 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
4843 Register scratch = value;
4844 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4845 FixedArray::kHeaderSize));
4847 // Is it a weak cell?
4849 Label not_array, smi_key, key_okay;
4850 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
4851 __ j(not_equal, &try_array);
4852 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
4854 // Is it a fixed array?
4855 __ bind(&try_array);
4856 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
4857 __ j(not_equal, ¬_array);
4858 HandlePolymorphicKeyedStoreCase(masm, receiver, key, vector, slot, scratch,
4861 __ bind(¬_array);
4862 Label try_poly_name;
4863 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4864 __ j(not_equal, &try_poly_name);
4868 Handle<Code> megamorphic_stub =
4869 KeyedStoreIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4870 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4872 __ bind(&try_poly_name);
4873 // We might have a name in feedback, and a fixed array in the next slot.
4874 __ cmp(key, scratch);
4875 __ j(not_equal, &miss);
4876 // If the name comparison succeeded, we know we have a fixed array with
4877 // at least one map/handler pair.
4878 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4879 FixedArray::kHeaderSize + kPointerSize));
4880 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
4884 KeyedStoreIC::GenerateMiss(masm);
4888 void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4889 EmitLoadTypeFeedbackVector(masm, ebx);
4890 CallICStub stub(isolate(), state());
4891 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4895 void CallIC_ArrayTrampolineStub::Generate(MacroAssembler* masm) {
4896 EmitLoadTypeFeedbackVector(masm, ebx);
4897 CallIC_ArrayStub stub(isolate(), state());
4898 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4902 void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4903 if (masm->isolate()->function_entry_hook() != NULL) {
4904 ProfileEntryHookStub stub(masm->isolate());
4905 masm->CallStub(&stub);
4910 void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4911 // Save volatile registers.
4912 const int kNumSavedRegisters = 3;
4917 // Calculate and push the original stack pointer.
4918 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4921 // Retrieve our return address and use it to calculate the calling
4922 // function's address.
4923 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4924 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
4927 // Call the entry hook.
4928 DCHECK(isolate()->function_entry_hook() != NULL);
4929 __ call(FUNCTION_ADDR(isolate()->function_entry_hook()),
4930 RelocInfo::RUNTIME_ENTRY);
4931 __ add(esp, Immediate(2 * kPointerSize));
4943 static void CreateArrayDispatch(MacroAssembler* masm,
4944 AllocationSiteOverrideMode mode) {
4945 if (mode == DISABLE_ALLOCATION_SITES) {
4946 T stub(masm->isolate(),
4947 GetInitialFastElementsKind(),
4949 __ TailCallStub(&stub);
4950 } else if (mode == DONT_OVERRIDE) {
4951 int last_index = GetSequenceIndexFromFastElementsKind(
4952 TERMINAL_FAST_ELEMENTS_KIND);
4953 for (int i = 0; i <= last_index; ++i) {
4955 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4957 __ j(not_equal, &next);
4958 T stub(masm->isolate(), kind);
4959 __ TailCallStub(&stub);
4963 // If we reached this point there is a problem.
4964 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4971 static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4972 AllocationSiteOverrideMode mode) {
4973 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4974 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
4975 // eax - number of arguments
4976 // edi - constructor?
4977 // esp[0] - return address
4978 // esp[4] - last argument
4979 Label normal_sequence;
4980 if (mode == DONT_OVERRIDE) {
4981 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4982 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4983 STATIC_ASSERT(FAST_ELEMENTS == 2);
4984 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4985 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4986 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4988 // is the low bit set? If so, we are holey and that is good.
4990 __ j(not_zero, &normal_sequence);
4993 // look at the first argument
4994 __ mov(ecx, Operand(esp, kPointerSize));
4996 __ j(zero, &normal_sequence);
4998 if (mode == DISABLE_ALLOCATION_SITES) {
4999 ElementsKind initial = GetInitialFastElementsKind();
5000 ElementsKind holey_initial = GetHoleyElementsKind(initial);
5002 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
5004 DISABLE_ALLOCATION_SITES);
5005 __ TailCallStub(&stub_holey);
5007 __ bind(&normal_sequence);
5008 ArraySingleArgumentConstructorStub stub(masm->isolate(),
5010 DISABLE_ALLOCATION_SITES);
5011 __ TailCallStub(&stub);
5012 } else if (mode == DONT_OVERRIDE) {
5013 // We are going to create a holey array, but our kind is non-holey.
5014 // Fix kind and retry.
5017 if (FLAG_debug_code) {
5018 Handle<Map> allocation_site_map =
5019 masm->isolate()->factory()->allocation_site_map();
5020 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
5021 __ Assert(equal, kExpectedAllocationSite);
5024 // Save the resulting elements kind in type info. We can't just store r3
5025 // in the AllocationSite::transition_info field because elements kind is
5026 // restricted to a portion of the field...upper bits need to be left alone.
5027 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
5028 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
5029 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
5031 __ bind(&normal_sequence);
5032 int last_index = GetSequenceIndexFromFastElementsKind(
5033 TERMINAL_FAST_ELEMENTS_KIND);
5034 for (int i = 0; i <= last_index; ++i) {
5036 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
5038 __ j(not_equal, &next);
5039 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
5040 __ TailCallStub(&stub);
5044 // If we reached this point there is a problem.
5045 __ Abort(kUnexpectedElementsKindInArrayConstructor);
5053 static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
5054 int to_index = GetSequenceIndexFromFastElementsKind(
5055 TERMINAL_FAST_ELEMENTS_KIND);
5056 for (int i = 0; i <= to_index; ++i) {
5057 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
5058 T stub(isolate, kind);
5060 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
5061 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
5068 void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
5069 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
5071 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
5073 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
5078 void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
5080 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
5081 for (int i = 0; i < 2; i++) {
5082 // For internal arrays we only need a few things
5083 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
5085 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
5087 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
5093 void ArrayConstructorStub::GenerateDispatchToArrayStub(
5094 MacroAssembler* masm,
5095 AllocationSiteOverrideMode mode) {
5096 if (argument_count() == ANY) {
5097 Label not_zero_case, not_one_case;
5099 __ j(not_zero, ¬_zero_case);
5100 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
5102 __ bind(¬_zero_case);
5104 __ j(greater, ¬_one_case);
5105 CreateArrayDispatchOneArgument(masm, mode);
5107 __ bind(¬_one_case);
5108 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
5109 } else if (argument_count() == NONE) {
5110 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
5111 } else if (argument_count() == ONE) {
5112 CreateArrayDispatchOneArgument(masm, mode);
5113 } else if (argument_count() == MORE_THAN_ONE) {
5114 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
5121 void ArrayConstructorStub::Generate(MacroAssembler* masm) {
5122 // ----------- S t a t e -------------
5123 // -- eax : argc (only if argument_count() is ANY or MORE_THAN_ONE)
5124 // -- ebx : AllocationSite or undefined
5125 // -- edi : constructor
5126 // -- edx : Original constructor
5127 // -- esp[0] : return address
5128 // -- esp[4] : last argument
5129 // -----------------------------------
5130 if (FLAG_debug_code) {
5131 // The array construct code is only set for the global and natives
5132 // builtin Array functions which always have maps.
5134 // Initial map for the builtin Array function should be a map.
5135 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5136 // Will both indicate a NULL and a Smi.
5137 __ test(ecx, Immediate(kSmiTagMask));
5138 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
5139 __ CmpObjectType(ecx, MAP_TYPE, ecx);
5140 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
5142 // We should either have undefined in ebx or a valid AllocationSite
5143 __ AssertUndefinedOrAllocationSite(ebx);
5149 __ j(not_equal, &subclassing);
5152 // If the feedback vector is the undefined value call an array constructor
5153 // that doesn't use AllocationSites.
5154 __ cmp(ebx, isolate()->factory()->undefined_value());
5155 __ j(equal, &no_info);
5157 // Only look at the lower 16 bits of the transition info.
5158 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
5160 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
5161 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
5162 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
5165 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
5168 __ bind(&subclassing);
5169 __ pop(ecx); // return address.
5174 switch (argument_count()) {
5177 __ add(eax, Immediate(2));
5180 __ mov(eax, Immediate(2));
5183 __ mov(eax, Immediate(3));
5188 __ JumpToExternalReference(
5189 ExternalReference(Runtime::kArrayConstructorWithSubclassing, isolate()));
5193 void InternalArrayConstructorStub::GenerateCase(
5194 MacroAssembler* masm, ElementsKind kind) {
5195 Label not_zero_case, not_one_case;
5196 Label normal_sequence;
5199 __ j(not_zero, ¬_zero_case);
5200 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
5201 __ TailCallStub(&stub0);
5203 __ bind(¬_zero_case);
5205 __ j(greater, ¬_one_case);
5207 if (IsFastPackedElementsKind(kind)) {
5208 // We might need to create a holey array
5209 // look at the first argument
5210 __ mov(ecx, Operand(esp, kPointerSize));
5212 __ j(zero, &normal_sequence);
5214 InternalArraySingleArgumentConstructorStub
5215 stub1_holey(isolate(), GetHoleyElementsKind(kind));
5216 __ TailCallStub(&stub1_holey);
5219 __ bind(&normal_sequence);
5220 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
5221 __ TailCallStub(&stub1);
5223 __ bind(¬_one_case);
5224 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
5225 __ TailCallStub(&stubN);
5229 void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
5230 // ----------- S t a t e -------------
5232 // -- edi : constructor
5233 // -- esp[0] : return address
5234 // -- esp[4] : last argument
5235 // -----------------------------------
5237 if (FLAG_debug_code) {
5238 // The array construct code is only set for the global and natives
5239 // builtin Array functions which always have maps.
5241 // Initial map for the builtin Array function should be a map.
5242 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5243 // Will both indicate a NULL and a Smi.
5244 __ test(ecx, Immediate(kSmiTagMask));
5245 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
5246 __ CmpObjectType(ecx, MAP_TYPE, ecx);
5247 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
5250 // Figure out the right elements kind
5251 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
5253 // Load the map's "bit field 2" into |result|. We only need the first byte,
5254 // but the following masking takes care of that anyway.
5255 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
5256 // Retrieve elements_kind from bit field 2.
5257 __ DecodeField<Map::ElementsKindBits>(ecx);
5259 if (FLAG_debug_code) {
5261 __ cmp(ecx, Immediate(FAST_ELEMENTS));
5263 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
5265 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
5269 Label fast_elements_case;
5270 __ cmp(ecx, Immediate(FAST_ELEMENTS));
5271 __ j(equal, &fast_elements_case);
5272 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
5274 __ bind(&fast_elements_case);
5275 GenerateCase(masm, FAST_ELEMENTS);
5279 void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
5280 Register context_reg = esi;
5281 Register slot_reg = ebx;
5282 Register result_reg = eax;
5285 // Go up context chain to the script context.
5286 for (int i = 0; i < depth(); ++i) {
5287 __ mov(result_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5288 context_reg = result_reg;
5291 // Load the PropertyCell value at the specified slot.
5292 __ mov(result_reg, ContextOperand(context_reg, slot_reg));
5293 __ mov(result_reg, FieldOperand(result_reg, PropertyCell::kValueOffset));
5295 // Check that value is not the_hole.
5296 __ CompareRoot(result_reg, Heap::kTheHoleValueRootIndex);
5297 __ j(equal, &slow_case, Label::kNear);
5300 // Fallback to the runtime.
5301 __ bind(&slow_case);
5302 __ SmiTag(slot_reg);
5303 __ Pop(result_reg); // Pop return address.
5305 __ Push(result_reg); // Push return address.
5306 __ TailCallRuntime(Runtime::kLoadGlobalViaContext, 1, 1);
5310 void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
5311 Register context_reg = esi;
5312 Register slot_reg = ebx;
5313 Register value_reg = eax;
5314 Register cell_reg = edi;
5315 Register cell_details_reg = edx;
5316 Register cell_value_reg = ecx;
5317 Label fast_heapobject_case, fast_smi_case, slow_case;
5319 if (FLAG_debug_code) {
5320 __ CompareRoot(value_reg, Heap::kTheHoleValueRootIndex);
5321 __ Check(not_equal, kUnexpectedValue);
5324 // Go up context chain to the script context.
5325 for (int i = 0; i < depth(); ++i) {
5326 __ mov(cell_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5327 context_reg = cell_reg;
5330 // Load the PropertyCell at the specified slot.
5331 __ mov(cell_reg, ContextOperand(context_reg, slot_reg));
5333 // Load PropertyDetails for the cell (actually only the cell_type and kind).
5334 __ mov(cell_details_reg,
5335 FieldOperand(cell_reg, PropertyCell::kDetailsOffset));
5336 __ SmiUntag(cell_details_reg);
5337 __ and_(cell_details_reg,
5338 Immediate(PropertyDetails::PropertyCellTypeField::kMask |
5339 PropertyDetails::KindField::kMask |
5340 PropertyDetails::kAttributesReadOnlyMask));
5342 // Check if PropertyCell holds mutable data.
5343 Label not_mutable_data;
5344 __ cmp(cell_details_reg,
5345 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5346 PropertyCellType::kMutable) |
5347 PropertyDetails::KindField::encode(kData)));
5348 __ j(not_equal, ¬_mutable_data);
5349 __ JumpIfSmi(value_reg, &fast_smi_case);
5350 __ bind(&fast_heapobject_case);
5351 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5352 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
5353 cell_details_reg, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
5355 // RecordWriteField clobbers the value register, so we need to reload.
5356 __ mov(value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5358 __ bind(¬_mutable_data);
5360 // Check if PropertyCell value matches the new value (relevant for Constant,
5361 // ConstantType and Undefined cells).
5362 Label not_same_value;
5363 __ mov(cell_value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5364 __ cmp(cell_value_reg, value_reg);
5365 __ j(not_equal, ¬_same_value,
5366 FLAG_debug_code ? Label::kFar : Label::kNear);
5367 // Make sure the PropertyCell is not marked READ_ONLY.
5368 __ test(cell_details_reg,
5369 Immediate(PropertyDetails::kAttributesReadOnlyMask));
5370 __ j(not_zero, &slow_case);
5371 if (FLAG_debug_code) {
5373 // This can only be true for Constant, ConstantType and Undefined cells,
5374 // because we never store the_hole via this stub.
5375 __ cmp(cell_details_reg,
5376 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5377 PropertyCellType::kConstant) |
5378 PropertyDetails::KindField::encode(kData)));
5380 __ cmp(cell_details_reg,
5381 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5382 PropertyCellType::kConstantType) |
5383 PropertyDetails::KindField::encode(kData)));
5385 __ cmp(cell_details_reg,
5386 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5387 PropertyCellType::kUndefined) |
5388 PropertyDetails::KindField::encode(kData)));
5389 __ Check(equal, kUnexpectedValue);
5393 __ bind(¬_same_value);
5395 // Check if PropertyCell contains data with constant type (and is not
5397 __ cmp(cell_details_reg,
5398 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5399 PropertyCellType::kConstantType) |
5400 PropertyDetails::KindField::encode(kData)));
5401 __ j(not_equal, &slow_case, Label::kNear);
5403 // Now either both old and new values must be SMIs or both must be heap
5404 // objects with same map.
5405 Label value_is_heap_object;
5406 __ JumpIfNotSmi(value_reg, &value_is_heap_object, Label::kNear);
5407 __ JumpIfNotSmi(cell_value_reg, &slow_case, Label::kNear);
5408 // Old and new values are SMIs, no need for a write barrier here.
5409 __ bind(&fast_smi_case);
5410 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5412 __ bind(&value_is_heap_object);
5413 __ JumpIfSmi(cell_value_reg, &slow_case, Label::kNear);
5414 Register cell_value_map_reg = cell_value_reg;
5415 __ mov(cell_value_map_reg,
5416 FieldOperand(cell_value_reg, HeapObject::kMapOffset));
5417 __ cmp(cell_value_map_reg, FieldOperand(value_reg, HeapObject::kMapOffset));
5418 __ j(equal, &fast_heapobject_case);
5420 // Fallback to the runtime.
5421 __ bind(&slow_case);
5422 __ SmiTag(slot_reg);
5423 __ Pop(cell_reg); // Pop return address.
5426 __ Push(cell_reg); // Push return address.
5427 __ TailCallRuntime(is_strict(language_mode())
5428 ? Runtime::kStoreGlobalViaContext_Strict
5429 : Runtime::kStoreGlobalViaContext_Sloppy,
5434 // Generates an Operand for saving parameters after PrepareCallApiFunction.
5435 static Operand ApiParameterOperand(int index) {
5436 return Operand(esp, index * kPointerSize);
5440 // Prepares stack to put arguments (aligns and so on). Reserves
5441 // space for return value if needed (assumes the return value is a handle).
5442 // Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
5443 // etc. Saves context (esi). If space was reserved for return value then
5444 // stores the pointer to the reserved slot into esi.
5445 static void PrepareCallApiFunction(MacroAssembler* masm, int argc) {
5446 __ EnterApiExitFrame(argc);
5447 if (__ emit_debug_code()) {
5448 __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
5453 // Calls an API function. Allocates HandleScope, extracts returned value
5454 // from handle and propagates exceptions. Clobbers ebx, edi and
5455 // caller-save registers. Restores context. On return removes
5456 // stack_space * kPointerSize (GCed).
5457 static void CallApiFunctionAndReturn(MacroAssembler* masm,
5458 Register function_address,
5459 ExternalReference thunk_ref,
5460 Operand thunk_last_arg, int stack_space,
5461 Operand* stack_space_operand,
5462 Operand return_value_operand,
5463 Operand* context_restore_operand) {
5464 Isolate* isolate = masm->isolate();
5466 ExternalReference next_address =
5467 ExternalReference::handle_scope_next_address(isolate);
5468 ExternalReference limit_address =
5469 ExternalReference::handle_scope_limit_address(isolate);
5470 ExternalReference level_address =
5471 ExternalReference::handle_scope_level_address(isolate);
5473 DCHECK(edx.is(function_address));
5474 // Allocate HandleScope in callee-save registers.
5475 __ mov(ebx, Operand::StaticVariable(next_address));
5476 __ mov(edi, Operand::StaticVariable(limit_address));
5477 __ add(Operand::StaticVariable(level_address), Immediate(1));
5479 if (FLAG_log_timer_events) {
5480 FrameScope frame(masm, StackFrame::MANUAL);
5481 __ PushSafepointRegisters();
5482 __ PrepareCallCFunction(1, eax);
5483 __ mov(Operand(esp, 0),
5484 Immediate(ExternalReference::isolate_address(isolate)));
5485 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5487 __ PopSafepointRegisters();
5491 Label profiler_disabled;
5492 Label end_profiler_check;
5493 __ mov(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
5494 __ cmpb(Operand(eax, 0), 0);
5495 __ j(zero, &profiler_disabled);
5497 // Additional parameter is the address of the actual getter function.
5498 __ mov(thunk_last_arg, function_address);
5499 // Call the api function.
5500 __ mov(eax, Immediate(thunk_ref));
5502 __ jmp(&end_profiler_check);
5504 __ bind(&profiler_disabled);
5505 // Call the api function.
5506 __ call(function_address);
5507 __ bind(&end_profiler_check);
5509 if (FLAG_log_timer_events) {
5510 FrameScope frame(masm, StackFrame::MANUAL);
5511 __ PushSafepointRegisters();
5512 __ PrepareCallCFunction(1, eax);
5513 __ mov(Operand(esp, 0),
5514 Immediate(ExternalReference::isolate_address(isolate)));
5515 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5517 __ PopSafepointRegisters();
5521 // Load the value from ReturnValue
5522 __ mov(eax, return_value_operand);
5524 Label promote_scheduled_exception;
5525 Label delete_allocated_handles;
5526 Label leave_exit_frame;
5529 // No more valid handles (the result handle was the last one). Restore
5530 // previous handle scope.
5531 __ mov(Operand::StaticVariable(next_address), ebx);
5532 __ sub(Operand::StaticVariable(level_address), Immediate(1));
5533 __ Assert(above_equal, kInvalidHandleScopeLevel);
5534 __ cmp(edi, Operand::StaticVariable(limit_address));
5535 __ j(not_equal, &delete_allocated_handles);
5537 // Leave the API exit frame.
5538 __ bind(&leave_exit_frame);
5539 bool restore_context = context_restore_operand != NULL;
5540 if (restore_context) {
5541 __ mov(esi, *context_restore_operand);
5543 if (stack_space_operand != nullptr) {
5544 __ mov(ebx, *stack_space_operand);
5546 __ LeaveApiExitFrame(!restore_context);
5548 // Check if the function scheduled an exception.
5549 ExternalReference scheduled_exception_address =
5550 ExternalReference::scheduled_exception_address(isolate);
5551 __ cmp(Operand::StaticVariable(scheduled_exception_address),
5552 Immediate(isolate->factory()->the_hole_value()));
5553 __ j(not_equal, &promote_scheduled_exception);
5556 // Check if the function returned a valid JavaScript value.
5558 Register return_value = eax;
5561 __ JumpIfSmi(return_value, &ok, Label::kNear);
5562 __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
5564 __ CmpInstanceType(map, LAST_NAME_TYPE);
5565 __ j(below_equal, &ok, Label::kNear);
5567 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
5568 __ j(above_equal, &ok, Label::kNear);
5570 __ cmp(map, isolate->factory()->heap_number_map());
5571 __ j(equal, &ok, Label::kNear);
5573 __ cmp(return_value, isolate->factory()->undefined_value());
5574 __ j(equal, &ok, Label::kNear);
5576 __ cmp(return_value, isolate->factory()->true_value());
5577 __ j(equal, &ok, Label::kNear);
5579 __ cmp(return_value, isolate->factory()->false_value());
5580 __ j(equal, &ok, Label::kNear);
5582 __ cmp(return_value, isolate->factory()->null_value());
5583 __ j(equal, &ok, Label::kNear);
5585 __ Abort(kAPICallReturnedInvalidObject);
5590 if (stack_space_operand != nullptr) {
5591 DCHECK_EQ(0, stack_space);
5596 __ ret(stack_space * kPointerSize);
5599 // Re-throw by promoting a scheduled exception.
5600 __ bind(&promote_scheduled_exception);
5601 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
5603 // HandleScope limit has changed. Delete allocated extensions.
5604 ExternalReference delete_extensions =
5605 ExternalReference::delete_handle_scope_extensions(isolate);
5606 __ bind(&delete_allocated_handles);
5607 __ mov(Operand::StaticVariable(limit_address), edi);
5609 __ mov(Operand(esp, 0),
5610 Immediate(ExternalReference::isolate_address(isolate)));
5611 __ mov(eax, Immediate(delete_extensions));
5614 __ jmp(&leave_exit_frame);
5618 static void CallApiFunctionStubHelper(MacroAssembler* masm,
5619 const ParameterCount& argc,
5620 bool return_first_arg,
5621 bool call_data_undefined) {
5622 // ----------- S t a t e -------------
5624 // -- ebx : call_data
5626 // -- edx : api_function_address
5628 // -- eax : number of arguments if argc is a register
5630 // -- esp[0] : return address
5631 // -- esp[4] : last argument
5633 // -- esp[argc * 4] : first argument
5634 // -- esp[(argc + 1) * 4] : receiver
5635 // -----------------------------------
5637 Register callee = edi;
5638 Register call_data = ebx;
5639 Register holder = ecx;
5640 Register api_function_address = edx;
5641 Register context = esi;
5642 Register return_address = eax;
5644 typedef FunctionCallbackArguments FCA;
5646 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5647 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5648 STATIC_ASSERT(FCA::kDataIndex == 4);
5649 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5650 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5651 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5652 STATIC_ASSERT(FCA::kHolderIndex == 0);
5653 STATIC_ASSERT(FCA::kArgsLength == 7);
5655 DCHECK(argc.is_immediate() || eax.is(argc.reg()));
5657 if (argc.is_immediate()) {
5658 __ pop(return_address);
5662 // pop return address and save context
5663 __ xchg(context, Operand(esp, 0));
5664 return_address = context;
5673 Register scratch = call_data;
5674 if (!call_data_undefined) {
5676 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
5677 // return value default
5678 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
5682 // return value default
5686 __ push(Immediate(reinterpret_cast<int>(masm->isolate())));
5690 __ mov(scratch, esp);
5692 // push return address
5693 __ push(return_address);
5695 // load context from callee
5696 __ mov(context, FieldOperand(callee, JSFunction::kContextOffset));
5698 // API function gets reference to the v8::Arguments. If CPU profiler
5699 // is enabled wrapper function will be called and we need to pass
5700 // address of the callback as additional parameter, always allocate
5702 const int kApiArgc = 1 + 1;
5704 // Allocate the v8::Arguments structure in the arguments' space since
5705 // it's not controlled by GC.
5706 const int kApiStackSpace = 4;
5708 PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace);
5710 // FunctionCallbackInfo::implicit_args_.
5711 __ mov(ApiParameterOperand(2), scratch);
5712 if (argc.is_immediate()) {
5714 Immediate((argc.immediate() + FCA::kArgsLength - 1) * kPointerSize));
5715 // FunctionCallbackInfo::values_.
5716 __ mov(ApiParameterOperand(3), scratch);
5717 // FunctionCallbackInfo::length_.
5718 __ Move(ApiParameterOperand(4), Immediate(argc.immediate()));
5719 // FunctionCallbackInfo::is_construct_call_.
5720 __ Move(ApiParameterOperand(5), Immediate(0));
5722 __ lea(scratch, Operand(scratch, argc.reg(), times_pointer_size,
5723 (FCA::kArgsLength - 1) * kPointerSize));
5724 // FunctionCallbackInfo::values_.
5725 __ mov(ApiParameterOperand(3), scratch);
5726 // FunctionCallbackInfo::length_.
5727 __ mov(ApiParameterOperand(4), argc.reg());
5728 // FunctionCallbackInfo::is_construct_call_.
5729 __ lea(argc.reg(), Operand(argc.reg(), times_pointer_size,
5730 (FCA::kArgsLength + 1) * kPointerSize));
5731 __ mov(ApiParameterOperand(5), argc.reg());
5734 // v8::InvocationCallback's argument.
5735 __ lea(scratch, ApiParameterOperand(2));
5736 __ mov(ApiParameterOperand(0), scratch);
5738 ExternalReference thunk_ref =
5739 ExternalReference::invoke_function_callback(masm->isolate());
5741 Operand context_restore_operand(ebp,
5742 (2 + FCA::kContextSaveIndex) * kPointerSize);
5743 // Stores return the first js argument
5744 int return_value_offset = 0;
5745 if (return_first_arg) {
5746 return_value_offset = 2 + FCA::kArgsLength;
5748 return_value_offset = 2 + FCA::kReturnValueOffset;
5750 Operand return_value_operand(ebp, return_value_offset * kPointerSize);
5751 int stack_space = 0;
5752 Operand is_construct_call_operand = ApiParameterOperand(5);
5753 Operand* stack_space_operand = &is_construct_call_operand;
5754 if (argc.is_immediate()) {
5755 stack_space = argc.immediate() + FCA::kArgsLength + 1;
5756 stack_space_operand = nullptr;
5758 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5759 ApiParameterOperand(1), stack_space,
5760 stack_space_operand, return_value_operand,
5761 &context_restore_operand);
5765 void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5766 bool call_data_undefined = this->call_data_undefined();
5767 CallApiFunctionStubHelper(masm, ParameterCount(eax), false,
5768 call_data_undefined);
5772 void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5773 bool is_store = this->is_store();
5774 int argc = this->argc();
5775 bool call_data_undefined = this->call_data_undefined();
5776 CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
5777 call_data_undefined);
5781 void CallApiGetterStub::Generate(MacroAssembler* masm) {
5782 // ----------- S t a t e -------------
5783 // -- esp[0] : return address
5785 // -- esp[8 - kArgsLength*4] : PropertyCallbackArguments object
5787 // -- edx : api_function_address
5788 // -----------------------------------
5789 DCHECK(edx.is(ApiGetterDescriptor::function_address()));
5791 // array for v8::Arguments::values_, handler for name and pointer
5792 // to the values (it considered as smi in GC).
5793 const int kStackSpace = PropertyCallbackArguments::kArgsLength + 2;
5794 // Allocate space for opional callback address parameter in case
5795 // CPU profiler is active.
5796 const int kApiArgc = 2 + 1;
5798 Register api_function_address = edx;
5799 Register scratch = ebx;
5801 // load address of name
5802 __ lea(scratch, Operand(esp, 1 * kPointerSize));
5804 PrepareCallApiFunction(masm, kApiArgc);
5805 __ mov(ApiParameterOperand(0), scratch); // name.
5806 __ add(scratch, Immediate(kPointerSize));
5807 __ mov(ApiParameterOperand(1), scratch); // arguments pointer.
5809 ExternalReference thunk_ref =
5810 ExternalReference::invoke_accessor_getter_callback(isolate());
5812 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5813 ApiParameterOperand(2), kStackSpace, nullptr,
5814 Operand(ebp, 7 * kPointerSize), NULL);
5820 } // namespace internal
5823 #endif // V8_TARGET_ARCH_IA32