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