blob: 0224f9d4939a69535669e704ce5d9029f6e082ac [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#if V8_TARGET_ARCH_ARM
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006
Ben Murdochda12d292016-06-02 14:46:10 +01007#include "src/code-stubs.h"
8#include "src/api-arguments.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/bits.h"
10#include "src/bootstrapper.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/codegen.h"
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"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010019
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000020#include "src/arm/code-stubs-arm.h"
21
Kristian Monsen80d68ea2010-09-08 11:05:35 +010022namespace v8 {
23namespace internal {
24
25
Ben Murdochb8a8cc12014-11-26 15:28:44 +000026static void InitializeArrayConstructorDescriptor(
27 Isolate* isolate, CodeStubDescriptor* descriptor,
28 int constant_stack_parameter_count) {
29 Address deopt_handler = Runtime::FunctionForId(
30 Runtime::kArrayConstructor)->entry;
31
32 if (constant_stack_parameter_count == 0) {
33 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
34 JS_FUNCTION_STUB_MODE);
35 } else {
36 descriptor->Initialize(r0, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000038 }
39}
40
41
42static void InitializeInternalArrayConstructorDescriptor(
43 Isolate* isolate, CodeStubDescriptor* descriptor,
44 int constant_stack_parameter_count) {
45 Address deopt_handler = Runtime::FunctionForId(
46 Runtime::kInternalArrayConstructor)->entry;
47
48 if (constant_stack_parameter_count == 0) {
49 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
50 JS_FUNCTION_STUB_MODE);
51 } else {
52 descriptor->Initialize(r0, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000053 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000054 }
55}
56
57
Ben Murdochb8a8cc12014-11-26 15:28:44 +000058void ArraySingleArgumentConstructorStub::InitializeDescriptor(
59 CodeStubDescriptor* descriptor) {
60 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
61}
62
63
64void ArrayNArgumentsConstructorStub::InitializeDescriptor(
65 CodeStubDescriptor* descriptor) {
66 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
67}
68
69
Ben Murdochda12d292016-06-02 14:46:10 +010070void FastArrayPushStub::InitializeDescriptor(CodeStubDescriptor* descriptor) {
71 Address deopt_handler = Runtime::FunctionForId(Runtime::kArrayPush)->entry;
72 descriptor->Initialize(r0, deopt_handler, -1, JS_FUNCTION_STUB_MODE);
73}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074
75void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
76 CodeStubDescriptor* descriptor) {
77 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
78}
79
80
81void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
82 CodeStubDescriptor* descriptor) {
83 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
84}
85
86
Kristian Monsen80d68ea2010-09-08 11:05:35 +010087#define __ ACCESS_MASM(masm)
88
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000089static void EmitIdenticalObjectComparison(MacroAssembler* masm, Label* slow,
Ben Murdoch097c5b22016-05-18 11:27:45 +010090 Condition cond);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010091static void EmitSmiNonsmiComparison(MacroAssembler* masm,
92 Register lhs,
93 Register rhs,
94 Label* lhs_not_nan,
95 Label* slow,
96 bool strict);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010097static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
98 Register lhs,
99 Register rhs);
100
101
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000102void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
103 ExternalReference miss) {
104 // Update the static counter each time a new code stub is generated.
105 isolate()->counters()->code_stubs()->Increment();
Ben Murdoch257744e2011-11-30 15:57:28 +0000106
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000108 int param_count = descriptor.GetRegisterParameterCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109 {
110 // Call the runtime system in a fresh internal frame.
111 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
112 DCHECK(param_count == 0 ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 r0.is(descriptor.GetRegisterParameter(param_count - 1)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 // Push arguments
115 for (int i = 0; i < param_count; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000116 __ push(descriptor.GetRegisterParameter(i));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100117 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 __ CallExternalReference(miss, param_count);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100119 }
120
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000121 __ Ret();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100122}
123
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100124
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000125void DoubleToIStub::Generate(MacroAssembler* masm) {
126 Label out_of_range, only_low, negate, done;
127 Register input_reg = source();
128 Register result_reg = destination();
129 DCHECK(is_truncating());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100130
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 int double_offset = offset();
132 // Account for saved regs if input is sp.
133 if (input_reg.is(sp)) double_offset += 3 * kPointerSize;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100134
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135 Register scratch = GetRegisterThatIsNotOneOf(input_reg, result_reg);
136 Register scratch_low =
137 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch);
138 Register scratch_high =
139 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch, scratch_low);
140 LowDwVfpRegister double_scratch = kScratchDoubleReg;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100141
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 __ Push(scratch_high, scratch_low, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100143
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 if (!skip_fastpath()) {
145 // Load double input.
146 __ vldr(double_scratch, MemOperand(input_reg, double_offset));
147 __ vmov(scratch_low, scratch_high, double_scratch);
148
149 // Do fast-path convert from double to int.
150 __ vcvt_s32_f64(double_scratch.low(), double_scratch);
151 __ vmov(result_reg, double_scratch.low());
152
153 // If result is not saturated (0x7fffffff or 0x80000000), we are done.
154 __ sub(scratch, result_reg, Operand(1));
155 __ cmp(scratch, Operand(0x7ffffffe));
156 __ b(lt, &done);
157 } else {
158 // We've already done MacroAssembler::TryFastTruncatedDoubleToILoad, so we
159 // know exponent > 31, so we can skip the vcvt_s32_f64 which will saturate.
160 if (double_offset == 0) {
161 __ ldm(ia, input_reg, scratch_low.bit() | scratch_high.bit());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100162 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 __ ldr(scratch_low, MemOperand(input_reg, double_offset));
164 __ ldr(scratch_high, MemOperand(input_reg, double_offset + kIntSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100165 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100166 }
167
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000168 __ Ubfx(scratch, scratch_high,
169 HeapNumber::kExponentShift, HeapNumber::kExponentBits);
170 // Load scratch with exponent - 1. This is faster than loading
171 // with exponent because Bias + 1 = 1024 which is an *ARM* immediate value.
172 STATIC_ASSERT(HeapNumber::kExponentBias + 1 == 1024);
173 __ sub(scratch, scratch, Operand(HeapNumber::kExponentBias + 1));
174 // If exponent is greater than or equal to 84, the 32 less significant
175 // bits are 0s (2^84 = 1, 52 significant bits, 32 uncoded bits),
176 // the result is 0.
177 // Compare exponent with 84 (compare exponent - 1 with 83).
178 __ cmp(scratch, Operand(83));
179 __ b(ge, &out_of_range);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100180
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 // If we reach this code, 31 <= exponent <= 83.
182 // So, we don't have to handle cases where 0 <= exponent <= 20 for
183 // which we would need to shift right the high part of the mantissa.
184 // Scratch contains exponent - 1.
185 // Load scratch with 52 - exponent (load with 51 - (exponent - 1)).
186 __ rsb(scratch, scratch, Operand(51), SetCC);
187 __ b(ls, &only_low);
188 // 21 <= exponent <= 51, shift scratch_low and scratch_high
189 // to generate the result.
190 __ mov(scratch_low, Operand(scratch_low, LSR, scratch));
191 // Scratch contains: 52 - exponent.
192 // We needs: exponent - 20.
193 // So we use: 32 - scratch = 32 - 52 + exponent = exponent - 20.
194 __ rsb(scratch, scratch, Operand(32));
195 __ Ubfx(result_reg, scratch_high,
196 0, HeapNumber::kMantissaBitsInTopWord);
197 // Set the implicit 1 before the mantissa part in scratch_high.
198 __ orr(result_reg, result_reg,
199 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
200 __ orr(result_reg, scratch_low, Operand(result_reg, LSL, scratch));
201 __ b(&negate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100202
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 __ bind(&out_of_range);
204 __ mov(result_reg, Operand::Zero());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100205 __ b(&done);
206
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000207 __ bind(&only_low);
208 // 52 <= exponent <= 83, shift only scratch_low.
209 // On entry, scratch contains: 52 - exponent.
210 __ rsb(scratch, scratch, Operand::Zero());
211 __ mov(result_reg, Operand(scratch_low, LSL, scratch));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100212
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 __ bind(&negate);
214 // If input was positive, scratch_high ASR 31 equals 0 and
215 // scratch_high LSR 31 equals zero.
216 // New result = (result eor 0) + 0 = result.
217 // If the input was negative, we have to negate the result.
218 // Input_high ASR 31 equals 0xffffffff and scratch_high LSR 31 equals 1.
219 // New result = (result eor 0xffffffff) + 1 = 0 - result.
220 __ eor(result_reg, result_reg, Operand(scratch_high, ASR, 31));
221 __ add(result_reg, result_reg, Operand(scratch_high, LSR, 31));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100222
223 __ bind(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224
225 __ Pop(scratch_high, scratch_low, scratch);
226 __ Ret();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100227}
228
229
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100230// Handle the case where the lhs and rhs are the same object.
231// Equality is almost reflexive (everything but NaN), so this is a test
232// for "identity and not NaN".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000233static void EmitIdenticalObjectComparison(MacroAssembler* masm, Label* slow,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100234 Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100235 Label not_identical;
236 Label heap_number, return_equal;
237 __ cmp(r0, r1);
238 __ b(ne, &not_identical);
239
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000240 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
241 // so we do the second best thing - test it ourselves.
242 // They are both equal and they are not both Smis so both of them are not
243 // Smis. If it's not a heap number, then return equal.
244 if (cond == lt || cond == gt) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000245 // Call runtime on identical JSObjects.
246 __ CompareObjectType(r0, r4, r4, FIRST_JS_RECEIVER_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 __ b(ge, slow);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000248 // Call runtime on identical symbols since we need to throw a TypeError.
249 __ cmp(r4, Operand(SYMBOL_TYPE));
250 __ b(eq, slow);
251 // Call runtime on identical SIMD values since we must throw a TypeError.
252 __ cmp(r4, Operand(SIMD128_VALUE_TYPE));
253 __ b(eq, slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000254 } else {
255 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
256 __ b(eq, &heap_number);
257 // Comparing JS objects with <=, >= is complicated.
258 if (cond != eq) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000259 __ cmp(r4, Operand(FIRST_JS_RECEIVER_TYPE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100260 __ b(ge, slow);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000261 // Call runtime on identical symbols since we need to throw a TypeError.
262 __ cmp(r4, Operand(SYMBOL_TYPE));
263 __ b(eq, slow);
264 // Call runtime on identical SIMD values since we must throw a TypeError.
265 __ cmp(r4, Operand(SIMD128_VALUE_TYPE));
266 __ b(eq, slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000267 // Normally here we fall through to return_equal, but undefined is
268 // special: (undefined == undefined) == true, but
269 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
270 if (cond == le || cond == ge) {
271 __ cmp(r4, Operand(ODDBALL_TYPE));
272 __ b(ne, &return_equal);
273 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
274 __ cmp(r0, r2);
275 __ b(ne, &return_equal);
276 if (cond == le) {
277 // undefined <= undefined should fail.
278 __ mov(r0, Operand(GREATER));
279 } else {
280 // undefined >= undefined should fail.
281 __ mov(r0, Operand(LESS));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100282 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100284 }
285 }
286 }
287
288 __ bind(&return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +0100289 if (cond == lt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100290 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
Steve Block1e0659c2011-05-24 12:43:12 +0100291 } else if (cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100292 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
293 } else {
294 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
295 }
296 __ Ret();
297
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 // For less and greater we don't have to check for NaN since the result of
299 // x < x is false regardless. For the others here is some code to check
300 // for NaN.
301 if (cond != lt && cond != gt) {
302 __ bind(&heap_number);
303 // It is a heap number, so return non-equal if it's NaN and equal if it's
304 // not NaN.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100305
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000306 // The representation of NaN values has all exponent bits (52..62) set,
307 // and not all mantissa bits (0..51) clear.
308 // Read top bits of double representation (second word of value).
309 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
310 // Test that exponent bits are all set.
311 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
312 // NaNs have all-one exponents so they sign extend to -1.
313 __ cmp(r3, Operand(-1));
314 __ b(ne, &return_equal);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100315
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000316 // Shift out flag and all exponent bits, retaining only mantissa.
317 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
318 // Or with all low-bits of mantissa.
319 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
320 __ orr(r0, r3, Operand(r2), SetCC);
321 // For equal we already have the right value in r0: Return zero (equal)
322 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
323 // not (it's a NaN). For <= and >= we need to load r0 with the failing
324 // value if it's a NaN.
325 if (cond != eq) {
326 // All-zero means Infinity means equal.
327 __ Ret(eq);
328 if (cond == le) {
329 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
330 } else {
331 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100332 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100333 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000334 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100335 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336 // No fall through here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100337
338 __ bind(&not_identical);
339}
340
341
342// See comment at call site.
343static void EmitSmiNonsmiComparison(MacroAssembler* masm,
344 Register lhs,
345 Register rhs,
346 Label* lhs_not_nan,
347 Label* slow,
348 bool strict) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 DCHECK((lhs.is(r0) && rhs.is(r1)) ||
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100350 (lhs.is(r1) && rhs.is(r0)));
351
352 Label rhs_is_smi;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000353 __ JumpIfSmi(rhs, &rhs_is_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100354
355 // Lhs is a Smi. Check whether the rhs is a heap number.
356 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
357 if (strict) {
358 // If rhs is not a number and lhs is a Smi then strict equality cannot
359 // succeed. Return non-equal
360 // If rhs is r0 then there is already a non zero value in it.
361 if (!rhs.is(r0)) {
362 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
363 }
364 __ Ret(ne);
365 } else {
366 // Smi compared non-strictly with a non-Smi non-heap-number. Call
367 // the runtime.
368 __ b(ne, slow);
369 }
370
371 // Lhs is a smi, rhs is a number.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000372 // Convert lhs to a double in d7.
373 __ SmiToDouble(d7, lhs);
374 // Load the double from rhs, tagged HeapNumber r0, to d6.
375 __ vldr(d6, rhs, HeapNumber::kValueOffset - kHeapObjectTag);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100376
377 // We now have both loaded as doubles but we can skip the lhs nan check
378 // since it's a smi.
379 __ jmp(lhs_not_nan);
380
381 __ bind(&rhs_is_smi);
382 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
383 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
384 if (strict) {
385 // If lhs is not a number and rhs is a smi then strict equality cannot
386 // succeed. Return non-equal.
387 // If lhs is r0 then there is already a non zero value in it.
388 if (!lhs.is(r0)) {
389 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
390 }
391 __ Ret(ne);
392 } else {
393 // Smi compared non-strictly with a non-smi non-heap-number. Call
394 // the runtime.
395 __ b(ne, slow);
396 }
397
398 // Rhs is a smi, lhs is a heap number.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000399 // Load the double from lhs, tagged HeapNumber r1, to d7.
400 __ vldr(d7, lhs, HeapNumber::kValueOffset - kHeapObjectTag);
401 // Convert rhs to a double in d6 .
402 __ SmiToDouble(d6, rhs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100403 // Fall through to both_loaded_as_doubles.
404}
405
406
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100407// See comment at call site.
408static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
409 Register lhs,
410 Register rhs) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000411 DCHECK((lhs.is(r0) && rhs.is(r1)) ||
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100412 (lhs.is(r1) && rhs.is(r0)));
413
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000414 // If either operand is a JS object or an oddball value, then they are
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100415 // not equal since their pointers are different.
416 // There is no test for undetectability in strict equality.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000417 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100418 Label first_non_object;
419 // Get the type of the first operand into r2 and compare it with
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000420 // FIRST_JS_RECEIVER_TYPE.
421 __ CompareObjectType(rhs, r2, r2, FIRST_JS_RECEIVER_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100422 __ b(lt, &first_non_object);
423
424 // Return non-zero (r0 is not zero)
425 Label return_not_equal;
426 __ bind(&return_not_equal);
427 __ Ret();
428
429 __ bind(&first_non_object);
430 // Check for oddballs: true, false, null, undefined.
431 __ cmp(r2, Operand(ODDBALL_TYPE));
432 __ b(eq, &return_not_equal);
433
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000434 __ CompareObjectType(lhs, r3, r3, FIRST_JS_RECEIVER_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100435 __ b(ge, &return_not_equal);
436
437 // Check for oddballs: true, false, null, undefined.
438 __ cmp(r3, Operand(ODDBALL_TYPE));
439 __ b(eq, &return_not_equal);
440
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000441 // Now that we have the types we might as well check for
442 // internalized-internalized.
443 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
444 __ orr(r2, r2, Operand(r3));
445 __ tst(r2, Operand(kIsNotStringMask | kIsNotInternalizedMask));
446 __ b(eq, &return_not_equal);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100447}
448
449
450// See comment at call site.
451static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
452 Register lhs,
453 Register rhs,
454 Label* both_loaded_as_doubles,
455 Label* not_heap_numbers,
456 Label* slow) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000457 DCHECK((lhs.is(r0) && rhs.is(r1)) ||
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100458 (lhs.is(r1) && rhs.is(r0)));
459
460 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
461 __ b(ne, not_heap_numbers);
462 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
463 __ cmp(r2, r3);
464 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
465
466 // Both are heap numbers. Load them up then jump to the code we have
467 // for that.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000468 __ vldr(d6, rhs, HeapNumber::kValueOffset - kHeapObjectTag);
469 __ vldr(d7, lhs, HeapNumber::kValueOffset - kHeapObjectTag);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100470 __ jmp(both_loaded_as_doubles);
471}
472
473
Ben Murdochda12d292016-06-02 14:46:10 +0100474// Fast negative check for internalized-to-internalized equality or receiver
475// equality. Also handles the undetectable receiver to null/undefined
476// comparison.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000477static void EmitCheckForInternalizedStringsOrObjects(MacroAssembler* masm,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100478 Register lhs, Register rhs,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000479 Label* possible_strings,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100480 Label* runtime_call) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000481 DCHECK((lhs.is(r0) && rhs.is(r1)) ||
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100482 (lhs.is(r1) && rhs.is(r0)));
483
484 // r2 is object type of rhs.
Ben Murdochda12d292016-06-02 14:46:10 +0100485 Label object_test, return_equal, return_unequal, undetectable;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000486 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100487 __ tst(r2, Operand(kIsNotStringMask));
488 __ b(ne, &object_test);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489 __ tst(r2, Operand(kIsNotInternalizedMask));
490 __ b(ne, possible_strings);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100491 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100492 __ b(ge, runtime_call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493 __ tst(r3, Operand(kIsNotInternalizedMask));
494 __ b(ne, possible_strings);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100495
Ben Murdoch097c5b22016-05-18 11:27:45 +0100496 // Both are internalized. We already checked they weren't the same pointer so
497 // they are not equal. Return non-equal by returning the non-zero object
498 // pointer in r0.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100499 __ Ret();
500
501 __ bind(&object_test);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100502 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100503 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +0100504 __ ldrb(r4, FieldMemOperand(r2, Map::kBitFieldOffset));
505 __ ldrb(r5, FieldMemOperand(r3, Map::kBitFieldOffset));
506 __ tst(r4, Operand(1 << Map::kIsUndetectable));
507 __ b(ne, &undetectable);
508 __ tst(r5, Operand(1 << Map::kIsUndetectable));
509 __ b(ne, &return_unequal);
510
511 __ CompareInstanceType(r2, r2, FIRST_JS_RECEIVER_TYPE);
512 __ b(lt, runtime_call);
513 __ CompareInstanceType(r3, r3, FIRST_JS_RECEIVER_TYPE);
514 __ b(lt, runtime_call);
515
516 __ bind(&return_unequal);
517 // Return non-equal by returning the non-zero object pointer in r0.
518 __ Ret();
519
520 __ bind(&undetectable);
521 __ tst(r5, Operand(1 << Map::kIsUndetectable));
522 __ b(eq, &return_unequal);
Ben Murdochda12d292016-06-02 14:46:10 +0100523
524 // If both sides are JSReceivers, then the result is false according to
525 // the HTML specification, which says that only comparisons with null or
526 // undefined are affected by special casing for document.all.
527 __ CompareInstanceType(r2, r2, ODDBALL_TYPE);
528 __ b(eq, &return_equal);
529 __ CompareInstanceType(r3, r3, ODDBALL_TYPE);
530 __ b(ne, &return_unequal);
531
532 __ bind(&return_equal);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100533 __ mov(r0, Operand(EQUAL));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100534 __ Ret();
535}
536
537
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000538static void CompareICStub_CheckInputType(MacroAssembler* masm, Register input,
539 Register scratch,
540 CompareICState::State expected,
541 Label* fail) {
542 Label ok;
543 if (expected == CompareICState::SMI) {
544 __ JumpIfNotSmi(input, fail);
545 } else if (expected == CompareICState::NUMBER) {
546 __ JumpIfSmi(input, &ok);
547 __ CheckMap(input, scratch, Heap::kHeapNumberMapRootIndex, fail,
548 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100549 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000550 // We could be strict about internalized/non-internalized here, but as long as
551 // hydrogen doesn't care, the stub doesn't have to care either.
552 __ bind(&ok);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100553}
554
555
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556// On entry r1 and r2 are the values to be compared.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100557// On exit r0 is 0, positive or negative to indicate the result of
558// the comparison.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000559void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
560 Register lhs = r1;
561 Register rhs = r0;
562 Condition cc = GetCondition();
563
564 Label miss;
565 CompareICStub_CheckInputType(masm, lhs, r2, left(), &miss);
566 CompareICStub_CheckInputType(masm, rhs, r3, right(), &miss);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100567
568 Label slow; // Call builtin.
569 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
570
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000571 Label not_two_smis, smi_done;
572 __ orr(r2, r1, r0);
573 __ JumpIfNotSmi(r2, &not_two_smis);
574 __ mov(r1, Operand(r1, ASR, 1));
575 __ sub(r0, r1, Operand(r0, ASR, 1));
576 __ Ret();
577 __ bind(&not_two_smis);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100578
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100579 // NOTICE! This code is only reached after a smi-fast-case check, so
580 // it is certain that at least one operand isn't a smi.
581
582 // Handle the case where the objects are identical. Either returns the answer
583 // or goes to slow. Only falls through if the objects were not identical.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100584 EmitIdenticalObjectComparison(masm, &slow, cc);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100585
586 // If either is a Smi (we know that not both are), then they can only
587 // be strictly equal if the other is a HeapNumber.
588 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000589 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000590 __ and_(r2, lhs, Operand(rhs));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000591 __ JumpIfNotSmi(r2, &not_smis);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100592 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
593 // 1) Return the answer.
594 // 2) Go to slow.
595 // 3) Fall through to both_loaded_as_doubles.
596 // 4) Jump to lhs_not_nan.
597 // In cases 3 and 4 we have found out we were dealing with a number-number
598 // comparison. If VFP3 is supported the double values of the numbers have
599 // been loaded into d7 and d6. Otherwise, the double values have been loaded
600 // into r0, r1, r2, and r3.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 EmitSmiNonsmiComparison(masm, lhs, rhs, &lhs_not_nan, &slow, strict());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100602
603 __ bind(&both_loaded_as_doubles);
604 // The arguments have been converted to doubles and stored in d6 and d7, if
605 // VFP3 is supported, or in r0, r1, r2, and r3.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000606 __ bind(&lhs_not_nan);
607 Label no_nan;
608 // ARMv7 VFP3 instructions to implement double precision comparison.
609 __ VFPCompareAndSetFlags(d7, d6);
610 Label nan;
611 __ b(vs, &nan);
612 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
613 __ mov(r0, Operand(LESS), LeaveCC, lt);
614 __ mov(r0, Operand(GREATER), LeaveCC, gt);
615 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100616
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000617 __ bind(&nan);
618 // If one of the sides was a NaN then the v flag is set. Load r0 with
619 // whatever it takes to make the comparison fail, since comparisons with NaN
620 // always fail.
621 if (cc == lt || cc == le) {
622 __ mov(r0, Operand(GREATER));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100623 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 __ mov(r0, Operand(LESS));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100625 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000626 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100627
628 __ bind(&not_smis);
629 // At this point we know we are dealing with two different objects,
630 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000631 if (strict()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100632 // This returns non-equal for some object types, or falls through if it
633 // was not lucky.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000634 EmitStrictTwoHeapObjectCompare(masm, lhs, rhs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100635 }
636
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000637 Label check_for_internalized_strings;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100638 Label flat_string_check;
639 // Check for heap-number-heap-number comparison. Can jump to slow case,
640 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000641 // that case. If the inputs are not doubles then jumps to
642 // check_for_internalized_strings.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100643 // In this case r2 will contain the type of rhs_. Never falls through.
644 EmitCheckForTwoHeapNumbers(masm,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000645 lhs,
646 rhs,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100647 &both_loaded_as_doubles,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000648 &check_for_internalized_strings,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100649 &flat_string_check);
650
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000651 __ bind(&check_for_internalized_strings);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100652 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000653 // internalized strings.
654 if (cc == eq && !strict()) {
655 // Returns an answer for two internalized strings or two detectable objects.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100656 // Otherwise jumps to string case or not both strings case.
657 // Assumes that r2 is the type of rhs_ on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000658 EmitCheckForInternalizedStringsOrObjects(
659 masm, lhs, rhs, &flat_string_check, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100660 }
661
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 // Check for both being sequential one-byte strings,
663 // and inline if that is the case.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100664 __ bind(&flat_string_check);
665
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000666 __ JumpIfNonSmisNotBothSequentialOneByteStrings(lhs, rhs, r2, r3, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100667
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000668 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1, r2,
669 r3);
670 if (cc == eq) {
671 StringHelper::GenerateFlatOneByteStringEquals(masm, lhs, rhs, r2, r3, r4);
Ben Murdoch257744e2011-11-30 15:57:28 +0000672 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000673 StringHelper::GenerateCompareFlatOneByteStrings(masm, lhs, rhs, r2, r3, r4,
674 r5);
Ben Murdoch257744e2011-11-30 15:57:28 +0000675 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100676 // Never falls through to here.
677
678 __ bind(&slow);
679
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000680 if (cc == eq) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100681 {
682 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
683 __ Push(lhs, rhs);
684 __ CallRuntime(strict() ? Runtime::kStrictEqual : Runtime::kEqual);
685 }
686 // Turn true into 0 and false into some non-zero value.
687 STATIC_ASSERT(EQUAL == 0);
688 __ LoadRoot(r1, Heap::kTrueValueRootIndex);
689 __ sub(r0, r0, r1);
690 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100691 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100692 __ Push(lhs, rhs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100693 int ncr; // NaN compare result
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000694 if (cc == lt || cc == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100695 ncr = GREATER;
696 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000697 DCHECK(cc == gt || cc == ge); // remaining cases
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100698 ncr = LESS;
699 }
700 __ mov(r0, Operand(Smi::FromInt(ncr)));
701 __ push(r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100702
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000703 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
704 // tagged as a small integer.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100705 __ TailCallRuntime(Runtime::kCompare);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000706 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100707
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000708 __ bind(&miss);
709 GenerateMiss(masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100710}
711
712
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100713void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
714 // We don't allow a GC during a store buffer overflow so there is no need to
715 // store the registers in any particular way, but we do have to store and
716 // restore them.
717 __ stm(db_w, sp, kCallerSaved | lr.bit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000718
719 const Register scratch = r1;
720
721 if (save_doubles()) {
722 __ SaveFPRegs(sp, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100723 }
724 const int argument_count = 1;
725 const int fp_argument_count = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100726
727 AllowExternalCallThatCantCauseGC scope(masm);
728 __ PrepareCallCFunction(argument_count, fp_argument_count, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000729 __ mov(r0, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100730 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000731 ExternalReference::store_buffer_overflow_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100732 argument_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000733 if (save_doubles()) {
734 __ RestoreFPRegs(sp, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100735 }
736 __ ldm(ia_w, sp, kCallerSaved | pc.bit()); // Also pop pc to get Ret(0).
737}
738
739
Steve Block44f0eee2011-05-26 01:26:41 +0100740void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100741 const Register base = r1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000742 const Register exponent = MathPowTaggedDescriptor::exponent();
743 DCHECK(exponent.is(r2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100744 const Register heapnumbermap = r5;
745 const Register heapnumber = r0;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000746 const DwVfpRegister double_base = d0;
747 const DwVfpRegister double_exponent = d1;
748 const DwVfpRegister double_result = d2;
749 const DwVfpRegister double_scratch = d3;
750 const SwVfpRegister single_scratch = s6;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100751 const Register scratch = r9;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000752 const Register scratch2 = r4;
Steve Block44f0eee2011-05-26 01:26:41 +0100753
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100754 Label call_runtime, done, int_exponent;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000755 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100756 Label base_is_smi, unpack_exponent;
757 // The exponent and base are supplied as arguments on the stack.
758 // This can only happen if the stub is called from non-optimized code.
759 // Load input parameters from stack to double registers.
Steve Block44f0eee2011-05-26 01:26:41 +0100760 __ ldr(base, MemOperand(sp, 1 * kPointerSize));
761 __ ldr(exponent, MemOperand(sp, 0 * kPointerSize));
762
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100763 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +0100764
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100765 __ UntagAndJumpIfSmi(scratch, base, &base_is_smi);
Steve Block44f0eee2011-05-26 01:26:41 +0100766 __ ldr(scratch, FieldMemOperand(base, JSObject::kMapOffset));
767 __ cmp(scratch, heapnumbermap);
768 __ b(ne, &call_runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100769
Ben Murdochc7cc0282012-03-05 14:35:55 +0000770 __ vldr(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100771 __ jmp(&unpack_exponent);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000772
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100773 __ bind(&base_is_smi);
774 __ vmov(single_scratch, scratch);
775 __ vcvt_f64_s32(double_base, single_scratch);
776 __ bind(&unpack_exponent);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000777
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100778 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
Steve Block44f0eee2011-05-26 01:26:41 +0100779
Steve Block44f0eee2011-05-26 01:26:41 +0100780 __ ldr(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
781 __ cmp(scratch, heapnumbermap);
782 __ b(ne, &call_runtime);
Steve Block44f0eee2011-05-26 01:26:41 +0100783 __ vldr(double_exponent,
784 FieldMemOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000785 } else if (exponent_type() == TAGGED) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100786 // Base is already in double_base.
787 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
Steve Block44f0eee2011-05-26 01:26:41 +0100788
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100789 __ vldr(double_exponent,
790 FieldMemOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +0000791 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100792
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000793 if (exponent_type() != INTEGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100794 Label int_exponent_convert;
795 // Detect integer exponents stored as double.
796 __ vcvt_u32_f64(single_scratch, double_exponent);
797 // We do not check for NaN or Infinity here because comparing numbers on
798 // ARM correctly distinguishes NaNs. We end up calling the built-in.
799 __ vcvt_f64_u32(double_scratch, single_scratch);
800 __ VFPCompareAndSetFlags(double_scratch, double_exponent);
801 __ b(eq, &int_exponent_convert);
802
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000803 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100804 // Detect square root case. Crankshaft detects constant +/-0.5 at
805 // compile time and uses DoMathPowHalf instead. We then skip this check
806 // for non-constant cases of +/-0.5 as these hardly occur.
807 Label not_plus_half;
808
809 // Test for 0.5.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000810 __ vmov(double_scratch, 0.5, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100811 __ VFPCompareAndSetFlags(double_exponent, double_scratch);
812 __ b(ne, &not_plus_half);
813
814 // Calculates square root of base. Check for the special case of
815 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000816 __ vmov(double_scratch, -V8_INFINITY, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100817 __ VFPCompareAndSetFlags(double_base, double_scratch);
818 __ vneg(double_result, double_scratch, eq);
819 __ b(eq, &done);
820
821 // Add +0 to convert -0 to +0.
822 __ vadd(double_scratch, double_base, kDoubleRegZero);
823 __ vsqrt(double_result, double_scratch);
824 __ jmp(&done);
825
826 __ bind(&not_plus_half);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000827 __ vmov(double_scratch, -0.5, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100828 __ VFPCompareAndSetFlags(double_exponent, double_scratch);
829 __ b(ne, &call_runtime);
830
831 // Calculates square root of base. Check for the special case of
832 // Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000833 __ vmov(double_scratch, -V8_INFINITY, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100834 __ VFPCompareAndSetFlags(double_base, double_scratch);
835 __ vmov(double_result, kDoubleRegZero, eq);
836 __ b(eq, &done);
837
838 // Add +0 to convert -0 to +0.
839 __ vadd(double_scratch, double_base, kDoubleRegZero);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000840 __ vmov(double_result, 1.0, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100841 __ vsqrt(double_scratch, double_scratch);
842 __ vdiv(double_result, double_result, double_scratch);
843 __ jmp(&done);
844 }
845
846 __ push(lr);
847 {
848 AllowExternalCallThatCantCauseGC scope(masm);
849 __ PrepareCallCFunction(0, 2, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850 __ MovToFloatParameters(double_base, double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100851 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000852 ExternalReference::power_double_double_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100853 0, 2);
854 }
855 __ pop(lr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000856 __ MovFromFloatResult(double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100857 __ jmp(&done);
858
859 __ bind(&int_exponent_convert);
860 __ vcvt_u32_f64(single_scratch, double_exponent);
861 __ vmov(scratch, single_scratch);
862 }
863
864 // Calculate power with integer exponent.
865 __ bind(&int_exponent);
866
867 // Get two copies of exponent in the registers scratch and exponent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868 if (exponent_type() == INTEGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100869 __ mov(scratch, exponent);
870 } else {
871 // Exponent has previously been stored into scratch as untagged integer.
872 __ mov(exponent, scratch);
873 }
874 __ vmov(double_scratch, double_base); // Back up base.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 __ vmov(double_result, 1.0, scratch2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100876
877 // Get absolute value of exponent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000878 __ cmp(scratch, Operand::Zero());
879 __ mov(scratch2, Operand::Zero(), LeaveCC, mi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100880 __ sub(scratch, scratch2, scratch, LeaveCC, mi);
881
882 Label while_true;
883 __ bind(&while_true);
884 __ mov(scratch, Operand(scratch, ASR, 1), SetCC);
885 __ vmul(double_result, double_result, double_scratch, cs);
886 __ vmul(double_scratch, double_scratch, double_scratch, ne);
887 __ b(ne, &while_true);
888
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000889 __ cmp(exponent, Operand::Zero());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100890 __ b(ge, &done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000891 __ vmov(double_scratch, 1.0, scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100892 __ vdiv(double_result, double_scratch, double_result);
893 // Test whether result is zero. Bail out to check for subnormal result.
894 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
895 __ VFPCompareAndSetFlags(double_result, 0.0);
896 __ b(ne, &done);
897 // double_exponent may not containe the exponent value if the input was a
898 // smi. We set it with exponent value before bailing out.
899 __ vmov(single_scratch, exponent);
900 __ vcvt_f64_s32(double_exponent, single_scratch);
901
902 // Returning or bailing out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000903 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100904 // The arguments are still on the stack.
905 __ bind(&call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000906 __ TailCallRuntime(Runtime::kMathPowRT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100907
908 // The stub is called from non-optimized code, which expects the result
909 // as heap number in exponent.
910 __ bind(&done);
911 __ AllocateHeapNumber(
912 heapnumber, scratch, scratch2, heapnumbermap, &call_runtime);
913 __ vstr(double_result,
914 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000915 DCHECK(heapnumber.is(r0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100916 __ Ret(2);
917 } else {
918 __ push(lr);
919 {
920 AllowExternalCallThatCantCauseGC scope(masm);
921 __ PrepareCallCFunction(0, 2, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000922 __ MovToFloatParameters(double_base, double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100923 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000924 ExternalReference::power_double_double_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100925 0, 2);
926 }
927 __ pop(lr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000928 __ MovFromFloatResult(double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100929
930 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100931 __ Ret();
932 }
Steve Block44f0eee2011-05-26 01:26:41 +0100933}
934
935
936bool CEntryStub::NeedsImmovableCode() {
937 return true;
938}
939
940
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000941void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
942 CEntryStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
944 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
945 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
946 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000947 CreateWeakCellStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000948 BinaryOpICStub::GenerateAheadOfTime(isolate);
949 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000950 StoreFastElementStub::GenerateAheadOfTime(isolate);
951 TypeofStub::GenerateAheadOfTime(isolate);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000952}
953
954
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000955void CodeStub::GenerateFPStubs(Isolate* isolate) {
956 // Generate if not already in cache.
957 SaveFPRegsMode mode = kSaveFPRegs;
958 CEntryStub(isolate, 1, mode).GetCode();
959 StoreBufferOverflowStub(isolate, mode).GetCode();
960 isolate->set_fp_stubs_generated(true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100961}
962
963
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000964void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
965 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
966 stub.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100967}
968
969
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970void CEntryStub::Generate(MacroAssembler* masm) {
971 // Called from JavaScript; parameters are on stack as if calling JS function.
972 // r0: number of arguments including receiver
973 // r1: pointer to builtin function
974 // fp: frame pointer (restored after C call)
975 // sp: stack pointer (restored as callee's sp after C call)
976 // cp: current context (C callee-saved)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000977 //
978 // If argv_in_register():
979 // r2: pointer to the first argument
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000980 ProfileEntryHookStub::MaybeCallEntryHook(masm);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000981
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982 __ mov(r5, Operand(r1));
983
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000984 if (argv_in_register()) {
985 // Move argv into the correct register.
986 __ mov(r1, Operand(r2));
987 } else {
988 // Compute the argv pointer in a callee-saved register.
989 __ add(r1, sp, Operand(r0, LSL, kPointerSizeLog2));
990 __ sub(r1, r1, Operand(kPointerSize));
991 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000992
993 // Enter the exit frame that transitions from JavaScript to C++.
994 FrameScope scope(masm, StackFrame::MANUAL);
995 __ EnterExitFrame(save_doubles());
996
997 // Store a copy of argc in callee-saved registers for later.
998 __ mov(r4, Operand(r0));
999
1000 // r0, r4: number of arguments including receiver (C callee-saved)
1001 // r1: pointer to the first argument (C callee-saved)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001002 // r5: pointer to builtin function (C callee-saved)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001003
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001004 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
1005 int frame_alignment_mask = frame_alignment - 1;
Ben Murdoch097c5b22016-05-18 11:27:45 +01001006#if V8_HOST_ARCH_ARM
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001007 if (FLAG_debug_code) {
1008 if (frame_alignment > kPointerSize) {
1009 Label alignment_as_expected;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001010 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
Steve Block1e0659c2011-05-24 12:43:12 +01001011 __ tst(sp, Operand(frame_alignment_mask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001012 __ b(eq, &alignment_as_expected);
1013 // Don't use Check here, as it will call Runtime_Abort re-entering here.
1014 __ stop("Unexpected alignment");
1015 __ bind(&alignment_as_expected);
1016 }
1017 }
1018#endif
1019
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001020 // Call C built-in.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001021 int result_stack_size;
1022 if (result_size() <= 2) {
1023 // r0 = argc, r1 = argv, r2 = isolate
1024 __ mov(r2, Operand(ExternalReference::isolate_address(isolate())));
1025 result_stack_size = 0;
1026 } else {
1027 DCHECK_EQ(3, result_size());
1028 // Allocate additional space for the result.
1029 result_stack_size =
1030 ((result_size() * kPointerSize) + frame_alignment_mask) &
1031 ~frame_alignment_mask;
1032 __ sub(sp, sp, Operand(result_stack_size));
1033
1034 // r0 = hidden result argument, r1 = argc, r2 = argv, r3 = isolate.
1035 __ mov(r3, Operand(ExternalReference::isolate_address(isolate())));
1036 __ mov(r2, Operand(r1));
1037 __ mov(r1, Operand(r0));
1038 __ mov(r0, Operand(sp));
1039 }
Steve Block44f0eee2011-05-26 01:26:41 +01001040
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001041 // To let the GC traverse the return address of the exit frames, we need to
1042 // know where the return address is. The CEntryStub is unmovable, so
1043 // we can store the address on the stack to be able to find it again and
1044 // we never have to restore it, because it will not change.
Steve Block1e0659c2011-05-24 12:43:12 +01001045 // Compute the return address in lr to return to after the jump below. Pc is
1046 // already at '+ 8' from the current instruction but return is after three
1047 // instructions so add another 4 to pc to get the return address.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001048 {
1049 // Prevent literal pool emission before return address.
1050 Assembler::BlockConstPoolScope block_const_pool(masm);
1051 __ add(lr, pc, Operand(4));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001052 __ str(lr, MemOperand(sp, result_stack_size));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001053 __ Call(r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001054 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001055 if (result_size() > 2) {
1056 DCHECK_EQ(3, result_size());
1057 // Read result values stored on stack.
Ben Murdochda12d292016-06-02 14:46:10 +01001058 __ ldr(r2, MemOperand(sp, 2 * kPointerSize));
1059 __ ldr(r1, MemOperand(sp, 1 * kPointerSize));
1060 __ ldr(r0, MemOperand(sp, 0 * kPointerSize));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001061 }
1062 // Result returned in r0, r1:r0 or r2:r1:r0 - do not destroy these registers!
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001063
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001064 // Check result for exception sentinel.
1065 Label exception_returned;
1066 __ CompareRoot(r0, Heap::kExceptionRootIndex);
1067 __ b(eq, &exception_returned);
1068
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001069 // Check that there is no pending exception, otherwise we
1070 // should have returned the exception sentinel.
1071 if (FLAG_debug_code) {
1072 Label okay;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001073 ExternalReference pending_exception_address(
1074 Isolate::kPendingExceptionAddress, isolate());
Ben Murdoch097c5b22016-05-18 11:27:45 +01001075 __ mov(r3, Operand(pending_exception_address));
1076 __ ldr(r3, MemOperand(r3));
1077 __ CompareRoot(r3, Heap::kTheHoleValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001078 // Cannot use check here as it attempts to generate call into runtime.
1079 __ b(eq, &okay);
1080 __ stop("Unexpected pending exception");
1081 __ bind(&okay);
1082 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001083
1084 // Exit C frame and return.
1085 // r0:r1: result
1086 // sp: stack pointer
1087 // fp: frame pointer
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001088 Register argc;
1089 if (argv_in_register()) {
1090 // We don't want to pop arguments so set argc to no_reg.
1091 argc = no_reg;
1092 } else {
1093 // Callee-saved register r4 still holds argc.
1094 argc = r4;
1095 }
1096 __ LeaveExitFrame(save_doubles(), argc, true);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001097 __ mov(pc, lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001098
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001099 // Handling of exception.
1100 __ bind(&exception_returned);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001101
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001102 ExternalReference pending_handler_context_address(
1103 Isolate::kPendingHandlerContextAddress, isolate());
1104 ExternalReference pending_handler_code_address(
1105 Isolate::kPendingHandlerCodeAddress, isolate());
1106 ExternalReference pending_handler_offset_address(
1107 Isolate::kPendingHandlerOffsetAddress, isolate());
1108 ExternalReference pending_handler_fp_address(
1109 Isolate::kPendingHandlerFPAddress, isolate());
1110 ExternalReference pending_handler_sp_address(
1111 Isolate::kPendingHandlerSPAddress, isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001112
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001113 // Ask the runtime for help to determine the handler. This will set r0 to
1114 // contain the current pending exception, don't clobber it.
1115 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
1116 isolate());
1117 {
1118 FrameScope scope(masm, StackFrame::MANUAL);
1119 __ PrepareCallCFunction(3, 0, r0);
1120 __ mov(r0, Operand(0));
1121 __ mov(r1, Operand(0));
1122 __ mov(r2, Operand(ExternalReference::isolate_address(isolate())));
1123 __ CallCFunction(find_handler, 3);
1124 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001125
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001126 // Retrieve the handler context, SP and FP.
1127 __ mov(cp, Operand(pending_handler_context_address));
1128 __ ldr(cp, MemOperand(cp));
1129 __ mov(sp, Operand(pending_handler_sp_address));
1130 __ ldr(sp, MemOperand(sp));
1131 __ mov(fp, Operand(pending_handler_fp_address));
1132 __ ldr(fp, MemOperand(fp));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001133
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001134 // If the handler is a JS frame, restore the context to the frame. Note that
1135 // the context will be set to (cp == 0) for non-JS frames.
1136 __ cmp(cp, Operand(0));
1137 __ str(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001138
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001139 // Compute the handler entry address and jump to it.
1140 ConstantPoolUnavailableScope constant_pool_unavailable(masm);
1141 __ mov(r1, Operand(pending_handler_code_address));
1142 __ ldr(r1, MemOperand(r1));
1143 __ mov(r2, Operand(pending_handler_offset_address));
1144 __ ldr(r2, MemOperand(r2));
1145 __ add(r1, r1, Operand(Code::kHeaderSize - kHeapObjectTag)); // Code start
1146 if (FLAG_enable_embedded_constant_pool) {
1147 __ LoadConstantPoolPointerRegisterFromCodeTargetAddress(r1);
1148 }
1149 __ add(pc, r1, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001150}
1151
1152
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001153void JSEntryStub::Generate(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001154 // r0: code entry
1155 // r1: function
1156 // r2: receiver
1157 // r3: argc
1158 // [sp+0]: argv
1159
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001160 Label invoke, handler_entry, exit;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001161
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001162 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1163
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001164 // Called from C, so do not pop argc and args on exit (preserve sp)
1165 // No need to save register-passed args
1166 // Save callee-saved registers (incl. cp and fp), sp, and lr
1167 __ stm(db_w, sp, kCalleeSaved | lr.bit());
1168
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001169 // Save callee-saved vfp registers.
1170 __ vstm(db_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
1171 // Set up the reserved register for 0.0.
1172 __ vmov(kDoubleRegZero, 0.0);
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01001173
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001174 // Get address of argv, see stm above.
1175 // r0: code entry
1176 // r1: function
1177 // r2: receiver
1178 // r3: argc
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01001179
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001180 // Set up argv in r4.
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01001181 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001182 offset_to_argv += kNumDoubleCalleeSaved * kDoubleSize;
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01001183 __ ldr(r4, MemOperand(sp, offset_to_argv));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001184
1185 // Push a frame with special values setup to mark it as an entry frame.
1186 // r0: code entry
1187 // r1: function
1188 // r2: receiver
1189 // r3: argc
1190 // r4: argv
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001191 int marker = type();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001192 if (FLAG_enable_embedded_constant_pool) {
1193 __ mov(r8, Operand::Zero());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001194 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001195 __ mov(r7, Operand(Smi::FromInt(marker)));
1196 __ mov(r6, Operand(Smi::FromInt(marker)));
Steve Block44f0eee2011-05-26 01:26:41 +01001197 __ mov(r5,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001198 Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001199 __ ldr(r5, MemOperand(r5));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001200 __ mov(ip, Operand(-1)); // Push a bad frame pointer to fail if it is used.
1201 __ stm(db_w, sp, r5.bit() | r6.bit() | r7.bit() |
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001202 (FLAG_enable_embedded_constant_pool ? r8.bit() : 0) |
1203 ip.bit());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001204
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001205 // Set up frame pointer for the frame to be pushed.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001206 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
1207
Ben Murdochb0fe1622011-05-05 13:52:32 +01001208 // If this is the outermost JS call, set js_entry_sp value.
Steve Block053d10c2011-06-13 19:13:29 +01001209 Label non_outermost_js;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001210 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001211 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
1212 __ ldr(r6, MemOperand(r5));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001213 __ cmp(r6, Operand::Zero());
Steve Block053d10c2011-06-13 19:13:29 +01001214 __ b(ne, &non_outermost_js);
1215 __ str(fp, MemOperand(r5));
1216 __ mov(ip, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1217 Label cont;
1218 __ b(&cont);
1219 __ bind(&non_outermost_js);
1220 __ mov(ip, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
1221 __ bind(&cont);
1222 __ push(ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001223
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001224 // Jump to a faked try block that does the invoke, with a faked catch
1225 // block that sets the pending exception.
1226 __ jmp(&invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001227
1228 // Block literal pool emission whilst taking the position of the handler
1229 // entry. This avoids making the assumption that literal pools are always
1230 // emitted after an instruction is emitted, rather than before.
1231 {
1232 Assembler::BlockConstPoolScope block_const_pool(masm);
1233 __ bind(&handler_entry);
1234 handler_offset_ = handler_entry.pos();
1235 // Caught exception: Store result (exception) in the pending exception
1236 // field in the JSEnv and return a failure sentinel. Coming in here the
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001237 // fp will be invalid because the PushStackHandler below sets it to 0 to
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001238 // signal the existence of the JSEntry frame.
1239 __ mov(ip, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
1240 isolate())));
1241 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001242 __ str(r0, MemOperand(ip));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001243 __ LoadRoot(r0, Heap::kExceptionRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001244 __ b(&exit);
1245
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001246 // Invoke: Link this frame into the handler chain.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001247 __ bind(&invoke);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001248 // Must preserve r0-r4, r5-r6 are available.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001249 __ PushStackHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001250 // If an exception not caught by another handler occurs, this handler
1251 // returns control to the code after the bl(&invoke) above, which
1252 // restores all kCalleeSaved registers (including cp and fp) to their
1253 // saved values before returning a failure to C.
1254
1255 // Clear any pending exceptions.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001256 __ mov(r5, Operand(isolate()->factory()->the_hole_value()));
Ben Murdoch589d6972011-11-30 16:04:58 +00001257 __ mov(ip, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001258 isolate())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001259 __ str(r5, MemOperand(ip));
1260
1261 // Invoke the function by calling through JS entry trampoline builtin.
1262 // Notice that we cannot store a reference to the trampoline code directly in
1263 // this stub, because runtime stubs are not traversed when doing GC.
1264
1265 // Expected registers by Builtins::JSEntryTrampoline
1266 // r0: code entry
1267 // r1: function
1268 // r2: receiver
1269 // r3: argc
1270 // r4: argv
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001271 if (type() == StackFrame::ENTRY_CONSTRUCT) {
Steve Block44f0eee2011-05-26 01:26:41 +01001272 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001273 isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001274 __ mov(ip, Operand(construct_entry));
1275 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001276 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001277 __ mov(ip, Operand(entry));
1278 }
1279 __ ldr(ip, MemOperand(ip)); // deref address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001280 __ add(ip, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001281
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001282 // Branch and link to JSEntryTrampoline.
1283 __ Call(ip);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001284
Steve Block053d10c2011-06-13 19:13:29 +01001285 // Unlink this frame from the handler chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001286 __ PopStackHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001287
1288 __ bind(&exit); // r0 holds result
Steve Block053d10c2011-06-13 19:13:29 +01001289 // Check if the current stack frame is marked as the outermost JS frame.
1290 Label non_outermost_js_2;
1291 __ pop(r5);
1292 __ cmp(r5, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1293 __ b(ne, &non_outermost_js_2);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001294 __ mov(r6, Operand::Zero());
Steve Block053d10c2011-06-13 19:13:29 +01001295 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
1296 __ str(r6, MemOperand(r5));
1297 __ bind(&non_outermost_js_2);
Steve Block053d10c2011-06-13 19:13:29 +01001298
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001299 // Restore the top frame descriptors from the stack.
1300 __ pop(r3);
Steve Block44f0eee2011-05-26 01:26:41 +01001301 __ mov(ip,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001302 Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001303 __ str(r3, MemOperand(ip));
1304
1305 // Reset the stack to the callee saved registers.
1306 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
1307
1308 // Restore callee-saved registers and return.
1309#ifdef DEBUG
1310 if (FLAG_debug_code) {
1311 __ mov(lr, Operand(pc));
1312 }
1313#endif
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01001314
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001315 // Restore callee-saved vfp registers.
1316 __ vldm(ia_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01001317
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001318 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
1319}
1320
1321
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001322void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
1323 Label miss;
1324 Register receiver = LoadDescriptor::ReceiverRegister();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001325 // Ensure that the vector and slot registers won't be clobbered before
1326 // calling the miss handler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001327 DCHECK(!AreAliased(r4, r5, LoadWithVectorDescriptor::VectorRegister(),
1328 LoadWithVectorDescriptor::SlotRegister()));
Steve Block1e0659c2011-05-24 12:43:12 +01001329
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001330 NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(masm, receiver, r4,
1331 r5, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001332 __ bind(&miss);
1333 PropertyAccessCompiler::TailCallBuiltin(
1334 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
1335}
Steve Block1e0659c2011-05-24 12:43:12 +01001336
1337
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001338void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
1339 // Return address is in lr.
1340 Label miss;
1341
1342 Register receiver = LoadDescriptor::ReceiverRegister();
1343 Register index = LoadDescriptor::NameRegister();
1344 Register scratch = r5;
1345 Register result = r0;
1346 DCHECK(!scratch.is(receiver) && !scratch.is(index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001347 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()) &&
1348 result.is(LoadWithVectorDescriptor::SlotRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001349
1350 // StringCharAtGenerator doesn't use the result register until it's passed
1351 // the different miss possibilities. If it did, we would have a conflict
1352 // when FLAG_vector_ics is true.
1353 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
1354 &miss, // When not a string.
1355 &miss, // When not a number.
1356 &miss, // When index out of range.
1357 STRING_INDEX_IS_ARRAY_INDEX,
1358 RECEIVER_IS_STRING);
1359 char_at_generator.GenerateFast(masm);
1360 __ Ret();
1361
1362 StubRuntimeCallHelper call_helper;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001363 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001364
1365 __ bind(&miss);
1366 PropertyAccessCompiler::TailCallBuiltin(
1367 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
1368}
1369
1370
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001371void RegExpExecStub::Generate(MacroAssembler* masm) {
1372 // Just jump directly to runtime if native RegExp is not selected at compile
1373 // time or if regexp entry in generated code is turned off runtime switch or
1374 // at compilation.
1375#ifdef V8_INTERPRETED_REGEXP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001376 __ TailCallRuntime(Runtime::kRegExpExec);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001377#else // V8_INTERPRETED_REGEXP
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001378
1379 // Stack frame on entry.
1380 // sp[0]: last_match_info (expected JSArray)
1381 // sp[4]: previous index
1382 // sp[8]: subject string
1383 // sp[12]: JSRegExp object
1384
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001385 const int kLastMatchInfoOffset = 0 * kPointerSize;
1386 const int kPreviousIndexOffset = 1 * kPointerSize;
1387 const int kSubjectOffset = 2 * kPointerSize;
1388 const int kJSRegExpOffset = 3 * kPointerSize;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001389
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001390 Label runtime;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001391 // Allocation of registers for this function. These are in callee save
1392 // registers and will be preserved by the call to the native RegExp code, as
1393 // this code is called using the normal C calling convention. When calling
1394 // directly from generated code the native RegExp code will not do a GC and
1395 // therefore the content of these registers are safe to use after the call.
1396 Register subject = r4;
1397 Register regexp_data = r5;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001398 Register last_match_info_elements = no_reg; // will be r6;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001399
1400 // Ensure that a RegExp stack is allocated.
1401 ExternalReference address_of_regexp_stack_memory_address =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001402 ExternalReference::address_of_regexp_stack_memory_address(isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001403 ExternalReference address_of_regexp_stack_memory_size =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001404 ExternalReference::address_of_regexp_stack_memory_size(isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001405 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
1406 __ ldr(r0, MemOperand(r0, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001407 __ cmp(r0, Operand::Zero());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001408 __ b(eq, &runtime);
1409
1410 // Check that the first argument is a JSRegExp object.
1411 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001412 __ JumpIfSmi(r0, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001413 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
1414 __ b(ne, &runtime);
1415
1416 // Check that the RegExp has been compiled (data contains a fixed array).
1417 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
1418 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001419 __ SmiTst(regexp_data);
1420 __ Check(ne, kUnexpectedTypeForRegExpDataFixedArrayExpected);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001421 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001422 __ Check(eq, kUnexpectedTypeForRegExpDataFixedArrayExpected);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001423 }
1424
1425 // regexp_data: RegExp data (FixedArray)
1426 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1427 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
1428 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
1429 __ b(ne, &runtime);
1430
1431 // regexp_data: RegExp data (FixedArray)
1432 // Check that the number of captures fit in the static offsets vector buffer.
1433 __ ldr(r2,
1434 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001435 // Check (number_of_captures + 1) * 2 <= offsets vector size
1436 // Or number_of_captures * 2 <= offsets vector size - 2
1437 // Multiplying by 2 comes for free since r2 is smi-tagged.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001438 STATIC_ASSERT(kSmiTag == 0);
1439 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001440 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1441 __ cmp(r2, Operand(Isolate::kJSRegexpStaticOffsetsVectorSize - 2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001442 __ b(hi, &runtime);
1443
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001444 // Reset offset for possibly sliced string.
1445 __ mov(r9, Operand::Zero());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001446 __ ldr(subject, MemOperand(sp, kSubjectOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001447 __ JumpIfSmi(subject, &runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001448 __ mov(r3, subject); // Make a copy of the original subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001449 // subject: subject string
1450 // r3: subject string
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001451 // regexp_data: RegExp data (FixedArray)
1452 // Handle subject string according to its encoding and representation:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001453 // (1) Sequential string? If yes, go to (4).
1454 // (2) Sequential or cons? If not, go to (5).
1455 // (3) Cons string. If the string is flat, replace subject with first string
1456 // and go to (1). Otherwise bail out to runtime.
1457 // (4) Sequential string. Load regexp code according to encoding.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001458 // (E) Carry on.
1459 /// [...]
1460
1461 // Deferred code at the end of the stub:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001462 // (5) Long external string? If not, go to (7).
1463 // (6) External string. Make it, offset-wise, look like a sequential string.
1464 // Go to (4).
1465 // (7) Short external string or not a string? If yes, bail out to runtime.
1466 // (8) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001467
Ben Murdoch097c5b22016-05-18 11:27:45 +01001468 Label seq_string /* 4 */, external_string /* 6 */, check_underlying /* 1 */,
1469 not_seq_nor_cons /* 5 */, not_long_external /* 7 */;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001470
Ben Murdoch097c5b22016-05-18 11:27:45 +01001471 __ bind(&check_underlying);
1472 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
1473 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
1474
1475 // (1) Sequential string? If yes, go to (4).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001476 __ and_(r1,
1477 r0,
1478 Operand(kIsNotStringMask |
1479 kStringRepresentationMask |
1480 kShortExternalStringMask),
1481 SetCC);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001482 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001483 __ b(eq, &seq_string); // Go to (4).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001484
Ben Murdoch097c5b22016-05-18 11:27:45 +01001485 // (2) Sequential or cons? If not, go to (5).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001486 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1487 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001488 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1489 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001490 __ cmp(r1, Operand(kExternalStringTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001491 __ b(ge, &not_seq_nor_cons); // Go to (5).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001492
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001493 // (3) Cons string. Check that it's flat.
1494 // Replace subject with first string and reload instance type.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001495 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001496 __ CompareRoot(r0, Heap::kempty_stringRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001497 __ b(ne, &runtime);
1498 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001499 __ jmp(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001500
Ben Murdoch097c5b22016-05-18 11:27:45 +01001501 // (4) Sequential string. Load regexp code according to encoding.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001502 __ bind(&seq_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001503 // subject: sequential subject string (or look-alike, external string)
1504 // r3: original subject string
1505 // Load previous index and check range before r3 is overwritten. We have to
1506 // use r3 instead of subject here because subject might have been only made
1507 // to look like a sequential string when it actually is an external string.
1508 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
1509 __ JumpIfNotSmi(r1, &runtime);
1510 __ ldr(r3, FieldMemOperand(r3, String::kLengthOffset));
1511 __ cmp(r3, Operand(r1));
1512 __ b(ls, &runtime);
1513 __ SmiUntag(r1);
1514
1515 STATIC_ASSERT(4 == kOneByteStringTag);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001516 STATIC_ASSERT(kTwoByteStringTag == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001517 __ and_(r0, r0, Operand(kStringEncodingMask));
1518 __ mov(r3, Operand(r0, ASR, 2), SetCC);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001519 __ ldr(r6, FieldMemOperand(regexp_data, JSRegExp::kDataOneByteCodeOffset),
1520 ne);
1521 __ ldr(r6, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001522
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523 // (E) Carry on. String handling is done.
1524 // r6: irregexp code
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001525 // Check that the irregexp code has been generated for the actual string
1526 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00001527 // a smi (code flushing support).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001528 __ JumpIfSmi(r6, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001529
1530 // r1: previous index
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001531 // r3: encoding of subject string (1 if one_byte, 0 if two_byte);
1532 // r6: code
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001533 // subject: Subject string
1534 // regexp_data: RegExp data (FixedArray)
1535 // All checks done. Now push arguments for native regexp code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001536 __ IncrementCounter(isolate()->counters()->regexp_entry_native(), 1, r0, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001537
Steve Block44f0eee2011-05-26 01:26:41 +01001538 // Isolates: note we add an additional parameter here (isolate pointer).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001539 const int kRegExpExecuteArguments = 9;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001540 const int kParameterRegisters = 4;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001541 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001542
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001543 // Stack pointer now points to cell where return address is to be written.
1544 // Arguments are before that on the stack or in registers.
1545
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001546 // Argument 9 (sp[20]): Pass current isolate address.
1547 __ mov(r0, Operand(ExternalReference::isolate_address(isolate())));
1548 __ str(r0, MemOperand(sp, 5 * kPointerSize));
1549
1550 // Argument 8 (sp[16]): Indicate that this is a direct call from JavaScript.
1551 __ mov(r0, Operand(1));
Steve Block44f0eee2011-05-26 01:26:41 +01001552 __ str(r0, MemOperand(sp, 4 * kPointerSize));
1553
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001554 // Argument 7 (sp[12]): Start (high end) of backtracking stack memory area.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001555 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
1556 __ ldr(r0, MemOperand(r0, 0));
1557 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
1558 __ ldr(r2, MemOperand(r2, 0));
1559 __ add(r0, r0, Operand(r2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001560 __ str(r0, MemOperand(sp, 3 * kPointerSize));
1561
1562 // Argument 6: Set the number of capture registers to zero to force global
1563 // regexps to behave as non-global. This does not affect non-global regexps.
1564 __ mov(r0, Operand::Zero());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001565 __ str(r0, MemOperand(sp, 2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001566
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001567 // Argument 5 (sp[4]): static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01001568 __ mov(r0,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001569 Operand(ExternalReference::address_of_static_offsets_vector(
1570 isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001571 __ str(r0, MemOperand(sp, 1 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001572
1573 // For arguments 4 and 3 get string length, calculate start of string data and
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001574 // calculate the shift of the index (0 for one-byte and 1 for two-byte).
1575 __ add(r7, subject, Operand(SeqString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001576 __ eor(r3, r3, Operand(1));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001577 // Load the length from the original subject string from the previous stack
1578 // frame. Therefore we have to use fp, which points exactly to two pointer
1579 // sizes below the previous sp. (Because creating a new stack frame pushes
1580 // the previous fp onto the stack and moves up sp by 2 * kPointerSize.)
Ben Murdoch589d6972011-11-30 16:04:58 +00001581 __ ldr(subject, MemOperand(fp, kSubjectOffset + 2 * kPointerSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001582 // If slice offset is not 0, load the length from the original sliced string.
1583 // Argument 4, r3: End of string data
1584 // Argument 3, r2: Start of string data
1585 // Prepare start and end index of the input.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001586 __ add(r9, r7, Operand(r9, LSL, r3));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001587 __ add(r2, r9, Operand(r1, LSL, r3));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001588
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001589 __ ldr(r7, FieldMemOperand(subject, String::kLengthOffset));
1590 __ SmiUntag(r7);
1591 __ add(r3, r9, Operand(r7, LSL, r3));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001592
1593 // Argument 2 (r1): Previous index.
1594 // Already there
1595
1596 // Argument 1 (r0): Subject string.
Ben Murdoch589d6972011-11-30 16:04:58 +00001597 __ mov(r0, subject);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001598
1599 // Locate the code entry and call it.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001600 __ add(r6, r6, Operand(Code::kHeaderSize - kHeapObjectTag));
1601 DirectCEntryStub stub(isolate());
1602 stub.GenerateCall(masm, r6);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001603
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001604 __ LeaveExitFrame(false, no_reg, true);
1605
1606 last_match_info_elements = r6;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001607
1608 // r0: result
1609 // subject: subject string (callee saved)
1610 // regexp_data: RegExp data (callee saved)
1611 // last_match_info_elements: Last match info elements (callee saved)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001612 // Check the result.
1613 Label success;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001614 __ cmp(r0, Operand(1));
1615 // We expect exactly one result since we force the called regexp to behave
1616 // as non-global.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001617 __ b(eq, &success);
1618 Label failure;
Ben Murdoch589d6972011-11-30 16:04:58 +00001619 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001620 __ b(eq, &failure);
Ben Murdoch589d6972011-11-30 16:04:58 +00001621 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001622 // If not exception it can only be retry. Handle that in the runtime system.
1623 __ b(ne, &runtime);
1624 // Result must now be exception. If there is no pending exception already a
1625 // stack overflow (on the backtrack stack) was detected in RegExp code but
1626 // haven't created the exception yet. Handle that in the runtime system.
1627 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001628 __ mov(r1, Operand(isolate()->factory()->the_hole_value()));
Ben Murdoch589d6972011-11-30 16:04:58 +00001629 __ mov(r2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001630 isolate())));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001631 __ ldr(r0, MemOperand(r2, 0));
Ben Murdoch589d6972011-11-30 16:04:58 +00001632 __ cmp(r0, r1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001633 __ b(eq, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001634
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001635 // For exception, throw the exception again.
1636 __ TailCallRuntime(Runtime::kRegExpExecReThrow);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001637
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001638 __ bind(&failure);
1639 // For failure and exception return null.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001640 __ mov(r0, Operand(isolate()->factory()->null_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001641 __ add(sp, sp, Operand(4 * kPointerSize));
1642 __ Ret();
1643
1644 // Process the result from the native regexp code.
1645 __ bind(&success);
1646 __ ldr(r1,
1647 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
1648 // Calculate number of capture registers (number_of_captures + 1) * 2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001649 // Multiplying by 2 comes for free since r1 is smi-tagged.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001650 STATIC_ASSERT(kSmiTag == 0);
1651 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1652 __ add(r1, r1, Operand(2)); // r1 was a smi.
1653
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001654 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
1655 __ JumpIfSmi(r0, &runtime);
1656 __ CompareObjectType(r0, r2, r2, JS_ARRAY_TYPE);
1657 __ b(ne, &runtime);
1658 // Check that the JSArray is in fast case.
1659 __ ldr(last_match_info_elements,
1660 FieldMemOperand(r0, JSArray::kElementsOffset));
1661 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
1662 __ CompareRoot(r0, Heap::kFixedArrayMapRootIndex);
1663 __ b(ne, &runtime);
1664 // Check that the last match info has space for the capture registers and the
1665 // additional information.
1666 __ ldr(r0,
1667 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
1668 __ add(r2, r1, Operand(RegExpImpl::kLastMatchOverhead));
1669 __ cmp(r2, Operand::SmiUntag(r0));
1670 __ b(gt, &runtime);
1671
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001672 // r1: number of capture registers
1673 // r4: subject string
1674 // Store the capture count.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001675 __ SmiTag(r2, r1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001676 __ str(r2, FieldMemOperand(last_match_info_elements,
1677 RegExpImpl::kLastCaptureCountOffset));
1678 // Store last subject and last input.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001679 __ str(subject,
1680 FieldMemOperand(last_match_info_elements,
1681 RegExpImpl::kLastSubjectOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001682 __ mov(r2, subject);
1683 __ RecordWriteField(last_match_info_elements,
1684 RegExpImpl::kLastSubjectOffset,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001685 subject,
1686 r3,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001687 kLRHasNotBeenSaved,
1688 kDontSaveFPRegs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001689 __ mov(subject, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001690 __ str(subject,
1691 FieldMemOperand(last_match_info_elements,
1692 RegExpImpl::kLastInputOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001693 __ RecordWriteField(last_match_info_elements,
1694 RegExpImpl::kLastInputOffset,
1695 subject,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001696 r3,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001697 kLRHasNotBeenSaved,
1698 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001699
1700 // Get the static offsets vector filled by the native regexp code.
1701 ExternalReference address_of_static_offsets_vector =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001702 ExternalReference::address_of_static_offsets_vector(isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001703 __ mov(r2, Operand(address_of_static_offsets_vector));
1704
1705 // r1: number of capture registers
1706 // r2: offsets vector
1707 Label next_capture, done;
1708 // Capture register counter starts from number of capture registers and
1709 // counts down until wraping after zero.
1710 __ add(r0,
1711 last_match_info_elements,
1712 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
1713 __ bind(&next_capture);
1714 __ sub(r1, r1, Operand(1), SetCC);
1715 __ b(mi, &done);
1716 // Read the value from the static offsets vector buffer.
1717 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
1718 // Store the smi value in the last match info.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001719 __ SmiTag(r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001720 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
1721 __ jmp(&next_capture);
1722 __ bind(&done);
1723
1724 // Return last match info.
1725 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
1726 __ add(sp, sp, Operand(4 * kPointerSize));
1727 __ Ret();
1728
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001729 // Do the runtime call to execute the regexp.
1730 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001731 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001732
1733 // Deferred code for string handling.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001734 // (5) Long external string? If not, go to (7).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001735 __ bind(&not_seq_nor_cons);
1736 // Compare flags are still set.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001737 __ b(gt, &not_long_external); // Go to (7).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001738
Ben Murdoch097c5b22016-05-18 11:27:45 +01001739 // (6) External string. Make it, offset-wise, look like a sequential string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001740 __ bind(&external_string);
1741 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
1742 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
1743 if (FLAG_debug_code) {
1744 // Assert that we do not have a cons or slice (indirect strings) here.
1745 // Sequential strings have already been ruled out.
1746 __ tst(r0, Operand(kIsIndirectStringMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001747 __ Assert(eq, kExternalStringExpectedButNotFound);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001748 }
1749 __ ldr(subject,
1750 FieldMemOperand(subject, ExternalString::kResourceDataOffset));
1751 // Move the pointer so that offset-wise, it looks like a sequential string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001752 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001753 __ sub(subject,
1754 subject,
1755 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001756 __ jmp(&seq_string); // Go to (4).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001757
Ben Murdoch097c5b22016-05-18 11:27:45 +01001758 // (7) Short external string or not a string? If yes, bail out to runtime.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001759 __ bind(&not_long_external);
1760 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1761 __ tst(r1, Operand(kIsNotStringMask | kShortExternalStringMask));
1762 __ b(ne, &runtime);
1763
Ben Murdoch097c5b22016-05-18 11:27:45 +01001764 // (8) Sliced string. Replace subject with parent. Go to (4).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001765 // Load offset into r9 and replace subject string with parent.
1766 __ ldr(r9, FieldMemOperand(subject, SlicedString::kOffsetOffset));
1767 __ SmiUntag(r9);
1768 __ ldr(subject, FieldMemOperand(subject, SlicedString::kParentOffset));
1769 __ jmp(&check_underlying); // Go to (4).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001770#endif // V8_INTERPRETED_REGEXP
1771}
1772
1773
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001774static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
1775 // r0 : number of arguments to the construct function
1776 // r1 : the function to call
1777 // r2 : feedback vector
1778 // r3 : slot in feedback vector (Smi)
1779 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
1780
1781 // Number-of-arguments register must be smi-tagged to call out.
1782 __ SmiTag(r0);
1783 __ Push(r3, r2, r1, r0);
1784
1785 __ CallStub(stub);
1786
1787 __ Pop(r3, r2, r1, r0);
1788 __ SmiUntag(r0);
1789}
1790
1791
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001792static void GenerateRecordCallTarget(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001793 // Cache the called function in a feedback vector slot. Cache states
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001794 // are uninitialized, monomorphic (indicated by a JSFunction), and
1795 // megamorphic.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001796 // r0 : number of arguments to the construct function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001797 // r1 : the function to call
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001798 // r2 : feedback vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001799 // r3 : slot in feedback vector (Smi)
1800 Label initialize, done, miss, megamorphic, not_array_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001801
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001802 DCHECK_EQ(*TypeFeedbackVector::MegamorphicSentinel(masm->isolate()),
1803 masm->isolate()->heap()->megamorphic_symbol());
1804 DCHECK_EQ(*TypeFeedbackVector::UninitializedSentinel(masm->isolate()),
1805 masm->isolate()->heap()->uninitialized_symbol());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001806
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001807 // Load the cache state into r5.
1808 __ add(r5, r2, Operand::PointerOffsetFromSmiKey(r3));
1809 __ ldr(r5, FieldMemOperand(r5, FixedArray::kHeaderSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001810
1811 // A monomorphic cache hit or an already megamorphic state: invoke the
1812 // function without changing the state.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001813 // We don't know if r5 is a WeakCell or a Symbol, but it's harmless to read at
1814 // this position in a symbol (see static asserts in type-feedback-vector.h).
1815 Label check_allocation_site;
1816 Register feedback_map = r6;
1817 Register weak_value = r9;
1818 __ ldr(weak_value, FieldMemOperand(r5, WeakCell::kValueOffset));
1819 __ cmp(r1, weak_value);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001820 __ b(eq, &done);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001821 __ CompareRoot(r5, Heap::kmegamorphic_symbolRootIndex);
1822 __ b(eq, &done);
1823 __ ldr(feedback_map, FieldMemOperand(r5, HeapObject::kMapOffset));
1824 __ CompareRoot(feedback_map, Heap::kWeakCellMapRootIndex);
1825 __ b(ne, &check_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001826
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001827 // If the weak cell is cleared, we have a new chance to become monomorphic.
1828 __ JumpIfSmi(weak_value, &initialize);
1829 __ jmp(&megamorphic);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001830
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001831 __ bind(&check_allocation_site);
1832 // If we came here, we need to see if we are the array function.
1833 // If we didn't have a matching function, and we didn't find the megamorph
1834 // sentinel, then we have in the slot either some other function or an
1835 // AllocationSite.
1836 __ CompareRoot(feedback_map, Heap::kAllocationSiteMapRootIndex);
1837 __ b(ne, &miss);
1838
1839 // Make sure the function is the Array() function
1840 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, r5);
1841 __ cmp(r1, r5);
1842 __ b(ne, &megamorphic);
1843 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001844
1845 __ bind(&miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001846
1847 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1848 // megamorphic.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001849 __ CompareRoot(r5, Heap::kuninitialized_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001850 __ b(eq, &initialize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001851 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1852 // write-barrier is needed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001853 __ bind(&megamorphic);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001854 __ add(r5, r2, Operand::PointerOffsetFromSmiKey(r3));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001855 __ LoadRoot(ip, Heap::kmegamorphic_symbolRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001856 __ str(ip, FieldMemOperand(r5, FixedArray::kHeaderSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001857 __ jmp(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001858
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001859 // An uninitialized cache is patched with the function
1860 __ bind(&initialize);
1861
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001862 // Make sure the function is the Array() function
1863 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, r5);
1864 __ cmp(r1, r5);
1865 __ b(ne, &not_array_function);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001866
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001867 // The target function is the Array constructor,
1868 // Create an AllocationSite if we don't already have it, store it in the
1869 // slot.
1870 CreateAllocationSiteStub create_stub(masm->isolate());
1871 CallStubInRecordCallTarget(masm, &create_stub);
1872 __ b(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001873
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001874 __ bind(&not_array_function);
1875 CreateWeakCellStub weak_cell_stub(masm->isolate());
1876 CallStubInRecordCallTarget(masm, &weak_cell_stub);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001877 __ bind(&done);
1878}
1879
1880
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001881void CallConstructStub::Generate(MacroAssembler* masm) {
1882 // r0 : number of arguments
1883 // r1 : the function to call
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001884 // r2 : feedback vector
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001885 // r3 : slot in feedback vector (Smi, for RecordCallTarget)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001886
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001887 Label non_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001888 // Check that the function is not a smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001889 __ JumpIfSmi(r1, &non_function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001890 // Check that the function is a JSFunction.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001891 __ CompareObjectType(r1, r5, r5, JS_FUNCTION_TYPE);
1892 __ b(ne, &non_function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001893
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001894 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001895
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001896 __ add(r5, r2, Operand::PointerOffsetFromSmiKey(r3));
1897 Label feedback_register_initialized;
1898 // Put the AllocationSite from the feedback vector into r2, or undefined.
1899 __ ldr(r2, FieldMemOperand(r5, FixedArray::kHeaderSize));
1900 __ ldr(r5, FieldMemOperand(r2, AllocationSite::kMapOffset));
1901 __ CompareRoot(r5, Heap::kAllocationSiteMapRootIndex);
1902 __ b(eq, &feedback_register_initialized);
1903 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1904 __ bind(&feedback_register_initialized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001905
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001906 __ AssertUndefinedOrAllocationSite(r2, r5);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001907
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001908 // Pass function as new target.
1909 __ mov(r3, r1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001910
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001911 // Tail call to the function-specific construct stub (still in the caller
1912 // context at this point).
1913 __ ldr(r4, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
1914 __ ldr(r4, FieldMemOperand(r4, SharedFunctionInfo::kConstructStubOffset));
1915 __ add(pc, r4, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001916
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001917 __ bind(&non_function);
1918 __ mov(r3, r1);
1919 __ Jump(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001920}
1921
1922
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001923void CallICStub::HandleArrayCase(MacroAssembler* masm, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001924 // r1 - function
1925 // r3 - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001926 // r2 - vector
1927 // r4 - allocation site (loaded from vector[slot])
1928 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, r5);
1929 __ cmp(r1, r5);
1930 __ b(ne, miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001931
1932 __ mov(r0, Operand(arg_count()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001933
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001934 // Increment the call count for monomorphic function calls.
1935 __ add(r2, r2, Operand::PointerOffsetFromSmiKey(r3));
1936 __ add(r2, r2, Operand(FixedArray::kHeaderSize + kPointerSize));
1937 __ ldr(r3, FieldMemOperand(r2, 0));
1938 __ add(r3, r3, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
1939 __ str(r3, FieldMemOperand(r2, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001940
1941 __ mov(r2, r4);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001942 __ mov(r3, r1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001943 ArrayConstructorStub stub(masm->isolate(), arg_count());
1944 __ TailCallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001945}
1946
1947
1948void CallICStub::Generate(MacroAssembler* masm) {
1949 // r1 - function
1950 // r3 - slot id (Smi)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001951 // r2 - vector
1952 Label extra_checks_or_miss, call, call_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001953 int argc = arg_count();
1954 ParameterCount actual(argc);
1955
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001956 // The checks. First, does r1 match the recorded monomorphic target?
1957 __ add(r4, r2, Operand::PointerOffsetFromSmiKey(r3));
1958 __ ldr(r4, FieldMemOperand(r4, FixedArray::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001959
1960 // We don't know that we have a weak cell. We might have a private symbol
1961 // or an AllocationSite, but the memory is safe to examine.
1962 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
1963 // FixedArray.
1964 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
1965 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
1966 // computed, meaning that it can't appear to be a pointer. If the low bit is
1967 // 0, then hash is computed, but the 0 bit prevents the field from appearing
1968 // to be a pointer.
1969 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
1970 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
1971 WeakCell::kValueOffset &&
1972 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
1973
1974 __ ldr(r5, FieldMemOperand(r4, WeakCell::kValueOffset));
1975 __ cmp(r1, r5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001976 __ b(ne, &extra_checks_or_miss);
1977
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001978 // The compare above could have been a SMI/SMI comparison. Guard against this
1979 // convincing us that we have a monomorphic JSFunction.
1980 __ JumpIfSmi(r1, &extra_checks_or_miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001981
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001982 // Increment the call count for monomorphic function calls.
1983 __ add(r2, r2, Operand::PointerOffsetFromSmiKey(r3));
1984 __ add(r2, r2, Operand(FixedArray::kHeaderSize + kPointerSize));
1985 __ ldr(r3, FieldMemOperand(r2, 0));
1986 __ add(r3, r3, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
1987 __ str(r3, FieldMemOperand(r2, 0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001988
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001989 __ bind(&call_function);
1990 __ mov(r0, Operand(argc));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001991 __ Jump(masm->isolate()->builtins()->CallFunction(convert_mode(),
1992 tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001993 RelocInfo::CODE_TARGET);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001994
1995 __ bind(&extra_checks_or_miss);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001996 Label uninitialized, miss, not_allocation_site;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001997
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001998 __ CompareRoot(r4, Heap::kmegamorphic_symbolRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001999 __ b(eq, &call);
2000
2001 // Verify that r4 contains an AllocationSite
2002 __ ldr(r5, FieldMemOperand(r4, HeapObject::kMapOffset));
2003 __ CompareRoot(r5, Heap::kAllocationSiteMapRootIndex);
2004 __ b(ne, &not_allocation_site);
2005
2006 // We have an allocation site.
2007 HandleArrayCase(masm, &miss);
2008
2009 __ bind(&not_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002010
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002011 // The following cases attempt to handle MISS cases without going to the
2012 // runtime.
2013 if (FLAG_trace_ic) {
2014 __ jmp(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002015 }
2016
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002017 __ CompareRoot(r4, Heap::kuninitialized_symbolRootIndex);
2018 __ b(eq, &uninitialized);
2019
2020 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2021 // to handle it here. More complex cases are dealt with in the runtime.
2022 __ AssertNotSmi(r4);
2023 __ CompareObjectType(r4, r5, r5, JS_FUNCTION_TYPE);
2024 __ b(ne, &miss);
2025 __ add(r4, r2, Operand::PointerOffsetFromSmiKey(r3));
2026 __ LoadRoot(ip, Heap::kmegamorphic_symbolRootIndex);
2027 __ str(ip, FieldMemOperand(r4, FixedArray::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002028
2029 __ bind(&call);
2030 __ mov(r0, Operand(argc));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002031 __ Jump(masm->isolate()->builtins()->Call(convert_mode(), tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002032 RelocInfo::CODE_TARGET);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002033
2034 __ bind(&uninitialized);
2035
2036 // We are going monomorphic, provided we actually have a JSFunction.
2037 __ JumpIfSmi(r1, &miss);
2038
2039 // Goto miss case if we do not have a function.
2040 __ CompareObjectType(r1, r4, r4, JS_FUNCTION_TYPE);
2041 __ b(ne, &miss);
2042
2043 // Make sure the function is not the Array() function, which requires special
2044 // behavior on MISS.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002045 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, r4);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002046 __ cmp(r1, r4);
2047 __ b(eq, &miss);
2048
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002049 // Make sure the function belongs to the same native context.
2050 __ ldr(r4, FieldMemOperand(r1, JSFunction::kContextOffset));
2051 __ ldr(r4, ContextMemOperand(r4, Context::NATIVE_CONTEXT_INDEX));
2052 __ ldr(ip, NativeContextMemOperand());
2053 __ cmp(r4, ip);
2054 __ b(ne, &miss);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002055
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002056 // Initialize the call counter.
2057 __ Move(r5, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002058 __ add(r4, r2, Operand::PointerOffsetFromSmiKey(r3));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002059 __ str(r5, FieldMemOperand(r4, FixedArray::kHeaderSize + kPointerSize));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002060
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002061 // Store the function. Use a stub since we need a frame for allocation.
2062 // r2 - vector
2063 // r3 - slot
2064 // r1 - function
2065 {
2066 FrameScope scope(masm, StackFrame::INTERNAL);
2067 CreateWeakCellStub create_stub(masm->isolate());
2068 __ Push(r1);
2069 __ CallStub(&create_stub);
2070 __ Pop(r1);
2071 }
2072
2073 __ jmp(&call_function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002074
2075 // We are here because tracing is on or we encountered a MISS case we can't
2076 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002077 __ bind(&miss);
2078 GenerateMiss(masm);
2079
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002080 __ jmp(&call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002081}
2082
2083
2084void CallICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002085 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002086
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002087 // Push the receiver and the function and feedback info.
2088 __ Push(r1, r2, r3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002089
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002090 // Call the entry.
2091 __ CallRuntime(Runtime::kCallIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002092
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002093 // Move result to edi and exit the internal frame.
2094 __ mov(r1, r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002095}
2096
2097
2098// StringCharCodeAtGenerator
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002099void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002100 // If the receiver is a smi trigger the non-string case.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002101 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2102 __ JumpIfSmi(object_, receiver_not_string_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002103
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002104 // Fetch the instance type of the receiver into result register.
2105 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
2106 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
2107 // If the receiver is not a string trigger the non-string case.
2108 __ tst(result_, Operand(kIsNotStringMask));
2109 __ b(ne, receiver_not_string_);
2110 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002111
2112 // If the index is non-smi trigger the non-smi case.
Steve Block1e0659c2011-05-24 12:43:12 +01002113 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002114 __ bind(&got_smi_index_);
2115
2116 // Check for index out of range.
2117 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002118 __ cmp(ip, Operand(index_));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002119 __ b(ls, index_out_of_range_);
2120
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002121 __ SmiUntag(index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002122
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002123 StringCharLoadGenerator::Generate(masm,
2124 object_,
2125 index_,
2126 result_,
2127 &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002128
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002129 __ SmiTag(result_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002130 __ bind(&exit_);
2131}
2132
2133
2134void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002135 MacroAssembler* masm, EmbedMode embed_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002136 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002137 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002138
2139 // Index is not a smi.
2140 __ bind(&index_not_smi_);
2141 // If index is a heap number, try converting it to an integer.
2142 __ CheckMap(index_,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002143 result_,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002144 Heap::kHeapNumberMapRootIndex,
2145 index_not_number_,
Ben Murdoch257744e2011-11-30 15:57:28 +00002146 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002147 call_helper.BeforeCall(masm);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002148 if (embed_mode == PART_OF_IC_HANDLER) {
2149 __ Push(LoadWithVectorDescriptor::VectorRegister(),
2150 LoadWithVectorDescriptor::SlotRegister(), object_, index_);
2151 } else {
2152 // index_ is consumed by runtime conversion function.
2153 __ Push(object_, index_);
2154 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002155 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002156 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002157 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002158 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002159 // NumberToSmi discards numbers that are not exact integers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002160 __ CallRuntime(Runtime::kNumberToSmi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002161 }
2162 // Save the conversion result before the pop instructions below
2163 // have a chance to overwrite it.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002164 __ Move(index_, r0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002165 if (embed_mode == PART_OF_IC_HANDLER) {
2166 __ Pop(LoadWithVectorDescriptor::VectorRegister(),
2167 LoadWithVectorDescriptor::SlotRegister(), object_);
2168 } else {
2169 __ pop(object_);
2170 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002171 // Reload the instance type.
2172 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
2173 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
2174 call_helper.AfterCall(masm);
2175 // If index is still not a smi, it must be out of range.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002176 __ JumpIfNotSmi(index_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002177 // Otherwise, return to the fast path.
2178 __ jmp(&got_smi_index_);
2179
2180 // Call runtime. We get here when the receiver is a string and the
2181 // index is a number, but the code of getting the actual character
2182 // is too complex (e.g., when the string needs to be flattened).
2183 __ bind(&call_runtime_);
2184 call_helper.BeforeCall(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002185 __ SmiTag(index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002186 __ Push(object_, index_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002187 __ CallRuntime(Runtime::kStringCharCodeAtRT);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002188 __ Move(result_, r0);
2189 call_helper.AfterCall(masm);
2190 __ jmp(&exit_);
2191
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002192 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002193}
2194
2195
2196// -------------------------------------------------------------------------
2197// StringCharFromCodeGenerator
2198
2199void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
2200 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2201 STATIC_ASSERT(kSmiTag == 0);
2202 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002203 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
2204 __ tst(code_, Operand(kSmiTagMask |
2205 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
Steve Block1e0659c2011-05-24 12:43:12 +01002206 __ b(ne, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002207
2208 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002209 // At this point code register contains smi tagged one-byte char code.
2210 __ add(result_, result_, Operand::PointerOffsetFromSmiKey(code_));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002211 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002212 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002213 __ b(eq, &slow_case_);
2214 __ bind(&exit_);
2215}
2216
2217
2218void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002219 MacroAssembler* masm,
2220 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002221 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002222
2223 __ bind(&slow_case_);
2224 call_helper.BeforeCall(masm);
2225 __ push(code_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002226 __ CallRuntime(Runtime::kStringCharFromCode);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002227 __ Move(result_, r0);
2228 call_helper.AfterCall(masm);
2229 __ jmp(&exit_);
2230
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002231 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002232}
2233
2234
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002235enum CopyCharactersFlags { COPY_ONE_BYTE = 1, DEST_ALWAYS_ALIGNED = 2 };
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002236
2237
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002238void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2239 Register dest,
2240 Register src,
2241 Register count,
2242 Register scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002243 String::Encoding encoding) {
2244 if (FLAG_debug_code) {
2245 // Check that destination is word aligned.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002246 __ tst(dest, Operand(kPointerAlignmentMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002247 __ Check(eq, kDestinationOfCopyNotAligned);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002248 }
2249
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002250 // Assumes word reads and writes are little endian.
2251 // Nothing to do for zero characters.
2252 Label done;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002253 if (encoding == String::TWO_BYTE_ENCODING) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002254 __ add(count, count, Operand(count), SetCC);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002255 }
2256
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002257 Register limit = count; // Read until dest equals this.
2258 __ add(limit, dest, Operand(count));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002259
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002260 Label loop_entry, loop;
2261 // Copy bytes from src to dest until dest hits limit.
2262 __ b(&loop_entry);
2263 __ bind(&loop);
2264 __ ldrb(scratch, MemOperand(src, 1, PostIndex), lt);
2265 __ strb(scratch, MemOperand(dest, 1, PostIndex));
2266 __ bind(&loop_entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002267 __ cmp(dest, Operand(limit));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002268 __ b(lt, &loop);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002269
2270 __ bind(&done);
2271}
2272
2273
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002274void SubStringStub::Generate(MacroAssembler* masm) {
2275 Label runtime;
2276
2277 // Stack frame on entry.
2278 // lr: return address
2279 // sp[0]: to
2280 // sp[4]: from
2281 // sp[8]: string
2282
2283 // This stub is called from the native-call %_SubString(...), so
2284 // nothing can be assumed about the arguments. It is tested that:
2285 // "string" is a sequential string,
2286 // both "from" and "to" are smis, and
2287 // 0 <= from <= to <= string.length.
2288 // If any of these assumptions fail, we call the runtime system.
2289
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002290 const int kToOffset = 0 * kPointerSize;
2291 const int kFromOffset = 1 * kPointerSize;
2292 const int kStringOffset = 2 * kPointerSize;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002293
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002294 __ Ldrd(r2, r3, MemOperand(sp, kToOffset));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002295 STATIC_ASSERT(kFromOffset == kToOffset + 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002296 STATIC_ASSERT(kSmiTag == 0);
2297 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002298
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002299 // Arithmetic shift right by one un-smi-tags. In this case we rotate right
2300 // instead because we bail out on non-smi values: ROR and ASR are equivalent
2301 // for smis but they set the flags in a way that's easier to optimize.
2302 __ mov(r2, Operand(r2, ROR, 1), SetCC);
2303 __ mov(r3, Operand(r3, ROR, 1), SetCC, cc);
2304 // If either to or from had the smi tag bit set, then C is set now, and N
2305 // has the same value: we rotated by 1, so the bottom bit is now the top bit.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002306 // We want to bailout to runtime here if From is negative. In that case, the
2307 // next instruction is not executed and we fall through to bailing out to
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002308 // runtime.
2309 // Executed if both r2 and r3 are untagged integers.
2310 __ sub(r2, r2, Operand(r3), SetCC, cc);
2311 // One of the above un-smis or the above SUB could have set N==1.
2312 __ b(mi, &runtime); // Either "from" or "to" is not an smi, or from > to.
Ben Murdoch85b71792012-04-11 18:30:58 +01002313
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002314 // Make sure first argument is a string.
Ben Murdoch589d6972011-11-30 16:04:58 +00002315 __ ldr(r0, MemOperand(sp, kStringOffset));
Ben Murdoch589d6972011-11-30 16:04:58 +00002316 __ JumpIfSmi(r0, &runtime);
2317 Condition is_string = masm->IsObjectStringType(r0, r1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002318 __ b(NegateCondition(is_string), &runtime);
2319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002320 Label single_char;
2321 __ cmp(r2, Operand(1));
2322 __ b(eq, &single_char);
2323
Ben Murdoch589d6972011-11-30 16:04:58 +00002324 // Short-cut for the case of trivial substring.
2325 Label return_r0;
2326 // r0: original string
2327 // r2: result string length
2328 __ ldr(r4, FieldMemOperand(r0, String::kLengthOffset));
2329 __ cmp(r2, Operand(r4, ASR, 1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002330 // Return original string.
Ben Murdoch589d6972011-11-30 16:04:58 +00002331 __ b(eq, &return_r0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002332 // Longer than original string's length or negative: unsafe arguments.
2333 __ b(hi, &runtime);
2334 // Shorter than original string's length: an actual substring.
Ben Murdoch589d6972011-11-30 16:04:58 +00002335
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002336 // Deal with different string types: update the index if necessary
2337 // and put the underlying string into r5.
2338 // r0: original string
2339 // r1: instance type
2340 // r2: length
2341 // r3: from index (untagged)
2342 Label underlying_unpacked, sliced_string, seq_or_external_string;
2343 // If the string is not indirect, it can only be sequential or external.
2344 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2345 STATIC_ASSERT(kIsIndirectStringMask != 0);
2346 __ tst(r1, Operand(kIsIndirectStringMask));
2347 __ b(eq, &seq_or_external_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002348
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002349 __ tst(r1, Operand(kSlicedNotConsMask));
2350 __ b(ne, &sliced_string);
2351 // Cons string. Check whether it is flat, then fetch first part.
2352 __ ldr(r5, FieldMemOperand(r0, ConsString::kSecondOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002353 __ CompareRoot(r5, Heap::kempty_stringRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002354 __ b(ne, &runtime);
2355 __ ldr(r5, FieldMemOperand(r0, ConsString::kFirstOffset));
2356 // Update instance type.
2357 __ ldr(r1, FieldMemOperand(r5, HeapObject::kMapOffset));
2358 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2359 __ jmp(&underlying_unpacked);
Ben Murdoch589d6972011-11-30 16:04:58 +00002360
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002361 __ bind(&sliced_string);
2362 // Sliced string. Fetch parent and correct start index by offset.
2363 __ ldr(r5, FieldMemOperand(r0, SlicedString::kParentOffset));
2364 __ ldr(r4, FieldMemOperand(r0, SlicedString::kOffsetOffset));
2365 __ add(r3, r3, Operand(r4, ASR, 1)); // Add offset to index.
2366 // Update instance type.
2367 __ ldr(r1, FieldMemOperand(r5, HeapObject::kMapOffset));
2368 __ ldrb(r1, FieldMemOperand(r1, Map::kInstanceTypeOffset));
2369 __ jmp(&underlying_unpacked);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002370
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002371 __ bind(&seq_or_external_string);
2372 // Sequential or external string. Just move string to the expected register.
2373 __ mov(r5, r0);
2374
2375 __ bind(&underlying_unpacked);
2376
2377 if (FLAG_string_slices) {
2378 Label copy_routine;
2379 // r5: underlying subject string
2380 // r1: instance type of underlying subject string
2381 // r2: length
2382 // r3: adjusted start index (untagged)
2383 __ cmp(r2, Operand(SlicedString::kMinLength));
2384 // Short slice. Copy instead of slicing.
2385 __ b(lt, &copy_routine);
2386 // Allocate new sliced string. At this point we do not reload the instance
2387 // type including the string encoding because we simply rely on the info
2388 // provided by the original string. It does not matter if the original
2389 // string's encoding is wrong because we always have to recheck encoding of
2390 // the newly created string's parent anyways due to externalized strings.
2391 Label two_byte_slice, set_slice_header;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002392 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002393 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2394 __ tst(r1, Operand(kStringEncodingMask));
2395 __ b(eq, &two_byte_slice);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002396 __ AllocateOneByteSlicedString(r0, r2, r6, r4, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002397 __ jmp(&set_slice_header);
2398 __ bind(&two_byte_slice);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002399 __ AllocateTwoByteSlicedString(r0, r2, r6, r4, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002400 __ bind(&set_slice_header);
2401 __ mov(r3, Operand(r3, LSL, 1));
2402 __ str(r5, FieldMemOperand(r0, SlicedString::kParentOffset));
2403 __ str(r3, FieldMemOperand(r0, SlicedString::kOffsetOffset));
2404 __ jmp(&return_r0);
2405
2406 __ bind(&copy_routine);
2407 }
2408
2409 // r5: underlying subject string
2410 // r1: instance type of underlying subject string
2411 // r2: length
2412 // r3: adjusted start index (untagged)
2413 Label two_byte_sequential, sequential_string, allocate_result;
2414 STATIC_ASSERT(kExternalStringTag != 0);
2415 STATIC_ASSERT(kSeqStringTag == 0);
2416 __ tst(r1, Operand(kExternalStringTag));
2417 __ b(eq, &sequential_string);
2418
2419 // Handle external string.
2420 // Rule out short external strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002421 STATIC_ASSERT(kShortExternalStringTag != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002422 __ tst(r1, Operand(kShortExternalStringTag));
2423 __ b(ne, &runtime);
2424 __ ldr(r5, FieldMemOperand(r5, ExternalString::kResourceDataOffset));
2425 // r5 already points to the first character of underlying string.
2426 __ jmp(&allocate_result);
2427
2428 __ bind(&sequential_string);
2429 // Locate first character of underlying subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002430 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2431 __ add(r5, r5, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002432
2433 __ bind(&allocate_result);
2434 // Sequential acii string. Allocate the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002435 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002436 __ tst(r1, Operand(kStringEncodingMask));
2437 __ b(eq, &two_byte_sequential);
2438
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002439 // Allocate and copy the resulting one-byte string.
2440 __ AllocateOneByteString(r0, r2, r4, r6, r1, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002441
2442 // Locate first character of substring to copy.
2443 __ add(r5, r5, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002444 // Locate first character of result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002445 __ add(r1, r0, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002446
Ben Murdoch589d6972011-11-30 16:04:58 +00002447 // r0: result string
2448 // r1: first character of result string
2449 // r2: result string length
2450 // r5: first character of substring to copy
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002451 STATIC_ASSERT((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
2452 StringHelper::GenerateCopyCharacters(
2453 masm, r1, r5, r2, r3, String::ONE_BYTE_ENCODING);
Ben Murdoch589d6972011-11-30 16:04:58 +00002454 __ jmp(&return_r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002455
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002456 // Allocate and copy the resulting two-byte string.
2457 __ bind(&two_byte_sequential);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002458 __ AllocateTwoByteString(r0, r2, r4, r6, r1, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002459
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002460 // Locate first character of substring to copy.
Ben Murdoch589d6972011-11-30 16:04:58 +00002461 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002462 __ add(r5, r5, Operand(r3, LSL, 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002463 // Locate first character of result.
2464 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch589d6972011-11-30 16:04:58 +00002465
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002466 // r0: result string.
2467 // r1: first character of result.
2468 // r2: result length.
Ben Murdoch589d6972011-11-30 16:04:58 +00002469 // r5: first character of substring to copy.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002470 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002471 StringHelper::GenerateCopyCharacters(
2472 masm, r1, r5, r2, r3, String::TWO_BYTE_ENCODING);
Ben Murdoch589d6972011-11-30 16:04:58 +00002473
2474 __ bind(&return_r0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002475 Counters* counters = isolate()->counters();
Steve Block44f0eee2011-05-26 01:26:41 +01002476 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002477 __ Drop(3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002478 __ Ret();
2479
2480 // Just jump to runtime to create the sub string.
2481 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002482 __ TailCallRuntime(Runtime::kSubString);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002483
2484 __ bind(&single_char);
2485 // r0: original string
2486 // r1: instance type
2487 // r2: length
2488 // r3: from index (untagged)
2489 __ SmiTag(r3, r3);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002490 StringCharAtGenerator generator(r0, r3, r2, r0, &runtime, &runtime, &runtime,
2491 STRING_INDEX_IS_NUMBER, RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002492 generator.GenerateFast(masm);
2493 __ Drop(3);
2494 __ Ret();
2495 generator.SkipSlow(masm, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002496}
2497
2498
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002499void ToNumberStub::Generate(MacroAssembler* masm) {
2500 // The ToNumber stub takes one argument in r0.
Ben Murdochda12d292016-06-02 14:46:10 +01002501 STATIC_ASSERT(kSmiTag == 0);
2502 __ tst(r0, Operand(kSmiTagMask));
2503 __ Ret(eq);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002504
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002505 __ CompareObjectType(r0, r1, r1, HEAP_NUMBER_TYPE);
2506 // r0: receiver
2507 // r1: receiver instance type
2508 __ Ret(eq);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002509
Ben Murdochda12d292016-06-02 14:46:10 +01002510 NonNumberToNumberStub stub(masm->isolate());
2511 __ TailCallStub(&stub);
2512}
2513
2514void NonNumberToNumberStub::Generate(MacroAssembler* masm) {
2515 // The NonNumberToNumber stub takes one argument in r0.
2516 __ AssertNotNumber(r0);
2517
2518 __ CompareObjectType(r0, r1, r1, FIRST_NONSTRING_TYPE);
2519 // r0: receiver
2520 // r1: receiver instance type
2521 StringToNumberStub stub(masm->isolate());
2522 __ TailCallStub(&stub, lo);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002523
2524 Label not_oddball;
2525 __ cmp(r1, Operand(ODDBALL_TYPE));
2526 __ b(ne, &not_oddball);
2527 __ ldr(r0, FieldMemOperand(r0, Oddball::kToNumberOffset));
2528 __ Ret();
2529 __ bind(&not_oddball);
2530
Ben Murdochda12d292016-06-02 14:46:10 +01002531 __ Push(r0); // Push argument.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002532 __ TailCallRuntime(Runtime::kToNumber);
2533}
2534
Ben Murdochda12d292016-06-02 14:46:10 +01002535void StringToNumberStub::Generate(MacroAssembler* masm) {
2536 // The StringToNumber stub takes one argument in r0.
2537 __ AssertString(r0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002538
Ben Murdochda12d292016-06-02 14:46:10 +01002539 // Check if string has a cached array index.
2540 Label runtime;
2541 __ ldr(r2, FieldMemOperand(r0, String::kHashFieldOffset));
2542 __ tst(r2, Operand(String::kContainsCachedArrayIndexMask));
2543 __ b(ne, &runtime);
2544 __ IndexFromHash(r2, r0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002545 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002546
Ben Murdochda12d292016-06-02 14:46:10 +01002547 __ bind(&runtime);
2548 __ Push(r0); // Push argument.
2549 __ TailCallRuntime(Runtime::kStringToNumber);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002550}
2551
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002552void ToStringStub::Generate(MacroAssembler* masm) {
2553 // The ToString stub takes one argument in r0.
2554 Label is_number;
2555 __ JumpIfSmi(r0, &is_number);
2556
2557 __ CompareObjectType(r0, r1, r1, FIRST_NONSTRING_TYPE);
2558 // r0: receiver
2559 // r1: receiver instance type
2560 __ Ret(lo);
2561
2562 Label not_heap_number;
2563 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
2564 __ b(ne, &not_heap_number);
2565 __ bind(&is_number);
2566 NumberToStringStub stub(isolate());
2567 __ TailCallStub(&stub);
2568 __ bind(&not_heap_number);
2569
2570 Label not_oddball;
2571 __ cmp(r1, Operand(ODDBALL_TYPE));
2572 __ b(ne, &not_oddball);
2573 __ ldr(r0, FieldMemOperand(r0, Oddball::kToStringOffset));
2574 __ Ret();
2575 __ bind(&not_oddball);
2576
2577 __ push(r0); // Push argument.
2578 __ TailCallRuntime(Runtime::kToString);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002579}
2580
2581
Ben Murdoch097c5b22016-05-18 11:27:45 +01002582void ToNameStub::Generate(MacroAssembler* masm) {
2583 // The ToName stub takes one argument in r0.
2584 Label is_number;
2585 __ JumpIfSmi(r0, &is_number);
2586
2587 STATIC_ASSERT(FIRST_NAME_TYPE == FIRST_TYPE);
2588 __ CompareObjectType(r0, r1, r1, LAST_NAME_TYPE);
2589 // r0: receiver
2590 // r1: receiver instance type
2591 __ Ret(ls);
2592
2593 Label not_heap_number;
2594 __ cmp(r1, Operand(HEAP_NUMBER_TYPE));
2595 __ b(ne, &not_heap_number);
2596 __ bind(&is_number);
2597 NumberToStringStub stub(isolate());
2598 __ TailCallStub(&stub);
2599 __ bind(&not_heap_number);
2600
2601 Label not_oddball;
2602 __ cmp(r1, Operand(ODDBALL_TYPE));
2603 __ b(ne, &not_oddball);
2604 __ ldr(r0, FieldMemOperand(r0, Oddball::kToStringOffset));
2605 __ Ret();
2606 __ bind(&not_oddball);
2607
2608 __ push(r0); // Push argument.
2609 __ TailCallRuntime(Runtime::kToName);
2610}
2611
2612
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002613void StringHelper::GenerateFlatOneByteStringEquals(
2614 MacroAssembler* masm, Register left, Register right, Register scratch1,
2615 Register scratch2, Register scratch3) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002616 Register length = scratch1;
2617
2618 // Compare lengths.
2619 Label strings_not_equal, check_zero_length;
2620 __ ldr(length, FieldMemOperand(left, String::kLengthOffset));
2621 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
2622 __ cmp(length, scratch2);
2623 __ b(eq, &check_zero_length);
2624 __ bind(&strings_not_equal);
2625 __ mov(r0, Operand(Smi::FromInt(NOT_EQUAL)));
2626 __ Ret();
2627
2628 // Check if the length is zero.
2629 Label compare_chars;
2630 __ bind(&check_zero_length);
2631 STATIC_ASSERT(kSmiTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002632 __ cmp(length, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00002633 __ b(ne, &compare_chars);
2634 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
2635 __ Ret();
2636
2637 // Compare characters.
2638 __ bind(&compare_chars);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002639 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2, scratch3,
2640 &strings_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00002641
2642 // Characters are equal.
2643 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
2644 __ Ret();
2645}
2646
2647
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002648void StringHelper::GenerateCompareFlatOneByteStrings(
2649 MacroAssembler* masm, Register left, Register right, Register scratch1,
2650 Register scratch2, Register scratch3, Register scratch4) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002651 Label result_not_equal, compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002652 // Find minimum length and length difference.
2653 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
2654 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
2655 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
2656 Register length_delta = scratch3;
2657 __ mov(scratch1, scratch2, LeaveCC, gt);
2658 Register min_length = scratch1;
2659 STATIC_ASSERT(kSmiTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002660 __ cmp(min_length, Operand::Zero());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002661 __ b(eq, &compare_lengths);
2662
Ben Murdoch257744e2011-11-30 15:57:28 +00002663 // Compare loop.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002664 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
2665 scratch4, &result_not_equal);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002666
Ben Murdoch257744e2011-11-30 15:57:28 +00002667 // Compare lengths - strings up to min-length are equal.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002668 __ bind(&compare_lengths);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002669 DCHECK(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002670 // Use length_delta as result if it's zero.
2671 __ mov(r0, Operand(length_delta), SetCC);
2672 __ bind(&result_not_equal);
2673 // Conditionally update the result based either on length_delta or
2674 // the last comparion performed in the loop above.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002675 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
2676 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
2677 __ Ret();
2678}
2679
2680
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002681void StringHelper::GenerateOneByteCharsCompareLoop(
2682 MacroAssembler* masm, Register left, Register right, Register length,
2683 Register scratch1, Register scratch2, Label* chars_not_equal) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002684 // Change index to run from -length to -1 by adding length to string
2685 // start. This means that loop ends when index reaches zero, which
2686 // doesn't need an additional compare.
2687 __ SmiUntag(length);
2688 __ add(scratch1, length,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002689 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00002690 __ add(left, left, Operand(scratch1));
2691 __ add(right, right, Operand(scratch1));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002692 __ rsb(length, length, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00002693 Register index = length; // index = -length;
2694
2695 // Compare loop.
2696 Label loop;
2697 __ bind(&loop);
2698 __ ldrb(scratch1, MemOperand(left, index));
2699 __ ldrb(scratch2, MemOperand(right, index));
2700 __ cmp(scratch1, scratch2);
2701 __ b(ne, chars_not_equal);
2702 __ add(index, index, Operand(1), SetCC);
2703 __ b(ne, &loop);
2704}
2705
2706
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002707void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
2708 // ----------- S t a t e -------------
2709 // -- r1 : left
2710 // -- r0 : right
2711 // -- lr : return address
2712 // -----------------------------------
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002713
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002714 // Load r2 with the allocation site. We stick an undefined dummy value here
2715 // and replace it with the real allocation site later when we instantiate this
2716 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
2717 __ Move(r2, handle(isolate()->heap()->undefined_value()));
Steve Block44f0eee2011-05-26 01:26:41 +01002718
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002719 // Make sure that we actually patched the allocation site.
2720 if (FLAG_debug_code) {
2721 __ tst(r2, Operand(kSmiTagMask));
2722 __ Assert(ne, kExpectedAllocationSite);
2723 __ push(r2);
2724 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
2725 __ LoadRoot(ip, Heap::kAllocationSiteMapRootIndex);
2726 __ cmp(r2, ip);
2727 __ pop(r2);
2728 __ Assert(eq, kExpectedAllocationSite);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002729 }
2730
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002731 // Tail call into the stub that handles binary operations with allocation
2732 // sites.
2733 BinaryOpWithAllocationSiteStub stub(isolate(), state());
2734 __ TailCallStub(&stub);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002735}
2736
2737
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002738void CompareICStub::GenerateBooleans(MacroAssembler* masm) {
2739 DCHECK_EQ(CompareICState::BOOLEAN, state());
2740 Label miss;
2741
2742 __ CheckMap(r1, r2, Heap::kBooleanMapRootIndex, &miss, DO_SMI_CHECK);
2743 __ CheckMap(r0, r3, Heap::kBooleanMapRootIndex, &miss, DO_SMI_CHECK);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002744 if (!Token::IsEqualityOp(op())) {
2745 __ ldr(r1, FieldMemOperand(r1, Oddball::kToNumberOffset));
2746 __ AssertSmi(r1);
2747 __ ldr(r0, FieldMemOperand(r0, Oddball::kToNumberOffset));
2748 __ AssertSmi(r0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002749 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002750 __ sub(r0, r1, r0);
2751 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002752
2753 __ bind(&miss);
2754 GenerateMiss(masm);
2755}
2756
2757
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002758void CompareICStub::GenerateSmis(MacroAssembler* masm) {
2759 DCHECK(state() == CompareICState::SMI);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002760 Label miss;
2761 __ orr(r2, r1, r0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002762 __ JumpIfNotSmi(r2, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002763
2764 if (GetCondition() == eq) {
2765 // For equality we do not care about the sign of the result.
2766 __ sub(r0, r0, r1, SetCC);
2767 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01002768 // Untag before subtracting to avoid handling overflow.
2769 __ SmiUntag(r1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002770 __ sub(r0, r1, Operand::SmiUntag(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002771 }
2772 __ Ret();
2773
2774 __ bind(&miss);
2775 GenerateMiss(masm);
2776}
2777
2778
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002779void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
2780 DCHECK(state() == CompareICState::NUMBER);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002781
2782 Label generic_stub;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002783 Label unordered, maybe_undefined1, maybe_undefined2;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002784 Label miss;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002785
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002786 if (left() == CompareICState::SMI) {
2787 __ JumpIfNotSmi(r1, &miss);
2788 }
2789 if (right() == CompareICState::SMI) {
2790 __ JumpIfNotSmi(r0, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002791 }
2792
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002793 // Inlining the double comparison and falling back to the general compare
2794 // stub if NaN is involved.
2795 // Load left and right operand.
2796 Label done, left, left_smi, right_smi;
2797 __ JumpIfSmi(r0, &right_smi);
2798 __ CheckMap(r0, r2, Heap::kHeapNumberMapRootIndex, &maybe_undefined1,
2799 DONT_DO_SMI_CHECK);
2800 __ sub(r2, r0, Operand(kHeapObjectTag));
2801 __ vldr(d1, r2, HeapNumber::kValueOffset);
2802 __ b(&left);
2803 __ bind(&right_smi);
2804 __ SmiToDouble(d1, r0);
2805
2806 __ bind(&left);
2807 __ JumpIfSmi(r1, &left_smi);
2808 __ CheckMap(r1, r2, Heap::kHeapNumberMapRootIndex, &maybe_undefined2,
2809 DONT_DO_SMI_CHECK);
2810 __ sub(r2, r1, Operand(kHeapObjectTag));
2811 __ vldr(d0, r2, HeapNumber::kValueOffset);
2812 __ b(&done);
2813 __ bind(&left_smi);
2814 __ SmiToDouble(d0, r1);
2815
2816 __ bind(&done);
2817 // Compare operands.
2818 __ VFPCompareAndSetFlags(d0, d1);
2819
2820 // Don't base result on status bits when a NaN is involved.
2821 __ b(vs, &unordered);
2822
2823 // Return a result of -1, 0, or 1, based on status bits.
2824 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
2825 __ mov(r0, Operand(LESS), LeaveCC, lt);
2826 __ mov(r0, Operand(GREATER), LeaveCC, gt);
2827 __ Ret();
2828
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002829 __ bind(&unordered);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002830 __ bind(&generic_stub);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002831 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002832 CompareICState::GENERIC, CompareICState::GENERIC);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002833 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
2834
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002835 __ bind(&maybe_undefined1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002836 if (Token::IsOrderedRelationalCompareOp(op())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002837 __ CompareRoot(r0, Heap::kUndefinedValueRootIndex);
2838 __ b(ne, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002839 __ JumpIfSmi(r1, &unordered);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002840 __ CompareObjectType(r1, r2, r2, HEAP_NUMBER_TYPE);
2841 __ b(ne, &maybe_undefined2);
2842 __ jmp(&unordered);
2843 }
2844
2845 __ bind(&maybe_undefined2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002846 if (Token::IsOrderedRelationalCompareOp(op())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002847 __ CompareRoot(r1, Heap::kUndefinedValueRootIndex);
2848 __ b(eq, &unordered);
2849 }
2850
Ben Murdochb0fe1622011-05-05 13:52:32 +01002851 __ bind(&miss);
2852 GenerateMiss(masm);
2853}
2854
2855
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002856void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
2857 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00002858 Label miss;
2859
2860 // Registers containing left and right operands respectively.
2861 Register left = r1;
2862 Register right = r0;
2863 Register tmp1 = r2;
2864 Register tmp2 = r3;
2865
2866 // Check that both operands are heap objects.
2867 __ JumpIfEitherSmi(left, right, &miss);
2868
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002869 // Check that both operands are internalized strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00002870 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
2871 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2872 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
2873 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002874 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
2875 __ orr(tmp1, tmp1, Operand(tmp2));
2876 __ tst(tmp1, Operand(kIsNotStringMask | kIsNotInternalizedMask));
2877 __ b(ne, &miss);
Ben Murdoch257744e2011-11-30 15:57:28 +00002878
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002879 // Internalized strings are compared by identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00002880 __ cmp(left, right);
2881 // Make sure r0 is non-zero. At this point input operands are
2882 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002883 DCHECK(right.is(r0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002884 STATIC_ASSERT(EQUAL == 0);
2885 STATIC_ASSERT(kSmiTag == 0);
2886 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
2887 __ Ret();
2888
2889 __ bind(&miss);
2890 GenerateMiss(masm);
2891}
2892
2893
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002894void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
2895 DCHECK(state() == CompareICState::UNIQUE_NAME);
2896 DCHECK(GetCondition() == eq);
Ben Murdoch257744e2011-11-30 15:57:28 +00002897 Label miss;
2898
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002899 // Registers containing left and right operands respectively.
2900 Register left = r1;
2901 Register right = r0;
2902 Register tmp1 = r2;
2903 Register tmp2 = r3;
2904
2905 // Check that both operands are heap objects.
2906 __ JumpIfEitherSmi(left, right, &miss);
2907
2908 // Check that both operands are unique names. This leaves the instance
2909 // types loaded in tmp1 and tmp2.
2910 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
2911 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2912 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
2913 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
2914
2915 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss);
2916 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss);
2917
2918 // Unique names are compared by identity.
2919 __ cmp(left, right);
2920 // Make sure r0 is non-zero. At this point input operands are
2921 // guaranteed to be non-zero.
2922 DCHECK(right.is(r0));
2923 STATIC_ASSERT(EQUAL == 0);
2924 STATIC_ASSERT(kSmiTag == 0);
2925 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
2926 __ Ret();
2927
2928 __ bind(&miss);
2929 GenerateMiss(masm);
2930}
2931
2932
2933void CompareICStub::GenerateStrings(MacroAssembler* masm) {
2934 DCHECK(state() == CompareICState::STRING);
2935 Label miss;
2936
2937 bool equality = Token::IsEqualityOp(op());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002938
Ben Murdoch257744e2011-11-30 15:57:28 +00002939 // Registers containing left and right operands respectively.
2940 Register left = r1;
2941 Register right = r0;
2942 Register tmp1 = r2;
2943 Register tmp2 = r3;
2944 Register tmp3 = r4;
2945 Register tmp4 = r5;
2946
2947 // Check that both operands are heap objects.
2948 __ JumpIfEitherSmi(left, right, &miss);
2949
2950 // Check that both operands are strings. This leaves the instance
2951 // types loaded in tmp1 and tmp2.
2952 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
2953 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
2954 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
2955 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
2956 STATIC_ASSERT(kNotStringTag != 0);
2957 __ orr(tmp3, tmp1, tmp2);
2958 __ tst(tmp3, Operand(kIsNotStringMask));
2959 __ b(ne, &miss);
2960
2961 // Fast check for identical strings.
2962 __ cmp(left, right);
2963 STATIC_ASSERT(EQUAL == 0);
2964 STATIC_ASSERT(kSmiTag == 0);
2965 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
2966 __ Ret(eq);
2967
2968 // Handle not identical strings.
2969
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002970 // Check that both strings are internalized strings. If they are, we're done
2971 // because we already know they are not identical. We know they are both
2972 // strings.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002973 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002974 DCHECK(GetCondition() == eq);
2975 STATIC_ASSERT(kInternalizedTag == 0);
2976 __ orr(tmp3, tmp1, Operand(tmp2));
2977 __ tst(tmp3, Operand(kIsNotInternalizedMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002978 // Make sure r0 is non-zero. At this point input operands are
2979 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002980 DCHECK(right.is(r0));
2981 __ Ret(eq);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002982 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002983
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002984 // Check that both strings are sequential one-byte.
Ben Murdoch257744e2011-11-30 15:57:28 +00002985 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002986 __ JumpIfBothInstanceTypesAreNotSequentialOneByte(tmp1, tmp2, tmp3, tmp4,
2987 &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00002988
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002989 // Compare flat one-byte strings. Returns when done.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002990 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002991 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1, tmp2,
2992 tmp3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002993 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002994 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
2995 tmp2, tmp3, tmp4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002996 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002997
2998 // Handle more complex cases in runtime.
2999 __ bind(&runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003000 if (equality) {
Ben Murdochda12d292016-06-02 14:46:10 +01003001 {
3002 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
3003 __ Push(left, right);
3004 __ CallRuntime(Runtime::kStringEqual);
3005 }
3006 __ LoadRoot(r1, Heap::kTrueValueRootIndex);
3007 __ sub(r0, r0, r1);
3008 __ Ret();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003009 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01003010 __ Push(left, right);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003011 __ TailCallRuntime(Runtime::kStringCompare);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003012 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003013
3014 __ bind(&miss);
3015 GenerateMiss(masm);
3016}
3017
3018
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003019void CompareICStub::GenerateReceivers(MacroAssembler* masm) {
3020 DCHECK_EQ(CompareICState::RECEIVER, state());
Ben Murdochb0fe1622011-05-05 13:52:32 +01003021 Label miss;
3022 __ and_(r2, r1, Operand(r0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003023 __ JumpIfSmi(r2, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003024
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003025 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
3026 __ CompareObjectType(r0, r2, r2, FIRST_JS_RECEIVER_TYPE);
3027 __ b(lt, &miss);
3028 __ CompareObjectType(r1, r2, r2, FIRST_JS_RECEIVER_TYPE);
3029 __ b(lt, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003030
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003031 DCHECK(GetCondition() == eq);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003032 __ sub(r0, r0, Operand(r1));
3033 __ Ret();
3034
3035 __ bind(&miss);
3036 GenerateMiss(masm);
3037}
3038
3039
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003040void CompareICStub::GenerateKnownReceivers(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003041 Label miss;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003042 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003043 __ and_(r2, r1, Operand(r0));
3044 __ JumpIfSmi(r2, &miss);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003045 __ GetWeakValue(r4, cell);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003046 __ ldr(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
3047 __ ldr(r3, FieldMemOperand(r1, HeapObject::kMapOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003048 __ cmp(r2, r4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003049 __ b(ne, &miss);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003050 __ cmp(r3, r4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003051 __ b(ne, &miss);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003052
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003053 if (Token::IsEqualityOp(op())) {
3054 __ sub(r0, r0, Operand(r1));
3055 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003056 } else {
3057 if (op() == Token::LT || op() == Token::LTE) {
3058 __ mov(r2, Operand(Smi::FromInt(GREATER)));
3059 } else {
3060 __ mov(r2, Operand(Smi::FromInt(LESS)));
3061 }
3062 __ Push(r1, r0, r2);
3063 __ TailCallRuntime(Runtime::kCompare);
3064 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003065
3066 __ bind(&miss);
3067 GenerateMiss(masm);
3068}
3069
3070
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003071void CompareICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003072 {
3073 // Call the runtime system in a fresh internal frame.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003074 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003075 __ Push(r1, r0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003076 __ Push(lr, r1, r0);
3077 __ mov(ip, Operand(Smi::FromInt(op())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003078 __ push(ip);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003079 __ CallRuntime(Runtime::kCompareIC_Miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003080 // Compute the entry point of the rewritten stub.
3081 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
3082 // Restore registers.
3083 __ pop(lr);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003084 __ Pop(r1, r0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003085 }
3086
Ben Murdochb0fe1622011-05-05 13:52:32 +01003087 __ Jump(r2);
3088}
3089
3090
Steve Block1e0659c2011-05-24 12:43:12 +01003091void DirectCEntryStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003092 // Place the return address on the stack, making the call
3093 // GC safe. The RegExp backend also relies on this.
3094 __ str(lr, MemOperand(sp, 0));
3095 __ blx(ip); // Call the C++ function.
Steve Block1e0659c2011-05-24 12:43:12 +01003096 __ ldr(pc, MemOperand(sp, 0));
3097}
3098
3099
3100void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003101 Register target) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003102 intptr_t code =
3103 reinterpret_cast<intptr_t>(GetCode().location());
3104 __ Move(ip, target);
3105 __ mov(lr, Operand(code, RelocInfo::CODE_TARGET));
3106 __ blx(lr); // Call the stub.
Steve Block1e0659c2011-05-24 12:43:12 +01003107}
3108
3109
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003110void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3111 Label* miss,
3112 Label* done,
3113 Register receiver,
3114 Register properties,
3115 Handle<Name> name,
3116 Register scratch0) {
3117 DCHECK(name->IsUniqueName());
Ben Murdoch257744e2011-11-30 15:57:28 +00003118 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3119 // not equal to the name and kProbes-th slot is not used (its name is the
3120 // undefined value), it guarantees the hash table doesn't contain the
3121 // property. It's true even if some slots represent deleted properties
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003122 // (their names are the hole value).
Ben Murdoch257744e2011-11-30 15:57:28 +00003123 for (int i = 0; i < kInlinedProbes; i++) {
3124 // scratch0 points to properties hash.
3125 // Compute the masked index: (hash + i + i * i) & mask.
3126 Register index = scratch0;
3127 // Capacity is smi 2^n.
3128 __ ldr(index, FieldMemOperand(properties, kCapacityOffset));
3129 __ sub(index, index, Operand(1));
3130 __ and_(index, index, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003131 Smi::FromInt(name->Hash() + NameDictionary::GetProbeOffset(i))));
Ben Murdoch257744e2011-11-30 15:57:28 +00003132
3133 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003134 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003135 __ add(index, index, Operand(index, LSL, 1)); // index *= 3.
3136
3137 Register entity_name = scratch0;
3138 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003139 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003140 Register tmp = properties;
3141 __ add(tmp, properties, Operand(index, LSL, 1));
3142 __ ldr(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
3143
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003144 DCHECK(!tmp.is(entity_name));
Ben Murdoch257744e2011-11-30 15:57:28 +00003145 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
3146 __ cmp(entity_name, tmp);
3147 __ b(eq, done);
3148
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003149 // Load the hole ready for use below:
3150 __ LoadRoot(tmp, Heap::kTheHoleValueRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003151
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003152 // Stop if found the property.
3153 __ cmp(entity_name, Operand(Handle<Name>(name)));
3154 __ b(eq, miss);
Ben Murdoch257744e2011-11-30 15:57:28 +00003155
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003156 Label good;
3157 __ cmp(entity_name, tmp);
3158 __ b(eq, &good);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003159
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003160 // Check if the entry name is not a unique name.
3161 __ ldr(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
3162 __ ldrb(entity_name,
3163 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
3164 __ JumpIfNotUniqueNameInstanceType(entity_name, miss);
3165 __ bind(&good);
Ben Murdoch257744e2011-11-30 15:57:28 +00003166
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003167 // Restore the properties.
3168 __ ldr(properties,
3169 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003170 }
3171
3172 const int spill_mask =
3173 (lr.bit() | r6.bit() | r5.bit() | r4.bit() | r3.bit() |
3174 r2.bit() | r1.bit() | r0.bit());
3175
3176 __ stm(db_w, sp, spill_mask);
3177 __ ldr(r0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003178 __ mov(r1, Operand(Handle<Name>(name)));
3179 NameDictionaryLookupStub stub(masm->isolate(), NEGATIVE_LOOKUP);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003180 __ CallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003181 __ cmp(r0, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00003182 __ ldm(ia_w, sp, spill_mask);
3183
3184 __ b(eq, done);
3185 __ b(ne, miss);
Ben Murdoch257744e2011-11-30 15:57:28 +00003186}
3187
3188
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003189// Probe the name dictionary in the |elements| register. Jump to the
Ben Murdoch257744e2011-11-30 15:57:28 +00003190// |done| label if a property with the given name is found. Jump to
3191// the |miss| label otherwise.
3192// If lookup was successful |scratch2| will be equal to elements + 4 * index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003193void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3194 Label* miss,
3195 Label* done,
3196 Register elements,
3197 Register name,
3198 Register scratch1,
3199 Register scratch2) {
3200 DCHECK(!elements.is(scratch1));
3201 DCHECK(!elements.is(scratch2));
3202 DCHECK(!name.is(scratch1));
3203 DCHECK(!name.is(scratch2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003204
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003205 __ AssertName(name);
Ben Murdoch257744e2011-11-30 15:57:28 +00003206
3207 // Compute the capacity mask.
3208 __ ldr(scratch1, FieldMemOperand(elements, kCapacityOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003209 __ SmiUntag(scratch1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003210 __ sub(scratch1, scratch1, Operand(1));
3211
3212 // Generate an unrolled loop that performs a few probes before
3213 // giving up. Measurements done on Gmail indicate that 2 probes
3214 // cover ~93% of loads from dictionaries.
3215 for (int i = 0; i < kInlinedProbes; i++) {
3216 // Compute the masked index: (hash + i + i * i) & mask.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003217 __ ldr(scratch2, FieldMemOperand(name, Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003218 if (i > 0) {
3219 // Add the probe offset (i + i * i) left shifted to avoid right shifting
3220 // the hash in a separate instruction. The value hash + i + i * i is right
3221 // shifted in the following and instruction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003222 DCHECK(NameDictionary::GetProbeOffset(i) <
3223 1 << (32 - Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003224 __ add(scratch2, scratch2, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003225 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003226 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003227 __ and_(scratch2, scratch1, Operand(scratch2, LSR, Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003228
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003229 // Scale the index by multiplying by the entry size.
3230 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003231 // scratch2 = scratch2 * 3.
3232 __ add(scratch2, scratch2, Operand(scratch2, LSL, 1));
3233
3234 // Check if the key is identical to the name.
3235 __ add(scratch2, elements, Operand(scratch2, LSL, 2));
3236 __ ldr(ip, FieldMemOperand(scratch2, kElementsStartOffset));
3237 __ cmp(name, Operand(ip));
3238 __ b(eq, done);
3239 }
3240
3241 const int spill_mask =
3242 (lr.bit() | r6.bit() | r5.bit() | r4.bit() |
3243 r3.bit() | r2.bit() | r1.bit() | r0.bit()) &
3244 ~(scratch1.bit() | scratch2.bit());
3245
3246 __ stm(db_w, sp, spill_mask);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003247 if (name.is(r0)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003248 DCHECK(!elements.is(r1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003249 __ Move(r1, name);
3250 __ Move(r0, elements);
3251 } else {
3252 __ Move(r0, elements);
3253 __ Move(r1, name);
3254 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003255 NameDictionaryLookupStub stub(masm->isolate(), POSITIVE_LOOKUP);
Ben Murdoch257744e2011-11-30 15:57:28 +00003256 __ CallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003257 __ cmp(r0, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00003258 __ mov(scratch2, Operand(r2));
3259 __ ldm(ia_w, sp, spill_mask);
3260
3261 __ b(ne, done);
3262 __ b(eq, miss);
3263}
3264
3265
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003266void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003267 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3268 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00003269 // Registers:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003270 // result: NameDictionary to probe
Ben Murdoch257744e2011-11-30 15:57:28 +00003271 // r1: key
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003272 // dictionary: NameDictionary to probe.
3273 // index: will hold an index of entry if lookup is successful.
3274 // might alias with result_.
Ben Murdoch257744e2011-11-30 15:57:28 +00003275 // Returns:
3276 // result_ is zero if lookup failed, non zero otherwise.
3277
3278 Register result = r0;
3279 Register dictionary = r0;
3280 Register key = r1;
3281 Register index = r2;
3282 Register mask = r3;
3283 Register hash = r4;
3284 Register undefined = r5;
3285 Register entry_key = r6;
3286
3287 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3288
3289 __ ldr(mask, FieldMemOperand(dictionary, kCapacityOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003290 __ SmiUntag(mask);
Ben Murdoch257744e2011-11-30 15:57:28 +00003291 __ sub(mask, mask, Operand(1));
3292
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003293 __ ldr(hash, FieldMemOperand(key, Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003294
3295 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
3296
3297 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3298 // Compute the masked index: (hash + i + i * i) & mask.
3299 // Capacity is smi 2^n.
3300 if (i > 0) {
3301 // Add the probe offset (i + i * i) left shifted to avoid right shifting
3302 // the hash in a separate instruction. The value hash + i + i * i is right
3303 // shifted in the following and instruction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003304 DCHECK(NameDictionary::GetProbeOffset(i) <
3305 1 << (32 - Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003306 __ add(index, hash, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003307 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003308 } else {
3309 __ mov(index, Operand(hash));
3310 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003311 __ and_(index, mask, Operand(index, LSR, Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003312
3313 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003314 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003315 __ add(index, index, Operand(index, LSL, 1)); // index *= 3.
3316
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003317 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003318 __ add(index, dictionary, Operand(index, LSL, 2));
3319 __ ldr(entry_key, FieldMemOperand(index, kElementsStartOffset));
3320
3321 // Having undefined at this place means the name is not contained.
3322 __ cmp(entry_key, Operand(undefined));
3323 __ b(eq, &not_in_dictionary);
3324
3325 // Stop if found the property.
3326 __ cmp(entry_key, Operand(key));
3327 __ b(eq, &in_dictionary);
3328
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003329 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3330 // Check if the entry name is not a unique name.
Ben Murdoch257744e2011-11-30 15:57:28 +00003331 __ ldr(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
3332 __ ldrb(entry_key,
3333 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003334 __ JumpIfNotUniqueNameInstanceType(entry_key, &maybe_in_dictionary);
Ben Murdoch257744e2011-11-30 15:57:28 +00003335 }
3336 }
3337
3338 __ bind(&maybe_in_dictionary);
3339 // If we are doing negative lookup then probing failure should be
3340 // treated as a lookup success. For positive lookup probing failure
3341 // should be treated as lookup failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003342 if (mode() == POSITIVE_LOOKUP) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003343 __ mov(result, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00003344 __ Ret();
3345 }
3346
3347 __ bind(&in_dictionary);
3348 __ mov(result, Operand(1));
3349 __ Ret();
3350
3351 __ bind(&not_in_dictionary);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003352 __ mov(result, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00003353 __ Ret();
3354}
3355
3356
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003357void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3358 Isolate* isolate) {
3359 StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
3360 stub1.GetCode();
3361 // Hydrogen code stubs need stub2 at snapshot time.
3362 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3363 stub2.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003364}
3365
3366
3367// Takes the input in 3 registers: address_ value_ and object_. A pointer to
3368// the value has just been written into the object, now this stub makes sure
3369// we keep the GC informed. The word in the object where the value has been
3370// written is in the address register.
3371void RecordWriteStub::Generate(MacroAssembler* masm) {
3372 Label skip_to_incremental_noncompacting;
3373 Label skip_to_incremental_compacting;
3374
3375 // The first two instructions are generated with labels so as to get the
3376 // offset fixed up correctly by the bind(Label*) call. We patch it back and
3377 // forth between a compare instructions (a nop in this position) and the
3378 // real branch when we start and stop incremental heap marking.
3379 // See RecordWriteStub::Patch for details.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003380 {
3381 // Block literal pool emission, as the position of these two instructions
3382 // is assumed by the patching code.
3383 Assembler::BlockConstPoolScope block_const_pool(masm);
3384 __ b(&skip_to_incremental_noncompacting);
3385 __ b(&skip_to_incremental_compacting);
3386 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003387
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003388 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3389 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003390 MacroAssembler::kReturnAtEnd);
3391 }
3392 __ Ret();
3393
3394 __ bind(&skip_to_incremental_noncompacting);
3395 GenerateIncremental(masm, INCREMENTAL);
3396
3397 __ bind(&skip_to_incremental_compacting);
3398 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3399
3400 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3401 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003402 DCHECK(Assembler::GetBranchOffset(masm->instr_at(0)) < (1 << 12));
3403 DCHECK(Assembler::GetBranchOffset(masm->instr_at(4)) < (1 << 12));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003404 PatchBranchIntoNop(masm, 0);
3405 PatchBranchIntoNop(masm, Assembler::kInstrSize);
3406}
3407
3408
3409void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3410 regs_.Save(masm);
3411
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003412 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003413 Label dont_need_remembered_set;
3414
3415 __ ldr(regs_.scratch0(), MemOperand(regs_.address(), 0));
3416 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
3417 regs_.scratch0(),
3418 &dont_need_remembered_set);
3419
Ben Murdoch097c5b22016-05-18 11:27:45 +01003420 __ JumpIfInNewSpace(regs_.object(), regs_.scratch0(),
3421 &dont_need_remembered_set);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003422
3423 // First notify the incremental marker if necessary, then update the
3424 // remembered set.
3425 CheckNeedsToInformIncrementalMarker(
3426 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003427 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003428 regs_.Restore(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003429 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003430 MacroAssembler::kReturnAtEnd);
3431
3432 __ bind(&dont_need_remembered_set);
3433 }
3434
3435 CheckNeedsToInformIncrementalMarker(
3436 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003437 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003438 regs_.Restore(masm);
3439 __ Ret();
3440}
3441
3442
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003443void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3444 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003445 int argument_count = 3;
3446 __ PrepareCallCFunction(argument_count, regs_.scratch0());
3447 Register address =
3448 r0.is(regs_.address()) ? regs_.scratch0() : regs_.address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003449 DCHECK(!address.is(regs_.object()));
3450 DCHECK(!address.is(r0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003451 __ Move(address, regs_.address());
3452 __ Move(r0, regs_.object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003453 __ Move(r1, address);
3454 __ mov(r2, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003455
3456 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003457 __ CallCFunction(
3458 ExternalReference::incremental_marking_record_write_function(isolate()),
3459 argument_count);
3460 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003461}
3462
3463
3464void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3465 MacroAssembler* masm,
3466 OnNoNeedToInformIncrementalMarker on_no_need,
3467 Mode mode) {
3468 Label on_black;
3469 Label need_incremental;
3470 Label need_incremental_pop_scratch;
3471
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003472 __ and_(regs_.scratch0(), regs_.object(), Operand(~Page::kPageAlignmentMask));
3473 __ ldr(regs_.scratch1(),
3474 MemOperand(regs_.scratch0(),
3475 MemoryChunk::kWriteBarrierCounterOffset));
3476 __ sub(regs_.scratch1(), regs_.scratch1(), Operand(1), SetCC);
3477 __ str(regs_.scratch1(),
3478 MemOperand(regs_.scratch0(),
3479 MemoryChunk::kWriteBarrierCounterOffset));
3480 __ b(mi, &need_incremental);
3481
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003482 // Let's look at the color of the object: If it is not black we don't have
3483 // to inform the incremental marker.
3484 __ JumpIfBlack(regs_.object(), regs_.scratch0(), regs_.scratch1(), &on_black);
3485
3486 regs_.Restore(masm);
3487 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003488 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003489 MacroAssembler::kReturnAtEnd);
3490 } else {
3491 __ Ret();
3492 }
3493
3494 __ bind(&on_black);
3495
3496 // Get the value from the slot.
3497 __ ldr(regs_.scratch0(), MemOperand(regs_.address(), 0));
3498
3499 if (mode == INCREMENTAL_COMPACTION) {
3500 Label ensure_not_white;
3501
3502 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3503 regs_.scratch1(), // Scratch.
3504 MemoryChunk::kEvacuationCandidateMask,
3505 eq,
3506 &ensure_not_white);
3507
3508 __ CheckPageFlag(regs_.object(),
3509 regs_.scratch1(), // Scratch.
3510 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3511 eq,
3512 &need_incremental);
3513
3514 __ bind(&ensure_not_white);
3515 }
3516
3517 // We need extra registers for this, so we push the object and the address
3518 // register temporarily.
3519 __ Push(regs_.object(), regs_.address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003520 __ JumpIfWhite(regs_.scratch0(), // The value.
3521 regs_.scratch1(), // Scratch.
3522 regs_.object(), // Scratch.
3523 regs_.address(), // Scratch.
3524 &need_incremental_pop_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003525 __ Pop(regs_.object(), regs_.address());
3526
3527 regs_.Restore(masm);
3528 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003529 __ RememberedSetHelper(object(), address(), value(), save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003530 MacroAssembler::kReturnAtEnd);
3531 } else {
3532 __ Ret();
3533 }
3534
3535 __ bind(&need_incremental_pop_scratch);
3536 __ Pop(regs_.object(), regs_.address());
3537
3538 __ bind(&need_incremental);
3539
3540 // Fall through when we need to inform the incremental marker.
3541}
3542
3543
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003544void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
3545 CEntryStub ces(isolate(), 1, kSaveFPRegs);
3546 __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
3547 int parameter_count_offset =
Ben Murdochda12d292016-06-02 14:46:10 +01003548 StubFailureTrampolineFrameConstants::kArgumentsLengthOffset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003549 __ ldr(r1, MemOperand(fp, parameter_count_offset));
3550 if (function_mode() == JS_FUNCTION_STUB_MODE) {
3551 __ add(r1, r1, Operand(1));
3552 }
3553 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
3554 __ mov(r1, Operand(r1, LSL, kPointerSizeLog2));
3555 __ add(sp, sp, r1);
3556 __ Ret();
3557}
3558
3559
3560void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003561 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3562 LoadICStub stub(isolate(), state());
3563 stub.GenerateForTrampoline(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003564}
3565
3566
3567void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003568 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3569 KeyedLoadICStub stub(isolate(), state());
3570 stub.GenerateForTrampoline(masm);
3571}
3572
3573
3574void CallICTrampolineStub::Generate(MacroAssembler* masm) {
3575 __ EmitLoadTypeFeedbackVector(r2);
3576 CallICStub stub(isolate(), state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003577 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
3578}
3579
3580
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003581void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
3582
3583
3584void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3585 GenerateImpl(masm, true);
3586}
3587
3588
3589static void HandleArrayCases(MacroAssembler* masm, Register feedback,
3590 Register receiver_map, Register scratch1,
3591 Register scratch2, bool is_polymorphic,
3592 Label* miss) {
3593 // feedback initially contains the feedback array
3594 Label next_loop, prepare_next;
3595 Label start_polymorphic;
3596
3597 Register cached_map = scratch1;
3598
3599 __ ldr(cached_map,
3600 FieldMemOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3601 __ ldr(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
3602 __ cmp(receiver_map, cached_map);
3603 __ b(ne, &start_polymorphic);
3604 // found, now call handler.
3605 Register handler = feedback;
3606 __ ldr(handler, FieldMemOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3607 __ add(pc, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
3608
3609
3610 Register length = scratch2;
3611 __ bind(&start_polymorphic);
3612 __ ldr(length, FieldMemOperand(feedback, FixedArray::kLengthOffset));
3613 if (!is_polymorphic) {
3614 // If the IC could be monomorphic we have to make sure we don't go past the
3615 // end of the feedback array.
3616 __ cmp(length, Operand(Smi::FromInt(2)));
3617 __ b(eq, miss);
3618 }
3619
3620 Register too_far = length;
3621 Register pointer_reg = feedback;
3622
3623 // +-----+------+------+-----+-----+ ... ----+
3624 // | map | len | wm0 | h0 | wm1 | hN |
3625 // +-----+------+------+-----+-----+ ... ----+
3626 // 0 1 2 len-1
3627 // ^ ^
3628 // | |
3629 // pointer_reg too_far
3630 // aka feedback scratch2
3631 // also need receiver_map
3632 // use cached_map (scratch1) to look in the weak map values.
3633 __ add(too_far, feedback, Operand::PointerOffsetFromSmiKey(length));
3634 __ add(too_far, too_far, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3635 __ add(pointer_reg, feedback,
3636 Operand(FixedArray::OffsetOfElementAt(2) - kHeapObjectTag));
3637
3638 __ bind(&next_loop);
3639 __ ldr(cached_map, MemOperand(pointer_reg));
3640 __ ldr(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
3641 __ cmp(receiver_map, cached_map);
3642 __ b(ne, &prepare_next);
3643 __ ldr(handler, MemOperand(pointer_reg, kPointerSize));
3644 __ add(pc, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
3645
3646 __ bind(&prepare_next);
3647 __ add(pointer_reg, pointer_reg, Operand(kPointerSize * 2));
3648 __ cmp(pointer_reg, too_far);
3649 __ b(lt, &next_loop);
3650
3651 // We exhausted our array of map handler pairs.
3652 __ jmp(miss);
3653}
3654
3655
3656static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
3657 Register receiver_map, Register feedback,
3658 Register vector, Register slot,
3659 Register scratch, Label* compare_map,
3660 Label* load_smi_map, Label* try_array) {
3661 __ JumpIfSmi(receiver, load_smi_map);
3662 __ ldr(receiver_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
3663 __ bind(compare_map);
3664 Register cached_map = scratch;
3665 // Move the weak map into the weak_cell register.
3666 __ ldr(cached_map, FieldMemOperand(feedback, WeakCell::kValueOffset));
3667 __ cmp(cached_map, receiver_map);
3668 __ b(ne, try_array);
3669 Register handler = feedback;
3670 __ add(handler, vector, Operand::PointerOffsetFromSmiKey(slot));
3671 __ ldr(handler,
3672 FieldMemOperand(handler, FixedArray::kHeaderSize + kPointerSize));
3673 __ add(pc, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
3674}
3675
3676
3677void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3678 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // r1
3679 Register name = LoadWithVectorDescriptor::NameRegister(); // r2
3680 Register vector = LoadWithVectorDescriptor::VectorRegister(); // r3
3681 Register slot = LoadWithVectorDescriptor::SlotRegister(); // r0
3682 Register feedback = r4;
3683 Register receiver_map = r5;
3684 Register scratch1 = r6;
3685
3686 __ add(feedback, vector, Operand::PointerOffsetFromSmiKey(slot));
3687 __ ldr(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
3688
3689 // Try to quickly handle the monomorphic case without knowing for sure
3690 // if we have a weak cell in feedback. We do know it's safe to look
3691 // at WeakCell::kValueOffset.
3692 Label try_array, load_smi_map, compare_map;
3693 Label not_array, miss;
3694 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
3695 scratch1, &compare_map, &load_smi_map, &try_array);
3696
3697 // Is it a fixed array?
3698 __ bind(&try_array);
3699 __ ldr(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
3700 __ CompareRoot(scratch1, Heap::kFixedArrayMapRootIndex);
3701 __ b(ne, &not_array);
3702 HandleArrayCases(masm, feedback, receiver_map, scratch1, r9, true, &miss);
3703
3704 __ bind(&not_array);
3705 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
3706 __ b(ne, &miss);
Ben Murdochc5610432016-08-08 18:44:38 +01003707 Code::Flags code_flags =
3708 Code::RemoveHolderFromFlags(Code::ComputeHandlerFlags(Code::LOAD_IC));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003709 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
3710 receiver, name, feedback,
3711 receiver_map, scratch1, r9);
3712
3713 __ bind(&miss);
3714 LoadIC::GenerateMiss(masm);
3715
3716 __ bind(&load_smi_map);
3717 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3718 __ jmp(&compare_map);
3719}
3720
3721
3722void KeyedLoadICStub::Generate(MacroAssembler* masm) {
3723 GenerateImpl(masm, false);
3724}
3725
3726
3727void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3728 GenerateImpl(masm, true);
3729}
3730
3731
3732void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3733 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // r1
3734 Register key = LoadWithVectorDescriptor::NameRegister(); // r2
3735 Register vector = LoadWithVectorDescriptor::VectorRegister(); // r3
3736 Register slot = LoadWithVectorDescriptor::SlotRegister(); // r0
3737 Register feedback = r4;
3738 Register receiver_map = r5;
3739 Register scratch1 = r6;
3740
3741 __ add(feedback, vector, Operand::PointerOffsetFromSmiKey(slot));
3742 __ ldr(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
3743
3744 // Try to quickly handle the monomorphic case without knowing for sure
3745 // if we have a weak cell in feedback. We do know it's safe to look
3746 // at WeakCell::kValueOffset.
3747 Label try_array, load_smi_map, compare_map;
3748 Label not_array, miss;
3749 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
3750 scratch1, &compare_map, &load_smi_map, &try_array);
3751
3752 __ bind(&try_array);
3753 // Is it a fixed array?
3754 __ ldr(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
3755 __ CompareRoot(scratch1, Heap::kFixedArrayMapRootIndex);
3756 __ b(ne, &not_array);
3757
3758 // We have a polymorphic element handler.
3759 Label polymorphic, try_poly_name;
3760 __ bind(&polymorphic);
3761 HandleArrayCases(masm, feedback, receiver_map, scratch1, r9, true, &miss);
3762
3763 __ bind(&not_array);
3764 // Is it generic?
3765 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
3766 __ b(ne, &try_poly_name);
3767 Handle<Code> megamorphic_stub =
3768 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3769 __ Jump(megamorphic_stub, RelocInfo::CODE_TARGET);
3770
3771 __ bind(&try_poly_name);
3772 // We might have a name in feedback, and a fixed array in the next slot.
3773 __ cmp(key, feedback);
3774 __ b(ne, &miss);
3775 // If the name comparison succeeded, we know we have a fixed array with
3776 // at least one map/handler pair.
3777 __ add(feedback, vector, Operand::PointerOffsetFromSmiKey(slot));
3778 __ ldr(feedback,
3779 FieldMemOperand(feedback, FixedArray::kHeaderSize + kPointerSize));
3780 HandleArrayCases(masm, feedback, receiver_map, scratch1, r9, false, &miss);
3781
3782 __ bind(&miss);
3783 KeyedLoadIC::GenerateMiss(masm);
3784
3785 __ bind(&load_smi_map);
3786 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3787 __ jmp(&compare_map);
3788}
3789
3790
3791void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3792 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3793 VectorStoreICStub stub(isolate(), state());
3794 stub.GenerateForTrampoline(masm);
3795}
3796
3797
3798void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3799 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3800 VectorKeyedStoreICStub stub(isolate(), state());
3801 stub.GenerateForTrampoline(masm);
3802}
3803
3804
3805void VectorStoreICStub::Generate(MacroAssembler* masm) {
3806 GenerateImpl(masm, false);
3807}
3808
3809
3810void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3811 GenerateImpl(masm, true);
3812}
3813
3814
3815void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3816 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // r1
3817 Register key = VectorStoreICDescriptor::NameRegister(); // r2
3818 Register vector = VectorStoreICDescriptor::VectorRegister(); // r3
3819 Register slot = VectorStoreICDescriptor::SlotRegister(); // r4
3820 DCHECK(VectorStoreICDescriptor::ValueRegister().is(r0)); // r0
3821 Register feedback = r5;
3822 Register receiver_map = r6;
3823 Register scratch1 = r9;
3824
3825 __ add(feedback, vector, Operand::PointerOffsetFromSmiKey(slot));
3826 __ ldr(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
3827
3828 // Try to quickly handle the monomorphic case without knowing for sure
3829 // if we have a weak cell in feedback. We do know it's safe to look
3830 // at WeakCell::kValueOffset.
3831 Label try_array, load_smi_map, compare_map;
3832 Label not_array, miss;
3833 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
3834 scratch1, &compare_map, &load_smi_map, &try_array);
3835
3836 // Is it a fixed array?
3837 __ bind(&try_array);
3838 __ ldr(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
3839 __ CompareRoot(scratch1, Heap::kFixedArrayMapRootIndex);
3840 __ b(ne, &not_array);
3841
3842 // We are using register r8, which is used for the embedded constant pool
3843 // when FLAG_enable_embedded_constant_pool is true.
3844 DCHECK(!FLAG_enable_embedded_constant_pool);
3845 Register scratch2 = r8;
3846 HandleArrayCases(masm, feedback, receiver_map, scratch1, scratch2, true,
3847 &miss);
3848
3849 __ bind(&not_array);
3850 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
3851 __ b(ne, &miss);
Ben Murdochc5610432016-08-08 18:44:38 +01003852 Code::Flags code_flags =
3853 Code::RemoveHolderFromFlags(Code::ComputeHandlerFlags(Code::STORE_IC));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003854 masm->isolate()->stub_cache()->GenerateProbe(
3855 masm, Code::STORE_IC, code_flags, receiver, key, feedback, receiver_map,
3856 scratch1, scratch2);
3857
3858 __ bind(&miss);
3859 StoreIC::GenerateMiss(masm);
3860
3861 __ bind(&load_smi_map);
3862 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3863 __ jmp(&compare_map);
3864}
3865
3866
3867void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
3868 GenerateImpl(masm, false);
3869}
3870
3871
3872void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
3873 GenerateImpl(masm, true);
3874}
3875
3876
3877static void HandlePolymorphicStoreCase(MacroAssembler* masm, Register feedback,
3878 Register receiver_map, Register scratch1,
3879 Register scratch2, Label* miss) {
3880 // feedback initially contains the feedback array
3881 Label next_loop, prepare_next;
3882 Label start_polymorphic;
3883 Label transition_call;
3884
3885 Register cached_map = scratch1;
3886 Register too_far = scratch2;
3887 Register pointer_reg = feedback;
3888 __ ldr(too_far, FieldMemOperand(feedback, FixedArray::kLengthOffset));
3889
3890 // +-----+------+------+-----+-----+-----+ ... ----+
3891 // | map | len | wm0 | wt0 | h0 | wm1 | hN |
3892 // +-----+------+------+-----+-----+ ----+ ... ----+
3893 // 0 1 2 len-1
3894 // ^ ^
3895 // | |
3896 // pointer_reg too_far
3897 // aka feedback scratch2
3898 // also need receiver_map
3899 // use cached_map (scratch1) to look in the weak map values.
3900 __ add(too_far, feedback, Operand::PointerOffsetFromSmiKey(too_far));
3901 __ add(too_far, too_far, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3902 __ add(pointer_reg, feedback,
3903 Operand(FixedArray::OffsetOfElementAt(0) - kHeapObjectTag));
3904
3905 __ bind(&next_loop);
3906 __ ldr(cached_map, MemOperand(pointer_reg));
3907 __ ldr(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
3908 __ cmp(receiver_map, cached_map);
3909 __ b(ne, &prepare_next);
3910 // Is it a transitioning store?
3911 __ ldr(too_far, MemOperand(pointer_reg, kPointerSize));
3912 __ CompareRoot(too_far, Heap::kUndefinedValueRootIndex);
3913 __ b(ne, &transition_call);
3914 __ ldr(pointer_reg, MemOperand(pointer_reg, kPointerSize * 2));
3915 __ add(pc, pointer_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
3916
3917 __ bind(&transition_call);
3918 __ ldr(too_far, FieldMemOperand(too_far, WeakCell::kValueOffset));
3919 __ JumpIfSmi(too_far, miss);
3920
3921 __ ldr(receiver_map, MemOperand(pointer_reg, kPointerSize * 2));
3922
3923 // Load the map into the correct register.
3924 DCHECK(feedback.is(VectorStoreTransitionDescriptor::MapRegister()));
3925 __ mov(feedback, too_far);
3926
3927 __ add(pc, receiver_map, Operand(Code::kHeaderSize - kHeapObjectTag));
3928
3929 __ bind(&prepare_next);
3930 __ add(pointer_reg, pointer_reg, Operand(kPointerSize * 3));
3931 __ cmp(pointer_reg, too_far);
3932 __ b(lt, &next_loop);
3933
3934 // We exhausted our array of map handler pairs.
3935 __ jmp(miss);
3936}
3937
3938
3939void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3940 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // r1
3941 Register key = VectorStoreICDescriptor::NameRegister(); // r2
3942 Register vector = VectorStoreICDescriptor::VectorRegister(); // r3
3943 Register slot = VectorStoreICDescriptor::SlotRegister(); // r4
3944 DCHECK(VectorStoreICDescriptor::ValueRegister().is(r0)); // r0
3945 Register feedback = r5;
3946 Register receiver_map = r6;
3947 Register scratch1 = r9;
3948
3949 __ add(feedback, vector, Operand::PointerOffsetFromSmiKey(slot));
3950 __ ldr(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
3951
3952 // Try to quickly handle the monomorphic case without knowing for sure
3953 // if we have a weak cell in feedback. We do know it's safe to look
3954 // at WeakCell::kValueOffset.
3955 Label try_array, load_smi_map, compare_map;
3956 Label not_array, miss;
3957 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
3958 scratch1, &compare_map, &load_smi_map, &try_array);
3959
3960 __ bind(&try_array);
3961 // Is it a fixed array?
3962 __ ldr(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
3963 __ CompareRoot(scratch1, Heap::kFixedArrayMapRootIndex);
3964 __ b(ne, &not_array);
3965
3966 // We have a polymorphic element handler.
3967 Label polymorphic, try_poly_name;
3968 __ bind(&polymorphic);
3969
3970 // We are using register r8, which is used for the embedded constant pool
3971 // when FLAG_enable_embedded_constant_pool is true.
3972 DCHECK(!FLAG_enable_embedded_constant_pool);
3973 Register scratch2 = r8;
3974
3975 HandlePolymorphicStoreCase(masm, feedback, receiver_map, scratch1, scratch2,
3976 &miss);
3977
3978 __ bind(&not_array);
3979 // Is it generic?
3980 __ CompareRoot(feedback, Heap::kmegamorphic_symbolRootIndex);
3981 __ b(ne, &try_poly_name);
3982 Handle<Code> megamorphic_stub =
3983 KeyedStoreIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3984 __ Jump(megamorphic_stub, RelocInfo::CODE_TARGET);
3985
3986 __ bind(&try_poly_name);
3987 // We might have a name in feedback, and a fixed array in the next slot.
3988 __ cmp(key, feedback);
3989 __ b(ne, &miss);
3990 // If the name comparison succeeded, we know we have a fixed array with
3991 // at least one map/handler pair.
3992 __ add(feedback, vector, Operand::PointerOffsetFromSmiKey(slot));
3993 __ ldr(feedback,
3994 FieldMemOperand(feedback, FixedArray::kHeaderSize + kPointerSize));
3995 HandleArrayCases(masm, feedback, receiver_map, scratch1, scratch2, false,
3996 &miss);
3997
3998 __ bind(&miss);
3999 KeyedStoreIC::GenerateMiss(masm);
4000
4001 __ bind(&load_smi_map);
4002 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4003 __ jmp(&compare_map);
4004}
4005
4006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004007void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4008 if (masm->isolate()->function_entry_hook() != NULL) {
4009 ProfileEntryHookStub stub(masm->isolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004010 PredictableCodeSizeScope predictable(masm);
4011 predictable.ExpectSize(masm->CallStubSize(&stub) +
4012 2 * Assembler::kInstrSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004013 __ push(lr);
4014 __ CallStub(&stub);
4015 __ pop(lr);
4016 }
4017}
4018
4019
4020void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4021 // The entry hook is a "push lr" instruction, followed by a call.
4022 const int32_t kReturnAddressDistanceFromFunctionStart =
4023 3 * Assembler::kInstrSize;
4024
4025 // This should contain all kCallerSaved registers.
4026 const RegList kSavedRegs =
4027 1 << 0 | // r0
4028 1 << 1 | // r1
4029 1 << 2 | // r2
4030 1 << 3 | // r3
4031 1 << 5 | // r5
4032 1 << 9; // r9
4033 // We also save lr, so the count here is one higher than the mask indicates.
4034 const int32_t kNumSavedRegs = 7;
4035
4036 DCHECK((kCallerSaved & kSavedRegs) == kCallerSaved);
4037
4038 // Save all caller-save registers as this may be called from anywhere.
4039 __ stm(db_w, sp, kSavedRegs | lr.bit());
4040
4041 // Compute the function's address for the first argument.
4042 __ sub(r0, lr, Operand(kReturnAddressDistanceFromFunctionStart));
4043
4044 // The caller's return address is above the saved temporaries.
4045 // Grab that for the second argument to the hook.
4046 __ add(r1, sp, Operand(kNumSavedRegs * kPointerSize));
4047
4048 // Align the stack if necessary.
4049 int frame_alignment = masm->ActivationFrameAlignment();
4050 if (frame_alignment > kPointerSize) {
4051 __ mov(r5, sp);
4052 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
4053 __ and_(sp, sp, Operand(-frame_alignment));
4054 }
4055
4056#if V8_HOST_ARCH_ARM
4057 int32_t entry_hook =
4058 reinterpret_cast<int32_t>(isolate()->function_entry_hook());
4059 __ mov(ip, Operand(entry_hook));
4060#else
4061 // Under the simulator we need to indirect the entry hook through a
4062 // trampoline function at a known address.
4063 // It additionally takes an isolate as a third parameter
4064 __ mov(r2, Operand(ExternalReference::isolate_address(isolate())));
4065
4066 ApiFunction dispatcher(FUNCTION_ADDR(EntryHookTrampoline));
4067 __ mov(ip, Operand(ExternalReference(&dispatcher,
4068 ExternalReference::BUILTIN_CALL,
4069 isolate())));
4070#endif
4071 __ Call(ip);
4072
4073 // Restore the stack pointer if needed.
4074 if (frame_alignment > kPointerSize) {
4075 __ mov(sp, r5);
4076 }
4077
4078 // Also pop pc to get Ret(0).
4079 __ ldm(ia_w, sp, kSavedRegs | pc.bit());
4080}
4081
4082
4083template<class T>
4084static void CreateArrayDispatch(MacroAssembler* masm,
4085 AllocationSiteOverrideMode mode) {
4086 if (mode == DISABLE_ALLOCATION_SITES) {
4087 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
4088 __ TailCallStub(&stub);
4089 } else if (mode == DONT_OVERRIDE) {
4090 int last_index = GetSequenceIndexFromFastElementsKind(
4091 TERMINAL_FAST_ELEMENTS_KIND);
4092 for (int i = 0; i <= last_index; ++i) {
4093 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4094 __ cmp(r3, Operand(kind));
4095 T stub(masm->isolate(), kind);
4096 __ TailCallStub(&stub, eq);
4097 }
4098
4099 // If we reached this point there is a problem.
4100 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4101 } else {
4102 UNREACHABLE();
4103 }
4104}
4105
4106
4107static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4108 AllocationSiteOverrideMode mode) {
4109 // r2 - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4110 // r3 - kind (if mode != DISABLE_ALLOCATION_SITES)
4111 // r0 - number of arguments
4112 // r1 - constructor?
4113 // sp[0] - last argument
4114 Label normal_sequence;
4115 if (mode == DONT_OVERRIDE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004116 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4117 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4118 STATIC_ASSERT(FAST_ELEMENTS == 2);
4119 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4120 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4121 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004122
4123 // is the low bit set? If so, we are holey and that is good.
4124 __ tst(r3, Operand(1));
4125 __ b(ne, &normal_sequence);
4126 }
4127
4128 // look at the first argument
4129 __ ldr(r5, MemOperand(sp, 0));
4130 __ cmp(r5, Operand::Zero());
4131 __ b(eq, &normal_sequence);
4132
4133 if (mode == DISABLE_ALLOCATION_SITES) {
4134 ElementsKind initial = GetInitialFastElementsKind();
4135 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4136
4137 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4138 holey_initial,
4139 DISABLE_ALLOCATION_SITES);
4140 __ TailCallStub(&stub_holey);
4141
4142 __ bind(&normal_sequence);
4143 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4144 initial,
4145 DISABLE_ALLOCATION_SITES);
4146 __ TailCallStub(&stub);
4147 } else if (mode == DONT_OVERRIDE) {
4148 // We are going to create a holey array, but our kind is non-holey.
4149 // Fix kind and retry (only if we have an allocation site in the slot).
4150 __ add(r3, r3, Operand(1));
4151
4152 if (FLAG_debug_code) {
4153 __ ldr(r5, FieldMemOperand(r2, 0));
4154 __ CompareRoot(r5, Heap::kAllocationSiteMapRootIndex);
4155 __ Assert(eq, kExpectedAllocationSite);
4156 }
4157
4158 // Save the resulting elements kind in type info. We can't just store r3
4159 // in the AllocationSite::transition_info field because elements kind is
4160 // restricted to a portion of the field...upper bits need to be left alone.
4161 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4162 __ ldr(r4, FieldMemOperand(r2, AllocationSite::kTransitionInfoOffset));
4163 __ add(r4, r4, Operand(Smi::FromInt(kFastElementsKindPackedToHoley)));
4164 __ str(r4, FieldMemOperand(r2, AllocationSite::kTransitionInfoOffset));
4165
4166 __ bind(&normal_sequence);
4167 int last_index = GetSequenceIndexFromFastElementsKind(
4168 TERMINAL_FAST_ELEMENTS_KIND);
4169 for (int i = 0; i <= last_index; ++i) {
4170 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4171 __ cmp(r3, Operand(kind));
4172 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4173 __ TailCallStub(&stub, eq);
4174 }
4175
4176 // If we reached this point there is a problem.
4177 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4178 } else {
4179 UNREACHABLE();
4180 }
4181}
4182
4183
4184template<class T>
4185static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4186 int to_index = GetSequenceIndexFromFastElementsKind(
4187 TERMINAL_FAST_ELEMENTS_KIND);
4188 for (int i = 0; i <= to_index; ++i) {
4189 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4190 T stub(isolate, kind);
4191 stub.GetCode();
4192 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4193 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4194 stub1.GetCode();
4195 }
4196 }
4197}
4198
4199
4200void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4201 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4202 isolate);
4203 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4204 isolate);
4205 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4206 isolate);
4207}
4208
4209
4210void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4211 Isolate* isolate) {
4212 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4213 for (int i = 0; i < 2; i++) {
4214 // For internal arrays we only need a few things
4215 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4216 stubh1.GetCode();
4217 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4218 stubh2.GetCode();
4219 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4220 stubh3.GetCode();
4221 }
4222}
4223
4224
4225void ArrayConstructorStub::GenerateDispatchToArrayStub(
4226 MacroAssembler* masm,
4227 AllocationSiteOverrideMode mode) {
4228 if (argument_count() == ANY) {
4229 Label not_zero_case, not_one_case;
4230 __ tst(r0, r0);
4231 __ b(ne, &not_zero_case);
4232 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4233
4234 __ bind(&not_zero_case);
4235 __ cmp(r0, Operand(1));
4236 __ b(gt, &not_one_case);
4237 CreateArrayDispatchOneArgument(masm, mode);
4238
4239 __ bind(&not_one_case);
4240 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4241 } else if (argument_count() == NONE) {
4242 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4243 } else if (argument_count() == ONE) {
4244 CreateArrayDispatchOneArgument(masm, mode);
4245 } else if (argument_count() == MORE_THAN_ONE) {
4246 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4247 } else {
4248 UNREACHABLE();
4249 }
4250}
4251
4252
4253void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4254 // ----------- S t a t e -------------
4255 // -- r0 : argc (only if argument_count() == ANY)
4256 // -- r1 : constructor
4257 // -- r2 : AllocationSite or undefined
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004258 // -- r3 : new target
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004259 // -- sp[0] : return address
4260 // -- sp[4] : last argument
4261 // -----------------------------------
4262
4263 if (FLAG_debug_code) {
4264 // The array construct code is only set for the global and natives
4265 // builtin Array functions which always have maps.
4266
4267 // Initial map for the builtin Array function should be a map.
4268 __ ldr(r4, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
4269 // Will both indicate a NULL and a Smi.
4270 __ tst(r4, Operand(kSmiTagMask));
4271 __ Assert(ne, kUnexpectedInitialMapForArrayFunction);
4272 __ CompareObjectType(r4, r4, r5, MAP_TYPE);
4273 __ Assert(eq, kUnexpectedInitialMapForArrayFunction);
4274
4275 // We should either have undefined in r2 or a valid AllocationSite
4276 __ AssertUndefinedOrAllocationSite(r2, r4);
4277 }
4278
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004279 // Enter the context of the Array function.
4280 __ ldr(cp, FieldMemOperand(r1, JSFunction::kContextOffset));
4281
4282 Label subclassing;
4283 __ cmp(r3, r1);
4284 __ b(ne, &subclassing);
4285
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004286 Label no_info;
4287 // Get the elements kind and case on that.
4288 __ CompareRoot(r2, Heap::kUndefinedValueRootIndex);
4289 __ b(eq, &no_info);
4290
4291 __ ldr(r3, FieldMemOperand(r2, AllocationSite::kTransitionInfoOffset));
4292 __ SmiUntag(r3);
4293 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4294 __ and_(r3, r3, Operand(AllocationSite::ElementsKindBits::kMask));
4295 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4296
4297 __ bind(&no_info);
4298 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004299
4300 __ bind(&subclassing);
4301 switch (argument_count()) {
4302 case ANY:
4303 case MORE_THAN_ONE:
4304 __ str(r1, MemOperand(sp, r0, LSL, kPointerSizeLog2));
4305 __ add(r0, r0, Operand(3));
4306 break;
4307 case NONE:
4308 __ str(r1, MemOperand(sp, 0 * kPointerSize));
4309 __ mov(r0, Operand(3));
4310 break;
4311 case ONE:
4312 __ str(r1, MemOperand(sp, 1 * kPointerSize));
4313 __ mov(r0, Operand(4));
4314 break;
4315 }
4316 __ Push(r3, r2);
4317 __ JumpToExternalReference(ExternalReference(Runtime::kNewArray, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004318}
4319
4320
4321void InternalArrayConstructorStub::GenerateCase(
4322 MacroAssembler* masm, ElementsKind kind) {
4323 __ cmp(r0, Operand(1));
4324
4325 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4326 __ TailCallStub(&stub0, lo);
4327
4328 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4329 __ TailCallStub(&stubN, hi);
4330
4331 if (IsFastPackedElementsKind(kind)) {
4332 // We might need to create a holey array
4333 // look at the first argument
4334 __ ldr(r3, MemOperand(sp, 0));
4335 __ cmp(r3, Operand::Zero());
4336
4337 InternalArraySingleArgumentConstructorStub
4338 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4339 __ TailCallStub(&stub1_holey, ne);
4340 }
4341
4342 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4343 __ TailCallStub(&stub1);
4344}
4345
4346
4347void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4348 // ----------- S t a t e -------------
4349 // -- r0 : argc
4350 // -- r1 : constructor
4351 // -- sp[0] : return address
4352 // -- sp[4] : last argument
4353 // -----------------------------------
4354
4355 if (FLAG_debug_code) {
4356 // The array construct code is only set for the global and natives
4357 // builtin Array functions which always have maps.
4358
4359 // Initial map for the builtin Array function should be a map.
4360 __ ldr(r3, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
4361 // Will both indicate a NULL and a Smi.
4362 __ tst(r3, Operand(kSmiTagMask));
4363 __ Assert(ne, kUnexpectedInitialMapForArrayFunction);
4364 __ CompareObjectType(r3, r3, r4, MAP_TYPE);
4365 __ Assert(eq, kUnexpectedInitialMapForArrayFunction);
4366 }
4367
4368 // Figure out the right elements kind
4369 __ ldr(r3, FieldMemOperand(r1, JSFunction::kPrototypeOrInitialMapOffset));
4370 // Load the map's "bit field 2" into |result|. We only need the first byte,
4371 // but the following bit field extraction takes care of that anyway.
4372 __ ldr(r3, FieldMemOperand(r3, Map::kBitField2Offset));
4373 // Retrieve elements_kind from bit field 2.
4374 __ DecodeField<Map::ElementsKindBits>(r3);
4375
4376 if (FLAG_debug_code) {
4377 Label done;
4378 __ cmp(r3, Operand(FAST_ELEMENTS));
4379 __ b(eq, &done);
4380 __ cmp(r3, Operand(FAST_HOLEY_ELEMENTS));
4381 __ Assert(eq,
4382 kInvalidElementsKindForInternalArrayOrInternalPackedArray);
4383 __ bind(&done);
4384 }
4385
4386 Label fast_elements_case;
4387 __ cmp(r3, Operand(FAST_ELEMENTS));
4388 __ b(eq, &fast_elements_case);
4389 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4390
4391 __ bind(&fast_elements_case);
4392 GenerateCase(masm, FAST_ELEMENTS);
4393}
4394
4395
Ben Murdoch097c5b22016-05-18 11:27:45 +01004396void FastNewObjectStub::Generate(MacroAssembler* masm) {
4397 // ----------- S t a t e -------------
4398 // -- r1 : target
4399 // -- r3 : new target
4400 // -- cp : context
4401 // -- lr : return address
4402 // -----------------------------------
4403 __ AssertFunction(r1);
4404 __ AssertReceiver(r3);
4405
4406 // Verify that the new target is a JSFunction.
4407 Label new_object;
4408 __ CompareObjectType(r3, r2, r2, JS_FUNCTION_TYPE);
4409 __ b(ne, &new_object);
4410
4411 // Load the initial map and verify that it's in fact a map.
4412 __ ldr(r2, FieldMemOperand(r3, JSFunction::kPrototypeOrInitialMapOffset));
4413 __ JumpIfSmi(r2, &new_object);
4414 __ CompareObjectType(r2, r0, r0, MAP_TYPE);
4415 __ b(ne, &new_object);
4416
4417 // Fall back to runtime if the target differs from the new target's
4418 // initial map constructor.
4419 __ ldr(r0, FieldMemOperand(r2, Map::kConstructorOrBackPointerOffset));
4420 __ cmp(r0, r1);
4421 __ b(ne, &new_object);
4422
4423 // Allocate the JSObject on the heap.
4424 Label allocate, done_allocate;
4425 __ ldrb(r4, FieldMemOperand(r2, Map::kInstanceSizeOffset));
4426 __ Allocate(r4, r0, r5, r6, &allocate, SIZE_IN_WORDS);
4427 __ bind(&done_allocate);
4428
4429 // Initialize the JSObject fields.
Ben Murdochc5610432016-08-08 18:44:38 +01004430 __ str(r2, FieldMemOperand(r0, JSObject::kMapOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004431 __ LoadRoot(r3, Heap::kEmptyFixedArrayRootIndex);
Ben Murdochc5610432016-08-08 18:44:38 +01004432 __ str(r3, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4433 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004434 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
Ben Murdochc5610432016-08-08 18:44:38 +01004435 __ add(r1, r0, Operand(JSObject::kHeaderSize - kHeapObjectTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004436
4437 // ----------- S t a t e -------------
Ben Murdochc5610432016-08-08 18:44:38 +01004438 // -- r0 : result (tagged)
Ben Murdoch097c5b22016-05-18 11:27:45 +01004439 // -- r1 : result fields (untagged)
4440 // -- r5 : result end (untagged)
4441 // -- r2 : initial map
4442 // -- cp : context
4443 // -- lr : return address
4444 // -----------------------------------
4445
4446 // Perform in-object slack tracking if requested.
4447 Label slack_tracking;
4448 STATIC_ASSERT(Map::kNoSlackTracking == 0);
4449 __ LoadRoot(r6, Heap::kUndefinedValueRootIndex);
4450 __ ldr(r3, FieldMemOperand(r2, Map::kBitField3Offset));
4451 __ tst(r3, Operand(Map::ConstructionCounter::kMask));
4452 __ b(ne, &slack_tracking);
4453 {
4454 // Initialize all in-object fields with undefined.
4455 __ InitializeFieldsWithFiller(r1, r5, r6);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004456 __ Ret();
4457 }
4458 __ bind(&slack_tracking);
4459 {
4460 // Decrease generous allocation count.
4461 STATIC_ASSERT(Map::ConstructionCounter::kNext == 32);
4462 __ sub(r3, r3, Operand(1 << Map::ConstructionCounter::kShift));
4463 __ str(r3, FieldMemOperand(r2, Map::kBitField3Offset));
4464
4465 // Initialize the in-object fields with undefined.
4466 __ ldrb(r4, FieldMemOperand(r2, Map::kUnusedPropertyFieldsOffset));
4467 __ sub(r4, r5, Operand(r4, LSL, kPointerSizeLog2));
4468 __ InitializeFieldsWithFiller(r1, r4, r6);
4469
4470 // Initialize the remaining (reserved) fields with one pointer filler map.
4471 __ LoadRoot(r6, Heap::kOnePointerFillerMapRootIndex);
4472 __ InitializeFieldsWithFiller(r1, r5, r6);
4473
Ben Murdoch097c5b22016-05-18 11:27:45 +01004474 // Check if we can finalize the instance size.
4475 STATIC_ASSERT(Map::kSlackTrackingCounterEnd == 1);
4476 __ tst(r3, Operand(Map::ConstructionCounter::kMask));
4477 __ Ret(ne);
4478
4479 // Finalize the instance size.
4480 {
4481 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
4482 __ Push(r0, r2);
4483 __ CallRuntime(Runtime::kFinalizeInstanceSize);
4484 __ Pop(r0);
4485 }
4486 __ Ret();
4487 }
4488
4489 // Fall back to %AllocateInNewSpace.
4490 __ bind(&allocate);
4491 {
4492 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
4493 STATIC_ASSERT(kSmiTag == 0);
4494 STATIC_ASSERT(kSmiTagSize == 1);
4495 __ mov(r4, Operand(r4, LSL, kPointerSizeLog2 + 1));
4496 __ Push(r2, r4);
4497 __ CallRuntime(Runtime::kAllocateInNewSpace);
4498 __ Pop(r2);
4499 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01004500 __ ldrb(r5, FieldMemOperand(r2, Map::kInstanceSizeOffset));
4501 __ add(r5, r0, Operand(r5, LSL, kPointerSizeLog2));
Ben Murdochc5610432016-08-08 18:44:38 +01004502 STATIC_ASSERT(kHeapObjectTag == 1);
4503 __ sub(r5, r5, Operand(kHeapObjectTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004504 __ b(&done_allocate);
4505
4506 // Fall back to %NewObject.
4507 __ bind(&new_object);
4508 __ Push(r1, r3);
4509 __ TailCallRuntime(Runtime::kNewObject);
4510}
4511
4512
4513void FastNewRestParameterStub::Generate(MacroAssembler* masm) {
4514 // ----------- S t a t e -------------
4515 // -- r1 : function
4516 // -- cp : context
4517 // -- fp : frame pointer
4518 // -- lr : return address
4519 // -----------------------------------
4520 __ AssertFunction(r1);
4521
Ben Murdochc5610432016-08-08 18:44:38 +01004522 // Make r2 point to the JavaScript frame.
4523 __ mov(r2, fp);
4524 if (skip_stub_frame()) {
4525 // For Ignition we need to skip the handler/stub frame to reach the
4526 // JavaScript frame for the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004527 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004528 }
4529 if (FLAG_debug_code) {
4530 Label ok;
Ben Murdochda12d292016-06-02 14:46:10 +01004531 __ ldr(ip, MemOperand(r2, StandardFrameConstants::kFunctionOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004532 __ cmp(ip, r1);
Ben Murdochc5610432016-08-08 18:44:38 +01004533 __ b(eq, &ok);
4534 __ Abort(kInvalidFrameForFastNewRestArgumentsStub);
4535 __ bind(&ok);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004536 }
4537
4538 // Check if we have rest parameters (only possible if we have an
4539 // arguments adaptor frame below the function frame).
4540 Label no_rest_parameters;
4541 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004542 __ ldr(ip, MemOperand(r2, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004543 __ cmp(ip, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4544 __ b(ne, &no_rest_parameters);
4545
4546 // Check if the arguments adaptor frame contains more arguments than
4547 // specified by the function's internal formal parameter count.
4548 Label rest_parameters;
4549 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4550 __ ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
4551 __ ldr(r1,
4552 FieldMemOperand(r1, SharedFunctionInfo::kFormalParameterCountOffset));
4553 __ sub(r0, r0, r1, SetCC);
4554 __ b(gt, &rest_parameters);
4555
4556 // Return an empty rest parameter array.
4557 __ bind(&no_rest_parameters);
4558 {
4559 // ----------- S t a t e -------------
4560 // -- cp : context
4561 // -- lr : return address
4562 // -----------------------------------
4563
4564 // Allocate an empty rest parameter array.
4565 Label allocate, done_allocate;
Ben Murdochc5610432016-08-08 18:44:38 +01004566 __ Allocate(JSArray::kSize, r0, r1, r2, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004567 __ bind(&done_allocate);
4568
4569 // Setup the rest parameter array in r0.
4570 __ LoadNativeContextSlot(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, r1);
4571 __ str(r1, FieldMemOperand(r0, JSArray::kMapOffset));
4572 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
4573 __ str(r1, FieldMemOperand(r0, JSArray::kPropertiesOffset));
4574 __ str(r1, FieldMemOperand(r0, JSArray::kElementsOffset));
4575 __ mov(r1, Operand(0));
4576 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4577 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4578 __ Ret();
4579
4580 // Fall back to %AllocateInNewSpace.
4581 __ bind(&allocate);
4582 {
4583 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
4584 __ Push(Smi::FromInt(JSArray::kSize));
4585 __ CallRuntime(Runtime::kAllocateInNewSpace);
4586 }
4587 __ jmp(&done_allocate);
4588 }
4589
4590 __ bind(&rest_parameters);
4591 {
4592 // Compute the pointer to the first rest parameter (skippping the receiver).
4593 __ add(r2, r2, Operand(r0, LSL, kPointerSizeLog2 - 1));
4594 __ add(r2, r2,
4595 Operand(StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4596
4597 // ----------- S t a t e -------------
4598 // -- cp : context
4599 // -- r0 : number of rest parameters (tagged)
4600 // -- r2 : pointer to first rest parameters
4601 // -- lr : return address
4602 // -----------------------------------
4603
4604 // Allocate space for the rest parameter array plus the backing store.
4605 Label allocate, done_allocate;
4606 __ mov(r1, Operand(JSArray::kSize + FixedArray::kHeaderSize));
4607 __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - 1));
Ben Murdochc5610432016-08-08 18:44:38 +01004608 __ Allocate(r1, r3, r4, r5, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004609 __ bind(&done_allocate);
4610
4611 // Setup the elements array in r3.
4612 __ LoadRoot(r1, Heap::kFixedArrayMapRootIndex);
4613 __ str(r1, FieldMemOperand(r3, FixedArray::kMapOffset));
4614 __ str(r0, FieldMemOperand(r3, FixedArray::kLengthOffset));
4615 __ add(r4, r3, Operand(FixedArray::kHeaderSize));
4616 {
4617 Label loop, done_loop;
4618 __ add(r1, r4, Operand(r0, LSL, kPointerSizeLog2 - 1));
4619 __ bind(&loop);
4620 __ cmp(r4, r1);
4621 __ b(eq, &done_loop);
4622 __ ldr(ip, MemOperand(r2, 1 * kPointerSize, NegPostIndex));
4623 __ str(ip, FieldMemOperand(r4, 0 * kPointerSize));
4624 __ add(r4, r4, Operand(1 * kPointerSize));
4625 __ b(&loop);
4626 __ bind(&done_loop);
4627 }
4628
4629 // Setup the rest parameter array in r4.
4630 __ LoadNativeContextSlot(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, r1);
4631 __ str(r1, FieldMemOperand(r4, JSArray::kMapOffset));
4632 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
4633 __ str(r1, FieldMemOperand(r4, JSArray::kPropertiesOffset));
4634 __ str(r3, FieldMemOperand(r4, JSArray::kElementsOffset));
4635 __ str(r0, FieldMemOperand(r4, JSArray::kLengthOffset));
4636 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4637 __ mov(r0, r4);
4638 __ Ret();
4639
4640 // Fall back to %AllocateInNewSpace.
4641 __ bind(&allocate);
4642 {
4643 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
4644 __ SmiTag(r1);
4645 __ Push(r0, r2, r1);
4646 __ CallRuntime(Runtime::kAllocateInNewSpace);
4647 __ mov(r3, r0);
4648 __ Pop(r0, r2);
4649 }
4650 __ jmp(&done_allocate);
4651 }
4652}
4653
4654
4655void FastNewSloppyArgumentsStub::Generate(MacroAssembler* masm) {
4656 // ----------- S t a t e -------------
4657 // -- r1 : function
4658 // -- cp : context
4659 // -- fp : frame pointer
4660 // -- lr : return address
4661 // -----------------------------------
4662 __ AssertFunction(r1);
4663
Ben Murdochc5610432016-08-08 18:44:38 +01004664 // Make r9 point to the JavaScript frame.
4665 __ mov(r9, fp);
4666 if (skip_stub_frame()) {
4667 // For Ignition we need to skip the handler/stub frame to reach the
4668 // JavaScript frame for the function.
4669 __ ldr(r9, MemOperand(r9, StandardFrameConstants::kCallerFPOffset));
4670 }
4671 if (FLAG_debug_code) {
4672 Label ok;
4673 __ ldr(ip, MemOperand(r9, StandardFrameConstants::kFunctionOffset));
4674 __ cmp(ip, r1);
4675 __ b(eq, &ok);
4676 __ Abort(kInvalidFrameForFastNewRestArgumentsStub);
4677 __ bind(&ok);
4678 }
4679
Ben Murdoch097c5b22016-05-18 11:27:45 +01004680 // TODO(bmeurer): Cleanup to match the FastNewStrictArgumentsStub.
4681 __ ldr(r2, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
4682 __ ldr(r2,
4683 FieldMemOperand(r2, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004684 __ add(r3, r9, Operand(r2, LSL, kPointerSizeLog2 - 1));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004685 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4686
4687 // r1 : function
4688 // r2 : number of parameters (tagged)
4689 // r3 : parameters pointer
Ben Murdochc5610432016-08-08 18:44:38 +01004690 // r9 : JavaScript frame pointer
Ben Murdoch097c5b22016-05-18 11:27:45 +01004691 // Registers used over whole function:
4692 // r5 : arguments count (tagged)
4693 // r6 : mapped parameter count (tagged)
4694
4695 // Check if the calling frame is an arguments adaptor frame.
4696 Label adaptor_frame, try_allocate, runtime;
Ben Murdochc5610432016-08-08 18:44:38 +01004697 __ ldr(r4, MemOperand(r9, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004698 __ ldr(r0, MemOperand(r4, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004699 __ cmp(r0, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4700 __ b(eq, &adaptor_frame);
4701
4702 // No adaptor, parameter count = argument count.
4703 __ mov(r5, r2);
4704 __ mov(r6, r2);
4705 __ b(&try_allocate);
4706
4707 // We have an adaptor frame. Patch the parameters pointer.
4708 __ bind(&adaptor_frame);
4709 __ ldr(r5, MemOperand(r4, ArgumentsAdaptorFrameConstants::kLengthOffset));
4710 __ add(r4, r4, Operand(r5, LSL, 1));
4711 __ add(r3, r4, Operand(StandardFrameConstants::kCallerSPOffset));
4712
4713 // r5 = argument count (tagged)
4714 // r6 = parameter count (tagged)
4715 // Compute the mapped parameter count = min(r6, r5) in r6.
4716 __ mov(r6, r2);
4717 __ cmp(r6, Operand(r5));
4718 __ mov(r6, Operand(r5), LeaveCC, gt);
4719
4720 __ bind(&try_allocate);
4721
4722 // Compute the sizes of backing store, parameter map, and arguments object.
4723 // 1. Parameter map, has 2 extra words containing context and backing store.
4724 const int kParameterMapHeaderSize =
4725 FixedArray::kHeaderSize + 2 * kPointerSize;
4726 // If there are no mapped parameters, we do not need the parameter_map.
4727 __ cmp(r6, Operand(Smi::FromInt(0)));
4728 __ mov(r9, Operand::Zero(), LeaveCC, eq);
4729 __ mov(r9, Operand(r6, LSL, 1), LeaveCC, ne);
4730 __ add(r9, r9, Operand(kParameterMapHeaderSize), LeaveCC, ne);
4731
4732 // 2. Backing store.
4733 __ add(r9, r9, Operand(r5, LSL, 1));
4734 __ add(r9, r9, Operand(FixedArray::kHeaderSize));
4735
4736 // 3. Arguments object.
4737 __ add(r9, r9, Operand(JSSloppyArgumentsObject::kSize));
4738
4739 // Do the allocation of all three objects in one go.
Ben Murdochc5610432016-08-08 18:44:38 +01004740 __ Allocate(r9, r0, r9, r4, &runtime, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004741
4742 // r0 = address of new object(s) (tagged)
4743 // r2 = argument count (smi-tagged)
4744 // Get the arguments boilerplate from the current native context into r4.
4745 const int kNormalOffset =
4746 Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX);
4747 const int kAliasedOffset =
4748 Context::SlotOffset(Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX);
4749
4750 __ ldr(r4, NativeContextMemOperand());
4751 __ cmp(r6, Operand::Zero());
4752 __ ldr(r4, MemOperand(r4, kNormalOffset), eq);
4753 __ ldr(r4, MemOperand(r4, kAliasedOffset), ne);
4754
4755 // r0 = address of new object (tagged)
4756 // r2 = argument count (smi-tagged)
4757 // r4 = address of arguments map (tagged)
4758 // r6 = mapped parameter count (tagged)
4759 __ str(r4, FieldMemOperand(r0, JSObject::kMapOffset));
4760 __ LoadRoot(r9, Heap::kEmptyFixedArrayRootIndex);
4761 __ str(r9, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4762 __ str(r9, FieldMemOperand(r0, JSObject::kElementsOffset));
4763
4764 // Set up the callee in-object property.
4765 __ AssertNotSmi(r1);
4766 __ str(r1, FieldMemOperand(r0, JSSloppyArgumentsObject::kCalleeOffset));
4767
4768 // Use the length (smi tagged) and set that as an in-object property too.
4769 __ AssertSmi(r5);
4770 __ str(r5, FieldMemOperand(r0, JSSloppyArgumentsObject::kLengthOffset));
4771
4772 // Set up the elements pointer in the allocated arguments object.
4773 // If we allocated a parameter map, r4 will point there, otherwise
4774 // it will point to the backing store.
4775 __ add(r4, r0, Operand(JSSloppyArgumentsObject::kSize));
4776 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
4777
4778 // r0 = address of new object (tagged)
4779 // r2 = argument count (tagged)
4780 // r4 = address of parameter map or backing store (tagged)
4781 // r6 = mapped parameter count (tagged)
4782 // Initialize parameter map. If there are no mapped arguments, we're done.
4783 Label skip_parameter_map;
4784 __ cmp(r6, Operand(Smi::FromInt(0)));
4785 // Move backing store address to r1, because it is
4786 // expected there when filling in the unmapped arguments.
4787 __ mov(r1, r4, LeaveCC, eq);
4788 __ b(eq, &skip_parameter_map);
4789
4790 __ LoadRoot(r5, Heap::kSloppyArgumentsElementsMapRootIndex);
4791 __ str(r5, FieldMemOperand(r4, FixedArray::kMapOffset));
4792 __ add(r5, r6, Operand(Smi::FromInt(2)));
4793 __ str(r5, FieldMemOperand(r4, FixedArray::kLengthOffset));
4794 __ str(cp, FieldMemOperand(r4, FixedArray::kHeaderSize + 0 * kPointerSize));
4795 __ add(r5, r4, Operand(r6, LSL, 1));
4796 __ add(r5, r5, Operand(kParameterMapHeaderSize));
4797 __ str(r5, FieldMemOperand(r4, FixedArray::kHeaderSize + 1 * kPointerSize));
4798
4799 // Copy the parameter slots and the holes in the arguments.
4800 // We need to fill in mapped_parameter_count slots. They index the context,
4801 // where parameters are stored in reverse order, at
4802 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
4803 // The mapped parameter thus need to get indices
4804 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
4805 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
4806 // We loop from right to left.
4807 Label parameters_loop, parameters_test;
4808 __ mov(r5, r6);
4809 __ add(r9, r2, Operand(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
4810 __ sub(r9, r9, Operand(r6));
4811 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
4812 __ add(r1, r4, Operand(r5, LSL, 1));
4813 __ add(r1, r1, Operand(kParameterMapHeaderSize));
4814
4815 // r1 = address of backing store (tagged)
4816 // r4 = address of parameter map (tagged), which is also the address of new
4817 // object + Heap::kSloppyArgumentsObjectSize (tagged)
4818 // r0 = temporary scratch (a.o., for address calculation)
4819 // r5 = loop variable (tagged)
4820 // ip = the hole value
4821 __ jmp(&parameters_test);
4822
4823 __ bind(&parameters_loop);
4824 __ sub(r5, r5, Operand(Smi::FromInt(1)));
4825 __ mov(r0, Operand(r5, LSL, 1));
4826 __ add(r0, r0, Operand(kParameterMapHeaderSize - kHeapObjectTag));
4827 __ str(r9, MemOperand(r4, r0));
4828 __ sub(r0, r0, Operand(kParameterMapHeaderSize - FixedArray::kHeaderSize));
4829 __ str(ip, MemOperand(r1, r0));
4830 __ add(r9, r9, Operand(Smi::FromInt(1)));
4831 __ bind(&parameters_test);
4832 __ cmp(r5, Operand(Smi::FromInt(0)));
4833 __ b(ne, &parameters_loop);
4834
4835 // Restore r0 = new object (tagged) and r5 = argument count (tagged).
4836 __ sub(r0, r4, Operand(JSSloppyArgumentsObject::kSize));
4837 __ ldr(r5, FieldMemOperand(r0, JSSloppyArgumentsObject::kLengthOffset));
4838
4839 __ bind(&skip_parameter_map);
4840 // r0 = address of new object (tagged)
4841 // r1 = address of backing store (tagged)
4842 // r5 = argument count (tagged)
4843 // r6 = mapped parameter count (tagged)
4844 // r9 = scratch
4845 // Copy arguments header and remaining slots (if there are any).
4846 __ LoadRoot(r9, Heap::kFixedArrayMapRootIndex);
4847 __ str(r9, FieldMemOperand(r1, FixedArray::kMapOffset));
4848 __ str(r5, FieldMemOperand(r1, FixedArray::kLengthOffset));
4849
4850 Label arguments_loop, arguments_test;
4851 __ sub(r3, r3, Operand(r6, LSL, 1));
4852 __ jmp(&arguments_test);
4853
4854 __ bind(&arguments_loop);
4855 __ sub(r3, r3, Operand(kPointerSize));
4856 __ ldr(r4, MemOperand(r3, 0));
4857 __ add(r9, r1, Operand(r6, LSL, 1));
4858 __ str(r4, FieldMemOperand(r9, FixedArray::kHeaderSize));
4859 __ add(r6, r6, Operand(Smi::FromInt(1)));
4860
4861 __ bind(&arguments_test);
4862 __ cmp(r6, Operand(r5));
4863 __ b(lt, &arguments_loop);
4864
4865 // Return.
4866 __ Ret();
4867
4868 // Do the runtime call to allocate the arguments object.
4869 // r0 = address of new object (tagged)
4870 // r5 = argument count (tagged)
4871 __ bind(&runtime);
4872 __ Push(r1, r3, r5);
4873 __ TailCallRuntime(Runtime::kNewSloppyArguments);
4874}
4875
4876
4877void FastNewStrictArgumentsStub::Generate(MacroAssembler* masm) {
4878 // ----------- S t a t e -------------
4879 // -- r1 : function
4880 // -- cp : context
4881 // -- fp : frame pointer
4882 // -- lr : return address
4883 // -----------------------------------
4884 __ AssertFunction(r1);
4885
Ben Murdochc5610432016-08-08 18:44:38 +01004886 // Make r2 point to the JavaScript frame.
4887 __ mov(r2, fp);
4888 if (skip_stub_frame()) {
4889 // For Ignition we need to skip the handler/stub frame to reach the
4890 // JavaScript frame for the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004891 __ ldr(r2, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004892 }
4893 if (FLAG_debug_code) {
4894 Label ok;
Ben Murdochda12d292016-06-02 14:46:10 +01004895 __ ldr(ip, MemOperand(r2, StandardFrameConstants::kFunctionOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004896 __ cmp(ip, r1);
Ben Murdochc5610432016-08-08 18:44:38 +01004897 __ b(eq, &ok);
4898 __ Abort(kInvalidFrameForFastNewRestArgumentsStub);
4899 __ bind(&ok);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004900 }
4901
4902 // Check if we have an arguments adaptor frame below the function frame.
4903 Label arguments_adaptor, arguments_done;
4904 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004905 __ ldr(ip, MemOperand(r3, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004906 __ cmp(ip, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4907 __ b(eq, &arguments_adaptor);
4908 {
4909 __ ldr(r1, FieldMemOperand(r1, JSFunction::kSharedFunctionInfoOffset));
4910 __ ldr(r0, FieldMemOperand(
4911 r1, SharedFunctionInfo::kFormalParameterCountOffset));
4912 __ add(r2, r2, Operand(r0, LSL, kPointerSizeLog2 - 1));
4913 __ add(r2, r2,
4914 Operand(StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4915 }
4916 __ b(&arguments_done);
4917 __ bind(&arguments_adaptor);
4918 {
4919 __ ldr(r0, MemOperand(r3, ArgumentsAdaptorFrameConstants::kLengthOffset));
4920 __ add(r2, r3, Operand(r0, LSL, kPointerSizeLog2 - 1));
4921 __ add(r2, r2,
4922 Operand(StandardFrameConstants::kCallerSPOffset - 1 * kPointerSize));
4923 }
4924 __ bind(&arguments_done);
4925
4926 // ----------- S t a t e -------------
4927 // -- cp : context
4928 // -- r0 : number of rest parameters (tagged)
4929 // -- r2 : pointer to first rest parameters
4930 // -- lr : return address
4931 // -----------------------------------
4932
4933 // Allocate space for the strict arguments object plus the backing store.
4934 Label allocate, done_allocate;
4935 __ mov(r1, Operand(JSStrictArgumentsObject::kSize + FixedArray::kHeaderSize));
4936 __ add(r1, r1, Operand(r0, LSL, kPointerSizeLog2 - 1));
Ben Murdochc5610432016-08-08 18:44:38 +01004937 __ Allocate(r1, r3, r4, r5, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004938 __ bind(&done_allocate);
4939
4940 // Setup the elements array in r3.
4941 __ LoadRoot(r1, Heap::kFixedArrayMapRootIndex);
4942 __ str(r1, FieldMemOperand(r3, FixedArray::kMapOffset));
4943 __ str(r0, FieldMemOperand(r3, FixedArray::kLengthOffset));
4944 __ add(r4, r3, Operand(FixedArray::kHeaderSize));
4945 {
4946 Label loop, done_loop;
4947 __ add(r1, r4, Operand(r0, LSL, kPointerSizeLog2 - 1));
4948 __ bind(&loop);
4949 __ cmp(r4, r1);
4950 __ b(eq, &done_loop);
4951 __ ldr(ip, MemOperand(r2, 1 * kPointerSize, NegPostIndex));
4952 __ str(ip, FieldMemOperand(r4, 0 * kPointerSize));
4953 __ add(r4, r4, Operand(1 * kPointerSize));
4954 __ b(&loop);
4955 __ bind(&done_loop);
4956 }
4957
4958 // Setup the strict arguments object in r4.
4959 __ LoadNativeContextSlot(Context::STRICT_ARGUMENTS_MAP_INDEX, r1);
4960 __ str(r1, FieldMemOperand(r4, JSStrictArgumentsObject::kMapOffset));
4961 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
4962 __ str(r1, FieldMemOperand(r4, JSStrictArgumentsObject::kPropertiesOffset));
4963 __ str(r3, FieldMemOperand(r4, JSStrictArgumentsObject::kElementsOffset));
4964 __ str(r0, FieldMemOperand(r4, JSStrictArgumentsObject::kLengthOffset));
4965 STATIC_ASSERT(JSStrictArgumentsObject::kSize == 4 * kPointerSize);
4966 __ mov(r0, r4);
4967 __ Ret();
4968
4969 // Fall back to %AllocateInNewSpace.
4970 __ bind(&allocate);
4971 {
4972 FrameAndConstantPoolScope scope(masm, StackFrame::INTERNAL);
4973 __ SmiTag(r1);
4974 __ Push(r0, r2, r1);
4975 __ CallRuntime(Runtime::kAllocateInNewSpace);
4976 __ mov(r3, r0);
4977 __ Pop(r0, r2);
4978 }
4979 __ b(&done_allocate);
4980}
4981
4982
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004983void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
4984 Register context = cp;
4985 Register result = r0;
4986 Register slot = r2;
4987
4988 // Go up the context chain to the script context.
4989 for (int i = 0; i < depth(); ++i) {
4990 __ ldr(result, ContextMemOperand(context, Context::PREVIOUS_INDEX));
4991 context = result;
4992 }
4993
4994 // Load the PropertyCell value at the specified slot.
4995 __ add(result, context, Operand(slot, LSL, kPointerSizeLog2));
4996 __ ldr(result, ContextMemOperand(result));
4997 __ ldr(result, FieldMemOperand(result, PropertyCell::kValueOffset));
4998
4999 // If the result is not the_hole, return. Otherwise, handle in the runtime.
5000 __ CompareRoot(result, Heap::kTheHoleValueRootIndex);
5001 __ Ret(ne);
5002
5003 // Fallback to runtime.
5004 __ SmiTag(slot);
5005 __ push(slot);
5006 __ TailCallRuntime(Runtime::kLoadGlobalViaContext);
5007}
5008
5009
5010void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
5011 Register value = r0;
5012 Register slot = r2;
5013
5014 Register cell = r1;
5015 Register cell_details = r4;
5016 Register cell_value = r5;
5017 Register cell_value_map = r6;
5018 Register scratch = r9;
5019
5020 Register context = cp;
5021 Register context_temp = cell;
5022
5023 Label fast_heapobject_case, fast_smi_case, slow_case;
5024
5025 if (FLAG_debug_code) {
5026 __ CompareRoot(value, Heap::kTheHoleValueRootIndex);
5027 __ Check(ne, kUnexpectedValue);
5028 }
5029
5030 // Go up the context chain to the script context.
5031 for (int i = 0; i < depth(); i++) {
5032 __ ldr(context_temp, ContextMemOperand(context, Context::PREVIOUS_INDEX));
5033 context = context_temp;
5034 }
5035
5036 // Load the PropertyCell at the specified slot.
5037 __ add(cell, context, Operand(slot, LSL, kPointerSizeLog2));
5038 __ ldr(cell, ContextMemOperand(cell));
5039
5040 // Load PropertyDetails for the cell (actually only the cell_type and kind).
5041 __ ldr(cell_details, FieldMemOperand(cell, PropertyCell::kDetailsOffset));
5042 __ SmiUntag(cell_details);
5043 __ and_(cell_details, cell_details,
5044 Operand(PropertyDetails::PropertyCellTypeField::kMask |
5045 PropertyDetails::KindField::kMask |
5046 PropertyDetails::kAttributesReadOnlyMask));
5047
5048 // Check if PropertyCell holds mutable data.
5049 Label not_mutable_data;
5050 __ cmp(cell_details, Operand(PropertyDetails::PropertyCellTypeField::encode(
5051 PropertyCellType::kMutable) |
5052 PropertyDetails::KindField::encode(kData)));
5053 __ b(ne, &not_mutable_data);
5054 __ JumpIfSmi(value, &fast_smi_case);
5055
5056 __ bind(&fast_heapobject_case);
5057 __ str(value, FieldMemOperand(cell, PropertyCell::kValueOffset));
5058 // RecordWriteField clobbers the value register, so we copy it before the
5059 // call.
5060 __ mov(r4, Operand(value));
5061 __ RecordWriteField(cell, PropertyCell::kValueOffset, r4, scratch,
5062 kLRHasNotBeenSaved, kDontSaveFPRegs, EMIT_REMEMBERED_SET,
5063 OMIT_SMI_CHECK);
5064 __ Ret();
5065
5066 __ bind(&not_mutable_data);
5067 // Check if PropertyCell value matches the new value (relevant for Constant,
5068 // ConstantType and Undefined cells).
5069 Label not_same_value;
5070 __ ldr(cell_value, FieldMemOperand(cell, PropertyCell::kValueOffset));
5071 __ cmp(cell_value, value);
5072 __ b(ne, &not_same_value);
5073
5074 // Make sure the PropertyCell is not marked READ_ONLY.
5075 __ tst(cell_details, Operand(PropertyDetails::kAttributesReadOnlyMask));
5076 __ b(ne, &slow_case);
5077
5078 if (FLAG_debug_code) {
5079 Label done;
5080 // This can only be true for Constant, ConstantType and Undefined cells,
5081 // because we never store the_hole via this stub.
5082 __ cmp(cell_details, Operand(PropertyDetails::PropertyCellTypeField::encode(
5083 PropertyCellType::kConstant) |
5084 PropertyDetails::KindField::encode(kData)));
5085 __ b(eq, &done);
5086 __ cmp(cell_details, Operand(PropertyDetails::PropertyCellTypeField::encode(
5087 PropertyCellType::kConstantType) |
5088 PropertyDetails::KindField::encode(kData)));
5089 __ b(eq, &done);
5090 __ cmp(cell_details, Operand(PropertyDetails::PropertyCellTypeField::encode(
5091 PropertyCellType::kUndefined) |
5092 PropertyDetails::KindField::encode(kData)));
5093 __ Check(eq, kUnexpectedValue);
5094 __ bind(&done);
5095 }
5096 __ Ret();
5097 __ bind(&not_same_value);
5098
5099 // Check if PropertyCell contains data with constant type (and is not
5100 // READ_ONLY).
5101 __ cmp(cell_details, Operand(PropertyDetails::PropertyCellTypeField::encode(
5102 PropertyCellType::kConstantType) |
5103 PropertyDetails::KindField::encode(kData)));
5104 __ b(ne, &slow_case);
5105
5106 // Now either both old and new values must be smis or both must be heap
5107 // objects with same map.
5108 Label value_is_heap_object;
5109 __ JumpIfNotSmi(value, &value_is_heap_object);
5110 __ JumpIfNotSmi(cell_value, &slow_case);
5111 // Old and new values are smis, no need for a write barrier here.
5112 __ bind(&fast_smi_case);
5113 __ str(value, FieldMemOperand(cell, PropertyCell::kValueOffset));
5114 __ Ret();
5115
5116 __ bind(&value_is_heap_object);
5117 __ JumpIfSmi(cell_value, &slow_case);
5118
5119 __ ldr(cell_value_map, FieldMemOperand(cell_value, HeapObject::kMapOffset));
5120 __ ldr(scratch, FieldMemOperand(value, HeapObject::kMapOffset));
5121 __ cmp(cell_value_map, scratch);
5122 __ b(eq, &fast_heapobject_case);
5123
5124 // Fallback to runtime.
5125 __ bind(&slow_case);
5126 __ SmiTag(slot);
5127 __ Push(slot, value);
5128 __ TailCallRuntime(is_strict(language_mode())
5129 ? Runtime::kStoreGlobalViaContext_Strict
5130 : Runtime::kStoreGlobalViaContext_Sloppy);
5131}
5132
5133
5134static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
5135 return ref0.address() - ref1.address();
5136}
5137
5138
5139// Calls an API function. Allocates HandleScope, extracts returned value
5140// from handle and propagates exceptions. Restores context. stack_space
5141// - space to be unwound on exit (includes the call JS arguments space and
5142// the additional space allocated for the fast call).
5143static void CallApiFunctionAndReturn(MacroAssembler* masm,
5144 Register function_address,
5145 ExternalReference thunk_ref,
5146 int stack_space,
5147 MemOperand* stack_space_operand,
5148 MemOperand return_value_operand,
5149 MemOperand* context_restore_operand) {
5150 Isolate* isolate = masm->isolate();
5151 ExternalReference next_address =
5152 ExternalReference::handle_scope_next_address(isolate);
5153 const int kNextOffset = 0;
5154 const int kLimitOffset = AddressOffset(
5155 ExternalReference::handle_scope_limit_address(isolate), next_address);
5156 const int kLevelOffset = AddressOffset(
5157 ExternalReference::handle_scope_level_address(isolate), next_address);
5158
5159 DCHECK(function_address.is(r1) || function_address.is(r2));
5160
5161 Label profiler_disabled;
5162 Label end_profiler_check;
5163 __ mov(r9, Operand(ExternalReference::is_profiling_address(isolate)));
5164 __ ldrb(r9, MemOperand(r9, 0));
5165 __ cmp(r9, Operand(0));
5166 __ b(eq, &profiler_disabled);
5167
5168 // Additional parameter is the address of the actual callback.
5169 __ mov(r3, Operand(thunk_ref));
5170 __ jmp(&end_profiler_check);
5171
5172 __ bind(&profiler_disabled);
5173 __ Move(r3, function_address);
5174 __ bind(&end_profiler_check);
5175
5176 // Allocate HandleScope in callee-save registers.
5177 __ mov(r9, Operand(next_address));
5178 __ ldr(r4, MemOperand(r9, kNextOffset));
5179 __ ldr(r5, MemOperand(r9, kLimitOffset));
5180 __ ldr(r6, MemOperand(r9, kLevelOffset));
5181 __ add(r6, r6, Operand(1));
5182 __ str(r6, MemOperand(r9, kLevelOffset));
5183
5184 if (FLAG_log_timer_events) {
5185 FrameScope frame(masm, StackFrame::MANUAL);
5186 __ PushSafepointRegisters();
5187 __ PrepareCallCFunction(1, r0);
5188 __ mov(r0, Operand(ExternalReference::isolate_address(isolate)));
5189 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5190 1);
5191 __ PopSafepointRegisters();
5192 }
5193
5194 // Native call returns to the DirectCEntry stub which redirects to the
5195 // return address pushed on stack (could have moved after GC).
5196 // DirectCEntry stub itself is generated early and never moves.
5197 DirectCEntryStub stub(isolate);
5198 stub.GenerateCall(masm, r3);
5199
5200 if (FLAG_log_timer_events) {
5201 FrameScope frame(masm, StackFrame::MANUAL);
5202 __ PushSafepointRegisters();
5203 __ PrepareCallCFunction(1, r0);
5204 __ mov(r0, Operand(ExternalReference::isolate_address(isolate)));
5205 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5206 1);
5207 __ PopSafepointRegisters();
5208 }
5209
5210 Label promote_scheduled_exception;
5211 Label delete_allocated_handles;
5212 Label leave_exit_frame;
5213 Label return_value_loaded;
5214
5215 // load value from ReturnValue
5216 __ ldr(r0, return_value_operand);
5217 __ bind(&return_value_loaded);
5218 // No more valid handles (the result handle was the last one). Restore
5219 // previous handle scope.
5220 __ str(r4, MemOperand(r9, kNextOffset));
5221 if (__ emit_debug_code()) {
5222 __ ldr(r1, MemOperand(r9, kLevelOffset));
5223 __ cmp(r1, r6);
5224 __ Check(eq, kUnexpectedLevelAfterReturnFromApiCall);
5225 }
5226 __ sub(r6, r6, Operand(1));
5227 __ str(r6, MemOperand(r9, kLevelOffset));
5228 __ ldr(ip, MemOperand(r9, kLimitOffset));
5229 __ cmp(r5, ip);
5230 __ b(ne, &delete_allocated_handles);
5231
5232 // Leave the API exit frame.
5233 __ bind(&leave_exit_frame);
5234 bool restore_context = context_restore_operand != NULL;
5235 if (restore_context) {
5236 __ ldr(cp, *context_restore_operand);
5237 }
5238 // LeaveExitFrame expects unwind space to be in a register.
5239 if (stack_space_operand != NULL) {
5240 __ ldr(r4, *stack_space_operand);
5241 } else {
5242 __ mov(r4, Operand(stack_space));
5243 }
5244 __ LeaveExitFrame(false, r4, !restore_context, stack_space_operand != NULL);
5245
5246 // Check if the function scheduled an exception.
5247 __ LoadRoot(r4, Heap::kTheHoleValueRootIndex);
5248 __ mov(ip, Operand(ExternalReference::scheduled_exception_address(isolate)));
5249 __ ldr(r5, MemOperand(ip));
5250 __ cmp(r4, r5);
5251 __ b(ne, &promote_scheduled_exception);
5252
5253 __ mov(pc, lr);
5254
5255 // Re-throw by promoting a scheduled exception.
5256 __ bind(&promote_scheduled_exception);
5257 __ TailCallRuntime(Runtime::kPromoteScheduledException);
5258
5259 // HandleScope limit has changed. Delete allocated extensions.
5260 __ bind(&delete_allocated_handles);
5261 __ str(r5, MemOperand(r9, kLimitOffset));
5262 __ mov(r4, r0);
5263 __ PrepareCallCFunction(1, r5);
5264 __ mov(r0, Operand(ExternalReference::isolate_address(isolate)));
5265 __ CallCFunction(ExternalReference::delete_handle_scope_extensions(isolate),
5266 1);
5267 __ mov(r0, r4);
5268 __ jmp(&leave_exit_frame);
5269}
5270
Ben Murdochda12d292016-06-02 14:46:10 +01005271void CallApiCallbackStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005272 // ----------- S t a t e -------------
5273 // -- r0 : callee
5274 // -- r4 : call_data
5275 // -- r2 : holder
5276 // -- r1 : api_function_address
5277 // -- cp : context
5278 // --
5279 // -- sp[0] : last argument
5280 // -- ...
5281 // -- sp[(argc - 1)* 4] : first argument
5282 // -- sp[argc * 4] : receiver
5283 // -----------------------------------
5284
5285 Register callee = r0;
5286 Register call_data = r4;
5287 Register holder = r2;
5288 Register api_function_address = r1;
5289 Register context = cp;
5290
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005291 typedef FunctionCallbackArguments FCA;
5292
5293 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5294 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5295 STATIC_ASSERT(FCA::kDataIndex == 4);
5296 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5297 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5298 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5299 STATIC_ASSERT(FCA::kHolderIndex == 0);
Ben Murdochc5610432016-08-08 18:44:38 +01005300 STATIC_ASSERT(FCA::kNewTargetIndex == 7);
5301 STATIC_ASSERT(FCA::kArgsLength == 8);
5302
5303 // new target
5304 __ PushRoot(Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005305
5306 // context save
5307 __ push(context);
Ben Murdochda12d292016-06-02 14:46:10 +01005308 if (!is_lazy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005309 // load context from callee
5310 __ ldr(context, FieldMemOperand(callee, JSFunction::kContextOffset));
5311 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005312
5313 // callee
5314 __ push(callee);
5315
5316 // call data
5317 __ push(call_data);
5318
5319 Register scratch = call_data;
Ben Murdochda12d292016-06-02 14:46:10 +01005320 if (!call_data_undefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005321 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5322 }
5323 // return value
5324 __ push(scratch);
5325 // return value default
5326 __ push(scratch);
5327 // isolate
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005328 __ mov(scratch, Operand(ExternalReference::isolate_address(masm->isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005329 __ push(scratch);
5330 // holder
5331 __ push(holder);
5332
5333 // Prepare arguments.
5334 __ mov(scratch, sp);
5335
5336 // Allocate the v8::Arguments structure in the arguments' space since
5337 // it's not controlled by GC.
Ben Murdochc5610432016-08-08 18:44:38 +01005338 const int kApiStackSpace = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005339
5340 FrameScope frame_scope(masm, StackFrame::MANUAL);
5341 __ EnterExitFrame(false, kApiStackSpace);
5342
5343 DCHECK(!api_function_address.is(r0) && !scratch.is(r0));
5344 // r0 = FunctionCallbackInfo&
5345 // Arguments is after the return address.
5346 __ add(r0, sp, Operand(1 * kPointerSize));
5347 // FunctionCallbackInfo::implicit_args_
5348 __ str(scratch, MemOperand(r0, 0 * kPointerSize));
Ben Murdochda12d292016-06-02 14:46:10 +01005349 // FunctionCallbackInfo::values_
5350 __ add(ip, scratch, Operand((FCA::kArgsLength - 1 + argc()) * kPointerSize));
5351 __ str(ip, MemOperand(r0, 1 * kPointerSize));
5352 // FunctionCallbackInfo::length_ = argc
5353 __ mov(ip, Operand(argc()));
5354 __ str(ip, MemOperand(r0, 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005355
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005356 ExternalReference thunk_ref =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005357 ExternalReference::invoke_function_callback(masm->isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005358
5359 AllowExternalCallThatCantCauseGC scope(masm);
5360 MemOperand context_restore_operand(
5361 fp, (2 + FCA::kContextSaveIndex) * kPointerSize);
5362 // Stores return the first js argument
5363 int return_value_offset = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005364 if (is_store()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005365 return_value_offset = 2 + FCA::kArgsLength;
5366 } else {
5367 return_value_offset = 2 + FCA::kReturnValueOffset;
5368 }
5369 MemOperand return_value_operand(fp, return_value_offset * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005370 int stack_space = 0;
Ben Murdochc5610432016-08-08 18:44:38 +01005371 MemOperand length_operand = MemOperand(sp, 3 * kPointerSize);
5372 MemOperand* stack_space_operand = &length_operand;
Ben Murdochda12d292016-06-02 14:46:10 +01005373 stack_space = argc() + FCA::kArgsLength + 1;
5374 stack_space_operand = NULL;
5375
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005376 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, stack_space,
5377 stack_space_operand, return_value_operand,
5378 &context_restore_operand);
5379}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005380
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005381
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005382void CallApiGetterStub::Generate(MacroAssembler* masm) {
Ben Murdochc5610432016-08-08 18:44:38 +01005383 // Build v8::PropertyCallbackInfo::args_ array on the stack and push property
5384 // name below the exit frame to make GC aware of them.
5385 STATIC_ASSERT(PropertyCallbackArguments::kShouldThrowOnErrorIndex == 0);
5386 STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 1);
5387 STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 2);
5388 STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 3);
5389 STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 4);
5390 STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 5);
5391 STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 6);
5392 STATIC_ASSERT(PropertyCallbackArguments::kArgsLength == 7);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005393
Ben Murdochc5610432016-08-08 18:44:38 +01005394 Register receiver = ApiGetterDescriptor::ReceiverRegister();
5395 Register holder = ApiGetterDescriptor::HolderRegister();
5396 Register callback = ApiGetterDescriptor::CallbackRegister();
5397 Register scratch = r4;
5398 DCHECK(!AreAliased(receiver, holder, callback, scratch));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005399
Ben Murdochc5610432016-08-08 18:44:38 +01005400 Register api_function_address = r2;
5401
5402 __ push(receiver);
5403 // Push data from AccessorInfo.
5404 __ ldr(scratch, FieldMemOperand(callback, AccessorInfo::kDataOffset));
5405 __ push(scratch);
5406 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5407 __ Push(scratch, scratch);
5408 __ mov(scratch, Operand(ExternalReference::isolate_address(isolate())));
5409 __ Push(scratch, holder);
5410 __ Push(Smi::FromInt(0)); // should_throw_on_error -> false
5411 __ ldr(scratch, FieldMemOperand(callback, AccessorInfo::kNameOffset));
5412 __ push(scratch);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005413 // v8::PropertyCallbackInfo::args_ array and name handle.
5414 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5415
5416 // Load address of v8::PropertyAccessorInfo::args_ array and name handle.
5417 __ mov(r0, sp); // r0 = Handle<Name>
5418 __ add(r1, r0, Operand(1 * kPointerSize)); // r1 = v8::PCI::args_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005419
5420 const int kApiStackSpace = 1;
5421 FrameScope frame_scope(masm, StackFrame::MANUAL);
5422 __ EnterExitFrame(false, kApiStackSpace);
5423
Ben Murdoch097c5b22016-05-18 11:27:45 +01005424 // Create v8::PropertyCallbackInfo object on the stack and initialize
5425 // it's args_ field.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005426 __ str(r1, MemOperand(sp, 1 * kPointerSize));
Ben Murdoch097c5b22016-05-18 11:27:45 +01005427 __ add(r1, sp, Operand(1 * kPointerSize)); // r1 = v8::PropertyCallbackInfo&
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005428
5429 ExternalReference thunk_ref =
5430 ExternalReference::invoke_accessor_getter_callback(isolate());
Ben Murdoch097c5b22016-05-18 11:27:45 +01005431
Ben Murdochc5610432016-08-08 18:44:38 +01005432 __ ldr(scratch, FieldMemOperand(callback, AccessorInfo::kJsGetterOffset));
5433 __ ldr(api_function_address,
5434 FieldMemOperand(scratch, Foreign::kForeignAddressOffset));
5435
Ben Murdoch097c5b22016-05-18 11:27:45 +01005436 // +3 is to skip prolog, return address and name handle.
5437 MemOperand return_value_operand(
5438 fp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005439 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005440 kStackUnwindSpace, NULL, return_value_operand, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005441}
5442
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005443#undef __
5444
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005445} // namespace internal
5446} // namespace v8
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005447
5448#endif // V8_TARGET_ARCH_ARM