blob: 71adfd353110ebbdb7187e84d37831d1c2061314 [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
Ben Murdochda12d292016-06-02 14:46:10 +01007#include "src/code-stubs.h"
8#include "src/api-arguments.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/bits.h"
10#include "src/bootstrapper.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/codegen.h"
12#include "src/ic/handler-compiler.h"
13#include "src/ic/ic.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014#include "src/ic/stub-cache.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/isolate.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016#include "src/regexp/jsregexp.h"
17#include "src/regexp/regexp-macro-assembler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018#include "src/runtime/runtime.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000019#include "src/x87/code-stubs-x87.h"
20#include "src/x87/frames-x87.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021
22namespace v8 {
23namespace internal {
24
25
26static void InitializeArrayConstructorDescriptor(
27 Isolate* isolate, CodeStubDescriptor* descriptor,
28 int constant_stack_parameter_count) {
29 // register state
30 // eax -- number of arguments
31 // edi -- function
32 // ebx -- allocation site with elements kind
33 Address deopt_handler = Runtime::FunctionForId(
34 Runtime::kArrayConstructor)->entry;
35
36 if (constant_stack_parameter_count == 0) {
37 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
38 JS_FUNCTION_STUB_MODE);
39 } else {
40 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000042 }
43}
44
45
46static void InitializeInternalArrayConstructorDescriptor(
47 Isolate* isolate, CodeStubDescriptor* descriptor,
48 int constant_stack_parameter_count) {
49 // register state
50 // eax -- number of arguments
51 // edi -- constructor function
52 Address deopt_handler = Runtime::FunctionForId(
53 Runtime::kInternalArrayConstructor)->entry;
54
55 if (constant_stack_parameter_count == 0) {
56 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
57 JS_FUNCTION_STUB_MODE);
58 } else {
59 descriptor->Initialize(eax, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000060 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000061 }
62}
63
64
65void ArrayNoArgumentConstructorStub::InitializeDescriptor(
66 CodeStubDescriptor* descriptor) {
67 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
68}
69
70
71void ArraySingleArgumentConstructorStub::InitializeDescriptor(
72 CodeStubDescriptor* descriptor) {
73 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
74}
75
76
77void ArrayNArgumentsConstructorStub::InitializeDescriptor(
78 CodeStubDescriptor* descriptor) {
79 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
80}
81
82
83void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
84 CodeStubDescriptor* descriptor) {
85 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
86}
87
Ben Murdochda12d292016-06-02 14:46:10 +010088void FastArrayPushStub::InitializeDescriptor(CodeStubDescriptor* descriptor) {
89 Address deopt_handler = Runtime::FunctionForId(Runtime::kArrayPush)->entry;
90 descriptor->Initialize(eax, deopt_handler, -1, JS_FUNCTION_STUB_MODE);
91}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000092
93void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
94 CodeStubDescriptor* descriptor) {
95 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
96}
97
98
99void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
100 CodeStubDescriptor* descriptor) {
101 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
102}
103
104
105#define __ ACCESS_MASM(masm)
106
107
108void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
109 ExternalReference miss) {
110 // Update the static counter each time a new code stub is generated.
111 isolate()->counters()->code_stubs()->Increment();
112
113 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000114 int param_count = descriptor.GetRegisterParameterCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 {
116 // Call the runtime system in a fresh internal frame.
117 FrameScope scope(masm, StackFrame::INTERNAL);
118 DCHECK(param_count == 0 ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000119 eax.is(descriptor.GetRegisterParameter(param_count - 1)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000120 // Push arguments
121 for (int i = 0; i < param_count; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000122 __ push(descriptor.GetRegisterParameter(i));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000123 }
124 __ CallExternalReference(miss, param_count);
125 }
126
127 __ ret(0);
128}
129
130
131void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
132 // We don't allow a GC during a store buffer overflow so there is no need to
133 // store the registers in any particular way, but we do have to store and
134 // restore them.
135 __ pushad();
136 if (save_doubles()) {
137 // Save FPU stat in m108byte.
138 __ sub(esp, Immediate(108));
139 __ fnsave(Operand(esp, 0));
140 }
141 const int argument_count = 1;
142
143 AllowExternalCallThatCantCauseGC scope(masm);
144 __ PrepareCallCFunction(argument_count, ecx);
145 __ mov(Operand(esp, 0 * kPointerSize),
146 Immediate(ExternalReference::isolate_address(isolate())));
147 __ CallCFunction(
148 ExternalReference::store_buffer_overflow_function(isolate()),
149 argument_count);
150 if (save_doubles()) {
151 // Restore FPU stat in m108byte.
152 __ frstor(Operand(esp, 0));
153 __ add(esp, Immediate(108));
154 }
155 __ popad();
156 __ ret(0);
157}
158
159
160class FloatingPointHelper : public AllStatic {
161 public:
162 enum ArgLocation {
163 ARGS_ON_STACK,
164 ARGS_IN_REGISTERS
165 };
166
167 // Code pattern for loading a floating point value. Input value must
168 // be either a smi or a heap number object (fp value). Requirements:
169 // operand in register number. Returns operand as floating point number
170 // on FPU stack.
171 static void LoadFloatOperand(MacroAssembler* masm, Register number);
172
173 // Test if operands are smi or number objects (fp). Requirements:
174 // operand_1 in eax, operand_2 in edx; falls through on float
175 // operands, jumps to the non_float label otherwise.
176 static void CheckFloatOperands(MacroAssembler* masm,
177 Label* non_float,
178 Register scratch);
179};
180
181
182void DoubleToIStub::Generate(MacroAssembler* masm) {
183 Register input_reg = this->source();
184 Register final_result_reg = this->destination();
185 DCHECK(is_truncating());
186
187 Label check_negative, process_64_bits, done, done_no_stash;
188
189 int double_offset = offset();
190
191 // Account for return address and saved regs if input is esp.
192 if (input_reg.is(esp)) double_offset += 3 * kPointerSize;
193
194 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
195 MemOperand exponent_operand(MemOperand(input_reg,
196 double_offset + kDoubleSize / 2));
197
198 Register scratch1;
199 {
200 Register scratch_candidates[3] = { ebx, edx, edi };
201 for (int i = 0; i < 3; i++) {
202 scratch1 = scratch_candidates[i];
203 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
204 }
205 }
206 // Since we must use ecx for shifts below, use some other register (eax)
207 // to calculate the result if ecx is the requested return register.
208 Register result_reg = final_result_reg.is(ecx) ? eax : final_result_reg;
209 // Save ecx if it isn't the return register and therefore volatile, or if it
210 // is the return register, then save the temp register we use in its stead for
211 // the result.
212 Register save_reg = final_result_reg.is(ecx) ? eax : ecx;
213 __ push(scratch1);
214 __ push(save_reg);
215
216 bool stash_exponent_copy = !input_reg.is(esp);
217 __ mov(scratch1, mantissa_operand);
218 __ mov(ecx, exponent_operand);
219 if (stash_exponent_copy) __ push(ecx);
220
221 __ and_(ecx, HeapNumber::kExponentMask);
222 __ shr(ecx, HeapNumber::kExponentShift);
223 __ lea(result_reg, MemOperand(ecx, -HeapNumber::kExponentBias));
224 __ cmp(result_reg, Immediate(HeapNumber::kMantissaBits));
225 __ j(below, &process_64_bits);
226
227 // Result is entirely in lower 32-bits of mantissa
228 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
229 __ sub(ecx, Immediate(delta));
230 __ xor_(result_reg, result_reg);
231 __ cmp(ecx, Immediate(31));
232 __ j(above, &done);
233 __ shl_cl(scratch1);
234 __ jmp(&check_negative);
235
236 __ bind(&process_64_bits);
237 // Result must be extracted from shifted 32-bit mantissa
238 __ sub(ecx, Immediate(delta));
239 __ neg(ecx);
240 if (stash_exponent_copy) {
241 __ mov(result_reg, MemOperand(esp, 0));
242 } else {
243 __ mov(result_reg, exponent_operand);
244 }
245 __ and_(result_reg,
246 Immediate(static_cast<uint32_t>(Double::kSignificandMask >> 32)));
247 __ add(result_reg,
248 Immediate(static_cast<uint32_t>(Double::kHiddenBit >> 32)));
Ben Murdochda12d292016-06-02 14:46:10 +0100249 __ shrd_cl(scratch1, result_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000250 __ shr_cl(result_reg);
251 __ test(ecx, Immediate(32));
252 {
253 Label skip_mov;
254 __ j(equal, &skip_mov, Label::kNear);
255 __ mov(scratch1, result_reg);
256 __ bind(&skip_mov);
257 }
258
259 // If the double was negative, negate the integer result.
260 __ bind(&check_negative);
261 __ mov(result_reg, scratch1);
262 __ neg(result_reg);
263 if (stash_exponent_copy) {
264 __ cmp(MemOperand(esp, 0), Immediate(0));
265 } else {
266 __ cmp(exponent_operand, Immediate(0));
267 }
268 {
269 Label skip_mov;
270 __ j(less_equal, &skip_mov, Label::kNear);
271 __ mov(result_reg, scratch1);
272 __ bind(&skip_mov);
273 }
274
275 // Restore registers
276 __ bind(&done);
277 if (stash_exponent_copy) {
278 __ add(esp, Immediate(kDoubleSize / 2));
279 }
280 __ bind(&done_no_stash);
281 if (!final_result_reg.is(result_reg)) {
282 DCHECK(final_result_reg.is(ecx));
283 __ mov(final_result_reg, result_reg);
284 }
285 __ pop(save_reg);
286 __ pop(scratch1);
287 __ ret(0);
288}
289
290
291void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
292 Register number) {
293 Label load_smi, done;
294
295 __ JumpIfSmi(number, &load_smi, Label::kNear);
296 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
297 __ jmp(&done, Label::kNear);
298
299 __ bind(&load_smi);
300 __ SmiUntag(number);
301 __ push(number);
302 __ fild_s(Operand(esp, 0));
303 __ pop(number);
304
305 __ bind(&done);
306}
307
308
309void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
310 Label* non_float,
311 Register scratch) {
312 Label test_other, done;
313 // Test if both operands are floats or smi -> scratch=k_is_float;
314 // Otherwise scratch = k_not_float.
315 __ JumpIfSmi(edx, &test_other, Label::kNear);
316 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
317 Factory* factory = masm->isolate()->factory();
318 __ cmp(scratch, factory->heap_number_map());
319 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
320
321 __ bind(&test_other);
322 __ JumpIfSmi(eax, &done, Label::kNear);
323 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
324 __ cmp(scratch, factory->heap_number_map());
325 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
326
327 // Fall-through: Both operands are numbers.
328 __ bind(&done);
329}
330
331
332void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000333 const Register base = edx;
334 const Register scratch = ecx;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000335 Label call_runtime;
336
337 // We will call runtime helper function directly.
338 if (exponent_type() == ON_STACK) {
339 // The arguments are still on the stack.
340 __ bind(&call_runtime);
341 __ TailCallRuntime(Runtime::kMathPowRT);
342
343 // The stub is called from non-optimized code, which expects the result
344 // as heap number in exponent.
345 __ AllocateHeapNumber(eax, scratch, base, &call_runtime);
346 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000347 __ ret(2 * kPointerSize);
348 } else {
349 // Currently it's only called from full-compiler and exponent type is
350 // ON_STACK.
351 UNIMPLEMENTED();
352 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000353}
354
355
356void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
357 Label miss;
358 Register receiver = LoadDescriptor::ReceiverRegister();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000359 // With careful management, we won't have to save slot and vector on
360 // the stack. Simply handle the possibly missing case first.
361 // TODO(mvstanton): this code can be more efficient.
362 __ cmp(FieldOperand(receiver, JSFunction::kPrototypeOrInitialMapOffset),
363 Immediate(isolate()->factory()->the_hole_value()));
364 __ j(equal, &miss);
365 __ TryGetFunctionPrototype(receiver, eax, ebx, &miss);
366 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000367
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000368 __ bind(&miss);
369 PropertyAccessCompiler::TailCallBuiltin(
370 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
371}
372
373
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400374void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
375 // Return address is on the stack.
376 Label miss;
377
378 Register receiver = LoadDescriptor::ReceiverRegister();
379 Register index = LoadDescriptor::NameRegister();
380 Register scratch = edi;
381 DCHECK(!scratch.is(receiver) && !scratch.is(index));
382 Register result = eax;
383 DCHECK(!result.is(scratch));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000384 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
385 result.is(LoadDescriptor::SlotRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400386
387 // StringCharAtGenerator doesn't use the result register until it's passed
388 // the different miss possibilities. If it did, we would have a conflict
389 // when FLAG_vector_ics is true.
390
391 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
392 &miss, // When not a string.
393 &miss, // When not a number.
394 &miss, // When index out of range.
395 STRING_INDEX_IS_ARRAY_INDEX,
396 RECEIVER_IS_STRING);
397 char_at_generator.GenerateFast(masm);
398 __ ret(0);
399
400 StubRuntimeCallHelper call_helper;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000401 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400402
403 __ bind(&miss);
404 PropertyAccessCompiler::TailCallBuiltin(
405 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
406}
407
408
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409void RegExpExecStub::Generate(MacroAssembler* masm) {
410 // Just jump directly to runtime if native RegExp is not selected at compile
411 // time or if regexp entry in generated code is turned off runtime switch or
412 // at compilation.
413#ifdef V8_INTERPRETED_REGEXP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000414 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415#else // V8_INTERPRETED_REGEXP
416
417 // Stack frame on entry.
418 // esp[0]: return address
419 // esp[4]: last_match_info (expected JSArray)
420 // esp[8]: previous index
421 // esp[12]: subject string
422 // esp[16]: JSRegExp object
423
424 static const int kLastMatchInfoOffset = 1 * kPointerSize;
425 static const int kPreviousIndexOffset = 2 * kPointerSize;
426 static const int kSubjectOffset = 3 * kPointerSize;
427 static const int kJSRegExpOffset = 4 * kPointerSize;
428
429 Label runtime;
430 Factory* factory = isolate()->factory();
431
432 // Ensure that a RegExp stack is allocated.
433 ExternalReference address_of_regexp_stack_memory_address =
434 ExternalReference::address_of_regexp_stack_memory_address(isolate());
435 ExternalReference address_of_regexp_stack_memory_size =
436 ExternalReference::address_of_regexp_stack_memory_size(isolate());
437 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
438 __ test(ebx, ebx);
439 __ j(zero, &runtime);
440
441 // Check that the first argument is a JSRegExp object.
442 __ mov(eax, Operand(esp, kJSRegExpOffset));
443 STATIC_ASSERT(kSmiTag == 0);
444 __ JumpIfSmi(eax, &runtime);
445 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
446 __ j(not_equal, &runtime);
447
448 // Check that the RegExp has been compiled (data contains a fixed array).
449 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
450 if (FLAG_debug_code) {
451 __ test(ecx, Immediate(kSmiTagMask));
452 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
453 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
454 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
455 }
456
457 // ecx: RegExp data (FixedArray)
458 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
459 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
460 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
461 __ j(not_equal, &runtime);
462
463 // ecx: RegExp data (FixedArray)
464 // Check that the number of captures fit in the static offsets vector buffer.
465 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
466 // Check (number_of_captures + 1) * 2 <= offsets vector size
467 // Or number_of_captures * 2 <= offsets vector size - 2
468 // Multiplying by 2 comes for free since edx is smi-tagged.
469 STATIC_ASSERT(kSmiTag == 0);
470 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
471 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
472 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
473 __ j(above, &runtime);
474
475 // Reset offset for possibly sliced string.
476 __ Move(edi, Immediate(0));
477 __ mov(eax, Operand(esp, kSubjectOffset));
478 __ JumpIfSmi(eax, &runtime);
479 __ mov(edx, eax); // Make a copy of the original subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480
481 // eax: subject string
482 // edx: subject string
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000483 // ecx: RegExp data (FixedArray)
484 // Handle subject string according to its encoding and representation:
485 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch097c5b22016-05-18 11:27:45 +0100486 // (2) Sequential one byte? If yes, go to (5).
487 // (3) Sequential or cons? If not, go to (6).
488 // (4) Cons string. If the string is flat, replace subject with first string
489 // and go to (1). Otherwise bail out to runtime.
490 // (5) One byte sequential. Load regexp code for one byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000491 // (E) Carry on.
492 /// [...]
493
494 // Deferred code at the end of the stub:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100495 // (6) Long external string? If not, go to (10).
496 // (7) External string. Make it, offset-wise, look like a sequential string.
497 // (8) Is the external string one byte? If yes, go to (5).
498 // (9) Two byte sequential. Load regexp code for two byte. Go to (E).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000499 // (10) Short external string or not a string? If yes, bail out to runtime.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100500 // (11) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000501
Ben Murdoch097c5b22016-05-18 11:27:45 +0100502 Label seq_one_byte_string /* 5 */, seq_two_byte_string /* 9 */,
503 external_string /* 7 */, check_underlying /* 1 */,
504 not_seq_nor_cons /* 6 */, check_code /* E */, not_long_external /* 10 */;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000505
Ben Murdoch097c5b22016-05-18 11:27:45 +0100506 __ bind(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000507 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch097c5b22016-05-18 11:27:45 +0100508 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
509 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
510
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000511 __ and_(ebx, kIsNotStringMask |
512 kStringRepresentationMask |
513 kStringEncodingMask |
514 kShortExternalStringMask);
515 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
516 __ j(zero, &seq_two_byte_string); // Go to (9).
517
Ben Murdoch097c5b22016-05-18 11:27:45 +0100518 // (2) Sequential one byte? If yes, go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000519 // Any other sequential string must be one byte.
520 __ and_(ebx, Immediate(kIsNotStringMask |
521 kStringRepresentationMask |
522 kShortExternalStringMask));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100523 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524
Ben Murdoch097c5b22016-05-18 11:27:45 +0100525 // (3) Sequential or cons? If not, go to (6).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000526 // We check whether the subject string is a cons, since sequential strings
527 // have already been covered.
528 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
529 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
530 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
531 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
532 __ cmp(ebx, Immediate(kExternalStringTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100533 __ j(greater_equal, &not_seq_nor_cons); // Go to (6).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000534
535 // (4) Cons string. Check that it's flat.
536 // Replace subject with first string and reload instance type.
537 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
538 __ j(not_equal, &runtime);
539 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100540 __ jmp(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000541
542 // eax: sequential subject string (or look-alike, external string)
543 // edx: original subject string
544 // ecx: RegExp data (FixedArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100545 // (5) One byte sequential. Load regexp code for one byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000546 __ bind(&seq_one_byte_string);
547 // Load previous index and check range before edx is overwritten. We have
548 // to use edx instead of eax here because it might have been only made to
549 // look like a sequential string when it actually is an external string.
550 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
551 __ JumpIfNotSmi(ebx, &runtime);
552 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
553 __ j(above_equal, &runtime);
554 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataOneByteCodeOffset));
555 __ Move(ecx, Immediate(1)); // Type is one byte.
556
557 // (E) Carry on. String handling is done.
558 __ bind(&check_code);
559 // edx: irregexp code
560 // Check that the irregexp code has been generated for the actual string
561 // encoding. If it has, the field contains a code object otherwise it contains
562 // a smi (code flushing support).
563 __ JumpIfSmi(edx, &runtime);
564
565 // eax: subject string
566 // ebx: previous index (smi)
567 // edx: code
568 // ecx: encoding of subject string (1 if one_byte, 0 if two_byte);
569 // All checks done. Now push arguments for native regexp code.
570 Counters* counters = isolate()->counters();
571 __ IncrementCounter(counters->regexp_entry_native(), 1);
572
573 // Isolates: note we add an additional parameter here (isolate pointer).
574 static const int kRegExpExecuteArguments = 9;
575 __ EnterApiExitFrame(kRegExpExecuteArguments);
576
577 // Argument 9: Pass current isolate address.
578 __ mov(Operand(esp, 8 * kPointerSize),
579 Immediate(ExternalReference::isolate_address(isolate())));
580
581 // Argument 8: Indicate that this is a direct call from JavaScript.
582 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
583
584 // Argument 7: Start (high end) of backtracking stack memory area.
585 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
586 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
587 __ mov(Operand(esp, 6 * kPointerSize), esi);
588
589 // Argument 6: Set the number of capture registers to zero to force global
590 // regexps to behave as non-global. This does not affect non-global regexps.
591 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
592
593 // Argument 5: static offsets vector buffer.
594 __ mov(Operand(esp, 4 * kPointerSize),
595 Immediate(ExternalReference::address_of_static_offsets_vector(
596 isolate())));
597
598 // Argument 2: Previous index.
599 __ SmiUntag(ebx);
600 __ mov(Operand(esp, 1 * kPointerSize), ebx);
601
602 // Argument 1: Original subject string.
603 // The original subject is in the previous stack frame. Therefore we have to
604 // use ebp, which points exactly to one pointer size below the previous esp.
605 // (Because creating a new stack frame pushes the previous ebp onto the stack
606 // and thereby moves up esp by one kPointerSize.)
607 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
608 __ mov(Operand(esp, 0 * kPointerSize), esi);
609
610 // esi: original subject string
611 // eax: underlying subject string
612 // ebx: previous index
613 // ecx: encoding of subject string (1 if one_byte 0 if two_byte);
614 // edx: code
615 // Argument 4: End of string data
616 // Argument 3: Start of string data
617 // Prepare start and end index of the input.
618 // Load the length from the original sliced string if that is the case.
619 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
620 __ add(esi, edi); // Calculate input end wrt offset.
621 __ SmiUntag(edi);
622 __ add(ebx, edi); // Calculate input start wrt offset.
623
624 // ebx: start index of the input string
625 // esi: end index of the input string
626 Label setup_two_byte, setup_rest;
627 __ test(ecx, ecx);
628 __ j(zero, &setup_two_byte, Label::kNear);
629 __ SmiUntag(esi);
630 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
631 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
632 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
633 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
634 __ jmp(&setup_rest, Label::kNear);
635
636 __ bind(&setup_two_byte);
637 STATIC_ASSERT(kSmiTag == 0);
638 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
639 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
640 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
641 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
642 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
643
644 __ bind(&setup_rest);
645
646 // Locate the code entry and call it.
647 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
648 __ call(edx);
649
650 // Drop arguments and come back to JS mode.
651 __ LeaveApiExitFrame(true);
652
653 // Check the result.
654 Label success;
655 __ cmp(eax, 1);
656 // We expect exactly one result since we force the called regexp to behave
657 // as non-global.
658 __ j(equal, &success);
659 Label failure;
660 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
661 __ j(equal, &failure);
662 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
663 // If not exception it can only be retry. Handle that in the runtime system.
664 __ j(not_equal, &runtime);
665 // Result must now be exception. If there is no pending exception already a
666 // stack overflow (on the backtrack stack) was detected in RegExp code but
667 // haven't created the exception yet. Handle that in the runtime system.
668 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
669 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
670 isolate());
671 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
672 __ mov(eax, Operand::StaticVariable(pending_exception));
673 __ cmp(edx, eax);
674 __ j(equal, &runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000675
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000676 // For exception, throw the exception again.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000677 __ TailCallRuntime(Runtime::kRegExpExecReThrow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678
679 __ bind(&failure);
680 // For failure to match, return null.
681 __ mov(eax, factory->null_value());
682 __ ret(4 * kPointerSize);
683
684 // Load RegExp data.
685 __ bind(&success);
686 __ mov(eax, Operand(esp, kJSRegExpOffset));
687 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
688 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
689 // Calculate number of capture registers (number_of_captures + 1) * 2.
690 STATIC_ASSERT(kSmiTag == 0);
691 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
692 __ add(edx, Immediate(2)); // edx was a smi.
693
694 // edx: Number of capture registers
695 // Load last_match_info which is still known to be a fast case JSArray.
696 // Check that the fourth object is a JSArray object.
697 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
698 __ JumpIfSmi(eax, &runtime);
699 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
700 __ j(not_equal, &runtime);
701 // Check that the JSArray is in fast case.
702 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
703 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
704 __ cmp(eax, factory->fixed_array_map());
705 __ j(not_equal, &runtime);
706 // Check that the last match info has space for the capture registers and the
707 // additional information.
708 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
709 __ SmiUntag(eax);
710 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
711 __ cmp(edx, eax);
712 __ j(greater, &runtime);
713
714 // ebx: last_match_info backing store (FixedArray)
715 // edx: number of capture registers
716 // Store the capture count.
717 __ SmiTag(edx); // Number of capture registers to smi.
718 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
719 __ SmiUntag(edx); // Number of capture registers back from smi.
720 // Store last subject and last input.
721 __ mov(eax, Operand(esp, kSubjectOffset));
722 __ mov(ecx, eax);
723 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
724 __ RecordWriteField(ebx, RegExpImpl::kLastSubjectOffset, eax, edi,
725 kDontSaveFPRegs);
726 __ mov(eax, ecx);
727 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
728 __ RecordWriteField(ebx, RegExpImpl::kLastInputOffset, eax, edi,
729 kDontSaveFPRegs);
730
731 // Get the static offsets vector filled by the native regexp code.
732 ExternalReference address_of_static_offsets_vector =
733 ExternalReference::address_of_static_offsets_vector(isolate());
734 __ mov(ecx, Immediate(address_of_static_offsets_vector));
735
736 // ebx: last_match_info backing store (FixedArray)
737 // ecx: offsets vector
738 // edx: number of capture registers
739 Label next_capture, done;
740 // Capture register counter starts from number of capture registers and
741 // counts down until wraping after zero.
742 __ bind(&next_capture);
743 __ sub(edx, Immediate(1));
744 __ j(negative, &done, Label::kNear);
745 // Read the value from the static offsets vector buffer.
746 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
747 __ SmiTag(edi);
748 // Store the smi value in the last match info.
749 __ mov(FieldOperand(ebx,
750 edx,
751 times_pointer_size,
752 RegExpImpl::kFirstCaptureOffset),
753 edi);
754 __ jmp(&next_capture);
755 __ bind(&done);
756
757 // Return last match info.
758 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
759 __ ret(4 * kPointerSize);
760
761 // Do the runtime call to execute the regexp.
762 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000763 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000764
765 // Deferred code for string handling.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100766 // (6) Long external string? If not, go to (10).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000767 __ bind(&not_seq_nor_cons);
768 // Compare flags are still set from (3).
769 __ j(greater, &not_long_external, Label::kNear); // Go to (10).
770
Ben Murdoch097c5b22016-05-18 11:27:45 +0100771 // (7) External string. Short external strings have been ruled out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000772 __ bind(&external_string);
773 // Reload instance type.
774 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
775 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
776 if (FLAG_debug_code) {
777 // Assert that we do not have a cons or slice (indirect strings) here.
778 // Sequential strings have already been ruled out.
Ben Murdochda12d292016-06-02 14:46:10 +0100779 __ test_b(ebx, Immediate(kIsIndirectStringMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000780 __ Assert(zero, kExternalStringExpectedButNotFound);
781 }
782 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
783 // Move the pointer so that offset-wise, it looks like a sequential string.
784 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
785 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
786 STATIC_ASSERT(kTwoByteStringTag == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100787 // (8) Is the external string one byte? If yes, go to (5).
Ben Murdochda12d292016-06-02 14:46:10 +0100788 __ test_b(ebx, Immediate(kStringEncodingMask));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100789 __ j(not_zero, &seq_one_byte_string); // Go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790
791 // eax: sequential subject string (or look-alike, external string)
792 // edx: original subject string
793 // ecx: RegExp data (FixedArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100794 // (9) Two byte sequential. Load regexp code for two byte. Go to (E).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000795 __ bind(&seq_two_byte_string);
796 // Load previous index and check range before edx is overwritten. We have
797 // to use edx instead of eax here because it might have been only made to
798 // look like a sequential string when it actually is an external string.
799 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
800 __ JumpIfNotSmi(ebx, &runtime);
801 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
802 __ j(above_equal, &runtime);
803 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
804 __ Move(ecx, Immediate(0)); // Type is two byte.
805 __ jmp(&check_code); // Go to (E).
806
807 // (10) Not a string or a short external string? If yes, bail out to runtime.
808 __ bind(&not_long_external);
809 // Catch non-string subject or short external string.
810 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
811 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
812 __ j(not_zero, &runtime);
813
Ben Murdoch097c5b22016-05-18 11:27:45 +0100814 // (11) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000815 // Load offset into edi and replace subject string with parent.
816 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
817 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100818 __ jmp(&check_underlying); // Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000819#endif // V8_INTERPRETED_REGEXP
820}
821
822
823static int NegativeComparisonResult(Condition cc) {
824 DCHECK(cc != equal);
825 DCHECK((cc == less) || (cc == less_equal)
826 || (cc == greater) || (cc == greater_equal));
827 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
828}
829
830
831static void CheckInputType(MacroAssembler* masm, Register input,
832 CompareICState::State expected, Label* fail) {
833 Label ok;
834 if (expected == CompareICState::SMI) {
835 __ JumpIfNotSmi(input, fail);
836 } else if (expected == CompareICState::NUMBER) {
837 __ JumpIfSmi(input, &ok);
838 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
839 Immediate(masm->isolate()->factory()->heap_number_map()));
840 __ j(not_equal, fail);
841 }
842 // We could be strict about internalized/non-internalized here, but as long as
843 // hydrogen doesn't care, the stub doesn't have to care either.
844 __ bind(&ok);
845}
846
847
848static void BranchIfNotInternalizedString(MacroAssembler* masm,
849 Label* label,
850 Register object,
851 Register scratch) {
852 __ JumpIfSmi(object, label);
853 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
854 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
855 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
856 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
857 __ j(not_zero, label);
858}
859
860
861void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000862 Label runtime_call, check_unequal_objects;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000863 Condition cc = GetCondition();
864
865 Label miss;
866 CheckInputType(masm, edx, left(), &miss);
867 CheckInputType(masm, eax, right(), &miss);
868
869 // Compare two smis.
870 Label non_smi, smi_done;
871 __ mov(ecx, edx);
872 __ or_(ecx, eax);
873 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
874 __ sub(edx, eax); // Return on the result of the subtraction.
875 __ j(no_overflow, &smi_done, Label::kNear);
876 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
877 __ bind(&smi_done);
878 __ mov(eax, edx);
879 __ ret(0);
880 __ bind(&non_smi);
881
882 // NOTICE! This code is only reached after a smi-fast-case check, so
883 // it is certain that at least one operand isn't a smi.
884
885 // Identical objects can be compared fast, but there are some tricky cases
886 // for NaN and undefined.
887 Label generic_heap_number_comparison;
888 {
889 Label not_identical;
890 __ cmp(eax, edx);
891 __ j(not_equal, &not_identical);
892
893 if (cc != equal) {
894 // Check for undefined. undefined OP undefined is false even though
895 // undefined == undefined.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 __ cmp(edx, isolate()->factory()->undefined_value());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100897 Label check_for_nan;
898 __ j(not_equal, &check_for_nan, Label::kNear);
899 __ Move(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
900 __ ret(0);
901 __ bind(&check_for_nan);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000902 }
903
904 // Test for NaN. Compare heap numbers in a general way,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000905 // to handle NaNs correctly.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000906 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
907 Immediate(isolate()->factory()->heap_number_map()));
908 __ j(equal, &generic_heap_number_comparison, Label::kNear);
909 if (cc != equal) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000910 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
911 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000912 // Call runtime on identical JSObjects. Otherwise return equal.
Ben Murdochda12d292016-06-02 14:46:10 +0100913 __ cmpb(ecx, Immediate(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000914 __ j(above_equal, &runtime_call, Label::kFar);
915 // Call runtime on identical symbols since we need to throw a TypeError.
Ben Murdochda12d292016-06-02 14:46:10 +0100916 __ cmpb(ecx, Immediate(SYMBOL_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000917 __ j(equal, &runtime_call, Label::kFar);
918 // Call runtime on identical SIMD values since we must throw a TypeError.
Ben Murdochda12d292016-06-02 14:46:10 +0100919 __ cmpb(ecx, Immediate(SIMD128_VALUE_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000920 __ j(equal, &runtime_call, Label::kFar);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000921 }
922 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
923 __ ret(0);
924
925
926 __ bind(&not_identical);
927 }
928
929 // Strict equality can quickly decide whether objects are equal.
930 // Non-strict object equality is slower, so it is handled later in the stub.
931 if (cc == equal && strict()) {
932 Label slow; // Fallthrough label.
933 Label not_smis;
934 // If we're doing a strict equality comparison, we don't have to do
935 // type conversion, so we generate code to do fast comparison for objects
936 // and oddballs. Non-smi numbers and strings still go through the usual
937 // slow-case code.
938 // If either is a Smi (we know that not both are), then they can only
939 // be equal if the other is a HeapNumber. If so, use the slow case.
940 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000941 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000942 __ mov(ecx, Immediate(kSmiTagMask));
943 __ and_(ecx, eax);
944 __ test(ecx, edx);
945 __ j(not_zero, &not_smis, Label::kNear);
946 // One operand is a smi.
947
948 // Check whether the non-smi is a heap number.
949 STATIC_ASSERT(kSmiTagMask == 1);
950 // ecx still holds eax & kSmiTag, which is either zero or one.
951 __ sub(ecx, Immediate(0x01));
952 __ mov(ebx, edx);
953 __ xor_(ebx, eax);
954 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
955 __ xor_(ebx, eax);
956 // if eax was smi, ebx is now edx, else eax.
957
958 // Check if the non-smi operand is a heap number.
959 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
960 Immediate(isolate()->factory()->heap_number_map()));
961 // If heap number, handle it in the slow case.
962 __ j(equal, &slow, Label::kNear);
963 // Return non-equal (ebx is not zero)
964 __ mov(eax, ebx);
965 __ ret(0);
966
967 __ bind(&not_smis);
968 // If either operand is a JSObject or an oddball value, then they are not
969 // equal since their pointers are different
970 // There is no test for undetectability in strict equality.
971
972 // Get the type of the first operand.
973 // If the first object is a JS object, we have done pointer comparison.
974 Label first_non_object;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000975 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
976 __ CmpObjectType(eax, FIRST_JS_RECEIVER_TYPE, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000977 __ j(below, &first_non_object, Label::kNear);
978
979 // Return non-zero (eax is not zero)
980 Label return_not_equal;
981 STATIC_ASSERT(kHeapObjectTag != 0);
982 __ bind(&return_not_equal);
983 __ ret(0);
984
985 __ bind(&first_non_object);
986 // Check for oddballs: true, false, null, undefined.
987 __ CmpInstanceType(ecx, ODDBALL_TYPE);
988 __ j(equal, &return_not_equal);
989
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000990 __ CmpObjectType(edx, FIRST_JS_RECEIVER_TYPE, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000991 __ j(above_equal, &return_not_equal);
992
993 // Check for oddballs: true, false, null, undefined.
994 __ CmpInstanceType(ecx, ODDBALL_TYPE);
995 __ j(equal, &return_not_equal);
996
997 // Fall through to the general case.
998 __ bind(&slow);
999 }
1000
1001 // Generate the number comparison code.
1002 Label non_number_comparison;
1003 Label unordered;
1004 __ bind(&generic_heap_number_comparison);
1005 FloatingPointHelper::CheckFloatOperands(
1006 masm, &non_number_comparison, ebx);
1007 FloatingPointHelper::LoadFloatOperand(masm, eax);
1008 FloatingPointHelper::LoadFloatOperand(masm, edx);
1009 __ FCmp();
1010
1011 // Don't base result on EFLAGS when a NaN is involved.
1012 __ j(parity_even, &unordered, Label::kNear);
1013
1014 Label below_label, above_label;
1015 // Return a result of -1, 0, or 1, based on EFLAGS.
1016 __ j(below, &below_label, Label::kNear);
1017 __ j(above, &above_label, Label::kNear);
1018
1019 __ Move(eax, Immediate(0));
1020 __ ret(0);
1021
1022 __ bind(&below_label);
1023 __ mov(eax, Immediate(Smi::FromInt(-1)));
1024 __ ret(0);
1025
1026 __ bind(&above_label);
1027 __ mov(eax, Immediate(Smi::FromInt(1)));
1028 __ ret(0);
1029
1030 // If one of the numbers was NaN, then the result is always false.
1031 // The cc is never not-equal.
1032 __ bind(&unordered);
1033 DCHECK(cc != not_equal);
1034 if (cc == less || cc == less_equal) {
1035 __ mov(eax, Immediate(Smi::FromInt(1)));
1036 } else {
1037 __ mov(eax, Immediate(Smi::FromInt(-1)));
1038 }
1039 __ ret(0);
1040
1041 // The number comparison code did not provide a valid result.
1042 __ bind(&non_number_comparison);
1043
1044 // Fast negative check for internalized-to-internalized equality.
1045 Label check_for_strings;
1046 if (cc == equal) {
1047 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
1048 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
1049
1050 // We've already checked for object identity, so if both operands
1051 // are internalized they aren't equal. Register eax already holds a
1052 // non-zero value, which indicates not equal, so just return.
1053 __ ret(0);
1054 }
1055
1056 __ bind(&check_for_strings);
1057
1058 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx,
1059 &check_unequal_objects);
1060
1061 // Inline comparison of one-byte strings.
1062 if (cc == equal) {
1063 StringHelper::GenerateFlatOneByteStringEquals(masm, edx, eax, ecx, ebx);
1064 } else {
1065 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
1066 edi);
1067 }
1068#ifdef DEBUG
1069 __ Abort(kUnexpectedFallThroughFromStringComparison);
1070#endif
1071
1072 __ bind(&check_unequal_objects);
1073 if (cc == equal && !strict()) {
1074 // Non-strict equality. Objects are unequal if
1075 // they are both JSObjects and not undetectable,
1076 // and their pointers are different.
Ben Murdochda12d292016-06-02 14:46:10 +01001077 Label return_equal, return_unequal, undetectable;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001078 // At most one is a smi, so we can test for smi by adding the two.
1079 // A smi plus a heap object has the low bit set, a heap object plus
1080 // a heap object has the low bit clear.
1081 STATIC_ASSERT(kSmiTag == 0);
1082 STATIC_ASSERT(kSmiTagMask == 1);
1083 __ lea(ecx, Operand(eax, edx, times_1, 0));
1084 __ test(ecx, Immediate(kSmiTagMask));
Ben Murdochda12d292016-06-02 14:46:10 +01001085 __ j(not_zero, &runtime_call);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001086
1087 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1088 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
1089
1090 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001091 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001092 __ j(not_zero, &undetectable, Label::kNear);
1093 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001094 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001095 __ j(not_zero, &return_unequal, Label::kNear);
1096
1097 __ CmpInstanceType(ebx, FIRST_JS_RECEIVER_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001098 __ j(below, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001099 __ CmpInstanceType(ecx, FIRST_JS_RECEIVER_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001100 __ j(below, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001101
1102 __ bind(&return_unequal);
1103 // Return non-equal by returning the non-zero object pointer in eax.
1104 __ ret(0); // eax, edx were pushed
1105
1106 __ bind(&undetectable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001108 Immediate(1 << Map::kIsUndetectable));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001109 __ j(zero, &return_unequal, Label::kNear);
Ben Murdochda12d292016-06-02 14:46:10 +01001110
1111 // If both sides are JSReceivers, then the result is false according to
1112 // the HTML specification, which says that only comparisons with null or
1113 // undefined are affected by special casing for document.all.
1114 __ CmpInstanceType(ebx, ODDBALL_TYPE);
1115 __ j(zero, &return_equal, Label::kNear);
1116 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1117 __ j(not_zero, &return_unequal, Label::kNear);
1118
1119 __ bind(&return_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001120 __ Move(eax, Immediate(EQUAL));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001121 __ ret(0); // eax, edx were pushed
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001122 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001123 __ bind(&runtime_call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001124
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001125 if (cc == equal) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001126 {
1127 FrameScope scope(masm, StackFrame::INTERNAL);
1128 __ Push(edx);
1129 __ Push(eax);
1130 __ CallRuntime(strict() ? Runtime::kStrictEqual : Runtime::kEqual);
1131 }
1132 // Turn true into 0 and false into some non-zero value.
1133 STATIC_ASSERT(EQUAL == 0);
1134 __ sub(eax, Immediate(isolate()->factory()->true_value()));
1135 __ Ret();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001136 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001137 // Push arguments below the return address.
1138 __ pop(ecx);
1139 __ push(edx);
1140 __ push(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001141 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001142
1143 // Restore return address on the stack.
1144 __ push(ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001145 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1146 // tagged as a small integer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001147 __ TailCallRuntime(Runtime::kCompare);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001148 }
1149
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001150 __ bind(&miss);
1151 GenerateMiss(masm);
1152}
1153
1154
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001155static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
1156 // eax : number of arguments to the construct function
1157 // ebx : feedback vector
1158 // edx : slot in feedback vector (Smi)
1159 // edi : the function to call
1160
1161 {
1162 FrameScope scope(masm, StackFrame::INTERNAL);
1163
1164 // Number-of-arguments register must be smi-tagged to call out.
1165 __ SmiTag(eax);
1166 __ push(eax);
1167 __ push(edi);
1168 __ push(edx);
1169 __ push(ebx);
1170
1171 __ CallStub(stub);
1172
1173 __ pop(ebx);
1174 __ pop(edx);
1175 __ pop(edi);
1176 __ pop(eax);
1177 __ SmiUntag(eax);
1178 }
1179}
1180
1181
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001182static void GenerateRecordCallTarget(MacroAssembler* masm) {
1183 // Cache the called function in a feedback vector slot. Cache states
1184 // are uninitialized, monomorphic (indicated by a JSFunction), and
1185 // megamorphic.
1186 // eax : number of arguments to the construct function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001187 // ebx : feedback vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001188 // edx : slot in feedback vector (Smi)
1189 // edi : the function to call
1190 Isolate* isolate = masm->isolate();
1191 Label initialize, done, miss, megamorphic, not_array_function;
1192
1193 // Load the cache state into ecx.
1194 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1195 FixedArray::kHeaderSize));
1196
1197 // A monomorphic cache hit or an already megamorphic state: invoke the
1198 // function without changing the state.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001199 // We don't know if ecx is a WeakCell or a Symbol, but it's harmless to read
1200 // at this position in a symbol (see static asserts in
1201 // type-feedback-vector.h).
1202 Label check_allocation_site;
1203 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001204 __ j(equal, &done, Label::kFar);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001205 __ CompareRoot(ecx, Heap::kmegamorphic_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001206 __ j(equal, &done, Label::kFar);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001207 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1208 Heap::kWeakCellMapRootIndex);
1209 __ j(not_equal, &check_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001211 // If the weak cell is cleared, we have a new chance to become monomorphic.
1212 __ JumpIfSmi(FieldOperand(ecx, WeakCell::kValueOffset), &initialize);
1213 __ jmp(&megamorphic);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001214
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001215 __ bind(&check_allocation_site);
1216 // If we came here, we need to see if we are the array function.
1217 // If we didn't have a matching function, and we didn't find the megamorph
1218 // sentinel, then we have in the slot either some other function or an
1219 // AllocationSite.
1220 __ CompareRoot(FieldOperand(ecx, 0), Heap::kAllocationSiteMapRootIndex);
1221 __ j(not_equal, &miss);
1222
1223 // Make sure the function is the Array() function
1224 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1225 __ cmp(edi, ecx);
1226 __ j(not_equal, &megamorphic);
1227 __ jmp(&done, Label::kFar);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001228
1229 __ bind(&miss);
1230
1231 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1232 // megamorphic.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001233 __ CompareRoot(ecx, Heap::kuninitialized_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001234 __ j(equal, &initialize);
1235 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1236 // write-barrier is needed.
1237 __ bind(&megamorphic);
1238 __ mov(
1239 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1240 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
1241 __ jmp(&done, Label::kFar);
1242
1243 // An uninitialized cache is patched with the function or sentinel to
1244 // indicate the ElementsKind if function is the Array constructor.
1245 __ bind(&initialize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001246 // Make sure the function is the Array() function
1247 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1248 __ cmp(edi, ecx);
1249 __ j(not_equal, &not_array_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001250
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001251 // The target function is the Array constructor,
1252 // Create an AllocationSite if we don't already have it, store it in the
1253 // slot.
1254 CreateAllocationSiteStub create_stub(isolate);
1255 CallStubInRecordCallTarget(masm, &create_stub);
1256 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001257
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001258 __ bind(&not_array_function);
1259 CreateWeakCellStub weak_cell_stub(isolate);
1260 CallStubInRecordCallTarget(masm, &weak_cell_stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001261 __ bind(&done);
1262}
1263
1264
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001265void CallConstructStub::Generate(MacroAssembler* masm) {
1266 // eax : number of arguments
1267 // ebx : feedback vector
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001268 // edx : slot in feedback vector (Smi, for RecordCallTarget)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001269 // edi : constructor function
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001270
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001271 Label non_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001272 // Check that function is not a smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001273 __ JumpIfSmi(edi, &non_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001274 // Check that function is a JSFunction.
1275 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001276 __ j(not_equal, &non_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001277
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001278 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001279
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001280 Label feedback_register_initialized;
1281 // Put the AllocationSite from the feedback vector into ebx, or undefined.
1282 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
1283 FixedArray::kHeaderSize));
1284 Handle<Map> allocation_site_map = isolate()->factory()->allocation_site_map();
1285 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
1286 __ j(equal, &feedback_register_initialized);
1287 __ mov(ebx, isolate()->factory()->undefined_value());
1288 __ bind(&feedback_register_initialized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001289
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001290 __ AssertUndefinedOrAllocationSite(ebx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001291
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001292 // Pass new target to construct stub.
1293 __ mov(edx, edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001294
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001295 // Tail call to the function-specific construct stub (still in the caller
1296 // context at this point).
1297 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1298 __ mov(ecx, FieldOperand(ecx, SharedFunctionInfo::kConstructStubOffset));
1299 __ lea(ecx, FieldOperand(ecx, Code::kHeaderSize));
1300 __ jmp(ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001301
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001302 __ bind(&non_function);
1303 __ mov(edx, edi);
1304 __ Jump(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001305}
1306
1307
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001308void CallICStub::HandleArrayCase(MacroAssembler* masm, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001309 // edi - function
1310 // edx - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001311 // ebx - vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1313 __ cmp(edi, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001314 __ j(not_equal, miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001315
1316 __ mov(eax, arg_count());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001317 // Reload ecx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001318 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1319 FixedArray::kHeaderSize));
1320
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001321 // Increment the call count for monomorphic function calls.
1322 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1323 FixedArray::kHeaderSize + kPointerSize),
1324 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001325
1326 __ mov(ebx, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001327 __ mov(edx, edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001328 ArrayConstructorStub stub(masm->isolate(), arg_count());
1329 __ TailCallStub(&stub);
1330
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001331 // Unreachable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001332}
1333
1334
1335void CallICStub::Generate(MacroAssembler* masm) {
1336 // edi - function
1337 // edx - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001338 // ebx - vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001339 Isolate* isolate = masm->isolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001340 Label extra_checks_or_miss, call, call_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001341 int argc = arg_count();
1342 ParameterCount actual(argc);
1343
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 // The checks. First, does edi match the recorded monomorphic target?
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001345 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1346 FixedArray::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001347
1348 // We don't know that we have a weak cell. We might have a private symbol
1349 // or an AllocationSite, but the memory is safe to examine.
1350 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
1351 // FixedArray.
1352 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
1353 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
1354 // computed, meaning that it can't appear to be a pointer. If the low bit is
1355 // 0, then hash is computed, but the 0 bit prevents the field from appearing
1356 // to be a pointer.
1357 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
1358 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
1359 WeakCell::kValueOffset &&
1360 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
1361
1362 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
1363 __ j(not_equal, &extra_checks_or_miss);
1364
1365 // The compare above could have been a SMI/SMI comparison. Guard against this
1366 // convincing us that we have a monomorphic JSFunction.
1367 __ JumpIfSmi(edi, &extra_checks_or_miss);
1368
1369 // Increment the call count for monomorphic function calls.
1370 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1371 FixedArray::kHeaderSize + kPointerSize),
1372 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
1373
1374 __ bind(&call_function);
1375 __ Set(eax, argc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001376 __ Jump(masm->isolate()->builtins()->CallFunction(convert_mode(),
1377 tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001378 RelocInfo::CODE_TARGET);
1379
1380 __ bind(&extra_checks_or_miss);
1381 Label uninitialized, miss, not_allocation_site;
1382
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001383 __ cmp(ecx, Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001384 __ j(equal, &call);
1385
1386 // Check if we have an allocation site.
1387 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1388 Heap::kAllocationSiteMapRootIndex);
1389 __ j(not_equal, &not_allocation_site);
1390
1391 // We have an allocation site.
1392 HandleArrayCase(masm, &miss);
1393
1394 __ bind(&not_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001395
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001396 // The following cases attempt to handle MISS cases without going to the
1397 // runtime.
1398 if (FLAG_trace_ic) {
1399 __ jmp(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001400 }
1401
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001402 __ cmp(ecx, Immediate(TypeFeedbackVector::UninitializedSentinel(isolate)));
1403 __ j(equal, &uninitialized);
1404
1405 // We are going megamorphic. If the feedback is a JSFunction, it is fine
1406 // to handle it here. More complex cases are dealt with in the runtime.
1407 __ AssertNotSmi(ecx);
1408 __ CmpObjectType(ecx, JS_FUNCTION_TYPE, ecx);
1409 __ j(not_equal, &miss);
1410 __ mov(
1411 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1412 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001413
1414 __ bind(&call);
1415 __ Set(eax, argc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001416 __ Jump(masm->isolate()->builtins()->Call(convert_mode(), tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001417 RelocInfo::CODE_TARGET);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001418
1419 __ bind(&uninitialized);
1420
1421 // We are going monomorphic, provided we actually have a JSFunction.
1422 __ JumpIfSmi(edi, &miss);
1423
1424 // Goto miss case if we do not have a function.
1425 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1426 __ j(not_equal, &miss);
1427
1428 // Make sure the function is not the Array() function, which requires special
1429 // behavior on MISS.
1430 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1431 __ cmp(edi, ecx);
1432 __ j(equal, &miss);
1433
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001434 // Make sure the function belongs to the same native context.
1435 __ mov(ecx, FieldOperand(edi, JSFunction::kContextOffset));
1436 __ mov(ecx, ContextOperand(ecx, Context::NATIVE_CONTEXT_INDEX));
1437 __ cmp(ecx, NativeContextOperand());
1438 __ j(not_equal, &miss);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001439
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001440 // Initialize the call counter.
1441 __ mov(FieldOperand(ebx, edx, times_half_pointer_size,
1442 FixedArray::kHeaderSize + kPointerSize),
1443 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001444
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001445 // Store the function. Use a stub since we need a frame for allocation.
1446 // ebx - vector
1447 // edx - slot
1448 // edi - function
1449 {
1450 FrameScope scope(masm, StackFrame::INTERNAL);
1451 CreateWeakCellStub create_stub(isolate);
1452 __ push(edi);
1453 __ CallStub(&create_stub);
1454 __ pop(edi);
1455 }
1456
1457 __ jmp(&call_function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001458
1459 // We are here because tracing is on or we encountered a MISS case we can't
1460 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001461 __ bind(&miss);
1462 GenerateMiss(masm);
1463
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001464 __ jmp(&call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001465
1466 // Unreachable
1467 __ int3();
1468}
1469
1470
1471void CallICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001472 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001473
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001474 // Push the function and feedback info.
1475 __ push(edi);
1476 __ push(ebx);
1477 __ push(edx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001478
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001479 // Call the entry.
1480 __ CallRuntime(Runtime::kCallIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001481
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001482 // Move result to edi and exit the internal frame.
1483 __ mov(edi, eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001484}
1485
1486
1487bool CEntryStub::NeedsImmovableCode() {
1488 return false;
1489}
1490
1491
1492void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
1493 CEntryStub::GenerateAheadOfTime(isolate);
1494 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
1495 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
1496 // It is important that the store buffer overflow stubs are generated first.
1497 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
1498 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001499 CreateWeakCellStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001500 BinaryOpICStub::GenerateAheadOfTime(isolate);
1501 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001502 StoreFastElementStub::GenerateAheadOfTime(isolate);
1503 TypeofStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001504}
1505
1506
1507void CodeStub::GenerateFPStubs(Isolate* isolate) {
1508 CEntryStub save_doubles(isolate, 1, kSaveFPRegs);
1509 // Stubs might already be in the snapshot, detect that and don't regenerate,
1510 // which would lead to code stub initialization state being messed up.
1511 Code* save_doubles_code;
1512 if (!save_doubles.FindCodeInCache(&save_doubles_code)) {
1513 save_doubles_code = *(save_doubles.GetCode());
1514 }
1515 isolate->set_fp_stubs_generated(true);
1516}
1517
1518
1519void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1520 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1521 stub.GetCode();
1522}
1523
1524
1525void CEntryStub::Generate(MacroAssembler* masm) {
1526 // eax: number of arguments including receiver
1527 // ebx: pointer to C function (C callee-saved)
1528 // ebp: frame pointer (restored after C call)
1529 // esp: stack pointer (restored after C call)
1530 // esi: current context (C callee-saved)
1531 // edi: JS function of the caller (C callee-saved)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001532 //
1533 // If argv_in_register():
1534 // ecx: pointer to the first argument
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001535
1536 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1537
Ben Murdoch097c5b22016-05-18 11:27:45 +01001538 // Reserve space on the stack for the three arguments passed to the call. If
1539 // result size is greater than can be returned in registers, also reserve
1540 // space for the hidden argument for the result location, and space for the
1541 // result itself.
1542 int arg_stack_space = result_size() < 3 ? 3 : 4 + result_size();
1543
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001544 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001545 if (argv_in_register()) {
1546 DCHECK(!save_doubles());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001547 __ EnterApiExitFrame(arg_stack_space);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001548
1549 // Move argc and argv into the correct registers.
1550 __ mov(esi, ecx);
1551 __ mov(edi, eax);
1552 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001553 __ EnterExitFrame(arg_stack_space, save_doubles());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001554 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001555
1556 // ebx: pointer to C function (C callee-saved)
1557 // ebp: frame pointer (restored after C call)
1558 // esp: stack pointer (restored after C call)
1559 // edi: number of arguments including receiver (C callee-saved)
1560 // esi: pointer to the first argument (C callee-saved)
1561
1562 // Result returned in eax, or eax+edx if result size is 2.
1563
1564 // Check stack alignment.
1565 if (FLAG_debug_code) {
1566 __ CheckStackAlignment();
1567 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001568 // Call C function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001569 if (result_size() <= 2) {
1570 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
1571 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
1572 __ mov(Operand(esp, 2 * kPointerSize),
1573 Immediate(ExternalReference::isolate_address(isolate())));
1574 } else {
1575 DCHECK_EQ(3, result_size());
1576 // Pass a pointer to the result location as the first argument.
1577 __ lea(eax, Operand(esp, 4 * kPointerSize));
1578 __ mov(Operand(esp, 0 * kPointerSize), eax);
1579 __ mov(Operand(esp, 1 * kPointerSize), edi); // argc.
1580 __ mov(Operand(esp, 2 * kPointerSize), esi); // argv.
1581 __ mov(Operand(esp, 3 * kPointerSize),
1582 Immediate(ExternalReference::isolate_address(isolate())));
1583 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001584 __ call(ebx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001585
1586 if (result_size() > 2) {
1587 DCHECK_EQ(3, result_size());
1588#ifndef _WIN32
1589 // Restore the "hidden" argument on the stack which was popped by caller.
1590 __ sub(esp, Immediate(kPointerSize));
1591#endif
1592 // Read result values stored on stack. Result is stored above the arguments.
1593 __ mov(kReturnRegister0, Operand(esp, 4 * kPointerSize));
1594 __ mov(kReturnRegister1, Operand(esp, 5 * kPointerSize));
1595 __ mov(kReturnRegister2, Operand(esp, 6 * kPointerSize));
1596 }
1597 // Result is in eax, edx:eax or edi:edx:eax - do not destroy these registers!
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001598
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001599 // Check result for exception sentinel.
1600 Label exception_returned;
1601 __ cmp(eax, isolate()->factory()->exception());
1602 __ j(equal, &exception_returned);
1603
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001604 // Check that there is no pending exception, otherwise we
1605 // should have returned the exception sentinel.
1606 if (FLAG_debug_code) {
1607 __ push(edx);
1608 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1609 Label okay;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001610 ExternalReference pending_exception_address(
1611 Isolate::kPendingExceptionAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001612 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
1613 // Cannot use check here as it attempts to generate call into runtime.
1614 __ j(equal, &okay, Label::kNear);
1615 __ int3();
1616 __ bind(&okay);
1617 __ pop(edx);
1618 }
1619
1620 // Exit the JavaScript to C++ exit frame.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001621 __ LeaveExitFrame(save_doubles(), !argv_in_register());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001622 __ ret(0);
1623
1624 // Handling of exception.
1625 __ bind(&exception_returned);
1626
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001627 ExternalReference pending_handler_context_address(
1628 Isolate::kPendingHandlerContextAddress, isolate());
1629 ExternalReference pending_handler_code_address(
1630 Isolate::kPendingHandlerCodeAddress, isolate());
1631 ExternalReference pending_handler_offset_address(
1632 Isolate::kPendingHandlerOffsetAddress, isolate());
1633 ExternalReference pending_handler_fp_address(
1634 Isolate::kPendingHandlerFPAddress, isolate());
1635 ExternalReference pending_handler_sp_address(
1636 Isolate::kPendingHandlerSPAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001637
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001638 // Ask the runtime for help to determine the handler. This will set eax to
1639 // contain the current pending exception, don't clobber it.
1640 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
1641 isolate());
1642 {
1643 FrameScope scope(masm, StackFrame::MANUAL);
1644 __ PrepareCallCFunction(3, eax);
1645 __ mov(Operand(esp, 0 * kPointerSize), Immediate(0)); // argc.
1646 __ mov(Operand(esp, 1 * kPointerSize), Immediate(0)); // argv.
1647 __ mov(Operand(esp, 2 * kPointerSize),
1648 Immediate(ExternalReference::isolate_address(isolate())));
1649 __ CallCFunction(find_handler, 3);
1650 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001651
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001652 // Retrieve the handler context, SP and FP.
1653 __ mov(esi, Operand::StaticVariable(pending_handler_context_address));
1654 __ mov(esp, Operand::StaticVariable(pending_handler_sp_address));
1655 __ mov(ebp, Operand::StaticVariable(pending_handler_fp_address));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001656
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001657 // If the handler is a JS frame, restore the context to the frame. Note that
1658 // the context will be set to (esi == 0) for non-JS frames.
1659 Label skip;
1660 __ test(esi, esi);
1661 __ j(zero, &skip, Label::kNear);
1662 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
1663 __ bind(&skip);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001665 // Compute the handler entry address and jump to it.
1666 __ mov(edi, Operand::StaticVariable(pending_handler_code_address));
1667 __ mov(edx, Operand::StaticVariable(pending_handler_offset_address));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001668 // Check whether it's a turbofanned exception handler code before jump to it.
1669 Label not_turbo;
1670 __ push(eax);
1671 __ mov(eax, Operand(edi, Code::kKindSpecificFlags1Offset - kHeapObjectTag));
1672 __ and_(eax, Immediate(1 << Code::kIsTurbofannedBit));
1673 __ j(zero, &not_turbo);
1674 __ fninit();
1675 __ fld1();
1676 __ bind(&not_turbo);
1677 __ pop(eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001678 __ lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
1679 __ jmp(edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001680}
1681
1682
1683void JSEntryStub::Generate(MacroAssembler* masm) {
1684 Label invoke, handler_entry, exit;
1685 Label not_outermost_js, not_outermost_js_2;
1686
1687 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1688
1689 // Set up frame.
1690 __ push(ebp);
1691 __ mov(ebp, esp);
1692
1693 // Push marker in two places.
1694 int marker = type();
Ben Murdochda12d292016-06-02 14:46:10 +01001695 __ push(Immediate(Smi::FromInt(marker))); // marker
1696 ExternalReference context_address(Isolate::kContextAddress, isolate());
1697 __ push(Operand::StaticVariable(context_address)); // context
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001698 // Save callee-saved registers (C calling conventions).
1699 __ push(edi);
1700 __ push(esi);
1701 __ push(ebx);
1702
1703 // Save copies of the top frame descriptor on the stack.
1704 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
1705 __ push(Operand::StaticVariable(c_entry_fp));
1706
1707 // If this is the outermost JS call, set js_entry_sp value.
1708 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
1709 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
1710 __ j(not_equal, &not_outermost_js, Label::kNear);
1711 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
1712 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1713 __ jmp(&invoke, Label::kNear);
1714 __ bind(&not_outermost_js);
1715 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
1716
1717 // Jump to a faked try block that does the invoke, with a faked catch
1718 // block that sets the pending exception.
1719 __ jmp(&invoke);
1720 __ bind(&handler_entry);
1721 handler_offset_ = handler_entry.pos();
1722 // Caught exception: Store result (exception) in the pending exception
1723 // field in the JSEnv and return a failure sentinel.
1724 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
1725 isolate());
1726 __ mov(Operand::StaticVariable(pending_exception), eax);
1727 __ mov(eax, Immediate(isolate()->factory()->exception()));
1728 __ jmp(&exit);
1729
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001730 // Invoke: Link this frame into the handler chain.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001731 __ bind(&invoke);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001732 __ PushStackHandler();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001733
1734 // Clear any pending exceptions.
1735 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1736 __ mov(Operand::StaticVariable(pending_exception), edx);
1737
1738 // Fake a receiver (NULL).
1739 __ push(Immediate(0)); // receiver
1740
1741 // Invoke the function by calling through JS entry trampoline builtin and
1742 // pop the faked function when we return. Notice that we cannot store a
1743 // reference to the trampoline code directly in this stub, because the
1744 // builtin stubs may not have been generated yet.
1745 if (type() == StackFrame::ENTRY_CONSTRUCT) {
1746 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
1747 isolate());
1748 __ mov(edx, Immediate(construct_entry));
1749 } else {
1750 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
1751 __ mov(edx, Immediate(entry));
1752 }
1753 __ mov(edx, Operand(edx, 0)); // deref address
1754 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
1755 __ call(edx);
1756
1757 // Unlink this frame from the handler chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001758 __ PopStackHandler();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001759
1760 __ bind(&exit);
1761 // Check if the current stack frame is marked as the outermost JS frame.
1762 __ pop(ebx);
1763 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1764 __ j(not_equal, &not_outermost_js_2);
1765 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
1766 __ bind(&not_outermost_js_2);
1767
1768 // Restore the top frame descriptor from the stack.
1769 __ pop(Operand::StaticVariable(ExternalReference(
1770 Isolate::kCEntryFPAddress, isolate())));
1771
1772 // Restore callee-saved registers (C calling conventions).
1773 __ pop(ebx);
1774 __ pop(esi);
1775 __ pop(edi);
1776 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
1777
1778 // Restore frame pointer and return.
1779 __ pop(ebp);
1780 __ ret(0);
1781}
1782
1783
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001784void InstanceOfStub::Generate(MacroAssembler* masm) {
1785 Register const object = edx; // Object (lhs).
1786 Register const function = eax; // Function (rhs).
1787 Register const object_map = ecx; // Map of {object}.
1788 Register const function_map = ebx; // Map of {function}.
1789 Register const function_prototype = function_map; // Prototype of {function}.
1790 Register const scratch = edi;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001791
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001792 DCHECK(object.is(InstanceOfDescriptor::LeftRegister()));
1793 DCHECK(function.is(InstanceOfDescriptor::RightRegister()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001794
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001795 // Check if {object} is a smi.
1796 Label object_is_smi;
1797 __ JumpIfSmi(object, &object_is_smi, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001798
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001799 // Lookup the {function} and the {object} map in the global instanceof cache.
1800 // Note: This is safe because we clear the global instanceof cache whenever
1801 // we change the prototype of any object.
1802 Label fast_case, slow_case;
1803 __ mov(object_map, FieldOperand(object, HeapObject::kMapOffset));
1804 __ CompareRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
1805 __ j(not_equal, &fast_case, Label::kNear);
1806 __ CompareRoot(object_map, scratch, Heap::kInstanceofCacheMapRootIndex);
1807 __ j(not_equal, &fast_case, Label::kNear);
1808 __ LoadRoot(eax, Heap::kInstanceofCacheAnswerRootIndex);
1809 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001810
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001811 // If {object} is a smi we can safely return false if {function} is a JS
1812 // function, otherwise we have to miss to the runtime and throw an exception.
1813 __ bind(&object_is_smi);
1814 __ JumpIfSmi(function, &slow_case);
1815 __ CmpObjectType(function, JS_FUNCTION_TYPE, function_map);
1816 __ j(not_equal, &slow_case);
1817 __ LoadRoot(eax, Heap::kFalseValueRootIndex);
1818 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001819
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001820 // Fast-case: The {function} must be a valid JSFunction.
1821 __ bind(&fast_case);
1822 __ JumpIfSmi(function, &slow_case);
1823 __ CmpObjectType(function, JS_FUNCTION_TYPE, function_map);
1824 __ j(not_equal, &slow_case);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001825
Ben Murdochda12d292016-06-02 14:46:10 +01001826 // Go to the runtime if the function is not a constructor.
1827 __ test_b(FieldOperand(function_map, Map::kBitFieldOffset),
1828 Immediate(1 << Map::kIsConstructor));
1829 __ j(zero, &slow_case);
1830
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001831 // Ensure that {function} has an instance prototype.
1832 __ test_b(FieldOperand(function_map, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001833 Immediate(1 << Map::kHasNonInstancePrototype));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001834 __ j(not_zero, &slow_case);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001835
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001836 // Get the "prototype" (or initial map) of the {function}.
1837 __ mov(function_prototype,
1838 FieldOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1839 __ AssertNotSmi(function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001840
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001841 // Resolve the prototype if the {function} has an initial map. Afterwards the
1842 // {function_prototype} will be either the JSReceiver prototype object or the
1843 // hole value, which means that no instances of the {function} were created so
1844 // far and hence we should return false.
1845 Label function_prototype_valid;
1846 Register const function_prototype_map = scratch;
1847 __ CmpObjectType(function_prototype, MAP_TYPE, function_prototype_map);
1848 __ j(not_equal, &function_prototype_valid, Label::kNear);
1849 __ mov(function_prototype,
1850 FieldOperand(function_prototype, Map::kPrototypeOffset));
1851 __ bind(&function_prototype_valid);
1852 __ AssertNotSmi(function_prototype);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001853
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001854 // Update the global instanceof cache with the current {object} map and
1855 // {function}. The cached answer will be set when it is known below.
1856 __ StoreRoot(function, scratch, Heap::kInstanceofCacheFunctionRootIndex);
1857 __ StoreRoot(object_map, scratch, Heap::kInstanceofCacheMapRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001858
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001859 // Loop through the prototype chain looking for the {function} prototype.
1860 // Assume true, and change to false if not found.
1861 Label done, loop, fast_runtime_fallback;
1862 __ mov(eax, isolate()->factory()->true_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001863 __ bind(&loop);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001864
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001865 // Check if the object needs to be access checked.
1866 __ test_b(FieldOperand(object_map, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001867 Immediate(1 << Map::kIsAccessCheckNeeded));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001868 __ j(not_zero, &fast_runtime_fallback, Label::kNear);
1869 // Check if the current object is a Proxy.
1870 __ CmpInstanceType(object_map, JS_PROXY_TYPE);
1871 __ j(equal, &fast_runtime_fallback, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001872
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001873 __ mov(object, FieldOperand(object_map, Map::kPrototypeOffset));
1874 __ cmp(object, function_prototype);
1875 __ j(equal, &done, Label::kNear);
1876 __ mov(object_map, FieldOperand(object, HeapObject::kMapOffset));
1877 __ cmp(object, isolate()->factory()->null_value());
1878 __ j(not_equal, &loop);
1879 __ mov(eax, isolate()->factory()->false_value());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001880
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001881 __ bind(&done);
1882 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheAnswerRootIndex);
1883 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001884
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001885 // Found Proxy or access check needed: Call the runtime.
1886 __ bind(&fast_runtime_fallback);
1887 __ PopReturnAddressTo(scratch);
1888 __ Push(object);
1889 __ Push(function_prototype);
1890 __ PushReturnAddressFrom(scratch);
1891 // Invalidate the instanceof cache.
1892 __ Move(eax, Immediate(Smi::FromInt(0)));
1893 __ StoreRoot(eax, scratch, Heap::kInstanceofCacheFunctionRootIndex);
1894 __ TailCallRuntime(Runtime::kHasInPrototypeChain);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001895
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001896 // Slow-case: Call the %InstanceOf runtime function.
1897 __ bind(&slow_case);
1898 __ PopReturnAddressTo(scratch);
1899 __ Push(object);
1900 __ Push(function);
1901 __ PushReturnAddressFrom(scratch);
Ben Murdochda12d292016-06-02 14:46:10 +01001902 __ TailCallRuntime(is_es6_instanceof() ? Runtime::kOrdinaryHasInstance
1903 : Runtime::kInstanceOf);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001904}
1905
1906
1907// -------------------------------------------------------------------------
1908// StringCharCodeAtGenerator
1909
1910void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
1911 // If the receiver is a smi trigger the non-string case.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001912 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
1913 __ JumpIfSmi(object_, receiver_not_string_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001914
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001915 // Fetch the instance type of the receiver into result register.
1916 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
1917 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
1918 // If the receiver is not a string trigger the non-string case.
1919 __ test(result_, Immediate(kIsNotStringMask));
1920 __ j(not_zero, receiver_not_string_);
1921 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001922
1923 // If the index is non-smi trigger the non-smi case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001924 __ JumpIfNotSmi(index_, &index_not_smi_);
1925 __ bind(&got_smi_index_);
1926
1927 // Check for index out of range.
1928 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
1929 __ j(above_equal, index_out_of_range_);
1930
1931 __ SmiUntag(index_);
1932
1933 Factory* factory = masm->isolate()->factory();
1934 StringCharLoadGenerator::Generate(
1935 masm, factory, object_, index_, result_, &call_runtime_);
1936
1937 __ SmiTag(result_);
1938 __ bind(&exit_);
1939}
1940
1941
1942void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001943 MacroAssembler* masm, EmbedMode embed_mode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001944 const RuntimeCallHelper& call_helper) {
1945 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
1946
1947 // Index is not a smi.
1948 __ bind(&index_not_smi_);
1949 // If index is a heap number, try converting it to an integer.
1950 __ CheckMap(index_,
1951 masm->isolate()->factory()->heap_number_map(),
1952 index_not_number_,
1953 DONT_DO_SMI_CHECK);
1954 call_helper.BeforeCall(masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001955 if (embed_mode == PART_OF_IC_HANDLER) {
1956 __ push(LoadWithVectorDescriptor::VectorRegister());
1957 __ push(LoadDescriptor::SlotRegister());
1958 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001959 __ push(object_);
1960 __ push(index_); // Consumed by runtime conversion function.
1961 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001962 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001963 } else {
1964 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
1965 // NumberToSmi discards numbers that are not exact integers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001966 __ CallRuntime(Runtime::kNumberToSmi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001967 }
1968 if (!index_.is(eax)) {
1969 // Save the conversion result before the pop instructions below
1970 // have a chance to overwrite it.
1971 __ mov(index_, eax);
1972 }
1973 __ pop(object_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001974 if (embed_mode == PART_OF_IC_HANDLER) {
1975 __ pop(LoadDescriptor::SlotRegister());
1976 __ pop(LoadWithVectorDescriptor::VectorRegister());
1977 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001978 // Reload the instance type.
1979 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
1980 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
1981 call_helper.AfterCall(masm);
1982 // If index is still not a smi, it must be out of range.
1983 STATIC_ASSERT(kSmiTag == 0);
1984 __ JumpIfNotSmi(index_, index_out_of_range_);
1985 // Otherwise, return to the fast path.
1986 __ jmp(&got_smi_index_);
1987
1988 // Call runtime. We get here when the receiver is a string and the
1989 // index is a number, but the code of getting the actual character
1990 // is too complex (e.g., when the string needs to be flattened).
1991 __ bind(&call_runtime_);
1992 call_helper.BeforeCall(masm);
1993 __ push(object_);
1994 __ SmiTag(index_);
1995 __ push(index_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001996 __ CallRuntime(Runtime::kStringCharCodeAtRT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001997 if (!result_.is(eax)) {
1998 __ mov(result_, eax);
1999 }
2000 call_helper.AfterCall(masm);
2001 __ jmp(&exit_);
2002
2003 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
2004}
2005
2006
2007// -------------------------------------------------------------------------
2008// StringCharFromCodeGenerator
2009
2010void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
2011 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2012 STATIC_ASSERT(kSmiTag == 0);
2013 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002014 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
2015 __ test(code_, Immediate(kSmiTagMask |
2016 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002017 __ j(not_zero, &slow_case_);
2018
2019 Factory* factory = masm->isolate()->factory();
2020 __ Move(result_, Immediate(factory->single_character_string_cache()));
2021 STATIC_ASSERT(kSmiTag == 0);
2022 STATIC_ASSERT(kSmiTagSize == 1);
2023 STATIC_ASSERT(kSmiShiftSize == 0);
2024 // At this point code register contains smi tagged one byte char code.
2025 __ mov(result_, FieldOperand(result_,
2026 code_, times_half_pointer_size,
2027 FixedArray::kHeaderSize));
2028 __ cmp(result_, factory->undefined_value());
2029 __ j(equal, &slow_case_);
2030 __ bind(&exit_);
2031}
2032
2033
2034void StringCharFromCodeGenerator::GenerateSlow(
2035 MacroAssembler* masm,
2036 const RuntimeCallHelper& call_helper) {
2037 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
2038
2039 __ bind(&slow_case_);
2040 call_helper.BeforeCall(masm);
2041 __ push(code_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002042 __ CallRuntime(Runtime::kStringCharFromCode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002043 if (!result_.is(eax)) {
2044 __ mov(result_, eax);
2045 }
2046 call_helper.AfterCall(masm);
2047 __ jmp(&exit_);
2048
2049 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
2050}
2051
2052
2053void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2054 Register dest,
2055 Register src,
2056 Register count,
2057 Register scratch,
2058 String::Encoding encoding) {
2059 DCHECK(!scratch.is(dest));
2060 DCHECK(!scratch.is(src));
2061 DCHECK(!scratch.is(count));
2062
2063 // Nothing to do for zero characters.
2064 Label done;
2065 __ test(count, count);
2066 __ j(zero, &done);
2067
2068 // Make count the number of bytes to copy.
2069 if (encoding == String::TWO_BYTE_ENCODING) {
2070 __ shl(count, 1);
2071 }
2072
2073 Label loop;
2074 __ bind(&loop);
2075 __ mov_b(scratch, Operand(src, 0));
2076 __ mov_b(Operand(dest, 0), scratch);
2077 __ inc(src);
2078 __ inc(dest);
2079 __ dec(count);
2080 __ j(not_zero, &loop);
2081
2082 __ bind(&done);
2083}
2084
2085
2086void SubStringStub::Generate(MacroAssembler* masm) {
2087 Label runtime;
2088
2089 // Stack frame on entry.
2090 // esp[0]: return address
2091 // esp[4]: to
2092 // esp[8]: from
2093 // esp[12]: string
2094
2095 // Make sure first argument is a string.
2096 __ mov(eax, Operand(esp, 3 * kPointerSize));
2097 STATIC_ASSERT(kSmiTag == 0);
2098 __ JumpIfSmi(eax, &runtime);
2099 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
2100 __ j(NegateCondition(is_string), &runtime);
2101
2102 // eax: string
2103 // ebx: instance type
2104
2105 // Calculate length of sub string using the smi values.
2106 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
2107 __ JumpIfNotSmi(ecx, &runtime);
2108 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
2109 __ JumpIfNotSmi(edx, &runtime);
2110 __ sub(ecx, edx);
2111 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
2112 Label not_original_string;
2113 // Shorter than original string's length: an actual substring.
2114 __ j(below, &not_original_string, Label::kNear);
2115 // Longer than original string's length or negative: unsafe arguments.
2116 __ j(above, &runtime);
2117 // Return original string.
2118 Counters* counters = isolate()->counters();
2119 __ IncrementCounter(counters->sub_string_native(), 1);
2120 __ ret(3 * kPointerSize);
2121 __ bind(&not_original_string);
2122
2123 Label single_char;
2124 __ cmp(ecx, Immediate(Smi::FromInt(1)));
2125 __ j(equal, &single_char);
2126
2127 // eax: string
2128 // ebx: instance type
2129 // ecx: sub string length (smi)
2130 // edx: from index (smi)
2131 // Deal with different string types: update the index if necessary
2132 // and put the underlying string into edi.
2133 Label underlying_unpacked, sliced_string, seq_or_external_string;
2134 // If the string is not indirect, it can only be sequential or external.
2135 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2136 STATIC_ASSERT(kIsIndirectStringMask != 0);
2137 __ test(ebx, Immediate(kIsIndirectStringMask));
2138 __ j(zero, &seq_or_external_string, Label::kNear);
2139
2140 Factory* factory = isolate()->factory();
2141 __ test(ebx, Immediate(kSlicedNotConsMask));
2142 __ j(not_zero, &sliced_string, Label::kNear);
2143 // Cons string. Check whether it is flat, then fetch first part.
2144 // Flat cons strings have an empty second part.
2145 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
2146 factory->empty_string());
2147 __ j(not_equal, &runtime);
2148 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
2149 // Update instance type.
2150 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2151 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2152 __ jmp(&underlying_unpacked, Label::kNear);
2153
2154 __ bind(&sliced_string);
2155 // Sliced string. Fetch parent and adjust start index by offset.
2156 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
2157 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
2158 // Update instance type.
2159 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2160 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2161 __ jmp(&underlying_unpacked, Label::kNear);
2162
2163 __ bind(&seq_or_external_string);
2164 // Sequential or external string. Just move string to the expected register.
2165 __ mov(edi, eax);
2166
2167 __ bind(&underlying_unpacked);
2168
2169 if (FLAG_string_slices) {
2170 Label copy_routine;
2171 // edi: underlying subject string
2172 // ebx: instance type of underlying subject string
2173 // edx: adjusted start index (smi)
2174 // ecx: length (smi)
2175 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
2176 // Short slice. Copy instead of slicing.
2177 __ j(less, &copy_routine);
2178 // Allocate new sliced string. At this point we do not reload the instance
2179 // type including the string encoding because we simply rely on the info
2180 // provided by the original string. It does not matter if the original
2181 // string's encoding is wrong because we always have to recheck encoding of
2182 // the newly created string's parent anyways due to externalized strings.
2183 Label two_byte_slice, set_slice_header;
2184 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
2185 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2186 __ test(ebx, Immediate(kStringEncodingMask));
2187 __ j(zero, &two_byte_slice, Label::kNear);
2188 __ AllocateOneByteSlicedString(eax, ebx, no_reg, &runtime);
2189 __ jmp(&set_slice_header, Label::kNear);
2190 __ bind(&two_byte_slice);
2191 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
2192 __ bind(&set_slice_header);
2193 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
2194 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
2195 Immediate(String::kEmptyHashField));
2196 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
2197 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
2198 __ IncrementCounter(counters->sub_string_native(), 1);
2199 __ ret(3 * kPointerSize);
2200
2201 __ bind(&copy_routine);
2202 }
2203
2204 // edi: underlying subject string
2205 // ebx: instance type of underlying subject string
2206 // edx: adjusted start index (smi)
2207 // ecx: length (smi)
2208 // The subject string can only be external or sequential string of either
2209 // encoding at this point.
2210 Label two_byte_sequential, runtime_drop_two, sequential_string;
2211 STATIC_ASSERT(kExternalStringTag != 0);
2212 STATIC_ASSERT(kSeqStringTag == 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002213 __ test_b(ebx, Immediate(kExternalStringTag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002214 __ j(zero, &sequential_string);
2215
2216 // Handle external string.
2217 // Rule out short external strings.
2218 STATIC_ASSERT(kShortExternalStringTag != 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002219 __ test_b(ebx, Immediate(kShortExternalStringMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002220 __ j(not_zero, &runtime);
2221 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
2222 // Move the pointer so that offset-wise, it looks like a sequential string.
2223 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2224 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2225
2226 __ bind(&sequential_string);
2227 // Stash away (adjusted) index and (underlying) string.
2228 __ push(edx);
2229 __ push(edi);
2230 __ SmiUntag(ecx);
2231 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002232 __ test_b(ebx, Immediate(kStringEncodingMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002233 __ j(zero, &two_byte_sequential);
2234
2235 // Sequential one byte string. Allocate the result.
2236 __ AllocateOneByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
2237
2238 // eax: result string
2239 // ecx: result string length
2240 // Locate first character of result.
2241 __ mov(edi, eax);
2242 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
2243 // Load string argument and locate character of sub string start.
2244 __ pop(edx);
2245 __ pop(ebx);
2246 __ SmiUntag(ebx);
2247 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqOneByteString::kHeaderSize));
2248
2249 // eax: result string
2250 // ecx: result length
2251 // edi: first character of result
2252 // edx: character of sub string start
2253 StringHelper::GenerateCopyCharacters(
2254 masm, edi, edx, ecx, ebx, String::ONE_BYTE_ENCODING);
2255 __ IncrementCounter(counters->sub_string_native(), 1);
2256 __ ret(3 * kPointerSize);
2257
2258 __ bind(&two_byte_sequential);
2259 // Sequential two-byte string. Allocate the result.
2260 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
2261
2262 // eax: result string
2263 // ecx: result string length
2264 // Locate first character of result.
2265 __ mov(edi, eax);
2266 __ add(edi,
2267 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2268 // Load string argument and locate character of sub string start.
2269 __ pop(edx);
2270 __ pop(ebx);
2271 // As from is a smi it is 2 times the value which matches the size of a two
2272 // byte character.
2273 STATIC_ASSERT(kSmiTag == 0);
2274 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2275 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqTwoByteString::kHeaderSize));
2276
2277 // eax: result string
2278 // ecx: result length
2279 // edi: first character of result
2280 // edx: character of sub string start
2281 StringHelper::GenerateCopyCharacters(
2282 masm, edi, edx, ecx, ebx, String::TWO_BYTE_ENCODING);
2283 __ IncrementCounter(counters->sub_string_native(), 1);
2284 __ ret(3 * kPointerSize);
2285
2286 // Drop pushed values on the stack before tail call.
2287 __ bind(&runtime_drop_two);
2288 __ Drop(2);
2289
2290 // Just jump to runtime to create the sub string.
2291 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002292 __ TailCallRuntime(Runtime::kSubString);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002293
2294 __ bind(&single_char);
2295 // eax: string
2296 // ebx: instance type
2297 // ecx: sub string length (smi)
2298 // edx: from index (smi)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002299 StringCharAtGenerator generator(eax, edx, ecx, eax, &runtime, &runtime,
2300 &runtime, STRING_INDEX_IS_NUMBER,
2301 RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002302 generator.GenerateFast(masm);
2303 __ ret(3 * kPointerSize);
2304 generator.SkipSlow(masm, &runtime);
2305}
2306
2307
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002308void ToNumberStub::Generate(MacroAssembler* masm) {
2309 // The ToNumber stub takes one argument in eax.
2310 Label not_smi;
2311 __ JumpIfNotSmi(eax, &not_smi, Label::kNear);
2312 __ Ret();
2313 __ bind(&not_smi);
2314
2315 Label not_heap_number;
2316 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2317 __ j(not_equal, &not_heap_number, Label::kNear);
2318 __ Ret();
2319 __ bind(&not_heap_number);
2320
Ben Murdochda12d292016-06-02 14:46:10 +01002321 NonNumberToNumberStub stub(masm->isolate());
2322 __ TailCallStub(&stub);
2323}
2324
2325void NonNumberToNumberStub::Generate(MacroAssembler* masm) {
2326 // The NonNumberToNumber stub takes one argument in eax.
2327 __ AssertNotNumber(eax);
2328
2329 Label not_string;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002330 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
2331 // eax: object
2332 // edi: object map
2333 __ j(above_equal, &not_string, Label::kNear);
Ben Murdochda12d292016-06-02 14:46:10 +01002334 StringToNumberStub stub(masm->isolate());
2335 __ TailCallStub(&stub);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002336 __ bind(&not_string);
2337
2338 Label not_oddball;
2339 __ CmpInstanceType(edi, ODDBALL_TYPE);
2340 __ j(not_equal, &not_oddball, Label::kNear);
2341 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
2342 __ Ret();
2343 __ bind(&not_oddball);
2344
2345 __ pop(ecx); // Pop return address.
2346 __ push(eax); // Push argument.
2347 __ push(ecx); // Push return address.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002348 __ TailCallRuntime(Runtime::kToNumber);
2349}
2350
Ben Murdochda12d292016-06-02 14:46:10 +01002351void StringToNumberStub::Generate(MacroAssembler* masm) {
2352 // The StringToNumber stub takes one argument in eax.
2353 __ AssertString(eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002354
Ben Murdochda12d292016-06-02 14:46:10 +01002355 // Check if string has a cached array index.
2356 Label runtime;
2357 __ test(FieldOperand(eax, String::kHashFieldOffset),
2358 Immediate(String::kContainsCachedArrayIndexMask));
2359 __ j(not_zero, &runtime, Label::kNear);
2360 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
2361 __ IndexFromHash(eax, eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002362 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002363
Ben Murdochda12d292016-06-02 14:46:10 +01002364 __ bind(&runtime);
2365 __ PopReturnAddressTo(ecx); // Pop return address.
2366 __ Push(eax); // Push argument.
2367 __ PushReturnAddressFrom(ecx); // Push return address.
2368 __ TailCallRuntime(Runtime::kStringToNumber);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002369}
2370
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002371void ToStringStub::Generate(MacroAssembler* masm) {
2372 // The ToString stub takes one argument in eax.
2373 Label is_number;
2374 __ JumpIfSmi(eax, &is_number, Label::kNear);
2375
2376 Label not_string;
2377 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
2378 // eax: receiver
2379 // edi: receiver map
2380 __ j(above_equal, &not_string, Label::kNear);
2381 __ Ret();
2382 __ bind(&not_string);
2383
2384 Label not_heap_number;
2385 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2386 __ j(not_equal, &not_heap_number, Label::kNear);
2387 __ bind(&is_number);
2388 NumberToStringStub stub(isolate());
2389 __ TailCallStub(&stub);
2390 __ bind(&not_heap_number);
2391
2392 Label not_oddball;
2393 __ CmpInstanceType(edi, ODDBALL_TYPE);
2394 __ j(not_equal, &not_oddball, Label::kNear);
2395 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
2396 __ Ret();
2397 __ bind(&not_oddball);
2398
2399 __ pop(ecx); // Pop return address.
2400 __ push(eax); // Push argument.
2401 __ push(ecx); // Push return address.
2402 __ TailCallRuntime(Runtime::kToString);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002403}
2404
2405
Ben Murdoch097c5b22016-05-18 11:27:45 +01002406void ToNameStub::Generate(MacroAssembler* masm) {
2407 // The ToName stub takes one argument in eax.
2408 Label is_number;
2409 __ JumpIfSmi(eax, &is_number, Label::kNear);
2410
2411 Label not_name;
2412 STATIC_ASSERT(FIRST_NAME_TYPE == FIRST_TYPE);
2413 __ CmpObjectType(eax, LAST_NAME_TYPE, edi);
2414 // eax: receiver
2415 // edi: receiver map
2416 __ j(above, &not_name, Label::kNear);
2417 __ Ret();
2418 __ bind(&not_name);
2419
2420 Label not_heap_number;
2421 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2422 __ j(not_equal, &not_heap_number, Label::kNear);
2423 __ bind(&is_number);
2424 NumberToStringStub stub(isolate());
2425 __ TailCallStub(&stub);
2426 __ bind(&not_heap_number);
2427
2428 Label not_oddball;
2429 __ CmpInstanceType(edi, ODDBALL_TYPE);
2430 __ j(not_equal, &not_oddball, Label::kNear);
2431 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
2432 __ Ret();
2433 __ bind(&not_oddball);
2434
2435 __ pop(ecx); // Pop return address.
2436 __ push(eax); // Push argument.
2437 __ push(ecx); // Push return address.
2438 __ TailCallRuntime(Runtime::kToName);
2439}
2440
2441
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002442void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
2443 Register left,
2444 Register right,
2445 Register scratch1,
2446 Register scratch2) {
2447 Register length = scratch1;
2448
2449 // Compare lengths.
2450 Label strings_not_equal, check_zero_length;
2451 __ mov(length, FieldOperand(left, String::kLengthOffset));
2452 __ cmp(length, FieldOperand(right, String::kLengthOffset));
2453 __ j(equal, &check_zero_length, Label::kNear);
2454 __ bind(&strings_not_equal);
2455 __ Move(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
2456 __ ret(0);
2457
2458 // Check if the length is zero.
2459 Label compare_chars;
2460 __ bind(&check_zero_length);
2461 STATIC_ASSERT(kSmiTag == 0);
2462 __ test(length, length);
2463 __ j(not_zero, &compare_chars, Label::kNear);
2464 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2465 __ ret(0);
2466
2467 // Compare characters.
2468 __ bind(&compare_chars);
2469 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
2470 &strings_not_equal, Label::kNear);
2471
2472 // Characters are equal.
2473 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2474 __ ret(0);
2475}
2476
2477
2478void StringHelper::GenerateCompareFlatOneByteStrings(
2479 MacroAssembler* masm, Register left, Register right, Register scratch1,
2480 Register scratch2, Register scratch3) {
2481 Counters* counters = masm->isolate()->counters();
2482 __ IncrementCounter(counters->string_compare_native(), 1);
2483
2484 // Find minimum length.
2485 Label left_shorter;
2486 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
2487 __ mov(scratch3, scratch1);
2488 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
2489
2490 Register length_delta = scratch3;
2491
2492 __ j(less_equal, &left_shorter, Label::kNear);
2493 // Right string is shorter. Change scratch1 to be length of right string.
2494 __ sub(scratch1, length_delta);
2495 __ bind(&left_shorter);
2496
2497 Register min_length = scratch1;
2498
2499 // If either length is zero, just compare lengths.
2500 Label compare_lengths;
2501 __ test(min_length, min_length);
2502 __ j(zero, &compare_lengths, Label::kNear);
2503
2504 // Compare characters.
2505 Label result_not_equal;
2506 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
2507 &result_not_equal, Label::kNear);
2508
2509 // Compare lengths - strings up to min-length are equal.
2510 __ bind(&compare_lengths);
2511 __ test(length_delta, length_delta);
2512 Label length_not_equal;
2513 __ j(not_zero, &length_not_equal, Label::kNear);
2514
2515 // Result is EQUAL.
2516 STATIC_ASSERT(EQUAL == 0);
2517 STATIC_ASSERT(kSmiTag == 0);
2518 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2519 __ ret(0);
2520
2521 Label result_greater;
2522 Label result_less;
2523 __ bind(&length_not_equal);
2524 __ j(greater, &result_greater, Label::kNear);
2525 __ jmp(&result_less, Label::kNear);
2526 __ bind(&result_not_equal);
2527 __ j(above, &result_greater, Label::kNear);
2528 __ bind(&result_less);
2529
2530 // Result is LESS.
2531 __ Move(eax, Immediate(Smi::FromInt(LESS)));
2532 __ ret(0);
2533
2534 // Result is GREATER.
2535 __ bind(&result_greater);
2536 __ Move(eax, Immediate(Smi::FromInt(GREATER)));
2537 __ ret(0);
2538}
2539
2540
2541void StringHelper::GenerateOneByteCharsCompareLoop(
2542 MacroAssembler* masm, Register left, Register right, Register length,
2543 Register scratch, Label* chars_not_equal,
2544 Label::Distance chars_not_equal_near) {
2545 // Change index to run from -length to -1 by adding length to string
2546 // start. This means that loop ends when index reaches zero, which
2547 // doesn't need an additional compare.
2548 __ SmiUntag(length);
2549 __ lea(left,
2550 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
2551 __ lea(right,
2552 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
2553 __ neg(length);
2554 Register index = length; // index = -length;
2555
2556 // Compare loop.
2557 Label loop;
2558 __ bind(&loop);
2559 __ mov_b(scratch, Operand(left, index, times_1, 0));
2560 __ cmpb(scratch, Operand(right, index, times_1, 0));
2561 __ j(not_equal, chars_not_equal, chars_not_equal_near);
2562 __ inc(index);
2563 __ j(not_zero, &loop);
2564}
2565
2566
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002567void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
2568 // ----------- S t a t e -------------
2569 // -- edx : left
2570 // -- eax : right
2571 // -- esp[0] : return address
2572 // -----------------------------------
2573
2574 // Load ecx with the allocation site. We stick an undefined dummy value here
2575 // and replace it with the real allocation site later when we instantiate this
2576 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
2577 __ mov(ecx, handle(isolate()->heap()->undefined_value()));
2578
2579 // Make sure that we actually patched the allocation site.
2580 if (FLAG_debug_code) {
2581 __ test(ecx, Immediate(kSmiTagMask));
2582 __ Assert(not_equal, kExpectedAllocationSite);
2583 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
2584 isolate()->factory()->allocation_site_map());
2585 __ Assert(equal, kExpectedAllocationSite);
2586 }
2587
2588 // Tail call into the stub that handles binary operations with allocation
2589 // sites.
2590 BinaryOpWithAllocationSiteStub stub(isolate(), state());
2591 __ TailCallStub(&stub);
2592}
2593
2594
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002595void CompareICStub::GenerateBooleans(MacroAssembler* masm) {
2596 DCHECK_EQ(CompareICState::BOOLEAN, state());
2597 Label miss;
2598 Label::Distance const miss_distance =
2599 masm->emit_debug_code() ? Label::kFar : Label::kNear;
2600
2601 __ JumpIfSmi(edx, &miss, miss_distance);
2602 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
2603 __ JumpIfSmi(eax, &miss, miss_distance);
2604 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2605 __ JumpIfNotRoot(ecx, Heap::kBooleanMapRootIndex, &miss, miss_distance);
2606 __ JumpIfNotRoot(ebx, Heap::kBooleanMapRootIndex, &miss, miss_distance);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002607 if (!Token::IsEqualityOp(op())) {
2608 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
2609 __ AssertSmi(eax);
2610 __ mov(edx, FieldOperand(edx, Oddball::kToNumberOffset));
2611 __ AssertSmi(edx);
2612 __ xchg(eax, edx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002613 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002614 __ sub(eax, edx);
2615 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002616
2617 __ bind(&miss);
2618 GenerateMiss(masm);
2619}
2620
2621
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002622void CompareICStub::GenerateSmis(MacroAssembler* masm) {
2623 DCHECK(state() == CompareICState::SMI);
2624 Label miss;
2625 __ mov(ecx, edx);
2626 __ or_(ecx, eax);
2627 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
2628
2629 if (GetCondition() == equal) {
2630 // For equality we do not care about the sign of the result.
2631 __ sub(eax, edx);
2632 } else {
2633 Label done;
2634 __ sub(edx, eax);
2635 __ j(no_overflow, &done, Label::kNear);
2636 // Correct sign of result in case of overflow.
2637 __ not_(edx);
2638 __ bind(&done);
2639 __ mov(eax, edx);
2640 }
2641 __ ret(0);
2642
2643 __ bind(&miss);
2644 GenerateMiss(masm);
2645}
2646
2647
2648void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
2649 DCHECK(state() == CompareICState::NUMBER);
2650
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002651 Label generic_stub, check_left;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002652 Label unordered, maybe_undefined1, maybe_undefined2;
2653 Label miss;
2654
2655 if (left() == CompareICState::SMI) {
2656 __ JumpIfNotSmi(edx, &miss);
2657 }
2658 if (right() == CompareICState::SMI) {
2659 __ JumpIfNotSmi(eax, &miss);
2660 }
2661
2662 // Inlining the double comparison and falling back to the general compare
2663 // stub if NaN is involved or SSE2 or CMOV is unsupported.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002664 __ JumpIfSmi(eax, &check_left, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002665 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
2666 isolate()->factory()->heap_number_map());
2667 __ j(not_equal, &maybe_undefined1, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002668
2669 __ bind(&check_left);
2670 __ JumpIfSmi(edx, &generic_stub, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002671 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
2672 isolate()->factory()->heap_number_map());
2673 __ j(not_equal, &maybe_undefined2, Label::kNear);
2674
2675 __ bind(&unordered);
2676 __ bind(&generic_stub);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002677 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002678 CompareICState::GENERIC, CompareICState::GENERIC);
2679 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
2680
2681 __ bind(&maybe_undefined1);
2682 if (Token::IsOrderedRelationalCompareOp(op())) {
2683 __ cmp(eax, Immediate(isolate()->factory()->undefined_value()));
2684 __ j(not_equal, &miss);
2685 __ JumpIfSmi(edx, &unordered);
2686 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
2687 __ j(not_equal, &maybe_undefined2, Label::kNear);
2688 __ jmp(&unordered);
2689 }
2690
2691 __ bind(&maybe_undefined2);
2692 if (Token::IsOrderedRelationalCompareOp(op())) {
2693 __ cmp(edx, Immediate(isolate()->factory()->undefined_value()));
2694 __ j(equal, &unordered);
2695 }
2696
2697 __ bind(&miss);
2698 GenerateMiss(masm);
2699}
2700
2701
2702void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
2703 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
2704 DCHECK(GetCondition() == equal);
2705
2706 // Registers containing left and right operands respectively.
2707 Register left = edx;
2708 Register right = eax;
2709 Register tmp1 = ecx;
2710 Register tmp2 = ebx;
2711
2712 // Check that both operands are heap objects.
2713 Label miss;
2714 __ mov(tmp1, left);
2715 STATIC_ASSERT(kSmiTag == 0);
2716 __ and_(tmp1, right);
2717 __ JumpIfSmi(tmp1, &miss, Label::kNear);
2718
2719 // Check that both operands are internalized strings.
2720 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
2721 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
2722 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
2723 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
2724 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2725 __ or_(tmp1, tmp2);
2726 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2727 __ j(not_zero, &miss, Label::kNear);
2728
2729 // Internalized strings are compared by identity.
2730 Label done;
2731 __ cmp(left, right);
2732 // Make sure eax is non-zero. At this point input operands are
2733 // guaranteed to be non-zero.
2734 DCHECK(right.is(eax));
2735 __ j(not_equal, &done, Label::kNear);
2736 STATIC_ASSERT(EQUAL == 0);
2737 STATIC_ASSERT(kSmiTag == 0);
2738 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2739 __ bind(&done);
2740 __ ret(0);
2741
2742 __ bind(&miss);
2743 GenerateMiss(masm);
2744}
2745
2746
2747void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
2748 DCHECK(state() == CompareICState::UNIQUE_NAME);
2749 DCHECK(GetCondition() == equal);
2750
2751 // Registers containing left and right operands respectively.
2752 Register left = edx;
2753 Register right = eax;
2754 Register tmp1 = ecx;
2755 Register tmp2 = ebx;
2756
2757 // Check that both operands are heap objects.
2758 Label miss;
2759 __ mov(tmp1, left);
2760 STATIC_ASSERT(kSmiTag == 0);
2761 __ and_(tmp1, right);
2762 __ JumpIfSmi(tmp1, &miss, Label::kNear);
2763
2764 // Check that both operands are unique names. This leaves the instance
2765 // types loaded in tmp1 and tmp2.
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
2771 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
2772 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
2773
2774 // Unique names are compared by identity.
2775 Label done;
2776 __ cmp(left, right);
2777 // Make sure eax is non-zero. At this point input operands are
2778 // guaranteed to be non-zero.
2779 DCHECK(right.is(eax));
2780 __ j(not_equal, &done, Label::kNear);
2781 STATIC_ASSERT(EQUAL == 0);
2782 STATIC_ASSERT(kSmiTag == 0);
2783 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2784 __ bind(&done);
2785 __ ret(0);
2786
2787 __ bind(&miss);
2788 GenerateMiss(masm);
2789}
2790
2791
2792void CompareICStub::GenerateStrings(MacroAssembler* masm) {
2793 DCHECK(state() == CompareICState::STRING);
2794 Label miss;
2795
2796 bool equality = Token::IsEqualityOp(op());
2797
2798 // Registers containing left and right operands respectively.
2799 Register left = edx;
2800 Register right = eax;
2801 Register tmp1 = ecx;
2802 Register tmp2 = ebx;
2803 Register tmp3 = edi;
2804
2805 // Check that both operands are heap objects.
2806 __ mov(tmp1, left);
2807 STATIC_ASSERT(kSmiTag == 0);
2808 __ and_(tmp1, right);
2809 __ JumpIfSmi(tmp1, &miss);
2810
2811 // Check that both operands are strings. This leaves the instance
2812 // types loaded in tmp1 and tmp2.
2813 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
2814 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
2815 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
2816 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
2817 __ mov(tmp3, tmp1);
2818 STATIC_ASSERT(kNotStringTag != 0);
2819 __ or_(tmp3, tmp2);
2820 __ test(tmp3, Immediate(kIsNotStringMask));
2821 __ j(not_zero, &miss);
2822
2823 // Fast check for identical strings.
2824 Label not_same;
2825 __ cmp(left, right);
2826 __ j(not_equal, &not_same, Label::kNear);
2827 STATIC_ASSERT(EQUAL == 0);
2828 STATIC_ASSERT(kSmiTag == 0);
2829 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2830 __ ret(0);
2831
2832 // Handle not identical strings.
2833 __ bind(&not_same);
2834
2835 // Check that both strings are internalized. If they are, we're done
2836 // because we already know they are not identical. But in the case of
2837 // non-equality compare, we still need to determine the order. We
2838 // also know they are both strings.
2839 if (equality) {
2840 Label do_compare;
2841 STATIC_ASSERT(kInternalizedTag == 0);
2842 __ or_(tmp1, tmp2);
2843 __ test(tmp1, Immediate(kIsNotInternalizedMask));
2844 __ j(not_zero, &do_compare, Label::kNear);
2845 // Make sure eax is non-zero. At this point input operands are
2846 // guaranteed to be non-zero.
2847 DCHECK(right.is(eax));
2848 __ ret(0);
2849 __ bind(&do_compare);
2850 }
2851
2852 // Check that both strings are sequential one-byte.
2853 Label runtime;
2854 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
2855
2856 // Compare flat one byte strings. Returns when done.
2857 if (equality) {
2858 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
2859 tmp2);
2860 } else {
2861 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
2862 tmp2, tmp3);
2863 }
2864
2865 // Handle more complex cases in runtime.
2866 __ bind(&runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002867 if (equality) {
Ben Murdochda12d292016-06-02 14:46:10 +01002868 {
2869 FrameScope scope(masm, StackFrame::INTERNAL);
2870 __ Push(left);
2871 __ Push(right);
2872 __ CallRuntime(Runtime::kStringEqual);
2873 }
2874 __ sub(eax, Immediate(masm->isolate()->factory()->true_value()));
2875 __ Ret();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002876 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01002877 __ pop(tmp1); // Return address.
2878 __ push(left);
2879 __ push(right);
2880 __ push(tmp1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002881 __ TailCallRuntime(Runtime::kStringCompare);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002882 }
2883
2884 __ bind(&miss);
2885 GenerateMiss(masm);
2886}
2887
2888
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002889void CompareICStub::GenerateReceivers(MacroAssembler* masm) {
2890 DCHECK_EQ(CompareICState::RECEIVER, state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002891 Label miss;
2892 __ mov(ecx, edx);
2893 __ and_(ecx, eax);
2894 __ JumpIfSmi(ecx, &miss, Label::kNear);
2895
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002896 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
2897 __ CmpObjectType(eax, FIRST_JS_RECEIVER_TYPE, ecx);
2898 __ j(below, &miss, Label::kNear);
2899 __ CmpObjectType(edx, FIRST_JS_RECEIVER_TYPE, ecx);
2900 __ j(below, &miss, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002901
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002902 DCHECK_EQ(equal, GetCondition());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002903 __ sub(eax, edx);
2904 __ ret(0);
2905
2906 __ bind(&miss);
2907 GenerateMiss(masm);
2908}
2909
2910
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002911void CompareICStub::GenerateKnownReceivers(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002912 Label miss;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002913 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002914 __ mov(ecx, edx);
2915 __ and_(ecx, eax);
2916 __ JumpIfSmi(ecx, &miss, Label::kNear);
2917
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002918 __ GetWeakValue(edi, cell);
2919 __ cmp(edi, FieldOperand(eax, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002920 __ j(not_equal, &miss, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002921 __ cmp(edi, FieldOperand(edx, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002922 __ j(not_equal, &miss, Label::kNear);
2923
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002924 if (Token::IsEqualityOp(op())) {
2925 __ sub(eax, edx);
2926 __ ret(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002927 } else {
2928 __ PopReturnAddressTo(ecx);
2929 __ Push(edx);
2930 __ Push(eax);
2931 __ Push(Immediate(Smi::FromInt(NegativeComparisonResult(GetCondition()))));
2932 __ PushReturnAddressFrom(ecx);
2933 __ TailCallRuntime(Runtime::kCompare);
2934 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002935
2936 __ bind(&miss);
2937 GenerateMiss(masm);
2938}
2939
2940
2941void CompareICStub::GenerateMiss(MacroAssembler* masm) {
2942 {
2943 // Call the runtime system in a fresh internal frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002944 FrameScope scope(masm, StackFrame::INTERNAL);
2945 __ push(edx); // Preserve edx and eax.
2946 __ push(eax);
2947 __ push(edx); // And also use them as the arguments.
2948 __ push(eax);
2949 __ push(Immediate(Smi::FromInt(op())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002950 __ CallRuntime(Runtime::kCompareIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002951 // Compute the entry point of the rewritten stub.
2952 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
2953 __ pop(eax);
2954 __ pop(edx);
2955 }
2956
2957 // Do a tail call to the rewritten stub.
2958 __ jmp(edi);
2959}
2960
2961
2962// Helper function used to check that the dictionary doesn't contain
2963// the property. This function may return false negatives, so miss_label
2964// must always call a backup property check that is complete.
2965// This function is safe to call if the receiver has fast properties.
2966// Name must be a unique name and receiver must be a heap object.
2967void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
2968 Label* miss,
2969 Label* done,
2970 Register properties,
2971 Handle<Name> name,
2972 Register r0) {
2973 DCHECK(name->IsUniqueName());
2974
2975 // If names of slots in range from 1 to kProbes - 1 for the hash value are
2976 // not equal to the name and kProbes-th slot is not used (its name is the
2977 // undefined value), it guarantees the hash table doesn't contain the
2978 // property. It's true even if some slots represent deleted properties
2979 // (their names are the hole value).
2980 for (int i = 0; i < kInlinedProbes; i++) {
2981 // Compute the masked index: (hash + i + i * i) & mask.
2982 Register index = r0;
2983 // Capacity is smi 2^n.
2984 __ mov(index, FieldOperand(properties, kCapacityOffset));
2985 __ dec(index);
2986 __ and_(index,
2987 Immediate(Smi::FromInt(name->Hash() +
2988 NameDictionary::GetProbeOffset(i))));
2989
2990 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002991 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002992 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
2993 Register entity_name = r0;
2994 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002995 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002996 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
2997 kElementsStartOffset - kHeapObjectTag));
2998 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
2999 __ j(equal, done);
3000
3001 // Stop if found the property.
3002 __ cmp(entity_name, Handle<Name>(name));
3003 __ j(equal, miss);
3004
3005 Label good;
3006 // Check for the hole and skip.
3007 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
3008 __ j(equal, &good, Label::kNear);
3009
3010 // Check if the entry name is not a unique name.
3011 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
3012 __ JumpIfNotUniqueNameInstanceType(
3013 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
3014 __ bind(&good);
3015 }
3016
3017 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
3018 NEGATIVE_LOOKUP);
3019 __ push(Immediate(Handle<Object>(name)));
3020 __ push(Immediate(name->Hash()));
3021 __ CallStub(&stub);
3022 __ test(r0, r0);
3023 __ j(not_zero, miss);
3024 __ jmp(done);
3025}
3026
3027
3028// Probe the name dictionary in the |elements| register. Jump to the
3029// |done| label if a property with the given name is found leaving the
3030// index into the dictionary in |r0|. Jump to the |miss| label
3031// otherwise.
3032void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3033 Label* miss,
3034 Label* done,
3035 Register elements,
3036 Register name,
3037 Register r0,
3038 Register r1) {
3039 DCHECK(!elements.is(r0));
3040 DCHECK(!elements.is(r1));
3041 DCHECK(!name.is(r0));
3042 DCHECK(!name.is(r1));
3043
3044 __ AssertName(name);
3045
3046 __ mov(r1, FieldOperand(elements, kCapacityOffset));
3047 __ shr(r1, kSmiTagSize); // convert smi to int
3048 __ dec(r1);
3049
3050 // Generate an unrolled loop that performs a few probes before
3051 // giving up. Measurements done on Gmail indicate that 2 probes
3052 // cover ~93% of loads from dictionaries.
3053 for (int i = 0; i < kInlinedProbes; i++) {
3054 // Compute the masked index: (hash + i + i * i) & mask.
3055 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3056 __ shr(r0, Name::kHashShift);
3057 if (i > 0) {
3058 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
3059 }
3060 __ and_(r0, r1);
3061
3062 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003063 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003064 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
3065
3066 // Check if the key is identical to the name.
3067 __ cmp(name, Operand(elements,
3068 r0,
3069 times_4,
3070 kElementsStartOffset - kHeapObjectTag));
3071 __ j(equal, done);
3072 }
3073
3074 NameDictionaryLookupStub stub(masm->isolate(), elements, r1, r0,
3075 POSITIVE_LOOKUP);
3076 __ push(name);
3077 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
3078 __ shr(r0, Name::kHashShift);
3079 __ push(r0);
3080 __ CallStub(&stub);
3081
3082 __ test(r1, r1);
3083 __ j(zero, miss);
3084 __ jmp(done);
3085}
3086
3087
3088void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
3089 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3090 // we cannot call anything that could cause a GC from this stub.
3091 // Stack frame on entry:
3092 // esp[0 * kPointerSize]: return address.
3093 // esp[1 * kPointerSize]: key's hash.
3094 // esp[2 * kPointerSize]: key.
3095 // Registers:
3096 // dictionary_: NameDictionary to probe.
3097 // result_: used as scratch.
3098 // index_: will hold an index of entry if lookup is successful.
3099 // might alias with result_.
3100 // Returns:
3101 // result_ is zero if lookup failed, non zero otherwise.
3102
3103 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3104
3105 Register scratch = result();
3106
3107 __ mov(scratch, FieldOperand(dictionary(), kCapacityOffset));
3108 __ dec(scratch);
3109 __ SmiUntag(scratch);
3110 __ push(scratch);
3111
3112 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3113 // not equal to the name and kProbes-th slot is not used (its name is the
3114 // undefined value), it guarantees the hash table doesn't contain the
3115 // property. It's true even if some slots represent deleted properties
3116 // (their names are the null value).
3117 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3118 // Compute the masked index: (hash + i + i * i) & mask.
3119 __ mov(scratch, Operand(esp, 2 * kPointerSize));
3120 if (i > 0) {
3121 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
3122 }
3123 __ and_(scratch, Operand(esp, 0));
3124
3125 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003126 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003127 __ lea(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
3128
3129 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003130 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003131 __ mov(scratch, Operand(dictionary(), index(), times_pointer_size,
3132 kElementsStartOffset - kHeapObjectTag));
3133 __ cmp(scratch, isolate()->factory()->undefined_value());
3134 __ j(equal, &not_in_dictionary);
3135
3136 // Stop if found the property.
3137 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
3138 __ j(equal, &in_dictionary);
3139
3140 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3141 // If we hit a key that is not a unique name during negative
3142 // lookup we have to bailout as this key might be equal to the
3143 // key we are looking for.
3144
3145 // Check if the entry name is not a unique name.
3146 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
3147 __ JumpIfNotUniqueNameInstanceType(
3148 FieldOperand(scratch, Map::kInstanceTypeOffset),
3149 &maybe_in_dictionary);
3150 }
3151 }
3152
3153 __ bind(&maybe_in_dictionary);
3154 // If we are doing negative lookup then probing failure should be
3155 // treated as a lookup success. For positive lookup probing failure
3156 // should be treated as lookup failure.
3157 if (mode() == POSITIVE_LOOKUP) {
3158 __ mov(result(), Immediate(0));
3159 __ Drop(1);
3160 __ ret(2 * kPointerSize);
3161 }
3162
3163 __ bind(&in_dictionary);
3164 __ mov(result(), Immediate(1));
3165 __ Drop(1);
3166 __ ret(2 * kPointerSize);
3167
3168 __ bind(&not_in_dictionary);
3169 __ mov(result(), Immediate(0));
3170 __ Drop(1);
3171 __ ret(2 * kPointerSize);
3172}
3173
3174
3175void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3176 Isolate* isolate) {
3177 StoreBufferOverflowStub stub(isolate, kDontSaveFPRegs);
3178 stub.GetCode();
3179 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3180 stub2.GetCode();
3181}
3182
3183
3184// Takes the input in 3 registers: address_ value_ and object_. A pointer to
3185// the value has just been written into the object, now this stub makes sure
3186// we keep the GC informed. The word in the object where the value has been
3187// written is in the address register.
3188void RecordWriteStub::Generate(MacroAssembler* masm) {
3189 Label skip_to_incremental_noncompacting;
3190 Label skip_to_incremental_compacting;
3191
3192 // The first two instructions are generated with labels so as to get the
3193 // offset fixed up correctly by the bind(Label*) call. We patch it back and
3194 // forth between a compare instructions (a nop in this position) and the
3195 // real branch when we start and stop incremental heap marking.
3196 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
3197 __ jmp(&skip_to_incremental_compacting, Label::kFar);
3198
3199 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3200 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3201 MacroAssembler::kReturnAtEnd);
3202 } else {
3203 __ ret(0);
3204 }
3205
3206 __ bind(&skip_to_incremental_noncompacting);
3207 GenerateIncremental(masm, INCREMENTAL);
3208
3209 __ bind(&skip_to_incremental_compacting);
3210 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3211
3212 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3213 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
3214 masm->set_byte_at(0, kTwoByteNopInstruction);
3215 masm->set_byte_at(2, kFiveByteNopInstruction);
3216}
3217
3218
3219void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3220 regs_.Save(masm);
3221
3222 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3223 Label dont_need_remembered_set;
3224
3225 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3226 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
3227 regs_.scratch0(),
3228 &dont_need_remembered_set);
3229
Ben Murdoch097c5b22016-05-18 11:27:45 +01003230 __ JumpIfInNewSpace(regs_.object(), regs_.scratch0(),
3231 &dont_need_remembered_set);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003232
3233 // First notify the incremental marker if necessary, then update the
3234 // remembered set.
3235 CheckNeedsToInformIncrementalMarker(
3236 masm,
3237 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
3238 mode);
3239 InformIncrementalMarker(masm);
3240 regs_.Restore(masm);
3241 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3242 MacroAssembler::kReturnAtEnd);
3243
3244 __ bind(&dont_need_remembered_set);
3245 }
3246
3247 CheckNeedsToInformIncrementalMarker(
3248 masm,
3249 kReturnOnNoNeedToInformIncrementalMarker,
3250 mode);
3251 InformIncrementalMarker(masm);
3252 regs_.Restore(masm);
3253 __ ret(0);
3254}
3255
3256
3257void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3258 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
3259 int argument_count = 3;
3260 __ PrepareCallCFunction(argument_count, regs_.scratch0());
3261 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
3262 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
3263 __ mov(Operand(esp, 2 * kPointerSize),
3264 Immediate(ExternalReference::isolate_address(isolate())));
3265
3266 AllowExternalCallThatCantCauseGC scope(masm);
3267 __ CallCFunction(
3268 ExternalReference::incremental_marking_record_write_function(isolate()),
3269 argument_count);
3270
3271 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
3272}
3273
3274
3275void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3276 MacroAssembler* masm,
3277 OnNoNeedToInformIncrementalMarker on_no_need,
3278 Mode mode) {
3279 Label object_is_black, need_incremental, need_incremental_pop_object;
3280
3281 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
3282 __ and_(regs_.scratch0(), regs_.object());
3283 __ mov(regs_.scratch1(),
3284 Operand(regs_.scratch0(),
3285 MemoryChunk::kWriteBarrierCounterOffset));
3286 __ sub(regs_.scratch1(), Immediate(1));
3287 __ mov(Operand(regs_.scratch0(),
3288 MemoryChunk::kWriteBarrierCounterOffset),
3289 regs_.scratch1());
3290 __ j(negative, &need_incremental);
3291
3292 // Let's look at the color of the object: If it is not black we don't have
3293 // to inform the incremental marker.
3294 __ JumpIfBlack(regs_.object(),
3295 regs_.scratch0(),
3296 regs_.scratch1(),
3297 &object_is_black,
3298 Label::kNear);
3299
3300 regs_.Restore(masm);
3301 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
3302 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3303 MacroAssembler::kReturnAtEnd);
3304 } else {
3305 __ ret(0);
3306 }
3307
3308 __ bind(&object_is_black);
3309
3310 // Get the value from the slot.
3311 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3312
3313 if (mode == INCREMENTAL_COMPACTION) {
3314 Label ensure_not_white;
3315
3316 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3317 regs_.scratch1(), // Scratch.
3318 MemoryChunk::kEvacuationCandidateMask,
3319 zero,
3320 &ensure_not_white,
3321 Label::kNear);
3322
3323 __ CheckPageFlag(regs_.object(),
3324 regs_.scratch1(), // Scratch.
3325 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3326 not_zero,
3327 &ensure_not_white,
3328 Label::kNear);
3329
3330 __ jmp(&need_incremental);
3331
3332 __ bind(&ensure_not_white);
3333 }
3334
3335 // We need an extra register for this, so we push the object register
3336 // temporarily.
3337 __ push(regs_.object());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003338 __ JumpIfWhite(regs_.scratch0(), // The value.
3339 regs_.scratch1(), // Scratch.
3340 regs_.object(), // Scratch.
3341 &need_incremental_pop_object, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003342 __ pop(regs_.object());
3343
3344 regs_.Restore(masm);
3345 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
3346 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3347 MacroAssembler::kReturnAtEnd);
3348 } else {
3349 __ ret(0);
3350 }
3351
3352 __ bind(&need_incremental_pop_object);
3353 __ pop(regs_.object());
3354
3355 __ bind(&need_incremental);
3356
3357 // Fall through when we need to inform the incremental marker.
3358}
3359
3360
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003361void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
3362 CEntryStub ces(isolate(), 1, kSaveFPRegs);
3363 __ call(ces.GetCode(), RelocInfo::CODE_TARGET);
3364 int parameter_count_offset =
Ben Murdochda12d292016-06-02 14:46:10 +01003365 StubFailureTrampolineFrameConstants::kArgumentsLengthOffset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003366 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
3367 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
3368 __ pop(ecx);
3369 int additional_offset =
3370 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
3371 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
3372 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
3373}
3374
3375
3376void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003377 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3378 LoadICStub stub(isolate(), state());
3379 stub.GenerateForTrampoline(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003380}
3381
3382
3383void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003384 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3385 KeyedLoadICStub stub(isolate(), state());
3386 stub.GenerateForTrampoline(masm);
3387}
3388
3389
3390static void HandleArrayCases(MacroAssembler* masm, Register receiver,
3391 Register key, Register vector, Register slot,
3392 Register feedback, bool is_polymorphic,
3393 Label* miss) {
3394 // feedback initially contains the feedback array
3395 Label next, next_loop, prepare_next;
3396 Label load_smi_map, compare_map;
3397 Label start_polymorphic;
3398
3399 __ push(receiver);
3400 __ push(vector);
3401
3402 Register receiver_map = receiver;
3403 Register cached_map = vector;
3404
3405 // Receiver might not be a heap object.
3406 __ JumpIfSmi(receiver, &load_smi_map);
3407 __ mov(receiver_map, FieldOperand(receiver, 0));
3408 __ bind(&compare_map);
3409 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3410
3411 // A named keyed load might have a 2 element array, all other cases can count
3412 // on an array with at least 2 {map, handler} pairs, so they can go right
3413 // into polymorphic array handling.
3414 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3415 __ j(not_equal, is_polymorphic ? &start_polymorphic : &next);
3416
3417 // found, now call handler.
3418 Register handler = feedback;
3419 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3420 __ pop(vector);
3421 __ pop(receiver);
3422 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3423 __ jmp(handler);
3424
3425 if (!is_polymorphic) {
3426 __ bind(&next);
3427 __ cmp(FieldOperand(feedback, FixedArray::kLengthOffset),
3428 Immediate(Smi::FromInt(2)));
3429 __ j(not_equal, &start_polymorphic);
3430 __ pop(vector);
3431 __ pop(receiver);
3432 __ jmp(miss);
3433 }
3434
3435 // Polymorphic, we have to loop from 2 to N
3436 __ bind(&start_polymorphic);
3437 __ push(key);
3438 Register counter = key;
3439 __ mov(counter, Immediate(Smi::FromInt(2)));
3440 __ bind(&next_loop);
3441 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3442 FixedArray::kHeaderSize));
3443 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3444 __ j(not_equal, &prepare_next);
3445 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
3446 FixedArray::kHeaderSize + kPointerSize));
3447 __ pop(key);
3448 __ pop(vector);
3449 __ pop(receiver);
3450 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3451 __ jmp(handler);
3452
3453 __ bind(&prepare_next);
3454 __ add(counter, Immediate(Smi::FromInt(2)));
3455 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3456 __ j(less, &next_loop);
3457
3458 // We exhausted our array of map handler pairs.
3459 __ pop(key);
3460 __ pop(vector);
3461 __ pop(receiver);
3462 __ jmp(miss);
3463
3464 __ bind(&load_smi_map);
3465 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3466 __ jmp(&compare_map);
3467}
3468
3469
3470static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
3471 Register key, Register vector, Register slot,
3472 Register weak_cell, Label* miss) {
3473 // feedback initially contains the feedback array
3474 Label compare_smi_map;
3475
3476 // Move the weak map into the weak_cell register.
3477 Register ic_map = weak_cell;
3478 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
3479
3480 // Receiver might not be a heap object.
3481 __ JumpIfSmi(receiver, &compare_smi_map);
3482 __ cmp(ic_map, FieldOperand(receiver, 0));
3483 __ j(not_equal, miss);
3484 Register handler = weak_cell;
3485 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
3486 FixedArray::kHeaderSize + kPointerSize));
3487 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3488 __ jmp(handler);
3489
3490 // In microbenchmarks, it made sense to unroll this code so that the call to
3491 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
3492 __ bind(&compare_smi_map);
3493 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
3494 __ j(not_equal, miss);
3495 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
3496 FixedArray::kHeaderSize + kPointerSize));
3497 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3498 __ jmp(handler);
3499}
3500
3501
3502void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
3503
3504
3505void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3506 GenerateImpl(masm, true);
3507}
3508
3509
3510void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3511 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
3512 Register name = LoadWithVectorDescriptor::NameRegister(); // ecx
3513 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
3514 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
3515 Register scratch = edi;
3516 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3517 FixedArray::kHeaderSize));
3518
3519 // Is it a weak cell?
3520 Label try_array;
3521 Label not_array, smi_key, key_okay, miss;
3522 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3523 __ j(not_equal, &try_array);
3524 HandleMonomorphicCase(masm, receiver, name, vector, slot, scratch, &miss);
3525
3526 // Is it a fixed array?
3527 __ bind(&try_array);
3528 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3529 __ j(not_equal, &not_array);
3530 HandleArrayCases(masm, receiver, name, vector, slot, scratch, true, &miss);
3531
3532 __ bind(&not_array);
3533 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3534 __ j(not_equal, &miss);
3535 __ push(slot);
3536 __ push(vector);
3537 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
3538 Code::ComputeHandlerFlags(Code::LOAD_IC));
3539 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
3540 receiver, name, vector, scratch);
3541 __ pop(vector);
3542 __ pop(slot);
3543
3544 __ bind(&miss);
3545 LoadIC::GenerateMiss(masm);
3546}
3547
3548
3549void KeyedLoadICStub::Generate(MacroAssembler* masm) {
3550 GenerateImpl(masm, false);
3551}
3552
3553
3554void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3555 GenerateImpl(masm, true);
3556}
3557
3558
3559void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3560 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
3561 Register key = LoadWithVectorDescriptor::NameRegister(); // ecx
3562 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
3563 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
3564 Register feedback = edi;
3565 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
3566 FixedArray::kHeaderSize));
3567 // Is it a weak cell?
3568 Label try_array;
3569 Label not_array, smi_key, key_okay, miss;
3570 __ CompareRoot(FieldOperand(feedback, 0), Heap::kWeakCellMapRootIndex);
3571 __ j(not_equal, &try_array);
3572 HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, &miss);
3573
3574 __ bind(&try_array);
3575 // Is it a fixed array?
3576 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
3577 __ j(not_equal, &not_array);
3578
3579 // We have a polymorphic element handler.
3580 Label polymorphic, try_poly_name;
3581 __ bind(&polymorphic);
3582 HandleArrayCases(masm, receiver, key, vector, slot, feedback, true, &miss);
3583
3584 __ bind(&not_array);
3585 // Is it generic?
3586 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
3587 __ j(not_equal, &try_poly_name);
3588 Handle<Code> megamorphic_stub =
3589 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3590 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
3591
3592 __ bind(&try_poly_name);
3593 // We might have a name in feedback, and a fixed array in the next slot.
3594 __ cmp(key, feedback);
3595 __ j(not_equal, &miss);
3596 // If the name comparison succeeded, we know we have a fixed array with
3597 // at least one map/handler pair.
3598 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
3599 FixedArray::kHeaderSize + kPointerSize));
3600 HandleArrayCases(masm, receiver, key, vector, slot, feedback, false, &miss);
3601
3602 __ bind(&miss);
3603 KeyedLoadIC::GenerateMiss(masm);
3604}
3605
3606
3607void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3608 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3609 VectorStoreICStub stub(isolate(), state());
3610 stub.GenerateForTrampoline(masm);
3611}
3612
3613
3614void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3615 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3616 VectorKeyedStoreICStub stub(isolate(), state());
3617 stub.GenerateForTrampoline(masm);
3618}
3619
3620
3621void VectorStoreICStub::Generate(MacroAssembler* masm) {
3622 GenerateImpl(masm, false);
3623}
3624
3625
3626void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3627 GenerateImpl(masm, true);
3628}
3629
3630
3631// value is on the stack already.
3632static void HandlePolymorphicStoreCase(MacroAssembler* masm, Register receiver,
3633 Register key, Register vector,
3634 Register slot, Register feedback,
3635 bool is_polymorphic, Label* miss) {
3636 // feedback initially contains the feedback array
3637 Label next, next_loop, prepare_next;
3638 Label load_smi_map, compare_map;
3639 Label start_polymorphic;
3640 Label pop_and_miss;
3641 ExternalReference virtual_register =
3642 ExternalReference::virtual_handler_register(masm->isolate());
3643
3644 __ push(receiver);
3645 __ push(vector);
3646
3647 Register receiver_map = receiver;
3648 Register cached_map = vector;
3649
3650 // Receiver might not be a heap object.
3651 __ JumpIfSmi(receiver, &load_smi_map);
3652 __ mov(receiver_map, FieldOperand(receiver, 0));
3653 __ bind(&compare_map);
3654 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3655
3656 // A named keyed store might have a 2 element array, all other cases can count
3657 // on an array with at least 2 {map, handler} pairs, so they can go right
3658 // into polymorphic array handling.
3659 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3660 __ j(not_equal, &start_polymorphic);
3661
3662 // found, now call handler.
3663 Register handler = feedback;
3664 DCHECK(handler.is(VectorStoreICDescriptor::ValueRegister()));
3665 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3666 __ pop(vector);
3667 __ pop(receiver);
3668 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3669 __ mov(Operand::StaticVariable(virtual_register), handler);
3670 __ pop(handler); // Pop "value".
3671 __ jmp(Operand::StaticVariable(virtual_register));
3672
3673 // Polymorphic, we have to loop from 2 to N
3674 __ bind(&start_polymorphic);
3675 __ push(key);
3676 Register counter = key;
3677 __ mov(counter, Immediate(Smi::FromInt(2)));
3678
3679 if (!is_polymorphic) {
3680 // If is_polymorphic is false, we may only have a two element array.
3681 // Check against length now in that case.
3682 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3683 __ j(greater_equal, &pop_and_miss);
3684 }
3685
3686 __ bind(&next_loop);
3687 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3688 FixedArray::kHeaderSize));
3689 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3690 __ j(not_equal, &prepare_next);
3691 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
3692 FixedArray::kHeaderSize + kPointerSize));
3693 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3694 __ pop(key);
3695 __ pop(vector);
3696 __ pop(receiver);
3697 __ mov(Operand::StaticVariable(virtual_register), handler);
3698 __ pop(handler); // Pop "value".
3699 __ jmp(Operand::StaticVariable(virtual_register));
3700
3701 __ bind(&prepare_next);
3702 __ add(counter, Immediate(Smi::FromInt(2)));
3703 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3704 __ j(less, &next_loop);
3705
3706 // We exhausted our array of map handler pairs.
3707 __ bind(&pop_and_miss);
3708 __ pop(key);
3709 __ pop(vector);
3710 __ pop(receiver);
3711 __ jmp(miss);
3712
3713 __ bind(&load_smi_map);
3714 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3715 __ jmp(&compare_map);
3716}
3717
3718
3719static void HandleMonomorphicStoreCase(MacroAssembler* masm, Register receiver,
3720 Register key, Register vector,
3721 Register slot, Register weak_cell,
3722 Label* miss) {
3723 // The store ic value is on the stack.
3724 DCHECK(weak_cell.is(VectorStoreICDescriptor::ValueRegister()));
3725 ExternalReference virtual_register =
3726 ExternalReference::virtual_handler_register(masm->isolate());
3727
3728 // feedback initially contains the feedback array
3729 Label compare_smi_map;
3730
3731 // Move the weak map into the weak_cell register.
3732 Register ic_map = weak_cell;
3733 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
3734
3735 // Receiver might not be a heap object.
3736 __ JumpIfSmi(receiver, &compare_smi_map);
3737 __ cmp(ic_map, FieldOperand(receiver, 0));
3738 __ j(not_equal, miss);
3739 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
3740 FixedArray::kHeaderSize + kPointerSize));
3741 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
3742 // Put the store ic value back in it's register.
3743 __ mov(Operand::StaticVariable(virtual_register), weak_cell);
3744 __ pop(weak_cell); // Pop "value".
3745 // jump to the handler.
3746 __ jmp(Operand::StaticVariable(virtual_register));
3747
3748 // In microbenchmarks, it made sense to unroll this code so that the call to
3749 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
3750 __ bind(&compare_smi_map);
3751 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
3752 __ j(not_equal, miss);
3753 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
3754 FixedArray::kHeaderSize + kPointerSize));
3755 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
3756 __ mov(Operand::StaticVariable(virtual_register), weak_cell);
3757 __ pop(weak_cell); // Pop "value".
3758 // jump to the handler.
3759 __ jmp(Operand::StaticVariable(virtual_register));
3760}
3761
3762
3763void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3764 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
3765 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
3766 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
3767 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
3768 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
3769 Label miss;
3770
3771 __ push(value);
3772
3773 Register scratch = value;
3774 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3775 FixedArray::kHeaderSize));
3776
3777 // Is it a weak cell?
3778 Label try_array;
3779 Label not_array, smi_key, key_okay;
3780 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3781 __ j(not_equal, &try_array);
3782 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
3783
3784 // Is it a fixed array?
3785 __ bind(&try_array);
3786 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3787 __ j(not_equal, &not_array);
3788 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, true,
3789 &miss);
3790
3791 __ bind(&not_array);
3792 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3793 __ j(not_equal, &miss);
3794
3795 __ pop(value);
3796 __ push(slot);
3797 __ push(vector);
3798 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
3799 Code::ComputeHandlerFlags(Code::STORE_IC));
3800 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::STORE_IC, code_flags,
3801 receiver, key, slot, no_reg);
3802 __ pop(vector);
3803 __ pop(slot);
3804 Label no_pop_miss;
3805 __ jmp(&no_pop_miss);
3806
3807 __ bind(&miss);
3808 __ pop(value);
3809 __ bind(&no_pop_miss);
3810 StoreIC::GenerateMiss(masm);
3811}
3812
3813
3814void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
3815 GenerateImpl(masm, false);
3816}
3817
3818
3819void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3820 GenerateImpl(masm, true);
3821}
3822
3823
3824static void HandlePolymorphicKeyedStoreCase(MacroAssembler* masm,
3825 Register receiver, Register key,
3826 Register vector, Register slot,
3827 Register feedback, Label* miss) {
3828 // feedback initially contains the feedback array
3829 Label next, next_loop, prepare_next;
3830 Label load_smi_map, compare_map;
3831 Label transition_call;
3832 Label pop_and_miss;
3833 ExternalReference virtual_register =
3834 ExternalReference::virtual_handler_register(masm->isolate());
3835 ExternalReference virtual_slot =
3836 ExternalReference::virtual_slot_register(masm->isolate());
3837
3838 __ push(receiver);
3839 __ push(vector);
3840
3841 Register receiver_map = receiver;
3842 Register cached_map = vector;
3843 Register value = StoreDescriptor::ValueRegister();
3844
3845 // Receiver might not be a heap object.
3846 __ JumpIfSmi(receiver, &load_smi_map);
3847 __ mov(receiver_map, FieldOperand(receiver, 0));
3848 __ bind(&compare_map);
3849
3850 // Polymorphic, we have to loop from 0 to N - 1
3851 __ push(key);
3852 // Current stack layout:
3853 // - esp[0] -- key
3854 // - esp[4] -- vector
3855 // - esp[8] -- receiver
3856 // - esp[12] -- value
3857 // - esp[16] -- return address
3858 //
3859 // Required stack layout for handler call:
3860 // - esp[0] -- return address
3861 // - receiver, key, value, vector, slot in registers.
3862 // - handler in virtual register.
3863 Register counter = key;
3864 __ mov(counter, Immediate(Smi::FromInt(0)));
3865 __ bind(&next_loop);
3866 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3867 FixedArray::kHeaderSize));
3868 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3869 __ j(not_equal, &prepare_next);
3870 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3871 FixedArray::kHeaderSize + kPointerSize));
3872 __ CompareRoot(cached_map, Heap::kUndefinedValueRootIndex);
3873 __ j(not_equal, &transition_call);
3874 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
3875 FixedArray::kHeaderSize + 2 * kPointerSize));
3876 __ pop(key);
3877 __ pop(vector);
3878 __ pop(receiver);
3879 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
3880 __ mov(Operand::StaticVariable(virtual_register), feedback);
3881 __ pop(value);
3882 __ jmp(Operand::StaticVariable(virtual_register));
3883
3884 __ bind(&transition_call);
3885 // Current stack layout:
3886 // - esp[0] -- key
3887 // - esp[4] -- vector
3888 // - esp[8] -- receiver
3889 // - esp[12] -- value
3890 // - esp[16] -- return address
3891 //
3892 // Required stack layout for handler call:
3893 // - esp[0] -- return address
3894 // - receiver, key, value, map, vector in registers.
3895 // - handler and slot in virtual registers.
3896 __ mov(Operand::StaticVariable(virtual_slot), slot);
3897 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
3898 FixedArray::kHeaderSize + 2 * kPointerSize));
3899 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
3900 __ mov(Operand::StaticVariable(virtual_register), feedback);
3901
3902 __ mov(cached_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3903 // The weak cell may have been cleared.
3904 __ JumpIfSmi(cached_map, &pop_and_miss);
3905 DCHECK(!cached_map.is(VectorStoreTransitionDescriptor::MapRegister()));
3906 __ mov(VectorStoreTransitionDescriptor::MapRegister(), cached_map);
3907
3908 // Pop key into place.
3909 __ pop(key);
3910 __ pop(vector);
3911 __ pop(receiver);
3912 __ pop(value);
3913 __ jmp(Operand::StaticVariable(virtual_register));
3914
3915 __ bind(&prepare_next);
3916 __ add(counter, Immediate(Smi::FromInt(3)));
3917 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3918 __ j(less, &next_loop);
3919
3920 // We exhausted our array of map handler pairs.
3921 __ bind(&pop_and_miss);
3922 __ pop(key);
3923 __ pop(vector);
3924 __ pop(receiver);
3925 __ jmp(miss);
3926
3927 __ bind(&load_smi_map);
3928 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3929 __ jmp(&compare_map);
3930}
3931
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003932void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3933 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
3934 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
3935 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
3936 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
3937 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
3938 Label miss;
3939
3940 __ push(value);
3941
3942 Register scratch = value;
3943 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3944 FixedArray::kHeaderSize));
3945
3946 // Is it a weak cell?
3947 Label try_array;
3948 Label not_array, smi_key, key_okay;
3949 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3950 __ j(not_equal, &try_array);
3951 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
3952
3953 // Is it a fixed array?
3954 __ bind(&try_array);
3955 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3956 __ j(not_equal, &not_array);
3957 HandlePolymorphicKeyedStoreCase(masm, receiver, key, vector, slot, scratch,
3958 &miss);
3959
3960 __ bind(&not_array);
3961 Label try_poly_name;
3962 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3963 __ j(not_equal, &try_poly_name);
3964
3965 __ pop(value);
3966
3967 Handle<Code> megamorphic_stub =
3968 KeyedStoreIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3969 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
3970
3971 __ bind(&try_poly_name);
3972 // We might have a name in feedback, and a fixed array in the next slot.
3973 __ cmp(key, scratch);
3974 __ j(not_equal, &miss);
3975 // If the name comparison succeeded, we know we have a fixed array with
3976 // at least one map/handler pair.
3977 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3978 FixedArray::kHeaderSize + kPointerSize));
3979 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, false,
3980 &miss);
3981
3982 __ bind(&miss);
3983 __ pop(value);
3984 KeyedStoreIC::GenerateMiss(masm);
3985}
3986
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003987void CallICTrampolineStub::Generate(MacroAssembler* masm) {
3988 __ EmitLoadTypeFeedbackVector(ebx);
3989 CallICStub stub(isolate(), state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003990 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3991}
3992
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003993void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
3994 if (masm->isolate()->function_entry_hook() != NULL) {
3995 ProfileEntryHookStub stub(masm->isolate());
3996 masm->CallStub(&stub);
3997 }
3998}
3999
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004000void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4001 // Save volatile registers.
4002 const int kNumSavedRegisters = 3;
4003 __ push(eax);
4004 __ push(ecx);
4005 __ push(edx);
4006
4007 // Calculate and push the original stack pointer.
4008 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4009 __ push(eax);
4010
4011 // Retrieve our return address and use it to calculate the calling
4012 // function's address.
4013 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
4014 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
4015 __ push(eax);
4016
4017 // Call the entry hook.
4018 DCHECK(isolate()->function_entry_hook() != NULL);
4019 __ call(FUNCTION_ADDR(isolate()->function_entry_hook()),
4020 RelocInfo::RUNTIME_ENTRY);
4021 __ add(esp, Immediate(2 * kPointerSize));
4022
4023 // Restore ecx.
4024 __ pop(edx);
4025 __ pop(ecx);
4026 __ pop(eax);
4027
4028 __ ret(0);
4029}
4030
Ben Murdoch097c5b22016-05-18 11:27:45 +01004031template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004032static void CreateArrayDispatch(MacroAssembler* masm,
4033 AllocationSiteOverrideMode mode) {
4034 if (mode == DISABLE_ALLOCATION_SITES) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004035 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004036 __ TailCallStub(&stub);
4037 } else if (mode == DONT_OVERRIDE) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004038 int last_index =
4039 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004040 for (int i = 0; i <= last_index; ++i) {
4041 Label next;
4042 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4043 __ cmp(edx, kind);
4044 __ j(not_equal, &next);
4045 T stub(masm->isolate(), kind);
4046 __ TailCallStub(&stub);
4047 __ bind(&next);
4048 }
4049
4050 // If we reached this point there is a problem.
4051 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4052 } else {
4053 UNREACHABLE();
4054 }
4055}
4056
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004057static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4058 AllocationSiteOverrideMode mode) {
4059 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4060 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
4061 // eax - number of arguments
4062 // edi - constructor?
4063 // esp[0] - return address
4064 // esp[4] - last argument
4065 Label normal_sequence;
4066 if (mode == DONT_OVERRIDE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004067 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4068 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4069 STATIC_ASSERT(FAST_ELEMENTS == 2);
4070 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4071 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4072 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004073
4074 // is the low bit set? If so, we are holey and that is good.
Ben Murdochda12d292016-06-02 14:46:10 +01004075 __ test_b(edx, Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004076 __ j(not_zero, &normal_sequence);
4077 }
4078
4079 // look at the first argument
4080 __ mov(ecx, Operand(esp, kPointerSize));
4081 __ test(ecx, ecx);
4082 __ j(zero, &normal_sequence);
4083
4084 if (mode == DISABLE_ALLOCATION_SITES) {
4085 ElementsKind initial = GetInitialFastElementsKind();
4086 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4087
Ben Murdoch097c5b22016-05-18 11:27:45 +01004088 ArraySingleArgumentConstructorStub stub_holey(
4089 masm->isolate(), holey_initial, DISABLE_ALLOCATION_SITES);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004090 __ TailCallStub(&stub_holey);
4091
4092 __ bind(&normal_sequence);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004093 ArraySingleArgumentConstructorStub stub(masm->isolate(), initial,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004094 DISABLE_ALLOCATION_SITES);
4095 __ TailCallStub(&stub);
4096 } else if (mode == DONT_OVERRIDE) {
4097 // We are going to create a holey array, but our kind is non-holey.
4098 // Fix kind and retry.
4099 __ inc(edx);
4100
4101 if (FLAG_debug_code) {
4102 Handle<Map> allocation_site_map =
4103 masm->isolate()->factory()->allocation_site_map();
4104 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
4105 __ Assert(equal, kExpectedAllocationSite);
4106 }
4107
4108 // Save the resulting elements kind in type info. We can't just store r3
4109 // in the AllocationSite::transition_info field because elements kind is
4110 // restricted to a portion of the field...upper bits need to be left alone.
4111 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4112 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
4113 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
4114
4115 __ bind(&normal_sequence);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004116 int last_index =
4117 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004118 for (int i = 0; i <= last_index; ++i) {
4119 Label next;
4120 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4121 __ cmp(edx, kind);
4122 __ j(not_equal, &next);
4123 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4124 __ TailCallStub(&stub);
4125 __ bind(&next);
4126 }
4127
4128 // If we reached this point there is a problem.
4129 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4130 } else {
4131 UNREACHABLE();
4132 }
4133}
4134
Ben Murdoch097c5b22016-05-18 11:27:45 +01004135template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004136static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004137 int to_index =
4138 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004139 for (int i = 0; i <= to_index; ++i) {
4140 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4141 T stub(isolate, kind);
4142 stub.GetCode();
4143 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4144 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4145 stub1.GetCode();
4146 }
4147 }
4148}
4149
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004150void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4151 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4152 isolate);
4153 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4154 isolate);
4155 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4156 isolate);
4157}
4158
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004159void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4160 Isolate* isolate) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004161 ElementsKind kinds[2] = {FAST_ELEMENTS, FAST_HOLEY_ELEMENTS};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004162 for (int i = 0; i < 2; i++) {
4163 // For internal arrays we only need a few things
4164 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4165 stubh1.GetCode();
4166 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4167 stubh2.GetCode();
4168 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4169 stubh3.GetCode();
4170 }
4171}
4172
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004173void ArrayConstructorStub::GenerateDispatchToArrayStub(
Ben Murdoch097c5b22016-05-18 11:27:45 +01004174 MacroAssembler* masm, AllocationSiteOverrideMode mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004175 if (argument_count() == ANY) {
4176 Label not_zero_case, not_one_case;
4177 __ test(eax, eax);
4178 __ j(not_zero, &not_zero_case);
4179 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4180
4181 __ bind(&not_zero_case);
4182 __ cmp(eax, 1);
4183 __ j(greater, &not_one_case);
4184 CreateArrayDispatchOneArgument(masm, mode);
4185
4186 __ bind(&not_one_case);
4187 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4188 } else if (argument_count() == NONE) {
4189 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4190 } else if (argument_count() == ONE) {
4191 CreateArrayDispatchOneArgument(masm, mode);
4192 } else if (argument_count() == MORE_THAN_ONE) {
4193 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4194 } else {
4195 UNREACHABLE();
4196 }
4197}
4198
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004199void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4200 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004201 // -- eax : argc (only if argument_count() is ANY or MORE_THAN_ONE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004202 // -- ebx : AllocationSite or undefined
4203 // -- edi : constructor
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004204 // -- edx : Original constructor
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004205 // -- esp[0] : return address
4206 // -- esp[4] : last argument
4207 // -----------------------------------
4208 if (FLAG_debug_code) {
4209 // The array construct code is only set for the global and natives
4210 // builtin Array functions which always have maps.
4211
4212 // Initial map for the builtin Array function should be a map.
4213 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4214 // Will both indicate a NULL and a Smi.
4215 __ test(ecx, Immediate(kSmiTagMask));
4216 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4217 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4218 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4219
4220 // We should either have undefined in ebx or a valid AllocationSite
4221 __ AssertUndefinedOrAllocationSite(ebx);
4222 }
4223
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004224 Label subclassing;
4225
4226 // Enter the context of the Array function.
4227 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
4228
4229 __ cmp(edx, edi);
4230 __ j(not_equal, &subclassing);
4231
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004232 Label no_info;
4233 // If the feedback vector is the undefined value call an array constructor
4234 // that doesn't use AllocationSites.
4235 __ cmp(ebx, isolate()->factory()->undefined_value());
4236 __ j(equal, &no_info);
4237
4238 // Only look at the lower 16 bits of the transition info.
4239 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
4240 __ SmiUntag(edx);
4241 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4242 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
4243 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4244
4245 __ bind(&no_info);
4246 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004247
4248 // Subclassing.
4249 __ bind(&subclassing);
4250 switch (argument_count()) {
4251 case ANY:
4252 case MORE_THAN_ONE:
4253 __ mov(Operand(esp, eax, times_pointer_size, kPointerSize), edi);
4254 __ add(eax, Immediate(3));
4255 break;
4256 case NONE:
4257 __ mov(Operand(esp, 1 * kPointerSize), edi);
4258 __ mov(eax, Immediate(3));
4259 break;
4260 case ONE:
4261 __ mov(Operand(esp, 2 * kPointerSize), edi);
4262 __ mov(eax, Immediate(4));
4263 break;
4264 }
4265 __ PopReturnAddressTo(ecx);
4266 __ Push(edx);
4267 __ Push(ebx);
4268 __ PushReturnAddressFrom(ecx);
4269 __ JumpToExternalReference(ExternalReference(Runtime::kNewArray, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004270}
4271
Ben Murdoch097c5b22016-05-18 11:27:45 +01004272void InternalArrayConstructorStub::GenerateCase(MacroAssembler* masm,
4273 ElementsKind kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004274 Label not_zero_case, not_one_case;
4275 Label normal_sequence;
4276
4277 __ test(eax, eax);
4278 __ j(not_zero, &not_zero_case);
4279 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4280 __ TailCallStub(&stub0);
4281
4282 __ bind(&not_zero_case);
4283 __ cmp(eax, 1);
4284 __ j(greater, &not_one_case);
4285
4286 if (IsFastPackedElementsKind(kind)) {
4287 // We might need to create a holey array
4288 // look at the first argument
4289 __ mov(ecx, Operand(esp, kPointerSize));
4290 __ test(ecx, ecx);
4291 __ j(zero, &normal_sequence);
4292
Ben Murdoch097c5b22016-05-18 11:27:45 +01004293 InternalArraySingleArgumentConstructorStub stub1_holey(
4294 isolate(), GetHoleyElementsKind(kind));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004295 __ TailCallStub(&stub1_holey);
4296 }
4297
4298 __ bind(&normal_sequence);
4299 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4300 __ TailCallStub(&stub1);
4301
4302 __ bind(&not_one_case);
4303 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4304 __ TailCallStub(&stubN);
4305}
4306
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004307void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4308 // ----------- S t a t e -------------
4309 // -- eax : argc
4310 // -- edi : constructor
4311 // -- esp[0] : return address
4312 // -- esp[4] : last argument
4313 // -----------------------------------
4314
4315 if (FLAG_debug_code) {
4316 // The array construct code is only set for the global and natives
4317 // builtin Array functions which always have maps.
4318
4319 // Initial map for the builtin Array function should be a map.
4320 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4321 // Will both indicate a NULL and a Smi.
4322 __ test(ecx, Immediate(kSmiTagMask));
4323 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4324 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4325 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4326 }
4327
4328 // Figure out the right elements kind
4329 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4330
4331 // Load the map's "bit field 2" into |result|. We only need the first byte,
4332 // but the following masking takes care of that anyway.
4333 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
4334 // Retrieve elements_kind from bit field 2.
4335 __ DecodeField<Map::ElementsKindBits>(ecx);
4336
4337 if (FLAG_debug_code) {
4338 Label done;
4339 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4340 __ j(equal, &done);
4341 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004342 __ Assert(equal, kInvalidElementsKindForInternalArrayOrInternalPackedArray);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004343 __ bind(&done);
4344 }
4345
4346 Label fast_elements_case;
4347 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4348 __ j(equal, &fast_elements_case);
4349 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4350
4351 __ bind(&fast_elements_case);
4352 GenerateCase(masm, FAST_ELEMENTS);
4353}
4354
Ben Murdoch097c5b22016-05-18 11:27:45 +01004355void FastNewObjectStub::Generate(MacroAssembler* masm) {
4356 // ----------- S t a t e -------------
4357 // -- edi : target
4358 // -- edx : new target
4359 // -- esi : context
4360 // -- esp[0] : return address
4361 // -----------------------------------
4362 __ AssertFunction(edi);
4363 __ AssertReceiver(edx);
4364
4365 // Verify that the new target is a JSFunction.
4366 Label new_object;
4367 __ CmpObjectType(edx, JS_FUNCTION_TYPE, ebx);
4368 __ j(not_equal, &new_object);
4369
4370 // Load the initial map and verify that it's in fact a map.
4371 __ mov(ecx, FieldOperand(edx, JSFunction::kPrototypeOrInitialMapOffset));
4372 __ JumpIfSmi(ecx, &new_object);
4373 __ CmpObjectType(ecx, MAP_TYPE, ebx);
4374 __ j(not_equal, &new_object);
4375
4376 // Fall back to runtime if the target differs from the new target's
4377 // initial map constructor.
4378 __ cmp(edi, FieldOperand(ecx, Map::kConstructorOrBackPointerOffset));
4379 __ j(not_equal, &new_object);
4380
4381 // Allocate the JSObject on the heap.
4382 Label allocate, done_allocate;
4383 __ movzx_b(ebx, FieldOperand(ecx, Map::kInstanceSizeOffset));
4384 __ lea(ebx, Operand(ebx, times_pointer_size, 0));
4385 __ Allocate(ebx, eax, edi, no_reg, &allocate, NO_ALLOCATION_FLAGS);
4386 __ bind(&done_allocate);
4387
4388 // Initialize the JSObject fields.
4389 __ mov(Operand(eax, JSObject::kMapOffset), ecx);
4390 __ mov(Operand(eax, JSObject::kPropertiesOffset),
4391 masm->isolate()->factory()->empty_fixed_array());
4392 __ mov(Operand(eax, JSObject::kElementsOffset),
4393 masm->isolate()->factory()->empty_fixed_array());
4394 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
4395 __ lea(ebx, Operand(eax, JSObject::kHeaderSize));
4396
4397 // ----------- S t a t e -------------
4398 // -- eax : result (untagged)
4399 // -- ebx : result fields (untagged)
4400 // -- edi : result end (untagged)
4401 // -- ecx : initial map
4402 // -- esi : context
4403 // -- esp[0] : return address
4404 // -----------------------------------
4405
4406 // Perform in-object slack tracking if requested.
4407 Label slack_tracking;
4408 STATIC_ASSERT(Map::kNoSlackTracking == 0);
4409 __ test(FieldOperand(ecx, Map::kBitField3Offset),
4410 Immediate(Map::ConstructionCounter::kMask));
4411 __ j(not_zero, &slack_tracking, Label::kNear);
4412 {
4413 // Initialize all in-object fields with undefined.
4414 __ LoadRoot(edx, Heap::kUndefinedValueRootIndex);
4415 __ InitializeFieldsWithFiller(ebx, edi, edx);
4416
4417 // Add the object tag to make the JSObject real.
4418 STATIC_ASSERT(kHeapObjectTag == 1);
4419 __ inc(eax);
4420 __ Ret();
4421 }
4422 __ bind(&slack_tracking);
4423 {
4424 // Decrease generous allocation count.
4425 STATIC_ASSERT(Map::ConstructionCounter::kNext == 32);
4426 __ sub(FieldOperand(ecx, Map::kBitField3Offset),
4427 Immediate(1 << Map::ConstructionCounter::kShift));
4428
4429 // Initialize the in-object fields with undefined.
4430 __ movzx_b(edx, FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset));
4431 __ neg(edx);
4432 __ lea(edx, Operand(edi, edx, times_pointer_size, 0));
4433 __ LoadRoot(edi, Heap::kUndefinedValueRootIndex);
4434 __ InitializeFieldsWithFiller(ebx, edx, edi);
4435
4436 // Initialize the remaining (reserved) fields with one pointer filler map.
4437 __ movzx_b(edx, FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset));
4438 __ lea(edx, Operand(ebx, edx, times_pointer_size, 0));
4439 __ LoadRoot(edi, Heap::kOnePointerFillerMapRootIndex);
4440 __ InitializeFieldsWithFiller(ebx, edx, edi);
4441
4442 // Add the object tag to make the JSObject real.
4443 STATIC_ASSERT(kHeapObjectTag == 1);
4444 __ inc(eax);
4445
4446 // Check if we can finalize the instance size.
4447 Label finalize;
4448 STATIC_ASSERT(Map::kSlackTrackingCounterEnd == 1);
4449 __ test(FieldOperand(ecx, Map::kBitField3Offset),
4450 Immediate(Map::ConstructionCounter::kMask));
4451 __ j(zero, &finalize, Label::kNear);
4452 __ Ret();
4453
4454 // Finalize the instance size.
4455 __ bind(&finalize);
4456 {
4457 FrameScope scope(masm, StackFrame::INTERNAL);
4458 __ Push(eax);
4459 __ Push(ecx);
4460 __ CallRuntime(Runtime::kFinalizeInstanceSize);
4461 __ Pop(eax);
4462 }
4463 __ Ret();
4464 }
4465
4466 // Fall back to %AllocateInNewSpace.
4467 __ bind(&allocate);
4468 {
4469 FrameScope scope(masm, StackFrame::INTERNAL);
4470 __ SmiTag(ebx);
4471 __ Push(ecx);
4472 __ Push(ebx);
4473 __ CallRuntime(Runtime::kAllocateInNewSpace);
4474 __ Pop(ecx);
4475 }
4476 STATIC_ASSERT(kHeapObjectTag == 1);
4477 __ dec(eax);
4478 __ movzx_b(ebx, FieldOperand(ecx, Map::kInstanceSizeOffset));
4479 __ lea(edi, Operand(eax, ebx, times_pointer_size, 0));
4480 __ jmp(&done_allocate);
4481
4482 // Fall back to %NewObject.
4483 __ bind(&new_object);
4484 __ PopReturnAddressTo(ecx);
4485 __ Push(edi);
4486 __ Push(edx);
4487 __ PushReturnAddressFrom(ecx);
4488 __ TailCallRuntime(Runtime::kNewObject);
4489}
4490
4491void FastNewRestParameterStub::Generate(MacroAssembler* masm) {
4492 // ----------- S t a t e -------------
4493 // -- edi : function
4494 // -- esi : context
4495 // -- ebp : frame pointer
4496 // -- esp[0] : return address
4497 // -----------------------------------
4498 __ AssertFunction(edi);
4499
4500 // For Ignition we need to skip all possible handler/stub frames until
4501 // we reach the JavaScript frame for the function (similar to what the
4502 // runtime fallback implementation does). So make edx point to that
4503 // JavaScript frame.
4504 {
4505 Label loop, loop_entry;
4506 __ mov(edx, ebp);
4507 __ jmp(&loop_entry, Label::kNear);
4508 __ bind(&loop);
4509 __ mov(edx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
4510 __ bind(&loop_entry);
Ben Murdochda12d292016-06-02 14:46:10 +01004511 __ cmp(edi, Operand(edx, StandardFrameConstants::kFunctionOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004512 __ j(not_equal, &loop);
4513 }
4514
4515 // Check if we have rest parameters (only possible if we have an
4516 // arguments adaptor frame below the function frame).
4517 Label no_rest_parameters;
4518 __ mov(ebx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004519 __ cmp(Operand(ebx, CommonFrameConstants::kContextOrFrameTypeOffset),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004520 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4521 __ j(not_equal, &no_rest_parameters, Label::kNear);
4522
4523 // Check if the arguments adaptor frame contains more arguments than
4524 // specified by the function's internal formal parameter count.
4525 Label rest_parameters;
4526 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4527 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4528 __ sub(eax,
4529 FieldOperand(ecx, SharedFunctionInfo::kFormalParameterCountOffset));
4530 __ j(greater, &rest_parameters);
4531
4532 // Return an empty rest parameter array.
4533 __ bind(&no_rest_parameters);
4534 {
4535 // ----------- S t a t e -------------
4536 // -- esi : context
4537 // -- esp[0] : return address
4538 // -----------------------------------
4539
4540 // Allocate an empty rest parameter array.
4541 Label allocate, done_allocate;
4542 __ Allocate(JSArray::kSize, eax, edx, ecx, &allocate, TAG_OBJECT);
4543 __ bind(&done_allocate);
4544
4545 // Setup the rest parameter array in rax.
4546 __ LoadGlobalFunction(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, ecx);
4547 __ mov(FieldOperand(eax, JSArray::kMapOffset), ecx);
4548 __ mov(ecx, isolate()->factory()->empty_fixed_array());
4549 __ mov(FieldOperand(eax, JSArray::kPropertiesOffset), ecx);
4550 __ mov(FieldOperand(eax, JSArray::kElementsOffset), ecx);
4551 __ mov(FieldOperand(eax, JSArray::kLengthOffset),
4552 Immediate(Smi::FromInt(0)));
4553 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4554 __ Ret();
4555
4556 // Fall back to %AllocateInNewSpace.
4557 __ bind(&allocate);
4558 {
4559 FrameScope scope(masm, StackFrame::INTERNAL);
4560 __ Push(Smi::FromInt(JSArray::kSize));
4561 __ CallRuntime(Runtime::kAllocateInNewSpace);
4562 }
4563 __ jmp(&done_allocate);
4564 }
4565
4566 __ bind(&rest_parameters);
4567 {
4568 // Compute the pointer to the first rest parameter (skippping the receiver).
4569 __ lea(ebx,
4570 Operand(ebx, eax, times_half_pointer_size,
4571 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4572
4573 // ----------- S t a t e -------------
4574 // -- esi : context
4575 // -- eax : number of rest parameters (tagged)
4576 // -- ebx : pointer to first rest parameters
4577 // -- esp[0] : return address
4578 // -----------------------------------
4579
4580 // Allocate space for the rest parameter array plus the backing store.
4581 Label allocate, done_allocate;
4582 __ lea(ecx, Operand(eax, times_half_pointer_size,
4583 JSArray::kSize + FixedArray::kHeaderSize));
4584 __ Allocate(ecx, edx, edi, no_reg, &allocate, TAG_OBJECT);
4585 __ bind(&done_allocate);
4586
4587 // Setup the elements array in edx.
4588 __ mov(FieldOperand(edx, FixedArray::kMapOffset),
4589 isolate()->factory()->fixed_array_map());
4590 __ mov(FieldOperand(edx, FixedArray::kLengthOffset), eax);
4591 {
4592 Label loop, done_loop;
4593 __ Move(ecx, Smi::FromInt(0));
4594 __ bind(&loop);
4595 __ cmp(ecx, eax);
4596 __ j(equal, &done_loop, Label::kNear);
4597 __ mov(edi, Operand(ebx, 0 * kPointerSize));
4598 __ mov(FieldOperand(edx, ecx, times_half_pointer_size,
4599 FixedArray::kHeaderSize),
4600 edi);
4601 __ sub(ebx, Immediate(1 * kPointerSize));
4602 __ add(ecx, Immediate(Smi::FromInt(1)));
4603 __ jmp(&loop);
4604 __ bind(&done_loop);
4605 }
4606
4607 // Setup the rest parameter array in edi.
4608 __ lea(edi,
4609 Operand(edx, eax, times_half_pointer_size, FixedArray::kHeaderSize));
4610 __ LoadGlobalFunction(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, ecx);
4611 __ mov(FieldOperand(edi, JSArray::kMapOffset), ecx);
4612 __ mov(FieldOperand(edi, JSArray::kPropertiesOffset),
4613 isolate()->factory()->empty_fixed_array());
4614 __ mov(FieldOperand(edi, JSArray::kElementsOffset), edx);
4615 __ mov(FieldOperand(edi, JSArray::kLengthOffset), eax);
4616 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4617 __ mov(eax, edi);
4618 __ Ret();
4619
4620 // Fall back to %AllocateInNewSpace.
4621 __ bind(&allocate);
4622 {
4623 FrameScope scope(masm, StackFrame::INTERNAL);
4624 __ SmiTag(ecx);
4625 __ Push(eax);
4626 __ Push(ebx);
4627 __ Push(ecx);
4628 __ CallRuntime(Runtime::kAllocateInNewSpace);
4629 __ mov(edx, eax);
4630 __ Pop(ebx);
4631 __ Pop(eax);
4632 }
4633 __ jmp(&done_allocate);
4634 }
4635}
4636
4637void FastNewSloppyArgumentsStub::Generate(MacroAssembler* masm) {
4638 // ----------- S t a t e -------------
4639 // -- edi : function
4640 // -- esi : context
4641 // -- ebp : frame pointer
4642 // -- esp[0] : return address
4643 // -----------------------------------
4644 __ AssertFunction(edi);
4645
4646 // TODO(bmeurer): Cleanup to match the FastNewStrictArgumentsStub.
4647 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4648 __ mov(ecx,
4649 FieldOperand(ecx, SharedFunctionInfo::kFormalParameterCountOffset));
4650 __ lea(edx, Operand(ebp, ecx, times_half_pointer_size,
4651 StandardFrameConstants::kCallerSPOffset));
4652
4653 // ecx : number of parameters (tagged)
4654 // edx : parameters pointer
4655 // edi : function
4656 // esp[0] : return address
4657
4658 // Check if the calling frame is an arguments adaptor frame.
4659 Label adaptor_frame, try_allocate, runtime;
4660 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004661 __ mov(eax, Operand(ebx, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004662 __ cmp(eax, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4663 __ j(equal, &adaptor_frame, Label::kNear);
4664
4665 // No adaptor, parameter count = argument count.
4666 __ mov(ebx, ecx);
4667 __ push(ecx);
4668 __ jmp(&try_allocate, Label::kNear);
4669
4670 // We have an adaptor frame. Patch the parameters pointer.
4671 __ bind(&adaptor_frame);
4672 __ mov(ebx, ecx);
4673 __ push(ecx);
4674 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
4675 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4676 __ lea(edx,
4677 Operand(edx, ecx, times_2, StandardFrameConstants::kCallerSPOffset));
4678
4679 // ebx = parameter count (tagged)
4680 // ecx = argument count (smi-tagged)
4681 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
4682 __ cmp(ebx, ecx);
4683 __ j(less_equal, &try_allocate, Label::kNear);
4684 __ mov(ebx, ecx);
4685
4686 // Save mapped parameter count and function.
4687 __ bind(&try_allocate);
4688 __ push(edi);
4689 __ push(ebx);
4690
4691 // Compute the sizes of backing store, parameter map, and arguments object.
4692 // 1. Parameter map, has 2 extra words containing context and backing store.
4693 const int kParameterMapHeaderSize =
4694 FixedArray::kHeaderSize + 2 * kPointerSize;
4695 Label no_parameter_map;
4696 __ test(ebx, ebx);
4697 __ j(zero, &no_parameter_map, Label::kNear);
4698 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
4699 __ bind(&no_parameter_map);
4700
4701 // 2. Backing store.
4702 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
4703
4704 // 3. Arguments object.
4705 __ add(ebx, Immediate(JSSloppyArgumentsObject::kSize));
4706
4707 // Do the allocation of all three objects in one go.
4708 __ Allocate(ebx, eax, edi, no_reg, &runtime, TAG_OBJECT);
4709
4710 // eax = address of new object(s) (tagged)
4711 // ecx = argument count (smi-tagged)
4712 // esp[0] = mapped parameter count (tagged)
4713 // esp[4] = function
4714 // esp[8] = parameter count (tagged)
4715 // Get the arguments map from the current native context into edi.
4716 Label has_mapped_parameters, instantiate;
4717 __ mov(edi, NativeContextOperand());
4718 __ mov(ebx, Operand(esp, 0 * kPointerSize));
4719 __ test(ebx, ebx);
4720 __ j(not_zero, &has_mapped_parameters, Label::kNear);
4721 __ mov(
4722 edi,
4723 Operand(edi, Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX)));
4724 __ jmp(&instantiate, Label::kNear);
4725
4726 __ bind(&has_mapped_parameters);
4727 __ mov(edi, Operand(edi, Context::SlotOffset(
4728 Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX)));
4729 __ bind(&instantiate);
4730
4731 // eax = address of new object (tagged)
4732 // ebx = mapped parameter count (tagged)
4733 // ecx = argument count (smi-tagged)
4734 // edi = address of arguments map (tagged)
4735 // esp[0] = mapped parameter count (tagged)
4736 // esp[4] = function
4737 // esp[8] = parameter count (tagged)
4738 // Copy the JS object part.
4739 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
4740 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
4741 masm->isolate()->factory()->empty_fixed_array());
4742 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
4743 masm->isolate()->factory()->empty_fixed_array());
4744
4745 // Set up the callee in-object property.
4746 STATIC_ASSERT(JSSloppyArgumentsObject::kCalleeIndex == 1);
4747 __ mov(edi, Operand(esp, 1 * kPointerSize));
4748 __ AssertNotSmi(edi);
4749 __ mov(FieldOperand(eax, JSSloppyArgumentsObject::kCalleeOffset), edi);
4750
4751 // Use the length (smi tagged) and set that as an in-object property too.
4752 __ AssertSmi(ecx);
4753 __ mov(FieldOperand(eax, JSSloppyArgumentsObject::kLengthOffset), ecx);
4754
4755 // Set up the elements pointer in the allocated arguments object.
4756 // If we allocated a parameter map, edi will point there, otherwise to the
4757 // backing store.
4758 __ lea(edi, Operand(eax, JSSloppyArgumentsObject::kSize));
4759 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
4760
4761 // eax = address of new object (tagged)
4762 // ebx = mapped parameter count (tagged)
4763 // ecx = argument count (tagged)
4764 // edx = address of receiver argument
4765 // edi = address of parameter map or backing store (tagged)
4766 // esp[0] = mapped parameter count (tagged)
4767 // esp[4] = function
4768 // esp[8] = parameter count (tagged)
4769 // Free two registers.
4770 __ push(edx);
4771 __ push(eax);
4772
4773 // Initialize parameter map. If there are no mapped arguments, we're done.
4774 Label skip_parameter_map;
4775 __ test(ebx, ebx);
4776 __ j(zero, &skip_parameter_map);
4777
4778 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
4779 Immediate(isolate()->factory()->sloppy_arguments_elements_map()));
4780 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
4781 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
4782 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
4783 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
4784 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
4785
4786 // Copy the parameter slots and the holes in the arguments.
4787 // We need to fill in mapped_parameter_count slots. They index the context,
4788 // where parameters are stored in reverse order, at
4789 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
4790 // The mapped parameter thus need to get indices
4791 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
4792 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
4793 // We loop from right to left.
4794 Label parameters_loop, parameters_test;
4795 __ push(ecx);
4796 __ mov(eax, Operand(esp, 3 * kPointerSize));
4797 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
4798 __ add(ebx, Operand(esp, 5 * kPointerSize));
4799 __ sub(ebx, eax);
4800 __ mov(ecx, isolate()->factory()->the_hole_value());
4801 __ mov(edx, edi);
4802 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
4803 // eax = loop variable (tagged)
4804 // ebx = mapping index (tagged)
4805 // ecx = the hole value
4806 // edx = address of parameter map (tagged)
4807 // edi = address of backing store (tagged)
4808 // esp[0] = argument count (tagged)
4809 // esp[4] = address of new object (tagged)
4810 // esp[8] = address of receiver argument
4811 // esp[12] = mapped parameter count (tagged)
4812 // esp[16] = function
4813 // esp[20] = parameter count (tagged)
4814 __ jmp(&parameters_test, Label::kNear);
4815
4816 __ bind(&parameters_loop);
4817 __ sub(eax, Immediate(Smi::FromInt(1)));
4818 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
4819 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
4820 __ add(ebx, Immediate(Smi::FromInt(1)));
4821 __ bind(&parameters_test);
4822 __ test(eax, eax);
4823 __ j(not_zero, &parameters_loop, Label::kNear);
4824 __ pop(ecx);
4825
4826 __ bind(&skip_parameter_map);
4827
4828 // ecx = argument count (tagged)
4829 // edi = address of backing store (tagged)
4830 // esp[0] = address of new object (tagged)
4831 // esp[4] = address of receiver argument
4832 // esp[8] = mapped parameter count (tagged)
4833 // esp[12] = function
4834 // esp[16] = parameter count (tagged)
4835 // Copy arguments header and remaining slots (if there are any).
4836 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
4837 Immediate(isolate()->factory()->fixed_array_map()));
4838 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
4839
4840 Label arguments_loop, arguments_test;
4841 __ mov(ebx, Operand(esp, 2 * kPointerSize));
4842 __ mov(edx, Operand(esp, 1 * kPointerSize));
4843 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
4844 __ sub(edx, ebx);
4845 __ jmp(&arguments_test, Label::kNear);
4846
4847 __ bind(&arguments_loop);
4848 __ sub(edx, Immediate(kPointerSize));
4849 __ mov(eax, Operand(edx, 0));
4850 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
4851 __ add(ebx, Immediate(Smi::FromInt(1)));
4852
4853 __ bind(&arguments_test);
4854 __ cmp(ebx, ecx);
4855 __ j(less, &arguments_loop, Label::kNear);
4856
4857 // Restore.
4858 __ pop(eax); // Address of arguments object.
4859 __ Drop(4);
4860
4861 // Return.
4862 __ ret(0);
4863
4864 // Do the runtime call to allocate the arguments object.
4865 __ bind(&runtime);
4866 __ pop(eax); // Remove saved mapped parameter count.
4867 __ pop(edi); // Pop saved function.
4868 __ pop(eax); // Remove saved parameter count.
4869 __ pop(eax); // Pop return address.
4870 __ push(edi); // Push function.
4871 __ push(edx); // Push parameters pointer.
4872 __ push(ecx); // Push parameter count.
4873 __ push(eax); // Push return address.
4874 __ TailCallRuntime(Runtime::kNewSloppyArguments);
4875}
4876
4877void FastNewStrictArgumentsStub::Generate(MacroAssembler* masm) {
4878 // ----------- S t a t e -------------
4879 // -- edi : function
4880 // -- esi : context
4881 // -- ebp : frame pointer
4882 // -- esp[0] : return address
4883 // -----------------------------------
4884 __ AssertFunction(edi);
4885
4886 // For Ignition we need to skip all possible handler/stub frames until
4887 // we reach the JavaScript frame for the function (similar to what the
4888 // runtime fallback implementation does). So make edx point to that
4889 // JavaScript frame.
4890 {
4891 Label loop, loop_entry;
4892 __ mov(edx, ebp);
4893 __ jmp(&loop_entry, Label::kNear);
4894 __ bind(&loop);
4895 __ mov(edx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
4896 __ bind(&loop_entry);
Ben Murdochda12d292016-06-02 14:46:10 +01004897 __ cmp(edi, Operand(edx, StandardFrameConstants::kFunctionOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004898 __ j(not_equal, &loop);
4899 }
4900
4901 // Check if we have an arguments adaptor frame below the function frame.
4902 Label arguments_adaptor, arguments_done;
4903 __ mov(ebx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004904 __ cmp(Operand(ebx, CommonFrameConstants::kContextOrFrameTypeOffset),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004905 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4906 __ j(equal, &arguments_adaptor, Label::kNear);
4907 {
4908 __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4909 __ mov(eax,
4910 FieldOperand(eax, SharedFunctionInfo::kFormalParameterCountOffset));
4911 __ lea(ebx,
4912 Operand(edx, eax, times_half_pointer_size,
4913 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4914 }
4915 __ jmp(&arguments_done, Label::kNear);
4916 __ bind(&arguments_adaptor);
4917 {
4918 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4919 __ lea(ebx,
4920 Operand(ebx, eax, times_half_pointer_size,
4921 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4922 }
4923 __ bind(&arguments_done);
4924
4925 // ----------- S t a t e -------------
4926 // -- eax : number of arguments (tagged)
4927 // -- ebx : pointer to the first argument
4928 // -- esi : context
4929 // -- esp[0] : return address
4930 // -----------------------------------
4931
4932 // Allocate space for the strict arguments object plus the backing store.
4933 Label allocate, done_allocate;
4934 __ lea(ecx,
4935 Operand(eax, times_half_pointer_size,
4936 JSStrictArgumentsObject::kSize + FixedArray::kHeaderSize));
4937 __ Allocate(ecx, edx, edi, no_reg, &allocate, TAG_OBJECT);
4938 __ bind(&done_allocate);
4939
4940 // Setup the elements array in edx.
4941 __ mov(FieldOperand(edx, FixedArray::kMapOffset),
4942 isolate()->factory()->fixed_array_map());
4943 __ mov(FieldOperand(edx, FixedArray::kLengthOffset), eax);
4944 {
4945 Label loop, done_loop;
4946 __ Move(ecx, Smi::FromInt(0));
4947 __ bind(&loop);
4948 __ cmp(ecx, eax);
4949 __ j(equal, &done_loop, Label::kNear);
4950 __ mov(edi, Operand(ebx, 0 * kPointerSize));
4951 __ mov(FieldOperand(edx, ecx, times_half_pointer_size,
4952 FixedArray::kHeaderSize),
4953 edi);
4954 __ sub(ebx, Immediate(1 * kPointerSize));
4955 __ add(ecx, Immediate(Smi::FromInt(1)));
4956 __ jmp(&loop);
4957 __ bind(&done_loop);
4958 }
4959
4960 // Setup the rest parameter array in edi.
4961 __ lea(edi,
4962 Operand(edx, eax, times_half_pointer_size, FixedArray::kHeaderSize));
4963 __ LoadGlobalFunction(Context::STRICT_ARGUMENTS_MAP_INDEX, ecx);
4964 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kMapOffset), ecx);
4965 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kPropertiesOffset),
4966 isolate()->factory()->empty_fixed_array());
4967 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kElementsOffset), edx);
4968 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kLengthOffset), eax);
4969 STATIC_ASSERT(JSStrictArgumentsObject::kSize == 4 * kPointerSize);
4970 __ mov(eax, edi);
4971 __ Ret();
4972
4973 // Fall back to %AllocateInNewSpace.
4974 __ bind(&allocate);
4975 {
4976 FrameScope scope(masm, StackFrame::INTERNAL);
4977 __ SmiTag(ecx);
4978 __ Push(eax);
4979 __ Push(ebx);
4980 __ Push(ecx);
4981 __ CallRuntime(Runtime::kAllocateInNewSpace);
4982 __ mov(edx, eax);
4983 __ Pop(ebx);
4984 __ Pop(eax);
4985 }
4986 __ jmp(&done_allocate);
4987}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004988
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004989void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
4990 Register context_reg = esi;
4991 Register slot_reg = ebx;
4992 Register result_reg = eax;
4993 Label slow_case;
4994
4995 // Go up context chain to the script context.
4996 for (int i = 0; i < depth(); ++i) {
4997 __ mov(result_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
4998 context_reg = result_reg;
4999 }
5000
5001 // Load the PropertyCell value at the specified slot.
5002 __ mov(result_reg, ContextOperand(context_reg, slot_reg));
5003 __ mov(result_reg, FieldOperand(result_reg, PropertyCell::kValueOffset));
5004
5005 // Check that value is not the_hole.
5006 __ CompareRoot(result_reg, Heap::kTheHoleValueRootIndex);
5007 __ j(equal, &slow_case, Label::kNear);
5008 __ Ret();
5009
5010 // Fallback to the runtime.
5011 __ bind(&slow_case);
5012 __ SmiTag(slot_reg);
5013 __ Pop(result_reg); // Pop return address.
5014 __ Push(slot_reg);
5015 __ Push(result_reg); // Push return address.
5016 __ TailCallRuntime(Runtime::kLoadGlobalViaContext);
5017}
5018
5019
5020void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
5021 Register context_reg = esi;
5022 Register slot_reg = ebx;
5023 Register value_reg = eax;
5024 Register cell_reg = edi;
5025 Register cell_details_reg = edx;
5026 Register cell_value_reg = ecx;
5027 Label fast_heapobject_case, fast_smi_case, slow_case;
5028
5029 if (FLAG_debug_code) {
5030 __ CompareRoot(value_reg, Heap::kTheHoleValueRootIndex);
5031 __ Check(not_equal, kUnexpectedValue);
5032 }
5033
5034 // Go up context chain to the script context.
5035 for (int i = 0; i < depth(); ++i) {
5036 __ mov(cell_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
5037 context_reg = cell_reg;
5038 }
5039
5040 // Load the PropertyCell at the specified slot.
5041 __ mov(cell_reg, ContextOperand(context_reg, slot_reg));
5042
5043 // Load PropertyDetails for the cell (actually only the cell_type and kind).
5044 __ mov(cell_details_reg,
5045 FieldOperand(cell_reg, PropertyCell::kDetailsOffset));
5046 __ SmiUntag(cell_details_reg);
5047 __ and_(cell_details_reg,
5048 Immediate(PropertyDetails::PropertyCellTypeField::kMask |
5049 PropertyDetails::KindField::kMask |
5050 PropertyDetails::kAttributesReadOnlyMask));
5051
5052 // Check if PropertyCell holds mutable data.
5053 Label not_mutable_data;
5054 __ cmp(cell_details_reg,
5055 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5056 PropertyCellType::kMutable) |
5057 PropertyDetails::KindField::encode(kData)));
5058 __ j(not_equal, &not_mutable_data);
5059 __ JumpIfSmi(value_reg, &fast_smi_case);
5060 __ bind(&fast_heapobject_case);
5061 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5062 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
5063 cell_details_reg, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
5064 OMIT_SMI_CHECK);
5065 // RecordWriteField clobbers the value register, so we need to reload.
5066 __ mov(value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5067 __ Ret();
5068 __ bind(&not_mutable_data);
5069
5070 // Check if PropertyCell value matches the new value (relevant for Constant,
5071 // ConstantType and Undefined cells).
5072 Label not_same_value;
5073 __ mov(cell_value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
5074 __ cmp(cell_value_reg, value_reg);
5075 __ j(not_equal, &not_same_value,
5076 FLAG_debug_code ? Label::kFar : Label::kNear);
5077 // Make sure the PropertyCell is not marked READ_ONLY.
5078 __ test(cell_details_reg,
5079 Immediate(PropertyDetails::kAttributesReadOnlyMask));
5080 __ j(not_zero, &slow_case);
5081 if (FLAG_debug_code) {
5082 Label done;
5083 // This can only be true for Constant, ConstantType and Undefined cells,
5084 // because we never store the_hole via this stub.
5085 __ cmp(cell_details_reg,
5086 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5087 PropertyCellType::kConstant) |
5088 PropertyDetails::KindField::encode(kData)));
5089 __ j(equal, &done);
5090 __ cmp(cell_details_reg,
5091 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5092 PropertyCellType::kConstantType) |
5093 PropertyDetails::KindField::encode(kData)));
5094 __ j(equal, &done);
5095 __ cmp(cell_details_reg,
5096 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5097 PropertyCellType::kUndefined) |
5098 PropertyDetails::KindField::encode(kData)));
5099 __ Check(equal, kUnexpectedValue);
5100 __ bind(&done);
5101 }
5102 __ Ret();
5103 __ bind(&not_same_value);
5104
5105 // Check if PropertyCell contains data with constant type (and is not
5106 // READ_ONLY).
5107 __ cmp(cell_details_reg,
5108 Immediate(PropertyDetails::PropertyCellTypeField::encode(
5109 PropertyCellType::kConstantType) |
5110 PropertyDetails::KindField::encode(kData)));
5111 __ j(not_equal, &slow_case, Label::kNear);
5112
5113 // Now either both old and new values must be SMIs or both must be heap
5114 // objects with same map.
5115 Label value_is_heap_object;
5116 __ JumpIfNotSmi(value_reg, &value_is_heap_object, Label::kNear);
5117 __ JumpIfNotSmi(cell_value_reg, &slow_case, Label::kNear);
5118 // Old and new values are SMIs, no need for a write barrier here.
5119 __ bind(&fast_smi_case);
5120 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
5121 __ Ret();
5122 __ bind(&value_is_heap_object);
5123 __ JumpIfSmi(cell_value_reg, &slow_case, Label::kNear);
5124 Register cell_value_map_reg = cell_value_reg;
5125 __ mov(cell_value_map_reg,
5126 FieldOperand(cell_value_reg, HeapObject::kMapOffset));
5127 __ cmp(cell_value_map_reg, FieldOperand(value_reg, HeapObject::kMapOffset));
5128 __ j(equal, &fast_heapobject_case);
5129
5130 // Fallback to the runtime.
5131 __ bind(&slow_case);
5132 __ SmiTag(slot_reg);
5133 __ Pop(cell_reg); // Pop return address.
5134 __ Push(slot_reg);
5135 __ Push(value_reg);
5136 __ Push(cell_reg); // Push return address.
5137 __ TailCallRuntime(is_strict(language_mode())
5138 ? Runtime::kStoreGlobalViaContext_Strict
5139 : Runtime::kStoreGlobalViaContext_Sloppy);
5140}
5141
5142
5143// Generates an Operand for saving parameters after PrepareCallApiFunction.
5144static Operand ApiParameterOperand(int index) {
5145 return Operand(esp, index * kPointerSize);
5146}
5147
5148
5149// Prepares stack to put arguments (aligns and so on). Reserves
5150// space for return value if needed (assumes the return value is a handle).
5151// Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
5152// etc. Saves context (esi). If space was reserved for return value then
5153// stores the pointer to the reserved slot into esi.
5154static void PrepareCallApiFunction(MacroAssembler* masm, int argc) {
5155 __ EnterApiExitFrame(argc);
5156 if (__ emit_debug_code()) {
5157 __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
5158 }
5159}
5160
5161
5162// Calls an API function. Allocates HandleScope, extracts returned value
5163// from handle and propagates exceptions. Clobbers ebx, edi and
5164// caller-save registers. Restores context. On return removes
5165// stack_space * kPointerSize (GCed).
5166static void CallApiFunctionAndReturn(MacroAssembler* masm,
5167 Register function_address,
5168 ExternalReference thunk_ref,
5169 Operand thunk_last_arg, int stack_space,
5170 Operand* stack_space_operand,
5171 Operand return_value_operand,
5172 Operand* context_restore_operand) {
5173 Isolate* isolate = masm->isolate();
5174
5175 ExternalReference next_address =
5176 ExternalReference::handle_scope_next_address(isolate);
5177 ExternalReference limit_address =
5178 ExternalReference::handle_scope_limit_address(isolate);
5179 ExternalReference level_address =
5180 ExternalReference::handle_scope_level_address(isolate);
5181
5182 DCHECK(edx.is(function_address));
5183 // Allocate HandleScope in callee-save registers.
5184 __ mov(ebx, Operand::StaticVariable(next_address));
5185 __ mov(edi, Operand::StaticVariable(limit_address));
5186 __ add(Operand::StaticVariable(level_address), Immediate(1));
5187
5188 if (FLAG_log_timer_events) {
5189 FrameScope frame(masm, StackFrame::MANUAL);
5190 __ PushSafepointRegisters();
5191 __ PrepareCallCFunction(1, eax);
5192 __ mov(Operand(esp, 0),
5193 Immediate(ExternalReference::isolate_address(isolate)));
5194 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5195 1);
5196 __ PopSafepointRegisters();
5197 }
5198
5199
5200 Label profiler_disabled;
5201 Label end_profiler_check;
5202 __ mov(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
Ben Murdochda12d292016-06-02 14:46:10 +01005203 __ cmpb(Operand(eax, 0), Immediate(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005204 __ j(zero, &profiler_disabled);
5205
5206 // Additional parameter is the address of the actual getter function.
5207 __ mov(thunk_last_arg, function_address);
5208 // Call the api function.
5209 __ mov(eax, Immediate(thunk_ref));
5210 __ call(eax);
5211 __ jmp(&end_profiler_check);
5212
5213 __ bind(&profiler_disabled);
5214 // Call the api function.
5215 __ call(function_address);
5216 __ bind(&end_profiler_check);
5217
5218 if (FLAG_log_timer_events) {
5219 FrameScope frame(masm, StackFrame::MANUAL);
5220 __ PushSafepointRegisters();
5221 __ PrepareCallCFunction(1, eax);
5222 __ mov(Operand(esp, 0),
5223 Immediate(ExternalReference::isolate_address(isolate)));
5224 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5225 1);
5226 __ PopSafepointRegisters();
5227 }
5228
5229 Label prologue;
5230 // Load the value from ReturnValue
5231 __ mov(eax, return_value_operand);
5232
5233 Label promote_scheduled_exception;
5234 Label delete_allocated_handles;
5235 Label leave_exit_frame;
5236
5237 __ bind(&prologue);
5238 // No more valid handles (the result handle was the last one). Restore
5239 // previous handle scope.
5240 __ mov(Operand::StaticVariable(next_address), ebx);
5241 __ sub(Operand::StaticVariable(level_address), Immediate(1));
5242 __ Assert(above_equal, kInvalidHandleScopeLevel);
5243 __ cmp(edi, Operand::StaticVariable(limit_address));
5244 __ j(not_equal, &delete_allocated_handles);
5245
5246 // Leave the API exit frame.
5247 __ bind(&leave_exit_frame);
5248 bool restore_context = context_restore_operand != NULL;
5249 if (restore_context) {
5250 __ mov(esi, *context_restore_operand);
5251 }
5252 if (stack_space_operand != nullptr) {
5253 __ mov(ebx, *stack_space_operand);
5254 }
5255 __ LeaveApiExitFrame(!restore_context);
5256
5257 // Check if the function scheduled an exception.
5258 ExternalReference scheduled_exception_address =
5259 ExternalReference::scheduled_exception_address(isolate);
5260 __ cmp(Operand::StaticVariable(scheduled_exception_address),
5261 Immediate(isolate->factory()->the_hole_value()));
5262 __ j(not_equal, &promote_scheduled_exception);
5263
5264#if DEBUG
5265 // Check if the function returned a valid JavaScript value.
5266 Label ok;
5267 Register return_value = eax;
5268 Register map = ecx;
5269
5270 __ JumpIfSmi(return_value, &ok, Label::kNear);
5271 __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
5272
5273 __ CmpInstanceType(map, LAST_NAME_TYPE);
5274 __ j(below_equal, &ok, Label::kNear);
5275
5276 __ CmpInstanceType(map, FIRST_JS_RECEIVER_TYPE);
5277 __ j(above_equal, &ok, Label::kNear);
5278
5279 __ cmp(map, isolate->factory()->heap_number_map());
5280 __ j(equal, &ok, Label::kNear);
5281
5282 __ cmp(return_value, isolate->factory()->undefined_value());
5283 __ j(equal, &ok, Label::kNear);
5284
5285 __ cmp(return_value, isolate->factory()->true_value());
5286 __ j(equal, &ok, Label::kNear);
5287
5288 __ cmp(return_value, isolate->factory()->false_value());
5289 __ j(equal, &ok, Label::kNear);
5290
5291 __ cmp(return_value, isolate->factory()->null_value());
5292 __ j(equal, &ok, Label::kNear);
5293
5294 __ Abort(kAPICallReturnedInvalidObject);
5295
5296 __ bind(&ok);
5297#endif
5298
5299 if (stack_space_operand != nullptr) {
5300 DCHECK_EQ(0, stack_space);
5301 __ pop(ecx);
5302 __ add(esp, ebx);
5303 __ jmp(ecx);
5304 } else {
5305 __ ret(stack_space * kPointerSize);
5306 }
5307
5308 // Re-throw by promoting a scheduled exception.
5309 __ bind(&promote_scheduled_exception);
5310 __ TailCallRuntime(Runtime::kPromoteScheduledException);
5311
5312 // HandleScope limit has changed. Delete allocated extensions.
5313 ExternalReference delete_extensions =
5314 ExternalReference::delete_handle_scope_extensions(isolate);
5315 __ bind(&delete_allocated_handles);
5316 __ mov(Operand::StaticVariable(limit_address), edi);
5317 __ mov(edi, eax);
5318 __ mov(Operand(esp, 0),
5319 Immediate(ExternalReference::isolate_address(isolate)));
5320 __ mov(eax, Immediate(delete_extensions));
5321 __ call(eax);
5322 __ mov(eax, edi);
5323 __ jmp(&leave_exit_frame);
5324}
5325
Ben Murdochda12d292016-06-02 14:46:10 +01005326void CallApiCallbackStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005327 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005328 // -- edi : callee
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005329 // -- ebx : call_data
5330 // -- ecx : holder
5331 // -- edx : api_function_address
5332 // -- esi : context
5333 // --
5334 // -- esp[0] : return address
5335 // -- esp[4] : last argument
5336 // -- ...
5337 // -- esp[argc * 4] : first argument
5338 // -- esp[(argc + 1) * 4] : receiver
5339 // -----------------------------------
5340
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005341 Register callee = edi;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005342 Register call_data = ebx;
5343 Register holder = ecx;
5344 Register api_function_address = edx;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005345 Register context = esi;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005346 Register return_address = eax;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005347
5348 typedef FunctionCallbackArguments FCA;
5349
5350 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5351 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5352 STATIC_ASSERT(FCA::kDataIndex == 4);
5353 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5354 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5355 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5356 STATIC_ASSERT(FCA::kHolderIndex == 0);
5357 STATIC_ASSERT(FCA::kArgsLength == 7);
5358
Ben Murdochda12d292016-06-02 14:46:10 +01005359 __ pop(return_address);
5360 // context save.
5361 __ push(context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005362
5363 // callee
5364 __ push(callee);
5365
5366 // call data
5367 __ push(call_data);
5368
5369 Register scratch = call_data;
Ben Murdochda12d292016-06-02 14:46:10 +01005370 if (!call_data_undefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005371 // return value
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005372 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005373 // return value default
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005374 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005375 } else {
5376 // return value
5377 __ push(scratch);
5378 // return value default
5379 __ push(scratch);
5380 }
5381 // isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005382 __ push(Immediate(reinterpret_cast<int>(masm->isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005383 // holder
5384 __ push(holder);
5385
5386 __ mov(scratch, esp);
5387
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005388 // push return address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005389 __ push(return_address);
5390
Ben Murdochda12d292016-06-02 14:46:10 +01005391 if (!is_lazy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005392 // load context from callee
5393 __ mov(context, FieldOperand(callee, JSFunction::kContextOffset));
5394 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005395
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005396 // API function gets reference to the v8::Arguments. If CPU profiler
5397 // is enabled wrapper function will be called and we need to pass
5398 // address of the callback as additional parameter, always allocate
5399 // space for it.
5400 const int kApiArgc = 1 + 1;
5401
5402 // Allocate the v8::Arguments structure in the arguments' space since
5403 // it's not controlled by GC.
5404 const int kApiStackSpace = 4;
5405
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005406 PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005407
5408 // FunctionCallbackInfo::implicit_args_.
5409 __ mov(ApiParameterOperand(2), scratch);
Ben Murdochda12d292016-06-02 14:46:10 +01005410 __ add(scratch, Immediate((argc() + FCA::kArgsLength - 1) * kPointerSize));
5411 // FunctionCallbackInfo::values_.
5412 __ mov(ApiParameterOperand(3), scratch);
5413 // FunctionCallbackInfo::length_.
5414 __ Move(ApiParameterOperand(4), Immediate(argc()));
5415 // FunctionCallbackInfo::is_construct_call_.
5416 __ Move(ApiParameterOperand(5), Immediate(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005417
5418 // v8::InvocationCallback's argument.
5419 __ lea(scratch, ApiParameterOperand(2));
5420 __ mov(ApiParameterOperand(0), scratch);
5421
5422 ExternalReference thunk_ref =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005423 ExternalReference::invoke_function_callback(masm->isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005424
5425 Operand context_restore_operand(ebp,
5426 (2 + FCA::kContextSaveIndex) * kPointerSize);
5427 // Stores return the first js argument
5428 int return_value_offset = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005429 if (is_store()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005430 return_value_offset = 2 + FCA::kArgsLength;
5431 } else {
5432 return_value_offset = 2 + FCA::kReturnValueOffset;
5433 }
5434 Operand return_value_operand(ebp, return_value_offset * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005435 int stack_space = 0;
5436 Operand is_construct_call_operand = ApiParameterOperand(5);
5437 Operand* stack_space_operand = &is_construct_call_operand;
Ben Murdochda12d292016-06-02 14:46:10 +01005438 stack_space = argc() + FCA::kArgsLength + 1;
5439 stack_space_operand = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005440 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5441 ApiParameterOperand(1), stack_space,
5442 stack_space_operand, return_value_operand,
5443 &context_restore_operand);
5444}
5445
5446
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005447void CallApiGetterStub::Generate(MacroAssembler* masm) {
5448 // ----------- S t a t e -------------
Ben Murdoch097c5b22016-05-18 11:27:45 +01005449 // -- esp[0] : return address
5450 // -- esp[4] : name
5451 // -- esp[8 .. (8 + kArgsLength*4)] : v8::PropertyCallbackInfo::args_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005452 // -- ...
Ben Murdoch097c5b22016-05-18 11:27:45 +01005453 // -- edx : api_function_address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005454 // -----------------------------------
5455 DCHECK(edx.is(ApiGetterDescriptor::function_address()));
5456
Ben Murdoch097c5b22016-05-18 11:27:45 +01005457 // v8::PropertyCallbackInfo::args_ array and name handle.
5458 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5459
5460 // Allocate v8::PropertyCallbackInfo object, arguments for callback and
5461 // space for optional callback address parameter (in case CPU profiler is
5462 // active) in non-GCed stack space.
5463 const int kApiArgc = 3 + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005464
5465 Register api_function_address = edx;
5466 Register scratch = ebx;
5467
Ben Murdoch097c5b22016-05-18 11:27:45 +01005468 // Load address of v8::PropertyAccessorInfo::args_ array.
5469 __ lea(scratch, Operand(esp, 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005470
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005471 PrepareCallApiFunction(masm, kApiArgc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005472 // Create v8::PropertyCallbackInfo object on the stack and initialize
5473 // it's args_ field.
5474 Operand info_object = ApiParameterOperand(3);
5475 __ mov(info_object, scratch);
5476
5477 __ sub(scratch, Immediate(kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005478 __ mov(ApiParameterOperand(0), scratch); // name.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005479 __ lea(scratch, info_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005480 __ mov(ApiParameterOperand(1), scratch); // arguments pointer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005481 // Reserve space for optional callback address parameter.
5482 Operand thunk_last_arg = ApiParameterOperand(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005483
5484 ExternalReference thunk_ref =
5485 ExternalReference::invoke_accessor_getter_callback(isolate());
5486
Ben Murdoch097c5b22016-05-18 11:27:45 +01005487 // +3 is to skip prolog, return address and name handle.
5488 Operand return_value_operand(
5489 ebp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005490 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005491 thunk_last_arg, kStackUnwindSpace, nullptr,
5492 return_value_operand, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005493}
5494
5495
5496#undef __
5497
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005498} // namespace internal
5499} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005500
5501#endif // V8_TARGET_ARCH_X87