blob: 40849646623a90b452a6d69164f6c6572f16aeeb [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 Murdochda12d292016-06-02 14:46:10 +01007#include "src/code-stubs.h"
8#include "src/api-arguments.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/bits.h"
10#include "src/bootstrapper.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000011#include "src/codegen.h"
12#include "src/ic/handler-compiler.h"
13#include "src/ic/ic.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000014#include "src/ic/stub-cache.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015#include "src/isolate.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000016#include "src/mips/code-stubs-mips.h"
17#include "src/regexp/jsregexp.h"
18#include "src/regexp/regexp-macro-assembler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019#include "src/runtime/runtime.h"
Steve Block44f0eee2011-05-26 01:26:41 +010020
21namespace v8 {
22namespace internal {
23
24
Ben Murdochb8a8cc12014-11-26 15:28:44 +000025static void InitializeArrayConstructorDescriptor(
26 Isolate* isolate, CodeStubDescriptor* descriptor,
27 int constant_stack_parameter_count) {
28 Address deopt_handler = Runtime::FunctionForId(
29 Runtime::kArrayConstructor)->entry;
30
31 if (constant_stack_parameter_count == 0) {
32 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
33 JS_FUNCTION_STUB_MODE);
34 } else {
35 descriptor->Initialize(a0, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000037 }
38}
39
40
41static void InitializeInternalArrayConstructorDescriptor(
42 Isolate* isolate, CodeStubDescriptor* descriptor,
43 int constant_stack_parameter_count) {
44 Address deopt_handler = Runtime::FunctionForId(
45 Runtime::kInternalArrayConstructor)->entry;
46
47 if (constant_stack_parameter_count == 0) {
48 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
49 JS_FUNCTION_STUB_MODE);
50 } else {
51 descriptor->Initialize(a0, deopt_handler, constant_stack_parameter_count,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000052 JS_FUNCTION_STUB_MODE);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000053 }
54}
55
56
Ben Murdochb8a8cc12014-11-26 15:28:44 +000057void ArraySingleArgumentConstructorStub::InitializeDescriptor(
58 CodeStubDescriptor* descriptor) {
59 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
60}
61
62
63void ArrayNArgumentsConstructorStub::InitializeDescriptor(
64 CodeStubDescriptor* descriptor) {
65 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
66}
67
68
Ben Murdochda12d292016-06-02 14:46:10 +010069void FastArrayPushStub::InitializeDescriptor(CodeStubDescriptor* descriptor) {
70 Address deopt_handler = Runtime::FunctionForId(Runtime::kArrayPush)->entry;
71 descriptor->Initialize(a0, deopt_handler, -1, JS_FUNCTION_STUB_MODE);
72}
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073
74void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
75 CodeStubDescriptor* descriptor) {
76 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
77}
78
79
80void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
81 CodeStubDescriptor* descriptor) {
82 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
83}
84
85
Steve Block44f0eee2011-05-26 01:26:41 +010086#define __ ACCESS_MASM(masm)
87
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000088static void EmitIdenticalObjectComparison(MacroAssembler* masm, Label* slow,
Ben Murdoch097c5b22016-05-18 11:27:45 +010089 Condition cc);
Ben Murdoch257744e2011-11-30 15:57:28 +000090static void EmitSmiNonsmiComparison(MacroAssembler* masm,
91 Register lhs,
92 Register rhs,
93 Label* rhs_not_nan,
94 Label* slow,
95 bool strict);
Ben Murdoch257744e2011-11-30 15:57:28 +000096static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
97 Register lhs,
98 Register rhs);
99
100
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
102 ExternalReference miss) {
103 // Update the static counter each time a new code stub is generated.
104 isolate()->counters()->code_stubs()->Increment();
Ben Murdoch257744e2011-11-30 15:57:28 +0000105
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000106 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000107 int param_count = descriptor.GetRegisterParameterCount();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000108 {
109 // Call the runtime system in a fresh internal frame.
110 FrameScope scope(masm, StackFrame::INTERNAL);
111 DCHECK(param_count == 0 ||
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000112 a0.is(descriptor.GetRegisterParameter(param_count - 1)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000113 // Push arguments, adjust sp.
114 __ Subu(sp, sp, Operand(param_count * kPointerSize));
115 for (int i = 0; i < param_count; ++i) {
116 // Store argument to stack.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000117 __ sw(descriptor.GetRegisterParameter(i),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000118 MemOperand(sp, (param_count - 1 - i) * kPointerSize));
119 }
120 __ CallExternalReference(miss, param_count);
121 }
Steve Block44f0eee2011-05-26 01:26:41 +0100122
Ben Murdoch257744e2011-11-30 15:57:28 +0000123 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100124}
125
126
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000127void DoubleToIStub::Generate(MacroAssembler* masm) {
128 Label out_of_range, only_low, negate, done;
129 Register input_reg = source();
130 Register result_reg = destination();
Ben Murdoch257744e2011-11-30 15:57:28 +0000131
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000132 int double_offset = offset();
133 // Account for saved regs if input is sp.
134 if (input_reg.is(sp)) double_offset += 3 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +0000135
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 Register scratch =
137 GetRegisterThatIsNotOneOf(input_reg, result_reg);
138 Register scratch2 =
139 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch);
140 Register scratch3 =
141 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch, scratch2);
142 DoubleRegister double_scratch = kLithiumScratchDouble;
Ben Murdoch257744e2011-11-30 15:57:28 +0000143
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 __ Push(scratch, scratch2, scratch3);
Ben Murdoch257744e2011-11-30 15:57:28 +0000145
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000146 if (!skip_fastpath()) {
147 // Load double input.
148 __ ldc1(double_scratch, MemOperand(input_reg, double_offset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000149
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 // Clear cumulative exception flags and save the FCSR.
151 __ cfc1(scratch2, FCSR);
152 __ ctc1(zero_reg, FCSR);
Ben Murdoch257744e2011-11-30 15:57:28 +0000153
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 // Try a conversion to a signed integer.
155 __ Trunc_w_d(double_scratch, double_scratch);
156 // Move the converted value into the result register.
157 __ mfc1(scratch3, double_scratch);
Ben Murdoch257744e2011-11-30 15:57:28 +0000158
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000159 // Retrieve and restore the FCSR.
160 __ cfc1(scratch, FCSR);
161 __ ctc1(scratch2, FCSR);
Steve Block44f0eee2011-05-26 01:26:41 +0100162
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000163 // Check for overflow and NaNs.
164 __ And(
165 scratch, scratch,
166 kFCSROverflowFlagMask | kFCSRUnderflowFlagMask
167 | kFCSRInvalidOpFlagMask);
168 // If we had no exceptions then set result_reg and we are done.
169 Label error;
170 __ Branch(&error, ne, scratch, Operand(zero_reg));
171 __ Move(result_reg, scratch3);
Ben Murdoch257744e2011-11-30 15:57:28 +0000172 __ Branch(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173 __ bind(&error);
Ben Murdoch257744e2011-11-30 15:57:28 +0000174 }
175
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000176 // Load the double value and perform a manual truncation.
177 Register input_high = scratch2;
178 Register input_low = scratch3;
Ben Murdoch257744e2011-11-30 15:57:28 +0000179
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000180 __ lw(input_low,
181 MemOperand(input_reg, double_offset + Register::kMantissaOffset));
182 __ lw(input_high,
183 MemOperand(input_reg, double_offset + Register::kExponentOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000184
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000185 Label normal_exponent, restore_sign;
186 // Extract the biased exponent in result.
187 __ Ext(result_reg,
188 input_high,
Ben Murdoch257744e2011-11-30 15:57:28 +0000189 HeapNumber::kExponentShift,
190 HeapNumber::kExponentBits);
191
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000192 // Check for Infinity and NaNs, which should return 0.
193 __ Subu(scratch, result_reg, HeapNumber::kExponentMask);
194 __ Movz(result_reg, zero_reg, scratch);
195 __ Branch(&done, eq, scratch, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000196
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000197 // Express exponent as delta to (number of mantissa bits + 31).
198 __ Subu(result_reg,
199 result_reg,
200 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31));
Ben Murdoch257744e2011-11-30 15:57:28 +0000201
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000202 // If the delta is strictly positive, all bits would be shifted away,
203 // which means that we can return 0.
204 __ Branch(&normal_exponent, le, result_reg, Operand(zero_reg));
205 __ mov(result_reg, zero_reg);
206 __ Branch(&done);
Ben Murdoch257744e2011-11-30 15:57:28 +0000207
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000208 __ bind(&normal_exponent);
209 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
210 // Calculate shift.
211 __ Addu(scratch, result_reg, Operand(kShiftBase + HeapNumber::kMantissaBits));
Ben Murdoch257744e2011-11-30 15:57:28 +0000212
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000213 // Save the sign.
214 Register sign = result_reg;
215 result_reg = no_reg;
216 __ And(sign, input_high, Operand(HeapNumber::kSignMask));
Ben Murdoch257744e2011-11-30 15:57:28 +0000217
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000218 // On ARM shifts > 31 bits are valid and will result in zero. On MIPS we need
219 // to check for this specific case.
220 Label high_shift_needed, high_shift_done;
221 __ Branch(&high_shift_needed, lt, scratch, Operand(32));
222 __ mov(input_high, zero_reg);
223 __ Branch(&high_shift_done);
224 __ bind(&high_shift_needed);
225
226 // Set the implicit 1 before the mantissa part in input_high.
227 __ Or(input_high,
228 input_high,
229 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
230 // Shift the mantissa bits to the correct position.
231 // We don't need to clear non-mantissa bits as they will be shifted away.
232 // If they weren't, it would mean that the answer is in the 32bit range.
233 __ sllv(input_high, input_high, scratch);
234
235 __ bind(&high_shift_done);
236
237 // Replace the shifted bits with bits from the lower mantissa word.
238 Label pos_shift, shift_done;
Ben Murdoch257744e2011-11-30 15:57:28 +0000239 __ li(at, 32);
240 __ subu(scratch, at, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000241 __ Branch(&pos_shift, ge, scratch, Operand(zero_reg));
242
243 // Negate scratch.
244 __ Subu(scratch, zero_reg, scratch);
245 __ sllv(input_low, input_low, scratch);
246 __ Branch(&shift_done);
247
248 __ bind(&pos_shift);
249 __ srlv(input_low, input_low, scratch);
250
251 __ bind(&shift_done);
252 __ Or(input_high, input_high, Operand(input_low));
253 // Restore sign if necessary.
254 __ mov(scratch, sign);
255 result_reg = sign;
256 sign = no_reg;
257 __ Subu(result_reg, zero_reg, input_high);
258 __ Movz(result_reg, input_high, scratch);
259
260 __ bind(&done);
261
262 __ Pop(scratch, scratch2, scratch3);
263 __ Ret();
Ben Murdoch257744e2011-11-30 15:57:28 +0000264}
265
266
Ben Murdoch257744e2011-11-30 15:57:28 +0000267// Handle the case where the lhs and rhs are the same object.
268// Equality is almost reflexive (everything but NaN), so this is a test
269// for "identity and not NaN".
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000270static void EmitIdenticalObjectComparison(MacroAssembler* masm, Label* slow,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100271 Condition cc) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000272 Label not_identical;
273 Label heap_number, return_equal;
274 Register exp_mask_reg = t5;
275
276 __ Branch(&not_identical, ne, a0, Operand(a1));
277
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000278 __ li(exp_mask_reg, Operand(HeapNumber::kExponentMask));
Ben Murdoch257744e2011-11-30 15:57:28 +0000279
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
281 // so we do the second best thing - test it ourselves.
282 // They are both equal and they are not both Smis so both of them are not
283 // Smis. If it's not a heap number, then return equal.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000284 __ GetObjectType(a0, t4, t4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000285 if (cc == less || cc == greater) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000286 // Call runtime on identical JSObjects.
287 __ Branch(slow, greater, t4, Operand(FIRST_JS_RECEIVER_TYPE));
288 // Call runtime on identical symbols since we need to throw a TypeError.
289 __ Branch(slow, eq, t4, Operand(SYMBOL_TYPE));
290 // Call runtime on identical SIMD values since we must throw a TypeError.
291 __ Branch(slow, eq, t4, Operand(SIMD128_VALUE_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000292 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000293 __ Branch(&heap_number, eq, t4, Operand(HEAP_NUMBER_TYPE));
294 // Comparing JS objects with <=, >= is complicated.
295 if (cc != eq) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100296 __ Branch(slow, greater, t4, Operand(FIRST_JS_RECEIVER_TYPE));
297 // Call runtime on identical symbols since we need to throw a TypeError.
298 __ Branch(slow, eq, t4, Operand(SYMBOL_TYPE));
299 // Call runtime on identical SIMD values since we must throw a TypeError.
300 __ Branch(slow, eq, t4, Operand(SIMD128_VALUE_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 // Normally here we fall through to return_equal, but undefined is
302 // special: (undefined == undefined) == true, but
303 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
304 if (cc == less_equal || cc == greater_equal) {
305 __ Branch(&return_equal, ne, t4, Operand(ODDBALL_TYPE));
306 __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
307 __ Branch(&return_equal, ne, a0, Operand(t2));
308 DCHECK(is_int16(GREATER) && is_int16(LESS));
309 __ Ret(USE_DELAY_SLOT);
310 if (cc == le) {
311 // undefined <= undefined should fail.
312 __ li(v0, Operand(GREATER));
313 } else {
314 // undefined >= undefined should fail.
315 __ li(v0, Operand(LESS));
Ben Murdoch257744e2011-11-30 15:57:28 +0000316 }
317 }
318 }
319 }
320
321 __ bind(&return_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 DCHECK(is_int16(GREATER) && is_int16(LESS));
323 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000324 if (cc == less) {
325 __ li(v0, Operand(GREATER)); // Things aren't less than themselves.
326 } else if (cc == greater) {
327 __ li(v0, Operand(LESS)); // Things aren't greater than themselves.
328 } else {
329 __ mov(v0, zero_reg); // Things are <=, >=, ==, === themselves.
330 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000331
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000332 // For less and greater we don't have to check for NaN since the result of
333 // x < x is false regardless. For the others here is some code to check
334 // for NaN.
335 if (cc != lt && cc != gt) {
336 __ bind(&heap_number);
337 // It is a heap number, so return non-equal if it's NaN and equal if it's
338 // not NaN.
Ben Murdoch257744e2011-11-30 15:57:28 +0000339
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000340 // The representation of NaN values has all exponent bits (52..62) set,
341 // and not all mantissa bits (0..51) clear.
342 // Read top bits of double representation (second word of value).
343 __ lw(t2, FieldMemOperand(a0, HeapNumber::kExponentOffset));
344 // Test that exponent bits are all set.
345 __ And(t3, t2, Operand(exp_mask_reg));
346 // If all bits not set (ne cond), then not a NaN, objects are equal.
347 __ Branch(&return_equal, ne, t3, Operand(exp_mask_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000348
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000349 // Shift out flag and all exponent bits, retaining only mantissa.
350 __ sll(t2, t2, HeapNumber::kNonMantissaBitsInTopWord);
351 // Or with all low-bits of mantissa.
352 __ lw(t3, FieldMemOperand(a0, HeapNumber::kMantissaOffset));
353 __ Or(v0, t3, Operand(t2));
354 // For equal we already have the right value in v0: Return zero (equal)
355 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
356 // not (it's a NaN). For <= and >= we need to load v0 with the failing
357 // value if it's a NaN.
358 if (cc != eq) {
359 // All-zero means Infinity means equal.
360 __ Ret(eq, v0, Operand(zero_reg));
361 DCHECK(is_int16(GREATER) && is_int16(LESS));
362 __ Ret(USE_DELAY_SLOT);
363 if (cc == le) {
364 __ li(v0, Operand(GREATER)); // NaN <= NaN should fail.
365 } else {
366 __ li(v0, Operand(LESS)); // NaN >= NaN should fail.
Ben Murdoch257744e2011-11-30 15:57:28 +0000367 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000368 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000369 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000370 // No fall through here.
Ben Murdoch257744e2011-11-30 15:57:28 +0000371
372 __ bind(&not_identical);
373}
374
375
376static void EmitSmiNonsmiComparison(MacroAssembler* masm,
377 Register lhs,
378 Register rhs,
379 Label* both_loaded_as_doubles,
380 Label* slow,
381 bool strict) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000382 DCHECK((lhs.is(a0) && rhs.is(a1)) ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000383 (lhs.is(a1) && rhs.is(a0)));
384
385 Label lhs_is_smi;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100386 __ JumpIfSmi(lhs, &lhs_is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +0000387 // Rhs is a Smi.
388 // Check whether the non-smi is a heap number.
389 __ GetObjectType(lhs, t4, t4);
390 if (strict) {
391 // If lhs was not a number and rhs was a Smi then strict equality cannot
392 // succeed. Return non-equal (lhs is already not zero).
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100393 __ Ret(USE_DELAY_SLOT, ne, t4, Operand(HEAP_NUMBER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000394 __ mov(v0, lhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000395 } else {
396 // Smi compared non-strictly with a non-Smi non-heap-number. Call
397 // the runtime.
398 __ Branch(slow, ne, t4, Operand(HEAP_NUMBER_TYPE));
399 }
400
401 // Rhs is a smi, lhs is a number.
402 // Convert smi rhs to double.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000403 __ sra(at, rhs, kSmiTagSize);
404 __ mtc1(at, f14);
405 __ cvt_d_w(f14, f14);
406 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000407
408 // We now have both loaded as doubles.
409 __ jmp(both_loaded_as_doubles);
410
411 __ bind(&lhs_is_smi);
412 // Lhs is a Smi. Check whether the non-smi is a heap number.
413 __ GetObjectType(rhs, t4, t4);
414 if (strict) {
415 // If lhs was not a number and rhs was a Smi then strict equality cannot
416 // succeed. Return non-equal.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100417 __ Ret(USE_DELAY_SLOT, ne, t4, Operand(HEAP_NUMBER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000418 __ li(v0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +0000419 } else {
420 // Smi compared non-strictly with a non-Smi non-heap-number. Call
421 // the runtime.
422 __ Branch(slow, ne, t4, Operand(HEAP_NUMBER_TYPE));
423 }
424
425 // Lhs is a smi, rhs is a number.
426 // Convert smi lhs to double.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000427 __ sra(at, lhs, kSmiTagSize);
428 __ mtc1(at, f12);
429 __ cvt_d_w(f12, f12);
430 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000431 // Fall through to both_loaded_as_doubles.
Steve Block44f0eee2011-05-26 01:26:41 +0100432}
433
434
Ben Murdoch257744e2011-11-30 15:57:28 +0000435static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
436 Register lhs,
437 Register rhs) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000438 // If either operand is a JS object or an oddball value, then they are
Ben Murdoch257744e2011-11-30 15:57:28 +0000439 // not equal since their pointers are different.
440 // There is no test for undetectability in strict equality.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000441 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +0000442 Label first_non_object;
443 // Get the type of the first operand into a2 and compare it with
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000444 // FIRST_JS_RECEIVER_TYPE.
Ben Murdoch257744e2011-11-30 15:57:28 +0000445 __ GetObjectType(lhs, a2, a2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000446 __ Branch(&first_non_object, less, a2, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000447
448 // Return non-zero.
449 Label return_not_equal;
450 __ bind(&return_not_equal);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100451 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000452 __ li(v0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +0000453
454 __ bind(&first_non_object);
455 // Check for oddballs: true, false, null, undefined.
456 __ Branch(&return_not_equal, eq, a2, Operand(ODDBALL_TYPE));
457
458 __ GetObjectType(rhs, a3, a3);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000459 __ Branch(&return_not_equal, greater, a3, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000460
461 // Check for oddballs: true, false, null, undefined.
462 __ Branch(&return_not_equal, eq, a3, Operand(ODDBALL_TYPE));
463
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000464 // Now that we have the types we might as well check for
465 // internalized-internalized.
466 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
467 __ Or(a2, a2, Operand(a3));
468 __ And(at, a2, Operand(kIsNotStringMask | kIsNotInternalizedMask));
469 __ Branch(&return_not_equal, eq, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000470}
471
472
473static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
474 Register lhs,
475 Register rhs,
476 Label* both_loaded_as_doubles,
477 Label* not_heap_numbers,
478 Label* slow) {
479 __ GetObjectType(lhs, a3, a2);
480 __ Branch(not_heap_numbers, ne, a2, Operand(HEAP_NUMBER_TYPE));
481 __ lw(a2, FieldMemOperand(rhs, HeapObject::kMapOffset));
482 // If first was a heap number & second wasn't, go to slow case.
483 __ Branch(slow, ne, a3, Operand(a2));
484
485 // Both are heap numbers. Load them up then jump to the code we have
486 // for that.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000487 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
488 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
489
Ben Murdoch257744e2011-11-30 15:57:28 +0000490 __ jmp(both_loaded_as_doubles);
491}
492
493
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000494// Fast negative check for internalized-to-internalized equality.
495static void EmitCheckForInternalizedStringsOrObjects(MacroAssembler* masm,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100496 Register lhs, Register rhs,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000497 Label* possible_strings,
Ben Murdoch097c5b22016-05-18 11:27:45 +0100498 Label* runtime_call) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000499 DCHECK((lhs.is(a0) && rhs.is(a1)) ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000500 (lhs.is(a1) && rhs.is(a0)));
501
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000502 // a2 is object type of rhs.
Ben Murdochda12d292016-06-02 14:46:10 +0100503 Label object_test, return_equal, return_unequal, undetectable;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000504 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000505 __ And(at, a2, Operand(kIsNotStringMask));
506 __ Branch(&object_test, ne, at, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000507 __ And(at, a2, Operand(kIsNotInternalizedMask));
508 __ Branch(possible_strings, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000509 __ GetObjectType(rhs, a3, a3);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100510 __ Branch(runtime_call, ge, a3, Operand(FIRST_NONSTRING_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000511 __ And(at, a3, Operand(kIsNotInternalizedMask));
512 __ Branch(possible_strings, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000513
Ben Murdoch097c5b22016-05-18 11:27:45 +0100514 // Both are internalized. We already checked they weren't the same pointer so
515 // they are not equal. Return non-equal by returning the non-zero object
516 // pointer in v0.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100517 __ Ret(USE_DELAY_SLOT);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100518 __ mov(v0, a0); // In delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +0000519
520 __ bind(&object_test);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100521 __ lw(a2, FieldMemOperand(lhs, HeapObject::kMapOffset));
522 __ lw(a3, FieldMemOperand(rhs, HeapObject::kMapOffset));
523 __ lbu(t0, FieldMemOperand(a2, Map::kBitFieldOffset));
524 __ lbu(t1, FieldMemOperand(a3, Map::kBitFieldOffset));
525 __ And(at, t0, Operand(1 << Map::kIsUndetectable));
526 __ Branch(&undetectable, ne, at, Operand(zero_reg));
527 __ And(at, t1, Operand(1 << Map::kIsUndetectable));
528 __ Branch(&return_unequal, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000529
Ben Murdoch097c5b22016-05-18 11:27:45 +0100530 __ GetInstanceType(a2, a2);
531 __ Branch(runtime_call, lt, a2, Operand(FIRST_JS_RECEIVER_TYPE));
532 __ GetInstanceType(a3, a3);
533 __ Branch(runtime_call, lt, a3, Operand(FIRST_JS_RECEIVER_TYPE));
534
535 __ bind(&return_unequal);
536 // Return non-equal by returning the non-zero object pointer in v0.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100537 __ Ret(USE_DELAY_SLOT);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100538 __ mov(v0, a0); // In delay slot.
539
540 __ bind(&undetectable);
541 __ And(at, t1, Operand(1 << Map::kIsUndetectable));
542 __ Branch(&return_unequal, eq, at, Operand(zero_reg));
Ben Murdochda12d292016-06-02 14:46:10 +0100543
544 // If both sides are JSReceivers, then the result is false according to
545 // the HTML specification, which says that only comparisons with null or
546 // undefined are affected by special casing for document.all.
547 __ GetInstanceType(a2, a2);
548 __ Branch(&return_equal, eq, a2, Operand(ODDBALL_TYPE));
549 __ GetInstanceType(a3, a3);
550 __ Branch(&return_unequal, ne, a3, Operand(ODDBALL_TYPE));
551
552 __ bind(&return_equal);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100553 __ Ret(USE_DELAY_SLOT);
554 __ li(v0, Operand(EQUAL)); // In delay slot.
Steve Block44f0eee2011-05-26 01:26:41 +0100555}
556
557
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000558static void CompareICStub_CheckInputType(MacroAssembler* masm, Register input,
559 Register scratch,
560 CompareICState::State expected,
561 Label* fail) {
562 Label ok;
563 if (expected == CompareICState::SMI) {
564 __ JumpIfNotSmi(input, fail);
565 } else if (expected == CompareICState::NUMBER) {
566 __ JumpIfSmi(input, &ok);
567 __ CheckMap(input, scratch, Heap::kHeapNumberMapRootIndex, fail,
568 DONT_DO_SMI_CHECK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000569 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000570 // We could be strict about internalized/string here, but as long as
571 // hydrogen doesn't care, the stub doesn't have to care either.
572 __ bind(&ok);
Steve Block44f0eee2011-05-26 01:26:41 +0100573}
574
575
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000576// On entry a1 and a2 are the values to be compared.
577// On exit a0 is 0, positive or negative to indicate the result of
578// the comparison.
579void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
580 Register lhs = a1;
581 Register rhs = a0;
582 Condition cc = GetCondition();
Ben Murdoch257744e2011-11-30 15:57:28 +0000583
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000584 Label miss;
585 CompareICStub_CheckInputType(masm, lhs, a2, left(), &miss);
586 CompareICStub_CheckInputType(masm, rhs, a3, right(), &miss);
Ben Murdoch257744e2011-11-30 15:57:28 +0000587
Ben Murdoch257744e2011-11-30 15:57:28 +0000588 Label slow; // Call builtin.
589 Label not_smis, both_loaded_as_doubles;
590
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591 Label not_two_smis, smi_done;
592 __ Or(a2, a1, a0);
593 __ JumpIfNotSmi(a2, &not_two_smis);
594 __ sra(a1, a1, 1);
595 __ sra(a0, a0, 1);
596 __ Ret(USE_DELAY_SLOT);
597 __ subu(v0, a1, a0);
598 __ bind(&not_two_smis);
Ben Murdoch257744e2011-11-30 15:57:28 +0000599
600 // NOTICE! This code is only reached after a smi-fast-case check, so
601 // it is certain that at least one operand isn't a smi.
602
603 // Handle the case where the objects are identical. Either returns the answer
604 // or goes to slow. Only falls through if the objects were not identical.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100605 EmitIdenticalObjectComparison(masm, &slow, cc);
Ben Murdoch257744e2011-11-30 15:57:28 +0000606
607 // If either is a Smi (we know that not both are), then they can only
608 // be strictly equal if the other is a HeapNumber.
609 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000610 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000611 __ And(t2, lhs, Operand(rhs));
Ben Murdoch257744e2011-11-30 15:57:28 +0000612 __ JumpIfNotSmi(t2, &not_smis, t0);
613 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
614 // 1) Return the answer.
615 // 2) Go to slow.
616 // 3) Fall through to both_loaded_as_doubles.
617 // 4) Jump to rhs_not_nan.
618 // In cases 3 and 4 we have found out we were dealing with a number-number
619 // comparison and the numbers have been loaded into f12 and f14 as doubles,
620 // or in GP registers (a0, a1, a2, a3) depending on the presence of the FPU.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000621 EmitSmiNonsmiComparison(masm, lhs, rhs,
622 &both_loaded_as_doubles, &slow, strict());
Ben Murdoch257744e2011-11-30 15:57:28 +0000623
624 __ bind(&both_loaded_as_doubles);
625 // f12, f14 are the double representations of the left hand side
626 // and the right hand side if we have FPU. Otherwise a2, a3 represent
627 // left hand side and a0, a1 represent right hand side.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000628 Label nan;
629 __ li(t0, Operand(LESS));
630 __ li(t1, Operand(GREATER));
631 __ li(t2, Operand(EQUAL));
Ben Murdoch257744e2011-11-30 15:57:28 +0000632
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000633 // Check if either rhs or lhs is NaN.
634 __ BranchF(NULL, &nan, eq, f12, f14);
Ben Murdoch257744e2011-11-30 15:57:28 +0000635
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636 // Check if LESS condition is satisfied. If true, move conditionally
637 // result to v0.
638 if (!IsMipsArchVariant(kMips32r6)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000639 __ c(OLT, D, f12, f14);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100640 __ Movt(v0, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000641 // Use previous check to store conditionally to v0 oposite condition
642 // (GREATER). If rhs is equal to lhs, this will be corrected in next
643 // check.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100644 __ Movf(v0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000645 // Check if EQUAL condition is satisfied. If true, move conditionally
646 // result to v0.
647 __ c(EQ, D, f12, f14);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100648 __ Movt(v0, t2);
Ben Murdoch257744e2011-11-30 15:57:28 +0000649 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000650 Label skip;
651 __ BranchF(USE_DELAY_SLOT, &skip, NULL, lt, f12, f14);
652 __ mov(v0, t0); // Return LESS as result.
Ben Murdoch257744e2011-11-30 15:57:28 +0000653
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000654 __ BranchF(USE_DELAY_SLOT, &skip, NULL, eq, f12, f14);
655 __ mov(v0, t2); // Return EQUAL as result.
656
657 __ mov(v0, t1); // Return GREATER as result.
658 __ bind(&skip);
Ben Murdoch257744e2011-11-30 15:57:28 +0000659 }
660
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000661 __ Ret();
662
663 __ bind(&nan);
664 // NaN comparisons always fail.
665 // Load whatever we need in v0 to make the comparison fail.
666 DCHECK(is_int16(GREATER) && is_int16(LESS));
667 __ Ret(USE_DELAY_SLOT);
668 if (cc == lt || cc == le) {
669 __ li(v0, Operand(GREATER));
670 } else {
671 __ li(v0, Operand(LESS));
672 }
673
674
Ben Murdoch257744e2011-11-30 15:57:28 +0000675 __ bind(&not_smis);
676 // At this point we know we are dealing with two different objects,
677 // and neither of them is a Smi. The objects are in lhs_ and rhs_.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678 if (strict()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000679 // This returns non-equal for some object types, or falls through if it
680 // was not lucky.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000681 EmitStrictTwoHeapObjectCompare(masm, lhs, rhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000682 }
683
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000684 Label check_for_internalized_strings;
Ben Murdoch257744e2011-11-30 15:57:28 +0000685 Label flat_string_check;
686 // Check for heap-number-heap-number comparison. Can jump to slow case,
687 // or load both doubles and jump to the code that handles
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000688 // that case. If the inputs are not doubles then jumps to
689 // check_for_internalized_strings.
Ben Murdoch257744e2011-11-30 15:57:28 +0000690 // In this case a2 will contain the type of lhs_.
691 EmitCheckForTwoHeapNumbers(masm,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000692 lhs,
693 rhs,
Ben Murdoch257744e2011-11-30 15:57:28 +0000694 &both_loaded_as_doubles,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000695 &check_for_internalized_strings,
Ben Murdoch257744e2011-11-30 15:57:28 +0000696 &flat_string_check);
697
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000698 __ bind(&check_for_internalized_strings);
699 if (cc == eq && !strict()) {
700 // Returns an answer for two internalized strings or two
701 // detectable objects.
Ben Murdoch257744e2011-11-30 15:57:28 +0000702 // Otherwise jumps to string case or not both strings case.
703 // Assumes that a2 is the type of lhs_ on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000704 EmitCheckForInternalizedStringsOrObjects(
705 masm, lhs, rhs, &flat_string_check, &slow);
Ben Murdoch257744e2011-11-30 15:57:28 +0000706 }
707
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000708 // Check for both being sequential one-byte strings,
709 // and inline if that is the case.
Ben Murdoch257744e2011-11-30 15:57:28 +0000710 __ bind(&flat_string_check);
711
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000712 __ JumpIfNonSmisNotBothSequentialOneByteStrings(lhs, rhs, a2, a3, &slow);
Ben Murdoch257744e2011-11-30 15:57:28 +0000713
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000714 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1, a2,
715 a3);
716 if (cc == eq) {
717 StringHelper::GenerateFlatOneByteStringEquals(masm, lhs, rhs, a2, a3, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000718 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000719 StringHelper::GenerateCompareFlatOneByteStrings(masm, lhs, rhs, a2, a3, t0,
720 t1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000721 }
722 // Never falls through to here.
723
724 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000725 if (cc == eq) {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100726 {
727 FrameScope scope(masm, StackFrame::INTERNAL);
728 __ Push(lhs, rhs);
729 __ CallRuntime(strict() ? Runtime::kStrictEqual : Runtime::kEqual);
730 }
731 // Turn true into 0 and false into some non-zero value.
732 STATIC_ASSERT(EQUAL == 0);
733 __ LoadRoot(a0, Heap::kTrueValueRootIndex);
734 __ Ret(USE_DELAY_SLOT);
735 __ subu(v0, v0, a0); // In delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +0000736 } else {
Ben Murdoch097c5b22016-05-18 11:27:45 +0100737 // Prepare for call to builtin. Push object pointers, a0 (lhs) first,
738 // a1 (rhs) second.
739 __ Push(lhs, rhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000740 int ncr; // NaN compare result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000741 if (cc == lt || cc == le) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000742 ncr = GREATER;
743 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000744 DCHECK(cc == gt || cc == ge); // Remaining cases.
Ben Murdoch257744e2011-11-30 15:57:28 +0000745 ncr = LESS;
746 }
747 __ li(a0, Operand(Smi::FromInt(ncr)));
748 __ push(a0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000749
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000750 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
751 // tagged as a small integer.
Ben Murdoch097c5b22016-05-18 11:27:45 +0100752 __ TailCallRuntime(Runtime::kCompare);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000753 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000754
755 __ bind(&miss);
756 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +0100757}
758
759
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000760void StoreRegistersStateStub::Generate(MacroAssembler* masm) {
761 __ mov(t9, ra);
762 __ pop(ra);
763 __ PushSafepointRegisters();
764 __ Jump(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100765}
Ben Murdoch257744e2011-11-30 15:57:28 +0000766
Ben Murdoch257744e2011-11-30 15:57:28 +0000767
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000768void RestoreRegistersStateStub::Generate(MacroAssembler* masm) {
769 __ mov(t9, ra);
770 __ pop(ra);
771 __ PopSafepointRegisters();
772 __ Jump(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100773}
774
775
776void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
777 // We don't allow a GC during a store buffer overflow so there is no need to
778 // store the registers in any particular way, but we do have to store and
779 // restore them.
780 __ MultiPush(kJSCallerSaved | ra.bit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000781 if (save_doubles()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100782 __ MultiPushFPU(kCallerSavedFPU);
783 }
784 const int argument_count = 1;
785 const int fp_argument_count = 0;
786 const Register scratch = a1;
787
788 AllowExternalCallThatCantCauseGC scope(masm);
789 __ PrepareCallCFunction(argument_count, fp_argument_count, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100791 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000792 ExternalReference::store_buffer_overflow_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100793 argument_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000794 if (save_doubles()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100795 __ MultiPopFPU(kCallerSavedFPU);
796 }
797
798 __ MultiPop(kJSCallerSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +0000799 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100800}
801
802
Ben Murdoch257744e2011-11-30 15:57:28 +0000803void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100804 const Register base = a1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000805 const Register exponent = MathPowTaggedDescriptor::exponent();
806 DCHECK(exponent.is(a2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100807 const Register heapnumbermap = t1;
808 const Register heapnumber = v0;
809 const DoubleRegister double_base = f2;
810 const DoubleRegister double_exponent = f4;
811 const DoubleRegister double_result = f0;
812 const DoubleRegister double_scratch = f6;
813 const FPURegister single_scratch = f8;
814 const Register scratch = t5;
815 const Register scratch2 = t3;
Ben Murdoch257744e2011-11-30 15:57:28 +0000816
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100817 Label call_runtime, done, int_exponent;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000818 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100819 Label base_is_smi, unpack_exponent;
820 // The exponent and base are supplied as arguments on the stack.
821 // This can only happen if the stub is called from non-optimized code.
822 // Load input parameters from stack to double registers.
Ben Murdoch257744e2011-11-30 15:57:28 +0000823 __ lw(base, MemOperand(sp, 1 * kPointerSize));
824 __ lw(exponent, MemOperand(sp, 0 * kPointerSize));
825
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100826 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +0000827
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100828 __ UntagAndJumpIfSmi(scratch, base, &base_is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +0000829 __ lw(scratch, FieldMemOperand(base, JSObject::kMapOffset));
830 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100831
Ben Murdochc7cc0282012-03-05 14:35:55 +0000832 __ ldc1(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100833 __ jmp(&unpack_exponent);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000834
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100835 __ bind(&base_is_smi);
836 __ mtc1(scratch, single_scratch);
837 __ cvt_d_w(double_base, single_scratch);
838 __ bind(&unpack_exponent);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000839
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100840 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
Ben Murdoch85b71792012-04-11 18:30:58 +0100841
Ben Murdoch85b71792012-04-11 18:30:58 +0100842 __ lw(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
843 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
Ben Murdoch85b71792012-04-11 18:30:58 +0100844 __ ldc1(double_exponent,
845 FieldMemOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000846 } else if (exponent_type() == TAGGED) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100847 // Base is already in double_base.
848 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
Ben Murdoch85b71792012-04-11 18:30:58 +0100849
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100850 __ ldc1(double_exponent,
851 FieldMemOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +0000852 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100853
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000854 if (exponent_type() != INTEGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100855 Label int_exponent_convert;
856 // Detect integer exponents stored as double.
857 __ EmitFPUTruncate(kRoundToMinusInf,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100858 scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000859 double_exponent,
860 at,
861 double_scratch,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100862 scratch2,
863 kCheckForInexactConversion);
864 // scratch2 == 0 means there was no conversion error.
865 __ Branch(&int_exponent_convert, eq, scratch2, Operand(zero_reg));
866
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000867 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100868 // Detect square root case. Crankshaft detects constant +/-0.5 at
869 // compile time and uses DoMathPowHalf instead. We then skip this check
870 // for non-constant cases of +/-0.5 as these hardly occur.
871 Label not_plus_half;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100872 // Test for 0.5.
873 __ Move(double_scratch, 0.5);
874 __ BranchF(USE_DELAY_SLOT,
875 &not_plus_half,
876 NULL,
877 ne,
878 double_exponent,
879 double_scratch);
880 // double_scratch can be overwritten in the delay slot.
881 // Calculates square root of base. Check for the special case of
882 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400883 __ Move(double_scratch, static_cast<double>(-V8_INFINITY));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100884 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, double_base, double_scratch);
885 __ neg_d(double_result, double_scratch);
886
887 // Add +0 to convert -0 to +0.
888 __ add_d(double_scratch, double_base, kDoubleRegZero);
889 __ sqrt_d(double_result, double_scratch);
890 __ jmp(&done);
891
892 __ bind(&not_plus_half);
893 __ Move(double_scratch, -0.5);
894 __ BranchF(USE_DELAY_SLOT,
895 &call_runtime,
896 NULL,
897 ne,
898 double_exponent,
899 double_scratch);
900 // double_scratch can be overwritten in the delay slot.
901 // Calculates square root of base. Check for the special case of
902 // Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400903 __ Move(double_scratch, static_cast<double>(-V8_INFINITY));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100904 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, double_base, double_scratch);
905 __ Move(double_result, kDoubleRegZero);
906
907 // Add +0 to convert -0 to +0.
908 __ add_d(double_scratch, double_base, kDoubleRegZero);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400909 __ Move(double_result, 1.);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100910 __ sqrt_d(double_scratch, double_scratch);
911 __ div_d(double_result, double_result, double_scratch);
912 __ jmp(&done);
913 }
914
915 __ push(ra);
916 {
917 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000918 __ PrepareCallCFunction(0, 2, scratch2);
919 __ MovToFloatParameters(double_base, double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100920 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000921 ExternalReference::power_double_double_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100922 0, 2);
923 }
924 __ pop(ra);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000925 __ MovFromFloatResult(double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100926 __ jmp(&done);
927
928 __ bind(&int_exponent_convert);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100929 }
930
931 // Calculate power with integer exponent.
932 __ bind(&int_exponent);
933
934 // Get two copies of exponent in the registers scratch and exponent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000935 if (exponent_type() == INTEGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100936 __ mov(scratch, exponent);
937 } else {
938 // Exponent has previously been stored into scratch as untagged integer.
939 __ mov(exponent, scratch);
940 }
941
942 __ mov_d(double_scratch, double_base); // Back up base.
943 __ Move(double_result, 1.0);
944
945 // Get absolute value of exponent.
946 Label positive_exponent;
947 __ Branch(&positive_exponent, ge, scratch, Operand(zero_reg));
948 __ Subu(scratch, zero_reg, scratch);
949 __ bind(&positive_exponent);
950
951 Label while_true, no_carry, loop_end;
952 __ bind(&while_true);
953
954 __ And(scratch2, scratch, 1);
955
956 __ Branch(&no_carry, eq, scratch2, Operand(zero_reg));
957 __ mul_d(double_result, double_result, double_scratch);
958 __ bind(&no_carry);
959
960 __ sra(scratch, scratch, 1);
961
962 __ Branch(&loop_end, eq, scratch, Operand(zero_reg));
963 __ mul_d(double_scratch, double_scratch, double_scratch);
964
965 __ Branch(&while_true);
966
967 __ bind(&loop_end);
968
969 __ Branch(&done, ge, exponent, Operand(zero_reg));
970 __ Move(double_scratch, 1.0);
971 __ div_d(double_result, double_scratch, double_result);
972 // Test whether result is zero. Bail out to check for subnormal result.
973 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
974 __ BranchF(&done, NULL, ne, double_result, kDoubleRegZero);
975
976 // double_exponent may not contain the exponent value if the input was a
977 // smi. We set it with exponent value before bailing out.
978 __ mtc1(exponent, single_scratch);
979 __ cvt_d_w(double_exponent, single_scratch);
980
981 // Returning or bailing out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000982 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100983 // The arguments are still on the stack.
984 __ bind(&call_runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000985 __ TailCallRuntime(Runtime::kMathPowRT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100986
987 // The stub is called from non-optimized code, which expects the result
988 // as heap number in exponent.
989 __ bind(&done);
990 __ AllocateHeapNumber(
991 heapnumber, scratch, scratch2, heapnumbermap, &call_runtime);
992 __ sdc1(double_result,
993 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000994 DCHECK(heapnumber.is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100995 __ DropAndRet(2);
996 } else {
997 __ push(ra);
998 {
999 AllowExternalCallThatCantCauseGC scope(masm);
1000 __ PrepareCallCFunction(0, 2, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001001 __ MovToFloatParameters(double_base, double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001002 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001003 ExternalReference::power_double_double_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001004 0, 2);
1005 }
1006 __ pop(ra);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001007 __ MovFromFloatResult(double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001008
1009 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001010 __ Ret();
1011 }
Steve Block44f0eee2011-05-26 01:26:41 +01001012}
1013
1014
1015bool CEntryStub::NeedsImmovableCode() {
1016 return true;
1017}
1018
1019
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001020void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
1021 CEntryStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001022 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
1023 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
1024 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
1025 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001026 CreateWeakCellStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001027 BinaryOpICStub::GenerateAheadOfTime(isolate);
1028 StoreRegistersStateStub::GenerateAheadOfTime(isolate);
1029 RestoreRegistersStateStub::GenerateAheadOfTime(isolate);
1030 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001031 StoreFastElementStub::GenerateAheadOfTime(isolate);
1032 TypeofStub::GenerateAheadOfTime(isolate);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001033}
1034
1035
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001036void StoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
1037 StoreRegistersStateStub stub(isolate);
1038 stub.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001039}
1040
1041
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001042void RestoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
1043 RestoreRegistersStateStub stub(isolate);
1044 stub.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001045}
1046
1047
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001048void CodeStub::GenerateFPStubs(Isolate* isolate) {
1049 // Generate if not already in cache.
1050 SaveFPRegsMode mode = kSaveFPRegs;
1051 CEntryStub(isolate, 1, mode).GetCode();
1052 StoreBufferOverflowStub(isolate, mode).GetCode();
1053 isolate->set_fp_stubs_generated(true);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001054}
1055
1056
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1058 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1059 stub.GetCode();
1060}
Ben Murdoch257744e2011-11-30 15:57:28 +00001061
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001062
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001063void CEntryStub::Generate(MacroAssembler* masm) {
1064 // Called from JavaScript; parameters are on stack as if calling JS function
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001065 // a0: number of arguments including receiver
1066 // a1: pointer to builtin function
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001067 // fp: frame pointer (restored after C call)
1068 // sp: stack pointer (restored as callee's sp after C call)
1069 // cp: current context (C callee-saved)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001070 //
1071 // If argv_in_register():
1072 // a2: pointer to the first argument
Ben Murdoch257744e2011-11-30 15:57:28 +00001073
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001074 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1075
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001076 if (argv_in_register()) {
1077 // Move argv into the correct register.
1078 __ mov(s1, a2);
1079 } else {
1080 // Compute the argv pointer in a callee-saved register.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001081 __ Lsa(s1, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001082 __ Subu(s1, s1, kPointerSize);
1083 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001084
1085 // Enter the exit frame that transitions from JavaScript to C++.
1086 FrameScope scope(masm, StackFrame::MANUAL);
1087 __ EnterExitFrame(save_doubles());
1088
1089 // s0: number of arguments including receiver (C callee-saved)
1090 // s1: pointer to first argument (C callee-saved)
1091 // s2: pointer to builtin function (C callee-saved)
Ben Murdoch257744e2011-11-30 15:57:28 +00001092
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001093 // Prepare arguments for C routine.
1094 // a0 = argc
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001095 __ mov(s0, a0);
1096 __ mov(s2, a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001097
1098 // We are calling compiled C/C++ code. a0 and a1 hold our two arguments. We
1099 // also need to reserve the 4 argument slots on the stack.
1100
1101 __ AssertStackIsAligned();
1102
Ben Murdoch097c5b22016-05-18 11:27:45 +01001103 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
1104 int frame_alignment_mask = frame_alignment - 1;
1105 int result_stack_size;
1106 if (result_size() <= 2) {
1107 // a0 = argc, a1 = argv, a2 = isolate
1108 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
1109 __ mov(a1, s1);
1110 result_stack_size = 0;
1111 } else {
1112 DCHECK_EQ(3, result_size());
1113 // Allocate additional space for the result.
1114 result_stack_size =
1115 ((result_size() * kPointerSize) + frame_alignment_mask) &
1116 ~frame_alignment_mask;
1117 __ Subu(sp, sp, Operand(result_stack_size));
1118
1119 // a0 = hidden result argument, a1 = argc, a2 = argv, a3 = isolate.
1120 __ li(a3, Operand(ExternalReference::isolate_address(isolate())));
1121 __ mov(a2, s1);
1122 __ mov(a1, a0);
1123 __ mov(a0, sp);
1124 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001125
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001126 // To let the GC traverse the return address of the exit frames, we need to
1127 // know where the return address is. The CEntryStub is unmovable, so
1128 // we can store the address on the stack to be able to find it again and
1129 // we never have to restore it, because it will not change.
Ben Murdoch257744e2011-11-30 15:57:28 +00001130 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001131 int kNumInstructionsToJump = 4;
Ben Murdoch257744e2011-11-30 15:57:28 +00001132 Label find_ra;
Ben Murdoch257744e2011-11-30 15:57:28 +00001133 // Adjust the value in ra to point to the correct return location, 2nd
1134 // instruction past the real call into C code (the jalr(t9)), and push it.
1135 // This is the return address of the exit frame.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001136 if (kArchVariant >= kMips32r6) {
1137 __ addiupc(ra, kNumInstructionsToJump + 1);
1138 } else {
1139 // This branch-and-link sequence is needed to find the current PC on mips
1140 // before r6, saved to the ra register.
1141 __ bal(&find_ra); // bal exposes branch delay slot.
1142 __ Addu(ra, ra, kNumInstructionsToJump * Instruction::kInstrSize);
1143 }
1144 __ bind(&find_ra);
1145
1146 // This spot was reserved in EnterExitFrame.
1147 __ sw(ra, MemOperand(sp, result_stack_size));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001148 // Stack space reservation moved to the branch delay slot below.
Ben Murdoch257744e2011-11-30 15:57:28 +00001149 // Stack is still aligned.
1150
1151 // Call the C routine.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001152 __ mov(t9, s2); // Function pointer to t9 to conform to ABI for PIC.
1153 __ jalr(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001154 // Set up sp in the delay slot.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001155 __ addiu(sp, sp, -kCArgsSlotsSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001156 // Make sure the stored 'ra' points to this position.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001157 DCHECK_EQ(kNumInstructionsToJump,
Ben Murdoch257744e2011-11-30 15:57:28 +00001158 masm->InstructionsGeneratedSince(&find_ra));
1159 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01001160 if (result_size() > 2) {
1161 DCHECK_EQ(3, result_size());
1162 // Read result values stored on stack.
1163 __ lw(a0, MemOperand(v0, 2 * kPointerSize));
1164 __ lw(v1, MemOperand(v0, 1 * kPointerSize));
1165 __ lw(v0, MemOperand(v0, 0 * kPointerSize));
1166 }
1167 // Result returned in v0, v1:v0 or a0:v1:v0 - do not destroy these registers!
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001168
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001169 // Check result for exception sentinel.
1170 Label exception_returned;
1171 __ LoadRoot(t0, Heap::kExceptionRootIndex);
1172 __ Branch(&exception_returned, eq, t0, Operand(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001173
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001174 // Check that there is no pending exception, otherwise we
1175 // should have returned the exception sentinel.
1176 if (FLAG_debug_code) {
1177 Label okay;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001178 ExternalReference pending_exception_address(
1179 Isolate::kPendingExceptionAddress, isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001180 __ li(a2, Operand(pending_exception_address));
1181 __ lw(a2, MemOperand(a2));
1182 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1183 // Cannot use check here as it attempts to generate call into runtime.
1184 __ Branch(&okay, eq, t0, Operand(a2));
1185 __ stop("Unexpected pending exception");
1186 __ bind(&okay);
1187 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001188
1189 // Exit C frame and return.
1190 // v0:v1: result
1191 // sp: stack pointer
1192 // fp: frame pointer
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001193 Register argc;
1194 if (argv_in_register()) {
1195 // We don't want to pop arguments so set argc to no_reg.
1196 argc = no_reg;
1197 } else {
1198 // s0: still holds argc (callee-saved).
1199 argc = s0;
1200 }
1201 __ LeaveExitFrame(save_doubles(), argc, true, EMIT_RETURN);
Ben Murdoch257744e2011-11-30 15:57:28 +00001202
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001203 // Handling of exception.
1204 __ bind(&exception_returned);
Ben Murdoch257744e2011-11-30 15:57:28 +00001205
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001206 ExternalReference pending_handler_context_address(
1207 Isolate::kPendingHandlerContextAddress, isolate());
1208 ExternalReference pending_handler_code_address(
1209 Isolate::kPendingHandlerCodeAddress, isolate());
1210 ExternalReference pending_handler_offset_address(
1211 Isolate::kPendingHandlerOffsetAddress, isolate());
1212 ExternalReference pending_handler_fp_address(
1213 Isolate::kPendingHandlerFPAddress, isolate());
1214 ExternalReference pending_handler_sp_address(
1215 Isolate::kPendingHandlerSPAddress, isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001216
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001217 // Ask the runtime for help to determine the handler. This will set v0 to
1218 // contain the current pending exception, don't clobber it.
1219 ExternalReference find_handler(Runtime::kUnwindAndFindExceptionHandler,
1220 isolate());
1221 {
1222 FrameScope scope(masm, StackFrame::MANUAL);
1223 __ PrepareCallCFunction(3, 0, a0);
1224 __ mov(a0, zero_reg);
1225 __ mov(a1, zero_reg);
1226 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
1227 __ CallCFunction(find_handler, 3);
1228 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001229
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001230 // Retrieve the handler context, SP and FP.
1231 __ li(cp, Operand(pending_handler_context_address));
1232 __ lw(cp, MemOperand(cp));
1233 __ li(sp, Operand(pending_handler_sp_address));
1234 __ lw(sp, MemOperand(sp));
1235 __ li(fp, Operand(pending_handler_fp_address));
1236 __ lw(fp, MemOperand(fp));
Ben Murdoch257744e2011-11-30 15:57:28 +00001237
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001238 // If the handler is a JS frame, restore the context to the frame. Note that
1239 // the context will be set to (cp == 0) for non-JS frames.
1240 Label zero;
1241 __ Branch(&zero, eq, cp, Operand(zero_reg));
1242 __ sw(cp, MemOperand(fp, StandardFrameConstants::kContextOffset));
1243 __ bind(&zero);
Ben Murdoch257744e2011-11-30 15:57:28 +00001244
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001245 // Compute the handler entry address and jump to it.
1246 __ li(a1, Operand(pending_handler_code_address));
1247 __ lw(a1, MemOperand(a1));
1248 __ li(a2, Operand(pending_handler_offset_address));
1249 __ lw(a2, MemOperand(a2));
1250 __ Addu(a1, a1, Operand(Code::kHeaderSize - kHeapObjectTag));
1251 __ Addu(t9, a1, a2);
1252 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01001253}
1254
1255
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001256void JSEntryStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001257 Label invoke, handler_entry, exit;
1258 Isolate* isolate = masm->isolate();
Ben Murdoch257744e2011-11-30 15:57:28 +00001259
1260 // Registers:
1261 // a0: entry address
1262 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001263 // a2: receiver
Ben Murdoch257744e2011-11-30 15:57:28 +00001264 // a3: argc
1265 //
1266 // Stack:
1267 // 4 args slots
1268 // args
1269
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001270 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1271
Ben Murdoch257744e2011-11-30 15:57:28 +00001272 // Save callee saved registers on the stack.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001273 __ MultiPush(kCalleeSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00001274
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001275 // Save callee-saved FPU registers.
1276 __ MultiPushFPU(kCalleeSavedFPU);
1277 // Set up the reserved register for 0.0.
1278 __ Move(kDoubleRegZero, 0.0);
Ben Murdoch589d6972011-11-30 16:04:58 +00001279
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001280
Ben Murdoch257744e2011-11-30 15:57:28 +00001281 // Load argv in s0 register.
Ben Murdoch589d6972011-11-30 16:04:58 +00001282 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001283 offset_to_argv += kNumCalleeSavedFPU * kDoubleSize;
Ben Murdoch589d6972011-11-30 16:04:58 +00001284
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001285 __ InitializeRootRegister();
Ben Murdoch589d6972011-11-30 16:04:58 +00001286 __ lw(s0, MemOperand(sp, offset_to_argv + kCArgsSlotsSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001287
1288 // We build an EntryFrame.
1289 __ li(t3, Operand(-1)); // Push a bad frame pointer to fail if it is used.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001290 int marker = type();
Ben Murdoch257744e2011-11-30 15:57:28 +00001291 __ li(t2, Operand(Smi::FromInt(marker)));
1292 __ li(t1, Operand(Smi::FromInt(marker)));
Ben Murdoch589d6972011-11-30 16:04:58 +00001293 __ li(t0, Operand(ExternalReference(Isolate::kCEntryFPAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001294 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001295 __ lw(t0, MemOperand(t0));
1296 __ Push(t3, t2, t1, t0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001297 // Set up frame pointer for the frame to be pushed.
Ben Murdoch257744e2011-11-30 15:57:28 +00001298 __ addiu(fp, sp, -EntryFrameConstants::kCallerFPOffset);
1299
1300 // Registers:
1301 // a0: entry_address
1302 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001303 // a2: receiver_pointer
Ben Murdoch257744e2011-11-30 15:57:28 +00001304 // a3: argc
1305 // s0: argv
1306 //
1307 // Stack:
1308 // caller fp |
1309 // function slot | entry frame
1310 // context slot |
1311 // bad fp (0xff...f) |
1312 // callee saved registers + ra
1313 // 4 args slots
1314 // args
1315
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001316 // If this is the outermost JS call, set js_entry_sp value.
1317 Label non_outermost_js;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001318 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001319 __ li(t1, Operand(ExternalReference(js_entry_sp)));
1320 __ lw(t2, MemOperand(t1));
1321 __ Branch(&non_outermost_js, ne, t2, Operand(zero_reg));
1322 __ sw(fp, MemOperand(t1));
1323 __ li(t0, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1324 Label cont;
1325 __ b(&cont);
1326 __ nop(); // Branch delay slot nop.
1327 __ bind(&non_outermost_js);
1328 __ li(t0, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
1329 __ bind(&cont);
1330 __ push(t0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001331
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001332 // Jump to a faked try block that does the invoke, with a faked catch
1333 // block that sets the pending exception.
1334 __ jmp(&invoke);
1335 __ bind(&handler_entry);
1336 handler_offset_ = handler_entry.pos();
1337 // Caught exception: Store result (exception) in the pending exception
1338 // field in the JSEnv and return a failure sentinel. Coming in here the
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001339 // fp will be invalid because the PushStackHandler below sets it to 0 to
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001340 // signal the existence of the JSEntry frame.
Ben Murdoch589d6972011-11-30 16:04:58 +00001341 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001342 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001343 __ sw(v0, MemOperand(t0)); // We come back from 'invoke'. result is in v0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001344 __ LoadRoot(v0, Heap::kExceptionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001345 __ b(&exit); // b exposes branch delay slot.
1346 __ nop(); // Branch delay slot nop.
1347
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001348 // Invoke: Link this frame into the handler chain.
Ben Murdoch257744e2011-11-30 15:57:28 +00001349 __ bind(&invoke);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001350 __ PushStackHandler();
Ben Murdoch257744e2011-11-30 15:57:28 +00001351 // If an exception not caught by another handler occurs, this handler
1352 // returns control to the code after the bal(&invoke) above, which
1353 // restores all kCalleeSaved registers (including cp and fp) to their
1354 // saved values before returning a failure to C.
1355
1356 // Clear any pending exceptions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001357 __ LoadRoot(t1, Heap::kTheHoleValueRootIndex);
Ben Murdoch589d6972011-11-30 16:04:58 +00001358 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001359 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001360 __ sw(t1, MemOperand(t0));
1361
1362 // Invoke the function by calling through JS entry trampoline builtin.
1363 // Notice that we cannot store a reference to the trampoline code directly in
1364 // this stub, because runtime stubs are not traversed when doing GC.
1365
1366 // Registers:
1367 // a0: entry_address
1368 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001369 // a2: receiver_pointer
Ben Murdoch257744e2011-11-30 15:57:28 +00001370 // a3: argc
1371 // s0: argv
1372 //
1373 // Stack:
1374 // handler frame
1375 // entry frame
1376 // callee saved registers + ra
1377 // 4 args slots
1378 // args
1379
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001380 if (type() == StackFrame::ENTRY_CONSTRUCT) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001381 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001382 isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00001383 __ li(t0, Operand(construct_entry));
1384 } else {
1385 ExternalReference entry(Builtins::kJSEntryTrampoline, masm->isolate());
1386 __ li(t0, Operand(entry));
1387 }
1388 __ lw(t9, MemOperand(t0)); // Deref address.
1389
1390 // Call JSEntryTrampoline.
1391 __ addiu(t9, t9, Code::kHeaderSize - kHeapObjectTag);
1392 __ Call(t9);
1393
1394 // Unlink this frame from the handler chain.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001395 __ PopStackHandler();
Ben Murdoch257744e2011-11-30 15:57:28 +00001396
1397 __ bind(&exit); // v0 holds result
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001398 // Check if the current stack frame is marked as the outermost JS frame.
1399 Label non_outermost_js_2;
1400 __ pop(t1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001401 __ Branch(&non_outermost_js_2,
1402 ne,
1403 t1,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001404 Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1405 __ li(t1, Operand(ExternalReference(js_entry_sp)));
1406 __ sw(zero_reg, MemOperand(t1));
1407 __ bind(&non_outermost_js_2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001408
1409 // Restore the top frame descriptors from the stack.
1410 __ pop(t1);
Ben Murdoch589d6972011-11-30 16:04:58 +00001411 __ li(t0, Operand(ExternalReference(Isolate::kCEntryFPAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001412 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001413 __ sw(t1, MemOperand(t0));
1414
1415 // Reset the stack to the callee saved registers.
1416 __ addiu(sp, sp, -EntryFrameConstants::kCallerFPOffset);
1417
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001418 // Restore callee-saved fpu registers.
1419 __ MultiPopFPU(kCalleeSavedFPU);
Ben Murdoch589d6972011-11-30 16:04:58 +00001420
Ben Murdoch257744e2011-11-30 15:57:28 +00001421 // Restore callee saved registers from the stack.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001422 __ MultiPop(kCalleeSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00001423 // Return.
1424 __ Jump(ra);
Steve Block44f0eee2011-05-26 01:26:41 +01001425}
1426
1427
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001428void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
1429 // Return address is in ra.
1430 Label miss;
1431
1432 Register receiver = LoadDescriptor::ReceiverRegister();
1433 Register index = LoadDescriptor::NameRegister();
1434 Register scratch = t1;
1435 Register result = v0;
1436 DCHECK(!scratch.is(receiver) && !scratch.is(index));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001437 DCHECK(!scratch.is(LoadWithVectorDescriptor::VectorRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001438
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001439 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
1440 &miss, // When not a string.
1441 &miss, // When not a number.
1442 &miss, // When index out of range.
1443 STRING_INDEX_IS_ARRAY_INDEX,
1444 RECEIVER_IS_STRING);
1445 char_at_generator.GenerateFast(masm);
1446 __ Ret();
1447
1448 StubRuntimeCallHelper call_helper;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001449 char_at_generator.GenerateSlow(masm, PART_OF_IC_HANDLER, call_helper);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001450
1451 __ bind(&miss);
1452 PropertyAccessCompiler::TailCallBuiltin(
1453 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
1454}
1455
1456
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001457void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
1458 Label miss;
1459 Register receiver = LoadDescriptor::ReceiverRegister();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001460 // Ensure that the vector and slot registers won't be clobbered before
1461 // calling the miss handler.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001462 DCHECK(!AreAliased(t0, t1, LoadWithVectorDescriptor::VectorRegister(),
1463 LoadWithVectorDescriptor::SlotRegister()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001464
1465 NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(masm, receiver, t0,
1466 t1, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001467 __ bind(&miss);
1468 PropertyAccessCompiler::TailCallBuiltin(
1469 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
1470}
Ben Murdoch257744e2011-11-30 15:57:28 +00001471
Steve Block44f0eee2011-05-26 01:26:41 +01001472void RegExpExecStub::Generate(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001473 // Just jump directly to runtime if native RegExp is not selected at compile
1474 // time or if regexp entry in generated code is turned off runtime switch or
1475 // at compilation.
1476#ifdef V8_INTERPRETED_REGEXP
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001477 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdoch257744e2011-11-30 15:57:28 +00001478#else // V8_INTERPRETED_REGEXP
Ben Murdoch257744e2011-11-30 15:57:28 +00001479
1480 // Stack frame on entry.
1481 // sp[0]: last_match_info (expected JSArray)
1482 // sp[4]: previous index
1483 // sp[8]: subject string
1484 // sp[12]: JSRegExp object
1485
Ben Murdochdb1b4382012-04-26 19:03:50 +01001486 const int kLastMatchInfoOffset = 0 * kPointerSize;
1487 const int kPreviousIndexOffset = 1 * kPointerSize;
1488 const int kSubjectOffset = 2 * kPointerSize;
1489 const int kJSRegExpOffset = 3 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00001490
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001491 Label runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00001492 // Allocation of registers for this function. These are in callee save
1493 // registers and will be preserved by the call to the native RegExp code, as
1494 // this code is called using the normal C calling convention. When calling
1495 // directly from generated code the native RegExp code will not do a GC and
1496 // therefore the content of these registers are safe to use after the call.
1497 // MIPS - using s0..s2, since we are not using CEntry Stub.
1498 Register subject = s0;
1499 Register regexp_data = s1;
1500 Register last_match_info_elements = s2;
1501
1502 // Ensure that a RegExp stack is allocated.
1503 ExternalReference address_of_regexp_stack_memory_address =
Ben Murdoch097c5b22016-05-18 11:27:45 +01001504 ExternalReference::address_of_regexp_stack_memory_address(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001505 ExternalReference address_of_regexp_stack_memory_size =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001506 ExternalReference::address_of_regexp_stack_memory_size(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001507 __ li(a0, Operand(address_of_regexp_stack_memory_size));
1508 __ lw(a0, MemOperand(a0, 0));
1509 __ Branch(&runtime, eq, a0, Operand(zero_reg));
1510
1511 // Check that the first argument is a JSRegExp object.
1512 __ lw(a0, MemOperand(sp, kJSRegExpOffset));
1513 STATIC_ASSERT(kSmiTag == 0);
1514 __ JumpIfSmi(a0, &runtime);
1515 __ GetObjectType(a0, a1, a1);
1516 __ Branch(&runtime, ne, a1, Operand(JS_REGEXP_TYPE));
1517
1518 // Check that the RegExp has been compiled (data contains a fixed array).
1519 __ lw(regexp_data, FieldMemOperand(a0, JSRegExp::kDataOffset));
1520 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001521 __ SmiTst(regexp_data, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001522 __ Check(nz,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001523 kUnexpectedTypeForRegExpDataFixedArrayExpected,
Ben Murdoch257744e2011-11-30 15:57:28 +00001524 t0,
1525 Operand(zero_reg));
1526 __ GetObjectType(regexp_data, a0, a0);
1527 __ Check(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001528 kUnexpectedTypeForRegExpDataFixedArrayExpected,
Ben Murdoch257744e2011-11-30 15:57:28 +00001529 a0,
1530 Operand(FIXED_ARRAY_TYPE));
1531 }
1532
1533 // regexp_data: RegExp data (FixedArray)
1534 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
1535 __ lw(a0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
1536 __ Branch(&runtime, ne, a0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
1537
1538 // regexp_data: RegExp data (FixedArray)
1539 // Check that the number of captures fit in the static offsets vector buffer.
1540 __ lw(a2,
1541 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001542 // Check (number_of_captures + 1) * 2 <= offsets vector size
1543 // Or number_of_captures * 2 <= offsets vector size - 2
1544 // Multiplying by 2 comes for free since a2 is smi-tagged.
Ben Murdoch257744e2011-11-30 15:57:28 +00001545 STATIC_ASSERT(kSmiTag == 0);
1546 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001547 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
1548 __ Branch(
1549 &runtime, hi, a2, Operand(Isolate::kJSRegexpStaticOffsetsVectorSize - 2));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001550
1551 // Reset offset for possibly sliced string.
1552 __ mov(t0, zero_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001553 __ lw(subject, MemOperand(sp, kSubjectOffset));
1554 __ JumpIfSmi(subject, &runtime);
1555 __ mov(a3, subject); // Make a copy of the original subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001556 // subject: subject string
1557 // a3: subject string
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001558 // regexp_data: RegExp data (FixedArray)
1559 // Handle subject string according to its encoding and representation:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001560 // (1) Sequential string? If yes, go to (4).
1561 // (2) Sequential or cons? If not, go to (5).
1562 // (3) Cons string. If the string is flat, replace subject with first string
1563 // and go to (1). Otherwise bail out to runtime.
1564 // (4) Sequential string. Load regexp code according to encoding.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001565 // (E) Carry on.
1566 /// [...]
1567
1568 // Deferred code at the end of the stub:
Ben Murdoch097c5b22016-05-18 11:27:45 +01001569 // (5) Long external string? If not, go to (7).
1570 // (6) External string. Make it, offset-wise, look like a sequential string.
1571 // Go to (4).
1572 // (7) Short external string or not a string? If yes, bail out to runtime.
1573 // (8) Sliced string. Replace subject with parent. Go to (1).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001574
Ben Murdoch097c5b22016-05-18 11:27:45 +01001575 Label seq_string /* 4 */, external_string /* 6 */, check_underlying /* 1 */,
1576 not_seq_nor_cons /* 5 */, not_long_external /* 7 */;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001577
Ben Murdoch097c5b22016-05-18 11:27:45 +01001578 __ bind(&check_underlying);
1579 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
1580 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
1581
1582 // (1) Sequential string? If yes, go to (4).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001583 __ And(a1,
1584 a0,
1585 Operand(kIsNotStringMask |
1586 kStringRepresentationMask |
1587 kShortExternalStringMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00001588 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001589 __ Branch(&seq_string, eq, a1, Operand(zero_reg)); // Go to (5).
Ben Murdoch257744e2011-11-30 15:57:28 +00001590
Ben Murdoch097c5b22016-05-18 11:27:45 +01001591 // (2) Sequential or cons? If not, go to (5).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001592 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
1593 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001594 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
1595 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001596 // Go to (5).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001597 __ Branch(&not_seq_nor_cons, ge, a1, Operand(kExternalStringTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001598
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001599 // (3) Cons string. Check that it's flat.
1600 // Replace subject with first string and reload instance type.
Ben Murdoch257744e2011-11-30 15:57:28 +00001601 __ lw(a0, FieldMemOperand(subject, ConsString::kSecondOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001602 __ LoadRoot(a1, Heap::kempty_stringRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001603 __ Branch(&runtime, ne, a0, Operand(a1));
1604 __ lw(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01001605 __ jmp(&check_underlying);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001606
Ben Murdoch097c5b22016-05-18 11:27:45 +01001607 // (4) Sequential string. Load regexp code according to encoding.
Ben Murdoch257744e2011-11-30 15:57:28 +00001608 __ bind(&seq_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001609 // subject: sequential subject string (or look-alike, external string)
1610 // a3: original subject string
1611 // Load previous index and check range before a3 is overwritten. We have to
1612 // use a3 instead of subject here because subject might have been only made
1613 // to look like a sequential string when it actually is an external string.
1614 __ lw(a1, MemOperand(sp, kPreviousIndexOffset));
1615 __ JumpIfNotSmi(a1, &runtime);
1616 __ lw(a3, FieldMemOperand(a3, String::kLengthOffset));
1617 __ Branch(&runtime, ls, a3, Operand(a1));
1618 __ sra(a1, a1, kSmiTagSize); // Untag the Smi.
1619
Ben Murdoch257744e2011-11-30 15:57:28 +00001620 STATIC_ASSERT(kStringEncodingMask == 4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001621 STATIC_ASSERT(kOneByteStringTag == 4);
Ben Murdoch257744e2011-11-30 15:57:28 +00001622 STATIC_ASSERT(kTwoByteStringTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001623 __ And(a0, a0, Operand(kStringEncodingMask)); // Non-zero for one-byte.
1624 __ lw(t9, FieldMemOperand(regexp_data, JSRegExp::kDataOneByteCodeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001625 __ sra(a3, a0, 2); // a3 is 1 for ASCII, 0 for UC16 (used below).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001626 __ lw(t1, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001627 __ Movz(t9, t1, a0); // If UC16 (a0 is 0), replace t9 w/kDataUC16CodeOffset.
Ben Murdoch257744e2011-11-30 15:57:28 +00001628
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001629 // (E) Carry on. String handling is done.
1630 // t9: irregexp code
Ben Murdoch257744e2011-11-30 15:57:28 +00001631 // Check that the irregexp code has been generated for the actual string
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001632 // encoding. If it has, the field contains a code object otherwise it contains
1633 // a smi (code flushing support).
1634 __ JumpIfSmi(t9, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00001635
Ben Murdoch257744e2011-11-30 15:57:28 +00001636 // a1: previous index
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001637 // a3: encoding of subject string (1 if one_byte, 0 if two_byte);
Ben Murdoch257744e2011-11-30 15:57:28 +00001638 // t9: code
1639 // subject: Subject string
1640 // regexp_data: RegExp data (FixedArray)
1641 // All checks done. Now push arguments for native regexp code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642 __ IncrementCounter(isolate()->counters()->regexp_entry_native(),
Ben Murdoch257744e2011-11-30 15:57:28 +00001643 1, a0, a2);
1644
1645 // Isolates: note we add an additional parameter here (isolate pointer).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001646 const int kRegExpExecuteArguments = 9;
Ben Murdochdb1b4382012-04-26 19:03:50 +01001647 const int kParameterRegisters = 4;
Ben Murdoch257744e2011-11-30 15:57:28 +00001648 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
1649
1650 // Stack pointer now points to cell where return address is to be written.
1651 // Arguments are before that on the stack or in registers, meaning we
1652 // treat the return address as argument 5. Thus every argument after that
1653 // needs to be shifted back by 1. Since DirectCEntryStub will handle
1654 // allocating space for the c argument slots, we don't need to calculate
1655 // that into the argument positions on the stack. This is how the stack will
1656 // look (sp meaning the value of sp at this moment):
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001657 // [sp + 5] - Argument 9
Ben Murdoch257744e2011-11-30 15:57:28 +00001658 // [sp + 4] - Argument 8
1659 // [sp + 3] - Argument 7
1660 // [sp + 2] - Argument 6
1661 // [sp + 1] - Argument 5
1662 // [sp + 0] - saved ra
1663
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664 // Argument 9: Pass current isolate address.
Ben Murdoch257744e2011-11-30 15:57:28 +00001665 // CFunctionArgumentOperand handles MIPS stack argument slots.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001666 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
1667 __ sw(a0, MemOperand(sp, 5 * kPointerSize));
1668
1669 // Argument 8: Indicate that this is a direct call from JavaScript.
1670 __ li(a0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00001671 __ sw(a0, MemOperand(sp, 4 * kPointerSize));
1672
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001673 // Argument 7: Start (high end) of backtracking stack memory area.
Ben Murdoch257744e2011-11-30 15:57:28 +00001674 __ li(a0, Operand(address_of_regexp_stack_memory_address));
1675 __ lw(a0, MemOperand(a0, 0));
1676 __ li(a2, Operand(address_of_regexp_stack_memory_size));
1677 __ lw(a2, MemOperand(a2, 0));
1678 __ addu(a0, a0, a2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001679 __ sw(a0, MemOperand(sp, 3 * kPointerSize));
1680
1681 // Argument 6: Set the number of capture registers to zero to force global
1682 // regexps to behave as non-global. This does not affect non-global regexps.
1683 __ mov(a0, zero_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00001684 __ sw(a0, MemOperand(sp, 2 * kPointerSize));
1685
1686 // Argument 5: static offsets vector buffer.
1687 __ li(a0, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001688 ExternalReference::address_of_static_offsets_vector(isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00001689 __ sw(a0, MemOperand(sp, 1 * kPointerSize));
1690
1691 // For arguments 4 and 3 get string length, calculate start of string data
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001692 // calculate the shift of the index (0 for one-byte and 1 for two-byte).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001693 __ Addu(t2, subject, Operand(SeqString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00001694 __ Xor(a3, a3, Operand(1)); // 1 for 2-byte str, 0 for 1-byte.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001695 // Load the length from the original subject string from the previous stack
1696 // frame. Therefore we have to use fp, which points exactly to two pointer
1697 // sizes below the previous sp. (Because creating a new stack frame pushes
1698 // the previous fp onto the stack and moves up sp by 2 * kPointerSize.)
Ben Murdoch589d6972011-11-30 16:04:58 +00001699 __ lw(subject, MemOperand(fp, kSubjectOffset + 2 * kPointerSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001700 // If slice offset is not 0, load the length from the original sliced string.
1701 // Argument 4, a3: End of string data
1702 // Argument 3, a2: Start of string data
1703 // Prepare start and end index of the input.
1704 __ sllv(t1, t0, a3);
1705 __ addu(t0, t2, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001706 __ sllv(t1, a1, a3);
1707 __ addu(a2, t0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001708
Ben Murdoch589d6972011-11-30 16:04:58 +00001709 __ lw(t2, FieldMemOperand(subject, String::kLengthOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001710 __ sra(t2, t2, kSmiTagSize);
1711 __ sllv(t1, t2, a3);
1712 __ addu(a3, t0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001713 // Argument 2 (a1): Previous index.
1714 // Already there
1715
1716 // Argument 1 (a0): Subject string.
Ben Murdoch589d6972011-11-30 16:04:58 +00001717 __ mov(a0, subject);
Ben Murdoch257744e2011-11-30 15:57:28 +00001718
1719 // Locate the code entry and call it.
1720 __ Addu(t9, t9, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001721 DirectCEntryStub stub(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001722 stub.GenerateCall(masm, t9);
1723
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001724 __ LeaveExitFrame(false, no_reg, true);
Ben Murdoch257744e2011-11-30 15:57:28 +00001725
1726 // v0: result
1727 // subject: subject string (callee saved)
1728 // regexp_data: RegExp data (callee saved)
1729 // last_match_info_elements: Last match info elements (callee saved)
Ben Murdoch257744e2011-11-30 15:57:28 +00001730 // Check the result.
Ben Murdoch257744e2011-11-30 15:57:28 +00001731 Label success;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001732 __ Branch(&success, eq, v0, Operand(1));
1733 // We expect exactly one result since we force the called regexp to behave
1734 // as non-global.
Ben Murdoch257744e2011-11-30 15:57:28 +00001735 Label failure;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001736 __ Branch(&failure, eq, v0, Operand(NativeRegExpMacroAssembler::FAILURE));
Ben Murdoch257744e2011-11-30 15:57:28 +00001737 // If not exception it can only be retry. Handle that in the runtime system.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001738 __ Branch(&runtime, ne, v0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
Ben Murdoch257744e2011-11-30 15:57:28 +00001739 // Result must now be exception. If there is no pending exception already a
1740 // stack overflow (on the backtrack stack) was detected in RegExp code but
1741 // haven't created the exception yet. Handle that in the runtime system.
1742 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 __ li(a1, Operand(isolate()->factory()->the_hole_value()));
Ben Murdoch589d6972011-11-30 16:04:58 +00001744 __ li(a2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001745 isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00001746 __ lw(v0, MemOperand(a2, 0));
Ben Murdoch589d6972011-11-30 16:04:58 +00001747 __ Branch(&runtime, eq, v0, Operand(a1));
Ben Murdoch257744e2011-11-30 15:57:28 +00001748
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001749 // For exception, throw the exception again.
1750 __ TailCallRuntime(Runtime::kRegExpExecReThrow);
Ben Murdoch257744e2011-11-30 15:57:28 +00001751
1752 __ bind(&failure);
1753 // For failure and exception return null.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001754 __ li(v0, Operand(isolate()->factory()->null_value()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001755 __ DropAndRet(4);
Ben Murdoch257744e2011-11-30 15:57:28 +00001756
1757 // Process the result from the native regexp code.
1758 __ bind(&success);
1759 __ lw(a1,
1760 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
1761 // Calculate number of capture registers (number_of_captures + 1) * 2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001762 // Multiplying by 2 comes for free since r1 is smi-tagged.
Ben Murdoch257744e2011-11-30 15:57:28 +00001763 STATIC_ASSERT(kSmiTag == 0);
1764 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
1765 __ Addu(a1, a1, Operand(2)); // a1 was a smi.
1766
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001767 __ lw(a0, MemOperand(sp, kLastMatchInfoOffset));
1768 __ JumpIfSmi(a0, &runtime);
1769 __ GetObjectType(a0, a2, a2);
1770 __ Branch(&runtime, ne, a2, Operand(JS_ARRAY_TYPE));
1771 // Check that the JSArray is in fast case.
1772 __ lw(last_match_info_elements,
1773 FieldMemOperand(a0, JSArray::kElementsOffset));
1774 __ lw(a0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
1775 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
1776 __ Branch(&runtime, ne, a0, Operand(at));
1777 // Check that the last match info has space for the capture registers and the
1778 // additional information.
1779 __ lw(a0,
1780 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
1781 __ Addu(a2, a1, Operand(RegExpImpl::kLastMatchOverhead));
1782 __ sra(at, a0, kSmiTagSize);
1783 __ Branch(&runtime, gt, a2, Operand(at));
1784
Ben Murdoch257744e2011-11-30 15:57:28 +00001785 // a1: number of capture registers
1786 // subject: subject string
1787 // Store the capture count.
1788 __ sll(a2, a1, kSmiTagSize + kSmiShiftSize); // To smi.
1789 __ sw(a2, FieldMemOperand(last_match_info_elements,
1790 RegExpImpl::kLastCaptureCountOffset));
1791 // Store last subject and last input.
Ben Murdoch257744e2011-11-30 15:57:28 +00001792 __ sw(subject,
1793 FieldMemOperand(last_match_info_elements,
1794 RegExpImpl::kLastSubjectOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001795 __ mov(a2, subject);
1796 __ RecordWriteField(last_match_info_elements,
1797 RegExpImpl::kLastSubjectOffset,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001798 subject,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001799 t3,
1800 kRAHasNotBeenSaved,
1801 kDontSaveFPRegs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001802 __ mov(subject, a2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001803 __ sw(subject,
1804 FieldMemOperand(last_match_info_elements,
1805 RegExpImpl::kLastInputOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001806 __ RecordWriteField(last_match_info_elements,
1807 RegExpImpl::kLastInputOffset,
1808 subject,
1809 t3,
1810 kRAHasNotBeenSaved,
1811 kDontSaveFPRegs);
Ben Murdoch257744e2011-11-30 15:57:28 +00001812
1813 // Get the static offsets vector filled by the native regexp code.
1814 ExternalReference address_of_static_offsets_vector =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001815 ExternalReference::address_of_static_offsets_vector(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00001816 __ li(a2, Operand(address_of_static_offsets_vector));
1817
1818 // a1: number of capture registers
1819 // a2: offsets vector
1820 Label next_capture, done;
1821 // Capture register counter starts from number of capture registers and
1822 // counts down until wrapping after zero.
1823 __ Addu(a0,
1824 last_match_info_elements,
1825 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
1826 __ bind(&next_capture);
1827 __ Subu(a1, a1, Operand(1));
1828 __ Branch(&done, lt, a1, Operand(zero_reg));
1829 // Read the value from the static offsets vector buffer.
1830 __ lw(a3, MemOperand(a2, 0));
1831 __ addiu(a2, a2, kPointerSize);
1832 // Store the smi value in the last match info.
1833 __ sll(a3, a3, kSmiTagSize); // Convert to Smi.
1834 __ sw(a3, MemOperand(a0, 0));
1835 __ Branch(&next_capture, USE_DELAY_SLOT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001836 __ addiu(a0, a0, kPointerSize); // In branch delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00001837
1838 __ bind(&done);
1839
1840 // Return last match info.
1841 __ lw(v0, MemOperand(sp, kLastMatchInfoOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001842 __ DropAndRet(4);
1843
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001844 // Do the runtime call to execute the regexp.
1845 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001846 __ TailCallRuntime(Runtime::kRegExpExec);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001847
1848 // Deferred code for string handling.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001849 // (5) Long external string? If not, go to (7).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001850 __ bind(&not_seq_nor_cons);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001851 // Go to (7).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001852 __ Branch(&not_long_external, gt, a1, Operand(kExternalStringTag));
1853
Ben Murdoch097c5b22016-05-18 11:27:45 +01001854 // (6) External string. Make it, offset-wise, look like a sequential string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001855 __ bind(&external_string);
1856 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
1857 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
1858 if (FLAG_debug_code) {
1859 // Assert that we do not have a cons or slice (indirect strings) here.
1860 // Sequential strings have already been ruled out.
1861 __ And(at, a0, Operand(kIsIndirectStringMask));
1862 __ Assert(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001863 kExternalStringExpectedButNotFound,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001864 at,
1865 Operand(zero_reg));
1866 }
1867 __ lw(subject,
1868 FieldMemOperand(subject, ExternalString::kResourceDataOffset));
1869 // Move the pointer so that offset-wise, it looks like a sequential string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001870 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001871 __ Subu(subject,
1872 subject,
1873 SeqTwoByteString::kHeaderSize - kHeapObjectTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001874 __ jmp(&seq_string); // Go to (5).
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001875
Ben Murdoch097c5b22016-05-18 11:27:45 +01001876 // (7) Short external string or not a string? If yes, bail out to runtime.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001877 __ bind(&not_long_external);
1878 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
1879 __ And(at, a1, Operand(kIsNotStringMask | kShortExternalStringMask));
1880 __ Branch(&runtime, ne, at, Operand(zero_reg));
1881
Ben Murdoch097c5b22016-05-18 11:27:45 +01001882 // (8) Sliced string. Replace subject with parent. Go to (4).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001883 // Load offset into t0 and replace subject string with parent.
1884 __ lw(t0, FieldMemOperand(subject, SlicedString::kOffsetOffset));
1885 __ sra(t0, t0, kSmiTagSize);
1886 __ lw(subject, FieldMemOperand(subject, SlicedString::kParentOffset));
1887 __ jmp(&check_underlying); // Go to (4).
Ben Murdoch257744e2011-11-30 15:57:28 +00001888#endif // V8_INTERPRETED_REGEXP
Steve Block44f0eee2011-05-26 01:26:41 +01001889}
1890
1891
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001892static void CallStubInRecordCallTarget(MacroAssembler* masm, CodeStub* stub) {
1893 // a0 : number of arguments to the construct function
1894 // a2 : feedback vector
1895 // a3 : slot in feedback vector (Smi)
1896 // a1 : the function to call
1897 FrameScope scope(masm, StackFrame::INTERNAL);
1898 const RegList kSavedRegs = 1 << 4 | // a0
1899 1 << 5 | // a1
1900 1 << 6 | // a2
1901 1 << 7; // a3
1902
1903 // Number-of-arguments register must be smi-tagged to call out.
1904 __ SmiTag(a0);
1905 __ MultiPush(kSavedRegs);
1906
1907 __ CallStub(stub);
1908
1909 __ MultiPop(kSavedRegs);
1910 __ SmiUntag(a0);
1911}
1912
1913
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001914static void GenerateRecordCallTarget(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001915 // Cache the called function in a feedback vector slot. Cache states
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001916 // are uninitialized, monomorphic (indicated by a JSFunction), and
1917 // megamorphic.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001918 // a0 : number of arguments to the construct function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001919 // a1 : the function to call
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001920 // a2 : feedback vector
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001921 // a3 : slot in feedback vector (Smi)
1922 Label initialize, done, miss, megamorphic, not_array_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001923
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001924 DCHECK_EQ(*TypeFeedbackVector::MegamorphicSentinel(masm->isolate()),
1925 masm->isolate()->heap()->megamorphic_symbol());
1926 DCHECK_EQ(*TypeFeedbackVector::UninitializedSentinel(masm->isolate()),
1927 masm->isolate()->heap()->uninitialized_symbol());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001928
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001929 // Load the cache state into t2.
Ben Murdoch097c5b22016-05-18 11:27:45 +01001930 __ Lsa(t2, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001931 __ lw(t2, FieldMemOperand(t2, FixedArray::kHeaderSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001932
1933 // A monomorphic cache hit or an already megamorphic state: invoke the
1934 // function without changing the state.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001935 // We don't know if t2 is a WeakCell or a Symbol, but it's harmless to read at
1936 // this position in a symbol (see static asserts in type-feedback-vector.h).
1937 Label check_allocation_site;
1938 Register feedback_map = t1;
1939 Register weak_value = t4;
1940 __ lw(weak_value, FieldMemOperand(t2, WeakCell::kValueOffset));
1941 __ Branch(&done, eq, a1, Operand(weak_value));
1942 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
1943 __ Branch(&done, eq, t2, Operand(at));
1944 __ lw(feedback_map, FieldMemOperand(t2, HeapObject::kMapOffset));
1945 __ LoadRoot(at, Heap::kWeakCellMapRootIndex);
1946 __ Branch(&check_allocation_site, ne, feedback_map, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001947
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001948 // If the weak cell is cleared, we have a new chance to become monomorphic.
1949 __ JumpIfSmi(weak_value, &initialize);
1950 __ jmp(&megamorphic);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001951
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001952 __ bind(&check_allocation_site);
1953 // If we came here, we need to see if we are the array function.
1954 // If we didn't have a matching function, and we didn't find the megamorph
1955 // sentinel, then we have in the slot either some other function or an
1956 // AllocationSite.
1957 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
1958 __ Branch(&miss, ne, feedback_map, Operand(at));
1959
1960 // Make sure the function is the Array() function
1961 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, t2);
1962 __ Branch(&megamorphic, ne, a1, Operand(t2));
1963 __ jmp(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001964
1965 __ bind(&miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001966
1967 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
1968 // megamorphic.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001969 __ LoadRoot(at, Heap::kuninitialized_symbolRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001970 __ Branch(&initialize, eq, t2, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001971 // MegamorphicSentinel is an immortal immovable object (undefined) so no
1972 // write-barrier is needed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001973 __ bind(&megamorphic);
Ben Murdoch097c5b22016-05-18 11:27:45 +01001974 __ Lsa(t2, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001975 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001976 __ sw(at, FieldMemOperand(t2, FixedArray::kHeaderSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001977 __ jmp(&done);
1978
1979 // An uninitialized cache is patched with the function.
1980 __ bind(&initialize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001981 // Make sure the function is the Array() function.
1982 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, t2);
1983 __ Branch(&not_array_function, ne, a1, Operand(t2));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001984
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001985 // The target function is the Array constructor,
1986 // Create an AllocationSite if we don't already have it, store it in the
1987 // slot.
1988 CreateAllocationSiteStub create_stub(masm->isolate());
1989 CallStubInRecordCallTarget(masm, &create_stub);
1990 __ Branch(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001991
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001992 __ bind(&not_array_function);
1993 CreateWeakCellStub weak_cell_stub(masm->isolate());
1994 CallStubInRecordCallTarget(masm, &weak_cell_stub);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001995 __ bind(&done);
1996}
1997
1998
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001999void CallConstructStub::Generate(MacroAssembler* masm) {
2000 // a0 : number of arguments
2001 // a1 : the function to call
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002002 // a2 : feedback vector
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002003 // a3 : slot in feedback vector (Smi, for RecordCallTarget)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002004
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002005 Label non_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002006 // Check that the function is not a smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002007 __ JumpIfSmi(a1, &non_function);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002008 // Check that the function is a JSFunction.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002009 __ GetObjectType(a1, t1, t1);
2010 __ Branch(&non_function, ne, t1, Operand(JS_FUNCTION_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002011
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002012 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002013
Ben Murdoch097c5b22016-05-18 11:27:45 +01002014 __ Lsa(t1, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002015 Label feedback_register_initialized;
2016 // Put the AllocationSite from the feedback vector into a2, or undefined.
2017 __ lw(a2, FieldMemOperand(t1, FixedArray::kHeaderSize));
2018 __ lw(t1, FieldMemOperand(a2, AllocationSite::kMapOffset));
2019 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2020 __ Branch(&feedback_register_initialized, eq, t1, Operand(at));
2021 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2022 __ bind(&feedback_register_initialized);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002023
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002024 __ AssertUndefinedOrAllocationSite(a2, t1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002025
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002026 // Pass function as new target.
2027 __ mov(a3, a1);
2028
2029 // Tail call to the function-specific construct stub (still in the caller
2030 // context at this point).
2031 __ lw(t0, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2032 __ lw(t0, FieldMemOperand(t0, SharedFunctionInfo::kConstructStubOffset));
2033 __ Addu(at, t0, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002034 __ Jump(at);
2035
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002036 __ bind(&non_function);
2037 __ mov(a3, a1);
2038 __ Jump(isolate()->builtins()->Construct(), RelocInfo::CODE_TARGET);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002039}
2040
2041
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002042void CallICStub::HandleArrayCase(MacroAssembler* masm, Label* miss) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002043 // a1 - function
2044 // a3 - slot id
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002045 // a2 - vector
2046 // t0 - loaded from vector[slot]
2047 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, at);
2048 __ Branch(miss, ne, a1, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002049
2050 __ li(a0, Operand(arg_count()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002051
2052 // Increment the call count for monomorphic function calls.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002053 __ Lsa(at, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002054 __ lw(a3, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
2055 __ Addu(a3, a3, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2056 __ sw(a3, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002057
2058 __ mov(a2, t0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002059 __ mov(a3, a1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002060 ArrayConstructorStub stub(masm->isolate(), arg_count());
2061 __ TailCallStub(&stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002062}
2063
2064
2065void CallICStub::Generate(MacroAssembler* masm) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002066 // a1 - function
2067 // a3 - slot id (Smi)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002068 // a2 - vector
2069 Label extra_checks_or_miss, call, call_function;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002070 int argc = arg_count();
2071 ParameterCount actual(argc);
2072
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002073 // The checks. First, does r1 match the recorded monomorphic target?
Ben Murdoch097c5b22016-05-18 11:27:45 +01002074 __ Lsa(t0, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002075 __ lw(t0, FieldMemOperand(t0, FixedArray::kHeaderSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002076
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002077 // We don't know that we have a weak cell. We might have a private symbol
2078 // or an AllocationSite, but the memory is safe to examine.
2079 // AllocationSite::kTransitionInfoOffset - contains a Smi or pointer to
2080 // FixedArray.
2081 // WeakCell::kValueOffset - contains a JSFunction or Smi(0)
2082 // Symbol::kHashFieldSlot - if the low bit is 1, then the hash is not
2083 // computed, meaning that it can't appear to be a pointer. If the low bit is
2084 // 0, then hash is computed, but the 0 bit prevents the field from appearing
2085 // to be a pointer.
2086 STATIC_ASSERT(WeakCell::kSize >= kPointerSize);
2087 STATIC_ASSERT(AllocationSite::kTransitionInfoOffset ==
2088 WeakCell::kValueOffset &&
2089 WeakCell::kValueOffset == Symbol::kHashFieldSlot);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002090
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002091 __ lw(t1, FieldMemOperand(t0, WeakCell::kValueOffset));
2092 __ Branch(&extra_checks_or_miss, ne, a1, Operand(t1));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002093
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002094 // The compare above could have been a SMI/SMI comparison. Guard against this
2095 // convincing us that we have a monomorphic JSFunction.
2096 __ JumpIfSmi(a1, &extra_checks_or_miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002097
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002098 // Increment the call count for monomorphic function calls.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002099 __ Lsa(at, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002100 __ lw(a3, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
2101 __ Addu(a3, a3, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2102 __ sw(a3, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002103
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002104 __ bind(&call_function);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002105 __ Jump(masm->isolate()->builtins()->CallFunction(convert_mode(),
2106 tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002107 RelocInfo::CODE_TARGET, al, zero_reg, Operand(zero_reg),
2108 USE_DELAY_SLOT);
2109 __ li(a0, Operand(argc)); // In delay slot.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002110
2111 __ bind(&extra_checks_or_miss);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002112 Label uninitialized, miss, not_allocation_site;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002113
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002114 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002115 __ Branch(&call, eq, t0, Operand(at));
2116
2117 // Verify that t0 contains an AllocationSite
2118 __ lw(t1, FieldMemOperand(t0, HeapObject::kMapOffset));
2119 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2120 __ Branch(&not_allocation_site, ne, t1, Operand(at));
2121
2122 HandleArrayCase(masm, &miss);
2123
2124 __ bind(&not_allocation_site);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002125
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002126 // The following cases attempt to handle MISS cases without going to the
2127 // runtime.
2128 if (FLAG_trace_ic) {
2129 __ Branch(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002130 }
2131
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002132 __ LoadRoot(at, Heap::kuninitialized_symbolRootIndex);
2133 __ Branch(&uninitialized, eq, t0, Operand(at));
2134
2135 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2136 // to handle it here. More complex cases are dealt with in the runtime.
2137 __ AssertNotSmi(t0);
2138 __ GetObjectType(t0, t1, t1);
2139 __ Branch(&miss, ne, t1, Operand(JS_FUNCTION_TYPE));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002140 __ Lsa(t0, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002141 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
2142 __ sw(at, FieldMemOperand(t0, FixedArray::kHeaderSize));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002143
2144 __ bind(&call);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002145 __ Jump(masm->isolate()->builtins()->Call(convert_mode(), tail_call_mode()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002146 RelocInfo::CODE_TARGET, al, zero_reg, Operand(zero_reg),
2147 USE_DELAY_SLOT);
2148 __ li(a0, Operand(argc)); // In delay slot.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002149
2150 __ bind(&uninitialized);
2151
2152 // We are going monomorphic, provided we actually have a JSFunction.
2153 __ JumpIfSmi(a1, &miss);
2154
2155 // Goto miss case if we do not have a function.
2156 __ GetObjectType(a1, t0, t0);
2157 __ Branch(&miss, ne, t0, Operand(JS_FUNCTION_TYPE));
2158
2159 // Make sure the function is not the Array() function, which requires special
2160 // behavior on MISS.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002161 __ LoadNativeContextSlot(Context::ARRAY_FUNCTION_INDEX, t0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002162 __ Branch(&miss, eq, a1, Operand(t0));
2163
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002164 // Make sure the function belongs to the same native context.
2165 __ lw(t0, FieldMemOperand(a1, JSFunction::kContextOffset));
2166 __ lw(t0, ContextMemOperand(t0, Context::NATIVE_CONTEXT_INDEX));
2167 __ lw(t1, NativeContextMemOperand());
2168 __ Branch(&miss, ne, t0, Operand(t1));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002169
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002170 // Initialize the call counter.
Ben Murdoch097c5b22016-05-18 11:27:45 +01002171 __ Lsa(at, a2, a3, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002172 __ li(t0, Operand(Smi::FromInt(CallICNexus::kCallCountIncrement)));
2173 __ sw(t0, FieldMemOperand(at, FixedArray::kHeaderSize + kPointerSize));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002174
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002175 // Store the function. Use a stub since we need a frame for allocation.
2176 // a2 - vector
2177 // a3 - slot
2178 // a1 - function
2179 {
2180 FrameScope scope(masm, StackFrame::INTERNAL);
2181 CreateWeakCellStub create_stub(masm->isolate());
2182 __ Push(a1);
2183 __ CallStub(&create_stub);
2184 __ Pop(a1);
2185 }
2186
2187 __ Branch(&call_function);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002188
2189 // We are here because tracing is on or we encountered a MISS case we can't
2190 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002191 __ bind(&miss);
2192 GenerateMiss(masm);
2193
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002194 __ Branch(&call);
Steve Block44f0eee2011-05-26 01:26:41 +01002195}
2196
2197
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002198void CallICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002199 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002200
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002201 // Push the receiver and the function and feedback info.
2202 __ Push(a1, a2, a3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002203
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002204 // Call the entry.
2205 __ CallRuntime(Runtime::kCallIC_Miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002206
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002207 // Move result to a1 and exit the internal frame.
2208 __ mov(a1, v0);
Steve Block44f0eee2011-05-26 01:26:41 +01002209}
2210
2211
Ben Murdoch257744e2011-11-30 15:57:28 +00002212// StringCharCodeAtGenerator.
Steve Block44f0eee2011-05-26 01:26:41 +01002213void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002214 DCHECK(!t0.is(index_));
2215 DCHECK(!t0.is(result_));
2216 DCHECK(!t0.is(object_));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002217 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2218 // If the receiver is a smi trigger the non-string case.
2219 __ JumpIfSmi(object_, receiver_not_string_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002220
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002221 // Fetch the instance type of the receiver into result register.
2222 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
2223 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
2224 // If the receiver is not a string trigger the non-string case.
2225 __ And(t0, result_, Operand(kIsNotStringMask));
2226 __ Branch(receiver_not_string_, ne, t0, Operand(zero_reg));
2227 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002228
2229 // If the index is non-smi trigger the non-smi case.
2230 __ JumpIfNotSmi(index_, &index_not_smi_);
2231
Ben Murdoch257744e2011-11-30 15:57:28 +00002232 __ bind(&got_smi_index_);
2233
2234 // Check for index out of range.
2235 __ lw(t0, FieldMemOperand(object_, String::kLengthOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002236 __ Branch(index_out_of_range_, ls, t0, Operand(index_));
Ben Murdoch257744e2011-11-30 15:57:28 +00002237
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002238 __ sra(index_, index_, kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00002239
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002240 StringCharLoadGenerator::Generate(masm,
2241 object_,
2242 index_,
2243 result_,
2244 &call_runtime_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002245
Ben Murdoch257744e2011-11-30 15:57:28 +00002246 __ sll(result_, result_, kSmiTagSize);
2247 __ bind(&exit_);
Steve Block44f0eee2011-05-26 01:26:41 +01002248}
2249
2250
2251void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002252 MacroAssembler* masm, EmbedMode embed_mode,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002253 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002254 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
Ben Murdoch257744e2011-11-30 15:57:28 +00002255
2256 // Index is not a smi.
2257 __ bind(&index_not_smi_);
2258 // If index is a heap number, try converting it to an integer.
2259 __ CheckMap(index_,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002260 result_,
Ben Murdoch257744e2011-11-30 15:57:28 +00002261 Heap::kHeapNumberMapRootIndex,
2262 index_not_number_,
2263 DONT_DO_SMI_CHECK);
2264 call_helper.BeforeCall(masm);
2265 // Consumed by runtime conversion function:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002266 if (embed_mode == PART_OF_IC_HANDLER) {
2267 __ Push(LoadWithVectorDescriptor::VectorRegister(),
2268 LoadWithVectorDescriptor::SlotRegister(), object_, index_);
2269 } else {
2270 __ Push(object_, index_);
2271 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002272 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002273 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero);
Ben Murdoch257744e2011-11-30 15:57:28 +00002274 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002275 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
Ben Murdoch257744e2011-11-30 15:57:28 +00002276 // NumberToSmi discards numbers that are not exact integers.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002277 __ CallRuntime(Runtime::kNumberToSmi);
Ben Murdoch257744e2011-11-30 15:57:28 +00002278 }
2279
2280 // Save the conversion result before the pop instructions below
2281 // have a chance to overwrite it.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002282 __ Move(index_, v0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002283 if (embed_mode == PART_OF_IC_HANDLER) {
2284 __ Pop(LoadWithVectorDescriptor::VectorRegister(),
2285 LoadWithVectorDescriptor::SlotRegister(), object_);
2286 } else {
2287 __ pop(object_);
2288 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002289 // Reload the instance type.
2290 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
2291 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
2292 call_helper.AfterCall(masm);
2293 // If index is still not a smi, it must be out of range.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002294 __ JumpIfNotSmi(index_, index_out_of_range_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002295 // Otherwise, return to the fast path.
2296 __ Branch(&got_smi_index_);
2297
2298 // Call runtime. We get here when the receiver is a string and the
2299 // index is a number, but the code of getting the actual character
2300 // is too complex (e.g., when the string needs to be flattened).
2301 __ bind(&call_runtime_);
2302 call_helper.BeforeCall(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002303 __ sll(index_, index_, kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00002304 __ Push(object_, index_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002305 __ CallRuntime(Runtime::kStringCharCodeAtRT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002306
2307 __ Move(result_, v0);
2308
2309 call_helper.AfterCall(masm);
2310 __ jmp(&exit_);
2311
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002312 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
Steve Block44f0eee2011-05-26 01:26:41 +01002313}
2314
2315
2316// -------------------------------------------------------------------------
2317// StringCharFromCodeGenerator
2318
2319void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002320 // Fast case of Heap::LookupSingleCharacterStringFromCode.
2321
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002322 DCHECK(!t0.is(result_));
2323 DCHECK(!t0.is(code_));
Ben Murdoch257744e2011-11-30 15:57:28 +00002324
2325 STATIC_ASSERT(kSmiTag == 0);
2326 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002327 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCodeU + 1));
2328 __ And(t0, code_, Operand(kSmiTagMask |
2329 ((~String::kMaxOneByteCharCodeU) << kSmiTagSize)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002330 __ Branch(&slow_case_, ne, t0, Operand(zero_reg));
2331
2332 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002333 // At this point code register contains smi tagged one-byte char code.
Ben Murdoch257744e2011-11-30 15:57:28 +00002334 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002335 __ Lsa(result_, result_, code_, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00002336 __ lw(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
2337 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
2338 __ Branch(&slow_case_, eq, result_, Operand(t0));
2339 __ bind(&exit_);
Steve Block44f0eee2011-05-26 01:26:41 +01002340}
2341
2342
2343void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002344 MacroAssembler* masm,
2345 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002346 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
Ben Murdoch257744e2011-11-30 15:57:28 +00002347
2348 __ bind(&slow_case_);
2349 call_helper.BeforeCall(masm);
2350 __ push(code_);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002351 __ CallRuntime(Runtime::kStringCharFromCode);
Ben Murdoch257744e2011-11-30 15:57:28 +00002352 __ Move(result_, v0);
2353
2354 call_helper.AfterCall(masm);
2355 __ Branch(&exit_);
2356
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002357 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
Steve Block44f0eee2011-05-26 01:26:41 +01002358}
2359
2360
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002361enum CopyCharactersFlags { COPY_ONE_BYTE = 1, DEST_ALWAYS_ALIGNED = 2 };
Steve Block44f0eee2011-05-26 01:26:41 +01002362
2363
Steve Block44f0eee2011-05-26 01:26:41 +01002364void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
2365 Register dest,
2366 Register src,
2367 Register count,
2368 Register scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002369 String::Encoding encoding) {
2370 if (FLAG_debug_code) {
2371 // Check that destination is word aligned.
2372 __ And(scratch, dest, Operand(kPointerAlignmentMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00002373 __ Check(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002374 kDestinationOfCopyNotAligned,
2375 scratch,
Ben Murdoch257744e2011-11-30 15:57:28 +00002376 Operand(zero_reg));
2377 }
2378
Ben Murdoch257744e2011-11-30 15:57:28 +00002379 // Assumes word reads and writes are little endian.
2380 // Nothing to do for zero characters.
2381 Label done;
2382
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002383 if (encoding == String::TWO_BYTE_ENCODING) {
2384 __ Addu(count, count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00002385 }
2386
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002387 Register limit = count; // Read until dest equals this.
2388 __ Addu(limit, dest, Operand(count));
Ben Murdoch257744e2011-11-30 15:57:28 +00002389
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002390 Label loop_entry, loop;
Ben Murdoch257744e2011-11-30 15:57:28 +00002391 // Copy bytes from src to dest until dest hits limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002392 __ Branch(&loop_entry);
2393 __ bind(&loop);
2394 __ lbu(scratch, MemOperand(src));
2395 __ Addu(src, src, Operand(1));
2396 __ sb(scratch, MemOperand(dest));
2397 __ Addu(dest, dest, Operand(1));
2398 __ bind(&loop_entry);
2399 __ Branch(&loop, lt, dest, Operand(limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00002400
2401 __ bind(&done);
Steve Block44f0eee2011-05-26 01:26:41 +01002402}
2403
2404
Steve Block44f0eee2011-05-26 01:26:41 +01002405void SubStringStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002406 Label runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00002407 // Stack frame on entry.
2408 // ra: return address
2409 // sp[0]: to
2410 // sp[4]: from
2411 // sp[8]: string
2412
2413 // This stub is called from the native-call %_SubString(...), so
2414 // nothing can be assumed about the arguments. It is tested that:
2415 // "string" is a sequential string,
2416 // both "from" and "to" are smis, and
2417 // 0 <= from <= to <= string.length.
2418 // If any of these assumptions fail, we call the runtime system.
2419
Ben Murdochdb1b4382012-04-26 19:03:50 +01002420 const int kToOffset = 0 * kPointerSize;
2421 const int kFromOffset = 1 * kPointerSize;
2422 const int kStringOffset = 2 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00002423
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002424 __ lw(a2, MemOperand(sp, kToOffset));
2425 __ lw(a3, MemOperand(sp, kFromOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002426 STATIC_ASSERT(kFromOffset == kToOffset + 4);
2427 STATIC_ASSERT(kSmiTag == 0);
2428 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2429
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002430 // Utilize delay slots. SmiUntag doesn't emit a jump, everything else is
2431 // safe in this case.
2432 __ UntagAndJumpIfNotSmi(a2, a2, &runtime);
2433 __ UntagAndJumpIfNotSmi(a3, a3, &runtime);
2434 // Both a2 and a3 are untagged integers.
Ben Murdoch257744e2011-11-30 15:57:28 +00002435
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002436 __ Branch(&runtime, lt, a3, Operand(zero_reg)); // From < 0.
Ben Murdoch257744e2011-11-30 15:57:28 +00002437
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002438 __ Branch(&runtime, gt, a3, Operand(a2)); // Fail if from > to.
2439 __ Subu(a2, a2, a3);
Ben Murdoch257744e2011-11-30 15:57:28 +00002440
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002441 // Make sure first argument is a string.
Ben Murdoch589d6972011-11-30 16:04:58 +00002442 __ lw(v0, MemOperand(sp, kStringOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002443 __ JumpIfSmi(v0, &runtime);
Ben Murdoch589d6972011-11-30 16:04:58 +00002444 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002445 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002446 __ And(t0, a1, Operand(kIsNotStringMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00002447
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002448 __ Branch(&runtime, ne, t0, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00002449
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002450 Label single_char;
2451 __ Branch(&single_char, eq, a2, Operand(1));
2452
Ben Murdoch589d6972011-11-30 16:04:58 +00002453 // Short-cut for the case of trivial substring.
2454 Label return_v0;
2455 // v0: original string
2456 // a2: result string length
2457 __ lw(t0, FieldMemOperand(v0, String::kLengthOffset));
2458 __ sra(t0, t0, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002459 // Return original string.
Ben Murdoch589d6972011-11-30 16:04:58 +00002460 __ Branch(&return_v0, eq, a2, Operand(t0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002461 // Longer than original string's length or negative: unsafe arguments.
2462 __ Branch(&runtime, hi, a2, Operand(t0));
2463 // Shorter than original string's length: an actual substring.
Ben Murdoch257744e2011-11-30 15:57:28 +00002464
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002465 // Deal with different string types: update the index if necessary
2466 // and put the underlying string into t1.
2467 // v0: original string
2468 // a1: instance type
2469 // a2: length
2470 // a3: from index (untagged)
2471 Label underlying_unpacked, sliced_string, seq_or_external_string;
2472 // If the string is not indirect, it can only be sequential or external.
2473 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
2474 STATIC_ASSERT(kIsIndirectStringMask != 0);
2475 __ And(t0, a1, Operand(kIsIndirectStringMask));
2476 __ Branch(USE_DELAY_SLOT, &seq_or_external_string, eq, t0, Operand(zero_reg));
2477 // t0 is used as a scratch register and can be overwritten in either case.
2478 __ And(t0, a1, Operand(kSlicedNotConsMask));
2479 __ Branch(&sliced_string, ne, t0, Operand(zero_reg));
2480 // Cons string. Check whether it is flat, then fetch first part.
2481 __ lw(t1, FieldMemOperand(v0, ConsString::kSecondOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002482 __ LoadRoot(t0, Heap::kempty_stringRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002483 __ Branch(&runtime, ne, t1, Operand(t0));
2484 __ lw(t1, FieldMemOperand(v0, ConsString::kFirstOffset));
2485 // Update instance type.
2486 __ lw(a1, FieldMemOperand(t1, HeapObject::kMapOffset));
2487 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
2488 __ jmp(&underlying_unpacked);
Ben Murdoch257744e2011-11-30 15:57:28 +00002489
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002490 __ bind(&sliced_string);
2491 // Sliced string. Fetch parent and correct start index by offset.
2492 __ lw(t1, FieldMemOperand(v0, SlicedString::kParentOffset));
2493 __ lw(t0, FieldMemOperand(v0, SlicedString::kOffsetOffset));
2494 __ sra(t0, t0, 1); // Add offset to index.
2495 __ Addu(a3, a3, t0);
2496 // Update instance type.
2497 __ lw(a1, FieldMemOperand(t1, HeapObject::kMapOffset));
2498 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
2499 __ jmp(&underlying_unpacked);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002500
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002501 __ bind(&seq_or_external_string);
2502 // Sequential or external string. Just move string to the expected register.
2503 __ mov(t1, v0);
2504
2505 __ bind(&underlying_unpacked);
2506
2507 if (FLAG_string_slices) {
2508 Label copy_routine;
2509 // t1: underlying subject string
2510 // a1: instance type of underlying subject string
2511 // a2: length
2512 // a3: adjusted start index (untagged)
2513 // Short slice. Copy instead of slicing.
2514 __ Branch(&copy_routine, lt, a2, Operand(SlicedString::kMinLength));
2515 // Allocate new sliced string. At this point we do not reload the instance
2516 // type including the string encoding because we simply rely on the info
2517 // provided by the original string. It does not matter if the original
2518 // string's encoding is wrong because we always have to recheck encoding of
2519 // the newly created string's parent anyways due to externalized strings.
2520 Label two_byte_slice, set_slice_header;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002521 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002522 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
2523 __ And(t0, a1, Operand(kStringEncodingMask));
2524 __ Branch(&two_byte_slice, eq, t0, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002525 __ AllocateOneByteSlicedString(v0, a2, t2, t3, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002526 __ jmp(&set_slice_header);
2527 __ bind(&two_byte_slice);
2528 __ AllocateTwoByteSlicedString(v0, a2, t2, t3, &runtime);
2529 __ bind(&set_slice_header);
2530 __ sll(a3, a3, 1);
2531 __ sw(t1, FieldMemOperand(v0, SlicedString::kParentOffset));
2532 __ sw(a3, FieldMemOperand(v0, SlicedString::kOffsetOffset));
2533 __ jmp(&return_v0);
2534
2535 __ bind(&copy_routine);
2536 }
2537
2538 // t1: underlying subject string
2539 // a1: instance type of underlying subject string
2540 // a2: length
2541 // a3: adjusted start index (untagged)
2542 Label two_byte_sequential, sequential_string, allocate_result;
2543 STATIC_ASSERT(kExternalStringTag != 0);
2544 STATIC_ASSERT(kSeqStringTag == 0);
2545 __ And(t0, a1, Operand(kExternalStringTag));
2546 __ Branch(&sequential_string, eq, t0, Operand(zero_reg));
2547
2548 // Handle external string.
2549 // Rule out short external strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002550 STATIC_ASSERT(kShortExternalStringTag != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002551 __ And(t0, a1, Operand(kShortExternalStringTag));
2552 __ Branch(&runtime, ne, t0, Operand(zero_reg));
2553 __ lw(t1, FieldMemOperand(t1, ExternalString::kResourceDataOffset));
2554 // t1 already points to the first character of underlying string.
2555 __ jmp(&allocate_result);
2556
2557 __ bind(&sequential_string);
2558 // Locate first character of underlying subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002559 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2560 __ Addu(t1, t1, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002561
2562 __ bind(&allocate_result);
2563 // Sequential acii string. Allocate the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002564 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002565 __ And(t0, a1, Operand(kStringEncodingMask));
2566 __ Branch(&two_byte_sequential, eq, t0, Operand(zero_reg));
2567
2568 // Allocate and copy the resulting ASCII string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002569 __ AllocateOneByteString(v0, a2, t0, t2, t3, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002570
2571 // Locate first character of substring to copy.
2572 __ Addu(t1, t1, a3);
2573
Ben Murdoch257744e2011-11-30 15:57:28 +00002574 // Locate first character of result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002575 __ Addu(a1, v0, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00002576
Ben Murdoch589d6972011-11-30 16:04:58 +00002577 // v0: result string
2578 // a1: first character of result string
2579 // a2: result string length
2580 // t1: first character of substring to copy
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002581 STATIC_ASSERT((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
2582 StringHelper::GenerateCopyCharacters(
2583 masm, a1, t1, a2, a3, String::ONE_BYTE_ENCODING);
Ben Murdoch589d6972011-11-30 16:04:58 +00002584 __ jmp(&return_v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002585
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002586 // Allocate and copy the resulting two-byte string.
2587 __ bind(&two_byte_sequential);
2588 __ AllocateTwoByteString(v0, a2, t0, t2, t3, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00002589
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002590 // Locate first character of substring to copy.
Ben Murdoch589d6972011-11-30 16:04:58 +00002591 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002592 __ Lsa(t1, t1, a3, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002593 // Locate first character of result.
2594 __ Addu(a1, v0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch589d6972011-11-30 16:04:58 +00002595
Ben Murdoch257744e2011-11-30 15:57:28 +00002596 // v0: result string.
2597 // a1: first character of result.
2598 // a2: result length.
Ben Murdoch589d6972011-11-30 16:04:58 +00002599 // t1: first character of substring to copy.
Ben Murdoch257744e2011-11-30 15:57:28 +00002600 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002601 StringHelper::GenerateCopyCharacters(
2602 masm, a1, t1, a2, a3, String::TWO_BYTE_ENCODING);
Ben Murdoch589d6972011-11-30 16:04:58 +00002603
2604 __ bind(&return_v0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002605 Counters* counters = isolate()->counters();
Ben Murdoch257744e2011-11-30 15:57:28 +00002606 __ IncrementCounter(counters->sub_string_native(), 1, a3, t0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002607 __ DropAndRet(3);
Ben Murdoch257744e2011-11-30 15:57:28 +00002608
2609 // Just jump to runtime to create the sub string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002610 __ bind(&runtime);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002611 __ TailCallRuntime(Runtime::kSubString);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002612
2613 __ bind(&single_char);
2614 // v0: original string
2615 // a1: instance type
2616 // a2: length
2617 // a3: from index (untagged)
2618 __ SmiTag(a3, a3);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002619 StringCharAtGenerator generator(v0, a3, a2, v0, &runtime, &runtime, &runtime,
2620 STRING_INDEX_IS_NUMBER, RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002621 generator.GenerateFast(masm);
2622 __ DropAndRet(3);
2623 generator.SkipSlow(masm, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00002624}
2625
2626
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002627void ToNumberStub::Generate(MacroAssembler* masm) {
2628 // The ToNumber stub takes one argument in a0.
2629 Label not_smi;
2630 __ JumpIfNotSmi(a0, &not_smi);
2631 __ Ret(USE_DELAY_SLOT);
2632 __ mov(v0, a0);
2633 __ bind(&not_smi);
2634
2635 Label not_heap_number;
Ben Murdochda12d292016-06-02 14:46:10 +01002636 __ GetObjectType(a0, a1, a1);
2637 // a0: receiver
2638 // a1: receiver instance type
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002639 __ Branch(&not_heap_number, ne, a1, Operand(HEAP_NUMBER_TYPE));
2640 __ Ret(USE_DELAY_SLOT);
2641 __ mov(v0, a0);
2642 __ bind(&not_heap_number);
2643
Ben Murdochda12d292016-06-02 14:46:10 +01002644 NonNumberToNumberStub stub(masm->isolate());
2645 __ TailCallStub(&stub);
2646}
2647
2648void NonNumberToNumberStub::Generate(MacroAssembler* masm) {
2649 // The NonNumberToNumber stub takes on argument in a0.
2650 __ AssertNotNumber(a0);
2651
2652 Label not_string;
2653 __ GetObjectType(a0, a1, a1);
2654 // a0: receiver
2655 // a1: receiver instance type
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002656 __ Branch(&not_string, hs, a1, Operand(FIRST_NONSTRING_TYPE));
Ben Murdochda12d292016-06-02 14:46:10 +01002657 StringToNumberStub stub(masm->isolate());
2658 __ TailCallStub(&stub);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002659 __ bind(&not_string);
2660
2661 Label not_oddball;
2662 __ Branch(&not_oddball, ne, a1, Operand(ODDBALL_TYPE));
2663 __ Ret(USE_DELAY_SLOT);
Ben Murdochda12d292016-06-02 14:46:10 +01002664 __ lw(v0, FieldMemOperand(a0, Oddball::kToNumberOffset)); // In delay slot.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002665 __ bind(&not_oddball);
2666
Ben Murdochda12d292016-06-02 14:46:10 +01002667 __ Push(a0); // Push argument.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002668 __ TailCallRuntime(Runtime::kToNumber);
2669}
2670
Ben Murdochda12d292016-06-02 14:46:10 +01002671void StringToNumberStub::Generate(MacroAssembler* masm) {
2672 // The StringToNumber stub takes on argument in a0.
2673 __ AssertString(a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002674
Ben Murdochda12d292016-06-02 14:46:10 +01002675 // Check if string has a cached array index.
2676 Label runtime;
2677 __ lw(a2, FieldMemOperand(a0, String::kHashFieldOffset));
2678 __ And(at, a2, Operand(String::kContainsCachedArrayIndexMask));
2679 __ Branch(&runtime, ne, at, Operand(zero_reg));
2680 __ IndexFromHash(a2, v0);
2681 __ Ret();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002682
Ben Murdochda12d292016-06-02 14:46:10 +01002683 __ bind(&runtime);
2684 __ Push(a0); // Push argument.
2685 __ TailCallRuntime(Runtime::kStringToNumber);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002686}
2687
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002688void ToStringStub::Generate(MacroAssembler* masm) {
2689 // The ToString stub takes on argument in a0.
2690 Label is_number;
2691 __ JumpIfSmi(a0, &is_number);
2692
2693 Label not_string;
2694 __ GetObjectType(a0, a1, a1);
2695 // a0: receiver
2696 // a1: receiver instance type
2697 __ Branch(&not_string, ge, a1, Operand(FIRST_NONSTRING_TYPE));
2698 __ Ret(USE_DELAY_SLOT);
2699 __ mov(v0, a0);
2700 __ bind(&not_string);
2701
2702 Label not_heap_number;
2703 __ Branch(&not_heap_number, ne, a1, Operand(HEAP_NUMBER_TYPE));
2704 __ bind(&is_number);
2705 NumberToStringStub stub(isolate());
2706 __ TailCallStub(&stub);
2707 __ bind(&not_heap_number);
2708
2709 Label not_oddball;
2710 __ Branch(&not_oddball, ne, a1, Operand(ODDBALL_TYPE));
2711 __ Ret(USE_DELAY_SLOT);
2712 __ lw(v0, FieldMemOperand(a0, Oddball::kToStringOffset));
2713 __ bind(&not_oddball);
2714
2715 __ push(a0); // Push argument.
2716 __ TailCallRuntime(Runtime::kToString);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002717}
2718
2719
Ben Murdoch097c5b22016-05-18 11:27:45 +01002720void ToNameStub::Generate(MacroAssembler* masm) {
2721 // The ToName stub takes on argument in a0.
2722 Label is_number;
2723 __ JumpIfSmi(a0, &is_number);
2724
2725 Label not_name;
2726 STATIC_ASSERT(FIRST_NAME_TYPE == FIRST_TYPE);
2727 __ GetObjectType(a0, a1, a1);
2728 // a0: receiver
2729 // a1: receiver instance type
2730 __ Branch(&not_name, gt, a1, Operand(LAST_NAME_TYPE));
2731 __ Ret(USE_DELAY_SLOT);
2732 __ mov(v0, a0);
2733 __ bind(&not_name);
2734
2735 Label not_heap_number;
2736 __ Branch(&not_heap_number, ne, a1, Operand(HEAP_NUMBER_TYPE));
2737 __ bind(&is_number);
2738 NumberToStringStub stub(isolate());
2739 __ TailCallStub(&stub);
2740 __ bind(&not_heap_number);
2741
2742 Label not_oddball;
2743 __ Branch(&not_oddball, ne, a1, Operand(ODDBALL_TYPE));
2744 __ Ret(USE_DELAY_SLOT);
2745 __ lw(v0, FieldMemOperand(a0, Oddball::kToStringOffset));
2746 __ bind(&not_oddball);
2747
2748 __ push(a0); // Push argument.
2749 __ TailCallRuntime(Runtime::kToName);
2750}
2751
2752
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002753void StringHelper::GenerateFlatOneByteStringEquals(
2754 MacroAssembler* masm, Register left, Register right, Register scratch1,
2755 Register scratch2, Register scratch3) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002756 Register length = scratch1;
2757
2758 // Compare lengths.
2759 Label strings_not_equal, check_zero_length;
2760 __ lw(length, FieldMemOperand(left, String::kLengthOffset));
2761 __ lw(scratch2, FieldMemOperand(right, String::kLengthOffset));
2762 __ Branch(&check_zero_length, eq, length, Operand(scratch2));
2763 __ bind(&strings_not_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002764 DCHECK(is_int16(NOT_EQUAL));
2765 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002766 __ li(v0, Operand(Smi::FromInt(NOT_EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002767
2768 // Check if the length is zero.
2769 Label compare_chars;
2770 __ bind(&check_zero_length);
2771 STATIC_ASSERT(kSmiTag == 0);
2772 __ Branch(&compare_chars, ne, length, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002773 DCHECK(is_int16(EQUAL));
2774 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002775 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00002776
2777 // Compare characters.
2778 __ bind(&compare_chars);
2779
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002780 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2, scratch3,
2781 v0, &strings_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00002782
2783 // Characters are equal.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002784 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002785 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01002786}
2787
2788
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002789void StringHelper::GenerateCompareFlatOneByteStrings(
2790 MacroAssembler* masm, Register left, Register right, Register scratch1,
2791 Register scratch2, Register scratch3, Register scratch4) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002792 Label result_not_equal, compare_lengths;
2793 // Find minimum length and length difference.
2794 __ lw(scratch1, FieldMemOperand(left, String::kLengthOffset));
2795 __ lw(scratch2, FieldMemOperand(right, String::kLengthOffset));
2796 __ Subu(scratch3, scratch1, Operand(scratch2));
2797 Register length_delta = scratch3;
2798 __ slt(scratch4, scratch2, scratch1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002799 __ Movn(scratch1, scratch2, scratch4);
Ben Murdoch257744e2011-11-30 15:57:28 +00002800 Register min_length = scratch1;
2801 STATIC_ASSERT(kSmiTag == 0);
2802 __ Branch(&compare_lengths, eq, min_length, Operand(zero_reg));
2803
2804 // Compare loop.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002805 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
2806 scratch4, v0, &result_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00002807
2808 // Compare lengths - strings up to min-length are equal.
2809 __ bind(&compare_lengths);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002810 DCHECK(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002811 // Use length_delta as result if it's zero.
2812 __ mov(scratch2, length_delta);
2813 __ mov(scratch4, zero_reg);
2814 __ mov(v0, zero_reg);
2815
2816 __ bind(&result_not_equal);
2817 // Conditionally update the result based either on length_delta or
2818 // the last comparion performed in the loop above.
2819 Label ret;
2820 __ Branch(&ret, eq, scratch2, Operand(scratch4));
2821 __ li(v0, Operand(Smi::FromInt(GREATER)));
2822 __ Branch(&ret, gt, scratch2, Operand(scratch4));
2823 __ li(v0, Operand(Smi::FromInt(LESS)));
2824 __ bind(&ret);
2825 __ Ret();
2826}
2827
2828
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002829void StringHelper::GenerateOneByteCharsCompareLoop(
2830 MacroAssembler* masm, Register left, Register right, Register length,
2831 Register scratch1, Register scratch2, Register scratch3,
Ben Murdoch257744e2011-11-30 15:57:28 +00002832 Label* chars_not_equal) {
2833 // Change index to run from -length to -1 by adding length to string
2834 // start. This means that loop ends when index reaches zero, which
2835 // doesn't need an additional compare.
2836 __ SmiUntag(length);
2837 __ Addu(scratch1, length,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002838 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00002839 __ Addu(left, left, Operand(scratch1));
2840 __ Addu(right, right, Operand(scratch1));
2841 __ Subu(length, zero_reg, length);
2842 Register index = length; // index = -length;
2843
2844
2845 // Compare loop.
2846 Label loop;
2847 __ bind(&loop);
2848 __ Addu(scratch3, left, index);
2849 __ lbu(scratch1, MemOperand(scratch3));
2850 __ Addu(scratch3, right, index);
2851 __ lbu(scratch2, MemOperand(scratch3));
2852 __ Branch(chars_not_equal, ne, scratch1, Operand(scratch2));
2853 __ Addu(index, index, 1);
2854 __ Branch(&loop, ne, index, Operand(zero_reg));
Steve Block44f0eee2011-05-26 01:26:41 +01002855}
2856
2857
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002858void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
2859 // ----------- S t a t e -------------
2860 // -- a1 : left
2861 // -- a0 : right
2862 // -- ra : return address
2863 // -----------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00002864
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002865 // Load a2 with the allocation site. We stick an undefined dummy value here
2866 // and replace it with the real allocation site later when we instantiate this
2867 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
2868 __ li(a2, handle(isolate()->heap()->undefined_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00002869
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002870 // Make sure that we actually patched the allocation site.
2871 if (FLAG_debug_code) {
2872 __ And(at, a2, Operand(kSmiTagMask));
2873 __ Assert(ne, kExpectedAllocationSite, at, Operand(zero_reg));
2874 __ lw(t0, FieldMemOperand(a2, HeapObject::kMapOffset));
2875 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2876 __ Assert(eq, kExpectedAllocationSite, t0, Operand(at));
Ben Murdoch257744e2011-11-30 15:57:28 +00002877 }
2878
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002879 // Tail call into the stub that handles binary operations with allocation
2880 // sites.
2881 BinaryOpWithAllocationSiteStub stub(isolate(), state());
2882 __ TailCallStub(&stub);
Ben Murdoch257744e2011-11-30 15:57:28 +00002883}
2884
2885
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002886void CompareICStub::GenerateBooleans(MacroAssembler* masm) {
2887 DCHECK_EQ(CompareICState::BOOLEAN, state());
2888 Label miss;
2889
2890 __ CheckMap(a1, a2, Heap::kBooleanMapRootIndex, &miss, DO_SMI_CHECK);
2891 __ CheckMap(a0, a3, Heap::kBooleanMapRootIndex, &miss, DO_SMI_CHECK);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002892 if (!Token::IsEqualityOp(op())) {
2893 __ lw(a1, FieldMemOperand(a1, Oddball::kToNumberOffset));
2894 __ AssertSmi(a1);
2895 __ lw(a0, FieldMemOperand(a0, Oddball::kToNumberOffset));
2896 __ AssertSmi(a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002897 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01002898 __ Ret(USE_DELAY_SLOT);
2899 __ Subu(v0, a1, a0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002900
2901 __ bind(&miss);
2902 GenerateMiss(masm);
2903}
2904
2905
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002906void CompareICStub::GenerateSmis(MacroAssembler* masm) {
2907 DCHECK(state() == CompareICState::SMI);
Ben Murdoch257744e2011-11-30 15:57:28 +00002908 Label miss;
2909 __ Or(a2, a1, a0);
2910 __ JumpIfNotSmi(a2, &miss);
2911
2912 if (GetCondition() == eq) {
2913 // For equality we do not care about the sign of the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002914 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002915 __ Subu(v0, a0, a1);
2916 } else {
2917 // Untag before subtracting to avoid handling overflow.
2918 __ SmiUntag(a1);
2919 __ SmiUntag(a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002920 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00002921 __ Subu(v0, a1, a0);
2922 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002923
2924 __ bind(&miss);
2925 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01002926}
2927
2928
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002929void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
2930 DCHECK(state() == CompareICState::NUMBER);
Ben Murdoch257744e2011-11-30 15:57:28 +00002931
2932 Label generic_stub;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002933 Label unordered, maybe_undefined1, maybe_undefined2;
Ben Murdoch257744e2011-11-30 15:57:28 +00002934 Label miss;
Ben Murdoch257744e2011-11-30 15:57:28 +00002935
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002936 if (left() == CompareICState::SMI) {
2937 __ JumpIfNotSmi(a1, &miss);
2938 }
2939 if (right() == CompareICState::SMI) {
2940 __ JumpIfNotSmi(a0, &miss);
Ben Murdoch85b71792012-04-11 18:30:58 +01002941 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01002942
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002943 // Inlining the double comparison and falling back to the general compare
2944 // stub if NaN is involved.
2945 // Load left and right operand.
2946 Label done, left, left_smi, right_smi;
2947 __ JumpIfSmi(a0, &right_smi);
2948 __ CheckMap(a0, a2, Heap::kHeapNumberMapRootIndex, &maybe_undefined1,
2949 DONT_DO_SMI_CHECK);
2950 __ Subu(a2, a0, Operand(kHeapObjectTag));
2951 __ ldc1(f2, MemOperand(a2, HeapNumber::kValueOffset));
2952 __ Branch(&left);
2953 __ bind(&right_smi);
2954 __ SmiUntag(a2, a0); // Can't clobber a0 yet.
2955 FPURegister single_scratch = f6;
2956 __ mtc1(a2, single_scratch);
2957 __ cvt_d_w(f2, single_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002958
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002959 __ bind(&left);
2960 __ JumpIfSmi(a1, &left_smi);
2961 __ CheckMap(a1, a2, Heap::kHeapNumberMapRootIndex, &maybe_undefined2,
2962 DONT_DO_SMI_CHECK);
2963 __ Subu(a2, a1, Operand(kHeapObjectTag));
2964 __ ldc1(f0, MemOperand(a2, HeapNumber::kValueOffset));
2965 __ Branch(&done);
2966 __ bind(&left_smi);
2967 __ SmiUntag(a2, a1); // Can't clobber a1 yet.
2968 single_scratch = f8;
2969 __ mtc1(a2, single_scratch);
2970 __ cvt_d_w(f0, single_scratch);
2971
2972 __ bind(&done);
2973
2974 // Return a result of -1, 0, or 1, or use CompareStub for NaNs.
2975 Label fpu_eq, fpu_lt;
2976 // Test if equal, and also handle the unordered/NaN case.
2977 __ BranchF(&fpu_eq, &unordered, eq, f0, f2);
2978
2979 // Test if less (unordered case is already handled).
2980 __ BranchF(&fpu_lt, NULL, lt, f0, f2);
2981
2982 // Otherwise it's greater, so just fall thru, and return.
2983 DCHECK(is_int16(GREATER) && is_int16(EQUAL) && is_int16(LESS));
2984 __ Ret(USE_DELAY_SLOT);
2985 __ li(v0, Operand(GREATER));
2986
2987 __ bind(&fpu_eq);
2988 __ Ret(USE_DELAY_SLOT);
2989 __ li(v0, Operand(EQUAL));
2990
2991 __ bind(&fpu_lt);
2992 __ Ret(USE_DELAY_SLOT);
2993 __ li(v0, Operand(LESS));
2994
2995 __ bind(&unordered);
Ben Murdoch257744e2011-11-30 15:57:28 +00002996 __ bind(&generic_stub);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002997 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002998 CompareICState::GENERIC, CompareICState::GENERIC);
Ben Murdoch257744e2011-11-30 15:57:28 +00002999 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
3000
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003001 __ bind(&maybe_undefined1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003002 if (Token::IsOrderedRelationalCompareOp(op())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003003 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3004 __ Branch(&miss, ne, a0, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003005 __ JumpIfSmi(a1, &unordered);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003006 __ GetObjectType(a1, a2, a2);
3007 __ Branch(&maybe_undefined2, ne, a2, Operand(HEAP_NUMBER_TYPE));
3008 __ jmp(&unordered);
3009 }
3010
3011 __ bind(&maybe_undefined2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003012 if (Token::IsOrderedRelationalCompareOp(op())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003013 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3014 __ Branch(&unordered, eq, a1, Operand(at));
3015 }
3016
Ben Murdoch257744e2011-11-30 15:57:28 +00003017 __ bind(&miss);
3018 GenerateMiss(masm);
3019}
3020
3021
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003022void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3023 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00003024 Label miss;
3025
3026 // Registers containing left and right operands respectively.
3027 Register left = a1;
3028 Register right = a0;
3029 Register tmp1 = a2;
3030 Register tmp2 = a3;
3031
3032 // Check that both operands are heap objects.
3033 __ JumpIfEitherSmi(left, right, &miss);
3034
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003035 // Check that both operands are internalized strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00003036 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3037 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3038 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3039 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003040 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3041 __ Or(tmp1, tmp1, Operand(tmp2));
3042 __ And(at, tmp1, Operand(kIsNotStringMask | kIsNotInternalizedMask));
3043 __ Branch(&miss, ne, at, Operand(zero_reg));
3044
Ben Murdoch257744e2011-11-30 15:57:28 +00003045 // Make sure a0 is non-zero. At this point input operands are
3046 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003047 DCHECK(right.is(a0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003048 STATIC_ASSERT(EQUAL == 0);
3049 STATIC_ASSERT(kSmiTag == 0);
3050 __ mov(v0, right);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003051 // Internalized strings are compared by identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00003052 __ Ret(ne, left, Operand(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003053 DCHECK(is_int16(EQUAL));
3054 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003055 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003056
3057 __ bind(&miss);
3058 GenerateMiss(masm);
3059}
3060
3061
3062void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3063 DCHECK(state() == CompareICState::UNIQUE_NAME);
3064 DCHECK(GetCondition() == eq);
3065 Label miss;
3066
3067 // Registers containing left and right operands respectively.
3068 Register left = a1;
3069 Register right = a0;
3070 Register tmp1 = a2;
3071 Register tmp2 = a3;
3072
3073 // Check that both operands are heap objects.
3074 __ JumpIfEitherSmi(left, right, &miss);
3075
3076 // Check that both operands are unique names. This leaves the instance
3077 // types loaded in tmp1 and tmp2.
3078 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3079 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3080 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3081 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3082
3083 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss);
3084 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss);
3085
3086 // Use a0 as result
3087 __ mov(v0, a0);
3088
3089 // Unique names are compared by identity.
3090 Label done;
3091 __ Branch(&done, ne, left, Operand(right));
3092 // Make sure a0 is non-zero. At this point input operands are
3093 // guaranteed to be non-zero.
3094 DCHECK(right.is(a0));
3095 STATIC_ASSERT(EQUAL == 0);
3096 STATIC_ASSERT(kSmiTag == 0);
3097 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3098 __ bind(&done);
Ben Murdoch257744e2011-11-30 15:57:28 +00003099 __ Ret();
3100
3101 __ bind(&miss);
3102 GenerateMiss(masm);
3103}
3104
3105
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003106void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3107 DCHECK(state() == CompareICState::STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00003108 Label miss;
3109
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003110 bool equality = Token::IsEqualityOp(op());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003111
Ben Murdoch257744e2011-11-30 15:57:28 +00003112 // Registers containing left and right operands respectively.
3113 Register left = a1;
3114 Register right = a0;
3115 Register tmp1 = a2;
3116 Register tmp2 = a3;
3117 Register tmp3 = t0;
3118 Register tmp4 = t1;
3119 Register tmp5 = t2;
3120
3121 // Check that both operands are heap objects.
3122 __ JumpIfEitherSmi(left, right, &miss);
3123
3124 // Check that both operands are strings. This leaves the instance
3125 // types loaded in tmp1 and tmp2.
3126 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3127 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3128 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3129 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3130 STATIC_ASSERT(kNotStringTag != 0);
3131 __ Or(tmp3, tmp1, tmp2);
3132 __ And(tmp5, tmp3, Operand(kIsNotStringMask));
3133 __ Branch(&miss, ne, tmp5, Operand(zero_reg));
3134
3135 // Fast check for identical strings.
3136 Label left_ne_right;
3137 STATIC_ASSERT(EQUAL == 0);
3138 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003139 __ Branch(&left_ne_right, ne, left, Operand(right));
3140 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003141 __ mov(v0, zero_reg); // In the delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00003142 __ bind(&left_ne_right);
3143
3144 // Handle not identical strings.
3145
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003146 // Check that both strings are internalized strings. If they are, we're done
3147 // because we already know they are not identical. We know they are both
3148 // strings.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003149 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003150 DCHECK(GetCondition() == eq);
3151 STATIC_ASSERT(kInternalizedTag == 0);
3152 __ Or(tmp3, tmp1, Operand(tmp2));
3153 __ And(tmp5, tmp3, Operand(kIsNotInternalizedMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003154 Label is_symbol;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003155 __ Branch(&is_symbol, ne, tmp5, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003156 // Make sure a0 is non-zero. At this point input operands are
3157 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003158 DCHECK(right.is(a0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003159 __ Ret(USE_DELAY_SLOT);
3160 __ mov(v0, a0); // In the delay slot.
3161 __ bind(&is_symbol);
3162 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003163
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003164 // Check that both strings are sequential one-byte.
Ben Murdoch257744e2011-11-30 15:57:28 +00003165 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003166 __ JumpIfBothInstanceTypesAreNotSequentialOneByte(tmp1, tmp2, tmp3, tmp4,
3167 &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003168
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003169 // Compare flat one-byte strings. Returns when done.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003170 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003171 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1, tmp2,
3172 tmp3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003173 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003174 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3175 tmp2, tmp3, tmp4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003176 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003177
3178 // Handle more complex cases in runtime.
3179 __ bind(&runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003180 if (equality) {
Ben Murdochda12d292016-06-02 14:46:10 +01003181 {
3182 FrameScope scope(masm, StackFrame::INTERNAL);
3183 __ Push(left, right);
3184 __ CallRuntime(Runtime::kStringEqual);
3185 }
3186 __ LoadRoot(a0, Heap::kTrueValueRootIndex);
3187 __ Ret(USE_DELAY_SLOT);
3188 __ Subu(v0, v0, a0); // In delay slot.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003189 } else {
Ben Murdochda12d292016-06-02 14:46:10 +01003190 __ Push(left, right);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003191 __ TailCallRuntime(Runtime::kStringCompare);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003192 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003193
3194 __ bind(&miss);
3195 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01003196}
3197
3198
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003199void CompareICStub::GenerateReceivers(MacroAssembler* masm) {
3200 DCHECK_EQ(CompareICState::RECEIVER, state());
Ben Murdoch257744e2011-11-30 15:57:28 +00003201 Label miss;
3202 __ And(a2, a1, Operand(a0));
3203 __ JumpIfSmi(a2, &miss);
3204
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003205 STATIC_ASSERT(LAST_TYPE == LAST_JS_RECEIVER_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +00003206 __ GetObjectType(a0, a2, a2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003207 __ Branch(&miss, lt, a2, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00003208 __ GetObjectType(a1, a2, a2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003209 __ Branch(&miss, lt, a2, Operand(FIRST_JS_RECEIVER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +00003210
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003211 DCHECK_EQ(eq, GetCondition());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003212 __ Ret(USE_DELAY_SLOT);
3213 __ subu(v0, a0, a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003214
3215 __ bind(&miss);
3216 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01003217}
3218
3219
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003220void CompareICStub::GenerateKnownReceivers(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003221 Label miss;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003222 Handle<WeakCell> cell = Map::WeakCellForMap(known_map_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003223 __ And(a2, a1, a0);
3224 __ JumpIfSmi(a2, &miss);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003225 __ GetWeakValue(t0, cell);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003226 __ lw(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
3227 __ lw(a3, FieldMemOperand(a1, HeapObject::kMapOffset));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003228 __ Branch(&miss, ne, a2, Operand(t0));
3229 __ Branch(&miss, ne, a3, Operand(t0));
Ben Murdoch85b71792012-04-11 18:30:58 +01003230
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003231 if (Token::IsEqualityOp(op())) {
3232 __ Ret(USE_DELAY_SLOT);
3233 __ subu(v0, a0, a1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003234 } else {
3235 if (op() == Token::LT || op() == Token::LTE) {
3236 __ li(a2, Operand(Smi::FromInt(GREATER)));
3237 } else {
3238 __ li(a2, Operand(Smi::FromInt(LESS)));
3239 }
3240 __ Push(a1, a0, a2);
3241 __ TailCallRuntime(Runtime::kCompare);
3242 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003243
3244 __ bind(&miss);
3245 GenerateMiss(masm);
3246}
3247
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003248
3249void CompareICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003250 {
3251 // Call the runtime system in a fresh internal frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003252 FrameScope scope(masm, StackFrame::INTERNAL);
3253 __ Push(a1, a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003254 __ Push(ra, a1, a0);
3255 __ li(t0, Operand(Smi::FromInt(op())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003256 __ addiu(sp, sp, -kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003257 __ CallRuntime(Runtime::kCompareIC_Miss, 3, kDontSaveFPRegs,
3258 USE_DELAY_SLOT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003259 __ sw(t0, MemOperand(sp)); // In the delay slot.
3260 // Compute the entry point of the rewritten stub.
3261 __ Addu(a2, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
3262 // Restore registers.
3263 __ Pop(a1, a0, ra);
3264 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003265 __ Jump(a2);
3266}
3267
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003268
Ben Murdoch257744e2011-11-30 15:57:28 +00003269void DirectCEntryStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003270 // Make place for arguments to fit C calling convention. Most of the callers
3271 // of DirectCEntryStub::GenerateCall are using EnterExitFrame/LeaveExitFrame
3272 // so they handle stack restoring and we don't have to do that here.
3273 // Any caller of DirectCEntryStub::GenerateCall must take care of dropping
3274 // kCArgsSlotsSize stack space after the call.
3275 __ Subu(sp, sp, Operand(kCArgsSlotsSize));
3276 // Place the return address on the stack, making the call
3277 // GC safe. The RegExp backend also relies on this.
3278 __ sw(ra, MemOperand(sp, kCArgsSlotsSize));
3279 __ Call(t9); // Call the C++ function.
Ben Murdoch257744e2011-11-30 15:57:28 +00003280 __ lw(t9, MemOperand(sp, kCArgsSlotsSize));
3281
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003282 if (FLAG_debug_code && FLAG_enable_slow_asserts) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003283 // In case of an error the return address may point to a memory area
3284 // filled with kZapValue by the GC.
3285 // Dereference the address and check for this.
3286 __ lw(t0, MemOperand(t9));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003287 __ Assert(ne, kReceivedInvalidReturnAddress, t0,
Ben Murdoch257744e2011-11-30 15:57:28 +00003288 Operand(reinterpret_cast<uint32_t>(kZapValue)));
3289 }
3290 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01003291}
3292
3293
Ben Murdoch257744e2011-11-30 15:57:28 +00003294void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoch257744e2011-11-30 15:57:28 +00003295 Register target) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003296 intptr_t loc =
3297 reinterpret_cast<intptr_t>(GetCode().location());
Ben Murdoch257744e2011-11-30 15:57:28 +00003298 __ Move(t9, target);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003299 __ li(at, Operand(loc, RelocInfo::CODE_TARGET), CONSTANT_SIZE);
3300 __ Call(at);
Ben Murdoch257744e2011-11-30 15:57:28 +00003301}
3302
3303
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003304void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3305 Label* miss,
3306 Label* done,
3307 Register receiver,
3308 Register properties,
3309 Handle<Name> name,
3310 Register scratch0) {
3311 DCHECK(name->IsUniqueName());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003312 // If names of slots in range from 1 to kProbes - 1 for the hash value are
Ben Murdoch257744e2011-11-30 15:57:28 +00003313 // not equal to the name and kProbes-th slot is not used (its name is the
3314 // undefined value), it guarantees the hash table doesn't contain the
3315 // property. It's true even if some slots represent deleted properties
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003316 // (their names are the hole value).
Ben Murdoch257744e2011-11-30 15:57:28 +00003317 for (int i = 0; i < kInlinedProbes; i++) {
3318 // scratch0 points to properties hash.
3319 // Compute the masked index: (hash + i + i * i) & mask.
3320 Register index = scratch0;
3321 // Capacity is smi 2^n.
3322 __ lw(index, FieldMemOperand(properties, kCapacityOffset));
3323 __ Subu(index, index, Operand(1));
3324 __ And(index, index, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003325 Smi::FromInt(name->Hash() + NameDictionary::GetProbeOffset(i))));
Ben Murdoch257744e2011-11-30 15:57:28 +00003326
3327 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003328 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003329 __ Lsa(index, index, index, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003330
3331 Register entity_name = scratch0;
3332 // Having undefined at this place means the name is not contained.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003333 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003334 Register tmp = properties;
Ben Murdoch097c5b22016-05-18 11:27:45 +01003335 __ Lsa(tmp, properties, index, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003336 __ lw(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
3337
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003338 DCHECK(!tmp.is(entity_name));
Ben Murdoch257744e2011-11-30 15:57:28 +00003339 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
3340 __ Branch(done, eq, entity_name, Operand(tmp));
3341
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003342 // Load the hole ready for use below:
3343 __ LoadRoot(tmp, Heap::kTheHoleValueRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003344
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003345 // Stop if found the property.
3346 __ Branch(miss, eq, entity_name, Operand(Handle<Name>(name)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003347
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003348 Label good;
3349 __ Branch(&good, eq, entity_name, Operand(tmp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003350
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003351 // Check if the entry name is not a unique name.
3352 __ lw(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
3353 __ lbu(entity_name,
3354 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
3355 __ JumpIfNotUniqueNameInstanceType(entity_name, miss);
3356 __ bind(&good);
Ben Murdoch257744e2011-11-30 15:57:28 +00003357
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003358 // Restore the properties.
3359 __ lw(properties,
3360 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003361 }
3362
3363 const int spill_mask =
3364 (ra.bit() | t2.bit() | t1.bit() | t0.bit() | a3.bit() |
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003365 a2.bit() | a1.bit() | a0.bit() | v0.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00003366
3367 __ MultiPush(spill_mask);
3368 __ lw(a0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003369 __ li(a1, Operand(Handle<Name>(name)));
3370 NameDictionaryLookupStub stub(masm->isolate(), NEGATIVE_LOOKUP);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003371 __ CallStub(&stub);
3372 __ mov(at, v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003373 __ MultiPop(spill_mask);
3374
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003375 __ Branch(done, eq, at, Operand(zero_reg));
3376 __ Branch(miss, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00003377}
3378
3379
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003380// Probe the name dictionary in the |elements| register. Jump to the
Ben Murdoch257744e2011-11-30 15:57:28 +00003381// |done| label if a property with the given name is found. Jump to
3382// the |miss| label otherwise.
3383// If lookup was successful |scratch2| will be equal to elements + 4 * index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003384void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
3385 Label* miss,
3386 Label* done,
3387 Register elements,
3388 Register name,
3389 Register scratch1,
3390 Register scratch2) {
3391 DCHECK(!elements.is(scratch1));
3392 DCHECK(!elements.is(scratch2));
3393 DCHECK(!name.is(scratch1));
3394 DCHECK(!name.is(scratch2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003395
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003396 __ AssertName(name);
Ben Murdoch257744e2011-11-30 15:57:28 +00003397
3398 // Compute the capacity mask.
3399 __ lw(scratch1, FieldMemOperand(elements, kCapacityOffset));
3400 __ sra(scratch1, scratch1, kSmiTagSize); // convert smi to int
3401 __ Subu(scratch1, scratch1, Operand(1));
3402
3403 // Generate an unrolled loop that performs a few probes before
3404 // giving up. Measurements done on Gmail indicate that 2 probes
3405 // cover ~93% of loads from dictionaries.
3406 for (int i = 0; i < kInlinedProbes; i++) {
3407 // Compute the masked index: (hash + i + i * i) & mask.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003408 __ lw(scratch2, FieldMemOperand(name, Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003409 if (i > 0) {
3410 // Add the probe offset (i + i * i) left shifted to avoid right shifting
3411 // the hash in a separate instruction. The value hash + i + i * i is right
3412 // shifted in the following and instruction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003413 DCHECK(NameDictionary::GetProbeOffset(i) <
3414 1 << (32 - Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003415 __ Addu(scratch2, scratch2, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003416 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003417 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003418 __ srl(scratch2, scratch2, Name::kHashShift);
Ben Murdoch257744e2011-11-30 15:57:28 +00003419 __ And(scratch2, scratch1, scratch2);
3420
3421 // Scale the index by multiplying by the element size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003422 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003423 // scratch2 = scratch2 * 3.
3424
Ben Murdoch097c5b22016-05-18 11:27:45 +01003425 __ Lsa(scratch2, scratch2, scratch2, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003426
3427 // Check if the key is identical to the name.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003428 __ Lsa(scratch2, elements, scratch2, 2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003429 __ lw(at, FieldMemOperand(scratch2, kElementsStartOffset));
3430 __ Branch(done, eq, name, Operand(at));
3431 }
3432
3433 const int spill_mask =
3434 (ra.bit() | t2.bit() | t1.bit() | t0.bit() |
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003435 a3.bit() | a2.bit() | a1.bit() | a0.bit() | v0.bit()) &
Ben Murdoch257744e2011-11-30 15:57:28 +00003436 ~(scratch1.bit() | scratch2.bit());
3437
3438 __ MultiPush(spill_mask);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003439 if (name.is(a0)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003440 DCHECK(!elements.is(a1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003441 __ Move(a1, name);
3442 __ Move(a0, elements);
3443 } else {
3444 __ Move(a0, elements);
3445 __ Move(a1, name);
3446 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003447 NameDictionaryLookupStub stub(masm->isolate(), POSITIVE_LOOKUP);
Ben Murdoch257744e2011-11-30 15:57:28 +00003448 __ CallStub(&stub);
3449 __ mov(scratch2, a2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003450 __ mov(at, v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003451 __ MultiPop(spill_mask);
3452
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003453 __ Branch(done, ne, at, Operand(zero_reg));
3454 __ Branch(miss, eq, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00003455}
3456
3457
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003458void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003459 // This stub overrides SometimesSetsUpAFrame() to return false. That means
3460 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00003461 // Registers:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003462 // result: NameDictionary to probe
Ben Murdoch257744e2011-11-30 15:57:28 +00003463 // a1: key
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003464 // dictionary: NameDictionary to probe.
3465 // index: will hold an index of entry if lookup is successful.
3466 // might alias with result_.
Ben Murdoch257744e2011-11-30 15:57:28 +00003467 // Returns:
3468 // result_ is zero if lookup failed, non zero otherwise.
3469
3470 Register result = v0;
3471 Register dictionary = a0;
3472 Register key = a1;
3473 Register index = a2;
3474 Register mask = a3;
3475 Register hash = t0;
3476 Register undefined = t1;
3477 Register entry_key = t2;
3478
3479 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
3480
3481 __ lw(mask, FieldMemOperand(dictionary, kCapacityOffset));
3482 __ sra(mask, mask, kSmiTagSize);
3483 __ Subu(mask, mask, Operand(1));
3484
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003485 __ lw(hash, FieldMemOperand(key, Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003486
3487 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
3488
3489 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
3490 // Compute the masked index: (hash + i + i * i) & mask.
3491 // Capacity is smi 2^n.
3492 if (i > 0) {
3493 // Add the probe offset (i + i * i) left shifted to avoid right shifting
3494 // the hash in a separate instruction. The value hash + i + i * i is right
3495 // shifted in the following and instruction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003496 DCHECK(NameDictionary::GetProbeOffset(i) <
3497 1 << (32 - Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003498 __ Addu(index, hash, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003499 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00003500 } else {
3501 __ mov(index, hash);
3502 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003503 __ srl(index, index, Name::kHashShift);
Ben Murdoch257744e2011-11-30 15:57:28 +00003504 __ And(index, mask, index);
3505
3506 // Scale the index by multiplying by the entry size.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003507 STATIC_ASSERT(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003508 // index *= 3.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003509 __ Lsa(index, index, index, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003510
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003511 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +01003512 __ Lsa(index, dictionary, index, 2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003513 __ lw(entry_key, FieldMemOperand(index, kElementsStartOffset));
3514
3515 // Having undefined at this place means the name is not contained.
3516 __ Branch(&not_in_dictionary, eq, entry_key, Operand(undefined));
3517
3518 // Stop if found the property.
3519 __ Branch(&in_dictionary, eq, entry_key, Operand(key));
3520
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003521 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
3522 // Check if the entry name is not a unique name.
Ben Murdoch257744e2011-11-30 15:57:28 +00003523 __ lw(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
3524 __ lbu(entry_key,
3525 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003526 __ JumpIfNotUniqueNameInstanceType(entry_key, &maybe_in_dictionary);
Ben Murdoch257744e2011-11-30 15:57:28 +00003527 }
3528 }
3529
3530 __ bind(&maybe_in_dictionary);
3531 // If we are doing negative lookup then probing failure should be
3532 // treated as a lookup success. For positive lookup probing failure
3533 // should be treated as lookup failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003534 if (mode() == POSITIVE_LOOKUP) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003535 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003536 __ mov(result, zero_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00003537 }
3538
3539 __ bind(&in_dictionary);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003540 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003541 __ li(result, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003542
3543 __ bind(&not_in_dictionary);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003544 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003545 __ mov(result, zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003546}
3547
3548
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003549void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
3550 Isolate* isolate) {
3551 StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
3552 stub1.GetCode();
3553 // Hydrogen code stubs need stub2 at snapshot time.
3554 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
3555 stub2.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003556}
3557
3558
3559// Takes the input in 3 registers: address_ value_ and object_. A pointer to
3560// the value has just been written into the object, now this stub makes sure
3561// we keep the GC informed. The word in the object where the value has been
3562// written is in the address register.
3563void RecordWriteStub::Generate(MacroAssembler* masm) {
3564 Label skip_to_incremental_noncompacting;
3565 Label skip_to_incremental_compacting;
3566
3567 // The first two branch+nop instructions are generated with labels so as to
3568 // get the offset fixed up correctly by the bind(Label*) call. We patch it
3569 // back and forth between a "bne zero_reg, zero_reg, ..." (a nop in this
3570 // position) and the "beq zero_reg, zero_reg, ..." when we start and stop
3571 // incremental heap marking.
3572 // See RecordWriteStub::Patch for details.
3573 __ beq(zero_reg, zero_reg, &skip_to_incremental_noncompacting);
3574 __ nop();
3575 __ beq(zero_reg, zero_reg, &skip_to_incremental_compacting);
3576 __ nop();
3577
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003578 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
3579 __ RememberedSetHelper(object(),
3580 address(),
3581 value(),
3582 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003583 MacroAssembler::kReturnAtEnd);
3584 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003585 __ Ret();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003586
3587 __ bind(&skip_to_incremental_noncompacting);
3588 GenerateIncremental(masm, INCREMENTAL);
3589
3590 __ bind(&skip_to_incremental_compacting);
3591 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
3592
3593 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
3594 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
3595
3596 PatchBranchIntoNop(masm, 0);
3597 PatchBranchIntoNop(masm, 2 * Assembler::kInstrSize);
3598}
3599
3600
3601void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
3602 regs_.Save(masm);
3603
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003604 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003605 Label dont_need_remembered_set;
3606
3607 __ lw(regs_.scratch0(), MemOperand(regs_.address(), 0));
3608 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
3609 regs_.scratch0(),
3610 &dont_need_remembered_set);
3611
Ben Murdoch097c5b22016-05-18 11:27:45 +01003612 __ JumpIfInNewSpace(regs_.object(), regs_.scratch0(),
3613 &dont_need_remembered_set);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003614
3615 // First notify the incremental marker if necessary, then update the
3616 // remembered set.
3617 CheckNeedsToInformIncrementalMarker(
3618 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003619 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003620 regs_.Restore(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003621 __ RememberedSetHelper(object(),
3622 address(),
3623 value(),
3624 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003625 MacroAssembler::kReturnAtEnd);
3626
3627 __ bind(&dont_need_remembered_set);
3628 }
3629
3630 CheckNeedsToInformIncrementalMarker(
3631 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003632 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003633 regs_.Restore(masm);
3634 __ Ret();
3635}
3636
3637
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003638void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
3639 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003640 int argument_count = 3;
3641 __ PrepareCallCFunction(argument_count, regs_.scratch0());
3642 Register address =
3643 a0.is(regs_.address()) ? regs_.scratch0() : regs_.address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003644 DCHECK(!address.is(regs_.object()));
3645 DCHECK(!address.is(a0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003646 __ Move(address, regs_.address());
3647 __ Move(a0, regs_.object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003648 __ Move(a1, address);
3649 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003650
3651 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003652 __ CallCFunction(
3653 ExternalReference::incremental_marking_record_write_function(isolate()),
3654 argument_count);
3655 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003656}
3657
3658
3659void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
3660 MacroAssembler* masm,
3661 OnNoNeedToInformIncrementalMarker on_no_need,
3662 Mode mode) {
3663 Label on_black;
3664 Label need_incremental;
3665 Label need_incremental_pop_scratch;
3666
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003667 __ And(regs_.scratch0(), regs_.object(), Operand(~Page::kPageAlignmentMask));
3668 __ lw(regs_.scratch1(),
3669 MemOperand(regs_.scratch0(),
3670 MemoryChunk::kWriteBarrierCounterOffset));
3671 __ Subu(regs_.scratch1(), regs_.scratch1(), Operand(1));
3672 __ sw(regs_.scratch1(),
3673 MemOperand(regs_.scratch0(),
3674 MemoryChunk::kWriteBarrierCounterOffset));
3675 __ Branch(&need_incremental, lt, regs_.scratch1(), Operand(zero_reg));
3676
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003677 // Let's look at the color of the object: If it is not black we don't have
3678 // to inform the incremental marker.
3679 __ JumpIfBlack(regs_.object(), regs_.scratch0(), regs_.scratch1(), &on_black);
3680
3681 regs_.Restore(masm);
3682 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003683 __ RememberedSetHelper(object(),
3684 address(),
3685 value(),
3686 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003687 MacroAssembler::kReturnAtEnd);
3688 } else {
3689 __ Ret();
3690 }
3691
3692 __ bind(&on_black);
3693
3694 // Get the value from the slot.
3695 __ lw(regs_.scratch0(), MemOperand(regs_.address(), 0));
3696
3697 if (mode == INCREMENTAL_COMPACTION) {
3698 Label ensure_not_white;
3699
3700 __ CheckPageFlag(regs_.scratch0(), // Contains value.
3701 regs_.scratch1(), // Scratch.
3702 MemoryChunk::kEvacuationCandidateMask,
3703 eq,
3704 &ensure_not_white);
3705
3706 __ CheckPageFlag(regs_.object(),
3707 regs_.scratch1(), // Scratch.
3708 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
3709 eq,
3710 &need_incremental);
3711
3712 __ bind(&ensure_not_white);
3713 }
3714
3715 // We need extra registers for this, so we push the object and the address
3716 // register temporarily.
3717 __ Push(regs_.object(), regs_.address());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003718 __ JumpIfWhite(regs_.scratch0(), // The value.
3719 regs_.scratch1(), // Scratch.
3720 regs_.object(), // Scratch.
3721 regs_.address(), // Scratch.
3722 &need_incremental_pop_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003723 __ Pop(regs_.object(), regs_.address());
3724
3725 regs_.Restore(masm);
3726 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003727 __ RememberedSetHelper(object(),
3728 address(),
3729 value(),
3730 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003731 MacroAssembler::kReturnAtEnd);
3732 } else {
3733 __ Ret();
3734 }
3735
3736 __ bind(&need_incremental_pop_scratch);
3737 __ Pop(regs_.object(), regs_.address());
3738
3739 __ bind(&need_incremental);
3740
3741 // Fall through when we need to inform the incremental marker.
3742}
3743
3744
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003745void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
3746 CEntryStub ces(isolate(), 1, kSaveFPRegs);
3747 __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
3748 int parameter_count_offset =
Ben Murdochda12d292016-06-02 14:46:10 +01003749 StubFailureTrampolineFrameConstants::kArgumentsLengthOffset;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003750 __ lw(a1, MemOperand(fp, parameter_count_offset));
3751 if (function_mode() == JS_FUNCTION_STUB_MODE) {
3752 __ Addu(a1, a1, Operand(1));
3753 }
3754 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
3755 __ sll(a1, a1, kPointerSizeLog2);
3756 __ Ret(USE_DELAY_SLOT);
3757 __ Addu(sp, sp, a1);
3758}
3759
3760
3761void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003762 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3763 LoadICStub stub(isolate(), state());
3764 stub.GenerateForTrampoline(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003765}
3766
3767
3768void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003769 __ EmitLoadTypeFeedbackVector(LoadWithVectorDescriptor::VectorRegister());
3770 KeyedLoadICStub stub(isolate(), state());
3771 stub.GenerateForTrampoline(masm);
3772}
3773
3774
3775void CallICTrampolineStub::Generate(MacroAssembler* masm) {
3776 __ EmitLoadTypeFeedbackVector(a2);
3777 CallICStub stub(isolate(), state());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003778 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
3779}
3780
3781
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003782void LoadICStub::Generate(MacroAssembler* masm) { GenerateImpl(masm, false); }
3783
3784
3785void LoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3786 GenerateImpl(masm, true);
3787}
3788
3789
3790static void HandleArrayCases(MacroAssembler* masm, Register feedback,
3791 Register receiver_map, Register scratch1,
3792 Register scratch2, bool is_polymorphic,
3793 Label* miss) {
3794 // feedback initially contains the feedback array
3795 Label next_loop, prepare_next;
3796 Label start_polymorphic;
3797
3798 Register cached_map = scratch1;
3799
3800 __ lw(cached_map,
3801 FieldMemOperand(feedback, FixedArray::OffsetOfElementAt(0)));
3802 __ lw(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
3803 __ Branch(&start_polymorphic, ne, receiver_map, Operand(cached_map));
3804 // found, now call handler.
3805 Register handler = feedback;
3806 __ lw(handler, FieldMemOperand(feedback, FixedArray::OffsetOfElementAt(1)));
3807 __ Addu(t9, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
3808 __ Jump(t9);
3809
3810
3811 Register length = scratch2;
3812 __ bind(&start_polymorphic);
3813 __ lw(length, FieldMemOperand(feedback, FixedArray::kLengthOffset));
3814 if (!is_polymorphic) {
3815 // If the IC could be monomorphic we have to make sure we don't go past the
3816 // end of the feedback array.
3817 __ Branch(miss, eq, length, Operand(Smi::FromInt(2)));
3818 }
3819
3820 Register too_far = length;
3821 Register pointer_reg = feedback;
3822
3823 // +-----+------+------+-----+-----+ ... ----+
3824 // | map | len | wm0 | h0 | wm1 | hN |
3825 // +-----+------+------+-----+-----+ ... ----+
3826 // 0 1 2 len-1
3827 // ^ ^
3828 // | |
3829 // pointer_reg too_far
3830 // aka feedback scratch2
3831 // also need receiver_map
3832 // use cached_map (scratch1) to look in the weak map values.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003833 __ Lsa(too_far, feedback, length, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003834 __ Addu(too_far, too_far, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3835 __ Addu(pointer_reg, feedback,
3836 Operand(FixedArray::OffsetOfElementAt(2) - kHeapObjectTag));
3837
3838 __ bind(&next_loop);
3839 __ lw(cached_map, MemOperand(pointer_reg));
3840 __ lw(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
3841 __ Branch(&prepare_next, ne, receiver_map, Operand(cached_map));
3842 __ lw(handler, MemOperand(pointer_reg, kPointerSize));
3843 __ Addu(t9, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
3844 __ Jump(t9);
3845
3846 __ bind(&prepare_next);
3847 __ Addu(pointer_reg, pointer_reg, Operand(kPointerSize * 2));
3848 __ Branch(&next_loop, lt, pointer_reg, Operand(too_far));
3849
3850 // We exhausted our array of map handler pairs.
3851 __ jmp(miss);
3852}
3853
3854
3855static void HandleMonomorphicCase(MacroAssembler* masm, Register receiver,
3856 Register receiver_map, Register feedback,
3857 Register vector, Register slot,
3858 Register scratch, Label* compare_map,
3859 Label* load_smi_map, Label* try_array) {
3860 __ JumpIfSmi(receiver, load_smi_map);
3861 __ lw(receiver_map, FieldMemOperand(receiver, HeapObject::kMapOffset));
3862 __ bind(compare_map);
3863 Register cached_map = scratch;
3864 // Move the weak map into the weak_cell register.
3865 __ lw(cached_map, FieldMemOperand(feedback, WeakCell::kValueOffset));
3866 __ Branch(try_array, ne, cached_map, Operand(receiver_map));
3867 Register handler = feedback;
3868
Ben Murdoch097c5b22016-05-18 11:27:45 +01003869 __ Lsa(handler, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003870 __ lw(handler,
3871 FieldMemOperand(handler, FixedArray::kHeaderSize + kPointerSize));
3872 __ Addu(t9, handler, Operand(Code::kHeaderSize - kHeapObjectTag));
3873 __ Jump(t9);
3874}
3875
3876
3877void LoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3878 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // a1
3879 Register name = LoadWithVectorDescriptor::NameRegister(); // a2
3880 Register vector = LoadWithVectorDescriptor::VectorRegister(); // a3
3881 Register slot = LoadWithVectorDescriptor::SlotRegister(); // a0
3882 Register feedback = t0;
3883 Register receiver_map = t1;
3884 Register scratch1 = t4;
3885
Ben Murdoch097c5b22016-05-18 11:27:45 +01003886 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003887 __ lw(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
3888
3889 // Try to quickly handle the monomorphic case without knowing for sure
3890 // if we have a weak cell in feedback. We do know it's safe to look
3891 // at WeakCell::kValueOffset.
3892 Label try_array, load_smi_map, compare_map;
3893 Label not_array, miss;
3894 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
3895 scratch1, &compare_map, &load_smi_map, &try_array);
3896
3897 // Is it a fixed array?
3898 __ bind(&try_array);
3899 __ lw(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
3900 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
3901 __ Branch(&not_array, ne, at, Operand(scratch1));
3902 HandleArrayCases(masm, feedback, receiver_map, scratch1, t5, true, &miss);
3903
3904 __ bind(&not_array);
3905 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
3906 __ Branch(&miss, ne, at, Operand(feedback));
Ben Murdochc5610432016-08-08 18:44:38 +01003907 Code::Flags code_flags =
3908 Code::RemoveHolderFromFlags(Code::ComputeHandlerFlags(Code::LOAD_IC));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003909 masm->isolate()->stub_cache()->GenerateProbe(masm, Code::LOAD_IC, code_flags,
3910 receiver, name, feedback,
3911 receiver_map, scratch1, t5);
3912
3913 __ bind(&miss);
3914 LoadIC::GenerateMiss(masm);
3915
3916 __ bind(&load_smi_map);
3917 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3918 __ jmp(&compare_map);
3919}
3920
3921
3922void KeyedLoadICStub::Generate(MacroAssembler* masm) {
3923 GenerateImpl(masm, false);
3924}
3925
3926
3927void KeyedLoadICStub::GenerateForTrampoline(MacroAssembler* masm) {
3928 GenerateImpl(masm, true);
3929}
3930
3931
3932void KeyedLoadICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
3933 Register receiver = LoadWithVectorDescriptor::ReceiverRegister(); // a1
3934 Register key = LoadWithVectorDescriptor::NameRegister(); // a2
3935 Register vector = LoadWithVectorDescriptor::VectorRegister(); // a3
3936 Register slot = LoadWithVectorDescriptor::SlotRegister(); // a0
3937 Register feedback = t0;
3938 Register receiver_map = t1;
3939 Register scratch1 = t4;
3940
Ben Murdoch097c5b22016-05-18 11:27:45 +01003941 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003942 __ lw(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
3943
3944 // Try to quickly handle the monomorphic case without knowing for sure
3945 // if we have a weak cell in feedback. We do know it's safe to look
3946 // at WeakCell::kValueOffset.
3947 Label try_array, load_smi_map, compare_map;
3948 Label not_array, miss;
3949 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
3950 scratch1, &compare_map, &load_smi_map, &try_array);
3951
3952 __ bind(&try_array);
3953 // Is it a fixed array?
3954 __ lw(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
3955 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
3956 __ Branch(&not_array, ne, at, Operand(scratch1));
3957 // We have a polymorphic element handler.
3958 __ JumpIfNotSmi(key, &miss);
3959
3960 Label polymorphic, try_poly_name;
3961 __ bind(&polymorphic);
3962 HandleArrayCases(masm, feedback, receiver_map, scratch1, t5, true, &miss);
3963
3964 __ bind(&not_array);
3965 // Is it generic?
3966 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
3967 __ Branch(&try_poly_name, ne, at, Operand(feedback));
3968 Handle<Code> megamorphic_stub =
3969 KeyedLoadIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
3970 __ Jump(megamorphic_stub, RelocInfo::CODE_TARGET);
3971
3972 __ bind(&try_poly_name);
3973 // We might have a name in feedback, and a fixed array in the next slot.
3974 __ Branch(&miss, ne, key, Operand(feedback));
3975 // If the name comparison succeeded, we know we have a fixed array with
3976 // at least one map/handler pair.
Ben Murdoch097c5b22016-05-18 11:27:45 +01003977 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00003978 __ lw(feedback,
3979 FieldMemOperand(feedback, FixedArray::kHeaderSize + kPointerSize));
3980 HandleArrayCases(masm, feedback, receiver_map, scratch1, t5, false, &miss);
3981
3982 __ bind(&miss);
3983 KeyedLoadIC::GenerateMiss(masm);
3984
3985 __ bind(&load_smi_map);
3986 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex);
3987 __ jmp(&compare_map);
3988}
3989
3990
3991void VectorStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3992 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
3993 VectorStoreICStub stub(isolate(), state());
3994 stub.GenerateForTrampoline(masm);
3995}
3996
3997
3998void VectorKeyedStoreICTrampolineStub::Generate(MacroAssembler* masm) {
3999 __ EmitLoadTypeFeedbackVector(VectorStoreICDescriptor::VectorRegister());
4000 VectorKeyedStoreICStub stub(isolate(), state());
4001 stub.GenerateForTrampoline(masm);
4002}
4003
4004
4005void VectorStoreICStub::Generate(MacroAssembler* masm) {
4006 GenerateImpl(masm, false);
4007}
4008
4009
4010void VectorStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4011 GenerateImpl(masm, true);
4012}
4013
4014
4015void VectorStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4016 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // a1
4017 Register key = VectorStoreICDescriptor::NameRegister(); // a2
4018 Register vector = VectorStoreICDescriptor::VectorRegister(); // a3
4019 Register slot = VectorStoreICDescriptor::SlotRegister(); // t0
4020 DCHECK(VectorStoreICDescriptor::ValueRegister().is(a0)); // a0
4021 Register feedback = t1;
4022 Register receiver_map = t2;
4023 Register scratch1 = t5;
4024
Ben Murdoch097c5b22016-05-18 11:27:45 +01004025 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004026 __ lw(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
4027
4028 // Try to quickly handle the monomorphic case without knowing for sure
4029 // if we have a weak cell in feedback. We do know it's safe to look
4030 // at WeakCell::kValueOffset.
4031 Label try_array, load_smi_map, compare_map;
4032 Label not_array, miss;
4033 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
4034 scratch1, &compare_map, &load_smi_map, &try_array);
4035
4036 // Is it a fixed array?
4037 __ bind(&try_array);
4038 __ lw(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
4039 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4040 __ Branch(&not_array, ne, scratch1, Operand(at));
4041
4042 Register scratch2 = t4;
4043 HandleArrayCases(masm, feedback, receiver_map, scratch1, scratch2, true,
4044 &miss);
4045
4046 __ bind(&not_array);
4047 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
4048 __ Branch(&miss, ne, feedback, Operand(at));
Ben Murdochc5610432016-08-08 18:44:38 +01004049 Code::Flags code_flags =
4050 Code::RemoveHolderFromFlags(Code::ComputeHandlerFlags(Code::STORE_IC));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004051 masm->isolate()->stub_cache()->GenerateProbe(
4052 masm, Code::STORE_IC, code_flags, receiver, key, feedback, receiver_map,
4053 scratch1, scratch2);
4054
4055 __ bind(&miss);
4056 StoreIC::GenerateMiss(masm);
4057
4058 __ bind(&load_smi_map);
4059 __ Branch(USE_DELAY_SLOT, &compare_map);
4060 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex); // In delay slot.
4061}
4062
4063
4064void VectorKeyedStoreICStub::Generate(MacroAssembler* masm) {
4065 GenerateImpl(masm, false);
4066}
4067
4068
4069void VectorKeyedStoreICStub::GenerateForTrampoline(MacroAssembler* masm) {
4070 GenerateImpl(masm, true);
4071}
4072
4073
4074static void HandlePolymorphicStoreCase(MacroAssembler* masm, Register feedback,
4075 Register receiver_map, Register scratch1,
4076 Register scratch2, Label* miss) {
4077 // feedback initially contains the feedback array
4078 Label next_loop, prepare_next;
4079 Label start_polymorphic;
4080 Label transition_call;
4081
4082 Register cached_map = scratch1;
4083 Register too_far = scratch2;
4084 Register pointer_reg = feedback;
4085 __ lw(too_far, FieldMemOperand(feedback, FixedArray::kLengthOffset));
4086
4087 // +-----+------+------+-----+-----+-----+ ... ----+
4088 // | map | len | wm0 | wt0 | h0 | wm1 | hN |
4089 // +-----+------+------+-----+-----+ ----+ ... ----+
4090 // 0 1 2 len-1
4091 // ^ ^
4092 // | |
4093 // pointer_reg too_far
4094 // aka feedback scratch2
4095 // also need receiver_map
4096 // use cached_map (scratch1) to look in the weak map values.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004097 __ Lsa(too_far, feedback, too_far, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004098 __ Addu(too_far, too_far, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4099 __ Addu(pointer_reg, feedback,
4100 Operand(FixedArray::OffsetOfElementAt(0) - kHeapObjectTag));
4101
4102 __ bind(&next_loop);
4103 __ lw(cached_map, MemOperand(pointer_reg));
4104 __ lw(cached_map, FieldMemOperand(cached_map, WeakCell::kValueOffset));
4105 __ Branch(&prepare_next, ne, receiver_map, Operand(cached_map));
4106 // Is it a transitioning store?
4107 __ lw(too_far, MemOperand(pointer_reg, kPointerSize));
4108 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4109 __ Branch(&transition_call, ne, too_far, Operand(at));
4110 __ lw(pointer_reg, MemOperand(pointer_reg, kPointerSize * 2));
4111 __ Addu(t9, pointer_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
4112 __ Jump(t9);
4113
4114 __ bind(&transition_call);
4115 __ lw(too_far, FieldMemOperand(too_far, WeakCell::kValueOffset));
4116 __ JumpIfSmi(too_far, miss);
4117
4118 __ lw(receiver_map, MemOperand(pointer_reg, kPointerSize * 2));
4119
4120 // Load the map into the correct register.
4121 DCHECK(feedback.is(VectorStoreTransitionDescriptor::MapRegister()));
4122 __ mov(feedback, too_far);
4123
4124 __ Addu(t9, receiver_map, Operand(Code::kHeaderSize - kHeapObjectTag));
4125 __ Jump(t9);
4126
4127 __ bind(&prepare_next);
4128 __ Addu(pointer_reg, pointer_reg, Operand(kPointerSize * 3));
4129 __ Branch(&next_loop, lt, pointer_reg, Operand(too_far));
4130
4131 // We exhausted our array of map handler pairs.
4132 __ jmp(miss);
4133}
4134
4135
4136void VectorKeyedStoreICStub::GenerateImpl(MacroAssembler* masm, bool in_frame) {
4137 Register receiver = VectorStoreICDescriptor::ReceiverRegister(); // a1
4138 Register key = VectorStoreICDescriptor::NameRegister(); // a2
4139 Register vector = VectorStoreICDescriptor::VectorRegister(); // a3
4140 Register slot = VectorStoreICDescriptor::SlotRegister(); // t0
4141 DCHECK(VectorStoreICDescriptor::ValueRegister().is(a0)); // a0
4142 Register feedback = t1;
4143 Register receiver_map = t2;
4144 Register scratch1 = t5;
4145
Ben Murdoch097c5b22016-05-18 11:27:45 +01004146 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004147 __ lw(feedback, FieldMemOperand(feedback, FixedArray::kHeaderSize));
4148
4149 // Try to quickly handle the monomorphic case without knowing for sure
4150 // if we have a weak cell in feedback. We do know it's safe to look
4151 // at WeakCell::kValueOffset.
4152 Label try_array, load_smi_map, compare_map;
4153 Label not_array, miss;
4154 HandleMonomorphicCase(masm, receiver, receiver_map, feedback, vector, slot,
4155 scratch1, &compare_map, &load_smi_map, &try_array);
4156
4157 __ bind(&try_array);
4158 // Is it a fixed array?
4159 __ lw(scratch1, FieldMemOperand(feedback, HeapObject::kMapOffset));
4160 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4161 __ Branch(&not_array, ne, scratch1, Operand(at));
4162
4163 // We have a polymorphic element handler.
4164 Label polymorphic, try_poly_name;
4165 __ bind(&polymorphic);
4166
4167 Register scratch2 = t4;
4168
4169 HandlePolymorphicStoreCase(masm, feedback, receiver_map, scratch1, scratch2,
4170 &miss);
4171
4172 __ bind(&not_array);
4173 // Is it generic?
4174 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
4175 __ Branch(&try_poly_name, ne, feedback, Operand(at));
4176 Handle<Code> megamorphic_stub =
4177 KeyedStoreIC::ChooseMegamorphicStub(masm->isolate(), GetExtraICState());
4178 __ Jump(megamorphic_stub, RelocInfo::CODE_TARGET);
4179
4180 __ bind(&try_poly_name);
4181 // We might have a name in feedback, and a fixed array in the next slot.
4182 __ Branch(&miss, ne, key, Operand(feedback));
4183 // If the name comparison succeeded, we know we have a fixed array with
4184 // at least one map/handler pair.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004185 __ Lsa(feedback, vector, slot, kPointerSizeLog2 - kSmiTagSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004186 __ lw(feedback,
4187 FieldMemOperand(feedback, FixedArray::kHeaderSize + kPointerSize));
4188 HandleArrayCases(masm, feedback, receiver_map, scratch1, scratch2, false,
4189 &miss);
4190
4191 __ bind(&miss);
4192 KeyedStoreIC::GenerateMiss(masm);
4193
4194 __ bind(&load_smi_map);
4195 __ Branch(USE_DELAY_SLOT, &compare_map);
4196 __ LoadRoot(receiver_map, Heap::kHeapNumberMapRootIndex); // In delay slot.
4197}
4198
4199
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004200void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4201 if (masm->isolate()->function_entry_hook() != NULL) {
4202 ProfileEntryHookStub stub(masm->isolate());
4203 __ push(ra);
4204 __ CallStub(&stub);
4205 __ pop(ra);
4206 }
4207}
4208
4209
4210void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4211 // The entry hook is a "push ra" instruction, followed by a call.
4212 // Note: on MIPS "push" is 2 instruction
4213 const int32_t kReturnAddressDistanceFromFunctionStart =
4214 Assembler::kCallTargetAddressOffset + (2 * Assembler::kInstrSize);
4215
4216 // This should contain all kJSCallerSaved registers.
4217 const RegList kSavedRegs =
4218 kJSCallerSaved | // Caller saved registers.
4219 s5.bit(); // Saved stack pointer.
4220
4221 // We also save ra, so the count here is one higher than the mask indicates.
4222 const int32_t kNumSavedRegs = kNumJSCallerSaved + 2;
4223
4224 // Save all caller-save registers as this may be called from anywhere.
4225 __ MultiPush(kSavedRegs | ra.bit());
4226
4227 // Compute the function's address for the first argument.
4228 __ Subu(a0, ra, Operand(kReturnAddressDistanceFromFunctionStart));
4229
4230 // The caller's return address is above the saved temporaries.
4231 // Grab that for the second argument to the hook.
4232 __ Addu(a1, sp, Operand(kNumSavedRegs * kPointerSize));
4233
4234 // Align the stack if necessary.
4235 int frame_alignment = masm->ActivationFrameAlignment();
4236 if (frame_alignment > kPointerSize) {
4237 __ mov(s5, sp);
4238 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
4239 __ And(sp, sp, Operand(-frame_alignment));
4240 }
4241 __ Subu(sp, sp, kCArgsSlotsSize);
4242#if defined(V8_HOST_ARCH_MIPS)
4243 int32_t entry_hook =
4244 reinterpret_cast<int32_t>(isolate()->function_entry_hook());
4245 __ li(t9, Operand(entry_hook));
4246#else
4247 // Under the simulator we need to indirect the entry hook through a
4248 // trampoline function at a known address.
4249 // It additionally takes an isolate as a third parameter.
4250 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
4251
4252 ApiFunction dispatcher(FUNCTION_ADDR(EntryHookTrampoline));
4253 __ li(t9, Operand(ExternalReference(&dispatcher,
4254 ExternalReference::BUILTIN_CALL,
4255 isolate())));
4256#endif
4257 // Call C function through t9 to conform ABI for PIC.
4258 __ Call(t9);
4259
4260 // Restore the stack pointer if needed.
4261 if (frame_alignment > kPointerSize) {
4262 __ mov(sp, s5);
4263 } else {
4264 __ Addu(sp, sp, kCArgsSlotsSize);
4265 }
4266
4267 // Also pop ra to get Ret(0).
4268 __ MultiPop(kSavedRegs | ra.bit());
4269 __ Ret();
4270}
4271
4272
4273template<class T>
4274static void CreateArrayDispatch(MacroAssembler* masm,
4275 AllocationSiteOverrideMode mode) {
4276 if (mode == DISABLE_ALLOCATION_SITES) {
4277 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
4278 __ TailCallStub(&stub);
4279 } else if (mode == DONT_OVERRIDE) {
4280 int last_index = GetSequenceIndexFromFastElementsKind(
4281 TERMINAL_FAST_ELEMENTS_KIND);
4282 for (int i = 0; i <= last_index; ++i) {
4283 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4284 T stub(masm->isolate(), kind);
4285 __ TailCallStub(&stub, eq, a3, Operand(kind));
4286 }
4287
4288 // If we reached this point there is a problem.
4289 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4290 } else {
4291 UNREACHABLE();
4292 }
4293}
4294
4295
4296static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4297 AllocationSiteOverrideMode mode) {
4298 // a2 - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4299 // a3 - kind (if mode != DISABLE_ALLOCATION_SITES)
4300 // a0 - number of arguments
4301 // a1 - constructor?
4302 // sp[0] - last argument
4303 Label normal_sequence;
4304 if (mode == DONT_OVERRIDE) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004305 STATIC_ASSERT(FAST_SMI_ELEMENTS == 0);
4306 STATIC_ASSERT(FAST_HOLEY_SMI_ELEMENTS == 1);
4307 STATIC_ASSERT(FAST_ELEMENTS == 2);
4308 STATIC_ASSERT(FAST_HOLEY_ELEMENTS == 3);
4309 STATIC_ASSERT(FAST_DOUBLE_ELEMENTS == 4);
4310 STATIC_ASSERT(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004311
4312 // is the low bit set? If so, we are holey and that is good.
4313 __ And(at, a3, Operand(1));
4314 __ Branch(&normal_sequence, ne, at, Operand(zero_reg));
4315 }
4316
4317 // look at the first argument
4318 __ lw(t1, MemOperand(sp, 0));
4319 __ Branch(&normal_sequence, eq, t1, Operand(zero_reg));
4320
4321 if (mode == DISABLE_ALLOCATION_SITES) {
4322 ElementsKind initial = GetInitialFastElementsKind();
4323 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4324
4325 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4326 holey_initial,
4327 DISABLE_ALLOCATION_SITES);
4328 __ TailCallStub(&stub_holey);
4329
4330 __ bind(&normal_sequence);
4331 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4332 initial,
4333 DISABLE_ALLOCATION_SITES);
4334 __ TailCallStub(&stub);
4335 } else if (mode == DONT_OVERRIDE) {
4336 // We are going to create a holey array, but our kind is non-holey.
4337 // Fix kind and retry (only if we have an allocation site in the slot).
4338 __ Addu(a3, a3, Operand(1));
4339
4340 if (FLAG_debug_code) {
4341 __ lw(t1, FieldMemOperand(a2, 0));
4342 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
4343 __ Assert(eq, kExpectedAllocationSite, t1, Operand(at));
4344 }
4345
4346 // Save the resulting elements kind in type info. We can't just store a3
4347 // in the AllocationSite::transition_info field because elements kind is
4348 // restricted to a portion of the field...upper bits need to be left alone.
4349 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4350 __ lw(t0, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4351 __ Addu(t0, t0, Operand(Smi::FromInt(kFastElementsKindPackedToHoley)));
4352 __ sw(t0, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4353
4354
4355 __ bind(&normal_sequence);
4356 int last_index = GetSequenceIndexFromFastElementsKind(
4357 TERMINAL_FAST_ELEMENTS_KIND);
4358 for (int i = 0; i <= last_index; ++i) {
4359 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4360 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4361 __ TailCallStub(&stub, eq, a3, Operand(kind));
4362 }
4363
4364 // If we reached this point there is a problem.
4365 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4366 } else {
4367 UNREACHABLE();
4368 }
4369}
4370
4371
4372template<class T>
4373static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4374 int to_index = GetSequenceIndexFromFastElementsKind(
4375 TERMINAL_FAST_ELEMENTS_KIND);
4376 for (int i = 0; i <= to_index; ++i) {
4377 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4378 T stub(isolate, kind);
4379 stub.GetCode();
4380 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4381 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4382 stub1.GetCode();
4383 }
4384 }
4385}
4386
4387
4388void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4389 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4390 isolate);
4391 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4392 isolate);
4393 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4394 isolate);
4395}
4396
4397
4398void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4399 Isolate* isolate) {
4400 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4401 for (int i = 0; i < 2; i++) {
4402 // For internal arrays we only need a few things.
4403 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4404 stubh1.GetCode();
4405 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4406 stubh2.GetCode();
4407 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4408 stubh3.GetCode();
4409 }
4410}
4411
4412
4413void ArrayConstructorStub::GenerateDispatchToArrayStub(
4414 MacroAssembler* masm,
4415 AllocationSiteOverrideMode mode) {
4416 if (argument_count() == ANY) {
4417 Label not_zero_case, not_one_case;
4418 __ And(at, a0, a0);
4419 __ Branch(&not_zero_case, ne, at, Operand(zero_reg));
4420 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4421
4422 __ bind(&not_zero_case);
4423 __ Branch(&not_one_case, gt, a0, Operand(1));
4424 CreateArrayDispatchOneArgument(masm, mode);
4425
4426 __ bind(&not_one_case);
4427 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4428 } else if (argument_count() == NONE) {
4429 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4430 } else if (argument_count() == ONE) {
4431 CreateArrayDispatchOneArgument(masm, mode);
4432 } else if (argument_count() == MORE_THAN_ONE) {
4433 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4434 } else {
4435 UNREACHABLE();
4436 }
4437}
4438
4439
4440void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4441 // ----------- S t a t e -------------
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004442 // -- a0 : argc (only if argument_count() is ANY or MORE_THAN_ONE)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004443 // -- a1 : constructor
4444 // -- a2 : AllocationSite or undefined
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004445 // -- a3 : Original constructor
4446 // -- sp[0] : last argument
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004447 // -----------------------------------
4448
4449 if (FLAG_debug_code) {
4450 // The array construct code is only set for the global and natives
4451 // builtin Array functions which always have maps.
4452
4453 // Initial map for the builtin Array function should be a map.
4454 __ lw(t0, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4455 // Will both indicate a NULL and a Smi.
4456 __ SmiTst(t0, at);
4457 __ Assert(ne, kUnexpectedInitialMapForArrayFunction,
4458 at, Operand(zero_reg));
4459 __ GetObjectType(t0, t0, t1);
4460 __ Assert(eq, kUnexpectedInitialMapForArrayFunction,
4461 t1, Operand(MAP_TYPE));
4462
4463 // We should either have undefined in a2 or a valid AllocationSite
4464 __ AssertUndefinedOrAllocationSite(a2, t0);
4465 }
4466
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004467 // Enter the context of the Array function.
4468 __ lw(cp, FieldMemOperand(a1, JSFunction::kContextOffset));
4469
4470 Label subclassing;
4471 __ Branch(&subclassing, ne, a1, Operand(a3));
4472
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004473 Label no_info;
4474 // Get the elements kind and case on that.
4475 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4476 __ Branch(&no_info, eq, a2, Operand(at));
4477
4478 __ lw(a3, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4479 __ SmiUntag(a3);
4480 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4481 __ And(a3, a3, Operand(AllocationSite::ElementsKindBits::kMask));
4482 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4483
4484 __ bind(&no_info);
4485 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004486
4487 // Subclassing.
4488 __ bind(&subclassing);
4489 switch (argument_count()) {
4490 case ANY:
4491 case MORE_THAN_ONE:
Ben Murdoch097c5b22016-05-18 11:27:45 +01004492 __ Lsa(at, sp, a0, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00004493 __ sw(a1, MemOperand(at));
4494 __ li(at, Operand(3));
4495 __ addu(a0, a0, at);
4496 break;
4497 case NONE:
4498 __ sw(a1, MemOperand(sp, 0 * kPointerSize));
4499 __ li(a0, Operand(3));
4500 break;
4501 case ONE:
4502 __ sw(a1, MemOperand(sp, 1 * kPointerSize));
4503 __ li(a0, Operand(4));
4504 break;
4505 }
4506 __ Push(a3, a2);
4507 __ JumpToExternalReference(ExternalReference(Runtime::kNewArray, isolate()));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004508}
4509
4510
4511void InternalArrayConstructorStub::GenerateCase(
4512 MacroAssembler* masm, ElementsKind kind) {
4513
4514 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4515 __ TailCallStub(&stub0, lo, a0, Operand(1));
4516
4517 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4518 __ TailCallStub(&stubN, hi, a0, Operand(1));
4519
4520 if (IsFastPackedElementsKind(kind)) {
4521 // We might need to create a holey array
4522 // look at the first argument.
4523 __ lw(at, MemOperand(sp, 0));
4524
4525 InternalArraySingleArgumentConstructorStub
4526 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4527 __ TailCallStub(&stub1_holey, ne, at, Operand(zero_reg));
4528 }
4529
4530 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4531 __ TailCallStub(&stub1);
4532}
4533
4534
4535void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4536 // ----------- S t a t e -------------
4537 // -- a0 : argc
4538 // -- a1 : constructor
4539 // -- sp[0] : return address
4540 // -- sp[4] : last argument
4541 // -----------------------------------
4542
4543 if (FLAG_debug_code) {
4544 // The array construct code is only set for the global and natives
4545 // builtin Array functions which always have maps.
4546
4547 // Initial map for the builtin Array function should be a map.
4548 __ lw(a3, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4549 // Will both indicate a NULL and a Smi.
4550 __ SmiTst(a3, at);
4551 __ Assert(ne, kUnexpectedInitialMapForArrayFunction,
4552 at, Operand(zero_reg));
4553 __ GetObjectType(a3, a3, t0);
4554 __ Assert(eq, kUnexpectedInitialMapForArrayFunction,
4555 t0, Operand(MAP_TYPE));
4556 }
4557
4558 // Figure out the right elements kind.
4559 __ lw(a3, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4560
4561 // Load the map's "bit field 2" into a3. We only need the first byte,
4562 // but the following bit field extraction takes care of that anyway.
4563 __ lbu(a3, FieldMemOperand(a3, Map::kBitField2Offset));
4564 // Retrieve elements_kind from bit field 2.
4565 __ DecodeField<Map::ElementsKindBits>(a3);
4566
4567 if (FLAG_debug_code) {
4568 Label done;
4569 __ Branch(&done, eq, a3, Operand(FAST_ELEMENTS));
4570 __ Assert(
4571 eq, kInvalidElementsKindForInternalArrayOrInternalPackedArray,
4572 a3, Operand(FAST_HOLEY_ELEMENTS));
4573 __ bind(&done);
4574 }
4575
4576 Label fast_elements_case;
4577 __ Branch(&fast_elements_case, eq, a3, Operand(FAST_ELEMENTS));
4578 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4579
4580 __ bind(&fast_elements_case);
4581 GenerateCase(masm, FAST_ELEMENTS);
4582}
4583
4584
Ben Murdoch097c5b22016-05-18 11:27:45 +01004585void FastNewObjectStub::Generate(MacroAssembler* masm) {
4586 // ----------- S t a t e -------------
4587 // -- a1 : target
4588 // -- a3 : new target
4589 // -- cp : context
4590 // -- ra : return address
4591 // -----------------------------------
4592 __ AssertFunction(a1);
4593 __ AssertReceiver(a3);
4594
4595 // Verify that the new target is a JSFunction.
4596 Label new_object;
4597 __ GetObjectType(a3, a2, a2);
4598 __ Branch(&new_object, ne, a2, Operand(JS_FUNCTION_TYPE));
4599
4600 // Load the initial map and verify that it's in fact a map.
4601 __ lw(a2, FieldMemOperand(a3, JSFunction::kPrototypeOrInitialMapOffset));
4602 __ JumpIfSmi(a2, &new_object);
4603 __ GetObjectType(a2, a0, a0);
4604 __ Branch(&new_object, ne, a0, Operand(MAP_TYPE));
4605
4606 // Fall back to runtime if the target differs from the new target's
4607 // initial map constructor.
4608 __ lw(a0, FieldMemOperand(a2, Map::kConstructorOrBackPointerOffset));
4609 __ Branch(&new_object, ne, a0, Operand(a1));
4610
4611 // Allocate the JSObject on the heap.
4612 Label allocate, done_allocate;
4613 __ lbu(t0, FieldMemOperand(a2, Map::kInstanceSizeOffset));
4614 __ Allocate(t0, v0, t1, a0, &allocate, SIZE_IN_WORDS);
4615 __ bind(&done_allocate);
4616
4617 // Initialize the JSObject fields.
Ben Murdochc5610432016-08-08 18:44:38 +01004618 __ sw(a2, FieldMemOperand(v0, JSObject::kMapOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004619 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
Ben Murdochc5610432016-08-08 18:44:38 +01004620 __ sw(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
4621 __ sw(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004622 STATIC_ASSERT(JSObject::kHeaderSize == 3 * kPointerSize);
Ben Murdochc5610432016-08-08 18:44:38 +01004623 __ Addu(a1, v0, Operand(JSObject::kHeaderSize - kHeapObjectTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004624
4625 // ----------- S t a t e -------------
Ben Murdochc5610432016-08-08 18:44:38 +01004626 // -- v0 : result (tagged)
Ben Murdoch097c5b22016-05-18 11:27:45 +01004627 // -- a1 : result fields (untagged)
4628 // -- t1 : result end (untagged)
4629 // -- a2 : initial map
4630 // -- cp : context
4631 // -- ra : return address
4632 // -----------------------------------
4633
4634 // Perform in-object slack tracking if requested.
4635 Label slack_tracking;
4636 STATIC_ASSERT(Map::kNoSlackTracking == 0);
4637 __ lw(a3, FieldMemOperand(a2, Map::kBitField3Offset));
4638 __ And(at, a3, Operand(Map::ConstructionCounter::kMask));
4639 __ Branch(USE_DELAY_SLOT, &slack_tracking, ne, at, Operand(0));
4640 __ LoadRoot(a0, Heap::kUndefinedValueRootIndex); // In delay slot.
4641 {
4642 // Initialize all in-object fields with undefined.
4643 __ InitializeFieldsWithFiller(a1, t1, a0);
Ben Murdochc5610432016-08-08 18:44:38 +01004644 __ Ret();
Ben Murdoch097c5b22016-05-18 11:27:45 +01004645 }
4646 __ bind(&slack_tracking);
4647 {
4648 // Decrease generous allocation count.
4649 STATIC_ASSERT(Map::ConstructionCounter::kNext == 32);
4650 __ Subu(a3, a3, Operand(1 << Map::ConstructionCounter::kShift));
4651 __ sw(a3, FieldMemOperand(a2, Map::kBitField3Offset));
4652
4653 // Initialize the in-object fields with undefined.
4654 __ lbu(t0, FieldMemOperand(a2, Map::kUnusedPropertyFieldsOffset));
4655 __ sll(t0, t0, kPointerSizeLog2);
4656 __ subu(t0, t1, t0);
4657 __ InitializeFieldsWithFiller(a1, t0, a0);
4658
4659 // Initialize the remaining (reserved) fields with one pointer filler map.
4660 __ LoadRoot(a0, Heap::kOnePointerFillerMapRootIndex);
4661 __ InitializeFieldsWithFiller(a1, t1, a0);
4662
4663 // Check if we can finalize the instance size.
4664 Label finalize;
4665 STATIC_ASSERT(Map::kSlackTrackingCounterEnd == 1);
4666 __ And(a3, a3, Operand(Map::ConstructionCounter::kMask));
Ben Murdochc5610432016-08-08 18:44:38 +01004667 __ Branch(&finalize, eq, a3, Operand(zero_reg));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004668 __ Ret();
4669
4670 // Finalize the instance size.
4671 __ bind(&finalize);
4672 {
4673 FrameScope scope(masm, StackFrame::INTERNAL);
4674 __ Push(v0, a2);
4675 __ CallRuntime(Runtime::kFinalizeInstanceSize);
4676 __ Pop(v0);
4677 }
4678 __ Ret();
4679 }
4680
4681 // Fall back to %AllocateInNewSpace.
4682 __ bind(&allocate);
4683 {
4684 FrameScope scope(masm, StackFrame::INTERNAL);
4685 STATIC_ASSERT(kSmiTag == 0);
4686 STATIC_ASSERT(kSmiTagSize == 1);
4687 __ sll(t0, t0, kPointerSizeLog2 + kSmiTagSize);
4688 __ Push(a2, t0);
4689 __ CallRuntime(Runtime::kAllocateInNewSpace);
4690 __ Pop(a2);
4691 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01004692 __ lbu(t1, FieldMemOperand(a2, Map::kInstanceSizeOffset));
4693 __ Lsa(t1, v0, t1, kPointerSizeLog2);
Ben Murdochc5610432016-08-08 18:44:38 +01004694 STATIC_ASSERT(kHeapObjectTag == 1);
4695 __ Subu(t1, t1, Operand(kHeapObjectTag));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004696 __ jmp(&done_allocate);
4697
4698 // Fall back to %NewObject.
4699 __ bind(&new_object);
4700 __ Push(a1, a3);
4701 __ TailCallRuntime(Runtime::kNewObject);
4702}
4703
4704
4705void FastNewRestParameterStub::Generate(MacroAssembler* masm) {
4706 // ----------- S t a t e -------------
4707 // -- a1 : function
4708 // -- cp : context
4709 // -- fp : frame pointer
4710 // -- ra : return address
4711 // -----------------------------------
4712 __ AssertFunction(a1);
4713
Ben Murdochc5610432016-08-08 18:44:38 +01004714 // Make a2 point to the JavaScript frame.
4715 __ mov(a2, fp);
4716 if (skip_stub_frame()) {
4717 // For Ignition we need to skip the handler/stub frame to reach the
4718 // JavaScript frame for the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01004719 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004720 }
4721 if (FLAG_debug_code) {
4722 Label ok;
Ben Murdochda12d292016-06-02 14:46:10 +01004723 __ lw(a3, MemOperand(a2, StandardFrameConstants::kFunctionOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004724 __ Branch(&ok, eq, a1, Operand(a3));
4725 __ Abort(kInvalidFrameForFastNewRestArgumentsStub);
4726 __ bind(&ok);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004727 }
4728
4729 // Check if we have rest parameters (only possible if we have an
4730 // arguments adaptor frame below the function frame).
4731 Label no_rest_parameters;
4732 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004733 __ lw(a3, MemOperand(a2, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004734 __ Branch(&no_rest_parameters, ne, a3,
4735 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4736
4737 // Check if the arguments adaptor frame contains more arguments than
4738 // specified by the function's internal formal parameter count.
4739 Label rest_parameters;
4740 __ lw(a0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4741 __ lw(a1, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
4742 __ lw(a1,
4743 FieldMemOperand(a1, SharedFunctionInfo::kFormalParameterCountOffset));
4744 __ Subu(a0, a0, Operand(a1));
4745 __ Branch(&rest_parameters, gt, a0, Operand(zero_reg));
4746
4747 // Return an empty rest parameter array.
4748 __ bind(&no_rest_parameters);
4749 {
4750 // ----------- S t a t e -------------
4751 // -- cp : context
4752 // -- ra : return address
4753 // -----------------------------------
4754
4755 // Allocate an empty rest parameter array.
4756 Label allocate, done_allocate;
Ben Murdochc5610432016-08-08 18:44:38 +01004757 __ Allocate(JSArray::kSize, v0, a0, a1, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004758 __ bind(&done_allocate);
4759
4760 // Setup the rest parameter array in v0.
4761 __ LoadNativeContextSlot(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, a1);
4762 __ sw(a1, FieldMemOperand(v0, JSArray::kMapOffset));
4763 __ LoadRoot(a1, Heap::kEmptyFixedArrayRootIndex);
4764 __ sw(a1, FieldMemOperand(v0, JSArray::kPropertiesOffset));
4765 __ sw(a1, FieldMemOperand(v0, JSArray::kElementsOffset));
4766 __ Move(a1, Smi::FromInt(0));
4767 __ Ret(USE_DELAY_SLOT);
4768 __ sw(a1, FieldMemOperand(v0, JSArray::kLengthOffset)); // In delay slot
4769 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4770
4771 // Fall back to %AllocateInNewSpace.
4772 __ bind(&allocate);
4773 {
4774 FrameScope scope(masm, StackFrame::INTERNAL);
4775 __ Push(Smi::FromInt(JSArray::kSize));
4776 __ CallRuntime(Runtime::kAllocateInNewSpace);
4777 }
4778 __ jmp(&done_allocate);
4779 }
4780
4781 __ bind(&rest_parameters);
4782 {
4783 // Compute the pointer to the first rest parameter (skippping the receiver).
4784 __ Lsa(a2, a2, a0, kPointerSizeLog2 - 1);
4785 __ Addu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset -
4786 1 * kPointerSize));
4787
4788 // ----------- S t a t e -------------
4789 // -- cp : context
4790 // -- a0 : number of rest parameters (tagged)
4791 // -- a2 : pointer to first rest parameters
4792 // -- ra : return address
4793 // -----------------------------------
4794
4795 // Allocate space for the rest parameter array plus the backing store.
4796 Label allocate, done_allocate;
4797 __ li(a1, Operand(JSArray::kSize + FixedArray::kHeaderSize));
4798 __ Lsa(a1, a1, a0, kPointerSizeLog2 - 1);
Ben Murdochc5610432016-08-08 18:44:38 +01004799 __ Allocate(a1, v0, a3, t0, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004800 __ bind(&done_allocate);
4801
4802 // Setup the elements array in v0.
4803 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
4804 __ sw(at, FieldMemOperand(v0, FixedArray::kMapOffset));
4805 __ sw(a0, FieldMemOperand(v0, FixedArray::kLengthOffset));
4806 __ Addu(a3, v0, Operand(FixedArray::kHeaderSize));
4807 {
4808 Label loop, done_loop;
4809 __ sll(at, a0, kPointerSizeLog2 - 1);
4810 __ Addu(a1, a3, at);
4811 __ bind(&loop);
4812 __ Branch(&done_loop, eq, a1, Operand(a3));
4813 __ lw(at, MemOperand(a2, 0 * kPointerSize));
4814 __ sw(at, FieldMemOperand(a3, 0 * kPointerSize));
4815 __ Subu(a2, a2, Operand(1 * kPointerSize));
4816 __ Addu(a3, a3, Operand(1 * kPointerSize));
4817 __ jmp(&loop);
4818 __ bind(&done_loop);
4819 }
4820
4821 // Setup the rest parameter array in a3.
4822 __ LoadNativeContextSlot(Context::JS_ARRAY_FAST_ELEMENTS_MAP_INDEX, at);
4823 __ sw(at, FieldMemOperand(a3, JSArray::kMapOffset));
4824 __ LoadRoot(at, Heap::kEmptyFixedArrayRootIndex);
4825 __ sw(at, FieldMemOperand(a3, JSArray::kPropertiesOffset));
4826 __ sw(v0, FieldMemOperand(a3, JSArray::kElementsOffset));
4827 __ sw(a0, FieldMemOperand(a3, JSArray::kLengthOffset));
4828 STATIC_ASSERT(JSArray::kSize == 4 * kPointerSize);
4829 __ Ret(USE_DELAY_SLOT);
4830 __ mov(v0, a3); // In delay slot
4831
4832 // Fall back to %AllocateInNewSpace.
4833 __ bind(&allocate);
4834 {
4835 FrameScope scope(masm, StackFrame::INTERNAL);
4836 __ SmiTag(a1);
4837 __ Push(a0, a2, a1);
4838 __ CallRuntime(Runtime::kAllocateInNewSpace);
4839 __ Pop(a0, a2);
4840 }
4841 __ jmp(&done_allocate);
4842 }
4843}
4844
4845
4846void FastNewSloppyArgumentsStub::Generate(MacroAssembler* masm) {
4847 // ----------- S t a t e -------------
4848 // -- a1 : function
4849 // -- cp : context
4850 // -- fp : frame pointer
4851 // -- ra : return address
4852 // -----------------------------------
4853 __ AssertFunction(a1);
4854
Ben Murdochc5610432016-08-08 18:44:38 +01004855 // Make t0 point to the JavaScript frame.
4856 __ mov(t0, fp);
4857 if (skip_stub_frame()) {
4858 // For Ignition we need to skip the handler/stub frame to reach the
4859 // JavaScript frame for the function.
4860 __ lw(t0, MemOperand(t0, StandardFrameConstants::kCallerFPOffset));
4861 }
4862 if (FLAG_debug_code) {
4863 Label ok;
4864 __ lw(a3, MemOperand(t0, StandardFrameConstants::kFunctionOffset));
4865 __ Branch(&ok, eq, a1, Operand(a3));
4866 __ Abort(kInvalidFrameForFastNewRestArgumentsStub);
4867 __ bind(&ok);
4868 }
4869
Ben Murdoch097c5b22016-05-18 11:27:45 +01004870 // TODO(bmeurer): Cleanup to match the FastNewStrictArgumentsStub.
4871 __ lw(a2, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
4872 __ lw(a2,
4873 FieldMemOperand(a2, SharedFunctionInfo::kFormalParameterCountOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01004874 __ Lsa(a3, t0, a2, kPointerSizeLog2 - 1);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004875 __ Addu(a3, a3, Operand(StandardFrameConstants::kCallerSPOffset));
4876
4877 // a1 : function
4878 // a2 : number of parameters (tagged)
4879 // a3 : parameters pointer
Ben Murdochc5610432016-08-08 18:44:38 +01004880 // t0 : Javascript frame pointer
Ben Murdoch097c5b22016-05-18 11:27:45 +01004881 // Registers used over whole function:
4882 // t1 : arguments count (tagged)
4883 // t2 : mapped parameter count (tagged)
4884
4885 // Check if the calling frame is an arguments adaptor frame.
4886 Label adaptor_frame, try_allocate, runtime;
Ben Murdochc5610432016-08-08 18:44:38 +01004887 __ lw(t0, MemOperand(t0, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01004888 __ lw(a0, MemOperand(t0, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01004889 __ Branch(&adaptor_frame, eq, a0,
4890 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4891
4892 // No adaptor, parameter count = argument count.
4893 __ mov(t1, a2);
4894 __ Branch(USE_DELAY_SLOT, &try_allocate);
4895 __ mov(t2, a2); // In delay slot.
4896
4897 // We have an adaptor frame. Patch the parameters pointer.
4898 __ bind(&adaptor_frame);
4899 __ lw(t1, MemOperand(t0, ArgumentsAdaptorFrameConstants::kLengthOffset));
4900 __ Lsa(t0, t0, t1, 1);
4901 __ Addu(a3, t0, Operand(StandardFrameConstants::kCallerSPOffset));
4902
4903 // t1 = argument count (tagged)
4904 // t2 = parameter count (tagged)
4905 // Compute the mapped parameter count = min(t2, t1) in t2.
4906 __ mov(t2, a2);
4907 __ Branch(&try_allocate, le, t2, Operand(t1));
4908 __ mov(t2, t1);
4909
4910 __ bind(&try_allocate);
4911
4912 // Compute the sizes of backing store, parameter map, and arguments object.
4913 // 1. Parameter map, has 2 extra words containing context and backing store.
4914 const int kParameterMapHeaderSize =
4915 FixedArray::kHeaderSize + 2 * kPointerSize;
4916 // If there are no mapped parameters, we do not need the parameter_map.
4917 Label param_map_size;
4918 DCHECK_EQ(static_cast<Smi*>(0), Smi::FromInt(0));
4919 __ Branch(USE_DELAY_SLOT, &param_map_size, eq, t2, Operand(zero_reg));
4920 __ mov(t5, zero_reg); // In delay slot: param map size = 0 when t2 == 0.
4921 __ sll(t5, t2, 1);
4922 __ addiu(t5, t5, kParameterMapHeaderSize);
4923 __ bind(&param_map_size);
4924
4925 // 2. Backing store.
4926 __ Lsa(t5, t5, t1, 1);
4927 __ Addu(t5, t5, Operand(FixedArray::kHeaderSize));
4928
4929 // 3. Arguments object.
4930 __ Addu(t5, t5, Operand(JSSloppyArgumentsObject::kSize));
4931
4932 // Do the allocation of all three objects in one go.
Ben Murdochc5610432016-08-08 18:44:38 +01004933 __ Allocate(t5, v0, t5, t0, &runtime, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01004934
4935 // v0 = address of new object(s) (tagged)
4936 // a2 = argument count (smi-tagged)
4937 // Get the arguments boilerplate from the current native context into t0.
4938 const int kNormalOffset =
4939 Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX);
4940 const int kAliasedOffset =
4941 Context::SlotOffset(Context::FAST_ALIASED_ARGUMENTS_MAP_INDEX);
4942
4943 __ lw(t0, NativeContextMemOperand());
4944 Label skip2_ne, skip2_eq;
4945 __ Branch(&skip2_ne, ne, t2, Operand(zero_reg));
4946 __ lw(t0, MemOperand(t0, kNormalOffset));
4947 __ bind(&skip2_ne);
4948
4949 __ Branch(&skip2_eq, eq, t2, Operand(zero_reg));
4950 __ lw(t0, MemOperand(t0, kAliasedOffset));
4951 __ bind(&skip2_eq);
4952
4953 // v0 = address of new object (tagged)
4954 // a2 = argument count (smi-tagged)
4955 // t0 = address of arguments map (tagged)
4956 // t2 = mapped parameter count (tagged)
4957 __ sw(t0, FieldMemOperand(v0, JSObject::kMapOffset));
4958 __ LoadRoot(t5, Heap::kEmptyFixedArrayRootIndex);
4959 __ sw(t5, FieldMemOperand(v0, JSObject::kPropertiesOffset));
4960 __ sw(t5, FieldMemOperand(v0, JSObject::kElementsOffset));
4961
4962 // Set up the callee in-object property.
4963 __ AssertNotSmi(a1);
4964 __ sw(a1, FieldMemOperand(v0, JSSloppyArgumentsObject::kCalleeOffset));
4965
4966 // Use the length (smi tagged) and set that as an in-object property too.
4967 __ AssertSmi(t1);
4968 __ sw(t1, FieldMemOperand(v0, JSSloppyArgumentsObject::kLengthOffset));
4969
4970 // Set up the elements pointer in the allocated arguments object.
4971 // If we allocated a parameter map, t0 will point there, otherwise
4972 // it will point to the backing store.
4973 __ Addu(t0, v0, Operand(JSSloppyArgumentsObject::kSize));
4974 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
4975
4976 // v0 = address of new object (tagged)
4977 // a2 = argument count (tagged)
4978 // t0 = address of parameter map or backing store (tagged)
4979 // t2 = mapped parameter count (tagged)
4980 // Initialize parameter map. If there are no mapped arguments, we're done.
4981 Label skip_parameter_map;
4982 Label skip3;
4983 __ Branch(&skip3, ne, t2, Operand(Smi::FromInt(0)));
4984 // Move backing store address to a1, because it is
4985 // expected there when filling in the unmapped arguments.
4986 __ mov(a1, t0);
4987 __ bind(&skip3);
4988
4989 __ Branch(&skip_parameter_map, eq, t2, Operand(Smi::FromInt(0)));
4990
4991 __ LoadRoot(t1, Heap::kSloppyArgumentsElementsMapRootIndex);
4992 __ sw(t1, FieldMemOperand(t0, FixedArray::kMapOffset));
4993 __ Addu(t1, t2, Operand(Smi::FromInt(2)));
4994 __ sw(t1, FieldMemOperand(t0, FixedArray::kLengthOffset));
4995 __ sw(cp, FieldMemOperand(t0, FixedArray::kHeaderSize + 0 * kPointerSize));
4996 __ Lsa(t1, t0, t2, 1);
4997 __ Addu(t1, t1, Operand(kParameterMapHeaderSize));
4998 __ sw(t1, FieldMemOperand(t0, FixedArray::kHeaderSize + 1 * kPointerSize));
4999
5000 // Copy the parameter slots and the holes in the arguments.
5001 // We need to fill in mapped_parameter_count slots. They index the context,
5002 // where parameters are stored in reverse order, at
5003 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
5004 // The mapped parameter thus need to get indices
5005 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
5006 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
5007 // We loop from right to left.
5008 Label parameters_loop, parameters_test;
5009 __ mov(t1, t2);
5010 __ Addu(t5, a2, Operand(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
5011 __ Subu(t5, t5, Operand(t2));
5012 __ LoadRoot(t3, Heap::kTheHoleValueRootIndex);
5013 __ Lsa(a1, t0, t1, 1);
5014 __ Addu(a1, a1, Operand(kParameterMapHeaderSize));
5015
5016 // a1 = address of backing store (tagged)
5017 // t0 = address of parameter map (tagged)
5018 // a0 = temporary scratch (a.o., for address calculation)
5019 // t1 = loop variable (tagged)
5020 // t3 = the hole value
5021 __ jmp(&parameters_test);
5022
5023 __ bind(&parameters_loop);
5024 __ Subu(t1, t1, Operand(Smi::FromInt(1)));
5025 __ sll(a0, t1, 1);
5026 __ Addu(a0, a0, Operand(kParameterMapHeaderSize - kHeapObjectTag));
5027 __ Addu(t6, t0, a0);
5028 __ sw(t5, MemOperand(t6));
5029 __ Subu(a0, a0, Operand(kParameterMapHeaderSize - FixedArray::kHeaderSize));
5030 __ Addu(t6, a1, a0);
5031 __ sw(t3, MemOperand(t6));
5032 __ Addu(t5, t5, Operand(Smi::FromInt(1)));
5033 __ bind(&parameters_test);
5034 __ Branch(&parameters_loop, ne, t1, Operand(Smi::FromInt(0)));
5035
5036 // t1 = argument count (tagged).
5037 __ lw(t1, FieldMemOperand(v0, JSSloppyArgumentsObject::kLengthOffset));
5038
5039 __ bind(&skip_parameter_map);
5040 // v0 = address of new object (tagged)
5041 // a1 = address of backing store (tagged)
5042 // t1 = argument count (tagged)
5043 // t2 = mapped parameter count (tagged)
5044 // t5 = scratch
5045 // Copy arguments header and remaining slots (if there are any).
5046 __ LoadRoot(t5, Heap::kFixedArrayMapRootIndex);
5047 __ sw(t5, FieldMemOperand(a1, FixedArray::kMapOffset));
5048 __ sw(t1, FieldMemOperand(a1, FixedArray::kLengthOffset));
5049
5050 Label arguments_loop, arguments_test;
5051 __ sll(t6, t2, 1);
5052 __ Subu(a3, a3, Operand(t6));
5053 __ jmp(&arguments_test);
5054
5055 __ bind(&arguments_loop);
5056 __ Subu(a3, a3, Operand(kPointerSize));
5057 __ lw(t0, MemOperand(a3, 0));
5058 __ Lsa(t5, a1, t2, 1);
5059 __ sw(t0, FieldMemOperand(t5, FixedArray::kHeaderSize));
5060 __ Addu(t2, t2, Operand(Smi::FromInt(1)));
5061
5062 __ bind(&arguments_test);
5063 __ Branch(&arguments_loop, lt, t2, Operand(t1));
5064
5065 // Return.
5066 __ Ret();
5067
5068 // Do the runtime call to allocate the arguments object.
5069 // t1 = argument count (tagged)
5070 __ bind(&runtime);
5071 __ Push(a1, a3, t1);
5072 __ TailCallRuntime(Runtime::kNewSloppyArguments);
5073}
5074
5075
5076void FastNewStrictArgumentsStub::Generate(MacroAssembler* masm) {
5077 // ----------- S t a t e -------------
5078 // -- a1 : function
5079 // -- cp : context
5080 // -- fp : frame pointer
5081 // -- ra : return address
5082 // -----------------------------------
5083 __ AssertFunction(a1);
5084
Ben Murdochc5610432016-08-08 18:44:38 +01005085 // Make a2 point to the JavaScript frame.
5086 __ mov(a2, fp);
5087 if (skip_stub_frame()) {
5088 // For Ignition we need to skip the handler/stub frame to reach the
5089 // JavaScript frame for the function.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005090 __ lw(a2, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01005091 }
5092 if (FLAG_debug_code) {
5093 Label ok;
Ben Murdochda12d292016-06-02 14:46:10 +01005094 __ lw(a3, MemOperand(a2, StandardFrameConstants::kFunctionOffset));
Ben Murdochc5610432016-08-08 18:44:38 +01005095 __ Branch(&ok, eq, a1, Operand(a3));
5096 __ Abort(kInvalidFrameForFastNewRestArgumentsStub);
5097 __ bind(&ok);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005098 }
5099
5100 // Check if we have an arguments adaptor frame below the function frame.
5101 Label arguments_adaptor, arguments_done;
5102 __ lw(a3, MemOperand(a2, StandardFrameConstants::kCallerFPOffset));
Ben Murdochda12d292016-06-02 14:46:10 +01005103 __ lw(a0, MemOperand(a3, CommonFrameConstants::kContextOrFrameTypeOffset));
Ben Murdoch097c5b22016-05-18 11:27:45 +01005104 __ Branch(&arguments_adaptor, eq, a0,
5105 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
5106 {
5107 __ lw(a1, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
5108 __ lw(a0,
5109 FieldMemOperand(a1, SharedFunctionInfo::kFormalParameterCountOffset));
5110 __ Lsa(a2, a2, a0, kPointerSizeLog2 - 1);
5111 __ Addu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset -
5112 1 * kPointerSize));
5113 }
5114 __ Branch(&arguments_done);
5115 __ bind(&arguments_adaptor);
5116 {
5117 __ lw(a0, MemOperand(a3, ArgumentsAdaptorFrameConstants::kLengthOffset));
5118 __ Lsa(a2, a3, a0, kPointerSizeLog2 - 1);
5119 __ Addu(a2, a2, Operand(StandardFrameConstants::kCallerSPOffset -
5120 1 * kPointerSize));
5121 }
5122 __ bind(&arguments_done);
5123
5124 // ----------- S t a t e -------------
5125 // -- cp : context
5126 // -- a0 : number of rest parameters (tagged)
5127 // -- a2 : pointer to first rest parameters
5128 // -- ra : return address
5129 // -----------------------------------
5130
5131 // Allocate space for the strict arguments object plus the backing store.
5132 Label allocate, done_allocate;
5133 __ li(a1, Operand(JSStrictArgumentsObject::kSize + FixedArray::kHeaderSize));
5134 __ Lsa(a1, a1, a0, kPointerSizeLog2 - 1);
Ben Murdochc5610432016-08-08 18:44:38 +01005135 __ Allocate(a1, v0, a3, t0, &allocate, NO_ALLOCATION_FLAGS);
Ben Murdoch097c5b22016-05-18 11:27:45 +01005136 __ bind(&done_allocate);
5137
5138 // Setup the elements array in v0.
5139 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
5140 __ sw(at, FieldMemOperand(v0, FixedArray::kMapOffset));
5141 __ sw(a0, FieldMemOperand(v0, FixedArray::kLengthOffset));
5142 __ Addu(a3, v0, Operand(FixedArray::kHeaderSize));
5143 {
5144 Label loop, done_loop;
5145 __ sll(at, a0, kPointerSizeLog2 - 1);
5146 __ Addu(a1, a3, at);
5147 __ bind(&loop);
5148 __ Branch(&done_loop, eq, a1, Operand(a3));
5149 __ lw(at, MemOperand(a2, 0 * kPointerSize));
5150 __ sw(at, FieldMemOperand(a3, 0 * kPointerSize));
5151 __ Subu(a2, a2, Operand(1 * kPointerSize));
5152 __ Addu(a3, a3, Operand(1 * kPointerSize));
5153 __ Branch(&loop);
5154 __ bind(&done_loop);
5155 }
5156
5157 // Setup the strict arguments object in a3.
5158 __ LoadNativeContextSlot(Context::STRICT_ARGUMENTS_MAP_INDEX, at);
5159 __ sw(at, FieldMemOperand(a3, JSStrictArgumentsObject::kMapOffset));
5160 __ LoadRoot(at, Heap::kEmptyFixedArrayRootIndex);
5161 __ sw(at, FieldMemOperand(a3, JSStrictArgumentsObject::kPropertiesOffset));
5162 __ sw(v0, FieldMemOperand(a3, JSStrictArgumentsObject::kElementsOffset));
5163 __ sw(a0, FieldMemOperand(a3, JSStrictArgumentsObject::kLengthOffset));
5164 STATIC_ASSERT(JSStrictArgumentsObject::kSize == 4 * kPointerSize);
5165 __ Ret(USE_DELAY_SLOT);
5166 __ mov(v0, a3); // In delay slot
5167
5168 // Fall back to %AllocateInNewSpace.
5169 __ bind(&allocate);
5170 {
5171 FrameScope scope(masm, StackFrame::INTERNAL);
5172 __ SmiTag(a1);
5173 __ Push(a0, a2, a1);
5174 __ CallRuntime(Runtime::kAllocateInNewSpace);
5175 __ Pop(a0, a2);
5176 }
5177 __ jmp(&done_allocate);
5178}
5179
5180
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005181void LoadGlobalViaContextStub::Generate(MacroAssembler* masm) {
5182 Register context_reg = cp;
5183 Register slot_reg = a2;
5184 Register result_reg = v0;
5185 Label slow_case;
5186
5187 // Go up context chain to the script context.
5188 for (int i = 0; i < depth(); ++i) {
5189 __ lw(result_reg, ContextMemOperand(context_reg, Context::PREVIOUS_INDEX));
5190 context_reg = result_reg;
5191 }
5192
5193 // Load the PropertyCell value at the specified slot.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005194 __ Lsa(at, context_reg, slot_reg, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005195 __ lw(result_reg, ContextMemOperand(at, 0));
5196 __ lw(result_reg, FieldMemOperand(result_reg, PropertyCell::kValueOffset));
5197
5198 // Check that value is not the_hole.
5199 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
5200 __ Branch(&slow_case, eq, result_reg, Operand(at));
5201 __ Ret();
5202
5203 // Fallback to the runtime.
5204 __ bind(&slow_case);
5205 __ SmiTag(slot_reg);
5206 __ Push(slot_reg);
5207 __ TailCallRuntime(Runtime::kLoadGlobalViaContext);
5208}
5209
5210
5211void StoreGlobalViaContextStub::Generate(MacroAssembler* masm) {
5212 Register context_reg = cp;
5213 Register slot_reg = a2;
5214 Register value_reg = a0;
5215 Register cell_reg = t0;
5216 Register cell_value_reg = t1;
5217 Register cell_details_reg = t2;
5218 Label fast_heapobject_case, fast_smi_case, slow_case;
5219
5220 if (FLAG_debug_code) {
5221 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
5222 __ Check(ne, kUnexpectedValue, value_reg, Operand(at));
5223 }
5224
5225 // Go up context chain to the script context.
5226 for (int i = 0; i < depth(); ++i) {
5227 __ lw(cell_reg, ContextMemOperand(context_reg, Context::PREVIOUS_INDEX));
5228 context_reg = cell_reg;
5229 }
5230
5231 // Load the PropertyCell at the specified slot.
Ben Murdoch097c5b22016-05-18 11:27:45 +01005232 __ Lsa(at, context_reg, slot_reg, kPointerSizeLog2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005233 __ lw(cell_reg, ContextMemOperand(at, 0));
5234
5235 // Load PropertyDetails for the cell (actually only the cell_type and kind).
5236 __ lw(cell_details_reg,
5237 FieldMemOperand(cell_reg, PropertyCell::kDetailsOffset));
5238 __ SmiUntag(cell_details_reg);
5239 __ And(cell_details_reg, cell_details_reg,
5240 PropertyDetails::PropertyCellTypeField::kMask |
5241 PropertyDetails::KindField::kMask |
5242 PropertyDetails::kAttributesReadOnlyMask);
5243
5244 // Check if PropertyCell holds mutable data.
5245 Label not_mutable_data;
5246 __ Branch(&not_mutable_data, ne, cell_details_reg,
5247 Operand(PropertyDetails::PropertyCellTypeField::encode(
5248 PropertyCellType::kMutable) |
5249 PropertyDetails::KindField::encode(kData)));
5250 __ JumpIfSmi(value_reg, &fast_smi_case);
5251 __ bind(&fast_heapobject_case);
5252 __ sw(value_reg, FieldMemOperand(cell_reg, PropertyCell::kValueOffset));
5253 __ RecordWriteField(cell_reg, PropertyCell::kValueOffset, value_reg,
5254 cell_details_reg, kRAHasNotBeenSaved, kDontSaveFPRegs,
5255 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
5256 // RecordWriteField clobbers the value register, so we need to reload.
5257 __ Ret(USE_DELAY_SLOT);
5258 __ lw(value_reg, FieldMemOperand(cell_reg, PropertyCell::kValueOffset));
5259 __ bind(&not_mutable_data);
5260
5261 // Check if PropertyCell value matches the new value (relevant for Constant,
5262 // ConstantType and Undefined cells).
5263 Label not_same_value;
5264 __ lw(cell_value_reg, FieldMemOperand(cell_reg, PropertyCell::kValueOffset));
5265 __ Branch(&not_same_value, ne, value_reg, Operand(cell_value_reg));
5266 // Make sure the PropertyCell is not marked READ_ONLY.
5267 __ And(at, cell_details_reg, PropertyDetails::kAttributesReadOnlyMask);
5268 __ Branch(&slow_case, ne, at, Operand(zero_reg));
5269 if (FLAG_debug_code) {
5270 Label done;
5271 // This can only be true for Constant, ConstantType and Undefined cells,
5272 // because we never store the_hole via this stub.
5273 __ Branch(&done, eq, cell_details_reg,
5274 Operand(PropertyDetails::PropertyCellTypeField::encode(
5275 PropertyCellType::kConstant) |
5276 PropertyDetails::KindField::encode(kData)));
5277 __ Branch(&done, eq, cell_details_reg,
5278 Operand(PropertyDetails::PropertyCellTypeField::encode(
5279 PropertyCellType::kConstantType) |
5280 PropertyDetails::KindField::encode(kData)));
5281 __ Check(eq, kUnexpectedValue, cell_details_reg,
5282 Operand(PropertyDetails::PropertyCellTypeField::encode(
5283 PropertyCellType::kUndefined) |
5284 PropertyDetails::KindField::encode(kData)));
5285 __ bind(&done);
5286 }
5287 __ Ret();
5288 __ bind(&not_same_value);
5289
5290 // Check if PropertyCell contains data with constant type (and is not
5291 // READ_ONLY).
5292 __ Branch(&slow_case, ne, cell_details_reg,
5293 Operand(PropertyDetails::PropertyCellTypeField::encode(
5294 PropertyCellType::kConstantType) |
5295 PropertyDetails::KindField::encode(kData)));
5296
5297 // Now either both old and new values must be SMIs or both must be heap
5298 // objects with same map.
5299 Label value_is_heap_object;
5300 __ JumpIfNotSmi(value_reg, &value_is_heap_object);
5301 __ JumpIfNotSmi(cell_value_reg, &slow_case);
5302 // Old and new values are SMIs, no need for a write barrier here.
5303 __ bind(&fast_smi_case);
5304 __ Ret(USE_DELAY_SLOT);
5305 __ sw(value_reg, FieldMemOperand(cell_reg, PropertyCell::kValueOffset));
5306 __ bind(&value_is_heap_object);
5307 __ JumpIfSmi(cell_value_reg, &slow_case);
5308 Register cell_value_map_reg = cell_value_reg;
5309 __ lw(cell_value_map_reg,
5310 FieldMemOperand(cell_value_reg, HeapObject::kMapOffset));
5311 __ Branch(&fast_heapobject_case, eq, cell_value_map_reg,
5312 FieldMemOperand(value_reg, HeapObject::kMapOffset));
5313
5314 // Fallback to the runtime.
5315 __ bind(&slow_case);
5316 __ SmiTag(slot_reg);
5317 __ Push(slot_reg, value_reg);
5318 __ TailCallRuntime(is_strict(language_mode())
5319 ? Runtime::kStoreGlobalViaContext_Strict
5320 : Runtime::kStoreGlobalViaContext_Sloppy);
5321}
5322
5323
5324static int AddressOffset(ExternalReference ref0, ExternalReference ref1) {
5325 return ref0.address() - ref1.address();
5326}
5327
5328
5329// Calls an API function. Allocates HandleScope, extracts returned value
5330// from handle and propagates exceptions. Restores context. stack_space
5331// - space to be unwound on exit (includes the call JS arguments space and
5332// the additional space allocated for the fast call).
5333static void CallApiFunctionAndReturn(
5334 MacroAssembler* masm, Register function_address,
5335 ExternalReference thunk_ref, int stack_space, int32_t stack_space_offset,
5336 MemOperand return_value_operand, MemOperand* context_restore_operand) {
5337 Isolate* isolate = masm->isolate();
5338 ExternalReference next_address =
5339 ExternalReference::handle_scope_next_address(isolate);
5340 const int kNextOffset = 0;
5341 const int kLimitOffset = AddressOffset(
5342 ExternalReference::handle_scope_limit_address(isolate), next_address);
5343 const int kLevelOffset = AddressOffset(
5344 ExternalReference::handle_scope_level_address(isolate), next_address);
5345
5346 DCHECK(function_address.is(a1) || function_address.is(a2));
5347
5348 Label profiler_disabled;
5349 Label end_profiler_check;
5350 __ li(t9, Operand(ExternalReference::is_profiling_address(isolate)));
5351 __ lb(t9, MemOperand(t9, 0));
5352 __ Branch(&profiler_disabled, eq, t9, Operand(zero_reg));
5353
5354 // Additional parameter is the address of the actual callback.
5355 __ li(t9, Operand(thunk_ref));
5356 __ jmp(&end_profiler_check);
5357
5358 __ bind(&profiler_disabled);
5359 __ mov(t9, function_address);
5360 __ bind(&end_profiler_check);
5361
5362 // Allocate HandleScope in callee-save registers.
5363 __ li(s3, Operand(next_address));
5364 __ lw(s0, MemOperand(s3, kNextOffset));
5365 __ lw(s1, MemOperand(s3, kLimitOffset));
5366 __ lw(s2, MemOperand(s3, kLevelOffset));
5367 __ Addu(s2, s2, Operand(1));
5368 __ sw(s2, MemOperand(s3, kLevelOffset));
5369
5370 if (FLAG_log_timer_events) {
5371 FrameScope frame(masm, StackFrame::MANUAL);
5372 __ PushSafepointRegisters();
5373 __ PrepareCallCFunction(1, a0);
5374 __ li(a0, Operand(ExternalReference::isolate_address(isolate)));
5375 __ CallCFunction(ExternalReference::log_enter_external_function(isolate),
5376 1);
5377 __ PopSafepointRegisters();
5378 }
5379
5380 // Native call returns to the DirectCEntry stub which redirects to the
5381 // return address pushed on stack (could have moved after GC).
5382 // DirectCEntry stub itself is generated early and never moves.
5383 DirectCEntryStub stub(isolate);
5384 stub.GenerateCall(masm, t9);
5385
5386 if (FLAG_log_timer_events) {
5387 FrameScope frame(masm, StackFrame::MANUAL);
5388 __ PushSafepointRegisters();
5389 __ PrepareCallCFunction(1, a0);
5390 __ li(a0, Operand(ExternalReference::isolate_address(isolate)));
5391 __ CallCFunction(ExternalReference::log_leave_external_function(isolate),
5392 1);
5393 __ PopSafepointRegisters();
5394 }
5395
5396 Label promote_scheduled_exception;
5397 Label delete_allocated_handles;
5398 Label leave_exit_frame;
5399 Label return_value_loaded;
5400
5401 // Load value from ReturnValue.
5402 __ lw(v0, return_value_operand);
5403 __ bind(&return_value_loaded);
5404
5405 // No more valid handles (the result handle was the last one). Restore
5406 // previous handle scope.
5407 __ sw(s0, MemOperand(s3, kNextOffset));
5408 if (__ emit_debug_code()) {
5409 __ lw(a1, MemOperand(s3, kLevelOffset));
5410 __ Check(eq, kUnexpectedLevelAfterReturnFromApiCall, a1, Operand(s2));
5411 }
5412 __ Subu(s2, s2, Operand(1));
5413 __ sw(s2, MemOperand(s3, kLevelOffset));
5414 __ lw(at, MemOperand(s3, kLimitOffset));
5415 __ Branch(&delete_allocated_handles, ne, s1, Operand(at));
5416
5417 // Leave the API exit frame.
5418 __ bind(&leave_exit_frame);
5419
5420 bool restore_context = context_restore_operand != NULL;
5421 if (restore_context) {
5422 __ lw(cp, *context_restore_operand);
5423 }
5424 if (stack_space_offset != kInvalidStackOffset) {
5425 // ExitFrame contains four MIPS argument slots after DirectCEntryStub call
5426 // so this must be accounted for.
5427 __ lw(s0, MemOperand(sp, stack_space_offset + kCArgsSlotsSize));
5428 } else {
5429 __ li(s0, Operand(stack_space));
5430 }
5431 __ LeaveExitFrame(false, s0, !restore_context, NO_EMIT_RETURN,
5432 stack_space_offset != kInvalidStackOffset);
5433
5434 // Check if the function scheduled an exception.
5435 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
5436 __ li(at, Operand(ExternalReference::scheduled_exception_address(isolate)));
5437 __ lw(t1, MemOperand(at));
5438 __ Branch(&promote_scheduled_exception, ne, t0, Operand(t1));
5439
5440 __ Ret();
5441
5442 // Re-throw by promoting a scheduled exception.
5443 __ bind(&promote_scheduled_exception);
5444 __ TailCallRuntime(Runtime::kPromoteScheduledException);
5445
5446 // HandleScope limit has changed. Delete allocated extensions.
5447 __ bind(&delete_allocated_handles);
5448 __ sw(s1, MemOperand(s3, kLimitOffset));
5449 __ mov(s0, v0);
5450 __ mov(a0, v0);
5451 __ PrepareCallCFunction(1, s1);
5452 __ li(a0, Operand(ExternalReference::isolate_address(isolate)));
5453 __ CallCFunction(ExternalReference::delete_handle_scope_extensions(isolate),
5454 1);
5455 __ mov(v0, s0);
5456 __ jmp(&leave_exit_frame);
5457}
5458
Ben Murdochda12d292016-06-02 14:46:10 +01005459void CallApiCallbackStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005460 // ----------- S t a t e -------------
5461 // -- a0 : callee
5462 // -- t0 : call_data
5463 // -- a2 : holder
5464 // -- a1 : api_function_address
5465 // -- cp : context
5466 // --
5467 // -- sp[0] : last argument
5468 // -- ...
5469 // -- sp[(argc - 1)* 4] : first argument
5470 // -- sp[argc * 4] : receiver
5471 // -----------------------------------
5472
5473 Register callee = a0;
5474 Register call_data = t0;
5475 Register holder = a2;
5476 Register api_function_address = a1;
5477 Register context = cp;
5478
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005479 typedef FunctionCallbackArguments FCA;
5480
5481 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
5482 STATIC_ASSERT(FCA::kCalleeIndex == 5);
5483 STATIC_ASSERT(FCA::kDataIndex == 4);
5484 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
5485 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
5486 STATIC_ASSERT(FCA::kIsolateIndex == 1);
5487 STATIC_ASSERT(FCA::kHolderIndex == 0);
Ben Murdochc5610432016-08-08 18:44:38 +01005488 STATIC_ASSERT(FCA::kNewTargetIndex == 7);
5489 STATIC_ASSERT(FCA::kArgsLength == 8);
5490
5491 // new target
5492 __ PushRoot(Heap::kUndefinedValueRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005493
5494 // Save context, callee and call data.
5495 __ Push(context, callee, call_data);
Ben Murdochda12d292016-06-02 14:46:10 +01005496 if (!is_lazy()) {
Ben Murdoch097c5b22016-05-18 11:27:45 +01005497 // Load context from callee.
5498 __ lw(context, FieldMemOperand(callee, JSFunction::kContextOffset));
5499 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005500
5501 Register scratch = call_data;
Ben Murdochda12d292016-06-02 14:46:10 +01005502 if (!call_data_undefined()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005503 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5504 }
5505 // Push return value and default return value.
5506 __ Push(scratch, scratch);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005507 __ li(scratch, Operand(ExternalReference::isolate_address(masm->isolate())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005508 // Push isolate and holder.
5509 __ Push(scratch, holder);
5510
5511 // Prepare arguments.
5512 __ mov(scratch, sp);
5513
5514 // Allocate the v8::Arguments structure in the arguments' space since
5515 // it's not controlled by GC.
Ben Murdochc5610432016-08-08 18:44:38 +01005516 const int kApiStackSpace = 3;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005517
5518 FrameScope frame_scope(masm, StackFrame::MANUAL);
5519 __ EnterExitFrame(false, kApiStackSpace);
5520
5521 DCHECK(!api_function_address.is(a0) && !scratch.is(a0));
5522 // a0 = FunctionCallbackInfo&
5523 // Arguments is after the return address.
5524 __ Addu(a0, sp, Operand(1 * kPointerSize));
5525 // FunctionCallbackInfo::implicit_args_
5526 __ sw(scratch, MemOperand(a0, 0 * kPointerSize));
Ben Murdochda12d292016-06-02 14:46:10 +01005527 // FunctionCallbackInfo::values_
5528 __ Addu(at, scratch, Operand((FCA::kArgsLength - 1 + argc()) * kPointerSize));
5529 __ sw(at, MemOperand(a0, 1 * kPointerSize));
5530 // FunctionCallbackInfo::length_ = argc
5531 __ li(at, Operand(argc()));
5532 __ sw(at, MemOperand(a0, 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005533
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005534 ExternalReference thunk_ref =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005535 ExternalReference::invoke_function_callback(masm->isolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005536
5537 AllowExternalCallThatCantCauseGC scope(masm);
5538 MemOperand context_restore_operand(
5539 fp, (2 + FCA::kContextSaveIndex) * kPointerSize);
5540 // Stores return the first js argument.
5541 int return_value_offset = 0;
Ben Murdochda12d292016-06-02 14:46:10 +01005542 if (is_store()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005543 return_value_offset = 2 + FCA::kArgsLength;
5544 } else {
5545 return_value_offset = 2 + FCA::kReturnValueOffset;
5546 }
5547 MemOperand return_value_operand(fp, return_value_offset * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005548 int stack_space = 0;
Ben Murdochc5610432016-08-08 18:44:38 +01005549 int32_t stack_space_offset = 3 * kPointerSize;
Ben Murdochda12d292016-06-02 14:46:10 +01005550 stack_space = argc() + FCA::kArgsLength + 1;
Ben Murdochc5610432016-08-08 18:44:38 +01005551 // TODO(adamk): Why are we clobbering this immediately?
Ben Murdochda12d292016-06-02 14:46:10 +01005552 stack_space_offset = kInvalidStackOffset;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005553 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref, stack_space,
5554 stack_space_offset, return_value_operand,
5555 &context_restore_operand);
5556}
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005557
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005558
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005559void CallApiGetterStub::Generate(MacroAssembler* masm) {
Ben Murdochc5610432016-08-08 18:44:38 +01005560 // Build v8::PropertyCallbackInfo::args_ array on the stack and push property
5561 // name below the exit frame to make GC aware of them.
5562 STATIC_ASSERT(PropertyCallbackArguments::kShouldThrowOnErrorIndex == 0);
5563 STATIC_ASSERT(PropertyCallbackArguments::kHolderIndex == 1);
5564 STATIC_ASSERT(PropertyCallbackArguments::kIsolateIndex == 2);
5565 STATIC_ASSERT(PropertyCallbackArguments::kReturnValueDefaultValueIndex == 3);
5566 STATIC_ASSERT(PropertyCallbackArguments::kReturnValueOffset == 4);
5567 STATIC_ASSERT(PropertyCallbackArguments::kDataIndex == 5);
5568 STATIC_ASSERT(PropertyCallbackArguments::kThisIndex == 6);
5569 STATIC_ASSERT(PropertyCallbackArguments::kArgsLength == 7);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005570
Ben Murdochc5610432016-08-08 18:44:38 +01005571 Register receiver = ApiGetterDescriptor::ReceiverRegister();
5572 Register holder = ApiGetterDescriptor::HolderRegister();
5573 Register callback = ApiGetterDescriptor::CallbackRegister();
5574 Register scratch = t0;
5575 DCHECK(!AreAliased(receiver, holder, callback, scratch));
5576
5577 Register api_function_address = a2;
5578
5579 // Here and below +1 is for name() pushed after the args_ array.
5580 typedef PropertyCallbackArguments PCA;
5581 __ Subu(sp, sp, (PCA::kArgsLength + 1) * kPointerSize);
5582 __ sw(receiver, MemOperand(sp, (PCA::kThisIndex + 1) * kPointerSize));
5583 __ lw(scratch, FieldMemOperand(callback, AccessorInfo::kDataOffset));
5584 __ sw(scratch, MemOperand(sp, (PCA::kDataIndex + 1) * kPointerSize));
5585 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
5586 __ sw(scratch, MemOperand(sp, (PCA::kReturnValueOffset + 1) * kPointerSize));
5587 __ sw(scratch, MemOperand(sp, (PCA::kReturnValueDefaultValueIndex + 1) *
5588 kPointerSize));
5589 __ li(scratch, Operand(ExternalReference::isolate_address(isolate())));
5590 __ sw(scratch, MemOperand(sp, (PCA::kIsolateIndex + 1) * kPointerSize));
5591 __ sw(holder, MemOperand(sp, (PCA::kHolderIndex + 1) * kPointerSize));
5592 // should_throw_on_error -> false
5593 DCHECK(Smi::FromInt(0) == nullptr);
5594 __ sw(zero_reg,
5595 MemOperand(sp, (PCA::kShouldThrowOnErrorIndex + 1) * kPointerSize));
5596 __ lw(scratch, FieldMemOperand(callback, AccessorInfo::kNameOffset));
5597 __ sw(scratch, MemOperand(sp, 0 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005598
Ben Murdoch097c5b22016-05-18 11:27:45 +01005599 // v8::PropertyCallbackInfo::args_ array and name handle.
5600 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5601
5602 // Load address of v8::PropertyAccessorInfo::args_ array and name handle.
5603 __ mov(a0, sp); // a0 = Handle<Name>
5604 __ Addu(a1, a0, Operand(1 * kPointerSize)); // a1 = v8::PCI::args_
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005605
5606 const int kApiStackSpace = 1;
5607 FrameScope frame_scope(masm, StackFrame::MANUAL);
5608 __ EnterExitFrame(false, kApiStackSpace);
5609
Ben Murdoch097c5b22016-05-18 11:27:45 +01005610 // Create v8::PropertyCallbackInfo object on the stack and initialize
5611 // it's args_ field.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005612 __ sw(a1, MemOperand(sp, 1 * kPointerSize));
Ben Murdoch097c5b22016-05-18 11:27:45 +01005613 __ Addu(a1, sp, Operand(1 * kPointerSize)); // a1 = v8::PropertyCallbackInfo&
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005614
5615 ExternalReference thunk_ref =
5616 ExternalReference::invoke_accessor_getter_callback(isolate());
Ben Murdoch097c5b22016-05-18 11:27:45 +01005617
Ben Murdochc5610432016-08-08 18:44:38 +01005618 __ lw(scratch, FieldMemOperand(callback, AccessorInfo::kJsGetterOffset));
5619 __ lw(api_function_address,
5620 FieldMemOperand(scratch, Foreign::kForeignAddressOffset));
5621
Ben Murdoch097c5b22016-05-18 11:27:45 +01005622 // +3 is to skip prolog, return address and name handle.
5623 MemOperand return_value_operand(
5624 fp, (PropertyCallbackArguments::kReturnValueOffset + 3) * kPointerSize);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005625 CallApiFunctionAndReturn(masm, api_function_address, thunk_ref,
5626 kStackUnwindSpace, kInvalidStackOffset,
Ben Murdoch097c5b22016-05-18 11:27:45 +01005627 return_value_operand, NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005628}
5629
Steve Block44f0eee2011-05-26 01:26:41 +01005630#undef __
5631
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005632} // namespace internal
5633} // namespace v8
Steve Block44f0eee2011-05-26 01:26:41 +01005634
5635#endif // V8_TARGET_ARCH_MIPS