1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
9 #include "src/bootstrapper.h"
10 #include "src/code-stubs.h"
11 #include "src/codegen.h"
12 #include "src/ic/handler-compiler.h"
13 #include "src/ic/ic.h"
14 #include "src/ic/stub-cache.h"
15 #include "src/isolate.h"
16 #include "src/jsregexp.h"
17 #include "src/regexp-macro-assembler.h"
18 #include "src/runtime/runtime.h"
24 static void InitializeArrayConstructorDescriptor(
25 Isolate* isolate, CodeStubDescriptor* descriptor,
26 int constant_stack_parameter_count) {
27 Address deopt_handler = Runtime::FunctionForId(
28 Runtime::kArrayConstructor)->entry;
30 if (constant_stack_parameter_count == 0) {
31 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
32 JS_FUNCTION_STUB_MODE);
34 descriptor->Initialize(rax, deopt_handler, constant_stack_parameter_count,
35 JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
40 static void InitializeInternalArrayConstructorDescriptor(
41 Isolate* isolate, CodeStubDescriptor* descriptor,
42 int constant_stack_parameter_count) {
43 Address deopt_handler = Runtime::FunctionForId(
44 Runtime::kInternalArrayConstructor)->entry;
46 if (constant_stack_parameter_count == 0) {
47 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
48 JS_FUNCTION_STUB_MODE);
50 descriptor->Initialize(rax, deopt_handler, constant_stack_parameter_count,
51 JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
56 void ArrayNoArgumentConstructorStub::InitializeDescriptor(
57 CodeStubDescriptor* descriptor) {
58 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
62 void ArraySingleArgumentConstructorStub::InitializeDescriptor(
63 CodeStubDescriptor* descriptor) {
64 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
68 void ArrayNArgumentsConstructorStub::InitializeDescriptor(
69 CodeStubDescriptor* descriptor) {
70 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
74 void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
75 CodeStubDescriptor* descriptor) {
76 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
80 void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
81 CodeStubDescriptor* descriptor) {
82 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
86 void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
87 CodeStubDescriptor* descriptor) {
88 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
92 #define __ ACCESS_MASM(masm)
95 void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
96 ExternalReference miss) {
97 // Update the static counter each time a new code stub is generated.
98 isolate()->counters()->code_stubs()->Increment();
100 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
101 int param_count = descriptor.GetRegisterParameterCount();
103 // Call the runtime system in a fresh internal frame.
104 FrameScope scope(masm, StackFrame::INTERNAL);
105 DCHECK(param_count == 0 ||
106 rax.is(descriptor.GetRegisterParameter(param_count - 1)));
108 for (int i = 0; i < param_count; ++i) {
109 __ Push(descriptor.GetRegisterParameter(i));
111 __ CallExternalReference(miss, param_count);
118 void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
119 __ PushCallerSaved(save_doubles() ? kSaveFPRegs : kDontSaveFPRegs);
120 const int argument_count = 1;
121 __ PrepareCallCFunction(argument_count);
122 __ LoadAddress(arg_reg_1,
123 ExternalReference::isolate_address(isolate()));
125 AllowExternalCallThatCantCauseGC scope(masm);
127 ExternalReference::store_buffer_overflow_function(isolate()),
129 __ PopCallerSaved(save_doubles() ? kSaveFPRegs : kDontSaveFPRegs);
134 class FloatingPointHelper : public AllStatic {
136 enum ConvertUndefined {
137 CONVERT_UNDEFINED_TO_ZERO,
140 // Load the operands from rdx and rax into xmm0 and xmm1, as doubles.
141 // If the operands are not both numbers, jump to not_numbers.
142 // Leaves rdx and rax unchanged. SmiOperands assumes both are smis.
143 // NumberOperands assumes both are smis or heap numbers.
144 static void LoadSSE2UnknownOperands(MacroAssembler* masm,
149 void DoubleToIStub::Generate(MacroAssembler* masm) {
150 Register input_reg = this->source();
151 Register final_result_reg = this->destination();
152 DCHECK(is_truncating());
154 Label check_negative, process_64_bits, done;
156 int double_offset = offset();
158 // Account for return address and saved regs if input is rsp.
159 if (input_reg.is(rsp)) double_offset += 3 * kRegisterSize;
161 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
162 MemOperand exponent_operand(MemOperand(input_reg,
163 double_offset + kDoubleSize / 2));
166 Register scratch_candidates[3] = { rbx, rdx, rdi };
167 for (int i = 0; i < 3; i++) {
168 scratch1 = scratch_candidates[i];
169 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
172 // Since we must use rcx for shifts below, use some other register (rax)
173 // to calculate the result if ecx is the requested return register.
174 Register result_reg = final_result_reg.is(rcx) ? rax : final_result_reg;
175 // Save ecx if it isn't the return register and therefore volatile, or if it
176 // is the return register, then save the temp register we use in its stead
178 Register save_reg = final_result_reg.is(rcx) ? rax : rcx;
182 bool stash_exponent_copy = !input_reg.is(rsp);
183 __ movl(scratch1, mantissa_operand);
184 __ movsd(xmm0, mantissa_operand);
185 __ movl(rcx, exponent_operand);
186 if (stash_exponent_copy) __ pushq(rcx);
188 __ andl(rcx, Immediate(HeapNumber::kExponentMask));
189 __ shrl(rcx, Immediate(HeapNumber::kExponentShift));
190 __ leal(result_reg, MemOperand(rcx, -HeapNumber::kExponentBias));
191 __ cmpl(result_reg, Immediate(HeapNumber::kMantissaBits));
192 __ j(below, &process_64_bits);
194 // Result is entirely in lower 32-bits of mantissa
195 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
196 __ subl(rcx, Immediate(delta));
197 __ xorl(result_reg, result_reg);
198 __ cmpl(rcx, Immediate(31));
200 __ shll_cl(scratch1);
201 __ jmp(&check_negative);
203 __ bind(&process_64_bits);
204 __ cvttsd2siq(result_reg, xmm0);
205 __ jmp(&done, Label::kNear);
207 // If the double was negative, negate the integer result.
208 __ bind(&check_negative);
209 __ movl(result_reg, scratch1);
211 if (stash_exponent_copy) {
212 __ cmpl(MemOperand(rsp, 0), Immediate(0));
214 __ cmpl(exponent_operand, Immediate(0));
216 __ cmovl(greater, result_reg, scratch1);
220 if (stash_exponent_copy) {
221 __ addp(rsp, Immediate(kDoubleSize));
223 if (!final_result_reg.is(result_reg)) {
224 DCHECK(final_result_reg.is(rcx));
225 __ movl(final_result_reg, result_reg);
233 void FloatingPointHelper::LoadSSE2UnknownOperands(MacroAssembler* masm,
234 Label* not_numbers) {
235 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, load_float_rax, done;
236 // Load operand in rdx into xmm0, or branch to not_numbers.
237 __ LoadRoot(rcx, Heap::kHeapNumberMapRootIndex);
238 __ JumpIfSmi(rdx, &load_smi_rdx);
239 __ cmpp(FieldOperand(rdx, HeapObject::kMapOffset), rcx);
240 __ j(not_equal, not_numbers); // Argument in rdx is not a number.
241 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
242 // Load operand in rax into xmm1, or branch to not_numbers.
243 __ JumpIfSmi(rax, &load_smi_rax);
245 __ bind(&load_nonsmi_rax);
246 __ cmpp(FieldOperand(rax, HeapObject::kMapOffset), rcx);
247 __ j(not_equal, not_numbers);
248 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
251 __ bind(&load_smi_rdx);
252 __ SmiToInteger32(kScratchRegister, rdx);
253 __ Cvtlsi2sd(xmm0, kScratchRegister);
254 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
256 __ bind(&load_smi_rax);
257 __ SmiToInteger32(kScratchRegister, rax);
258 __ Cvtlsi2sd(xmm1, kScratchRegister);
263 void MathPowStub::Generate(MacroAssembler* masm) {
264 const Register exponent = MathPowTaggedDescriptor::exponent();
265 DCHECK(exponent.is(rdx));
266 const Register base = rax;
267 const Register scratch = rcx;
268 const XMMRegister double_result = xmm3;
269 const XMMRegister double_base = xmm2;
270 const XMMRegister double_exponent = xmm1;
271 const XMMRegister double_scratch = xmm4;
273 Label call_runtime, done, exponent_not_smi, int_exponent;
275 // Save 1 in double_result - we need this several times later on.
276 __ movp(scratch, Immediate(1));
277 __ Cvtlsi2sd(double_result, scratch);
279 if (exponent_type() == ON_STACK) {
280 Label base_is_smi, unpack_exponent;
281 // The exponent and base are supplied as arguments on the stack.
282 // This can only happen if the stub is called from non-optimized code.
283 // Load input parameters from stack.
284 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER);
285 __ movp(base, args.GetArgumentOperand(0));
286 __ movp(exponent, args.GetArgumentOperand(1));
287 __ JumpIfSmi(base, &base_is_smi, Label::kNear);
288 __ CompareRoot(FieldOperand(base, HeapObject::kMapOffset),
289 Heap::kHeapNumberMapRootIndex);
290 __ j(not_equal, &call_runtime);
292 __ movsd(double_base, FieldOperand(base, HeapNumber::kValueOffset));
293 __ jmp(&unpack_exponent, Label::kNear);
295 __ bind(&base_is_smi);
296 __ SmiToInteger32(base, base);
297 __ Cvtlsi2sd(double_base, base);
298 __ bind(&unpack_exponent);
300 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
301 __ SmiToInteger32(exponent, exponent);
302 __ jmp(&int_exponent);
304 __ bind(&exponent_not_smi);
305 __ CompareRoot(FieldOperand(exponent, HeapObject::kMapOffset),
306 Heap::kHeapNumberMapRootIndex);
307 __ j(not_equal, &call_runtime);
308 __ movsd(double_exponent, FieldOperand(exponent, HeapNumber::kValueOffset));
309 } else if (exponent_type() == TAGGED) {
310 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
311 __ SmiToInteger32(exponent, exponent);
312 __ jmp(&int_exponent);
314 __ bind(&exponent_not_smi);
315 __ movsd(double_exponent, FieldOperand(exponent, HeapNumber::kValueOffset));
318 if (exponent_type() != INTEGER) {
319 Label fast_power, try_arithmetic_simplification;
320 // Detect integer exponents stored as double.
321 __ DoubleToI(exponent, double_exponent, double_scratch,
322 TREAT_MINUS_ZERO_AS_ZERO, &try_arithmetic_simplification,
323 &try_arithmetic_simplification,
324 &try_arithmetic_simplification);
325 __ jmp(&int_exponent);
327 __ bind(&try_arithmetic_simplification);
328 __ cvttsd2si(exponent, double_exponent);
329 // Skip to runtime if possibly NaN (indicated by the indefinite integer).
330 __ cmpl(exponent, Immediate(0x1));
331 __ j(overflow, &call_runtime);
333 if (exponent_type() == ON_STACK) {
334 // Detect square root case. Crankshaft detects constant +/-0.5 at
335 // compile time and uses DoMathPowHalf instead. We then skip this check
336 // for non-constant cases of +/-0.5 as these hardly occur.
337 Label continue_sqrt, continue_rsqrt, not_plus_half;
339 // Load double_scratch with 0.5.
340 __ movq(scratch, V8_UINT64_C(0x3FE0000000000000));
341 __ movq(double_scratch, scratch);
342 // Already ruled out NaNs for exponent.
343 __ ucomisd(double_scratch, double_exponent);
344 __ j(not_equal, ¬_plus_half, Label::kNear);
346 // Calculates square root of base. Check for the special case of
347 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
348 // According to IEEE-754, double-precision -Infinity has the highest
349 // 12 bits set and the lowest 52 bits cleared.
350 __ movq(scratch, V8_UINT64_C(0xFFF0000000000000));
351 __ movq(double_scratch, scratch);
352 __ ucomisd(double_scratch, double_base);
353 // Comparing -Infinity with NaN results in "unordered", which sets the
354 // zero flag as if both were equal. However, it also sets the carry flag.
355 __ j(not_equal, &continue_sqrt, Label::kNear);
356 __ j(carry, &continue_sqrt, Label::kNear);
358 // Set result to Infinity in the special case.
359 __ xorps(double_result, double_result);
360 __ subsd(double_result, double_scratch);
363 __ bind(&continue_sqrt);
364 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
365 __ xorps(double_scratch, double_scratch);
366 __ addsd(double_scratch, double_base); // Convert -0 to 0.
367 __ sqrtsd(double_result, double_scratch);
371 __ bind(¬_plus_half);
372 // Load double_scratch with -0.5 by substracting 1.
373 __ subsd(double_scratch, double_result);
374 // Already ruled out NaNs for exponent.
375 __ ucomisd(double_scratch, double_exponent);
376 __ j(not_equal, &fast_power, Label::kNear);
378 // Calculates reciprocal of square root of base. Check for the special
379 // case of Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
380 // According to IEEE-754, double-precision -Infinity has the highest
381 // 12 bits set and the lowest 52 bits cleared.
382 __ movq(scratch, V8_UINT64_C(0xFFF0000000000000));
383 __ movq(double_scratch, scratch);
384 __ ucomisd(double_scratch, double_base);
385 // Comparing -Infinity with NaN results in "unordered", which sets the
386 // zero flag as if both were equal. However, it also sets the carry flag.
387 __ j(not_equal, &continue_rsqrt, Label::kNear);
388 __ j(carry, &continue_rsqrt, Label::kNear);
390 // Set result to 0 in the special case.
391 __ xorps(double_result, double_result);
394 __ bind(&continue_rsqrt);
395 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
396 __ xorps(double_exponent, double_exponent);
397 __ addsd(double_exponent, double_base); // Convert -0 to +0.
398 __ sqrtsd(double_exponent, double_exponent);
399 __ divsd(double_result, double_exponent);
403 // Using FPU instructions to calculate power.
404 Label fast_power_failed;
405 __ bind(&fast_power);
406 __ fnclex(); // Clear flags to catch exceptions later.
407 // Transfer (B)ase and (E)xponent onto the FPU register stack.
408 __ subp(rsp, Immediate(kDoubleSize));
409 __ movsd(Operand(rsp, 0), double_exponent);
410 __ fld_d(Operand(rsp, 0)); // E
411 __ movsd(Operand(rsp, 0), double_base);
412 __ fld_d(Operand(rsp, 0)); // B, E
414 // Exponent is in st(1) and base is in st(0)
415 // B ^ E = (2^(E * log2(B)) - 1) + 1 = (2^X - 1) + 1 for X = E * log2(B)
416 // FYL2X calculates st(1) * log2(st(0))
419 __ frndint(); // rnd(X), X
420 __ fsub(1); // rnd(X), X-rnd(X)
421 __ fxch(1); // X - rnd(X), rnd(X)
422 // F2XM1 calculates 2^st(0) - 1 for -1 < st(0) < 1
423 __ f2xm1(); // 2^(X-rnd(X)) - 1, rnd(X)
424 __ fld1(); // 1, 2^(X-rnd(X)) - 1, rnd(X)
425 __ faddp(1); // 2^(X-rnd(X)), rnd(X)
426 // FSCALE calculates st(0) * 2^st(1)
427 __ fscale(); // 2^X, rnd(X)
429 // Bail out to runtime in case of exceptions in the status word.
431 __ testb(rax, Immediate(0x5F)); // Check for all but precision exception.
432 __ j(not_zero, &fast_power_failed, Label::kNear);
433 __ fstp_d(Operand(rsp, 0));
434 __ movsd(double_result, Operand(rsp, 0));
435 __ addp(rsp, Immediate(kDoubleSize));
438 __ bind(&fast_power_failed);
440 __ addp(rsp, Immediate(kDoubleSize));
441 __ jmp(&call_runtime);
444 // Calculate power with integer exponent.
445 __ bind(&int_exponent);
446 const XMMRegister double_scratch2 = double_exponent;
447 // Back up exponent as we need to check if exponent is negative later.
448 __ movp(scratch, exponent); // Back up exponent.
449 __ movsd(double_scratch, double_base); // Back up base.
450 __ movsd(double_scratch2, double_result); // Load double_exponent with 1.
452 // Get absolute value of exponent.
453 Label no_neg, while_true, while_false;
454 __ testl(scratch, scratch);
455 __ j(positive, &no_neg, Label::kNear);
459 __ j(zero, &while_false, Label::kNear);
460 __ shrl(scratch, Immediate(1));
461 // Above condition means CF==0 && ZF==0. This means that the
462 // bit that has been shifted out is 0 and the result is not 0.
463 __ j(above, &while_true, Label::kNear);
464 __ movsd(double_result, double_scratch);
465 __ j(zero, &while_false, Label::kNear);
467 __ bind(&while_true);
468 __ shrl(scratch, Immediate(1));
469 __ mulsd(double_scratch, double_scratch);
470 __ j(above, &while_true, Label::kNear);
471 __ mulsd(double_result, double_scratch);
472 __ j(not_zero, &while_true);
474 __ bind(&while_false);
475 // If the exponent is negative, return 1/result.
476 __ testl(exponent, exponent);
477 __ j(greater, &done);
478 __ divsd(double_scratch2, double_result);
479 __ movsd(double_result, double_scratch2);
480 // Test whether result is zero. Bail out to check for subnormal result.
481 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
482 __ xorps(double_scratch2, double_scratch2);
483 __ ucomisd(double_scratch2, double_result);
484 // double_exponent aliased as double_scratch2 has already been overwritten
485 // and may not have contained the exponent value in the first place when the
486 // input was a smi. We reset it with exponent value before bailing out.
487 __ j(not_equal, &done);
488 __ Cvtlsi2sd(double_exponent, exponent);
490 // Returning or bailing out.
491 Counters* counters = isolate()->counters();
492 if (exponent_type() == ON_STACK) {
493 // The arguments are still on the stack.
494 __ bind(&call_runtime);
495 __ TailCallRuntime(Runtime::kMathPowRT, 2, 1);
497 // The stub is called from non-optimized code, which expects the result
498 // as heap number in rax.
500 __ AllocateHeapNumber(rax, rcx, &call_runtime);
501 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), double_result);
502 __ IncrementCounter(counters->math_pow(), 1);
503 __ ret(2 * kPointerSize);
505 __ bind(&call_runtime);
506 // Move base to the correct argument register. Exponent is already in xmm1.
507 __ movsd(xmm0, double_base);
508 DCHECK(double_exponent.is(xmm1));
510 AllowExternalCallThatCantCauseGC scope(masm);
511 __ PrepareCallCFunction(2);
513 ExternalReference::power_double_double_function(isolate()), 2);
515 // Return value is in xmm0.
516 __ movsd(double_result, xmm0);
519 __ IncrementCounter(counters->math_pow(), 1);
525 void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
527 Register receiver = LoadDescriptor::ReceiverRegister();
528 // Ensure that the vector and slot registers won't be clobbered before
529 // calling the miss handler.
530 DCHECK(!AreAliased(r8, r9, LoadWithVectorDescriptor::VectorRegister(),
531 LoadDescriptor::SlotRegister()));
533 NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(masm, receiver, r8,
536 PropertyAccessCompiler::TailCallBuiltin(
537 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
541 void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
542 // The key is in rdx and the parameter count is in rax.
543 DCHECK(rdx.is(ArgumentsAccessReadDescriptor::index()));
544 DCHECK(rax.is(ArgumentsAccessReadDescriptor::parameter_count()));
546 // Check that the key is a smi.
548 __ JumpIfNotSmi(rdx, &slow);
550 // Check if the calling frame is an arguments adaptor frame. We look at the
551 // context offset, and if the frame is not a regular one, then we find a
552 // Smi instead of the context. We can't use SmiCompare here, because that
553 // only works for comparing two smis.
555 __ movp(rbx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
556 __ Cmp(Operand(rbx, StandardFrameConstants::kContextOffset),
557 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
558 __ j(equal, &adaptor);
560 // Check index against formal parameters count limit passed in
561 // through register rax. Use unsigned comparison to get negative
564 __ j(above_equal, &slow);
566 // Read the argument from the stack and return it.
567 __ SmiSub(rax, rax, rdx);
568 __ SmiToInteger32(rax, rax);
569 StackArgumentsAccessor args(rbp, rax, ARGUMENTS_DONT_CONTAIN_RECEIVER);
570 __ movp(rax, args.GetArgumentOperand(0));
573 // Arguments adaptor case: Check index against actual arguments
574 // limit found in the arguments adaptor frame. Use unsigned
575 // comparison to get negative check for free.
577 __ movp(rcx, Operand(rbx, ArgumentsAdaptorFrameConstants::kLengthOffset));
579 __ j(above_equal, &slow);
581 // Read the argument from the stack and return it.
582 __ SmiSub(rcx, rcx, rdx);
583 __ SmiToInteger32(rcx, rcx);
584 StackArgumentsAccessor adaptor_args(rbx, rcx,
585 ARGUMENTS_DONT_CONTAIN_RECEIVER);
586 __ movp(rax, adaptor_args.GetArgumentOperand(0));
589 // Slow-case: Handle non-smi or out-of-bounds access to arguments
590 // by calling the runtime system.
592 __ PopReturnAddressTo(rbx);
594 __ PushReturnAddressFrom(rbx);
595 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
599 void ArgumentsAccessStub::GenerateNewSloppyFast(MacroAssembler* masm) {
601 // rsp[0] : return address
602 // rsp[8] : number of parameters (tagged)
603 // rsp[16] : receiver displacement
604 // rsp[24] : function
605 // Registers used over the whole function:
606 // rbx: the mapped parameter count (untagged)
607 // rax: the allocated object (tagged).
608 Factory* factory = isolate()->factory();
610 StackArgumentsAccessor args(rsp, 3, ARGUMENTS_DONT_CONTAIN_RECEIVER);
611 __ SmiToInteger64(rbx, args.GetArgumentOperand(2));
612 // rbx = parameter count (untagged)
614 // Check if the calling frame is an arguments adaptor frame.
616 Label adaptor_frame, try_allocate;
617 __ movp(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
618 __ movp(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
619 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
620 __ j(equal, &adaptor_frame);
622 // No adaptor, parameter count = argument count.
624 __ jmp(&try_allocate, Label::kNear);
626 // We have an adaptor frame. Patch the parameters pointer.
627 __ bind(&adaptor_frame);
628 __ SmiToInteger64(rcx,
630 ArgumentsAdaptorFrameConstants::kLengthOffset));
631 __ leap(rdx, Operand(rdx, rcx, times_pointer_size,
632 StandardFrameConstants::kCallerSPOffset));
633 __ movp(args.GetArgumentOperand(1), rdx);
635 // rbx = parameter count (untagged)
636 // rcx = argument count (untagged)
637 // Compute the mapped parameter count = min(rbx, rcx) in rbx.
639 __ j(less_equal, &try_allocate, Label::kNear);
642 __ bind(&try_allocate);
644 // Compute the sizes of backing store, parameter map, and arguments object.
645 // 1. Parameter map, has 2 extra words containing context and backing store.
646 const int kParameterMapHeaderSize =
647 FixedArray::kHeaderSize + 2 * kPointerSize;
648 Label no_parameter_map;
651 __ j(zero, &no_parameter_map, Label::kNear);
652 __ leap(r8, Operand(rbx, times_pointer_size, kParameterMapHeaderSize));
653 __ bind(&no_parameter_map);
656 __ leap(r8, Operand(r8, rcx, times_pointer_size, FixedArray::kHeaderSize));
658 // 3. Arguments object.
659 __ addp(r8, Immediate(Heap::kSloppyArgumentsObjectSize));
661 // Do the allocation of all three objects in one go.
662 __ Allocate(r8, rax, rdx, rdi, &runtime, TAG_OBJECT);
664 // rax = address of new object(s) (tagged)
665 // rcx = argument count (untagged)
666 // Get the arguments map from the current native context into rdi.
667 Label has_mapped_parameters, instantiate;
668 __ movp(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
669 __ movp(rdi, FieldOperand(rdi, GlobalObject::kNativeContextOffset));
671 __ j(not_zero, &has_mapped_parameters, Label::kNear);
673 const int kIndex = Context::SLOPPY_ARGUMENTS_MAP_INDEX;
674 __ movp(rdi, Operand(rdi, Context::SlotOffset(kIndex)));
675 __ jmp(&instantiate, Label::kNear);
677 const int kAliasedIndex = Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX;
678 __ bind(&has_mapped_parameters);
679 __ movp(rdi, Operand(rdi, Context::SlotOffset(kAliasedIndex)));
680 __ bind(&instantiate);
682 // rax = address of new object (tagged)
683 // rbx = mapped parameter count (untagged)
684 // rcx = argument count (untagged)
685 // rdi = address of arguments map (tagged)
686 __ movp(FieldOperand(rax, JSObject::kMapOffset), rdi);
687 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
688 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
689 __ movp(FieldOperand(rax, JSObject::kElementsOffset), kScratchRegister);
691 // Set up the callee in-object property.
692 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
693 __ movp(rdx, args.GetArgumentOperand(0));
694 __ AssertNotSmi(rdx);
695 __ movp(FieldOperand(rax, JSObject::kHeaderSize +
696 Heap::kArgumentsCalleeIndex * kPointerSize),
699 // Use the length (smi tagged) and set that as an in-object property too.
700 // Note: rcx is tagged from here on.
701 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
702 __ Integer32ToSmi(rcx, rcx);
703 __ movp(FieldOperand(rax, JSObject::kHeaderSize +
704 Heap::kArgumentsLengthIndex * kPointerSize),
707 // Set up the elements pointer in the allocated arguments object.
708 // If we allocated a parameter map, edi will point there, otherwise to the
710 __ leap(rdi, Operand(rax, Heap::kSloppyArgumentsObjectSize));
711 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rdi);
713 // rax = address of new object (tagged)
714 // rbx = mapped parameter count (untagged)
715 // rcx = argument count (tagged)
716 // rdi = address of parameter map or backing store (tagged)
718 // Initialize parameter map. If there are no mapped arguments, we're done.
719 Label skip_parameter_map;
721 __ j(zero, &skip_parameter_map);
723 __ LoadRoot(kScratchRegister, Heap::kSloppyArgumentsElementsMapRootIndex);
724 // rbx contains the untagged argument count. Add 2 and tag to write.
725 __ movp(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
726 __ Integer64PlusConstantToSmi(r9, rbx, 2);
727 __ movp(FieldOperand(rdi, FixedArray::kLengthOffset), r9);
728 __ movp(FieldOperand(rdi, FixedArray::kHeaderSize + 0 * kPointerSize), rsi);
729 __ leap(r9, Operand(rdi, rbx, times_pointer_size, kParameterMapHeaderSize));
730 __ movp(FieldOperand(rdi, FixedArray::kHeaderSize + 1 * kPointerSize), r9);
732 // Copy the parameter slots and the holes in the arguments.
733 // We need to fill in mapped_parameter_count slots. They index the context,
734 // where parameters are stored in reverse order, at
735 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
736 // The mapped parameter thus need to get indices
737 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
738 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
739 // We loop from right to left.
740 Label parameters_loop, parameters_test;
742 // Load tagged parameter count into r9.
743 __ Integer32ToSmi(r9, rbx);
744 __ Move(r8, Smi::FromInt(Context::MIN_CONTEXT_SLOTS));
745 __ addp(r8, args.GetArgumentOperand(2));
747 __ Move(r11, factory->the_hole_value());
749 __ leap(rdi, Operand(rdi, rbx, times_pointer_size, kParameterMapHeaderSize));
750 // r9 = loop variable (tagged)
751 // r8 = mapping index (tagged)
752 // r11 = the hole value
753 // rdx = address of parameter map (tagged)
754 // rdi = address of backing store (tagged)
755 __ jmp(¶meters_test, Label::kNear);
757 __ bind(¶meters_loop);
758 __ SmiSubConstant(r9, r9, Smi::FromInt(1));
759 __ SmiToInteger64(kScratchRegister, r9);
760 __ movp(FieldOperand(rdx, kScratchRegister,
762 kParameterMapHeaderSize),
764 __ movp(FieldOperand(rdi, kScratchRegister,
766 FixedArray::kHeaderSize),
768 __ SmiAddConstant(r8, r8, Smi::FromInt(1));
769 __ bind(¶meters_test);
771 __ j(not_zero, ¶meters_loop, Label::kNear);
773 __ bind(&skip_parameter_map);
775 // rcx = argument count (tagged)
776 // rdi = address of backing store (tagged)
777 // Copy arguments header and remaining slots (if there are any).
778 __ Move(FieldOperand(rdi, FixedArray::kMapOffset),
779 factory->fixed_array_map());
780 __ movp(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
782 Label arguments_loop, arguments_test;
784 __ movp(rdx, args.GetArgumentOperand(1));
785 // Untag rcx for the loop below.
786 __ SmiToInteger64(rcx, rcx);
787 __ leap(kScratchRegister, Operand(r8, times_pointer_size, 0));
788 __ subp(rdx, kScratchRegister);
789 __ jmp(&arguments_test, Label::kNear);
791 __ bind(&arguments_loop);
792 __ subp(rdx, Immediate(kPointerSize));
793 __ movp(r9, Operand(rdx, 0));
794 __ movp(FieldOperand(rdi, r8,
796 FixedArray::kHeaderSize),
798 __ addp(r8, Immediate(1));
800 __ bind(&arguments_test);
802 __ j(less, &arguments_loop, Label::kNear);
804 // Return and remove the on-stack parameters.
805 __ ret(3 * kPointerSize);
807 // Do the runtime call to allocate the arguments object.
808 // rcx = argument count (untagged)
810 __ Integer32ToSmi(rcx, rcx);
811 __ movp(args.GetArgumentOperand(2), rcx); // Patch argument count.
812 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
816 void ArgumentsAccessStub::GenerateNewSloppySlow(MacroAssembler* masm) {
817 // rsp[0] : return address
818 // rsp[8] : number of parameters
819 // rsp[16] : receiver displacement
820 // rsp[24] : function
822 // Check if the calling frame is an arguments adaptor frame.
824 __ movp(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
825 __ movp(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
826 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
827 __ j(not_equal, &runtime);
829 // Patch the arguments.length and the parameters pointer.
830 StackArgumentsAccessor args(rsp, 3, ARGUMENTS_DONT_CONTAIN_RECEIVER);
831 __ movp(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
832 __ movp(args.GetArgumentOperand(2), rcx);
833 __ SmiToInteger64(rcx, rcx);
834 __ leap(rdx, Operand(rdx, rcx, times_pointer_size,
835 StandardFrameConstants::kCallerSPOffset));
836 __ movp(args.GetArgumentOperand(1), rdx);
839 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
843 void RestParamAccessStub::GenerateNew(MacroAssembler* masm) {
844 // rsp[0] : return address
845 // rsp[8] : language mode
846 // rsp[16] : index of rest parameter
847 // rsp[24] : number of parameters
848 // rsp[32] : receiver displacement
850 // Check if the calling frame is an arguments adaptor frame.
852 __ movp(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
853 __ movp(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
854 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
855 __ j(not_equal, &runtime);
857 // Patch the arguments.length and the parameters pointer.
858 StackArgumentsAccessor args(rsp, 4, ARGUMENTS_DONT_CONTAIN_RECEIVER);
859 __ movp(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
860 __ movp(args.GetArgumentOperand(1), rcx);
861 __ SmiToInteger64(rcx, rcx);
862 __ leap(rdx, Operand(rdx, rcx, times_pointer_size,
863 StandardFrameConstants::kCallerSPOffset));
864 __ movp(args.GetArgumentOperand(0), rdx);
867 __ TailCallRuntime(Runtime::kNewRestParam, 4, 1);
871 void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
872 // Return address is on the stack.
875 Register receiver = LoadDescriptor::ReceiverRegister();
876 Register key = LoadDescriptor::NameRegister();
877 Register scratch = rax;
878 DCHECK(!scratch.is(receiver) && !scratch.is(key));
880 // Check that the key is an array index, that is Uint32.
881 STATIC_ASSERT(kSmiValueSize <= 32);
882 __ JumpUnlessNonNegativeSmi(key, &slow);
884 // Everything is fine, call runtime.
885 __ PopReturnAddressTo(scratch);
886 __ Push(receiver); // receiver
888 __ PushReturnAddressFrom(scratch);
890 // Perform tail call to the entry.
891 __ TailCallExternalReference(
892 ExternalReference(IC_Utility(IC::kLoadElementWithInterceptor),
897 PropertyAccessCompiler::TailCallBuiltin(
898 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
902 void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
903 // Return address is on the stack.
906 Register receiver = LoadDescriptor::ReceiverRegister();
907 Register index = LoadDescriptor::NameRegister();
908 Register scratch = rdi;
909 Register result = rax;
910 DCHECK(!scratch.is(receiver) && !scratch.is(index));
911 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
912 result.is(LoadDescriptor::SlotRegister()));
914 // StringCharAtGenerator doesn't use the result register until it's passed
915 // the different miss possibilities. If it did, we would have a conflict
916 // when FLAG_vector_ics is true.
917 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
918 &miss, // When not a string.
919 &miss, // When not a number.
920 &miss, // When index out of range.
921 STRING_INDEX_IS_ARRAY_INDEX,
923 char_at_generator.GenerateFast(masm);
926 StubRuntimeCallHelper call_helper;
927 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
930 PropertyAccessCompiler::TailCallBuiltin(
931 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
935 void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
936 // rsp[0] : return address
937 // rsp[8] : number of parameters
938 // rsp[16] : receiver displacement
939 // rsp[24] : function
941 // Check if the calling frame is an arguments adaptor frame.
942 Label adaptor_frame, try_allocate, runtime;
943 __ movp(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
944 __ movp(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
945 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
946 __ j(equal, &adaptor_frame);
948 // Get the length from the frame.
949 StackArgumentsAccessor args(rsp, 3, ARGUMENTS_DONT_CONTAIN_RECEIVER);
950 __ movp(rcx, args.GetArgumentOperand(2));
951 __ SmiToInteger64(rcx, rcx);
952 __ jmp(&try_allocate);
954 // Patch the arguments.length and the parameters pointer.
955 __ bind(&adaptor_frame);
956 __ movp(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
958 __ movp(args.GetArgumentOperand(2), rcx);
959 __ SmiToInteger64(rcx, rcx);
960 __ leap(rdx, Operand(rdx, rcx, times_pointer_size,
961 StandardFrameConstants::kCallerSPOffset));
962 __ movp(args.GetArgumentOperand(1), rdx);
964 // Try the new space allocation. Start out with computing the size of
965 // the arguments object and the elements array.
966 Label add_arguments_object;
967 __ bind(&try_allocate);
969 __ j(zero, &add_arguments_object, Label::kNear);
970 __ leap(rcx, Operand(rcx, times_pointer_size, FixedArray::kHeaderSize));
971 __ bind(&add_arguments_object);
972 __ addp(rcx, Immediate(Heap::kStrictArgumentsObjectSize));
974 // Do the allocation of both objects in one go.
975 __ Allocate(rcx, rax, rdx, rbx, &runtime, TAG_OBJECT);
977 // Get the arguments map from the current native context.
978 __ movp(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
979 __ movp(rdi, FieldOperand(rdi, GlobalObject::kNativeContextOffset));
980 const int offset = Context::SlotOffset(Context::STRICT_ARGUMENTS_MAP_INDEX);
981 __ movp(rdi, Operand(rdi, offset));
983 __ movp(FieldOperand(rax, JSObject::kMapOffset), rdi);
984 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
985 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
986 __ movp(FieldOperand(rax, JSObject::kElementsOffset), kScratchRegister);
988 // Get the length (smi tagged) and set that as an in-object property too.
989 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
990 __ movp(rcx, args.GetArgumentOperand(2));
991 __ movp(FieldOperand(rax, JSObject::kHeaderSize +
992 Heap::kArgumentsLengthIndex * kPointerSize),
995 // If there are no actual arguments, we're done.
1000 // Get the parameters pointer from the stack.
1001 __ movp(rdx, args.GetArgumentOperand(1));
1003 // Set up the elements pointer in the allocated arguments object and
1004 // initialize the header in the elements fixed array.
1005 __ leap(rdi, Operand(rax, Heap::kStrictArgumentsObjectSize));
1006 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rdi);
1007 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
1008 __ movp(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
1011 __ movp(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
1012 // Untag the length for the loop below.
1013 __ SmiToInteger64(rcx, rcx);
1015 // Copy the fixed array slots.
1018 __ movp(rbx, Operand(rdx, -1 * kPointerSize)); // Skip receiver.
1019 __ movp(FieldOperand(rdi, FixedArray::kHeaderSize), rbx);
1020 __ addp(rdi, Immediate(kPointerSize));
1021 __ subp(rdx, Immediate(kPointerSize));
1023 __ j(not_zero, &loop);
1025 // Return and remove the on-stack parameters.
1027 __ ret(3 * kPointerSize);
1029 // Do the runtime call to allocate the arguments object.
1031 __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
1035 void RegExpExecStub::Generate(MacroAssembler* masm) {
1036 // Just jump directly to runtime if native RegExp is not selected at compile
1037 // time or if regexp entry in generated code is turned off runtime switch or
1039 #ifdef V8_INTERPRETED_REGEXP
1040 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1041 #else // V8_INTERPRETED_REGEXP
1043 // Stack frame on entry.
1044 // rsp[0] : return address
1045 // rsp[8] : last_match_info (expected JSArray)
1046 // rsp[16] : previous index
1047 // rsp[24] : subject string
1048 // rsp[32] : JSRegExp object
1050 enum RegExpExecStubArgumentIndices {
1051 JS_REG_EXP_OBJECT_ARGUMENT_INDEX,
1052 SUBJECT_STRING_ARGUMENT_INDEX,
1053 PREVIOUS_INDEX_ARGUMENT_INDEX,
1054 LAST_MATCH_INFO_ARGUMENT_INDEX,
1055 REG_EXP_EXEC_ARGUMENT_COUNT
1058 StackArgumentsAccessor args(rsp, REG_EXP_EXEC_ARGUMENT_COUNT,
1059 ARGUMENTS_DONT_CONTAIN_RECEIVER);
1061 // Ensure that a RegExp stack is allocated.
1062 ExternalReference address_of_regexp_stack_memory_address =
1063 ExternalReference::address_of_regexp_stack_memory_address(isolate());
1064 ExternalReference address_of_regexp_stack_memory_size =
1065 ExternalReference::address_of_regexp_stack_memory_size(isolate());
1066 __ Load(kScratchRegister, address_of_regexp_stack_memory_size);
1067 __ testp(kScratchRegister, kScratchRegister);
1068 __ j(zero, &runtime);
1070 // Check that the first argument is a JSRegExp object.
1071 __ movp(rax, args.GetArgumentOperand(JS_REG_EXP_OBJECT_ARGUMENT_INDEX));
1072 __ JumpIfSmi(rax, &runtime);
1073 __ CmpObjectType(rax, JS_REGEXP_TYPE, kScratchRegister);
1074 __ j(not_equal, &runtime);
1076 // Check that the RegExp has been compiled (data contains a fixed array).
1077 __ movp(rax, FieldOperand(rax, JSRegExp::kDataOffset));
1078 if (FLAG_debug_code) {
1079 Condition is_smi = masm->CheckSmi(rax);
1080 __ Check(NegateCondition(is_smi),
1081 kUnexpectedTypeForRegExpDataFixedArrayExpected);
1082 __ CmpObjectType(rax, FIXED_ARRAY_TYPE, kScratchRegister);
1083 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
1086 // rax: RegExp data (FixedArray)
1087 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1088 __ SmiToInteger32(rbx, FieldOperand(rax, JSRegExp::kDataTagOffset));
1089 __ cmpl(rbx, Immediate(JSRegExp::IRREGEXP));
1090 __ j(not_equal, &runtime);
1092 // rax: RegExp data (FixedArray)
1093 // Check that the number of captures fit in the static offsets vector buffer.
1094 __ SmiToInteger32(rdx,
1095 FieldOperand(rax, JSRegExp::kIrregexpCaptureCountOffset));
1096 // Check (number_of_captures + 1) * 2 <= offsets vector size
1097 // Or number_of_captures <= offsets vector size / 2 - 1
1098 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1099 __ cmpl(rdx, Immediate(Isolate::kJSRegexpStaticOffsetsVectorSize / 2 - 1));
1100 __ j(above, &runtime);
1102 // Reset offset for possibly sliced string.
1104 __ movp(rdi, args.GetArgumentOperand(SUBJECT_STRING_ARGUMENT_INDEX));
1105 __ JumpIfSmi(rdi, &runtime);
1106 __ movp(r15, rdi); // Make a copy of the original subject string.
1107 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
1108 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1109 // rax: RegExp data (FixedArray)
1110 // rdi: subject string
1111 // r15: subject string
1112 // Handle subject string according to its encoding and representation:
1113 // (1) Sequential two byte? If yes, go to (9).
1114 // (2) Sequential one byte? If yes, go to (6).
1115 // (3) Anything but sequential or cons? If yes, go to (7).
1116 // (4) Cons string. If the string is flat, replace subject with first string.
1117 // Otherwise bailout.
1118 // (5a) Is subject sequential two byte? If yes, go to (9).
1119 // (5b) Is subject external? If yes, go to (8).
1120 // (6) One byte sequential. Load regexp code for one byte.
1124 // Deferred code at the end of the stub:
1125 // (7) Not a long external string? If yes, go to (10).
1126 // (8) External string. Make it, offset-wise, look like a sequential string.
1127 // (8a) Is the external string one byte? If yes, go to (6).
1128 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1129 // (10) Short external string or not a string? If yes, bail out to runtime.
1130 // (11) Sliced string. Replace subject with parent. Go to (5a).
1132 Label seq_one_byte_string /* 6 */, seq_two_byte_string /* 9 */,
1133 external_string /* 8 */, check_underlying /* 5a */,
1134 not_seq_nor_cons /* 7 */, check_code /* E */,
1135 not_long_external /* 10 */;
1137 // (1) Sequential two byte? If yes, go to (9).
1138 __ andb(rbx, Immediate(kIsNotStringMask |
1139 kStringRepresentationMask |
1140 kStringEncodingMask |
1141 kShortExternalStringMask));
1142 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
1143 __ j(zero, &seq_two_byte_string); // Go to (9).
1145 // (2) Sequential one byte? If yes, go to (6).
1146 // Any other sequential string must be one byte.
1147 __ andb(rbx, Immediate(kIsNotStringMask |
1148 kStringRepresentationMask |
1149 kShortExternalStringMask));
1150 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (6).
1152 // (3) Anything but sequential or cons? If yes, go to (7).
1153 // We check whether the subject string is a cons, since sequential strings
1154 // have already been covered.
1155 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1156 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
1157 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1158 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
1159 __ cmpp(rbx, Immediate(kExternalStringTag));
1160 __ j(greater_equal, ¬_seq_nor_cons); // Go to (7).
1162 // (4) Cons string. Check that it's flat.
1163 // Replace subject with first string and reload instance type.
1164 __ CompareRoot(FieldOperand(rdi, ConsString::kSecondOffset),
1165 Heap::kempty_stringRootIndex);
1166 __ j(not_equal, &runtime);
1167 __ movp(rdi, FieldOperand(rdi, ConsString::kFirstOffset));
1168 __ bind(&check_underlying);
1169 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
1170 __ movp(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1172 // (5a) Is subject sequential two byte? If yes, go to (9).
1173 __ testb(rbx, Immediate(kStringRepresentationMask | kStringEncodingMask));
1174 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
1175 __ j(zero, &seq_two_byte_string); // Go to (9).
1176 // (5b) Is subject external? If yes, go to (8).
1177 __ testb(rbx, Immediate(kStringRepresentationMask));
1178 // The underlying external string is never a short external string.
1179 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
1180 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
1181 __ j(not_zero, &external_string); // Go to (8)
1183 // (6) One byte sequential. Load regexp code for one byte.
1184 __ bind(&seq_one_byte_string);
1185 // rax: RegExp data (FixedArray)
1186 __ movp(r11, FieldOperand(rax, JSRegExp::kDataOneByteCodeOffset));
1187 __ Set(rcx, 1); // Type is one byte.
1189 // (E) Carry on. String handling is done.
1190 __ bind(&check_code);
1191 // r11: irregexp code
1192 // Check that the irregexp code has been generated for the actual string
1193 // encoding. If it has, the field contains a code object otherwise it contains
1194 // smi (code flushing support)
1195 __ JumpIfSmi(r11, &runtime);
1197 // rdi: sequential subject string (or look-alike, external string)
1198 // r15: original subject string
1199 // rcx: encoding of subject string (1 if one_byte, 0 if two_byte);
1201 // Load used arguments before starting to push arguments for call to native
1202 // RegExp code to avoid handling changing stack height.
1203 // We have to use r15 instead of rdi to load the length because rdi might
1204 // have been only made to look like a sequential string when it actually
1205 // is an external string.
1206 __ movp(rbx, args.GetArgumentOperand(PREVIOUS_INDEX_ARGUMENT_INDEX));
1207 __ JumpIfNotSmi(rbx, &runtime);
1208 __ SmiCompare(rbx, FieldOperand(r15, String::kLengthOffset));
1209 __ j(above_equal, &runtime);
1210 __ SmiToInteger64(rbx, rbx);
1212 // rdi: subject string
1213 // rbx: previous index
1214 // rcx: encoding of subject string (1 if one_byte 0 if two_byte);
1216 // All checks done. Now push arguments for native regexp code.
1217 Counters* counters = isolate()->counters();
1218 __ IncrementCounter(counters->regexp_entry_native(), 1);
1220 // Isolates: note we add an additional parameter here (isolate pointer).
1221 static const int kRegExpExecuteArguments = 9;
1222 int argument_slots_on_stack =
1223 masm->ArgumentStackSlotsForCFunctionCall(kRegExpExecuteArguments);
1224 __ EnterApiExitFrame(argument_slots_on_stack);
1226 // Argument 9: Pass current isolate address.
1227 __ LoadAddress(kScratchRegister,
1228 ExternalReference::isolate_address(isolate()));
1229 __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kRegisterSize),
1232 // Argument 8: Indicate that this is a direct call from JavaScript.
1233 __ movq(Operand(rsp, (argument_slots_on_stack - 2) * kRegisterSize),
1236 // Argument 7: Start (high end) of backtracking stack memory area.
1237 __ Move(kScratchRegister, address_of_regexp_stack_memory_address);
1238 __ movp(r9, Operand(kScratchRegister, 0));
1239 __ Move(kScratchRegister, address_of_regexp_stack_memory_size);
1240 __ addp(r9, Operand(kScratchRegister, 0));
1241 __ movq(Operand(rsp, (argument_slots_on_stack - 3) * kRegisterSize), r9);
1243 // Argument 6: Set the number of capture registers to zero to force global
1244 // regexps to behave as non-global. This does not affect non-global regexps.
1245 // Argument 6 is passed in r9 on Linux and on the stack on Windows.
1247 __ movq(Operand(rsp, (argument_slots_on_stack - 4) * kRegisterSize),
1253 // Argument 5: static offsets vector buffer.
1255 r8, ExternalReference::address_of_static_offsets_vector(isolate()));
1256 // Argument 5 passed in r8 on Linux and on the stack on Windows.
1258 __ movq(Operand(rsp, (argument_slots_on_stack - 5) * kRegisterSize), r8);
1261 // rdi: subject string
1262 // rbx: previous index
1263 // rcx: encoding of subject string (1 if one_byte 0 if two_byte);
1265 // r14: slice offset
1266 // r15: original subject string
1268 // Argument 2: Previous index.
1269 __ movp(arg_reg_2, rbx);
1271 // Argument 4: End of string data
1272 // Argument 3: Start of string data
1273 Label setup_two_byte, setup_rest, got_length, length_not_from_slice;
1274 // Prepare start and end index of the input.
1275 // Load the length from the original sliced string if that is the case.
1277 __ SmiToInteger32(arg_reg_3, FieldOperand(r15, String::kLengthOffset));
1278 __ addp(r14, arg_reg_3); // Using arg3 as scratch.
1280 // rbx: start index of the input
1281 // r14: end index of the input
1282 // r15: original subject string
1283 __ testb(rcx, rcx); // Last use of rcx as encoding of subject string.
1284 __ j(zero, &setup_two_byte, Label::kNear);
1286 FieldOperand(rdi, r14, times_1, SeqOneByteString::kHeaderSize));
1288 FieldOperand(rdi, rbx, times_1, SeqOneByteString::kHeaderSize));
1289 __ jmp(&setup_rest, Label::kNear);
1290 __ bind(&setup_two_byte);
1292 FieldOperand(rdi, r14, times_2, SeqTwoByteString::kHeaderSize));
1294 FieldOperand(rdi, rbx, times_2, SeqTwoByteString::kHeaderSize));
1295 __ bind(&setup_rest);
1297 // Argument 1: Original subject string.
1298 // The original subject is in the previous stack frame. Therefore we have to
1299 // use rbp, which points exactly to one pointer size below the previous rsp.
1300 // (Because creating a new stack frame pushes the previous rbp onto the stack
1301 // and thereby moves up rsp by one kPointerSize.)
1302 __ movp(arg_reg_1, r15);
1304 // Locate the code entry and call it.
1305 __ addp(r11, Immediate(Code::kHeaderSize - kHeapObjectTag));
1308 __ LeaveApiExitFrame(true);
1310 // Check the result.
1313 __ cmpl(rax, Immediate(1));
1314 // We expect exactly one result since we force the called regexp to behave
1316 __ j(equal, &success, Label::kNear);
1317 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::EXCEPTION));
1318 __ j(equal, &exception);
1319 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::FAILURE));
1320 // If none of the above, it can only be retry.
1321 // Handle that in the runtime system.
1322 __ j(not_equal, &runtime);
1324 // For failure return null.
1325 __ LoadRoot(rax, Heap::kNullValueRootIndex);
1326 __ ret(REG_EXP_EXEC_ARGUMENT_COUNT * kPointerSize);
1328 // Load RegExp data.
1330 __ movp(rax, args.GetArgumentOperand(JS_REG_EXP_OBJECT_ARGUMENT_INDEX));
1331 __ movp(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
1332 __ SmiToInteger32(rax,
1333 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
1334 // Calculate number of capture registers (number_of_captures + 1) * 2.
1335 __ leal(rdx, Operand(rax, rax, times_1, 2));
1337 // rdx: Number of capture registers
1338 // Check that the fourth object is a JSArray object.
1339 __ movp(r15, args.GetArgumentOperand(LAST_MATCH_INFO_ARGUMENT_INDEX));
1340 __ JumpIfSmi(r15, &runtime);
1341 __ CmpObjectType(r15, JS_ARRAY_TYPE, kScratchRegister);
1342 __ j(not_equal, &runtime);
1343 // Check that the JSArray is in fast case.
1344 __ movp(rbx, FieldOperand(r15, JSArray::kElementsOffset));
1345 __ movp(rax, FieldOperand(rbx, HeapObject::kMapOffset));
1346 __ CompareRoot(rax, Heap::kFixedArrayMapRootIndex);
1347 __ j(not_equal, &runtime);
1348 // Check that the last match info has space for the capture registers and the
1349 // additional information. Ensure no overflow in add.
1350 STATIC_ASSERT(FixedArray::kMaxLength < kMaxInt - FixedArray::kLengthOffset);
1351 __ SmiToInteger32(rax, FieldOperand(rbx, FixedArray::kLengthOffset));
1352 __ subl(rax, Immediate(RegExpImpl::kLastMatchOverhead));
1354 __ j(greater, &runtime);
1356 // rbx: last_match_info backing store (FixedArray)
1357 // rdx: number of capture registers
1358 // Store the capture count.
1359 __ Integer32ToSmi(kScratchRegister, rdx);
1360 __ movp(FieldOperand(rbx, RegExpImpl::kLastCaptureCountOffset),
1362 // Store last subject and last input.
1363 __ movp(rax, args.GetArgumentOperand(SUBJECT_STRING_ARGUMENT_INDEX));
1364 __ movp(FieldOperand(rbx, RegExpImpl::kLastSubjectOffset), rax);
1366 __ RecordWriteField(rbx,
1367 RegExpImpl::kLastSubjectOffset,
1372 __ movp(FieldOperand(rbx, RegExpImpl::kLastInputOffset), rax);
1373 __ RecordWriteField(rbx,
1374 RegExpImpl::kLastInputOffset,
1379 // Get the static offsets vector filled by the native regexp code.
1381 rcx, ExternalReference::address_of_static_offsets_vector(isolate()));
1383 // rbx: last_match_info backing store (FixedArray)
1384 // rcx: offsets vector
1385 // rdx: number of capture registers
1386 Label next_capture, done;
1387 // Capture register counter starts from number of capture registers and
1388 // counts down until wraping after zero.
1389 __ bind(&next_capture);
1390 __ subp(rdx, Immediate(1));
1391 __ j(negative, &done, Label::kNear);
1392 // Read the value from the static offsets vector buffer and make it a smi.
1393 __ movl(rdi, Operand(rcx, rdx, times_int_size, 0));
1394 __ Integer32ToSmi(rdi, rdi);
1395 // Store the smi value in the last match info.
1396 __ movp(FieldOperand(rbx,
1399 RegExpImpl::kFirstCaptureOffset),
1401 __ jmp(&next_capture);
1404 // Return last match info.
1406 __ ret(REG_EXP_EXEC_ARGUMENT_COUNT * kPointerSize);
1408 __ bind(&exception);
1409 // Result must now be exception. If there is no pending exception already a
1410 // stack overflow (on the backtrack stack) was detected in RegExp code but
1411 // haven't created the exception yet. Handle that in the runtime system.
1412 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
1413 ExternalReference pending_exception_address(
1414 Isolate::kPendingExceptionAddress, isolate());
1415 Operand pending_exception_operand =
1416 masm->ExternalOperand(pending_exception_address, rbx);
1417 __ movp(rax, pending_exception_operand);
1418 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
1420 __ j(equal, &runtime);
1422 // For exception, throw the exception again.
1423 __ TailCallRuntime(Runtime::kRegExpExecReThrow, 4, 1);
1425 // Do the runtime call to execute the regexp.
1427 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1429 // Deferred code for string handling.
1430 // (7) Not a long external string? If yes, go to (10).
1431 __ bind(¬_seq_nor_cons);
1432 // Compare flags are still set from (3).
1433 __ j(greater, ¬_long_external, Label::kNear); // Go to (10).
1435 // (8) External string. Short external strings have been ruled out.
1436 __ bind(&external_string);
1437 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
1438 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1439 if (FLAG_debug_code) {
1440 // Assert that we do not have a cons or slice (indirect strings) here.
1441 // Sequential strings have already been ruled out.
1442 __ testb(rbx, Immediate(kIsIndirectStringMask));
1443 __ Assert(zero, kExternalStringExpectedButNotFound);
1445 __ movp(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
1446 // Move the pointer so that offset-wise, it looks like a sequential string.
1447 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
1448 __ subp(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
1449 STATIC_ASSERT(kTwoByteStringTag == 0);
1450 // (8a) Is the external string one byte? If yes, go to (6).
1451 __ testb(rbx, Immediate(kStringEncodingMask));
1452 __ j(not_zero, &seq_one_byte_string); // Goto (6).
1454 // rdi: subject string (flat two-byte)
1455 // rax: RegExp data (FixedArray)
1456 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1457 __ bind(&seq_two_byte_string);
1458 __ movp(r11, FieldOperand(rax, JSRegExp::kDataUC16CodeOffset));
1459 __ Set(rcx, 0); // Type is two byte.
1460 __ jmp(&check_code); // Go to (E).
1462 // (10) Not a string or a short external string? If yes, bail out to runtime.
1463 __ bind(¬_long_external);
1464 // Catch non-string subject or short external string.
1465 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1466 __ testb(rbx, Immediate(kIsNotStringMask | kShortExternalStringMask));
1467 __ j(not_zero, &runtime);
1469 // (11) Sliced string. Replace subject with parent. Go to (5a).
1470 // Load offset into r14 and replace subject string with parent.
1471 __ SmiToInteger32(r14, FieldOperand(rdi, SlicedString::kOffsetOffset));
1472 __ movp(rdi, FieldOperand(rdi, SlicedString::kParentOffset));
1473 __ jmp(&check_underlying);
1474 #endif // V8_INTERPRETED_REGEXP
1478 static int NegativeComparisonResult(Condition cc) {
1479 DCHECK(cc != equal);
1480 DCHECK((cc == less) || (cc == less_equal)
1481 || (cc == greater) || (cc == greater_equal));
1482 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
1486 static void CheckInputType(MacroAssembler* masm, Register input,
1487 CompareICState::State expected, Label* fail) {
1489 if (expected == CompareICState::SMI) {
1490 __ JumpIfNotSmi(input, fail);
1491 } else if (expected == CompareICState::NUMBER) {
1492 __ JumpIfSmi(input, &ok);
1493 __ CompareMap(input, masm->isolate()->factory()->heap_number_map());
1494 __ j(not_equal, fail);
1496 // We could be strict about internalized/non-internalized here, but as long as
1497 // hydrogen doesn't care, the stub doesn't have to care either.
1502 static void BranchIfNotInternalizedString(MacroAssembler* masm,
1506 __ JumpIfSmi(object, label);
1507 __ movp(scratch, FieldOperand(object, HeapObject::kMapOffset));
1509 FieldOperand(scratch, Map::kInstanceTypeOffset));
1510 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
1511 __ testb(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
1512 __ j(not_zero, label);
1516 void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
1517 Label runtime_call, check_unequal_objects, done;
1518 Condition cc = GetCondition();
1519 Factory* factory = isolate()->factory();
1522 CheckInputType(masm, rdx, left(), &miss);
1523 CheckInputType(masm, rax, right(), &miss);
1525 // Compare two smis.
1526 Label non_smi, smi_done;
1527 __ JumpIfNotBothSmi(rax, rdx, &non_smi);
1529 __ j(no_overflow, &smi_done);
1530 __ notp(rdx); // Correct sign in case of overflow. rdx cannot be 0 here.
1536 // The compare stub returns a positive, negative, or zero 64-bit integer
1537 // value in rax, corresponding to result of comparing the two inputs.
1538 // NOTICE! This code is only reached after a smi-fast-case check, so
1539 // it is certain that at least one operand isn't a smi.
1541 // Two identical objects are equal unless they are both NaN or undefined.
1543 Label not_identical;
1545 __ j(not_equal, ¬_identical, Label::kNear);
1548 // Check for undefined. undefined OP undefined is false even though
1549 // undefined == undefined.
1550 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
1551 if (is_strong(strength())) {
1552 // In strong mode, this comparison must throw, so call the runtime.
1553 __ j(equal, &runtime_call, Label::kFar);
1555 Label check_for_nan;
1556 __ j(not_equal, &check_for_nan, Label::kNear);
1557 __ Set(rax, NegativeComparisonResult(cc));
1559 __ bind(&check_for_nan);
1563 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
1564 // so we do the second best thing - test it ourselves.
1566 // If it's not a heap number, then return equal for (in)equality operator.
1567 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset),
1568 factory->heap_number_map());
1569 __ j(equal, &heap_number, Label::kNear);
1571 __ movp(rcx, FieldOperand(rax, HeapObject::kMapOffset));
1572 __ movzxbl(rcx, FieldOperand(rcx, Map::kInstanceTypeOffset));
1573 // Call runtime on identical objects. Otherwise return equal.
1574 __ cmpb(rcx, Immediate(static_cast<uint8_t>(FIRST_SPEC_OBJECT_TYPE)));
1575 __ j(above_equal, &runtime_call, Label::kFar);
1576 // Call runtime on identical symbols since we need to throw a TypeError.
1577 __ cmpb(rcx, Immediate(static_cast<uint8_t>(SYMBOL_TYPE)));
1578 __ j(equal, &runtime_call, Label::kFar);
1579 // Call runtime on identical SIMD values since we must throw a TypeError.
1580 __ cmpb(rcx, Immediate(static_cast<uint8_t>(FLOAT32X4_TYPE)));
1581 __ j(equal, &runtime_call, Label::kFar);
1582 if (is_strong(strength())) {
1583 // We have already tested for smis and heap numbers, so if both
1584 // arguments are not strings we must proceed to the slow case.
1585 __ testb(rcx, Immediate(kIsNotStringMask));
1586 __ j(not_zero, &runtime_call, Label::kFar);
1592 __ bind(&heap_number);
1593 // It is a heap number, so return equal if it's not NaN.
1594 // For NaN, return 1 for every condition except greater and
1595 // greater-equal. Return -1 for them, so the comparison yields
1596 // false for all conditions except not-equal.
1598 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1599 __ ucomisd(xmm0, xmm0);
1600 __ setcc(parity_even, rax);
1601 // rax is 0 for equal non-NaN heapnumbers, 1 for NaNs.
1602 if (cc == greater_equal || cc == greater) {
1607 __ bind(¬_identical);
1610 if (cc == equal) { // Both strict and non-strict.
1611 Label slow; // Fallthrough label.
1613 // If we're doing a strict equality comparison, we don't have to do
1614 // type conversion, so we generate code to do fast comparison for objects
1615 // and oddballs. Non-smi numbers and strings still go through the usual
1618 // If either is a Smi (we know that not both are), then they can only
1619 // be equal if the other is a HeapNumber. If so, use the slow case.
1622 __ SelectNonSmi(rbx, rax, rdx, ¬_smis);
1624 // Check if the non-smi operand is a heap number.
1625 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
1626 factory->heap_number_map());
1627 // If heap number, handle it in the slow case.
1629 // Return non-equal. ebx (the lower half of rbx) is not zero.
1636 // If either operand is a JSObject or an oddball value, then they are not
1637 // equal since their pointers are different
1638 // There is no test for undetectability in strict equality.
1640 // If the first object is a JS object, we have done pointer comparison.
1641 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
1642 Label first_non_object;
1643 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
1644 __ j(below, &first_non_object, Label::kNear);
1645 // Return non-zero (rax (not rax) is not zero)
1646 Label return_not_equal;
1647 STATIC_ASSERT(kHeapObjectTag != 0);
1648 __ bind(&return_not_equal);
1651 __ bind(&first_non_object);
1652 // Check for oddballs: true, false, null, undefined.
1653 __ CmpInstanceType(rcx, ODDBALL_TYPE);
1654 __ j(equal, &return_not_equal);
1656 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
1657 __ j(above_equal, &return_not_equal);
1659 // Check for oddballs: true, false, null, undefined.
1660 __ CmpInstanceType(rcx, ODDBALL_TYPE);
1661 __ j(equal, &return_not_equal);
1663 // Fall through to the general case.
1668 // Generate the number comparison code.
1669 Label non_number_comparison;
1671 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &non_number_comparison);
1674 __ ucomisd(xmm0, xmm1);
1676 // Don't base result on EFLAGS when a NaN is involved.
1677 __ j(parity_even, &unordered, Label::kNear);
1678 // Return a result of -1, 0, or 1, based on EFLAGS.
1679 __ setcc(above, rax);
1680 __ setcc(below, rcx);
1684 // If one of the numbers was NaN, then the result is always false.
1685 // The cc is never not-equal.
1686 __ bind(&unordered);
1687 DCHECK(cc != not_equal);
1688 if (cc == less || cc == less_equal) {
1695 // The number comparison code did not provide a valid result.
1696 __ bind(&non_number_comparison);
1698 // Fast negative check for internalized-to-internalized equality.
1699 Label check_for_strings;
1701 BranchIfNotInternalizedString(
1702 masm, &check_for_strings, rax, kScratchRegister);
1703 BranchIfNotInternalizedString(
1704 masm, &check_for_strings, rdx, kScratchRegister);
1706 // We've already checked for object identity, so if both operands are
1707 // internalized strings they aren't equal. Register rax (not rax) already
1708 // holds a non-zero value, which indicates not equal, so just return.
1712 __ bind(&check_for_strings);
1714 __ JumpIfNotBothSequentialOneByteStrings(rdx, rax, rcx, rbx,
1715 &check_unequal_objects);
1717 // Inline comparison of one-byte strings.
1719 StringHelper::GenerateFlatOneByteStringEquals(masm, rdx, rax, rcx, rbx);
1721 StringHelper::GenerateCompareFlatOneByteStrings(masm, rdx, rax, rcx, rbx,
1726 __ Abort(kUnexpectedFallThroughFromStringComparison);
1729 __ bind(&check_unequal_objects);
1730 if (cc == equal && !strict()) {
1731 // Not strict equality. Objects are unequal if
1732 // they are both JSObjects and not undetectable,
1733 // and their pointers are different.
1734 Label return_unequal;
1735 // At most one is a smi, so we can test for smi by adding the two.
1736 // A smi plus a heap object has the low bit set, a heap object plus
1737 // a heap object has the low bit clear.
1738 STATIC_ASSERT(kSmiTag == 0);
1739 STATIC_ASSERT(kSmiTagMask == 1);
1740 __ leap(rcx, Operand(rax, rdx, times_1, 0));
1741 __ testb(rcx, Immediate(kSmiTagMask));
1742 __ j(not_zero, &runtime_call, Label::kNear);
1743 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rbx);
1744 __ j(below, &runtime_call, Label::kNear);
1745 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
1746 __ j(below, &runtime_call, Label::kNear);
1747 __ testb(FieldOperand(rbx, Map::kBitFieldOffset),
1748 Immediate(1 << Map::kIsUndetectable));
1749 __ j(zero, &return_unequal, Label::kNear);
1750 __ testb(FieldOperand(rcx, Map::kBitFieldOffset),
1751 Immediate(1 << Map::kIsUndetectable));
1752 __ j(zero, &return_unequal, Label::kNear);
1753 // The objects are both undetectable, so they both compare as the value
1754 // undefined, and are equal.
1756 __ bind(&return_unequal);
1757 // Return non-equal by returning the non-zero object pointer in rax,
1758 // or return equal if we fell through to here.
1761 __ bind(&runtime_call);
1763 // Push arguments below the return address to prepare jump to builtin.
1764 __ PopReturnAddressTo(rcx);
1768 // Figure out which native to call and setup the arguments.
1769 Builtins::JavaScript builtin;
1771 builtin = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1774 is_strong(strength()) ? Builtins::COMPARE_STRONG : Builtins::COMPARE;
1775 __ Push(Smi::FromInt(NegativeComparisonResult(cc)));
1778 __ PushReturnAddressFrom(rcx);
1780 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1781 // tagged as a small integer.
1782 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
1789 static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
1790 // eax : number of arguments to the construct function
1791 // ebx : Feedback vector
1792 // edx : slot in feedback vector (Smi)
1793 // edi : the function to call
1794 FrameScope scope(masm, StackFrame::INTERNAL);
1796 // Number-of-arguments register must be smi-tagged to call out.
1797 __ Integer32ToSmi(rax, rax);
1800 __ Integer32ToSmi(rdx, rdx);
1810 __ SmiToInteger32(rax, rax);
1814 static void GenerateRecordCallTarget(MacroAssembler* masm) {
1815 // Cache the called function in a feedback vector slot. Cache states
1816 // are uninitialized, monomorphic (indicated by a JSFunction), and
1818 // rax : number of arguments to the construct function
1819 // rbx : Feedback vector
1820 // rdx : slot in feedback vector (Smi)
1821 // rdi : the function to call
1822 Isolate* isolate = masm->isolate();
1823 Label initialize, done, miss, megamorphic, not_array_function,
1824 done_no_smi_convert;
1826 // Load the cache state into rcx.
1827 __ SmiToInteger32(rdx, rdx);
1828 __ movp(rcx, FieldOperand(rbx, rdx, times_pointer_size,
1829 FixedArray::kHeaderSize));
1831 // A monomorphic cache hit or an already megamorphic state: invoke the
1832 // function without changing the state.
1833 // We don't know if rcx is a WeakCell or a Symbol, but it's harmless to read
1834 // at this position in a symbol (see static asserts in
1835 // type-feedback-vector.h).
1836 Label check_allocation_site;
1837 __ cmpp(rdi, FieldOperand(rcx, WeakCell::kValueOffset));
1838 __ j(equal, &done, Label::kFar);
1839 __ CompareRoot(rcx, Heap::kmegamorphic_symbolRootIndex);
1840 __ j(equal, &done, Label::kFar);
1841 __ CompareRoot(FieldOperand(rcx, HeapObject::kMapOffset),
1842 Heap::kWeakCellMapRootIndex);
1843 __ j(not_equal, FLAG_pretenuring_call_new ? &miss : &check_allocation_site);
1845 // If the weak cell is cleared, we have a new chance to become monomorphic.
1846 __ CheckSmi(FieldOperand(rcx, WeakCell::kValueOffset));
1847 __ j(equal, &initialize);
1848 __ jmp(&megamorphic);
1850 if (!FLAG_pretenuring_call_new) {
1851 __ bind(&check_allocation_site);
1852 // If we came here, we need to see if we are the array function.
1853 // If we didn't have a matching function, and we didn't find the megamorph
1854 // sentinel, then we have in the slot either some other function or an
1856 __ CompareRoot(FieldOperand(rcx, 0), Heap::kAllocationSiteMapRootIndex);
1857 __ j(not_equal, &miss);
1859 // Make sure the function is the Array() function
1860 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, rcx);
1862 __ j(not_equal, &megamorphic);
1868 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1870 __ CompareRoot(rcx, Heap::kuninitialized_symbolRootIndex);
1871 __ j(equal, &initialize);
1872 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1873 // write-barrier is needed.
1874 __ bind(&megamorphic);
1875 __ Move(FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize),
1876 TypeFeedbackVector::MegamorphicSentinel(isolate));
1879 // An uninitialized cache is patched with the function or sentinel to
1880 // indicate the ElementsKind if function is the Array constructor.
1881 __ bind(&initialize);
1883 if (!FLAG_pretenuring_call_new) {
1884 // Make sure the function is the Array() function
1885 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, rcx);
1887 __ j(not_equal, ¬_array_function);
1889 CreateAllocationSiteStub create_stub(isolate);
1890 CallStubInRecordCallTarget(masm, &create_stub);
1891 __ jmp(&done_no_smi_convert);
1893 __ bind(¬_array_function);
1896 CreateWeakCellStub create_stub(isolate);
1897 CallStubInRecordCallTarget(masm, &create_stub);
1898 __ jmp(&done_no_smi_convert);
1901 __ Integer32ToSmi(rdx, rdx);
1903 __ bind(&done_no_smi_convert);
1907 static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
1908 // Do not transform the receiver for strict mode functions.
1909 __ movp(rcx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1910 __ testb(FieldOperand(rcx, SharedFunctionInfo::kStrictModeByteOffset),
1911 Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
1912 __ j(not_equal, cont);
1914 // Do not transform the receiver for natives.
1915 // SharedFunctionInfo is already loaded into rcx.
1916 __ testb(FieldOperand(rcx, SharedFunctionInfo::kNativeByteOffset),
1917 Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
1918 __ j(not_equal, cont);
1922 static void EmitSlowCase(Isolate* isolate,
1923 MacroAssembler* masm,
1924 StackArgumentsAccessor* args,
1926 Label* non_function) {
1927 // Check for function proxy.
1928 __ CmpInstanceType(rcx, JS_FUNCTION_PROXY_TYPE);
1929 __ j(not_equal, non_function);
1930 __ PopReturnAddressTo(rcx);
1931 __ Push(rdi); // put proxy as additional argument under return address
1932 __ PushReturnAddressFrom(rcx);
1933 __ Set(rax, argc + 1);
1935 __ GetBuiltinEntry(rdx, Builtins::CALL_FUNCTION_PROXY);
1937 Handle<Code> adaptor =
1938 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
1939 __ jmp(adaptor, RelocInfo::CODE_TARGET);
1942 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
1943 // of the original receiver from the call site).
1944 __ bind(non_function);
1945 __ movp(args->GetReceiverOperand(), rdi);
1948 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
1949 Handle<Code> adaptor =
1950 isolate->builtins()->ArgumentsAdaptorTrampoline();
1951 __ Jump(adaptor, RelocInfo::CODE_TARGET);
1955 static void EmitWrapCase(MacroAssembler* masm,
1956 StackArgumentsAccessor* args,
1958 // Wrap the receiver and patch it back onto the stack.
1959 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
1962 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1965 __ movp(args->GetReceiverOperand(), rax);
1970 static void CallFunctionNoFeedback(MacroAssembler* masm,
1971 int argc, bool needs_checks,
1972 bool call_as_method) {
1973 // rdi : the function to call
1975 // wrap_and_call can only be true if we are compiling a monomorphic method.
1976 Isolate* isolate = masm->isolate();
1977 Label slow, non_function, wrap, cont;
1978 StackArgumentsAccessor args(rsp, argc);
1981 // Check that the function really is a JavaScript function.
1982 __ JumpIfSmi(rdi, &non_function);
1984 // Goto slow case if we do not have a function.
1985 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
1986 __ j(not_equal, &slow);
1989 // Fast-case: Just invoke the function.
1990 ParameterCount actual(argc);
1992 if (call_as_method) {
1994 EmitContinueIfStrictOrNative(masm, &cont);
1997 // Load the receiver from the stack.
1998 __ movp(rax, args.GetReceiverOperand());
2001 __ JumpIfSmi(rax, &wrap);
2003 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
2012 __ InvokeFunction(rdi, actual, JUMP_FUNCTION, NullCallWrapper());
2015 // Slow-case: Non-function called.
2017 EmitSlowCase(isolate, masm, &args, argc, &non_function);
2020 if (call_as_method) {
2022 EmitWrapCase(masm, &args, &cont);
2027 void CallFunctionStub::Generate(MacroAssembler* masm) {
2028 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
2032 void CallConstructStub::Generate(MacroAssembler* masm) {
2033 // rax : number of arguments
2034 // rbx : feedback vector
2035 // rcx : original constructor (for IsSuperConstructorCall)
2036 // rdx : slot in feedback vector (Smi, for RecordCallTarget)
2037 // rdi : constructor function
2038 Label slow, non_function_call;
2040 // Check that function is not a smi.
2041 __ JumpIfSmi(rdi, &non_function_call);
2042 // Check that function is a JSFunction.
2043 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, r11);
2044 __ j(not_equal, &slow);
2046 if (RecordCallTarget()) {
2047 if (IsSuperConstructorCall()) {
2050 GenerateRecordCallTarget(masm);
2051 if (IsSuperConstructorCall()) {
2055 __ SmiToInteger32(rdx, rdx);
2056 if (FLAG_pretenuring_call_new) {
2057 // Put the AllocationSite from the feedback vector into ebx.
2058 // By adding kPointerSize we encode that we know the AllocationSite
2059 // entry is at the feedback vector slot given by rdx + 1.
2060 __ movp(rbx, FieldOperand(rbx, rdx, times_pointer_size,
2061 FixedArray::kHeaderSize + kPointerSize));
2063 Label feedback_register_initialized;
2064 // Put the AllocationSite from the feedback vector into rbx, or undefined.
2065 __ movp(rbx, FieldOperand(rbx, rdx, times_pointer_size,
2066 FixedArray::kHeaderSize));
2067 __ CompareRoot(FieldOperand(rbx, 0), Heap::kAllocationSiteMapRootIndex);
2068 __ j(equal, &feedback_register_initialized);
2069 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
2070 __ bind(&feedback_register_initialized);
2073 __ AssertUndefinedOrAllocationSite(rbx);
2076 // Pass original constructor to construct stub.
2077 if (IsSuperConstructorCall()) {
2083 // Jump to the function-specific construct stub.
2084 Register jmp_reg = rcx;
2085 __ movp(jmp_reg, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
2086 __ movp(jmp_reg, FieldOperand(jmp_reg,
2087 SharedFunctionInfo::kConstructStubOffset));
2088 __ leap(jmp_reg, FieldOperand(jmp_reg, Code::kHeaderSize));
2091 // rdi: called object
2092 // rax: number of arguments
2096 __ CmpInstanceType(r11, JS_FUNCTION_PROXY_TYPE);
2097 __ j(not_equal, &non_function_call);
2098 __ GetBuiltinEntry(rdx, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
2101 __ bind(&non_function_call);
2102 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
2104 // Set expected number of arguments to zero (not changing rax).
2106 __ Jump(isolate()->builtins()->ArgumentsAdaptorTrampoline(),
2107 RelocInfo::CODE_TARGET);
2111 static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
2112 __ movp(vector, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
2113 __ movp(vector, FieldOperand(vector, JSFunction::kSharedFunctionInfoOffset));
2114 __ movp(vector, FieldOperand(vector,
2115 SharedFunctionInfo::kFeedbackVectorOffset));
2119 void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
2121 // rdx - slot id (as integer)
2124 int argc = arg_count();
2125 ParameterCount actual(argc);
2127 __ SmiToInteger32(rdx, rdx);
2129 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, rcx);
2131 __ j(not_equal, &miss);
2133 __ movp(rax, Immediate(arg_count()));
2134 __ movp(rcx, FieldOperand(rbx, rdx, times_pointer_size,
2135 FixedArray::kHeaderSize));
2136 // Verify that ecx contains an AllocationSite
2137 Factory* factory = masm->isolate()->factory();
2138 __ Cmp(FieldOperand(rcx, HeapObject::kMapOffset),
2139 factory->allocation_site_map());
2140 __ j(not_equal, &miss);
2142 // Increment the call count for monomorphic function calls.
2143 __ SmiAddConstant(FieldOperand(rbx, rdx, times_pointer_size,
2144 FixedArray::kHeaderSize + kPointerSize),
2145 Smi::FromInt(CallICNexus::kCallCountIncrement));
2149 ArrayConstructorStub stub(masm->isolate(), arg_count());
2150 __ TailCallStub(&stub);
2155 // The slow case, we need this no matter what to complete a call after a miss.
2156 CallFunctionNoFeedback(masm,
2166 void CallICStub::Generate(MacroAssembler* masm) {
2170 Isolate* isolate = masm->isolate();
2171 const int with_types_offset =
2172 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kWithTypesIndex);
2173 const int generic_offset =
2174 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kGenericCountIndex);
2175 Label extra_checks_or_miss, slow_start;
2176 Label slow, non_function, wrap, cont;
2177 Label have_js_function;
2178 int argc = arg_count();
2179 StackArgumentsAccessor args(rsp, argc);
2180 ParameterCount actual(argc);
2182 // The checks. First, does rdi match the recorded monomorphic target?
2183 __ SmiToInteger32(rdx, rdx);
2185 FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize));
2187 // We don't know that we have a weak cell. We might have a private symbol
2188 // or an AllocationSite, but the memory is safe to examine.
2189 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
2191 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
2192 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
2193 // computed, meaning that it can't appear to be a pointer. If the low bit is
2194 // 0, then hash is computed, but the 0 bit prevents the field from appearing
2196 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
2197 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
2198 WeakCell::kValueOffset &&
2199 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
2201 __ cmpp(rdi, FieldOperand(rcx, WeakCell::kValueOffset));
2202 __ j(not_equal, &extra_checks_or_miss);
2204 // The compare above could have been a SMI/SMI comparison. Guard against this
2205 // convincing us that we have a monomorphic JSFunction.
2206 __ JumpIfSmi(rdi, &extra_checks_or_miss);
2208 // Increment the call count for monomorphic function calls.
2209 __ SmiAddConstant(FieldOperand(rbx, rdx, times_pointer_size,
2210 FixedArray::kHeaderSize + kPointerSize),
2211 Smi::FromInt(CallICNexus::kCallCountIncrement));
2213 __ bind(&have_js_function);
2214 if (CallAsMethod()) {
2215 EmitContinueIfStrictOrNative(masm, &cont);
2217 // Load the receiver from the stack.
2218 __ movp(rax, args.GetReceiverOperand());
2220 __ JumpIfSmi(rax, &wrap);
2222 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
2228 __ InvokeFunction(rdi, actual, JUMP_FUNCTION, NullCallWrapper());
2231 EmitSlowCase(isolate, masm, &args, argc, &non_function);
2233 if (CallAsMethod()) {
2235 EmitWrapCase(masm, &args, &cont);
2238 __ bind(&extra_checks_or_miss);
2239 Label uninitialized, miss;
2241 __ Cmp(rcx, TypeFeedbackVector::MegamorphicSentinel(isolate));
2242 __ j(equal, &slow_start);
2244 // The following cases attempt to handle MISS cases without going to the
2246 if (FLAG_trace_ic) {
2250 __ Cmp(rcx, TypeFeedbackVector::UninitializedSentinel(isolate));
2251 __ j(equal, &uninitialized);
2253 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2254 // to handle it here. More complex cases are dealt with in the runtime.
2255 __ AssertNotSmi(rcx);
2256 __ CmpObjectType(rcx, JS_FUNCTION_TYPE, rcx);
2257 __ j(not_equal, &miss);
2258 __ Move(FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize),
2259 TypeFeedbackVector::MegamorphicSentinel(isolate));
2260 // We have to update statistics for runtime profiling.
2261 __ SmiAddConstant(FieldOperand(rbx, with_types_offset), Smi::FromInt(-1));
2262 __ SmiAddConstant(FieldOperand(rbx, generic_offset), Smi::FromInt(1));
2263 __ jmp(&slow_start);
2265 __ bind(&uninitialized);
2267 // We are going monomorphic, provided we actually have a JSFunction.
2268 __ JumpIfSmi(rdi, &miss);
2270 // Goto miss case if we do not have a function.
2271 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
2272 __ j(not_equal, &miss);
2274 // Make sure the function is not the Array() function, which requires special
2275 // behavior on MISS.
2276 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, rcx);
2281 __ SmiAddConstant(FieldOperand(rbx, with_types_offset), Smi::FromInt(1));
2283 // Initialize the call counter.
2284 __ Move(FieldOperand(rbx, rdx, times_pointer_size,
2285 FixedArray::kHeaderSize + kPointerSize),
2286 Smi::FromInt(CallICNexus::kCallCountIncrement));
2288 // Store the function. Use a stub since we need a frame for allocation.
2290 // rdx - slot (needs to be in smi form)
2293 FrameScope scope(masm, StackFrame::INTERNAL);
2294 CreateWeakCellStub create_stub(isolate);
2296 __ Integer32ToSmi(rdx, rdx);
2298 __ CallStub(&create_stub);
2302 __ jmp(&have_js_function);
2304 // We are here because tracing is on or we encountered a MISS case we can't
2310 __ bind(&slow_start);
2311 // Check that function is not a smi.
2312 __ JumpIfSmi(rdi, &non_function);
2313 // Check that function is a JSFunction.
2314 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
2315 __ j(not_equal, &slow);
2316 __ jmp(&have_js_function);
2323 void CallICStub::GenerateMiss(MacroAssembler* masm) {
2324 FrameScope scope(masm, StackFrame::INTERNAL);
2326 // Push the receiver and the function and feedback info.
2329 __ Integer32ToSmi(rdx, rdx);
2333 IC::UtilityId id = GetICState() == DEFAULT ? IC::kCallIC_Miss
2334 : IC::kCallIC_Customization_Miss;
2336 ExternalReference miss = ExternalReference(IC_Utility(id), masm->isolate());
2337 __ CallExternalReference(miss, 3);
2339 // Move result to edi and exit the internal frame.
2344 bool CEntryStub::NeedsImmovableCode() {
2349 void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
2350 CEntryStub::GenerateAheadOfTime(isolate);
2351 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
2352 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
2353 // It is important that the store buffer overflow stubs are generated first.
2354 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
2355 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
2356 CreateWeakCellStub::GenerateAheadOfTime(isolate);
2357 BinaryOpICStub::GenerateAheadOfTime(isolate);
2358 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
2359 StoreFastElementStub::GenerateAheadOfTime(isolate);
2360 TypeofStub::GenerateAheadOfTime(isolate);
2364 void CodeStub::GenerateFPStubs(Isolate* isolate) {
2368 void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
2369 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
2371 CEntryStub save_doubles(isolate, 1, kSaveFPRegs);
2372 save_doubles.GetCode();
2376 void CEntryStub::Generate(MacroAssembler* masm) {
2377 // rax: number of arguments including receiver
2378 // rbx: pointer to C function (C callee-saved)
2379 // rbp: frame pointer of calling JS frame (restored after C call)
2380 // rsp: stack pointer (restored after C call)
2381 // rsi: current context (restored)
2383 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2385 // Enter the exit frame that transitions from JavaScript to C++.
2387 int arg_stack_space = (result_size() < 2 ? 2 : 4);
2389 int arg_stack_space = 0;
2391 __ EnterExitFrame(arg_stack_space, save_doubles());
2393 // rbx: pointer to builtin function (C callee-saved).
2394 // rbp: frame pointer of exit frame (restored after C call).
2395 // rsp: stack pointer (restored after C call).
2396 // r14: number of arguments including receiver (C callee-saved).
2397 // r15: argv pointer (C callee-saved).
2399 // Simple results returned in rax (both AMD64 and Win64 calling conventions).
2400 // Complex results must be written to address passed as first argument.
2401 // AMD64 calling convention: a struct of two pointers in rax+rdx
2403 // Check stack alignment.
2404 if (FLAG_debug_code) {
2405 __ CheckStackAlignment();
2410 // Windows 64-bit ABI passes arguments in rcx, rdx, r8, r9.
2411 // Pass argv and argc as two parameters. The arguments object will
2412 // be created by stubs declared by DECLARE_RUNTIME_FUNCTION().
2413 if (result_size() < 2) {
2414 // Pass a pointer to the Arguments object as the first argument.
2415 // Return result in single register (rax).
2416 __ movp(rcx, r14); // argc.
2417 __ movp(rdx, r15); // argv.
2418 __ Move(r8, ExternalReference::isolate_address(isolate()));
2420 DCHECK_EQ(2, result_size());
2421 // Pass a pointer to the result location as the first argument.
2422 __ leap(rcx, StackSpaceOperand(2));
2423 // Pass a pointer to the Arguments object as the second argument.
2424 __ movp(rdx, r14); // argc.
2425 __ movp(r8, r15); // argv.
2426 __ Move(r9, ExternalReference::isolate_address(isolate()));
2430 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9.
2431 __ movp(rdi, r14); // argc.
2432 __ movp(rsi, r15); // argv.
2433 __ Move(rdx, ExternalReference::isolate_address(isolate()));
2436 // Result is in rax - do not destroy this register!
2439 // If return value is on the stack, pop it to registers.
2440 if (result_size() > 1) {
2441 DCHECK_EQ(2, result_size());
2442 // Read result values stored on stack. Result is stored
2443 // above the four argument mirror slots and the two
2444 // Arguments object slots.
2445 __ movq(rax, Operand(rsp, 6 * kRegisterSize));
2446 __ movq(rdx, Operand(rsp, 7 * kRegisterSize));
2450 // Check result for exception sentinel.
2451 Label exception_returned;
2452 __ CompareRoot(rax, Heap::kExceptionRootIndex);
2453 __ j(equal, &exception_returned);
2455 // Check that there is no pending exception, otherwise we
2456 // should have returned the exception sentinel.
2457 if (FLAG_debug_code) {
2459 __ LoadRoot(r14, Heap::kTheHoleValueRootIndex);
2460 ExternalReference pending_exception_address(
2461 Isolate::kPendingExceptionAddress, isolate());
2462 Operand pending_exception_operand =
2463 masm->ExternalOperand(pending_exception_address);
2464 __ cmpp(r14, pending_exception_operand);
2465 __ j(equal, &okay, Label::kNear);
2470 // Exit the JavaScript to C++ exit frame.
2471 __ LeaveExitFrame(save_doubles());
2474 // Handling of exception.
2475 __ bind(&exception_returned);
2477 ExternalReference pending_handler_context_address(
2478 Isolate::kPendingHandlerContextAddress, isolate());
2479 ExternalReference pending_handler_code_address(
2480 Isolate::kPendingHandlerCodeAddress, isolate());
2481 ExternalReference pending_handler_offset_address(
2482 Isolate::kPendingHandlerOffsetAddress, isolate());
2483 ExternalReference pending_handler_fp_address(
2484 Isolate::kPendingHandlerFPAddress, isolate());
2485 ExternalReference pending_handler_sp_address(
2486 Isolate::kPendingHandlerSPAddress, isolate());
2488 // Ask the runtime for help to determine the handler. This will set rax to
2489 // contain the current pending exception, don't clobber it.
2490 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
2493 FrameScope scope(masm, StackFrame::MANUAL);
2494 __ movp(arg_reg_1, Immediate(0)); // argc.
2495 __ movp(arg_reg_2, Immediate(0)); // argv.
2496 __ Move(arg_reg_3, ExternalReference::isolate_address(isolate()));
2497 __ PrepareCallCFunction(3);
2498 __ CallCFunction(find_handler, 3);
2501 // Retrieve the handler context, SP and FP.
2502 __ movp(rsi, masm->ExternalOperand(pending_handler_context_address));
2503 __ movp(rsp, masm->ExternalOperand(pending_handler_sp_address));
2504 __ movp(rbp, masm->ExternalOperand(pending_handler_fp_address));
2506 // If the handler is a JS frame, restore the context to the frame. Note that
2507 // the context will be set to (rsi == 0) for non-JS frames.
2510 __ j(zero, &skip, Label::kNear);
2511 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rsi);
2514 // Compute the handler entry address and jump to it.
2515 __ movp(rdi, masm->ExternalOperand(pending_handler_code_address));
2516 __ movp(rdx, masm->ExternalOperand(pending_handler_offset_address));
2517 __ leap(rdi, FieldOperand(rdi, rdx, times_1, Code::kHeaderSize));
2522 void JSEntryStub::Generate(MacroAssembler* masm) {
2523 Label invoke, handler_entry, exit;
2524 Label not_outermost_js, not_outermost_js_2;
2526 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2528 { // NOLINT. Scope block confuses linter.
2529 MacroAssembler::NoRootArrayScope uninitialized_root_register(masm);
2534 // Push the stack frame type marker twice.
2535 int marker = type();
2536 // Scratch register is neither callee-save, nor an argument register on any
2537 // platform. It's free to use at this point.
2538 // Cannot use smi-register for loading yet.
2539 __ Move(kScratchRegister, Smi::FromInt(marker), Assembler::RelocInfoNone());
2540 __ Push(kScratchRegister); // context slot
2541 __ Push(kScratchRegister); // function slot
2542 // Save callee-saved registers (X64/X32/Win64 calling conventions).
2548 __ pushq(rdi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
2549 __ pushq(rsi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
2554 // On Win64 XMM6-XMM15 are callee-save
2555 __ subp(rsp, Immediate(EntryFrameConstants::kXMMRegistersBlockSize));
2556 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 0), xmm6);
2557 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 1), xmm7);
2558 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 2), xmm8);
2559 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 3), xmm9);
2560 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 4), xmm10);
2561 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 5), xmm11);
2562 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 6), xmm12);
2563 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 7), xmm13);
2564 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 8), xmm14);
2565 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 9), xmm15);
2568 // Set up the roots and smi constant registers.
2569 // Needs to be done before any further smi loads.
2570 __ InitializeRootRegister();
2573 // Save copies of the top frame descriptor on the stack.
2574 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
2576 Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
2577 __ Push(c_entry_fp_operand);
2580 // If this is the outermost JS call, set js_entry_sp value.
2581 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
2582 __ Load(rax, js_entry_sp);
2584 __ j(not_zero, ¬_outermost_js);
2585 __ Push(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
2587 __ Store(js_entry_sp, rax);
2590 __ bind(¬_outermost_js);
2591 __ Push(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME));
2594 // Jump to a faked try block that does the invoke, with a faked catch
2595 // block that sets the pending exception.
2597 __ bind(&handler_entry);
2598 handler_offset_ = handler_entry.pos();
2599 // Caught exception: Store result (exception) in the pending exception
2600 // field in the JSEnv and return a failure sentinel.
2601 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
2603 __ Store(pending_exception, rax);
2604 __ LoadRoot(rax, Heap::kExceptionRootIndex);
2607 // Invoke: Link this frame into the handler chain.
2609 __ PushStackHandler();
2611 // Clear any pending exceptions.
2612 __ LoadRoot(rax, Heap::kTheHoleValueRootIndex);
2613 __ Store(pending_exception, rax);
2615 // Fake a receiver (NULL).
2616 __ Push(Immediate(0)); // receiver
2618 // Invoke the function by calling through JS entry trampoline builtin and
2619 // pop the faked function when we return. We load the address from an
2620 // external reference instead of inlining the call target address directly
2621 // in the code, because the builtin stubs may not have been generated yet
2622 // at the time this code is generated.
2623 if (type() == StackFrame::ENTRY_CONSTRUCT) {
2624 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
2626 __ Load(rax, construct_entry);
2628 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
2629 __ Load(rax, entry);
2631 __ leap(kScratchRegister, FieldOperand(rax, Code::kHeaderSize));
2632 __ call(kScratchRegister);
2634 // Unlink this frame from the handler chain.
2635 __ PopStackHandler();
2638 // Check if the current stack frame is marked as the outermost JS frame.
2640 __ Cmp(rbx, Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
2641 __ j(not_equal, ¬_outermost_js_2);
2642 __ Move(kScratchRegister, js_entry_sp);
2643 __ movp(Operand(kScratchRegister, 0), Immediate(0));
2644 __ bind(¬_outermost_js_2);
2646 // Restore the top frame descriptor from the stack.
2647 { Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
2648 __ Pop(c_entry_fp_operand);
2651 // Restore callee-saved registers (X64 conventions).
2653 // On Win64 XMM6-XMM15 are callee-save
2654 __ movdqu(xmm6, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 0));
2655 __ movdqu(xmm7, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 1));
2656 __ movdqu(xmm8, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 2));
2657 __ movdqu(xmm9, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 3));
2658 __ movdqu(xmm10, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 4));
2659 __ movdqu(xmm11, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 5));
2660 __ movdqu(xmm12, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 6));
2661 __ movdqu(xmm13, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 7));
2662 __ movdqu(xmm14, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 8));
2663 __ movdqu(xmm15, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 9));
2664 __ addp(rsp, Immediate(EntryFrameConstants::kXMMRegistersBlockSize));
2669 // Callee save on in Win64 ABI, arguments/volatile in AMD64 ABI.
2677 __ addp(rsp, Immediate(2 * kPointerSize)); // remove markers
2679 // Restore frame pointer and return.
2685 void InstanceofStub::Generate(MacroAssembler* masm) {
2686 // Implements "value instanceof function" operator.
2687 // Expected input state with no inline cache:
2688 // rsp[0] : return address
2689 // rsp[8] : function pointer
2691 // Expected input state with an inline one-element cache:
2692 // rsp[0] : return address
2693 // rsp[8] : offset from return address to location of inline cache
2694 // rsp[16] : function pointer
2696 // Returns a bitwise zero to indicate that the value
2697 // is and instance of the function and anything else to
2698 // indicate that the value is not an instance.
2700 // Fixed register usage throughout the stub.
2701 Register object = rax; // Object (lhs).
2702 Register map = rbx; // Map of the object.
2703 Register function = rdx; // Function (rhs).
2704 Register prototype = rdi; // Prototype of the function.
2705 Register scratch = rcx;
2707 static const int kOffsetToMapCheckValue = 2;
2708 static const int kOffsetToResultValue = kPointerSize == kInt64Size ? 18 : 14;
2709 // The last 4 bytes of the instruction sequence
2710 // movp(rdi, FieldOperand(rax, HeapObject::kMapOffset))
2711 // Move(kScratchRegister, Factory::the_hole_value())
2712 // in front of the hole value address.
2713 static const unsigned int kWordBeforeMapCheckValue =
2714 kPointerSize == kInt64Size ? 0xBA49FF78 : 0xBA41FF78;
2715 // The last 4 bytes of the instruction sequence
2716 // __ j(not_equal, &cache_miss);
2717 // __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
2718 // before the offset of the hole value in the root array.
2719 static const unsigned int kWordBeforeResultValue =
2720 kPointerSize == kInt64Size ? 0x458B4906 : 0x458B4106;
2722 int extra_argument_offset = HasCallSiteInlineCheck() ? 1 : 0;
2724 DCHECK_EQ(object.code(), InstanceofStub::left().code());
2725 DCHECK_EQ(function.code(), InstanceofStub::right().code());
2727 // Get the object and function - they are always both needed.
2728 // Go slow case if the object is a smi.
2730 StackArgumentsAccessor args(rsp, 2 + extra_argument_offset,
2731 ARGUMENTS_DONT_CONTAIN_RECEIVER);
2732 if (!HasArgsInRegisters()) {
2733 __ movp(object, args.GetArgumentOperand(0));
2734 __ movp(function, args.GetArgumentOperand(1));
2736 __ JumpIfSmi(object, &slow);
2738 // Check that the left hand is a JS object. Leave its map in rax.
2739 __ CmpObjectType(object, FIRST_SPEC_OBJECT_TYPE, map);
2741 __ CmpInstanceType(map, LAST_SPEC_OBJECT_TYPE);
2744 // If there is a call site cache don't look in the global cache, but do the
2745 // real lookup and update the call site cache.
2746 if (!HasCallSiteInlineCheck() && !ReturnTrueFalseObject()) {
2747 // Look up the function and the map in the instanceof cache.
2749 __ CompareRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
2750 __ j(not_equal, &miss, Label::kNear);
2751 __ CompareRoot(map, Heap::kInstanceofCacheMapRootIndex);
2752 __ j(not_equal, &miss, Label::kNear);
2753 __ LoadRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
2754 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2758 // Get the prototype of the function.
2759 __ TryGetFunctionPrototype(function, prototype, &slow, true);
2761 // Check that the function prototype is a JS object.
2762 __ JumpIfSmi(prototype, &slow);
2763 __ CmpObjectType(prototype, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
2765 __ CmpInstanceType(kScratchRegister, LAST_SPEC_OBJECT_TYPE);
2768 // Update the global instanceof or call site inlined cache with the current
2769 // map and function. The cached answer will be set when it is known below.
2770 if (!HasCallSiteInlineCheck()) {
2771 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
2772 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
2774 // The constants for the code patching are based on push instructions
2775 // at the call site.
2776 DCHECK(!HasArgsInRegisters());
2777 // Get return address and delta to inlined map check.
2778 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2779 __ subp(kScratchRegister, args.GetArgumentOperand(2));
2780 if (FLAG_debug_code) {
2781 __ movl(scratch, Immediate(kWordBeforeMapCheckValue));
2782 __ cmpl(Operand(kScratchRegister, kOffsetToMapCheckValue - 4), scratch);
2783 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCheck);
2785 __ movp(kScratchRegister,
2786 Operand(kScratchRegister, kOffsetToMapCheckValue));
2787 __ movp(Operand(kScratchRegister, 0), map);
2790 // Scratch points at the cell payload. Calculate the start of the object.
2791 __ subp(kScratchRegister, Immediate(Cell::kValueOffset - 1));
2792 __ RecordWriteField(kScratchRegister, Cell::kValueOffset, r8, function,
2793 kDontSaveFPRegs, OMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2796 // Loop through the prototype chain looking for the function prototype.
2797 __ movp(scratch, FieldOperand(map, Map::kPrototypeOffset));
2798 Label loop, is_instance, is_not_instance;
2799 __ LoadRoot(kScratchRegister, Heap::kNullValueRootIndex);
2801 __ cmpp(scratch, prototype);
2802 __ j(equal, &is_instance, Label::kNear);
2803 __ cmpp(scratch, kScratchRegister);
2804 // The code at is_not_instance assumes that kScratchRegister contains a
2805 // non-zero GCable value (the null object in this case).
2806 __ j(equal, &is_not_instance, Label::kNear);
2807 __ movp(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
2808 __ movp(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
2811 __ bind(&is_instance);
2812 if (!HasCallSiteInlineCheck()) {
2814 // Store bitwise zero in the cache. This is a Smi in GC terms.
2815 STATIC_ASSERT(kSmiTag == 0);
2816 __ StoreRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
2817 if (ReturnTrueFalseObject()) {
2818 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
2821 // Store offset of true in the root array at the inline check site.
2822 int true_offset = 0x100 +
2823 (Heap::kTrueValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
2824 // Assert it is a 1-byte signed value.
2825 DCHECK(true_offset >= 0 && true_offset < 0x100);
2826 __ movl(rax, Immediate(true_offset));
2827 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2828 __ subp(kScratchRegister, args.GetArgumentOperand(2));
2829 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
2830 if (FLAG_debug_code) {
2831 __ movl(rax, Immediate(kWordBeforeResultValue));
2832 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
2833 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2835 if (!ReturnTrueFalseObject()) {
2839 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2842 __ bind(&is_not_instance);
2843 if (!HasCallSiteInlineCheck()) {
2844 // We have to store a non-zero value in the cache.
2845 __ StoreRoot(kScratchRegister, Heap::kInstanceofCacheAnswerRootIndex);
2846 if (ReturnTrueFalseObject()) {
2847 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
2850 // Store offset of false in the root array at the inline check site.
2851 int false_offset = 0x100 +
2852 (Heap::kFalseValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
2853 // Assert it is a 1-byte signed value.
2854 DCHECK(false_offset >= 0 && false_offset < 0x100);
2855 __ movl(rax, Immediate(false_offset));
2856 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2857 __ subp(kScratchRegister, args.GetArgumentOperand(2));
2858 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
2859 if (FLAG_debug_code) {
2860 __ movl(rax, Immediate(kWordBeforeResultValue));
2861 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
2862 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2865 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2868 // Slow-case: Go through the JavaScript implementation.
2870 if (!ReturnTrueFalseObject()) {
2871 // Tail call the builtin which returns 0 or 1.
2872 DCHECK(!HasArgsInRegisters());
2873 if (HasCallSiteInlineCheck()) {
2874 // Remove extra value from the stack.
2875 __ PopReturnAddressTo(rcx);
2877 __ PushReturnAddressFrom(rcx);
2879 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
2881 // Call the builtin and convert 0/1 to true/false.
2883 FrameScope scope(masm, StackFrame::INTERNAL);
2886 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
2888 Label true_value, done;
2890 __ j(zero, &true_value, Label::kNear);
2891 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
2892 __ jmp(&done, Label::kNear);
2893 __ bind(&true_value);
2894 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
2896 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2902 // -------------------------------------------------------------------------
2903 // StringCharCodeAtGenerator
2905 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
2906 // If the receiver is a smi trigger the non-string case.
2907 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2908 __ JumpIfSmi(object_, receiver_not_string_);
2910 // Fetch the instance type of the receiver into result register.
2911 __ movp(result_, FieldOperand(object_, HeapObject::kMapOffset));
2912 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2913 // If the receiver is not a string trigger the non-string case.
2914 __ testb(result_, Immediate(kIsNotStringMask));
2915 __ j(not_zero, receiver_not_string_);
2918 // If the index is non-smi trigger the non-smi case.
2919 __ JumpIfNotSmi(index_, &index_not_smi_);
2920 __ bind(&got_smi_index_);
2922 // Check for index out of range.
2923 __ SmiCompare(index_, FieldOperand(object_, String::kLengthOffset));
2924 __ j(above_equal, index_out_of_range_);
2926 __ SmiToInteger32(index_, index_);
2928 StringCharLoadGenerator::Generate(
2929 masm, object_, index_, result_, &call_runtime_);
2931 __ Integer32ToSmi(result_, result_);
2936 void StringCharCodeAtGenerator::GenerateSlow(
2937 MacroAssembler* masm, EmbedMode embed_mode,
2938 const RuntimeCallHelper& call_helper) {
2939 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
2941 Factory* factory = masm->isolate()->factory();
2942 // Index is not a smi.
2943 __ bind(&index_not_smi_);
2944 // If index is a heap number, try converting it to an integer.
2946 factory->heap_number_map(),
2949 call_helper.BeforeCall(masm);
2950 if (embed_mode == PART_OF_IC_HANDLER) {
2951 __ Push(LoadWithVectorDescriptor::VectorRegister());
2952 __ Push(LoadDescriptor::SlotRegister());
2955 __ Push(index_); // Consumed by runtime conversion function.
2956 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
2957 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
2959 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
2960 // NumberToSmi discards numbers that are not exact integers.
2961 __ CallRuntime(Runtime::kNumberToSmi, 1);
2963 if (!index_.is(rax)) {
2964 // Save the conversion result before the pop instructions below
2965 // have a chance to overwrite it.
2966 __ movp(index_, rax);
2969 if (embed_mode == PART_OF_IC_HANDLER) {
2970 __ Pop(LoadDescriptor::SlotRegister());
2971 __ Pop(LoadWithVectorDescriptor::VectorRegister());
2973 // Reload the instance type.
2974 __ movp(result_, FieldOperand(object_, HeapObject::kMapOffset));
2975 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2976 call_helper.AfterCall(masm);
2977 // If index is still not a smi, it must be out of range.
2978 __ JumpIfNotSmi(index_, index_out_of_range_);
2979 // Otherwise, return to the fast path.
2980 __ jmp(&got_smi_index_);
2982 // Call runtime. We get here when the receiver is a string and the
2983 // index is a number, but the code of getting the actual character
2984 // is too complex (e.g., when the string needs to be flattened).
2985 __ bind(&call_runtime_);
2986 call_helper.BeforeCall(masm);
2988 __ Integer32ToSmi(index_, index_);
2990 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
2991 if (!result_.is(rax)) {
2992 __ movp(result_, rax);
2994 call_helper.AfterCall(masm);
2997 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
3001 // -------------------------------------------------------------------------
3002 // StringCharFromCodeGenerator
3004 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3005 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3006 __ JumpIfNotSmi(code_, &slow_case_);
3007 __ SmiCompare(code_, Smi::FromInt(String::kMaxOneByteCharCode));
3008 __ j(above, &slow_case_);
3010 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
3011 SmiIndex index = masm->SmiToIndex(kScratchRegister, code_, kPointerSizeLog2);
3012 __ movp(result_, FieldOperand(result_, index.reg, index.scale,
3013 FixedArray::kHeaderSize));
3014 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
3015 __ j(equal, &slow_case_);
3020 void StringCharFromCodeGenerator::GenerateSlow(
3021 MacroAssembler* masm,
3022 const RuntimeCallHelper& call_helper) {
3023 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
3025 __ bind(&slow_case_);
3026 call_helper.BeforeCall(masm);
3028 __ CallRuntime(Runtime::kCharFromCode, 1);
3029 if (!result_.is(rax)) {
3030 __ movp(result_, rax);
3032 call_helper.AfterCall(masm);
3035 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
3039 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3043 String::Encoding encoding) {
3044 // Nothing to do for zero characters.
3046 __ testl(count, count);
3047 __ j(zero, &done, Label::kNear);
3049 // Make count the number of bytes to copy.
3050 if (encoding == String::TWO_BYTE_ENCODING) {
3051 STATIC_ASSERT(2 == sizeof(uc16));
3052 __ addl(count, count);
3055 // Copy remaining characters.
3058 __ movb(kScratchRegister, Operand(src, 0));
3059 __ movb(Operand(dest, 0), kScratchRegister);
3063 __ j(not_zero, &loop);
3069 void SubStringStub::Generate(MacroAssembler* masm) {
3072 // Stack frame on entry.
3073 // rsp[0] : return address
3078 enum SubStringStubArgumentIndices {
3079 STRING_ARGUMENT_INDEX,
3080 FROM_ARGUMENT_INDEX,
3082 SUB_STRING_ARGUMENT_COUNT
3085 StackArgumentsAccessor args(rsp, SUB_STRING_ARGUMENT_COUNT,
3086 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3088 // Make sure first argument is a string.
3089 __ movp(rax, args.GetArgumentOperand(STRING_ARGUMENT_INDEX));
3090 STATIC_ASSERT(kSmiTag == 0);
3091 __ testl(rax, Immediate(kSmiTagMask));
3092 __ j(zero, &runtime);
3093 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
3094 __ j(NegateCondition(is_string), &runtime);
3097 // rbx: instance type
3098 // Calculate length of sub string using the smi values.
3099 __ movp(rcx, args.GetArgumentOperand(TO_ARGUMENT_INDEX));
3100 __ movp(rdx, args.GetArgumentOperand(FROM_ARGUMENT_INDEX));
3101 __ JumpUnlessBothNonNegativeSmi(rcx, rdx, &runtime);
3103 __ SmiSub(rcx, rcx, rdx); // Overflow doesn't happen.
3104 __ cmpp(rcx, FieldOperand(rax, String::kLengthOffset));
3105 Label not_original_string;
3106 // Shorter than original string's length: an actual substring.
3107 __ j(below, ¬_original_string, Label::kNear);
3108 // Longer than original string's length or negative: unsafe arguments.
3109 __ j(above, &runtime);
3110 // Return original string.
3111 Counters* counters = isolate()->counters();
3112 __ IncrementCounter(counters->sub_string_native(), 1);
3113 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3114 __ bind(¬_original_string);
3117 __ SmiCompare(rcx, Smi::FromInt(1));
3118 __ j(equal, &single_char);
3120 __ SmiToInteger32(rcx, rcx);
3123 // rbx: instance type
3124 // rcx: sub string length
3125 // rdx: from index (smi)
3126 // Deal with different string types: update the index if necessary
3127 // and put the underlying string into edi.
3128 Label underlying_unpacked, sliced_string, seq_or_external_string;
3129 // If the string is not indirect, it can only be sequential or external.
3130 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3131 STATIC_ASSERT(kIsIndirectStringMask != 0);
3132 __ testb(rbx, Immediate(kIsIndirectStringMask));
3133 __ j(zero, &seq_or_external_string, Label::kNear);
3135 __ testb(rbx, Immediate(kSlicedNotConsMask));
3136 __ j(not_zero, &sliced_string, Label::kNear);
3137 // Cons string. Check whether it is flat, then fetch first part.
3138 // Flat cons strings have an empty second part.
3139 __ CompareRoot(FieldOperand(rax, ConsString::kSecondOffset),
3140 Heap::kempty_stringRootIndex);
3141 __ j(not_equal, &runtime);
3142 __ movp(rdi, FieldOperand(rax, ConsString::kFirstOffset));
3143 // Update instance type.
3144 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
3145 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
3146 __ jmp(&underlying_unpacked, Label::kNear);
3148 __ bind(&sliced_string);
3149 // Sliced string. Fetch parent and correct start index by offset.
3150 __ addp(rdx, FieldOperand(rax, SlicedString::kOffsetOffset));
3151 __ movp(rdi, FieldOperand(rax, SlicedString::kParentOffset));
3152 // Update instance type.
3153 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
3154 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
3155 __ jmp(&underlying_unpacked, Label::kNear);
3157 __ bind(&seq_or_external_string);
3158 // Sequential or external string. Just move string to the correct register.
3161 __ bind(&underlying_unpacked);
3163 if (FLAG_string_slices) {
3165 // rdi: underlying subject string
3166 // rbx: instance type of underlying subject string
3167 // rdx: adjusted start index (smi)
3169 // If coming from the make_two_character_string path, the string
3170 // is too short to be sliced anyways.
3171 __ cmpp(rcx, Immediate(SlicedString::kMinLength));
3172 // Short slice. Copy instead of slicing.
3173 __ j(less, ©_routine);
3174 // Allocate new sliced string. At this point we do not reload the instance
3175 // type including the string encoding because we simply rely on the info
3176 // provided by the original string. It does not matter if the original
3177 // string's encoding is wrong because we always have to recheck encoding of
3178 // the newly created string's parent anyways due to externalized strings.
3179 Label two_byte_slice, set_slice_header;
3180 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
3181 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3182 __ testb(rbx, Immediate(kStringEncodingMask));
3183 __ j(zero, &two_byte_slice, Label::kNear);
3184 __ AllocateOneByteSlicedString(rax, rbx, r14, &runtime);
3185 __ jmp(&set_slice_header, Label::kNear);
3186 __ bind(&two_byte_slice);
3187 __ AllocateTwoByteSlicedString(rax, rbx, r14, &runtime);
3188 __ bind(&set_slice_header);
3189 __ Integer32ToSmi(rcx, rcx);
3190 __ movp(FieldOperand(rax, SlicedString::kLengthOffset), rcx);
3191 __ movp(FieldOperand(rax, SlicedString::kHashFieldOffset),
3192 Immediate(String::kEmptyHashField));
3193 __ movp(FieldOperand(rax, SlicedString::kParentOffset), rdi);
3194 __ movp(FieldOperand(rax, SlicedString::kOffsetOffset), rdx);
3195 __ IncrementCounter(counters->sub_string_native(), 1);
3196 __ ret(3 * kPointerSize);
3198 __ bind(©_routine);
3201 // rdi: underlying subject string
3202 // rbx: instance type of underlying subject string
3203 // rdx: adjusted start index (smi)
3205 // The subject string can only be external or sequential string of either
3206 // encoding at this point.
3207 Label two_byte_sequential, sequential_string;
3208 STATIC_ASSERT(kExternalStringTag != 0);
3209 STATIC_ASSERT(kSeqStringTag == 0);
3210 __ testb(rbx, Immediate(kExternalStringTag));
3211 __ j(zero, &sequential_string);
3213 // Handle external string.
3214 // Rule out short external strings.
3215 STATIC_ASSERT(kShortExternalStringTag != 0);
3216 __ testb(rbx, Immediate(kShortExternalStringMask));
3217 __ j(not_zero, &runtime);
3218 __ movp(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
3219 // Move the pointer so that offset-wise, it looks like a sequential string.
3220 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3221 __ subp(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3223 __ bind(&sequential_string);
3224 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
3225 __ testb(rbx, Immediate(kStringEncodingMask));
3226 __ j(zero, &two_byte_sequential);
3228 // Allocate the result.
3229 __ AllocateOneByteString(rax, rcx, r11, r14, r15, &runtime);
3231 // rax: result string
3232 // rcx: result string length
3233 { // Locate character of sub string start.
3234 SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_1);
3235 __ leap(r14, Operand(rdi, smi_as_index.reg, smi_as_index.scale,
3236 SeqOneByteString::kHeaderSize - kHeapObjectTag));
3238 // Locate first character of result.
3239 __ leap(rdi, FieldOperand(rax, SeqOneByteString::kHeaderSize));
3241 // rax: result string
3242 // rcx: result length
3243 // r14: first character of result
3244 // rsi: character of sub string start
3245 StringHelper::GenerateCopyCharacters(
3246 masm, rdi, r14, rcx, String::ONE_BYTE_ENCODING);
3247 __ IncrementCounter(counters->sub_string_native(), 1);
3248 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3250 __ bind(&two_byte_sequential);
3251 // Allocate the result.
3252 __ AllocateTwoByteString(rax, rcx, r11, r14, r15, &runtime);
3254 // rax: result string
3255 // rcx: result string length
3256 { // Locate character of sub string start.
3257 SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_2);
3258 __ leap(r14, Operand(rdi, smi_as_index.reg, smi_as_index.scale,
3259 SeqOneByteString::kHeaderSize - kHeapObjectTag));
3261 // Locate first character of result.
3262 __ leap(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
3264 // rax: result string
3265 // rcx: result length
3266 // rdi: first character of result
3267 // r14: character of sub string start
3268 StringHelper::GenerateCopyCharacters(
3269 masm, rdi, r14, rcx, String::TWO_BYTE_ENCODING);
3270 __ IncrementCounter(counters->sub_string_native(), 1);
3271 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3273 // Just jump to runtime to create the sub string.
3275 __ TailCallRuntime(Runtime::kSubStringRT, 3, 1);
3277 __ bind(&single_char);
3279 // rbx: instance type
3280 // rcx: sub string length (smi)
3281 // rdx: from index (smi)
3282 StringCharAtGenerator generator(rax, rdx, rcx, rax, &runtime, &runtime,
3283 &runtime, STRING_INDEX_IS_NUMBER,
3284 RECEIVER_IS_STRING);
3285 generator.GenerateFast(masm);
3286 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3287 generator.SkipSlow(masm, &runtime);
3291 void ToNumberStub::Generate(MacroAssembler* masm) {
3292 // The ToNumber stub takes one argument in rax.
3294 __ JumpIfNotSmi(rax, ¬_smi, Label::kNear);
3298 Label not_heap_number;
3299 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
3300 Heap::kHeapNumberMapRootIndex);
3301 __ j(not_equal, ¬_heap_number, Label::kNear);
3303 __ bind(¬_heap_number);
3305 Label not_string, slow_string;
3306 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, rdi);
3309 __ j(above_equal, ¬_string, Label::kNear);
3310 // Check if string has a cached array index.
3311 __ testl(FieldOperand(rax, String::kHashFieldOffset),
3312 Immediate(String::kContainsCachedArrayIndexMask));
3313 __ j(not_zero, &slow_string, Label::kNear);
3314 __ movl(rax, FieldOperand(rax, String::kHashFieldOffset));
3315 __ IndexFromHash(rax, rax);
3317 __ bind(&slow_string);
3318 __ PopReturnAddressTo(rcx); // Pop return address.
3319 __ Push(rax); // Push argument.
3320 __ PushReturnAddressFrom(rcx); // Push return address.
3321 __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
3322 __ bind(¬_string);
3325 __ CmpInstanceType(rdi, ODDBALL_TYPE);
3326 __ j(not_equal, ¬_oddball, Label::kNear);
3327 __ movp(rax, FieldOperand(rax, Oddball::kToNumberOffset));
3329 __ bind(¬_oddball);
3331 __ PopReturnAddressTo(rcx); // Pop return address.
3332 __ Push(rax); // Push argument.
3333 __ PushReturnAddressFrom(rcx); // Push return address.
3334 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
3338 void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
3342 Register scratch2) {
3343 Register length = scratch1;
3346 Label check_zero_length;
3347 __ movp(length, FieldOperand(left, String::kLengthOffset));
3348 __ SmiCompare(length, FieldOperand(right, String::kLengthOffset));
3349 __ j(equal, &check_zero_length, Label::kNear);
3350 __ Move(rax, Smi::FromInt(NOT_EQUAL));
3353 // Check if the length is zero.
3354 Label compare_chars;
3355 __ bind(&check_zero_length);
3356 STATIC_ASSERT(kSmiTag == 0);
3358 __ j(not_zero, &compare_chars, Label::kNear);
3359 __ Move(rax, Smi::FromInt(EQUAL));
3362 // Compare characters.
3363 __ bind(&compare_chars);
3364 Label strings_not_equal;
3365 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
3366 &strings_not_equal, Label::kNear);
3368 // Characters are equal.
3369 __ Move(rax, Smi::FromInt(EQUAL));
3372 // Characters are not equal.
3373 __ bind(&strings_not_equal);
3374 __ Move(rax, Smi::FromInt(NOT_EQUAL));
3379 void StringHelper::GenerateCompareFlatOneByteStrings(
3380 MacroAssembler* masm, Register left, Register right, Register scratch1,
3381 Register scratch2, Register scratch3, Register scratch4) {
3382 // Ensure that you can always subtract a string length from a non-negative
3383 // number (e.g. another length).
3384 STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
3386 // Find minimum length and length difference.
3387 __ movp(scratch1, FieldOperand(left, String::kLengthOffset));
3388 __ movp(scratch4, scratch1);
3391 FieldOperand(right, String::kLengthOffset));
3392 // Register scratch4 now holds left.length - right.length.
3393 const Register length_difference = scratch4;
3395 __ j(less, &left_shorter, Label::kNear);
3396 // The right string isn't longer that the left one.
3397 // Get the right string's length by subtracting the (non-negative) difference
3398 // from the left string's length.
3399 __ SmiSub(scratch1, scratch1, length_difference);
3400 __ bind(&left_shorter);
3401 // Register scratch1 now holds Min(left.length, right.length).
3402 const Register min_length = scratch1;
3404 Label compare_lengths;
3405 // If min-length is zero, go directly to comparing lengths.
3406 __ SmiTest(min_length);
3407 __ j(zero, &compare_lengths, Label::kNear);
3410 Label result_not_equal;
3411 GenerateOneByteCharsCompareLoop(
3412 masm, left, right, min_length, scratch2, &result_not_equal,
3413 // In debug-code mode, SmiTest below might push
3414 // the target label outside the near range.
3417 // Completed loop without finding different characters.
3418 // Compare lengths (precomputed).
3419 __ bind(&compare_lengths);
3420 __ SmiTest(length_difference);
3421 Label length_not_equal;
3422 __ j(not_zero, &length_not_equal, Label::kNear);
3425 __ Move(rax, Smi::FromInt(EQUAL));
3428 Label result_greater;
3430 __ bind(&length_not_equal);
3431 __ j(greater, &result_greater, Label::kNear);
3432 __ jmp(&result_less, Label::kNear);
3433 __ bind(&result_not_equal);
3434 // Unequal comparison of left to right, either character or length.
3435 __ j(above, &result_greater, Label::kNear);
3436 __ bind(&result_less);
3439 __ Move(rax, Smi::FromInt(LESS));
3442 // Result is GREATER.
3443 __ bind(&result_greater);
3444 __ Move(rax, Smi::FromInt(GREATER));
3449 void StringHelper::GenerateOneByteCharsCompareLoop(
3450 MacroAssembler* masm, Register left, Register right, Register length,
3451 Register scratch, Label* chars_not_equal, Label::Distance near_jump) {
3452 // Change index to run from -length to -1 by adding length to string
3453 // start. This means that loop ends when index reaches zero, which
3454 // doesn't need an additional compare.
3455 __ SmiToInteger32(length, length);
3457 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
3459 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
3461 Register index = length; // index = -length;
3466 __ movb(scratch, Operand(left, index, times_1, 0));
3467 __ cmpb(scratch, Operand(right, index, times_1, 0));
3468 __ j(not_equal, chars_not_equal, near_jump);
3470 __ j(not_zero, &loop);
3474 void StringCompareStub::Generate(MacroAssembler* masm) {
3477 // Stack frame on entry.
3478 // rsp[0] : return address
3479 // rsp[8] : right string
3480 // rsp[16] : left string
3482 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER);
3483 __ movp(rdx, args.GetArgumentOperand(0)); // left
3484 __ movp(rax, args.GetArgumentOperand(1)); // right
3486 // Check for identity.
3489 __ j(not_equal, ¬_same, Label::kNear);
3490 __ Move(rax, Smi::FromInt(EQUAL));
3491 Counters* counters = isolate()->counters();
3492 __ IncrementCounter(counters->string_compare_native(), 1);
3493 __ ret(2 * kPointerSize);
3497 // Check that both are sequential one-byte strings.
3498 __ JumpIfNotBothSequentialOneByteStrings(rdx, rax, rcx, rbx, &runtime);
3500 // Inline comparison of one-byte strings.
3501 __ IncrementCounter(counters->string_compare_native(), 1);
3502 // Drop arguments from the stack
3503 __ PopReturnAddressTo(rcx);
3504 __ addp(rsp, Immediate(2 * kPointerSize));
3505 __ PushReturnAddressFrom(rcx);
3506 StringHelper::GenerateCompareFlatOneByteStrings(masm, rdx, rax, rcx, rbx, rdi,
3509 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
3510 // tagged as a small integer.
3512 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3516 void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3517 // ----------- S t a t e -------------
3520 // -- rsp[0] : return address
3521 // -----------------------------------
3523 // Load rcx with the allocation site. We stick an undefined dummy value here
3524 // and replace it with the real allocation site later when we instantiate this
3525 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3526 __ Move(rcx, handle(isolate()->heap()->undefined_value()));
3528 // Make sure that we actually patched the allocation site.
3529 if (FLAG_debug_code) {
3530 __ testb(rcx, Immediate(kSmiTagMask));
3531 __ Assert(not_equal, kExpectedAllocationSite);
3532 __ Cmp(FieldOperand(rcx, HeapObject::kMapOffset),
3533 isolate()->factory()->allocation_site_map());
3534 __ Assert(equal, kExpectedAllocationSite);
3537 // Tail call into the stub that handles binary operations with allocation
3539 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3540 __ TailCallStub(&stub);
3544 void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3545 DCHECK(state() == CompareICState::SMI);
3547 __ JumpIfNotBothSmi(rdx, rax, &miss, Label::kNear);
3549 if (GetCondition() == equal) {
3550 // For equality we do not care about the sign of the result.
3555 __ j(no_overflow, &done, Label::kNear);
3556 // Correct sign of result in case of overflow.
3568 void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3569 DCHECK(state() == CompareICState::NUMBER);
3572 Label unordered, maybe_undefined1, maybe_undefined2;
3575 if (left() == CompareICState::SMI) {
3576 __ JumpIfNotSmi(rdx, &miss);
3578 if (right() == CompareICState::SMI) {
3579 __ JumpIfNotSmi(rax, &miss);
3582 // Load left and right operand.
3583 Label done, left, left_smi, right_smi;
3584 __ JumpIfSmi(rax, &right_smi, Label::kNear);
3585 __ CompareMap(rax, isolate()->factory()->heap_number_map());
3586 __ j(not_equal, &maybe_undefined1, Label::kNear);
3587 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
3588 __ jmp(&left, Label::kNear);
3589 __ bind(&right_smi);
3590 __ SmiToInteger32(rcx, rax); // Can't clobber rax yet.
3591 __ Cvtlsi2sd(xmm1, rcx);
3594 __ JumpIfSmi(rdx, &left_smi, Label::kNear);
3595 __ CompareMap(rdx, isolate()->factory()->heap_number_map());
3596 __ j(not_equal, &maybe_undefined2, Label::kNear);
3597 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
3600 __ SmiToInteger32(rcx, rdx); // Can't clobber rdx yet.
3601 __ Cvtlsi2sd(xmm0, rcx);
3605 __ ucomisd(xmm0, xmm1);
3607 // Don't base result on EFLAGS when a NaN is involved.
3608 __ j(parity_even, &unordered, Label::kNear);
3610 // Return a result of -1, 0, or 1, based on EFLAGS.
3611 // Performing mov, because xor would destroy the flag register.
3612 __ movl(rax, Immediate(0));
3613 __ movl(rcx, Immediate(0));
3614 __ setcc(above, rax); // Add one to zero if carry clear and not equal.
3615 __ sbbp(rax, rcx); // Subtract one if below (aka. carry set).
3618 __ bind(&unordered);
3619 __ bind(&generic_stub);
3620 CompareICStub stub(isolate(), op(), strength(), CompareICState::GENERIC,
3621 CompareICState::GENERIC, CompareICState::GENERIC);
3622 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3624 __ bind(&maybe_undefined1);
3625 if (Token::IsOrderedRelationalCompareOp(op())) {
3626 __ Cmp(rax, isolate()->factory()->undefined_value());
3627 __ j(not_equal, &miss);
3628 __ JumpIfSmi(rdx, &unordered);
3629 __ CmpObjectType(rdx, HEAP_NUMBER_TYPE, rcx);
3630 __ j(not_equal, &maybe_undefined2, Label::kNear);
3634 __ bind(&maybe_undefined2);
3635 if (Token::IsOrderedRelationalCompareOp(op())) {
3636 __ Cmp(rdx, isolate()->factory()->undefined_value());
3637 __ j(equal, &unordered);
3645 void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3646 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3647 DCHECK(GetCondition() == equal);
3649 // Registers containing left and right operands respectively.
3650 Register left = rdx;
3651 Register right = rax;
3652 Register tmp1 = rcx;
3653 Register tmp2 = rbx;
3655 // Check that both operands are heap objects.
3657 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3658 __ j(cond, &miss, Label::kNear);
3660 // Check that both operands are internalized strings.
3661 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3662 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3663 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3664 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3665 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3667 __ testb(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
3668 __ j(not_zero, &miss, Label::kNear);
3670 // Internalized strings are compared by identity.
3672 __ cmpp(left, right);
3673 // Make sure rax is non-zero. At this point input operands are
3674 // guaranteed to be non-zero.
3675 DCHECK(right.is(rax));
3676 __ j(not_equal, &done, Label::kNear);
3677 STATIC_ASSERT(EQUAL == 0);
3678 STATIC_ASSERT(kSmiTag == 0);
3679 __ Move(rax, Smi::FromInt(EQUAL));
3688 void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3689 DCHECK(state() == CompareICState::UNIQUE_NAME);
3690 DCHECK(GetCondition() == equal);
3692 // Registers containing left and right operands respectively.
3693 Register left = rdx;
3694 Register right = rax;
3695 Register tmp1 = rcx;
3696 Register tmp2 = rbx;
3698 // Check that both operands are heap objects.
3700 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3701 __ j(cond, &miss, Label::kNear);
3703 // Check that both operands are unique names. This leaves the instance
3704 // types loaded in tmp1 and tmp2.
3705 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3706 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3707 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3708 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3710 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
3711 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
3713 // Unique names are compared by identity.
3715 __ cmpp(left, right);
3716 // Make sure rax is non-zero. At this point input operands are
3717 // guaranteed to be non-zero.
3718 DCHECK(right.is(rax));
3719 __ j(not_equal, &done, Label::kNear);
3720 STATIC_ASSERT(EQUAL == 0);
3721 STATIC_ASSERT(kSmiTag == 0);
3722 __ Move(rax, Smi::FromInt(EQUAL));
3731 void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3732 DCHECK(state() == CompareICState::STRING);
3735 bool equality = Token::IsEqualityOp(op());
3737 // Registers containing left and right operands respectively.
3738 Register left = rdx;
3739 Register right = rax;
3740 Register tmp1 = rcx;
3741 Register tmp2 = rbx;
3742 Register tmp3 = rdi;
3744 // Check that both operands are heap objects.
3745 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3748 // Check that both operands are strings. This leaves the instance
3749 // types loaded in tmp1 and tmp2.
3750 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3751 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3752 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3753 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3754 __ movp(tmp3, tmp1);
3755 STATIC_ASSERT(kNotStringTag != 0);
3757 __ testb(tmp3, Immediate(kIsNotStringMask));
3758 __ j(not_zero, &miss);
3760 // Fast check for identical strings.
3762 __ cmpp(left, right);
3763 __ j(not_equal, ¬_same, Label::kNear);
3764 STATIC_ASSERT(EQUAL == 0);
3765 STATIC_ASSERT(kSmiTag == 0);
3766 __ Move(rax, Smi::FromInt(EQUAL));
3769 // Handle not identical strings.
3772 // Check that both strings are internalized strings. If they are, we're done
3773 // because we already know they are not identical. We also know they are both
3777 STATIC_ASSERT(kInternalizedTag == 0);
3779 __ testb(tmp1, Immediate(kIsNotInternalizedMask));
3780 __ j(not_zero, &do_compare, Label::kNear);
3781 // Make sure rax is non-zero. At this point input operands are
3782 // guaranteed to be non-zero.
3783 DCHECK(right.is(rax));
3785 __ bind(&do_compare);
3788 // Check that both strings are sequential one-byte.
3790 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
3792 // Compare flat one-byte strings. Returns when done.
3794 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
3797 StringHelper::GenerateCompareFlatOneByteStrings(
3798 masm, left, right, tmp1, tmp2, tmp3, kScratchRegister);
3801 // Handle more complex cases in runtime.
3803 __ PopReturnAddressTo(tmp1);
3806 __ PushReturnAddressFrom(tmp1);
3808 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3810 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3818 void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3819 DCHECK(state() == CompareICState::OBJECT);
3821 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
3822 __ j(either_smi, &miss, Label::kNear);
3824 __ CmpObjectType(rax, JS_OBJECT_TYPE, rcx);
3825 __ j(not_equal, &miss, Label::kNear);
3826 __ CmpObjectType(rdx, JS_OBJECT_TYPE, rcx);
3827 __ j(not_equal, &miss, Label::kNear);
3829 DCHECK(GetCondition() == equal);
3838 void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
3840 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
3841 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
3842 __ j(either_smi, &miss, Label::kNear);
3844 __ GetWeakValue(rdi, cell);
3845 __ movp(rcx, FieldOperand(rax, HeapObject::kMapOffset));
3846 __ movp(rbx, FieldOperand(rdx, HeapObject::kMapOffset));
3848 __ j(not_equal, &miss, Label::kNear);
3850 __ j(not_equal, &miss, Label::kNear);
3860 void CompareICStub::GenerateMiss(MacroAssembler* masm) {
3862 // Call the runtime system in a fresh internal frame.
3863 ExternalReference miss =
3864 ExternalReference(IC_Utility(IC::kCompareIC_Miss), isolate());
3866 FrameScope scope(masm, StackFrame::INTERNAL);
3871 __ Push(Smi::FromInt(op()));
3872 __ CallExternalReference(miss, 3);
3874 // Compute the entry point of the rewritten stub.
3875 __ leap(rdi, FieldOperand(rax, Code::kHeaderSize));
3880 // Do a tail call to the rewritten stub.
3885 void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3888 Register properties,
3891 DCHECK(name->IsUniqueName());
3892 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3893 // not equal to the name and kProbes-th slot is not used (its name is the
3894 // undefined value), it guarantees the hash table doesn't contain the
3895 // property. It's true even if some slots represent deleted properties
3896 // (their names are the hole value).
3897 for (int i = 0; i < kInlinedProbes; i++) {
3898 // r0 points to properties hash.
3899 // Compute the masked index: (hash + i + i * i) & mask.
3900 Register index = r0;
3901 // Capacity is smi 2^n.
3902 __ SmiToInteger32(index, FieldOperand(properties, kCapacityOffset));
3905 Immediate(name->Hash() + NameDictionary::GetProbeOffset(i)));
3907 // Scale the index by multiplying by the entry size.
3908 DCHECK(NameDictionary::kEntrySize == 3);
3909 __ leap(index, Operand(index, index, times_2, 0)); // index *= 3.
3911 Register entity_name = r0;
3912 // Having undefined at this place means the name is not contained.
3913 DCHECK_EQ(kSmiTagSize, 1);
3914 __ movp(entity_name, Operand(properties,
3917 kElementsStartOffset - kHeapObjectTag));
3918 __ Cmp(entity_name, masm->isolate()->factory()->undefined_value());
3921 // Stop if found the property.
3922 __ Cmp(entity_name, Handle<Name>(name));
3926 // Check for the hole and skip.
3927 __ CompareRoot(entity_name, Heap::kTheHoleValueRootIndex);
3928 __ j(equal, &good, Label::kNear);
3930 // Check if the entry name is not a unique name.
3931 __ movp(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3932 __ JumpIfNotUniqueNameInstanceType(
3933 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3937 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3939 __ Push(Handle<Object>(name));
3940 __ Push(Immediate(name->Hash()));
3943 __ j(not_zero, miss);
3948 // Probe the name dictionary in the |elements| register. Jump to the
3949 // |done| label if a property with the given name is found leaving the
3950 // index into the dictionary in |r1|. Jump to the |miss| label
3952 void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3959 DCHECK(!elements.is(r0));
3960 DCHECK(!elements.is(r1));
3961 DCHECK(!name.is(r0));
3962 DCHECK(!name.is(r1));
3964 __ AssertName(name);
3966 __ SmiToInteger32(r0, FieldOperand(elements, kCapacityOffset));
3969 for (int i = 0; i < kInlinedProbes; i++) {
3970 // Compute the masked index: (hash + i + i * i) & mask.
3971 __ movl(r1, FieldOperand(name, Name::kHashFieldOffset));
3972 __ shrl(r1, Immediate(Name::kHashShift));
3974 __ addl(r1, Immediate(NameDictionary::GetProbeOffset(i)));
3978 // Scale the index by multiplying by the entry size.
3979 DCHECK(NameDictionary::kEntrySize == 3);
3980 __ leap(r1, Operand(r1, r1, times_2, 0)); // r1 = r1 * 3
3982 // Check if the key is identical to the name.
3983 __ cmpp(name, Operand(elements, r1, times_pointer_size,
3984 kElementsStartOffset - kHeapObjectTag));
3988 NameDictionaryLookupStub stub(masm->isolate(), elements, r0, r1,
3991 __ movl(r0, FieldOperand(name, Name::kHashFieldOffset));
3992 __ shrl(r0, Immediate(Name::kHashShift));
4002 void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
4003 // This stub overrides SometimesSetsUpAFrame() to return false. That means
4004 // we cannot call anything that could cause a GC from this stub.
4005 // Stack frame on entry:
4006 // rsp[0 * kPointerSize] : return address.
4007 // rsp[1 * kPointerSize] : key's hash.
4008 // rsp[2 * kPointerSize] : key.
4010 // dictionary_: NameDictionary to probe.
4011 // result_: used as scratch.
4012 // index_: will hold an index of entry if lookup is successful.
4013 // might alias with result_.
4015 // result_ is zero if lookup failed, non zero otherwise.
4017 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
4019 Register scratch = result();
4021 __ SmiToInteger32(scratch, FieldOperand(dictionary(), kCapacityOffset));
4025 // If names of slots in range from 1 to kProbes - 1 for the hash value are
4026 // not equal to the name and kProbes-th slot is not used (its name is the
4027 // undefined value), it guarantees the hash table doesn't contain the
4028 // property. It's true even if some slots represent deleted properties
4029 // (their names are the null value).
4030 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER,
4032 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
4033 // Compute the masked index: (hash + i + i * i) & mask.
4034 __ movp(scratch, args.GetArgumentOperand(1));
4036 __ addl(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
4038 __ andp(scratch, Operand(rsp, 0));
4040 // Scale the index by multiplying by the entry size.
4041 DCHECK(NameDictionary::kEntrySize == 3);
4042 __ leap(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
4044 // Having undefined at this place means the name is not contained.
4045 __ movp(scratch, Operand(dictionary(), index(), times_pointer_size,
4046 kElementsStartOffset - kHeapObjectTag));
4048 __ Cmp(scratch, isolate()->factory()->undefined_value());
4049 __ j(equal, ¬_in_dictionary);
4051 // Stop if found the property.
4052 __ cmpp(scratch, args.GetArgumentOperand(0));
4053 __ j(equal, &in_dictionary);
4055 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
4056 // If we hit a key that is not a unique name during negative
4057 // lookup we have to bailout as this key might be equal to the
4058 // key we are looking for.
4060 // Check if the entry name is not a unique name.
4061 __ movp(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4062 __ JumpIfNotUniqueNameInstanceType(
4063 FieldOperand(scratch, Map::kInstanceTypeOffset),
4064 &maybe_in_dictionary);
4068 __ bind(&maybe_in_dictionary);
4069 // If we are doing negative lookup then probing failure should be
4070 // treated as a lookup success. For positive lookup probing failure
4071 // should be treated as lookup failure.
4072 if (mode() == POSITIVE_LOOKUP) {
4073 __ movp(scratch, Immediate(0));
4075 __ ret(2 * kPointerSize);
4078 __ bind(&in_dictionary);
4079 __ movp(scratch, Immediate(1));
4081 __ ret(2 * kPointerSize);
4083 __ bind(¬_in_dictionary);
4084 __ movp(scratch, Immediate(0));
4086 __ ret(2 * kPointerSize);
4090 void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
4092 StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
4094 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
4099 // Takes the input in 3 registers: address_ value_ and object_. A pointer to
4100 // the value has just been written into the object, now this stub makes sure
4101 // we keep the GC informed. The word in the object where the value has been
4102 // written is in the address register.
4103 void RecordWriteStub::Generate(MacroAssembler* masm) {
4104 Label skip_to_incremental_noncompacting;
4105 Label skip_to_incremental_compacting;
4107 // The first two instructions are generated with labels so as to get the
4108 // offset fixed up correctly by the bind(Label*) call. We patch it back and
4109 // forth between a compare instructions (a nop in this position) and the
4110 // real branch when we start and stop incremental heap marking.
4111 // See RecordWriteStub::Patch for details.
4112 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
4113 __ jmp(&skip_to_incremental_compacting, Label::kFar);
4115 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4116 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4117 MacroAssembler::kReturnAtEnd);
4122 __ bind(&skip_to_incremental_noncompacting);
4123 GenerateIncremental(masm, INCREMENTAL);
4125 __ bind(&skip_to_incremental_compacting);
4126 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4128 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
4129 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
4130 masm->set_byte_at(0, kTwoByteNopInstruction);
4131 masm->set_byte_at(2, kFiveByteNopInstruction);
4135 void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4138 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4139 Label dont_need_remembered_set;
4141 __ movp(regs_.scratch0(), Operand(regs_.address(), 0));
4142 __ JumpIfNotInNewSpace(regs_.scratch0(),
4144 &dont_need_remembered_set);
4146 __ CheckPageFlag(regs_.object(),
4148 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4150 &dont_need_remembered_set);
4152 // First notify the incremental marker if necessary, then update the
4154 CheckNeedsToInformIncrementalMarker(
4155 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
4156 InformIncrementalMarker(masm);
4157 regs_.Restore(masm);
4158 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4159 MacroAssembler::kReturnAtEnd);
4161 __ bind(&dont_need_remembered_set);
4164 CheckNeedsToInformIncrementalMarker(
4165 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
4166 InformIncrementalMarker(masm);
4167 regs_.Restore(masm);
4172 void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
4173 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
4175 arg_reg_1.is(regs_.address()) ? kScratchRegister : regs_.address();
4176 DCHECK(!address.is(regs_.object()));
4177 DCHECK(!address.is(arg_reg_1));
4178 __ Move(address, regs_.address());
4179 __ Move(arg_reg_1, regs_.object());
4180 // TODO(gc) Can we just set address arg2 in the beginning?
4181 __ Move(arg_reg_2, address);
4182 __ LoadAddress(arg_reg_3,
4183 ExternalReference::isolate_address(isolate()));
4184 int argument_count = 3;
4186 AllowExternalCallThatCantCauseGC scope(masm);
4187 __ PrepareCallCFunction(argument_count);
4189 ExternalReference::incremental_marking_record_write_function(isolate()),
4191 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
4195 void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4196 MacroAssembler* masm,
4197 OnNoNeedToInformIncrementalMarker on_no_need,
4200 Label need_incremental;
4201 Label need_incremental_pop_object;
4203 __ movp(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
4204 __ andp(regs_.scratch0(), regs_.object());
4205 __ movp(regs_.scratch1(),
4206 Operand(regs_.scratch0(),
4207 MemoryChunk::kWriteBarrierCounterOffset));
4208 __ subp(regs_.scratch1(), Immediate(1));
4209 __ movp(Operand(regs_.scratch0(),
4210 MemoryChunk::kWriteBarrierCounterOffset),
4212 __ j(negative, &need_incremental);
4214 // Let's look at the color of the object: If it is not black we don't have
4215 // to inform the incremental marker.
4216 __ JumpIfBlack(regs_.object(),
4222 regs_.Restore(masm);
4223 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4224 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4225 MacroAssembler::kReturnAtEnd);
4232 // Get the value from the slot.
4233 __ movp(regs_.scratch0(), Operand(regs_.address(), 0));
4235 if (mode == INCREMENTAL_COMPACTION) {
4236 Label ensure_not_white;
4238 __ CheckPageFlag(regs_.scratch0(), // Contains value.
4239 regs_.scratch1(), // Scratch.
4240 MemoryChunk::kEvacuationCandidateMask,
4245 __ CheckPageFlag(regs_.object(),
4246 regs_.scratch1(), // Scratch.
4247 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4251 __ bind(&ensure_not_white);
4254 // We need an extra register for this, so we push the object register
4256 __ Push(regs_.object());
4257 __ EnsureNotWhite(regs_.scratch0(), // The value.
4258 regs_.scratch1(), // Scratch.
4259 regs_.object(), // Scratch.
4260 &need_incremental_pop_object,
4262 __ Pop(regs_.object());
4264 regs_.Restore(masm);
4265 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4266 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4267 MacroAssembler::kReturnAtEnd);
4272 __ bind(&need_incremental_pop_object);
4273 __ Pop(regs_.object());
4275 __ bind(&need_incremental);
4277 // Fall through when we need to inform the incremental marker.
4281 void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4282 // ----------- S t a t e -------------
4283 // -- rax : element value to store
4284 // -- rcx : element index as smi
4285 // -- rsp[0] : return address
4286 // -- rsp[8] : array literal index in function
4287 // -- rsp[16] : array literal
4288 // clobbers rbx, rdx, rdi
4289 // -----------------------------------
4292 Label double_elements;
4294 Label slow_elements;
4295 Label fast_elements;
4297 // Get array literal index, array literal and its map.
4298 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4299 __ movp(rdx, args.GetArgumentOperand(1));
4300 __ movp(rbx, args.GetArgumentOperand(0));
4301 __ movp(rdi, FieldOperand(rbx, JSObject::kMapOffset));
4303 __ CheckFastElements(rdi, &double_elements);
4305 // FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS
4306 __ JumpIfSmi(rax, &smi_element);
4307 __ CheckFastSmiElements(rdi, &fast_elements);
4309 // Store into the array literal requires a elements transition. Call into
4312 __ bind(&slow_elements);
4313 __ PopReturnAddressTo(rdi);
4317 __ movp(rbx, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
4318 __ Push(FieldOperand(rbx, JSFunction::kLiteralsOffset));
4320 __ PushReturnAddressFrom(rdi);
4321 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4323 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
4324 __ bind(&fast_elements);
4325 __ SmiToInteger32(kScratchRegister, rcx);
4326 __ movp(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
4327 __ leap(rcx, FieldOperand(rbx, kScratchRegister, times_pointer_size,
4328 FixedArrayBase::kHeaderSize));
4329 __ movp(Operand(rcx, 0), rax);
4330 // Update the write barrier for the array store.
4331 __ RecordWrite(rbx, rcx, rax,
4333 EMIT_REMEMBERED_SET,
4337 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or
4338 // FAST_*_ELEMENTS, and value is Smi.
4339 __ bind(&smi_element);
4340 __ SmiToInteger32(kScratchRegister, rcx);
4341 __ movp(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
4342 __ movp(FieldOperand(rbx, kScratchRegister, times_pointer_size,
4343 FixedArrayBase::kHeaderSize), rax);
4346 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
4347 __ bind(&double_elements);
4349 __ movp(r9, FieldOperand(rbx, JSObject::kElementsOffset));
4350 __ SmiToInteger32(r11, rcx);
4351 __ StoreNumberToDoubleElements(rax,
4360 void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4361 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4362 __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
4363 int parameter_count_offset =
4364 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4365 __ movp(rbx, MemOperand(rbp, parameter_count_offset));
4366 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4367 __ PopReturnAddressTo(rcx);
4368 int additional_offset =
4369 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
4370 __ leap(rsp, MemOperand(rsp, rbx, times_pointer_size, additional_offset));
4371 __ jmp(rcx); // Return to IC Miss stub, continuation still on stack.
4375 void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4376 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4377 LoadICStub stub(isolate(), state());
4378 stub.GenerateForTrampoline(masm);
4382 void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4383 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4384 KeyedLoadICStub stub(isolate(), state());
4385 stub.GenerateForTrampoline(masm);
4389 static void HandleArrayCases(MacroAssembler* masm, Register receiver,
4390 Register key, Register vector, Register slot,
4391 Register feedback, Register receiver_map,
4392 Register scratch1, Register scratch2,
4393 Register scratch3, bool is_polymorphic,
4395 // feedback initially contains the feedback array
4396 Label next_loop, prepare_next;
4397 Label start_polymorphic;
4399 Register counter = scratch1;
4400 Register length = scratch2;
4401 Register cached_map = scratch3;
4403 __ movp(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
4404 __ cmpp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4405 __ j(not_equal, &start_polymorphic);
4407 // found, now call handler.
4408 Register handler = feedback;
4409 __ movp(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
4410 __ leap(handler, FieldOperand(handler, Code::kHeaderSize));
4413 // Polymorphic, we have to loop from 2 to N
4414 __ bind(&start_polymorphic);
4415 __ SmiToInteger32(length, FieldOperand(feedback, FixedArray::kLengthOffset));
4416 if (!is_polymorphic) {
4417 // If the IC could be monomorphic we have to make sure we don't go past the
4418 // end of the feedback array.
4419 __ cmpl(length, Immediate(2));
4422 __ movl(counter, Immediate(2));
4424 __ bind(&next_loop);
4425 __ movp(cached_map, FieldOperand(feedback, counter, times_pointer_size,
4426 FixedArray::kHeaderSize));
4427 __ cmpp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4428 __ j(not_equal, &prepare_next);
4429 __ movp(handler, FieldOperand(feedback, counter, times_pointer_size,
4430 FixedArray::kHeaderSize + kPointerSize));
4431 __ leap(handler, FieldOperand(handler, Code::kHeaderSize));
4434 __ bind(&prepare_next);
4435 __ addl(counter, Immediate(2));
4436 __ cmpl(counter, length);
4437 __ j(less, &next_loop);
4439 // We exhausted our array of map handler pairs.
4444 static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
4445 Register receiver_map, Register feedback,
4446 Register vector, Register integer_slot,
4447 Label* compare_map, Label* load_smi_map,
4449 __ JumpIfSmi(receiver, load_smi_map);
4450 __ movp(receiver_map, FieldOperand(receiver, 0));
4452 __ bind(compare_map);
4453 __ cmpp(receiver_map, FieldOperand(feedback, WeakCell::kValueOffset));
4454 __ j(not_equal, try_array);
4455 Register handler = feedback;
4456 __ movp(handler, FieldOperand(vector, integer_slot, times_pointer_size,
4457 FixedArray::kHeaderSize + kPointerSize));
4458 __ leap(handler, FieldOperand(handler, Code::kHeaderSize));
4463 void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
4466 void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4467 GenerateImpl(masm, true);
4471 void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4472 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // rdx
4473 Register name = LoadWithVectorDescriptor::NameRegister(); // rcx
4474 Register vector = LoadWithVectorDescriptor::VectorRegister(); // rbx
4475 Register slot = LoadWithVectorDescriptor::SlotRegister(); // rax
4476 Register feedback = rdi;
4477 Register integer_slot = r8;
4478 Register receiver_map = r9;
4480 __ SmiToInteger32(integer_slot, slot);
4481 __ movp(feedback, FieldOperand(vector, integer_slot, times_pointer_size,
4482 FixedArray::kHeaderSize));
4484 // Try to quickly handle the monomorphic case without knowing for sure
4485 // if we have a weak cell in feedback. We do know it's safe to look
4486 // at WeakCell::kValueOffset.
4487 Label try_array, load_smi_map, compare_map;
4488 Label not_array, miss;
4489 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector,
4490 integer_slot, &compare_map, &load_smi_map, &try_array);
4492 // Is it a fixed array?
4493 __ bind(&try_array);
4494 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
4495 __ j(not_equal, ¬_array);
4496 HandleArrayCases(masm, receiver, name, vector, slot, feedback, receiver_map,
4497 integer_slot, r11, r15, true, &miss);
4499 __ bind(¬_array);
4500 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
4501 __ j(not_equal, &miss);
4502 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4503 Code::ComputeHandlerFlags(Code::LOAD_IC));
4504 masm->isolate()->stub_cache()->GenerateProbe(
4505 masm, Code::LOAD_IC, code_flags, false, receiver, name, feedback, no_reg);
4508 LoadIC::GenerateMiss(masm);
4510 __ bind(&load_smi_map);
4511 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4512 __ jmp(&compare_map);
4516 void KeyedLoadICStub::Generate(MacroAssembler* masm) {
4517 GenerateImpl(masm, false);
4521 void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4522 GenerateImpl(masm, true);
4526 void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4527 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // rdx
4528 Register key = LoadWithVectorDescriptor::NameRegister(); // rcx
4529 Register vector = LoadWithVectorDescriptor::VectorRegister(); // rbx
4530 Register slot = LoadWithVectorDescriptor::SlotRegister(); // rax
4531 Register feedback = rdi;
4532 Register integer_slot = r8;
4533 Register receiver_map = r9;
4535 __ SmiToInteger32(integer_slot, slot);
4536 __ movp(feedback, FieldOperand(vector, integer_slot, times_pointer_size,
4537 FixedArray::kHeaderSize));
4539 // Try to quickly handle the monomorphic case without knowing for sure
4540 // if we have a weak cell in feedback. We do know it's safe to look
4541 // at WeakCell::kValueOffset.
4542 Label try_array, load_smi_map, compare_map;
4543 Label not_array, miss;
4544 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector,
4545 integer_slot, &compare_map, &load_smi_map, &try_array);
4547 __ bind(&try_array);
4548 // Is it a fixed array?
4549 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
4550 __ j(not_equal, ¬_array);
4552 // We have a polymorphic element handler.
4553 Label polymorphic, try_poly_name;
4554 __ bind(&polymorphic);
4555 HandleArrayCases(masm, receiver, key, vector, slot, feedback, receiver_map,
4556 integer_slot, r11, r15, true, &miss);
4558 __ bind(¬_array);
4560 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
4561 __ j(not_equal, &try_poly_name);
4562 Handle<Code> megamorphic_stub =
4563 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4564 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4566 __ bind(&try_poly_name);
4567 // We might have a name in feedback, and a fixed array in the next slot.
4568 __ cmpp(key, feedback);
4569 __ j(not_equal, &miss);
4570 // If the name comparison succeeded, we know we have a fixed array with
4571 // at least one map/handler pair.
4572 __ movp(feedback, FieldOperand(vector, integer_slot, times_pointer_size,
4573 FixedArray::kHeaderSize + kPointerSize));
4574 HandleArrayCases(masm, receiver, key, vector, slot, feedback, receiver_map,
4575 integer_slot, r11, r15, false, &miss);
4578 KeyedLoadIC::GenerateMiss(masm);
4580 __ bind(&load_smi_map);
4581 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4582 __ jmp(&compare_map);
4586 void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4587 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4588 VectorStoreICStub stub(isolate(), state());
4589 stub.GenerateForTrampoline(masm);
4593 void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4594 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4595 VectorKeyedStoreICStub stub(isolate(), state());
4596 stub.GenerateForTrampoline(masm);
4600 void VectorStoreICStub::Generate(MacroAssembler* masm) {
4601 GenerateImpl(masm, false);
4605 void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4606 GenerateImpl(masm, true);
4610 void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4613 // TODO(mvstanton): Implement.
4615 StoreIC::GenerateMiss(masm);
4619 void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4620 GenerateImpl(masm, false);
4624 void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4625 GenerateImpl(masm, true);
4629 void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4632 // TODO(mvstanton): Implement.
4634 KeyedStoreIC::GenerateMiss(masm);
4638 void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4639 EmitLoadTypeFeedbackVector(masm, rbx);
4640 CallICStub stub(isolate(), state());
4641 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4645 void CallIC_ArrayTrampolineStub::Generate(MacroAssembler* masm) {
4646 EmitLoadTypeFeedbackVector(masm, rbx);
4647 CallIC_ArrayStub stub(isolate(), state());
4648 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4652 void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4653 if (masm->isolate()->function_entry_hook() != NULL) {
4654 ProfileEntryHookStub stub(masm->isolate());
4655 masm->CallStub(&stub);
4660 void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4661 // This stub can be called from essentially anywhere, so it needs to save
4662 // all volatile and callee-save registers.
4663 const size_t kNumSavedRegisters = 2;
4664 __ pushq(arg_reg_1);
4665 __ pushq(arg_reg_2);
4667 // Calculate the original stack pointer and store it in the second arg.
4669 Operand(rsp, kNumSavedRegisters * kRegisterSize + kPCOnStackSize));
4671 // Calculate the function address to the first arg.
4672 __ movp(arg_reg_1, Operand(rsp, kNumSavedRegisters * kRegisterSize));
4673 __ subp(arg_reg_1, Immediate(Assembler::kShortCallInstructionLength));
4675 // Save the remainder of the volatile registers.
4676 masm->PushCallerSaved(kSaveFPRegs, arg_reg_1, arg_reg_2);
4678 // Call the entry hook function.
4679 __ Move(rax, FUNCTION_ADDR(isolate()->function_entry_hook()),
4680 Assembler::RelocInfoNone());
4682 AllowExternalCallThatCantCauseGC scope(masm);
4684 const int kArgumentCount = 2;
4685 __ PrepareCallCFunction(kArgumentCount);
4686 __ CallCFunction(rax, kArgumentCount);
4688 // Restore volatile regs.
4689 masm->PopCallerSaved(kSaveFPRegs, arg_reg_1, arg_reg_2);
4698 static void CreateArrayDispatch(MacroAssembler* masm,
4699 AllocationSiteOverrideMode mode) {
4700 if (mode == DISABLE_ALLOCATION_SITES) {
4701 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
4702 __ TailCallStub(&stub);
4703 } else if (mode == DONT_OVERRIDE) {
4704 int last_index = GetSequenceIndexFromFastElementsKind(
4705 TERMINAL_FAST_ELEMENTS_KIND);
4706 for (int i = 0; i <= last_index; ++i) {
4708 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4709 __ cmpl(rdx, Immediate(kind));
4710 __ j(not_equal, &next);
4711 T stub(masm->isolate(), kind);
4712 __ TailCallStub(&stub);
4716 // If we reached this point there is a problem.
4717 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4724 static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4725 AllocationSiteOverrideMode mode) {
4726 // rbx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4727 // rdx - kind (if mode != DISABLE_ALLOCATION_SITES)
4728 // rax - number of arguments
4729 // rdi - constructor?
4730 // rsp[0] - return address
4731 // rsp[8] - last argument
4732 Handle<Object> undefined_sentinel(
4733 masm->isolate()->heap()->undefined_value(),
4736 Label normal_sequence;
4737 if (mode == DONT_OVERRIDE) {
4738 DCHECK(FAST_SMI_ELEMENTS == 0);
4739 DCHECK(FAST_HOLEY_SMI_ELEMENTS == 1);
4740 DCHECK(FAST_ELEMENTS == 2);
4741 DCHECK(FAST_HOLEY_ELEMENTS == 3);
4742 DCHECK(FAST_DOUBLE_ELEMENTS == 4);
4743 DCHECK(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4745 // is the low bit set? If so, we are holey and that is good.
4746 __ testb(rdx, Immediate(1));
4747 __ j(not_zero, &normal_sequence);
4750 // look at the first argument
4751 StackArgumentsAccessor args(rsp, 1, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4752 __ movp(rcx, args.GetArgumentOperand(0));
4754 __ j(zero, &normal_sequence);
4756 if (mode == DISABLE_ALLOCATION_SITES) {
4757 ElementsKind initial = GetInitialFastElementsKind();
4758 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4760 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4762 DISABLE_ALLOCATION_SITES);
4763 __ TailCallStub(&stub_holey);
4765 __ bind(&normal_sequence);
4766 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4768 DISABLE_ALLOCATION_SITES);
4769 __ TailCallStub(&stub);
4770 } else if (mode == DONT_OVERRIDE) {
4771 // We are going to create a holey array, but our kind is non-holey.
4772 // Fix kind and retry (only if we have an allocation site in the slot).
4775 if (FLAG_debug_code) {
4776 Handle<Map> allocation_site_map =
4777 masm->isolate()->factory()->allocation_site_map();
4778 __ Cmp(FieldOperand(rbx, 0), allocation_site_map);
4779 __ Assert(equal, kExpectedAllocationSite);
4782 // Save the resulting elements kind in type info. We can't just store r3
4783 // in the AllocationSite::transition_info field because elements kind is
4784 // restricted to a portion of the field...upper bits need to be left alone.
4785 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4786 __ SmiAddConstant(FieldOperand(rbx, AllocationSite::kTransitionInfoOffset),
4787 Smi::FromInt(kFastElementsKindPackedToHoley));
4789 __ bind(&normal_sequence);
4790 int last_index = GetSequenceIndexFromFastElementsKind(
4791 TERMINAL_FAST_ELEMENTS_KIND);
4792 for (int i = 0; i <= last_index; ++i) {
4794 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4795 __ cmpl(rdx, Immediate(kind));
4796 __ j(not_equal, &next);
4797 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4798 __ TailCallStub(&stub);
4802 // If we reached this point there is a problem.
4803 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4811 static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4812 int to_index = GetSequenceIndexFromFastElementsKind(
4813 TERMINAL_FAST_ELEMENTS_KIND);
4814 for (int i = 0; i <= to_index; ++i) {
4815 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4816 T stub(isolate, kind);
4818 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4819 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4826 void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4827 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4829 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4831 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4836 void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4838 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4839 for (int i = 0; i < 2; i++) {
4840 // For internal arrays we only need a few things
4841 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4843 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4845 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4851 void ArrayConstructorStub::GenerateDispatchToArrayStub(
4852 MacroAssembler* masm,
4853 AllocationSiteOverrideMode mode) {
4854 if (argument_count() == ANY) {
4855 Label not_zero_case, not_one_case;
4857 __ j(not_zero, ¬_zero_case);
4858 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4860 __ bind(¬_zero_case);
4861 __ cmpl(rax, Immediate(1));
4862 __ j(greater, ¬_one_case);
4863 CreateArrayDispatchOneArgument(masm, mode);
4865 __ bind(¬_one_case);
4866 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4867 } else if (argument_count() == NONE) {
4868 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4869 } else if (argument_count() == ONE) {
4870 CreateArrayDispatchOneArgument(masm, mode);
4871 } else if (argument_count() == MORE_THAN_ONE) {
4872 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4879 void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4880 // ----------- S t a t e -------------
4882 // -- rbx : AllocationSite or undefined
4883 // -- rdi : constructor
4884 // -- rdx : original constructor
4885 // -- rsp[0] : return address
4886 // -- rsp[8] : last argument
4887 // -----------------------------------
4888 if (FLAG_debug_code) {
4889 // The array construct code is only set for the global and natives
4890 // builtin Array functions which always have maps.
4892 // Initial map for the builtin Array function should be a map.
4893 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
4894 // Will both indicate a NULL and a Smi.
4895 STATIC_ASSERT(kSmiTag == 0);
4896 Condition not_smi = NegateCondition(masm->CheckSmi(rcx));
4897 __ Check(not_smi, kUnexpectedInitialMapForArrayFunction);
4898 __ CmpObjectType(rcx, MAP_TYPE, rcx);
4899 __ Check(equal, kUnexpectedInitialMapForArrayFunction);
4901 // We should either have undefined in rbx or a valid AllocationSite
4902 __ AssertUndefinedOrAllocationSite(rbx);
4907 __ j(not_equal, &subclassing);
4910 // If the feedback vector is the undefined value call an array constructor
4911 // that doesn't use AllocationSites.
4912 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
4913 __ j(equal, &no_info);
4915 // Only look at the lower 16 bits of the transition info.
4916 __ movp(rdx, FieldOperand(rbx, AllocationSite::kTransitionInfoOffset));
4917 __ SmiToInteger32(rdx, rdx);
4918 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4919 __ andp(rdx, Immediate(AllocationSite::ElementsKindBits::kMask));
4920 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4923 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
4926 __ bind(&subclassing);
4927 __ Pop(rcx); // return address.
4932 switch (argument_count()) {
4935 __ addp(rax, Immediate(2));
4938 __ movp(rax, Immediate(2));
4941 __ movp(rax, Immediate(3));
4946 __ JumpToExternalReference(
4947 ExternalReference(Runtime::kArrayConstructorWithSubclassing, isolate()),
4952 void InternalArrayConstructorStub::GenerateCase(
4953 MacroAssembler* masm, ElementsKind kind) {
4954 Label not_zero_case, not_one_case;
4955 Label normal_sequence;
4958 __ j(not_zero, ¬_zero_case);
4959 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4960 __ TailCallStub(&stub0);
4962 __ bind(¬_zero_case);
4963 __ cmpl(rax, Immediate(1));
4964 __ j(greater, ¬_one_case);
4966 if (IsFastPackedElementsKind(kind)) {
4967 // We might need to create a holey array
4968 // look at the first argument
4969 StackArgumentsAccessor args(rsp, 1, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4970 __ movp(rcx, args.GetArgumentOperand(0));
4972 __ j(zero, &normal_sequence);
4974 InternalArraySingleArgumentConstructorStub
4975 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4976 __ TailCallStub(&stub1_holey);
4979 __ bind(&normal_sequence);
4980 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4981 __ TailCallStub(&stub1);
4983 __ bind(¬_one_case);
4984 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4985 __ TailCallStub(&stubN);
4989 void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4990 // ----------- S t a t e -------------
4992 // -- rdi : constructor
4993 // -- rsp[0] : return address
4994 // -- rsp[8] : last argument
4995 // -----------------------------------
4997 if (FLAG_debug_code) {
4998 // The array construct code is only set for the global and natives
4999 // builtin Array functions which always have maps.
5001 // Initial map for the builtin Array function should be a map.
5002 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
5003 // Will both indicate a NULL and a Smi.
5004 STATIC_ASSERT(kSmiTag == 0);
5005 Condition not_smi = NegateCondition(masm->CheckSmi(rcx));
5006 __ Check(not_smi, kUnexpectedInitialMapForArrayFunction);
5007 __ CmpObjectType(rcx, MAP_TYPE, rcx);
5008 __ Check(equal, kUnexpectedInitialMapForArrayFunction);
5011 // Figure out the right elements kind
5012 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
5014 // Load the map's "bit field 2" into |result|. We only need the first byte,
5015 // but the following masking takes care of that anyway.
5016 __ movzxbp(rcx, FieldOperand(rcx, Map::kBitField2Offset));
5017 // Retrieve elements_kind from bit field 2.
5018 __ DecodeField<Map::ElementsKindBits>(rcx);
5020 if (FLAG_debug_code) {
5022 __ cmpl(rcx, Immediate(FAST_ELEMENTS));
5024 __ cmpl(rcx, Immediate(FAST_HOLEY_ELEMENTS));
5026 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
5030 Label fast_elements_case;
5031 __ cmpl(rcx, Immediate(FAST_ELEMENTS));
5032 __ j(equal, &fast_elements_case);
5033 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
5035 __ bind(&fast_elements_case);
5036 GenerateCase(masm, FAST_ELEMENTS);
5040 static int Offset(ExternalReference ref0, ExternalReference ref1) {
5041 int64_t offset = (ref0.address() - ref1.address());
5042 // Check that fits into int.
5043 DCHECK(static_cast<int>(offset) == offset);
5044 return static_cast<int>(offset);
5048 // Prepares stack to put arguments (aligns and so on). WIN64 calling
5049 // convention requires to put the pointer to the return value slot into
5050 // rcx (rcx must be preserverd until CallApiFunctionAndReturn). Saves
5051 // context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
5052 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
5053 static void PrepareCallApiFunction(MacroAssembler* masm, int arg_stack_space) {
5054 __ EnterApiExitFrame(arg_stack_space);
5058 // Calls an API function. Allocates HandleScope, extracts returned value
5059 // from handle and propagates exceptions. Clobbers r14, r15, rbx and
5060 // caller-save registers. Restores context. On return removes
5061 // stack_space * kPointerSize (GCed).
5062 static void CallApiFunctionAndReturn(MacroAssembler* masm,
5063 Register function_address,
5064 ExternalReference thunk_ref,
5065 Register thunk_last_arg, int stack_space,
5066 Operand* stack_space_operand,
5067 Operand return_value_operand,
5068 Operand* context_restore_operand) {
5070 Label promote_scheduled_exception;
5071 Label delete_allocated_handles;
5072 Label leave_exit_frame;
5075 Isolate* isolate = masm->isolate();
5076 Factory* factory = isolate->factory();
5077 ExternalReference next_address =
5078 ExternalReference::handle_scope_next_address(isolate);
5079 const int kNextOffset = 0;
5080 const int kLimitOffset = Offset(
5081 ExternalReference::handle_scope_limit_address(isolate), next_address);
5082 const int kLevelOffset = Offset(
5083 ExternalReference::handle_scope_level_address(isolate), next_address);
5084 ExternalReference scheduled_exception_address =
5085 ExternalReference::scheduled_exception_address(isolate);
5087 DCHECK(rdx.is(function_address) || r8.is(function_address));
5088 // Allocate HandleScope in callee-save registers.
5089 Register prev_next_address_reg = r14;
5090 Register prev_limit_reg = rbx;
5091 Register base_reg = r15;
5092 __ Move(base_reg, next_address);
5093 __ movp(prev_next_address_reg, Operand(base_reg, kNextOffset));
5094 __ movp(prev_limit_reg, Operand(base_reg, kLimitOffset));
5095 __ addl(Operand(base_reg, kLevelOffset), Immediate(1));
5097 if (FLAG_log_timer_events) {
5098 FrameScope frame(masm, StackFrame::MANUAL);
5099 __ PushSafepointRegisters();
5100 __ PrepareCallCFunction(1);
5101 __ LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate));
5102 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5104 __ PopSafepointRegisters();
5107 Label profiler_disabled;
5108 Label end_profiler_check;
5109 __ Move(rax, ExternalReference::is_profiling_address(isolate));
5110 __ cmpb(Operand(rax, 0), Immediate(0));
5111 __ j(zero, &profiler_disabled);
5113 // Third parameter is the address of the actual getter function.
5114 __ Move(thunk_last_arg, function_address);
5115 __ Move(rax, thunk_ref);
5116 __ jmp(&end_profiler_check);
5118 __ bind(&profiler_disabled);
5119 // Call the api function!
5120 __ Move(rax, function_address);
5122 __ bind(&end_profiler_check);
5124 // Call the api function!
5127 if (FLAG_log_timer_events) {
5128 FrameScope frame(masm, StackFrame::MANUAL);
5129 __ PushSafepointRegisters();
5130 __ PrepareCallCFunction(1);
5131 __ LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate));
5132 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5134 __ PopSafepointRegisters();
5137 // Load the value from ReturnValue
5138 __ movp(rax, return_value_operand);
5141 // No more valid handles (the result handle was the last one). Restore
5142 // previous handle scope.
5143 __ subl(Operand(base_reg, kLevelOffset), Immediate(1));
5144 __ movp(Operand(base_reg, kNextOffset), prev_next_address_reg);
5145 __ cmpp(prev_limit_reg, Operand(base_reg, kLimitOffset));
5146 __ j(not_equal, &delete_allocated_handles);
5148 // Leave the API exit frame.
5149 __ bind(&leave_exit_frame);
5150 bool restore_context = context_restore_operand != NULL;
5151 if (restore_context) {
5152 __ movp(rsi, *context_restore_operand);
5154 if (stack_space_operand != nullptr) {
5155 __ movp(rbx, *stack_space_operand);
5157 __ LeaveApiExitFrame(!restore_context);
5159 // Check if the function scheduled an exception.
5160 __ Move(rdi, scheduled_exception_address);
5161 __ Cmp(Operand(rdi, 0), factory->the_hole_value());
5162 __ j(not_equal, &promote_scheduled_exception);
5165 // Check if the function returned a valid JavaScript value.
5167 Register return_value = rax;
5170 __ JumpIfSmi(return_value, &ok, Label::kNear);
5171 __ movp(map, FieldOperand(return_value, HeapObject::kMapOffset));
5173 __ CmpInstanceType(map, LAST_NAME_TYPE);
5174 __ j(below_equal, &ok, Label::kNear);
5176 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
5177 __ j(above_equal, &ok, Label::kNear);
5179 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
5180 __ j(equal, &ok, Label::kNear);
5182 __ CompareRoot(return_value, Heap::kUndefinedValueRootIndex);
5183 __ j(equal, &ok, Label::kNear);
5185 __ CompareRoot(return_value, Heap::kTrueValueRootIndex);
5186 __ j(equal, &ok, Label::kNear);
5188 __ CompareRoot(return_value, Heap::kFalseValueRootIndex);
5189 __ j(equal, &ok, Label::kNear);
5191 __ CompareRoot(return_value, Heap::kNullValueRootIndex);
5192 __ j(equal, &ok, Label::kNear);
5194 __ Abort(kAPICallReturnedInvalidObject);
5199 if (stack_space_operand != nullptr) {
5200 DCHECK_EQ(stack_space, 0);
5201 __ PopReturnAddressTo(rcx);
5205 __ ret(stack_space * kPointerSize);
5208 // Re-throw by promoting a scheduled exception.
5209 __ bind(&promote_scheduled_exception);
5210 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
5212 // HandleScope limit has changed. Delete allocated extensions.
5213 __ bind(&delete_allocated_handles);
5214 __ movp(Operand(base_reg, kLimitOffset), prev_limit_reg);
5215 __ movp(prev_limit_reg, rax);
5216 __ LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate));
5218 ExternalReference::delete_handle_scope_extensions(isolate));
5220 __ movp(rax, prev_limit_reg);
5221 __ jmp(&leave_exit_frame);
5225 static void CallApiFunctionStubHelper(MacroAssembler* masm,
5226 const ParameterCount& argc,
5227 bool return_first_arg,
5228 bool call_data_undefined) {
5229 // ----------- S t a t e -------------
5231 // -- rbx : call_data
5233 // -- rdx : api_function_address
5235 // -- rax : number of arguments if argc is a register
5236 // -- rsp[0] : return address
5237 // -- rsp[8] : last argument
5239 // -- rsp[argc * 8] : first argument
5240 // -- rsp[(argc + 1) * 8] : receiver
5241 // -----------------------------------
5243 Register callee = rdi;
5244 Register call_data = rbx;
5245 Register holder = rcx;
5246 Register api_function_address = rdx;
5247 Register context = rsi;
5248 Register return_address = r8;
5250 typedef FunctionCallbackArguments FCA;
5252 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5253 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5254 STATIC_ASSERT(FCA::kDataIndex == 4);
5255 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5256 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5257 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5258 STATIC_ASSERT(FCA::kHolderIndex == 0);
5259 STATIC_ASSERT(FCA::kArgsLength == 7);
5261 DCHECK(argc.is_immediate() || rax.is(argc.reg()));
5263 __ PopReturnAddressTo(return_address);
5273 Register scratch = call_data;
5274 if (!call_data_undefined) {
5275 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5279 // return value default
5282 __ Move(scratch, ExternalReference::isolate_address(masm->isolate()));
5287 __ movp(scratch, rsp);
5288 // Push return address back on stack.
5289 __ PushReturnAddressFrom(return_address);
5291 // load context from callee
5292 __ movp(context, FieldOperand(callee, JSFunction::kContextOffset));
5294 // Allocate the v8::Arguments structure in the arguments' space since
5295 // it's not controlled by GC.
5296 const int kApiStackSpace = 4;
5298 PrepareCallApiFunction(masm, kApiStackSpace);
5300 // FunctionCallbackInfo::implicit_args_.
5301 __ movp(StackSpaceOperand(0), scratch);
5302 if (argc.is_immediate()) {
5303 __ addp(scratch, Immediate((argc.immediate() + FCA::kArgsLength - 1) *
5305 // FunctionCallbackInfo::values_.
5306 __ movp(StackSpaceOperand(1), scratch);
5307 // FunctionCallbackInfo::length_.
5308 __ Set(StackSpaceOperand(2), argc.immediate());
5309 // FunctionCallbackInfo::is_construct_call_.
5310 __ Set(StackSpaceOperand(3), 0);
5312 __ leap(scratch, Operand(scratch, argc.reg(), times_pointer_size,
5313 (FCA::kArgsLength - 1) * kPointerSize));
5314 // FunctionCallbackInfo::values_.
5315 __ movp(StackSpaceOperand(1), scratch);
5316 // FunctionCallbackInfo::length_.
5317 __ movp(StackSpaceOperand(2), argc.reg());
5318 // FunctionCallbackInfo::is_construct_call_.
5319 __ leap(argc.reg(), Operand(argc.reg(), times_pointer_size,
5320 (FCA::kArgsLength + 1) * kPointerSize));
5321 __ movp(StackSpaceOperand(3), argc.reg());
5324 #if defined(__MINGW64__) || defined(_WIN64)
5325 Register arguments_arg = rcx;
5326 Register callback_arg = rdx;
5328 Register arguments_arg = rdi;
5329 Register callback_arg = rsi;
5332 // It's okay if api_function_address == callback_arg
5333 // but not arguments_arg
5334 DCHECK(!api_function_address.is(arguments_arg));
5336 // v8::InvocationCallback's argument.
5337 __ leap(arguments_arg, StackSpaceOperand(0));
5339 ExternalReference thunk_ref =
5340 ExternalReference::invoke_function_callback(masm->isolate());
5342 // Accessor for FunctionCallbackInfo and first js arg.
5343 StackArgumentsAccessor args_from_rbp(rbp, FCA::kArgsLength + 1,
5344 ARGUMENTS_DONT_CONTAIN_RECEIVER);
5345 Operand context_restore_operand = args_from_rbp.GetArgumentOperand(
5346 FCA::kArgsLength - FCA::kContextSaveIndex);
5347 Operand is_construct_call_operand = StackSpaceOperand(3);
5348 Operand return_value_operand = args_from_rbp.GetArgumentOperand(
5349 return_first_arg ? 0 : FCA::kArgsLength - FCA::kReturnValueOffset);
5350 int stack_space = 0;
5351 Operand* stack_space_operand = &is_construct_call_operand;
5352 if (argc.is_immediate()) {
5353 stack_space = argc.immediate() + FCA::kArgsLength + 1;
5354 stack_space_operand = nullptr;
5356 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, callback_arg,
5357 stack_space, stack_space_operand,
5358 return_value_operand, &context_restore_operand);
5362 void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5363 bool call_data_undefined = this->call_data_undefined();
5364 CallApiFunctionStubHelper(masm, ParameterCount(rax), false,
5365 call_data_undefined);
5369 void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5370 bool is_store = this->is_store();
5371 int argc = this->argc();
5372 bool call_data_undefined = this->call_data_undefined();
5373 CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
5374 call_data_undefined);
5378 void CallApiGetterStub::Generate(MacroAssembler* masm) {
5379 // ----------- S t a t e -------------
5380 // -- rsp[0] : return address
5382 // -- rsp[16 - kArgsLength*8] : PropertyCallbackArguments object
5384 // -- r8 : api_function_address
5385 // -----------------------------------
5387 #if defined(__MINGW64__) || defined(_WIN64)
5388 Register getter_arg = r8;
5389 Register accessor_info_arg = rdx;
5390 Register name_arg = rcx;
5392 Register getter_arg = rdx;
5393 Register accessor_info_arg = rsi;
5394 Register name_arg = rdi;
5396 Register api_function_address = ApiGetterDescriptor::function_address();
5397 DCHECK(api_function_address.is(r8));
5398 Register scratch = rax;
5400 // v8::Arguments::values_ and handler for name.
5401 const int kStackSpace = PropertyCallbackArguments::kArgsLength + 1;
5403 // Allocate v8::AccessorInfo in non-GCed stack space.
5404 const int kArgStackSpace = 1;
5406 __ leap(name_arg, Operand(rsp, kPCOnStackSize));
5408 PrepareCallApiFunction(masm, kArgStackSpace);
5409 __ leap(scratch, Operand(name_arg, 1 * kPointerSize));
5411 // v8::PropertyAccessorInfo::args_.
5412 __ movp(StackSpaceOperand(0), scratch);
5414 // The context register (rsi) has been saved in PrepareCallApiFunction and
5415 // could be used to pass arguments.
5416 __ leap(accessor_info_arg, StackSpaceOperand(0));
5418 ExternalReference thunk_ref =
5419 ExternalReference::invoke_accessor_getter_callback(isolate());
5421 // It's okay if api_function_address == getter_arg
5422 // but not accessor_info_arg or name_arg
5423 DCHECK(!api_function_address.is(accessor_info_arg) &&
5424 !api_function_address.is(name_arg));
5426 // The name handler is counted as an argument.
5427 StackArgumentsAccessor args(rbp, PropertyCallbackArguments::kArgsLength);
5428 Operand return_value_operand = args.GetArgumentOperand(
5429 PropertyCallbackArguments::kArgsLength - 1 -
5430 PropertyCallbackArguments::kReturnValueOffset);
5431 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, getter_arg,
5432 kStackSpace, nullptr, return_value_operand, NULL);
5438 } // namespace internal
5441 #endif // V8_TARGET_ARCH_X64