Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(161)

Side by Side Diff: src/arm/code-stubs-arm.cc

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

Powered by Google App Engine
This is Rietveld 408576698