blob: f75ee8bde8add583efd789b1ce5b2e82a7fb0451 [file] [log] [blame]
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001// Copyright 2010 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#if defined(V8_TARGET_ARCH_ARM)
31
32#include "bootstrapper.h"
33#include "code-stubs.h"
34#include "regexp-macro-assembler.h"
35
36namespace v8 {
37namespace internal {
38
39
40#define __ ACCESS_MASM(masm)
41
42static void EmitIdenticalObjectComparison(MacroAssembler* masm,
43 Label* slow,
44 Condition cc,
45 bool never_nan_nan);
46static void EmitSmiNonsmiComparison(MacroAssembler* masm,
47 Register lhs,
48 Register rhs,
49 Label* lhs_not_nan,
50 Label* slow,
51 bool strict);
52static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
53static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
54 Register lhs,
55 Register rhs);
56
57
58void FastNewClosureStub::Generate(MacroAssembler* masm) {
59 // Create a new closure from the given function info in new
60 // space. Set the context to the current context in cp.
61 Label gc;
62
63 // Pop the function info from the stack.
64 __ pop(r3);
65
66 // Attempt to allocate new JSFunction in new space.
67 __ AllocateInNewSpace(JSFunction::kSize,
68 r0,
69 r1,
70 r2,
71 &gc,
72 TAG_OBJECT);
73
74 // Compute the function map in the current global context and set that
75 // as the map of the allocated object.
76 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
77 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
78 __ ldr(r2, MemOperand(r2, Context::SlotOffset(Context::FUNCTION_MAP_INDEX)));
79 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
80
81 // Initialize the rest of the function. We don't have to update the
82 // write barrier because the allocated object is in new space.
83 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
84 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
85 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
86 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
87 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
88 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
89 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
90 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
91
92 // Initialize the code pointer in the function to be the one
93 // found in the shared function info object.
94 __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset));
95 __ add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
96 __ str(r3, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
97
98 // Return result. The argument function info has been popped already.
99 __ Ret();
100
101 // Create a new closure through the slower runtime call.
102 __ bind(&gc);
103 __ Push(cp, r3);
104 __ TailCallRuntime(Runtime::kNewClosure, 2, 1);
105}
106
107
108void FastNewContextStub::Generate(MacroAssembler* masm) {
109 // Try to allocate the context in new space.
110 Label gc;
111 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
112
113 // Attempt to allocate the context in new space.
114 __ AllocateInNewSpace(FixedArray::SizeFor(length),
115 r0,
116 r1,
117 r2,
118 &gc,
119 TAG_OBJECT);
120
121 // Load the function from the stack.
122 __ ldr(r3, MemOperand(sp, 0));
123
124 // Setup the object header.
125 __ LoadRoot(r2, Heap::kContextMapRootIndex);
126 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
127 __ mov(r2, Operand(Smi::FromInt(length)));
128 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
129
130 // Setup the fixed slots.
131 __ mov(r1, Operand(Smi::FromInt(0)));
132 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
133 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
134 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
135 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
136
137 // Copy the global object from the surrounding context.
138 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
139 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
140
141 // Initialize the rest of the slots to undefined.
142 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
143 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
144 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
145 }
146
147 // Remove the on-stack argument and return.
148 __ mov(cp, r0);
149 __ pop();
150 __ Ret();
151
152 // Need to collect. Call into runtime system.
153 __ bind(&gc);
154 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
155}
156
157
158void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
159 // Stack layout on entry:
160 //
161 // [sp]: constant elements.
162 // [sp + kPointerSize]: literal index.
163 // [sp + (2 * kPointerSize)]: literals array.
164
165 // All sizes here are multiples of kPointerSize.
166 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
167 int size = JSArray::kSize + elements_size;
168
169 // Load boilerplate object into r3 and check if we need to create a
170 // boilerplate.
171 Label slow_case;
172 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
173 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
174 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
175 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
176 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
177 __ cmp(r3, ip);
178 __ b(eq, &slow_case);
179
180 if (FLAG_debug_code) {
181 const char* message;
182 Heap::RootListIndex expected_map_index;
183 if (mode_ == CLONE_ELEMENTS) {
184 message = "Expected (writable) fixed array";
185 expected_map_index = Heap::kFixedArrayMapRootIndex;
186 } else {
187 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
188 message = "Expected copy-on-write fixed array";
189 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
190 }
191 __ push(r3);
192 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
193 __ ldr(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
194 __ LoadRoot(ip, expected_map_index);
195 __ cmp(r3, ip);
196 __ Assert(eq, message);
197 __ pop(r3);
198 }
199
200 // Allocate both the JS array and the elements array in one big
201 // allocation. This avoids multiple limit checks.
202 __ AllocateInNewSpace(size,
203 r0,
204 r1,
205 r2,
206 &slow_case,
207 TAG_OBJECT);
208
209 // Copy the JS array part.
210 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
211 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
212 __ ldr(r1, FieldMemOperand(r3, i));
213 __ str(r1, FieldMemOperand(r0, i));
214 }
215 }
216
217 if (length_ > 0) {
218 // Get hold of the elements array of the boilerplate and setup the
219 // elements pointer in the resulting object.
220 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
221 __ add(r2, r0, Operand(JSArray::kSize));
222 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
223
224 // Copy the elements array.
225 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
226 }
227
228 // Return and remove the on-stack parameters.
229 __ add(sp, sp, Operand(3 * kPointerSize));
230 __ Ret();
231
232 __ bind(&slow_case);
233 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
234}
235
236
237// Takes a Smi and converts to an IEEE 64 bit floating point value in two
238// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
239// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
240// scratch register. Destroys the source register. No GC occurs during this
241// stub so you don't have to set up the frame.
242class ConvertToDoubleStub : public CodeStub {
243 public:
244 ConvertToDoubleStub(Register result_reg_1,
245 Register result_reg_2,
246 Register source_reg,
247 Register scratch_reg)
248 : result1_(result_reg_1),
249 result2_(result_reg_2),
250 source_(source_reg),
251 zeros_(scratch_reg) { }
252
253 private:
254 Register result1_;
255 Register result2_;
256 Register source_;
257 Register zeros_;
258
259 // Minor key encoding in 16 bits.
260 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
261 class OpBits: public BitField<Token::Value, 2, 14> {};
262
263 Major MajorKey() { return ConvertToDouble; }
264 int MinorKey() {
265 // Encode the parameters in a unique 16 bit value.
266 return result1_.code() +
267 (result2_.code() << 4) +
268 (source_.code() << 8) +
269 (zeros_.code() << 12);
270 }
271
272 void Generate(MacroAssembler* masm);
273
274 const char* GetName() { return "ConvertToDoubleStub"; }
275
276#ifdef DEBUG
277 void Print() { PrintF("ConvertToDoubleStub\n"); }
278#endif
279};
280
281
282void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
283#ifndef BIG_ENDIAN_FLOATING_POINT
284 Register exponent = result1_;
285 Register mantissa = result2_;
286#else
287 Register exponent = result2_;
288 Register mantissa = result1_;
289#endif
290 Label not_special;
291 // Convert from Smi to integer.
292 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
293 // Move sign bit from source to destination. This works because the sign bit
294 // in the exponent word of the double has the same position and polarity as
295 // the 2's complement sign bit in a Smi.
296 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
297 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
298 // Subtract from 0 if source was negative.
299 __ rsb(source_, source_, Operand(0), LeaveCC, ne);
300
301 // We have -1, 0 or 1, which we treat specially. Register source_ contains
302 // absolute value: it is either equal to 1 (special case of -1 and 1),
303 // greater than 1 (not a special case) or less than 1 (special case of 0).
304 __ cmp(source_, Operand(1));
305 __ b(gt, &not_special);
306
307 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
308 static const uint32_t exponent_word_for_1 =
309 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
310 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
311 // 1, 0 and -1 all have 0 for the second word.
312 __ mov(mantissa, Operand(0));
313 __ Ret();
314
315 __ bind(&not_special);
316 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
317 // Gets the wrong answer for 0, but we already checked for that case above.
318 __ CountLeadingZeros(zeros_, source_, mantissa);
319 // Compute exponent and or it into the exponent register.
320 // We use mantissa as a scratch register here. Use a fudge factor to
321 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
322 // that fit in the ARM's constant field.
323 int fudge = 0x400;
324 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
325 __ add(mantissa, mantissa, Operand(fudge));
326 __ orr(exponent,
327 exponent,
328 Operand(mantissa, LSL, HeapNumber::kExponentShift));
329 // Shift up the source chopping the top bit off.
330 __ add(zeros_, zeros_, Operand(1));
331 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
332 __ mov(source_, Operand(source_, LSL, zeros_));
333 // Compute lower part of fraction (last 12 bits).
334 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
335 // And the top (top 20 bits).
336 __ orr(exponent,
337 exponent,
338 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
339 __ Ret();
340}
341
342
343// See comment for class.
344void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
345 Label max_negative_int;
346 // the_int_ has the answer which is a signed int32 but not a Smi.
347 // We test for the special value that has a different exponent. This test
348 // has the neat side effect of setting the flags according to the sign.
349 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
350 __ cmp(the_int_, Operand(0x80000000u));
351 __ b(eq, &max_negative_int);
352 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
353 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
354 uint32_t non_smi_exponent =
355 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
356 __ mov(scratch_, Operand(non_smi_exponent));
357 // Set the sign bit in scratch_ if the value was negative.
358 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
359 // Subtract from 0 if the value was negative.
360 __ rsb(the_int_, the_int_, Operand(0), LeaveCC, cs);
361 // We should be masking the implict first digit of the mantissa away here,
362 // but it just ends up combining harmlessly with the last digit of the
363 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
364 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
365 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
366 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
367 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
368 __ str(scratch_, FieldMemOperand(the_heap_number_,
369 HeapNumber::kExponentOffset));
370 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
371 __ str(scratch_, FieldMemOperand(the_heap_number_,
372 HeapNumber::kMantissaOffset));
373 __ Ret();
374
375 __ bind(&max_negative_int);
376 // The max negative int32 is stored as a positive number in the mantissa of
377 // a double because it uses a sign bit instead of using two's complement.
378 // The actual mantissa bits stored are all 0 because the implicit most
379 // significant 1 bit is not stored.
380 non_smi_exponent += 1 << HeapNumber::kExponentShift;
381 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
382 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
383 __ mov(ip, Operand(0));
384 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
385 __ Ret();
386}
387
388
389// Handle the case where the lhs and rhs are the same object.
390// Equality is almost reflexive (everything but NaN), so this is a test
391// for "identity and not NaN".
392static void EmitIdenticalObjectComparison(MacroAssembler* masm,
393 Label* slow,
394 Condition cc,
395 bool never_nan_nan) {
396 Label not_identical;
397 Label heap_number, return_equal;
398 __ cmp(r0, r1);
399 __ b(ne, &not_identical);
400
401 // The two objects are identical. If we know that one of them isn't NaN then
402 // we now know they test equal.
403 if (cc != eq || !never_nan_nan) {
404 // Test for NaN. Sadly, we can't just compare to Factory::nan_value(),
405 // so we do the second best thing - test it ourselves.
406 // They are both equal and they are not both Smis so both of them are not
407 // Smis. If it's not a heap number, then return equal.
408 if (cc == lt || cc == gt) {
409 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
410 __ b(ge, slow);
411 } else {
412 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
413 __ b(eq, &heap_number);
414 // Comparing JS objects with <=, >= is complicated.
415 if (cc != eq) {
416 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
417 __ b(ge, slow);
418 // Normally here we fall through to return_equal, but undefined is
419 // special: (undefined == undefined) == true, but
420 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
421 if (cc == le || cc == ge) {
422 __ cmp(r4, Operand(ODDBALL_TYPE));
423 __ b(ne, &return_equal);
424 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
425 __ cmp(r0, r2);
426 __ b(ne, &return_equal);
427 if (cc == le) {
428 // undefined <= undefined should fail.
429 __ mov(r0, Operand(GREATER));
430 } else {
431 // undefined >= undefined should fail.
432 __ mov(r0, Operand(LESS));
433 }
434 __ Ret();
435 }
436 }
437 }
438 }
439
440 __ bind(&return_equal);
441 if (cc == lt) {
442 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
443 } else if (cc == gt) {
444 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
445 } else {
446 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
447 }
448 __ Ret();
449
450 if (cc != eq || !never_nan_nan) {
451 // For less and greater we don't have to check for NaN since the result of
452 // x < x is false regardless. For the others here is some code to check
453 // for NaN.
454 if (cc != lt && cc != gt) {
455 __ bind(&heap_number);
456 // It is a heap number, so return non-equal if it's NaN and equal if it's
457 // not NaN.
458
459 // The representation of NaN values has all exponent bits (52..62) set,
460 // and not all mantissa bits (0..51) clear.
461 // Read top bits of double representation (second word of value).
462 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
463 // Test that exponent bits are all set.
464 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
465 // NaNs have all-one exponents so they sign extend to -1.
466 __ cmp(r3, Operand(-1));
467 __ b(ne, &return_equal);
468
469 // Shift out flag and all exponent bits, retaining only mantissa.
470 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
471 // Or with all low-bits of mantissa.
472 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
473 __ orr(r0, r3, Operand(r2), SetCC);
474 // For equal we already have the right value in r0: Return zero (equal)
475 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
476 // not (it's a NaN). For <= and >= we need to load r0 with the failing
477 // value if it's a NaN.
478 if (cc != eq) {
479 // All-zero means Infinity means equal.
480 __ Ret(eq);
481 if (cc == le) {
482 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
483 } else {
484 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
485 }
486 }
487 __ Ret();
488 }
489 // No fall through here.
490 }
491
492 __ bind(&not_identical);
493}
494
495
496// See comment at call site.
497static void EmitSmiNonsmiComparison(MacroAssembler* masm,
498 Register lhs,
499 Register rhs,
500 Label* lhs_not_nan,
501 Label* slow,
502 bool strict) {
503 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
504 (lhs.is(r1) && rhs.is(r0)));
505
506 Label rhs_is_smi;
507 __ tst(rhs, Operand(kSmiTagMask));
508 __ b(eq, &rhs_is_smi);
509
510 // Lhs is a Smi. Check whether the rhs is a heap number.
511 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
512 if (strict) {
513 // If rhs is not a number and lhs is a Smi then strict equality cannot
514 // succeed. Return non-equal
515 // If rhs is r0 then there is already a non zero value in it.
516 if (!rhs.is(r0)) {
517 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
518 }
519 __ Ret(ne);
520 } else {
521 // Smi compared non-strictly with a non-Smi non-heap-number. Call
522 // the runtime.
523 __ b(ne, slow);
524 }
525
526 // Lhs is a smi, rhs is a number.
527 if (CpuFeatures::IsSupported(VFP3)) {
528 // Convert lhs to a double in d7.
529 CpuFeatures::Scope scope(VFP3);
530 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
531 // Load the double from rhs, tagged HeapNumber r0, to d6.
532 __ sub(r7, rhs, Operand(kHeapObjectTag));
533 __ vldr(d6, r7, HeapNumber::kValueOffset);
534 } else {
535 __ push(lr);
536 // Convert lhs to a double in r2, r3.
537 __ mov(r7, Operand(lhs));
538 ConvertToDoubleStub stub1(r3, r2, r7, r6);
539 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
540 // Load rhs to a double in r0, r1.
541 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
542 __ pop(lr);
543 }
544
545 // We now have both loaded as doubles but we can skip the lhs nan check
546 // since it's a smi.
547 __ jmp(lhs_not_nan);
548
549 __ bind(&rhs_is_smi);
550 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
551 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
552 if (strict) {
553 // If lhs is not a number and rhs is a smi then strict equality cannot
554 // succeed. Return non-equal.
555 // If lhs is r0 then there is already a non zero value in it.
556 if (!lhs.is(r0)) {
557 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
558 }
559 __ Ret(ne);
560 } else {
561 // Smi compared non-strictly with a non-smi non-heap-number. Call
562 // the runtime.
563 __ b(ne, slow);
564 }
565
566 // Rhs is a smi, lhs is a heap number.
567 if (CpuFeatures::IsSupported(VFP3)) {
568 CpuFeatures::Scope scope(VFP3);
569 // Load the double from lhs, tagged HeapNumber r1, to d7.
570 __ sub(r7, lhs, Operand(kHeapObjectTag));
571 __ vldr(d7, r7, HeapNumber::kValueOffset);
572 // Convert rhs to a double in d6 .
573 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
574 } else {
575 __ push(lr);
576 // Load lhs to a double in r2, r3.
577 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
578 // Convert rhs to a double in r0, r1.
579 __ mov(r7, Operand(rhs));
580 ConvertToDoubleStub stub2(r1, r0, r7, r6);
581 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
582 __ pop(lr);
583 }
584 // Fall through to both_loaded_as_doubles.
585}
586
587
588void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cc) {
589 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
590 Register rhs_exponent = exp_first ? r0 : r1;
591 Register lhs_exponent = exp_first ? r2 : r3;
592 Register rhs_mantissa = exp_first ? r1 : r0;
593 Register lhs_mantissa = exp_first ? r3 : r2;
594 Label one_is_nan, neither_is_nan;
595
596 __ Sbfx(r4,
597 lhs_exponent,
598 HeapNumber::kExponentShift,
599 HeapNumber::kExponentBits);
600 // NaNs have all-one exponents so they sign extend to -1.
601 __ cmp(r4, Operand(-1));
602 __ b(ne, lhs_not_nan);
603 __ mov(r4,
604 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
605 SetCC);
606 __ b(ne, &one_is_nan);
607 __ cmp(lhs_mantissa, Operand(0));
608 __ b(ne, &one_is_nan);
609
610 __ bind(lhs_not_nan);
611 __ Sbfx(r4,
612 rhs_exponent,
613 HeapNumber::kExponentShift,
614 HeapNumber::kExponentBits);
615 // NaNs have all-one exponents so they sign extend to -1.
616 __ cmp(r4, Operand(-1));
617 __ b(ne, &neither_is_nan);
618 __ mov(r4,
619 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
620 SetCC);
621 __ b(ne, &one_is_nan);
622 __ cmp(rhs_mantissa, Operand(0));
623 __ b(eq, &neither_is_nan);
624
625 __ bind(&one_is_nan);
626 // NaN comparisons always fail.
627 // Load whatever we need in r0 to make the comparison fail.
628 if (cc == lt || cc == le) {
629 __ mov(r0, Operand(GREATER));
630 } else {
631 __ mov(r0, Operand(LESS));
632 }
633 __ Ret();
634
635 __ bind(&neither_is_nan);
636}
637
638
639// See comment at call site.
640static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
641 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
642 Register rhs_exponent = exp_first ? r0 : r1;
643 Register lhs_exponent = exp_first ? r2 : r3;
644 Register rhs_mantissa = exp_first ? r1 : r0;
645 Register lhs_mantissa = exp_first ? r3 : r2;
646
647 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
648 if (cc == eq) {
649 // Doubles are not equal unless they have the same bit pattern.
650 // Exception: 0 and -0.
651 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
652 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
653 // Return non-zero if the numbers are unequal.
654 __ Ret(ne);
655
656 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
657 // If exponents are equal then return 0.
658 __ Ret(eq);
659
660 // Exponents are unequal. The only way we can return that the numbers
661 // are equal is if one is -0 and the other is 0. We already dealt
662 // with the case where both are -0 or both are 0.
663 // We start by seeing if the mantissas (that are equal) or the bottom
664 // 31 bits of the rhs exponent are non-zero. If so we return not
665 // equal.
666 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
667 __ mov(r0, Operand(r4), LeaveCC, ne);
668 __ Ret(ne);
669 // Now they are equal if and only if the lhs exponent is zero in its
670 // low 31 bits.
671 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
672 __ Ret();
673 } else {
674 // Call a native function to do a comparison between two non-NaNs.
675 // Call C routine that may not cause GC or other trouble.
676 __ push(lr);
677 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
678 __ CallCFunction(ExternalReference::compare_doubles(), 4);
679 __ pop(pc); // Return.
680 }
681}
682
683
684// See comment at call site.
685static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
686 Register lhs,
687 Register rhs) {
688 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
689 (lhs.is(r1) && rhs.is(r0)));
690
691 // If either operand is a JSObject or an oddball value, then they are
692 // not equal since their pointers are different.
693 // There is no test for undetectability in strict equality.
694 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
695 Label first_non_object;
696 // Get the type of the first operand into r2 and compare it with
697 // FIRST_JS_OBJECT_TYPE.
698 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
699 __ b(lt, &first_non_object);
700
701 // Return non-zero (r0 is not zero)
702 Label return_not_equal;
703 __ bind(&return_not_equal);
704 __ Ret();
705
706 __ bind(&first_non_object);
707 // Check for oddballs: true, false, null, undefined.
708 __ cmp(r2, Operand(ODDBALL_TYPE));
709 __ b(eq, &return_not_equal);
710
711 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
712 __ b(ge, &return_not_equal);
713
714 // Check for oddballs: true, false, null, undefined.
715 __ cmp(r3, Operand(ODDBALL_TYPE));
716 __ b(eq, &return_not_equal);
717
718 // Now that we have the types we might as well check for symbol-symbol.
719 // Ensure that no non-strings have the symbol bit set.
720 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
721 STATIC_ASSERT(kSymbolTag != 0);
722 __ and_(r2, r2, Operand(r3));
723 __ tst(r2, Operand(kIsSymbolMask));
724 __ b(ne, &return_not_equal);
725}
726
727
728// See comment at call site.
729static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
730 Register lhs,
731 Register rhs,
732 Label* both_loaded_as_doubles,
733 Label* not_heap_numbers,
734 Label* slow) {
735 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
736 (lhs.is(r1) && rhs.is(r0)));
737
738 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
739 __ b(ne, not_heap_numbers);
740 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
741 __ cmp(r2, r3);
742 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
743
744 // Both are heap numbers. Load them up then jump to the code we have
745 // for that.
746 if (CpuFeatures::IsSupported(VFP3)) {
747 CpuFeatures::Scope scope(VFP3);
748 __ sub(r7, rhs, Operand(kHeapObjectTag));
749 __ vldr(d6, r7, HeapNumber::kValueOffset);
750 __ sub(r7, lhs, Operand(kHeapObjectTag));
751 __ vldr(d7, r7, HeapNumber::kValueOffset);
752 } else {
753 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
754 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
755 }
756 __ jmp(both_loaded_as_doubles);
757}
758
759
760// Fast negative check for symbol-to-symbol equality.
761static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
762 Register lhs,
763 Register rhs,
764 Label* possible_strings,
765 Label* not_both_strings) {
766 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
767 (lhs.is(r1) && rhs.is(r0)));
768
769 // r2 is object type of rhs.
770 // Ensure that no non-strings have the symbol bit set.
771 Label object_test;
772 STATIC_ASSERT(kSymbolTag != 0);
773 __ tst(r2, Operand(kIsNotStringMask));
774 __ b(ne, &object_test);
775 __ tst(r2, Operand(kIsSymbolMask));
776 __ b(eq, possible_strings);
777 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
778 __ b(ge, not_both_strings);
779 __ tst(r3, Operand(kIsSymbolMask));
780 __ b(eq, possible_strings);
781
782 // Both are symbols. We already checked they weren't the same pointer
783 // so they are not equal.
784 __ mov(r0, Operand(NOT_EQUAL));
785 __ Ret();
786
787 __ bind(&object_test);
788 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
789 __ b(lt, not_both_strings);
790 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
791 __ b(lt, not_both_strings);
792 // If both objects are undetectable, they are equal. Otherwise, they
793 // are not equal, since they are different objects and an object is not
794 // equal to undefined.
795 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
796 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
797 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
798 __ and_(r0, r2, Operand(r3));
799 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
800 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
801 __ Ret();
802}
803
804
805void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
806 Register object,
807 Register result,
808 Register scratch1,
809 Register scratch2,
810 Register scratch3,
811 bool object_is_smi,
812 Label* not_found) {
813 // Use of registers. Register result is used as a temporary.
814 Register number_string_cache = result;
815 Register mask = scratch3;
816
817 // Load the number string cache.
818 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
819
820 // Make the hash mask from the length of the number string cache. It
821 // contains two elements (number and string) for each cache entry.
822 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
823 // Divide length by two (length is a smi).
824 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
825 __ sub(mask, mask, Operand(1)); // Make mask.
826
827 // Calculate the entry in the number string cache. The hash value in the
828 // number string cache for smis is just the smi value, and the hash for
829 // doubles is the xor of the upper and lower words. See
830 // Heap::GetNumberStringCache.
831 Label is_smi;
832 Label load_result_from_cache;
833 if (!object_is_smi) {
834 __ BranchOnSmi(object, &is_smi);
835 if (CpuFeatures::IsSupported(VFP3)) {
836 CpuFeatures::Scope scope(VFP3);
837 __ CheckMap(object,
838 scratch1,
839 Heap::kHeapNumberMapRootIndex,
840 not_found,
841 true);
842
843 STATIC_ASSERT(8 == kDoubleSize);
844 __ add(scratch1,
845 object,
846 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
847 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
848 __ eor(scratch1, scratch1, Operand(scratch2));
849 __ and_(scratch1, scratch1, Operand(mask));
850
851 // Calculate address of entry in string cache: each entry consists
852 // of two pointer sized fields.
853 __ add(scratch1,
854 number_string_cache,
855 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
856
857 Register probe = mask;
858 __ ldr(probe,
859 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
860 __ BranchOnSmi(probe, not_found);
861 __ sub(scratch2, object, Operand(kHeapObjectTag));
862 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
863 __ sub(probe, probe, Operand(kHeapObjectTag));
864 __ vldr(d1, probe, HeapNumber::kValueOffset);
865 __ vcmp(d0, d1);
866 __ vmrs(pc);
867 __ b(ne, not_found); // The cache did not contain this value.
868 __ b(&load_result_from_cache);
869 } else {
870 __ b(not_found);
871 }
872 }
873
874 __ bind(&is_smi);
875 Register scratch = scratch1;
876 __ and_(scratch, mask, Operand(object, ASR, 1));
877 // Calculate address of entry in string cache: each entry consists
878 // of two pointer sized fields.
879 __ add(scratch,
880 number_string_cache,
881 Operand(scratch, LSL, kPointerSizeLog2 + 1));
882
883 // Check if the entry is the smi we are looking for.
884 Register probe = mask;
885 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
886 __ cmp(object, probe);
887 __ b(ne, not_found);
888
889 // Get the result from the cache.
890 __ bind(&load_result_from_cache);
891 __ ldr(result,
892 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
893 __ IncrementCounter(&Counters::number_to_string_native,
894 1,
895 scratch1,
896 scratch2);
897}
898
899
900void NumberToStringStub::Generate(MacroAssembler* masm) {
901 Label runtime;
902
903 __ ldr(r1, MemOperand(sp, 0));
904
905 // Generate code to lookup number in the number string cache.
906 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
907 __ add(sp, sp, Operand(1 * kPointerSize));
908 __ Ret();
909
910 __ bind(&runtime);
911 // Handle number to string in the runtime system if not found in the cache.
912 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
913}
914
915
916void RecordWriteStub::Generate(MacroAssembler* masm) {
917 __ add(offset_, object_, Operand(offset_));
918 __ RecordWriteHelper(object_, offset_, scratch_);
919 __ Ret();
920}
921
922
923// On entry lhs_ and rhs_ are the values to be compared.
924// On exit r0 is 0, positive or negative to indicate the result of
925// the comparison.
926void CompareStub::Generate(MacroAssembler* masm) {
927 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
928 (lhs_.is(r1) && rhs_.is(r0)));
929
930 Label slow; // Call builtin.
931 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
932
933 // NOTICE! This code is only reached after a smi-fast-case check, so
934 // it is certain that at least one operand isn't a smi.
935
936 // Handle the case where the objects are identical. Either returns the answer
937 // or goes to slow. Only falls through if the objects were not identical.
938 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
939
940 // If either is a Smi (we know that not both are), then they can only
941 // be strictly equal if the other is a HeapNumber.
942 STATIC_ASSERT(kSmiTag == 0);
943 ASSERT_EQ(0, Smi::FromInt(0));
944 __ and_(r2, lhs_, Operand(rhs_));
945 __ tst(r2, Operand(kSmiTagMask));
946 __ b(ne, &not_smis);
947 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
948 // 1) Return the answer.
949 // 2) Go to slow.
950 // 3) Fall through to both_loaded_as_doubles.
951 // 4) Jump to lhs_not_nan.
952 // In cases 3 and 4 we have found out we were dealing with a number-number
953 // comparison. If VFP3 is supported the double values of the numbers have
954 // been loaded into d7 and d6. Otherwise, the double values have been loaded
955 // into r0, r1, r2, and r3.
956 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
957
958 __ bind(&both_loaded_as_doubles);
959 // The arguments have been converted to doubles and stored in d6 and d7, if
960 // VFP3 is supported, or in r0, r1, r2, and r3.
961 if (CpuFeatures::IsSupported(VFP3)) {
962 __ bind(&lhs_not_nan);
963 CpuFeatures::Scope scope(VFP3);
964 Label no_nan;
965 // ARMv7 VFP3 instructions to implement double precision comparison.
966 __ vcmp(d7, d6);
967 __ vmrs(pc); // Move vector status bits to normal status bits.
968 Label nan;
969 __ b(vs, &nan);
970 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
971 __ mov(r0, Operand(LESS), LeaveCC, lt);
972 __ mov(r0, Operand(GREATER), LeaveCC, gt);
973 __ Ret();
974
975 __ bind(&nan);
976 // If one of the sides was a NaN then the v flag is set. Load r0 with
977 // whatever it takes to make the comparison fail, since comparisons with NaN
978 // always fail.
979 if (cc_ == lt || cc_ == le) {
980 __ mov(r0, Operand(GREATER));
981 } else {
982 __ mov(r0, Operand(LESS));
983 }
984 __ Ret();
985 } else {
986 // Checks for NaN in the doubles we have loaded. Can return the answer or
987 // fall through if neither is a NaN. Also binds lhs_not_nan.
988 EmitNanCheck(masm, &lhs_not_nan, cc_);
989 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
990 // answer. Never falls through.
991 EmitTwoNonNanDoubleComparison(masm, cc_);
992 }
993
994 __ bind(&not_smis);
995 // At this point we know we are dealing with two different objects,
996 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
997 if (strict_) {
998 // This returns non-equal for some object types, or falls through if it
999 // was not lucky.
1000 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1001 }
1002
1003 Label check_for_symbols;
1004 Label flat_string_check;
1005 // Check for heap-number-heap-number comparison. Can jump to slow case,
1006 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
1007 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1008 // In this case r2 will contain the type of rhs_. Never falls through.
1009 EmitCheckForTwoHeapNumbers(masm,
1010 lhs_,
1011 rhs_,
1012 &both_loaded_as_doubles,
1013 &check_for_symbols,
1014 &flat_string_check);
1015
1016 __ bind(&check_for_symbols);
1017 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
1018 // symbols.
1019 if (cc_ == eq && !strict_) {
1020 // Returns an answer for two symbols or two detectable objects.
1021 // Otherwise jumps to string case or not both strings case.
1022 // Assumes that r2 is the type of rhs_ on entry.
1023 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1024 }
1025
1026 // Check for both being sequential ASCII strings, and inline if that is the
1027 // case.
1028 __ bind(&flat_string_check);
1029
1030 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
1031
1032 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
1033 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1034 lhs_,
1035 rhs_,
1036 r2,
1037 r3,
1038 r4,
1039 r5);
1040 // Never falls through to here.
1041
1042 __ bind(&slow);
1043
1044 __ Push(lhs_, rhs_);
1045 // Figure out which native to call and setup the arguments.
1046 Builtins::JavaScript native;
1047 if (cc_ == eq) {
1048 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1049 } else {
1050 native = Builtins::COMPARE;
1051 int ncr; // NaN compare result
1052 if (cc_ == lt || cc_ == le) {
1053 ncr = GREATER;
1054 } else {
1055 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
1056 ncr = LESS;
1057 }
1058 __ mov(r0, Operand(Smi::FromInt(ncr)));
1059 __ push(r0);
1060 }
1061
1062 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1063 // tagged as a small integer.
1064 __ InvokeBuiltin(native, JUMP_JS);
1065}
1066
1067
1068// This stub does not handle the inlined cases (Smis, Booleans, undefined).
1069// The stub returns zero for false, and a non-zero value for true.
1070void ToBooleanStub::Generate(MacroAssembler* masm) {
1071 Label false_result;
1072 Label not_heap_number;
1073 Register scratch = r7;
1074
1075 // HeapNumber => false iff +0, -0, or NaN.
1076 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1077 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
1078 __ cmp(scratch, ip);
1079 __ b(&not_heap_number, ne);
1080
1081 __ sub(ip, tos_, Operand(kHeapObjectTag));
1082 __ vldr(d1, ip, HeapNumber::kValueOffset);
1083 __ vcmp(d1, 0.0);
1084 __ vmrs(pc);
1085 // "tos_" is a register, and contains a non zero value by default.
1086 // Hence we only need to overwrite "tos_" with zero to return false for
1087 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
1088 __ mov(tos_, Operand(0), LeaveCC, eq); // for FP_ZERO
1089 __ mov(tos_, Operand(0), LeaveCC, vs); // for FP_NAN
1090 __ Ret();
1091
1092 __ bind(&not_heap_number);
1093
1094 // Check if the value is 'null'.
1095 // 'null' => false.
1096 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1097 __ cmp(tos_, ip);
1098 __ b(&false_result, eq);
1099
1100 // It can be an undetectable object.
1101 // Undetectable => false.
1102 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
1103 __ ldrb(scratch, FieldMemOperand(ip, Map::kBitFieldOffset));
1104 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
1105 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
1106 __ b(&false_result, eq);
1107
1108 // JavaScript object => true.
1109 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1110 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1111 __ cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
1112 // "tos_" is a register and contains a non-zero value.
1113 // Hence we implicitly return true if the greater than
1114 // condition is satisfied.
1115 __ Ret(gt);
1116
1117 // Check for string
1118 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1119 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1120 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
1121 // "tos_" is a register and contains a non-zero value.
1122 // Hence we implicitly return true if the greater than
1123 // condition is satisfied.
1124 __ Ret(gt);
1125
1126 // String value => false iff empty, i.e., length is zero
1127 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
1128 // If length is zero, "tos_" contains zero ==> false.
1129 // If length is not zero, "tos_" contains a non-zero value ==> true.
1130 __ Ret();
1131
1132 // Return 0 in "tos_" for false .
1133 __ bind(&false_result);
1134 __ mov(tos_, Operand(0));
1135 __ Ret();
1136}
1137
1138
1139// We fall into this code if the operands were Smis, but the result was
1140// not (eg. overflow). We branch into this code (to the not_smi label) if
1141// the operands were not both Smi. The operands are in r0 and r1. In order
1142// to call the C-implemented binary fp operation routines we need to end up
1143// with the double precision floating point operands in r0 and r1 (for the
1144// value in r1) and r2 and r3 (for the value in r0).
1145void GenericBinaryOpStub::HandleBinaryOpSlowCases(
1146 MacroAssembler* masm,
1147 Label* not_smi,
1148 Register lhs,
1149 Register rhs,
1150 const Builtins::JavaScript& builtin) {
1151 Label slow, slow_reverse, do_the_call;
1152 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
1153
1154 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
1155 Register heap_number_map = r6;
1156
1157 if (ShouldGenerateSmiCode()) {
1158 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1159
1160 // Smi-smi case (overflow).
1161 // Since both are Smis there is no heap number to overwrite, so allocate.
1162 // The new heap number is in r5. r3 and r7 are scratch.
1163 __ AllocateHeapNumber(
1164 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
1165
1166 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
1167 // using registers d7 and d6 for the double values.
1168 if (CpuFeatures::IsSupported(VFP3)) {
1169 CpuFeatures::Scope scope(VFP3);
1170 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
1171 __ vmov(s15, r7);
1172 __ vcvt_f64_s32(d7, s15);
1173 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
1174 __ vmov(s13, r7);
1175 __ vcvt_f64_s32(d6, s13);
1176 if (!use_fp_registers) {
1177 __ vmov(r2, r3, d7);
1178 __ vmov(r0, r1, d6);
1179 }
1180 } else {
1181 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
1182 __ mov(r7, Operand(rhs));
1183 ConvertToDoubleStub stub1(r3, r2, r7, r9);
1184 __ push(lr);
1185 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
1186 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
1187 __ mov(r7, Operand(lhs));
1188 ConvertToDoubleStub stub2(r1, r0, r7, r9);
1189 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
1190 __ pop(lr);
1191 }
1192 __ jmp(&do_the_call); // Tail call. No return.
1193 }
1194
1195 // We branch here if at least one of r0 and r1 is not a Smi.
1196 __ bind(not_smi);
1197 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1198
1199 // After this point we have the left hand side in r1 and the right hand side
1200 // in r0.
1201 if (lhs.is(r0)) {
1202 __ Swap(r0, r1, ip);
1203 }
1204
1205 // The type transition also calculates the answer.
1206 bool generate_code_to_calculate_answer = true;
1207
1208 if (ShouldGenerateFPCode()) {
1209 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
1210 switch (op_) {
1211 case Token::ADD:
1212 case Token::SUB:
1213 case Token::MUL:
1214 case Token::DIV:
1215 GenerateTypeTransition(masm); // Tail call.
1216 generate_code_to_calculate_answer = false;
1217 break;
1218
1219 default:
1220 break;
1221 }
1222 }
1223
1224 if (generate_code_to_calculate_answer) {
1225 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
1226 if (mode_ == NO_OVERWRITE) {
1227 // In the case where there is no chance of an overwritable float we may
1228 // as well do the allocation immediately while r0 and r1 are untouched.
1229 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
1230 }
1231
1232 // Move r0 to a double in r2-r3.
1233 __ tst(r0, Operand(kSmiTagMask));
1234 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
1235 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
1236 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1237 __ cmp(r4, heap_number_map);
1238 __ b(ne, &slow);
1239 if (mode_ == OVERWRITE_RIGHT) {
1240 __ mov(r5, Operand(r0)); // Overwrite this heap number.
1241 }
1242 if (use_fp_registers) {
1243 CpuFeatures::Scope scope(VFP3);
1244 // Load the double from tagged HeapNumber r0 to d7.
1245 __ sub(r7, r0, Operand(kHeapObjectTag));
1246 __ vldr(d7, r7, HeapNumber::kValueOffset);
1247 } else {
1248 // Calling convention says that second double is in r2 and r3.
1249 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
1250 }
1251 __ jmp(&finished_loading_r0);
1252 __ bind(&r0_is_smi);
1253 if (mode_ == OVERWRITE_RIGHT) {
1254 // We can't overwrite a Smi so get address of new heap number into r5.
1255 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1256 }
1257
1258 if (CpuFeatures::IsSupported(VFP3)) {
1259 CpuFeatures::Scope scope(VFP3);
1260 // Convert smi in r0 to double in d7.
1261 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
1262 __ vmov(s15, r7);
1263 __ vcvt_f64_s32(d7, s15);
1264 if (!use_fp_registers) {
1265 __ vmov(r2, r3, d7);
1266 }
1267 } else {
1268 // Write Smi from r0 to r3 and r2 in double format.
1269 __ mov(r7, Operand(r0));
1270 ConvertToDoubleStub stub3(r3, r2, r7, r4);
1271 __ push(lr);
1272 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
1273 __ pop(lr);
1274 }
1275
1276 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
1277 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
1278 Label r1_is_not_smi;
1279 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
1280 __ tst(r1, Operand(kSmiTagMask));
1281 __ b(ne, &r1_is_not_smi);
1282 GenerateTypeTransition(masm); // Tail call.
1283 }
1284
1285 __ bind(&finished_loading_r0);
1286
1287 // Move r1 to a double in r0-r1.
1288 __ tst(r1, Operand(kSmiTagMask));
1289 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
1290 __ bind(&r1_is_not_smi);
1291 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
1292 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1293 __ cmp(r4, heap_number_map);
1294 __ b(ne, &slow);
1295 if (mode_ == OVERWRITE_LEFT) {
1296 __ mov(r5, Operand(r1)); // Overwrite this heap number.
1297 }
1298 if (use_fp_registers) {
1299 CpuFeatures::Scope scope(VFP3);
1300 // Load the double from tagged HeapNumber r1 to d6.
1301 __ sub(r7, r1, Operand(kHeapObjectTag));
1302 __ vldr(d6, r7, HeapNumber::kValueOffset);
1303 } else {
1304 // Calling convention says that first double is in r0 and r1.
1305 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
1306 }
1307 __ jmp(&finished_loading_r1);
1308 __ bind(&r1_is_smi);
1309 if (mode_ == OVERWRITE_LEFT) {
1310 // We can't overwrite a Smi so get address of new heap number into r5.
1311 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1312 }
1313
1314 if (CpuFeatures::IsSupported(VFP3)) {
1315 CpuFeatures::Scope scope(VFP3);
1316 // Convert smi in r1 to double in d6.
1317 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
1318 __ vmov(s13, r7);
1319 __ vcvt_f64_s32(d6, s13);
1320 if (!use_fp_registers) {
1321 __ vmov(r0, r1, d6);
1322 }
1323 } else {
1324 // Write Smi from r1 to r1 and r0 in double format.
1325 __ mov(r7, Operand(r1));
1326 ConvertToDoubleStub stub4(r1, r0, r7, r9);
1327 __ push(lr);
1328 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
1329 __ pop(lr);
1330 }
1331
1332 __ bind(&finished_loading_r1);
1333 }
1334
1335 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
1336 __ bind(&do_the_call);
1337 // If we are inlining the operation using VFP3 instructions for
1338 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
1339 if (use_fp_registers) {
1340 CpuFeatures::Scope scope(VFP3);
1341 // ARMv7 VFP3 instructions to implement
1342 // double precision, add, subtract, multiply, divide.
1343
1344 if (Token::MUL == op_) {
1345 __ vmul(d5, d6, d7);
1346 } else if (Token::DIV == op_) {
1347 __ vdiv(d5, d6, d7);
1348 } else if (Token::ADD == op_) {
1349 __ vadd(d5, d6, d7);
1350 } else if (Token::SUB == op_) {
1351 __ vsub(d5, d6, d7);
1352 } else {
1353 UNREACHABLE();
1354 }
1355 __ sub(r0, r5, Operand(kHeapObjectTag));
1356 __ vstr(d5, r0, HeapNumber::kValueOffset);
1357 __ add(r0, r0, Operand(kHeapObjectTag));
1358 __ mov(pc, lr);
1359 } else {
1360 // If we did not inline the operation, then the arguments are in:
1361 // r0: Left value (least significant part of mantissa).
1362 // r1: Left value (sign, exponent, top of mantissa).
1363 // r2: Right value (least significant part of mantissa).
1364 // r3: Right value (sign, exponent, top of mantissa).
1365 // r5: Address of heap number for result.
1366
1367 __ push(lr); // For later.
1368 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
1369 // Call C routine that may not cause GC or other trouble. r5 is callee
1370 // save.
1371 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
1372 // Store answer in the overwritable heap number.
1373 #if !defined(USE_ARM_EABI)
1374 // Double returned in fp coprocessor register 0 and 1, encoded as
1375 // register cr8. Offsets must be divisible by 4 for coprocessor so we
1376 // need to substract the tag from r5.
1377 __ sub(r4, r5, Operand(kHeapObjectTag));
1378 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
1379 #else
1380 // Double returned in registers 0 and 1.
1381 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
1382 #endif
1383 __ mov(r0, Operand(r5));
1384 // And we are done.
1385 __ pop(pc);
1386 }
1387 }
1388 }
1389
1390 if (!generate_code_to_calculate_answer &&
1391 !slow_reverse.is_linked() &&
1392 !slow.is_linked()) {
1393 return;
1394 }
1395
1396 if (lhs.is(r0)) {
1397 __ b(&slow);
1398 __ bind(&slow_reverse);
1399 __ Swap(r0, r1, ip);
1400 }
1401
1402 heap_number_map = no_reg; // Don't use this any more from here on.
1403
1404 // We jump to here if something goes wrong (one param is not a number of any
1405 // sort or new-space allocation fails).
1406 __ bind(&slow);
1407
1408 // Push arguments to the stack
1409 __ Push(r1, r0);
1410
1411 if (Token::ADD == op_) {
1412 // Test for string arguments before calling runtime.
1413 // r1 : first argument
1414 // r0 : second argument
1415 // sp[0] : second argument
1416 // sp[4] : first argument
1417
1418 Label not_strings, not_string1, string1, string1_smi2;
1419 __ tst(r1, Operand(kSmiTagMask));
1420 __ b(eq, &not_string1);
1421 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
1422 __ b(ge, &not_string1);
1423
1424 // First argument is a a string, test second.
1425 __ tst(r0, Operand(kSmiTagMask));
1426 __ b(eq, &string1_smi2);
1427 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
1428 __ b(ge, &string1);
1429
1430 // First and second argument are strings.
1431 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1432 __ TailCallStub(&string_add_stub);
1433
1434 __ bind(&string1_smi2);
1435 // First argument is a string, second is a smi. Try to lookup the number
1436 // string for the smi in the number string cache.
1437 NumberToStringStub::GenerateLookupNumberStringCache(
1438 masm, r0, r2, r4, r5, r6, true, &string1);
1439
1440 // Replace second argument on stack and tailcall string add stub to make
1441 // the result.
1442 __ str(r2, MemOperand(sp, 0));
1443 __ TailCallStub(&string_add_stub);
1444
1445 // Only first argument is a string.
1446 __ bind(&string1);
1447 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
1448
1449 // First argument was not a string, test second.
1450 __ bind(&not_string1);
1451 __ tst(r0, Operand(kSmiTagMask));
1452 __ b(eq, &not_strings);
1453 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
1454 __ b(ge, &not_strings);
1455
1456 // Only second argument is a string.
1457 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
1458
1459 __ bind(&not_strings);
1460 }
1461
1462 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
1463}
1464
1465
1466// Tries to get a signed int32 out of a double precision floating point heap
1467// number. Rounds towards 0. Fastest for doubles that are in the ranges
1468// -0x7fffffff to -0x40000000 or 0x40000000 to 0x7fffffff. This corresponds
1469// almost to the range of signed int32 values that are not Smis. Jumps to the
1470// label 'slow' if the double isn't in the range -0x80000000.0 to 0x80000000.0
1471// (excluding the endpoints).
1472static void GetInt32(MacroAssembler* masm,
1473 Register source,
1474 Register dest,
1475 Register scratch,
1476 Register scratch2,
1477 Label* slow) {
1478 Label right_exponent, done;
1479 // Get exponent word.
1480 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kExponentOffset));
1481 // Get exponent alone in scratch2.
1482 __ Ubfx(scratch2,
1483 scratch,
1484 HeapNumber::kExponentShift,
1485 HeapNumber::kExponentBits);
1486 // Load dest with zero. We use this either for the final shift or
1487 // for the answer.
1488 __ mov(dest, Operand(0));
1489 // Check whether the exponent matches a 32 bit signed int that is not a Smi.
1490 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased). This is
1491 // the exponent that we are fastest at and also the highest exponent we can
1492 // handle here.
1493 const uint32_t non_smi_exponent = HeapNumber::kExponentBias + 30;
1494 // The non_smi_exponent, 0x41d, is too big for ARM's immediate field so we
1495 // split it up to avoid a constant pool entry. You can't do that in general
1496 // for cmp because of the overflow flag, but we know the exponent is in the
1497 // range 0-2047 so there is no overflow.
1498 int fudge_factor = 0x400;
1499 __ sub(scratch2, scratch2, Operand(fudge_factor));
1500 __ cmp(scratch2, Operand(non_smi_exponent - fudge_factor));
1501 // If we have a match of the int32-but-not-Smi exponent then skip some logic.
1502 __ b(eq, &right_exponent);
1503 // If the exponent is higher than that then go to slow case. This catches
1504 // numbers that don't fit in a signed int32, infinities and NaNs.
1505 __ b(gt, slow);
1506
1507 // We know the exponent is smaller than 30 (biased). If it is less than
1508 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, ie
1509 // it rounds to zero.
1510 const uint32_t zero_exponent = HeapNumber::kExponentBias + 0;
1511 __ sub(scratch2, scratch2, Operand(zero_exponent - fudge_factor), SetCC);
1512 // Dest already has a Smi zero.
1513 __ b(lt, &done);
1514 if (!CpuFeatures::IsSupported(VFP3)) {
1515 // We have an exponent between 0 and 30 in scratch2. Subtract from 30 to
1516 // get how much to shift down.
1517 __ rsb(dest, scratch2, Operand(30));
1518 }
1519 __ bind(&right_exponent);
1520 if (CpuFeatures::IsSupported(VFP3)) {
1521 CpuFeatures::Scope scope(VFP3);
1522 // ARMv7 VFP3 instructions implementing double precision to integer
1523 // conversion using round to zero.
1524 __ ldr(scratch2, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1525 __ vmov(d7, scratch2, scratch);
1526 __ vcvt_s32_f64(s15, d7);
1527 __ vmov(dest, s15);
1528 } else {
1529 // Get the top bits of the mantissa.
1530 __ and_(scratch2, scratch, Operand(HeapNumber::kMantissaMask));
1531 // Put back the implicit 1.
1532 __ orr(scratch2, scratch2, Operand(1 << HeapNumber::kExponentShift));
1533 // Shift up the mantissa bits to take up the space the exponent used to
1534 // take. We just orred in the implicit bit so that took care of one and
1535 // we want to leave the sign bit 0 so we subtract 2 bits from the shift
1536 // distance.
1537 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1538 __ mov(scratch2, Operand(scratch2, LSL, shift_distance));
1539 // Put sign in zero flag.
1540 __ tst(scratch, Operand(HeapNumber::kSignMask));
1541 // Get the second half of the double. For some exponents we don't
1542 // actually need this because the bits get shifted out again, but
1543 // it's probably slower to test than just to do it.
1544 __ ldr(scratch, FieldMemOperand(source, HeapNumber::kMantissaOffset));
1545 // Shift down 22 bits to get the last 10 bits.
1546 __ orr(scratch, scratch2, Operand(scratch, LSR, 32 - shift_distance));
1547 // Move down according to the exponent.
1548 __ mov(dest, Operand(scratch, LSR, dest));
1549 // Fix sign if sign bit was set.
1550 __ rsb(dest, dest, Operand(0), LeaveCC, ne);
1551 }
1552 __ bind(&done);
1553}
1554
1555// For bitwise ops where the inputs are not both Smis we here try to determine
1556// whether both inputs are either Smis or at least heap numbers that can be
1557// represented by a 32 bit signed value. We truncate towards zero as required
1558// by the ES spec. If this is the case we do the bitwise op and see if the
1559// result is a Smi. If so, great, otherwise we try to find a heap number to
1560// write the answer into (either by allocating or by overwriting).
1561// On entry the operands are in lhs and rhs. On exit the answer is in r0.
1562void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
1563 Register lhs,
1564 Register rhs) {
1565 Label slow, result_not_a_smi;
1566 Label rhs_is_smi, lhs_is_smi;
1567 Label done_checking_rhs, done_checking_lhs;
1568
1569 Register heap_number_map = r6;
1570 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1571
1572 __ tst(lhs, Operand(kSmiTagMask));
1573 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
1574 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
1575 __ cmp(r4, heap_number_map);
1576 __ b(ne, &slow);
1577 GetInt32(masm, lhs, r3, r5, r4, &slow);
1578 __ jmp(&done_checking_lhs);
1579 __ bind(&lhs_is_smi);
1580 __ mov(r3, Operand(lhs, ASR, 1));
1581 __ bind(&done_checking_lhs);
1582
1583 __ tst(rhs, Operand(kSmiTagMask));
1584 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
1585 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
1586 __ cmp(r4, heap_number_map);
1587 __ b(ne, &slow);
1588 GetInt32(masm, rhs, r2, r5, r4, &slow);
1589 __ jmp(&done_checking_rhs);
1590 __ bind(&rhs_is_smi);
1591 __ mov(r2, Operand(rhs, ASR, 1));
1592 __ bind(&done_checking_rhs);
1593
1594 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
1595
1596 // r0 and r1: Original operands (Smi or heap numbers).
1597 // r2 and r3: Signed int32 operands.
1598 switch (op_) {
1599 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
1600 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
1601 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
1602 case Token::SAR:
1603 // Use only the 5 least significant bits of the shift count.
1604 __ and_(r2, r2, Operand(0x1f));
1605 __ mov(r2, Operand(r3, ASR, r2));
1606 break;
1607 case Token::SHR:
1608 // Use only the 5 least significant bits of the shift count.
1609 __ and_(r2, r2, Operand(0x1f));
1610 __ mov(r2, Operand(r3, LSR, r2), SetCC);
1611 // SHR is special because it is required to produce a positive answer.
1612 // The code below for writing into heap numbers isn't capable of writing
1613 // the register as an unsigned int so we go to slow case if we hit this
1614 // case.
1615 if (CpuFeatures::IsSupported(VFP3)) {
1616 __ b(mi, &result_not_a_smi);
1617 } else {
1618 __ b(mi, &slow);
1619 }
1620 break;
1621 case Token::SHL:
1622 // Use only the 5 least significant bits of the shift count.
1623 __ and_(r2, r2, Operand(0x1f));
1624 __ mov(r2, Operand(r3, LSL, r2));
1625 break;
1626 default: UNREACHABLE();
1627 }
1628 // check that the *signed* result fits in a smi
1629 __ add(r3, r2, Operand(0x40000000), SetCC);
1630 __ b(mi, &result_not_a_smi);
1631 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
1632 __ Ret();
1633
1634 Label have_to_allocate, got_a_heap_number;
1635 __ bind(&result_not_a_smi);
1636 switch (mode_) {
1637 case OVERWRITE_RIGHT: {
1638 __ tst(rhs, Operand(kSmiTagMask));
1639 __ b(eq, &have_to_allocate);
1640 __ mov(r5, Operand(rhs));
1641 break;
1642 }
1643 case OVERWRITE_LEFT: {
1644 __ tst(lhs, Operand(kSmiTagMask));
1645 __ b(eq, &have_to_allocate);
1646 __ mov(r5, Operand(lhs));
1647 break;
1648 }
1649 case NO_OVERWRITE: {
1650 // Get a new heap number in r5. r4 and r7 are scratch.
1651 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1652 }
1653 default: break;
1654 }
1655 __ bind(&got_a_heap_number);
1656 // r2: Answer as signed int32.
1657 // r5: Heap number to write answer into.
1658
1659 // Nothing can go wrong now, so move the heap number to r0, which is the
1660 // result.
1661 __ mov(r0, Operand(r5));
1662
1663 if (CpuFeatures::IsSupported(VFP3)) {
1664 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
1665 CpuFeatures::Scope scope(VFP3);
1666 __ vmov(s0, r2);
1667 if (op_ == Token::SHR) {
1668 __ vcvt_f64_u32(d0, s0);
1669 } else {
1670 __ vcvt_f64_s32(d0, s0);
1671 }
1672 __ sub(r3, r0, Operand(kHeapObjectTag));
1673 __ vstr(d0, r3, HeapNumber::kValueOffset);
1674 __ Ret();
1675 } else {
1676 // Tail call that writes the int32 in r2 to the heap number in r0, using
1677 // r3 as scratch. r0 is preserved and returned.
1678 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
1679 __ TailCallStub(&stub);
1680 }
1681
1682 if (mode_ != NO_OVERWRITE) {
1683 __ bind(&have_to_allocate);
1684 // Get a new heap number in r5. r4 and r7 are scratch.
1685 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1686 __ jmp(&got_a_heap_number);
1687 }
1688
1689 // If all else failed then we go to the runtime system.
1690 __ bind(&slow);
1691 __ Push(lhs, rhs); // Restore stack.
1692 switch (op_) {
1693 case Token::BIT_OR:
1694 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
1695 break;
1696 case Token::BIT_AND:
1697 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
1698 break;
1699 case Token::BIT_XOR:
1700 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
1701 break;
1702 case Token::SAR:
1703 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
1704 break;
1705 case Token::SHR:
1706 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
1707 break;
1708 case Token::SHL:
1709 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
1710 break;
1711 default:
1712 UNREACHABLE();
1713 }
1714}
1715
1716
1717
1718
1719// This function takes the known int in a register for the cases
1720// where it doesn't know a good trick, and may deliver
1721// a result that needs shifting.
1722static void MultiplyByKnownIntInStub(
1723 MacroAssembler* masm,
1724 Register result,
1725 Register source,
1726 Register known_int_register, // Smi tagged.
1727 int known_int,
1728 int* required_shift) { // Including Smi tag shift
1729 switch (known_int) {
1730 case 3:
1731 __ add(result, source, Operand(source, LSL, 1));
1732 *required_shift = 1;
1733 break;
1734 case 5:
1735 __ add(result, source, Operand(source, LSL, 2));
1736 *required_shift = 1;
1737 break;
1738 case 6:
1739 __ add(result, source, Operand(source, LSL, 1));
1740 *required_shift = 2;
1741 break;
1742 case 7:
1743 __ rsb(result, source, Operand(source, LSL, 3));
1744 *required_shift = 1;
1745 break;
1746 case 9:
1747 __ add(result, source, Operand(source, LSL, 3));
1748 *required_shift = 1;
1749 break;
1750 case 10:
1751 __ add(result, source, Operand(source, LSL, 2));
1752 *required_shift = 2;
1753 break;
1754 default:
1755 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
1756 __ mul(result, source, known_int_register);
1757 *required_shift = 0;
1758 }
1759}
1760
1761
1762// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
1763// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
1764// Takes the sum of the digits base (mask + 1) repeatedly until we have a
1765// number from 0 to mask. On exit the 'eq' condition flags are set if the
1766// answer is exactly the mask.
1767void IntegerModStub::DigitSum(MacroAssembler* masm,
1768 Register lhs,
1769 int mask,
1770 int shift,
1771 Label* entry) {
1772 ASSERT(mask > 0);
1773 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
1774 Label loop;
1775 __ bind(&loop);
1776 __ and_(ip, lhs, Operand(mask));
1777 __ add(lhs, ip, Operand(lhs, LSR, shift));
1778 __ bind(entry);
1779 __ cmp(lhs, Operand(mask));
1780 __ b(gt, &loop);
1781}
1782
1783
1784void IntegerModStub::DigitSum(MacroAssembler* masm,
1785 Register lhs,
1786 Register scratch,
1787 int mask,
1788 int shift1,
1789 int shift2,
1790 Label* entry) {
1791 ASSERT(mask > 0);
1792 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
1793 Label loop;
1794 __ bind(&loop);
1795 __ bic(scratch, lhs, Operand(mask));
1796 __ and_(ip, lhs, Operand(mask));
1797 __ add(lhs, ip, Operand(lhs, LSR, shift1));
1798 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
1799 __ bind(entry);
1800 __ cmp(lhs, Operand(mask));
1801 __ b(gt, &loop);
1802}
1803
1804
1805// Splits the number into two halves (bottom half has shift bits). The top
1806// half is subtracted from the bottom half. If the result is negative then
1807// rhs is added.
1808void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
1809 Register lhs,
1810 int shift,
1811 int rhs) {
1812 int mask = (1 << shift) - 1;
1813 __ and_(ip, lhs, Operand(mask));
1814 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
1815 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
1816}
1817
1818
1819void IntegerModStub::ModReduce(MacroAssembler* masm,
1820 Register lhs,
1821 int max,
1822 int denominator) {
1823 int limit = denominator;
1824 while (limit * 2 <= max) limit *= 2;
1825 while (limit >= denominator) {
1826 __ cmp(lhs, Operand(limit));
1827 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
1828 limit >>= 1;
1829 }
1830}
1831
1832
1833void IntegerModStub::ModAnswer(MacroAssembler* masm,
1834 Register result,
1835 Register shift_distance,
1836 Register mask_bits,
1837 Register sum_of_digits) {
1838 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
1839 __ Ret();
1840}
1841
1842
1843// See comment for class.
1844void IntegerModStub::Generate(MacroAssembler* masm) {
1845 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
1846 __ bic(odd_number_, odd_number_, Operand(1));
1847 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
1848 // We now have (odd_number_ - 1) * 2 in the register.
1849 // Build a switch out of branches instead of data because it avoids
1850 // having to teach the assembler about intra-code-object pointers
1851 // that are not in relative branch instructions.
1852 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
1853 Label mod21, mod23, mod25;
1854 { Assembler::BlockConstPoolScope block_const_pool(masm);
1855 __ add(pc, pc, Operand(odd_number_));
1856 // When you read pc it is always 8 ahead, but when you write it you always
1857 // write the actual value. So we put in two nops to take up the slack.
1858 __ nop();
1859 __ nop();
1860 __ b(&mod3);
1861 __ b(&mod5);
1862 __ b(&mod7);
1863 __ b(&mod9);
1864 __ b(&mod11);
1865 __ b(&mod13);
1866 __ b(&mod15);
1867 __ b(&mod17);
1868 __ b(&mod19);
1869 __ b(&mod21);
1870 __ b(&mod23);
1871 __ b(&mod25);
1872 }
1873
1874 // For each denominator we find a multiple that is almost only ones
1875 // when expressed in binary. Then we do the sum-of-digits trick for
1876 // that number. If the multiple is not 1 then we have to do a little
1877 // more work afterwards to get the answer into the 0-denominator-1
1878 // range.
1879 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
1880 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
1881 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1882
1883 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
1884 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
1885 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1886
1887 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
1888 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
1889 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1890
1891 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
1892 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
1893 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1894
1895 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
1896 ModReduce(masm, lhs_, 0x3f, 11);
1897 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1898
1899 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
1900 ModReduce(masm, lhs_, 0xff, 13);
1901 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1902
1903 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
1904 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
1905 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1906
1907 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
1908 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
1909 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1910
1911 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
1912 ModReduce(masm, lhs_, 0xff, 19);
1913 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1914
1915 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
1916 ModReduce(masm, lhs_, 0x3f, 21);
1917 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1918
1919 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
1920 ModReduce(masm, lhs_, 0xff, 23);
1921 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1922
1923 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
1924 ModReduce(masm, lhs_, 0x7f, 25);
1925 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1926}
1927
1928
1929void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
1930 // lhs_ : x
1931 // rhs_ : y
1932 // r0 : result
1933
1934 Register result = r0;
1935 Register lhs = lhs_;
1936 Register rhs = rhs_;
1937
1938 // This code can't cope with other register allocations yet.
1939 ASSERT(result.is(r0) &&
1940 ((lhs.is(r0) && rhs.is(r1)) ||
1941 (lhs.is(r1) && rhs.is(r0))));
1942
1943 Register smi_test_reg = r7;
1944 Register scratch = r9;
1945
1946 // All ops need to know whether we are dealing with two Smis. Set up
1947 // smi_test_reg to tell us that.
1948 if (ShouldGenerateSmiCode()) {
1949 __ orr(smi_test_reg, lhs, Operand(rhs));
1950 }
1951
1952 switch (op_) {
1953 case Token::ADD: {
1954 Label not_smi;
1955 // Fast path.
1956 if (ShouldGenerateSmiCode()) {
1957 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
1958 __ tst(smi_test_reg, Operand(kSmiTagMask));
1959 __ b(ne, &not_smi);
1960 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
1961 // Return if no overflow.
1962 __ Ret(vc);
1963 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
1964 }
1965 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
1966 break;
1967 }
1968
1969 case Token::SUB: {
1970 Label not_smi;
1971 // Fast path.
1972 if (ShouldGenerateSmiCode()) {
1973 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
1974 __ tst(smi_test_reg, Operand(kSmiTagMask));
1975 __ b(ne, &not_smi);
1976 if (lhs.is(r1)) {
1977 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
1978 // Return if no overflow.
1979 __ Ret(vc);
1980 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
1981 } else {
1982 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
1983 // Return if no overflow.
1984 __ Ret(vc);
1985 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
1986 }
1987 }
1988 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
1989 break;
1990 }
1991
1992 case Token::MUL: {
1993 Label not_smi, slow;
1994 if (ShouldGenerateSmiCode()) {
1995 STATIC_ASSERT(kSmiTag == 0); // adjust code below
1996 __ tst(smi_test_reg, Operand(kSmiTagMask));
1997 Register scratch2 = smi_test_reg;
1998 smi_test_reg = no_reg;
1999 __ b(ne, &not_smi);
2000 // Remove tag from one operand (but keep sign), so that result is Smi.
2001 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
2002 // Do multiplication
2003 // scratch = lower 32 bits of ip * lhs.
2004 __ smull(scratch, scratch2, lhs, ip);
2005 // Go slow on overflows (overflow bit is not set).
2006 __ mov(ip, Operand(scratch, ASR, 31));
2007 // No overflow if higher 33 bits are identical.
2008 __ cmp(ip, Operand(scratch2));
2009 __ b(ne, &slow);
2010 // Go slow on zero result to handle -0.
2011 __ tst(scratch, Operand(scratch));
2012 __ mov(result, Operand(scratch), LeaveCC, ne);
2013 __ Ret(ne);
2014 // We need -0 if we were multiplying a negative number with 0 to get 0.
2015 // We know one of them was zero.
2016 __ add(scratch2, rhs, Operand(lhs), SetCC);
2017 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
2018 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
2019 // Slow case. We fall through here if we multiplied a negative number
2020 // with 0, because that would mean we should produce -0.
2021 __ bind(&slow);
2022 }
2023 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
2024 break;
2025 }
2026
2027 case Token::DIV:
2028 case Token::MOD: {
2029 Label not_smi;
2030 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
2031 Label lhs_is_unsuitable;
2032 __ BranchOnNotSmi(lhs, &not_smi);
2033 if (IsPowerOf2(constant_rhs_)) {
2034 if (op_ == Token::MOD) {
2035 __ and_(rhs,
2036 lhs,
2037 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
2038 SetCC);
2039 // We now have the answer, but if the input was negative we also
2040 // have the sign bit. Our work is done if the result is
2041 // positive or zero:
2042 if (!rhs.is(r0)) {
2043 __ mov(r0, rhs, LeaveCC, pl);
2044 }
2045 __ Ret(pl);
2046 // A mod of a negative left hand side must return a negative number.
2047 // Unfortunately if the answer is 0 then we must return -0. And we
2048 // already optimistically trashed rhs so we may need to restore it.
2049 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
2050 // Next two instructions are conditional on the answer being -0.
2051 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
2052 __ b(eq, &lhs_is_unsuitable);
2053 // We need to subtract the dividend. Eg. -3 % 4 == -3.
2054 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
2055 } else {
2056 ASSERT(op_ == Token::DIV);
2057 __ tst(lhs,
2058 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
2059 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
2060 int shift = 0;
2061 int d = constant_rhs_;
2062 while ((d & 1) == 0) {
2063 d >>= 1;
2064 shift++;
2065 }
2066 __ mov(r0, Operand(lhs, LSR, shift));
2067 __ bic(r0, r0, Operand(kSmiTagMask));
2068 }
2069 } else {
2070 // Not a power of 2.
2071 __ tst(lhs, Operand(0x80000000u));
2072 __ b(ne, &lhs_is_unsuitable);
2073 // Find a fixed point reciprocal of the divisor so we can divide by
2074 // multiplying.
2075 double divisor = 1.0 / constant_rhs_;
2076 int shift = 32;
2077 double scale = 4294967296.0; // 1 << 32.
2078 uint32_t mul;
2079 // Maximise the precision of the fixed point reciprocal.
2080 while (true) {
2081 mul = static_cast<uint32_t>(scale * divisor);
2082 if (mul >= 0x7fffffff) break;
2083 scale *= 2.0;
2084 shift++;
2085 }
2086 mul++;
2087 Register scratch2 = smi_test_reg;
2088 smi_test_reg = no_reg;
2089 __ mov(scratch2, Operand(mul));
2090 __ umull(scratch, scratch2, scratch2, lhs);
2091 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
2092 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
2093 // rhs is still the known rhs. rhs is Smi tagged.
2094 // lhs is still the unkown lhs. lhs is Smi tagged.
2095 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
2096 // scratch = scratch2 * rhs.
2097 MultiplyByKnownIntInStub(masm,
2098 scratch,
2099 scratch2,
2100 rhs,
2101 constant_rhs_,
2102 &required_scratch_shift);
2103 // scratch << required_scratch_shift is now the Smi tagged rhs *
2104 // (lhs / rhs) where / indicates integer division.
2105 if (op_ == Token::DIV) {
2106 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
2107 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
2108 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
2109 } else {
2110 ASSERT(op_ == Token::MOD);
2111 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
2112 }
2113 }
2114 __ Ret();
2115 __ bind(&lhs_is_unsuitable);
2116 } else if (op_ == Token::MOD &&
2117 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
2118 runtime_operands_type_ != BinaryOpIC::STRINGS) {
2119 // Do generate a bit of smi code for modulus even though the default for
2120 // modulus is not to do it, but as the ARM processor has no coprocessor
2121 // support for modulus checking for smis makes sense. We can handle
2122 // 1 to 25 times any power of 2. This covers over half the numbers from
2123 // 1 to 100 including all of the first 25. (Actually the constants < 10
2124 // are handled above by reciprocal multiplication. We only get here for
2125 // those cases if the right hand side is not a constant or for cases
2126 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
2127 // stub.)
2128 Label slow;
2129 Label not_power_of_2;
2130 ASSERT(!ShouldGenerateSmiCode());
2131 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
2132 // Check for two positive smis.
2133 __ orr(smi_test_reg, lhs, Operand(rhs));
2134 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
2135 __ b(ne, &slow);
2136 // Check that rhs is a power of two and not zero.
2137 Register mask_bits = r3;
2138 __ sub(scratch, rhs, Operand(1), SetCC);
2139 __ b(mi, &slow);
2140 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
2141 __ b(ne, &not_power_of_2);
2142 // Calculate power of two modulus.
2143 __ and_(result, lhs, Operand(scratch));
2144 __ Ret();
2145
2146 __ bind(&not_power_of_2);
2147 __ eor(scratch, scratch, Operand(mask_bits));
2148 // At least two bits are set in the modulus. The high one(s) are in
2149 // mask_bits and the low one is scratch + 1.
2150 __ and_(mask_bits, scratch, Operand(lhs));
2151 Register shift_distance = scratch;
2152 scratch = no_reg;
2153
2154 // The rhs consists of a power of 2 multiplied by some odd number.
2155 // The power-of-2 part we handle by putting the corresponding bits
2156 // from the lhs in the mask_bits register, and the power in the
2157 // shift_distance register. Shift distance is never 0 due to Smi
2158 // tagging.
2159 __ CountLeadingZeros(r4, shift_distance, shift_distance);
2160 __ rsb(shift_distance, r4, Operand(32));
2161
2162 // Now we need to find out what the odd number is. The last bit is
2163 // always 1.
2164 Register odd_number = r4;
2165 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
2166 __ cmp(odd_number, Operand(25));
2167 __ b(gt, &slow);
2168
2169 IntegerModStub stub(
2170 result, shift_distance, odd_number, mask_bits, lhs, r5);
2171 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
2172
2173 __ bind(&slow);
2174 }
2175 HandleBinaryOpSlowCases(
2176 masm,
2177 &not_smi,
2178 lhs,
2179 rhs,
2180 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
2181 break;
2182 }
2183
2184 case Token::BIT_OR:
2185 case Token::BIT_AND:
2186 case Token::BIT_XOR:
2187 case Token::SAR:
2188 case Token::SHR:
2189 case Token::SHL: {
2190 Label slow;
2191 STATIC_ASSERT(kSmiTag == 0); // adjust code below
2192 __ tst(smi_test_reg, Operand(kSmiTagMask));
2193 __ b(ne, &slow);
2194 Register scratch2 = smi_test_reg;
2195 smi_test_reg = no_reg;
2196 switch (op_) {
2197 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
2198 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
2199 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
2200 case Token::SAR:
2201 // Remove tags from right operand.
2202 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2203 __ mov(result, Operand(lhs, ASR, scratch2));
2204 // Smi tag result.
2205 __ bic(result, result, Operand(kSmiTagMask));
2206 break;
2207 case Token::SHR:
2208 // Remove tags from operands. We can't do this on a 31 bit number
2209 // because then the 0s get shifted into bit 30 instead of bit 31.
2210 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
2211 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2212 __ mov(scratch, Operand(scratch, LSR, scratch2));
2213 // Unsigned shift is not allowed to produce a negative number, so
2214 // check the sign bit and the sign bit after Smi tagging.
2215 __ tst(scratch, Operand(0xc0000000));
2216 __ b(ne, &slow);
2217 // Smi tag result.
2218 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
2219 break;
2220 case Token::SHL:
2221 // Remove tags from operands.
2222 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
2223 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2224 __ mov(scratch, Operand(scratch, LSL, scratch2));
2225 // Check that the signed result fits in a Smi.
2226 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
2227 __ b(mi, &slow);
2228 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
2229 break;
2230 default: UNREACHABLE();
2231 }
2232 __ Ret();
2233 __ bind(&slow);
2234 HandleNonSmiBitwiseOp(masm, lhs, rhs);
2235 break;
2236 }
2237
2238 default: UNREACHABLE();
2239 }
2240 // This code should be unreachable.
2241 __ stop("Unreachable");
2242
2243 // Generate an unreachable reference to the DEFAULT stub so that it can be
2244 // found at the end of this stub when clearing ICs at GC.
2245 // TODO(kaznacheev): Check performance impact and get rid of this.
2246 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
2247 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
2248 __ CallStub(&uninit);
2249 }
2250}
2251
2252
2253void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
2254 Label get_result;
2255
2256 __ Push(r1, r0);
2257
2258 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
2259 __ mov(r1, Operand(Smi::FromInt(op_)));
2260 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
2261 __ Push(r2, r1, r0);
2262
2263 __ TailCallExternalReference(
2264 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
2265 5,
2266 1);
2267}
2268
2269
2270Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
2271 GenericBinaryOpStub stub(key, type_info);
2272 return stub.GetCode();
2273}
2274
2275
2276void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
2277 // Argument is a number and is on stack and in r0.
2278 Label runtime_call;
2279 Label input_not_smi;
2280 Label loaded;
2281
2282 if (CpuFeatures::IsSupported(VFP3)) {
2283 // Load argument and check if it is a smi.
2284 __ BranchOnNotSmi(r0, &input_not_smi);
2285
2286 CpuFeatures::Scope scope(VFP3);
2287 // Input is a smi. Convert to double and load the low and high words
2288 // of the double into r2, r3.
2289 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
2290 __ b(&loaded);
2291
2292 __ bind(&input_not_smi);
2293 // Check if input is a HeapNumber.
2294 __ CheckMap(r0,
2295 r1,
2296 Heap::kHeapNumberMapRootIndex,
2297 &runtime_call,
2298 true);
2299 // Input is a HeapNumber. Load it to a double register and store the
2300 // low and high words into r2, r3.
2301 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
2302
2303 __ bind(&loaded);
2304 // r2 = low 32 bits of double value
2305 // r3 = high 32 bits of double value
2306 // Compute hash (the shifts are arithmetic):
2307 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2308 __ eor(r1, r2, Operand(r3));
2309 __ eor(r1, r1, Operand(r1, ASR, 16));
2310 __ eor(r1, r1, Operand(r1, ASR, 8));
2311 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
2312 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
2313
2314 // r2 = low 32 bits of double value.
2315 // r3 = high 32 bits of double value.
2316 // r1 = TranscendentalCache::hash(double value).
2317 __ mov(r0,
2318 Operand(ExternalReference::transcendental_cache_array_address()));
2319 // r0 points to cache array.
2320 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
2321 // r0 points to the cache for the type type_.
2322 // If NULL, the cache hasn't been initialized yet, so go through runtime.
2323 __ cmp(r0, Operand(0));
2324 __ b(eq, &runtime_call);
2325
2326#ifdef DEBUG
2327 // Check that the layout of cache elements match expectations.
2328 { TranscendentalCache::Element test_elem[2];
2329 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2330 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2331 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2332 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2333 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2334 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2335 CHECK_EQ(0, elem_in0 - elem_start);
2336 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2337 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2338 }
2339#endif
2340
2341 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
2342 __ add(r1, r1, Operand(r1, LSL, 1));
2343 __ add(r0, r0, Operand(r1, LSL, 2));
2344 // Check if cache matches: Double value is stored in uint32_t[2] array.
2345 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
2346 __ cmp(r2, r4);
2347 __ b(ne, &runtime_call);
2348 __ cmp(r3, r5);
2349 __ b(ne, &runtime_call);
2350 // Cache hit. Load result, pop argument and return.
2351 __ mov(r0, Operand(r6));
2352 __ pop();
2353 __ Ret();
2354 }
2355
2356 __ bind(&runtime_call);
2357 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
2358}
2359
2360
2361Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2362 switch (type_) {
2363 // Add more cases when necessary.
2364 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2365 case TranscendentalCache::COS: return Runtime::kMath_cos;
2366 default:
2367 UNIMPLEMENTED();
2368 return Runtime::kAbort;
2369 }
2370}
2371
2372
2373void StackCheckStub::Generate(MacroAssembler* masm) {
2374 // Do tail-call to runtime routine. Runtime routines expect at least one
2375 // argument, so give it a Smi.
2376 __ mov(r0, Operand(Smi::FromInt(0)));
2377 __ push(r0);
2378 __ TailCallRuntime(Runtime::kStackGuard, 1, 1);
2379
2380 __ StubReturn(1);
2381}
2382
2383
2384void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
2385 Label slow, done;
2386
2387 Register heap_number_map = r6;
2388 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2389
2390 if (op_ == Token::SUB) {
2391 // Check whether the value is a smi.
2392 Label try_float;
2393 __ tst(r0, Operand(kSmiTagMask));
2394 __ b(ne, &try_float);
2395
2396 // Go slow case if the value of the expression is zero
2397 // to make sure that we switch between 0 and -0.
2398 if (negative_zero_ == kStrictNegativeZero) {
2399 // If we have to check for zero, then we can check for the max negative
2400 // smi while we are at it.
2401 __ bic(ip, r0, Operand(0x80000000), SetCC);
2402 __ b(eq, &slow);
2403 __ rsb(r0, r0, Operand(0));
2404 __ StubReturn(1);
2405 } else {
2406 // The value of the expression is a smi and 0 is OK for -0. Try
2407 // optimistic subtraction '0 - value'.
2408 __ rsb(r0, r0, Operand(0), SetCC);
2409 __ StubReturn(1, vc);
2410 // We don't have to reverse the optimistic neg since the only case
2411 // where we fall through is the minimum negative Smi, which is the case
2412 // where the neg leaves the register unchanged.
2413 __ jmp(&slow); // Go slow on max negative Smi.
2414 }
2415
2416 __ bind(&try_float);
2417 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2418 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2419 __ cmp(r1, heap_number_map);
2420 __ b(ne, &slow);
2421 // r0 is a heap number. Get a new heap number in r1.
2422 if (overwrite_ == UNARY_OVERWRITE) {
2423 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2424 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
2425 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2426 } else {
2427 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
2428 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
2429 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2430 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
2431 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
2432 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
2433 __ mov(r0, Operand(r1));
2434 }
2435 } else if (op_ == Token::BIT_NOT) {
2436 // Check if the operand is a heap number.
2437 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2438 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2439 __ cmp(r1, heap_number_map);
2440 __ b(ne, &slow);
2441
2442 // Convert the heap number is r0 to an untagged integer in r1.
2443 GetInt32(masm, r0, r1, r2, r3, &slow);
2444
2445 // Do the bitwise operation (move negated) and check if the result
2446 // fits in a smi.
2447 Label try_float;
2448 __ mvn(r1, Operand(r1));
2449 __ add(r2, r1, Operand(0x40000000), SetCC);
2450 __ b(mi, &try_float);
2451 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
2452 __ b(&done);
2453
2454 __ bind(&try_float);
2455 if (!overwrite_ == UNARY_OVERWRITE) {
2456 // Allocate a fresh heap number, but don't overwrite r0 until
2457 // we're sure we can do it without going through the slow case
2458 // that needs the value in r0.
2459 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
2460 __ mov(r0, Operand(r2));
2461 }
2462
2463 if (CpuFeatures::IsSupported(VFP3)) {
2464 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
2465 CpuFeatures::Scope scope(VFP3);
2466 __ vmov(s0, r1);
2467 __ vcvt_f64_s32(d0, s0);
2468 __ sub(r2, r0, Operand(kHeapObjectTag));
2469 __ vstr(d0, r2, HeapNumber::kValueOffset);
2470 } else {
2471 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
2472 // have to set up a frame.
2473 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
2474 __ push(lr);
2475 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
2476 __ pop(lr);
2477 }
2478 } else {
2479 UNIMPLEMENTED();
2480 }
2481
2482 __ bind(&done);
2483 __ StubReturn(1);
2484
2485 // Handle the slow case by jumping to the JavaScript builtin.
2486 __ bind(&slow);
2487 __ push(r0);
2488 switch (op_) {
2489 case Token::SUB:
2490 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
2491 break;
2492 case Token::BIT_NOT:
2493 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
2494 break;
2495 default:
2496 UNREACHABLE();
2497 }
2498}
2499
2500
2501void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
2502 // r0 holds the exception.
2503
2504 // Adjust this code if not the case.
2505 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
2506
2507 // Drop the sp to the top of the handler.
2508 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2509 __ ldr(sp, MemOperand(r3));
2510
2511 // Restore the next handler and frame pointer, discard handler state.
2512 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
2513 __ pop(r2);
2514 __ str(r2, MemOperand(r3));
2515 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
2516 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
2517
2518 // Before returning we restore the context from the frame pointer if
2519 // not NULL. The frame pointer is NULL in the exception handler of a
2520 // JS entry frame.
2521 __ cmp(fp, Operand(0));
2522 // Set cp to NULL if fp is NULL.
2523 __ mov(cp, Operand(0), LeaveCC, eq);
2524 // Restore cp otherwise.
2525 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
2526#ifdef DEBUG
2527 if (FLAG_debug_code) {
2528 __ mov(lr, Operand(pc));
2529 }
2530#endif
2531 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
2532 __ pop(pc);
2533}
2534
2535
2536void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
2537 UncatchableExceptionType type) {
2538 // Adjust this code if not the case.
2539 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
2540
2541 // Drop sp to the top stack handler.
2542 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2543 __ ldr(sp, MemOperand(r3));
2544
2545 // Unwind the handlers until the ENTRY handler is found.
2546 Label loop, done;
2547 __ bind(&loop);
2548 // Load the type of the current stack handler.
2549 const int kStateOffset = StackHandlerConstants::kStateOffset;
2550 __ ldr(r2, MemOperand(sp, kStateOffset));
2551 __ cmp(r2, Operand(StackHandler::ENTRY));
2552 __ b(eq, &done);
2553 // Fetch the next handler in the list.
2554 const int kNextOffset = StackHandlerConstants::kNextOffset;
2555 __ ldr(sp, MemOperand(sp, kNextOffset));
2556 __ jmp(&loop);
2557 __ bind(&done);
2558
2559 // Set the top handler address to next handler past the current ENTRY handler.
2560 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
2561 __ pop(r2);
2562 __ str(r2, MemOperand(r3));
2563
2564 if (type == OUT_OF_MEMORY) {
2565 // Set external caught exception to false.
2566 ExternalReference external_caught(Top::k_external_caught_exception_address);
2567 __ mov(r0, Operand(false));
2568 __ mov(r2, Operand(external_caught));
2569 __ str(r0, MemOperand(r2));
2570
2571 // Set pending exception and r0 to out of memory exception.
2572 Failure* out_of_memory = Failure::OutOfMemoryException();
2573 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
2574 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
2575 __ str(r0, MemOperand(r2));
2576 }
2577
2578 // Stack layout at this point. See also StackHandlerConstants.
2579 // sp -> state (ENTRY)
2580 // fp
2581 // lr
2582
2583 // Discard handler state (r2 is not used) and restore frame pointer.
2584 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
2585 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
2586 // Before returning we restore the context from the frame pointer if
2587 // not NULL. The frame pointer is NULL in the exception handler of a
2588 // JS entry frame.
2589 __ cmp(fp, Operand(0));
2590 // Set cp to NULL if fp is NULL.
2591 __ mov(cp, Operand(0), LeaveCC, eq);
2592 // Restore cp otherwise.
2593 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
2594#ifdef DEBUG
2595 if (FLAG_debug_code) {
2596 __ mov(lr, Operand(pc));
2597 }
2598#endif
2599 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
2600 __ pop(pc);
2601}
2602
2603
2604void CEntryStub::GenerateCore(MacroAssembler* masm,
2605 Label* throw_normal_exception,
2606 Label* throw_termination_exception,
2607 Label* throw_out_of_memory_exception,
2608 bool do_gc,
2609 bool always_allocate,
2610 int frame_alignment_skew) {
2611 // r0: result parameter for PerformGC, if any
2612 // r4: number of arguments including receiver (C callee-saved)
2613 // r5: pointer to builtin function (C callee-saved)
2614 // r6: pointer to the first argument (C callee-saved)
2615
2616 if (do_gc) {
2617 // Passing r0.
2618 __ PrepareCallCFunction(1, r1);
2619 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
2620 }
2621
2622 ExternalReference scope_depth =
2623 ExternalReference::heap_always_allocate_scope_depth();
2624 if (always_allocate) {
2625 __ mov(r0, Operand(scope_depth));
2626 __ ldr(r1, MemOperand(r0));
2627 __ add(r1, r1, Operand(1));
2628 __ str(r1, MemOperand(r0));
2629 }
2630
2631 // Call C built-in.
2632 // r0 = argc, r1 = argv
2633 __ mov(r0, Operand(r4));
2634 __ mov(r1, Operand(r6));
2635
2636 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
2637 int frame_alignment_mask = frame_alignment - 1;
2638#if defined(V8_HOST_ARCH_ARM)
2639 if (FLAG_debug_code) {
2640 if (frame_alignment > kPointerSize) {
2641 Label alignment_as_expected;
2642 ASSERT(IsPowerOf2(frame_alignment));
2643 __ sub(r2, sp, Operand(frame_alignment_skew));
2644 __ tst(r2, Operand(frame_alignment_mask));
2645 __ b(eq, &alignment_as_expected);
2646 // Don't use Check here, as it will call Runtime_Abort re-entering here.
2647 __ stop("Unexpected alignment");
2648 __ bind(&alignment_as_expected);
2649 }
2650 }
2651#endif
2652
2653 // Just before the call (jump) below lr is pushed, so the actual alignment is
2654 // adding one to the current skew.
2655 int alignment_before_call =
2656 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
2657 if (alignment_before_call > 0) {
2658 // Push until the alignment before the call is met.
2659 __ mov(r2, Operand(0));
2660 for (int i = alignment_before_call;
2661 (i & frame_alignment_mask) != 0;
2662 i += kPointerSize) {
2663 __ push(r2);
2664 }
2665 }
2666
2667 // TODO(1242173): To let the GC traverse the return address of the exit
2668 // frames, we need to know where the return address is. Right now,
2669 // we push it on the stack to be able to find it again, but we never
2670 // restore from it in case of changes, which makes it impossible to
2671 // support moving the C entry code stub. This should be fixed, but currently
2672 // this is OK because the CEntryStub gets generated so early in the V8 boot
2673 // sequence that it is not moving ever.
2674 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
2675 masm->push(lr);
2676 masm->Jump(r5);
2677
2678 // Restore sp back to before aligning the stack.
2679 if (alignment_before_call > 0) {
2680 __ add(sp, sp, Operand(alignment_before_call));
2681 }
2682
2683 if (always_allocate) {
2684 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
2685 // though (contain the result).
2686 __ mov(r2, Operand(scope_depth));
2687 __ ldr(r3, MemOperand(r2));
2688 __ sub(r3, r3, Operand(1));
2689 __ str(r3, MemOperand(r2));
2690 }
2691
2692 // check for failure result
2693 Label failure_returned;
2694 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
2695 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
2696 __ add(r2, r0, Operand(1));
2697 __ tst(r2, Operand(kFailureTagMask));
2698 __ b(eq, &failure_returned);
2699
2700 // Exit C frame and return.
2701 // r0:r1: result
2702 // sp: stack pointer
2703 // fp: frame pointer
2704 __ LeaveExitFrame();
2705
2706 // check if we should retry or throw exception
2707 Label retry;
2708 __ bind(&failure_returned);
2709 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
2710 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
2711 __ b(eq, &retry);
2712
2713 // Special handling of out of memory exceptions.
2714 Failure* out_of_memory = Failure::OutOfMemoryException();
2715 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
2716 __ b(eq, throw_out_of_memory_exception);
2717
2718 // Retrieve the pending exception and clear the variable.
2719 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
2720 __ ldr(r3, MemOperand(ip));
2721 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
2722 __ ldr(r0, MemOperand(ip));
2723 __ str(r3, MemOperand(ip));
2724
2725 // Special handling of termination exceptions which are uncatchable
2726 // by javascript code.
2727 __ cmp(r0, Operand(Factory::termination_exception()));
2728 __ b(eq, throw_termination_exception);
2729
2730 // Handle normal exception.
2731 __ jmp(throw_normal_exception);
2732
2733 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
2734}
2735
2736
2737void CEntryStub::Generate(MacroAssembler* masm) {
2738 // Called from JavaScript; parameters are on stack as if calling JS function
2739 // r0: number of arguments including receiver
2740 // r1: pointer to builtin function
2741 // fp: frame pointer (restored after C call)
2742 // sp: stack pointer (restored as callee's sp after C call)
2743 // cp: current context (C callee-saved)
2744
2745 // Result returned in r0 or r0+r1 by default.
2746
2747 // NOTE: Invocations of builtins may return failure objects
2748 // instead of a proper result. The builtin entry handles
2749 // this by performing a garbage collection and retrying the
2750 // builtin once.
2751
2752 // Enter the exit frame that transitions from JavaScript to C++.
2753 __ EnterExitFrame();
2754
2755 // r4: number of arguments (C callee-saved)
2756 // r5: pointer to builtin function (C callee-saved)
2757 // r6: pointer to first argument (C callee-saved)
2758
2759 Label throw_normal_exception;
2760 Label throw_termination_exception;
2761 Label throw_out_of_memory_exception;
2762
2763 // Call into the runtime system.
2764 GenerateCore(masm,
2765 &throw_normal_exception,
2766 &throw_termination_exception,
2767 &throw_out_of_memory_exception,
2768 false,
2769 false,
2770 -kPointerSize);
2771
2772 // Do space-specific GC and retry runtime call.
2773 GenerateCore(masm,
2774 &throw_normal_exception,
2775 &throw_termination_exception,
2776 &throw_out_of_memory_exception,
2777 true,
2778 false,
2779 0);
2780
2781 // Do full GC and retry runtime call one final time.
2782 Failure* failure = Failure::InternalError();
2783 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
2784 GenerateCore(masm,
2785 &throw_normal_exception,
2786 &throw_termination_exception,
2787 &throw_out_of_memory_exception,
2788 true,
2789 true,
2790 kPointerSize);
2791
2792 __ bind(&throw_out_of_memory_exception);
2793 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
2794
2795 __ bind(&throw_termination_exception);
2796 GenerateThrowUncatchable(masm, TERMINATION);
2797
2798 __ bind(&throw_normal_exception);
2799 GenerateThrowTOS(masm);
2800}
2801
2802
2803void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
2804 // r0: code entry
2805 // r1: function
2806 // r2: receiver
2807 // r3: argc
2808 // [sp+0]: argv
2809
2810 Label invoke, exit;
2811
2812 // Called from C, so do not pop argc and args on exit (preserve sp)
2813 // No need to save register-passed args
2814 // Save callee-saved registers (incl. cp and fp), sp, and lr
2815 __ stm(db_w, sp, kCalleeSaved | lr.bit());
2816
2817 // Get address of argv, see stm above.
2818 // r0: code entry
2819 // r1: function
2820 // r2: receiver
2821 // r3: argc
2822 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
2823
2824 // Push a frame with special values setup to mark it as an entry frame.
2825 // r0: code entry
2826 // r1: function
2827 // r2: receiver
2828 // r3: argc
2829 // r4: argv
2830 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
2831 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
2832 __ mov(r7, Operand(Smi::FromInt(marker)));
2833 __ mov(r6, Operand(Smi::FromInt(marker)));
2834 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
2835 __ ldr(r5, MemOperand(r5));
2836 __ Push(r8, r7, r6, r5);
2837
2838 // Setup frame pointer for the frame to be pushed.
2839 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
2840
2841 // Call a faked try-block that does the invoke.
2842 __ bl(&invoke);
2843
2844 // Caught exception: Store result (exception) in the pending
2845 // exception field in the JSEnv and return a failure sentinel.
2846 // Coming in here the fp will be invalid because the PushTryHandler below
2847 // sets it to 0 to signal the existence of the JSEntry frame.
2848 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
2849 __ str(r0, MemOperand(ip));
2850 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
2851 __ b(&exit);
2852
2853 // Invoke: Link this frame into the handler chain.
2854 __ bind(&invoke);
2855 // Must preserve r0-r4, r5-r7 are available.
2856 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
2857 // If an exception not caught by another handler occurs, this handler
2858 // returns control to the code after the bl(&invoke) above, which
2859 // restores all kCalleeSaved registers (including cp and fp) to their
2860 // saved values before returning a failure to C.
2861
2862 // Clear any pending exceptions.
2863 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
2864 __ ldr(r5, MemOperand(ip));
2865 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
2866 __ str(r5, MemOperand(ip));
2867
2868 // Invoke the function by calling through JS entry trampoline builtin.
2869 // Notice that we cannot store a reference to the trampoline code directly in
2870 // this stub, because runtime stubs are not traversed when doing GC.
2871
2872 // Expected registers by Builtins::JSEntryTrampoline
2873 // r0: code entry
2874 // r1: function
2875 // r2: receiver
2876 // r3: argc
2877 // r4: argv
2878 if (is_construct) {
2879 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
2880 __ mov(ip, Operand(construct_entry));
2881 } else {
2882 ExternalReference entry(Builtins::JSEntryTrampoline);
2883 __ mov(ip, Operand(entry));
2884 }
2885 __ ldr(ip, MemOperand(ip)); // deref address
2886
2887 // Branch and link to JSEntryTrampoline. We don't use the double underscore
2888 // macro for the add instruction because we don't want the coverage tool
2889 // inserting instructions here after we read the pc.
2890 __ mov(lr, Operand(pc));
2891 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
2892
2893 // Unlink this frame from the handler chain. When reading the
2894 // address of the next handler, there is no need to use the address
2895 // displacement since the current stack pointer (sp) points directly
2896 // to the stack handler.
2897 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
2898 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
2899 __ str(r3, MemOperand(ip));
2900 // No need to restore registers
2901 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
2902
2903
2904 __ bind(&exit); // r0 holds result
2905 // Restore the top frame descriptors from the stack.
2906 __ pop(r3);
2907 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
2908 __ str(r3, MemOperand(ip));
2909
2910 // Reset the stack to the callee saved registers.
2911 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
2912
2913 // Restore callee-saved registers and return.
2914#ifdef DEBUG
2915 if (FLAG_debug_code) {
2916 __ mov(lr, Operand(pc));
2917 }
2918#endif
2919 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
2920}
2921
2922
2923// This stub performs an instanceof, calling the builtin function if
2924// necessary. Uses r1 for the object, r0 for the function that it may
2925// be an instance of (these are fetched from the stack).
2926void InstanceofStub::Generate(MacroAssembler* masm) {
2927 // Get the object - slow case for smis (we may need to throw an exception
2928 // depending on the rhs).
2929 Label slow, loop, is_instance, is_not_instance;
2930 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
2931 __ BranchOnSmi(r0, &slow);
2932
2933 // Check that the left hand is a JS object and put map in r3.
2934 __ CompareObjectType(r0, r3, r2, FIRST_JS_OBJECT_TYPE);
2935 __ b(lt, &slow);
2936 __ cmp(r2, Operand(LAST_JS_OBJECT_TYPE));
2937 __ b(gt, &slow);
2938
2939 // Get the prototype of the function (r4 is result, r2 is scratch).
2940 __ ldr(r1, MemOperand(sp, 0));
2941 // r1 is function, r3 is map.
2942
2943 // Look up the function and the map in the instanceof cache.
2944 Label miss;
2945 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
2946 __ cmp(r1, ip);
2947 __ b(ne, &miss);
2948 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
2949 __ cmp(r3, ip);
2950 __ b(ne, &miss);
2951 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
2952 __ pop();
2953 __ pop();
2954 __ mov(pc, Operand(lr));
2955
2956 __ bind(&miss);
2957 __ TryGetFunctionPrototype(r1, r4, r2, &slow);
2958
2959 // Check that the function prototype is a JS object.
2960 __ BranchOnSmi(r4, &slow);
2961 __ CompareObjectType(r4, r5, r5, FIRST_JS_OBJECT_TYPE);
2962 __ b(lt, &slow);
2963 __ cmp(r5, Operand(LAST_JS_OBJECT_TYPE));
2964 __ b(gt, &slow);
2965
2966 __ StoreRoot(r1, Heap::kInstanceofCacheFunctionRootIndex);
2967 __ StoreRoot(r3, Heap::kInstanceofCacheMapRootIndex);
2968
2969 // Register mapping: r3 is object map and r4 is function prototype.
2970 // Get prototype of object into r2.
2971 __ ldr(r2, FieldMemOperand(r3, Map::kPrototypeOffset));
2972
2973 // Loop through the prototype chain looking for the function prototype.
2974 __ bind(&loop);
2975 __ cmp(r2, Operand(r4));
2976 __ b(eq, &is_instance);
2977 __ LoadRoot(ip, Heap::kNullValueRootIndex);
2978 __ cmp(r2, ip);
2979 __ b(eq, &is_not_instance);
2980 __ ldr(r2, FieldMemOperand(r2, HeapObject::kMapOffset));
2981 __ ldr(r2, FieldMemOperand(r2, Map::kPrototypeOffset));
2982 __ jmp(&loop);
2983
2984 __ bind(&is_instance);
2985 __ mov(r0, Operand(Smi::FromInt(0)));
2986 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
2987 __ pop();
2988 __ pop();
2989 __ mov(pc, Operand(lr)); // Return.
2990
2991 __ bind(&is_not_instance);
2992 __ mov(r0, Operand(Smi::FromInt(1)));
2993 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
2994 __ pop();
2995 __ pop();
2996 __ mov(pc, Operand(lr)); // Return.
2997
2998 // Slow-case. Tail call builtin.
2999 __ bind(&slow);
3000 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
3001}
3002
3003
3004void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
3005 // The displacement is the offset of the last parameter (if any)
3006 // relative to the frame pointer.
3007 static const int kDisplacement =
3008 StandardFrameConstants::kCallerSPOffset - kPointerSize;
3009
3010 // Check that the key is a smi.
3011 Label slow;
3012 __ BranchOnNotSmi(r1, &slow);
3013
3014 // Check if the calling frame is an arguments adaptor frame.
3015 Label adaptor;
3016 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3017 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3018 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3019 __ b(eq, &adaptor);
3020
3021 // Check index against formal parameters count limit passed in
3022 // through register r0. Use unsigned comparison to get negative
3023 // check for free.
3024 __ cmp(r1, r0);
3025 __ b(cs, &slow);
3026
3027 // Read the argument from the stack and return it.
3028 __ sub(r3, r0, r1);
3029 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3030 __ ldr(r0, MemOperand(r3, kDisplacement));
3031 __ Jump(lr);
3032
3033 // Arguments adaptor case: Check index against actual arguments
3034 // limit found in the arguments adaptor frame. Use unsigned
3035 // comparison to get negative check for free.
3036 __ bind(&adaptor);
3037 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3038 __ cmp(r1, r0);
3039 __ b(cs, &slow);
3040
3041 // Read the argument from the adaptor frame and return it.
3042 __ sub(r3, r0, r1);
3043 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3044 __ ldr(r0, MemOperand(r3, kDisplacement));
3045 __ Jump(lr);
3046
3047 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3048 // by calling the runtime system.
3049 __ bind(&slow);
3050 __ push(r1);
3051 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3052}
3053
3054
3055void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
3056 // sp[0] : number of parameters
3057 // sp[4] : receiver displacement
3058 // sp[8] : function
3059
3060 // Check if the calling frame is an arguments adaptor frame.
3061 Label adaptor_frame, try_allocate, runtime;
3062 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3063 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3064 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3065 __ b(eq, &adaptor_frame);
3066
3067 // Get the length from the frame.
3068 __ ldr(r1, MemOperand(sp, 0));
3069 __ b(&try_allocate);
3070
3071 // Patch the arguments.length and the parameters pointer.
3072 __ bind(&adaptor_frame);
3073 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3074 __ str(r1, MemOperand(sp, 0));
3075 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3076 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
3077 __ str(r3, MemOperand(sp, 1 * kPointerSize));
3078
3079 // Try the new space allocation. Start out with computing the size
3080 // of the arguments object and the elements array in words.
3081 Label add_arguments_object;
3082 __ bind(&try_allocate);
3083 __ cmp(r1, Operand(0));
3084 __ b(eq, &add_arguments_object);
3085 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
3086 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
3087 __ bind(&add_arguments_object);
3088 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
3089
3090 // Do the allocation of both objects in one go.
3091 __ AllocateInNewSpace(
3092 r1,
3093 r0,
3094 r2,
3095 r3,
3096 &runtime,
3097 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
3098
3099 // Get the arguments boilerplate from the current (global) context.
3100 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
3101 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
3102 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
3103 __ ldr(r4, MemOperand(r4, offset));
3104
3105 // Copy the JS object part.
3106 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
3107
3108 // Setup the callee in-object property.
3109 STATIC_ASSERT(Heap::arguments_callee_index == 0);
3110 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
3111 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
3112
3113 // Get the length (smi tagged) and set that as an in-object property too.
3114 STATIC_ASSERT(Heap::arguments_length_index == 1);
3115 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
3116 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
3117
3118 // If there are no actual arguments, we're done.
3119 Label done;
3120 __ cmp(r1, Operand(0));
3121 __ b(eq, &done);
3122
3123 // Get the parameters pointer from the stack.
3124 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
3125
3126 // Setup the elements pointer in the allocated arguments object and
3127 // initialize the header in the elements fixed array.
3128 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
3129 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
3130 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
3131 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
3132 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
3133 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
3134
3135 // Copy the fixed array slots.
3136 Label loop;
3137 // Setup r4 to point to the first array slot.
3138 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3139 __ bind(&loop);
3140 // Pre-decrement r2 with kPointerSize on each iteration.
3141 // Pre-decrement in order to skip receiver.
3142 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
3143 // Post-increment r4 with kPointerSize on each iteration.
3144 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
3145 __ sub(r1, r1, Operand(1));
3146 __ cmp(r1, Operand(0));
3147 __ b(ne, &loop);
3148
3149 // Return and remove the on-stack parameters.
3150 __ bind(&done);
3151 __ add(sp, sp, Operand(3 * kPointerSize));
3152 __ Ret();
3153
3154 // Do the runtime call to allocate the arguments object.
3155 __ bind(&runtime);
3156 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3157}
3158
3159
3160void RegExpExecStub::Generate(MacroAssembler* masm) {
3161 // Just jump directly to runtime if native RegExp is not selected at compile
3162 // time or if regexp entry in generated code is turned off runtime switch or
3163 // at compilation.
3164#ifdef V8_INTERPRETED_REGEXP
3165 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3166#else // V8_INTERPRETED_REGEXP
3167 if (!FLAG_regexp_entry_native) {
3168 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3169 return;
3170 }
3171
3172 // Stack frame on entry.
3173 // sp[0]: last_match_info (expected JSArray)
3174 // sp[4]: previous index
3175 // sp[8]: subject string
3176 // sp[12]: JSRegExp object
3177
3178 static const int kLastMatchInfoOffset = 0 * kPointerSize;
3179 static const int kPreviousIndexOffset = 1 * kPointerSize;
3180 static const int kSubjectOffset = 2 * kPointerSize;
3181 static const int kJSRegExpOffset = 3 * kPointerSize;
3182
3183 Label runtime, invoke_regexp;
3184
3185 // Allocation of registers for this function. These are in callee save
3186 // registers and will be preserved by the call to the native RegExp code, as
3187 // this code is called using the normal C calling convention. When calling
3188 // directly from generated code the native RegExp code will not do a GC and
3189 // therefore the content of these registers are safe to use after the call.
3190 Register subject = r4;
3191 Register regexp_data = r5;
3192 Register last_match_info_elements = r6;
3193
3194 // Ensure that a RegExp stack is allocated.
3195 ExternalReference address_of_regexp_stack_memory_address =
3196 ExternalReference::address_of_regexp_stack_memory_address();
3197 ExternalReference address_of_regexp_stack_memory_size =
3198 ExternalReference::address_of_regexp_stack_memory_size();
3199 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
3200 __ ldr(r0, MemOperand(r0, 0));
3201 __ tst(r0, Operand(r0));
3202 __ b(eq, &runtime);
3203
3204 // Check that the first argument is a JSRegExp object.
3205 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
3206 STATIC_ASSERT(kSmiTag == 0);
3207 __ tst(r0, Operand(kSmiTagMask));
3208 __ b(eq, &runtime);
3209 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3210 __ b(ne, &runtime);
3211
3212 // Check that the RegExp has been compiled (data contains a fixed array).
3213 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
3214 if (FLAG_debug_code) {
3215 __ tst(regexp_data, Operand(kSmiTagMask));
3216 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
3217 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
3218 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
3219 }
3220
3221 // regexp_data: RegExp data (FixedArray)
3222 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3223 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
3224 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
3225 __ b(ne, &runtime);
3226
3227 // regexp_data: RegExp data (FixedArray)
3228 // Check that the number of captures fit in the static offsets vector buffer.
3229 __ ldr(r2,
3230 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
3231 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3232 // uses the asumption that smis are 2 * their untagged value.
3233 STATIC_ASSERT(kSmiTag == 0);
3234 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3235 __ add(r2, r2, Operand(2)); // r2 was a smi.
3236 // Check that the static offsets vector buffer is large enough.
3237 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
3238 __ b(hi, &runtime);
3239
3240 // r2: Number of capture registers
3241 // regexp_data: RegExp data (FixedArray)
3242 // Check that the second argument is a string.
3243 __ ldr(subject, MemOperand(sp, kSubjectOffset));
3244 __ tst(subject, Operand(kSmiTagMask));
3245 __ b(eq, &runtime);
3246 Condition is_string = masm->IsObjectStringType(subject, r0);
3247 __ b(NegateCondition(is_string), &runtime);
3248 // Get the length of the string to r3.
3249 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
3250
3251 // r2: Number of capture registers
3252 // r3: Length of subject string as a smi
3253 // subject: Subject string
3254 // regexp_data: RegExp data (FixedArray)
3255 // Check that the third argument is a positive smi less than the subject
3256 // string length. A negative value will be greater (unsigned comparison).
3257 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
3258 __ tst(r0, Operand(kSmiTagMask));
3259 __ b(ne, &runtime);
3260 __ cmp(r3, Operand(r0));
3261 __ b(ls, &runtime);
3262
3263 // r2: Number of capture registers
3264 // subject: Subject string
3265 // regexp_data: RegExp data (FixedArray)
3266 // Check that the fourth object is a JSArray object.
3267 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
3268 __ tst(r0, Operand(kSmiTagMask));
3269 __ b(eq, &runtime);
3270 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
3271 __ b(ne, &runtime);
3272 // Check that the JSArray is in fast case.
3273 __ ldr(last_match_info_elements,
3274 FieldMemOperand(r0, JSArray::kElementsOffset));
3275 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
3276 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
3277 __ cmp(r0, ip);
3278 __ b(ne, &runtime);
3279 // Check that the last match info has space for the capture registers and the
3280 // additional information.
3281 __ ldr(r0,
3282 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
3283 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
3284 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
3285 __ b(gt, &runtime);
3286
3287 // subject: Subject string
3288 // regexp_data: RegExp data (FixedArray)
3289 // Check the representation and encoding of the subject string.
3290 Label seq_string;
3291 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
3292 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3293 // First check for flat string.
3294 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
3295 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
3296 __ b(eq, &seq_string);
3297
3298 // subject: Subject string
3299 // regexp_data: RegExp data (FixedArray)
3300 // Check for flat cons string.
3301 // A flat cons string is a cons string where the second part is the empty
3302 // string. In that case the subject string is just the first part of the cons
3303 // string. Also in this case the first part of the cons string is known to be
3304 // a sequential string or an external string.
3305 STATIC_ASSERT(kExternalStringTag !=0);
3306 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3307 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
3308 __ b(ne, &runtime);
3309 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
3310 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
3311 __ cmp(r0, r1);
3312 __ b(ne, &runtime);
3313 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
3314 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
3315 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3316 // Is first part a flat string?
3317 STATIC_ASSERT(kSeqStringTag == 0);
3318 __ tst(r0, Operand(kStringRepresentationMask));
3319 __ b(nz, &runtime);
3320
3321 __ bind(&seq_string);
3322 // subject: Subject string
3323 // regexp_data: RegExp data (FixedArray)
3324 // r0: Instance type of subject string
3325 STATIC_ASSERT(4 == kAsciiStringTag);
3326 STATIC_ASSERT(kTwoByteStringTag == 0);
3327 // Find the code object based on the assumptions above.
3328 __ and_(r0, r0, Operand(kStringEncodingMask));
3329 __ mov(r3, Operand(r0, ASR, 2), SetCC);
3330 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
3331 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
3332
3333 // Check that the irregexp code has been generated for the actual string
3334 // encoding. If it has, the field contains a code object otherwise it contains
3335 // the hole.
3336 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
3337 __ b(ne, &runtime);
3338
3339 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
3340 // r7: code
3341 // subject: Subject string
3342 // regexp_data: RegExp data (FixedArray)
3343 // Load used arguments before starting to push arguments for call to native
3344 // RegExp code to avoid handling changing stack height.
3345 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
3346 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3347
3348 // r1: previous index
3349 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
3350 // r7: code
3351 // subject: Subject string
3352 // regexp_data: RegExp data (FixedArray)
3353 // All checks done. Now push arguments for native regexp code.
3354 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
3355
3356 static const int kRegExpExecuteArguments = 7;
3357 __ push(lr);
3358 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
3359
3360 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
3361 __ mov(r0, Operand(1));
3362 __ str(r0, MemOperand(sp, 2 * kPointerSize));
3363
3364 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
3365 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
3366 __ ldr(r0, MemOperand(r0, 0));
3367 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
3368 __ ldr(r2, MemOperand(r2, 0));
3369 __ add(r0, r0, Operand(r2));
3370 __ str(r0, MemOperand(sp, 1 * kPointerSize));
3371
3372 // Argument 5 (sp[0]): static offsets vector buffer.
3373 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
3374 __ str(r0, MemOperand(sp, 0 * kPointerSize));
3375
3376 // For arguments 4 and 3 get string length, calculate start of string data and
3377 // calculate the shift of the index (0 for ASCII and 1 for two byte).
3378 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
3379 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3380 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
3381 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3382 __ eor(r3, r3, Operand(1));
3383 // Argument 4 (r3): End of string data
3384 // Argument 3 (r2): Start of string data
3385 __ add(r2, r9, Operand(r1, LSL, r3));
3386 __ add(r3, r9, Operand(r0, LSL, r3));
3387
3388 // Argument 2 (r1): Previous index.
3389 // Already there
3390
3391 // Argument 1 (r0): Subject string.
3392 __ mov(r0, subject);
3393
3394 // Locate the code entry and call it.
3395 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
3396 __ CallCFunction(r7, kRegExpExecuteArguments);
3397 __ pop(lr);
3398
3399 // r0: result
3400 // subject: subject string (callee saved)
3401 // regexp_data: RegExp data (callee saved)
3402 // last_match_info_elements: Last match info elements (callee saved)
3403
3404 // Check the result.
3405 Label success;
3406 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
3407 __ b(eq, &success);
3408 Label failure;
3409 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
3410 __ b(eq, &failure);
3411 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
3412 // If not exception it can only be retry. Handle that in the runtime system.
3413 __ b(ne, &runtime);
3414 // Result must now be exception. If there is no pending exception already a
3415 // stack overflow (on the backtrack stack) was detected in RegExp code but
3416 // haven't created the exception yet. Handle that in the runtime system.
3417 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
3418 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
3419 __ ldr(r0, MemOperand(r0, 0));
3420 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
3421 __ ldr(r1, MemOperand(r1, 0));
3422 __ cmp(r0, r1);
3423 __ b(eq, &runtime);
3424 __ bind(&failure);
3425 // For failure and exception return null.
3426 __ mov(r0, Operand(Factory::null_value()));
3427 __ add(sp, sp, Operand(4 * kPointerSize));
3428 __ Ret();
3429
3430 // Process the result from the native regexp code.
3431 __ bind(&success);
3432 __ ldr(r1,
3433 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
3434 // Calculate number of capture registers (number_of_captures + 1) * 2.
3435 STATIC_ASSERT(kSmiTag == 0);
3436 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3437 __ add(r1, r1, Operand(2)); // r1 was a smi.
3438
3439 // r1: number of capture registers
3440 // r4: subject string
3441 // Store the capture count.
3442 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
3443 __ str(r2, FieldMemOperand(last_match_info_elements,
3444 RegExpImpl::kLastCaptureCountOffset));
3445 // Store last subject and last input.
3446 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
3447 __ str(subject,
3448 FieldMemOperand(last_match_info_elements,
3449 RegExpImpl::kLastSubjectOffset));
3450 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
3451 __ str(subject,
3452 FieldMemOperand(last_match_info_elements,
3453 RegExpImpl::kLastInputOffset));
3454 __ mov(r3, last_match_info_elements);
3455 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
3456
3457 // Get the static offsets vector filled by the native regexp code.
3458 ExternalReference address_of_static_offsets_vector =
3459 ExternalReference::address_of_static_offsets_vector();
3460 __ mov(r2, Operand(address_of_static_offsets_vector));
3461
3462 // r1: number of capture registers
3463 // r2: offsets vector
3464 Label next_capture, done;
3465 // Capture register counter starts from number of capture registers and
3466 // counts down until wraping after zero.
3467 __ add(r0,
3468 last_match_info_elements,
3469 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
3470 __ bind(&next_capture);
3471 __ sub(r1, r1, Operand(1), SetCC);
3472 __ b(mi, &done);
3473 // Read the value from the static offsets vector buffer.
3474 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
3475 // Store the smi value in the last match info.
3476 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
3477 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
3478 __ jmp(&next_capture);
3479 __ bind(&done);
3480
3481 // Return last match info.
3482 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
3483 __ add(sp, sp, Operand(4 * kPointerSize));
3484 __ Ret();
3485
3486 // Do the runtime call to execute the regexp.
3487 __ bind(&runtime);
3488 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3489#endif // V8_INTERPRETED_REGEXP
3490}
3491
3492
3493void CallFunctionStub::Generate(MacroAssembler* masm) {
3494 Label slow;
3495
3496 // If the receiver might be a value (string, number or boolean) check for this
3497 // and box it if it is.
3498 if (ReceiverMightBeValue()) {
3499 // Get the receiver from the stack.
3500 // function, receiver [, arguments]
3501 Label receiver_is_value, receiver_is_js_object;
3502 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
3503
3504 // Check if receiver is a smi (which is a number value).
3505 __ BranchOnSmi(r1, &receiver_is_value);
3506
3507 // Check if the receiver is a valid JS object.
3508 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
3509 __ b(ge, &receiver_is_js_object);
3510
3511 // Call the runtime to box the value.
3512 __ bind(&receiver_is_value);
3513 __ EnterInternalFrame();
3514 __ push(r1);
3515 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
3516 __ LeaveInternalFrame();
3517 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
3518
3519 __ bind(&receiver_is_js_object);
3520 }
3521
3522 // Get the function to call from the stack.
3523 // function, receiver [, arguments]
3524 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
3525
3526 // Check that the function is really a JavaScript function.
3527 // r1: pushed function (to be verified)
3528 __ BranchOnSmi(r1, &slow);
3529 // Get the map of the function object.
3530 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
3531 __ b(ne, &slow);
3532
3533 // Fast-case: Invoke the function now.
3534 // r1: pushed function
3535 ParameterCount actual(argc_);
3536 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
3537
3538 // Slow-case: Non-function called.
3539 __ bind(&slow);
3540 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3541 // of the original receiver from the call site).
3542 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
3543 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
3544 __ mov(r2, Operand(0));
3545 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
3546 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
3547 RelocInfo::CODE_TARGET);
3548}
3549
3550
3551// Unfortunately you have to run without snapshots to see most of these
3552// names in the profile since most compare stubs end up in the snapshot.
3553const char* CompareStub::GetName() {
3554 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
3555 (lhs_.is(r1) && rhs_.is(r0)));
3556
3557 if (name_ != NULL) return name_;
3558 const int kMaxNameLength = 100;
3559 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
3560 if (name_ == NULL) return "OOM";
3561
3562 const char* cc_name;
3563 switch (cc_) {
3564 case lt: cc_name = "LT"; break;
3565 case gt: cc_name = "GT"; break;
3566 case le: cc_name = "LE"; break;
3567 case ge: cc_name = "GE"; break;
3568 case eq: cc_name = "EQ"; break;
3569 case ne: cc_name = "NE"; break;
3570 default: cc_name = "UnknownCondition"; break;
3571 }
3572
3573 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
3574 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
3575
3576 const char* strict_name = "";
3577 if (strict_ && (cc_ == eq || cc_ == ne)) {
3578 strict_name = "_STRICT";
3579 }
3580
3581 const char* never_nan_nan_name = "";
3582 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
3583 never_nan_nan_name = "_NO_NAN";
3584 }
3585
3586 const char* include_number_compare_name = "";
3587 if (!include_number_compare_) {
3588 include_number_compare_name = "_NO_NUMBER";
3589 }
3590
3591 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
3592 "CompareStub_%s%s%s%s%s%s",
3593 cc_name,
3594 lhs_name,
3595 rhs_name,
3596 strict_name,
3597 never_nan_nan_name,
3598 include_number_compare_name);
3599 return name_;
3600}
3601
3602
3603int CompareStub::MinorKey() {
3604 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
3605 // stubs the never NaN NaN condition is only taken into account if the
3606 // condition is equals.
3607 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
3608 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
3609 (lhs_.is(r1) && rhs_.is(r0)));
3610 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
3611 | RegisterField::encode(lhs_.is(r0))
3612 | StrictField::encode(strict_)
3613 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
3614 | IncludeNumberCompareField::encode(include_number_compare_);
3615}
3616
3617
3618// StringCharCodeAtGenerator
3619
3620void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
3621 Label flat_string;
3622 Label ascii_string;
3623 Label got_char_code;
3624
3625 // If the receiver is a smi trigger the non-string case.
3626 __ BranchOnSmi(object_, receiver_not_string_);
3627
3628 // Fetch the instance type of the receiver into result register.
3629 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3630 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3631 // If the receiver is not a string trigger the non-string case.
3632 __ tst(result_, Operand(kIsNotStringMask));
3633 __ b(ne, receiver_not_string_);
3634
3635 // If the index is non-smi trigger the non-smi case.
3636 __ BranchOnNotSmi(index_, &index_not_smi_);
3637
3638 // Put smi-tagged index into scratch register.
3639 __ mov(scratch_, index_);
3640 __ bind(&got_smi_index_);
3641
3642 // Check for index out of range.
3643 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
3644 __ cmp(ip, Operand(scratch_));
3645 __ b(ls, index_out_of_range_);
3646
3647 // We need special handling for non-flat strings.
3648 STATIC_ASSERT(kSeqStringTag == 0);
3649 __ tst(result_, Operand(kStringRepresentationMask));
3650 __ b(eq, &flat_string);
3651
3652 // Handle non-flat strings.
3653 __ tst(result_, Operand(kIsConsStringMask));
3654 __ b(eq, &call_runtime_);
3655
3656 // ConsString.
3657 // Check whether the right hand side is the empty string (i.e. if
3658 // this is really a flat string in a cons string). If that is not
3659 // the case we would rather go to the runtime system now to flatten
3660 // the string.
3661 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
3662 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
3663 __ cmp(result_, Operand(ip));
3664 __ b(ne, &call_runtime_);
3665 // Get the first of the two strings and load its instance type.
3666 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
3667 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3668 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3669 // If the first cons component is also non-flat, then go to runtime.
3670 STATIC_ASSERT(kSeqStringTag == 0);
3671 __ tst(result_, Operand(kStringRepresentationMask));
3672 __ b(nz, &call_runtime_);
3673
3674 // Check for 1-byte or 2-byte string.
3675 __ bind(&flat_string);
3676 STATIC_ASSERT(kAsciiStringTag != 0);
3677 __ tst(result_, Operand(kStringEncodingMask));
3678 __ b(nz, &ascii_string);
3679
3680 // 2-byte string.
3681 // Load the 2-byte character code into the result register. We can
3682 // add without shifting since the smi tag size is the log2 of the
3683 // number of bytes in a two-byte character.
3684 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
3685 __ add(scratch_, object_, Operand(scratch_));
3686 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
3687 __ jmp(&got_char_code);
3688
3689 // ASCII string.
3690 // Load the byte into the result register.
3691 __ bind(&ascii_string);
3692 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
3693 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
3694
3695 __ bind(&got_char_code);
3696 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
3697 __ bind(&exit_);
3698}
3699
3700
3701void StringCharCodeAtGenerator::GenerateSlow(
3702 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3703 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
3704
3705 // Index is not a smi.
3706 __ bind(&index_not_smi_);
3707 // If index is a heap number, try converting it to an integer.
3708 __ CheckMap(index_,
3709 scratch_,
3710 Heap::kHeapNumberMapRootIndex,
3711 index_not_number_,
3712 true);
3713 call_helper.BeforeCall(masm);
3714 __ Push(object_, index_);
3715 __ push(index_); // Consumed by runtime conversion function.
3716 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3717 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3718 } else {
3719 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3720 // NumberToSmi discards numbers that are not exact integers.
3721 __ CallRuntime(Runtime::kNumberToSmi, 1);
3722 }
3723 // Save the conversion result before the pop instructions below
3724 // have a chance to overwrite it.
3725 __ Move(scratch_, r0);
3726 __ pop(index_);
3727 __ pop(object_);
3728 // Reload the instance type.
3729 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3730 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3731 call_helper.AfterCall(masm);
3732 // If index is still not a smi, it must be out of range.
3733 __ BranchOnNotSmi(scratch_, index_out_of_range_);
3734 // Otherwise, return to the fast path.
3735 __ jmp(&got_smi_index_);
3736
3737 // Call runtime. We get here when the receiver is a string and the
3738 // index is a number, but the code of getting the actual character
3739 // is too complex (e.g., when the string needs to be flattened).
3740 __ bind(&call_runtime_);
3741 call_helper.BeforeCall(masm);
3742 __ Push(object_, index_);
3743 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
3744 __ Move(result_, r0);
3745 call_helper.AfterCall(masm);
3746 __ jmp(&exit_);
3747
3748 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
3749}
3750
3751
3752// -------------------------------------------------------------------------
3753// StringCharFromCodeGenerator
3754
3755void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3756 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3757 STATIC_ASSERT(kSmiTag == 0);
3758 STATIC_ASSERT(kSmiShiftSize == 0);
3759 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3760 __ tst(code_,
3761 Operand(kSmiTagMask |
3762 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3763 __ b(nz, &slow_case_);
3764
3765 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
3766 // At this point code register contains smi tagged ascii char code.
3767 STATIC_ASSERT(kSmiTag == 0);
3768 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
3769 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
3770 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3771 __ cmp(result_, Operand(ip));
3772 __ b(eq, &slow_case_);
3773 __ bind(&exit_);
3774}
3775
3776
3777void StringCharFromCodeGenerator::GenerateSlow(
3778 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3779 __ Abort("Unexpected fallthrough to CharFromCode slow case");
3780
3781 __ bind(&slow_case_);
3782 call_helper.BeforeCall(masm);
3783 __ push(code_);
3784 __ CallRuntime(Runtime::kCharFromCode, 1);
3785 __ Move(result_, r0);
3786 call_helper.AfterCall(masm);
3787 __ jmp(&exit_);
3788
3789 __ Abort("Unexpected fallthrough from CharFromCode slow case");
3790}
3791
3792
3793// -------------------------------------------------------------------------
3794// StringCharAtGenerator
3795
3796void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
3797 char_code_at_generator_.GenerateFast(masm);
3798 char_from_code_generator_.GenerateFast(masm);
3799}
3800
3801
3802void StringCharAtGenerator::GenerateSlow(
3803 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3804 char_code_at_generator_.GenerateSlow(masm, call_helper);
3805 char_from_code_generator_.GenerateSlow(masm, call_helper);
3806}
3807
3808
3809class StringHelper : public AllStatic {
3810 public:
3811 // Generate code for copying characters using a simple loop. This should only
3812 // be used in places where the number of characters is small and the
3813 // additional setup and checking in GenerateCopyCharactersLong adds too much
3814 // overhead. Copying of overlapping regions is not supported.
3815 // Dest register ends at the position after the last character written.
3816 static void GenerateCopyCharacters(MacroAssembler* masm,
3817 Register dest,
3818 Register src,
3819 Register count,
3820 Register scratch,
3821 bool ascii);
3822
3823 // Generate code for copying a large number of characters. This function
3824 // is allowed to spend extra time setting up conditions to make copying
3825 // faster. Copying of overlapping regions is not supported.
3826 // Dest register ends at the position after the last character written.
3827 static void GenerateCopyCharactersLong(MacroAssembler* masm,
3828 Register dest,
3829 Register src,
3830 Register count,
3831 Register scratch1,
3832 Register scratch2,
3833 Register scratch3,
3834 Register scratch4,
3835 Register scratch5,
3836 int flags);
3837
3838
3839 // Probe the symbol table for a two character string. If the string is
3840 // not found by probing a jump to the label not_found is performed. This jump
3841 // does not guarantee that the string is not in the symbol table. If the
3842 // string is found the code falls through with the string in register r0.
3843 // Contents of both c1 and c2 registers are modified. At the exit c1 is
3844 // guaranteed to contain halfword with low and high bytes equal to
3845 // initial contents of c1 and c2 respectively.
3846 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
3847 Register c1,
3848 Register c2,
3849 Register scratch1,
3850 Register scratch2,
3851 Register scratch3,
3852 Register scratch4,
3853 Register scratch5,
3854 Label* not_found);
3855
3856 // Generate string hash.
3857 static void GenerateHashInit(MacroAssembler* masm,
3858 Register hash,
3859 Register character);
3860
3861 static void GenerateHashAddCharacter(MacroAssembler* masm,
3862 Register hash,
3863 Register character);
3864
3865 static void GenerateHashGetHash(MacroAssembler* masm,
3866 Register hash);
3867
3868 private:
3869 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
3870};
3871
3872
3873void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3874 Register dest,
3875 Register src,
3876 Register count,
3877 Register scratch,
3878 bool ascii) {
3879 Label loop;
3880 Label done;
3881 // This loop just copies one character at a time, as it is only used for very
3882 // short strings.
3883 if (!ascii) {
3884 __ add(count, count, Operand(count), SetCC);
3885 } else {
3886 __ cmp(count, Operand(0));
3887 }
3888 __ b(eq, &done);
3889
3890 __ bind(&loop);
3891 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
3892 // Perform sub between load and dependent store to get the load time to
3893 // complete.
3894 __ sub(count, count, Operand(1), SetCC);
3895 __ strb(scratch, MemOperand(dest, 1, PostIndex));
3896 // last iteration.
3897 __ b(gt, &loop);
3898
3899 __ bind(&done);
3900}
3901
3902
3903enum CopyCharactersFlags {
3904 COPY_ASCII = 1,
3905 DEST_ALWAYS_ALIGNED = 2
3906};
3907
3908
3909void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
3910 Register dest,
3911 Register src,
3912 Register count,
3913 Register scratch1,
3914 Register scratch2,
3915 Register scratch3,
3916 Register scratch4,
3917 Register scratch5,
3918 int flags) {
3919 bool ascii = (flags & COPY_ASCII) != 0;
3920 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
3921
3922 if (dest_always_aligned && FLAG_debug_code) {
3923 // Check that destination is actually word aligned if the flag says
3924 // that it is.
3925 __ tst(dest, Operand(kPointerAlignmentMask));
3926 __ Check(eq, "Destination of copy not aligned.");
3927 }
3928
3929 const int kReadAlignment = 4;
3930 const int kReadAlignmentMask = kReadAlignment - 1;
3931 // Ensure that reading an entire aligned word containing the last character
3932 // of a string will not read outside the allocated area (because we pad up
3933 // to kObjectAlignment).
3934 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
3935 // Assumes word reads and writes are little endian.
3936 // Nothing to do for zero characters.
3937 Label done;
3938 if (!ascii) {
3939 __ add(count, count, Operand(count), SetCC);
3940 } else {
3941 __ cmp(count, Operand(0));
3942 }
3943 __ b(eq, &done);
3944
3945 // Assume that you cannot read (or write) unaligned.
3946 Label byte_loop;
3947 // Must copy at least eight bytes, otherwise just do it one byte at a time.
3948 __ cmp(count, Operand(8));
3949 __ add(count, dest, Operand(count));
3950 Register limit = count; // Read until src equals this.
3951 __ b(lt, &byte_loop);
3952
3953 if (!dest_always_aligned) {
3954 // Align dest by byte copying. Copies between zero and three bytes.
3955 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
3956 Label dest_aligned;
3957 __ b(eq, &dest_aligned);
3958 __ cmp(scratch4, Operand(2));
3959 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
3960 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
3961 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
3962 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
3963 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
3964 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
3965 __ bind(&dest_aligned);
3966 }
3967
3968 Label simple_loop;
3969
3970 __ sub(scratch4, dest, Operand(src));
3971 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
3972 __ b(eq, &simple_loop);
3973 // Shift register is number of bits in a source word that
3974 // must be combined with bits in the next source word in order
3975 // to create a destination word.
3976
3977 // Complex loop for src/dst that are not aligned the same way.
3978 {
3979 Label loop;
3980 __ mov(scratch4, Operand(scratch4, LSL, 3));
3981 Register left_shift = scratch4;
3982 __ and_(src, src, Operand(~3)); // Round down to load previous word.
3983 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
3984 // Store the "shift" most significant bits of scratch in the least
3985 // signficant bits (i.e., shift down by (32-shift)).
3986 __ rsb(scratch2, left_shift, Operand(32));
3987 Register right_shift = scratch2;
3988 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
3989
3990 __ bind(&loop);
3991 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
3992 __ sub(scratch5, limit, Operand(dest));
3993 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
3994 __ str(scratch1, MemOperand(dest, 4, PostIndex));
3995 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
3996 // Loop if four or more bytes left to copy.
3997 // Compare to eight, because we did the subtract before increasing dst.
3998 __ sub(scratch5, scratch5, Operand(8), SetCC);
3999 __ b(ge, &loop);
4000 }
4001 // There is now between zero and three bytes left to copy (negative that
4002 // number is in scratch5), and between one and three bytes already read into
4003 // scratch1 (eight times that number in scratch4). We may have read past
4004 // the end of the string, but because objects are aligned, we have not read
4005 // past the end of the object.
4006 // Find the minimum of remaining characters to move and preloaded characters
4007 // and write those as bytes.
4008 __ add(scratch5, scratch5, Operand(4), SetCC);
4009 __ b(eq, &done);
4010 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
4011 // Move minimum of bytes read and bytes left to copy to scratch4.
4012 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
4013 // Between one and three (value in scratch5) characters already read into
4014 // scratch ready to write.
4015 __ cmp(scratch5, Operand(2));
4016 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4017 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
4018 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
4019 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
4020 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
4021 // Copy any remaining bytes.
4022 __ b(&byte_loop);
4023
4024 // Simple loop.
4025 // Copy words from src to dst, until less than four bytes left.
4026 // Both src and dest are word aligned.
4027 __ bind(&simple_loop);
4028 {
4029 Label loop;
4030 __ bind(&loop);
4031 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
4032 __ sub(scratch3, limit, Operand(dest));
4033 __ str(scratch1, MemOperand(dest, 4, PostIndex));
4034 // Compare to 8, not 4, because we do the substraction before increasing
4035 // dest.
4036 __ cmp(scratch3, Operand(8));
4037 __ b(ge, &loop);
4038 }
4039
4040 // Copy bytes from src to dst until dst hits limit.
4041 __ bind(&byte_loop);
4042 __ cmp(dest, Operand(limit));
4043 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
4044 __ b(ge, &done);
4045 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4046 __ b(&byte_loop);
4047
4048 __ bind(&done);
4049}
4050
4051
4052void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4053 Register c1,
4054 Register c2,
4055 Register scratch1,
4056 Register scratch2,
4057 Register scratch3,
4058 Register scratch4,
4059 Register scratch5,
4060 Label* not_found) {
4061 // Register scratch3 is the general scratch register in this function.
4062 Register scratch = scratch3;
4063
4064 // Make sure that both characters are not digits as such strings has a
4065 // different hash algorithm. Don't try to look for these in the symbol table.
4066 Label not_array_index;
4067 __ sub(scratch, c1, Operand(static_cast<int>('0')));
4068 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
4069 __ b(hi, &not_array_index);
4070 __ sub(scratch, c2, Operand(static_cast<int>('0')));
4071 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
4072
4073 // If check failed combine both characters into single halfword.
4074 // This is required by the contract of the method: code at the
4075 // not_found branch expects this combination in c1 register
4076 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
4077 __ b(ls, not_found);
4078
4079 __ bind(&not_array_index);
4080 // Calculate the two character string hash.
4081 Register hash = scratch1;
4082 StringHelper::GenerateHashInit(masm, hash, c1);
4083 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
4084 StringHelper::GenerateHashGetHash(masm, hash);
4085
4086 // Collect the two characters in a register.
4087 Register chars = c1;
4088 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
4089
4090 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4091 // hash: hash of two character string.
4092
4093 // Load symbol table
4094 // Load address of first element of the symbol table.
4095 Register symbol_table = c2;
4096 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
4097
4098 // Load undefined value
4099 Register undefined = scratch4;
4100 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
4101
4102 // Calculate capacity mask from the symbol table capacity.
4103 Register mask = scratch2;
4104 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
4105 __ mov(mask, Operand(mask, ASR, 1));
4106 __ sub(mask, mask, Operand(1));
4107
4108 // Calculate untagged address of the first element of the symbol table.
4109 Register first_symbol_table_element = symbol_table;
4110 __ add(first_symbol_table_element, symbol_table,
4111 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
4112
4113 // Registers
4114 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4115 // hash: hash of two character string
4116 // mask: capacity mask
4117 // first_symbol_table_element: address of the first element of
4118 // the symbol table
4119 // scratch: -
4120
4121 // Perform a number of probes in the symbol table.
4122 static const int kProbes = 4;
4123 Label found_in_symbol_table;
4124 Label next_probe[kProbes];
4125 for (int i = 0; i < kProbes; i++) {
4126 Register candidate = scratch5; // Scratch register contains candidate.
4127
4128 // Calculate entry in symbol table.
4129 if (i > 0) {
4130 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
4131 } else {
4132 __ mov(candidate, hash);
4133 }
4134
4135 __ and_(candidate, candidate, Operand(mask));
4136
4137 // Load the entry from the symble table.
4138 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4139 __ ldr(candidate,
4140 MemOperand(first_symbol_table_element,
4141 candidate,
4142 LSL,
4143 kPointerSizeLog2));
4144
4145 // If entry is undefined no string with this hash can be found.
4146 __ cmp(candidate, undefined);
4147 __ b(eq, not_found);
4148
4149 // If length is not 2 the string is not a candidate.
4150 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
4151 __ cmp(scratch, Operand(Smi::FromInt(2)));
4152 __ b(ne, &next_probe[i]);
4153
4154 // Check that the candidate is a non-external ascii string.
4155 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
4156 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
4157 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
4158 &next_probe[i]);
4159
4160 // Check if the two characters match.
4161 // Assumes that word load is little endian.
4162 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
4163 __ cmp(chars, scratch);
4164 __ b(eq, &found_in_symbol_table);
4165 __ bind(&next_probe[i]);
4166 }
4167
4168 // No matching 2 character string found by probing.
4169 __ jmp(not_found);
4170
4171 // Scratch register contains result when we fall through to here.
4172 Register result = scratch;
4173 __ bind(&found_in_symbol_table);
4174 __ Move(r0, result);
4175}
4176
4177
4178void StringHelper::GenerateHashInit(MacroAssembler* masm,
4179 Register hash,
4180 Register character) {
4181 // hash = character + (character << 10);
4182 __ add(hash, character, Operand(character, LSL, 10));
4183 // hash ^= hash >> 6;
4184 __ eor(hash, hash, Operand(hash, ASR, 6));
4185}
4186
4187
4188void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4189 Register hash,
4190 Register character) {
4191 // hash += character;
4192 __ add(hash, hash, Operand(character));
4193 // hash += hash << 10;
4194 __ add(hash, hash, Operand(hash, LSL, 10));
4195 // hash ^= hash >> 6;
4196 __ eor(hash, hash, Operand(hash, ASR, 6));
4197}
4198
4199
4200void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4201 Register hash) {
4202 // hash += hash << 3;
4203 __ add(hash, hash, Operand(hash, LSL, 3));
4204 // hash ^= hash >> 11;
4205 __ eor(hash, hash, Operand(hash, ASR, 11));
4206 // hash += hash << 15;
4207 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
4208
4209 // if (hash == 0) hash = 27;
4210 __ mov(hash, Operand(27), LeaveCC, nz);
4211}
4212
4213
4214void SubStringStub::Generate(MacroAssembler* masm) {
4215 Label runtime;
4216
4217 // Stack frame on entry.
4218 // lr: return address
4219 // sp[0]: to
4220 // sp[4]: from
4221 // sp[8]: string
4222
4223 // This stub is called from the native-call %_SubString(...), so
4224 // nothing can be assumed about the arguments. It is tested that:
4225 // "string" is a sequential string,
4226 // both "from" and "to" are smis, and
4227 // 0 <= from <= to <= string.length.
4228 // If any of these assumptions fail, we call the runtime system.
4229
4230 static const int kToOffset = 0 * kPointerSize;
4231 static const int kFromOffset = 1 * kPointerSize;
4232 static const int kStringOffset = 2 * kPointerSize;
4233
4234
4235 // Check bounds and smi-ness.
4236 __ ldr(r7, MemOperand(sp, kToOffset));
4237 __ ldr(r6, MemOperand(sp, kFromOffset));
4238 STATIC_ASSERT(kSmiTag == 0);
4239 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4240 // I.e., arithmetic shift right by one un-smi-tags.
4241 __ mov(r2, Operand(r7, ASR, 1), SetCC);
4242 __ mov(r3, Operand(r6, ASR, 1), SetCC, cc);
4243 // If either r2 or r6 had the smi tag bit set, then carry is set now.
4244 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
4245 __ b(mi, &runtime); // From is negative.
4246
4247 __ sub(r2, r2, Operand(r3), SetCC);
4248 __ b(mi, &runtime); // Fail if from > to.
4249 // Special handling of sub-strings of length 1 and 2. One character strings
4250 // are handled in the runtime system (looked up in the single character
4251 // cache). Two character strings are looked for in the symbol cache.
4252 __ cmp(r2, Operand(2));
4253 __ b(lt, &runtime);
4254
4255 // r2: length
4256 // r3: from index (untaged smi)
4257 // r6: from (smi)
4258 // r7: to (smi)
4259
4260 // Make sure first argument is a sequential (or flat) string.
4261 __ ldr(r5, MemOperand(sp, kStringOffset));
4262 STATIC_ASSERT(kSmiTag == 0);
4263 __ tst(r5, Operand(kSmiTagMask));
4264 __ b(eq, &runtime);
4265 Condition is_string = masm->IsObjectStringType(r5, r1);
4266 __ b(NegateCondition(is_string), &runtime);
4267
4268 // r1: instance type
4269 // r2: length
4270 // r3: from index (untaged smi)
4271 // r5: string
4272 // r6: from (smi)
4273 // r7: to (smi)
4274 Label seq_string;
4275 __ and_(r4, r1, Operand(kStringRepresentationMask));
4276 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
4277 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
4278 __ cmp(r4, Operand(kConsStringTag));
4279 __ b(gt, &runtime); // External strings go to runtime.
4280 __ b(lt, &seq_string); // Sequential strings are handled directly.
4281
4282 // Cons string. Try to recurse (once) on the first substring.
4283 // (This adds a little more generality than necessary to handle flattened
4284 // cons strings, but not much).
4285 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
4286 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
4287 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4288 __ tst(r1, Operand(kStringRepresentationMask));
4289 STATIC_ASSERT(kSeqStringTag == 0);
4290 __ b(ne, &runtime); // Cons and External strings go to runtime.
4291
4292 // Definitly a sequential string.
4293 __ bind(&seq_string);
4294
4295 // r1: instance type.
4296 // r2: length
4297 // r3: from index (untaged smi)
4298 // r5: string
4299 // r6: from (smi)
4300 // r7: to (smi)
4301 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
4302 __ cmp(r4, Operand(r7));
4303 __ b(lt, &runtime); // Fail if to > length.
4304
4305 // r1: instance type.
4306 // r2: result string length.
4307 // r3: from index (untaged smi)
4308 // r5: string.
4309 // r6: from offset (smi)
4310 // Check for flat ascii string.
4311 Label non_ascii_flat;
4312 __ tst(r1, Operand(kStringEncodingMask));
4313 STATIC_ASSERT(kTwoByteStringTag == 0);
4314 __ b(eq, &non_ascii_flat);
4315
4316 Label result_longer_than_two;
4317 __ cmp(r2, Operand(2));
4318 __ b(gt, &result_longer_than_two);
4319
4320 // Sub string of length 2 requested.
4321 // Get the two characters forming the sub string.
4322 __ add(r5, r5, Operand(r3));
4323 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
4324 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
4325
4326 // Try to lookup two character string in symbol table.
4327 Label make_two_character_string;
4328 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4329 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
4330 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4331 __ add(sp, sp, Operand(3 * kPointerSize));
4332 __ Ret();
4333
4334 // r2: result string length.
4335 // r3: two characters combined into halfword in little endian byte order.
4336 __ bind(&make_two_character_string);
4337 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
4338 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4339 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4340 __ add(sp, sp, Operand(3 * kPointerSize));
4341 __ Ret();
4342
4343 __ bind(&result_longer_than_two);
4344
4345 // Allocate the result.
4346 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
4347
4348 // r0: result string.
4349 // r2: result string length.
4350 // r5: string.
4351 // r6: from offset (smi)
4352 // Locate first character of result.
4353 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4354 // Locate 'from' character of string.
4355 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4356 __ add(r5, r5, Operand(r6, ASR, 1));
4357
4358 // r0: result string.
4359 // r1: first character of result string.
4360 // r2: result string length.
4361 // r5: first character of sub string to copy.
4362 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
4363 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
4364 COPY_ASCII | DEST_ALWAYS_ALIGNED);
4365 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4366 __ add(sp, sp, Operand(3 * kPointerSize));
4367 __ Ret();
4368
4369 __ bind(&non_ascii_flat);
4370 // r2: result string length.
4371 // r5: string.
4372 // r6: from offset (smi)
4373 // Check for flat two byte string.
4374
4375 // Allocate the result.
4376 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
4377
4378 // r0: result string.
4379 // r2: result string length.
4380 // r5: string.
4381 // Locate first character of result.
4382 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4383 // Locate 'from' character of string.
4384 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4385 // As "from" is a smi it is 2 times the value which matches the size of a two
4386 // byte character.
4387 __ add(r5, r5, Operand(r6));
4388
4389 // r0: result string.
4390 // r1: first character of result.
4391 // r2: result length.
4392 // r5: first character of string to copy.
4393 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
4394 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
4395 DEST_ALWAYS_ALIGNED);
4396 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4397 __ add(sp, sp, Operand(3 * kPointerSize));
4398 __ Ret();
4399
4400 // Just jump to runtime to create the sub string.
4401 __ bind(&runtime);
4402 __ TailCallRuntime(Runtime::kSubString, 3, 1);
4403}
4404
4405
4406void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
4407 Register left,
4408 Register right,
4409 Register scratch1,
4410 Register scratch2,
4411 Register scratch3,
4412 Register scratch4) {
4413 Label compare_lengths;
4414 // Find minimum length and length difference.
4415 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
4416 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
4417 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
4418 Register length_delta = scratch3;
4419 __ mov(scratch1, scratch2, LeaveCC, gt);
4420 Register min_length = scratch1;
4421 STATIC_ASSERT(kSmiTag == 0);
4422 __ tst(min_length, Operand(min_length));
4423 __ b(eq, &compare_lengths);
4424
4425 // Untag smi.
4426 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
4427
4428 // Setup registers so that we only need to increment one register
4429 // in the loop.
4430 __ add(scratch2, min_length,
4431 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4432 __ add(left, left, Operand(scratch2));
4433 __ add(right, right, Operand(scratch2));
4434 // Registers left and right points to the min_length character of strings.
4435 __ rsb(min_length, min_length, Operand(-1));
4436 Register index = min_length;
4437 // Index starts at -min_length.
4438
4439 {
4440 // Compare loop.
4441 Label loop;
4442 __ bind(&loop);
4443 // Compare characters.
4444 __ add(index, index, Operand(1), SetCC);
4445 __ ldrb(scratch2, MemOperand(left, index), ne);
4446 __ ldrb(scratch4, MemOperand(right, index), ne);
4447 // Skip to compare lengths with eq condition true.
4448 __ b(eq, &compare_lengths);
4449 __ cmp(scratch2, scratch4);
4450 __ b(eq, &loop);
4451 // Fallthrough with eq condition false.
4452 }
4453 // Compare lengths - strings up to min-length are equal.
4454 __ bind(&compare_lengths);
4455 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
4456 // Use zero length_delta as result.
4457 __ mov(r0, Operand(length_delta), SetCC, eq);
4458 // Fall through to here if characters compare not-equal.
4459 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
4460 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
4461 __ Ret();
4462}
4463
4464
4465void StringCompareStub::Generate(MacroAssembler* masm) {
4466 Label runtime;
4467
4468 // Stack frame on entry.
4469 // sp[0]: right string
4470 // sp[4]: left string
4471 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // left
4472 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // right
4473
4474 Label not_same;
4475 __ cmp(r0, r1);
4476 __ b(ne, &not_same);
4477 STATIC_ASSERT(EQUAL == 0);
4478 STATIC_ASSERT(kSmiTag == 0);
4479 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
4480 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
4481 __ add(sp, sp, Operand(2 * kPointerSize));
4482 __ Ret();
4483
4484 __ bind(&not_same);
4485
4486 // Check that both objects are sequential ascii strings.
4487 __ JumpIfNotBothSequentialAsciiStrings(r0, r1, r2, r3, &runtime);
4488
4489 // Compare flat ascii strings natively. Remove arguments from stack first.
4490 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
4491 __ add(sp, sp, Operand(2 * kPointerSize));
4492 GenerateCompareFlatAsciiStrings(masm, r0, r1, r2, r3, r4, r5);
4493
4494 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
4495 // tagged as a small integer.
4496 __ bind(&runtime);
4497 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
4498}
4499
4500
4501void StringAddStub::Generate(MacroAssembler* masm) {
4502 Label string_add_runtime;
4503 // Stack on entry:
4504 // sp[0]: second argument.
4505 // sp[4]: first argument.
4506
4507 // Load the two arguments.
4508 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
4509 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
4510
4511 // Make sure that both arguments are strings if not known in advance.
4512 if (string_check_) {
4513 STATIC_ASSERT(kSmiTag == 0);
4514 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
4515 // Load instance types.
4516 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4517 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4518 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4519 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4520 STATIC_ASSERT(kStringTag == 0);
4521 // If either is not a string, go to runtime.
4522 __ tst(r4, Operand(kIsNotStringMask));
4523 __ tst(r5, Operand(kIsNotStringMask), eq);
4524 __ b(ne, &string_add_runtime);
4525 }
4526
4527 // Both arguments are strings.
4528 // r0: first string
4529 // r1: second string
4530 // r4: first string instance type (if string_check_)
4531 // r5: second string instance type (if string_check_)
4532 {
4533 Label strings_not_empty;
4534 // Check if either of the strings are empty. In that case return the other.
4535 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
4536 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
4537 STATIC_ASSERT(kSmiTag == 0);
4538 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
4539 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
4540 STATIC_ASSERT(kSmiTag == 0);
4541 // Else test if second string is empty.
4542 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
4543 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
4544
4545 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4546 __ add(sp, sp, Operand(2 * kPointerSize));
4547 __ Ret();
4548
4549 __ bind(&strings_not_empty);
4550 }
4551
4552 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
4553 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
4554 // Both strings are non-empty.
4555 // r0: first string
4556 // r1: second string
4557 // r2: length of first string
4558 // r3: length of second string
4559 // r4: first string instance type (if string_check_)
4560 // r5: second string instance type (if string_check_)
4561 // Look at the length of the result of adding the two strings.
4562 Label string_add_flat_result, longer_than_two;
4563 // Adding two lengths can't overflow.
4564 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
4565 __ add(r6, r2, Operand(r3));
4566 // Use the runtime system when adding two one character strings, as it
4567 // contains optimizations for this specific case using the symbol table.
4568 __ cmp(r6, Operand(2));
4569 __ b(ne, &longer_than_two);
4570
4571 // Check that both strings are non-external ascii strings.
4572 if (!string_check_) {
4573 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4574 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4575 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4576 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4577 }
4578 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
4579 &string_add_runtime);
4580
4581 // Get the two characters forming the sub string.
4582 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4583 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
4584
4585 // Try to lookup two character string in symbol table. If it is not found
4586 // just allocate a new one.
4587 Label make_two_character_string;
4588 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4589 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
4590 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4591 __ add(sp, sp, Operand(2 * kPointerSize));
4592 __ Ret();
4593
4594 __ bind(&make_two_character_string);
4595 // Resulting string has length 2 and first chars of two strings
4596 // are combined into single halfword in r2 register.
4597 // So we can fill resulting string without two loops by a single
4598 // halfword store instruction (which assumes that processor is
4599 // in a little endian mode)
4600 __ mov(r6, Operand(2));
4601 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
4602 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4603 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4604 __ add(sp, sp, Operand(2 * kPointerSize));
4605 __ Ret();
4606
4607 __ bind(&longer_than_two);
4608 // Check if resulting string will be flat.
4609 __ cmp(r6, Operand(String::kMinNonFlatLength));
4610 __ b(lt, &string_add_flat_result);
4611 // Handle exceptionally long strings in the runtime system.
4612 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
4613 ASSERT(IsPowerOf2(String::kMaxLength + 1));
4614 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
4615 __ cmp(r6, Operand(String::kMaxLength + 1));
4616 __ b(hs, &string_add_runtime);
4617
4618 // If result is not supposed to be flat, allocate a cons string object.
4619 // If both strings are ascii the result is an ascii cons string.
4620 if (!string_check_) {
4621 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4622 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4623 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4624 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4625 }
4626 Label non_ascii, allocated, ascii_data;
4627 STATIC_ASSERT(kTwoByteStringTag == 0);
4628 __ tst(r4, Operand(kStringEncodingMask));
4629 __ tst(r5, Operand(kStringEncodingMask), ne);
4630 __ b(eq, &non_ascii);
4631
4632 // Allocate an ASCII cons string.
4633 __ bind(&ascii_data);
4634 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
4635 __ bind(&allocated);
4636 // Fill the fields of the cons string.
4637 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
4638 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
4639 __ mov(r0, Operand(r7));
4640 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4641 __ add(sp, sp, Operand(2 * kPointerSize));
4642 __ Ret();
4643
4644 __ bind(&non_ascii);
4645 // At least one of the strings is two-byte. Check whether it happens
4646 // to contain only ascii characters.
4647 // r4: first instance type.
4648 // r5: second instance type.
4649 __ tst(r4, Operand(kAsciiDataHintMask));
4650 __ tst(r5, Operand(kAsciiDataHintMask), ne);
4651 __ b(ne, &ascii_data);
4652 __ eor(r4, r4, Operand(r5));
4653 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4654 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
4655 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
4656 __ b(eq, &ascii_data);
4657
4658 // Allocate a two byte cons string.
4659 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
4660 __ jmp(&allocated);
4661
4662 // Handle creating a flat result. First check that both strings are
4663 // sequential and that they have the same encoding.
4664 // r0: first string
4665 // r1: second string
4666 // r2: length of first string
4667 // r3: length of second string
4668 // r4: first string instance type (if string_check_)
4669 // r5: second string instance type (if string_check_)
4670 // r6: sum of lengths.
4671 __ bind(&string_add_flat_result);
4672 if (!string_check_) {
4673 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4674 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4675 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4676 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4677 }
4678 // Check that both strings are sequential.
4679 STATIC_ASSERT(kSeqStringTag == 0);
4680 __ tst(r4, Operand(kStringRepresentationMask));
4681 __ tst(r5, Operand(kStringRepresentationMask), eq);
4682 __ b(ne, &string_add_runtime);
4683 // Now check if both strings have the same encoding (ASCII/Two-byte).
4684 // r0: first string.
4685 // r1: second string.
4686 // r2: length of first string.
4687 // r3: length of second string.
4688 // r6: sum of lengths..
4689 Label non_ascii_string_add_flat_result;
4690 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
4691 __ eor(r7, r4, Operand(r5));
4692 __ tst(r7, Operand(kStringEncodingMask));
4693 __ b(ne, &string_add_runtime);
4694 // And see if it's ASCII or two-byte.
4695 __ tst(r4, Operand(kStringEncodingMask));
4696 __ b(eq, &non_ascii_string_add_flat_result);
4697
4698 // Both strings are sequential ASCII strings. We also know that they are
4699 // short (since the sum of the lengths is less than kMinNonFlatLength).
4700 // r6: length of resulting flat string
4701 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
4702 // Locate first character of result.
4703 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4704 // Locate first character of first argument.
4705 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4706 // r0: first character of first string.
4707 // r1: second string.
4708 // r2: length of first string.
4709 // r3: length of second string.
4710 // r6: first character of result.
4711 // r7: result string.
4712 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
4713
4714 // Load second argument and locate first character.
4715 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4716 // r1: first character of second string.
4717 // r3: length of second string.
4718 // r6: next character of result.
4719 // r7: result string.
4720 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
4721 __ mov(r0, Operand(r7));
4722 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4723 __ add(sp, sp, Operand(2 * kPointerSize));
4724 __ Ret();
4725
4726 __ bind(&non_ascii_string_add_flat_result);
4727 // Both strings are sequential two byte strings.
4728 // r0: first string.
4729 // r1: second string.
4730 // r2: length of first string.
4731 // r3: length of second string.
4732 // r6: sum of length of strings.
4733 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
4734 // r0: first string.
4735 // r1: second string.
4736 // r2: length of first string.
4737 // r3: length of second string.
4738 // r7: result string.
4739
4740 // Locate first character of result.
4741 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4742 // Locate first character of first argument.
4743 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4744
4745 // r0: first character of first string.
4746 // r1: second string.
4747 // r2: length of first string.
4748 // r3: length of second string.
4749 // r6: first character of result.
4750 // r7: result string.
4751 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
4752
4753 // Locate first character of second argument.
4754 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4755
4756 // r1: first character of second string.
4757 // r3: length of second string.
4758 // r6: next character of result (after copy of first string).
4759 // r7: result string.
4760 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
4761
4762 __ mov(r0, Operand(r7));
4763 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4764 __ add(sp, sp, Operand(2 * kPointerSize));
4765 __ Ret();
4766
4767 // Just jump to runtime to add the two strings.
4768 __ bind(&string_add_runtime);
4769 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
4770}
4771
4772
4773#undef __
4774
4775} } // namespace v8::internal
4776
4777#endif // V8_TARGET_ARCH_ARM