blob: fdb97ee5a5c4cf754a287bd34fba6088134ba7e5 [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
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065void ArraySingleArgumentConstructorStub::InitializeDescriptor(
66 CodeStubDescriptor* descriptor) {
67 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
68}
69
70
71void ArrayNArgumentsConstructorStub::InitializeDescriptor(
72 CodeStubDescriptor* descriptor) {
73 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
74}
75
76
Ben Murdochda12d292016-06-02 14:46:10 +010077void FastArrayPushStub::InitializeDescriptor(CodeStubDescriptor* descriptor) {
78 Address deopt_handler = Runtime::FunctionForId(Runtime::kArrayPush)->entry;
79 descriptor->Initialize(eax, deopt_handler, -1, JS_FUNCTION_STUB_MODE);
80}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081
82void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
83 CodeStubDescriptor* descriptor) {
84 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
85}
86
87
88void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
89 CodeStubDescriptor* descriptor) {
90 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
91}
92
93
94#define __ ACCESS_MASM(masm)
95
96
97void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
98 ExternalReference miss) {
99 // Update the static counter each time a new code stub is generated.
100 isolate()->counters()->code_stubs()->Increment();
101
102 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000103 int param_count = descriptor.GetRegisterParameterCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000104 {
105 // Call the runtime system in a fresh internal frame.
106 FrameScope scope(masm, StackFrame::INTERNAL);
107 DCHECK(param_count == 0 ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108 eax.is(descriptor.GetRegisterParameter(param_count - 1)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 // Push arguments
110 for (int i = 0; i < param_count; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000111 __ push(descriptor.GetRegisterParameter(i));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 }
113 __ CallExternalReference(miss, param_count);
114 }
115
116 __ ret(0);
117}
118
119
120void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
121 // We don't allow a GC during a store buffer overflow so there is no need to
122 // store the registers in any particular way, but we do have to store and
123 // restore them.
124 __ pushad();
125 if (save_doubles()) {
126 // Save FPU stat in m108byte.
127 __ sub(esp, Immediate(108));
128 __ fnsave(Operand(esp, 0));
129 }
130 const int argument_count = 1;
131
132 AllowExternalCallThatCantCauseGC scope(masm);
133 __ PrepareCallCFunction(argument_count, ecx);
134 __ mov(Operand(esp, 0 * kPointerSize),
135 Immediate(ExternalReference::isolate_address(isolate())));
136 __ CallCFunction(
137 ExternalReference::store_buffer_overflow_function(isolate()),
138 argument_count);
139 if (save_doubles()) {
140 // Restore FPU stat in m108byte.
141 __ frstor(Operand(esp, 0));
142 __ add(esp, Immediate(108));
143 }
144 __ popad();
145 __ ret(0);
146}
147
148
149class FloatingPointHelper : public AllStatic {
150 public:
151 enum ArgLocation {
152 ARGS_ON_STACK,
153 ARGS_IN_REGISTERS
154 };
155
156 // Code pattern for loading a floating point value. Input value must
157 // be either a smi or a heap number object (fp value). Requirements:
158 // operand in register number. Returns operand as floating point number
159 // on FPU stack.
160 static void LoadFloatOperand(MacroAssembler* masm, Register number);
161
162 // Test if operands are smi or number objects (fp). Requirements:
163 // operand_1 in eax, operand_2 in edx; falls through on float
164 // operands, jumps to the non_float label otherwise.
165 static void CheckFloatOperands(MacroAssembler* masm,
166 Label* non_float,
167 Register scratch);
168};
169
170
171void DoubleToIStub::Generate(MacroAssembler* masm) {
172 Register input_reg = this->source();
173 Register final_result_reg = this->destination();
174 DCHECK(is_truncating());
175
176 Label check_negative, process_64_bits, done, done_no_stash;
177
178 int double_offset = offset();
179
180 // Account for return address and saved regs if input is esp.
181 if (input_reg.is(esp)) double_offset += 3 * kPointerSize;
182
183 MemOperand mantissa_operand(MemOperand(input_reg, double_offset));
184 MemOperand exponent_operand(MemOperand(input_reg,
185 double_offset + kDoubleSize / 2));
186
187 Register scratch1;
188 {
189 Register scratch_candidates[3] = { ebx, edx, edi };
190 for (int i = 0; i < 3; i++) {
191 scratch1 = scratch_candidates[i];
192 if (!final_result_reg.is(scratch1) && !input_reg.is(scratch1)) break;
193 }
194 }
195 // Since we must use ecx for shifts below, use some other register (eax)
196 // to calculate the result if ecx is the requested return register.
197 Register result_reg = final_result_reg.is(ecx) ? eax : final_result_reg;
198 // Save ecx if it isn't the return register and therefore volatile, or if it
199 // is the return register, then save the temp register we use in its stead for
200 // the result.
201 Register save_reg = final_result_reg.is(ecx) ? eax : ecx;
202 __ push(scratch1);
203 __ push(save_reg);
204
205 bool stash_exponent_copy = !input_reg.is(esp);
206 __ mov(scratch1, mantissa_operand);
207 __ mov(ecx, exponent_operand);
208 if (stash_exponent_copy) __ push(ecx);
209
210 __ and_(ecx, HeapNumber::kExponentMask);
211 __ shr(ecx, HeapNumber::kExponentShift);
212 __ lea(result_reg, MemOperand(ecx, -HeapNumber::kExponentBias));
213 __ cmp(result_reg, Immediate(HeapNumber::kMantissaBits));
214 __ j(below, &process_64_bits);
215
216 // Result is entirely in lower 32-bits of mantissa
217 int delta = HeapNumber::kExponentBias + Double::kPhysicalSignificandSize;
218 __ sub(ecx, Immediate(delta));
219 __ xor_(result_reg, result_reg);
220 __ cmp(ecx, Immediate(31));
221 __ j(above, &done);
222 __ shl_cl(scratch1);
223 __ jmp(&check_negative);
224
225 __ bind(&process_64_bits);
226 // Result must be extracted from shifted 32-bit mantissa
227 __ sub(ecx, Immediate(delta));
228 __ neg(ecx);
229 if (stash_exponent_copy) {
230 __ mov(result_reg, MemOperand(esp, 0));
231 } else {
232 __ mov(result_reg, exponent_operand);
233 }
234 __ and_(result_reg,
235 Immediate(static_cast<uint32_t>(Double::kSignificandMask >> 32)));
236 __ add(result_reg,
237 Immediate(static_cast<uint32_t>(Double::kHiddenBit >> 32)));
Ben Murdochda12d292016-06-02 14:46:10 +0100238 __ shrd_cl(scratch1, result_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000239 __ shr_cl(result_reg);
240 __ test(ecx, Immediate(32));
241 {
242 Label skip_mov;
243 __ j(equal, &skip_mov, Label::kNear);
244 __ mov(scratch1, result_reg);
245 __ bind(&skip_mov);
246 }
247
248 // If the double was negative, negate the integer result.
249 __ bind(&check_negative);
250 __ mov(result_reg, scratch1);
251 __ neg(result_reg);
252 if (stash_exponent_copy) {
253 __ cmp(MemOperand(esp, 0), Immediate(0));
254 } else {
255 __ cmp(exponent_operand, Immediate(0));
256 }
257 {
258 Label skip_mov;
259 __ j(less_equal, &skip_mov, Label::kNear);
260 __ mov(result_reg, scratch1);
261 __ bind(&skip_mov);
262 }
263
264 // Restore registers
265 __ bind(&done);
266 if (stash_exponent_copy) {
267 __ add(esp, Immediate(kDoubleSize / 2));
268 }
269 __ bind(&done_no_stash);
270 if (!final_result_reg.is(result_reg)) {
271 DCHECK(final_result_reg.is(ecx));
272 __ mov(final_result_reg, result_reg);
273 }
274 __ pop(save_reg);
275 __ pop(scratch1);
276 __ ret(0);
277}
278
279
280void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
281 Register number) {
282 Label load_smi, done;
283
284 __ JumpIfSmi(number, &load_smi, Label::kNear);
285 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
286 __ jmp(&done, Label::kNear);
287
288 __ bind(&load_smi);
289 __ SmiUntag(number);
290 __ push(number);
291 __ fild_s(Operand(esp, 0));
292 __ pop(number);
293
294 __ bind(&done);
295}
296
297
298void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
299 Label* non_float,
300 Register scratch) {
301 Label test_other, done;
302 // Test if both operands are floats or smi -> scratch=k_is_float;
303 // Otherwise scratch = k_not_float.
304 __ JumpIfSmi(edx, &test_other, Label::kNear);
305 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
306 Factory* factory = masm->isolate()->factory();
307 __ cmp(scratch, factory->heap_number_map());
308 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
309
310 __ bind(&test_other);
311 __ JumpIfSmi(eax, &done, Label::kNear);
312 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
313 __ cmp(scratch, factory->heap_number_map());
314 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
315
316 // Fall-through: Both operands are numbers.
317 __ bind(&done);
318}
319
320
321void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000322 const Register base = edx;
323 const Register scratch = ecx;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324 Label call_runtime;
325
326 // We will call runtime helper function directly.
327 if (exponent_type() == ON_STACK) {
328 // The arguments are still on the stack.
329 __ bind(&call_runtime);
330 __ TailCallRuntime(Runtime::kMathPowRT);
331
332 // The stub is called from non-optimized code, which expects the result
333 // as heap number in exponent.
334 __ AllocateHeapNumber(eax, scratch, base, &call_runtime);
335 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000336 __ ret(2 * kPointerSize);
337 } else {
338 // Currently it's only called from full-compiler and exponent type is
339 // ON_STACK.
340 UNIMPLEMENTED();
341 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000342}
343
344
345void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
346 Label miss;
347 Register receiver = LoadDescriptor::ReceiverRegister();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000348 // With careful management, we won't have to save slot and vector on
349 // the stack. Simply handle the possibly missing case first.
350 // TODO(mvstanton): this code can be more efficient.
351 __ cmp(FieldOperand(receiver, JSFunction::kPrototypeOrInitialMapOffset),
352 Immediate(isolate()->factory()->the_hole_value()));
353 __ j(equal, &miss);
354 __ TryGetFunctionPrototype(receiver, eax, ebx, &miss);
355 __ ret(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000356
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000357 __ bind(&miss);
358 PropertyAccessCompiler::TailCallBuiltin(
359 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
360}
361
362
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400363void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
364 // Return address is on the stack.
365 Label miss;
366
367 Register receiver = LoadDescriptor::ReceiverRegister();
368 Register index = LoadDescriptor::NameRegister();
369 Register scratch = edi;
370 DCHECK(!scratch.is(receiver) && !scratch.is(index));
371 Register result = eax;
372 DCHECK(!result.is(scratch));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000373 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
374 result.is(LoadDescriptor::SlotRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400375
376 // StringCharAtGenerator doesn't use the result register until it's passed
377 // the different miss possibilities. If it did, we would have a conflict
378 // when FLAG_vector_ics is true.
379
380 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
381 &miss, // When not a string.
382 &miss, // When not a number.
383 &miss, // When index out of range.
384 STRING_INDEX_IS_ARRAY_INDEX,
385 RECEIVER_IS_STRING);
386 char_at_generator.GenerateFast(masm);
387 __ ret(0);
388
389 StubRuntimeCallHelper call_helper;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000390 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400391
392 __ bind(&miss);
393 PropertyAccessCompiler::TailCallBuiltin(
394 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
395}
396
397
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000398void RegExpExecStub::Generate(MacroAssembler* masm) {
399 // Just jump directly to runtime if native RegExp is not selected at compile
400 // time or if regexp entry in generated code is turned off runtime switch or
401 // at compilation.
402#ifdef V8_INTERPRETED_REGEXP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000403 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404#else // V8_INTERPRETED_REGEXP
405
406 // Stack frame on entry.
407 // esp[0]: return address
408 // esp[4]: last_match_info (expected JSArray)
409 // esp[8]: previous index
410 // esp[12]: subject string
411 // esp[16]: JSRegExp object
412
413 static const int kLastMatchInfoOffset = 1 * kPointerSize;
414 static const int kPreviousIndexOffset = 2 * kPointerSize;
415 static const int kSubjectOffset = 3 * kPointerSize;
416 static const int kJSRegExpOffset = 4 * kPointerSize;
417
418 Label runtime;
419 Factory* factory = isolate()->factory();
420
421 // Ensure that a RegExp stack is allocated.
422 ExternalReference address_of_regexp_stack_memory_address =
423 ExternalReference::address_of_regexp_stack_memory_address(isolate());
424 ExternalReference address_of_regexp_stack_memory_size =
425 ExternalReference::address_of_regexp_stack_memory_size(isolate());
426 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
427 __ test(ebx, ebx);
428 __ j(zero, &runtime);
429
430 // Check that the first argument is a JSRegExp object.
431 __ mov(eax, Operand(esp, kJSRegExpOffset));
432 STATIC_ASSERT(kSmiTag == 0);
433 __ JumpIfSmi(eax, &runtime);
434 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
435 __ j(not_equal, &runtime);
436
437 // Check that the RegExp has been compiled (data contains a fixed array).
438 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
439 if (FLAG_debug_code) {
440 __ test(ecx, Immediate(kSmiTagMask));
441 __ Check(not_zero, kUnexpectedTypeForRegExpDataFixedArrayExpected);
442 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
443 __ Check(equal, kUnexpectedTypeForRegExpDataFixedArrayExpected);
444 }
445
446 // ecx: RegExp data (FixedArray)
447 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
448 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
449 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
450 __ j(not_equal, &runtime);
451
452 // ecx: RegExp data (FixedArray)
453 // Check that the number of captures fit in the static offsets vector buffer.
454 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
455 // Check (number_of_captures + 1) * 2 <= offsets vector size
456 // Or number_of_captures * 2 <= offsets vector size - 2
457 // Multiplying by 2 comes for free since edx is smi-tagged.
458 STATIC_ASSERT(kSmiTag == 0);
459 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
460 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
461 __ cmp(edx, Isolate::kJSRegexpStaticOffsetsVectorSize - 2);
462 __ j(above, &runtime);
463
464 // Reset offset for possibly sliced string.
465 __ Move(edi, Immediate(0));
466 __ mov(eax, Operand(esp, kSubjectOffset));
467 __ JumpIfSmi(eax, &runtime);
468 __ mov(edx, eax); // Make a copy of the original subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000469
470 // eax: subject string
471 // edx: subject string
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000472 // ecx: RegExp data (FixedArray)
473 // Handle subject string according to its encoding and representation:
474 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch097c5b22016-05-18 11:27:45 +0100475 // (2) Sequential one byte? If yes, go to (5).
476 // (3) Sequential or cons? If not, go to (6).
477 // (4) Cons string. If the string is flat, replace subject with first string
478 // and go to (1). Otherwise bail out to runtime.
479 // (5) One byte sequential. Load regexp code for one byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 // (E) Carry on.
481 /// [...]
482
483 // Deferred code at the end of the stub:
Ben Murdoch097c5b22016-05-18 11:27:45 +0100484 // (6) Long external string? If not, go to (10).
485 // (7) External string. Make it, offset-wise, look like a sequential string.
486 // (8) Is the external string one byte? If yes, go to (5).
487 // (9) Two byte sequential. Load regexp code for two byte. Go to (E).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000488 // (10) Short external string or not a string? If yes, bail out to runtime.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100489 // (11) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000490
Ben Murdoch097c5b22016-05-18 11:27:45 +0100491 Label seq_one_byte_string /* 5 */, seq_two_byte_string /* 9 */,
492 external_string /* 7 */, check_underlying /* 1 */,
493 not_seq_nor_cons /* 6 */, check_code /* E */, not_long_external /* 10 */;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000494
Ben Murdoch097c5b22016-05-18 11:27:45 +0100495 __ bind(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000496 // (1) Sequential two byte? If yes, go to (9).
Ben Murdoch097c5b22016-05-18 11:27:45 +0100497 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
498 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
499
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500 __ and_(ebx, kIsNotStringMask |
501 kStringRepresentationMask |
502 kStringEncodingMask |
503 kShortExternalStringMask);
504 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
505 __ j(zero, &seq_two_byte_string); // Go to (9).
506
Ben Murdoch097c5b22016-05-18 11:27:45 +0100507 // (2) Sequential one byte? If yes, go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 // Any other sequential string must be one byte.
509 __ and_(ebx, Immediate(kIsNotStringMask |
510 kStringRepresentationMask |
511 kShortExternalStringMask));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100512 __ j(zero, &seq_one_byte_string, Label::kNear); // Go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000513
Ben Murdoch097c5b22016-05-18 11:27:45 +0100514 // (3) Sequential or cons? If not, go to (6).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000515 // We check whether the subject string is a cons, since sequential strings
516 // have already been covered.
517 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
518 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
519 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
520 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
521 __ cmp(ebx, Immediate(kExternalStringTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100522 __ j(greater_equal, &not_seq_nor_cons); // Go to (6).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000523
524 // (4) Cons string. Check that it's flat.
525 // Replace subject with first string and reload instance type.
526 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
527 __ j(not_equal, &runtime);
528 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100529 __ jmp(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000530
531 // eax: sequential subject string (or look-alike, external string)
532 // edx: original subject string
533 // ecx: RegExp data (FixedArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100534 // (5) One byte sequential. Load regexp code for one byte.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000535 __ bind(&seq_one_byte_string);
536 // Load previous index and check range before edx is overwritten. We have
537 // to use edx instead of eax here because it might have been only made to
538 // look like a sequential string when it actually is an external string.
539 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
540 __ JumpIfNotSmi(ebx, &runtime);
541 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
542 __ j(above_equal, &runtime);
543 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataOneByteCodeOffset));
544 __ Move(ecx, Immediate(1)); // Type is one byte.
545
546 // (E) Carry on. String handling is done.
547 __ bind(&check_code);
548 // edx: irregexp code
549 // Check that the irregexp code has been generated for the actual string
550 // encoding. If it has, the field contains a code object otherwise it contains
551 // a smi (code flushing support).
552 __ JumpIfSmi(edx, &runtime);
553
554 // eax: subject string
555 // ebx: previous index (smi)
556 // edx: code
557 // ecx: encoding of subject string (1 if one_byte, 0 if two_byte);
558 // All checks done. Now push arguments for native regexp code.
559 Counters* counters = isolate()->counters();
560 __ IncrementCounter(counters->regexp_entry_native(), 1);
561
562 // Isolates: note we add an additional parameter here (isolate pointer).
563 static const int kRegExpExecuteArguments = 9;
564 __ EnterApiExitFrame(kRegExpExecuteArguments);
565
566 // Argument 9: Pass current isolate address.
567 __ mov(Operand(esp, 8 * kPointerSize),
568 Immediate(ExternalReference::isolate_address(isolate())));
569
570 // Argument 8: Indicate that this is a direct call from JavaScript.
571 __ mov(Operand(esp, 7 * kPointerSize), Immediate(1));
572
573 // Argument 7: Start (high end) of backtracking stack memory area.
574 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
575 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
576 __ mov(Operand(esp, 6 * kPointerSize), esi);
577
578 // Argument 6: Set the number of capture registers to zero to force global
579 // regexps to behave as non-global. This does not affect non-global regexps.
580 __ mov(Operand(esp, 5 * kPointerSize), Immediate(0));
581
582 // Argument 5: static offsets vector buffer.
583 __ mov(Operand(esp, 4 * kPointerSize),
584 Immediate(ExternalReference::address_of_static_offsets_vector(
585 isolate())));
586
587 // Argument 2: Previous index.
588 __ SmiUntag(ebx);
589 __ mov(Operand(esp, 1 * kPointerSize), ebx);
590
591 // Argument 1: Original subject string.
592 // The original subject is in the previous stack frame. Therefore we have to
593 // use ebp, which points exactly to one pointer size below the previous esp.
594 // (Because creating a new stack frame pushes the previous ebp onto the stack
595 // and thereby moves up esp by one kPointerSize.)
596 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
597 __ mov(Operand(esp, 0 * kPointerSize), esi);
598
599 // esi: original subject string
600 // eax: underlying subject string
601 // ebx: previous index
602 // ecx: encoding of subject string (1 if one_byte 0 if two_byte);
603 // edx: code
604 // Argument 4: End of string data
605 // Argument 3: Start of string data
606 // Prepare start and end index of the input.
607 // Load the length from the original sliced string if that is the case.
608 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
609 __ add(esi, edi); // Calculate input end wrt offset.
610 __ SmiUntag(edi);
611 __ add(ebx, edi); // Calculate input start wrt offset.
612
613 // ebx: start index of the input string
614 // esi: end index of the input string
615 Label setup_two_byte, setup_rest;
616 __ test(ecx, ecx);
617 __ j(zero, &setup_two_byte, Label::kNear);
618 __ SmiUntag(esi);
619 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqOneByteString::kHeaderSize));
620 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
621 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqOneByteString::kHeaderSize));
622 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
623 __ jmp(&setup_rest, Label::kNear);
624
625 __ bind(&setup_two_byte);
626 STATIC_ASSERT(kSmiTag == 0);
627 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
628 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
629 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
630 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
631 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
632
633 __ bind(&setup_rest);
634
635 // Locate the code entry and call it.
636 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
637 __ call(edx);
638
639 // Drop arguments and come back to JS mode.
640 __ LeaveApiExitFrame(true);
641
642 // Check the result.
643 Label success;
644 __ cmp(eax, 1);
645 // We expect exactly one result since we force the called regexp to behave
646 // as non-global.
647 __ j(equal, &success);
648 Label failure;
649 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
650 __ j(equal, &failure);
651 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
652 // If not exception it can only be retry. Handle that in the runtime system.
653 __ j(not_equal, &runtime);
654 // Result must now be exception. If there is no pending exception already a
655 // stack overflow (on the backtrack stack) was detected in RegExp code but
656 // haven't created the exception yet. Handle that in the runtime system.
657 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
658 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
659 isolate());
660 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
661 __ mov(eax, Operand::StaticVariable(pending_exception));
662 __ cmp(edx, eax);
663 __ j(equal, &runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000664
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000665 // For exception, throw the exception again.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000666 __ TailCallRuntime(Runtime::kRegExpExecReThrow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000667
668 __ bind(&failure);
669 // For failure to match, return null.
670 __ mov(eax, factory->null_value());
671 __ ret(4 * kPointerSize);
672
673 // Load RegExp data.
674 __ bind(&success);
675 __ mov(eax, Operand(esp, kJSRegExpOffset));
676 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
677 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
678 // Calculate number of capture registers (number_of_captures + 1) * 2.
679 STATIC_ASSERT(kSmiTag == 0);
680 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
681 __ add(edx, Immediate(2)); // edx was a smi.
682
683 // edx: Number of capture registers
684 // Load last_match_info which is still known to be a fast case JSArray.
685 // Check that the fourth object is a JSArray object.
686 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
687 __ JumpIfSmi(eax, &runtime);
688 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
689 __ j(not_equal, &runtime);
690 // Check that the JSArray is in fast case.
691 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
692 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
693 __ cmp(eax, factory->fixed_array_map());
694 __ j(not_equal, &runtime);
695 // Check that the last match info has space for the capture registers and the
696 // additional information.
697 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
698 __ SmiUntag(eax);
699 __ sub(eax, Immediate(RegExpImpl::kLastMatchOverhead));
700 __ cmp(edx, eax);
701 __ j(greater, &runtime);
702
703 // ebx: last_match_info backing store (FixedArray)
704 // edx: number of capture registers
705 // Store the capture count.
706 __ SmiTag(edx); // Number of capture registers to smi.
707 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
708 __ SmiUntag(edx); // Number of capture registers back from smi.
709 // Store last subject and last input.
710 __ mov(eax, Operand(esp, kSubjectOffset));
711 __ mov(ecx, eax);
712 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
713 __ RecordWriteField(ebx, RegExpImpl::kLastSubjectOffset, eax, edi,
714 kDontSaveFPRegs);
715 __ mov(eax, ecx);
716 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
717 __ RecordWriteField(ebx, RegExpImpl::kLastInputOffset, eax, edi,
718 kDontSaveFPRegs);
719
720 // Get the static offsets vector filled by the native regexp code.
721 ExternalReference address_of_static_offsets_vector =
722 ExternalReference::address_of_static_offsets_vector(isolate());
723 __ mov(ecx, Immediate(address_of_static_offsets_vector));
724
725 // ebx: last_match_info backing store (FixedArray)
726 // ecx: offsets vector
727 // edx: number of capture registers
728 Label next_capture, done;
729 // Capture register counter starts from number of capture registers and
730 // counts down until wraping after zero.
731 __ bind(&next_capture);
732 __ sub(edx, Immediate(1));
733 __ j(negative, &done, Label::kNear);
734 // Read the value from the static offsets vector buffer.
735 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
736 __ SmiTag(edi);
737 // Store the smi value in the last match info.
738 __ mov(FieldOperand(ebx,
739 edx,
740 times_pointer_size,
741 RegExpImpl::kFirstCaptureOffset),
742 edi);
743 __ jmp(&next_capture);
744 __ bind(&done);
745
746 // Return last match info.
747 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
748 __ ret(4 * kPointerSize);
749
750 // Do the runtime call to execute the regexp.
751 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000752 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000753
754 // Deferred code for string handling.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100755 // (6) Long external string? If not, go to (10).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756 __ bind(&not_seq_nor_cons);
757 // Compare flags are still set from (3).
758 __ j(greater, &not_long_external, Label::kNear); // Go to (10).
759
Ben Murdoch097c5b22016-05-18 11:27:45 +0100760 // (7) External string. Short external strings have been ruled out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000761 __ bind(&external_string);
762 // Reload instance type.
763 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
764 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
765 if (FLAG_debug_code) {
766 // Assert that we do not have a cons or slice (indirect strings) here.
767 // Sequential strings have already been ruled out.
Ben Murdochda12d292016-06-02 14:46:10 +0100768 __ test_b(ebx, Immediate(kIsIndirectStringMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 __ Assert(zero, kExternalStringExpectedButNotFound);
770 }
771 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
772 // Move the pointer so that offset-wise, it looks like a sequential string.
773 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
774 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
775 STATIC_ASSERT(kTwoByteStringTag == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100776 // (8) Is the external string one byte? If yes, go to (5).
Ben Murdochda12d292016-06-02 14:46:10 +0100777 __ test_b(ebx, Immediate(kStringEncodingMask));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100778 __ j(not_zero, &seq_one_byte_string); // Go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000779
780 // eax: sequential subject string (or look-alike, external string)
781 // edx: original subject string
782 // ecx: RegExp data (FixedArray)
Ben Murdoch097c5b22016-05-18 11:27:45 +0100783 // (9) Two byte sequential. Load regexp code for two byte. Go to (E).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000784 __ bind(&seq_two_byte_string);
785 // Load previous index and check range before edx is overwritten. We have
786 // to use edx instead of eax here because it might have been only made to
787 // look like a sequential string when it actually is an external string.
788 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
789 __ JumpIfNotSmi(ebx, &runtime);
790 __ cmp(ebx, FieldOperand(edx, String::kLengthOffset));
791 __ j(above_equal, &runtime);
792 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
793 __ Move(ecx, Immediate(0)); // Type is two byte.
794 __ jmp(&check_code); // Go to (E).
795
796 // (10) Not a string or a short external string? If yes, bail out to runtime.
797 __ bind(&not_long_external);
798 // Catch non-string subject or short external string.
799 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
800 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
801 __ j(not_zero, &runtime);
802
Ben Murdoch097c5b22016-05-18 11:27:45 +0100803 // (11) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000804 // Load offset into edi and replace subject string with parent.
805 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
806 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100807 __ jmp(&check_underlying); // Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000808#endif // V8_INTERPRETED_REGEXP
809}
810
811
812static int NegativeComparisonResult(Condition cc) {
813 DCHECK(cc != equal);
814 DCHECK((cc == less) || (cc == less_equal)
815 || (cc == greater) || (cc == greater_equal));
816 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
817}
818
819
820static void CheckInputType(MacroAssembler* masm, Register input,
821 CompareICState::State expected, Label* fail) {
822 Label ok;
823 if (expected == CompareICState::SMI) {
824 __ JumpIfNotSmi(input, fail);
825 } else if (expected == CompareICState::NUMBER) {
826 __ JumpIfSmi(input, &ok);
827 __ cmp(FieldOperand(input, HeapObject::kMapOffset),
828 Immediate(masm->isolate()->factory()->heap_number_map()));
829 __ j(not_equal, fail);
830 }
831 // We could be strict about internalized/non-internalized here, but as long as
832 // hydrogen doesn't care, the stub doesn't have to care either.
833 __ bind(&ok);
834}
835
836
837static void BranchIfNotInternalizedString(MacroAssembler* masm,
838 Label* label,
839 Register object,
840 Register scratch) {
841 __ JumpIfSmi(object, label);
842 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
843 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
844 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
845 __ test(scratch, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
846 __ j(not_zero, label);
847}
848
849
850void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000851 Label runtime_call, check_unequal_objects;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 Condition cc = GetCondition();
853
854 Label miss;
855 CheckInputType(masm, edx, left(), &miss);
856 CheckInputType(masm, eax, right(), &miss);
857
858 // Compare two smis.
859 Label non_smi, smi_done;
860 __ mov(ecx, edx);
861 __ or_(ecx, eax);
862 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
863 __ sub(edx, eax); // Return on the result of the subtraction.
864 __ j(no_overflow, &smi_done, Label::kNear);
865 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
866 __ bind(&smi_done);
867 __ mov(eax, edx);
868 __ ret(0);
869 __ bind(&non_smi);
870
871 // NOTICE! This code is only reached after a smi-fast-case check, so
872 // it is certain that at least one operand isn't a smi.
873
874 // Identical objects can be compared fast, but there are some tricky cases
875 // for NaN and undefined.
876 Label generic_heap_number_comparison;
877 {
878 Label not_identical;
879 __ cmp(eax, edx);
880 __ j(not_equal, &not_identical);
881
882 if (cc != equal) {
883 // Check for undefined. undefined OP undefined is false even though
884 // undefined == undefined.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000885 __ cmp(edx, isolate()->factory()->undefined_value());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100886 Label check_for_nan;
887 __ j(not_equal, &check_for_nan, Label::kNear);
888 __ Move(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
889 __ ret(0);
890 __ bind(&check_for_nan);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 }
892
893 // Test for NaN. Compare heap numbers in a general way,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000894 // to handle NaNs correctly.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000895 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
896 Immediate(isolate()->factory()->heap_number_map()));
897 __ j(equal, &generic_heap_number_comparison, Label::kNear);
898 if (cc != equal) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000899 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
900 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000901 // Call runtime on identical JSObjects. Otherwise return equal.
Ben Murdochda12d292016-06-02 14:46:10 +0100902 __ cmpb(ecx, Immediate(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000903 __ j(above_equal, &runtime_call, Label::kFar);
904 // Call runtime on identical symbols since we need to throw a TypeError.
Ben Murdochda12d292016-06-02 14:46:10 +0100905 __ cmpb(ecx, Immediate(SYMBOL_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000906 __ j(equal, &runtime_call, Label::kFar);
907 // Call runtime on identical SIMD values since we must throw a TypeError.
Ben Murdochda12d292016-06-02 14:46:10 +0100908 __ cmpb(ecx, Immediate(SIMD128_VALUE_TYPE));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000909 __ j(equal, &runtime_call, Label::kFar);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000910 }
911 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
912 __ ret(0);
913
914
915 __ bind(&not_identical);
916 }
917
918 // Strict equality can quickly decide whether objects are equal.
919 // Non-strict object equality is slower, so it is handled later in the stub.
920 if (cc == equal && strict()) {
921 Label slow; // Fallthrough label.
922 Label not_smis;
923 // If we're doing a strict equality comparison, we don't have to do
924 // type conversion, so we generate code to do fast comparison for objects
925 // and oddballs. Non-smi numbers and strings still go through the usual
926 // slow-case code.
927 // If either is a Smi (we know that not both are), then they can only
928 // be equal if the other is a HeapNumber. If so, use the slow case.
929 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000930 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931 __ mov(ecx, Immediate(kSmiTagMask));
932 __ and_(ecx, eax);
933 __ test(ecx, edx);
934 __ j(not_zero, &not_smis, Label::kNear);
935 // One operand is a smi.
936
937 // Check whether the non-smi is a heap number.
938 STATIC_ASSERT(kSmiTagMask == 1);
939 // ecx still holds eax & kSmiTag, which is either zero or one.
940 __ sub(ecx, Immediate(0x01));
941 __ mov(ebx, edx);
942 __ xor_(ebx, eax);
943 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
944 __ xor_(ebx, eax);
945 // if eax was smi, ebx is now edx, else eax.
946
947 // Check if the non-smi operand is a heap number.
948 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
949 Immediate(isolate()->factory()->heap_number_map()));
950 // If heap number, handle it in the slow case.
951 __ j(equal, &slow, Label::kNear);
952 // Return non-equal (ebx is not zero)
953 __ mov(eax, ebx);
954 __ ret(0);
955
956 __ bind(&not_smis);
957 // If either operand is a JSObject or an oddball value, then they are not
958 // equal since their pointers are different
959 // There is no test for undetectability in strict equality.
960
961 // Get the type of the first operand.
962 // If the first object is a JS object, we have done pointer comparison.
963 Label first_non_object;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000964 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
965 __ CmpObjectType(eax, FIRST_JS_RECEIVER_TYPE, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000966 __ j(below, &first_non_object, Label::kNear);
967
968 // Return non-zero (eax is not zero)
969 Label return_not_equal;
970 STATIC_ASSERT(kHeapObjectTag != 0);
971 __ bind(&return_not_equal);
972 __ ret(0);
973
974 __ bind(&first_non_object);
975 // Check for oddballs: true, false, null, undefined.
976 __ CmpInstanceType(ecx, ODDBALL_TYPE);
977 __ j(equal, &return_not_equal);
978
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000979 __ CmpObjectType(edx, FIRST_JS_RECEIVER_TYPE, ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000980 __ j(above_equal, &return_not_equal);
981
982 // Check for oddballs: true, false, null, undefined.
983 __ CmpInstanceType(ecx, ODDBALL_TYPE);
984 __ j(equal, &return_not_equal);
985
986 // Fall through to the general case.
987 __ bind(&slow);
988 }
989
990 // Generate the number comparison code.
991 Label non_number_comparison;
992 Label unordered;
993 __ bind(&generic_heap_number_comparison);
994 FloatingPointHelper::CheckFloatOperands(
995 masm, &non_number_comparison, ebx);
996 FloatingPointHelper::LoadFloatOperand(masm, eax);
997 FloatingPointHelper::LoadFloatOperand(masm, edx);
998 __ FCmp();
999
1000 // Don't base result on EFLAGS when a NaN is involved.
1001 __ j(parity_even, &unordered, Label::kNear);
1002
1003 Label below_label, above_label;
1004 // Return a result of -1, 0, or 1, based on EFLAGS.
1005 __ j(below, &below_label, Label::kNear);
1006 __ j(above, &above_label, Label::kNear);
1007
1008 __ Move(eax, Immediate(0));
1009 __ ret(0);
1010
1011 __ bind(&below_label);
1012 __ mov(eax, Immediate(Smi::FromInt(-1)));
1013 __ ret(0);
1014
1015 __ bind(&above_label);
1016 __ mov(eax, Immediate(Smi::FromInt(1)));
1017 __ ret(0);
1018
1019 // If one of the numbers was NaN, then the result is always false.
1020 // The cc is never not-equal.
1021 __ bind(&unordered);
1022 DCHECK(cc != not_equal);
1023 if (cc == less || cc == less_equal) {
1024 __ mov(eax, Immediate(Smi::FromInt(1)));
1025 } else {
1026 __ mov(eax, Immediate(Smi::FromInt(-1)));
1027 }
1028 __ ret(0);
1029
1030 // The number comparison code did not provide a valid result.
1031 __ bind(&non_number_comparison);
1032
1033 // Fast negative check for internalized-to-internalized equality.
1034 Label check_for_strings;
1035 if (cc == equal) {
1036 BranchIfNotInternalizedString(masm, &check_for_strings, eax, ecx);
1037 BranchIfNotInternalizedString(masm, &check_for_strings, edx, ecx);
1038
1039 // We've already checked for object identity, so if both operands
1040 // are internalized they aren't equal. Register eax already holds a
1041 // non-zero value, which indicates not equal, so just return.
1042 __ ret(0);
1043 }
1044
1045 __ bind(&check_for_strings);
1046
1047 __ JumpIfNotBothSequentialOneByteStrings(edx, eax, ecx, ebx,
1048 &check_unequal_objects);
1049
1050 // Inline comparison of one-byte strings.
1051 if (cc == equal) {
1052 StringHelper::GenerateFlatOneByteStringEquals(masm, edx, eax, ecx, ebx);
1053 } else {
1054 StringHelper::GenerateCompareFlatOneByteStrings(masm, edx, eax, ecx, ebx,
1055 edi);
1056 }
1057#ifdef DEBUG
1058 __ Abort(kUnexpectedFallThroughFromStringComparison);
1059#endif
1060
1061 __ bind(&check_unequal_objects);
1062 if (cc == equal && !strict()) {
1063 // Non-strict equality. Objects are unequal if
1064 // they are both JSObjects and not undetectable,
1065 // and their pointers are different.
Ben Murdochda12d292016-06-02 14:46:10 +01001066 Label return_equal, return_unequal, undetectable;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001067 // At most one is a smi, so we can test for smi by adding the two.
1068 // A smi plus a heap object has the low bit set, a heap object plus
1069 // a heap object has the low bit clear.
1070 STATIC_ASSERT(kSmiTag == 0);
1071 STATIC_ASSERT(kSmiTagMask == 1);
1072 __ lea(ecx, Operand(eax, edx, times_1, 0));
1073 __ test(ecx, Immediate(kSmiTagMask));
Ben Murdochda12d292016-06-02 14:46:10 +01001074 __ j(not_zero, &runtime_call);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001075
1076 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
1077 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
1078
1079 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001080 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001081 __ j(not_zero, &undetectable, Label::kNear);
1082 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001083 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001084 __ j(not_zero, &return_unequal, Label::kNear);
1085
1086 __ CmpInstanceType(ebx, FIRST_JS_RECEIVER_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001087 __ j(below, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001088 __ CmpInstanceType(ecx, FIRST_JS_RECEIVER_TYPE);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001089 __ j(below, &runtime_call, Label::kNear);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001090
1091 __ bind(&return_unequal);
1092 // Return non-equal by returning the non-zero object pointer in eax.
1093 __ ret(0); // eax, edx were pushed
1094
1095 __ bind(&undetectable);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001096 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
Ben Murdochda12d292016-06-02 14:46:10 +01001097 Immediate(1 << Map::kIsUndetectable));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001098 __ j(zero, &return_unequal, Label::kNear);
Ben Murdochda12d292016-06-02 14:46:10 +01001099
1100 // If both sides are JSReceivers, then the result is false according to
1101 // the HTML specification, which says that only comparisons with null or
1102 // undefined are affected by special casing for document.all.
1103 __ CmpInstanceType(ebx, ODDBALL_TYPE);
1104 __ j(zero, &return_equal, Label::kNear);
1105 __ CmpInstanceType(ecx, ODDBALL_TYPE);
1106 __ j(not_zero, &return_unequal, Label::kNear);
1107
1108 __ bind(&return_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001109 __ Move(eax, Immediate(EQUAL));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001110 __ ret(0); // eax, edx were pushed
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001111 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001112 __ bind(&runtime_call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001113
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001114 if (cc == equal) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001115 {
1116 FrameScope scope(masm, StackFrame::INTERNAL);
1117 __ Push(edx);
1118 __ Push(eax);
1119 __ CallRuntime(strict() ? Runtime::kStrictEqual : Runtime::kEqual);
1120 }
1121 // Turn true into 0 and false into some non-zero value.
1122 STATIC_ASSERT(EQUAL == 0);
1123 __ sub(eax, Immediate(isolate()->factory()->true_value()));
1124 __ Ret();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001125 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001126 // Push arguments below the return address.
1127 __ pop(ecx);
1128 __ push(edx);
1129 __ push(eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001130 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc))));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001131
1132 // Restore return address on the stack.
1133 __ push(ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001134 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1135 // tagged as a small integer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001136 __ TailCallRuntime(Runtime::kCompare);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001137 }
1138
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001139 __ bind(&miss);
1140 GenerateMiss(masm);
1141}
1142
1143
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001144static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
1145 // eax : number of arguments to the construct function
1146 // ebx : feedback vector
1147 // edx : slot in feedback vector (Smi)
1148 // edi : the function to call
1149
1150 {
1151 FrameScope scope(masm, StackFrame::INTERNAL);
1152
1153 // Number-of-arguments register must be smi-tagged to call out.
1154 __ SmiTag(eax);
1155 __ push(eax);
1156 __ push(edi);
1157 __ push(edx);
1158 __ push(ebx);
1159
1160 __ CallStub(stub);
1161
1162 __ pop(ebx);
1163 __ pop(edx);
1164 __ pop(edi);
1165 __ pop(eax);
1166 __ SmiUntag(eax);
1167 }
1168}
1169
1170
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001171static void GenerateRecordCallTarget(MacroAssembler* masm) {
1172 // Cache the called function in a feedback vector slot. Cache states
1173 // are uninitialized, monomorphic (indicated by a JSFunction), and
1174 // megamorphic.
1175 // eax : number of arguments to the construct function
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001176 // ebx : feedback vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001177 // edx : slot in feedback vector (Smi)
1178 // edi : the function to call
1179 Isolate* isolate = masm->isolate();
1180 Label initialize, done, miss, megamorphic, not_array_function;
1181
1182 // Load the cache state into ecx.
1183 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1184 FixedArray::kHeaderSize));
1185
1186 // A monomorphic cache hit or an already megamorphic state: invoke the
1187 // function without changing the state.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001188 // We don't know if ecx is a WeakCell or a Symbol, but it's harmless to read
1189 // at this position in a symbol (see static asserts in
1190 // type-feedback-vector.h).
1191 Label check_allocation_site;
1192 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001193 __ j(equal, &done, Label::kFar);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001194 __ CompareRoot(ecx, Heap::kmegamorphic_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001195 __ j(equal, &done, Label::kFar);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001196 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1197 Heap::kWeakCellMapRootIndex);
1198 __ j(not_equal, &check_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001199
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001200 // If the weak cell is cleared, we have a new chance to become monomorphic.
1201 __ JumpIfSmi(FieldOperand(ecx, WeakCell::kValueOffset), &initialize);
1202 __ jmp(&megamorphic);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001203
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001204 __ bind(&check_allocation_site);
1205 // If we came here, we need to see if we are the array function.
1206 // If we didn't have a matching function, and we didn't find the megamorph
1207 // sentinel, then we have in the slot either some other function or an
1208 // AllocationSite.
1209 __ CompareRoot(FieldOperand(ecx, 0), Heap::kAllocationSiteMapRootIndex);
1210 __ j(not_equal, &miss);
1211
1212 // Make sure the function is the Array() function
1213 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1214 __ cmp(edi, ecx);
1215 __ j(not_equal, &megamorphic);
1216 __ jmp(&done, Label::kFar);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001217
1218 __ bind(&miss);
1219
1220 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1221 // megamorphic.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001222 __ CompareRoot(ecx, Heap::kuninitialized_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001223 __ j(equal, &initialize);
1224 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1225 // write-barrier is needed.
1226 __ bind(&megamorphic);
1227 __ mov(
1228 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1229 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
1230 __ jmp(&done, Label::kFar);
1231
1232 // An uninitialized cache is patched with the function or sentinel to
1233 // indicate the ElementsKind if function is the Array constructor.
1234 __ bind(&initialize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001235 // Make sure the function is the Array() function
1236 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1237 __ cmp(edi, ecx);
1238 __ j(not_equal, &not_array_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001239
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001240 // The target function is the Array constructor,
1241 // Create an AllocationSite if we don't already have it, store it in the
1242 // slot.
1243 CreateAllocationSiteStub create_stub(isolate);
1244 CallStubInRecordCallTarget(masm, &create_stub);
1245 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001246
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001247 __ bind(&not_array_function);
1248 CreateWeakCellStub weak_cell_stub(isolate);
1249 CallStubInRecordCallTarget(masm, &weak_cell_stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001250 __ bind(&done);
1251}
1252
1253
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001254void CallConstructStub::Generate(MacroAssembler* masm) {
1255 // eax : number of arguments
1256 // ebx : feedback vector
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001257 // edx : slot in feedback vector (Smi, for RecordCallTarget)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001258 // edi : constructor function
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001259
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001260 Label non_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001261 // Check that function is not a smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001262 __ JumpIfSmi(edi, &non_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001263 // Check that function is a JSFunction.
1264 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001265 __ j(not_equal, &non_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001266
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001267 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001268
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001269 Label feedback_register_initialized;
1270 // Put the AllocationSite from the feedback vector into ebx, or undefined.
1271 __ mov(ebx, FieldOperand(ebx, edx, times_half_pointer_size,
1272 FixedArray::kHeaderSize));
1273 Handle<Map> allocation_site_map = isolate()->factory()->allocation_site_map();
1274 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
1275 __ j(equal, &feedback_register_initialized);
1276 __ mov(ebx, isolate()->factory()->undefined_value());
1277 __ bind(&feedback_register_initialized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001278
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001279 __ AssertUndefinedOrAllocationSite(ebx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001280
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001281 // Pass new target to construct stub.
1282 __ mov(edx, edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001284 // Tail call to the function-specific construct stub (still in the caller
1285 // context at this point).
1286 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
1287 __ mov(ecx, FieldOperand(ecx, SharedFunctionInfo::kConstructStubOffset));
1288 __ lea(ecx, FieldOperand(ecx, Code::kHeaderSize));
1289 __ jmp(ecx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001290
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001291 __ bind(&non_function);
1292 __ mov(edx, edi);
1293 __ Jump(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001294}
1295
1296
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001297void CallICStub::HandleArrayCase(MacroAssembler* masm, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001298 // edi - function
1299 // edx - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001300 // ebx - vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001301 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1302 __ cmp(edi, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001303 __ j(not_equal, miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001304
1305 __ mov(eax, arg_count());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001306 // Reload ecx.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001307 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1308 FixedArray::kHeaderSize));
1309
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001310 // Increment the call count for monomorphic function calls.
1311 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1312 FixedArray::kHeaderSize + kPointerSize),
1313 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001314
1315 __ mov(ebx, ecx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001316 __ mov(edx, edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001317 ArrayConstructorStub stub(masm->isolate(), arg_count());
1318 __ TailCallStub(&stub);
1319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320 // Unreachable.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001321}
1322
1323
1324void CallICStub::Generate(MacroAssembler* masm) {
1325 // edi - function
1326 // edx - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001327 // ebx - vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001328 Isolate* isolate = masm->isolate();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001329 Label extra_checks_or_miss, call, call_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001330 int argc = arg_count();
1331 ParameterCount actual(argc);
1332
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001333 // The checks. First, does edi match the recorded monomorphic target?
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001334 __ mov(ecx, FieldOperand(ebx, edx, times_half_pointer_size,
1335 FixedArray::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001336
1337 // We don't know that we have a weak cell. We might have a private symbol
1338 // or an AllocationSite, but the memory is safe to examine.
1339 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
1340 // FixedArray.
1341 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
1342 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
1343 // computed, meaning that it can't appear to be a pointer. If the low bit is
1344 // 0, then hash is computed, but the 0 bit prevents the field from appearing
1345 // to be a pointer.
1346 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
1347 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
1348 WeakCell::kValueOffset &&
1349 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
1350
1351 __ cmp(edi, FieldOperand(ecx, WeakCell::kValueOffset));
1352 __ j(not_equal, &extra_checks_or_miss);
1353
1354 // The compare above could have been a SMI/SMI comparison. Guard against this
1355 // convincing us that we have a monomorphic JSFunction.
1356 __ JumpIfSmi(edi, &extra_checks_or_miss);
1357
1358 // Increment the call count for monomorphic function calls.
1359 __ add(FieldOperand(ebx, edx, times_half_pointer_size,
1360 FixedArray::kHeaderSize + kPointerSize),
1361 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
1362
1363 __ bind(&call_function);
1364 __ Set(eax, argc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001365 __ Jump(masm->isolate()->builtins()->CallFunction(convert_mode(),
1366 tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001367 RelocInfo::CODE_TARGET);
1368
1369 __ bind(&extra_checks_or_miss);
1370 Label uninitialized, miss, not_allocation_site;
1371
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001372 __ cmp(ecx, Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001373 __ j(equal, &call);
1374
1375 // Check if we have an allocation site.
1376 __ CompareRoot(FieldOperand(ecx, HeapObject::kMapOffset),
1377 Heap::kAllocationSiteMapRootIndex);
1378 __ j(not_equal, &not_allocation_site);
1379
1380 // We have an allocation site.
1381 HandleArrayCase(masm, &miss);
1382
1383 __ bind(&not_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001384
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001385 // The following cases attempt to handle MISS cases without going to the
1386 // runtime.
1387 if (FLAG_trace_ic) {
1388 __ jmp(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001389 }
1390
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001391 __ cmp(ecx, Immediate(TypeFeedbackVector::UninitializedSentinel(isolate)));
1392 __ j(equal, &uninitialized);
1393
1394 // We are going megamorphic. If the feedback is a JSFunction, it is fine
1395 // to handle it here. More complex cases are dealt with in the runtime.
1396 __ AssertNotSmi(ecx);
1397 __ CmpObjectType(ecx, JS_FUNCTION_TYPE, ecx);
1398 __ j(not_equal, &miss);
1399 __ mov(
1400 FieldOperand(ebx, edx, times_half_pointer_size, FixedArray::kHeaderSize),
1401 Immediate(TypeFeedbackVector::MegamorphicSentinel(isolate)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001402
1403 __ bind(&call);
1404 __ Set(eax, argc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001405 __ Jump(masm->isolate()->builtins()->Call(convert_mode(), tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001406 RelocInfo::CODE_TARGET);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001407
1408 __ bind(&uninitialized);
1409
1410 // We are going monomorphic, provided we actually have a JSFunction.
1411 __ JumpIfSmi(edi, &miss);
1412
1413 // Goto miss case if we do not have a function.
1414 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
1415 __ j(not_equal, &miss);
1416
1417 // Make sure the function is not the Array() function, which requires special
1418 // behavior on MISS.
1419 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, ecx);
1420 __ cmp(edi, ecx);
1421 __ j(equal, &miss);
1422
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001423 // Make sure the function belongs to the same native context.
1424 __ mov(ecx, FieldOperand(edi, JSFunction::kContextOffset));
1425 __ mov(ecx, ContextOperand(ecx, Context::NATIVE_CONTEXT_INDEX));
1426 __ cmp(ecx, NativeContextOperand());
1427 __ j(not_equal, &miss);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001428
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001429 // Initialize the call counter.
1430 __ mov(FieldOperand(ebx, edx, times_half_pointer_size,
1431 FixedArray::kHeaderSize + kPointerSize),
1432 Immediate(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001433
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001434 // Store the function. Use a stub since we need a frame for allocation.
1435 // ebx - vector
1436 // edx - slot
1437 // edi - function
1438 {
1439 FrameScope scope(masm, StackFrame::INTERNAL);
1440 CreateWeakCellStub create_stub(isolate);
1441 __ push(edi);
1442 __ CallStub(&create_stub);
1443 __ pop(edi);
1444 }
1445
1446 __ jmp(&call_function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001447
1448 // We are here because tracing is on or we encountered a MISS case we can't
1449 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001450 __ bind(&miss);
1451 GenerateMiss(masm);
1452
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001453 __ jmp(&call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001454
1455 // Unreachable
1456 __ int3();
1457}
1458
1459
1460void CallICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001461 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001462
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001463 // Push the function and feedback info.
1464 __ push(edi);
1465 __ push(ebx);
1466 __ push(edx);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001467
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001468 // Call the entry.
1469 __ CallRuntime(Runtime::kCallIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001470
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001471 // Move result to edi and exit the internal frame.
1472 __ mov(edi, eax);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001473}
1474
1475
1476bool CEntryStub::NeedsImmovableCode() {
1477 return false;
1478}
1479
1480
1481void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
1482 CEntryStub::GenerateAheadOfTime(isolate);
1483 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
1484 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
1485 // It is important that the store buffer overflow stubs are generated first.
1486 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
1487 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001488 CreateWeakCellStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001489 BinaryOpICStub::GenerateAheadOfTime(isolate);
1490 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001491 StoreFastElementStub::GenerateAheadOfTime(isolate);
1492 TypeofStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001493}
1494
1495
1496void CodeStub::GenerateFPStubs(Isolate* isolate) {
1497 CEntryStub save_doubles(isolate, 1, kSaveFPRegs);
1498 // Stubs might already be in the snapshot, detect that and don't regenerate,
1499 // which would lead to code stub initialization state being messed up.
1500 Code* save_doubles_code;
1501 if (!save_doubles.FindCodeInCache(&save_doubles_code)) {
1502 save_doubles_code = *(save_doubles.GetCode());
1503 }
1504 isolate->set_fp_stubs_generated(true);
1505}
1506
1507
1508void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1509 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1510 stub.GetCode();
1511}
1512
1513
1514void CEntryStub::Generate(MacroAssembler* masm) {
1515 // eax: number of arguments including receiver
1516 // ebx: pointer to C function (C callee-saved)
1517 // ebp: frame pointer (restored after C call)
1518 // esp: stack pointer (restored after C call)
1519 // esi: current context (C callee-saved)
1520 // edi: JS function of the caller (C callee-saved)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001521 //
1522 // If argv_in_register():
1523 // ecx: pointer to the first argument
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001524
1525 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1526
Ben Murdoch097c5b22016-05-18 11:27:45 +01001527 // Reserve space on the stack for the three arguments passed to the call. If
1528 // result size is greater than can be returned in registers, also reserve
1529 // space for the hidden argument for the result location, and space for the
1530 // result itself.
1531 int arg_stack_space = result_size() < 3 ? 3 : 4 + result_size();
1532
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001533 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001534 if (argv_in_register()) {
1535 DCHECK(!save_doubles());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001536 __ EnterApiExitFrame(arg_stack_space);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001537
1538 // Move argc and argv into the correct registers.
1539 __ mov(esi, ecx);
1540 __ mov(edi, eax);
1541 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +01001542 __ EnterExitFrame(arg_stack_space, save_doubles());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001543 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001544
1545 // ebx: pointer to C function (C callee-saved)
1546 // ebp: frame pointer (restored after C call)
1547 // esp: stack pointer (restored after C call)
1548 // edi: number of arguments including receiver (C callee-saved)
1549 // esi: pointer to the first argument (C callee-saved)
1550
1551 // Result returned in eax, or eax+edx if result size is 2.
1552
1553 // Check stack alignment.
1554 if (FLAG_debug_code) {
1555 __ CheckStackAlignment();
1556 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001557 // Call C function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001558 if (result_size() <= 2) {
1559 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
1560 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
1561 __ mov(Operand(esp, 2 * kPointerSize),
1562 Immediate(ExternalReference::isolate_address(isolate())));
1563 } else {
1564 DCHECK_EQ(3, result_size());
1565 // Pass a pointer to the result location as the first argument.
1566 __ lea(eax, Operand(esp, 4 * kPointerSize));
1567 __ mov(Operand(esp, 0 * kPointerSize), eax);
1568 __ mov(Operand(esp, 1 * kPointerSize), edi); // argc.
1569 __ mov(Operand(esp, 2 * kPointerSize), esi); // argv.
1570 __ mov(Operand(esp, 3 * kPointerSize),
1571 Immediate(ExternalReference::isolate_address(isolate())));
1572 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001573 __ call(ebx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001574
1575 if (result_size() > 2) {
1576 DCHECK_EQ(3, result_size());
1577#ifndef _WIN32
1578 // Restore the "hidden" argument on the stack which was popped by caller.
1579 __ sub(esp, Immediate(kPointerSize));
1580#endif
1581 // Read result values stored on stack. Result is stored above the arguments.
1582 __ mov(kReturnRegister0, Operand(esp, 4 * kPointerSize));
1583 __ mov(kReturnRegister1, Operand(esp, 5 * kPointerSize));
1584 __ mov(kReturnRegister2, Operand(esp, 6 * kPointerSize));
1585 }
1586 // Result is in eax, edx:eax or edi:edx:eax - do not destroy these registers!
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001587
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001588 // Check result for exception sentinel.
1589 Label exception_returned;
1590 __ cmp(eax, isolate()->factory()->exception());
1591 __ j(equal, &exception_returned);
1592
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001593 // Check that there is no pending exception, otherwise we
1594 // should have returned the exception sentinel.
1595 if (FLAG_debug_code) {
1596 __ push(edx);
1597 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1598 Label okay;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001599 ExternalReference pending_exception_address(
1600 Isolate::kPendingExceptionAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001601 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
1602 // Cannot use check here as it attempts to generate call into runtime.
1603 __ j(equal, &okay, Label::kNear);
1604 __ int3();
1605 __ bind(&okay);
1606 __ pop(edx);
1607 }
1608
1609 // Exit the JavaScript to C++ exit frame.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001610 __ LeaveExitFrame(save_doubles(), !argv_in_register());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001611 __ ret(0);
1612
1613 // Handling of exception.
1614 __ bind(&exception_returned);
1615
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001616 ExternalReference pending_handler_context_address(
1617 Isolate::kPendingHandlerContextAddress, isolate());
1618 ExternalReference pending_handler_code_address(
1619 Isolate::kPendingHandlerCodeAddress, isolate());
1620 ExternalReference pending_handler_offset_address(
1621 Isolate::kPendingHandlerOffsetAddress, isolate());
1622 ExternalReference pending_handler_fp_address(
1623 Isolate::kPendingHandlerFPAddress, isolate());
1624 ExternalReference pending_handler_sp_address(
1625 Isolate::kPendingHandlerSPAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001626
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001627 // Ask the runtime for help to determine the handler. This will set eax to
1628 // contain the current pending exception, don't clobber it.
1629 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
1630 isolate());
1631 {
1632 FrameScope scope(masm, StackFrame::MANUAL);
1633 __ PrepareCallCFunction(3, eax);
1634 __ mov(Operand(esp, 0 * kPointerSize), Immediate(0)); // argc.
1635 __ mov(Operand(esp, 1 * kPointerSize), Immediate(0)); // argv.
1636 __ mov(Operand(esp, 2 * kPointerSize),
1637 Immediate(ExternalReference::isolate_address(isolate())));
1638 __ CallCFunction(find_handler, 3);
1639 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001640
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001641 // Retrieve the handler context, SP and FP.
1642 __ mov(esi, Operand::StaticVariable(pending_handler_context_address));
1643 __ mov(esp, Operand::StaticVariable(pending_handler_sp_address));
1644 __ mov(ebp, Operand::StaticVariable(pending_handler_fp_address));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001645
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001646 // If the handler is a JS frame, restore the context to the frame. Note that
1647 // the context will be set to (esi == 0) for non-JS frames.
1648 Label skip;
1649 __ test(esi, esi);
1650 __ j(zero, &skip, Label::kNear);
1651 __ mov(Operand(ebp, StandardFrameConstants::kContextOffset), esi);
1652 __ bind(&skip);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001653
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001654 // Compute the handler entry address and jump to it.
1655 __ mov(edi, Operand::StaticVariable(pending_handler_code_address));
1656 __ mov(edx, Operand::StaticVariable(pending_handler_offset_address));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001657 // Check whether it's a turbofanned exception handler code before jump to it.
1658 Label not_turbo;
1659 __ push(eax);
1660 __ mov(eax, Operand(edi, Code::kKindSpecificFlags1Offset - kHeapObjectTag));
1661 __ and_(eax, Immediate(1 << Code::kIsTurbofannedBit));
1662 __ j(zero, &not_turbo);
1663 __ fninit();
1664 __ fld1();
1665 __ bind(&not_turbo);
1666 __ pop(eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001667 __ lea(edi, FieldOperand(edi, edx, times_1, Code::kHeaderSize));
1668 __ jmp(edi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001669}
1670
1671
1672void JSEntryStub::Generate(MacroAssembler* masm) {
1673 Label invoke, handler_entry, exit;
1674 Label not_outermost_js, not_outermost_js_2;
1675
1676 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1677
1678 // Set up frame.
1679 __ push(ebp);
1680 __ mov(ebp, esp);
1681
1682 // Push marker in two places.
1683 int marker = type();
Ben Murdochda12d292016-06-02 14:46:10 +01001684 __ push(Immediate(Smi::FromInt(marker))); // marker
1685 ExternalReference context_address(Isolate::kContextAddress, isolate());
1686 __ push(Operand::StaticVariable(context_address)); // context
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001687 // Save callee-saved registers (C calling conventions).
1688 __ push(edi);
1689 __ push(esi);
1690 __ push(ebx);
1691
1692 // Save copies of the top frame descriptor on the stack.
1693 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate());
1694 __ push(Operand::StaticVariable(c_entry_fp));
1695
1696 // If this is the outermost JS call, set js_entry_sp value.
1697 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
1698 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
1699 __ j(not_equal, &not_outermost_js, Label::kNear);
1700 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
1701 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1702 __ jmp(&invoke, Label::kNear);
1703 __ bind(&not_outermost_js);
1704 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
1705
1706 // Jump to a faked try block that does the invoke, with a faked catch
1707 // block that sets the pending exception.
1708 __ jmp(&invoke);
1709 __ bind(&handler_entry);
1710 handler_offset_ = handler_entry.pos();
1711 // Caught exception: Store result (exception) in the pending exception
1712 // field in the JSEnv and return a failure sentinel.
1713 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
1714 isolate());
1715 __ mov(Operand::StaticVariable(pending_exception), eax);
1716 __ mov(eax, Immediate(isolate()->factory()->exception()));
1717 __ jmp(&exit);
1718
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001719 // Invoke: Link this frame into the handler chain.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001720 __ bind(&invoke);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001721 __ PushStackHandler();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001722
1723 // Clear any pending exceptions.
1724 __ mov(edx, Immediate(isolate()->factory()->the_hole_value()));
1725 __ mov(Operand::StaticVariable(pending_exception), edx);
1726
1727 // Fake a receiver (NULL).
1728 __ push(Immediate(0)); // receiver
1729
1730 // Invoke the function by calling through JS entry trampoline builtin and
1731 // pop the faked function when we return. Notice that we cannot store a
1732 // reference to the trampoline code directly in this stub, because the
1733 // builtin stubs may not have been generated yet.
1734 if (type() == StackFrame::ENTRY_CONSTRUCT) {
1735 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
1736 isolate());
1737 __ mov(edx, Immediate(construct_entry));
1738 } else {
1739 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
1740 __ mov(edx, Immediate(entry));
1741 }
1742 __ mov(edx, Operand(edx, 0)); // deref address
1743 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
1744 __ call(edx);
1745
1746 // Unlink this frame from the handler chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001747 __ PopStackHandler();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001748
1749 __ bind(&exit);
1750 // Check if the current stack frame is marked as the outermost JS frame.
1751 __ pop(ebx);
1752 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1753 __ j(not_equal, &not_outermost_js_2);
1754 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
1755 __ bind(&not_outermost_js_2);
1756
1757 // Restore the top frame descriptor from the stack.
1758 __ pop(Operand::StaticVariable(ExternalReference(
1759 Isolate::kCEntryFPAddress, isolate())));
1760
1761 // Restore callee-saved registers (C calling conventions).
1762 __ pop(ebx);
1763 __ pop(esi);
1764 __ pop(edi);
1765 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
1766
1767 // Restore frame pointer and return.
1768 __ pop(ebp);
1769 __ ret(0);
1770}
1771
1772
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001773// -------------------------------------------------------------------------
1774// StringCharCodeAtGenerator
1775
1776void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
1777 // If the receiver is a smi trigger the non-string case.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001778 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
1779 __ JumpIfSmi(object_, receiver_not_string_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001780
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001781 // Fetch the instance type of the receiver into result register.
1782 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
1783 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
1784 // If the receiver is not a string trigger the non-string case.
1785 __ test(result_, Immediate(kIsNotStringMask));
1786 __ j(not_zero, receiver_not_string_);
1787 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001788
1789 // If the index is non-smi trigger the non-smi case.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001790 __ JumpIfNotSmi(index_, &index_not_smi_);
1791 __ bind(&got_smi_index_);
1792
1793 // Check for index out of range.
1794 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
1795 __ j(above_equal, index_out_of_range_);
1796
1797 __ SmiUntag(index_);
1798
1799 Factory* factory = masm->isolate()->factory();
1800 StringCharLoadGenerator::Generate(
1801 masm, factory, object_, index_, result_, &call_runtime_);
1802
1803 __ SmiTag(result_);
1804 __ bind(&exit_);
1805}
1806
1807
1808void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001809 MacroAssembler* masm, EmbedMode embed_mode,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001810 const RuntimeCallHelper& call_helper) {
1811 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
1812
1813 // Index is not a smi.
1814 __ bind(&index_not_smi_);
1815 // If index is a heap number, try converting it to an integer.
1816 __ CheckMap(index_,
1817 masm->isolate()->factory()->heap_number_map(),
1818 index_not_number_,
1819 DONT_DO_SMI_CHECK);
1820 call_helper.BeforeCall(masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001821 if (embed_mode == PART_OF_IC_HANDLER) {
1822 __ push(LoadWithVectorDescriptor::VectorRegister());
1823 __ push(LoadDescriptor::SlotRegister());
1824 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001825 __ push(object_);
1826 __ push(index_); // Consumed by runtime conversion function.
1827 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001828 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001829 } else {
1830 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
1831 // NumberToSmi discards numbers that are not exact integers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001832 __ CallRuntime(Runtime::kNumberToSmi);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001833 }
1834 if (!index_.is(eax)) {
1835 // Save the conversion result before the pop instructions below
1836 // have a chance to overwrite it.
1837 __ mov(index_, eax);
1838 }
1839 __ pop(object_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001840 if (embed_mode == PART_OF_IC_HANDLER) {
1841 __ pop(LoadDescriptor::SlotRegister());
1842 __ pop(LoadWithVectorDescriptor::VectorRegister());
1843 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001844 // Reload the instance type.
1845 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
1846 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
1847 call_helper.AfterCall(masm);
1848 // If index is still not a smi, it must be out of range.
1849 STATIC_ASSERT(kSmiTag == 0);
1850 __ JumpIfNotSmi(index_, index_out_of_range_);
1851 // Otherwise, return to the fast path.
1852 __ jmp(&got_smi_index_);
1853
1854 // Call runtime. We get here when the receiver is a string and the
1855 // index is a number, but the code of getting the actual character
1856 // is too complex (e.g., when the string needs to be flattened).
1857 __ bind(&call_runtime_);
1858 call_helper.BeforeCall(masm);
1859 __ push(object_);
1860 __ SmiTag(index_);
1861 __ push(index_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001862 __ CallRuntime(Runtime::kStringCharCodeAtRT);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001863 if (!result_.is(eax)) {
1864 __ mov(result_, eax);
1865 }
1866 call_helper.AfterCall(masm);
1867 __ jmp(&exit_);
1868
1869 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
1870}
1871
1872
1873// -------------------------------------------------------------------------
1874// StringCharFromCodeGenerator
1875
1876void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
1877 // Fast case of Heap::LookupSingleCharacterStringFromCode.
1878 STATIC_ASSERT(kSmiTag == 0);
1879 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001880 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
1881 __ test(code_, Immediate(kSmiTagMask |
1882 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001883 __ j(not_zero, &slow_case_);
1884
1885 Factory* factory = masm->isolate()->factory();
1886 __ Move(result_, Immediate(factory->single_character_string_cache()));
1887 STATIC_ASSERT(kSmiTag == 0);
1888 STATIC_ASSERT(kSmiTagSize == 1);
1889 STATIC_ASSERT(kSmiShiftSize == 0);
1890 // At this point code register contains smi tagged one byte char code.
1891 __ mov(result_, FieldOperand(result_,
1892 code_, times_half_pointer_size,
1893 FixedArray::kHeaderSize));
1894 __ cmp(result_, factory->undefined_value());
1895 __ j(equal, &slow_case_);
1896 __ bind(&exit_);
1897}
1898
1899
1900void StringCharFromCodeGenerator::GenerateSlow(
1901 MacroAssembler* masm,
1902 const RuntimeCallHelper& call_helper) {
1903 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
1904
1905 __ bind(&slow_case_);
1906 call_helper.BeforeCall(masm);
1907 __ push(code_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001908 __ CallRuntime(Runtime::kStringCharFromCode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001909 if (!result_.is(eax)) {
1910 __ mov(result_, eax);
1911 }
1912 call_helper.AfterCall(masm);
1913 __ jmp(&exit_);
1914
1915 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
1916}
1917
1918
1919void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
1920 Register dest,
1921 Register src,
1922 Register count,
1923 Register scratch,
1924 String::Encoding encoding) {
1925 DCHECK(!scratch.is(dest));
1926 DCHECK(!scratch.is(src));
1927 DCHECK(!scratch.is(count));
1928
1929 // Nothing to do for zero characters.
1930 Label done;
1931 __ test(count, count);
1932 __ j(zero, &done);
1933
1934 // Make count the number of bytes to copy.
1935 if (encoding == String::TWO_BYTE_ENCODING) {
1936 __ shl(count, 1);
1937 }
1938
1939 Label loop;
1940 __ bind(&loop);
1941 __ mov_b(scratch, Operand(src, 0));
1942 __ mov_b(Operand(dest, 0), scratch);
1943 __ inc(src);
1944 __ inc(dest);
1945 __ dec(count);
1946 __ j(not_zero, &loop);
1947
1948 __ bind(&done);
1949}
1950
1951
1952void SubStringStub::Generate(MacroAssembler* masm) {
1953 Label runtime;
1954
1955 // Stack frame on entry.
1956 // esp[0]: return address
1957 // esp[4]: to
1958 // esp[8]: from
1959 // esp[12]: string
1960
1961 // Make sure first argument is a string.
1962 __ mov(eax, Operand(esp, 3 * kPointerSize));
1963 STATIC_ASSERT(kSmiTag == 0);
1964 __ JumpIfSmi(eax, &runtime);
1965 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
1966 __ j(NegateCondition(is_string), &runtime);
1967
1968 // eax: string
1969 // ebx: instance type
1970
1971 // Calculate length of sub string using the smi values.
1972 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
1973 __ JumpIfNotSmi(ecx, &runtime);
1974 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
1975 __ JumpIfNotSmi(edx, &runtime);
1976 __ sub(ecx, edx);
1977 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
1978 Label not_original_string;
1979 // Shorter than original string's length: an actual substring.
1980 __ j(below, &not_original_string, Label::kNear);
1981 // Longer than original string's length or negative: unsafe arguments.
1982 __ j(above, &runtime);
1983 // Return original string.
1984 Counters* counters = isolate()->counters();
1985 __ IncrementCounter(counters->sub_string_native(), 1);
1986 __ ret(3 * kPointerSize);
1987 __ bind(&not_original_string);
1988
1989 Label single_char;
1990 __ cmp(ecx, Immediate(Smi::FromInt(1)));
1991 __ j(equal, &single_char);
1992
1993 // eax: string
1994 // ebx: instance type
1995 // ecx: sub string length (smi)
1996 // edx: from index (smi)
1997 // Deal with different string types: update the index if necessary
1998 // and put the underlying string into edi.
1999 Label underlying_unpacked, sliced_string, seq_or_external_string;
2000 // If the string is not indirect, it can only be sequential or external.
2001 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2002 STATIC_ASSERT(kIsIndirectStringMask != 0);
2003 __ test(ebx, Immediate(kIsIndirectStringMask));
2004 __ j(zero, &seq_or_external_string, Label::kNear);
2005
2006 Factory* factory = isolate()->factory();
2007 __ test(ebx, Immediate(kSlicedNotConsMask));
2008 __ j(not_zero, &sliced_string, Label::kNear);
2009 // Cons string. Check whether it is flat, then fetch first part.
2010 // Flat cons strings have an empty second part.
2011 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
2012 factory->empty_string());
2013 __ j(not_equal, &runtime);
2014 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
2015 // Update instance type.
2016 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2017 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2018 __ jmp(&underlying_unpacked, Label::kNear);
2019
2020 __ bind(&sliced_string);
2021 // Sliced string. Fetch parent and adjust start index by offset.
2022 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
2023 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
2024 // Update instance type.
2025 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
2026 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
2027 __ jmp(&underlying_unpacked, Label::kNear);
2028
2029 __ bind(&seq_or_external_string);
2030 // Sequential or external string. Just move string to the expected register.
2031 __ mov(edi, eax);
2032
2033 __ bind(&underlying_unpacked);
2034
2035 if (FLAG_string_slices) {
2036 Label copy_routine;
2037 // edi: underlying subject string
2038 // ebx: instance type of underlying subject string
2039 // edx: adjusted start index (smi)
2040 // ecx: length (smi)
2041 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
2042 // Short slice. Copy instead of slicing.
2043 __ j(less, &copy_routine);
2044 // Allocate new sliced string. At this point we do not reload the instance
2045 // type including the string encoding because we simply rely on the info
2046 // provided by the original string. It does not matter if the original
2047 // string's encoding is wrong because we always have to recheck encoding of
2048 // the newly created string's parent anyways due to externalized strings.
2049 Label two_byte_slice, set_slice_header;
2050 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
2051 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2052 __ test(ebx, Immediate(kStringEncodingMask));
2053 __ j(zero, &two_byte_slice, Label::kNear);
2054 __ AllocateOneByteSlicedString(eax, ebx, no_reg, &runtime);
2055 __ jmp(&set_slice_header, Label::kNear);
2056 __ bind(&two_byte_slice);
2057 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
2058 __ bind(&set_slice_header);
2059 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
2060 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
2061 Immediate(String::kEmptyHashField));
2062 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
2063 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
2064 __ IncrementCounter(counters->sub_string_native(), 1);
2065 __ ret(3 * kPointerSize);
2066
2067 __ bind(&copy_routine);
2068 }
2069
2070 // edi: underlying subject string
2071 // ebx: instance type of underlying subject string
2072 // edx: adjusted start index (smi)
2073 // ecx: length (smi)
2074 // The subject string can only be external or sequential string of either
2075 // encoding at this point.
2076 Label two_byte_sequential, runtime_drop_two, sequential_string;
2077 STATIC_ASSERT(kExternalStringTag != 0);
2078 STATIC_ASSERT(kSeqStringTag == 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002079 __ test_b(ebx, Immediate(kExternalStringTag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002080 __ j(zero, &sequential_string);
2081
2082 // Handle external string.
2083 // Rule out short external strings.
2084 STATIC_ASSERT(kShortExternalStringTag != 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002085 __ test_b(ebx, Immediate(kShortExternalStringMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002086 __ j(not_zero, &runtime);
2087 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
2088 // Move the pointer so that offset-wise, it looks like a sequential string.
2089 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2090 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2091
2092 __ bind(&sequential_string);
2093 // Stash away (adjusted) index and (underlying) string.
2094 __ push(edx);
2095 __ push(edi);
2096 __ SmiUntag(ecx);
2097 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
Ben Murdochda12d292016-06-02 14:46:10 +01002098 __ test_b(ebx, Immediate(kStringEncodingMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002099 __ j(zero, &two_byte_sequential);
2100
2101 // Sequential one byte string. Allocate the result.
2102 __ AllocateOneByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
2103
2104 // eax: result string
2105 // ecx: result string length
2106 // Locate first character of result.
2107 __ mov(edi, eax);
2108 __ add(edi, Immediate(SeqOneByteString::kHeaderSize - kHeapObjectTag));
2109 // Load string argument and locate character of sub string start.
2110 __ pop(edx);
2111 __ pop(ebx);
2112 __ SmiUntag(ebx);
2113 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqOneByteString::kHeaderSize));
2114
2115 // eax: result string
2116 // ecx: result length
2117 // edi: first character of result
2118 // edx: character of sub string start
2119 StringHelper::GenerateCopyCharacters(
2120 masm, edi, edx, ecx, ebx, String::ONE_BYTE_ENCODING);
2121 __ IncrementCounter(counters->sub_string_native(), 1);
2122 __ ret(3 * kPointerSize);
2123
2124 __ bind(&two_byte_sequential);
2125 // Sequential two-byte string. Allocate the result.
2126 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
2127
2128 // eax: result string
2129 // ecx: result string length
2130 // Locate first character of result.
2131 __ mov(edi, eax);
2132 __ add(edi,
2133 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2134 // Load string argument and locate character of sub string start.
2135 __ pop(edx);
2136 __ pop(ebx);
2137 // As from is a smi it is 2 times the value which matches the size of a two
2138 // byte character.
2139 STATIC_ASSERT(kSmiTag == 0);
2140 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2141 __ lea(edx, FieldOperand(edx, ebx, times_1, SeqTwoByteString::kHeaderSize));
2142
2143 // eax: result string
2144 // ecx: result length
2145 // edi: first character of result
2146 // edx: character of sub string start
2147 StringHelper::GenerateCopyCharacters(
2148 masm, edi, edx, ecx, ebx, String::TWO_BYTE_ENCODING);
2149 __ IncrementCounter(counters->sub_string_native(), 1);
2150 __ ret(3 * kPointerSize);
2151
2152 // Drop pushed values on the stack before tail call.
2153 __ bind(&runtime_drop_two);
2154 __ Drop(2);
2155
2156 // Just jump to runtime to create the sub string.
2157 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002158 __ TailCallRuntime(Runtime::kSubString);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002159
2160 __ bind(&single_char);
2161 // eax: string
2162 // ebx: instance type
2163 // ecx: sub string length (smi)
2164 // edx: from index (smi)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002165 StringCharAtGenerator generator(eax, edx, ecx, eax, &runtime, &runtime,
2166 &runtime, STRING_INDEX_IS_NUMBER,
2167 RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002168 generator.GenerateFast(masm);
2169 __ ret(3 * kPointerSize);
2170 generator.SkipSlow(masm, &runtime);
2171}
2172
2173
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002174void ToNumberStub::Generate(MacroAssembler* masm) {
2175 // The ToNumber stub takes one argument in eax.
2176 Label not_smi;
2177 __ JumpIfNotSmi(eax, &not_smi, Label::kNear);
2178 __ Ret();
2179 __ bind(&not_smi);
2180
2181 Label not_heap_number;
2182 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2183 __ j(not_equal, &not_heap_number, Label::kNear);
2184 __ Ret();
2185 __ bind(&not_heap_number);
2186
Ben Murdochda12d292016-06-02 14:46:10 +01002187 NonNumberToNumberStub stub(masm->isolate());
2188 __ TailCallStub(&stub);
2189}
2190
2191void NonNumberToNumberStub::Generate(MacroAssembler* masm) {
2192 // The NonNumberToNumber stub takes one argument in eax.
2193 __ AssertNotNumber(eax);
2194
2195 Label not_string;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002196 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
2197 // eax: object
2198 // edi: object map
2199 __ j(above_equal, &not_string, Label::kNear);
Ben Murdochda12d292016-06-02 14:46:10 +01002200 StringToNumberStub stub(masm->isolate());
2201 __ TailCallStub(&stub);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002202 __ bind(&not_string);
2203
2204 Label not_oddball;
2205 __ CmpInstanceType(edi, ODDBALL_TYPE);
2206 __ j(not_equal, &not_oddball, Label::kNear);
2207 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
2208 __ Ret();
2209 __ bind(&not_oddball);
2210
2211 __ pop(ecx); // Pop return address.
2212 __ push(eax); // Push argument.
2213 __ push(ecx); // Push return address.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002214 __ TailCallRuntime(Runtime::kToNumber);
2215}
2216
Ben Murdochda12d292016-06-02 14:46:10 +01002217void StringToNumberStub::Generate(MacroAssembler* masm) {
2218 // The StringToNumber stub takes one argument in eax.
2219 __ AssertString(eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002220
Ben Murdochda12d292016-06-02 14:46:10 +01002221 // Check if string has a cached array index.
2222 Label runtime;
2223 __ test(FieldOperand(eax, String::kHashFieldOffset),
2224 Immediate(String::kContainsCachedArrayIndexMask));
2225 __ j(not_zero, &runtime, Label::kNear);
2226 __ mov(eax, FieldOperand(eax, String::kHashFieldOffset));
2227 __ IndexFromHash(eax, eax);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002228 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002229
Ben Murdochda12d292016-06-02 14:46:10 +01002230 __ bind(&runtime);
2231 __ PopReturnAddressTo(ecx); // Pop return address.
2232 __ Push(eax); // Push argument.
2233 __ PushReturnAddressFrom(ecx); // Push return address.
2234 __ TailCallRuntime(Runtime::kStringToNumber);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002235}
2236
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002237void ToStringStub::Generate(MacroAssembler* masm) {
2238 // The ToString stub takes one argument in eax.
2239 Label is_number;
2240 __ JumpIfSmi(eax, &is_number, Label::kNear);
2241
2242 Label not_string;
2243 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, edi);
2244 // eax: receiver
2245 // edi: receiver map
2246 __ j(above_equal, &not_string, Label::kNear);
2247 __ Ret();
2248 __ bind(&not_string);
2249
2250 Label not_heap_number;
2251 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2252 __ j(not_equal, &not_heap_number, Label::kNear);
2253 __ bind(&is_number);
2254 NumberToStringStub stub(isolate());
2255 __ TailCallStub(&stub);
2256 __ bind(&not_heap_number);
2257
2258 Label not_oddball;
2259 __ CmpInstanceType(edi, ODDBALL_TYPE);
2260 __ j(not_equal, &not_oddball, Label::kNear);
2261 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
2262 __ Ret();
2263 __ bind(&not_oddball);
2264
2265 __ pop(ecx); // Pop return address.
2266 __ push(eax); // Push argument.
2267 __ push(ecx); // Push return address.
2268 __ TailCallRuntime(Runtime::kToString);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002269}
2270
2271
Ben Murdoch097c5b22016-05-18 11:27:45 +01002272void ToNameStub::Generate(MacroAssembler* masm) {
2273 // The ToName stub takes one argument in eax.
2274 Label is_number;
2275 __ JumpIfSmi(eax, &is_number, Label::kNear);
2276
2277 Label not_name;
2278 STATIC_ASSERT(FIRST_NAME_TYPE == FIRST_TYPE);
2279 __ CmpObjectType(eax, LAST_NAME_TYPE, edi);
2280 // eax: receiver
2281 // edi: receiver map
2282 __ j(above, &not_name, Label::kNear);
2283 __ Ret();
2284 __ bind(&not_name);
2285
2286 Label not_heap_number;
2287 __ CompareMap(eax, masm->isolate()->factory()->heap_number_map());
2288 __ j(not_equal, &not_heap_number, Label::kNear);
2289 __ bind(&is_number);
2290 NumberToStringStub stub(isolate());
2291 __ TailCallStub(&stub);
2292 __ bind(&not_heap_number);
2293
2294 Label not_oddball;
2295 __ CmpInstanceType(edi, ODDBALL_TYPE);
2296 __ j(not_equal, &not_oddball, Label::kNear);
2297 __ mov(eax, FieldOperand(eax, Oddball::kToStringOffset));
2298 __ Ret();
2299 __ bind(&not_oddball);
2300
2301 __ pop(ecx); // Pop return address.
2302 __ push(eax); // Push argument.
2303 __ push(ecx); // Push return address.
2304 __ TailCallRuntime(Runtime::kToName);
2305}
2306
2307
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002308void StringHelper::GenerateFlatOneByteStringEquals(MacroAssembler* masm,
2309 Register left,
2310 Register right,
2311 Register scratch1,
2312 Register scratch2) {
2313 Register length = scratch1;
2314
2315 // Compare lengths.
2316 Label strings_not_equal, check_zero_length;
2317 __ mov(length, FieldOperand(left, String::kLengthOffset));
2318 __ cmp(length, FieldOperand(right, String::kLengthOffset));
2319 __ j(equal, &check_zero_length, Label::kNear);
2320 __ bind(&strings_not_equal);
2321 __ Move(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
2322 __ ret(0);
2323
2324 // Check if the length is zero.
2325 Label compare_chars;
2326 __ bind(&check_zero_length);
2327 STATIC_ASSERT(kSmiTag == 0);
2328 __ test(length, length);
2329 __ j(not_zero, &compare_chars, Label::kNear);
2330 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2331 __ ret(0);
2332
2333 // Compare characters.
2334 __ bind(&compare_chars);
2335 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2,
2336 &strings_not_equal, Label::kNear);
2337
2338 // Characters are equal.
2339 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2340 __ ret(0);
2341}
2342
2343
2344void StringHelper::GenerateCompareFlatOneByteStrings(
2345 MacroAssembler* masm, Register left, Register right, Register scratch1,
2346 Register scratch2, Register scratch3) {
2347 Counters* counters = masm->isolate()->counters();
2348 __ IncrementCounter(counters->string_compare_native(), 1);
2349
2350 // Find minimum length.
2351 Label left_shorter;
2352 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
2353 __ mov(scratch3, scratch1);
2354 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
2355
2356 Register length_delta = scratch3;
2357
2358 __ j(less_equal, &left_shorter, Label::kNear);
2359 // Right string is shorter. Change scratch1 to be length of right string.
2360 __ sub(scratch1, length_delta);
2361 __ bind(&left_shorter);
2362
2363 Register min_length = scratch1;
2364
2365 // If either length is zero, just compare lengths.
2366 Label compare_lengths;
2367 __ test(min_length, min_length);
2368 __ j(zero, &compare_lengths, Label::kNear);
2369
2370 // Compare characters.
2371 Label result_not_equal;
2372 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
2373 &result_not_equal, Label::kNear);
2374
2375 // Compare lengths - strings up to min-length are equal.
2376 __ bind(&compare_lengths);
2377 __ test(length_delta, length_delta);
2378 Label length_not_equal;
2379 __ j(not_zero, &length_not_equal, Label::kNear);
2380
2381 // Result is EQUAL.
2382 STATIC_ASSERT(EQUAL == 0);
2383 STATIC_ASSERT(kSmiTag == 0);
2384 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2385 __ ret(0);
2386
2387 Label result_greater;
2388 Label result_less;
2389 __ bind(&length_not_equal);
2390 __ j(greater, &result_greater, Label::kNear);
2391 __ jmp(&result_less, Label::kNear);
2392 __ bind(&result_not_equal);
2393 __ j(above, &result_greater, Label::kNear);
2394 __ bind(&result_less);
2395
2396 // Result is LESS.
2397 __ Move(eax, Immediate(Smi::FromInt(LESS)));
2398 __ ret(0);
2399
2400 // Result is GREATER.
2401 __ bind(&result_greater);
2402 __ Move(eax, Immediate(Smi::FromInt(GREATER)));
2403 __ ret(0);
2404}
2405
2406
2407void StringHelper::GenerateOneByteCharsCompareLoop(
2408 MacroAssembler* masm, Register left, Register right, Register length,
2409 Register scratch, Label* chars_not_equal,
2410 Label::Distance chars_not_equal_near) {
2411 // Change index to run from -length to -1 by adding length to string
2412 // start. This means that loop ends when index reaches zero, which
2413 // doesn't need an additional compare.
2414 __ SmiUntag(length);
2415 __ lea(left,
2416 FieldOperand(left, length, times_1, SeqOneByteString::kHeaderSize));
2417 __ lea(right,
2418 FieldOperand(right, length, times_1, SeqOneByteString::kHeaderSize));
2419 __ neg(length);
2420 Register index = length; // index = -length;
2421
2422 // Compare loop.
2423 Label loop;
2424 __ bind(&loop);
2425 __ mov_b(scratch, Operand(left, index, times_1, 0));
2426 __ cmpb(scratch, Operand(right, index, times_1, 0));
2427 __ j(not_equal, chars_not_equal, chars_not_equal_near);
2428 __ inc(index);
2429 __ j(not_zero, &loop);
2430}
2431
2432
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002433void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
2434 // ----------- S t a t e -------------
2435 // -- edx : left
2436 // -- eax : right
2437 // -- esp[0] : return address
2438 // -----------------------------------
2439
2440 // Load ecx with the allocation site. We stick an undefined dummy value here
2441 // and replace it with the real allocation site later when we instantiate this
2442 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
2443 __ mov(ecx, handle(isolate()->heap()->undefined_value()));
2444
2445 // Make sure that we actually patched the allocation site.
2446 if (FLAG_debug_code) {
2447 __ test(ecx, Immediate(kSmiTagMask));
2448 __ Assert(not_equal, kExpectedAllocationSite);
2449 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset),
2450 isolate()->factory()->allocation_site_map());
2451 __ Assert(equal, kExpectedAllocationSite);
2452 }
2453
2454 // Tail call into the stub that handles binary operations with allocation
2455 // sites.
2456 BinaryOpWithAllocationSiteStub stub(isolate(), state());
2457 __ TailCallStub(&stub);
2458}
2459
2460
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002461void CompareICStub::GenerateBooleans(MacroAssembler* masm) {
2462 DCHECK_EQ(CompareICState::BOOLEAN, state());
2463 Label miss;
2464 Label::Distance const miss_distance =
2465 masm->emit_debug_code() ? Label::kFar : Label::kNear;
2466
2467 __ JumpIfSmi(edx, &miss, miss_distance);
2468 __ mov(ecx, FieldOperand(edx, HeapObject::kMapOffset));
2469 __ JumpIfSmi(eax, &miss, miss_distance);
2470 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
2471 __ JumpIfNotRoot(ecx, Heap::kBooleanMapRootIndex, &miss, miss_distance);
2472 __ JumpIfNotRoot(ebx, Heap::kBooleanMapRootIndex, &miss, miss_distance);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002473 if (!Token::IsEqualityOp(op())) {
2474 __ mov(eax, FieldOperand(eax, Oddball::kToNumberOffset));
2475 __ AssertSmi(eax);
2476 __ mov(edx, FieldOperand(edx, Oddball::kToNumberOffset));
2477 __ AssertSmi(edx);
2478 __ xchg(eax, edx);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002479 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002480 __ sub(eax, edx);
2481 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002482
2483 __ bind(&miss);
2484 GenerateMiss(masm);
2485}
2486
2487
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002488void CompareICStub::GenerateSmis(MacroAssembler* masm) {
2489 DCHECK(state() == CompareICState::SMI);
2490 Label miss;
2491 __ mov(ecx, edx);
2492 __ or_(ecx, eax);
2493 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
2494
2495 if (GetCondition() == equal) {
2496 // For equality we do not care about the sign of the result.
2497 __ sub(eax, edx);
2498 } else {
2499 Label done;
2500 __ sub(edx, eax);
2501 __ j(no_overflow, &done, Label::kNear);
2502 // Correct sign of result in case of overflow.
2503 __ not_(edx);
2504 __ bind(&done);
2505 __ mov(eax, edx);
2506 }
2507 __ ret(0);
2508
2509 __ bind(&miss);
2510 GenerateMiss(masm);
2511}
2512
2513
2514void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
2515 DCHECK(state() == CompareICState::NUMBER);
2516
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002517 Label generic_stub, check_left;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002518 Label unordered, maybe_undefined1, maybe_undefined2;
2519 Label miss;
2520
2521 if (left() == CompareICState::SMI) {
2522 __ JumpIfNotSmi(edx, &miss);
2523 }
2524 if (right() == CompareICState::SMI) {
2525 __ JumpIfNotSmi(eax, &miss);
2526 }
2527
2528 // Inlining the double comparison and falling back to the general compare
2529 // stub if NaN is involved or SSE2 or CMOV is unsupported.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002530 __ JumpIfSmi(eax, &check_left, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002531 __ cmp(FieldOperand(eax, HeapObject::kMapOffset),
2532 isolate()->factory()->heap_number_map());
2533 __ j(not_equal, &maybe_undefined1, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002534
2535 __ bind(&check_left);
2536 __ JumpIfSmi(edx, &generic_stub, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002537 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
2538 isolate()->factory()->heap_number_map());
2539 __ j(not_equal, &maybe_undefined2, Label::kNear);
2540
2541 __ bind(&unordered);
2542 __ bind(&generic_stub);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002543 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002544 CompareICState::GENERIC, CompareICState::GENERIC);
2545 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
2546
2547 __ bind(&maybe_undefined1);
2548 if (Token::IsOrderedRelationalCompareOp(op())) {
2549 __ cmp(eax, Immediate(isolate()->factory()->undefined_value()));
2550 __ j(not_equal, &miss);
2551 __ JumpIfSmi(edx, &unordered);
2552 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
2553 __ j(not_equal, &maybe_undefined2, Label::kNear);
2554 __ jmp(&unordered);
2555 }
2556
2557 __ bind(&maybe_undefined2);
2558 if (Token::IsOrderedRelationalCompareOp(op())) {
2559 __ cmp(edx, Immediate(isolate()->factory()->undefined_value()));
2560 __ j(equal, &unordered);
2561 }
2562
2563 __ bind(&miss);
2564 GenerateMiss(masm);
2565}
2566
2567
2568void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
2569 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
2570 DCHECK(GetCondition() == equal);
2571
2572 // Registers containing left and right operands respectively.
2573 Register left = edx;
2574 Register right = eax;
2575 Register tmp1 = ecx;
2576 Register tmp2 = ebx;
2577
2578 // Check that both operands are heap objects.
2579 Label miss;
2580 __ mov(tmp1, left);
2581 STATIC_ASSERT(kSmiTag == 0);
2582 __ and_(tmp1, right);
2583 __ JumpIfSmi(tmp1, &miss, Label::kNear);
2584
2585 // Check that both operands are internalized strings.
2586 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
2587 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
2588 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
2589 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
2590 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2591 __ or_(tmp1, tmp2);
2592 __ test(tmp1, Immediate(kIsNotStringMask | kIsNotInternalizedMask));
2593 __ j(not_zero, &miss, Label::kNear);
2594
2595 // Internalized strings are compared by identity.
2596 Label done;
2597 __ cmp(left, right);
2598 // Make sure eax is non-zero. At this point input operands are
2599 // guaranteed to be non-zero.
2600 DCHECK(right.is(eax));
2601 __ j(not_equal, &done, Label::kNear);
2602 STATIC_ASSERT(EQUAL == 0);
2603 STATIC_ASSERT(kSmiTag == 0);
2604 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2605 __ bind(&done);
2606 __ ret(0);
2607
2608 __ bind(&miss);
2609 GenerateMiss(masm);
2610}
2611
2612
2613void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
2614 DCHECK(state() == CompareICState::UNIQUE_NAME);
2615 DCHECK(GetCondition() == equal);
2616
2617 // Registers containing left and right operands respectively.
2618 Register left = edx;
2619 Register right = eax;
2620 Register tmp1 = ecx;
2621 Register tmp2 = ebx;
2622
2623 // Check that both operands are heap objects.
2624 Label miss;
2625 __ mov(tmp1, left);
2626 STATIC_ASSERT(kSmiTag == 0);
2627 __ and_(tmp1, right);
2628 __ JumpIfSmi(tmp1, &miss, Label::kNear);
2629
2630 // Check that both operands are unique names. This leaves the instance
2631 // types loaded in tmp1 and tmp2.
2632 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
2633 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
2634 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
2635 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
2636
2637 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss, Label::kNear);
2638 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss, Label::kNear);
2639
2640 // Unique names are compared by identity.
2641 Label done;
2642 __ cmp(left, right);
2643 // Make sure eax is non-zero. At this point input operands are
2644 // guaranteed to be non-zero.
2645 DCHECK(right.is(eax));
2646 __ j(not_equal, &done, Label::kNear);
2647 STATIC_ASSERT(EQUAL == 0);
2648 STATIC_ASSERT(kSmiTag == 0);
2649 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2650 __ bind(&done);
2651 __ ret(0);
2652
2653 __ bind(&miss);
2654 GenerateMiss(masm);
2655}
2656
2657
2658void CompareICStub::GenerateStrings(MacroAssembler* masm) {
2659 DCHECK(state() == CompareICState::STRING);
2660 Label miss;
2661
2662 bool equality = Token::IsEqualityOp(op());
2663
2664 // Registers containing left and right operands respectively.
2665 Register left = edx;
2666 Register right = eax;
2667 Register tmp1 = ecx;
2668 Register tmp2 = ebx;
2669 Register tmp3 = edi;
2670
2671 // Check that both operands are heap objects.
2672 __ mov(tmp1, left);
2673 STATIC_ASSERT(kSmiTag == 0);
2674 __ and_(tmp1, right);
2675 __ JumpIfSmi(tmp1, &miss);
2676
2677 // Check that both operands are strings. This leaves the instance
2678 // types loaded in tmp1 and tmp2.
2679 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
2680 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
2681 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
2682 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
2683 __ mov(tmp3, tmp1);
2684 STATIC_ASSERT(kNotStringTag != 0);
2685 __ or_(tmp3, tmp2);
2686 __ test(tmp3, Immediate(kIsNotStringMask));
2687 __ j(not_zero, &miss);
2688
2689 // Fast check for identical strings.
2690 Label not_same;
2691 __ cmp(left, right);
2692 __ j(not_equal, &not_same, Label::kNear);
2693 STATIC_ASSERT(EQUAL == 0);
2694 STATIC_ASSERT(kSmiTag == 0);
2695 __ Move(eax, Immediate(Smi::FromInt(EQUAL)));
2696 __ ret(0);
2697
2698 // Handle not identical strings.
2699 __ bind(&not_same);
2700
2701 // Check that both strings are internalized. If they are, we're done
2702 // because we already know they are not identical. But in the case of
2703 // non-equality compare, we still need to determine the order. We
2704 // also know they are both strings.
2705 if (equality) {
2706 Label do_compare;
2707 STATIC_ASSERT(kInternalizedTag == 0);
2708 __ or_(tmp1, tmp2);
2709 __ test(tmp1, Immediate(kIsNotInternalizedMask));
2710 __ j(not_zero, &do_compare, Label::kNear);
2711 // Make sure eax is non-zero. At this point input operands are
2712 // guaranteed to be non-zero.
2713 DCHECK(right.is(eax));
2714 __ ret(0);
2715 __ bind(&do_compare);
2716 }
2717
2718 // Check that both strings are sequential one-byte.
2719 Label runtime;
2720 __ JumpIfNotBothSequentialOneByteStrings(left, right, tmp1, tmp2, &runtime);
2721
2722 // Compare flat one byte strings. Returns when done.
2723 if (equality) {
2724 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1,
2725 tmp2);
2726 } else {
2727 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
2728 tmp2, tmp3);
2729 }
2730
2731 // Handle more complex cases in runtime.
2732 __ bind(&runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002733 if (equality) {
Ben Murdochda12d292016-06-02 14:46:10 +01002734 {
2735 FrameScope scope(masm, StackFrame::INTERNAL);
2736 __ Push(left);
2737 __ Push(right);
2738 __ CallRuntime(Runtime::kStringEqual);
2739 }
2740 __ sub(eax, Immediate(masm->isolate()->factory()->true_value()));
2741 __ Ret();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002742 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01002743 __ pop(tmp1); // Return address.
2744 __ push(left);
2745 __ push(right);
2746 __ push(tmp1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002747 __ TailCallRuntime(Runtime::kStringCompare);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002748 }
2749
2750 __ bind(&miss);
2751 GenerateMiss(masm);
2752}
2753
2754
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002755void CompareICStub::GenerateReceivers(MacroAssembler* masm) {
2756 DCHECK_EQ(CompareICState::RECEIVER, state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002757 Label miss;
2758 __ mov(ecx, edx);
2759 __ and_(ecx, eax);
2760 __ JumpIfSmi(ecx, &miss, Label::kNear);
2761
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002762 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
2763 __ CmpObjectType(eax, FIRST_JS_RECEIVER_TYPE, ecx);
2764 __ j(below, &miss, Label::kNear);
2765 __ CmpObjectType(edx, FIRST_JS_RECEIVER_TYPE, ecx);
2766 __ j(below, &miss, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002767
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002768 DCHECK_EQ(equal, GetCondition());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002769 __ sub(eax, edx);
2770 __ ret(0);
2771
2772 __ bind(&miss);
2773 GenerateMiss(masm);
2774}
2775
2776
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002777void CompareICStub::GenerateKnownReceivers(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002778 Label miss;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002779 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002780 __ mov(ecx, edx);
2781 __ and_(ecx, eax);
2782 __ JumpIfSmi(ecx, &miss, Label::kNear);
2783
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002784 __ GetWeakValue(edi, cell);
2785 __ cmp(edi, FieldOperand(eax, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002786 __ j(not_equal, &miss, Label::kNear);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002787 __ cmp(edi, FieldOperand(edx, HeapObject::kMapOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002788 __ j(not_equal, &miss, Label::kNear);
2789
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002790 if (Token::IsEqualityOp(op())) {
2791 __ sub(eax, edx);
2792 __ ret(0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002793 } else {
2794 __ PopReturnAddressTo(ecx);
2795 __ Push(edx);
2796 __ Push(eax);
2797 __ Push(Immediate(Smi::FromInt(NegativeComparisonResult(GetCondition()))));
2798 __ PushReturnAddressFrom(ecx);
2799 __ TailCallRuntime(Runtime::kCompare);
2800 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002801
2802 __ bind(&miss);
2803 GenerateMiss(masm);
2804}
2805
2806
2807void CompareICStub::GenerateMiss(MacroAssembler* masm) {
2808 {
2809 // Call the runtime system in a fresh internal frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002810 FrameScope scope(masm, StackFrame::INTERNAL);
2811 __ push(edx); // Preserve edx and eax.
2812 __ push(eax);
2813 __ push(edx); // And also use them as the arguments.
2814 __ push(eax);
2815 __ push(Immediate(Smi::FromInt(op())));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002816 __ CallRuntime(Runtime::kCompareIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002817 // Compute the entry point of the rewritten stub.
2818 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
2819 __ pop(eax);
2820 __ pop(edx);
2821 }
2822
2823 // Do a tail call to the rewritten stub.
2824 __ jmp(edi);
2825}
2826
2827
2828// Helper function used to check that the dictionary doesn't contain
2829// the property. This function may return false negatives, so miss_label
2830// must always call a backup property check that is complete.
2831// This function is safe to call if the receiver has fast properties.
2832// Name must be a unique name and receiver must be a heap object.
2833void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
2834 Label* miss,
2835 Label* done,
2836 Register properties,
2837 Handle<Name> name,
2838 Register r0) {
2839 DCHECK(name->IsUniqueName());
2840
2841 // If names of slots in range from 1 to kProbes - 1 for the hash value are
2842 // not equal to the name and kProbes-th slot is not used (its name is the
2843 // undefined value), it guarantees the hash table doesn't contain the
2844 // property. It's true even if some slots represent deleted properties
2845 // (their names are the hole value).
2846 for (int i = 0; i < kInlinedProbes; i++) {
2847 // Compute the masked index: (hash + i + i * i) & mask.
2848 Register index = r0;
2849 // Capacity is smi 2^n.
2850 __ mov(index, FieldOperand(properties, kCapacityOffset));
2851 __ dec(index);
2852 __ and_(index,
2853 Immediate(Smi::FromInt(name->Hash() +
2854 NameDictionary::GetProbeOffset(i))));
2855
2856 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002857 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002858 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
2859 Register entity_name = r0;
2860 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002861 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002862 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
2863 kElementsStartOffset - kHeapObjectTag));
2864 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
2865 __ j(equal, done);
2866
2867 // Stop if found the property.
2868 __ cmp(entity_name, Handle<Name>(name));
2869 __ j(equal, miss);
2870
2871 Label good;
2872 // Check for the hole and skip.
2873 __ cmp(entity_name, masm->isolate()->factory()->the_hole_value());
2874 __ j(equal, &good, Label::kNear);
2875
2876 // Check if the entry name is not a unique name.
2877 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
2878 __ JumpIfNotUniqueNameInstanceType(
2879 FieldOperand(entity_name, Map::kInstanceTypeOffset), miss);
2880 __ bind(&good);
2881 }
2882
2883 NameDictionaryLookupStub stub(masm->isolate(), properties, r0, r0,
2884 NEGATIVE_LOOKUP);
2885 __ push(Immediate(Handle<Object>(name)));
2886 __ push(Immediate(name->Hash()));
2887 __ CallStub(&stub);
2888 __ test(r0, r0);
2889 __ j(not_zero, miss);
2890 __ jmp(done);
2891}
2892
2893
2894// Probe the name dictionary in the |elements| register. Jump to the
2895// |done| label if a property with the given name is found leaving the
2896// index into the dictionary in |r0|. Jump to the |miss| label
2897// otherwise.
2898void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
2899 Label* miss,
2900 Label* done,
2901 Register elements,
2902 Register name,
2903 Register r0,
2904 Register r1) {
2905 DCHECK(!elements.is(r0));
2906 DCHECK(!elements.is(r1));
2907 DCHECK(!name.is(r0));
2908 DCHECK(!name.is(r1));
2909
2910 __ AssertName(name);
2911
2912 __ mov(r1, FieldOperand(elements, kCapacityOffset));
2913 __ shr(r1, kSmiTagSize); // convert smi to int
2914 __ dec(r1);
2915
2916 // Generate an unrolled loop that performs a few probes before
2917 // giving up. Measurements done on Gmail indicate that 2 probes
2918 // cover ~93% of loads from dictionaries.
2919 for (int i = 0; i < kInlinedProbes; i++) {
2920 // Compute the masked index: (hash + i + i * i) & mask.
2921 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
2922 __ shr(r0, Name::kHashShift);
2923 if (i > 0) {
2924 __ add(r0, Immediate(NameDictionary::GetProbeOffset(i)));
2925 }
2926 __ and_(r0, r1);
2927
2928 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002929 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002930 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
2931
2932 // Check if the key is identical to the name.
2933 __ cmp(name, Operand(elements,
2934 r0,
2935 times_4,
2936 kElementsStartOffset - kHeapObjectTag));
2937 __ j(equal, done);
2938 }
2939
2940 NameDictionaryLookupStub stub(masm->isolate(), elements, r1, r0,
2941 POSITIVE_LOOKUP);
2942 __ push(name);
2943 __ mov(r0, FieldOperand(name, Name::kHashFieldOffset));
2944 __ shr(r0, Name::kHashShift);
2945 __ push(r0);
2946 __ CallStub(&stub);
2947
2948 __ test(r1, r1);
2949 __ j(zero, miss);
2950 __ jmp(done);
2951}
2952
2953
2954void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
2955 // This stub overrides SometimesSetsUpAFrame() to return false. That means
2956 // we cannot call anything that could cause a GC from this stub.
2957 // Stack frame on entry:
2958 // esp[0 * kPointerSize]: return address.
2959 // esp[1 * kPointerSize]: key's hash.
2960 // esp[2 * kPointerSize]: key.
2961 // Registers:
2962 // dictionary_: NameDictionary to probe.
2963 // result_: used as scratch.
2964 // index_: will hold an index of entry if lookup is successful.
2965 // might alias with result_.
2966 // Returns:
2967 // result_ is zero if lookup failed, non zero otherwise.
2968
2969 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
2970
2971 Register scratch = result();
2972
2973 __ mov(scratch, FieldOperand(dictionary(), kCapacityOffset));
2974 __ dec(scratch);
2975 __ SmiUntag(scratch);
2976 __ push(scratch);
2977
2978 // If names of slots in range from 1 to kProbes - 1 for the hash value are
2979 // not equal to the name and kProbes-th slot is not used (its name is the
2980 // undefined value), it guarantees the hash table doesn't contain the
2981 // property. It's true even if some slots represent deleted properties
2982 // (their names are the null value).
2983 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
2984 // Compute the masked index: (hash + i + i * i) & mask.
2985 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2986 if (i > 0) {
2987 __ add(scratch, Immediate(NameDictionary::GetProbeOffset(i)));
2988 }
2989 __ and_(scratch, Operand(esp, 0));
2990
2991 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002992 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002993 __ lea(index(), Operand(scratch, scratch, times_2, 0)); // index *= 3.
2994
2995 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002996 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002997 __ mov(scratch, Operand(dictionary(), index(), times_pointer_size,
2998 kElementsStartOffset - kHeapObjectTag));
2999 __ cmp(scratch, isolate()->factory()->undefined_value());
3000 __ j(equal, &not_in_dictionary);
3001
3002 // Stop if found the property.
3003 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
3004 __ j(equal, &in_dictionary);
3005
3006 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3007 // If we hit a key that is not a unique name during negative
3008 // lookup we have to bailout as this key might be equal to the
3009 // key we are looking for.
3010
3011 // Check if the entry name is not a unique name.
3012 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
3013 __ JumpIfNotUniqueNameInstanceType(
3014 FieldOperand(scratch, Map::kInstanceTypeOffset),
3015 &maybe_in_dictionary);
3016 }
3017 }
3018
3019 __ bind(&maybe_in_dictionary);
3020 // If we are doing negative lookup then probing failure should be
3021 // treated as a lookup success. For positive lookup probing failure
3022 // should be treated as lookup failure.
3023 if (mode() == POSITIVE_LOOKUP) {
3024 __ mov(result(), Immediate(0));
3025 __ Drop(1);
3026 __ ret(2 * kPointerSize);
3027 }
3028
3029 __ bind(&in_dictionary);
3030 __ mov(result(), Immediate(1));
3031 __ Drop(1);
3032 __ ret(2 * kPointerSize);
3033
3034 __ bind(&not_in_dictionary);
3035 __ mov(result(), Immediate(0));
3036 __ Drop(1);
3037 __ ret(2 * kPointerSize);
3038}
3039
3040
3041void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3042 Isolate* isolate) {
3043 StoreBufferOverflowStub stub(isolate, kDontSaveFPRegs);
3044 stub.GetCode();
3045 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3046 stub2.GetCode();
3047}
3048
3049
3050// Takes the input in 3 registers: address_ value_ and object_. A pointer to
3051// the value has just been written into the object, now this stub makes sure
3052// we keep the GC informed. The word in the object where the value has been
3053// written is in the address register.
3054void RecordWriteStub::Generate(MacroAssembler* masm) {
3055 Label skip_to_incremental_noncompacting;
3056 Label skip_to_incremental_compacting;
3057
3058 // The first two instructions are generated with labels so as to get the
3059 // offset fixed up correctly by the bind(Label*) call. We patch it back and
3060 // forth between a compare instructions (a nop in this position) and the
3061 // real branch when we start and stop incremental heap marking.
3062 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
3063 __ jmp(&skip_to_incremental_compacting, Label::kFar);
3064
3065 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3066 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3067 MacroAssembler::kReturnAtEnd);
3068 } else {
3069 __ ret(0);
3070 }
3071
3072 __ bind(&skip_to_incremental_noncompacting);
3073 GenerateIncremental(masm, INCREMENTAL);
3074
3075 __ bind(&skip_to_incremental_compacting);
3076 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3077
3078 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3079 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
3080 masm->set_byte_at(0, kTwoByteNopInstruction);
3081 masm->set_byte_at(2, kFiveByteNopInstruction);
3082}
3083
3084
3085void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3086 regs_.Save(masm);
3087
3088 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3089 Label dont_need_remembered_set;
3090
3091 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3092 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
3093 regs_.scratch0(),
3094 &dont_need_remembered_set);
3095
Ben Murdoch097c5b22016-05-18 11:27:45 +01003096 __ JumpIfInNewSpace(regs_.object(), regs_.scratch0(),
3097 &dont_need_remembered_set);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003098
3099 // First notify the incremental marker if necessary, then update the
3100 // remembered set.
3101 CheckNeedsToInformIncrementalMarker(
3102 masm,
3103 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
3104 mode);
3105 InformIncrementalMarker(masm);
3106 regs_.Restore(masm);
3107 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3108 MacroAssembler::kReturnAtEnd);
3109
3110 __ bind(&dont_need_remembered_set);
3111 }
3112
3113 CheckNeedsToInformIncrementalMarker(
3114 masm,
3115 kReturnOnNoNeedToInformIncrementalMarker,
3116 mode);
3117 InformIncrementalMarker(masm);
3118 regs_.Restore(masm);
3119 __ ret(0);
3120}
3121
3122
3123void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3124 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
3125 int argument_count = 3;
3126 __ PrepareCallCFunction(argument_count, regs_.scratch0());
3127 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
3128 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
3129 __ mov(Operand(esp, 2 * kPointerSize),
3130 Immediate(ExternalReference::isolate_address(isolate())));
3131
3132 AllowExternalCallThatCantCauseGC scope(masm);
3133 __ CallCFunction(
3134 ExternalReference::incremental_marking_record_write_function(isolate()),
3135 argument_count);
3136
3137 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
3138}
3139
3140
3141void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3142 MacroAssembler* masm,
3143 OnNoNeedToInformIncrementalMarker on_no_need,
3144 Mode mode) {
3145 Label object_is_black, need_incremental, need_incremental_pop_object;
3146
3147 __ mov(regs_.scratch0(), Immediate(~Page::kPageAlignmentMask));
3148 __ and_(regs_.scratch0(), regs_.object());
3149 __ mov(regs_.scratch1(),
3150 Operand(regs_.scratch0(),
3151 MemoryChunk::kWriteBarrierCounterOffset));
3152 __ sub(regs_.scratch1(), Immediate(1));
3153 __ mov(Operand(regs_.scratch0(),
3154 MemoryChunk::kWriteBarrierCounterOffset),
3155 regs_.scratch1());
3156 __ j(negative, &need_incremental);
3157
3158 // Let's look at the color of the object: If it is not black we don't have
3159 // to inform the incremental marker.
3160 __ JumpIfBlack(regs_.object(),
3161 regs_.scratch0(),
3162 regs_.scratch1(),
3163 &object_is_black,
3164 Label::kNear);
3165
3166 regs_.Restore(masm);
3167 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
3168 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3169 MacroAssembler::kReturnAtEnd);
3170 } else {
3171 __ ret(0);
3172 }
3173
3174 __ bind(&object_is_black);
3175
3176 // Get the value from the slot.
3177 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
3178
3179 if (mode == INCREMENTAL_COMPACTION) {
3180 Label ensure_not_white;
3181
3182 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3183 regs_.scratch1(), // Scratch.
3184 MemoryChunk::kEvacuationCandidateMask,
3185 zero,
3186 &ensure_not_white,
3187 Label::kNear);
3188
3189 __ CheckPageFlag(regs_.object(),
3190 regs_.scratch1(), // Scratch.
3191 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3192 not_zero,
3193 &ensure_not_white,
3194 Label::kNear);
3195
3196 __ jmp(&need_incremental);
3197
3198 __ bind(&ensure_not_white);
3199 }
3200
3201 // We need an extra register for this, so we push the object register
3202 // temporarily.
3203 __ push(regs_.object());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003204 __ JumpIfWhite(regs_.scratch0(), // The value.
3205 regs_.scratch1(), // Scratch.
3206 regs_.object(), // Scratch.
3207 &need_incremental_pop_object, Label::kNear);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003208 __ pop(regs_.object());
3209
3210 regs_.Restore(masm);
3211 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
3212 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
3213 MacroAssembler::kReturnAtEnd);
3214 } else {
3215 __ ret(0);
3216 }
3217
3218 __ bind(&need_incremental_pop_object);
3219 __ pop(regs_.object());
3220
3221 __ bind(&need_incremental);
3222
3223 // Fall through when we need to inform the incremental marker.
3224}
3225
3226
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003227void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
3228 CEntryStub ces(isolate(), 1, kSaveFPRegs);
3229 __ call(ces.GetCode(), RelocInfo::CODE_TARGET);
3230 int parameter_count_offset =
Ben Murdochda12d292016-06-02 14:46:10 +01003231 StubFailureTrampolineFrameConstants::kArgumentsLengthOffset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003232 __ mov(ebx, MemOperand(ebp, parameter_count_offset));
3233 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
3234 __ pop(ecx);
3235 int additional_offset =
3236 function_mode() == JS_FUNCTION_STUB_MODE ? kPointerSize : 0;
3237 __ lea(esp, MemOperand(esp, ebx, times_pointer_size, additional_offset));
3238 __ jmp(ecx); // Return to IC Miss stub, continuation still on stack.
3239}
3240
3241
3242void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003243 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3244 LoadICStub stub(isolate(), state());
3245 stub.GenerateForTrampoline(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003246}
3247
3248
3249void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003250 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3251 KeyedLoadICStub stub(isolate(), state());
3252 stub.GenerateForTrampoline(masm);
3253}
3254
3255
3256static void HandleArrayCases(MacroAssembler* masm, Register receiver,
3257 Register key, Register vector, Register slot,
3258 Register feedback, bool is_polymorphic,
3259 Label* miss) {
3260 // feedback initially contains the feedback array
3261 Label next, next_loop, prepare_next;
3262 Label load_smi_map, compare_map;
3263 Label start_polymorphic;
3264
3265 __ push(receiver);
3266 __ push(vector);
3267
3268 Register receiver_map = receiver;
3269 Register cached_map = vector;
3270
3271 // Receiver might not be a heap object.
3272 __ JumpIfSmi(receiver, &load_smi_map);
3273 __ mov(receiver_map, FieldOperand(receiver, 0));
3274 __ bind(&compare_map);
3275 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3276
3277 // A named keyed load might have a 2 element array, all other cases can count
3278 // on an array with at least 2 {map, handler} pairs, so they can go right
3279 // into polymorphic array handling.
3280 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3281 __ j(not_equal, is_polymorphic ? &start_polymorphic : &next);
3282
3283 // found, now call handler.
3284 Register handler = feedback;
3285 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3286 __ pop(vector);
3287 __ pop(receiver);
3288 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3289 __ jmp(handler);
3290
3291 if (!is_polymorphic) {
3292 __ bind(&next);
3293 __ cmp(FieldOperand(feedback, FixedArray::kLengthOffset),
3294 Immediate(Smi::FromInt(2)));
3295 __ j(not_equal, &start_polymorphic);
3296 __ pop(vector);
3297 __ pop(receiver);
3298 __ jmp(miss);
3299 }
3300
3301 // Polymorphic, we have to loop from 2 to N
3302 __ bind(&start_polymorphic);
3303 __ push(key);
3304 Register counter = key;
3305 __ mov(counter, Immediate(Smi::FromInt(2)));
3306 __ bind(&next_loop);
3307 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3308 FixedArray::kHeaderSize));
3309 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3310 __ j(not_equal, &prepare_next);
3311 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
3312 FixedArray::kHeaderSize + kPointerSize));
3313 __ pop(key);
3314 __ pop(vector);
3315 __ pop(receiver);
3316 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3317 __ jmp(handler);
3318
3319 __ bind(&prepare_next);
3320 __ add(counter, Immediate(Smi::FromInt(2)));
3321 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3322 __ j(less, &next_loop);
3323
3324 // We exhausted our array of map handler pairs.
3325 __ pop(key);
3326 __ pop(vector);
3327 __ pop(receiver);
3328 __ jmp(miss);
3329
3330 __ bind(&load_smi_map);
3331 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3332 __ jmp(&compare_map);
3333}
3334
3335
3336static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
3337 Register key, Register vector, Register slot,
3338 Register weak_cell, Label* miss) {
3339 // feedback initially contains the feedback array
3340 Label compare_smi_map;
3341
3342 // Move the weak map into the weak_cell register.
3343 Register ic_map = weak_cell;
3344 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
3345
3346 // Receiver might not be a heap object.
3347 __ JumpIfSmi(receiver, &compare_smi_map);
3348 __ cmp(ic_map, FieldOperand(receiver, 0));
3349 __ j(not_equal, miss);
3350 Register handler = weak_cell;
3351 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
3352 FixedArray::kHeaderSize + kPointerSize));
3353 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3354 __ jmp(handler);
3355
3356 // In microbenchmarks, it made sense to unroll this code so that the call to
3357 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
3358 __ bind(&compare_smi_map);
3359 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
3360 __ j(not_equal, miss);
3361 __ mov(handler, FieldOperand(vector, slot, times_half_pointer_size,
3362 FixedArray::kHeaderSize + kPointerSize));
3363 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3364 __ jmp(handler);
3365}
3366
3367
3368void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
3369
3370
3371void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3372 GenerateImpl(masm, true);
3373}
3374
3375
3376void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3377 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
3378 Register name = LoadWithVectorDescriptor::NameRegister(); // ecx
3379 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
3380 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
3381 Register scratch = edi;
3382 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3383 FixedArray::kHeaderSize));
3384
3385 // Is it a weak cell?
3386 Label try_array;
3387 Label not_array, smi_key, key_okay, miss;
3388 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3389 __ j(not_equal, &try_array);
3390 HandleMonomorphicCase(masm, receiver, name, vector, slot, scratch, &miss);
3391
3392 // Is it a fixed array?
3393 __ bind(&try_array);
3394 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3395 __ j(not_equal, &not_array);
3396 HandleArrayCases(masm, receiver, name, vector, slot, scratch, true, &miss);
3397
3398 __ bind(&not_array);
3399 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3400 __ j(not_equal, &miss);
3401 __ push(slot);
3402 __ push(vector);
Ben Murdochc5610432016-08-08 18:44:38 +01003403 Code::Flags code_flags =
3404 Code::RemoveHolderFromFlags(Code::ComputeHandlerFlags(Code::LOAD_IC));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003405 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
3406 receiver, name, vector, scratch);
3407 __ pop(vector);
3408 __ pop(slot);
3409
3410 __ bind(&miss);
3411 LoadIC::GenerateMiss(masm);
3412}
3413
3414
3415void KeyedLoadICStub::Generate(MacroAssembler* masm) {
3416 GenerateImpl(masm, false);
3417}
3418
3419
3420void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3421 GenerateImpl(masm, true);
3422}
3423
3424
3425void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3426 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // edx
3427 Register key = LoadWithVectorDescriptor::NameRegister(); // ecx
3428 Register vector = LoadWithVectorDescriptor::VectorRegister(); // ebx
3429 Register slot = LoadWithVectorDescriptor::SlotRegister(); // eax
3430 Register feedback = edi;
3431 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
3432 FixedArray::kHeaderSize));
3433 // Is it a weak cell?
3434 Label try_array;
3435 Label not_array, smi_key, key_okay, miss;
3436 __ CompareRoot(FieldOperand(feedback, 0), Heap::kWeakCellMapRootIndex);
3437 __ j(not_equal, &try_array);
3438 HandleMonomorphicCase(masm, receiver, key, vector, slot, feedback, &miss);
3439
3440 __ bind(&try_array);
3441 // Is it a fixed array?
3442 __ CompareRoot(FieldOperand(feedback, 0), Heap::kFixedArrayMapRootIndex);
3443 __ j(not_equal, &not_array);
3444
3445 // We have a polymorphic element handler.
3446 Label polymorphic, try_poly_name;
3447 __ bind(&polymorphic);
3448 HandleArrayCases(masm, receiver, key, vector, slot, feedback, true, &miss);
3449
3450 __ bind(&not_array);
3451 // Is it generic?
3452 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
3453 __ j(not_equal, &try_poly_name);
3454 Handle<Code> megamorphic_stub =
3455 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3456 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
3457
3458 __ bind(&try_poly_name);
3459 // We might have a name in feedback, and a fixed array in the next slot.
3460 __ cmp(key, feedback);
3461 __ j(not_equal, &miss);
3462 // If the name comparison succeeded, we know we have a fixed array with
3463 // at least one map/handler pair.
3464 __ mov(feedback, FieldOperand(vector, slot, times_half_pointer_size,
3465 FixedArray::kHeaderSize + kPointerSize));
3466 HandleArrayCases(masm, receiver, key, vector, slot, feedback, false, &miss);
3467
3468 __ bind(&miss);
3469 KeyedLoadIC::GenerateMiss(masm);
3470}
3471
3472
3473void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3474 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3475 VectorStoreICStub stub(isolate(), state());
3476 stub.GenerateForTrampoline(masm);
3477}
3478
3479
3480void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3481 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3482 VectorKeyedStoreICStub stub(isolate(), state());
3483 stub.GenerateForTrampoline(masm);
3484}
3485
3486
3487void VectorStoreICStub::Generate(MacroAssembler* masm) {
3488 GenerateImpl(masm, false);
3489}
3490
3491
3492void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3493 GenerateImpl(masm, true);
3494}
3495
3496
3497// value is on the stack already.
3498static void HandlePolymorphicStoreCase(MacroAssembler* masm, Register receiver,
3499 Register key, Register vector,
3500 Register slot, Register feedback,
3501 bool is_polymorphic, Label* miss) {
3502 // feedback initially contains the feedback array
3503 Label next, next_loop, prepare_next;
3504 Label load_smi_map, compare_map;
3505 Label start_polymorphic;
3506 Label pop_and_miss;
3507 ExternalReference virtual_register =
3508 ExternalReference::virtual_handler_register(masm->isolate());
3509
3510 __ push(receiver);
3511 __ push(vector);
3512
3513 Register receiver_map = receiver;
3514 Register cached_map = vector;
3515
3516 // Receiver might not be a heap object.
3517 __ JumpIfSmi(receiver, &load_smi_map);
3518 __ mov(receiver_map, FieldOperand(receiver, 0));
3519 __ bind(&compare_map);
3520 __ mov(cached_map, FieldOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3521
3522 // A named keyed store might have a 2 element array, all other cases can count
3523 // on an array with at least 2 {map, handler} pairs, so they can go right
3524 // into polymorphic array handling.
3525 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3526 __ j(not_equal, &start_polymorphic);
3527
3528 // found, now call handler.
3529 Register handler = feedback;
3530 DCHECK(handler.is(VectorStoreICDescriptor::ValueRegister()));
3531 __ mov(handler, FieldOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3532 __ pop(vector);
3533 __ pop(receiver);
3534 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3535 __ mov(Operand::StaticVariable(virtual_register), handler);
3536 __ pop(handler); // Pop "value".
3537 __ jmp(Operand::StaticVariable(virtual_register));
3538
3539 // Polymorphic, we have to loop from 2 to N
3540 __ bind(&start_polymorphic);
3541 __ push(key);
3542 Register counter = key;
3543 __ mov(counter, Immediate(Smi::FromInt(2)));
3544
3545 if (!is_polymorphic) {
3546 // If is_polymorphic is false, we may only have a two element array.
3547 // Check against length now in that case.
3548 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3549 __ j(greater_equal, &pop_and_miss);
3550 }
3551
3552 __ bind(&next_loop);
3553 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3554 FixedArray::kHeaderSize));
3555 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3556 __ j(not_equal, &prepare_next);
3557 __ mov(handler, FieldOperand(feedback, counter, times_half_pointer_size,
3558 FixedArray::kHeaderSize + kPointerSize));
3559 __ lea(handler, FieldOperand(handler, Code::kHeaderSize));
3560 __ pop(key);
3561 __ pop(vector);
3562 __ pop(receiver);
3563 __ mov(Operand::StaticVariable(virtual_register), handler);
3564 __ pop(handler); // Pop "value".
3565 __ jmp(Operand::StaticVariable(virtual_register));
3566
3567 __ bind(&prepare_next);
3568 __ add(counter, Immediate(Smi::FromInt(2)));
3569 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3570 __ j(less, &next_loop);
3571
3572 // We exhausted our array of map handler pairs.
3573 __ bind(&pop_and_miss);
3574 __ pop(key);
3575 __ pop(vector);
3576 __ pop(receiver);
3577 __ jmp(miss);
3578
3579 __ bind(&load_smi_map);
3580 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3581 __ jmp(&compare_map);
3582}
3583
3584
3585static void HandleMonomorphicStoreCase(MacroAssembler* masm, Register receiver,
3586 Register key, Register vector,
3587 Register slot, Register weak_cell,
3588 Label* miss) {
3589 // The store ic value is on the stack.
3590 DCHECK(weak_cell.is(VectorStoreICDescriptor::ValueRegister()));
3591 ExternalReference virtual_register =
3592 ExternalReference::virtual_handler_register(masm->isolate());
3593
3594 // feedback initially contains the feedback array
3595 Label compare_smi_map;
3596
3597 // Move the weak map into the weak_cell register.
3598 Register ic_map = weak_cell;
3599 __ mov(ic_map, FieldOperand(weak_cell, WeakCell::kValueOffset));
3600
3601 // Receiver might not be a heap object.
3602 __ JumpIfSmi(receiver, &compare_smi_map);
3603 __ cmp(ic_map, FieldOperand(receiver, 0));
3604 __ j(not_equal, miss);
3605 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
3606 FixedArray::kHeaderSize + kPointerSize));
3607 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
3608 // Put the store ic value back in it's register.
3609 __ mov(Operand::StaticVariable(virtual_register), weak_cell);
3610 __ pop(weak_cell); // Pop "value".
3611 // jump to the handler.
3612 __ jmp(Operand::StaticVariable(virtual_register));
3613
3614 // In microbenchmarks, it made sense to unroll this code so that the call to
3615 // the handler is duplicated for a HeapObject receiver and a Smi receiver.
3616 __ bind(&compare_smi_map);
3617 __ CompareRoot(ic_map, Heap::kHeapNumberMapRootIndex);
3618 __ j(not_equal, miss);
3619 __ mov(weak_cell, FieldOperand(vector, slot, times_half_pointer_size,
3620 FixedArray::kHeaderSize + kPointerSize));
3621 __ lea(weak_cell, FieldOperand(weak_cell, Code::kHeaderSize));
3622 __ mov(Operand::StaticVariable(virtual_register), weak_cell);
3623 __ pop(weak_cell); // Pop "value".
3624 // jump to the handler.
3625 __ jmp(Operand::StaticVariable(virtual_register));
3626}
3627
3628
3629void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3630 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
3631 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
3632 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
3633 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
3634 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
3635 Label miss;
3636
3637 __ push(value);
3638
3639 Register scratch = value;
3640 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3641 FixedArray::kHeaderSize));
3642
3643 // Is it a weak cell?
3644 Label try_array;
3645 Label not_array, smi_key, key_okay;
3646 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3647 __ j(not_equal, &try_array);
3648 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
3649
3650 // Is it a fixed array?
3651 __ bind(&try_array);
3652 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3653 __ j(not_equal, &not_array);
3654 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, true,
3655 &miss);
3656
3657 __ bind(&not_array);
3658 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3659 __ j(not_equal, &miss);
3660
3661 __ pop(value);
3662 __ push(slot);
3663 __ push(vector);
Ben Murdochc5610432016-08-08 18:44:38 +01003664 Code::Flags code_flags =
3665 Code::RemoveHolderFromFlags(Code::ComputeHandlerFlags(Code::STORE_IC));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003666 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::STORE_IC, code_flags,
3667 receiver, key, slot, no_reg);
3668 __ pop(vector);
3669 __ pop(slot);
3670 Label no_pop_miss;
3671 __ jmp(&no_pop_miss);
3672
3673 __ bind(&miss);
3674 __ pop(value);
3675 __ bind(&no_pop_miss);
3676 StoreIC::GenerateMiss(masm);
3677}
3678
3679
3680void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
3681 GenerateImpl(masm, false);
3682}
3683
3684
3685void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3686 GenerateImpl(masm, true);
3687}
3688
3689
3690static void HandlePolymorphicKeyedStoreCase(MacroAssembler* masm,
3691 Register receiver, Register key,
3692 Register vector, Register slot,
3693 Register feedback, Label* miss) {
3694 // feedback initially contains the feedback array
3695 Label next, next_loop, prepare_next;
3696 Label load_smi_map, compare_map;
3697 Label transition_call;
3698 Label pop_and_miss;
3699 ExternalReference virtual_register =
3700 ExternalReference::virtual_handler_register(masm->isolate());
3701 ExternalReference virtual_slot =
3702 ExternalReference::virtual_slot_register(masm->isolate());
3703
3704 __ push(receiver);
3705 __ push(vector);
3706
3707 Register receiver_map = receiver;
3708 Register cached_map = vector;
3709 Register value = StoreDescriptor::ValueRegister();
3710
3711 // Receiver might not be a heap object.
3712 __ JumpIfSmi(receiver, &load_smi_map);
3713 __ mov(receiver_map, FieldOperand(receiver, 0));
3714 __ bind(&compare_map);
3715
3716 // Polymorphic, we have to loop from 0 to N - 1
3717 __ push(key);
3718 // Current stack layout:
3719 // - esp[0] -- key
3720 // - esp[4] -- vector
3721 // - esp[8] -- receiver
3722 // - esp[12] -- value
3723 // - esp[16] -- return address
3724 //
3725 // Required stack layout for handler call:
3726 // - esp[0] -- return address
3727 // - receiver, key, value, vector, slot in registers.
3728 // - handler in virtual register.
3729 Register counter = key;
3730 __ mov(counter, Immediate(Smi::FromInt(0)));
3731 __ bind(&next_loop);
3732 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3733 FixedArray::kHeaderSize));
3734 __ cmp(receiver_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3735 __ j(not_equal, &prepare_next);
3736 __ mov(cached_map, FieldOperand(feedback, counter, times_half_pointer_size,
3737 FixedArray::kHeaderSize + kPointerSize));
3738 __ CompareRoot(cached_map, Heap::kUndefinedValueRootIndex);
3739 __ j(not_equal, &transition_call);
3740 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
3741 FixedArray::kHeaderSize + 2 * kPointerSize));
3742 __ pop(key);
3743 __ pop(vector);
3744 __ pop(receiver);
3745 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
3746 __ mov(Operand::StaticVariable(virtual_register), feedback);
3747 __ pop(value);
3748 __ jmp(Operand::StaticVariable(virtual_register));
3749
3750 __ bind(&transition_call);
3751 // Current stack layout:
3752 // - esp[0] -- key
3753 // - esp[4] -- vector
3754 // - esp[8] -- receiver
3755 // - esp[12] -- value
3756 // - esp[16] -- return address
3757 //
3758 // Required stack layout for handler call:
3759 // - esp[0] -- return address
3760 // - receiver, key, value, map, vector in registers.
3761 // - handler and slot in virtual registers.
3762 __ mov(Operand::StaticVariable(virtual_slot), slot);
3763 __ mov(feedback, FieldOperand(feedback, counter, times_half_pointer_size,
3764 FixedArray::kHeaderSize + 2 * kPointerSize));
3765 __ lea(feedback, FieldOperand(feedback, Code::kHeaderSize));
3766 __ mov(Operand::StaticVariable(virtual_register), feedback);
3767
3768 __ mov(cached_map, FieldOperand(cached_map, WeakCell::kValueOffset));
3769 // The weak cell may have been cleared.
3770 __ JumpIfSmi(cached_map, &pop_and_miss);
3771 DCHECK(!cached_map.is(VectorStoreTransitionDescriptor::MapRegister()));
3772 __ mov(VectorStoreTransitionDescriptor::MapRegister(), cached_map);
3773
3774 // Pop key into place.
3775 __ pop(key);
3776 __ pop(vector);
3777 __ pop(receiver);
3778 __ pop(value);
3779 __ jmp(Operand::StaticVariable(virtual_register));
3780
3781 __ bind(&prepare_next);
3782 __ add(counter, Immediate(Smi::FromInt(3)));
3783 __ cmp(counter, FieldOperand(feedback, FixedArray::kLengthOffset));
3784 __ j(less, &next_loop);
3785
3786 // We exhausted our array of map handler pairs.
3787 __ bind(&pop_and_miss);
3788 __ pop(key);
3789 __ pop(vector);
3790 __ pop(receiver);
3791 __ jmp(miss);
3792
3793 __ bind(&load_smi_map);
3794 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3795 __ jmp(&compare_map);
3796}
3797
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003798void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3799 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // edx
3800 Register key = VectorStoreICDescriptor::NameRegister(); // ecx
3801 Register value = VectorStoreICDescriptor::ValueRegister(); // eax
3802 Register vector = VectorStoreICDescriptor::VectorRegister(); // ebx
3803 Register slot = VectorStoreICDescriptor::SlotRegister(); // edi
3804 Label miss;
3805
3806 __ push(value);
3807
3808 Register scratch = value;
3809 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3810 FixedArray::kHeaderSize));
3811
3812 // Is it a weak cell?
3813 Label try_array;
3814 Label not_array, smi_key, key_okay;
3815 __ CompareRoot(FieldOperand(scratch, 0), Heap::kWeakCellMapRootIndex);
3816 __ j(not_equal, &try_array);
3817 HandleMonomorphicStoreCase(masm, receiver, key, vector, slot, scratch, &miss);
3818
3819 // Is it a fixed array?
3820 __ bind(&try_array);
3821 __ CompareRoot(FieldOperand(scratch, 0), Heap::kFixedArrayMapRootIndex);
3822 __ j(not_equal, &not_array);
3823 HandlePolymorphicKeyedStoreCase(masm, receiver, key, vector, slot, scratch,
3824 &miss);
3825
3826 __ bind(&not_array);
3827 Label try_poly_name;
3828 __ CompareRoot(scratch, Heap::kmegamorphic_symbolRootIndex);
3829 __ j(not_equal, &try_poly_name);
3830
3831 __ pop(value);
3832
3833 Handle<Code> megamorphic_stub =
3834 KeyedStoreIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3835 __ jmp(megamorphic_stub, RelocInfo::CODE_TARGET);
3836
3837 __ bind(&try_poly_name);
3838 // We might have a name in feedback, and a fixed array in the next slot.
3839 __ cmp(key, scratch);
3840 __ j(not_equal, &miss);
3841 // If the name comparison succeeded, we know we have a fixed array with
3842 // at least one map/handler pair.
3843 __ mov(scratch, FieldOperand(vector, slot, times_half_pointer_size,
3844 FixedArray::kHeaderSize + kPointerSize));
3845 HandlePolymorphicStoreCase(masm, receiver, key, vector, slot, scratch, false,
3846 &miss);
3847
3848 __ bind(&miss);
3849 __ pop(value);
3850 KeyedStoreIC::GenerateMiss(masm);
3851}
3852
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003853void CallICTrampolineStub::Generate(MacroAssembler* masm) {
3854 __ EmitLoadTypeFeedbackVector(ebx);
3855 CallICStub stub(isolate(), state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003856 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
3857}
3858
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003859void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
3860 if (masm->isolate()->function_entry_hook() != NULL) {
3861 ProfileEntryHookStub stub(masm->isolate());
3862 masm->CallStub(&stub);
3863 }
3864}
3865
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003866void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
3867 // Save volatile registers.
3868 const int kNumSavedRegisters = 3;
3869 __ push(eax);
3870 __ push(ecx);
3871 __ push(edx);
3872
3873 // Calculate and push the original stack pointer.
3874 __ lea(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
3875 __ push(eax);
3876
3877 // Retrieve our return address and use it to calculate the calling
3878 // function's address.
3879 __ mov(eax, Operand(esp, (kNumSavedRegisters + 1) * kPointerSize));
3880 __ sub(eax, Immediate(Assembler::kCallInstructionLength));
3881 __ push(eax);
3882
3883 // Call the entry hook.
3884 DCHECK(isolate()->function_entry_hook() != NULL);
3885 __ call(FUNCTION_ADDR(isolate()->function_entry_hook()),
3886 RelocInfo::RUNTIME_ENTRY);
3887 __ add(esp, Immediate(2 * kPointerSize));
3888
3889 // Restore ecx.
3890 __ pop(edx);
3891 __ pop(ecx);
3892 __ pop(eax);
3893
3894 __ ret(0);
3895}
3896
Ben Murdoch097c5b22016-05-18 11:27:45 +01003897template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003898static void CreateArrayDispatch(MacroAssembler* masm,
3899 AllocationSiteOverrideMode mode) {
3900 if (mode == DISABLE_ALLOCATION_SITES) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003901 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003902 __ TailCallStub(&stub);
3903 } else if (mode == DONT_OVERRIDE) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01003904 int last_index =
3905 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003906 for (int i = 0; i <= last_index; ++i) {
3907 Label next;
3908 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
3909 __ cmp(edx, kind);
3910 __ j(not_equal, &next);
3911 T stub(masm->isolate(), kind);
3912 __ TailCallStub(&stub);
3913 __ bind(&next);
3914 }
3915
3916 // If we reached this point there is a problem.
3917 __ Abort(kUnexpectedElementsKindInArrayConstructor);
3918 } else {
3919 UNREACHABLE();
3920 }
3921}
3922
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003923static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
3924 AllocationSiteOverrideMode mode) {
3925 // ebx - allocation site (if mode != DISABLE_ALLOCATION_SITES)
3926 // edx - kind (if mode != DISABLE_ALLOCATION_SITES)
3927 // eax - number of arguments
3928 // edi - constructor?
3929 // esp[0] - return address
3930 // esp[4] - last argument
3931 Label normal_sequence;
3932 if (mode == DONT_OVERRIDE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003933 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
3934 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
3935 STATIC_ASSERT(FAST_ELEMENTS == 2);
3936 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
3937 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
3938 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003939
3940 // is the low bit set? If so, we are holey and that is good.
Ben Murdochda12d292016-06-02 14:46:10 +01003941 __ test_b(edx, Immediate(1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003942 __ j(not_zero, &normal_sequence);
3943 }
3944
3945 // look at the first argument
3946 __ mov(ecx, Operand(esp, kPointerSize));
3947 __ test(ecx, ecx);
3948 __ j(zero, &normal_sequence);
3949
3950 if (mode == DISABLE_ALLOCATION_SITES) {
3951 ElementsKind initial = GetInitialFastElementsKind();
3952 ElementsKind holey_initial = GetHoleyElementsKind(initial);
3953
Ben Murdoch097c5b22016-05-18 11:27:45 +01003954 ArraySingleArgumentConstructorStub stub_holey(
3955 masm->isolate(), holey_initial, DISABLE_ALLOCATION_SITES);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003956 __ TailCallStub(&stub_holey);
3957
3958 __ bind(&normal_sequence);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003959 ArraySingleArgumentConstructorStub stub(masm->isolate(), initial,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003960 DISABLE_ALLOCATION_SITES);
3961 __ TailCallStub(&stub);
3962 } else if (mode == DONT_OVERRIDE) {
3963 // We are going to create a holey array, but our kind is non-holey.
3964 // Fix kind and retry.
3965 __ inc(edx);
3966
3967 if (FLAG_debug_code) {
3968 Handle<Map> allocation_site_map =
3969 masm->isolate()->factory()->allocation_site_map();
3970 __ cmp(FieldOperand(ebx, 0), Immediate(allocation_site_map));
3971 __ Assert(equal, kExpectedAllocationSite);
3972 }
3973
3974 // Save the resulting elements kind in type info. We can't just store r3
3975 // in the AllocationSite::transition_info field because elements kind is
3976 // restricted to a portion of the field...upper bits need to be left alone.
3977 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
3978 __ add(FieldOperand(ebx, AllocationSite::kTransitionInfoOffset),
3979 Immediate(Smi::FromInt(kFastElementsKindPackedToHoley)));
3980
3981 __ bind(&normal_sequence);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003982 int last_index =
3983 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003984 for (int i = 0; i <= last_index; ++i) {
3985 Label next;
3986 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
3987 __ cmp(edx, kind);
3988 __ j(not_equal, &next);
3989 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
3990 __ TailCallStub(&stub);
3991 __ bind(&next);
3992 }
3993
3994 // If we reached this point there is a problem.
3995 __ Abort(kUnexpectedElementsKindInArrayConstructor);
3996 } else {
3997 UNREACHABLE();
3998 }
3999}
4000
Ben Murdoch097c5b22016-05-18 11:27:45 +01004001template <class T>
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004002static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004003 int to_index =
4004 GetSequenceIndexFromFastElementsKind(TERMINAL_FAST_ELEMENTS_KIND);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004005 for (int i = 0; i <= to_index; ++i) {
4006 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4007 T stub(isolate, kind);
4008 stub.GetCode();
4009 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4010 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4011 stub1.GetCode();
4012 }
4013 }
4014}
4015
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004016void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4017 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4018 isolate);
4019 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4020 isolate);
4021 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4022 isolate);
4023}
4024
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004025void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4026 Isolate* isolate) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01004027 ElementsKind kinds[2] = {FAST_ELEMENTS, FAST_HOLEY_ELEMENTS};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004028 for (int i = 0; i < 2; i++) {
4029 // For internal arrays we only need a few things
4030 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4031 stubh1.GetCode();
4032 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4033 stubh2.GetCode();
4034 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4035 stubh3.GetCode();
4036 }
4037}
4038
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004039void ArrayConstructorStub::GenerateDispatchToArrayStub(
Ben Murdoch097c5b22016-05-18 11:27:45 +01004040 MacroAssembler* masm, AllocationSiteOverrideMode mode) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004041 if (argument_count() == ANY) {
4042 Label not_zero_case, not_one_case;
4043 __ test(eax, eax);
4044 __ j(not_zero, &not_zero_case);
4045 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4046
4047 __ bind(&not_zero_case);
4048 __ cmp(eax, 1);
4049 __ j(greater, &not_one_case);
4050 CreateArrayDispatchOneArgument(masm, mode);
4051
4052 __ bind(&not_one_case);
4053 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4054 } else if (argument_count() == NONE) {
4055 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4056 } else if (argument_count() == ONE) {
4057 CreateArrayDispatchOneArgument(masm, mode);
4058 } else if (argument_count() == MORE_THAN_ONE) {
4059 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4060 } else {
4061 UNREACHABLE();
4062 }
4063}
4064
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004065void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4066 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004067 // -- eax : argc (only if argument_count() is ANY or MORE_THAN_ONE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004068 // -- ebx : AllocationSite or undefined
4069 // -- edi : constructor
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004070 // -- edx : Original constructor
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004071 // -- esp[0] : return address
4072 // -- esp[4] : last argument
4073 // -----------------------------------
4074 if (FLAG_debug_code) {
4075 // The array construct code is only set for the global and natives
4076 // builtin Array functions which always have maps.
4077
4078 // Initial map for the builtin Array function should be a map.
4079 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4080 // Will both indicate a NULL and a Smi.
4081 __ test(ecx, Immediate(kSmiTagMask));
4082 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4083 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4084 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4085
4086 // We should either have undefined in ebx or a valid AllocationSite
4087 __ AssertUndefinedOrAllocationSite(ebx);
4088 }
4089
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004090 Label subclassing;
4091
4092 // Enter the context of the Array function.
4093 __ mov(esi, FieldOperand(edi, JSFunction::kContextOffset));
4094
4095 __ cmp(edx, edi);
4096 __ j(not_equal, &subclassing);
4097
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004098 Label no_info;
4099 // If the feedback vector is the undefined value call an array constructor
4100 // that doesn't use AllocationSites.
4101 __ cmp(ebx, isolate()->factory()->undefined_value());
4102 __ j(equal, &no_info);
4103
4104 // Only look at the lower 16 bits of the transition info.
4105 __ mov(edx, FieldOperand(ebx, AllocationSite::kTransitionInfoOffset));
4106 __ SmiUntag(edx);
4107 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4108 __ and_(edx, Immediate(AllocationSite::ElementsKindBits::kMask));
4109 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4110
4111 __ bind(&no_info);
4112 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004113
4114 // Subclassing.
4115 __ bind(&subclassing);
4116 switch (argument_count()) {
4117 case ANY:
4118 case MORE_THAN_ONE:
4119 __ mov(Operand(esp, eax, times_pointer_size, kPointerSize), edi);
4120 __ add(eax, Immediate(3));
4121 break;
4122 case NONE:
4123 __ mov(Operand(esp, 1 * kPointerSize), edi);
4124 __ mov(eax, Immediate(3));
4125 break;
4126 case ONE:
4127 __ mov(Operand(esp, 2 * kPointerSize), edi);
4128 __ mov(eax, Immediate(4));
4129 break;
4130 }
4131 __ PopReturnAddressTo(ecx);
4132 __ Push(edx);
4133 __ Push(ebx);
4134 __ PushReturnAddressFrom(ecx);
4135 __ JumpToExternalReference(ExternalReference(Runtime::kNewArray, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004136}
4137
Ben Murdoch097c5b22016-05-18 11:27:45 +01004138void InternalArrayConstructorStub::GenerateCase(MacroAssembler* masm,
4139 ElementsKind kind) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004140 Label not_zero_case, not_one_case;
4141 Label normal_sequence;
4142
4143 __ test(eax, eax);
4144 __ j(not_zero, &not_zero_case);
4145 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4146 __ TailCallStub(&stub0);
4147
4148 __ bind(&not_zero_case);
4149 __ cmp(eax, 1);
4150 __ j(greater, &not_one_case);
4151
4152 if (IsFastPackedElementsKind(kind)) {
4153 // We might need to create a holey array
4154 // look at the first argument
4155 __ mov(ecx, Operand(esp, kPointerSize));
4156 __ test(ecx, ecx);
4157 __ j(zero, &normal_sequence);
4158
Ben Murdoch097c5b22016-05-18 11:27:45 +01004159 InternalArraySingleArgumentConstructorStub stub1_holey(
4160 isolate(), GetHoleyElementsKind(kind));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004161 __ TailCallStub(&stub1_holey);
4162 }
4163
4164 __ bind(&normal_sequence);
4165 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4166 __ TailCallStub(&stub1);
4167
4168 __ bind(&not_one_case);
4169 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4170 __ TailCallStub(&stubN);
4171}
4172
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004173void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4174 // ----------- S t a t e -------------
4175 // -- eax : argc
4176 // -- edi : constructor
4177 // -- esp[0] : return address
4178 // -- esp[4] : last argument
4179 // -----------------------------------
4180
4181 if (FLAG_debug_code) {
4182 // The array construct code is only set for the global and natives
4183 // builtin Array functions which always have maps.
4184
4185 // Initial map for the builtin Array function should be a map.
4186 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4187 // Will both indicate a NULL and a Smi.
4188 __ test(ecx, Immediate(kSmiTagMask));
4189 __ Assert(not_zero, kUnexpectedInitialMapForArrayFunction);
4190 __ CmpObjectType(ecx, MAP_TYPE, ecx);
4191 __ Assert(equal, kUnexpectedInitialMapForArrayFunction);
4192 }
4193
4194 // Figure out the right elements kind
4195 __ mov(ecx, FieldOperand(edi, JSFunction::kPrototypeOrInitialMapOffset));
4196
4197 // Load the map's "bit field 2" into |result|. We only need the first byte,
4198 // but the following masking takes care of that anyway.
4199 __ mov(ecx, FieldOperand(ecx, Map::kBitField2Offset));
4200 // Retrieve elements_kind from bit field 2.
4201 __ DecodeField<Map::ElementsKindBits>(ecx);
4202
4203 if (FLAG_debug_code) {
4204 Label done;
4205 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4206 __ j(equal, &done);
4207 __ cmp(ecx, Immediate(FAST_HOLEY_ELEMENTS));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004208 __ Assert(equal, kInvalidElementsKindForInternalArrayOrInternalPackedArray);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004209 __ bind(&done);
4210 }
4211
4212 Label fast_elements_case;
4213 __ cmp(ecx, Immediate(FAST_ELEMENTS));
4214 __ j(equal, &fast_elements_case);
4215 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4216
4217 __ bind(&fast_elements_case);
4218 GenerateCase(masm, FAST_ELEMENTS);
4219}
4220
Ben Murdoch097c5b22016-05-18 11:27:45 +01004221void FastNewObjectStub::Generate(MacroAssembler* masm) {
4222 // ----------- S t a t e -------------
4223 // -- edi : target
4224 // -- edx : new target
4225 // -- esi : context
4226 // -- esp[0] : return address
4227 // -----------------------------------
4228 __ AssertFunction(edi);
4229 __ AssertReceiver(edx);
4230
4231 // Verify that the new target is a JSFunction.
4232 Label new_object;
4233 __ CmpObjectType(edx, JS_FUNCTION_TYPE, ebx);
4234 __ j(not_equal, &new_object);
4235
4236 // Load the initial map and verify that it's in fact a map.
4237 __ mov(ecx, FieldOperand(edx, JSFunction::kPrototypeOrInitialMapOffset));
4238 __ JumpIfSmi(ecx, &new_object);
4239 __ CmpObjectType(ecx, MAP_TYPE, ebx);
4240 __ j(not_equal, &new_object);
4241
4242 // Fall back to runtime if the target differs from the new target's
4243 // initial map constructor.
4244 __ cmp(edi, FieldOperand(ecx, Map::kConstructorOrBackPointerOffset));
4245 __ j(not_equal, &new_object);
4246
4247 // Allocate the JSObject on the heap.
4248 Label allocate, done_allocate;
4249 __ movzx_b(ebx, FieldOperand(ecx, Map::kInstanceSizeOffset));
4250 __ lea(ebx, Operand(ebx, times_pointer_size, 0));
4251 __ Allocate(ebx, eax, edi, no_reg, &allocate, NO_ALLOCATION_FLAGS);
4252 __ bind(&done_allocate);
4253
4254 // Initialize the JSObject fields.
Ben Murdochc5610432016-08-08 18:44:38 +01004255 __ mov(FieldOperand(eax, JSObject::kMapOffset), ecx);
4256 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004257 masm->isolate()->factory()->empty_fixed_array());
Ben Murdochc5610432016-08-08 18:44:38 +01004258 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004259 masm->isolate()->factory()->empty_fixed_array());
4260 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
Ben Murdochc5610432016-08-08 18:44:38 +01004261 __ lea(ebx, FieldOperand(eax, JSObject::kHeaderSize));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004262
4263 // ----------- S t a t e -------------
Ben Murdochc5610432016-08-08 18:44:38 +01004264 // -- eax : result (tagged)
Ben Murdoch097c5b22016-05-18 11:27:45 +01004265 // -- ebx : result fields (untagged)
4266 // -- edi : result end (untagged)
4267 // -- ecx : initial map
4268 // -- esi : context
4269 // -- esp[0] : return address
4270 // -----------------------------------
4271
4272 // Perform in-object slack tracking if requested.
4273 Label slack_tracking;
4274 STATIC_ASSERT(Map::kNoSlackTracking == 0);
4275 __ test(FieldOperand(ecx, Map::kBitField3Offset),
4276 Immediate(Map::ConstructionCounter::kMask));
4277 __ j(not_zero, &slack_tracking, Label::kNear);
4278 {
4279 // Initialize all in-object fields with undefined.
4280 __ LoadRoot(edx, Heap::kUndefinedValueRootIndex);
4281 __ InitializeFieldsWithFiller(ebx, edi, edx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004282 __ Ret();
4283 }
4284 __ bind(&slack_tracking);
4285 {
4286 // Decrease generous allocation count.
4287 STATIC_ASSERT(Map::ConstructionCounter::kNext == 32);
4288 __ sub(FieldOperand(ecx, Map::kBitField3Offset),
4289 Immediate(1 << Map::ConstructionCounter::kShift));
4290
4291 // Initialize the in-object fields with undefined.
4292 __ movzx_b(edx, FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset));
4293 __ neg(edx);
4294 __ lea(edx, Operand(edi, edx, times_pointer_size, 0));
4295 __ LoadRoot(edi, Heap::kUndefinedValueRootIndex);
4296 __ InitializeFieldsWithFiller(ebx, edx, edi);
4297
4298 // Initialize the remaining (reserved) fields with one pointer filler map.
4299 __ movzx_b(edx, FieldOperand(ecx, Map::kUnusedPropertyFieldsOffset));
4300 __ lea(edx, Operand(ebx, edx, times_pointer_size, 0));
4301 __ LoadRoot(edi, Heap::kOnePointerFillerMapRootIndex);
4302 __ InitializeFieldsWithFiller(ebx, edx, edi);
4303
Ben Murdoch097c5b22016-05-18 11:27:45 +01004304 // Check if we can finalize the instance size.
4305 Label finalize;
4306 STATIC_ASSERT(Map::kSlackTrackingCounterEnd == 1);
4307 __ test(FieldOperand(ecx, Map::kBitField3Offset),
4308 Immediate(Map::ConstructionCounter::kMask));
4309 __ j(zero, &finalize, Label::kNear);
4310 __ Ret();
4311
4312 // Finalize the instance size.
4313 __ bind(&finalize);
4314 {
4315 FrameScope scope(masm, StackFrame::INTERNAL);
4316 __ Push(eax);
4317 __ Push(ecx);
4318 __ CallRuntime(Runtime::kFinalizeInstanceSize);
4319 __ Pop(eax);
4320 }
4321 __ Ret();
4322 }
4323
4324 // Fall back to %AllocateInNewSpace.
4325 __ bind(&allocate);
4326 {
4327 FrameScope scope(masm, StackFrame::INTERNAL);
4328 __ SmiTag(ebx);
4329 __ Push(ecx);
4330 __ Push(ebx);
4331 __ CallRuntime(Runtime::kAllocateInNewSpace);
4332 __ Pop(ecx);
4333 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01004334 __ movzx_b(ebx, FieldOperand(ecx, Map::kInstanceSizeOffset));
4335 __ lea(edi, Operand(eax, ebx, times_pointer_size, 0));
Ben Murdochc5610432016-08-08 18:44:38 +01004336 STATIC_ASSERT(kHeapObjectTag == 1);
4337 __ dec(edi);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004338 __ jmp(&done_allocate);
4339
4340 // Fall back to %NewObject.
4341 __ bind(&new_object);
4342 __ PopReturnAddressTo(ecx);
4343 __ Push(edi);
4344 __ Push(edx);
4345 __ PushReturnAddressFrom(ecx);
4346 __ TailCallRuntime(Runtime::kNewObject);
4347}
4348
4349void FastNewRestParameterStub::Generate(MacroAssembler* masm) {
4350 // ----------- S t a t e -------------
4351 // -- edi : function
4352 // -- esi : context
4353 // -- ebp : frame pointer
4354 // -- esp[0] : return address
4355 // -----------------------------------
4356 __ AssertFunction(edi);
4357
Ben Murdochc5610432016-08-08 18:44:38 +01004358 // Make edx point to the JavaScript frame.
4359 __ mov(edx, ebp);
4360 if (skip_stub_frame()) {
4361 // For Ignition we need to skip the handler/stub frame to reach the
4362 // JavaScript frame for the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004363 __ mov(edx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004364 }
4365 if (FLAG_debug_code) {
4366 Label ok;
Ben Murdochda12d292016-06-02 14:46:10 +01004367 __ cmp(edi, Operand(edx, StandardFrameConstants::kFunctionOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004368 __ j(equal, &ok);
4369 __ Abort(kInvalidFrameForFastNewRestArgumentsStub);
4370 __ bind(&ok);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004371 }
4372
4373 // Check if we have rest parameters (only possible if we have an
4374 // arguments adaptor frame below the function frame).
4375 Label no_rest_parameters;
4376 __ mov(ebx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004377 __ cmp(Operand(ebx, CommonFrameConstants::kContextOrFrameTypeOffset),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004378 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4379 __ j(not_equal, &no_rest_parameters, Label::kNear);
4380
4381 // Check if the arguments adaptor frame contains more arguments than
4382 // specified by the function's internal formal parameter count.
4383 Label rest_parameters;
4384 __ mov(ecx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4385 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4386 __ sub(eax,
4387 FieldOperand(ecx, SharedFunctionInfo::kFormalParameterCountOffset));
4388 __ j(greater, &rest_parameters);
4389
4390 // Return an empty rest parameter array.
4391 __ bind(&no_rest_parameters);
4392 {
4393 // ----------- S t a t e -------------
4394 // -- esi : context
4395 // -- esp[0] : return address
4396 // -----------------------------------
4397
4398 // Allocate an empty rest parameter array.
4399 Label allocate, done_allocate;
Ben Murdochc5610432016-08-08 18:44:38 +01004400 __ Allocate(JSArray::kSize, eax, edx, ecx, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004401 __ bind(&done_allocate);
4402
4403 // Setup the rest parameter array in rax.
4404 __ LoadGlobalFunction(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, ecx);
4405 __ mov(FieldOperand(eax, JSArray::kMapOffset), ecx);
4406 __ mov(ecx, isolate()->factory()->empty_fixed_array());
4407 __ mov(FieldOperand(eax, JSArray::kPropertiesOffset), ecx);
4408 __ mov(FieldOperand(eax, JSArray::kElementsOffset), ecx);
4409 __ mov(FieldOperand(eax, JSArray::kLengthOffset),
4410 Immediate(Smi::FromInt(0)));
4411 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4412 __ Ret();
4413
4414 // Fall back to %AllocateInNewSpace.
4415 __ bind(&allocate);
4416 {
4417 FrameScope scope(masm, StackFrame::INTERNAL);
4418 __ Push(Smi::FromInt(JSArray::kSize));
4419 __ CallRuntime(Runtime::kAllocateInNewSpace);
4420 }
4421 __ jmp(&done_allocate);
4422 }
4423
4424 __ bind(&rest_parameters);
4425 {
4426 // Compute the pointer to the first rest parameter (skippping the receiver).
4427 __ lea(ebx,
4428 Operand(ebx, eax, times_half_pointer_size,
4429 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4430
4431 // ----------- S t a t e -------------
4432 // -- esi : context
4433 // -- eax : number of rest parameters (tagged)
4434 // -- ebx : pointer to first rest parameters
4435 // -- esp[0] : return address
4436 // -----------------------------------
4437
4438 // Allocate space for the rest parameter array plus the backing store.
4439 Label allocate, done_allocate;
4440 __ lea(ecx, Operand(eax, times_half_pointer_size,
4441 JSArray::kSize + FixedArray::kHeaderSize));
Ben Murdochc5610432016-08-08 18:44:38 +01004442 __ Allocate(ecx, edx, edi, no_reg, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004443 __ bind(&done_allocate);
4444
4445 // Setup the elements array in edx.
4446 __ mov(FieldOperand(edx, FixedArray::kMapOffset),
4447 isolate()->factory()->fixed_array_map());
4448 __ mov(FieldOperand(edx, FixedArray::kLengthOffset), eax);
4449 {
4450 Label loop, done_loop;
4451 __ Move(ecx, Smi::FromInt(0));
4452 __ bind(&loop);
4453 __ cmp(ecx, eax);
4454 __ j(equal, &done_loop, Label::kNear);
4455 __ mov(edi, Operand(ebx, 0 * kPointerSize));
4456 __ mov(FieldOperand(edx, ecx, times_half_pointer_size,
4457 FixedArray::kHeaderSize),
4458 edi);
4459 __ sub(ebx, Immediate(1 * kPointerSize));
4460 __ add(ecx, Immediate(Smi::FromInt(1)));
4461 __ jmp(&loop);
4462 __ bind(&done_loop);
4463 }
4464
4465 // Setup the rest parameter array in edi.
4466 __ lea(edi,
4467 Operand(edx, eax, times_half_pointer_size, FixedArray::kHeaderSize));
4468 __ LoadGlobalFunction(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, ecx);
4469 __ mov(FieldOperand(edi, JSArray::kMapOffset), ecx);
4470 __ mov(FieldOperand(edi, JSArray::kPropertiesOffset),
4471 isolate()->factory()->empty_fixed_array());
4472 __ mov(FieldOperand(edi, JSArray::kElementsOffset), edx);
4473 __ mov(FieldOperand(edi, JSArray::kLengthOffset), eax);
4474 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4475 __ mov(eax, edi);
4476 __ Ret();
4477
4478 // Fall back to %AllocateInNewSpace.
4479 __ bind(&allocate);
4480 {
4481 FrameScope scope(masm, StackFrame::INTERNAL);
4482 __ SmiTag(ecx);
4483 __ Push(eax);
4484 __ Push(ebx);
4485 __ Push(ecx);
4486 __ CallRuntime(Runtime::kAllocateInNewSpace);
4487 __ mov(edx, eax);
4488 __ Pop(ebx);
4489 __ Pop(eax);
4490 }
4491 __ jmp(&done_allocate);
4492 }
4493}
4494
4495void FastNewSloppyArgumentsStub::Generate(MacroAssembler* masm) {
4496 // ----------- S t a t e -------------
4497 // -- edi : function
4498 // -- esi : context
4499 // -- ebp : frame pointer
4500 // -- esp[0] : return address
4501 // -----------------------------------
4502 __ AssertFunction(edi);
4503
Ben Murdochc5610432016-08-08 18:44:38 +01004504 // Make ecx point to the JavaScript frame.
4505 __ mov(ecx, ebp);
4506 if (skip_stub_frame()) {
4507 // For Ignition we need to skip the handler/stub frame to reach the
4508 // JavaScript frame for the function.
4509 __ mov(ecx, Operand(ecx, StandardFrameConstants::kCallerFPOffset));
4510 }
4511 if (FLAG_debug_code) {
4512 Label ok;
4513 __ cmp(edi, Operand(ecx, StandardFrameConstants::kFunctionOffset));
4514 __ j(equal, &ok);
4515 __ Abort(kInvalidFrameForFastNewSloppyArgumentsStub);
4516 __ bind(&ok);
4517 }
4518
Ben Murdoch097c5b22016-05-18 11:27:45 +01004519 // TODO(bmeurer): Cleanup to match the FastNewStrictArgumentsStub.
Ben Murdochc5610432016-08-08 18:44:38 +01004520 __ mov(ebx, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4521 __ mov(ebx,
4522 FieldOperand(ebx, SharedFunctionInfo::kFormalParameterCountOffset));
4523 __ lea(edx, Operand(ecx, ebx, times_half_pointer_size,
Ben Murdoch097c5b22016-05-18 11:27:45 +01004524 StandardFrameConstants::kCallerSPOffset));
4525
Ben Murdochc5610432016-08-08 18:44:38 +01004526 // ebx : number of parameters (tagged)
Ben Murdoch097c5b22016-05-18 11:27:45 +01004527 // edx : parameters pointer
4528 // edi : function
Ben Murdochc5610432016-08-08 18:44:38 +01004529 // ecx : JavaScript frame pointer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004530 // esp[0] : return address
4531
4532 // Check if the calling frame is an arguments adaptor frame.
4533 Label adaptor_frame, try_allocate, runtime;
Ben Murdochc5610432016-08-08 18:44:38 +01004534 __ mov(eax, Operand(ecx, StandardFrameConstants::kCallerFPOffset));
4535 __ mov(eax, Operand(eax, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004536 __ cmp(eax, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4537 __ j(equal, &adaptor_frame, Label::kNear);
4538
4539 // No adaptor, parameter count = argument count.
Ben Murdochc5610432016-08-08 18:44:38 +01004540 __ mov(ecx, ebx);
4541 __ push(ebx);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004542 __ jmp(&try_allocate, Label::kNear);
4543
4544 // We have an adaptor frame. Patch the parameters pointer.
4545 __ bind(&adaptor_frame);
Ben Murdochc5610432016-08-08 18:44:38 +01004546 __ push(ebx);
4547 __ mov(edx, Operand(ecx, StandardFrameConstants::kCallerFPOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004548 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4549 __ lea(edx,
4550 Operand(edx, ecx, times_2, StandardFrameConstants::kCallerSPOffset));
4551
4552 // ebx = parameter count (tagged)
4553 // ecx = argument count (smi-tagged)
4554 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
4555 __ cmp(ebx, ecx);
4556 __ j(less_equal, &try_allocate, Label::kNear);
4557 __ mov(ebx, ecx);
4558
4559 // Save mapped parameter count and function.
4560 __ bind(&try_allocate);
4561 __ push(edi);
4562 __ push(ebx);
4563
4564 // Compute the sizes of backing store, parameter map, and arguments object.
4565 // 1. Parameter map, has 2 extra words containing context and backing store.
4566 const int kParameterMapHeaderSize =
4567 FixedArray::kHeaderSize + 2 * kPointerSize;
4568 Label no_parameter_map;
4569 __ test(ebx, ebx);
4570 __ j(zero, &no_parameter_map, Label::kNear);
4571 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
4572 __ bind(&no_parameter_map);
4573
4574 // 2. Backing store.
4575 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
4576
4577 // 3. Arguments object.
4578 __ add(ebx, Immediate(JSSloppyArgumentsObject::kSize));
4579
4580 // Do the allocation of all three objects in one go.
Ben Murdochc5610432016-08-08 18:44:38 +01004581 __ Allocate(ebx, eax, edi, no_reg, &runtime, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004582
4583 // eax = address of new object(s) (tagged)
4584 // ecx = argument count (smi-tagged)
4585 // esp[0] = mapped parameter count (tagged)
4586 // esp[4] = function
4587 // esp[8] = parameter count (tagged)
4588 // Get the arguments map from the current native context into edi.
4589 Label has_mapped_parameters, instantiate;
4590 __ mov(edi, NativeContextOperand());
4591 __ mov(ebx, Operand(esp, 0 * kPointerSize));
4592 __ test(ebx, ebx);
4593 __ j(not_zero, &has_mapped_parameters, Label::kNear);
4594 __ mov(
4595 edi,
4596 Operand(edi, Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX)));
4597 __ jmp(&instantiate, Label::kNear);
4598
4599 __ bind(&has_mapped_parameters);
4600 __ mov(edi, Operand(edi, Context::SlotOffset(
4601 Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX)));
4602 __ bind(&instantiate);
4603
4604 // eax = address of new object (tagged)
4605 // ebx = mapped parameter count (tagged)
4606 // ecx = argument count (smi-tagged)
4607 // edi = address of arguments map (tagged)
4608 // esp[0] = mapped parameter count (tagged)
4609 // esp[4] = function
4610 // esp[8] = parameter count (tagged)
4611 // Copy the JS object part.
4612 __ mov(FieldOperand(eax, JSObject::kMapOffset), edi);
4613 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset),
4614 masm->isolate()->factory()->empty_fixed_array());
4615 __ mov(FieldOperand(eax, JSObject::kElementsOffset),
4616 masm->isolate()->factory()->empty_fixed_array());
4617
4618 // Set up the callee in-object property.
4619 STATIC_ASSERT(JSSloppyArgumentsObject::kCalleeIndex == 1);
4620 __ mov(edi, Operand(esp, 1 * kPointerSize));
4621 __ AssertNotSmi(edi);
4622 __ mov(FieldOperand(eax, JSSloppyArgumentsObject::kCalleeOffset), edi);
4623
4624 // Use the length (smi tagged) and set that as an in-object property too.
4625 __ AssertSmi(ecx);
4626 __ mov(FieldOperand(eax, JSSloppyArgumentsObject::kLengthOffset), ecx);
4627
4628 // Set up the elements pointer in the allocated arguments object.
4629 // If we allocated a parameter map, edi will point there, otherwise to the
4630 // backing store.
4631 __ lea(edi, Operand(eax, JSSloppyArgumentsObject::kSize));
4632 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
4633
4634 // eax = address of new object (tagged)
4635 // ebx = mapped parameter count (tagged)
4636 // ecx = argument count (tagged)
4637 // edx = address of receiver argument
4638 // edi = address of parameter map or backing store (tagged)
4639 // esp[0] = mapped parameter count (tagged)
4640 // esp[4] = function
4641 // esp[8] = parameter count (tagged)
4642 // Free two registers.
4643 __ push(edx);
4644 __ push(eax);
4645
4646 // Initialize parameter map. If there are no mapped arguments, we're done.
4647 Label skip_parameter_map;
4648 __ test(ebx, ebx);
4649 __ j(zero, &skip_parameter_map);
4650
4651 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
4652 Immediate(isolate()->factory()->sloppy_arguments_elements_map()));
4653 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
4654 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
4655 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
4656 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
4657 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
4658
4659 // Copy the parameter slots and the holes in the arguments.
4660 // We need to fill in mapped_parameter_count slots. They index the context,
4661 // where parameters are stored in reverse order, at
4662 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
4663 // The mapped parameter thus need to get indices
4664 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
4665 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
4666 // We loop from right to left.
4667 Label parameters_loop, parameters_test;
4668 __ push(ecx);
4669 __ mov(eax, Operand(esp, 3 * kPointerSize));
4670 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
4671 __ add(ebx, Operand(esp, 5 * kPointerSize));
4672 __ sub(ebx, eax);
4673 __ mov(ecx, isolate()->factory()->the_hole_value());
4674 __ mov(edx, edi);
4675 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
4676 // eax = loop variable (tagged)
4677 // ebx = mapping index (tagged)
4678 // ecx = the hole value
4679 // edx = address of parameter map (tagged)
4680 // edi = address of backing store (tagged)
4681 // esp[0] = argument count (tagged)
4682 // esp[4] = address of new object (tagged)
4683 // esp[8] = address of receiver argument
4684 // esp[12] = mapped parameter count (tagged)
4685 // esp[16] = function
4686 // esp[20] = parameter count (tagged)
4687 __ jmp(&parameters_test, Label::kNear);
4688
4689 __ bind(&parameters_loop);
4690 __ sub(eax, Immediate(Smi::FromInt(1)));
4691 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
4692 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
4693 __ add(ebx, Immediate(Smi::FromInt(1)));
4694 __ bind(&parameters_test);
4695 __ test(eax, eax);
4696 __ j(not_zero, &parameters_loop, Label::kNear);
4697 __ pop(ecx);
4698
4699 __ bind(&skip_parameter_map);
4700
4701 // ecx = argument count (tagged)
4702 // edi = address of backing store (tagged)
4703 // esp[0] = address of new object (tagged)
4704 // esp[4] = address of receiver argument
4705 // esp[8] = mapped parameter count (tagged)
4706 // esp[12] = function
4707 // esp[16] = parameter count (tagged)
4708 // Copy arguments header and remaining slots (if there are any).
4709 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
4710 Immediate(isolate()->factory()->fixed_array_map()));
4711 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
4712
4713 Label arguments_loop, arguments_test;
4714 __ mov(ebx, Operand(esp, 2 * kPointerSize));
4715 __ mov(edx, Operand(esp, 1 * kPointerSize));
4716 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
4717 __ sub(edx, ebx);
4718 __ jmp(&arguments_test, Label::kNear);
4719
4720 __ bind(&arguments_loop);
4721 __ sub(edx, Immediate(kPointerSize));
4722 __ mov(eax, Operand(edx, 0));
4723 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
4724 __ add(ebx, Immediate(Smi::FromInt(1)));
4725
4726 __ bind(&arguments_test);
4727 __ cmp(ebx, ecx);
4728 __ j(less, &arguments_loop, Label::kNear);
4729
4730 // Restore.
4731 __ pop(eax); // Address of arguments object.
4732 __ Drop(4);
4733
4734 // Return.
4735 __ ret(0);
4736
4737 // Do the runtime call to allocate the arguments object.
4738 __ bind(&runtime);
4739 __ pop(eax); // Remove saved mapped parameter count.
4740 __ pop(edi); // Pop saved function.
4741 __ pop(eax); // Remove saved parameter count.
4742 __ pop(eax); // Pop return address.
4743 __ push(edi); // Push function.
4744 __ push(edx); // Push parameters pointer.
4745 __ push(ecx); // Push parameter count.
4746 __ push(eax); // Push return address.
4747 __ TailCallRuntime(Runtime::kNewSloppyArguments);
4748}
4749
4750void FastNewStrictArgumentsStub::Generate(MacroAssembler* masm) {
4751 // ----------- S t a t e -------------
4752 // -- edi : function
4753 // -- esi : context
4754 // -- ebp : frame pointer
4755 // -- esp[0] : return address
4756 // -----------------------------------
4757 __ AssertFunction(edi);
4758
Ben Murdochc5610432016-08-08 18:44:38 +01004759 // Make edx point to the JavaScript frame.
4760 __ mov(edx, ebp);
4761 if (skip_stub_frame()) {
4762 // For Ignition we need to skip the handler/stub frame to reach the
4763 // JavaScript frame for the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004764 __ mov(edx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004765 }
4766 if (FLAG_debug_code) {
4767 Label ok;
Ben Murdochda12d292016-06-02 14:46:10 +01004768 __ cmp(edi, Operand(edx, StandardFrameConstants::kFunctionOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004769 __ j(equal, &ok);
4770 __ Abort(kInvalidFrameForFastNewStrictArgumentsStub);
4771 __ bind(&ok);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004772 }
4773
4774 // Check if we have an arguments adaptor frame below the function frame.
4775 Label arguments_adaptor, arguments_done;
4776 __ mov(ebx, Operand(edx, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004777 __ cmp(Operand(ebx, CommonFrameConstants::kContextOrFrameTypeOffset),
Ben Murdoch097c5b22016-05-18 11:27:45 +01004778 Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4779 __ j(equal, &arguments_adaptor, Label::kNear);
4780 {
4781 __ mov(eax, FieldOperand(edi, JSFunction::kSharedFunctionInfoOffset));
4782 __ mov(eax,
4783 FieldOperand(eax, SharedFunctionInfo::kFormalParameterCountOffset));
4784 __ lea(ebx,
4785 Operand(edx, eax, times_half_pointer_size,
4786 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4787 }
4788 __ jmp(&arguments_done, Label::kNear);
4789 __ bind(&arguments_adaptor);
4790 {
4791 __ mov(eax, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
4792 __ lea(ebx,
4793 Operand(ebx, eax, times_half_pointer_size,
4794 StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4795 }
4796 __ bind(&arguments_done);
4797
4798 // ----------- S t a t e -------------
4799 // -- eax : number of arguments (tagged)
4800 // -- ebx : pointer to the first argument
4801 // -- esi : context
4802 // -- esp[0] : return address
4803 // -----------------------------------
4804
4805 // Allocate space for the strict arguments object plus the backing store.
4806 Label allocate, done_allocate;
4807 __ lea(ecx,
4808 Operand(eax, times_half_pointer_size,
4809 JSStrictArgumentsObject::kSize + FixedArray::kHeaderSize));
Ben Murdochc5610432016-08-08 18:44:38 +01004810 __ Allocate(ecx, edx, edi, no_reg, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004811 __ bind(&done_allocate);
4812
4813 // Setup the elements array in edx.
4814 __ mov(FieldOperand(edx, FixedArray::kMapOffset),
4815 isolate()->factory()->fixed_array_map());
4816 __ mov(FieldOperand(edx, FixedArray::kLengthOffset), eax);
4817 {
4818 Label loop, done_loop;
4819 __ Move(ecx, Smi::FromInt(0));
4820 __ bind(&loop);
4821 __ cmp(ecx, eax);
4822 __ j(equal, &done_loop, Label::kNear);
4823 __ mov(edi, Operand(ebx, 0 * kPointerSize));
4824 __ mov(FieldOperand(edx, ecx, times_half_pointer_size,
4825 FixedArray::kHeaderSize),
4826 edi);
4827 __ sub(ebx, Immediate(1 * kPointerSize));
4828 __ add(ecx, Immediate(Smi::FromInt(1)));
4829 __ jmp(&loop);
4830 __ bind(&done_loop);
4831 }
4832
4833 // Setup the rest parameter array in edi.
4834 __ lea(edi,
4835 Operand(edx, eax, times_half_pointer_size, FixedArray::kHeaderSize));
4836 __ LoadGlobalFunction(Context::STRICT_ARGUMENTS_MAP_INDEX, ecx);
4837 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kMapOffset), ecx);
4838 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kPropertiesOffset),
4839 isolate()->factory()->empty_fixed_array());
4840 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kElementsOffset), edx);
4841 __ mov(FieldOperand(edi, JSStrictArgumentsObject::kLengthOffset), eax);
4842 STATIC_ASSERT(JSStrictArgumentsObject::kSize == 4 * kPointerSize);
4843 __ mov(eax, edi);
4844 __ Ret();
4845
4846 // Fall back to %AllocateInNewSpace.
4847 __ bind(&allocate);
4848 {
4849 FrameScope scope(masm, StackFrame::INTERNAL);
4850 __ SmiTag(ecx);
4851 __ Push(eax);
4852 __ Push(ebx);
4853 __ Push(ecx);
4854 __ CallRuntime(Runtime::kAllocateInNewSpace);
4855 __ mov(edx, eax);
4856 __ Pop(ebx);
4857 __ Pop(eax);
4858 }
4859 __ jmp(&done_allocate);
4860}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004861
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004862void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
4863 Register context_reg = esi;
4864 Register slot_reg = ebx;
4865 Register result_reg = eax;
4866 Label slow_case;
4867
4868 // Go up context chain to the script context.
4869 for (int i = 0; i < depth(); ++i) {
4870 __ mov(result_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
4871 context_reg = result_reg;
4872 }
4873
4874 // Load the PropertyCell value at the specified slot.
4875 __ mov(result_reg, ContextOperand(context_reg, slot_reg));
4876 __ mov(result_reg, FieldOperand(result_reg, PropertyCell::kValueOffset));
4877
4878 // Check that value is not the_hole.
4879 __ CompareRoot(result_reg, Heap::kTheHoleValueRootIndex);
4880 __ j(equal, &slow_case, Label::kNear);
4881 __ Ret();
4882
4883 // Fallback to the runtime.
4884 __ bind(&slow_case);
4885 __ SmiTag(slot_reg);
4886 __ Pop(result_reg); // Pop return address.
4887 __ Push(slot_reg);
4888 __ Push(result_reg); // Push return address.
4889 __ TailCallRuntime(Runtime::kLoadGlobalViaContext);
4890}
4891
4892
4893void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
4894 Register context_reg = esi;
4895 Register slot_reg = ebx;
4896 Register value_reg = eax;
4897 Register cell_reg = edi;
4898 Register cell_details_reg = edx;
4899 Register cell_value_reg = ecx;
4900 Label fast_heapobject_case, fast_smi_case, slow_case;
4901
4902 if (FLAG_debug_code) {
4903 __ CompareRoot(value_reg, Heap::kTheHoleValueRootIndex);
4904 __ Check(not_equal, kUnexpectedValue);
4905 }
4906
4907 // Go up context chain to the script context.
4908 for (int i = 0; i < depth(); ++i) {
4909 __ mov(cell_reg, ContextOperand(context_reg, Context::PREVIOUS_INDEX));
4910 context_reg = cell_reg;
4911 }
4912
4913 // Load the PropertyCell at the specified slot.
4914 __ mov(cell_reg, ContextOperand(context_reg, slot_reg));
4915
4916 // Load PropertyDetails for the cell (actually only the cell_type and kind).
4917 __ mov(cell_details_reg,
4918 FieldOperand(cell_reg, PropertyCell::kDetailsOffset));
4919 __ SmiUntag(cell_details_reg);
4920 __ and_(cell_details_reg,
4921 Immediate(PropertyDetails::PropertyCellTypeField::kMask |
4922 PropertyDetails::KindField::kMask |
4923 PropertyDetails::kAttributesReadOnlyMask));
4924
4925 // Check if PropertyCell holds mutable data.
4926 Label not_mutable_data;
4927 __ cmp(cell_details_reg,
4928 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4929 PropertyCellType::kMutable) |
4930 PropertyDetails::KindField::encode(kData)));
4931 __ j(not_equal, &not_mutable_data);
4932 __ JumpIfSmi(value_reg, &fast_smi_case);
4933 __ bind(&fast_heapobject_case);
4934 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
4935 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
4936 cell_details_reg, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
4937 OMIT_SMI_CHECK);
4938 // RecordWriteField clobbers the value register, so we need to reload.
4939 __ mov(value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
4940 __ Ret();
4941 __ bind(&not_mutable_data);
4942
4943 // Check if PropertyCell value matches the new value (relevant for Constant,
4944 // ConstantType and Undefined cells).
4945 Label not_same_value;
4946 __ mov(cell_value_reg, FieldOperand(cell_reg, PropertyCell::kValueOffset));
4947 __ cmp(cell_value_reg, value_reg);
4948 __ j(not_equal, &not_same_value,
4949 FLAG_debug_code ? Label::kFar : Label::kNear);
4950 // Make sure the PropertyCell is not marked READ_ONLY.
4951 __ test(cell_details_reg,
4952 Immediate(PropertyDetails::kAttributesReadOnlyMask));
4953 __ j(not_zero, &slow_case);
4954 if (FLAG_debug_code) {
4955 Label done;
4956 // This can only be true for Constant, ConstantType and Undefined cells,
4957 // because we never store the_hole via this stub.
4958 __ cmp(cell_details_reg,
4959 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4960 PropertyCellType::kConstant) |
4961 PropertyDetails::KindField::encode(kData)));
4962 __ j(equal, &done);
4963 __ cmp(cell_details_reg,
4964 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4965 PropertyCellType::kConstantType) |
4966 PropertyDetails::KindField::encode(kData)));
4967 __ j(equal, &done);
4968 __ cmp(cell_details_reg,
4969 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4970 PropertyCellType::kUndefined) |
4971 PropertyDetails::KindField::encode(kData)));
4972 __ Check(equal, kUnexpectedValue);
4973 __ bind(&done);
4974 }
4975 __ Ret();
4976 __ bind(&not_same_value);
4977
4978 // Check if PropertyCell contains data with constant type (and is not
4979 // READ_ONLY).
4980 __ cmp(cell_details_reg,
4981 Immediate(PropertyDetails::PropertyCellTypeField::encode(
4982 PropertyCellType::kConstantType) |
4983 PropertyDetails::KindField::encode(kData)));
4984 __ j(not_equal, &slow_case, Label::kNear);
4985
4986 // Now either both old and new values must be SMIs or both must be heap
4987 // objects with same map.
4988 Label value_is_heap_object;
4989 __ JumpIfNotSmi(value_reg, &value_is_heap_object, Label::kNear);
4990 __ JumpIfNotSmi(cell_value_reg, &slow_case, Label::kNear);
4991 // Old and new values are SMIs, no need for a write barrier here.
4992 __ bind(&fast_smi_case);
4993 __ mov(FieldOperand(cell_reg, PropertyCell::kValueOffset), value_reg);
4994 __ Ret();
4995 __ bind(&value_is_heap_object);
4996 __ JumpIfSmi(cell_value_reg, &slow_case, Label::kNear);
4997 Register cell_value_map_reg = cell_value_reg;
4998 __ mov(cell_value_map_reg,
4999 FieldOperand(cell_value_reg, HeapObject::kMapOffset));
5000 __ cmp(cell_value_map_reg, FieldOperand(value_reg, HeapObject::kMapOffset));
5001 __ j(equal, &fast_heapobject_case);
5002
5003 // Fallback to the runtime.
5004 __ bind(&slow_case);
5005 __ SmiTag(slot_reg);
5006 __ Pop(cell_reg); // Pop return address.
5007 __ Push(slot_reg);
5008 __ Push(value_reg);
5009 __ Push(cell_reg); // Push return address.
5010 __ TailCallRuntime(is_strict(language_mode())
5011 ? Runtime::kStoreGlobalViaContext_Strict
5012 : Runtime::kStoreGlobalViaContext_Sloppy);
5013}
5014
5015
5016// Generates an Operand for saving parameters after PrepareCallApiFunction.
5017static Operand ApiParameterOperand(int index) {
5018 return Operand(esp, index * kPointerSize);
5019}
5020
5021
5022// Prepares stack to put arguments (aligns and so on). Reserves
5023// space for return value if needed (assumes the return value is a handle).
5024// Arguments must be stored in ApiParameterOperand(0), ApiParameterOperand(1)
5025// etc. Saves context (esi). If space was reserved for return value then
5026// stores the pointer to the reserved slot into esi.
5027static void PrepareCallApiFunction(MacroAssembler* masm, int argc) {
5028 __ EnterApiExitFrame(argc);
5029 if (__ emit_debug_code()) {
5030 __ mov(esi, Immediate(bit_cast<int32_t>(kZapValue)));
5031 }
5032}
5033
5034
5035// Calls an API function. Allocates HandleScope, extracts returned value
5036// from handle and propagates exceptions. Clobbers ebx, edi and
5037// caller-save registers. Restores context. On return removes
5038// stack_space * kPointerSize (GCed).
5039static void CallApiFunctionAndReturn(MacroAssembler* masm,
5040 Register function_address,
5041 ExternalReference thunk_ref,
5042 Operand thunk_last_arg, int stack_space,
5043 Operand* stack_space_operand,
5044 Operand return_value_operand,
5045 Operand* context_restore_operand) {
5046 Isolate* isolate = masm->isolate();
5047
5048 ExternalReference next_address =
5049 ExternalReference::handle_scope_next_address(isolate);
5050 ExternalReference limit_address =
5051 ExternalReference::handle_scope_limit_address(isolate);
5052 ExternalReference level_address =
5053 ExternalReference::handle_scope_level_address(isolate);
5054
5055 DCHECK(edx.is(function_address));
5056 // Allocate HandleScope in callee-save registers.
5057 __ mov(ebx, Operand::StaticVariable(next_address));
5058 __ mov(edi, Operand::StaticVariable(limit_address));
5059 __ add(Operand::StaticVariable(level_address), Immediate(1));
5060
5061 if (FLAG_log_timer_events) {
5062 FrameScope frame(masm, StackFrame::MANUAL);
5063 __ PushSafepointRegisters();
5064 __ PrepareCallCFunction(1, eax);
5065 __ mov(Operand(esp, 0),
5066 Immediate(ExternalReference::isolate_address(isolate)));
5067 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5068 1);
5069 __ PopSafepointRegisters();
5070 }
5071
5072
5073 Label profiler_disabled;
5074 Label end_profiler_check;
5075 __ mov(eax, Immediate(ExternalReference::is_profiling_address(isolate)));
Ben Murdochda12d292016-06-02 14:46:10 +01005076 __ cmpb(Operand(eax, 0), Immediate(0));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005077 __ j(zero, &profiler_disabled);
5078
5079 // Additional parameter is the address of the actual getter function.
5080 __ mov(thunk_last_arg, function_address);
5081 // Call the api function.
5082 __ mov(eax, Immediate(thunk_ref));
5083 __ call(eax);
5084 __ jmp(&end_profiler_check);
5085
5086 __ bind(&profiler_disabled);
5087 // Call the api function.
5088 __ call(function_address);
5089 __ bind(&end_profiler_check);
5090
5091 if (FLAG_log_timer_events) {
5092 FrameScope frame(masm, StackFrame::MANUAL);
5093 __ PushSafepointRegisters();
5094 __ PrepareCallCFunction(1, eax);
5095 __ mov(Operand(esp, 0),
5096 Immediate(ExternalReference::isolate_address(isolate)));
5097 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5098 1);
5099 __ PopSafepointRegisters();
5100 }
5101
5102 Label prologue;
5103 // Load the value from ReturnValue
5104 __ mov(eax, return_value_operand);
5105
5106 Label promote_scheduled_exception;
5107 Label delete_allocated_handles;
5108 Label leave_exit_frame;
5109
5110 __ bind(&prologue);
5111 // No more valid handles (the result handle was the last one). Restore
5112 // previous handle scope.
5113 __ mov(Operand::StaticVariable(next_address), ebx);
5114 __ sub(Operand::StaticVariable(level_address), Immediate(1));
5115 __ Assert(above_equal, kInvalidHandleScopeLevel);
5116 __ cmp(edi, Operand::StaticVariable(limit_address));
5117 __ j(not_equal, &delete_allocated_handles);
5118
5119 // Leave the API exit frame.
5120 __ bind(&leave_exit_frame);
5121 bool restore_context = context_restore_operand != NULL;
5122 if (restore_context) {
5123 __ mov(esi, *context_restore_operand);
5124 }
5125 if (stack_space_operand != nullptr) {
5126 __ mov(ebx, *stack_space_operand);
5127 }
5128 __ LeaveApiExitFrame(!restore_context);
5129
5130 // Check if the function scheduled an exception.
5131 ExternalReference scheduled_exception_address =
5132 ExternalReference::scheduled_exception_address(isolate);
5133 __ cmp(Operand::StaticVariable(scheduled_exception_address),
5134 Immediate(isolate->factory()->the_hole_value()));
5135 __ j(not_equal, &promote_scheduled_exception);
5136
5137#if DEBUG
5138 // Check if the function returned a valid JavaScript value.
5139 Label ok;
5140 Register return_value = eax;
5141 Register map = ecx;
5142
5143 __ JumpIfSmi(return_value, &ok, Label::kNear);
5144 __ mov(map, FieldOperand(return_value, HeapObject::kMapOffset));
5145
5146 __ CmpInstanceType(map, LAST_NAME_TYPE);
5147 __ j(below_equal, &ok, Label::kNear);
5148
5149 __ CmpInstanceType(map, FIRST_JS_RECEIVER_TYPE);
5150 __ j(above_equal, &ok, Label::kNear);
5151
5152 __ cmp(map, isolate->factory()->heap_number_map());
5153 __ j(equal, &ok, Label::kNear);
5154
5155 __ cmp(return_value, isolate->factory()->undefined_value());
5156 __ j(equal, &ok, Label::kNear);
5157
5158 __ cmp(return_value, isolate->factory()->true_value());
5159 __ j(equal, &ok, Label::kNear);
5160
5161 __ cmp(return_value, isolate->factory()->false_value());
5162 __ j(equal, &ok, Label::kNear);
5163
5164 __ cmp(return_value, isolate->factory()->null_value());
5165 __ j(equal, &ok, Label::kNear);
5166
5167 __ Abort(kAPICallReturnedInvalidObject);
5168
5169 __ bind(&ok);
5170#endif
5171
5172 if (stack_space_operand != nullptr) {
5173 DCHECK_EQ(0, stack_space);
5174 __ pop(ecx);
5175 __ add(esp, ebx);
5176 __ jmp(ecx);
5177 } else {
5178 __ ret(stack_space * kPointerSize);
5179 }
5180
5181 // Re-throw by promoting a scheduled exception.
5182 __ bind(&promote_scheduled_exception);
5183 __ TailCallRuntime(Runtime::kPromoteScheduledException);
5184
5185 // HandleScope limit has changed. Delete allocated extensions.
5186 ExternalReference delete_extensions =
5187 ExternalReference::delete_handle_scope_extensions(isolate);
5188 __ bind(&delete_allocated_handles);
5189 __ mov(Operand::StaticVariable(limit_address), edi);
5190 __ mov(edi, eax);
5191 __ mov(Operand(esp, 0),
5192 Immediate(ExternalReference::isolate_address(isolate)));
5193 __ mov(eax, Immediate(delete_extensions));
5194 __ call(eax);
5195 __ mov(eax, edi);
5196 __ jmp(&leave_exit_frame);
5197}
5198
Ben Murdochda12d292016-06-02 14:46:10 +01005199void CallApiCallbackStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005200 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005201 // -- edi : callee
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005202 // -- ebx : call_data
5203 // -- ecx : holder
5204 // -- edx : api_function_address
5205 // -- esi : context
5206 // --
5207 // -- esp[0] : return address
5208 // -- esp[4] : last argument
5209 // -- ...
5210 // -- esp[argc * 4] : first argument
5211 // -- esp[(argc + 1) * 4] : receiver
5212 // -----------------------------------
5213
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005214 Register callee = edi;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005215 Register call_data = ebx;
5216 Register holder = ecx;
5217 Register api_function_address = edx;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005218 Register context = esi;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005219 Register return_address = eax;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005220
5221 typedef FunctionCallbackArguments FCA;
5222
5223 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5224 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5225 STATIC_ASSERT(FCA::kDataIndex == 4);
5226 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5227 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5228 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5229 STATIC_ASSERT(FCA::kHolderIndex == 0);
Ben Murdochc5610432016-08-08 18:44:38 +01005230 STATIC_ASSERT(FCA::kNewTargetIndex == 7);
5231 STATIC_ASSERT(FCA::kArgsLength == 8);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005232
Ben Murdochda12d292016-06-02 14:46:10 +01005233 __ pop(return_address);
Ben Murdochc5610432016-08-08 18:44:38 +01005234
5235 // new target
5236 __ PushRoot(Heap::kUndefinedValueRootIndex);
5237
Ben Murdochda12d292016-06-02 14:46:10 +01005238 // context save.
5239 __ push(context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005240
5241 // callee
5242 __ push(callee);
5243
5244 // call data
5245 __ push(call_data);
5246
5247 Register scratch = call_data;
Ben Murdochda12d292016-06-02 14:46:10 +01005248 if (!call_data_undefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005249 // return value
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005250 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005251 // return value default
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005252 __ push(Immediate(masm->isolate()->factory()->undefined_value()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005253 } else {
5254 // return value
5255 __ push(scratch);
5256 // return value default
5257 __ push(scratch);
5258 }
5259 // isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005260 __ push(Immediate(reinterpret_cast<int>(masm->isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005261 // holder
5262 __ push(holder);
5263
5264 __ mov(scratch, esp);
5265
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005266 // push return address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005267 __ push(return_address);
5268
Ben Murdochda12d292016-06-02 14:46:10 +01005269 if (!is_lazy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005270 // load context from callee
5271 __ mov(context, FieldOperand(callee, JSFunction::kContextOffset));
5272 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005273
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005274 // API function gets reference to the v8::Arguments. If CPU profiler
5275 // is enabled wrapper function will be called and we need to pass
5276 // address of the callback as additional parameter, always allocate
5277 // space for it.
5278 const int kApiArgc = 1 + 1;
5279
5280 // Allocate the v8::Arguments structure in the arguments' space since
5281 // it's not controlled by GC.
Ben Murdochc5610432016-08-08 18:44:38 +01005282 const int kApiStackSpace = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005283
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005284 PrepareCallApiFunction(masm, kApiArgc + kApiStackSpace);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005285
5286 // FunctionCallbackInfo::implicit_args_.
5287 __ mov(ApiParameterOperand(2), scratch);
Ben Murdochda12d292016-06-02 14:46:10 +01005288 __ add(scratch, Immediate((argc() + FCA::kArgsLength - 1) * kPointerSize));
5289 // FunctionCallbackInfo::values_.
5290 __ mov(ApiParameterOperand(3), scratch);
5291 // FunctionCallbackInfo::length_.
5292 __ Move(ApiParameterOperand(4), Immediate(argc()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005293
5294 // v8::InvocationCallback's argument.
5295 __ lea(scratch, ApiParameterOperand(2));
5296 __ mov(ApiParameterOperand(0), scratch);
5297
5298 ExternalReference thunk_ref =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005299 ExternalReference::invoke_function_callback(masm->isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005300
5301 Operand context_restore_operand(ebp,
5302 (2 + FCA::kContextSaveIndex) * kPointerSize);
5303 // Stores return the first js argument
5304 int return_value_offset = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005305 if (is_store()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005306 return_value_offset = 2 + FCA::kArgsLength;
5307 } else {
5308 return_value_offset = 2 + FCA::kReturnValueOffset;
5309 }
5310 Operand return_value_operand(ebp, return_value_offset * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005311 int stack_space = 0;
Ben Murdochc5610432016-08-08 18:44:38 +01005312 Operand length_operand = ApiParameterOperand(4);
5313 Operand* stack_space_operand = &length_operand;
Ben Murdochda12d292016-06-02 14:46:10 +01005314 stack_space = argc() + FCA::kArgsLength + 1;
5315 stack_space_operand = nullptr;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005316 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5317 ApiParameterOperand(1), stack_space,
5318 stack_space_operand, return_value_operand,
5319 &context_restore_operand);
5320}
5321
5322
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005323void CallApiGetterStub::Generate(MacroAssembler* masm) {
Ben Murdochc5610432016-08-08 18:44:38 +01005324 // Build v8::PropertyCallbackInfo::args_ array on the stack and push property
5325 // name below the exit frame to make GC aware of them.
5326 STATIC_ASSERT(PropertyCallbackArguments::kShouldThrowOnErrorIndex == 0);
5327 STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 1);
5328 STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 2);
5329 STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 3);
5330 STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 4);
5331 STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 5);
5332 STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 6);
5333 STATIC_ASSERT(PropertyCallbackArguments::kArgsLength == 7);
5334
5335 Register receiver = ApiGetterDescriptor::ReceiverRegister();
5336 Register holder = ApiGetterDescriptor::HolderRegister();
5337 Register callback = ApiGetterDescriptor::CallbackRegister();
5338 Register scratch = ebx;
5339 DCHECK(!AreAliased(receiver, holder, callback, scratch));
5340
5341 __ pop(scratch); // Pop return address to extend the frame.
5342 __ push(receiver);
5343 __ push(FieldOperand(callback, AccessorInfo::kDataOffset));
5344 __ PushRoot(Heap::kUndefinedValueRootIndex); // ReturnValue
5345 // ReturnValue default value
5346 __ PushRoot(Heap::kUndefinedValueRootIndex);
5347 __ push(Immediate(ExternalReference::isolate_address(isolate())));
5348 __ push(holder);
5349 __ push(Immediate(Smi::FromInt(0))); // should_throw_on_error -> false
5350 __ push(FieldOperand(callback, AccessorInfo::kNameOffset));
5351 __ push(scratch); // Restore return address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005352
Ben Murdoch097c5b22016-05-18 11:27:45 +01005353 // v8::PropertyCallbackInfo::args_ array and name handle.
5354 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5355
5356 // Allocate v8::PropertyCallbackInfo object, arguments for callback and
5357 // space for optional callback address parameter (in case CPU profiler is
5358 // active) in non-GCed stack space.
5359 const int kApiArgc = 3 + 1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005360
Ben Murdoch097c5b22016-05-18 11:27:45 +01005361 // Load address of v8::PropertyAccessorInfo::args_ array.
5362 __ lea(scratch, Operand(esp, 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005363
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005364 PrepareCallApiFunction(masm, kApiArgc);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005365 // Create v8::PropertyCallbackInfo object on the stack and initialize
5366 // it's args_ field.
5367 Operand info_object = ApiParameterOperand(3);
5368 __ mov(info_object, scratch);
5369
Ben Murdochc5610432016-08-08 18:44:38 +01005370 // Name as handle.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005371 __ sub(scratch, Immediate(kPointerSize));
Ben Murdochc5610432016-08-08 18:44:38 +01005372 __ mov(ApiParameterOperand(0), scratch);
5373 // Arguments pointer.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005374 __ lea(scratch, info_object);
Ben Murdochc5610432016-08-08 18:44:38 +01005375 __ mov(ApiParameterOperand(1), scratch);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005376 // Reserve space for optional callback address parameter.
5377 Operand thunk_last_arg = ApiParameterOperand(2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005378
5379 ExternalReference thunk_ref =
5380 ExternalReference::invoke_accessor_getter_callback(isolate());
5381
Ben Murdochc5610432016-08-08 18:44:38 +01005382 __ mov(scratch, FieldOperand(callback, AccessorInfo::kJsGetterOffset));
5383 Register function_address = edx;
5384 __ mov(function_address,
5385 FieldOperand(scratch, Foreign::kForeignAddressOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01005386 // +3 is to skip prolog, return address and name handle.
5387 Operand return_value_operand(
5388 ebp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
Ben Murdochc5610432016-08-08 18:44:38 +01005389 CallApiFunctionAndReturn(masm, function_address, thunk_ref, thunk_last_arg,
5390 kStackUnwindSpace, nullptr, return_value_operand,
5391 NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005392}
5393
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005394#undef __
5395
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005396} // namespace internal
5397} // namespace v8
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005398
5399#endif // V8_TARGET_ARCH_X87