blob: 53b35a3a841c666cc03a5449a774703b1de5f026 [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// 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#if V8_TARGET_ARCH_IA32
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006
Ben Murdochda12d292016-06-02 14:46:10 +01007#include "src/code-stubs.h"
8#include "src/api-arguments.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/bits.h"
10#include "src/bootstrapper.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/codegen.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012#include "src/ia32/code-stubs-ia32.h"
13#include "src/ia32/frames-ia32.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "src/ic/handler-compiler.h"
15#include "src/ic/ic.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016#include "src/ic/stub-cache.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000017#include "src/isolate.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018#include "src/regexp/jsregexp.h"
19#include "src/regexp/regexp-macro-assembler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040020#include "src/runtime/runtime.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010021
22namespace v8 {
23namespace internal {
24
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025
26static void InitializeArrayConstructorDescriptor(
27 Isolate* isolate, CodeStubDescriptor* descriptor,
28 int constant_stack_parameter_count) {
29 // register state
30 // eax -- number of arguments
31 // edi -- function
32 // ebx -- allocation site with elements kind
33 Address deopt_handler = Runtime::FunctionForId(
34 Runtime::kArrayConstructor)->entry;
35
36 if (constant_stack_parameter_count == 0) {
37 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
38 JS_FUNCTION_STUB_MODE);
39 } else {
40 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 }
43}
44
45
46static void InitializeInternalArrayConstructorDescriptor(
47 Isolate* isolate, CodeStubDescriptor* descriptor,
48 int constant_stack_parameter_count) {
49 // register state
50 // eax -- number of arguments
51 // edi -- constructor function
52 Address deopt_handler = Runtime::FunctionForId(
53 Runtime::kInternalArrayConstructor)->entry;
54
55 if (constant_stack_parameter_count == 0) {
56 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
57 JS_FUNCTION_STUB_MODE);
58 } else {
59 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 }
62}
63
64
65void ArrayNoArgumentConstructorStub::InitializeDescriptor(
66 CodeStubDescriptor* descriptor) {
67 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
68}
69
70
71void ArraySingleArgumentConstructorStub::InitializeDescriptor(
72 CodeStubDescriptor* descriptor) {
73 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
74}
75
76
77void ArrayNArgumentsConstructorStub::InitializeDescriptor(
78 CodeStubDescriptor* descriptor) {
79 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
80}
81
82
83void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
84 CodeStubDescriptor* descriptor) {
85 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
86}
87
Ben Murdochda12d292016-06-02 14:46:10 +010088void FastArrayPushStub::InitializeDescriptor(CodeStubDescriptor* descriptor) {
89 Address deopt_handler = Runtime::FunctionForId(Runtime::kArrayPush)->entry;
90 descriptor->Initialize(eax, deopt_handler, -1, JS_FUNCTION_STUB_MODE);
91}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092
93void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
94 CodeStubDescriptor* descriptor) {
95 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
96}
97
98
99void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
100 CodeStubDescriptor* descriptor) {
101 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
102}
103
104
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100105#define __ ACCESS_MASM(masm)
Steve Block1e0659c2011-05-24 12:43:12 +0100106
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107
108void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
109 ExternalReference miss) {
110 // Update the static counter each time a new code stub is generated.
111 isolate()->counters()->code_stubs()->Increment();
112
113 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 int param_count = descriptor.GetRegisterParameterCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 {
116 // Call the runtime system in a fresh internal frame.
117 FrameScope scope(masm, StackFrame::INTERNAL);
118 DCHECK(param_count == 0 ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 eax.is(descriptor.GetRegisterParameter(param_count - 1)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 // Push arguments
121 for (int i = 0; i < param_count; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 __ push(descriptor.GetRegisterParameter(i));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 }
124 __ CallExternalReference(miss, param_count);
125 }
126
Steve Block1e0659c2011-05-24 12:43:12 +0100127 __ ret(0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000128}
129
130
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100131void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
132 // We don't allow a GC during a store buffer overflow so there is no need to
133 // store the registers in any particular way, but we do have to store and
134 // restore them.
135 __ pushad();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 if (save_doubles()) {
137 __ sub(esp, Immediate(kDoubleSize * XMMRegister::kMaxNumRegisters));
138 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100139 XMMRegister reg = XMMRegister::from_code(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 __ movsd(Operand(esp, i * kDoubleSize), reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100141 }
142 }
143 const int argument_count = 1;
144
145 AllowExternalCallThatCantCauseGC scope(masm);
146 __ PrepareCallCFunction(argument_count, ecx);
147 __ mov(Operand(esp, 0 * kPointerSize),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000148 Immediate(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100149 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 ExternalReference::store_buffer_overflow_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100151 argument_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 if (save_doubles()) {
153 for (int i = 0; i < XMMRegister::kMaxNumRegisters; i++) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100154 XMMRegister reg = XMMRegister::from_code(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155 __ movsd(reg, Operand(esp, i * kDoubleSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100156 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000157 __ add(esp, Immediate(kDoubleSize * XMMRegister::kMaxNumRegisters));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100158 }
159 __ popad();
160 __ ret(0);
161}
162
163
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100164class FloatingPointHelper : public AllStatic {
165 public:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100166 enum ArgLocation {
167 ARGS_ON_STACK,
168 ARGS_IN_REGISTERS
169 };
170
171 // Code pattern for loading a floating point value. Input value must
172 // be either a smi or a heap number object (fp value). Requirements:
173 // operand in register number. Returns operand as floating point number
174 // on FPU stack.
175 static void LoadFloatOperand(MacroAssembler* masm, Register number);
176
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100177 // Test if operands are smi or number objects (fp). Requirements:
178 // operand_1 in eax, operand_2 in edx; falls through on float
179 // operands, jumps to the non_float label otherwise.
180 static void CheckFloatOperands(MacroAssembler* masm,
181 Label* non_float,
182 Register scratch);
183
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100184 // Test if operands are numbers (smi or HeapNumber objects), and load
185 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
186 // either operand is not a number. Operands are in edx and eax.
187 // Leaves operands unchanged.
188 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100189};
190
191
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192void DoubleToIStub::Generate(MacroAssembler* masm) {
193 Register input_reg = this->source();
194 Register final_result_reg = this->destination();
195 DCHECK(is_truncating());
196
197 Label check_negative, process_64_bits, done, done_no_stash;
198
199 int double_offset = offset();
200
201 // Account for return address and saved regs if input is esp.
202 if (input_reg.is(esp)) double_offset += 3 * kPointerSize;
203
204 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
205 MemOperand exponent_operand(MemOperand(input_reg,
206 double_offset + kDoubleSize / 2));
207
208 Register scratch1;
209 {
210 Register scratch_candidates[3] = { ebx, edx, edi };
211 for (int i = 0; i < 3; i++) {
212 scratch1 = scratch_candidates[i];
213 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
214 }
215 }
216 // Since we must use ecx for shifts below, use some other register (eax)
217 // to calculate the result if ecx is the requested return register.
218 Register result_reg = final_result_reg.is(ecx) ? eax : final_result_reg;
219 // Save ecx if it isn't the return register and therefore volatile, or if it
220 // is the return register, then save the temp register we use in its stead for
221 // the result.
222 Register save_reg = final_result_reg.is(ecx) ? eax : ecx;
223 __ push(scratch1);
224 __ push(save_reg);
225
226 bool stash_exponent_copy = !input_reg.is(esp);
227 __ mov(scratch1, mantissa_operand);
228 if (CpuFeatures::IsSupported(SSE3)) {
229 CpuFeatureScope scope(masm, SSE3);
Ben Murdoch257744e2011-11-30 15:57:28 +0000230 // Load x87 register with heap number.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000231 __ fld_d(mantissa_operand);
232 }
233 __ mov(ecx, exponent_operand);
234 if (stash_exponent_copy) __ push(ecx);
235
236 __ and_(ecx, HeapNumber::kExponentMask);
237 __ shr(ecx, HeapNumber::kExponentShift);
238 __ lea(result_reg, MemOperand(ecx, -HeapNumber::kExponentBias));
239 __ cmp(result_reg, Immediate(HeapNumber::kMantissaBits));
240 __ j(below, &process_64_bits);
241
242 // Result is entirely in lower 32-bits of mantissa
243 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
244 if (CpuFeatures::IsSupported(SSE3)) {
245 __ fstp(0);
246 }
247 __ sub(ecx, Immediate(delta));
248 __ xor_(result_reg, result_reg);
249 __ cmp(ecx, Immediate(31));
250 __ j(above, &done);
251 __ shl_cl(scratch1);
252 __ jmp(&check_negative);
253
254 __ bind(&process_64_bits);
255 if (CpuFeatures::IsSupported(SSE3)) {
256 CpuFeatureScope scope(masm, SSE3);
257 if (stash_exponent_copy) {
258 // Already a copy of the exponent on the stack, overwrite it.
259 STATIC_ASSERT(kDoubleSize == 2 * kPointerSize);
260 __ sub(esp, Immediate(kDoubleSize / 2));
261 } else {
262 // Reserve space for 64 bit answer.
263 __ sub(esp, Immediate(kDoubleSize)); // Nolint.
264 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000265 // Do conversion, which cannot fail because we checked the exponent.
266 __ fisttp_d(Operand(esp, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267 __ mov(result_reg, Operand(esp, 0)); // Load low word of answer as result
268 __ add(esp, Immediate(kDoubleSize));
269 __ jmp(&done_no_stash);
Ben Murdoch257744e2011-11-30 15:57:28 +0000270 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271 // Result must be extracted from shifted 32-bit mantissa
272 __ sub(ecx, Immediate(delta));
273 __ neg(ecx);
274 if (stash_exponent_copy) {
275 __ mov(result_reg, MemOperand(esp, 0));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100276 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000277 __ mov(result_reg, exponent_operand);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100278 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000279 __ and_(result_reg,
280 Immediate(static_cast<uint32_t>(Double::kSignificandMask >> 32)));
281 __ add(result_reg,
282 Immediate(static_cast<uint32_t>(Double::kHiddenBit >> 32)));
Ben Murdochda12d292016-06-02 14:46:10 +0100283 __ shrd_cl(scratch1, result_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284 __ shr_cl(result_reg);
285 __ test(ecx, Immediate(32));
286 __ cmov(not_equal, scratch1, result_reg);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100287 }
288
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289 // If the double was negative, negate the integer result.
290 __ bind(&check_negative);
291 __ mov(result_reg, scratch1);
292 __ neg(result_reg);
293 if (stash_exponent_copy) {
294 __ cmp(MemOperand(esp, 0), Immediate(0));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100295 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000296 __ cmp(exponent_operand, Immediate(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100297 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 __ cmov(greater, result_reg, scratch1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100299
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000300 // Restore registers
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100301 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000302 if (stash_exponent_copy) {
303 __ add(esp, Immediate(kDoubleSize / 2));
304 }
305 __ bind(&done_no_stash);
306 if (!final_result_reg.is(result_reg)) {
307 DCHECK(final_result_reg.is(ecx));
308 __ mov(final_result_reg, result_reg);
309 }
310 __ pop(save_reg);
311 __ pop(scratch1);
312 __ ret(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100313}
314
315
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100316void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
317 Register number) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000318 Label load_smi, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100319
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000320 __ JumpIfSmi(number, &load_smi, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100321 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000322 __ jmp(&done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100323
324 __ bind(&load_smi);
325 __ SmiUntag(number);
326 __ push(number);
327 __ fild_s(Operand(esp, 0));
328 __ pop(number);
329
330 __ bind(&done);
331}
332
333
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100334void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
335 Label* not_numbers) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000336 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100337 // Load operand in edx into xmm0, or branch to not_numbers.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000338 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +0100339 Factory* factory = masm->isolate()->factory();
340 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100341 __ j(not_equal, not_numbers); // Argument in edx is not a number.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000342 __ movsd(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100343 __ bind(&load_eax);
344 // Load operand in eax into xmm1, or branch to not_numbers.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000345 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +0100346 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
Ben Murdoch257744e2011-11-30 15:57:28 +0000347 __ j(equal, &load_float_eax, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100348 __ jmp(not_numbers); // Argument in eax is not a number.
349 __ bind(&load_smi_edx);
350 __ SmiUntag(edx); // Untag smi before converting to float.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000351 __ Cvtsi2sd(xmm0, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100352 __ SmiTag(edx); // Retag smi for heap number overwriting test.
353 __ jmp(&load_eax);
354 __ bind(&load_smi_eax);
355 __ SmiUntag(eax); // Untag smi before converting to float.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356 __ Cvtsi2sd(xmm1, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100357 __ SmiTag(eax); // Retag smi for heap number overwriting test.
Ben Murdoch257744e2011-11-30 15:57:28 +0000358 __ jmp(&done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100359 __ bind(&load_float_eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000360 __ movsd(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100361 __ bind(&done);
362}
363
364
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100365void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
366 Label* non_float,
367 Register scratch) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000368 Label test_other, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100369 // Test if both operands are floats or smi -> scratch=k_is_float;
370 // Otherwise scratch = k_not_float.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000371 __ JumpIfSmi(edx, &test_other, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100372 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100373 Factory* factory = masm->isolate()->factory();
374 __ cmp(scratch, factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100375 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
376
377 __ bind(&test_other);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000378 __ JumpIfSmi(eax, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100379 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100380 __ cmp(scratch, factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100381 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
382
383 // Fall-through: Both operands are numbers.
384 __ bind(&done);
385}
386
387
Ben Murdochb0fe1622011-05-05 13:52:32 +0100388void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000389 Factory* factory = isolate()->factory();
390 const Register exponent = MathPowTaggedDescriptor::exponent();
391 DCHECK(exponent.is(eax));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100392 const Register base = edx;
393 const Register scratch = ecx;
394 const XMMRegister double_result = xmm3;
395 const XMMRegister double_base = xmm2;
396 const XMMRegister double_exponent = xmm1;
397 const XMMRegister double_scratch = xmm4;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100398
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100399 Label call_runtime, done, exponent_not_smi, int_exponent;
Ben Murdochb0fe1622011-05-05 13:52:32 +0100400
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100401 // Save 1 in double_result - we need this several times later on.
402 __ mov(scratch, Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000403 __ Cvtsi2sd(double_result, scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100404
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000405 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100406 Label base_is_smi, unpack_exponent;
407 // The exponent and base are supplied as arguments on the stack.
408 // This can only happen if the stub is called from non-optimized code.
409 // Load input parameters from stack.
410 __ mov(base, Operand(esp, 2 * kPointerSize));
411 __ mov(exponent, Operand(esp, 1 * kPointerSize));
412
413 __ JumpIfSmi(base, &base_is_smi, Label::kNear);
414 __ cmp(FieldOperand(base, HeapObject::kMapOffset),
415 factory->heap_number_map());
416 __ j(not_equal, &call_runtime);
417
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000418 __ movsd(double_base, FieldOperand(base, HeapNumber::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100419 __ jmp(&unpack_exponent, Label::kNear);
420
421 __ bind(&base_is_smi);
422 __ SmiUntag(base);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000423 __ Cvtsi2sd(double_base, base);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100424
425 __ bind(&unpack_exponent);
426 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
427 __ SmiUntag(exponent);
428 __ jmp(&int_exponent);
429
430 __ bind(&exponent_not_smi);
431 __ cmp(FieldOperand(exponent, HeapObject::kMapOffset),
432 factory->heap_number_map());
433 __ j(not_equal, &call_runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 __ movsd(double_exponent,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100435 FieldOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000436 } else if (exponent_type() == TAGGED) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100437 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
438 __ SmiUntag(exponent);
439 __ jmp(&int_exponent);
440
441 __ bind(&exponent_not_smi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000442 __ movsd(double_exponent,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100443 FieldOperand(exponent, HeapNumber::kValueOffset));
444 }
445
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446 if (exponent_type() != INTEGER) {
447 Label fast_power, try_arithmetic_simplification;
448 __ DoubleToI(exponent, double_exponent, double_scratch,
449 TREAT_MINUS_ZERO_AS_ZERO, &try_arithmetic_simplification,
450 &try_arithmetic_simplification,
451 &try_arithmetic_simplification);
452 __ jmp(&int_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100453
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000454 __ bind(&try_arithmetic_simplification);
455 // Skip to runtime if possibly NaN (indicated by the indefinite integer).
456 __ cvttsd2si(exponent, Operand(double_exponent));
457 __ cmp(exponent, Immediate(0x1));
458 __ j(overflow, &call_runtime);
459
460 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100461 // Detect square root case. Crankshaft detects constant +/-0.5 at
462 // compile time and uses DoMathPowHalf instead. We then skip this check
463 // for non-constant cases of +/-0.5 as these hardly occur.
464 Label continue_sqrt, continue_rsqrt, not_plus_half;
465 // Test for 0.5.
466 // Load double_scratch with 0.5.
467 __ mov(scratch, Immediate(0x3F000000u));
468 __ movd(double_scratch, scratch);
469 __ cvtss2sd(double_scratch, double_scratch);
470 // Already ruled out NaNs for exponent.
471 __ ucomisd(double_scratch, double_exponent);
472 __ j(not_equal, &not_plus_half, Label::kNear);
473
474 // Calculates square root of base. Check for the special case of
475 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
476 // According to IEEE-754, single-precision -Infinity has the highest
477 // 9 bits set and the lowest 23 bits cleared.
478 __ mov(scratch, 0xFF800000u);
479 __ movd(double_scratch, scratch);
480 __ cvtss2sd(double_scratch, double_scratch);
481 __ ucomisd(double_base, double_scratch);
482 // Comparing -Infinity with NaN results in "unordered", which sets the
483 // zero flag as if both were equal. However, it also sets the carry flag.
484 __ j(not_equal, &continue_sqrt, Label::kNear);
485 __ j(carry, &continue_sqrt, Label::kNear);
486
487 // Set result to Infinity in the special case.
488 __ xorps(double_result, double_result);
489 __ subsd(double_result, double_scratch);
490 __ jmp(&done);
491
492 __ bind(&continue_sqrt);
493 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
494 __ xorps(double_scratch, double_scratch);
495 __ addsd(double_scratch, double_base); // Convert -0 to +0.
496 __ sqrtsd(double_result, double_scratch);
497 __ jmp(&done);
498
499 // Test for -0.5.
500 __ bind(&not_plus_half);
501 // Load double_exponent with -0.5 by substracting 1.
502 __ subsd(double_scratch, double_result);
503 // Already ruled out NaNs for exponent.
504 __ ucomisd(double_scratch, double_exponent);
505 __ j(not_equal, &fast_power, Label::kNear);
506
507 // Calculates reciprocal of square root of base. Check for the special
508 // case of Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
509 // According to IEEE-754, single-precision -Infinity has the highest
510 // 9 bits set and the lowest 23 bits cleared.
511 __ mov(scratch, 0xFF800000u);
512 __ movd(double_scratch, scratch);
513 __ cvtss2sd(double_scratch, double_scratch);
514 __ ucomisd(double_base, double_scratch);
515 // Comparing -Infinity with NaN results in "unordered", which sets the
516 // zero flag as if both were equal. However, it also sets the carry flag.
517 __ j(not_equal, &continue_rsqrt, Label::kNear);
518 __ j(carry, &continue_rsqrt, Label::kNear);
519
520 // Set result to 0 in the special case.
521 __ xorps(double_result, double_result);
522 __ jmp(&done);
523
524 __ bind(&continue_rsqrt);
525 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
526 __ xorps(double_exponent, double_exponent);
527 __ addsd(double_exponent, double_base); // Convert -0 to +0.
528 __ sqrtsd(double_exponent, double_exponent);
529 __ divsd(double_result, double_exponent);
530 __ jmp(&done);
531 }
532
533 // Using FPU instructions to calculate power.
534 Label fast_power_failed;
535 __ bind(&fast_power);
536 __ fnclex(); // Clear flags to catch exceptions later.
537 // Transfer (B)ase and (E)xponent onto the FPU register stack.
538 __ sub(esp, Immediate(kDoubleSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539 __ movsd(Operand(esp, 0), double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100540 __ fld_d(Operand(esp, 0)); // E
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541 __ movsd(Operand(esp, 0), double_base);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100542 __ fld_d(Operand(esp, 0)); // B, E
543
544 // Exponent is in st(1) and base is in st(0)
545 // B ^ E = (2^(E * log2(B)) - 1) + 1 = (2^X - 1) + 1 for X = E * log2(B)
546 // FYL2X calculates st(1) * log2(st(0))
547 __ fyl2x(); // X
548 __ fld(0); // X, X
549 __ frndint(); // rnd(X), X
550 __ fsub(1); // rnd(X), X-rnd(X)
551 __ fxch(1); // X - rnd(X), rnd(X)
552 // F2XM1 calculates 2^st(0) - 1 for -1 < st(0) < 1
553 __ f2xm1(); // 2^(X-rnd(X)) - 1, rnd(X)
554 __ fld1(); // 1, 2^(X-rnd(X)) - 1, rnd(X)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000555 __ faddp(1); // 2^(X-rnd(X)), rnd(X)
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100556 // FSCALE calculates st(0) * 2^st(1)
557 __ fscale(); // 2^X, rnd(X)
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558 __ fstp(1); // 2^X
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100559 // Bail out to runtime in case of exceptions in the status word.
560 __ fnstsw_ax();
Ben Murdochda12d292016-06-02 14:46:10 +0100561 __ test_b(eax,
562 Immediate(0x5F)); // We check for all but precision exception.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100563 __ j(not_zero, &fast_power_failed, Label::kNear);
564 __ fstp_d(Operand(esp, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565 __ movsd(double_result, Operand(esp, 0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100566 __ add(esp, Immediate(kDoubleSize));
567 __ jmp(&done);
568
569 __ bind(&fast_power_failed);
570 __ fninit();
571 __ add(esp, Immediate(kDoubleSize));
572 __ jmp(&call_runtime);
573 }
574
575 // Calculate power with integer exponent.
576 __ bind(&int_exponent);
577 const XMMRegister double_scratch2 = double_exponent;
578 __ mov(scratch, exponent); // Back up exponent.
579 __ movsd(double_scratch, double_base); // Back up base.
580 __ movsd(double_scratch2, double_result); // Load double_exponent with 1.
Ben Murdochb0fe1622011-05-05 13:52:32 +0100581
582 // Get absolute value of exponent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000583 Label no_neg, while_true, while_false;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100584 __ test(scratch, scratch);
585 __ j(positive, &no_neg, Label::kNear);
586 __ neg(scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100587 __ bind(&no_neg);
588
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 __ j(zero, &while_false, Label::kNear);
590 __ shr(scratch, 1);
591 // Above condition means CF==0 && ZF==0. This means that the
592 // bit that has been shifted out is 0 and the result is not 0.
593 __ j(above, &while_true, Label::kNear);
594 __ movsd(double_result, double_scratch);
595 __ j(zero, &while_false, Label::kNear);
596
Ben Murdoch85b71792012-04-11 18:30:58 +0100597 __ bind(&while_true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100598 __ shr(scratch, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100599 __ mulsd(double_scratch, double_scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000600 __ j(above, &while_true, Label::kNear);
601 __ mulsd(double_result, double_scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100602 __ j(not_zero, &while_true);
603
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000604 __ bind(&while_false);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100605 // scratch has the original value of the exponent - if the exponent is
606 // negative, return 1/result.
607 __ test(exponent, exponent);
608 __ j(positive, &done);
609 __ divsd(double_scratch2, double_result);
610 __ movsd(double_result, double_scratch2);
611 // Test whether result is zero. Bail out to check for subnormal result.
612 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
613 __ xorps(double_scratch2, double_scratch2);
614 __ ucomisd(double_scratch2, double_result); // Result cannot be NaN.
615 // double_exponent aliased as double_scratch2 has already been overwritten
616 // and may not have contained the exponent value in the first place when the
617 // exponent is a smi. We reset it with exponent value before bailing out.
618 __ j(not_equal, &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000619 __ Cvtsi2sd(double_exponent, exponent);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100620
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100621 // Returning or bailing out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000622 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100623 // The arguments are still on the stack.
624 __ bind(&call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000625 __ TailCallRuntime(Runtime::kMathPowRT);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100626
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100627 // The stub is called from non-optimized code, which expects the result
628 // as heap number in exponent.
629 __ bind(&done);
630 __ AllocateHeapNumber(eax, scratch, base, &call_runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000631 __ movsd(FieldOperand(eax, HeapNumber::kValueOffset), double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100632 __ ret(2 * kPointerSize);
633 } else {
634 __ bind(&call_runtime);
635 {
636 AllowExternalCallThatCantCauseGC scope(masm);
637 __ PrepareCallCFunction(4, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000638 __ movsd(Operand(esp, 0 * kDoubleSize), double_base);
639 __ movsd(Operand(esp, 1 * kDoubleSize), double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100640 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641 ExternalReference::power_double_double_function(isolate()), 4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100642 }
643 // Return value is in st(0) on ia32.
644 // Store it into the (fixed) result register.
645 __ sub(esp, Immediate(kDoubleSize));
646 __ fstp_d(Operand(esp, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647 __ movsd(double_result, Operand(esp, 0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100648 __ add(esp, Immediate(kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100649
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100650 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100651 __ ret(0);
652 }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100653}
654
655
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000656void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
657 Label miss;
658 Register receiver = LoadDescriptor::ReceiverRegister();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000659 // With careful management, we won't have to save slot and vector on
660 // the stack. Simply handle the possibly missing case first.
661 // TODO(mvstanton): this code can be more efficient.
662 __ cmp(FieldOperand(receiver, JSFunction::kPrototypeOrInitialMapOffset),
663 Immediate(isolate()->factory()->the_hole_value()));
664 __ j(equal, &miss);
665 __ TryGetFunctionPrototype(receiver, eax, ebx, &miss);
666 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000667
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000668 __ bind(&miss);
669 PropertyAccessCompiler::TailCallBuiltin(
670 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
671}
672
673
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400674void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
675 // Return address is on the stack.
676 Label miss;
677
678 Register receiver = LoadDescriptor::ReceiverRegister();
679 Register index = LoadDescriptor::NameRegister();
680 Register scratch = edi;
681 DCHECK(!scratch.is(receiver) && !scratch.is(index));
682 Register result = eax;
683 DCHECK(!result.is(scratch));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000684 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
685 result.is(LoadDescriptor::SlotRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400686
687 // StringCharAtGenerator doesn't use the result register until it's passed
688 // the different miss possibilities. If it did, we would have a conflict
689 // when FLAG_vector_ics is true.
690 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
691 &miss, // When not a string.
692 &miss, // When not a number.
693 &miss, // When index out of range.
694 STRING_INDEX_IS_ARRAY_INDEX,
695 RECEIVER_IS_STRING);
696 char_at_generator.GenerateFast(masm);
697 __ ret(0);
698
699 StubRuntimeCallHelper call_helper;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000700 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400701
702 __ bind(&miss);
703 PropertyAccessCompiler::TailCallBuiltin(
704 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
705}
706
707
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100708void RegExpExecStub::Generate(MacroAssembler* masm) {
709 // Just jump directly to runtime if native RegExp is not selected at compile
710 // time or if regexp entry in generated code is turned off runtime switch or
711 // at compilation.
712#ifdef V8_INTERPRETED_REGEXP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000713 __ TailCallRuntime(Runtime::kRegExpExec);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100714#else // V8_INTERPRETED_REGEXP
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100715
716 // Stack frame on entry.
717 // esp[0]: return address
718 // esp[4]: last_match_info (expected JSArray)
719 // esp[8]: previous index
720 // esp[12]: subject string
721 // esp[16]: JSRegExp object
722
723 static const int kLastMatchInfoOffset = 1 * kPointerSize;
724 static const int kPreviousIndexOffset = 2 * kPointerSize;
725 static const int kSubjectOffset = 3 * kPointerSize;
726 static const int kJSRegExpOffset = 4 * kPointerSize;
727
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000728 Label runtime;
729 Factory* factory = isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100730
731 // Ensure that a RegExp stack is allocated.
732 ExternalReference address_of_regexp_stack_memory_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 ExternalReference::address_of_regexp_stack_memory_address(isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100734 ExternalReference address_of_regexp_stack_memory_size =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000735 ExternalReference::address_of_regexp_stack_memory_size(isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100736 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100737 __ test(ebx, ebx);
Ben Murdoch257744e2011-11-30 15:57:28 +0000738 __ j(zero, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100739
740 // Check that the first argument is a JSRegExp object.
741 __ mov(eax, Operand(esp, kJSRegExpOffset));
742 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000743 __ JumpIfSmi(eax, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100744 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
745 __ j(not_equal, &runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000746
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100747 // Check that the RegExp has been compiled (data contains a fixed array).
748 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
749 if (FLAG_debug_code) {
750 __ test(ecx, Immediate(kSmiTagMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000751 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100752 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000753 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100754 }
755
756 // ecx: RegExp data (FixedArray)
757 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
758 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100759 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100760 __ j(not_equal, &runtime);
761
762 // ecx: RegExp data (FixedArray)
763 // Check that the number of captures fit in the static offsets vector buffer.
764 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000765 // Check (number_of_captures + 1) * 2 <= offsets vector size
766 // Or number_of_captures * 2 <= offsets vector size - 2
767 // Multiplying by 2 comes for free since edx is smi-tagged.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100768 STATIC_ASSERT(kSmiTag == 0);
769 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000770 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
771 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100772 __ j(above, &runtime);
773
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000774 // Reset offset for possibly sliced string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000775 __ Move(edi, Immediate(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100776 __ mov(eax, Operand(esp, kSubjectOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777 __ JumpIfSmi(eax, &runtime);
778 __ mov(edx, eax); // Make a copy of the original subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000779
780 // eax: subject string
781 // edx: subject string
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000782 // ecx: RegExp data (FixedArray)
783 // Handle subject string according to its encoding and representation:
784 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch097c5b22016-05-18 11:27:45 +0100785 // (2) Sequential one byte? If yes, go to (5).
786 // (3) Sequential or cons? If not, go to (6).
787 // (4) Cons string. If the string is flat, replace subject with first string
788 // and go to (1). Otherwise bail out to runtime.
789 // (5) One byte sequential. Load regexp code for one byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790 // (E) Carry on.
791 /// [...]
792
793 // Deferred code at the end of the stub:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100794 // (6) Long external string? If not, go to (10).
795 // (7) External string. Make it, offset-wise, look like a sequential string.
796 // (8) Is the external string one byte? If yes, go to (5).
797 // (9) Two byte sequential. Load regexp code for two byte. Go to (E).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000798 // (10) Short external string or not a string? If yes, bail out to runtime.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100799 // (11) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000800
Ben Murdoch097c5b22016-05-18 11:27:45 +0100801 Label seq_one_byte_string /* 5 */, seq_two_byte_string /* 9 */,
802 external_string /* 7 */, check_underlying /* 1 */,
803 not_seq_nor_cons /* 6 */, check_code /* E */, not_long_external /* 10 */;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804
Ben Murdoch097c5b22016-05-18 11:27:45 +0100805 __ bind(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000806 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch097c5b22016-05-18 11:27:45 +0100807 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
808 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
809
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100810 __ and_(ebx, kIsNotStringMask |
811 kStringRepresentationMask |
812 kStringEncodingMask |
813 kShortExternalStringMask);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100814 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000815 __ j(zero, &seq_two_byte_string); // Go to (9).
816
Ben Murdoch097c5b22016-05-18 11:27:45 +0100817 // (2) Sequential one byte? If yes, go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 // Any other sequential string must be one byte.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100819 __ and_(ebx, Immediate(kIsNotStringMask |
820 kStringRepresentationMask |
821 kShortExternalStringMask));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100822 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (5).
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100823
Ben Murdoch097c5b22016-05-18 11:27:45 +0100824 // (3) Sequential or cons? If not, go to (6).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000825 // We check whether the subject string is a cons, since sequential strings
826 // have already been covered.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000827 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
828 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100829 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
830 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
831 __ cmp(ebx, Immediate(kExternalStringTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100832 __ j(greater_equal, &not_seq_nor_cons); // Go to (6).
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100833
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000834 // (4) Cons string. Check that it's flat.
835 // Replace subject with first string and reload instance type.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000836 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100837 __ j(not_equal, &runtime);
838 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100839 __ jmp(&check_underlying);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100840
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000841 // eax: sequential subject string (or look-alike, external string)
842 // edx: original subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100843 // ecx: RegExp data (FixedArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100844 // (5) One byte sequential. Load regexp code for one byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000845 __ bind(&seq_one_byte_string);
846 // Load previous index and check range before edx is overwritten. We have
847 // to use edx instead of eax here because it might have been only made to
848 // look like a sequential string when it actually is an external string.
849 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
850 __ JumpIfNotSmi(ebx, &runtime);
851 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
852 __ j(above_equal, &runtime);
853 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataOneByteCodeOffset));
854 __ Move(ecx, Immediate(1)); // Type is one byte.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100855
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000856 // (E) Carry on. String handling is done.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100857 __ bind(&check_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000858 // edx: irregexp code
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100859 // Check that the irregexp code has been generated for the actual string
860 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +0000861 // a smi (code flushing support).
862 __ JumpIfSmi(edx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100863
864 // eax: subject string
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000865 // ebx: previous index (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100866 // edx: code
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000867 // ecx: encoding of subject string (1 if one_byte, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100868 // All checks done. Now push arguments for native regexp code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000869 Counters* counters = isolate()->counters();
Steve Block44f0eee2011-05-26 01:26:41 +0100870 __ IncrementCounter(counters->regexp_entry_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100871
Steve Block44f0eee2011-05-26 01:26:41 +0100872 // Isolates: note we add an additional parameter here (isolate pointer).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873 static const int kRegExpExecuteArguments = 9;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100874 __ EnterApiExitFrame(kRegExpExecuteArguments);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100875
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000876 // Argument 9: Pass current isolate address.
877 __ mov(Operand(esp, 8 * kPointerSize),
878 Immediate(ExternalReference::isolate_address(isolate())));
Steve Block44f0eee2011-05-26 01:26:41 +0100879
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000880 // Argument 8: Indicate that this is a direct call from JavaScript.
881 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100882
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000883 // Argument 7: Start (high end) of backtracking stack memory area.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000884 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
885 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000886 __ mov(Operand(esp, 6 * kPointerSize), esi);
887
888 // Argument 6: Set the number of capture registers to zero to force global
889 // regexps to behave as non-global. This does not affect non-global regexps.
890 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100891
892 // Argument 5: static offsets vector buffer.
893 __ mov(Operand(esp, 4 * kPointerSize),
Steve Block44f0eee2011-05-26 01:26:41 +0100894 Immediate(ExternalReference::address_of_static_offsets_vector(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000895 isolate())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100896
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000897 // Argument 2: Previous index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000898 __ SmiUntag(ebx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000899 __ mov(Operand(esp, 1 * kPointerSize), ebx);
900
901 // Argument 1: Original subject string.
902 // The original subject is in the previous stack frame. Therefore we have to
903 // use ebp, which points exactly to one pointer size below the previous esp.
904 // (Because creating a new stack frame pushes the previous ebp onto the stack
905 // and thereby moves up esp by one kPointerSize.)
906 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
907 __ mov(Operand(esp, 0 * kPointerSize), esi);
908
909 // esi: original subject string
910 // eax: underlying subject string
911 // ebx: previous index
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912 // ecx: encoding of subject string (1 if one_byte 0 if two_byte);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000913 // edx: code
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100914 // Argument 4: End of string data
915 // Argument 3: Start of string data
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000916 // Prepare start and end index of the input.
917 // Load the length from the original sliced string if that is the case.
918 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100919 __ add(esi, edi); // Calculate input end wrt offset.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100920 __ SmiUntag(edi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100921 __ add(ebx, edi); // Calculate input start wrt offset.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000922
923 // ebx: start index of the input string
924 // esi: end index of the input string
925 Label setup_two_byte, setup_rest;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100926 __ test(ecx, ecx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000927 __ j(zero, &setup_two_byte, Label::kNear);
928 __ SmiUntag(esi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000929 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100930 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100932 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
Ben Murdoch257744e2011-11-30 15:57:28 +0000933 __ jmp(&setup_rest, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100934
935 __ bind(&setup_two_byte);
936 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000937 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
938 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100939 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
940 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
941 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
942
943 __ bind(&setup_rest);
944
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100945 // Locate the code entry and call it.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100946 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
947 __ call(edx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100948
949 // Drop arguments and come back to JS mode.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000950 __ LeaveApiExitFrame(true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100951
952 // Check the result.
953 Label success;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000954 __ cmp(eax, 1);
955 // We expect exactly one result since we force the called regexp to behave
956 // as non-global.
Ben Murdoch257744e2011-11-30 15:57:28 +0000957 __ j(equal, &success);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100958 Label failure;
959 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
Ben Murdoch257744e2011-11-30 15:57:28 +0000960 __ j(equal, &failure);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100961 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
962 // If not exception it can only be retry. Handle that in the runtime system.
963 __ j(not_equal, &runtime);
964 // Result must now be exception. If there is no pending exception already a
965 // stack overflow (on the backtrack stack) was detected in RegExp code but
966 // haven't created the exception yet. Handle that in the runtime system.
967 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Ben Murdoch589d6972011-11-30 16:04:58 +0000968 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000969 isolate());
970 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100971 __ mov(eax, Operand::StaticVariable(pending_exception));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100972 __ cmp(edx, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100973 __ j(equal, &runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000974
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100975 // For exception, throw the exception again.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000976 __ TailCallRuntime(Runtime::kRegExpExecReThrow);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100977
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100978 __ bind(&failure);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100979 // For failure to match, return null.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100980 __ mov(eax, factory->null_value());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100981 __ ret(4 * kPointerSize);
982
983 // Load RegExp data.
984 __ bind(&success);
985 __ mov(eax, Operand(esp, kJSRegExpOffset));
986 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
987 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
988 // Calculate number of capture registers (number_of_captures + 1) * 2.
989 STATIC_ASSERT(kSmiTag == 0);
990 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100991 __ add(edx, Immediate(2)); // edx was a smi.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100992
993 // edx: Number of capture registers
994 // Load last_match_info which is still known to be a fast case JSArray.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000995 // Check that the fourth object is a JSArray object.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100996 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997 __ JumpIfSmi(eax, &runtime);
998 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
999 __ j(not_equal, &runtime);
1000 // Check that the JSArray is in fast case.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001001 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001002 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
1003 __ cmp(eax, factory->fixed_array_map());
1004 __ j(not_equal, &runtime);
1005 // Check that the last match info has space for the capture registers and the
1006 // additional information.
1007 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
1008 __ SmiUntag(eax);
1009 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
1010 __ cmp(edx, eax);
1011 __ j(greater, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001012
1013 // ebx: last_match_info backing store (FixedArray)
1014 // edx: number of capture registers
1015 // Store the capture count.
1016 __ SmiTag(edx); // Number of capture registers to smi.
1017 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
1018 __ SmiUntag(edx); // Number of capture registers back from smi.
1019 // Store last subject and last input.
1020 __ mov(eax, Operand(esp, kSubjectOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001021 __ mov(ecx, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001022 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001023 __ RecordWriteField(ebx,
1024 RegExpImpl::kLastSubjectOffset,
1025 eax,
1026 edi,
1027 kDontSaveFPRegs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001028 __ mov(eax, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001029 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001030 __ RecordWriteField(ebx,
1031 RegExpImpl::kLastInputOffset,
1032 eax,
1033 edi,
1034 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001035
1036 // Get the static offsets vector filled by the native regexp code.
1037 ExternalReference address_of_static_offsets_vector =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001038 ExternalReference::address_of_static_offsets_vector(isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001039 __ mov(ecx, Immediate(address_of_static_offsets_vector));
1040
1041 // ebx: last_match_info backing store (FixedArray)
1042 // ecx: offsets vector
1043 // edx: number of capture registers
Ben Murdoch257744e2011-11-30 15:57:28 +00001044 Label next_capture, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001045 // Capture register counter starts from number of capture registers and
1046 // counts down until wraping after zero.
1047 __ bind(&next_capture);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001048 __ sub(edx, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00001049 __ j(negative, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001050 // Read the value from the static offsets vector buffer.
1051 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
1052 __ SmiTag(edi);
1053 // Store the smi value in the last match info.
1054 __ mov(FieldOperand(ebx,
1055 edx,
1056 times_pointer_size,
1057 RegExpImpl::kFirstCaptureOffset),
1058 edi);
1059 __ jmp(&next_capture);
1060 __ bind(&done);
1061
1062 // Return last match info.
1063 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
1064 __ ret(4 * kPointerSize);
1065
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001066 // Do the runtime call to execute the regexp.
1067 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001068 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001069
1070 // Deferred code for string handling.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001071 // (6) Long external string? If not, go to (10).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001072 __ bind(&not_seq_nor_cons);
1073 // Compare flags are still set from (3).
1074 __ j(greater, &not_long_external, Label::kNear); // Go to (10).
1075
Ben Murdoch097c5b22016-05-18 11:27:45 +01001076 // (7) External string. Short external strings have been ruled out.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001077 __ bind(&external_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001078 // Reload instance type.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001079 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
1080 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
1081 if (FLAG_debug_code) {
1082 // Assert that we do not have a cons or slice (indirect strings) here.
1083 // Sequential strings have already been ruled out.
Ben Murdochda12d292016-06-02 14:46:10 +01001084 __ test_b(ebx, Immediate(kIsIndirectStringMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001085 __ Assert(zero, kExternalStringExpectedButNotFound);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001086 }
1087 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
1088 // Move the pointer so that offset-wise, it looks like a sequential string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001089 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001090 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
1091 STATIC_ASSERT(kTwoByteStringTag == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001092 // (8) Is the external string one byte? If yes, go to (5).
Ben Murdochda12d292016-06-02 14:46:10 +01001093 __ test_b(ebx, Immediate(kStringEncodingMask));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001094 __ j(not_zero, &seq_one_byte_string); // Go to (5).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001095
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001096 // eax: sequential subject string (or look-alike, external string)
1097 // edx: original subject string
1098 // ecx: RegExp data (FixedArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +01001099 // (9) Two byte sequential. Load regexp code for two byte. Go to (E).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001100 __ bind(&seq_two_byte_string);
1101 // Load previous index and check range before edx is overwritten. We have
1102 // to use edx instead of eax here because it might have been only made to
1103 // look like a sequential string when it actually is an external string.
1104 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
1105 __ JumpIfNotSmi(ebx, &runtime);
1106 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
1107 __ j(above_equal, &runtime);
1108 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
1109 __ Move(ecx, Immediate(0)); // Type is two byte.
1110 __ jmp(&check_code); // Go to (E).
1111
1112 // (10) Not a string or a short external string? If yes, bail out to runtime.
1113 __ bind(&not_long_external);
1114 // Catch non-string subject or short external string.
1115 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1116 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
1117 __ j(not_zero, &runtime);
1118
Ben Murdoch097c5b22016-05-18 11:27:45 +01001119 // (11) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001120 // Load offset into edi and replace subject string with parent.
1121 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
1122 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001123 __ jmp(&check_underlying); // Go to (1).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001124#endif // V8_INTERPRETED_REGEXP
1125}
1126
1127
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001128static int NegativeComparisonResult(Condition cc) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001129 DCHECK(cc != equal);
1130 DCHECK((cc == less) || (cc == less_equal)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001131 || (cc == greater) || (cc == greater_equal));
1132 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
1133}
1134
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001135
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001136static void CheckInputType(MacroAssembler* masm, Register input,
1137 CompareICState::State expected, Label* fail) {
1138 Label ok;
1139 if (expected == CompareICState::SMI) {
1140 __ JumpIfNotSmi(input, fail);
1141 } else if (expected == CompareICState::NUMBER) {
1142 __ JumpIfSmi(input, &ok);
1143 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
1144 Immediate(masm->isolate()->factory()->heap_number_map()));
1145 __ j(not_equal, fail);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001146 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001147 // We could be strict about internalized/non-internalized here, but as long as
1148 // hydrogen doesn't care, the stub doesn't have to care either.
1149 __ bind(&ok);
1150}
1151
1152
1153static void BranchIfNotInternalizedString(MacroAssembler* masm,
1154 Label* label,
1155 Register object,
1156 Register scratch) {
1157 __ JumpIfSmi(object, label);
1158 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
1159 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
1160 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
1161 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
1162 __ j(not_zero, label);
1163}
1164
1165
1166void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001167 Label runtime_call, check_unequal_objects;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001168 Condition cc = GetCondition();
1169
1170 Label miss;
1171 CheckInputType(masm, edx, left(), &miss);
1172 CheckInputType(masm, eax, right(), &miss);
1173
1174 // Compare two smis.
1175 Label non_smi, smi_done;
1176 __ mov(ecx, edx);
1177 __ or_(ecx, eax);
1178 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
1179 __ sub(edx, eax); // Return on the result of the subtraction.
1180 __ j(no_overflow, &smi_done, Label::kNear);
1181 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
1182 __ bind(&smi_done);
1183 __ mov(eax, edx);
1184 __ ret(0);
1185 __ bind(&non_smi);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001186
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001187 // NOTICE! This code is only reached after a smi-fast-case check, so
1188 // it is certain that at least one operand isn't a smi.
1189
1190 // Identical objects can be compared fast, but there are some tricky cases
1191 // for NaN and undefined.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001192 Label generic_heap_number_comparison;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001193 {
1194 Label not_identical;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001195 __ cmp(eax, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001196 __ j(not_equal, &not_identical);
1197
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001198 if (cc != equal) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001199 // Check for undefined. undefined OP undefined is false even though
1200 // undefined == undefined.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001201 __ cmp(edx, isolate()->factory()->undefined_value());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001202 Label check_for_nan;
1203 __ j(not_equal, &check_for_nan, Label::kNear);
1204 __ Move(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
1205 __ ret(0);
1206 __ bind(&check_for_nan);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001207 }
1208
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001209 // Test for NaN. Compare heap numbers in a general way,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001210 // to handle NaNs correctly.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001211 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
1212 Immediate(isolate()->factory()->heap_number_map()));
1213 __ j(equal, &generic_heap_number_comparison, Label::kNear);
1214 if (cc != equal) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001215 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1216 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001217 // Call runtime on identical JSObjects. Otherwise return equal.
Ben Murdochda12d292016-06-02 14:46:10 +01001218 __ cmpb(ecx, Immediate(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001219 __ j(above_equal, &runtime_call, Label::kFar);
1220 // Call runtime on identical symbols since we need to throw a TypeError.
Ben Murdochda12d292016-06-02 14:46:10 +01001221 __ cmpb(ecx, Immediate(SYMBOL_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001222 __ j(equal, &runtime_call, Label::kFar);
1223 // Call runtime on identical SIMD values since we must throw a TypeError.
Ben Murdochda12d292016-06-02 14:46:10 +01001224 __ cmpb(ecx, Immediate(SIMD128_VALUE_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001225 __ j(equal, &runtime_call, Label::kFar);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001226 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001227 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
1228 __ ret(0);
1229
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001230
1231 __ bind(&not_identical);
1232 }
1233
1234 // Strict equality can quickly decide whether objects are equal.
1235 // Non-strict object equality is slower, so it is handled later in the stub.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001236 if (cc == equal && strict()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001237 Label slow; // Fallthrough label.
Ben Murdoch257744e2011-11-30 15:57:28 +00001238 Label not_smis;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001239 // If we're doing a strict equality comparison, we don't have to do
1240 // type conversion, so we generate code to do fast comparison for objects
1241 // and oddballs. Non-smi numbers and strings still go through the usual
1242 // slow-case code.
1243 // If either is a Smi (we know that not both are), then they can only
1244 // be equal if the other is a HeapNumber. If so, use the slow case.
1245 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001246 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001247 __ mov(ecx, Immediate(kSmiTagMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001248 __ and_(ecx, eax);
1249 __ test(ecx, edx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001250 __ j(not_zero, &not_smis, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001251 // One operand is a smi.
1252
1253 // Check whether the non-smi is a heap number.
1254 STATIC_ASSERT(kSmiTagMask == 1);
1255 // ecx still holds eax & kSmiTag, which is either zero or one.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001256 __ sub(ecx, Immediate(0x01));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001257 __ mov(ebx, edx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001258 __ xor_(ebx, eax);
1259 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
1260 __ xor_(ebx, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001261 // if eax was smi, ebx is now edx, else eax.
1262
1263 // Check if the non-smi operand is a heap number.
1264 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001265 Immediate(isolate()->factory()->heap_number_map()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001266 // If heap number, handle it in the slow case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001267 __ j(equal, &slow, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001268 // Return non-equal (ebx is not zero)
1269 __ mov(eax, ebx);
1270 __ ret(0);
1271
1272 __ bind(&not_smis);
1273 // If either operand is a JSObject or an oddball value, then they are not
1274 // equal since their pointers are different
1275 // There is no test for undetectability in strict equality.
1276
1277 // Get the type of the first operand.
1278 // If the first object is a JS object, we have done pointer comparison.
Ben Murdoch257744e2011-11-30 15:57:28 +00001279 Label first_non_object;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001280 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
1281 __ CmpObjectType(eax, FIRST_JS_RECEIVER_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001282 __ j(below, &first_non_object, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001283
1284 // Return non-zero (eax is not zero)
Ben Murdoch257744e2011-11-30 15:57:28 +00001285 Label return_not_equal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001286 STATIC_ASSERT(kHeapObjectTag != 0);
1287 __ bind(&return_not_equal);
1288 __ ret(0);
1289
1290 __ bind(&first_non_object);
1291 // Check for oddballs: true, false, null, undefined.
1292 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1293 __ j(equal, &return_not_equal);
1294
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001295 __ CmpObjectType(edx, FIRST_JS_RECEIVER_TYPE, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001296 __ j(above_equal, &return_not_equal);
1297
1298 // Check for oddballs: true, false, null, undefined.
1299 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1300 __ j(equal, &return_not_equal);
1301
1302 // Fall through to the general case.
1303 __ bind(&slow);
1304 }
1305
1306 // Generate the number comparison code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001307 Label non_number_comparison;
1308 Label unordered;
1309 __ bind(&generic_heap_number_comparison);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001310
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001311 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
1312 __ ucomisd(xmm0, xmm1);
1313 // Don't base result on EFLAGS when a NaN is involved.
1314 __ j(parity_even, &unordered, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001315
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001316 __ mov(eax, 0); // equal
1317 __ mov(ecx, Immediate(Smi::FromInt(1)));
1318 __ cmov(above, eax, ecx);
1319 __ mov(ecx, Immediate(Smi::FromInt(-1)));
1320 __ cmov(below, eax, ecx);
1321 __ ret(0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001322
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001323 // If one of the numbers was NaN, then the result is always false.
1324 // The cc is never not-equal.
1325 __ bind(&unordered);
1326 DCHECK(cc != not_equal);
1327 if (cc == less || cc == less_equal) {
1328 __ mov(eax, Immediate(Smi::FromInt(1)));
1329 } else {
1330 __ mov(eax, Immediate(Smi::FromInt(-1)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001331 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001332 __ ret(0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001333
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001334 // The number comparison code did not provide a valid result.
1335 __ bind(&non_number_comparison);
1336
1337 // Fast negative check for internalized-to-internalized equality.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001338 Label check_for_strings;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001339 if (cc == equal) {
1340 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
1341 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001342
1343 // We've already checked for object identity, so if both operands
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 // are internalized they aren't equal. Register eax already holds a
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001345 // non-zero value, which indicates not equal, so just return.
1346 __ ret(0);
1347 }
1348
1349 __ bind(&check_for_strings);
1350
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001351 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx,
1352 &check_unequal_objects);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001353
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001354 // Inline comparison of one-byte strings.
1355 if (cc == equal) {
1356 StringHelper::GenerateFlatOneByteStringEquals(masm, edx, eax, ecx, ebx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001357 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001358 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
1359 edi);
Ben Murdoch257744e2011-11-30 15:57:28 +00001360 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001361#ifdef DEBUG
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001362 __ Abort(kUnexpectedFallThroughFromStringComparison);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001363#endif
1364
1365 __ bind(&check_unequal_objects);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001366 if (cc == equal && !strict()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001367 // Non-strict equality. Objects are unequal if
1368 // they are both JSObjects and not undetectable,
1369 // and their pointers are different.
Ben Murdochda12d292016-06-02 14:46:10 +01001370 Label return_equal, return_unequal, undetectable;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001371 // At most one is a smi, so we can test for smi by adding the two.
1372 // A smi plus a heap object has the low bit set, a heap object plus
1373 // a heap object has the low bit clear.
1374 STATIC_ASSERT(kSmiTag == 0);
1375 STATIC_ASSERT(kSmiTagMask == 1);
1376 __ lea(ecx, Operand(eax, edx, times_1, 0));
1377 __ test(ecx, Immediate(kSmiTagMask));
Ben Murdochda12d292016-06-02 14:46:10 +01001378 __ j(not_zero, &runtime_call);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001379
1380 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1381 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
1382
1383 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001384 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001385 __ j(not_zero, &undetectable, Label::kNear);
1386 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001387 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001388 __ j(not_zero, &return_unequal, Label::kNear);
1389
1390 __ CmpInstanceType(ebx, FIRST_JS_RECEIVER_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001391 __ j(below, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001392 __ CmpInstanceType(ecx, FIRST_JS_RECEIVER_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001393 __ j(below, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001394
1395 __ bind(&return_unequal);
1396 // Return non-equal by returning the non-zero object pointer in eax.
1397 __ ret(0); // eax, edx were pushed
1398
1399 __ bind(&undetectable);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001400 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001401 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00001402 __ j(zero, &return_unequal, Label::kNear);
Ben Murdochda12d292016-06-02 14:46:10 +01001403
1404 // If both sides are JSReceivers, then the result is false according to
1405 // the HTML specification, which says that only comparisons with null or
1406 // undefined are affected by special casing for document.all.
1407 __ CmpInstanceType(ebx, ODDBALL_TYPE);
1408 __ j(zero, &return_equal, Label::kNear);
1409 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1410 __ j(not_zero, &return_unequal, Label::kNear);
1411
1412 __ bind(&return_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001413 __ Move(eax, Immediate(EQUAL));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001414 __ ret(0); // eax, edx were pushed
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001415 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001416 __ bind(&runtime_call);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001418 if (cc == equal) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001419 {
1420 FrameScope scope(masm, StackFrame::INTERNAL);
1421 __ Push(edx);
1422 __ Push(eax);
1423 __ CallRuntime(strict() ? Runtime::kStrictEqual : Runtime::kEqual);
1424 }
1425 // Turn true into 0 and false into some non-zero value.
1426 STATIC_ASSERT(EQUAL == 0);
1427 __ sub(eax, Immediate(isolate()->factory()->true_value()));
1428 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001429 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001430 // Push arguments below the return address.
1431 __ pop(ecx);
1432 __ push(edx);
1433 __ push(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001434 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001435 __ push(ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001436 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1437 // tagged as a small integer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001438 __ TailCallRuntime(Runtime::kCompare);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001439 }
1440
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001441 __ bind(&miss);
1442 GenerateMiss(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001443}
1444
1445
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001446static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
1447 // eax : number of arguments to the construct function
1448 // ebx : feedback vector
1449 // edx : slot in feedback vector (Smi)
1450 // edi : the function to call
1451
1452 {
1453 FrameScope scope(masm, StackFrame::INTERNAL);
1454
1455 // Number-of-arguments register must be smi-tagged to call out.
1456 __ SmiTag(eax);
1457 __ push(eax);
1458 __ push(edi);
1459 __ push(edx);
1460 __ push(ebx);
1461
1462 __ CallStub(stub);
1463
1464 __ pop(ebx);
1465 __ pop(edx);
1466 __ pop(edi);
1467 __ pop(eax);
1468 __ SmiUntag(eax);
1469 }
1470}
1471
1472
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001473static void GenerateRecordCallTarget(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001474 // Cache the called function in a feedback vector slot. Cache states
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001475 // are uninitialized, monomorphic (indicated by a JSFunction), and
1476 // megamorphic.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001477 // eax : number of arguments to the construct function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001478 // ebx : feedback vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001479 // edx : slot in feedback vector (Smi)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001480 // edi : the function to call
1481 Isolate* isolate = masm->isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001482 Label initialize, done, miss, megamorphic, not_array_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001483
1484 // Load the cache state into ecx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001485 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1486 FixedArray::kHeaderSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001487
1488 // A monomorphic cache hit or an already megamorphic state: invoke the
1489 // function without changing the state.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001490 // We don't know if ecx is a WeakCell or a Symbol, but it's harmless to read
1491 // at this position in a symbol (see static asserts in
1492 // type-feedback-vector.h).
1493 Label check_allocation_site;
1494 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001495 __ j(equal, &done, Label::kFar);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001496 __ CompareRoot(ecx, Heap::kmegamorphic_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001497 __ j(equal, &done, Label::kFar);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001498 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1499 Heap::kWeakCellMapRootIndex);
1500 __ j(not_equal, &check_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001501
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001502 // If the weak cell is cleared, we have a new chance to become monomorphic.
1503 __ JumpIfSmi(FieldOperand(ecx, WeakCell::kValueOffset), &initialize);
1504 __ jmp(&megamorphic);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001505
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001506 __ bind(&check_allocation_site);
1507 // If we came here, we need to see if we are the array function.
1508 // If we didn't have a matching function, and we didn't find the megamorph
1509 // sentinel, then we have in the slot either some other function or an
1510 // AllocationSite.
1511 __ CompareRoot(FieldOperand(ecx, 0), Heap::kAllocationSiteMapRootIndex);
1512 __ j(not_equal, &miss);
1513
1514 // Make sure the function is the Array() function
1515 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1516 __ cmp(edi, ecx);
1517 __ j(not_equal, &megamorphic);
1518 __ jmp(&done, Label::kFar);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001519
1520 __ bind(&miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001521
1522 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1523 // megamorphic.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001524 __ CompareRoot(ecx, Heap::kuninitialized_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001525 __ j(equal, &initialize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001526 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1527 // write-barrier is needed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001528 __ bind(&megamorphic);
1529 __ mov(
1530 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1531 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
1532 __ jmp(&done, Label::kFar);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001533
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001534 // An uninitialized cache is patched with the function or sentinel to
1535 // indicate the ElementsKind if function is the Array constructor.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001536 __ bind(&initialize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001537 // Make sure the function is the Array() function
1538 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1539 __ cmp(edi, ecx);
1540 __ j(not_equal, &not_array_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001541
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001542 // The target function is the Array constructor,
1543 // Create an AllocationSite if we don't already have it, store it in the
1544 // slot.
1545 CreateAllocationSiteStub create_stub(isolate);
1546 CallStubInRecordCallTarget(masm, &create_stub);
1547 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001548
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001549 __ bind(&not_array_function);
1550 CreateWeakCellStub weak_cell_stub(isolate);
1551 CallStubInRecordCallTarget(masm, &weak_cell_stub);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001552 __ bind(&done);
1553}
1554
1555
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001556void CallConstructStub::Generate(MacroAssembler* masm) {
1557 // eax : number of arguments
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001558 // ebx : feedback vector
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001559 // edx : slot in feedback vector (Smi, for RecordCallTarget)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001560 // edi : constructor function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001561
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001562 Label non_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001563 // Check that function is not a smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001564 __ JumpIfSmi(edi, &non_function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001565 // Check that function is a JSFunction.
1566 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001567 __ j(not_equal, &non_function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001568
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001569 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001570
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001571 Label feedback_register_initialized;
1572 // Put the AllocationSite from the feedback vector into ebx, or undefined.
1573 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
1574 FixedArray::kHeaderSize));
1575 Handle<Map> allocation_site_map = isolate()->factory()->allocation_site_map();
1576 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
1577 __ j(equal, &feedback_register_initialized);
1578 __ mov(ebx, isolate()->factory()->undefined_value());
1579 __ bind(&feedback_register_initialized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001580
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001581 __ AssertUndefinedOrAllocationSite(ebx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001582
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001583 // Pass new target to construct stub.
1584 __ mov(edx, edi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001585
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001586 // Tail call to the function-specific construct stub (still in the caller
1587 // context at this point).
1588 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1589 __ mov(ecx, FieldOperand(ecx, SharedFunctionInfo::kConstructStubOffset));
1590 __ lea(ecx, FieldOperand(ecx, Code::kHeaderSize));
1591 __ jmp(ecx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001592
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001593 __ bind(&non_function);
1594 __ mov(edx, edi);
1595 __ Jump(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001596}
1597
1598
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001599void CallICStub::HandleArrayCase(MacroAssembler* masm, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001600 // edi - function
1601 // edx - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001602 // ebx - vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001603 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1604 __ cmp(edi, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001605 __ j(not_equal, miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001606
1607 __ mov(eax, arg_count());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001608 // Reload ecx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001609 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1610 FixedArray::kHeaderSize));
1611
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001612 // Increment the call count for monomorphic function calls.
1613 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1614 FixedArray::kHeaderSize + kPointerSize),
1615 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001616
1617 __ mov(ebx, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001618 __ mov(edx, edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001619 ArrayConstructorStub stub(masm->isolate(), arg_count());
1620 __ TailCallStub(&stub);
1621
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001622 // Unreachable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001623}
1624
1625
1626void CallICStub::Generate(MacroAssembler* masm) {
1627 // edi - function
1628 // edx - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001629 // ebx - vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001630 Isolate* isolate = masm->isolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001631 Label extra_checks_or_miss, call, call_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001632 int argc = arg_count();
1633 ParameterCount actual(argc);
1634
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001635 // The checks. First, does edi match the recorded monomorphic target?
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001636 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1637 FixedArray::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001638
1639 // We don't know that we have a weak cell. We might have a private symbol
1640 // or an AllocationSite, but the memory is safe to examine.
1641 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
1642 // FixedArray.
1643 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
1644 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
1645 // computed, meaning that it can't appear to be a pointer. If the low bit is
1646 // 0, then hash is computed, but the 0 bit prevents the field from appearing
1647 // to be a pointer.
1648 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
1649 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
1650 WeakCell::kValueOffset &&
1651 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
1652
1653 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
1654 __ j(not_equal, &extra_checks_or_miss);
1655
1656 // The compare above could have been a SMI/SMI comparison. Guard against this
1657 // convincing us that we have a monomorphic JSFunction.
1658 __ JumpIfSmi(edi, &extra_checks_or_miss);
1659
1660 // Increment the call count for monomorphic function calls.
1661 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1662 FixedArray::kHeaderSize + kPointerSize),
1663 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
1664
1665 __ bind(&call_function);
1666 __ Set(eax, argc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001667 __ Jump(masm->isolate()->builtins()->CallFunction(convert_mode(),
1668 tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001669 RelocInfo::CODE_TARGET);
1670
1671 __ bind(&extra_checks_or_miss);
1672 Label uninitialized, miss, not_allocation_site;
1673
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001674 __ cmp(ecx, Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001675 __ j(equal, &call);
1676
1677 // Check if we have an allocation site.
1678 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1679 Heap::kAllocationSiteMapRootIndex);
1680 __ j(not_equal, &not_allocation_site);
1681
1682 // We have an allocation site.
1683 HandleArrayCase(masm, &miss);
1684
1685 __ bind(&not_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001686
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001687 // The following cases attempt to handle MISS cases without going to the
1688 // runtime.
1689 if (FLAG_trace_ic) {
1690 __ jmp(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001691 }
1692
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001693 __ cmp(ecx, Immediate(TypeFeedbackVector::UninitializedSentinel(isolate)));
1694 __ j(equal, &uninitialized);
1695
1696 // We are going megamorphic. If the feedback is a JSFunction, it is fine
1697 // to handle it here. More complex cases are dealt with in the runtime.
1698 __ AssertNotSmi(ecx);
1699 __ CmpObjectType(ecx, JS_FUNCTION_TYPE, ecx);
1700 __ j(not_equal, &miss);
1701 __ mov(
1702 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1703 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001704
1705 __ bind(&call);
1706 __ Set(eax, argc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001707 __ Jump(masm->isolate()->builtins()->Call(convert_mode(), tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001708 RelocInfo::CODE_TARGET);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001709
1710 __ bind(&uninitialized);
1711
1712 // We are going monomorphic, provided we actually have a JSFunction.
1713 __ JumpIfSmi(edi, &miss);
1714
1715 // Goto miss case if we do not have a function.
1716 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1717 __ j(not_equal, &miss);
1718
1719 // Make sure the function is not the Array() function, which requires special
1720 // behavior on MISS.
1721 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1722 __ cmp(edi, ecx);
1723 __ j(equal, &miss);
1724
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001725 // Make sure the function belongs to the same native context.
1726 __ mov(ecx, FieldOperand(edi, JSFunction::kContextOffset));
1727 __ mov(ecx, ContextOperand(ecx, Context::NATIVE_CONTEXT_INDEX));
1728 __ cmp(ecx, NativeContextOperand());
1729 __ j(not_equal, &miss);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001730
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001731 // Initialize the call counter.
1732 __ mov(FieldOperand(ebx, edx, times_half_pointer_size,
1733 FixedArray::kHeaderSize + kPointerSize),
1734 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001735
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001736 // Store the function. Use a stub since we need a frame for allocation.
1737 // ebx - vector
1738 // edx - slot
1739 // edi - function
1740 {
1741 FrameScope scope(masm, StackFrame::INTERNAL);
1742 CreateWeakCellStub create_stub(isolate);
1743 __ push(edi);
1744 __ CallStub(&create_stub);
1745 __ pop(edi);
1746 }
1747
1748 __ jmp(&call_function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001749
1750 // We are here because tracing is on or we encountered a MISS case we can't
1751 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001752 __ bind(&miss);
1753 GenerateMiss(masm);
1754
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001755 __ jmp(&call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001756
1757 // Unreachable
1758 __ int3();
1759}
1760
1761
1762void CallICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001763 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001764
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001765 // Push the function and feedback info.
1766 __ push(edi);
1767 __ push(ebx);
1768 __ push(edx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001769
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001770 // Call the entry.
1771 __ CallRuntime(Runtime::kCallIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001772
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001773 // Move result to edi and exit the internal frame.
1774 __ mov(edi, eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001775}
1776
1777
Steve Block44f0eee2011-05-26 01:26:41 +01001778bool CEntryStub::NeedsImmovableCode() {
1779 return false;
1780}
1781
1782
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001783void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
1784 CEntryStub::GenerateAheadOfTime(isolate);
1785 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
1786 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001787 // It is important that the store buffer overflow stubs are generated first.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001788 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
1789 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001790 CreateWeakCellStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001791 BinaryOpICStub::GenerateAheadOfTime(isolate);
1792 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001793 StoreFastElementStub::GenerateAheadOfTime(isolate);
1794 TypeofStub::GenerateAheadOfTime(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001795}
1796
1797
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001798void CodeStub::GenerateFPStubs(Isolate* isolate) {
1799 // Generate if not already in cache.
1800 CEntryStub(isolate, 1, kSaveFPRegs).GetCode();
1801 isolate->set_fp_stubs_generated(true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001802}
1803
1804
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001805void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1806 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1807 stub.GetCode();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001808}
1809
1810
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001811void CEntryStub::Generate(MacroAssembler* masm) {
1812 // eax: number of arguments including receiver
1813 // ebx: pointer to C function (C callee-saved)
1814 // ebp: frame pointer (restored after C call)
1815 // esp: stack pointer (restored after C call)
1816 // esi: current context (C callee-saved)
1817 // edi: JS function of the caller (C callee-saved)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001818 //
1819 // If argv_in_register():
1820 // ecx: pointer to the first argument
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001821
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001822 ProfileEntryHookStub::MaybeCallEntryHook(masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001823
Ben Murdoch097c5b22016-05-18 11:27:45 +01001824 // Reserve space on the stack for the three arguments passed to the call. If
1825 // result size is greater than can be returned in registers, also reserve
1826 // space for the hidden argument for the result location, and space for the
1827 // result itself.
1828 int arg_stack_space = result_size() < 3 ? 3 : 4 + result_size();
1829
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001830 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001831 if (argv_in_register()) {
1832 DCHECK(!save_doubles());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001833 __ EnterApiExitFrame(arg_stack_space);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001834
1835 // Move argc and argv into the correct registers.
1836 __ mov(esi, ecx);
1837 __ mov(edi, eax);
1838 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001839 __ EnterExitFrame(arg_stack_space, save_doubles());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001840 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001841
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001842 // ebx: pointer to C function (C callee-saved)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001843 // ebp: frame pointer (restored after C call)
1844 // esp: stack pointer (restored after C call)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001845 // edi: number of arguments including receiver (C callee-saved)
1846 // esi: pointer to the first argument (C callee-saved)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001847
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001848 // Result returned in eax, or eax+edx if result size is 2.
1849
1850 // Check stack alignment.
1851 if (FLAG_debug_code) {
1852 __ CheckStackAlignment();
1853 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001854 // Call C function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001855 if (result_size() <= 2) {
1856 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
1857 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
1858 __ mov(Operand(esp, 2 * kPointerSize),
1859 Immediate(ExternalReference::isolate_address(isolate())));
1860 } else {
1861 DCHECK_EQ(3, result_size());
1862 // Pass a pointer to the result location as the first argument.
1863 __ lea(eax, Operand(esp, 4 * kPointerSize));
1864 __ mov(Operand(esp, 0 * kPointerSize), eax);
1865 __ mov(Operand(esp, 1 * kPointerSize), edi); // argc.
1866 __ mov(Operand(esp, 2 * kPointerSize), esi); // argv.
1867 __ mov(Operand(esp, 3 * kPointerSize),
1868 Immediate(ExternalReference::isolate_address(isolate())));
1869 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001870 __ call(ebx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001871
1872 if (result_size() > 2) {
1873 DCHECK_EQ(3, result_size());
1874#ifndef _WIN32
1875 // Restore the "hidden" argument on the stack which was popped by caller.
1876 __ sub(esp, Immediate(kPointerSize));
1877#endif
1878 // Read result values stored on stack. Result is stored above the arguments.
1879 __ mov(kReturnRegister0, Operand(esp, 4 * kPointerSize));
1880 __ mov(kReturnRegister1, Operand(esp, 5 * kPointerSize));
1881 __ mov(kReturnRegister2, Operand(esp, 6 * kPointerSize));
1882 }
1883 // Result is in eax, edx:eax or edi:edx:eax - do not destroy these registers!
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001884
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001885 // Check result for exception sentinel.
1886 Label exception_returned;
1887 __ cmp(eax, isolate()->factory()->exception());
1888 __ j(equal, &exception_returned);
1889
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001890 // Check that there is no pending exception, otherwise we
1891 // should have returned the exception sentinel.
1892 if (FLAG_debug_code) {
1893 __ push(edx);
1894 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1895 Label okay;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001896 ExternalReference pending_exception_address(
1897 Isolate::kPendingExceptionAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001898 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
1899 // Cannot use check here as it attempts to generate call into runtime.
1900 __ j(equal, &okay, Label::kNear);
1901 __ int3();
1902 __ bind(&okay);
1903 __ pop(edx);
1904 }
1905
1906 // Exit the JavaScript to C++ exit frame.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001907 __ LeaveExitFrame(save_doubles(), !argv_in_register());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001908 __ ret(0);
1909
1910 // Handling of exception.
1911 __ bind(&exception_returned);
1912
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001913 ExternalReference pending_handler_context_address(
1914 Isolate::kPendingHandlerContextAddress, isolate());
1915 ExternalReference pending_handler_code_address(
1916 Isolate::kPendingHandlerCodeAddress, isolate());
1917 ExternalReference pending_handler_offset_address(
1918 Isolate::kPendingHandlerOffsetAddress, isolate());
1919 ExternalReference pending_handler_fp_address(
1920 Isolate::kPendingHandlerFPAddress, isolate());
1921 ExternalReference pending_handler_sp_address(
1922 Isolate::kPendingHandlerSPAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001923
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001924 // Ask the runtime for help to determine the handler. This will set eax to
1925 // contain the current pending exception, don't clobber it.
1926 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
1927 isolate());
1928 {
1929 FrameScope scope(masm, StackFrame::MANUAL);
1930 __ PrepareCallCFunction(3, eax);
1931 __ mov(Operand(esp, 0 * kPointerSize), Immediate(0)); // argc.
1932 __ mov(Operand(esp, 1 * kPointerSize), Immediate(0)); // argv.
1933 __ mov(Operand(esp, 2 * kPointerSize),
1934 Immediate(ExternalReference::isolate_address(isolate())));
1935 __ CallCFunction(find_handler, 3);
1936 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001937
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001938 // Retrieve the handler context, SP and FP.
1939 __ mov(esi, Operand::StaticVariable(pending_handler_context_address));
1940 __ mov(esp, Operand::StaticVariable(pending_handler_sp_address));
1941 __ mov(ebp, Operand::StaticVariable(pending_handler_fp_address));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001942
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001943 // If the handler is a JS frame, restore the context to the frame. Note that
1944 // the context will be set to (esi == 0) for non-JS frames.
1945 Label skip;
1946 __ test(esi, esi);
1947 __ j(zero, &skip, Label::kNear);
1948 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
1949 __ bind(&skip);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001950
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001951 // Compute the handler entry address and jump to it.
1952 __ mov(edi, Operand::StaticVariable(pending_handler_code_address));
1953 __ mov(edx, Operand::StaticVariable(pending_handler_offset_address));
1954 __ lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
1955 __ jmp(edi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001956}
1957
1958
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001959void JSEntryStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001960 Label invoke, handler_entry, exit;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001961 Label not_outermost_js, not_outermost_js_2;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001962
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001963 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1964
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001965 // Set up frame.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001966 __ push(ebp);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001967 __ mov(ebp, esp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001968
1969 // Push marker in two places.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001970 int marker = type();
Ben Murdochda12d292016-06-02 14:46:10 +01001971 __ push(Immediate(Smi::FromInt(marker))); // marker
1972 ExternalReference context_address(Isolate::kContextAddress, isolate());
1973 __ push(Operand::StaticVariable(context_address)); // context
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001974 // Save callee-saved registers (C calling conventions).
1975 __ push(edi);
1976 __ push(esi);
1977 __ push(ebx);
1978
1979 // Save copies of the top frame descriptor on the stack.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001980 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001981 __ push(Operand::StaticVariable(c_entry_fp));
1982
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001983 // If this is the outermost JS call, set js_entry_sp value.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001984 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001985 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001986 __ j(not_equal, &not_outermost_js, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001987 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
Steve Block053d10c2011-06-13 19:13:29 +01001988 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001989 __ jmp(&invoke, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001990 __ bind(&not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01001991 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001992
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001993 // Jump to a faked try block that does the invoke, with a faked catch
1994 // block that sets the pending exception.
1995 __ jmp(&invoke);
1996 __ bind(&handler_entry);
1997 handler_offset_ = handler_entry.pos();
1998 // Caught exception: Store result (exception) in the pending exception
1999 // field in the JSEnv and return a failure sentinel.
Ben Murdoch589d6972011-11-30 16:04:58 +00002000 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002001 isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002002 __ mov(Operand::StaticVariable(pending_exception), eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002003 __ mov(eax, Immediate(isolate()->factory()->exception()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002004 __ jmp(&exit);
2005
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002006 // Invoke: Link this frame into the handler chain.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002007 __ bind(&invoke);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002008 __ PushStackHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002009
2010 // Clear any pending exceptions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002011 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002012 __ mov(Operand::StaticVariable(pending_exception), edx);
2013
2014 // Fake a receiver (NULL).
2015 __ push(Immediate(0)); // receiver
2016
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002017 // Invoke the function by calling through JS entry trampoline builtin and
2018 // pop the faked function when we return. Notice that we cannot store a
2019 // reference to the trampoline code directly in this stub, because the
2020 // builtin stubs may not have been generated yet.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002021 if (type() == StackFrame::ENTRY_CONSTRUCT) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002022 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002023 isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002024 __ mov(edx, Immediate(construct_entry));
2025 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002026 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002027 __ mov(edx, Immediate(entry));
2028 }
2029 __ mov(edx, Operand(edx, 0)); // deref address
2030 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002031 __ call(edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002032
2033 // Unlink this frame from the handler chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002034 __ PopStackHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002035
Steve Block053d10c2011-06-13 19:13:29 +01002036 __ bind(&exit);
Steve Block053d10c2011-06-13 19:13:29 +01002037 // Check if the current stack frame is marked as the outermost JS frame.
2038 __ pop(ebx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002039 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002040 __ j(not_equal, &not_outermost_js_2);
2041 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
2042 __ bind(&not_outermost_js_2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002043
2044 // Restore the top frame descriptor from the stack.
Steve Block44f0eee2011-05-26 01:26:41 +01002045 __ pop(Operand::StaticVariable(ExternalReference(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002046 Isolate::kCEntryFPAddress, isolate())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002047
2048 // Restore callee-saved registers (C calling conventions).
2049 __ pop(ebx);
2050 __ pop(esi);
2051 __ pop(edi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002052 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002053
2054 // Restore frame pointer and return.
2055 __ pop(ebp);
2056 __ ret(0);
2057}
2058
2059
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002060void InstanceOfStub::Generate(MacroAssembler* masm) {
2061 Register const object = edx; // Object (lhs).
2062 Register const function = eax; // Function (rhs).
2063 Register const object_map = ecx; // Map of {object}.
2064 Register const function_map = ebx; // Map of {function}.
2065 Register const function_prototype = function_map; // Prototype of {function}.
2066 Register const scratch = edi;
Ben Murdoch086aeea2011-05-13 15:57:08 +01002067
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002068 DCHECK(object.is(InstanceOfDescriptor::LeftRegister()));
2069 DCHECK(function.is(InstanceOfDescriptor::RightRegister()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002070
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002071 // Check if {object} is a smi.
2072 Label object_is_smi;
2073 __ JumpIfSmi(object, &object_is_smi, Label::kNear);
Ben Murdoch086aeea2011-05-13 15:57:08 +01002074
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002075 // Lookup the {function} and the {object} map in the global instanceof cache.
2076 // Note: This is safe because we clear the global instanceof cache whenever
2077 // we change the prototype of any object.
2078 Label fast_case, slow_case;
2079 __ mov(object_map, FieldOperand(object, HeapObject::kMapOffset));
2080 __ CompareRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2081 __ j(not_equal, &fast_case, Label::kNear);
2082 __ CompareRoot(object_map, scratch, Heap::kInstanceofCacheMapRootIndex);
2083 __ j(not_equal, &fast_case, Label::kNear);
2084 __ LoadRoot(eax, Heap::kInstanceofCacheAnswerRootIndex);
2085 __ ret(0);
Ben Murdoch086aeea2011-05-13 15:57:08 +01002086
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002087 // If {object} is a smi we can safely return false if {function} is a JS
2088 // function, otherwise we have to miss to the runtime and throw an exception.
2089 __ bind(&object_is_smi);
2090 __ JumpIfSmi(function, &slow_case);
2091 __ CmpObjectType(function, JS_FUNCTION_TYPE, function_map);
2092 __ j(not_equal, &slow_case);
2093 __ LoadRoot(eax, Heap::kFalseValueRootIndex);
2094 __ ret(0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002095
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002096 // Fast-case: The {function} must be a valid JSFunction.
2097 __ bind(&fast_case);
2098 __ JumpIfSmi(function, &slow_case);
2099 __ CmpObjectType(function, JS_FUNCTION_TYPE, function_map);
2100 __ j(not_equal, &slow_case);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002101
Ben Murdochda12d292016-06-02 14:46:10 +01002102 // Go to the runtime if the function is not a constructor.
2103 __ test_b(FieldOperand(function_map, Map::kBitFieldOffset),
2104 Immediate(1 << Map::kIsConstructor));
2105 __ j(zero, &slow_case);
2106
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002107 // Ensure that {function} has an instance prototype.
2108 __ test_b(FieldOperand(function_map, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01002109 Immediate(1 << Map::kHasNonInstancePrototype));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002110 __ j(not_zero, &slow_case);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002111
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002112 // Get the "prototype" (or initial map) of the {function}.
2113 __ mov(function_prototype,
2114 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
2115 __ AssertNotSmi(function_prototype);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002116
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002117 // Resolve the prototype if the {function} has an initial map. Afterwards the
2118 // {function_prototype} will be either the JSReceiver prototype object or the
2119 // hole value, which means that no instances of the {function} were created so
2120 // far and hence we should return false.
2121 Label function_prototype_valid;
2122 Register const function_prototype_map = scratch;
2123 __ CmpObjectType(function_prototype, MAP_TYPE, function_prototype_map);
2124 __ j(not_equal, &function_prototype_valid, Label::kNear);
2125 __ mov(function_prototype,
2126 FieldOperand(function_prototype, Map::kPrototypeOffset));
2127 __ bind(&function_prototype_valid);
2128 __ AssertNotSmi(function_prototype);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002129
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002130 // Update the global instanceof cache with the current {object} map and
2131 // {function}. The cached answer will be set when it is known below.
2132 __ StoreRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2133 __ StoreRoot(object_map, scratch, Heap::kInstanceofCacheMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002134
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002135 // Loop through the prototype chain looking for the {function} prototype.
2136 // Assume true, and change to false if not found.
2137 Label done, loop, fast_runtime_fallback;
2138 __ mov(eax, isolate()->factory()->true_value());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002139 __ bind(&loop);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002140
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002141 // Check if the object needs to be access checked.
2142 __ test_b(FieldOperand(object_map, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01002143 Immediate(1 << Map::kIsAccessCheckNeeded));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002144 __ j(not_zero, &fast_runtime_fallback, Label::kNear);
2145 // Check if the current object is a Proxy.
2146 __ CmpInstanceType(object_map, JS_PROXY_TYPE);
2147 __ j(equal, &fast_runtime_fallback, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002148
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002149 __ mov(object, FieldOperand(object_map, Map::kPrototypeOffset));
2150 __ cmp(object, function_prototype);
2151 __ j(equal, &done, Label::kNear);
2152 __ mov(object_map, FieldOperand(object, HeapObject::kMapOffset));
2153 __ cmp(object, isolate()->factory()->null_value());
2154 __ j(not_equal, &loop);
2155 __ mov(eax, isolate()->factory()->false_value());
Ben Murdochb0fe1622011-05-05 13:52:32 +01002156
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002157 __ bind(&done);
2158 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
2159 __ ret(0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002160
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002161 // Found Proxy or access check needed: Call the runtime.
2162 __ bind(&fast_runtime_fallback);
2163 __ PopReturnAddressTo(scratch);
2164 __ Push(object);
2165 __ Push(function_prototype);
2166 __ PushReturnAddressFrom(scratch);
2167 // Invalidate the instanceof cache.
2168 __ Move(eax, Immediate(Smi::FromInt(0)));
2169 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheFunctionRootIndex);
2170 __ TailCallRuntime(Runtime::kHasInPrototypeChain);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002171
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002172 // Slow-case: Call the %InstanceOf runtime function.
2173 __ bind(&slow_case);
2174 __ PopReturnAddressTo(scratch);
2175 __ Push(object);
2176 __ Push(function);
2177 __ PushReturnAddressFrom(scratch);
Ben Murdochda12d292016-06-02 14:46:10 +01002178 __ TailCallRuntime(is_es6_instanceof() ? Runtime::kOrdinaryHasInstance
2179 : Runtime::kInstanceOf);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002180}
2181
2182
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002183// -------------------------------------------------------------------------
2184// StringCharCodeAtGenerator
2185
2186void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002187 // If the receiver is a smi trigger the non-string case.
2188 STATIC_ASSERT(kSmiTag == 0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002189 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2190 __ JumpIfSmi(object_, receiver_not_string_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002191
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002192 // Fetch the instance type of the receiver into result register.
2193 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
2194 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2195 // If the receiver is not a string trigger the non-string case.
2196 __ test(result_, Immediate(kIsNotStringMask));
2197 __ j(not_zero, receiver_not_string_);
2198 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002199
2200 // If the index is non-smi trigger the non-smi case.
2201 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002202 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002203 __ bind(&got_smi_index_);
2204
2205 // Check for index out of range.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002206 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002207 __ j(above_equal, index_out_of_range_);
2208
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002209 __ SmiUntag(index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002210
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002211 Factory* factory = masm->isolate()->factory();
2212 StringCharLoadGenerator::Generate(
2213 masm, factory, object_, index_, result_, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002214
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002215 __ SmiTag(result_);
2216 __ bind(&exit_);
2217}
2218
2219
2220void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002221 MacroAssembler* masm, EmbedMode embed_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002222 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002223 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002224
2225 // Index is not a smi.
2226 __ bind(&index_not_smi_);
2227 // If index is a heap number, try converting it to an integer.
Steve Block44f0eee2011-05-26 01:26:41 +01002228 __ CheckMap(index_,
2229 masm->isolate()->factory()->heap_number_map(),
2230 index_not_number_,
Ben Murdoch257744e2011-11-30 15:57:28 +00002231 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002232 call_helper.BeforeCall(masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002233 if (embed_mode == PART_OF_IC_HANDLER) {
2234 __ push(LoadWithVectorDescriptor::VectorRegister());
2235 __ push(LoadDescriptor::SlotRegister());
2236 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002237 __ push(object_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002238 __ push(index_); // Consumed by runtime conversion function.
2239 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002240 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002241 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002242 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002243 // NumberToSmi discards numbers that are not exact integers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002244 __ CallRuntime(Runtime::kNumberToSmi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002245 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002246 if (!index_.is(eax)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002247 // Save the conversion result before the pop instructions below
2248 // have a chance to overwrite it.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002249 __ mov(index_, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002250 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002251 __ pop(object_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002252 if (embed_mode == PART_OF_IC_HANDLER) {
2253 __ pop(LoadDescriptor::SlotRegister());
2254 __ pop(LoadWithVectorDescriptor::VectorRegister());
2255 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002256 // Reload the instance type.
2257 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
2258 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
2259 call_helper.AfterCall(masm);
2260 // If index is still not a smi, it must be out of range.
2261 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002262 __ JumpIfNotSmi(index_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002263 // Otherwise, return to the fast path.
2264 __ jmp(&got_smi_index_);
2265
2266 // Call runtime. We get here when the receiver is a string and the
2267 // index is a number, but the code of getting the actual character
2268 // is too complex (e.g., when the string needs to be flattened).
2269 __ bind(&call_runtime_);
2270 call_helper.BeforeCall(masm);
2271 __ push(object_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002272 __ SmiTag(index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002273 __ push(index_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002274 __ CallRuntime(Runtime::kStringCharCodeAtRT);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002275 if (!result_.is(eax)) {
2276 __ mov(result_, eax);
2277 }
2278 call_helper.AfterCall(masm);
2279 __ jmp(&exit_);
2280
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002281 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002282}
2283
2284
2285// -------------------------------------------------------------------------
2286// StringCharFromCodeGenerator
2287
2288void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
2289 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2290 STATIC_ASSERT(kSmiTag == 0);
2291 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002292 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
2293 __ test(code_, Immediate(kSmiTagMask |
2294 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002295 __ j(not_zero, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002296
Steve Block44f0eee2011-05-26 01:26:41 +01002297 Factory* factory = masm->isolate()->factory();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002298 __ Move(result_, Immediate(factory->single_character_string_cache()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002299 STATIC_ASSERT(kSmiTag == 0);
2300 STATIC_ASSERT(kSmiTagSize == 1);
2301 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002302 // At this point code register contains smi tagged one byte char code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002303 __ mov(result_, FieldOperand(result_,
2304 code_, times_half_pointer_size,
2305 FixedArray::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01002306 __ cmp(result_, factory->undefined_value());
Ben Murdoch257744e2011-11-30 15:57:28 +00002307 __ j(equal, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002308 __ bind(&exit_);
2309}
2310
2311
2312void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002313 MacroAssembler* masm,
2314 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002315 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002316
2317 __ bind(&slow_case_);
2318 call_helper.BeforeCall(masm);
2319 __ push(code_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002320 __ CallRuntime(Runtime::kStringCharFromCode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002321 if (!result_.is(eax)) {
2322 __ mov(result_, eax);
2323 }
2324 call_helper.AfterCall(masm);
2325 __ jmp(&exit_);
2326
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002327 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002328}
2329
2330
2331void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2332 Register dest,
2333 Register src,
2334 Register count,
2335 Register scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002336 String::Encoding encoding) {
2337 DCHECK(!scratch.is(dest));
2338 DCHECK(!scratch.is(src));
2339 DCHECK(!scratch.is(count));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002340
2341 // Nothing to do for zero characters.
2342 Label done;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002343 __ test(count, count);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002344 __ j(zero, &done);
2345
2346 // Make count the number of bytes to copy.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002347 if (encoding == String::TWO_BYTE_ENCODING) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002348 __ shl(count, 1);
2349 }
2350
Ben Murdoch257744e2011-11-30 15:57:28 +00002351 Label loop;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002352 __ bind(&loop);
2353 __ mov_b(scratch, Operand(src, 0));
2354 __ mov_b(Operand(dest, 0), scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002355 __ inc(src);
2356 __ inc(dest);
2357 __ dec(count);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002358 __ j(not_zero, &loop);
2359
2360 __ bind(&done);
2361}
2362
2363
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002364void SubStringStub::Generate(MacroAssembler* masm) {
2365 Label runtime;
2366
2367 // Stack frame on entry.
2368 // esp[0]: return address
2369 // esp[4]: to
2370 // esp[8]: from
2371 // esp[12]: string
2372
2373 // Make sure first argument is a string.
2374 __ mov(eax, Operand(esp, 3 * kPointerSize));
2375 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002376 __ JumpIfSmi(eax, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002377 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
2378 __ j(NegateCondition(is_string), &runtime);
2379
2380 // eax: string
2381 // ebx: instance type
2382
2383 // Calculate length of sub string using the smi values.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002384 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002385 __ JumpIfNotSmi(ecx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002386 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002387 __ JumpIfNotSmi(edx, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002388 __ sub(ecx, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002389 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002390 Label not_original_string;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002391 // Shorter than original string's length: an actual substring.
2392 __ j(below, &not_original_string, Label::kNear);
2393 // Longer than original string's length or negative: unsafe arguments.
2394 __ j(above, &runtime);
2395 // Return original string.
2396 Counters* counters = isolate()->counters();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002397 __ IncrementCounter(counters->sub_string_native(), 1);
2398 __ ret(3 * kPointerSize);
2399 __ bind(&not_original_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002400
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002401 Label single_char;
2402 __ cmp(ecx, Immediate(Smi::FromInt(1)));
2403 __ j(equal, &single_char);
2404
Ben Murdochc7cc0282012-03-05 14:35:55 +00002405 // eax: string
2406 // ebx: instance type
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002407 // ecx: sub string length (smi)
Ben Murdochc7cc0282012-03-05 14:35:55 +00002408 // edx: from index (smi)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002409 // Deal with different string types: update the index if necessary
2410 // and put the underlying string into edi.
2411 Label underlying_unpacked, sliced_string, seq_or_external_string;
2412 // If the string is not indirect, it can only be sequential or external.
2413 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2414 STATIC_ASSERT(kIsIndirectStringMask != 0);
2415 __ test(ebx, Immediate(kIsIndirectStringMask));
2416 __ j(zero, &seq_or_external_string, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002418 Factory* factory = isolate()->factory();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002419 __ test(ebx, Immediate(kSlicedNotConsMask));
2420 __ j(not_zero, &sliced_string, Label::kNear);
2421 // Cons string. Check whether it is flat, then fetch first part.
2422 // Flat cons strings have an empty second part.
2423 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
2424 factory->empty_string());
2425 __ j(not_equal, &runtime);
2426 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
2427 // Update instance type.
2428 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002429 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002430 __ jmp(&underlying_unpacked, Label::kNear);
2431
2432 __ bind(&sliced_string);
2433 // Sliced string. Fetch parent and adjust start index by offset.
2434 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
2435 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
2436 // Update instance type.
2437 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2438 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2439 __ jmp(&underlying_unpacked, Label::kNear);
2440
2441 __ bind(&seq_or_external_string);
2442 // Sequential or external string. Just move string to the expected register.
2443 __ mov(edi, eax);
2444
2445 __ bind(&underlying_unpacked);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002446
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002447 if (FLAG_string_slices) {
2448 Label copy_routine;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002449 // edi: underlying subject string
2450 // ebx: instance type of underlying subject string
2451 // edx: adjusted start index (smi)
2452 // ecx: length (smi)
2453 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002454 // Short slice. Copy instead of slicing.
2455 __ j(less, &copy_routine);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002456 // Allocate new sliced string. At this point we do not reload the instance
2457 // type including the string encoding because we simply rely on the info
2458 // provided by the original string. It does not matter if the original
2459 // string's encoding is wrong because we always have to recheck encoding of
2460 // the newly created string's parent anyways due to externalized strings.
2461 Label two_byte_slice, set_slice_header;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002462 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00002463 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2464 __ test(ebx, Immediate(kStringEncodingMask));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002465 __ j(zero, &two_byte_slice, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002466 __ AllocateOneByteSlicedString(eax, ebx, no_reg, &runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002467 __ jmp(&set_slice_header, Label::kNear);
2468 __ bind(&two_byte_slice);
Ben Murdoch589d6972011-11-30 16:04:58 +00002469 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002470 __ bind(&set_slice_header);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002471 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002472 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
2473 Immediate(String::kEmptyHashField));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002474 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
2475 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
2476 __ IncrementCounter(counters->sub_string_native(), 1);
2477 __ ret(3 * kPointerSize);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002478
2479 __ bind(&copy_routine);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002480 }
2481
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002482 // edi: underlying subject string
2483 // ebx: instance type of underlying subject string
2484 // edx: adjusted start index (smi)
2485 // ecx: length (smi)
2486 // The subject string can only be external or sequential string of either
2487 // encoding at this point.
2488 Label two_byte_sequential, runtime_drop_two, sequential_string;
2489 STATIC_ASSERT(kExternalStringTag != 0);
2490 STATIC_ASSERT(kSeqStringTag == 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002491 __ test_b(ebx, Immediate(kExternalStringTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002492 __ j(zero, &sequential_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002493
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002494 // Handle external string.
2495 // Rule out short external strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002496 STATIC_ASSERT(kShortExternalStringTag != 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002497 __ test_b(ebx, Immediate(kShortExternalStringMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002498 __ j(not_zero, &runtime);
2499 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
2500 // Move the pointer so that offset-wise, it looks like a sequential string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002501 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002502 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2503
2504 __ bind(&sequential_string);
2505 // Stash away (adjusted) index and (underlying) string.
2506 __ push(edx);
2507 __ push(edi);
2508 __ SmiUntag(ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002509 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002510 __ test_b(ebx, Immediate(kStringEncodingMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002511 __ j(zero, &two_byte_sequential);
2512
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002513 // Sequential one byte string. Allocate the result.
2514 __ AllocateOneByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002515
2516 // eax: result string
2517 // ecx: result string length
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002518 // Locate first character of result.
2519 __ mov(edi, eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002520 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002521 // Load string argument and locate character of sub string start.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002522 __ pop(edx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002523 __ pop(ebx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002524 __ SmiUntag(ebx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002525 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqOneByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002526
2527 // eax: result string
2528 // ecx: result length
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002529 // edi: first character of result
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002530 // edx: character of sub string start
2531 StringHelper::GenerateCopyCharacters(
2532 masm, edi, edx, ecx, ebx, String::ONE_BYTE_ENCODING);
Steve Block44f0eee2011-05-26 01:26:41 +01002533 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002534 __ ret(3 * kPointerSize);
2535
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002536 __ bind(&two_byte_sequential);
2537 // Sequential two-byte string. Allocate the result.
2538 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002539
2540 // eax: result string
2541 // ecx: result string length
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002542 // Locate first character of result.
2543 __ mov(edi, eax);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002544 __ add(edi,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002545 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2546 // Load string argument and locate character of sub string start.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002547 __ pop(edx);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002548 __ pop(ebx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002549 // As from is a smi it is 2 times the value which matches the size of a two
2550 // byte character.
2551 STATIC_ASSERT(kSmiTag == 0);
2552 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002553 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqTwoByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002554
2555 // eax: result string
2556 // ecx: result length
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002557 // edi: first character of result
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002558 // edx: character of sub string start
2559 StringHelper::GenerateCopyCharacters(
2560 masm, edi, edx, ecx, ebx, String::TWO_BYTE_ENCODING);
Steve Block44f0eee2011-05-26 01:26:41 +01002561 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002562 __ ret(3 * kPointerSize);
2563
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002564 // Drop pushed values on the stack before tail call.
2565 __ bind(&runtime_drop_two);
2566 __ Drop(2);
2567
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002568 // Just jump to runtime to create the sub string.
2569 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002570 __ TailCallRuntime(Runtime::kSubString);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002571
2572 __ bind(&single_char);
2573 // eax: string
2574 // ebx: instance type
2575 // ecx: sub string length (smi)
2576 // edx: from index (smi)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002577 StringCharAtGenerator generator(eax, edx, ecx, eax, &runtime, &runtime,
2578 &runtime, STRING_INDEX_IS_NUMBER,
2579 RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002580 generator.GenerateFast(masm);
2581 __ ret(3 * kPointerSize);
2582 generator.SkipSlow(masm, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002583}
2584
2585
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002586void ToNumberStub::Generate(MacroAssembler* masm) {
2587 // The ToNumber stub takes one argument in eax.
2588 Label not_smi;
2589 __ JumpIfNotSmi(eax, &not_smi, Label::kNear);
2590 __ Ret();
2591 __ bind(&not_smi);
2592
2593 Label not_heap_number;
2594 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2595 __ j(not_equal, &not_heap_number, Label::kNear);
2596 __ Ret();
2597 __ bind(&not_heap_number);
2598
Ben Murdochda12d292016-06-02 14:46:10 +01002599 NonNumberToNumberStub stub(masm->isolate());
2600 __ TailCallStub(&stub);
2601}
2602
2603void NonNumberToNumberStub::Generate(MacroAssembler* masm) {
2604 // The NonNumberToNumber stub takes one argument in eax.
2605 __ AssertNotNumber(eax);
2606
2607 Label not_string;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002608 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
2609 // eax: object
2610 // edi: object map
2611 __ j(above_equal, &not_string, Label::kNear);
Ben Murdochda12d292016-06-02 14:46:10 +01002612 StringToNumberStub stub(masm->isolate());
2613 __ TailCallStub(&stub);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002614 __ bind(&not_string);
2615
2616 Label not_oddball;
2617 __ CmpInstanceType(edi, ODDBALL_TYPE);
2618 __ j(not_equal, &not_oddball, Label::kNear);
2619 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
2620 __ Ret();
2621 __ bind(&not_oddball);
2622
2623 __ pop(ecx); // Pop return address.
2624 __ push(eax); // Push argument.
2625 __ push(ecx); // Push return address.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002626 __ TailCallRuntime(Runtime::kToNumber);
2627}
2628
Ben Murdochda12d292016-06-02 14:46:10 +01002629void StringToNumberStub::Generate(MacroAssembler* masm) {
2630 // The StringToNumber stub takes one argument in eax.
2631 __ AssertString(eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002632
Ben Murdochda12d292016-06-02 14:46:10 +01002633 // Check if string has a cached array index.
2634 Label runtime;
2635 __ test(FieldOperand(eax, String::kHashFieldOffset),
2636 Immediate(String::kContainsCachedArrayIndexMask));
2637 __ j(not_zero, &runtime, Label::kNear);
2638 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
2639 __ IndexFromHash(eax, eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002640 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002641
Ben Murdochda12d292016-06-02 14:46:10 +01002642 __ bind(&runtime);
2643 __ PopReturnAddressTo(ecx); // Pop return address.
2644 __ Push(eax); // Push argument.
2645 __ PushReturnAddressFrom(ecx); // Push return address.
2646 __ TailCallRuntime(Runtime::kStringToNumber);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002647}
2648
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002649void ToStringStub::Generate(MacroAssembler* masm) {
2650 // The ToString stub takes one argument in eax.
2651 Label is_number;
2652 __ JumpIfSmi(eax, &is_number, Label::kNear);
2653
2654 Label not_string;
2655 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
2656 // eax: receiver
2657 // edi: receiver map
2658 __ j(above_equal, &not_string, Label::kNear);
2659 __ Ret();
2660 __ bind(&not_string);
2661
2662 Label not_heap_number;
2663 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2664 __ j(not_equal, &not_heap_number, Label::kNear);
2665 __ bind(&is_number);
2666 NumberToStringStub stub(isolate());
2667 __ TailCallStub(&stub);
2668 __ bind(&not_heap_number);
2669
2670 Label not_oddball;
2671 __ CmpInstanceType(edi, ODDBALL_TYPE);
2672 __ j(not_equal, &not_oddball, Label::kNear);
2673 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
2674 __ Ret();
2675 __ bind(&not_oddball);
2676
2677 __ pop(ecx); // Pop return address.
2678 __ push(eax); // Push argument.
2679 __ push(ecx); // Push return address.
2680 __ TailCallRuntime(Runtime::kToString);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002681}
2682
2683
Ben Murdoch097c5b22016-05-18 11:27:45 +01002684void ToNameStub::Generate(MacroAssembler* masm) {
2685 // The ToName stub takes one argument in eax.
2686 Label is_number;
2687 __ JumpIfSmi(eax, &is_number, Label::kNear);
2688
2689 Label not_name;
2690 STATIC_ASSERT(FIRST_NAME_TYPE == FIRST_TYPE);
2691 __ CmpObjectType(eax, LAST_NAME_TYPE, edi);
2692 // eax: receiver
2693 // edi: receiver map
2694 __ j(above, &not_name, Label::kNear);
2695 __ Ret();
2696 __ bind(&not_name);
2697
2698 Label not_heap_number;
2699 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2700 __ j(not_equal, &not_heap_number, Label::kNear);
2701 __ bind(&is_number);
2702 NumberToStringStub stub(isolate());
2703 __ TailCallStub(&stub);
2704 __ bind(&not_heap_number);
2705
2706 Label not_oddball;
2707 __ CmpInstanceType(edi, ODDBALL_TYPE);
2708 __ j(not_equal, &not_oddball, Label::kNear);
2709 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
2710 __ Ret();
2711 __ bind(&not_oddball);
2712
2713 __ pop(ecx); // Pop return address.
2714 __ push(eax); // Push argument.
2715 __ push(ecx); // Push return address.
2716 __ TailCallRuntime(Runtime::kToName);
2717}
2718
2719
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002720void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
2721 Register left,
2722 Register right,
2723 Register scratch1,
2724 Register scratch2) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002725 Register length = scratch1;
2726
2727 // Compare lengths.
2728 Label strings_not_equal, check_zero_length;
2729 __ mov(length, FieldOperand(left, String::kLengthOffset));
2730 __ cmp(length, FieldOperand(right, String::kLengthOffset));
2731 __ j(equal, &check_zero_length, Label::kNear);
2732 __ bind(&strings_not_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002733 __ Move(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002734 __ ret(0);
2735
2736 // Check if the length is zero.
2737 Label compare_chars;
2738 __ bind(&check_zero_length);
2739 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002740 __ test(length, length);
Ben Murdoch257744e2011-11-30 15:57:28 +00002741 __ j(not_zero, &compare_chars, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002742 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002743 __ ret(0);
2744
2745 // Compare characters.
2746 __ bind(&compare_chars);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002747 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
2748 &strings_not_equal, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00002749
2750 // Characters are equal.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002751 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002752 __ ret(0);
2753}
2754
2755
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002756void StringHelper::GenerateCompareFlatOneByteStrings(
2757 MacroAssembler* masm, Register left, Register right, Register scratch1,
2758 Register scratch2, Register scratch3) {
Steve Block44f0eee2011-05-26 01:26:41 +01002759 Counters* counters = masm->isolate()->counters();
2760 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002761
2762 // Find minimum length.
Ben Murdoch257744e2011-11-30 15:57:28 +00002763 Label left_shorter;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002764 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
2765 __ mov(scratch3, scratch1);
2766 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
2767
2768 Register length_delta = scratch3;
2769
Ben Murdoch257744e2011-11-30 15:57:28 +00002770 __ j(less_equal, &left_shorter, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002771 // Right string is shorter. Change scratch1 to be length of right string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002772 __ sub(scratch1, length_delta);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002773 __ bind(&left_shorter);
2774
2775 Register min_length = scratch1;
2776
2777 // If either length is zero, just compare lengths.
Ben Murdoch257744e2011-11-30 15:57:28 +00002778 Label compare_lengths;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002779 __ test(min_length, min_length);
Ben Murdoch257744e2011-11-30 15:57:28 +00002780 __ j(zero, &compare_lengths, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002781
Ben Murdoch257744e2011-11-30 15:57:28 +00002782 // Compare characters.
2783 Label result_not_equal;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002784 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
2785 &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002786
2787 // Compare lengths - strings up to min-length are equal.
2788 __ bind(&compare_lengths);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002789 __ test(length_delta, length_delta);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002790 Label length_not_equal;
2791 __ j(not_zero, &length_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002792
2793 // Result is EQUAL.
2794 STATIC_ASSERT(EQUAL == 0);
2795 STATIC_ASSERT(kSmiTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002796 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002797 __ ret(0);
2798
Ben Murdoch257744e2011-11-30 15:57:28 +00002799 Label result_greater;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002800 Label result_less;
2801 __ bind(&length_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00002802 __ j(greater, &result_greater, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002803 __ jmp(&result_less, Label::kNear);
2804 __ bind(&result_not_equal);
2805 __ j(above, &result_greater, Label::kNear);
2806 __ bind(&result_less);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002807
2808 // Result is LESS.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002809 __ Move(eax, Immediate(Smi::FromInt(LESS)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002810 __ ret(0);
2811
2812 // Result is GREATER.
2813 __ bind(&result_greater);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002814 __ Move(eax, Immediate(Smi::FromInt(GREATER)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002815 __ ret(0);
2816}
2817
2818
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002819void StringHelper::GenerateOneByteCharsCompareLoop(
2820 MacroAssembler* masm, Register left, Register right, Register length,
2821 Register scratch, Label* chars_not_equal,
Ben Murdoch257744e2011-11-30 15:57:28 +00002822 Label::Distance chars_not_equal_near) {
2823 // Change index to run from -length to -1 by adding length to string
2824 // start. This means that loop ends when index reaches zero, which
2825 // doesn't need an additional compare.
2826 __ SmiUntag(length);
2827 __ lea(left,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002828 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00002829 __ lea(right,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002830 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00002831 __ neg(length);
2832 Register index = length; // index = -length;
2833
2834 // Compare loop.
2835 Label loop;
2836 __ bind(&loop);
2837 __ mov_b(scratch, Operand(left, index, times_1, 0));
2838 __ cmpb(scratch, Operand(right, index, times_1, 0));
2839 __ j(not_equal, chars_not_equal, chars_not_equal_near);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002840 __ inc(index);
Ben Murdoch257744e2011-11-30 15:57:28 +00002841 __ j(not_zero, &loop);
2842}
2843
2844
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002845void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
2846 // ----------- S t a t e -------------
2847 // -- edx : left
2848 // -- eax : right
2849 // -- esp[0] : return address
2850 // -----------------------------------
2851
2852 // Load ecx with the allocation site. We stick an undefined dummy value here
2853 // and replace it with the real allocation site later when we instantiate this
2854 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
2855 __ mov(ecx, handle(isolate()->heap()->undefined_value()));
2856
2857 // Make sure that we actually patched the allocation site.
2858 if (FLAG_debug_code) {
2859 __ test(ecx, Immediate(kSmiTagMask));
2860 __ Assert(not_equal, kExpectedAllocationSite);
2861 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
2862 isolate()->factory()->allocation_site_map());
2863 __ Assert(equal, kExpectedAllocationSite);
2864 }
2865
2866 // Tail call into the stub that handles binary operations with allocation
2867 // sites.
2868 BinaryOpWithAllocationSiteStub stub(isolate(), state());
2869 __ TailCallStub(&stub);
2870}
2871
2872
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002873void CompareICStub::GenerateBooleans(MacroAssembler* masm) {
2874 DCHECK_EQ(CompareICState::BOOLEAN, state());
2875 Label miss;
2876 Label::Distance const miss_distance =
2877 masm->emit_debug_code() ? Label::kFar : Label::kNear;
2878
2879 __ JumpIfSmi(edx, &miss, miss_distance);
2880 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
2881 __ JumpIfSmi(eax, &miss, miss_distance);
2882 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2883 __ JumpIfNotRoot(ecx, Heap::kBooleanMapRootIndex, &miss, miss_distance);
2884 __ JumpIfNotRoot(ebx, Heap::kBooleanMapRootIndex, &miss, miss_distance);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002885 if (!Token::IsEqualityOp(op())) {
2886 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
2887 __ AssertSmi(eax);
2888 __ mov(edx, FieldOperand(edx, Oddball::kToNumberOffset));
2889 __ AssertSmi(edx);
2890 __ push(eax);
2891 __ mov(eax, edx);
2892 __ pop(edx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002893 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002894 __ sub(eax, edx);
2895 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002896
2897 __ bind(&miss);
2898 GenerateMiss(masm);
2899}
2900
2901
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002902void CompareICStub::GenerateSmis(MacroAssembler* masm) {
2903 DCHECK(state() == CompareICState::SMI);
Ben Murdoch257744e2011-11-30 15:57:28 +00002904 Label miss;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002905 __ mov(ecx, edx);
2906 __ or_(ecx, eax);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002907 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002908
2909 if (GetCondition() == equal) {
2910 // For equality we do not care about the sign of the result.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002911 __ sub(eax, edx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002912 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002913 Label done;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002914 __ sub(edx, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00002915 __ j(no_overflow, &done, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002916 // Correct sign of result in case of overflow.
2917 __ not_(edx);
2918 __ bind(&done);
2919 __ mov(eax, edx);
2920 }
2921 __ ret(0);
2922
2923 __ bind(&miss);
2924 GenerateMiss(masm);
2925}
2926
2927
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002928void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
2929 DCHECK(state() == CompareICState::NUMBER);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002930
Ben Murdoch257744e2011-11-30 15:57:28 +00002931 Label generic_stub;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002932 Label unordered, maybe_undefined1, maybe_undefined2;
Ben Murdoch257744e2011-11-30 15:57:28 +00002933 Label miss;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002934
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002935 if (left() == CompareICState::SMI) {
2936 __ JumpIfNotSmi(edx, &miss);
2937 }
2938 if (right() == CompareICState::SMI) {
2939 __ JumpIfNotSmi(eax, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002940 }
2941
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002942 // Load left and right operand.
2943 Label done, left, left_smi, right_smi;
2944 __ JumpIfSmi(eax, &right_smi, Label::kNear);
2945 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
2946 isolate()->factory()->heap_number_map());
2947 __ j(not_equal, &maybe_undefined1, Label::kNear);
2948 __ movsd(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2949 __ jmp(&left, Label::kNear);
2950 __ bind(&right_smi);
2951 __ mov(ecx, eax); // Can't clobber eax because we can still jump away.
2952 __ SmiUntag(ecx);
2953 __ Cvtsi2sd(xmm1, ecx);
2954
2955 __ bind(&left);
2956 __ JumpIfSmi(edx, &left_smi, Label::kNear);
2957 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
2958 isolate()->factory()->heap_number_map());
2959 __ j(not_equal, &maybe_undefined2, Label::kNear);
2960 __ movsd(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2961 __ jmp(&done);
2962 __ bind(&left_smi);
2963 __ mov(ecx, edx); // Can't clobber edx because we can still jump away.
2964 __ SmiUntag(ecx);
2965 __ Cvtsi2sd(xmm0, ecx);
2966
2967 __ bind(&done);
2968 // Compare operands.
2969 __ ucomisd(xmm0, xmm1);
2970
2971 // Don't base result on EFLAGS when a NaN is involved.
2972 __ j(parity_even, &unordered, Label::kNear);
2973
2974 // Return a result of -1, 0, or 1, based on EFLAGS.
2975 // Performing mov, because xor would destroy the flag register.
2976 __ mov(eax, 0); // equal
2977 __ mov(ecx, Immediate(Smi::FromInt(1)));
2978 __ cmov(above, eax, ecx);
2979 __ mov(ecx, Immediate(Smi::FromInt(-1)));
2980 __ cmov(below, eax, ecx);
2981 __ ret(0);
2982
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002983 __ bind(&unordered);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002984 __ bind(&generic_stub);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002985 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002986 CompareICState::GENERIC, CompareICState::GENERIC);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002987 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
2988
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002989 __ bind(&maybe_undefined1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002990 if (Token::IsOrderedRelationalCompareOp(op())) {
2991 __ cmp(eax, Immediate(isolate()->factory()->undefined_value()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002992 __ j(not_equal, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002993 __ JumpIfSmi(edx, &unordered);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002994 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
2995 __ j(not_equal, &maybe_undefined2, Label::kNear);
2996 __ jmp(&unordered);
2997 }
2998
2999 __ bind(&maybe_undefined2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003000 if (Token::IsOrderedRelationalCompareOp(op())) {
3001 __ cmp(edx, Immediate(isolate()->factory()->undefined_value()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003002 __ j(equal, &unordered);
3003 }
3004
Ben Murdochb0fe1622011-05-05 13:52:32 +01003005 __ bind(&miss);
3006 GenerateMiss(masm);
3007}
3008
3009
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003010void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3011 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3012 DCHECK(GetCondition() == equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00003013
3014 // Registers containing left and right operands respectively.
3015 Register left = edx;
3016 Register right = eax;
3017 Register tmp1 = ecx;
3018 Register tmp2 = ebx;
3019
3020 // Check that both operands are heap objects.
3021 Label miss;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003022 __ mov(tmp1, left);
Ben Murdoch257744e2011-11-30 15:57:28 +00003023 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003024 __ and_(tmp1, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003025 __ JumpIfSmi(tmp1, &miss, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00003026
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003027 // Check that both operands are internalized strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00003028 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3029 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3030 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3031 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003032 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3033 __ or_(tmp1, tmp2);
3034 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
3035 __ j(not_zero, &miss, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00003036
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003037 // Internalized strings are compared by identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00003038 Label done;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003039 __ cmp(left, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00003040 // Make sure eax is non-zero. At this point input operands are
3041 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003042 DCHECK(right.is(eax));
Ben Murdoch257744e2011-11-30 15:57:28 +00003043 __ j(not_equal, &done, Label::kNear);
3044 STATIC_ASSERT(EQUAL == 0);
3045 STATIC_ASSERT(kSmiTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003046 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003047 __ bind(&done);
3048 __ ret(0);
3049
3050 __ bind(&miss);
3051 GenerateMiss(masm);
3052}
3053
3054
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003055void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3056 DCHECK(state() == CompareICState::UNIQUE_NAME);
3057 DCHECK(GetCondition() == equal);
3058
3059 // Registers containing left and right operands respectively.
3060 Register left = edx;
3061 Register right = eax;
3062 Register tmp1 = ecx;
3063 Register tmp2 = ebx;
3064
3065 // Check that both operands are heap objects.
3066 Label miss;
3067 __ mov(tmp1, left);
3068 STATIC_ASSERT(kSmiTag == 0);
3069 __ and_(tmp1, right);
3070 __ JumpIfSmi(tmp1, &miss, Label::kNear);
3071
3072 // Check that both operands are unique names. This leaves the instance
3073 // types loaded in tmp1 and tmp2.
3074 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3075 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3076 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3077 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3078
3079 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
3080 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
3081
3082 // Unique names are compared by identity.
3083 Label done;
3084 __ cmp(left, right);
3085 // Make sure eax is non-zero. At this point input operands are
3086 // guaranteed to be non-zero.
3087 DCHECK(right.is(eax));
3088 __ j(not_equal, &done, Label::kNear);
3089 STATIC_ASSERT(EQUAL == 0);
3090 STATIC_ASSERT(kSmiTag == 0);
3091 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
3092 __ bind(&done);
3093 __ ret(0);
3094
3095 __ bind(&miss);
3096 GenerateMiss(masm);
3097}
3098
3099
3100void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3101 DCHECK(state() == CompareICState::STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00003102 Label miss;
3103
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003104 bool equality = Token::IsEqualityOp(op());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003105
Ben Murdoch257744e2011-11-30 15:57:28 +00003106 // Registers containing left and right operands respectively.
3107 Register left = edx;
3108 Register right = eax;
3109 Register tmp1 = ecx;
3110 Register tmp2 = ebx;
3111 Register tmp3 = edi;
3112
3113 // Check that both operands are heap objects.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003114 __ mov(tmp1, left);
Ben Murdoch257744e2011-11-30 15:57:28 +00003115 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003116 __ and_(tmp1, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003117 __ JumpIfSmi(tmp1, &miss);
Ben Murdoch257744e2011-11-30 15:57:28 +00003118
3119 // Check that both operands are strings. This leaves the instance
3120 // types loaded in tmp1 and tmp2.
3121 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
3122 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
3123 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
3124 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
3125 __ mov(tmp3, tmp1);
3126 STATIC_ASSERT(kNotStringTag != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003127 __ or_(tmp3, tmp2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003128 __ test(tmp3, Immediate(kIsNotStringMask));
3129 __ j(not_zero, &miss);
3130
3131 // Fast check for identical strings.
3132 Label not_same;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003133 __ cmp(left, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00003134 __ j(not_equal, &not_same, Label::kNear);
3135 STATIC_ASSERT(EQUAL == 0);
3136 STATIC_ASSERT(kSmiTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003137 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003138 __ ret(0);
3139
3140 // Handle not identical strings.
3141 __ bind(&not_same);
3142
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003143 // Check that both strings are internalized. If they are, we're done
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003144 // because we already know they are not identical. But in the case of
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003145 // non-equality compare, we still need to determine the order. We
3146 // also know they are both strings.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003147 if (equality) {
3148 Label do_compare;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003149 STATIC_ASSERT(kInternalizedTag == 0);
3150 __ or_(tmp1, tmp2);
3151 __ test(tmp1, Immediate(kIsNotInternalizedMask));
3152 __ j(not_zero, &do_compare, Label::kNear);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003153 // Make sure eax is non-zero. At this point input operands are
3154 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003155 DCHECK(right.is(eax));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003156 __ ret(0);
3157 __ bind(&do_compare);
3158 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003159
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003160 // Check that both strings are sequential one-byte.
Ben Murdoch257744e2011-11-30 15:57:28 +00003161 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003162 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003163
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003164 // Compare flat one byte strings. Returns when done.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003165 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003166 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
3167 tmp2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003168 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003169 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3170 tmp2, tmp3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003171 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003172
3173 // Handle more complex cases in runtime.
3174 __ bind(&runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003175 if (equality) {
Ben Murdochda12d292016-06-02 14:46:10 +01003176 {
3177 FrameScope scope(masm, StackFrame::INTERNAL);
3178 __ Push(left);
3179 __ Push(right);
3180 __ CallRuntime(Runtime::kStringEqual);
3181 }
3182 __ sub(eax, Immediate(masm->isolate()->factory()->true_value()));
3183 __ Ret();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003184 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01003185 __ pop(tmp1); // Return address.
3186 __ push(left);
3187 __ push(right);
3188 __ push(tmp1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003189 __ TailCallRuntime(Runtime::kStringCompare);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003190 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003191
3192 __ bind(&miss);
3193 GenerateMiss(masm);
3194}
3195
3196
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003197void CompareICStub::GenerateReceivers(MacroAssembler* masm) {
3198 DCHECK_EQ(CompareICState::RECEIVER, state());
Ben Murdoch257744e2011-11-30 15:57:28 +00003199 Label miss;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003200 __ mov(ecx, edx);
3201 __ and_(ecx, eax);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003202 __ JumpIfSmi(ecx, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003203
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003204 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
3205 __ CmpObjectType(eax, FIRST_JS_RECEIVER_TYPE, ecx);
3206 __ j(below, &miss, Label::kNear);
3207 __ CmpObjectType(edx, FIRST_JS_RECEIVER_TYPE, ecx);
3208 __ j(below, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003209
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003210 DCHECK_EQ(equal, GetCondition());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003211 __ sub(eax, edx);
3212 __ ret(0);
3213
3214 __ bind(&miss);
3215 GenerateMiss(masm);
3216}
3217
3218
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003219void CompareICStub::GenerateKnownReceivers(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003220 Label miss;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003221 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003222 __ mov(ecx, edx);
3223 __ and_(ecx, eax);
3224 __ JumpIfSmi(ecx, &miss, Label::kNear);
3225
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003226 __ GetWeakValue(edi, cell);
3227 __ cmp(edi, FieldOperand(eax, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003228 __ j(not_equal, &miss, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003229 __ cmp(edi, FieldOperand(edx, HeapObject::kMapOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003230 __ j(not_equal, &miss, Label::kNear);
3231
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003232 if (Token::IsEqualityOp(op())) {
3233 __ sub(eax, edx);
3234 __ ret(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003235 } else {
3236 __ PopReturnAddressTo(ecx);
3237 __ Push(edx);
3238 __ Push(eax);
3239 __ Push(Immediate(Smi::FromInt(NegativeComparisonResult(GetCondition()))));
3240 __ PushReturnAddressFrom(ecx);
3241 __ TailCallRuntime(Runtime::kCompare);
3242 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00003243
3244 __ bind(&miss);
3245 GenerateMiss(masm);
3246}
3247
3248
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003249void CompareICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003250 {
3251 // Call the runtime system in a fresh internal frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003252 FrameScope scope(masm, StackFrame::INTERNAL);
3253 __ push(edx); // Preserve edx and eax.
3254 __ push(eax);
3255 __ push(edx); // And also use them as the arguments.
3256 __ push(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003257 __ push(Immediate(Smi::FromInt(op())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003258 __ CallRuntime(Runtime::kCompareIC_Miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003259 // Compute the entry point of the rewritten stub.
3260 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
3261 __ pop(eax);
3262 __ pop(edx);
3263 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003264
Ben Murdochb0fe1622011-05-05 13:52:32 +01003265 // Do a tail call to the rewritten stub.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003266 __ jmp(edi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003267}
3268
3269
Ben Murdoch257744e2011-11-30 15:57:28 +00003270// Helper function used to check that the dictionary doesn't contain
3271// the property. This function may return false negatives, so miss_label
3272// must always call a backup property check that is complete.
3273// This function is safe to call if the receiver has fast properties.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003274// Name must be a unique name and receiver must be a heap object.
3275void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3276 Label* miss,
3277 Label* done,
3278 Register properties,
3279 Handle<Name> name,
3280 Register r0) {
3281 DCHECK(name->IsUniqueName());
Ben Murdoch257744e2011-11-30 15:57:28 +00003282
3283 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3284 // not equal to the name and kProbes-th slot is not used (its name is the
3285 // undefined value), it guarantees the hash table doesn't contain the
3286 // property. It's true even if some slots represent deleted properties
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003287 // (their names are the hole value).
Ben Murdoch257744e2011-11-30 15:57:28 +00003288 for (int i = 0; i < kInlinedProbes; i++) {
3289 // Compute the masked index: (hash + i + i * i) & mask.
3290 Register index = r0;
3291 // Capacity is smi 2^n.
3292 __ mov(index, FieldOperand(properties, kCapacityOffset));
3293 __ dec(index);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003294 __ and_(index,
3295 Immediate(Smi::FromInt(name->Hash() +
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003296 NameDictionary::GetProbeOffset(i))));
Ben Murdoch257744e2011-11-30 15:57:28 +00003297
3298 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003299 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003300 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
3301 Register entity_name = r0;
3302 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003303 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003304 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
3305 kElementsStartOffset - kHeapObjectTag));
3306 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
3307 __ j(equal, done);
3308
3309 // Stop if found the property.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003310 __ cmp(entity_name, Handle<Name>(name));
Ben Murdoch257744e2011-11-30 15:57:28 +00003311 __ j(equal, miss);
3312
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003313 Label good;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003314 // Check for the hole and skip.
3315 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003316 __ j(equal, &good, Label::kNear);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003317
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003318 // Check if the entry name is not a unique name.
Ben Murdoch257744e2011-11-30 15:57:28 +00003319 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003320 __ JumpIfNotUniqueNameInstanceType(
3321 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3322 __ bind(&good);
Ben Murdoch257744e2011-11-30 15:57:28 +00003323 }
3324
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003325 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3326 NEGATIVE_LOOKUP);
Ben Murdoch257744e2011-11-30 15:57:28 +00003327 __ push(Immediate(Handle<Object>(name)));
3328 __ push(Immediate(name->Hash()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003329 __ CallStub(&stub);
3330 __ test(r0, r0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003331 __ j(not_zero, miss);
3332 __ jmp(done);
Ben Murdoch257744e2011-11-30 15:57:28 +00003333}
3334
3335
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003336// Probe the name dictionary in the |elements| register. Jump to the
Ben Murdoch257744e2011-11-30 15:57:28 +00003337// |done| label if a property with the given name is found leaving the
3338// index into the dictionary in |r0|. Jump to the |miss| label
3339// otherwise.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003340void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3341 Label* miss,
3342 Label* done,
3343 Register elements,
3344 Register name,
3345 Register r0,
3346 Register r1) {
3347 DCHECK(!elements.is(r0));
3348 DCHECK(!elements.is(r1));
3349 DCHECK(!name.is(r0));
3350 DCHECK(!name.is(r1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003351
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003352 __ AssertName(name);
Ben Murdoch257744e2011-11-30 15:57:28 +00003353
3354 __ mov(r1, FieldOperand(elements, kCapacityOffset));
3355 __ shr(r1, kSmiTagSize); // convert smi to int
3356 __ dec(r1);
3357
3358 // Generate an unrolled loop that performs a few probes before
3359 // giving up. Measurements done on Gmail indicate that 2 probes
3360 // cover ~93% of loads from dictionaries.
3361 for (int i = 0; i < kInlinedProbes; i++) {
3362 // Compute the masked index: (hash + i + i * i) & mask.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003363 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3364 __ shr(r0, Name::kHashShift);
Ben Murdoch257744e2011-11-30 15:57:28 +00003365 if (i > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003366 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003367 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003368 __ and_(r0, r1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003369
3370 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003371 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003372 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
3373
3374 // Check if the key is identical to the name.
3375 __ cmp(name, Operand(elements,
3376 r0,
3377 times_4,
3378 kElementsStartOffset - kHeapObjectTag));
3379 __ j(equal, done);
3380 }
3381
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003382 NameDictionaryLookupStub stub(masm->isolate(), elements, r1, r0,
3383 POSITIVE_LOOKUP);
Ben Murdoch257744e2011-11-30 15:57:28 +00003384 __ push(name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003385 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3386 __ shr(r0, Name::kHashShift);
Ben Murdoch257744e2011-11-30 15:57:28 +00003387 __ push(r0);
3388 __ CallStub(&stub);
3389
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003390 __ test(r1, r1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003391 __ j(zero, miss);
3392 __ jmp(done);
3393}
3394
3395
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003396void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003397 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3398 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00003399 // Stack frame on entry:
3400 // esp[0 * kPointerSize]: return address.
3401 // esp[1 * kPointerSize]: key's hash.
3402 // esp[2 * kPointerSize]: key.
3403 // Registers:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003404 // dictionary_: NameDictionary to probe.
Ben Murdoch257744e2011-11-30 15:57:28 +00003405 // result_: used as scratch.
3406 // index_: will hold an index of entry if lookup is successful.
3407 // might alias with result_.
3408 // Returns:
3409 // result_ is zero if lookup failed, non zero otherwise.
3410
3411 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3412
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003413 Register scratch = result();
Ben Murdoch257744e2011-11-30 15:57:28 +00003414
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003415 __ mov(scratch, FieldOperand(dictionary(), kCapacityOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003416 __ dec(scratch);
3417 __ SmiUntag(scratch);
3418 __ push(scratch);
3419
3420 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3421 // not equal to the name and kProbes-th slot is not used (its name is the
3422 // undefined value), it guarantees the hash table doesn't contain the
3423 // property. It's true even if some slots represent deleted properties
3424 // (their names are the null value).
3425 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3426 // Compute the masked index: (hash + i + i * i) & mask.
3427 __ mov(scratch, Operand(esp, 2 * kPointerSize));
3428 if (i > 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003429 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003430 }
3431 __ and_(scratch, Operand(esp, 0));
3432
3433 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003434 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003435 __ lea(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
Ben Murdoch257744e2011-11-30 15:57:28 +00003436
3437 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003438 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003439 __ mov(scratch, Operand(dictionary(), index(), times_pointer_size,
Ben Murdoch257744e2011-11-30 15:57:28 +00003440 kElementsStartOffset - kHeapObjectTag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003441 __ cmp(scratch, isolate()->factory()->undefined_value());
Ben Murdoch257744e2011-11-30 15:57:28 +00003442 __ j(equal, &not_in_dictionary);
3443
3444 // Stop if found the property.
3445 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
3446 __ j(equal, &in_dictionary);
3447
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003448 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3449 // If we hit a key that is not a unique name during negative
3450 // lookup we have to bailout as this key might be equal to the
Ben Murdoch257744e2011-11-30 15:57:28 +00003451 // key we are looking for.
3452
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003453 // Check if the entry name is not a unique name.
Ben Murdoch257744e2011-11-30 15:57:28 +00003454 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003455 __ JumpIfNotUniqueNameInstanceType(
3456 FieldOperand(scratch, Map::kInstanceTypeOffset),
3457 &maybe_in_dictionary);
Ben Murdoch257744e2011-11-30 15:57:28 +00003458 }
3459 }
3460
3461 __ bind(&maybe_in_dictionary);
3462 // If we are doing negative lookup then probing failure should be
3463 // treated as a lookup success. For positive lookup probing failure
3464 // should be treated as lookup failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003465 if (mode() == POSITIVE_LOOKUP) {
3466 __ mov(result(), Immediate(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003467 __ Drop(1);
3468 __ ret(2 * kPointerSize);
3469 }
3470
3471 __ bind(&in_dictionary);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003472 __ mov(result(), Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00003473 __ Drop(1);
3474 __ ret(2 * kPointerSize);
3475
3476 __ bind(&not_in_dictionary);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003477 __ mov(result(), Immediate(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003478 __ Drop(1);
3479 __ ret(2 * kPointerSize);
3480}
3481
3482
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003483void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3484 Isolate* isolate) {
3485 StoreBufferOverflowStub stub(isolate, kDontSaveFPRegs);
3486 stub.GetCode();
3487 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3488 stub2.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003489}
3490
3491
3492// Takes the input in 3 registers: address_ value_ and object_. A pointer to
3493// the value has just been written into the object, now this stub makes sure
3494// we keep the GC informed. The word in the object where the value has been
3495// written is in the address register.
3496void RecordWriteStub::Generate(MacroAssembler* masm) {
3497 Label skip_to_incremental_noncompacting;
3498 Label skip_to_incremental_compacting;
3499
3500 // The first two instructions are generated with labels so as to get the
3501 // offset fixed up correctly by the bind(Label*) call. We patch it back and
3502 // forth between a compare instructions (a nop in this position) and the
3503 // real branch when we start and stop incremental heap marking.
3504 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
3505 __ jmp(&skip_to_incremental_compacting, Label::kFar);
3506
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003507 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3508 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003509 MacroAssembler::kReturnAtEnd);
3510 } else {
3511 __ ret(0);
3512 }
3513
3514 __ bind(&skip_to_incremental_noncompacting);
3515 GenerateIncremental(masm, INCREMENTAL);
3516
3517 __ bind(&skip_to_incremental_compacting);
3518 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3519
3520 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3521 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
3522 masm->set_byte_at(0, kTwoByteNopInstruction);
3523 masm->set_byte_at(2, kFiveByteNopInstruction);
3524}
3525
3526
3527void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3528 regs_.Save(masm);
3529
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003530 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003531 Label dont_need_remembered_set;
3532
3533 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3534 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
3535 regs_.scratch0(),
3536 &dont_need_remembered_set);
3537
Ben Murdoch097c5b22016-05-18 11:27:45 +01003538 __ JumpIfInNewSpace(regs_.object(), regs_.scratch0(),
3539 &dont_need_remembered_set);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003540
3541 // First notify the incremental marker if necessary, then update the
3542 // remembered set.
3543 CheckNeedsToInformIncrementalMarker(
3544 masm,
3545 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
3546 mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003547 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003548 regs_.Restore(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003549 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003550 MacroAssembler::kReturnAtEnd);
3551
3552 __ bind(&dont_need_remembered_set);
3553 }
3554
3555 CheckNeedsToInformIncrementalMarker(
3556 masm,
3557 kReturnOnNoNeedToInformIncrementalMarker,
3558 mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003559 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003560 regs_.Restore(masm);
3561 __ ret(0);
3562}
3563
3564
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003565void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3566 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003567 int argument_count = 3;
3568 __ PrepareCallCFunction(argument_count, regs_.scratch0());
3569 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003570 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003571 __ mov(Operand(esp, 2 * kPointerSize),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003572 Immediate(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003573
3574 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003575 __ CallCFunction(
3576 ExternalReference::incremental_marking_record_write_function(isolate()),
3577 argument_count);
3578
3579 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003580}
3581
3582
3583void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3584 MacroAssembler* masm,
3585 OnNoNeedToInformIncrementalMarker on_no_need,
3586 Mode mode) {
3587 Label object_is_black, need_incremental, need_incremental_pop_object;
3588
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003589 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
3590 __ and_(regs_.scratch0(), regs_.object());
3591 __ mov(regs_.scratch1(),
3592 Operand(regs_.scratch0(),
3593 MemoryChunk::kWriteBarrierCounterOffset));
3594 __ sub(regs_.scratch1(), Immediate(1));
3595 __ mov(Operand(regs_.scratch0(),
3596 MemoryChunk::kWriteBarrierCounterOffset),
3597 regs_.scratch1());
3598 __ j(negative, &need_incremental);
3599
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003600 // Let's look at the color of the object: If it is not black we don't have
3601 // to inform the incremental marker.
3602 __ JumpIfBlack(regs_.object(),
3603 regs_.scratch0(),
3604 regs_.scratch1(),
3605 &object_is_black,
3606 Label::kNear);
3607
3608 regs_.Restore(masm);
3609 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003610 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003611 MacroAssembler::kReturnAtEnd);
3612 } else {
3613 __ ret(0);
3614 }
3615
3616 __ bind(&object_is_black);
3617
3618 // Get the value from the slot.
3619 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3620
3621 if (mode == INCREMENTAL_COMPACTION) {
3622 Label ensure_not_white;
3623
3624 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3625 regs_.scratch1(), // Scratch.
3626 MemoryChunk::kEvacuationCandidateMask,
3627 zero,
3628 &ensure_not_white,
3629 Label::kNear);
3630
3631 __ CheckPageFlag(regs_.object(),
3632 regs_.scratch1(), // Scratch.
3633 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3634 not_zero,
3635 &ensure_not_white,
3636 Label::kNear);
3637
3638 __ jmp(&need_incremental);
3639
3640 __ bind(&ensure_not_white);
3641 }
3642
3643 // We need an extra register for this, so we push the object register
3644 // temporarily.
3645 __ push(regs_.object());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003646 __ JumpIfWhite(regs_.scratch0(), // The value.
3647 regs_.scratch1(), // Scratch.
3648 regs_.object(), // Scratch.
3649 &need_incremental_pop_object, Label::kNear);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003650 __ pop(regs_.object());
3651
3652 regs_.Restore(masm);
3653 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003654 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003655 MacroAssembler::kReturnAtEnd);
3656 } else {
3657 __ ret(0);
3658 }
3659
3660 __ bind(&need_incremental_pop_object);
3661 __ pop(regs_.object());
3662
3663 __ bind(&need_incremental);
3664
3665 // Fall through when we need to inform the incremental marker.
3666}
3667
3668
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003669void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
3670 CEntryStub ces(isolate(), 1, kSaveFPRegs);
3671 __ call(ces.GetCode(), RelocInfo::CODE_TARGET);
3672 int parameter_count_offset =
Ben Murdochda12d292016-06-02 14:46:10 +01003673 StubFailureTrampolineFrameConstants::kArgumentsLengthOffset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003674 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
3675 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
3676 __ pop(ecx);
3677 int additional_offset =
3678 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
3679 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
3680 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
3681}
3682
3683
3684void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003685 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3686 LoadICStub stub(isolate(), state());
3687 stub.GenerateForTrampoline(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003688}
3689
3690
3691void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003692 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3693 KeyedLoadICStub stub(isolate(), state());
3694 stub.GenerateForTrampoline(masm);
3695}
3696
3697
3698static void HandleArrayCases(MacroAssembler* masm, Register receiver,
3699 Register key, Register vector, Register slot,
3700 Register feedback, bool is_polymorphic,
3701 Label* miss) {
3702 // feedback initially contains the feedback array
3703 Label next, next_loop, prepare_next;
3704 Label load_smi_map, compare_map;
3705 Label start_polymorphic;
3706
3707 __ push(receiver);
3708 __ push(vector);
3709
3710 Register receiver_map = receiver;
3711 Register cached_map = vector;
3712
3713 // Receiver might not be a heap object.
3714 __ JumpIfSmi(receiver, &load_smi_map);
3715 __ mov(receiver_map, FieldOperand(receiver, 0));
3716 __ bind(&compare_map);
3717 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3718
3719 // A named keyed load might have a 2 element array, all other cases can count
3720 // on an array with at least 2 {map, handler} pairs, so they can go right
3721 // into polymorphic array handling.
3722 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3723 __ j(not_equal, is_polymorphic ? &start_polymorphic : &next);
3724
3725 // found, now call handler.
3726 Register handler = feedback;
3727 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3728 __ pop(vector);
3729 __ pop(receiver);
3730 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3731 __ jmp(handler);
3732
3733 if (!is_polymorphic) {
3734 __ bind(&next);
3735 __ cmp(FieldOperand(feedback, FixedArray::kLengthOffset),
3736 Immediate(Smi::FromInt(2)));
3737 __ j(not_equal, &start_polymorphic);
3738 __ pop(vector);
3739 __ pop(receiver);
3740 __ jmp(miss);
3741 }
3742
3743 // Polymorphic, we have to loop from 2 to N
3744 __ bind(&start_polymorphic);
3745 __ push(key);
3746 Register counter = key;
3747 __ mov(counter, Immediate(Smi::FromInt(2)));
3748 __ bind(&next_loop);
3749 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3750 FixedArray::kHeaderSize));
3751 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3752 __ j(not_equal, &prepare_next);
3753 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
3754 FixedArray::kHeaderSize + kPointerSize));
3755 __ pop(key);
3756 __ pop(vector);
3757 __ pop(receiver);
3758 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3759 __ jmp(handler);
3760
3761 __ bind(&prepare_next);
3762 __ add(counter, Immediate(Smi::FromInt(2)));
3763 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3764 __ j(less, &next_loop);
3765
3766 // We exhausted our array of map handler pairs.
3767 __ pop(key);
3768 __ pop(vector);
3769 __ pop(receiver);
3770 __ jmp(miss);
3771
3772 __ bind(&load_smi_map);
3773 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3774 __ jmp(&compare_map);
3775}
3776
3777
3778static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
3779 Register key, Register vector, Register slot,
3780 Register weak_cell, Label* miss) {
3781 // feedback initially contains the feedback array
3782 Label compare_smi_map;
3783
3784 // Move the weak map into the weak_cell register.
3785 Register ic_map = weak_cell;
3786 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
3787
3788 // Receiver might not be a heap object.
3789 __ JumpIfSmi(receiver, &compare_smi_map);
3790 __ cmp(ic_map, FieldOperand(receiver, 0));
3791 __ j(not_equal, miss);
3792 Register handler = weak_cell;
3793 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
3794 FixedArray::kHeaderSize + kPointerSize));
3795 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3796 __ jmp(handler);
3797
3798 // In microbenchmarks, it made sense to unroll this code so that the call to
3799 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
3800 __ bind(&compare_smi_map);
3801 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
3802 __ j(not_equal, miss);
3803 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
3804 FixedArray::kHeaderSize + kPointerSize));
3805 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3806 __ jmp(handler);
3807}
3808
3809
3810void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
3811
3812
3813void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3814 GenerateImpl(masm, true);
3815}
3816
3817
3818void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3819 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
3820 Register name = LoadWithVectorDescriptor::NameRegister(); // ecx
3821 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
3822 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
3823 Register scratch = edi;
3824 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3825 FixedArray::kHeaderSize));
3826
3827 // Is it a weak cell?
3828 Label try_array;
3829 Label not_array, smi_key, key_okay, miss;
3830 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3831 __ j(not_equal, &try_array);
3832 HandleMonomorphicCase(masm, receiver, name, vector, slot, scratch, &miss);
3833
3834 // Is it a fixed array?
3835 __ bind(&try_array);
3836 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3837 __ j(not_equal, &not_array);
3838 HandleArrayCases(masm, receiver, name, vector, slot, scratch, true, &miss);
3839
3840 __ bind(&not_array);
3841 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3842 __ j(not_equal, &miss);
3843 __ push(slot);
3844 __ push(vector);
3845 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
3846 Code::ComputeHandlerFlags(Code::LOAD_IC));
3847 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
3848 receiver, name, vector, scratch);
3849 __ pop(vector);
3850 __ pop(slot);
3851
3852 __ bind(&miss);
3853 LoadIC::GenerateMiss(masm);
3854}
3855
3856
3857void KeyedLoadICStub::Generate(MacroAssembler* masm) {
3858 GenerateImpl(masm, false);
3859}
3860
3861
3862void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3863 GenerateImpl(masm, true);
3864}
3865
3866
3867void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3868 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
3869 Register key = LoadWithVectorDescriptor::NameRegister(); // ecx
3870 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
3871 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
3872 Register feedback = edi;
3873 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
3874 FixedArray::kHeaderSize));
3875 // Is it a weak cell?
3876 Label try_array;
3877 Label not_array, smi_key, key_okay, miss;
3878 __ CompareRoot(FieldOperand(feedback, 0), Heap::kWeakCellMapRootIndex);
3879 __ j(not_equal, &try_array);
3880 HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, &miss);
3881
3882 __ bind(&try_array);
3883 // Is it a fixed array?
3884 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
3885 __ j(not_equal, &not_array);
3886
3887 // We have a polymorphic element handler.
3888 Label polymorphic, try_poly_name;
3889 __ bind(&polymorphic);
3890 HandleArrayCases(masm, receiver, key, vector, slot, feedback, true, &miss);
3891
3892 __ bind(&not_array);
3893 // Is it generic?
3894 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
3895 __ j(not_equal, &try_poly_name);
3896 Handle<Code> megamorphic_stub =
3897 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3898 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
3899
3900 __ bind(&try_poly_name);
3901 // We might have a name in feedback, and a fixed array in the next slot.
3902 __ cmp(key, feedback);
3903 __ j(not_equal, &miss);
3904 // If the name comparison succeeded, we know we have a fixed array with
3905 // at least one map/handler pair.
3906 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
3907 FixedArray::kHeaderSize + kPointerSize));
3908 HandleArrayCases(masm, receiver, key, vector, slot, feedback, false, &miss);
3909
3910 __ bind(&miss);
3911 KeyedLoadIC::GenerateMiss(masm);
3912}
3913
3914
3915void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3916 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3917 VectorStoreICStub stub(isolate(), state());
3918 stub.GenerateForTrampoline(masm);
3919}
3920
3921
3922void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3923 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3924 VectorKeyedStoreICStub stub(isolate(), state());
3925 stub.GenerateForTrampoline(masm);
3926}
3927
3928
3929void VectorStoreICStub::Generate(MacroAssembler* masm) {
3930 GenerateImpl(masm, false);
3931}
3932
3933
3934void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3935 GenerateImpl(masm, true);
3936}
3937
3938
3939// value is on the stack already.
3940static void HandlePolymorphicStoreCase(MacroAssembler* masm, Register receiver,
3941 Register key, Register vector,
3942 Register slot, Register feedback,
3943 bool is_polymorphic, Label* miss) {
3944 // feedback initially contains the feedback array
3945 Label next, next_loop, prepare_next;
3946 Label load_smi_map, compare_map;
3947 Label start_polymorphic;
3948 Label pop_and_miss;
3949 ExternalReference virtual_register =
3950 ExternalReference::virtual_handler_register(masm->isolate());
3951
3952 __ push(receiver);
3953 __ push(vector);
3954
3955 Register receiver_map = receiver;
3956 Register cached_map = vector;
3957
3958 // Receiver might not be a heap object.
3959 __ JumpIfSmi(receiver, &load_smi_map);
3960 __ mov(receiver_map, FieldOperand(receiver, 0));
3961 __ bind(&compare_map);
3962 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3963
3964 // A named keyed store might have a 2 element array, all other cases can count
3965 // on an array with at least 2 {map, handler} pairs, so they can go right
3966 // into polymorphic array handling.
3967 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3968 __ j(not_equal, &start_polymorphic);
3969
3970 // found, now call handler.
3971 Register handler = feedback;
3972 DCHECK(handler.is(VectorStoreICDescriptor::ValueRegister()));
3973 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3974 __ pop(vector);
3975 __ pop(receiver);
3976 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3977 __ mov(Operand::StaticVariable(virtual_register), handler);
3978 __ pop(handler); // Pop "value".
3979 __ jmp(Operand::StaticVariable(virtual_register));
3980
3981 // Polymorphic, we have to loop from 2 to N
3982 __ bind(&start_polymorphic);
3983 __ push(key);
3984 Register counter = key;
3985 __ mov(counter, Immediate(Smi::FromInt(2)));
3986
3987 if (!is_polymorphic) {
3988 // If is_polymorphic is false, we may only have a two element array.
3989 // Check against length now in that case.
3990 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3991 __ j(greater_equal, &pop_and_miss);
3992 }
3993
3994 __ bind(&next_loop);
3995 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3996 FixedArray::kHeaderSize));
3997 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3998 __ j(not_equal, &prepare_next);
3999 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
4000 FixedArray::kHeaderSize + kPointerSize));
4001 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
4002 __ pop(key);
4003 __ pop(vector);
4004 __ pop(receiver);
4005 __ mov(Operand::StaticVariable(virtual_register), handler);
4006 __ pop(handler); // Pop "value".
4007 __ jmp(Operand::StaticVariable(virtual_register));
4008
4009 __ bind(&prepare_next);
4010 __ add(counter, Immediate(Smi::FromInt(2)));
4011 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
4012 __ j(less, &next_loop);
4013
4014 // We exhausted our array of map handler pairs.
4015 __ bind(&pop_and_miss);
4016 __ pop(key);
4017 __ pop(vector);
4018 __ pop(receiver);
4019 __ jmp(miss);
4020
4021 __ bind(&load_smi_map);
4022 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4023 __ jmp(&compare_map);
4024}
4025
4026
4027static void HandleMonomorphicStoreCase(MacroAssembler* masm, Register receiver,
4028 Register key, Register vector,
4029 Register slot, Register weak_cell,
4030 Label* miss) {
4031 // The store ic value is on the stack.
4032 DCHECK(weak_cell.is(VectorStoreICDescriptor::ValueRegister()));
4033 ExternalReference virtual_register =
4034 ExternalReference::virtual_handler_register(masm->isolate());
4035
4036 // feedback initially contains the feedback array
4037 Label compare_smi_map;
4038
4039 // Move the weak map into the weak_cell register.
4040 Register ic_map = weak_cell;
4041 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
4042
4043 // Receiver might not be a heap object.
4044 __ JumpIfSmi(receiver, &compare_smi_map);
4045 __ cmp(ic_map, FieldOperand(receiver, 0));
4046 __ j(not_equal, miss);
4047 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
4048 FixedArray::kHeaderSize + kPointerSize));
4049 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
4050 // Put the store ic value back in it's register.
4051 __ mov(Operand::StaticVariable(virtual_register), weak_cell);
4052 __ pop(weak_cell); // Pop "value".
4053 // jump to the handler.
4054 __ jmp(Operand::StaticVariable(virtual_register));
4055
4056 // In microbenchmarks, it made sense to unroll this code so that the call to
4057 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
4058 __ bind(&compare_smi_map);
4059 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
4060 __ j(not_equal, miss);
4061 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
4062 FixedArray::kHeaderSize + kPointerSize));
4063 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
4064 __ mov(Operand::StaticVariable(virtual_register), weak_cell);
4065 __ pop(weak_cell); // Pop "value".
4066 // jump to the handler.
4067 __ jmp(Operand::StaticVariable(virtual_register));
4068}
4069
4070
4071void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4072 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
4073 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
4074 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
4075 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
4076 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
4077 Label miss;
4078
4079 __ push(value);
4080
4081 Register scratch = value;
4082 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4083 FixedArray::kHeaderSize));
4084
4085 // Is it a weak cell?
4086 Label try_array;
4087 Label not_array, smi_key, key_okay;
4088 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
4089 __ j(not_equal, &try_array);
4090 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
4091
4092 // Is it a fixed array?
4093 __ bind(&try_array);
4094 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
4095 __ j(not_equal, &not_array);
4096 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, true,
4097 &miss);
4098
4099 __ bind(&not_array);
4100 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4101 __ j(not_equal, &miss);
4102
4103 __ pop(value);
4104 __ push(slot);
4105 __ push(vector);
4106 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4107 Code::ComputeHandlerFlags(Code::STORE_IC));
4108 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::STORE_IC, code_flags,
4109 receiver, key, slot, no_reg);
4110 __ pop(vector);
4111 __ pop(slot);
4112 Label no_pop_miss;
4113 __ jmp(&no_pop_miss);
4114
4115 __ bind(&miss);
4116 __ pop(value);
4117 __ bind(&no_pop_miss);
4118 StoreIC::GenerateMiss(masm);
4119}
4120
4121
4122void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4123 GenerateImpl(masm, false);
4124}
4125
4126
4127void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4128 GenerateImpl(masm, true);
4129}
4130
4131
4132static void HandlePolymorphicKeyedStoreCase(MacroAssembler* masm,
4133 Register receiver, Register key,
4134 Register vector, Register slot,
4135 Register feedback, Label* miss) {
4136 // feedback initially contains the feedback array
4137 Label next, next_loop, prepare_next;
4138 Label load_smi_map, compare_map;
4139 Label transition_call;
4140 Label pop_and_miss;
4141 ExternalReference virtual_register =
4142 ExternalReference::virtual_handler_register(masm->isolate());
4143 ExternalReference virtual_slot =
4144 ExternalReference::virtual_slot_register(masm->isolate());
4145
4146 __ push(receiver);
4147 __ push(vector);
4148
4149 Register receiver_map = receiver;
4150 Register cached_map = vector;
4151 Register value = StoreDescriptor::ValueRegister();
4152
4153 // Receiver might not be a heap object.
4154 __ JumpIfSmi(receiver, &load_smi_map);
4155 __ mov(receiver_map, FieldOperand(receiver, 0));
4156 __ bind(&compare_map);
4157
4158 // Polymorphic, we have to loop from 0 to N - 1
4159 __ push(key);
4160 // Current stack layout:
4161 // - esp[0] -- key
4162 // - esp[4] -- vector
4163 // - esp[8] -- receiver
4164 // - esp[12] -- value
4165 // - esp[16] -- return address
4166 //
4167 // Required stack layout for handler call:
4168 // - esp[0] -- return address
4169 // - receiver, key, value, vector, slot in registers.
4170 // - handler in virtual register.
4171 Register counter = key;
4172 __ mov(counter, Immediate(Smi::FromInt(0)));
4173 __ bind(&next_loop);
4174 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4175 FixedArray::kHeaderSize));
4176 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4177 __ j(not_equal, &prepare_next);
4178 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
4179 FixedArray::kHeaderSize + kPointerSize));
4180 __ CompareRoot(cached_map, Heap::kUndefinedValueRootIndex);
4181 __ j(not_equal, &transition_call);
4182 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
4183 FixedArray::kHeaderSize + 2 * kPointerSize));
4184 __ pop(key);
4185 __ pop(vector);
4186 __ pop(receiver);
4187 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
4188 __ mov(Operand::StaticVariable(virtual_register), feedback);
4189 __ pop(value);
4190 __ jmp(Operand::StaticVariable(virtual_register));
4191
4192 __ bind(&transition_call);
4193 // Current stack layout:
4194 // - esp[0] -- key
4195 // - esp[4] -- vector
4196 // - esp[8] -- receiver
4197 // - esp[12] -- value
4198 // - esp[16] -- return address
4199 //
4200 // Required stack layout for handler call:
4201 // - esp[0] -- return address
4202 // - receiver, key, value, map, vector in registers.
4203 // - handler and slot in virtual registers.
4204 __ mov(Operand::StaticVariable(virtual_slot), slot);
4205 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
4206 FixedArray::kHeaderSize + 2 * kPointerSize));
4207 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
4208 __ mov(Operand::StaticVariable(virtual_register), feedback);
4209
4210 __ mov(cached_map, FieldOperand(cached_map, WeakCell::kValueOffset));
4211 // The weak cell may have been cleared.
4212 __ JumpIfSmi(cached_map, &pop_and_miss);
4213 DCHECK(!cached_map.is(VectorStoreTransitionDescriptor::MapRegister()));
4214 __ mov(VectorStoreTransitionDescriptor::MapRegister(), cached_map);
4215
4216 // Pop key into place.
4217 __ pop(key);
4218 __ pop(vector);
4219 __ pop(receiver);
4220 __ pop(value);
4221 __ jmp(Operand::StaticVariable(virtual_register));
4222
4223 __ bind(&prepare_next);
4224 __ add(counter, Immediate(Smi::FromInt(3)));
4225 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
4226 __ j(less, &next_loop);
4227
4228 // We exhausted our array of map handler pairs.
4229 __ bind(&pop_and_miss);
4230 __ pop(key);
4231 __ pop(vector);
4232 __ pop(receiver);
4233 __ jmp(miss);
4234
4235 __ bind(&load_smi_map);
4236 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4237 __ jmp(&compare_map);
4238}
4239
4240
4241void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4242 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
4243 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
4244 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
4245 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
4246 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
4247 Label miss;
4248
4249 __ push(value);
4250
4251 Register scratch = value;
4252 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4253 FixedArray::kHeaderSize));
4254
4255 // Is it a weak cell?
4256 Label try_array;
4257 Label not_array, smi_key, key_okay;
4258 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
4259 __ j(not_equal, &try_array);
4260 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
4261
4262 // Is it a fixed array?
4263 __ bind(&try_array);
4264 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
4265 __ j(not_equal, &not_array);
4266 HandlePolymorphicKeyedStoreCase(masm, receiver, key, vector, slot, scratch,
4267 &miss);
4268
4269 __ bind(&not_array);
4270 Label try_poly_name;
4271 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4272 __ j(not_equal, &try_poly_name);
4273
4274 __ pop(value);
4275
4276 Handle<Code> megamorphic_stub =
4277 KeyedStoreIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4278 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4279
4280 __ bind(&try_poly_name);
4281 // We might have a name in feedback, and a fixed array in the next slot.
4282 __ cmp(key, scratch);
4283 __ j(not_equal, &miss);
4284 // If the name comparison succeeded, we know we have a fixed array with
4285 // at least one map/handler pair.
4286 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4287 FixedArray::kHeaderSize + kPointerSize));
4288 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, false,
4289 &miss);
4290
4291 __ bind(&miss);
4292 __ pop(value);
4293 KeyedStoreIC::GenerateMiss(masm);
4294}
4295
4296
4297void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4298 __ EmitLoadTypeFeedbackVector(ebx);
4299 CallICStub stub(isolate(), state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004300 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4301}
4302
4303
4304void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4305 if (masm->isolate()->function_entry_hook() != NULL) {
4306 ProfileEntryHookStub stub(masm->isolate());
4307 masm->CallStub(&stub);
4308 }
4309}
4310
4311
4312void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4313 // Save volatile registers.
4314 const int kNumSavedRegisters = 3;
4315 __ push(eax);
4316 __ push(ecx);
4317 __ push(edx);
4318
4319 // Calculate and push the original stack pointer.
4320 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4321 __ push(eax);
4322
4323 // Retrieve our return address and use it to calculate the calling
4324 // function's address.
4325 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4326 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
4327 __ push(eax);
4328
4329 // Call the entry hook.
4330 DCHECK(isolate()->function_entry_hook() != NULL);
4331 __ call(FUNCTION_ADDR(isolate()->function_entry_hook()),
4332 RelocInfo::RUNTIME_ENTRY);
4333 __ add(esp, Immediate(2 * kPointerSize));
4334
4335 // Restore ecx.
4336 __ pop(edx);
4337 __ pop(ecx);
4338 __ pop(eax);
4339
4340 __ ret(0);
4341}
4342
4343
4344template<class T>
4345static void CreateArrayDispatch(MacroAssembler* masm,
4346 AllocationSiteOverrideMode mode) {
4347 if (mode == DISABLE_ALLOCATION_SITES) {
4348 T stub(masm->isolate(),
4349 GetInitialFastElementsKind(),
4350 mode);
4351 __ TailCallStub(&stub);
4352 } else if (mode == DONT_OVERRIDE) {
4353 int last_index = GetSequenceIndexFromFastElementsKind(
4354 TERMINAL_FAST_ELEMENTS_KIND);
4355 for (int i = 0; i <= last_index; ++i) {
4356 Label next;
4357 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4358 __ cmp(edx, kind);
4359 __ j(not_equal, &next);
4360 T stub(masm->isolate(), kind);
4361 __ TailCallStub(&stub);
4362 __ bind(&next);
4363 }
4364
4365 // If we reached this point there is a problem.
4366 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4367 } else {
4368 UNREACHABLE();
4369 }
4370}
4371
4372
4373static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4374 AllocationSiteOverrideMode mode) {
4375 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4376 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
4377 // eax - number of arguments
4378 // edi - constructor?
4379 // esp[0] - return address
4380 // esp[4] - last argument
4381 Label normal_sequence;
4382 if (mode == DONT_OVERRIDE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004383 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4384 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4385 STATIC_ASSERT(FAST_ELEMENTS == 2);
4386 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4387 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4388 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004389
4390 // is the low bit set? If so, we are holey and that is good.
Ben Murdochda12d292016-06-02 14:46:10 +01004391 __ test_b(edx, Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004392 __ j(not_zero, &normal_sequence);
4393 }
4394
4395 // look at the first argument
4396 __ mov(ecx, Operand(esp, kPointerSize));
4397 __ test(ecx, ecx);
4398 __ j(zero, &normal_sequence);
4399
4400 if (mode == DISABLE_ALLOCATION_SITES) {
4401 ElementsKind initial = GetInitialFastElementsKind();
4402 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4403
4404 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4405 holey_initial,
4406 DISABLE_ALLOCATION_SITES);
4407 __ TailCallStub(&stub_holey);
4408
4409 __ bind(&normal_sequence);
4410 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4411 initial,
4412 DISABLE_ALLOCATION_SITES);
4413 __ TailCallStub(&stub);
4414 } else if (mode == DONT_OVERRIDE) {
4415 // We are going to create a holey array, but our kind is non-holey.
4416 // Fix kind and retry.
4417 __ inc(edx);
4418
4419 if (FLAG_debug_code) {
4420 Handle<Map> allocation_site_map =
4421 masm->isolate()->factory()->allocation_site_map();
4422 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
4423 __ Assert(equal, kExpectedAllocationSite);
4424 }
4425
4426 // Save the resulting elements kind in type info. We can't just store r3
4427 // in the AllocationSite::transition_info field because elements kind is
4428 // restricted to a portion of the field...upper bits need to be left alone.
4429 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4430 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
4431 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
4432
4433 __ bind(&normal_sequence);
4434 int last_index = GetSequenceIndexFromFastElementsKind(
4435 TERMINAL_FAST_ELEMENTS_KIND);
4436 for (int i = 0; i <= last_index; ++i) {
4437 Label next;
4438 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4439 __ cmp(edx, kind);
4440 __ j(not_equal, &next);
4441 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4442 __ TailCallStub(&stub);
4443 __ bind(&next);
4444 }
4445
4446 // If we reached this point there is a problem.
4447 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4448 } else {
4449 UNREACHABLE();
4450 }
4451}
4452
4453
4454template<class T>
4455static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4456 int to_index = GetSequenceIndexFromFastElementsKind(
4457 TERMINAL_FAST_ELEMENTS_KIND);
4458 for (int i = 0; i <= to_index; ++i) {
4459 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4460 T stub(isolate, kind);
4461 stub.GetCode();
4462 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4463 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4464 stub1.GetCode();
4465 }
4466 }
4467}
4468
4469
4470void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4471 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4472 isolate);
4473 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4474 isolate);
4475 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4476 isolate);
4477}
4478
4479
4480void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4481 Isolate* isolate) {
4482 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4483 for (int i = 0; i < 2; i++) {
4484 // For internal arrays we only need a few things
4485 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4486 stubh1.GetCode();
4487 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4488 stubh2.GetCode();
4489 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4490 stubh3.GetCode();
4491 }
4492}
4493
4494
4495void ArrayConstructorStub::GenerateDispatchToArrayStub(
4496 MacroAssembler* masm,
4497 AllocationSiteOverrideMode mode) {
4498 if (argument_count() == ANY) {
4499 Label not_zero_case, not_one_case;
4500 __ test(eax, eax);
4501 __ j(not_zero, &not_zero_case);
4502 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4503
4504 __ bind(&not_zero_case);
4505 __ cmp(eax, 1);
4506 __ j(greater, &not_one_case);
4507 CreateArrayDispatchOneArgument(masm, mode);
4508
4509 __ bind(&not_one_case);
4510 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4511 } else if (argument_count() == NONE) {
4512 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4513 } else if (argument_count() == ONE) {
4514 CreateArrayDispatchOneArgument(masm, mode);
4515 } else if (argument_count() == MORE_THAN_ONE) {
4516 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4517 } else {
4518 UNREACHABLE();
4519 }
4520}
4521
4522
4523void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4524 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004525 // -- eax : argc (only if argument_count() is ANY or MORE_THAN_ONE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004526 // -- ebx : AllocationSite or undefined
4527 // -- edi : constructor
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004528 // -- edx : Original constructor
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004529 // -- esp[0] : return address
4530 // -- esp[4] : last argument
4531 // -----------------------------------
4532 if (FLAG_debug_code) {
4533 // The array construct code is only set for the global and natives
4534 // builtin Array functions which always have maps.
4535
4536 // Initial map for the builtin Array function should be a map.
4537 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4538 // Will both indicate a NULL and a Smi.
4539 __ test(ecx, Immediate(kSmiTagMask));
4540 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4541 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4542 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4543
4544 // We should either have undefined in ebx or a valid AllocationSite
4545 __ AssertUndefinedOrAllocationSite(ebx);
4546 }
4547
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004548 Label subclassing;
4549
4550 // Enter the context of the Array function.
4551 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
4552
4553 __ cmp(edx, edi);
4554 __ j(not_equal, &subclassing);
4555
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004556 Label no_info;
4557 // If the feedback vector is the undefined value call an array constructor
4558 // that doesn't use AllocationSites.
4559 __ cmp(ebx, isolate()->factory()->undefined_value());
4560 __ j(equal, &no_info);
4561
4562 // Only look at the lower 16 bits of the transition info.
4563 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
4564 __ SmiUntag(edx);
4565 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4566 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
4567 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4568
4569 __ bind(&no_info);
4570 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004571
4572 // Subclassing.
4573 __ bind(&subclassing);
4574 switch (argument_count()) {
4575 case ANY:
4576 case MORE_THAN_ONE:
4577 __ mov(Operand(esp, eax, times_pointer_size, kPointerSize), edi);
4578 __ add(eax, Immediate(3));
4579 break;
4580 case NONE:
4581 __ mov(Operand(esp, 1 * kPointerSize), edi);
4582 __ mov(eax, Immediate(3));
4583 break;
4584 case ONE:
4585 __ mov(Operand(esp, 2 * kPointerSize), edi);
4586 __ mov(eax, Immediate(4));
4587 break;
4588 }
4589 __ PopReturnAddressTo(ecx);
4590 __ Push(edx);
4591 __ Push(ebx);
4592 __ PushReturnAddressFrom(ecx);
4593 __ JumpToExternalReference(ExternalReference(Runtime::kNewArray, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004594}
4595
4596
4597void InternalArrayConstructorStub::GenerateCase(
4598 MacroAssembler* masm, ElementsKind kind) {
4599 Label not_zero_case, not_one_case;
4600 Label normal_sequence;
4601
4602 __ test(eax, eax);
4603 __ j(not_zero, &not_zero_case);
4604 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4605 __ TailCallStub(&stub0);
4606
4607 __ bind(&not_zero_case);
4608 __ cmp(eax, 1);
4609 __ j(greater, &not_one_case);
4610
4611 if (IsFastPackedElementsKind(kind)) {
4612 // We might need to create a holey array
4613 // look at the first argument
4614 __ mov(ecx, Operand(esp, kPointerSize));
4615 __ test(ecx, ecx);
4616 __ j(zero, &normal_sequence);
4617
4618 InternalArraySingleArgumentConstructorStub
4619 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4620 __ TailCallStub(&stub1_holey);
4621 }
4622
4623 __ bind(&normal_sequence);
4624 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4625 __ TailCallStub(&stub1);
4626
4627 __ bind(&not_one_case);
4628 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4629 __ TailCallStub(&stubN);
4630}
4631
4632
4633void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4634 // ----------- S t a t e -------------
4635 // -- eax : argc
4636 // -- edi : constructor
4637 // -- esp[0] : return address
4638 // -- esp[4] : last argument
4639 // -----------------------------------
4640
4641 if (FLAG_debug_code) {
4642 // The array construct code is only set for the global and natives
4643 // builtin Array functions which always have maps.
4644
4645 // Initial map for the builtin Array function should be a map.
4646 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4647 // Will both indicate a NULL and a Smi.
4648 __ test(ecx, Immediate(kSmiTagMask));
4649 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4650 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4651 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4652 }
4653
4654 // Figure out the right elements kind
4655 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4656
4657 // Load the map's "bit field 2" into |result|. We only need the first byte,
4658 // but the following masking takes care of that anyway.
4659 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
4660 // Retrieve elements_kind from bit field 2.
4661 __ DecodeField<Map::ElementsKindBits>(ecx);
4662
4663 if (FLAG_debug_code) {
4664 Label done;
4665 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4666 __ j(equal, &done);
4667 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
4668 __ Assert(equal,
4669 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
4670 __ bind(&done);
4671 }
4672
4673 Label fast_elements_case;
4674 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4675 __ j(equal, &fast_elements_case);
4676 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4677
4678 __ bind(&fast_elements_case);
4679 GenerateCase(masm, FAST_ELEMENTS);
4680}
4681
4682
Ben Murdoch097c5b22016-05-18 11:27:45 +01004683void FastNewObjectStub::Generate(MacroAssembler* masm) {
4684 // ----------- S t a t e -------------
4685 // -- edi : target
4686 // -- edx : new target
4687 // -- esi : context
4688 // -- esp[0] : return address
4689 // -----------------------------------
4690 __ AssertFunction(edi);
4691 __ AssertReceiver(edx);
4692
4693 // Verify that the new target is a JSFunction.
4694 Label new_object;
4695 __ CmpObjectType(edx, JS_FUNCTION_TYPE, ebx);
4696 __ j(not_equal, &new_object);
4697
4698 // Load the initial map and verify that it's in fact a map.
4699 __ mov(ecx, FieldOperand(edx, JSFunction::kPrototypeOrInitialMapOffset));
4700 __ JumpIfSmi(ecx, &new_object);
4701 __ CmpObjectType(ecx, MAP_TYPE, ebx);
4702 __ j(not_equal, &new_object);
4703
4704 // Fall back to runtime if the target differs from the new target's
4705 // initial map constructor.
4706 __ cmp(edi, FieldOperand(ecx, Map::kConstructorOrBackPointerOffset));
4707 __ j(not_equal, &new_object);
4708
4709 // Allocate the JSObject on the heap.
4710 Label allocate, done_allocate;
4711 __ movzx_b(ebx, FieldOperand(ecx, Map::kInstanceSizeOffset));
4712 __ lea(ebx, Operand(ebx, times_pointer_size, 0));
4713 __ Allocate(ebx, eax, edi, no_reg, &allocate, NO_ALLOCATION_FLAGS);
4714 __ bind(&done_allocate);
4715
4716 // Initialize the JSObject fields.
4717 __ mov(Operand(eax, JSObject::kMapOffset), ecx);
4718 __ mov(Operand(eax, JSObject::kPropertiesOffset),
4719 masm->isolate()->factory()->empty_fixed_array());
4720 __ mov(Operand(eax, JSObject::kElementsOffset),
4721 masm->isolate()->factory()->empty_fixed_array());
4722 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
4723 __ lea(ebx, Operand(eax, JSObject::kHeaderSize));
4724
4725 // ----------- S t a t e -------------
4726 // -- eax : result (untagged)
4727 // -- ebx : result fields (untagged)
4728 // -- edi : result end (untagged)
4729 // -- ecx : initial map
4730 // -- esi : context
4731 // -- esp[0] : return address
4732 // -----------------------------------
4733
4734 // Perform in-object slack tracking if requested.
4735 Label slack_tracking;
4736 STATIC_ASSERT(Map::kNoSlackTracking == 0);
4737 __ test(FieldOperand(ecx, Map::kBitField3Offset),
4738 Immediate(Map::ConstructionCounter::kMask));
4739 __ j(not_zero, &slack_tracking, Label::kNear);
4740 {
4741 // Initialize all in-object fields with undefined.
4742 __ LoadRoot(edx, Heap::kUndefinedValueRootIndex);
4743 __ InitializeFieldsWithFiller(ebx, edi, edx);
4744
4745 // Add the object tag to make the JSObject real.
4746 STATIC_ASSERT(kHeapObjectTag == 1);
4747 __ inc(eax);
4748 __ Ret();
4749 }
4750 __ bind(&slack_tracking);
4751 {
4752 // Decrease generous allocation count.
4753 STATIC_ASSERT(Map::ConstructionCounter::kNext == 32);
4754 __ sub(FieldOperand(ecx, Map::kBitField3Offset),
4755 Immediate(1 << Map::ConstructionCounter::kShift));
4756
4757 // Initialize the in-object fields with undefined.
4758 __ movzx_b(edx, FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset));
4759 __ neg(edx);
4760 __ lea(edx, Operand(edi, edx, times_pointer_size, 0));
4761 __ LoadRoot(edi, Heap::kUndefinedValueRootIndex);
4762 __ InitializeFieldsWithFiller(ebx, edx, edi);
4763
4764 // Initialize the remaining (reserved) fields with one pointer filler map.
4765 __ movzx_b(edx, FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset));
4766 __ lea(edx, Operand(ebx, edx, times_pointer_size, 0));
4767 __ LoadRoot(edi, Heap::kOnePointerFillerMapRootIndex);
4768 __ InitializeFieldsWithFiller(ebx, edx, edi);
4769
4770 // Add the object tag to make the JSObject real.
4771 STATIC_ASSERT(kHeapObjectTag == 1);
4772 __ inc(eax);
4773
4774 // Check if we can finalize the instance size.
4775 Label finalize;
4776 STATIC_ASSERT(Map::kSlackTrackingCounterEnd == 1);
4777 __ test(FieldOperand(ecx, Map::kBitField3Offset),
4778 Immediate(Map::ConstructionCounter::kMask));
4779 __ j(zero, &finalize, Label::kNear);
4780 __ Ret();
4781
4782 // Finalize the instance size.
4783 __ bind(&finalize);
4784 {
4785 FrameScope scope(masm, StackFrame::INTERNAL);
4786 __ Push(eax);
4787 __ Push(ecx);
4788 __ CallRuntime(Runtime::kFinalizeInstanceSize);
4789 __ Pop(eax);
4790 }
4791 __ Ret();
4792 }
4793
4794 // Fall back to %AllocateInNewSpace.
4795 __ bind(&allocate);
4796 {
4797 FrameScope scope(masm, StackFrame::INTERNAL);
4798 __ SmiTag(ebx);
4799 __ Push(ecx);
4800 __ Push(ebx);
4801 __ CallRuntime(Runtime::kAllocateInNewSpace);
4802 __ Pop(ecx);
4803 }
4804 STATIC_ASSERT(kHeapObjectTag == 1);
4805 __ dec(eax);
4806 __ movzx_b(ebx, FieldOperand(ecx, Map::kInstanceSizeOffset));
4807 __ lea(edi, Operand(eax, ebx, times_pointer_size, 0));
4808 __ jmp(&done_allocate);
4809
4810 // Fall back to %NewObject.
4811 __ bind(&new_object);
4812 __ PopReturnAddressTo(ecx);
4813 __ Push(edi);
4814 __ Push(edx);
4815 __ PushReturnAddressFrom(ecx);
4816 __ TailCallRuntime(Runtime::kNewObject);
4817}
4818
4819
4820void FastNewRestParameterStub::Generate(MacroAssembler* masm) {
4821 // ----------- S t a t e -------------
4822 // -- edi : function
4823 // -- esi : context
4824 // -- ebp : frame pointer
4825 // -- esp[0] : return address
4826 // -----------------------------------
4827 __ AssertFunction(edi);
4828
4829 // For Ignition we need to skip all possible handler/stub frames until
4830 // we reach the JavaScript frame for the function (similar to what the
4831 // runtime fallback implementation does). So make edx point to that
4832 // JavaScript frame.
4833 {
4834 Label loop, loop_entry;
4835 __ mov(edx, ebp);
4836 __ jmp(&loop_entry, Label::kNear);
4837 __ bind(&loop);
4838 __ mov(edx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
4839 __ bind(&loop_entry);
Ben Murdochda12d292016-06-02 14:46:10 +01004840 __ cmp(edi, Operand(edx, StandardFrameConstants::kFunctionOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004841 __ j(not_equal, &loop);
4842 }
4843
4844 // Check if we have rest parameters (only possible if we have an
4845 // arguments adaptor frame below the function frame).
4846 Label no_rest_parameters;
4847 __ mov(ebx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004848 __ cmp(Operand(ebx, CommonFrameConstants::kContextOrFrameTypeOffset),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004849 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4850 __ j(not_equal, &no_rest_parameters, Label::kNear);
4851
4852 // Check if the arguments adaptor frame contains more arguments than
4853 // specified by the function's internal formal parameter count.
4854 Label rest_parameters;
4855 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4856 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4857 __ sub(eax,
4858 FieldOperand(ecx, SharedFunctionInfo::kFormalParameterCountOffset));
4859 __ j(greater, &rest_parameters);
4860
4861 // Return an empty rest parameter array.
4862 __ bind(&no_rest_parameters);
4863 {
4864 // ----------- S t a t e -------------
4865 // -- esi : context
4866 // -- esp[0] : return address
4867 // -----------------------------------
4868
4869 // Allocate an empty rest parameter array.
4870 Label allocate, done_allocate;
4871 __ Allocate(JSArray::kSize, eax, edx, ecx, &allocate, TAG_OBJECT);
4872 __ bind(&done_allocate);
4873
4874 // Setup the rest parameter array in rax.
4875 __ LoadGlobalFunction(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, ecx);
4876 __ mov(FieldOperand(eax, JSArray::kMapOffset), ecx);
4877 __ mov(ecx, isolate()->factory()->empty_fixed_array());
4878 __ mov(FieldOperand(eax, JSArray::kPropertiesOffset), ecx);
4879 __ mov(FieldOperand(eax, JSArray::kElementsOffset), ecx);
4880 __ mov(FieldOperand(eax, JSArray::kLengthOffset),
4881 Immediate(Smi::FromInt(0)));
4882 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4883 __ Ret();
4884
4885 // Fall back to %AllocateInNewSpace.
4886 __ bind(&allocate);
4887 {
4888 FrameScope scope(masm, StackFrame::INTERNAL);
4889 __ Push(Smi::FromInt(JSArray::kSize));
4890 __ CallRuntime(Runtime::kAllocateInNewSpace);
4891 }
4892 __ jmp(&done_allocate);
4893 }
4894
4895 __ bind(&rest_parameters);
4896 {
4897 // Compute the pointer to the first rest parameter (skippping the receiver).
4898 __ lea(ebx,
4899 Operand(ebx, eax, times_half_pointer_size,
4900 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4901
4902 // ----------- S t a t e -------------
4903 // -- esi : context
4904 // -- eax : number of rest parameters (tagged)
4905 // -- ebx : pointer to first rest parameters
4906 // -- esp[0] : return address
4907 // -----------------------------------
4908
4909 // Allocate space for the rest parameter array plus the backing store.
4910 Label allocate, done_allocate;
4911 __ lea(ecx, Operand(eax, times_half_pointer_size,
4912 JSArray::kSize + FixedArray::kHeaderSize));
4913 __ Allocate(ecx, edx, edi, no_reg, &allocate, TAG_OBJECT);
4914 __ bind(&done_allocate);
4915
4916 // Setup the elements array in edx.
4917 __ mov(FieldOperand(edx, FixedArray::kMapOffset),
4918 isolate()->factory()->fixed_array_map());
4919 __ mov(FieldOperand(edx, FixedArray::kLengthOffset), eax);
4920 {
4921 Label loop, done_loop;
4922 __ Move(ecx, Smi::FromInt(0));
4923 __ bind(&loop);
4924 __ cmp(ecx, eax);
4925 __ j(equal, &done_loop, Label::kNear);
4926 __ mov(edi, Operand(ebx, 0 * kPointerSize));
4927 __ mov(FieldOperand(edx, ecx, times_half_pointer_size,
4928 FixedArray::kHeaderSize),
4929 edi);
4930 __ sub(ebx, Immediate(1 * kPointerSize));
4931 __ add(ecx, Immediate(Smi::FromInt(1)));
4932 __ jmp(&loop);
4933 __ bind(&done_loop);
4934 }
4935
4936 // Setup the rest parameter array in edi.
4937 __ lea(edi,
4938 Operand(edx, eax, times_half_pointer_size, FixedArray::kHeaderSize));
4939 __ LoadGlobalFunction(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, ecx);
4940 __ mov(FieldOperand(edi, JSArray::kMapOffset), ecx);
4941 __ mov(FieldOperand(edi, JSArray::kPropertiesOffset),
4942 isolate()->factory()->empty_fixed_array());
4943 __ mov(FieldOperand(edi, JSArray::kElementsOffset), edx);
4944 __ mov(FieldOperand(edi, JSArray::kLengthOffset), eax);
4945 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4946 __ mov(eax, edi);
4947 __ Ret();
4948
4949 // Fall back to %AllocateInNewSpace.
4950 __ bind(&allocate);
4951 {
4952 FrameScope scope(masm, StackFrame::INTERNAL);
4953 __ SmiTag(ecx);
4954 __ Push(eax);
4955 __ Push(ebx);
4956 __ Push(ecx);
4957 __ CallRuntime(Runtime::kAllocateInNewSpace);
4958 __ mov(edx, eax);
4959 __ Pop(ebx);
4960 __ Pop(eax);
4961 }
4962 __ jmp(&done_allocate);
4963 }
4964}
4965
4966
4967void FastNewSloppyArgumentsStub::Generate(MacroAssembler* masm) {
4968 // ----------- S t a t e -------------
4969 // -- edi : function
4970 // -- esi : context
4971 // -- ebp : frame pointer
4972 // -- esp[0] : return address
4973 // -----------------------------------
4974 __ AssertFunction(edi);
4975
4976 // TODO(bmeurer): Cleanup to match the FastNewStrictArgumentsStub.
4977 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4978 __ mov(ecx,
4979 FieldOperand(ecx, SharedFunctionInfo::kFormalParameterCountOffset));
4980 __ lea(edx, Operand(ebp, ecx, times_half_pointer_size,
4981 StandardFrameConstants::kCallerSPOffset));
4982
4983 // ecx : number of parameters (tagged)
4984 // edx : parameters pointer
4985 // edi : function
4986 // esp[0] : return address
4987
4988 // Check if the calling frame is an arguments adaptor frame.
4989 Label adaptor_frame, try_allocate, runtime;
4990 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004991 __ mov(eax, Operand(ebx, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004992 __ cmp(eax, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4993 __ j(equal, &adaptor_frame, Label::kNear);
4994
4995 // No adaptor, parameter count = argument count.
4996 __ mov(ebx, ecx);
4997 __ push(ecx);
4998 __ jmp(&try_allocate, Label::kNear);
4999
5000 // We have an adaptor frame. Patch the parameters pointer.
5001 __ bind(&adaptor_frame);
5002 __ mov(ebx, ecx);
5003 __ push(ecx);
5004 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
5005 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
5006 __ lea(edx, Operand(edx, ecx, times_2,
5007 StandardFrameConstants::kCallerSPOffset));
5008
5009 // ebx = parameter count (tagged)
5010 // ecx = argument count (smi-tagged)
5011 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
5012 __ cmp(ebx, ecx);
5013 __ j(less_equal, &try_allocate, Label::kNear);
5014 __ mov(ebx, ecx);
5015
5016 // Save mapped parameter count and function.
5017 __ bind(&try_allocate);
5018 __ push(edi);
5019 __ push(ebx);
5020
5021 // Compute the sizes of backing store, parameter map, and arguments object.
5022 // 1. Parameter map, has 2 extra words containing context and backing store.
5023 const int kParameterMapHeaderSize =
5024 FixedArray::kHeaderSize + 2 * kPointerSize;
5025 Label no_parameter_map;
5026 __ test(ebx, ebx);
5027 __ j(zero, &no_parameter_map, Label::kNear);
5028 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
5029 __ bind(&no_parameter_map);
5030
5031 // 2. Backing store.
5032 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
5033
5034 // 3. Arguments object.
5035 __ add(ebx, Immediate(JSSloppyArgumentsObject::kSize));
5036
5037 // Do the allocation of all three objects in one go.
5038 __ Allocate(ebx, eax, edi, no_reg, &runtime, TAG_OBJECT);
5039
5040 // eax = address of new object(s) (tagged)
5041 // ecx = argument count (smi-tagged)
5042 // esp[0] = mapped parameter count (tagged)
5043 // esp[4] = function
5044 // esp[8] = parameter count (tagged)
5045 // Get the arguments map from the current native context into edi.
5046 Label has_mapped_parameters, instantiate;
5047 __ mov(edi, NativeContextOperand());
5048 __ mov(ebx, Operand(esp, 0 * kPointerSize));
5049 __ test(ebx, ebx);
5050 __ j(not_zero, &has_mapped_parameters, Label::kNear);
5051 __ mov(
5052 edi,
5053 Operand(edi, Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX)));
5054 __ jmp(&instantiate, Label::kNear);
5055
5056 __ bind(&has_mapped_parameters);
5057 __ mov(edi, Operand(edi, Context::SlotOffset(
5058 Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX)));
5059 __ bind(&instantiate);
5060
5061 // eax = address of new object (tagged)
5062 // ebx = mapped parameter count (tagged)
5063 // ecx = argument count (smi-tagged)
5064 // edi = address of arguments map (tagged)
5065 // esp[0] = mapped parameter count (tagged)
5066 // esp[4] = function
5067 // esp[8] = parameter count (tagged)
5068 // Copy the JS object part.
5069 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
5070 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
5071 masm->isolate()->factory()->empty_fixed_array());
5072 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
5073 masm->isolate()->factory()->empty_fixed_array());
5074
5075 // Set up the callee in-object property.
5076 STATIC_ASSERT(JSSloppyArgumentsObject::kCalleeIndex == 1);
5077 __ mov(edi, Operand(esp, 1 * kPointerSize));
5078 __ AssertNotSmi(edi);
5079 __ mov(FieldOperand(eax, JSSloppyArgumentsObject::kCalleeOffset), edi);
5080
5081 // Use the length (smi tagged) and set that as an in-object property too.
5082 __ AssertSmi(ecx);
5083 __ mov(FieldOperand(eax, JSSloppyArgumentsObject::kLengthOffset), ecx);
5084
5085 // Set up the elements pointer in the allocated arguments object.
5086 // If we allocated a parameter map, edi will point there, otherwise to the
5087 // backing store.
5088 __ lea(edi, Operand(eax, JSSloppyArgumentsObject::kSize));
5089 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
5090
5091 // eax = address of new object (tagged)
5092 // ebx = mapped parameter count (tagged)
5093 // ecx = argument count (tagged)
5094 // edx = address of receiver argument
5095 // edi = address of parameter map or backing store (tagged)
5096 // esp[0] = mapped parameter count (tagged)
5097 // esp[4] = function
5098 // esp[8] = parameter count (tagged)
5099 // Free two registers.
5100 __ push(edx);
5101 __ push(eax);
5102
5103 // Initialize parameter map. If there are no mapped arguments, we're done.
5104 Label skip_parameter_map;
5105 __ test(ebx, ebx);
5106 __ j(zero, &skip_parameter_map);
5107
5108 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
5109 Immediate(isolate()->factory()->sloppy_arguments_elements_map()));
5110 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
5111 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
5112 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
5113 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
5114 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
5115
5116 // Copy the parameter slots and the holes in the arguments.
5117 // We need to fill in mapped_parameter_count slots. They index the context,
5118 // where parameters are stored in reverse order, at
5119 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
5120 // The mapped parameter thus need to get indices
5121 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
5122 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
5123 // We loop from right to left.
5124 Label parameters_loop, parameters_test;
5125 __ push(ecx);
5126 __ mov(eax, Operand(esp, 3 * kPointerSize));
5127 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
5128 __ add(ebx, Operand(esp, 5 * kPointerSize));
5129 __ sub(ebx, eax);
5130 __ mov(ecx, isolate()->factory()->the_hole_value());
5131 __ mov(edx, edi);
5132 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
5133 // eax = loop variable (tagged)
5134 // ebx = mapping index (tagged)
5135 // ecx = the hole value
5136 // edx = address of parameter map (tagged)
5137 // edi = address of backing store (tagged)
5138 // esp[0] = argument count (tagged)
5139 // esp[4] = address of new object (tagged)
5140 // esp[8] = address of receiver argument
5141 // esp[12] = mapped parameter count (tagged)
5142 // esp[16] = function
5143 // esp[20] = parameter count (tagged)
5144 __ jmp(&parameters_test, Label::kNear);
5145
5146 __ bind(&parameters_loop);
5147 __ sub(eax, Immediate(Smi::FromInt(1)));
5148 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
5149 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
5150 __ add(ebx, Immediate(Smi::FromInt(1)));
5151 __ bind(&parameters_test);
5152 __ test(eax, eax);
5153 __ j(not_zero, &parameters_loop, Label::kNear);
5154 __ pop(ecx);
5155
5156 __ bind(&skip_parameter_map);
5157
5158 // ecx = argument count (tagged)
5159 // edi = address of backing store (tagged)
5160 // esp[0] = address of new object (tagged)
5161 // esp[4] = address of receiver argument
5162 // esp[8] = mapped parameter count (tagged)
5163 // esp[12] = function
5164 // esp[16] = parameter count (tagged)
5165 // Copy arguments header and remaining slots (if there are any).
5166 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
5167 Immediate(isolate()->factory()->fixed_array_map()));
5168 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
5169
5170 Label arguments_loop, arguments_test;
5171 __ mov(ebx, Operand(esp, 2 * kPointerSize));
5172 __ mov(edx, Operand(esp, 1 * kPointerSize));
5173 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
5174 __ sub(edx, ebx);
5175 __ jmp(&arguments_test, Label::kNear);
5176
5177 __ bind(&arguments_loop);
5178 __ sub(edx, Immediate(kPointerSize));
5179 __ mov(eax, Operand(edx, 0));
5180 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
5181 __ add(ebx, Immediate(Smi::FromInt(1)));
5182
5183 __ bind(&arguments_test);
5184 __ cmp(ebx, ecx);
5185 __ j(less, &arguments_loop, Label::kNear);
5186
5187 // Restore.
5188 __ pop(eax); // Address of arguments object.
5189 __ Drop(4);
5190
5191 // Return.
5192 __ ret(0);
5193
5194 // Do the runtime call to allocate the arguments object.
5195 __ bind(&runtime);
5196 __ pop(eax); // Remove saved mapped parameter count.
5197 __ pop(edi); // Pop saved function.
5198 __ pop(eax); // Remove saved parameter count.
5199 __ pop(eax); // Pop return address.
5200 __ push(edi); // Push function.
5201 __ push(edx); // Push parameters pointer.
5202 __ push(ecx); // Push parameter count.
5203 __ push(eax); // Push return address.
5204 __ TailCallRuntime(Runtime::kNewSloppyArguments);
5205}
5206
5207
5208void FastNewStrictArgumentsStub::Generate(MacroAssembler* masm) {
5209 // ----------- S t a t e -------------
5210 // -- edi : function
5211 // -- esi : context
5212 // -- ebp : frame pointer
5213 // -- esp[0] : return address
5214 // -----------------------------------
5215 __ AssertFunction(edi);
5216
5217 // For Ignition we need to skip all possible handler/stub frames until
5218 // we reach the JavaScript frame for the function (similar to what the
5219 // runtime fallback implementation does). So make edx point to that
5220 // JavaScript frame.
5221 {
5222 Label loop, loop_entry;
5223 __ mov(edx, ebp);
5224 __ jmp(&loop_entry, Label::kNear);
5225 __ bind(&loop);
5226 __ mov(edx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
5227 __ bind(&loop_entry);
Ben Murdochda12d292016-06-02 14:46:10 +01005228 __ cmp(edi, Operand(edx, StandardFrameConstants::kFunctionOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01005229 __ j(not_equal, &loop);
5230 }
5231
5232 // Check if we have an arguments adaptor frame below the function frame.
5233 Label arguments_adaptor, arguments_done;
5234 __ mov(ebx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01005235 __ cmp(Operand(ebx, CommonFrameConstants::kContextOrFrameTypeOffset),
Ben Murdoch097c5b22016-05-18 11:27:45 +01005236 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5237 __ j(equal, &arguments_adaptor, Label::kNear);
5238 {
5239 __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
5240 __ mov(eax,
5241 FieldOperand(eax, SharedFunctionInfo::kFormalParameterCountOffset));
5242 __ lea(ebx,
5243 Operand(edx, eax, times_half_pointer_size,
5244 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
5245 }
5246 __ jmp(&arguments_done, Label::kNear);
5247 __ bind(&arguments_adaptor);
5248 {
5249 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
5250 __ lea(ebx,
5251 Operand(ebx, eax, times_half_pointer_size,
5252 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
5253 }
5254 __ bind(&arguments_done);
5255
5256 // ----------- S t a t e -------------
5257 // -- eax : number of arguments (tagged)
5258 // -- ebx : pointer to the first argument
5259 // -- esi : context
5260 // -- esp[0] : return address
5261 // -----------------------------------
5262
5263 // Allocate space for the strict arguments object plus the backing store.
5264 Label allocate, done_allocate;
5265 __ lea(ecx,
5266 Operand(eax, times_half_pointer_size,
5267 JSStrictArgumentsObject::kSize + FixedArray::kHeaderSize));
5268 __ Allocate(ecx, edx, edi, no_reg, &allocate, TAG_OBJECT);
5269 __ bind(&done_allocate);
5270
5271 // Setup the elements array in edx.
5272 __ mov(FieldOperand(edx, FixedArray::kMapOffset),
5273 isolate()->factory()->fixed_array_map());
5274 __ mov(FieldOperand(edx, FixedArray::kLengthOffset), eax);
5275 {
5276 Label loop, done_loop;
5277 __ Move(ecx, Smi::FromInt(0));
5278 __ bind(&loop);
5279 __ cmp(ecx, eax);
5280 __ j(equal, &done_loop, Label::kNear);
5281 __ mov(edi, Operand(ebx, 0 * kPointerSize));
5282 __ mov(FieldOperand(edx, ecx, times_half_pointer_size,
5283 FixedArray::kHeaderSize),
5284 edi);
5285 __ sub(ebx, Immediate(1 * kPointerSize));
5286 __ add(ecx, Immediate(Smi::FromInt(1)));
5287 __ jmp(&loop);
5288 __ bind(&done_loop);
5289 }
5290
5291 // Setup the rest parameter array in edi.
5292 __ lea(edi,
5293 Operand(edx, eax, times_half_pointer_size, FixedArray::kHeaderSize));
5294 __ LoadGlobalFunction(Context::STRICT_ARGUMENTS_MAP_INDEX, ecx);
5295 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kMapOffset), ecx);
5296 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kPropertiesOffset),
5297 isolate()->factory()->empty_fixed_array());
5298 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kElementsOffset), edx);
5299 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kLengthOffset), eax);
5300 STATIC_ASSERT(JSStrictArgumentsObject::kSize == 4 * kPointerSize);
5301 __ mov(eax, edi);
5302 __ Ret();
5303
5304 // Fall back to %AllocateInNewSpace.
5305 __ bind(&allocate);
5306 {
5307 FrameScope scope(masm, StackFrame::INTERNAL);
5308 __ SmiTag(ecx);
5309 __ Push(eax);
5310 __ Push(ebx);
5311 __ Push(ecx);
5312 __ CallRuntime(Runtime::kAllocateInNewSpace);
5313 __ mov(edx, eax);
5314 __ Pop(ebx);
5315 __ Pop(eax);
5316 }
5317 __ jmp(&done_allocate);
5318}
5319
5320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005321void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
5322 Register context_reg = esi;
5323 Register slot_reg = ebx;
5324 Register result_reg = eax;
5325 Label slow_case;
5326
5327 // Go up context chain to the script context.
5328 for (int i = 0; i < depth(); ++i) {
5329 __ mov(result_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5330 context_reg = result_reg;
5331 }
5332
5333 // Load the PropertyCell value at the specified slot.
5334 __ mov(result_reg, ContextOperand(context_reg, slot_reg));
5335 __ mov(result_reg, FieldOperand(result_reg, PropertyCell::kValueOffset));
5336
5337 // Check that value is not the_hole.
5338 __ CompareRoot(result_reg, Heap::kTheHoleValueRootIndex);
5339 __ j(equal, &slow_case, Label::kNear);
5340 __ Ret();
5341
5342 // Fallback to the runtime.
5343 __ bind(&slow_case);
5344 __ SmiTag(slot_reg);
5345 __ Pop(result_reg); // Pop return address.
5346 __ Push(slot_reg);
5347 __ Push(result_reg); // Push return address.
5348 __ TailCallRuntime(Runtime::kLoadGlobalViaContext);
5349}
5350
5351
5352void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
5353 Register context_reg = esi;
5354 Register slot_reg = ebx;
5355 Register value_reg = eax;
5356 Register cell_reg = edi;
5357 Register cell_details_reg = edx;
5358 Register cell_value_reg = ecx;
5359 Label fast_heapobject_case, fast_smi_case, slow_case;
5360
5361 if (FLAG_debug_code) {
5362 __ CompareRoot(value_reg, Heap::kTheHoleValueRootIndex);
5363 __ Check(not_equal, kUnexpectedValue);
5364 }
5365
5366 // Go up context chain to the script context.
5367 for (int i = 0; i < depth(); ++i) {
5368 __ mov(cell_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5369 context_reg = cell_reg;
5370 }
5371
5372 // Load the PropertyCell at the specified slot.
5373 __ mov(cell_reg, ContextOperand(context_reg, slot_reg));
5374
5375 // Load PropertyDetails for the cell (actually only the cell_type and kind).
5376 __ mov(cell_details_reg,
5377 FieldOperand(cell_reg, PropertyCell::kDetailsOffset));
5378 __ SmiUntag(cell_details_reg);
5379 __ and_(cell_details_reg,
5380 Immediate(PropertyDetails::PropertyCellTypeField::kMask |
5381 PropertyDetails::KindField::kMask |
5382 PropertyDetails::kAttributesReadOnlyMask));
5383
5384 // Check if PropertyCell holds mutable data.
5385 Label not_mutable_data;
5386 __ cmp(cell_details_reg,
5387 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5388 PropertyCellType::kMutable) |
5389 PropertyDetails::KindField::encode(kData)));
5390 __ j(not_equal, &not_mutable_data);
5391 __ JumpIfSmi(value_reg, &fast_smi_case);
5392 __ bind(&fast_heapobject_case);
5393 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5394 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
5395 cell_details_reg, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
5396 OMIT_SMI_CHECK);
5397 // RecordWriteField clobbers the value register, so we need to reload.
5398 __ mov(value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5399 __ Ret();
5400 __ bind(&not_mutable_data);
5401
5402 // Check if PropertyCell value matches the new value (relevant for Constant,
5403 // ConstantType and Undefined cells).
5404 Label not_same_value;
5405 __ mov(cell_value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5406 __ cmp(cell_value_reg, value_reg);
5407 __ j(not_equal, &not_same_value,
5408 FLAG_debug_code ? Label::kFar : Label::kNear);
5409 // Make sure the PropertyCell is not marked READ_ONLY.
5410 __ test(cell_details_reg,
5411 Immediate(PropertyDetails::kAttributesReadOnlyMask));
5412 __ j(not_zero, &slow_case);
5413 if (FLAG_debug_code) {
5414 Label done;
5415 // This can only be true for Constant, ConstantType and Undefined cells,
5416 // because we never store the_hole via this stub.
5417 __ cmp(cell_details_reg,
5418 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5419 PropertyCellType::kConstant) |
5420 PropertyDetails::KindField::encode(kData)));
5421 __ j(equal, &done);
5422 __ cmp(cell_details_reg,
5423 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5424 PropertyCellType::kConstantType) |
5425 PropertyDetails::KindField::encode(kData)));
5426 __ j(equal, &done);
5427 __ cmp(cell_details_reg,
5428 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5429 PropertyCellType::kUndefined) |
5430 PropertyDetails::KindField::encode(kData)));
5431 __ Check(equal, kUnexpectedValue);
5432 __ bind(&done);
5433 }
5434 __ Ret();
5435 __ bind(&not_same_value);
5436
5437 // Check if PropertyCell contains data with constant type (and is not
5438 // READ_ONLY).
5439 __ cmp(cell_details_reg,
5440 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5441 PropertyCellType::kConstantType) |
5442 PropertyDetails::KindField::encode(kData)));
5443 __ j(not_equal, &slow_case, Label::kNear);
5444
5445 // Now either both old and new values must be SMIs or both must be heap
5446 // objects with same map.
5447 Label value_is_heap_object;
5448 __ JumpIfNotSmi(value_reg, &value_is_heap_object, Label::kNear);
5449 __ JumpIfNotSmi(cell_value_reg, &slow_case, Label::kNear);
5450 // Old and new values are SMIs, no need for a write barrier here.
5451 __ bind(&fast_smi_case);
5452 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5453 __ Ret();
5454 __ bind(&value_is_heap_object);
5455 __ JumpIfSmi(cell_value_reg, &slow_case, Label::kNear);
5456 Register cell_value_map_reg = cell_value_reg;
5457 __ mov(cell_value_map_reg,
5458 FieldOperand(cell_value_reg, HeapObject::kMapOffset));
5459 __ cmp(cell_value_map_reg, FieldOperand(value_reg, HeapObject::kMapOffset));
5460 __ j(equal, &fast_heapobject_case);
5461
5462 // Fallback to the runtime.
5463 __ bind(&slow_case);
5464 __ SmiTag(slot_reg);
5465 __ Pop(cell_reg); // Pop return address.
5466 __ Push(slot_reg);
5467 __ Push(value_reg);
5468 __ Push(cell_reg); // Push return address.
5469 __ TailCallRuntime(is_strict(language_mode())
5470 ? Runtime::kStoreGlobalViaContext_Strict
5471 : Runtime::kStoreGlobalViaContext_Sloppy);
5472}
5473
5474
5475// Generates an Operand for saving parameters after PrepareCallApiFunction.
5476static Operand ApiParameterOperand(int index) {
5477 return Operand(esp, index * kPointerSize);
5478}
5479
5480
5481// Prepares stack to put arguments (aligns and so on). Reserves
5482// space for return value if needed (assumes the return value is a handle).
5483// Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
5484// etc. Saves context (esi). If space was reserved for return value then
5485// stores the pointer to the reserved slot into esi.
5486static void PrepareCallApiFunction(MacroAssembler* masm, int argc) {
5487 __ EnterApiExitFrame(argc);
5488 if (__ emit_debug_code()) {
5489 __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
5490 }
5491}
5492
5493
5494// Calls an API function. Allocates HandleScope, extracts returned value
5495// from handle and propagates exceptions. Clobbers ebx, edi and
5496// caller-save registers. Restores context. On return removes
5497// stack_space * kPointerSize (GCed).
5498static void CallApiFunctionAndReturn(MacroAssembler* masm,
5499 Register function_address,
5500 ExternalReference thunk_ref,
5501 Operand thunk_last_arg, int stack_space,
5502 Operand* stack_space_operand,
5503 Operand return_value_operand,
5504 Operand* context_restore_operand) {
5505 Isolate* isolate = masm->isolate();
5506
5507 ExternalReference next_address =
5508 ExternalReference::handle_scope_next_address(isolate);
5509 ExternalReference limit_address =
5510 ExternalReference::handle_scope_limit_address(isolate);
5511 ExternalReference level_address =
5512 ExternalReference::handle_scope_level_address(isolate);
5513
5514 DCHECK(edx.is(function_address));
5515 // Allocate HandleScope in callee-save registers.
5516 __ mov(ebx, Operand::StaticVariable(next_address));
5517 __ mov(edi, Operand::StaticVariable(limit_address));
5518 __ add(Operand::StaticVariable(level_address), Immediate(1));
5519
5520 if (FLAG_log_timer_events) {
5521 FrameScope frame(masm, StackFrame::MANUAL);
5522 __ PushSafepointRegisters();
5523 __ PrepareCallCFunction(1, eax);
5524 __ mov(Operand(esp, 0),
5525 Immediate(ExternalReference::isolate_address(isolate)));
5526 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5527 1);
5528 __ PopSafepointRegisters();
5529 }
5530
5531
5532 Label profiler_disabled;
5533 Label end_profiler_check;
5534 __ mov(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
Ben Murdochda12d292016-06-02 14:46:10 +01005535 __ cmpb(Operand(eax, 0), Immediate(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005536 __ j(zero, &profiler_disabled);
5537
5538 // Additional parameter is the address of the actual getter function.
5539 __ mov(thunk_last_arg, function_address);
5540 // Call the api function.
5541 __ mov(eax, Immediate(thunk_ref));
5542 __ call(eax);
5543 __ jmp(&end_profiler_check);
5544
5545 __ bind(&profiler_disabled);
5546 // Call the api function.
5547 __ call(function_address);
5548 __ bind(&end_profiler_check);
5549
5550 if (FLAG_log_timer_events) {
5551 FrameScope frame(masm, StackFrame::MANUAL);
5552 __ PushSafepointRegisters();
5553 __ PrepareCallCFunction(1, eax);
5554 __ mov(Operand(esp, 0),
5555 Immediate(ExternalReference::isolate_address(isolate)));
5556 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5557 1);
5558 __ PopSafepointRegisters();
5559 }
5560
5561 Label prologue;
5562 // Load the value from ReturnValue
5563 __ mov(eax, return_value_operand);
5564
5565 Label promote_scheduled_exception;
5566 Label delete_allocated_handles;
5567 Label leave_exit_frame;
5568
5569 __ bind(&prologue);
5570 // No more valid handles (the result handle was the last one). Restore
5571 // previous handle scope.
5572 __ mov(Operand::StaticVariable(next_address), ebx);
5573 __ sub(Operand::StaticVariable(level_address), Immediate(1));
5574 __ Assert(above_equal, kInvalidHandleScopeLevel);
5575 __ cmp(edi, Operand::StaticVariable(limit_address));
5576 __ j(not_equal, &delete_allocated_handles);
5577
5578 // Leave the API exit frame.
5579 __ bind(&leave_exit_frame);
5580 bool restore_context = context_restore_operand != NULL;
5581 if (restore_context) {
5582 __ mov(esi, *context_restore_operand);
5583 }
5584 if (stack_space_operand != nullptr) {
5585 __ mov(ebx, *stack_space_operand);
5586 }
5587 __ LeaveApiExitFrame(!restore_context);
5588
5589 // Check if the function scheduled an exception.
5590 ExternalReference scheduled_exception_address =
5591 ExternalReference::scheduled_exception_address(isolate);
5592 __ cmp(Operand::StaticVariable(scheduled_exception_address),
5593 Immediate(isolate->factory()->the_hole_value()));
5594 __ j(not_equal, &promote_scheduled_exception);
5595
5596#if DEBUG
5597 // Check if the function returned a valid JavaScript value.
5598 Label ok;
5599 Register return_value = eax;
5600 Register map = ecx;
5601
5602 __ JumpIfSmi(return_value, &ok, Label::kNear);
5603 __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
5604
5605 __ CmpInstanceType(map, LAST_NAME_TYPE);
5606 __ j(below_equal, &ok, Label::kNear);
5607
5608 __ CmpInstanceType(map, FIRST_JS_RECEIVER_TYPE);
5609 __ j(above_equal, &ok, Label::kNear);
5610
5611 __ cmp(map, isolate->factory()->heap_number_map());
5612 __ j(equal, &ok, Label::kNear);
5613
5614 __ cmp(return_value, isolate->factory()->undefined_value());
5615 __ j(equal, &ok, Label::kNear);
5616
5617 __ cmp(return_value, isolate->factory()->true_value());
5618 __ j(equal, &ok, Label::kNear);
5619
5620 __ cmp(return_value, isolate->factory()->false_value());
5621 __ j(equal, &ok, Label::kNear);
5622
5623 __ cmp(return_value, isolate->factory()->null_value());
5624 __ j(equal, &ok, Label::kNear);
5625
5626 __ Abort(kAPICallReturnedInvalidObject);
5627
5628 __ bind(&ok);
5629#endif
5630
5631 if (stack_space_operand != nullptr) {
5632 DCHECK_EQ(0, stack_space);
5633 __ pop(ecx);
5634 __ add(esp, ebx);
5635 __ jmp(ecx);
5636 } else {
5637 __ ret(stack_space * kPointerSize);
5638 }
5639
5640 // Re-throw by promoting a scheduled exception.
5641 __ bind(&promote_scheduled_exception);
5642 __ TailCallRuntime(Runtime::kPromoteScheduledException);
5643
5644 // HandleScope limit has changed. Delete allocated extensions.
5645 ExternalReference delete_extensions =
5646 ExternalReference::delete_handle_scope_extensions(isolate);
5647 __ bind(&delete_allocated_handles);
5648 __ mov(Operand::StaticVariable(limit_address), edi);
5649 __ mov(edi, eax);
5650 __ mov(Operand(esp, 0),
5651 Immediate(ExternalReference::isolate_address(isolate)));
5652 __ mov(eax, Immediate(delete_extensions));
5653 __ call(eax);
5654 __ mov(eax, edi);
5655 __ jmp(&leave_exit_frame);
5656}
5657
Ben Murdochda12d292016-06-02 14:46:10 +01005658void CallApiCallbackStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005659 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005660 // -- edi : callee
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005661 // -- ebx : call_data
5662 // -- ecx : holder
5663 // -- edx : api_function_address
5664 // -- esi : context
5665 // --
5666 // -- esp[0] : return address
5667 // -- esp[4] : last argument
5668 // -- ...
5669 // -- esp[argc * 4] : first argument
5670 // -- esp[(argc + 1) * 4] : receiver
5671 // -----------------------------------
5672
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005673 Register callee = edi;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005674 Register call_data = ebx;
5675 Register holder = ecx;
5676 Register api_function_address = edx;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005677 Register context = esi;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005678 Register return_address = eax;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005679
5680 typedef FunctionCallbackArguments FCA;
5681
5682 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5683 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5684 STATIC_ASSERT(FCA::kDataIndex == 4);
5685 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5686 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5687 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5688 STATIC_ASSERT(FCA::kHolderIndex == 0);
5689 STATIC_ASSERT(FCA::kArgsLength == 7);
5690
Ben Murdochda12d292016-06-02 14:46:10 +01005691 __ pop(return_address);
5692 // context save.
5693 __ push(context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005694
5695 // callee
5696 __ push(callee);
5697
5698 // call data
5699 __ push(call_data);
5700
5701 Register scratch = call_data;
Ben Murdochda12d292016-06-02 14:46:10 +01005702 if (!call_data_undefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005703 // return value
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005704 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005705 // return value default
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005706 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005707 } else {
5708 // return value
5709 __ push(scratch);
5710 // return value default
5711 __ push(scratch);
5712 }
5713 // isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005714 __ push(Immediate(reinterpret_cast<int>(masm->isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005715 // holder
5716 __ push(holder);
5717
5718 __ mov(scratch, esp);
5719
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005720 // push return address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005721 __ push(return_address);
5722
Ben Murdochda12d292016-06-02 14:46:10 +01005723 if (!is_lazy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005724 // load context from callee
5725 __ mov(context, FieldOperand(callee, JSFunction::kContextOffset));
5726 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005727
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005728 // API function gets reference to the v8::Arguments. If CPU profiler
5729 // is enabled wrapper function will be called and we need to pass
5730 // address of the callback as additional parameter, always allocate
5731 // space for it.
5732 const int kApiArgc = 1 + 1;
5733
5734 // Allocate the v8::Arguments structure in the arguments' space since
5735 // it's not controlled by GC.
5736 const int kApiStackSpace = 4;
5737
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005738 PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005739
5740 // FunctionCallbackInfo::implicit_args_.
5741 __ mov(ApiParameterOperand(2), scratch);
Ben Murdochda12d292016-06-02 14:46:10 +01005742 __ add(scratch, Immediate((argc() + FCA::kArgsLength - 1) * kPointerSize));
5743 // FunctionCallbackInfo::values_.
5744 __ mov(ApiParameterOperand(3), scratch);
5745 // FunctionCallbackInfo::length_.
5746 __ Move(ApiParameterOperand(4), Immediate(argc()));
5747 // FunctionCallbackInfo::is_construct_call_.
5748 __ Move(ApiParameterOperand(5), Immediate(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005749
5750 // v8::InvocationCallback's argument.
5751 __ lea(scratch, ApiParameterOperand(2));
5752 __ mov(ApiParameterOperand(0), scratch);
5753
5754 ExternalReference thunk_ref =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005755 ExternalReference::invoke_function_callback(masm->isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005756
5757 Operand context_restore_operand(ebp,
5758 (2 + FCA::kContextSaveIndex) * kPointerSize);
5759 // Stores return the first js argument
5760 int return_value_offset = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005761 if (is_store()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005762 return_value_offset = 2 + FCA::kArgsLength;
5763 } else {
5764 return_value_offset = 2 + FCA::kReturnValueOffset;
5765 }
5766 Operand return_value_operand(ebp, return_value_offset * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005767 int stack_space = 0;
5768 Operand is_construct_call_operand = ApiParameterOperand(5);
5769 Operand* stack_space_operand = &is_construct_call_operand;
Ben Murdochda12d292016-06-02 14:46:10 +01005770 stack_space = argc() + FCA::kArgsLength + 1;
5771 stack_space_operand = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005772 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5773 ApiParameterOperand(1), stack_space,
5774 stack_space_operand, return_value_operand,
5775 &context_restore_operand);
5776}
5777
5778
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005779void CallApiGetterStub::Generate(MacroAssembler* masm) {
5780 // ----------- S t a t e -------------
Ben Murdoch097c5b22016-05-18 11:27:45 +01005781 // -- esp[0] : return address
5782 // -- esp[4] : name
5783 // -- esp[8 .. (8 + kArgsLength*4)] : v8::PropertyCallbackInfo::args_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005784 // -- ...
Ben Murdoch097c5b22016-05-18 11:27:45 +01005785 // -- edx : api_function_address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005786 // -----------------------------------
5787 DCHECK(edx.is(ApiGetterDescriptor::function_address()));
5788
Ben Murdoch097c5b22016-05-18 11:27:45 +01005789 // v8::PropertyCallbackInfo::args_ array and name handle.
5790 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5791
5792 // Allocate v8::PropertyCallbackInfo object, arguments for callback and
5793 // space for optional callback address parameter (in case CPU profiler is
5794 // active) in non-GCed stack space.
5795 const int kApiArgc = 3 + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005796
5797 Register api_function_address = edx;
5798 Register scratch = ebx;
5799
Ben Murdoch097c5b22016-05-18 11:27:45 +01005800 // Load address of v8::PropertyAccessorInfo::args_ array.
5801 __ lea(scratch, Operand(esp, 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005802
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005803 PrepareCallApiFunction(masm, kApiArgc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005804 // Create v8::PropertyCallbackInfo object on the stack and initialize
5805 // it's args_ field.
5806 Operand info_object = ApiParameterOperand(3);
5807 __ mov(info_object, scratch);
5808
5809 __ sub(scratch, Immediate(kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005810 __ mov(ApiParameterOperand(0), scratch); // name.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005811 __ lea(scratch, info_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005812 __ mov(ApiParameterOperand(1), scratch); // arguments pointer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005813 // Reserve space for optional callback address parameter.
5814 Operand thunk_last_arg = ApiParameterOperand(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005815
5816 ExternalReference thunk_ref =
5817 ExternalReference::invoke_accessor_getter_callback(isolate());
5818
Ben Murdoch097c5b22016-05-18 11:27:45 +01005819 // +3 is to skip prolog, return address and name handle.
5820 Operand return_value_operand(
5821 ebp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005822 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005823 thunk_last_arg, kStackUnwindSpace, nullptr,
5824 return_value_operand, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005825}
5826
5827
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005828#undef __
5829
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005830} // namespace internal
5831} // namespace v8
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005832
5833#endif // V8_TARGET_ARCH_IA32