blob: 97eed7470725f5ab2c1cb194ab78da0e25dfabaa [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#include "src/v8.h"
Steve Block44f0eee2011-05-26 01:26:41 +01006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#if V8_TARGET_ARCH_MIPS
Steve Block44f0eee2011-05-26 01:26:41 +01008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/base/bits.h"
10#include "src/bootstrapper.h"
11#include "src/code-stubs.h"
12#include "src/codegen.h"
13#include "src/ic/handler-compiler.h"
14#include "src/ic/ic.h"
15#include "src/isolate.h"
16#include "src/jsregexp.h"
17#include "src/regexp-macro-assembler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040018#include "src/runtime/runtime.h"
Steve Block44f0eee2011-05-26 01:26:41 +010019
20namespace v8 {
21namespace internal {
22
23
Ben Murdochb8a8cc12014-11-26 15:28:44 +000024static void InitializeArrayConstructorDescriptor(
25 Isolate* isolate, CodeStubDescriptor* descriptor,
26 int constant_stack_parameter_count) {
27 Address deopt_handler = Runtime::FunctionForId(
28 Runtime::kArrayConstructor)->entry;
29
30 if (constant_stack_parameter_count == 0) {
31 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
32 JS_FUNCTION_STUB_MODE);
33 } else {
34 descriptor->Initialize(a0, deopt_handler, constant_stack_parameter_count,
35 JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
36 }
37}
38
39
40static void InitializeInternalArrayConstructorDescriptor(
41 Isolate* isolate, CodeStubDescriptor* descriptor,
42 int constant_stack_parameter_count) {
43 Address deopt_handler = Runtime::FunctionForId(
44 Runtime::kInternalArrayConstructor)->entry;
45
46 if (constant_stack_parameter_count == 0) {
47 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
48 JS_FUNCTION_STUB_MODE);
49 } else {
50 descriptor->Initialize(a0, deopt_handler, constant_stack_parameter_count,
51 JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
52 }
53}
54
55
56void ArrayNoArgumentConstructorStub::InitializeDescriptor(
57 CodeStubDescriptor* descriptor) {
58 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
59}
60
61
62void ArraySingleArgumentConstructorStub::InitializeDescriptor(
63 CodeStubDescriptor* descriptor) {
64 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
65}
66
67
68void ArrayNArgumentsConstructorStub::InitializeDescriptor(
69 CodeStubDescriptor* descriptor) {
70 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
71}
72
73
74void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
75 CodeStubDescriptor* descriptor) {
76 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
77}
78
79
80void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
81 CodeStubDescriptor* descriptor) {
82 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
83}
84
85
86void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
87 CodeStubDescriptor* descriptor) {
88 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
89}
90
91
Steve Block44f0eee2011-05-26 01:26:41 +010092#define __ ACCESS_MASM(masm)
93
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094
Ben Murdoch257744e2011-11-30 15:57:28 +000095static void EmitIdenticalObjectComparison(MacroAssembler* masm,
96 Label* slow,
Ben Murdochb8a8cc12014-11-26 15:28:44 +000097 Condition cc);
Ben Murdoch257744e2011-11-30 15:57:28 +000098static void EmitSmiNonsmiComparison(MacroAssembler* masm,
99 Register lhs,
100 Register rhs,
101 Label* rhs_not_nan,
102 Label* slow,
103 bool strict);
Ben Murdoch257744e2011-11-30 15:57:28 +0000104static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
105 Register lhs,
106 Register rhs);
107
108
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
110 ExternalReference miss) {
111 // Update the static counter each time a new code stub is generated.
112 isolate()->counters()->code_stubs()->Increment();
Ben Murdoch257744e2011-11-30 15:57:28 +0000113
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000114 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
115 int param_count = descriptor.GetEnvironmentParameterCount();
116 {
117 // Call the runtime system in a fresh internal frame.
118 FrameScope scope(masm, StackFrame::INTERNAL);
119 DCHECK(param_count == 0 ||
120 a0.is(descriptor.GetEnvironmentParameterRegister(param_count - 1)));
121 // Push arguments, adjust sp.
122 __ Subu(sp, sp, Operand(param_count * kPointerSize));
123 for (int i = 0; i < param_count; ++i) {
124 // Store argument to stack.
125 __ sw(descriptor.GetEnvironmentParameterRegister(i),
126 MemOperand(sp, (param_count - 1 - i) * kPointerSize));
127 }
128 __ CallExternalReference(miss, param_count);
129 }
Steve Block44f0eee2011-05-26 01:26:41 +0100130
Ben Murdoch257744e2011-11-30 15:57:28 +0000131 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100132}
133
134
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000135void DoubleToIStub::Generate(MacroAssembler* masm) {
136 Label out_of_range, only_low, negate, done;
137 Register input_reg = source();
138 Register result_reg = destination();
Ben Murdoch257744e2011-11-30 15:57:28 +0000139
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140 int double_offset = offset();
141 // Account for saved regs if input is sp.
142 if (input_reg.is(sp)) double_offset += 3 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +0000143
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000144 Register scratch =
145 GetRegisterThatIsNotOneOf(input_reg, result_reg);
146 Register scratch2 =
147 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch);
148 Register scratch3 =
149 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch, scratch2);
150 DoubleRegister double_scratch = kLithiumScratchDouble;
Ben Murdoch257744e2011-11-30 15:57:28 +0000151
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 __ Push(scratch, scratch2, scratch3);
Ben Murdoch257744e2011-11-30 15:57:28 +0000153
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000154 if (!skip_fastpath()) {
155 // Load double input.
156 __ ldc1(double_scratch, MemOperand(input_reg, double_offset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000157
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000158 // Clear cumulative exception flags and save the FCSR.
159 __ cfc1(scratch2, FCSR);
160 __ ctc1(zero_reg, FCSR);
Ben Murdoch257744e2011-11-30 15:57:28 +0000161
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 // Try a conversion to a signed integer.
163 __ Trunc_w_d(double_scratch, double_scratch);
164 // Move the converted value into the result register.
165 __ mfc1(scratch3, double_scratch);
Ben Murdoch257744e2011-11-30 15:57:28 +0000166
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000167 // Retrieve and restore the FCSR.
168 __ cfc1(scratch, FCSR);
169 __ ctc1(scratch2, FCSR);
Steve Block44f0eee2011-05-26 01:26:41 +0100170
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000171 // Check for overflow and NaNs.
172 __ And(
173 scratch, scratch,
174 kFCSROverflowFlagMask | kFCSRUnderflowFlagMask
175 | kFCSRInvalidOpFlagMask);
176 // If we had no exceptions then set result_reg and we are done.
177 Label error;
178 __ Branch(&error, ne, scratch, Operand(zero_reg));
179 __ Move(result_reg, scratch3);
Ben Murdoch257744e2011-11-30 15:57:28 +0000180 __ Branch(&done);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000181 __ bind(&error);
Ben Murdoch257744e2011-11-30 15:57:28 +0000182 }
183
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 // Load the double value and perform a manual truncation.
185 Register input_high = scratch2;
186 Register input_low = scratch3;
Ben Murdoch257744e2011-11-30 15:57:28 +0000187
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000188 __ lw(input_low,
189 MemOperand(input_reg, double_offset + Register::kMantissaOffset));
190 __ lw(input_high,
191 MemOperand(input_reg, double_offset + Register::kExponentOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000192
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000193 Label normal_exponent, restore_sign;
194 // Extract the biased exponent in result.
195 __ Ext(result_reg,
196 input_high,
Ben Murdoch257744e2011-11-30 15:57:28 +0000197 HeapNumber::kExponentShift,
198 HeapNumber::kExponentBits);
199
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000200 // Check for Infinity and NaNs, which should return 0.
201 __ Subu(scratch, result_reg, HeapNumber::kExponentMask);
202 __ Movz(result_reg, zero_reg, scratch);
203 __ Branch(&done, eq, scratch, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000204
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000205 // Express exponent as delta to (number of mantissa bits + 31).
206 __ Subu(result_reg,
207 result_reg,
208 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31));
Ben Murdoch257744e2011-11-30 15:57:28 +0000209
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 // If the delta is strictly positive, all bits would be shifted away,
211 // which means that we can return 0.
212 __ Branch(&normal_exponent, le, result_reg, Operand(zero_reg));
213 __ mov(result_reg, zero_reg);
214 __ Branch(&done);
Ben Murdoch257744e2011-11-30 15:57:28 +0000215
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000216 __ bind(&normal_exponent);
217 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
218 // Calculate shift.
219 __ Addu(scratch, result_reg, Operand(kShiftBase + HeapNumber::kMantissaBits));
Ben Murdoch257744e2011-11-30 15:57:28 +0000220
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000221 // Save the sign.
222 Register sign = result_reg;
223 result_reg = no_reg;
224 __ And(sign, input_high, Operand(HeapNumber::kSignMask));
Ben Murdoch257744e2011-11-30 15:57:28 +0000225
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000226 // On ARM shifts > 31 bits are valid and will result in zero. On MIPS we need
227 // to check for this specific case.
228 Label high_shift_needed, high_shift_done;
229 __ Branch(&high_shift_needed, lt, scratch, Operand(32));
230 __ mov(input_high, zero_reg);
231 __ Branch(&high_shift_done);
232 __ bind(&high_shift_needed);
233
234 // Set the implicit 1 before the mantissa part in input_high.
235 __ Or(input_high,
236 input_high,
237 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
238 // Shift the mantissa bits to the correct position.
239 // We don't need to clear non-mantissa bits as they will be shifted away.
240 // If they weren't, it would mean that the answer is in the 32bit range.
241 __ sllv(input_high, input_high, scratch);
242
243 __ bind(&high_shift_done);
244
245 // Replace the shifted bits with bits from the lower mantissa word.
246 Label pos_shift, shift_done;
Ben Murdoch257744e2011-11-30 15:57:28 +0000247 __ li(at, 32);
248 __ subu(scratch, at, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000249 __ Branch(&pos_shift, ge, scratch, Operand(zero_reg));
250
251 // Negate scratch.
252 __ Subu(scratch, zero_reg, scratch);
253 __ sllv(input_low, input_low, scratch);
254 __ Branch(&shift_done);
255
256 __ bind(&pos_shift);
257 __ srlv(input_low, input_low, scratch);
258
259 __ bind(&shift_done);
260 __ Or(input_high, input_high, Operand(input_low));
261 // Restore sign if necessary.
262 __ mov(scratch, sign);
263 result_reg = sign;
264 sign = no_reg;
265 __ Subu(result_reg, zero_reg, input_high);
266 __ Movz(result_reg, input_high, scratch);
267
268 __ bind(&done);
269
270 __ Pop(scratch, scratch2, scratch3);
271 __ Ret();
Ben Murdoch257744e2011-11-30 15:57:28 +0000272}
273
274
Ben Murdoch257744e2011-11-30 15:57:28 +0000275// Handle the case where the lhs and rhs are the same object.
276// Equality is almost reflexive (everything but NaN), so this is a test
277// for "identity and not NaN".
278static void EmitIdenticalObjectComparison(MacroAssembler* masm,
279 Label* slow,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000280 Condition cc) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000281 Label not_identical;
282 Label heap_number, return_equal;
283 Register exp_mask_reg = t5;
284
285 __ Branch(&not_identical, ne, a0, Operand(a1));
286
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000287 __ li(exp_mask_reg, Operand(HeapNumber::kExponentMask));
Ben Murdoch257744e2011-11-30 15:57:28 +0000288
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000289 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
290 // so we do the second best thing - test it ourselves.
291 // They are both equal and they are not both Smis so both of them are not
292 // Smis. If it's not a heap number, then return equal.
293 if (cc == less || cc == greater) {
294 __ GetObjectType(a0, t4, t4);
295 __ Branch(slow, greater, t4, Operand(FIRST_SPEC_OBJECT_TYPE));
296 } else {
297 __ GetObjectType(a0, t4, t4);
298 __ Branch(&heap_number, eq, t4, Operand(HEAP_NUMBER_TYPE));
299 // Comparing JS objects with <=, >= is complicated.
300 if (cc != eq) {
301 __ Branch(slow, greater, t4, Operand(FIRST_SPEC_OBJECT_TYPE));
302 // Normally here we fall through to return_equal, but undefined is
303 // special: (undefined == undefined) == true, but
304 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
305 if (cc == less_equal || cc == greater_equal) {
306 __ Branch(&return_equal, ne, t4, Operand(ODDBALL_TYPE));
307 __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
308 __ Branch(&return_equal, ne, a0, Operand(t2));
309 DCHECK(is_int16(GREATER) && is_int16(LESS));
310 __ Ret(USE_DELAY_SLOT);
311 if (cc == le) {
312 // undefined <= undefined should fail.
313 __ li(v0, Operand(GREATER));
314 } else {
315 // undefined >= undefined should fail.
316 __ li(v0, Operand(LESS));
Ben Murdoch257744e2011-11-30 15:57:28 +0000317 }
318 }
319 }
320 }
321
322 __ bind(&return_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000323 DCHECK(is_int16(GREATER) && is_int16(LESS));
324 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000325 if (cc == less) {
326 __ li(v0, Operand(GREATER)); // Things aren't less than themselves.
327 } else if (cc == greater) {
328 __ li(v0, Operand(LESS)); // Things aren't greater than themselves.
329 } else {
330 __ mov(v0, zero_reg); // Things are <=, >=, ==, === themselves.
331 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000332
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000333 // For less and greater we don't have to check for NaN since the result of
334 // x < x is false regardless. For the others here is some code to check
335 // for NaN.
336 if (cc != lt && cc != gt) {
337 __ bind(&heap_number);
338 // It is a heap number, so return non-equal if it's NaN and equal if it's
339 // not NaN.
Ben Murdoch257744e2011-11-30 15:57:28 +0000340
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 // The representation of NaN values has all exponent bits (52..62) set,
342 // and not all mantissa bits (0..51) clear.
343 // Read top bits of double representation (second word of value).
344 __ lw(t2, FieldMemOperand(a0, HeapNumber::kExponentOffset));
345 // Test that exponent bits are all set.
346 __ And(t3, t2, Operand(exp_mask_reg));
347 // If all bits not set (ne cond), then not a NaN, objects are equal.
348 __ Branch(&return_equal, ne, t3, Operand(exp_mask_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000349
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000350 // Shift out flag and all exponent bits, retaining only mantissa.
351 __ sll(t2, t2, HeapNumber::kNonMantissaBitsInTopWord);
352 // Or with all low-bits of mantissa.
353 __ lw(t3, FieldMemOperand(a0, HeapNumber::kMantissaOffset));
354 __ Or(v0, t3, Operand(t2));
355 // For equal we already have the right value in v0: Return zero (equal)
356 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
357 // not (it's a NaN). For <= and >= we need to load v0 with the failing
358 // value if it's a NaN.
359 if (cc != eq) {
360 // All-zero means Infinity means equal.
361 __ Ret(eq, v0, Operand(zero_reg));
362 DCHECK(is_int16(GREATER) && is_int16(LESS));
363 __ Ret(USE_DELAY_SLOT);
364 if (cc == le) {
365 __ li(v0, Operand(GREATER)); // NaN <= NaN should fail.
366 } else {
367 __ li(v0, Operand(LESS)); // NaN >= NaN should fail.
Ben Murdoch257744e2011-11-30 15:57:28 +0000368 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000369 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000370 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000371 // No fall through here.
Ben Murdoch257744e2011-11-30 15:57:28 +0000372
373 __ bind(&not_identical);
374}
375
376
377static void EmitSmiNonsmiComparison(MacroAssembler* masm,
378 Register lhs,
379 Register rhs,
380 Label* both_loaded_as_doubles,
381 Label* slow,
382 bool strict) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000383 DCHECK((lhs.is(a0) && rhs.is(a1)) ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000384 (lhs.is(a1) && rhs.is(a0)));
385
386 Label lhs_is_smi;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100387 __ JumpIfSmi(lhs, &lhs_is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +0000388 // Rhs is a Smi.
389 // Check whether the non-smi is a heap number.
390 __ GetObjectType(lhs, t4, t4);
391 if (strict) {
392 // If lhs was not a number and rhs was a Smi then strict equality cannot
393 // succeed. Return non-equal (lhs is already not zero).
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100394 __ Ret(USE_DELAY_SLOT, ne, t4, Operand(HEAP_NUMBER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000395 __ mov(v0, lhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000396 } else {
397 // Smi compared non-strictly with a non-Smi non-heap-number. Call
398 // the runtime.
399 __ Branch(slow, ne, t4, Operand(HEAP_NUMBER_TYPE));
400 }
401
402 // Rhs is a smi, lhs is a number.
403 // Convert smi rhs to double.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000404 __ sra(at, rhs, kSmiTagSize);
405 __ mtc1(at, f14);
406 __ cvt_d_w(f14, f14);
407 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000408
409 // We now have both loaded as doubles.
410 __ jmp(both_loaded_as_doubles);
411
412 __ bind(&lhs_is_smi);
413 // Lhs is a Smi. Check whether the non-smi is a heap number.
414 __ GetObjectType(rhs, t4, t4);
415 if (strict) {
416 // If lhs was not a number and rhs was a Smi then strict equality cannot
417 // succeed. Return non-equal.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100418 __ Ret(USE_DELAY_SLOT, ne, t4, Operand(HEAP_NUMBER_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000419 __ li(v0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +0000420 } else {
421 // Smi compared non-strictly with a non-Smi non-heap-number. Call
422 // the runtime.
423 __ Branch(slow, ne, t4, Operand(HEAP_NUMBER_TYPE));
424 }
425
426 // Lhs is a smi, rhs is a number.
427 // Convert smi lhs to double.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000428 __ sra(at, lhs, kSmiTagSize);
429 __ mtc1(at, f12);
430 __ cvt_d_w(f12, f12);
431 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +0000432 // Fall through to both_loaded_as_doubles.
Steve Block44f0eee2011-05-26 01:26:41 +0100433}
434
435
Ben Murdoch257744e2011-11-30 15:57:28 +0000436static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
437 Register lhs,
438 Register rhs) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000439 // If either operand is a JS object or an oddball value, then they are
Ben Murdoch257744e2011-11-30 15:57:28 +0000440 // not equal since their pointers are different.
441 // There is no test for undetectability in strict equality.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100442 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +0000443 Label first_non_object;
444 // Get the type of the first operand into a2 and compare it with
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000445 // FIRST_SPEC_OBJECT_TYPE.
Ben Murdoch257744e2011-11-30 15:57:28 +0000446 __ GetObjectType(lhs, a2, a2);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000447 __ Branch(&first_non_object, less, a2, Operand(FIRST_SPEC_OBJECT_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000448
449 // Return non-zero.
450 Label return_not_equal;
451 __ bind(&return_not_equal);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100452 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000453 __ li(v0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +0000454
455 __ bind(&first_non_object);
456 // Check for oddballs: true, false, null, undefined.
457 __ Branch(&return_not_equal, eq, a2, Operand(ODDBALL_TYPE));
458
459 __ GetObjectType(rhs, a3, a3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000460 __ Branch(&return_not_equal, greater, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000461
462 // Check for oddballs: true, false, null, undefined.
463 __ Branch(&return_not_equal, eq, a3, Operand(ODDBALL_TYPE));
464
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000465 // Now that we have the types we might as well check for
466 // internalized-internalized.
467 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
468 __ Or(a2, a2, Operand(a3));
469 __ And(at, a2, Operand(kIsNotStringMask | kIsNotInternalizedMask));
470 __ Branch(&return_not_equal, eq, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000471}
472
473
474static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
475 Register lhs,
476 Register rhs,
477 Label* both_loaded_as_doubles,
478 Label* not_heap_numbers,
479 Label* slow) {
480 __ GetObjectType(lhs, a3, a2);
481 __ Branch(not_heap_numbers, ne, a2, Operand(HEAP_NUMBER_TYPE));
482 __ lw(a2, FieldMemOperand(rhs, HeapObject::kMapOffset));
483 // If first was a heap number & second wasn't, go to slow case.
484 __ Branch(slow, ne, a3, Operand(a2));
485
486 // Both are heap numbers. Load them up then jump to the code we have
487 // for that.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000488 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
489 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
490
Ben Murdoch257744e2011-11-30 15:57:28 +0000491 __ jmp(both_loaded_as_doubles);
492}
493
494
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000495// Fast negative check for internalized-to-internalized equality.
496static void EmitCheckForInternalizedStringsOrObjects(MacroAssembler* masm,
497 Register lhs,
498 Register rhs,
499 Label* possible_strings,
500 Label* not_both_strings) {
501 DCHECK((lhs.is(a0) && rhs.is(a1)) ||
Ben Murdoch257744e2011-11-30 15:57:28 +0000502 (lhs.is(a1) && rhs.is(a0)));
503
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000504 // a2 is object type of rhs.
Ben Murdoch257744e2011-11-30 15:57:28 +0000505 Label object_test;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000506 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000507 __ And(at, a2, Operand(kIsNotStringMask));
508 __ Branch(&object_test, ne, at, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 __ And(at, a2, Operand(kIsNotInternalizedMask));
510 __ Branch(possible_strings, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000511 __ GetObjectType(rhs, a3, a3);
512 __ Branch(not_both_strings, ge, a3, Operand(FIRST_NONSTRING_TYPE));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000513 __ And(at, a3, Operand(kIsNotInternalizedMask));
514 __ Branch(possible_strings, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +0000515
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000516 // Both are internalized strings. We already checked they weren't the same
517 // pointer so they are not equal.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100518 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +0000519 __ li(v0, Operand(1)); // Non-zero indicates not equal.
Ben Murdoch257744e2011-11-30 15:57:28 +0000520
521 __ bind(&object_test);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000522 __ Branch(not_both_strings, lt, a2, Operand(FIRST_SPEC_OBJECT_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000523 __ GetObjectType(rhs, a2, a3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000524 __ Branch(not_both_strings, lt, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
Ben Murdoch257744e2011-11-30 15:57:28 +0000525
526 // If both objects are undetectable, they are equal. Otherwise, they
527 // are not equal, since they are different objects and an object is not
528 // equal to undefined.
529 __ lw(a3, FieldMemOperand(lhs, HeapObject::kMapOffset));
530 __ lbu(a2, FieldMemOperand(a2, Map::kBitFieldOffset));
531 __ lbu(a3, FieldMemOperand(a3, Map::kBitFieldOffset));
532 __ and_(a0, a2, a3);
533 __ And(a0, a0, Operand(1 << Map::kIsUndetectable));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100534 __ Ret(USE_DELAY_SLOT);
535 __ xori(v0, a0, 1 << Map::kIsUndetectable);
Steve Block44f0eee2011-05-26 01:26:41 +0100536}
537
538
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539static void CompareICStub_CheckInputType(MacroAssembler* masm, Register input,
540 Register scratch,
541 CompareICState::State expected,
542 Label* fail) {
543 Label ok;
544 if (expected == CompareICState::SMI) {
545 __ JumpIfNotSmi(input, fail);
546 } else if (expected == CompareICState::NUMBER) {
547 __ JumpIfSmi(input, &ok);
548 __ CheckMap(input, scratch, Heap::kHeapNumberMapRootIndex, fail,
549 DONT_DO_SMI_CHECK);
Ben Murdoch257744e2011-11-30 15:57:28 +0000550 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000551 // We could be strict about internalized/string here, but as long as
552 // hydrogen doesn't care, the stub doesn't have to care either.
553 __ bind(&ok);
Steve Block44f0eee2011-05-26 01:26:41 +0100554}
555
556
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000557// On entry a1 and a2 are the values to be compared.
558// On exit a0 is 0, positive or negative to indicate the result of
559// the comparison.
560void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
561 Register lhs = a1;
562 Register rhs = a0;
563 Condition cc = GetCondition();
Ben Murdoch257744e2011-11-30 15:57:28 +0000564
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000565 Label miss;
566 CompareICStub_CheckInputType(masm, lhs, a2, left(), &miss);
567 CompareICStub_CheckInputType(masm, rhs, a3, right(), &miss);
Ben Murdoch257744e2011-11-30 15:57:28 +0000568
Ben Murdoch257744e2011-11-30 15:57:28 +0000569 Label slow; // Call builtin.
570 Label not_smis, both_loaded_as_doubles;
571
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000572 Label not_two_smis, smi_done;
573 __ Or(a2, a1, a0);
574 __ JumpIfNotSmi(a2, &not_two_smis);
575 __ sra(a1, a1, 1);
576 __ sra(a0, a0, 1);
577 __ Ret(USE_DELAY_SLOT);
578 __ subu(v0, a1, a0);
579 __ bind(&not_two_smis);
Ben Murdoch257744e2011-11-30 15:57:28 +0000580
581 // NOTICE! This code is only reached after a smi-fast-case check, so
582 // it is certain that at least one operand isn't a smi.
583
584 // Handle the case where the objects are identical. Either returns the answer
585 // or goes to slow. Only falls through if the objects were not identical.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000586 EmitIdenticalObjectComparison(masm, &slow, cc);
Ben Murdoch257744e2011-11-30 15:57:28 +0000587
588 // If either is a Smi (we know that not both are), then they can only
589 // be strictly equal if the other is a HeapNumber.
590 STATIC_ASSERT(kSmiTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000591 DCHECK_EQ(0, Smi::FromInt(0));
592 __ And(t2, lhs, Operand(rhs));
Ben Murdoch257744e2011-11-30 15:57:28 +0000593 __ JumpIfNotSmi(t2, &not_smis, t0);
594 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
595 // 1) Return the answer.
596 // 2) Go to slow.
597 // 3) Fall through to both_loaded_as_doubles.
598 // 4) Jump to rhs_not_nan.
599 // In cases 3 and 4 we have found out we were dealing with a number-number
600 // comparison and the numbers have been loaded into f12 and f14 as doubles,
601 // or in GP registers (a0, a1, a2, a3) depending on the presence of the FPU.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000602 EmitSmiNonsmiComparison(masm, lhs, rhs,
603 &both_loaded_as_doubles, &slow, strict());
Ben Murdoch257744e2011-11-30 15:57:28 +0000604
605 __ bind(&both_loaded_as_doubles);
606 // f12, f14 are the double representations of the left hand side
607 // and the right hand side if we have FPU. Otherwise a2, a3 represent
608 // left hand side and a0, a1 represent right hand side.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000609 Label nan;
610 __ li(t0, Operand(LESS));
611 __ li(t1, Operand(GREATER));
612 __ li(t2, Operand(EQUAL));
Ben Murdoch257744e2011-11-30 15:57:28 +0000613
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000614 // Check if either rhs or lhs is NaN.
615 __ BranchF(NULL, &nan, eq, f12, f14);
Ben Murdoch257744e2011-11-30 15:57:28 +0000616
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000617 // Check if LESS condition is satisfied. If true, move conditionally
618 // result to v0.
619 if (!IsMipsArchVariant(kMips32r6)) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000620 __ c(OLT, D, f12, f14);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100621 __ Movt(v0, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000622 // Use previous check to store conditionally to v0 oposite condition
623 // (GREATER). If rhs is equal to lhs, this will be corrected in next
624 // check.
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100625 __ Movf(v0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000626 // Check if EQUAL condition is satisfied. If true, move conditionally
627 // result to v0.
628 __ c(EQ, D, f12, f14);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100629 __ Movt(v0, t2);
Ben Murdoch257744e2011-11-30 15:57:28 +0000630 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000631 Label skip;
632 __ BranchF(USE_DELAY_SLOT, &skip, NULL, lt, f12, f14);
633 __ mov(v0, t0); // Return LESS as result.
Ben Murdoch257744e2011-11-30 15:57:28 +0000634
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000635 __ BranchF(USE_DELAY_SLOT, &skip, NULL, eq, f12, f14);
636 __ mov(v0, t2); // Return EQUAL as result.
637
638 __ mov(v0, t1); // Return GREATER as result.
639 __ bind(&skip);
Ben Murdoch257744e2011-11-30 15:57:28 +0000640 }
641
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000642 __ Ret();
643
644 __ bind(&nan);
645 // NaN comparisons always fail.
646 // Load whatever we need in v0 to make the comparison fail.
647 DCHECK(is_int16(GREATER) && is_int16(LESS));
648 __ Ret(USE_DELAY_SLOT);
649 if (cc == lt || cc == le) {
650 __ li(v0, Operand(GREATER));
651 } else {
652 __ li(v0, Operand(LESS));
653 }
654
655
Ben Murdoch257744e2011-11-30 15:57:28 +0000656 __ bind(&not_smis);
657 // At this point we know we are dealing with two different objects,
658 // and neither of them is a Smi. The objects are in lhs_ and rhs_.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000659 if (strict()) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000660 // This returns non-equal for some object types, or falls through if it
661 // was not lucky.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000662 EmitStrictTwoHeapObjectCompare(masm, lhs, rhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000663 }
664
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000665 Label check_for_internalized_strings;
Ben Murdoch257744e2011-11-30 15:57:28 +0000666 Label flat_string_check;
667 // Check for heap-number-heap-number comparison. Can jump to slow case,
668 // or load both doubles and jump to the code that handles
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000669 // that case. If the inputs are not doubles then jumps to
670 // check_for_internalized_strings.
Ben Murdoch257744e2011-11-30 15:57:28 +0000671 // In this case a2 will contain the type of lhs_.
672 EmitCheckForTwoHeapNumbers(masm,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000673 lhs,
674 rhs,
Ben Murdoch257744e2011-11-30 15:57:28 +0000675 &both_loaded_as_doubles,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000676 &check_for_internalized_strings,
Ben Murdoch257744e2011-11-30 15:57:28 +0000677 &flat_string_check);
678
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000679 __ bind(&check_for_internalized_strings);
680 if (cc == eq && !strict()) {
681 // Returns an answer for two internalized strings or two
682 // detectable objects.
Ben Murdoch257744e2011-11-30 15:57:28 +0000683 // Otherwise jumps to string case or not both strings case.
684 // Assumes that a2 is the type of lhs_ on entry.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000685 EmitCheckForInternalizedStringsOrObjects(
686 masm, lhs, rhs, &flat_string_check, &slow);
Ben Murdoch257744e2011-11-30 15:57:28 +0000687 }
688
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000689 // Check for both being sequential one-byte strings,
690 // and inline if that is the case.
Ben Murdoch257744e2011-11-30 15:57:28 +0000691 __ bind(&flat_string_check);
692
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000693 __ JumpIfNonSmisNotBothSequentialOneByteStrings(lhs, rhs, a2, a3, &slow);
Ben Murdoch257744e2011-11-30 15:57:28 +0000694
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000695 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1, a2,
696 a3);
697 if (cc == eq) {
698 StringHelper::GenerateFlatOneByteStringEquals(masm, lhs, rhs, a2, a3, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +0000699 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000700 StringHelper::GenerateCompareFlatOneByteStrings(masm, lhs, rhs, a2, a3, t0,
701 t1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000702 }
703 // Never falls through to here.
704
705 __ bind(&slow);
706 // Prepare for call to builtin. Push object pointers, a0 (lhs) first,
707 // a1 (rhs) second.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000708 __ Push(lhs, rhs);
Ben Murdoch257744e2011-11-30 15:57:28 +0000709 // Figure out which native to call and setup the arguments.
710 Builtins::JavaScript native;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000711 if (cc == eq) {
712 native = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
Ben Murdoch257744e2011-11-30 15:57:28 +0000713 } else {
714 native = Builtins::COMPARE;
715 int ncr; // NaN compare result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000716 if (cc == lt || cc == le) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000717 ncr = GREATER;
718 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000719 DCHECK(cc == gt || cc == ge); // Remaining cases.
Ben Murdoch257744e2011-11-30 15:57:28 +0000720 ncr = LESS;
721 }
722 __ li(a0, Operand(Smi::FromInt(ncr)));
723 __ push(a0);
724 }
725
726 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
727 // tagged as a small integer.
728 __ InvokeBuiltin(native, JUMP_FUNCTION);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000729
730 __ bind(&miss);
731 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +0100732}
733
734
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000735void StoreRegistersStateStub::Generate(MacroAssembler* masm) {
736 __ mov(t9, ra);
737 __ pop(ra);
738 __ PushSafepointRegisters();
739 __ Jump(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100740}
Ben Murdoch257744e2011-11-30 15:57:28 +0000741
Ben Murdoch257744e2011-11-30 15:57:28 +0000742
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000743void RestoreRegistersStateStub::Generate(MacroAssembler* masm) {
744 __ mov(t9, ra);
745 __ pop(ra);
746 __ PopSafepointRegisters();
747 __ Jump(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100748}
749
750
751void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
752 // We don't allow a GC during a store buffer overflow so there is no need to
753 // store the registers in any particular way, but we do have to store and
754 // restore them.
755 __ MultiPush(kJSCallerSaved | ra.bit());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000756 if (save_doubles()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100757 __ MultiPushFPU(kCallerSavedFPU);
758 }
759 const int argument_count = 1;
760 const int fp_argument_count = 0;
761 const Register scratch = a1;
762
763 AllowExternalCallThatCantCauseGC scope(masm);
764 __ PrepareCallCFunction(argument_count, fp_argument_count, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000765 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100766 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000767 ExternalReference::store_buffer_overflow_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100768 argument_count);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000769 if (save_doubles()) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100770 __ MultiPopFPU(kCallerSavedFPU);
771 }
772
773 __ MultiPop(kJSCallerSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +0000774 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +0100775}
776
777
Ben Murdoch257744e2011-11-30 15:57:28 +0000778void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100779 const Register base = a1;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000780 const Register exponent = MathPowTaggedDescriptor::exponent();
781 DCHECK(exponent.is(a2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100782 const Register heapnumbermap = t1;
783 const Register heapnumber = v0;
784 const DoubleRegister double_base = f2;
785 const DoubleRegister double_exponent = f4;
786 const DoubleRegister double_result = f0;
787 const DoubleRegister double_scratch = f6;
788 const FPURegister single_scratch = f8;
789 const Register scratch = t5;
790 const Register scratch2 = t3;
Ben Murdoch257744e2011-11-30 15:57:28 +0000791
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100792 Label call_runtime, done, int_exponent;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000793 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100794 Label base_is_smi, unpack_exponent;
795 // The exponent and base are supplied as arguments on the stack.
796 // This can only happen if the stub is called from non-optimized code.
797 // Load input parameters from stack to double registers.
Ben Murdoch257744e2011-11-30 15:57:28 +0000798 __ lw(base, MemOperand(sp, 1 * kPointerSize));
799 __ lw(exponent, MemOperand(sp, 0 * kPointerSize));
800
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100801 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +0000802
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100803 __ UntagAndJumpIfSmi(scratch, base, &base_is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +0000804 __ lw(scratch, FieldMemOperand(base, JSObject::kMapOffset));
805 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100806
Ben Murdochc7cc0282012-03-05 14:35:55 +0000807 __ ldc1(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100808 __ jmp(&unpack_exponent);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000809
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100810 __ bind(&base_is_smi);
811 __ mtc1(scratch, single_scratch);
812 __ cvt_d_w(double_base, single_scratch);
813 __ bind(&unpack_exponent);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000814
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100815 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
Ben Murdoch85b71792012-04-11 18:30:58 +0100816
Ben Murdoch85b71792012-04-11 18:30:58 +0100817 __ lw(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
818 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
Ben Murdoch85b71792012-04-11 18:30:58 +0100819 __ ldc1(double_exponent,
820 FieldMemOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000821 } else if (exponent_type() == TAGGED) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100822 // Base is already in double_base.
823 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
Ben Murdoch85b71792012-04-11 18:30:58 +0100824
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100825 __ ldc1(double_exponent,
826 FieldMemOperand(exponent, HeapNumber::kValueOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +0000827 }
Ben Murdoch85b71792012-04-11 18:30:58 +0100828
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000829 if (exponent_type() != INTEGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100830 Label int_exponent_convert;
831 // Detect integer exponents stored as double.
832 __ EmitFPUTruncate(kRoundToMinusInf,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100833 scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000834 double_exponent,
835 at,
836 double_scratch,
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100837 scratch2,
838 kCheckForInexactConversion);
839 // scratch2 == 0 means there was no conversion error.
840 __ Branch(&int_exponent_convert, eq, scratch2, Operand(zero_reg));
841
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000842 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100843 // Detect square root case. Crankshaft detects constant +/-0.5 at
844 // compile time and uses DoMathPowHalf instead. We then skip this check
845 // for non-constant cases of +/-0.5 as these hardly occur.
846 Label not_plus_half;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100847 // Test for 0.5.
848 __ Move(double_scratch, 0.5);
849 __ BranchF(USE_DELAY_SLOT,
850 &not_plus_half,
851 NULL,
852 ne,
853 double_exponent,
854 double_scratch);
855 // double_scratch can be overwritten in the delay slot.
856 // Calculates square root of base. Check for the special case of
857 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400858 __ Move(double_scratch, static_cast<double>(-V8_INFINITY));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100859 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, double_base, double_scratch);
860 __ neg_d(double_result, double_scratch);
861
862 // Add +0 to convert -0 to +0.
863 __ add_d(double_scratch, double_base, kDoubleRegZero);
864 __ sqrt_d(double_result, double_scratch);
865 __ jmp(&done);
866
867 __ bind(&not_plus_half);
868 __ Move(double_scratch, -0.5);
869 __ BranchF(USE_DELAY_SLOT,
870 &call_runtime,
871 NULL,
872 ne,
873 double_exponent,
874 double_scratch);
875 // double_scratch can be overwritten in the delay slot.
876 // Calculates square root of base. Check for the special case of
877 // Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400878 __ Move(double_scratch, static_cast<double>(-V8_INFINITY));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100879 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, double_base, double_scratch);
880 __ Move(double_result, kDoubleRegZero);
881
882 // Add +0 to convert -0 to +0.
883 __ add_d(double_scratch, double_base, kDoubleRegZero);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400884 __ Move(double_result, 1.);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100885 __ sqrt_d(double_scratch, double_scratch);
886 __ div_d(double_result, double_result, double_scratch);
887 __ jmp(&done);
888 }
889
890 __ push(ra);
891 {
892 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000893 __ PrepareCallCFunction(0, 2, scratch2);
894 __ MovToFloatParameters(double_base, double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100895 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000896 ExternalReference::power_double_double_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100897 0, 2);
898 }
899 __ pop(ra);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000900 __ MovFromFloatResult(double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100901 __ jmp(&done);
902
903 __ bind(&int_exponent_convert);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100904 }
905
906 // Calculate power with integer exponent.
907 __ bind(&int_exponent);
908
909 // Get two copies of exponent in the registers scratch and exponent.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000910 if (exponent_type() == INTEGER) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100911 __ mov(scratch, exponent);
912 } else {
913 // Exponent has previously been stored into scratch as untagged integer.
914 __ mov(exponent, scratch);
915 }
916
917 __ mov_d(double_scratch, double_base); // Back up base.
918 __ Move(double_result, 1.0);
919
920 // Get absolute value of exponent.
921 Label positive_exponent;
922 __ Branch(&positive_exponent, ge, scratch, Operand(zero_reg));
923 __ Subu(scratch, zero_reg, scratch);
924 __ bind(&positive_exponent);
925
926 Label while_true, no_carry, loop_end;
927 __ bind(&while_true);
928
929 __ And(scratch2, scratch, 1);
930
931 __ Branch(&no_carry, eq, scratch2, Operand(zero_reg));
932 __ mul_d(double_result, double_result, double_scratch);
933 __ bind(&no_carry);
934
935 __ sra(scratch, scratch, 1);
936
937 __ Branch(&loop_end, eq, scratch, Operand(zero_reg));
938 __ mul_d(double_scratch, double_scratch, double_scratch);
939
940 __ Branch(&while_true);
941
942 __ bind(&loop_end);
943
944 __ Branch(&done, ge, exponent, Operand(zero_reg));
945 __ Move(double_scratch, 1.0);
946 __ div_d(double_result, double_scratch, double_result);
947 // Test whether result is zero. Bail out to check for subnormal result.
948 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
949 __ BranchF(&done, NULL, ne, double_result, kDoubleRegZero);
950
951 // double_exponent may not contain the exponent value if the input was a
952 // smi. We set it with exponent value before bailing out.
953 __ mtc1(exponent, single_scratch);
954 __ cvt_d_w(double_exponent, single_scratch);
955
956 // Returning or bailing out.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000957 Counters* counters = isolate()->counters();
958 if (exponent_type() == ON_STACK) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100959 // The arguments are still on the stack.
960 __ bind(&call_runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000961 __ TailCallRuntime(Runtime::kMathPowRT, 2, 1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100962
963 // The stub is called from non-optimized code, which expects the result
964 // as heap number in exponent.
965 __ bind(&done);
966 __ AllocateHeapNumber(
967 heapnumber, scratch, scratch2, heapnumbermap, &call_runtime);
968 __ sdc1(double_result,
969 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 DCHECK(heapnumber.is(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100971 __ IncrementCounter(counters->math_pow(), 1, scratch, scratch2);
972 __ DropAndRet(2);
973 } else {
974 __ push(ra);
975 {
976 AllowExternalCallThatCantCauseGC scope(masm);
977 __ PrepareCallCFunction(0, 2, scratch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000978 __ MovToFloatParameters(double_base, double_exponent);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100979 __ CallCFunction(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000980 ExternalReference::power_double_double_function(isolate()),
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100981 0, 2);
982 }
983 __ pop(ra);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000984 __ MovFromFloatResult(double_result);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100985
986 __ bind(&done);
987 __ IncrementCounter(counters->math_pow(), 1, scratch, scratch2);
988 __ Ret();
989 }
Steve Block44f0eee2011-05-26 01:26:41 +0100990}
991
992
993bool CEntryStub::NeedsImmovableCode() {
994 return true;
995}
996
997
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000998void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
999 CEntryStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001000 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
1001 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
1002 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
1003 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
1004 BinaryOpICStub::GenerateAheadOfTime(isolate);
1005 StoreRegistersStateStub::GenerateAheadOfTime(isolate);
1006 RestoreRegistersStateStub::GenerateAheadOfTime(isolate);
1007 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001008}
1009
1010
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001011void StoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
1012 StoreRegistersStateStub stub(isolate);
1013 stub.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001014}
1015
1016
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001017void RestoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
1018 RestoreRegistersStateStub stub(isolate);
1019 stub.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001020}
1021
1022
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001023void CodeStub::GenerateFPStubs(Isolate* isolate) {
1024 // Generate if not already in cache.
1025 SaveFPRegsMode mode = kSaveFPRegs;
1026 CEntryStub(isolate, 1, mode).GetCode();
1027 StoreBufferOverflowStub(isolate, mode).GetCode();
1028 isolate->set_fp_stubs_generated(true);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001029}
1030
1031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001032void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1033 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1034 stub.GetCode();
1035}
Ben Murdoch257744e2011-11-30 15:57:28 +00001036
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001037
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001038void CEntryStub::Generate(MacroAssembler* masm) {
1039 // Called from JavaScript; parameters are on stack as if calling JS function
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001040 // a0: number of arguments including receiver
1041 // a1: pointer to builtin function
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001042 // fp: frame pointer (restored after C call)
1043 // sp: stack pointer (restored as callee's sp after C call)
1044 // cp: current context (C callee-saved)
Ben Murdoch257744e2011-11-30 15:57:28 +00001045
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001046 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1047
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001048 // Compute the argv pointer in a callee-saved register.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001049 __ sll(s1, a0, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001050 __ Addu(s1, sp, s1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001051 __ Subu(s1, s1, kPointerSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001052
1053 // Enter the exit frame that transitions from JavaScript to C++.
1054 FrameScope scope(masm, StackFrame::MANUAL);
1055 __ EnterExitFrame(save_doubles());
1056
1057 // s0: number of arguments including receiver (C callee-saved)
1058 // s1: pointer to first argument (C callee-saved)
1059 // s2: pointer to builtin function (C callee-saved)
Ben Murdoch257744e2011-11-30 15:57:28 +00001060
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001061 // Prepare arguments for C routine.
1062 // a0 = argc
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001063 __ mov(s0, a0);
1064 __ mov(s2, a1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001065 // a1 = argv (set in the delay slot after find_ra below).
Ben Murdoch257744e2011-11-30 15:57:28 +00001066
1067 // We are calling compiled C/C++ code. a0 and a1 hold our two arguments. We
1068 // also need to reserve the 4 argument slots on the stack.
1069
1070 __ AssertStackIsAligned();
1071
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001072 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00001073
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001074 // To let the GC traverse the return address of the exit frames, we need to
1075 // know where the return address is. The CEntryStub is unmovable, so
1076 // we can store the address on the stack to be able to find it again and
1077 // we never have to restore it, because it will not change.
Ben Murdoch257744e2011-11-30 15:57:28 +00001078 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm);
1079 // This branch-and-link sequence is needed to find the current PC on mips,
1080 // saved to the ra register.
1081 // Use masm-> here instead of the double-underscore macro since extra
1082 // coverage code can interfere with the proper calculation of ra.
1083 Label find_ra;
1084 masm->bal(&find_ra); // bal exposes branch delay slot.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001085 masm->mov(a1, s1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001086 masm->bind(&find_ra);
1087
1088 // Adjust the value in ra to point to the correct return location, 2nd
1089 // instruction past the real call into C code (the jalr(t9)), and push it.
1090 // This is the return address of the exit frame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001091 const int kNumInstructionsToJump = 5;
Ben Murdoch257744e2011-11-30 15:57:28 +00001092 masm->Addu(ra, ra, kNumInstructionsToJump * kPointerSize);
1093 masm->sw(ra, MemOperand(sp)); // This spot was reserved in EnterExitFrame.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001094 // Stack space reservation moved to the branch delay slot below.
Ben Murdoch257744e2011-11-30 15:57:28 +00001095 // Stack is still aligned.
1096
1097 // Call the C routine.
1098 masm->mov(t9, s2); // Function pointer to t9 to conform to ABI for PIC.
1099 masm->jalr(t9);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001100 // Set up sp in the delay slot.
1101 masm->addiu(sp, sp, -kCArgsSlotsSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001102 // Make sure the stored 'ra' points to this position.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001103 DCHECK_EQ(kNumInstructionsToJump,
Ben Murdoch257744e2011-11-30 15:57:28 +00001104 masm->InstructionsGeneratedSince(&find_ra));
1105 }
1106
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001107
1108 // Runtime functions should not return 'the hole'. Allowing it to escape may
1109 // lead to crashes in the IC code later.
1110 if (FLAG_debug_code) {
1111 Label okay;
1112 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1113 __ Branch(&okay, ne, v0, Operand(t0));
1114 __ stop("The hole escaped");
1115 __ bind(&okay);
Ben Murdoch257744e2011-11-30 15:57:28 +00001116 }
1117
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001118 // Check result for exception sentinel.
1119 Label exception_returned;
1120 __ LoadRoot(t0, Heap::kExceptionRootIndex);
1121 __ Branch(&exception_returned, eq, t0, Operand(v0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001122
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001123 ExternalReference pending_exception_address(
1124 Isolate::kPendingExceptionAddress, isolate());
1125
1126 // Check that there is no pending exception, otherwise we
1127 // should have returned the exception sentinel.
1128 if (FLAG_debug_code) {
1129 Label okay;
1130 __ li(a2, Operand(pending_exception_address));
1131 __ lw(a2, MemOperand(a2));
1132 __ LoadRoot(t0, Heap::kTheHoleValueRootIndex);
1133 // Cannot use check here as it attempts to generate call into runtime.
1134 __ Branch(&okay, eq, t0, Operand(a2));
1135 __ stop("Unexpected pending exception");
1136 __ bind(&okay);
1137 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001138
1139 // Exit C frame and return.
1140 // v0:v1: result
1141 // sp: stack pointer
1142 // fp: frame pointer
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001143 // s0: still holds argc (callee-saved).
1144 __ LeaveExitFrame(save_doubles(), s0, true, EMIT_RETURN);
Ben Murdoch257744e2011-11-30 15:57:28 +00001145
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001146 // Handling of exception.
1147 __ bind(&exception_returned);
Ben Murdoch257744e2011-11-30 15:57:28 +00001148
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001149 // Retrieve the pending exception.
1150 __ li(a2, Operand(pending_exception_address));
1151 __ lw(v0, MemOperand(a2));
Ben Murdoch257744e2011-11-30 15:57:28 +00001152
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001153 // Clear the pending exception.
1154 __ li(a3, Operand(isolate()->factory()->the_hole_value()));
1155 __ sw(a3, MemOperand(a2));
Ben Murdoch257744e2011-11-30 15:57:28 +00001156
1157 // Special handling of termination exceptions which are uncatchable
1158 // by javascript code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001159 Label throw_termination_exception;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001160 __ LoadRoot(t0, Heap::kTerminationExceptionRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001161 __ Branch(&throw_termination_exception, eq, v0, Operand(t0));
Ben Murdoch257744e2011-11-30 15:57:28 +00001162
1163 // Handle normal exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001164 __ Throw(v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001165
1166 __ bind(&throw_termination_exception);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001167 __ ThrowUncatchable(v0);
Steve Block44f0eee2011-05-26 01:26:41 +01001168}
1169
1170
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001171void JSEntryStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001172 Label invoke, handler_entry, exit;
1173 Isolate* isolate = masm->isolate();
Ben Murdoch257744e2011-11-30 15:57:28 +00001174
1175 // Registers:
1176 // a0: entry address
1177 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001178 // a2: receiver
Ben Murdoch257744e2011-11-30 15:57:28 +00001179 // a3: argc
1180 //
1181 // Stack:
1182 // 4 args slots
1183 // args
1184
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001185 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1186
Ben Murdoch257744e2011-11-30 15:57:28 +00001187 // Save callee saved registers on the stack.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001188 __ MultiPush(kCalleeSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00001189
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001190 // Save callee-saved FPU registers.
1191 __ MultiPushFPU(kCalleeSavedFPU);
1192 // Set up the reserved register for 0.0.
1193 __ Move(kDoubleRegZero, 0.0);
Ben Murdoch589d6972011-11-30 16:04:58 +00001194
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001195
Ben Murdoch257744e2011-11-30 15:57:28 +00001196 // Load argv in s0 register.
Ben Murdoch589d6972011-11-30 16:04:58 +00001197 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001198 offset_to_argv += kNumCalleeSavedFPU * kDoubleSize;
Ben Murdoch589d6972011-11-30 16:04:58 +00001199
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001200 __ InitializeRootRegister();
Ben Murdoch589d6972011-11-30 16:04:58 +00001201 __ lw(s0, MemOperand(sp, offset_to_argv + kCArgsSlotsSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001202
1203 // We build an EntryFrame.
1204 __ li(t3, Operand(-1)); // Push a bad frame pointer to fail if it is used.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001205 int marker = type();
Ben Murdoch257744e2011-11-30 15:57:28 +00001206 __ li(t2, Operand(Smi::FromInt(marker)));
1207 __ li(t1, Operand(Smi::FromInt(marker)));
Ben Murdoch589d6972011-11-30 16:04:58 +00001208 __ li(t0, Operand(ExternalReference(Isolate::kCEntryFPAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001209 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001210 __ lw(t0, MemOperand(t0));
1211 __ Push(t3, t2, t1, t0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001212 // Set up frame pointer for the frame to be pushed.
Ben Murdoch257744e2011-11-30 15:57:28 +00001213 __ addiu(fp, sp, -EntryFrameConstants::kCallerFPOffset);
1214
1215 // Registers:
1216 // a0: entry_address
1217 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001218 // a2: receiver_pointer
Ben Murdoch257744e2011-11-30 15:57:28 +00001219 // a3: argc
1220 // s0: argv
1221 //
1222 // Stack:
1223 // caller fp |
1224 // function slot | entry frame
1225 // context slot |
1226 // bad fp (0xff...f) |
1227 // callee saved registers + ra
1228 // 4 args slots
1229 // args
1230
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001231 // If this is the outermost JS call, set js_entry_sp value.
1232 Label non_outermost_js;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001233 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001234 __ li(t1, Operand(ExternalReference(js_entry_sp)));
1235 __ lw(t2, MemOperand(t1));
1236 __ Branch(&non_outermost_js, ne, t2, Operand(zero_reg));
1237 __ sw(fp, MemOperand(t1));
1238 __ li(t0, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1239 Label cont;
1240 __ b(&cont);
1241 __ nop(); // Branch delay slot nop.
1242 __ bind(&non_outermost_js);
1243 __ li(t0, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
1244 __ bind(&cont);
1245 __ push(t0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001246
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001247 // Jump to a faked try block that does the invoke, with a faked catch
1248 // block that sets the pending exception.
1249 __ jmp(&invoke);
1250 __ bind(&handler_entry);
1251 handler_offset_ = handler_entry.pos();
1252 // Caught exception: Store result (exception) in the pending exception
1253 // field in the JSEnv and return a failure sentinel. Coming in here the
1254 // fp will be invalid because the PushTryHandler below sets it to 0 to
1255 // signal the existence of the JSEntry frame.
Ben Murdoch589d6972011-11-30 16:04:58 +00001256 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001257 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001258 __ sw(v0, MemOperand(t0)); // We come back from 'invoke'. result is in v0.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001259 __ LoadRoot(v0, Heap::kExceptionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001260 __ b(&exit); // b exposes branch delay slot.
1261 __ nop(); // Branch delay slot nop.
1262
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001263 // Invoke: Link this frame into the handler chain. There's only one
1264 // handler block in this code object, so its index is 0.
Ben Murdoch257744e2011-11-30 15:57:28 +00001265 __ bind(&invoke);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001266 __ PushTryHandler(StackHandler::JS_ENTRY, 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001267 // If an exception not caught by another handler occurs, this handler
1268 // returns control to the code after the bal(&invoke) above, which
1269 // restores all kCalleeSaved registers (including cp and fp) to their
1270 // saved values before returning a failure to C.
1271
1272 // Clear any pending exceptions.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001273 __ LoadRoot(t1, Heap::kTheHoleValueRootIndex);
Ben Murdoch589d6972011-11-30 16:04:58 +00001274 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001275 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001276 __ sw(t1, MemOperand(t0));
1277
1278 // Invoke the function by calling through JS entry trampoline builtin.
1279 // Notice that we cannot store a reference to the trampoline code directly in
1280 // this stub, because runtime stubs are not traversed when doing GC.
1281
1282 // Registers:
1283 // a0: entry_address
1284 // a1: function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001285 // a2: receiver_pointer
Ben Murdoch257744e2011-11-30 15:57:28 +00001286 // a3: argc
1287 // s0: argv
1288 //
1289 // Stack:
1290 // handler frame
1291 // entry frame
1292 // callee saved registers + ra
1293 // 4 args slots
1294 // args
1295
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001296 if (type() == StackFrame::ENTRY_CONSTRUCT) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001297 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001298 isolate);
Ben Murdoch257744e2011-11-30 15:57:28 +00001299 __ li(t0, Operand(construct_entry));
1300 } else {
1301 ExternalReference entry(Builtins::kJSEntryTrampoline, masm->isolate());
1302 __ li(t0, Operand(entry));
1303 }
1304 __ lw(t9, MemOperand(t0)); // Deref address.
1305
1306 // Call JSEntryTrampoline.
1307 __ addiu(t9, t9, Code::kHeaderSize - kHeapObjectTag);
1308 __ Call(t9);
1309
1310 // Unlink this frame from the handler chain.
1311 __ PopTryHandler();
1312
1313 __ bind(&exit); // v0 holds result
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001314 // Check if the current stack frame is marked as the outermost JS frame.
1315 Label non_outermost_js_2;
1316 __ pop(t1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001317 __ Branch(&non_outermost_js_2,
1318 ne,
1319 t1,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001320 Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1321 __ li(t1, Operand(ExternalReference(js_entry_sp)));
1322 __ sw(zero_reg, MemOperand(t1));
1323 __ bind(&non_outermost_js_2);
Ben Murdoch257744e2011-11-30 15:57:28 +00001324
1325 // Restore the top frame descriptors from the stack.
1326 __ pop(t1);
Ben Murdoch589d6972011-11-30 16:04:58 +00001327 __ li(t0, Operand(ExternalReference(Isolate::kCEntryFPAddress,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001328 isolate)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001329 __ sw(t1, MemOperand(t0));
1330
1331 // Reset the stack to the callee saved registers.
1332 __ addiu(sp, sp, -EntryFrameConstants::kCallerFPOffset);
1333
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001334 // Restore callee-saved fpu registers.
1335 __ MultiPopFPU(kCalleeSavedFPU);
Ben Murdoch589d6972011-11-30 16:04:58 +00001336
Ben Murdoch257744e2011-11-30 15:57:28 +00001337 // Restore callee saved registers from the stack.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001338 __ MultiPop(kCalleeSaved | ra.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00001339 // Return.
1340 __ Jump(ra);
Steve Block44f0eee2011-05-26 01:26:41 +01001341}
1342
1343
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001344void LoadIndexedStringStub::Generate(MacroAssembler* masm) {
1345 // Return address is in ra.
1346 Label miss;
1347
1348 Register receiver = LoadDescriptor::ReceiverRegister();
1349 Register index = LoadDescriptor::NameRegister();
1350 Register scratch = t1;
1351 Register result = v0;
1352 DCHECK(!scratch.is(receiver) && !scratch.is(index));
1353 DCHECK(!FLAG_vector_ics ||
1354 (!scratch.is(VectorLoadICDescriptor::VectorRegister()) &&
1355 result.is(VectorLoadICDescriptor::SlotRegister())));
1356
1357 // StringCharAtGenerator doesn't use the result register until it's passed
1358 // the different miss possibilities. If it did, we would have a conflict
1359 // when FLAG_vector_ics is true.
1360 StringCharAtGenerator char_at_generator(receiver, index, scratch, result,
1361 &miss, // When not a string.
1362 &miss, // When not a number.
1363 &miss, // When index out of range.
1364 STRING_INDEX_IS_ARRAY_INDEX,
1365 RECEIVER_IS_STRING);
1366 char_at_generator.GenerateFast(masm);
1367 __ Ret();
1368
1369 StubRuntimeCallHelper call_helper;
1370 char_at_generator.GenerateSlow(masm, call_helper);
1371
1372 __ bind(&miss);
1373 PropertyAccessCompiler::TailCallBuiltin(
1374 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
1375}
1376
1377
Ben Murdoch257744e2011-11-30 15:57:28 +00001378// Uses registers a0 to t0.
1379// Expected input (depending on whether args are in registers or on the stack):
1380// * object: a0 or at sp + 1 * kPointerSize.
1381// * function: a1 or at sp.
1382//
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001383// An inlined call site may have been generated before calling this stub.
1384// In this case the offset to the inline site to patch is passed on the stack,
1385// in the safepoint slot for register t0.
Steve Block44f0eee2011-05-26 01:26:41 +01001386void InstanceofStub::Generate(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001387 // Call site inlining and patching implies arguments in registers.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001388 DCHECK(HasArgsInRegisters() || !HasCallSiteInlineCheck());
Ben Murdoch257744e2011-11-30 15:57:28 +00001389
1390 // Fixed register usage throughout the stub:
1391 const Register object = a0; // Object (lhs).
1392 Register map = a3; // Map of the object.
1393 const Register function = a1; // Function (rhs).
1394 const Register prototype = t0; // Prototype of the function.
1395 const Register inline_site = t5;
1396 const Register scratch = a2;
1397
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001398 const int32_t kDeltaToLoadBoolResult = 5 * kPointerSize;
1399
Ben Murdoch257744e2011-11-30 15:57:28 +00001400 Label slow, loop, is_instance, is_not_instance, not_js_object;
1401
1402 if (!HasArgsInRegisters()) {
1403 __ lw(object, MemOperand(sp, 1 * kPointerSize));
1404 __ lw(function, MemOperand(sp, 0));
1405 }
1406
1407 // Check that the left hand is a JS object and load map.
1408 __ JumpIfSmi(object, &not_js_object);
1409 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
1410
1411 // If there is a call site cache don't look in the global cache, but do the
1412 // real lookup and update the call site cache.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001413 if (!HasCallSiteInlineCheck() && !ReturnTrueFalseObject()) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001414 Label miss;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001415 __ LoadRoot(at, Heap::kInstanceofCacheFunctionRootIndex);
1416 __ Branch(&miss, ne, function, Operand(at));
1417 __ LoadRoot(at, Heap::kInstanceofCacheMapRootIndex);
1418 __ Branch(&miss, ne, map, Operand(at));
Ben Murdoch257744e2011-11-30 15:57:28 +00001419 __ LoadRoot(v0, Heap::kInstanceofCacheAnswerRootIndex);
1420 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
1421
1422 __ bind(&miss);
1423 }
1424
1425 // Get the prototype of the function.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001426 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
Ben Murdoch257744e2011-11-30 15:57:28 +00001427
1428 // Check that the function prototype is a JS object.
1429 __ JumpIfSmi(prototype, &slow);
1430 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
1431
1432 // Update the global instanceof or call site inlined cache with the current
1433 // map and function. The cached answer will be set when it is known below.
1434 if (!HasCallSiteInlineCheck()) {
1435 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
1436 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
1437 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001438 DCHECK(HasArgsInRegisters());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001439 // Patch the (relocated) inlined map check.
1440
1441 // The offset was stored in t0 safepoint slot.
1442 // (See LCodeGen::DoDeferredLInstanceOfKnownGlobal).
1443 __ LoadFromSafepointRegisterSlot(scratch, t0);
1444 __ Subu(inline_site, ra, scratch);
1445 // Get the map location in scratch and patch it.
1446 __ GetRelocatedValue(inline_site, scratch, v1); // v1 used as scratch.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001447 __ sw(map, FieldMemOperand(scratch, Cell::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001448 }
1449
1450 // Register mapping: a3 is object map and t0 is function prototype.
1451 // Get prototype of object into a2.
1452 __ lw(scratch, FieldMemOperand(map, Map::kPrototypeOffset));
1453
1454 // We don't need map any more. Use it as a scratch register.
1455 Register scratch2 = map;
1456 map = no_reg;
1457
1458 // Loop through the prototype chain looking for the function prototype.
1459 __ LoadRoot(scratch2, Heap::kNullValueRootIndex);
1460 __ bind(&loop);
1461 __ Branch(&is_instance, eq, scratch, Operand(prototype));
1462 __ Branch(&is_not_instance, eq, scratch, Operand(scratch2));
1463 __ lw(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
1464 __ lw(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
1465 __ Branch(&loop);
1466
1467 __ bind(&is_instance);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001468 DCHECK(Smi::FromInt(0) == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001469 if (!HasCallSiteInlineCheck()) {
1470 __ mov(v0, zero_reg);
1471 __ StoreRoot(v0, Heap::kInstanceofCacheAnswerRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001472 if (ReturnTrueFalseObject()) {
1473 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
1474 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001475 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001476 // Patch the call site to return true.
1477 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
1478 __ Addu(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
1479 // Get the boolean result location in scratch and patch it.
1480 __ PatchRelocatedValue(inline_site, scratch, v0);
1481
1482 if (!ReturnTrueFalseObject()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001483 DCHECK_EQ(Smi::FromInt(0), 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001484 __ mov(v0, zero_reg);
1485 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001486 }
1487 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
1488
1489 __ bind(&is_not_instance);
1490 if (!HasCallSiteInlineCheck()) {
1491 __ li(v0, Operand(Smi::FromInt(1)));
1492 __ StoreRoot(v0, Heap::kInstanceofCacheAnswerRootIndex);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001493 if (ReturnTrueFalseObject()) {
1494 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
1495 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001496 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001497 // Patch the call site to return false.
1498 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
1499 __ Addu(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
1500 // Get the boolean result location in scratch and patch it.
1501 __ PatchRelocatedValue(inline_site, scratch, v0);
1502
1503 if (!ReturnTrueFalseObject()) {
1504 __ li(v0, Operand(Smi::FromInt(1)));
1505 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001506 }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001507
Ben Murdoch257744e2011-11-30 15:57:28 +00001508 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
1509
1510 Label object_not_null, object_not_null_or_smi;
1511 __ bind(&not_js_object);
1512 // Before null, smi and string value checks, check that the rhs is a function
1513 // as for a non-function rhs an exception needs to be thrown.
1514 __ JumpIfSmi(function, &slow);
1515 __ GetObjectType(function, scratch2, scratch);
1516 __ Branch(&slow, ne, scratch, Operand(JS_FUNCTION_TYPE));
1517
1518 // Null is not instance of anything.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001519 __ Branch(&object_not_null, ne, object,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001520 Operand(isolate()->factory()->null_value()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001521 if (ReturnTrueFalseObject()) {
1522 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
1523 } else {
1524 __ li(v0, Operand(Smi::FromInt(1)));
1525 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001526 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
1527
1528 __ bind(&object_not_null);
1529 // Smi values are not instances of anything.
1530 __ JumpIfNotSmi(object, &object_not_null_or_smi);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001531 if (ReturnTrueFalseObject()) {
1532 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
1533 } else {
1534 __ li(v0, Operand(Smi::FromInt(1)));
1535 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001536 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
1537
1538 __ bind(&object_not_null_or_smi);
1539 // String values are not instances of anything.
1540 __ IsObjectJSStringType(object, scratch, &slow);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001541 if (ReturnTrueFalseObject()) {
1542 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
1543 } else {
1544 __ li(v0, Operand(Smi::FromInt(1)));
1545 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001546 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
1547
1548 // Slow-case. Tail call builtin.
1549 __ bind(&slow);
1550 if (!ReturnTrueFalseObject()) {
1551 if (HasArgsInRegisters()) {
1552 __ Push(a0, a1);
1553 }
1554 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
1555 } else {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001556 {
1557 FrameScope scope(masm, StackFrame::INTERNAL);
1558 __ Push(a0, a1);
1559 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
1560 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001561 __ mov(a0, v0);
1562 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
1563 __ DropAndRet(HasArgsInRegisters() ? 0 : 2, eq, a0, Operand(zero_reg));
1564 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
1565 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
1566 }
Steve Block44f0eee2011-05-26 01:26:41 +01001567}
1568
1569
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001570void FunctionPrototypeStub::Generate(MacroAssembler* masm) {
1571 Label miss;
1572 Register receiver = LoadDescriptor::ReceiverRegister();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001573 // Ensure that the vector and slot registers won't be clobbered before
1574 // calling the miss handler.
1575 DCHECK(!FLAG_vector_ics ||
1576 !AreAliased(t0, t1, VectorLoadICDescriptor::VectorRegister(),
1577 VectorLoadICDescriptor::SlotRegister()));
1578
1579 NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(masm, receiver, t0,
1580 t1, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001581 __ bind(&miss);
1582 PropertyAccessCompiler::TailCallBuiltin(
1583 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
1584}
Ben Murdoch257744e2011-11-30 15:57:28 +00001585
1586
Steve Block44f0eee2011-05-26 01:26:41 +01001587void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001588 // The displacement is the offset of the last parameter (if any)
1589 // relative to the frame pointer.
Ben Murdochdb1b4382012-04-26 19:03:50 +01001590 const int kDisplacement =
Ben Murdoch257744e2011-11-30 15:57:28 +00001591 StandardFrameConstants::kCallerSPOffset - kPointerSize;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001592 DCHECK(a1.is(ArgumentsAccessReadDescriptor::index()));
1593 DCHECK(a0.is(ArgumentsAccessReadDescriptor::parameter_count()));
Ben Murdoch257744e2011-11-30 15:57:28 +00001594
1595 // Check that the key is a smiGenerateReadElement.
1596 Label slow;
1597 __ JumpIfNotSmi(a1, &slow);
1598
1599 // Check if the calling frame is an arguments adaptor frame.
1600 Label adaptor;
1601 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1602 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
1603 __ Branch(&adaptor,
1604 eq,
1605 a3,
1606 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1607
1608 // Check index (a1) against formal parameters count limit passed in
1609 // through register a0. Use unsigned comparison to get negative
1610 // check for free.
1611 __ Branch(&slow, hs, a1, Operand(a0));
1612
1613 // Read the argument from the stack and return it.
1614 __ subu(a3, a0, a1);
1615 __ sll(t3, a3, kPointerSizeLog2 - kSmiTagSize);
1616 __ Addu(a3, fp, Operand(t3));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001617 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00001618 __ lw(v0, MemOperand(a3, kDisplacement));
Ben Murdoch257744e2011-11-30 15:57:28 +00001619
1620 // Arguments adaptor case: Check index (a1) against actual arguments
1621 // limit found in the arguments adaptor frame. Use unsigned
1622 // comparison to get negative check for free.
1623 __ bind(&adaptor);
1624 __ lw(a0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1625 __ Branch(&slow, Ugreater_equal, a1, Operand(a0));
1626
1627 // Read the argument from the adaptor frame and return it.
1628 __ subu(a3, a0, a1);
1629 __ sll(t3, a3, kPointerSizeLog2 - kSmiTagSize);
1630 __ Addu(a3, a2, Operand(t3));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001631 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00001632 __ lw(v0, MemOperand(a3, kDisplacement));
Ben Murdoch257744e2011-11-30 15:57:28 +00001633
1634 // Slow-case: Handle non-smi or out-of-bounds access to arguments
1635 // by calling the runtime system.
1636 __ bind(&slow);
1637 __ push(a1);
1638 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
Steve Block44f0eee2011-05-26 01:26:41 +01001639}
1640
1641
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001642void ArgumentsAccessStub::GenerateNewSloppySlow(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001643 // sp[0] : number of parameters
1644 // sp[4] : receiver displacement
1645 // sp[8] : function
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001646 // Check if the calling frame is an arguments adaptor frame.
1647 Label runtime;
1648 __ lw(a3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1649 __ lw(a2, MemOperand(a3, StandardFrameConstants::kContextOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001650 __ Branch(&runtime,
1651 ne,
1652 a2,
1653 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001654
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001655 // Patch the arguments.length and the parameters pointer in the current frame.
1656 __ lw(a2, MemOperand(a3, ArgumentsAdaptorFrameConstants::kLengthOffset));
1657 __ sw(a2, MemOperand(sp, 0 * kPointerSize));
1658 __ sll(t3, a2, 1);
1659 __ Addu(a3, a3, Operand(t3));
1660 __ addiu(a3, a3, StandardFrameConstants::kCallerSPOffset);
1661 __ sw(a3, MemOperand(sp, 1 * kPointerSize));
1662
1663 __ bind(&runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001665}
1666
1667
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001668void ArgumentsAccessStub::GenerateNewSloppyFast(MacroAssembler* masm) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001669 // Stack layout:
1670 // sp[0] : number of parameters (tagged)
1671 // sp[4] : address of receiver argument
1672 // sp[8] : function
1673 // Registers used over whole function:
1674 // t2 : allocated object (tagged)
1675 // t5 : mapped parameter count (tagged)
1676
1677 __ lw(a1, MemOperand(sp, 0 * kPointerSize));
1678 // a1 = parameter count (tagged)
1679
1680 // Check if the calling frame is an arguments adaptor frame.
1681 Label runtime;
1682 Label adaptor_frame, try_allocate;
1683 __ lw(a3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1684 __ lw(a2, MemOperand(a3, StandardFrameConstants::kContextOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001685 __ Branch(&adaptor_frame,
1686 eq,
1687 a2,
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001688 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1689
1690 // No adaptor, parameter count = argument count.
1691 __ mov(a2, a1);
1692 __ b(&try_allocate);
1693 __ nop(); // Branch delay slot nop.
1694
1695 // We have an adaptor frame. Patch the parameters pointer.
1696 __ bind(&adaptor_frame);
1697 __ lw(a2, MemOperand(a3, ArgumentsAdaptorFrameConstants::kLengthOffset));
1698 __ sll(t6, a2, 1);
1699 __ Addu(a3, a3, Operand(t6));
1700 __ Addu(a3, a3, Operand(StandardFrameConstants::kCallerSPOffset));
1701 __ sw(a3, MemOperand(sp, 1 * kPointerSize));
1702
1703 // a1 = parameter count (tagged)
1704 // a2 = argument count (tagged)
1705 // Compute the mapped parameter count = min(a1, a2) in a1.
1706 Label skip_min;
1707 __ Branch(&skip_min, lt, a1, Operand(a2));
1708 __ mov(a1, a2);
1709 __ bind(&skip_min);
1710
1711 __ bind(&try_allocate);
1712
1713 // Compute the sizes of backing store, parameter map, and arguments object.
1714 // 1. Parameter map, has 2 extra words containing context and backing store.
1715 const int kParameterMapHeaderSize =
1716 FixedArray::kHeaderSize + 2 * kPointerSize;
1717 // If there are no mapped parameters, we do not need the parameter_map.
1718 Label param_map_size;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001719 DCHECK_EQ(0, Smi::FromInt(0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001720 __ Branch(USE_DELAY_SLOT, &param_map_size, eq, a1, Operand(zero_reg));
1721 __ mov(t5, zero_reg); // In delay slot: param map size = 0 when a1 == 0.
1722 __ sll(t5, a1, 1);
1723 __ addiu(t5, t5, kParameterMapHeaderSize);
1724 __ bind(&param_map_size);
1725
1726 // 2. Backing store.
1727 __ sll(t6, a2, 1);
1728 __ Addu(t5, t5, Operand(t6));
1729 __ Addu(t5, t5, Operand(FixedArray::kHeaderSize));
1730
1731 // 3. Arguments object.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001732 __ Addu(t5, t5, Operand(Heap::kSloppyArgumentsObjectSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001733
1734 // Do the allocation of all three objects in one go.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001735 __ Allocate(t5, v0, a3, t0, &runtime, TAG_OBJECT);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001736
1737 // v0 = address of new object(s) (tagged)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001738 // a2 = argument count (smi-tagged)
1739 // Get the arguments boilerplate from the current native context into t0.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001740 const int kNormalOffset =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001741 Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001742 const int kAliasedOffset =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 Context::SlotOffset(Context::ALIASED_ARGUMENTS_MAP_INDEX);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001744
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001745 __ lw(t0, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1746 __ lw(t0, FieldMemOperand(t0, GlobalObject::kNativeContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001747 Label skip2_ne, skip2_eq;
1748 __ Branch(&skip2_ne, ne, a1, Operand(zero_reg));
1749 __ lw(t0, MemOperand(t0, kNormalOffset));
1750 __ bind(&skip2_ne);
1751
1752 __ Branch(&skip2_eq, eq, a1, Operand(zero_reg));
1753 __ lw(t0, MemOperand(t0, kAliasedOffset));
1754 __ bind(&skip2_eq);
1755
1756 // v0 = address of new object (tagged)
1757 // a1 = mapped parameter count (tagged)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001758 // a2 = argument count (smi-tagged)
1759 // t0 = address of arguments map (tagged)
1760 __ sw(t0, FieldMemOperand(v0, JSObject::kMapOffset));
1761 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
1762 __ sw(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
1763 __ sw(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001764
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001765 // Set up the callee in-object property.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001766 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
1767 __ lw(a3, MemOperand(sp, 2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001768 __ AssertNotSmi(a3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001769 const int kCalleeOffset = JSObject::kHeaderSize +
1770 Heap::kArgumentsCalleeIndex * kPointerSize;
1771 __ sw(a3, FieldMemOperand(v0, kCalleeOffset));
1772
1773 // Use the length (smi tagged) and set that as an in-object property too.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001774 __ AssertSmi(a2);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001775 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
1776 const int kLengthOffset = JSObject::kHeaderSize +
1777 Heap::kArgumentsLengthIndex * kPointerSize;
1778 __ sw(a2, FieldMemOperand(v0, kLengthOffset));
1779
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001780 // Set up the elements pointer in the allocated arguments object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001781 // If we allocated a parameter map, t0 will point there, otherwise
1782 // it will point to the backing store.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001783 __ Addu(t0, v0, Operand(Heap::kSloppyArgumentsObjectSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001784 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
1785
1786 // v0 = address of new object (tagged)
1787 // a1 = mapped parameter count (tagged)
1788 // a2 = argument count (tagged)
1789 // t0 = address of parameter map or backing store (tagged)
1790 // Initialize parameter map. If there are no mapped arguments, we're done.
1791 Label skip_parameter_map;
1792 Label skip3;
1793 __ Branch(&skip3, ne, a1, Operand(Smi::FromInt(0)));
1794 // Move backing store address to a3, because it is
1795 // expected there when filling in the unmapped arguments.
1796 __ mov(a3, t0);
1797 __ bind(&skip3);
1798
1799 __ Branch(&skip_parameter_map, eq, a1, Operand(Smi::FromInt(0)));
1800
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001801 __ LoadRoot(t2, Heap::kSloppyArgumentsElementsMapRootIndex);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001802 __ sw(t2, FieldMemOperand(t0, FixedArray::kMapOffset));
1803 __ Addu(t2, a1, Operand(Smi::FromInt(2)));
1804 __ sw(t2, FieldMemOperand(t0, FixedArray::kLengthOffset));
1805 __ sw(cp, FieldMemOperand(t0, FixedArray::kHeaderSize + 0 * kPointerSize));
1806 __ sll(t6, a1, 1);
1807 __ Addu(t2, t0, Operand(t6));
1808 __ Addu(t2, t2, Operand(kParameterMapHeaderSize));
1809 __ sw(t2, FieldMemOperand(t0, FixedArray::kHeaderSize + 1 * kPointerSize));
1810
1811 // Copy the parameter slots and the holes in the arguments.
1812 // We need to fill in mapped_parameter_count slots. They index the context,
1813 // where parameters are stored in reverse order, at
1814 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
1815 // The mapped parameter thus need to get indices
1816 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
1817 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
1818 // We loop from right to left.
1819 Label parameters_loop, parameters_test;
1820 __ mov(t2, a1);
1821 __ lw(t5, MemOperand(sp, 0 * kPointerSize));
1822 __ Addu(t5, t5, Operand(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
1823 __ Subu(t5, t5, Operand(a1));
1824 __ LoadRoot(t3, Heap::kTheHoleValueRootIndex);
1825 __ sll(t6, t2, 1);
1826 __ Addu(a3, t0, Operand(t6));
1827 __ Addu(a3, a3, Operand(kParameterMapHeaderSize));
1828
1829 // t2 = loop variable (tagged)
1830 // a1 = mapping index (tagged)
1831 // a3 = address of backing store (tagged)
1832 // t0 = address of parameter map (tagged)
1833 // t1 = temporary scratch (a.o., for address calculation)
1834 // t3 = the hole value
1835 __ jmp(&parameters_test);
1836
1837 __ bind(&parameters_loop);
1838 __ Subu(t2, t2, Operand(Smi::FromInt(1)));
1839 __ sll(t1, t2, 1);
1840 __ Addu(t1, t1, Operand(kParameterMapHeaderSize - kHeapObjectTag));
1841 __ Addu(t6, t0, t1);
1842 __ sw(t5, MemOperand(t6));
1843 __ Subu(t1, t1, Operand(kParameterMapHeaderSize - FixedArray::kHeaderSize));
1844 __ Addu(t6, a3, t1);
1845 __ sw(t3, MemOperand(t6));
1846 __ Addu(t5, t5, Operand(Smi::FromInt(1)));
1847 __ bind(&parameters_test);
1848 __ Branch(&parameters_loop, ne, t2, Operand(Smi::FromInt(0)));
1849
1850 __ bind(&skip_parameter_map);
1851 // a2 = argument count (tagged)
1852 // a3 = address of backing store (tagged)
1853 // t1 = scratch
1854 // Copy arguments header and remaining slots (if there are any).
1855 __ LoadRoot(t1, Heap::kFixedArrayMapRootIndex);
1856 __ sw(t1, FieldMemOperand(a3, FixedArray::kMapOffset));
1857 __ sw(a2, FieldMemOperand(a3, FixedArray::kLengthOffset));
1858
1859 Label arguments_loop, arguments_test;
1860 __ mov(t5, a1);
1861 __ lw(t0, MemOperand(sp, 1 * kPointerSize));
1862 __ sll(t6, t5, 1);
1863 __ Subu(t0, t0, Operand(t6));
1864 __ jmp(&arguments_test);
1865
1866 __ bind(&arguments_loop);
1867 __ Subu(t0, t0, Operand(kPointerSize));
1868 __ lw(t2, MemOperand(t0, 0));
1869 __ sll(t6, t5, 1);
1870 __ Addu(t1, a3, Operand(t6));
1871 __ sw(t2, FieldMemOperand(t1, FixedArray::kHeaderSize));
1872 __ Addu(t5, t5, Operand(Smi::FromInt(1)));
1873
1874 __ bind(&arguments_test);
1875 __ Branch(&arguments_loop, lt, t5, Operand(a2));
1876
1877 // Return and remove the on-stack parameters.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001878 __ DropAndRet(3);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001879
1880 // Do the runtime call to allocate the arguments object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001881 // a2 = argument count (tagged)
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001882 __ bind(&runtime);
1883 __ sw(a2, MemOperand(sp, 0 * kPointerSize)); // Patch argument count.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001884 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
1885}
1886
1887
1888void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
1889 // Return address is in ra.
1890 Label slow;
1891
1892 Register receiver = LoadDescriptor::ReceiverRegister();
1893 Register key = LoadDescriptor::NameRegister();
1894
1895 // Check that the key is an array index, that is Uint32.
1896 __ And(t0, key, Operand(kSmiTagMask | kSmiSignMask));
1897 __ Branch(&slow, ne, t0, Operand(zero_reg));
1898
1899 // Everything is fine, call runtime.
1900 __ Push(receiver, key); // Receiver, key.
1901
1902 // Perform tail call to the entry.
1903 __ TailCallExternalReference(
1904 ExternalReference(IC_Utility(IC::kLoadElementWithInterceptor),
1905 masm->isolate()),
1906 2, 1);
1907
1908 __ bind(&slow);
1909 PropertyAccessCompiler::TailCallBuiltin(
1910 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001911}
1912
1913
1914void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
1915 // sp[0] : number of parameters
1916 // sp[4] : receiver displacement
1917 // sp[8] : function
Ben Murdoch257744e2011-11-30 15:57:28 +00001918 // Check if the calling frame is an arguments adaptor frame.
1919 Label adaptor_frame, try_allocate, runtime;
1920 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1921 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
1922 __ Branch(&adaptor_frame,
1923 eq,
1924 a3,
1925 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1926
1927 // Get the length from the frame.
1928 __ lw(a1, MemOperand(sp, 0));
1929 __ Branch(&try_allocate);
1930
1931 // Patch the arguments.length and the parameters pointer.
1932 __ bind(&adaptor_frame);
1933 __ lw(a1, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1934 __ sw(a1, MemOperand(sp, 0));
1935 __ sll(at, a1, kPointerSizeLog2 - kSmiTagSize);
1936 __ Addu(a3, a2, Operand(at));
1937
1938 __ Addu(a3, a3, Operand(StandardFrameConstants::kCallerSPOffset));
1939 __ sw(a3, MemOperand(sp, 1 * kPointerSize));
1940
1941 // Try the new space allocation. Start out with computing the size
1942 // of the arguments object and the elements array in words.
1943 Label add_arguments_object;
1944 __ bind(&try_allocate);
1945 __ Branch(&add_arguments_object, eq, a1, Operand(zero_reg));
1946 __ srl(a1, a1, kSmiTagSize);
1947
1948 __ Addu(a1, a1, Operand(FixedArray::kHeaderSize / kPointerSize));
1949 __ bind(&add_arguments_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001950 __ Addu(a1, a1, Operand(Heap::kStrictArgumentsObjectSize / kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001951
1952 // Do the allocation of both objects in one go.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001953 __ Allocate(a1, v0, a2, a3, &runtime,
1954 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
Ben Murdoch257744e2011-11-30 15:57:28 +00001955
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001956 // Get the arguments boilerplate from the current native context.
1957 __ lw(t0, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1958 __ lw(t0, FieldMemOperand(t0, GlobalObject::kNativeContextOffset));
1959 __ lw(t0, MemOperand(
1960 t0, Context::SlotOffset(Context::STRICT_ARGUMENTS_MAP_INDEX)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001961
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001962 __ sw(t0, FieldMemOperand(v0, JSObject::kMapOffset));
1963 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
1964 __ sw(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
1965 __ sw(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001966
Ben Murdoch257744e2011-11-30 15:57:28 +00001967 // Get the length (smi tagged) and set that as an in-object property too.
1968 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
1969 __ lw(a1, MemOperand(sp, 0 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001970 __ AssertSmi(a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001971 __ sw(a1, FieldMemOperand(v0, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001972 Heap::kArgumentsLengthIndex * kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001973
1974 Label done;
1975 __ Branch(&done, eq, a1, Operand(zero_reg));
1976
1977 // Get the parameters pointer from the stack.
1978 __ lw(a2, MemOperand(sp, 1 * kPointerSize));
1979
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001980 // Set up the elements pointer in the allocated arguments object and
Ben Murdoch257744e2011-11-30 15:57:28 +00001981 // initialize the header in the elements fixed array.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001982 __ Addu(t0, v0, Operand(Heap::kStrictArgumentsObjectSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001983 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
1984 __ LoadRoot(a3, Heap::kFixedArrayMapRootIndex);
1985 __ sw(a3, FieldMemOperand(t0, FixedArray::kMapOffset));
1986 __ sw(a1, FieldMemOperand(t0, FixedArray::kLengthOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001987 // Untag the length for the loop.
1988 __ srl(a1, a1, kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00001989
1990 // Copy the fixed array slots.
1991 Label loop;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001992 // Set up t0 to point to the first array slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00001993 __ Addu(t0, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1994 __ bind(&loop);
1995 // Pre-decrement a2 with kPointerSize on each iteration.
1996 // Pre-decrement in order to skip receiver.
1997 __ Addu(a2, a2, Operand(-kPointerSize));
1998 __ lw(a3, MemOperand(a2));
1999 // Post-increment t0 with kPointerSize on each iteration.
2000 __ sw(a3, MemOperand(t0));
2001 __ Addu(t0, t0, Operand(kPointerSize));
2002 __ Subu(a1, a1, Operand(1));
2003 __ Branch(&loop, ne, a1, Operand(zero_reg));
2004
2005 // Return and remove the on-stack parameters.
2006 __ bind(&done);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002007 __ DropAndRet(3);
Ben Murdoch257744e2011-11-30 15:57:28 +00002008
2009 // Do the runtime call to allocate the arguments object.
2010 __ bind(&runtime);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002011 __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
Steve Block44f0eee2011-05-26 01:26:41 +01002012}
2013
2014
2015void RegExpExecStub::Generate(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002016 // Just jump directly to runtime if native RegExp is not selected at compile
2017 // time or if regexp entry in generated code is turned off runtime switch or
2018 // at compilation.
2019#ifdef V8_INTERPRETED_REGEXP
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002020 __ TailCallRuntime(Runtime::kRegExpExecRT, 4, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002021#else // V8_INTERPRETED_REGEXP
Ben Murdoch257744e2011-11-30 15:57:28 +00002022
2023 // Stack frame on entry.
2024 // sp[0]: last_match_info (expected JSArray)
2025 // sp[4]: previous index
2026 // sp[8]: subject string
2027 // sp[12]: JSRegExp object
2028
Ben Murdochdb1b4382012-04-26 19:03:50 +01002029 const int kLastMatchInfoOffset = 0 * kPointerSize;
2030 const int kPreviousIndexOffset = 1 * kPointerSize;
2031 const int kSubjectOffset = 2 * kPointerSize;
2032 const int kJSRegExpOffset = 3 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00002033
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002034 Label runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00002035 // Allocation of registers for this function. These are in callee save
2036 // registers and will be preserved by the call to the native RegExp code, as
2037 // this code is called using the normal C calling convention. When calling
2038 // directly from generated code the native RegExp code will not do a GC and
2039 // therefore the content of these registers are safe to use after the call.
2040 // MIPS - using s0..s2, since we are not using CEntry Stub.
2041 Register subject = s0;
2042 Register regexp_data = s1;
2043 Register last_match_info_elements = s2;
2044
2045 // Ensure that a RegExp stack is allocated.
2046 ExternalReference address_of_regexp_stack_memory_address =
2047 ExternalReference::address_of_regexp_stack_memory_address(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002048 isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00002049 ExternalReference address_of_regexp_stack_memory_size =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002050 ExternalReference::address_of_regexp_stack_memory_size(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00002051 __ li(a0, Operand(address_of_regexp_stack_memory_size));
2052 __ lw(a0, MemOperand(a0, 0));
2053 __ Branch(&runtime, eq, a0, Operand(zero_reg));
2054
2055 // Check that the first argument is a JSRegExp object.
2056 __ lw(a0, MemOperand(sp, kJSRegExpOffset));
2057 STATIC_ASSERT(kSmiTag == 0);
2058 __ JumpIfSmi(a0, &runtime);
2059 __ GetObjectType(a0, a1, a1);
2060 __ Branch(&runtime, ne, a1, Operand(JS_REGEXP_TYPE));
2061
2062 // Check that the RegExp has been compiled (data contains a fixed array).
2063 __ lw(regexp_data, FieldMemOperand(a0, JSRegExp::kDataOffset));
2064 if (FLAG_debug_code) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002065 __ SmiTst(regexp_data, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002066 __ Check(nz,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002067 kUnexpectedTypeForRegExpDataFixedArrayExpected,
Ben Murdoch257744e2011-11-30 15:57:28 +00002068 t0,
2069 Operand(zero_reg));
2070 __ GetObjectType(regexp_data, a0, a0);
2071 __ Check(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002072 kUnexpectedTypeForRegExpDataFixedArrayExpected,
Ben Murdoch257744e2011-11-30 15:57:28 +00002073 a0,
2074 Operand(FIXED_ARRAY_TYPE));
2075 }
2076
2077 // regexp_data: RegExp data (FixedArray)
2078 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
2079 __ lw(a0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
2080 __ Branch(&runtime, ne, a0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
2081
2082 // regexp_data: RegExp data (FixedArray)
2083 // Check that the number of captures fit in the static offsets vector buffer.
2084 __ lw(a2,
2085 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002086 // Check (number_of_captures + 1) * 2 <= offsets vector size
2087 // Or number_of_captures * 2 <= offsets vector size - 2
2088 // Multiplying by 2 comes for free since a2 is smi-tagged.
Ben Murdoch257744e2011-11-30 15:57:28 +00002089 STATIC_ASSERT(kSmiTag == 0);
2090 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002091 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
2092 __ Branch(
2093 &runtime, hi, a2, Operand(Isolate::kJSRegexpStaticOffsetsVectorSize - 2));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002094
2095 // Reset offset for possibly sliced string.
2096 __ mov(t0, zero_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002097 __ lw(subject, MemOperand(sp, kSubjectOffset));
2098 __ JumpIfSmi(subject, &runtime);
2099 __ mov(a3, subject); // Make a copy of the original subject string.
Ben Murdoch257744e2011-11-30 15:57:28 +00002100 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
2101 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002102 // subject: subject string
2103 // a3: subject string
2104 // a0: subject string instance type
2105 // regexp_data: RegExp data (FixedArray)
2106 // Handle subject string according to its encoding and representation:
2107 // (1) Sequential string? If yes, go to (5).
2108 // (2) Anything but sequential or cons? If yes, go to (6).
2109 // (3) Cons string. If the string is flat, replace subject with first string.
2110 // Otherwise bailout.
2111 // (4) Is subject external? If yes, go to (7).
2112 // (5) Sequential string. Load regexp code according to encoding.
2113 // (E) Carry on.
2114 /// [...]
2115
2116 // Deferred code at the end of the stub:
2117 // (6) Not a long external string? If yes, go to (8).
2118 // (7) External string. Make it, offset-wise, look like a sequential string.
2119 // Go to (5).
2120 // (8) Short external string or not a string? If yes, bail out to runtime.
2121 // (9) Sliced string. Replace subject with parent. Go to (4).
2122
2123 Label seq_string /* 5 */, external_string /* 7 */,
2124 check_underlying /* 4 */, not_seq_nor_cons /* 6 */,
2125 not_long_external /* 8 */;
2126
2127 // (1) Sequential string? If yes, go to (5).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002128 __ And(a1,
2129 a0,
2130 Operand(kIsNotStringMask |
2131 kStringRepresentationMask |
2132 kShortExternalStringMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00002133 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002134 __ Branch(&seq_string, eq, a1, Operand(zero_reg)); // Go to (5).
Ben Murdoch257744e2011-11-30 15:57:28 +00002135
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002136 // (2) Anything but sequential or cons? If yes, go to (6).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002137 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
2138 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002139 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
2140 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002141 // Go to (6).
2142 __ Branch(&not_seq_nor_cons, ge, a1, Operand(kExternalStringTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002143
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002144 // (3) Cons string. Check that it's flat.
2145 // Replace subject with first string and reload instance type.
Ben Murdoch257744e2011-11-30 15:57:28 +00002146 __ lw(a0, FieldMemOperand(subject, ConsString::kSecondOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002147 __ LoadRoot(a1, Heap::kempty_stringRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002148 __ Branch(&runtime, ne, a0, Operand(a1));
2149 __ lw(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002150
2151 // (4) Is subject external? If yes, go to (7).
2152 __ bind(&check_underlying);
Ben Murdoch257744e2011-11-30 15:57:28 +00002153 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
2154 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002155 STATIC_ASSERT(kSeqStringTag == 0);
2156 __ And(at, a0, Operand(kStringRepresentationMask));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002157 // The underlying external string is never a short external string.
2158 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
2159 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
2160 __ Branch(&external_string, ne, at, Operand(zero_reg)); // Go to (7).
Ben Murdoch257744e2011-11-30 15:57:28 +00002161
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002162 // (5) Sequential string. Load regexp code according to encoding.
Ben Murdoch257744e2011-11-30 15:57:28 +00002163 __ bind(&seq_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002164 // subject: sequential subject string (or look-alike, external string)
2165 // a3: original subject string
2166 // Load previous index and check range before a3 is overwritten. We have to
2167 // use a3 instead of subject here because subject might have been only made
2168 // to look like a sequential string when it actually is an external string.
2169 __ lw(a1, MemOperand(sp, kPreviousIndexOffset));
2170 __ JumpIfNotSmi(a1, &runtime);
2171 __ lw(a3, FieldMemOperand(a3, String::kLengthOffset));
2172 __ Branch(&runtime, ls, a3, Operand(a1));
2173 __ sra(a1, a1, kSmiTagSize); // Untag the Smi.
2174
Ben Murdoch257744e2011-11-30 15:57:28 +00002175 STATIC_ASSERT(kStringEncodingMask == 4);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002176 STATIC_ASSERT(kOneByteStringTag == 4);
Ben Murdoch257744e2011-11-30 15:57:28 +00002177 STATIC_ASSERT(kTwoByteStringTag == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002178 __ And(a0, a0, Operand(kStringEncodingMask)); // Non-zero for one-byte.
2179 __ lw(t9, FieldMemOperand(regexp_data, JSRegExp::kDataOneByteCodeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002180 __ sra(a3, a0, 2); // a3 is 1 for ASCII, 0 for UC16 (used below).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002181 __ lw(t1, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002182 __ Movz(t9, t1, a0); // If UC16 (a0 is 0), replace t9 w/kDataUC16CodeOffset.
Ben Murdoch257744e2011-11-30 15:57:28 +00002183
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002184 // (E) Carry on. String handling is done.
2185 // t9: irregexp code
Ben Murdoch257744e2011-11-30 15:57:28 +00002186 // Check that the irregexp code has been generated for the actual string
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002187 // encoding. If it has, the field contains a code object otherwise it contains
2188 // a smi (code flushing support).
2189 __ JumpIfSmi(t9, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00002190
Ben Murdoch257744e2011-11-30 15:57:28 +00002191 // a1: previous index
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002192 // a3: encoding of subject string (1 if one_byte, 0 if two_byte);
Ben Murdoch257744e2011-11-30 15:57:28 +00002193 // t9: code
2194 // subject: Subject string
2195 // regexp_data: RegExp data (FixedArray)
2196 // All checks done. Now push arguments for native regexp code.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002197 __ IncrementCounter(isolate()->counters()->regexp_entry_native(),
Ben Murdoch257744e2011-11-30 15:57:28 +00002198 1, a0, a2);
2199
2200 // Isolates: note we add an additional parameter here (isolate pointer).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002201 const int kRegExpExecuteArguments = 9;
Ben Murdochdb1b4382012-04-26 19:03:50 +01002202 const int kParameterRegisters = 4;
Ben Murdoch257744e2011-11-30 15:57:28 +00002203 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
2204
2205 // Stack pointer now points to cell where return address is to be written.
2206 // Arguments are before that on the stack or in registers, meaning we
2207 // treat the return address as argument 5. Thus every argument after that
2208 // needs to be shifted back by 1. Since DirectCEntryStub will handle
2209 // allocating space for the c argument slots, we don't need to calculate
2210 // that into the argument positions on the stack. This is how the stack will
2211 // look (sp meaning the value of sp at this moment):
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002212 // [sp + 5] - Argument 9
Ben Murdoch257744e2011-11-30 15:57:28 +00002213 // [sp + 4] - Argument 8
2214 // [sp + 3] - Argument 7
2215 // [sp + 2] - Argument 6
2216 // [sp + 1] - Argument 5
2217 // [sp + 0] - saved ra
2218
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002219 // Argument 9: Pass current isolate address.
Ben Murdoch257744e2011-11-30 15:57:28 +00002220 // CFunctionArgumentOperand handles MIPS stack argument slots.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002221 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
2222 __ sw(a0, MemOperand(sp, 5 * kPointerSize));
2223
2224 // Argument 8: Indicate that this is a direct call from JavaScript.
2225 __ li(a0, Operand(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00002226 __ sw(a0, MemOperand(sp, 4 * kPointerSize));
2227
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002228 // Argument 7: Start (high end) of backtracking stack memory area.
Ben Murdoch257744e2011-11-30 15:57:28 +00002229 __ li(a0, Operand(address_of_regexp_stack_memory_address));
2230 __ lw(a0, MemOperand(a0, 0));
2231 __ li(a2, Operand(address_of_regexp_stack_memory_size));
2232 __ lw(a2, MemOperand(a2, 0));
2233 __ addu(a0, a0, a2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002234 __ sw(a0, MemOperand(sp, 3 * kPointerSize));
2235
2236 // Argument 6: Set the number of capture registers to zero to force global
2237 // regexps to behave as non-global. This does not affect non-global regexps.
2238 __ mov(a0, zero_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00002239 __ sw(a0, MemOperand(sp, 2 * kPointerSize));
2240
2241 // Argument 5: static offsets vector buffer.
2242 __ li(a0, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002243 ExternalReference::address_of_static_offsets_vector(isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00002244 __ sw(a0, MemOperand(sp, 1 * kPointerSize));
2245
2246 // For arguments 4 and 3 get string length, calculate start of string data
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002247 // calculate the shift of the index (0 for one-byte and 1 for two-byte).
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002248 __ Addu(t2, subject, Operand(SeqString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00002249 __ Xor(a3, a3, Operand(1)); // 1 for 2-byte str, 0 for 1-byte.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002250 // Load the length from the original subject string from the previous stack
2251 // frame. Therefore we have to use fp, which points exactly to two pointer
2252 // sizes below the previous sp. (Because creating a new stack frame pushes
2253 // the previous fp onto the stack and moves up sp by 2 * kPointerSize.)
Ben Murdoch589d6972011-11-30 16:04:58 +00002254 __ lw(subject, MemOperand(fp, kSubjectOffset + 2 * kPointerSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002255 // If slice offset is not 0, load the length from the original sliced string.
2256 // Argument 4, a3: End of string data
2257 // Argument 3, a2: Start of string data
2258 // Prepare start and end index of the input.
2259 __ sllv(t1, t0, a3);
2260 __ addu(t0, t2, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002261 __ sllv(t1, a1, a3);
2262 __ addu(a2, t0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002263
Ben Murdoch589d6972011-11-30 16:04:58 +00002264 __ lw(t2, FieldMemOperand(subject, String::kLengthOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002265 __ sra(t2, t2, kSmiTagSize);
2266 __ sllv(t1, t2, a3);
2267 __ addu(a3, t0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002268 // Argument 2 (a1): Previous index.
2269 // Already there
2270
2271 // Argument 1 (a0): Subject string.
Ben Murdoch589d6972011-11-30 16:04:58 +00002272 __ mov(a0, subject);
Ben Murdoch257744e2011-11-30 15:57:28 +00002273
2274 // Locate the code entry and call it.
2275 __ Addu(t9, t9, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002276 DirectCEntryStub stub(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00002277 stub.GenerateCall(masm, t9);
2278
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002279 __ LeaveExitFrame(false, no_reg, true);
Ben Murdoch257744e2011-11-30 15:57:28 +00002280
2281 // v0: result
2282 // subject: subject string (callee saved)
2283 // regexp_data: RegExp data (callee saved)
2284 // last_match_info_elements: Last match info elements (callee saved)
Ben Murdoch257744e2011-11-30 15:57:28 +00002285 // Check the result.
Ben Murdoch257744e2011-11-30 15:57:28 +00002286 Label success;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002287 __ Branch(&success, eq, v0, Operand(1));
2288 // We expect exactly one result since we force the called regexp to behave
2289 // as non-global.
Ben Murdoch257744e2011-11-30 15:57:28 +00002290 Label failure;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002291 __ Branch(&failure, eq, v0, Operand(NativeRegExpMacroAssembler::FAILURE));
Ben Murdoch257744e2011-11-30 15:57:28 +00002292 // If not exception it can only be retry. Handle that in the runtime system.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002293 __ Branch(&runtime, ne, v0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
Ben Murdoch257744e2011-11-30 15:57:28 +00002294 // Result must now be exception. If there is no pending exception already a
2295 // stack overflow (on the backtrack stack) was detected in RegExp code but
2296 // haven't created the exception yet. Handle that in the runtime system.
2297 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002298 __ li(a1, Operand(isolate()->factory()->the_hole_value()));
Ben Murdoch589d6972011-11-30 16:04:58 +00002299 __ li(a2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002300 isolate())));
Ben Murdoch257744e2011-11-30 15:57:28 +00002301 __ lw(v0, MemOperand(a2, 0));
Ben Murdoch589d6972011-11-30 16:04:58 +00002302 __ Branch(&runtime, eq, v0, Operand(a1));
Ben Murdoch257744e2011-11-30 15:57:28 +00002303
2304 __ sw(a1, MemOperand(a2, 0)); // Clear pending exception.
2305
2306 // Check if the exception is a termination. If so, throw as uncatchable.
2307 __ LoadRoot(a0, Heap::kTerminationExceptionRootIndex);
2308 Label termination_exception;
Ben Murdoch589d6972011-11-30 16:04:58 +00002309 __ Branch(&termination_exception, eq, v0, Operand(a0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002310
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002311 __ Throw(v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002312
2313 __ bind(&termination_exception);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002314 __ ThrowUncatchable(v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002315
2316 __ bind(&failure);
2317 // For failure and exception return null.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002318 __ li(v0, Operand(isolate()->factory()->null_value()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002319 __ DropAndRet(4);
Ben Murdoch257744e2011-11-30 15:57:28 +00002320
2321 // Process the result from the native regexp code.
2322 __ bind(&success);
2323 __ lw(a1,
2324 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
2325 // Calculate number of capture registers (number_of_captures + 1) * 2.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002326 // Multiplying by 2 comes for free since r1 is smi-tagged.
Ben Murdoch257744e2011-11-30 15:57:28 +00002327 STATIC_ASSERT(kSmiTag == 0);
2328 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
2329 __ Addu(a1, a1, Operand(2)); // a1 was a smi.
2330
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002331 __ lw(a0, MemOperand(sp, kLastMatchInfoOffset));
2332 __ JumpIfSmi(a0, &runtime);
2333 __ GetObjectType(a0, a2, a2);
2334 __ Branch(&runtime, ne, a2, Operand(JS_ARRAY_TYPE));
2335 // Check that the JSArray is in fast case.
2336 __ lw(last_match_info_elements,
2337 FieldMemOperand(a0, JSArray::kElementsOffset));
2338 __ lw(a0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
2339 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
2340 __ Branch(&runtime, ne, a0, Operand(at));
2341 // Check that the last match info has space for the capture registers and the
2342 // additional information.
2343 __ lw(a0,
2344 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
2345 __ Addu(a2, a1, Operand(RegExpImpl::kLastMatchOverhead));
2346 __ sra(at, a0, kSmiTagSize);
2347 __ Branch(&runtime, gt, a2, Operand(at));
2348
Ben Murdoch257744e2011-11-30 15:57:28 +00002349 // a1: number of capture registers
2350 // subject: subject string
2351 // Store the capture count.
2352 __ sll(a2, a1, kSmiTagSize + kSmiShiftSize); // To smi.
2353 __ sw(a2, FieldMemOperand(last_match_info_elements,
2354 RegExpImpl::kLastCaptureCountOffset));
2355 // Store last subject and last input.
Ben Murdoch257744e2011-11-30 15:57:28 +00002356 __ sw(subject,
2357 FieldMemOperand(last_match_info_elements,
2358 RegExpImpl::kLastSubjectOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002359 __ mov(a2, subject);
2360 __ RecordWriteField(last_match_info_elements,
2361 RegExpImpl::kLastSubjectOffset,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002362 subject,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002363 t3,
2364 kRAHasNotBeenSaved,
2365 kDontSaveFPRegs);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002366 __ mov(subject, a2);
Ben Murdoch257744e2011-11-30 15:57:28 +00002367 __ sw(subject,
2368 FieldMemOperand(last_match_info_elements,
2369 RegExpImpl::kLastInputOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002370 __ RecordWriteField(last_match_info_elements,
2371 RegExpImpl::kLastInputOffset,
2372 subject,
2373 t3,
2374 kRAHasNotBeenSaved,
2375 kDontSaveFPRegs);
Ben Murdoch257744e2011-11-30 15:57:28 +00002376
2377 // Get the static offsets vector filled by the native regexp code.
2378 ExternalReference address_of_static_offsets_vector =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002379 ExternalReference::address_of_static_offsets_vector(isolate());
Ben Murdoch257744e2011-11-30 15:57:28 +00002380 __ li(a2, Operand(address_of_static_offsets_vector));
2381
2382 // a1: number of capture registers
2383 // a2: offsets vector
2384 Label next_capture, done;
2385 // Capture register counter starts from number of capture registers and
2386 // counts down until wrapping after zero.
2387 __ Addu(a0,
2388 last_match_info_elements,
2389 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
2390 __ bind(&next_capture);
2391 __ Subu(a1, a1, Operand(1));
2392 __ Branch(&done, lt, a1, Operand(zero_reg));
2393 // Read the value from the static offsets vector buffer.
2394 __ lw(a3, MemOperand(a2, 0));
2395 __ addiu(a2, a2, kPointerSize);
2396 // Store the smi value in the last match info.
2397 __ sll(a3, a3, kSmiTagSize); // Convert to Smi.
2398 __ sw(a3, MemOperand(a0, 0));
2399 __ Branch(&next_capture, USE_DELAY_SLOT);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002400 __ addiu(a0, a0, kPointerSize); // In branch delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00002401
2402 __ bind(&done);
2403
2404 // Return last match info.
2405 __ lw(v0, MemOperand(sp, kLastMatchInfoOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002406 __ DropAndRet(4);
2407
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002408 // Do the runtime call to execute the regexp.
2409 __ bind(&runtime);
2410 __ TailCallRuntime(Runtime::kRegExpExecRT, 4, 1);
2411
2412 // Deferred code for string handling.
2413 // (6) Not a long external string? If yes, go to (8).
2414 __ bind(&not_seq_nor_cons);
2415 // Go to (8).
2416 __ Branch(&not_long_external, gt, a1, Operand(kExternalStringTag));
2417
2418 // (7) External string. Make it, offset-wise, look like a sequential string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002419 __ bind(&external_string);
2420 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
2421 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
2422 if (FLAG_debug_code) {
2423 // Assert that we do not have a cons or slice (indirect strings) here.
2424 // Sequential strings have already been ruled out.
2425 __ And(at, a0, Operand(kIsIndirectStringMask));
2426 __ Assert(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002427 kExternalStringExpectedButNotFound,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002428 at,
2429 Operand(zero_reg));
2430 }
2431 __ lw(subject,
2432 FieldMemOperand(subject, ExternalString::kResourceDataOffset));
2433 // Move the pointer so that offset-wise, it looks like a sequential string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002434 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002435 __ Subu(subject,
2436 subject,
2437 SeqTwoByteString::kHeaderSize - kHeapObjectTag);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002438 __ jmp(&seq_string); // Go to (5).
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002439
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002440 // (8) Short external string or not a string? If yes, bail out to runtime.
2441 __ bind(&not_long_external);
2442 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
2443 __ And(at, a1, Operand(kIsNotStringMask | kShortExternalStringMask));
2444 __ Branch(&runtime, ne, at, Operand(zero_reg));
2445
2446 // (9) Sliced string. Replace subject with parent. Go to (4).
2447 // Load offset into t0 and replace subject string with parent.
2448 __ lw(t0, FieldMemOperand(subject, SlicedString::kOffsetOffset));
2449 __ sra(t0, t0, kSmiTagSize);
2450 __ lw(subject, FieldMemOperand(subject, SlicedString::kParentOffset));
2451 __ jmp(&check_underlying); // Go to (4).
Ben Murdoch257744e2011-11-30 15:57:28 +00002452#endif // V8_INTERPRETED_REGEXP
Steve Block44f0eee2011-05-26 01:26:41 +01002453}
2454
2455
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002456static void GenerateRecordCallTarget(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002457 // Cache the called function in a feedback vector slot. Cache states
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002458 // are uninitialized, monomorphic (indicated by a JSFunction), and
2459 // megamorphic.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002460 // a0 : number of arguments to the construct function
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002461 // a1 : the function to call
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002462 // a2 : Feedback vector
2463 // a3 : slot in feedback vector (Smi)
2464 Label initialize, done, miss, megamorphic, not_array_function;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002465
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002466 DCHECK_EQ(*TypeFeedbackVector::MegamorphicSentinel(masm->isolate()),
2467 masm->isolate()->heap()->megamorphic_symbol());
2468 DCHECK_EQ(*TypeFeedbackVector::UninitializedSentinel(masm->isolate()),
2469 masm->isolate()->heap()->uninitialized_symbol());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002470
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002471 // Load the cache state into t0.
2472 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
2473 __ Addu(t0, a2, Operand(t0));
2474 __ lw(t0, FieldMemOperand(t0, FixedArray::kHeaderSize));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002475
2476 // A monomorphic cache hit or an already megamorphic state: invoke the
2477 // function without changing the state.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002478 __ Branch(&done, eq, t0, Operand(a1));
2479
2480 if (!FLAG_pretenuring_call_new) {
2481 // If we came here, we need to see if we are the array function.
2482 // If we didn't have a matching function, and we didn't find the megamorph
2483 // sentinel, then we have in the slot either some other function or an
2484 // AllocationSite. Do a map check on the object in a3.
2485 __ lw(t1, FieldMemOperand(t0, 0));
2486 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2487 __ Branch(&miss, ne, t1, Operand(at));
2488
2489 // Make sure the function is the Array() function
2490 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, t0);
2491 __ Branch(&megamorphic, ne, a1, Operand(t0));
2492 __ jmp(&done);
2493 }
2494
2495 __ bind(&miss);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002496
2497 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
2498 // megamorphic.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002499 __ LoadRoot(at, Heap::kuninitialized_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002500 __ Branch(&initialize, eq, t0, Operand(at));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002501 // MegamorphicSentinel is an immortal immovable object (undefined) so no
2502 // write-barrier is needed.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002503 __ bind(&megamorphic);
2504 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
2505 __ Addu(t0, a2, Operand(t0));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002506 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002507 __ sw(at, FieldMemOperand(t0, FixedArray::kHeaderSize));
2508 __ jmp(&done);
2509
2510 // An uninitialized cache is patched with the function.
2511 __ bind(&initialize);
2512 if (!FLAG_pretenuring_call_new) {
2513 // Make sure the function is the Array() function.
2514 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, t0);
2515 __ Branch(&not_array_function, ne, a1, Operand(t0));
2516
2517 // The target function is the Array constructor,
2518 // Create an AllocationSite if we don't already have it, store it in the
2519 // slot.
2520 {
2521 FrameScope scope(masm, StackFrame::INTERNAL);
2522 const RegList kSavedRegs =
2523 1 << 4 | // a0
2524 1 << 5 | // a1
2525 1 << 6 | // a2
2526 1 << 7; // a3
2527
2528 // Arguments register must be smi-tagged to call out.
2529 __ SmiTag(a0);
2530 __ MultiPush(kSavedRegs);
2531
2532 CreateAllocationSiteStub create_stub(masm->isolate());
2533 __ CallStub(&create_stub);
2534
2535 __ MultiPop(kSavedRegs);
2536 __ SmiUntag(a0);
2537 }
2538 __ Branch(&done);
2539
2540 __ bind(&not_array_function);
2541 }
2542
2543 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
2544 __ Addu(t0, a2, Operand(t0));
2545 __ Addu(t0, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2546 __ sw(a1, MemOperand(t0, 0));
2547
2548 __ Push(t0, a2, a1);
2549 __ RecordWrite(a2, t0, a1, kRAHasNotBeenSaved, kDontSaveFPRegs,
2550 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2551 __ Pop(t0, a2, a1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002552
2553 __ bind(&done);
2554}
2555
2556
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002557static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
2558 __ lw(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2559 __ lw(t0, FieldMemOperand(a3, SharedFunctionInfo::kCompilerHintsOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002560
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002561 // Do not transform the receiver for strict mode functions.
2562 int32_t strict_mode_function_mask =
2563 1 << (SharedFunctionInfo::kStrictModeFunction + kSmiTagSize);
2564 // Do not transform the receiver for native (Compilerhints already in a3).
2565 int32_t native_mask = 1 << (SharedFunctionInfo::kNative + kSmiTagSize);
2566 __ And(at, t0, Operand(strict_mode_function_mask | native_mask));
2567 __ Branch(cont, ne, at, Operand(zero_reg));
2568}
Ben Murdoch257744e2011-11-30 15:57:28 +00002569
Ben Murdoch257744e2011-11-30 15:57:28 +00002570
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002571static void EmitSlowCase(MacroAssembler* masm,
2572 int argc,
2573 Label* non_function) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002574 // Check for function proxy.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002575 __ Branch(non_function, ne, t0, Operand(JS_FUNCTION_PROXY_TYPE));
2576 __ push(a1); // put proxy as additional argument
2577 __ li(a0, Operand(argc + 1, RelocInfo::NONE32));
2578 __ mov(a2, zero_reg);
2579 __ GetBuiltinFunction(a1, Builtins::CALL_FUNCTION_PROXY);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002580 {
2581 Handle<Code> adaptor =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002582 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002583 __ Jump(adaptor, RelocInfo::CODE_TARGET);
2584 }
2585
Ben Murdoch257744e2011-11-30 15:57:28 +00002586 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
2587 // of the original receiver from the call site).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002588 __ bind(non_function);
2589 __ sw(a1, MemOperand(sp, argc * kPointerSize));
2590 __ li(a0, Operand(argc)); // Set up the number of arguments.
Ben Murdoch257744e2011-11-30 15:57:28 +00002591 __ mov(a2, zero_reg);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002592 __ GetBuiltinFunction(a1, Builtins::CALL_NON_FUNCTION);
Ben Murdoch257744e2011-11-30 15:57:28 +00002593 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
2594 RelocInfo::CODE_TARGET);
Steve Block44f0eee2011-05-26 01:26:41 +01002595}
2596
2597
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002598static void EmitWrapCase(MacroAssembler* masm, int argc, Label* cont) {
2599 // Wrap the receiver and patch it back onto the stack.
2600 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
2601 __ Push(a1, a3);
2602 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
2603 __ pop(a1);
2604 }
2605 __ Branch(USE_DELAY_SLOT, cont);
2606 __ sw(v0, MemOperand(sp, argc * kPointerSize));
2607}
2608
2609
2610static void CallFunctionNoFeedback(MacroAssembler* masm,
2611 int argc, bool needs_checks,
2612 bool call_as_method) {
2613 // a1 : the function to call
2614 Label slow, non_function, wrap, cont;
2615
2616 if (needs_checks) {
2617 // Check that the function is really a JavaScript function.
2618 // a1: pushed function (to be verified)
2619 __ JumpIfSmi(a1, &non_function);
2620
2621 // Goto slow case if we do not have a function.
2622 __ GetObjectType(a1, t0, t0);
2623 __ Branch(&slow, ne, t0, Operand(JS_FUNCTION_TYPE));
2624 }
2625
2626 // Fast-case: Invoke the function now.
2627 // a1: pushed function
2628 ParameterCount actual(argc);
2629
2630 if (call_as_method) {
2631 if (needs_checks) {
2632 EmitContinueIfStrictOrNative(masm, &cont);
2633 }
2634
2635 // Compute the receiver in sloppy mode.
2636 __ lw(a3, MemOperand(sp, argc * kPointerSize));
2637
2638 if (needs_checks) {
2639 __ JumpIfSmi(a3, &wrap);
2640 __ GetObjectType(a3, t0, t0);
2641 __ Branch(&wrap, lt, t0, Operand(FIRST_SPEC_OBJECT_TYPE));
2642 } else {
2643 __ jmp(&wrap);
2644 }
2645
2646 __ bind(&cont);
2647 }
2648
2649 __ InvokeFunction(a1, actual, JUMP_FUNCTION, NullCallWrapper());
2650
2651 if (needs_checks) {
2652 // Slow-case: Non-function called.
2653 __ bind(&slow);
2654 EmitSlowCase(masm, argc, &non_function);
2655 }
2656
2657 if (call_as_method) {
2658 __ bind(&wrap);
2659 // Wrap the receiver and patch it back onto the stack.
2660 EmitWrapCase(masm, argc, &cont);
2661 }
2662}
2663
2664
2665void CallFunctionStub::Generate(MacroAssembler* masm) {
2666 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
2667}
2668
2669
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002670void CallConstructStub::Generate(MacroAssembler* masm) {
2671 // a0 : number of arguments
2672 // a1 : the function to call
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002673 // a2 : feedback vector
2674 // a3 : (only if a2 is not undefined) slot in feedback vector (Smi)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002675 Label slow, non_function_call;
2676
2677 // Check that the function is not a smi.
2678 __ JumpIfSmi(a1, &non_function_call);
2679 // Check that the function is a JSFunction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002680 __ GetObjectType(a1, t0, t0);
2681 __ Branch(&slow, ne, t0, Operand(JS_FUNCTION_TYPE));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002682
2683 if (RecordCallTarget()) {
2684 GenerateRecordCallTarget(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002685
2686 __ sll(at, a3, kPointerSizeLog2 - kSmiTagSize);
2687 __ Addu(t1, a2, at);
2688 if (FLAG_pretenuring_call_new) {
2689 // Put the AllocationSite from the feedback vector into a2.
2690 // By adding kPointerSize we encode that we know the AllocationSite
2691 // entry is at the feedback vector slot given by a3 + 1.
2692 __ lw(a2, FieldMemOperand(t1, FixedArray::kHeaderSize + kPointerSize));
2693 } else {
2694 Label feedback_register_initialized;
2695 // Put the AllocationSite from the feedback vector into a2, or undefined.
2696 __ lw(a2, FieldMemOperand(t1, FixedArray::kHeaderSize));
2697 __ lw(t1, FieldMemOperand(a2, AllocationSite::kMapOffset));
2698 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2699 __ Branch(&feedback_register_initialized, eq, t1, Operand(at));
2700 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2701 __ bind(&feedback_register_initialized);
2702 }
2703
2704 __ AssertUndefinedOrAllocationSite(a2, t1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002705 }
2706
2707 // Jump to the function-specific construct stub.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002708 Register jmp_reg = t0;
2709 __ lw(jmp_reg, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2710 __ lw(jmp_reg, FieldMemOperand(jmp_reg,
2711 SharedFunctionInfo::kConstructStubOffset));
2712 __ Addu(at, jmp_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002713 __ Jump(at);
2714
2715 // a0: number of arguments
2716 // a1: called object
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002717 // t0: object type
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002718 Label do_call;
2719 __ bind(&slow);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002720 __ Branch(&non_function_call, ne, t0, Operand(JS_FUNCTION_PROXY_TYPE));
2721 __ GetBuiltinFunction(a1, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002722 __ jmp(&do_call);
2723
2724 __ bind(&non_function_call);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002725 __ GetBuiltinFunction(a1, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002726 __ bind(&do_call);
2727 // Set expected number of arguments to zero (not changing r0).
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002728 __ li(a2, Operand(0, RelocInfo::NONE32));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002729 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002730 RelocInfo::CODE_TARGET);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002731}
2732
2733
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002734static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
2735 __ lw(vector, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
2736 __ lw(vector, FieldMemOperand(vector,
2737 JSFunction::kSharedFunctionInfoOffset));
2738 __ lw(vector, FieldMemOperand(vector,
2739 SharedFunctionInfo::kFeedbackVectorOffset));
2740}
2741
2742
2743void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
2744 // a1 - function
2745 // a3 - slot id
2746 Label miss;
2747
2748 EmitLoadTypeFeedbackVector(masm, a2);
2749
2750 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, at);
2751 __ Branch(&miss, ne, a1, Operand(at));
2752
2753 __ li(a0, Operand(arg_count()));
2754 __ sll(at, a3, kPointerSizeLog2 - kSmiTagSize);
2755 __ Addu(at, a2, Operand(at));
2756 __ lw(t0, FieldMemOperand(at, FixedArray::kHeaderSize));
2757
2758 // Verify that t0 contains an AllocationSite
2759 __ lw(t1, FieldMemOperand(t0, HeapObject::kMapOffset));
2760 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2761 __ Branch(&miss, ne, t1, Operand(at));
2762
2763 __ mov(a2, t0);
2764 ArrayConstructorStub stub(masm->isolate(), arg_count());
2765 __ TailCallStub(&stub);
2766
2767 __ bind(&miss);
2768 GenerateMiss(masm);
2769
2770 // The slow case, we need this no matter what to complete a call after a miss.
2771 CallFunctionNoFeedback(masm,
2772 arg_count(),
2773 true,
2774 CallAsMethod());
2775
2776 // Unreachable.
2777 __ stop("Unexpected code address");
2778}
2779
2780
2781void CallICStub::Generate(MacroAssembler* masm) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002782 // a1 - function
2783 // a3 - slot id (Smi)
2784 const int with_types_offset =
2785 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kWithTypesIndex);
2786 const int generic_offset =
2787 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kGenericCountIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002788 Label extra_checks_or_miss, slow_start;
2789 Label slow, non_function, wrap, cont;
2790 Label have_js_function;
2791 int argc = arg_count();
2792 ParameterCount actual(argc);
2793
2794 EmitLoadTypeFeedbackVector(masm, a2);
2795
2796 // The checks. First, does r1 match the recorded monomorphic target?
2797 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
2798 __ Addu(t0, a2, Operand(t0));
2799 __ lw(t0, FieldMemOperand(t0, FixedArray::kHeaderSize));
2800 __ Branch(&extra_checks_or_miss, ne, a1, Operand(t0));
2801
2802 __ bind(&have_js_function);
2803 if (CallAsMethod()) {
2804 EmitContinueIfStrictOrNative(masm, &cont);
2805 // Compute the receiver in sloppy mode.
2806 __ lw(a3, MemOperand(sp, argc * kPointerSize));
2807
2808 __ JumpIfSmi(a3, &wrap);
2809 __ GetObjectType(a3, t0, t0);
2810 __ Branch(&wrap, lt, t0, Operand(FIRST_SPEC_OBJECT_TYPE));
2811
2812 __ bind(&cont);
Ben Murdoch257744e2011-11-30 15:57:28 +00002813 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002814
2815 __ InvokeFunction(a1, actual, JUMP_FUNCTION, NullCallWrapper());
2816
2817 __ bind(&slow);
2818 EmitSlowCase(masm, argc, &non_function);
2819
2820 if (CallAsMethod()) {
2821 __ bind(&wrap);
2822 EmitWrapCase(masm, argc, &cont);
2823 }
2824
2825 __ bind(&extra_checks_or_miss);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002826 Label uninitialized, miss;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002827
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002828 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002829 __ Branch(&slow_start, eq, t0, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002830
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002831 // The following cases attempt to handle MISS cases without going to the
2832 // runtime.
2833 if (FLAG_trace_ic) {
2834 __ Branch(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002835 }
2836
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002837 __ LoadRoot(at, Heap::kuninitialized_symbolRootIndex);
2838 __ Branch(&uninitialized, eq, t0, Operand(at));
2839
2840 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2841 // to handle it here. More complex cases are dealt with in the runtime.
2842 __ AssertNotSmi(t0);
2843 __ GetObjectType(t0, t1, t1);
2844 __ Branch(&miss, ne, t1, Operand(JS_FUNCTION_TYPE));
2845 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
2846 __ Addu(t0, a2, Operand(t0));
2847 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
2848 __ sw(at, FieldMemOperand(t0, FixedArray::kHeaderSize));
2849 // We have to update statistics for runtime profiling.
2850 __ lw(t0, FieldMemOperand(a2, with_types_offset));
2851 __ Subu(t0, t0, Operand(Smi::FromInt(1)));
2852 __ sw(t0, FieldMemOperand(a2, with_types_offset));
2853 __ lw(t0, FieldMemOperand(a2, generic_offset));
2854 __ Addu(t0, t0, Operand(Smi::FromInt(1)));
2855 __ Branch(USE_DELAY_SLOT, &slow_start);
2856 __ sw(t0, FieldMemOperand(a2, generic_offset)); // In delay slot.
2857
2858 __ bind(&uninitialized);
2859
2860 // We are going monomorphic, provided we actually have a JSFunction.
2861 __ JumpIfSmi(a1, &miss);
2862
2863 // Goto miss case if we do not have a function.
2864 __ GetObjectType(a1, t0, t0);
2865 __ Branch(&miss, ne, t0, Operand(JS_FUNCTION_TYPE));
2866
2867 // Make sure the function is not the Array() function, which requires special
2868 // behavior on MISS.
2869 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, t0);
2870 __ Branch(&miss, eq, a1, Operand(t0));
2871
2872 // Update stats.
2873 __ lw(t0, FieldMemOperand(a2, with_types_offset));
2874 __ Addu(t0, t0, Operand(Smi::FromInt(1)));
2875 __ sw(t0, FieldMemOperand(a2, with_types_offset));
2876
2877 // Store the function.
2878 __ sll(t0, a3, kPointerSizeLog2 - kSmiTagSize);
2879 __ Addu(t0, a2, Operand(t0));
2880 __ Addu(t0, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2881 __ sw(a1, MemOperand(t0, 0));
2882
2883 // Update the write barrier.
2884 __ mov(t1, a1);
2885 __ RecordWrite(a2, t0, t1, kRAHasNotBeenSaved, kDontSaveFPRegs,
2886 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2887 __ Branch(&have_js_function);
2888
2889 // We are here because tracing is on or we encountered a MISS case we can't
2890 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002891 __ bind(&miss);
2892 GenerateMiss(masm);
2893
2894 // the slow case
2895 __ bind(&slow_start);
2896 // Check that the function is really a JavaScript function.
2897 // r1: pushed function (to be verified)
2898 __ JumpIfSmi(a1, &non_function);
2899
2900 // Goto slow case if we do not have a function.
2901 __ GetObjectType(a1, t0, t0);
2902 __ Branch(&slow, ne, t0, Operand(JS_FUNCTION_TYPE));
2903 __ Branch(&have_js_function);
Steve Block44f0eee2011-05-26 01:26:41 +01002904}
2905
2906
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002907void CallICStub::GenerateMiss(MacroAssembler* masm) {
2908 // Get the receiver of the function from the stack; 1 ~ return address.
2909 __ lw(t0, MemOperand(sp, (arg_count() + 1) * kPointerSize));
2910
2911 {
2912 FrameScope scope(masm, StackFrame::INTERNAL);
2913
2914 // Push the receiver and the function and feedback info.
2915 __ Push(t0, a1, a2, a3);
2916
2917 // Call the entry.
2918 IC::UtilityId id = GetICState() == DEFAULT ? IC::kCallIC_Miss
2919 : IC::kCallIC_Customization_Miss;
2920
2921 ExternalReference miss = ExternalReference(IC_Utility(id),
2922 masm->isolate());
2923 __ CallExternalReference(miss, 4);
2924
2925 // Move result to a1 and exit the internal frame.
2926 __ mov(a1, v0);
2927 }
Steve Block44f0eee2011-05-26 01:26:41 +01002928}
2929
2930
Ben Murdoch257744e2011-11-30 15:57:28 +00002931// StringCharCodeAtGenerator.
Steve Block44f0eee2011-05-26 01:26:41 +01002932void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002933 DCHECK(!t0.is(index_));
2934 DCHECK(!t0.is(result_));
2935 DCHECK(!t0.is(object_));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002936 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2937 // If the receiver is a smi trigger the non-string case.
2938 __ JumpIfSmi(object_, receiver_not_string_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002939
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002940 // Fetch the instance type of the receiver into result register.
2941 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
2942 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
2943 // If the receiver is not a string trigger the non-string case.
2944 __ And(t0, result_, Operand(kIsNotStringMask));
2945 __ Branch(receiver_not_string_, ne, t0, Operand(zero_reg));
2946 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002947
2948 // If the index is non-smi trigger the non-smi case.
2949 __ JumpIfNotSmi(index_, &index_not_smi_);
2950
Ben Murdoch257744e2011-11-30 15:57:28 +00002951 __ bind(&got_smi_index_);
2952
2953 // Check for index out of range.
2954 __ lw(t0, FieldMemOperand(object_, String::kLengthOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002955 __ Branch(index_out_of_range_, ls, t0, Operand(index_));
Ben Murdoch257744e2011-11-30 15:57:28 +00002956
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002957 __ sra(index_, index_, kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00002958
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002959 StringCharLoadGenerator::Generate(masm,
2960 object_,
2961 index_,
2962 result_,
2963 &call_runtime_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002964
Ben Murdoch257744e2011-11-30 15:57:28 +00002965 __ sll(result_, result_, kSmiTagSize);
2966 __ bind(&exit_);
Steve Block44f0eee2011-05-26 01:26:41 +01002967}
2968
2969
2970void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002971 MacroAssembler* masm,
2972 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002973 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
Ben Murdoch257744e2011-11-30 15:57:28 +00002974
2975 // Index is not a smi.
2976 __ bind(&index_not_smi_);
2977 // If index is a heap number, try converting it to an integer.
2978 __ CheckMap(index_,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002979 result_,
Ben Murdoch257744e2011-11-30 15:57:28 +00002980 Heap::kHeapNumberMapRootIndex,
2981 index_not_number_,
2982 DONT_DO_SMI_CHECK);
2983 call_helper.BeforeCall(masm);
2984 // Consumed by runtime conversion function:
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002985 __ Push(object_, index_);
Ben Murdoch257744e2011-11-30 15:57:28 +00002986 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
2987 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
2988 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002989 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
Ben Murdoch257744e2011-11-30 15:57:28 +00002990 // NumberToSmi discards numbers that are not exact integers.
2991 __ CallRuntime(Runtime::kNumberToSmi, 1);
2992 }
2993
2994 // Save the conversion result before the pop instructions below
2995 // have a chance to overwrite it.
2996
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002997 __ Move(index_, v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002998 __ pop(object_);
2999 // Reload the instance type.
3000 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3001 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3002 call_helper.AfterCall(masm);
3003 // If index is still not a smi, it must be out of range.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003004 __ JumpIfNotSmi(index_, index_out_of_range_);
Ben Murdoch257744e2011-11-30 15:57:28 +00003005 // Otherwise, return to the fast path.
3006 __ Branch(&got_smi_index_);
3007
3008 // Call runtime. We get here when the receiver is a string and the
3009 // index is a number, but the code of getting the actual character
3010 // is too complex (e.g., when the string needs to be flattened).
3011 __ bind(&call_runtime_);
3012 call_helper.BeforeCall(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003013 __ sll(index_, index_, kSmiTagSize);
Ben Murdoch257744e2011-11-30 15:57:28 +00003014 __ Push(object_, index_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003015 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003016
3017 __ Move(result_, v0);
3018
3019 call_helper.AfterCall(masm);
3020 __ jmp(&exit_);
3021
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003022 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
Steve Block44f0eee2011-05-26 01:26:41 +01003023}
3024
3025
3026// -------------------------------------------------------------------------
3027// StringCharFromCodeGenerator
3028
3029void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003030 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3031
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003032 DCHECK(!t0.is(result_));
3033 DCHECK(!t0.is(code_));
Ben Murdoch257744e2011-11-30 15:57:28 +00003034
3035 STATIC_ASSERT(kSmiTag == 0);
3036 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003037 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCode + 1));
Ben Murdoch257744e2011-11-30 15:57:28 +00003038 __ And(t0,
3039 code_,
3040 Operand(kSmiTagMask |
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003041 ((~String::kMaxOneByteCharCode) << kSmiTagSize)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003042 __ Branch(&slow_case_, ne, t0, Operand(zero_reg));
3043
3044 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003045 // At this point code register contains smi tagged one-byte char code.
Ben Murdoch257744e2011-11-30 15:57:28 +00003046 STATIC_ASSERT(kSmiTag == 0);
3047 __ sll(t0, code_, kPointerSizeLog2 - kSmiTagSize);
3048 __ Addu(result_, result_, t0);
3049 __ lw(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
3050 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
3051 __ Branch(&slow_case_, eq, result_, Operand(t0));
3052 __ bind(&exit_);
Steve Block44f0eee2011-05-26 01:26:41 +01003053}
3054
3055
3056void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003057 MacroAssembler* masm,
3058 const RuntimeCallHelper& call_helper) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003059 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
Ben Murdoch257744e2011-11-30 15:57:28 +00003060
3061 __ bind(&slow_case_);
3062 call_helper.BeforeCall(masm);
3063 __ push(code_);
3064 __ CallRuntime(Runtime::kCharFromCode, 1);
3065 __ Move(result_, v0);
3066
3067 call_helper.AfterCall(masm);
3068 __ Branch(&exit_);
3069
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003070 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
Steve Block44f0eee2011-05-26 01:26:41 +01003071}
3072
3073
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003074enum CopyCharactersFlags { COPY_ONE_BYTE = 1, DEST_ALWAYS_ALIGNED = 2 };
Steve Block44f0eee2011-05-26 01:26:41 +01003075
3076
Steve Block44f0eee2011-05-26 01:26:41 +01003077void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3078 Register dest,
3079 Register src,
3080 Register count,
3081 Register scratch,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003082 String::Encoding encoding) {
3083 if (FLAG_debug_code) {
3084 // Check that destination is word aligned.
3085 __ And(scratch, dest, Operand(kPointerAlignmentMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00003086 __ Check(eq,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003087 kDestinationOfCopyNotAligned,
3088 scratch,
Ben Murdoch257744e2011-11-30 15:57:28 +00003089 Operand(zero_reg));
3090 }
3091
Ben Murdoch257744e2011-11-30 15:57:28 +00003092 // Assumes word reads and writes are little endian.
3093 // Nothing to do for zero characters.
3094 Label done;
3095
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003096 if (encoding == String::TWO_BYTE_ENCODING) {
3097 __ Addu(count, count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00003098 }
3099
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003100 Register limit = count; // Read until dest equals this.
3101 __ Addu(limit, dest, Operand(count));
Ben Murdoch257744e2011-11-30 15:57:28 +00003102
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003103 Label loop_entry, loop;
Ben Murdoch257744e2011-11-30 15:57:28 +00003104 // Copy bytes from src to dest until dest hits limit.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003105 __ Branch(&loop_entry);
3106 __ bind(&loop);
3107 __ lbu(scratch, MemOperand(src));
3108 __ Addu(src, src, Operand(1));
3109 __ sb(scratch, MemOperand(dest));
3110 __ Addu(dest, dest, Operand(1));
3111 __ bind(&loop_entry);
3112 __ Branch(&loop, lt, dest, Operand(limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00003113
3114 __ bind(&done);
Steve Block44f0eee2011-05-26 01:26:41 +01003115}
3116
3117
Steve Block44f0eee2011-05-26 01:26:41 +01003118void SubStringStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003119 Label runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00003120 // Stack frame on entry.
3121 // ra: return address
3122 // sp[0]: to
3123 // sp[4]: from
3124 // sp[8]: string
3125
3126 // This stub is called from the native-call %_SubString(...), so
3127 // nothing can be assumed about the arguments. It is tested that:
3128 // "string" is a sequential string,
3129 // both "from" and "to" are smis, and
3130 // 0 <= from <= to <= string.length.
3131 // If any of these assumptions fail, we call the runtime system.
3132
Ben Murdochdb1b4382012-04-26 19:03:50 +01003133 const int kToOffset = 0 * kPointerSize;
3134 const int kFromOffset = 1 * kPointerSize;
3135 const int kStringOffset = 2 * kPointerSize;
Ben Murdoch257744e2011-11-30 15:57:28 +00003136
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003137 __ lw(a2, MemOperand(sp, kToOffset));
3138 __ lw(a3, MemOperand(sp, kFromOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003139 STATIC_ASSERT(kFromOffset == kToOffset + 4);
3140 STATIC_ASSERT(kSmiTag == 0);
3141 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3142
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003143 // Utilize delay slots. SmiUntag doesn't emit a jump, everything else is
3144 // safe in this case.
3145 __ UntagAndJumpIfNotSmi(a2, a2, &runtime);
3146 __ UntagAndJumpIfNotSmi(a3, a3, &runtime);
3147 // Both a2 and a3 are untagged integers.
Ben Murdoch257744e2011-11-30 15:57:28 +00003148
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003149 __ Branch(&runtime, lt, a3, Operand(zero_reg)); // From < 0.
Ben Murdoch257744e2011-11-30 15:57:28 +00003150
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003151 __ Branch(&runtime, gt, a3, Operand(a2)); // Fail if from > to.
3152 __ Subu(a2, a2, a3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003153
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003154 // Make sure first argument is a string.
Ben Murdoch589d6972011-11-30 16:04:58 +00003155 __ lw(v0, MemOperand(sp, kStringOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003156 __ JumpIfSmi(v0, &runtime);
Ben Murdoch589d6972011-11-30 16:04:58 +00003157 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003158 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003159 __ And(t0, a1, Operand(kIsNotStringMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00003160
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003161 __ Branch(&runtime, ne, t0, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00003162
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003163 Label single_char;
3164 __ Branch(&single_char, eq, a2, Operand(1));
3165
Ben Murdoch589d6972011-11-30 16:04:58 +00003166 // Short-cut for the case of trivial substring.
3167 Label return_v0;
3168 // v0: original string
3169 // a2: result string length
3170 __ lw(t0, FieldMemOperand(v0, String::kLengthOffset));
3171 __ sra(t0, t0, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003172 // Return original string.
Ben Murdoch589d6972011-11-30 16:04:58 +00003173 __ Branch(&return_v0, eq, a2, Operand(t0));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003174 // Longer than original string's length or negative: unsafe arguments.
3175 __ Branch(&runtime, hi, a2, Operand(t0));
3176 // Shorter than original string's length: an actual substring.
Ben Murdoch257744e2011-11-30 15:57:28 +00003177
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003178 // Deal with different string types: update the index if necessary
3179 // and put the underlying string into t1.
3180 // v0: original string
3181 // a1: instance type
3182 // a2: length
3183 // a3: from index (untagged)
3184 Label underlying_unpacked, sliced_string, seq_or_external_string;
3185 // If the string is not indirect, it can only be sequential or external.
3186 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3187 STATIC_ASSERT(kIsIndirectStringMask != 0);
3188 __ And(t0, a1, Operand(kIsIndirectStringMask));
3189 __ Branch(USE_DELAY_SLOT, &seq_or_external_string, eq, t0, Operand(zero_reg));
3190 // t0 is used as a scratch register and can be overwritten in either case.
3191 __ And(t0, a1, Operand(kSlicedNotConsMask));
3192 __ Branch(&sliced_string, ne, t0, Operand(zero_reg));
3193 // Cons string. Check whether it is flat, then fetch first part.
3194 __ lw(t1, FieldMemOperand(v0, ConsString::kSecondOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003195 __ LoadRoot(t0, Heap::kempty_stringRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003196 __ Branch(&runtime, ne, t1, Operand(t0));
3197 __ lw(t1, FieldMemOperand(v0, ConsString::kFirstOffset));
3198 // Update instance type.
3199 __ lw(a1, FieldMemOperand(t1, HeapObject::kMapOffset));
3200 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
3201 __ jmp(&underlying_unpacked);
Ben Murdoch257744e2011-11-30 15:57:28 +00003202
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003203 __ bind(&sliced_string);
3204 // Sliced string. Fetch parent and correct start index by offset.
3205 __ lw(t1, FieldMemOperand(v0, SlicedString::kParentOffset));
3206 __ lw(t0, FieldMemOperand(v0, SlicedString::kOffsetOffset));
3207 __ sra(t0, t0, 1); // Add offset to index.
3208 __ Addu(a3, a3, t0);
3209 // Update instance type.
3210 __ lw(a1, FieldMemOperand(t1, HeapObject::kMapOffset));
3211 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
3212 __ jmp(&underlying_unpacked);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003213
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003214 __ bind(&seq_or_external_string);
3215 // Sequential or external string. Just move string to the expected register.
3216 __ mov(t1, v0);
3217
3218 __ bind(&underlying_unpacked);
3219
3220 if (FLAG_string_slices) {
3221 Label copy_routine;
3222 // t1: underlying subject string
3223 // a1: instance type of underlying subject string
3224 // a2: length
3225 // a3: adjusted start index (untagged)
3226 // Short slice. Copy instead of slicing.
3227 __ Branch(&copy_routine, lt, a2, Operand(SlicedString::kMinLength));
3228 // Allocate new sliced string. At this point we do not reload the instance
3229 // type including the string encoding because we simply rely on the info
3230 // provided by the original string. It does not matter if the original
3231 // string's encoding is wrong because we always have to recheck encoding of
3232 // the newly created string's parent anyways due to externalized strings.
3233 Label two_byte_slice, set_slice_header;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003234 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003235 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3236 __ And(t0, a1, Operand(kStringEncodingMask));
3237 __ Branch(&two_byte_slice, eq, t0, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003238 __ AllocateOneByteSlicedString(v0, a2, t2, t3, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003239 __ jmp(&set_slice_header);
3240 __ bind(&two_byte_slice);
3241 __ AllocateTwoByteSlicedString(v0, a2, t2, t3, &runtime);
3242 __ bind(&set_slice_header);
3243 __ sll(a3, a3, 1);
3244 __ sw(t1, FieldMemOperand(v0, SlicedString::kParentOffset));
3245 __ sw(a3, FieldMemOperand(v0, SlicedString::kOffsetOffset));
3246 __ jmp(&return_v0);
3247
3248 __ bind(&copy_routine);
3249 }
3250
3251 // t1: underlying subject string
3252 // a1: instance type of underlying subject string
3253 // a2: length
3254 // a3: adjusted start index (untagged)
3255 Label two_byte_sequential, sequential_string, allocate_result;
3256 STATIC_ASSERT(kExternalStringTag != 0);
3257 STATIC_ASSERT(kSeqStringTag == 0);
3258 __ And(t0, a1, Operand(kExternalStringTag));
3259 __ Branch(&sequential_string, eq, t0, Operand(zero_reg));
3260
3261 // Handle external string.
3262 // Rule out short external strings.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003263 STATIC_ASSERT(kShortExternalStringTag != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003264 __ And(t0, a1, Operand(kShortExternalStringTag));
3265 __ Branch(&runtime, ne, t0, Operand(zero_reg));
3266 __ lw(t1, FieldMemOperand(t1, ExternalString::kResourceDataOffset));
3267 // t1 already points to the first character of underlying string.
3268 __ jmp(&allocate_result);
3269
3270 __ bind(&sequential_string);
3271 // Locate first character of underlying subject string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003272 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3273 __ Addu(t1, t1, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003274
3275 __ bind(&allocate_result);
3276 // Sequential acii string. Allocate the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003277 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003278 __ And(t0, a1, Operand(kStringEncodingMask));
3279 __ Branch(&two_byte_sequential, eq, t0, Operand(zero_reg));
3280
3281 // Allocate and copy the resulting ASCII string.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003282 __ AllocateOneByteString(v0, a2, t0, t2, t3, &runtime);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003283
3284 // Locate first character of substring to copy.
3285 __ Addu(t1, t1, a3);
3286
Ben Murdoch257744e2011-11-30 15:57:28 +00003287 // Locate first character of result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003288 __ Addu(a1, v0, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00003289
Ben Murdoch589d6972011-11-30 16:04:58 +00003290 // v0: result string
3291 // a1: first character of result string
3292 // a2: result string length
3293 // t1: first character of substring to copy
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003294 STATIC_ASSERT((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3295 StringHelper::GenerateCopyCharacters(
3296 masm, a1, t1, a2, a3, String::ONE_BYTE_ENCODING);
Ben Murdoch589d6972011-11-30 16:04:58 +00003297 __ jmp(&return_v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003298
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003299 // Allocate and copy the resulting two-byte string.
3300 __ bind(&two_byte_sequential);
3301 __ AllocateTwoByteString(v0, a2, t0, t2, t3, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003302
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003303 // Locate first character of substring to copy.
Ben Murdoch589d6972011-11-30 16:04:58 +00003304 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003305 __ sll(t0, a3, 1);
3306 __ Addu(t1, t1, t0);
Ben Murdoch257744e2011-11-30 15:57:28 +00003307 // Locate first character of result.
3308 __ Addu(a1, v0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch589d6972011-11-30 16:04:58 +00003309
Ben Murdoch257744e2011-11-30 15:57:28 +00003310 // v0: result string.
3311 // a1: first character of result.
3312 // a2: result length.
Ben Murdoch589d6972011-11-30 16:04:58 +00003313 // t1: first character of substring to copy.
Ben Murdoch257744e2011-11-30 15:57:28 +00003314 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003315 StringHelper::GenerateCopyCharacters(
3316 masm, a1, t1, a2, a3, String::TWO_BYTE_ENCODING);
Ben Murdoch589d6972011-11-30 16:04:58 +00003317
3318 __ bind(&return_v0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003319 Counters* counters = isolate()->counters();
Ben Murdoch257744e2011-11-30 15:57:28 +00003320 __ IncrementCounter(counters->sub_string_native(), 1, a3, t0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003321 __ DropAndRet(3);
Ben Murdoch257744e2011-11-30 15:57:28 +00003322
3323 // Just jump to runtime to create the sub string.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003324 __ bind(&runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003325 __ TailCallRuntime(Runtime::kSubString, 3, 1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003326
3327 __ bind(&single_char);
3328 // v0: original string
3329 // a1: instance type
3330 // a2: length
3331 // a3: from index (untagged)
3332 __ SmiTag(a3, a3);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003333 StringCharAtGenerator generator(v0, a3, a2, v0, &runtime, &runtime, &runtime,
3334 STRING_INDEX_IS_NUMBER, RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003335 generator.GenerateFast(masm);
3336 __ DropAndRet(3);
3337 generator.SkipSlow(masm, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003338}
3339
3340
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003341void ToNumberStub::Generate(MacroAssembler* masm) {
3342 // The ToNumber stub takes one argument in a0.
3343 Label not_smi;
3344 __ JumpIfNotSmi(a0, &not_smi);
3345 __ Ret(USE_DELAY_SLOT);
3346 __ mov(v0, a0);
3347 __ bind(&not_smi);
3348
3349 Label not_heap_number;
3350 __ lw(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
3351 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
3352 // a0: object
3353 // a1: instance type.
3354 __ Branch(&not_heap_number, ne, a1, Operand(HEAP_NUMBER_TYPE));
3355 __ Ret(USE_DELAY_SLOT);
3356 __ mov(v0, a0);
3357 __ bind(&not_heap_number);
3358
3359 Label not_string, slow_string;
3360 __ Branch(&not_string, hs, a1, Operand(FIRST_NONSTRING_TYPE));
3361 // Check if string has a cached array index.
3362 __ lw(a2, FieldMemOperand(a0, String::kHashFieldOffset));
3363 __ And(at, a2, Operand(String::kContainsCachedArrayIndexMask));
3364 __ Branch(&slow_string, ne, at, Operand(zero_reg));
3365 __ IndexFromHash(a2, a0);
3366 __ Ret(USE_DELAY_SLOT);
3367 __ mov(v0, a0);
3368 __ bind(&slow_string);
3369 __ push(a0); // Push argument.
3370 __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
3371 __ bind(&not_string);
3372
3373 Label not_oddball;
3374 __ Branch(&not_oddball, ne, a1, Operand(ODDBALL_TYPE));
3375 __ Ret(USE_DELAY_SLOT);
3376 __ lw(v0, FieldMemOperand(a0, Oddball::kToNumberOffset));
3377 __ bind(&not_oddball);
3378
3379 __ push(a0); // Push argument.
3380 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
3381}
3382
3383
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003384void StringHelper::GenerateFlatOneByteStringEquals(
3385 MacroAssembler* masm, Register left, Register right, Register scratch1,
3386 Register scratch2, Register scratch3) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003387 Register length = scratch1;
3388
3389 // Compare lengths.
3390 Label strings_not_equal, check_zero_length;
3391 __ lw(length, FieldMemOperand(left, String::kLengthOffset));
3392 __ lw(scratch2, FieldMemOperand(right, String::kLengthOffset));
3393 __ Branch(&check_zero_length, eq, length, Operand(scratch2));
3394 __ bind(&strings_not_equal);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003395 DCHECK(is_int16(NOT_EQUAL));
3396 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003397 __ li(v0, Operand(Smi::FromInt(NOT_EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003398
3399 // Check if the length is zero.
3400 Label compare_chars;
3401 __ bind(&check_zero_length);
3402 STATIC_ASSERT(kSmiTag == 0);
3403 __ Branch(&compare_chars, ne, length, Operand(zero_reg));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003404 DCHECK(is_int16(EQUAL));
3405 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003406 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003407
3408 // Compare characters.
3409 __ bind(&compare_chars);
3410
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003411 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2, scratch3,
3412 v0, &strings_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00003413
3414 // Characters are equal.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003415 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003416 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01003417}
3418
3419
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003420void StringHelper::GenerateCompareFlatOneByteStrings(
3421 MacroAssembler* masm, Register left, Register right, Register scratch1,
3422 Register scratch2, Register scratch3, Register scratch4) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003423 Label result_not_equal, compare_lengths;
3424 // Find minimum length and length difference.
3425 __ lw(scratch1, FieldMemOperand(left, String::kLengthOffset));
3426 __ lw(scratch2, FieldMemOperand(right, String::kLengthOffset));
3427 __ Subu(scratch3, scratch1, Operand(scratch2));
3428 Register length_delta = scratch3;
3429 __ slt(scratch4, scratch2, scratch1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003430 __ Movn(scratch1, scratch2, scratch4);
Ben Murdoch257744e2011-11-30 15:57:28 +00003431 Register min_length = scratch1;
3432 STATIC_ASSERT(kSmiTag == 0);
3433 __ Branch(&compare_lengths, eq, min_length, Operand(zero_reg));
3434
3435 // Compare loop.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003436 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
3437 scratch4, v0, &result_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00003438
3439 // Compare lengths - strings up to min-length are equal.
3440 __ bind(&compare_lengths);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003441 DCHECK(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003442 // Use length_delta as result if it's zero.
3443 __ mov(scratch2, length_delta);
3444 __ mov(scratch4, zero_reg);
3445 __ mov(v0, zero_reg);
3446
3447 __ bind(&result_not_equal);
3448 // Conditionally update the result based either on length_delta or
3449 // the last comparion performed in the loop above.
3450 Label ret;
3451 __ Branch(&ret, eq, scratch2, Operand(scratch4));
3452 __ li(v0, Operand(Smi::FromInt(GREATER)));
3453 __ Branch(&ret, gt, scratch2, Operand(scratch4));
3454 __ li(v0, Operand(Smi::FromInt(LESS)));
3455 __ bind(&ret);
3456 __ Ret();
3457}
3458
3459
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003460void StringHelper::GenerateOneByteCharsCompareLoop(
3461 MacroAssembler* masm, Register left, Register right, Register length,
3462 Register scratch1, Register scratch2, Register scratch3,
Ben Murdoch257744e2011-11-30 15:57:28 +00003463 Label* chars_not_equal) {
3464 // Change index to run from -length to -1 by adding length to string
3465 // start. This means that loop ends when index reaches zero, which
3466 // doesn't need an additional compare.
3467 __ SmiUntag(length);
3468 __ Addu(scratch1, length,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003469 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch257744e2011-11-30 15:57:28 +00003470 __ Addu(left, left, Operand(scratch1));
3471 __ Addu(right, right, Operand(scratch1));
3472 __ Subu(length, zero_reg, length);
3473 Register index = length; // index = -length;
3474
3475
3476 // Compare loop.
3477 Label loop;
3478 __ bind(&loop);
3479 __ Addu(scratch3, left, index);
3480 __ lbu(scratch1, MemOperand(scratch3));
3481 __ Addu(scratch3, right, index);
3482 __ lbu(scratch2, MemOperand(scratch3));
3483 __ Branch(chars_not_equal, ne, scratch1, Operand(scratch2));
3484 __ Addu(index, index, 1);
3485 __ Branch(&loop, ne, index, Operand(zero_reg));
Steve Block44f0eee2011-05-26 01:26:41 +01003486}
3487
3488
3489void StringCompareStub::Generate(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003490 Label runtime;
3491
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003492 Counters* counters = isolate()->counters();
Ben Murdoch257744e2011-11-30 15:57:28 +00003493
3494 // Stack frame on entry.
3495 // sp[0]: right string
3496 // sp[4]: left string
3497 __ lw(a1, MemOperand(sp, 1 * kPointerSize)); // Left.
3498 __ lw(a0, MemOperand(sp, 0 * kPointerSize)); // Right.
3499
3500 Label not_same;
3501 __ Branch(&not_same, ne, a0, Operand(a1));
3502 STATIC_ASSERT(EQUAL == 0);
3503 STATIC_ASSERT(kSmiTag == 0);
3504 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3505 __ IncrementCounter(counters->string_compare_native(), 1, a1, a2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003506 __ DropAndRet(2);
Ben Murdoch257744e2011-11-30 15:57:28 +00003507
3508 __ bind(&not_same);
3509
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003510 // Check that both objects are sequential one-byte strings.
3511 __ JumpIfNotBothSequentialOneByteStrings(a1, a0, a2, a3, &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003512
3513 // Compare flat ASCII strings natively. Remove arguments from stack first.
3514 __ IncrementCounter(counters->string_compare_native(), 1, a2, a3);
3515 __ Addu(sp, sp, Operand(2 * kPointerSize));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003516 StringHelper::GenerateCompareFlatOneByteStrings(masm, a1, a0, a2, a3, t0, t1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003517
3518 __ bind(&runtime);
3519 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
Steve Block44f0eee2011-05-26 01:26:41 +01003520}
3521
3522
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003523void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3524 // ----------- S t a t e -------------
3525 // -- a1 : left
3526 // -- a0 : right
3527 // -- ra : return address
3528 // -----------------------------------
Ben Murdoch257744e2011-11-30 15:57:28 +00003529
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003530 // Load a2 with the allocation site. We stick an undefined dummy value here
3531 // and replace it with the real allocation site later when we instantiate this
3532 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3533 __ li(a2, handle(isolate()->heap()->undefined_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00003534
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003535 // Make sure that we actually patched the allocation site.
3536 if (FLAG_debug_code) {
3537 __ And(at, a2, Operand(kSmiTagMask));
3538 __ Assert(ne, kExpectedAllocationSite, at, Operand(zero_reg));
3539 __ lw(t0, FieldMemOperand(a2, HeapObject::kMapOffset));
3540 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
3541 __ Assert(eq, kExpectedAllocationSite, t0, Operand(at));
Ben Murdoch257744e2011-11-30 15:57:28 +00003542 }
3543
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003544 // Tail call into the stub that handles binary operations with allocation
3545 // sites.
3546 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3547 __ TailCallStub(&stub);
Ben Murdoch257744e2011-11-30 15:57:28 +00003548}
3549
3550
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003551void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3552 DCHECK(state() == CompareICState::SMI);
Ben Murdoch257744e2011-11-30 15:57:28 +00003553 Label miss;
3554 __ Or(a2, a1, a0);
3555 __ JumpIfNotSmi(a2, &miss);
3556
3557 if (GetCondition() == eq) {
3558 // For equality we do not care about the sign of the result.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003559 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003560 __ Subu(v0, a0, a1);
3561 } else {
3562 // Untag before subtracting to avoid handling overflow.
3563 __ SmiUntag(a1);
3564 __ SmiUntag(a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003565 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003566 __ Subu(v0, a1, a0);
3567 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003568
3569 __ bind(&miss);
3570 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01003571}
3572
3573
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003574void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3575 DCHECK(state() == CompareICState::NUMBER);
Ben Murdoch257744e2011-11-30 15:57:28 +00003576
3577 Label generic_stub;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003578 Label unordered, maybe_undefined1, maybe_undefined2;
Ben Murdoch257744e2011-11-30 15:57:28 +00003579 Label miss;
Ben Murdoch257744e2011-11-30 15:57:28 +00003580
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003581 if (left() == CompareICState::SMI) {
3582 __ JumpIfNotSmi(a1, &miss);
3583 }
3584 if (right() == CompareICState::SMI) {
3585 __ JumpIfNotSmi(a0, &miss);
Ben Murdoch85b71792012-04-11 18:30:58 +01003586 }
Ben Murdoch5d4cdbf2012-04-11 10:23:59 +01003587
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003588 // Inlining the double comparison and falling back to the general compare
3589 // stub if NaN is involved.
3590 // Load left and right operand.
3591 Label done, left, left_smi, right_smi;
3592 __ JumpIfSmi(a0, &right_smi);
3593 __ CheckMap(a0, a2, Heap::kHeapNumberMapRootIndex, &maybe_undefined1,
3594 DONT_DO_SMI_CHECK);
3595 __ Subu(a2, a0, Operand(kHeapObjectTag));
3596 __ ldc1(f2, MemOperand(a2, HeapNumber::kValueOffset));
3597 __ Branch(&left);
3598 __ bind(&right_smi);
3599 __ SmiUntag(a2, a0); // Can't clobber a0 yet.
3600 FPURegister single_scratch = f6;
3601 __ mtc1(a2, single_scratch);
3602 __ cvt_d_w(f2, single_scratch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003603
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003604 __ bind(&left);
3605 __ JumpIfSmi(a1, &left_smi);
3606 __ CheckMap(a1, a2, Heap::kHeapNumberMapRootIndex, &maybe_undefined2,
3607 DONT_DO_SMI_CHECK);
3608 __ Subu(a2, a1, Operand(kHeapObjectTag));
3609 __ ldc1(f0, MemOperand(a2, HeapNumber::kValueOffset));
3610 __ Branch(&done);
3611 __ bind(&left_smi);
3612 __ SmiUntag(a2, a1); // Can't clobber a1 yet.
3613 single_scratch = f8;
3614 __ mtc1(a2, single_scratch);
3615 __ cvt_d_w(f0, single_scratch);
3616
3617 __ bind(&done);
3618
3619 // Return a result of -1, 0, or 1, or use CompareStub for NaNs.
3620 Label fpu_eq, fpu_lt;
3621 // Test if equal, and also handle the unordered/NaN case.
3622 __ BranchF(&fpu_eq, &unordered, eq, f0, f2);
3623
3624 // Test if less (unordered case is already handled).
3625 __ BranchF(&fpu_lt, NULL, lt, f0, f2);
3626
3627 // Otherwise it's greater, so just fall thru, and return.
3628 DCHECK(is_int16(GREATER) && is_int16(EQUAL) && is_int16(LESS));
3629 __ Ret(USE_DELAY_SLOT);
3630 __ li(v0, Operand(GREATER));
3631
3632 __ bind(&fpu_eq);
3633 __ Ret(USE_DELAY_SLOT);
3634 __ li(v0, Operand(EQUAL));
3635
3636 __ bind(&fpu_lt);
3637 __ Ret(USE_DELAY_SLOT);
3638 __ li(v0, Operand(LESS));
3639
3640 __ bind(&unordered);
Ben Murdoch257744e2011-11-30 15:57:28 +00003641 __ bind(&generic_stub);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003642 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
3643 CompareICState::GENERIC, CompareICState::GENERIC);
Ben Murdoch257744e2011-11-30 15:57:28 +00003644 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
3645
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003646 __ bind(&maybe_undefined1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003647 if (Token::IsOrderedRelationalCompareOp(op())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003648 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3649 __ Branch(&miss, ne, a0, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003650 __ JumpIfSmi(a1, &unordered);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003651 __ GetObjectType(a1, a2, a2);
3652 __ Branch(&maybe_undefined2, ne, a2, Operand(HEAP_NUMBER_TYPE));
3653 __ jmp(&unordered);
3654 }
3655
3656 __ bind(&maybe_undefined2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003657 if (Token::IsOrderedRelationalCompareOp(op())) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003658 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3659 __ Branch(&unordered, eq, a1, Operand(at));
3660 }
3661
Ben Murdoch257744e2011-11-30 15:57:28 +00003662 __ bind(&miss);
3663 GenerateMiss(masm);
3664}
3665
3666
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003667void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3668 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00003669 Label miss;
3670
3671 // Registers containing left and right operands respectively.
3672 Register left = a1;
3673 Register right = a0;
3674 Register tmp1 = a2;
3675 Register tmp2 = a3;
3676
3677 // Check that both operands are heap objects.
3678 __ JumpIfEitherSmi(left, right, &miss);
3679
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003680 // Check that both operands are internalized strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00003681 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3682 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3683 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3684 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003685 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3686 __ Or(tmp1, tmp1, Operand(tmp2));
3687 __ And(at, tmp1, Operand(kIsNotStringMask | kIsNotInternalizedMask));
3688 __ Branch(&miss, ne, at, Operand(zero_reg));
3689
Ben Murdoch257744e2011-11-30 15:57:28 +00003690 // Make sure a0 is non-zero. At this point input operands are
3691 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003692 DCHECK(right.is(a0));
Ben Murdoch257744e2011-11-30 15:57:28 +00003693 STATIC_ASSERT(EQUAL == 0);
3694 STATIC_ASSERT(kSmiTag == 0);
3695 __ mov(v0, right);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003696 // Internalized strings are compared by identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00003697 __ Ret(ne, left, Operand(right));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003698 DCHECK(is_int16(EQUAL));
3699 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003700 __ li(v0, Operand(Smi::FromInt(EQUAL)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003701
3702 __ bind(&miss);
3703 GenerateMiss(masm);
3704}
3705
3706
3707void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3708 DCHECK(state() == CompareICState::UNIQUE_NAME);
3709 DCHECK(GetCondition() == eq);
3710 Label miss;
3711
3712 // Registers containing left and right operands respectively.
3713 Register left = a1;
3714 Register right = a0;
3715 Register tmp1 = a2;
3716 Register tmp2 = a3;
3717
3718 // Check that both operands are heap objects.
3719 __ JumpIfEitherSmi(left, right, &miss);
3720
3721 // Check that both operands are unique names. This leaves the instance
3722 // types loaded in tmp1 and tmp2.
3723 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3724 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3725 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3726 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3727
3728 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss);
3729 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss);
3730
3731 // Use a0 as result
3732 __ mov(v0, a0);
3733
3734 // Unique names are compared by identity.
3735 Label done;
3736 __ Branch(&done, ne, left, Operand(right));
3737 // Make sure a0 is non-zero. At this point input operands are
3738 // guaranteed to be non-zero.
3739 DCHECK(right.is(a0));
3740 STATIC_ASSERT(EQUAL == 0);
3741 STATIC_ASSERT(kSmiTag == 0);
3742 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3743 __ bind(&done);
Ben Murdoch257744e2011-11-30 15:57:28 +00003744 __ Ret();
3745
3746 __ bind(&miss);
3747 GenerateMiss(masm);
3748}
3749
3750
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003751void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3752 DCHECK(state() == CompareICState::STRING);
Ben Murdoch257744e2011-11-30 15:57:28 +00003753 Label miss;
3754
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003755 bool equality = Token::IsEqualityOp(op());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003756
Ben Murdoch257744e2011-11-30 15:57:28 +00003757 // Registers containing left and right operands respectively.
3758 Register left = a1;
3759 Register right = a0;
3760 Register tmp1 = a2;
3761 Register tmp2 = a3;
3762 Register tmp3 = t0;
3763 Register tmp4 = t1;
3764 Register tmp5 = t2;
3765
3766 // Check that both operands are heap objects.
3767 __ JumpIfEitherSmi(left, right, &miss);
3768
3769 // Check that both operands are strings. This leaves the instance
3770 // types loaded in tmp1 and tmp2.
3771 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3772 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3773 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3774 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3775 STATIC_ASSERT(kNotStringTag != 0);
3776 __ Or(tmp3, tmp1, tmp2);
3777 __ And(tmp5, tmp3, Operand(kIsNotStringMask));
3778 __ Branch(&miss, ne, tmp5, Operand(zero_reg));
3779
3780 // Fast check for identical strings.
3781 Label left_ne_right;
3782 STATIC_ASSERT(EQUAL == 0);
3783 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003784 __ Branch(&left_ne_right, ne, left, Operand(right));
3785 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003786 __ mov(v0, zero_reg); // In the delay slot.
Ben Murdoch257744e2011-11-30 15:57:28 +00003787 __ bind(&left_ne_right);
3788
3789 // Handle not identical strings.
3790
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003791 // Check that both strings are internalized strings. If they are, we're done
3792 // because we already know they are not identical. We know they are both
3793 // strings.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003794 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003795 DCHECK(GetCondition() == eq);
3796 STATIC_ASSERT(kInternalizedTag == 0);
3797 __ Or(tmp3, tmp1, Operand(tmp2));
3798 __ And(tmp5, tmp3, Operand(kIsNotInternalizedMask));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003799 Label is_symbol;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003800 __ Branch(&is_symbol, ne, tmp5, Operand(zero_reg));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003801 // Make sure a0 is non-zero. At this point input operands are
3802 // guaranteed to be non-zero.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003803 DCHECK(right.is(a0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003804 __ Ret(USE_DELAY_SLOT);
3805 __ mov(v0, a0); // In the delay slot.
3806 __ bind(&is_symbol);
3807 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003808
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003809 // Check that both strings are sequential one-byte.
Ben Murdoch257744e2011-11-30 15:57:28 +00003810 Label runtime;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003811 __ JumpIfBothInstanceTypesAreNotSequentialOneByte(tmp1, tmp2, tmp3, tmp4,
3812 &runtime);
Ben Murdoch257744e2011-11-30 15:57:28 +00003813
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003814 // Compare flat one-byte strings. Returns when done.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003815 if (equality) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003816 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1, tmp2,
3817 tmp3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003818 } else {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003819 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3820 tmp2, tmp3, tmp4);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003821 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003822
3823 // Handle more complex cases in runtime.
3824 __ bind(&runtime);
3825 __ Push(left, right);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003826 if (equality) {
3827 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3828 } else {
3829 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
3830 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003831
3832 __ bind(&miss);
3833 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01003834}
3835
3836
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003837void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3838 DCHECK(state() == CompareICState::OBJECT);
Ben Murdoch257744e2011-11-30 15:57:28 +00003839 Label miss;
3840 __ And(a2, a1, Operand(a0));
3841 __ JumpIfSmi(a2, &miss);
3842
3843 __ GetObjectType(a0, a2, a2);
3844 __ Branch(&miss, ne, a2, Operand(JS_OBJECT_TYPE));
3845 __ GetObjectType(a1, a2, a2);
3846 __ Branch(&miss, ne, a2, Operand(JS_OBJECT_TYPE));
3847
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003848 DCHECK(GetCondition() == eq);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003849 __ Ret(USE_DELAY_SLOT);
3850 __ subu(v0, a0, a1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003851
3852 __ bind(&miss);
3853 GenerateMiss(masm);
Steve Block44f0eee2011-05-26 01:26:41 +01003854}
3855
3856
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003857void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003858 Label miss;
3859 __ And(a2, a1, a0);
3860 __ JumpIfSmi(a2, &miss);
3861 __ lw(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
3862 __ lw(a3, FieldMemOperand(a1, HeapObject::kMapOffset));
3863 __ Branch(&miss, ne, a2, Operand(known_map_));
3864 __ Branch(&miss, ne, a3, Operand(known_map_));
Ben Murdoch85b71792012-04-11 18:30:58 +01003865
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003866 __ Ret(USE_DELAY_SLOT);
3867 __ subu(v0, a0, a1);
3868
3869 __ bind(&miss);
3870 GenerateMiss(masm);
3871}
3872
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003873
3874void CompareICStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003875 {
3876 // Call the runtime system in a fresh internal frame.
3877 ExternalReference miss =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003878 ExternalReference(IC_Utility(IC::kCompareIC_Miss), isolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003879 FrameScope scope(masm, StackFrame::INTERNAL);
3880 __ Push(a1, a0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003881 __ Push(ra, a1, a0);
3882 __ li(t0, Operand(Smi::FromInt(op())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003883 __ addiu(sp, sp, -kPointerSize);
3884 __ CallExternalReference(miss, 3, USE_DELAY_SLOT);
3885 __ sw(t0, MemOperand(sp)); // In the delay slot.
3886 // Compute the entry point of the rewritten stub.
3887 __ Addu(a2, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
3888 // Restore registers.
3889 __ Pop(a1, a0, ra);
3890 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003891 __ Jump(a2);
3892}
3893
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003894
Ben Murdoch257744e2011-11-30 15:57:28 +00003895void DirectCEntryStub::Generate(MacroAssembler* masm) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003896 // Make place for arguments to fit C calling convention. Most of the callers
3897 // of DirectCEntryStub::GenerateCall are using EnterExitFrame/LeaveExitFrame
3898 // so they handle stack restoring and we don't have to do that here.
3899 // Any caller of DirectCEntryStub::GenerateCall must take care of dropping
3900 // kCArgsSlotsSize stack space after the call.
3901 __ Subu(sp, sp, Operand(kCArgsSlotsSize));
3902 // Place the return address on the stack, making the call
3903 // GC safe. The RegExp backend also relies on this.
3904 __ sw(ra, MemOperand(sp, kCArgsSlotsSize));
3905 __ Call(t9); // Call the C++ function.
Ben Murdoch257744e2011-11-30 15:57:28 +00003906 __ lw(t9, MemOperand(sp, kCArgsSlotsSize));
3907
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003908 if (FLAG_debug_code && FLAG_enable_slow_asserts) {
Ben Murdoch257744e2011-11-30 15:57:28 +00003909 // In case of an error the return address may point to a memory area
3910 // filled with kZapValue by the GC.
3911 // Dereference the address and check for this.
3912 __ lw(t0, MemOperand(t9));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003913 __ Assert(ne, kReceivedInvalidReturnAddress, t0,
Ben Murdoch257744e2011-11-30 15:57:28 +00003914 Operand(reinterpret_cast<uint32_t>(kZapValue)));
3915 }
3916 __ Jump(t9);
Steve Block44f0eee2011-05-26 01:26:41 +01003917}
3918
3919
Ben Murdoch257744e2011-11-30 15:57:28 +00003920void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoch257744e2011-11-30 15:57:28 +00003921 Register target) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003922 intptr_t loc =
3923 reinterpret_cast<intptr_t>(GetCode().location());
Ben Murdoch257744e2011-11-30 15:57:28 +00003924 __ Move(t9, target);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003925 __ li(ra, Operand(loc, RelocInfo::CODE_TARGET), CONSTANT_SIZE);
3926 __ Call(ra);
Ben Murdoch257744e2011-11-30 15:57:28 +00003927}
3928
3929
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003930void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3931 Label* miss,
3932 Label* done,
3933 Register receiver,
3934 Register properties,
3935 Handle<Name> name,
3936 Register scratch0) {
3937 DCHECK(name->IsUniqueName());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003938 // If names of slots in range from 1 to kProbes - 1 for the hash value are
Ben Murdoch257744e2011-11-30 15:57:28 +00003939 // not equal to the name and kProbes-th slot is not used (its name is the
3940 // undefined value), it guarantees the hash table doesn't contain the
3941 // property. It's true even if some slots represent deleted properties
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003942 // (their names are the hole value).
Ben Murdoch257744e2011-11-30 15:57:28 +00003943 for (int i = 0; i < kInlinedProbes; i++) {
3944 // scratch0 points to properties hash.
3945 // Compute the masked index: (hash + i + i * i) & mask.
3946 Register index = scratch0;
3947 // Capacity is smi 2^n.
3948 __ lw(index, FieldMemOperand(properties, kCapacityOffset));
3949 __ Subu(index, index, Operand(1));
3950 __ And(index, index, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003951 Smi::FromInt(name->Hash() + NameDictionary::GetProbeOffset(i))));
Ben Murdoch257744e2011-11-30 15:57:28 +00003952
3953 // Scale the index by multiplying by the entry size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003954 DCHECK(NameDictionary::kEntrySize == 3);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003955 __ sll(at, index, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003956 __ Addu(index, index, at);
3957
3958 Register entity_name = scratch0;
3959 // Having undefined at this place means the name is not contained.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003960 DCHECK_EQ(kSmiTagSize, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003961 Register tmp = properties;
Ben Murdoch257744e2011-11-30 15:57:28 +00003962 __ sll(scratch0, index, 1);
3963 __ Addu(tmp, properties, scratch0);
3964 __ lw(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
3965
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003966 DCHECK(!tmp.is(entity_name));
Ben Murdoch257744e2011-11-30 15:57:28 +00003967 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
3968 __ Branch(done, eq, entity_name, Operand(tmp));
3969
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003970 // Load the hole ready for use below:
3971 __ LoadRoot(tmp, Heap::kTheHoleValueRootIndex);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003972
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003973 // Stop if found the property.
3974 __ Branch(miss, eq, entity_name, Operand(Handle<Name>(name)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003975
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003976 Label good;
3977 __ Branch(&good, eq, entity_name, Operand(tmp));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003978
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003979 // Check if the entry name is not a unique name.
3980 __ lw(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
3981 __ lbu(entity_name,
3982 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
3983 __ JumpIfNotUniqueNameInstanceType(entity_name, miss);
3984 __ bind(&good);
Ben Murdoch257744e2011-11-30 15:57:28 +00003985
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003986 // Restore the properties.
3987 __ lw(properties,
3988 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00003989 }
3990
3991 const int spill_mask =
3992 (ra.bit() | t2.bit() | t1.bit() | t0.bit() | a3.bit() |
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003993 a2.bit() | a1.bit() | a0.bit() | v0.bit());
Ben Murdoch257744e2011-11-30 15:57:28 +00003994
3995 __ MultiPush(spill_mask);
3996 __ lw(a0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003997 __ li(a1, Operand(Handle<Name>(name)));
3998 NameDictionaryLookupStub stub(masm->isolate(), NEGATIVE_LOOKUP);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01003999 __ CallStub(&stub);
4000 __ mov(at, v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00004001 __ MultiPop(spill_mask);
4002
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004003 __ Branch(done, eq, at, Operand(zero_reg));
4004 __ Branch(miss, ne, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00004005}
4006
4007
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004008// Probe the name dictionary in the |elements| register. Jump to the
Ben Murdoch257744e2011-11-30 15:57:28 +00004009// |done| label if a property with the given name is found. Jump to
4010// the |miss| label otherwise.
4011// If lookup was successful |scratch2| will be equal to elements + 4 * index.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004012void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
4013 Label* miss,
4014 Label* done,
4015 Register elements,
4016 Register name,
4017 Register scratch1,
4018 Register scratch2) {
4019 DCHECK(!elements.is(scratch1));
4020 DCHECK(!elements.is(scratch2));
4021 DCHECK(!name.is(scratch1));
4022 DCHECK(!name.is(scratch2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004023
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004024 __ AssertName(name);
Ben Murdoch257744e2011-11-30 15:57:28 +00004025
4026 // Compute the capacity mask.
4027 __ lw(scratch1, FieldMemOperand(elements, kCapacityOffset));
4028 __ sra(scratch1, scratch1, kSmiTagSize); // convert smi to int
4029 __ Subu(scratch1, scratch1, Operand(1));
4030
4031 // Generate an unrolled loop that performs a few probes before
4032 // giving up. Measurements done on Gmail indicate that 2 probes
4033 // cover ~93% of loads from dictionaries.
4034 for (int i = 0; i < kInlinedProbes; i++) {
4035 // Compute the masked index: (hash + i + i * i) & mask.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004036 __ lw(scratch2, FieldMemOperand(name, Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00004037 if (i > 0) {
4038 // Add the probe offset (i + i * i) left shifted to avoid right shifting
4039 // the hash in a separate instruction. The value hash + i + i * i is right
4040 // shifted in the following and instruction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004041 DCHECK(NameDictionary::GetProbeOffset(i) <
4042 1 << (32 - Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00004043 __ Addu(scratch2, scratch2, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004044 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00004045 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004046 __ srl(scratch2, scratch2, Name::kHashShift);
Ben Murdoch257744e2011-11-30 15:57:28 +00004047 __ And(scratch2, scratch1, scratch2);
4048
4049 // Scale the index by multiplying by the element size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004050 DCHECK(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00004051 // scratch2 = scratch2 * 3.
4052
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004053 __ sll(at, scratch2, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00004054 __ Addu(scratch2, scratch2, at);
4055
4056 // Check if the key is identical to the name.
4057 __ sll(at, scratch2, 2);
4058 __ Addu(scratch2, elements, at);
4059 __ lw(at, FieldMemOperand(scratch2, kElementsStartOffset));
4060 __ Branch(done, eq, name, Operand(at));
4061 }
4062
4063 const int spill_mask =
4064 (ra.bit() | t2.bit() | t1.bit() | t0.bit() |
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004065 a3.bit() | a2.bit() | a1.bit() | a0.bit() | v0.bit()) &
Ben Murdoch257744e2011-11-30 15:57:28 +00004066 ~(scratch1.bit() | scratch2.bit());
4067
4068 __ MultiPush(spill_mask);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004069 if (name.is(a0)) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004070 DCHECK(!elements.is(a1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004071 __ Move(a1, name);
4072 __ Move(a0, elements);
4073 } else {
4074 __ Move(a0, elements);
4075 __ Move(a1, name);
4076 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004077 NameDictionaryLookupStub stub(masm->isolate(), POSITIVE_LOOKUP);
Ben Murdoch257744e2011-11-30 15:57:28 +00004078 __ CallStub(&stub);
4079 __ mov(scratch2, a2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004080 __ mov(at, v0);
Ben Murdoch257744e2011-11-30 15:57:28 +00004081 __ MultiPop(spill_mask);
4082
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004083 __ Branch(done, ne, at, Operand(zero_reg));
4084 __ Branch(miss, eq, at, Operand(zero_reg));
Ben Murdoch257744e2011-11-30 15:57:28 +00004085}
4086
4087
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004088void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004089 // This stub overrides SometimesSetsUpAFrame() to return false. That means
4090 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00004091 // Registers:
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004092 // result: NameDictionary to probe
Ben Murdoch257744e2011-11-30 15:57:28 +00004093 // a1: key
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004094 // dictionary: NameDictionary to probe.
4095 // index: will hold an index of entry if lookup is successful.
4096 // might alias with result_.
Ben Murdoch257744e2011-11-30 15:57:28 +00004097 // Returns:
4098 // result_ is zero if lookup failed, non zero otherwise.
4099
4100 Register result = v0;
4101 Register dictionary = a0;
4102 Register key = a1;
4103 Register index = a2;
4104 Register mask = a3;
4105 Register hash = t0;
4106 Register undefined = t1;
4107 Register entry_key = t2;
4108
4109 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
4110
4111 __ lw(mask, FieldMemOperand(dictionary, kCapacityOffset));
4112 __ sra(mask, mask, kSmiTagSize);
4113 __ Subu(mask, mask, Operand(1));
4114
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004115 __ lw(hash, FieldMemOperand(key, Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00004116
4117 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
4118
4119 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
4120 // Compute the masked index: (hash + i + i * i) & mask.
4121 // Capacity is smi 2^n.
4122 if (i > 0) {
4123 // Add the probe offset (i + i * i) left shifted to avoid right shifting
4124 // the hash in a separate instruction. The value hash + i + i * i is right
4125 // shifted in the following and instruction.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004126 DCHECK(NameDictionary::GetProbeOffset(i) <
4127 1 << (32 - Name::kHashFieldOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00004128 __ Addu(index, hash, Operand(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004129 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
Ben Murdoch257744e2011-11-30 15:57:28 +00004130 } else {
4131 __ mov(index, hash);
4132 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004133 __ srl(index, index, Name::kHashShift);
Ben Murdoch257744e2011-11-30 15:57:28 +00004134 __ And(index, mask, index);
4135
4136 // Scale the index by multiplying by the entry size.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004137 DCHECK(NameDictionary::kEntrySize == 3);
Ben Murdoch257744e2011-11-30 15:57:28 +00004138 // index *= 3.
4139 __ mov(at, index);
4140 __ sll(index, index, 1);
4141 __ Addu(index, index, at);
4142
4143
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004144 DCHECK_EQ(kSmiTagSize, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00004145 __ sll(index, index, 2);
4146 __ Addu(index, index, dictionary);
4147 __ lw(entry_key, FieldMemOperand(index, kElementsStartOffset));
4148
4149 // Having undefined at this place means the name is not contained.
4150 __ Branch(&not_in_dictionary, eq, entry_key, Operand(undefined));
4151
4152 // Stop if found the property.
4153 __ Branch(&in_dictionary, eq, entry_key, Operand(key));
4154
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004155 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
4156 // Check if the entry name is not a unique name.
Ben Murdoch257744e2011-11-30 15:57:28 +00004157 __ lw(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
4158 __ lbu(entry_key,
4159 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004160 __ JumpIfNotUniqueNameInstanceType(entry_key, &maybe_in_dictionary);
Ben Murdoch257744e2011-11-30 15:57:28 +00004161 }
4162 }
4163
4164 __ bind(&maybe_in_dictionary);
4165 // If we are doing negative lookup then probing failure should be
4166 // treated as a lookup success. For positive lookup probing failure
4167 // should be treated as lookup failure.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004168 if (mode() == POSITIVE_LOOKUP) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004169 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00004170 __ mov(result, zero_reg);
Ben Murdoch257744e2011-11-30 15:57:28 +00004171 }
4172
4173 __ bind(&in_dictionary);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004174 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00004175 __ li(result, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00004176
4177 __ bind(&not_in_dictionary);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004178 __ Ret(USE_DELAY_SLOT);
Ben Murdoch257744e2011-11-30 15:57:28 +00004179 __ mov(result, zero_reg);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004180}
4181
4182
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004183void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
4184 Isolate* isolate) {
4185 StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
4186 stub1.GetCode();
4187 // Hydrogen code stubs need stub2 at snapshot time.
4188 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
4189 stub2.GetCode();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004190}
4191
4192
4193// Takes the input in 3 registers: address_ value_ and object_. A pointer to
4194// the value has just been written into the object, now this stub makes sure
4195// we keep the GC informed. The word in the object where the value has been
4196// written is in the address register.
4197void RecordWriteStub::Generate(MacroAssembler* masm) {
4198 Label skip_to_incremental_noncompacting;
4199 Label skip_to_incremental_compacting;
4200
4201 // The first two branch+nop instructions are generated with labels so as to
4202 // get the offset fixed up correctly by the bind(Label*) call. We patch it
4203 // back and forth between a "bne zero_reg, zero_reg, ..." (a nop in this
4204 // position) and the "beq zero_reg, zero_reg, ..." when we start and stop
4205 // incremental heap marking.
4206 // See RecordWriteStub::Patch for details.
4207 __ beq(zero_reg, zero_reg, &skip_to_incremental_noncompacting);
4208 __ nop();
4209 __ beq(zero_reg, zero_reg, &skip_to_incremental_compacting);
4210 __ nop();
4211
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004212 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4213 __ RememberedSetHelper(object(),
4214 address(),
4215 value(),
4216 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004217 MacroAssembler::kReturnAtEnd);
4218 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004219 __ Ret();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004220
4221 __ bind(&skip_to_incremental_noncompacting);
4222 GenerateIncremental(masm, INCREMENTAL);
4223
4224 __ bind(&skip_to_incremental_compacting);
4225 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4226
4227 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
4228 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
4229
4230 PatchBranchIntoNop(masm, 0);
4231 PatchBranchIntoNop(masm, 2 * Assembler::kInstrSize);
4232}
4233
4234
4235void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4236 regs_.Save(masm);
4237
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004238 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004239 Label dont_need_remembered_set;
4240
4241 __ lw(regs_.scratch0(), MemOperand(regs_.address(), 0));
4242 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
4243 regs_.scratch0(),
4244 &dont_need_remembered_set);
4245
4246 __ CheckPageFlag(regs_.object(),
4247 regs_.scratch0(),
4248 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4249 ne,
4250 &dont_need_remembered_set);
4251
4252 // First notify the incremental marker if necessary, then update the
4253 // remembered set.
4254 CheckNeedsToInformIncrementalMarker(
4255 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004256 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004257 regs_.Restore(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004258 __ RememberedSetHelper(object(),
4259 address(),
4260 value(),
4261 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004262 MacroAssembler::kReturnAtEnd);
4263
4264 __ bind(&dont_need_remembered_set);
4265 }
4266
4267 CheckNeedsToInformIncrementalMarker(
4268 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004269 InformIncrementalMarker(masm);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004270 regs_.Restore(masm);
4271 __ Ret();
4272}
4273
4274
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004275void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
4276 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004277 int argument_count = 3;
4278 __ PrepareCallCFunction(argument_count, regs_.scratch0());
4279 Register address =
4280 a0.is(regs_.address()) ? regs_.scratch0() : regs_.address();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004281 DCHECK(!address.is(regs_.object()));
4282 DCHECK(!address.is(a0));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004283 __ Move(address, regs_.address());
4284 __ Move(a0, regs_.object());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004285 __ Move(a1, address);
4286 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004287
4288 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004289 __ CallCFunction(
4290 ExternalReference::incremental_marking_record_write_function(isolate()),
4291 argument_count);
4292 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004293}
4294
4295
4296void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4297 MacroAssembler* masm,
4298 OnNoNeedToInformIncrementalMarker on_no_need,
4299 Mode mode) {
4300 Label on_black;
4301 Label need_incremental;
4302 Label need_incremental_pop_scratch;
4303
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004304 __ And(regs_.scratch0(), regs_.object(), Operand(~Page::kPageAlignmentMask));
4305 __ lw(regs_.scratch1(),
4306 MemOperand(regs_.scratch0(),
4307 MemoryChunk::kWriteBarrierCounterOffset));
4308 __ Subu(regs_.scratch1(), regs_.scratch1(), Operand(1));
4309 __ sw(regs_.scratch1(),
4310 MemOperand(regs_.scratch0(),
4311 MemoryChunk::kWriteBarrierCounterOffset));
4312 __ Branch(&need_incremental, lt, regs_.scratch1(), Operand(zero_reg));
4313
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004314 // Let's look at the color of the object: If it is not black we don't have
4315 // to inform the incremental marker.
4316 __ JumpIfBlack(regs_.object(), regs_.scratch0(), regs_.scratch1(), &on_black);
4317
4318 regs_.Restore(masm);
4319 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004320 __ RememberedSetHelper(object(),
4321 address(),
4322 value(),
4323 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004324 MacroAssembler::kReturnAtEnd);
4325 } else {
4326 __ Ret();
4327 }
4328
4329 __ bind(&on_black);
4330
4331 // Get the value from the slot.
4332 __ lw(regs_.scratch0(), MemOperand(regs_.address(), 0));
4333
4334 if (mode == INCREMENTAL_COMPACTION) {
4335 Label ensure_not_white;
4336
4337 __ CheckPageFlag(regs_.scratch0(), // Contains value.
4338 regs_.scratch1(), // Scratch.
4339 MemoryChunk::kEvacuationCandidateMask,
4340 eq,
4341 &ensure_not_white);
4342
4343 __ CheckPageFlag(regs_.object(),
4344 regs_.scratch1(), // Scratch.
4345 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4346 eq,
4347 &need_incremental);
4348
4349 __ bind(&ensure_not_white);
4350 }
4351
4352 // We need extra registers for this, so we push the object and the address
4353 // register temporarily.
4354 __ Push(regs_.object(), regs_.address());
4355 __ EnsureNotWhite(regs_.scratch0(), // The value.
4356 regs_.scratch1(), // Scratch.
4357 regs_.object(), // Scratch.
4358 regs_.address(), // Scratch.
4359 &need_incremental_pop_scratch);
4360 __ Pop(regs_.object(), regs_.address());
4361
4362 regs_.Restore(masm);
4363 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004364 __ RememberedSetHelper(object(),
4365 address(),
4366 value(),
4367 save_fp_regs_mode(),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004368 MacroAssembler::kReturnAtEnd);
4369 } else {
4370 __ Ret();
4371 }
4372
4373 __ bind(&need_incremental_pop_scratch);
4374 __ Pop(regs_.object(), regs_.address());
4375
4376 __ bind(&need_incremental);
4377
4378 // Fall through when we need to inform the incremental marker.
4379}
4380
4381
4382void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4383 // ----------- S t a t e -------------
4384 // -- a0 : element value to store
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004385 // -- a3 : element index as smi
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004386 // -- sp[0] : array literal index in function as smi
4387 // -- sp[4] : array literal
4388 // clobbers a1, a2, t0
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004389 // -----------------------------------
4390
4391 Label element_done;
4392 Label double_elements;
4393 Label smi_element;
4394 Label slow_elements;
4395 Label fast_elements;
4396
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004397 // Get array literal index, array literal and its map.
4398 __ lw(t0, MemOperand(sp, 0 * kPointerSize));
4399 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
4400 __ lw(a2, FieldMemOperand(a1, JSObject::kMapOffset));
4401
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004402 __ CheckFastElements(a2, t1, &double_elements);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004403 // Check for FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS elements
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004404 __ JumpIfSmi(a0, &smi_element);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004405 __ CheckFastSmiElements(a2, t1, &fast_elements);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004406
4407 // Store into the array literal requires a elements transition. Call into
4408 // the runtime.
4409 __ bind(&slow_elements);
4410 // call.
4411 __ Push(a1, a3, a0);
4412 __ lw(t1, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4413 __ lw(t1, FieldMemOperand(t1, JSFunction::kLiteralsOffset));
4414 __ Push(t1, t0);
4415 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4416
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004417 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004418 __ bind(&fast_elements);
4419 __ lw(t1, FieldMemOperand(a1, JSObject::kElementsOffset));
4420 __ sll(t2, a3, kPointerSizeLog2 - kSmiTagSize);
4421 __ Addu(t2, t1, t2);
4422 __ Addu(t2, t2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4423 __ sw(a0, MemOperand(t2, 0));
4424 // Update the write barrier for the array store.
4425 __ RecordWrite(t1, t2, a0, kRAHasNotBeenSaved, kDontSaveFPRegs,
4426 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
4427 __ Ret(USE_DELAY_SLOT);
4428 __ mov(v0, a0);
4429
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004430 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS,
4431 // and value is Smi.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004432 __ bind(&smi_element);
4433 __ lw(t1, FieldMemOperand(a1, JSObject::kElementsOffset));
4434 __ sll(t2, a3, kPointerSizeLog2 - kSmiTagSize);
4435 __ Addu(t2, t1, t2);
4436 __ sw(a0, FieldMemOperand(t2, FixedArray::kHeaderSize));
4437 __ Ret(USE_DELAY_SLOT);
4438 __ mov(v0, a0);
4439
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004440 // Array literal has ElementsKind of FAST_*_DOUBLE_ELEMENTS.
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004441 __ bind(&double_elements);
4442 __ lw(t1, FieldMemOperand(a1, JSObject::kElementsOffset));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004443 __ StoreNumberToDoubleElements(a0, a3, t1, t3, t5, a2, &slow_elements);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01004444 __ Ret(USE_DELAY_SLOT);
4445 __ mov(v0, a0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004446}
4447
4448
Ben Murdochb8a8cc12014-11-26 15:28:44 +00004449void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4450 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4451 __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
4452 int parameter_count_offset =
4453 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4454 __ lw(a1, MemOperand(fp, parameter_count_offset));
4455 if (function_mode() == JS_FUNCTION_STUB_MODE) {
4456 __ Addu(a1, a1, Operand(1));
4457 }
4458 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4459 __ sll(a1, a1, kPointerSizeLog2);
4460 __ Ret(USE_DELAY_SLOT);
4461 __ Addu(sp, sp, a1);
4462}
4463
4464
4465void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4466 EmitLoadTypeFeedbackVector(masm, VectorLoadICDescriptor::VectorRegister());
4467 VectorLoadStub stub(isolate(), state());
4468 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4469}
4470
4471
4472void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4473 EmitLoadTypeFeedbackVector(masm, VectorLoadICDescriptor::VectorRegister());
4474 VectorKeyedLoadStub stub(isolate());
4475 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4476}
4477
4478
4479void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4480 if (masm->isolate()->function_entry_hook() != NULL) {
4481 ProfileEntryHookStub stub(masm->isolate());
4482 __ push(ra);
4483 __ CallStub(&stub);
4484 __ pop(ra);
4485 }
4486}
4487
4488
4489void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4490 // The entry hook is a "push ra" instruction, followed by a call.
4491 // Note: on MIPS "push" is 2 instruction
4492 const int32_t kReturnAddressDistanceFromFunctionStart =
4493 Assembler::kCallTargetAddressOffset + (2 * Assembler::kInstrSize);
4494
4495 // This should contain all kJSCallerSaved registers.
4496 const RegList kSavedRegs =
4497 kJSCallerSaved | // Caller saved registers.
4498 s5.bit(); // Saved stack pointer.
4499
4500 // We also save ra, so the count here is one higher than the mask indicates.
4501 const int32_t kNumSavedRegs = kNumJSCallerSaved + 2;
4502
4503 // Save all caller-save registers as this may be called from anywhere.
4504 __ MultiPush(kSavedRegs | ra.bit());
4505
4506 // Compute the function's address for the first argument.
4507 __ Subu(a0, ra, Operand(kReturnAddressDistanceFromFunctionStart));
4508
4509 // The caller's return address is above the saved temporaries.
4510 // Grab that for the second argument to the hook.
4511 __ Addu(a1, sp, Operand(kNumSavedRegs * kPointerSize));
4512
4513 // Align the stack if necessary.
4514 int frame_alignment = masm->ActivationFrameAlignment();
4515 if (frame_alignment > kPointerSize) {
4516 __ mov(s5, sp);
4517 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
4518 __ And(sp, sp, Operand(-frame_alignment));
4519 }
4520 __ Subu(sp, sp, kCArgsSlotsSize);
4521#if defined(V8_HOST_ARCH_MIPS)
4522 int32_t entry_hook =
4523 reinterpret_cast<int32_t>(isolate()->function_entry_hook());
4524 __ li(t9, Operand(entry_hook));
4525#else
4526 // Under the simulator we need to indirect the entry hook through a
4527 // trampoline function at a known address.
4528 // It additionally takes an isolate as a third parameter.
4529 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
4530
4531 ApiFunction dispatcher(FUNCTION_ADDR(EntryHookTrampoline));
4532 __ li(t9, Operand(ExternalReference(&dispatcher,
4533 ExternalReference::BUILTIN_CALL,
4534 isolate())));
4535#endif
4536 // Call C function through t9 to conform ABI for PIC.
4537 __ Call(t9);
4538
4539 // Restore the stack pointer if needed.
4540 if (frame_alignment > kPointerSize) {
4541 __ mov(sp, s5);
4542 } else {
4543 __ Addu(sp, sp, kCArgsSlotsSize);
4544 }
4545
4546 // Also pop ra to get Ret(0).
4547 __ MultiPop(kSavedRegs | ra.bit());
4548 __ Ret();
4549}
4550
4551
4552template<class T>
4553static void CreateArrayDispatch(MacroAssembler* masm,
4554 AllocationSiteOverrideMode mode) {
4555 if (mode == DISABLE_ALLOCATION_SITES) {
4556 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
4557 __ TailCallStub(&stub);
4558 } else if (mode == DONT_OVERRIDE) {
4559 int last_index = GetSequenceIndexFromFastElementsKind(
4560 TERMINAL_FAST_ELEMENTS_KIND);
4561 for (int i = 0; i <= last_index; ++i) {
4562 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4563 T stub(masm->isolate(), kind);
4564 __ TailCallStub(&stub, eq, a3, Operand(kind));
4565 }
4566
4567 // If we reached this point there is a problem.
4568 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4569 } else {
4570 UNREACHABLE();
4571 }
4572}
4573
4574
4575static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4576 AllocationSiteOverrideMode mode) {
4577 // a2 - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4578 // a3 - kind (if mode != DISABLE_ALLOCATION_SITES)
4579 // a0 - number of arguments
4580 // a1 - constructor?
4581 // sp[0] - last argument
4582 Label normal_sequence;
4583 if (mode == DONT_OVERRIDE) {
4584 DCHECK(FAST_SMI_ELEMENTS == 0);
4585 DCHECK(FAST_HOLEY_SMI_ELEMENTS == 1);
4586 DCHECK(FAST_ELEMENTS == 2);
4587 DCHECK(FAST_HOLEY_ELEMENTS == 3);
4588 DCHECK(FAST_DOUBLE_ELEMENTS == 4);
4589 DCHECK(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4590
4591 // is the low bit set? If so, we are holey and that is good.
4592 __ And(at, a3, Operand(1));
4593 __ Branch(&normal_sequence, ne, at, Operand(zero_reg));
4594 }
4595
4596 // look at the first argument
4597 __ lw(t1, MemOperand(sp, 0));
4598 __ Branch(&normal_sequence, eq, t1, Operand(zero_reg));
4599
4600 if (mode == DISABLE_ALLOCATION_SITES) {
4601 ElementsKind initial = GetInitialFastElementsKind();
4602 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4603
4604 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4605 holey_initial,
4606 DISABLE_ALLOCATION_SITES);
4607 __ TailCallStub(&stub_holey);
4608
4609 __ bind(&normal_sequence);
4610 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4611 initial,
4612 DISABLE_ALLOCATION_SITES);
4613 __ TailCallStub(&stub);
4614 } else if (mode == DONT_OVERRIDE) {
4615 // We are going to create a holey array, but our kind is non-holey.
4616 // Fix kind and retry (only if we have an allocation site in the slot).
4617 __ Addu(a3, a3, Operand(1));
4618
4619 if (FLAG_debug_code) {
4620 __ lw(t1, FieldMemOperand(a2, 0));
4621 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
4622 __ Assert(eq, kExpectedAllocationSite, t1, Operand(at));
4623 }
4624
4625 // Save the resulting elements kind in type info. We can't just store a3
4626 // in the AllocationSite::transition_info field because elements kind is
4627 // restricted to a portion of the field...upper bits need to be left alone.
4628 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4629 __ lw(t0, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4630 __ Addu(t0, t0, Operand(Smi::FromInt(kFastElementsKindPackedToHoley)));
4631 __ sw(t0, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4632
4633
4634 __ bind(&normal_sequence);
4635 int last_index = GetSequenceIndexFromFastElementsKind(
4636 TERMINAL_FAST_ELEMENTS_KIND);
4637 for (int i = 0; i <= last_index; ++i) {
4638 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4639 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4640 __ TailCallStub(&stub, eq, a3, Operand(kind));
4641 }
4642
4643 // If we reached this point there is a problem.
4644 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4645 } else {
4646 UNREACHABLE();
4647 }
4648}
4649
4650
4651template<class T>
4652static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4653 int to_index = GetSequenceIndexFromFastElementsKind(
4654 TERMINAL_FAST_ELEMENTS_KIND);
4655 for (int i = 0; i <= to_index; ++i) {
4656 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4657 T stub(isolate, kind);
4658 stub.GetCode();
4659 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4660 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4661 stub1.GetCode();
4662 }
4663 }
4664}
4665
4666
4667void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4668 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4669 isolate);
4670 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4671 isolate);
4672 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4673 isolate);
4674}
4675
4676
4677void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4678 Isolate* isolate) {
4679 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4680 for (int i = 0; i < 2; i++) {
4681 // For internal arrays we only need a few things.
4682 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4683 stubh1.GetCode();
4684 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4685 stubh2.GetCode();
4686 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4687 stubh3.GetCode();
4688 }
4689}
4690
4691
4692void ArrayConstructorStub::GenerateDispatchToArrayStub(
4693 MacroAssembler* masm,
4694 AllocationSiteOverrideMode mode) {
4695 if (argument_count() == ANY) {
4696 Label not_zero_case, not_one_case;
4697 __ And(at, a0, a0);
4698 __ Branch(&not_zero_case, ne, at, Operand(zero_reg));
4699 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4700
4701 __ bind(&not_zero_case);
4702 __ Branch(&not_one_case, gt, a0, Operand(1));
4703 CreateArrayDispatchOneArgument(masm, mode);
4704
4705 __ bind(&not_one_case);
4706 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4707 } else if (argument_count() == NONE) {
4708 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4709 } else if (argument_count() == ONE) {
4710 CreateArrayDispatchOneArgument(masm, mode);
4711 } else if (argument_count() == MORE_THAN_ONE) {
4712 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4713 } else {
4714 UNREACHABLE();
4715 }
4716}
4717
4718
4719void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4720 // ----------- S t a t e -------------
4721 // -- a0 : argc (only if argument_count() == ANY)
4722 // -- a1 : constructor
4723 // -- a2 : AllocationSite or undefined
4724 // -- sp[0] : return address
4725 // -- sp[4] : last argument
4726 // -----------------------------------
4727
4728 if (FLAG_debug_code) {
4729 // The array construct code is only set for the global and natives
4730 // builtin Array functions which always have maps.
4731
4732 // Initial map for the builtin Array function should be a map.
4733 __ lw(t0, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4734 // Will both indicate a NULL and a Smi.
4735 __ SmiTst(t0, at);
4736 __ Assert(ne, kUnexpectedInitialMapForArrayFunction,
4737 at, Operand(zero_reg));
4738 __ GetObjectType(t0, t0, t1);
4739 __ Assert(eq, kUnexpectedInitialMapForArrayFunction,
4740 t1, Operand(MAP_TYPE));
4741
4742 // We should either have undefined in a2 or a valid AllocationSite
4743 __ AssertUndefinedOrAllocationSite(a2, t0);
4744 }
4745
4746 Label no_info;
4747 // Get the elements kind and case on that.
4748 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4749 __ Branch(&no_info, eq, a2, Operand(at));
4750
4751 __ lw(a3, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4752 __ SmiUntag(a3);
4753 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4754 __ And(a3, a3, Operand(AllocationSite::ElementsKindBits::kMask));
4755 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4756
4757 __ bind(&no_info);
4758 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
4759}
4760
4761
4762void InternalArrayConstructorStub::GenerateCase(
4763 MacroAssembler* masm, ElementsKind kind) {
4764
4765 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4766 __ TailCallStub(&stub0, lo, a0, Operand(1));
4767
4768 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4769 __ TailCallStub(&stubN, hi, a0, Operand(1));
4770
4771 if (IsFastPackedElementsKind(kind)) {
4772 // We might need to create a holey array
4773 // look at the first argument.
4774 __ lw(at, MemOperand(sp, 0));
4775
4776 InternalArraySingleArgumentConstructorStub
4777 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4778 __ TailCallStub(&stub1_holey, ne, at, Operand(zero_reg));
4779 }
4780
4781 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4782 __ TailCallStub(&stub1);
4783}
4784
4785
4786void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4787 // ----------- S t a t e -------------
4788 // -- a0 : argc
4789 // -- a1 : constructor
4790 // -- sp[0] : return address
4791 // -- sp[4] : last argument
4792 // -----------------------------------
4793
4794 if (FLAG_debug_code) {
4795 // The array construct code is only set for the global and natives
4796 // builtin Array functions which always have maps.
4797
4798 // Initial map for the builtin Array function should be a map.
4799 __ lw(a3, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4800 // Will both indicate a NULL and a Smi.
4801 __ SmiTst(a3, at);
4802 __ Assert(ne, kUnexpectedInitialMapForArrayFunction,
4803 at, Operand(zero_reg));
4804 __ GetObjectType(a3, a3, t0);
4805 __ Assert(eq, kUnexpectedInitialMapForArrayFunction,
4806 t0, Operand(MAP_TYPE));
4807 }
4808
4809 // Figure out the right elements kind.
4810 __ lw(a3, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4811
4812 // Load the map's "bit field 2" into a3. We only need the first byte,
4813 // but the following bit field extraction takes care of that anyway.
4814 __ lbu(a3, FieldMemOperand(a3, Map::kBitField2Offset));
4815 // Retrieve elements_kind from bit field 2.
4816 __ DecodeField<Map::ElementsKindBits>(a3);
4817
4818 if (FLAG_debug_code) {
4819 Label done;
4820 __ Branch(&done, eq, a3, Operand(FAST_ELEMENTS));
4821 __ Assert(
4822 eq, kInvalidElementsKindForInternalArrayOrInternalPackedArray,
4823 a3, Operand(FAST_HOLEY_ELEMENTS));
4824 __ bind(&done);
4825 }
4826
4827 Label fast_elements_case;
4828 __ Branch(&fast_elements_case, eq, a3, Operand(FAST_ELEMENTS));
4829 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4830
4831 __ bind(&fast_elements_case);
4832 GenerateCase(masm, FAST_ELEMENTS);
4833}
4834
4835
4836void CallApiFunctionStub::Generate(MacroAssembler* masm) {
4837 // ----------- S t a t e -------------
4838 // -- a0 : callee
4839 // -- t0 : call_data
4840 // -- a2 : holder
4841 // -- a1 : api_function_address
4842 // -- cp : context
4843 // --
4844 // -- sp[0] : last argument
4845 // -- ...
4846 // -- sp[(argc - 1)* 4] : first argument
4847 // -- sp[argc * 4] : receiver
4848 // -----------------------------------
4849
4850 Register callee = a0;
4851 Register call_data = t0;
4852 Register holder = a2;
4853 Register api_function_address = a1;
4854 Register context = cp;
4855
4856 int argc = this->argc();
4857 bool is_store = this->is_store();
4858 bool call_data_undefined = this->call_data_undefined();
4859
4860 typedef FunctionCallbackArguments FCA;
4861
4862 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
4863 STATIC_ASSERT(FCA::kCalleeIndex == 5);
4864 STATIC_ASSERT(FCA::kDataIndex == 4);
4865 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
4866 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
4867 STATIC_ASSERT(FCA::kIsolateIndex == 1);
4868 STATIC_ASSERT(FCA::kHolderIndex == 0);
4869 STATIC_ASSERT(FCA::kArgsLength == 7);
4870
4871 // Save context, callee and call data.
4872 __ Push(context, callee, call_data);
4873 // Load context from callee.
4874 __ lw(context, FieldMemOperand(callee, JSFunction::kContextOffset));
4875
4876 Register scratch = call_data;
4877 if (!call_data_undefined) {
4878 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4879 }
4880 // Push return value and default return value.
4881 __ Push(scratch, scratch);
4882 __ li(scratch,
4883 Operand(ExternalReference::isolate_address(isolate())));
4884 // Push isolate and holder.
4885 __ Push(scratch, holder);
4886
4887 // Prepare arguments.
4888 __ mov(scratch, sp);
4889
4890 // Allocate the v8::Arguments structure in the arguments' space since
4891 // it's not controlled by GC.
4892 const int kApiStackSpace = 4;
4893
4894 FrameScope frame_scope(masm, StackFrame::MANUAL);
4895 __ EnterExitFrame(false, kApiStackSpace);
4896
4897 DCHECK(!api_function_address.is(a0) && !scratch.is(a0));
4898 // a0 = FunctionCallbackInfo&
4899 // Arguments is after the return address.
4900 __ Addu(a0, sp, Operand(1 * kPointerSize));
4901 // FunctionCallbackInfo::implicit_args_
4902 __ sw(scratch, MemOperand(a0, 0 * kPointerSize));
4903 // FunctionCallbackInfo::values_
4904 __ Addu(at, scratch, Operand((FCA::kArgsLength - 1 + argc) * kPointerSize));
4905 __ sw(at, MemOperand(a0, 1 * kPointerSize));
4906 // FunctionCallbackInfo::length_ = argc
4907 __ li(at, Operand(argc));
4908 __ sw(at, MemOperand(a0, 2 * kPointerSize));
4909 // FunctionCallbackInfo::is_construct_call = 0
4910 __ sw(zero_reg, MemOperand(a0, 3 * kPointerSize));
4911
4912 const int kStackUnwindSpace = argc + FCA::kArgsLength + 1;
4913 ExternalReference thunk_ref =
4914 ExternalReference::invoke_function_callback(isolate());
4915
4916 AllowExternalCallThatCantCauseGC scope(masm);
4917 MemOperand context_restore_operand(
4918 fp, (2 + FCA::kContextSaveIndex) * kPointerSize);
4919 // Stores return the first js argument.
4920 int return_value_offset = 0;
4921 if (is_store) {
4922 return_value_offset = 2 + FCA::kArgsLength;
4923 } else {
4924 return_value_offset = 2 + FCA::kReturnValueOffset;
4925 }
4926 MemOperand return_value_operand(fp, return_value_offset * kPointerSize);
4927
4928 __ CallApiFunctionAndReturn(api_function_address,
4929 thunk_ref,
4930 kStackUnwindSpace,
4931 return_value_operand,
4932 &context_restore_operand);
4933}
4934
4935
4936void CallApiGetterStub::Generate(MacroAssembler* masm) {
4937 // ----------- S t a t e -------------
4938 // -- sp[0] : name
4939 // -- sp[4 - kArgsLength*4] : PropertyCallbackArguments object
4940 // -- ...
4941 // -- a2 : api_function_address
4942 // -----------------------------------
4943
4944 Register api_function_address = ApiGetterDescriptor::function_address();
4945 DCHECK(api_function_address.is(a2));
4946
4947 __ mov(a0, sp); // a0 = Handle<Name>
4948 __ Addu(a1, a0, Operand(1 * kPointerSize)); // a1 = PCA
4949
4950 const int kApiStackSpace = 1;
4951 FrameScope frame_scope(masm, StackFrame::MANUAL);
4952 __ EnterExitFrame(false, kApiStackSpace);
4953
4954 // Create PropertyAccessorInfo instance on the stack above the exit frame with
4955 // a1 (internal::Object** args_) as the data.
4956 __ sw(a1, MemOperand(sp, 1 * kPointerSize));
4957 __ Addu(a1, sp, Operand(1 * kPointerSize)); // a1 = AccessorInfo&
4958
4959 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
4960
4961 ExternalReference thunk_ref =
4962 ExternalReference::invoke_accessor_getter_callback(isolate());
4963 __ CallApiFunctionAndReturn(api_function_address,
4964 thunk_ref,
4965 kStackUnwindSpace,
4966 MemOperand(fp, 6 * kPointerSize),
4967 NULL);
4968}
4969
4970
Steve Block44f0eee2011-05-26 01:26:41 +01004971#undef __
4972
4973} } // namespace v8::internal
4974
4975#endif // V8_TARGET_ARCH_MIPS