blob: ff6c8d29e51e50699f1612d5f3e71ffb6656f218 [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#if V8_TARGET_ARCH_X87
6
7#include "src/base/bits.h"
8#include "src/bootstrapper.h"
9#include "src/code-stubs.h"
10#include "src/codegen.h"
11#include "src/ic/handler-compiler.h"
12#include "src/ic/ic.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/ic/stub-cache.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "src/isolate.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015#include "src/regexp/jsregexp.h"
16#include "src/regexp/regexp-macro-assembler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017#include "src/runtime/runtime.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000018#include "src/x87/code-stubs-x87.h"
19#include "src/x87/frames-x87.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000020
21namespace v8 {
22namespace internal {
23
24
25static void InitializeArrayConstructorDescriptor(
26 Isolate* isolate, CodeStubDescriptor* descriptor,
27 int constant_stack_parameter_count) {
28 // register state
29 // eax -- number of arguments
30 // edi -- function
31 // ebx -- allocation site with elements kind
32 Address deopt_handler = Runtime::FunctionForId(
33 Runtime::kArrayConstructor)->entry;
34
35 if (constant_stack_parameter_count == 0) {
36 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
37 JS_FUNCTION_STUB_MODE);
38 } else {
39 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000040 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041 }
42}
43
44
45static void InitializeInternalArrayConstructorDescriptor(
46 Isolate* isolate, CodeStubDescriptor* descriptor,
47 int constant_stack_parameter_count) {
48 // register state
49 // eax -- number of arguments
50 // edi -- constructor function
51 Address deopt_handler = Runtime::FunctionForId(
52 Runtime::kInternalArrayConstructor)->entry;
53
54 if (constant_stack_parameter_count == 0) {
55 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
56 JS_FUNCTION_STUB_MODE);
57 } else {
58 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000059 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000060 }
61}
62
63
64void ArrayNoArgumentConstructorStub::InitializeDescriptor(
65 CodeStubDescriptor* descriptor) {
66 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
67}
68
69
70void ArraySingleArgumentConstructorStub::InitializeDescriptor(
71 CodeStubDescriptor* descriptor) {
72 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
73}
74
75
76void ArrayNArgumentsConstructorStub::InitializeDescriptor(
77 CodeStubDescriptor* descriptor) {
78 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
79}
80
81
82void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
83 CodeStubDescriptor* descriptor) {
84 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
85}
86
87
88void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
89 CodeStubDescriptor* descriptor) {
90 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
91}
92
93
94void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
95 CodeStubDescriptor* descriptor) {
96 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
97}
98
99
100#define __ ACCESS_MASM(masm)
101
102
103void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
104 ExternalReference miss) {
105 // Update the static counter each time a new code stub is generated.
106 isolate()->counters()->code_stubs()->Increment();
107
108 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000109 int param_count = descriptor.GetRegisterParameterCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000110 {
111 // Call the runtime system in a fresh internal frame.
112 FrameScope scope(masm, StackFrame::INTERNAL);
113 DCHECK(param_count == 0 ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 eax.is(descriptor.GetRegisterParameter(param_count - 1)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 // Push arguments
116 for (int i = 0; i < param_count; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117 __ push(descriptor.GetRegisterParameter(i));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 }
119 __ CallExternalReference(miss, param_count);
120 }
121
122 __ ret(0);
123}
124
125
126void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
127 // We don't allow a GC during a store buffer overflow so there is no need to
128 // store the registers in any particular way, but we do have to store and
129 // restore them.
130 __ pushad();
131 if (save_doubles()) {
132 // Save FPU stat in m108byte.
133 __ sub(esp, Immediate(108));
134 __ fnsave(Operand(esp, 0));
135 }
136 const int argument_count = 1;
137
138 AllowExternalCallThatCantCauseGC scope(masm);
139 __ PrepareCallCFunction(argument_count, ecx);
140 __ mov(Operand(esp, 0 * kPointerSize),
141 Immediate(ExternalReference::isolate_address(isolate())));
142 __ CallCFunction(
143 ExternalReference::store_buffer_overflow_function(isolate()),
144 argument_count);
145 if (save_doubles()) {
146 // Restore FPU stat in m108byte.
147 __ frstor(Operand(esp, 0));
148 __ add(esp, Immediate(108));
149 }
150 __ popad();
151 __ ret(0);
152}
153
154
155class FloatingPointHelper : public AllStatic {
156 public:
157 enum ArgLocation {
158 ARGS_ON_STACK,
159 ARGS_IN_REGISTERS
160 };
161
162 // Code pattern for loading a floating point value. Input value must
163 // be either a smi or a heap number object (fp value). Requirements:
164 // operand in register number. Returns operand as floating point number
165 // on FPU stack.
166 static void LoadFloatOperand(MacroAssembler* masm, Register number);
167
168 // Test if operands are smi or number objects (fp). Requirements:
169 // operand_1 in eax, operand_2 in edx; falls through on float
170 // operands, jumps to the non_float label otherwise.
171 static void CheckFloatOperands(MacroAssembler* masm,
172 Label* non_float,
173 Register scratch);
174};
175
176
177void DoubleToIStub::Generate(MacroAssembler* masm) {
178 Register input_reg = this->source();
179 Register final_result_reg = this->destination();
180 DCHECK(is_truncating());
181
182 Label check_negative, process_64_bits, done, done_no_stash;
183
184 int double_offset = offset();
185
186 // Account for return address and saved regs if input is esp.
187 if (input_reg.is(esp)) double_offset += 3 * kPointerSize;
188
189 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
190 MemOperand exponent_operand(MemOperand(input_reg,
191 double_offset + kDoubleSize / 2));
192
193 Register scratch1;
194 {
195 Register scratch_candidates[3] = { ebx, edx, edi };
196 for (int i = 0; i < 3; i++) {
197 scratch1 = scratch_candidates[i];
198 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
199 }
200 }
201 // Since we must use ecx for shifts below, use some other register (eax)
202 // to calculate the result if ecx is the requested return register.
203 Register result_reg = final_result_reg.is(ecx) ? eax : final_result_reg;
204 // Save ecx if it isn't the return register and therefore volatile, or if it
205 // is the return register, then save the temp register we use in its stead for
206 // the result.
207 Register save_reg = final_result_reg.is(ecx) ? eax : ecx;
208 __ push(scratch1);
209 __ push(save_reg);
210
211 bool stash_exponent_copy = !input_reg.is(esp);
212 __ mov(scratch1, mantissa_operand);
213 __ mov(ecx, exponent_operand);
214 if (stash_exponent_copy) __ push(ecx);
215
216 __ and_(ecx, HeapNumber::kExponentMask);
217 __ shr(ecx, HeapNumber::kExponentShift);
218 __ lea(result_reg, MemOperand(ecx, -HeapNumber::kExponentBias));
219 __ cmp(result_reg, Immediate(HeapNumber::kMantissaBits));
220 __ j(below, &process_64_bits);
221
222 // Result is entirely in lower 32-bits of mantissa
223 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
224 __ sub(ecx, Immediate(delta));
225 __ xor_(result_reg, result_reg);
226 __ cmp(ecx, Immediate(31));
227 __ j(above, &done);
228 __ shl_cl(scratch1);
229 __ jmp(&check_negative);
230
231 __ bind(&process_64_bits);
232 // Result must be extracted from shifted 32-bit mantissa
233 __ sub(ecx, Immediate(delta));
234 __ neg(ecx);
235 if (stash_exponent_copy) {
236 __ mov(result_reg, MemOperand(esp, 0));
237 } else {
238 __ mov(result_reg, exponent_operand);
239 }
240 __ and_(result_reg,
241 Immediate(static_cast<uint32_t>(Double::kSignificandMask >> 32)));
242 __ add(result_reg,
243 Immediate(static_cast<uint32_t>(Double::kHiddenBit >> 32)));
244 __ shrd(result_reg, scratch1);
245 __ shr_cl(result_reg);
246 __ test(ecx, Immediate(32));
247 {
248 Label skip_mov;
249 __ j(equal, &skip_mov, Label::kNear);
250 __ mov(scratch1, result_reg);
251 __ bind(&skip_mov);
252 }
253
254 // If the double was negative, negate the integer result.
255 __ bind(&check_negative);
256 __ mov(result_reg, scratch1);
257 __ neg(result_reg);
258 if (stash_exponent_copy) {
259 __ cmp(MemOperand(esp, 0), Immediate(0));
260 } else {
261 __ cmp(exponent_operand, Immediate(0));
262 }
263 {
264 Label skip_mov;
265 __ j(less_equal, &skip_mov, Label::kNear);
266 __ mov(result_reg, scratch1);
267 __ bind(&skip_mov);
268 }
269
270 // Restore registers
271 __ bind(&done);
272 if (stash_exponent_copy) {
273 __ add(esp, Immediate(kDoubleSize / 2));
274 }
275 __ bind(&done_no_stash);
276 if (!final_result_reg.is(result_reg)) {
277 DCHECK(final_result_reg.is(ecx));
278 __ mov(final_result_reg, result_reg);
279 }
280 __ pop(save_reg);
281 __ pop(scratch1);
282 __ ret(0);
283}
284
285
286void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
287 Register number) {
288 Label load_smi, done;
289
290 __ JumpIfSmi(number, &load_smi, Label::kNear);
291 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
292 __ jmp(&done, Label::kNear);
293
294 __ bind(&load_smi);
295 __ SmiUntag(number);
296 __ push(number);
297 __ fild_s(Operand(esp, 0));
298 __ pop(number);
299
300 __ bind(&done);
301}
302
303
304void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
305 Label* non_float,
306 Register scratch) {
307 Label test_other, done;
308 // Test if both operands are floats or smi -> scratch=k_is_float;
309 // Otherwise scratch = k_not_float.
310 __ JumpIfSmi(edx, &test_other, Label::kNear);
311 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
312 Factory* factory = masm->isolate()->factory();
313 __ cmp(scratch, factory->heap_number_map());
314 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
315
316 __ bind(&test_other);
317 __ JumpIfSmi(eax, &done, Label::kNear);
318 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
319 __ cmp(scratch, factory->heap_number_map());
320 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
321
322 // Fall-through: Both operands are numbers.
323 __ bind(&done);
324}
325
326
327void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000328 const Register base = edx;
329 const Register scratch = ecx;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000330 Label call_runtime;
331
332 // We will call runtime helper function directly.
333 if (exponent_type() == ON_STACK) {
334 // The arguments are still on the stack.
335 __ bind(&call_runtime);
336 __ TailCallRuntime(Runtime::kMathPowRT);
337
338 // The stub is called from non-optimized code, which expects the result
339 // as heap number in exponent.
340 __ AllocateHeapNumber(eax, scratch, base, &call_runtime);
341 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000342 __ ret(2 * kPointerSize);
343 } else {
344 // Currently it's only called from full-compiler and exponent type is
345 // ON_STACK.
346 UNIMPLEMENTED();
347 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000348}
349
350
351void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
352 Label miss;
353 Register receiver = LoadDescriptor::ReceiverRegister();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000354 // With careful management, we won't have to save slot and vector on
355 // the stack. Simply handle the possibly missing case first.
356 // TODO(mvstanton): this code can be more efficient.
357 __ cmp(FieldOperand(receiver, JSFunction::kPrototypeOrInitialMapOffset),
358 Immediate(isolate()->factory()->the_hole_value()));
359 __ j(equal, &miss);
360 __ TryGetFunctionPrototype(receiver, eax, ebx, &miss);
361 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000362
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000363 __ bind(&miss);
364 PropertyAccessCompiler::TailCallBuiltin(
365 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
366}
367
368
369void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
370 // Return address is on the stack.
371 Label slow;
372
373 Register receiver = LoadDescriptor::ReceiverRegister();
374 Register key = LoadDescriptor::NameRegister();
375 Register scratch = eax;
376 DCHECK(!scratch.is(receiver) && !scratch.is(key));
377
378 // Check that the key is an array index, that is Uint32.
379 __ test(key, Immediate(kSmiTagMask | kSmiSignMask));
380 __ j(not_zero, &slow);
381
382 // Everything is fine, call runtime.
383 __ pop(scratch);
384 __ push(receiver); // receiver
385 __ push(key); // key
386 __ push(scratch); // return address
387
388 // Perform tail call to the entry.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389 __ TailCallRuntime(Runtime::kLoadElementWithInterceptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000390
391 __ bind(&slow);
392 PropertyAccessCompiler::TailCallBuiltin(
393 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
394}
395
396
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400397void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
398 // Return address is on the stack.
399 Label miss;
400
401 Register receiver = LoadDescriptor::ReceiverRegister();
402 Register index = LoadDescriptor::NameRegister();
403 Register scratch = edi;
404 DCHECK(!scratch.is(receiver) && !scratch.is(index));
405 Register result = eax;
406 DCHECK(!result.is(scratch));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000407 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
408 result.is(LoadDescriptor::SlotRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400409
410 // StringCharAtGenerator doesn't use the result register until it's passed
411 // the different miss possibilities. If it did, we would have a conflict
412 // when FLAG_vector_ics is true.
413
414 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
415 &miss, // When not a string.
416 &miss, // When not a number.
417 &miss, // When index out of range.
418 STRING_INDEX_IS_ARRAY_INDEX,
419 RECEIVER_IS_STRING);
420 char_at_generator.GenerateFast(masm);
421 __ ret(0);
422
423 StubRuntimeCallHelper call_helper;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000424 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400425
426 __ bind(&miss);
427 PropertyAccessCompiler::TailCallBuiltin(
428 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
429}
430
431
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000432void RegExpExecStub::Generate(MacroAssembler* masm) {
433 // Just jump directly to runtime if native RegExp is not selected at compile
434 // time or if regexp entry in generated code is turned off runtime switch or
435 // at compilation.
436#ifdef V8_INTERPRETED_REGEXP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000437 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000438#else // V8_INTERPRETED_REGEXP
439
440 // Stack frame on entry.
441 // esp[0]: return address
442 // esp[4]: last_match_info (expected JSArray)
443 // esp[8]: previous index
444 // esp[12]: subject string
445 // esp[16]: JSRegExp object
446
447 static const int kLastMatchInfoOffset = 1 * kPointerSize;
448 static const int kPreviousIndexOffset = 2 * kPointerSize;
449 static const int kSubjectOffset = 3 * kPointerSize;
450 static const int kJSRegExpOffset = 4 * kPointerSize;
451
452 Label runtime;
453 Factory* factory = isolate()->factory();
454
455 // Ensure that a RegExp stack is allocated.
456 ExternalReference address_of_regexp_stack_memory_address =
457 ExternalReference::address_of_regexp_stack_memory_address(isolate());
458 ExternalReference address_of_regexp_stack_memory_size =
459 ExternalReference::address_of_regexp_stack_memory_size(isolate());
460 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
461 __ test(ebx, ebx);
462 __ j(zero, &runtime);
463
464 // Check that the first argument is a JSRegExp object.
465 __ mov(eax, Operand(esp, kJSRegExpOffset));
466 STATIC_ASSERT(kSmiTag == 0);
467 __ JumpIfSmi(eax, &runtime);
468 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
469 __ j(not_equal, &runtime);
470
471 // Check that the RegExp has been compiled (data contains a fixed array).
472 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
473 if (FLAG_debug_code) {
474 __ test(ecx, Immediate(kSmiTagMask));
475 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
476 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
477 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
478 }
479
480 // ecx: RegExp data (FixedArray)
481 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
482 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
483 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
484 __ j(not_equal, &runtime);
485
486 // ecx: RegExp data (FixedArray)
487 // Check that the number of captures fit in the static offsets vector buffer.
488 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
489 // Check (number_of_captures + 1) * 2 <= offsets vector size
490 // Or number_of_captures * 2 <= offsets vector size - 2
491 // Multiplying by 2 comes for free since edx is smi-tagged.
492 STATIC_ASSERT(kSmiTag == 0);
493 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
494 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
495 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
496 __ j(above, &runtime);
497
498 // Reset offset for possibly sliced string.
499 __ Move(edi, Immediate(0));
500 __ mov(eax, Operand(esp, kSubjectOffset));
501 __ JumpIfSmi(eax, &runtime);
502 __ mov(edx, eax); // Make a copy of the original subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503
504 // eax: subject string
505 // edx: subject string
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000506 // ecx: RegExp data (FixedArray)
507 // Handle subject string according to its encoding and representation:
508 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch097c5b22016-05-18 11:27:45 +0100509 // (2) Sequential one byte? If yes, go to (5).
510 // (3) Sequential or cons? If not, go to (6).
511 // (4) Cons string. If the string is flat, replace subject with first string
512 // and go to (1). Otherwise bail out to runtime.
513 // (5) One byte sequential. Load regexp code for one byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000514 // (E) Carry on.
515 /// [...]
516
517 // Deferred code at the end of the stub:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100518 // (6) Long external string? If not, go to (10).
519 // (7) External string. Make it, offset-wise, look like a sequential string.
520 // (8) Is the external string one byte? If yes, go to (5).
521 // (9) Two byte sequential. Load regexp code for two byte. Go to (E).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000522 // (10) Short external string or not a string? If yes, bail out to runtime.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100523 // (11) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524
Ben Murdoch097c5b22016-05-18 11:27:45 +0100525 Label seq_one_byte_string /* 5 */, seq_two_byte_string /* 9 */,
526 external_string /* 7 */, check_underlying /* 1 */,
527 not_seq_nor_cons /* 6 */, check_code /* E */, not_long_external /* 10 */;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528
Ben Murdoch097c5b22016-05-18 11:27:45 +0100529 __ bind(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch097c5b22016-05-18 11:27:45 +0100531 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
532 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
533
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534 __ and_(ebx, kIsNotStringMask |
535 kStringRepresentationMask |
536 kStringEncodingMask |
537 kShortExternalStringMask);
538 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
539 __ j(zero, &seq_two_byte_string); // Go to (9).
540
Ben Murdoch097c5b22016-05-18 11:27:45 +0100541 // (2) Sequential one byte? If yes, go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000542 // Any other sequential string must be one byte.
543 __ and_(ebx, Immediate(kIsNotStringMask |
544 kStringRepresentationMask |
545 kShortExternalStringMask));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100546 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000547
Ben Murdoch097c5b22016-05-18 11:27:45 +0100548 // (3) Sequential or cons? If not, go to (6).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000549 // We check whether the subject string is a cons, since sequential strings
550 // have already been covered.
551 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
552 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
553 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
554 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
555 __ cmp(ebx, Immediate(kExternalStringTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100556 __ j(greater_equal, &not_seq_nor_cons); // Go to (6).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557
558 // (4) Cons string. Check that it's flat.
559 // Replace subject with first string and reload instance type.
560 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
561 __ j(not_equal, &runtime);
562 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100563 __ jmp(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000564
565 // eax: sequential subject string (or look-alike, external string)
566 // edx: original subject string
567 // ecx: RegExp data (FixedArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100568 // (5) One byte sequential. Load regexp code for one byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000569 __ bind(&seq_one_byte_string);
570 // Load previous index and check range before edx is overwritten. We have
571 // to use edx instead of eax here because it might have been only made to
572 // look like a sequential string when it actually is an external string.
573 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
574 __ JumpIfNotSmi(ebx, &runtime);
575 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
576 __ j(above_equal, &runtime);
577 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataOneByteCodeOffset));
578 __ Move(ecx, Immediate(1)); // Type is one byte.
579
580 // (E) Carry on. String handling is done.
581 __ bind(&check_code);
582 // edx: irregexp code
583 // Check that the irregexp code has been generated for the actual string
584 // encoding. If it has, the field contains a code object otherwise it contains
585 // a smi (code flushing support).
586 __ JumpIfSmi(edx, &runtime);
587
588 // eax: subject string
589 // ebx: previous index (smi)
590 // edx: code
591 // ecx: encoding of subject string (1 if one_byte, 0 if two_byte);
592 // All checks done. Now push arguments for native regexp code.
593 Counters* counters = isolate()->counters();
594 __ IncrementCounter(counters->regexp_entry_native(), 1);
595
596 // Isolates: note we add an additional parameter here (isolate pointer).
597 static const int kRegExpExecuteArguments = 9;
598 __ EnterApiExitFrame(kRegExpExecuteArguments);
599
600 // Argument 9: Pass current isolate address.
601 __ mov(Operand(esp, 8 * kPointerSize),
602 Immediate(ExternalReference::isolate_address(isolate())));
603
604 // Argument 8: Indicate that this is a direct call from JavaScript.
605 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
606
607 // Argument 7: Start (high end) of backtracking stack memory area.
608 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
609 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
610 __ mov(Operand(esp, 6 * kPointerSize), esi);
611
612 // Argument 6: Set the number of capture registers to zero to force global
613 // regexps to behave as non-global. This does not affect non-global regexps.
614 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
615
616 // Argument 5: static offsets vector buffer.
617 __ mov(Operand(esp, 4 * kPointerSize),
618 Immediate(ExternalReference::address_of_static_offsets_vector(
619 isolate())));
620
621 // Argument 2: Previous index.
622 __ SmiUntag(ebx);
623 __ mov(Operand(esp, 1 * kPointerSize), ebx);
624
625 // Argument 1: Original subject string.
626 // The original subject is in the previous stack frame. Therefore we have to
627 // use ebp, which points exactly to one pointer size below the previous esp.
628 // (Because creating a new stack frame pushes the previous ebp onto the stack
629 // and thereby moves up esp by one kPointerSize.)
630 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
631 __ mov(Operand(esp, 0 * kPointerSize), esi);
632
633 // esi: original subject string
634 // eax: underlying subject string
635 // ebx: previous index
636 // ecx: encoding of subject string (1 if one_byte 0 if two_byte);
637 // edx: code
638 // Argument 4: End of string data
639 // Argument 3: Start of string data
640 // Prepare start and end index of the input.
641 // Load the length from the original sliced string if that is the case.
642 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
643 __ add(esi, edi); // Calculate input end wrt offset.
644 __ SmiUntag(edi);
645 __ add(ebx, edi); // Calculate input start wrt offset.
646
647 // ebx: start index of the input string
648 // esi: end index of the input string
649 Label setup_two_byte, setup_rest;
650 __ test(ecx, ecx);
651 __ j(zero, &setup_two_byte, Label::kNear);
652 __ SmiUntag(esi);
653 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
654 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
655 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
656 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
657 __ jmp(&setup_rest, Label::kNear);
658
659 __ bind(&setup_two_byte);
660 STATIC_ASSERT(kSmiTag == 0);
661 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
662 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
663 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
664 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
665 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
666
667 __ bind(&setup_rest);
668
669 // Locate the code entry and call it.
670 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
671 __ call(edx);
672
673 // Drop arguments and come back to JS mode.
674 __ LeaveApiExitFrame(true);
675
676 // Check the result.
677 Label success;
678 __ cmp(eax, 1);
679 // We expect exactly one result since we force the called regexp to behave
680 // as non-global.
681 __ j(equal, &success);
682 Label failure;
683 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
684 __ j(equal, &failure);
685 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
686 // If not exception it can only be retry. Handle that in the runtime system.
687 __ j(not_equal, &runtime);
688 // Result must now be exception. If there is no pending exception already a
689 // stack overflow (on the backtrack stack) was detected in RegExp code but
690 // haven't created the exception yet. Handle that in the runtime system.
691 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
692 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
693 isolate());
694 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
695 __ mov(eax, Operand::StaticVariable(pending_exception));
696 __ cmp(edx, eax);
697 __ j(equal, &runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000698
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000699 // For exception, throw the exception again.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000700 __ TailCallRuntime(Runtime::kRegExpExecReThrow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000701
702 __ bind(&failure);
703 // For failure to match, return null.
704 __ mov(eax, factory->null_value());
705 __ ret(4 * kPointerSize);
706
707 // Load RegExp data.
708 __ bind(&success);
709 __ mov(eax, Operand(esp, kJSRegExpOffset));
710 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
711 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
712 // Calculate number of capture registers (number_of_captures + 1) * 2.
713 STATIC_ASSERT(kSmiTag == 0);
714 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
715 __ add(edx, Immediate(2)); // edx was a smi.
716
717 // edx: Number of capture registers
718 // Load last_match_info which is still known to be a fast case JSArray.
719 // Check that the fourth object is a JSArray object.
720 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
721 __ JumpIfSmi(eax, &runtime);
722 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
723 __ j(not_equal, &runtime);
724 // Check that the JSArray is in fast case.
725 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
726 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
727 __ cmp(eax, factory->fixed_array_map());
728 __ j(not_equal, &runtime);
729 // Check that the last match info has space for the capture registers and the
730 // additional information.
731 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
732 __ SmiUntag(eax);
733 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
734 __ cmp(edx, eax);
735 __ j(greater, &runtime);
736
737 // ebx: last_match_info backing store (FixedArray)
738 // edx: number of capture registers
739 // Store the capture count.
740 __ SmiTag(edx); // Number of capture registers to smi.
741 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
742 __ SmiUntag(edx); // Number of capture registers back from smi.
743 // Store last subject and last input.
744 __ mov(eax, Operand(esp, kSubjectOffset));
745 __ mov(ecx, eax);
746 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
747 __ RecordWriteField(ebx, RegExpImpl::kLastSubjectOffset, eax, edi,
748 kDontSaveFPRegs);
749 __ mov(eax, ecx);
750 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
751 __ RecordWriteField(ebx, RegExpImpl::kLastInputOffset, eax, edi,
752 kDontSaveFPRegs);
753
754 // Get the static offsets vector filled by the native regexp code.
755 ExternalReference address_of_static_offsets_vector =
756 ExternalReference::address_of_static_offsets_vector(isolate());
757 __ mov(ecx, Immediate(address_of_static_offsets_vector));
758
759 // ebx: last_match_info backing store (FixedArray)
760 // ecx: offsets vector
761 // edx: number of capture registers
762 Label next_capture, done;
763 // Capture register counter starts from number of capture registers and
764 // counts down until wraping after zero.
765 __ bind(&next_capture);
766 __ sub(edx, Immediate(1));
767 __ j(negative, &done, Label::kNear);
768 // Read the value from the static offsets vector buffer.
769 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
770 __ SmiTag(edi);
771 // Store the smi value in the last match info.
772 __ mov(FieldOperand(ebx,
773 edx,
774 times_pointer_size,
775 RegExpImpl::kFirstCaptureOffset),
776 edi);
777 __ jmp(&next_capture);
778 __ bind(&done);
779
780 // Return last match info.
781 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
782 __ ret(4 * kPointerSize);
783
784 // Do the runtime call to execute the regexp.
785 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000786 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000787
788 // Deferred code for string handling.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100789 // (6) Long external string? If not, go to (10).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790 __ bind(&not_seq_nor_cons);
791 // Compare flags are still set from (3).
792 __ j(greater, &not_long_external, Label::kNear); // Go to (10).
793
Ben Murdoch097c5b22016-05-18 11:27:45 +0100794 // (7) External string. Short external strings have been ruled out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000795 __ bind(&external_string);
796 // Reload instance type.
797 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
798 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
799 if (FLAG_debug_code) {
800 // Assert that we do not have a cons or slice (indirect strings) here.
801 // Sequential strings have already been ruled out.
802 __ test_b(ebx, kIsIndirectStringMask);
803 __ Assert(zero, kExternalStringExpectedButNotFound);
804 }
805 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
806 // Move the pointer so that offset-wise, it looks like a sequential string.
807 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
808 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
809 STATIC_ASSERT(kTwoByteStringTag == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100810 // (8) Is the external string one byte? If yes, go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000811 __ test_b(ebx, kStringEncodingMask);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100812 __ j(not_zero, &seq_one_byte_string); // Go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000813
814 // eax: sequential subject string (or look-alike, external string)
815 // edx: original subject string
816 // ecx: RegExp data (FixedArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100817 // (9) Two byte sequential. Load regexp code for two byte. Go to (E).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 __ bind(&seq_two_byte_string);
819 // Load previous index and check range before edx is overwritten. We have
820 // to use edx instead of eax here because it might have been only made to
821 // look like a sequential string when it actually is an external string.
822 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
823 __ JumpIfNotSmi(ebx, &runtime);
824 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
825 __ j(above_equal, &runtime);
826 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
827 __ Move(ecx, Immediate(0)); // Type is two byte.
828 __ jmp(&check_code); // Go to (E).
829
830 // (10) Not a string or a short external string? If yes, bail out to runtime.
831 __ bind(&not_long_external);
832 // Catch non-string subject or short external string.
833 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
834 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
835 __ j(not_zero, &runtime);
836
Ben Murdoch097c5b22016-05-18 11:27:45 +0100837 // (11) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000838 // Load offset into edi and replace subject string with parent.
839 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
840 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100841 __ jmp(&check_underlying); // Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000842#endif // V8_INTERPRETED_REGEXP
843}
844
845
846static int NegativeComparisonResult(Condition cc) {
847 DCHECK(cc != equal);
848 DCHECK((cc == less) || (cc == less_equal)
849 || (cc == greater) || (cc == greater_equal));
850 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
851}
852
853
854static void CheckInputType(MacroAssembler* masm, Register input,
855 CompareICState::State expected, Label* fail) {
856 Label ok;
857 if (expected == CompareICState::SMI) {
858 __ JumpIfNotSmi(input, fail);
859 } else if (expected == CompareICState::NUMBER) {
860 __ JumpIfSmi(input, &ok);
861 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
862 Immediate(masm->isolate()->factory()->heap_number_map()));
863 __ j(not_equal, fail);
864 }
865 // We could be strict about internalized/non-internalized here, but as long as
866 // hydrogen doesn't care, the stub doesn't have to care either.
867 __ bind(&ok);
868}
869
870
871static void BranchIfNotInternalizedString(MacroAssembler* masm,
872 Label* label,
873 Register object,
874 Register scratch) {
875 __ JumpIfSmi(object, label);
876 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
877 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
878 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
879 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
880 __ j(not_zero, label);
881}
882
883
884void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000885 Label runtime_call, check_unequal_objects;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000886 Condition cc = GetCondition();
887
888 Label miss;
889 CheckInputType(masm, edx, left(), &miss);
890 CheckInputType(masm, eax, right(), &miss);
891
892 // Compare two smis.
893 Label non_smi, smi_done;
894 __ mov(ecx, edx);
895 __ or_(ecx, eax);
896 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
897 __ sub(edx, eax); // Return on the result of the subtraction.
898 __ j(no_overflow, &smi_done, Label::kNear);
899 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
900 __ bind(&smi_done);
901 __ mov(eax, edx);
902 __ ret(0);
903 __ bind(&non_smi);
904
905 // NOTICE! This code is only reached after a smi-fast-case check, so
906 // it is certain that at least one operand isn't a smi.
907
908 // Identical objects can be compared fast, but there are some tricky cases
909 // for NaN and undefined.
910 Label generic_heap_number_comparison;
911 {
912 Label not_identical;
913 __ cmp(eax, edx);
914 __ j(not_equal, &not_identical);
915
916 if (cc != equal) {
917 // Check for undefined. undefined OP undefined is false even though
918 // undefined == undefined.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000919 __ cmp(edx, isolate()->factory()->undefined_value());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100920 Label check_for_nan;
921 __ j(not_equal, &check_for_nan, Label::kNear);
922 __ Move(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
923 __ ret(0);
924 __ bind(&check_for_nan);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000925 }
926
927 // Test for NaN. Compare heap numbers in a general way,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000928 // to handle NaNs correctly.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000929 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
930 Immediate(isolate()->factory()->heap_number_map()));
931 __ j(equal, &generic_heap_number_comparison, Label::kNear);
932 if (cc != equal) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000933 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
934 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000935 // Call runtime on identical JSObjects. Otherwise return equal.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000936 __ cmpb(ecx, static_cast<uint8_t>(FIRST_JS_RECEIVER_TYPE));
937 __ j(above_equal, &runtime_call, Label::kFar);
938 // Call runtime on identical symbols since we need to throw a TypeError.
939 __ cmpb(ecx, static_cast<uint8_t>(SYMBOL_TYPE));
940 __ j(equal, &runtime_call, Label::kFar);
941 // Call runtime on identical SIMD values since we must throw a TypeError.
942 __ cmpb(ecx, static_cast<uint8_t>(SIMD128_VALUE_TYPE));
943 __ j(equal, &runtime_call, Label::kFar);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000944 }
945 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
946 __ ret(0);
947
948
949 __ bind(&not_identical);
950 }
951
952 // Strict equality can quickly decide whether objects are equal.
953 // Non-strict object equality is slower, so it is handled later in the stub.
954 if (cc == equal && strict()) {
955 Label slow; // Fallthrough label.
956 Label not_smis;
957 // If we're doing a strict equality comparison, we don't have to do
958 // type conversion, so we generate code to do fast comparison for objects
959 // and oddballs. Non-smi numbers and strings still go through the usual
960 // slow-case code.
961 // If either is a Smi (we know that not both are), then they can only
962 // be equal if the other is a HeapNumber. If so, use the slow case.
963 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000964 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000965 __ mov(ecx, Immediate(kSmiTagMask));
966 __ and_(ecx, eax);
967 __ test(ecx, edx);
968 __ j(not_zero, &not_smis, Label::kNear);
969 // One operand is a smi.
970
971 // Check whether the non-smi is a heap number.
972 STATIC_ASSERT(kSmiTagMask == 1);
973 // ecx still holds eax & kSmiTag, which is either zero or one.
974 __ sub(ecx, Immediate(0x01));
975 __ mov(ebx, edx);
976 __ xor_(ebx, eax);
977 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
978 __ xor_(ebx, eax);
979 // if eax was smi, ebx is now edx, else eax.
980
981 // Check if the non-smi operand is a heap number.
982 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
983 Immediate(isolate()->factory()->heap_number_map()));
984 // If heap number, handle it in the slow case.
985 __ j(equal, &slow, Label::kNear);
986 // Return non-equal (ebx is not zero)
987 __ mov(eax, ebx);
988 __ ret(0);
989
990 __ bind(&not_smis);
991 // If either operand is a JSObject or an oddball value, then they are not
992 // equal since their pointers are different
993 // There is no test for undetectability in strict equality.
994
995 // Get the type of the first operand.
996 // If the first object is a JS object, we have done pointer comparison.
997 Label first_non_object;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000998 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
999 __ CmpObjectType(eax, FIRST_JS_RECEIVER_TYPE, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001000 __ j(below, &first_non_object, Label::kNear);
1001
1002 // Return non-zero (eax is not zero)
1003 Label return_not_equal;
1004 STATIC_ASSERT(kHeapObjectTag != 0);
1005 __ bind(&return_not_equal);
1006 __ ret(0);
1007
1008 __ bind(&first_non_object);
1009 // Check for oddballs: true, false, null, undefined.
1010 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1011 __ j(equal, &return_not_equal);
1012
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001013 __ CmpObjectType(edx, FIRST_JS_RECEIVER_TYPE, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001014 __ j(above_equal, &return_not_equal);
1015
1016 // Check for oddballs: true, false, null, undefined.
1017 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1018 __ j(equal, &return_not_equal);
1019
1020 // Fall through to the general case.
1021 __ bind(&slow);
1022 }
1023
1024 // Generate the number comparison code.
1025 Label non_number_comparison;
1026 Label unordered;
1027 __ bind(&generic_heap_number_comparison);
1028 FloatingPointHelper::CheckFloatOperands(
1029 masm, &non_number_comparison, ebx);
1030 FloatingPointHelper::LoadFloatOperand(masm, eax);
1031 FloatingPointHelper::LoadFloatOperand(masm, edx);
1032 __ FCmp();
1033
1034 // Don't base result on EFLAGS when a NaN is involved.
1035 __ j(parity_even, &unordered, Label::kNear);
1036
1037 Label below_label, above_label;
1038 // Return a result of -1, 0, or 1, based on EFLAGS.
1039 __ j(below, &below_label, Label::kNear);
1040 __ j(above, &above_label, Label::kNear);
1041
1042 __ Move(eax, Immediate(0));
1043 __ ret(0);
1044
1045 __ bind(&below_label);
1046 __ mov(eax, Immediate(Smi::FromInt(-1)));
1047 __ ret(0);
1048
1049 __ bind(&above_label);
1050 __ mov(eax, Immediate(Smi::FromInt(1)));
1051 __ ret(0);
1052
1053 // If one of the numbers was NaN, then the result is always false.
1054 // The cc is never not-equal.
1055 __ bind(&unordered);
1056 DCHECK(cc != not_equal);
1057 if (cc == less || cc == less_equal) {
1058 __ mov(eax, Immediate(Smi::FromInt(1)));
1059 } else {
1060 __ mov(eax, Immediate(Smi::FromInt(-1)));
1061 }
1062 __ ret(0);
1063
1064 // The number comparison code did not provide a valid result.
1065 __ bind(&non_number_comparison);
1066
1067 // Fast negative check for internalized-to-internalized equality.
1068 Label check_for_strings;
1069 if (cc == equal) {
1070 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
1071 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
1072
1073 // We've already checked for object identity, so if both operands
1074 // are internalized they aren't equal. Register eax already holds a
1075 // non-zero value, which indicates not equal, so just return.
1076 __ ret(0);
1077 }
1078
1079 __ bind(&check_for_strings);
1080
1081 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx,
1082 &check_unequal_objects);
1083
1084 // Inline comparison of one-byte strings.
1085 if (cc == equal) {
1086 StringHelper::GenerateFlatOneByteStringEquals(masm, edx, eax, ecx, ebx);
1087 } else {
1088 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
1089 edi);
1090 }
1091#ifdef DEBUG
1092 __ Abort(kUnexpectedFallThroughFromStringComparison);
1093#endif
1094
1095 __ bind(&check_unequal_objects);
1096 if (cc == equal && !strict()) {
1097 // Non-strict equality. Objects are unequal if
1098 // they are both JSObjects and not undetectable,
1099 // and their pointers are different.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001100 Label return_unequal, undetectable;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001101 // At most one is a smi, so we can test for smi by adding the two.
1102 // A smi plus a heap object has the low bit set, a heap object plus
1103 // a heap object has the low bit clear.
1104 STATIC_ASSERT(kSmiTag == 0);
1105 STATIC_ASSERT(kSmiTagMask == 1);
1106 __ lea(ecx, Operand(eax, edx, times_1, 0));
1107 __ test(ecx, Immediate(kSmiTagMask));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001108 __ j(not_zero, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001109
1110 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1111 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
1112
1113 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
1114 1 << Map::kIsUndetectable);
1115 __ j(not_zero, &undetectable, Label::kNear);
1116 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
1117 1 << Map::kIsUndetectable);
1118 __ j(not_zero, &return_unequal, Label::kNear);
1119
1120 __ CmpInstanceType(ebx, FIRST_JS_RECEIVER_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001121 __ j(below, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001122 __ CmpInstanceType(ecx, FIRST_JS_RECEIVER_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001123 __ j(below, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001124
1125 __ bind(&return_unequal);
1126 // Return non-equal by returning the non-zero object pointer in eax.
1127 __ ret(0); // eax, edx were pushed
1128
1129 __ bind(&undetectable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
1131 1 << Map::kIsUndetectable);
1132 __ j(zero, &return_unequal, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001133 __ Move(eax, Immediate(EQUAL));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001134 __ ret(0); // eax, edx were pushed
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001135 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001136 __ bind(&runtime_call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001137
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001138 if (cc == equal) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001139 {
1140 FrameScope scope(masm, StackFrame::INTERNAL);
1141 __ Push(edx);
1142 __ Push(eax);
1143 __ CallRuntime(strict() ? Runtime::kStrictEqual : Runtime::kEqual);
1144 }
1145 // Turn true into 0 and false into some non-zero value.
1146 STATIC_ASSERT(EQUAL == 0);
1147 __ sub(eax, Immediate(isolate()->factory()->true_value()));
1148 __ Ret();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001149 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001150 // Push arguments below the return address.
1151 __ pop(ecx);
1152 __ push(edx);
1153 __ push(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001154 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001155
1156 // Restore return address on the stack.
1157 __ push(ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001158 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1159 // tagged as a small integer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001160 __ TailCallRuntime(Runtime::kCompare);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001161 }
1162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001163 __ bind(&miss);
1164 GenerateMiss(masm);
1165}
1166
1167
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001168static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
1169 // eax : number of arguments to the construct function
1170 // ebx : feedback vector
1171 // edx : slot in feedback vector (Smi)
1172 // edi : the function to call
1173
1174 {
1175 FrameScope scope(masm, StackFrame::INTERNAL);
1176
1177 // Number-of-arguments register must be smi-tagged to call out.
1178 __ SmiTag(eax);
1179 __ push(eax);
1180 __ push(edi);
1181 __ push(edx);
1182 __ push(ebx);
1183
1184 __ CallStub(stub);
1185
1186 __ pop(ebx);
1187 __ pop(edx);
1188 __ pop(edi);
1189 __ pop(eax);
1190 __ SmiUntag(eax);
1191 }
1192}
1193
1194
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001195static void GenerateRecordCallTarget(MacroAssembler* masm) {
1196 // Cache the called function in a feedback vector slot. Cache states
1197 // are uninitialized, monomorphic (indicated by a JSFunction), and
1198 // megamorphic.
1199 // eax : number of arguments to the construct function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001200 // ebx : feedback vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001201 // edx : slot in feedback vector (Smi)
1202 // edi : the function to call
1203 Isolate* isolate = masm->isolate();
1204 Label initialize, done, miss, megamorphic, not_array_function;
1205
1206 // Load the cache state into ecx.
1207 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1208 FixedArray::kHeaderSize));
1209
1210 // A monomorphic cache hit or an already megamorphic state: invoke the
1211 // function without changing the state.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001212 // We don't know if ecx is a WeakCell or a Symbol, but it's harmless to read
1213 // at this position in a symbol (see static asserts in
1214 // type-feedback-vector.h).
1215 Label check_allocation_site;
1216 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001217 __ j(equal, &done, Label::kFar);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001218 __ CompareRoot(ecx, Heap::kmegamorphic_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001219 __ j(equal, &done, Label::kFar);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001220 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1221 Heap::kWeakCellMapRootIndex);
1222 __ j(not_equal, &check_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001223
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001224 // If the weak cell is cleared, we have a new chance to become monomorphic.
1225 __ JumpIfSmi(FieldOperand(ecx, WeakCell::kValueOffset), &initialize);
1226 __ jmp(&megamorphic);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001227
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001228 __ bind(&check_allocation_site);
1229 // If we came here, we need to see if we are the array function.
1230 // If we didn't have a matching function, and we didn't find the megamorph
1231 // sentinel, then we have in the slot either some other function or an
1232 // AllocationSite.
1233 __ CompareRoot(FieldOperand(ecx, 0), Heap::kAllocationSiteMapRootIndex);
1234 __ j(not_equal, &miss);
1235
1236 // Make sure the function is the Array() function
1237 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1238 __ cmp(edi, ecx);
1239 __ j(not_equal, &megamorphic);
1240 __ jmp(&done, Label::kFar);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001241
1242 __ bind(&miss);
1243
1244 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1245 // megamorphic.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001246 __ CompareRoot(ecx, Heap::kuninitialized_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001247 __ j(equal, &initialize);
1248 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1249 // write-barrier is needed.
1250 __ bind(&megamorphic);
1251 __ mov(
1252 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1253 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
1254 __ jmp(&done, Label::kFar);
1255
1256 // An uninitialized cache is patched with the function or sentinel to
1257 // indicate the ElementsKind if function is the Array constructor.
1258 __ bind(&initialize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001259 // Make sure the function is the Array() function
1260 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1261 __ cmp(edi, ecx);
1262 __ j(not_equal, &not_array_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001263
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001264 // The target function is the Array constructor,
1265 // Create an AllocationSite if we don't already have it, store it in the
1266 // slot.
1267 CreateAllocationSiteStub create_stub(isolate);
1268 CallStubInRecordCallTarget(masm, &create_stub);
1269 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001270
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001271 __ bind(&not_array_function);
1272 CreateWeakCellStub weak_cell_stub(isolate);
1273 CallStubInRecordCallTarget(masm, &weak_cell_stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001274 __ bind(&done);
1275}
1276
1277
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001278void CallConstructStub::Generate(MacroAssembler* masm) {
1279 // eax : number of arguments
1280 // ebx : feedback vector
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001281 // edx : slot in feedback vector (Smi, for RecordCallTarget)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001282 // edi : constructor function
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001284 Label non_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001285 // Check that function is not a smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001286 __ JumpIfSmi(edi, &non_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001287 // Check that function is a JSFunction.
1288 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001289 __ j(not_equal, &non_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001290
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001291 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001292
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001293 Label feedback_register_initialized;
1294 // Put the AllocationSite from the feedback vector into ebx, or undefined.
1295 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
1296 FixedArray::kHeaderSize));
1297 Handle<Map> allocation_site_map = isolate()->factory()->allocation_site_map();
1298 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
1299 __ j(equal, &feedback_register_initialized);
1300 __ mov(ebx, isolate()->factory()->undefined_value());
1301 __ bind(&feedback_register_initialized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001302
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001303 __ AssertUndefinedOrAllocationSite(ebx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001304
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001305 // Pass new target to construct stub.
1306 __ mov(edx, edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001307
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001308 // Tail call to the function-specific construct stub (still in the caller
1309 // context at this point).
1310 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1311 __ mov(ecx, FieldOperand(ecx, SharedFunctionInfo::kConstructStubOffset));
1312 __ lea(ecx, FieldOperand(ecx, Code::kHeaderSize));
1313 __ jmp(ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001314
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001315 __ bind(&non_function);
1316 __ mov(edx, edi);
1317 __ Jump(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001318}
1319
1320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001321void CallICStub::HandleArrayCase(MacroAssembler* masm, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001322 // edi - function
1323 // edx - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001324 // ebx - vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001325 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1326 __ cmp(edi, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001327 __ j(not_equal, miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001328
1329 __ mov(eax, arg_count());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001330 // Reload ecx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001331 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1332 FixedArray::kHeaderSize));
1333
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001334 // Increment the call count for monomorphic function calls.
1335 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1336 FixedArray::kHeaderSize + kPointerSize),
1337 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001338
1339 __ mov(ebx, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001340 __ mov(edx, edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341 ArrayConstructorStub stub(masm->isolate(), arg_count());
1342 __ TailCallStub(&stub);
1343
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 // Unreachable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001345}
1346
1347
1348void CallICStub::Generate(MacroAssembler* masm) {
1349 // edi - function
1350 // edx - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001351 // ebx - vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001352 Isolate* isolate = masm->isolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001353 Label extra_checks_or_miss, call, call_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001354 int argc = arg_count();
1355 ParameterCount actual(argc);
1356
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001357 // The checks. First, does edi match the recorded monomorphic target?
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001358 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1359 FixedArray::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001360
1361 // We don't know that we have a weak cell. We might have a private symbol
1362 // or an AllocationSite, but the memory is safe to examine.
1363 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
1364 // FixedArray.
1365 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
1366 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
1367 // computed, meaning that it can't appear to be a pointer. If the low bit is
1368 // 0, then hash is computed, but the 0 bit prevents the field from appearing
1369 // to be a pointer.
1370 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
1371 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
1372 WeakCell::kValueOffset &&
1373 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
1374
1375 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
1376 __ j(not_equal, &extra_checks_or_miss);
1377
1378 // The compare above could have been a SMI/SMI comparison. Guard against this
1379 // convincing us that we have a monomorphic JSFunction.
1380 __ JumpIfSmi(edi, &extra_checks_or_miss);
1381
1382 // Increment the call count for monomorphic function calls.
1383 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1384 FixedArray::kHeaderSize + kPointerSize),
1385 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
1386
1387 __ bind(&call_function);
1388 __ Set(eax, argc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001389 __ Jump(masm->isolate()->builtins()->CallFunction(convert_mode(),
1390 tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001391 RelocInfo::CODE_TARGET);
1392
1393 __ bind(&extra_checks_or_miss);
1394 Label uninitialized, miss, not_allocation_site;
1395
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001396 __ cmp(ecx, Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001397 __ j(equal, &call);
1398
1399 // Check if we have an allocation site.
1400 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1401 Heap::kAllocationSiteMapRootIndex);
1402 __ j(not_equal, &not_allocation_site);
1403
1404 // We have an allocation site.
1405 HandleArrayCase(masm, &miss);
1406
1407 __ bind(&not_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001408
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001409 // The following cases attempt to handle MISS cases without going to the
1410 // runtime.
1411 if (FLAG_trace_ic) {
1412 __ jmp(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001413 }
1414
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001415 __ cmp(ecx, Immediate(TypeFeedbackVector::UninitializedSentinel(isolate)));
1416 __ j(equal, &uninitialized);
1417
1418 // We are going megamorphic. If the feedback is a JSFunction, it is fine
1419 // to handle it here. More complex cases are dealt with in the runtime.
1420 __ AssertNotSmi(ecx);
1421 __ CmpObjectType(ecx, JS_FUNCTION_TYPE, ecx);
1422 __ j(not_equal, &miss);
1423 __ mov(
1424 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1425 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001426
1427 __ bind(&call);
1428 __ Set(eax, argc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001429 __ Jump(masm->isolate()->builtins()->Call(convert_mode(), tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001430 RelocInfo::CODE_TARGET);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001431
1432 __ bind(&uninitialized);
1433
1434 // We are going monomorphic, provided we actually have a JSFunction.
1435 __ JumpIfSmi(edi, &miss);
1436
1437 // Goto miss case if we do not have a function.
1438 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1439 __ j(not_equal, &miss);
1440
1441 // Make sure the function is not the Array() function, which requires special
1442 // behavior on MISS.
1443 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1444 __ cmp(edi, ecx);
1445 __ j(equal, &miss);
1446
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001447 // Make sure the function belongs to the same native context.
1448 __ mov(ecx, FieldOperand(edi, JSFunction::kContextOffset));
1449 __ mov(ecx, ContextOperand(ecx, Context::NATIVE_CONTEXT_INDEX));
1450 __ cmp(ecx, NativeContextOperand());
1451 __ j(not_equal, &miss);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001452
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001453 // Initialize the call counter.
1454 __ mov(FieldOperand(ebx, edx, times_half_pointer_size,
1455 FixedArray::kHeaderSize + kPointerSize),
1456 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001457
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001458 // Store the function. Use a stub since we need a frame for allocation.
1459 // ebx - vector
1460 // edx - slot
1461 // edi - function
1462 {
1463 FrameScope scope(masm, StackFrame::INTERNAL);
1464 CreateWeakCellStub create_stub(isolate);
1465 __ push(edi);
1466 __ CallStub(&create_stub);
1467 __ pop(edi);
1468 }
1469
1470 __ jmp(&call_function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001471
1472 // We are here because tracing is on or we encountered a MISS case we can't
1473 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001474 __ bind(&miss);
1475 GenerateMiss(masm);
1476
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001477 __ jmp(&call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001478
1479 // Unreachable
1480 __ int3();
1481}
1482
1483
1484void CallICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001485 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001486
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001487 // Push the function and feedback info.
1488 __ push(edi);
1489 __ push(ebx);
1490 __ push(edx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001491
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001492 // Call the entry.
1493 __ CallRuntime(Runtime::kCallIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001494
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001495 // Move result to edi and exit the internal frame.
1496 __ mov(edi, eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001497}
1498
1499
1500bool CEntryStub::NeedsImmovableCode() {
1501 return false;
1502}
1503
1504
1505void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
1506 CEntryStub::GenerateAheadOfTime(isolate);
1507 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
1508 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
1509 // It is important that the store buffer overflow stubs are generated first.
1510 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
1511 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001512 CreateWeakCellStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001513 BinaryOpICStub::GenerateAheadOfTime(isolate);
1514 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001515 StoreFastElementStub::GenerateAheadOfTime(isolate);
1516 TypeofStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001517}
1518
1519
1520void CodeStub::GenerateFPStubs(Isolate* isolate) {
1521 CEntryStub save_doubles(isolate, 1, kSaveFPRegs);
1522 // Stubs might already be in the snapshot, detect that and don't regenerate,
1523 // which would lead to code stub initialization state being messed up.
1524 Code* save_doubles_code;
1525 if (!save_doubles.FindCodeInCache(&save_doubles_code)) {
1526 save_doubles_code = *(save_doubles.GetCode());
1527 }
1528 isolate->set_fp_stubs_generated(true);
1529}
1530
1531
1532void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1533 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1534 stub.GetCode();
1535}
1536
1537
1538void CEntryStub::Generate(MacroAssembler* masm) {
1539 // eax: number of arguments including receiver
1540 // ebx: pointer to C function (C callee-saved)
1541 // ebp: frame pointer (restored after C call)
1542 // esp: stack pointer (restored after C call)
1543 // esi: current context (C callee-saved)
1544 // edi: JS function of the caller (C callee-saved)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001545 //
1546 // If argv_in_register():
1547 // ecx: pointer to the first argument
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001548
1549 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1550
Ben Murdoch097c5b22016-05-18 11:27:45 +01001551 // Reserve space on the stack for the three arguments passed to the call. If
1552 // result size is greater than can be returned in registers, also reserve
1553 // space for the hidden argument for the result location, and space for the
1554 // result itself.
1555 int arg_stack_space = result_size() < 3 ? 3 : 4 + result_size();
1556
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001557 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001558 if (argv_in_register()) {
1559 DCHECK(!save_doubles());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001560 __ EnterApiExitFrame(arg_stack_space);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001561
1562 // Move argc and argv into the correct registers.
1563 __ mov(esi, ecx);
1564 __ mov(edi, eax);
1565 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001566 __ EnterExitFrame(arg_stack_space, save_doubles());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001567 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001568
1569 // ebx: pointer to C function (C callee-saved)
1570 // ebp: frame pointer (restored after C call)
1571 // esp: stack pointer (restored after C call)
1572 // edi: number of arguments including receiver (C callee-saved)
1573 // esi: pointer to the first argument (C callee-saved)
1574
1575 // Result returned in eax, or eax+edx if result size is 2.
1576
1577 // Check stack alignment.
1578 if (FLAG_debug_code) {
1579 __ CheckStackAlignment();
1580 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001581 // Call C function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001582 if (result_size() <= 2) {
1583 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
1584 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
1585 __ mov(Operand(esp, 2 * kPointerSize),
1586 Immediate(ExternalReference::isolate_address(isolate())));
1587 } else {
1588 DCHECK_EQ(3, result_size());
1589 // Pass a pointer to the result location as the first argument.
1590 __ lea(eax, Operand(esp, 4 * kPointerSize));
1591 __ mov(Operand(esp, 0 * kPointerSize), eax);
1592 __ mov(Operand(esp, 1 * kPointerSize), edi); // argc.
1593 __ mov(Operand(esp, 2 * kPointerSize), esi); // argv.
1594 __ mov(Operand(esp, 3 * kPointerSize),
1595 Immediate(ExternalReference::isolate_address(isolate())));
1596 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001597 __ call(ebx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001598
1599 if (result_size() > 2) {
1600 DCHECK_EQ(3, result_size());
1601#ifndef _WIN32
1602 // Restore the "hidden" argument on the stack which was popped by caller.
1603 __ sub(esp, Immediate(kPointerSize));
1604#endif
1605 // Read result values stored on stack. Result is stored above the arguments.
1606 __ mov(kReturnRegister0, Operand(esp, 4 * kPointerSize));
1607 __ mov(kReturnRegister1, Operand(esp, 5 * kPointerSize));
1608 __ mov(kReturnRegister2, Operand(esp, 6 * kPointerSize));
1609 }
1610 // Result is in eax, edx:eax or edi:edx:eax - do not destroy these registers!
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001611
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001612 // Check result for exception sentinel.
1613 Label exception_returned;
1614 __ cmp(eax, isolate()->factory()->exception());
1615 __ j(equal, &exception_returned);
1616
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001617 // Check that there is no pending exception, otherwise we
1618 // should have returned the exception sentinel.
1619 if (FLAG_debug_code) {
1620 __ push(edx);
1621 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1622 Label okay;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001623 ExternalReference pending_exception_address(
1624 Isolate::kPendingExceptionAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001625 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
1626 // Cannot use check here as it attempts to generate call into runtime.
1627 __ j(equal, &okay, Label::kNear);
1628 __ int3();
1629 __ bind(&okay);
1630 __ pop(edx);
1631 }
1632
1633 // Exit the JavaScript to C++ exit frame.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001634 __ LeaveExitFrame(save_doubles(), !argv_in_register());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001635 __ ret(0);
1636
1637 // Handling of exception.
1638 __ bind(&exception_returned);
1639
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001640 ExternalReference pending_handler_context_address(
1641 Isolate::kPendingHandlerContextAddress, isolate());
1642 ExternalReference pending_handler_code_address(
1643 Isolate::kPendingHandlerCodeAddress, isolate());
1644 ExternalReference pending_handler_offset_address(
1645 Isolate::kPendingHandlerOffsetAddress, isolate());
1646 ExternalReference pending_handler_fp_address(
1647 Isolate::kPendingHandlerFPAddress, isolate());
1648 ExternalReference pending_handler_sp_address(
1649 Isolate::kPendingHandlerSPAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001650
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001651 // Ask the runtime for help to determine the handler. This will set eax to
1652 // contain the current pending exception, don't clobber it.
1653 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
1654 isolate());
1655 {
1656 FrameScope scope(masm, StackFrame::MANUAL);
1657 __ PrepareCallCFunction(3, eax);
1658 __ mov(Operand(esp, 0 * kPointerSize), Immediate(0)); // argc.
1659 __ mov(Operand(esp, 1 * kPointerSize), Immediate(0)); // argv.
1660 __ mov(Operand(esp, 2 * kPointerSize),
1661 Immediate(ExternalReference::isolate_address(isolate())));
1662 __ CallCFunction(find_handler, 3);
1663 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001665 // Retrieve the handler context, SP and FP.
1666 __ mov(esi, Operand::StaticVariable(pending_handler_context_address));
1667 __ mov(esp, Operand::StaticVariable(pending_handler_sp_address));
1668 __ mov(ebp, Operand::StaticVariable(pending_handler_fp_address));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001669
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001670 // If the handler is a JS frame, restore the context to the frame. Note that
1671 // the context will be set to (esi == 0) for non-JS frames.
1672 Label skip;
1673 __ test(esi, esi);
1674 __ j(zero, &skip, Label::kNear);
1675 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
1676 __ bind(&skip);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001677
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001678 // Compute the handler entry address and jump to it.
1679 __ mov(edi, Operand::StaticVariable(pending_handler_code_address));
1680 __ mov(edx, Operand::StaticVariable(pending_handler_offset_address));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001681 // Check whether it's a turbofanned exception handler code before jump to it.
1682 Label not_turbo;
1683 __ push(eax);
1684 __ mov(eax, Operand(edi, Code::kKindSpecificFlags1Offset - kHeapObjectTag));
1685 __ and_(eax, Immediate(1 << Code::kIsTurbofannedBit));
1686 __ j(zero, &not_turbo);
1687 __ fninit();
1688 __ fld1();
1689 __ bind(&not_turbo);
1690 __ pop(eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001691 __ lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
1692 __ jmp(edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001693}
1694
1695
1696void JSEntryStub::Generate(MacroAssembler* masm) {
1697 Label invoke, handler_entry, exit;
1698 Label not_outermost_js, not_outermost_js_2;
1699
1700 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1701
1702 // Set up frame.
1703 __ push(ebp);
1704 __ mov(ebp, esp);
1705
1706 // Push marker in two places.
1707 int marker = type();
1708 __ push(Immediate(Smi::FromInt(marker))); // context slot
1709 __ push(Immediate(Smi::FromInt(marker))); // function slot
1710 // Save callee-saved registers (C calling conventions).
1711 __ push(edi);
1712 __ push(esi);
1713 __ push(ebx);
1714
1715 // Save copies of the top frame descriptor on the stack.
1716 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
1717 __ push(Operand::StaticVariable(c_entry_fp));
1718
1719 // If this is the outermost JS call, set js_entry_sp value.
1720 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
1721 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
1722 __ j(not_equal, &not_outermost_js, Label::kNear);
1723 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
1724 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1725 __ jmp(&invoke, Label::kNear);
1726 __ bind(&not_outermost_js);
1727 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
1728
1729 // Jump to a faked try block that does the invoke, with a faked catch
1730 // block that sets the pending exception.
1731 __ jmp(&invoke);
1732 __ bind(&handler_entry);
1733 handler_offset_ = handler_entry.pos();
1734 // Caught exception: Store result (exception) in the pending exception
1735 // field in the JSEnv and return a failure sentinel.
1736 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
1737 isolate());
1738 __ mov(Operand::StaticVariable(pending_exception), eax);
1739 __ mov(eax, Immediate(isolate()->factory()->exception()));
1740 __ jmp(&exit);
1741
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001742 // Invoke: Link this frame into the handler chain.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 __ bind(&invoke);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001744 __ PushStackHandler();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001745
1746 // Clear any pending exceptions.
1747 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1748 __ mov(Operand::StaticVariable(pending_exception), edx);
1749
1750 // Fake a receiver (NULL).
1751 __ push(Immediate(0)); // receiver
1752
1753 // Invoke the function by calling through JS entry trampoline builtin and
1754 // pop the faked function when we return. Notice that we cannot store a
1755 // reference to the trampoline code directly in this stub, because the
1756 // builtin stubs may not have been generated yet.
1757 if (type() == StackFrame::ENTRY_CONSTRUCT) {
1758 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
1759 isolate());
1760 __ mov(edx, Immediate(construct_entry));
1761 } else {
1762 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
1763 __ mov(edx, Immediate(entry));
1764 }
1765 __ mov(edx, Operand(edx, 0)); // deref address
1766 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
1767 __ call(edx);
1768
1769 // Unlink this frame from the handler chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001770 __ PopStackHandler();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001771
1772 __ bind(&exit);
1773 // Check if the current stack frame is marked as the outermost JS frame.
1774 __ pop(ebx);
1775 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1776 __ j(not_equal, &not_outermost_js_2);
1777 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
1778 __ bind(&not_outermost_js_2);
1779
1780 // Restore the top frame descriptor from the stack.
1781 __ pop(Operand::StaticVariable(ExternalReference(
1782 Isolate::kCEntryFPAddress, isolate())));
1783
1784 // Restore callee-saved registers (C calling conventions).
1785 __ pop(ebx);
1786 __ pop(esi);
1787 __ pop(edi);
1788 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
1789
1790 // Restore frame pointer and return.
1791 __ pop(ebp);
1792 __ ret(0);
1793}
1794
1795
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001796void InstanceOfStub::Generate(MacroAssembler* masm) {
1797 Register const object = edx; // Object (lhs).
1798 Register const function = eax; // Function (rhs).
1799 Register const object_map = ecx; // Map of {object}.
1800 Register const function_map = ebx; // Map of {function}.
1801 Register const function_prototype = function_map; // Prototype of {function}.
1802 Register const scratch = edi;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001803
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001804 DCHECK(object.is(InstanceOfDescriptor::LeftRegister()));
1805 DCHECK(function.is(InstanceOfDescriptor::RightRegister()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001806
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001807 // Check if {object} is a smi.
1808 Label object_is_smi;
1809 __ JumpIfSmi(object, &object_is_smi, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001810
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001811 // Lookup the {function} and the {object} map in the global instanceof cache.
1812 // Note: This is safe because we clear the global instanceof cache whenever
1813 // we change the prototype of any object.
1814 Label fast_case, slow_case;
1815 __ mov(object_map, FieldOperand(object, HeapObject::kMapOffset));
1816 __ CompareRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
1817 __ j(not_equal, &fast_case, Label::kNear);
1818 __ CompareRoot(object_map, scratch, Heap::kInstanceofCacheMapRootIndex);
1819 __ j(not_equal, &fast_case, Label::kNear);
1820 __ LoadRoot(eax, Heap::kInstanceofCacheAnswerRootIndex);
1821 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001822
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001823 // If {object} is a smi we can safely return false if {function} is a JS
1824 // function, otherwise we have to miss to the runtime and throw an exception.
1825 __ bind(&object_is_smi);
1826 __ JumpIfSmi(function, &slow_case);
1827 __ CmpObjectType(function, JS_FUNCTION_TYPE, function_map);
1828 __ j(not_equal, &slow_case);
1829 __ LoadRoot(eax, Heap::kFalseValueRootIndex);
1830 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001831
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001832 // Fast-case: The {function} must be a valid JSFunction.
1833 __ bind(&fast_case);
1834 __ JumpIfSmi(function, &slow_case);
1835 __ CmpObjectType(function, JS_FUNCTION_TYPE, function_map);
1836 __ j(not_equal, &slow_case);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001837
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001838 // Ensure that {function} has an instance prototype.
1839 __ test_b(FieldOperand(function_map, Map::kBitFieldOffset),
1840 static_cast<uint8_t>(1 << Map::kHasNonInstancePrototype));
1841 __ j(not_zero, &slow_case);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001842
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001843 // Get the "prototype" (or initial map) of the {function}.
1844 __ mov(function_prototype,
1845 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1846 __ AssertNotSmi(function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001847
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001848 // Resolve the prototype if the {function} has an initial map. Afterwards the
1849 // {function_prototype} will be either the JSReceiver prototype object or the
1850 // hole value, which means that no instances of the {function} were created so
1851 // far and hence we should return false.
1852 Label function_prototype_valid;
1853 Register const function_prototype_map = scratch;
1854 __ CmpObjectType(function_prototype, MAP_TYPE, function_prototype_map);
1855 __ j(not_equal, &function_prototype_valid, Label::kNear);
1856 __ mov(function_prototype,
1857 FieldOperand(function_prototype, Map::kPrototypeOffset));
1858 __ bind(&function_prototype_valid);
1859 __ AssertNotSmi(function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001860
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001861 // Update the global instanceof cache with the current {object} map and
1862 // {function}. The cached answer will be set when it is known below.
1863 __ StoreRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
1864 __ StoreRoot(object_map, scratch, Heap::kInstanceofCacheMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001865
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001866 // Loop through the prototype chain looking for the {function} prototype.
1867 // Assume true, and change to false if not found.
1868 Label done, loop, fast_runtime_fallback;
1869 __ mov(eax, isolate()->factory()->true_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001870 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001871
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001872 // Check if the object needs to be access checked.
1873 __ test_b(FieldOperand(object_map, Map::kBitFieldOffset),
1874 1 << Map::kIsAccessCheckNeeded);
1875 __ j(not_zero, &fast_runtime_fallback, Label::kNear);
1876 // Check if the current object is a Proxy.
1877 __ CmpInstanceType(object_map, JS_PROXY_TYPE);
1878 __ j(equal, &fast_runtime_fallback, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001879
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001880 __ mov(object, FieldOperand(object_map, Map::kPrototypeOffset));
1881 __ cmp(object, function_prototype);
1882 __ j(equal, &done, Label::kNear);
1883 __ mov(object_map, FieldOperand(object, HeapObject::kMapOffset));
1884 __ cmp(object, isolate()->factory()->null_value());
1885 __ j(not_equal, &loop);
1886 __ mov(eax, isolate()->factory()->false_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001887
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001888 __ bind(&done);
1889 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
1890 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001891
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001892 // Found Proxy or access check needed: Call the runtime.
1893 __ bind(&fast_runtime_fallback);
1894 __ PopReturnAddressTo(scratch);
1895 __ Push(object);
1896 __ Push(function_prototype);
1897 __ PushReturnAddressFrom(scratch);
1898 // Invalidate the instanceof cache.
1899 __ Move(eax, Immediate(Smi::FromInt(0)));
1900 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheFunctionRootIndex);
1901 __ TailCallRuntime(Runtime::kHasInPrototypeChain);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001902
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001903 // Slow-case: Call the %InstanceOf runtime function.
1904 __ bind(&slow_case);
1905 __ PopReturnAddressTo(scratch);
1906 __ Push(object);
1907 __ Push(function);
1908 __ PushReturnAddressFrom(scratch);
1909 __ TailCallRuntime(Runtime::kInstanceOf);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001910}
1911
1912
1913// -------------------------------------------------------------------------
1914// StringCharCodeAtGenerator
1915
1916void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
1917 // If the receiver is a smi trigger the non-string case.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001918 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
1919 __ JumpIfSmi(object_, receiver_not_string_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001920
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001921 // Fetch the instance type of the receiver into result register.
1922 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
1923 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
1924 // If the receiver is not a string trigger the non-string case.
1925 __ test(result_, Immediate(kIsNotStringMask));
1926 __ j(not_zero, receiver_not_string_);
1927 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001928
1929 // If the index is non-smi trigger the non-smi case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001930 __ JumpIfNotSmi(index_, &index_not_smi_);
1931 __ bind(&got_smi_index_);
1932
1933 // Check for index out of range.
1934 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
1935 __ j(above_equal, index_out_of_range_);
1936
1937 __ SmiUntag(index_);
1938
1939 Factory* factory = masm->isolate()->factory();
1940 StringCharLoadGenerator::Generate(
1941 masm, factory, object_, index_, result_, &call_runtime_);
1942
1943 __ SmiTag(result_);
1944 __ bind(&exit_);
1945}
1946
1947
1948void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001949 MacroAssembler* masm, EmbedMode embed_mode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001950 const RuntimeCallHelper& call_helper) {
1951 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
1952
1953 // Index is not a smi.
1954 __ bind(&index_not_smi_);
1955 // If index is a heap number, try converting it to an integer.
1956 __ CheckMap(index_,
1957 masm->isolate()->factory()->heap_number_map(),
1958 index_not_number_,
1959 DONT_DO_SMI_CHECK);
1960 call_helper.BeforeCall(masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001961 if (embed_mode == PART_OF_IC_HANDLER) {
1962 __ push(LoadWithVectorDescriptor::VectorRegister());
1963 __ push(LoadDescriptor::SlotRegister());
1964 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001965 __ push(object_);
1966 __ push(index_); // Consumed by runtime conversion function.
1967 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001968 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001969 } else {
1970 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
1971 // NumberToSmi discards numbers that are not exact integers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001972 __ CallRuntime(Runtime::kNumberToSmi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001973 }
1974 if (!index_.is(eax)) {
1975 // Save the conversion result before the pop instructions below
1976 // have a chance to overwrite it.
1977 __ mov(index_, eax);
1978 }
1979 __ pop(object_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001980 if (embed_mode == PART_OF_IC_HANDLER) {
1981 __ pop(LoadDescriptor::SlotRegister());
1982 __ pop(LoadWithVectorDescriptor::VectorRegister());
1983 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001984 // Reload the instance type.
1985 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
1986 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
1987 call_helper.AfterCall(masm);
1988 // If index is still not a smi, it must be out of range.
1989 STATIC_ASSERT(kSmiTag == 0);
1990 __ JumpIfNotSmi(index_, index_out_of_range_);
1991 // Otherwise, return to the fast path.
1992 __ jmp(&got_smi_index_);
1993
1994 // Call runtime. We get here when the receiver is a string and the
1995 // index is a number, but the code of getting the actual character
1996 // is too complex (e.g., when the string needs to be flattened).
1997 __ bind(&call_runtime_);
1998 call_helper.BeforeCall(masm);
1999 __ push(object_);
2000 __ SmiTag(index_);
2001 __ push(index_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002002 __ CallRuntime(Runtime::kStringCharCodeAtRT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002003 if (!result_.is(eax)) {
2004 __ mov(result_, eax);
2005 }
2006 call_helper.AfterCall(masm);
2007 __ jmp(&exit_);
2008
2009 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
2010}
2011
2012
2013// -------------------------------------------------------------------------
2014// StringCharFromCodeGenerator
2015
2016void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
2017 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2018 STATIC_ASSERT(kSmiTag == 0);
2019 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002020 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
2021 __ test(code_, Immediate(kSmiTagMask |
2022 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002023 __ j(not_zero, &slow_case_);
2024
2025 Factory* factory = masm->isolate()->factory();
2026 __ Move(result_, Immediate(factory->single_character_string_cache()));
2027 STATIC_ASSERT(kSmiTag == 0);
2028 STATIC_ASSERT(kSmiTagSize == 1);
2029 STATIC_ASSERT(kSmiShiftSize == 0);
2030 // At this point code register contains smi tagged one byte char code.
2031 __ mov(result_, FieldOperand(result_,
2032 code_, times_half_pointer_size,
2033 FixedArray::kHeaderSize));
2034 __ cmp(result_, factory->undefined_value());
2035 __ j(equal, &slow_case_);
2036 __ bind(&exit_);
2037}
2038
2039
2040void StringCharFromCodeGenerator::GenerateSlow(
2041 MacroAssembler* masm,
2042 const RuntimeCallHelper& call_helper) {
2043 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
2044
2045 __ bind(&slow_case_);
2046 call_helper.BeforeCall(masm);
2047 __ push(code_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002048 __ CallRuntime(Runtime::kStringCharFromCode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002049 if (!result_.is(eax)) {
2050 __ mov(result_, eax);
2051 }
2052 call_helper.AfterCall(masm);
2053 __ jmp(&exit_);
2054
2055 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
2056}
2057
2058
2059void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2060 Register dest,
2061 Register src,
2062 Register count,
2063 Register scratch,
2064 String::Encoding encoding) {
2065 DCHECK(!scratch.is(dest));
2066 DCHECK(!scratch.is(src));
2067 DCHECK(!scratch.is(count));
2068
2069 // Nothing to do for zero characters.
2070 Label done;
2071 __ test(count, count);
2072 __ j(zero, &done);
2073
2074 // Make count the number of bytes to copy.
2075 if (encoding == String::TWO_BYTE_ENCODING) {
2076 __ shl(count, 1);
2077 }
2078
2079 Label loop;
2080 __ bind(&loop);
2081 __ mov_b(scratch, Operand(src, 0));
2082 __ mov_b(Operand(dest, 0), scratch);
2083 __ inc(src);
2084 __ inc(dest);
2085 __ dec(count);
2086 __ j(not_zero, &loop);
2087
2088 __ bind(&done);
2089}
2090
2091
2092void SubStringStub::Generate(MacroAssembler* masm) {
2093 Label runtime;
2094
2095 // Stack frame on entry.
2096 // esp[0]: return address
2097 // esp[4]: to
2098 // esp[8]: from
2099 // esp[12]: string
2100
2101 // Make sure first argument is a string.
2102 __ mov(eax, Operand(esp, 3 * kPointerSize));
2103 STATIC_ASSERT(kSmiTag == 0);
2104 __ JumpIfSmi(eax, &runtime);
2105 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
2106 __ j(NegateCondition(is_string), &runtime);
2107
2108 // eax: string
2109 // ebx: instance type
2110
2111 // Calculate length of sub string using the smi values.
2112 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
2113 __ JumpIfNotSmi(ecx, &runtime);
2114 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
2115 __ JumpIfNotSmi(edx, &runtime);
2116 __ sub(ecx, edx);
2117 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
2118 Label not_original_string;
2119 // Shorter than original string's length: an actual substring.
2120 __ j(below, &not_original_string, Label::kNear);
2121 // Longer than original string's length or negative: unsafe arguments.
2122 __ j(above, &runtime);
2123 // Return original string.
2124 Counters* counters = isolate()->counters();
2125 __ IncrementCounter(counters->sub_string_native(), 1);
2126 __ ret(3 * kPointerSize);
2127 __ bind(&not_original_string);
2128
2129 Label single_char;
2130 __ cmp(ecx, Immediate(Smi::FromInt(1)));
2131 __ j(equal, &single_char);
2132
2133 // eax: string
2134 // ebx: instance type
2135 // ecx: sub string length (smi)
2136 // edx: from index (smi)
2137 // Deal with different string types: update the index if necessary
2138 // and put the underlying string into edi.
2139 Label underlying_unpacked, sliced_string, seq_or_external_string;
2140 // If the string is not indirect, it can only be sequential or external.
2141 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2142 STATIC_ASSERT(kIsIndirectStringMask != 0);
2143 __ test(ebx, Immediate(kIsIndirectStringMask));
2144 __ j(zero, &seq_or_external_string, Label::kNear);
2145
2146 Factory* factory = isolate()->factory();
2147 __ test(ebx, Immediate(kSlicedNotConsMask));
2148 __ j(not_zero, &sliced_string, Label::kNear);
2149 // Cons string. Check whether it is flat, then fetch first part.
2150 // Flat cons strings have an empty second part.
2151 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
2152 factory->empty_string());
2153 __ j(not_equal, &runtime);
2154 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
2155 // Update instance type.
2156 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2157 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2158 __ jmp(&underlying_unpacked, Label::kNear);
2159
2160 __ bind(&sliced_string);
2161 // Sliced string. Fetch parent and adjust start index by offset.
2162 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
2163 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
2164 // Update instance type.
2165 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2166 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2167 __ jmp(&underlying_unpacked, Label::kNear);
2168
2169 __ bind(&seq_or_external_string);
2170 // Sequential or external string. Just move string to the expected register.
2171 __ mov(edi, eax);
2172
2173 __ bind(&underlying_unpacked);
2174
2175 if (FLAG_string_slices) {
2176 Label copy_routine;
2177 // edi: underlying subject string
2178 // ebx: instance type of underlying subject string
2179 // edx: adjusted start index (smi)
2180 // ecx: length (smi)
2181 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
2182 // Short slice. Copy instead of slicing.
2183 __ j(less, &copy_routine);
2184 // Allocate new sliced string. At this point we do not reload the instance
2185 // type including the string encoding because we simply rely on the info
2186 // provided by the original string. It does not matter if the original
2187 // string's encoding is wrong because we always have to recheck encoding of
2188 // the newly created string's parent anyways due to externalized strings.
2189 Label two_byte_slice, set_slice_header;
2190 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
2191 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2192 __ test(ebx, Immediate(kStringEncodingMask));
2193 __ j(zero, &two_byte_slice, Label::kNear);
2194 __ AllocateOneByteSlicedString(eax, ebx, no_reg, &runtime);
2195 __ jmp(&set_slice_header, Label::kNear);
2196 __ bind(&two_byte_slice);
2197 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
2198 __ bind(&set_slice_header);
2199 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
2200 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
2201 Immediate(String::kEmptyHashField));
2202 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
2203 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
2204 __ IncrementCounter(counters->sub_string_native(), 1);
2205 __ ret(3 * kPointerSize);
2206
2207 __ bind(&copy_routine);
2208 }
2209
2210 // edi: underlying subject string
2211 // ebx: instance type of underlying subject string
2212 // edx: adjusted start index (smi)
2213 // ecx: length (smi)
2214 // The subject string can only be external or sequential string of either
2215 // encoding at this point.
2216 Label two_byte_sequential, runtime_drop_two, sequential_string;
2217 STATIC_ASSERT(kExternalStringTag != 0);
2218 STATIC_ASSERT(kSeqStringTag == 0);
2219 __ test_b(ebx, kExternalStringTag);
2220 __ j(zero, &sequential_string);
2221
2222 // Handle external string.
2223 // Rule out short external strings.
2224 STATIC_ASSERT(kShortExternalStringTag != 0);
2225 __ test_b(ebx, kShortExternalStringMask);
2226 __ j(not_zero, &runtime);
2227 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
2228 // Move the pointer so that offset-wise, it looks like a sequential string.
2229 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2230 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2231
2232 __ bind(&sequential_string);
2233 // Stash away (adjusted) index and (underlying) string.
2234 __ push(edx);
2235 __ push(edi);
2236 __ SmiUntag(ecx);
2237 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
2238 __ test_b(ebx, kStringEncodingMask);
2239 __ j(zero, &two_byte_sequential);
2240
2241 // Sequential one byte string. Allocate the result.
2242 __ AllocateOneByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
2243
2244 // eax: result string
2245 // ecx: result string length
2246 // Locate first character of result.
2247 __ mov(edi, eax);
2248 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
2249 // Load string argument and locate character of sub string start.
2250 __ pop(edx);
2251 __ pop(ebx);
2252 __ SmiUntag(ebx);
2253 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqOneByteString::kHeaderSize));
2254
2255 // eax: result string
2256 // ecx: result length
2257 // edi: first character of result
2258 // edx: character of sub string start
2259 StringHelper::GenerateCopyCharacters(
2260 masm, edi, edx, ecx, ebx, String::ONE_BYTE_ENCODING);
2261 __ IncrementCounter(counters->sub_string_native(), 1);
2262 __ ret(3 * kPointerSize);
2263
2264 __ bind(&two_byte_sequential);
2265 // Sequential two-byte string. Allocate the result.
2266 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
2267
2268 // eax: result string
2269 // ecx: result string length
2270 // Locate first character of result.
2271 __ mov(edi, eax);
2272 __ add(edi,
2273 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2274 // Load string argument and locate character of sub string start.
2275 __ pop(edx);
2276 __ pop(ebx);
2277 // As from is a smi it is 2 times the value which matches the size of a two
2278 // byte character.
2279 STATIC_ASSERT(kSmiTag == 0);
2280 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2281 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqTwoByteString::kHeaderSize));
2282
2283 // eax: result string
2284 // ecx: result length
2285 // edi: first character of result
2286 // edx: character of sub string start
2287 StringHelper::GenerateCopyCharacters(
2288 masm, edi, edx, ecx, ebx, String::TWO_BYTE_ENCODING);
2289 __ IncrementCounter(counters->sub_string_native(), 1);
2290 __ ret(3 * kPointerSize);
2291
2292 // Drop pushed values on the stack before tail call.
2293 __ bind(&runtime_drop_two);
2294 __ Drop(2);
2295
2296 // Just jump to runtime to create the sub string.
2297 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002298 __ TailCallRuntime(Runtime::kSubString);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002299
2300 __ bind(&single_char);
2301 // eax: string
2302 // ebx: instance type
2303 // ecx: sub string length (smi)
2304 // edx: from index (smi)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002305 StringCharAtGenerator generator(eax, edx, ecx, eax, &runtime, &runtime,
2306 &runtime, STRING_INDEX_IS_NUMBER,
2307 RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002308 generator.GenerateFast(masm);
2309 __ ret(3 * kPointerSize);
2310 generator.SkipSlow(masm, &runtime);
2311}
2312
2313
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002314void ToNumberStub::Generate(MacroAssembler* masm) {
2315 // The ToNumber stub takes one argument in eax.
2316 Label not_smi;
2317 __ JumpIfNotSmi(eax, &not_smi, Label::kNear);
2318 __ Ret();
2319 __ bind(&not_smi);
2320
2321 Label not_heap_number;
2322 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2323 __ j(not_equal, &not_heap_number, Label::kNear);
2324 __ Ret();
2325 __ bind(&not_heap_number);
2326
2327 Label not_string, slow_string;
2328 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
2329 // eax: object
2330 // edi: object map
2331 __ j(above_equal, &not_string, Label::kNear);
2332 // Check if string has a cached array index.
2333 __ test(FieldOperand(eax, String::kHashFieldOffset),
2334 Immediate(String::kContainsCachedArrayIndexMask));
2335 __ j(not_zero, &slow_string, Label::kNear);
2336 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
2337 __ IndexFromHash(eax, eax);
2338 __ Ret();
2339 __ bind(&slow_string);
2340 __ pop(ecx); // Pop return address.
2341 __ push(eax); // Push argument.
2342 __ push(ecx); // Push return address.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002343 __ TailCallRuntime(Runtime::kStringToNumber);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002344 __ bind(&not_string);
2345
2346 Label not_oddball;
2347 __ CmpInstanceType(edi, ODDBALL_TYPE);
2348 __ j(not_equal, &not_oddball, Label::kNear);
2349 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
2350 __ Ret();
2351 __ bind(&not_oddball);
2352
2353 __ pop(ecx); // Pop return address.
2354 __ push(eax); // Push argument.
2355 __ push(ecx); // Push return address.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002356 __ TailCallRuntime(Runtime::kToNumber);
2357}
2358
2359
2360void ToLengthStub::Generate(MacroAssembler* masm) {
2361 // The ToLength stub takes on argument in eax.
2362 Label not_smi, positive_smi;
2363 __ JumpIfNotSmi(eax, &not_smi, Label::kNear);
2364 STATIC_ASSERT(kSmiTag == 0);
2365 __ test(eax, eax);
2366 __ j(greater_equal, &positive_smi, Label::kNear);
2367 __ xor_(eax, eax);
2368 __ bind(&positive_smi);
2369 __ Ret();
2370 __ bind(&not_smi);
2371
2372 __ pop(ecx); // Pop return address.
2373 __ push(eax); // Push argument.
2374 __ push(ecx); // Push return address.
2375 __ TailCallRuntime(Runtime::kToLength);
2376}
2377
2378
2379void ToStringStub::Generate(MacroAssembler* masm) {
2380 // The ToString stub takes one argument in eax.
2381 Label is_number;
2382 __ JumpIfSmi(eax, &is_number, Label::kNear);
2383
2384 Label not_string;
2385 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
2386 // eax: receiver
2387 // edi: receiver map
2388 __ j(above_equal, &not_string, Label::kNear);
2389 __ Ret();
2390 __ bind(&not_string);
2391
2392 Label not_heap_number;
2393 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2394 __ j(not_equal, &not_heap_number, Label::kNear);
2395 __ bind(&is_number);
2396 NumberToStringStub stub(isolate());
2397 __ TailCallStub(&stub);
2398 __ bind(&not_heap_number);
2399
2400 Label not_oddball;
2401 __ CmpInstanceType(edi, ODDBALL_TYPE);
2402 __ j(not_equal, &not_oddball, Label::kNear);
2403 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
2404 __ Ret();
2405 __ bind(&not_oddball);
2406
2407 __ pop(ecx); // Pop return address.
2408 __ push(eax); // Push argument.
2409 __ push(ecx); // Push return address.
2410 __ TailCallRuntime(Runtime::kToString);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002411}
2412
2413
Ben Murdoch097c5b22016-05-18 11:27:45 +01002414void ToNameStub::Generate(MacroAssembler* masm) {
2415 // The ToName stub takes one argument in eax.
2416 Label is_number;
2417 __ JumpIfSmi(eax, &is_number, Label::kNear);
2418
2419 Label not_name;
2420 STATIC_ASSERT(FIRST_NAME_TYPE == FIRST_TYPE);
2421 __ CmpObjectType(eax, LAST_NAME_TYPE, edi);
2422 // eax: receiver
2423 // edi: receiver map
2424 __ j(above, &not_name, Label::kNear);
2425 __ Ret();
2426 __ bind(&not_name);
2427
2428 Label not_heap_number;
2429 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2430 __ j(not_equal, &not_heap_number, Label::kNear);
2431 __ bind(&is_number);
2432 NumberToStringStub stub(isolate());
2433 __ TailCallStub(&stub);
2434 __ bind(&not_heap_number);
2435
2436 Label not_oddball;
2437 __ CmpInstanceType(edi, ODDBALL_TYPE);
2438 __ j(not_equal, &not_oddball, Label::kNear);
2439 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
2440 __ Ret();
2441 __ bind(&not_oddball);
2442
2443 __ pop(ecx); // Pop return address.
2444 __ push(eax); // Push argument.
2445 __ push(ecx); // Push return address.
2446 __ TailCallRuntime(Runtime::kToName);
2447}
2448
2449
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002450void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
2451 Register left,
2452 Register right,
2453 Register scratch1,
2454 Register scratch2) {
2455 Register length = scratch1;
2456
2457 // Compare lengths.
2458 Label strings_not_equal, check_zero_length;
2459 __ mov(length, FieldOperand(left, String::kLengthOffset));
2460 __ cmp(length, FieldOperand(right, String::kLengthOffset));
2461 __ j(equal, &check_zero_length, Label::kNear);
2462 __ bind(&strings_not_equal);
2463 __ Move(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
2464 __ ret(0);
2465
2466 // Check if the length is zero.
2467 Label compare_chars;
2468 __ bind(&check_zero_length);
2469 STATIC_ASSERT(kSmiTag == 0);
2470 __ test(length, length);
2471 __ j(not_zero, &compare_chars, Label::kNear);
2472 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2473 __ ret(0);
2474
2475 // Compare characters.
2476 __ bind(&compare_chars);
2477 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
2478 &strings_not_equal, Label::kNear);
2479
2480 // Characters are equal.
2481 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2482 __ ret(0);
2483}
2484
2485
2486void StringHelper::GenerateCompareFlatOneByteStrings(
2487 MacroAssembler* masm, Register left, Register right, Register scratch1,
2488 Register scratch2, Register scratch3) {
2489 Counters* counters = masm->isolate()->counters();
2490 __ IncrementCounter(counters->string_compare_native(), 1);
2491
2492 // Find minimum length.
2493 Label left_shorter;
2494 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
2495 __ mov(scratch3, scratch1);
2496 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
2497
2498 Register length_delta = scratch3;
2499
2500 __ j(less_equal, &left_shorter, Label::kNear);
2501 // Right string is shorter. Change scratch1 to be length of right string.
2502 __ sub(scratch1, length_delta);
2503 __ bind(&left_shorter);
2504
2505 Register min_length = scratch1;
2506
2507 // If either length is zero, just compare lengths.
2508 Label compare_lengths;
2509 __ test(min_length, min_length);
2510 __ j(zero, &compare_lengths, Label::kNear);
2511
2512 // Compare characters.
2513 Label result_not_equal;
2514 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
2515 &result_not_equal, Label::kNear);
2516
2517 // Compare lengths - strings up to min-length are equal.
2518 __ bind(&compare_lengths);
2519 __ test(length_delta, length_delta);
2520 Label length_not_equal;
2521 __ j(not_zero, &length_not_equal, Label::kNear);
2522
2523 // Result is EQUAL.
2524 STATIC_ASSERT(EQUAL == 0);
2525 STATIC_ASSERT(kSmiTag == 0);
2526 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2527 __ ret(0);
2528
2529 Label result_greater;
2530 Label result_less;
2531 __ bind(&length_not_equal);
2532 __ j(greater, &result_greater, Label::kNear);
2533 __ jmp(&result_less, Label::kNear);
2534 __ bind(&result_not_equal);
2535 __ j(above, &result_greater, Label::kNear);
2536 __ bind(&result_less);
2537
2538 // Result is LESS.
2539 __ Move(eax, Immediate(Smi::FromInt(LESS)));
2540 __ ret(0);
2541
2542 // Result is GREATER.
2543 __ bind(&result_greater);
2544 __ Move(eax, Immediate(Smi::FromInt(GREATER)));
2545 __ ret(0);
2546}
2547
2548
2549void StringHelper::GenerateOneByteCharsCompareLoop(
2550 MacroAssembler* masm, Register left, Register right, Register length,
2551 Register scratch, Label* chars_not_equal,
2552 Label::Distance chars_not_equal_near) {
2553 // Change index to run from -length to -1 by adding length to string
2554 // start. This means that loop ends when index reaches zero, which
2555 // doesn't need an additional compare.
2556 __ SmiUntag(length);
2557 __ lea(left,
2558 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
2559 __ lea(right,
2560 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
2561 __ neg(length);
2562 Register index = length; // index = -length;
2563
2564 // Compare loop.
2565 Label loop;
2566 __ bind(&loop);
2567 __ mov_b(scratch, Operand(left, index, times_1, 0));
2568 __ cmpb(scratch, Operand(right, index, times_1, 0));
2569 __ j(not_equal, chars_not_equal, chars_not_equal_near);
2570 __ inc(index);
2571 __ j(not_zero, &loop);
2572}
2573
2574
2575void StringCompareStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002576 // ----------- S t a t e -------------
2577 // -- edx : left string
2578 // -- eax : right string
2579 // -- esp[0] : return address
2580 // -----------------------------------
2581 __ AssertString(edx);
2582 __ AssertString(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002583
2584 Label not_same;
2585 __ cmp(edx, eax);
2586 __ j(not_equal, &not_same, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002587 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2588 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002589 __ Ret();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002590
2591 __ bind(&not_same);
2592
2593 // Check that both objects are sequential one-byte strings.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002594 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002595 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx, &runtime);
2596
2597 // Compare flat one-byte strings.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002598 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002599 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
2600 edi);
2601
2602 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
2603 // tagged as a small integer.
2604 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002605 __ PopReturnAddressTo(ecx);
2606 __ Push(edx);
2607 __ Push(eax);
2608 __ PushReturnAddressFrom(ecx);
2609 __ TailCallRuntime(Runtime::kStringCompare);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002610}
2611
2612
2613void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
2614 // ----------- S t a t e -------------
2615 // -- edx : left
2616 // -- eax : right
2617 // -- esp[0] : return address
2618 // -----------------------------------
2619
2620 // Load ecx with the allocation site. We stick an undefined dummy value here
2621 // and replace it with the real allocation site later when we instantiate this
2622 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
2623 __ mov(ecx, handle(isolate()->heap()->undefined_value()));
2624
2625 // Make sure that we actually patched the allocation site.
2626 if (FLAG_debug_code) {
2627 __ test(ecx, Immediate(kSmiTagMask));
2628 __ Assert(not_equal, kExpectedAllocationSite);
2629 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
2630 isolate()->factory()->allocation_site_map());
2631 __ Assert(equal, kExpectedAllocationSite);
2632 }
2633
2634 // Tail call into the stub that handles binary operations with allocation
2635 // sites.
2636 BinaryOpWithAllocationSiteStub stub(isolate(), state());
2637 __ TailCallStub(&stub);
2638}
2639
2640
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002641void CompareICStub::GenerateBooleans(MacroAssembler* masm) {
2642 DCHECK_EQ(CompareICState::BOOLEAN, state());
2643 Label miss;
2644 Label::Distance const miss_distance =
2645 masm->emit_debug_code() ? Label::kFar : Label::kNear;
2646
2647 __ JumpIfSmi(edx, &miss, miss_distance);
2648 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
2649 __ JumpIfSmi(eax, &miss, miss_distance);
2650 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2651 __ JumpIfNotRoot(ecx, Heap::kBooleanMapRootIndex, &miss, miss_distance);
2652 __ JumpIfNotRoot(ebx, Heap::kBooleanMapRootIndex, &miss, miss_distance);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002653 if (!Token::IsEqualityOp(op())) {
2654 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
2655 __ AssertSmi(eax);
2656 __ mov(edx, FieldOperand(edx, Oddball::kToNumberOffset));
2657 __ AssertSmi(edx);
2658 __ xchg(eax, edx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002659 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002660 __ sub(eax, edx);
2661 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002662
2663 __ bind(&miss);
2664 GenerateMiss(masm);
2665}
2666
2667
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002668void CompareICStub::GenerateSmis(MacroAssembler* masm) {
2669 DCHECK(state() == CompareICState::SMI);
2670 Label miss;
2671 __ mov(ecx, edx);
2672 __ or_(ecx, eax);
2673 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
2674
2675 if (GetCondition() == equal) {
2676 // For equality we do not care about the sign of the result.
2677 __ sub(eax, edx);
2678 } else {
2679 Label done;
2680 __ sub(edx, eax);
2681 __ j(no_overflow, &done, Label::kNear);
2682 // Correct sign of result in case of overflow.
2683 __ not_(edx);
2684 __ bind(&done);
2685 __ mov(eax, edx);
2686 }
2687 __ ret(0);
2688
2689 __ bind(&miss);
2690 GenerateMiss(masm);
2691}
2692
2693
2694void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
2695 DCHECK(state() == CompareICState::NUMBER);
2696
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002697 Label generic_stub, check_left;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002698 Label unordered, maybe_undefined1, maybe_undefined2;
2699 Label miss;
2700
2701 if (left() == CompareICState::SMI) {
2702 __ JumpIfNotSmi(edx, &miss);
2703 }
2704 if (right() == CompareICState::SMI) {
2705 __ JumpIfNotSmi(eax, &miss);
2706 }
2707
2708 // Inlining the double comparison and falling back to the general compare
2709 // stub if NaN is involved or SSE2 or CMOV is unsupported.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002710 __ JumpIfSmi(eax, &check_left, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002711 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
2712 isolate()->factory()->heap_number_map());
2713 __ j(not_equal, &maybe_undefined1, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002714
2715 __ bind(&check_left);
2716 __ JumpIfSmi(edx, &generic_stub, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002717 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
2718 isolate()->factory()->heap_number_map());
2719 __ j(not_equal, &maybe_undefined2, Label::kNear);
2720
2721 __ bind(&unordered);
2722 __ bind(&generic_stub);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002723 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002724 CompareICState::GENERIC, CompareICState::GENERIC);
2725 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
2726
2727 __ bind(&maybe_undefined1);
2728 if (Token::IsOrderedRelationalCompareOp(op())) {
2729 __ cmp(eax, Immediate(isolate()->factory()->undefined_value()));
2730 __ j(not_equal, &miss);
2731 __ JumpIfSmi(edx, &unordered);
2732 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
2733 __ j(not_equal, &maybe_undefined2, Label::kNear);
2734 __ jmp(&unordered);
2735 }
2736
2737 __ bind(&maybe_undefined2);
2738 if (Token::IsOrderedRelationalCompareOp(op())) {
2739 __ cmp(edx, Immediate(isolate()->factory()->undefined_value()));
2740 __ j(equal, &unordered);
2741 }
2742
2743 __ bind(&miss);
2744 GenerateMiss(masm);
2745}
2746
2747
2748void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
2749 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
2750 DCHECK(GetCondition() == equal);
2751
2752 // Registers containing left and right operands respectively.
2753 Register left = edx;
2754 Register right = eax;
2755 Register tmp1 = ecx;
2756 Register tmp2 = ebx;
2757
2758 // Check that both operands are heap objects.
2759 Label miss;
2760 __ mov(tmp1, left);
2761 STATIC_ASSERT(kSmiTag == 0);
2762 __ and_(tmp1, right);
2763 __ JumpIfSmi(tmp1, &miss, Label::kNear);
2764
2765 // Check that both operands are internalized strings.
2766 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
2767 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
2768 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
2769 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
2770 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2771 __ or_(tmp1, tmp2);
2772 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2773 __ j(not_zero, &miss, Label::kNear);
2774
2775 // Internalized strings are compared by identity.
2776 Label done;
2777 __ cmp(left, right);
2778 // Make sure eax is non-zero. At this point input operands are
2779 // guaranteed to be non-zero.
2780 DCHECK(right.is(eax));
2781 __ j(not_equal, &done, Label::kNear);
2782 STATIC_ASSERT(EQUAL == 0);
2783 STATIC_ASSERT(kSmiTag == 0);
2784 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2785 __ bind(&done);
2786 __ ret(0);
2787
2788 __ bind(&miss);
2789 GenerateMiss(masm);
2790}
2791
2792
2793void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
2794 DCHECK(state() == CompareICState::UNIQUE_NAME);
2795 DCHECK(GetCondition() == equal);
2796
2797 // Registers containing left and right operands respectively.
2798 Register left = edx;
2799 Register right = eax;
2800 Register tmp1 = ecx;
2801 Register tmp2 = ebx;
2802
2803 // Check that both operands are heap objects.
2804 Label miss;
2805 __ mov(tmp1, left);
2806 STATIC_ASSERT(kSmiTag == 0);
2807 __ and_(tmp1, right);
2808 __ JumpIfSmi(tmp1, &miss, Label::kNear);
2809
2810 // Check that both operands are unique names. This leaves the instance
2811 // types loaded in tmp1 and tmp2.
2812 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
2813 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
2814 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
2815 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
2816
2817 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
2818 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
2819
2820 // Unique names are compared by identity.
2821 Label done;
2822 __ cmp(left, right);
2823 // Make sure eax is non-zero. At this point input operands are
2824 // guaranteed to be non-zero.
2825 DCHECK(right.is(eax));
2826 __ j(not_equal, &done, Label::kNear);
2827 STATIC_ASSERT(EQUAL == 0);
2828 STATIC_ASSERT(kSmiTag == 0);
2829 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2830 __ bind(&done);
2831 __ ret(0);
2832
2833 __ bind(&miss);
2834 GenerateMiss(masm);
2835}
2836
2837
2838void CompareICStub::GenerateStrings(MacroAssembler* masm) {
2839 DCHECK(state() == CompareICState::STRING);
2840 Label miss;
2841
2842 bool equality = Token::IsEqualityOp(op());
2843
2844 // Registers containing left and right operands respectively.
2845 Register left = edx;
2846 Register right = eax;
2847 Register tmp1 = ecx;
2848 Register tmp2 = ebx;
2849 Register tmp3 = edi;
2850
2851 // Check that both operands are heap objects.
2852 __ mov(tmp1, left);
2853 STATIC_ASSERT(kSmiTag == 0);
2854 __ and_(tmp1, right);
2855 __ JumpIfSmi(tmp1, &miss);
2856
2857 // Check that both operands are strings. This leaves the instance
2858 // types loaded in tmp1 and tmp2.
2859 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
2860 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
2861 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
2862 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
2863 __ mov(tmp3, tmp1);
2864 STATIC_ASSERT(kNotStringTag != 0);
2865 __ or_(tmp3, tmp2);
2866 __ test(tmp3, Immediate(kIsNotStringMask));
2867 __ j(not_zero, &miss);
2868
2869 // Fast check for identical strings.
2870 Label not_same;
2871 __ cmp(left, right);
2872 __ j(not_equal, &not_same, Label::kNear);
2873 STATIC_ASSERT(EQUAL == 0);
2874 STATIC_ASSERT(kSmiTag == 0);
2875 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2876 __ ret(0);
2877
2878 // Handle not identical strings.
2879 __ bind(&not_same);
2880
2881 // Check that both strings are internalized. If they are, we're done
2882 // because we already know they are not identical. But in the case of
2883 // non-equality compare, we still need to determine the order. We
2884 // also know they are both strings.
2885 if (equality) {
2886 Label do_compare;
2887 STATIC_ASSERT(kInternalizedTag == 0);
2888 __ or_(tmp1, tmp2);
2889 __ test(tmp1, Immediate(kIsNotInternalizedMask));
2890 __ j(not_zero, &do_compare, Label::kNear);
2891 // Make sure eax is non-zero. At this point input operands are
2892 // guaranteed to be non-zero.
2893 DCHECK(right.is(eax));
2894 __ ret(0);
2895 __ bind(&do_compare);
2896 }
2897
2898 // Check that both strings are sequential one-byte.
2899 Label runtime;
2900 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
2901
2902 // Compare flat one byte strings. Returns when done.
2903 if (equality) {
2904 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
2905 tmp2);
2906 } else {
2907 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
2908 tmp2, tmp3);
2909 }
2910
2911 // Handle more complex cases in runtime.
2912 __ bind(&runtime);
2913 __ pop(tmp1); // Return address.
2914 __ push(left);
2915 __ push(right);
2916 __ push(tmp1);
2917 if (equality) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002918 __ TailCallRuntime(Runtime::kStringEquals);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002919 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002920 __ TailCallRuntime(Runtime::kStringCompare);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002921 }
2922
2923 __ bind(&miss);
2924 GenerateMiss(masm);
2925}
2926
2927
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002928void CompareICStub::GenerateReceivers(MacroAssembler* masm) {
2929 DCHECK_EQ(CompareICState::RECEIVER, state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002930 Label miss;
2931 __ mov(ecx, edx);
2932 __ and_(ecx, eax);
2933 __ JumpIfSmi(ecx, &miss, Label::kNear);
2934
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002935 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
2936 __ CmpObjectType(eax, FIRST_JS_RECEIVER_TYPE, ecx);
2937 __ j(below, &miss, Label::kNear);
2938 __ CmpObjectType(edx, FIRST_JS_RECEIVER_TYPE, ecx);
2939 __ j(below, &miss, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002940
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002941 DCHECK_EQ(equal, GetCondition());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002942 __ sub(eax, edx);
2943 __ ret(0);
2944
2945 __ bind(&miss);
2946 GenerateMiss(masm);
2947}
2948
2949
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002950void CompareICStub::GenerateKnownReceivers(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002951 Label miss;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002952 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002953 __ mov(ecx, edx);
2954 __ and_(ecx, eax);
2955 __ JumpIfSmi(ecx, &miss, Label::kNear);
2956
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002957 __ GetWeakValue(edi, cell);
2958 __ cmp(edi, FieldOperand(eax, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002959 __ j(not_equal, &miss, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002960 __ cmp(edi, FieldOperand(edx, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002961 __ j(not_equal, &miss, Label::kNear);
2962
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002963 if (Token::IsEqualityOp(op())) {
2964 __ sub(eax, edx);
2965 __ ret(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002966 } else {
2967 __ PopReturnAddressTo(ecx);
2968 __ Push(edx);
2969 __ Push(eax);
2970 __ Push(Immediate(Smi::FromInt(NegativeComparisonResult(GetCondition()))));
2971 __ PushReturnAddressFrom(ecx);
2972 __ TailCallRuntime(Runtime::kCompare);
2973 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002974
2975 __ bind(&miss);
2976 GenerateMiss(masm);
2977}
2978
2979
2980void CompareICStub::GenerateMiss(MacroAssembler* masm) {
2981 {
2982 // Call the runtime system in a fresh internal frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002983 FrameScope scope(masm, StackFrame::INTERNAL);
2984 __ push(edx); // Preserve edx and eax.
2985 __ push(eax);
2986 __ push(edx); // And also use them as the arguments.
2987 __ push(eax);
2988 __ push(Immediate(Smi::FromInt(op())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002989 __ CallRuntime(Runtime::kCompareIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002990 // Compute the entry point of the rewritten stub.
2991 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
2992 __ pop(eax);
2993 __ pop(edx);
2994 }
2995
2996 // Do a tail call to the rewritten stub.
2997 __ jmp(edi);
2998}
2999
3000
3001// Helper function used to check that the dictionary doesn't contain
3002// the property. This function may return false negatives, so miss_label
3003// must always call a backup property check that is complete.
3004// This function is safe to call if the receiver has fast properties.
3005// Name must be a unique name and receiver must be a heap object.
3006void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3007 Label* miss,
3008 Label* done,
3009 Register properties,
3010 Handle<Name> name,
3011 Register r0) {
3012 DCHECK(name->IsUniqueName());
3013
3014 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3015 // not equal to the name and kProbes-th slot is not used (its name is the
3016 // undefined value), it guarantees the hash table doesn't contain the
3017 // property. It's true even if some slots represent deleted properties
3018 // (their names are the hole value).
3019 for (int i = 0; i < kInlinedProbes; i++) {
3020 // Compute the masked index: (hash + i + i * i) & mask.
3021 Register index = r0;
3022 // Capacity is smi 2^n.
3023 __ mov(index, FieldOperand(properties, kCapacityOffset));
3024 __ dec(index);
3025 __ and_(index,
3026 Immediate(Smi::FromInt(name->Hash() +
3027 NameDictionary::GetProbeOffset(i))));
3028
3029 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003030 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003031 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
3032 Register entity_name = r0;
3033 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003034 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003035 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
3036 kElementsStartOffset - kHeapObjectTag));
3037 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
3038 __ j(equal, done);
3039
3040 // Stop if found the property.
3041 __ cmp(entity_name, Handle<Name>(name));
3042 __ j(equal, miss);
3043
3044 Label good;
3045 // Check for the hole and skip.
3046 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
3047 __ j(equal, &good, Label::kNear);
3048
3049 // Check if the entry name is not a unique name.
3050 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3051 __ JumpIfNotUniqueNameInstanceType(
3052 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3053 __ bind(&good);
3054 }
3055
3056 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3057 NEGATIVE_LOOKUP);
3058 __ push(Immediate(Handle<Object>(name)));
3059 __ push(Immediate(name->Hash()));
3060 __ CallStub(&stub);
3061 __ test(r0, r0);
3062 __ j(not_zero, miss);
3063 __ jmp(done);
3064}
3065
3066
3067// Probe the name dictionary in the |elements| register. Jump to the
3068// |done| label if a property with the given name is found leaving the
3069// index into the dictionary in |r0|. Jump to the |miss| label
3070// otherwise.
3071void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3072 Label* miss,
3073 Label* done,
3074 Register elements,
3075 Register name,
3076 Register r0,
3077 Register r1) {
3078 DCHECK(!elements.is(r0));
3079 DCHECK(!elements.is(r1));
3080 DCHECK(!name.is(r0));
3081 DCHECK(!name.is(r1));
3082
3083 __ AssertName(name);
3084
3085 __ mov(r1, FieldOperand(elements, kCapacityOffset));
3086 __ shr(r1, kSmiTagSize); // convert smi to int
3087 __ dec(r1);
3088
3089 // Generate an unrolled loop that performs a few probes before
3090 // giving up. Measurements done on Gmail indicate that 2 probes
3091 // cover ~93% of loads from dictionaries.
3092 for (int i = 0; i < kInlinedProbes; i++) {
3093 // Compute the masked index: (hash + i + i * i) & mask.
3094 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3095 __ shr(r0, Name::kHashShift);
3096 if (i > 0) {
3097 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
3098 }
3099 __ and_(r0, r1);
3100
3101 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003102 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003103 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
3104
3105 // Check if the key is identical to the name.
3106 __ cmp(name, Operand(elements,
3107 r0,
3108 times_4,
3109 kElementsStartOffset - kHeapObjectTag));
3110 __ j(equal, done);
3111 }
3112
3113 NameDictionaryLookupStub stub(masm->isolate(), elements, r1, r0,
3114 POSITIVE_LOOKUP);
3115 __ push(name);
3116 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3117 __ shr(r0, Name::kHashShift);
3118 __ push(r0);
3119 __ CallStub(&stub);
3120
3121 __ test(r1, r1);
3122 __ j(zero, miss);
3123 __ jmp(done);
3124}
3125
3126
3127void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
3128 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3129 // we cannot call anything that could cause a GC from this stub.
3130 // Stack frame on entry:
3131 // esp[0 * kPointerSize]: return address.
3132 // esp[1 * kPointerSize]: key's hash.
3133 // esp[2 * kPointerSize]: key.
3134 // Registers:
3135 // dictionary_: NameDictionary to probe.
3136 // result_: used as scratch.
3137 // index_: will hold an index of entry if lookup is successful.
3138 // might alias with result_.
3139 // Returns:
3140 // result_ is zero if lookup failed, non zero otherwise.
3141
3142 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3143
3144 Register scratch = result();
3145
3146 __ mov(scratch, FieldOperand(dictionary(), kCapacityOffset));
3147 __ dec(scratch);
3148 __ SmiUntag(scratch);
3149 __ push(scratch);
3150
3151 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3152 // not equal to the name and kProbes-th slot is not used (its name is the
3153 // undefined value), it guarantees the hash table doesn't contain the
3154 // property. It's true even if some slots represent deleted properties
3155 // (their names are the null value).
3156 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3157 // Compute the masked index: (hash + i + i * i) & mask.
3158 __ mov(scratch, Operand(esp, 2 * kPointerSize));
3159 if (i > 0) {
3160 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
3161 }
3162 __ and_(scratch, Operand(esp, 0));
3163
3164 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003165 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003166 __ lea(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
3167
3168 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003169 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003170 __ mov(scratch, Operand(dictionary(), index(), times_pointer_size,
3171 kElementsStartOffset - kHeapObjectTag));
3172 __ cmp(scratch, isolate()->factory()->undefined_value());
3173 __ j(equal, &not_in_dictionary);
3174
3175 // Stop if found the property.
3176 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
3177 __ j(equal, &in_dictionary);
3178
3179 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3180 // If we hit a key that is not a unique name during negative
3181 // lookup we have to bailout as this key might be equal to the
3182 // key we are looking for.
3183
3184 // Check if the entry name is not a unique name.
3185 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
3186 __ JumpIfNotUniqueNameInstanceType(
3187 FieldOperand(scratch, Map::kInstanceTypeOffset),
3188 &maybe_in_dictionary);
3189 }
3190 }
3191
3192 __ bind(&maybe_in_dictionary);
3193 // If we are doing negative lookup then probing failure should be
3194 // treated as a lookup success. For positive lookup probing failure
3195 // should be treated as lookup failure.
3196 if (mode() == POSITIVE_LOOKUP) {
3197 __ mov(result(), Immediate(0));
3198 __ Drop(1);
3199 __ ret(2 * kPointerSize);
3200 }
3201
3202 __ bind(&in_dictionary);
3203 __ mov(result(), Immediate(1));
3204 __ Drop(1);
3205 __ ret(2 * kPointerSize);
3206
3207 __ bind(&not_in_dictionary);
3208 __ mov(result(), Immediate(0));
3209 __ Drop(1);
3210 __ ret(2 * kPointerSize);
3211}
3212
3213
3214void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3215 Isolate* isolate) {
3216 StoreBufferOverflowStub stub(isolate, kDontSaveFPRegs);
3217 stub.GetCode();
3218 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3219 stub2.GetCode();
3220}
3221
3222
3223// Takes the input in 3 registers: address_ value_ and object_. A pointer to
3224// the value has just been written into the object, now this stub makes sure
3225// we keep the GC informed. The word in the object where the value has been
3226// written is in the address register.
3227void RecordWriteStub::Generate(MacroAssembler* masm) {
3228 Label skip_to_incremental_noncompacting;
3229 Label skip_to_incremental_compacting;
3230
3231 // The first two instructions are generated with labels so as to get the
3232 // offset fixed up correctly by the bind(Label*) call. We patch it back and
3233 // forth between a compare instructions (a nop in this position) and the
3234 // real branch when we start and stop incremental heap marking.
3235 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
3236 __ jmp(&skip_to_incremental_compacting, Label::kFar);
3237
3238 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3239 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3240 MacroAssembler::kReturnAtEnd);
3241 } else {
3242 __ ret(0);
3243 }
3244
3245 __ bind(&skip_to_incremental_noncompacting);
3246 GenerateIncremental(masm, INCREMENTAL);
3247
3248 __ bind(&skip_to_incremental_compacting);
3249 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3250
3251 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3252 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
3253 masm->set_byte_at(0, kTwoByteNopInstruction);
3254 masm->set_byte_at(2, kFiveByteNopInstruction);
3255}
3256
3257
3258void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3259 regs_.Save(masm);
3260
3261 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3262 Label dont_need_remembered_set;
3263
3264 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3265 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
3266 regs_.scratch0(),
3267 &dont_need_remembered_set);
3268
Ben Murdoch097c5b22016-05-18 11:27:45 +01003269 __ JumpIfInNewSpace(regs_.object(), regs_.scratch0(),
3270 &dont_need_remembered_set);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003271
3272 // First notify the incremental marker if necessary, then update the
3273 // remembered set.
3274 CheckNeedsToInformIncrementalMarker(
3275 masm,
3276 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
3277 mode);
3278 InformIncrementalMarker(masm);
3279 regs_.Restore(masm);
3280 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3281 MacroAssembler::kReturnAtEnd);
3282
3283 __ bind(&dont_need_remembered_set);
3284 }
3285
3286 CheckNeedsToInformIncrementalMarker(
3287 masm,
3288 kReturnOnNoNeedToInformIncrementalMarker,
3289 mode);
3290 InformIncrementalMarker(masm);
3291 regs_.Restore(masm);
3292 __ ret(0);
3293}
3294
3295
3296void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3297 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
3298 int argument_count = 3;
3299 __ PrepareCallCFunction(argument_count, regs_.scratch0());
3300 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
3301 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
3302 __ mov(Operand(esp, 2 * kPointerSize),
3303 Immediate(ExternalReference::isolate_address(isolate())));
3304
3305 AllowExternalCallThatCantCauseGC scope(masm);
3306 __ CallCFunction(
3307 ExternalReference::incremental_marking_record_write_function(isolate()),
3308 argument_count);
3309
3310 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
3311}
3312
3313
3314void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3315 MacroAssembler* masm,
3316 OnNoNeedToInformIncrementalMarker on_no_need,
3317 Mode mode) {
3318 Label object_is_black, need_incremental, need_incremental_pop_object;
3319
3320 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
3321 __ and_(regs_.scratch0(), regs_.object());
3322 __ mov(regs_.scratch1(),
3323 Operand(regs_.scratch0(),
3324 MemoryChunk::kWriteBarrierCounterOffset));
3325 __ sub(regs_.scratch1(), Immediate(1));
3326 __ mov(Operand(regs_.scratch0(),
3327 MemoryChunk::kWriteBarrierCounterOffset),
3328 regs_.scratch1());
3329 __ j(negative, &need_incremental);
3330
3331 // Let's look at the color of the object: If it is not black we don't have
3332 // to inform the incremental marker.
3333 __ JumpIfBlack(regs_.object(),
3334 regs_.scratch0(),
3335 regs_.scratch1(),
3336 &object_is_black,
3337 Label::kNear);
3338
3339 regs_.Restore(masm);
3340 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
3341 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3342 MacroAssembler::kReturnAtEnd);
3343 } else {
3344 __ ret(0);
3345 }
3346
3347 __ bind(&object_is_black);
3348
3349 // Get the value from the slot.
3350 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3351
3352 if (mode == INCREMENTAL_COMPACTION) {
3353 Label ensure_not_white;
3354
3355 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3356 regs_.scratch1(), // Scratch.
3357 MemoryChunk::kEvacuationCandidateMask,
3358 zero,
3359 &ensure_not_white,
3360 Label::kNear);
3361
3362 __ CheckPageFlag(regs_.object(),
3363 regs_.scratch1(), // Scratch.
3364 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3365 not_zero,
3366 &ensure_not_white,
3367 Label::kNear);
3368
3369 __ jmp(&need_incremental);
3370
3371 __ bind(&ensure_not_white);
3372 }
3373
3374 // We need an extra register for this, so we push the object register
3375 // temporarily.
3376 __ push(regs_.object());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003377 __ JumpIfWhite(regs_.scratch0(), // The value.
3378 regs_.scratch1(), // Scratch.
3379 regs_.object(), // Scratch.
3380 &need_incremental_pop_object, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003381 __ pop(regs_.object());
3382
3383 regs_.Restore(masm);
3384 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
3385 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3386 MacroAssembler::kReturnAtEnd);
3387 } else {
3388 __ ret(0);
3389 }
3390
3391 __ bind(&need_incremental_pop_object);
3392 __ pop(regs_.object());
3393
3394 __ bind(&need_incremental);
3395
3396 // Fall through when we need to inform the incremental marker.
3397}
3398
3399
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003400void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
3401 CEntryStub ces(isolate(), 1, kSaveFPRegs);
3402 __ call(ces.GetCode(), RelocInfo::CODE_TARGET);
3403 int parameter_count_offset =
3404 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
3405 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
3406 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
3407 __ pop(ecx);
3408 int additional_offset =
3409 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
3410 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
3411 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
3412}
3413
3414
3415void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003416 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3417 LoadICStub stub(isolate(), state());
3418 stub.GenerateForTrampoline(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003419}
3420
3421
3422void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003423 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3424 KeyedLoadICStub stub(isolate(), state());
3425 stub.GenerateForTrampoline(masm);
3426}
3427
3428
3429static void HandleArrayCases(MacroAssembler* masm, Register receiver,
3430 Register key, Register vector, Register slot,
3431 Register feedback, bool is_polymorphic,
3432 Label* miss) {
3433 // feedback initially contains the feedback array
3434 Label next, next_loop, prepare_next;
3435 Label load_smi_map, compare_map;
3436 Label start_polymorphic;
3437
3438 __ push(receiver);
3439 __ push(vector);
3440
3441 Register receiver_map = receiver;
3442 Register cached_map = vector;
3443
3444 // Receiver might not be a heap object.
3445 __ JumpIfSmi(receiver, &load_smi_map);
3446 __ mov(receiver_map, FieldOperand(receiver, 0));
3447 __ bind(&compare_map);
3448 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3449
3450 // A named keyed load might have a 2 element array, all other cases can count
3451 // on an array with at least 2 {map, handler} pairs, so they can go right
3452 // into polymorphic array handling.
3453 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3454 __ j(not_equal, is_polymorphic ? &start_polymorphic : &next);
3455
3456 // found, now call handler.
3457 Register handler = feedback;
3458 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3459 __ pop(vector);
3460 __ pop(receiver);
3461 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3462 __ jmp(handler);
3463
3464 if (!is_polymorphic) {
3465 __ bind(&next);
3466 __ cmp(FieldOperand(feedback, FixedArray::kLengthOffset),
3467 Immediate(Smi::FromInt(2)));
3468 __ j(not_equal, &start_polymorphic);
3469 __ pop(vector);
3470 __ pop(receiver);
3471 __ jmp(miss);
3472 }
3473
3474 // Polymorphic, we have to loop from 2 to N
3475 __ bind(&start_polymorphic);
3476 __ push(key);
3477 Register counter = key;
3478 __ mov(counter, Immediate(Smi::FromInt(2)));
3479 __ bind(&next_loop);
3480 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3481 FixedArray::kHeaderSize));
3482 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3483 __ j(not_equal, &prepare_next);
3484 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
3485 FixedArray::kHeaderSize + kPointerSize));
3486 __ pop(key);
3487 __ pop(vector);
3488 __ pop(receiver);
3489 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3490 __ jmp(handler);
3491
3492 __ bind(&prepare_next);
3493 __ add(counter, Immediate(Smi::FromInt(2)));
3494 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3495 __ j(less, &next_loop);
3496
3497 // We exhausted our array of map handler pairs.
3498 __ pop(key);
3499 __ pop(vector);
3500 __ pop(receiver);
3501 __ jmp(miss);
3502
3503 __ bind(&load_smi_map);
3504 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3505 __ jmp(&compare_map);
3506}
3507
3508
3509static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
3510 Register key, Register vector, Register slot,
3511 Register weak_cell, Label* miss) {
3512 // feedback initially contains the feedback array
3513 Label compare_smi_map;
3514
3515 // Move the weak map into the weak_cell register.
3516 Register ic_map = weak_cell;
3517 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
3518
3519 // Receiver might not be a heap object.
3520 __ JumpIfSmi(receiver, &compare_smi_map);
3521 __ cmp(ic_map, FieldOperand(receiver, 0));
3522 __ j(not_equal, miss);
3523 Register handler = weak_cell;
3524 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
3525 FixedArray::kHeaderSize + kPointerSize));
3526 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3527 __ jmp(handler);
3528
3529 // In microbenchmarks, it made sense to unroll this code so that the call to
3530 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
3531 __ bind(&compare_smi_map);
3532 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
3533 __ j(not_equal, miss);
3534 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
3535 FixedArray::kHeaderSize + kPointerSize));
3536 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3537 __ jmp(handler);
3538}
3539
3540
3541void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
3542
3543
3544void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3545 GenerateImpl(masm, true);
3546}
3547
3548
3549void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3550 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
3551 Register name = LoadWithVectorDescriptor::NameRegister(); // ecx
3552 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
3553 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
3554 Register scratch = edi;
3555 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3556 FixedArray::kHeaderSize));
3557
3558 // Is it a weak cell?
3559 Label try_array;
3560 Label not_array, smi_key, key_okay, miss;
3561 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3562 __ j(not_equal, &try_array);
3563 HandleMonomorphicCase(masm, receiver, name, vector, slot, scratch, &miss);
3564
3565 // Is it a fixed array?
3566 __ bind(&try_array);
3567 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3568 __ j(not_equal, &not_array);
3569 HandleArrayCases(masm, receiver, name, vector, slot, scratch, true, &miss);
3570
3571 __ bind(&not_array);
3572 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3573 __ j(not_equal, &miss);
3574 __ push(slot);
3575 __ push(vector);
3576 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
3577 Code::ComputeHandlerFlags(Code::LOAD_IC));
3578 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
3579 receiver, name, vector, scratch);
3580 __ pop(vector);
3581 __ pop(slot);
3582
3583 __ bind(&miss);
3584 LoadIC::GenerateMiss(masm);
3585}
3586
3587
3588void KeyedLoadICStub::Generate(MacroAssembler* masm) {
3589 GenerateImpl(masm, false);
3590}
3591
3592
3593void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3594 GenerateImpl(masm, true);
3595}
3596
3597
3598void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3599 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
3600 Register key = LoadWithVectorDescriptor::NameRegister(); // ecx
3601 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
3602 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
3603 Register feedback = edi;
3604 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
3605 FixedArray::kHeaderSize));
3606 // Is it a weak cell?
3607 Label try_array;
3608 Label not_array, smi_key, key_okay, miss;
3609 __ CompareRoot(FieldOperand(feedback, 0), Heap::kWeakCellMapRootIndex);
3610 __ j(not_equal, &try_array);
3611 HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, &miss);
3612
3613 __ bind(&try_array);
3614 // Is it a fixed array?
3615 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
3616 __ j(not_equal, &not_array);
3617
3618 // We have a polymorphic element handler.
3619 Label polymorphic, try_poly_name;
3620 __ bind(&polymorphic);
3621 HandleArrayCases(masm, receiver, key, vector, slot, feedback, true, &miss);
3622
3623 __ bind(&not_array);
3624 // Is it generic?
3625 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
3626 __ j(not_equal, &try_poly_name);
3627 Handle<Code> megamorphic_stub =
3628 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3629 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
3630
3631 __ bind(&try_poly_name);
3632 // We might have a name in feedback, and a fixed array in the next slot.
3633 __ cmp(key, feedback);
3634 __ j(not_equal, &miss);
3635 // If the name comparison succeeded, we know we have a fixed array with
3636 // at least one map/handler pair.
3637 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
3638 FixedArray::kHeaderSize + kPointerSize));
3639 HandleArrayCases(masm, receiver, key, vector, slot, feedback, false, &miss);
3640
3641 __ bind(&miss);
3642 KeyedLoadIC::GenerateMiss(masm);
3643}
3644
3645
3646void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3647 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3648 VectorStoreICStub stub(isolate(), state());
3649 stub.GenerateForTrampoline(masm);
3650}
3651
3652
3653void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3654 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3655 VectorKeyedStoreICStub stub(isolate(), state());
3656 stub.GenerateForTrampoline(masm);
3657}
3658
3659
3660void VectorStoreICStub::Generate(MacroAssembler* masm) {
3661 GenerateImpl(masm, false);
3662}
3663
3664
3665void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3666 GenerateImpl(masm, true);
3667}
3668
3669
3670// value is on the stack already.
3671static void HandlePolymorphicStoreCase(MacroAssembler* masm, Register receiver,
3672 Register key, Register vector,
3673 Register slot, Register feedback,
3674 bool is_polymorphic, Label* miss) {
3675 // feedback initially contains the feedback array
3676 Label next, next_loop, prepare_next;
3677 Label load_smi_map, compare_map;
3678 Label start_polymorphic;
3679 Label pop_and_miss;
3680 ExternalReference virtual_register =
3681 ExternalReference::virtual_handler_register(masm->isolate());
3682
3683 __ push(receiver);
3684 __ push(vector);
3685
3686 Register receiver_map = receiver;
3687 Register cached_map = vector;
3688
3689 // Receiver might not be a heap object.
3690 __ JumpIfSmi(receiver, &load_smi_map);
3691 __ mov(receiver_map, FieldOperand(receiver, 0));
3692 __ bind(&compare_map);
3693 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3694
3695 // A named keyed store might have a 2 element array, all other cases can count
3696 // on an array with at least 2 {map, handler} pairs, so they can go right
3697 // into polymorphic array handling.
3698 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3699 __ j(not_equal, &start_polymorphic);
3700
3701 // found, now call handler.
3702 Register handler = feedback;
3703 DCHECK(handler.is(VectorStoreICDescriptor::ValueRegister()));
3704 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3705 __ pop(vector);
3706 __ pop(receiver);
3707 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3708 __ mov(Operand::StaticVariable(virtual_register), handler);
3709 __ pop(handler); // Pop "value".
3710 __ jmp(Operand::StaticVariable(virtual_register));
3711
3712 // Polymorphic, we have to loop from 2 to N
3713 __ bind(&start_polymorphic);
3714 __ push(key);
3715 Register counter = key;
3716 __ mov(counter, Immediate(Smi::FromInt(2)));
3717
3718 if (!is_polymorphic) {
3719 // If is_polymorphic is false, we may only have a two element array.
3720 // Check against length now in that case.
3721 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3722 __ j(greater_equal, &pop_and_miss);
3723 }
3724
3725 __ bind(&next_loop);
3726 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3727 FixedArray::kHeaderSize));
3728 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3729 __ j(not_equal, &prepare_next);
3730 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
3731 FixedArray::kHeaderSize + kPointerSize));
3732 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3733 __ pop(key);
3734 __ pop(vector);
3735 __ pop(receiver);
3736 __ mov(Operand::StaticVariable(virtual_register), handler);
3737 __ pop(handler); // Pop "value".
3738 __ jmp(Operand::StaticVariable(virtual_register));
3739
3740 __ bind(&prepare_next);
3741 __ add(counter, Immediate(Smi::FromInt(2)));
3742 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3743 __ j(less, &next_loop);
3744
3745 // We exhausted our array of map handler pairs.
3746 __ bind(&pop_and_miss);
3747 __ pop(key);
3748 __ pop(vector);
3749 __ pop(receiver);
3750 __ jmp(miss);
3751
3752 __ bind(&load_smi_map);
3753 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3754 __ jmp(&compare_map);
3755}
3756
3757
3758static void HandleMonomorphicStoreCase(MacroAssembler* masm, Register receiver,
3759 Register key, Register vector,
3760 Register slot, Register weak_cell,
3761 Label* miss) {
3762 // The store ic value is on the stack.
3763 DCHECK(weak_cell.is(VectorStoreICDescriptor::ValueRegister()));
3764 ExternalReference virtual_register =
3765 ExternalReference::virtual_handler_register(masm->isolate());
3766
3767 // feedback initially contains the feedback array
3768 Label compare_smi_map;
3769
3770 // Move the weak map into the weak_cell register.
3771 Register ic_map = weak_cell;
3772 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
3773
3774 // Receiver might not be a heap object.
3775 __ JumpIfSmi(receiver, &compare_smi_map);
3776 __ cmp(ic_map, FieldOperand(receiver, 0));
3777 __ j(not_equal, miss);
3778 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
3779 FixedArray::kHeaderSize + kPointerSize));
3780 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
3781 // Put the store ic value back in it's register.
3782 __ mov(Operand::StaticVariable(virtual_register), weak_cell);
3783 __ pop(weak_cell); // Pop "value".
3784 // jump to the handler.
3785 __ jmp(Operand::StaticVariable(virtual_register));
3786
3787 // In microbenchmarks, it made sense to unroll this code so that the call to
3788 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
3789 __ bind(&compare_smi_map);
3790 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
3791 __ j(not_equal, miss);
3792 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
3793 FixedArray::kHeaderSize + kPointerSize));
3794 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
3795 __ mov(Operand::StaticVariable(virtual_register), weak_cell);
3796 __ pop(weak_cell); // Pop "value".
3797 // jump to the handler.
3798 __ jmp(Operand::StaticVariable(virtual_register));
3799}
3800
3801
3802void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3803 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
3804 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
3805 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
3806 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
3807 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
3808 Label miss;
3809
3810 __ push(value);
3811
3812 Register scratch = value;
3813 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3814 FixedArray::kHeaderSize));
3815
3816 // Is it a weak cell?
3817 Label try_array;
3818 Label not_array, smi_key, key_okay;
3819 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3820 __ j(not_equal, &try_array);
3821 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
3822
3823 // Is it a fixed array?
3824 __ bind(&try_array);
3825 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3826 __ j(not_equal, &not_array);
3827 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, true,
3828 &miss);
3829
3830 __ bind(&not_array);
3831 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3832 __ j(not_equal, &miss);
3833
3834 __ pop(value);
3835 __ push(slot);
3836 __ push(vector);
3837 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
3838 Code::ComputeHandlerFlags(Code::STORE_IC));
3839 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::STORE_IC, code_flags,
3840 receiver, key, slot, no_reg);
3841 __ pop(vector);
3842 __ pop(slot);
3843 Label no_pop_miss;
3844 __ jmp(&no_pop_miss);
3845
3846 __ bind(&miss);
3847 __ pop(value);
3848 __ bind(&no_pop_miss);
3849 StoreIC::GenerateMiss(masm);
3850}
3851
3852
3853void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
3854 GenerateImpl(masm, false);
3855}
3856
3857
3858void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3859 GenerateImpl(masm, true);
3860}
3861
3862
3863static void HandlePolymorphicKeyedStoreCase(MacroAssembler* masm,
3864 Register receiver, Register key,
3865 Register vector, Register slot,
3866 Register feedback, Label* miss) {
3867 // feedback initially contains the feedback array
3868 Label next, next_loop, prepare_next;
3869 Label load_smi_map, compare_map;
3870 Label transition_call;
3871 Label pop_and_miss;
3872 ExternalReference virtual_register =
3873 ExternalReference::virtual_handler_register(masm->isolate());
3874 ExternalReference virtual_slot =
3875 ExternalReference::virtual_slot_register(masm->isolate());
3876
3877 __ push(receiver);
3878 __ push(vector);
3879
3880 Register receiver_map = receiver;
3881 Register cached_map = vector;
3882 Register value = StoreDescriptor::ValueRegister();
3883
3884 // Receiver might not be a heap object.
3885 __ JumpIfSmi(receiver, &load_smi_map);
3886 __ mov(receiver_map, FieldOperand(receiver, 0));
3887 __ bind(&compare_map);
3888
3889 // Polymorphic, we have to loop from 0 to N - 1
3890 __ push(key);
3891 // Current stack layout:
3892 // - esp[0] -- key
3893 // - esp[4] -- vector
3894 // - esp[8] -- receiver
3895 // - esp[12] -- value
3896 // - esp[16] -- return address
3897 //
3898 // Required stack layout for handler call:
3899 // - esp[0] -- return address
3900 // - receiver, key, value, vector, slot in registers.
3901 // - handler in virtual register.
3902 Register counter = key;
3903 __ mov(counter, Immediate(Smi::FromInt(0)));
3904 __ bind(&next_loop);
3905 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3906 FixedArray::kHeaderSize));
3907 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3908 __ j(not_equal, &prepare_next);
3909 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3910 FixedArray::kHeaderSize + kPointerSize));
3911 __ CompareRoot(cached_map, Heap::kUndefinedValueRootIndex);
3912 __ j(not_equal, &transition_call);
3913 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
3914 FixedArray::kHeaderSize + 2 * kPointerSize));
3915 __ pop(key);
3916 __ pop(vector);
3917 __ pop(receiver);
3918 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
3919 __ mov(Operand::StaticVariable(virtual_register), feedback);
3920 __ pop(value);
3921 __ jmp(Operand::StaticVariable(virtual_register));
3922
3923 __ bind(&transition_call);
3924 // Current stack layout:
3925 // - esp[0] -- key
3926 // - esp[4] -- vector
3927 // - esp[8] -- receiver
3928 // - esp[12] -- value
3929 // - esp[16] -- return address
3930 //
3931 // Required stack layout for handler call:
3932 // - esp[0] -- return address
3933 // - receiver, key, value, map, vector in registers.
3934 // - handler and slot in virtual registers.
3935 __ mov(Operand::StaticVariable(virtual_slot), slot);
3936 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
3937 FixedArray::kHeaderSize + 2 * kPointerSize));
3938 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
3939 __ mov(Operand::StaticVariable(virtual_register), feedback);
3940
3941 __ mov(cached_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3942 // The weak cell may have been cleared.
3943 __ JumpIfSmi(cached_map, &pop_and_miss);
3944 DCHECK(!cached_map.is(VectorStoreTransitionDescriptor::MapRegister()));
3945 __ mov(VectorStoreTransitionDescriptor::MapRegister(), cached_map);
3946
3947 // Pop key into place.
3948 __ pop(key);
3949 __ pop(vector);
3950 __ pop(receiver);
3951 __ pop(value);
3952 __ jmp(Operand::StaticVariable(virtual_register));
3953
3954 __ bind(&prepare_next);
3955 __ add(counter, Immediate(Smi::FromInt(3)));
3956 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3957 __ j(less, &next_loop);
3958
3959 // We exhausted our array of map handler pairs.
3960 __ bind(&pop_and_miss);
3961 __ pop(key);
3962 __ pop(vector);
3963 __ pop(receiver);
3964 __ jmp(miss);
3965
3966 __ bind(&load_smi_map);
3967 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3968 __ jmp(&compare_map);
3969}
3970
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003971void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3972 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
3973 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
3974 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
3975 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
3976 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
3977 Label miss;
3978
3979 __ push(value);
3980
3981 Register scratch = value;
3982 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3983 FixedArray::kHeaderSize));
3984
3985 // Is it a weak cell?
3986 Label try_array;
3987 Label not_array, smi_key, key_okay;
3988 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3989 __ j(not_equal, &try_array);
3990 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
3991
3992 // Is it a fixed array?
3993 __ bind(&try_array);
3994 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3995 __ j(not_equal, &not_array);
3996 HandlePolymorphicKeyedStoreCase(masm, receiver, key, vector, slot, scratch,
3997 &miss);
3998
3999 __ bind(&not_array);
4000 Label try_poly_name;
4001 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
4002 __ j(not_equal, &try_poly_name);
4003
4004 __ pop(value);
4005
4006 Handle<Code> megamorphic_stub =
4007 KeyedStoreIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4008 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
4009
4010 __ bind(&try_poly_name);
4011 // We might have a name in feedback, and a fixed array in the next slot.
4012 __ cmp(key, scratch);
4013 __ j(not_equal, &miss);
4014 // If the name comparison succeeded, we know we have a fixed array with
4015 // at least one map/handler pair.
4016 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
4017 FixedArray::kHeaderSize + kPointerSize));
4018 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, false,
4019 &miss);
4020
4021 __ bind(&miss);
4022 __ pop(value);
4023 KeyedStoreIC::GenerateMiss(masm);
4024}
4025
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004026void CallICTrampolineStub::Generate(MacroAssembler* masm) {
4027 __ EmitLoadTypeFeedbackVector(ebx);
4028 CallICStub stub(isolate(), state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004029 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
4030}
4031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004032void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4033 if (masm->isolate()->function_entry_hook() != NULL) {
4034 ProfileEntryHookStub stub(masm->isolate());
4035 masm->CallStub(&stub);
4036 }
4037}
4038
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004039void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4040 // Save volatile registers.
4041 const int kNumSavedRegisters = 3;
4042 __ push(eax);
4043 __ push(ecx);
4044 __ push(edx);
4045
4046 // Calculate and push the original stack pointer.
4047 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4048 __ push(eax);
4049
4050 // Retrieve our return address and use it to calculate the calling
4051 // function's address.
4052 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4053 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
4054 __ push(eax);
4055
4056 // Call the entry hook.
4057 DCHECK(isolate()->function_entry_hook() != NULL);
4058 __ call(FUNCTION_ADDR(isolate()->function_entry_hook()),
4059 RelocInfo::RUNTIME_ENTRY);
4060 __ add(esp, Immediate(2 * kPointerSize));
4061
4062 // Restore ecx.
4063 __ pop(edx);
4064 __ pop(ecx);
4065 __ pop(eax);
4066
4067 __ ret(0);
4068}
4069
Ben Murdoch097c5b22016-05-18 11:27:45 +01004070template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004071static void CreateArrayDispatch(MacroAssembler* masm,
4072 AllocationSiteOverrideMode mode) {
4073 if (mode == DISABLE_ALLOCATION_SITES) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004074 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004075 __ TailCallStub(&stub);
4076 } else if (mode == DONT_OVERRIDE) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004077 int last_index =
4078 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004079 for (int i = 0; i <= last_index; ++i) {
4080 Label next;
4081 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4082 __ cmp(edx, kind);
4083 __ j(not_equal, &next);
4084 T stub(masm->isolate(), kind);
4085 __ TailCallStub(&stub);
4086 __ bind(&next);
4087 }
4088
4089 // If we reached this point there is a problem.
4090 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4091 } else {
4092 UNREACHABLE();
4093 }
4094}
4095
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004096static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4097 AllocationSiteOverrideMode mode) {
4098 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4099 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
4100 // eax - number of arguments
4101 // edi - constructor?
4102 // esp[0] - return address
4103 // esp[4] - last argument
4104 Label normal_sequence;
4105 if (mode == DONT_OVERRIDE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004106 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4107 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4108 STATIC_ASSERT(FAST_ELEMENTS == 2);
4109 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4110 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4111 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004112
4113 // is the low bit set? If so, we are holey and that is good.
4114 __ test_b(edx, 1);
4115 __ j(not_zero, &normal_sequence);
4116 }
4117
4118 // look at the first argument
4119 __ mov(ecx, Operand(esp, kPointerSize));
4120 __ test(ecx, ecx);
4121 __ j(zero, &normal_sequence);
4122
4123 if (mode == DISABLE_ALLOCATION_SITES) {
4124 ElementsKind initial = GetInitialFastElementsKind();
4125 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4126
Ben Murdoch097c5b22016-05-18 11:27:45 +01004127 ArraySingleArgumentConstructorStub stub_holey(
4128 masm->isolate(), holey_initial, DISABLE_ALLOCATION_SITES);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004129 __ TailCallStub(&stub_holey);
4130
4131 __ bind(&normal_sequence);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004132 ArraySingleArgumentConstructorStub stub(masm->isolate(), initial,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004133 DISABLE_ALLOCATION_SITES);
4134 __ TailCallStub(&stub);
4135 } else if (mode == DONT_OVERRIDE) {
4136 // We are going to create a holey array, but our kind is non-holey.
4137 // Fix kind and retry.
4138 __ inc(edx);
4139
4140 if (FLAG_debug_code) {
4141 Handle<Map> allocation_site_map =
4142 masm->isolate()->factory()->allocation_site_map();
4143 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
4144 __ Assert(equal, kExpectedAllocationSite);
4145 }
4146
4147 // Save the resulting elements kind in type info. We can't just store r3
4148 // in the AllocationSite::transition_info field because elements kind is
4149 // restricted to a portion of the field...upper bits need to be left alone.
4150 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4151 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
4152 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
4153
4154 __ bind(&normal_sequence);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004155 int last_index =
4156 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004157 for (int i = 0; i <= last_index; ++i) {
4158 Label next;
4159 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4160 __ cmp(edx, kind);
4161 __ j(not_equal, &next);
4162 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4163 __ TailCallStub(&stub);
4164 __ bind(&next);
4165 }
4166
4167 // If we reached this point there is a problem.
4168 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4169 } else {
4170 UNREACHABLE();
4171 }
4172}
4173
Ben Murdoch097c5b22016-05-18 11:27:45 +01004174template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004175static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004176 int to_index =
4177 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004178 for (int i = 0; i <= to_index; ++i) {
4179 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4180 T stub(isolate, kind);
4181 stub.GetCode();
4182 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4183 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4184 stub1.GetCode();
4185 }
4186 }
4187}
4188
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004189void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4190 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4191 isolate);
4192 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4193 isolate);
4194 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4195 isolate);
4196}
4197
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004198void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4199 Isolate* isolate) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004200 ElementsKind kinds[2] = {FAST_ELEMENTS, FAST_HOLEY_ELEMENTS};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004201 for (int i = 0; i < 2; i++) {
4202 // For internal arrays we only need a few things
4203 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4204 stubh1.GetCode();
4205 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4206 stubh2.GetCode();
4207 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4208 stubh3.GetCode();
4209 }
4210}
4211
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004212void ArrayConstructorStub::GenerateDispatchToArrayStub(
Ben Murdoch097c5b22016-05-18 11:27:45 +01004213 MacroAssembler* masm, AllocationSiteOverrideMode mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004214 if (argument_count() == ANY) {
4215 Label not_zero_case, not_one_case;
4216 __ test(eax, eax);
4217 __ j(not_zero, &not_zero_case);
4218 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4219
4220 __ bind(&not_zero_case);
4221 __ cmp(eax, 1);
4222 __ j(greater, &not_one_case);
4223 CreateArrayDispatchOneArgument(masm, mode);
4224
4225 __ bind(&not_one_case);
4226 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4227 } else if (argument_count() == NONE) {
4228 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4229 } else if (argument_count() == ONE) {
4230 CreateArrayDispatchOneArgument(masm, mode);
4231 } else if (argument_count() == MORE_THAN_ONE) {
4232 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4233 } else {
4234 UNREACHABLE();
4235 }
4236}
4237
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004238void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4239 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004240 // -- eax : argc (only if argument_count() is ANY or MORE_THAN_ONE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004241 // -- ebx : AllocationSite or undefined
4242 // -- edi : constructor
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004243 // -- edx : Original constructor
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004244 // -- esp[0] : return address
4245 // -- esp[4] : last argument
4246 // -----------------------------------
4247 if (FLAG_debug_code) {
4248 // The array construct code is only set for the global and natives
4249 // builtin Array functions which always have maps.
4250
4251 // Initial map for the builtin Array function should be a map.
4252 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4253 // Will both indicate a NULL and a Smi.
4254 __ test(ecx, Immediate(kSmiTagMask));
4255 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4256 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4257 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4258
4259 // We should either have undefined in ebx or a valid AllocationSite
4260 __ AssertUndefinedOrAllocationSite(ebx);
4261 }
4262
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004263 Label subclassing;
4264
4265 // Enter the context of the Array function.
4266 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
4267
4268 __ cmp(edx, edi);
4269 __ j(not_equal, &subclassing);
4270
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004271 Label no_info;
4272 // If the feedback vector is the undefined value call an array constructor
4273 // that doesn't use AllocationSites.
4274 __ cmp(ebx, isolate()->factory()->undefined_value());
4275 __ j(equal, &no_info);
4276
4277 // Only look at the lower 16 bits of the transition info.
4278 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
4279 __ SmiUntag(edx);
4280 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4281 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
4282 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4283
4284 __ bind(&no_info);
4285 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004286
4287 // Subclassing.
4288 __ bind(&subclassing);
4289 switch (argument_count()) {
4290 case ANY:
4291 case MORE_THAN_ONE:
4292 __ mov(Operand(esp, eax, times_pointer_size, kPointerSize), edi);
4293 __ add(eax, Immediate(3));
4294 break;
4295 case NONE:
4296 __ mov(Operand(esp, 1 * kPointerSize), edi);
4297 __ mov(eax, Immediate(3));
4298 break;
4299 case ONE:
4300 __ mov(Operand(esp, 2 * kPointerSize), edi);
4301 __ mov(eax, Immediate(4));
4302 break;
4303 }
4304 __ PopReturnAddressTo(ecx);
4305 __ Push(edx);
4306 __ Push(ebx);
4307 __ PushReturnAddressFrom(ecx);
4308 __ JumpToExternalReference(ExternalReference(Runtime::kNewArray, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004309}
4310
Ben Murdoch097c5b22016-05-18 11:27:45 +01004311void InternalArrayConstructorStub::GenerateCase(MacroAssembler* masm,
4312 ElementsKind kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004313 Label not_zero_case, not_one_case;
4314 Label normal_sequence;
4315
4316 __ test(eax, eax);
4317 __ j(not_zero, &not_zero_case);
4318 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4319 __ TailCallStub(&stub0);
4320
4321 __ bind(&not_zero_case);
4322 __ cmp(eax, 1);
4323 __ j(greater, &not_one_case);
4324
4325 if (IsFastPackedElementsKind(kind)) {
4326 // We might need to create a holey array
4327 // look at the first argument
4328 __ mov(ecx, Operand(esp, kPointerSize));
4329 __ test(ecx, ecx);
4330 __ j(zero, &normal_sequence);
4331
Ben Murdoch097c5b22016-05-18 11:27:45 +01004332 InternalArraySingleArgumentConstructorStub stub1_holey(
4333 isolate(), GetHoleyElementsKind(kind));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004334 __ TailCallStub(&stub1_holey);
4335 }
4336
4337 __ bind(&normal_sequence);
4338 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4339 __ TailCallStub(&stub1);
4340
4341 __ bind(&not_one_case);
4342 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4343 __ TailCallStub(&stubN);
4344}
4345
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004346void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4347 // ----------- S t a t e -------------
4348 // -- eax : argc
4349 // -- edi : constructor
4350 // -- esp[0] : return address
4351 // -- esp[4] : last argument
4352 // -----------------------------------
4353
4354 if (FLAG_debug_code) {
4355 // The array construct code is only set for the global and natives
4356 // builtin Array functions which always have maps.
4357
4358 // Initial map for the builtin Array function should be a map.
4359 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4360 // Will both indicate a NULL and a Smi.
4361 __ test(ecx, Immediate(kSmiTagMask));
4362 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4363 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4364 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4365 }
4366
4367 // Figure out the right elements kind
4368 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4369
4370 // Load the map's "bit field 2" into |result|. We only need the first byte,
4371 // but the following masking takes care of that anyway.
4372 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
4373 // Retrieve elements_kind from bit field 2.
4374 __ DecodeField<Map::ElementsKindBits>(ecx);
4375
4376 if (FLAG_debug_code) {
4377 Label done;
4378 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4379 __ j(equal, &done);
4380 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004381 __ Assert(equal, kInvalidElementsKindForInternalArrayOrInternalPackedArray);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004382 __ bind(&done);
4383 }
4384
4385 Label fast_elements_case;
4386 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4387 __ j(equal, &fast_elements_case);
4388 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4389
4390 __ bind(&fast_elements_case);
4391 GenerateCase(masm, FAST_ELEMENTS);
4392}
4393
Ben Murdoch097c5b22016-05-18 11:27:45 +01004394void FastNewObjectStub::Generate(MacroAssembler* masm) {
4395 // ----------- S t a t e -------------
4396 // -- edi : target
4397 // -- edx : new target
4398 // -- esi : context
4399 // -- esp[0] : return address
4400 // -----------------------------------
4401 __ AssertFunction(edi);
4402 __ AssertReceiver(edx);
4403
4404 // Verify that the new target is a JSFunction.
4405 Label new_object;
4406 __ CmpObjectType(edx, JS_FUNCTION_TYPE, ebx);
4407 __ j(not_equal, &new_object);
4408
4409 // Load the initial map and verify that it's in fact a map.
4410 __ mov(ecx, FieldOperand(edx, JSFunction::kPrototypeOrInitialMapOffset));
4411 __ JumpIfSmi(ecx, &new_object);
4412 __ CmpObjectType(ecx, MAP_TYPE, ebx);
4413 __ j(not_equal, &new_object);
4414
4415 // Fall back to runtime if the target differs from the new target's
4416 // initial map constructor.
4417 __ cmp(edi, FieldOperand(ecx, Map::kConstructorOrBackPointerOffset));
4418 __ j(not_equal, &new_object);
4419
4420 // Allocate the JSObject on the heap.
4421 Label allocate, done_allocate;
4422 __ movzx_b(ebx, FieldOperand(ecx, Map::kInstanceSizeOffset));
4423 __ lea(ebx, Operand(ebx, times_pointer_size, 0));
4424 __ Allocate(ebx, eax, edi, no_reg, &allocate, NO_ALLOCATION_FLAGS);
4425 __ bind(&done_allocate);
4426
4427 // Initialize the JSObject fields.
4428 __ mov(Operand(eax, JSObject::kMapOffset), ecx);
4429 __ mov(Operand(eax, JSObject::kPropertiesOffset),
4430 masm->isolate()->factory()->empty_fixed_array());
4431 __ mov(Operand(eax, JSObject::kElementsOffset),
4432 masm->isolate()->factory()->empty_fixed_array());
4433 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
4434 __ lea(ebx, Operand(eax, JSObject::kHeaderSize));
4435
4436 // ----------- S t a t e -------------
4437 // -- eax : result (untagged)
4438 // -- ebx : result fields (untagged)
4439 // -- edi : result end (untagged)
4440 // -- ecx : initial map
4441 // -- esi : context
4442 // -- esp[0] : return address
4443 // -----------------------------------
4444
4445 // Perform in-object slack tracking if requested.
4446 Label slack_tracking;
4447 STATIC_ASSERT(Map::kNoSlackTracking == 0);
4448 __ test(FieldOperand(ecx, Map::kBitField3Offset),
4449 Immediate(Map::ConstructionCounter::kMask));
4450 __ j(not_zero, &slack_tracking, Label::kNear);
4451 {
4452 // Initialize all in-object fields with undefined.
4453 __ LoadRoot(edx, Heap::kUndefinedValueRootIndex);
4454 __ InitializeFieldsWithFiller(ebx, edi, edx);
4455
4456 // Add the object tag to make the JSObject real.
4457 STATIC_ASSERT(kHeapObjectTag == 1);
4458 __ inc(eax);
4459 __ Ret();
4460 }
4461 __ bind(&slack_tracking);
4462 {
4463 // Decrease generous allocation count.
4464 STATIC_ASSERT(Map::ConstructionCounter::kNext == 32);
4465 __ sub(FieldOperand(ecx, Map::kBitField3Offset),
4466 Immediate(1 << Map::ConstructionCounter::kShift));
4467
4468 // Initialize the in-object fields with undefined.
4469 __ movzx_b(edx, FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset));
4470 __ neg(edx);
4471 __ lea(edx, Operand(edi, edx, times_pointer_size, 0));
4472 __ LoadRoot(edi, Heap::kUndefinedValueRootIndex);
4473 __ InitializeFieldsWithFiller(ebx, edx, edi);
4474
4475 // Initialize the remaining (reserved) fields with one pointer filler map.
4476 __ movzx_b(edx, FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset));
4477 __ lea(edx, Operand(ebx, edx, times_pointer_size, 0));
4478 __ LoadRoot(edi, Heap::kOnePointerFillerMapRootIndex);
4479 __ InitializeFieldsWithFiller(ebx, edx, edi);
4480
4481 // Add the object tag to make the JSObject real.
4482 STATIC_ASSERT(kHeapObjectTag == 1);
4483 __ inc(eax);
4484
4485 // Check if we can finalize the instance size.
4486 Label finalize;
4487 STATIC_ASSERT(Map::kSlackTrackingCounterEnd == 1);
4488 __ test(FieldOperand(ecx, Map::kBitField3Offset),
4489 Immediate(Map::ConstructionCounter::kMask));
4490 __ j(zero, &finalize, Label::kNear);
4491 __ Ret();
4492
4493 // Finalize the instance size.
4494 __ bind(&finalize);
4495 {
4496 FrameScope scope(masm, StackFrame::INTERNAL);
4497 __ Push(eax);
4498 __ Push(ecx);
4499 __ CallRuntime(Runtime::kFinalizeInstanceSize);
4500 __ Pop(eax);
4501 }
4502 __ Ret();
4503 }
4504
4505 // Fall back to %AllocateInNewSpace.
4506 __ bind(&allocate);
4507 {
4508 FrameScope scope(masm, StackFrame::INTERNAL);
4509 __ SmiTag(ebx);
4510 __ Push(ecx);
4511 __ Push(ebx);
4512 __ CallRuntime(Runtime::kAllocateInNewSpace);
4513 __ Pop(ecx);
4514 }
4515 STATIC_ASSERT(kHeapObjectTag == 1);
4516 __ dec(eax);
4517 __ movzx_b(ebx, FieldOperand(ecx, Map::kInstanceSizeOffset));
4518 __ lea(edi, Operand(eax, ebx, times_pointer_size, 0));
4519 __ jmp(&done_allocate);
4520
4521 // Fall back to %NewObject.
4522 __ bind(&new_object);
4523 __ PopReturnAddressTo(ecx);
4524 __ Push(edi);
4525 __ Push(edx);
4526 __ PushReturnAddressFrom(ecx);
4527 __ TailCallRuntime(Runtime::kNewObject);
4528}
4529
4530void FastNewRestParameterStub::Generate(MacroAssembler* masm) {
4531 // ----------- S t a t e -------------
4532 // -- edi : function
4533 // -- esi : context
4534 // -- ebp : frame pointer
4535 // -- esp[0] : return address
4536 // -----------------------------------
4537 __ AssertFunction(edi);
4538
4539 // For Ignition we need to skip all possible handler/stub frames until
4540 // we reach the JavaScript frame for the function (similar to what the
4541 // runtime fallback implementation does). So make edx point to that
4542 // JavaScript frame.
4543 {
4544 Label loop, loop_entry;
4545 __ mov(edx, ebp);
4546 __ jmp(&loop_entry, Label::kNear);
4547 __ bind(&loop);
4548 __ mov(edx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
4549 __ bind(&loop_entry);
4550 __ cmp(edi, Operand(edx, StandardFrameConstants::kMarkerOffset));
4551 __ j(not_equal, &loop);
4552 }
4553
4554 // Check if we have rest parameters (only possible if we have an
4555 // arguments adaptor frame below the function frame).
4556 Label no_rest_parameters;
4557 __ mov(ebx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
4558 __ cmp(Operand(ebx, StandardFrameConstants::kContextOffset),
4559 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4560 __ j(not_equal, &no_rest_parameters, Label::kNear);
4561
4562 // Check if the arguments adaptor frame contains more arguments than
4563 // specified by the function's internal formal parameter count.
4564 Label rest_parameters;
4565 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4566 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4567 __ sub(eax,
4568 FieldOperand(ecx, SharedFunctionInfo::kFormalParameterCountOffset));
4569 __ j(greater, &rest_parameters);
4570
4571 // Return an empty rest parameter array.
4572 __ bind(&no_rest_parameters);
4573 {
4574 // ----------- S t a t e -------------
4575 // -- esi : context
4576 // -- esp[0] : return address
4577 // -----------------------------------
4578
4579 // Allocate an empty rest parameter array.
4580 Label allocate, done_allocate;
4581 __ Allocate(JSArray::kSize, eax, edx, ecx, &allocate, TAG_OBJECT);
4582 __ bind(&done_allocate);
4583
4584 // Setup the rest parameter array in rax.
4585 __ LoadGlobalFunction(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, ecx);
4586 __ mov(FieldOperand(eax, JSArray::kMapOffset), ecx);
4587 __ mov(ecx, isolate()->factory()->empty_fixed_array());
4588 __ mov(FieldOperand(eax, JSArray::kPropertiesOffset), ecx);
4589 __ mov(FieldOperand(eax, JSArray::kElementsOffset), ecx);
4590 __ mov(FieldOperand(eax, JSArray::kLengthOffset),
4591 Immediate(Smi::FromInt(0)));
4592 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4593 __ Ret();
4594
4595 // Fall back to %AllocateInNewSpace.
4596 __ bind(&allocate);
4597 {
4598 FrameScope scope(masm, StackFrame::INTERNAL);
4599 __ Push(Smi::FromInt(JSArray::kSize));
4600 __ CallRuntime(Runtime::kAllocateInNewSpace);
4601 }
4602 __ jmp(&done_allocate);
4603 }
4604
4605 __ bind(&rest_parameters);
4606 {
4607 // Compute the pointer to the first rest parameter (skippping the receiver).
4608 __ lea(ebx,
4609 Operand(ebx, eax, times_half_pointer_size,
4610 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4611
4612 // ----------- S t a t e -------------
4613 // -- esi : context
4614 // -- eax : number of rest parameters (tagged)
4615 // -- ebx : pointer to first rest parameters
4616 // -- esp[0] : return address
4617 // -----------------------------------
4618
4619 // Allocate space for the rest parameter array plus the backing store.
4620 Label allocate, done_allocate;
4621 __ lea(ecx, Operand(eax, times_half_pointer_size,
4622 JSArray::kSize + FixedArray::kHeaderSize));
4623 __ Allocate(ecx, edx, edi, no_reg, &allocate, TAG_OBJECT);
4624 __ bind(&done_allocate);
4625
4626 // Setup the elements array in edx.
4627 __ mov(FieldOperand(edx, FixedArray::kMapOffset),
4628 isolate()->factory()->fixed_array_map());
4629 __ mov(FieldOperand(edx, FixedArray::kLengthOffset), eax);
4630 {
4631 Label loop, done_loop;
4632 __ Move(ecx, Smi::FromInt(0));
4633 __ bind(&loop);
4634 __ cmp(ecx, eax);
4635 __ j(equal, &done_loop, Label::kNear);
4636 __ mov(edi, Operand(ebx, 0 * kPointerSize));
4637 __ mov(FieldOperand(edx, ecx, times_half_pointer_size,
4638 FixedArray::kHeaderSize),
4639 edi);
4640 __ sub(ebx, Immediate(1 * kPointerSize));
4641 __ add(ecx, Immediate(Smi::FromInt(1)));
4642 __ jmp(&loop);
4643 __ bind(&done_loop);
4644 }
4645
4646 // Setup the rest parameter array in edi.
4647 __ lea(edi,
4648 Operand(edx, eax, times_half_pointer_size, FixedArray::kHeaderSize));
4649 __ LoadGlobalFunction(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, ecx);
4650 __ mov(FieldOperand(edi, JSArray::kMapOffset), ecx);
4651 __ mov(FieldOperand(edi, JSArray::kPropertiesOffset),
4652 isolate()->factory()->empty_fixed_array());
4653 __ mov(FieldOperand(edi, JSArray::kElementsOffset), edx);
4654 __ mov(FieldOperand(edi, JSArray::kLengthOffset), eax);
4655 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4656 __ mov(eax, edi);
4657 __ Ret();
4658
4659 // Fall back to %AllocateInNewSpace.
4660 __ bind(&allocate);
4661 {
4662 FrameScope scope(masm, StackFrame::INTERNAL);
4663 __ SmiTag(ecx);
4664 __ Push(eax);
4665 __ Push(ebx);
4666 __ Push(ecx);
4667 __ CallRuntime(Runtime::kAllocateInNewSpace);
4668 __ mov(edx, eax);
4669 __ Pop(ebx);
4670 __ Pop(eax);
4671 }
4672 __ jmp(&done_allocate);
4673 }
4674}
4675
4676void FastNewSloppyArgumentsStub::Generate(MacroAssembler* masm) {
4677 // ----------- S t a t e -------------
4678 // -- edi : function
4679 // -- esi : context
4680 // -- ebp : frame pointer
4681 // -- esp[0] : return address
4682 // -----------------------------------
4683 __ AssertFunction(edi);
4684
4685 // TODO(bmeurer): Cleanup to match the FastNewStrictArgumentsStub.
4686 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4687 __ mov(ecx,
4688 FieldOperand(ecx, SharedFunctionInfo::kFormalParameterCountOffset));
4689 __ lea(edx, Operand(ebp, ecx, times_half_pointer_size,
4690 StandardFrameConstants::kCallerSPOffset));
4691
4692 // ecx : number of parameters (tagged)
4693 // edx : parameters pointer
4694 // edi : function
4695 // esp[0] : return address
4696
4697 // Check if the calling frame is an arguments adaptor frame.
4698 Label adaptor_frame, try_allocate, runtime;
4699 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
4700 __ mov(eax, Operand(ebx, StandardFrameConstants::kContextOffset));
4701 __ cmp(eax, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4702 __ j(equal, &adaptor_frame, Label::kNear);
4703
4704 // No adaptor, parameter count = argument count.
4705 __ mov(ebx, ecx);
4706 __ push(ecx);
4707 __ jmp(&try_allocate, Label::kNear);
4708
4709 // We have an adaptor frame. Patch the parameters pointer.
4710 __ bind(&adaptor_frame);
4711 __ mov(ebx, ecx);
4712 __ push(ecx);
4713 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
4714 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4715 __ lea(edx,
4716 Operand(edx, ecx, times_2, StandardFrameConstants::kCallerSPOffset));
4717
4718 // ebx = parameter count (tagged)
4719 // ecx = argument count (smi-tagged)
4720 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
4721 __ cmp(ebx, ecx);
4722 __ j(less_equal, &try_allocate, Label::kNear);
4723 __ mov(ebx, ecx);
4724
4725 // Save mapped parameter count and function.
4726 __ bind(&try_allocate);
4727 __ push(edi);
4728 __ push(ebx);
4729
4730 // Compute the sizes of backing store, parameter map, and arguments object.
4731 // 1. Parameter map, has 2 extra words containing context and backing store.
4732 const int kParameterMapHeaderSize =
4733 FixedArray::kHeaderSize + 2 * kPointerSize;
4734 Label no_parameter_map;
4735 __ test(ebx, ebx);
4736 __ j(zero, &no_parameter_map, Label::kNear);
4737 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
4738 __ bind(&no_parameter_map);
4739
4740 // 2. Backing store.
4741 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
4742
4743 // 3. Arguments object.
4744 __ add(ebx, Immediate(JSSloppyArgumentsObject::kSize));
4745
4746 // Do the allocation of all three objects in one go.
4747 __ Allocate(ebx, eax, edi, no_reg, &runtime, TAG_OBJECT);
4748
4749 // eax = address of new object(s) (tagged)
4750 // ecx = argument count (smi-tagged)
4751 // esp[0] = mapped parameter count (tagged)
4752 // esp[4] = function
4753 // esp[8] = parameter count (tagged)
4754 // Get the arguments map from the current native context into edi.
4755 Label has_mapped_parameters, instantiate;
4756 __ mov(edi, NativeContextOperand());
4757 __ mov(ebx, Operand(esp, 0 * kPointerSize));
4758 __ test(ebx, ebx);
4759 __ j(not_zero, &has_mapped_parameters, Label::kNear);
4760 __ mov(
4761 edi,
4762 Operand(edi, Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX)));
4763 __ jmp(&instantiate, Label::kNear);
4764
4765 __ bind(&has_mapped_parameters);
4766 __ mov(edi, Operand(edi, Context::SlotOffset(
4767 Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX)));
4768 __ bind(&instantiate);
4769
4770 // eax = address of new object (tagged)
4771 // ebx = mapped parameter count (tagged)
4772 // ecx = argument count (smi-tagged)
4773 // edi = address of arguments map (tagged)
4774 // esp[0] = mapped parameter count (tagged)
4775 // esp[4] = function
4776 // esp[8] = parameter count (tagged)
4777 // Copy the JS object part.
4778 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
4779 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
4780 masm->isolate()->factory()->empty_fixed_array());
4781 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
4782 masm->isolate()->factory()->empty_fixed_array());
4783
4784 // Set up the callee in-object property.
4785 STATIC_ASSERT(JSSloppyArgumentsObject::kCalleeIndex == 1);
4786 __ mov(edi, Operand(esp, 1 * kPointerSize));
4787 __ AssertNotSmi(edi);
4788 __ mov(FieldOperand(eax, JSSloppyArgumentsObject::kCalleeOffset), edi);
4789
4790 // Use the length (smi tagged) and set that as an in-object property too.
4791 __ AssertSmi(ecx);
4792 __ mov(FieldOperand(eax, JSSloppyArgumentsObject::kLengthOffset), ecx);
4793
4794 // Set up the elements pointer in the allocated arguments object.
4795 // If we allocated a parameter map, edi will point there, otherwise to the
4796 // backing store.
4797 __ lea(edi, Operand(eax, JSSloppyArgumentsObject::kSize));
4798 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
4799
4800 // eax = address of new object (tagged)
4801 // ebx = mapped parameter count (tagged)
4802 // ecx = argument count (tagged)
4803 // edx = address of receiver argument
4804 // edi = address of parameter map or backing store (tagged)
4805 // esp[0] = mapped parameter count (tagged)
4806 // esp[4] = function
4807 // esp[8] = parameter count (tagged)
4808 // Free two registers.
4809 __ push(edx);
4810 __ push(eax);
4811
4812 // Initialize parameter map. If there are no mapped arguments, we're done.
4813 Label skip_parameter_map;
4814 __ test(ebx, ebx);
4815 __ j(zero, &skip_parameter_map);
4816
4817 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
4818 Immediate(isolate()->factory()->sloppy_arguments_elements_map()));
4819 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
4820 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
4821 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
4822 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
4823 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
4824
4825 // Copy the parameter slots and the holes in the arguments.
4826 // We need to fill in mapped_parameter_count slots. They index the context,
4827 // where parameters are stored in reverse order, at
4828 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
4829 // The mapped parameter thus need to get indices
4830 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
4831 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
4832 // We loop from right to left.
4833 Label parameters_loop, parameters_test;
4834 __ push(ecx);
4835 __ mov(eax, Operand(esp, 3 * kPointerSize));
4836 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
4837 __ add(ebx, Operand(esp, 5 * kPointerSize));
4838 __ sub(ebx, eax);
4839 __ mov(ecx, isolate()->factory()->the_hole_value());
4840 __ mov(edx, edi);
4841 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
4842 // eax = loop variable (tagged)
4843 // ebx = mapping index (tagged)
4844 // ecx = the hole value
4845 // edx = address of parameter map (tagged)
4846 // edi = address of backing store (tagged)
4847 // esp[0] = argument count (tagged)
4848 // esp[4] = address of new object (tagged)
4849 // esp[8] = address of receiver argument
4850 // esp[12] = mapped parameter count (tagged)
4851 // esp[16] = function
4852 // esp[20] = parameter count (tagged)
4853 __ jmp(&parameters_test, Label::kNear);
4854
4855 __ bind(&parameters_loop);
4856 __ sub(eax, Immediate(Smi::FromInt(1)));
4857 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
4858 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
4859 __ add(ebx, Immediate(Smi::FromInt(1)));
4860 __ bind(&parameters_test);
4861 __ test(eax, eax);
4862 __ j(not_zero, &parameters_loop, Label::kNear);
4863 __ pop(ecx);
4864
4865 __ bind(&skip_parameter_map);
4866
4867 // ecx = argument count (tagged)
4868 // edi = address of backing store (tagged)
4869 // esp[0] = address of new object (tagged)
4870 // esp[4] = address of receiver argument
4871 // esp[8] = mapped parameter count (tagged)
4872 // esp[12] = function
4873 // esp[16] = parameter count (tagged)
4874 // Copy arguments header and remaining slots (if there are any).
4875 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
4876 Immediate(isolate()->factory()->fixed_array_map()));
4877 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
4878
4879 Label arguments_loop, arguments_test;
4880 __ mov(ebx, Operand(esp, 2 * kPointerSize));
4881 __ mov(edx, Operand(esp, 1 * kPointerSize));
4882 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
4883 __ sub(edx, ebx);
4884 __ jmp(&arguments_test, Label::kNear);
4885
4886 __ bind(&arguments_loop);
4887 __ sub(edx, Immediate(kPointerSize));
4888 __ mov(eax, Operand(edx, 0));
4889 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
4890 __ add(ebx, Immediate(Smi::FromInt(1)));
4891
4892 __ bind(&arguments_test);
4893 __ cmp(ebx, ecx);
4894 __ j(less, &arguments_loop, Label::kNear);
4895
4896 // Restore.
4897 __ pop(eax); // Address of arguments object.
4898 __ Drop(4);
4899
4900 // Return.
4901 __ ret(0);
4902
4903 // Do the runtime call to allocate the arguments object.
4904 __ bind(&runtime);
4905 __ pop(eax); // Remove saved mapped parameter count.
4906 __ pop(edi); // Pop saved function.
4907 __ pop(eax); // Remove saved parameter count.
4908 __ pop(eax); // Pop return address.
4909 __ push(edi); // Push function.
4910 __ push(edx); // Push parameters pointer.
4911 __ push(ecx); // Push parameter count.
4912 __ push(eax); // Push return address.
4913 __ TailCallRuntime(Runtime::kNewSloppyArguments);
4914}
4915
4916void FastNewStrictArgumentsStub::Generate(MacroAssembler* masm) {
4917 // ----------- S t a t e -------------
4918 // -- edi : function
4919 // -- esi : context
4920 // -- ebp : frame pointer
4921 // -- esp[0] : return address
4922 // -----------------------------------
4923 __ AssertFunction(edi);
4924
4925 // For Ignition we need to skip all possible handler/stub frames until
4926 // we reach the JavaScript frame for the function (similar to what the
4927 // runtime fallback implementation does). So make edx point to that
4928 // JavaScript frame.
4929 {
4930 Label loop, loop_entry;
4931 __ mov(edx, ebp);
4932 __ jmp(&loop_entry, Label::kNear);
4933 __ bind(&loop);
4934 __ mov(edx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
4935 __ bind(&loop_entry);
4936 __ cmp(edi, Operand(edx, StandardFrameConstants::kMarkerOffset));
4937 __ j(not_equal, &loop);
4938 }
4939
4940 // Check if we have an arguments adaptor frame below the function frame.
4941 Label arguments_adaptor, arguments_done;
4942 __ mov(ebx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
4943 __ cmp(Operand(ebx, StandardFrameConstants::kContextOffset),
4944 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4945 __ j(equal, &arguments_adaptor, Label::kNear);
4946 {
4947 __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4948 __ mov(eax,
4949 FieldOperand(eax, SharedFunctionInfo::kFormalParameterCountOffset));
4950 __ lea(ebx,
4951 Operand(edx, eax, times_half_pointer_size,
4952 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4953 }
4954 __ jmp(&arguments_done, Label::kNear);
4955 __ bind(&arguments_adaptor);
4956 {
4957 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4958 __ lea(ebx,
4959 Operand(ebx, eax, times_half_pointer_size,
4960 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4961 }
4962 __ bind(&arguments_done);
4963
4964 // ----------- S t a t e -------------
4965 // -- eax : number of arguments (tagged)
4966 // -- ebx : pointer to the first argument
4967 // -- esi : context
4968 // -- esp[0] : return address
4969 // -----------------------------------
4970
4971 // Allocate space for the strict arguments object plus the backing store.
4972 Label allocate, done_allocate;
4973 __ lea(ecx,
4974 Operand(eax, times_half_pointer_size,
4975 JSStrictArgumentsObject::kSize + FixedArray::kHeaderSize));
4976 __ Allocate(ecx, edx, edi, no_reg, &allocate, TAG_OBJECT);
4977 __ bind(&done_allocate);
4978
4979 // Setup the elements array in edx.
4980 __ mov(FieldOperand(edx, FixedArray::kMapOffset),
4981 isolate()->factory()->fixed_array_map());
4982 __ mov(FieldOperand(edx, FixedArray::kLengthOffset), eax);
4983 {
4984 Label loop, done_loop;
4985 __ Move(ecx, Smi::FromInt(0));
4986 __ bind(&loop);
4987 __ cmp(ecx, eax);
4988 __ j(equal, &done_loop, Label::kNear);
4989 __ mov(edi, Operand(ebx, 0 * kPointerSize));
4990 __ mov(FieldOperand(edx, ecx, times_half_pointer_size,
4991 FixedArray::kHeaderSize),
4992 edi);
4993 __ sub(ebx, Immediate(1 * kPointerSize));
4994 __ add(ecx, Immediate(Smi::FromInt(1)));
4995 __ jmp(&loop);
4996 __ bind(&done_loop);
4997 }
4998
4999 // Setup the rest parameter array in edi.
5000 __ lea(edi,
5001 Operand(edx, eax, times_half_pointer_size, FixedArray::kHeaderSize));
5002 __ LoadGlobalFunction(Context::STRICT_ARGUMENTS_MAP_INDEX, ecx);
5003 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kMapOffset), ecx);
5004 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kPropertiesOffset),
5005 isolate()->factory()->empty_fixed_array());
5006 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kElementsOffset), edx);
5007 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kLengthOffset), eax);
5008 STATIC_ASSERT(JSStrictArgumentsObject::kSize == 4 * kPointerSize);
5009 __ mov(eax, edi);
5010 __ Ret();
5011
5012 // Fall back to %AllocateInNewSpace.
5013 __ bind(&allocate);
5014 {
5015 FrameScope scope(masm, StackFrame::INTERNAL);
5016 __ SmiTag(ecx);
5017 __ Push(eax);
5018 __ Push(ebx);
5019 __ Push(ecx);
5020 __ CallRuntime(Runtime::kAllocateInNewSpace);
5021 __ mov(edx, eax);
5022 __ Pop(ebx);
5023 __ Pop(eax);
5024 }
5025 __ jmp(&done_allocate);
5026}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005027
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005028void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
5029 Register context_reg = esi;
5030 Register slot_reg = ebx;
5031 Register result_reg = eax;
5032 Label slow_case;
5033
5034 // Go up context chain to the script context.
5035 for (int i = 0; i < depth(); ++i) {
5036 __ mov(result_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5037 context_reg = result_reg;
5038 }
5039
5040 // Load the PropertyCell value at the specified slot.
5041 __ mov(result_reg, ContextOperand(context_reg, slot_reg));
5042 __ mov(result_reg, FieldOperand(result_reg, PropertyCell::kValueOffset));
5043
5044 // Check that value is not the_hole.
5045 __ CompareRoot(result_reg, Heap::kTheHoleValueRootIndex);
5046 __ j(equal, &slow_case, Label::kNear);
5047 __ Ret();
5048
5049 // Fallback to the runtime.
5050 __ bind(&slow_case);
5051 __ SmiTag(slot_reg);
5052 __ Pop(result_reg); // Pop return address.
5053 __ Push(slot_reg);
5054 __ Push(result_reg); // Push return address.
5055 __ TailCallRuntime(Runtime::kLoadGlobalViaContext);
5056}
5057
5058
5059void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
5060 Register context_reg = esi;
5061 Register slot_reg = ebx;
5062 Register value_reg = eax;
5063 Register cell_reg = edi;
5064 Register cell_details_reg = edx;
5065 Register cell_value_reg = ecx;
5066 Label fast_heapobject_case, fast_smi_case, slow_case;
5067
5068 if (FLAG_debug_code) {
5069 __ CompareRoot(value_reg, Heap::kTheHoleValueRootIndex);
5070 __ Check(not_equal, kUnexpectedValue);
5071 }
5072
5073 // Go up context chain to the script context.
5074 for (int i = 0; i < depth(); ++i) {
5075 __ mov(cell_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5076 context_reg = cell_reg;
5077 }
5078
5079 // Load the PropertyCell at the specified slot.
5080 __ mov(cell_reg, ContextOperand(context_reg, slot_reg));
5081
5082 // Load PropertyDetails for the cell (actually only the cell_type and kind).
5083 __ mov(cell_details_reg,
5084 FieldOperand(cell_reg, PropertyCell::kDetailsOffset));
5085 __ SmiUntag(cell_details_reg);
5086 __ and_(cell_details_reg,
5087 Immediate(PropertyDetails::PropertyCellTypeField::kMask |
5088 PropertyDetails::KindField::kMask |
5089 PropertyDetails::kAttributesReadOnlyMask));
5090
5091 // Check if PropertyCell holds mutable data.
5092 Label not_mutable_data;
5093 __ cmp(cell_details_reg,
5094 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5095 PropertyCellType::kMutable) |
5096 PropertyDetails::KindField::encode(kData)));
5097 __ j(not_equal, &not_mutable_data);
5098 __ JumpIfSmi(value_reg, &fast_smi_case);
5099 __ bind(&fast_heapobject_case);
5100 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5101 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
5102 cell_details_reg, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
5103 OMIT_SMI_CHECK);
5104 // RecordWriteField clobbers the value register, so we need to reload.
5105 __ mov(value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5106 __ Ret();
5107 __ bind(&not_mutable_data);
5108
5109 // Check if PropertyCell value matches the new value (relevant for Constant,
5110 // ConstantType and Undefined cells).
5111 Label not_same_value;
5112 __ mov(cell_value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5113 __ cmp(cell_value_reg, value_reg);
5114 __ j(not_equal, &not_same_value,
5115 FLAG_debug_code ? Label::kFar : Label::kNear);
5116 // Make sure the PropertyCell is not marked READ_ONLY.
5117 __ test(cell_details_reg,
5118 Immediate(PropertyDetails::kAttributesReadOnlyMask));
5119 __ j(not_zero, &slow_case);
5120 if (FLAG_debug_code) {
5121 Label done;
5122 // This can only be true for Constant, ConstantType and Undefined cells,
5123 // because we never store the_hole via this stub.
5124 __ cmp(cell_details_reg,
5125 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5126 PropertyCellType::kConstant) |
5127 PropertyDetails::KindField::encode(kData)));
5128 __ j(equal, &done);
5129 __ cmp(cell_details_reg,
5130 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5131 PropertyCellType::kConstantType) |
5132 PropertyDetails::KindField::encode(kData)));
5133 __ j(equal, &done);
5134 __ cmp(cell_details_reg,
5135 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5136 PropertyCellType::kUndefined) |
5137 PropertyDetails::KindField::encode(kData)));
5138 __ Check(equal, kUnexpectedValue);
5139 __ bind(&done);
5140 }
5141 __ Ret();
5142 __ bind(&not_same_value);
5143
5144 // Check if PropertyCell contains data with constant type (and is not
5145 // READ_ONLY).
5146 __ cmp(cell_details_reg,
5147 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5148 PropertyCellType::kConstantType) |
5149 PropertyDetails::KindField::encode(kData)));
5150 __ j(not_equal, &slow_case, Label::kNear);
5151
5152 // Now either both old and new values must be SMIs or both must be heap
5153 // objects with same map.
5154 Label value_is_heap_object;
5155 __ JumpIfNotSmi(value_reg, &value_is_heap_object, Label::kNear);
5156 __ JumpIfNotSmi(cell_value_reg, &slow_case, Label::kNear);
5157 // Old and new values are SMIs, no need for a write barrier here.
5158 __ bind(&fast_smi_case);
5159 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5160 __ Ret();
5161 __ bind(&value_is_heap_object);
5162 __ JumpIfSmi(cell_value_reg, &slow_case, Label::kNear);
5163 Register cell_value_map_reg = cell_value_reg;
5164 __ mov(cell_value_map_reg,
5165 FieldOperand(cell_value_reg, HeapObject::kMapOffset));
5166 __ cmp(cell_value_map_reg, FieldOperand(value_reg, HeapObject::kMapOffset));
5167 __ j(equal, &fast_heapobject_case);
5168
5169 // Fallback to the runtime.
5170 __ bind(&slow_case);
5171 __ SmiTag(slot_reg);
5172 __ Pop(cell_reg); // Pop return address.
5173 __ Push(slot_reg);
5174 __ Push(value_reg);
5175 __ Push(cell_reg); // Push return address.
5176 __ TailCallRuntime(is_strict(language_mode())
5177 ? Runtime::kStoreGlobalViaContext_Strict
5178 : Runtime::kStoreGlobalViaContext_Sloppy);
5179}
5180
5181
5182// Generates an Operand for saving parameters after PrepareCallApiFunction.
5183static Operand ApiParameterOperand(int index) {
5184 return Operand(esp, index * kPointerSize);
5185}
5186
5187
5188// Prepares stack to put arguments (aligns and so on). Reserves
5189// space for return value if needed (assumes the return value is a handle).
5190// Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
5191// etc. Saves context (esi). If space was reserved for return value then
5192// stores the pointer to the reserved slot into esi.
5193static void PrepareCallApiFunction(MacroAssembler* masm, int argc) {
5194 __ EnterApiExitFrame(argc);
5195 if (__ emit_debug_code()) {
5196 __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
5197 }
5198}
5199
5200
5201// Calls an API function. Allocates HandleScope, extracts returned value
5202// from handle and propagates exceptions. Clobbers ebx, edi and
5203// caller-save registers. Restores context. On return removes
5204// stack_space * kPointerSize (GCed).
5205static void CallApiFunctionAndReturn(MacroAssembler* masm,
5206 Register function_address,
5207 ExternalReference thunk_ref,
5208 Operand thunk_last_arg, int stack_space,
5209 Operand* stack_space_operand,
5210 Operand return_value_operand,
5211 Operand* context_restore_operand) {
5212 Isolate* isolate = masm->isolate();
5213
5214 ExternalReference next_address =
5215 ExternalReference::handle_scope_next_address(isolate);
5216 ExternalReference limit_address =
5217 ExternalReference::handle_scope_limit_address(isolate);
5218 ExternalReference level_address =
5219 ExternalReference::handle_scope_level_address(isolate);
5220
5221 DCHECK(edx.is(function_address));
5222 // Allocate HandleScope in callee-save registers.
5223 __ mov(ebx, Operand::StaticVariable(next_address));
5224 __ mov(edi, Operand::StaticVariable(limit_address));
5225 __ add(Operand::StaticVariable(level_address), Immediate(1));
5226
5227 if (FLAG_log_timer_events) {
5228 FrameScope frame(masm, StackFrame::MANUAL);
5229 __ PushSafepointRegisters();
5230 __ PrepareCallCFunction(1, eax);
5231 __ mov(Operand(esp, 0),
5232 Immediate(ExternalReference::isolate_address(isolate)));
5233 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5234 1);
5235 __ PopSafepointRegisters();
5236 }
5237
5238
5239 Label profiler_disabled;
5240 Label end_profiler_check;
5241 __ mov(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
5242 __ cmpb(Operand(eax, 0), 0);
5243 __ j(zero, &profiler_disabled);
5244
5245 // Additional parameter is the address of the actual getter function.
5246 __ mov(thunk_last_arg, function_address);
5247 // Call the api function.
5248 __ mov(eax, Immediate(thunk_ref));
5249 __ call(eax);
5250 __ jmp(&end_profiler_check);
5251
5252 __ bind(&profiler_disabled);
5253 // Call the api function.
5254 __ call(function_address);
5255 __ bind(&end_profiler_check);
5256
5257 if (FLAG_log_timer_events) {
5258 FrameScope frame(masm, StackFrame::MANUAL);
5259 __ PushSafepointRegisters();
5260 __ PrepareCallCFunction(1, eax);
5261 __ mov(Operand(esp, 0),
5262 Immediate(ExternalReference::isolate_address(isolate)));
5263 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5264 1);
5265 __ PopSafepointRegisters();
5266 }
5267
5268 Label prologue;
5269 // Load the value from ReturnValue
5270 __ mov(eax, return_value_operand);
5271
5272 Label promote_scheduled_exception;
5273 Label delete_allocated_handles;
5274 Label leave_exit_frame;
5275
5276 __ bind(&prologue);
5277 // No more valid handles (the result handle was the last one). Restore
5278 // previous handle scope.
5279 __ mov(Operand::StaticVariable(next_address), ebx);
5280 __ sub(Operand::StaticVariable(level_address), Immediate(1));
5281 __ Assert(above_equal, kInvalidHandleScopeLevel);
5282 __ cmp(edi, Operand::StaticVariable(limit_address));
5283 __ j(not_equal, &delete_allocated_handles);
5284
5285 // Leave the API exit frame.
5286 __ bind(&leave_exit_frame);
5287 bool restore_context = context_restore_operand != NULL;
5288 if (restore_context) {
5289 __ mov(esi, *context_restore_operand);
5290 }
5291 if (stack_space_operand != nullptr) {
5292 __ mov(ebx, *stack_space_operand);
5293 }
5294 __ LeaveApiExitFrame(!restore_context);
5295
5296 // Check if the function scheduled an exception.
5297 ExternalReference scheduled_exception_address =
5298 ExternalReference::scheduled_exception_address(isolate);
5299 __ cmp(Operand::StaticVariable(scheduled_exception_address),
5300 Immediate(isolate->factory()->the_hole_value()));
5301 __ j(not_equal, &promote_scheduled_exception);
5302
5303#if DEBUG
5304 // Check if the function returned a valid JavaScript value.
5305 Label ok;
5306 Register return_value = eax;
5307 Register map = ecx;
5308
5309 __ JumpIfSmi(return_value, &ok, Label::kNear);
5310 __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
5311
5312 __ CmpInstanceType(map, LAST_NAME_TYPE);
5313 __ j(below_equal, &ok, Label::kNear);
5314
5315 __ CmpInstanceType(map, FIRST_JS_RECEIVER_TYPE);
5316 __ j(above_equal, &ok, Label::kNear);
5317
5318 __ cmp(map, isolate->factory()->heap_number_map());
5319 __ j(equal, &ok, Label::kNear);
5320
5321 __ cmp(return_value, isolate->factory()->undefined_value());
5322 __ j(equal, &ok, Label::kNear);
5323
5324 __ cmp(return_value, isolate->factory()->true_value());
5325 __ j(equal, &ok, Label::kNear);
5326
5327 __ cmp(return_value, isolate->factory()->false_value());
5328 __ j(equal, &ok, Label::kNear);
5329
5330 __ cmp(return_value, isolate->factory()->null_value());
5331 __ j(equal, &ok, Label::kNear);
5332
5333 __ Abort(kAPICallReturnedInvalidObject);
5334
5335 __ bind(&ok);
5336#endif
5337
5338 if (stack_space_operand != nullptr) {
5339 DCHECK_EQ(0, stack_space);
5340 __ pop(ecx);
5341 __ add(esp, ebx);
5342 __ jmp(ecx);
5343 } else {
5344 __ ret(stack_space * kPointerSize);
5345 }
5346
5347 // Re-throw by promoting a scheduled exception.
5348 __ bind(&promote_scheduled_exception);
5349 __ TailCallRuntime(Runtime::kPromoteScheduledException);
5350
5351 // HandleScope limit has changed. Delete allocated extensions.
5352 ExternalReference delete_extensions =
5353 ExternalReference::delete_handle_scope_extensions(isolate);
5354 __ bind(&delete_allocated_handles);
5355 __ mov(Operand::StaticVariable(limit_address), edi);
5356 __ mov(edi, eax);
5357 __ mov(Operand(esp, 0),
5358 Immediate(ExternalReference::isolate_address(isolate)));
5359 __ mov(eax, Immediate(delete_extensions));
5360 __ call(eax);
5361 __ mov(eax, edi);
5362 __ jmp(&leave_exit_frame);
5363}
5364
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005365static void CallApiFunctionStubHelper(MacroAssembler* masm,
5366 const ParameterCount& argc,
5367 bool return_first_arg,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005368 bool call_data_undefined, bool is_lazy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005369 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005370 // -- edi : callee
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005371 // -- ebx : call_data
5372 // -- ecx : holder
5373 // -- edx : api_function_address
5374 // -- esi : context
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005375 // -- eax : number of arguments if argc is a register
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005376 // --
5377 // -- esp[0] : return address
5378 // -- esp[4] : last argument
5379 // -- ...
5380 // -- esp[argc * 4] : first argument
5381 // -- esp[(argc + 1) * 4] : receiver
5382 // -----------------------------------
5383
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005384 Register callee = edi;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005385 Register call_data = ebx;
5386 Register holder = ecx;
5387 Register api_function_address = edx;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005388 Register context = esi;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005389 Register return_address = eax;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005390
5391 typedef FunctionCallbackArguments FCA;
5392
5393 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5394 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5395 STATIC_ASSERT(FCA::kDataIndex == 4);
5396 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5397 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5398 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5399 STATIC_ASSERT(FCA::kHolderIndex == 0);
5400 STATIC_ASSERT(FCA::kArgsLength == 7);
5401
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005402 DCHECK(argc.is_immediate() || eax.is(argc.reg()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005403
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005404 if (argc.is_immediate()) {
5405 __ pop(return_address);
5406 // context save.
5407 __ push(context);
5408 } else {
5409 // pop return address and save context
5410 __ xchg(context, Operand(esp, 0));
5411 return_address = context;
5412 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005413
5414 // callee
5415 __ push(callee);
5416
5417 // call data
5418 __ push(call_data);
5419
5420 Register scratch = call_data;
5421 if (!call_data_undefined) {
5422 // return value
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005423 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005424 // return value default
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005425 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005426 } else {
5427 // return value
5428 __ push(scratch);
5429 // return value default
5430 __ push(scratch);
5431 }
5432 // isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005433 __ push(Immediate(reinterpret_cast<int>(masm->isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005434 // holder
5435 __ push(holder);
5436
5437 __ mov(scratch, esp);
5438
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005439 // push return address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005440 __ push(return_address);
5441
Ben Murdoch097c5b22016-05-18 11:27:45 +01005442 if (!is_lazy) {
5443 // load context from callee
5444 __ mov(context, FieldOperand(callee, JSFunction::kContextOffset));
5445 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005446
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005447 // API function gets reference to the v8::Arguments. If CPU profiler
5448 // is enabled wrapper function will be called and we need to pass
5449 // address of the callback as additional parameter, always allocate
5450 // space for it.
5451 const int kApiArgc = 1 + 1;
5452
5453 // Allocate the v8::Arguments structure in the arguments' space since
5454 // it's not controlled by GC.
5455 const int kApiStackSpace = 4;
5456
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005457 PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005458
5459 // FunctionCallbackInfo::implicit_args_.
5460 __ mov(ApiParameterOperand(2), scratch);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005461 if (argc.is_immediate()) {
5462 __ add(scratch,
5463 Immediate((argc.immediate() + FCA::kArgsLength - 1) * kPointerSize));
5464 // FunctionCallbackInfo::values_.
5465 __ mov(ApiParameterOperand(3), scratch);
5466 // FunctionCallbackInfo::length_.
5467 __ Move(ApiParameterOperand(4), Immediate(argc.immediate()));
5468 // FunctionCallbackInfo::is_construct_call_.
5469 __ Move(ApiParameterOperand(5), Immediate(0));
5470 } else {
5471 __ lea(scratch, Operand(scratch, argc.reg(), times_pointer_size,
5472 (FCA::kArgsLength - 1) * kPointerSize));
5473 // FunctionCallbackInfo::values_.
5474 __ mov(ApiParameterOperand(3), scratch);
5475 // FunctionCallbackInfo::length_.
5476 __ mov(ApiParameterOperand(4), argc.reg());
5477 // FunctionCallbackInfo::is_construct_call_.
5478 __ lea(argc.reg(), Operand(argc.reg(), times_pointer_size,
5479 (FCA::kArgsLength + 1) * kPointerSize));
5480 __ mov(ApiParameterOperand(5), argc.reg());
5481 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005482
5483 // v8::InvocationCallback's argument.
5484 __ lea(scratch, ApiParameterOperand(2));
5485 __ mov(ApiParameterOperand(0), scratch);
5486
5487 ExternalReference thunk_ref =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005488 ExternalReference::invoke_function_callback(masm->isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005489
5490 Operand context_restore_operand(ebp,
5491 (2 + FCA::kContextSaveIndex) * kPointerSize);
5492 // Stores return the first js argument
5493 int return_value_offset = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005494 if (return_first_arg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005495 return_value_offset = 2 + FCA::kArgsLength;
5496 } else {
5497 return_value_offset = 2 + FCA::kReturnValueOffset;
5498 }
5499 Operand return_value_operand(ebp, return_value_offset * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005500 int stack_space = 0;
5501 Operand is_construct_call_operand = ApiParameterOperand(5);
5502 Operand* stack_space_operand = &is_construct_call_operand;
5503 if (argc.is_immediate()) {
5504 stack_space = argc.immediate() + FCA::kArgsLength + 1;
5505 stack_space_operand = nullptr;
5506 }
5507 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5508 ApiParameterOperand(1), stack_space,
5509 stack_space_operand, return_value_operand,
5510 &context_restore_operand);
5511}
5512
5513
5514void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5515 bool call_data_undefined = this->call_data_undefined();
5516 CallApiFunctionStubHelper(masm, ParameterCount(eax), false,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005517 call_data_undefined, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005518}
5519
5520
5521void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5522 bool is_store = this->is_store();
5523 int argc = this->argc();
5524 bool call_data_undefined = this->call_data_undefined();
Ben Murdoch097c5b22016-05-18 11:27:45 +01005525 bool is_lazy = this->is_lazy();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005526 CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005527 call_data_undefined, is_lazy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005528}
5529
5530
5531void CallApiGetterStub::Generate(MacroAssembler* masm) {
5532 // ----------- S t a t e -------------
Ben Murdoch097c5b22016-05-18 11:27:45 +01005533 // -- esp[0] : return address
5534 // -- esp[4] : name
5535 // -- esp[8 .. (8 + kArgsLength*4)] : v8::PropertyCallbackInfo::args_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005536 // -- ...
Ben Murdoch097c5b22016-05-18 11:27:45 +01005537 // -- edx : api_function_address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005538 // -----------------------------------
5539 DCHECK(edx.is(ApiGetterDescriptor::function_address()));
5540
Ben Murdoch097c5b22016-05-18 11:27:45 +01005541 // v8::PropertyCallbackInfo::args_ array and name handle.
5542 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5543
5544 // Allocate v8::PropertyCallbackInfo object, arguments for callback and
5545 // space for optional callback address parameter (in case CPU profiler is
5546 // active) in non-GCed stack space.
5547 const int kApiArgc = 3 + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005548
5549 Register api_function_address = edx;
5550 Register scratch = ebx;
5551
Ben Murdoch097c5b22016-05-18 11:27:45 +01005552 // Load address of v8::PropertyAccessorInfo::args_ array.
5553 __ lea(scratch, Operand(esp, 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005554
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005555 PrepareCallApiFunction(masm, kApiArgc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005556 // Create v8::PropertyCallbackInfo object on the stack and initialize
5557 // it's args_ field.
5558 Operand info_object = ApiParameterOperand(3);
5559 __ mov(info_object, scratch);
5560
5561 __ sub(scratch, Immediate(kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005562 __ mov(ApiParameterOperand(0), scratch); // name.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005563 __ lea(scratch, info_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005564 __ mov(ApiParameterOperand(1), scratch); // arguments pointer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005565 // Reserve space for optional callback address parameter.
5566 Operand thunk_last_arg = ApiParameterOperand(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005567
5568 ExternalReference thunk_ref =
5569 ExternalReference::invoke_accessor_getter_callback(isolate());
5570
Ben Murdoch097c5b22016-05-18 11:27:45 +01005571 // +3 is to skip prolog, return address and name handle.
5572 Operand return_value_operand(
5573 ebp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005574 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005575 thunk_last_arg, kStackUnwindSpace, nullptr,
5576 return_value_operand, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005577}
5578
5579
5580#undef __
5581
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005582} // namespace internal
5583} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005584
5585#endif // V8_TARGET_ARCH_X87