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