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);
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);
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 __ TailCallRuntime(Runtime::kLoadElementWithInterceptor, 2, 1);
894 PropertyAccessCompiler::TailCallBuiltin(
895 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
899 void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
900 // Return address is on the stack.
903 Register receiver = LoadDescriptor::ReceiverRegister();
904 Register index = LoadDescriptor::NameRegister();
905 Register scratch = rdi;
906 Register result = rax;
907 DCHECK(!scratch.is(receiver) && !scratch.is(index));
908 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
909 result.is(LoadDescriptor::SlotRegister()));
911 // StringCharAtGenerator doesn't use the result register until it's passed
912 // the different miss possibilities. If it did, we would have a conflict
913 // when FLAG_vector_ics is true.
914 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
915 &miss, // When not a string.
916 &miss, // When not a number.
917 &miss, // When index out of range.
918 STRING_INDEX_IS_ARRAY_INDEX,
920 char_at_generator.GenerateFast(masm);
923 StubRuntimeCallHelper call_helper;
924 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
927 PropertyAccessCompiler::TailCallBuiltin(
928 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
932 void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
933 // rsp[0] : return address
934 // rsp[8] : number of parameters
935 // rsp[16] : receiver displacement
936 // rsp[24] : function
938 // Check if the calling frame is an arguments adaptor frame.
939 Label adaptor_frame, try_allocate, runtime;
940 __ movp(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
941 __ movp(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
942 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
943 __ j(equal, &adaptor_frame);
945 // Get the length from the frame.
946 StackArgumentsAccessor args(rsp, 3, ARGUMENTS_DONT_CONTAIN_RECEIVER);
947 __ movp(rcx, args.GetArgumentOperand(2));
948 __ SmiToInteger64(rcx, rcx);
949 __ jmp(&try_allocate);
951 // Patch the arguments.length and the parameters pointer.
952 __ bind(&adaptor_frame);
953 __ movp(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
955 __ movp(args.GetArgumentOperand(2), rcx);
956 __ SmiToInteger64(rcx, rcx);
957 __ leap(rdx, Operand(rdx, rcx, times_pointer_size,
958 StandardFrameConstants::kCallerSPOffset));
959 __ movp(args.GetArgumentOperand(1), rdx);
961 // Try the new space allocation. Start out with computing the size of
962 // the arguments object and the elements array.
963 Label add_arguments_object;
964 __ bind(&try_allocate);
966 __ j(zero, &add_arguments_object, Label::kNear);
967 __ leap(rcx, Operand(rcx, times_pointer_size, FixedArray::kHeaderSize));
968 __ bind(&add_arguments_object);
969 __ addp(rcx, Immediate(Heap::kStrictArgumentsObjectSize));
971 // Do the allocation of both objects in one go.
972 __ Allocate(rcx, rax, rdx, rbx, &runtime, TAG_OBJECT);
974 // Get the arguments map from the current native context.
975 __ movp(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
976 __ movp(rdi, FieldOperand(rdi, GlobalObject::kNativeContextOffset));
977 const int offset = Context::SlotOffset(Context::STRICT_ARGUMENTS_MAP_INDEX);
978 __ movp(rdi, Operand(rdi, offset));
980 __ movp(FieldOperand(rax, JSObject::kMapOffset), rdi);
981 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
982 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
983 __ movp(FieldOperand(rax, JSObject::kElementsOffset), kScratchRegister);
985 // Get the length (smi tagged) and set that as an in-object property too.
986 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
987 __ movp(rcx, args.GetArgumentOperand(2));
988 __ movp(FieldOperand(rax, JSObject::kHeaderSize +
989 Heap::kArgumentsLengthIndex * kPointerSize),
992 // If there are no actual arguments, we're done.
997 // Get the parameters pointer from the stack.
998 __ movp(rdx, args.GetArgumentOperand(1));
1000 // Set up the elements pointer in the allocated arguments object and
1001 // initialize the header in the elements fixed array.
1002 __ leap(rdi, Operand(rax, Heap::kStrictArgumentsObjectSize));
1003 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rdi);
1004 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
1005 __ movp(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
1008 __ movp(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
1009 // Untag the length for the loop below.
1010 __ SmiToInteger64(rcx, rcx);
1012 // Copy the fixed array slots.
1015 __ movp(rbx, Operand(rdx, -1 * kPointerSize)); // Skip receiver.
1016 __ movp(FieldOperand(rdi, FixedArray::kHeaderSize), rbx);
1017 __ addp(rdi, Immediate(kPointerSize));
1018 __ subp(rdx, Immediate(kPointerSize));
1020 __ j(not_zero, &loop);
1022 // Return and remove the on-stack parameters.
1024 __ ret(3 * kPointerSize);
1026 // Do the runtime call to allocate the arguments object.
1028 __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
1032 void RegExpExecStub::Generate(MacroAssembler* masm) {
1033 // Just jump directly to runtime if native RegExp is not selected at compile
1034 // time or if regexp entry in generated code is turned off runtime switch or
1036 #ifdef V8_INTERPRETED_REGEXP
1037 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1038 #else // V8_INTERPRETED_REGEXP
1040 // Stack frame on entry.
1041 // rsp[0] : return address
1042 // rsp[8] : last_match_info (expected JSArray)
1043 // rsp[16] : previous index
1044 // rsp[24] : subject string
1045 // rsp[32] : JSRegExp object
1047 enum RegExpExecStubArgumentIndices {
1048 JS_REG_EXP_OBJECT_ARGUMENT_INDEX,
1049 SUBJECT_STRING_ARGUMENT_INDEX,
1050 PREVIOUS_INDEX_ARGUMENT_INDEX,
1051 LAST_MATCH_INFO_ARGUMENT_INDEX,
1052 REG_EXP_EXEC_ARGUMENT_COUNT
1055 StackArgumentsAccessor args(rsp, REG_EXP_EXEC_ARGUMENT_COUNT,
1056 ARGUMENTS_DONT_CONTAIN_RECEIVER);
1058 // Ensure that a RegExp stack is allocated.
1059 ExternalReference address_of_regexp_stack_memory_address =
1060 ExternalReference::address_of_regexp_stack_memory_address(isolate());
1061 ExternalReference address_of_regexp_stack_memory_size =
1062 ExternalReference::address_of_regexp_stack_memory_size(isolate());
1063 __ Load(kScratchRegister, address_of_regexp_stack_memory_size);
1064 __ testp(kScratchRegister, kScratchRegister);
1065 __ j(zero, &runtime);
1067 // Check that the first argument is a JSRegExp object.
1068 __ movp(rax, args.GetArgumentOperand(JS_REG_EXP_OBJECT_ARGUMENT_INDEX));
1069 __ JumpIfSmi(rax, &runtime);
1070 __ CmpObjectType(rax, JS_REGEXP_TYPE, kScratchRegister);
1071 __ j(not_equal, &runtime);
1073 // Check that the RegExp has been compiled (data contains a fixed array).
1074 __ movp(rax, FieldOperand(rax, JSRegExp::kDataOffset));
1075 if (FLAG_debug_code) {
1076 Condition is_smi = masm->CheckSmi(rax);
1077 __ Check(NegateCondition(is_smi),
1078 kUnexpectedTypeForRegExpDataFixedArrayExpected);
1079 __ CmpObjectType(rax, FIXED_ARRAY_TYPE, kScratchRegister);
1080 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
1083 // rax: RegExp data (FixedArray)
1084 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1085 __ SmiToInteger32(rbx, FieldOperand(rax, JSRegExp::kDataTagOffset));
1086 __ cmpl(rbx, Immediate(JSRegExp::IRREGEXP));
1087 __ j(not_equal, &runtime);
1089 // rax: RegExp data (FixedArray)
1090 // Check that the number of captures fit in the static offsets vector buffer.
1091 __ SmiToInteger32(rdx,
1092 FieldOperand(rax, JSRegExp::kIrregexpCaptureCountOffset));
1093 // Check (number_of_captures + 1) * 2 <= offsets vector size
1094 // Or number_of_captures <= offsets vector size / 2 - 1
1095 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1096 __ cmpl(rdx, Immediate(Isolate::kJSRegexpStaticOffsetsVectorSize / 2 - 1));
1097 __ j(above, &runtime);
1099 // Reset offset for possibly sliced string.
1101 __ movp(rdi, args.GetArgumentOperand(SUBJECT_STRING_ARGUMENT_INDEX));
1102 __ JumpIfSmi(rdi, &runtime);
1103 __ movp(r15, rdi); // Make a copy of the original subject string.
1104 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
1105 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1106 // rax: RegExp data (FixedArray)
1107 // rdi: subject string
1108 // r15: subject string
1109 // Handle subject string according to its encoding and representation:
1110 // (1) Sequential two byte? If yes, go to (9).
1111 // (2) Sequential one byte? If yes, go to (6).
1112 // (3) Anything but sequential or cons? If yes, go to (7).
1113 // (4) Cons string. If the string is flat, replace subject with first string.
1114 // Otherwise bailout.
1115 // (5a) Is subject sequential two byte? If yes, go to (9).
1116 // (5b) Is subject external? If yes, go to (8).
1117 // (6) One byte sequential. Load regexp code for one byte.
1121 // Deferred code at the end of the stub:
1122 // (7) Not a long external string? If yes, go to (10).
1123 // (8) External string. Make it, offset-wise, look like a sequential string.
1124 // (8a) Is the external string one byte? If yes, go to (6).
1125 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1126 // (10) Short external string or not a string? If yes, bail out to runtime.
1127 // (11) Sliced string. Replace subject with parent. Go to (5a).
1129 Label seq_one_byte_string /* 6 */, seq_two_byte_string /* 9 */,
1130 external_string /* 8 */, check_underlying /* 5a */,
1131 not_seq_nor_cons /* 7 */, check_code /* E */,
1132 not_long_external /* 10 */;
1134 // (1) Sequential two byte? If yes, go to (9).
1135 __ andb(rbx, Immediate(kIsNotStringMask |
1136 kStringRepresentationMask |
1137 kStringEncodingMask |
1138 kShortExternalStringMask));
1139 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
1140 __ j(zero, &seq_two_byte_string); // Go to (9).
1142 // (2) Sequential one byte? If yes, go to (6).
1143 // Any other sequential string must be one byte.
1144 __ andb(rbx, Immediate(kIsNotStringMask |
1145 kStringRepresentationMask |
1146 kShortExternalStringMask));
1147 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (6).
1149 // (3) Anything but sequential or cons? If yes, go to (7).
1150 // We check whether the subject string is a cons, since sequential strings
1151 // have already been covered.
1152 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1153 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
1154 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1155 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
1156 __ cmpp(rbx, Immediate(kExternalStringTag));
1157 __ j(greater_equal, ¬_seq_nor_cons); // Go to (7).
1159 // (4) Cons string. Check that it's flat.
1160 // Replace subject with first string and reload instance type.
1161 __ CompareRoot(FieldOperand(rdi, ConsString::kSecondOffset),
1162 Heap::kempty_stringRootIndex);
1163 __ j(not_equal, &runtime);
1164 __ movp(rdi, FieldOperand(rdi, ConsString::kFirstOffset));
1165 __ bind(&check_underlying);
1166 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
1167 __ movp(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1169 // (5a) Is subject sequential two byte? If yes, go to (9).
1170 __ testb(rbx, Immediate(kStringRepresentationMask | kStringEncodingMask));
1171 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
1172 __ j(zero, &seq_two_byte_string); // Go to (9).
1173 // (5b) Is subject external? If yes, go to (8).
1174 __ testb(rbx, Immediate(kStringRepresentationMask));
1175 // The underlying external string is never a short external string.
1176 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
1177 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
1178 __ j(not_zero, &external_string); // Go to (8)
1180 // (6) One byte sequential. Load regexp code for one byte.
1181 __ bind(&seq_one_byte_string);
1182 // rax: RegExp data (FixedArray)
1183 __ movp(r11, FieldOperand(rax, JSRegExp::kDataOneByteCodeOffset));
1184 __ Set(rcx, 1); // Type is one byte.
1186 // (E) Carry on. String handling is done.
1187 __ bind(&check_code);
1188 // r11: irregexp code
1189 // Check that the irregexp code has been generated for the actual string
1190 // encoding. If it has, the field contains a code object otherwise it contains
1191 // smi (code flushing support)
1192 __ JumpIfSmi(r11, &runtime);
1194 // rdi: sequential subject string (or look-alike, external string)
1195 // r15: original subject string
1196 // rcx: encoding of subject string (1 if one_byte, 0 if two_byte);
1198 // Load used arguments before starting to push arguments for call to native
1199 // RegExp code to avoid handling changing stack height.
1200 // We have to use r15 instead of rdi to load the length because rdi might
1201 // have been only made to look like a sequential string when it actually
1202 // is an external string.
1203 __ movp(rbx, args.GetArgumentOperand(PREVIOUS_INDEX_ARGUMENT_INDEX));
1204 __ JumpIfNotSmi(rbx, &runtime);
1205 __ SmiCompare(rbx, FieldOperand(r15, String::kLengthOffset));
1206 __ j(above_equal, &runtime);
1207 __ SmiToInteger64(rbx, rbx);
1209 // rdi: subject string
1210 // rbx: previous index
1211 // rcx: encoding of subject string (1 if one_byte 0 if two_byte);
1213 // All checks done. Now push arguments for native regexp code.
1214 Counters* counters = isolate()->counters();
1215 __ IncrementCounter(counters->regexp_entry_native(), 1);
1217 // Isolates: note we add an additional parameter here (isolate pointer).
1218 static const int kRegExpExecuteArguments = 9;
1219 int argument_slots_on_stack =
1220 masm->ArgumentStackSlotsForCFunctionCall(kRegExpExecuteArguments);
1221 __ EnterApiExitFrame(argument_slots_on_stack);
1223 // Argument 9: Pass current isolate address.
1224 __ LoadAddress(kScratchRegister,
1225 ExternalReference::isolate_address(isolate()));
1226 __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kRegisterSize),
1229 // Argument 8: Indicate that this is a direct call from JavaScript.
1230 __ movq(Operand(rsp, (argument_slots_on_stack - 2) * kRegisterSize),
1233 // Argument 7: Start (high end) of backtracking stack memory area.
1234 __ Move(kScratchRegister, address_of_regexp_stack_memory_address);
1235 __ movp(r9, Operand(kScratchRegister, 0));
1236 __ Move(kScratchRegister, address_of_regexp_stack_memory_size);
1237 __ addp(r9, Operand(kScratchRegister, 0));
1238 __ movq(Operand(rsp, (argument_slots_on_stack - 3) * kRegisterSize), r9);
1240 // Argument 6: Set the number of capture registers to zero to force global
1241 // regexps to behave as non-global. This does not affect non-global regexps.
1242 // Argument 6 is passed in r9 on Linux and on the stack on Windows.
1244 __ movq(Operand(rsp, (argument_slots_on_stack - 4) * kRegisterSize),
1250 // Argument 5: static offsets vector buffer.
1252 r8, ExternalReference::address_of_static_offsets_vector(isolate()));
1253 // Argument 5 passed in r8 on Linux and on the stack on Windows.
1255 __ movq(Operand(rsp, (argument_slots_on_stack - 5) * kRegisterSize), r8);
1258 // rdi: subject string
1259 // rbx: previous index
1260 // rcx: encoding of subject string (1 if one_byte 0 if two_byte);
1262 // r14: slice offset
1263 // r15: original subject string
1265 // Argument 2: Previous index.
1266 __ movp(arg_reg_2, rbx);
1268 // Argument 4: End of string data
1269 // Argument 3: Start of string data
1270 Label setup_two_byte, setup_rest, got_length, length_not_from_slice;
1271 // Prepare start and end index of the input.
1272 // Load the length from the original sliced string if that is the case.
1274 __ SmiToInteger32(arg_reg_3, FieldOperand(r15, String::kLengthOffset));
1275 __ addp(r14, arg_reg_3); // Using arg3 as scratch.
1277 // rbx: start index of the input
1278 // r14: end index of the input
1279 // r15: original subject string
1280 __ testb(rcx, rcx); // Last use of rcx as encoding of subject string.
1281 __ j(zero, &setup_two_byte, Label::kNear);
1283 FieldOperand(rdi, r14, times_1, SeqOneByteString::kHeaderSize));
1285 FieldOperand(rdi, rbx, times_1, SeqOneByteString::kHeaderSize));
1286 __ jmp(&setup_rest, Label::kNear);
1287 __ bind(&setup_two_byte);
1289 FieldOperand(rdi, r14, times_2, SeqTwoByteString::kHeaderSize));
1291 FieldOperand(rdi, rbx, times_2, SeqTwoByteString::kHeaderSize));
1292 __ bind(&setup_rest);
1294 // Argument 1: Original subject string.
1295 // The original subject is in the previous stack frame. Therefore we have to
1296 // use rbp, which points exactly to one pointer size below the previous rsp.
1297 // (Because creating a new stack frame pushes the previous rbp onto the stack
1298 // and thereby moves up rsp by one kPointerSize.)
1299 __ movp(arg_reg_1, r15);
1301 // Locate the code entry and call it.
1302 __ addp(r11, Immediate(Code::kHeaderSize - kHeapObjectTag));
1305 __ LeaveApiExitFrame(true);
1307 // Check the result.
1310 __ cmpl(rax, Immediate(1));
1311 // We expect exactly one result since we force the called regexp to behave
1313 __ j(equal, &success, Label::kNear);
1314 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::EXCEPTION));
1315 __ j(equal, &exception);
1316 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::FAILURE));
1317 // If none of the above, it can only be retry.
1318 // Handle that in the runtime system.
1319 __ j(not_equal, &runtime);
1321 // For failure return null.
1322 __ LoadRoot(rax, Heap::kNullValueRootIndex);
1323 __ ret(REG_EXP_EXEC_ARGUMENT_COUNT * kPointerSize);
1325 // Load RegExp data.
1327 __ movp(rax, args.GetArgumentOperand(JS_REG_EXP_OBJECT_ARGUMENT_INDEX));
1328 __ movp(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
1329 __ SmiToInteger32(rax,
1330 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
1331 // Calculate number of capture registers (number_of_captures + 1) * 2.
1332 __ leal(rdx, Operand(rax, rax, times_1, 2));
1334 // rdx: Number of capture registers
1335 // Check that the fourth object is a JSArray object.
1336 __ movp(r15, args.GetArgumentOperand(LAST_MATCH_INFO_ARGUMENT_INDEX));
1337 __ JumpIfSmi(r15, &runtime);
1338 __ CmpObjectType(r15, JS_ARRAY_TYPE, kScratchRegister);
1339 __ j(not_equal, &runtime);
1340 // Check that the JSArray is in fast case.
1341 __ movp(rbx, FieldOperand(r15, JSArray::kElementsOffset));
1342 __ movp(rax, FieldOperand(rbx, HeapObject::kMapOffset));
1343 __ CompareRoot(rax, Heap::kFixedArrayMapRootIndex);
1344 __ j(not_equal, &runtime);
1345 // Check that the last match info has space for the capture registers and the
1346 // additional information. Ensure no overflow in add.
1347 STATIC_ASSERT(FixedArray::kMaxLength < kMaxInt - FixedArray::kLengthOffset);
1348 __ SmiToInteger32(rax, FieldOperand(rbx, FixedArray::kLengthOffset));
1349 __ subl(rax, Immediate(RegExpImpl::kLastMatchOverhead));
1351 __ j(greater, &runtime);
1353 // rbx: last_match_info backing store (FixedArray)
1354 // rdx: number of capture registers
1355 // Store the capture count.
1356 __ Integer32ToSmi(kScratchRegister, rdx);
1357 __ movp(FieldOperand(rbx, RegExpImpl::kLastCaptureCountOffset),
1359 // Store last subject and last input.
1360 __ movp(rax, args.GetArgumentOperand(SUBJECT_STRING_ARGUMENT_INDEX));
1361 __ movp(FieldOperand(rbx, RegExpImpl::kLastSubjectOffset), rax);
1363 __ RecordWriteField(rbx,
1364 RegExpImpl::kLastSubjectOffset,
1369 __ movp(FieldOperand(rbx, RegExpImpl::kLastInputOffset), rax);
1370 __ RecordWriteField(rbx,
1371 RegExpImpl::kLastInputOffset,
1376 // Get the static offsets vector filled by the native regexp code.
1378 rcx, ExternalReference::address_of_static_offsets_vector(isolate()));
1380 // rbx: last_match_info backing store (FixedArray)
1381 // rcx: offsets vector
1382 // rdx: number of capture registers
1383 Label next_capture, done;
1384 // Capture register counter starts from number of capture registers and
1385 // counts down until wraping after zero.
1386 __ bind(&next_capture);
1387 __ subp(rdx, Immediate(1));
1388 __ j(negative, &done, Label::kNear);
1389 // Read the value from the static offsets vector buffer and make it a smi.
1390 __ movl(rdi, Operand(rcx, rdx, times_int_size, 0));
1391 __ Integer32ToSmi(rdi, rdi);
1392 // Store the smi value in the last match info.
1393 __ movp(FieldOperand(rbx,
1396 RegExpImpl::kFirstCaptureOffset),
1398 __ jmp(&next_capture);
1401 // Return last match info.
1403 __ ret(REG_EXP_EXEC_ARGUMENT_COUNT * kPointerSize);
1405 __ bind(&exception);
1406 // Result must now be exception. If there is no pending exception already a
1407 // stack overflow (on the backtrack stack) was detected in RegExp code but
1408 // haven't created the exception yet. Handle that in the runtime system.
1409 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
1410 ExternalReference pending_exception_address(
1411 Isolate::kPendingExceptionAddress, isolate());
1412 Operand pending_exception_operand =
1413 masm->ExternalOperand(pending_exception_address, rbx);
1414 __ movp(rax, pending_exception_operand);
1415 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
1417 __ j(equal, &runtime);
1419 // For exception, throw the exception again.
1420 __ TailCallRuntime(Runtime::kRegExpExecReThrow, 4, 1);
1422 // Do the runtime call to execute the regexp.
1424 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
1426 // Deferred code for string handling.
1427 // (7) Not a long external string? If yes, go to (10).
1428 __ bind(¬_seq_nor_cons);
1429 // Compare flags are still set from (3).
1430 __ j(greater, ¬_long_external, Label::kNear); // Go to (10).
1432 // (8) External string. Short external strings have been ruled out.
1433 __ bind(&external_string);
1434 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
1435 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1436 if (FLAG_debug_code) {
1437 // Assert that we do not have a cons or slice (indirect strings) here.
1438 // Sequential strings have already been ruled out.
1439 __ testb(rbx, Immediate(kIsIndirectStringMask));
1440 __ Assert(zero, kExternalStringExpectedButNotFound);
1442 __ movp(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
1443 // Move the pointer so that offset-wise, it looks like a sequential string.
1444 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
1445 __ subp(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
1446 STATIC_ASSERT(kTwoByteStringTag == 0);
1447 // (8a) Is the external string one byte? If yes, go to (6).
1448 __ testb(rbx, Immediate(kStringEncodingMask));
1449 __ j(not_zero, &seq_one_byte_string); // Goto (6).
1451 // rdi: subject string (flat two-byte)
1452 // rax: RegExp data (FixedArray)
1453 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1454 __ bind(&seq_two_byte_string);
1455 __ movp(r11, FieldOperand(rax, JSRegExp::kDataUC16CodeOffset));
1456 __ Set(rcx, 0); // Type is two byte.
1457 __ jmp(&check_code); // Go to (E).
1459 // (10) Not a string or a short external string? If yes, bail out to runtime.
1460 __ bind(¬_long_external);
1461 // Catch non-string subject or short external string.
1462 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1463 __ testb(rbx, Immediate(kIsNotStringMask | kShortExternalStringMask));
1464 __ j(not_zero, &runtime);
1466 // (11) Sliced string. Replace subject with parent. Go to (5a).
1467 // Load offset into r14 and replace subject string with parent.
1468 __ SmiToInteger32(r14, FieldOperand(rdi, SlicedString::kOffsetOffset));
1469 __ movp(rdi, FieldOperand(rdi, SlicedString::kParentOffset));
1470 __ jmp(&check_underlying);
1471 #endif // V8_INTERPRETED_REGEXP
1475 static int NegativeComparisonResult(Condition cc) {
1476 DCHECK(cc != equal);
1477 DCHECK((cc == less) || (cc == less_equal)
1478 || (cc == greater) || (cc == greater_equal));
1479 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
1483 static void CheckInputType(MacroAssembler* masm, Register input,
1484 CompareICState::State expected, Label* fail) {
1486 if (expected == CompareICState::SMI) {
1487 __ JumpIfNotSmi(input, fail);
1488 } else if (expected == CompareICState::NUMBER) {
1489 __ JumpIfSmi(input, &ok);
1490 __ CompareMap(input, masm->isolate()->factory()->heap_number_map());
1491 __ j(not_equal, fail);
1493 // We could be strict about internalized/non-internalized here, but as long as
1494 // hydrogen doesn't care, the stub doesn't have to care either.
1499 static void BranchIfNotInternalizedString(MacroAssembler* masm,
1503 __ JumpIfSmi(object, label);
1504 __ movp(scratch, FieldOperand(object, HeapObject::kMapOffset));
1506 FieldOperand(scratch, Map::kInstanceTypeOffset));
1507 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
1508 __ testb(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
1509 __ j(not_zero, label);
1513 void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
1514 Label runtime_call, check_unequal_objects, done;
1515 Condition cc = GetCondition();
1516 Factory* factory = isolate()->factory();
1519 CheckInputType(masm, rdx, left(), &miss);
1520 CheckInputType(masm, rax, right(), &miss);
1522 // Compare two smis.
1523 Label non_smi, smi_done;
1524 __ JumpIfNotBothSmi(rax, rdx, &non_smi);
1526 __ j(no_overflow, &smi_done);
1527 __ notp(rdx); // Correct sign in case of overflow. rdx cannot be 0 here.
1533 // The compare stub returns a positive, negative, or zero 64-bit integer
1534 // value in rax, corresponding to result of comparing the two inputs.
1535 // NOTICE! This code is only reached after a smi-fast-case check, so
1536 // it is certain that at least one operand isn't a smi.
1538 // Two identical objects are equal unless they are both NaN or undefined.
1540 Label not_identical;
1542 __ j(not_equal, ¬_identical, Label::kNear);
1545 // Check for undefined. undefined OP undefined is false even though
1546 // undefined == undefined.
1547 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
1548 if (is_strong(strength())) {
1549 // In strong mode, this comparison must throw, so call the runtime.
1550 __ j(equal, &runtime_call, Label::kFar);
1552 Label check_for_nan;
1553 __ j(not_equal, &check_for_nan, Label::kNear);
1554 __ Set(rax, NegativeComparisonResult(cc));
1556 __ bind(&check_for_nan);
1560 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
1561 // so we do the second best thing - test it ourselves.
1563 // If it's not a heap number, then return equal for (in)equality operator.
1564 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset),
1565 factory->heap_number_map());
1566 __ j(equal, &heap_number, Label::kNear);
1568 __ movp(rcx, FieldOperand(rax, HeapObject::kMapOffset));
1569 __ movzxbl(rcx, FieldOperand(rcx, Map::kInstanceTypeOffset));
1570 // Call runtime on identical objects. Otherwise return equal.
1571 __ cmpb(rcx, Immediate(static_cast<uint8_t>(FIRST_SPEC_OBJECT_TYPE)));
1572 __ j(above_equal, &runtime_call, Label::kFar);
1573 // Call runtime on identical symbols since we need to throw a TypeError.
1574 __ cmpb(rcx, Immediate(static_cast<uint8_t>(SYMBOL_TYPE)));
1575 __ j(equal, &runtime_call, Label::kFar);
1576 // Call runtime on identical SIMD values since we must throw a TypeError.
1577 __ cmpb(rcx, Immediate(static_cast<uint8_t>(FLOAT32X4_TYPE)));
1578 __ j(equal, &runtime_call, Label::kFar);
1579 if (is_strong(strength())) {
1580 // We have already tested for smis and heap numbers, so if both
1581 // arguments are not strings we must proceed to the slow case.
1582 __ testb(rcx, Immediate(kIsNotStringMask));
1583 __ j(not_zero, &runtime_call, Label::kFar);
1589 __ bind(&heap_number);
1590 // It is a heap number, so return equal if it's not NaN.
1591 // For NaN, return 1 for every condition except greater and
1592 // greater-equal. Return -1 for them, so the comparison yields
1593 // false for all conditions except not-equal.
1595 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1596 __ ucomisd(xmm0, xmm0);
1597 __ setcc(parity_even, rax);
1598 // rax is 0 for equal non-NaN heapnumbers, 1 for NaNs.
1599 if (cc == greater_equal || cc == greater) {
1604 __ bind(¬_identical);
1607 if (cc == equal) { // Both strict and non-strict.
1608 Label slow; // Fallthrough label.
1610 // If we're doing a strict equality comparison, we don't have to do
1611 // type conversion, so we generate code to do fast comparison for objects
1612 // and oddballs. Non-smi numbers and strings still go through the usual
1615 // If either is a Smi (we know that not both are), then they can only
1616 // be equal if the other is a HeapNumber. If so, use the slow case.
1619 __ SelectNonSmi(rbx, rax, rdx, ¬_smis);
1621 // Check if the non-smi operand is a heap number.
1622 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
1623 factory->heap_number_map());
1624 // If heap number, handle it in the slow case.
1626 // Return non-equal. ebx (the lower half of rbx) is not zero.
1633 // If either operand is a JSObject or an oddball value, then they are not
1634 // equal since their pointers are different
1635 // There is no test for undetectability in strict equality.
1637 // If the first object is a JS object, we have done pointer comparison.
1638 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
1639 Label first_non_object;
1640 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
1641 __ j(below, &first_non_object, Label::kNear);
1642 // Return non-zero (rax (not rax) is not zero)
1643 Label return_not_equal;
1644 STATIC_ASSERT(kHeapObjectTag != 0);
1645 __ bind(&return_not_equal);
1648 __ bind(&first_non_object);
1649 // Check for oddballs: true, false, null, undefined.
1650 __ CmpInstanceType(rcx, ODDBALL_TYPE);
1651 __ j(equal, &return_not_equal);
1653 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
1654 __ j(above_equal, &return_not_equal);
1656 // Check for oddballs: true, false, null, undefined.
1657 __ CmpInstanceType(rcx, ODDBALL_TYPE);
1658 __ j(equal, &return_not_equal);
1660 // Fall through to the general case.
1665 // Generate the number comparison code.
1666 Label non_number_comparison;
1668 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &non_number_comparison);
1671 __ ucomisd(xmm0, xmm1);
1673 // Don't base result on EFLAGS when a NaN is involved.
1674 __ j(parity_even, &unordered, Label::kNear);
1675 // Return a result of -1, 0, or 1, based on EFLAGS.
1676 __ setcc(above, rax);
1677 __ setcc(below, rcx);
1681 // If one of the numbers was NaN, then the result is always false.
1682 // The cc is never not-equal.
1683 __ bind(&unordered);
1684 DCHECK(cc != not_equal);
1685 if (cc == less || cc == less_equal) {
1692 // The number comparison code did not provide a valid result.
1693 __ bind(&non_number_comparison);
1695 // Fast negative check for internalized-to-internalized equality.
1696 Label check_for_strings;
1698 BranchIfNotInternalizedString(
1699 masm, &check_for_strings, rax, kScratchRegister);
1700 BranchIfNotInternalizedString(
1701 masm, &check_for_strings, rdx, kScratchRegister);
1703 // We've already checked for object identity, so if both operands are
1704 // internalized strings they aren't equal. Register rax (not rax) already
1705 // holds a non-zero value, which indicates not equal, so just return.
1709 __ bind(&check_for_strings);
1711 __ JumpIfNotBothSequentialOneByteStrings(rdx, rax, rcx, rbx,
1712 &check_unequal_objects);
1714 // Inline comparison of one-byte strings.
1716 StringHelper::GenerateFlatOneByteStringEquals(masm, rdx, rax, rcx, rbx);
1718 StringHelper::GenerateCompareFlatOneByteStrings(masm, rdx, rax, rcx, rbx,
1723 __ Abort(kUnexpectedFallThroughFromStringComparison);
1726 __ bind(&check_unequal_objects);
1727 if (cc == equal && !strict()) {
1728 // Not strict equality. Objects are unequal if
1729 // they are both JSObjects and not undetectable,
1730 // and their pointers are different.
1731 Label return_unequal;
1732 // At most one is a smi, so we can test for smi by adding the two.
1733 // A smi plus a heap object has the low bit set, a heap object plus
1734 // a heap object has the low bit clear.
1735 STATIC_ASSERT(kSmiTag == 0);
1736 STATIC_ASSERT(kSmiTagMask == 1);
1737 __ leap(rcx, Operand(rax, rdx, times_1, 0));
1738 __ testb(rcx, Immediate(kSmiTagMask));
1739 __ j(not_zero, &runtime_call, Label::kNear);
1740 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rbx);
1741 __ j(below, &runtime_call, Label::kNear);
1742 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
1743 __ j(below, &runtime_call, Label::kNear);
1744 __ testb(FieldOperand(rbx, Map::kBitFieldOffset),
1745 Immediate(1 << Map::kIsUndetectable));
1746 __ j(zero, &return_unequal, Label::kNear);
1747 __ testb(FieldOperand(rcx, Map::kBitFieldOffset),
1748 Immediate(1 << Map::kIsUndetectable));
1749 __ j(zero, &return_unequal, Label::kNear);
1750 // The objects are both undetectable, so they both compare as the value
1751 // undefined, and are equal.
1753 __ bind(&return_unequal);
1754 // Return non-equal by returning the non-zero object pointer in rax,
1755 // or return equal if we fell through to here.
1758 __ bind(&runtime_call);
1760 // Push arguments below the return address to prepare jump to builtin.
1761 __ PopReturnAddressTo(rcx);
1765 // Figure out which native to call and setup the arguments.
1766 Builtins::JavaScript builtin;
1768 builtin = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1771 is_strong(strength()) ? Builtins::COMPARE_STRONG : Builtins::COMPARE;
1772 __ Push(Smi::FromInt(NegativeComparisonResult(cc)));
1775 __ PushReturnAddressFrom(rcx);
1777 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1778 // tagged as a small integer.
1779 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
1786 static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub,
1788 // rax : number of arguments to the construct function
1789 // rbx : feedback vector
1790 // rcx : original constructor (for IsSuperConstructorCall)
1791 // rdx : slot in feedback vector (Smi)
1792 // rdi : the function to call
1793 FrameScope scope(masm, StackFrame::INTERNAL);
1795 // Number-of-arguments register must be smi-tagged to call out.
1796 __ Integer32ToSmi(rax, rax);
1799 __ Integer32ToSmi(rdx, rdx);
1815 __ SmiToInteger32(rax, rax);
1819 static void GenerateRecordCallTarget(MacroAssembler* masm, bool is_super) {
1820 // Cache the called function in a feedback vector slot. Cache states
1821 // are uninitialized, monomorphic (indicated by a JSFunction), and
1823 // rax : number of arguments to the construct function
1824 // rbx : feedback vector
1825 // rcx : original constructor (for IsSuperConstructorCall)
1826 // rdx : slot in feedback vector (Smi)
1827 // rdi : the function to call
1828 Isolate* isolate = masm->isolate();
1829 Label initialize, done, miss, megamorphic, not_array_function,
1830 done_no_smi_convert;
1832 // Load the cache state into r11.
1833 __ SmiToInteger32(rdx, rdx);
1835 FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize));
1837 // A monomorphic cache hit or an already megamorphic state: invoke the
1838 // function without changing the state.
1839 // We don't know if r11 is a WeakCell or a Symbol, but it's harmless to read
1840 // at this position in a symbol (see static asserts in
1841 // type-feedback-vector.h).
1842 Label check_allocation_site;
1843 __ cmpp(rdi, FieldOperand(r11, WeakCell::kValueOffset));
1844 __ j(equal, &done, Label::kFar);
1845 __ CompareRoot(r11, Heap::kmegamorphic_symbolRootIndex);
1846 __ j(equal, &done, Label::kFar);
1847 __ CompareRoot(FieldOperand(r11, HeapObject::kMapOffset),
1848 Heap::kWeakCellMapRootIndex);
1849 __ j(not_equal, FLAG_pretenuring_call_new ? &miss : &check_allocation_site);
1851 // If the weak cell is cleared, we have a new chance to become monomorphic.
1852 __ CheckSmi(FieldOperand(r11, WeakCell::kValueOffset));
1853 __ j(equal, &initialize);
1854 __ jmp(&megamorphic);
1856 if (!FLAG_pretenuring_call_new) {
1857 __ bind(&check_allocation_site);
1858 // If we came here, we need to see if we are the array function.
1859 // If we didn't have a matching function, and we didn't find the megamorph
1860 // sentinel, then we have in the slot either some other function or an
1862 __ CompareRoot(FieldOperand(r11, 0), Heap::kAllocationSiteMapRootIndex);
1863 __ j(not_equal, &miss);
1865 // Make sure the function is the Array() function
1866 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, r11);
1868 __ j(not_equal, &megamorphic);
1874 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1876 __ CompareRoot(r11, Heap::kuninitialized_symbolRootIndex);
1877 __ j(equal, &initialize);
1878 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1879 // write-barrier is needed.
1880 __ bind(&megamorphic);
1881 __ Move(FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize),
1882 TypeFeedbackVector::MegamorphicSentinel(isolate));
1885 // An uninitialized cache is patched with the function or sentinel to
1886 // indicate the ElementsKind if function is the Array constructor.
1887 __ bind(&initialize);
1889 if (!FLAG_pretenuring_call_new) {
1890 // Make sure the function is the Array() function
1891 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, r11);
1893 __ j(not_equal, ¬_array_function);
1895 CreateAllocationSiteStub create_stub(isolate);
1896 CallStubInRecordCallTarget(masm, &create_stub, is_super);
1897 __ jmp(&done_no_smi_convert);
1899 __ bind(¬_array_function);
1902 CreateWeakCellStub create_stub(isolate);
1903 CallStubInRecordCallTarget(masm, &create_stub, is_super);
1904 __ jmp(&done_no_smi_convert);
1907 __ Integer32ToSmi(rdx, rdx);
1909 __ bind(&done_no_smi_convert);
1913 static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
1914 // Do not transform the receiver for strict mode functions.
1915 __ movp(rcx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1916 __ testb(FieldOperand(rcx, SharedFunctionInfo::kStrictModeByteOffset),
1917 Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
1918 __ j(not_equal, cont);
1920 // Do not transform the receiver for natives.
1921 // SharedFunctionInfo is already loaded into rcx.
1922 __ testb(FieldOperand(rcx, SharedFunctionInfo::kNativeByteOffset),
1923 Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
1924 __ j(not_equal, cont);
1928 static void EmitSlowCase(Isolate* isolate,
1929 MacroAssembler* masm,
1930 StackArgumentsAccessor* args,
1932 Label* non_function) {
1933 // Check for function proxy.
1934 __ CmpInstanceType(rcx, JS_FUNCTION_PROXY_TYPE);
1935 __ j(not_equal, non_function);
1936 __ PopReturnAddressTo(rcx);
1937 __ Push(rdi); // put proxy as additional argument under return address
1938 __ PushReturnAddressFrom(rcx);
1939 __ Set(rax, argc + 1);
1941 __ GetBuiltinEntry(rdx, Builtins::CALL_FUNCTION_PROXY);
1943 Handle<Code> adaptor =
1944 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
1945 __ jmp(adaptor, RelocInfo::CODE_TARGET);
1948 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
1949 // of the original receiver from the call site).
1950 __ bind(non_function);
1951 __ movp(args->GetReceiverOperand(), rdi);
1954 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
1955 Handle<Code> adaptor =
1956 isolate->builtins()->ArgumentsAdaptorTrampoline();
1957 __ Jump(adaptor, RelocInfo::CODE_TARGET);
1961 static void EmitWrapCase(MacroAssembler* masm,
1962 StackArgumentsAccessor* args,
1964 // Wrap the receiver and patch it back onto the stack.
1965 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
1968 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1971 __ movp(args->GetReceiverOperand(), rax);
1976 static void CallFunctionNoFeedback(MacroAssembler* masm,
1977 int argc, bool needs_checks,
1978 bool call_as_method) {
1979 // rdi : the function to call
1981 // wrap_and_call can only be true if we are compiling a monomorphic method.
1982 Isolate* isolate = masm->isolate();
1983 Label slow, non_function, wrap, cont;
1984 StackArgumentsAccessor args(rsp, argc);
1987 // Check that the function really is a JavaScript function.
1988 __ JumpIfSmi(rdi, &non_function);
1990 // Goto slow case if we do not have a function.
1991 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
1992 __ j(not_equal, &slow);
1995 // Fast-case: Just invoke the function.
1996 ParameterCount actual(argc);
1998 if (call_as_method) {
2000 EmitContinueIfStrictOrNative(masm, &cont);
2003 // Load the receiver from the stack.
2004 __ movp(rax, args.GetReceiverOperand());
2007 __ JumpIfSmi(rax, &wrap);
2009 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
2018 __ InvokeFunction(rdi, actual, JUMP_FUNCTION, NullCallWrapper());
2021 // Slow-case: Non-function called.
2023 EmitSlowCase(isolate, masm, &args, argc, &non_function);
2026 if (call_as_method) {
2028 EmitWrapCase(masm, &args, &cont);
2033 void CallFunctionStub::Generate(MacroAssembler* masm) {
2034 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
2038 void CallConstructStub::Generate(MacroAssembler* masm) {
2039 // rax : number of arguments
2040 // rbx : feedback vector
2041 // rcx : original constructor (for IsSuperConstructorCall)
2042 // rdx : slot in feedback vector (Smi, for RecordCallTarget)
2043 // rdi : constructor function
2044 Label slow, non_function_call;
2046 // Check that function is not a smi.
2047 __ JumpIfSmi(rdi, &non_function_call);
2048 // Check that function is a JSFunction.
2049 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, r11);
2050 __ j(not_equal, &slow);
2052 if (RecordCallTarget()) {
2053 GenerateRecordCallTarget(masm, 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 Runtime::FunctionId id = GetICState() == DEFAULT
2334 ? Runtime::kCallIC_Miss
2335 : Runtime::kCallIC_Customization_Miss;
2336 __ CallRuntime(id, 3);
2338 // Move result to edi and exit the internal frame.
2343 bool CEntryStub::NeedsImmovableCode() {
2348 void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
2349 CEntryStub::GenerateAheadOfTime(isolate);
2350 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
2351 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
2352 // It is important that the store buffer overflow stubs are generated first.
2353 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
2354 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
2355 CreateWeakCellStub::GenerateAheadOfTime(isolate);
2356 BinaryOpICStub::GenerateAheadOfTime(isolate);
2357 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
2358 StoreFastElementStub::GenerateAheadOfTime(isolate);
2359 TypeofStub::GenerateAheadOfTime(isolate);
2363 void CodeStub::GenerateFPStubs(Isolate* isolate) {
2367 void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
2368 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
2370 CEntryStub save_doubles(isolate, 1, kSaveFPRegs);
2371 save_doubles.GetCode();
2375 void CEntryStub::Generate(MacroAssembler* masm) {
2376 // rax: number of arguments including receiver
2377 // rbx: pointer to C function (C callee-saved)
2378 // rbp: frame pointer of calling JS frame (restored after C call)
2379 // rsp: stack pointer (restored after C call)
2380 // rsi: current context (restored)
2382 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2384 // Enter the exit frame that transitions from JavaScript to C++.
2386 int arg_stack_space = (result_size() < 2 ? 2 : 4);
2388 int arg_stack_space = 0;
2390 __ EnterExitFrame(arg_stack_space, save_doubles());
2392 // rbx: pointer to builtin function (C callee-saved).
2393 // rbp: frame pointer of exit frame (restored after C call).
2394 // rsp: stack pointer (restored after C call).
2395 // r14: number of arguments including receiver (C callee-saved).
2396 // r15: argv pointer (C callee-saved).
2398 // Simple results returned in rax (both AMD64 and Win64 calling conventions).
2399 // Complex results must be written to address passed as first argument.
2400 // AMD64 calling convention: a struct of two pointers in rax+rdx
2402 // Check stack alignment.
2403 if (FLAG_debug_code) {
2404 __ CheckStackAlignment();
2409 // Windows 64-bit ABI passes arguments in rcx, rdx, r8, r9.
2410 // Pass argv and argc as two parameters. The arguments object will
2411 // be created by stubs declared by DECLARE_RUNTIME_FUNCTION().
2412 if (result_size() < 2) {
2413 // Pass a pointer to the Arguments object as the first argument.
2414 // Return result in single register (rax).
2415 __ movp(rcx, r14); // argc.
2416 __ movp(rdx, r15); // argv.
2417 __ Move(r8, ExternalReference::isolate_address(isolate()));
2419 DCHECK_EQ(2, result_size());
2420 // Pass a pointer to the result location as the first argument.
2421 __ leap(rcx, StackSpaceOperand(2));
2422 // Pass a pointer to the Arguments object as the second argument.
2423 __ movp(rdx, r14); // argc.
2424 __ movp(r8, r15); // argv.
2425 __ Move(r9, ExternalReference::isolate_address(isolate()));
2429 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9.
2430 __ movp(rdi, r14); // argc.
2431 __ movp(rsi, r15); // argv.
2432 __ Move(rdx, ExternalReference::isolate_address(isolate()));
2435 // Result is in rax - do not destroy this register!
2438 // If return value is on the stack, pop it to registers.
2439 if (result_size() > 1) {
2440 DCHECK_EQ(2, result_size());
2441 // Read result values stored on stack. Result is stored
2442 // above the four argument mirror slots and the two
2443 // Arguments object slots.
2444 __ movq(rax, Operand(rsp, 6 * kRegisterSize));
2445 __ movq(rdx, Operand(rsp, 7 * kRegisterSize));
2449 // Check result for exception sentinel.
2450 Label exception_returned;
2451 __ CompareRoot(rax, Heap::kExceptionRootIndex);
2452 __ j(equal, &exception_returned);
2454 // Check that there is no pending exception, otherwise we
2455 // should have returned the exception sentinel.
2456 if (FLAG_debug_code) {
2458 __ LoadRoot(r14, Heap::kTheHoleValueRootIndex);
2459 ExternalReference pending_exception_address(
2460 Isolate::kPendingExceptionAddress, isolate());
2461 Operand pending_exception_operand =
2462 masm->ExternalOperand(pending_exception_address);
2463 __ cmpp(r14, pending_exception_operand);
2464 __ j(equal, &okay, Label::kNear);
2469 // Exit the JavaScript to C++ exit frame.
2470 __ LeaveExitFrame(save_doubles());
2473 // Handling of exception.
2474 __ bind(&exception_returned);
2476 ExternalReference pending_handler_context_address(
2477 Isolate::kPendingHandlerContextAddress, isolate());
2478 ExternalReference pending_handler_code_address(
2479 Isolate::kPendingHandlerCodeAddress, isolate());
2480 ExternalReference pending_handler_offset_address(
2481 Isolate::kPendingHandlerOffsetAddress, isolate());
2482 ExternalReference pending_handler_fp_address(
2483 Isolate::kPendingHandlerFPAddress, isolate());
2484 ExternalReference pending_handler_sp_address(
2485 Isolate::kPendingHandlerSPAddress, isolate());
2487 // Ask the runtime for help to determine the handler. This will set rax to
2488 // contain the current pending exception, don't clobber it.
2489 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
2492 FrameScope scope(masm, StackFrame::MANUAL);
2493 __ movp(arg_reg_1, Immediate(0)); // argc.
2494 __ movp(arg_reg_2, Immediate(0)); // argv.
2495 __ Move(arg_reg_3, ExternalReference::isolate_address(isolate()));
2496 __ PrepareCallCFunction(3);
2497 __ CallCFunction(find_handler, 3);
2500 // Retrieve the handler context, SP and FP.
2501 __ movp(rsi, masm->ExternalOperand(pending_handler_context_address));
2502 __ movp(rsp, masm->ExternalOperand(pending_handler_sp_address));
2503 __ movp(rbp, masm->ExternalOperand(pending_handler_fp_address));
2505 // If the handler is a JS frame, restore the context to the frame. Note that
2506 // the context will be set to (rsi == 0) for non-JS frames.
2509 __ j(zero, &skip, Label::kNear);
2510 __ movp(Operand(rbp, StandardFrameConstants::kContextOffset), rsi);
2513 // Compute the handler entry address and jump to it.
2514 __ movp(rdi, masm->ExternalOperand(pending_handler_code_address));
2515 __ movp(rdx, masm->ExternalOperand(pending_handler_offset_address));
2516 __ leap(rdi, FieldOperand(rdi, rdx, times_1, Code::kHeaderSize));
2521 void JSEntryStub::Generate(MacroAssembler* masm) {
2522 Label invoke, handler_entry, exit;
2523 Label not_outermost_js, not_outermost_js_2;
2525 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2527 { // NOLINT. Scope block confuses linter.
2528 MacroAssembler::NoRootArrayScope uninitialized_root_register(masm);
2533 // Push the stack frame type marker twice.
2534 int marker = type();
2535 // Scratch register is neither callee-save, nor an argument register on any
2536 // platform. It's free to use at this point.
2537 // Cannot use smi-register for loading yet.
2538 __ Move(kScratchRegister, Smi::FromInt(marker), Assembler::RelocInfoNone());
2539 __ Push(kScratchRegister); // context slot
2540 __ Push(kScratchRegister); // function slot
2541 // Save callee-saved registers (X64/X32/Win64 calling conventions).
2547 __ pushq(rdi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
2548 __ pushq(rsi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
2553 // On Win64 XMM6-XMM15 are callee-save
2554 __ subp(rsp, Immediate(EntryFrameConstants::kXMMRegistersBlockSize));
2555 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 0), xmm6);
2556 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 1), xmm7);
2557 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 2), xmm8);
2558 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 3), xmm9);
2559 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 4), xmm10);
2560 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 5), xmm11);
2561 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 6), xmm12);
2562 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 7), xmm13);
2563 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 8), xmm14);
2564 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 9), xmm15);
2567 // Set up the roots and smi constant registers.
2568 // Needs to be done before any further smi loads.
2569 __ InitializeRootRegister();
2572 // Save copies of the top frame descriptor on the stack.
2573 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
2575 Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
2576 __ Push(c_entry_fp_operand);
2579 // If this is the outermost JS call, set js_entry_sp value.
2580 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
2581 __ Load(rax, js_entry_sp);
2583 __ j(not_zero, ¬_outermost_js);
2584 __ Push(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
2586 __ Store(js_entry_sp, rax);
2589 __ bind(¬_outermost_js);
2590 __ Push(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME));
2593 // Jump to a faked try block that does the invoke, with a faked catch
2594 // block that sets the pending exception.
2596 __ bind(&handler_entry);
2597 handler_offset_ = handler_entry.pos();
2598 // Caught exception: Store result (exception) in the pending exception
2599 // field in the JSEnv and return a failure sentinel.
2600 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
2602 __ Store(pending_exception, rax);
2603 __ LoadRoot(rax, Heap::kExceptionRootIndex);
2606 // Invoke: Link this frame into the handler chain.
2608 __ PushStackHandler();
2610 // Clear any pending exceptions.
2611 __ LoadRoot(rax, Heap::kTheHoleValueRootIndex);
2612 __ Store(pending_exception, rax);
2614 // Fake a receiver (NULL).
2615 __ Push(Immediate(0)); // receiver
2617 // Invoke the function by calling through JS entry trampoline builtin and
2618 // pop the faked function when we return. We load the address from an
2619 // external reference instead of inlining the call target address directly
2620 // in the code, because the builtin stubs may not have been generated yet
2621 // at the time this code is generated.
2622 if (type() == StackFrame::ENTRY_CONSTRUCT) {
2623 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
2625 __ Load(rax, construct_entry);
2627 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
2628 __ Load(rax, entry);
2630 __ leap(kScratchRegister, FieldOperand(rax, Code::kHeaderSize));
2631 __ call(kScratchRegister);
2633 // Unlink this frame from the handler chain.
2634 __ PopStackHandler();
2637 // Check if the current stack frame is marked as the outermost JS frame.
2639 __ Cmp(rbx, Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
2640 __ j(not_equal, ¬_outermost_js_2);
2641 __ Move(kScratchRegister, js_entry_sp);
2642 __ movp(Operand(kScratchRegister, 0), Immediate(0));
2643 __ bind(¬_outermost_js_2);
2645 // Restore the top frame descriptor from the stack.
2646 { Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
2647 __ Pop(c_entry_fp_operand);
2650 // Restore callee-saved registers (X64 conventions).
2652 // On Win64 XMM6-XMM15 are callee-save
2653 __ movdqu(xmm6, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 0));
2654 __ movdqu(xmm7, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 1));
2655 __ movdqu(xmm8, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 2));
2656 __ movdqu(xmm9, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 3));
2657 __ movdqu(xmm10, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 4));
2658 __ movdqu(xmm11, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 5));
2659 __ movdqu(xmm12, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 6));
2660 __ movdqu(xmm13, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 7));
2661 __ movdqu(xmm14, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 8));
2662 __ movdqu(xmm15, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 9));
2663 __ addp(rsp, Immediate(EntryFrameConstants::kXMMRegistersBlockSize));
2668 // Callee save on in Win64 ABI, arguments/volatile in AMD64 ABI.
2676 __ addp(rsp, Immediate(2 * kPointerSize)); // remove markers
2678 // Restore frame pointer and return.
2684 void InstanceofStub::Generate(MacroAssembler* masm) {
2685 // Implements "value instanceof function" operator.
2686 // Expected input state with no inline cache:
2687 // rsp[0] : return address
2688 // rsp[8] : function pointer
2690 // Expected input state with an inline one-element cache:
2691 // rsp[0] : return address
2692 // rsp[8] : offset from return address to location of inline cache
2693 // rsp[16] : function pointer
2695 // Returns a bitwise zero to indicate that the value
2696 // is and instance of the function and anything else to
2697 // indicate that the value is not an instance.
2699 // Fixed register usage throughout the stub.
2700 Register object = rax; // Object (lhs).
2701 Register map = rbx; // Map of the object.
2702 Register function = rdx; // Function (rhs).
2703 Register prototype = rdi; // Prototype of the function.
2704 Register scratch = rcx;
2706 static const int kOffsetToMapCheckValue = 2;
2707 static const int kOffsetToResultValue = kPointerSize == kInt64Size ? 18 : 14;
2708 // The last 4 bytes of the instruction sequence
2709 // movp(rdi, FieldOperand(rax, HeapObject::kMapOffset))
2710 // Move(kScratchRegister, Factory::the_hole_value())
2711 // in front of the hole value address.
2712 static const unsigned int kWordBeforeMapCheckValue =
2713 kPointerSize == kInt64Size ? 0xBA49FF78 : 0xBA41FF78;
2714 // The last 4 bytes of the instruction sequence
2715 // __ j(not_equal, &cache_miss);
2716 // __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
2717 // before the offset of the hole value in the root array.
2718 static const unsigned int kWordBeforeResultValue =
2719 kPointerSize == kInt64Size ? 0x458B4906 : 0x458B4106;
2721 int extra_argument_offset = HasCallSiteInlineCheck() ? 1 : 0;
2723 DCHECK_EQ(object.code(), InstanceofStub::left().code());
2724 DCHECK_EQ(function.code(), InstanceofStub::right().code());
2726 // Get the object and function - they are always both needed.
2727 // Go slow case if the object is a smi.
2729 StackArgumentsAccessor args(rsp, 2 + extra_argument_offset,
2730 ARGUMENTS_DONT_CONTAIN_RECEIVER);
2731 if (!HasArgsInRegisters()) {
2732 __ movp(object, args.GetArgumentOperand(0));
2733 __ movp(function, args.GetArgumentOperand(1));
2735 __ JumpIfSmi(object, &slow);
2737 // Check that the left hand is a JS object. Leave its map in rax.
2738 __ CmpObjectType(object, FIRST_SPEC_OBJECT_TYPE, map);
2740 __ CmpInstanceType(map, LAST_SPEC_OBJECT_TYPE);
2743 // If there is a call site cache don't look in the global cache, but do the
2744 // real lookup and update the call site cache.
2745 if (!HasCallSiteInlineCheck() && !ReturnTrueFalseObject()) {
2746 // Look up the function and the map in the instanceof cache.
2748 __ CompareRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
2749 __ j(not_equal, &miss, Label::kNear);
2750 __ CompareRoot(map, Heap::kInstanceofCacheMapRootIndex);
2751 __ j(not_equal, &miss, Label::kNear);
2752 __ LoadRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
2753 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
2757 // Get the prototype of the function.
2758 __ TryGetFunctionPrototype(function, prototype, &slow, true);
2760 // Check that the function prototype is a JS object.
2761 __ JumpIfSmi(prototype, &slow);
2762 __ CmpObjectType(prototype, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
2764 __ CmpInstanceType(kScratchRegister, LAST_SPEC_OBJECT_TYPE);
2767 // Update the global instanceof or call site inlined cache with the current
2768 // map and function. The cached answer will be set when it is known below.
2769 if (!HasCallSiteInlineCheck()) {
2770 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
2771 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
2773 // The constants for the code patching are based on push instructions
2774 // at the call site.
2775 DCHECK(!HasArgsInRegisters());
2776 // Get return address and delta to inlined map check.
2777 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2778 __ subp(kScratchRegister, args.GetArgumentOperand(2));
2779 if (FLAG_debug_code) {
2780 __ movl(scratch, Immediate(kWordBeforeMapCheckValue));
2781 __ cmpl(Operand(kScratchRegister, kOffsetToMapCheckValue - 4), scratch);
2782 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCheck);
2784 __ movp(kScratchRegister,
2785 Operand(kScratchRegister, kOffsetToMapCheckValue));
2786 __ movp(Operand(kScratchRegister, 0), map);
2789 // Scratch points at the cell payload. Calculate the start of the object.
2790 __ subp(kScratchRegister, Immediate(Cell::kValueOffset - 1));
2791 __ RecordWriteField(kScratchRegister, Cell::kValueOffset, r8, function,
2792 kDontSaveFPRegs, OMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2795 // Loop through the prototype chain looking for the function prototype.
2796 __ movp(scratch, FieldOperand(map, Map::kPrototypeOffset));
2797 Label loop, is_instance, is_not_instance;
2798 __ LoadRoot(kScratchRegister, Heap::kNullValueRootIndex);
2800 __ cmpp(scratch, prototype);
2801 __ j(equal, &is_instance, Label::kNear);
2802 __ cmpp(scratch, kScratchRegister);
2803 // The code at is_not_instance assumes that kScratchRegister contains a
2804 // non-zero GCable value (the null object in this case).
2805 __ j(equal, &is_not_instance, Label::kNear);
2806 __ movp(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
2807 __ movp(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
2810 __ bind(&is_instance);
2811 if (!HasCallSiteInlineCheck()) {
2813 // Store bitwise zero in the cache. This is a Smi in GC terms.
2814 STATIC_ASSERT(kSmiTag == 0);
2815 __ StoreRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
2816 if (ReturnTrueFalseObject()) {
2817 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
2820 // Store offset of true in the root array at the inline check site.
2821 int true_offset = 0x100 +
2822 (Heap::kTrueValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
2823 // Assert it is a 1-byte signed value.
2824 DCHECK(true_offset >= 0 && true_offset < 0x100);
2825 __ movl(rax, Immediate(true_offset));
2826 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2827 __ subp(kScratchRegister, args.GetArgumentOperand(2));
2828 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
2829 if (FLAG_debug_code) {
2830 __ movl(rax, Immediate(kWordBeforeResultValue));
2831 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
2832 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2834 if (!ReturnTrueFalseObject()) {
2838 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2841 __ bind(&is_not_instance);
2842 if (!HasCallSiteInlineCheck()) {
2843 // We have to store a non-zero value in the cache.
2844 __ StoreRoot(kScratchRegister, Heap::kInstanceofCacheAnswerRootIndex);
2845 if (ReturnTrueFalseObject()) {
2846 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
2849 // Store offset of false in the root array at the inline check site.
2850 int false_offset = 0x100 +
2851 (Heap::kFalseValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
2852 // Assert it is a 1-byte signed value.
2853 DCHECK(false_offset >= 0 && false_offset < 0x100);
2854 __ movl(rax, Immediate(false_offset));
2855 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2856 __ subp(kScratchRegister, args.GetArgumentOperand(2));
2857 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
2858 if (FLAG_debug_code) {
2859 __ movl(rax, Immediate(kWordBeforeResultValue));
2860 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
2861 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
2864 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2867 // Slow-case: Go through the JavaScript implementation.
2869 if (!ReturnTrueFalseObject()) {
2870 // Tail call the builtin which returns 0 or 1.
2871 DCHECK(!HasArgsInRegisters());
2872 if (HasCallSiteInlineCheck()) {
2873 // Remove extra value from the stack.
2874 __ PopReturnAddressTo(rcx);
2876 __ PushReturnAddressFrom(rcx);
2878 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
2880 // Call the builtin and convert 0/1 to true/false.
2882 FrameScope scope(masm, StackFrame::INTERNAL);
2885 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
2887 Label true_value, done;
2889 __ j(zero, &true_value, Label::kNear);
2890 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
2891 __ jmp(&done, Label::kNear);
2892 __ bind(&true_value);
2893 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
2895 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2901 // -------------------------------------------------------------------------
2902 // StringCharCodeAtGenerator
2904 void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
2905 // If the receiver is a smi trigger the non-string case.
2906 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2907 __ JumpIfSmi(object_, receiver_not_string_);
2909 // Fetch the instance type of the receiver into result register.
2910 __ movp(result_, FieldOperand(object_, HeapObject::kMapOffset));
2911 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2912 // If the receiver is not a string trigger the non-string case.
2913 __ testb(result_, Immediate(kIsNotStringMask));
2914 __ j(not_zero, receiver_not_string_);
2917 // If the index is non-smi trigger the non-smi case.
2918 __ JumpIfNotSmi(index_, &index_not_smi_);
2919 __ bind(&got_smi_index_);
2921 // Check for index out of range.
2922 __ SmiCompare(index_, FieldOperand(object_, String::kLengthOffset));
2923 __ j(above_equal, index_out_of_range_);
2925 __ SmiToInteger32(index_, index_);
2927 StringCharLoadGenerator::Generate(
2928 masm, object_, index_, result_, &call_runtime_);
2930 __ Integer32ToSmi(result_, result_);
2935 void StringCharCodeAtGenerator::GenerateSlow(
2936 MacroAssembler* masm, EmbedMode embed_mode,
2937 const RuntimeCallHelper& call_helper) {
2938 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
2940 Factory* factory = masm->isolate()->factory();
2941 // Index is not a smi.
2942 __ bind(&index_not_smi_);
2943 // If index is a heap number, try converting it to an integer.
2945 factory->heap_number_map(),
2948 call_helper.BeforeCall(masm);
2949 if (embed_mode == PART_OF_IC_HANDLER) {
2950 __ Push(LoadWithVectorDescriptor::VectorRegister());
2951 __ Push(LoadDescriptor::SlotRegister());
2954 __ Push(index_); // Consumed by runtime conversion function.
2955 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
2956 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
2958 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
2959 // NumberToSmi discards numbers that are not exact integers.
2960 __ CallRuntime(Runtime::kNumberToSmi, 1);
2962 if (!index_.is(rax)) {
2963 // Save the conversion result before the pop instructions below
2964 // have a chance to overwrite it.
2965 __ movp(index_, rax);
2968 if (embed_mode == PART_OF_IC_HANDLER) {
2969 __ Pop(LoadDescriptor::SlotRegister());
2970 __ Pop(LoadWithVectorDescriptor::VectorRegister());
2972 // Reload the instance type.
2973 __ movp(result_, FieldOperand(object_, HeapObject::kMapOffset));
2974 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2975 call_helper.AfterCall(masm);
2976 // If index is still not a smi, it must be out of range.
2977 __ JumpIfNotSmi(index_, index_out_of_range_);
2978 // Otherwise, return to the fast path.
2979 __ jmp(&got_smi_index_);
2981 // Call runtime. We get here when the receiver is a string and the
2982 // index is a number, but the code of getting the actual character
2983 // is too complex (e.g., when the string needs to be flattened).
2984 __ bind(&call_runtime_);
2985 call_helper.BeforeCall(masm);
2987 __ Integer32ToSmi(index_, index_);
2989 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
2990 if (!result_.is(rax)) {
2991 __ movp(result_, rax);
2993 call_helper.AfterCall(masm);
2996 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
3000 // -------------------------------------------------------------------------
3001 // StringCharFromCodeGenerator
3003 void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3004 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3005 __ JumpIfNotSmi(code_, &slow_case_);
3006 __ SmiCompare(code_, Smi::FromInt(String::kMaxOneByteCharCode));
3007 __ j(above, &slow_case_);
3009 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
3010 SmiIndex index = masm->SmiToIndex(kScratchRegister, code_, kPointerSizeLog2);
3011 __ movp(result_, FieldOperand(result_, index.reg, index.scale,
3012 FixedArray::kHeaderSize));
3013 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
3014 __ j(equal, &slow_case_);
3019 void StringCharFromCodeGenerator::GenerateSlow(
3020 MacroAssembler* masm,
3021 const RuntimeCallHelper& call_helper) {
3022 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
3024 __ bind(&slow_case_);
3025 call_helper.BeforeCall(masm);
3027 __ CallRuntime(Runtime::kCharFromCode, 1);
3028 if (!result_.is(rax)) {
3029 __ movp(result_, rax);
3031 call_helper.AfterCall(masm);
3034 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
3038 void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3042 String::Encoding encoding) {
3043 // Nothing to do for zero characters.
3045 __ testl(count, count);
3046 __ j(zero, &done, Label::kNear);
3048 // Make count the number of bytes to copy.
3049 if (encoding == String::TWO_BYTE_ENCODING) {
3050 STATIC_ASSERT(2 == sizeof(uc16));
3051 __ addl(count, count);
3054 // Copy remaining characters.
3057 __ movb(kScratchRegister, Operand(src, 0));
3058 __ movb(Operand(dest, 0), kScratchRegister);
3062 __ j(not_zero, &loop);
3068 void SubStringStub::Generate(MacroAssembler* masm) {
3071 // Stack frame on entry.
3072 // rsp[0] : return address
3077 enum SubStringStubArgumentIndices {
3078 STRING_ARGUMENT_INDEX,
3079 FROM_ARGUMENT_INDEX,
3081 SUB_STRING_ARGUMENT_COUNT
3084 StackArgumentsAccessor args(rsp, SUB_STRING_ARGUMENT_COUNT,
3085 ARGUMENTS_DONT_CONTAIN_RECEIVER);
3087 // Make sure first argument is a string.
3088 __ movp(rax, args.GetArgumentOperand(STRING_ARGUMENT_INDEX));
3089 STATIC_ASSERT(kSmiTag == 0);
3090 __ testl(rax, Immediate(kSmiTagMask));
3091 __ j(zero, &runtime);
3092 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
3093 __ j(NegateCondition(is_string), &runtime);
3096 // rbx: instance type
3097 // Calculate length of sub string using the smi values.
3098 __ movp(rcx, args.GetArgumentOperand(TO_ARGUMENT_INDEX));
3099 __ movp(rdx, args.GetArgumentOperand(FROM_ARGUMENT_INDEX));
3100 __ JumpUnlessBothNonNegativeSmi(rcx, rdx, &runtime);
3102 __ SmiSub(rcx, rcx, rdx); // Overflow doesn't happen.
3103 __ cmpp(rcx, FieldOperand(rax, String::kLengthOffset));
3104 Label not_original_string;
3105 // Shorter than original string's length: an actual substring.
3106 __ j(below, ¬_original_string, Label::kNear);
3107 // Longer than original string's length or negative: unsafe arguments.
3108 __ j(above, &runtime);
3109 // Return original string.
3110 Counters* counters = isolate()->counters();
3111 __ IncrementCounter(counters->sub_string_native(), 1);
3112 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3113 __ bind(¬_original_string);
3116 __ SmiCompare(rcx, Smi::FromInt(1));
3117 __ j(equal, &single_char);
3119 __ SmiToInteger32(rcx, rcx);
3122 // rbx: instance type
3123 // rcx: sub string length
3124 // rdx: from index (smi)
3125 // Deal with different string types: update the index if necessary
3126 // and put the underlying string into edi.
3127 Label underlying_unpacked, sliced_string, seq_or_external_string;
3128 // If the string is not indirect, it can only be sequential or external.
3129 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3130 STATIC_ASSERT(kIsIndirectStringMask != 0);
3131 __ testb(rbx, Immediate(kIsIndirectStringMask));
3132 __ j(zero, &seq_or_external_string, Label::kNear);
3134 __ testb(rbx, Immediate(kSlicedNotConsMask));
3135 __ j(not_zero, &sliced_string, Label::kNear);
3136 // Cons string. Check whether it is flat, then fetch first part.
3137 // Flat cons strings have an empty second part.
3138 __ CompareRoot(FieldOperand(rax, ConsString::kSecondOffset),
3139 Heap::kempty_stringRootIndex);
3140 __ j(not_equal, &runtime);
3141 __ movp(rdi, FieldOperand(rax, ConsString::kFirstOffset));
3142 // Update instance type.
3143 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
3144 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
3145 __ jmp(&underlying_unpacked, Label::kNear);
3147 __ bind(&sliced_string);
3148 // Sliced string. Fetch parent and correct start index by offset.
3149 __ addp(rdx, FieldOperand(rax, SlicedString::kOffsetOffset));
3150 __ movp(rdi, FieldOperand(rax, SlicedString::kParentOffset));
3151 // Update instance type.
3152 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
3153 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
3154 __ jmp(&underlying_unpacked, Label::kNear);
3156 __ bind(&seq_or_external_string);
3157 // Sequential or external string. Just move string to the correct register.
3160 __ bind(&underlying_unpacked);
3162 if (FLAG_string_slices) {
3164 // rdi: underlying subject string
3165 // rbx: instance type of underlying subject string
3166 // rdx: adjusted start index (smi)
3168 // If coming from the make_two_character_string path, the string
3169 // is too short to be sliced anyways.
3170 __ cmpp(rcx, Immediate(SlicedString::kMinLength));
3171 // Short slice. Copy instead of slicing.
3172 __ j(less, ©_routine);
3173 // Allocate new sliced string. At this point we do not reload the instance
3174 // type including the string encoding because we simply rely on the info
3175 // provided by the original string. It does not matter if the original
3176 // string's encoding is wrong because we always have to recheck encoding of
3177 // the newly created string's parent anyways due to externalized strings.
3178 Label two_byte_slice, set_slice_header;
3179 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
3180 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3181 __ testb(rbx, Immediate(kStringEncodingMask));
3182 __ j(zero, &two_byte_slice, Label::kNear);
3183 __ AllocateOneByteSlicedString(rax, rbx, r14, &runtime);
3184 __ jmp(&set_slice_header, Label::kNear);
3185 __ bind(&two_byte_slice);
3186 __ AllocateTwoByteSlicedString(rax, rbx, r14, &runtime);
3187 __ bind(&set_slice_header);
3188 __ Integer32ToSmi(rcx, rcx);
3189 __ movp(FieldOperand(rax, SlicedString::kLengthOffset), rcx);
3190 __ movp(FieldOperand(rax, SlicedString::kHashFieldOffset),
3191 Immediate(String::kEmptyHashField));
3192 __ movp(FieldOperand(rax, SlicedString::kParentOffset), rdi);
3193 __ movp(FieldOperand(rax, SlicedString::kOffsetOffset), rdx);
3194 __ IncrementCounter(counters->sub_string_native(), 1);
3195 __ ret(3 * kPointerSize);
3197 __ bind(©_routine);
3200 // rdi: underlying subject string
3201 // rbx: instance type of underlying subject string
3202 // rdx: adjusted start index (smi)
3204 // The subject string can only be external or sequential string of either
3205 // encoding at this point.
3206 Label two_byte_sequential, sequential_string;
3207 STATIC_ASSERT(kExternalStringTag != 0);
3208 STATIC_ASSERT(kSeqStringTag == 0);
3209 __ testb(rbx, Immediate(kExternalStringTag));
3210 __ j(zero, &sequential_string);
3212 // Handle external string.
3213 // Rule out short external strings.
3214 STATIC_ASSERT(kShortExternalStringTag != 0);
3215 __ testb(rbx, Immediate(kShortExternalStringMask));
3216 __ j(not_zero, &runtime);
3217 __ movp(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
3218 // Move the pointer so that offset-wise, it looks like a sequential string.
3219 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3220 __ subp(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3222 __ bind(&sequential_string);
3223 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
3224 __ testb(rbx, Immediate(kStringEncodingMask));
3225 __ j(zero, &two_byte_sequential);
3227 // Allocate the result.
3228 __ AllocateOneByteString(rax, rcx, r11, r14, r15, &runtime);
3230 // rax: result string
3231 // rcx: result string length
3232 { // Locate character of sub string start.
3233 SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_1);
3234 __ leap(r14, Operand(rdi, smi_as_index.reg, smi_as_index.scale,
3235 SeqOneByteString::kHeaderSize - kHeapObjectTag));
3237 // Locate first character of result.
3238 __ leap(rdi, FieldOperand(rax, SeqOneByteString::kHeaderSize));
3240 // rax: result string
3241 // rcx: result length
3242 // r14: first character of result
3243 // rsi: character of sub string start
3244 StringHelper::GenerateCopyCharacters(
3245 masm, rdi, r14, rcx, String::ONE_BYTE_ENCODING);
3246 __ IncrementCounter(counters->sub_string_native(), 1);
3247 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3249 __ bind(&two_byte_sequential);
3250 // Allocate the result.
3251 __ AllocateTwoByteString(rax, rcx, r11, r14, r15, &runtime);
3253 // rax: result string
3254 // rcx: result string length
3255 { // Locate character of sub string start.
3256 SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_2);
3257 __ leap(r14, Operand(rdi, smi_as_index.reg, smi_as_index.scale,
3258 SeqOneByteString::kHeaderSize - kHeapObjectTag));
3260 // Locate first character of result.
3261 __ leap(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
3263 // rax: result string
3264 // rcx: result length
3265 // rdi: first character of result
3266 // r14: character of sub string start
3267 StringHelper::GenerateCopyCharacters(
3268 masm, rdi, r14, rcx, String::TWO_BYTE_ENCODING);
3269 __ IncrementCounter(counters->sub_string_native(), 1);
3270 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3272 // Just jump to runtime to create the sub string.
3274 __ TailCallRuntime(Runtime::kSubStringRT, 3, 1);
3276 __ bind(&single_char);
3278 // rbx: instance type
3279 // rcx: sub string length (smi)
3280 // rdx: from index (smi)
3281 StringCharAtGenerator generator(rax, rdx, rcx, rax, &runtime, &runtime,
3282 &runtime, STRING_INDEX_IS_NUMBER,
3283 RECEIVER_IS_STRING);
3284 generator.GenerateFast(masm);
3285 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3286 generator.SkipSlow(masm, &runtime);
3290 void ToNumberStub::Generate(MacroAssembler* masm) {
3291 // The ToNumber stub takes one argument in rax.
3293 __ JumpIfNotSmi(rax, ¬_smi, Label::kNear);
3297 Label not_heap_number;
3298 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
3299 Heap::kHeapNumberMapRootIndex);
3300 __ j(not_equal, ¬_heap_number, Label::kNear);
3302 __ bind(¬_heap_number);
3304 Label not_string, slow_string;
3305 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, rdi);
3308 __ j(above_equal, ¬_string, Label::kNear);
3309 // Check if string has a cached array index.
3310 __ testl(FieldOperand(rax, String::kHashFieldOffset),
3311 Immediate(String::kContainsCachedArrayIndexMask));
3312 __ j(not_zero, &slow_string, Label::kNear);
3313 __ movl(rax, FieldOperand(rax, String::kHashFieldOffset));
3314 __ IndexFromHash(rax, rax);
3316 __ bind(&slow_string);
3317 __ PopReturnAddressTo(rcx); // Pop return address.
3318 __ Push(rax); // Push argument.
3319 __ PushReturnAddressFrom(rcx); // Push return address.
3320 __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
3321 __ bind(¬_string);
3324 __ CmpInstanceType(rdi, ODDBALL_TYPE);
3325 __ j(not_equal, ¬_oddball, Label::kNear);
3326 __ movp(rax, FieldOperand(rax, Oddball::kToNumberOffset));
3328 __ bind(¬_oddball);
3330 __ PopReturnAddressTo(rcx); // Pop return address.
3331 __ Push(rax); // Push argument.
3332 __ PushReturnAddressFrom(rcx); // Push return address.
3333 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
3337 void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
3341 Register scratch2) {
3342 Register length = scratch1;
3345 Label check_zero_length;
3346 __ movp(length, FieldOperand(left, String::kLengthOffset));
3347 __ SmiCompare(length, FieldOperand(right, String::kLengthOffset));
3348 __ j(equal, &check_zero_length, Label::kNear);
3349 __ Move(rax, Smi::FromInt(NOT_EQUAL));
3352 // Check if the length is zero.
3353 Label compare_chars;
3354 __ bind(&check_zero_length);
3355 STATIC_ASSERT(kSmiTag == 0);
3357 __ j(not_zero, &compare_chars, Label::kNear);
3358 __ Move(rax, Smi::FromInt(EQUAL));
3361 // Compare characters.
3362 __ bind(&compare_chars);
3363 Label strings_not_equal;
3364 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
3365 &strings_not_equal, Label::kNear);
3367 // Characters are equal.
3368 __ Move(rax, Smi::FromInt(EQUAL));
3371 // Characters are not equal.
3372 __ bind(&strings_not_equal);
3373 __ Move(rax, Smi::FromInt(NOT_EQUAL));
3378 void StringHelper::GenerateCompareFlatOneByteStrings(
3379 MacroAssembler* masm, Register left, Register right, Register scratch1,
3380 Register scratch2, Register scratch3, Register scratch4) {
3381 // Ensure that you can always subtract a string length from a non-negative
3382 // number (e.g. another length).
3383 STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
3385 // Find minimum length and length difference.
3386 __ movp(scratch1, FieldOperand(left, String::kLengthOffset));
3387 __ movp(scratch4, scratch1);
3390 FieldOperand(right, String::kLengthOffset));
3391 // Register scratch4 now holds left.length - right.length.
3392 const Register length_difference = scratch4;
3394 __ j(less, &left_shorter, Label::kNear);
3395 // The right string isn't longer that the left one.
3396 // Get the right string's length by subtracting the (non-negative) difference
3397 // from the left string's length.
3398 __ SmiSub(scratch1, scratch1, length_difference);
3399 __ bind(&left_shorter);
3400 // Register scratch1 now holds Min(left.length, right.length).
3401 const Register min_length = scratch1;
3403 Label compare_lengths;
3404 // If min-length is zero, go directly to comparing lengths.
3405 __ SmiTest(min_length);
3406 __ j(zero, &compare_lengths, Label::kNear);
3409 Label result_not_equal;
3410 GenerateOneByteCharsCompareLoop(
3411 masm, left, right, min_length, scratch2, &result_not_equal,
3412 // In debug-code mode, SmiTest below might push
3413 // the target label outside the near range.
3416 // Completed loop without finding different characters.
3417 // Compare lengths (precomputed).
3418 __ bind(&compare_lengths);
3419 __ SmiTest(length_difference);
3420 Label length_not_equal;
3421 __ j(not_zero, &length_not_equal, Label::kNear);
3424 __ Move(rax, Smi::FromInt(EQUAL));
3427 Label result_greater;
3429 __ bind(&length_not_equal);
3430 __ j(greater, &result_greater, Label::kNear);
3431 __ jmp(&result_less, Label::kNear);
3432 __ bind(&result_not_equal);
3433 // Unequal comparison of left to right, either character or length.
3434 __ j(above, &result_greater, Label::kNear);
3435 __ bind(&result_less);
3438 __ Move(rax, Smi::FromInt(LESS));
3441 // Result is GREATER.
3442 __ bind(&result_greater);
3443 __ Move(rax, Smi::FromInt(GREATER));
3448 void StringHelper::GenerateOneByteCharsCompareLoop(
3449 MacroAssembler* masm, Register left, Register right, Register length,
3450 Register scratch, Label* chars_not_equal, Label::Distance near_jump) {
3451 // Change index to run from -length to -1 by adding length to string
3452 // start. This means that loop ends when index reaches zero, which
3453 // doesn't need an additional compare.
3454 __ SmiToInteger32(length, length);
3456 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
3458 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
3460 Register index = length; // index = -length;
3465 __ movb(scratch, Operand(left, index, times_1, 0));
3466 __ cmpb(scratch, Operand(right, index, times_1, 0));
3467 __ j(not_equal, chars_not_equal, near_jump);
3469 __ j(not_zero, &loop);
3473 void StringCompareStub::Generate(MacroAssembler* masm) {
3476 // Stack frame on entry.
3477 // rsp[0] : return address
3478 // rsp[8] : right string
3479 // rsp[16] : left string
3481 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER);
3482 __ movp(rdx, args.GetArgumentOperand(0)); // left
3483 __ movp(rax, args.GetArgumentOperand(1)); // right
3485 // Check for identity.
3488 __ j(not_equal, ¬_same, Label::kNear);
3489 __ Move(rax, Smi::FromInt(EQUAL));
3490 Counters* counters = isolate()->counters();
3491 __ IncrementCounter(counters->string_compare_native(), 1);
3492 __ ret(2 * kPointerSize);
3496 // Check that both are sequential one-byte strings.
3497 __ JumpIfNotBothSequentialOneByteStrings(rdx, rax, rcx, rbx, &runtime);
3499 // Inline comparison of one-byte strings.
3500 __ IncrementCounter(counters->string_compare_native(), 1);
3501 // Drop arguments from the stack
3502 __ PopReturnAddressTo(rcx);
3503 __ addp(rsp, Immediate(2 * kPointerSize));
3504 __ PushReturnAddressFrom(rcx);
3505 StringHelper::GenerateCompareFlatOneByteStrings(masm, rdx, rax, rcx, rbx, rdi,
3508 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
3509 // tagged as a small integer.
3511 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3515 void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3516 // ----------- S t a t e -------------
3519 // -- rsp[0] : return address
3520 // -----------------------------------
3522 // Load rcx with the allocation site. We stick an undefined dummy value here
3523 // and replace it with the real allocation site later when we instantiate this
3524 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3525 __ Move(rcx, handle(isolate()->heap()->undefined_value()));
3527 // Make sure that we actually patched the allocation site.
3528 if (FLAG_debug_code) {
3529 __ testb(rcx, Immediate(kSmiTagMask));
3530 __ Assert(not_equal, kExpectedAllocationSite);
3531 __ Cmp(FieldOperand(rcx, HeapObject::kMapOffset),
3532 isolate()->factory()->allocation_site_map());
3533 __ Assert(equal, kExpectedAllocationSite);
3536 // Tail call into the stub that handles binary operations with allocation
3538 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3539 __ TailCallStub(&stub);
3543 void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3544 DCHECK(state() == CompareICState::SMI);
3546 __ JumpIfNotBothSmi(rdx, rax, &miss, Label::kNear);
3548 if (GetCondition() == equal) {
3549 // For equality we do not care about the sign of the result.
3554 __ j(no_overflow, &done, Label::kNear);
3555 // Correct sign of result in case of overflow.
3567 void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3568 DCHECK(state() == CompareICState::NUMBER);
3571 Label unordered, maybe_undefined1, maybe_undefined2;
3574 if (left() == CompareICState::SMI) {
3575 __ JumpIfNotSmi(rdx, &miss);
3577 if (right() == CompareICState::SMI) {
3578 __ JumpIfNotSmi(rax, &miss);
3581 // Load left and right operand.
3582 Label done, left, left_smi, right_smi;
3583 __ JumpIfSmi(rax, &right_smi, Label::kNear);
3584 __ CompareMap(rax, isolate()->factory()->heap_number_map());
3585 __ j(not_equal, &maybe_undefined1, Label::kNear);
3586 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
3587 __ jmp(&left, Label::kNear);
3588 __ bind(&right_smi);
3589 __ SmiToInteger32(rcx, rax); // Can't clobber rax yet.
3590 __ Cvtlsi2sd(xmm1, rcx);
3593 __ JumpIfSmi(rdx, &left_smi, Label::kNear);
3594 __ CompareMap(rdx, isolate()->factory()->heap_number_map());
3595 __ j(not_equal, &maybe_undefined2, Label::kNear);
3596 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
3599 __ SmiToInteger32(rcx, rdx); // Can't clobber rdx yet.
3600 __ Cvtlsi2sd(xmm0, rcx);
3604 __ ucomisd(xmm0, xmm1);
3606 // Don't base result on EFLAGS when a NaN is involved.
3607 __ j(parity_even, &unordered, Label::kNear);
3609 // Return a result of -1, 0, or 1, based on EFLAGS.
3610 // Performing mov, because xor would destroy the flag register.
3611 __ movl(rax, Immediate(0));
3612 __ movl(rcx, Immediate(0));
3613 __ setcc(above, rax); // Add one to zero if carry clear and not equal.
3614 __ sbbp(rax, rcx); // Subtract one if below (aka. carry set).
3617 __ bind(&unordered);
3618 __ bind(&generic_stub);
3619 CompareICStub stub(isolate(), op(), strength(), CompareICState::GENERIC,
3620 CompareICState::GENERIC, CompareICState::GENERIC);
3621 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3623 __ bind(&maybe_undefined1);
3624 if (Token::IsOrderedRelationalCompareOp(op())) {
3625 __ Cmp(rax, isolate()->factory()->undefined_value());
3626 __ j(not_equal, &miss);
3627 __ JumpIfSmi(rdx, &unordered);
3628 __ CmpObjectType(rdx, HEAP_NUMBER_TYPE, rcx);
3629 __ j(not_equal, &maybe_undefined2, Label::kNear);
3633 __ bind(&maybe_undefined2);
3634 if (Token::IsOrderedRelationalCompareOp(op())) {
3635 __ Cmp(rdx, isolate()->factory()->undefined_value());
3636 __ j(equal, &unordered);
3644 void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3645 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3646 DCHECK(GetCondition() == equal);
3648 // Registers containing left and right operands respectively.
3649 Register left = rdx;
3650 Register right = rax;
3651 Register tmp1 = rcx;
3652 Register tmp2 = rbx;
3654 // Check that both operands are heap objects.
3656 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3657 __ j(cond, &miss, Label::kNear);
3659 // Check that both operands are internalized strings.
3660 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3661 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3662 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3663 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3664 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3666 __ testb(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
3667 __ j(not_zero, &miss, Label::kNear);
3669 // Internalized strings are compared by identity.
3671 __ cmpp(left, right);
3672 // Make sure rax is non-zero. At this point input operands are
3673 // guaranteed to be non-zero.
3674 DCHECK(right.is(rax));
3675 __ j(not_equal, &done, Label::kNear);
3676 STATIC_ASSERT(EQUAL == 0);
3677 STATIC_ASSERT(kSmiTag == 0);
3678 __ Move(rax, Smi::FromInt(EQUAL));
3687 void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3688 DCHECK(state() == CompareICState::UNIQUE_NAME);
3689 DCHECK(GetCondition() == equal);
3691 // Registers containing left and right operands respectively.
3692 Register left = rdx;
3693 Register right = rax;
3694 Register tmp1 = rcx;
3695 Register tmp2 = rbx;
3697 // Check that both operands are heap objects.
3699 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3700 __ j(cond, &miss, Label::kNear);
3702 // Check that both operands are unique names. This leaves the instance
3703 // types loaded in tmp1 and tmp2.
3704 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3705 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3706 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3707 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3709 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
3710 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
3712 // Unique names are compared by identity.
3714 __ cmpp(left, right);
3715 // Make sure rax is non-zero. At this point input operands are
3716 // guaranteed to be non-zero.
3717 DCHECK(right.is(rax));
3718 __ j(not_equal, &done, Label::kNear);
3719 STATIC_ASSERT(EQUAL == 0);
3720 STATIC_ASSERT(kSmiTag == 0);
3721 __ Move(rax, Smi::FromInt(EQUAL));
3730 void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3731 DCHECK(state() == CompareICState::STRING);
3734 bool equality = Token::IsEqualityOp(op());
3736 // Registers containing left and right operands respectively.
3737 Register left = rdx;
3738 Register right = rax;
3739 Register tmp1 = rcx;
3740 Register tmp2 = rbx;
3741 Register tmp3 = rdi;
3743 // Check that both operands are heap objects.
3744 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3747 // Check that both operands are strings. This leaves the instance
3748 // types loaded in tmp1 and tmp2.
3749 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3750 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3751 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3752 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3753 __ movp(tmp3, tmp1);
3754 STATIC_ASSERT(kNotStringTag != 0);
3756 __ testb(tmp3, Immediate(kIsNotStringMask));
3757 __ j(not_zero, &miss);
3759 // Fast check for identical strings.
3761 __ cmpp(left, right);
3762 __ j(not_equal, ¬_same, Label::kNear);
3763 STATIC_ASSERT(EQUAL == 0);
3764 STATIC_ASSERT(kSmiTag == 0);
3765 __ Move(rax, Smi::FromInt(EQUAL));
3768 // Handle not identical strings.
3771 // Check that both strings are internalized strings. If they are, we're done
3772 // because we already know they are not identical. We also know they are both
3776 STATIC_ASSERT(kInternalizedTag == 0);
3778 __ testb(tmp1, Immediate(kIsNotInternalizedMask));
3779 __ j(not_zero, &do_compare, Label::kNear);
3780 // Make sure rax is non-zero. At this point input operands are
3781 // guaranteed to be non-zero.
3782 DCHECK(right.is(rax));
3784 __ bind(&do_compare);
3787 // Check that both strings are sequential one-byte.
3789 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
3791 // Compare flat one-byte strings. Returns when done.
3793 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
3796 StringHelper::GenerateCompareFlatOneByteStrings(
3797 masm, left, right, tmp1, tmp2, tmp3, kScratchRegister);
3800 // Handle more complex cases in runtime.
3802 __ PopReturnAddressTo(tmp1);
3805 __ PushReturnAddressFrom(tmp1);
3807 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3809 __ TailCallRuntime(Runtime::kStringCompareRT, 2, 1);
3817 void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3818 DCHECK(state() == CompareICState::OBJECT);
3820 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
3821 __ j(either_smi, &miss, Label::kNear);
3823 __ CmpObjectType(rax, JS_OBJECT_TYPE, rcx);
3824 __ j(not_equal, &miss, Label::kNear);
3825 __ CmpObjectType(rdx, JS_OBJECT_TYPE, rcx);
3826 __ j(not_equal, &miss, Label::kNear);
3828 DCHECK(GetCondition() == equal);
3837 void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
3839 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
3840 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
3841 __ j(either_smi, &miss, Label::kNear);
3843 __ GetWeakValue(rdi, cell);
3844 __ movp(rcx, FieldOperand(rax, HeapObject::kMapOffset));
3845 __ movp(rbx, FieldOperand(rdx, HeapObject::kMapOffset));
3847 __ j(not_equal, &miss, Label::kNear);
3849 __ j(not_equal, &miss, Label::kNear);
3859 void CompareICStub::GenerateMiss(MacroAssembler* masm) {
3861 // Call the runtime system in a fresh internal frame.
3862 FrameScope scope(masm, StackFrame::INTERNAL);
3867 __ Push(Smi::FromInt(op()));
3868 __ CallRuntime(Runtime::kCompareIC_Miss, 3);
3870 // Compute the entry point of the rewritten stub.
3871 __ leap(rdi, FieldOperand(rax, Code::kHeaderSize));
3876 // Do a tail call to the rewritten stub.
3881 void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3884 Register properties,
3887 DCHECK(name->IsUniqueName());
3888 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3889 // not equal to the name and kProbes-th slot is not used (its name is the
3890 // undefined value), it guarantees the hash table doesn't contain the
3891 // property. It's true even if some slots represent deleted properties
3892 // (their names are the hole value).
3893 for (int i = 0; i < kInlinedProbes; i++) {
3894 // r0 points to properties hash.
3895 // Compute the masked index: (hash + i + i * i) & mask.
3896 Register index = r0;
3897 // Capacity is smi 2^n.
3898 __ SmiToInteger32(index, FieldOperand(properties, kCapacityOffset));
3901 Immediate(name->Hash() + NameDictionary::GetProbeOffset(i)));
3903 // Scale the index by multiplying by the entry size.
3904 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3905 __ leap(index, Operand(index, index, times_2, 0)); // index *= 3.
3907 Register entity_name = r0;
3908 // Having undefined at this place means the name is not contained.
3909 STATIC_ASSERT(kSmiTagSize == 1);
3910 __ movp(entity_name, Operand(properties,
3913 kElementsStartOffset - kHeapObjectTag));
3914 __ Cmp(entity_name, masm->isolate()->factory()->undefined_value());
3917 // Stop if found the property.
3918 __ Cmp(entity_name, Handle<Name>(name));
3922 // Check for the hole and skip.
3923 __ CompareRoot(entity_name, Heap::kTheHoleValueRootIndex);
3924 __ j(equal, &good, Label::kNear);
3926 // Check if the entry name is not a unique name.
3927 __ movp(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3928 __ JumpIfNotUniqueNameInstanceType(
3929 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3933 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3935 __ Push(Handle<Object>(name));
3936 __ Push(Immediate(name->Hash()));
3939 __ j(not_zero, miss);
3944 // Probe the name dictionary in the |elements| register. Jump to the
3945 // |done| label if a property with the given name is found leaving the
3946 // index into the dictionary in |r1|. Jump to the |miss| label
3948 void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3955 DCHECK(!elements.is(r0));
3956 DCHECK(!elements.is(r1));
3957 DCHECK(!name.is(r0));
3958 DCHECK(!name.is(r1));
3960 __ AssertName(name);
3962 __ SmiToInteger32(r0, FieldOperand(elements, kCapacityOffset));
3965 for (int i = 0; i < kInlinedProbes; i++) {
3966 // Compute the masked index: (hash + i + i * i) & mask.
3967 __ movl(r1, FieldOperand(name, Name::kHashFieldOffset));
3968 __ shrl(r1, Immediate(Name::kHashShift));
3970 __ addl(r1, Immediate(NameDictionary::GetProbeOffset(i)));
3974 // Scale the index by multiplying by the entry size.
3975 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
3976 __ leap(r1, Operand(r1, r1, times_2, 0)); // r1 = r1 * 3
3978 // Check if the key is identical to the name.
3979 __ cmpp(name, Operand(elements, r1, times_pointer_size,
3980 kElementsStartOffset - kHeapObjectTag));
3984 NameDictionaryLookupStub stub(masm->isolate(), elements, r0, r1,
3987 __ movl(r0, FieldOperand(name, Name::kHashFieldOffset));
3988 __ shrl(r0, Immediate(Name::kHashShift));
3998 void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
3999 // This stub overrides SometimesSetsUpAFrame() to return false. That means
4000 // we cannot call anything that could cause a GC from this stub.
4001 // Stack frame on entry:
4002 // rsp[0 * kPointerSize] : return address.
4003 // rsp[1 * kPointerSize] : key's hash.
4004 // rsp[2 * kPointerSize] : key.
4006 // dictionary_: NameDictionary to probe.
4007 // result_: used as scratch.
4008 // index_: will hold an index of entry if lookup is successful.
4009 // might alias with result_.
4011 // result_ is zero if lookup failed, non zero otherwise.
4013 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
4015 Register scratch = result();
4017 __ SmiToInteger32(scratch, FieldOperand(dictionary(), kCapacityOffset));
4021 // If names of slots in range from 1 to kProbes - 1 for the hash value are
4022 // not equal to the name and kProbes-th slot is not used (its name is the
4023 // undefined value), it guarantees the hash table doesn't contain the
4024 // property. It's true even if some slots represent deleted properties
4025 // (their names are the null value).
4026 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER,
4028 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
4029 // Compute the masked index: (hash + i + i * i) & mask.
4030 __ movp(scratch, args.GetArgumentOperand(1));
4032 __ addl(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
4034 __ andp(scratch, Operand(rsp, 0));
4036 // Scale the index by multiplying by the entry size.
4037 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
4038 __ leap(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
4040 // Having undefined at this place means the name is not contained.
4041 __ movp(scratch, Operand(dictionary(), index(), times_pointer_size,
4042 kElementsStartOffset - kHeapObjectTag));
4044 __ Cmp(scratch, isolate()->factory()->undefined_value());
4045 __ j(equal, ¬_in_dictionary);
4047 // Stop if found the property.
4048 __ cmpp(scratch, args.GetArgumentOperand(0));
4049 __ j(equal, &in_dictionary);
4051 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
4052 // If we hit a key that is not a unique name during negative
4053 // lookup we have to bailout as this key might be equal to the
4054 // key we are looking for.
4056 // Check if the entry name is not a unique name.
4057 __ movp(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
4058 __ JumpIfNotUniqueNameInstanceType(
4059 FieldOperand(scratch, Map::kInstanceTypeOffset),
4060 &maybe_in_dictionary);
4064 __ bind(&maybe_in_dictionary);
4065 // If we are doing negative lookup then probing failure should be
4066 // treated as a lookup success. For positive lookup probing failure
4067 // should be treated as lookup failure.
4068 if (mode() == POSITIVE_LOOKUP) {
4069 __ movp(scratch, Immediate(0));
4071 __ ret(2 * kPointerSize);
4074 __ bind(&in_dictionary);
4075 __ movp(scratch, Immediate(1));
4077 __ ret(2 * kPointerSize);
4079 __ bind(¬_in_dictionary);
4080 __ movp(scratch, Immediate(0));
4082 __ ret(2 * kPointerSize);
4086 void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
4088 StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
4090 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
4095 // Takes the input in 3 registers: address_ value_ and object_. A pointer to
4096 // the value has just been written into the object, now this stub makes sure
4097 // we keep the GC informed. The word in the object where the value has been
4098 // written is in the address register.
4099 void RecordWriteStub::Generate(MacroAssembler* masm) {
4100 Label skip_to_incremental_noncompacting;
4101 Label skip_to_incremental_compacting;
4103 // The first two instructions are generated with labels so as to get the
4104 // offset fixed up correctly by the bind(Label*) call. We patch it back and
4105 // forth between a compare instructions (a nop in this position) and the
4106 // real branch when we start and stop incremental heap marking.
4107 // See RecordWriteStub::Patch for details.
4108 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
4109 __ jmp(&skip_to_incremental_compacting, Label::kFar);
4111 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4112 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4113 MacroAssembler::kReturnAtEnd);
4118 __ bind(&skip_to_incremental_noncompacting);
4119 GenerateIncremental(masm, INCREMENTAL);
4121 __ bind(&skip_to_incremental_compacting);
4122 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4124 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
4125 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
4126 masm->set_byte_at(0, kTwoByteNopInstruction);
4127 masm->set_byte_at(2, kFiveByteNopInstruction);
4131 void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4134 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4135 Label dont_need_remembered_set;
4137 __ movp(regs_.scratch0(), Operand(regs_.address(), 0));
4138 __ JumpIfNotInNewSpace(regs_.scratch0(),
4140 &dont_need_remembered_set);
4142 __ CheckPageFlag(regs_.object(),
4144 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4146 &dont_need_remembered_set);
4148 // First notify the incremental marker if necessary, then update the
4150 CheckNeedsToInformIncrementalMarker(
4151 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
4152 InformIncrementalMarker(masm);
4153 regs_.Restore(masm);
4154 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4155 MacroAssembler::kReturnAtEnd);
4157 __ bind(&dont_need_remembered_set);
4160 CheckNeedsToInformIncrementalMarker(
4161 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
4162 InformIncrementalMarker(masm);
4163 regs_.Restore(masm);
4168 void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
4169 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
4171 arg_reg_1.is(regs_.address()) ? kScratchRegister : regs_.address();
4172 DCHECK(!address.is(regs_.object()));
4173 DCHECK(!address.is(arg_reg_1));
4174 __ Move(address, regs_.address());
4175 __ Move(arg_reg_1, regs_.object());
4176 // TODO(gc) Can we just set address arg2 in the beginning?
4177 __ Move(arg_reg_2, address);
4178 __ LoadAddress(arg_reg_3,
4179 ExternalReference::isolate_address(isolate()));
4180 int argument_count = 3;
4182 AllowExternalCallThatCantCauseGC scope(masm);
4183 __ PrepareCallCFunction(argument_count);
4185 ExternalReference::incremental_marking_record_write_function(isolate()),
4187 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
4191 void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4192 MacroAssembler* masm,
4193 OnNoNeedToInformIncrementalMarker on_no_need,
4196 Label need_incremental;
4197 Label need_incremental_pop_object;
4199 __ movp(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
4200 __ andp(regs_.scratch0(), regs_.object());
4201 __ movp(regs_.scratch1(),
4202 Operand(regs_.scratch0(),
4203 MemoryChunk::kWriteBarrierCounterOffset));
4204 __ subp(regs_.scratch1(), Immediate(1));
4205 __ movp(Operand(regs_.scratch0(),
4206 MemoryChunk::kWriteBarrierCounterOffset),
4208 __ j(negative, &need_incremental);
4210 // Let's look at the color of the object: If it is not black we don't have
4211 // to inform the incremental marker.
4212 __ JumpIfBlack(regs_.object(),
4218 regs_.Restore(masm);
4219 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4220 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4221 MacroAssembler::kReturnAtEnd);
4228 // Get the value from the slot.
4229 __ movp(regs_.scratch0(), Operand(regs_.address(), 0));
4231 if (mode == INCREMENTAL_COMPACTION) {
4232 Label ensure_not_white;
4234 __ CheckPageFlag(regs_.scratch0(), // Contains value.
4235 regs_.scratch1(), // Scratch.
4236 MemoryChunk::kEvacuationCandidateMask,
4241 __ CheckPageFlag(regs_.object(),
4242 regs_.scratch1(), // Scratch.
4243 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4247 __ bind(&ensure_not_white);
4250 // We need an extra register for this, so we push the object register
4252 __ Push(regs_.object());
4253 __ EnsureNotWhite(regs_.scratch0(), // The value.
4254 regs_.scratch1(), // Scratch.
4255 regs_.object(), // Scratch.
4256 &need_incremental_pop_object,
4258 __ Pop(regs_.object());
4260 regs_.Restore(masm);
4261 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4262 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
4263 MacroAssembler::kReturnAtEnd);
4268 __ bind(&need_incremental_pop_object);
4269 __ Pop(regs_.object());
4271 __ bind(&need_incremental);
4273 // Fall through when we need to inform the incremental marker.
4277 void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4278 // ----------- S t a t e -------------
4279 // -- rax : element value to store
4280 // -- rcx : element index as smi
4281 // -- rsp[0] : return address
4282 // -- rsp[8] : array literal index in function
4283 // -- rsp[16] : array literal
4284 // clobbers rbx, rdx, rdi
4285 // -----------------------------------
4288 Label double_elements;
4290 Label slow_elements;
4291 Label fast_elements;
4293 // Get array literal index, array literal and its map.
4294 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4295 __ movp(rdx, args.GetArgumentOperand(1));
4296 __ movp(rbx, args.GetArgumentOperand(0));
4297 __ movp(rdi, FieldOperand(rbx, JSObject::kMapOffset));
4299 __ CheckFastElements(rdi, &double_elements);
4301 // FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS
4302 __ JumpIfSmi(rax, &smi_element);
4303 __ CheckFastSmiElements(rdi, &fast_elements);
4305 // Store into the array literal requires a elements transition. Call into
4308 __ bind(&slow_elements);
4309 __ PopReturnAddressTo(rdi);
4313 __ movp(rbx, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
4314 __ Push(FieldOperand(rbx, JSFunction::kLiteralsOffset));
4316 __ PushReturnAddressFrom(rdi);
4317 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4319 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
4320 __ bind(&fast_elements);
4321 __ SmiToInteger32(kScratchRegister, rcx);
4322 __ movp(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
4323 __ leap(rcx, FieldOperand(rbx, kScratchRegister, times_pointer_size,
4324 FixedArrayBase::kHeaderSize));
4325 __ movp(Operand(rcx, 0), rax);
4326 // Update the write barrier for the array store.
4327 __ RecordWrite(rbx, rcx, rax,
4329 EMIT_REMEMBERED_SET,
4333 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or
4334 // FAST_*_ELEMENTS, and value is Smi.
4335 __ bind(&smi_element);
4336 __ SmiToInteger32(kScratchRegister, rcx);
4337 __ movp(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
4338 __ movp(FieldOperand(rbx, kScratchRegister, times_pointer_size,
4339 FixedArrayBase::kHeaderSize), rax);
4342 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
4343 __ bind(&double_elements);
4345 __ movp(r9, FieldOperand(rbx, JSObject::kElementsOffset));
4346 __ SmiToInteger32(r11, rcx);
4347 __ StoreNumberToDoubleElements(rax,
4356 void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4357 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4358 __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
4359 int parameter_count_offset =
4360 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4361 __ movp(rbx, MemOperand(rbp, parameter_count_offset));
4362 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4363 __ PopReturnAddressTo(rcx);
4364 int additional_offset =
4365 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
4366 __ leap(rsp, MemOperand(rsp, rbx, times_pointer_size, additional_offset));
4367 __ jmp(rcx); // Return to IC Miss stub, continuation still on stack.
4371 void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4372 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4373 LoadICStub stub(isolate(), state());
4374 stub.GenerateForTrampoline(masm);
4378 void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4379 EmitLoadTypeFeedbackVector(masm, LoadWithVectorDescriptor::VectorRegister());
4380 KeyedLoadICStub stub(isolate(), state());
4381 stub.GenerateForTrampoline(masm);
4385 static void HandleArrayCases(MacroAssembler* masm, Register receiver,
4386 Register key, Register vector, Register slot,
4387 Register feedback, Register receiver_map,
4388 Register scratch1, Register scratch2,
4389 Register scratch3, bool is_polymorphic,
4391 // feedback initially contains the feedback array
4392 Label next_loop, prepare_next;
4393 Label start_polymorphic;
4395 Register counter = scratch1;
4396 Register length = scratch2;
4397 Register cached_map = scratch3;
4399 __ movp(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
4400 __ cmpp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4401 __ j(not_equal, &start_polymorphic);
4403 // found, now call handler.
4404 Register handler = feedback;
4405 __ movp(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
4406 __ leap(handler, FieldOperand(handler, Code::kHeaderSize));
4409 // Polymorphic, we have to loop from 2 to N
4410 __ bind(&start_polymorphic);
4411 __ SmiToInteger32(length, FieldOperand(feedback, FixedArray::kLengthOffset));
4412 if (!is_polymorphic) {
4413 // If the IC could be monomorphic we have to make sure we don't go past the
4414 // end of the feedback array.
4415 __ cmpl(length, Immediate(2));
4418 __ movl(counter, Immediate(2));
4420 __ bind(&next_loop);
4421 __ movp(cached_map, FieldOperand(feedback, counter, times_pointer_size,
4422 FixedArray::kHeaderSize));
4423 __ cmpp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4424 __ j(not_equal, &prepare_next);
4425 __ movp(handler, FieldOperand(feedback, counter, times_pointer_size,
4426 FixedArray::kHeaderSize + kPointerSize));
4427 __ leap(handler, FieldOperand(handler, Code::kHeaderSize));
4430 __ bind(&prepare_next);
4431 __ addl(counter, Immediate(2));
4432 __ cmpl(counter, length);
4433 __ j(less, &next_loop);
4435 // We exhausted our array of map handler pairs.
4440 static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
4441 Register receiver_map, Register feedback,
4442 Register vector, Register integer_slot,
4443 Label* compare_map, Label* load_smi_map,
4445 __ JumpIfSmi(receiver, load_smi_map);
4446 __ movp(receiver_map, FieldOperand(receiver, 0));
4448 __ bind(compare_map);
4449 __ cmpp(receiver_map, FieldOperand(feedback, WeakCell::kValueOffset));
4450 __ j(not_equal, try_array);
4451 Register handler = feedback;
4452 __ movp(handler, FieldOperand(vector, integer_slot, times_pointer_size,
4453 FixedArray::kHeaderSize + kPointerSize));
4454 __ leap(handler, FieldOperand(handler, Code::kHeaderSize));
4459 void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
4462 void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4463 GenerateImpl(masm, true);
4467 void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4468 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // rdx
4469 Register name = LoadWithVectorDescriptor::NameRegister(); // rcx
4470 Register vector = LoadWithVectorDescriptor::VectorRegister(); // rbx
4471 Register slot = LoadWithVectorDescriptor::SlotRegister(); // rax
4472 Register feedback = rdi;
4473 Register integer_slot = r8;
4474 Register receiver_map = r9;
4476 __ SmiToInteger32(integer_slot, slot);
4477 __ movp(feedback, FieldOperand(vector, integer_slot, times_pointer_size,
4478 FixedArray::kHeaderSize));
4480 // Try to quickly handle the monomorphic case without knowing for sure
4481 // if we have a weak cell in feedback. We do know it's safe to look
4482 // at WeakCell::kValueOffset.
4483 Label try_array, load_smi_map, compare_map;
4484 Label not_array, miss;
4485 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector,
4486 integer_slot, &compare_map, &load_smi_map, &try_array);
4488 // Is it a fixed array?
4489 __ bind(&try_array);
4490 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
4491 __ j(not_equal, ¬_array);
4492 HandleArrayCases(masm, receiver, name, vector, slot, feedback, receiver_map,
4493 integer_slot, r11, r15, true, &miss);
4495 __ bind(¬_array);
4496 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
4497 __ j(not_equal, &miss);
4498 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4499 Code::ComputeHandlerFlags(Code::LOAD_IC));
4500 masm->isolate()->stub_cache()->GenerateProbe(
4501 masm, Code::LOAD_IC, code_flags, false, receiver, name, feedback, no_reg);
4504 LoadIC::GenerateMiss(masm);
4506 __ bind(&load_smi_map);
4507 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4508 __ jmp(&compare_map);
4512 void KeyedLoadICStub::Generate(MacroAssembler* masm) {
4513 GenerateImpl(masm, false);
4517 void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4518 GenerateImpl(masm, true);
4522 void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4523 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // rdx
4524 Register key = LoadWithVectorDescriptor::NameRegister(); // rcx
4525 Register vector = LoadWithVectorDescriptor::VectorRegister(); // rbx
4526 Register slot = LoadWithVectorDescriptor::SlotRegister(); // rax
4527 Register feedback = rdi;
4528 Register integer_slot = r8;
4529 Register receiver_map = r9;
4531 __ SmiToInteger32(integer_slot, slot);
4532 __ movp(feedback, FieldOperand(vector, integer_slot, times_pointer_size,
4533 FixedArray::kHeaderSize));
4535 // Try to quickly handle the monomorphic case without knowing for sure
4536 // if we have a weak cell in feedback. We do know it's safe to look
4537 // at WeakCell::kValueOffset.
4538 Label try_array, load_smi_map, compare_map;
4539 Label not_array, miss;
4540 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector,
4541 integer_slot, &compare_map, &load_smi_map, &try_array);
4543 __ bind(&try_array);
4544 // Is it a fixed array?
4545 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
4546 __ j(not_equal, ¬_array);
4548 // We have a polymorphic element handler.
4549 Label polymorphic, try_poly_name;
4550 __ bind(&polymorphic);
4551 HandleArrayCases(masm, receiver, key, vector, slot, feedback, receiver_map,
4552 integer_slot, r11, r15, true, &miss);
4554 __ bind(¬_array);
4556 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
4557 __ j(not_equal, &try_poly_name);
4558 Handle<Code> megamorphic_stub =
4559 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4560 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4562 __ bind(&try_poly_name);
4563 // We might have a name in feedback, and a fixed array in the next slot.
4564 __ cmpp(key, feedback);
4565 __ j(not_equal, &miss);
4566 // If the name comparison succeeded, we know we have a fixed array with
4567 // at least one map/handler pair.
4568 __ movp(feedback, FieldOperand(vector, integer_slot, times_pointer_size,
4569 FixedArray::kHeaderSize + kPointerSize));
4570 HandleArrayCases(masm, receiver, key, vector, slot, feedback, receiver_map,
4571 integer_slot, r11, r15, false, &miss);
4574 KeyedLoadIC::GenerateMiss(masm);
4576 __ bind(&load_smi_map);
4577 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4578 __ jmp(&compare_map);
4582 void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4583 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4584 VectorStoreICStub stub(isolate(), state());
4585 stub.GenerateForTrampoline(masm);
4589 void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4590 EmitLoadTypeFeedbackVector(masm, VectorStoreICDescriptor::VectorRegister());
4591 VectorKeyedStoreICStub stub(isolate(), state());
4592 stub.GenerateForTrampoline(masm);
4596 void VectorStoreICStub::Generate(MacroAssembler* masm) {
4597 GenerateImpl(masm, false);
4601 void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4602 GenerateImpl(masm, true);
4606 void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4609 // TODO(mvstanton): Implement.
4611 StoreIC::GenerateMiss(masm);
4615 void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4616 GenerateImpl(masm, false);
4620 void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4621 GenerateImpl(masm, true);
4625 void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4628 // TODO(mvstanton): Implement.
4630 KeyedStoreIC::GenerateMiss(masm);
4634 void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4635 EmitLoadTypeFeedbackVector(masm, rbx);
4636 CallICStub stub(isolate(), state());
4637 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4641 void CallIC_ArrayTrampolineStub::Generate(MacroAssembler* masm) {
4642 EmitLoadTypeFeedbackVector(masm, rbx);
4643 CallIC_ArrayStub stub(isolate(), state());
4644 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4648 void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4649 if (masm->isolate()->function_entry_hook() != NULL) {
4650 ProfileEntryHookStub stub(masm->isolate());
4651 masm->CallStub(&stub);
4656 void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4657 // This stub can be called from essentially anywhere, so it needs to save
4658 // all volatile and callee-save registers.
4659 const size_t kNumSavedRegisters = 2;
4660 __ pushq(arg_reg_1);
4661 __ pushq(arg_reg_2);
4663 // Calculate the original stack pointer and store it in the second arg.
4665 Operand(rsp, kNumSavedRegisters * kRegisterSize + kPCOnStackSize));
4667 // Calculate the function address to the first arg.
4668 __ movp(arg_reg_1, Operand(rsp, kNumSavedRegisters * kRegisterSize));
4669 __ subp(arg_reg_1, Immediate(Assembler::kShortCallInstructionLength));
4671 // Save the remainder of the volatile registers.
4672 masm->PushCallerSaved(kSaveFPRegs, arg_reg_1, arg_reg_2);
4674 // Call the entry hook function.
4675 __ Move(rax, FUNCTION_ADDR(isolate()->function_entry_hook()),
4676 Assembler::RelocInfoNone());
4678 AllowExternalCallThatCantCauseGC scope(masm);
4680 const int kArgumentCount = 2;
4681 __ PrepareCallCFunction(kArgumentCount);
4682 __ CallCFunction(rax, kArgumentCount);
4684 // Restore volatile regs.
4685 masm->PopCallerSaved(kSaveFPRegs, arg_reg_1, arg_reg_2);
4694 static void CreateArrayDispatch(MacroAssembler* masm,
4695 AllocationSiteOverrideMode mode) {
4696 if (mode == DISABLE_ALLOCATION_SITES) {
4697 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
4698 __ TailCallStub(&stub);
4699 } else if (mode == DONT_OVERRIDE) {
4700 int last_index = GetSequenceIndexFromFastElementsKind(
4701 TERMINAL_FAST_ELEMENTS_KIND);
4702 for (int i = 0; i <= last_index; ++i) {
4704 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4705 __ cmpl(rdx, Immediate(kind));
4706 __ j(not_equal, &next);
4707 T stub(masm->isolate(), kind);
4708 __ TailCallStub(&stub);
4712 // If we reached this point there is a problem.
4713 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4720 static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4721 AllocationSiteOverrideMode mode) {
4722 // rbx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4723 // rdx - kind (if mode != DISABLE_ALLOCATION_SITES)
4724 // rax - number of arguments
4725 // rdi - constructor?
4726 // rsp[0] - return address
4727 // rsp[8] - last argument
4728 Handle<Object> undefined_sentinel(
4729 masm->isolate()->heap()->undefined_value(),
4732 Label normal_sequence;
4733 if (mode == DONT_OVERRIDE) {
4734 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4735 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4736 STATIC_ASSERT(FAST_ELEMENTS == 2);
4737 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4738 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4739 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4741 // is the low bit set? If so, we are holey and that is good.
4742 __ testb(rdx, Immediate(1));
4743 __ j(not_zero, &normal_sequence);
4746 // look at the first argument
4747 StackArgumentsAccessor args(rsp, 1, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4748 __ movp(rcx, args.GetArgumentOperand(0));
4750 __ j(zero, &normal_sequence);
4752 if (mode == DISABLE_ALLOCATION_SITES) {
4753 ElementsKind initial = GetInitialFastElementsKind();
4754 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4756 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4758 DISABLE_ALLOCATION_SITES);
4759 __ TailCallStub(&stub_holey);
4761 __ bind(&normal_sequence);
4762 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4764 DISABLE_ALLOCATION_SITES);
4765 __ TailCallStub(&stub);
4766 } else if (mode == DONT_OVERRIDE) {
4767 // We are going to create a holey array, but our kind is non-holey.
4768 // Fix kind and retry (only if we have an allocation site in the slot).
4771 if (FLAG_debug_code) {
4772 Handle<Map> allocation_site_map =
4773 masm->isolate()->factory()->allocation_site_map();
4774 __ Cmp(FieldOperand(rbx, 0), allocation_site_map);
4775 __ Assert(equal, kExpectedAllocationSite);
4778 // Save the resulting elements kind in type info. We can't just store r3
4779 // in the AllocationSite::transition_info field because elements kind is
4780 // restricted to a portion of the field...upper bits need to be left alone.
4781 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4782 __ SmiAddConstant(FieldOperand(rbx, AllocationSite::kTransitionInfoOffset),
4783 Smi::FromInt(kFastElementsKindPackedToHoley));
4785 __ bind(&normal_sequence);
4786 int last_index = GetSequenceIndexFromFastElementsKind(
4787 TERMINAL_FAST_ELEMENTS_KIND);
4788 for (int i = 0; i <= last_index; ++i) {
4790 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4791 __ cmpl(rdx, Immediate(kind));
4792 __ j(not_equal, &next);
4793 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4794 __ TailCallStub(&stub);
4798 // If we reached this point there is a problem.
4799 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4807 static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4808 int to_index = GetSequenceIndexFromFastElementsKind(
4809 TERMINAL_FAST_ELEMENTS_KIND);
4810 for (int i = 0; i <= to_index; ++i) {
4811 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4812 T stub(isolate, kind);
4814 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4815 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4822 void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4823 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4825 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4827 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4832 void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4834 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4835 for (int i = 0; i < 2; i++) {
4836 // For internal arrays we only need a few things
4837 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4839 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4841 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4847 void ArrayConstructorStub::GenerateDispatchToArrayStub(
4848 MacroAssembler* masm,
4849 AllocationSiteOverrideMode mode) {
4850 if (argument_count() == ANY) {
4851 Label not_zero_case, not_one_case;
4853 __ j(not_zero, ¬_zero_case);
4854 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4856 __ bind(¬_zero_case);
4857 __ cmpl(rax, Immediate(1));
4858 __ j(greater, ¬_one_case);
4859 CreateArrayDispatchOneArgument(masm, mode);
4861 __ bind(¬_one_case);
4862 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4863 } else if (argument_count() == NONE) {
4864 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4865 } else if (argument_count() == ONE) {
4866 CreateArrayDispatchOneArgument(masm, mode);
4867 } else if (argument_count() == MORE_THAN_ONE) {
4868 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4875 void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4876 // ----------- S t a t e -------------
4878 // -- rbx : AllocationSite or undefined
4879 // -- rdi : constructor
4880 // -- rdx : original constructor
4881 // -- rsp[0] : return address
4882 // -- rsp[8] : last argument
4883 // -----------------------------------
4884 if (FLAG_debug_code) {
4885 // The array construct code is only set for the global and natives
4886 // builtin Array functions which always have maps.
4888 // Initial map for the builtin Array function should be a map.
4889 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
4890 // Will both indicate a NULL and a Smi.
4891 STATIC_ASSERT(kSmiTag == 0);
4892 Condition not_smi = NegateCondition(masm->CheckSmi(rcx));
4893 __ Check(not_smi, kUnexpectedInitialMapForArrayFunction);
4894 __ CmpObjectType(rcx, MAP_TYPE, rcx);
4895 __ Check(equal, kUnexpectedInitialMapForArrayFunction);
4897 // We should either have undefined in rbx or a valid AllocationSite
4898 __ AssertUndefinedOrAllocationSite(rbx);
4903 __ j(not_equal, &subclassing);
4906 // If the feedback vector is the undefined value call an array constructor
4907 // that doesn't use AllocationSites.
4908 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
4909 __ j(equal, &no_info);
4911 // Only look at the lower 16 bits of the transition info.
4912 __ movp(rdx, FieldOperand(rbx, AllocationSite::kTransitionInfoOffset));
4913 __ SmiToInteger32(rdx, rdx);
4914 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4915 __ andp(rdx, Immediate(AllocationSite::ElementsKindBits::kMask));
4916 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4919 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
4922 __ bind(&subclassing);
4923 __ Pop(rcx); // return address.
4928 switch (argument_count()) {
4931 __ addp(rax, Immediate(2));
4934 __ movp(rax, Immediate(2));
4937 __ movp(rax, Immediate(3));
4942 __ JumpToExternalReference(
4943 ExternalReference(Runtime::kArrayConstructorWithSubclassing, isolate()),
4948 void InternalArrayConstructorStub::GenerateCase(
4949 MacroAssembler* masm, ElementsKind kind) {
4950 Label not_zero_case, not_one_case;
4951 Label normal_sequence;
4954 __ j(not_zero, ¬_zero_case);
4955 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4956 __ TailCallStub(&stub0);
4958 __ bind(¬_zero_case);
4959 __ cmpl(rax, Immediate(1));
4960 __ j(greater, ¬_one_case);
4962 if (IsFastPackedElementsKind(kind)) {
4963 // We might need to create a holey array
4964 // look at the first argument
4965 StackArgumentsAccessor args(rsp, 1, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4966 __ movp(rcx, args.GetArgumentOperand(0));
4968 __ j(zero, &normal_sequence);
4970 InternalArraySingleArgumentConstructorStub
4971 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4972 __ TailCallStub(&stub1_holey);
4975 __ bind(&normal_sequence);
4976 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4977 __ TailCallStub(&stub1);
4979 __ bind(¬_one_case);
4980 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4981 __ TailCallStub(&stubN);
4985 void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4986 // ----------- S t a t e -------------
4988 // -- rdi : constructor
4989 // -- rsp[0] : return address
4990 // -- rsp[8] : last argument
4991 // -----------------------------------
4993 if (FLAG_debug_code) {
4994 // The array construct code is only set for the global and natives
4995 // builtin Array functions which always have maps.
4997 // Initial map for the builtin Array function should be a map.
4998 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
4999 // Will both indicate a NULL and a Smi.
5000 STATIC_ASSERT(kSmiTag == 0);
5001 Condition not_smi = NegateCondition(masm->CheckSmi(rcx));
5002 __ Check(not_smi, kUnexpectedInitialMapForArrayFunction);
5003 __ CmpObjectType(rcx, MAP_TYPE, rcx);
5004 __ Check(equal, kUnexpectedInitialMapForArrayFunction);
5007 // Figure out the right elements kind
5008 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
5010 // Load the map's "bit field 2" into |result|. We only need the first byte,
5011 // but the following masking takes care of that anyway.
5012 __ movzxbp(rcx, FieldOperand(rcx, Map::kBitField2Offset));
5013 // Retrieve elements_kind from bit field 2.
5014 __ DecodeField<Map::ElementsKindBits>(rcx);
5016 if (FLAG_debug_code) {
5018 __ cmpl(rcx, Immediate(FAST_ELEMENTS));
5020 __ cmpl(rcx, Immediate(FAST_HOLEY_ELEMENTS));
5022 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
5026 Label fast_elements_case;
5027 __ cmpl(rcx, Immediate(FAST_ELEMENTS));
5028 __ j(equal, &fast_elements_case);
5029 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
5031 __ bind(&fast_elements_case);
5032 GenerateCase(masm, FAST_ELEMENTS);
5036 static int Offset(ExternalReference ref0, ExternalReference ref1) {
5037 int64_t offset = (ref0.address() - ref1.address());
5038 // Check that fits into int.
5039 DCHECK(static_cast<int>(offset) == offset);
5040 return static_cast<int>(offset);
5044 // Prepares stack to put arguments (aligns and so on). WIN64 calling
5045 // convention requires to put the pointer to the return value slot into
5046 // rcx (rcx must be preserverd until CallApiFunctionAndReturn). Saves
5047 // context (rsi). Clobbers rax. Allocates arg_stack_space * kPointerSize
5048 // inside the exit frame (not GCed) accessible via StackSpaceOperand.
5049 static void PrepareCallApiFunction(MacroAssembler* masm, int arg_stack_space) {
5050 __ EnterApiExitFrame(arg_stack_space);
5054 // Calls an API function. Allocates HandleScope, extracts returned value
5055 // from handle and propagates exceptions. Clobbers r14, r15, rbx and
5056 // caller-save registers. Restores context. On return removes
5057 // stack_space * kPointerSize (GCed).
5058 static void CallApiFunctionAndReturn(MacroAssembler* masm,
5059 Register function_address,
5060 ExternalReference thunk_ref,
5061 Register thunk_last_arg, int stack_space,
5062 Operand* stack_space_operand,
5063 Operand return_value_operand,
5064 Operand* context_restore_operand) {
5066 Label promote_scheduled_exception;
5067 Label delete_allocated_handles;
5068 Label leave_exit_frame;
5071 Isolate* isolate = masm->isolate();
5072 Factory* factory = isolate->factory();
5073 ExternalReference next_address =
5074 ExternalReference::handle_scope_next_address(isolate);
5075 const int kNextOffset = 0;
5076 const int kLimitOffset = Offset(
5077 ExternalReference::handle_scope_limit_address(isolate), next_address);
5078 const int kLevelOffset = Offset(
5079 ExternalReference::handle_scope_level_address(isolate), next_address);
5080 ExternalReference scheduled_exception_address =
5081 ExternalReference::scheduled_exception_address(isolate);
5083 DCHECK(rdx.is(function_address) || r8.is(function_address));
5084 // Allocate HandleScope in callee-save registers.
5085 Register prev_next_address_reg = r14;
5086 Register prev_limit_reg = rbx;
5087 Register base_reg = r15;
5088 __ Move(base_reg, next_address);
5089 __ movp(prev_next_address_reg, Operand(base_reg, kNextOffset));
5090 __ movp(prev_limit_reg, Operand(base_reg, kLimitOffset));
5091 __ addl(Operand(base_reg, kLevelOffset), Immediate(1));
5093 if (FLAG_log_timer_events) {
5094 FrameScope frame(masm, StackFrame::MANUAL);
5095 __ PushSafepointRegisters();
5096 __ PrepareCallCFunction(1);
5097 __ LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate));
5098 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5100 __ PopSafepointRegisters();
5103 Label profiler_disabled;
5104 Label end_profiler_check;
5105 __ Move(rax, ExternalReference::is_profiling_address(isolate));
5106 __ cmpb(Operand(rax, 0), Immediate(0));
5107 __ j(zero, &profiler_disabled);
5109 // Third parameter is the address of the actual getter function.
5110 __ Move(thunk_last_arg, function_address);
5111 __ Move(rax, thunk_ref);
5112 __ jmp(&end_profiler_check);
5114 __ bind(&profiler_disabled);
5115 // Call the api function!
5116 __ Move(rax, function_address);
5118 __ bind(&end_profiler_check);
5120 // Call the api function!
5123 if (FLAG_log_timer_events) {
5124 FrameScope frame(masm, StackFrame::MANUAL);
5125 __ PushSafepointRegisters();
5126 __ PrepareCallCFunction(1);
5127 __ LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate));
5128 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5130 __ PopSafepointRegisters();
5133 // Load the value from ReturnValue
5134 __ movp(rax, return_value_operand);
5137 // No more valid handles (the result handle was the last one). Restore
5138 // previous handle scope.
5139 __ subl(Operand(base_reg, kLevelOffset), Immediate(1));
5140 __ movp(Operand(base_reg, kNextOffset), prev_next_address_reg);
5141 __ cmpp(prev_limit_reg, Operand(base_reg, kLimitOffset));
5142 __ j(not_equal, &delete_allocated_handles);
5144 // Leave the API exit frame.
5145 __ bind(&leave_exit_frame);
5146 bool restore_context = context_restore_operand != NULL;
5147 if (restore_context) {
5148 __ movp(rsi, *context_restore_operand);
5150 if (stack_space_operand != nullptr) {
5151 __ movp(rbx, *stack_space_operand);
5153 __ LeaveApiExitFrame(!restore_context);
5155 // Check if the function scheduled an exception.
5156 __ Move(rdi, scheduled_exception_address);
5157 __ Cmp(Operand(rdi, 0), factory->the_hole_value());
5158 __ j(not_equal, &promote_scheduled_exception);
5161 // Check if the function returned a valid JavaScript value.
5163 Register return_value = rax;
5166 __ JumpIfSmi(return_value, &ok, Label::kNear);
5167 __ movp(map, FieldOperand(return_value, HeapObject::kMapOffset));
5169 __ CmpInstanceType(map, LAST_NAME_TYPE);
5170 __ j(below_equal, &ok, Label::kNear);
5172 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
5173 __ j(above_equal, &ok, Label::kNear);
5175 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
5176 __ j(equal, &ok, Label::kNear);
5178 __ CompareRoot(return_value, Heap::kUndefinedValueRootIndex);
5179 __ j(equal, &ok, Label::kNear);
5181 __ CompareRoot(return_value, Heap::kTrueValueRootIndex);
5182 __ j(equal, &ok, Label::kNear);
5184 __ CompareRoot(return_value, Heap::kFalseValueRootIndex);
5185 __ j(equal, &ok, Label::kNear);
5187 __ CompareRoot(return_value, Heap::kNullValueRootIndex);
5188 __ j(equal, &ok, Label::kNear);
5190 __ Abort(kAPICallReturnedInvalidObject);
5195 if (stack_space_operand != nullptr) {
5196 DCHECK_EQ(stack_space, 0);
5197 __ PopReturnAddressTo(rcx);
5201 __ ret(stack_space * kPointerSize);
5204 // Re-throw by promoting a scheduled exception.
5205 __ bind(&promote_scheduled_exception);
5206 __ TailCallRuntime(Runtime::kPromoteScheduledException, 0, 1);
5208 // HandleScope limit has changed. Delete allocated extensions.
5209 __ bind(&delete_allocated_handles);
5210 __ movp(Operand(base_reg, kLimitOffset), prev_limit_reg);
5211 __ movp(prev_limit_reg, rax);
5212 __ LoadAddress(arg_reg_1, ExternalReference::isolate_address(isolate));
5214 ExternalReference::delete_handle_scope_extensions(isolate));
5216 __ movp(rax, prev_limit_reg);
5217 __ jmp(&leave_exit_frame);
5221 static void CallApiFunctionStubHelper(MacroAssembler* masm,
5222 const ParameterCount& argc,
5223 bool return_first_arg,
5224 bool call_data_undefined) {
5225 // ----------- S t a t e -------------
5227 // -- rbx : call_data
5229 // -- rdx : api_function_address
5231 // -- rax : number of arguments if argc is a register
5232 // -- rsp[0] : return address
5233 // -- rsp[8] : last argument
5235 // -- rsp[argc * 8] : first argument
5236 // -- rsp[(argc + 1) * 8] : receiver
5237 // -----------------------------------
5239 Register callee = rdi;
5240 Register call_data = rbx;
5241 Register holder = rcx;
5242 Register api_function_address = rdx;
5243 Register context = rsi;
5244 Register return_address = r8;
5246 typedef FunctionCallbackArguments FCA;
5248 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5249 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5250 STATIC_ASSERT(FCA::kDataIndex == 4);
5251 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5252 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5253 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5254 STATIC_ASSERT(FCA::kHolderIndex == 0);
5255 STATIC_ASSERT(FCA::kArgsLength == 7);
5257 DCHECK(argc.is_immediate() || rax.is(argc.reg()));
5259 __ PopReturnAddressTo(return_address);
5269 Register scratch = call_data;
5270 if (!call_data_undefined) {
5271 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5275 // return value default
5278 __ Move(scratch, ExternalReference::isolate_address(masm->isolate()));
5283 __ movp(scratch, rsp);
5284 // Push return address back on stack.
5285 __ PushReturnAddressFrom(return_address);
5287 // load context from callee
5288 __ movp(context, FieldOperand(callee, JSFunction::kContextOffset));
5290 // Allocate the v8::Arguments structure in the arguments' space since
5291 // it's not controlled by GC.
5292 const int kApiStackSpace = 4;
5294 PrepareCallApiFunction(masm, kApiStackSpace);
5296 // FunctionCallbackInfo::implicit_args_.
5297 __ movp(StackSpaceOperand(0), scratch);
5298 if (argc.is_immediate()) {
5299 __ addp(scratch, Immediate((argc.immediate() + FCA::kArgsLength - 1) *
5301 // FunctionCallbackInfo::values_.
5302 __ movp(StackSpaceOperand(1), scratch);
5303 // FunctionCallbackInfo::length_.
5304 __ Set(StackSpaceOperand(2), argc.immediate());
5305 // FunctionCallbackInfo::is_construct_call_.
5306 __ Set(StackSpaceOperand(3), 0);
5308 __ leap(scratch, Operand(scratch, argc.reg(), times_pointer_size,
5309 (FCA::kArgsLength - 1) * kPointerSize));
5310 // FunctionCallbackInfo::values_.
5311 __ movp(StackSpaceOperand(1), scratch);
5312 // FunctionCallbackInfo::length_.
5313 __ movp(StackSpaceOperand(2), argc.reg());
5314 // FunctionCallbackInfo::is_construct_call_.
5315 __ leap(argc.reg(), Operand(argc.reg(), times_pointer_size,
5316 (FCA::kArgsLength + 1) * kPointerSize));
5317 __ movp(StackSpaceOperand(3), argc.reg());
5320 #if defined(__MINGW64__) || defined(_WIN64)
5321 Register arguments_arg = rcx;
5322 Register callback_arg = rdx;
5324 Register arguments_arg = rdi;
5325 Register callback_arg = rsi;
5328 // It's okay if api_function_address == callback_arg
5329 // but not arguments_arg
5330 DCHECK(!api_function_address.is(arguments_arg));
5332 // v8::InvocationCallback's argument.
5333 __ leap(arguments_arg, StackSpaceOperand(0));
5335 ExternalReference thunk_ref =
5336 ExternalReference::invoke_function_callback(masm->isolate());
5338 // Accessor for FunctionCallbackInfo and first js arg.
5339 StackArgumentsAccessor args_from_rbp(rbp, FCA::kArgsLength + 1,
5340 ARGUMENTS_DONT_CONTAIN_RECEIVER);
5341 Operand context_restore_operand = args_from_rbp.GetArgumentOperand(
5342 FCA::kArgsLength - FCA::kContextSaveIndex);
5343 Operand is_construct_call_operand = StackSpaceOperand(3);
5344 Operand return_value_operand = args_from_rbp.GetArgumentOperand(
5345 return_first_arg ? 0 : FCA::kArgsLength - FCA::kReturnValueOffset);
5346 int stack_space = 0;
5347 Operand* stack_space_operand = &is_construct_call_operand;
5348 if (argc.is_immediate()) {
5349 stack_space = argc.immediate() + FCA::kArgsLength + 1;
5350 stack_space_operand = nullptr;
5352 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, callback_arg,
5353 stack_space, stack_space_operand,
5354 return_value_operand, &context_restore_operand);
5358 void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5359 bool call_data_undefined = this->call_data_undefined();
5360 CallApiFunctionStubHelper(masm, ParameterCount(rax), false,
5361 call_data_undefined);
5365 void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5366 bool is_store = this->is_store();
5367 int argc = this->argc();
5368 bool call_data_undefined = this->call_data_undefined();
5369 CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
5370 call_data_undefined);
5374 void CallApiGetterStub::Generate(MacroAssembler* masm) {
5375 // ----------- S t a t e -------------
5376 // -- rsp[0] : return address
5378 // -- rsp[16 - kArgsLength*8] : PropertyCallbackArguments object
5380 // -- r8 : api_function_address
5381 // -----------------------------------
5383 #if defined(__MINGW64__) || defined(_WIN64)
5384 Register getter_arg = r8;
5385 Register accessor_info_arg = rdx;
5386 Register name_arg = rcx;
5388 Register getter_arg = rdx;
5389 Register accessor_info_arg = rsi;
5390 Register name_arg = rdi;
5392 Register api_function_address = ApiGetterDescriptor::function_address();
5393 DCHECK(api_function_address.is(r8));
5394 Register scratch = rax;
5396 // v8::Arguments::values_ and handler for name.
5397 const int kStackSpace = PropertyCallbackArguments::kArgsLength + 1;
5399 // Allocate v8::AccessorInfo in non-GCed stack space.
5400 const int kArgStackSpace = 1;
5402 __ leap(name_arg, Operand(rsp, kPCOnStackSize));
5404 PrepareCallApiFunction(masm, kArgStackSpace);
5405 __ leap(scratch, Operand(name_arg, 1 * kPointerSize));
5407 // v8::PropertyAccessorInfo::args_.
5408 __ movp(StackSpaceOperand(0), scratch);
5410 // The context register (rsi) has been saved in PrepareCallApiFunction and
5411 // could be used to pass arguments.
5412 __ leap(accessor_info_arg, StackSpaceOperand(0));
5414 ExternalReference thunk_ref =
5415 ExternalReference::invoke_accessor_getter_callback(isolate());
5417 // It's okay if api_function_address == getter_arg
5418 // but not accessor_info_arg or name_arg
5419 DCHECK(!api_function_address.is(accessor_info_arg) &&
5420 !api_function_address.is(name_arg));
5422 // The name handler is counted as an argument.
5423 StackArgumentsAccessor args(rbp, PropertyCallbackArguments::kArgsLength);
5424 Operand return_value_operand = args.GetArgumentOperand(
5425 PropertyCallbackArguments::kArgsLength - 1 -
5426 PropertyCallbackArguments::kReturnValueOffset);
5427 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, getter_arg,
5428 kStackSpace, nullptr, return_value_operand, NULL);
5434 } // namespace internal
5437 #endif // V8_TARGET_ARCH_X64