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