blob: 6bbd1a3183a21baba02a4e177e5798b4a3cd95be [file] [log] [blame]
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001// Copyright 2012 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/v8.h"
6
7#if V8_TARGET_ARCH_MIPS64
8
9#include "src/bootstrapper.h"
10#include "src/code-stubs.h"
11#include "src/codegen.h"
12#include "src/ic/handler-compiler.h"
13#include "src/ic/ic.h"
14#include "src/isolate.h"
15#include "src/jsregexp.h"
16#include "src/regexp-macro-assembler.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040017#include "src/runtime/runtime.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000018
19namespace v8 {
20namespace internal {
21
22
23static void InitializeArrayConstructorDescriptor(
24 Isolate* isolate, CodeStubDescriptor* descriptor,
25 int constant_stack_parameter_count) {
26 Address deopt_handler = Runtime::FunctionForId(
27 Runtime::kArrayConstructor)->entry;
28
29 if (constant_stack_parameter_count == 0) {
30 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
31 JS_FUNCTION_STUB_MODE);
32 } else {
33 descriptor->Initialize(a0, deopt_handler, constant_stack_parameter_count,
34 JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
35 }
36}
37
38
39static void InitializeInternalArrayConstructorDescriptor(
40 Isolate* isolate, CodeStubDescriptor* descriptor,
41 int constant_stack_parameter_count) {
42 Address deopt_handler = Runtime::FunctionForId(
43 Runtime::kInternalArrayConstructor)->entry;
44
45 if (constant_stack_parameter_count == 0) {
46 descriptor->Initialize(deopt_handler, constant_stack_parameter_count,
47 JS_FUNCTION_STUB_MODE);
48 } else {
49 descriptor->Initialize(a0, deopt_handler, constant_stack_parameter_count,
50 JS_FUNCTION_STUB_MODE, PASS_ARGUMENTS);
51 }
52}
53
54
55void ArrayNoArgumentConstructorStub::InitializeDescriptor(
56 CodeStubDescriptor* descriptor) {
57 InitializeArrayConstructorDescriptor(isolate(), descriptor, 0);
58}
59
60
61void ArraySingleArgumentConstructorStub::InitializeDescriptor(
62 CodeStubDescriptor* descriptor) {
63 InitializeArrayConstructorDescriptor(isolate(), descriptor, 1);
64}
65
66
67void ArrayNArgumentsConstructorStub::InitializeDescriptor(
68 CodeStubDescriptor* descriptor) {
69 InitializeArrayConstructorDescriptor(isolate(), descriptor, -1);
70}
71
72
73void InternalArrayNoArgumentConstructorStub::InitializeDescriptor(
74 CodeStubDescriptor* descriptor) {
75 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 0);
76}
77
78
79void InternalArraySingleArgumentConstructorStub::InitializeDescriptor(
80 CodeStubDescriptor* descriptor) {
81 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, 1);
82}
83
84
85void InternalArrayNArgumentsConstructorStub::InitializeDescriptor(
86 CodeStubDescriptor* descriptor) {
87 InitializeInternalArrayConstructorDescriptor(isolate(), descriptor, -1);
88}
89
90
91#define __ ACCESS_MASM(masm)
92
93
94static void EmitIdenticalObjectComparison(MacroAssembler* masm,
95 Label* slow,
96 Condition cc);
97static void EmitSmiNonsmiComparison(MacroAssembler* masm,
98 Register lhs,
99 Register rhs,
100 Label* rhs_not_nan,
101 Label* slow,
102 bool strict);
103static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
104 Register lhs,
105 Register rhs);
106
107
108void HydrogenCodeStub::GenerateLightweightMiss(MacroAssembler* masm,
109 ExternalReference miss) {
110 // Update the static counter each time a new code stub is generated.
111 isolate()->counters()->code_stubs()->Increment();
112
113 CallInterfaceDescriptor descriptor = GetCallInterfaceDescriptor();
114 int param_count = descriptor.GetEnvironmentParameterCount();
115 {
116 // Call the runtime system in a fresh internal frame.
117 FrameScope scope(masm, StackFrame::INTERNAL);
118 DCHECK((param_count == 0) ||
119 a0.is(descriptor.GetEnvironmentParameterRegister(param_count - 1)));
120 // Push arguments, adjust sp.
121 __ Dsubu(sp, sp, Operand(param_count * kPointerSize));
122 for (int i = 0; i < param_count; ++i) {
123 // Store argument to stack.
124 __ sd(descriptor.GetEnvironmentParameterRegister(i),
125 MemOperand(sp, (param_count - 1 - i) * kPointerSize));
126 }
127 __ CallExternalReference(miss, param_count);
128 }
129
130 __ Ret();
131}
132
133
134void DoubleToIStub::Generate(MacroAssembler* masm) {
135 Label out_of_range, only_low, negate, done;
136 Register input_reg = source();
137 Register result_reg = destination();
138
139 int double_offset = offset();
140 // Account for saved regs if input is sp.
141 if (input_reg.is(sp)) double_offset += 3 * kPointerSize;
142
143 Register scratch =
144 GetRegisterThatIsNotOneOf(input_reg, result_reg);
145 Register scratch2 =
146 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch);
147 Register scratch3 =
148 GetRegisterThatIsNotOneOf(input_reg, result_reg, scratch, scratch2);
149 DoubleRegister double_scratch = kLithiumScratchDouble;
150
151 __ Push(scratch, scratch2, scratch3);
152 if (!skip_fastpath()) {
153 // Load double input.
154 __ ldc1(double_scratch, MemOperand(input_reg, double_offset));
155
156 // Clear cumulative exception flags and save the FCSR.
157 __ cfc1(scratch2, FCSR);
158 __ ctc1(zero_reg, FCSR);
159
160 // Try a conversion to a signed integer.
161 __ Trunc_w_d(double_scratch, double_scratch);
162 // Move the converted value into the result register.
163 __ mfc1(scratch3, double_scratch);
164
165 // Retrieve and restore the FCSR.
166 __ cfc1(scratch, FCSR);
167 __ ctc1(scratch2, FCSR);
168
169 // Check for overflow and NaNs.
170 __ And(
171 scratch, scratch,
172 kFCSROverflowFlagMask | kFCSRUnderflowFlagMask
173 | kFCSRInvalidOpFlagMask);
174 // If we had no exceptions then set result_reg and we are done.
175 Label error;
176 __ Branch(&error, ne, scratch, Operand(zero_reg));
177 __ Move(result_reg, scratch3);
178 __ Branch(&done);
179 __ bind(&error);
180 }
181
182 // Load the double value and perform a manual truncation.
183 Register input_high = scratch2;
184 Register input_low = scratch3;
185
186 __ lw(input_low, MemOperand(input_reg, double_offset));
187 __ lw(input_high, MemOperand(input_reg, double_offset + kIntSize));
188
189 Label normal_exponent, restore_sign;
190 // Extract the biased exponent in result.
191 __ Ext(result_reg,
192 input_high,
193 HeapNumber::kExponentShift,
194 HeapNumber::kExponentBits);
195
196 // Check for Infinity and NaNs, which should return 0.
197 __ Subu(scratch, result_reg, HeapNumber::kExponentMask);
198 __ Movz(result_reg, zero_reg, scratch);
199 __ Branch(&done, eq, scratch, Operand(zero_reg));
200
201 // Express exponent as delta to (number of mantissa bits + 31).
202 __ Subu(result_reg,
203 result_reg,
204 Operand(HeapNumber::kExponentBias + HeapNumber::kMantissaBits + 31));
205
206 // If the delta is strictly positive, all bits would be shifted away,
207 // which means that we can return 0.
208 __ Branch(&normal_exponent, le, result_reg, Operand(zero_reg));
209 __ mov(result_reg, zero_reg);
210 __ Branch(&done);
211
212 __ bind(&normal_exponent);
213 const int kShiftBase = HeapNumber::kNonMantissaBitsInTopWord - 1;
214 // Calculate shift.
215 __ Addu(scratch, result_reg, Operand(kShiftBase + HeapNumber::kMantissaBits));
216
217 // Save the sign.
218 Register sign = result_reg;
219 result_reg = no_reg;
220 __ And(sign, input_high, Operand(HeapNumber::kSignMask));
221
222 // On ARM shifts > 31 bits are valid and will result in zero. On MIPS we need
223 // to check for this specific case.
224 Label high_shift_needed, high_shift_done;
225 __ Branch(&high_shift_needed, lt, scratch, Operand(32));
226 __ mov(input_high, zero_reg);
227 __ Branch(&high_shift_done);
228 __ bind(&high_shift_needed);
229
230 // Set the implicit 1 before the mantissa part in input_high.
231 __ Or(input_high,
232 input_high,
233 Operand(1 << HeapNumber::kMantissaBitsInTopWord));
234 // Shift the mantissa bits to the correct position.
235 // We don't need to clear non-mantissa bits as they will be shifted away.
236 // If they weren't, it would mean that the answer is in the 32bit range.
237 __ sllv(input_high, input_high, scratch);
238
239 __ bind(&high_shift_done);
240
241 // Replace the shifted bits with bits from the lower mantissa word.
242 Label pos_shift, shift_done;
243 __ li(at, 32);
244 __ subu(scratch, at, scratch);
245 __ Branch(&pos_shift, ge, scratch, Operand(zero_reg));
246
247 // Negate scratch.
248 __ Subu(scratch, zero_reg, scratch);
249 __ sllv(input_low, input_low, scratch);
250 __ Branch(&shift_done);
251
252 __ bind(&pos_shift);
253 __ srlv(input_low, input_low, scratch);
254
255 __ bind(&shift_done);
256 __ Or(input_high, input_high, Operand(input_low));
257 // Restore sign if necessary.
258 __ mov(scratch, sign);
259 result_reg = sign;
260 sign = no_reg;
261 __ Subu(result_reg, zero_reg, input_high);
262 __ Movz(result_reg, input_high, scratch);
263
264 __ bind(&done);
265
266 __ Pop(scratch, scratch2, scratch3);
267 __ Ret();
268}
269
270
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000271// Handle the case where the lhs and rhs are the same object.
272// Equality is almost reflexive (everything but NaN), so this is a test
273// for "identity and not NaN".
274static void EmitIdenticalObjectComparison(MacroAssembler* masm,
275 Label* slow,
276 Condition cc) {
277 Label not_identical;
278 Label heap_number, return_equal;
279 Register exp_mask_reg = t1;
280
281 __ Branch(&not_identical, ne, a0, Operand(a1));
282
283 __ li(exp_mask_reg, Operand(HeapNumber::kExponentMask));
284
285 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
286 // so we do the second best thing - test it ourselves.
287 // They are both equal and they are not both Smis so both of them are not
288 // Smis. If it's not a heap number, then return equal.
289 if (cc == less || cc == greater) {
290 __ GetObjectType(a0, t0, t0);
291 __ Branch(slow, greater, t0, Operand(FIRST_SPEC_OBJECT_TYPE));
292 } else {
293 __ GetObjectType(a0, t0, t0);
294 __ Branch(&heap_number, eq, t0, Operand(HEAP_NUMBER_TYPE));
295 // Comparing JS objects with <=, >= is complicated.
296 if (cc != eq) {
297 __ Branch(slow, greater, t0, Operand(FIRST_SPEC_OBJECT_TYPE));
298 // Normally here we fall through to return_equal, but undefined is
299 // special: (undefined == undefined) == true, but
300 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
301 if (cc == less_equal || cc == greater_equal) {
302 __ Branch(&return_equal, ne, t0, Operand(ODDBALL_TYPE));
303 __ LoadRoot(a6, Heap::kUndefinedValueRootIndex);
304 __ Branch(&return_equal, ne, a0, Operand(a6));
305 DCHECK(is_int16(GREATER) && is_int16(LESS));
306 __ Ret(USE_DELAY_SLOT);
307 if (cc == le) {
308 // undefined <= undefined should fail.
309 __ li(v0, Operand(GREATER));
310 } else {
311 // undefined >= undefined should fail.
312 __ li(v0, Operand(LESS));
313 }
314 }
315 }
316 }
317
318 __ bind(&return_equal);
319 DCHECK(is_int16(GREATER) && is_int16(LESS));
320 __ Ret(USE_DELAY_SLOT);
321 if (cc == less) {
322 __ li(v0, Operand(GREATER)); // Things aren't less than themselves.
323 } else if (cc == greater) {
324 __ li(v0, Operand(LESS)); // Things aren't greater than themselves.
325 } else {
326 __ mov(v0, zero_reg); // Things are <=, >=, ==, === themselves.
327 }
328 // For less and greater we don't have to check for NaN since the result of
329 // x < x is false regardless. For the others here is some code to check
330 // for NaN.
331 if (cc != lt && cc != gt) {
332 __ bind(&heap_number);
333 // It is a heap number, so return non-equal if it's NaN and equal if it's
334 // not NaN.
335
336 // The representation of NaN values has all exponent bits (52..62) set,
337 // and not all mantissa bits (0..51) clear.
338 // Read top bits of double representation (second word of value).
339 __ lwu(a6, FieldMemOperand(a0, HeapNumber::kExponentOffset));
340 // Test that exponent bits are all set.
341 __ And(a7, a6, Operand(exp_mask_reg));
342 // If all bits not set (ne cond), then not a NaN, objects are equal.
343 __ Branch(&return_equal, ne, a7, Operand(exp_mask_reg));
344
345 // Shift out flag and all exponent bits, retaining only mantissa.
346 __ sll(a6, a6, HeapNumber::kNonMantissaBitsInTopWord);
347 // Or with all low-bits of mantissa.
348 __ lwu(a7, FieldMemOperand(a0, HeapNumber::kMantissaOffset));
349 __ Or(v0, a7, Operand(a6));
350 // For equal we already have the right value in v0: Return zero (equal)
351 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
352 // not (it's a NaN). For <= and >= we need to load v0 with the failing
353 // value if it's a NaN.
354 if (cc != eq) {
355 // All-zero means Infinity means equal.
356 __ Ret(eq, v0, Operand(zero_reg));
357 DCHECK(is_int16(GREATER) && is_int16(LESS));
358 __ Ret(USE_DELAY_SLOT);
359 if (cc == le) {
360 __ li(v0, Operand(GREATER)); // NaN <= NaN should fail.
361 } else {
362 __ li(v0, Operand(LESS)); // NaN >= NaN should fail.
363 }
364 }
365 }
366 // No fall through here.
367
368 __ bind(&not_identical);
369}
370
371
372static void EmitSmiNonsmiComparison(MacroAssembler* masm,
373 Register lhs,
374 Register rhs,
375 Label* both_loaded_as_doubles,
376 Label* slow,
377 bool strict) {
378 DCHECK((lhs.is(a0) && rhs.is(a1)) ||
379 (lhs.is(a1) && rhs.is(a0)));
380
381 Label lhs_is_smi;
382 __ JumpIfSmi(lhs, &lhs_is_smi);
383 // Rhs is a Smi.
384 // Check whether the non-smi is a heap number.
385 __ GetObjectType(lhs, t0, t0);
386 if (strict) {
387 // If lhs was not a number and rhs was a Smi then strict equality cannot
388 // succeed. Return non-equal (lhs is already not zero).
389 __ Ret(USE_DELAY_SLOT, ne, t0, Operand(HEAP_NUMBER_TYPE));
390 __ mov(v0, lhs);
391 } else {
392 // Smi compared non-strictly with a non-Smi non-heap-number. Call
393 // the runtime.
394 __ Branch(slow, ne, t0, Operand(HEAP_NUMBER_TYPE));
395 }
396 // Rhs is a smi, lhs is a number.
397 // Convert smi rhs to double.
398 __ SmiUntag(at, rhs);
399 __ mtc1(at, f14);
400 __ cvt_d_w(f14, f14);
401 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
402
403 // We now have both loaded as doubles.
404 __ jmp(both_loaded_as_doubles);
405
406 __ bind(&lhs_is_smi);
407 // Lhs is a Smi. Check whether the non-smi is a heap number.
408 __ GetObjectType(rhs, t0, t0);
409 if (strict) {
410 // If lhs was not a number and rhs was a Smi then strict equality cannot
411 // succeed. Return non-equal.
412 __ Ret(USE_DELAY_SLOT, ne, t0, Operand(HEAP_NUMBER_TYPE));
413 __ li(v0, Operand(1));
414 } else {
415 // Smi compared non-strictly with a non-Smi non-heap-number. Call
416 // the runtime.
417 __ Branch(slow, ne, t0, Operand(HEAP_NUMBER_TYPE));
418 }
419
420 // Lhs is a smi, rhs is a number.
421 // Convert smi lhs to double.
422 __ SmiUntag(at, lhs);
423 __ mtc1(at, f12);
424 __ cvt_d_w(f12, f12);
425 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
426 // Fall through to both_loaded_as_doubles.
427}
428
429
430static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
431 Register lhs,
432 Register rhs) {
433 // If either operand is a JS object or an oddball value, then they are
434 // not equal since their pointers are different.
435 // There is no test for undetectability in strict equality.
436 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
437 Label first_non_object;
438 // Get the type of the first operand into a2 and compare it with
439 // FIRST_SPEC_OBJECT_TYPE.
440 __ GetObjectType(lhs, a2, a2);
441 __ Branch(&first_non_object, less, a2, Operand(FIRST_SPEC_OBJECT_TYPE));
442
443 // Return non-zero.
444 Label return_not_equal;
445 __ bind(&return_not_equal);
446 __ Ret(USE_DELAY_SLOT);
447 __ li(v0, Operand(1));
448
449 __ bind(&first_non_object);
450 // Check for oddballs: true, false, null, undefined.
451 __ Branch(&return_not_equal, eq, a2, Operand(ODDBALL_TYPE));
452
453 __ GetObjectType(rhs, a3, a3);
454 __ Branch(&return_not_equal, greater, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
455
456 // Check for oddballs: true, false, null, undefined.
457 __ Branch(&return_not_equal, eq, a3, Operand(ODDBALL_TYPE));
458
459 // Now that we have the types we might as well check for
460 // internalized-internalized.
461 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
462 __ Or(a2, a2, Operand(a3));
463 __ And(at, a2, Operand(kIsNotStringMask | kIsNotInternalizedMask));
464 __ Branch(&return_not_equal, eq, at, Operand(zero_reg));
465}
466
467
468static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
469 Register lhs,
470 Register rhs,
471 Label* both_loaded_as_doubles,
472 Label* not_heap_numbers,
473 Label* slow) {
474 __ GetObjectType(lhs, a3, a2);
475 __ Branch(not_heap_numbers, ne, a2, Operand(HEAP_NUMBER_TYPE));
476 __ ld(a2, FieldMemOperand(rhs, HeapObject::kMapOffset));
477 // If first was a heap number & second wasn't, go to slow case.
478 __ Branch(slow, ne, a3, Operand(a2));
479
480 // Both are heap numbers. Load them up then jump to the code we have
481 // for that.
482 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
483 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
484
485 __ jmp(both_loaded_as_doubles);
486}
487
488
489// Fast negative check for internalized-to-internalized equality.
490static void EmitCheckForInternalizedStringsOrObjects(MacroAssembler* masm,
491 Register lhs,
492 Register rhs,
493 Label* possible_strings,
494 Label* not_both_strings) {
495 DCHECK((lhs.is(a0) && rhs.is(a1)) ||
496 (lhs.is(a1) && rhs.is(a0)));
497
498 // a2 is object type of rhs.
499 Label object_test;
500 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
501 __ And(at, a2, Operand(kIsNotStringMask));
502 __ Branch(&object_test, ne, at, Operand(zero_reg));
503 __ And(at, a2, Operand(kIsNotInternalizedMask));
504 __ Branch(possible_strings, ne, at, Operand(zero_reg));
505 __ GetObjectType(rhs, a3, a3);
506 __ Branch(not_both_strings, ge, a3, Operand(FIRST_NONSTRING_TYPE));
507 __ And(at, a3, Operand(kIsNotInternalizedMask));
508 __ Branch(possible_strings, ne, at, Operand(zero_reg));
509
510 // Both are internalized strings. We already checked they weren't the same
511 // pointer so they are not equal.
512 __ Ret(USE_DELAY_SLOT);
513 __ li(v0, Operand(1)); // Non-zero indicates not equal.
514
515 __ bind(&object_test);
516 __ Branch(not_both_strings, lt, a2, Operand(FIRST_SPEC_OBJECT_TYPE));
517 __ GetObjectType(rhs, a2, a3);
518 __ Branch(not_both_strings, lt, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
519
520 // If both objects are undetectable, they are equal. Otherwise, they
521 // are not equal, since they are different objects and an object is not
522 // equal to undefined.
523 __ ld(a3, FieldMemOperand(lhs, HeapObject::kMapOffset));
524 __ lbu(a2, FieldMemOperand(a2, Map::kBitFieldOffset));
525 __ lbu(a3, FieldMemOperand(a3, Map::kBitFieldOffset));
526 __ and_(a0, a2, a3);
527 __ And(a0, a0, Operand(1 << Map::kIsUndetectable));
528 __ Ret(USE_DELAY_SLOT);
529 __ xori(v0, a0, 1 << Map::kIsUndetectable);
530}
531
532
533static void CompareICStub_CheckInputType(MacroAssembler* masm, Register input,
534 Register scratch,
535 CompareICState::State expected,
536 Label* fail) {
537 Label ok;
538 if (expected == CompareICState::SMI) {
539 __ JumpIfNotSmi(input, fail);
540 } else if (expected == CompareICState::NUMBER) {
541 __ JumpIfSmi(input, &ok);
542 __ CheckMap(input, scratch, Heap::kHeapNumberMapRootIndex, fail,
543 DONT_DO_SMI_CHECK);
544 }
545 // We could be strict about internalized/string here, but as long as
546 // hydrogen doesn't care, the stub doesn't have to care either.
547 __ bind(&ok);
548}
549
550
551// On entry a1 and a2 are the values to be compared.
552// On exit a0 is 0, positive or negative to indicate the result of
553// the comparison.
554void CompareICStub::GenerateGeneric(MacroAssembler* masm) {
555 Register lhs = a1;
556 Register rhs = a0;
557 Condition cc = GetCondition();
558
559 Label miss;
560 CompareICStub_CheckInputType(masm, lhs, a2, left(), &miss);
561 CompareICStub_CheckInputType(masm, rhs, a3, right(), &miss);
562
563 Label slow; // Call builtin.
564 Label not_smis, both_loaded_as_doubles;
565
566 Label not_two_smis, smi_done;
567 __ Or(a2, a1, a0);
568 __ JumpIfNotSmi(a2, &not_two_smis);
569 __ SmiUntag(a1);
570 __ SmiUntag(a0);
571
572 __ Ret(USE_DELAY_SLOT);
573 __ dsubu(v0, a1, a0);
574 __ bind(&not_two_smis);
575
576 // NOTICE! This code is only reached after a smi-fast-case check, so
577 // it is certain that at least one operand isn't a smi.
578
579 // Handle the case where the objects are identical. Either returns the answer
580 // or goes to slow. Only falls through if the objects were not identical.
581 EmitIdenticalObjectComparison(masm, &slow, cc);
582
583 // If either is a Smi (we know that not both are), then they can only
584 // be strictly equal if the other is a HeapNumber.
585 STATIC_ASSERT(kSmiTag == 0);
586 DCHECK_EQ(0, Smi::FromInt(0));
587 __ And(a6, lhs, Operand(rhs));
588 __ JumpIfNotSmi(a6, &not_smis, a4);
589 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
590 // 1) Return the answer.
591 // 2) Go to slow.
592 // 3) Fall through to both_loaded_as_doubles.
593 // 4) Jump to rhs_not_nan.
594 // In cases 3 and 4 we have found out we were dealing with a number-number
595 // comparison and the numbers have been loaded into f12 and f14 as doubles,
596 // or in GP registers (a0, a1, a2, a3) depending on the presence of the FPU.
597 EmitSmiNonsmiComparison(masm, lhs, rhs,
598 &both_loaded_as_doubles, &slow, strict());
599
600 __ bind(&both_loaded_as_doubles);
601 // f12, f14 are the double representations of the left hand side
602 // and the right hand side if we have FPU. Otherwise a2, a3 represent
603 // left hand side and a0, a1 represent right hand side.
604
605 Label nan;
606 __ li(a4, Operand(LESS));
607 __ li(a5, Operand(GREATER));
608 __ li(a6, Operand(EQUAL));
609
610 // Check if either rhs or lhs is NaN.
611 __ BranchF(NULL, &nan, eq, f12, f14);
612
613 // Check if LESS condition is satisfied. If true, move conditionally
614 // result to v0.
615 if (kArchVariant != kMips64r6) {
616 __ c(OLT, D, f12, f14);
617 __ Movt(v0, a4);
618 // Use previous check to store conditionally to v0 oposite condition
619 // (GREATER). If rhs is equal to lhs, this will be corrected in next
620 // check.
621 __ Movf(v0, a5);
622 // Check if EQUAL condition is satisfied. If true, move conditionally
623 // result to v0.
624 __ c(EQ, D, f12, f14);
625 __ Movt(v0, a6);
626 } else {
627 Label skip;
628 __ BranchF(USE_DELAY_SLOT, &skip, NULL, lt, f12, f14);
629 __ mov(v0, a4); // Return LESS as result.
630
631 __ BranchF(USE_DELAY_SLOT, &skip, NULL, eq, f12, f14);
632 __ mov(v0, a6); // Return EQUAL as result.
633
634 __ mov(v0, a5); // Return GREATER as result.
635 __ bind(&skip);
636 }
637 __ Ret();
638
639 __ bind(&nan);
640 // NaN comparisons always fail.
641 // Load whatever we need in v0 to make the comparison fail.
642 DCHECK(is_int16(GREATER) && is_int16(LESS));
643 __ Ret(USE_DELAY_SLOT);
644 if (cc == lt || cc == le) {
645 __ li(v0, Operand(GREATER));
646 } else {
647 __ li(v0, Operand(LESS));
648 }
649
650
651 __ bind(&not_smis);
652 // At this point we know we are dealing with two different objects,
653 // and neither of them is a Smi. The objects are in lhs_ and rhs_.
654 if (strict()) {
655 // This returns non-equal for some object types, or falls through if it
656 // was not lucky.
657 EmitStrictTwoHeapObjectCompare(masm, lhs, rhs);
658 }
659
660 Label check_for_internalized_strings;
661 Label flat_string_check;
662 // Check for heap-number-heap-number comparison. Can jump to slow case,
663 // or load both doubles and jump to the code that handles
664 // that case. If the inputs are not doubles then jumps to
665 // check_for_internalized_strings.
666 // In this case a2 will contain the type of lhs_.
667 EmitCheckForTwoHeapNumbers(masm,
668 lhs,
669 rhs,
670 &both_loaded_as_doubles,
671 &check_for_internalized_strings,
672 &flat_string_check);
673
674 __ bind(&check_for_internalized_strings);
675 if (cc == eq && !strict()) {
676 // Returns an answer for two internalized strings or two
677 // detectable objects.
678 // Otherwise jumps to string case or not both strings case.
679 // Assumes that a2 is the type of lhs_ on entry.
680 EmitCheckForInternalizedStringsOrObjects(
681 masm, lhs, rhs, &flat_string_check, &slow);
682 }
683
684 // Check for both being sequential one-byte strings,
685 // and inline if that is the case.
686 __ bind(&flat_string_check);
687
688 __ JumpIfNonSmisNotBothSequentialOneByteStrings(lhs, rhs, a2, a3, &slow);
689
690 __ IncrementCounter(isolate()->counters()->string_compare_native(), 1, a2,
691 a3);
692 if (cc == eq) {
693 StringHelper::GenerateFlatOneByteStringEquals(masm, lhs, rhs, a2, a3, a4);
694 } else {
695 StringHelper::GenerateCompareFlatOneByteStrings(masm, lhs, rhs, a2, a3, a4,
696 a5);
697 }
698 // Never falls through to here.
699
700 __ bind(&slow);
701 // Prepare for call to builtin. Push object pointers, a0 (lhs) first,
702 // a1 (rhs) second.
703 __ Push(lhs, rhs);
704 // Figure out which native to call and setup the arguments.
705 Builtins::JavaScript native;
706 if (cc == eq) {
707 native = strict() ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
708 } else {
709 native = Builtins::COMPARE;
710 int ncr; // NaN compare result.
711 if (cc == lt || cc == le) {
712 ncr = GREATER;
713 } else {
714 DCHECK(cc == gt || cc == ge); // Remaining cases.
715 ncr = LESS;
716 }
717 __ li(a0, Operand(Smi::FromInt(ncr)));
718 __ push(a0);
719 }
720
721 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
722 // tagged as a small integer.
723 __ InvokeBuiltin(native, JUMP_FUNCTION);
724
725 __ bind(&miss);
726 GenerateMiss(masm);
727}
728
729
730void StoreRegistersStateStub::Generate(MacroAssembler* masm) {
731 __ mov(t9, ra);
732 __ pop(ra);
733 __ PushSafepointRegisters();
734 __ Jump(t9);
735}
736
737
738void RestoreRegistersStateStub::Generate(MacroAssembler* masm) {
739 __ mov(t9, ra);
740 __ pop(ra);
741 __ PopSafepointRegisters();
742 __ Jump(t9);
743}
744
745
746void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
747 // We don't allow a GC during a store buffer overflow so there is no need to
748 // store the registers in any particular way, but we do have to store and
749 // restore them.
750 __ MultiPush(kJSCallerSaved | ra.bit());
751 if (save_doubles()) {
752 __ MultiPushFPU(kCallerSavedFPU);
753 }
754 const int argument_count = 1;
755 const int fp_argument_count = 0;
756 const Register scratch = a1;
757
758 AllowExternalCallThatCantCauseGC scope(masm);
759 __ PrepareCallCFunction(argument_count, fp_argument_count, scratch);
760 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
761 __ CallCFunction(
762 ExternalReference::store_buffer_overflow_function(isolate()),
763 argument_count);
764 if (save_doubles()) {
765 __ MultiPopFPU(kCallerSavedFPU);
766 }
767
768 __ MultiPop(kJSCallerSaved | ra.bit());
769 __ Ret();
770}
771
772
773void MathPowStub::Generate(MacroAssembler* masm) {
774 const Register base = a1;
775 const Register exponent = MathPowTaggedDescriptor::exponent();
776 DCHECK(exponent.is(a2));
777 const Register heapnumbermap = a5;
778 const Register heapnumber = v0;
779 const DoubleRegister double_base = f2;
780 const DoubleRegister double_exponent = f4;
781 const DoubleRegister double_result = f0;
782 const DoubleRegister double_scratch = f6;
783 const FPURegister single_scratch = f8;
784 const Register scratch = t1;
785 const Register scratch2 = a7;
786
787 Label call_runtime, done, int_exponent;
788 if (exponent_type() == ON_STACK) {
789 Label base_is_smi, unpack_exponent;
790 // The exponent and base are supplied as arguments on the stack.
791 // This can only happen if the stub is called from non-optimized code.
792 // Load input parameters from stack to double registers.
793 __ ld(base, MemOperand(sp, 1 * kPointerSize));
794 __ ld(exponent, MemOperand(sp, 0 * kPointerSize));
795
796 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
797
798 __ UntagAndJumpIfSmi(scratch, base, &base_is_smi);
799 __ ld(scratch, FieldMemOperand(base, JSObject::kMapOffset));
800 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
801
802 __ ldc1(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
803 __ jmp(&unpack_exponent);
804
805 __ bind(&base_is_smi);
806 __ mtc1(scratch, single_scratch);
807 __ cvt_d_w(double_base, single_scratch);
808 __ bind(&unpack_exponent);
809
810 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
811
812 __ ld(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
813 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
814 __ ldc1(double_exponent,
815 FieldMemOperand(exponent, HeapNumber::kValueOffset));
816 } else if (exponent_type() == TAGGED) {
817 // Base is already in double_base.
818 __ UntagAndJumpIfSmi(scratch, exponent, &int_exponent);
819
820 __ ldc1(double_exponent,
821 FieldMemOperand(exponent, HeapNumber::kValueOffset));
822 }
823
824 if (exponent_type() != INTEGER) {
825 Label int_exponent_convert;
826 // Detect integer exponents stored as double.
827 __ EmitFPUTruncate(kRoundToMinusInf,
828 scratch,
829 double_exponent,
830 at,
831 double_scratch,
832 scratch2,
833 kCheckForInexactConversion);
834 // scratch2 == 0 means there was no conversion error.
835 __ Branch(&int_exponent_convert, eq, scratch2, Operand(zero_reg));
836
837 if (exponent_type() == ON_STACK) {
838 // Detect square root case. Crankshaft detects constant +/-0.5 at
839 // compile time and uses DoMathPowHalf instead. We then skip this check
840 // for non-constant cases of +/-0.5 as these hardly occur.
841 Label not_plus_half;
842
843 // Test for 0.5.
844 __ Move(double_scratch, 0.5);
845 __ BranchF(USE_DELAY_SLOT,
846 &not_plus_half,
847 NULL,
848 ne,
849 double_exponent,
850 double_scratch);
851 // double_scratch can be overwritten in the delay slot.
852 // Calculates square root of base. Check for the special case of
853 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400854 __ Move(double_scratch, static_cast<double>(-V8_INFINITY));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000855 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, double_base, double_scratch);
856 __ neg_d(double_result, double_scratch);
857
858 // Add +0 to convert -0 to +0.
859 __ add_d(double_scratch, double_base, kDoubleRegZero);
860 __ sqrt_d(double_result, double_scratch);
861 __ jmp(&done);
862
863 __ bind(&not_plus_half);
864 __ Move(double_scratch, -0.5);
865 __ BranchF(USE_DELAY_SLOT,
866 &call_runtime,
867 NULL,
868 ne,
869 double_exponent,
870 double_scratch);
871 // double_scratch can be overwritten in the delay slot.
872 // Calculates square root of base. Check for the special case of
873 // Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400874 __ Move(double_scratch, static_cast<double>(-V8_INFINITY));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000875 __ BranchF(USE_DELAY_SLOT, &done, NULL, eq, double_base, double_scratch);
876 __ Move(double_result, kDoubleRegZero);
877
878 // Add +0 to convert -0 to +0.
879 __ add_d(double_scratch, double_base, kDoubleRegZero);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400880 __ Move(double_result, 1.);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000881 __ sqrt_d(double_scratch, double_scratch);
882 __ div_d(double_result, double_result, double_scratch);
883 __ jmp(&done);
884 }
885
886 __ push(ra);
887 {
888 AllowExternalCallThatCantCauseGC scope(masm);
889 __ PrepareCallCFunction(0, 2, scratch2);
890 __ MovToFloatParameters(double_base, double_exponent);
891 __ CallCFunction(
892 ExternalReference::power_double_double_function(isolate()),
893 0, 2);
894 }
895 __ pop(ra);
896 __ MovFromFloatResult(double_result);
897 __ jmp(&done);
898
899 __ bind(&int_exponent_convert);
900 }
901
902 // Calculate power with integer exponent.
903 __ bind(&int_exponent);
904
905 // Get two copies of exponent in the registers scratch and exponent.
906 if (exponent_type() == INTEGER) {
907 __ mov(scratch, exponent);
908 } else {
909 // Exponent has previously been stored into scratch as untagged integer.
910 __ mov(exponent, scratch);
911 }
912
913 __ mov_d(double_scratch, double_base); // Back up base.
914 __ Move(double_result, 1.0);
915
916 // Get absolute value of exponent.
917 Label positive_exponent;
918 __ Branch(&positive_exponent, ge, scratch, Operand(zero_reg));
919 __ Dsubu(scratch, zero_reg, scratch);
920 __ bind(&positive_exponent);
921
922 Label while_true, no_carry, loop_end;
923 __ bind(&while_true);
924
925 __ And(scratch2, scratch, 1);
926
927 __ Branch(&no_carry, eq, scratch2, Operand(zero_reg));
928 __ mul_d(double_result, double_result, double_scratch);
929 __ bind(&no_carry);
930
931 __ dsra(scratch, scratch, 1);
932
933 __ Branch(&loop_end, eq, scratch, Operand(zero_reg));
934 __ mul_d(double_scratch, double_scratch, double_scratch);
935
936 __ Branch(&while_true);
937
938 __ bind(&loop_end);
939
940 __ Branch(&done, ge, exponent, Operand(zero_reg));
941 __ Move(double_scratch, 1.0);
942 __ div_d(double_result, double_scratch, double_result);
943 // Test whether result is zero. Bail out to check for subnormal result.
944 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
945 __ BranchF(&done, NULL, ne, double_result, kDoubleRegZero);
946
947 // double_exponent may not contain the exponent value if the input was a
948 // smi. We set it with exponent value before bailing out.
949 __ mtc1(exponent, single_scratch);
950 __ cvt_d_w(double_exponent, single_scratch);
951
952 // Returning or bailing out.
953 Counters* counters = isolate()->counters();
954 if (exponent_type() == ON_STACK) {
955 // The arguments are still on the stack.
956 __ bind(&call_runtime);
957 __ TailCallRuntime(Runtime::kMathPowRT, 2, 1);
958
959 // The stub is called from non-optimized code, which expects the result
960 // as heap number in exponent.
961 __ bind(&done);
962 __ AllocateHeapNumber(
963 heapnumber, scratch, scratch2, heapnumbermap, &call_runtime);
964 __ sdc1(double_result,
965 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
966 DCHECK(heapnumber.is(v0));
967 __ IncrementCounter(counters->math_pow(), 1, scratch, scratch2);
968 __ DropAndRet(2);
969 } else {
970 __ push(ra);
971 {
972 AllowExternalCallThatCantCauseGC scope(masm);
973 __ PrepareCallCFunction(0, 2, scratch);
974 __ MovToFloatParameters(double_base, double_exponent);
975 __ CallCFunction(
976 ExternalReference::power_double_double_function(isolate()),
977 0, 2);
978 }
979 __ pop(ra);
980 __ MovFromFloatResult(double_result);
981
982 __ bind(&done);
983 __ IncrementCounter(counters->math_pow(), 1, scratch, scratch2);
984 __ Ret();
985 }
986}
987
988
989bool CEntryStub::NeedsImmovableCode() {
990 return true;
991}
992
993
994void CodeStub::GenerateStubsAheadOfTime(Isolate* isolate) {
995 CEntryStub::GenerateAheadOfTime(isolate);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000996 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(isolate);
997 StubFailureTrampolineStub::GenerateAheadOfTime(isolate);
998 ArrayConstructorStubBase::GenerateStubsAheadOfTime(isolate);
999 CreateAllocationSiteStub::GenerateAheadOfTime(isolate);
1000 BinaryOpICStub::GenerateAheadOfTime(isolate);
1001 StoreRegistersStateStub::GenerateAheadOfTime(isolate);
1002 RestoreRegistersStateStub::GenerateAheadOfTime(isolate);
1003 BinaryOpICWithAllocationSiteStub::GenerateAheadOfTime(isolate);
1004}
1005
1006
1007void StoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
1008 StoreRegistersStateStub stub(isolate);
1009 stub.GetCode();
1010}
1011
1012
1013void RestoreRegistersStateStub::GenerateAheadOfTime(Isolate* isolate) {
1014 RestoreRegistersStateStub stub(isolate);
1015 stub.GetCode();
1016}
1017
1018
1019void CodeStub::GenerateFPStubs(Isolate* isolate) {
1020 // Generate if not already in cache.
1021 SaveFPRegsMode mode = kSaveFPRegs;
1022 CEntryStub(isolate, 1, mode).GetCode();
1023 StoreBufferOverflowStub(isolate, mode).GetCode();
1024 isolate->set_fp_stubs_generated(true);
1025}
1026
1027
1028void CEntryStub::GenerateAheadOfTime(Isolate* isolate) {
1029 CEntryStub stub(isolate, 1, kDontSaveFPRegs);
1030 stub.GetCode();
1031}
1032
1033
1034void CEntryStub::Generate(MacroAssembler* masm) {
1035 // Called from JavaScript; parameters are on stack as if calling JS function
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001036 // a0: number of arguments including receiver
1037 // a1: pointer to builtin function
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001038 // fp: frame pointer (restored after C call)
1039 // sp: stack pointer (restored as callee's sp after C call)
1040 // cp: current context (C callee-saved)
1041
1042 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1043
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001044 // Compute the argv pointer in a callee-saved register.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001045 __ dsll(s1, a0, kPointerSizeLog2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001046 __ Daddu(s1, sp, s1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001047 __ Dsubu(s1, s1, kPointerSize);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001048
1049 // Enter the exit frame that transitions from JavaScript to C++.
1050 FrameScope scope(masm, StackFrame::MANUAL);
1051 __ EnterExitFrame(save_doubles());
1052
1053 // s0: number of arguments including receiver (C callee-saved)
1054 // s1: pointer to first argument (C callee-saved)
1055 // s2: pointer to builtin function (C callee-saved)
1056
1057 // Prepare arguments for C routine.
1058 // a0 = argc
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001059 __ mov(s0, a0);
1060 __ mov(s2, a1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001061 // a1 = argv (set in the delay slot after find_ra below).
1062
1063 // We are calling compiled C/C++ code. a0 and a1 hold our two arguments. We
1064 // also need to reserve the 4 argument slots on the stack.
1065
1066 __ AssertStackIsAligned();
1067
1068 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
1069
1070 // To let the GC traverse the return address of the exit frames, we need to
1071 // know where the return address is. The CEntryStub is unmovable, so
1072 // we can store the address on the stack to be able to find it again and
1073 // we never have to restore it, because it will not change.
1074 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm);
1075 // This branch-and-link sequence is needed to find the current PC on mips,
1076 // saved to the ra register.
1077 // Use masm-> here instead of the double-underscore macro since extra
1078 // coverage code can interfere with the proper calculation of ra.
1079 Label find_ra;
1080 masm->bal(&find_ra); // bal exposes branch delay slot.
1081 masm->mov(a1, s1);
1082 masm->bind(&find_ra);
1083
1084 // Adjust the value in ra to point to the correct return location, 2nd
1085 // instruction past the real call into C code (the jalr(t9)), and push it.
1086 // This is the return address of the exit frame.
1087 const int kNumInstructionsToJump = 5;
1088 masm->Daddu(ra, ra, kNumInstructionsToJump * kInt32Size);
1089 masm->sd(ra, MemOperand(sp)); // This spot was reserved in EnterExitFrame.
1090 // Stack space reservation moved to the branch delay slot below.
1091 // Stack is still aligned.
1092
1093 // Call the C routine.
1094 masm->mov(t9, s2); // Function pointer to t9 to conform to ABI for PIC.
1095 masm->jalr(t9);
1096 // Set up sp in the delay slot.
1097 masm->daddiu(sp, sp, -kCArgsSlotsSize);
1098 // Make sure the stored 'ra' points to this position.
1099 DCHECK_EQ(kNumInstructionsToJump,
1100 masm->InstructionsGeneratedSince(&find_ra));
1101 }
1102
1103 // Runtime functions should not return 'the hole'. Allowing it to escape may
1104 // lead to crashes in the IC code later.
1105 if (FLAG_debug_code) {
1106 Label okay;
1107 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
1108 __ Branch(&okay, ne, v0, Operand(a4));
1109 __ stop("The hole escaped");
1110 __ bind(&okay);
1111 }
1112
1113 // Check result for exception sentinel.
1114 Label exception_returned;
1115 __ LoadRoot(a4, Heap::kExceptionRootIndex);
1116 __ Branch(&exception_returned, eq, a4, Operand(v0));
1117
1118 ExternalReference pending_exception_address(
1119 Isolate::kPendingExceptionAddress, isolate());
1120
1121 // Check that there is no pending exception, otherwise we
1122 // should have returned the exception sentinel.
1123 if (FLAG_debug_code) {
1124 Label okay;
1125 __ li(a2, Operand(pending_exception_address));
1126 __ ld(a2, MemOperand(a2));
1127 __ LoadRoot(a4, Heap::kTheHoleValueRootIndex);
1128 // Cannot use check here as it attempts to generate call into runtime.
1129 __ Branch(&okay, eq, a4, Operand(a2));
1130 __ stop("Unexpected pending exception");
1131 __ bind(&okay);
1132 }
1133
1134 // Exit C frame and return.
1135 // v0:v1: result
1136 // sp: stack pointer
1137 // fp: frame pointer
1138 // s0: still holds argc (callee-saved).
1139 __ LeaveExitFrame(save_doubles(), s0, true, EMIT_RETURN);
1140
1141 // Handling of exception.
1142 __ bind(&exception_returned);
1143
1144 // Retrieve the pending exception.
1145 __ li(a2, Operand(pending_exception_address));
1146 __ ld(v0, MemOperand(a2));
1147
1148 // Clear the pending exception.
1149 __ li(a3, Operand(isolate()->factory()->the_hole_value()));
1150 __ sd(a3, MemOperand(a2));
1151
1152 // Special handling of termination exceptions which are uncatchable
1153 // by javascript code.
1154 Label throw_termination_exception;
1155 __ LoadRoot(a4, Heap::kTerminationExceptionRootIndex);
1156 __ Branch(&throw_termination_exception, eq, v0, Operand(a4));
1157
1158 // Handle normal exception.
1159 __ Throw(v0);
1160
1161 __ bind(&throw_termination_exception);
1162 __ ThrowUncatchable(v0);
1163}
1164
1165
1166void JSEntryStub::Generate(MacroAssembler* masm) {
1167 Label invoke, handler_entry, exit;
1168 Isolate* isolate = masm->isolate();
1169
1170 // TODO(plind): unify the ABI description here.
1171 // Registers:
1172 // a0: entry address
1173 // a1: function
1174 // a2: receiver
1175 // a3: argc
1176 // a4 (a4): on mips64
1177
1178 // Stack:
1179 // 0 arg slots on mips64 (4 args slots on mips)
1180 // args -- in a4/a4 on mips64, on stack on mips
1181
1182 ProfileEntryHookStub::MaybeCallEntryHook(masm);
1183
1184 // Save callee saved registers on the stack.
1185 __ MultiPush(kCalleeSaved | ra.bit());
1186
1187 // Save callee-saved FPU registers.
1188 __ MultiPushFPU(kCalleeSavedFPU);
1189 // Set up the reserved register for 0.0.
1190 __ Move(kDoubleRegZero, 0.0);
1191
1192 // Load argv in s0 register.
1193 if (kMipsAbi == kN64) {
1194 __ mov(s0, a4); // 5th parameter in mips64 a4 (a4) register.
1195 } else { // Abi O32.
1196 // 5th parameter on stack for O32 abi.
1197 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
1198 offset_to_argv += kNumCalleeSavedFPU * kDoubleSize;
1199 __ ld(s0, MemOperand(sp, offset_to_argv + kCArgsSlotsSize));
1200 }
1201
1202 __ InitializeRootRegister();
1203
1204 // We build an EntryFrame.
1205 __ li(a7, Operand(-1)); // Push a bad frame pointer to fail if it is used.
1206 int marker = type();
1207 __ li(a6, Operand(Smi::FromInt(marker)));
1208 __ li(a5, Operand(Smi::FromInt(marker)));
1209 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate);
1210 __ li(a4, Operand(c_entry_fp));
1211 __ ld(a4, MemOperand(a4));
1212 __ Push(a7, a6, a5, a4);
1213 // Set up frame pointer for the frame to be pushed.
1214 __ daddiu(fp, sp, -EntryFrameConstants::kCallerFPOffset);
1215
1216 // Registers:
1217 // a0: entry_address
1218 // a1: function
1219 // a2: receiver_pointer
1220 // a3: argc
1221 // s0: argv
1222 //
1223 // Stack:
1224 // caller fp |
1225 // function slot | entry frame
1226 // context slot |
1227 // bad fp (0xff...f) |
1228 // callee saved registers + ra
1229 // [ O32: 4 args slots]
1230 // args
1231
1232 // If this is the outermost JS call, set js_entry_sp value.
1233 Label non_outermost_js;
1234 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
1235 __ li(a5, Operand(ExternalReference(js_entry_sp)));
1236 __ ld(a6, MemOperand(a5));
1237 __ Branch(&non_outermost_js, ne, a6, Operand(zero_reg));
1238 __ sd(fp, MemOperand(a5));
1239 __ li(a4, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1240 Label cont;
1241 __ b(&cont);
1242 __ nop(); // Branch delay slot nop.
1243 __ bind(&non_outermost_js);
1244 __ li(a4, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
1245 __ bind(&cont);
1246 __ push(a4);
1247
1248 // Jump to a faked try block that does the invoke, with a faked catch
1249 // block that sets the pending exception.
1250 __ jmp(&invoke);
1251 __ bind(&handler_entry);
1252 handler_offset_ = handler_entry.pos();
1253 // Caught exception: Store result (exception) in the pending exception
1254 // field in the JSEnv and return a failure sentinel. Coming in here the
1255 // fp will be invalid because the PushTryHandler below sets it to 0 to
1256 // signal the existence of the JSEntry frame.
1257 __ li(a4, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
1258 isolate)));
1259 __ sd(v0, MemOperand(a4)); // We come back from 'invoke'. result is in v0.
1260 __ LoadRoot(v0, Heap::kExceptionRootIndex);
1261 __ b(&exit); // b exposes branch delay slot.
1262 __ nop(); // Branch delay slot nop.
1263
1264 // Invoke: Link this frame into the handler chain. There's only one
1265 // handler block in this code object, so its index is 0.
1266 __ bind(&invoke);
1267 __ PushTryHandler(StackHandler::JS_ENTRY, 0);
1268 // If an exception not caught by another handler occurs, this handler
1269 // returns control to the code after the bal(&invoke) above, which
1270 // restores all kCalleeSaved registers (including cp and fp) to their
1271 // saved values before returning a failure to C.
1272
1273 // Clear any pending exceptions.
1274 __ LoadRoot(a5, Heap::kTheHoleValueRootIndex);
1275 __ li(a4, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
1276 isolate)));
1277 __ sd(a5, MemOperand(a4));
1278
1279 // Invoke the function by calling through JS entry trampoline builtin.
1280 // Notice that we cannot store a reference to the trampoline code directly in
1281 // this stub, because runtime stubs are not traversed when doing GC.
1282
1283 // Registers:
1284 // a0: entry_address
1285 // a1: function
1286 // a2: receiver_pointer
1287 // a3: argc
1288 // s0: argv
1289 //
1290 // Stack:
1291 // handler frame
1292 // entry frame
1293 // callee saved registers + ra
1294 // [ O32: 4 args slots]
1295 // args
1296
1297 if (type() == StackFrame::ENTRY_CONSTRUCT) {
1298 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
1299 isolate);
1300 __ li(a4, Operand(construct_entry));
1301 } else {
1302 ExternalReference entry(Builtins::kJSEntryTrampoline, masm->isolate());
1303 __ li(a4, Operand(entry));
1304 }
1305 __ ld(t9, MemOperand(a4)); // Deref address.
1306 // Call JSEntryTrampoline.
1307 __ daddiu(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
1314 // Check if the current stack frame is marked as the outermost JS frame.
1315 Label non_outermost_js_2;
1316 __ pop(a5);
1317 __ Branch(&non_outermost_js_2,
1318 ne,
1319 a5,
1320 Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
1321 __ li(a5, Operand(ExternalReference(js_entry_sp)));
1322 __ sd(zero_reg, MemOperand(a5));
1323 __ bind(&non_outermost_js_2);
1324
1325 // Restore the top frame descriptors from the stack.
1326 __ pop(a5);
1327 __ li(a4, Operand(ExternalReference(Isolate::kCEntryFPAddress,
1328 isolate)));
1329 __ sd(a5, MemOperand(a4));
1330
1331 // Reset the stack to the callee saved registers.
1332 __ daddiu(sp, sp, -EntryFrameConstants::kCallerFPOffset);
1333
1334 // Restore callee-saved fpu registers.
1335 __ MultiPopFPU(kCalleeSavedFPU);
1336
1337 // Restore callee saved registers from the stack.
1338 __ MultiPop(kCalleeSaved | ra.bit());
1339 // Return.
1340 __ Jump(ra);
1341}
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 = a4;
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 Murdochb8a8cc12014-11-26 15:28:44 +00001378// Uses registers a0 to a4.
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//
1383// 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 a4.
1386void InstanceofStub::Generate(MacroAssembler* masm) {
1387 // Call site inlining and patching implies arguments in registers.
1388 DCHECK(HasArgsInRegisters() || !HasCallSiteInlineCheck());
Ben Murdochb8a8cc12014-11-26 15:28:44 +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 = a4; // Prototype of the function.
1395 const Register inline_site = t1;
1396 const Register scratch = a2;
1397
1398 const int32_t kDeltaToLoadBoolResult = 7 * Assembler::kInstrSize;
1399
1400 Label slow, loop, is_instance, is_not_instance, not_js_object;
1401
1402 if (!HasArgsInRegisters()) {
1403 __ ld(object, MemOperand(sp, 1 * kPointerSize));
1404 __ ld(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 Murdochb8a8cc12014-11-26 15:28:44 +00001414 Label miss;
1415 __ LoadRoot(at, Heap::kInstanceofCacheFunctionRootIndex);
1416 __ Branch(&miss, ne, function, Operand(at));
1417 __ LoadRoot(at, Heap::kInstanceofCacheMapRootIndex);
1418 __ Branch(&miss, ne, map, Operand(at));
1419 __ LoadRoot(v0, Heap::kInstanceofCacheAnswerRootIndex);
1420 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
1421
1422 __ bind(&miss);
1423 }
1424
1425 // Get the prototype of the function.
1426 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
1427
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 {
1438 DCHECK(HasArgsInRegisters());
1439 // Patch the (relocated) inlined map check.
1440
1441 // The offset was stored in a4 safepoint slot.
1442 // (See LCodeGen::DoDeferredLInstanceOfKnownGlobal).
1443 __ LoadFromSafepointRegisterSlot(scratch, a4);
1444 __ Dsubu(inline_site, ra, scratch);
1445 // Get the map location in scratch and patch it.
1446 __ GetRelocatedValue(inline_site, scratch, v1); // v1 used as scratch.
1447 __ sd(map, FieldMemOperand(scratch, Cell::kValueOffset));
1448 }
1449
1450 // Register mapping: a3 is object map and a4 is function prototype.
1451 // Get prototype of object into a2.
1452 __ ld(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 __ ld(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
1464 __ ld(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
1465 __ Branch(&loop);
1466
1467 __ bind(&is_instance);
1468 DCHECK(Smi::FromInt(0) == 0);
1469 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 Murdochb8a8cc12014-11-26 15:28:44 +00001475 } else {
1476 // Patch the call site to return true.
1477 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
1478 __ Daddu(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()) {
1483 DCHECK_EQ(Smi::FromInt(0), 0);
1484 __ mov(v0, zero_reg);
1485 }
1486 }
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 Murdochb8a8cc12014-11-26 15:28:44 +00001496 } else {
1497 // Patch the call site to return false.
1498 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
1499 __ Daddu(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 }
1506 }
1507
1508 __ 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 Murdochb8a8cc12014-11-26 15:28:44 +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 Murdochb8a8cc12014-11-26 15:28:44 +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 Murdochb8a8cc12014-11-26 15:28:44 +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 {
1556 {
1557 FrameScope scope(masm, StackFrame::INTERNAL);
1558 __ Push(a0, a1);
1559 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
1560 }
1561 __ 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 }
1567}
1568
1569
1570void 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(a4, a5, VectorLoadICDescriptor::VectorRegister(),
1577 VectorLoadICDescriptor::SlotRegister()));
1578
1579 NamedLoadHandlerCompiler::GenerateLoadFunctionPrototype(masm, receiver, a4,
1580 a5, &miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001581 __ bind(&miss);
1582 PropertyAccessCompiler::TailCallBuiltin(
1583 masm, PropertyAccessCompiler::MissBuiltin(Code::LOAD_IC));
1584}
1585
1586
1587void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
1588 // The displacement is the offset of the last parameter (if any)
1589 // relative to the frame pointer.
1590 const int kDisplacement =
1591 StandardFrameConstants::kCallerSPOffset - kPointerSize;
1592 DCHECK(a1.is(ArgumentsAccessReadDescriptor::index()));
1593 DCHECK(a0.is(ArgumentsAccessReadDescriptor::parameter_count()));
1594
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 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1602 __ ld(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 __ dsubu(a3, a0, a1);
1615 __ SmiScale(a7, a3, kPointerSizeLog2);
1616 __ Daddu(a3, fp, Operand(a7));
1617 __ Ret(USE_DELAY_SLOT);
1618 __ ld(v0, MemOperand(a3, kDisplacement));
1619
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 __ ld(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 __ dsubu(a3, a0, a1);
1629 __ SmiScale(a7, a3, kPointerSizeLog2);
1630 __ Daddu(a3, a2, Operand(a7));
1631 __ Ret(USE_DELAY_SLOT);
1632 __ ld(v0, MemOperand(a3, kDisplacement));
1633
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);
1639}
1640
1641
1642void ArgumentsAccessStub::GenerateNewSloppySlow(MacroAssembler* masm) {
1643 // sp[0] : number of parameters
1644 // sp[4] : receiver displacement
1645 // sp[8] : function
1646 // Check if the calling frame is an arguments adaptor frame.
1647 Label runtime;
1648 __ ld(a3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1649 __ ld(a2, MemOperand(a3, StandardFrameConstants::kContextOffset));
1650 __ Branch(&runtime,
1651 ne,
1652 a2,
1653 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1654
1655 // Patch the arguments.length and the parameters pointer in the current frame.
1656 __ ld(a2, MemOperand(a3, ArgumentsAdaptorFrameConstants::kLengthOffset));
1657 __ sd(a2, MemOperand(sp, 0 * kPointerSize));
1658 __ SmiScale(a7, a2, kPointerSizeLog2);
1659 __ Daddu(a3, a3, Operand(a7));
1660 __ daddiu(a3, a3, StandardFrameConstants::kCallerSPOffset);
1661 __ sd(a3, MemOperand(sp, 1 * kPointerSize));
1662
1663 __ bind(&runtime);
1664 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
1665}
1666
1667
1668void ArgumentsAccessStub::GenerateNewSloppyFast(MacroAssembler* masm) {
1669 // 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 // a6 : allocated object (tagged)
1675 // t1 : mapped parameter count (tagged)
1676
1677 __ ld(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 __ ld(a3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1684 __ ld(a2, MemOperand(a3, StandardFrameConstants::kContextOffset));
1685 __ Branch(&adaptor_frame,
1686 eq,
1687 a2,
1688 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1689
1690 // No adaptor, parameter count = argument count.
1691 __ mov(a2, a1);
1692 __ Branch(&try_allocate);
1693
1694 // We have an adaptor frame. Patch the parameters pointer.
1695 __ bind(&adaptor_frame);
1696 __ ld(a2, MemOperand(a3, ArgumentsAdaptorFrameConstants::kLengthOffset));
1697 __ SmiScale(t2, a2, kPointerSizeLog2);
1698 __ Daddu(a3, a3, Operand(t2));
1699 __ Daddu(a3, a3, Operand(StandardFrameConstants::kCallerSPOffset));
1700 __ sd(a3, MemOperand(sp, 1 * kPointerSize));
1701
1702 // a1 = parameter count (tagged)
1703 // a2 = argument count (tagged)
1704 // Compute the mapped parameter count = min(a1, a2) in a1.
1705 Label skip_min;
1706 __ Branch(&skip_min, lt, a1, Operand(a2));
1707 __ mov(a1, a2);
1708 __ bind(&skip_min);
1709
1710 __ bind(&try_allocate);
1711
1712 // Compute the sizes of backing store, parameter map, and arguments object.
1713 // 1. Parameter map, has 2 extra words containing context and backing store.
1714 const int kParameterMapHeaderSize =
1715 FixedArray::kHeaderSize + 2 * kPointerSize;
1716 // If there are no mapped parameters, we do not need the parameter_map.
1717 Label param_map_size;
1718 DCHECK_EQ(0, Smi::FromInt(0));
1719 __ Branch(USE_DELAY_SLOT, &param_map_size, eq, a1, Operand(zero_reg));
1720 __ mov(t1, zero_reg); // In delay slot: param map size = 0 when a1 == 0.
1721 __ SmiScale(t1, a1, kPointerSizeLog2);
1722 __ daddiu(t1, t1, kParameterMapHeaderSize);
1723 __ bind(&param_map_size);
1724
1725 // 2. Backing store.
1726 __ SmiScale(t2, a2, kPointerSizeLog2);
1727 __ Daddu(t1, t1, Operand(t2));
1728 __ Daddu(t1, t1, Operand(FixedArray::kHeaderSize));
1729
1730 // 3. Arguments object.
1731 __ Daddu(t1, t1, Operand(Heap::kSloppyArgumentsObjectSize));
1732
1733 // Do the allocation of all three objects in one go.
1734 __ Allocate(t1, v0, a3, a4, &runtime, TAG_OBJECT);
1735
1736 // v0 = address of new object(s) (tagged)
1737 // a2 = argument count (smi-tagged)
1738 // Get the arguments boilerplate from the current native context into a4.
1739 const int kNormalOffset =
1740 Context::SlotOffset(Context::SLOPPY_ARGUMENTS_MAP_INDEX);
1741 const int kAliasedOffset =
1742 Context::SlotOffset(Context::ALIASED_ARGUMENTS_MAP_INDEX);
1743
1744 __ ld(a4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1745 __ ld(a4, FieldMemOperand(a4, GlobalObject::kNativeContextOffset));
1746 Label skip2_ne, skip2_eq;
1747 __ Branch(&skip2_ne, ne, a1, Operand(zero_reg));
1748 __ ld(a4, MemOperand(a4, kNormalOffset));
1749 __ bind(&skip2_ne);
1750
1751 __ Branch(&skip2_eq, eq, a1, Operand(zero_reg));
1752 __ ld(a4, MemOperand(a4, kAliasedOffset));
1753 __ bind(&skip2_eq);
1754
1755 // v0 = address of new object (tagged)
1756 // a1 = mapped parameter count (tagged)
1757 // a2 = argument count (smi-tagged)
1758 // a4 = address of arguments map (tagged)
1759 __ sd(a4, FieldMemOperand(v0, JSObject::kMapOffset));
1760 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
1761 __ sd(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
1762 __ sd(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
1763
1764 // Set up the callee in-object property.
1765 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
1766 __ ld(a3, MemOperand(sp, 2 * kPointerSize));
1767 __ AssertNotSmi(a3);
1768 const int kCalleeOffset = JSObject::kHeaderSize +
1769 Heap::kArgumentsCalleeIndex * kPointerSize;
1770 __ sd(a3, FieldMemOperand(v0, kCalleeOffset));
1771
1772 // Use the length (smi tagged) and set that as an in-object property too.
1773 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
1774 const int kLengthOffset = JSObject::kHeaderSize +
1775 Heap::kArgumentsLengthIndex * kPointerSize;
1776 __ sd(a2, FieldMemOperand(v0, kLengthOffset));
1777
1778 // Set up the elements pointer in the allocated arguments object.
1779 // If we allocated a parameter map, a4 will point there, otherwise
1780 // it will point to the backing store.
1781 __ Daddu(a4, v0, Operand(Heap::kSloppyArgumentsObjectSize));
1782 __ sd(a4, FieldMemOperand(v0, JSObject::kElementsOffset));
1783
1784 // v0 = address of new object (tagged)
1785 // a1 = mapped parameter count (tagged)
1786 // a2 = argument count (tagged)
1787 // a4 = address of parameter map or backing store (tagged)
1788 // Initialize parameter map. If there are no mapped arguments, we're done.
1789 Label skip_parameter_map;
1790 Label skip3;
1791 __ Branch(&skip3, ne, a1, Operand(Smi::FromInt(0)));
1792 // Move backing store address to a3, because it is
1793 // expected there when filling in the unmapped arguments.
1794 __ mov(a3, a4);
1795 __ bind(&skip3);
1796
1797 __ Branch(&skip_parameter_map, eq, a1, Operand(Smi::FromInt(0)));
1798
1799 __ LoadRoot(a6, Heap::kSloppyArgumentsElementsMapRootIndex);
1800 __ sd(a6, FieldMemOperand(a4, FixedArray::kMapOffset));
1801 __ Daddu(a6, a1, Operand(Smi::FromInt(2)));
1802 __ sd(a6, FieldMemOperand(a4, FixedArray::kLengthOffset));
1803 __ sd(cp, FieldMemOperand(a4, FixedArray::kHeaderSize + 0 * kPointerSize));
1804 __ SmiScale(t2, a1, kPointerSizeLog2);
1805 __ Daddu(a6, a4, Operand(t2));
1806 __ Daddu(a6, a6, Operand(kParameterMapHeaderSize));
1807 __ sd(a6, FieldMemOperand(a4, FixedArray::kHeaderSize + 1 * kPointerSize));
1808
1809 // Copy the parameter slots and the holes in the arguments.
1810 // We need to fill in mapped_parameter_count slots. They index the context,
1811 // where parameters are stored in reverse order, at
1812 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
1813 // The mapped parameter thus need to get indices
1814 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
1815 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
1816 // We loop from right to left.
1817 Label parameters_loop, parameters_test;
1818 __ mov(a6, a1);
1819 __ ld(t1, MemOperand(sp, 0 * kPointerSize));
1820 __ Daddu(t1, t1, Operand(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
1821 __ Dsubu(t1, t1, Operand(a1));
1822 __ LoadRoot(a7, Heap::kTheHoleValueRootIndex);
1823 __ SmiScale(t2, a6, kPointerSizeLog2);
1824 __ Daddu(a3, a4, Operand(t2));
1825 __ Daddu(a3, a3, Operand(kParameterMapHeaderSize));
1826
1827 // a6 = loop variable (tagged)
1828 // a1 = mapping index (tagged)
1829 // a3 = address of backing store (tagged)
1830 // a4 = address of parameter map (tagged)
1831 // a5 = temporary scratch (a.o., for address calculation)
1832 // a7 = the hole value
1833 __ jmp(&parameters_test);
1834
1835 __ bind(&parameters_loop);
1836
1837 __ Dsubu(a6, a6, Operand(Smi::FromInt(1)));
1838 __ SmiScale(a5, a6, kPointerSizeLog2);
1839 __ Daddu(a5, a5, Operand(kParameterMapHeaderSize - kHeapObjectTag));
1840 __ Daddu(t2, a4, a5);
1841 __ sd(t1, MemOperand(t2));
1842 __ Dsubu(a5, a5, Operand(kParameterMapHeaderSize - FixedArray::kHeaderSize));
1843 __ Daddu(t2, a3, a5);
1844 __ sd(a7, MemOperand(t2));
1845 __ Daddu(t1, t1, Operand(Smi::FromInt(1)));
1846 __ bind(&parameters_test);
1847 __ Branch(&parameters_loop, ne, a6, Operand(Smi::FromInt(0)));
1848
1849 __ bind(&skip_parameter_map);
1850 // a2 = argument count (tagged)
1851 // a3 = address of backing store (tagged)
1852 // a5 = scratch
1853 // Copy arguments header and remaining slots (if there are any).
1854 __ LoadRoot(a5, Heap::kFixedArrayMapRootIndex);
1855 __ sd(a5, FieldMemOperand(a3, FixedArray::kMapOffset));
1856 __ sd(a2, FieldMemOperand(a3, FixedArray::kLengthOffset));
1857
1858 Label arguments_loop, arguments_test;
1859 __ mov(t1, a1);
1860 __ ld(a4, MemOperand(sp, 1 * kPointerSize));
1861 __ SmiScale(t2, t1, kPointerSizeLog2);
1862 __ Dsubu(a4, a4, Operand(t2));
1863 __ jmp(&arguments_test);
1864
1865 __ bind(&arguments_loop);
1866 __ Dsubu(a4, a4, Operand(kPointerSize));
1867 __ ld(a6, MemOperand(a4, 0));
1868 __ SmiScale(t2, t1, kPointerSizeLog2);
1869 __ Daddu(a5, a3, Operand(t2));
1870 __ sd(a6, FieldMemOperand(a5, FixedArray::kHeaderSize));
1871 __ Daddu(t1, t1, Operand(Smi::FromInt(1)));
1872
1873 __ bind(&arguments_test);
1874 __ Branch(&arguments_loop, lt, t1, Operand(a2));
1875
1876 // Return and remove the on-stack parameters.
1877 __ DropAndRet(3);
1878
1879 // Do the runtime call to allocate the arguments object.
1880 // a2 = argument count (tagged)
1881 __ bind(&runtime);
1882 __ sd(a2, MemOperand(sp, 0 * kPointerSize)); // Patch argument count.
1883 __ TailCallRuntime(Runtime::kNewSloppyArguments, 3, 1);
1884}
1885
1886
1887void LoadIndexedInterceptorStub::Generate(MacroAssembler* masm) {
1888 // Return address is in ra.
1889 Label slow;
1890
1891 Register receiver = LoadDescriptor::ReceiverRegister();
1892 Register key = LoadDescriptor::NameRegister();
1893
1894 // Check that the key is an array index, that is Uint32.
1895 __ And(t0, key, Operand(kSmiTagMask | kSmiSignMask));
1896 __ Branch(&slow, ne, t0, Operand(zero_reg));
1897
1898 // Everything is fine, call runtime.
1899 __ Push(receiver, key); // Receiver, key.
1900
1901 // Perform tail call to the entry.
1902 __ TailCallExternalReference(
1903 ExternalReference(IC_Utility(IC::kLoadElementWithInterceptor),
1904 masm->isolate()),
1905 2, 1);
1906
1907 __ bind(&slow);
1908 PropertyAccessCompiler::TailCallBuiltin(
1909 masm, PropertyAccessCompiler::MissBuiltin(Code::KEYED_LOAD_IC));
1910}
1911
1912
1913void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
1914 // sp[0] : number of parameters
1915 // sp[4] : receiver displacement
1916 // sp[8] : function
1917 // Check if the calling frame is an arguments adaptor frame.
1918 Label adaptor_frame, try_allocate, runtime;
1919 __ ld(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
1920 __ ld(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
1921 __ Branch(&adaptor_frame,
1922 eq,
1923 a3,
1924 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
1925
1926 // Get the length from the frame.
1927 __ ld(a1, MemOperand(sp, 0));
1928 __ Branch(&try_allocate);
1929
1930 // Patch the arguments.length and the parameters pointer.
1931 __ bind(&adaptor_frame);
1932 __ ld(a1, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
1933 __ sd(a1, MemOperand(sp, 0));
1934 __ SmiScale(at, a1, kPointerSizeLog2);
1935
1936 __ Daddu(a3, a2, Operand(at));
1937
1938 __ Daddu(a3, a3, Operand(StandardFrameConstants::kCallerSPOffset));
1939 __ sd(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 __ SmiUntag(a1);
1947
1948 __ Daddu(a1, a1, Operand(FixedArray::kHeaderSize / kPointerSize));
1949 __ bind(&add_arguments_object);
1950 __ Daddu(a1, a1, Operand(Heap::kStrictArgumentsObjectSize / kPointerSize));
1951
1952 // Do the allocation of both objects in one go.
1953 __ Allocate(a1, v0, a2, a3, &runtime,
1954 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
1955
1956 // Get the arguments boilerplate from the current native context.
1957 __ ld(a4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_OBJECT_INDEX)));
1958 __ ld(a4, FieldMemOperand(a4, GlobalObject::kNativeContextOffset));
1959 __ ld(a4, MemOperand(a4, Context::SlotOffset(
1960 Context::STRICT_ARGUMENTS_MAP_INDEX)));
1961
1962 __ sd(a4, FieldMemOperand(v0, JSObject::kMapOffset));
1963 __ LoadRoot(a3, Heap::kEmptyFixedArrayRootIndex);
1964 __ sd(a3, FieldMemOperand(v0, JSObject::kPropertiesOffset));
1965 __ sd(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
1966
1967 // Get the length (smi tagged) and set that as an in-object property too.
1968 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
1969 __ ld(a1, MemOperand(sp, 0 * kPointerSize));
1970 __ AssertSmi(a1);
1971 __ sd(a1, FieldMemOperand(v0, JSObject::kHeaderSize +
1972 Heap::kArgumentsLengthIndex * kPointerSize));
1973
1974 Label done;
1975 __ Branch(&done, eq, a1, Operand(zero_reg));
1976
1977 // Get the parameters pointer from the stack.
1978 __ ld(a2, MemOperand(sp, 1 * kPointerSize));
1979
1980 // Set up the elements pointer in the allocated arguments object and
1981 // initialize the header in the elements fixed array.
1982 __ Daddu(a4, v0, Operand(Heap::kStrictArgumentsObjectSize));
1983 __ sd(a4, FieldMemOperand(v0, JSObject::kElementsOffset));
1984 __ LoadRoot(a3, Heap::kFixedArrayMapRootIndex);
1985 __ sd(a3, FieldMemOperand(a4, FixedArray::kMapOffset));
1986 __ sd(a1, FieldMemOperand(a4, FixedArray::kLengthOffset));
1987 // Untag the length for the loop.
1988 __ SmiUntag(a1);
1989
1990
1991 // Copy the fixed array slots.
1992 Label loop;
1993 // Set up a4 to point to the first array slot.
1994 __ Daddu(a4, a4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
1995 __ bind(&loop);
1996 // Pre-decrement a2 with kPointerSize on each iteration.
1997 // Pre-decrement in order to skip receiver.
1998 __ Daddu(a2, a2, Operand(-kPointerSize));
1999 __ ld(a3, MemOperand(a2));
2000 // Post-increment a4 with kPointerSize on each iteration.
2001 __ sd(a3, MemOperand(a4));
2002 __ Daddu(a4, a4, Operand(kPointerSize));
2003 __ Dsubu(a1, a1, Operand(1));
2004 __ Branch(&loop, ne, a1, Operand(zero_reg));
2005
2006 // Return and remove the on-stack parameters.
2007 __ bind(&done);
2008 __ DropAndRet(3);
2009
2010 // Do the runtime call to allocate the arguments object.
2011 __ bind(&runtime);
2012 __ TailCallRuntime(Runtime::kNewStrictArguments, 3, 1);
2013}
2014
2015
2016void RegExpExecStub::Generate(MacroAssembler* masm) {
2017 // Just jump directly to runtime if native RegExp is not selected at compile
2018 // time or if regexp entry in generated code is turned off runtime switch or
2019 // at compilation.
2020#ifdef V8_INTERPRETED_REGEXP
2021 __ TailCallRuntime(Runtime::kRegExpExecRT, 4, 1);
2022#else // V8_INTERPRETED_REGEXP
2023
2024 // Stack frame on entry.
2025 // sp[0]: last_match_info (expected JSArray)
2026 // sp[4]: previous index
2027 // sp[8]: subject string
2028 // sp[12]: JSRegExp object
2029
2030 const int kLastMatchInfoOffset = 0 * kPointerSize;
2031 const int kPreviousIndexOffset = 1 * kPointerSize;
2032 const int kSubjectOffset = 2 * kPointerSize;
2033 const int kJSRegExpOffset = 3 * kPointerSize;
2034
2035 Label runtime;
2036 // Allocation of registers for this function. These are in callee save
2037 // registers and will be preserved by the call to the native RegExp code, as
2038 // this code is called using the normal C calling convention. When calling
2039 // directly from generated code the native RegExp code will not do a GC and
2040 // therefore the content of these registers are safe to use after the call.
2041 // MIPS - using s0..s2, since we are not using CEntry Stub.
2042 Register subject = s0;
2043 Register regexp_data = s1;
2044 Register last_match_info_elements = s2;
2045
2046 // Ensure that a RegExp stack is allocated.
2047 ExternalReference address_of_regexp_stack_memory_address =
2048 ExternalReference::address_of_regexp_stack_memory_address(
2049 isolate());
2050 ExternalReference address_of_regexp_stack_memory_size =
2051 ExternalReference::address_of_regexp_stack_memory_size(isolate());
2052 __ li(a0, Operand(address_of_regexp_stack_memory_size));
2053 __ ld(a0, MemOperand(a0, 0));
2054 __ Branch(&runtime, eq, a0, Operand(zero_reg));
2055
2056 // Check that the first argument is a JSRegExp object.
2057 __ ld(a0, MemOperand(sp, kJSRegExpOffset));
2058 STATIC_ASSERT(kSmiTag == 0);
2059 __ JumpIfSmi(a0, &runtime);
2060 __ GetObjectType(a0, a1, a1);
2061 __ Branch(&runtime, ne, a1, Operand(JS_REGEXP_TYPE));
2062
2063 // Check that the RegExp has been compiled (data contains a fixed array).
2064 __ ld(regexp_data, FieldMemOperand(a0, JSRegExp::kDataOffset));
2065 if (FLAG_debug_code) {
2066 __ SmiTst(regexp_data, a4);
2067 __ Check(nz,
2068 kUnexpectedTypeForRegExpDataFixedArrayExpected,
2069 a4,
2070 Operand(zero_reg));
2071 __ GetObjectType(regexp_data, a0, a0);
2072 __ Check(eq,
2073 kUnexpectedTypeForRegExpDataFixedArrayExpected,
2074 a0,
2075 Operand(FIXED_ARRAY_TYPE));
2076 }
2077
2078 // regexp_data: RegExp data (FixedArray)
2079 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
2080 __ ld(a0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
2081 __ Branch(&runtime, ne, a0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
2082
2083 // regexp_data: RegExp data (FixedArray)
2084 // Check that the number of captures fit in the static offsets vector buffer.
2085 __ ld(a2,
2086 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
2087 // Check (number_of_captures + 1) * 2 <= offsets vector size
2088 // Or number_of_captures * 2 <= offsets vector size - 2
2089 // Or number_of_captures <= offsets vector size / 2 - 1
2090 // Multiplying by 2 comes for free since a2 is smi-tagged.
2091 STATIC_ASSERT(Isolate::kJSRegexpStaticOffsetsVectorSize >= 2);
2092 int temp = Isolate::kJSRegexpStaticOffsetsVectorSize / 2 - 1;
2093 __ Branch(&runtime, hi, a2, Operand(Smi::FromInt(temp)));
2094
2095 // Reset offset for possibly sliced string.
2096 __ mov(t0, zero_reg);
2097 __ ld(subject, MemOperand(sp, kSubjectOffset));
2098 __ JumpIfSmi(subject, &runtime);
2099 __ mov(a3, subject); // Make a copy of the original subject string.
2100 __ ld(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
2101 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
2102 // 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 check_underlying; // (4)
2124 Label seq_string; // (5)
2125 Label not_seq_nor_cons; // (6)
2126 Label external_string; // (7)
2127 Label not_long_external; // (8)
2128
2129 // (1) Sequential string? If yes, go to (5).
2130 __ And(a1,
2131 a0,
2132 Operand(kIsNotStringMask |
2133 kStringRepresentationMask |
2134 kShortExternalStringMask));
2135 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
2136 __ Branch(&seq_string, eq, a1, Operand(zero_reg)); // Go to (5).
2137
2138 // (2) Anything but sequential or cons? If yes, go to (6).
2139 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
2140 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
2141 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
2142 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
2143 // Go to (6).
2144 __ Branch(&not_seq_nor_cons, ge, a1, Operand(kExternalStringTag));
2145
2146 // (3) Cons string. Check that it's flat.
2147 // Replace subject with first string and reload instance type.
2148 __ ld(a0, FieldMemOperand(subject, ConsString::kSecondOffset));
2149 __ LoadRoot(a1, Heap::kempty_stringRootIndex);
2150 __ Branch(&runtime, ne, a0, Operand(a1));
2151 __ ld(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
2152
2153 // (4) Is subject external? If yes, go to (7).
2154 __ bind(&check_underlying);
2155 __ ld(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
2156 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
2157 STATIC_ASSERT(kSeqStringTag == 0);
2158 __ And(at, a0, Operand(kStringRepresentationMask));
2159 // The underlying external string is never a short external string.
2160 STATIC_ASSERT(ExternalString::kMaxShortLength < ConsString::kMinLength);
2161 STATIC_ASSERT(ExternalString::kMaxShortLength < SlicedString::kMinLength);
2162 __ Branch(&external_string, ne, at, Operand(zero_reg)); // Go to (7).
2163
2164 // (5) Sequential string. Load regexp code according to encoding.
2165 __ bind(&seq_string);
2166 // subject: sequential subject string (or look-alike, external string)
2167 // a3: original subject string
2168 // Load previous index and check range before a3 is overwritten. We have to
2169 // use a3 instead of subject here because subject might have been only made
2170 // to look like a sequential string when it actually is an external string.
2171 __ ld(a1, MemOperand(sp, kPreviousIndexOffset));
2172 __ JumpIfNotSmi(a1, &runtime);
2173 __ ld(a3, FieldMemOperand(a3, String::kLengthOffset));
2174 __ Branch(&runtime, ls, a3, Operand(a1));
2175 __ SmiUntag(a1);
2176
2177 STATIC_ASSERT(kStringEncodingMask == 4);
2178 STATIC_ASSERT(kOneByteStringTag == 4);
2179 STATIC_ASSERT(kTwoByteStringTag == 0);
2180 __ And(a0, a0, Operand(kStringEncodingMask)); // Non-zero for one_byte.
2181 __ ld(t9, FieldMemOperand(regexp_data, JSRegExp::kDataOneByteCodeOffset));
2182 __ dsra(a3, a0, 2); // a3 is 1 for one_byte, 0 for UC16 (used below).
2183 __ ld(a5, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset));
2184 __ Movz(t9, a5, a0); // If UC16 (a0 is 0), replace t9 w/kDataUC16CodeOffset.
2185
2186 // (E) Carry on. String handling is done.
2187 // t9: irregexp code
2188 // Check that the irregexp code has been generated for the actual string
2189 // encoding. If it has, the field contains a code object otherwise it contains
2190 // a smi (code flushing support).
2191 __ JumpIfSmi(t9, &runtime);
2192
2193 // a1: previous index
2194 // a3: encoding of subject string (1 if one_byte, 0 if two_byte);
2195 // t9: code
2196 // subject: Subject string
2197 // regexp_data: RegExp data (FixedArray)
2198 // All checks done. Now push arguments for native regexp code.
2199 __ IncrementCounter(isolate()->counters()->regexp_entry_native(),
2200 1, a0, a2);
2201
2202 // Isolates: note we add an additional parameter here (isolate pointer).
2203 const int kRegExpExecuteArguments = 9;
2204 const int kParameterRegisters = (kMipsAbi == kN64) ? 8 : 4;
2205 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
2206
2207 // Stack pointer now points to cell where return address is to be written.
2208 // Arguments are before that on the stack or in registers, meaning we
2209 // treat the return address as argument 5. Thus every argument after that
2210 // needs to be shifted back by 1. Since DirectCEntryStub will handle
2211 // allocating space for the c argument slots, we don't need to calculate
2212 // that into the argument positions on the stack. This is how the stack will
2213 // look (sp meaning the value of sp at this moment):
2214 // Abi n64:
2215 // [sp + 1] - Argument 9
2216 // [sp + 0] - saved ra
2217 // Abi O32:
2218 // [sp + 5] - Argument 9
2219 // [sp + 4] - Argument 8
2220 // [sp + 3] - Argument 7
2221 // [sp + 2] - Argument 6
2222 // [sp + 1] - Argument 5
2223 // [sp + 0] - saved ra
2224
2225 if (kMipsAbi == kN64) {
2226 // Argument 9: Pass current isolate address.
2227 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
2228 __ sd(a0, MemOperand(sp, 1 * kPointerSize));
2229
2230 // Argument 8: Indicate that this is a direct call from JavaScript.
2231 __ li(a7, Operand(1));
2232
2233 // Argument 7: Start (high end) of backtracking stack memory area.
2234 __ li(a0, Operand(address_of_regexp_stack_memory_address));
2235 __ ld(a0, MemOperand(a0, 0));
2236 __ li(a2, Operand(address_of_regexp_stack_memory_size));
2237 __ ld(a2, MemOperand(a2, 0));
2238 __ daddu(a6, a0, a2);
2239
2240 // Argument 6: Set the number of capture registers to zero to force global
2241 // regexps to behave as non-global. This does not affect non-global regexps.
2242 __ mov(a5, zero_reg);
2243
2244 // Argument 5: static offsets vector buffer.
2245 __ li(a4, Operand(
2246 ExternalReference::address_of_static_offsets_vector(isolate())));
2247 } else { // O32.
2248 DCHECK(kMipsAbi == kO32);
2249
2250 // Argument 9: Pass current isolate address.
2251 // CFunctionArgumentOperand handles MIPS stack argument slots.
2252 __ li(a0, Operand(ExternalReference::isolate_address(isolate())));
2253 __ sd(a0, MemOperand(sp, 5 * kPointerSize));
2254
2255 // Argument 8: Indicate that this is a direct call from JavaScript.
2256 __ li(a0, Operand(1));
2257 __ sd(a0, MemOperand(sp, 4 * kPointerSize));
2258
2259 // Argument 7: Start (high end) of backtracking stack memory area.
2260 __ li(a0, Operand(address_of_regexp_stack_memory_address));
2261 __ ld(a0, MemOperand(a0, 0));
2262 __ li(a2, Operand(address_of_regexp_stack_memory_size));
2263 __ ld(a2, MemOperand(a2, 0));
2264 __ daddu(a0, a0, a2);
2265 __ sd(a0, MemOperand(sp, 3 * kPointerSize));
2266
2267 // Argument 6: Set the number of capture registers to zero to force global
2268 // regexps to behave as non-global. This does not affect non-global regexps.
2269 __ mov(a0, zero_reg);
2270 __ sd(a0, MemOperand(sp, 2 * kPointerSize));
2271
2272 // Argument 5: static offsets vector buffer.
2273 __ li(a0, Operand(
2274 ExternalReference::address_of_static_offsets_vector(isolate())));
2275 __ sd(a0, MemOperand(sp, 1 * kPointerSize));
2276 }
2277
2278 // For arguments 4 and 3 get string length, calculate start of string data
2279 // and calculate the shift of the index (0 for one_byte and 1 for two byte).
2280 __ Daddu(t2, subject, Operand(SeqString::kHeaderSize - kHeapObjectTag));
2281 __ Xor(a3, a3, Operand(1)); // 1 for 2-byte str, 0 for 1-byte.
2282 // Load the length from the original subject string from the previous stack
2283 // frame. Therefore we have to use fp, which points exactly to two pointer
2284 // sizes below the previous sp. (Because creating a new stack frame pushes
2285 // the previous fp onto the stack and moves up sp by 2 * kPointerSize.)
2286 __ ld(subject, MemOperand(fp, kSubjectOffset + 2 * kPointerSize));
2287 // If slice offset is not 0, load the length from the original sliced string.
2288 // Argument 4, a3: End of string data
2289 // Argument 3, a2: Start of string data
2290 // Prepare start and end index of the input.
2291 __ dsllv(t1, t0, a3);
2292 __ daddu(t0, t2, t1);
2293 __ dsllv(t1, a1, a3);
2294 __ daddu(a2, t0, t1);
2295
2296 __ ld(t2, FieldMemOperand(subject, String::kLengthOffset));
2297
2298 __ SmiUntag(t2);
2299 __ dsllv(t1, t2, a3);
2300 __ daddu(a3, t0, t1);
2301 // Argument 2 (a1): Previous index.
2302 // Already there
2303
2304 // Argument 1 (a0): Subject string.
2305 __ mov(a0, subject);
2306
2307 // Locate the code entry and call it.
2308 __ Daddu(t9, t9, Operand(Code::kHeaderSize - kHeapObjectTag));
2309 DirectCEntryStub stub(isolate());
2310 stub.GenerateCall(masm, t9);
2311
2312 __ LeaveExitFrame(false, no_reg, true);
2313
2314 // v0: result
2315 // subject: subject string (callee saved)
2316 // regexp_data: RegExp data (callee saved)
2317 // last_match_info_elements: Last match info elements (callee saved)
2318 // Check the result.
2319 Label success;
2320 __ Branch(&success, eq, v0, Operand(1));
2321 // We expect exactly one result since we force the called regexp to behave
2322 // as non-global.
2323 Label failure;
2324 __ Branch(&failure, eq, v0, Operand(NativeRegExpMacroAssembler::FAILURE));
2325 // If not exception it can only be retry. Handle that in the runtime system.
2326 __ Branch(&runtime, ne, v0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
2327 // Result must now be exception. If there is no pending exception already a
2328 // stack overflow (on the backtrack stack) was detected in RegExp code but
2329 // haven't created the exception yet. Handle that in the runtime system.
2330 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
2331 __ li(a1, Operand(isolate()->factory()->the_hole_value()));
2332 __ li(a2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
2333 isolate())));
2334 __ ld(v0, MemOperand(a2, 0));
2335 __ Branch(&runtime, eq, v0, Operand(a1));
2336
2337 __ sd(a1, MemOperand(a2, 0)); // Clear pending exception.
2338
2339 // Check if the exception is a termination. If so, throw as uncatchable.
2340 __ LoadRoot(a0, Heap::kTerminationExceptionRootIndex);
2341 Label termination_exception;
2342 __ Branch(&termination_exception, eq, v0, Operand(a0));
2343
2344 __ Throw(v0);
2345
2346 __ bind(&termination_exception);
2347 __ ThrowUncatchable(v0);
2348
2349 __ bind(&failure);
2350 // For failure and exception return null.
2351 __ li(v0, Operand(isolate()->factory()->null_value()));
2352 __ DropAndRet(4);
2353
2354 // Process the result from the native regexp code.
2355 __ bind(&success);
2356
2357 __ lw(a1, UntagSmiFieldMemOperand(
2358 regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
2359 // Calculate number of capture registers (number_of_captures + 1) * 2.
2360 __ Daddu(a1, a1, Operand(1));
2361 __ dsll(a1, a1, 1); // Multiply by 2.
2362
2363 __ ld(a0, MemOperand(sp, kLastMatchInfoOffset));
2364 __ JumpIfSmi(a0, &runtime);
2365 __ GetObjectType(a0, a2, a2);
2366 __ Branch(&runtime, ne, a2, Operand(JS_ARRAY_TYPE));
2367 // Check that the JSArray is in fast case.
2368 __ ld(last_match_info_elements,
2369 FieldMemOperand(a0, JSArray::kElementsOffset));
2370 __ ld(a0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
2371 __ LoadRoot(at, Heap::kFixedArrayMapRootIndex);
2372 __ Branch(&runtime, ne, a0, Operand(at));
2373 // Check that the last match info has space for the capture registers and the
2374 // additional information.
2375 __ ld(a0,
2376 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
2377 __ Daddu(a2, a1, Operand(RegExpImpl::kLastMatchOverhead));
2378
2379 __ SmiUntag(at, a0);
2380 __ Branch(&runtime, gt, a2, Operand(at));
2381
2382 // a1: number of capture registers
2383 // subject: subject string
2384 // Store the capture count.
2385 __ SmiTag(a2, a1); // To smi.
2386 __ sd(a2, FieldMemOperand(last_match_info_elements,
2387 RegExpImpl::kLastCaptureCountOffset));
2388 // Store last subject and last input.
2389 __ sd(subject,
2390 FieldMemOperand(last_match_info_elements,
2391 RegExpImpl::kLastSubjectOffset));
2392 __ mov(a2, subject);
2393 __ RecordWriteField(last_match_info_elements,
2394 RegExpImpl::kLastSubjectOffset,
2395 subject,
2396 a7,
2397 kRAHasNotBeenSaved,
2398 kDontSaveFPRegs);
2399 __ mov(subject, a2);
2400 __ sd(subject,
2401 FieldMemOperand(last_match_info_elements,
2402 RegExpImpl::kLastInputOffset));
2403 __ RecordWriteField(last_match_info_elements,
2404 RegExpImpl::kLastInputOffset,
2405 subject,
2406 a7,
2407 kRAHasNotBeenSaved,
2408 kDontSaveFPRegs);
2409
2410 // Get the static offsets vector filled by the native regexp code.
2411 ExternalReference address_of_static_offsets_vector =
2412 ExternalReference::address_of_static_offsets_vector(isolate());
2413 __ li(a2, Operand(address_of_static_offsets_vector));
2414
2415 // a1: number of capture registers
2416 // a2: offsets vector
2417 Label next_capture, done;
2418 // Capture register counter starts from number of capture registers and
2419 // counts down until wrapping after zero.
2420 __ Daddu(a0,
2421 last_match_info_elements,
2422 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
2423 __ bind(&next_capture);
2424 __ Dsubu(a1, a1, Operand(1));
2425 __ Branch(&done, lt, a1, Operand(zero_reg));
2426 // Read the value from the static offsets vector buffer.
2427 __ lw(a3, MemOperand(a2, 0));
2428 __ daddiu(a2, a2, kIntSize);
2429 // Store the smi value in the last match info.
2430 __ SmiTag(a3);
2431 __ sd(a3, MemOperand(a0, 0));
2432 __ Branch(&next_capture, USE_DELAY_SLOT);
2433 __ daddiu(a0, a0, kPointerSize); // In branch delay slot.
2434
2435 __ bind(&done);
2436
2437 // Return last match info.
2438 __ ld(v0, MemOperand(sp, kLastMatchInfoOffset));
2439 __ DropAndRet(4);
2440
2441 // Do the runtime call to execute the regexp.
2442 __ bind(&runtime);
2443 __ TailCallRuntime(Runtime::kRegExpExecRT, 4, 1);
2444
2445 // Deferred code for string handling.
2446 // (6) Not a long external string? If yes, go to (8).
2447 __ bind(&not_seq_nor_cons);
2448 // Go to (8).
2449 __ Branch(&not_long_external, gt, a1, Operand(kExternalStringTag));
2450
2451 // (7) External string. Make it, offset-wise, look like a sequential string.
2452 __ bind(&external_string);
2453 __ ld(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
2454 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
2455 if (FLAG_debug_code) {
2456 // Assert that we do not have a cons or slice (indirect strings) here.
2457 // Sequential strings have already been ruled out.
2458 __ And(at, a0, Operand(kIsIndirectStringMask));
2459 __ Assert(eq,
2460 kExternalStringExpectedButNotFound,
2461 at,
2462 Operand(zero_reg));
2463 }
2464 __ ld(subject,
2465 FieldMemOperand(subject, ExternalString::kResourceDataOffset));
2466 // Move the pointer so that offset-wise, it looks like a sequential string.
2467 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
2468 __ Dsubu(subject,
2469 subject,
2470 SeqTwoByteString::kHeaderSize - kHeapObjectTag);
2471 __ jmp(&seq_string); // Go to (5).
2472
2473 // (8) Short external string or not a string? If yes, bail out to runtime.
2474 __ bind(&not_long_external);
2475 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
2476 __ And(at, a1, Operand(kIsNotStringMask | kShortExternalStringMask));
2477 __ Branch(&runtime, ne, at, Operand(zero_reg));
2478
2479 // (9) Sliced string. Replace subject with parent. Go to (4).
2480 // Load offset into t0 and replace subject string with parent.
2481 __ ld(t0, FieldMemOperand(subject, SlicedString::kOffsetOffset));
2482 __ SmiUntag(t0);
2483 __ ld(subject, FieldMemOperand(subject, SlicedString::kParentOffset));
2484 __ jmp(&check_underlying); // Go to (4).
2485#endif // V8_INTERPRETED_REGEXP
2486}
2487
2488
2489static void GenerateRecordCallTarget(MacroAssembler* masm) {
2490 // Cache the called function in a feedback vector slot. Cache states
2491 // are uninitialized, monomorphic (indicated by a JSFunction), and
2492 // megamorphic.
2493 // a0 : number of arguments to the construct function
2494 // a1 : the function to call
2495 // a2 : Feedback vector
2496 // a3 : slot in feedback vector (Smi)
2497 Label initialize, done, miss, megamorphic, not_array_function;
2498
2499 DCHECK_EQ(*TypeFeedbackVector::MegamorphicSentinel(masm->isolate()),
2500 masm->isolate()->heap()->megamorphic_symbol());
2501 DCHECK_EQ(*TypeFeedbackVector::UninitializedSentinel(masm->isolate()),
2502 masm->isolate()->heap()->uninitialized_symbol());
2503
2504 // Load the cache state into a4.
2505 __ dsrl(a4, a3, 32 - kPointerSizeLog2);
2506 __ Daddu(a4, a2, Operand(a4));
2507 __ ld(a4, FieldMemOperand(a4, FixedArray::kHeaderSize));
2508
2509 // A monomorphic cache hit or an already megamorphic state: invoke the
2510 // function without changing the state.
2511 __ Branch(&done, eq, a4, Operand(a1));
2512
2513 if (!FLAG_pretenuring_call_new) {
2514 // If we came here, we need to see if we are the array function.
2515 // If we didn't have a matching function, and we didn't find the megamorph
2516 // sentinel, then we have in the slot either some other function or an
2517 // AllocationSite. Do a map check on the object in a3.
2518 __ ld(a5, FieldMemOperand(a4, 0));
2519 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2520 __ Branch(&miss, ne, a5, Operand(at));
2521
2522 // Make sure the function is the Array() function
2523 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, a4);
2524 __ Branch(&megamorphic, ne, a1, Operand(a4));
2525 __ jmp(&done);
2526 }
2527
2528 __ bind(&miss);
2529
2530 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
2531 // megamorphic.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002532 __ LoadRoot(at, Heap::kuninitialized_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002533 __ Branch(&initialize, eq, a4, Operand(at));
2534 // MegamorphicSentinel is an immortal immovable object (undefined) so no
2535 // write-barrier is needed.
2536 __ bind(&megamorphic);
2537 __ dsrl(a4, a3, 32- kPointerSizeLog2);
2538 __ Daddu(a4, a2, Operand(a4));
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002539 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002540 __ sd(at, FieldMemOperand(a4, FixedArray::kHeaderSize));
2541 __ jmp(&done);
2542
2543 // An uninitialized cache is patched with the function.
2544 __ bind(&initialize);
2545 if (!FLAG_pretenuring_call_new) {
2546 // Make sure the function is the Array() function.
2547 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, a4);
2548 __ Branch(&not_array_function, ne, a1, Operand(a4));
2549
2550 // The target function is the Array constructor,
2551 // Create an AllocationSite if we don't already have it, store it in the
2552 // slot.
2553 {
2554 FrameScope scope(masm, StackFrame::INTERNAL);
2555 const RegList kSavedRegs =
2556 1 << 4 | // a0
2557 1 << 5 | // a1
2558 1 << 6 | // a2
2559 1 << 7; // a3
2560
2561 // Arguments register must be smi-tagged to call out.
2562 __ SmiTag(a0);
2563 __ MultiPush(kSavedRegs);
2564
2565 CreateAllocationSiteStub create_stub(masm->isolate());
2566 __ CallStub(&create_stub);
2567
2568 __ MultiPop(kSavedRegs);
2569 __ SmiUntag(a0);
2570 }
2571 __ Branch(&done);
2572
2573 __ bind(&not_array_function);
2574 }
2575
2576 __ dsrl(a4, a3, 32 - kPointerSizeLog2);
2577 __ Daddu(a4, a2, Operand(a4));
2578 __ Daddu(a4, a4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2579 __ sd(a1, MemOperand(a4, 0));
2580
2581 __ Push(a4, a2, a1);
2582 __ RecordWrite(a2, a4, a1, kRAHasNotBeenSaved, kDontSaveFPRegs,
2583 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2584 __ Pop(a4, a2, a1);
2585
2586 __ bind(&done);
2587}
2588
2589
2590static void EmitContinueIfStrictOrNative(MacroAssembler* masm, Label* cont) {
2591 __ ld(a3, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2592
2593 // Do not transform the receiver for strict mode functions.
2594 int32_t strict_mode_function_mask =
2595 1 << SharedFunctionInfo::kStrictModeBitWithinByte ;
2596 // Do not transform the receiver for native (Compilerhints already in a3).
2597 int32_t native_mask = 1 << SharedFunctionInfo::kNativeBitWithinByte;
2598
2599 __ lbu(a4, FieldMemOperand(a3, SharedFunctionInfo::kStrictModeByteOffset));
2600 __ And(at, a4, Operand(strict_mode_function_mask));
2601 __ Branch(cont, ne, at, Operand(zero_reg));
2602 __ lbu(a4, FieldMemOperand(a3, SharedFunctionInfo::kNativeByteOffset));
2603 __ And(at, a4, Operand(native_mask));
2604 __ Branch(cont, ne, at, Operand(zero_reg));
2605}
2606
2607
2608static void EmitSlowCase(MacroAssembler* masm,
2609 int argc,
2610 Label* non_function) {
2611 // Check for function proxy.
2612 __ Branch(non_function, ne, a4, Operand(JS_FUNCTION_PROXY_TYPE));
2613 __ push(a1); // put proxy as additional argument
2614 __ li(a0, Operand(argc + 1, RelocInfo::NONE32));
2615 __ mov(a2, zero_reg);
2616 __ GetBuiltinFunction(a1, Builtins::CALL_FUNCTION_PROXY);
2617 {
2618 Handle<Code> adaptor =
2619 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
2620 __ Jump(adaptor, RelocInfo::CODE_TARGET);
2621 }
2622
2623 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
2624 // of the original receiver from the call site).
2625 __ bind(non_function);
2626 __ sd(a1, MemOperand(sp, argc * kPointerSize));
2627 __ li(a0, Operand(argc)); // Set up the number of arguments.
2628 __ mov(a2, zero_reg);
2629 __ GetBuiltinFunction(a1, Builtins::CALL_NON_FUNCTION);
2630 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
2631 RelocInfo::CODE_TARGET);
2632}
2633
2634
2635static void EmitWrapCase(MacroAssembler* masm, int argc, Label* cont) {
2636 // Wrap the receiver and patch it back onto the stack.
2637 { FrameScope frame_scope(masm, StackFrame::INTERNAL);
2638 __ Push(a1, a3);
2639 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_FUNCTION);
2640 __ pop(a1);
2641 }
2642 __ Branch(USE_DELAY_SLOT, cont);
2643 __ sd(v0, MemOperand(sp, argc * kPointerSize));
2644}
2645
2646
2647static void CallFunctionNoFeedback(MacroAssembler* masm,
2648 int argc, bool needs_checks,
2649 bool call_as_method) {
2650 // a1 : the function to call
2651 Label slow, non_function, wrap, cont;
2652
2653 if (needs_checks) {
2654 // Check that the function is really a JavaScript function.
2655 // a1: pushed function (to be verified)
2656 __ JumpIfSmi(a1, &non_function);
2657
2658 // Goto slow case if we do not have a function.
2659 __ GetObjectType(a1, a4, a4);
2660 __ Branch(&slow, ne, a4, Operand(JS_FUNCTION_TYPE));
2661 }
2662
2663 // Fast-case: Invoke the function now.
2664 // a1: pushed function
2665 ParameterCount actual(argc);
2666
2667 if (call_as_method) {
2668 if (needs_checks) {
2669 EmitContinueIfStrictOrNative(masm, &cont);
2670 }
2671
2672 // Compute the receiver in sloppy mode.
2673 __ ld(a3, MemOperand(sp, argc * kPointerSize));
2674
2675 if (needs_checks) {
2676 __ JumpIfSmi(a3, &wrap);
2677 __ GetObjectType(a3, a4, a4);
2678 __ Branch(&wrap, lt, a4, Operand(FIRST_SPEC_OBJECT_TYPE));
2679 } else {
2680 __ jmp(&wrap);
2681 }
2682
2683 __ bind(&cont);
2684 }
2685 __ InvokeFunction(a1, actual, JUMP_FUNCTION, NullCallWrapper());
2686
2687 if (needs_checks) {
2688 // Slow-case: Non-function called.
2689 __ bind(&slow);
2690 EmitSlowCase(masm, argc, &non_function);
2691 }
2692
2693 if (call_as_method) {
2694 __ bind(&wrap);
2695 // Wrap the receiver and patch it back onto the stack.
2696 EmitWrapCase(masm, argc, &cont);
2697 }
2698}
2699
2700
2701void CallFunctionStub::Generate(MacroAssembler* masm) {
2702 CallFunctionNoFeedback(masm, argc(), NeedsChecks(), CallAsMethod());
2703}
2704
2705
2706void CallConstructStub::Generate(MacroAssembler* masm) {
2707 // a0 : number of arguments
2708 // a1 : the function to call
2709 // a2 : feedback vector
2710 // a3 : (only if a2 is not undefined) slot in feedback vector (Smi)
2711 Label slow, non_function_call;
2712 // Check that the function is not a smi.
2713 __ JumpIfSmi(a1, &non_function_call);
2714 // Check that the function is a JSFunction.
2715 __ GetObjectType(a1, a4, a4);
2716 __ Branch(&slow, ne, a4, Operand(JS_FUNCTION_TYPE));
2717
2718 if (RecordCallTarget()) {
2719 GenerateRecordCallTarget(masm);
2720
2721 __ dsrl(at, a3, 32 - kPointerSizeLog2);
2722 __ Daddu(a5, a2, at);
2723 if (FLAG_pretenuring_call_new) {
2724 // Put the AllocationSite from the feedback vector into a2.
2725 // By adding kPointerSize we encode that we know the AllocationSite
2726 // entry is at the feedback vector slot given by a3 + 1.
2727 __ ld(a2, FieldMemOperand(a5, FixedArray::kHeaderSize + kPointerSize));
2728 } else {
2729 Label feedback_register_initialized;
2730 // Put the AllocationSite from the feedback vector into a2, or undefined.
2731 __ ld(a2, FieldMemOperand(a5, FixedArray::kHeaderSize));
2732 __ ld(a5, FieldMemOperand(a2, AllocationSite::kMapOffset));
2733 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2734 __ Branch(&feedback_register_initialized, eq, a5, Operand(at));
2735 __ LoadRoot(a2, Heap::kUndefinedValueRootIndex);
2736 __ bind(&feedback_register_initialized);
2737 }
2738
2739 __ AssertUndefinedOrAllocationSite(a2, a5);
2740 }
2741
2742 // Jump to the function-specific construct stub.
2743 Register jmp_reg = a4;
2744 __ ld(jmp_reg, FieldMemOperand(a1, JSFunction::kSharedFunctionInfoOffset));
2745 __ ld(jmp_reg, FieldMemOperand(jmp_reg,
2746 SharedFunctionInfo::kConstructStubOffset));
2747 __ Daddu(at, jmp_reg, Operand(Code::kHeaderSize - kHeapObjectTag));
2748 __ Jump(at);
2749
2750 // a0: number of arguments
2751 // a1: called object
2752 // a4: object type
2753 Label do_call;
2754 __ bind(&slow);
2755 __ Branch(&non_function_call, ne, a4, Operand(JS_FUNCTION_PROXY_TYPE));
2756 __ GetBuiltinFunction(a1, Builtins::CALL_FUNCTION_PROXY_AS_CONSTRUCTOR);
2757 __ jmp(&do_call);
2758
2759 __ bind(&non_function_call);
2760 __ GetBuiltinFunction(a1, Builtins::CALL_NON_FUNCTION_AS_CONSTRUCTOR);
2761 __ bind(&do_call);
2762 // Set expected number of arguments to zero (not changing r0).
2763 __ li(a2, Operand(0, RelocInfo::NONE32));
2764 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
2765 RelocInfo::CODE_TARGET);
2766}
2767
2768
2769// StringCharCodeAtGenerator.
2770void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
2771 DCHECK(!a4.is(index_));
2772 DCHECK(!a4.is(result_));
2773 DCHECK(!a4.is(object_));
2774
2775 // If the receiver is a smi trigger the non-string case.
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002776 if (check_mode_ == RECEIVER_IS_UNKNOWN) {
2777 __ JumpIfSmi(object_, receiver_not_string_);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002778
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002779 // Fetch the instance type of the receiver into result register.
2780 __ ld(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
2781 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
2782 // If the receiver is not a string trigger the non-string case.
2783 __ And(a4, result_, Operand(kIsNotStringMask));
2784 __ Branch(receiver_not_string_, ne, a4, Operand(zero_reg));
2785 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002786
2787 // If the index is non-smi trigger the non-smi case.
2788 __ JumpIfNotSmi(index_, &index_not_smi_);
2789
2790 __ bind(&got_smi_index_);
2791
2792 // Check for index out of range.
2793 __ ld(a4, FieldMemOperand(object_, String::kLengthOffset));
2794 __ Branch(index_out_of_range_, ls, a4, Operand(index_));
2795
2796 __ SmiUntag(index_);
2797
2798 StringCharLoadGenerator::Generate(masm,
2799 object_,
2800 index_,
2801 result_,
2802 &call_runtime_);
2803
2804 __ SmiTag(result_);
2805 __ bind(&exit_);
2806}
2807
2808
2809static void EmitLoadTypeFeedbackVector(MacroAssembler* masm, Register vector) {
2810 __ ld(vector, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
2811 __ ld(vector, FieldMemOperand(vector,
2812 JSFunction::kSharedFunctionInfoOffset));
2813 __ ld(vector, FieldMemOperand(vector,
2814 SharedFunctionInfo::kFeedbackVectorOffset));
2815}
2816
2817
2818void CallIC_ArrayStub::Generate(MacroAssembler* masm) {
2819 // a1 - function
2820 // a3 - slot id
2821 Label miss;
2822
2823 EmitLoadTypeFeedbackVector(masm, a2);
2824
2825 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, at);
2826 __ Branch(&miss, ne, a1, Operand(at));
2827
2828 __ li(a0, Operand(arg_count()));
2829 __ dsrl(at, a3, 32 - kPointerSizeLog2);
2830 __ Daddu(at, a2, Operand(at));
2831 __ ld(a4, FieldMemOperand(at, FixedArray::kHeaderSize));
2832
2833 // Verify that a4 contains an AllocationSite
2834 __ ld(a5, FieldMemOperand(a4, HeapObject::kMapOffset));
2835 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
2836 __ Branch(&miss, ne, a5, Operand(at));
2837
2838 __ mov(a2, a4);
2839 ArrayConstructorStub stub(masm->isolate(), arg_count());
2840 __ TailCallStub(&stub);
2841
2842 __ bind(&miss);
2843 GenerateMiss(masm);
2844
2845 // The slow case, we need this no matter what to complete a call after a miss.
2846 CallFunctionNoFeedback(masm,
2847 arg_count(),
2848 true,
2849 CallAsMethod());
2850
2851 // Unreachable.
2852 __ stop("Unexpected code address");
2853}
2854
2855
2856void CallICStub::Generate(MacroAssembler* masm) {
2857 // a1 - function
2858 // a3 - slot id (Smi)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002859 const int with_types_offset =
2860 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kWithTypesIndex);
2861 const int generic_offset =
2862 FixedArray::OffsetOfElementAt(TypeFeedbackVector::kGenericCountIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002863 Label extra_checks_or_miss, slow_start;
2864 Label slow, non_function, wrap, cont;
2865 Label have_js_function;
2866 int argc = arg_count();
2867 ParameterCount actual(argc);
2868
2869 EmitLoadTypeFeedbackVector(masm, a2);
2870
2871 // The checks. First, does r1 match the recorded monomorphic target?
2872 __ dsrl(a4, a3, 32 - kPointerSizeLog2);
2873 __ Daddu(a4, a2, Operand(a4));
2874 __ ld(a4, FieldMemOperand(a4, FixedArray::kHeaderSize));
2875 __ Branch(&extra_checks_or_miss, ne, a1, Operand(a4));
2876
2877 __ bind(&have_js_function);
2878 if (CallAsMethod()) {
2879 EmitContinueIfStrictOrNative(masm, &cont);
2880 // Compute the receiver in sloppy mode.
2881 __ ld(a3, MemOperand(sp, argc * kPointerSize));
2882
2883 __ JumpIfSmi(a3, &wrap);
2884 __ GetObjectType(a3, a4, a4);
2885 __ Branch(&wrap, lt, a4, Operand(FIRST_SPEC_OBJECT_TYPE));
2886
2887 __ bind(&cont);
2888 }
2889
2890 __ InvokeFunction(a1, actual, JUMP_FUNCTION, NullCallWrapper());
2891
2892 __ bind(&slow);
2893 EmitSlowCase(masm, argc, &non_function);
2894
2895 if (CallAsMethod()) {
2896 __ bind(&wrap);
2897 EmitWrapCase(masm, argc, &cont);
2898 }
2899
2900 __ bind(&extra_checks_or_miss);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002901 Label uninitialized, miss;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002902
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002903 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002904 __ Branch(&slow_start, eq, a4, Operand(at));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002905
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002906 // The following cases attempt to handle MISS cases without going to the
2907 // runtime.
2908 if (FLAG_trace_ic) {
2909 __ Branch(&miss);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002910 }
2911
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002912 __ LoadRoot(at, Heap::kuninitialized_symbolRootIndex);
2913 __ Branch(&uninitialized, eq, a4, Operand(at));
2914
2915 // We are going megamorphic. If the feedback is a JSFunction, it is fine
2916 // to handle it here. More complex cases are dealt with in the runtime.
2917 __ AssertNotSmi(a4);
2918 __ GetObjectType(a4, a5, a5);
2919 __ Branch(&miss, ne, a5, Operand(JS_FUNCTION_TYPE));
2920 __ dsrl(a4, a3, 32 - kPointerSizeLog2);
2921 __ Daddu(a4, a2, Operand(a4));
2922 __ LoadRoot(at, Heap::kmegamorphic_symbolRootIndex);
2923 __ sd(at, FieldMemOperand(a4, FixedArray::kHeaderSize));
2924 // We have to update statistics for runtime profiling.
2925 __ ld(a4, FieldMemOperand(a2, with_types_offset));
2926 __ Dsubu(a4, a4, Operand(Smi::FromInt(1)));
2927 __ sd(a4, FieldMemOperand(a2, with_types_offset));
2928 __ ld(a4, FieldMemOperand(a2, generic_offset));
2929 __ Daddu(a4, a4, Operand(Smi::FromInt(1)));
2930 __ Branch(USE_DELAY_SLOT, &slow_start);
2931 __ sd(a4, FieldMemOperand(a2, generic_offset)); // In delay slot.
2932
2933 __ bind(&uninitialized);
2934
2935 // We are going monomorphic, provided we actually have a JSFunction.
2936 __ JumpIfSmi(a1, &miss);
2937
2938 // Goto miss case if we do not have a function.
2939 __ GetObjectType(a1, a4, a4);
2940 __ Branch(&miss, ne, a4, Operand(JS_FUNCTION_TYPE));
2941
2942 // Make sure the function is not the Array() function, which requires special
2943 // behavior on MISS.
2944 __ LoadGlobalFunction(Context::ARRAY_FUNCTION_INDEX, a4);
2945 __ Branch(&miss, eq, a1, Operand(a4));
2946
2947 // Update stats.
2948 __ ld(a4, FieldMemOperand(a2, with_types_offset));
2949 __ Daddu(a4, a4, Operand(Smi::FromInt(1)));
2950 __ sd(a4, FieldMemOperand(a2, with_types_offset));
2951
2952 // Store the function.
2953 __ dsrl(a4, a3, 32 - kPointerSizeLog2);
2954 __ Daddu(a4, a2, Operand(a4));
2955 __ Daddu(a4, a4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
2956 __ sd(a1, MemOperand(a4, 0));
2957
2958 // Update the write barrier.
2959 __ mov(a5, a1);
2960 __ RecordWrite(a2, a4, a5, kRAHasNotBeenSaved, kDontSaveFPRegs,
2961 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
2962 __ Branch(&have_js_function);
2963
2964 // We are here because tracing is on or we encountered a MISS case we can't
2965 // handle here.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002966 __ bind(&miss);
2967 GenerateMiss(masm);
2968
2969 // the slow case
2970 __ bind(&slow_start);
2971 // Check that the function is really a JavaScript function.
2972 // r1: pushed function (to be verified)
2973 __ JumpIfSmi(a1, &non_function);
2974
2975 // Goto slow case if we do not have a function.
2976 __ GetObjectType(a1, a4, a4);
2977 __ Branch(&slow, ne, a4, Operand(JS_FUNCTION_TYPE));
2978 __ Branch(&have_js_function);
2979}
2980
2981
2982void CallICStub::GenerateMiss(MacroAssembler* masm) {
2983 // Get the receiver of the function from the stack; 1 ~ return address.
2984 __ ld(a4, MemOperand(sp, (arg_count() + 1) * kPointerSize));
2985
2986 {
2987 FrameScope scope(masm, StackFrame::INTERNAL);
2988
2989 // Push the receiver and the function and feedback info.
2990 __ Push(a4, a1, a2, a3);
2991
2992 // Call the entry.
2993 IC::UtilityId id = GetICState() == DEFAULT ? IC::kCallIC_Miss
2994 : IC::kCallIC_Customization_Miss;
2995
2996 ExternalReference miss = ExternalReference(IC_Utility(id),
2997 masm->isolate());
2998 __ CallExternalReference(miss, 4);
2999
3000 // Move result to a1 and exit the internal frame.
3001 __ mov(a1, v0);
3002 }
3003}
3004
3005
3006void StringCharCodeAtGenerator::GenerateSlow(
3007 MacroAssembler* masm,
3008 const RuntimeCallHelper& call_helper) {
3009 __ Abort(kUnexpectedFallthroughToCharCodeAtSlowCase);
3010
3011 // Index is not a smi.
3012 __ bind(&index_not_smi_);
3013 // If index is a heap number, try converting it to an integer.
3014 __ CheckMap(index_,
3015 result_,
3016 Heap::kHeapNumberMapRootIndex,
3017 index_not_number_,
3018 DONT_DO_SMI_CHECK);
3019 call_helper.BeforeCall(masm);
3020 // Consumed by runtime conversion function:
3021 __ Push(object_, index_);
3022 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3023 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3024 } else {
3025 DCHECK(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3026 // NumberToSmi discards numbers that are not exact integers.
3027 __ CallRuntime(Runtime::kNumberToSmi, 1);
3028 }
3029
3030 // Save the conversion result before the pop instructions below
3031 // have a chance to overwrite it.
3032
3033 __ Move(index_, v0);
3034 __ pop(object_);
3035 // Reload the instance type.
3036 __ ld(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3037 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3038 call_helper.AfterCall(masm);
3039 // If index is still not a smi, it must be out of range.
3040 __ JumpIfNotSmi(index_, index_out_of_range_);
3041 // Otherwise, return to the fast path.
3042 __ Branch(&got_smi_index_);
3043
3044 // Call runtime. We get here when the receiver is a string and the
3045 // index is a number, but the code of getting the actual character
3046 // is too complex (e.g., when the string needs to be flattened).
3047 __ bind(&call_runtime_);
3048 call_helper.BeforeCall(masm);
3049 __ SmiTag(index_);
3050 __ Push(object_, index_);
3051 __ CallRuntime(Runtime::kStringCharCodeAtRT, 2);
3052
3053 __ Move(result_, v0);
3054
3055 call_helper.AfterCall(masm);
3056 __ jmp(&exit_);
3057
3058 __ Abort(kUnexpectedFallthroughFromCharCodeAtSlowCase);
3059}
3060
3061
3062// -------------------------------------------------------------------------
3063// StringCharFromCodeGenerator
3064
3065void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3066 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3067
3068 DCHECK(!a4.is(result_));
3069 DCHECK(!a4.is(code_));
3070
3071 STATIC_ASSERT(kSmiTag == 0);
3072 DCHECK(base::bits::IsPowerOfTwo32(String::kMaxOneByteCharCode + 1));
3073 __ And(a4,
3074 code_,
3075 Operand(kSmiTagMask |
3076 ((~String::kMaxOneByteCharCode) << kSmiTagSize)));
3077 __ Branch(&slow_case_, ne, a4, Operand(zero_reg));
3078
3079
3080 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
3081 // At this point code register contains smi tagged one_byte char code.
3082 STATIC_ASSERT(kSmiTag == 0);
3083 __ SmiScale(a4, code_, kPointerSizeLog2);
3084 __ Daddu(result_, result_, a4);
3085 __ ld(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
3086 __ LoadRoot(a4, Heap::kUndefinedValueRootIndex);
3087 __ Branch(&slow_case_, eq, result_, Operand(a4));
3088 __ bind(&exit_);
3089}
3090
3091
3092void StringCharFromCodeGenerator::GenerateSlow(
3093 MacroAssembler* masm,
3094 const RuntimeCallHelper& call_helper) {
3095 __ Abort(kUnexpectedFallthroughToCharFromCodeSlowCase);
3096
3097 __ bind(&slow_case_);
3098 call_helper.BeforeCall(masm);
3099 __ push(code_);
3100 __ CallRuntime(Runtime::kCharFromCode, 1);
3101 __ Move(result_, v0);
3102
3103 call_helper.AfterCall(masm);
3104 __ Branch(&exit_);
3105
3106 __ Abort(kUnexpectedFallthroughFromCharFromCodeSlowCase);
3107}
3108
3109
3110enum CopyCharactersFlags { COPY_ONE_BYTE = 1, DEST_ALWAYS_ALIGNED = 2 };
3111
3112
3113void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3114 Register dest,
3115 Register src,
3116 Register count,
3117 Register scratch,
3118 String::Encoding encoding) {
3119 if (FLAG_debug_code) {
3120 // Check that destination is word aligned.
3121 __ And(scratch, dest, Operand(kPointerAlignmentMask));
3122 __ Check(eq,
3123 kDestinationOfCopyNotAligned,
3124 scratch,
3125 Operand(zero_reg));
3126 }
3127
3128 // Assumes word reads and writes are little endian.
3129 // Nothing to do for zero characters.
3130 Label done;
3131
3132 if (encoding == String::TWO_BYTE_ENCODING) {
3133 __ Daddu(count, count, count);
3134 }
3135
3136 Register limit = count; // Read until dest equals this.
3137 __ Daddu(limit, dest, Operand(count));
3138
3139 Label loop_entry, loop;
3140 // Copy bytes from src to dest until dest hits limit.
3141 __ Branch(&loop_entry);
3142 __ bind(&loop);
3143 __ lbu(scratch, MemOperand(src));
3144 __ daddiu(src, src, 1);
3145 __ sb(scratch, MemOperand(dest));
3146 __ daddiu(dest, dest, 1);
3147 __ bind(&loop_entry);
3148 __ Branch(&loop, lt, dest, Operand(limit));
3149
3150 __ bind(&done);
3151}
3152
3153
3154void SubStringStub::Generate(MacroAssembler* masm) {
3155 Label runtime;
3156 // Stack frame on entry.
3157 // ra: return address
3158 // sp[0]: to
3159 // sp[4]: from
3160 // sp[8]: string
3161
3162 // This stub is called from the native-call %_SubString(...), so
3163 // nothing can be assumed about the arguments. It is tested that:
3164 // "string" is a sequential string,
3165 // both "from" and "to" are smis, and
3166 // 0 <= from <= to <= string.length.
3167 // If any of these assumptions fail, we call the runtime system.
3168
3169 const int kToOffset = 0 * kPointerSize;
3170 const int kFromOffset = 1 * kPointerSize;
3171 const int kStringOffset = 2 * kPointerSize;
3172
3173 __ ld(a2, MemOperand(sp, kToOffset));
3174 __ ld(a3, MemOperand(sp, kFromOffset));
3175// Does not needed?
3176// STATIC_ASSERT(kFromOffset == kToOffset + 4);
3177 STATIC_ASSERT(kSmiTag == 0);
3178// Does not needed?
3179// STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3180
3181 // Utilize delay slots. SmiUntag doesn't emit a jump, everything else is
3182 // safe in this case.
3183 __ JumpIfNotSmi(a2, &runtime);
3184 __ JumpIfNotSmi(a3, &runtime);
3185 // Both a2 and a3 are untagged integers.
3186
3187 __ SmiUntag(a2, a2);
3188 __ SmiUntag(a3, a3);
3189 __ Branch(&runtime, lt, a3, Operand(zero_reg)); // From < 0.
3190
3191 __ Branch(&runtime, gt, a3, Operand(a2)); // Fail if from > to.
3192 __ Dsubu(a2, a2, a3);
3193
3194 // Make sure first argument is a string.
3195 __ ld(v0, MemOperand(sp, kStringOffset));
3196 __ JumpIfSmi(v0, &runtime);
3197 __ ld(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
3198 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
3199 __ And(a4, a1, Operand(kIsNotStringMask));
3200
3201 __ Branch(&runtime, ne, a4, Operand(zero_reg));
3202
3203 Label single_char;
3204 __ Branch(&single_char, eq, a2, Operand(1));
3205
3206 // Short-cut for the case of trivial substring.
3207 Label return_v0;
3208 // v0: original string
3209 // a2: result string length
3210 __ ld(a4, FieldMemOperand(v0, String::kLengthOffset));
3211 __ SmiUntag(a4);
3212 // Return original string.
3213 __ Branch(&return_v0, eq, a2, Operand(a4));
3214 // Longer than original string's length or negative: unsafe arguments.
3215 __ Branch(&runtime, hi, a2, Operand(a4));
3216 // Shorter than original string's length: an actual substring.
3217
3218 // Deal with different string types: update the index if necessary
3219 // and put the underlying string into a5.
3220 // v0: original string
3221 // a1: instance type
3222 // a2: length
3223 // a3: from index (untagged)
3224 Label underlying_unpacked, sliced_string, seq_or_external_string;
3225 // If the string is not indirect, it can only be sequential or external.
3226 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
3227 STATIC_ASSERT(kIsIndirectStringMask != 0);
3228 __ And(a4, a1, Operand(kIsIndirectStringMask));
3229 __ Branch(USE_DELAY_SLOT, &seq_or_external_string, eq, a4, Operand(zero_reg));
3230 // a4 is used as a scratch register and can be overwritten in either case.
3231 __ And(a4, a1, Operand(kSlicedNotConsMask));
3232 __ Branch(&sliced_string, ne, a4, Operand(zero_reg));
3233 // Cons string. Check whether it is flat, then fetch first part.
3234 __ ld(a5, FieldMemOperand(v0, ConsString::kSecondOffset));
3235 __ LoadRoot(a4, Heap::kempty_stringRootIndex);
3236 __ Branch(&runtime, ne, a5, Operand(a4));
3237 __ ld(a5, FieldMemOperand(v0, ConsString::kFirstOffset));
3238 // Update instance type.
3239 __ ld(a1, FieldMemOperand(a5, HeapObject::kMapOffset));
3240 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
3241 __ jmp(&underlying_unpacked);
3242
3243 __ bind(&sliced_string);
3244 // Sliced string. Fetch parent and correct start index by offset.
3245 __ ld(a5, FieldMemOperand(v0, SlicedString::kParentOffset));
3246 __ ld(a4, FieldMemOperand(v0, SlicedString::kOffsetOffset));
3247 __ SmiUntag(a4); // Add offset to index.
3248 __ Daddu(a3, a3, a4);
3249 // Update instance type.
3250 __ ld(a1, FieldMemOperand(a5, HeapObject::kMapOffset));
3251 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
3252 __ jmp(&underlying_unpacked);
3253
3254 __ bind(&seq_or_external_string);
3255 // Sequential or external string. Just move string to the expected register.
3256 __ mov(a5, v0);
3257
3258 __ bind(&underlying_unpacked);
3259
3260 if (FLAG_string_slices) {
3261 Label copy_routine;
3262 // a5: underlying subject string
3263 // a1: instance type of underlying subject string
3264 // a2: length
3265 // a3: adjusted start index (untagged)
3266 // Short slice. Copy instead of slicing.
3267 __ Branch(&copy_routine, lt, a2, Operand(SlicedString::kMinLength));
3268 // Allocate new sliced string. At this point we do not reload the instance
3269 // type including the string encoding because we simply rely on the info
3270 // provided by the original string. It does not matter if the original
3271 // string's encoding is wrong because we always have to recheck encoding of
3272 // the newly created string's parent anyways due to externalized strings.
3273 Label two_byte_slice, set_slice_header;
3274 STATIC_ASSERT((kStringEncodingMask & kOneByteStringTag) != 0);
3275 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
3276 __ And(a4, a1, Operand(kStringEncodingMask));
3277 __ Branch(&two_byte_slice, eq, a4, Operand(zero_reg));
3278 __ AllocateOneByteSlicedString(v0, a2, a6, a7, &runtime);
3279 __ jmp(&set_slice_header);
3280 __ bind(&two_byte_slice);
3281 __ AllocateTwoByteSlicedString(v0, a2, a6, a7, &runtime);
3282 __ bind(&set_slice_header);
3283 __ SmiTag(a3);
3284 __ sd(a5, FieldMemOperand(v0, SlicedString::kParentOffset));
3285 __ sd(a3, FieldMemOperand(v0, SlicedString::kOffsetOffset));
3286 __ jmp(&return_v0);
3287
3288 __ bind(&copy_routine);
3289 }
3290
3291 // a5: underlying subject string
3292 // a1: instance type of underlying subject string
3293 // a2: length
3294 // a3: adjusted start index (untagged)
3295 Label two_byte_sequential, sequential_string, allocate_result;
3296 STATIC_ASSERT(kExternalStringTag != 0);
3297 STATIC_ASSERT(kSeqStringTag == 0);
3298 __ And(a4, a1, Operand(kExternalStringTag));
3299 __ Branch(&sequential_string, eq, a4, Operand(zero_reg));
3300
3301 // Handle external string.
3302 // Rule out short external strings.
3303 STATIC_ASSERT(kShortExternalStringTag != 0);
3304 __ And(a4, a1, Operand(kShortExternalStringTag));
3305 __ Branch(&runtime, ne, a4, Operand(zero_reg));
3306 __ ld(a5, FieldMemOperand(a5, ExternalString::kResourceDataOffset));
3307 // a5 already points to the first character of underlying string.
3308 __ jmp(&allocate_result);
3309
3310 __ bind(&sequential_string);
3311 // Locate first character of underlying subject string.
3312 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqOneByteString::kHeaderSize);
3313 __ Daddu(a5, a5, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3314
3315 __ bind(&allocate_result);
3316 // Sequential acii string. Allocate the result.
3317 STATIC_ASSERT((kOneByteStringTag & kStringEncodingMask) != 0);
3318 __ And(a4, a1, Operand(kStringEncodingMask));
3319 __ Branch(&two_byte_sequential, eq, a4, Operand(zero_reg));
3320
3321 // Allocate and copy the resulting one_byte string.
3322 __ AllocateOneByteString(v0, a2, a4, a6, a7, &runtime);
3323
3324 // Locate first character of substring to copy.
3325 __ Daddu(a5, a5, a3);
3326
3327 // Locate first character of result.
3328 __ Daddu(a1, v0, Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3329
3330 // v0: result string
3331 // a1: first character of result string
3332 // a2: result string length
3333 // a5: first character of substring to copy
3334 STATIC_ASSERT((SeqOneByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3335 StringHelper::GenerateCopyCharacters(
3336 masm, a1, a5, a2, a3, String::ONE_BYTE_ENCODING);
3337 __ jmp(&return_v0);
3338
3339 // Allocate and copy the resulting two-byte string.
3340 __ bind(&two_byte_sequential);
3341 __ AllocateTwoByteString(v0, a2, a4, a6, a7, &runtime);
3342
3343 // Locate first character of substring to copy.
3344 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
3345 __ dsll(a4, a3, 1);
3346 __ Daddu(a5, a5, a4);
3347 // Locate first character of result.
3348 __ Daddu(a1, v0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3349
3350 // v0: result string.
3351 // a1: first character of result.
3352 // a2: result length.
3353 // a5: first character of substring to copy.
3354 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
3355 StringHelper::GenerateCopyCharacters(
3356 masm, a1, a5, a2, a3, String::TWO_BYTE_ENCODING);
3357
3358 __ bind(&return_v0);
3359 Counters* counters = isolate()->counters();
3360 __ IncrementCounter(counters->sub_string_native(), 1, a3, a4);
3361 __ DropAndRet(3);
3362
3363 // Just jump to runtime to create the sub string.
3364 __ bind(&runtime);
3365 __ TailCallRuntime(Runtime::kSubString, 3, 1);
3366
3367 __ bind(&single_char);
3368 // v0: original string
3369 // a1: instance type
3370 // a2: length
3371 // a3: from index (untagged)
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003372 StringCharAtGenerator generator(v0, a3, a2, v0, &runtime, &runtime, &runtime,
3373 STRING_INDEX_IS_NUMBER, RECEIVER_IS_STRING);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003374 generator.GenerateFast(masm);
3375 __ DropAndRet(3);
3376 generator.SkipSlow(masm, &runtime);
3377}
3378
3379
Emily Bernierd0a1eb72015-03-24 16:35:39 -04003380void ToNumberStub::Generate(MacroAssembler* masm) {
3381 // The ToNumber stub takes one argument in a0.
3382 Label not_smi;
3383 __ JumpIfNotSmi(a0, &not_smi);
3384 __ Ret(USE_DELAY_SLOT);
3385 __ mov(v0, a0);
3386 __ bind(&not_smi);
3387
3388 Label not_heap_number;
3389 __ ld(a1, FieldMemOperand(a0, HeapObject::kMapOffset));
3390 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
3391 // a0: object
3392 // a1: instance type.
3393 __ Branch(&not_heap_number, ne, a1, Operand(HEAP_NUMBER_TYPE));
3394 __ Ret(USE_DELAY_SLOT);
3395 __ mov(v0, a0);
3396 __ bind(&not_heap_number);
3397
3398 Label not_string, slow_string;
3399 __ Branch(&not_string, hs, a1, Operand(FIRST_NONSTRING_TYPE));
3400 // Check if string has a cached array index.
3401 __ ld(a2, FieldMemOperand(a0, String::kHashFieldOffset));
3402 __ And(at, a2, Operand(String::kContainsCachedArrayIndexMask));
3403 __ Branch(&slow_string, ne, at, Operand(zero_reg));
3404 __ IndexFromHash(a2, a0);
3405 __ Ret(USE_DELAY_SLOT);
3406 __ mov(v0, a0);
3407 __ bind(&slow_string);
3408 __ push(a0); // Push argument.
3409 __ TailCallRuntime(Runtime::kStringToNumber, 1, 1);
3410 __ bind(&not_string);
3411
3412 Label not_oddball;
3413 __ Branch(&not_oddball, ne, a1, Operand(ODDBALL_TYPE));
3414 __ Ret(USE_DELAY_SLOT);
3415 __ ld(v0, FieldMemOperand(a0, Oddball::kToNumberOffset));
3416 __ bind(&not_oddball);
3417
3418 __ push(a0); // Push argument.
3419 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
3420}
3421
3422
Ben Murdochb8a8cc12014-11-26 15:28:44 +00003423void StringHelper::GenerateFlatOneByteStringEquals(
3424 MacroAssembler* masm, Register left, Register right, Register scratch1,
3425 Register scratch2, Register scratch3) {
3426 Register length = scratch1;
3427
3428 // Compare lengths.
3429 Label strings_not_equal, check_zero_length;
3430 __ ld(length, FieldMemOperand(left, String::kLengthOffset));
3431 __ ld(scratch2, FieldMemOperand(right, String::kLengthOffset));
3432 __ Branch(&check_zero_length, eq, length, Operand(scratch2));
3433 __ bind(&strings_not_equal);
3434 // Can not put li in delayslot, it has multi instructions.
3435 __ li(v0, Operand(Smi::FromInt(NOT_EQUAL)));
3436 __ Ret();
3437
3438 // Check if the length is zero.
3439 Label compare_chars;
3440 __ bind(&check_zero_length);
3441 STATIC_ASSERT(kSmiTag == 0);
3442 __ Branch(&compare_chars, ne, length, Operand(zero_reg));
3443 DCHECK(is_int16((intptr_t)Smi::FromInt(EQUAL)));
3444 __ Ret(USE_DELAY_SLOT);
3445 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3446
3447 // Compare characters.
3448 __ bind(&compare_chars);
3449
3450 GenerateOneByteCharsCompareLoop(masm, left, right, length, scratch2, scratch3,
3451 v0, &strings_not_equal);
3452
3453 // Characters are equal.
3454 __ Ret(USE_DELAY_SLOT);
3455 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3456}
3457
3458
3459void StringHelper::GenerateCompareFlatOneByteStrings(
3460 MacroAssembler* masm, Register left, Register right, Register scratch1,
3461 Register scratch2, Register scratch3, Register scratch4) {
3462 Label result_not_equal, compare_lengths;
3463 // Find minimum length and length difference.
3464 __ ld(scratch1, FieldMemOperand(left, String::kLengthOffset));
3465 __ ld(scratch2, FieldMemOperand(right, String::kLengthOffset));
3466 __ Dsubu(scratch3, scratch1, Operand(scratch2));
3467 Register length_delta = scratch3;
3468 __ slt(scratch4, scratch2, scratch1);
3469 __ Movn(scratch1, scratch2, scratch4);
3470 Register min_length = scratch1;
3471 STATIC_ASSERT(kSmiTag == 0);
3472 __ Branch(&compare_lengths, eq, min_length, Operand(zero_reg));
3473
3474 // Compare loop.
3475 GenerateOneByteCharsCompareLoop(masm, left, right, min_length, scratch2,
3476 scratch4, v0, &result_not_equal);
3477
3478 // Compare lengths - strings up to min-length are equal.
3479 __ bind(&compare_lengths);
3480 DCHECK(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
3481 // Use length_delta as result if it's zero.
3482 __ mov(scratch2, length_delta);
3483 __ mov(scratch4, zero_reg);
3484 __ mov(v0, zero_reg);
3485
3486 __ bind(&result_not_equal);
3487 // Conditionally update the result based either on length_delta or
3488 // the last comparion performed in the loop above.
3489 Label ret;
3490 __ Branch(&ret, eq, scratch2, Operand(scratch4));
3491 __ li(v0, Operand(Smi::FromInt(GREATER)));
3492 __ Branch(&ret, gt, scratch2, Operand(scratch4));
3493 __ li(v0, Operand(Smi::FromInt(LESS)));
3494 __ bind(&ret);
3495 __ Ret();
3496}
3497
3498
3499void StringHelper::GenerateOneByteCharsCompareLoop(
3500 MacroAssembler* masm, Register left, Register right, Register length,
3501 Register scratch1, Register scratch2, Register scratch3,
3502 Label* chars_not_equal) {
3503 // Change index to run from -length to -1 by adding length to string
3504 // start. This means that loop ends when index reaches zero, which
3505 // doesn't need an additional compare.
3506 __ SmiUntag(length);
3507 __ Daddu(scratch1, length,
3508 Operand(SeqOneByteString::kHeaderSize - kHeapObjectTag));
3509 __ Daddu(left, left, Operand(scratch1));
3510 __ Daddu(right, right, Operand(scratch1));
3511 __ Dsubu(length, zero_reg, length);
3512 Register index = length; // index = -length;
3513
3514
3515 // Compare loop.
3516 Label loop;
3517 __ bind(&loop);
3518 __ Daddu(scratch3, left, index);
3519 __ lbu(scratch1, MemOperand(scratch3));
3520 __ Daddu(scratch3, right, index);
3521 __ lbu(scratch2, MemOperand(scratch3));
3522 __ Branch(chars_not_equal, ne, scratch1, Operand(scratch2));
3523 __ Daddu(index, index, 1);
3524 __ Branch(&loop, ne, index, Operand(zero_reg));
3525}
3526
3527
3528void StringCompareStub::Generate(MacroAssembler* masm) {
3529 Label runtime;
3530
3531 Counters* counters = isolate()->counters();
3532
3533 // Stack frame on entry.
3534 // sp[0]: right string
3535 // sp[4]: left string
3536 __ ld(a1, MemOperand(sp, 1 * kPointerSize)); // Left.
3537 __ ld(a0, MemOperand(sp, 0 * kPointerSize)); // Right.
3538
3539 Label not_same;
3540 __ Branch(&not_same, ne, a0, Operand(a1));
3541 STATIC_ASSERT(EQUAL == 0);
3542 STATIC_ASSERT(kSmiTag == 0);
3543 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3544 __ IncrementCounter(counters->string_compare_native(), 1, a1, a2);
3545 __ DropAndRet(2);
3546
3547 __ bind(&not_same);
3548
3549 // Check that both objects are sequential one_byte strings.
3550 __ JumpIfNotBothSequentialOneByteStrings(a1, a0, a2, a3, &runtime);
3551
3552 // Compare flat one_byte strings natively. Remove arguments from stack first.
3553 __ IncrementCounter(counters->string_compare_native(), 1, a2, a3);
3554 __ Daddu(sp, sp, Operand(2 * kPointerSize));
3555 StringHelper::GenerateCompareFlatOneByteStrings(masm, a1, a0, a2, a3, a4, a5);
3556
3557 __ bind(&runtime);
3558 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
3559}
3560
3561
3562void BinaryOpICWithAllocationSiteStub::Generate(MacroAssembler* masm) {
3563 // ----------- S t a t e -------------
3564 // -- a1 : left
3565 // -- a0 : right
3566 // -- ra : return address
3567 // -----------------------------------
3568
3569 // Load a2 with the allocation site. We stick an undefined dummy value here
3570 // and replace it with the real allocation site later when we instantiate this
3571 // stub in BinaryOpICWithAllocationSiteStub::GetCodeCopyFromTemplate().
3572 __ li(a2, handle(isolate()->heap()->undefined_value()));
3573
3574 // Make sure that we actually patched the allocation site.
3575 if (FLAG_debug_code) {
3576 __ And(at, a2, Operand(kSmiTagMask));
3577 __ Assert(ne, kExpectedAllocationSite, at, Operand(zero_reg));
3578 __ ld(a4, FieldMemOperand(a2, HeapObject::kMapOffset));
3579 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
3580 __ Assert(eq, kExpectedAllocationSite, a4, Operand(at));
3581 }
3582
3583 // Tail call into the stub that handles binary operations with allocation
3584 // sites.
3585 BinaryOpWithAllocationSiteStub stub(isolate(), state());
3586 __ TailCallStub(&stub);
3587}
3588
3589
3590void CompareICStub::GenerateSmis(MacroAssembler* masm) {
3591 DCHECK(state() == CompareICState::SMI);
3592 Label miss;
3593 __ Or(a2, a1, a0);
3594 __ JumpIfNotSmi(a2, &miss);
3595
3596 if (GetCondition() == eq) {
3597 // For equality we do not care about the sign of the result.
3598 __ Ret(USE_DELAY_SLOT);
3599 __ Dsubu(v0, a0, a1);
3600 } else {
3601 // Untag before subtracting to avoid handling overflow.
3602 __ SmiUntag(a1);
3603 __ SmiUntag(a0);
3604 __ Ret(USE_DELAY_SLOT);
3605 __ Dsubu(v0, a1, a0);
3606 }
3607
3608 __ bind(&miss);
3609 GenerateMiss(masm);
3610}
3611
3612
3613void CompareICStub::GenerateNumbers(MacroAssembler* masm) {
3614 DCHECK(state() == CompareICState::NUMBER);
3615
3616 Label generic_stub;
3617 Label unordered, maybe_undefined1, maybe_undefined2;
3618 Label miss;
3619
3620 if (left() == CompareICState::SMI) {
3621 __ JumpIfNotSmi(a1, &miss);
3622 }
3623 if (right() == CompareICState::SMI) {
3624 __ JumpIfNotSmi(a0, &miss);
3625 }
3626
3627 // Inlining the double comparison and falling back to the general compare
3628 // stub if NaN is involved.
3629 // Load left and right operand.
3630 Label done, left, left_smi, right_smi;
3631 __ JumpIfSmi(a0, &right_smi);
3632 __ CheckMap(a0, a2, Heap::kHeapNumberMapRootIndex, &maybe_undefined1,
3633 DONT_DO_SMI_CHECK);
3634 __ Dsubu(a2, a0, Operand(kHeapObjectTag));
3635 __ ldc1(f2, MemOperand(a2, HeapNumber::kValueOffset));
3636 __ Branch(&left);
3637 __ bind(&right_smi);
3638 __ SmiUntag(a2, a0); // Can't clobber a0 yet.
3639 FPURegister single_scratch = f6;
3640 __ mtc1(a2, single_scratch);
3641 __ cvt_d_w(f2, single_scratch);
3642
3643 __ bind(&left);
3644 __ JumpIfSmi(a1, &left_smi);
3645 __ CheckMap(a1, a2, Heap::kHeapNumberMapRootIndex, &maybe_undefined2,
3646 DONT_DO_SMI_CHECK);
3647 __ Dsubu(a2, a1, Operand(kHeapObjectTag));
3648 __ ldc1(f0, MemOperand(a2, HeapNumber::kValueOffset));
3649 __ Branch(&done);
3650 __ bind(&left_smi);
3651 __ SmiUntag(a2, a1); // Can't clobber a1 yet.
3652 single_scratch = f8;
3653 __ mtc1(a2, single_scratch);
3654 __ cvt_d_w(f0, single_scratch);
3655
3656 __ bind(&done);
3657
3658 // Return a result of -1, 0, or 1, or use CompareStub for NaNs.
3659 Label fpu_eq, fpu_lt;
3660 // Test if equal, and also handle the unordered/NaN case.
3661 __ BranchF(&fpu_eq, &unordered, eq, f0, f2);
3662
3663 // Test if less (unordered case is already handled).
3664 __ BranchF(&fpu_lt, NULL, lt, f0, f2);
3665
3666 // Otherwise it's greater, so just fall thru, and return.
3667 DCHECK(is_int16(GREATER) && is_int16(EQUAL) && is_int16(LESS));
3668 __ Ret(USE_DELAY_SLOT);
3669 __ li(v0, Operand(GREATER));
3670
3671 __ bind(&fpu_eq);
3672 __ Ret(USE_DELAY_SLOT);
3673 __ li(v0, Operand(EQUAL));
3674
3675 __ bind(&fpu_lt);
3676 __ Ret(USE_DELAY_SLOT);
3677 __ li(v0, Operand(LESS));
3678
3679 __ bind(&unordered);
3680 __ bind(&generic_stub);
3681 CompareICStub stub(isolate(), op(), CompareICState::GENERIC,
3682 CompareICState::GENERIC, CompareICState::GENERIC);
3683 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
3684
3685 __ bind(&maybe_undefined1);
3686 if (Token::IsOrderedRelationalCompareOp(op())) {
3687 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3688 __ Branch(&miss, ne, a0, Operand(at));
3689 __ JumpIfSmi(a1, &unordered);
3690 __ GetObjectType(a1, a2, a2);
3691 __ Branch(&maybe_undefined2, ne, a2, Operand(HEAP_NUMBER_TYPE));
3692 __ jmp(&unordered);
3693 }
3694
3695 __ bind(&maybe_undefined2);
3696 if (Token::IsOrderedRelationalCompareOp(op())) {
3697 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
3698 __ Branch(&unordered, eq, a1, Operand(at));
3699 }
3700
3701 __ bind(&miss);
3702 GenerateMiss(masm);
3703}
3704
3705
3706void CompareICStub::GenerateInternalizedStrings(MacroAssembler* masm) {
3707 DCHECK(state() == CompareICState::INTERNALIZED_STRING);
3708 Label miss;
3709
3710 // Registers containing left and right operands respectively.
3711 Register left = a1;
3712 Register right = a0;
3713 Register tmp1 = a2;
3714 Register tmp2 = a3;
3715
3716 // Check that both operands are heap objects.
3717 __ JumpIfEitherSmi(left, right, &miss);
3718
3719 // Check that both operands are internalized strings.
3720 __ ld(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3721 __ ld(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3722 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3723 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3724 STATIC_ASSERT(kInternalizedTag == 0 && kStringTag == 0);
3725 __ Or(tmp1, tmp1, Operand(tmp2));
3726 __ And(at, tmp1, Operand(kIsNotStringMask | kIsNotInternalizedMask));
3727 __ Branch(&miss, ne, at, Operand(zero_reg));
3728
3729 // Make sure a0 is non-zero. At this point input operands are
3730 // guaranteed to be non-zero.
3731 DCHECK(right.is(a0));
3732 STATIC_ASSERT(EQUAL == 0);
3733 STATIC_ASSERT(kSmiTag == 0);
3734 __ mov(v0, right);
3735 // Internalized strings are compared by identity.
3736 __ Ret(ne, left, Operand(right));
3737 DCHECK(is_int16(EQUAL));
3738 __ Ret(USE_DELAY_SLOT);
3739 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3740
3741 __ bind(&miss);
3742 GenerateMiss(masm);
3743}
3744
3745
3746void CompareICStub::GenerateUniqueNames(MacroAssembler* masm) {
3747 DCHECK(state() == CompareICState::UNIQUE_NAME);
3748 DCHECK(GetCondition() == eq);
3749 Label miss;
3750
3751 // Registers containing left and right operands respectively.
3752 Register left = a1;
3753 Register right = a0;
3754 Register tmp1 = a2;
3755 Register tmp2 = a3;
3756
3757 // Check that both operands are heap objects.
3758 __ JumpIfEitherSmi(left, right, &miss);
3759
3760 // Check that both operands are unique names. This leaves the instance
3761 // types loaded in tmp1 and tmp2.
3762 __ ld(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3763 __ ld(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3764 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3765 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3766
3767 __ JumpIfNotUniqueNameInstanceType(tmp1, &miss);
3768 __ JumpIfNotUniqueNameInstanceType(tmp2, &miss);
3769
3770 // Use a0 as result
3771 __ mov(v0, a0);
3772
3773 // Unique names are compared by identity.
3774 Label done;
3775 __ Branch(&done, ne, left, Operand(right));
3776 // Make sure a0 is non-zero. At this point input operands are
3777 // guaranteed to be non-zero.
3778 DCHECK(right.is(a0));
3779 STATIC_ASSERT(EQUAL == 0);
3780 STATIC_ASSERT(kSmiTag == 0);
3781 __ li(v0, Operand(Smi::FromInt(EQUAL)));
3782 __ bind(&done);
3783 __ Ret();
3784
3785 __ bind(&miss);
3786 GenerateMiss(masm);
3787}
3788
3789
3790void CompareICStub::GenerateStrings(MacroAssembler* masm) {
3791 DCHECK(state() == CompareICState::STRING);
3792 Label miss;
3793
3794 bool equality = Token::IsEqualityOp(op());
3795
3796 // Registers containing left and right operands respectively.
3797 Register left = a1;
3798 Register right = a0;
3799 Register tmp1 = a2;
3800 Register tmp2 = a3;
3801 Register tmp3 = a4;
3802 Register tmp4 = a5;
3803 Register tmp5 = a6;
3804
3805 // Check that both operands are heap objects.
3806 __ JumpIfEitherSmi(left, right, &miss);
3807
3808 // Check that both operands are strings. This leaves the instance
3809 // types loaded in tmp1 and tmp2.
3810 __ ld(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
3811 __ ld(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
3812 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
3813 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
3814 STATIC_ASSERT(kNotStringTag != 0);
3815 __ Or(tmp3, tmp1, tmp2);
3816 __ And(tmp5, tmp3, Operand(kIsNotStringMask));
3817 __ Branch(&miss, ne, tmp5, Operand(zero_reg));
3818
3819 // Fast check for identical strings.
3820 Label left_ne_right;
3821 STATIC_ASSERT(EQUAL == 0);
3822 STATIC_ASSERT(kSmiTag == 0);
3823 __ Branch(&left_ne_right, ne, left, Operand(right));
3824 __ Ret(USE_DELAY_SLOT);
3825 __ mov(v0, zero_reg); // In the delay slot.
3826 __ bind(&left_ne_right);
3827
3828 // Handle not identical strings.
3829
3830 // Check that both strings are internalized strings. If they are, we're done
3831 // because we already know they are not identical. We know they are both
3832 // strings.
3833 if (equality) {
3834 DCHECK(GetCondition() == eq);
3835 STATIC_ASSERT(kInternalizedTag == 0);
3836 __ Or(tmp3, tmp1, Operand(tmp2));
3837 __ And(tmp5, tmp3, Operand(kIsNotInternalizedMask));
3838 Label is_symbol;
3839 __ Branch(&is_symbol, ne, tmp5, Operand(zero_reg));
3840 // Make sure a0 is non-zero. At this point input operands are
3841 // guaranteed to be non-zero.
3842 DCHECK(right.is(a0));
3843 __ Ret(USE_DELAY_SLOT);
3844 __ mov(v0, a0); // In the delay slot.
3845 __ bind(&is_symbol);
3846 }
3847
3848 // Check that both strings are sequential one_byte.
3849 Label runtime;
3850 __ JumpIfBothInstanceTypesAreNotSequentialOneByte(tmp1, tmp2, tmp3, tmp4,
3851 &runtime);
3852
3853 // Compare flat one_byte strings. Returns when done.
3854 if (equality) {
3855 StringHelper::GenerateFlatOneByteStringEquals(masm, left, right, tmp1, tmp2,
3856 tmp3);
3857 } else {
3858 StringHelper::GenerateCompareFlatOneByteStrings(masm, left, right, tmp1,
3859 tmp2, tmp3, tmp4);
3860 }
3861
3862 // Handle more complex cases in runtime.
3863 __ bind(&runtime);
3864 __ Push(left, right);
3865 if (equality) {
3866 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
3867 } else {
3868 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
3869 }
3870
3871 __ bind(&miss);
3872 GenerateMiss(masm);
3873}
3874
3875
3876void CompareICStub::GenerateObjects(MacroAssembler* masm) {
3877 DCHECK(state() == CompareICState::OBJECT);
3878 Label miss;
3879 __ And(a2, a1, Operand(a0));
3880 __ JumpIfSmi(a2, &miss);
3881
3882 __ GetObjectType(a0, a2, a2);
3883 __ Branch(&miss, ne, a2, Operand(JS_OBJECT_TYPE));
3884 __ GetObjectType(a1, a2, a2);
3885 __ Branch(&miss, ne, a2, Operand(JS_OBJECT_TYPE));
3886
3887 DCHECK(GetCondition() == eq);
3888 __ Ret(USE_DELAY_SLOT);
3889 __ dsubu(v0, a0, a1);
3890
3891 __ bind(&miss);
3892 GenerateMiss(masm);
3893}
3894
3895
3896void CompareICStub::GenerateKnownObjects(MacroAssembler* masm) {
3897 Label miss;
3898 __ And(a2, a1, a0);
3899 __ JumpIfSmi(a2, &miss);
3900 __ ld(a2, FieldMemOperand(a0, HeapObject::kMapOffset));
3901 __ ld(a3, FieldMemOperand(a1, HeapObject::kMapOffset));
3902 __ Branch(&miss, ne, a2, Operand(known_map_));
3903 __ Branch(&miss, ne, a3, Operand(known_map_));
3904
3905 __ Ret(USE_DELAY_SLOT);
3906 __ dsubu(v0, a0, a1);
3907
3908 __ bind(&miss);
3909 GenerateMiss(masm);
3910}
3911
3912
3913void CompareICStub::GenerateMiss(MacroAssembler* masm) {
3914 {
3915 // Call the runtime system in a fresh internal frame.
3916 ExternalReference miss =
3917 ExternalReference(IC_Utility(IC::kCompareIC_Miss), isolate());
3918 FrameScope scope(masm, StackFrame::INTERNAL);
3919 __ Push(a1, a0);
3920 __ Push(ra, a1, a0);
3921 __ li(a4, Operand(Smi::FromInt(op())));
3922 __ daddiu(sp, sp, -kPointerSize);
3923 __ CallExternalReference(miss, 3, USE_DELAY_SLOT);
3924 __ sd(a4, MemOperand(sp)); // In the delay slot.
3925 // Compute the entry point of the rewritten stub.
3926 __ Daddu(a2, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
3927 // Restore registers.
3928 __ Pop(a1, a0, ra);
3929 }
3930 __ Jump(a2);
3931}
3932
3933
3934void DirectCEntryStub::Generate(MacroAssembler* masm) {
3935 // Make place for arguments to fit C calling convention. Most of the callers
3936 // of DirectCEntryStub::GenerateCall are using EnterExitFrame/LeaveExitFrame
3937 // so they handle stack restoring and we don't have to do that here.
3938 // Any caller of DirectCEntryStub::GenerateCall must take care of dropping
3939 // kCArgsSlotsSize stack space after the call.
3940 __ daddiu(sp, sp, -kCArgsSlotsSize);
3941 // Place the return address on the stack, making the call
3942 // GC safe. The RegExp backend also relies on this.
3943 __ sd(ra, MemOperand(sp, kCArgsSlotsSize));
3944 __ Call(t9); // Call the C++ function.
3945 __ ld(t9, MemOperand(sp, kCArgsSlotsSize));
3946
3947 if (FLAG_debug_code && FLAG_enable_slow_asserts) {
3948 // In case of an error the return address may point to a memory area
3949 // filled with kZapValue by the GC.
3950 // Dereference the address and check for this.
3951 __ Uld(a4, MemOperand(t9));
3952 __ Assert(ne, kReceivedInvalidReturnAddress, a4,
3953 Operand(reinterpret_cast<uint64_t>(kZapValue)));
3954 }
3955 __ Jump(t9);
3956}
3957
3958
3959void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
3960 Register target) {
3961 intptr_t loc =
3962 reinterpret_cast<intptr_t>(GetCode().location());
3963 __ Move(t9, target);
3964 __ li(ra, Operand(loc, RelocInfo::CODE_TARGET), CONSTANT_SIZE);
3965 __ Call(ra);
3966}
3967
3968
3969void NameDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
3970 Label* miss,
3971 Label* done,
3972 Register receiver,
3973 Register properties,
3974 Handle<Name> name,
3975 Register scratch0) {
3976 DCHECK(name->IsUniqueName());
3977 // If names of slots in range from 1 to kProbes - 1 for the hash value are
3978 // not equal to the name and kProbes-th slot is not used (its name is the
3979 // undefined value), it guarantees the hash table doesn't contain the
3980 // property. It's true even if some slots represent deleted properties
3981 // (their names are the hole value).
3982 for (int i = 0; i < kInlinedProbes; i++) {
3983 // scratch0 points to properties hash.
3984 // Compute the masked index: (hash + i + i * i) & mask.
3985 Register index = scratch0;
3986 // Capacity is smi 2^n.
3987 __ SmiLoadUntag(index, FieldMemOperand(properties, kCapacityOffset));
3988 __ Dsubu(index, index, Operand(1));
3989 __ And(index, index,
3990 Operand(name->Hash() + NameDictionary::GetProbeOffset(i)));
3991
3992 // Scale the index by multiplying by the entry size.
3993 DCHECK(NameDictionary::kEntrySize == 3);
3994 __ dsll(at, index, 1);
3995 __ Daddu(index, index, at); // index *= 3.
3996
3997 Register entity_name = scratch0;
3998 // Having undefined at this place means the name is not contained.
3999 DCHECK_EQ(kSmiTagSize, 1);
4000 Register tmp = properties;
4001
4002 __ dsll(scratch0, index, kPointerSizeLog2);
4003 __ Daddu(tmp, properties, scratch0);
4004 __ ld(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
4005
4006 DCHECK(!tmp.is(entity_name));
4007 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
4008 __ Branch(done, eq, entity_name, Operand(tmp));
4009
4010 // Load the hole ready for use below:
4011 __ LoadRoot(tmp, Heap::kTheHoleValueRootIndex);
4012
4013 // Stop if found the property.
4014 __ Branch(miss, eq, entity_name, Operand(Handle<Name>(name)));
4015
4016 Label good;
4017 __ Branch(&good, eq, entity_name, Operand(tmp));
4018
4019 // Check if the entry name is not a unique name.
4020 __ ld(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
4021 __ lbu(entity_name,
4022 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
4023 __ JumpIfNotUniqueNameInstanceType(entity_name, miss);
4024 __ bind(&good);
4025
4026 // Restore the properties.
4027 __ ld(properties,
4028 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
4029 }
4030
4031 const int spill_mask =
4032 (ra.bit() | a6.bit() | a5.bit() | a4.bit() | a3.bit() |
4033 a2.bit() | a1.bit() | a0.bit() | v0.bit());
4034
4035 __ MultiPush(spill_mask);
4036 __ ld(a0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
4037 __ li(a1, Operand(Handle<Name>(name)));
4038 NameDictionaryLookupStub stub(masm->isolate(), NEGATIVE_LOOKUP);
4039 __ CallStub(&stub);
4040 __ mov(at, v0);
4041 __ MultiPop(spill_mask);
4042
4043 __ Branch(done, eq, at, Operand(zero_reg));
4044 __ Branch(miss, ne, at, Operand(zero_reg));
4045}
4046
4047
4048// Probe the name dictionary in the |elements| register. Jump to the
4049// |done| label if a property with the given name is found. Jump to
4050// the |miss| label otherwise.
4051// If lookup was successful |scratch2| will be equal to elements + 4 * index.
4052void NameDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
4053 Label* miss,
4054 Label* done,
4055 Register elements,
4056 Register name,
4057 Register scratch1,
4058 Register scratch2) {
4059 DCHECK(!elements.is(scratch1));
4060 DCHECK(!elements.is(scratch2));
4061 DCHECK(!name.is(scratch1));
4062 DCHECK(!name.is(scratch2));
4063
4064 __ AssertName(name);
4065
4066 // Compute the capacity mask.
4067 __ ld(scratch1, FieldMemOperand(elements, kCapacityOffset));
4068 __ SmiUntag(scratch1);
4069 __ Dsubu(scratch1, scratch1, Operand(1));
4070
4071 // Generate an unrolled loop that performs a few probes before
4072 // giving up. Measurements done on Gmail indicate that 2 probes
4073 // cover ~93% of loads from dictionaries.
4074 for (int i = 0; i < kInlinedProbes; i++) {
4075 // Compute the masked index: (hash + i + i * i) & mask.
4076 __ lwu(scratch2, FieldMemOperand(name, Name::kHashFieldOffset));
4077 if (i > 0) {
4078 // Add the probe offset (i + i * i) left shifted to avoid right shifting
4079 // the hash in a separate instruction. The value hash + i + i * i is right
4080 // shifted in the following and instruction.
4081 DCHECK(NameDictionary::GetProbeOffset(i) <
4082 1 << (32 - Name::kHashFieldOffset));
4083 __ Daddu(scratch2, scratch2, Operand(
4084 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
4085 }
4086 __ dsrl(scratch2, scratch2, Name::kHashShift);
4087 __ And(scratch2, scratch1, scratch2);
4088
4089 // Scale the index by multiplying by the element size.
4090 DCHECK(NameDictionary::kEntrySize == 3);
4091 // scratch2 = scratch2 * 3.
4092
4093 __ dsll(at, scratch2, 1);
4094 __ Daddu(scratch2, scratch2, at);
4095
4096 // Check if the key is identical to the name.
4097 __ dsll(at, scratch2, kPointerSizeLog2);
4098 __ Daddu(scratch2, elements, at);
4099 __ ld(at, FieldMemOperand(scratch2, kElementsStartOffset));
4100 __ Branch(done, eq, name, Operand(at));
4101 }
4102
4103 const int spill_mask =
4104 (ra.bit() | a6.bit() | a5.bit() | a4.bit() |
4105 a3.bit() | a2.bit() | a1.bit() | a0.bit() | v0.bit()) &
4106 ~(scratch1.bit() | scratch2.bit());
4107
4108 __ MultiPush(spill_mask);
4109 if (name.is(a0)) {
4110 DCHECK(!elements.is(a1));
4111 __ Move(a1, name);
4112 __ Move(a0, elements);
4113 } else {
4114 __ Move(a0, elements);
4115 __ Move(a1, name);
4116 }
4117 NameDictionaryLookupStub stub(masm->isolate(), POSITIVE_LOOKUP);
4118 __ CallStub(&stub);
4119 __ mov(scratch2, a2);
4120 __ mov(at, v0);
4121 __ MultiPop(spill_mask);
4122
4123 __ Branch(done, ne, at, Operand(zero_reg));
4124 __ Branch(miss, eq, at, Operand(zero_reg));
4125}
4126
4127
4128void NameDictionaryLookupStub::Generate(MacroAssembler* masm) {
4129 // This stub overrides SometimesSetsUpAFrame() to return false. That means
4130 // we cannot call anything that could cause a GC from this stub.
4131 // Registers:
4132 // result: NameDictionary to probe
4133 // a1: key
4134 // dictionary: NameDictionary to probe.
4135 // index: will hold an index of entry if lookup is successful.
4136 // might alias with result_.
4137 // Returns:
4138 // result_ is zero if lookup failed, non zero otherwise.
4139
4140 Register result = v0;
4141 Register dictionary = a0;
4142 Register key = a1;
4143 Register index = a2;
4144 Register mask = a3;
4145 Register hash = a4;
4146 Register undefined = a5;
4147 Register entry_key = a6;
4148
4149 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
4150
4151 __ ld(mask, FieldMemOperand(dictionary, kCapacityOffset));
4152 __ SmiUntag(mask);
4153 __ Dsubu(mask, mask, Operand(1));
4154
4155 __ lwu(hash, FieldMemOperand(key, Name::kHashFieldOffset));
4156
4157 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
4158
4159 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
4160 // Compute the masked index: (hash + i + i * i) & mask.
4161 // Capacity is smi 2^n.
4162 if (i > 0) {
4163 // Add the probe offset (i + i * i) left shifted to avoid right shifting
4164 // the hash in a separate instruction. The value hash + i + i * i is right
4165 // shifted in the following and instruction.
4166 DCHECK(NameDictionary::GetProbeOffset(i) <
4167 1 << (32 - Name::kHashFieldOffset));
4168 __ Daddu(index, hash, Operand(
4169 NameDictionary::GetProbeOffset(i) << Name::kHashShift));
4170 } else {
4171 __ mov(index, hash);
4172 }
4173 __ dsrl(index, index, Name::kHashShift);
4174 __ And(index, mask, index);
4175
4176 // Scale the index by multiplying by the entry size.
4177 DCHECK(NameDictionary::kEntrySize == 3);
4178 // index *= 3.
4179 __ mov(at, index);
4180 __ dsll(index, index, 1);
4181 __ Daddu(index, index, at);
4182
4183
4184 DCHECK_EQ(kSmiTagSize, 1);
4185 __ dsll(index, index, kPointerSizeLog2);
4186 __ Daddu(index, index, dictionary);
4187 __ ld(entry_key, FieldMemOperand(index, kElementsStartOffset));
4188
4189 // Having undefined at this place means the name is not contained.
4190 __ Branch(&not_in_dictionary, eq, entry_key, Operand(undefined));
4191
4192 // Stop if found the property.
4193 __ Branch(&in_dictionary, eq, entry_key, Operand(key));
4194
4195 if (i != kTotalProbes - 1 && mode() == NEGATIVE_LOOKUP) {
4196 // Check if the entry name is not a unique name.
4197 __ ld(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
4198 __ lbu(entry_key,
4199 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
4200 __ JumpIfNotUniqueNameInstanceType(entry_key, &maybe_in_dictionary);
4201 }
4202 }
4203
4204 __ bind(&maybe_in_dictionary);
4205 // If we are doing negative lookup then probing failure should be
4206 // treated as a lookup success. For positive lookup probing failure
4207 // should be treated as lookup failure.
4208 if (mode() == POSITIVE_LOOKUP) {
4209 __ Ret(USE_DELAY_SLOT);
4210 __ mov(result, zero_reg);
4211 }
4212
4213 __ bind(&in_dictionary);
4214 __ Ret(USE_DELAY_SLOT);
4215 __ li(result, 1);
4216
4217 __ bind(&not_in_dictionary);
4218 __ Ret(USE_DELAY_SLOT);
4219 __ mov(result, zero_reg);
4220}
4221
4222
4223void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime(
4224 Isolate* isolate) {
4225 StoreBufferOverflowStub stub1(isolate, kDontSaveFPRegs);
4226 stub1.GetCode();
4227 // Hydrogen code stubs need stub2 at snapshot time.
4228 StoreBufferOverflowStub stub2(isolate, kSaveFPRegs);
4229 stub2.GetCode();
4230}
4231
4232
4233// Takes the input in 3 registers: address_ value_ and object_. A pointer to
4234// the value has just been written into the object, now this stub makes sure
4235// we keep the GC informed. The word in the object where the value has been
4236// written is in the address register.
4237void RecordWriteStub::Generate(MacroAssembler* masm) {
4238 Label skip_to_incremental_noncompacting;
4239 Label skip_to_incremental_compacting;
4240
4241 // The first two branch+nop instructions are generated with labels so as to
4242 // get the offset fixed up correctly by the bind(Label*) call. We patch it
4243 // back and forth between a "bne zero_reg, zero_reg, ..." (a nop in this
4244 // position) and the "beq zero_reg, zero_reg, ..." when we start and stop
4245 // incremental heap marking.
4246 // See RecordWriteStub::Patch for details.
4247 __ beq(zero_reg, zero_reg, &skip_to_incremental_noncompacting);
4248 __ nop();
4249 __ beq(zero_reg, zero_reg, &skip_to_incremental_compacting);
4250 __ nop();
4251
4252 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4253 __ RememberedSetHelper(object(),
4254 address(),
4255 value(),
4256 save_fp_regs_mode(),
4257 MacroAssembler::kReturnAtEnd);
4258 }
4259 __ Ret();
4260
4261 __ bind(&skip_to_incremental_noncompacting);
4262 GenerateIncremental(masm, INCREMENTAL);
4263
4264 __ bind(&skip_to_incremental_compacting);
4265 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
4266
4267 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
4268 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
4269
4270 PatchBranchIntoNop(masm, 0);
4271 PatchBranchIntoNop(masm, 2 * Assembler::kInstrSize);
4272}
4273
4274
4275void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
4276 regs_.Save(masm);
4277
4278 if (remembered_set_action() == EMIT_REMEMBERED_SET) {
4279 Label dont_need_remembered_set;
4280
4281 __ ld(regs_.scratch0(), MemOperand(regs_.address(), 0));
4282 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
4283 regs_.scratch0(),
4284 &dont_need_remembered_set);
4285
4286 __ CheckPageFlag(regs_.object(),
4287 regs_.scratch0(),
4288 1 << MemoryChunk::SCAN_ON_SCAVENGE,
4289 ne,
4290 &dont_need_remembered_set);
4291
4292 // First notify the incremental marker if necessary, then update the
4293 // remembered set.
4294 CheckNeedsToInformIncrementalMarker(
4295 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
4296 InformIncrementalMarker(masm);
4297 regs_.Restore(masm);
4298 __ RememberedSetHelper(object(),
4299 address(),
4300 value(),
4301 save_fp_regs_mode(),
4302 MacroAssembler::kReturnAtEnd);
4303
4304 __ bind(&dont_need_remembered_set);
4305 }
4306
4307 CheckNeedsToInformIncrementalMarker(
4308 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
4309 InformIncrementalMarker(masm);
4310 regs_.Restore(masm);
4311 __ Ret();
4312}
4313
4314
4315void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm) {
4316 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode());
4317 int argument_count = 3;
4318 __ PrepareCallCFunction(argument_count, regs_.scratch0());
4319 Register address =
4320 a0.is(regs_.address()) ? regs_.scratch0() : regs_.address();
4321 DCHECK(!address.is(regs_.object()));
4322 DCHECK(!address.is(a0));
4323 __ Move(address, regs_.address());
4324 __ Move(a0, regs_.object());
4325 __ Move(a1, address);
4326 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
4327
4328 AllowExternalCallThatCantCauseGC scope(masm);
4329 __ CallCFunction(
4330 ExternalReference::incremental_marking_record_write_function(isolate()),
4331 argument_count);
4332 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode());
4333}
4334
4335
4336void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
4337 MacroAssembler* masm,
4338 OnNoNeedToInformIncrementalMarker on_no_need,
4339 Mode mode) {
4340 Label on_black;
4341 Label need_incremental;
4342 Label need_incremental_pop_scratch;
4343
4344 __ And(regs_.scratch0(), regs_.object(), Operand(~Page::kPageAlignmentMask));
4345 __ ld(regs_.scratch1(),
4346 MemOperand(regs_.scratch0(),
4347 MemoryChunk::kWriteBarrierCounterOffset));
4348 __ Dsubu(regs_.scratch1(), regs_.scratch1(), Operand(1));
4349 __ sd(regs_.scratch1(),
4350 MemOperand(regs_.scratch0(),
4351 MemoryChunk::kWriteBarrierCounterOffset));
4352 __ Branch(&need_incremental, lt, regs_.scratch1(), Operand(zero_reg));
4353
4354 // Let's look at the color of the object: If it is not black we don't have
4355 // to inform the incremental marker.
4356 __ JumpIfBlack(regs_.object(), regs_.scratch0(), regs_.scratch1(), &on_black);
4357
4358 regs_.Restore(masm);
4359 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4360 __ RememberedSetHelper(object(),
4361 address(),
4362 value(),
4363 save_fp_regs_mode(),
4364 MacroAssembler::kReturnAtEnd);
4365 } else {
4366 __ Ret();
4367 }
4368
4369 __ bind(&on_black);
4370
4371 // Get the value from the slot.
4372 __ ld(regs_.scratch0(), MemOperand(regs_.address(), 0));
4373
4374 if (mode == INCREMENTAL_COMPACTION) {
4375 Label ensure_not_white;
4376
4377 __ CheckPageFlag(regs_.scratch0(), // Contains value.
4378 regs_.scratch1(), // Scratch.
4379 MemoryChunk::kEvacuationCandidateMask,
4380 eq,
4381 &ensure_not_white);
4382
4383 __ CheckPageFlag(regs_.object(),
4384 regs_.scratch1(), // Scratch.
4385 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
4386 eq,
4387 &need_incremental);
4388
4389 __ bind(&ensure_not_white);
4390 }
4391
4392 // We need extra registers for this, so we push the object and the address
4393 // register temporarily.
4394 __ Push(regs_.object(), regs_.address());
4395 __ EnsureNotWhite(regs_.scratch0(), // The value.
4396 regs_.scratch1(), // Scratch.
4397 regs_.object(), // Scratch.
4398 regs_.address(), // Scratch.
4399 &need_incremental_pop_scratch);
4400 __ Pop(regs_.object(), regs_.address());
4401
4402 regs_.Restore(masm);
4403 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
4404 __ RememberedSetHelper(object(),
4405 address(),
4406 value(),
4407 save_fp_regs_mode(),
4408 MacroAssembler::kReturnAtEnd);
4409 } else {
4410 __ Ret();
4411 }
4412
4413 __ bind(&need_incremental_pop_scratch);
4414 __ Pop(regs_.object(), regs_.address());
4415
4416 __ bind(&need_incremental);
4417
4418 // Fall through when we need to inform the incremental marker.
4419}
4420
4421
4422void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
4423 // ----------- S t a t e -------------
4424 // -- a0 : element value to store
4425 // -- a3 : element index as smi
4426 // -- sp[0] : array literal index in function as smi
4427 // -- sp[4] : array literal
4428 // clobbers a1, a2, a4
4429 // -----------------------------------
4430
4431 Label element_done;
4432 Label double_elements;
4433 Label smi_element;
4434 Label slow_elements;
4435 Label fast_elements;
4436
4437 // Get array literal index, array literal and its map.
4438 __ ld(a4, MemOperand(sp, 0 * kPointerSize));
4439 __ ld(a1, MemOperand(sp, 1 * kPointerSize));
4440 __ ld(a2, FieldMemOperand(a1, JSObject::kMapOffset));
4441
4442 __ CheckFastElements(a2, a5, &double_elements);
4443 // Check for FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS elements
4444 __ JumpIfSmi(a0, &smi_element);
4445 __ CheckFastSmiElements(a2, a5, &fast_elements);
4446
4447 // Store into the array literal requires a elements transition. Call into
4448 // the runtime.
4449 __ bind(&slow_elements);
4450 // call.
4451 __ Push(a1, a3, a0);
4452 __ ld(a5, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
4453 __ ld(a5, FieldMemOperand(a5, JSFunction::kLiteralsOffset));
4454 __ Push(a5, a4);
4455 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
4456
4457 // Array literal has ElementsKind of FAST_*_ELEMENTS and value is an object.
4458 __ bind(&fast_elements);
4459 __ ld(a5, FieldMemOperand(a1, JSObject::kElementsOffset));
4460 __ SmiScale(a6, a3, kPointerSizeLog2);
4461 __ Daddu(a6, a5, a6);
4462 __ Daddu(a6, a6, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4463 __ sd(a0, MemOperand(a6, 0));
4464 // Update the write barrier for the array store.
4465 __ RecordWrite(a5, a6, a0, kRAHasNotBeenSaved, kDontSaveFPRegs,
4466 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
4467 __ Ret(USE_DELAY_SLOT);
4468 __ mov(v0, a0);
4469
4470 // Array literal has ElementsKind of FAST_*_SMI_ELEMENTS or FAST_*_ELEMENTS,
4471 // and value is Smi.
4472 __ bind(&smi_element);
4473 __ ld(a5, FieldMemOperand(a1, JSObject::kElementsOffset));
4474 __ SmiScale(a6, a3, kPointerSizeLog2);
4475 __ Daddu(a6, a5, a6);
4476 __ sd(a0, FieldMemOperand(a6, FixedArray::kHeaderSize));
4477 __ Ret(USE_DELAY_SLOT);
4478 __ mov(v0, a0);
4479
4480 // Array literal has ElementsKind of FAST_*_DOUBLE_ELEMENTS.
4481 __ bind(&double_elements);
4482 __ ld(a5, FieldMemOperand(a1, JSObject::kElementsOffset));
4483 __ StoreNumberToDoubleElements(a0, a3, a5, a7, t1, a2, &slow_elements);
4484 __ Ret(USE_DELAY_SLOT);
4485 __ mov(v0, a0);
4486}
4487
4488
4489void StubFailureTrampolineStub::Generate(MacroAssembler* masm) {
4490 CEntryStub ces(isolate(), 1, kSaveFPRegs);
4491 __ Call(ces.GetCode(), RelocInfo::CODE_TARGET);
4492 int parameter_count_offset =
4493 StubFailureTrampolineFrame::kCallerStackParameterCountFrameOffset;
4494 __ ld(a1, MemOperand(fp, parameter_count_offset));
4495 if (function_mode() == JS_FUNCTION_STUB_MODE) {
4496 __ Daddu(a1, a1, Operand(1));
4497 }
4498 masm->LeaveFrame(StackFrame::STUB_FAILURE_TRAMPOLINE);
4499 __ dsll(a1, a1, kPointerSizeLog2);
4500 __ Ret(USE_DELAY_SLOT);
4501 __ Daddu(sp, sp, a1);
4502}
4503
4504
4505void LoadICTrampolineStub::Generate(MacroAssembler* masm) {
4506 EmitLoadTypeFeedbackVector(masm, VectorLoadICDescriptor::VectorRegister());
4507 VectorLoadStub stub(isolate(), state());
4508 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4509}
4510
4511
4512void KeyedLoadICTrampolineStub::Generate(MacroAssembler* masm) {
4513 EmitLoadTypeFeedbackVector(masm, VectorLoadICDescriptor::VectorRegister());
4514 VectorKeyedLoadStub stub(isolate());
4515 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4516}
4517
4518
4519void ProfileEntryHookStub::MaybeCallEntryHook(MacroAssembler* masm) {
4520 if (masm->isolate()->function_entry_hook() != NULL) {
4521 ProfileEntryHookStub stub(masm->isolate());
4522 __ push(ra);
4523 __ CallStub(&stub);
4524 __ pop(ra);
4525 }
4526}
4527
4528
4529void ProfileEntryHookStub::Generate(MacroAssembler* masm) {
4530 // The entry hook is a "push ra" instruction, followed by a call.
4531 // Note: on MIPS "push" is 2 instruction
4532 const int32_t kReturnAddressDistanceFromFunctionStart =
4533 Assembler::kCallTargetAddressOffset + (2 * Assembler::kInstrSize);
4534
4535 // This should contain all kJSCallerSaved registers.
4536 const RegList kSavedRegs =
4537 kJSCallerSaved | // Caller saved registers.
4538 s5.bit(); // Saved stack pointer.
4539
4540 // We also save ra, so the count here is one higher than the mask indicates.
4541 const int32_t kNumSavedRegs = kNumJSCallerSaved + 2;
4542
4543 // Save all caller-save registers as this may be called from anywhere.
4544 __ MultiPush(kSavedRegs | ra.bit());
4545
4546 // Compute the function's address for the first argument.
4547 __ Dsubu(a0, ra, Operand(kReturnAddressDistanceFromFunctionStart));
4548
4549 // The caller's return address is above the saved temporaries.
4550 // Grab that for the second argument to the hook.
4551 __ Daddu(a1, sp, Operand(kNumSavedRegs * kPointerSize));
4552
4553 // Align the stack if necessary.
4554 int frame_alignment = masm->ActivationFrameAlignment();
4555 if (frame_alignment > kPointerSize) {
4556 __ mov(s5, sp);
4557 DCHECK(base::bits::IsPowerOfTwo32(frame_alignment));
4558 __ And(sp, sp, Operand(-frame_alignment));
4559 }
4560
4561 __ Dsubu(sp, sp, kCArgsSlotsSize);
4562#if defined(V8_HOST_ARCH_MIPS) || defined(V8_HOST_ARCH_MIPS64)
4563 int64_t entry_hook =
4564 reinterpret_cast<int64_t>(isolate()->function_entry_hook());
4565 __ li(t9, Operand(entry_hook));
4566#else
4567 // Under the simulator we need to indirect the entry hook through a
4568 // trampoline function at a known address.
4569 // It additionally takes an isolate as a third parameter.
4570 __ li(a2, Operand(ExternalReference::isolate_address(isolate())));
4571
4572 ApiFunction dispatcher(FUNCTION_ADDR(EntryHookTrampoline));
4573 __ li(t9, Operand(ExternalReference(&dispatcher,
4574 ExternalReference::BUILTIN_CALL,
4575 isolate())));
4576#endif
4577 // Call C function through t9 to conform ABI for PIC.
4578 __ Call(t9);
4579
4580 // Restore the stack pointer if needed.
4581 if (frame_alignment > kPointerSize) {
4582 __ mov(sp, s5);
4583 } else {
4584 __ Daddu(sp, sp, kCArgsSlotsSize);
4585 }
4586
4587 // Also pop ra to get Ret(0).
4588 __ MultiPop(kSavedRegs | ra.bit());
4589 __ Ret();
4590}
4591
4592
4593template<class T>
4594static void CreateArrayDispatch(MacroAssembler* masm,
4595 AllocationSiteOverrideMode mode) {
4596 if (mode == DISABLE_ALLOCATION_SITES) {
4597 T stub(masm->isolate(), GetInitialFastElementsKind(), mode);
4598 __ TailCallStub(&stub);
4599 } else if (mode == DONT_OVERRIDE) {
4600 int last_index = GetSequenceIndexFromFastElementsKind(
4601 TERMINAL_FAST_ELEMENTS_KIND);
4602 for (int i = 0; i <= last_index; ++i) {
4603 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4604 T stub(masm->isolate(), kind);
4605 __ TailCallStub(&stub, eq, a3, Operand(kind));
4606 }
4607
4608 // If we reached this point there is a problem.
4609 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4610 } else {
4611 UNREACHABLE();
4612 }
4613}
4614
4615
4616static void CreateArrayDispatchOneArgument(MacroAssembler* masm,
4617 AllocationSiteOverrideMode mode) {
4618 // a2 - allocation site (if mode != DISABLE_ALLOCATION_SITES)
4619 // a3 - kind (if mode != DISABLE_ALLOCATION_SITES)
4620 // a0 - number of arguments
4621 // a1 - constructor?
4622 // sp[0] - last argument
4623 Label normal_sequence;
4624 if (mode == DONT_OVERRIDE) {
4625 DCHECK(FAST_SMI_ELEMENTS == 0);
4626 DCHECK(FAST_HOLEY_SMI_ELEMENTS == 1);
4627 DCHECK(FAST_ELEMENTS == 2);
4628 DCHECK(FAST_HOLEY_ELEMENTS == 3);
4629 DCHECK(FAST_DOUBLE_ELEMENTS == 4);
4630 DCHECK(FAST_HOLEY_DOUBLE_ELEMENTS == 5);
4631
4632 // is the low bit set? If so, we are holey and that is good.
4633 __ And(at, a3, Operand(1));
4634 __ Branch(&normal_sequence, ne, at, Operand(zero_reg));
4635 }
4636 // look at the first argument
4637 __ ld(a5, MemOperand(sp, 0));
4638 __ Branch(&normal_sequence, eq, a5, Operand(zero_reg));
4639
4640 if (mode == DISABLE_ALLOCATION_SITES) {
4641 ElementsKind initial = GetInitialFastElementsKind();
4642 ElementsKind holey_initial = GetHoleyElementsKind(initial);
4643
4644 ArraySingleArgumentConstructorStub stub_holey(masm->isolate(),
4645 holey_initial,
4646 DISABLE_ALLOCATION_SITES);
4647 __ TailCallStub(&stub_holey);
4648
4649 __ bind(&normal_sequence);
4650 ArraySingleArgumentConstructorStub stub(masm->isolate(),
4651 initial,
4652 DISABLE_ALLOCATION_SITES);
4653 __ TailCallStub(&stub);
4654 } else if (mode == DONT_OVERRIDE) {
4655 // We are going to create a holey array, but our kind is non-holey.
4656 // Fix kind and retry (only if we have an allocation site in the slot).
4657 __ Daddu(a3, a3, Operand(1));
4658
4659 if (FLAG_debug_code) {
4660 __ ld(a5, FieldMemOperand(a2, 0));
4661 __ LoadRoot(at, Heap::kAllocationSiteMapRootIndex);
4662 __ Assert(eq, kExpectedAllocationSite, a5, Operand(at));
4663 }
4664
4665 // Save the resulting elements kind in type info. We can't just store a3
4666 // in the AllocationSite::transition_info field because elements kind is
4667 // restricted to a portion of the field...upper bits need to be left alone.
4668 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4669 __ ld(a4, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4670 __ Daddu(a4, a4, Operand(Smi::FromInt(kFastElementsKindPackedToHoley)));
4671 __ sd(a4, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4672
4673
4674 __ bind(&normal_sequence);
4675 int last_index = GetSequenceIndexFromFastElementsKind(
4676 TERMINAL_FAST_ELEMENTS_KIND);
4677 for (int i = 0; i <= last_index; ++i) {
4678 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4679 ArraySingleArgumentConstructorStub stub(masm->isolate(), kind);
4680 __ TailCallStub(&stub, eq, a3, Operand(kind));
4681 }
4682
4683 // If we reached this point there is a problem.
4684 __ Abort(kUnexpectedElementsKindInArrayConstructor);
4685 } else {
4686 UNREACHABLE();
4687 }
4688}
4689
4690
4691template<class T>
4692static void ArrayConstructorStubAheadOfTimeHelper(Isolate* isolate) {
4693 int to_index = GetSequenceIndexFromFastElementsKind(
4694 TERMINAL_FAST_ELEMENTS_KIND);
4695 for (int i = 0; i <= to_index; ++i) {
4696 ElementsKind kind = GetFastElementsKindFromSequenceIndex(i);
4697 T stub(isolate, kind);
4698 stub.GetCode();
4699 if (AllocationSite::GetMode(kind) != DONT_TRACK_ALLOCATION_SITE) {
4700 T stub1(isolate, kind, DISABLE_ALLOCATION_SITES);
4701 stub1.GetCode();
4702 }
4703 }
4704}
4705
4706
4707void ArrayConstructorStubBase::GenerateStubsAheadOfTime(Isolate* isolate) {
4708 ArrayConstructorStubAheadOfTimeHelper<ArrayNoArgumentConstructorStub>(
4709 isolate);
4710 ArrayConstructorStubAheadOfTimeHelper<ArraySingleArgumentConstructorStub>(
4711 isolate);
4712 ArrayConstructorStubAheadOfTimeHelper<ArrayNArgumentsConstructorStub>(
4713 isolate);
4714}
4715
4716
4717void InternalArrayConstructorStubBase::GenerateStubsAheadOfTime(
4718 Isolate* isolate) {
4719 ElementsKind kinds[2] = { FAST_ELEMENTS, FAST_HOLEY_ELEMENTS };
4720 for (int i = 0; i < 2; i++) {
4721 // For internal arrays we only need a few things.
4722 InternalArrayNoArgumentConstructorStub stubh1(isolate, kinds[i]);
4723 stubh1.GetCode();
4724 InternalArraySingleArgumentConstructorStub stubh2(isolate, kinds[i]);
4725 stubh2.GetCode();
4726 InternalArrayNArgumentsConstructorStub stubh3(isolate, kinds[i]);
4727 stubh3.GetCode();
4728 }
4729}
4730
4731
4732void ArrayConstructorStub::GenerateDispatchToArrayStub(
4733 MacroAssembler* masm,
4734 AllocationSiteOverrideMode mode) {
4735 if (argument_count() == ANY) {
4736 Label not_zero_case, not_one_case;
4737 __ And(at, a0, a0);
4738 __ Branch(&not_zero_case, ne, at, Operand(zero_reg));
4739 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4740
4741 __ bind(&not_zero_case);
4742 __ Branch(&not_one_case, gt, a0, Operand(1));
4743 CreateArrayDispatchOneArgument(masm, mode);
4744
4745 __ bind(&not_one_case);
4746 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4747 } else if (argument_count() == NONE) {
4748 CreateArrayDispatch<ArrayNoArgumentConstructorStub>(masm, mode);
4749 } else if (argument_count() == ONE) {
4750 CreateArrayDispatchOneArgument(masm, mode);
4751 } else if (argument_count() == MORE_THAN_ONE) {
4752 CreateArrayDispatch<ArrayNArgumentsConstructorStub>(masm, mode);
4753 } else {
4754 UNREACHABLE();
4755 }
4756}
4757
4758
4759void ArrayConstructorStub::Generate(MacroAssembler* masm) {
4760 // ----------- S t a t e -------------
4761 // -- a0 : argc (only if argument_count() == ANY)
4762 // -- a1 : constructor
4763 // -- a2 : AllocationSite or undefined
4764 // -- sp[0] : return address
4765 // -- sp[4] : last argument
4766 // -----------------------------------
4767
4768 if (FLAG_debug_code) {
4769 // The array construct code is only set for the global and natives
4770 // builtin Array functions which always have maps.
4771
4772 // Initial map for the builtin Array function should be a map.
4773 __ ld(a4, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4774 // Will both indicate a NULL and a Smi.
4775 __ SmiTst(a4, at);
4776 __ Assert(ne, kUnexpectedInitialMapForArrayFunction,
4777 at, Operand(zero_reg));
4778 __ GetObjectType(a4, a4, a5);
4779 __ Assert(eq, kUnexpectedInitialMapForArrayFunction,
4780 a5, Operand(MAP_TYPE));
4781
4782 // We should either have undefined in a2 or a valid AllocationSite
4783 __ AssertUndefinedOrAllocationSite(a2, a4);
4784 }
4785
4786 Label no_info;
4787 // Get the elements kind and case on that.
4788 __ LoadRoot(at, Heap::kUndefinedValueRootIndex);
4789 __ Branch(&no_info, eq, a2, Operand(at));
4790
4791 __ ld(a3, FieldMemOperand(a2, AllocationSite::kTransitionInfoOffset));
4792 __ SmiUntag(a3);
4793 STATIC_ASSERT(AllocationSite::ElementsKindBits::kShift == 0);
4794 __ And(a3, a3, Operand(AllocationSite::ElementsKindBits::kMask));
4795 GenerateDispatchToArrayStub(masm, DONT_OVERRIDE);
4796
4797 __ bind(&no_info);
4798 GenerateDispatchToArrayStub(masm, DISABLE_ALLOCATION_SITES);
4799}
4800
4801
4802void InternalArrayConstructorStub::GenerateCase(
4803 MacroAssembler* masm, ElementsKind kind) {
4804
4805 InternalArrayNoArgumentConstructorStub stub0(isolate(), kind);
4806 __ TailCallStub(&stub0, lo, a0, Operand(1));
4807
4808 InternalArrayNArgumentsConstructorStub stubN(isolate(), kind);
4809 __ TailCallStub(&stubN, hi, a0, Operand(1));
4810
4811 if (IsFastPackedElementsKind(kind)) {
4812 // We might need to create a holey array
4813 // look at the first argument.
4814 __ ld(at, MemOperand(sp, 0));
4815
4816 InternalArraySingleArgumentConstructorStub
4817 stub1_holey(isolate(), GetHoleyElementsKind(kind));
4818 __ TailCallStub(&stub1_holey, ne, at, Operand(zero_reg));
4819 }
4820
4821 InternalArraySingleArgumentConstructorStub stub1(isolate(), kind);
4822 __ TailCallStub(&stub1);
4823}
4824
4825
4826void InternalArrayConstructorStub::Generate(MacroAssembler* masm) {
4827 // ----------- S t a t e -------------
4828 // -- a0 : argc
4829 // -- a1 : constructor
4830 // -- sp[0] : return address
4831 // -- sp[4] : last argument
4832 // -----------------------------------
4833
4834 if (FLAG_debug_code) {
4835 // The array construct code is only set for the global and natives
4836 // builtin Array functions which always have maps.
4837
4838 // Initial map for the builtin Array function should be a map.
4839 __ ld(a3, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4840 // Will both indicate a NULL and a Smi.
4841 __ SmiTst(a3, at);
4842 __ Assert(ne, kUnexpectedInitialMapForArrayFunction,
4843 at, Operand(zero_reg));
4844 __ GetObjectType(a3, a3, a4);
4845 __ Assert(eq, kUnexpectedInitialMapForArrayFunction,
4846 a4, Operand(MAP_TYPE));
4847 }
4848
4849 // Figure out the right elements kind.
4850 __ ld(a3, FieldMemOperand(a1, JSFunction::kPrototypeOrInitialMapOffset));
4851
4852 // Load the map's "bit field 2" into a3. We only need the first byte,
4853 // but the following bit field extraction takes care of that anyway.
4854 __ lbu(a3, FieldMemOperand(a3, Map::kBitField2Offset));
4855 // Retrieve elements_kind from bit field 2.
4856 __ DecodeField<Map::ElementsKindBits>(a3);
4857
4858 if (FLAG_debug_code) {
4859 Label done;
4860 __ Branch(&done, eq, a3, Operand(FAST_ELEMENTS));
4861 __ Assert(
4862 eq, kInvalidElementsKindForInternalArrayOrInternalPackedArray,
4863 a3, Operand(FAST_HOLEY_ELEMENTS));
4864 __ bind(&done);
4865 }
4866
4867 Label fast_elements_case;
4868 __ Branch(&fast_elements_case, eq, a3, Operand(FAST_ELEMENTS));
4869 GenerateCase(masm, FAST_HOLEY_ELEMENTS);
4870
4871 __ bind(&fast_elements_case);
4872 GenerateCase(masm, FAST_ELEMENTS);
4873}
4874
4875
4876void CallApiFunctionStub::Generate(MacroAssembler* masm) {
4877 // ----------- S t a t e -------------
4878 // -- a0 : callee
4879 // -- a4 : call_data
4880 // -- a2 : holder
4881 // -- a1 : api_function_address
4882 // -- cp : context
4883 // --
4884 // -- sp[0] : last argument
4885 // -- ...
4886 // -- sp[(argc - 1)* 4] : first argument
4887 // -- sp[argc * 4] : receiver
4888 // -----------------------------------
4889
4890 Register callee = a0;
4891 Register call_data = a4;
4892 Register holder = a2;
4893 Register api_function_address = a1;
4894 Register context = cp;
4895
4896 int argc = this->argc();
4897 bool is_store = this->is_store();
4898 bool call_data_undefined = this->call_data_undefined();
4899
4900 typedef FunctionCallbackArguments FCA;
4901
4902 STATIC_ASSERT(FCA::kContextSaveIndex == 6);
4903 STATIC_ASSERT(FCA::kCalleeIndex == 5);
4904 STATIC_ASSERT(FCA::kDataIndex == 4);
4905 STATIC_ASSERT(FCA::kReturnValueOffset == 3);
4906 STATIC_ASSERT(FCA::kReturnValueDefaultValueIndex == 2);
4907 STATIC_ASSERT(FCA::kIsolateIndex == 1);
4908 STATIC_ASSERT(FCA::kHolderIndex == 0);
4909 STATIC_ASSERT(FCA::kArgsLength == 7);
4910
4911 // Save context, callee and call data.
4912 __ Push(context, callee, call_data);
4913 // Load context from callee.
4914 __ ld(context, FieldMemOperand(callee, JSFunction::kContextOffset));
4915
4916 Register scratch = call_data;
4917 if (!call_data_undefined) {
4918 __ LoadRoot(scratch, Heap::kUndefinedValueRootIndex);
4919 }
4920 // Push return value and default return value.
4921 __ Push(scratch, scratch);
4922 __ li(scratch,
4923 Operand(ExternalReference::isolate_address(isolate())));
4924 // Push isolate and holder.
4925 __ Push(scratch, holder);
4926
4927 // Prepare arguments.
4928 __ mov(scratch, sp);
4929
4930 // Allocate the v8::Arguments structure in the arguments' space since
4931 // it's not controlled by GC.
4932 const int kApiStackSpace = 4;
4933
4934 FrameScope frame_scope(masm, StackFrame::MANUAL);
4935 __ EnterExitFrame(false, kApiStackSpace);
4936
4937 DCHECK(!api_function_address.is(a0) && !scratch.is(a0));
4938 // a0 = FunctionCallbackInfo&
4939 // Arguments is after the return address.
4940 __ Daddu(a0, sp, Operand(1 * kPointerSize));
4941 // FunctionCallbackInfo::implicit_args_
4942 __ sd(scratch, MemOperand(a0, 0 * kPointerSize));
4943 // FunctionCallbackInfo::values_
4944 __ Daddu(at, scratch, Operand((FCA::kArgsLength - 1 + argc) * kPointerSize));
4945 __ sd(at, MemOperand(a0, 1 * kPointerSize));
4946 // FunctionCallbackInfo::length_ = argc
4947 __ li(at, Operand(argc));
4948 __ sd(at, MemOperand(a0, 2 * kPointerSize));
4949 // FunctionCallbackInfo::is_construct_call = 0
4950 __ sd(zero_reg, MemOperand(a0, 3 * kPointerSize));
4951
4952 const int kStackUnwindSpace = argc + FCA::kArgsLength + 1;
4953 ExternalReference thunk_ref =
4954 ExternalReference::invoke_function_callback(isolate());
4955
4956 AllowExternalCallThatCantCauseGC scope(masm);
4957 MemOperand context_restore_operand(
4958 fp, (2 + FCA::kContextSaveIndex) * kPointerSize);
4959 // Stores return the first js argument.
4960 int return_value_offset = 0;
4961 if (is_store) {
4962 return_value_offset = 2 + FCA::kArgsLength;
4963 } else {
4964 return_value_offset = 2 + FCA::kReturnValueOffset;
4965 }
4966 MemOperand return_value_operand(fp, return_value_offset * kPointerSize);
4967
4968 __ CallApiFunctionAndReturn(api_function_address,
4969 thunk_ref,
4970 kStackUnwindSpace,
4971 return_value_operand,
4972 &context_restore_operand);
4973}
4974
4975
4976void CallApiGetterStub::Generate(MacroAssembler* masm) {
4977 // ----------- S t a t e -------------
4978 // -- sp[0] : name
4979 // -- sp[4 - kArgsLength*4] : PropertyCallbackArguments object
4980 // -- ...
4981 // -- a2 : api_function_address
4982 // -----------------------------------
4983
4984 Register api_function_address = ApiGetterDescriptor::function_address();
4985 DCHECK(api_function_address.is(a2));
4986
4987 __ mov(a0, sp); // a0 = Handle<Name>
4988 __ Daddu(a1, a0, Operand(1 * kPointerSize)); // a1 = PCA
4989
4990 const int kApiStackSpace = 1;
4991 FrameScope frame_scope(masm, StackFrame::MANUAL);
4992 __ EnterExitFrame(false, kApiStackSpace);
4993
4994 // Create PropertyAccessorInfo instance on the stack above the exit frame with
4995 // a1 (internal::Object** args_) as the data.
4996 __ sd(a1, MemOperand(sp, 1 * kPointerSize));
4997 __ Daddu(a1, sp, Operand(1 * kPointerSize)); // a1 = AccessorInfo&
4998
4999 const int kStackUnwindSpace = PropertyCallbackArguments::kArgsLength + 1;
5000
5001 ExternalReference thunk_ref =
5002 ExternalReference::invoke_accessor_getter_callback(isolate());
5003 __ CallApiFunctionAndReturn(api_function_address,
5004 thunk_ref,
5005 kStackUnwindSpace,
5006 MemOperand(fp, 6 * kPointerSize),
5007 NULL);
5008}
5009
5010
5011#undef __
5012
5013} } // namespace v8::internal
5014
5015#endif // V8_TARGET_ARCH_MIPS64