blob: 541e73e2fa407e4e9614090b4d5b0fce1ded9e9f [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.
Steve Block44f0eee2011-05-26 01:26:41 +01004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#if V8_TARGET_ARCH_MIPS
Steve Block44f0eee2011-05-26 01:26:41 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/base/bits.h"
8#include "src/bootstrapper.h"
9#include "src/code-stubs.h"
10#include "src/codegen.h"
11#include "src/ic/handler-compiler.h"
12#include "src/ic/ic.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000013#include "src/ic/stub-cache.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000014#include "src/isolate.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000015#include "src/mips/code-stubs-mips.h"
16#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"
Steve Block44f0eee2011-05-26 01:26:41 +010019
20namespace v8 {
21namespace internal {
22
23
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024static void InitializeArrayConstructorDescriptor(
25 Isolate* isolate, CodeStubDescriptor* descriptor,
26 int constant_stack_parameter_count) {
27 Address deopt_handler = Runtime::FunctionForId(
28 Runtime::kArrayConstructor)->entry;
29
30 if (constant_stack_parameter_count == 0) {
31 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
32 JS_FUNCTION_STUB_MODE);
33 } else {
34 descriptor->Initialize(a0, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000035 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000036 }
37}
38
39
40static void InitializeInternalArrayConstructorDescriptor(
41 Isolate* isolate, CodeStubDescriptor* descriptor,
42 int constant_stack_parameter_count) {
43 Address deopt_handler = Runtime::FunctionForId(
44 Runtime::kInternalArrayConstructor)->entry;
45
46 if (constant_stack_parameter_count == 0) {
47 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
48 JS_FUNCTION_STUB_MODE);
49 } else {
50 descriptor->Initialize(a0, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000051 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000052 }
53}
54
55
56void ArrayNoArgumentConstructorStub::InitializeDescriptor(
57 CodeStubDescriptor* descriptor) {
58 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
59}
60
61
62void ArraySingleArgumentConstructorStub::InitializeDescriptor(
63 CodeStubDescriptor* descriptor) {
64 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
65}
66
67
68void ArrayNArgumentsConstructorStub::InitializeDescriptor(
69 CodeStubDescriptor* descriptor) {
70 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
71}
72
73
74void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
75 CodeStubDescriptor* descriptor) {
76 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
77}
78
79
80void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
81 CodeStubDescriptor* descriptor) {
82 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
83}
84
85
86void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
87 CodeStubDescriptor* descriptor) {
88 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
89}
90
91
Steve Block44f0eee2011-05-26 01:26:41 +010092#define __ ACCESS_MASM(masm)
93
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000094static void EmitIdenticalObjectComparison(MacroAssembler* masm, Label* slow,
Ben Murdoch097c5b22016-05-18 11:27:45 +010095 Condition cc);
Ben Murdoch257744e2011-11-30 15:57:28 +000096static void EmitSmiNonsmiComparison(MacroAssembler* masm,
97 Register lhs,
98 Register rhs,
99 Label* rhs_not_nan,
100 Label* slow,
101 bool strict);
Ben Murdoch257744e2011-11-30 15:57:28 +0000102static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
103 Register lhs,
104 Register rhs);
105
106
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000107void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
108 ExternalReference miss) {
109 // Update the static counter each time a new code stub is generated.
110 isolate()->counters()->code_stubs()->Increment();
Ben Murdoch257744e2011-11-30 15:57:28 +0000111
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000112 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000113 int param_count = descriptor.GetRegisterParameterCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 {
115 // Call the runtime system in a fresh internal frame.
116 FrameScope scope(masm, StackFrame::INTERNAL);
117 DCHECK(param_count == 0 ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000118 a0.is(descriptor.GetRegisterParameter(param_count - 1)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000119 // Push arguments, adjust sp.
120 __ Subu(sp, sp, Operand(param_count * kPointerSize));
121 for (int i = 0; i < param_count; ++i) {
122 // Store argument to stack.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123 __ sw(descriptor.GetRegisterParameter(i),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000124 MemOperand(sp, (param_count - 1 - i) * kPointerSize));
125 }
126 __ CallExternalReference(miss, param_count);
127 }
Steve Block44f0eee2011-05-26 01:26:41 +0100128
Ben Murdoch257744e2011-11-30 15:57:28 +0000129 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100130}
131
132
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000133void DoubleToIStub::Generate(MacroAssembler* masm) {
134 Label out_of_range, only_low, negate, done;
135 Register input_reg = source();
136 Register result_reg = destination();
Ben Murdoch257744e2011-11-30 15:57:28 +0000137
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000138 int double_offset = offset();
139 // Account for saved regs if input is sp.
140 if (input_reg.is(sp)) double_offset += 3 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +0000141
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000142 Register scratch =
143 GetRegisterThatIsNotOneOf(input_reg, result_reg);
144 Register scratch2 =
145 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch);
146 Register scratch3 =
147 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch, scratch2);
148 DoubleRegister double_scratch = kLithiumScratchDouble;
Ben Murdoch257744e2011-11-30 15:57:28 +0000149
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 __ Push(scratch, scratch2, scratch3);
Ben Murdoch257744e2011-11-30 15:57:28 +0000151
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 if (!skip_fastpath()) {
153 // Load double input.
154 __ ldc1(double_scratch, MemOperand(input_reg, double_offset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000155
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000156 // Clear cumulative exception flags and save the FCSR.
157 __ cfc1(scratch2, FCSR);
158 __ ctc1(zero_reg, FCSR);
Ben Murdoch257744e2011-11-30 15:57:28 +0000159
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 // Try a conversion to a signed integer.
161 __ Trunc_w_d(double_scratch, double_scratch);
162 // Move the converted value into the result register.
163 __ mfc1(scratch3, double_scratch);
Ben Murdoch257744e2011-11-30 15:57:28 +0000164
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000165 // Retrieve and restore the FCSR.
166 __ cfc1(scratch, FCSR);
167 __ ctc1(scratch2, FCSR);
Steve Block44f0eee2011-05-26 01:26:41 +0100168
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000169 // Check for overflow and NaNs.
170 __ And(
171 scratch, scratch,
172 kFCSROverflowFlagMask | kFCSRUnderflowFlagMask
173 | kFCSRInvalidOpFlagMask);
174 // If we had no exceptions then set result_reg and we are done.
175 Label error;
176 __ Branch(&error, ne, scratch, Operand(zero_reg));
177 __ Move(result_reg, scratch3);
Ben Murdoch257744e2011-11-30 15:57:28 +0000178 __ Branch(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000179 __ bind(&error);
Ben Murdoch257744e2011-11-30 15:57:28 +0000180 }
181
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000182 // Load the double value and perform a manual truncation.
183 Register input_high = scratch2;
184 Register input_low = scratch3;
Ben Murdoch257744e2011-11-30 15:57:28 +0000185
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000186 __ lw(input_low,
187 MemOperand(input_reg, double_offset + Register::kMantissaOffset));
188 __ lw(input_high,
189 MemOperand(input_reg, double_offset + Register::kExponentOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000190
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000191 Label normal_exponent, restore_sign;
192 // Extract the biased exponent in result.
193 __ Ext(result_reg,
194 input_high,
Ben Murdoch257744e2011-11-30 15:57:28 +0000195 HeapNumber::kExponentShift,
196 HeapNumber::kExponentBits);
197
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000198 // Check for Infinity and NaNs, which should return 0.
199 __ Subu(scratch, result_reg, HeapNumber::kExponentMask);
200 __ Movz(result_reg, zero_reg, scratch);
201 __ Branch(&done, eq, scratch, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000202
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000203 // Express exponent as delta to (number of mantissa bits + 31).
204 __ Subu(result_reg,
205 result_reg,
206 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31));
Ben Murdoch257744e2011-11-30 15:57:28 +0000207
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 // If the delta is strictly positive, all bits would be shifted away,
209 // which means that we can return 0.
210 __ Branch(&normal_exponent, le, result_reg, Operand(zero_reg));
211 __ mov(result_reg, zero_reg);
212 __ Branch(&done);
Ben Murdoch257744e2011-11-30 15:57:28 +0000213
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 __ bind(&normal_exponent);
215 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
216 // Calculate shift.
217 __ Addu(scratch, result_reg, Operand(kShiftBase + HeapNumber::kMantissaBits));
Ben Murdoch257744e2011-11-30 15:57:28 +0000218
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000219 // Save the sign.
220 Register sign = result_reg;
221 result_reg = no_reg;
222 __ And(sign, input_high, Operand(HeapNumber::kSignMask));
Ben Murdoch257744e2011-11-30 15:57:28 +0000223
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000224 // On ARM shifts > 31 bits are valid and will result in zero. On MIPS we need
225 // to check for this specific case.
226 Label high_shift_needed, high_shift_done;
227 __ Branch(&high_shift_needed, lt, scratch, Operand(32));
228 __ mov(input_high, zero_reg);
229 __ Branch(&high_shift_done);
230 __ bind(&high_shift_needed);
231
232 // Set the implicit 1 before the mantissa part in input_high.
233 __ Or(input_high,
234 input_high,
235 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
236 // Shift the mantissa bits to the correct position.
237 // We don't need to clear non-mantissa bits as they will be shifted away.
238 // If they weren't, it would mean that the answer is in the 32bit range.
239 __ sllv(input_high, input_high, scratch);
240
241 __ bind(&high_shift_done);
242
243 // Replace the shifted bits with bits from the lower mantissa word.
244 Label pos_shift, shift_done;
Ben Murdoch257744e2011-11-30 15:57:28 +0000245 __ li(at, 32);
246 __ subu(scratch, at, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000247 __ Branch(&pos_shift, ge, scratch, Operand(zero_reg));
248
249 // Negate scratch.
250 __ Subu(scratch, zero_reg, scratch);
251 __ sllv(input_low, input_low, scratch);
252 __ Branch(&shift_done);
253
254 __ bind(&pos_shift);
255 __ srlv(input_low, input_low, scratch);
256
257 __ bind(&shift_done);
258 __ Or(input_high, input_high, Operand(input_low));
259 // Restore sign if necessary.
260 __ mov(scratch, sign);
261 result_reg = sign;
262 sign = no_reg;
263 __ Subu(result_reg, zero_reg, input_high);
264 __ Movz(result_reg, input_high, scratch);
265
266 __ bind(&done);
267
268 __ Pop(scratch, scratch2, scratch3);
269 __ Ret();
Ben Murdoch257744e2011-11-30 15:57:28 +0000270}
271
272
Ben Murdoch257744e2011-11-30 15:57:28 +0000273// Handle the case where the lhs and rhs are the same object.
274// Equality is almost reflexive (everything but NaN), so this is a test
275// for "identity and not NaN".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000276static void EmitIdenticalObjectComparison(MacroAssembler* masm, Label* slow,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100277 Condition cc) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000278 Label not_identical;
279 Label heap_number, return_equal;
280 Register exp_mask_reg = t5;
281
282 __ Branch(&not_identical, ne, a0, Operand(a1));
283
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000284 __ li(exp_mask_reg, Operand(HeapNumber::kExponentMask));
Ben Murdoch257744e2011-11-30 15:57:28 +0000285
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000286 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
287 // so we do the second best thing - test it ourselves.
288 // They are both equal and they are not both Smis so both of them are not
289 // Smis. If it's not a heap number, then return equal.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000290 __ GetObjectType(a0, t4, t4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000291 if (cc == less || cc == greater) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000292 // Call runtime on identical JSObjects.
293 __ Branch(slow, greater, t4, Operand(FIRST_JS_RECEIVER_TYPE));
294 // Call runtime on identical symbols since we need to throw a TypeError.
295 __ Branch(slow, eq, t4, Operand(SYMBOL_TYPE));
296 // Call runtime on identical SIMD values since we must throw a TypeError.
297 __ Branch(slow, eq, t4, Operand(SIMD128_VALUE_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000298 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000299 __ Branch(&heap_number, eq, t4, Operand(HEAP_NUMBER_TYPE));
300 // Comparing JS objects with <=, >= is complicated.
301 if (cc != eq) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100302 __ Branch(slow, greater, t4, Operand(FIRST_JS_RECEIVER_TYPE));
303 // Call runtime on identical symbols since we need to throw a TypeError.
304 __ Branch(slow, eq, t4, Operand(SYMBOL_TYPE));
305 // Call runtime on identical SIMD values since we must throw a TypeError.
306 __ Branch(slow, eq, t4, Operand(SIMD128_VALUE_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000307 // Normally here we fall through to return_equal, but undefined is
308 // special: (undefined == undefined) == true, but
309 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
310 if (cc == less_equal || cc == greater_equal) {
311 __ Branch(&return_equal, ne, t4, Operand(ODDBALL_TYPE));
312 __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
313 __ Branch(&return_equal, ne, a0, Operand(t2));
314 DCHECK(is_int16(GREATER) && is_int16(LESS));
315 __ Ret(USE_DELAY_SLOT);
316 if (cc == le) {
317 // undefined <= undefined should fail.
318 __ li(v0, Operand(GREATER));
319 } else {
320 // undefined >= undefined should fail.
321 __ li(v0, Operand(LESS));
Ben Murdoch257744e2011-11-30 15:57:28 +0000322 }
323 }
324 }
325 }
326
327 __ bind(&return_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328 DCHECK(is_int16(GREATER) && is_int16(LESS));
329 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000330 if (cc == less) {
331 __ li(v0, Operand(GREATER)); // Things aren't less than themselves.
332 } else if (cc == greater) {
333 __ li(v0, Operand(LESS)); // Things aren't greater than themselves.
334 } else {
335 __ mov(v0, zero_reg); // Things are <=, >=, ==, === themselves.
336 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000337
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000338 // For less and greater we don't have to check for NaN since the result of
339 // x < x is false regardless. For the others here is some code to check
340 // for NaN.
341 if (cc != lt && cc != gt) {
342 __ bind(&heap_number);
343 // It is a heap number, so return non-equal if it's NaN and equal if it's
344 // not NaN.
Ben Murdoch257744e2011-11-30 15:57:28 +0000345
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000346 // The representation of NaN values has all exponent bits (52..62) set,
347 // and not all mantissa bits (0..51) clear.
348 // Read top bits of double representation (second word of value).
349 __ lw(t2, FieldMemOperand(a0, HeapNumber::kExponentOffset));
350 // Test that exponent bits are all set.
351 __ And(t3, t2, Operand(exp_mask_reg));
352 // If all bits not set (ne cond), then not a NaN, objects are equal.
353 __ Branch(&return_equal, ne, t3, Operand(exp_mask_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000354
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000355 // Shift out flag and all exponent bits, retaining only mantissa.
356 __ sll(t2, t2, HeapNumber::kNonMantissaBitsInTopWord);
357 // Or with all low-bits of mantissa.
358 __ lw(t3, FieldMemOperand(a0, HeapNumber::kMantissaOffset));
359 __ Or(v0, t3, Operand(t2));
360 // For equal we already have the right value in v0: Return zero (equal)
361 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
362 // not (it's a NaN). For <= and >= we need to load v0 with the failing
363 // value if it's a NaN.
364 if (cc != eq) {
365 // All-zero means Infinity means equal.
366 __ Ret(eq, v0, Operand(zero_reg));
367 DCHECK(is_int16(GREATER) && is_int16(LESS));
368 __ Ret(USE_DELAY_SLOT);
369 if (cc == le) {
370 __ li(v0, Operand(GREATER)); // NaN <= NaN should fail.
371 } else {
372 __ li(v0, Operand(LESS)); // NaN >= NaN should fail.
Ben Murdoch257744e2011-11-30 15:57:28 +0000373 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000374 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000375 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376 // No fall through here.
Ben Murdoch257744e2011-11-30 15:57:28 +0000377
378 __ bind(&not_identical);
379}
380
381
382static void EmitSmiNonsmiComparison(MacroAssembler* masm,
383 Register lhs,
384 Register rhs,
385 Label* both_loaded_as_doubles,
386 Label* slow,
387 bool strict) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000388 DCHECK((lhs.is(a0) && rhs.is(a1)) ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000389 (lhs.is(a1) && rhs.is(a0)));
390
391 Label lhs_is_smi;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100392 __ JumpIfSmi(lhs, &lhs_is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +0000393 // Rhs is a Smi.
394 // Check whether the non-smi is a heap number.
395 __ GetObjectType(lhs, t4, t4);
396 if (strict) {
397 // If lhs was not a number and rhs was a Smi then strict equality cannot
398 // succeed. Return non-equal (lhs is already not zero).
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100399 __ Ret(USE_DELAY_SLOT, ne, t4, Operand(HEAP_NUMBER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000400 __ mov(v0, lhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000401 } else {
402 // Smi compared non-strictly with a non-Smi non-heap-number. Call
403 // the runtime.
404 __ Branch(slow, ne, t4, Operand(HEAP_NUMBER_TYPE));
405 }
406
407 // Rhs is a smi, lhs is a number.
408 // Convert smi rhs to double.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409 __ sra(at, rhs, kSmiTagSize);
410 __ mtc1(at, f14);
411 __ cvt_d_w(f14, f14);
412 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000413
414 // We now have both loaded as doubles.
415 __ jmp(both_loaded_as_doubles);
416
417 __ bind(&lhs_is_smi);
418 // Lhs is a Smi. Check whether the non-smi is a heap number.
419 __ GetObjectType(rhs, t4, t4);
420 if (strict) {
421 // If lhs was not a number and rhs was a Smi then strict equality cannot
422 // succeed. Return non-equal.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100423 __ Ret(USE_DELAY_SLOT, ne, t4, Operand(HEAP_NUMBER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000424 __ li(v0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +0000425 } else {
426 // Smi compared non-strictly with a non-Smi non-heap-number. Call
427 // the runtime.
428 __ Branch(slow, ne, t4, Operand(HEAP_NUMBER_TYPE));
429 }
430
431 // Lhs is a smi, rhs is a number.
432 // Convert smi lhs to double.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000433 __ sra(at, lhs, kSmiTagSize);
434 __ mtc1(at, f12);
435 __ cvt_d_w(f12, f12);
436 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000437 // Fall through to both_loaded_as_doubles.
Steve Block44f0eee2011-05-26 01:26:41 +0100438}
439
440
Ben Murdoch257744e2011-11-30 15:57:28 +0000441static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
442 Register lhs,
443 Register rhs) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000444 // If either operand is a JS object or an oddball value, then they are
Ben Murdoch257744e2011-11-30 15:57:28 +0000445 // not equal since their pointers are different.
446 // There is no test for undetectability in strict equality.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000447 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +0000448 Label first_non_object;
449 // Get the type of the first operand into a2 and compare it with
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000450 // FIRST_JS_RECEIVER_TYPE.
Ben Murdoch257744e2011-11-30 15:57:28 +0000451 __ GetObjectType(lhs, a2, a2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000452 __ Branch(&first_non_object, less, a2, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000453
454 // Return non-zero.
455 Label return_not_equal;
456 __ bind(&return_not_equal);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100457 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000458 __ li(v0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +0000459
460 __ bind(&first_non_object);
461 // Check for oddballs: true, false, null, undefined.
462 __ Branch(&return_not_equal, eq, a2, Operand(ODDBALL_TYPE));
463
464 __ GetObjectType(rhs, a3, a3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000465 __ Branch(&return_not_equal, greater, a3, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000466
467 // Check for oddballs: true, false, null, undefined.
468 __ Branch(&return_not_equal, eq, a3, Operand(ODDBALL_TYPE));
469
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000470 // Now that we have the types we might as well check for
471 // internalized-internalized.
472 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
473 __ Or(a2, a2, Operand(a3));
474 __ And(at, a2, Operand(kIsNotStringMask | kIsNotInternalizedMask));
475 __ Branch(&return_not_equal, eq, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000476}
477
478
479static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
480 Register lhs,
481 Register rhs,
482 Label* both_loaded_as_doubles,
483 Label* not_heap_numbers,
484 Label* slow) {
485 __ GetObjectType(lhs, a3, a2);
486 __ Branch(not_heap_numbers, ne, a2, Operand(HEAP_NUMBER_TYPE));
487 __ lw(a2, FieldMemOperand(rhs, HeapObject::kMapOffset));
488 // If first was a heap number & second wasn't, go to slow case.
489 __ Branch(slow, ne, a3, Operand(a2));
490
491 // Both are heap numbers. Load them up then jump to the code we have
492 // for that.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000493 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
494 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
495
Ben Murdoch257744e2011-11-30 15:57:28 +0000496 __ jmp(both_loaded_as_doubles);
497}
498
499
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000500// Fast negative check for internalized-to-internalized equality.
501static void EmitCheckForInternalizedStringsOrObjects(MacroAssembler* masm,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100502 Register lhs, Register rhs,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000503 Label* possible_strings,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100504 Label* runtime_call) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000505 DCHECK((lhs.is(a0) && rhs.is(a1)) ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000506 (lhs.is(a1) && rhs.is(a0)));
507
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000508 // a2 is object type of rhs.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100509 Label object_test, return_unequal, undetectable;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000510 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000511 __ And(at, a2, Operand(kIsNotStringMask));
512 __ Branch(&object_test, ne, at, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000513 __ And(at, a2, Operand(kIsNotInternalizedMask));
514 __ Branch(possible_strings, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000515 __ GetObjectType(rhs, a3, a3);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100516 __ Branch(runtime_call, ge, a3, Operand(FIRST_NONSTRING_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000517 __ And(at, a3, Operand(kIsNotInternalizedMask));
518 __ Branch(possible_strings, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000519
Ben Murdoch097c5b22016-05-18 11:27:45 +0100520 // Both are internalized. We already checked they weren't the same pointer so
521 // they are not equal. Return non-equal by returning the non-zero object
522 // pointer in v0.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100523 __ Ret(USE_DELAY_SLOT);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100524 __ mov(v0, a0); // In delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +0000525
526 __ bind(&object_test);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100527 __ lw(a2, FieldMemOperand(lhs, HeapObject::kMapOffset));
528 __ lw(a3, FieldMemOperand(rhs, HeapObject::kMapOffset));
529 __ lbu(t0, FieldMemOperand(a2, Map::kBitFieldOffset));
530 __ lbu(t1, FieldMemOperand(a3, Map::kBitFieldOffset));
531 __ And(at, t0, Operand(1 << Map::kIsUndetectable));
532 __ Branch(&undetectable, ne, at, Operand(zero_reg));
533 __ And(at, t1, Operand(1 << Map::kIsUndetectable));
534 __ Branch(&return_unequal, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000535
Ben Murdoch097c5b22016-05-18 11:27:45 +0100536 __ GetInstanceType(a2, a2);
537 __ Branch(runtime_call, lt, a2, Operand(FIRST_JS_RECEIVER_TYPE));
538 __ GetInstanceType(a3, a3);
539 __ Branch(runtime_call, lt, a3, Operand(FIRST_JS_RECEIVER_TYPE));
540
541 __ bind(&return_unequal);
542 // Return non-equal by returning the non-zero object pointer in v0.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100543 __ Ret(USE_DELAY_SLOT);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100544 __ mov(v0, a0); // In delay slot.
545
546 __ bind(&undetectable);
547 __ And(at, t1, Operand(1 << Map::kIsUndetectable));
548 __ Branch(&return_unequal, eq, at, Operand(zero_reg));
549 __ Ret(USE_DELAY_SLOT);
550 __ li(v0, Operand(EQUAL)); // In delay slot.
Steve Block44f0eee2011-05-26 01:26:41 +0100551}
552
553
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000554static void CompareICStub_CheckInputType(MacroAssembler* masm, Register input,
555 Register scratch,
556 CompareICState::State expected,
557 Label* fail) {
558 Label ok;
559 if (expected == CompareICState::SMI) {
560 __ JumpIfNotSmi(input, fail);
561 } else if (expected == CompareICState::NUMBER) {
562 __ JumpIfSmi(input, &ok);
563 __ CheckMap(input, scratch, Heap::kHeapNumberMapRootIndex, fail,
564 DONT_DO_SMI_CHECK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000565 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000566 // We could be strict about internalized/string here, but as long as
567 // hydrogen doesn't care, the stub doesn't have to care either.
568 __ bind(&ok);
Steve Block44f0eee2011-05-26 01:26:41 +0100569}
570
571
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572// On entry a1 and a2 are the values to be compared.
573// On exit a0 is 0, positive or negative to indicate the result of
574// the comparison.
575void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
576 Register lhs = a1;
577 Register rhs = a0;
578 Condition cc = GetCondition();
Ben Murdoch257744e2011-11-30 15:57:28 +0000579
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000580 Label miss;
581 CompareICStub_CheckInputType(masm, lhs, a2, left(), &miss);
582 CompareICStub_CheckInputType(masm, rhs, a3, right(), &miss);
Ben Murdoch257744e2011-11-30 15:57:28 +0000583
Ben Murdoch257744e2011-11-30 15:57:28 +0000584 Label slow; // Call builtin.
585 Label not_smis, both_loaded_as_doubles;
586
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000587 Label not_two_smis, smi_done;
588 __ Or(a2, a1, a0);
589 __ JumpIfNotSmi(a2, &not_two_smis);
590 __ sra(a1, a1, 1);
591 __ sra(a0, a0, 1);
592 __ Ret(USE_DELAY_SLOT);
593 __ subu(v0, a1, a0);
594 __ bind(&not_two_smis);
Ben Murdoch257744e2011-11-30 15:57:28 +0000595
596 // NOTICE! This code is only reached after a smi-fast-case check, so
597 // it is certain that at least one operand isn't a smi.
598
599 // Handle the case where the objects are identical. Either returns the answer
600 // or goes to slow. Only falls through if the objects were not identical.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100601 EmitIdenticalObjectComparison(masm, &slow, cc);
Ben Murdoch257744e2011-11-30 15:57:28 +0000602
603 // If either is a Smi (we know that not both are), then they can only
604 // be strictly equal if the other is a HeapNumber.
605 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000606 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000607 __ And(t2, lhs, Operand(rhs));
Ben Murdoch257744e2011-11-30 15:57:28 +0000608 __ JumpIfNotSmi(t2, &not_smis, t0);
609 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
610 // 1) Return the answer.
611 // 2) Go to slow.
612 // 3) Fall through to both_loaded_as_doubles.
613 // 4) Jump to rhs_not_nan.
614 // In cases 3 and 4 we have found out we were dealing with a number-number
615 // comparison and the numbers have been loaded into f12 and f14 as doubles,
616 // or in GP registers (a0, a1, a2, a3) depending on the presence of the FPU.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000617 EmitSmiNonsmiComparison(masm, lhs, rhs,
618 &both_loaded_as_doubles, &slow, strict());
Ben Murdoch257744e2011-11-30 15:57:28 +0000619
620 __ bind(&both_loaded_as_doubles);
621 // f12, f14 are the double representations of the left hand side
622 // and the right hand side if we have FPU. Otherwise a2, a3 represent
623 // left hand side and a0, a1 represent right hand side.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000624 Label nan;
625 __ li(t0, Operand(LESS));
626 __ li(t1, Operand(GREATER));
627 __ li(t2, Operand(EQUAL));
Ben Murdoch257744e2011-11-30 15:57:28 +0000628
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000629 // Check if either rhs or lhs is NaN.
630 __ BranchF(NULL, &nan, eq, f12, f14);
Ben Murdoch257744e2011-11-30 15:57:28 +0000631
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000632 // Check if LESS condition is satisfied. If true, move conditionally
633 // result to v0.
634 if (!IsMipsArchVariant(kMips32r6)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000635 __ c(OLT, D, f12, f14);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100636 __ Movt(v0, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000637 // Use previous check to store conditionally to v0 oposite condition
638 // (GREATER). If rhs is equal to lhs, this will be corrected in next
639 // check.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100640 __ Movf(v0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000641 // Check if EQUAL condition is satisfied. If true, move conditionally
642 // result to v0.
643 __ c(EQ, D, f12, f14);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100644 __ Movt(v0, t2);
Ben Murdoch257744e2011-11-30 15:57:28 +0000645 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000646 Label skip;
647 __ BranchF(USE_DELAY_SLOT, &skip, NULL, lt, f12, f14);
648 __ mov(v0, t0); // Return LESS as result.
Ben Murdoch257744e2011-11-30 15:57:28 +0000649
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650 __ BranchF(USE_DELAY_SLOT, &skip, NULL, eq, f12, f14);
651 __ mov(v0, t2); // Return EQUAL as result.
652
653 __ mov(v0, t1); // Return GREATER as result.
654 __ bind(&skip);
Ben Murdoch257744e2011-11-30 15:57:28 +0000655 }
656
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000657 __ Ret();
658
659 __ bind(&nan);
660 // NaN comparisons always fail.
661 // Load whatever we need in v0 to make the comparison fail.
662 DCHECK(is_int16(GREATER) && is_int16(LESS));
663 __ Ret(USE_DELAY_SLOT);
664 if (cc == lt || cc == le) {
665 __ li(v0, Operand(GREATER));
666 } else {
667 __ li(v0, Operand(LESS));
668 }
669
670
Ben Murdoch257744e2011-11-30 15:57:28 +0000671 __ bind(&not_smis);
672 // At this point we know we are dealing with two different objects,
673 // and neither of them is a Smi. The objects are in lhs_ and rhs_.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000674 if (strict()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000675 // This returns non-equal for some object types, or falls through if it
676 // was not lucky.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000677 EmitStrictTwoHeapObjectCompare(masm, lhs, rhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000678 }
679
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000680 Label check_for_internalized_strings;
Ben Murdoch257744e2011-11-30 15:57:28 +0000681 Label flat_string_check;
682 // Check for heap-number-heap-number comparison. Can jump to slow case,
683 // or load both doubles and jump to the code that handles
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000684 // that case. If the inputs are not doubles then jumps to
685 // check_for_internalized_strings.
Ben Murdoch257744e2011-11-30 15:57:28 +0000686 // In this case a2 will contain the type of lhs_.
687 EmitCheckForTwoHeapNumbers(masm,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000688 lhs,
689 rhs,
Ben Murdoch257744e2011-11-30 15:57:28 +0000690 &both_loaded_as_doubles,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000691 &check_for_internalized_strings,
Ben Murdoch257744e2011-11-30 15:57:28 +0000692 &flat_string_check);
693
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000694 __ bind(&check_for_internalized_strings);
695 if (cc == eq && !strict()) {
696 // Returns an answer for two internalized strings or two
697 // detectable objects.
Ben Murdoch257744e2011-11-30 15:57:28 +0000698 // Otherwise jumps to string case or not both strings case.
699 // Assumes that a2 is the type of lhs_ on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700 EmitCheckForInternalizedStringsOrObjects(
701 masm, lhs, rhs, &flat_string_check, &slow);
Ben Murdoch257744e2011-11-30 15:57:28 +0000702 }
703
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000704 // Check for both being sequential one-byte strings,
705 // and inline if that is the case.
Ben Murdoch257744e2011-11-30 15:57:28 +0000706 __ bind(&flat_string_check);
707
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000708 __ JumpIfNonSmisNotBothSequentialOneByteStrings(lhs, rhs, a2, a3, &slow);
Ben Murdoch257744e2011-11-30 15:57:28 +0000709
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000710 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1, a2,
711 a3);
712 if (cc == eq) {
713 StringHelper::GenerateFlatOneByteStringEquals(masm, lhs, rhs, a2, a3, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000714 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000715 StringHelper::GenerateCompareFlatOneByteStrings(masm, lhs, rhs, a2, a3, t0,
716 t1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000717 }
718 // Never falls through to here.
719
720 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000721 if (cc == eq) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100722 {
723 FrameScope scope(masm, StackFrame::INTERNAL);
724 __ Push(lhs, rhs);
725 __ CallRuntime(strict() ? Runtime::kStrictEqual : Runtime::kEqual);
726 }
727 // Turn true into 0 and false into some non-zero value.
728 STATIC_ASSERT(EQUAL == 0);
729 __ LoadRoot(a0, Heap::kTrueValueRootIndex);
730 __ Ret(USE_DELAY_SLOT);
731 __ subu(v0, v0, a0); // In delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +0000732 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100733 // Prepare for call to builtin. Push object pointers, a0 (lhs) first,
734 // a1 (rhs) second.
735 __ Push(lhs, rhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000736 int ncr; // NaN compare result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000737 if (cc == lt || cc == le) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000738 ncr = GREATER;
739 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000740 DCHECK(cc == gt || cc == ge); // Remaining cases.
Ben Murdoch257744e2011-11-30 15:57:28 +0000741 ncr = LESS;
742 }
743 __ li(a0, Operand(Smi::FromInt(ncr)));
744 __ push(a0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000745
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000746 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
747 // tagged as a small integer.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100748 __ TailCallRuntime(Runtime::kCompare);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000749 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000750
751 __ bind(&miss);
752 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +0100753}
754
755
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756void StoreRegistersStateStub::Generate(MacroAssembler* masm) {
757 __ mov(t9, ra);
758 __ pop(ra);
759 __ PushSafepointRegisters();
760 __ Jump(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100761}
Ben Murdoch257744e2011-11-30 15:57:28 +0000762
Ben Murdoch257744e2011-11-30 15:57:28 +0000763
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000764void RestoreRegistersStateStub::Generate(MacroAssembler* masm) {
765 __ mov(t9, ra);
766 __ pop(ra);
767 __ PopSafepointRegisters();
768 __ Jump(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100769}
770
771
772void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
773 // We don't allow a GC during a store buffer overflow so there is no need to
774 // store the registers in any particular way, but we do have to store and
775 // restore them.
776 __ MultiPush(kJSCallerSaved | ra.bit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000777 if (save_doubles()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100778 __ MultiPushFPU(kCallerSavedFPU);
779 }
780 const int argument_count = 1;
781 const int fp_argument_count = 0;
782 const Register scratch = a1;
783
784 AllowExternalCallThatCantCauseGC scope(masm);
785 __ PrepareCallCFunction(argument_count, fp_argument_count, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000786 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100787 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000788 ExternalReference::store_buffer_overflow_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100789 argument_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790 if (save_doubles()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100791 __ MultiPopFPU(kCallerSavedFPU);
792 }
793
794 __ MultiPop(kJSCallerSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +0000795 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100796}
797
798
Ben Murdoch257744e2011-11-30 15:57:28 +0000799void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100800 const Register base = a1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000801 const Register exponent = MathPowTaggedDescriptor::exponent();
802 DCHECK(exponent.is(a2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100803 const Register heapnumbermap = t1;
804 const Register heapnumber = v0;
805 const DoubleRegister double_base = f2;
806 const DoubleRegister double_exponent = f4;
807 const DoubleRegister double_result = f0;
808 const DoubleRegister double_scratch = f6;
809 const FPURegister single_scratch = f8;
810 const Register scratch = t5;
811 const Register scratch2 = t3;
Ben Murdoch257744e2011-11-30 15:57:28 +0000812
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100813 Label call_runtime, done, int_exponent;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000814 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100815 Label base_is_smi, unpack_exponent;
816 // The exponent and base are supplied as arguments on the stack.
817 // This can only happen if the stub is called from non-optimized code.
818 // Load input parameters from stack to double registers.
Ben Murdoch257744e2011-11-30 15:57:28 +0000819 __ lw(base, MemOperand(sp, 1 * kPointerSize));
820 __ lw(exponent, MemOperand(sp, 0 * kPointerSize));
821
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100822 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +0000823
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100824 __ UntagAndJumpIfSmi(scratch, base, &base_is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +0000825 __ lw(scratch, FieldMemOperand(base, JSObject::kMapOffset));
826 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100827
Ben Murdochc7cc0282012-03-05 14:35:55 +0000828 __ ldc1(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100829 __ jmp(&unpack_exponent);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000830
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100831 __ bind(&base_is_smi);
832 __ mtc1(scratch, single_scratch);
833 __ cvt_d_w(double_base, single_scratch);
834 __ bind(&unpack_exponent);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000835
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100836 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
Ben Murdoch85b71792012-04-11 18:30:58 +0100837
Ben Murdoch85b71792012-04-11 18:30:58 +0100838 __ lw(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
839 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
Ben Murdoch85b71792012-04-11 18:30:58 +0100840 __ ldc1(double_exponent,
841 FieldMemOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000842 } else if (exponent_type() == TAGGED) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100843 // Base is already in double_base.
844 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
Ben Murdoch85b71792012-04-11 18:30:58 +0100845
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100846 __ ldc1(double_exponent,
847 FieldMemOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +0000848 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100849
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000850 if (exponent_type() != INTEGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100851 Label int_exponent_convert;
852 // Detect integer exponents stored as double.
853 __ EmitFPUTruncate(kRoundToMinusInf,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100854 scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855 double_exponent,
856 at,
857 double_scratch,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100858 scratch2,
859 kCheckForInexactConversion);
860 // scratch2 == 0 means there was no conversion error.
861 __ Branch(&int_exponent_convert, eq, scratch2, Operand(zero_reg));
862
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000863 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100864 // Detect square root case. Crankshaft detects constant +/-0.5 at
865 // compile time and uses DoMathPowHalf instead. We then skip this check
866 // for non-constant cases of +/-0.5 as these hardly occur.
867 Label not_plus_half;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100868 // Test for 0.5.
869 __ Move(double_scratch, 0.5);
870 __ BranchF(USE_DELAY_SLOT,
871 &not_plus_half,
872 NULL,
873 ne,
874 double_exponent,
875 double_scratch);
876 // double_scratch can be overwritten in the delay slot.
877 // Calculates square root of base. Check for the special case of
878 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400879 __ Move(double_scratch, static_cast<double>(-V8_INFINITY));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100880 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, double_base, double_scratch);
881 __ neg_d(double_result, double_scratch);
882
883 // Add +0 to convert -0 to +0.
884 __ add_d(double_scratch, double_base, kDoubleRegZero);
885 __ sqrt_d(double_result, double_scratch);
886 __ jmp(&done);
887
888 __ bind(&not_plus_half);
889 __ Move(double_scratch, -0.5);
890 __ BranchF(USE_DELAY_SLOT,
891 &call_runtime,
892 NULL,
893 ne,
894 double_exponent,
895 double_scratch);
896 // double_scratch can be overwritten in the delay slot.
897 // Calculates square root of base. Check for the special case of
898 // Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400899 __ Move(double_scratch, static_cast<double>(-V8_INFINITY));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100900 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, double_base, double_scratch);
901 __ Move(double_result, kDoubleRegZero);
902
903 // Add +0 to convert -0 to +0.
904 __ add_d(double_scratch, double_base, kDoubleRegZero);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400905 __ Move(double_result, 1.);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100906 __ sqrt_d(double_scratch, double_scratch);
907 __ div_d(double_result, double_result, double_scratch);
908 __ jmp(&done);
909 }
910
911 __ push(ra);
912 {
913 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000914 __ PrepareCallCFunction(0, 2, scratch2);
915 __ MovToFloatParameters(double_base, double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100916 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000917 ExternalReference::power_double_double_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100918 0, 2);
919 }
920 __ pop(ra);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000921 __ MovFromFloatResult(double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100922 __ jmp(&done);
923
924 __ bind(&int_exponent_convert);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100925 }
926
927 // Calculate power with integer exponent.
928 __ bind(&int_exponent);
929
930 // Get two copies of exponent in the registers scratch and exponent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000931 if (exponent_type() == INTEGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100932 __ mov(scratch, exponent);
933 } else {
934 // Exponent has previously been stored into scratch as untagged integer.
935 __ mov(exponent, scratch);
936 }
937
938 __ mov_d(double_scratch, double_base); // Back up base.
939 __ Move(double_result, 1.0);
940
941 // Get absolute value of exponent.
942 Label positive_exponent;
943 __ Branch(&positive_exponent, ge, scratch, Operand(zero_reg));
944 __ Subu(scratch, zero_reg, scratch);
945 __ bind(&positive_exponent);
946
947 Label while_true, no_carry, loop_end;
948 __ bind(&while_true);
949
950 __ And(scratch2, scratch, 1);
951
952 __ Branch(&no_carry, eq, scratch2, Operand(zero_reg));
953 __ mul_d(double_result, double_result, double_scratch);
954 __ bind(&no_carry);
955
956 __ sra(scratch, scratch, 1);
957
958 __ Branch(&loop_end, eq, scratch, Operand(zero_reg));
959 __ mul_d(double_scratch, double_scratch, double_scratch);
960
961 __ Branch(&while_true);
962
963 __ bind(&loop_end);
964
965 __ Branch(&done, ge, exponent, Operand(zero_reg));
966 __ Move(double_scratch, 1.0);
967 __ div_d(double_result, double_scratch, double_result);
968 // Test whether result is zero. Bail out to check for subnormal result.
969 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
970 __ BranchF(&done, NULL, ne, double_result, kDoubleRegZero);
971
972 // double_exponent may not contain the exponent value if the input was a
973 // smi. We set it with exponent value before bailing out.
974 __ mtc1(exponent, single_scratch);
975 __ cvt_d_w(double_exponent, single_scratch);
976
977 // Returning or bailing out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000978 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100979 // The arguments are still on the stack.
980 __ bind(&call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000981 __ TailCallRuntime(Runtime::kMathPowRT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100982
983 // The stub is called from non-optimized code, which expects the result
984 // as heap number in exponent.
985 __ bind(&done);
986 __ AllocateHeapNumber(
987 heapnumber, scratch, scratch2, heapnumbermap, &call_runtime);
988 __ sdc1(double_result,
989 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000990 DCHECK(heapnumber.is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100991 __ DropAndRet(2);
992 } else {
993 __ push(ra);
994 {
995 AllowExternalCallThatCantCauseGC scope(masm);
996 __ PrepareCallCFunction(0, 2, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000997 __ MovToFloatParameters(double_base, double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100998 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000999 ExternalReference::power_double_double_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001000 0, 2);
1001 }
1002 __ pop(ra);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001003 __ MovFromFloatResult(double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001004
1005 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001006 __ Ret();
1007 }
Steve Block44f0eee2011-05-26 01:26:41 +01001008}
1009
1010
1011bool CEntryStub::NeedsImmovableCode() {
1012 return true;
1013}
1014
1015
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001016void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
1017 CEntryStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001018 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
1019 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
1020 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
1021 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001022 CreateWeakCellStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001023 BinaryOpICStub::GenerateAheadOfTime(isolate);
1024 StoreRegistersStateStub::GenerateAheadOfTime(isolate);
1025 RestoreRegistersStateStub::GenerateAheadOfTime(isolate);
1026 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001027 StoreFastElementStub::GenerateAheadOfTime(isolate);
1028 TypeofStub::GenerateAheadOfTime(isolate);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001029}
1030
1031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032void StoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
1033 StoreRegistersStateStub stub(isolate);
1034 stub.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001035}
1036
1037
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001038void RestoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
1039 RestoreRegistersStateStub stub(isolate);
1040 stub.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001041}
1042
1043
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001044void CodeStub::GenerateFPStubs(Isolate* isolate) {
1045 // Generate if not already in cache.
1046 SaveFPRegsMode mode = kSaveFPRegs;
1047 CEntryStub(isolate, 1, mode).GetCode();
1048 StoreBufferOverflowStub(isolate, mode).GetCode();
1049 isolate->set_fp_stubs_generated(true);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001050}
1051
1052
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001053void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1054 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1055 stub.GetCode();
1056}
Ben Murdoch257744e2011-11-30 15:57:28 +00001057
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001058
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001059void CEntryStub::Generate(MacroAssembler* masm) {
1060 // Called from JavaScript; parameters are on stack as if calling JS function
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001061 // a0: number of arguments including receiver
1062 // a1: pointer to builtin function
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001063 // fp: frame pointer (restored after C call)
1064 // sp: stack pointer (restored as callee's sp after C call)
1065 // cp: current context (C callee-saved)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001066 //
1067 // If argv_in_register():
1068 // a2: pointer to the first argument
Ben Murdoch257744e2011-11-30 15:57:28 +00001069
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001070 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1071
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001072 if (argv_in_register()) {
1073 // Move argv into the correct register.
1074 __ mov(s1, a2);
1075 } else {
1076 // Compute the argv pointer in a callee-saved register.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001077 __ Lsa(s1, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001078 __ Subu(s1, s1, kPointerSize);
1079 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001080
1081 // Enter the exit frame that transitions from JavaScript to C++.
1082 FrameScope scope(masm, StackFrame::MANUAL);
1083 __ EnterExitFrame(save_doubles());
1084
1085 // s0: number of arguments including receiver (C callee-saved)
1086 // s1: pointer to first argument (C callee-saved)
1087 // s2: pointer to builtin function (C callee-saved)
Ben Murdoch257744e2011-11-30 15:57:28 +00001088
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001089 // Prepare arguments for C routine.
1090 // a0 = argc
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001091 __ mov(s0, a0);
1092 __ mov(s2, a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001093
1094 // We are calling compiled C/C++ code. a0 and a1 hold our two arguments. We
1095 // also need to reserve the 4 argument slots on the stack.
1096
1097 __ AssertStackIsAligned();
1098
Ben Murdoch097c5b22016-05-18 11:27:45 +01001099 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
1100 int frame_alignment_mask = frame_alignment - 1;
1101 int result_stack_size;
1102 if (result_size() <= 2) {
1103 // a0 = argc, a1 = argv, a2 = isolate
1104 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
1105 __ mov(a1, s1);
1106 result_stack_size = 0;
1107 } else {
1108 DCHECK_EQ(3, result_size());
1109 // Allocate additional space for the result.
1110 result_stack_size =
1111 ((result_size() * kPointerSize) + frame_alignment_mask) &
1112 ~frame_alignment_mask;
1113 __ Subu(sp, sp, Operand(result_stack_size));
1114
1115 // a0 = hidden result argument, a1 = argc, a2 = argv, a3 = isolate.
1116 __ li(a3, Operand(ExternalReference::isolate_address(isolate())));
1117 __ mov(a2, s1);
1118 __ mov(a1, a0);
1119 __ mov(a0, sp);
1120 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001121
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001122 // To let the GC traverse the return address of the exit frames, we need to
1123 // know where the return address is. The CEntryStub is unmovable, so
1124 // we can store the address on the stack to be able to find it again and
1125 // we never have to restore it, because it will not change.
Ben Murdoch257744e2011-11-30 15:57:28 +00001126 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001127 int kNumInstructionsToJump = 4;
Ben Murdoch257744e2011-11-30 15:57:28 +00001128 Label find_ra;
Ben Murdoch257744e2011-11-30 15:57:28 +00001129 // Adjust the value in ra to point to the correct return location, 2nd
1130 // instruction past the real call into C code (the jalr(t9)), and push it.
1131 // This is the return address of the exit frame.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001132 if (kArchVariant >= kMips32r6) {
1133 __ addiupc(ra, kNumInstructionsToJump + 1);
1134 } else {
1135 // This branch-and-link sequence is needed to find the current PC on mips
1136 // before r6, saved to the ra register.
1137 __ bal(&find_ra); // bal exposes branch delay slot.
1138 __ Addu(ra, ra, kNumInstructionsToJump * Instruction::kInstrSize);
1139 }
1140 __ bind(&find_ra);
1141
1142 // This spot was reserved in EnterExitFrame.
1143 __ sw(ra, MemOperand(sp, result_stack_size));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001144 // Stack space reservation moved to the branch delay slot below.
Ben Murdoch257744e2011-11-30 15:57:28 +00001145 // Stack is still aligned.
1146
1147 // Call the C routine.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001148 __ mov(t9, s2); // Function pointer to t9 to conform to ABI for PIC.
1149 __ jalr(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001150 // Set up sp in the delay slot.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001151 __ addiu(sp, sp, -kCArgsSlotsSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001152 // Make sure the stored 'ra' points to this position.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001153 DCHECK_EQ(kNumInstructionsToJump,
Ben Murdoch257744e2011-11-30 15:57:28 +00001154 masm->InstructionsGeneratedSince(&find_ra));
1155 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001156 if (result_size() > 2) {
1157 DCHECK_EQ(3, result_size());
1158 // Read result values stored on stack.
1159 __ lw(a0, MemOperand(v0, 2 * kPointerSize));
1160 __ lw(v1, MemOperand(v0, 1 * kPointerSize));
1161 __ lw(v0, MemOperand(v0, 0 * kPointerSize));
1162 }
1163 // Result returned in v0, v1:v0 or a0:v1:v0 - do not destroy these registers!
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001164
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001165 // Check result for exception sentinel.
1166 Label exception_returned;
1167 __ LoadRoot(t0, Heap::kExceptionRootIndex);
1168 __ Branch(&exception_returned, eq, t0, Operand(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001169
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001170 // Check that there is no pending exception, otherwise we
1171 // should have returned the exception sentinel.
1172 if (FLAG_debug_code) {
1173 Label okay;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001174 ExternalReference pending_exception_address(
1175 Isolate::kPendingExceptionAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001176 __ li(a2, Operand(pending_exception_address));
1177 __ lw(a2, MemOperand(a2));
1178 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1179 // Cannot use check here as it attempts to generate call into runtime.
1180 __ Branch(&okay, eq, t0, Operand(a2));
1181 __ stop("Unexpected pending exception");
1182 __ bind(&okay);
1183 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001184
1185 // Exit C frame and return.
1186 // v0:v1: result
1187 // sp: stack pointer
1188 // fp: frame pointer
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001189 Register argc;
1190 if (argv_in_register()) {
1191 // We don't want to pop arguments so set argc to no_reg.
1192 argc = no_reg;
1193 } else {
1194 // s0: still holds argc (callee-saved).
1195 argc = s0;
1196 }
1197 __ LeaveExitFrame(save_doubles(), argc, true, EMIT_RETURN);
Ben Murdoch257744e2011-11-30 15:57:28 +00001198
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001199 // Handling of exception.
1200 __ bind(&exception_returned);
Ben Murdoch257744e2011-11-30 15:57:28 +00001201
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001202 ExternalReference pending_handler_context_address(
1203 Isolate::kPendingHandlerContextAddress, isolate());
1204 ExternalReference pending_handler_code_address(
1205 Isolate::kPendingHandlerCodeAddress, isolate());
1206 ExternalReference pending_handler_offset_address(
1207 Isolate::kPendingHandlerOffsetAddress, isolate());
1208 ExternalReference pending_handler_fp_address(
1209 Isolate::kPendingHandlerFPAddress, isolate());
1210 ExternalReference pending_handler_sp_address(
1211 Isolate::kPendingHandlerSPAddress, isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001212
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001213 // Ask the runtime for help to determine the handler. This will set v0 to
1214 // contain the current pending exception, don't clobber it.
1215 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
1216 isolate());
1217 {
1218 FrameScope scope(masm, StackFrame::MANUAL);
1219 __ PrepareCallCFunction(3, 0, a0);
1220 __ mov(a0, zero_reg);
1221 __ mov(a1, zero_reg);
1222 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
1223 __ CallCFunction(find_handler, 3);
1224 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001225
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001226 // Retrieve the handler context, SP and FP.
1227 __ li(cp, Operand(pending_handler_context_address));
1228 __ lw(cp, MemOperand(cp));
1229 __ li(sp, Operand(pending_handler_sp_address));
1230 __ lw(sp, MemOperand(sp));
1231 __ li(fp, Operand(pending_handler_fp_address));
1232 __ lw(fp, MemOperand(fp));
Ben Murdoch257744e2011-11-30 15:57:28 +00001233
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001234 // If the handler is a JS frame, restore the context to the frame. Note that
1235 // the context will be set to (cp == 0) for non-JS frames.
1236 Label zero;
1237 __ Branch(&zero, eq, cp, Operand(zero_reg));
1238 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1239 __ bind(&zero);
Ben Murdoch257744e2011-11-30 15:57:28 +00001240
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001241 // Compute the handler entry address and jump to it.
1242 __ li(a1, Operand(pending_handler_code_address));
1243 __ lw(a1, MemOperand(a1));
1244 __ li(a2, Operand(pending_handler_offset_address));
1245 __ lw(a2, MemOperand(a2));
1246 __ Addu(a1, a1, Operand(Code::kHeaderSize - kHeapObjectTag));
1247 __ Addu(t9, a1, a2);
1248 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01001249}
1250
1251
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001252void JSEntryStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001253 Label invoke, handler_entry, exit;
1254 Isolate* isolate = masm->isolate();
Ben Murdoch257744e2011-11-30 15:57:28 +00001255
1256 // Registers:
1257 // a0: entry address
1258 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001259 // a2: receiver
Ben Murdoch257744e2011-11-30 15:57:28 +00001260 // a3: argc
1261 //
1262 // Stack:
1263 // 4 args slots
1264 // args
1265
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001266 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1267
Ben Murdoch257744e2011-11-30 15:57:28 +00001268 // Save callee saved registers on the stack.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001269 __ MultiPush(kCalleeSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00001270
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001271 // Save callee-saved FPU registers.
1272 __ MultiPushFPU(kCalleeSavedFPU);
1273 // Set up the reserved register for 0.0.
1274 __ Move(kDoubleRegZero, 0.0);
Ben Murdoch589d6972011-11-30 16:04:58 +00001275
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001276
Ben Murdoch257744e2011-11-30 15:57:28 +00001277 // Load argv in s0 register.
Ben Murdoch589d6972011-11-30 16:04:58 +00001278 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001279 offset_to_argv += kNumCalleeSavedFPU * kDoubleSize;
Ben Murdoch589d6972011-11-30 16:04:58 +00001280
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001281 __ InitializeRootRegister();
Ben Murdoch589d6972011-11-30 16:04:58 +00001282 __ lw(s0, MemOperand(sp, offset_to_argv + kCArgsSlotsSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001283
1284 // We build an EntryFrame.
1285 __ li(t3, Operand(-1)); // Push a bad frame pointer to fail if it is used.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001286 int marker = type();
Ben Murdoch257744e2011-11-30 15:57:28 +00001287 __ li(t2, Operand(Smi::FromInt(marker)));
1288 __ li(t1, Operand(Smi::FromInt(marker)));
Ben Murdoch589d6972011-11-30 16:04:58 +00001289 __ li(t0, Operand(ExternalReference(Isolate::kCEntryFPAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001290 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001291 __ lw(t0, MemOperand(t0));
1292 __ Push(t3, t2, t1, t0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001293 // Set up frame pointer for the frame to be pushed.
Ben Murdoch257744e2011-11-30 15:57:28 +00001294 __ addiu(fp, sp, -EntryFrameConstants::kCallerFPOffset);
1295
1296 // Registers:
1297 // a0: entry_address
1298 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001299 // a2: receiver_pointer
Ben Murdoch257744e2011-11-30 15:57:28 +00001300 // a3: argc
1301 // s0: argv
1302 //
1303 // Stack:
1304 // caller fp |
1305 // function slot | entry frame
1306 // context slot |
1307 // bad fp (0xff...f) |
1308 // callee saved registers + ra
1309 // 4 args slots
1310 // args
1311
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001312 // If this is the outermost JS call, set js_entry_sp value.
1313 Label non_outermost_js;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001314 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001315 __ li(t1, Operand(ExternalReference(js_entry_sp)));
1316 __ lw(t2, MemOperand(t1));
1317 __ Branch(&non_outermost_js, ne, t2, Operand(zero_reg));
1318 __ sw(fp, MemOperand(t1));
1319 __ li(t0, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1320 Label cont;
1321 __ b(&cont);
1322 __ nop(); // Branch delay slot nop.
1323 __ bind(&non_outermost_js);
1324 __ li(t0, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
1325 __ bind(&cont);
1326 __ push(t0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001327
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001328 // Jump to a faked try block that does the invoke, with a faked catch
1329 // block that sets the pending exception.
1330 __ jmp(&invoke);
1331 __ bind(&handler_entry);
1332 handler_offset_ = handler_entry.pos();
1333 // Caught exception: Store result (exception) in the pending exception
1334 // field in the JSEnv and return a failure sentinel. Coming in here the
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001335 // fp will be invalid because the PushStackHandler below sets it to 0 to
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001336 // signal the existence of the JSEntry frame.
Ben Murdoch589d6972011-11-30 16:04:58 +00001337 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001338 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001339 __ sw(v0, MemOperand(t0)); // We come back from 'invoke'. result is in v0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001340 __ LoadRoot(v0, Heap::kExceptionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001341 __ b(&exit); // b exposes branch delay slot.
1342 __ nop(); // Branch delay slot nop.
1343
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001344 // Invoke: Link this frame into the handler chain.
Ben Murdoch257744e2011-11-30 15:57:28 +00001345 __ bind(&invoke);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001346 __ PushStackHandler();
Ben Murdoch257744e2011-11-30 15:57:28 +00001347 // If an exception not caught by another handler occurs, this handler
1348 // returns control to the code after the bal(&invoke) above, which
1349 // restores all kCalleeSaved registers (including cp and fp) to their
1350 // saved values before returning a failure to C.
1351
1352 // Clear any pending exceptions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001353 __ LoadRoot(t1, Heap::kTheHoleValueRootIndex);
Ben Murdoch589d6972011-11-30 16:04:58 +00001354 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001355 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001356 __ sw(t1, MemOperand(t0));
1357
1358 // Invoke the function by calling through JS entry trampoline builtin.
1359 // Notice that we cannot store a reference to the trampoline code directly in
1360 // this stub, because runtime stubs are not traversed when doing GC.
1361
1362 // Registers:
1363 // a0: entry_address
1364 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001365 // a2: receiver_pointer
Ben Murdoch257744e2011-11-30 15:57:28 +00001366 // a3: argc
1367 // s0: argv
1368 //
1369 // Stack:
1370 // handler frame
1371 // entry frame
1372 // callee saved registers + ra
1373 // 4 args slots
1374 // args
1375
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001376 if (type() == StackFrame::ENTRY_CONSTRUCT) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001377 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001378 isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00001379 __ li(t0, Operand(construct_entry));
1380 } else {
1381 ExternalReference entry(Builtins::kJSEntryTrampoline, masm->isolate());
1382 __ li(t0, Operand(entry));
1383 }
1384 __ lw(t9, MemOperand(t0)); // Deref address.
1385
1386 // Call JSEntryTrampoline.
1387 __ addiu(t9, t9, Code::kHeaderSize - kHeapObjectTag);
1388 __ Call(t9);
1389
1390 // Unlink this frame from the handler chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001391 __ PopStackHandler();
Ben Murdoch257744e2011-11-30 15:57:28 +00001392
1393 __ bind(&exit); // v0 holds result
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001394 // Check if the current stack frame is marked as the outermost JS frame.
1395 Label non_outermost_js_2;
1396 __ pop(t1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001397 __ Branch(&non_outermost_js_2,
1398 ne,
1399 t1,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001400 Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1401 __ li(t1, Operand(ExternalReference(js_entry_sp)));
1402 __ sw(zero_reg, MemOperand(t1));
1403 __ bind(&non_outermost_js_2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001404
1405 // Restore the top frame descriptors from the stack.
1406 __ pop(t1);
Ben Murdoch589d6972011-11-30 16:04:58 +00001407 __ li(t0, Operand(ExternalReference(Isolate::kCEntryFPAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001408 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001409 __ sw(t1, MemOperand(t0));
1410
1411 // Reset the stack to the callee saved registers.
1412 __ addiu(sp, sp, -EntryFrameConstants::kCallerFPOffset);
1413
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001414 // Restore callee-saved fpu registers.
1415 __ MultiPopFPU(kCalleeSavedFPU);
Ben Murdoch589d6972011-11-30 16:04:58 +00001416
Ben Murdoch257744e2011-11-30 15:57:28 +00001417 // Restore callee saved registers from the stack.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001418 __ MultiPop(kCalleeSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00001419 // Return.
1420 __ Jump(ra);
Steve Block44f0eee2011-05-26 01:26:41 +01001421}
1422
1423
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001424void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
1425 // Return address is in ra.
1426 Label miss;
1427
1428 Register receiver = LoadDescriptor::ReceiverRegister();
1429 Register index = LoadDescriptor::NameRegister();
1430 Register scratch = t1;
1431 Register result = v0;
1432 DCHECK(!scratch.is(receiver) && !scratch.is(index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001433 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001434
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001435 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
1436 &miss, // When not a string.
1437 &miss, // When not a number.
1438 &miss, // When index out of range.
1439 STRING_INDEX_IS_ARRAY_INDEX,
1440 RECEIVER_IS_STRING);
1441 char_at_generator.GenerateFast(masm);
1442 __ Ret();
1443
1444 StubRuntimeCallHelper call_helper;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001445 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001446
1447 __ bind(&miss);
1448 PropertyAccessCompiler::TailCallBuiltin(
1449 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
1450}
1451
1452
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001453void InstanceOfStub::Generate(MacroAssembler* masm) {
1454 Register const object = a1; // Object (lhs).
1455 Register const function = a0; // Function (rhs).
1456 Register const object_map = a2; // Map of {object}.
1457 Register const function_map = a3; // Map of {function}.
1458 Register const function_prototype = t0; // Prototype of {function}.
1459 Register const scratch = t1;
Ben Murdoch257744e2011-11-30 15:57:28 +00001460
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001461 DCHECK(object.is(InstanceOfDescriptor::LeftRegister()));
1462 DCHECK(function.is(InstanceOfDescriptor::RightRegister()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001463
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001464 // Check if {object} is a smi.
1465 Label object_is_smi;
1466 __ JumpIfSmi(object, &object_is_smi);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001467
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001468 // Lookup the {function} and the {object} map in the global instanceof cache.
1469 // Note: This is safe because we clear the global instanceof cache whenever
1470 // we change the prototype of any object.
1471 Label fast_case, slow_case;
1472 __ lw(object_map, FieldMemOperand(object, HeapObject::kMapOffset));
1473 __ LoadRoot(at, Heap::kInstanceofCacheFunctionRootIndex);
1474 __ Branch(&fast_case, ne, function, Operand(at));
1475 __ LoadRoot(at, Heap::kInstanceofCacheMapRootIndex);
1476 __ Branch(&fast_case, ne, object_map, Operand(at));
1477 __ Ret(USE_DELAY_SLOT);
1478 __ LoadRoot(v0, Heap::kInstanceofCacheAnswerRootIndex); // In delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00001479
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001480 // If {object} is a smi we can safely return false if {function} is a JS
1481 // function, otherwise we have to miss to the runtime and throw an exception.
1482 __ bind(&object_is_smi);
1483 __ JumpIfSmi(function, &slow_case);
1484 __ GetObjectType(function, function_map, scratch);
1485 __ Branch(&slow_case, ne, scratch, Operand(JS_FUNCTION_TYPE));
1486 __ Ret(USE_DELAY_SLOT);
1487 __ LoadRoot(v0, Heap::kFalseValueRootIndex); // In delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00001488
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001489 // Fast-case: The {function} must be a valid JSFunction.
1490 __ bind(&fast_case);
1491 __ JumpIfSmi(function, &slow_case);
1492 __ GetObjectType(function, function_map, scratch);
1493 __ Branch(&slow_case, ne, scratch, Operand(JS_FUNCTION_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001494
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001495 // Ensure that {function} has an instance prototype.
1496 __ lbu(scratch, FieldMemOperand(function_map, Map::kBitFieldOffset));
1497 __ And(at, scratch, Operand(1 << Map::kHasNonInstancePrototype));
1498 __ Branch(&slow_case, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00001499
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001500 // Get the "prototype" (or initial map) of the {function}.
1501 __ lw(function_prototype,
1502 FieldMemOperand(function, JSFunction::kPrototypeOrInitialMapOffset));
1503 __ AssertNotSmi(function_prototype);
Ben Murdoch257744e2011-11-30 15:57:28 +00001504
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001505 // Resolve the prototype if the {function} has an initial map. Afterwards the
1506 // {function_prototype} will be either the JSReceiver prototype object or the
1507 // hole value, which means that no instances of the {function} were created so
1508 // far and hence we should return false.
1509 Label function_prototype_valid;
1510 __ GetObjectType(function_prototype, scratch, scratch);
1511 __ Branch(&function_prototype_valid, ne, scratch, Operand(MAP_TYPE));
1512 __ lw(function_prototype,
1513 FieldMemOperand(function_prototype, Map::kPrototypeOffset));
1514 __ bind(&function_prototype_valid);
1515 __ AssertNotSmi(function_prototype);
Ben Murdoch257744e2011-11-30 15:57:28 +00001516
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001517 // Update the global instanceof cache with the current {object} map and
1518 // {function}. The cached answer will be set when it is known below.
1519 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
1520 __ StoreRoot(object_map, Heap::kInstanceofCacheMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001521
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001522 // Loop through the prototype chain looking for the {function} prototype.
1523 // Assume true, and change to false if not found.
1524 Register const object_instance_type = function_map;
1525 Register const map_bit_field = function_map;
1526 Register const null = scratch;
1527 Register const result = v0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001528
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001529 Label done, loop, fast_runtime_fallback;
1530 __ LoadRoot(result, Heap::kTrueValueRootIndex);
1531 __ LoadRoot(null, Heap::kNullValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001532 __ bind(&loop);
Ben Murdoch257744e2011-11-30 15:57:28 +00001533
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001534 // Check if the object needs to be access checked.
1535 __ lbu(map_bit_field, FieldMemOperand(object_map, Map::kBitFieldOffset));
1536 __ And(map_bit_field, map_bit_field, Operand(1 << Map::kIsAccessCheckNeeded));
1537 __ Branch(&fast_runtime_fallback, ne, map_bit_field, Operand(zero_reg));
1538 // Check if the current object is a Proxy.
1539 __ lbu(object_instance_type,
1540 FieldMemOperand(object_map, Map::kInstanceTypeOffset));
1541 __ Branch(&fast_runtime_fallback, eq, object_instance_type,
1542 Operand(JS_PROXY_TYPE));
1543
1544 __ lw(object, FieldMemOperand(object_map, Map::kPrototypeOffset));
1545 __ Branch(&done, eq, object, Operand(function_prototype));
1546 __ Branch(USE_DELAY_SLOT, &loop, ne, object, Operand(null));
1547 __ lw(object_map,
1548 FieldMemOperand(object, HeapObject::kMapOffset)); // In delay slot.
1549 __ LoadRoot(result, Heap::kFalseValueRootIndex);
1550 __ bind(&done);
1551 __ Ret(USE_DELAY_SLOT);
1552 __ StoreRoot(result,
1553 Heap::kInstanceofCacheAnswerRootIndex); // In delay slot.
1554
1555 // Found Proxy or access check needed: Call the runtime
1556 __ bind(&fast_runtime_fallback);
1557 __ Push(object, function_prototype);
1558 // Invalidate the instanceof cache.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001559 DCHECK(Smi::FromInt(0) == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001560 __ StoreRoot(zero_reg, Heap::kInstanceofCacheFunctionRootIndex);
1561 __ TailCallRuntime(Runtime::kHasInPrototypeChain);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001562
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001563 // Slow-case: Call the %InstanceOf runtime function.
1564 __ bind(&slow_case);
1565 __ Push(object, function);
1566 __ TailCallRuntime(Runtime::kInstanceOf);
Steve Block44f0eee2011-05-26 01:26:41 +01001567}
1568
1569
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001570void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
1571 Label miss;
1572 Register receiver = LoadDescriptor::ReceiverRegister();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001573 // Ensure that the vector and slot registers won't be clobbered before
1574 // calling the miss handler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001575 DCHECK(!AreAliased(t0, t1, LoadWithVectorDescriptor::VectorRegister(),
1576 LoadWithVectorDescriptor::SlotRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001577
1578 NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(masm, receiver, t0,
1579 t1, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001580 __ bind(&miss);
1581 PropertyAccessCompiler::TailCallBuiltin(
1582 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
1583}
Ben Murdoch257744e2011-11-30 15:57:28 +00001584
1585
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001586void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
1587 // Return address is in ra.
1588 Label slow;
1589
1590 Register receiver = LoadDescriptor::ReceiverRegister();
1591 Register key = LoadDescriptor::NameRegister();
1592
1593 // Check that the key is an array index, that is Uint32.
1594 __ And(t0, key, Operand(kSmiTagMask | kSmiSignMask));
1595 __ Branch(&slow, ne, t0, Operand(zero_reg));
1596
1597 // Everything is fine, call runtime.
1598 __ Push(receiver, key); // Receiver, key.
1599
1600 // Perform tail call to the entry.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001601 __ TailCallRuntime(Runtime::kLoadElementWithInterceptor);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001602
1603 __ bind(&slow);
1604 PropertyAccessCompiler::TailCallBuiltin(
1605 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001606}
1607
Steve Block44f0eee2011-05-26 01:26:41 +01001608void RegExpExecStub::Generate(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001609 // Just jump directly to runtime if native RegExp is not selected at compile
1610 // time or if regexp entry in generated code is turned off runtime switch or
1611 // at compilation.
1612#ifdef V8_INTERPRETED_REGEXP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001613 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdoch257744e2011-11-30 15:57:28 +00001614#else // V8_INTERPRETED_REGEXP
Ben Murdoch257744e2011-11-30 15:57:28 +00001615
1616 // Stack frame on entry.
1617 // sp[0]: last_match_info (expected JSArray)
1618 // sp[4]: previous index
1619 // sp[8]: subject string
1620 // sp[12]: JSRegExp object
1621
Ben Murdochdb1b4382012-04-26 19:03:50 +01001622 const int kLastMatchInfoOffset = 0 * kPointerSize;
1623 const int kPreviousIndexOffset = 1 * kPointerSize;
1624 const int kSubjectOffset = 2 * kPointerSize;
1625 const int kJSRegExpOffset = 3 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00001626
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001627 Label runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00001628 // Allocation of registers for this function. These are in callee save
1629 // registers and will be preserved by the call to the native RegExp code, as
1630 // this code is called using the normal C calling convention. When calling
1631 // directly from generated code the native RegExp code will not do a GC and
1632 // therefore the content of these registers are safe to use after the call.
1633 // MIPS - using s0..s2, since we are not using CEntry Stub.
1634 Register subject = s0;
1635 Register regexp_data = s1;
1636 Register last_match_info_elements = s2;
1637
1638 // Ensure that a RegExp stack is allocated.
1639 ExternalReference address_of_regexp_stack_memory_address =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001640 ExternalReference::address_of_regexp_stack_memory_address(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001641 ExternalReference address_of_regexp_stack_memory_size =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642 ExternalReference::address_of_regexp_stack_memory_size(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001643 __ li(a0, Operand(address_of_regexp_stack_memory_size));
1644 __ lw(a0, MemOperand(a0, 0));
1645 __ Branch(&runtime, eq, a0, Operand(zero_reg));
1646
1647 // Check that the first argument is a JSRegExp object.
1648 __ lw(a0, MemOperand(sp, kJSRegExpOffset));
1649 STATIC_ASSERT(kSmiTag == 0);
1650 __ JumpIfSmi(a0, &runtime);
1651 __ GetObjectType(a0, a1, a1);
1652 __ Branch(&runtime, ne, a1, Operand(JS_REGEXP_TYPE));
1653
1654 // Check that the RegExp has been compiled (data contains a fixed array).
1655 __ lw(regexp_data, FieldMemOperand(a0, JSRegExp::kDataOffset));
1656 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001657 __ SmiTst(regexp_data, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001658 __ Check(nz,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659 kUnexpectedTypeForRegExpDataFixedArrayExpected,
Ben Murdoch257744e2011-11-30 15:57:28 +00001660 t0,
1661 Operand(zero_reg));
1662 __ GetObjectType(regexp_data, a0, a0);
1663 __ Check(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664 kUnexpectedTypeForRegExpDataFixedArrayExpected,
Ben Murdoch257744e2011-11-30 15:57:28 +00001665 a0,
1666 Operand(FIXED_ARRAY_TYPE));
1667 }
1668
1669 // regexp_data: RegExp data (FixedArray)
1670 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1671 __ lw(a0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
1672 __ Branch(&runtime, ne, a0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
1673
1674 // regexp_data: RegExp data (FixedArray)
1675 // Check that the number of captures fit in the static offsets vector buffer.
1676 __ lw(a2,
1677 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001678 // Check (number_of_captures + 1) * 2 <= offsets vector size
1679 // Or number_of_captures * 2 <= offsets vector size - 2
1680 // Multiplying by 2 comes for free since a2 is smi-tagged.
Ben Murdoch257744e2011-11-30 15:57:28 +00001681 STATIC_ASSERT(kSmiTag == 0);
1682 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001683 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1684 __ Branch(
1685 &runtime, hi, a2, Operand(Isolate::kJSRegexpStaticOffsetsVectorSize - 2));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001686
1687 // Reset offset for possibly sliced string.
1688 __ mov(t0, zero_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001689 __ lw(subject, MemOperand(sp, kSubjectOffset));
1690 __ JumpIfSmi(subject, &runtime);
1691 __ mov(a3, subject); // Make a copy of the original subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001692 // subject: subject string
1693 // a3: subject string
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001694 // regexp_data: RegExp data (FixedArray)
1695 // Handle subject string according to its encoding and representation:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001696 // (1) Sequential string? If yes, go to (4).
1697 // (2) Sequential or cons? If not, go to (5).
1698 // (3) Cons string. If the string is flat, replace subject with first string
1699 // and go to (1). Otherwise bail out to runtime.
1700 // (4) Sequential string. Load regexp code according to encoding.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001701 // (E) Carry on.
1702 /// [...]
1703
1704 // Deferred code at the end of the stub:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001705 // (5) Long external string? If not, go to (7).
1706 // (6) External string. Make it, offset-wise, look like a sequential string.
1707 // Go to (4).
1708 // (7) Short external string or not a string? If yes, bail out to runtime.
1709 // (8) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001710
Ben Murdoch097c5b22016-05-18 11:27:45 +01001711 Label seq_string /* 4 */, external_string /* 6 */, check_underlying /* 1 */,
1712 not_seq_nor_cons /* 5 */, not_long_external /* 7 */;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001713
Ben Murdoch097c5b22016-05-18 11:27:45 +01001714 __ bind(&check_underlying);
1715 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
1716 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
1717
1718 // (1) Sequential string? If yes, go to (4).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001719 __ And(a1,
1720 a0,
1721 Operand(kIsNotStringMask |
1722 kStringRepresentationMask |
1723 kShortExternalStringMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00001724 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001725 __ Branch(&seq_string, eq, a1, Operand(zero_reg)); // Go to (5).
Ben Murdoch257744e2011-11-30 15:57:28 +00001726
Ben Murdoch097c5b22016-05-18 11:27:45 +01001727 // (2) Sequential or cons? If not, go to (5).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001728 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1729 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001730 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1731 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001732 // Go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001733 __ Branch(&not_seq_nor_cons, ge, a1, Operand(kExternalStringTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001734
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001735 // (3) Cons string. Check that it's flat.
1736 // Replace subject with first string and reload instance type.
Ben Murdoch257744e2011-11-30 15:57:28 +00001737 __ lw(a0, FieldMemOperand(subject, ConsString::kSecondOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001738 __ LoadRoot(a1, Heap::kempty_stringRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001739 __ Branch(&runtime, ne, a0, Operand(a1));
1740 __ lw(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001741 __ jmp(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001742
Ben Murdoch097c5b22016-05-18 11:27:45 +01001743 // (4) Sequential string. Load regexp code according to encoding.
Ben Murdoch257744e2011-11-30 15:57:28 +00001744 __ bind(&seq_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001745 // subject: sequential subject string (or look-alike, external string)
1746 // a3: original subject string
1747 // Load previous index and check range before a3 is overwritten. We have to
1748 // use a3 instead of subject here because subject might have been only made
1749 // to look like a sequential string when it actually is an external string.
1750 __ lw(a1, MemOperand(sp, kPreviousIndexOffset));
1751 __ JumpIfNotSmi(a1, &runtime);
1752 __ lw(a3, FieldMemOperand(a3, String::kLengthOffset));
1753 __ Branch(&runtime, ls, a3, Operand(a1));
1754 __ sra(a1, a1, kSmiTagSize); // Untag the Smi.
1755
Ben Murdoch257744e2011-11-30 15:57:28 +00001756 STATIC_ASSERT(kStringEncodingMask == 4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001757 STATIC_ASSERT(kOneByteStringTag == 4);
Ben Murdoch257744e2011-11-30 15:57:28 +00001758 STATIC_ASSERT(kTwoByteStringTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001759 __ And(a0, a0, Operand(kStringEncodingMask)); // Non-zero for one-byte.
1760 __ lw(t9, FieldMemOperand(regexp_data, JSRegExp::kDataOneByteCodeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001761 __ sra(a3, a0, 2); // a3 is 1 for ASCII, 0 for UC16 (used below).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001762 __ lw(t1, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001763 __ Movz(t9, t1, a0); // If UC16 (a0 is 0), replace t9 w/kDataUC16CodeOffset.
Ben Murdoch257744e2011-11-30 15:57:28 +00001764
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001765 // (E) Carry on. String handling is done.
1766 // t9: irregexp code
Ben Murdoch257744e2011-11-30 15:57:28 +00001767 // Check that the irregexp code has been generated for the actual string
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001768 // encoding. If it has, the field contains a code object otherwise it contains
1769 // a smi (code flushing support).
1770 __ JumpIfSmi(t9, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00001771
Ben Murdoch257744e2011-11-30 15:57:28 +00001772 // a1: previous index
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001773 // a3: encoding of subject string (1 if one_byte, 0 if two_byte);
Ben Murdoch257744e2011-11-30 15:57:28 +00001774 // t9: code
1775 // subject: Subject string
1776 // regexp_data: RegExp data (FixedArray)
1777 // All checks done. Now push arguments for native regexp code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001778 __ IncrementCounter(isolate()->counters()->regexp_entry_native(),
Ben Murdoch257744e2011-11-30 15:57:28 +00001779 1, a0, a2);
1780
1781 // Isolates: note we add an additional parameter here (isolate pointer).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001782 const int kRegExpExecuteArguments = 9;
Ben Murdochdb1b4382012-04-26 19:03:50 +01001783 const int kParameterRegisters = 4;
Ben Murdoch257744e2011-11-30 15:57:28 +00001784 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
1785
1786 // Stack pointer now points to cell where return address is to be written.
1787 // Arguments are before that on the stack or in registers, meaning we
1788 // treat the return address as argument 5. Thus every argument after that
1789 // needs to be shifted back by 1. Since DirectCEntryStub will handle
1790 // allocating space for the c argument slots, we don't need to calculate
1791 // that into the argument positions on the stack. This is how the stack will
1792 // look (sp meaning the value of sp at this moment):
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001793 // [sp + 5] - Argument 9
Ben Murdoch257744e2011-11-30 15:57:28 +00001794 // [sp + 4] - Argument 8
1795 // [sp + 3] - Argument 7
1796 // [sp + 2] - Argument 6
1797 // [sp + 1] - Argument 5
1798 // [sp + 0] - saved ra
1799
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001800 // Argument 9: Pass current isolate address.
Ben Murdoch257744e2011-11-30 15:57:28 +00001801 // CFunctionArgumentOperand handles MIPS stack argument slots.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001802 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
1803 __ sw(a0, MemOperand(sp, 5 * kPointerSize));
1804
1805 // Argument 8: Indicate that this is a direct call from JavaScript.
1806 __ li(a0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00001807 __ sw(a0, MemOperand(sp, 4 * kPointerSize));
1808
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001809 // Argument 7: Start (high end) of backtracking stack memory area.
Ben Murdoch257744e2011-11-30 15:57:28 +00001810 __ li(a0, Operand(address_of_regexp_stack_memory_address));
1811 __ lw(a0, MemOperand(a0, 0));
1812 __ li(a2, Operand(address_of_regexp_stack_memory_size));
1813 __ lw(a2, MemOperand(a2, 0));
1814 __ addu(a0, a0, a2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001815 __ sw(a0, MemOperand(sp, 3 * kPointerSize));
1816
1817 // Argument 6: Set the number of capture registers to zero to force global
1818 // regexps to behave as non-global. This does not affect non-global regexps.
1819 __ mov(a0, zero_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00001820 __ sw(a0, MemOperand(sp, 2 * kPointerSize));
1821
1822 // Argument 5: static offsets vector buffer.
1823 __ li(a0, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001824 ExternalReference::address_of_static_offsets_vector(isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00001825 __ sw(a0, MemOperand(sp, 1 * kPointerSize));
1826
1827 // For arguments 4 and 3 get string length, calculate start of string data
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001828 // calculate the shift of the index (0 for one-byte and 1 for two-byte).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001829 __ Addu(t2, subject, Operand(SeqString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00001830 __ Xor(a3, a3, Operand(1)); // 1 for 2-byte str, 0 for 1-byte.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001831 // Load the length from the original subject string from the previous stack
1832 // frame. Therefore we have to use fp, which points exactly to two pointer
1833 // sizes below the previous sp. (Because creating a new stack frame pushes
1834 // the previous fp onto the stack and moves up sp by 2 * kPointerSize.)
Ben Murdoch589d6972011-11-30 16:04:58 +00001835 __ lw(subject, MemOperand(fp, kSubjectOffset + 2 * kPointerSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001836 // If slice offset is not 0, load the length from the original sliced string.
1837 // Argument 4, a3: End of string data
1838 // Argument 3, a2: Start of string data
1839 // Prepare start and end index of the input.
1840 __ sllv(t1, t0, a3);
1841 __ addu(t0, t2, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001842 __ sllv(t1, a1, a3);
1843 __ addu(a2, t0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001844
Ben Murdoch589d6972011-11-30 16:04:58 +00001845 __ lw(t2, FieldMemOperand(subject, String::kLengthOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001846 __ sra(t2, t2, kSmiTagSize);
1847 __ sllv(t1, t2, a3);
1848 __ addu(a3, t0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001849 // Argument 2 (a1): Previous index.
1850 // Already there
1851
1852 // Argument 1 (a0): Subject string.
Ben Murdoch589d6972011-11-30 16:04:58 +00001853 __ mov(a0, subject);
Ben Murdoch257744e2011-11-30 15:57:28 +00001854
1855 // Locate the code entry and call it.
1856 __ Addu(t9, t9, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001857 DirectCEntryStub stub(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001858 stub.GenerateCall(masm, t9);
1859
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001860 __ LeaveExitFrame(false, no_reg, true);
Ben Murdoch257744e2011-11-30 15:57:28 +00001861
1862 // v0: result
1863 // subject: subject string (callee saved)
1864 // regexp_data: RegExp data (callee saved)
1865 // last_match_info_elements: Last match info elements (callee saved)
Ben Murdoch257744e2011-11-30 15:57:28 +00001866 // Check the result.
Ben Murdoch257744e2011-11-30 15:57:28 +00001867 Label success;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001868 __ Branch(&success, eq, v0, Operand(1));
1869 // We expect exactly one result since we force the called regexp to behave
1870 // as non-global.
Ben Murdoch257744e2011-11-30 15:57:28 +00001871 Label failure;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001872 __ Branch(&failure, eq, v0, Operand(NativeRegExpMacroAssembler::FAILURE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001873 // If not exception it can only be retry. Handle that in the runtime system.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001874 __ Branch(&runtime, ne, v0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
Ben Murdoch257744e2011-11-30 15:57:28 +00001875 // Result must now be exception. If there is no pending exception already a
1876 // stack overflow (on the backtrack stack) was detected in RegExp code but
1877 // haven't created the exception yet. Handle that in the runtime system.
1878 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001879 __ li(a1, Operand(isolate()->factory()->the_hole_value()));
Ben Murdoch589d6972011-11-30 16:04:58 +00001880 __ li(a2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001881 isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00001882 __ lw(v0, MemOperand(a2, 0));
Ben Murdoch589d6972011-11-30 16:04:58 +00001883 __ Branch(&runtime, eq, v0, Operand(a1));
Ben Murdoch257744e2011-11-30 15:57:28 +00001884
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001885 // For exception, throw the exception again.
1886 __ TailCallRuntime(Runtime::kRegExpExecReThrow);
Ben Murdoch257744e2011-11-30 15:57:28 +00001887
1888 __ bind(&failure);
1889 // For failure and exception return null.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001890 __ li(v0, Operand(isolate()->factory()->null_value()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001891 __ DropAndRet(4);
Ben Murdoch257744e2011-11-30 15:57:28 +00001892
1893 // Process the result from the native regexp code.
1894 __ bind(&success);
1895 __ lw(a1,
1896 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
1897 // Calculate number of capture registers (number_of_captures + 1) * 2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001898 // Multiplying by 2 comes for free since r1 is smi-tagged.
Ben Murdoch257744e2011-11-30 15:57:28 +00001899 STATIC_ASSERT(kSmiTag == 0);
1900 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1901 __ Addu(a1, a1, Operand(2)); // a1 was a smi.
1902
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001903 __ lw(a0, MemOperand(sp, kLastMatchInfoOffset));
1904 __ JumpIfSmi(a0, &runtime);
1905 __ GetObjectType(a0, a2, a2);
1906 __ Branch(&runtime, ne, a2, Operand(JS_ARRAY_TYPE));
1907 // Check that the JSArray is in fast case.
1908 __ lw(last_match_info_elements,
1909 FieldMemOperand(a0, JSArray::kElementsOffset));
1910 __ lw(a0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
1911 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
1912 __ Branch(&runtime, ne, a0, Operand(at));
1913 // Check that the last match info has space for the capture registers and the
1914 // additional information.
1915 __ lw(a0,
1916 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
1917 __ Addu(a2, a1, Operand(RegExpImpl::kLastMatchOverhead));
1918 __ sra(at, a0, kSmiTagSize);
1919 __ Branch(&runtime, gt, a2, Operand(at));
1920
Ben Murdoch257744e2011-11-30 15:57:28 +00001921 // a1: number of capture registers
1922 // subject: subject string
1923 // Store the capture count.
1924 __ sll(a2, a1, kSmiTagSize + kSmiShiftSize); // To smi.
1925 __ sw(a2, FieldMemOperand(last_match_info_elements,
1926 RegExpImpl::kLastCaptureCountOffset));
1927 // Store last subject and last input.
Ben Murdoch257744e2011-11-30 15:57:28 +00001928 __ sw(subject,
1929 FieldMemOperand(last_match_info_elements,
1930 RegExpImpl::kLastSubjectOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001931 __ mov(a2, subject);
1932 __ RecordWriteField(last_match_info_elements,
1933 RegExpImpl::kLastSubjectOffset,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001934 subject,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001935 t3,
1936 kRAHasNotBeenSaved,
1937 kDontSaveFPRegs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001938 __ mov(subject, a2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001939 __ sw(subject,
1940 FieldMemOperand(last_match_info_elements,
1941 RegExpImpl::kLastInputOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001942 __ RecordWriteField(last_match_info_elements,
1943 RegExpImpl::kLastInputOffset,
1944 subject,
1945 t3,
1946 kRAHasNotBeenSaved,
1947 kDontSaveFPRegs);
Ben Murdoch257744e2011-11-30 15:57:28 +00001948
1949 // Get the static offsets vector filled by the native regexp code.
1950 ExternalReference address_of_static_offsets_vector =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001951 ExternalReference::address_of_static_offsets_vector(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001952 __ li(a2, Operand(address_of_static_offsets_vector));
1953
1954 // a1: number of capture registers
1955 // a2: offsets vector
1956 Label next_capture, done;
1957 // Capture register counter starts from number of capture registers and
1958 // counts down until wrapping after zero.
1959 __ Addu(a0,
1960 last_match_info_elements,
1961 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
1962 __ bind(&next_capture);
1963 __ Subu(a1, a1, Operand(1));
1964 __ Branch(&done, lt, a1, Operand(zero_reg));
1965 // Read the value from the static offsets vector buffer.
1966 __ lw(a3, MemOperand(a2, 0));
1967 __ addiu(a2, a2, kPointerSize);
1968 // Store the smi value in the last match info.
1969 __ sll(a3, a3, kSmiTagSize); // Convert to Smi.
1970 __ sw(a3, MemOperand(a0, 0));
1971 __ Branch(&next_capture, USE_DELAY_SLOT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001972 __ addiu(a0, a0, kPointerSize); // In branch delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00001973
1974 __ bind(&done);
1975
1976 // Return last match info.
1977 __ lw(v0, MemOperand(sp, kLastMatchInfoOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001978 __ DropAndRet(4);
1979
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001980 // Do the runtime call to execute the regexp.
1981 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001982 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001983
1984 // Deferred code for string handling.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001985 // (5) Long external string? If not, go to (7).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001986 __ bind(&not_seq_nor_cons);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001987 // Go to (7).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001988 __ Branch(&not_long_external, gt, a1, Operand(kExternalStringTag));
1989
Ben Murdoch097c5b22016-05-18 11:27:45 +01001990 // (6) External string. Make it, offset-wise, look like a sequential string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001991 __ bind(&external_string);
1992 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
1993 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
1994 if (FLAG_debug_code) {
1995 // Assert that we do not have a cons or slice (indirect strings) here.
1996 // Sequential strings have already been ruled out.
1997 __ And(at, a0, Operand(kIsIndirectStringMask));
1998 __ Assert(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001999 kExternalStringExpectedButNotFound,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002000 at,
2001 Operand(zero_reg));
2002 }
2003 __ lw(subject,
2004 FieldMemOperand(subject, ExternalString::kResourceDataOffset));
2005 // Move the pointer so that offset-wise, it looks like a sequential string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002006 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002007 __ Subu(subject,
2008 subject,
2009 SeqTwoByteString::kHeaderSize - kHeapObjectTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002010 __ jmp(&seq_string); // Go to (5).
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002011
Ben Murdoch097c5b22016-05-18 11:27:45 +01002012 // (7) Short external string or not a string? If yes, bail out to runtime.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002013 __ bind(&not_long_external);
2014 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
2015 __ And(at, a1, Operand(kIsNotStringMask | kShortExternalStringMask));
2016 __ Branch(&runtime, ne, at, Operand(zero_reg));
2017
Ben Murdoch097c5b22016-05-18 11:27:45 +01002018 // (8) Sliced string. Replace subject with parent. Go to (4).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002019 // Load offset into t0 and replace subject string with parent.
2020 __ lw(t0, FieldMemOperand(subject, SlicedString::kOffsetOffset));
2021 __ sra(t0, t0, kSmiTagSize);
2022 __ lw(subject, FieldMemOperand(subject, SlicedString::kParentOffset));
2023 __ jmp(&check_underlying); // Go to (4).
Ben Murdoch257744e2011-11-30 15:57:28 +00002024#endif // V8_INTERPRETED_REGEXP
Steve Block44f0eee2011-05-26 01:26:41 +01002025}
2026
2027
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002028static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
2029 // a0 : number of arguments to the construct function
2030 // a2 : feedback vector
2031 // a3 : slot in feedback vector (Smi)
2032 // a1 : the function to call
2033 FrameScope scope(masm, StackFrame::INTERNAL);
2034 const RegList kSavedRegs = 1 << 4 | // a0
2035 1 << 5 | // a1
2036 1 << 6 | // a2
2037 1 << 7; // a3
2038
2039 // Number-of-arguments register must be smi-tagged to call out.
2040 __ SmiTag(a0);
2041 __ MultiPush(kSavedRegs);
2042
2043 __ CallStub(stub);
2044
2045 __ MultiPop(kSavedRegs);
2046 __ SmiUntag(a0);
2047}
2048
2049
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002050static void GenerateRecordCallTarget(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002051 // Cache the called function in a feedback vector slot. Cache states
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002052 // are uninitialized, monomorphic (indicated by a JSFunction), and
2053 // megamorphic.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002054 // a0 : number of arguments to the construct function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002055 // a1 : the function to call
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002056 // a2 : feedback vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002057 // a3 : slot in feedback vector (Smi)
2058 Label initialize, done, miss, megamorphic, not_array_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002059
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002060 DCHECK_EQ(*TypeFeedbackVector::MegamorphicSentinel(masm->isolate()),
2061 masm->isolate()->heap()->megamorphic_symbol());
2062 DCHECK_EQ(*TypeFeedbackVector::UninitializedSentinel(masm->isolate()),
2063 masm->isolate()->heap()->uninitialized_symbol());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002064
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002065 // Load the cache state into t2.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002066 __ Lsa(t2, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002067 __ lw(t2, FieldMemOperand(t2, FixedArray::kHeaderSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002068
2069 // A monomorphic cache hit or an already megamorphic state: invoke the
2070 // function without changing the state.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002071 // We don't know if t2 is a WeakCell or a Symbol, but it's harmless to read at
2072 // this position in a symbol (see static asserts in type-feedback-vector.h).
2073 Label check_allocation_site;
2074 Register feedback_map = t1;
2075 Register weak_value = t4;
2076 __ lw(weak_value, FieldMemOperand(t2, WeakCell::kValueOffset));
2077 __ Branch(&done, eq, a1, Operand(weak_value));
2078 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
2079 __ Branch(&done, eq, t2, Operand(at));
2080 __ lw(feedback_map, FieldMemOperand(t2, HeapObject::kMapOffset));
2081 __ LoadRoot(at, Heap::kWeakCellMapRootIndex);
2082 __ Branch(&check_allocation_site, ne, feedback_map, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002083
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002084 // If the weak cell is cleared, we have a new chance to become monomorphic.
2085 __ JumpIfSmi(weak_value, &initialize);
2086 __ jmp(&megamorphic);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002087
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002088 __ bind(&check_allocation_site);
2089 // If we came here, we need to see if we are the array function.
2090 // If we didn't have a matching function, and we didn't find the megamorph
2091 // sentinel, then we have in the slot either some other function or an
2092 // AllocationSite.
2093 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2094 __ Branch(&miss, ne, feedback_map, Operand(at));
2095
2096 // Make sure the function is the Array() function
2097 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, t2);
2098 __ Branch(&megamorphic, ne, a1, Operand(t2));
2099 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002100
2101 __ bind(&miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002102
2103 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
2104 // megamorphic.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002105 __ LoadRoot(at, Heap::kuninitialized_symbolRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002106 __ Branch(&initialize, eq, t2, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002107 // MegamorphicSentinel is an immortal immovable object (undefined) so no
2108 // write-barrier is needed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002109 __ bind(&megamorphic);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002110 __ Lsa(t2, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002111 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002112 __ sw(at, FieldMemOperand(t2, FixedArray::kHeaderSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002113 __ jmp(&done);
2114
2115 // An uninitialized cache is patched with the function.
2116 __ bind(&initialize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002117 // Make sure the function is the Array() function.
2118 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, t2);
2119 __ Branch(&not_array_function, ne, a1, Operand(t2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002120
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002121 // The target function is the Array constructor,
2122 // Create an AllocationSite if we don't already have it, store it in the
2123 // slot.
2124 CreateAllocationSiteStub create_stub(masm->isolate());
2125 CallStubInRecordCallTarget(masm, &create_stub);
2126 __ Branch(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002127
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002128 __ bind(&not_array_function);
2129 CreateWeakCellStub weak_cell_stub(masm->isolate());
2130 CallStubInRecordCallTarget(masm, &weak_cell_stub);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002131 __ bind(&done);
2132}
2133
2134
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002135void CallConstructStub::Generate(MacroAssembler* masm) {
2136 // a0 : number of arguments
2137 // a1 : the function to call
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002138 // a2 : feedback vector
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002139 // a3 : slot in feedback vector (Smi, for RecordCallTarget)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002140
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002141 Label non_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002142 // Check that the function is not a smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002143 __ JumpIfSmi(a1, &non_function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002144 // Check that the function is a JSFunction.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002145 __ GetObjectType(a1, t1, t1);
2146 __ Branch(&non_function, ne, t1, Operand(JS_FUNCTION_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002147
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002148 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002149
Ben Murdoch097c5b22016-05-18 11:27:45 +01002150 __ Lsa(t1, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002151 Label feedback_register_initialized;
2152 // Put the AllocationSite from the feedback vector into a2, or undefined.
2153 __ lw(a2, FieldMemOperand(t1, FixedArray::kHeaderSize));
2154 __ lw(t1, FieldMemOperand(a2, AllocationSite::kMapOffset));
2155 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2156 __ Branch(&feedback_register_initialized, eq, t1, Operand(at));
2157 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2158 __ bind(&feedback_register_initialized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002159
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002160 __ AssertUndefinedOrAllocationSite(a2, t1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002161
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002162 // Pass function as new target.
2163 __ mov(a3, a1);
2164
2165 // Tail call to the function-specific construct stub (still in the caller
2166 // context at this point).
2167 __ lw(t0, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2168 __ lw(t0, FieldMemOperand(t0, SharedFunctionInfo::kConstructStubOffset));
2169 __ Addu(at, t0, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002170 __ Jump(at);
2171
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002172 __ bind(&non_function);
2173 __ mov(a3, a1);
2174 __ Jump(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002175}
2176
2177
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002178void CallICStub::HandleArrayCase(MacroAssembler* masm, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002179 // a1 - function
2180 // a3 - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002181 // a2 - vector
2182 // t0 - loaded from vector[slot]
2183 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, at);
2184 __ Branch(miss, ne, a1, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002185
2186 __ li(a0, Operand(arg_count()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002187
2188 // Increment the call count for monomorphic function calls.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002189 __ Lsa(at, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002190 __ lw(a3, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
2191 __ Addu(a3, a3, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2192 __ sw(a3, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002193
2194 __ mov(a2, t0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002195 __ mov(a3, a1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002196 ArrayConstructorStub stub(masm->isolate(), arg_count());
2197 __ TailCallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002198}
2199
2200
2201void CallICStub::Generate(MacroAssembler* masm) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002202 // a1 - function
2203 // a3 - slot id (Smi)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002204 // a2 - vector
2205 Label extra_checks_or_miss, call, call_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002206 int argc = arg_count();
2207 ParameterCount actual(argc);
2208
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002209 // The checks. First, does r1 match the recorded monomorphic target?
Ben Murdoch097c5b22016-05-18 11:27:45 +01002210 __ Lsa(t0, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002211 __ lw(t0, FieldMemOperand(t0, FixedArray::kHeaderSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002212
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002213 // We don't know that we have a weak cell. We might have a private symbol
2214 // or an AllocationSite, but the memory is safe to examine.
2215 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
2216 // FixedArray.
2217 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
2218 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
2219 // computed, meaning that it can't appear to be a pointer. If the low bit is
2220 // 0, then hash is computed, but the 0 bit prevents the field from appearing
2221 // to be a pointer.
2222 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
2223 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
2224 WeakCell::kValueOffset &&
2225 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002226
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002227 __ lw(t1, FieldMemOperand(t0, WeakCell::kValueOffset));
2228 __ Branch(&extra_checks_or_miss, ne, a1, Operand(t1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002229
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002230 // The compare above could have been a SMI/SMI comparison. Guard against this
2231 // convincing us that we have a monomorphic JSFunction.
2232 __ JumpIfSmi(a1, &extra_checks_or_miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002233
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002234 // Increment the call count for monomorphic function calls.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002235 __ Lsa(at, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002236 __ lw(a3, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
2237 __ Addu(a3, a3, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2238 __ sw(a3, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002239
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002240 __ bind(&call_function);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002241 __ Jump(masm->isolate()->builtins()->CallFunction(convert_mode(),
2242 tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002243 RelocInfo::CODE_TARGET, al, zero_reg, Operand(zero_reg),
2244 USE_DELAY_SLOT);
2245 __ li(a0, Operand(argc)); // In delay slot.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002246
2247 __ bind(&extra_checks_or_miss);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002248 Label uninitialized, miss, not_allocation_site;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002249
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002250 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002251 __ Branch(&call, eq, t0, Operand(at));
2252
2253 // Verify that t0 contains an AllocationSite
2254 __ lw(t1, FieldMemOperand(t0, HeapObject::kMapOffset));
2255 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2256 __ Branch(&not_allocation_site, ne, t1, Operand(at));
2257
2258 HandleArrayCase(masm, &miss);
2259
2260 __ bind(&not_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002261
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002262 // The following cases attempt to handle MISS cases without going to the
2263 // runtime.
2264 if (FLAG_trace_ic) {
2265 __ Branch(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002266 }
2267
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002268 __ LoadRoot(at, Heap::kuninitialized_symbolRootIndex);
2269 __ Branch(&uninitialized, eq, t0, Operand(at));
2270
2271 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2272 // to handle it here. More complex cases are dealt with in the runtime.
2273 __ AssertNotSmi(t0);
2274 __ GetObjectType(t0, t1, t1);
2275 __ Branch(&miss, ne, t1, Operand(JS_FUNCTION_TYPE));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002276 __ Lsa(t0, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002277 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
2278 __ sw(at, FieldMemOperand(t0, FixedArray::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002279
2280 __ bind(&call);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002281 __ Jump(masm->isolate()->builtins()->Call(convert_mode(), tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002282 RelocInfo::CODE_TARGET, al, zero_reg, Operand(zero_reg),
2283 USE_DELAY_SLOT);
2284 __ li(a0, Operand(argc)); // In delay slot.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002285
2286 __ bind(&uninitialized);
2287
2288 // We are going monomorphic, provided we actually have a JSFunction.
2289 __ JumpIfSmi(a1, &miss);
2290
2291 // Goto miss case if we do not have a function.
2292 __ GetObjectType(a1, t0, t0);
2293 __ Branch(&miss, ne, t0, Operand(JS_FUNCTION_TYPE));
2294
2295 // Make sure the function is not the Array() function, which requires special
2296 // behavior on MISS.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002297 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, t0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002298 __ Branch(&miss, eq, a1, Operand(t0));
2299
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002300 // Make sure the function belongs to the same native context.
2301 __ lw(t0, FieldMemOperand(a1, JSFunction::kContextOffset));
2302 __ lw(t0, ContextMemOperand(t0, Context::NATIVE_CONTEXT_INDEX));
2303 __ lw(t1, NativeContextMemOperand());
2304 __ Branch(&miss, ne, t0, Operand(t1));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002305
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002306 // Initialize the call counter.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002307 __ Lsa(at, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002308 __ li(t0, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2309 __ sw(t0, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002310
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002311 // Store the function. Use a stub since we need a frame for allocation.
2312 // a2 - vector
2313 // a3 - slot
2314 // a1 - function
2315 {
2316 FrameScope scope(masm, StackFrame::INTERNAL);
2317 CreateWeakCellStub create_stub(masm->isolate());
2318 __ Push(a1);
2319 __ CallStub(&create_stub);
2320 __ Pop(a1);
2321 }
2322
2323 __ Branch(&call_function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002324
2325 // We are here because tracing is on or we encountered a MISS case we can't
2326 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002327 __ bind(&miss);
2328 GenerateMiss(masm);
2329
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002330 __ Branch(&call);
Steve Block44f0eee2011-05-26 01:26:41 +01002331}
2332
2333
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002334void CallICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002335 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002336
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002337 // Push the receiver and the function and feedback info.
2338 __ Push(a1, a2, a3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002339
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002340 // Call the entry.
2341 __ CallRuntime(Runtime::kCallIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002342
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002343 // Move result to a1 and exit the internal frame.
2344 __ mov(a1, v0);
Steve Block44f0eee2011-05-26 01:26:41 +01002345}
2346
2347
Ben Murdoch257744e2011-11-30 15:57:28 +00002348// StringCharCodeAtGenerator.
Steve Block44f0eee2011-05-26 01:26:41 +01002349void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002350 DCHECK(!t0.is(index_));
2351 DCHECK(!t0.is(result_));
2352 DCHECK(!t0.is(object_));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002353 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2354 // If the receiver is a smi trigger the non-string case.
2355 __ JumpIfSmi(object_, receiver_not_string_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002356
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002357 // Fetch the instance type of the receiver into result register.
2358 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
2359 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
2360 // If the receiver is not a string trigger the non-string case.
2361 __ And(t0, result_, Operand(kIsNotStringMask));
2362 __ Branch(receiver_not_string_, ne, t0, Operand(zero_reg));
2363 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002364
2365 // If the index is non-smi trigger the non-smi case.
2366 __ JumpIfNotSmi(index_, &index_not_smi_);
2367
Ben Murdoch257744e2011-11-30 15:57:28 +00002368 __ bind(&got_smi_index_);
2369
2370 // Check for index out of range.
2371 __ lw(t0, FieldMemOperand(object_, String::kLengthOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002372 __ Branch(index_out_of_range_, ls, t0, Operand(index_));
Ben Murdoch257744e2011-11-30 15:57:28 +00002373
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002374 __ sra(index_, index_, kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00002375
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002376 StringCharLoadGenerator::Generate(masm,
2377 object_,
2378 index_,
2379 result_,
2380 &call_runtime_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002381
Ben Murdoch257744e2011-11-30 15:57:28 +00002382 __ sll(result_, result_, kSmiTagSize);
2383 __ bind(&exit_);
Steve Block44f0eee2011-05-26 01:26:41 +01002384}
2385
2386
2387void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002388 MacroAssembler* masm, EmbedMode embed_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002389 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002390 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
Ben Murdoch257744e2011-11-30 15:57:28 +00002391
2392 // Index is not a smi.
2393 __ bind(&index_not_smi_);
2394 // If index is a heap number, try converting it to an integer.
2395 __ CheckMap(index_,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002396 result_,
Ben Murdoch257744e2011-11-30 15:57:28 +00002397 Heap::kHeapNumberMapRootIndex,
2398 index_not_number_,
2399 DONT_DO_SMI_CHECK);
2400 call_helper.BeforeCall(masm);
2401 // Consumed by runtime conversion function:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002402 if (embed_mode == PART_OF_IC_HANDLER) {
2403 __ Push(LoadWithVectorDescriptor::VectorRegister(),
2404 LoadWithVectorDescriptor::SlotRegister(), object_, index_);
2405 } else {
2406 __ Push(object_, index_);
2407 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002408 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002409 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero);
Ben Murdoch257744e2011-11-30 15:57:28 +00002410 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002411 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
Ben Murdoch257744e2011-11-30 15:57:28 +00002412 // NumberToSmi discards numbers that are not exact integers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002413 __ CallRuntime(Runtime::kNumberToSmi);
Ben Murdoch257744e2011-11-30 15:57:28 +00002414 }
2415
2416 // Save the conversion result before the pop instructions below
2417 // have a chance to overwrite it.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002418 __ Move(index_, v0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002419 if (embed_mode == PART_OF_IC_HANDLER) {
2420 __ Pop(LoadWithVectorDescriptor::VectorRegister(),
2421 LoadWithVectorDescriptor::SlotRegister(), object_);
2422 } else {
2423 __ pop(object_);
2424 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002425 // Reload the instance type.
2426 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
2427 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
2428 call_helper.AfterCall(masm);
2429 // If index is still not a smi, it must be out of range.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002430 __ JumpIfNotSmi(index_, index_out_of_range_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002431 // Otherwise, return to the fast path.
2432 __ Branch(&got_smi_index_);
2433
2434 // Call runtime. We get here when the receiver is a string and the
2435 // index is a number, but the code of getting the actual character
2436 // is too complex (e.g., when the string needs to be flattened).
2437 __ bind(&call_runtime_);
2438 call_helper.BeforeCall(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002439 __ sll(index_, index_, kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00002440 __ Push(object_, index_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002441 __ CallRuntime(Runtime::kStringCharCodeAtRT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002442
2443 __ Move(result_, v0);
2444
2445 call_helper.AfterCall(masm);
2446 __ jmp(&exit_);
2447
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002448 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
Steve Block44f0eee2011-05-26 01:26:41 +01002449}
2450
2451
2452// -------------------------------------------------------------------------
2453// StringCharFromCodeGenerator
2454
2455void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002456 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2457
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002458 DCHECK(!t0.is(result_));
2459 DCHECK(!t0.is(code_));
Ben Murdoch257744e2011-11-30 15:57:28 +00002460
2461 STATIC_ASSERT(kSmiTag == 0);
2462 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002463 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
2464 __ And(t0, code_, Operand(kSmiTagMask |
2465 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002466 __ Branch(&slow_case_, ne, t0, Operand(zero_reg));
2467
2468 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002469 // At this point code register contains smi tagged one-byte char code.
Ben Murdoch257744e2011-11-30 15:57:28 +00002470 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002471 __ Lsa(result_, result_, code_, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00002472 __ lw(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
2473 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
2474 __ Branch(&slow_case_, eq, result_, Operand(t0));
2475 __ bind(&exit_);
Steve Block44f0eee2011-05-26 01:26:41 +01002476}
2477
2478
2479void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002480 MacroAssembler* masm,
2481 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002482 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
Ben Murdoch257744e2011-11-30 15:57:28 +00002483
2484 __ bind(&slow_case_);
2485 call_helper.BeforeCall(masm);
2486 __ push(code_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002487 __ CallRuntime(Runtime::kStringCharFromCode);
Ben Murdoch257744e2011-11-30 15:57:28 +00002488 __ Move(result_, v0);
2489
2490 call_helper.AfterCall(masm);
2491 __ Branch(&exit_);
2492
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002493 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
Steve Block44f0eee2011-05-26 01:26:41 +01002494}
2495
2496
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002497enum CopyCharactersFlags { COPY_ONE_BYTE = 1, DEST_ALWAYS_ALIGNED = 2 };
Steve Block44f0eee2011-05-26 01:26:41 +01002498
2499
Steve Block44f0eee2011-05-26 01:26:41 +01002500void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2501 Register dest,
2502 Register src,
2503 Register count,
2504 Register scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002505 String::Encoding encoding) {
2506 if (FLAG_debug_code) {
2507 // Check that destination is word aligned.
2508 __ And(scratch, dest, Operand(kPointerAlignmentMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00002509 __ Check(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002510 kDestinationOfCopyNotAligned,
2511 scratch,
Ben Murdoch257744e2011-11-30 15:57:28 +00002512 Operand(zero_reg));
2513 }
2514
Ben Murdoch257744e2011-11-30 15:57:28 +00002515 // Assumes word reads and writes are little endian.
2516 // Nothing to do for zero characters.
2517 Label done;
2518
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002519 if (encoding == String::TWO_BYTE_ENCODING) {
2520 __ Addu(count, count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00002521 }
2522
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002523 Register limit = count; // Read until dest equals this.
2524 __ Addu(limit, dest, Operand(count));
Ben Murdoch257744e2011-11-30 15:57:28 +00002525
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002526 Label loop_entry, loop;
Ben Murdoch257744e2011-11-30 15:57:28 +00002527 // Copy bytes from src to dest until dest hits limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002528 __ Branch(&loop_entry);
2529 __ bind(&loop);
2530 __ lbu(scratch, MemOperand(src));
2531 __ Addu(src, src, Operand(1));
2532 __ sb(scratch, MemOperand(dest));
2533 __ Addu(dest, dest, Operand(1));
2534 __ bind(&loop_entry);
2535 __ Branch(&loop, lt, dest, Operand(limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00002536
2537 __ bind(&done);
Steve Block44f0eee2011-05-26 01:26:41 +01002538}
2539
2540
Steve Block44f0eee2011-05-26 01:26:41 +01002541void SubStringStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002542 Label runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00002543 // Stack frame on entry.
2544 // ra: return address
2545 // sp[0]: to
2546 // sp[4]: from
2547 // sp[8]: string
2548
2549 // This stub is called from the native-call %_SubString(...), so
2550 // nothing can be assumed about the arguments. It is tested that:
2551 // "string" is a sequential string,
2552 // both "from" and "to" are smis, and
2553 // 0 <= from <= to <= string.length.
2554 // If any of these assumptions fail, we call the runtime system.
2555
Ben Murdochdb1b4382012-04-26 19:03:50 +01002556 const int kToOffset = 0 * kPointerSize;
2557 const int kFromOffset = 1 * kPointerSize;
2558 const int kStringOffset = 2 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00002559
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002560 __ lw(a2, MemOperand(sp, kToOffset));
2561 __ lw(a3, MemOperand(sp, kFromOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002562 STATIC_ASSERT(kFromOffset == kToOffset + 4);
2563 STATIC_ASSERT(kSmiTag == 0);
2564 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2565
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002566 // Utilize delay slots. SmiUntag doesn't emit a jump, everything else is
2567 // safe in this case.
2568 __ UntagAndJumpIfNotSmi(a2, a2, &runtime);
2569 __ UntagAndJumpIfNotSmi(a3, a3, &runtime);
2570 // Both a2 and a3 are untagged integers.
Ben Murdoch257744e2011-11-30 15:57:28 +00002571
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002572 __ Branch(&runtime, lt, a3, Operand(zero_reg)); // From < 0.
Ben Murdoch257744e2011-11-30 15:57:28 +00002573
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002574 __ Branch(&runtime, gt, a3, Operand(a2)); // Fail if from > to.
2575 __ Subu(a2, a2, a3);
Ben Murdoch257744e2011-11-30 15:57:28 +00002576
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002577 // Make sure first argument is a string.
Ben Murdoch589d6972011-11-30 16:04:58 +00002578 __ lw(v0, MemOperand(sp, kStringOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002579 __ JumpIfSmi(v0, &runtime);
Ben Murdoch589d6972011-11-30 16:04:58 +00002580 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002581 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002582 __ And(t0, a1, Operand(kIsNotStringMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00002583
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002584 __ Branch(&runtime, ne, t0, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00002585
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002586 Label single_char;
2587 __ Branch(&single_char, eq, a2, Operand(1));
2588
Ben Murdoch589d6972011-11-30 16:04:58 +00002589 // Short-cut for the case of trivial substring.
2590 Label return_v0;
2591 // v0: original string
2592 // a2: result string length
2593 __ lw(t0, FieldMemOperand(v0, String::kLengthOffset));
2594 __ sra(t0, t0, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002595 // Return original string.
Ben Murdoch589d6972011-11-30 16:04:58 +00002596 __ Branch(&return_v0, eq, a2, Operand(t0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002597 // Longer than original string's length or negative: unsafe arguments.
2598 __ Branch(&runtime, hi, a2, Operand(t0));
2599 // Shorter than original string's length: an actual substring.
Ben Murdoch257744e2011-11-30 15:57:28 +00002600
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002601 // Deal with different string types: update the index if necessary
2602 // and put the underlying string into t1.
2603 // v0: original string
2604 // a1: instance type
2605 // a2: length
2606 // a3: from index (untagged)
2607 Label underlying_unpacked, sliced_string, seq_or_external_string;
2608 // If the string is not indirect, it can only be sequential or external.
2609 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2610 STATIC_ASSERT(kIsIndirectStringMask != 0);
2611 __ And(t0, a1, Operand(kIsIndirectStringMask));
2612 __ Branch(USE_DELAY_SLOT, &seq_or_external_string, eq, t0, Operand(zero_reg));
2613 // t0 is used as a scratch register and can be overwritten in either case.
2614 __ And(t0, a1, Operand(kSlicedNotConsMask));
2615 __ Branch(&sliced_string, ne, t0, Operand(zero_reg));
2616 // Cons string. Check whether it is flat, then fetch first part.
2617 __ lw(t1, FieldMemOperand(v0, ConsString::kSecondOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002618 __ LoadRoot(t0, Heap::kempty_stringRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002619 __ Branch(&runtime, ne, t1, Operand(t0));
2620 __ lw(t1, FieldMemOperand(v0, ConsString::kFirstOffset));
2621 // Update instance type.
2622 __ lw(a1, FieldMemOperand(t1, HeapObject::kMapOffset));
2623 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
2624 __ jmp(&underlying_unpacked);
Ben Murdoch257744e2011-11-30 15:57:28 +00002625
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002626 __ bind(&sliced_string);
2627 // Sliced string. Fetch parent and correct start index by offset.
2628 __ lw(t1, FieldMemOperand(v0, SlicedString::kParentOffset));
2629 __ lw(t0, FieldMemOperand(v0, SlicedString::kOffsetOffset));
2630 __ sra(t0, t0, 1); // Add offset to index.
2631 __ Addu(a3, a3, t0);
2632 // Update instance type.
2633 __ lw(a1, FieldMemOperand(t1, HeapObject::kMapOffset));
2634 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
2635 __ jmp(&underlying_unpacked);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002636
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002637 __ bind(&seq_or_external_string);
2638 // Sequential or external string. Just move string to the expected register.
2639 __ mov(t1, v0);
2640
2641 __ bind(&underlying_unpacked);
2642
2643 if (FLAG_string_slices) {
2644 Label copy_routine;
2645 // t1: underlying subject string
2646 // a1: instance type of underlying subject string
2647 // a2: length
2648 // a3: adjusted start index (untagged)
2649 // Short slice. Copy instead of slicing.
2650 __ Branch(&copy_routine, lt, a2, Operand(SlicedString::kMinLength));
2651 // Allocate new sliced string. At this point we do not reload the instance
2652 // type including the string encoding because we simply rely on the info
2653 // provided by the original string. It does not matter if the original
2654 // string's encoding is wrong because we always have to recheck encoding of
2655 // the newly created string's parent anyways due to externalized strings.
2656 Label two_byte_slice, set_slice_header;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002657 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002658 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2659 __ And(t0, a1, Operand(kStringEncodingMask));
2660 __ Branch(&two_byte_slice, eq, t0, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002661 __ AllocateOneByteSlicedString(v0, a2, t2, t3, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002662 __ jmp(&set_slice_header);
2663 __ bind(&two_byte_slice);
2664 __ AllocateTwoByteSlicedString(v0, a2, t2, t3, &runtime);
2665 __ bind(&set_slice_header);
2666 __ sll(a3, a3, 1);
2667 __ sw(t1, FieldMemOperand(v0, SlicedString::kParentOffset));
2668 __ sw(a3, FieldMemOperand(v0, SlicedString::kOffsetOffset));
2669 __ jmp(&return_v0);
2670
2671 __ bind(&copy_routine);
2672 }
2673
2674 // t1: underlying subject string
2675 // a1: instance type of underlying subject string
2676 // a2: length
2677 // a3: adjusted start index (untagged)
2678 Label two_byte_sequential, sequential_string, allocate_result;
2679 STATIC_ASSERT(kExternalStringTag != 0);
2680 STATIC_ASSERT(kSeqStringTag == 0);
2681 __ And(t0, a1, Operand(kExternalStringTag));
2682 __ Branch(&sequential_string, eq, t0, Operand(zero_reg));
2683
2684 // Handle external string.
2685 // Rule out short external strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002686 STATIC_ASSERT(kShortExternalStringTag != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002687 __ And(t0, a1, Operand(kShortExternalStringTag));
2688 __ Branch(&runtime, ne, t0, Operand(zero_reg));
2689 __ lw(t1, FieldMemOperand(t1, ExternalString::kResourceDataOffset));
2690 // t1 already points to the first character of underlying string.
2691 __ jmp(&allocate_result);
2692
2693 __ bind(&sequential_string);
2694 // Locate first character of underlying subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002695 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2696 __ Addu(t1, t1, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002697
2698 __ bind(&allocate_result);
2699 // Sequential acii string. Allocate the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002700 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002701 __ And(t0, a1, Operand(kStringEncodingMask));
2702 __ Branch(&two_byte_sequential, eq, t0, Operand(zero_reg));
2703
2704 // Allocate and copy the resulting ASCII string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002705 __ AllocateOneByteString(v0, a2, t0, t2, t3, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002706
2707 // Locate first character of substring to copy.
2708 __ Addu(t1, t1, a3);
2709
Ben Murdoch257744e2011-11-30 15:57:28 +00002710 // Locate first character of result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002711 __ Addu(a1, v0, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00002712
Ben Murdoch589d6972011-11-30 16:04:58 +00002713 // v0: result string
2714 // a1: first character of result string
2715 // a2: result string length
2716 // t1: first character of substring to copy
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002717 STATIC_ASSERT((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
2718 StringHelper::GenerateCopyCharacters(
2719 masm, a1, t1, a2, a3, String::ONE_BYTE_ENCODING);
Ben Murdoch589d6972011-11-30 16:04:58 +00002720 __ jmp(&return_v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002721
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002722 // Allocate and copy the resulting two-byte string.
2723 __ bind(&two_byte_sequential);
2724 __ AllocateTwoByteString(v0, a2, t0, t2, t3, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00002725
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002726 // Locate first character of substring to copy.
Ben Murdoch589d6972011-11-30 16:04:58 +00002727 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002728 __ Lsa(t1, t1, a3, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002729 // Locate first character of result.
2730 __ Addu(a1, v0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch589d6972011-11-30 16:04:58 +00002731
Ben Murdoch257744e2011-11-30 15:57:28 +00002732 // v0: result string.
2733 // a1: first character of result.
2734 // a2: result length.
Ben Murdoch589d6972011-11-30 16:04:58 +00002735 // t1: first character of substring to copy.
Ben Murdoch257744e2011-11-30 15:57:28 +00002736 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002737 StringHelper::GenerateCopyCharacters(
2738 masm, a1, t1, a2, a3, String::TWO_BYTE_ENCODING);
Ben Murdoch589d6972011-11-30 16:04:58 +00002739
2740 __ bind(&return_v0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002741 Counters* counters = isolate()->counters();
Ben Murdoch257744e2011-11-30 15:57:28 +00002742 __ IncrementCounter(counters->sub_string_native(), 1, a3, t0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002743 __ DropAndRet(3);
Ben Murdoch257744e2011-11-30 15:57:28 +00002744
2745 // Just jump to runtime to create the sub string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002746 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002747 __ TailCallRuntime(Runtime::kSubString);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002748
2749 __ bind(&single_char);
2750 // v0: original string
2751 // a1: instance type
2752 // a2: length
2753 // a3: from index (untagged)
2754 __ SmiTag(a3, a3);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002755 StringCharAtGenerator generator(v0, a3, a2, v0, &runtime, &runtime, &runtime,
2756 STRING_INDEX_IS_NUMBER, RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002757 generator.GenerateFast(masm);
2758 __ DropAndRet(3);
2759 generator.SkipSlow(masm, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00002760}
2761
2762
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002763void ToNumberStub::Generate(MacroAssembler* masm) {
2764 // The ToNumber stub takes one argument in a0.
2765 Label not_smi;
2766 __ JumpIfNotSmi(a0, &not_smi);
2767 __ Ret(USE_DELAY_SLOT);
2768 __ mov(v0, a0);
2769 __ bind(&not_smi);
2770
2771 Label not_heap_number;
2772 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
2773 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
2774 // a0: object
2775 // a1: instance type.
2776 __ Branch(&not_heap_number, ne, a1, Operand(HEAP_NUMBER_TYPE));
2777 __ Ret(USE_DELAY_SLOT);
2778 __ mov(v0, a0);
2779 __ bind(&not_heap_number);
2780
2781 Label not_string, slow_string;
2782 __ Branch(&not_string, hs, a1, Operand(FIRST_NONSTRING_TYPE));
2783 // Check if string has a cached array index.
2784 __ lw(a2, FieldMemOperand(a0, String::kHashFieldOffset));
2785 __ And(at, a2, Operand(String::kContainsCachedArrayIndexMask));
2786 __ Branch(&slow_string, ne, at, Operand(zero_reg));
2787 __ IndexFromHash(a2, a0);
2788 __ Ret(USE_DELAY_SLOT);
2789 __ mov(v0, a0);
2790 __ bind(&slow_string);
2791 __ push(a0); // Push argument.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002792 __ TailCallRuntime(Runtime::kStringToNumber);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002793 __ bind(&not_string);
2794
2795 Label not_oddball;
2796 __ Branch(&not_oddball, ne, a1, Operand(ODDBALL_TYPE));
2797 __ Ret(USE_DELAY_SLOT);
2798 __ lw(v0, FieldMemOperand(a0, Oddball::kToNumberOffset));
2799 __ bind(&not_oddball);
2800
2801 __ push(a0); // Push argument.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002802 __ TailCallRuntime(Runtime::kToNumber);
2803}
2804
2805
2806void ToLengthStub::Generate(MacroAssembler* masm) {
2807 // The ToLength stub takes on argument in a0.
2808 Label not_smi, positive_smi;
2809 __ JumpIfNotSmi(a0, &not_smi);
2810 STATIC_ASSERT(kSmiTag == 0);
2811 __ Branch(&positive_smi, ge, a0, Operand(zero_reg));
2812 __ mov(a0, zero_reg);
2813 __ bind(&positive_smi);
2814 __ Ret(USE_DELAY_SLOT);
2815 __ mov(v0, a0);
2816 __ bind(&not_smi);
2817
2818 __ push(a0); // Push argument.
2819 __ TailCallRuntime(Runtime::kToLength);
2820}
2821
2822
2823void ToStringStub::Generate(MacroAssembler* masm) {
2824 // The ToString stub takes on argument in a0.
2825 Label is_number;
2826 __ JumpIfSmi(a0, &is_number);
2827
2828 Label not_string;
2829 __ GetObjectType(a0, a1, a1);
2830 // a0: receiver
2831 // a1: receiver instance type
2832 __ Branch(&not_string, ge, a1, Operand(FIRST_NONSTRING_TYPE));
2833 __ Ret(USE_DELAY_SLOT);
2834 __ mov(v0, a0);
2835 __ bind(&not_string);
2836
2837 Label not_heap_number;
2838 __ Branch(&not_heap_number, ne, a1, Operand(HEAP_NUMBER_TYPE));
2839 __ bind(&is_number);
2840 NumberToStringStub stub(isolate());
2841 __ TailCallStub(&stub);
2842 __ bind(&not_heap_number);
2843
2844 Label not_oddball;
2845 __ Branch(&not_oddball, ne, a1, Operand(ODDBALL_TYPE));
2846 __ Ret(USE_DELAY_SLOT);
2847 __ lw(v0, FieldMemOperand(a0, Oddball::kToStringOffset));
2848 __ bind(&not_oddball);
2849
2850 __ push(a0); // Push argument.
2851 __ TailCallRuntime(Runtime::kToString);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002852}
2853
2854
Ben Murdoch097c5b22016-05-18 11:27:45 +01002855void ToNameStub::Generate(MacroAssembler* masm) {
2856 // The ToName stub takes on argument in a0.
2857 Label is_number;
2858 __ JumpIfSmi(a0, &is_number);
2859
2860 Label not_name;
2861 STATIC_ASSERT(FIRST_NAME_TYPE == FIRST_TYPE);
2862 __ GetObjectType(a0, a1, a1);
2863 // a0: receiver
2864 // a1: receiver instance type
2865 __ Branch(&not_name, gt, a1, Operand(LAST_NAME_TYPE));
2866 __ Ret(USE_DELAY_SLOT);
2867 __ mov(v0, a0);
2868 __ bind(&not_name);
2869
2870 Label not_heap_number;
2871 __ Branch(&not_heap_number, ne, a1, Operand(HEAP_NUMBER_TYPE));
2872 __ bind(&is_number);
2873 NumberToStringStub stub(isolate());
2874 __ TailCallStub(&stub);
2875 __ bind(&not_heap_number);
2876
2877 Label not_oddball;
2878 __ Branch(&not_oddball, ne, a1, Operand(ODDBALL_TYPE));
2879 __ Ret(USE_DELAY_SLOT);
2880 __ lw(v0, FieldMemOperand(a0, Oddball::kToStringOffset));
2881 __ bind(&not_oddball);
2882
2883 __ push(a0); // Push argument.
2884 __ TailCallRuntime(Runtime::kToName);
2885}
2886
2887
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002888void StringHelper::GenerateFlatOneByteStringEquals(
2889 MacroAssembler* masm, Register left, Register right, Register scratch1,
2890 Register scratch2, Register scratch3) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002891 Register length = scratch1;
2892
2893 // Compare lengths.
2894 Label strings_not_equal, check_zero_length;
2895 __ lw(length, FieldMemOperand(left, String::kLengthOffset));
2896 __ lw(scratch2, FieldMemOperand(right, String::kLengthOffset));
2897 __ Branch(&check_zero_length, eq, length, Operand(scratch2));
2898 __ bind(&strings_not_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002899 DCHECK(is_int16(NOT_EQUAL));
2900 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002901 __ li(v0, Operand(Smi::FromInt(NOT_EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002902
2903 // Check if the length is zero.
2904 Label compare_chars;
2905 __ bind(&check_zero_length);
2906 STATIC_ASSERT(kSmiTag == 0);
2907 __ Branch(&compare_chars, ne, length, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002908 DCHECK(is_int16(EQUAL));
2909 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002910 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002911
2912 // Compare characters.
2913 __ bind(&compare_chars);
2914
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002915 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2, scratch3,
2916 v0, &strings_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00002917
2918 // Characters are equal.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002919 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002920 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01002921}
2922
2923
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002924void StringHelper::GenerateCompareFlatOneByteStrings(
2925 MacroAssembler* masm, Register left, Register right, Register scratch1,
2926 Register scratch2, Register scratch3, Register scratch4) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002927 Label result_not_equal, compare_lengths;
2928 // Find minimum length and length difference.
2929 __ lw(scratch1, FieldMemOperand(left, String::kLengthOffset));
2930 __ lw(scratch2, FieldMemOperand(right, String::kLengthOffset));
2931 __ Subu(scratch3, scratch1, Operand(scratch2));
2932 Register length_delta = scratch3;
2933 __ slt(scratch4, scratch2, scratch1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002934 __ Movn(scratch1, scratch2, scratch4);
Ben Murdoch257744e2011-11-30 15:57:28 +00002935 Register min_length = scratch1;
2936 STATIC_ASSERT(kSmiTag == 0);
2937 __ Branch(&compare_lengths, eq, min_length, Operand(zero_reg));
2938
2939 // Compare loop.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002940 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
2941 scratch4, v0, &result_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00002942
2943 // Compare lengths - strings up to min-length are equal.
2944 __ bind(&compare_lengths);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002945 DCHECK(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002946 // Use length_delta as result if it's zero.
2947 __ mov(scratch2, length_delta);
2948 __ mov(scratch4, zero_reg);
2949 __ mov(v0, zero_reg);
2950
2951 __ bind(&result_not_equal);
2952 // Conditionally update the result based either on length_delta or
2953 // the last comparion performed in the loop above.
2954 Label ret;
2955 __ Branch(&ret, eq, scratch2, Operand(scratch4));
2956 __ li(v0, Operand(Smi::FromInt(GREATER)));
2957 __ Branch(&ret, gt, scratch2, Operand(scratch4));
2958 __ li(v0, Operand(Smi::FromInt(LESS)));
2959 __ bind(&ret);
2960 __ Ret();
2961}
2962
2963
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002964void StringHelper::GenerateOneByteCharsCompareLoop(
2965 MacroAssembler* masm, Register left, Register right, Register length,
2966 Register scratch1, Register scratch2, Register scratch3,
Ben Murdoch257744e2011-11-30 15:57:28 +00002967 Label* chars_not_equal) {
2968 // Change index to run from -length to -1 by adding length to string
2969 // start. This means that loop ends when index reaches zero, which
2970 // doesn't need an additional compare.
2971 __ SmiUntag(length);
2972 __ Addu(scratch1, length,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002973 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00002974 __ Addu(left, left, Operand(scratch1));
2975 __ Addu(right, right, Operand(scratch1));
2976 __ Subu(length, zero_reg, length);
2977 Register index = length; // index = -length;
2978
2979
2980 // Compare loop.
2981 Label loop;
2982 __ bind(&loop);
2983 __ Addu(scratch3, left, index);
2984 __ lbu(scratch1, MemOperand(scratch3));
2985 __ Addu(scratch3, right, index);
2986 __ lbu(scratch2, MemOperand(scratch3));
2987 __ Branch(chars_not_equal, ne, scratch1, Operand(scratch2));
2988 __ Addu(index, index, 1);
2989 __ Branch(&loop, ne, index, Operand(zero_reg));
Steve Block44f0eee2011-05-26 01:26:41 +01002990}
2991
2992
2993void StringCompareStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002994 // ----------- S t a t e -------------
2995 // -- a1 : left
2996 // -- a0 : right
2997 // -- ra : return address
2998 // -----------------------------------
2999 __ AssertString(a1);
3000 __ AssertString(a0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003001
3002 Label not_same;
3003 __ Branch(&not_same, ne, a0, Operand(a1));
Ben Murdoch257744e2011-11-30 15:57:28 +00003004 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003005 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1, a1,
3006 a2);
3007 __ Ret();
Ben Murdoch257744e2011-11-30 15:57:28 +00003008
3009 __ bind(&not_same);
3010
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003011 // Check that both objects are sequential one-byte strings.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003012 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003013 __ JumpIfNotBothSequentialOneByteStrings(a1, a0, a2, a3, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003014
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003015 // Compare flat ASCII strings natively.
3016 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1, a2,
3017 a3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003018 StringHelper::GenerateCompareFlatOneByteStrings(masm, a1, a0, a2, a3, t0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003019
3020 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003021 __ Push(a1, a0);
3022 __ TailCallRuntime(Runtime::kStringCompare);
Steve Block44f0eee2011-05-26 01:26:41 +01003023}
3024
3025
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003026void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3027 // ----------- S t a t e -------------
3028 // -- a1 : left
3029 // -- a0 : right
3030 // -- ra : return address
3031 // -----------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00003032
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003033 // Load a2 with the allocation site. We stick an undefined dummy value here
3034 // and replace it with the real allocation site later when we instantiate this
3035 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3036 __ li(a2, handle(isolate()->heap()->undefined_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00003037
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003038 // Make sure that we actually patched the allocation site.
3039 if (FLAG_debug_code) {
3040 __ And(at, a2, Operand(kSmiTagMask));
3041 __ Assert(ne, kExpectedAllocationSite, at, Operand(zero_reg));
3042 __ lw(t0, FieldMemOperand(a2, HeapObject::kMapOffset));
3043 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
3044 __ Assert(eq, kExpectedAllocationSite, t0, Operand(at));
Ben Murdoch257744e2011-11-30 15:57:28 +00003045 }
3046
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003047 // Tail call into the stub that handles binary operations with allocation
3048 // sites.
3049 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3050 __ TailCallStub(&stub);
Ben Murdoch257744e2011-11-30 15:57:28 +00003051}
3052
3053
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003054void CompareICStub::GenerateBooleans(MacroAssembler* masm) {
3055 DCHECK_EQ(CompareICState::BOOLEAN, state());
3056 Label miss;
3057
3058 __ CheckMap(a1, a2, Heap::kBooleanMapRootIndex, &miss, DO_SMI_CHECK);
3059 __ CheckMap(a0, a3, Heap::kBooleanMapRootIndex, &miss, DO_SMI_CHECK);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003060 if (!Token::IsEqualityOp(op())) {
3061 __ lw(a1, FieldMemOperand(a1, Oddball::kToNumberOffset));
3062 __ AssertSmi(a1);
3063 __ lw(a0, FieldMemOperand(a0, Oddball::kToNumberOffset));
3064 __ AssertSmi(a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003065 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01003066 __ Ret(USE_DELAY_SLOT);
3067 __ Subu(v0, a1, a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003068
3069 __ bind(&miss);
3070 GenerateMiss(masm);
3071}
3072
3073
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003074void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3075 DCHECK(state() == CompareICState::SMI);
Ben Murdoch257744e2011-11-30 15:57:28 +00003076 Label miss;
3077 __ Or(a2, a1, a0);
3078 __ JumpIfNotSmi(a2, &miss);
3079
3080 if (GetCondition() == eq) {
3081 // For equality we do not care about the sign of the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003082 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003083 __ Subu(v0, a0, a1);
3084 } else {
3085 // Untag before subtracting to avoid handling overflow.
3086 __ SmiUntag(a1);
3087 __ SmiUntag(a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003088 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003089 __ Subu(v0, a1, a0);
3090 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003091
3092 __ bind(&miss);
3093 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01003094}
3095
3096
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003097void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3098 DCHECK(state() == CompareICState::NUMBER);
Ben Murdoch257744e2011-11-30 15:57:28 +00003099
3100 Label generic_stub;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003101 Label unordered, maybe_undefined1, maybe_undefined2;
Ben Murdoch257744e2011-11-30 15:57:28 +00003102 Label miss;
Ben Murdoch257744e2011-11-30 15:57:28 +00003103
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003104 if (left() == CompareICState::SMI) {
3105 __ JumpIfNotSmi(a1, &miss);
3106 }
3107 if (right() == CompareICState::SMI) {
3108 __ JumpIfNotSmi(a0, &miss);
Ben Murdoch85b71792012-04-11 18:30:58 +01003109 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01003110
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003111 // Inlining the double comparison and falling back to the general compare
3112 // stub if NaN is involved.
3113 // Load left and right operand.
3114 Label done, left, left_smi, right_smi;
3115 __ JumpIfSmi(a0, &right_smi);
3116 __ CheckMap(a0, a2, Heap::kHeapNumberMapRootIndex, &maybe_undefined1,
3117 DONT_DO_SMI_CHECK);
3118 __ Subu(a2, a0, Operand(kHeapObjectTag));
3119 __ ldc1(f2, MemOperand(a2, HeapNumber::kValueOffset));
3120 __ Branch(&left);
3121 __ bind(&right_smi);
3122 __ SmiUntag(a2, a0); // Can't clobber a0 yet.
3123 FPURegister single_scratch = f6;
3124 __ mtc1(a2, single_scratch);
3125 __ cvt_d_w(f2, single_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003126
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003127 __ bind(&left);
3128 __ JumpIfSmi(a1, &left_smi);
3129 __ CheckMap(a1, a2, Heap::kHeapNumberMapRootIndex, &maybe_undefined2,
3130 DONT_DO_SMI_CHECK);
3131 __ Subu(a2, a1, Operand(kHeapObjectTag));
3132 __ ldc1(f0, MemOperand(a2, HeapNumber::kValueOffset));
3133 __ Branch(&done);
3134 __ bind(&left_smi);
3135 __ SmiUntag(a2, a1); // Can't clobber a1 yet.
3136 single_scratch = f8;
3137 __ mtc1(a2, single_scratch);
3138 __ cvt_d_w(f0, single_scratch);
3139
3140 __ bind(&done);
3141
3142 // Return a result of -1, 0, or 1, or use CompareStub for NaNs.
3143 Label fpu_eq, fpu_lt;
3144 // Test if equal, and also handle the unordered/NaN case.
3145 __ BranchF(&fpu_eq, &unordered, eq, f0, f2);
3146
3147 // Test if less (unordered case is already handled).
3148 __ BranchF(&fpu_lt, NULL, lt, f0, f2);
3149
3150 // Otherwise it's greater, so just fall thru, and return.
3151 DCHECK(is_int16(GREATER) && is_int16(EQUAL) && is_int16(LESS));
3152 __ Ret(USE_DELAY_SLOT);
3153 __ li(v0, Operand(GREATER));
3154
3155 __ bind(&fpu_eq);
3156 __ Ret(USE_DELAY_SLOT);
3157 __ li(v0, Operand(EQUAL));
3158
3159 __ bind(&fpu_lt);
3160 __ Ret(USE_DELAY_SLOT);
3161 __ li(v0, Operand(LESS));
3162
3163 __ bind(&unordered);
Ben Murdoch257744e2011-11-30 15:57:28 +00003164 __ bind(&generic_stub);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003165 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003166 CompareICState::GENERIC, CompareICState::GENERIC);
Ben Murdoch257744e2011-11-30 15:57:28 +00003167 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
3168
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003169 __ bind(&maybe_undefined1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003170 if (Token::IsOrderedRelationalCompareOp(op())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003171 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3172 __ Branch(&miss, ne, a0, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003173 __ JumpIfSmi(a1, &unordered);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003174 __ GetObjectType(a1, a2, a2);
3175 __ Branch(&maybe_undefined2, ne, a2, Operand(HEAP_NUMBER_TYPE));
3176 __ jmp(&unordered);
3177 }
3178
3179 __ bind(&maybe_undefined2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003180 if (Token::IsOrderedRelationalCompareOp(op())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003181 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3182 __ Branch(&unordered, eq, a1, Operand(at));
3183 }
3184
Ben Murdoch257744e2011-11-30 15:57:28 +00003185 __ bind(&miss);
3186 GenerateMiss(masm);
3187}
3188
3189
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003190void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3191 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00003192 Label miss;
3193
3194 // Registers containing left and right operands respectively.
3195 Register left = a1;
3196 Register right = a0;
3197 Register tmp1 = a2;
3198 Register tmp2 = a3;
3199
3200 // Check that both operands are heap objects.
3201 __ JumpIfEitherSmi(left, right, &miss);
3202
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003203 // Check that both operands are internalized strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00003204 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3205 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3206 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3207 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003208 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3209 __ Or(tmp1, tmp1, Operand(tmp2));
3210 __ And(at, tmp1, Operand(kIsNotStringMask | kIsNotInternalizedMask));
3211 __ Branch(&miss, ne, at, Operand(zero_reg));
3212
Ben Murdoch257744e2011-11-30 15:57:28 +00003213 // Make sure a0 is non-zero. At this point input operands are
3214 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003215 DCHECK(right.is(a0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003216 STATIC_ASSERT(EQUAL == 0);
3217 STATIC_ASSERT(kSmiTag == 0);
3218 __ mov(v0, right);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003219 // Internalized strings are compared by identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00003220 __ Ret(ne, left, Operand(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003221 DCHECK(is_int16(EQUAL));
3222 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003223 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003224
3225 __ bind(&miss);
3226 GenerateMiss(masm);
3227}
3228
3229
3230void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3231 DCHECK(state() == CompareICState::UNIQUE_NAME);
3232 DCHECK(GetCondition() == eq);
3233 Label miss;
3234
3235 // Registers containing left and right operands respectively.
3236 Register left = a1;
3237 Register right = a0;
3238 Register tmp1 = a2;
3239 Register tmp2 = a3;
3240
3241 // Check that both operands are heap objects.
3242 __ JumpIfEitherSmi(left, right, &miss);
3243
3244 // Check that both operands are unique names. This leaves the instance
3245 // types loaded in tmp1 and tmp2.
3246 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3247 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3248 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3249 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3250
3251 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss);
3252 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss);
3253
3254 // Use a0 as result
3255 __ mov(v0, a0);
3256
3257 // Unique names are compared by identity.
3258 Label done;
3259 __ Branch(&done, ne, left, Operand(right));
3260 // Make sure a0 is non-zero. At this point input operands are
3261 // guaranteed to be non-zero.
3262 DCHECK(right.is(a0));
3263 STATIC_ASSERT(EQUAL == 0);
3264 STATIC_ASSERT(kSmiTag == 0);
3265 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3266 __ bind(&done);
Ben Murdoch257744e2011-11-30 15:57:28 +00003267 __ Ret();
3268
3269 __ bind(&miss);
3270 GenerateMiss(masm);
3271}
3272
3273
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003274void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3275 DCHECK(state() == CompareICState::STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00003276 Label miss;
3277
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003278 bool equality = Token::IsEqualityOp(op());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003279
Ben Murdoch257744e2011-11-30 15:57:28 +00003280 // Registers containing left and right operands respectively.
3281 Register left = a1;
3282 Register right = a0;
3283 Register tmp1 = a2;
3284 Register tmp2 = a3;
3285 Register tmp3 = t0;
3286 Register tmp4 = t1;
3287 Register tmp5 = t2;
3288
3289 // Check that both operands are heap objects.
3290 __ JumpIfEitherSmi(left, right, &miss);
3291
3292 // Check that both operands are strings. This leaves the instance
3293 // types loaded in tmp1 and tmp2.
3294 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3295 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3296 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3297 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3298 STATIC_ASSERT(kNotStringTag != 0);
3299 __ Or(tmp3, tmp1, tmp2);
3300 __ And(tmp5, tmp3, Operand(kIsNotStringMask));
3301 __ Branch(&miss, ne, tmp5, Operand(zero_reg));
3302
3303 // Fast check for identical strings.
3304 Label left_ne_right;
3305 STATIC_ASSERT(EQUAL == 0);
3306 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003307 __ Branch(&left_ne_right, ne, left, Operand(right));
3308 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003309 __ mov(v0, zero_reg); // In the delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00003310 __ bind(&left_ne_right);
3311
3312 // Handle not identical strings.
3313
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003314 // Check that both strings are internalized strings. If they are, we're done
3315 // because we already know they are not identical. We know they are both
3316 // strings.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003317 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003318 DCHECK(GetCondition() == eq);
3319 STATIC_ASSERT(kInternalizedTag == 0);
3320 __ Or(tmp3, tmp1, Operand(tmp2));
3321 __ And(tmp5, tmp3, Operand(kIsNotInternalizedMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003322 Label is_symbol;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003323 __ Branch(&is_symbol, ne, tmp5, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003324 // Make sure a0 is non-zero. At this point input operands are
3325 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003326 DCHECK(right.is(a0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003327 __ Ret(USE_DELAY_SLOT);
3328 __ mov(v0, a0); // In the delay slot.
3329 __ bind(&is_symbol);
3330 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003331
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003332 // Check that both strings are sequential one-byte.
Ben Murdoch257744e2011-11-30 15:57:28 +00003333 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003334 __ JumpIfBothInstanceTypesAreNotSequentialOneByte(tmp1, tmp2, tmp3, tmp4,
3335 &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003336
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003337 // Compare flat one-byte strings. Returns when done.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003338 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003339 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1, tmp2,
3340 tmp3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003341 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003342 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3343 tmp2, tmp3, tmp4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003344 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003345
3346 // Handle more complex cases in runtime.
3347 __ bind(&runtime);
3348 __ Push(left, right);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003349 if (equality) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003350 __ TailCallRuntime(Runtime::kStringEquals);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003351 } else {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003352 __ TailCallRuntime(Runtime::kStringCompare);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003353 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003354
3355 __ bind(&miss);
3356 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01003357}
3358
3359
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003360void CompareICStub::GenerateReceivers(MacroAssembler* masm) {
3361 DCHECK_EQ(CompareICState::RECEIVER, state());
Ben Murdoch257744e2011-11-30 15:57:28 +00003362 Label miss;
3363 __ And(a2, a1, Operand(a0));
3364 __ JumpIfSmi(a2, &miss);
3365
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003366 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +00003367 __ GetObjectType(a0, a2, a2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003368 __ Branch(&miss, lt, a2, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00003369 __ GetObjectType(a1, a2, a2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003370 __ Branch(&miss, lt, a2, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00003371
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003372 DCHECK_EQ(eq, GetCondition());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003373 __ Ret(USE_DELAY_SLOT);
3374 __ subu(v0, a0, a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003375
3376 __ bind(&miss);
3377 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01003378}
3379
3380
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003381void CompareICStub::GenerateKnownReceivers(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003382 Label miss;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003383 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003384 __ And(a2, a1, a0);
3385 __ JumpIfSmi(a2, &miss);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003386 __ GetWeakValue(t0, cell);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003387 __ lw(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
3388 __ lw(a3, FieldMemOperand(a1, HeapObject::kMapOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003389 __ Branch(&miss, ne, a2, Operand(t0));
3390 __ Branch(&miss, ne, a3, Operand(t0));
Ben Murdoch85b71792012-04-11 18:30:58 +01003391
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003392 if (Token::IsEqualityOp(op())) {
3393 __ Ret(USE_DELAY_SLOT);
3394 __ subu(v0, a0, a1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003395 } else {
3396 if (op() == Token::LT || op() == Token::LTE) {
3397 __ li(a2, Operand(Smi::FromInt(GREATER)));
3398 } else {
3399 __ li(a2, Operand(Smi::FromInt(LESS)));
3400 }
3401 __ Push(a1, a0, a2);
3402 __ TailCallRuntime(Runtime::kCompare);
3403 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003404
3405 __ bind(&miss);
3406 GenerateMiss(masm);
3407}
3408
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003409
3410void CompareICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003411 {
3412 // Call the runtime system in a fresh internal frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003413 FrameScope scope(masm, StackFrame::INTERNAL);
3414 __ Push(a1, a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003415 __ Push(ra, a1, a0);
3416 __ li(t0, Operand(Smi::FromInt(op())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003417 __ addiu(sp, sp, -kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003418 __ CallRuntime(Runtime::kCompareIC_Miss, 3, kDontSaveFPRegs,
3419 USE_DELAY_SLOT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003420 __ sw(t0, MemOperand(sp)); // In the delay slot.
3421 // Compute the entry point of the rewritten stub.
3422 __ Addu(a2, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
3423 // Restore registers.
3424 __ Pop(a1, a0, ra);
3425 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003426 __ Jump(a2);
3427}
3428
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003429
Ben Murdoch257744e2011-11-30 15:57:28 +00003430void DirectCEntryStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003431 // Make place for arguments to fit C calling convention. Most of the callers
3432 // of DirectCEntryStub::GenerateCall are using EnterExitFrame/LeaveExitFrame
3433 // so they handle stack restoring and we don't have to do that here.
3434 // Any caller of DirectCEntryStub::GenerateCall must take care of dropping
3435 // kCArgsSlotsSize stack space after the call.
3436 __ Subu(sp, sp, Operand(kCArgsSlotsSize));
3437 // Place the return address on the stack, making the call
3438 // GC safe. The RegExp backend also relies on this.
3439 __ sw(ra, MemOperand(sp, kCArgsSlotsSize));
3440 __ Call(t9); // Call the C++ function.
Ben Murdoch257744e2011-11-30 15:57:28 +00003441 __ lw(t9, MemOperand(sp, kCArgsSlotsSize));
3442
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003443 if (FLAG_debug_code && FLAG_enable_slow_asserts) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003444 // In case of an error the return address may point to a memory area
3445 // filled with kZapValue by the GC.
3446 // Dereference the address and check for this.
3447 __ lw(t0, MemOperand(t9));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003448 __ Assert(ne, kReceivedInvalidReturnAddress, t0,
Ben Murdoch257744e2011-11-30 15:57:28 +00003449 Operand(reinterpret_cast<uint32_t>(kZapValue)));
3450 }
3451 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01003452}
3453
3454
Ben Murdoch257744e2011-11-30 15:57:28 +00003455void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoch257744e2011-11-30 15:57:28 +00003456 Register target) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003457 intptr_t loc =
3458 reinterpret_cast<intptr_t>(GetCode().location());
Ben Murdoch257744e2011-11-30 15:57:28 +00003459 __ Move(t9, target);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003460 __ li(at, Operand(loc, RelocInfo::CODE_TARGET), CONSTANT_SIZE);
3461 __ Call(at);
Ben Murdoch257744e2011-11-30 15:57:28 +00003462}
3463
3464
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003465void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3466 Label* miss,
3467 Label* done,
3468 Register receiver,
3469 Register properties,
3470 Handle<Name> name,
3471 Register scratch0) {
3472 DCHECK(name->IsUniqueName());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003473 // If names of slots in range from 1 to kProbes - 1 for the hash value are
Ben Murdoch257744e2011-11-30 15:57:28 +00003474 // not equal to the name and kProbes-th slot is not used (its name is the
3475 // undefined value), it guarantees the hash table doesn't contain the
3476 // property. It's true even if some slots represent deleted properties
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003477 // (their names are the hole value).
Ben Murdoch257744e2011-11-30 15:57:28 +00003478 for (int i = 0; i < kInlinedProbes; i++) {
3479 // scratch0 points to properties hash.
3480 // Compute the masked index: (hash + i + i * i) & mask.
3481 Register index = scratch0;
3482 // Capacity is smi 2^n.
3483 __ lw(index, FieldMemOperand(properties, kCapacityOffset));
3484 __ Subu(index, index, Operand(1));
3485 __ And(index, index, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003486 Smi::FromInt(name->Hash() + NameDictionary::GetProbeOffset(i))));
Ben Murdoch257744e2011-11-30 15:57:28 +00003487
3488 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003489 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003490 __ Lsa(index, index, index, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003491
3492 Register entity_name = scratch0;
3493 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003494 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003495 Register tmp = properties;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003496 __ Lsa(tmp, properties, index, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003497 __ lw(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
3498
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003499 DCHECK(!tmp.is(entity_name));
Ben Murdoch257744e2011-11-30 15:57:28 +00003500 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
3501 __ Branch(done, eq, entity_name, Operand(tmp));
3502
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003503 // Load the hole ready for use below:
3504 __ LoadRoot(tmp, Heap::kTheHoleValueRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003505
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003506 // Stop if found the property.
3507 __ Branch(miss, eq, entity_name, Operand(Handle<Name>(name)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003508
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003509 Label good;
3510 __ Branch(&good, eq, entity_name, Operand(tmp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003511
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003512 // Check if the entry name is not a unique name.
3513 __ lw(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
3514 __ lbu(entity_name,
3515 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
3516 __ JumpIfNotUniqueNameInstanceType(entity_name, miss);
3517 __ bind(&good);
Ben Murdoch257744e2011-11-30 15:57:28 +00003518
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003519 // Restore the properties.
3520 __ lw(properties,
3521 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003522 }
3523
3524 const int spill_mask =
3525 (ra.bit() | t2.bit() | t1.bit() | t0.bit() | a3.bit() |
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003526 a2.bit() | a1.bit() | a0.bit() | v0.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00003527
3528 __ MultiPush(spill_mask);
3529 __ lw(a0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003530 __ li(a1, Operand(Handle<Name>(name)));
3531 NameDictionaryLookupStub stub(masm->isolate(), NEGATIVE_LOOKUP);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003532 __ CallStub(&stub);
3533 __ mov(at, v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003534 __ MultiPop(spill_mask);
3535
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003536 __ Branch(done, eq, at, Operand(zero_reg));
3537 __ Branch(miss, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00003538}
3539
3540
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003541// Probe the name dictionary in the |elements| register. Jump to the
Ben Murdoch257744e2011-11-30 15:57:28 +00003542// |done| label if a property with the given name is found. Jump to
3543// the |miss| label otherwise.
3544// If lookup was successful |scratch2| will be equal to elements + 4 * index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003545void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3546 Label* miss,
3547 Label* done,
3548 Register elements,
3549 Register name,
3550 Register scratch1,
3551 Register scratch2) {
3552 DCHECK(!elements.is(scratch1));
3553 DCHECK(!elements.is(scratch2));
3554 DCHECK(!name.is(scratch1));
3555 DCHECK(!name.is(scratch2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003556
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003557 __ AssertName(name);
Ben Murdoch257744e2011-11-30 15:57:28 +00003558
3559 // Compute the capacity mask.
3560 __ lw(scratch1, FieldMemOperand(elements, kCapacityOffset));
3561 __ sra(scratch1, scratch1, kSmiTagSize); // convert smi to int
3562 __ Subu(scratch1, scratch1, Operand(1));
3563
3564 // Generate an unrolled loop that performs a few probes before
3565 // giving up. Measurements done on Gmail indicate that 2 probes
3566 // cover ~93% of loads from dictionaries.
3567 for (int i = 0; i < kInlinedProbes; i++) {
3568 // Compute the masked index: (hash + i + i * i) & mask.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003569 __ lw(scratch2, FieldMemOperand(name, Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003570 if (i > 0) {
3571 // Add the probe offset (i + i * i) left shifted to avoid right shifting
3572 // the hash in a separate instruction. The value hash + i + i * i is right
3573 // shifted in the following and instruction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003574 DCHECK(NameDictionary::GetProbeOffset(i) <
3575 1 << (32 - Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003576 __ Addu(scratch2, scratch2, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003577 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003578 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003579 __ srl(scratch2, scratch2, Name::kHashShift);
Ben Murdoch257744e2011-11-30 15:57:28 +00003580 __ And(scratch2, scratch1, scratch2);
3581
3582 // Scale the index by multiplying by the element size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003583 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003584 // scratch2 = scratch2 * 3.
3585
Ben Murdoch097c5b22016-05-18 11:27:45 +01003586 __ Lsa(scratch2, scratch2, scratch2, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003587
3588 // Check if the key is identical to the name.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003589 __ Lsa(scratch2, elements, scratch2, 2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003590 __ lw(at, FieldMemOperand(scratch2, kElementsStartOffset));
3591 __ Branch(done, eq, name, Operand(at));
3592 }
3593
3594 const int spill_mask =
3595 (ra.bit() | t2.bit() | t1.bit() | t0.bit() |
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003596 a3.bit() | a2.bit() | a1.bit() | a0.bit() | v0.bit()) &
Ben Murdoch257744e2011-11-30 15:57:28 +00003597 ~(scratch1.bit() | scratch2.bit());
3598
3599 __ MultiPush(spill_mask);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003600 if (name.is(a0)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003601 DCHECK(!elements.is(a1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003602 __ Move(a1, name);
3603 __ Move(a0, elements);
3604 } else {
3605 __ Move(a0, elements);
3606 __ Move(a1, name);
3607 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003608 NameDictionaryLookupStub stub(masm->isolate(), POSITIVE_LOOKUP);
Ben Murdoch257744e2011-11-30 15:57:28 +00003609 __ CallStub(&stub);
3610 __ mov(scratch2, a2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003611 __ mov(at, v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003612 __ MultiPop(spill_mask);
3613
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003614 __ Branch(done, ne, at, Operand(zero_reg));
3615 __ Branch(miss, eq, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00003616}
3617
3618
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003619void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003620 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3621 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00003622 // Registers:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003623 // result: NameDictionary to probe
Ben Murdoch257744e2011-11-30 15:57:28 +00003624 // a1: key
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003625 // dictionary: NameDictionary to probe.
3626 // index: will hold an index of entry if lookup is successful.
3627 // might alias with result_.
Ben Murdoch257744e2011-11-30 15:57:28 +00003628 // Returns:
3629 // result_ is zero if lookup failed, non zero otherwise.
3630
3631 Register result = v0;
3632 Register dictionary = a0;
3633 Register key = a1;
3634 Register index = a2;
3635 Register mask = a3;
3636 Register hash = t0;
3637 Register undefined = t1;
3638 Register entry_key = t2;
3639
3640 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3641
3642 __ lw(mask, FieldMemOperand(dictionary, kCapacityOffset));
3643 __ sra(mask, mask, kSmiTagSize);
3644 __ Subu(mask, mask, Operand(1));
3645
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003646 __ lw(hash, FieldMemOperand(key, Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003647
3648 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
3649
3650 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3651 // Compute the masked index: (hash + i + i * i) & mask.
3652 // Capacity is smi 2^n.
3653 if (i > 0) {
3654 // Add the probe offset (i + i * i) left shifted to avoid right shifting
3655 // the hash in a separate instruction. The value hash + i + i * i is right
3656 // shifted in the following and instruction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003657 DCHECK(NameDictionary::GetProbeOffset(i) <
3658 1 << (32 - Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003659 __ Addu(index, hash, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003660 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003661 } else {
3662 __ mov(index, hash);
3663 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003664 __ srl(index, index, Name::kHashShift);
Ben Murdoch257744e2011-11-30 15:57:28 +00003665 __ And(index, mask, index);
3666
3667 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003668 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003669 // index *= 3.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003670 __ Lsa(index, index, index, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003671
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003672 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003673 __ Lsa(index, dictionary, index, 2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003674 __ lw(entry_key, FieldMemOperand(index, kElementsStartOffset));
3675
3676 // Having undefined at this place means the name is not contained.
3677 __ Branch(&not_in_dictionary, eq, entry_key, Operand(undefined));
3678
3679 // Stop if found the property.
3680 __ Branch(&in_dictionary, eq, entry_key, Operand(key));
3681
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003682 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3683 // Check if the entry name is not a unique name.
Ben Murdoch257744e2011-11-30 15:57:28 +00003684 __ lw(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
3685 __ lbu(entry_key,
3686 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003687 __ JumpIfNotUniqueNameInstanceType(entry_key, &maybe_in_dictionary);
Ben Murdoch257744e2011-11-30 15:57:28 +00003688 }
3689 }
3690
3691 __ bind(&maybe_in_dictionary);
3692 // If we are doing negative lookup then probing failure should be
3693 // treated as a lookup success. For positive lookup probing failure
3694 // should be treated as lookup failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003695 if (mode() == POSITIVE_LOOKUP) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003696 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003697 __ mov(result, zero_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00003698 }
3699
3700 __ bind(&in_dictionary);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003701 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003702 __ li(result, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003703
3704 __ bind(&not_in_dictionary);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003705 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003706 __ mov(result, zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003707}
3708
3709
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003710void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3711 Isolate* isolate) {
3712 StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
3713 stub1.GetCode();
3714 // Hydrogen code stubs need stub2 at snapshot time.
3715 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3716 stub2.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003717}
3718
3719
3720// Takes the input in 3 registers: address_ value_ and object_. A pointer to
3721// the value has just been written into the object, now this stub makes sure
3722// we keep the GC informed. The word in the object where the value has been
3723// written is in the address register.
3724void RecordWriteStub::Generate(MacroAssembler* masm) {
3725 Label skip_to_incremental_noncompacting;
3726 Label skip_to_incremental_compacting;
3727
3728 // The first two branch+nop instructions are generated with labels so as to
3729 // get the offset fixed up correctly by the bind(Label*) call. We patch it
3730 // back and forth between a "bne zero_reg, zero_reg, ..." (a nop in this
3731 // position) and the "beq zero_reg, zero_reg, ..." when we start and stop
3732 // incremental heap marking.
3733 // See RecordWriteStub::Patch for details.
3734 __ beq(zero_reg, zero_reg, &skip_to_incremental_noncompacting);
3735 __ nop();
3736 __ beq(zero_reg, zero_reg, &skip_to_incremental_compacting);
3737 __ nop();
3738
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003739 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3740 __ RememberedSetHelper(object(),
3741 address(),
3742 value(),
3743 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003744 MacroAssembler::kReturnAtEnd);
3745 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003746 __ Ret();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003747
3748 __ bind(&skip_to_incremental_noncompacting);
3749 GenerateIncremental(masm, INCREMENTAL);
3750
3751 __ bind(&skip_to_incremental_compacting);
3752 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3753
3754 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3755 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
3756
3757 PatchBranchIntoNop(masm, 0);
3758 PatchBranchIntoNop(masm, 2 * Assembler::kInstrSize);
3759}
3760
3761
3762void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3763 regs_.Save(masm);
3764
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003765 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003766 Label dont_need_remembered_set;
3767
3768 __ lw(regs_.scratch0(), MemOperand(regs_.address(), 0));
3769 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
3770 regs_.scratch0(),
3771 &dont_need_remembered_set);
3772
Ben Murdoch097c5b22016-05-18 11:27:45 +01003773 __ JumpIfInNewSpace(regs_.object(), regs_.scratch0(),
3774 &dont_need_remembered_set);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003775
3776 // First notify the incremental marker if necessary, then update the
3777 // remembered set.
3778 CheckNeedsToInformIncrementalMarker(
3779 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003780 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003781 regs_.Restore(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003782 __ RememberedSetHelper(object(),
3783 address(),
3784 value(),
3785 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003786 MacroAssembler::kReturnAtEnd);
3787
3788 __ bind(&dont_need_remembered_set);
3789 }
3790
3791 CheckNeedsToInformIncrementalMarker(
3792 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003793 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003794 regs_.Restore(masm);
3795 __ Ret();
3796}
3797
3798
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003799void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3800 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003801 int argument_count = 3;
3802 __ PrepareCallCFunction(argument_count, regs_.scratch0());
3803 Register address =
3804 a0.is(regs_.address()) ? regs_.scratch0() : regs_.address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003805 DCHECK(!address.is(regs_.object()));
3806 DCHECK(!address.is(a0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003807 __ Move(address, regs_.address());
3808 __ Move(a0, regs_.object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003809 __ Move(a1, address);
3810 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003811
3812 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003813 __ CallCFunction(
3814 ExternalReference::incremental_marking_record_write_function(isolate()),
3815 argument_count);
3816 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003817}
3818
3819
3820void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3821 MacroAssembler* masm,
3822 OnNoNeedToInformIncrementalMarker on_no_need,
3823 Mode mode) {
3824 Label on_black;
3825 Label need_incremental;
3826 Label need_incremental_pop_scratch;
3827
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003828 __ And(regs_.scratch0(), regs_.object(), Operand(~Page::kPageAlignmentMask));
3829 __ lw(regs_.scratch1(),
3830 MemOperand(regs_.scratch0(),
3831 MemoryChunk::kWriteBarrierCounterOffset));
3832 __ Subu(regs_.scratch1(), regs_.scratch1(), Operand(1));
3833 __ sw(regs_.scratch1(),
3834 MemOperand(regs_.scratch0(),
3835 MemoryChunk::kWriteBarrierCounterOffset));
3836 __ Branch(&need_incremental, lt, regs_.scratch1(), Operand(zero_reg));
3837
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003838 // Let's look at the color of the object: If it is not black we don't have
3839 // to inform the incremental marker.
3840 __ JumpIfBlack(regs_.object(), regs_.scratch0(), regs_.scratch1(), &on_black);
3841
3842 regs_.Restore(masm);
3843 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003844 __ RememberedSetHelper(object(),
3845 address(),
3846 value(),
3847 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003848 MacroAssembler::kReturnAtEnd);
3849 } else {
3850 __ Ret();
3851 }
3852
3853 __ bind(&on_black);
3854
3855 // Get the value from the slot.
3856 __ lw(regs_.scratch0(), MemOperand(regs_.address(), 0));
3857
3858 if (mode == INCREMENTAL_COMPACTION) {
3859 Label ensure_not_white;
3860
3861 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3862 regs_.scratch1(), // Scratch.
3863 MemoryChunk::kEvacuationCandidateMask,
3864 eq,
3865 &ensure_not_white);
3866
3867 __ CheckPageFlag(regs_.object(),
3868 regs_.scratch1(), // Scratch.
3869 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3870 eq,
3871 &need_incremental);
3872
3873 __ bind(&ensure_not_white);
3874 }
3875
3876 // We need extra registers for this, so we push the object and the address
3877 // register temporarily.
3878 __ Push(regs_.object(), regs_.address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003879 __ JumpIfWhite(regs_.scratch0(), // The value.
3880 regs_.scratch1(), // Scratch.
3881 regs_.object(), // Scratch.
3882 regs_.address(), // Scratch.
3883 &need_incremental_pop_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003884 __ Pop(regs_.object(), regs_.address());
3885
3886 regs_.Restore(masm);
3887 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003888 __ RememberedSetHelper(object(),
3889 address(),
3890 value(),
3891 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003892 MacroAssembler::kReturnAtEnd);
3893 } else {
3894 __ Ret();
3895 }
3896
3897 __ bind(&need_incremental_pop_scratch);
3898 __ Pop(regs_.object(), regs_.address());
3899
3900 __ bind(&need_incremental);
3901
3902 // Fall through when we need to inform the incremental marker.
3903}
3904
3905
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003906void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
3907 CEntryStub ces(isolate(), 1, kSaveFPRegs);
3908 __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
3909 int parameter_count_offset =
3910 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
3911 __ lw(a1, MemOperand(fp, parameter_count_offset));
3912 if (function_mode() == JS_FUNCTION_STUB_MODE) {
3913 __ Addu(a1, a1, Operand(1));
3914 }
3915 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
3916 __ sll(a1, a1, kPointerSizeLog2);
3917 __ Ret(USE_DELAY_SLOT);
3918 __ Addu(sp, sp, a1);
3919}
3920
3921
3922void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003923 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3924 LoadICStub stub(isolate(), state());
3925 stub.GenerateForTrampoline(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003926}
3927
3928
3929void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003930 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3931 KeyedLoadICStub stub(isolate(), state());
3932 stub.GenerateForTrampoline(masm);
3933}
3934
3935
3936void CallICTrampolineStub::Generate(MacroAssembler* masm) {
3937 __ EmitLoadTypeFeedbackVector(a2);
3938 CallICStub stub(isolate(), state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003939 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
3940}
3941
3942
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003943void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
3944
3945
3946void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3947 GenerateImpl(masm, true);
3948}
3949
3950
3951static void HandleArrayCases(MacroAssembler* masm, Register feedback,
3952 Register receiver_map, Register scratch1,
3953 Register scratch2, bool is_polymorphic,
3954 Label* miss) {
3955 // feedback initially contains the feedback array
3956 Label next_loop, prepare_next;
3957 Label start_polymorphic;
3958
3959 Register cached_map = scratch1;
3960
3961 __ lw(cached_map,
3962 FieldMemOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3963 __ lw(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
3964 __ Branch(&start_polymorphic, ne, receiver_map, Operand(cached_map));
3965 // found, now call handler.
3966 Register handler = feedback;
3967 __ lw(handler, FieldMemOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3968 __ Addu(t9, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
3969 __ Jump(t9);
3970
3971
3972 Register length = scratch2;
3973 __ bind(&start_polymorphic);
3974 __ lw(length, FieldMemOperand(feedback, FixedArray::kLengthOffset));
3975 if (!is_polymorphic) {
3976 // If the IC could be monomorphic we have to make sure we don't go past the
3977 // end of the feedback array.
3978 __ Branch(miss, eq, length, Operand(Smi::FromInt(2)));
3979 }
3980
3981 Register too_far = length;
3982 Register pointer_reg = feedback;
3983
3984 // +-----+------+------+-----+-----+ ... ----+
3985 // | map | len | wm0 | h0 | wm1 | hN |
3986 // +-----+------+------+-----+-----+ ... ----+
3987 // 0 1 2 len-1
3988 // ^ ^
3989 // | |
3990 // pointer_reg too_far
3991 // aka feedback scratch2
3992 // also need receiver_map
3993 // use cached_map (scratch1) to look in the weak map values.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003994 __ Lsa(too_far, feedback, length, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003995 __ Addu(too_far, too_far, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3996 __ Addu(pointer_reg, feedback,
3997 Operand(FixedArray::OffsetOfElementAt(2) - kHeapObjectTag));
3998
3999 __ bind(&next_loop);
4000 __ lw(cached_map, MemOperand(pointer_reg));
4001 __ lw(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
4002 __ Branch(&prepare_next, ne, receiver_map, Operand(cached_map));
4003 __ lw(handler, MemOperand(pointer_reg, kPointerSize));
4004 __ Addu(t9, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
4005 __ Jump(t9);
4006
4007 __ bind(&prepare_next);
4008 __ Addu(pointer_reg, pointer_reg, Operand(kPointerSize * 2));
4009 __ Branch(&next_loop, lt, pointer_reg, Operand(too_far));
4010
4011 // We exhausted our array of map handler pairs.
4012 __ jmp(miss);
4013}
4014
4015
4016static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
4017 Register receiver_map, Register feedback,
4018 Register vector, Register slot,
4019 Register scratch, Label* compare_map,
4020 Label* load_smi_map, Label* try_array) {
4021 __ JumpIfSmi(receiver, load_smi_map);
4022 __ lw(receiver_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
4023 __ bind(compare_map);
4024 Register cached_map = scratch;
4025 // Move the weak map into the weak_cell register.
4026 __ lw(cached_map, FieldMemOperand(feedback, WeakCell::kValueOffset));
4027 __ Branch(try_array, ne, cached_map, Operand(receiver_map));
4028 Register handler = feedback;
4029
Ben Murdoch097c5b22016-05-18 11:27:45 +01004030 __ Lsa(handler, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004031 __ lw(handler,
4032 FieldMemOperand(handler, FixedArray::kHeaderSize + kPointerSize));
4033 __ Addu(t9, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
4034 __ Jump(t9);
4035}
4036
4037
4038void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4039 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // a1
4040 Register name = LoadWithVectorDescriptor::NameRegister(); // a2
4041 Register vector = LoadWithVectorDescriptor::VectorRegister(); // a3
4042 Register slot = LoadWithVectorDescriptor::SlotRegister(); // a0
4043 Register feedback = t0;
4044 Register receiver_map = t1;
4045 Register scratch1 = t4;
4046
Ben Murdoch097c5b22016-05-18 11:27:45 +01004047 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004048 __ lw(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
4049
4050 // Try to quickly handle the monomorphic case without knowing for sure
4051 // if we have a weak cell in feedback. We do know it's safe to look
4052 // at WeakCell::kValueOffset.
4053 Label try_array, load_smi_map, compare_map;
4054 Label not_array, miss;
4055 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
4056 scratch1, &compare_map, &load_smi_map, &try_array);
4057
4058 // Is it a fixed array?
4059 __ bind(&try_array);
4060 __ lw(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
4061 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4062 __ Branch(&not_array, ne, at, Operand(scratch1));
4063 HandleArrayCases(masm, feedback, receiver_map, scratch1, t5, true, &miss);
4064
4065 __ bind(&not_array);
4066 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
4067 __ Branch(&miss, ne, at, Operand(feedback));
4068 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4069 Code::ComputeHandlerFlags(Code::LOAD_IC));
4070 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
4071 receiver, name, feedback,
4072 receiver_map, scratch1, t5);
4073
4074 __ bind(&miss);
4075 LoadIC::GenerateMiss(masm);
4076
4077 __ bind(&load_smi_map);
4078 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4079 __ jmp(&compare_map);
4080}
4081
4082
4083void KeyedLoadICStub::Generate(MacroAssembler* masm) {
4084 GenerateImpl(masm, false);
4085}
4086
4087
4088void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
4089 GenerateImpl(masm, true);
4090}
4091
4092
4093void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4094 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // a1
4095 Register key = LoadWithVectorDescriptor::NameRegister(); // a2
4096 Register vector = LoadWithVectorDescriptor::VectorRegister(); // a3
4097 Register slot = LoadWithVectorDescriptor::SlotRegister(); // a0
4098 Register feedback = t0;
4099 Register receiver_map = t1;
4100 Register scratch1 = t4;
4101
Ben Murdoch097c5b22016-05-18 11:27:45 +01004102 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004103 __ lw(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
4104
4105 // Try to quickly handle the monomorphic case without knowing for sure
4106 // if we have a weak cell in feedback. We do know it's safe to look
4107 // at WeakCell::kValueOffset.
4108 Label try_array, load_smi_map, compare_map;
4109 Label not_array, miss;
4110 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
4111 scratch1, &compare_map, &load_smi_map, &try_array);
4112
4113 __ bind(&try_array);
4114 // Is it a fixed array?
4115 __ lw(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
4116 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4117 __ Branch(&not_array, ne, at, Operand(scratch1));
4118 // We have a polymorphic element handler.
4119 __ JumpIfNotSmi(key, &miss);
4120
4121 Label polymorphic, try_poly_name;
4122 __ bind(&polymorphic);
4123 HandleArrayCases(masm, feedback, receiver_map, scratch1, t5, true, &miss);
4124
4125 __ bind(&not_array);
4126 // Is it generic?
4127 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
4128 __ Branch(&try_poly_name, ne, at, Operand(feedback));
4129 Handle<Code> megamorphic_stub =
4130 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4131 __ Jump(megamorphic_stub, RelocInfo::CODE_TARGET);
4132
4133 __ bind(&try_poly_name);
4134 // We might have a name in feedback, and a fixed array in the next slot.
4135 __ Branch(&miss, ne, key, Operand(feedback));
4136 // If the name comparison succeeded, we know we have a fixed array with
4137 // at least one map/handler pair.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004138 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004139 __ lw(feedback,
4140 FieldMemOperand(feedback, FixedArray::kHeaderSize + kPointerSize));
4141 HandleArrayCases(masm, feedback, receiver_map, scratch1, t5, false, &miss);
4142
4143 __ bind(&miss);
4144 KeyedLoadIC::GenerateMiss(masm);
4145
4146 __ bind(&load_smi_map);
4147 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
4148 __ jmp(&compare_map);
4149}
4150
4151
4152void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4153 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
4154 VectorStoreICStub stub(isolate(), state());
4155 stub.GenerateForTrampoline(masm);
4156}
4157
4158
4159void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
4160 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
4161 VectorKeyedStoreICStub stub(isolate(), state());
4162 stub.GenerateForTrampoline(masm);
4163}
4164
4165
4166void VectorStoreICStub::Generate(MacroAssembler* masm) {
4167 GenerateImpl(masm, false);
4168}
4169
4170
4171void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4172 GenerateImpl(masm, true);
4173}
4174
4175
4176void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4177 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // a1
4178 Register key = VectorStoreICDescriptor::NameRegister(); // a2
4179 Register vector = VectorStoreICDescriptor::VectorRegister(); // a3
4180 Register slot = VectorStoreICDescriptor::SlotRegister(); // t0
4181 DCHECK(VectorStoreICDescriptor::ValueRegister().is(a0)); // a0
4182 Register feedback = t1;
4183 Register receiver_map = t2;
4184 Register scratch1 = t5;
4185
Ben Murdoch097c5b22016-05-18 11:27:45 +01004186 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004187 __ lw(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
4188
4189 // Try to quickly handle the monomorphic case without knowing for sure
4190 // if we have a weak cell in feedback. We do know it's safe to look
4191 // at WeakCell::kValueOffset.
4192 Label try_array, load_smi_map, compare_map;
4193 Label not_array, miss;
4194 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
4195 scratch1, &compare_map, &load_smi_map, &try_array);
4196
4197 // Is it a fixed array?
4198 __ bind(&try_array);
4199 __ lw(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
4200 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4201 __ Branch(&not_array, ne, scratch1, Operand(at));
4202
4203 Register scratch2 = t4;
4204 HandleArrayCases(masm, feedback, receiver_map, scratch1, scratch2, true,
4205 &miss);
4206
4207 __ bind(&not_array);
4208 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
4209 __ Branch(&miss, ne, feedback, Operand(at));
4210 Code::Flags code_flags = Code::RemoveTypeAndHolderFromFlags(
4211 Code::ComputeHandlerFlags(Code::STORE_IC));
4212 masm->isolate()->stub_cache()->GenerateProbe(
4213 masm, Code::STORE_IC, code_flags, receiver, key, feedback, receiver_map,
4214 scratch1, scratch2);
4215
4216 __ bind(&miss);
4217 StoreIC::GenerateMiss(masm);
4218
4219 __ bind(&load_smi_map);
4220 __ Branch(USE_DELAY_SLOT, &compare_map);
4221 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex); // In delay slot.
4222}
4223
4224
4225void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4226 GenerateImpl(masm, false);
4227}
4228
4229
4230void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4231 GenerateImpl(masm, true);
4232}
4233
4234
4235static void HandlePolymorphicStoreCase(MacroAssembler* masm, Register feedback,
4236 Register receiver_map, Register scratch1,
4237 Register scratch2, Label* miss) {
4238 // feedback initially contains the feedback array
4239 Label next_loop, prepare_next;
4240 Label start_polymorphic;
4241 Label transition_call;
4242
4243 Register cached_map = scratch1;
4244 Register too_far = scratch2;
4245 Register pointer_reg = feedback;
4246 __ lw(too_far, FieldMemOperand(feedback, FixedArray::kLengthOffset));
4247
4248 // +-----+------+------+-----+-----+-----+ ... ----+
4249 // | map | len | wm0 | wt0 | h0 | wm1 | hN |
4250 // +-----+------+------+-----+-----+ ----+ ... ----+
4251 // 0 1 2 len-1
4252 // ^ ^
4253 // | |
4254 // pointer_reg too_far
4255 // aka feedback scratch2
4256 // also need receiver_map
4257 // use cached_map (scratch1) to look in the weak map values.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004258 __ Lsa(too_far, feedback, too_far, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004259 __ Addu(too_far, too_far, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4260 __ Addu(pointer_reg, feedback,
4261 Operand(FixedArray::OffsetOfElementAt(0) - kHeapObjectTag));
4262
4263 __ bind(&next_loop);
4264 __ lw(cached_map, MemOperand(pointer_reg));
4265 __ lw(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
4266 __ Branch(&prepare_next, ne, receiver_map, Operand(cached_map));
4267 // Is it a transitioning store?
4268 __ lw(too_far, MemOperand(pointer_reg, kPointerSize));
4269 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4270 __ Branch(&transition_call, ne, too_far, Operand(at));
4271 __ lw(pointer_reg, MemOperand(pointer_reg, kPointerSize * 2));
4272 __ Addu(t9, pointer_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
4273 __ Jump(t9);
4274
4275 __ bind(&transition_call);
4276 __ lw(too_far, FieldMemOperand(too_far, WeakCell::kValueOffset));
4277 __ JumpIfSmi(too_far, miss);
4278
4279 __ lw(receiver_map, MemOperand(pointer_reg, kPointerSize * 2));
4280
4281 // Load the map into the correct register.
4282 DCHECK(feedback.is(VectorStoreTransitionDescriptor::MapRegister()));
4283 __ mov(feedback, too_far);
4284
4285 __ Addu(t9, receiver_map, Operand(Code::kHeaderSize - kHeapObjectTag));
4286 __ Jump(t9);
4287
4288 __ bind(&prepare_next);
4289 __ Addu(pointer_reg, pointer_reg, Operand(kPointerSize * 3));
4290 __ Branch(&next_loop, lt, pointer_reg, Operand(too_far));
4291
4292 // We exhausted our array of map handler pairs.
4293 __ jmp(miss);
4294}
4295
4296
4297void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4298 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // a1
4299 Register key = VectorStoreICDescriptor::NameRegister(); // a2
4300 Register vector = VectorStoreICDescriptor::VectorRegister(); // a3
4301 Register slot = VectorStoreICDescriptor::SlotRegister(); // t0
4302 DCHECK(VectorStoreICDescriptor::ValueRegister().is(a0)); // a0
4303 Register feedback = t1;
4304 Register receiver_map = t2;
4305 Register scratch1 = t5;
4306
Ben Murdoch097c5b22016-05-18 11:27:45 +01004307 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004308 __ lw(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
4309
4310 // Try to quickly handle the monomorphic case without knowing for sure
4311 // if we have a weak cell in feedback. We do know it's safe to look
4312 // at WeakCell::kValueOffset.
4313 Label try_array, load_smi_map, compare_map;
4314 Label not_array, miss;
4315 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
4316 scratch1, &compare_map, &load_smi_map, &try_array);
4317
4318 __ bind(&try_array);
4319 // Is it a fixed array?
4320 __ lw(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
4321 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4322 __ Branch(&not_array, ne, scratch1, Operand(at));
4323
4324 // We have a polymorphic element handler.
4325 Label polymorphic, try_poly_name;
4326 __ bind(&polymorphic);
4327
4328 Register scratch2 = t4;
4329
4330 HandlePolymorphicStoreCase(masm, feedback, receiver_map, scratch1, scratch2,
4331 &miss);
4332
4333 __ bind(&not_array);
4334 // Is it generic?
4335 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
4336 __ Branch(&try_poly_name, ne, feedback, Operand(at));
4337 Handle<Code> megamorphic_stub =
4338 KeyedStoreIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4339 __ Jump(megamorphic_stub, RelocInfo::CODE_TARGET);
4340
4341 __ bind(&try_poly_name);
4342 // We might have a name in feedback, and a fixed array in the next slot.
4343 __ Branch(&miss, ne, key, Operand(feedback));
4344 // If the name comparison succeeded, we know we have a fixed array with
4345 // at least one map/handler pair.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004346 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004347 __ lw(feedback,
4348 FieldMemOperand(feedback, FixedArray::kHeaderSize + kPointerSize));
4349 HandleArrayCases(masm, feedback, receiver_map, scratch1, scratch2, false,
4350 &miss);
4351
4352 __ bind(&miss);
4353 KeyedStoreIC::GenerateMiss(masm);
4354
4355 __ bind(&load_smi_map);
4356 __ Branch(USE_DELAY_SLOT, &compare_map);
4357 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex); // In delay slot.
4358}
4359
4360
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004361void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4362 if (masm->isolate()->function_entry_hook() != NULL) {
4363 ProfileEntryHookStub stub(masm->isolate());
4364 __ push(ra);
4365 __ CallStub(&stub);
4366 __ pop(ra);
4367 }
4368}
4369
4370
4371void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4372 // The entry hook is a "push ra" instruction, followed by a call.
4373 // Note: on MIPS "push" is 2 instruction
4374 const int32_t kReturnAddressDistanceFromFunctionStart =
4375 Assembler::kCallTargetAddressOffset + (2 * Assembler::kInstrSize);
4376
4377 // This should contain all kJSCallerSaved registers.
4378 const RegList kSavedRegs =
4379 kJSCallerSaved | // Caller saved registers.
4380 s5.bit(); // Saved stack pointer.
4381
4382 // We also save ra, so the count here is one higher than the mask indicates.
4383 const int32_t kNumSavedRegs = kNumJSCallerSaved + 2;
4384
4385 // Save all caller-save registers as this may be called from anywhere.
4386 __ MultiPush(kSavedRegs | ra.bit());
4387
4388 // Compute the function's address for the first argument.
4389 __ Subu(a0, ra, Operand(kReturnAddressDistanceFromFunctionStart));
4390
4391 // The caller's return address is above the saved temporaries.
4392 // Grab that for the second argument to the hook.
4393 __ Addu(a1, sp, Operand(kNumSavedRegs * kPointerSize));
4394
4395 // Align the stack if necessary.
4396 int frame_alignment = masm->ActivationFrameAlignment();
4397 if (frame_alignment > kPointerSize) {
4398 __ mov(s5, sp);
4399 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
4400 __ And(sp, sp, Operand(-frame_alignment));
4401 }
4402 __ Subu(sp, sp, kCArgsSlotsSize);
4403#if defined(V8_HOST_ARCH_MIPS)
4404 int32_t entry_hook =
4405 reinterpret_cast<int32_t>(isolate()->function_entry_hook());
4406 __ li(t9, Operand(entry_hook));
4407#else
4408 // Under the simulator we need to indirect the entry hook through a
4409 // trampoline function at a known address.
4410 // It additionally takes an isolate as a third parameter.
4411 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
4412
4413 ApiFunction dispatcher(FUNCTION_ADDR(EntryHookTrampoline));
4414 __ li(t9, Operand(ExternalReference(&dispatcher,
4415 ExternalReference::BUILTIN_CALL,
4416 isolate())));
4417#endif
4418 // Call C function through t9 to conform ABI for PIC.
4419 __ Call(t9);
4420
4421 // Restore the stack pointer if needed.
4422 if (frame_alignment > kPointerSize) {
4423 __ mov(sp, s5);
4424 } else {
4425 __ Addu(sp, sp, kCArgsSlotsSize);
4426 }
4427
4428 // Also pop ra to get Ret(0).
4429 __ MultiPop(kSavedRegs | ra.bit());
4430 __ Ret();
4431}
4432
4433
4434template<class T>
4435static void CreateArrayDispatch(MacroAssembler* masm,
4436 AllocationSiteOverrideMode mode) {
4437 if (mode == DISABLE_ALLOCATION_SITES) {
4438 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
4439 __ TailCallStub(&stub);
4440 } else if (mode == DONT_OVERRIDE) {
4441 int last_index = GetSequenceIndexFromFastElementsKind(
4442 TERMINAL_FAST_ELEMENTS_KIND);
4443 for (int i = 0; i <= last_index; ++i) {
4444 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4445 T stub(masm->isolate(), kind);
4446 __ TailCallStub(&stub, eq, a3, Operand(kind));
4447 }
4448
4449 // If we reached this point there is a problem.
4450 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4451 } else {
4452 UNREACHABLE();
4453 }
4454}
4455
4456
4457static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4458 AllocationSiteOverrideMode mode) {
4459 // a2 - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4460 // a3 - kind (if mode != DISABLE_ALLOCATION_SITES)
4461 // a0 - number of arguments
4462 // a1 - constructor?
4463 // sp[0] - last argument
4464 Label normal_sequence;
4465 if (mode == DONT_OVERRIDE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004466 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4467 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4468 STATIC_ASSERT(FAST_ELEMENTS == 2);
4469 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4470 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4471 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004472
4473 // is the low bit set? If so, we are holey and that is good.
4474 __ And(at, a3, Operand(1));
4475 __ Branch(&normal_sequence, ne, at, Operand(zero_reg));
4476 }
4477
4478 // look at the first argument
4479 __ lw(t1, MemOperand(sp, 0));
4480 __ Branch(&normal_sequence, eq, t1, Operand(zero_reg));
4481
4482 if (mode == DISABLE_ALLOCATION_SITES) {
4483 ElementsKind initial = GetInitialFastElementsKind();
4484 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4485
4486 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4487 holey_initial,
4488 DISABLE_ALLOCATION_SITES);
4489 __ TailCallStub(&stub_holey);
4490
4491 __ bind(&normal_sequence);
4492 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4493 initial,
4494 DISABLE_ALLOCATION_SITES);
4495 __ TailCallStub(&stub);
4496 } else if (mode == DONT_OVERRIDE) {
4497 // We are going to create a holey array, but our kind is non-holey.
4498 // Fix kind and retry (only if we have an allocation site in the slot).
4499 __ Addu(a3, a3, Operand(1));
4500
4501 if (FLAG_debug_code) {
4502 __ lw(t1, FieldMemOperand(a2, 0));
4503 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
4504 __ Assert(eq, kExpectedAllocationSite, t1, Operand(at));
4505 }
4506
4507 // Save the resulting elements kind in type info. We can't just store a3
4508 // in the AllocationSite::transition_info field because elements kind is
4509 // restricted to a portion of the field...upper bits need to be left alone.
4510 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4511 __ lw(t0, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4512 __ Addu(t0, t0, Operand(Smi::FromInt(kFastElementsKindPackedToHoley)));
4513 __ sw(t0, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4514
4515
4516 __ bind(&normal_sequence);
4517 int last_index = GetSequenceIndexFromFastElementsKind(
4518 TERMINAL_FAST_ELEMENTS_KIND);
4519 for (int i = 0; i <= last_index; ++i) {
4520 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4521 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4522 __ TailCallStub(&stub, eq, a3, Operand(kind));
4523 }
4524
4525 // If we reached this point there is a problem.
4526 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4527 } else {
4528 UNREACHABLE();
4529 }
4530}
4531
4532
4533template<class T>
4534static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4535 int to_index = GetSequenceIndexFromFastElementsKind(
4536 TERMINAL_FAST_ELEMENTS_KIND);
4537 for (int i = 0; i <= to_index; ++i) {
4538 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4539 T stub(isolate, kind);
4540 stub.GetCode();
4541 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4542 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4543 stub1.GetCode();
4544 }
4545 }
4546}
4547
4548
4549void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4550 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4551 isolate);
4552 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4553 isolate);
4554 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4555 isolate);
4556}
4557
4558
4559void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4560 Isolate* isolate) {
4561 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4562 for (int i = 0; i < 2; i++) {
4563 // For internal arrays we only need a few things.
4564 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4565 stubh1.GetCode();
4566 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4567 stubh2.GetCode();
4568 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4569 stubh3.GetCode();
4570 }
4571}
4572
4573
4574void ArrayConstructorStub::GenerateDispatchToArrayStub(
4575 MacroAssembler* masm,
4576 AllocationSiteOverrideMode mode) {
4577 if (argument_count() == ANY) {
4578 Label not_zero_case, not_one_case;
4579 __ And(at, a0, a0);
4580 __ Branch(&not_zero_case, ne, at, Operand(zero_reg));
4581 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4582
4583 __ bind(&not_zero_case);
4584 __ Branch(&not_one_case, gt, a0, Operand(1));
4585 CreateArrayDispatchOneArgument(masm, mode);
4586
4587 __ bind(&not_one_case);
4588 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4589 } else if (argument_count() == NONE) {
4590 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4591 } else if (argument_count() == ONE) {
4592 CreateArrayDispatchOneArgument(masm, mode);
4593 } else if (argument_count() == MORE_THAN_ONE) {
4594 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4595 } else {
4596 UNREACHABLE();
4597 }
4598}
4599
4600
4601void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4602 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004603 // -- a0 : argc (only if argument_count() is ANY or MORE_THAN_ONE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004604 // -- a1 : constructor
4605 // -- a2 : AllocationSite or undefined
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004606 // -- a3 : Original constructor
4607 // -- sp[0] : last argument
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004608 // -----------------------------------
4609
4610 if (FLAG_debug_code) {
4611 // The array construct code is only set for the global and natives
4612 // builtin Array functions which always have maps.
4613
4614 // Initial map for the builtin Array function should be a map.
4615 __ lw(t0, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4616 // Will both indicate a NULL and a Smi.
4617 __ SmiTst(t0, at);
4618 __ Assert(ne, kUnexpectedInitialMapForArrayFunction,
4619 at, Operand(zero_reg));
4620 __ GetObjectType(t0, t0, t1);
4621 __ Assert(eq, kUnexpectedInitialMapForArrayFunction,
4622 t1, Operand(MAP_TYPE));
4623
4624 // We should either have undefined in a2 or a valid AllocationSite
4625 __ AssertUndefinedOrAllocationSite(a2, t0);
4626 }
4627
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004628 // Enter the context of the Array function.
4629 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4630
4631 Label subclassing;
4632 __ Branch(&subclassing, ne, a1, Operand(a3));
4633
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004634 Label no_info;
4635 // Get the elements kind and case on that.
4636 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4637 __ Branch(&no_info, eq, a2, Operand(at));
4638
4639 __ lw(a3, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4640 __ SmiUntag(a3);
4641 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4642 __ And(a3, a3, Operand(AllocationSite::ElementsKindBits::kMask));
4643 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4644
4645 __ bind(&no_info);
4646 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004647
4648 // Subclassing.
4649 __ bind(&subclassing);
4650 switch (argument_count()) {
4651 case ANY:
4652 case MORE_THAN_ONE:
Ben Murdoch097c5b22016-05-18 11:27:45 +01004653 __ Lsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004654 __ sw(a1, MemOperand(at));
4655 __ li(at, Operand(3));
4656 __ addu(a0, a0, at);
4657 break;
4658 case NONE:
4659 __ sw(a1, MemOperand(sp, 0 * kPointerSize));
4660 __ li(a0, Operand(3));
4661 break;
4662 case ONE:
4663 __ sw(a1, MemOperand(sp, 1 * kPointerSize));
4664 __ li(a0, Operand(4));
4665 break;
4666 }
4667 __ Push(a3, a2);
4668 __ JumpToExternalReference(ExternalReference(Runtime::kNewArray, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004669}
4670
4671
4672void InternalArrayConstructorStub::GenerateCase(
4673 MacroAssembler* masm, ElementsKind kind) {
4674
4675 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4676 __ TailCallStub(&stub0, lo, a0, Operand(1));
4677
4678 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4679 __ TailCallStub(&stubN, hi, a0, Operand(1));
4680
4681 if (IsFastPackedElementsKind(kind)) {
4682 // We might need to create a holey array
4683 // look at the first argument.
4684 __ lw(at, MemOperand(sp, 0));
4685
4686 InternalArraySingleArgumentConstructorStub
4687 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4688 __ TailCallStub(&stub1_holey, ne, at, Operand(zero_reg));
4689 }
4690
4691 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4692 __ TailCallStub(&stub1);
4693}
4694
4695
4696void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4697 // ----------- S t a t e -------------
4698 // -- a0 : argc
4699 // -- a1 : constructor
4700 // -- sp[0] : return address
4701 // -- sp[4] : last argument
4702 // -----------------------------------
4703
4704 if (FLAG_debug_code) {
4705 // The array construct code is only set for the global and natives
4706 // builtin Array functions which always have maps.
4707
4708 // Initial map for the builtin Array function should be a map.
4709 __ lw(a3, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4710 // Will both indicate a NULL and a Smi.
4711 __ SmiTst(a3, at);
4712 __ Assert(ne, kUnexpectedInitialMapForArrayFunction,
4713 at, Operand(zero_reg));
4714 __ GetObjectType(a3, a3, t0);
4715 __ Assert(eq, kUnexpectedInitialMapForArrayFunction,
4716 t0, Operand(MAP_TYPE));
4717 }
4718
4719 // Figure out the right elements kind.
4720 __ lw(a3, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4721
4722 // Load the map's "bit field 2" into a3. We only need the first byte,
4723 // but the following bit field extraction takes care of that anyway.
4724 __ lbu(a3, FieldMemOperand(a3, Map::kBitField2Offset));
4725 // Retrieve elements_kind from bit field 2.
4726 __ DecodeField<Map::ElementsKindBits>(a3);
4727
4728 if (FLAG_debug_code) {
4729 Label done;
4730 __ Branch(&done, eq, a3, Operand(FAST_ELEMENTS));
4731 __ Assert(
4732 eq, kInvalidElementsKindForInternalArrayOrInternalPackedArray,
4733 a3, Operand(FAST_HOLEY_ELEMENTS));
4734 __ bind(&done);
4735 }
4736
4737 Label fast_elements_case;
4738 __ Branch(&fast_elements_case, eq, a3, Operand(FAST_ELEMENTS));
4739 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4740
4741 __ bind(&fast_elements_case);
4742 GenerateCase(masm, FAST_ELEMENTS);
4743}
4744
4745
Ben Murdoch097c5b22016-05-18 11:27:45 +01004746void FastNewObjectStub::Generate(MacroAssembler* masm) {
4747 // ----------- S t a t e -------------
4748 // -- a1 : target
4749 // -- a3 : new target
4750 // -- cp : context
4751 // -- ra : return address
4752 // -----------------------------------
4753 __ AssertFunction(a1);
4754 __ AssertReceiver(a3);
4755
4756 // Verify that the new target is a JSFunction.
4757 Label new_object;
4758 __ GetObjectType(a3, a2, a2);
4759 __ Branch(&new_object, ne, a2, Operand(JS_FUNCTION_TYPE));
4760
4761 // Load the initial map and verify that it's in fact a map.
4762 __ lw(a2, FieldMemOperand(a3, JSFunction::kPrototypeOrInitialMapOffset));
4763 __ JumpIfSmi(a2, &new_object);
4764 __ GetObjectType(a2, a0, a0);
4765 __ Branch(&new_object, ne, a0, Operand(MAP_TYPE));
4766
4767 // Fall back to runtime if the target differs from the new target's
4768 // initial map constructor.
4769 __ lw(a0, FieldMemOperand(a2, Map::kConstructorOrBackPointerOffset));
4770 __ Branch(&new_object, ne, a0, Operand(a1));
4771
4772 // Allocate the JSObject on the heap.
4773 Label allocate, done_allocate;
4774 __ lbu(t0, FieldMemOperand(a2, Map::kInstanceSizeOffset));
4775 __ Allocate(t0, v0, t1, a0, &allocate, SIZE_IN_WORDS);
4776 __ bind(&done_allocate);
4777
4778 // Initialize the JSObject fields.
4779 __ sw(a2, MemOperand(v0, JSObject::kMapOffset));
4780 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
4781 __ sw(a3, MemOperand(v0, JSObject::kPropertiesOffset));
4782 __ sw(a3, MemOperand(v0, JSObject::kElementsOffset));
4783 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
4784 __ Addu(a1, v0, Operand(JSObject::kHeaderSize));
4785
4786 // ----------- S t a t e -------------
4787 // -- v0 : result (untagged)
4788 // -- a1 : result fields (untagged)
4789 // -- t1 : result end (untagged)
4790 // -- a2 : initial map
4791 // -- cp : context
4792 // -- ra : return address
4793 // -----------------------------------
4794
4795 // Perform in-object slack tracking if requested.
4796 Label slack_tracking;
4797 STATIC_ASSERT(Map::kNoSlackTracking == 0);
4798 __ lw(a3, FieldMemOperand(a2, Map::kBitField3Offset));
4799 __ And(at, a3, Operand(Map::ConstructionCounter::kMask));
4800 __ Branch(USE_DELAY_SLOT, &slack_tracking, ne, at, Operand(0));
4801 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex); // In delay slot.
4802 {
4803 // Initialize all in-object fields with undefined.
4804 __ InitializeFieldsWithFiller(a1, t1, a0);
4805
4806 // Add the object tag to make the JSObject real.
4807 STATIC_ASSERT(kHeapObjectTag == 1);
4808 __ Ret(USE_DELAY_SLOT);
4809 __ Addu(v0, v0, Operand(kHeapObjectTag)); // In delay slot.
4810 }
4811 __ bind(&slack_tracking);
4812 {
4813 // Decrease generous allocation count.
4814 STATIC_ASSERT(Map::ConstructionCounter::kNext == 32);
4815 __ Subu(a3, a3, Operand(1 << Map::ConstructionCounter::kShift));
4816 __ sw(a3, FieldMemOperand(a2, Map::kBitField3Offset));
4817
4818 // Initialize the in-object fields with undefined.
4819 __ lbu(t0, FieldMemOperand(a2, Map::kUnusedPropertyFieldsOffset));
4820 __ sll(t0, t0, kPointerSizeLog2);
4821 __ subu(t0, t1, t0);
4822 __ InitializeFieldsWithFiller(a1, t0, a0);
4823
4824 // Initialize the remaining (reserved) fields with one pointer filler map.
4825 __ LoadRoot(a0, Heap::kOnePointerFillerMapRootIndex);
4826 __ InitializeFieldsWithFiller(a1, t1, a0);
4827
4828 // Check if we can finalize the instance size.
4829 Label finalize;
4830 STATIC_ASSERT(Map::kSlackTrackingCounterEnd == 1);
4831 __ And(a3, a3, Operand(Map::ConstructionCounter::kMask));
4832 __ Branch(USE_DELAY_SLOT, &finalize, eq, a3, Operand(zero_reg));
4833 STATIC_ASSERT(kHeapObjectTag == 1);
4834 __ Addu(v0, v0, Operand(kHeapObjectTag)); // In delay slot.
4835 __ Ret();
4836
4837 // Finalize the instance size.
4838 __ bind(&finalize);
4839 {
4840 FrameScope scope(masm, StackFrame::INTERNAL);
4841 __ Push(v0, a2);
4842 __ CallRuntime(Runtime::kFinalizeInstanceSize);
4843 __ Pop(v0);
4844 }
4845 __ Ret();
4846 }
4847
4848 // Fall back to %AllocateInNewSpace.
4849 __ bind(&allocate);
4850 {
4851 FrameScope scope(masm, StackFrame::INTERNAL);
4852 STATIC_ASSERT(kSmiTag == 0);
4853 STATIC_ASSERT(kSmiTagSize == 1);
4854 __ sll(t0, t0, kPointerSizeLog2 + kSmiTagSize);
4855 __ Push(a2, t0);
4856 __ CallRuntime(Runtime::kAllocateInNewSpace);
4857 __ Pop(a2);
4858 }
4859 STATIC_ASSERT(kHeapObjectTag == 1);
4860 __ Subu(v0, v0, Operand(kHeapObjectTag));
4861 __ lbu(t1, FieldMemOperand(a2, Map::kInstanceSizeOffset));
4862 __ Lsa(t1, v0, t1, kPointerSizeLog2);
4863 __ jmp(&done_allocate);
4864
4865 // Fall back to %NewObject.
4866 __ bind(&new_object);
4867 __ Push(a1, a3);
4868 __ TailCallRuntime(Runtime::kNewObject);
4869}
4870
4871
4872void FastNewRestParameterStub::Generate(MacroAssembler* masm) {
4873 // ----------- S t a t e -------------
4874 // -- a1 : function
4875 // -- cp : context
4876 // -- fp : frame pointer
4877 // -- ra : return address
4878 // -----------------------------------
4879 __ AssertFunction(a1);
4880
4881 // For Ignition we need to skip all possible handler/stub frames until
4882 // we reach the JavaScript frame for the function (similar to what the
4883 // runtime fallback implementation does). So make a2 point to that
4884 // JavaScript frame.
4885 {
4886 Label loop, loop_entry;
4887 __ Branch(USE_DELAY_SLOT, &loop_entry);
4888 __ mov(a2, fp); // In delay slot.
4889 __ bind(&loop);
4890 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
4891 __ bind(&loop_entry);
4892 __ lw(a3, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
4893 __ Branch(&loop, ne, a1, Operand(a3));
4894 }
4895
4896 // Check if we have rest parameters (only possible if we have an
4897 // arguments adaptor frame below the function frame).
4898 Label no_rest_parameters;
4899 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
4900 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
4901 __ Branch(&no_rest_parameters, ne, a3,
4902 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4903
4904 // Check if the arguments adaptor frame contains more arguments than
4905 // specified by the function's internal formal parameter count.
4906 Label rest_parameters;
4907 __ lw(a0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4908 __ lw(a1, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
4909 __ lw(a1,
4910 FieldMemOperand(a1, SharedFunctionInfo::kFormalParameterCountOffset));
4911 __ Subu(a0, a0, Operand(a1));
4912 __ Branch(&rest_parameters, gt, a0, Operand(zero_reg));
4913
4914 // Return an empty rest parameter array.
4915 __ bind(&no_rest_parameters);
4916 {
4917 // ----------- S t a t e -------------
4918 // -- cp : context
4919 // -- ra : return address
4920 // -----------------------------------
4921
4922 // Allocate an empty rest parameter array.
4923 Label allocate, done_allocate;
4924 __ Allocate(JSArray::kSize, v0, a0, a1, &allocate, TAG_OBJECT);
4925 __ bind(&done_allocate);
4926
4927 // Setup the rest parameter array in v0.
4928 __ LoadNativeContextSlot(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, a1);
4929 __ sw(a1, FieldMemOperand(v0, JSArray::kMapOffset));
4930 __ LoadRoot(a1, Heap::kEmptyFixedArrayRootIndex);
4931 __ sw(a1, FieldMemOperand(v0, JSArray::kPropertiesOffset));
4932 __ sw(a1, FieldMemOperand(v0, JSArray::kElementsOffset));
4933 __ Move(a1, Smi::FromInt(0));
4934 __ Ret(USE_DELAY_SLOT);
4935 __ sw(a1, FieldMemOperand(v0, JSArray::kLengthOffset)); // In delay slot
4936 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4937
4938 // Fall back to %AllocateInNewSpace.
4939 __ bind(&allocate);
4940 {
4941 FrameScope scope(masm, StackFrame::INTERNAL);
4942 __ Push(Smi::FromInt(JSArray::kSize));
4943 __ CallRuntime(Runtime::kAllocateInNewSpace);
4944 }
4945 __ jmp(&done_allocate);
4946 }
4947
4948 __ bind(&rest_parameters);
4949 {
4950 // Compute the pointer to the first rest parameter (skippping the receiver).
4951 __ Lsa(a2, a2, a0, kPointerSizeLog2 - 1);
4952 __ Addu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset -
4953 1 * kPointerSize));
4954
4955 // ----------- S t a t e -------------
4956 // -- cp : context
4957 // -- a0 : number of rest parameters (tagged)
4958 // -- a2 : pointer to first rest parameters
4959 // -- ra : return address
4960 // -----------------------------------
4961
4962 // Allocate space for the rest parameter array plus the backing store.
4963 Label allocate, done_allocate;
4964 __ li(a1, Operand(JSArray::kSize + FixedArray::kHeaderSize));
4965 __ Lsa(a1, a1, a0, kPointerSizeLog2 - 1);
4966 __ Allocate(a1, v0, a3, t0, &allocate, TAG_OBJECT);
4967 __ bind(&done_allocate);
4968
4969 // Setup the elements array in v0.
4970 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4971 __ sw(at, FieldMemOperand(v0, FixedArray::kMapOffset));
4972 __ sw(a0, FieldMemOperand(v0, FixedArray::kLengthOffset));
4973 __ Addu(a3, v0, Operand(FixedArray::kHeaderSize));
4974 {
4975 Label loop, done_loop;
4976 __ sll(at, a0, kPointerSizeLog2 - 1);
4977 __ Addu(a1, a3, at);
4978 __ bind(&loop);
4979 __ Branch(&done_loop, eq, a1, Operand(a3));
4980 __ lw(at, MemOperand(a2, 0 * kPointerSize));
4981 __ sw(at, FieldMemOperand(a3, 0 * kPointerSize));
4982 __ Subu(a2, a2, Operand(1 * kPointerSize));
4983 __ Addu(a3, a3, Operand(1 * kPointerSize));
4984 __ jmp(&loop);
4985 __ bind(&done_loop);
4986 }
4987
4988 // Setup the rest parameter array in a3.
4989 __ LoadNativeContextSlot(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, at);
4990 __ sw(at, FieldMemOperand(a3, JSArray::kMapOffset));
4991 __ LoadRoot(at, Heap::kEmptyFixedArrayRootIndex);
4992 __ sw(at, FieldMemOperand(a3, JSArray::kPropertiesOffset));
4993 __ sw(v0, FieldMemOperand(a3, JSArray::kElementsOffset));
4994 __ sw(a0, FieldMemOperand(a3, JSArray::kLengthOffset));
4995 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4996 __ Ret(USE_DELAY_SLOT);
4997 __ mov(v0, a3); // In delay slot
4998
4999 // Fall back to %AllocateInNewSpace.
5000 __ bind(&allocate);
5001 {
5002 FrameScope scope(masm, StackFrame::INTERNAL);
5003 __ SmiTag(a1);
5004 __ Push(a0, a2, a1);
5005 __ CallRuntime(Runtime::kAllocateInNewSpace);
5006 __ Pop(a0, a2);
5007 }
5008 __ jmp(&done_allocate);
5009 }
5010}
5011
5012
5013void FastNewSloppyArgumentsStub::Generate(MacroAssembler* masm) {
5014 // ----------- S t a t e -------------
5015 // -- a1 : function
5016 // -- cp : context
5017 // -- fp : frame pointer
5018 // -- ra : return address
5019 // -----------------------------------
5020 __ AssertFunction(a1);
5021
5022 // TODO(bmeurer): Cleanup to match the FastNewStrictArgumentsStub.
5023 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
5024 __ lw(a2,
5025 FieldMemOperand(a2, SharedFunctionInfo::kFormalParameterCountOffset));
5026 __ Lsa(a3, fp, a2, kPointerSizeLog2 - 1);
5027 __ Addu(a3, a3, Operand(StandardFrameConstants::kCallerSPOffset));
5028
5029 // a1 : function
5030 // a2 : number of parameters (tagged)
5031 // a3 : parameters pointer
5032 // Registers used over whole function:
5033 // t1 : arguments count (tagged)
5034 // t2 : mapped parameter count (tagged)
5035
5036 // Check if the calling frame is an arguments adaptor frame.
5037 Label adaptor_frame, try_allocate, runtime;
5038 __ lw(t0, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
5039 __ lw(a0, MemOperand(t0, StandardFrameConstants::kContextOffset));
5040 __ Branch(&adaptor_frame, eq, a0,
5041 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5042
5043 // No adaptor, parameter count = argument count.
5044 __ mov(t1, a2);
5045 __ Branch(USE_DELAY_SLOT, &try_allocate);
5046 __ mov(t2, a2); // In delay slot.
5047
5048 // We have an adaptor frame. Patch the parameters pointer.
5049 __ bind(&adaptor_frame);
5050 __ lw(t1, MemOperand(t0, ArgumentsAdaptorFrameConstants::kLengthOffset));
5051 __ Lsa(t0, t0, t1, 1);
5052 __ Addu(a3, t0, Operand(StandardFrameConstants::kCallerSPOffset));
5053
5054 // t1 = argument count (tagged)
5055 // t2 = parameter count (tagged)
5056 // Compute the mapped parameter count = min(t2, t1) in t2.
5057 __ mov(t2, a2);
5058 __ Branch(&try_allocate, le, t2, Operand(t1));
5059 __ mov(t2, t1);
5060
5061 __ bind(&try_allocate);
5062
5063 // Compute the sizes of backing store, parameter map, and arguments object.
5064 // 1. Parameter map, has 2 extra words containing context and backing store.
5065 const int kParameterMapHeaderSize =
5066 FixedArray::kHeaderSize + 2 * kPointerSize;
5067 // If there are no mapped parameters, we do not need the parameter_map.
5068 Label param_map_size;
5069 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
5070 __ Branch(USE_DELAY_SLOT, &param_map_size, eq, t2, Operand(zero_reg));
5071 __ mov(t5, zero_reg); // In delay slot: param map size = 0 when t2 == 0.
5072 __ sll(t5, t2, 1);
5073 __ addiu(t5, t5, kParameterMapHeaderSize);
5074 __ bind(&param_map_size);
5075
5076 // 2. Backing store.
5077 __ Lsa(t5, t5, t1, 1);
5078 __ Addu(t5, t5, Operand(FixedArray::kHeaderSize));
5079
5080 // 3. Arguments object.
5081 __ Addu(t5, t5, Operand(JSSloppyArgumentsObject::kSize));
5082
5083 // Do the allocation of all three objects in one go.
5084 __ Allocate(t5, v0, t5, t0, &runtime, TAG_OBJECT);
5085
5086 // v0 = address of new object(s) (tagged)
5087 // a2 = argument count (smi-tagged)
5088 // Get the arguments boilerplate from the current native context into t0.
5089 const int kNormalOffset =
5090 Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX);
5091 const int kAliasedOffset =
5092 Context::SlotOffset(Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX);
5093
5094 __ lw(t0, NativeContextMemOperand());
5095 Label skip2_ne, skip2_eq;
5096 __ Branch(&skip2_ne, ne, t2, Operand(zero_reg));
5097 __ lw(t0, MemOperand(t0, kNormalOffset));
5098 __ bind(&skip2_ne);
5099
5100 __ Branch(&skip2_eq, eq, t2, Operand(zero_reg));
5101 __ lw(t0, MemOperand(t0, kAliasedOffset));
5102 __ bind(&skip2_eq);
5103
5104 // v0 = address of new object (tagged)
5105 // a2 = argument count (smi-tagged)
5106 // t0 = address of arguments map (tagged)
5107 // t2 = mapped parameter count (tagged)
5108 __ sw(t0, FieldMemOperand(v0, JSObject::kMapOffset));
5109 __ LoadRoot(t5, Heap::kEmptyFixedArrayRootIndex);
5110 __ sw(t5, FieldMemOperand(v0, JSObject::kPropertiesOffset));
5111 __ sw(t5, FieldMemOperand(v0, JSObject::kElementsOffset));
5112
5113 // Set up the callee in-object property.
5114 __ AssertNotSmi(a1);
5115 __ sw(a1, FieldMemOperand(v0, JSSloppyArgumentsObject::kCalleeOffset));
5116
5117 // Use the length (smi tagged) and set that as an in-object property too.
5118 __ AssertSmi(t1);
5119 __ sw(t1, FieldMemOperand(v0, JSSloppyArgumentsObject::kLengthOffset));
5120
5121 // Set up the elements pointer in the allocated arguments object.
5122 // If we allocated a parameter map, t0 will point there, otherwise
5123 // it will point to the backing store.
5124 __ Addu(t0, v0, Operand(JSSloppyArgumentsObject::kSize));
5125 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
5126
5127 // v0 = address of new object (tagged)
5128 // a2 = argument count (tagged)
5129 // t0 = address of parameter map or backing store (tagged)
5130 // t2 = mapped parameter count (tagged)
5131 // Initialize parameter map. If there are no mapped arguments, we're done.
5132 Label skip_parameter_map;
5133 Label skip3;
5134 __ Branch(&skip3, ne, t2, Operand(Smi::FromInt(0)));
5135 // Move backing store address to a1, because it is
5136 // expected there when filling in the unmapped arguments.
5137 __ mov(a1, t0);
5138 __ bind(&skip3);
5139
5140 __ Branch(&skip_parameter_map, eq, t2, Operand(Smi::FromInt(0)));
5141
5142 __ LoadRoot(t1, Heap::kSloppyArgumentsElementsMapRootIndex);
5143 __ sw(t1, FieldMemOperand(t0, FixedArray::kMapOffset));
5144 __ Addu(t1, t2, Operand(Smi::FromInt(2)));
5145 __ sw(t1, FieldMemOperand(t0, FixedArray::kLengthOffset));
5146 __ sw(cp, FieldMemOperand(t0, FixedArray::kHeaderSize + 0 * kPointerSize));
5147 __ Lsa(t1, t0, t2, 1);
5148 __ Addu(t1, t1, Operand(kParameterMapHeaderSize));
5149 __ sw(t1, FieldMemOperand(t0, FixedArray::kHeaderSize + 1 * kPointerSize));
5150
5151 // Copy the parameter slots and the holes in the arguments.
5152 // We need to fill in mapped_parameter_count slots. They index the context,
5153 // where parameters are stored in reverse order, at
5154 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
5155 // The mapped parameter thus need to get indices
5156 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
5157 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
5158 // We loop from right to left.
5159 Label parameters_loop, parameters_test;
5160 __ mov(t1, t2);
5161 __ Addu(t5, a2, Operand(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
5162 __ Subu(t5, t5, Operand(t2));
5163 __ LoadRoot(t3, Heap::kTheHoleValueRootIndex);
5164 __ Lsa(a1, t0, t1, 1);
5165 __ Addu(a1, a1, Operand(kParameterMapHeaderSize));
5166
5167 // a1 = address of backing store (tagged)
5168 // t0 = address of parameter map (tagged)
5169 // a0 = temporary scratch (a.o., for address calculation)
5170 // t1 = loop variable (tagged)
5171 // t3 = the hole value
5172 __ jmp(&parameters_test);
5173
5174 __ bind(&parameters_loop);
5175 __ Subu(t1, t1, Operand(Smi::FromInt(1)));
5176 __ sll(a0, t1, 1);
5177 __ Addu(a0, a0, Operand(kParameterMapHeaderSize - kHeapObjectTag));
5178 __ Addu(t6, t0, a0);
5179 __ sw(t5, MemOperand(t6));
5180 __ Subu(a0, a0, Operand(kParameterMapHeaderSize - FixedArray::kHeaderSize));
5181 __ Addu(t6, a1, a0);
5182 __ sw(t3, MemOperand(t6));
5183 __ Addu(t5, t5, Operand(Smi::FromInt(1)));
5184 __ bind(&parameters_test);
5185 __ Branch(&parameters_loop, ne, t1, Operand(Smi::FromInt(0)));
5186
5187 // t1 = argument count (tagged).
5188 __ lw(t1, FieldMemOperand(v0, JSSloppyArgumentsObject::kLengthOffset));
5189
5190 __ bind(&skip_parameter_map);
5191 // v0 = address of new object (tagged)
5192 // a1 = address of backing store (tagged)
5193 // t1 = argument count (tagged)
5194 // t2 = mapped parameter count (tagged)
5195 // t5 = scratch
5196 // Copy arguments header and remaining slots (if there are any).
5197 __ LoadRoot(t5, Heap::kFixedArrayMapRootIndex);
5198 __ sw(t5, FieldMemOperand(a1, FixedArray::kMapOffset));
5199 __ sw(t1, FieldMemOperand(a1, FixedArray::kLengthOffset));
5200
5201 Label arguments_loop, arguments_test;
5202 __ sll(t6, t2, 1);
5203 __ Subu(a3, a3, Operand(t6));
5204 __ jmp(&arguments_test);
5205
5206 __ bind(&arguments_loop);
5207 __ Subu(a3, a3, Operand(kPointerSize));
5208 __ lw(t0, MemOperand(a3, 0));
5209 __ Lsa(t5, a1, t2, 1);
5210 __ sw(t0, FieldMemOperand(t5, FixedArray::kHeaderSize));
5211 __ Addu(t2, t2, Operand(Smi::FromInt(1)));
5212
5213 __ bind(&arguments_test);
5214 __ Branch(&arguments_loop, lt, t2, Operand(t1));
5215
5216 // Return.
5217 __ Ret();
5218
5219 // Do the runtime call to allocate the arguments object.
5220 // t1 = argument count (tagged)
5221 __ bind(&runtime);
5222 __ Push(a1, a3, t1);
5223 __ TailCallRuntime(Runtime::kNewSloppyArguments);
5224}
5225
5226
5227void FastNewStrictArgumentsStub::Generate(MacroAssembler* masm) {
5228 // ----------- S t a t e -------------
5229 // -- a1 : function
5230 // -- cp : context
5231 // -- fp : frame pointer
5232 // -- ra : return address
5233 // -----------------------------------
5234 __ AssertFunction(a1);
5235
5236 // For Ignition we need to skip all possible handler/stub frames until
5237 // we reach the JavaScript frame for the function (similar to what the
5238 // runtime fallback implementation does). So make a2 point to that
5239 // JavaScript frame.
5240 {
5241 Label loop, loop_entry;
5242 __ Branch(USE_DELAY_SLOT, &loop_entry);
5243 __ mov(a2, fp); // In delay slot.
5244 __ bind(&loop);
5245 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
5246 __ bind(&loop_entry);
5247 __ lw(a3, MemOperand(a2, StandardFrameConstants::kMarkerOffset));
5248 __ Branch(&loop, ne, a1, Operand(a3));
5249 }
5250
5251 // Check if we have an arguments adaptor frame below the function frame.
5252 Label arguments_adaptor, arguments_done;
5253 __ lw(a3, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
5254 __ lw(a0, MemOperand(a3, StandardFrameConstants::kContextOffset));
5255 __ Branch(&arguments_adaptor, eq, a0,
5256 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5257 {
5258 __ lw(a1, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
5259 __ lw(a0,
5260 FieldMemOperand(a1, SharedFunctionInfo::kFormalParameterCountOffset));
5261 __ Lsa(a2, a2, a0, kPointerSizeLog2 - 1);
5262 __ Addu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset -
5263 1 * kPointerSize));
5264 }
5265 __ Branch(&arguments_done);
5266 __ bind(&arguments_adaptor);
5267 {
5268 __ lw(a0, MemOperand(a3, ArgumentsAdaptorFrameConstants::kLengthOffset));
5269 __ Lsa(a2, a3, a0, kPointerSizeLog2 - 1);
5270 __ Addu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset -
5271 1 * kPointerSize));
5272 }
5273 __ bind(&arguments_done);
5274
5275 // ----------- S t a t e -------------
5276 // -- cp : context
5277 // -- a0 : number of rest parameters (tagged)
5278 // -- a2 : pointer to first rest parameters
5279 // -- ra : return address
5280 // -----------------------------------
5281
5282 // Allocate space for the strict arguments object plus the backing store.
5283 Label allocate, done_allocate;
5284 __ li(a1, Operand(JSStrictArgumentsObject::kSize + FixedArray::kHeaderSize));
5285 __ Lsa(a1, a1, a0, kPointerSizeLog2 - 1);
5286 __ Allocate(a1, v0, a3, t0, &allocate, TAG_OBJECT);
5287 __ bind(&done_allocate);
5288
5289 // Setup the elements array in v0.
5290 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
5291 __ sw(at, FieldMemOperand(v0, FixedArray::kMapOffset));
5292 __ sw(a0, FieldMemOperand(v0, FixedArray::kLengthOffset));
5293 __ Addu(a3, v0, Operand(FixedArray::kHeaderSize));
5294 {
5295 Label loop, done_loop;
5296 __ sll(at, a0, kPointerSizeLog2 - 1);
5297 __ Addu(a1, a3, at);
5298 __ bind(&loop);
5299 __ Branch(&done_loop, eq, a1, Operand(a3));
5300 __ lw(at, MemOperand(a2, 0 * kPointerSize));
5301 __ sw(at, FieldMemOperand(a3, 0 * kPointerSize));
5302 __ Subu(a2, a2, Operand(1 * kPointerSize));
5303 __ Addu(a3, a3, Operand(1 * kPointerSize));
5304 __ Branch(&loop);
5305 __ bind(&done_loop);
5306 }
5307
5308 // Setup the strict arguments object in a3.
5309 __ LoadNativeContextSlot(Context::STRICT_ARGUMENTS_MAP_INDEX, at);
5310 __ sw(at, FieldMemOperand(a3, JSStrictArgumentsObject::kMapOffset));
5311 __ LoadRoot(at, Heap::kEmptyFixedArrayRootIndex);
5312 __ sw(at, FieldMemOperand(a3, JSStrictArgumentsObject::kPropertiesOffset));
5313 __ sw(v0, FieldMemOperand(a3, JSStrictArgumentsObject::kElementsOffset));
5314 __ sw(a0, FieldMemOperand(a3, JSStrictArgumentsObject::kLengthOffset));
5315 STATIC_ASSERT(JSStrictArgumentsObject::kSize == 4 * kPointerSize);
5316 __ Ret(USE_DELAY_SLOT);
5317 __ mov(v0, a3); // In delay slot
5318
5319 // Fall back to %AllocateInNewSpace.
5320 __ bind(&allocate);
5321 {
5322 FrameScope scope(masm, StackFrame::INTERNAL);
5323 __ SmiTag(a1);
5324 __ Push(a0, a2, a1);
5325 __ CallRuntime(Runtime::kAllocateInNewSpace);
5326 __ Pop(a0, a2);
5327 }
5328 __ jmp(&done_allocate);
5329}
5330
5331
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005332void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
5333 Register context_reg = cp;
5334 Register slot_reg = a2;
5335 Register result_reg = v0;
5336 Label slow_case;
5337
5338 // Go up context chain to the script context.
5339 for (int i = 0; i < depth(); ++i) {
5340 __ lw(result_reg, ContextMemOperand(context_reg, Context::PREVIOUS_INDEX));
5341 context_reg = result_reg;
5342 }
5343
5344 // Load the PropertyCell value at the specified slot.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005345 __ Lsa(at, context_reg, slot_reg, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005346 __ lw(result_reg, ContextMemOperand(at, 0));
5347 __ lw(result_reg, FieldMemOperand(result_reg, PropertyCell::kValueOffset));
5348
5349 // Check that value is not the_hole.
5350 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
5351 __ Branch(&slow_case, eq, result_reg, Operand(at));
5352 __ Ret();
5353
5354 // Fallback to the runtime.
5355 __ bind(&slow_case);
5356 __ SmiTag(slot_reg);
5357 __ Push(slot_reg);
5358 __ TailCallRuntime(Runtime::kLoadGlobalViaContext);
5359}
5360
5361
5362void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
5363 Register context_reg = cp;
5364 Register slot_reg = a2;
5365 Register value_reg = a0;
5366 Register cell_reg = t0;
5367 Register cell_value_reg = t1;
5368 Register cell_details_reg = t2;
5369 Label fast_heapobject_case, fast_smi_case, slow_case;
5370
5371 if (FLAG_debug_code) {
5372 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
5373 __ Check(ne, kUnexpectedValue, value_reg, Operand(at));
5374 }
5375
5376 // Go up context chain to the script context.
5377 for (int i = 0; i < depth(); ++i) {
5378 __ lw(cell_reg, ContextMemOperand(context_reg, Context::PREVIOUS_INDEX));
5379 context_reg = cell_reg;
5380 }
5381
5382 // Load the PropertyCell at the specified slot.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005383 __ Lsa(at, context_reg, slot_reg, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005384 __ lw(cell_reg, ContextMemOperand(at, 0));
5385
5386 // Load PropertyDetails for the cell (actually only the cell_type and kind).
5387 __ lw(cell_details_reg,
5388 FieldMemOperand(cell_reg, PropertyCell::kDetailsOffset));
5389 __ SmiUntag(cell_details_reg);
5390 __ And(cell_details_reg, cell_details_reg,
5391 PropertyDetails::PropertyCellTypeField::kMask |
5392 PropertyDetails::KindField::kMask |
5393 PropertyDetails::kAttributesReadOnlyMask);
5394
5395 // Check if PropertyCell holds mutable data.
5396 Label not_mutable_data;
5397 __ Branch(&not_mutable_data, ne, cell_details_reg,
5398 Operand(PropertyDetails::PropertyCellTypeField::encode(
5399 PropertyCellType::kMutable) |
5400 PropertyDetails::KindField::encode(kData)));
5401 __ JumpIfSmi(value_reg, &fast_smi_case);
5402 __ bind(&fast_heapobject_case);
5403 __ sw(value_reg, FieldMemOperand(cell_reg, PropertyCell::kValueOffset));
5404 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
5405 cell_details_reg, kRAHasNotBeenSaved, kDontSaveFPRegs,
5406 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
5407 // RecordWriteField clobbers the value register, so we need to reload.
5408 __ Ret(USE_DELAY_SLOT);
5409 __ lw(value_reg, FieldMemOperand(cell_reg, PropertyCell::kValueOffset));
5410 __ bind(&not_mutable_data);
5411
5412 // Check if PropertyCell value matches the new value (relevant for Constant,
5413 // ConstantType and Undefined cells).
5414 Label not_same_value;
5415 __ lw(cell_value_reg, FieldMemOperand(cell_reg, PropertyCell::kValueOffset));
5416 __ Branch(&not_same_value, ne, value_reg, Operand(cell_value_reg));
5417 // Make sure the PropertyCell is not marked READ_ONLY.
5418 __ And(at, cell_details_reg, PropertyDetails::kAttributesReadOnlyMask);
5419 __ Branch(&slow_case, ne, at, Operand(zero_reg));
5420 if (FLAG_debug_code) {
5421 Label done;
5422 // This can only be true for Constant, ConstantType and Undefined cells,
5423 // because we never store the_hole via this stub.
5424 __ Branch(&done, eq, cell_details_reg,
5425 Operand(PropertyDetails::PropertyCellTypeField::encode(
5426 PropertyCellType::kConstant) |
5427 PropertyDetails::KindField::encode(kData)));
5428 __ Branch(&done, eq, cell_details_reg,
5429 Operand(PropertyDetails::PropertyCellTypeField::encode(
5430 PropertyCellType::kConstantType) |
5431 PropertyDetails::KindField::encode(kData)));
5432 __ Check(eq, kUnexpectedValue, cell_details_reg,
5433 Operand(PropertyDetails::PropertyCellTypeField::encode(
5434 PropertyCellType::kUndefined) |
5435 PropertyDetails::KindField::encode(kData)));
5436 __ bind(&done);
5437 }
5438 __ Ret();
5439 __ bind(&not_same_value);
5440
5441 // Check if PropertyCell contains data with constant type (and is not
5442 // READ_ONLY).
5443 __ Branch(&slow_case, ne, cell_details_reg,
5444 Operand(PropertyDetails::PropertyCellTypeField::encode(
5445 PropertyCellType::kConstantType) |
5446 PropertyDetails::KindField::encode(kData)));
5447
5448 // Now either both old and new values must be SMIs or both must be heap
5449 // objects with same map.
5450 Label value_is_heap_object;
5451 __ JumpIfNotSmi(value_reg, &value_is_heap_object);
5452 __ JumpIfNotSmi(cell_value_reg, &slow_case);
5453 // Old and new values are SMIs, no need for a write barrier here.
5454 __ bind(&fast_smi_case);
5455 __ Ret(USE_DELAY_SLOT);
5456 __ sw(value_reg, FieldMemOperand(cell_reg, PropertyCell::kValueOffset));
5457 __ bind(&value_is_heap_object);
5458 __ JumpIfSmi(cell_value_reg, &slow_case);
5459 Register cell_value_map_reg = cell_value_reg;
5460 __ lw(cell_value_map_reg,
5461 FieldMemOperand(cell_value_reg, HeapObject::kMapOffset));
5462 __ Branch(&fast_heapobject_case, eq, cell_value_map_reg,
5463 FieldMemOperand(value_reg, HeapObject::kMapOffset));
5464
5465 // Fallback to the runtime.
5466 __ bind(&slow_case);
5467 __ SmiTag(slot_reg);
5468 __ Push(slot_reg, value_reg);
5469 __ TailCallRuntime(is_strict(language_mode())
5470 ? Runtime::kStoreGlobalViaContext_Strict
5471 : Runtime::kStoreGlobalViaContext_Sloppy);
5472}
5473
5474
5475static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
5476 return ref0.address() - ref1.address();
5477}
5478
5479
5480// Calls an API function. Allocates HandleScope, extracts returned value
5481// from handle and propagates exceptions. Restores context. stack_space
5482// - space to be unwound on exit (includes the call JS arguments space and
5483// the additional space allocated for the fast call).
5484static void CallApiFunctionAndReturn(
5485 MacroAssembler* masm, Register function_address,
5486 ExternalReference thunk_ref, int stack_space, int32_t stack_space_offset,
5487 MemOperand return_value_operand, MemOperand* context_restore_operand) {
5488 Isolate* isolate = masm->isolate();
5489 ExternalReference next_address =
5490 ExternalReference::handle_scope_next_address(isolate);
5491 const int kNextOffset = 0;
5492 const int kLimitOffset = AddressOffset(
5493 ExternalReference::handle_scope_limit_address(isolate), next_address);
5494 const int kLevelOffset = AddressOffset(
5495 ExternalReference::handle_scope_level_address(isolate), next_address);
5496
5497 DCHECK(function_address.is(a1) || function_address.is(a2));
5498
5499 Label profiler_disabled;
5500 Label end_profiler_check;
5501 __ li(t9, Operand(ExternalReference::is_profiling_address(isolate)));
5502 __ lb(t9, MemOperand(t9, 0));
5503 __ Branch(&profiler_disabled, eq, t9, Operand(zero_reg));
5504
5505 // Additional parameter is the address of the actual callback.
5506 __ li(t9, Operand(thunk_ref));
5507 __ jmp(&end_profiler_check);
5508
5509 __ bind(&profiler_disabled);
5510 __ mov(t9, function_address);
5511 __ bind(&end_profiler_check);
5512
5513 // Allocate HandleScope in callee-save registers.
5514 __ li(s3, Operand(next_address));
5515 __ lw(s0, MemOperand(s3, kNextOffset));
5516 __ lw(s1, MemOperand(s3, kLimitOffset));
5517 __ lw(s2, MemOperand(s3, kLevelOffset));
5518 __ Addu(s2, s2, Operand(1));
5519 __ sw(s2, MemOperand(s3, kLevelOffset));
5520
5521 if (FLAG_log_timer_events) {
5522 FrameScope frame(masm, StackFrame::MANUAL);
5523 __ PushSafepointRegisters();
5524 __ PrepareCallCFunction(1, a0);
5525 __ li(a0, Operand(ExternalReference::isolate_address(isolate)));
5526 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5527 1);
5528 __ PopSafepointRegisters();
5529 }
5530
5531 // Native call returns to the DirectCEntry stub which redirects to the
5532 // return address pushed on stack (could have moved after GC).
5533 // DirectCEntry stub itself is generated early and never moves.
5534 DirectCEntryStub stub(isolate);
5535 stub.GenerateCall(masm, t9);
5536
5537 if (FLAG_log_timer_events) {
5538 FrameScope frame(masm, StackFrame::MANUAL);
5539 __ PushSafepointRegisters();
5540 __ PrepareCallCFunction(1, a0);
5541 __ li(a0, Operand(ExternalReference::isolate_address(isolate)));
5542 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5543 1);
5544 __ PopSafepointRegisters();
5545 }
5546
5547 Label promote_scheduled_exception;
5548 Label delete_allocated_handles;
5549 Label leave_exit_frame;
5550 Label return_value_loaded;
5551
5552 // Load value from ReturnValue.
5553 __ lw(v0, return_value_operand);
5554 __ bind(&return_value_loaded);
5555
5556 // No more valid handles (the result handle was the last one). Restore
5557 // previous handle scope.
5558 __ sw(s0, MemOperand(s3, kNextOffset));
5559 if (__ emit_debug_code()) {
5560 __ lw(a1, MemOperand(s3, kLevelOffset));
5561 __ Check(eq, kUnexpectedLevelAfterReturnFromApiCall, a1, Operand(s2));
5562 }
5563 __ Subu(s2, s2, Operand(1));
5564 __ sw(s2, MemOperand(s3, kLevelOffset));
5565 __ lw(at, MemOperand(s3, kLimitOffset));
5566 __ Branch(&delete_allocated_handles, ne, s1, Operand(at));
5567
5568 // Leave the API exit frame.
5569 __ bind(&leave_exit_frame);
5570
5571 bool restore_context = context_restore_operand != NULL;
5572 if (restore_context) {
5573 __ lw(cp, *context_restore_operand);
5574 }
5575 if (stack_space_offset != kInvalidStackOffset) {
5576 // ExitFrame contains four MIPS argument slots after DirectCEntryStub call
5577 // so this must be accounted for.
5578 __ lw(s0, MemOperand(sp, stack_space_offset + kCArgsSlotsSize));
5579 } else {
5580 __ li(s0, Operand(stack_space));
5581 }
5582 __ LeaveExitFrame(false, s0, !restore_context, NO_EMIT_RETURN,
5583 stack_space_offset != kInvalidStackOffset);
5584
5585 // Check if the function scheduled an exception.
5586 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
5587 __ li(at, Operand(ExternalReference::scheduled_exception_address(isolate)));
5588 __ lw(t1, MemOperand(at));
5589 __ Branch(&promote_scheduled_exception, ne, t0, Operand(t1));
5590
5591 __ Ret();
5592
5593 // Re-throw by promoting a scheduled exception.
5594 __ bind(&promote_scheduled_exception);
5595 __ TailCallRuntime(Runtime::kPromoteScheduledException);
5596
5597 // HandleScope limit has changed. Delete allocated extensions.
5598 __ bind(&delete_allocated_handles);
5599 __ sw(s1, MemOperand(s3, kLimitOffset));
5600 __ mov(s0, v0);
5601 __ mov(a0, v0);
5602 __ PrepareCallCFunction(1, s1);
5603 __ li(a0, Operand(ExternalReference::isolate_address(isolate)));
5604 __ CallCFunction(ExternalReference::delete_handle_scope_extensions(isolate),
5605 1);
5606 __ mov(v0, s0);
5607 __ jmp(&leave_exit_frame);
5608}
5609
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005610static void CallApiFunctionStubHelper(MacroAssembler* masm,
5611 const ParameterCount& argc,
5612 bool return_first_arg,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005613 bool call_data_undefined, bool is_lazy) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005614 // ----------- S t a t e -------------
5615 // -- a0 : callee
5616 // -- t0 : call_data
5617 // -- a2 : holder
5618 // -- a1 : api_function_address
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005619 // -- a3 : number of arguments if argc is a register
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005620 // -- cp : context
5621 // --
5622 // -- sp[0] : last argument
5623 // -- ...
5624 // -- sp[(argc - 1)* 4] : first argument
5625 // -- sp[argc * 4] : receiver
5626 // -----------------------------------
5627
5628 Register callee = a0;
5629 Register call_data = t0;
5630 Register holder = a2;
5631 Register api_function_address = a1;
5632 Register context = cp;
5633
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005634 typedef FunctionCallbackArguments FCA;
5635
5636 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5637 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5638 STATIC_ASSERT(FCA::kDataIndex == 4);
5639 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5640 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5641 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5642 STATIC_ASSERT(FCA::kHolderIndex == 0);
5643 STATIC_ASSERT(FCA::kArgsLength == 7);
5644
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005645 DCHECK(argc.is_immediate() || a3.is(argc.reg()));
5646
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005647 // Save context, callee and call data.
5648 __ Push(context, callee, call_data);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005649 if (!is_lazy) {
5650 // Load context from callee.
5651 __ lw(context, FieldMemOperand(callee, JSFunction::kContextOffset));
5652 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005653
5654 Register scratch = call_data;
5655 if (!call_data_undefined) {
5656 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5657 }
5658 // Push return value and default return value.
5659 __ Push(scratch, scratch);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005660 __ li(scratch, Operand(ExternalReference::isolate_address(masm->isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005661 // Push isolate and holder.
5662 __ Push(scratch, holder);
5663
5664 // Prepare arguments.
5665 __ mov(scratch, sp);
5666
5667 // Allocate the v8::Arguments structure in the arguments' space since
5668 // it's not controlled by GC.
5669 const int kApiStackSpace = 4;
5670
5671 FrameScope frame_scope(masm, StackFrame::MANUAL);
5672 __ EnterExitFrame(false, kApiStackSpace);
5673
5674 DCHECK(!api_function_address.is(a0) && !scratch.is(a0));
5675 // a0 = FunctionCallbackInfo&
5676 // Arguments is after the return address.
5677 __ Addu(a0, sp, Operand(1 * kPointerSize));
5678 // FunctionCallbackInfo::implicit_args_
5679 __ sw(scratch, MemOperand(a0, 0 * kPointerSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005680 if (argc.is_immediate()) {
5681 // FunctionCallbackInfo::values_
5682 __ Addu(at, scratch,
5683 Operand((FCA::kArgsLength - 1 + argc.immediate()) * kPointerSize));
5684 __ sw(at, MemOperand(a0, 1 * kPointerSize));
5685 // FunctionCallbackInfo::length_ = argc
5686 __ li(at, Operand(argc.immediate()));
5687 __ sw(at, MemOperand(a0, 2 * kPointerSize));
5688 // FunctionCallbackInfo::is_construct_call_ = 0
5689 __ sw(zero_reg, MemOperand(a0, 3 * kPointerSize));
5690 } else {
5691 // FunctionCallbackInfo::values_
5692 __ sll(at, argc.reg(), kPointerSizeLog2);
5693 __ Addu(at, at, scratch);
5694 __ Addu(at, at, Operand((FCA::kArgsLength - 1) * kPointerSize));
5695 __ sw(at, MemOperand(a0, 1 * kPointerSize));
5696 // FunctionCallbackInfo::length_ = argc
5697 __ sw(argc.reg(), MemOperand(a0, 2 * kPointerSize));
5698 // FunctionCallbackInfo::is_construct_call_
5699 __ Addu(argc.reg(), argc.reg(), Operand(FCA::kArgsLength + 1));
5700 __ sll(at, argc.reg(), kPointerSizeLog2);
5701 __ sw(at, MemOperand(a0, 3 * kPointerSize));
5702 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005703
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005704 ExternalReference thunk_ref =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005705 ExternalReference::invoke_function_callback(masm->isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005706
5707 AllowExternalCallThatCantCauseGC scope(masm);
5708 MemOperand context_restore_operand(
5709 fp, (2 + FCA::kContextSaveIndex) * kPointerSize);
5710 // Stores return the first js argument.
5711 int return_value_offset = 0;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005712 if (return_first_arg) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005713 return_value_offset = 2 + FCA::kArgsLength;
5714 } else {
5715 return_value_offset = 2 + FCA::kReturnValueOffset;
5716 }
5717 MemOperand return_value_operand(fp, return_value_offset * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005718 int stack_space = 0;
5719 int32_t stack_space_offset = 4 * kPointerSize;
5720 if (argc.is_immediate()) {
5721 stack_space = argc.immediate() + FCA::kArgsLength + 1;
5722 stack_space_offset = kInvalidStackOffset;
5723 }
5724 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, stack_space,
5725 stack_space_offset, return_value_operand,
5726 &context_restore_operand);
5727}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005728
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005729
5730void CallApiFunctionStub::Generate(MacroAssembler* masm) {
5731 bool call_data_undefined = this->call_data_undefined();
5732 CallApiFunctionStubHelper(masm, ParameterCount(a3), false,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005733 call_data_undefined, false);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005734}
5735
5736
5737void CallApiAccessorStub::Generate(MacroAssembler* masm) {
5738 bool is_store = this->is_store();
5739 int argc = this->argc();
5740 bool call_data_undefined = this->call_data_undefined();
Ben Murdoch097c5b22016-05-18 11:27:45 +01005741 bool is_lazy = this->is_lazy();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005742 CallApiFunctionStubHelper(masm, ParameterCount(argc), is_store,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005743 call_data_undefined, is_lazy);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005744}
5745
5746
5747void CallApiGetterStub::Generate(MacroAssembler* masm) {
5748 // ----------- S t a t e -------------
Ben Murdoch097c5b22016-05-18 11:27:45 +01005749 // -- sp[0] : name
5750 // -- sp[4 .. (4 + kArgsLength*4)] : v8::PropertyCallbackInfo::args_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005751 // -- ...
Ben Murdoch097c5b22016-05-18 11:27:45 +01005752 // -- a2 : api_function_address
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005753 // -----------------------------------
5754
5755 Register api_function_address = ApiGetterDescriptor::function_address();
5756 DCHECK(api_function_address.is(a2));
5757
Ben Murdoch097c5b22016-05-18 11:27:45 +01005758 // v8::PropertyCallbackInfo::args_ array and name handle.
5759 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5760
5761 // Load address of v8::PropertyAccessorInfo::args_ array and name handle.
5762 __ mov(a0, sp); // a0 = Handle<Name>
5763 __ Addu(a1, a0, Operand(1 * kPointerSize)); // a1 = v8::PCI::args_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005764
5765 const int kApiStackSpace = 1;
5766 FrameScope frame_scope(masm, StackFrame::MANUAL);
5767 __ EnterExitFrame(false, kApiStackSpace);
5768
Ben Murdoch097c5b22016-05-18 11:27:45 +01005769 // Create v8::PropertyCallbackInfo object on the stack and initialize
5770 // it's args_ field.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005771 __ sw(a1, MemOperand(sp, 1 * kPointerSize));
Ben Murdoch097c5b22016-05-18 11:27:45 +01005772 __ Addu(a1, sp, Operand(1 * kPointerSize)); // a1 = v8::PropertyCallbackInfo&
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005773
5774 ExternalReference thunk_ref =
5775 ExternalReference::invoke_accessor_getter_callback(isolate());
Ben Murdoch097c5b22016-05-18 11:27:45 +01005776
5777 // +3 is to skip prolog, return address and name handle.
5778 MemOperand return_value_operand(
5779 fp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005780 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5781 kStackUnwindSpace, kInvalidStackOffset,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005782 return_value_operand, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005783}
5784
5785
Steve Block44f0eee2011-05-26 01:26:41 +01005786#undef __
5787
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005788} // namespace internal
5789} // namespace v8
Steve Block44f0eee2011-05-26 01:26:41 +01005790
5791#endif // V8_TARGET_ARCH_MIPS