blob: 577ac6332c0e04e872106255b9ffa692c133662f [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);
869 __ vcmp(d0, d1);
870 __ vmrs(pc);
871 __ b(ne, not_found); // The cache did not contain this value.
872 __ b(&load_result_from_cache);
873 } else {
874 __ b(not_found);
875 }
876 }
877
878 __ bind(&is_smi);
879 Register scratch = scratch1;
880 __ and_(scratch, mask, Operand(object, ASR, 1));
881 // Calculate address of entry in string cache: each entry consists
882 // of two pointer sized fields.
883 __ add(scratch,
884 number_string_cache,
885 Operand(scratch, LSL, kPointerSizeLog2 + 1));
886
887 // Check if the entry is the smi we are looking for.
888 Register probe = mask;
889 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
890 __ cmp(object, probe);
891 __ b(ne, not_found);
892
893 // Get the result from the cache.
894 __ bind(&load_result_from_cache);
895 __ ldr(result,
896 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
897 __ IncrementCounter(&Counters::number_to_string_native,
898 1,
899 scratch1,
900 scratch2);
901}
902
903
904void NumberToStringStub::Generate(MacroAssembler* masm) {
905 Label runtime;
906
907 __ ldr(r1, MemOperand(sp, 0));
908
909 // Generate code to lookup number in the number string cache.
910 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
911 __ add(sp, sp, Operand(1 * kPointerSize));
912 __ Ret();
913
914 __ bind(&runtime);
915 // Handle number to string in the runtime system if not found in the cache.
916 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
917}
918
919
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100920// On entry lhs_ and rhs_ are the values to be compared.
921// On exit r0 is 0, positive or negative to indicate the result of
922// the comparison.
923void CompareStub::Generate(MacroAssembler* masm) {
924 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
925 (lhs_.is(r1) && rhs_.is(r0)));
926
927 Label slow; // Call builtin.
928 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
929
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100930 if (include_smi_compare_) {
931 Label not_two_smis, smi_done;
932 __ orr(r2, r1, r0);
933 __ tst(r2, Operand(kSmiTagMask));
934 __ b(ne, &not_two_smis);
Ben Murdochf87a2032010-10-22 12:50:53 +0100935 __ mov(r1, Operand(r1, ASR, 1));
936 __ sub(r0, r1, Operand(r0, ASR, 1));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100937 __ Ret();
938 __ bind(&not_two_smis);
939 } else if (FLAG_debug_code) {
940 __ orr(r2, r1, r0);
941 __ tst(r2, Operand(kSmiTagMask));
942 __ Assert(nz, "CompareStub: unexpected smi operands.");
943 }
944
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100945 // NOTICE! This code is only reached after a smi-fast-case check, so
946 // it is certain that at least one operand isn't a smi.
947
948 // Handle the case where the objects are identical. Either returns the answer
949 // or goes to slow. Only falls through if the objects were not identical.
950 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
951
952 // If either is a Smi (we know that not both are), then they can only
953 // be strictly equal if the other is a HeapNumber.
954 STATIC_ASSERT(kSmiTag == 0);
955 ASSERT_EQ(0, Smi::FromInt(0));
956 __ and_(r2, lhs_, Operand(rhs_));
957 __ tst(r2, Operand(kSmiTagMask));
958 __ b(ne, &not_smis);
959 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
960 // 1) Return the answer.
961 // 2) Go to slow.
962 // 3) Fall through to both_loaded_as_doubles.
963 // 4) Jump to lhs_not_nan.
964 // In cases 3 and 4 we have found out we were dealing with a number-number
965 // comparison. If VFP3 is supported the double values of the numbers have
966 // been loaded into d7 and d6. Otherwise, the double values have been loaded
967 // into r0, r1, r2, and r3.
968 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
969
970 __ bind(&both_loaded_as_doubles);
971 // The arguments have been converted to doubles and stored in d6 and d7, if
972 // VFP3 is supported, or in r0, r1, r2, and r3.
973 if (CpuFeatures::IsSupported(VFP3)) {
974 __ bind(&lhs_not_nan);
975 CpuFeatures::Scope scope(VFP3);
976 Label no_nan;
977 // ARMv7 VFP3 instructions to implement double precision comparison.
978 __ vcmp(d7, d6);
979 __ vmrs(pc); // Move vector status bits to normal status bits.
980 Label nan;
981 __ b(vs, &nan);
982 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
983 __ mov(r0, Operand(LESS), LeaveCC, lt);
984 __ mov(r0, Operand(GREATER), LeaveCC, gt);
985 __ Ret();
986
987 __ bind(&nan);
988 // If one of the sides was a NaN then the v flag is set. Load r0 with
989 // whatever it takes to make the comparison fail, since comparisons with NaN
990 // always fail.
991 if (cc_ == lt || cc_ == le) {
992 __ mov(r0, Operand(GREATER));
993 } else {
994 __ mov(r0, Operand(LESS));
995 }
996 __ Ret();
997 } else {
998 // Checks for NaN in the doubles we have loaded. Can return the answer or
999 // fall through if neither is a NaN. Also binds lhs_not_nan.
1000 EmitNanCheck(masm, &lhs_not_nan, cc_);
1001 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
1002 // answer. Never falls through.
1003 EmitTwoNonNanDoubleComparison(masm, cc_);
1004 }
1005
1006 __ bind(&not_smis);
1007 // At this point we know we are dealing with two different objects,
1008 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
1009 if (strict_) {
1010 // This returns non-equal for some object types, or falls through if it
1011 // was not lucky.
1012 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1013 }
1014
1015 Label check_for_symbols;
1016 Label flat_string_check;
1017 // Check for heap-number-heap-number comparison. Can jump to slow case,
1018 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
1019 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1020 // In this case r2 will contain the type of rhs_. Never falls through.
1021 EmitCheckForTwoHeapNumbers(masm,
1022 lhs_,
1023 rhs_,
1024 &both_loaded_as_doubles,
1025 &check_for_symbols,
1026 &flat_string_check);
1027
1028 __ bind(&check_for_symbols);
1029 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
1030 // symbols.
1031 if (cc_ == eq && !strict_) {
1032 // Returns an answer for two symbols or two detectable objects.
1033 // Otherwise jumps to string case or not both strings case.
1034 // Assumes that r2 is the type of rhs_ on entry.
1035 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1036 }
1037
1038 // Check for both being sequential ASCII strings, and inline if that is the
1039 // case.
1040 __ bind(&flat_string_check);
1041
1042 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
1043
1044 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
1045 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1046 lhs_,
1047 rhs_,
1048 r2,
1049 r3,
1050 r4,
1051 r5);
1052 // Never falls through to here.
1053
1054 __ bind(&slow);
1055
1056 __ Push(lhs_, rhs_);
1057 // Figure out which native to call and setup the arguments.
1058 Builtins::JavaScript native;
1059 if (cc_ == eq) {
1060 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1061 } else {
1062 native = Builtins::COMPARE;
1063 int ncr; // NaN compare result
1064 if (cc_ == lt || cc_ == le) {
1065 ncr = GREATER;
1066 } else {
1067 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
1068 ncr = LESS;
1069 }
1070 __ mov(r0, Operand(Smi::FromInt(ncr)));
1071 __ push(r0);
1072 }
1073
1074 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1075 // tagged as a small integer.
1076 __ InvokeBuiltin(native, JUMP_JS);
1077}
1078
1079
1080// This stub does not handle the inlined cases (Smis, Booleans, undefined).
1081// The stub returns zero for false, and a non-zero value for true.
1082void ToBooleanStub::Generate(MacroAssembler* masm) {
1083 Label false_result;
1084 Label not_heap_number;
1085 Register scratch = r7;
1086
Ben Murdochb0fe1622011-05-05 13:52:32 +01001087 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1088 __ cmp(tos_, ip);
1089 __ b(eq, &false_result);
1090
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001091 // HeapNumber => false iff +0, -0, or NaN.
1092 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1093 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
1094 __ cmp(scratch, ip);
1095 __ b(&not_heap_number, ne);
1096
1097 __ sub(ip, tos_, Operand(kHeapObjectTag));
1098 __ vldr(d1, ip, HeapNumber::kValueOffset);
1099 __ vcmp(d1, 0.0);
1100 __ vmrs(pc);
1101 // "tos_" is a register, and contains a non zero value by default.
1102 // Hence we only need to overwrite "tos_" with zero to return false for
1103 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001104 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, eq); // for FP_ZERO
1105 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, vs); // for FP_NAN
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001106 __ Ret();
1107
1108 __ bind(&not_heap_number);
1109
1110 // Check if the value is 'null'.
1111 // 'null' => false.
1112 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1113 __ cmp(tos_, ip);
1114 __ b(&false_result, eq);
1115
1116 // It can be an undetectable object.
1117 // Undetectable => false.
1118 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
1119 __ ldrb(scratch, FieldMemOperand(ip, Map::kBitFieldOffset));
1120 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
1121 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
1122 __ b(&false_result, eq);
1123
1124 // JavaScript object => true.
1125 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1126 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1127 __ cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
1128 // "tos_" is a register and contains a non-zero value.
1129 // Hence we implicitly return true if the greater than
1130 // condition is satisfied.
1131 __ Ret(gt);
1132
1133 // Check for string
1134 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1135 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1136 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
1137 // "tos_" is a register and contains a non-zero value.
1138 // Hence we implicitly return true if the greater than
1139 // condition is satisfied.
1140 __ Ret(gt);
1141
1142 // String value => false iff empty, i.e., length is zero
1143 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
1144 // If length is zero, "tos_" contains zero ==> false.
1145 // If length is not zero, "tos_" contains a non-zero value ==> true.
1146 __ Ret();
1147
1148 // Return 0 in "tos_" for false .
1149 __ bind(&false_result);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001150 __ mov(tos_, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001151 __ Ret();
1152}
1153
1154
1155// We fall into this code if the operands were Smis, but the result was
1156// not (eg. overflow). We branch into this code (to the not_smi label) if
1157// the operands were not both Smi. The operands are in r0 and r1. In order
1158// to call the C-implemented binary fp operation routines we need to end up
1159// with the double precision floating point operands in r0 and r1 (for the
1160// value in r1) and r2 and r3 (for the value in r0).
1161void GenericBinaryOpStub::HandleBinaryOpSlowCases(
1162 MacroAssembler* masm,
1163 Label* not_smi,
1164 Register lhs,
1165 Register rhs,
1166 const Builtins::JavaScript& builtin) {
1167 Label slow, slow_reverse, do_the_call;
1168 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
1169
1170 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
1171 Register heap_number_map = r6;
1172
1173 if (ShouldGenerateSmiCode()) {
1174 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1175
1176 // Smi-smi case (overflow).
1177 // Since both are Smis there is no heap number to overwrite, so allocate.
1178 // The new heap number is in r5. r3 and r7 are scratch.
1179 __ AllocateHeapNumber(
1180 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
1181
1182 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
1183 // using registers d7 and d6 for the double values.
1184 if (CpuFeatures::IsSupported(VFP3)) {
1185 CpuFeatures::Scope scope(VFP3);
1186 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
1187 __ vmov(s15, r7);
1188 __ vcvt_f64_s32(d7, s15);
1189 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
1190 __ vmov(s13, r7);
1191 __ vcvt_f64_s32(d6, s13);
1192 if (!use_fp_registers) {
1193 __ vmov(r2, r3, d7);
1194 __ vmov(r0, r1, d6);
1195 }
1196 } else {
1197 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
1198 __ mov(r7, Operand(rhs));
1199 ConvertToDoubleStub stub1(r3, r2, r7, r9);
1200 __ push(lr);
1201 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
1202 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
1203 __ mov(r7, Operand(lhs));
1204 ConvertToDoubleStub stub2(r1, r0, r7, r9);
1205 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
1206 __ pop(lr);
1207 }
1208 __ jmp(&do_the_call); // Tail call. No return.
1209 }
1210
1211 // We branch here if at least one of r0 and r1 is not a Smi.
1212 __ bind(not_smi);
1213 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1214
1215 // After this point we have the left hand side in r1 and the right hand side
1216 // in r0.
1217 if (lhs.is(r0)) {
1218 __ Swap(r0, r1, ip);
1219 }
1220
1221 // The type transition also calculates the answer.
1222 bool generate_code_to_calculate_answer = true;
1223
1224 if (ShouldGenerateFPCode()) {
Steve Block9fac8402011-05-12 15:51:54 +01001225 // DIV has neither SmiSmi fast code nor specialized slow code.
1226 // So don't try to patch a DIV Stub.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001227 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
1228 switch (op_) {
1229 case Token::ADD:
1230 case Token::SUB:
1231 case Token::MUL:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001232 GenerateTypeTransition(masm); // Tail call.
1233 generate_code_to_calculate_answer = false;
1234 break;
1235
Steve Block9fac8402011-05-12 15:51:54 +01001236 case Token::DIV:
1237 // DIV has neither SmiSmi fast code nor specialized slow code.
1238 // So don't try to patch a DIV Stub.
1239 break;
1240
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001241 default:
1242 break;
1243 }
1244 }
1245
1246 if (generate_code_to_calculate_answer) {
1247 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
1248 if (mode_ == NO_OVERWRITE) {
1249 // In the case where there is no chance of an overwritable float we may
1250 // as well do the allocation immediately while r0 and r1 are untouched.
1251 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
1252 }
1253
1254 // Move r0 to a double in r2-r3.
1255 __ tst(r0, Operand(kSmiTagMask));
1256 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
1257 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
1258 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1259 __ cmp(r4, heap_number_map);
1260 __ b(ne, &slow);
1261 if (mode_ == OVERWRITE_RIGHT) {
1262 __ mov(r5, Operand(r0)); // Overwrite this heap number.
1263 }
1264 if (use_fp_registers) {
1265 CpuFeatures::Scope scope(VFP3);
1266 // Load the double from tagged HeapNumber r0 to d7.
1267 __ sub(r7, r0, Operand(kHeapObjectTag));
1268 __ vldr(d7, r7, HeapNumber::kValueOffset);
1269 } else {
1270 // Calling convention says that second double is in r2 and r3.
1271 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
1272 }
1273 __ jmp(&finished_loading_r0);
1274 __ bind(&r0_is_smi);
1275 if (mode_ == OVERWRITE_RIGHT) {
1276 // We can't overwrite a Smi so get address of new heap number into r5.
1277 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1278 }
1279
1280 if (CpuFeatures::IsSupported(VFP3)) {
1281 CpuFeatures::Scope scope(VFP3);
1282 // Convert smi in r0 to double in d7.
1283 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
1284 __ vmov(s15, r7);
1285 __ vcvt_f64_s32(d7, s15);
1286 if (!use_fp_registers) {
1287 __ vmov(r2, r3, d7);
1288 }
1289 } else {
1290 // Write Smi from r0 to r3 and r2 in double format.
1291 __ mov(r7, Operand(r0));
1292 ConvertToDoubleStub stub3(r3, r2, r7, r4);
1293 __ push(lr);
1294 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
1295 __ pop(lr);
1296 }
1297
1298 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
1299 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
1300 Label r1_is_not_smi;
Steve Block9fac8402011-05-12 15:51:54 +01001301 if ((runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) &&
1302 HasSmiSmiFastPath()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001303 __ tst(r1, Operand(kSmiTagMask));
1304 __ b(ne, &r1_is_not_smi);
1305 GenerateTypeTransition(masm); // Tail call.
1306 }
1307
1308 __ bind(&finished_loading_r0);
1309
1310 // Move r1 to a double in r0-r1.
1311 __ tst(r1, Operand(kSmiTagMask));
1312 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
1313 __ bind(&r1_is_not_smi);
1314 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
1315 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1316 __ cmp(r4, heap_number_map);
1317 __ b(ne, &slow);
1318 if (mode_ == OVERWRITE_LEFT) {
1319 __ mov(r5, Operand(r1)); // Overwrite this heap number.
1320 }
1321 if (use_fp_registers) {
1322 CpuFeatures::Scope scope(VFP3);
1323 // Load the double from tagged HeapNumber r1 to d6.
1324 __ sub(r7, r1, Operand(kHeapObjectTag));
1325 __ vldr(d6, r7, HeapNumber::kValueOffset);
1326 } else {
1327 // Calling convention says that first double is in r0 and r1.
1328 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
1329 }
1330 __ jmp(&finished_loading_r1);
1331 __ bind(&r1_is_smi);
1332 if (mode_ == OVERWRITE_LEFT) {
1333 // We can't overwrite a Smi so get address of new heap number into r5.
1334 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1335 }
1336
1337 if (CpuFeatures::IsSupported(VFP3)) {
1338 CpuFeatures::Scope scope(VFP3);
1339 // Convert smi in r1 to double in d6.
1340 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
1341 __ vmov(s13, r7);
1342 __ vcvt_f64_s32(d6, s13);
1343 if (!use_fp_registers) {
1344 __ vmov(r0, r1, d6);
1345 }
1346 } else {
1347 // Write Smi from r1 to r1 and r0 in double format.
1348 __ mov(r7, Operand(r1));
1349 ConvertToDoubleStub stub4(r1, r0, r7, r9);
1350 __ push(lr);
1351 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
1352 __ pop(lr);
1353 }
1354
1355 __ bind(&finished_loading_r1);
1356 }
1357
1358 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
1359 __ bind(&do_the_call);
1360 // If we are inlining the operation using VFP3 instructions for
1361 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
1362 if (use_fp_registers) {
1363 CpuFeatures::Scope scope(VFP3);
1364 // ARMv7 VFP3 instructions to implement
1365 // double precision, add, subtract, multiply, divide.
1366
1367 if (Token::MUL == op_) {
1368 __ vmul(d5, d6, d7);
1369 } else if (Token::DIV == op_) {
1370 __ vdiv(d5, d6, d7);
1371 } else if (Token::ADD == op_) {
1372 __ vadd(d5, d6, d7);
1373 } else if (Token::SUB == op_) {
1374 __ vsub(d5, d6, d7);
1375 } else {
1376 UNREACHABLE();
1377 }
1378 __ sub(r0, r5, Operand(kHeapObjectTag));
1379 __ vstr(d5, r0, HeapNumber::kValueOffset);
1380 __ add(r0, r0, Operand(kHeapObjectTag));
1381 __ mov(pc, lr);
1382 } else {
1383 // If we did not inline the operation, then the arguments are in:
1384 // r0: Left value (least significant part of mantissa).
1385 // r1: Left value (sign, exponent, top of mantissa).
1386 // r2: Right value (least significant part of mantissa).
1387 // r3: Right value (sign, exponent, top of mantissa).
1388 // r5: Address of heap number for result.
1389
1390 __ push(lr); // For later.
1391 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
1392 // Call C routine that may not cause GC or other trouble. r5 is callee
1393 // save.
1394 __ CallCFunction(ExternalReference::double_fp_operation(op_), 4);
1395 // Store answer in the overwritable heap number.
1396 #if !defined(USE_ARM_EABI)
1397 // Double returned in fp coprocessor register 0 and 1, encoded as
1398 // register cr8. Offsets must be divisible by 4 for coprocessor so we
1399 // need to substract the tag from r5.
1400 __ sub(r4, r5, Operand(kHeapObjectTag));
1401 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
1402 #else
1403 // Double returned in registers 0 and 1.
1404 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
1405 #endif
1406 __ mov(r0, Operand(r5));
1407 // And we are done.
1408 __ pop(pc);
1409 }
1410 }
1411 }
1412
1413 if (!generate_code_to_calculate_answer &&
1414 !slow_reverse.is_linked() &&
1415 !slow.is_linked()) {
1416 return;
1417 }
1418
1419 if (lhs.is(r0)) {
1420 __ b(&slow);
1421 __ bind(&slow_reverse);
1422 __ Swap(r0, r1, ip);
1423 }
1424
1425 heap_number_map = no_reg; // Don't use this any more from here on.
1426
1427 // We jump to here if something goes wrong (one param is not a number of any
1428 // sort or new-space allocation fails).
1429 __ bind(&slow);
1430
1431 // Push arguments to the stack
1432 __ Push(r1, r0);
1433
1434 if (Token::ADD == op_) {
1435 // Test for string arguments before calling runtime.
1436 // r1 : first argument
1437 // r0 : second argument
1438 // sp[0] : second argument
1439 // sp[4] : first argument
1440
1441 Label not_strings, not_string1, string1, string1_smi2;
1442 __ tst(r1, Operand(kSmiTagMask));
1443 __ b(eq, &not_string1);
1444 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
1445 __ b(ge, &not_string1);
1446
1447 // First argument is a a string, test second.
1448 __ tst(r0, Operand(kSmiTagMask));
1449 __ b(eq, &string1_smi2);
1450 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
1451 __ b(ge, &string1);
1452
1453 // First and second argument are strings.
1454 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1455 __ TailCallStub(&string_add_stub);
1456
1457 __ bind(&string1_smi2);
1458 // First argument is a string, second is a smi. Try to lookup the number
1459 // string for the smi in the number string cache.
1460 NumberToStringStub::GenerateLookupNumberStringCache(
1461 masm, r0, r2, r4, r5, r6, true, &string1);
1462
1463 // Replace second argument on stack and tailcall string add stub to make
1464 // the result.
1465 __ str(r2, MemOperand(sp, 0));
1466 __ TailCallStub(&string_add_stub);
1467
1468 // Only first argument is a string.
1469 __ bind(&string1);
1470 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
1471
1472 // First argument was not a string, test second.
1473 __ bind(&not_string1);
1474 __ tst(r0, Operand(kSmiTagMask));
1475 __ b(eq, &not_strings);
1476 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
1477 __ b(ge, &not_strings);
1478
1479 // Only second argument is a string.
1480 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
1481
1482 __ bind(&not_strings);
1483 }
1484
1485 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
1486}
1487
1488
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001489// For bitwise ops where the inputs are not both Smis we here try to determine
1490// whether both inputs are either Smis or at least heap numbers that can be
1491// represented by a 32 bit signed value. We truncate towards zero as required
1492// by the ES spec. If this is the case we do the bitwise op and see if the
1493// result is a Smi. If so, great, otherwise we try to find a heap number to
1494// write the answer into (either by allocating or by overwriting).
1495// On entry the operands are in lhs and rhs. On exit the answer is in r0.
1496void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
1497 Register lhs,
1498 Register rhs) {
1499 Label slow, result_not_a_smi;
1500 Label rhs_is_smi, lhs_is_smi;
1501 Label done_checking_rhs, done_checking_lhs;
1502
1503 Register heap_number_map = r6;
1504 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1505
1506 __ tst(lhs, Operand(kSmiTagMask));
1507 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
1508 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
1509 __ cmp(r4, heap_number_map);
1510 __ b(ne, &slow);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001511 __ ConvertToInt32(lhs, r3, r5, r4, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001512 __ jmp(&done_checking_lhs);
1513 __ bind(&lhs_is_smi);
1514 __ mov(r3, Operand(lhs, ASR, 1));
1515 __ bind(&done_checking_lhs);
1516
1517 __ tst(rhs, Operand(kSmiTagMask));
1518 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
1519 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
1520 __ cmp(r4, heap_number_map);
1521 __ b(ne, &slow);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001522 __ ConvertToInt32(rhs, r2, r5, r4, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001523 __ jmp(&done_checking_rhs);
1524 __ bind(&rhs_is_smi);
1525 __ mov(r2, Operand(rhs, ASR, 1));
1526 __ bind(&done_checking_rhs);
1527
1528 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
1529
1530 // r0 and r1: Original operands (Smi or heap numbers).
1531 // r2 and r3: Signed int32 operands.
1532 switch (op_) {
1533 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
1534 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
1535 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
1536 case Token::SAR:
1537 // Use only the 5 least significant bits of the shift count.
1538 __ and_(r2, r2, Operand(0x1f));
1539 __ mov(r2, Operand(r3, ASR, r2));
1540 break;
1541 case Token::SHR:
1542 // Use only the 5 least significant bits of the shift count.
1543 __ and_(r2, r2, Operand(0x1f));
1544 __ mov(r2, Operand(r3, LSR, r2), SetCC);
1545 // SHR is special because it is required to produce a positive answer.
1546 // The code below for writing into heap numbers isn't capable of writing
1547 // the register as an unsigned int so we go to slow case if we hit this
1548 // case.
1549 if (CpuFeatures::IsSupported(VFP3)) {
1550 __ b(mi, &result_not_a_smi);
1551 } else {
1552 __ b(mi, &slow);
1553 }
1554 break;
1555 case Token::SHL:
1556 // Use only the 5 least significant bits of the shift count.
1557 __ and_(r2, r2, Operand(0x1f));
1558 __ mov(r2, Operand(r3, LSL, r2));
1559 break;
1560 default: UNREACHABLE();
1561 }
1562 // check that the *signed* result fits in a smi
1563 __ add(r3, r2, Operand(0x40000000), SetCC);
1564 __ b(mi, &result_not_a_smi);
1565 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
1566 __ Ret();
1567
1568 Label have_to_allocate, got_a_heap_number;
1569 __ bind(&result_not_a_smi);
1570 switch (mode_) {
1571 case OVERWRITE_RIGHT: {
1572 __ tst(rhs, Operand(kSmiTagMask));
1573 __ b(eq, &have_to_allocate);
1574 __ mov(r5, Operand(rhs));
1575 break;
1576 }
1577 case OVERWRITE_LEFT: {
1578 __ tst(lhs, Operand(kSmiTagMask));
1579 __ b(eq, &have_to_allocate);
1580 __ mov(r5, Operand(lhs));
1581 break;
1582 }
1583 case NO_OVERWRITE: {
1584 // Get a new heap number in r5. r4 and r7 are scratch.
1585 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1586 }
1587 default: break;
1588 }
1589 __ bind(&got_a_heap_number);
1590 // r2: Answer as signed int32.
1591 // r5: Heap number to write answer into.
1592
1593 // Nothing can go wrong now, so move the heap number to r0, which is the
1594 // result.
1595 __ mov(r0, Operand(r5));
1596
1597 if (CpuFeatures::IsSupported(VFP3)) {
1598 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
1599 CpuFeatures::Scope scope(VFP3);
1600 __ vmov(s0, r2);
1601 if (op_ == Token::SHR) {
1602 __ vcvt_f64_u32(d0, s0);
1603 } else {
1604 __ vcvt_f64_s32(d0, s0);
1605 }
1606 __ sub(r3, r0, Operand(kHeapObjectTag));
1607 __ vstr(d0, r3, HeapNumber::kValueOffset);
1608 __ Ret();
1609 } else {
1610 // Tail call that writes the int32 in r2 to the heap number in r0, using
1611 // r3 as scratch. r0 is preserved and returned.
1612 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
1613 __ TailCallStub(&stub);
1614 }
1615
1616 if (mode_ != NO_OVERWRITE) {
1617 __ bind(&have_to_allocate);
1618 // Get a new heap number in r5. r4 and r7 are scratch.
1619 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1620 __ jmp(&got_a_heap_number);
1621 }
1622
1623 // If all else failed then we go to the runtime system.
1624 __ bind(&slow);
1625 __ Push(lhs, rhs); // Restore stack.
1626 switch (op_) {
1627 case Token::BIT_OR:
1628 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
1629 break;
1630 case Token::BIT_AND:
1631 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
1632 break;
1633 case Token::BIT_XOR:
1634 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
1635 break;
1636 case Token::SAR:
1637 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
1638 break;
1639 case Token::SHR:
1640 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
1641 break;
1642 case Token::SHL:
1643 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
1644 break;
1645 default:
1646 UNREACHABLE();
1647 }
1648}
1649
1650
1651
1652
1653// This function takes the known int in a register for the cases
1654// where it doesn't know a good trick, and may deliver
1655// a result that needs shifting.
1656static void MultiplyByKnownIntInStub(
1657 MacroAssembler* masm,
1658 Register result,
1659 Register source,
1660 Register known_int_register, // Smi tagged.
1661 int known_int,
1662 int* required_shift) { // Including Smi tag shift
1663 switch (known_int) {
1664 case 3:
1665 __ add(result, source, Operand(source, LSL, 1));
1666 *required_shift = 1;
1667 break;
1668 case 5:
1669 __ add(result, source, Operand(source, LSL, 2));
1670 *required_shift = 1;
1671 break;
1672 case 6:
1673 __ add(result, source, Operand(source, LSL, 1));
1674 *required_shift = 2;
1675 break;
1676 case 7:
1677 __ rsb(result, source, Operand(source, LSL, 3));
1678 *required_shift = 1;
1679 break;
1680 case 9:
1681 __ add(result, source, Operand(source, LSL, 3));
1682 *required_shift = 1;
1683 break;
1684 case 10:
1685 __ add(result, source, Operand(source, LSL, 2));
1686 *required_shift = 2;
1687 break;
1688 default:
1689 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
1690 __ mul(result, source, known_int_register);
1691 *required_shift = 0;
1692 }
1693}
1694
1695
1696// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
1697// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
1698// Takes the sum of the digits base (mask + 1) repeatedly until we have a
1699// number from 0 to mask. On exit the 'eq' condition flags are set if the
1700// answer is exactly the mask.
1701void IntegerModStub::DigitSum(MacroAssembler* masm,
1702 Register lhs,
1703 int mask,
1704 int shift,
1705 Label* entry) {
1706 ASSERT(mask > 0);
1707 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
1708 Label loop;
1709 __ bind(&loop);
1710 __ and_(ip, lhs, Operand(mask));
1711 __ add(lhs, ip, Operand(lhs, LSR, shift));
1712 __ bind(entry);
1713 __ cmp(lhs, Operand(mask));
1714 __ b(gt, &loop);
1715}
1716
1717
1718void IntegerModStub::DigitSum(MacroAssembler* masm,
1719 Register lhs,
1720 Register scratch,
1721 int mask,
1722 int shift1,
1723 int shift2,
1724 Label* entry) {
1725 ASSERT(mask > 0);
1726 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
1727 Label loop;
1728 __ bind(&loop);
1729 __ bic(scratch, lhs, Operand(mask));
1730 __ and_(ip, lhs, Operand(mask));
1731 __ add(lhs, ip, Operand(lhs, LSR, shift1));
1732 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
1733 __ bind(entry);
1734 __ cmp(lhs, Operand(mask));
1735 __ b(gt, &loop);
1736}
1737
1738
1739// Splits the number into two halves (bottom half has shift bits). The top
1740// half is subtracted from the bottom half. If the result is negative then
1741// rhs is added.
1742void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
1743 Register lhs,
1744 int shift,
1745 int rhs) {
1746 int mask = (1 << shift) - 1;
1747 __ and_(ip, lhs, Operand(mask));
1748 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
1749 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
1750}
1751
1752
1753void IntegerModStub::ModReduce(MacroAssembler* masm,
1754 Register lhs,
1755 int max,
1756 int denominator) {
1757 int limit = denominator;
1758 while (limit * 2 <= max) limit *= 2;
1759 while (limit >= denominator) {
1760 __ cmp(lhs, Operand(limit));
1761 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
1762 limit >>= 1;
1763 }
1764}
1765
1766
1767void IntegerModStub::ModAnswer(MacroAssembler* masm,
1768 Register result,
1769 Register shift_distance,
1770 Register mask_bits,
1771 Register sum_of_digits) {
1772 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
1773 __ Ret();
1774}
1775
1776
1777// See comment for class.
1778void IntegerModStub::Generate(MacroAssembler* masm) {
1779 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
1780 __ bic(odd_number_, odd_number_, Operand(1));
1781 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
1782 // We now have (odd_number_ - 1) * 2 in the register.
1783 // Build a switch out of branches instead of data because it avoids
1784 // having to teach the assembler about intra-code-object pointers
1785 // that are not in relative branch instructions.
1786 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
1787 Label mod21, mod23, mod25;
1788 { Assembler::BlockConstPoolScope block_const_pool(masm);
1789 __ add(pc, pc, Operand(odd_number_));
1790 // When you read pc it is always 8 ahead, but when you write it you always
1791 // write the actual value. So we put in two nops to take up the slack.
1792 __ nop();
1793 __ nop();
1794 __ b(&mod3);
1795 __ b(&mod5);
1796 __ b(&mod7);
1797 __ b(&mod9);
1798 __ b(&mod11);
1799 __ b(&mod13);
1800 __ b(&mod15);
1801 __ b(&mod17);
1802 __ b(&mod19);
1803 __ b(&mod21);
1804 __ b(&mod23);
1805 __ b(&mod25);
1806 }
1807
1808 // For each denominator we find a multiple that is almost only ones
1809 // when expressed in binary. Then we do the sum-of-digits trick for
1810 // that number. If the multiple is not 1 then we have to do a little
1811 // more work afterwards to get the answer into the 0-denominator-1
1812 // range.
1813 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
1814 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
1815 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1816
1817 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
1818 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
1819 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1820
1821 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
1822 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
1823 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1824
1825 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
1826 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
1827 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1828
1829 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
1830 ModReduce(masm, lhs_, 0x3f, 11);
1831 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1832
1833 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
1834 ModReduce(masm, lhs_, 0xff, 13);
1835 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1836
1837 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
1838 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
1839 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1840
1841 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
1842 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
1843 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1844
1845 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
1846 ModReduce(masm, lhs_, 0xff, 19);
1847 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1848
1849 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
1850 ModReduce(masm, lhs_, 0x3f, 21);
1851 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1852
1853 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
1854 ModReduce(masm, lhs_, 0xff, 23);
1855 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1856
1857 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
1858 ModReduce(masm, lhs_, 0x7f, 25);
1859 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
1860}
1861
1862
1863void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
1864 // lhs_ : x
1865 // rhs_ : y
1866 // r0 : result
1867
1868 Register result = r0;
1869 Register lhs = lhs_;
1870 Register rhs = rhs_;
1871
1872 // This code can't cope with other register allocations yet.
1873 ASSERT(result.is(r0) &&
1874 ((lhs.is(r0) && rhs.is(r1)) ||
1875 (lhs.is(r1) && rhs.is(r0))));
1876
1877 Register smi_test_reg = r7;
1878 Register scratch = r9;
1879
1880 // All ops need to know whether we are dealing with two Smis. Set up
1881 // smi_test_reg to tell us that.
1882 if (ShouldGenerateSmiCode()) {
1883 __ orr(smi_test_reg, lhs, Operand(rhs));
1884 }
1885
1886 switch (op_) {
1887 case Token::ADD: {
1888 Label not_smi;
1889 // Fast path.
1890 if (ShouldGenerateSmiCode()) {
1891 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
1892 __ tst(smi_test_reg, Operand(kSmiTagMask));
1893 __ b(ne, &not_smi);
1894 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
1895 // Return if no overflow.
1896 __ Ret(vc);
1897 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
1898 }
1899 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
1900 break;
1901 }
1902
1903 case Token::SUB: {
1904 Label not_smi;
1905 // Fast path.
1906 if (ShouldGenerateSmiCode()) {
1907 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
1908 __ tst(smi_test_reg, Operand(kSmiTagMask));
1909 __ b(ne, &not_smi);
1910 if (lhs.is(r1)) {
1911 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
1912 // Return if no overflow.
1913 __ Ret(vc);
1914 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
1915 } else {
1916 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
1917 // Return if no overflow.
1918 __ Ret(vc);
1919 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
1920 }
1921 }
1922 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
1923 break;
1924 }
1925
1926 case Token::MUL: {
1927 Label not_smi, slow;
1928 if (ShouldGenerateSmiCode()) {
1929 STATIC_ASSERT(kSmiTag == 0); // adjust code below
1930 __ tst(smi_test_reg, Operand(kSmiTagMask));
1931 Register scratch2 = smi_test_reg;
1932 smi_test_reg = no_reg;
1933 __ b(ne, &not_smi);
1934 // Remove tag from one operand (but keep sign), so that result is Smi.
1935 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
1936 // Do multiplication
1937 // scratch = lower 32 bits of ip * lhs.
1938 __ smull(scratch, scratch2, lhs, ip);
1939 // Go slow on overflows (overflow bit is not set).
1940 __ mov(ip, Operand(scratch, ASR, 31));
1941 // No overflow if higher 33 bits are identical.
1942 __ cmp(ip, Operand(scratch2));
1943 __ b(ne, &slow);
1944 // Go slow on zero result to handle -0.
1945 __ tst(scratch, Operand(scratch));
1946 __ mov(result, Operand(scratch), LeaveCC, ne);
1947 __ Ret(ne);
1948 // We need -0 if we were multiplying a negative number with 0 to get 0.
1949 // We know one of them was zero.
1950 __ add(scratch2, rhs, Operand(lhs), SetCC);
1951 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
1952 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
1953 // Slow case. We fall through here if we multiplied a negative number
1954 // with 0, because that would mean we should produce -0.
1955 __ bind(&slow);
1956 }
1957 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
1958 break;
1959 }
1960
1961 case Token::DIV:
1962 case Token::MOD: {
1963 Label not_smi;
1964 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
1965 Label lhs_is_unsuitable;
1966 __ BranchOnNotSmi(lhs, &not_smi);
1967 if (IsPowerOf2(constant_rhs_)) {
1968 if (op_ == Token::MOD) {
1969 __ and_(rhs,
1970 lhs,
1971 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
1972 SetCC);
1973 // We now have the answer, but if the input was negative we also
1974 // have the sign bit. Our work is done if the result is
1975 // positive or zero:
1976 if (!rhs.is(r0)) {
1977 __ mov(r0, rhs, LeaveCC, pl);
1978 }
1979 __ Ret(pl);
1980 // A mod of a negative left hand side must return a negative number.
1981 // Unfortunately if the answer is 0 then we must return -0. And we
1982 // already optimistically trashed rhs so we may need to restore it.
1983 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
1984 // Next two instructions are conditional on the answer being -0.
1985 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
1986 __ b(eq, &lhs_is_unsuitable);
1987 // We need to subtract the dividend. Eg. -3 % 4 == -3.
1988 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
1989 } else {
1990 ASSERT(op_ == Token::DIV);
1991 __ tst(lhs,
1992 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
1993 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
1994 int shift = 0;
1995 int d = constant_rhs_;
1996 while ((d & 1) == 0) {
1997 d >>= 1;
1998 shift++;
1999 }
2000 __ mov(r0, Operand(lhs, LSR, shift));
2001 __ bic(r0, r0, Operand(kSmiTagMask));
2002 }
2003 } else {
2004 // Not a power of 2.
2005 __ tst(lhs, Operand(0x80000000u));
2006 __ b(ne, &lhs_is_unsuitable);
2007 // Find a fixed point reciprocal of the divisor so we can divide by
2008 // multiplying.
2009 double divisor = 1.0 / constant_rhs_;
2010 int shift = 32;
2011 double scale = 4294967296.0; // 1 << 32.
2012 uint32_t mul;
2013 // Maximise the precision of the fixed point reciprocal.
2014 while (true) {
2015 mul = static_cast<uint32_t>(scale * divisor);
2016 if (mul >= 0x7fffffff) break;
2017 scale *= 2.0;
2018 shift++;
2019 }
2020 mul++;
2021 Register scratch2 = smi_test_reg;
2022 smi_test_reg = no_reg;
2023 __ mov(scratch2, Operand(mul));
2024 __ umull(scratch, scratch2, scratch2, lhs);
2025 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
2026 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
2027 // rhs is still the known rhs. rhs is Smi tagged.
2028 // lhs is still the unkown lhs. lhs is Smi tagged.
2029 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
2030 // scratch = scratch2 * rhs.
2031 MultiplyByKnownIntInStub(masm,
2032 scratch,
2033 scratch2,
2034 rhs,
2035 constant_rhs_,
2036 &required_scratch_shift);
2037 // scratch << required_scratch_shift is now the Smi tagged rhs *
2038 // (lhs / rhs) where / indicates integer division.
2039 if (op_ == Token::DIV) {
2040 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
2041 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
2042 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
2043 } else {
2044 ASSERT(op_ == Token::MOD);
2045 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
2046 }
2047 }
2048 __ Ret();
2049 __ bind(&lhs_is_unsuitable);
2050 } else if (op_ == Token::MOD &&
2051 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
2052 runtime_operands_type_ != BinaryOpIC::STRINGS) {
2053 // Do generate a bit of smi code for modulus even though the default for
2054 // modulus is not to do it, but as the ARM processor has no coprocessor
2055 // support for modulus checking for smis makes sense. We can handle
2056 // 1 to 25 times any power of 2. This covers over half the numbers from
2057 // 1 to 100 including all of the first 25. (Actually the constants < 10
2058 // are handled above by reciprocal multiplication. We only get here for
2059 // those cases if the right hand side is not a constant or for cases
2060 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
2061 // stub.)
2062 Label slow;
2063 Label not_power_of_2;
2064 ASSERT(!ShouldGenerateSmiCode());
2065 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
2066 // Check for two positive smis.
2067 __ orr(smi_test_reg, lhs, Operand(rhs));
2068 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
2069 __ b(ne, &slow);
2070 // Check that rhs is a power of two and not zero.
2071 Register mask_bits = r3;
2072 __ sub(scratch, rhs, Operand(1), SetCC);
2073 __ b(mi, &slow);
2074 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
2075 __ b(ne, &not_power_of_2);
2076 // Calculate power of two modulus.
2077 __ and_(result, lhs, Operand(scratch));
2078 __ Ret();
2079
2080 __ bind(&not_power_of_2);
2081 __ eor(scratch, scratch, Operand(mask_bits));
2082 // At least two bits are set in the modulus. The high one(s) are in
2083 // mask_bits and the low one is scratch + 1.
2084 __ and_(mask_bits, scratch, Operand(lhs));
2085 Register shift_distance = scratch;
2086 scratch = no_reg;
2087
2088 // The rhs consists of a power of 2 multiplied by some odd number.
2089 // The power-of-2 part we handle by putting the corresponding bits
2090 // from the lhs in the mask_bits register, and the power in the
2091 // shift_distance register. Shift distance is never 0 due to Smi
2092 // tagging.
2093 __ CountLeadingZeros(r4, shift_distance, shift_distance);
2094 __ rsb(shift_distance, r4, Operand(32));
2095
2096 // Now we need to find out what the odd number is. The last bit is
2097 // always 1.
2098 Register odd_number = r4;
2099 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
2100 __ cmp(odd_number, Operand(25));
2101 __ b(gt, &slow);
2102
2103 IntegerModStub stub(
2104 result, shift_distance, odd_number, mask_bits, lhs, r5);
2105 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
2106
2107 __ bind(&slow);
2108 }
2109 HandleBinaryOpSlowCases(
2110 masm,
2111 &not_smi,
2112 lhs,
2113 rhs,
2114 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
2115 break;
2116 }
2117
2118 case Token::BIT_OR:
2119 case Token::BIT_AND:
2120 case Token::BIT_XOR:
2121 case Token::SAR:
2122 case Token::SHR:
2123 case Token::SHL: {
2124 Label slow;
2125 STATIC_ASSERT(kSmiTag == 0); // adjust code below
2126 __ tst(smi_test_reg, Operand(kSmiTagMask));
2127 __ b(ne, &slow);
2128 Register scratch2 = smi_test_reg;
2129 smi_test_reg = no_reg;
2130 switch (op_) {
2131 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
2132 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
2133 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
2134 case Token::SAR:
2135 // Remove tags from right operand.
2136 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2137 __ mov(result, Operand(lhs, ASR, scratch2));
2138 // Smi tag result.
2139 __ bic(result, result, Operand(kSmiTagMask));
2140 break;
2141 case Token::SHR:
2142 // Remove tags from operands. We can't do this on a 31 bit number
2143 // because then the 0s get shifted into bit 30 instead of bit 31.
2144 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
2145 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2146 __ mov(scratch, Operand(scratch, LSR, scratch2));
2147 // Unsigned shift is not allowed to produce a negative number, so
2148 // check the sign bit and the sign bit after Smi tagging.
2149 __ tst(scratch, Operand(0xc0000000));
2150 __ b(ne, &slow);
2151 // Smi tag result.
2152 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
2153 break;
2154 case Token::SHL:
2155 // Remove tags from operands.
2156 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
2157 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2158 __ mov(scratch, Operand(scratch, LSL, scratch2));
2159 // Check that the signed result fits in a Smi.
2160 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
2161 __ b(mi, &slow);
2162 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
2163 break;
2164 default: UNREACHABLE();
2165 }
2166 __ Ret();
2167 __ bind(&slow);
2168 HandleNonSmiBitwiseOp(masm, lhs, rhs);
2169 break;
2170 }
2171
2172 default: UNREACHABLE();
2173 }
2174 // This code should be unreachable.
2175 __ stop("Unreachable");
2176
2177 // Generate an unreachable reference to the DEFAULT stub so that it can be
2178 // found at the end of this stub when clearing ICs at GC.
2179 // TODO(kaznacheev): Check performance impact and get rid of this.
2180 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
2181 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
2182 __ CallStub(&uninit);
2183 }
2184}
2185
2186
2187void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
2188 Label get_result;
2189
2190 __ Push(r1, r0);
2191
2192 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
2193 __ mov(r1, Operand(Smi::FromInt(op_)));
2194 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
2195 __ Push(r2, r1, r0);
2196
2197 __ TailCallExternalReference(
2198 ExternalReference(IC_Utility(IC::kBinaryOp_Patch)),
2199 5,
2200 1);
2201}
2202
2203
2204Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
2205 GenericBinaryOpStub stub(key, type_info);
2206 return stub.GetCode();
2207}
2208
2209
Ben Murdochb0fe1622011-05-05 13:52:32 +01002210Handle<Code> GetTypeRecordingBinaryOpStub(int key,
2211 TRBinaryOpIC::TypeInfo type_info,
2212 TRBinaryOpIC::TypeInfo result_type_info) {
2213 UNIMPLEMENTED();
2214 return Handle<Code>::null();
2215}
2216
2217
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002218void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
2219 // Argument is a number and is on stack and in r0.
2220 Label runtime_call;
2221 Label input_not_smi;
2222 Label loaded;
2223
2224 if (CpuFeatures::IsSupported(VFP3)) {
2225 // Load argument and check if it is a smi.
2226 __ BranchOnNotSmi(r0, &input_not_smi);
2227
2228 CpuFeatures::Scope scope(VFP3);
2229 // Input is a smi. Convert to double and load the low and high words
2230 // of the double into r2, r3.
2231 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
2232 __ b(&loaded);
2233
2234 __ bind(&input_not_smi);
2235 // Check if input is a HeapNumber.
2236 __ CheckMap(r0,
2237 r1,
2238 Heap::kHeapNumberMapRootIndex,
2239 &runtime_call,
2240 true);
2241 // Input is a HeapNumber. Load it to a double register and store the
2242 // low and high words into r2, r3.
2243 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
2244
2245 __ bind(&loaded);
2246 // r2 = low 32 bits of double value
2247 // r3 = high 32 bits of double value
2248 // Compute hash (the shifts are arithmetic):
2249 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2250 __ eor(r1, r2, Operand(r3));
2251 __ eor(r1, r1, Operand(r1, ASR, 16));
2252 __ eor(r1, r1, Operand(r1, ASR, 8));
2253 ASSERT(IsPowerOf2(TranscendentalCache::kCacheSize));
2254 __ And(r1, r1, Operand(TranscendentalCache::kCacheSize - 1));
2255
2256 // r2 = low 32 bits of double value.
2257 // r3 = high 32 bits of double value.
2258 // r1 = TranscendentalCache::hash(double value).
2259 __ mov(r0,
2260 Operand(ExternalReference::transcendental_cache_array_address()));
2261 // r0 points to cache array.
2262 __ ldr(r0, MemOperand(r0, type_ * sizeof(TranscendentalCache::caches_[0])));
2263 // r0 points to the cache for the type type_.
2264 // If NULL, the cache hasn't been initialized yet, so go through runtime.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002265 __ cmp(r0, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002266 __ b(eq, &runtime_call);
2267
2268#ifdef DEBUG
2269 // Check that the layout of cache elements match expectations.
2270 { TranscendentalCache::Element test_elem[2];
2271 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2272 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2273 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2274 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2275 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2276 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2277 CHECK_EQ(0, elem_in0 - elem_start);
2278 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2279 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2280 }
2281#endif
2282
2283 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
2284 __ add(r1, r1, Operand(r1, LSL, 1));
2285 __ add(r0, r0, Operand(r1, LSL, 2));
2286 // Check if cache matches: Double value is stored in uint32_t[2] array.
2287 __ ldm(ia, r0, r4.bit()| r5.bit() | r6.bit());
2288 __ cmp(r2, r4);
2289 __ b(ne, &runtime_call);
2290 __ cmp(r3, r5);
2291 __ b(ne, &runtime_call);
2292 // Cache hit. Load result, pop argument and return.
2293 __ mov(r0, Operand(r6));
2294 __ pop();
2295 __ Ret();
2296 }
2297
2298 __ bind(&runtime_call);
2299 __ TailCallExternalReference(ExternalReference(RuntimeFunction()), 1, 1);
2300}
2301
2302
2303Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2304 switch (type_) {
2305 // Add more cases when necessary.
2306 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2307 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002308 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002309 default:
2310 UNIMPLEMENTED();
2311 return Runtime::kAbort;
2312 }
2313}
2314
2315
2316void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01002317 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002318}
2319
2320
2321void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
2322 Label slow, done;
2323
2324 Register heap_number_map = r6;
2325 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2326
2327 if (op_ == Token::SUB) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002328 if (include_smi_code_) {
2329 // Check whether the value is a smi.
2330 Label try_float;
2331 __ tst(r0, Operand(kSmiTagMask));
2332 __ b(ne, &try_float);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002333
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002334 // Go slow case if the value of the expression is zero
2335 // to make sure that we switch between 0 and -0.
2336 if (negative_zero_ == kStrictNegativeZero) {
2337 // If we have to check for zero, then we can check for the max negative
2338 // smi while we are at it.
2339 __ bic(ip, r0, Operand(0x80000000), SetCC);
2340 __ b(eq, &slow);
2341 __ rsb(r0, r0, Operand(0, RelocInfo::NONE));
2342 __ Ret();
2343 } else {
2344 // The value of the expression is a smi and 0 is OK for -0. Try
2345 // optimistic subtraction '0 - value'.
2346 __ rsb(r0, r0, Operand(0, RelocInfo::NONE), SetCC);
2347 __ Ret(vc);
2348 // We don't have to reverse the optimistic neg since the only case
2349 // where we fall through is the minimum negative Smi, which is the case
2350 // where the neg leaves the register unchanged.
2351 __ jmp(&slow); // Go slow on max negative Smi.
2352 }
2353 __ bind(&try_float);
2354 } else if (FLAG_debug_code) {
2355 __ tst(r0, Operand(kSmiTagMask));
2356 __ Assert(ne, "Unexpected smi operand.");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002357 }
2358
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002359 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2360 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2361 __ cmp(r1, heap_number_map);
2362 __ b(ne, &slow);
2363 // r0 is a heap number. Get a new heap number in r1.
2364 if (overwrite_ == UNARY_OVERWRITE) {
2365 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2366 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
2367 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2368 } else {
2369 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
2370 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
2371 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2372 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
2373 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
2374 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
2375 __ mov(r0, Operand(r1));
2376 }
2377 } else if (op_ == Token::BIT_NOT) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002378 if (include_smi_code_) {
2379 Label non_smi;
2380 __ BranchOnNotSmi(r0, &non_smi);
2381 __ mvn(r0, Operand(r0));
2382 // Bit-clear inverted smi-tag.
2383 __ bic(r0, r0, Operand(kSmiTagMask));
2384 __ Ret();
2385 __ bind(&non_smi);
2386 } else if (FLAG_debug_code) {
2387 __ tst(r0, Operand(kSmiTagMask));
2388 __ Assert(ne, "Unexpected smi operand.");
2389 }
2390
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002391 // Check if the operand is a heap number.
2392 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
2393 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2394 __ cmp(r1, heap_number_map);
2395 __ b(ne, &slow);
2396
2397 // Convert the heap number is r0 to an untagged integer in r1.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002398 __ ConvertToInt32(r0, r1, r2, r3, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002399
2400 // Do the bitwise operation (move negated) and check if the result
2401 // fits in a smi.
2402 Label try_float;
2403 __ mvn(r1, Operand(r1));
2404 __ add(r2, r1, Operand(0x40000000), SetCC);
2405 __ b(mi, &try_float);
2406 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
2407 __ b(&done);
2408
2409 __ bind(&try_float);
2410 if (!overwrite_ == UNARY_OVERWRITE) {
2411 // Allocate a fresh heap number, but don't overwrite r0 until
2412 // we're sure we can do it without going through the slow case
2413 // that needs the value in r0.
2414 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
2415 __ mov(r0, Operand(r2));
2416 }
2417
2418 if (CpuFeatures::IsSupported(VFP3)) {
2419 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
2420 CpuFeatures::Scope scope(VFP3);
2421 __ vmov(s0, r1);
2422 __ vcvt_f64_s32(d0, s0);
2423 __ sub(r2, r0, Operand(kHeapObjectTag));
2424 __ vstr(d0, r2, HeapNumber::kValueOffset);
2425 } else {
2426 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
2427 // have to set up a frame.
2428 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
2429 __ push(lr);
2430 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
2431 __ pop(lr);
2432 }
2433 } else {
2434 UNIMPLEMENTED();
2435 }
2436
2437 __ bind(&done);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002438 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002439
2440 // Handle the slow case by jumping to the JavaScript builtin.
2441 __ bind(&slow);
2442 __ push(r0);
2443 switch (op_) {
2444 case Token::SUB:
2445 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
2446 break;
2447 case Token::BIT_NOT:
2448 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
2449 break;
2450 default:
2451 UNREACHABLE();
2452 }
2453}
2454
2455
2456void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
2457 // r0 holds the exception.
2458
2459 // Adjust this code if not the case.
2460 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
2461
2462 // Drop the sp to the top of the handler.
2463 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2464 __ ldr(sp, MemOperand(r3));
2465
2466 // Restore the next handler and frame pointer, discard handler state.
2467 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
2468 __ pop(r2);
2469 __ str(r2, MemOperand(r3));
2470 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
2471 __ ldm(ia_w, sp, r3.bit() | fp.bit()); // r3: discarded state.
2472
2473 // Before returning we restore the context from the frame pointer if
2474 // not NULL. The frame pointer is NULL in the exception handler of a
2475 // JS entry frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002476 __ cmp(fp, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002477 // Set cp to NULL if fp is NULL.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002478 __ mov(cp, Operand(0, RelocInfo::NONE), LeaveCC, eq);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002479 // Restore cp otherwise.
2480 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
2481#ifdef DEBUG
2482 if (FLAG_debug_code) {
2483 __ mov(lr, Operand(pc));
2484 }
2485#endif
2486 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
2487 __ pop(pc);
2488}
2489
2490
2491void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
2492 UncatchableExceptionType type) {
2493 // Adjust this code if not the case.
2494 STATIC_ASSERT(StackHandlerConstants::kSize == 4 * kPointerSize);
2495
2496 // Drop sp to the top stack handler.
2497 __ mov(r3, Operand(ExternalReference(Top::k_handler_address)));
2498 __ ldr(sp, MemOperand(r3));
2499
2500 // Unwind the handlers until the ENTRY handler is found.
2501 Label loop, done;
2502 __ bind(&loop);
2503 // Load the type of the current stack handler.
2504 const int kStateOffset = StackHandlerConstants::kStateOffset;
2505 __ ldr(r2, MemOperand(sp, kStateOffset));
2506 __ cmp(r2, Operand(StackHandler::ENTRY));
2507 __ b(eq, &done);
2508 // Fetch the next handler in the list.
2509 const int kNextOffset = StackHandlerConstants::kNextOffset;
2510 __ ldr(sp, MemOperand(sp, kNextOffset));
2511 __ jmp(&loop);
2512 __ bind(&done);
2513
2514 // Set the top handler address to next handler past the current ENTRY handler.
2515 STATIC_ASSERT(StackHandlerConstants::kNextOffset == 0);
2516 __ pop(r2);
2517 __ str(r2, MemOperand(r3));
2518
2519 if (type == OUT_OF_MEMORY) {
2520 // Set external caught exception to false.
2521 ExternalReference external_caught(Top::k_external_caught_exception_address);
2522 __ mov(r0, Operand(false));
2523 __ mov(r2, Operand(external_caught));
2524 __ str(r0, MemOperand(r2));
2525
2526 // Set pending exception and r0 to out of memory exception.
2527 Failure* out_of_memory = Failure::OutOfMemoryException();
2528 __ mov(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
2529 __ mov(r2, Operand(ExternalReference(Top::k_pending_exception_address)));
2530 __ str(r0, MemOperand(r2));
2531 }
2532
2533 // Stack layout at this point. See also StackHandlerConstants.
2534 // sp -> state (ENTRY)
2535 // fp
2536 // lr
2537
2538 // Discard handler state (r2 is not used) and restore frame pointer.
2539 STATIC_ASSERT(StackHandlerConstants::kFPOffset == 2 * kPointerSize);
2540 __ ldm(ia_w, sp, r2.bit() | fp.bit()); // r2: discarded state.
2541 // Before returning we restore the context from the frame pointer if
2542 // not NULL. The frame pointer is NULL in the exception handler of a
2543 // JS entry frame.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002544 __ cmp(fp, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002545 // Set cp to NULL if fp is NULL.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002546 __ mov(cp, Operand(0, RelocInfo::NONE), LeaveCC, eq);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002547 // Restore cp otherwise.
2548 __ ldr(cp, MemOperand(fp, StandardFrameConstants::kContextOffset), ne);
2549#ifdef DEBUG
2550 if (FLAG_debug_code) {
2551 __ mov(lr, Operand(pc));
2552 }
2553#endif
2554 STATIC_ASSERT(StackHandlerConstants::kPCOffset == 3 * kPointerSize);
2555 __ pop(pc);
2556}
2557
2558
2559void CEntryStub::GenerateCore(MacroAssembler* masm,
2560 Label* throw_normal_exception,
2561 Label* throw_termination_exception,
2562 Label* throw_out_of_memory_exception,
2563 bool do_gc,
2564 bool always_allocate,
2565 int frame_alignment_skew) {
2566 // r0: result parameter for PerformGC, if any
2567 // r4: number of arguments including receiver (C callee-saved)
2568 // r5: pointer to builtin function (C callee-saved)
2569 // r6: pointer to the first argument (C callee-saved)
2570
2571 if (do_gc) {
2572 // Passing r0.
2573 __ PrepareCallCFunction(1, r1);
2574 __ CallCFunction(ExternalReference::perform_gc_function(), 1);
2575 }
2576
2577 ExternalReference scope_depth =
2578 ExternalReference::heap_always_allocate_scope_depth();
2579 if (always_allocate) {
2580 __ mov(r0, Operand(scope_depth));
2581 __ ldr(r1, MemOperand(r0));
2582 __ add(r1, r1, Operand(1));
2583 __ str(r1, MemOperand(r0));
2584 }
2585
2586 // Call C built-in.
2587 // r0 = argc, r1 = argv
2588 __ mov(r0, Operand(r4));
2589 __ mov(r1, Operand(r6));
2590
2591 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
2592 int frame_alignment_mask = frame_alignment - 1;
2593#if defined(V8_HOST_ARCH_ARM)
2594 if (FLAG_debug_code) {
2595 if (frame_alignment > kPointerSize) {
2596 Label alignment_as_expected;
2597 ASSERT(IsPowerOf2(frame_alignment));
2598 __ sub(r2, sp, Operand(frame_alignment_skew));
2599 __ tst(r2, Operand(frame_alignment_mask));
2600 __ b(eq, &alignment_as_expected);
2601 // Don't use Check here, as it will call Runtime_Abort re-entering here.
2602 __ stop("Unexpected alignment");
2603 __ bind(&alignment_as_expected);
2604 }
2605 }
2606#endif
2607
2608 // Just before the call (jump) below lr is pushed, so the actual alignment is
2609 // adding one to the current skew.
2610 int alignment_before_call =
2611 (frame_alignment_skew + kPointerSize) & frame_alignment_mask;
2612 if (alignment_before_call > 0) {
2613 // Push until the alignment before the call is met.
Iain Merrick9ac36c92010-09-13 15:29:50 +01002614 __ mov(r2, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002615 for (int i = alignment_before_call;
2616 (i & frame_alignment_mask) != 0;
2617 i += kPointerSize) {
2618 __ push(r2);
2619 }
2620 }
2621
2622 // TODO(1242173): To let the GC traverse the return address of the exit
2623 // frames, we need to know where the return address is. Right now,
2624 // we push it on the stack to be able to find it again, but we never
2625 // restore from it in case of changes, which makes it impossible to
2626 // support moving the C entry code stub. This should be fixed, but currently
2627 // this is OK because the CEntryStub gets generated so early in the V8 boot
2628 // sequence that it is not moving ever.
2629 masm->add(lr, pc, Operand(4)); // Compute return address: (pc + 8) + 4
2630 masm->push(lr);
2631 masm->Jump(r5);
2632
2633 // Restore sp back to before aligning the stack.
2634 if (alignment_before_call > 0) {
2635 __ add(sp, sp, Operand(alignment_before_call));
2636 }
2637
2638 if (always_allocate) {
2639 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
2640 // though (contain the result).
2641 __ mov(r2, Operand(scope_depth));
2642 __ ldr(r3, MemOperand(r2));
2643 __ sub(r3, r3, Operand(1));
2644 __ str(r3, MemOperand(r2));
2645 }
2646
2647 // check for failure result
2648 Label failure_returned;
2649 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
2650 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
2651 __ add(r2, r0, Operand(1));
2652 __ tst(r2, Operand(kFailureTagMask));
2653 __ b(eq, &failure_returned);
2654
2655 // Exit C frame and return.
2656 // r0:r1: result
2657 // sp: stack pointer
2658 // fp: frame pointer
Ben Murdochb0fe1622011-05-05 13:52:32 +01002659 __ LeaveExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002660
2661 // check if we should retry or throw exception
2662 Label retry;
2663 __ bind(&failure_returned);
2664 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
2665 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
2666 __ b(eq, &retry);
2667
2668 // Special handling of out of memory exceptions.
2669 Failure* out_of_memory = Failure::OutOfMemoryException();
2670 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
2671 __ b(eq, throw_out_of_memory_exception);
2672
2673 // Retrieve the pending exception and clear the variable.
2674 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
2675 __ ldr(r3, MemOperand(ip));
2676 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
2677 __ ldr(r0, MemOperand(ip));
2678 __ str(r3, MemOperand(ip));
2679
2680 // Special handling of termination exceptions which are uncatchable
2681 // by javascript code.
2682 __ cmp(r0, Operand(Factory::termination_exception()));
2683 __ b(eq, throw_termination_exception);
2684
2685 // Handle normal exception.
2686 __ jmp(throw_normal_exception);
2687
2688 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
2689}
2690
2691
2692void CEntryStub::Generate(MacroAssembler* masm) {
2693 // Called from JavaScript; parameters are on stack as if calling JS function
2694 // r0: number of arguments including receiver
2695 // r1: pointer to builtin function
2696 // fp: frame pointer (restored after C call)
2697 // sp: stack pointer (restored as callee's sp after C call)
2698 // cp: current context (C callee-saved)
2699
2700 // Result returned in r0 or r0+r1 by default.
2701
2702 // NOTE: Invocations of builtins may return failure objects
2703 // instead of a proper result. The builtin entry handles
2704 // this by performing a garbage collection and retrying the
2705 // builtin once.
2706
2707 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002708 __ EnterExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002709
2710 // r4: number of arguments (C callee-saved)
2711 // r5: pointer to builtin function (C callee-saved)
2712 // r6: pointer to first argument (C callee-saved)
2713
2714 Label throw_normal_exception;
2715 Label throw_termination_exception;
2716 Label throw_out_of_memory_exception;
2717
2718 // Call into the runtime system.
2719 GenerateCore(masm,
2720 &throw_normal_exception,
2721 &throw_termination_exception,
2722 &throw_out_of_memory_exception,
2723 false,
2724 false,
2725 -kPointerSize);
2726
2727 // Do space-specific GC and retry runtime call.
2728 GenerateCore(masm,
2729 &throw_normal_exception,
2730 &throw_termination_exception,
2731 &throw_out_of_memory_exception,
2732 true,
2733 false,
2734 0);
2735
2736 // Do full GC and retry runtime call one final time.
2737 Failure* failure = Failure::InternalError();
2738 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
2739 GenerateCore(masm,
2740 &throw_normal_exception,
2741 &throw_termination_exception,
2742 &throw_out_of_memory_exception,
2743 true,
2744 true,
2745 kPointerSize);
2746
2747 __ bind(&throw_out_of_memory_exception);
2748 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
2749
2750 __ bind(&throw_termination_exception);
2751 GenerateThrowUncatchable(masm, TERMINATION);
2752
2753 __ bind(&throw_normal_exception);
2754 GenerateThrowTOS(masm);
2755}
2756
2757
2758void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
2759 // r0: code entry
2760 // r1: function
2761 // r2: receiver
2762 // r3: argc
2763 // [sp+0]: argv
2764
2765 Label invoke, exit;
2766
2767 // Called from C, so do not pop argc and args on exit (preserve sp)
2768 // No need to save register-passed args
2769 // Save callee-saved registers (incl. cp and fp), sp, and lr
2770 __ stm(db_w, sp, kCalleeSaved | lr.bit());
2771
2772 // Get address of argv, see stm above.
2773 // r0: code entry
2774 // r1: function
2775 // r2: receiver
2776 // r3: argc
2777 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
2778
2779 // Push a frame with special values setup to mark it as an entry frame.
2780 // r0: code entry
2781 // r1: function
2782 // r2: receiver
2783 // r3: argc
2784 // r4: argv
2785 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
2786 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
2787 __ mov(r7, Operand(Smi::FromInt(marker)));
2788 __ mov(r6, Operand(Smi::FromInt(marker)));
2789 __ mov(r5, Operand(ExternalReference(Top::k_c_entry_fp_address)));
2790 __ ldr(r5, MemOperand(r5));
2791 __ Push(r8, r7, r6, r5);
2792
2793 // Setup frame pointer for the frame to be pushed.
2794 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
2795
Ben Murdochb0fe1622011-05-05 13:52:32 +01002796#ifdef ENABLE_LOGGING_AND_PROFILING
2797 // If this is the outermost JS call, set js_entry_sp value.
2798 ExternalReference js_entry_sp(Top::k_js_entry_sp_address);
2799 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
2800 __ ldr(r6, MemOperand(r5));
2801 __ cmp(r6, Operand(0, RelocInfo::NONE));
2802 __ str(fp, MemOperand(r5), eq);
2803#endif
2804
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002805 // Call a faked try-block that does the invoke.
2806 __ bl(&invoke);
2807
2808 // Caught exception: Store result (exception) in the pending
2809 // exception field in the JSEnv and return a failure sentinel.
2810 // Coming in here the fp will be invalid because the PushTryHandler below
2811 // sets it to 0 to signal the existence of the JSEntry frame.
2812 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
2813 __ str(r0, MemOperand(ip));
2814 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
2815 __ b(&exit);
2816
2817 // Invoke: Link this frame into the handler chain.
2818 __ bind(&invoke);
2819 // Must preserve r0-r4, r5-r7 are available.
2820 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
2821 // If an exception not caught by another handler occurs, this handler
2822 // returns control to the code after the bl(&invoke) above, which
2823 // restores all kCalleeSaved registers (including cp and fp) to their
2824 // saved values before returning a failure to C.
2825
2826 // Clear any pending exceptions.
2827 __ mov(ip, Operand(ExternalReference::the_hole_value_location()));
2828 __ ldr(r5, MemOperand(ip));
2829 __ mov(ip, Operand(ExternalReference(Top::k_pending_exception_address)));
2830 __ str(r5, MemOperand(ip));
2831
2832 // Invoke the function by calling through JS entry trampoline builtin.
2833 // Notice that we cannot store a reference to the trampoline code directly in
2834 // this stub, because runtime stubs are not traversed when doing GC.
2835
2836 // Expected registers by Builtins::JSEntryTrampoline
2837 // r0: code entry
2838 // r1: function
2839 // r2: receiver
2840 // r3: argc
2841 // r4: argv
2842 if (is_construct) {
2843 ExternalReference construct_entry(Builtins::JSConstructEntryTrampoline);
2844 __ mov(ip, Operand(construct_entry));
2845 } else {
2846 ExternalReference entry(Builtins::JSEntryTrampoline);
2847 __ mov(ip, Operand(entry));
2848 }
2849 __ ldr(ip, MemOperand(ip)); // deref address
2850
2851 // Branch and link to JSEntryTrampoline. We don't use the double underscore
2852 // macro for the add instruction because we don't want the coverage tool
2853 // inserting instructions here after we read the pc.
2854 __ mov(lr, Operand(pc));
2855 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
2856
2857 // Unlink this frame from the handler chain. When reading the
2858 // address of the next handler, there is no need to use the address
2859 // displacement since the current stack pointer (sp) points directly
2860 // to the stack handler.
2861 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
2862 __ mov(ip, Operand(ExternalReference(Top::k_handler_address)));
2863 __ str(r3, MemOperand(ip));
2864 // No need to restore registers
2865 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
2866
Ben Murdochb0fe1622011-05-05 13:52:32 +01002867#ifdef ENABLE_LOGGING_AND_PROFILING
2868 // If current FP value is the same as js_entry_sp value, it means that
2869 // the current function is the outermost.
2870 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
2871 __ ldr(r6, MemOperand(r5));
2872 __ cmp(fp, Operand(r6));
2873 __ mov(r6, Operand(0, RelocInfo::NONE), LeaveCC, eq);
2874 __ str(r6, MemOperand(r5), eq);
2875#endif
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002876
2877 __ bind(&exit); // r0 holds result
2878 // Restore the top frame descriptors from the stack.
2879 __ pop(r3);
2880 __ mov(ip, Operand(ExternalReference(Top::k_c_entry_fp_address)));
2881 __ str(r3, MemOperand(ip));
2882
2883 // Reset the stack to the callee saved registers.
2884 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
2885
2886 // Restore callee-saved registers and return.
2887#ifdef DEBUG
2888 if (FLAG_debug_code) {
2889 __ mov(lr, Operand(pc));
2890 }
2891#endif
2892 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
2893}
2894
2895
Ben Murdochb0fe1622011-05-05 13:52:32 +01002896// Uses registers r0 to r4. Expected input is
Steve Block9fac8402011-05-12 15:51:54 +01002897// object in r0 (or at sp+1*kPointerSize) and function in
Ben Murdochb0fe1622011-05-05 13:52:32 +01002898// r1 (or at sp), depending on whether or not
2899// args_in_registers() is true.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002900void InstanceofStub::Generate(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002901 // Fixed register usage throughout the stub:
Steve Block9fac8402011-05-12 15:51:54 +01002902 const Register object = r0; // Object (lhs).
Ben Murdochb0fe1622011-05-05 13:52:32 +01002903 const Register map = r3; // Map of the object.
Steve Block9fac8402011-05-12 15:51:54 +01002904 const Register function = r1; // Function (rhs).
Ben Murdochb0fe1622011-05-05 13:52:32 +01002905 const Register prototype = r4; // Prototype of the function.
2906 const Register scratch = r2;
2907 Label slow, loop, is_instance, is_not_instance, not_js_object;
2908 if (!args_in_registers()) {
Steve Block9fac8402011-05-12 15:51:54 +01002909 __ ldr(object, MemOperand(sp, 1 * kPointerSize));
2910 __ ldr(function, MemOperand(sp, 0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002911 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002912
Ben Murdochb0fe1622011-05-05 13:52:32 +01002913 // Check that the left hand is a JS object and load map.
Steve Block9fac8402011-05-12 15:51:54 +01002914 __ BranchOnSmi(object, &not_js_object);
2915 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002916
2917 // Look up the function and the map in the instanceof cache.
2918 Label miss;
2919 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
Steve Block9fac8402011-05-12 15:51:54 +01002920 __ cmp(function, ip);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002921 __ b(ne, &miss);
2922 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002923 __ cmp(map, ip);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002924 __ b(ne, &miss);
Steve Block9fac8402011-05-12 15:51:54 +01002925 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002926 __ Ret(args_in_registers() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002927
2928 __ bind(&miss);
Steve Block9fac8402011-05-12 15:51:54 +01002929 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002930
2931 // Check that the function prototype is a JS object.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002932 __ BranchOnSmi(prototype, &slow);
2933 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002934
Steve Block9fac8402011-05-12 15:51:54 +01002935 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002936 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002937
2938 // Register mapping: r3 is object map and r4 is function prototype.
2939 // Get prototype of object into r2.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002940 __ ldr(scratch, FieldMemOperand(map, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002941
2942 // Loop through the prototype chain looking for the function prototype.
2943 __ bind(&loop);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002944 __ cmp(scratch, Operand(prototype));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002945 __ b(eq, &is_instance);
2946 __ LoadRoot(ip, Heap::kNullValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002947 __ cmp(scratch, ip);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002948 __ b(eq, &is_not_instance);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002949 __ ldr(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
2950 __ ldr(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002951 __ jmp(&loop);
2952
2953 __ bind(&is_instance);
2954 __ mov(r0, Operand(Smi::FromInt(0)));
2955 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002956 __ Ret(args_in_registers() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002957
2958 __ bind(&is_not_instance);
2959 __ mov(r0, Operand(Smi::FromInt(1)));
Steve Block9fac8402011-05-12 15:51:54 +01002960 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002961 __ Ret(args_in_registers() ? 0 : 2);
2962
2963 Label object_not_null, object_not_null_or_smi;
2964 __ bind(&not_js_object);
2965 // Before null, smi and string value checks, check that the rhs is a function
2966 // as for a non-function rhs an exception needs to be thrown.
2967 __ BranchOnSmi(function, &slow);
2968 __ CompareObjectType(function, map, scratch, JS_FUNCTION_TYPE);
2969 __ b(ne, &slow);
2970
2971 // Null is not instance of anything.
2972 __ cmp(scratch, Operand(Factory::null_value()));
2973 __ b(ne, &object_not_null);
2974 __ mov(r0, Operand(Smi::FromInt(1)));
2975 __ Ret(args_in_registers() ? 0 : 2);
2976
2977 __ bind(&object_not_null);
2978 // Smi values are not instances of anything.
2979 __ BranchOnNotSmi(object, &object_not_null_or_smi);
2980 __ mov(r0, Operand(Smi::FromInt(1)));
2981 __ Ret(args_in_registers() ? 0 : 2);
2982
2983 __ bind(&object_not_null_or_smi);
2984 // String values are not instances of anything.
2985 __ IsObjectJSStringType(object, scratch, &slow);
2986 __ mov(r0, Operand(Smi::FromInt(1)));
2987 __ Ret(args_in_registers() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002988
2989 // Slow-case. Tail call builtin.
Steve Block9fac8402011-05-12 15:51:54 +01002990 if (args_in_registers()) {
2991 __ Push(r0, r1);
2992 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002993 __ bind(&slow);
2994 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
2995}
2996
2997
2998void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2999 // The displacement is the offset of the last parameter (if any)
3000 // relative to the frame pointer.
3001 static const int kDisplacement =
3002 StandardFrameConstants::kCallerSPOffset - kPointerSize;
3003
3004 // Check that the key is a smi.
3005 Label slow;
3006 __ BranchOnNotSmi(r1, &slow);
3007
3008 // Check if the calling frame is an arguments adaptor frame.
3009 Label adaptor;
3010 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3011 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3012 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3013 __ b(eq, &adaptor);
3014
3015 // Check index against formal parameters count limit passed in
3016 // through register r0. Use unsigned comparison to get negative
3017 // check for free.
3018 __ cmp(r1, r0);
3019 __ b(cs, &slow);
3020
3021 // Read the argument from the stack and return it.
3022 __ sub(r3, r0, r1);
3023 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3024 __ ldr(r0, MemOperand(r3, kDisplacement));
3025 __ Jump(lr);
3026
3027 // Arguments adaptor case: Check index against actual arguments
3028 // limit found in the arguments adaptor frame. Use unsigned
3029 // comparison to get negative check for free.
3030 __ bind(&adaptor);
3031 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3032 __ cmp(r1, r0);
3033 __ b(cs, &slow);
3034
3035 // Read the argument from the adaptor frame and return it.
3036 __ sub(r3, r0, r1);
3037 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3038 __ ldr(r0, MemOperand(r3, kDisplacement));
3039 __ Jump(lr);
3040
3041 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3042 // by calling the runtime system.
3043 __ bind(&slow);
3044 __ push(r1);
3045 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3046}
3047
3048
3049void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
3050 // sp[0] : number of parameters
3051 // sp[4] : receiver displacement
3052 // sp[8] : function
3053
3054 // Check if the calling frame is an arguments adaptor frame.
3055 Label adaptor_frame, try_allocate, runtime;
3056 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3057 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3058 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3059 __ b(eq, &adaptor_frame);
3060
3061 // Get the length from the frame.
3062 __ ldr(r1, MemOperand(sp, 0));
3063 __ b(&try_allocate);
3064
3065 // Patch the arguments.length and the parameters pointer.
3066 __ bind(&adaptor_frame);
3067 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3068 __ str(r1, MemOperand(sp, 0));
3069 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3070 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
3071 __ str(r3, MemOperand(sp, 1 * kPointerSize));
3072
3073 // Try the new space allocation. Start out with computing the size
3074 // of the arguments object and the elements array in words.
3075 Label add_arguments_object;
3076 __ bind(&try_allocate);
Iain Merrick9ac36c92010-09-13 15:29:50 +01003077 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003078 __ b(eq, &add_arguments_object);
3079 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
3080 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
3081 __ bind(&add_arguments_object);
3082 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
3083
3084 // Do the allocation of both objects in one go.
3085 __ AllocateInNewSpace(
3086 r1,
3087 r0,
3088 r2,
3089 r3,
3090 &runtime,
3091 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
3092
3093 // Get the arguments boilerplate from the current (global) context.
3094 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
3095 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
3096 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
3097 __ ldr(r4, MemOperand(r4, offset));
3098
3099 // Copy the JS object part.
3100 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
3101
3102 // Setup the callee in-object property.
3103 STATIC_ASSERT(Heap::arguments_callee_index == 0);
3104 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
3105 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
3106
3107 // Get the length (smi tagged) and set that as an in-object property too.
3108 STATIC_ASSERT(Heap::arguments_length_index == 1);
3109 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
3110 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
3111
3112 // If there are no actual arguments, we're done.
3113 Label done;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003114 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003115 __ b(eq, &done);
3116
3117 // Get the parameters pointer from the stack.
3118 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
3119
3120 // Setup the elements pointer in the allocated arguments object and
3121 // initialize the header in the elements fixed array.
3122 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
3123 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
3124 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
3125 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
3126 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
3127 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
3128
3129 // Copy the fixed array slots.
3130 Label loop;
3131 // Setup r4 to point to the first array slot.
3132 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3133 __ bind(&loop);
3134 // Pre-decrement r2 with kPointerSize on each iteration.
3135 // Pre-decrement in order to skip receiver.
3136 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
3137 // Post-increment r4 with kPointerSize on each iteration.
3138 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
3139 __ sub(r1, r1, Operand(1));
Iain Merrick9ac36c92010-09-13 15:29:50 +01003140 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003141 __ b(ne, &loop);
3142
3143 // Return and remove the on-stack parameters.
3144 __ bind(&done);
3145 __ add(sp, sp, Operand(3 * kPointerSize));
3146 __ Ret();
3147
3148 // Do the runtime call to allocate the arguments object.
3149 __ bind(&runtime);
3150 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3151}
3152
3153
3154void RegExpExecStub::Generate(MacroAssembler* masm) {
3155 // Just jump directly to runtime if native RegExp is not selected at compile
3156 // time or if regexp entry in generated code is turned off runtime switch or
3157 // at compilation.
3158#ifdef V8_INTERPRETED_REGEXP
3159 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3160#else // V8_INTERPRETED_REGEXP
3161 if (!FLAG_regexp_entry_native) {
3162 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3163 return;
3164 }
3165
3166 // Stack frame on entry.
3167 // sp[0]: last_match_info (expected JSArray)
3168 // sp[4]: previous index
3169 // sp[8]: subject string
3170 // sp[12]: JSRegExp object
3171
3172 static const int kLastMatchInfoOffset = 0 * kPointerSize;
3173 static const int kPreviousIndexOffset = 1 * kPointerSize;
3174 static const int kSubjectOffset = 2 * kPointerSize;
3175 static const int kJSRegExpOffset = 3 * kPointerSize;
3176
3177 Label runtime, invoke_regexp;
3178
3179 // Allocation of registers for this function. These are in callee save
3180 // registers and will be preserved by the call to the native RegExp code, as
3181 // this code is called using the normal C calling convention. When calling
3182 // directly from generated code the native RegExp code will not do a GC and
3183 // therefore the content of these registers are safe to use after the call.
3184 Register subject = r4;
3185 Register regexp_data = r5;
3186 Register last_match_info_elements = r6;
3187
3188 // Ensure that a RegExp stack is allocated.
3189 ExternalReference address_of_regexp_stack_memory_address =
3190 ExternalReference::address_of_regexp_stack_memory_address();
3191 ExternalReference address_of_regexp_stack_memory_size =
3192 ExternalReference::address_of_regexp_stack_memory_size();
3193 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
3194 __ ldr(r0, MemOperand(r0, 0));
3195 __ tst(r0, Operand(r0));
3196 __ b(eq, &runtime);
3197
3198 // Check that the first argument is a JSRegExp object.
3199 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
3200 STATIC_ASSERT(kSmiTag == 0);
3201 __ tst(r0, Operand(kSmiTagMask));
3202 __ b(eq, &runtime);
3203 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3204 __ b(ne, &runtime);
3205
3206 // Check that the RegExp has been compiled (data contains a fixed array).
3207 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
3208 if (FLAG_debug_code) {
3209 __ tst(regexp_data, Operand(kSmiTagMask));
3210 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
3211 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
3212 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
3213 }
3214
3215 // regexp_data: RegExp data (FixedArray)
3216 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3217 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
3218 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
3219 __ b(ne, &runtime);
3220
3221 // regexp_data: RegExp data (FixedArray)
3222 // Check that the number of captures fit in the static offsets vector buffer.
3223 __ ldr(r2,
3224 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
3225 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3226 // uses the asumption that smis are 2 * their untagged value.
3227 STATIC_ASSERT(kSmiTag == 0);
3228 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3229 __ add(r2, r2, Operand(2)); // r2 was a smi.
3230 // Check that the static offsets vector buffer is large enough.
3231 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
3232 __ b(hi, &runtime);
3233
3234 // r2: Number of capture registers
3235 // regexp_data: RegExp data (FixedArray)
3236 // Check that the second argument is a string.
3237 __ ldr(subject, MemOperand(sp, kSubjectOffset));
3238 __ tst(subject, Operand(kSmiTagMask));
3239 __ b(eq, &runtime);
3240 Condition is_string = masm->IsObjectStringType(subject, r0);
3241 __ b(NegateCondition(is_string), &runtime);
3242 // Get the length of the string to r3.
3243 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
3244
3245 // r2: Number of capture registers
3246 // r3: Length of subject string as a smi
3247 // subject: Subject string
3248 // regexp_data: RegExp data (FixedArray)
3249 // Check that the third argument is a positive smi less than the subject
3250 // string length. A negative value will be greater (unsigned comparison).
3251 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
3252 __ tst(r0, Operand(kSmiTagMask));
3253 __ b(ne, &runtime);
3254 __ cmp(r3, Operand(r0));
3255 __ b(ls, &runtime);
3256
3257 // r2: Number of capture registers
3258 // subject: Subject string
3259 // regexp_data: RegExp data (FixedArray)
3260 // Check that the fourth object is a JSArray object.
3261 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
3262 __ tst(r0, Operand(kSmiTagMask));
3263 __ b(eq, &runtime);
3264 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
3265 __ b(ne, &runtime);
3266 // Check that the JSArray is in fast case.
3267 __ ldr(last_match_info_elements,
3268 FieldMemOperand(r0, JSArray::kElementsOffset));
3269 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
3270 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
3271 __ cmp(r0, ip);
3272 __ b(ne, &runtime);
3273 // Check that the last match info has space for the capture registers and the
3274 // additional information.
3275 __ ldr(r0,
3276 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
3277 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
3278 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
3279 __ b(gt, &runtime);
3280
3281 // subject: Subject string
3282 // regexp_data: RegExp data (FixedArray)
3283 // Check the representation and encoding of the subject string.
3284 Label seq_string;
3285 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
3286 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3287 // First check for flat string.
3288 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
3289 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
3290 __ b(eq, &seq_string);
3291
3292 // subject: Subject string
3293 // regexp_data: RegExp data (FixedArray)
3294 // Check for flat cons string.
3295 // A flat cons string is a cons string where the second part is the empty
3296 // string. In that case the subject string is just the first part of the cons
3297 // string. Also in this case the first part of the cons string is known to be
3298 // a sequential string or an external string.
3299 STATIC_ASSERT(kExternalStringTag !=0);
3300 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3301 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
3302 __ b(ne, &runtime);
3303 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
3304 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
3305 __ cmp(r0, r1);
3306 __ b(ne, &runtime);
3307 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
3308 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
3309 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3310 // Is first part a flat string?
3311 STATIC_ASSERT(kSeqStringTag == 0);
3312 __ tst(r0, Operand(kStringRepresentationMask));
3313 __ b(nz, &runtime);
3314
3315 __ bind(&seq_string);
3316 // subject: Subject string
3317 // regexp_data: RegExp data (FixedArray)
3318 // r0: Instance type of subject string
3319 STATIC_ASSERT(4 == kAsciiStringTag);
3320 STATIC_ASSERT(kTwoByteStringTag == 0);
3321 // Find the code object based on the assumptions above.
3322 __ and_(r0, r0, Operand(kStringEncodingMask));
3323 __ mov(r3, Operand(r0, ASR, 2), SetCC);
3324 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
3325 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
3326
3327 // Check that the irregexp code has been generated for the actual string
3328 // encoding. If it has, the field contains a code object otherwise it contains
3329 // the hole.
3330 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
3331 __ b(ne, &runtime);
3332
3333 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
3334 // r7: code
3335 // subject: Subject string
3336 // regexp_data: RegExp data (FixedArray)
3337 // Load used arguments before starting to push arguments for call to native
3338 // RegExp code to avoid handling changing stack height.
3339 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
3340 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3341
3342 // r1: previous index
3343 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
3344 // r7: code
3345 // subject: Subject string
3346 // regexp_data: RegExp data (FixedArray)
3347 // All checks done. Now push arguments for native regexp code.
3348 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
3349
3350 static const int kRegExpExecuteArguments = 7;
3351 __ push(lr);
3352 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
3353
3354 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
3355 __ mov(r0, Operand(1));
3356 __ str(r0, MemOperand(sp, 2 * kPointerSize));
3357
3358 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
3359 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
3360 __ ldr(r0, MemOperand(r0, 0));
3361 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
3362 __ ldr(r2, MemOperand(r2, 0));
3363 __ add(r0, r0, Operand(r2));
3364 __ str(r0, MemOperand(sp, 1 * kPointerSize));
3365
3366 // Argument 5 (sp[0]): static offsets vector buffer.
3367 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
3368 __ str(r0, MemOperand(sp, 0 * kPointerSize));
3369
3370 // For arguments 4 and 3 get string length, calculate start of string data and
3371 // calculate the shift of the index (0 for ASCII and 1 for two byte).
3372 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
3373 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3374 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
3375 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3376 __ eor(r3, r3, Operand(1));
3377 // Argument 4 (r3): End of string data
3378 // Argument 3 (r2): Start of string data
3379 __ add(r2, r9, Operand(r1, LSL, r3));
3380 __ add(r3, r9, Operand(r0, LSL, r3));
3381
3382 // Argument 2 (r1): Previous index.
3383 // Already there
3384
3385 // Argument 1 (r0): Subject string.
3386 __ mov(r0, subject);
3387
3388 // Locate the code entry and call it.
3389 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
3390 __ CallCFunction(r7, kRegExpExecuteArguments);
3391 __ pop(lr);
3392
3393 // r0: result
3394 // subject: subject string (callee saved)
3395 // regexp_data: RegExp data (callee saved)
3396 // last_match_info_elements: Last match info elements (callee saved)
3397
3398 // Check the result.
3399 Label success;
3400 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
3401 __ b(eq, &success);
3402 Label failure;
3403 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
3404 __ b(eq, &failure);
3405 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
3406 // If not exception it can only be retry. Handle that in the runtime system.
3407 __ b(ne, &runtime);
3408 // Result must now be exception. If there is no pending exception already a
3409 // stack overflow (on the backtrack stack) was detected in RegExp code but
3410 // haven't created the exception yet. Handle that in the runtime system.
3411 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
3412 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
3413 __ ldr(r0, MemOperand(r0, 0));
3414 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
3415 __ ldr(r1, MemOperand(r1, 0));
3416 __ cmp(r0, r1);
3417 __ b(eq, &runtime);
3418 __ bind(&failure);
3419 // For failure and exception return null.
3420 __ mov(r0, Operand(Factory::null_value()));
3421 __ add(sp, sp, Operand(4 * kPointerSize));
3422 __ Ret();
3423
3424 // Process the result from the native regexp code.
3425 __ bind(&success);
3426 __ ldr(r1,
3427 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
3428 // Calculate number of capture registers (number_of_captures + 1) * 2.
3429 STATIC_ASSERT(kSmiTag == 0);
3430 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3431 __ add(r1, r1, Operand(2)); // r1 was a smi.
3432
3433 // r1: number of capture registers
3434 // r4: subject string
3435 // Store the capture count.
3436 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
3437 __ str(r2, FieldMemOperand(last_match_info_elements,
3438 RegExpImpl::kLastCaptureCountOffset));
3439 // Store last subject and last input.
3440 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
3441 __ str(subject,
3442 FieldMemOperand(last_match_info_elements,
3443 RegExpImpl::kLastSubjectOffset));
3444 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
3445 __ str(subject,
3446 FieldMemOperand(last_match_info_elements,
3447 RegExpImpl::kLastInputOffset));
3448 __ mov(r3, last_match_info_elements);
3449 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
3450
3451 // Get the static offsets vector filled by the native regexp code.
3452 ExternalReference address_of_static_offsets_vector =
3453 ExternalReference::address_of_static_offsets_vector();
3454 __ mov(r2, Operand(address_of_static_offsets_vector));
3455
3456 // r1: number of capture registers
3457 // r2: offsets vector
3458 Label next_capture, done;
3459 // Capture register counter starts from number of capture registers and
3460 // counts down until wraping after zero.
3461 __ add(r0,
3462 last_match_info_elements,
3463 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
3464 __ bind(&next_capture);
3465 __ sub(r1, r1, Operand(1), SetCC);
3466 __ b(mi, &done);
3467 // Read the value from the static offsets vector buffer.
3468 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
3469 // Store the smi value in the last match info.
3470 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
3471 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
3472 __ jmp(&next_capture);
3473 __ bind(&done);
3474
3475 // Return last match info.
3476 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
3477 __ add(sp, sp, Operand(4 * kPointerSize));
3478 __ Ret();
3479
3480 // Do the runtime call to execute the regexp.
3481 __ bind(&runtime);
3482 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3483#endif // V8_INTERPRETED_REGEXP
3484}
3485
3486
Ben Murdochb0fe1622011-05-05 13:52:32 +01003487void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3488 const int kMaxInlineLength = 100;
3489 Label slowcase;
3490 Label done;
3491 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
3492 STATIC_ASSERT(kSmiTag == 0);
3493 STATIC_ASSERT(kSmiTagSize == 1);
3494 __ tst(r1, Operand(kSmiTagMask));
3495 __ b(ne, &slowcase);
3496 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
3497 __ b(hi, &slowcase);
3498 // Smi-tagging is equivalent to multiplying by 2.
3499 // Allocate RegExpResult followed by FixedArray with size in ebx.
3500 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3501 // Elements: [Map][Length][..elements..]
3502 // Size of JSArray with two in-object properties and the header of a
3503 // FixedArray.
3504 int objects_size =
3505 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
3506 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
3507 __ add(r2, r5, Operand(objects_size));
3508 __ AllocateInNewSpace(
3509 r2, // In: Size, in words.
3510 r0, // Out: Start of allocation (tagged).
3511 r3, // Scratch register.
3512 r4, // Scratch register.
3513 &slowcase,
3514 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
3515 // r0: Start of allocated area, object-tagged.
3516 // r1: Number of elements in array, as smi.
3517 // r5: Number of elements, untagged.
3518
3519 // Set JSArray map to global.regexp_result_map().
3520 // Set empty properties FixedArray.
3521 // Set elements to point to FixedArray allocated right after the JSArray.
3522 // Interleave operations for better latency.
3523 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
3524 __ add(r3, r0, Operand(JSRegExpResult::kSize));
3525 __ mov(r4, Operand(Factory::empty_fixed_array()));
3526 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
3527 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
3528 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
3529 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
3530 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
3531
3532 // Set input, index and length fields from arguments.
3533 __ ldr(r1, MemOperand(sp, kPointerSize * 0));
3534 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
3535 __ ldr(r1, MemOperand(sp, kPointerSize * 1));
3536 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
3537 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
3538 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
3539
3540 // Fill out the elements FixedArray.
3541 // r0: JSArray, tagged.
3542 // r3: FixedArray, tagged.
3543 // r5: Number of elements in array, untagged.
3544
3545 // Set map.
3546 __ mov(r2, Operand(Factory::fixed_array_map()));
3547 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3548 // Set FixedArray length.
3549 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
3550 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
3551 // Fill contents of fixed-array with the-hole.
3552 __ mov(r2, Operand(Factory::the_hole_value()));
3553 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3554 // Fill fixed array elements with hole.
3555 // r0: JSArray, tagged.
3556 // r2: the hole.
3557 // r3: Start of elements in FixedArray.
3558 // r5: Number of elements to fill.
3559 Label loop;
3560 __ tst(r5, Operand(r5));
3561 __ bind(&loop);
3562 __ b(le, &done); // Jump if r1 is negative or zero.
3563 __ sub(r5, r5, Operand(1), SetCC);
3564 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
3565 __ jmp(&loop);
3566
3567 __ bind(&done);
3568 __ add(sp, sp, Operand(3 * kPointerSize));
3569 __ Ret();
3570
3571 __ bind(&slowcase);
3572 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3573}
3574
3575
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003576void CallFunctionStub::Generate(MacroAssembler* masm) {
3577 Label slow;
3578
3579 // If the receiver might be a value (string, number or boolean) check for this
3580 // and box it if it is.
3581 if (ReceiverMightBeValue()) {
3582 // Get the receiver from the stack.
3583 // function, receiver [, arguments]
3584 Label receiver_is_value, receiver_is_js_object;
3585 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
3586
3587 // Check if receiver is a smi (which is a number value).
3588 __ BranchOnSmi(r1, &receiver_is_value);
3589
3590 // Check if the receiver is a valid JS object.
3591 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
3592 __ b(ge, &receiver_is_js_object);
3593
3594 // Call the runtime to box the value.
3595 __ bind(&receiver_is_value);
3596 __ EnterInternalFrame();
3597 __ push(r1);
3598 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
3599 __ LeaveInternalFrame();
3600 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
3601
3602 __ bind(&receiver_is_js_object);
3603 }
3604
3605 // Get the function to call from the stack.
3606 // function, receiver [, arguments]
3607 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
3608
3609 // Check that the function is really a JavaScript function.
3610 // r1: pushed function (to be verified)
3611 __ BranchOnSmi(r1, &slow);
3612 // Get the map of the function object.
3613 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
3614 __ b(ne, &slow);
3615
3616 // Fast-case: Invoke the function now.
3617 // r1: pushed function
3618 ParameterCount actual(argc_);
3619 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
3620
3621 // Slow-case: Non-function called.
3622 __ bind(&slow);
3623 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3624 // of the original receiver from the call site).
3625 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
3626 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
Iain Merrick9ac36c92010-09-13 15:29:50 +01003627 __ mov(r2, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003628 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
3629 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
3630 RelocInfo::CODE_TARGET);
3631}
3632
3633
3634// Unfortunately you have to run without snapshots to see most of these
3635// names in the profile since most compare stubs end up in the snapshot.
3636const char* CompareStub::GetName() {
3637 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
3638 (lhs_.is(r1) && rhs_.is(r0)));
3639
3640 if (name_ != NULL) return name_;
3641 const int kMaxNameLength = 100;
3642 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
3643 if (name_ == NULL) return "OOM";
3644
3645 const char* cc_name;
3646 switch (cc_) {
3647 case lt: cc_name = "LT"; break;
3648 case gt: cc_name = "GT"; break;
3649 case le: cc_name = "LE"; break;
3650 case ge: cc_name = "GE"; break;
3651 case eq: cc_name = "EQ"; break;
3652 case ne: cc_name = "NE"; break;
3653 default: cc_name = "UnknownCondition"; break;
3654 }
3655
3656 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
3657 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
3658
3659 const char* strict_name = "";
3660 if (strict_ && (cc_ == eq || cc_ == ne)) {
3661 strict_name = "_STRICT";
3662 }
3663
3664 const char* never_nan_nan_name = "";
3665 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
3666 never_nan_nan_name = "_NO_NAN";
3667 }
3668
3669 const char* include_number_compare_name = "";
3670 if (!include_number_compare_) {
3671 include_number_compare_name = "_NO_NUMBER";
3672 }
3673
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003674 const char* include_smi_compare_name = "";
3675 if (!include_smi_compare_) {
3676 include_smi_compare_name = "_NO_SMI";
3677 }
3678
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003679 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
3680 "CompareStub_%s%s%s%s%s%s",
3681 cc_name,
3682 lhs_name,
3683 rhs_name,
3684 strict_name,
3685 never_nan_nan_name,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003686 include_number_compare_name,
3687 include_smi_compare_name);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003688 return name_;
3689}
3690
3691
3692int CompareStub::MinorKey() {
3693 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
3694 // stubs the never NaN NaN condition is only taken into account if the
3695 // condition is equals.
3696 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
3697 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
3698 (lhs_.is(r1) && rhs_.is(r0)));
3699 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
3700 | RegisterField::encode(lhs_.is(r0))
3701 | StrictField::encode(strict_)
3702 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003703 | IncludeNumberCompareField::encode(include_number_compare_)
3704 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003705}
3706
3707
3708// StringCharCodeAtGenerator
3709
3710void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
3711 Label flat_string;
3712 Label ascii_string;
3713 Label got_char_code;
3714
3715 // If the receiver is a smi trigger the non-string case.
3716 __ BranchOnSmi(object_, receiver_not_string_);
3717
3718 // Fetch the instance type of the receiver into result register.
3719 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3720 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3721 // If the receiver is not a string trigger the non-string case.
3722 __ tst(result_, Operand(kIsNotStringMask));
3723 __ b(ne, receiver_not_string_);
3724
3725 // If the index is non-smi trigger the non-smi case.
3726 __ BranchOnNotSmi(index_, &index_not_smi_);
3727
3728 // Put smi-tagged index into scratch register.
3729 __ mov(scratch_, index_);
3730 __ bind(&got_smi_index_);
3731
3732 // Check for index out of range.
3733 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
3734 __ cmp(ip, Operand(scratch_));
3735 __ b(ls, index_out_of_range_);
3736
3737 // We need special handling for non-flat strings.
3738 STATIC_ASSERT(kSeqStringTag == 0);
3739 __ tst(result_, Operand(kStringRepresentationMask));
3740 __ b(eq, &flat_string);
3741
3742 // Handle non-flat strings.
3743 __ tst(result_, Operand(kIsConsStringMask));
3744 __ b(eq, &call_runtime_);
3745
3746 // ConsString.
3747 // Check whether the right hand side is the empty string (i.e. if
3748 // this is really a flat string in a cons string). If that is not
3749 // the case we would rather go to the runtime system now to flatten
3750 // the string.
3751 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
3752 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
3753 __ cmp(result_, Operand(ip));
3754 __ b(ne, &call_runtime_);
3755 // Get the first of the two strings and load its instance type.
3756 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
3757 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3758 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3759 // If the first cons component is also non-flat, then go to runtime.
3760 STATIC_ASSERT(kSeqStringTag == 0);
3761 __ tst(result_, Operand(kStringRepresentationMask));
3762 __ b(nz, &call_runtime_);
3763
3764 // Check for 1-byte or 2-byte string.
3765 __ bind(&flat_string);
3766 STATIC_ASSERT(kAsciiStringTag != 0);
3767 __ tst(result_, Operand(kStringEncodingMask));
3768 __ b(nz, &ascii_string);
3769
3770 // 2-byte string.
3771 // Load the 2-byte character code into the result register. We can
3772 // add without shifting since the smi tag size is the log2 of the
3773 // number of bytes in a two-byte character.
3774 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
3775 __ add(scratch_, object_, Operand(scratch_));
3776 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
3777 __ jmp(&got_char_code);
3778
3779 // ASCII string.
3780 // Load the byte into the result register.
3781 __ bind(&ascii_string);
3782 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
3783 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
3784
3785 __ bind(&got_char_code);
3786 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
3787 __ bind(&exit_);
3788}
3789
3790
3791void StringCharCodeAtGenerator::GenerateSlow(
3792 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3793 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
3794
3795 // Index is not a smi.
3796 __ bind(&index_not_smi_);
3797 // If index is a heap number, try converting it to an integer.
3798 __ CheckMap(index_,
3799 scratch_,
3800 Heap::kHeapNumberMapRootIndex,
3801 index_not_number_,
3802 true);
3803 call_helper.BeforeCall(masm);
3804 __ Push(object_, index_);
3805 __ push(index_); // Consumed by runtime conversion function.
3806 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3807 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3808 } else {
3809 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3810 // NumberToSmi discards numbers that are not exact integers.
3811 __ CallRuntime(Runtime::kNumberToSmi, 1);
3812 }
3813 // Save the conversion result before the pop instructions below
3814 // have a chance to overwrite it.
3815 __ Move(scratch_, r0);
3816 __ pop(index_);
3817 __ pop(object_);
3818 // Reload the instance type.
3819 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3820 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3821 call_helper.AfterCall(masm);
3822 // If index is still not a smi, it must be out of range.
3823 __ BranchOnNotSmi(scratch_, index_out_of_range_);
3824 // Otherwise, return to the fast path.
3825 __ jmp(&got_smi_index_);
3826
3827 // Call runtime. We get here when the receiver is a string and the
3828 // index is a number, but the code of getting the actual character
3829 // is too complex (e.g., when the string needs to be flattened).
3830 __ bind(&call_runtime_);
3831 call_helper.BeforeCall(masm);
3832 __ Push(object_, index_);
3833 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
3834 __ Move(result_, r0);
3835 call_helper.AfterCall(masm);
3836 __ jmp(&exit_);
3837
3838 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
3839}
3840
3841
3842// -------------------------------------------------------------------------
3843// StringCharFromCodeGenerator
3844
3845void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3846 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3847 STATIC_ASSERT(kSmiTag == 0);
3848 STATIC_ASSERT(kSmiShiftSize == 0);
3849 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3850 __ tst(code_,
3851 Operand(kSmiTagMask |
3852 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3853 __ b(nz, &slow_case_);
3854
3855 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
3856 // At this point code register contains smi tagged ascii char code.
3857 STATIC_ASSERT(kSmiTag == 0);
3858 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
3859 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
3860 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3861 __ cmp(result_, Operand(ip));
3862 __ b(eq, &slow_case_);
3863 __ bind(&exit_);
3864}
3865
3866
3867void StringCharFromCodeGenerator::GenerateSlow(
3868 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3869 __ Abort("Unexpected fallthrough to CharFromCode slow case");
3870
3871 __ bind(&slow_case_);
3872 call_helper.BeforeCall(masm);
3873 __ push(code_);
3874 __ CallRuntime(Runtime::kCharFromCode, 1);
3875 __ Move(result_, r0);
3876 call_helper.AfterCall(masm);
3877 __ jmp(&exit_);
3878
3879 __ Abort("Unexpected fallthrough from CharFromCode slow case");
3880}
3881
3882
3883// -------------------------------------------------------------------------
3884// StringCharAtGenerator
3885
3886void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
3887 char_code_at_generator_.GenerateFast(masm);
3888 char_from_code_generator_.GenerateFast(masm);
3889}
3890
3891
3892void StringCharAtGenerator::GenerateSlow(
3893 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3894 char_code_at_generator_.GenerateSlow(masm, call_helper);
3895 char_from_code_generator_.GenerateSlow(masm, call_helper);
3896}
3897
3898
3899class StringHelper : public AllStatic {
3900 public:
3901 // Generate code for copying characters using a simple loop. This should only
3902 // be used in places where the number of characters is small and the
3903 // additional setup and checking in GenerateCopyCharactersLong adds too much
3904 // overhead. Copying of overlapping regions is not supported.
3905 // Dest register ends at the position after the last character written.
3906 static void GenerateCopyCharacters(MacroAssembler* masm,
3907 Register dest,
3908 Register src,
3909 Register count,
3910 Register scratch,
3911 bool ascii);
3912
3913 // Generate code for copying a large number of characters. This function
3914 // is allowed to spend extra time setting up conditions to make copying
3915 // faster. Copying of overlapping regions is not supported.
3916 // Dest register ends at the position after the last character written.
3917 static void GenerateCopyCharactersLong(MacroAssembler* masm,
3918 Register dest,
3919 Register src,
3920 Register count,
3921 Register scratch1,
3922 Register scratch2,
3923 Register scratch3,
3924 Register scratch4,
3925 Register scratch5,
3926 int flags);
3927
3928
3929 // Probe the symbol table for a two character string. If the string is
3930 // not found by probing a jump to the label not_found is performed. This jump
3931 // does not guarantee that the string is not in the symbol table. If the
3932 // string is found the code falls through with the string in register r0.
3933 // Contents of both c1 and c2 registers are modified. At the exit c1 is
3934 // guaranteed to contain halfword with low and high bytes equal to
3935 // initial contents of c1 and c2 respectively.
3936 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
3937 Register c1,
3938 Register c2,
3939 Register scratch1,
3940 Register scratch2,
3941 Register scratch3,
3942 Register scratch4,
3943 Register scratch5,
3944 Label* not_found);
3945
3946 // Generate string hash.
3947 static void GenerateHashInit(MacroAssembler* masm,
3948 Register hash,
3949 Register character);
3950
3951 static void GenerateHashAddCharacter(MacroAssembler* masm,
3952 Register hash,
3953 Register character);
3954
3955 static void GenerateHashGetHash(MacroAssembler* masm,
3956 Register hash);
3957
3958 private:
3959 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
3960};
3961
3962
3963void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3964 Register dest,
3965 Register src,
3966 Register count,
3967 Register scratch,
3968 bool ascii) {
3969 Label loop;
3970 Label done;
3971 // This loop just copies one character at a time, as it is only used for very
3972 // short strings.
3973 if (!ascii) {
3974 __ add(count, count, Operand(count), SetCC);
3975 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01003976 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003977 }
3978 __ b(eq, &done);
3979
3980 __ bind(&loop);
3981 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
3982 // Perform sub between load and dependent store to get the load time to
3983 // complete.
3984 __ sub(count, count, Operand(1), SetCC);
3985 __ strb(scratch, MemOperand(dest, 1, PostIndex));
3986 // last iteration.
3987 __ b(gt, &loop);
3988
3989 __ bind(&done);
3990}
3991
3992
3993enum CopyCharactersFlags {
3994 COPY_ASCII = 1,
3995 DEST_ALWAYS_ALIGNED = 2
3996};
3997
3998
3999void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
4000 Register dest,
4001 Register src,
4002 Register count,
4003 Register scratch1,
4004 Register scratch2,
4005 Register scratch3,
4006 Register scratch4,
4007 Register scratch5,
4008 int flags) {
4009 bool ascii = (flags & COPY_ASCII) != 0;
4010 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
4011
4012 if (dest_always_aligned && FLAG_debug_code) {
4013 // Check that destination is actually word aligned if the flag says
4014 // that it is.
4015 __ tst(dest, Operand(kPointerAlignmentMask));
4016 __ Check(eq, "Destination of copy not aligned.");
4017 }
4018
4019 const int kReadAlignment = 4;
4020 const int kReadAlignmentMask = kReadAlignment - 1;
4021 // Ensure that reading an entire aligned word containing the last character
4022 // of a string will not read outside the allocated area (because we pad up
4023 // to kObjectAlignment).
4024 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
4025 // Assumes word reads and writes are little endian.
4026 // Nothing to do for zero characters.
4027 Label done;
4028 if (!ascii) {
4029 __ add(count, count, Operand(count), SetCC);
4030 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01004031 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004032 }
4033 __ b(eq, &done);
4034
4035 // Assume that you cannot read (or write) unaligned.
4036 Label byte_loop;
4037 // Must copy at least eight bytes, otherwise just do it one byte at a time.
4038 __ cmp(count, Operand(8));
4039 __ add(count, dest, Operand(count));
4040 Register limit = count; // Read until src equals this.
4041 __ b(lt, &byte_loop);
4042
4043 if (!dest_always_aligned) {
4044 // Align dest by byte copying. Copies between zero and three bytes.
4045 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
4046 Label dest_aligned;
4047 __ b(eq, &dest_aligned);
4048 __ cmp(scratch4, Operand(2));
4049 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
4050 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
4051 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
4052 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4053 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
4054 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
4055 __ bind(&dest_aligned);
4056 }
4057
4058 Label simple_loop;
4059
4060 __ sub(scratch4, dest, Operand(src));
4061 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
4062 __ b(eq, &simple_loop);
4063 // Shift register is number of bits in a source word that
4064 // must be combined with bits in the next source word in order
4065 // to create a destination word.
4066
4067 // Complex loop for src/dst that are not aligned the same way.
4068 {
4069 Label loop;
4070 __ mov(scratch4, Operand(scratch4, LSL, 3));
4071 Register left_shift = scratch4;
4072 __ and_(src, src, Operand(~3)); // Round down to load previous word.
4073 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
4074 // Store the "shift" most significant bits of scratch in the least
4075 // signficant bits (i.e., shift down by (32-shift)).
4076 __ rsb(scratch2, left_shift, Operand(32));
4077 Register right_shift = scratch2;
4078 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
4079
4080 __ bind(&loop);
4081 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
4082 __ sub(scratch5, limit, Operand(dest));
4083 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
4084 __ str(scratch1, MemOperand(dest, 4, PostIndex));
4085 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
4086 // Loop if four or more bytes left to copy.
4087 // Compare to eight, because we did the subtract before increasing dst.
4088 __ sub(scratch5, scratch5, Operand(8), SetCC);
4089 __ b(ge, &loop);
4090 }
4091 // There is now between zero and three bytes left to copy (negative that
4092 // number is in scratch5), and between one and three bytes already read into
4093 // scratch1 (eight times that number in scratch4). We may have read past
4094 // the end of the string, but because objects are aligned, we have not read
4095 // past the end of the object.
4096 // Find the minimum of remaining characters to move and preloaded characters
4097 // and write those as bytes.
4098 __ add(scratch5, scratch5, Operand(4), SetCC);
4099 __ b(eq, &done);
4100 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
4101 // Move minimum of bytes read and bytes left to copy to scratch4.
4102 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
4103 // Between one and three (value in scratch5) characters already read into
4104 // scratch ready to write.
4105 __ cmp(scratch5, Operand(2));
4106 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4107 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
4108 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
4109 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
4110 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
4111 // Copy any remaining bytes.
4112 __ b(&byte_loop);
4113
4114 // Simple loop.
4115 // Copy words from src to dst, until less than four bytes left.
4116 // Both src and dest are word aligned.
4117 __ bind(&simple_loop);
4118 {
4119 Label loop;
4120 __ bind(&loop);
4121 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
4122 __ sub(scratch3, limit, Operand(dest));
4123 __ str(scratch1, MemOperand(dest, 4, PostIndex));
4124 // Compare to 8, not 4, because we do the substraction before increasing
4125 // dest.
4126 __ cmp(scratch3, Operand(8));
4127 __ b(ge, &loop);
4128 }
4129
4130 // Copy bytes from src to dst until dst hits limit.
4131 __ bind(&byte_loop);
4132 __ cmp(dest, Operand(limit));
4133 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
4134 __ b(ge, &done);
4135 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4136 __ b(&byte_loop);
4137
4138 __ bind(&done);
4139}
4140
4141
4142void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4143 Register c1,
4144 Register c2,
4145 Register scratch1,
4146 Register scratch2,
4147 Register scratch3,
4148 Register scratch4,
4149 Register scratch5,
4150 Label* not_found) {
4151 // Register scratch3 is the general scratch register in this function.
4152 Register scratch = scratch3;
4153
4154 // Make sure that both characters are not digits as such strings has a
4155 // different hash algorithm. Don't try to look for these in the symbol table.
4156 Label not_array_index;
4157 __ sub(scratch, c1, Operand(static_cast<int>('0')));
4158 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
4159 __ b(hi, &not_array_index);
4160 __ sub(scratch, c2, Operand(static_cast<int>('0')));
4161 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
4162
4163 // If check failed combine both characters into single halfword.
4164 // This is required by the contract of the method: code at the
4165 // not_found branch expects this combination in c1 register
4166 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
4167 __ b(ls, not_found);
4168
4169 __ bind(&not_array_index);
4170 // Calculate the two character string hash.
4171 Register hash = scratch1;
4172 StringHelper::GenerateHashInit(masm, hash, c1);
4173 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
4174 StringHelper::GenerateHashGetHash(masm, hash);
4175
4176 // Collect the two characters in a register.
4177 Register chars = c1;
4178 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
4179
4180 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4181 // hash: hash of two character string.
4182
4183 // Load symbol table
4184 // Load address of first element of the symbol table.
4185 Register symbol_table = c2;
4186 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
4187
4188 // Load undefined value
4189 Register undefined = scratch4;
4190 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
4191
4192 // Calculate capacity mask from the symbol table capacity.
4193 Register mask = scratch2;
4194 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
4195 __ mov(mask, Operand(mask, ASR, 1));
4196 __ sub(mask, mask, Operand(1));
4197
4198 // Calculate untagged address of the first element of the symbol table.
4199 Register first_symbol_table_element = symbol_table;
4200 __ add(first_symbol_table_element, symbol_table,
4201 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
4202
4203 // Registers
4204 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4205 // hash: hash of two character string
4206 // mask: capacity mask
4207 // first_symbol_table_element: address of the first element of
4208 // the symbol table
4209 // scratch: -
4210
4211 // Perform a number of probes in the symbol table.
4212 static const int kProbes = 4;
4213 Label found_in_symbol_table;
4214 Label next_probe[kProbes];
4215 for (int i = 0; i < kProbes; i++) {
4216 Register candidate = scratch5; // Scratch register contains candidate.
4217
4218 // Calculate entry in symbol table.
4219 if (i > 0) {
4220 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
4221 } else {
4222 __ mov(candidate, hash);
4223 }
4224
4225 __ and_(candidate, candidate, Operand(mask));
4226
4227 // Load the entry from the symble table.
4228 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4229 __ ldr(candidate,
4230 MemOperand(first_symbol_table_element,
4231 candidate,
4232 LSL,
4233 kPointerSizeLog2));
4234
4235 // If entry is undefined no string with this hash can be found.
4236 __ cmp(candidate, undefined);
4237 __ b(eq, not_found);
4238
4239 // If length is not 2 the string is not a candidate.
4240 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
4241 __ cmp(scratch, Operand(Smi::FromInt(2)));
4242 __ b(ne, &next_probe[i]);
4243
4244 // Check that the candidate is a non-external ascii string.
4245 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
4246 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
4247 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
4248 &next_probe[i]);
4249
4250 // Check if the two characters match.
4251 // Assumes that word load is little endian.
4252 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
4253 __ cmp(chars, scratch);
4254 __ b(eq, &found_in_symbol_table);
4255 __ bind(&next_probe[i]);
4256 }
4257
4258 // No matching 2 character string found by probing.
4259 __ jmp(not_found);
4260
4261 // Scratch register contains result when we fall through to here.
4262 Register result = scratch;
4263 __ bind(&found_in_symbol_table);
4264 __ Move(r0, result);
4265}
4266
4267
4268void StringHelper::GenerateHashInit(MacroAssembler* masm,
4269 Register hash,
4270 Register character) {
4271 // hash = character + (character << 10);
4272 __ add(hash, character, Operand(character, LSL, 10));
4273 // hash ^= hash >> 6;
4274 __ eor(hash, hash, Operand(hash, ASR, 6));
4275}
4276
4277
4278void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4279 Register hash,
4280 Register character) {
4281 // hash += character;
4282 __ add(hash, hash, Operand(character));
4283 // hash += hash << 10;
4284 __ add(hash, hash, Operand(hash, LSL, 10));
4285 // hash ^= hash >> 6;
4286 __ eor(hash, hash, Operand(hash, ASR, 6));
4287}
4288
4289
4290void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4291 Register hash) {
4292 // hash += hash << 3;
4293 __ add(hash, hash, Operand(hash, LSL, 3));
4294 // hash ^= hash >> 11;
4295 __ eor(hash, hash, Operand(hash, ASR, 11));
4296 // hash += hash << 15;
4297 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
4298
4299 // if (hash == 0) hash = 27;
4300 __ mov(hash, Operand(27), LeaveCC, nz);
4301}
4302
4303
4304void SubStringStub::Generate(MacroAssembler* masm) {
4305 Label runtime;
4306
4307 // Stack frame on entry.
4308 // lr: return address
4309 // sp[0]: to
4310 // sp[4]: from
4311 // sp[8]: string
4312
4313 // This stub is called from the native-call %_SubString(...), so
4314 // nothing can be assumed about the arguments. It is tested that:
4315 // "string" is a sequential string,
4316 // both "from" and "to" are smis, and
4317 // 0 <= from <= to <= string.length.
4318 // If any of these assumptions fail, we call the runtime system.
4319
4320 static const int kToOffset = 0 * kPointerSize;
4321 static const int kFromOffset = 1 * kPointerSize;
4322 static const int kStringOffset = 2 * kPointerSize;
4323
4324
4325 // Check bounds and smi-ness.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004326 Register to = r6;
4327 Register from = r7;
4328 __ Ldrd(to, from, MemOperand(sp, kToOffset));
4329 STATIC_ASSERT(kFromOffset == kToOffset + 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004330 STATIC_ASSERT(kSmiTag == 0);
4331 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4332 // I.e., arithmetic shift right by one un-smi-tags.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004333 __ mov(r2, Operand(to, ASR, 1), SetCC);
4334 __ mov(r3, Operand(from, ASR, 1), SetCC, cc);
4335 // If either to or from had the smi tag bit set, then carry is set now.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004336 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
4337 __ b(mi, &runtime); // From is negative.
4338
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004339 // Both to and from are smis.
4340
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004341 __ sub(r2, r2, Operand(r3), SetCC);
4342 __ b(mi, &runtime); // Fail if from > to.
4343 // Special handling of sub-strings of length 1 and 2. One character strings
4344 // are handled in the runtime system (looked up in the single character
4345 // cache). Two character strings are looked for in the symbol cache.
4346 __ cmp(r2, Operand(2));
4347 __ b(lt, &runtime);
4348
4349 // r2: length
4350 // r3: from index (untaged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004351 // r6 (a.k.a. to): to (smi)
4352 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004353
4354 // Make sure first argument is a sequential (or flat) string.
4355 __ ldr(r5, MemOperand(sp, kStringOffset));
4356 STATIC_ASSERT(kSmiTag == 0);
4357 __ tst(r5, Operand(kSmiTagMask));
4358 __ b(eq, &runtime);
4359 Condition is_string = masm->IsObjectStringType(r5, r1);
4360 __ b(NegateCondition(is_string), &runtime);
4361
4362 // r1: instance type
4363 // r2: length
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004364 // r3: from index (untagged smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004365 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004366 // r6 (a.k.a. to): to (smi)
4367 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004368 Label seq_string;
4369 __ and_(r4, r1, Operand(kStringRepresentationMask));
4370 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
4371 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
4372 __ cmp(r4, Operand(kConsStringTag));
4373 __ b(gt, &runtime); // External strings go to runtime.
4374 __ b(lt, &seq_string); // Sequential strings are handled directly.
4375
4376 // Cons string. Try to recurse (once) on the first substring.
4377 // (This adds a little more generality than necessary to handle flattened
4378 // cons strings, but not much).
4379 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
4380 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
4381 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4382 __ tst(r1, Operand(kStringRepresentationMask));
4383 STATIC_ASSERT(kSeqStringTag == 0);
4384 __ b(ne, &runtime); // Cons and External strings go to runtime.
4385
4386 // Definitly a sequential string.
4387 __ bind(&seq_string);
4388
4389 // r1: instance type.
4390 // r2: length
4391 // r3: from index (untaged smi)
4392 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004393 // r6 (a.k.a. to): to (smi)
4394 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004395 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004396 __ cmp(r4, Operand(to));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004397 __ b(lt, &runtime); // Fail if to > length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004398 to = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004399
4400 // r1: instance type.
4401 // r2: result string length.
4402 // r3: from index (untaged smi)
4403 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004404 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004405 // Check for flat ascii string.
4406 Label non_ascii_flat;
4407 __ tst(r1, Operand(kStringEncodingMask));
4408 STATIC_ASSERT(kTwoByteStringTag == 0);
4409 __ b(eq, &non_ascii_flat);
4410
4411 Label result_longer_than_two;
4412 __ cmp(r2, Operand(2));
4413 __ b(gt, &result_longer_than_two);
4414
4415 // Sub string of length 2 requested.
4416 // Get the two characters forming the sub string.
4417 __ add(r5, r5, Operand(r3));
4418 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
4419 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
4420
4421 // Try to lookup two character string in symbol table.
4422 Label make_two_character_string;
4423 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4424 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
4425 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4426 __ add(sp, sp, Operand(3 * kPointerSize));
4427 __ Ret();
4428
4429 // r2: result string length.
4430 // r3: two characters combined into halfword in little endian byte order.
4431 __ bind(&make_two_character_string);
4432 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
4433 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4434 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4435 __ add(sp, sp, Operand(3 * kPointerSize));
4436 __ Ret();
4437
4438 __ bind(&result_longer_than_two);
4439
4440 // Allocate the result.
4441 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
4442
4443 // r0: result string.
4444 // r2: result string length.
4445 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004446 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004447 // Locate first character of result.
4448 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4449 // Locate 'from' character of string.
4450 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004451 __ add(r5, r5, Operand(from, ASR, 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004452
4453 // r0: result string.
4454 // r1: first character of result string.
4455 // r2: result string length.
4456 // r5: first character of sub string to copy.
4457 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
4458 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
4459 COPY_ASCII | DEST_ALWAYS_ALIGNED);
4460 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4461 __ add(sp, sp, Operand(3 * kPointerSize));
4462 __ Ret();
4463
4464 __ bind(&non_ascii_flat);
4465 // r2: result string length.
4466 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004467 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004468 // Check for flat two byte string.
4469
4470 // Allocate the result.
4471 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
4472
4473 // r0: result string.
4474 // r2: result string length.
4475 // r5: string.
4476 // Locate first character of result.
4477 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4478 // Locate 'from' character of string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004479 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004480 // As "from" is a smi it is 2 times the value which matches the size of a two
4481 // byte character.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004482 __ add(r5, r5, Operand(from));
4483 from = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004484
4485 // r0: result string.
4486 // r1: first character of result.
4487 // r2: result length.
4488 // r5: first character of string to copy.
4489 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004490 StringHelper::GenerateCopyCharactersLong(
4491 masm, r1, r5, r2, r3, r4, r6, r7, r9, DEST_ALWAYS_ALIGNED);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004492 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4493 __ add(sp, sp, Operand(3 * kPointerSize));
4494 __ Ret();
4495
4496 // Just jump to runtime to create the sub string.
4497 __ bind(&runtime);
4498 __ TailCallRuntime(Runtime::kSubString, 3, 1);
4499}
4500
4501
4502void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
4503 Register left,
4504 Register right,
4505 Register scratch1,
4506 Register scratch2,
4507 Register scratch3,
4508 Register scratch4) {
4509 Label compare_lengths;
4510 // Find minimum length and length difference.
4511 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
4512 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
4513 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
4514 Register length_delta = scratch3;
4515 __ mov(scratch1, scratch2, LeaveCC, gt);
4516 Register min_length = scratch1;
4517 STATIC_ASSERT(kSmiTag == 0);
4518 __ tst(min_length, Operand(min_length));
4519 __ b(eq, &compare_lengths);
4520
4521 // Untag smi.
4522 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
4523
4524 // Setup registers so that we only need to increment one register
4525 // in the loop.
4526 __ add(scratch2, min_length,
4527 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4528 __ add(left, left, Operand(scratch2));
4529 __ add(right, right, Operand(scratch2));
4530 // Registers left and right points to the min_length character of strings.
4531 __ rsb(min_length, min_length, Operand(-1));
4532 Register index = min_length;
4533 // Index starts at -min_length.
4534
4535 {
4536 // Compare loop.
4537 Label loop;
4538 __ bind(&loop);
4539 // Compare characters.
4540 __ add(index, index, Operand(1), SetCC);
4541 __ ldrb(scratch2, MemOperand(left, index), ne);
4542 __ ldrb(scratch4, MemOperand(right, index), ne);
4543 // Skip to compare lengths with eq condition true.
4544 __ b(eq, &compare_lengths);
4545 __ cmp(scratch2, scratch4);
4546 __ b(eq, &loop);
4547 // Fallthrough with eq condition false.
4548 }
4549 // Compare lengths - strings up to min-length are equal.
4550 __ bind(&compare_lengths);
4551 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
4552 // Use zero length_delta as result.
4553 __ mov(r0, Operand(length_delta), SetCC, eq);
4554 // Fall through to here if characters compare not-equal.
4555 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
4556 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
4557 __ Ret();
4558}
4559
4560
4561void StringCompareStub::Generate(MacroAssembler* masm) {
4562 Label runtime;
4563
4564 // Stack frame on entry.
4565 // sp[0]: right string
4566 // sp[4]: left string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004567 __ Ldrd(r0 , r1, MemOperand(sp)); // Load right in r0, left in r1.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004568
4569 Label not_same;
4570 __ cmp(r0, r1);
4571 __ b(ne, &not_same);
4572 STATIC_ASSERT(EQUAL == 0);
4573 STATIC_ASSERT(kSmiTag == 0);
4574 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
4575 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
4576 __ add(sp, sp, Operand(2 * kPointerSize));
4577 __ Ret();
4578
4579 __ bind(&not_same);
4580
4581 // Check that both objects are sequential ascii strings.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004582 __ JumpIfNotBothSequentialAsciiStrings(r1, r0, r2, r3, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004583
4584 // Compare flat ascii strings natively. Remove arguments from stack first.
4585 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
4586 __ add(sp, sp, Operand(2 * kPointerSize));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004587 GenerateCompareFlatAsciiStrings(masm, r1, r0, r2, r3, r4, r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004588
4589 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
4590 // tagged as a small integer.
4591 __ bind(&runtime);
4592 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
4593}
4594
4595
4596void StringAddStub::Generate(MacroAssembler* masm) {
4597 Label string_add_runtime;
4598 // Stack on entry:
4599 // sp[0]: second argument.
4600 // sp[4]: first argument.
4601
4602 // Load the two arguments.
4603 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
4604 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
4605
4606 // Make sure that both arguments are strings if not known in advance.
4607 if (string_check_) {
4608 STATIC_ASSERT(kSmiTag == 0);
4609 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
4610 // Load instance types.
4611 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4612 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4613 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4614 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4615 STATIC_ASSERT(kStringTag == 0);
4616 // If either is not a string, go to runtime.
4617 __ tst(r4, Operand(kIsNotStringMask));
4618 __ tst(r5, Operand(kIsNotStringMask), eq);
4619 __ b(ne, &string_add_runtime);
4620 }
4621
4622 // Both arguments are strings.
4623 // r0: first string
4624 // r1: second string
4625 // r4: first string instance type (if string_check_)
4626 // r5: second string instance type (if string_check_)
4627 {
4628 Label strings_not_empty;
4629 // Check if either of the strings are empty. In that case return the other.
4630 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
4631 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
4632 STATIC_ASSERT(kSmiTag == 0);
4633 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
4634 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
4635 STATIC_ASSERT(kSmiTag == 0);
4636 // Else test if second string is empty.
4637 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
4638 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
4639
4640 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4641 __ add(sp, sp, Operand(2 * kPointerSize));
4642 __ Ret();
4643
4644 __ bind(&strings_not_empty);
4645 }
4646
4647 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
4648 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
4649 // Both strings are non-empty.
4650 // r0: first string
4651 // r1: second string
4652 // r2: length of first string
4653 // r3: length of second string
4654 // r4: first string instance type (if string_check_)
4655 // r5: second string instance type (if string_check_)
4656 // Look at the length of the result of adding the two strings.
4657 Label string_add_flat_result, longer_than_two;
4658 // Adding two lengths can't overflow.
4659 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
4660 __ add(r6, r2, Operand(r3));
4661 // Use the runtime system when adding two one character strings, as it
4662 // contains optimizations for this specific case using the symbol table.
4663 __ cmp(r6, Operand(2));
4664 __ b(ne, &longer_than_two);
4665
4666 // Check that both strings are non-external ascii strings.
4667 if (!string_check_) {
4668 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4669 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4670 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4671 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4672 }
4673 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
4674 &string_add_runtime);
4675
4676 // Get the two characters forming the sub string.
4677 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4678 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
4679
4680 // Try to lookup two character string in symbol table. If it is not found
4681 // just allocate a new one.
4682 Label make_two_character_string;
4683 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4684 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
4685 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4686 __ add(sp, sp, Operand(2 * kPointerSize));
4687 __ Ret();
4688
4689 __ bind(&make_two_character_string);
4690 // Resulting string has length 2 and first chars of two strings
4691 // are combined into single halfword in r2 register.
4692 // So we can fill resulting string without two loops by a single
4693 // halfword store instruction (which assumes that processor is
4694 // in a little endian mode)
4695 __ mov(r6, Operand(2));
4696 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
4697 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4698 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4699 __ add(sp, sp, Operand(2 * kPointerSize));
4700 __ Ret();
4701
4702 __ bind(&longer_than_two);
4703 // Check if resulting string will be flat.
4704 __ cmp(r6, Operand(String::kMinNonFlatLength));
4705 __ b(lt, &string_add_flat_result);
4706 // Handle exceptionally long strings in the runtime system.
4707 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
4708 ASSERT(IsPowerOf2(String::kMaxLength + 1));
4709 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
4710 __ cmp(r6, Operand(String::kMaxLength + 1));
4711 __ b(hs, &string_add_runtime);
4712
4713 // If result is not supposed to be flat, allocate a cons string object.
4714 // If both strings are ascii the result is an ascii cons string.
4715 if (!string_check_) {
4716 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4717 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4718 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4719 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4720 }
4721 Label non_ascii, allocated, ascii_data;
4722 STATIC_ASSERT(kTwoByteStringTag == 0);
4723 __ tst(r4, Operand(kStringEncodingMask));
4724 __ tst(r5, Operand(kStringEncodingMask), ne);
4725 __ b(eq, &non_ascii);
4726
4727 // Allocate an ASCII cons string.
4728 __ bind(&ascii_data);
4729 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
4730 __ bind(&allocated);
4731 // Fill the fields of the cons string.
4732 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
4733 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
4734 __ mov(r0, Operand(r7));
4735 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4736 __ add(sp, sp, Operand(2 * kPointerSize));
4737 __ Ret();
4738
4739 __ bind(&non_ascii);
4740 // At least one of the strings is two-byte. Check whether it happens
4741 // to contain only ascii characters.
4742 // r4: first instance type.
4743 // r5: second instance type.
4744 __ tst(r4, Operand(kAsciiDataHintMask));
4745 __ tst(r5, Operand(kAsciiDataHintMask), ne);
4746 __ b(ne, &ascii_data);
4747 __ eor(r4, r4, Operand(r5));
4748 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4749 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
4750 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
4751 __ b(eq, &ascii_data);
4752
4753 // Allocate a two byte cons string.
4754 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
4755 __ jmp(&allocated);
4756
4757 // Handle creating a flat result. First check that both strings are
4758 // sequential and that they have the same encoding.
4759 // r0: first string
4760 // r1: second string
4761 // r2: length of first string
4762 // r3: length of second string
4763 // r4: first string instance type (if string_check_)
4764 // r5: second string instance type (if string_check_)
4765 // r6: sum of lengths.
4766 __ bind(&string_add_flat_result);
4767 if (!string_check_) {
4768 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4769 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4770 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4771 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4772 }
4773 // Check that both strings are sequential.
4774 STATIC_ASSERT(kSeqStringTag == 0);
4775 __ tst(r4, Operand(kStringRepresentationMask));
4776 __ tst(r5, Operand(kStringRepresentationMask), eq);
4777 __ b(ne, &string_add_runtime);
4778 // Now check if both strings have the same encoding (ASCII/Two-byte).
4779 // r0: first string.
4780 // r1: second string.
4781 // r2: length of first string.
4782 // r3: length of second string.
4783 // r6: sum of lengths..
4784 Label non_ascii_string_add_flat_result;
4785 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
4786 __ eor(r7, r4, Operand(r5));
4787 __ tst(r7, Operand(kStringEncodingMask));
4788 __ b(ne, &string_add_runtime);
4789 // And see if it's ASCII or two-byte.
4790 __ tst(r4, Operand(kStringEncodingMask));
4791 __ b(eq, &non_ascii_string_add_flat_result);
4792
4793 // Both strings are sequential ASCII strings. We also know that they are
4794 // short (since the sum of the lengths is less than kMinNonFlatLength).
4795 // r6: length of resulting flat string
4796 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
4797 // Locate first character of result.
4798 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4799 // Locate first character of first argument.
4800 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4801 // r0: first character of first string.
4802 // r1: second string.
4803 // r2: length of first string.
4804 // r3: length of second string.
4805 // r6: first character of result.
4806 // r7: result string.
4807 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
4808
4809 // Load second argument and locate first character.
4810 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4811 // r1: first character of second string.
4812 // r3: length of second string.
4813 // r6: next character of result.
4814 // r7: result string.
4815 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
4816 __ mov(r0, Operand(r7));
4817 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4818 __ add(sp, sp, Operand(2 * kPointerSize));
4819 __ Ret();
4820
4821 __ bind(&non_ascii_string_add_flat_result);
4822 // Both strings are sequential two byte strings.
4823 // r0: first string.
4824 // r1: second string.
4825 // r2: length of first string.
4826 // r3: length of second string.
4827 // r6: sum of length of strings.
4828 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
4829 // r0: first string.
4830 // r1: second string.
4831 // r2: length of first string.
4832 // r3: length of second string.
4833 // r7: result string.
4834
4835 // Locate first character of result.
4836 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4837 // Locate first character of first argument.
4838 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4839
4840 // r0: first character of first string.
4841 // r1: second string.
4842 // r2: length of first string.
4843 // r3: length of second string.
4844 // r6: first character of result.
4845 // r7: result string.
4846 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
4847
4848 // Locate first character of second argument.
4849 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4850
4851 // r1: first character of second string.
4852 // r3: length of second string.
4853 // r6: next character of result (after copy of first string).
4854 // r7: result string.
4855 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
4856
4857 __ mov(r0, Operand(r7));
4858 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4859 __ add(sp, sp, Operand(2 * kPointerSize));
4860 __ Ret();
4861
4862 // Just jump to runtime to add the two strings.
4863 __ bind(&string_add_runtime);
4864 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
4865}
4866
4867
Ben Murdochb0fe1622011-05-05 13:52:32 +01004868void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
4869 ASSERT(state_ == CompareIC::SMIS);
4870 Label miss;
4871 __ orr(r2, r1, r0);
4872 __ tst(r2, Operand(kSmiTagMask));
4873 __ b(ne, &miss);
4874
4875 if (GetCondition() == eq) {
4876 // For equality we do not care about the sign of the result.
4877 __ sub(r0, r0, r1, SetCC);
4878 } else {
4879 __ sub(r1, r1, r0, SetCC);
4880 // Correct sign of result in case of overflow.
4881 __ rsb(r1, r1, Operand(0), SetCC, vs);
4882 __ mov(r0, r1);
4883 }
4884 __ Ret();
4885
4886 __ bind(&miss);
4887 GenerateMiss(masm);
4888}
4889
4890
4891void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
4892 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
4893
4894 Label generic_stub;
4895 Label unordered;
4896 Label miss;
4897 __ and_(r2, r1, Operand(r0));
4898 __ tst(r2, Operand(kSmiTagMask));
4899 __ b(eq, &generic_stub);
4900
4901 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4902 __ b(ne, &miss);
4903 __ CompareObjectType(r1, r2, r2, HEAP_NUMBER_TYPE);
4904 __ b(ne, &miss);
4905
4906 // Inlining the double comparison and falling back to the general compare
4907 // stub if NaN is involved or VFP3 is unsupported.
4908 if (CpuFeatures::IsSupported(VFP3)) {
4909 CpuFeatures::Scope scope(VFP3);
4910
4911 // Load left and right operand
4912 __ sub(r2, r1, Operand(kHeapObjectTag));
4913 __ vldr(d0, r2, HeapNumber::kValueOffset);
4914 __ sub(r2, r0, Operand(kHeapObjectTag));
4915 __ vldr(d1, r2, HeapNumber::kValueOffset);
4916
4917 // Compare operands
4918 __ vcmp(d0, d1);
4919 __ vmrs(pc); // Move vector status bits to normal status bits.
4920
4921 // Don't base result on status bits when a NaN is involved.
4922 __ b(vs, &unordered);
4923
4924 // Return a result of -1, 0, or 1, based on status bits.
4925 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
4926 __ mov(r0, Operand(LESS), LeaveCC, lt);
4927 __ mov(r0, Operand(GREATER), LeaveCC, gt);
4928 __ Ret();
4929
4930 __ bind(&unordered);
4931 }
4932
4933 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS, r1, r0);
4934 __ bind(&generic_stub);
4935 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4936
4937 __ bind(&miss);
4938 GenerateMiss(masm);
4939}
4940
4941
4942void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
4943 ASSERT(state_ == CompareIC::OBJECTS);
4944 Label miss;
4945 __ and_(r2, r1, Operand(r0));
4946 __ tst(r2, Operand(kSmiTagMask));
4947 __ b(eq, &miss);
4948
4949 __ CompareObjectType(r0, r2, r2, JS_OBJECT_TYPE);
4950 __ b(ne, &miss);
4951 __ CompareObjectType(r1, r2, r2, JS_OBJECT_TYPE);
4952 __ b(ne, &miss);
4953
4954 ASSERT(GetCondition() == eq);
4955 __ sub(r0, r0, Operand(r1));
4956 __ Ret();
4957
4958 __ bind(&miss);
4959 GenerateMiss(masm);
4960}
4961
4962
4963void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
4964 __ Push(r1, r0);
4965 __ push(lr);
4966
4967 // Call the runtime system in a fresh internal frame.
4968 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss));
4969 __ EnterInternalFrame();
4970 __ Push(r1, r0);
4971 __ mov(ip, Operand(Smi::FromInt(op_)));
4972 __ push(ip);
4973 __ CallExternalReference(miss, 3);
4974 __ LeaveInternalFrame();
4975 // Compute the entry point of the rewritten stub.
4976 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
4977 // Restore registers.
4978 __ pop(lr);
4979 __ pop(r0);
4980 __ pop(r1);
4981 __ Jump(r2);
4982}
4983
4984
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004985#undef __
4986
4987} } // namespace v8::internal
4988
4989#endif // V8_TARGET_ARCH_ARM