blob: 5ec8584f9ed9565a70edcec8561dc8a076a077fc [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
920void RecordWriteStub::Generate(MacroAssembler* masm) {
921 __ add(offset_, object_, Operand(offset_));
922 __ RecordWriteHelper(object_, offset_, scratch_);
923 __ Ret();
924}
925
926
927// On entry lhs_ and rhs_ are the values to be compared.
928// On exit r0 is 0, positive or negative to indicate the result of
929// the comparison.
930void CompareStub::Generate(MacroAssembler* masm) {
931 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
932 (lhs_.is(r1) && rhs_.is(r0)));
933
934 Label slow; // Call builtin.
935 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
936
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100937 if (include_smi_compare_) {
938 Label not_two_smis, smi_done;
939 __ orr(r2, r1, r0);
940 __ tst(r2, Operand(kSmiTagMask));
941 __ b(ne, &not_two_smis);
Ben Murdochf87a2032010-10-22 12:50:53 +0100942 __ mov(r1, Operand(r1, ASR, 1));
943 __ sub(r0, r1, Operand(r0, ASR, 1));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100944 __ Ret();
945 __ bind(&not_two_smis);
946 } else if (FLAG_debug_code) {
947 __ orr(r2, r1, r0);
948 __ tst(r2, Operand(kSmiTagMask));
949 __ Assert(nz, "CompareStub: unexpected smi operands.");
950 }
951
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100952 // NOTICE! This code is only reached after a smi-fast-case check, so
953 // it is certain that at least one operand isn't a smi.
954
955 // Handle the case where the objects are identical. Either returns the answer
956 // or goes to slow. Only falls through if the objects were not identical.
957 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
958
959 // If either is a Smi (we know that not both are), then they can only
960 // be strictly equal if the other is a HeapNumber.
961 STATIC_ASSERT(kSmiTag == 0);
962 ASSERT_EQ(0, Smi::FromInt(0));
963 __ and_(r2, lhs_, Operand(rhs_));
964 __ tst(r2, Operand(kSmiTagMask));
965 __ b(ne, &not_smis);
966 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
967 // 1) Return the answer.
968 // 2) Go to slow.
969 // 3) Fall through to both_loaded_as_doubles.
970 // 4) Jump to lhs_not_nan.
971 // In cases 3 and 4 we have found out we were dealing with a number-number
972 // comparison. If VFP3 is supported the double values of the numbers have
973 // been loaded into d7 and d6. Otherwise, the double values have been loaded
974 // into r0, r1, r2, and r3.
975 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
976
977 __ bind(&both_loaded_as_doubles);
978 // The arguments have been converted to doubles and stored in d6 and d7, if
979 // VFP3 is supported, or in r0, r1, r2, and r3.
980 if (CpuFeatures::IsSupported(VFP3)) {
981 __ bind(&lhs_not_nan);
982 CpuFeatures::Scope scope(VFP3);
983 Label no_nan;
984 // ARMv7 VFP3 instructions to implement double precision comparison.
985 __ vcmp(d7, d6);
986 __ vmrs(pc); // Move vector status bits to normal status bits.
987 Label nan;
988 __ b(vs, &nan);
989 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
990 __ mov(r0, Operand(LESS), LeaveCC, lt);
991 __ mov(r0, Operand(GREATER), LeaveCC, gt);
992 __ Ret();
993
994 __ bind(&nan);
995 // If one of the sides was a NaN then the v flag is set. Load r0 with
996 // whatever it takes to make the comparison fail, since comparisons with NaN
997 // always fail.
998 if (cc_ == lt || cc_ == le) {
999 __ mov(r0, Operand(GREATER));
1000 } else {
1001 __ mov(r0, Operand(LESS));
1002 }
1003 __ Ret();
1004 } else {
1005 // Checks for NaN in the doubles we have loaded. Can return the answer or
1006 // fall through if neither is a NaN. Also binds lhs_not_nan.
1007 EmitNanCheck(masm, &lhs_not_nan, cc_);
1008 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
1009 // answer. Never falls through.
1010 EmitTwoNonNanDoubleComparison(masm, cc_);
1011 }
1012
1013 __ bind(&not_smis);
1014 // At this point we know we are dealing with two different objects,
1015 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
1016 if (strict_) {
1017 // This returns non-equal for some object types, or falls through if it
1018 // was not lucky.
1019 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1020 }
1021
1022 Label check_for_symbols;
1023 Label flat_string_check;
1024 // Check for heap-number-heap-number comparison. Can jump to slow case,
1025 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
1026 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1027 // In this case r2 will contain the type of rhs_. Never falls through.
1028 EmitCheckForTwoHeapNumbers(masm,
1029 lhs_,
1030 rhs_,
1031 &both_loaded_as_doubles,
1032 &check_for_symbols,
1033 &flat_string_check);
1034
1035 __ bind(&check_for_symbols);
1036 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
1037 // symbols.
1038 if (cc_ == eq && !strict_) {
1039 // Returns an answer for two symbols or two detectable objects.
1040 // Otherwise jumps to string case or not both strings case.
1041 // Assumes that r2 is the type of rhs_ on entry.
1042 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1043 }
1044
1045 // Check for both being sequential ASCII strings, and inline if that is the
1046 // case.
1047 __ bind(&flat_string_check);
1048
1049 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
1050
1051 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
1052 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1053 lhs_,
1054 rhs_,
1055 r2,
1056 r3,
1057 r4,
1058 r5);
1059 // Never falls through to here.
1060
1061 __ bind(&slow);
1062
1063 __ Push(lhs_, rhs_);
1064 // Figure out which native to call and setup the arguments.
1065 Builtins::JavaScript native;
1066 if (cc_ == eq) {
1067 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1068 } else {
1069 native = Builtins::COMPARE;
1070 int ncr; // NaN compare result
1071 if (cc_ == lt || cc_ == le) {
1072 ncr = GREATER;
1073 } else {
1074 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
1075 ncr = LESS;
1076 }
1077 __ mov(r0, Operand(Smi::FromInt(ncr)));
1078 __ push(r0);
1079 }
1080
1081 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1082 // tagged as a small integer.
1083 __ InvokeBuiltin(native, JUMP_JS);
1084}
1085
1086
1087// This stub does not handle the inlined cases (Smis, Booleans, undefined).
1088// The stub returns zero for false, and a non-zero value for true.
1089void ToBooleanStub::Generate(MacroAssembler* masm) {
1090 Label false_result;
1091 Label not_heap_number;
1092 Register scratch = r7;
1093
Ben Murdochb0fe1622011-05-05 13:52:32 +01001094 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1095 __ cmp(tos_, ip);
1096 __ b(eq, &false_result);
1097
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001098 // HeapNumber => false iff +0, -0, or NaN.
1099 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1100 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
1101 __ cmp(scratch, ip);
1102 __ b(&not_heap_number, ne);
1103
1104 __ sub(ip, tos_, Operand(kHeapObjectTag));
1105 __ vldr(d1, ip, HeapNumber::kValueOffset);
1106 __ vcmp(d1, 0.0);
1107 __ vmrs(pc);
1108 // "tos_" is a register, and contains a non zero value by default.
1109 // Hence we only need to overwrite "tos_" with zero to return false for
1110 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001111 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, eq); // for FP_ZERO
1112 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, vs); // for FP_NAN
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001113 __ Ret();
1114
1115 __ bind(&not_heap_number);
1116
1117 // Check if the value is 'null'.
1118 // 'null' => false.
1119 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1120 __ cmp(tos_, ip);
1121 __ b(&false_result, eq);
1122
1123 // It can be an undetectable object.
1124 // Undetectable => false.
1125 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
1126 __ ldrb(scratch, FieldMemOperand(ip, Map::kBitFieldOffset));
1127 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
1128 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
1129 __ b(&false_result, eq);
1130
1131 // JavaScript object => true.
1132 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1133 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1134 __ cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
1135 // "tos_" is a register and contains a non-zero value.
1136 // Hence we implicitly return true if the greater than
1137 // condition is satisfied.
1138 __ Ret(gt);
1139
1140 // Check for string
1141 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1142 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1143 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
1144 // "tos_" is a register and contains a non-zero value.
1145 // Hence we implicitly return true if the greater than
1146 // condition is satisfied.
1147 __ Ret(gt);
1148
1149 // String value => false iff empty, i.e., length is zero
1150 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
1151 // If length is zero, "tos_" contains zero ==> false.
1152 // If length is not zero, "tos_" contains a non-zero value ==> true.
1153 __ Ret();
1154
1155 // Return 0 in "tos_" for false .
1156 __ bind(&false_result);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001157 __ mov(tos_, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001158 __ Ret();
1159}
1160
1161
1162// We fall into this code if the operands were Smis, but the result was
1163// not (eg. overflow). We branch into this code (to the not_smi label) if
1164// the operands were not both Smi. The operands are in r0 and r1. In order
1165// to call the C-implemented binary fp operation routines we need to end up
1166// with the double precision floating point operands in r0 and r1 (for the
1167// value in r1) and r2 and r3 (for the value in r0).
1168void GenericBinaryOpStub::HandleBinaryOpSlowCases(
1169 MacroAssembler* masm,
1170 Label* not_smi,
1171 Register lhs,
1172 Register rhs,
1173 const Builtins::JavaScript& builtin) {
1174 Label slow, slow_reverse, do_the_call;
1175 bool use_fp_registers = CpuFeatures::IsSupported(VFP3) && Token::MOD != op_;
1176
1177 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
1178 Register heap_number_map = r6;
1179
1180 if (ShouldGenerateSmiCode()) {
1181 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1182
1183 // Smi-smi case (overflow).
1184 // Since both are Smis there is no heap number to overwrite, so allocate.
1185 // The new heap number is in r5. r3 and r7 are scratch.
1186 __ AllocateHeapNumber(
1187 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
1188
1189 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
1190 // using registers d7 and d6 for the double values.
1191 if (CpuFeatures::IsSupported(VFP3)) {
1192 CpuFeatures::Scope scope(VFP3);
1193 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
1194 __ vmov(s15, r7);
1195 __ vcvt_f64_s32(d7, s15);
1196 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
1197 __ vmov(s13, r7);
1198 __ vcvt_f64_s32(d6, s13);
1199 if (!use_fp_registers) {
1200 __ vmov(r2, r3, d7);
1201 __ vmov(r0, r1, d6);
1202 }
1203 } else {
1204 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
1205 __ mov(r7, Operand(rhs));
1206 ConvertToDoubleStub stub1(r3, r2, r7, r9);
1207 __ push(lr);
1208 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
1209 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
1210 __ mov(r7, Operand(lhs));
1211 ConvertToDoubleStub stub2(r1, r0, r7, r9);
1212 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
1213 __ pop(lr);
1214 }
1215 __ jmp(&do_the_call); // Tail call. No return.
1216 }
1217
1218 // We branch here if at least one of r0 and r1 is not a Smi.
1219 __ bind(not_smi);
1220 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1221
1222 // After this point we have the left hand side in r1 and the right hand side
1223 // in r0.
1224 if (lhs.is(r0)) {
1225 __ Swap(r0, r1, ip);
1226 }
1227
1228 // The type transition also calculates the answer.
1229 bool generate_code_to_calculate_answer = true;
1230
1231 if (ShouldGenerateFPCode()) {
1232 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
1233 switch (op_) {
1234 case Token::ADD:
1235 case Token::SUB:
1236 case Token::MUL:
1237 case Token::DIV:
1238 GenerateTypeTransition(masm); // Tail call.
1239 generate_code_to_calculate_answer = false;
1240 break;
1241
1242 default:
1243 break;
1244 }
1245 }
1246
1247 if (generate_code_to_calculate_answer) {
1248 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
1249 if (mode_ == NO_OVERWRITE) {
1250 // In the case where there is no chance of an overwritable float we may
1251 // as well do the allocation immediately while r0 and r1 are untouched.
1252 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
1253 }
1254
1255 // Move r0 to a double in r2-r3.
1256 __ tst(r0, Operand(kSmiTagMask));
1257 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
1258 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
1259 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1260 __ cmp(r4, heap_number_map);
1261 __ b(ne, &slow);
1262 if (mode_ == OVERWRITE_RIGHT) {
1263 __ mov(r5, Operand(r0)); // Overwrite this heap number.
1264 }
1265 if (use_fp_registers) {
1266 CpuFeatures::Scope scope(VFP3);
1267 // Load the double from tagged HeapNumber r0 to d7.
1268 __ sub(r7, r0, Operand(kHeapObjectTag));
1269 __ vldr(d7, r7, HeapNumber::kValueOffset);
1270 } else {
1271 // Calling convention says that second double is in r2 and r3.
1272 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
1273 }
1274 __ jmp(&finished_loading_r0);
1275 __ bind(&r0_is_smi);
1276 if (mode_ == OVERWRITE_RIGHT) {
1277 // We can't overwrite a Smi so get address of new heap number into r5.
1278 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1279 }
1280
1281 if (CpuFeatures::IsSupported(VFP3)) {
1282 CpuFeatures::Scope scope(VFP3);
1283 // Convert smi in r0 to double in d7.
1284 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
1285 __ vmov(s15, r7);
1286 __ vcvt_f64_s32(d7, s15);
1287 if (!use_fp_registers) {
1288 __ vmov(r2, r3, d7);
1289 }
1290 } else {
1291 // Write Smi from r0 to r3 and r2 in double format.
1292 __ mov(r7, Operand(r0));
1293 ConvertToDoubleStub stub3(r3, r2, r7, r4);
1294 __ push(lr);
1295 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
1296 __ pop(lr);
1297 }
1298
1299 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
1300 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
1301 Label r1_is_not_smi;
1302 if (runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) {
1303 __ 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
2897// function in r0 (or at sp+1*ptrsz) and object in
2898// 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:
2902 const Register object = r1; // Object (lhs).
2903 const Register map = r3; // Map of the object.
2904 const Register function = r0; // Function (rhs).
2905 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()) {
2909 __ ldr(function, MemOperand(sp, 1 * kPointerSize));
2910 __ ldr(object, MemOperand(sp, 0));
2911 }
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.
2914 __ BranchOnSmi(object, &slow);
2915 __ IsObjectJSObjectType(object, map, scratch, &slow);
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);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002920 __ cmp(object, 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);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002925 __ LoadRoot(function, Heap::kInstanceofCacheAnswerRootIndex);
2926 __ Ret(args_in_registers() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002927
2928 __ bind(&miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002929 __ TryGetFunctionPrototype(object, 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
Ben Murdochb0fe1622011-05-05 13:52:32 +01002935 __ StoreRoot(object, Heap::kInstanceofCacheFunctionRootIndex);
2936 __ 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)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002960 __ Ret(args_in_registers() ? 0 : 2);
2961
2962 Label object_not_null, object_not_null_or_smi;
2963 __ bind(&not_js_object);
2964 // Before null, smi and string value checks, check that the rhs is a function
2965 // as for a non-function rhs an exception needs to be thrown.
2966 __ BranchOnSmi(function, &slow);
2967 __ CompareObjectType(function, map, scratch, JS_FUNCTION_TYPE);
2968 __ b(ne, &slow);
2969
2970 // Null is not instance of anything.
2971 __ cmp(scratch, Operand(Factory::null_value()));
2972 __ b(ne, &object_not_null);
2973 __ mov(r0, Operand(Smi::FromInt(1)));
2974 __ Ret(args_in_registers() ? 0 : 2);
2975
2976 __ bind(&object_not_null);
2977 // Smi values are not instances of anything.
2978 __ BranchOnNotSmi(object, &object_not_null_or_smi);
2979 __ mov(r0, Operand(Smi::FromInt(1)));
2980 __ Ret(args_in_registers() ? 0 : 2);
2981
2982 __ bind(&object_not_null_or_smi);
2983 // String values are not instances of anything.
2984 __ IsObjectJSStringType(object, scratch, &slow);
2985 __ mov(r0, Operand(Smi::FromInt(1)));
2986 __ Ret(args_in_registers() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002987
2988 // Slow-case. Tail call builtin.
2989 __ bind(&slow);
2990 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
2991}
2992
2993
2994void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2995 // The displacement is the offset of the last parameter (if any)
2996 // relative to the frame pointer.
2997 static const int kDisplacement =
2998 StandardFrameConstants::kCallerSPOffset - kPointerSize;
2999
3000 // Check that the key is a smi.
3001 Label slow;
3002 __ BranchOnNotSmi(r1, &slow);
3003
3004 // Check if the calling frame is an arguments adaptor frame.
3005 Label adaptor;
3006 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3007 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3008 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3009 __ b(eq, &adaptor);
3010
3011 // Check index against formal parameters count limit passed in
3012 // through register r0. Use unsigned comparison to get negative
3013 // check for free.
3014 __ cmp(r1, r0);
3015 __ b(cs, &slow);
3016
3017 // Read the argument from the stack and return it.
3018 __ sub(r3, r0, r1);
3019 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3020 __ ldr(r0, MemOperand(r3, kDisplacement));
3021 __ Jump(lr);
3022
3023 // Arguments adaptor case: Check index against actual arguments
3024 // limit found in the arguments adaptor frame. Use unsigned
3025 // comparison to get negative check for free.
3026 __ bind(&adaptor);
3027 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3028 __ cmp(r1, r0);
3029 __ b(cs, &slow);
3030
3031 // Read the argument from the adaptor frame and return it.
3032 __ sub(r3, r0, r1);
3033 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3034 __ ldr(r0, MemOperand(r3, kDisplacement));
3035 __ Jump(lr);
3036
3037 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3038 // by calling the runtime system.
3039 __ bind(&slow);
3040 __ push(r1);
3041 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3042}
3043
3044
3045void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
3046 // sp[0] : number of parameters
3047 // sp[4] : receiver displacement
3048 // sp[8] : function
3049
3050 // Check if the calling frame is an arguments adaptor frame.
3051 Label adaptor_frame, try_allocate, runtime;
3052 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3053 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3054 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3055 __ b(eq, &adaptor_frame);
3056
3057 // Get the length from the frame.
3058 __ ldr(r1, MemOperand(sp, 0));
3059 __ b(&try_allocate);
3060
3061 // Patch the arguments.length and the parameters pointer.
3062 __ bind(&adaptor_frame);
3063 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3064 __ str(r1, MemOperand(sp, 0));
3065 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3066 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
3067 __ str(r3, MemOperand(sp, 1 * kPointerSize));
3068
3069 // Try the new space allocation. Start out with computing the size
3070 // of the arguments object and the elements array in words.
3071 Label add_arguments_object;
3072 __ bind(&try_allocate);
Iain Merrick9ac36c92010-09-13 15:29:50 +01003073 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003074 __ b(eq, &add_arguments_object);
3075 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
3076 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
3077 __ bind(&add_arguments_object);
3078 __ add(r1, r1, Operand(Heap::kArgumentsObjectSize / kPointerSize));
3079
3080 // Do the allocation of both objects in one go.
3081 __ AllocateInNewSpace(
3082 r1,
3083 r0,
3084 r2,
3085 r3,
3086 &runtime,
3087 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
3088
3089 // Get the arguments boilerplate from the current (global) context.
3090 int offset = Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
3091 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
3092 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
3093 __ ldr(r4, MemOperand(r4, offset));
3094
3095 // Copy the JS object part.
3096 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
3097
3098 // Setup the callee in-object property.
3099 STATIC_ASSERT(Heap::arguments_callee_index == 0);
3100 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
3101 __ str(r3, FieldMemOperand(r0, JSObject::kHeaderSize));
3102
3103 // Get the length (smi tagged) and set that as an in-object property too.
3104 STATIC_ASSERT(Heap::arguments_length_index == 1);
3105 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
3106 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize + kPointerSize));
3107
3108 // If there are no actual arguments, we're done.
3109 Label done;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003110 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003111 __ b(eq, &done);
3112
3113 // Get the parameters pointer from the stack.
3114 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
3115
3116 // Setup the elements pointer in the allocated arguments object and
3117 // initialize the header in the elements fixed array.
3118 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
3119 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
3120 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
3121 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
3122 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
3123 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
3124
3125 // Copy the fixed array slots.
3126 Label loop;
3127 // Setup r4 to point to the first array slot.
3128 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3129 __ bind(&loop);
3130 // Pre-decrement r2 with kPointerSize on each iteration.
3131 // Pre-decrement in order to skip receiver.
3132 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
3133 // Post-increment r4 with kPointerSize on each iteration.
3134 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
3135 __ sub(r1, r1, Operand(1));
Iain Merrick9ac36c92010-09-13 15:29:50 +01003136 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003137 __ b(ne, &loop);
3138
3139 // Return and remove the on-stack parameters.
3140 __ bind(&done);
3141 __ add(sp, sp, Operand(3 * kPointerSize));
3142 __ Ret();
3143
3144 // Do the runtime call to allocate the arguments object.
3145 __ bind(&runtime);
3146 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3147}
3148
3149
3150void RegExpExecStub::Generate(MacroAssembler* masm) {
3151 // Just jump directly to runtime if native RegExp is not selected at compile
3152 // time or if regexp entry in generated code is turned off runtime switch or
3153 // at compilation.
3154#ifdef V8_INTERPRETED_REGEXP
3155 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3156#else // V8_INTERPRETED_REGEXP
3157 if (!FLAG_regexp_entry_native) {
3158 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3159 return;
3160 }
3161
3162 // Stack frame on entry.
3163 // sp[0]: last_match_info (expected JSArray)
3164 // sp[4]: previous index
3165 // sp[8]: subject string
3166 // sp[12]: JSRegExp object
3167
3168 static const int kLastMatchInfoOffset = 0 * kPointerSize;
3169 static const int kPreviousIndexOffset = 1 * kPointerSize;
3170 static const int kSubjectOffset = 2 * kPointerSize;
3171 static const int kJSRegExpOffset = 3 * kPointerSize;
3172
3173 Label runtime, invoke_regexp;
3174
3175 // Allocation of registers for this function. These are in callee save
3176 // registers and will be preserved by the call to the native RegExp code, as
3177 // this code is called using the normal C calling convention. When calling
3178 // directly from generated code the native RegExp code will not do a GC and
3179 // therefore the content of these registers are safe to use after the call.
3180 Register subject = r4;
3181 Register regexp_data = r5;
3182 Register last_match_info_elements = r6;
3183
3184 // Ensure that a RegExp stack is allocated.
3185 ExternalReference address_of_regexp_stack_memory_address =
3186 ExternalReference::address_of_regexp_stack_memory_address();
3187 ExternalReference address_of_regexp_stack_memory_size =
3188 ExternalReference::address_of_regexp_stack_memory_size();
3189 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
3190 __ ldr(r0, MemOperand(r0, 0));
3191 __ tst(r0, Operand(r0));
3192 __ b(eq, &runtime);
3193
3194 // Check that the first argument is a JSRegExp object.
3195 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
3196 STATIC_ASSERT(kSmiTag == 0);
3197 __ tst(r0, Operand(kSmiTagMask));
3198 __ b(eq, &runtime);
3199 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3200 __ b(ne, &runtime);
3201
3202 // Check that the RegExp has been compiled (data contains a fixed array).
3203 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
3204 if (FLAG_debug_code) {
3205 __ tst(regexp_data, Operand(kSmiTagMask));
3206 __ Check(nz, "Unexpected type for RegExp data, FixedArray expected");
3207 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
3208 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
3209 }
3210
3211 // regexp_data: RegExp data (FixedArray)
3212 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3213 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
3214 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
3215 __ b(ne, &runtime);
3216
3217 // regexp_data: RegExp data (FixedArray)
3218 // Check that the number of captures fit in the static offsets vector buffer.
3219 __ ldr(r2,
3220 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
3221 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3222 // uses the asumption that smis are 2 * their untagged value.
3223 STATIC_ASSERT(kSmiTag == 0);
3224 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3225 __ add(r2, r2, Operand(2)); // r2 was a smi.
3226 // Check that the static offsets vector buffer is large enough.
3227 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
3228 __ b(hi, &runtime);
3229
3230 // r2: Number of capture registers
3231 // regexp_data: RegExp data (FixedArray)
3232 // Check that the second argument is a string.
3233 __ ldr(subject, MemOperand(sp, kSubjectOffset));
3234 __ tst(subject, Operand(kSmiTagMask));
3235 __ b(eq, &runtime);
3236 Condition is_string = masm->IsObjectStringType(subject, r0);
3237 __ b(NegateCondition(is_string), &runtime);
3238 // Get the length of the string to r3.
3239 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
3240
3241 // r2: Number of capture registers
3242 // r3: Length of subject string as a smi
3243 // subject: Subject string
3244 // regexp_data: RegExp data (FixedArray)
3245 // Check that the third argument is a positive smi less than the subject
3246 // string length. A negative value will be greater (unsigned comparison).
3247 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
3248 __ tst(r0, Operand(kSmiTagMask));
3249 __ b(ne, &runtime);
3250 __ cmp(r3, Operand(r0));
3251 __ b(ls, &runtime);
3252
3253 // r2: Number of capture registers
3254 // subject: Subject string
3255 // regexp_data: RegExp data (FixedArray)
3256 // Check that the fourth object is a JSArray object.
3257 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
3258 __ tst(r0, Operand(kSmiTagMask));
3259 __ b(eq, &runtime);
3260 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
3261 __ b(ne, &runtime);
3262 // Check that the JSArray is in fast case.
3263 __ ldr(last_match_info_elements,
3264 FieldMemOperand(r0, JSArray::kElementsOffset));
3265 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
3266 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
3267 __ cmp(r0, ip);
3268 __ b(ne, &runtime);
3269 // Check that the last match info has space for the capture registers and the
3270 // additional information.
3271 __ ldr(r0,
3272 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
3273 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
3274 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
3275 __ b(gt, &runtime);
3276
3277 // subject: Subject string
3278 // regexp_data: RegExp data (FixedArray)
3279 // Check the representation and encoding of the subject string.
3280 Label seq_string;
3281 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
3282 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3283 // First check for flat string.
3284 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
3285 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
3286 __ b(eq, &seq_string);
3287
3288 // subject: Subject string
3289 // regexp_data: RegExp data (FixedArray)
3290 // Check for flat cons string.
3291 // A flat cons string is a cons string where the second part is the empty
3292 // string. In that case the subject string is just the first part of the cons
3293 // string. Also in this case the first part of the cons string is known to be
3294 // a sequential string or an external string.
3295 STATIC_ASSERT(kExternalStringTag !=0);
3296 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
3297 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
3298 __ b(ne, &runtime);
3299 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
3300 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
3301 __ cmp(r0, r1);
3302 __ b(ne, &runtime);
3303 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
3304 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
3305 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
3306 // Is first part a flat string?
3307 STATIC_ASSERT(kSeqStringTag == 0);
3308 __ tst(r0, Operand(kStringRepresentationMask));
3309 __ b(nz, &runtime);
3310
3311 __ bind(&seq_string);
3312 // subject: Subject string
3313 // regexp_data: RegExp data (FixedArray)
3314 // r0: Instance type of subject string
3315 STATIC_ASSERT(4 == kAsciiStringTag);
3316 STATIC_ASSERT(kTwoByteStringTag == 0);
3317 // Find the code object based on the assumptions above.
3318 __ and_(r0, r0, Operand(kStringEncodingMask));
3319 __ mov(r3, Operand(r0, ASR, 2), SetCC);
3320 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
3321 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
3322
3323 // Check that the irregexp code has been generated for the actual string
3324 // encoding. If it has, the field contains a code object otherwise it contains
3325 // the hole.
3326 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
3327 __ b(ne, &runtime);
3328
3329 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
3330 // r7: code
3331 // subject: Subject string
3332 // regexp_data: RegExp data (FixedArray)
3333 // Load used arguments before starting to push arguments for call to native
3334 // RegExp code to avoid handling changing stack height.
3335 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
3336 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
3337
3338 // r1: previous index
3339 // r3: encoding of subject string (1 if ascii, 0 if two_byte);
3340 // r7: code
3341 // subject: Subject string
3342 // regexp_data: RegExp data (FixedArray)
3343 // All checks done. Now push arguments for native regexp code.
3344 __ IncrementCounter(&Counters::regexp_entry_native, 1, r0, r2);
3345
3346 static const int kRegExpExecuteArguments = 7;
3347 __ push(lr);
3348 __ PrepareCallCFunction(kRegExpExecuteArguments, r0);
3349
3350 // Argument 7 (sp[8]): Indicate that this is a direct call from JavaScript.
3351 __ mov(r0, Operand(1));
3352 __ str(r0, MemOperand(sp, 2 * kPointerSize));
3353
3354 // Argument 6 (sp[4]): Start (high end) of backtracking stack memory area.
3355 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
3356 __ ldr(r0, MemOperand(r0, 0));
3357 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
3358 __ ldr(r2, MemOperand(r2, 0));
3359 __ add(r0, r0, Operand(r2));
3360 __ str(r0, MemOperand(sp, 1 * kPointerSize));
3361
3362 // Argument 5 (sp[0]): static offsets vector buffer.
3363 __ mov(r0, Operand(ExternalReference::address_of_static_offsets_vector()));
3364 __ str(r0, MemOperand(sp, 0 * kPointerSize));
3365
3366 // For arguments 4 and 3 get string length, calculate start of string data and
3367 // calculate the shift of the index (0 for ASCII and 1 for two byte).
3368 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
3369 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
3370 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
3371 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
3372 __ eor(r3, r3, Operand(1));
3373 // Argument 4 (r3): End of string data
3374 // Argument 3 (r2): Start of string data
3375 __ add(r2, r9, Operand(r1, LSL, r3));
3376 __ add(r3, r9, Operand(r0, LSL, r3));
3377
3378 // Argument 2 (r1): Previous index.
3379 // Already there
3380
3381 // Argument 1 (r0): Subject string.
3382 __ mov(r0, subject);
3383
3384 // Locate the code entry and call it.
3385 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
3386 __ CallCFunction(r7, kRegExpExecuteArguments);
3387 __ pop(lr);
3388
3389 // r0: result
3390 // subject: subject string (callee saved)
3391 // regexp_data: RegExp data (callee saved)
3392 // last_match_info_elements: Last match info elements (callee saved)
3393
3394 // Check the result.
3395 Label success;
3396 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
3397 __ b(eq, &success);
3398 Label failure;
3399 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
3400 __ b(eq, &failure);
3401 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
3402 // If not exception it can only be retry. Handle that in the runtime system.
3403 __ b(ne, &runtime);
3404 // Result must now be exception. If there is no pending exception already a
3405 // stack overflow (on the backtrack stack) was detected in RegExp code but
3406 // haven't created the exception yet. Handle that in the runtime system.
3407 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
3408 __ mov(r0, Operand(ExternalReference::the_hole_value_location()));
3409 __ ldr(r0, MemOperand(r0, 0));
3410 __ mov(r1, Operand(ExternalReference(Top::k_pending_exception_address)));
3411 __ ldr(r1, MemOperand(r1, 0));
3412 __ cmp(r0, r1);
3413 __ b(eq, &runtime);
3414 __ bind(&failure);
3415 // For failure and exception return null.
3416 __ mov(r0, Operand(Factory::null_value()));
3417 __ add(sp, sp, Operand(4 * kPointerSize));
3418 __ Ret();
3419
3420 // Process the result from the native regexp code.
3421 __ bind(&success);
3422 __ ldr(r1,
3423 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
3424 // Calculate number of capture registers (number_of_captures + 1) * 2.
3425 STATIC_ASSERT(kSmiTag == 0);
3426 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3427 __ add(r1, r1, Operand(2)); // r1 was a smi.
3428
3429 // r1: number of capture registers
3430 // r4: subject string
3431 // Store the capture count.
3432 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
3433 __ str(r2, FieldMemOperand(last_match_info_elements,
3434 RegExpImpl::kLastCaptureCountOffset));
3435 // Store last subject and last input.
3436 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
3437 __ str(subject,
3438 FieldMemOperand(last_match_info_elements,
3439 RegExpImpl::kLastSubjectOffset));
3440 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
3441 __ str(subject,
3442 FieldMemOperand(last_match_info_elements,
3443 RegExpImpl::kLastInputOffset));
3444 __ mov(r3, last_match_info_elements);
3445 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
3446
3447 // Get the static offsets vector filled by the native regexp code.
3448 ExternalReference address_of_static_offsets_vector =
3449 ExternalReference::address_of_static_offsets_vector();
3450 __ mov(r2, Operand(address_of_static_offsets_vector));
3451
3452 // r1: number of capture registers
3453 // r2: offsets vector
3454 Label next_capture, done;
3455 // Capture register counter starts from number of capture registers and
3456 // counts down until wraping after zero.
3457 __ add(r0,
3458 last_match_info_elements,
3459 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
3460 __ bind(&next_capture);
3461 __ sub(r1, r1, Operand(1), SetCC);
3462 __ b(mi, &done);
3463 // Read the value from the static offsets vector buffer.
3464 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
3465 // Store the smi value in the last match info.
3466 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
3467 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
3468 __ jmp(&next_capture);
3469 __ bind(&done);
3470
3471 // Return last match info.
3472 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
3473 __ add(sp, sp, Operand(4 * kPointerSize));
3474 __ Ret();
3475
3476 // Do the runtime call to execute the regexp.
3477 __ bind(&runtime);
3478 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3479#endif // V8_INTERPRETED_REGEXP
3480}
3481
3482
Ben Murdochb0fe1622011-05-05 13:52:32 +01003483void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3484 const int kMaxInlineLength = 100;
3485 Label slowcase;
3486 Label done;
3487 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
3488 STATIC_ASSERT(kSmiTag == 0);
3489 STATIC_ASSERT(kSmiTagSize == 1);
3490 __ tst(r1, Operand(kSmiTagMask));
3491 __ b(ne, &slowcase);
3492 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
3493 __ b(hi, &slowcase);
3494 // Smi-tagging is equivalent to multiplying by 2.
3495 // Allocate RegExpResult followed by FixedArray with size in ebx.
3496 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3497 // Elements: [Map][Length][..elements..]
3498 // Size of JSArray with two in-object properties and the header of a
3499 // FixedArray.
3500 int objects_size =
3501 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
3502 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
3503 __ add(r2, r5, Operand(objects_size));
3504 __ AllocateInNewSpace(
3505 r2, // In: Size, in words.
3506 r0, // Out: Start of allocation (tagged).
3507 r3, // Scratch register.
3508 r4, // Scratch register.
3509 &slowcase,
3510 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
3511 // r0: Start of allocated area, object-tagged.
3512 // r1: Number of elements in array, as smi.
3513 // r5: Number of elements, untagged.
3514
3515 // Set JSArray map to global.regexp_result_map().
3516 // Set empty properties FixedArray.
3517 // Set elements to point to FixedArray allocated right after the JSArray.
3518 // Interleave operations for better latency.
3519 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
3520 __ add(r3, r0, Operand(JSRegExpResult::kSize));
3521 __ mov(r4, Operand(Factory::empty_fixed_array()));
3522 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
3523 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
3524 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
3525 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
3526 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
3527
3528 // Set input, index and length fields from arguments.
3529 __ ldr(r1, MemOperand(sp, kPointerSize * 0));
3530 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
3531 __ ldr(r1, MemOperand(sp, kPointerSize * 1));
3532 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
3533 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
3534 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
3535
3536 // Fill out the elements FixedArray.
3537 // r0: JSArray, tagged.
3538 // r3: FixedArray, tagged.
3539 // r5: Number of elements in array, untagged.
3540
3541 // Set map.
3542 __ mov(r2, Operand(Factory::fixed_array_map()));
3543 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
3544 // Set FixedArray length.
3545 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
3546 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
3547 // Fill contents of fixed-array with the-hole.
3548 __ mov(r2, Operand(Factory::the_hole_value()));
3549 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3550 // Fill fixed array elements with hole.
3551 // r0: JSArray, tagged.
3552 // r2: the hole.
3553 // r3: Start of elements in FixedArray.
3554 // r5: Number of elements to fill.
3555 Label loop;
3556 __ tst(r5, Operand(r5));
3557 __ bind(&loop);
3558 __ b(le, &done); // Jump if r1 is negative or zero.
3559 __ sub(r5, r5, Operand(1), SetCC);
3560 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
3561 __ jmp(&loop);
3562
3563 __ bind(&done);
3564 __ add(sp, sp, Operand(3 * kPointerSize));
3565 __ Ret();
3566
3567 __ bind(&slowcase);
3568 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3569}
3570
3571
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003572void CallFunctionStub::Generate(MacroAssembler* masm) {
3573 Label slow;
3574
3575 // If the receiver might be a value (string, number or boolean) check for this
3576 // and box it if it is.
3577 if (ReceiverMightBeValue()) {
3578 // Get the receiver from the stack.
3579 // function, receiver [, arguments]
3580 Label receiver_is_value, receiver_is_js_object;
3581 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
3582
3583 // Check if receiver is a smi (which is a number value).
3584 __ BranchOnSmi(r1, &receiver_is_value);
3585
3586 // Check if the receiver is a valid JS object.
3587 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
3588 __ b(ge, &receiver_is_js_object);
3589
3590 // Call the runtime to box the value.
3591 __ bind(&receiver_is_value);
3592 __ EnterInternalFrame();
3593 __ push(r1);
3594 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
3595 __ LeaveInternalFrame();
3596 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
3597
3598 __ bind(&receiver_is_js_object);
3599 }
3600
3601 // Get the function to call from the stack.
3602 // function, receiver [, arguments]
3603 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
3604
3605 // Check that the function is really a JavaScript function.
3606 // r1: pushed function (to be verified)
3607 __ BranchOnSmi(r1, &slow);
3608 // Get the map of the function object.
3609 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
3610 __ b(ne, &slow);
3611
3612 // Fast-case: Invoke the function now.
3613 // r1: pushed function
3614 ParameterCount actual(argc_);
3615 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
3616
3617 // Slow-case: Non-function called.
3618 __ bind(&slow);
3619 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3620 // of the original receiver from the call site).
3621 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
3622 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
Iain Merrick9ac36c92010-09-13 15:29:50 +01003623 __ mov(r2, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003624 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
3625 __ Jump(Handle<Code>(Builtins::builtin(Builtins::ArgumentsAdaptorTrampoline)),
3626 RelocInfo::CODE_TARGET);
3627}
3628
3629
3630// Unfortunately you have to run without snapshots to see most of these
3631// names in the profile since most compare stubs end up in the snapshot.
3632const char* CompareStub::GetName() {
3633 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
3634 (lhs_.is(r1) && rhs_.is(r0)));
3635
3636 if (name_ != NULL) return name_;
3637 const int kMaxNameLength = 100;
3638 name_ = Bootstrapper::AllocateAutoDeletedArray(kMaxNameLength);
3639 if (name_ == NULL) return "OOM";
3640
3641 const char* cc_name;
3642 switch (cc_) {
3643 case lt: cc_name = "LT"; break;
3644 case gt: cc_name = "GT"; break;
3645 case le: cc_name = "LE"; break;
3646 case ge: cc_name = "GE"; break;
3647 case eq: cc_name = "EQ"; break;
3648 case ne: cc_name = "NE"; break;
3649 default: cc_name = "UnknownCondition"; break;
3650 }
3651
3652 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
3653 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
3654
3655 const char* strict_name = "";
3656 if (strict_ && (cc_ == eq || cc_ == ne)) {
3657 strict_name = "_STRICT";
3658 }
3659
3660 const char* never_nan_nan_name = "";
3661 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
3662 never_nan_nan_name = "_NO_NAN";
3663 }
3664
3665 const char* include_number_compare_name = "";
3666 if (!include_number_compare_) {
3667 include_number_compare_name = "_NO_NUMBER";
3668 }
3669
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003670 const char* include_smi_compare_name = "";
3671 if (!include_smi_compare_) {
3672 include_smi_compare_name = "_NO_SMI";
3673 }
3674
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003675 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
3676 "CompareStub_%s%s%s%s%s%s",
3677 cc_name,
3678 lhs_name,
3679 rhs_name,
3680 strict_name,
3681 never_nan_nan_name,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003682 include_number_compare_name,
3683 include_smi_compare_name);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003684 return name_;
3685}
3686
3687
3688int CompareStub::MinorKey() {
3689 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
3690 // stubs the never NaN NaN condition is only taken into account if the
3691 // condition is equals.
3692 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
3693 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
3694 (lhs_.is(r1) && rhs_.is(r0)));
3695 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
3696 | RegisterField::encode(lhs_.is(r0))
3697 | StrictField::encode(strict_)
3698 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003699 | IncludeNumberCompareField::encode(include_number_compare_)
3700 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003701}
3702
3703
3704// StringCharCodeAtGenerator
3705
3706void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
3707 Label flat_string;
3708 Label ascii_string;
3709 Label got_char_code;
3710
3711 // If the receiver is a smi trigger the non-string case.
3712 __ BranchOnSmi(object_, receiver_not_string_);
3713
3714 // Fetch the instance type of the receiver into result register.
3715 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3716 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3717 // If the receiver is not a string trigger the non-string case.
3718 __ tst(result_, Operand(kIsNotStringMask));
3719 __ b(ne, receiver_not_string_);
3720
3721 // If the index is non-smi trigger the non-smi case.
3722 __ BranchOnNotSmi(index_, &index_not_smi_);
3723
3724 // Put smi-tagged index into scratch register.
3725 __ mov(scratch_, index_);
3726 __ bind(&got_smi_index_);
3727
3728 // Check for index out of range.
3729 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
3730 __ cmp(ip, Operand(scratch_));
3731 __ b(ls, index_out_of_range_);
3732
3733 // We need special handling for non-flat strings.
3734 STATIC_ASSERT(kSeqStringTag == 0);
3735 __ tst(result_, Operand(kStringRepresentationMask));
3736 __ b(eq, &flat_string);
3737
3738 // Handle non-flat strings.
3739 __ tst(result_, Operand(kIsConsStringMask));
3740 __ b(eq, &call_runtime_);
3741
3742 // ConsString.
3743 // Check whether the right hand side is the empty string (i.e. if
3744 // this is really a flat string in a cons string). If that is not
3745 // the case we would rather go to the runtime system now to flatten
3746 // the string.
3747 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
3748 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
3749 __ cmp(result_, Operand(ip));
3750 __ b(ne, &call_runtime_);
3751 // Get the first of the two strings and load its instance type.
3752 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
3753 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3754 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3755 // If the first cons component is also non-flat, then go to runtime.
3756 STATIC_ASSERT(kSeqStringTag == 0);
3757 __ tst(result_, Operand(kStringRepresentationMask));
3758 __ b(nz, &call_runtime_);
3759
3760 // Check for 1-byte or 2-byte string.
3761 __ bind(&flat_string);
3762 STATIC_ASSERT(kAsciiStringTag != 0);
3763 __ tst(result_, Operand(kStringEncodingMask));
3764 __ b(nz, &ascii_string);
3765
3766 // 2-byte string.
3767 // Load the 2-byte character code into the result register. We can
3768 // add without shifting since the smi tag size is the log2 of the
3769 // number of bytes in a two-byte character.
3770 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
3771 __ add(scratch_, object_, Operand(scratch_));
3772 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
3773 __ jmp(&got_char_code);
3774
3775 // ASCII string.
3776 // Load the byte into the result register.
3777 __ bind(&ascii_string);
3778 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
3779 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
3780
3781 __ bind(&got_char_code);
3782 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
3783 __ bind(&exit_);
3784}
3785
3786
3787void StringCharCodeAtGenerator::GenerateSlow(
3788 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3789 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
3790
3791 // Index is not a smi.
3792 __ bind(&index_not_smi_);
3793 // If index is a heap number, try converting it to an integer.
3794 __ CheckMap(index_,
3795 scratch_,
3796 Heap::kHeapNumberMapRootIndex,
3797 index_not_number_,
3798 true);
3799 call_helper.BeforeCall(masm);
3800 __ Push(object_, index_);
3801 __ push(index_); // Consumed by runtime conversion function.
3802 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
3803 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
3804 } else {
3805 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
3806 // NumberToSmi discards numbers that are not exact integers.
3807 __ CallRuntime(Runtime::kNumberToSmi, 1);
3808 }
3809 // Save the conversion result before the pop instructions below
3810 // have a chance to overwrite it.
3811 __ Move(scratch_, r0);
3812 __ pop(index_);
3813 __ pop(object_);
3814 // Reload the instance type.
3815 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
3816 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
3817 call_helper.AfterCall(masm);
3818 // If index is still not a smi, it must be out of range.
3819 __ BranchOnNotSmi(scratch_, index_out_of_range_);
3820 // Otherwise, return to the fast path.
3821 __ jmp(&got_smi_index_);
3822
3823 // Call runtime. We get here when the receiver is a string and the
3824 // index is a number, but the code of getting the actual character
3825 // is too complex (e.g., when the string needs to be flattened).
3826 __ bind(&call_runtime_);
3827 call_helper.BeforeCall(masm);
3828 __ Push(object_, index_);
3829 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
3830 __ Move(result_, r0);
3831 call_helper.AfterCall(masm);
3832 __ jmp(&exit_);
3833
3834 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
3835}
3836
3837
3838// -------------------------------------------------------------------------
3839// StringCharFromCodeGenerator
3840
3841void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
3842 // Fast case of Heap::LookupSingleCharacterStringFromCode.
3843 STATIC_ASSERT(kSmiTag == 0);
3844 STATIC_ASSERT(kSmiShiftSize == 0);
3845 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
3846 __ tst(code_,
3847 Operand(kSmiTagMask |
3848 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
3849 __ b(nz, &slow_case_);
3850
3851 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
3852 // At this point code register contains smi tagged ascii char code.
3853 STATIC_ASSERT(kSmiTag == 0);
3854 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
3855 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
3856 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
3857 __ cmp(result_, Operand(ip));
3858 __ b(eq, &slow_case_);
3859 __ bind(&exit_);
3860}
3861
3862
3863void StringCharFromCodeGenerator::GenerateSlow(
3864 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3865 __ Abort("Unexpected fallthrough to CharFromCode slow case");
3866
3867 __ bind(&slow_case_);
3868 call_helper.BeforeCall(masm);
3869 __ push(code_);
3870 __ CallRuntime(Runtime::kCharFromCode, 1);
3871 __ Move(result_, r0);
3872 call_helper.AfterCall(masm);
3873 __ jmp(&exit_);
3874
3875 __ Abort("Unexpected fallthrough from CharFromCode slow case");
3876}
3877
3878
3879// -------------------------------------------------------------------------
3880// StringCharAtGenerator
3881
3882void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
3883 char_code_at_generator_.GenerateFast(masm);
3884 char_from_code_generator_.GenerateFast(masm);
3885}
3886
3887
3888void StringCharAtGenerator::GenerateSlow(
3889 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3890 char_code_at_generator_.GenerateSlow(masm, call_helper);
3891 char_from_code_generator_.GenerateSlow(masm, call_helper);
3892}
3893
3894
3895class StringHelper : public AllStatic {
3896 public:
3897 // Generate code for copying characters using a simple loop. This should only
3898 // be used in places where the number of characters is small and the
3899 // additional setup and checking in GenerateCopyCharactersLong adds too much
3900 // overhead. Copying of overlapping regions is not supported.
3901 // Dest register ends at the position after the last character written.
3902 static void GenerateCopyCharacters(MacroAssembler* masm,
3903 Register dest,
3904 Register src,
3905 Register count,
3906 Register scratch,
3907 bool ascii);
3908
3909 // Generate code for copying a large number of characters. This function
3910 // is allowed to spend extra time setting up conditions to make copying
3911 // faster. Copying of overlapping regions is not supported.
3912 // Dest register ends at the position after the last character written.
3913 static void GenerateCopyCharactersLong(MacroAssembler* masm,
3914 Register dest,
3915 Register src,
3916 Register count,
3917 Register scratch1,
3918 Register scratch2,
3919 Register scratch3,
3920 Register scratch4,
3921 Register scratch5,
3922 int flags);
3923
3924
3925 // Probe the symbol table for a two character string. If the string is
3926 // not found by probing a jump to the label not_found is performed. This jump
3927 // does not guarantee that the string is not in the symbol table. If the
3928 // string is found the code falls through with the string in register r0.
3929 // Contents of both c1 and c2 registers are modified. At the exit c1 is
3930 // guaranteed to contain halfword with low and high bytes equal to
3931 // initial contents of c1 and c2 respectively.
3932 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
3933 Register c1,
3934 Register c2,
3935 Register scratch1,
3936 Register scratch2,
3937 Register scratch3,
3938 Register scratch4,
3939 Register scratch5,
3940 Label* not_found);
3941
3942 // Generate string hash.
3943 static void GenerateHashInit(MacroAssembler* masm,
3944 Register hash,
3945 Register character);
3946
3947 static void GenerateHashAddCharacter(MacroAssembler* masm,
3948 Register hash,
3949 Register character);
3950
3951 static void GenerateHashGetHash(MacroAssembler* masm,
3952 Register hash);
3953
3954 private:
3955 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
3956};
3957
3958
3959void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
3960 Register dest,
3961 Register src,
3962 Register count,
3963 Register scratch,
3964 bool ascii) {
3965 Label loop;
3966 Label done;
3967 // This loop just copies one character at a time, as it is only used for very
3968 // short strings.
3969 if (!ascii) {
3970 __ add(count, count, Operand(count), SetCC);
3971 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01003972 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003973 }
3974 __ b(eq, &done);
3975
3976 __ bind(&loop);
3977 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
3978 // Perform sub between load and dependent store to get the load time to
3979 // complete.
3980 __ sub(count, count, Operand(1), SetCC);
3981 __ strb(scratch, MemOperand(dest, 1, PostIndex));
3982 // last iteration.
3983 __ b(gt, &loop);
3984
3985 __ bind(&done);
3986}
3987
3988
3989enum CopyCharactersFlags {
3990 COPY_ASCII = 1,
3991 DEST_ALWAYS_ALIGNED = 2
3992};
3993
3994
3995void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
3996 Register dest,
3997 Register src,
3998 Register count,
3999 Register scratch1,
4000 Register scratch2,
4001 Register scratch3,
4002 Register scratch4,
4003 Register scratch5,
4004 int flags) {
4005 bool ascii = (flags & COPY_ASCII) != 0;
4006 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
4007
4008 if (dest_always_aligned && FLAG_debug_code) {
4009 // Check that destination is actually word aligned if the flag says
4010 // that it is.
4011 __ tst(dest, Operand(kPointerAlignmentMask));
4012 __ Check(eq, "Destination of copy not aligned.");
4013 }
4014
4015 const int kReadAlignment = 4;
4016 const int kReadAlignmentMask = kReadAlignment - 1;
4017 // Ensure that reading an entire aligned word containing the last character
4018 // of a string will not read outside the allocated area (because we pad up
4019 // to kObjectAlignment).
4020 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
4021 // Assumes word reads and writes are little endian.
4022 // Nothing to do for zero characters.
4023 Label done;
4024 if (!ascii) {
4025 __ add(count, count, Operand(count), SetCC);
4026 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01004027 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004028 }
4029 __ b(eq, &done);
4030
4031 // Assume that you cannot read (or write) unaligned.
4032 Label byte_loop;
4033 // Must copy at least eight bytes, otherwise just do it one byte at a time.
4034 __ cmp(count, Operand(8));
4035 __ add(count, dest, Operand(count));
4036 Register limit = count; // Read until src equals this.
4037 __ b(lt, &byte_loop);
4038
4039 if (!dest_always_aligned) {
4040 // Align dest by byte copying. Copies between zero and three bytes.
4041 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
4042 Label dest_aligned;
4043 __ b(eq, &dest_aligned);
4044 __ cmp(scratch4, Operand(2));
4045 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
4046 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
4047 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
4048 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4049 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
4050 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
4051 __ bind(&dest_aligned);
4052 }
4053
4054 Label simple_loop;
4055
4056 __ sub(scratch4, dest, Operand(src));
4057 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
4058 __ b(eq, &simple_loop);
4059 // Shift register is number of bits in a source word that
4060 // must be combined with bits in the next source word in order
4061 // to create a destination word.
4062
4063 // Complex loop for src/dst that are not aligned the same way.
4064 {
4065 Label loop;
4066 __ mov(scratch4, Operand(scratch4, LSL, 3));
4067 Register left_shift = scratch4;
4068 __ and_(src, src, Operand(~3)); // Round down to load previous word.
4069 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
4070 // Store the "shift" most significant bits of scratch in the least
4071 // signficant bits (i.e., shift down by (32-shift)).
4072 __ rsb(scratch2, left_shift, Operand(32));
4073 Register right_shift = scratch2;
4074 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
4075
4076 __ bind(&loop);
4077 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
4078 __ sub(scratch5, limit, Operand(dest));
4079 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
4080 __ str(scratch1, MemOperand(dest, 4, PostIndex));
4081 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
4082 // Loop if four or more bytes left to copy.
4083 // Compare to eight, because we did the subtract before increasing dst.
4084 __ sub(scratch5, scratch5, Operand(8), SetCC);
4085 __ b(ge, &loop);
4086 }
4087 // There is now between zero and three bytes left to copy (negative that
4088 // number is in scratch5), and between one and three bytes already read into
4089 // scratch1 (eight times that number in scratch4). We may have read past
4090 // the end of the string, but because objects are aligned, we have not read
4091 // past the end of the object.
4092 // Find the minimum of remaining characters to move and preloaded characters
4093 // and write those as bytes.
4094 __ add(scratch5, scratch5, Operand(4), SetCC);
4095 __ b(eq, &done);
4096 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
4097 // Move minimum of bytes read and bytes left to copy to scratch4.
4098 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
4099 // Between one and three (value in scratch5) characters already read into
4100 // scratch ready to write.
4101 __ cmp(scratch5, Operand(2));
4102 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4103 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
4104 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
4105 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
4106 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
4107 // Copy any remaining bytes.
4108 __ b(&byte_loop);
4109
4110 // Simple loop.
4111 // Copy words from src to dst, until less than four bytes left.
4112 // Both src and dest are word aligned.
4113 __ bind(&simple_loop);
4114 {
4115 Label loop;
4116 __ bind(&loop);
4117 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
4118 __ sub(scratch3, limit, Operand(dest));
4119 __ str(scratch1, MemOperand(dest, 4, PostIndex));
4120 // Compare to 8, not 4, because we do the substraction before increasing
4121 // dest.
4122 __ cmp(scratch3, Operand(8));
4123 __ b(ge, &loop);
4124 }
4125
4126 // Copy bytes from src to dst until dst hits limit.
4127 __ bind(&byte_loop);
4128 __ cmp(dest, Operand(limit));
4129 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
4130 __ b(ge, &done);
4131 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4132 __ b(&byte_loop);
4133
4134 __ bind(&done);
4135}
4136
4137
4138void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4139 Register c1,
4140 Register c2,
4141 Register scratch1,
4142 Register scratch2,
4143 Register scratch3,
4144 Register scratch4,
4145 Register scratch5,
4146 Label* not_found) {
4147 // Register scratch3 is the general scratch register in this function.
4148 Register scratch = scratch3;
4149
4150 // Make sure that both characters are not digits as such strings has a
4151 // different hash algorithm. Don't try to look for these in the symbol table.
4152 Label not_array_index;
4153 __ sub(scratch, c1, Operand(static_cast<int>('0')));
4154 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
4155 __ b(hi, &not_array_index);
4156 __ sub(scratch, c2, Operand(static_cast<int>('0')));
4157 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
4158
4159 // If check failed combine both characters into single halfword.
4160 // This is required by the contract of the method: code at the
4161 // not_found branch expects this combination in c1 register
4162 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
4163 __ b(ls, not_found);
4164
4165 __ bind(&not_array_index);
4166 // Calculate the two character string hash.
4167 Register hash = scratch1;
4168 StringHelper::GenerateHashInit(masm, hash, c1);
4169 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
4170 StringHelper::GenerateHashGetHash(masm, hash);
4171
4172 // Collect the two characters in a register.
4173 Register chars = c1;
4174 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
4175
4176 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4177 // hash: hash of two character string.
4178
4179 // Load symbol table
4180 // Load address of first element of the symbol table.
4181 Register symbol_table = c2;
4182 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
4183
4184 // Load undefined value
4185 Register undefined = scratch4;
4186 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
4187
4188 // Calculate capacity mask from the symbol table capacity.
4189 Register mask = scratch2;
4190 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
4191 __ mov(mask, Operand(mask, ASR, 1));
4192 __ sub(mask, mask, Operand(1));
4193
4194 // Calculate untagged address of the first element of the symbol table.
4195 Register first_symbol_table_element = symbol_table;
4196 __ add(first_symbol_table_element, symbol_table,
4197 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
4198
4199 // Registers
4200 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4201 // hash: hash of two character string
4202 // mask: capacity mask
4203 // first_symbol_table_element: address of the first element of
4204 // the symbol table
4205 // scratch: -
4206
4207 // Perform a number of probes in the symbol table.
4208 static const int kProbes = 4;
4209 Label found_in_symbol_table;
4210 Label next_probe[kProbes];
4211 for (int i = 0; i < kProbes; i++) {
4212 Register candidate = scratch5; // Scratch register contains candidate.
4213
4214 // Calculate entry in symbol table.
4215 if (i > 0) {
4216 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
4217 } else {
4218 __ mov(candidate, hash);
4219 }
4220
4221 __ and_(candidate, candidate, Operand(mask));
4222
4223 // Load the entry from the symble table.
4224 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4225 __ ldr(candidate,
4226 MemOperand(first_symbol_table_element,
4227 candidate,
4228 LSL,
4229 kPointerSizeLog2));
4230
4231 // If entry is undefined no string with this hash can be found.
4232 __ cmp(candidate, undefined);
4233 __ b(eq, not_found);
4234
4235 // If length is not 2 the string is not a candidate.
4236 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
4237 __ cmp(scratch, Operand(Smi::FromInt(2)));
4238 __ b(ne, &next_probe[i]);
4239
4240 // Check that the candidate is a non-external ascii string.
4241 __ ldr(scratch, FieldMemOperand(candidate, HeapObject::kMapOffset));
4242 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
4243 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch,
4244 &next_probe[i]);
4245
4246 // Check if the two characters match.
4247 // Assumes that word load is little endian.
4248 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
4249 __ cmp(chars, scratch);
4250 __ b(eq, &found_in_symbol_table);
4251 __ bind(&next_probe[i]);
4252 }
4253
4254 // No matching 2 character string found by probing.
4255 __ jmp(not_found);
4256
4257 // Scratch register contains result when we fall through to here.
4258 Register result = scratch;
4259 __ bind(&found_in_symbol_table);
4260 __ Move(r0, result);
4261}
4262
4263
4264void StringHelper::GenerateHashInit(MacroAssembler* masm,
4265 Register hash,
4266 Register character) {
4267 // hash = character + (character << 10);
4268 __ add(hash, character, Operand(character, LSL, 10));
4269 // hash ^= hash >> 6;
4270 __ eor(hash, hash, Operand(hash, ASR, 6));
4271}
4272
4273
4274void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4275 Register hash,
4276 Register character) {
4277 // hash += character;
4278 __ add(hash, hash, Operand(character));
4279 // hash += hash << 10;
4280 __ add(hash, hash, Operand(hash, LSL, 10));
4281 // hash ^= hash >> 6;
4282 __ eor(hash, hash, Operand(hash, ASR, 6));
4283}
4284
4285
4286void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4287 Register hash) {
4288 // hash += hash << 3;
4289 __ add(hash, hash, Operand(hash, LSL, 3));
4290 // hash ^= hash >> 11;
4291 __ eor(hash, hash, Operand(hash, ASR, 11));
4292 // hash += hash << 15;
4293 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
4294
4295 // if (hash == 0) hash = 27;
4296 __ mov(hash, Operand(27), LeaveCC, nz);
4297}
4298
4299
4300void SubStringStub::Generate(MacroAssembler* masm) {
4301 Label runtime;
4302
4303 // Stack frame on entry.
4304 // lr: return address
4305 // sp[0]: to
4306 // sp[4]: from
4307 // sp[8]: string
4308
4309 // This stub is called from the native-call %_SubString(...), so
4310 // nothing can be assumed about the arguments. It is tested that:
4311 // "string" is a sequential string,
4312 // both "from" and "to" are smis, and
4313 // 0 <= from <= to <= string.length.
4314 // If any of these assumptions fail, we call the runtime system.
4315
4316 static const int kToOffset = 0 * kPointerSize;
4317 static const int kFromOffset = 1 * kPointerSize;
4318 static const int kStringOffset = 2 * kPointerSize;
4319
4320
4321 // Check bounds and smi-ness.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004322 Register to = r6;
4323 Register from = r7;
4324 __ Ldrd(to, from, MemOperand(sp, kToOffset));
4325 STATIC_ASSERT(kFromOffset == kToOffset + 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004326 STATIC_ASSERT(kSmiTag == 0);
4327 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4328 // I.e., arithmetic shift right by one un-smi-tags.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004329 __ mov(r2, Operand(to, ASR, 1), SetCC);
4330 __ mov(r3, Operand(from, ASR, 1), SetCC, cc);
4331 // If either to or from had the smi tag bit set, then carry is set now.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004332 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
4333 __ b(mi, &runtime); // From is negative.
4334
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004335 // Both to and from are smis.
4336
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004337 __ sub(r2, r2, Operand(r3), SetCC);
4338 __ b(mi, &runtime); // Fail if from > to.
4339 // Special handling of sub-strings of length 1 and 2. One character strings
4340 // are handled in the runtime system (looked up in the single character
4341 // cache). Two character strings are looked for in the symbol cache.
4342 __ cmp(r2, Operand(2));
4343 __ b(lt, &runtime);
4344
4345 // r2: length
4346 // r3: from index (untaged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004347 // r6 (a.k.a. to): to (smi)
4348 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004349
4350 // Make sure first argument is a sequential (or flat) string.
4351 __ ldr(r5, MemOperand(sp, kStringOffset));
4352 STATIC_ASSERT(kSmiTag == 0);
4353 __ tst(r5, Operand(kSmiTagMask));
4354 __ b(eq, &runtime);
4355 Condition is_string = masm->IsObjectStringType(r5, r1);
4356 __ b(NegateCondition(is_string), &runtime);
4357
4358 // r1: instance type
4359 // r2: length
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004360 // r3: from index (untagged smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004361 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004362 // r6 (a.k.a. to): to (smi)
4363 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004364 Label seq_string;
4365 __ and_(r4, r1, Operand(kStringRepresentationMask));
4366 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
4367 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
4368 __ cmp(r4, Operand(kConsStringTag));
4369 __ b(gt, &runtime); // External strings go to runtime.
4370 __ b(lt, &seq_string); // Sequential strings are handled directly.
4371
4372 // Cons string. Try to recurse (once) on the first substring.
4373 // (This adds a little more generality than necessary to handle flattened
4374 // cons strings, but not much).
4375 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
4376 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
4377 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4378 __ tst(r1, Operand(kStringRepresentationMask));
4379 STATIC_ASSERT(kSeqStringTag == 0);
4380 __ b(ne, &runtime); // Cons and External strings go to runtime.
4381
4382 // Definitly a sequential string.
4383 __ bind(&seq_string);
4384
4385 // r1: instance type.
4386 // r2: length
4387 // r3: from index (untaged smi)
4388 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004389 // r6 (a.k.a. to): to (smi)
4390 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004391 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004392 __ cmp(r4, Operand(to));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004393 __ b(lt, &runtime); // Fail if to > length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004394 to = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004395
4396 // r1: instance type.
4397 // r2: result string length.
4398 // r3: from index (untaged smi)
4399 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004400 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004401 // Check for flat ascii string.
4402 Label non_ascii_flat;
4403 __ tst(r1, Operand(kStringEncodingMask));
4404 STATIC_ASSERT(kTwoByteStringTag == 0);
4405 __ b(eq, &non_ascii_flat);
4406
4407 Label result_longer_than_two;
4408 __ cmp(r2, Operand(2));
4409 __ b(gt, &result_longer_than_two);
4410
4411 // Sub string of length 2 requested.
4412 // Get the two characters forming the sub string.
4413 __ add(r5, r5, Operand(r3));
4414 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
4415 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
4416
4417 // Try to lookup two character string in symbol table.
4418 Label make_two_character_string;
4419 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4420 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
4421 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4422 __ add(sp, sp, Operand(3 * kPointerSize));
4423 __ Ret();
4424
4425 // r2: result string length.
4426 // r3: two characters combined into halfword in little endian byte order.
4427 __ bind(&make_two_character_string);
4428 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
4429 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4430 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4431 __ add(sp, sp, Operand(3 * kPointerSize));
4432 __ Ret();
4433
4434 __ bind(&result_longer_than_two);
4435
4436 // Allocate the result.
4437 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
4438
4439 // r0: result string.
4440 // r2: result string length.
4441 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004442 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004443 // Locate first character of result.
4444 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4445 // Locate 'from' character of string.
4446 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004447 __ add(r5, r5, Operand(from, ASR, 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004448
4449 // r0: result string.
4450 // r1: first character of result string.
4451 // r2: result string length.
4452 // r5: first character of sub string to copy.
4453 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
4454 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
4455 COPY_ASCII | DEST_ALWAYS_ALIGNED);
4456 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4457 __ add(sp, sp, Operand(3 * kPointerSize));
4458 __ Ret();
4459
4460 __ bind(&non_ascii_flat);
4461 // r2: result string length.
4462 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004463 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004464 // Check for flat two byte string.
4465
4466 // Allocate the result.
4467 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
4468
4469 // r0: result string.
4470 // r2: result string length.
4471 // r5: string.
4472 // Locate first character of result.
4473 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4474 // Locate 'from' character of string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004475 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004476 // As "from" is a smi it is 2 times the value which matches the size of a two
4477 // byte character.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004478 __ add(r5, r5, Operand(from));
4479 from = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004480
4481 // r0: result string.
4482 // r1: first character of result.
4483 // r2: result length.
4484 // r5: first character of string to copy.
4485 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004486 StringHelper::GenerateCopyCharactersLong(
4487 masm, r1, r5, r2, r3, r4, r6, r7, r9, DEST_ALWAYS_ALIGNED);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004488 __ IncrementCounter(&Counters::sub_string_native, 1, r3, r4);
4489 __ add(sp, sp, Operand(3 * kPointerSize));
4490 __ Ret();
4491
4492 // Just jump to runtime to create the sub string.
4493 __ bind(&runtime);
4494 __ TailCallRuntime(Runtime::kSubString, 3, 1);
4495}
4496
4497
4498void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
4499 Register left,
4500 Register right,
4501 Register scratch1,
4502 Register scratch2,
4503 Register scratch3,
4504 Register scratch4) {
4505 Label compare_lengths;
4506 // Find minimum length and length difference.
4507 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
4508 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
4509 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
4510 Register length_delta = scratch3;
4511 __ mov(scratch1, scratch2, LeaveCC, gt);
4512 Register min_length = scratch1;
4513 STATIC_ASSERT(kSmiTag == 0);
4514 __ tst(min_length, Operand(min_length));
4515 __ b(eq, &compare_lengths);
4516
4517 // Untag smi.
4518 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
4519
4520 // Setup registers so that we only need to increment one register
4521 // in the loop.
4522 __ add(scratch2, min_length,
4523 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4524 __ add(left, left, Operand(scratch2));
4525 __ add(right, right, Operand(scratch2));
4526 // Registers left and right points to the min_length character of strings.
4527 __ rsb(min_length, min_length, Operand(-1));
4528 Register index = min_length;
4529 // Index starts at -min_length.
4530
4531 {
4532 // Compare loop.
4533 Label loop;
4534 __ bind(&loop);
4535 // Compare characters.
4536 __ add(index, index, Operand(1), SetCC);
4537 __ ldrb(scratch2, MemOperand(left, index), ne);
4538 __ ldrb(scratch4, MemOperand(right, index), ne);
4539 // Skip to compare lengths with eq condition true.
4540 __ b(eq, &compare_lengths);
4541 __ cmp(scratch2, scratch4);
4542 __ b(eq, &loop);
4543 // Fallthrough with eq condition false.
4544 }
4545 // Compare lengths - strings up to min-length are equal.
4546 __ bind(&compare_lengths);
4547 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
4548 // Use zero length_delta as result.
4549 __ mov(r0, Operand(length_delta), SetCC, eq);
4550 // Fall through to here if characters compare not-equal.
4551 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
4552 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
4553 __ Ret();
4554}
4555
4556
4557void StringCompareStub::Generate(MacroAssembler* masm) {
4558 Label runtime;
4559
4560 // Stack frame on entry.
4561 // sp[0]: right string
4562 // sp[4]: left string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004563 __ Ldrd(r0 , r1, MemOperand(sp)); // Load right in r0, left in r1.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004564
4565 Label not_same;
4566 __ cmp(r0, r1);
4567 __ b(ne, &not_same);
4568 STATIC_ASSERT(EQUAL == 0);
4569 STATIC_ASSERT(kSmiTag == 0);
4570 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
4571 __ IncrementCounter(&Counters::string_compare_native, 1, r1, r2);
4572 __ add(sp, sp, Operand(2 * kPointerSize));
4573 __ Ret();
4574
4575 __ bind(&not_same);
4576
4577 // Check that both objects are sequential ascii strings.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004578 __ JumpIfNotBothSequentialAsciiStrings(r1, r0, r2, r3, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004579
4580 // Compare flat ascii strings natively. Remove arguments from stack first.
4581 __ IncrementCounter(&Counters::string_compare_native, 1, r2, r3);
4582 __ add(sp, sp, Operand(2 * kPointerSize));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004583 GenerateCompareFlatAsciiStrings(masm, r1, r0, r2, r3, r4, r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004584
4585 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
4586 // tagged as a small integer.
4587 __ bind(&runtime);
4588 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
4589}
4590
4591
4592void StringAddStub::Generate(MacroAssembler* masm) {
4593 Label string_add_runtime;
4594 // Stack on entry:
4595 // sp[0]: second argument.
4596 // sp[4]: first argument.
4597
4598 // Load the two arguments.
4599 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
4600 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
4601
4602 // Make sure that both arguments are strings if not known in advance.
4603 if (string_check_) {
4604 STATIC_ASSERT(kSmiTag == 0);
4605 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
4606 // Load instance types.
4607 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4608 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4609 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4610 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4611 STATIC_ASSERT(kStringTag == 0);
4612 // If either is not a string, go to runtime.
4613 __ tst(r4, Operand(kIsNotStringMask));
4614 __ tst(r5, Operand(kIsNotStringMask), eq);
4615 __ b(ne, &string_add_runtime);
4616 }
4617
4618 // Both arguments are strings.
4619 // r0: first string
4620 // r1: second string
4621 // r4: first string instance type (if string_check_)
4622 // r5: second string instance type (if string_check_)
4623 {
4624 Label strings_not_empty;
4625 // Check if either of the strings are empty. In that case return the other.
4626 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
4627 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
4628 STATIC_ASSERT(kSmiTag == 0);
4629 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
4630 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
4631 STATIC_ASSERT(kSmiTag == 0);
4632 // Else test if second string is empty.
4633 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
4634 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
4635
4636 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4637 __ add(sp, sp, Operand(2 * kPointerSize));
4638 __ Ret();
4639
4640 __ bind(&strings_not_empty);
4641 }
4642
4643 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
4644 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
4645 // Both strings are non-empty.
4646 // r0: first string
4647 // r1: second string
4648 // r2: length of first string
4649 // r3: length of second string
4650 // r4: first string instance type (if string_check_)
4651 // r5: second string instance type (if string_check_)
4652 // Look at the length of the result of adding the two strings.
4653 Label string_add_flat_result, longer_than_two;
4654 // Adding two lengths can't overflow.
4655 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
4656 __ add(r6, r2, Operand(r3));
4657 // Use the runtime system when adding two one character strings, as it
4658 // contains optimizations for this specific case using the symbol table.
4659 __ cmp(r6, Operand(2));
4660 __ b(ne, &longer_than_two);
4661
4662 // Check that both strings are non-external ascii strings.
4663 if (!string_check_) {
4664 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4665 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4666 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4667 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4668 }
4669 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
4670 &string_add_runtime);
4671
4672 // Get the two characters forming the sub string.
4673 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4674 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
4675
4676 // Try to lookup two character string in symbol table. If it is not found
4677 // just allocate a new one.
4678 Label make_two_character_string;
4679 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4680 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
4681 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4682 __ add(sp, sp, Operand(2 * kPointerSize));
4683 __ Ret();
4684
4685 __ bind(&make_two_character_string);
4686 // Resulting string has length 2 and first chars of two strings
4687 // are combined into single halfword in r2 register.
4688 // So we can fill resulting string without two loops by a single
4689 // halfword store instruction (which assumes that processor is
4690 // in a little endian mode)
4691 __ mov(r6, Operand(2));
4692 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
4693 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
4694 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4695 __ add(sp, sp, Operand(2 * kPointerSize));
4696 __ Ret();
4697
4698 __ bind(&longer_than_two);
4699 // Check if resulting string will be flat.
4700 __ cmp(r6, Operand(String::kMinNonFlatLength));
4701 __ b(lt, &string_add_flat_result);
4702 // Handle exceptionally long strings in the runtime system.
4703 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
4704 ASSERT(IsPowerOf2(String::kMaxLength + 1));
4705 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
4706 __ cmp(r6, Operand(String::kMaxLength + 1));
4707 __ b(hs, &string_add_runtime);
4708
4709 // If result is not supposed to be flat, allocate a cons string object.
4710 // If both strings are ascii the result is an ascii cons string.
4711 if (!string_check_) {
4712 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4713 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4714 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4715 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4716 }
4717 Label non_ascii, allocated, ascii_data;
4718 STATIC_ASSERT(kTwoByteStringTag == 0);
4719 __ tst(r4, Operand(kStringEncodingMask));
4720 __ tst(r5, Operand(kStringEncodingMask), ne);
4721 __ b(eq, &non_ascii);
4722
4723 // Allocate an ASCII cons string.
4724 __ bind(&ascii_data);
4725 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
4726 __ bind(&allocated);
4727 // Fill the fields of the cons string.
4728 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
4729 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
4730 __ mov(r0, Operand(r7));
4731 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4732 __ add(sp, sp, Operand(2 * kPointerSize));
4733 __ Ret();
4734
4735 __ bind(&non_ascii);
4736 // At least one of the strings is two-byte. Check whether it happens
4737 // to contain only ascii characters.
4738 // r4: first instance type.
4739 // r5: second instance type.
4740 __ tst(r4, Operand(kAsciiDataHintMask));
4741 __ tst(r5, Operand(kAsciiDataHintMask), ne);
4742 __ b(ne, &ascii_data);
4743 __ eor(r4, r4, Operand(r5));
4744 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4745 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
4746 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
4747 __ b(eq, &ascii_data);
4748
4749 // Allocate a two byte cons string.
4750 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
4751 __ jmp(&allocated);
4752
4753 // Handle creating a flat result. First check that both strings are
4754 // sequential and that they have the same encoding.
4755 // r0: first string
4756 // r1: second string
4757 // r2: length of first string
4758 // r3: length of second string
4759 // r4: first string instance type (if string_check_)
4760 // r5: second string instance type (if string_check_)
4761 // r6: sum of lengths.
4762 __ bind(&string_add_flat_result);
4763 if (!string_check_) {
4764 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
4765 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
4766 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
4767 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
4768 }
4769 // Check that both strings are sequential.
4770 STATIC_ASSERT(kSeqStringTag == 0);
4771 __ tst(r4, Operand(kStringRepresentationMask));
4772 __ tst(r5, Operand(kStringRepresentationMask), eq);
4773 __ b(ne, &string_add_runtime);
4774 // Now check if both strings have the same encoding (ASCII/Two-byte).
4775 // r0: first string.
4776 // r1: second string.
4777 // r2: length of first string.
4778 // r3: length of second string.
4779 // r6: sum of lengths..
4780 Label non_ascii_string_add_flat_result;
4781 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
4782 __ eor(r7, r4, Operand(r5));
4783 __ tst(r7, Operand(kStringEncodingMask));
4784 __ b(ne, &string_add_runtime);
4785 // And see if it's ASCII or two-byte.
4786 __ tst(r4, Operand(kStringEncodingMask));
4787 __ b(eq, &non_ascii_string_add_flat_result);
4788
4789 // Both strings are sequential ASCII strings. We also know that they are
4790 // short (since the sum of the lengths is less than kMinNonFlatLength).
4791 // r6: length of resulting flat string
4792 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
4793 // Locate first character of result.
4794 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4795 // Locate first character of first argument.
4796 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4797 // r0: first character of first string.
4798 // r1: second string.
4799 // r2: length of first string.
4800 // r3: length of second string.
4801 // r6: first character of result.
4802 // r7: result string.
4803 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
4804
4805 // Load second argument and locate first character.
4806 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4807 // r1: first character of second string.
4808 // r3: length of second string.
4809 // r6: next character of result.
4810 // r7: result string.
4811 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
4812 __ mov(r0, Operand(r7));
4813 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4814 __ add(sp, sp, Operand(2 * kPointerSize));
4815 __ Ret();
4816
4817 __ bind(&non_ascii_string_add_flat_result);
4818 // Both strings are sequential two byte strings.
4819 // r0: first string.
4820 // r1: second string.
4821 // r2: length of first string.
4822 // r3: length of second string.
4823 // r6: sum of length of strings.
4824 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
4825 // r0: first string.
4826 // r1: second string.
4827 // r2: length of first string.
4828 // r3: length of second string.
4829 // r7: result string.
4830
4831 // Locate first character of result.
4832 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4833 // Locate first character of first argument.
4834 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4835
4836 // r0: first character of first string.
4837 // r1: second string.
4838 // r2: length of first string.
4839 // r3: length of second string.
4840 // r6: first character of result.
4841 // r7: result string.
4842 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
4843
4844 // Locate first character of second argument.
4845 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4846
4847 // r1: first character of second string.
4848 // r3: length of second string.
4849 // r6: next character of result (after copy of first string).
4850 // r7: result string.
4851 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
4852
4853 __ mov(r0, Operand(r7));
4854 __ IncrementCounter(&Counters::string_add_native, 1, r2, r3);
4855 __ add(sp, sp, Operand(2 * kPointerSize));
4856 __ Ret();
4857
4858 // Just jump to runtime to add the two strings.
4859 __ bind(&string_add_runtime);
4860 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
4861}
4862
4863
Ben Murdochb0fe1622011-05-05 13:52:32 +01004864void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
4865 ASSERT(state_ == CompareIC::SMIS);
4866 Label miss;
4867 __ orr(r2, r1, r0);
4868 __ tst(r2, Operand(kSmiTagMask));
4869 __ b(ne, &miss);
4870
4871 if (GetCondition() == eq) {
4872 // For equality we do not care about the sign of the result.
4873 __ sub(r0, r0, r1, SetCC);
4874 } else {
4875 __ sub(r1, r1, r0, SetCC);
4876 // Correct sign of result in case of overflow.
4877 __ rsb(r1, r1, Operand(0), SetCC, vs);
4878 __ mov(r0, r1);
4879 }
4880 __ Ret();
4881
4882 __ bind(&miss);
4883 GenerateMiss(masm);
4884}
4885
4886
4887void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
4888 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
4889
4890 Label generic_stub;
4891 Label unordered;
4892 Label miss;
4893 __ and_(r2, r1, Operand(r0));
4894 __ tst(r2, Operand(kSmiTagMask));
4895 __ b(eq, &generic_stub);
4896
4897 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
4898 __ b(ne, &miss);
4899 __ CompareObjectType(r1, r2, r2, HEAP_NUMBER_TYPE);
4900 __ b(ne, &miss);
4901
4902 // Inlining the double comparison and falling back to the general compare
4903 // stub if NaN is involved or VFP3 is unsupported.
4904 if (CpuFeatures::IsSupported(VFP3)) {
4905 CpuFeatures::Scope scope(VFP3);
4906
4907 // Load left and right operand
4908 __ sub(r2, r1, Operand(kHeapObjectTag));
4909 __ vldr(d0, r2, HeapNumber::kValueOffset);
4910 __ sub(r2, r0, Operand(kHeapObjectTag));
4911 __ vldr(d1, r2, HeapNumber::kValueOffset);
4912
4913 // Compare operands
4914 __ vcmp(d0, d1);
4915 __ vmrs(pc); // Move vector status bits to normal status bits.
4916
4917 // Don't base result on status bits when a NaN is involved.
4918 __ b(vs, &unordered);
4919
4920 // Return a result of -1, 0, or 1, based on status bits.
4921 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
4922 __ mov(r0, Operand(LESS), LeaveCC, lt);
4923 __ mov(r0, Operand(GREATER), LeaveCC, gt);
4924 __ Ret();
4925
4926 __ bind(&unordered);
4927 }
4928
4929 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS, r1, r0);
4930 __ bind(&generic_stub);
4931 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
4932
4933 __ bind(&miss);
4934 GenerateMiss(masm);
4935}
4936
4937
4938void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
4939 ASSERT(state_ == CompareIC::OBJECTS);
4940 Label miss;
4941 __ and_(r2, r1, Operand(r0));
4942 __ tst(r2, Operand(kSmiTagMask));
4943 __ b(eq, &miss);
4944
4945 __ CompareObjectType(r0, r2, r2, JS_OBJECT_TYPE);
4946 __ b(ne, &miss);
4947 __ CompareObjectType(r1, r2, r2, JS_OBJECT_TYPE);
4948 __ b(ne, &miss);
4949
4950 ASSERT(GetCondition() == eq);
4951 __ sub(r0, r0, Operand(r1));
4952 __ Ret();
4953
4954 __ bind(&miss);
4955 GenerateMiss(masm);
4956}
4957
4958
4959void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
4960 __ Push(r1, r0);
4961 __ push(lr);
4962
4963 // Call the runtime system in a fresh internal frame.
4964 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss));
4965 __ EnterInternalFrame();
4966 __ Push(r1, r0);
4967 __ mov(ip, Operand(Smi::FromInt(op_)));
4968 __ push(ip);
4969 __ CallExternalReference(miss, 3);
4970 __ LeaveInternalFrame();
4971 // Compute the entry point of the rewritten stub.
4972 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
4973 // Restore registers.
4974 __ pop(lr);
4975 __ pop(r0);
4976 __ pop(r1);
4977 __ Jump(r2);
4978}
4979
4980
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004981#undef __
4982
4983} } // namespace v8::internal
4984
4985#endif // V8_TARGET_ARCH_ARM