blob: a62526941f24fda55f38a9272c78f84dce94d90b [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// 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.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_X64
Kristian Monsen80d68ea2010-09-08 11:05:35 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#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/isolate.h"
15#include "src/jsregexp.h"
16#include "src/regexp-macro-assembler.h"
17#include "src/runtime.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010018
19namespace v8 {
20namespace internal {
21
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022
23static void InitializeArrayConstructorDescriptor(
24 Isolate* isolate, CodeStubDescriptor* descriptor,
25 int constant_stack_parameter_count) {
26 Address deopt_handler = Runtime::FunctionForId(
27 Runtime::kArrayConstructor)->entry;
28
29 if (constant_stack_parameter_count == 0) {
30 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
31 JS_FUNCTION_STUB_MODE);
32 } else {
33 descriptor->Initialize(rax, deopt_handler, constant_stack_parameter_count,
34 JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
35 }
36}
37
38
39static void InitializeInternalArrayConstructorDescriptor(
40 Isolate* isolate, CodeStubDescriptor* descriptor,
41 int constant_stack_parameter_count) {
42 Address deopt_handler = Runtime::FunctionForId(
43 Runtime::kInternalArrayConstructor)->entry;
44
45 if (constant_stack_parameter_count == 0) {
46 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
47 JS_FUNCTION_STUB_MODE);
48 } else {
49 descriptor->Initialize(rax, deopt_handler, constant_stack_parameter_count,
50 JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
51 }
52}
53
54
55void ArrayNoArgumentConstructorStub::InitializeDescriptor(
56 CodeStubDescriptor* descriptor) {
57 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
58}
59
60
61void ArraySingleArgumentConstructorStub::InitializeDescriptor(
62 CodeStubDescriptor* descriptor) {
63 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
64}
65
66
67void ArrayNArgumentsConstructorStub::InitializeDescriptor(
68 CodeStubDescriptor* descriptor) {
69 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
70}
71
72
73void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
74 CodeStubDescriptor* descriptor) {
75 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
76}
77
78
79void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
80 CodeStubDescriptor* descriptor) {
81 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
82}
83
84
85void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
86 CodeStubDescriptor* descriptor) {
87 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
88}
89
90
Kristian Monsen80d68ea2010-09-08 11:05:35 +010091#define __ ACCESS_MASM(masm)
Steve Block1e0659c2011-05-24 12:43:12 +010092
Ben Murdochb8a8cc12014-11-26 15:28:44 +000093
94void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
95 ExternalReference miss) {
96 // Update the static counter each time a new code stub is generated.
97 isolate()->counters()->code_stubs()->Increment();
98
99 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
100 int param_count = descriptor.GetEnvironmentParameterCount();
101 {
102 // Call the runtime system in a fresh internal frame.
103 FrameScope scope(masm, StackFrame::INTERNAL);
104 DCHECK(param_count == 0 ||
105 rax.is(descriptor.GetEnvironmentParameterRegister(param_count - 1)));
106 // Push arguments
107 for (int i = 0; i < param_count; ++i) {
108 __ Push(descriptor.GetEnvironmentParameterRegister(i));
109 }
110 __ CallExternalReference(miss, param_count);
111 }
112
Steve Block1e0659c2011-05-24 12:43:12 +0100113 __ Ret();
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000114}
115
116
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100117void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 __ PushCallerSaved(save_doubles() ? kSaveFPRegs : kDontSaveFPRegs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100119 const int argument_count = 1;
120 __ PrepareCallCFunction(argument_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 __ LoadAddress(arg_reg_1,
122 ExternalReference::isolate_address(isolate()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100123
124 AllowExternalCallThatCantCauseGC scope(masm);
125 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000126 ExternalReference::store_buffer_overflow_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100127 argument_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 __ PopCallerSaved(save_doubles() ? kSaveFPRegs : kDontSaveFPRegs);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100129 __ ret(0);
130}
131
132
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100133class FloatingPointHelper : public AllStatic {
134 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 enum ConvertUndefined {
136 CONVERT_UNDEFINED_TO_ZERO,
137 BAILOUT_ON_UNDEFINED
138 };
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100139 // Load the operands from rdx and rax into xmm0 and xmm1, as doubles.
140 // If the operands are not both numbers, jump to not_numbers.
141 // Leaves rdx and rax unchanged. SmiOperands assumes both are smis.
142 // NumberOperands assumes both are smis or heap numbers.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100143 static void LoadSSE2UnknownOperands(MacroAssembler* masm,
144 Label* not_numbers);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100145};
146
147
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148void DoubleToIStub::Generate(MacroAssembler* masm) {
149 Register input_reg = this->source();
150 Register final_result_reg = this->destination();
151 DCHECK(is_truncating());
Ben Murdoch257744e2011-11-30 15:57:28 +0000152
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000153 Label check_negative, process_64_bits, done;
Ben Murdoch257744e2011-11-30 15:57:28 +0000154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 int double_offset = offset();
Ben Murdoch257744e2011-11-30 15:57:28 +0000156
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 // Account for return address and saved regs if input is rsp.
158 if (input_reg.is(rsp)) double_offset += 3 * kRegisterSize;
Ben Murdoch257744e2011-11-30 15:57:28 +0000159
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
161 MemOperand exponent_operand(MemOperand(input_reg,
162 double_offset + kDoubleSize / 2));
Ben Murdoch257744e2011-11-30 15:57:28 +0000163
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000164 Register scratch1;
165 Register scratch_candidates[3] = { rbx, rdx, rdi };
166 for (int i = 0; i < 3; i++) {
167 scratch1 = scratch_candidates[i];
168 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
Steve Block1e0659c2011-05-24 12:43:12 +0100169 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100170
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 // Since we must use rcx for shifts below, use some other register (rax)
172 // to calculate the result if ecx is the requested return register.
173 Register result_reg = final_result_reg.is(rcx) ? rax : final_result_reg;
174 // Save ecx if it isn't the return register and therefore volatile, or if it
175 // is the return register, then save the temp register we use in its stead
176 // for the result.
177 Register save_reg = final_result_reg.is(rcx) ? rax : rcx;
178 __ pushq(scratch1);
179 __ pushq(save_reg);
180
181 bool stash_exponent_copy = !input_reg.is(rsp);
182 __ movl(scratch1, mantissa_operand);
183 __ movsd(xmm0, mantissa_operand);
184 __ movl(rcx, exponent_operand);
185 if (stash_exponent_copy) __ pushq(rcx);
186
187 __ andl(rcx, Immediate(HeapNumber::kExponentMask));
188 __ shrl(rcx, Immediate(HeapNumber::kExponentShift));
189 __ leal(result_reg, MemOperand(rcx, -HeapNumber::kExponentBias));
190 __ cmpl(result_reg, Immediate(HeapNumber::kMantissaBits));
191 __ j(below, &process_64_bits);
192
193 // Result is entirely in lower 32-bits of mantissa
194 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
195 __ subl(rcx, Immediate(delta));
196 __ xorl(result_reg, result_reg);
197 __ cmpl(rcx, Immediate(31));
198 __ j(above, &done);
199 __ shll_cl(scratch1);
200 __ jmp(&check_negative);
201
202 __ bind(&process_64_bits);
203 __ cvttsd2siq(result_reg, xmm0);
204 __ jmp(&done, Label::kNear);
205
206 // If the double was negative, negate the integer result.
207 __ bind(&check_negative);
208 __ movl(result_reg, scratch1);
209 __ negl(result_reg);
210 if (stash_exponent_copy) {
211 __ cmpl(MemOperand(rsp, 0), Immediate(0));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100212 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 __ cmpl(exponent_operand, Immediate(0));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100214 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000215 __ cmovl(greater, result_reg, scratch1);
Steve Block1e0659c2011-05-24 12:43:12 +0100216
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000217 // Restore registers
Ben Murdochb0fe1622011-05-05 13:52:32 +0100218 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 if (stash_exponent_copy) {
220 __ addp(rsp, Immediate(kDoubleSize));
221 }
222 if (!final_result_reg.is(result_reg)) {
223 DCHECK(final_result_reg.is(rcx));
224 __ movl(final_result_reg, result_reg);
225 }
226 __ popq(save_reg);
227 __ popq(scratch1);
228 __ ret(0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100229}
230
231
232void FloatingPointHelper::LoadSSE2UnknownOperands(MacroAssembler* masm,
233 Label* not_numbers) {
234 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, load_float_rax, done;
235 // Load operand in rdx into xmm0, or branch to not_numbers.
236 __ LoadRoot(rcx, Heap::kHeapNumberMapRootIndex);
237 __ JumpIfSmi(rdx, &load_smi_rdx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 __ cmpp(FieldOperand(rdx, HeapObject::kMapOffset), rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100239 __ j(not_equal, not_numbers); // Argument in rdx is not a number.
240 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
241 // Load operand in rax into xmm1, or branch to not_numbers.
242 __ JumpIfSmi(rax, &load_smi_rax);
243
244 __ bind(&load_nonsmi_rax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 __ cmpp(FieldOperand(rax, HeapObject::kMapOffset), rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100246 __ j(not_equal, not_numbers);
247 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
248 __ jmp(&done);
249
250 __ bind(&load_smi_rdx);
251 __ SmiToInteger32(kScratchRegister, rdx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000252 __ Cvtlsi2sd(xmm0, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100253 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
254
255 __ bind(&load_smi_rax);
256 __ SmiToInteger32(kScratchRegister, rax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000257 __ Cvtlsi2sd(xmm1, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100258 __ bind(&done);
259}
260
261
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100262void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000263 const Register exponent = MathPowTaggedDescriptor::exponent();
264 DCHECK(exponent.is(rdx));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100265 const Register base = rax;
266 const Register scratch = rcx;
267 const XMMRegister double_result = xmm3;
268 const XMMRegister double_base = xmm2;
269 const XMMRegister double_exponent = xmm1;
270 const XMMRegister double_scratch = xmm4;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100271
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100272 Label call_runtime, done, exponent_not_smi, int_exponent;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100273
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100274 // Save 1 in double_result - we need this several times later on.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000275 __ movp(scratch, Immediate(1));
276 __ Cvtlsi2sd(double_result, scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100277
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000278 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100279 Label base_is_smi, unpack_exponent;
280 // The exponent and base are supplied as arguments on the stack.
281 // This can only happen if the stub is called from non-optimized code.
282 // Load input parameters from stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER);
284 __ movp(base, args.GetArgumentOperand(0));
285 __ movp(exponent, args.GetArgumentOperand(1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100286 __ JumpIfSmi(base, &base_is_smi, Label::kNear);
287 __ CompareRoot(FieldOperand(base, HeapObject::kMapOffset),
288 Heap::kHeapNumberMapRootIndex);
289 __ j(not_equal, &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100290
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100291 __ movsd(double_base, FieldOperand(base, HeapNumber::kValueOffset));
292 __ jmp(&unpack_exponent, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100293
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100294 __ bind(&base_is_smi);
295 __ SmiToInteger32(base, base);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 __ Cvtlsi2sd(double_base, base);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100297 __ bind(&unpack_exponent);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100298
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100299 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
300 __ SmiToInteger32(exponent, exponent);
301 __ jmp(&int_exponent);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100302
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100303 __ bind(&exponent_not_smi);
304 __ CompareRoot(FieldOperand(exponent, HeapObject::kMapOffset),
305 Heap::kHeapNumberMapRootIndex);
306 __ j(not_equal, &call_runtime);
307 __ movsd(double_exponent, FieldOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000308 } else if (exponent_type() == TAGGED) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100309 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
310 __ SmiToInteger32(exponent, exponent);
311 __ jmp(&int_exponent);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100312
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100313 __ bind(&exponent_not_smi);
314 __ movsd(double_exponent, FieldOperand(exponent, HeapNumber::kValueOffset));
315 }
316
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000317 if (exponent_type() != INTEGER) {
318 Label fast_power, try_arithmetic_simplification;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100319 // Detect integer exponents stored as double.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000320 __ DoubleToI(exponent, double_exponent, double_scratch,
321 TREAT_MINUS_ZERO_AS_ZERO, &try_arithmetic_simplification,
322 &try_arithmetic_simplification,
323 &try_arithmetic_simplification);
324 __ jmp(&int_exponent);
325
326 __ bind(&try_arithmetic_simplification);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100327 __ cvttsd2si(exponent, double_exponent);
328 // Skip to runtime if possibly NaN (indicated by the indefinite integer).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000329 __ cmpl(exponent, Immediate(0x1));
330 __ j(overflow, &call_runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100331
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100333 // Detect square root case. Crankshaft detects constant +/-0.5 at
334 // compile time and uses DoMathPowHalf instead. We then skip this check
335 // for non-constant cases of +/-0.5 as these hardly occur.
336 Label continue_sqrt, continue_rsqrt, not_plus_half;
337 // Test for 0.5.
338 // Load double_scratch with 0.5.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339 __ movq(scratch, V8_UINT64_C(0x3FE0000000000000));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100340 __ movq(double_scratch, scratch);
341 // Already ruled out NaNs for exponent.
342 __ ucomisd(double_scratch, double_exponent);
343 __ j(not_equal, &not_plus_half, Label::kNear);
344
345 // Calculates square root of base. Check for the special case of
346 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
347 // According to IEEE-754, double-precision -Infinity has the highest
348 // 12 bits set and the lowest 52 bits cleared.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 __ movq(scratch, V8_UINT64_C(0xFFF0000000000000));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100350 __ movq(double_scratch, scratch);
351 __ ucomisd(double_scratch, double_base);
352 // Comparing -Infinity with NaN results in "unordered", which sets the
353 // zero flag as if both were equal. However, it also sets the carry flag.
354 __ j(not_equal, &continue_sqrt, Label::kNear);
355 __ j(carry, &continue_sqrt, Label::kNear);
356
357 // Set result to Infinity in the special case.
358 __ xorps(double_result, double_result);
359 __ subsd(double_result, double_scratch);
360 __ jmp(&done);
361
362 __ bind(&continue_sqrt);
363 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
364 __ xorps(double_scratch, double_scratch);
365 __ addsd(double_scratch, double_base); // Convert -0 to 0.
366 __ sqrtsd(double_result, double_scratch);
367 __ jmp(&done);
368
369 // Test for -0.5.
370 __ bind(&not_plus_half);
371 // Load double_scratch with -0.5 by substracting 1.
372 __ subsd(double_scratch, double_result);
373 // Already ruled out NaNs for exponent.
374 __ ucomisd(double_scratch, double_exponent);
375 __ j(not_equal, &fast_power, Label::kNear);
376
377 // Calculates reciprocal of square root of base. Check for the special
378 // case of Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
379 // According to IEEE-754, double-precision -Infinity has the highest
380 // 12 bits set and the lowest 52 bits cleared.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 __ movq(scratch, V8_UINT64_C(0xFFF0000000000000));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100382 __ movq(double_scratch, scratch);
383 __ ucomisd(double_scratch, double_base);
384 // Comparing -Infinity with NaN results in "unordered", which sets the
385 // zero flag as if both were equal. However, it also sets the carry flag.
386 __ j(not_equal, &continue_rsqrt, Label::kNear);
387 __ j(carry, &continue_rsqrt, Label::kNear);
388
389 // Set result to 0 in the special case.
390 __ xorps(double_result, double_result);
391 __ jmp(&done);
392
393 __ bind(&continue_rsqrt);
394 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
395 __ xorps(double_exponent, double_exponent);
396 __ addsd(double_exponent, double_base); // Convert -0 to +0.
397 __ sqrtsd(double_exponent, double_exponent);
398 __ divsd(double_result, double_exponent);
399 __ jmp(&done);
400 }
401
402 // Using FPU instructions to calculate power.
403 Label fast_power_failed;
404 __ bind(&fast_power);
405 __ fnclex(); // Clear flags to catch exceptions later.
406 // Transfer (B)ase and (E)xponent onto the FPU register stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000407 __ subp(rsp, Immediate(kDoubleSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100408 __ movsd(Operand(rsp, 0), double_exponent);
409 __ fld_d(Operand(rsp, 0)); // E
410 __ movsd(Operand(rsp, 0), double_base);
411 __ fld_d(Operand(rsp, 0)); // B, E
412
413 // Exponent is in st(1) and base is in st(0)
414 // B ^ E = (2^(E * log2(B)) - 1) + 1 = (2^X - 1) + 1 for X = E * log2(B)
415 // FYL2X calculates st(1) * log2(st(0))
416 __ fyl2x(); // X
417 __ fld(0); // X, X
418 __ frndint(); // rnd(X), X
419 __ fsub(1); // rnd(X), X-rnd(X)
420 __ fxch(1); // X - rnd(X), rnd(X)
421 // F2XM1 calculates 2^st(0) - 1 for -1 < st(0) < 1
422 __ f2xm1(); // 2^(X-rnd(X)) - 1, rnd(X)
423 __ fld1(); // 1, 2^(X-rnd(X)) - 1, rnd(X)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000424 __ faddp(1); // 2^(X-rnd(X)), rnd(X)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100425 // FSCALE calculates st(0) * 2^st(1)
426 __ fscale(); // 2^X, rnd(X)
427 __ fstp(1);
428 // Bail out to runtime in case of exceptions in the status word.
429 __ fnstsw_ax();
430 __ testb(rax, Immediate(0x5F)); // Check for all but precision exception.
431 __ j(not_zero, &fast_power_failed, Label::kNear);
432 __ fstp_d(Operand(rsp, 0));
433 __ movsd(double_result, Operand(rsp, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 __ addp(rsp, Immediate(kDoubleSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100435 __ jmp(&done);
436
437 __ bind(&fast_power_failed);
438 __ fninit();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000439 __ addp(rsp, Immediate(kDoubleSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100440 __ jmp(&call_runtime);
441 }
442
443 // Calculate power with integer exponent.
444 __ bind(&int_exponent);
445 const XMMRegister double_scratch2 = double_exponent;
446 // Back up exponent as we need to check if exponent is negative later.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000447 __ movp(scratch, exponent); // Back up exponent.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100448 __ movsd(double_scratch, double_base); // Back up base.
449 __ movsd(double_scratch2, double_result); // Load double_exponent with 1.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100450
451 // Get absolute value of exponent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 Label no_neg, while_true, while_false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100453 __ testl(scratch, scratch);
454 __ j(positive, &no_neg, Label::kNear);
455 __ negl(scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100456 __ bind(&no_neg);
457
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458 __ j(zero, &while_false, Label::kNear);
459 __ shrl(scratch, Immediate(1));
460 // Above condition means CF==0 && ZF==0. This means that the
461 // bit that has been shifted out is 0 and the result is not 0.
462 __ j(above, &while_true, Label::kNear);
463 __ movsd(double_result, double_scratch);
464 __ j(zero, &while_false, Label::kNear);
465
Ben Murdoch85b71792012-04-11 18:30:58 +0100466 __ bind(&while_true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100467 __ shrl(scratch, Immediate(1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100468 __ mulsd(double_scratch, double_scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469 __ j(above, &while_true, Label::kNear);
470 __ mulsd(double_result, double_scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100471 __ j(not_zero, &while_true);
472
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473 __ bind(&while_false);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100474 // If the exponent is negative, return 1/result.
475 __ testl(exponent, exponent);
476 __ j(greater, &done);
477 __ divsd(double_scratch2, double_result);
478 __ movsd(double_result, double_scratch2);
479 // Test whether result is zero. Bail out to check for subnormal result.
480 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
481 __ xorps(double_scratch2, double_scratch2);
482 __ ucomisd(double_scratch2, double_result);
483 // double_exponent aliased as double_scratch2 has already been overwritten
484 // and may not have contained the exponent value in the first place when the
485 // input was a smi. We reset it with exponent value before bailing out.
486 __ j(not_equal, &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487 __ Cvtlsi2sd(double_exponent, exponent);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100488
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100489 // Returning or bailing out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490 Counters* counters = isolate()->counters();
491 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100492 // The arguments are still on the stack.
493 __ bind(&call_runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000494 __ TailCallRuntime(Runtime::kMathPowRT, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100495
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100496 // The stub is called from non-optimized code, which expects the result
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000497 // as heap number in rax.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100498 __ bind(&done);
499 __ AllocateHeapNumber(rax, rcx, &call_runtime);
500 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), double_result);
501 __ IncrementCounter(counters->math_pow(), 1);
502 __ ret(2 * kPointerSize);
503 } else {
504 __ bind(&call_runtime);
505 // Move base to the correct argument register. Exponent is already in xmm1.
506 __ movsd(xmm0, double_base);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000507 DCHECK(double_exponent.is(xmm1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100508 {
509 AllowExternalCallThatCantCauseGC scope(masm);
510 __ PrepareCallCFunction(2);
511 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000512 ExternalReference::power_double_double_function(isolate()), 2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100513 }
514 // Return value is in xmm0.
515 __ movsd(double_result, xmm0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100516
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100517 __ bind(&done);
518 __ IncrementCounter(counters->math_pow(), 1);
519 __ ret(0);
520 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100521}
522
523
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
525 Label miss;
526 Register receiver = LoadDescriptor::ReceiverRegister();
527
528 NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(masm, receiver, r8,
529 r9, &miss);
530 __ bind(&miss);
531 PropertyAccessCompiler::TailCallBuiltin(
532 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
533}
534
535
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100536void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
537 // The key is in rdx and the parameter count is in rax.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538 DCHECK(rdx.is(ArgumentsAccessReadDescriptor::index()));
539 DCHECK(rax.is(ArgumentsAccessReadDescriptor::parameter_count()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100540
541 // Check that the key is a smi.
542 Label slow;
543 __ JumpIfNotSmi(rdx, &slow);
544
Steve Block44f0eee2011-05-26 01:26:41 +0100545 // Check if the calling frame is an arguments adaptor frame. We look at the
546 // context offset, and if the frame is not a regular one, then we find a
547 // Smi instead of the context. We can't use SmiCompare here, because that
548 // only works for comparing two smis.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100549 Label adaptor;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000550 __ movp(rbx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100551 __ Cmp(Operand(rbx, StandardFrameConstants::kContextOffset),
552 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100553 __ j(equal, &adaptor);
554
555 // Check index against formal parameters count limit passed in
556 // through register rax. Use unsigned comparison to get negative
557 // check for free.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558 __ cmpp(rdx, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100559 __ j(above_equal, &slow);
560
561 // Read the argument from the stack and return it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000562 __ SmiSub(rax, rax, rdx);
563 __ SmiToInteger32(rax, rax);
564 StackArgumentsAccessor args(rbp, rax, ARGUMENTS_DONT_CONTAIN_RECEIVER);
565 __ movp(rax, args.GetArgumentOperand(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100566 __ Ret();
567
568 // Arguments adaptor case: Check index against actual arguments
569 // limit found in the arguments adaptor frame. Use unsigned
570 // comparison to get negative check for free.
571 __ bind(&adaptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 __ movp(rcx, Operand(rbx, ArgumentsAdaptorFrameConstants::kLengthOffset));
573 __ cmpp(rdx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100574 __ j(above_equal, &slow);
575
576 // Read the argument from the stack and return it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000577 __ SmiSub(rcx, rcx, rdx);
578 __ SmiToInteger32(rcx, rcx);
579 StackArgumentsAccessor adaptor_args(rbx, rcx,
580 ARGUMENTS_DONT_CONTAIN_RECEIVER);
581 __ movp(rax, adaptor_args.GetArgumentOperand(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100582 __ Ret();
583
584 // Slow-case: Handle non-smi or out-of-bounds access to arguments
585 // by calling the runtime system.
586 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000587 __ PopReturnAddressTo(rbx);
588 __ Push(rdx);
589 __ PushReturnAddressFrom(rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100590 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
591}
592
593
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000594void ArgumentsAccessStub::GenerateNewSloppyFast(MacroAssembler* masm) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000595 // Stack layout:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000596 // rsp[0] : return address
597 // rsp[8] : number of parameters (tagged)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000598 // rsp[16] : receiver displacement
599 // rsp[24] : function
600 // Registers used over the whole function:
601 // rbx: the mapped parameter count (untagged)
602 // rax: the allocated object (tagged).
603
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000604 Factory* factory = isolate()->factory();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000605
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000606 StackArgumentsAccessor args(rsp, 3, ARGUMENTS_DONT_CONTAIN_RECEIVER);
607 __ SmiToInteger64(rbx, args.GetArgumentOperand(2));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000608 // rbx = parameter count (untagged)
609
610 // Check if the calling frame is an arguments adaptor frame.
611 Label runtime;
612 Label adaptor_frame, try_allocate;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000613 __ movp(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
614 __ movp(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000615 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
616 __ j(equal, &adaptor_frame);
617
618 // No adaptor, parameter count = argument count.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000619 __ movp(rcx, rbx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000620 __ jmp(&try_allocate, Label::kNear);
621
622 // We have an adaptor frame. Patch the parameters pointer.
623 __ bind(&adaptor_frame);
624 __ SmiToInteger64(rcx,
625 Operand(rdx,
626 ArgumentsAdaptorFrameConstants::kLengthOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000627 __ leap(rdx, Operand(rdx, rcx, times_pointer_size,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000628 StandardFrameConstants::kCallerSPOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629 __ movp(args.GetArgumentOperand(1), rdx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000630
631 // rbx = parameter count (untagged)
632 // rcx = argument count (untagged)
633 // Compute the mapped parameter count = min(rbx, rcx) in rbx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000634 __ cmpp(rbx, rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000635 __ j(less_equal, &try_allocate, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636 __ movp(rbx, rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000637
638 __ bind(&try_allocate);
639
640 // Compute the sizes of backing store, parameter map, and arguments object.
641 // 1. Parameter map, has 2 extra words containing context and backing store.
642 const int kParameterMapHeaderSize =
643 FixedArray::kHeaderSize + 2 * kPointerSize;
644 Label no_parameter_map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000645 __ xorp(r8, r8);
646 __ testp(rbx, rbx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000647 __ j(zero, &no_parameter_map, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648 __ leap(r8, Operand(rbx, times_pointer_size, kParameterMapHeaderSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000649 __ bind(&no_parameter_map);
650
651 // 2. Backing store.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000652 __ leap(r8, Operand(r8, rcx, times_pointer_size, FixedArray::kHeaderSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000653
654 // 3. Arguments object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000655 __ addp(r8, Immediate(Heap::kSloppyArgumentsObjectSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000656
657 // Do the allocation of all three objects in one go.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000658 __ Allocate(r8, rax, rdx, rdi, &runtime, TAG_OBJECT);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000659
660 // rax = address of new object(s) (tagged)
661 // rcx = argument count (untagged)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 // Get the arguments map from the current native context into rdi.
663 Label has_mapped_parameters, instantiate;
664 __ movp(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
665 __ movp(rdi, FieldOperand(rdi, GlobalObject::kNativeContextOffset));
666 __ testp(rbx, rbx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000667 __ j(not_zero, &has_mapped_parameters, Label::kNear);
668
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000669 const int kIndex = Context::SLOPPY_ARGUMENTS_MAP_INDEX;
670 __ movp(rdi, Operand(rdi, Context::SlotOffset(kIndex)));
671 __ jmp(&instantiate, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000672
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000673 const int kAliasedIndex = Context::ALIASED_ARGUMENTS_MAP_INDEX;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000674 __ bind(&has_mapped_parameters);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000675 __ movp(rdi, Operand(rdi, Context::SlotOffset(kAliasedIndex)));
676 __ bind(&instantiate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000677
678 // rax = address of new object (tagged)
679 // rbx = mapped parameter count (untagged)
680 // rcx = argument count (untagged)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000681 // rdi = address of arguments map (tagged)
682 __ movp(FieldOperand(rax, JSObject::kMapOffset), rdi);
683 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
684 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
685 __ movp(FieldOperand(rax, JSObject::kElementsOffset), kScratchRegister);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000686
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100687 // Set up the callee in-object property.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000688 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000689 __ movp(rdx, args.GetArgumentOperand(0));
690 __ AssertNotSmi(rdx);
691 __ movp(FieldOperand(rax, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000692 Heap::kArgumentsCalleeIndex * kPointerSize),
693 rdx);
694
695 // Use the length (smi tagged) and set that as an in-object property too.
696 // Note: rcx is tagged from here on.
697 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
698 __ Integer32ToSmi(rcx, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699 __ movp(FieldOperand(rax, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000700 Heap::kArgumentsLengthIndex * kPointerSize),
701 rcx);
702
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100703 // Set up the elements pointer in the allocated arguments object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000704 // If we allocated a parameter map, edi will point there, otherwise to the
705 // backing store.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000706 __ leap(rdi, Operand(rax, Heap::kSloppyArgumentsObjectSize));
707 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rdi);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000708
709 // rax = address of new object (tagged)
710 // rbx = mapped parameter count (untagged)
711 // rcx = argument count (tagged)
712 // rdi = address of parameter map or backing store (tagged)
713
714 // Initialize parameter map. If there are no mapped arguments, we're done.
715 Label skip_parameter_map;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000716 __ testp(rbx, rbx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000717 __ j(zero, &skip_parameter_map);
718
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000719 __ LoadRoot(kScratchRegister, Heap::kSloppyArgumentsElementsMapRootIndex);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000720 // rbx contains the untagged argument count. Add 2 and tag to write.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000721 __ movp(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000722 __ Integer64PlusConstantToSmi(r9, rbx, 2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000723 __ movp(FieldOperand(rdi, FixedArray::kLengthOffset), r9);
724 __ movp(FieldOperand(rdi, FixedArray::kHeaderSize + 0 * kPointerSize), rsi);
725 __ leap(r9, Operand(rdi, rbx, times_pointer_size, kParameterMapHeaderSize));
726 __ movp(FieldOperand(rdi, FixedArray::kHeaderSize + 1 * kPointerSize), r9);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000727
728 // Copy the parameter slots and the holes in the arguments.
729 // We need to fill in mapped_parameter_count slots. They index the context,
730 // where parameters are stored in reverse order, at
731 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
732 // The mapped parameter thus need to get indices
733 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
734 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
735 // We loop from right to left.
736 Label parameters_loop, parameters_test;
737
738 // Load tagged parameter count into r9.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100739 __ Integer32ToSmi(r9, rbx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000740 __ Move(r8, Smi::FromInt(Context::MIN_CONTEXT_SLOTS));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000741 __ addp(r8, args.GetArgumentOperand(2));
742 __ subp(r8, r9);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000743 __ Move(r11, factory->the_hole_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000744 __ movp(rdx, rdi);
745 __ leap(rdi, Operand(rdi, rbx, times_pointer_size, kParameterMapHeaderSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000746 // r9 = loop variable (tagged)
747 // r8 = mapping index (tagged)
748 // r11 = the hole value
749 // rdx = address of parameter map (tagged)
750 // rdi = address of backing store (tagged)
751 __ jmp(&parameters_test, Label::kNear);
752
753 __ bind(&parameters_loop);
754 __ SmiSubConstant(r9, r9, Smi::FromInt(1));
755 __ SmiToInteger64(kScratchRegister, r9);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756 __ movp(FieldOperand(rdx, kScratchRegister,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000757 times_pointer_size,
758 kParameterMapHeaderSize),
759 r8);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000760 __ movp(FieldOperand(rdi, kScratchRegister,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000761 times_pointer_size,
762 FixedArray::kHeaderSize),
763 r11);
764 __ SmiAddConstant(r8, r8, Smi::FromInt(1));
765 __ bind(&parameters_test);
766 __ SmiTest(r9);
767 __ j(not_zero, &parameters_loop, Label::kNear);
768
769 __ bind(&skip_parameter_map);
770
771 // rcx = argument count (tagged)
772 // rdi = address of backing store (tagged)
773 // Copy arguments header and remaining slots (if there are any).
774 __ Move(FieldOperand(rdi, FixedArray::kMapOffset),
775 factory->fixed_array_map());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000776 __ movp(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000777
778 Label arguments_loop, arguments_test;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000779 __ movp(r8, rbx);
780 __ movp(rdx, args.GetArgumentOperand(1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100781 // Untag rcx for the loop below.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000782 __ SmiToInteger64(rcx, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000783 __ leap(kScratchRegister, Operand(r8, times_pointer_size, 0));
784 __ subp(rdx, kScratchRegister);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000785 __ jmp(&arguments_test, Label::kNear);
786
787 __ bind(&arguments_loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000788 __ subp(rdx, Immediate(kPointerSize));
789 __ movp(r9, Operand(rdx, 0));
790 __ movp(FieldOperand(rdi, r8,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000791 times_pointer_size,
792 FixedArray::kHeaderSize),
793 r9);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000794 __ addp(r8, Immediate(1));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000795
796 __ bind(&arguments_test);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000797 __ cmpp(r8, rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000798 __ j(less, &arguments_loop, Label::kNear);
799
800 // Return and remove the on-stack parameters.
801 __ ret(3 * kPointerSize);
802
803 // Do the runtime call to allocate the arguments object.
804 // rcx = argument count (untagged)
805 __ bind(&runtime);
806 __ Integer32ToSmi(rcx, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000807 __ movp(args.GetArgumentOperand(2), rcx); // Patch argument count.
808 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000809}
810
811
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000812void ArgumentsAccessStub::GenerateNewSloppySlow(MacroAssembler* masm) {
813 // rsp[0] : return address
814 // rsp[8] : number of parameters
815 // rsp[16] : receiver displacement
816 // rsp[24] : function
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000817
818 // Check if the calling frame is an arguments adaptor frame.
819 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000820 __ movp(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
821 __ movp(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000822 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
823 __ j(not_equal, &runtime);
824
825 // Patch the arguments.length and the parameters pointer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000826 StackArgumentsAccessor args(rsp, 3, ARGUMENTS_DONT_CONTAIN_RECEIVER);
827 __ movp(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
828 __ movp(args.GetArgumentOperand(2), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000829 __ SmiToInteger64(rcx, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000830 __ leap(rdx, Operand(rdx, rcx, times_pointer_size,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000831 StandardFrameConstants::kCallerSPOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000832 __ movp(args.GetArgumentOperand(1), rdx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000833
834 __ bind(&runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000835 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
836}
837
838
839void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
840 // Return address is on the stack.
841 Label slow;
842
843 Register receiver = LoadDescriptor::ReceiverRegister();
844 Register key = LoadDescriptor::NameRegister();
845 Register scratch = rax;
846 DCHECK(!scratch.is(receiver) && !scratch.is(key));
847
848 // Check that the key is an array index, that is Uint32.
849 STATIC_ASSERT(kSmiValueSize <= 32);
850 __ JumpUnlessNonNegativeSmi(key, &slow);
851
852 // Everything is fine, call runtime.
853 __ PopReturnAddressTo(scratch);
854 __ Push(receiver); // receiver
855 __ Push(key); // key
856 __ PushReturnAddressFrom(scratch);
857
858 // Perform tail call to the entry.
859 __ TailCallExternalReference(
860 ExternalReference(IC_Utility(IC::kLoadElementWithInterceptor),
861 masm->isolate()),
862 2, 1);
863
864 __ bind(&slow);
865 PropertyAccessCompiler::TailCallBuiltin(
866 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000867}
868
869
870void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000871 // rsp[0] : return address
872 // rsp[8] : number of parameters
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100873 // rsp[16] : receiver displacement
874 // rsp[24] : function
875
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100876 // Check if the calling frame is an arguments adaptor frame.
877 Label adaptor_frame, try_allocate, runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000878 __ movp(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
879 __ movp(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000880 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100881 __ j(equal, &adaptor_frame);
882
883 // Get the length from the frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000884 StackArgumentsAccessor args(rsp, 3, ARGUMENTS_DONT_CONTAIN_RECEIVER);
885 __ movp(rcx, args.GetArgumentOperand(2));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000886 __ SmiToInteger64(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100887 __ jmp(&try_allocate);
888
889 // Patch the arguments.length and the parameters pointer.
890 __ bind(&adaptor_frame);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 __ movp(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
892 __ movp(args.GetArgumentOperand(2), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000893 __ SmiToInteger64(rcx, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000894 __ leap(rdx, Operand(rdx, rcx, times_pointer_size,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000895 StandardFrameConstants::kCallerSPOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 __ movp(args.GetArgumentOperand(1), rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100897
898 // Try the new space allocation. Start out with computing the size of
899 // the arguments object and the elements array.
900 Label add_arguments_object;
901 __ bind(&try_allocate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000902 __ testp(rcx, rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000903 __ j(zero, &add_arguments_object, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000904 __ leap(rcx, Operand(rcx, times_pointer_size, FixedArray::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100905 __ bind(&add_arguments_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000906 __ addp(rcx, Immediate(Heap::kStrictArgumentsObjectSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100907
908 // Do the allocation of both objects in one go.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000909 __ Allocate(rcx, rax, rdx, rbx, &runtime, TAG_OBJECT);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100910
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000911 // Get the arguments map from the current native context.
912 __ movp(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
913 __ movp(rdi, FieldOperand(rdi, GlobalObject::kNativeContextOffset));
914 const int offset = Context::SlotOffset(Context::STRICT_ARGUMENTS_MAP_INDEX);
915 __ movp(rdi, Operand(rdi, offset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100916
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000917 __ movp(FieldOperand(rax, JSObject::kMapOffset), rdi);
918 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
919 __ movp(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
920 __ movp(FieldOperand(rax, JSObject::kElementsOffset), kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100921
922 // Get the length (smi tagged) and set that as an in-object property too.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000923 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000924 __ movp(rcx, args.GetArgumentOperand(2));
925 __ movp(FieldOperand(rax, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000926 Heap::kArgumentsLengthIndex * kPointerSize),
Steve Block44f0eee2011-05-26 01:26:41 +0100927 rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100928
929 // If there are no actual arguments, we're done.
930 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931 __ testp(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100932 __ j(zero, &done);
933
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000934 // Get the parameters pointer from the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000935 __ movp(rdx, args.GetArgumentOperand(1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100936
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100937 // Set up the elements pointer in the allocated arguments object and
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100938 // initialize the header in the elements fixed array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000939 __ leap(rdi, Operand(rax, Heap::kStrictArgumentsObjectSize));
940 __ movp(FieldOperand(rax, JSObject::kElementsOffset), rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100941 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942 __ movp(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000943
944
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000945 __ movp(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000946 // Untag the length for the loop below.
947 __ SmiToInteger64(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100948
949 // Copy the fixed array slots.
950 Label loop;
951 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000952 __ movp(rbx, Operand(rdx, -1 * kPointerSize)); // Skip receiver.
953 __ movp(FieldOperand(rdi, FixedArray::kHeaderSize), rbx);
954 __ addp(rdi, Immediate(kPointerSize));
955 __ subp(rdx, Immediate(kPointerSize));
956 __ decp(rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100957 __ j(not_zero, &loop);
958
959 // Return and remove the on-stack parameters.
960 __ bind(&done);
961 __ ret(3 * kPointerSize);
962
963 // Do the runtime call to allocate the arguments object.
964 __ bind(&runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000965 __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100966}
967
968
969void RegExpExecStub::Generate(MacroAssembler* masm) {
970 // Just jump directly to runtime if native RegExp is not selected at compile
971 // time or if regexp entry in generated code is turned off runtime switch or
972 // at compilation.
973#ifdef V8_INTERPRETED_REGEXP
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000974 __ TailCallRuntime(Runtime::kRegExpExecRT, 4, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100975#else // V8_INTERPRETED_REGEXP
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100976
977 // Stack frame on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000978 // rsp[0] : return address
979 // rsp[8] : last_match_info (expected JSArray)
980 // rsp[16] : previous index
981 // rsp[24] : subject string
982 // rsp[32] : JSRegExp object
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100983
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000984 enum RegExpExecStubArgumentIndices {
985 JS_REG_EXP_OBJECT_ARGUMENT_INDEX,
986 SUBJECT_STRING_ARGUMENT_INDEX,
987 PREVIOUS_INDEX_ARGUMENT_INDEX,
988 LAST_MATCH_INFO_ARGUMENT_INDEX,
989 REG_EXP_EXEC_ARGUMENT_COUNT
990 };
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100991
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000992 StackArgumentsAccessor args(rsp, REG_EXP_EXEC_ARGUMENT_COUNT,
993 ARGUMENTS_DONT_CONTAIN_RECEIVER);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100994 Label runtime;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100995 // Ensure that a RegExp stack is allocated.
996 ExternalReference address_of_regexp_stack_memory_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997 ExternalReference::address_of_regexp_stack_memory_address(isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100998 ExternalReference address_of_regexp_stack_memory_size =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000999 ExternalReference::address_of_regexp_stack_memory_size(isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01001000 __ Load(kScratchRegister, address_of_regexp_stack_memory_size);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001001 __ testp(kScratchRegister, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001002 __ j(zero, &runtime);
1003
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001004 // Check that the first argument is a JSRegExp object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001005 __ movp(rax, args.GetArgumentOperand(JS_REG_EXP_OBJECT_ARGUMENT_INDEX));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001006 __ JumpIfSmi(rax, &runtime);
1007 __ CmpObjectType(rax, JS_REGEXP_TYPE, kScratchRegister);
1008 __ j(not_equal, &runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001009
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001010 // Check that the RegExp has been compiled (data contains a fixed array).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001011 __ movp(rax, FieldOperand(rax, JSRegExp::kDataOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001012 if (FLAG_debug_code) {
Steve Block44f0eee2011-05-26 01:26:41 +01001013 Condition is_smi = masm->CheckSmi(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001014 __ Check(NegateCondition(is_smi),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001015 kUnexpectedTypeForRegExpDataFixedArrayExpected);
Steve Block44f0eee2011-05-26 01:26:41 +01001016 __ CmpObjectType(rax, FIXED_ARRAY_TYPE, kScratchRegister);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001018 }
1019
Steve Block44f0eee2011-05-26 01:26:41 +01001020 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001021 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
Steve Block44f0eee2011-05-26 01:26:41 +01001022 __ SmiToInteger32(rbx, FieldOperand(rax, JSRegExp::kDataTagOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001023 __ cmpl(rbx, Immediate(JSRegExp::IRREGEXP));
1024 __ j(not_equal, &runtime);
1025
Steve Block44f0eee2011-05-26 01:26:41 +01001026 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001027 // Check that the number of captures fit in the static offsets vector buffer.
1028 __ SmiToInteger32(rdx,
Steve Block44f0eee2011-05-26 01:26:41 +01001029 FieldOperand(rax, JSRegExp::kIrregexpCaptureCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001030 // Check (number_of_captures + 1) * 2 <= offsets vector size
1031 // Or number_of_captures <= offsets vector size / 2 - 1
1032 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1033 __ cmpl(rdx, Immediate(Isolate::kJSRegexpStaticOffsetsVectorSize / 2 - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001034 __ j(above, &runtime);
1035
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001036 // Reset offset for possibly sliced string.
1037 __ Set(r14, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001038 __ movp(rdi, args.GetArgumentOperand(SUBJECT_STRING_ARGUMENT_INDEX));
1039 __ JumpIfSmi(rdi, &runtime);
1040 __ movp(r15, rdi); // Make a copy of the original subject string.
1041 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001042 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001043 // rax: RegExp data (FixedArray)
1044 // rdi: subject string
1045 // r15: subject string
1046 // Handle subject string according to its encoding and representation:
1047 // (1) Sequential two byte? If yes, go to (9).
1048 // (2) Sequential one byte? If yes, go to (6).
1049 // (3) Anything but sequential or cons? If yes, go to (7).
1050 // (4) Cons string. If the string is flat, replace subject with first string.
1051 // Otherwise bailout.
1052 // (5a) Is subject sequential two byte? If yes, go to (9).
1053 // (5b) Is subject external? If yes, go to (8).
1054 // (6) One byte sequential. Load regexp code for one byte.
1055 // (E) Carry on.
1056 /// [...]
1057
1058 // Deferred code at the end of the stub:
1059 // (7) Not a long external string? If yes, go to (10).
1060 // (8) External string. Make it, offset-wise, look like a sequential string.
1061 // (8a) Is the external string one byte? If yes, go to (6).
1062 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1063 // (10) Short external string or not a string? If yes, bail out to runtime.
1064 // (11) Sliced string. Replace subject with parent. Go to (5a).
1065
1066 Label seq_one_byte_string /* 6 */, seq_two_byte_string /* 9 */,
1067 external_string /* 8 */, check_underlying /* 5a */,
1068 not_seq_nor_cons /* 7 */, check_code /* E */,
1069 not_long_external /* 10 */;
1070
1071 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001072 __ andb(rbx, Immediate(kIsNotStringMask |
1073 kStringRepresentationMask |
1074 kStringEncodingMask |
1075 kShortExternalStringMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001076 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001077 __ j(zero, &seq_two_byte_string); // Go to (9).
1078
1079 // (2) Sequential one byte? If yes, go to (6).
1080 // Any other sequential string must be one byte.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001081 __ andb(rbx, Immediate(kIsNotStringMask |
1082 kStringRepresentationMask |
1083 kShortExternalStringMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (6).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001085
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001086 // (3) Anything but sequential or cons? If yes, go to (7).
1087 // We check whether the subject string is a cons, since sequential strings
1088 // have already been covered.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001089 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1090 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001091 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1092 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001093 __ cmpp(rbx, Immediate(kExternalStringTag));
1094 __ j(greater_equal, &not_seq_nor_cons); // Go to (7).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001095
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001096 // (4) Cons string. Check that it's flat.
1097 // Replace subject with first string and reload instance type.
Steve Block44f0eee2011-05-26 01:26:41 +01001098 __ CompareRoot(FieldOperand(rdi, ConsString::kSecondOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001099 Heap::kempty_stringRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001100 __ j(not_equal, &runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001101 __ movp(rdi, FieldOperand(rdi, ConsString::kFirstOffset));
1102 __ bind(&check_underlying);
1103 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
1104 __ movp(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1105
1106 // (5a) Is subject sequential two byte? If yes, go to (9).
1107 __ testb(rbx, Immediate(kStringRepresentationMask | kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001108 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001109 __ j(zero, &seq_two_byte_string); // Go to (9).
1110 // (5b) Is subject external? If yes, go to (8).
1111 __ testb(rbx, Immediate(kStringRepresentationMask));
1112 // The underlying external string is never a short external string.
1113 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
1114 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
1115 __ j(not_zero, &external_string); // Go to (8)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001116
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001117 // (6) One byte sequential. Load regexp code for one byte.
1118 __ bind(&seq_one_byte_string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001119 // rax: RegExp data (FixedArray)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001120 __ movp(r11, FieldOperand(rax, JSRegExp::kDataOneByteCodeOffset));
1121 __ Set(rcx, 1); // Type is one byte.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001122
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001123 // (E) Carry on. String handling is done.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001124 __ bind(&check_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001125 // r11: irregexp code
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001126 // Check that the irregexp code has been generated for the actual string
1127 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00001128 // smi (code flushing support)
1129 __ JumpIfSmi(r11, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001130
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001131 // rdi: sequential subject string (or look-alike, external string)
1132 // r15: original subject string
1133 // rcx: encoding of subject string (1 if one_byte, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001134 // r11: code
1135 // Load used arguments before starting to push arguments for call to native
1136 // RegExp code to avoid handling changing stack height.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001137 // We have to use r15 instead of rdi to load the length because rdi might
1138 // have been only made to look like a sequential string when it actually
1139 // is an external string.
1140 __ movp(rbx, args.GetArgumentOperand(PREVIOUS_INDEX_ARGUMENT_INDEX));
1141 __ JumpIfNotSmi(rbx, &runtime);
1142 __ SmiCompare(rbx, FieldOperand(r15, String::kLengthOffset));
1143 __ j(above_equal, &runtime);
1144 __ SmiToInteger64(rbx, rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001145
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001146 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001147 // rbx: previous index
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001148 // rcx: encoding of subject string (1 if one_byte 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001149 // r11: code
1150 // All checks done. Now push arguments for native regexp code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001151 Counters* counters = isolate()->counters();
Steve Block44f0eee2011-05-26 01:26:41 +01001152 __ IncrementCounter(counters->regexp_entry_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001153
Steve Block44f0eee2011-05-26 01:26:41 +01001154 // Isolates: note we add an additional parameter here (isolate pointer).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001155 static const int kRegExpExecuteArguments = 9;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001156 int argument_slots_on_stack =
1157 masm->ArgumentStackSlotsForCFunctionCall(kRegExpExecuteArguments);
Steve Block44f0eee2011-05-26 01:26:41 +01001158 __ EnterApiExitFrame(argument_slots_on_stack);
1159
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001160 // Argument 9: Pass current isolate address.
1161 __ LoadAddress(kScratchRegister,
1162 ExternalReference::isolate_address(isolate()));
1163 __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kRegisterSize),
Steve Block44f0eee2011-05-26 01:26:41 +01001164 kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001165
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001166 // Argument 8: Indicate that this is a direct call from JavaScript.
1167 __ movq(Operand(rsp, (argument_slots_on_stack - 2) * kRegisterSize),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001168 Immediate(1));
1169
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001170 // Argument 7: Start (high end) of backtracking stack memory area.
1171 __ Move(kScratchRegister, address_of_regexp_stack_memory_address);
1172 __ movp(r9, Operand(kScratchRegister, 0));
1173 __ Move(kScratchRegister, address_of_regexp_stack_memory_size);
1174 __ addp(r9, Operand(kScratchRegister, 0));
1175 __ movq(Operand(rsp, (argument_slots_on_stack - 3) * kRegisterSize), r9);
1176
1177 // Argument 6: Set the number of capture registers to zero to force global
1178 // regexps to behave as non-global. This does not affect non-global regexps.
1179 // Argument 6 is passed in r9 on Linux and on the stack on Windows.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001180#ifdef _WIN64
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001181 __ movq(Operand(rsp, (argument_slots_on_stack - 4) * kRegisterSize),
1182 Immediate(0));
1183#else
1184 __ Set(r9, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001185#endif
1186
1187 // Argument 5: static offsets vector buffer.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001188 __ LoadAddress(
1189 r8, ExternalReference::address_of_static_offsets_vector(isolate()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001190 // Argument 5 passed in r8 on Linux and on the stack on Windows.
1191#ifdef _WIN64
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001192 __ movq(Operand(rsp, (argument_slots_on_stack - 5) * kRegisterSize), r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001193#endif
1194
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001195 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001196 // rbx: previous index
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001197 // rcx: encoding of subject string (1 if one_byte 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001198 // r11: code
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001199 // r14: slice offset
1200 // r15: original subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001201
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001202 // Argument 2: Previous index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001203 __ movp(arg_reg_2, rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001204
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001205 // Argument 4: End of string data
1206 // Argument 3: Start of string data
1207 Label setup_two_byte, setup_rest, got_length, length_not_from_slice;
1208 // Prepare start and end index of the input.
1209 // Load the length from the original sliced string if that is the case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210 __ addp(rbx, r14);
1211 __ SmiToInteger32(arg_reg_3, FieldOperand(r15, String::kLengthOffset));
1212 __ addp(r14, arg_reg_3); // Using arg3 as scratch.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001213
1214 // rbx: start index of the input
1215 // r14: end index of the input
1216 // r15: original subject string
1217 __ testb(rcx, rcx); // Last use of rcx as encoding of subject string.
1218 __ j(zero, &setup_two_byte, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001219 __ leap(arg_reg_4,
1220 FieldOperand(rdi, r14, times_1, SeqOneByteString::kHeaderSize));
1221 __ leap(arg_reg_3,
1222 FieldOperand(rdi, rbx, times_1, SeqOneByteString::kHeaderSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001223 __ jmp(&setup_rest, Label::kNear);
1224 __ bind(&setup_two_byte);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001225 __ leap(arg_reg_4,
1226 FieldOperand(rdi, r14, times_2, SeqTwoByteString::kHeaderSize));
1227 __ leap(arg_reg_3,
1228 FieldOperand(rdi, rbx, times_2, SeqTwoByteString::kHeaderSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001229 __ bind(&setup_rest);
1230
1231 // Argument 1: Original subject string.
1232 // The original subject is in the previous stack frame. Therefore we have to
1233 // use rbp, which points exactly to one pointer size below the previous rsp.
1234 // (Because creating a new stack frame pushes the previous rbp onto the stack
1235 // and thereby moves up rsp by one kPointerSize.)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001236 __ movp(arg_reg_1, r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001237
1238 // Locate the code entry and call it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239 __ addp(r11, Immediate(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001240 __ call(r11);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001241
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001242 __ LeaveApiExitFrame(true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001243
1244 // Check the result.
Ben Murdoch257744e2011-11-30 15:57:28 +00001245 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001246 Label exception;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001247 __ cmpl(rax, Immediate(1));
1248 // We expect exactly one result since we force the called regexp to behave
1249 // as non-global.
Ben Murdoch257744e2011-11-30 15:57:28 +00001250 __ j(equal, &success, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001251 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::EXCEPTION));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001252 __ j(equal, &exception);
1253 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::FAILURE));
1254 // If none of the above, it can only be retry.
1255 // Handle that in the runtime system.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001256 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001257
1258 // For failure return null.
1259 __ LoadRoot(rax, Heap::kNullValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001260 __ ret(REG_EXP_EXEC_ARGUMENT_COUNT * kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001261
1262 // Load RegExp data.
1263 __ bind(&success);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001264 __ movp(rax, args.GetArgumentOperand(JS_REG_EXP_OBJECT_ARGUMENT_INDEX));
1265 __ movp(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001266 __ SmiToInteger32(rax,
1267 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
1268 // Calculate number of capture registers (number_of_captures + 1) * 2.
1269 __ leal(rdx, Operand(rax, rax, times_1, 2));
1270
1271 // rdx: Number of capture registers
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001272 // Check that the fourth object is a JSArray object.
1273 __ movp(r15, args.GetArgumentOperand(LAST_MATCH_INFO_ARGUMENT_INDEX));
1274 __ JumpIfSmi(r15, &runtime);
1275 __ CmpObjectType(r15, JS_ARRAY_TYPE, kScratchRegister);
1276 __ j(not_equal, &runtime);
1277 // Check that the JSArray is in fast case.
1278 __ movp(rbx, FieldOperand(r15, JSArray::kElementsOffset));
1279 __ movp(rax, FieldOperand(rbx, HeapObject::kMapOffset));
1280 __ CompareRoot(rax, Heap::kFixedArrayMapRootIndex);
1281 __ j(not_equal, &runtime);
1282 // Check that the last match info has space for the capture registers and the
1283 // additional information. Ensure no overflow in add.
1284 STATIC_ASSERT(FixedArray::kMaxLength < kMaxInt - FixedArray::kLengthOffset);
1285 __ SmiToInteger32(rax, FieldOperand(rbx, FixedArray::kLengthOffset));
1286 __ subl(rax, Immediate(RegExpImpl::kLastMatchOverhead));
1287 __ cmpl(rdx, rax);
1288 __ j(greater, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001289
1290 // rbx: last_match_info backing store (FixedArray)
1291 // rdx: number of capture registers
1292 // Store the capture count.
1293 __ Integer32ToSmi(kScratchRegister, rdx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001294 __ movp(FieldOperand(rbx, RegExpImpl::kLastCaptureCountOffset),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001295 kScratchRegister);
1296 // Store last subject and last input.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001297 __ movp(rax, args.GetArgumentOperand(SUBJECT_STRING_ARGUMENT_INDEX));
1298 __ movp(FieldOperand(rbx, RegExpImpl::kLastSubjectOffset), rax);
1299 __ movp(rcx, rax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001300 __ RecordWriteField(rbx,
1301 RegExpImpl::kLastSubjectOffset,
1302 rax,
1303 rdi,
1304 kDontSaveFPRegs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001305 __ movp(rax, rcx);
1306 __ movp(FieldOperand(rbx, RegExpImpl::kLastInputOffset), rax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001307 __ RecordWriteField(rbx,
1308 RegExpImpl::kLastInputOffset,
1309 rax,
1310 rdi,
1311 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001312
1313 // Get the static offsets vector filled by the native regexp code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001314 __ LoadAddress(
1315 rcx, ExternalReference::address_of_static_offsets_vector(isolate()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001316
1317 // rbx: last_match_info backing store (FixedArray)
1318 // rcx: offsets vector
1319 // rdx: number of capture registers
Ben Murdoch257744e2011-11-30 15:57:28 +00001320 Label next_capture, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001321 // Capture register counter starts from number of capture registers and
1322 // counts down until wraping after zero.
1323 __ bind(&next_capture);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001324 __ subp(rdx, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00001325 __ j(negative, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001326 // Read the value from the static offsets vector buffer and make it a smi.
1327 __ movl(rdi, Operand(rcx, rdx, times_int_size, 0));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001328 __ Integer32ToSmi(rdi, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001329 // Store the smi value in the last match info.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001330 __ movp(FieldOperand(rbx,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001331 rdx,
1332 times_pointer_size,
1333 RegExpImpl::kFirstCaptureOffset),
1334 rdi);
1335 __ jmp(&next_capture);
1336 __ bind(&done);
1337
1338 // Return last match info.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001339 __ movp(rax, r15);
1340 __ ret(REG_EXP_EXEC_ARGUMENT_COUNT * kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001341
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001342 __ bind(&exception);
1343 // Result must now be exception. If there is no pending exception already a
1344 // stack overflow (on the backtrack stack) was detected in RegExp code but
1345 // haven't created the exception yet. Handle that in the runtime system.
1346 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01001347 ExternalReference pending_exception_address(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001348 Isolate::kPendingExceptionAddress, isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01001349 Operand pending_exception_operand =
1350 masm->ExternalOperand(pending_exception_address, rbx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001351 __ movp(rax, pending_exception_operand);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001352 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001353 __ cmpp(rax, rdx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001354 __ j(equal, &runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001355 __ movp(pending_exception_operand, rdx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001356
1357 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001358 Label termination_exception;
1359 __ j(equal, &termination_exception, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001360 __ Throw(rax);
1361
1362 __ bind(&termination_exception);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001363 __ ThrowUncatchable(rax);
1364
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001365 // Do the runtime call to execute the regexp.
1366 __ bind(&runtime);
1367 __ TailCallRuntime(Runtime::kRegExpExecRT, 4, 1);
1368
1369 // Deferred code for string handling.
1370 // (7) Not a long external string? If yes, go to (10).
1371 __ bind(&not_seq_nor_cons);
1372 // Compare flags are still set from (3).
1373 __ j(greater, &not_long_external, Label::kNear); // Go to (10).
1374
1375 // (8) External string. Short external strings have been ruled out.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001376 __ bind(&external_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001377 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001378 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
1379 if (FLAG_debug_code) {
1380 // Assert that we do not have a cons or slice (indirect strings) here.
1381 // Sequential strings have already been ruled out.
1382 __ testb(rbx, Immediate(kIsIndirectStringMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001383 __ Assert(zero, kExternalStringExpectedButNotFound);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001384 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001385 __ movp(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001386 // Move the pointer so that offset-wise, it looks like a sequential string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001387 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
1388 __ subp(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001389 STATIC_ASSERT(kTwoByteStringTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001390 // (8a) Is the external string one byte? If yes, go to (6).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001391 __ testb(rbx, Immediate(kStringEncodingMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001392 __ j(not_zero, &seq_one_byte_string); // Goto (6).
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001393
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 // rdi: subject string (flat two-byte)
1395 // rax: RegExp data (FixedArray)
1396 // (9) Two byte sequential. Load regexp code for one byte. Go to (E).
1397 __ bind(&seq_two_byte_string);
1398 __ movp(r11, FieldOperand(rax, JSRegExp::kDataUC16CodeOffset));
1399 __ Set(rcx, 0); // Type is two byte.
1400 __ jmp(&check_code); // Go to (E).
1401
1402 // (10) Not a string or a short external string? If yes, bail out to runtime.
1403 __ bind(&not_long_external);
1404 // Catch non-string subject or short external string.
1405 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1406 __ testb(rbx, Immediate(kIsNotStringMask | kShortExternalStringMask));
1407 __ j(not_zero, &runtime);
1408
1409 // (11) Sliced string. Replace subject with parent. Go to (5a).
1410 // Load offset into r14 and replace subject string with parent.
1411 __ SmiToInteger32(r14, FieldOperand(rdi, SlicedString::kOffsetOffset));
1412 __ movp(rdi, FieldOperand(rdi, SlicedString::kParentOffset));
1413 __ jmp(&check_underlying);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001414#endif // V8_INTERPRETED_REGEXP
1415}
1416
1417
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001418static int NegativeComparisonResult(Condition cc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001419 DCHECK(cc != equal);
1420 DCHECK((cc == less) || (cc == less_equal)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001421 || (cc == greater) || (cc == greater_equal));
1422 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
1423}
1424
1425
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001426static void CheckInputType(MacroAssembler* masm, Register input,
1427 CompareICState::State expected, Label* fail) {
1428 Label ok;
1429 if (expected == CompareICState::SMI) {
1430 __ JumpIfNotSmi(input, fail);
1431 } else if (expected == CompareICState::NUMBER) {
1432 __ JumpIfSmi(input, &ok);
1433 __ CompareMap(input, masm->isolate()->factory()->heap_number_map());
1434 __ j(not_equal, fail);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001435 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001436 // We could be strict about internalized/non-internalized here, but as long as
1437 // hydrogen doesn't care, the stub doesn't have to care either.
1438 __ bind(&ok);
1439}
1440
1441
1442static void BranchIfNotInternalizedString(MacroAssembler* masm,
1443 Label* label,
1444 Register object,
1445 Register scratch) {
1446 __ JumpIfSmi(object, label);
1447 __ movp(scratch, FieldOperand(object, HeapObject::kMapOffset));
1448 __ movzxbp(scratch,
1449 FieldOperand(scratch, Map::kInstanceTypeOffset));
1450 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
1451 __ testb(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
1452 __ j(not_zero, label);
1453}
1454
1455
1456void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
1457 Label check_unequal_objects, done;
1458 Condition cc = GetCondition();
1459 Factory* factory = isolate()->factory();
1460
1461 Label miss;
1462 CheckInputType(masm, rdx, left(), &miss);
1463 CheckInputType(masm, rax, right(), &miss);
1464
1465 // Compare two smis.
1466 Label non_smi, smi_done;
1467 __ JumpIfNotBothSmi(rax, rdx, &non_smi);
1468 __ subp(rdx, rax);
1469 __ j(no_overflow, &smi_done);
1470 __ notp(rdx); // Correct sign in case of overflow. rdx cannot be 0 here.
1471 __ bind(&smi_done);
1472 __ movp(rax, rdx);
1473 __ ret(0);
1474 __ bind(&non_smi);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001475
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001476 // The compare stub returns a positive, negative, or zero 64-bit integer
1477 // value in rax, corresponding to result of comparing the two inputs.
1478 // NOTICE! This code is only reached after a smi-fast-case check, so
1479 // it is certain that at least one operand isn't a smi.
1480
1481 // Two identical objects are equal unless they are both NaN or undefined.
1482 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001483 Label not_identical;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001484 __ cmpp(rax, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001485 __ j(not_equal, &not_identical, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001486
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001487 if (cc != equal) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001488 // Check for undefined. undefined OP undefined is false even though
1489 // undefined == undefined.
Ben Murdoch257744e2011-11-30 15:57:28 +00001490 Label check_for_nan;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001491 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001492 __ j(not_equal, &check_for_nan, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001493 __ Set(rax, NegativeComparisonResult(cc));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001494 __ ret(0);
1495 __ bind(&check_for_nan);
1496 }
1497
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001498 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001499 // so we do the second best thing - test it ourselves.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001500 Label heap_number;
1501 // If it's not a heap number, then return equal for (in)equality operator.
1502 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset),
1503 factory->heap_number_map());
1504 __ j(equal, &heap_number, Label::kNear);
1505 if (cc != equal) {
1506 // Call runtime on identical objects. Otherwise return equal.
1507 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
1508 __ j(above_equal, &not_identical, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001509 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001510 __ Set(rax, EQUAL);
1511 __ ret(0);
1512
1513 __ bind(&heap_number);
1514 // It is a heap number, so return equal if it's not NaN.
1515 // For NaN, return 1 for every condition except greater and
1516 // greater-equal. Return -1 for them, so the comparison yields
1517 // false for all conditions except not-equal.
1518 __ Set(rax, EQUAL);
1519 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1520 __ ucomisd(xmm0, xmm0);
1521 __ setcc(parity_even, rax);
1522 // rax is 0 for equal non-NaN heapnumbers, 1 for NaNs.
1523 if (cc == greater_equal || cc == greater) {
1524 __ negp(rax);
1525 }
1526 __ ret(0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001527
1528 __ bind(&not_identical);
1529 }
1530
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001531 if (cc == equal) { // Both strict and non-strict.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001532 Label slow; // Fallthrough label.
1533
1534 // If we're doing a strict equality comparison, we don't have to do
1535 // type conversion, so we generate code to do fast comparison for objects
1536 // and oddballs. Non-smi numbers and strings still go through the usual
1537 // slow-case code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001538 if (strict()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001539 // If either is a Smi (we know that not both are), then they can only
1540 // be equal if the other is a HeapNumber. If so, use the slow case.
1541 {
1542 Label not_smis;
1543 __ SelectNonSmi(rbx, rax, rdx, &not_smis);
1544
1545 // Check if the non-smi operand is a heap number.
1546 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00001547 factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001548 // If heap number, handle it in the slow case.
1549 __ j(equal, &slow);
1550 // Return non-equal. ebx (the lower half of rbx) is not zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001551 __ movp(rax, rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001552 __ ret(0);
1553
1554 __ bind(&not_smis);
1555 }
1556
1557 // If either operand is a JSObject or an oddball value, then they are not
1558 // equal since their pointers are different
1559 // There is no test for undetectability in strict equality.
1560
1561 // If the first object is a JS object, we have done pointer comparison.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001562 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +00001563 Label first_non_object;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001564 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001565 __ j(below, &first_non_object, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001566 // Return non-zero (rax (not rax) is not zero)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001567 Label return_not_equal;
1568 STATIC_ASSERT(kHeapObjectTag != 0);
1569 __ bind(&return_not_equal);
1570 __ ret(0);
1571
1572 __ bind(&first_non_object);
1573 // Check for oddballs: true, false, null, undefined.
1574 __ CmpInstanceType(rcx, ODDBALL_TYPE);
1575 __ j(equal, &return_not_equal);
1576
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001577 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001578 __ j(above_equal, &return_not_equal);
1579
1580 // Check for oddballs: true, false, null, undefined.
1581 __ CmpInstanceType(rcx, ODDBALL_TYPE);
1582 __ j(equal, &return_not_equal);
1583
1584 // Fall through to the general case.
1585 }
1586 __ bind(&slow);
1587 }
1588
1589 // Generate the number comparison code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001590 Label non_number_comparison;
1591 Label unordered;
1592 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &non_number_comparison);
1593 __ xorl(rax, rax);
1594 __ xorl(rcx, rcx);
1595 __ ucomisd(xmm0, xmm1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001596
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001597 // Don't base result on EFLAGS when a NaN is involved.
1598 __ j(parity_even, &unordered, Label::kNear);
1599 // Return a result of -1, 0, or 1, based on EFLAGS.
1600 __ setcc(above, rax);
1601 __ setcc(below, rcx);
1602 __ subp(rax, rcx);
1603 __ ret(0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001604
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001605 // If one of the numbers was NaN, then the result is always false.
1606 // The cc is never not-equal.
1607 __ bind(&unordered);
1608 DCHECK(cc != not_equal);
1609 if (cc == less || cc == less_equal) {
1610 __ Set(rax, 1);
1611 } else {
1612 __ Set(rax, -1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001613 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001614 __ ret(0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001615
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001616 // The number comparison code did not provide a valid result.
1617 __ bind(&non_number_comparison);
1618
1619 // Fast negative check for internalized-to-internalized equality.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001620 Label check_for_strings;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001621 if (cc == equal) {
1622 BranchIfNotInternalizedString(
1623 masm, &check_for_strings, rax, kScratchRegister);
1624 BranchIfNotInternalizedString(
1625 masm, &check_for_strings, rdx, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001626
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001627 // We've already checked for object identity, so if both operands are
1628 // internalized strings they aren't equal. Register rax (not rax) already
1629 // holds a non-zero value, which indicates not equal, so just return.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001630 __ ret(0);
1631 }
1632
1633 __ bind(&check_for_strings);
1634
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001635 __ JumpIfNotBothSequentialOneByteStrings(rdx, rax, rcx, rbx,
1636 &check_unequal_objects);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001637
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001638 // Inline comparison of one-byte strings.
1639 if (cc == equal) {
1640 StringHelper::GenerateFlatOneByteStringEquals(masm, rdx, rax, rcx, rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001641 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642 StringHelper::GenerateCompareFlatOneByteStrings(masm, rdx, rax, rcx, rbx,
1643 rdi, r8);
Ben Murdoch257744e2011-11-30 15:57:28 +00001644 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001645
1646#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001647 __ Abort(kUnexpectedFallThroughFromStringComparison);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001648#endif
1649
1650 __ bind(&check_unequal_objects);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001651 if (cc == equal && !strict()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001652 // Not strict equality. Objects are unequal if
1653 // they are both JSObjects and not undetectable,
1654 // and their pointers are different.
Ben Murdoch257744e2011-11-30 15:57:28 +00001655 Label not_both_objects, return_unequal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001656 // At most one is a smi, so we can test for smi by adding the two.
1657 // A smi plus a heap object has the low bit set, a heap object plus
1658 // a heap object has the low bit clear.
1659 STATIC_ASSERT(kSmiTag == 0);
1660 STATIC_ASSERT(kSmiTagMask == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001661 __ leap(rcx, Operand(rax, rdx, times_1, 0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001662 __ testb(rcx, Immediate(kSmiTagMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00001663 __ j(not_zero, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001664 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001665 __ j(below, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001666 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001667 __ j(below, &not_both_objects, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001668 __ testb(FieldOperand(rbx, Map::kBitFieldOffset),
1669 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00001670 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001671 __ testb(FieldOperand(rcx, Map::kBitFieldOffset),
1672 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00001673 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001674 // The objects are both undetectable, so they both compare as the value
1675 // undefined, and are equal.
1676 __ Set(rax, EQUAL);
1677 __ bind(&return_unequal);
Steve Block1e0659c2011-05-24 12:43:12 +01001678 // Return non-equal by returning the non-zero object pointer in rax,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001679 // or return equal if we fell through to here.
1680 __ ret(0);
1681 __ bind(&not_both_objects);
1682 }
1683
1684 // Push arguments below the return address to prepare jump to builtin.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001685 __ PopReturnAddressTo(rcx);
1686 __ Push(rdx);
1687 __ Push(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001688
1689 // Figure out which native to call and setup the arguments.
1690 Builtins::JavaScript builtin;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001691 if (cc == equal) {
1692 builtin = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001693 } else {
1694 builtin = Builtins::COMPARE;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001695 __ Push(Smi::FromInt(NegativeComparisonResult(cc)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001696 }
1697
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698 __ PushReturnAddressFrom(rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001699
1700 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1701 // tagged as a small integer.
1702 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001703
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001704 __ bind(&miss);
1705 GenerateMiss(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001706}
1707
1708
1709static void GenerateRecordCallTarget(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001710 // Cache the called function in a feedback vector slot. Cache states
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001711 // are uninitialized, monomorphic (indicated by a JSFunction), and
1712 // megamorphic.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001713 // rax : number of arguments to the construct function
1714 // rbx : Feedback vector
1715 // rdx : slot in feedback vector (Smi)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001716 // rdi : the function to call
1717 Isolate* isolate = masm->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001718 Label initialize, done, miss, megamorphic, not_array_function,
1719 done_no_smi_convert;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001720
1721 // Load the cache state into rcx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001722 __ SmiToInteger32(rdx, rdx);
1723 __ movp(rcx, FieldOperand(rbx, rdx, times_pointer_size,
1724 FixedArray::kHeaderSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001725
1726 // A monomorphic cache hit or an already megamorphic state: invoke the
1727 // function without changing the state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001728 __ cmpp(rcx, rdi);
1729 __ j(equal, &done);
1730 __ Cmp(rcx, TypeFeedbackVector::MegamorphicSentinel(isolate));
1731 __ j(equal, &done);
1732
1733 if (!FLAG_pretenuring_call_new) {
1734 // If we came here, we need to see if we are the array function.
1735 // If we didn't have a matching function, and we didn't find the megamorph
1736 // sentinel, then we have in the slot either some other function or an
1737 // AllocationSite. Do a map check on the object in rcx.
1738 Handle<Map> allocation_site_map =
1739 masm->isolate()->factory()->allocation_site_map();
1740 __ Cmp(FieldOperand(rcx, 0), allocation_site_map);
1741 __ j(not_equal, &miss);
1742
1743 // Make sure the function is the Array() function
1744 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, rcx);
1745 __ cmpp(rdi, rcx);
1746 __ j(not_equal, &megamorphic);
1747 __ jmp(&done);
1748 }
1749
1750 __ bind(&miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001751
1752 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1753 // megamorphic.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001754 __ Cmp(rcx, TypeFeedbackVector::UninitializedSentinel(isolate));
1755 __ j(equal, &initialize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001756 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1757 // write-barrier is needed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001758 __ bind(&megamorphic);
1759 __ Move(FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize),
1760 TypeFeedbackVector::MegamorphicSentinel(isolate));
1761 __ jmp(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001762
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001763 // An uninitialized cache is patched with the function or sentinel to
1764 // indicate the ElementsKind if function is the Array constructor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001765 __ bind(&initialize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001766
1767 if (!FLAG_pretenuring_call_new) {
1768 // Make sure the function is the Array() function
1769 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, rcx);
1770 __ cmpp(rdi, rcx);
1771 __ j(not_equal, &not_array_function);
1772
1773 {
1774 FrameScope scope(masm, StackFrame::INTERNAL);
1775
1776 // Arguments register must be smi-tagged to call out.
1777 __ Integer32ToSmi(rax, rax);
1778 __ Push(rax);
1779 __ Push(rdi);
1780 __ Integer32ToSmi(rdx, rdx);
1781 __ Push(rdx);
1782 __ Push(rbx);
1783
1784 CreateAllocationSiteStub create_stub(isolate);
1785 __ CallStub(&create_stub);
1786
1787 __ Pop(rbx);
1788 __ Pop(rdx);
1789 __ Pop(rdi);
1790 __ Pop(rax);
1791 __ SmiToInteger32(rax, rax);
1792 }
1793 __ jmp(&done_no_smi_convert);
1794
1795 __ bind(&not_array_function);
1796 }
1797
1798 __ movp(FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize),
1799 rdi);
1800
1801 // We won't need rdx or rbx anymore, just save rdi
1802 __ Push(rdi);
1803 __ Push(rbx);
1804 __ Push(rdx);
1805 __ RecordWriteArray(rbx, rdi, rdx, kDontSaveFPRegs,
1806 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
1807 __ Pop(rdx);
1808 __ Pop(rbx);
1809 __ Pop(rdi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001810
1811 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001812 __ Integer32ToSmi(rdx, rdx);
1813
1814 __ bind(&done_no_smi_convert);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001815}
1816
1817
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001818static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
1819 // Do not transform the receiver for strict mode functions.
1820 __ movp(rcx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1821 __ testb(FieldOperand(rcx, SharedFunctionInfo::kStrictModeByteOffset),
1822 Immediate(1 << SharedFunctionInfo::kStrictModeBitWithinByte));
1823 __ j(not_equal, cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001824
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001825 // Do not transform the receiver for natives.
1826 // SharedFunctionInfo is already loaded into rcx.
1827 __ testb(FieldOperand(rcx, SharedFunctionInfo::kNativeByteOffset),
1828 Immediate(1 << SharedFunctionInfo::kNativeBitWithinByte));
1829 __ j(not_equal, cont);
1830}
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001831
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001832
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001833static void EmitSlowCase(Isolate* isolate,
1834 MacroAssembler* masm,
1835 StackArgumentsAccessor* args,
1836 int argc,
1837 Label* non_function) {
Ben Murdoch589d6972011-11-30 16:04:58 +00001838 // Check for function proxy.
1839 __ CmpInstanceType(rcx, JS_FUNCTION_PROXY_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001840 __ j(not_equal, non_function);
1841 __ PopReturnAddressTo(rcx);
1842 __ Push(rdi); // put proxy as additional argument under return address
1843 __ PushReturnAddressFrom(rcx);
1844 __ Set(rax, argc + 1);
Ben Murdoch589d6972011-11-30 16:04:58 +00001845 __ Set(rbx, 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00001846 __ GetBuiltinEntry(rdx, Builtins::CALL_FUNCTION_PROXY);
1847 {
1848 Handle<Code> adaptor =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001849 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
Ben Murdoch589d6972011-11-30 16:04:58 +00001850 __ jmp(adaptor, RelocInfo::CODE_TARGET);
1851 }
1852
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001853 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
1854 // of the original receiver from the call site).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001855 __ bind(non_function);
1856 __ movp(args->GetReceiverOperand(), rdi);
1857 __ Set(rax, argc);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001858 __ Set(rbx, 0);
1859 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01001860 Handle<Code> adaptor =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001861 isolate->builtins()->ArgumentsAdaptorTrampoline();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001862 __ Jump(adaptor, RelocInfo::CODE_TARGET);
1863}
1864
1865
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001866static void EmitWrapCase(MacroAssembler* masm,
1867 StackArgumentsAccessor* args,
1868 Label* cont) {
1869 // Wrap the receiver and patch it back onto the stack.
1870 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
1871 __ Push(rdi);
1872 __ Push(rax);
1873 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
1874 __ Pop(rdi);
1875 }
1876 __ movp(args->GetReceiverOperand(), rax);
1877 __ jmp(cont);
1878}
1879
1880
1881static void CallFunctionNoFeedback(MacroAssembler* masm,
1882 int argc, bool needs_checks,
1883 bool call_as_method) {
1884 // rdi : the function to call
1885
1886 // wrap_and_call can only be true if we are compiling a monomorphic method.
1887 Isolate* isolate = masm->isolate();
1888 Label slow, non_function, wrap, cont;
1889 StackArgumentsAccessor args(rsp, argc);
1890
1891 if (needs_checks) {
1892 // Check that the function really is a JavaScript function.
1893 __ JumpIfSmi(rdi, &non_function);
1894
1895 // Goto slow case if we do not have a function.
1896 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
1897 __ j(not_equal, &slow);
1898 }
1899
1900 // Fast-case: Just invoke the function.
1901 ParameterCount actual(argc);
1902
1903 if (call_as_method) {
1904 if (needs_checks) {
1905 EmitContinueIfStrictOrNative(masm, &cont);
1906 }
1907
1908 // Load the receiver from the stack.
1909 __ movp(rax, args.GetReceiverOperand());
1910
1911 if (needs_checks) {
1912 __ JumpIfSmi(rax, &wrap);
1913
1914 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
1915 __ j(below, &wrap);
1916 } else {
1917 __ jmp(&wrap);
1918 }
1919
1920 __ bind(&cont);
1921 }
1922
1923 __ InvokeFunction(rdi, actual, JUMP_FUNCTION, NullCallWrapper());
1924
1925 if (needs_checks) {
1926 // Slow-case: Non-function called.
1927 __ bind(&slow);
1928 EmitSlowCase(isolate, masm, &args, argc, &non_function);
1929 }
1930
1931 if (call_as_method) {
1932 __ bind(&wrap);
1933 EmitWrapCase(masm, &args, &cont);
1934 }
1935}
1936
1937
1938void CallFunctionStub::Generate(MacroAssembler* masm) {
1939 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
1940}
1941
1942
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001943void CallConstructStub::Generate(MacroAssembler* masm) {
1944 // rax : number of arguments
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001945 // rbx : feedback vector
1946 // rdx : (only if rbx is not the megamorphic symbol) slot in feedback
1947 // vector (Smi)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001948 // rdi : constructor function
1949 Label slow, non_function_call;
1950
1951 // Check that function is not a smi.
1952 __ JumpIfSmi(rdi, &non_function_call);
1953 // Check that function is a JSFunction.
1954 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
1955 __ j(not_equal, &slow);
1956
1957 if (RecordCallTarget()) {
1958 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001959
1960 __ SmiToInteger32(rdx, rdx);
1961 if (FLAG_pretenuring_call_new) {
1962 // Put the AllocationSite from the feedback vector into ebx.
1963 // By adding kPointerSize we encode that we know the AllocationSite
1964 // entry is at the feedback vector slot given by rdx + 1.
1965 __ movp(rbx, FieldOperand(rbx, rdx, times_pointer_size,
1966 FixedArray::kHeaderSize + kPointerSize));
1967 } else {
1968 Label feedback_register_initialized;
1969 // Put the AllocationSite from the feedback vector into rbx, or undefined.
1970 __ movp(rbx, FieldOperand(rbx, rdx, times_pointer_size,
1971 FixedArray::kHeaderSize));
1972 __ CompareRoot(FieldOperand(rbx, 0), Heap::kAllocationSiteMapRootIndex);
1973 __ j(equal, &feedback_register_initialized);
1974 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
1975 __ bind(&feedback_register_initialized);
1976 }
1977
1978 __ AssertUndefinedOrAllocationSite(rbx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001979 }
1980
1981 // Jump to the function-specific construct stub.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001982 Register jmp_reg = rcx;
1983 __ movp(jmp_reg, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
1984 __ movp(jmp_reg, FieldOperand(jmp_reg,
1985 SharedFunctionInfo::kConstructStubOffset));
1986 __ leap(jmp_reg, FieldOperand(jmp_reg, Code::kHeaderSize));
1987 __ jmp(jmp_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001988
1989 // rdi: called object
1990 // rax: number of arguments
1991 // rcx: object map
1992 Label do_call;
1993 __ bind(&slow);
1994 __ CmpInstanceType(rcx, JS_FUNCTION_PROXY_TYPE);
1995 __ j(not_equal, &non_function_call);
1996 __ GetBuiltinEntry(rdx, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
1997 __ jmp(&do_call);
1998
1999 __ bind(&non_function_call);
2000 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
2001 __ bind(&do_call);
2002 // Set expected number of arguments to zero (not changing rax).
2003 __ Set(rbx, 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002004 __ Jump(isolate()->builtins()->ArgumentsAdaptorTrampoline(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002005 RelocInfo::CODE_TARGET);
2006}
2007
2008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002009static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
2010 __ movp(vector, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
2011 __ movp(vector, FieldOperand(vector, JSFunction::kSharedFunctionInfoOffset));
2012 __ movp(vector, FieldOperand(vector,
2013 SharedFunctionInfo::kFeedbackVectorOffset));
2014}
2015
2016
2017void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
2018 // rdi - function
2019 // rdx - slot id (as integer)
2020 Label miss;
2021 int argc = arg_count();
2022 ParameterCount actual(argc);
2023
2024 EmitLoadTypeFeedbackVector(masm, rbx);
2025 __ SmiToInteger32(rdx, rdx);
2026
2027 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, rcx);
2028 __ cmpp(rdi, rcx);
2029 __ j(not_equal, &miss);
2030
2031 __ movp(rax, Immediate(arg_count()));
2032 __ movp(rcx, FieldOperand(rbx, rdx, times_pointer_size,
2033 FixedArray::kHeaderSize));
2034 // Verify that ecx contains an AllocationSite
2035 Factory* factory = masm->isolate()->factory();
2036 __ Cmp(FieldOperand(rcx, HeapObject::kMapOffset),
2037 factory->allocation_site_map());
2038 __ j(not_equal, &miss);
2039
2040 __ movp(rbx, rcx);
2041 ArrayConstructorStub stub(masm->isolate(), arg_count());
2042 __ TailCallStub(&stub);
2043
2044 __ bind(&miss);
2045 GenerateMiss(masm);
2046
2047 // The slow case, we need this no matter what to complete a call after a miss.
2048 CallFunctionNoFeedback(masm,
2049 arg_count(),
2050 true,
2051 CallAsMethod());
2052
2053 // Unreachable.
2054 __ int3();
2055}
2056
2057
2058void CallICStub::Generate(MacroAssembler* masm) {
2059 // rdi - function
2060 // rdx - slot id
2061 Isolate* isolate = masm->isolate();
2062 Label extra_checks_or_miss, slow_start;
2063 Label slow, non_function, wrap, cont;
2064 Label have_js_function;
2065 int argc = arg_count();
2066 StackArgumentsAccessor args(rsp, argc);
2067 ParameterCount actual(argc);
2068
2069 EmitLoadTypeFeedbackVector(masm, rbx);
2070
2071 // The checks. First, does rdi match the recorded monomorphic target?
2072 __ SmiToInteger32(rdx, rdx);
2073 __ cmpp(rdi, FieldOperand(rbx, rdx, times_pointer_size,
2074 FixedArray::kHeaderSize));
2075 __ j(not_equal, &extra_checks_or_miss);
2076
2077 __ bind(&have_js_function);
2078 if (CallAsMethod()) {
2079 EmitContinueIfStrictOrNative(masm, &cont);
2080
2081 // Load the receiver from the stack.
2082 __ movp(rax, args.GetReceiverOperand());
2083
2084 __ JumpIfSmi(rax, &wrap);
2085
2086 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
2087 __ j(below, &wrap);
2088
2089 __ bind(&cont);
2090 }
2091
2092 __ InvokeFunction(rdi, actual, JUMP_FUNCTION, NullCallWrapper());
2093
2094 __ bind(&slow);
2095 EmitSlowCase(isolate, masm, &args, argc, &non_function);
2096
2097 if (CallAsMethod()) {
2098 __ bind(&wrap);
2099 EmitWrapCase(masm, &args, &cont);
2100 }
2101
2102 __ bind(&extra_checks_or_miss);
2103 Label miss;
2104
2105 __ movp(rcx, FieldOperand(rbx, rdx, times_pointer_size,
2106 FixedArray::kHeaderSize));
2107 __ Cmp(rcx, TypeFeedbackVector::MegamorphicSentinel(isolate));
2108 __ j(equal, &slow_start);
2109 __ Cmp(rcx, TypeFeedbackVector::UninitializedSentinel(isolate));
2110 __ j(equal, &miss);
2111
2112 if (!FLAG_trace_ic) {
2113 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2114 // to handle it here. More complex cases are dealt with in the runtime.
2115 __ AssertNotSmi(rcx);
2116 __ CmpObjectType(rcx, JS_FUNCTION_TYPE, rcx);
2117 __ j(not_equal, &miss);
2118 __ Move(FieldOperand(rbx, rdx, times_pointer_size, FixedArray::kHeaderSize),
2119 TypeFeedbackVector::MegamorphicSentinel(isolate));
2120 __ jmp(&slow_start);
2121 }
2122
2123 // We are here because tracing is on or we are going monomorphic.
2124 __ bind(&miss);
2125 GenerateMiss(masm);
2126
2127 // the slow case
2128 __ bind(&slow_start);
2129 // Check that function is not a smi.
2130 __ JumpIfSmi(rdi, &non_function);
2131 // Check that function is a JSFunction.
2132 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
2133 __ j(not_equal, &slow);
2134 __ jmp(&have_js_function);
2135
2136 // Unreachable
2137 __ int3();
2138}
2139
2140
2141void CallICStub::GenerateMiss(MacroAssembler* masm) {
2142 // Get the receiver of the function from the stack; 1 ~ return address.
2143 __ movp(rcx, Operand(rsp, (arg_count() + 1) * kPointerSize));
2144
2145 {
2146 FrameScope scope(masm, StackFrame::INTERNAL);
2147
2148 // Push the receiver and the function and feedback info.
2149 __ Push(rcx);
2150 __ Push(rdi);
2151 __ Push(rbx);
2152 __ Integer32ToSmi(rdx, rdx);
2153 __ Push(rdx);
2154
2155 // Call the entry.
2156 IC::UtilityId id = GetICState() == DEFAULT ? IC::kCallIC_Miss
2157 : IC::kCallIC_Customization_Miss;
2158
2159 ExternalReference miss = ExternalReference(IC_Utility(id),
2160 masm->isolate());
2161 __ CallExternalReference(miss, 4);
2162
2163 // Move result to edi and exit the internal frame.
2164 __ movp(rdi, rax);
2165 }
2166}
2167
2168
Steve Block44f0eee2011-05-26 01:26:41 +01002169bool CEntryStub::NeedsImmovableCode() {
2170 return false;
2171}
2172
2173
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002174void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
2175 CEntryStub::GenerateAheadOfTime(isolate);
2176 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
2177 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002178 // It is important that the store buffer overflow stubs are generated first.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002179 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
2180 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
2181 BinaryOpICStub::GenerateAheadOfTime(isolate);
2182 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002183}
2184
2185
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002186void CodeStub::GenerateFPStubs(Isolate* isolate) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002187}
2188
2189
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002190void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
2191 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
2192 stub.GetCode();
2193 CEntryStub save_doubles(isolate, 1, kSaveFPRegs);
2194 save_doubles.GetCode();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002195}
2196
2197
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002198void CEntryStub::Generate(MacroAssembler* masm) {
2199 // rax: number of arguments including receiver
2200 // rbx: pointer to C function (C callee-saved)
2201 // rbp: frame pointer of calling JS frame (restored after C call)
2202 // rsp: stack pointer (restored after C call)
2203 // rsi: current context (restored)
2204
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002205 ProfileEntryHookStub::MaybeCallEntryHook(masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002206
2207 // Enter the exit frame that transitions from JavaScript to C++.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002208#ifdef _WIN64
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002209 int arg_stack_space = (result_size() < 2 ? 2 : 4);
2210#else // _WIN64
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08002211 int arg_stack_space = 0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002212#endif // _WIN64
2213 __ EnterExitFrame(arg_stack_space, save_doubles());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002214
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002215 // rbx: pointer to builtin function (C callee-saved).
2216 // rbp: frame pointer of exit frame (restored after C call).
2217 // rsp: stack pointer (restored after C call).
2218 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01002219 // r15: argv pointer (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002220
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002221 // Simple results returned in rax (both AMD64 and Win64 calling conventions).
2222 // Complex results must be written to address passed as first argument.
2223 // AMD64 calling convention: a struct of two pointers in rax+rdx
2224
2225 // Check stack alignment.
2226 if (FLAG_debug_code) {
2227 __ CheckStackAlignment();
2228 }
2229
2230 // Call C function.
2231#ifdef _WIN64
2232 // Windows 64-bit ABI passes arguments in rcx, rdx, r8, r9.
2233 // Pass argv and argc as two parameters. The arguments object will
2234 // be created by stubs declared by DECLARE_RUNTIME_FUNCTION().
2235 if (result_size() < 2) {
2236 // Pass a pointer to the Arguments object as the first argument.
2237 // Return result in single register (rax).
2238 __ movp(rcx, r14); // argc.
2239 __ movp(rdx, r15); // argv.
2240 __ Move(r8, ExternalReference::isolate_address(isolate()));
2241 } else {
2242 DCHECK_EQ(2, result_size());
2243 // Pass a pointer to the result location as the first argument.
2244 __ leap(rcx, StackSpaceOperand(2));
2245 // Pass a pointer to the Arguments object as the second argument.
2246 __ movp(rdx, r14); // argc.
2247 __ movp(r8, r15); // argv.
2248 __ Move(r9, ExternalReference::isolate_address(isolate()));
2249 }
2250
2251#else // _WIN64
2252 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9.
2253 __ movp(rdi, r14); // argc.
2254 __ movp(rsi, r15); // argv.
2255 __ Move(rdx, ExternalReference::isolate_address(isolate()));
2256#endif // _WIN64
2257 __ call(rbx);
2258 // Result is in rax - do not destroy this register!
2259
2260#ifdef _WIN64
2261 // If return value is on the stack, pop it to registers.
2262 if (result_size() > 1) {
2263 DCHECK_EQ(2, result_size());
2264 // Read result values stored on stack. Result is stored
2265 // above the four argument mirror slots and the two
2266 // Arguments object slots.
2267 __ movq(rax, Operand(rsp, 6 * kRegisterSize));
2268 __ movq(rdx, Operand(rsp, 7 * kRegisterSize));
2269 }
2270#endif // _WIN64
2271
2272 // Runtime functions should not return 'the hole'. Allowing it to escape may
2273 // lead to crashes in the IC code later.
2274 if (FLAG_debug_code) {
2275 Label okay;
2276 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
2277 __ j(not_equal, &okay, Label::kNear);
2278 __ int3();
2279 __ bind(&okay);
2280 }
2281
2282 // Check result for exception sentinel.
2283 Label exception_returned;
2284 __ CompareRoot(rax, Heap::kExceptionRootIndex);
2285 __ j(equal, &exception_returned);
2286
2287 ExternalReference pending_exception_address(
2288 Isolate::kPendingExceptionAddress, isolate());
2289
2290 // Check that there is no pending exception, otherwise we
2291 // should have returned the exception sentinel.
2292 if (FLAG_debug_code) {
2293 Label okay;
2294 __ LoadRoot(r14, Heap::kTheHoleValueRootIndex);
2295 Operand pending_exception_operand =
2296 masm->ExternalOperand(pending_exception_address);
2297 __ cmpp(r14, pending_exception_operand);
2298 __ j(equal, &okay, Label::kNear);
2299 __ int3();
2300 __ bind(&okay);
2301 }
2302
2303 // Exit the JavaScript to C++ exit frame.
2304 __ LeaveExitFrame(save_doubles());
2305 __ ret(0);
2306
2307 // Handling of exception.
2308 __ bind(&exception_returned);
2309
2310 // Retrieve the pending exception.
2311 Operand pending_exception_operand =
2312 masm->ExternalOperand(pending_exception_address);
2313 __ movp(rax, pending_exception_operand);
2314
2315 // Clear the pending exception.
2316 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
2317 __ movp(pending_exception_operand, rdx);
2318
2319 // Special handling of termination exceptions which are uncatchable
2320 // by javascript code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002321 Label throw_termination_exception;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002322 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
2323 __ j(equal, &throw_termination_exception);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002324
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002325 // Handle normal exception.
2326 __ Throw(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002327
2328 __ bind(&throw_termination_exception);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002329 __ ThrowUncatchable(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002330}
2331
2332
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002333void JSEntryStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002334 Label invoke, handler_entry, exit;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002335 Label not_outermost_js, not_outermost_js_2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002336
2337 ProfileEntryHookStub::MaybeCallEntryHook(masm);
2338
Steve Block44f0eee2011-05-26 01:26:41 +01002339 { // NOLINT. Scope block confuses linter.
2340 MacroAssembler::NoRootArrayScope uninitialized_root_register(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002341 // Set up frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002342 __ pushq(rbp);
2343 __ movp(rbp, rsp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002344
Steve Block44f0eee2011-05-26 01:26:41 +01002345 // Push the stack frame type marker twice.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002346 int marker = type();
Steve Block44f0eee2011-05-26 01:26:41 +01002347 // Scratch register is neither callee-save, nor an argument register on any
2348 // platform. It's free to use at this point.
2349 // Cannot use smi-register for loading yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002350 __ Move(kScratchRegister, Smi::FromInt(marker), Assembler::RelocInfoNone());
2351 __ Push(kScratchRegister); // context slot
2352 __ Push(kScratchRegister); // function slot
2353 // Save callee-saved registers (X64/X32/Win64 calling conventions).
2354 __ pushq(r12);
2355 __ pushq(r13);
2356 __ pushq(r14);
2357 __ pushq(r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002358#ifdef _WIN64
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002359 __ pushq(rdi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
2360 __ pushq(rsi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002361#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002362 __ pushq(rbx);
2363
2364#ifdef _WIN64
2365 // On Win64 XMM6-XMM15 are callee-save
2366 __ subp(rsp, Immediate(EntryFrameConstants::kXMMRegistersBlockSize));
2367 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 0), xmm6);
2368 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 1), xmm7);
2369 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 2), xmm8);
2370 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 3), xmm9);
2371 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 4), xmm10);
2372 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 5), xmm11);
2373 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 6), xmm12);
2374 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 7), xmm13);
2375 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 8), xmm14);
2376 __ movdqu(Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 9), xmm15);
2377#endif
Steve Block44f0eee2011-05-26 01:26:41 +01002378
2379 // Set up the roots and smi constant registers.
2380 // Needs to be done before any further smi loads.
2381 __ InitializeSmiConstantRegister();
2382 __ InitializeRootRegister();
2383 }
2384
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002385 // Save copies of the top frame descriptor on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002386 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01002387 {
2388 Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002389 __ Push(c_entry_fp_operand);
Steve Block44f0eee2011-05-26 01:26:41 +01002390 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002391
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002392 // If this is the outermost JS call, set js_entry_sp value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002393 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01002394 __ Load(rax, js_entry_sp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002395 __ testp(rax, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002396 __ j(not_zero, &not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01002397 __ Push(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002398 __ movp(rax, rbp);
Steve Block44f0eee2011-05-26 01:26:41 +01002399 __ Store(js_entry_sp, rax);
Steve Block053d10c2011-06-13 19:13:29 +01002400 Label cont;
2401 __ jmp(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002402 __ bind(&not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01002403 __ Push(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME));
2404 __ bind(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002405
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002406 // Jump to a faked try block that does the invoke, with a faked catch
2407 // block that sets the pending exception.
2408 __ jmp(&invoke);
2409 __ bind(&handler_entry);
2410 handler_offset_ = handler_entry.pos();
2411 // Caught exception: Store result (exception) in the pending exception
2412 // field in the JSEnv and return a failure sentinel.
Ben Murdoch589d6972011-11-30 16:04:58 +00002413 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002414 isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01002415 __ Store(pending_exception, rax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002416 __ LoadRoot(rax, Heap::kExceptionRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002417 __ jmp(&exit);
2418
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002419 // Invoke: Link this frame into the handler chain. There's only one
2420 // handler block in this code object, so its index is 0.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002421 __ bind(&invoke);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002422 __ PushTryHandler(StackHandler::JS_ENTRY, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002423
2424 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01002425 __ LoadRoot(rax, Heap::kTheHoleValueRootIndex);
2426 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002427
2428 // Fake a receiver (NULL).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002429 __ Push(Immediate(0)); // receiver
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002430
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002431 // Invoke the function by calling through JS entry trampoline builtin and
2432 // pop the faked function when we return. We load the address from an
2433 // external reference instead of inlining the call target address directly
2434 // in the code, because the builtin stubs may not have been generated yet
2435 // at the time this code is generated.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002436 if (type() == StackFrame::ENTRY_CONSTRUCT) {
Steve Block44f0eee2011-05-26 01:26:41 +01002437 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002438 isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01002439 __ Load(rax, construct_entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002440 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002441 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01002442 __ Load(rax, entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002443 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002444 __ leap(kScratchRegister, FieldOperand(rax, Code::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002445 __ call(kScratchRegister);
2446
2447 // Unlink this frame from the handler chain.
Steve Block053d10c2011-06-13 19:13:29 +01002448 __ PopTryHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002449
Steve Block053d10c2011-06-13 19:13:29 +01002450 __ bind(&exit);
Steve Block053d10c2011-06-13 19:13:29 +01002451 // Check if the current stack frame is marked as the outermost JS frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002452 __ Pop(rbx);
Steve Block053d10c2011-06-13 19:13:29 +01002453 __ Cmp(rbx, Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002454 __ j(not_equal, &not_outermost_js_2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002455 __ Move(kScratchRegister, js_entry_sp);
2456 __ movp(Operand(kScratchRegister, 0), Immediate(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002457 __ bind(&not_outermost_js_2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002458
2459 // Restore the top frame descriptor from the stack.
Steve Block053d10c2011-06-13 19:13:29 +01002460 { Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002461 __ Pop(c_entry_fp_operand);
Steve Block44f0eee2011-05-26 01:26:41 +01002462 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002463
2464 // Restore callee-saved registers (X64 conventions).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002465#ifdef _WIN64
2466 // On Win64 XMM6-XMM15 are callee-save
2467 __ movdqu(xmm6, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 0));
2468 __ movdqu(xmm7, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 1));
2469 __ movdqu(xmm8, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 2));
2470 __ movdqu(xmm9, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 3));
2471 __ movdqu(xmm10, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 4));
2472 __ movdqu(xmm11, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 5));
2473 __ movdqu(xmm12, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 6));
2474 __ movdqu(xmm13, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 7));
2475 __ movdqu(xmm14, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 8));
2476 __ movdqu(xmm15, Operand(rsp, EntryFrameConstants::kXMMRegisterSize * 9));
2477 __ addp(rsp, Immediate(EntryFrameConstants::kXMMRegistersBlockSize));
2478#endif
2479
2480 __ popq(rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002481#ifdef _WIN64
2482 // Callee save on in Win64 ABI, arguments/volatile in AMD64 ABI.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002483 __ popq(rsi);
2484 __ popq(rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002485#endif
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002486 __ popq(r15);
2487 __ popq(r14);
2488 __ popq(r13);
2489 __ popq(r12);
2490 __ addp(rsp, Immediate(2 * kPointerSize)); // remove markers
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002491
2492 // Restore frame pointer and return.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002493 __ popq(rbp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002494 __ ret(0);
2495}
2496
2497
2498void InstanceofStub::Generate(MacroAssembler* masm) {
2499 // Implements "value instanceof function" operator.
Steve Block44f0eee2011-05-26 01:26:41 +01002500 // Expected input state with no inline cache:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002501 // rsp[0] : return address
2502 // rsp[8] : function pointer
2503 // rsp[16] : value
Steve Block44f0eee2011-05-26 01:26:41 +01002504 // Expected input state with an inline one-element cache:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002505 // rsp[0] : return address
2506 // rsp[8] : offset from return address to location of inline cache
2507 // rsp[16] : function pointer
2508 // rsp[24] : value
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002509 // Returns a bitwise zero to indicate that the value
2510 // is and instance of the function and anything else to
2511 // indicate that the value is not an instance.
2512
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002513 // Fixed register usage throughout the stub.
2514 Register object = rax; // Object (lhs).
2515 Register map = rbx; // Map of the object.
2516 Register function = rdx; // Function (rhs).
2517 Register prototype = rdi; // Prototype of the function.
2518 Register scratch = rcx;
2519
Ben Murdoch8b112d22011-06-08 16:22:53 +01002520 static const int kOffsetToMapCheckValue = 2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002521 static const int kOffsetToResultValue = kPointerSize == kInt64Size ? 18 : 14;
Steve Block44f0eee2011-05-26 01:26:41 +01002522 // The last 4 bytes of the instruction sequence
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002523 // movp(rdi, FieldOperand(rax, HeapObject::kMapOffset))
2524 // Move(kScratchRegister, Factory::the_hole_value())
Steve Block44f0eee2011-05-26 01:26:41 +01002525 // in front of the hole value address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002526 static const unsigned int kWordBeforeMapCheckValue =
2527 kPointerSize == kInt64Size ? 0xBA49FF78 : 0xBA41FF78;
Steve Block44f0eee2011-05-26 01:26:41 +01002528 // The last 4 bytes of the instruction sequence
2529 // __ j(not_equal, &cache_miss);
2530 // __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
2531 // before the offset of the hole value in the root array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002532 static const unsigned int kWordBeforeResultValue =
2533 kPointerSize == kInt64Size ? 0x458B4906 : 0x458B4106;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002534
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002535 int extra_argument_offset = HasCallSiteInlineCheck() ? 1 : 0;
2536
2537 DCHECK_EQ(object.code(), InstanceofStub::left().code());
2538 DCHECK_EQ(function.code(), InstanceofStub::right().code());
2539
2540 // Get the object and function - they are always both needed.
2541 // Go slow case if the object is a smi.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002542 Label slow;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002543 StackArgumentsAccessor args(rsp, 2 + extra_argument_offset,
2544 ARGUMENTS_DONT_CONTAIN_RECEIVER);
2545 if (!HasArgsInRegisters()) {
2546 __ movp(object, args.GetArgumentOperand(0));
2547 __ movp(function, args.GetArgumentOperand(1));
2548 }
2549 __ JumpIfSmi(object, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002550
2551 // Check that the left hand is a JS object. Leave its map in rax.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002552 __ CmpObjectType(object, FIRST_SPEC_OBJECT_TYPE, map);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002553 __ j(below, &slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002554 __ CmpInstanceType(map, LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002555 __ j(above, &slow);
2556
Steve Block44f0eee2011-05-26 01:26:41 +01002557 // If there is a call site cache don't look in the global cache, but do the
2558 // real lookup and update the call site cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002559 if (!HasCallSiteInlineCheck() && !ReturnTrueFalseObject()) {
Steve Block44f0eee2011-05-26 01:26:41 +01002560 // Look up the function and the map in the instanceof cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00002561 Label miss;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002562 __ CompareRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002563 __ j(not_equal, &miss, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002564 __ CompareRoot(map, Heap::kInstanceofCacheMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002565 __ j(not_equal, &miss, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01002566 __ LoadRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002567 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
Steve Block44f0eee2011-05-26 01:26:41 +01002568 __ bind(&miss);
2569 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002570
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002571 // Get the prototype of the function.
2572 __ TryGetFunctionPrototype(function, prototype, &slow, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002573
2574 // Check that the function prototype is a JS object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002575 __ JumpIfSmi(prototype, &slow);
2576 __ CmpObjectType(prototype, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002577 __ j(below, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002578 __ CmpInstanceType(kScratchRegister, LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002579 __ j(above, &slow);
2580
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002581 // Update the global instanceof or call site inlined cache with the current
2582 // map and function. The cached answer will be set when it is known below.
Steve Block44f0eee2011-05-26 01:26:41 +01002583 if (!HasCallSiteInlineCheck()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002584 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
2585 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01002586 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002587 // The constants for the code patching are based on push instructions
2588 // at the call site.
2589 DCHECK(!HasArgsInRegisters());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002590 // Get return address and delta to inlined map check.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002591 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2592 __ subp(kScratchRegister, args.GetArgumentOperand(2));
Steve Block44f0eee2011-05-26 01:26:41 +01002593 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002594 __ movl(scratch, Immediate(kWordBeforeMapCheckValue));
2595 __ cmpl(Operand(kScratchRegister, kOffsetToMapCheckValue - 4), scratch);
2596 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheCheck);
Steve Block44f0eee2011-05-26 01:26:41 +01002597 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002598 __ movp(kScratchRegister,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002599 Operand(kScratchRegister, kOffsetToMapCheckValue));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002600 __ movp(Operand(kScratchRegister, 0), map);
Steve Block44f0eee2011-05-26 01:26:41 +01002601 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002602
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002603 // Loop through the prototype chain looking for the function prototype.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002604 __ movp(scratch, FieldOperand(map, Map::kPrototypeOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002605 Label loop, is_instance, is_not_instance;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002606 __ LoadRoot(kScratchRegister, Heap::kNullValueRootIndex);
2607 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002608 __ cmpp(scratch, prototype);
Ben Murdoch257744e2011-11-30 15:57:28 +00002609 __ j(equal, &is_instance, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002610 __ cmpp(scratch, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002611 // The code at is_not_instance assumes that kScratchRegister contains a
2612 // non-zero GCable value (the null object in this case).
Ben Murdoch257744e2011-11-30 15:57:28 +00002613 __ j(equal, &is_not_instance, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002614 __ movp(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
2615 __ movp(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002616 __ jmp(&loop);
2617
2618 __ bind(&is_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01002619 if (!HasCallSiteInlineCheck()) {
2620 __ xorl(rax, rax);
2621 // Store bitwise zero in the cache. This is a Smi in GC terms.
2622 STATIC_ASSERT(kSmiTag == 0);
2623 __ StoreRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002624 if (ReturnTrueFalseObject()) {
2625 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
2626 }
Steve Block44f0eee2011-05-26 01:26:41 +01002627 } else {
2628 // Store offset of true in the root array at the inline check site.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002629 int true_offset = 0x100 +
2630 (Heap::kTrueValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
2631 // Assert it is a 1-byte signed value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002632 DCHECK(true_offset >= 0 && true_offset < 0x100);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002633 __ movl(rax, Immediate(true_offset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002634 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2635 __ subp(kScratchRegister, args.GetArgumentOperand(2));
Steve Block44f0eee2011-05-26 01:26:41 +01002636 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
2637 if (FLAG_debug_code) {
2638 __ movl(rax, Immediate(kWordBeforeResultValue));
2639 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002640 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
Steve Block44f0eee2011-05-26 01:26:41 +01002641 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002642 if (!ReturnTrueFalseObject()) {
2643 __ Set(rax, 0);
2644 }
Steve Block44f0eee2011-05-26 01:26:41 +01002645 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002646 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2647 kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002648
2649 __ bind(&is_not_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01002650 if (!HasCallSiteInlineCheck()) {
2651 // We have to store a non-zero value in the cache.
2652 __ StoreRoot(kScratchRegister, Heap::kInstanceofCacheAnswerRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002653 if (ReturnTrueFalseObject()) {
2654 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
2655 }
Steve Block44f0eee2011-05-26 01:26:41 +01002656 } else {
2657 // Store offset of false in the root array at the inline check site.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002658 int false_offset = 0x100 +
2659 (Heap::kFalseValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
2660 // Assert it is a 1-byte signed value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002661 DCHECK(false_offset >= 0 && false_offset < 0x100);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002662 __ movl(rax, Immediate(false_offset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002663 __ movq(kScratchRegister, StackOperandForReturnAddress(0));
2664 __ subp(kScratchRegister, args.GetArgumentOperand(2));
Steve Block44f0eee2011-05-26 01:26:41 +01002665 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
2666 if (FLAG_debug_code) {
2667 __ movl(rax, Immediate(kWordBeforeResultValue));
2668 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002669 __ Assert(equal, kInstanceofStubUnexpectedCallSiteCacheMov);
Steve Block44f0eee2011-05-26 01:26:41 +01002670 }
2671 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002672 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2673 kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002674
2675 // Slow-case: Go through the JavaScript implementation.
2676 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002677 if (!ReturnTrueFalseObject()) {
2678 // Tail call the builtin which returns 0 or 1.
2679 DCHECK(!HasArgsInRegisters());
2680 if (HasCallSiteInlineCheck()) {
2681 // Remove extra value from the stack.
2682 __ PopReturnAddressTo(rcx);
2683 __ Pop(rax);
2684 __ PushReturnAddressFrom(rcx);
2685 }
2686 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
2687 } else {
2688 // Call the builtin and convert 0/1 to true/false.
2689 {
2690 FrameScope scope(masm, StackFrame::INTERNAL);
2691 __ Push(object);
2692 __ Push(function);
2693 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
2694 }
2695 Label true_value, done;
2696 __ testq(rax, rax);
2697 __ j(zero, &true_value, Label::kNear);
2698 __ LoadRoot(rax, Heap::kFalseValueRootIndex);
2699 __ jmp(&done, Label::kNear);
2700 __ bind(&true_value);
2701 __ LoadRoot(rax, Heap::kTrueValueRootIndex);
2702 __ bind(&done);
2703 __ ret(((HasArgsInRegisters() ? 0 : 2) + extra_argument_offset) *
2704 kPointerSize);
Steve Block44f0eee2011-05-26 01:26:41 +01002705 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002706}
2707
2708
2709// -------------------------------------------------------------------------
2710// StringCharCodeAtGenerator
2711
2712void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002713 // If the receiver is a smi trigger the non-string case.
2714 __ JumpIfSmi(object_, receiver_not_string_);
2715
2716 // Fetch the instance type of the receiver into result register.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002717 __ movp(result_, FieldOperand(object_, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002718 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2719 // If the receiver is not a string trigger the non-string case.
2720 __ testb(result_, Immediate(kIsNotStringMask));
2721 __ j(not_zero, receiver_not_string_);
2722
2723 // If the index is non-smi trigger the non-smi case.
2724 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002725 __ bind(&got_smi_index_);
2726
2727 // Check for index out of range.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002728 __ SmiCompare(index_, FieldOperand(object_, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002729 __ j(above_equal, index_out_of_range_);
2730
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002731 __ SmiToInteger32(index_, index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002732
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002733 StringCharLoadGenerator::Generate(
2734 masm, object_, index_, result_, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002735
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002736 __ Integer32ToSmi(result_, result_);
2737 __ bind(&exit_);
2738}
2739
2740
2741void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002742 MacroAssembler* masm,
2743 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002744 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002745
Ben Murdoch257744e2011-11-30 15:57:28 +00002746 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002747 // Index is not a smi.
2748 __ bind(&index_not_smi_);
2749 // If index is a heap number, try converting it to an integer.
Ben Murdoch257744e2011-11-30 15:57:28 +00002750 __ CheckMap(index_,
2751 factory->heap_number_map(),
2752 index_not_number_,
2753 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002754 call_helper.BeforeCall(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002755 __ Push(object_);
2756 __ Push(index_); // Consumed by runtime conversion function.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002757 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
2758 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
2759 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002760 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002761 // NumberToSmi discards numbers that are not exact integers.
2762 __ CallRuntime(Runtime::kNumberToSmi, 1);
2763 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002764 if (!index_.is(rax)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002765 // Save the conversion result before the pop instructions below
2766 // have a chance to overwrite it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002767 __ movp(index_, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002768 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002769 __ Pop(object_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002770 // Reload the instance type.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002771 __ movp(result_, FieldOperand(object_, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002772 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2773 call_helper.AfterCall(masm);
2774 // If index is still not a smi, it must be out of range.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002775 __ JumpIfNotSmi(index_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002776 // Otherwise, return to the fast path.
2777 __ jmp(&got_smi_index_);
2778
2779 // Call runtime. We get here when the receiver is a string and the
2780 // index is a number, but the code of getting the actual character
2781 // is too complex (e.g., when the string needs to be flattened).
2782 __ bind(&call_runtime_);
2783 call_helper.BeforeCall(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002784 __ Push(object_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002785 __ Integer32ToSmi(index_, index_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002786 __ Push(index_);
2787 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002788 if (!result_.is(rax)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002789 __ movp(result_, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002790 }
2791 call_helper.AfterCall(masm);
2792 __ jmp(&exit_);
2793
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002794 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002795}
2796
2797
2798// -------------------------------------------------------------------------
2799// StringCharFromCodeGenerator
2800
2801void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
2802 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2803 __ JumpIfNotSmi(code_, &slow_case_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002804 __ SmiCompare(code_, Smi::FromInt(String::kMaxOneByteCharCode));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002805 __ j(above, &slow_case_);
2806
2807 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
2808 SmiIndex index = masm->SmiToIndex(kScratchRegister, code_, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002809 __ movp(result_, FieldOperand(result_, index.reg, index.scale,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002810 FixedArray::kHeaderSize));
2811 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
2812 __ j(equal, &slow_case_);
2813 __ bind(&exit_);
2814}
2815
2816
2817void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002818 MacroAssembler* masm,
2819 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002820 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002821
2822 __ bind(&slow_case_);
2823 call_helper.BeforeCall(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002824 __ Push(code_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002825 __ CallRuntime(Runtime::kCharFromCode, 1);
2826 if (!result_.is(rax)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002827 __ movp(result_, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002828 }
2829 call_helper.AfterCall(masm);
2830 __ jmp(&exit_);
2831
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002832 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002833}
2834
2835
2836void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2837 Register dest,
2838 Register src,
2839 Register count,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002840 String::Encoding encoding) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002841 // Nothing to do for zero characters.
Ben Murdoch257744e2011-11-30 15:57:28 +00002842 Label done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002843 __ testl(count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00002844 __ j(zero, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002845
2846 // Make count the number of bytes to copy.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002847 if (encoding == String::TWO_BYTE_ENCODING) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002848 STATIC_ASSERT(2 == sizeof(uc16));
2849 __ addl(count, count);
2850 }
2851
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002852 // Copy remaining characters.
2853 Label loop;
2854 __ bind(&loop);
2855 __ movb(kScratchRegister, Operand(src, 0));
2856 __ movb(Operand(dest, 0), kScratchRegister);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002857 __ incp(src);
2858 __ incp(dest);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002859 __ decl(count);
2860 __ j(not_zero, &loop);
2861
2862 __ bind(&done);
2863}
2864
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002865
2866void SubStringStub::Generate(MacroAssembler* masm) {
2867 Label runtime;
2868
2869 // Stack frame on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002870 // rsp[0] : return address
2871 // rsp[8] : to
2872 // rsp[16] : from
2873 // rsp[24] : string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002874
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002875 enum SubStringStubArgumentIndices {
2876 STRING_ARGUMENT_INDEX,
2877 FROM_ARGUMENT_INDEX,
2878 TO_ARGUMENT_INDEX,
2879 SUB_STRING_ARGUMENT_COUNT
2880 };
2881
2882 StackArgumentsAccessor args(rsp, SUB_STRING_ARGUMENT_COUNT,
2883 ARGUMENTS_DONT_CONTAIN_RECEIVER);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002884
2885 // Make sure first argument is a string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002886 __ movp(rax, args.GetArgumentOperand(STRING_ARGUMENT_INDEX));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002887 STATIC_ASSERT(kSmiTag == 0);
2888 __ testl(rax, Immediate(kSmiTagMask));
2889 __ j(zero, &runtime);
2890 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
2891 __ j(NegateCondition(is_string), &runtime);
2892
2893 // rax: string
2894 // rbx: instance type
2895 // Calculate length of sub string using the smi values.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002896 __ movp(rcx, args.GetArgumentOperand(TO_ARGUMENT_INDEX));
2897 __ movp(rdx, args.GetArgumentOperand(FROM_ARGUMENT_INDEX));
Ben Murdochf87a2032010-10-22 12:50:53 +01002898 __ JumpUnlessBothNonNegativeSmi(rcx, rdx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002899
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002900 __ SmiSub(rcx, rcx, rdx); // Overflow doesn't happen.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002901 __ cmpp(rcx, FieldOperand(rax, String::kLengthOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002902 Label not_original_string;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002903 // Shorter than original string's length: an actual substring.
2904 __ j(below, &not_original_string, Label::kNear);
2905 // Longer than original string's length or negative: unsafe arguments.
2906 __ j(above, &runtime);
2907 // Return original string.
2908 Counters* counters = isolate()->counters();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002909 __ IncrementCounter(counters->sub_string_native(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002910 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002911 __ bind(&not_original_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002912
2913 Label single_char;
2914 __ SmiCompare(rcx, Smi::FromInt(1));
2915 __ j(equal, &single_char);
2916
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002917 __ SmiToInteger32(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002918
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002919 // rax: string
2920 // rbx: instance type
2921 // rcx: sub string length
2922 // rdx: from index (smi)
2923 // Deal with different string types: update the index if necessary
2924 // and put the underlying string into edi.
2925 Label underlying_unpacked, sliced_string, seq_or_external_string;
2926 // If the string is not indirect, it can only be sequential or external.
2927 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2928 STATIC_ASSERT(kIsIndirectStringMask != 0);
2929 __ testb(rbx, Immediate(kIsIndirectStringMask));
2930 __ j(zero, &seq_or_external_string, Label::kNear);
2931
2932 __ testb(rbx, Immediate(kSlicedNotConsMask));
2933 __ j(not_zero, &sliced_string, Label::kNear);
2934 // Cons string. Check whether it is flat, then fetch first part.
2935 // Flat cons strings have an empty second part.
2936 __ CompareRoot(FieldOperand(rax, ConsString::kSecondOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002937 Heap::kempty_stringRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002938 __ j(not_equal, &runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002939 __ movp(rdi, FieldOperand(rax, ConsString::kFirstOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002940 // Update instance type.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002941 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002942 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002943 __ jmp(&underlying_unpacked, Label::kNear);
2944
2945 __ bind(&sliced_string);
2946 // Sliced string. Fetch parent and correct start index by offset.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002947 __ addp(rdx, FieldOperand(rax, SlicedString::kOffsetOffset));
2948 __ movp(rdi, FieldOperand(rax, SlicedString::kParentOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002949 // Update instance type.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002950 __ movp(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002951 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
2952 __ jmp(&underlying_unpacked, Label::kNear);
2953
2954 __ bind(&seq_or_external_string);
2955 // Sequential or external string. Just move string to the correct register.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002956 __ movp(rdi, rax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002957
2958 __ bind(&underlying_unpacked);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002959
Ben Murdoch589d6972011-11-30 16:04:58 +00002960 if (FLAG_string_slices) {
2961 Label copy_routine;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002962 // rdi: underlying subject string
2963 // rbx: instance type of underlying subject string
2964 // rdx: adjusted start index (smi)
2965 // rcx: length
Ben Murdoch589d6972011-11-30 16:04:58 +00002966 // If coming from the make_two_character_string path, the string
2967 // is too short to be sliced anyways.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002968 __ cmpp(rcx, Immediate(SlicedString::kMinLength));
Ben Murdoch589d6972011-11-30 16:04:58 +00002969 // Short slice. Copy instead of slicing.
2970 __ j(less, &copy_routine);
Ben Murdoch589d6972011-11-30 16:04:58 +00002971 // Allocate new sliced string. At this point we do not reload the instance
2972 // type including the string encoding because we simply rely on the info
2973 // provided by the original string. It does not matter if the original
2974 // string's encoding is wrong because we always have to recheck encoding of
2975 // the newly created string's parent anyways due to externalized strings.
2976 Label two_byte_slice, set_slice_header;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002977 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00002978 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2979 __ testb(rbx, Immediate(kStringEncodingMask));
2980 __ j(zero, &two_byte_slice, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002981 __ AllocateOneByteSlicedString(rax, rbx, r14, &runtime);
Ben Murdoch589d6972011-11-30 16:04:58 +00002982 __ jmp(&set_slice_header, Label::kNear);
2983 __ bind(&two_byte_slice);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002984 __ AllocateTwoByteSlicedString(rax, rbx, r14, &runtime);
Ben Murdoch589d6972011-11-30 16:04:58 +00002985 __ bind(&set_slice_header);
Ben Murdoch589d6972011-11-30 16:04:58 +00002986 __ Integer32ToSmi(rcx, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002987 __ movp(FieldOperand(rax, SlicedString::kLengthOffset), rcx);
2988 __ movp(FieldOperand(rax, SlicedString::kHashFieldOffset),
Ben Murdoch589d6972011-11-30 16:04:58 +00002989 Immediate(String::kEmptyHashField));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002990 __ movp(FieldOperand(rax, SlicedString::kParentOffset), rdi);
2991 __ movp(FieldOperand(rax, SlicedString::kOffsetOffset), rdx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002992 __ IncrementCounter(counters->sub_string_native(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002993 __ ret(3 * kPointerSize);
Ben Murdoch589d6972011-11-30 16:04:58 +00002994
2995 __ bind(&copy_routine);
Ben Murdoch589d6972011-11-30 16:04:58 +00002996 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002997
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002998 // rdi: underlying subject string
2999 // rbx: instance type of underlying subject string
3000 // rdx: adjusted start index (smi)
3001 // rcx: length
3002 // The subject string can only be external or sequential string of either
3003 // encoding at this point.
3004 Label two_byte_sequential, sequential_string;
3005 STATIC_ASSERT(kExternalStringTag != 0);
3006 STATIC_ASSERT(kSeqStringTag == 0);
3007 __ testb(rbx, Immediate(kExternalStringTag));
3008 __ j(zero, &sequential_string);
3009
3010 // Handle external string.
3011 // Rule out short external strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003012 STATIC_ASSERT(kShortExternalStringTag != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003013 __ testb(rbx, Immediate(kShortExternalStringMask));
3014 __ j(not_zero, &runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003015 __ movp(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003016 // Move the pointer so that offset-wise, it looks like a sequential string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003017 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3018 __ subp(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003019
3020 __ bind(&sequential_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003021 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003022 __ testb(rbx, Immediate(kStringEncodingMask));
3023 __ j(zero, &two_byte_sequential);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003024
3025 // Allocate the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003026 __ AllocateOneByteString(rax, rcx, r11, r14, r15, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003027
3028 // rax: result string
3029 // rcx: result string length
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003030 { // Locate character of sub string start.
3031 SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003032 __ leap(r14, Operand(rdi, smi_as_index.reg, smi_as_index.scale,
3033 SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch85b71792012-04-11 18:30:58 +01003034 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003035 // Locate first character of result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003036 __ leap(rdi, FieldOperand(rax, SeqOneByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003037
3038 // rax: result string
3039 // rcx: result length
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003040 // r14: first character of result
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003041 // rsi: character of sub string start
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003042 StringHelper::GenerateCopyCharacters(
3043 masm, rdi, r14, rcx, String::ONE_BYTE_ENCODING);
Steve Block44f0eee2011-05-26 01:26:41 +01003044 __ IncrementCounter(counters->sub_string_native(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003045 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003046
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003047 __ bind(&two_byte_sequential);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003048 // Allocate the result.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003049 __ AllocateTwoByteString(rax, rcx, r11, r14, r15, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003050
3051 // rax: result string
3052 // rcx: result string length
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003053 { // Locate character of sub string start.
3054 SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003055 __ leap(r14, Operand(rdi, smi_as_index.reg, smi_as_index.scale,
3056 SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch85b71792012-04-11 18:30:58 +01003057 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003058 // Locate first character of result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003059 __ leap(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003060
3061 // rax: result string
3062 // rcx: result length
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003063 // rdi: first character of result
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003064 // r14: character of sub string start
3065 StringHelper::GenerateCopyCharacters(
3066 masm, rdi, r14, rcx, String::TWO_BYTE_ENCODING);
Steve Block44f0eee2011-05-26 01:26:41 +01003067 __ IncrementCounter(counters->sub_string_native(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003068 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003069
3070 // Just jump to runtime to create the sub string.
3071 __ bind(&runtime);
3072 __ TailCallRuntime(Runtime::kSubString, 3, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003073
3074 __ bind(&single_char);
3075 // rax: string
3076 // rbx: instance type
3077 // rcx: sub string length (smi)
3078 // rdx: from index (smi)
3079 StringCharAtGenerator generator(
3080 rax, rdx, rcx, rax, &runtime, &runtime, &runtime, STRING_INDEX_IS_NUMBER);
3081 generator.GenerateFast(masm);
3082 __ ret(SUB_STRING_ARGUMENT_COUNT * kPointerSize);
3083 generator.SkipSlow(masm, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003084}
3085
3086
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003087void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
3088 Register left,
3089 Register right,
3090 Register scratch1,
3091 Register scratch2) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003092 Register length = scratch1;
3093
3094 // Compare lengths.
3095 Label check_zero_length;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003096 __ movp(length, FieldOperand(left, String::kLengthOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003097 __ SmiCompare(length, FieldOperand(right, String::kLengthOffset));
3098 __ j(equal, &check_zero_length, Label::kNear);
3099 __ Move(rax, Smi::FromInt(NOT_EQUAL));
3100 __ ret(0);
3101
3102 // Check if the length is zero.
3103 Label compare_chars;
3104 __ bind(&check_zero_length);
3105 STATIC_ASSERT(kSmiTag == 0);
3106 __ SmiTest(length);
3107 __ j(not_zero, &compare_chars, Label::kNear);
3108 __ Move(rax, Smi::FromInt(EQUAL));
3109 __ ret(0);
3110
3111 // Compare characters.
3112 __ bind(&compare_chars);
3113 Label strings_not_equal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003114 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
3115 &strings_not_equal, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00003116
3117 // Characters are equal.
3118 __ Move(rax, Smi::FromInt(EQUAL));
3119 __ ret(0);
3120
3121 // Characters are not equal.
3122 __ bind(&strings_not_equal);
3123 __ Move(rax, Smi::FromInt(NOT_EQUAL));
3124 __ ret(0);
3125}
3126
3127
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003128void StringHelper::GenerateCompareFlatOneByteStrings(
3129 MacroAssembler* masm, Register left, Register right, Register scratch1,
3130 Register scratch2, Register scratch3, Register scratch4) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003131 // Ensure that you can always subtract a string length from a non-negative
3132 // number (e.g. another length).
3133 STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
3134
3135 // Find minimum length and length difference.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003136 __ movp(scratch1, FieldOperand(left, String::kLengthOffset));
3137 __ movp(scratch4, scratch1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003138 __ SmiSub(scratch4,
3139 scratch4,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003140 FieldOperand(right, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003141 // Register scratch4 now holds left.length - right.length.
3142 const Register length_difference = scratch4;
Ben Murdoch257744e2011-11-30 15:57:28 +00003143 Label left_shorter;
3144 __ j(less, &left_shorter, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003145 // The right string isn't longer that the left one.
3146 // Get the right string's length by subtracting the (non-negative) difference
3147 // from the left string's length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003148 __ SmiSub(scratch1, scratch1, length_difference);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003149 __ bind(&left_shorter);
3150 // Register scratch1 now holds Min(left.length, right.length).
3151 const Register min_length = scratch1;
3152
Ben Murdoch257744e2011-11-30 15:57:28 +00003153 Label compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003154 // If min-length is zero, go directly to comparing lengths.
3155 __ SmiTest(min_length);
Ben Murdoch257744e2011-11-30 15:57:28 +00003156 __ j(zero, &compare_lengths, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003157
Ben Murdoch257744e2011-11-30 15:57:28 +00003158 // Compare loop.
3159 Label result_not_equal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003160 GenerateOneByteCharsCompareLoop(
3161 masm, left, right, min_length, scratch2, &result_not_equal,
3162 // In debug-code mode, SmiTest below might push
3163 // the target label outside the near range.
3164 Label::kFar);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003165
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003166 // Completed loop without finding different characters.
3167 // Compare lengths (precomputed).
3168 __ bind(&compare_lengths);
3169 __ SmiTest(length_difference);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003170 Label length_not_equal;
3171 __ j(not_zero, &length_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003172
3173 // Result is EQUAL.
3174 __ Move(rax, Smi::FromInt(EQUAL));
3175 __ ret(0);
3176
Ben Murdoch257744e2011-11-30 15:57:28 +00003177 Label result_greater;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003178 Label result_less;
3179 __ bind(&length_not_equal);
3180 __ j(greater, &result_greater, Label::kNear);
3181 __ jmp(&result_less, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003182 __ bind(&result_not_equal);
3183 // Unequal comparison of left to right, either character or length.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003184 __ j(above, &result_greater, Label::kNear);
3185 __ bind(&result_less);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003186
3187 // Result is LESS.
3188 __ Move(rax, Smi::FromInt(LESS));
3189 __ ret(0);
3190
3191 // Result is GREATER.
3192 __ bind(&result_greater);
3193 __ Move(rax, Smi::FromInt(GREATER));
3194 __ ret(0);
3195}
3196
3197
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003198void StringHelper::GenerateOneByteCharsCompareLoop(
3199 MacroAssembler* masm, Register left, Register right, Register length,
3200 Register scratch, Label* chars_not_equal, Label::Distance near_jump) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003201 // Change index to run from -length to -1 by adding length to string
3202 // start. This means that loop ends when index reaches zero, which
3203 // doesn't need an additional compare.
3204 __ SmiToInteger32(length, length);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003205 __ leap(left,
3206 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
3207 __ leap(right,
3208 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
3209 __ negq(length);
Ben Murdoch257744e2011-11-30 15:57:28 +00003210 Register index = length; // index = -length;
3211
3212 // Compare loop.
3213 Label loop;
3214 __ bind(&loop);
3215 __ movb(scratch, Operand(left, index, times_1, 0));
3216 __ cmpb(scratch, Operand(right, index, times_1, 0));
3217 __ j(not_equal, chars_not_equal, near_jump);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003218 __ incq(index);
Ben Murdoch257744e2011-11-30 15:57:28 +00003219 __ j(not_zero, &loop);
3220}
3221
3222
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003223void StringCompareStub::Generate(MacroAssembler* masm) {
3224 Label runtime;
3225
3226 // Stack frame on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003227 // rsp[0] : return address
3228 // rsp[8] : right string
3229 // rsp[16] : left string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003230
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003231 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER);
3232 __ movp(rdx, args.GetArgumentOperand(0)); // left
3233 __ movp(rax, args.GetArgumentOperand(1)); // right
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003234
3235 // Check for identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00003236 Label not_same;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003237 __ cmpp(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00003238 __ j(not_equal, &not_same, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003239 __ Move(rax, Smi::FromInt(EQUAL));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003240 Counters* counters = isolate()->counters();
Steve Block44f0eee2011-05-26 01:26:41 +01003241 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003242 __ ret(2 * kPointerSize);
3243
3244 __ bind(&not_same);
3245
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003246 // Check that both are sequential one-byte strings.
3247 __ JumpIfNotBothSequentialOneByteStrings(rdx, rax, rcx, rbx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003248
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003249 // Inline comparison of one-byte strings.
Steve Block44f0eee2011-05-26 01:26:41 +01003250 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003251 // Drop arguments from the stack
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003252 __ PopReturnAddressTo(rcx);
3253 __ addp(rsp, Immediate(2 * kPointerSize));
3254 __ PushReturnAddressFrom(rcx);
3255 StringHelper::GenerateCompareFlatOneByteStrings(masm, rdx, rax, rcx, rbx, rdi,
3256 r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003257
3258 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
3259 // tagged as a small integer.
3260 __ bind(&runtime);
3261 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
3262}
3263
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003264
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003265void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3266 // ----------- S t a t e -------------
3267 // -- rdx : left
3268 // -- rax : right
3269 // -- rsp[0] : return address
3270 // -----------------------------------
3271
3272 // Load rcx with the allocation site. We stick an undefined dummy value here
3273 // and replace it with the real allocation site later when we instantiate this
3274 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3275 __ Move(rcx, handle(isolate()->heap()->undefined_value()));
3276
3277 // Make sure that we actually patched the allocation site.
3278 if (FLAG_debug_code) {
3279 __ testb(rcx, Immediate(kSmiTagMask));
3280 __ Assert(not_equal, kExpectedAllocationSite);
3281 __ Cmp(FieldOperand(rcx, HeapObject::kMapOffset),
3282 isolate()->factory()->allocation_site_map());
3283 __ Assert(equal, kExpectedAllocationSite);
3284 }
3285
3286 // Tail call into the stub that handles binary operations with allocation
3287 // sites.
3288 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3289 __ TailCallStub(&stub);
3290}
3291
3292
3293void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3294 DCHECK(state() == CompareICState::SMI);
Ben Murdoch257744e2011-11-30 15:57:28 +00003295 Label miss;
3296 __ JumpIfNotBothSmi(rdx, rax, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01003297
3298 if (GetCondition() == equal) {
3299 // For equality we do not care about the sign of the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003300 __ subp(rax, rdx);
Steve Block1e0659c2011-05-24 12:43:12 +01003301 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00003302 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003303 __ subp(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00003304 __ j(no_overflow, &done, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01003305 // Correct sign of result in case of overflow.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003306 __ notp(rdx);
Steve Block1e0659c2011-05-24 12:43:12 +01003307 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003308 __ movp(rax, rdx);
Steve Block1e0659c2011-05-24 12:43:12 +01003309 }
3310 __ ret(0);
3311
3312 __ bind(&miss);
3313 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003314}
3315
3316
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003317void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3318 DCHECK(state() == CompareICState::NUMBER);
Steve Block1e0659c2011-05-24 12:43:12 +01003319
Ben Murdoch257744e2011-11-30 15:57:28 +00003320 Label generic_stub;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003321 Label unordered, maybe_undefined1, maybe_undefined2;
Ben Murdoch257744e2011-11-30 15:57:28 +00003322 Label miss;
Steve Block1e0659c2011-05-24 12:43:12 +01003323
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003324 if (left() == CompareICState::SMI) {
3325 __ JumpIfNotSmi(rdx, &miss);
3326 }
3327 if (right() == CompareICState::SMI) {
3328 __ JumpIfNotSmi(rax, &miss);
3329 }
3330
3331 // Load left and right operand.
3332 Label done, left, left_smi, right_smi;
3333 __ JumpIfSmi(rax, &right_smi, Label::kNear);
3334 __ CompareMap(rax, isolate()->factory()->heap_number_map());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003335 __ j(not_equal, &maybe_undefined1, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01003336 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003337 __ jmp(&left, Label::kNear);
3338 __ bind(&right_smi);
3339 __ SmiToInteger32(rcx, rax); // Can't clobber rax yet.
3340 __ Cvtlsi2sd(xmm1, rcx);
Steve Block1e0659c2011-05-24 12:43:12 +01003341
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003342 __ bind(&left);
3343 __ JumpIfSmi(rdx, &left_smi, Label::kNear);
3344 __ CompareMap(rdx, isolate()->factory()->heap_number_map());
3345 __ j(not_equal, &maybe_undefined2, Label::kNear);
3346 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
3347 __ jmp(&done);
3348 __ bind(&left_smi);
3349 __ SmiToInteger32(rcx, rdx); // Can't clobber rdx yet.
3350 __ Cvtlsi2sd(xmm0, rcx);
3351
3352 __ bind(&done);
Steve Block1e0659c2011-05-24 12:43:12 +01003353 // Compare operands
3354 __ ucomisd(xmm0, xmm1);
3355
3356 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch257744e2011-11-30 15:57:28 +00003357 __ j(parity_even, &unordered, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01003358
3359 // Return a result of -1, 0, or 1, based on EFLAGS.
3360 // Performing mov, because xor would destroy the flag register.
3361 __ movl(rax, Immediate(0));
3362 __ movl(rcx, Immediate(0));
3363 __ setcc(above, rax); // Add one to zero if carry clear and not equal.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003364 __ sbbp(rax, rcx); // Subtract one if below (aka. carry set).
Steve Block1e0659c2011-05-24 12:43:12 +01003365 __ ret(0);
3366
3367 __ bind(&unordered);
Steve Block1e0659c2011-05-24 12:43:12 +01003368 __ bind(&generic_stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003369 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
3370 CompareICState::GENERIC, CompareICState::GENERIC);
Steve Block1e0659c2011-05-24 12:43:12 +01003371 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3372
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003373 __ bind(&maybe_undefined1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003374 if (Token::IsOrderedRelationalCompareOp(op())) {
3375 __ Cmp(rax, isolate()->factory()->undefined_value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003376 __ j(not_equal, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003377 __ JumpIfSmi(rdx, &unordered);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003378 __ CmpObjectType(rdx, HEAP_NUMBER_TYPE, rcx);
3379 __ j(not_equal, &maybe_undefined2, Label::kNear);
3380 __ jmp(&unordered);
3381 }
3382
3383 __ bind(&maybe_undefined2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003384 if (Token::IsOrderedRelationalCompareOp(op())) {
3385 __ Cmp(rdx, isolate()->factory()->undefined_value());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003386 __ j(equal, &unordered);
3387 }
3388
Steve Block1e0659c2011-05-24 12:43:12 +01003389 __ bind(&miss);
3390 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003391}
3392
3393
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003394void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3395 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3396 DCHECK(GetCondition() == equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00003397
3398 // Registers containing left and right operands respectively.
3399 Register left = rdx;
3400 Register right = rax;
3401 Register tmp1 = rcx;
3402 Register tmp2 = rbx;
3403
3404 // Check that both operands are heap objects.
3405 Label miss;
3406 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3407 __ j(cond, &miss, Label::kNear);
3408
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003409 // Check that both operands are internalized strings.
3410 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3411 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3412 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3413 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3414 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3415 __ orp(tmp1, tmp2);
3416 __ testb(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
3417 __ j(not_zero, &miss, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00003418
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003419 // Internalized strings are compared by identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00003420 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003421 __ cmpp(left, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00003422 // Make sure rax is non-zero. At this point input operands are
3423 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003424 DCHECK(right.is(rax));
Ben Murdoch257744e2011-11-30 15:57:28 +00003425 __ j(not_equal, &done, Label::kNear);
3426 STATIC_ASSERT(EQUAL == 0);
3427 STATIC_ASSERT(kSmiTag == 0);
3428 __ Move(rax, Smi::FromInt(EQUAL));
3429 __ bind(&done);
3430 __ ret(0);
3431
3432 __ bind(&miss);
3433 GenerateMiss(masm);
3434}
3435
3436
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003437void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3438 DCHECK(state() == CompareICState::UNIQUE_NAME);
3439 DCHECK(GetCondition() == equal);
3440
3441 // Registers containing left and right operands respectively.
3442 Register left = rdx;
3443 Register right = rax;
3444 Register tmp1 = rcx;
3445 Register tmp2 = rbx;
3446
3447 // Check that both operands are heap objects.
3448 Label miss;
3449 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3450 __ j(cond, &miss, Label::kNear);
3451
3452 // Check that both operands are unique names. This leaves the instance
3453 // types loaded in tmp1 and tmp2.
3454 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3455 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3456 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3457 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3458
3459 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
3460 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
3461
3462 // Unique names are compared by identity.
3463 Label done;
3464 __ cmpp(left, right);
3465 // Make sure rax is non-zero. At this point input operands are
3466 // guaranteed to be non-zero.
3467 DCHECK(right.is(rax));
3468 __ j(not_equal, &done, Label::kNear);
3469 STATIC_ASSERT(EQUAL == 0);
3470 STATIC_ASSERT(kSmiTag == 0);
3471 __ Move(rax, Smi::FromInt(EQUAL));
3472 __ bind(&done);
3473 __ ret(0);
3474
3475 __ bind(&miss);
3476 GenerateMiss(masm);
3477}
3478
3479
3480void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3481 DCHECK(state() == CompareICState::STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00003482 Label miss;
3483
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003484 bool equality = Token::IsEqualityOp(op());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003485
Ben Murdoch257744e2011-11-30 15:57:28 +00003486 // Registers containing left and right operands respectively.
3487 Register left = rdx;
3488 Register right = rax;
3489 Register tmp1 = rcx;
3490 Register tmp2 = rbx;
3491 Register tmp3 = rdi;
3492
3493 // Check that both operands are heap objects.
3494 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
3495 __ j(cond, &miss);
3496
3497 // Check that both operands are strings. This leaves the instance
3498 // types loaded in tmp1 and tmp2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003499 __ movp(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3500 __ movp(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3501 __ movzxbp(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3502 __ movzxbp(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3503 __ movp(tmp3, tmp1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003504 STATIC_ASSERT(kNotStringTag != 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003505 __ orp(tmp3, tmp2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003506 __ testb(tmp3, Immediate(kIsNotStringMask));
3507 __ j(not_zero, &miss);
3508
3509 // Fast check for identical strings.
3510 Label not_same;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003511 __ cmpp(left, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00003512 __ j(not_equal, &not_same, Label::kNear);
3513 STATIC_ASSERT(EQUAL == 0);
3514 STATIC_ASSERT(kSmiTag == 0);
3515 __ Move(rax, Smi::FromInt(EQUAL));
3516 __ ret(0);
3517
3518 // Handle not identical strings.
3519 __ bind(&not_same);
3520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003521 // Check that both strings are internalized strings. If they are, we're done
3522 // because we already know they are not identical. We also know they are both
3523 // strings.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003524 if (equality) {
3525 Label do_compare;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003526 STATIC_ASSERT(kInternalizedTag == 0);
3527 __ orp(tmp1, tmp2);
3528 __ testb(tmp1, Immediate(kIsNotInternalizedMask));
3529 __ j(not_zero, &do_compare, Label::kNear);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003530 // Make sure rax is non-zero. At this point input operands are
3531 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003532 DCHECK(right.is(rax));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003533 __ ret(0);
3534 __ bind(&do_compare);
3535 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003536
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003537 // Check that both strings are sequential one-byte.
Ben Murdoch257744e2011-11-30 15:57:28 +00003538 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003539 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003540
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003541 // Compare flat one-byte strings. Returns when done.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003542 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003543 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
3544 tmp2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003545 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003546 StringHelper::GenerateCompareFlatOneByteStrings(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003547 masm, left, right, tmp1, tmp2, tmp3, kScratchRegister);
3548 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003549
3550 // Handle more complex cases in runtime.
3551 __ bind(&runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003552 __ PopReturnAddressTo(tmp1);
3553 __ Push(left);
3554 __ Push(right);
3555 __ PushReturnAddressFrom(tmp1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003556 if (equality) {
3557 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3558 } else {
3559 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
3560 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003561
3562 __ bind(&miss);
3563 GenerateMiss(masm);
3564}
3565
3566
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003567void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3568 DCHECK(state() == CompareICState::OBJECT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003569 Label miss;
Steve Block1e0659c2011-05-24 12:43:12 +01003570 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00003571 __ j(either_smi, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01003572
3573 __ CmpObjectType(rax, JS_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003574 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01003575 __ CmpObjectType(rdx, JS_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003576 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01003577
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003578 DCHECK(GetCondition() == equal);
3579 __ subp(rax, rdx);
Steve Block1e0659c2011-05-24 12:43:12 +01003580 __ ret(0);
3581
3582 __ bind(&miss);
3583 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003584}
3585
3586
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003587void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003588 Label miss;
3589 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
3590 __ j(either_smi, &miss, Label::kNear);
3591
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003592 __ movp(rcx, FieldOperand(rax, HeapObject::kMapOffset));
3593 __ movp(rbx, FieldOperand(rdx, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003594 __ Cmp(rcx, known_map_);
3595 __ j(not_equal, &miss, Label::kNear);
3596 __ Cmp(rbx, known_map_);
3597 __ j(not_equal, &miss, Label::kNear);
3598
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003599 __ subp(rax, rdx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003600 __ ret(0);
3601
3602 __ bind(&miss);
3603 GenerateMiss(masm);
3604}
3605
3606
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003607void CompareICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003608 {
3609 // Call the runtime system in a fresh internal frame.
3610 ExternalReference miss =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003611 ExternalReference(IC_Utility(IC::kCompareIC_Miss), isolate());
Ben Murdochc7cc0282012-03-05 14:35:55 +00003612
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003613 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003614 __ Push(rdx);
3615 __ Push(rax);
3616 __ Push(rdx);
3617 __ Push(rax);
3618 __ Push(Smi::FromInt(op()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003619 __ CallExternalReference(miss, 3);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003620
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003621 // Compute the entry point of the rewritten stub.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003622 __ leap(rdi, FieldOperand(rax, Code::kHeaderSize));
3623 __ Pop(rax);
3624 __ Pop(rdx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003625 }
Steve Block1e0659c2011-05-24 12:43:12 +01003626
Steve Block1e0659c2011-05-24 12:43:12 +01003627 // Do a tail call to the rewritten stub.
3628 __ jmp(rdi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003629}
3630
Steve Block1e0659c2011-05-24 12:43:12 +01003631
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003632void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3633 Label* miss,
3634 Label* done,
3635 Register properties,
3636 Handle<Name> name,
3637 Register r0) {
3638 DCHECK(name->IsUniqueName());
Ben Murdoch257744e2011-11-30 15:57:28 +00003639 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3640 // not equal to the name and kProbes-th slot is not used (its name is the
3641 // undefined value), it guarantees the hash table doesn't contain the
3642 // property. It's true even if some slots represent deleted properties
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003643 // (their names are the hole value).
Ben Murdoch257744e2011-11-30 15:57:28 +00003644 for (int i = 0; i < kInlinedProbes; i++) {
3645 // r0 points to properties hash.
3646 // Compute the masked index: (hash + i + i * i) & mask.
3647 Register index = r0;
3648 // Capacity is smi 2^n.
3649 __ SmiToInteger32(index, FieldOperand(properties, kCapacityOffset));
3650 __ decl(index);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003651 __ andp(index,
3652 Immediate(name->Hash() + NameDictionary::GetProbeOffset(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003653
3654 // Scale the index by multiplying by the entry size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003655 DCHECK(NameDictionary::kEntrySize == 3);
3656 __ leap(index, Operand(index, index, times_2, 0)); // index *= 3.
Ben Murdoch257744e2011-11-30 15:57:28 +00003657
3658 Register entity_name = r0;
3659 // Having undefined at this place means the name is not contained.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003660 DCHECK_EQ(kSmiTagSize, 1);
3661 __ movp(entity_name, Operand(properties,
Ben Murdoch257744e2011-11-30 15:57:28 +00003662 index,
3663 times_pointer_size,
3664 kElementsStartOffset - kHeapObjectTag));
3665 __ Cmp(entity_name, masm->isolate()->factory()->undefined_value());
3666 __ j(equal, done);
3667
3668 // Stop if found the property.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003669 __ Cmp(entity_name, Handle<Name>(name));
Ben Murdoch257744e2011-11-30 15:57:28 +00003670 __ j(equal, miss);
3671
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003672 Label good;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003673 // Check for the hole and skip.
3674 __ CompareRoot(entity_name, Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003675 __ j(equal, &good, Label::kNear);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003676
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003677 // Check if the entry name is not a unique name.
3678 __ movp(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3679 __ JumpIfNotUniqueNameInstanceType(
3680 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3681 __ bind(&good);
Ben Murdoch257744e2011-11-30 15:57:28 +00003682 }
3683
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003684 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3685 NEGATIVE_LOOKUP);
Ben Murdoch257744e2011-11-30 15:57:28 +00003686 __ Push(Handle<Object>(name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003687 __ Push(Immediate(name->Hash()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003688 __ CallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003689 __ testp(r0, r0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003690 __ j(not_zero, miss);
3691 __ jmp(done);
Ben Murdoch257744e2011-11-30 15:57:28 +00003692}
3693
3694
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003695// Probe the name dictionary in the |elements| register. Jump to the
Ben Murdoch257744e2011-11-30 15:57:28 +00003696// |done| label if a property with the given name is found leaving the
3697// index into the dictionary in |r1|. Jump to the |miss| label
3698// otherwise.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003699void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3700 Label* miss,
3701 Label* done,
3702 Register elements,
3703 Register name,
3704 Register r0,
3705 Register r1) {
3706 DCHECK(!elements.is(r0));
3707 DCHECK(!elements.is(r1));
3708 DCHECK(!name.is(r0));
3709 DCHECK(!name.is(r1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003710
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003711 __ AssertName(name);
Ben Murdoch257744e2011-11-30 15:57:28 +00003712
3713 __ SmiToInteger32(r0, FieldOperand(elements, kCapacityOffset));
3714 __ decl(r0);
3715
3716 for (int i = 0; i < kInlinedProbes; i++) {
3717 // Compute the masked index: (hash + i + i * i) & mask.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003718 __ movl(r1, FieldOperand(name, Name::kHashFieldOffset));
3719 __ shrl(r1, Immediate(Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003720 if (i > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003721 __ addl(r1, Immediate(NameDictionary::GetProbeOffset(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003722 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003723 __ andp(r1, r0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003724
3725 // Scale the index by multiplying by the entry size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003726 DCHECK(NameDictionary::kEntrySize == 3);
3727 __ leap(r1, Operand(r1, r1, times_2, 0)); // r1 = r1 * 3
Ben Murdoch257744e2011-11-30 15:57:28 +00003728
3729 // Check if the key is identical to the name.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003730 __ cmpp(name, Operand(elements, r1, times_pointer_size,
Ben Murdoch257744e2011-11-30 15:57:28 +00003731 kElementsStartOffset - kHeapObjectTag));
3732 __ j(equal, done);
3733 }
3734
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003735 NameDictionaryLookupStub stub(masm->isolate(), elements, r0, r1,
3736 POSITIVE_LOOKUP);
3737 __ Push(name);
3738 __ movl(r0, FieldOperand(name, Name::kHashFieldOffset));
3739 __ shrl(r0, Immediate(Name::kHashShift));
3740 __ Push(r0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003741 __ CallStub(&stub);
3742
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003743 __ testp(r0, r0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003744 __ j(zero, miss);
3745 __ jmp(done);
3746}
3747
3748
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003749void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003750 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3751 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00003752 // Stack frame on entry:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003753 // rsp[0 * kPointerSize] : return address.
3754 // rsp[1 * kPointerSize] : key's hash.
3755 // rsp[2 * kPointerSize] : key.
Ben Murdoch257744e2011-11-30 15:57:28 +00003756 // Registers:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003757 // dictionary_: NameDictionary to probe.
Ben Murdoch257744e2011-11-30 15:57:28 +00003758 // result_: used as scratch.
3759 // index_: will hold an index of entry if lookup is successful.
3760 // might alias with result_.
3761 // Returns:
3762 // result_ is zero if lookup failed, non zero otherwise.
3763
3764 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3765
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003766 Register scratch = result();
Ben Murdoch257744e2011-11-30 15:57:28 +00003767
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003768 __ SmiToInteger32(scratch, FieldOperand(dictionary(), kCapacityOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003769 __ decl(scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003770 __ Push(scratch);
Ben Murdoch257744e2011-11-30 15:57:28 +00003771
3772 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3773 // not equal to the name and kProbes-th slot is not used (its name is the
3774 // undefined value), it guarantees the hash table doesn't contain the
3775 // property. It's true even if some slots represent deleted properties
3776 // (their names are the null value).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003777 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER,
3778 kPointerSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00003779 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3780 // Compute the masked index: (hash + i + i * i) & mask.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003781 __ movp(scratch, args.GetArgumentOperand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00003782 if (i > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003783 __ addl(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003784 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003785 __ andp(scratch, Operand(rsp, 0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003786
3787 // Scale the index by multiplying by the entry size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003788 DCHECK(NameDictionary::kEntrySize == 3);
3789 __ leap(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
Ben Murdoch257744e2011-11-30 15:57:28 +00003790
3791 // Having undefined at this place means the name is not contained.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003792 __ movp(scratch, Operand(dictionary(), index(), times_pointer_size,
Ben Murdoch257744e2011-11-30 15:57:28 +00003793 kElementsStartOffset - kHeapObjectTag));
3794
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003795 __ Cmp(scratch, isolate()->factory()->undefined_value());
Ben Murdoch257744e2011-11-30 15:57:28 +00003796 __ j(equal, &not_in_dictionary);
3797
3798 // Stop if found the property.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003799 __ cmpp(scratch, args.GetArgumentOperand(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003800 __ j(equal, &in_dictionary);
3801
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003802 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3803 // If we hit a key that is not a unique name during negative
3804 // lookup we have to bailout as this key might be equal to the
Ben Murdoch257744e2011-11-30 15:57:28 +00003805 // key we are looking for.
3806
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003807 // Check if the entry name is not a unique name.
3808 __ movp(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
3809 __ JumpIfNotUniqueNameInstanceType(
3810 FieldOperand(scratch, Map::kInstanceTypeOffset),
3811 &maybe_in_dictionary);
Ben Murdoch257744e2011-11-30 15:57:28 +00003812 }
3813 }
3814
3815 __ bind(&maybe_in_dictionary);
3816 // If we are doing negative lookup then probing failure should be
3817 // treated as a lookup success. For positive lookup probing failure
3818 // should be treated as lookup failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003819 if (mode() == POSITIVE_LOOKUP) {
3820 __ movp(scratch, Immediate(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003821 __ Drop(1);
3822 __ ret(2 * kPointerSize);
3823 }
3824
3825 __ bind(&in_dictionary);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003826 __ movp(scratch, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00003827 __ Drop(1);
3828 __ ret(2 * kPointerSize);
3829
3830 __ bind(&not_in_dictionary);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003831 __ movp(scratch, Immediate(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003832 __ Drop(1);
3833 __ ret(2 * kPointerSize);
3834}
3835
3836
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003837void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3838 Isolate* isolate) {
3839 StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
3840 stub1.GetCode();
3841 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3842 stub2.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003843}
3844
3845
3846// Takes the input in 3 registers: address_ value_ and object_. A pointer to
3847// the value has just been written into the object, now this stub makes sure
3848// we keep the GC informed. The word in the object where the value has been
3849// written is in the address register.
3850void RecordWriteStub::Generate(MacroAssembler* masm) {
3851 Label skip_to_incremental_noncompacting;
3852 Label skip_to_incremental_compacting;
3853
3854 // The first two instructions are generated with labels so as to get the
3855 // offset fixed up correctly by the bind(Label*) call. We patch it back and
3856 // forth between a compare instructions (a nop in this position) and the
3857 // real branch when we start and stop incremental heap marking.
3858 // See RecordWriteStub::Patch for details.
3859 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
3860 __ jmp(&skip_to_incremental_compacting, Label::kFar);
3861
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003862 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3863 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003864 MacroAssembler::kReturnAtEnd);
3865 } else {
3866 __ ret(0);
3867 }
3868
3869 __ bind(&skip_to_incremental_noncompacting);
3870 GenerateIncremental(masm, INCREMENTAL);
3871
3872 __ bind(&skip_to_incremental_compacting);
3873 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3874
3875 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3876 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
3877 masm->set_byte_at(0, kTwoByteNopInstruction);
3878 masm->set_byte_at(2, kFiveByteNopInstruction);
3879}
3880
3881
3882void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3883 regs_.Save(masm);
3884
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003885 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003886 Label dont_need_remembered_set;
3887
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003888 __ movp(regs_.scratch0(), Operand(regs_.address(), 0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003889 __ JumpIfNotInNewSpace(regs_.scratch0(),
3890 regs_.scratch0(),
3891 &dont_need_remembered_set);
3892
3893 __ CheckPageFlag(regs_.object(),
3894 regs_.scratch0(),
3895 1 << MemoryChunk::SCAN_ON_SCAVENGE,
3896 not_zero,
3897 &dont_need_remembered_set);
3898
3899 // First notify the incremental marker if necessary, then update the
3900 // remembered set.
3901 CheckNeedsToInformIncrementalMarker(
3902 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003903 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003904 regs_.Restore(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003905 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003906 MacroAssembler::kReturnAtEnd);
3907
3908 __ bind(&dont_need_remembered_set);
3909 }
3910
3911 CheckNeedsToInformIncrementalMarker(
3912 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003913 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003914 regs_.Restore(masm);
3915 __ ret(0);
3916}
3917
3918
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003919void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3920 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003921 Register address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003922 arg_reg_1.is(regs_.address()) ? kScratchRegister : regs_.address();
3923 DCHECK(!address.is(regs_.object()));
3924 DCHECK(!address.is(arg_reg_1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003925 __ Move(address, regs_.address());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003926 __ Move(arg_reg_1, regs_.object());
3927 // TODO(gc) Can we just set address arg2 in the beginning?
3928 __ Move(arg_reg_2, address);
3929 __ LoadAddress(arg_reg_3,
3930 ExternalReference::isolate_address(isolate()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003931 int argument_count = 3;
3932
3933 AllowExternalCallThatCantCauseGC scope(masm);
3934 __ PrepareCallCFunction(argument_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003935 __ CallCFunction(
3936 ExternalReference::incremental_marking_record_write_function(isolate()),
3937 argument_count);
3938 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003939}
3940
3941
3942void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3943 MacroAssembler* masm,
3944 OnNoNeedToInformIncrementalMarker on_no_need,
3945 Mode mode) {
3946 Label on_black;
3947 Label need_incremental;
3948 Label need_incremental_pop_object;
3949
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003950 __ movp(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
3951 __ andp(regs_.scratch0(), regs_.object());
3952 __ movp(regs_.scratch1(),
3953 Operand(regs_.scratch0(),
3954 MemoryChunk::kWriteBarrierCounterOffset));
3955 __ subp(regs_.scratch1(), Immediate(1));
3956 __ movp(Operand(regs_.scratch0(),
3957 MemoryChunk::kWriteBarrierCounterOffset),
3958 regs_.scratch1());
3959 __ j(negative, &need_incremental);
3960
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003961 // Let's look at the color of the object: If it is not black we don't have
3962 // to inform the incremental marker.
3963 __ JumpIfBlack(regs_.object(),
3964 regs_.scratch0(),
3965 regs_.scratch1(),
3966 &on_black,
3967 Label::kNear);
3968
3969 regs_.Restore(masm);
3970 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003971 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003972 MacroAssembler::kReturnAtEnd);
3973 } else {
3974 __ ret(0);
3975 }
3976
3977 __ bind(&on_black);
3978
3979 // Get the value from the slot.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003980 __ movp(regs_.scratch0(), Operand(regs_.address(), 0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003981
3982 if (mode == INCREMENTAL_COMPACTION) {
3983 Label ensure_not_white;
3984
3985 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3986 regs_.scratch1(), // Scratch.
3987 MemoryChunk::kEvacuationCandidateMask,
3988 zero,
3989 &ensure_not_white,
3990 Label::kNear);
3991
3992 __ CheckPageFlag(regs_.object(),
3993 regs_.scratch1(), // Scratch.
3994 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3995 zero,
3996 &need_incremental);
3997
3998 __ bind(&ensure_not_white);
3999 }
4000
4001 // We need an extra register for this, so we push the object register
4002 // temporarily.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004003 __ Push(regs_.object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004004 __ EnsureNotWhite(regs_.scratch0(), // The value.
4005 regs_.scratch1(), // Scratch.
4006 regs_.object(), // Scratch.
4007 &need_incremental_pop_object,
4008 Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004009 __ Pop(regs_.object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004010
4011 regs_.Restore(masm);
4012 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004013 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004014 MacroAssembler::kReturnAtEnd);
4015 } else {
4016 __ ret(0);
4017 }
4018
4019 __ bind(&need_incremental_pop_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004020 __ Pop(regs_.object());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004021
4022 __ bind(&need_incremental);
4023
4024 // Fall through when we need to inform the incremental marker.
4025}
4026
4027
4028void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4029 // ----------- S t a t e -------------
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004030 // -- rax : element value to store
4031 // -- rcx : element index as smi
4032 // -- rsp[0] : return address
4033 // -- rsp[8] : array literal index in function
4034 // -- rsp[16] : array literal
4035 // clobbers rbx, rdx, rdi
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004036 // -----------------------------------
4037
4038 Label element_done;
4039 Label double_elements;
4040 Label smi_element;
4041 Label slow_elements;
4042 Label fast_elements;
4043
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004044 // Get array literal index, array literal and its map.
4045 StackArgumentsAccessor args(rsp, 2, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4046 __ movp(rdx, args.GetArgumentOperand(1));
4047 __ movp(rbx, args.GetArgumentOperand(0));
4048 __ movp(rdi, FieldOperand(rbx, JSObject::kMapOffset));
4049
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004050 __ CheckFastElements(rdi, &double_elements);
4051
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004052 // FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004053 __ JumpIfSmi(rax, &smi_element);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004054 __ CheckFastSmiElements(rdi, &fast_elements);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004055
4056 // Store into the array literal requires a elements transition. Call into
4057 // the runtime.
4058
4059 __ bind(&slow_elements);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004060 __ PopReturnAddressTo(rdi);
4061 __ Push(rbx);
4062 __ Push(rcx);
4063 __ Push(rax);
4064 __ movp(rbx, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
4065 __ Push(FieldOperand(rbx, JSFunction::kLiteralsOffset));
4066 __ Push(rdx);
4067 __ PushReturnAddressFrom(rdi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004068 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4069
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004070 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004071 __ bind(&fast_elements);
4072 __ SmiToInteger32(kScratchRegister, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004073 __ movp(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
4074 __ leap(rcx, FieldOperand(rbx, kScratchRegister, times_pointer_size,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004075 FixedArrayBase::kHeaderSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004076 __ movp(Operand(rcx, 0), rax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004077 // Update the write barrier for the array store.
4078 __ RecordWrite(rbx, rcx, rax,
4079 kDontSaveFPRegs,
4080 EMIT_REMEMBERED_SET,
4081 OMIT_SMI_CHECK);
4082 __ ret(0);
4083
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004084 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or
4085 // FAST_*_ELEMENTS, and value is Smi.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004086 __ bind(&smi_element);
4087 __ SmiToInteger32(kScratchRegister, rcx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004088 __ movp(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
4089 __ movp(FieldOperand(rbx, kScratchRegister, times_pointer_size,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004090 FixedArrayBase::kHeaderSize), rax);
4091 __ ret(0);
4092
4093 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
4094 __ bind(&double_elements);
4095
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004096 __ movp(r9, FieldOperand(rbx, JSObject::kElementsOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004097 __ SmiToInteger32(r11, rcx);
4098 __ StoreNumberToDoubleElements(rax,
4099 r9,
4100 r11,
4101 xmm0,
4102 &slow_elements);
4103 __ ret(0);
4104}
4105
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004106
4107void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4108 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4109 __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
4110 int parameter_count_offset =
4111 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4112 __ movp(rbx, MemOperand(rbp, parameter_count_offset));
4113 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4114 __ PopReturnAddressTo(rcx);
4115 int additional_offset =
4116 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
4117 __ leap(rsp, MemOperand(rsp, rbx, times_pointer_size, additional_offset));
4118 __ jmp(rcx); // Return to IC Miss stub, continuation still on stack.
4119}
4120
4121
4122void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4123 EmitLoadTypeFeedbackVector(masm, VectorLoadICDescriptor::VectorRegister());
4124 VectorLoadStub stub(isolate(), state());
4125 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4126}
4127
4128
4129void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4130 EmitLoadTypeFeedbackVector(masm, VectorLoadICDescriptor::VectorRegister());
4131 VectorKeyedLoadStub stub(isolate());
4132 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4133}
4134
4135
4136void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4137 if (masm->isolate()->function_entry_hook() != NULL) {
4138 ProfileEntryHookStub stub(masm->isolate());
4139 masm->CallStub(&stub);
4140 }
4141}
4142
4143
4144void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4145 // This stub can be called from essentially anywhere, so it needs to save
4146 // all volatile and callee-save registers.
4147 const size_t kNumSavedRegisters = 2;
4148 __ pushq(arg_reg_1);
4149 __ pushq(arg_reg_2);
4150
4151 // Calculate the original stack pointer and store it in the second arg.
4152 __ leap(arg_reg_2,
4153 Operand(rsp, kNumSavedRegisters * kRegisterSize + kPCOnStackSize));
4154
4155 // Calculate the function address to the first arg.
4156 __ movp(arg_reg_1, Operand(rsp, kNumSavedRegisters * kRegisterSize));
4157 __ subp(arg_reg_1, Immediate(Assembler::kShortCallInstructionLength));
4158
4159 // Save the remainder of the volatile registers.
4160 masm->PushCallerSaved(kSaveFPRegs, arg_reg_1, arg_reg_2);
4161
4162 // Call the entry hook function.
4163 __ Move(rax, FUNCTION_ADDR(isolate()->function_entry_hook()),
4164 Assembler::RelocInfoNone());
4165
4166 AllowExternalCallThatCantCauseGC scope(masm);
4167
4168 const int kArgumentCount = 2;
4169 __ PrepareCallCFunction(kArgumentCount);
4170 __ CallCFunction(rax, kArgumentCount);
4171
4172 // Restore volatile regs.
4173 masm->PopCallerSaved(kSaveFPRegs, arg_reg_1, arg_reg_2);
4174 __ popq(arg_reg_2);
4175 __ popq(arg_reg_1);
4176
4177 __ Ret();
4178}
4179
4180
4181template<class T>
4182static void CreateArrayDispatch(MacroAssembler* masm,
4183 AllocationSiteOverrideMode mode) {
4184 if (mode == DISABLE_ALLOCATION_SITES) {
4185 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
4186 __ TailCallStub(&stub);
4187 } else if (mode == DONT_OVERRIDE) {
4188 int last_index = GetSequenceIndexFromFastElementsKind(
4189 TERMINAL_FAST_ELEMENTS_KIND);
4190 for (int i = 0; i <= last_index; ++i) {
4191 Label next;
4192 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4193 __ cmpl(rdx, Immediate(kind));
4194 __ j(not_equal, &next);
4195 T stub(masm->isolate(), kind);
4196 __ TailCallStub(&stub);
4197 __ bind(&next);
4198 }
4199
4200 // If we reached this point there is a problem.
4201 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4202 } else {
4203 UNREACHABLE();
4204 }
4205}
4206
4207
4208static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4209 AllocationSiteOverrideMode mode) {
4210 // rbx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4211 // rdx - kind (if mode != DISABLE_ALLOCATION_SITES)
4212 // rax - number of arguments
4213 // rdi - constructor?
4214 // rsp[0] - return address
4215 // rsp[8] - last argument
4216 Handle<Object> undefined_sentinel(
4217 masm->isolate()->heap()->undefined_value(),
4218 masm->isolate());
4219
4220 Label normal_sequence;
4221 if (mode == DONT_OVERRIDE) {
4222 DCHECK(FAST_SMI_ELEMENTS == 0);
4223 DCHECK(FAST_HOLEY_SMI_ELEMENTS == 1);
4224 DCHECK(FAST_ELEMENTS == 2);
4225 DCHECK(FAST_HOLEY_ELEMENTS == 3);
4226 DCHECK(FAST_DOUBLE_ELEMENTS == 4);
4227 DCHECK(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4228
4229 // is the low bit set? If so, we are holey and that is good.
4230 __ testb(rdx, Immediate(1));
4231 __ j(not_zero, &normal_sequence);
4232 }
4233
4234 // look at the first argument
4235 StackArgumentsAccessor args(rsp, 1, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4236 __ movp(rcx, args.GetArgumentOperand(0));
4237 __ testp(rcx, rcx);
4238 __ j(zero, &normal_sequence);
4239
4240 if (mode == DISABLE_ALLOCATION_SITES) {
4241 ElementsKind initial = GetInitialFastElementsKind();
4242 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4243
4244 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4245 holey_initial,
4246 DISABLE_ALLOCATION_SITES);
4247 __ TailCallStub(&stub_holey);
4248
4249 __ bind(&normal_sequence);
4250 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4251 initial,
4252 DISABLE_ALLOCATION_SITES);
4253 __ TailCallStub(&stub);
4254 } else if (mode == DONT_OVERRIDE) {
4255 // We are going to create a holey array, but our kind is non-holey.
4256 // Fix kind and retry (only if we have an allocation site in the slot).
4257 __ incl(rdx);
4258
4259 if (FLAG_debug_code) {
4260 Handle<Map> allocation_site_map =
4261 masm->isolate()->factory()->allocation_site_map();
4262 __ Cmp(FieldOperand(rbx, 0), allocation_site_map);
4263 __ Assert(equal, kExpectedAllocationSite);
4264 }
4265
4266 // Save the resulting elements kind in type info. We can't just store r3
4267 // in the AllocationSite::transition_info field because elements kind is
4268 // restricted to a portion of the field...upper bits need to be left alone.
4269 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4270 __ SmiAddConstant(FieldOperand(rbx, AllocationSite::kTransitionInfoOffset),
4271 Smi::FromInt(kFastElementsKindPackedToHoley));
4272
4273 __ bind(&normal_sequence);
4274 int last_index = GetSequenceIndexFromFastElementsKind(
4275 TERMINAL_FAST_ELEMENTS_KIND);
4276 for (int i = 0; i <= last_index; ++i) {
4277 Label next;
4278 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4279 __ cmpl(rdx, Immediate(kind));
4280 __ j(not_equal, &next);
4281 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4282 __ TailCallStub(&stub);
4283 __ bind(&next);
4284 }
4285
4286 // If we reached this point there is a problem.
4287 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4288 } else {
4289 UNREACHABLE();
4290 }
4291}
4292
4293
4294template<class T>
4295static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4296 int to_index = GetSequenceIndexFromFastElementsKind(
4297 TERMINAL_FAST_ELEMENTS_KIND);
4298 for (int i = 0; i <= to_index; ++i) {
4299 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4300 T stub(isolate, kind);
4301 stub.GetCode();
4302 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4303 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4304 stub1.GetCode();
4305 }
4306 }
4307}
4308
4309
4310void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4311 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4312 isolate);
4313 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4314 isolate);
4315 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4316 isolate);
4317}
4318
4319
4320void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4321 Isolate* isolate) {
4322 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4323 for (int i = 0; i < 2; i++) {
4324 // For internal arrays we only need a few things
4325 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4326 stubh1.GetCode();
4327 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4328 stubh2.GetCode();
4329 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4330 stubh3.GetCode();
4331 }
4332}
4333
4334
4335void ArrayConstructorStub::GenerateDispatchToArrayStub(
4336 MacroAssembler* masm,
4337 AllocationSiteOverrideMode mode) {
4338 if (argument_count() == ANY) {
4339 Label not_zero_case, not_one_case;
4340 __ testp(rax, rax);
4341 __ j(not_zero, &not_zero_case);
4342 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4343
4344 __ bind(&not_zero_case);
4345 __ cmpl(rax, Immediate(1));
4346 __ j(greater, &not_one_case);
4347 CreateArrayDispatchOneArgument(masm, mode);
4348
4349 __ bind(&not_one_case);
4350 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4351 } else if (argument_count() == NONE) {
4352 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4353 } else if (argument_count() == ONE) {
4354 CreateArrayDispatchOneArgument(masm, mode);
4355 } else if (argument_count() == MORE_THAN_ONE) {
4356 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4357 } else {
4358 UNREACHABLE();
4359 }
4360}
4361
4362
4363void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4364 // ----------- S t a t e -------------
4365 // -- rax : argc
4366 // -- rbx : AllocationSite or undefined
4367 // -- rdi : constructor
4368 // -- rsp[0] : return address
4369 // -- rsp[8] : last argument
4370 // -----------------------------------
4371 if (FLAG_debug_code) {
4372 // The array construct code is only set for the global and natives
4373 // builtin Array functions which always have maps.
4374
4375 // Initial map for the builtin Array function should be a map.
4376 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
4377 // Will both indicate a NULL and a Smi.
4378 STATIC_ASSERT(kSmiTag == 0);
4379 Condition not_smi = NegateCondition(masm->CheckSmi(rcx));
4380 __ Check(not_smi, kUnexpectedInitialMapForArrayFunction);
4381 __ CmpObjectType(rcx, MAP_TYPE, rcx);
4382 __ Check(equal, kUnexpectedInitialMapForArrayFunction);
4383
4384 // We should either have undefined in rbx or a valid AllocationSite
4385 __ AssertUndefinedOrAllocationSite(rbx);
4386 }
4387
4388 Label no_info;
4389 // If the feedback vector is the undefined value call an array constructor
4390 // that doesn't use AllocationSites.
4391 __ CompareRoot(rbx, Heap::kUndefinedValueRootIndex);
4392 __ j(equal, &no_info);
4393
4394 // Only look at the lower 16 bits of the transition info.
4395 __ movp(rdx, FieldOperand(rbx, AllocationSite::kTransitionInfoOffset));
4396 __ SmiToInteger32(rdx, rdx);
4397 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4398 __ andp(rdx, Immediate(AllocationSite::ElementsKindBits::kMask));
4399 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4400
4401 __ bind(&no_info);
4402 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
4403}
4404
4405
4406void InternalArrayConstructorStub::GenerateCase(
4407 MacroAssembler* masm, ElementsKind kind) {
4408 Label not_zero_case, not_one_case;
4409 Label normal_sequence;
4410
4411 __ testp(rax, rax);
4412 __ j(not_zero, &not_zero_case);
4413 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4414 __ TailCallStub(&stub0);
4415
4416 __ bind(&not_zero_case);
4417 __ cmpl(rax, Immediate(1));
4418 __ j(greater, &not_one_case);
4419
4420 if (IsFastPackedElementsKind(kind)) {
4421 // We might need to create a holey array
4422 // look at the first argument
4423 StackArgumentsAccessor args(rsp, 1, ARGUMENTS_DONT_CONTAIN_RECEIVER);
4424 __ movp(rcx, args.GetArgumentOperand(0));
4425 __ testp(rcx, rcx);
4426 __ j(zero, &normal_sequence);
4427
4428 InternalArraySingleArgumentConstructorStub
4429 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4430 __ TailCallStub(&stub1_holey);
4431 }
4432
4433 __ bind(&normal_sequence);
4434 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4435 __ TailCallStub(&stub1);
4436
4437 __ bind(&not_one_case);
4438 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4439 __ TailCallStub(&stubN);
4440}
4441
4442
4443void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4444 // ----------- S t a t e -------------
4445 // -- rax : argc
4446 // -- rdi : constructor
4447 // -- rsp[0] : return address
4448 // -- rsp[8] : last argument
4449 // -----------------------------------
4450
4451 if (FLAG_debug_code) {
4452 // The array construct code is only set for the global and natives
4453 // builtin Array functions which always have maps.
4454
4455 // Initial map for the builtin Array function should be a map.
4456 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
4457 // Will both indicate a NULL and a Smi.
4458 STATIC_ASSERT(kSmiTag == 0);
4459 Condition not_smi = NegateCondition(masm->CheckSmi(rcx));
4460 __ Check(not_smi, kUnexpectedInitialMapForArrayFunction);
4461 __ CmpObjectType(rcx, MAP_TYPE, rcx);
4462 __ Check(equal, kUnexpectedInitialMapForArrayFunction);
4463 }
4464
4465 // Figure out the right elements kind
4466 __ movp(rcx, FieldOperand(rdi, JSFunction::kPrototypeOrInitialMapOffset));
4467
4468 // Load the map's "bit field 2" into |result|. We only need the first byte,
4469 // but the following masking takes care of that anyway.
4470 __ movzxbp(rcx, FieldOperand(rcx, Map::kBitField2Offset));
4471 // Retrieve elements_kind from bit field 2.
4472 __ DecodeField<Map::ElementsKindBits>(rcx);
4473
4474 if (FLAG_debug_code) {
4475 Label done;
4476 __ cmpl(rcx, Immediate(FAST_ELEMENTS));
4477 __ j(equal, &done);
4478 __ cmpl(rcx, Immediate(FAST_HOLEY_ELEMENTS));
4479 __ Assert(equal,
4480 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
4481 __ bind(&done);
4482 }
4483
4484 Label fast_elements_case;
4485 __ cmpl(rcx, Immediate(FAST_ELEMENTS));
4486 __ j(equal, &fast_elements_case);
4487 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4488
4489 __ bind(&fast_elements_case);
4490 GenerateCase(masm, FAST_ELEMENTS);
4491}
4492
4493
4494void CallApiFunctionStub::Generate(MacroAssembler* masm) {
4495 // ----------- S t a t e -------------
4496 // -- rax : callee
4497 // -- rbx : call_data
4498 // -- rcx : holder
4499 // -- rdx : api_function_address
4500 // -- rsi : context
4501 // --
4502 // -- rsp[0] : return address
4503 // -- rsp[8] : last argument
4504 // -- ...
4505 // -- rsp[argc * 8] : first argument
4506 // -- rsp[(argc + 1) * 8] : receiver
4507 // -----------------------------------
4508
4509 Register callee = rax;
4510 Register call_data = rbx;
4511 Register holder = rcx;
4512 Register api_function_address = rdx;
4513 Register return_address = rdi;
4514 Register context = rsi;
4515
4516 int argc = this->argc();
4517 bool is_store = this->is_store();
4518 bool call_data_undefined = this->call_data_undefined();
4519
4520 typedef FunctionCallbackArguments FCA;
4521
4522 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
4523 STATIC_ASSERT(FCA::kCalleeIndex == 5);
4524 STATIC_ASSERT(FCA::kDataIndex == 4);
4525 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
4526 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
4527 STATIC_ASSERT(FCA::kIsolateIndex == 1);
4528 STATIC_ASSERT(FCA::kHolderIndex == 0);
4529 STATIC_ASSERT(FCA::kArgsLength == 7);
4530
4531 __ PopReturnAddressTo(return_address);
4532
4533 // context save
4534 __ Push(context);
4535 // load context from callee
4536 __ movp(context, FieldOperand(callee, JSFunction::kContextOffset));
4537
4538 // callee
4539 __ Push(callee);
4540
4541 // call data
4542 __ Push(call_data);
4543 Register scratch = call_data;
4544 if (!call_data_undefined) {
4545 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4546 }
4547 // return value
4548 __ Push(scratch);
4549 // return value default
4550 __ Push(scratch);
4551 // isolate
4552 __ Move(scratch,
4553 ExternalReference::isolate_address(isolate()));
4554 __ Push(scratch);
4555 // holder
4556 __ Push(holder);
4557
4558 __ movp(scratch, rsp);
4559 // Push return address back on stack.
4560 __ PushReturnAddressFrom(return_address);
4561
4562 // Allocate the v8::Arguments structure in the arguments' space since
4563 // it's not controlled by GC.
4564 const int kApiStackSpace = 4;
4565
4566 __ PrepareCallApiFunction(kApiStackSpace);
4567
4568 // FunctionCallbackInfo::implicit_args_.
4569 __ movp(StackSpaceOperand(0), scratch);
4570 __ addp(scratch, Immediate((argc + FCA::kArgsLength - 1) * kPointerSize));
4571 __ movp(StackSpaceOperand(1), scratch); // FunctionCallbackInfo::values_.
4572 __ Set(StackSpaceOperand(2), argc); // FunctionCallbackInfo::length_.
4573 // FunctionCallbackInfo::is_construct_call_.
4574 __ Set(StackSpaceOperand(3), 0);
4575
4576#if defined(__MINGW64__) || defined(_WIN64)
4577 Register arguments_arg = rcx;
4578 Register callback_arg = rdx;
4579#else
4580 Register arguments_arg = rdi;
4581 Register callback_arg = rsi;
4582#endif
4583
4584 // It's okay if api_function_address == callback_arg
4585 // but not arguments_arg
4586 DCHECK(!api_function_address.is(arguments_arg));
4587
4588 // v8::InvocationCallback's argument.
4589 __ leap(arguments_arg, StackSpaceOperand(0));
4590
4591 ExternalReference thunk_ref =
4592 ExternalReference::invoke_function_callback(isolate());
4593
4594 // Accessor for FunctionCallbackInfo and first js arg.
4595 StackArgumentsAccessor args_from_rbp(rbp, FCA::kArgsLength + 1,
4596 ARGUMENTS_DONT_CONTAIN_RECEIVER);
4597 Operand context_restore_operand = args_from_rbp.GetArgumentOperand(
4598 FCA::kArgsLength - FCA::kContextSaveIndex);
4599 // Stores return the first js argument
4600 Operand return_value_operand = args_from_rbp.GetArgumentOperand(
4601 is_store ? 0 : FCA::kArgsLength - FCA::kReturnValueOffset);
4602 __ CallApiFunctionAndReturn(
4603 api_function_address,
4604 thunk_ref,
4605 callback_arg,
4606 argc + FCA::kArgsLength + 1,
4607 return_value_operand,
4608 &context_restore_operand);
4609}
4610
4611
4612void CallApiGetterStub::Generate(MacroAssembler* masm) {
4613 // ----------- S t a t e -------------
4614 // -- rsp[0] : return address
4615 // -- rsp[8] : name
4616 // -- rsp[16 - kArgsLength*8] : PropertyCallbackArguments object
4617 // -- ...
4618 // -- r8 : api_function_address
4619 // -----------------------------------
4620
4621#if defined(__MINGW64__) || defined(_WIN64)
4622 Register getter_arg = r8;
4623 Register accessor_info_arg = rdx;
4624 Register name_arg = rcx;
4625#else
4626 Register getter_arg = rdx;
4627 Register accessor_info_arg = rsi;
4628 Register name_arg = rdi;
4629#endif
4630 Register api_function_address = ApiGetterDescriptor::function_address();
4631 DCHECK(api_function_address.is(r8));
4632 Register scratch = rax;
4633
4634 // v8::Arguments::values_ and handler for name.
4635 const int kStackSpace = PropertyCallbackArguments::kArgsLength + 1;
4636
4637 // Allocate v8::AccessorInfo in non-GCed stack space.
4638 const int kArgStackSpace = 1;
4639
4640 __ leap(name_arg, Operand(rsp, kPCOnStackSize));
4641
4642 __ PrepareCallApiFunction(kArgStackSpace);
4643 __ leap(scratch, Operand(name_arg, 1 * kPointerSize));
4644
4645 // v8::PropertyAccessorInfo::args_.
4646 __ movp(StackSpaceOperand(0), scratch);
4647
4648 // The context register (rsi) has been saved in PrepareCallApiFunction and
4649 // could be used to pass arguments.
4650 __ leap(accessor_info_arg, StackSpaceOperand(0));
4651
4652 ExternalReference thunk_ref =
4653 ExternalReference::invoke_accessor_getter_callback(isolate());
4654
4655 // It's okay if api_function_address == getter_arg
4656 // but not accessor_info_arg or name_arg
4657 DCHECK(!api_function_address.is(accessor_info_arg) &&
4658 !api_function_address.is(name_arg));
4659
4660 // The name handler is counted as an argument.
4661 StackArgumentsAccessor args(rbp, PropertyCallbackArguments::kArgsLength);
4662 Operand return_value_operand = args.GetArgumentOperand(
4663 PropertyCallbackArguments::kArgsLength - 1 -
4664 PropertyCallbackArguments::kReturnValueOffset);
4665 __ CallApiFunctionAndReturn(api_function_address,
4666 thunk_ref,
4667 getter_arg,
4668 kStackSpace,
4669 return_value_operand,
4670 NULL);
4671}
4672
4673
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004674#undef __
4675
4676} } // namespace v8::internal
4677
4678#endif // V8_TARGET_ARCH_X64