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