blob: 441adfe3af3eeeae6951cfa44176ad72e885b5ba [file] [log] [blame]
Steve Block1e0659c2011-05-24 12:43:12 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002// 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,
Steve Block1e0659c2011-05-24 12:43:12 +010044 Condition cond,
Kristian Monsen80d68ea2010-09-08 11:05:35 +010045 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);
Steve Block1e0659c2011-05-24 12:43:12 +010052static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cond);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010053static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
54 Register lhs,
55 Register rhs);
56
57
Steve Block1e0659c2011-05-24 12:43:12 +010058void ToNumberStub::Generate(MacroAssembler* masm) {
59 // The ToNumber stub takes one argument in eax.
60 Label check_heap_number, call_builtin;
61 __ tst(r0, Operand(kSmiTagMask));
62 __ b(ne, &check_heap_number);
63 __ Ret();
64
65 __ bind(&check_heap_number);
66 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
67 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
68 __ cmp(r1, ip);
69 __ b(ne, &call_builtin);
70 __ Ret();
71
72 __ bind(&call_builtin);
73 __ push(r0);
74 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_JS);
75}
76
77
Kristian Monsen80d68ea2010-09-08 11:05:35 +010078void FastNewClosureStub::Generate(MacroAssembler* masm) {
79 // Create a new closure from the given function info in new
80 // space. Set the context to the current context in cp.
81 Label gc;
82
83 // Pop the function info from the stack.
84 __ pop(r3);
85
86 // Attempt to allocate new JSFunction in new space.
87 __ AllocateInNewSpace(JSFunction::kSize,
88 r0,
89 r1,
90 r2,
91 &gc,
92 TAG_OBJECT);
93
Steve Block44f0eee2011-05-26 01:26:41 +010094 int map_index = strict_mode_ == kStrictMode
95 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
96 : Context::FUNCTION_MAP_INDEX;
97
Kristian Monsen80d68ea2010-09-08 11:05:35 +010098 // Compute the function map in the current global context and set that
99 // as the map of the allocated object.
100 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
101 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100102 __ ldr(r2, MemOperand(r2, Context::SlotOffset(map_index)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100103 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
104
105 // Initialize the rest of the function. We don't have to update the
106 // write barrier because the allocated object is in new space.
107 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
108 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100109 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100110 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
111 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
112 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
113 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
114 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
115 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100116 __ str(r4, FieldMemOperand(r0, JSFunction::kNextFunctionLinkOffset));
117
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100118
119 // Initialize the code pointer in the function to be the one
120 // found in the shared function info object.
121 __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset));
122 __ add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
123 __ str(r3, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
124
125 // Return result. The argument function info has been popped already.
126 __ Ret();
127
128 // Create a new closure through the slower runtime call.
129 __ bind(&gc);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800130 __ LoadRoot(r4, Heap::kFalseValueRootIndex);
131 __ Push(cp, r3, r4);
132 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100133}
134
135
136void FastNewContextStub::Generate(MacroAssembler* masm) {
137 // Try to allocate the context in new space.
138 Label gc;
139 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
140
141 // Attempt to allocate the context in new space.
142 __ AllocateInNewSpace(FixedArray::SizeFor(length),
143 r0,
144 r1,
145 r2,
146 &gc,
147 TAG_OBJECT);
148
149 // Load the function from the stack.
150 __ ldr(r3, MemOperand(sp, 0));
151
152 // Setup the object header.
153 __ LoadRoot(r2, Heap::kContextMapRootIndex);
154 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
155 __ mov(r2, Operand(Smi::FromInt(length)));
156 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
157
158 // Setup the fixed slots.
159 __ mov(r1, Operand(Smi::FromInt(0)));
160 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
161 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
162 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
163 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
164
165 // Copy the global object from the surrounding context.
166 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
167 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
168
169 // Initialize the rest of the slots to undefined.
170 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
171 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
172 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
173 }
174
175 // Remove the on-stack argument and return.
176 __ mov(cp, r0);
177 __ pop();
178 __ Ret();
179
180 // Need to collect. Call into runtime system.
181 __ bind(&gc);
182 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
183}
184
185
186void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
187 // Stack layout on entry:
188 //
189 // [sp]: constant elements.
190 // [sp + kPointerSize]: literal index.
191 // [sp + (2 * kPointerSize)]: literals array.
192
193 // All sizes here are multiples of kPointerSize.
194 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
195 int size = JSArray::kSize + elements_size;
196
197 // Load boilerplate object into r3 and check if we need to create a
198 // boilerplate.
199 Label slow_case;
200 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
201 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
202 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
203 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
204 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
205 __ cmp(r3, ip);
206 __ b(eq, &slow_case);
207
208 if (FLAG_debug_code) {
209 const char* message;
210 Heap::RootListIndex expected_map_index;
211 if (mode_ == CLONE_ELEMENTS) {
212 message = "Expected (writable) fixed array";
213 expected_map_index = Heap::kFixedArrayMapRootIndex;
214 } else {
215 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
216 message = "Expected copy-on-write fixed array";
217 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
218 }
219 __ push(r3);
220 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
221 __ ldr(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
222 __ LoadRoot(ip, expected_map_index);
223 __ cmp(r3, ip);
224 __ Assert(eq, message);
225 __ pop(r3);
226 }
227
228 // Allocate both the JS array and the elements array in one big
229 // allocation. This avoids multiple limit checks.
230 __ AllocateInNewSpace(size,
231 r0,
232 r1,
233 r2,
234 &slow_case,
235 TAG_OBJECT);
236
237 // Copy the JS array part.
238 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
239 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
240 __ ldr(r1, FieldMemOperand(r3, i));
241 __ str(r1, FieldMemOperand(r0, i));
242 }
243 }
244
245 if (length_ > 0) {
246 // Get hold of the elements array of the boilerplate and setup the
247 // elements pointer in the resulting object.
248 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
249 __ add(r2, r0, Operand(JSArray::kSize));
250 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
251
252 // Copy the elements array.
253 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
254 }
255
256 // Return and remove the on-stack parameters.
257 __ add(sp, sp, Operand(3 * kPointerSize));
258 __ Ret();
259
260 __ bind(&slow_case);
261 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
262}
263
264
265// Takes a Smi and converts to an IEEE 64 bit floating point value in two
266// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
267// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
268// scratch register. Destroys the source register. No GC occurs during this
269// stub so you don't have to set up the frame.
270class ConvertToDoubleStub : public CodeStub {
271 public:
272 ConvertToDoubleStub(Register result_reg_1,
273 Register result_reg_2,
274 Register source_reg,
275 Register scratch_reg)
276 : result1_(result_reg_1),
277 result2_(result_reg_2),
278 source_(source_reg),
279 zeros_(scratch_reg) { }
280
281 private:
282 Register result1_;
283 Register result2_;
284 Register source_;
285 Register zeros_;
286
287 // Minor key encoding in 16 bits.
288 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
289 class OpBits: public BitField<Token::Value, 2, 14> {};
290
291 Major MajorKey() { return ConvertToDouble; }
292 int MinorKey() {
293 // Encode the parameters in a unique 16 bit value.
294 return result1_.code() +
295 (result2_.code() << 4) +
296 (source_.code() << 8) +
297 (zeros_.code() << 12);
298 }
299
300 void Generate(MacroAssembler* masm);
301
302 const char* GetName() { return "ConvertToDoubleStub"; }
303
304#ifdef DEBUG
305 void Print() { PrintF("ConvertToDoubleStub\n"); }
306#endif
307};
308
309
310void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
311#ifndef BIG_ENDIAN_FLOATING_POINT
312 Register exponent = result1_;
313 Register mantissa = result2_;
314#else
315 Register exponent = result2_;
316 Register mantissa = result1_;
317#endif
318 Label not_special;
319 // Convert from Smi to integer.
320 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
321 // Move sign bit from source to destination. This works because the sign bit
322 // in the exponent word of the double has the same position and polarity as
323 // the 2's complement sign bit in a Smi.
324 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
325 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
326 // Subtract from 0 if source was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100327 __ rsb(source_, source_, Operand(0, RelocInfo::NONE), LeaveCC, ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100328
329 // We have -1, 0 or 1, which we treat specially. Register source_ contains
330 // absolute value: it is either equal to 1 (special case of -1 and 1),
331 // greater than 1 (not a special case) or less than 1 (special case of 0).
332 __ cmp(source_, Operand(1));
333 __ b(gt, &not_special);
334
335 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
336 static const uint32_t exponent_word_for_1 =
337 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
338 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
339 // 1, 0 and -1 all have 0 for the second word.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100340 __ mov(mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100341 __ Ret();
342
343 __ bind(&not_special);
344 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
345 // Gets the wrong answer for 0, but we already checked for that case above.
346 __ CountLeadingZeros(zeros_, source_, mantissa);
347 // Compute exponent and or it into the exponent register.
348 // We use mantissa as a scratch register here. Use a fudge factor to
349 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
350 // that fit in the ARM's constant field.
351 int fudge = 0x400;
352 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
353 __ add(mantissa, mantissa, Operand(fudge));
354 __ orr(exponent,
355 exponent,
356 Operand(mantissa, LSL, HeapNumber::kExponentShift));
357 // Shift up the source chopping the top bit off.
358 __ add(zeros_, zeros_, Operand(1));
359 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
360 __ mov(source_, Operand(source_, LSL, zeros_));
361 // Compute lower part of fraction (last 12 bits).
362 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
363 // And the top (top 20 bits).
364 __ orr(exponent,
365 exponent,
366 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
367 __ Ret();
368}
369
370
Steve Block1e0659c2011-05-24 12:43:12 +0100371class FloatingPointHelper : public AllStatic {
372 public:
373
374 enum Destination {
375 kVFPRegisters,
376 kCoreRegisters
377 };
378
379
380 // Loads smis from r0 and r1 (right and left in binary operations) into
381 // floating point registers. Depending on the destination the values ends up
382 // either d7 and d6 or in r2/r3 and r0/r1 respectively. If the destination is
383 // floating point registers VFP3 must be supported. If core registers are
384 // requested when VFP3 is supported d6 and d7 will be scratched.
385 static void LoadSmis(MacroAssembler* masm,
386 Destination destination,
387 Register scratch1,
388 Register scratch2);
389
390 // Loads objects from r0 and r1 (right and left in binary operations) into
391 // floating point registers. Depending on the destination the values ends up
392 // either d7 and d6 or in r2/r3 and r0/r1 respectively. If the destination is
393 // floating point registers VFP3 must be supported. If core registers are
394 // requested when VFP3 is supported d6 and d7 will still be scratched. If
395 // either r0 or r1 is not a number (not smi and not heap number object) the
396 // not_number label is jumped to with r0 and r1 intact.
397 static void LoadOperands(MacroAssembler* masm,
398 FloatingPointHelper::Destination destination,
399 Register heap_number_map,
400 Register scratch1,
401 Register scratch2,
402 Label* not_number);
403
Steve Block44f0eee2011-05-26 01:26:41 +0100404 // Convert the smi or heap number in object to an int32 using the rules
405 // for ToInt32 as described in ECMAScript 9.5.: the value is truncated
406 // and brought into the range -2^31 .. +2^31 - 1.
407 static void ConvertNumberToInt32(MacroAssembler* masm,
408 Register object,
409 Register dst,
410 Register heap_number_map,
411 Register scratch1,
412 Register scratch2,
413 Register scratch3,
414 DwVfpRegister double_scratch,
415 Label* not_int32);
Steve Block1e0659c2011-05-24 12:43:12 +0100416
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100417 // Load the number from object into double_dst in the double format.
418 // Control will jump to not_int32 if the value cannot be exactly represented
419 // by a 32-bit integer.
420 // Floating point value in the 32-bit integer range that are not exact integer
421 // won't be loaded.
422 static void LoadNumberAsInt32Double(MacroAssembler* masm,
423 Register object,
424 Destination destination,
425 DwVfpRegister double_dst,
426 Register dst1,
427 Register dst2,
428 Register heap_number_map,
429 Register scratch1,
430 Register scratch2,
431 SwVfpRegister single_scratch,
432 Label* not_int32);
433
434 // Loads the number from object into dst as a 32-bit integer.
435 // Control will jump to not_int32 if the object cannot be exactly represented
436 // by a 32-bit integer.
437 // Floating point value in the 32-bit integer range that are not exact integer
438 // won't be converted.
439 // scratch3 is not used when VFP3 is supported.
440 static void LoadNumberAsInt32(MacroAssembler* masm,
441 Register object,
442 Register dst,
443 Register heap_number_map,
444 Register scratch1,
445 Register scratch2,
446 Register scratch3,
447 DwVfpRegister double_scratch,
448 Label* not_int32);
449
450 // Generate non VFP3 code to check if a double can be exactly represented by a
451 // 32-bit integer. This does not check for 0 or -0, which need
452 // to be checked for separately.
453 // Control jumps to not_int32 if the value is not a 32-bit integer, and falls
454 // through otherwise.
455 // src1 and src2 will be cloberred.
456 //
457 // Expected input:
458 // - src1: higher (exponent) part of the double value.
459 // - src2: lower (mantissa) part of the double value.
460 // Output status:
461 // - dst: 32 higher bits of the mantissa. (mantissa[51:20])
462 // - src2: contains 1.
463 // - other registers are clobbered.
464 static void DoubleIs32BitInteger(MacroAssembler* masm,
465 Register src1,
466 Register src2,
467 Register dst,
468 Register scratch,
469 Label* not_int32);
470
471 // Generates code to call a C function to do a double operation using core
472 // registers. (Used when VFP3 is not supported.)
473 // This code never falls through, but returns with a heap number containing
474 // the result in r0.
475 // Register heapnumber_result must be a heap number in which the
476 // result of the operation will be stored.
477 // Requires the following layout on entry:
478 // r0: Left value (least significant part of mantissa).
479 // r1: Left value (sign, exponent, top of mantissa).
480 // r2: Right value (least significant part of mantissa).
481 // r3: Right value (sign, exponent, top of mantissa).
482 static void CallCCodeForDoubleOperation(MacroAssembler* masm,
483 Token::Value op,
484 Register heap_number_result,
485 Register scratch);
486
Steve Block1e0659c2011-05-24 12:43:12 +0100487 private:
488 static void LoadNumber(MacroAssembler* masm,
489 FloatingPointHelper::Destination destination,
490 Register object,
491 DwVfpRegister dst,
492 Register dst1,
493 Register dst2,
494 Register heap_number_map,
495 Register scratch1,
496 Register scratch2,
497 Label* not_number);
498};
499
500
501void FloatingPointHelper::LoadSmis(MacroAssembler* masm,
502 FloatingPointHelper::Destination destination,
503 Register scratch1,
504 Register scratch2) {
Steve Block44f0eee2011-05-26 01:26:41 +0100505 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100506 CpuFeatures::Scope scope(VFP3);
507 __ mov(scratch1, Operand(r0, ASR, kSmiTagSize));
508 __ vmov(d7.high(), scratch1);
509 __ vcvt_f64_s32(d7, d7.high());
510 __ mov(scratch1, Operand(r1, ASR, kSmiTagSize));
511 __ vmov(d6.high(), scratch1);
512 __ vcvt_f64_s32(d6, d6.high());
513 if (destination == kCoreRegisters) {
514 __ vmov(r2, r3, d7);
515 __ vmov(r0, r1, d6);
516 }
517 } else {
518 ASSERT(destination == kCoreRegisters);
519 // Write Smi from r0 to r3 and r2 in double format.
520 __ mov(scratch1, Operand(r0));
521 ConvertToDoubleStub stub1(r3, r2, scratch1, scratch2);
522 __ push(lr);
523 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
524 // Write Smi from r1 to r1 and r0 in double format. r9 is scratch.
525 __ mov(scratch1, Operand(r1));
526 ConvertToDoubleStub stub2(r1, r0, scratch1, scratch2);
527 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
528 __ pop(lr);
529 }
530}
531
532
533void FloatingPointHelper::LoadOperands(
534 MacroAssembler* masm,
535 FloatingPointHelper::Destination destination,
536 Register heap_number_map,
537 Register scratch1,
538 Register scratch2,
539 Label* slow) {
540
541 // Load right operand (r0) to d6 or r2/r3.
542 LoadNumber(masm, destination,
543 r0, d7, r2, r3, heap_number_map, scratch1, scratch2, slow);
544
545 // Load left operand (r1) to d7 or r0/r1.
546 LoadNumber(masm, destination,
547 r1, d6, r0, r1, heap_number_map, scratch1, scratch2, slow);
548}
549
550
551void FloatingPointHelper::LoadNumber(MacroAssembler* masm,
552 Destination destination,
553 Register object,
554 DwVfpRegister dst,
555 Register dst1,
556 Register dst2,
557 Register heap_number_map,
558 Register scratch1,
559 Register scratch2,
560 Label* not_number) {
561 if (FLAG_debug_code) {
562 __ AbortIfNotRootValue(heap_number_map,
563 Heap::kHeapNumberMapRootIndex,
564 "HeapNumberMap register clobbered.");
565 }
566
567 Label is_smi, done;
568
569 __ JumpIfSmi(object, &is_smi);
570 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_number);
571
572 // Handle loading a double from a heap number.
Steve Block44f0eee2011-05-26 01:26:41 +0100573 if (Isolate::Current()->cpu_features()->IsSupported(VFP3) &&
574 destination == kVFPRegisters) {
Steve Block1e0659c2011-05-24 12:43:12 +0100575 CpuFeatures::Scope scope(VFP3);
576 // Load the double from tagged HeapNumber to double register.
577 __ sub(scratch1, object, Operand(kHeapObjectTag));
578 __ vldr(dst, scratch1, HeapNumber::kValueOffset);
579 } else {
580 ASSERT(destination == kCoreRegisters);
581 // Load the double from heap number to dst1 and dst2 in double format.
582 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
583 }
584 __ jmp(&done);
585
586 // Handle loading a double from a smi.
587 __ bind(&is_smi);
Steve Block44f0eee2011-05-26 01:26:41 +0100588 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100589 CpuFeatures::Scope scope(VFP3);
590 // Convert smi to double using VFP instructions.
591 __ SmiUntag(scratch1, object);
592 __ vmov(dst.high(), scratch1);
593 __ vcvt_f64_s32(dst, dst.high());
594 if (destination == kCoreRegisters) {
595 // Load the converted smi to dst1 and dst2 in double format.
596 __ vmov(dst1, dst2, dst);
597 }
598 } else {
599 ASSERT(destination == kCoreRegisters);
600 // Write smi to dst1 and dst2 double format.
601 __ mov(scratch1, Operand(object));
602 ConvertToDoubleStub stub(dst2, dst1, scratch1, scratch2);
603 __ push(lr);
604 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
605 __ pop(lr);
606 }
607
608 __ bind(&done);
609}
610
611
Steve Block44f0eee2011-05-26 01:26:41 +0100612void FloatingPointHelper::ConvertNumberToInt32(MacroAssembler* masm,
613 Register object,
614 Register dst,
615 Register heap_number_map,
616 Register scratch1,
617 Register scratch2,
618 Register scratch3,
619 DwVfpRegister double_scratch,
620 Label* not_number) {
Steve Block1e0659c2011-05-24 12:43:12 +0100621 if (FLAG_debug_code) {
622 __ AbortIfNotRootValue(heap_number_map,
623 Heap::kHeapNumberMapRootIndex,
624 "HeapNumberMap register clobbered.");
625 }
Steve Block44f0eee2011-05-26 01:26:41 +0100626 Label is_smi;
627 Label done;
628 Label not_in_int32_range;
629
Steve Block1e0659c2011-05-24 12:43:12 +0100630 __ JumpIfSmi(object, &is_smi);
631 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kMapOffset));
632 __ cmp(scratch1, heap_number_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100633 __ b(ne, not_number);
634 __ ConvertToInt32(object,
635 dst,
636 scratch1,
637 scratch2,
638 double_scratch,
639 &not_in_int32_range);
Steve Block1e0659c2011-05-24 12:43:12 +0100640 __ jmp(&done);
Steve Block44f0eee2011-05-26 01:26:41 +0100641
642 __ bind(&not_in_int32_range);
643 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
644 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
645
646 __ EmitOutOfInt32RangeTruncate(dst,
647 scratch1,
648 scratch2,
649 scratch3);
650 __ jmp(&done);
651
Steve Block1e0659c2011-05-24 12:43:12 +0100652 __ bind(&is_smi);
653 __ SmiUntag(dst, object);
654 __ bind(&done);
655}
656
657
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100658void FloatingPointHelper::LoadNumberAsInt32Double(MacroAssembler* masm,
659 Register object,
660 Destination destination,
661 DwVfpRegister double_dst,
662 Register dst1,
663 Register dst2,
664 Register heap_number_map,
665 Register scratch1,
666 Register scratch2,
667 SwVfpRegister single_scratch,
668 Label* not_int32) {
669 ASSERT(!scratch1.is(object) && !scratch2.is(object));
670 ASSERT(!scratch1.is(scratch2));
671 ASSERT(!heap_number_map.is(object) &&
672 !heap_number_map.is(scratch1) &&
673 !heap_number_map.is(scratch2));
674
675 Label done, obj_is_not_smi;
676
677 __ JumpIfNotSmi(object, &obj_is_not_smi);
678 __ SmiUntag(scratch1, object);
Steve Block44f0eee2011-05-26 01:26:41 +0100679 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100680 CpuFeatures::Scope scope(VFP3);
681 __ vmov(single_scratch, scratch1);
682 __ vcvt_f64_s32(double_dst, single_scratch);
683 if (destination == kCoreRegisters) {
684 __ vmov(dst1, dst2, double_dst);
685 }
686 } else {
687 Label fewer_than_20_useful_bits;
688 // Expected output:
689 // | dst1 | dst2 |
690 // | s | exp | mantissa |
691
692 // Check for zero.
693 __ cmp(scratch1, Operand(0));
694 __ mov(dst1, scratch1);
695 __ mov(dst2, scratch1);
696 __ b(eq, &done);
697
698 // Preload the sign of the value.
699 __ and_(dst1, scratch1, Operand(HeapNumber::kSignMask), SetCC);
700 // Get the absolute value of the object (as an unsigned integer).
701 __ rsb(scratch1, scratch1, Operand(0), SetCC, mi);
702
703 // Get mantisssa[51:20].
704
705 // Get the position of the first set bit.
706 __ CountLeadingZeros(dst2, scratch1, scratch2);
707 __ rsb(dst2, dst2, Operand(31));
708
709 // Set the exponent.
710 __ add(scratch2, dst2, Operand(HeapNumber::kExponentBias));
711 __ Bfi(dst1, scratch2, scratch2,
712 HeapNumber::kExponentShift, HeapNumber::kExponentBits);
713
714 // Clear the first non null bit.
715 __ mov(scratch2, Operand(1));
716 __ bic(scratch1, scratch1, Operand(scratch2, LSL, dst2));
717
718 __ cmp(dst2, Operand(HeapNumber::kMantissaBitsInTopWord));
719 // Get the number of bits to set in the lower part of the mantissa.
720 __ sub(scratch2, dst2, Operand(HeapNumber::kMantissaBitsInTopWord), SetCC);
721 __ b(mi, &fewer_than_20_useful_bits);
722 // Set the higher 20 bits of the mantissa.
723 __ orr(dst1, dst1, Operand(scratch1, LSR, scratch2));
724 __ rsb(scratch2, scratch2, Operand(32));
725 __ mov(dst2, Operand(scratch1, LSL, scratch2));
726 __ b(&done);
727
728 __ bind(&fewer_than_20_useful_bits);
729 __ rsb(scratch2, dst2, Operand(HeapNumber::kMantissaBitsInTopWord));
730 __ mov(scratch2, Operand(scratch1, LSL, scratch2));
731 __ orr(dst1, dst1, scratch2);
732 // Set dst2 to 0.
733 __ mov(dst2, Operand(0));
734 }
735
736 __ b(&done);
737
738 __ bind(&obj_is_not_smi);
739 if (FLAG_debug_code) {
740 __ AbortIfNotRootValue(heap_number_map,
741 Heap::kHeapNumberMapRootIndex,
742 "HeapNumberMap register clobbered.");
743 }
744 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
745
746 // Load the number.
Steve Block44f0eee2011-05-26 01:26:41 +0100747 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100748 CpuFeatures::Scope scope(VFP3);
749 // Load the double value.
750 __ sub(scratch1, object, Operand(kHeapObjectTag));
751 __ vldr(double_dst, scratch1, HeapNumber::kValueOffset);
752
753 __ EmitVFPTruncate(kRoundToZero,
754 single_scratch,
755 double_dst,
756 scratch1,
757 scratch2,
758 kCheckForInexactConversion);
759
760 // Jump to not_int32 if the operation did not succeed.
761 __ b(ne, not_int32);
762
763 if (destination == kCoreRegisters) {
764 __ vmov(dst1, dst2, double_dst);
765 }
766
767 } else {
768 ASSERT(!scratch1.is(object) && !scratch2.is(object));
769 // Load the double value in the destination registers..
770 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
771
772 // Check for 0 and -0.
773 __ bic(scratch1, dst1, Operand(HeapNumber::kSignMask));
774 __ orr(scratch1, scratch1, Operand(dst2));
775 __ cmp(scratch1, Operand(0));
776 __ b(eq, &done);
777
778 // Check that the value can be exactly represented by a 32-bit integer.
779 // Jump to not_int32 if that's not the case.
780 DoubleIs32BitInteger(masm, dst1, dst2, scratch1, scratch2, not_int32);
781
782 // dst1 and dst2 were trashed. Reload the double value.
783 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
784 }
785
786 __ bind(&done);
787}
788
789
790void FloatingPointHelper::LoadNumberAsInt32(MacroAssembler* masm,
791 Register object,
792 Register dst,
793 Register heap_number_map,
794 Register scratch1,
795 Register scratch2,
796 Register scratch3,
797 DwVfpRegister double_scratch,
798 Label* not_int32) {
799 ASSERT(!dst.is(object));
800 ASSERT(!scratch1.is(object) && !scratch2.is(object) && !scratch3.is(object));
801 ASSERT(!scratch1.is(scratch2) &&
802 !scratch1.is(scratch3) &&
803 !scratch2.is(scratch3));
804
805 Label done;
806
807 // Untag the object into the destination register.
808 __ SmiUntag(dst, object);
809 // Just return if the object is a smi.
810 __ JumpIfSmi(object, &done);
811
812 if (FLAG_debug_code) {
813 __ AbortIfNotRootValue(heap_number_map,
814 Heap::kHeapNumberMapRootIndex,
815 "HeapNumberMap register clobbered.");
816 }
817 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
818
819 // Object is a heap number.
820 // Convert the floating point value to a 32-bit integer.
Steve Block44f0eee2011-05-26 01:26:41 +0100821 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100822 CpuFeatures::Scope scope(VFP3);
823 SwVfpRegister single_scratch = double_scratch.low();
824 // Load the double value.
825 __ sub(scratch1, object, Operand(kHeapObjectTag));
826 __ vldr(double_scratch, scratch1, HeapNumber::kValueOffset);
827
828 __ EmitVFPTruncate(kRoundToZero,
829 single_scratch,
830 double_scratch,
831 scratch1,
832 scratch2,
833 kCheckForInexactConversion);
834
835 // Jump to not_int32 if the operation did not succeed.
836 __ b(ne, not_int32);
837 // Get the result in the destination register.
838 __ vmov(dst, single_scratch);
839
840 } else {
841 // Load the double value in the destination registers.
842 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
843 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
844
845 // Check for 0 and -0.
846 __ bic(dst, scratch1, Operand(HeapNumber::kSignMask));
847 __ orr(dst, scratch2, Operand(dst));
848 __ cmp(dst, Operand(0));
849 __ b(eq, &done);
850
851 DoubleIs32BitInteger(masm, scratch1, scratch2, dst, scratch3, not_int32);
852
853 // Registers state after DoubleIs32BitInteger.
854 // dst: mantissa[51:20].
855 // scratch2: 1
856
857 // Shift back the higher bits of the mantissa.
858 __ mov(dst, Operand(dst, LSR, scratch3));
859 // Set the implicit first bit.
860 __ rsb(scratch3, scratch3, Operand(32));
861 __ orr(dst, dst, Operand(scratch2, LSL, scratch3));
862 // Set the sign.
863 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
864 __ tst(scratch1, Operand(HeapNumber::kSignMask));
865 __ rsb(dst, dst, Operand(0), LeaveCC, mi);
866 }
867
868 __ bind(&done);
869}
870
871
872void FloatingPointHelper::DoubleIs32BitInteger(MacroAssembler* masm,
873 Register src1,
874 Register src2,
875 Register dst,
876 Register scratch,
877 Label* not_int32) {
878 // Get exponent alone in scratch.
879 __ Ubfx(scratch,
880 src1,
881 HeapNumber::kExponentShift,
882 HeapNumber::kExponentBits);
883
884 // Substract the bias from the exponent.
885 __ sub(scratch, scratch, Operand(HeapNumber::kExponentBias), SetCC);
886
887 // src1: higher (exponent) part of the double value.
888 // src2: lower (mantissa) part of the double value.
889 // scratch: unbiased exponent.
890
891 // Fast cases. Check for obvious non 32-bit integer values.
892 // Negative exponent cannot yield 32-bit integers.
893 __ b(mi, not_int32);
894 // Exponent greater than 31 cannot yield 32-bit integers.
895 // Also, a positive value with an exponent equal to 31 is outside of the
896 // signed 32-bit integer range.
897 // Another way to put it is that if (exponent - signbit) > 30 then the
898 // number cannot be represented as an int32.
899 Register tmp = dst;
900 __ sub(tmp, scratch, Operand(src1, LSR, 31));
901 __ cmp(tmp, Operand(30));
902 __ b(gt, not_int32);
903 // - Bits [21:0] in the mantissa are not null.
904 __ tst(src2, Operand(0x3fffff));
905 __ b(ne, not_int32);
906
907 // Otherwise the exponent needs to be big enough to shift left all the
908 // non zero bits left. So we need the (30 - exponent) last bits of the
909 // 31 higher bits of the mantissa to be null.
910 // Because bits [21:0] are null, we can check instead that the
911 // (32 - exponent) last bits of the 32 higher bits of the mantisssa are null.
912
913 // Get the 32 higher bits of the mantissa in dst.
914 __ Ubfx(dst,
915 src2,
916 HeapNumber::kMantissaBitsInTopWord,
917 32 - HeapNumber::kMantissaBitsInTopWord);
918 __ orr(dst,
919 dst,
920 Operand(src1, LSL, HeapNumber::kNonMantissaBitsInTopWord));
921
922 // Create the mask and test the lower bits (of the higher bits).
923 __ rsb(scratch, scratch, Operand(32));
924 __ mov(src2, Operand(1));
925 __ mov(src1, Operand(src2, LSL, scratch));
926 __ sub(src1, src1, Operand(1));
927 __ tst(dst, src1);
928 __ b(ne, not_int32);
929}
930
931
932void FloatingPointHelper::CallCCodeForDoubleOperation(
933 MacroAssembler* masm,
934 Token::Value op,
935 Register heap_number_result,
936 Register scratch) {
937 // Using core registers:
938 // r0: Left value (least significant part of mantissa).
939 // r1: Left value (sign, exponent, top of mantissa).
940 // r2: Right value (least significant part of mantissa).
941 // r3: Right value (sign, exponent, top of mantissa).
942
943 // Assert that heap_number_result is callee-saved.
944 // We currently always use r5 to pass it.
945 ASSERT(heap_number_result.is(r5));
946
947 // Push the current return address before the C call. Return will be
948 // through pop(pc) below.
949 __ push(lr);
950 __ PrepareCallCFunction(4, scratch); // Two doubles are 4 arguments.
951 // Call C routine that may not cause GC or other trouble.
Steve Block44f0eee2011-05-26 01:26:41 +0100952 __ CallCFunction(ExternalReference::double_fp_operation(op, masm->isolate()),
953 4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100954 // Store answer in the overwritable heap number.
955#if !defined(USE_ARM_EABI)
956 // Double returned in fp coprocessor register 0 and 1, encoded as
957 // register cr8. Offsets must be divisible by 4 for coprocessor so we
958 // need to substract the tag from heap_number_result.
959 __ sub(scratch, heap_number_result, Operand(kHeapObjectTag));
960 __ stc(p1, cr8, MemOperand(scratch, HeapNumber::kValueOffset));
961#else
962 // Double returned in registers 0 and 1.
963 __ Strd(r0, r1, FieldMemOperand(heap_number_result,
964 HeapNumber::kValueOffset));
965#endif
966 // Place heap_number_result in r0 and return to the pushed return address.
967 __ mov(r0, Operand(heap_number_result));
968 __ pop(pc);
969}
970
Steve Block1e0659c2011-05-24 12:43:12 +0100971
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100972// See comment for class.
973void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
974 Label max_negative_int;
975 // the_int_ has the answer which is a signed int32 but not a Smi.
976 // We test for the special value that has a different exponent. This test
977 // has the neat side effect of setting the flags according to the sign.
978 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
979 __ cmp(the_int_, Operand(0x80000000u));
980 __ b(eq, &max_negative_int);
981 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
982 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
983 uint32_t non_smi_exponent =
984 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
985 __ mov(scratch_, Operand(non_smi_exponent));
986 // Set the sign bit in scratch_ if the value was negative.
987 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
988 // Subtract from 0 if the value was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100989 __ rsb(the_int_, the_int_, Operand(0, RelocInfo::NONE), LeaveCC, cs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100990 // We should be masking the implict first digit of the mantissa away here,
991 // but it just ends up combining harmlessly with the last digit of the
992 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
993 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
994 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
995 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
996 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
997 __ str(scratch_, FieldMemOperand(the_heap_number_,
998 HeapNumber::kExponentOffset));
999 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
1000 __ str(scratch_, FieldMemOperand(the_heap_number_,
1001 HeapNumber::kMantissaOffset));
1002 __ Ret();
1003
1004 __ bind(&max_negative_int);
1005 // The max negative int32 is stored as a positive number in the mantissa of
1006 // a double because it uses a sign bit instead of using two's complement.
1007 // The actual mantissa bits stored are all 0 because the implicit most
1008 // significant 1 bit is not stored.
1009 non_smi_exponent += 1 << HeapNumber::kExponentShift;
1010 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
1011 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
Iain Merrick9ac36c92010-09-13 15:29:50 +01001012 __ mov(ip, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001013 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
1014 __ Ret();
1015}
1016
1017
1018// Handle the case where the lhs and rhs are the same object.
1019// Equality is almost reflexive (everything but NaN), so this is a test
1020// for "identity and not NaN".
1021static void EmitIdenticalObjectComparison(MacroAssembler* masm,
1022 Label* slow,
Steve Block1e0659c2011-05-24 12:43:12 +01001023 Condition cond,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001024 bool never_nan_nan) {
1025 Label not_identical;
1026 Label heap_number, return_equal;
1027 __ cmp(r0, r1);
1028 __ b(ne, &not_identical);
1029
1030 // The two objects are identical. If we know that one of them isn't NaN then
1031 // we now know they test equal.
Steve Block1e0659c2011-05-24 12:43:12 +01001032 if (cond != eq || !never_nan_nan) {
Steve Block44f0eee2011-05-26 01:26:41 +01001033 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001034 // so we do the second best thing - test it ourselves.
1035 // They are both equal and they are not both Smis so both of them are not
1036 // Smis. If it's not a heap number, then return equal.
Steve Block1e0659c2011-05-24 12:43:12 +01001037 if (cond == lt || cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001038 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
1039 __ b(ge, slow);
1040 } else {
1041 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
1042 __ b(eq, &heap_number);
1043 // Comparing JS objects with <=, >= is complicated.
Steve Block1e0659c2011-05-24 12:43:12 +01001044 if (cond != eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001045 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
1046 __ b(ge, slow);
1047 // Normally here we fall through to return_equal, but undefined is
1048 // special: (undefined == undefined) == true, but
1049 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
Steve Block1e0659c2011-05-24 12:43:12 +01001050 if (cond == le || cond == ge) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001051 __ cmp(r4, Operand(ODDBALL_TYPE));
1052 __ b(ne, &return_equal);
1053 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1054 __ cmp(r0, r2);
1055 __ b(ne, &return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +01001056 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001057 // undefined <= undefined should fail.
1058 __ mov(r0, Operand(GREATER));
1059 } else {
1060 // undefined >= undefined should fail.
1061 __ mov(r0, Operand(LESS));
1062 }
1063 __ Ret();
1064 }
1065 }
1066 }
1067 }
1068
1069 __ bind(&return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +01001070 if (cond == lt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001071 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
Steve Block1e0659c2011-05-24 12:43:12 +01001072 } else if (cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001073 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
1074 } else {
1075 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
1076 }
1077 __ Ret();
1078
Steve Block1e0659c2011-05-24 12:43:12 +01001079 if (cond != eq || !never_nan_nan) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001080 // For less and greater we don't have to check for NaN since the result of
1081 // x < x is false regardless. For the others here is some code to check
1082 // for NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001083 if (cond != lt && cond != gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001084 __ bind(&heap_number);
1085 // It is a heap number, so return non-equal if it's NaN and equal if it's
1086 // not NaN.
1087
1088 // The representation of NaN values has all exponent bits (52..62) set,
1089 // and not all mantissa bits (0..51) clear.
1090 // Read top bits of double representation (second word of value).
1091 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1092 // Test that exponent bits are all set.
1093 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
1094 // NaNs have all-one exponents so they sign extend to -1.
1095 __ cmp(r3, Operand(-1));
1096 __ b(ne, &return_equal);
1097
1098 // Shift out flag and all exponent bits, retaining only mantissa.
1099 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
1100 // Or with all low-bits of mantissa.
1101 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
1102 __ orr(r0, r3, Operand(r2), SetCC);
1103 // For equal we already have the right value in r0: Return zero (equal)
1104 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
1105 // not (it's a NaN). For <= and >= we need to load r0 with the failing
1106 // value if it's a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001107 if (cond != eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001108 // All-zero means Infinity means equal.
1109 __ Ret(eq);
Steve Block1e0659c2011-05-24 12:43:12 +01001110 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001111 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
1112 } else {
1113 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
1114 }
1115 }
1116 __ Ret();
1117 }
1118 // No fall through here.
1119 }
1120
1121 __ bind(&not_identical);
1122}
1123
1124
1125// See comment at call site.
1126static void EmitSmiNonsmiComparison(MacroAssembler* masm,
1127 Register lhs,
1128 Register rhs,
1129 Label* lhs_not_nan,
1130 Label* slow,
1131 bool strict) {
1132 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1133 (lhs.is(r1) && rhs.is(r0)));
1134
1135 Label rhs_is_smi;
1136 __ tst(rhs, Operand(kSmiTagMask));
1137 __ b(eq, &rhs_is_smi);
1138
1139 // Lhs is a Smi. Check whether the rhs is a heap number.
1140 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
1141 if (strict) {
1142 // If rhs is not a number and lhs is a Smi then strict equality cannot
1143 // succeed. Return non-equal
1144 // If rhs is r0 then there is already a non zero value in it.
1145 if (!rhs.is(r0)) {
1146 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1147 }
1148 __ Ret(ne);
1149 } else {
1150 // Smi compared non-strictly with a non-Smi non-heap-number. Call
1151 // the runtime.
1152 __ b(ne, slow);
1153 }
1154
1155 // Lhs is a smi, rhs is a number.
Steve Block44f0eee2011-05-26 01:26:41 +01001156 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001157 // Convert lhs to a double in d7.
1158 CpuFeatures::Scope scope(VFP3);
1159 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
1160 // Load the double from rhs, tagged HeapNumber r0, to d6.
1161 __ sub(r7, rhs, Operand(kHeapObjectTag));
1162 __ vldr(d6, r7, HeapNumber::kValueOffset);
1163 } else {
1164 __ push(lr);
1165 // Convert lhs to a double in r2, r3.
1166 __ mov(r7, Operand(lhs));
1167 ConvertToDoubleStub stub1(r3, r2, r7, r6);
1168 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
1169 // Load rhs to a double in r0, r1.
1170 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1171 __ pop(lr);
1172 }
1173
1174 // We now have both loaded as doubles but we can skip the lhs nan check
1175 // since it's a smi.
1176 __ jmp(lhs_not_nan);
1177
1178 __ bind(&rhs_is_smi);
1179 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
1180 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
1181 if (strict) {
1182 // If lhs is not a number and rhs is a smi then strict equality cannot
1183 // succeed. Return non-equal.
1184 // If lhs is r0 then there is already a non zero value in it.
1185 if (!lhs.is(r0)) {
1186 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1187 }
1188 __ Ret(ne);
1189 } else {
1190 // Smi compared non-strictly with a non-smi non-heap-number. Call
1191 // the runtime.
1192 __ b(ne, slow);
1193 }
1194
1195 // Rhs is a smi, lhs is a heap number.
Steve Block44f0eee2011-05-26 01:26:41 +01001196 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001197 CpuFeatures::Scope scope(VFP3);
1198 // Load the double from lhs, tagged HeapNumber r1, to d7.
1199 __ sub(r7, lhs, Operand(kHeapObjectTag));
1200 __ vldr(d7, r7, HeapNumber::kValueOffset);
1201 // Convert rhs to a double in d6 .
1202 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
1203 } else {
1204 __ push(lr);
1205 // Load lhs to a double in r2, r3.
1206 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1207 // Convert rhs to a double in r0, r1.
1208 __ mov(r7, Operand(rhs));
1209 ConvertToDoubleStub stub2(r1, r0, r7, r6);
1210 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
1211 __ pop(lr);
1212 }
1213 // Fall through to both_loaded_as_doubles.
1214}
1215
1216
Steve Block1e0659c2011-05-24 12:43:12 +01001217void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001218 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1219 Register rhs_exponent = exp_first ? r0 : r1;
1220 Register lhs_exponent = exp_first ? r2 : r3;
1221 Register rhs_mantissa = exp_first ? r1 : r0;
1222 Register lhs_mantissa = exp_first ? r3 : r2;
1223 Label one_is_nan, neither_is_nan;
1224
1225 __ Sbfx(r4,
1226 lhs_exponent,
1227 HeapNumber::kExponentShift,
1228 HeapNumber::kExponentBits);
1229 // NaNs have all-one exponents so they sign extend to -1.
1230 __ cmp(r4, Operand(-1));
1231 __ b(ne, lhs_not_nan);
1232 __ mov(r4,
1233 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1234 SetCC);
1235 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001236 __ cmp(lhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001237 __ b(ne, &one_is_nan);
1238
1239 __ bind(lhs_not_nan);
1240 __ Sbfx(r4,
1241 rhs_exponent,
1242 HeapNumber::kExponentShift,
1243 HeapNumber::kExponentBits);
1244 // NaNs have all-one exponents so they sign extend to -1.
1245 __ cmp(r4, Operand(-1));
1246 __ b(ne, &neither_is_nan);
1247 __ mov(r4,
1248 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1249 SetCC);
1250 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001251 __ cmp(rhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001252 __ b(eq, &neither_is_nan);
1253
1254 __ bind(&one_is_nan);
1255 // NaN comparisons always fail.
1256 // Load whatever we need in r0 to make the comparison fail.
Steve Block1e0659c2011-05-24 12:43:12 +01001257 if (cond == lt || cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001258 __ mov(r0, Operand(GREATER));
1259 } else {
1260 __ mov(r0, Operand(LESS));
1261 }
1262 __ Ret();
1263
1264 __ bind(&neither_is_nan);
1265}
1266
1267
1268// See comment at call site.
Steve Block1e0659c2011-05-24 12:43:12 +01001269static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm,
1270 Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001271 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1272 Register rhs_exponent = exp_first ? r0 : r1;
1273 Register lhs_exponent = exp_first ? r2 : r3;
1274 Register rhs_mantissa = exp_first ? r1 : r0;
1275 Register lhs_mantissa = exp_first ? r3 : r2;
1276
1277 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001278 if (cond == eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001279 // Doubles are not equal unless they have the same bit pattern.
1280 // Exception: 0 and -0.
1281 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
1282 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
1283 // Return non-zero if the numbers are unequal.
1284 __ Ret(ne);
1285
1286 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
1287 // If exponents are equal then return 0.
1288 __ Ret(eq);
1289
1290 // Exponents are unequal. The only way we can return that the numbers
1291 // are equal is if one is -0 and the other is 0. We already dealt
1292 // with the case where both are -0 or both are 0.
1293 // We start by seeing if the mantissas (that are equal) or the bottom
1294 // 31 bits of the rhs exponent are non-zero. If so we return not
1295 // equal.
1296 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
1297 __ mov(r0, Operand(r4), LeaveCC, ne);
1298 __ Ret(ne);
1299 // Now they are equal if and only if the lhs exponent is zero in its
1300 // low 31 bits.
1301 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
1302 __ Ret();
1303 } else {
1304 // Call a native function to do a comparison between two non-NaNs.
1305 // Call C routine that may not cause GC or other trouble.
1306 __ push(lr);
1307 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
Steve Block44f0eee2011-05-26 01:26:41 +01001308 __ CallCFunction(ExternalReference::compare_doubles(masm->isolate()), 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001309 __ pop(pc); // Return.
1310 }
1311}
1312
1313
1314// See comment at call site.
1315static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
1316 Register lhs,
1317 Register rhs) {
1318 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1319 (lhs.is(r1) && rhs.is(r0)));
1320
1321 // If either operand is a JSObject or an oddball value, then they are
1322 // not equal since their pointers are different.
1323 // There is no test for undetectability in strict equality.
1324 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1325 Label first_non_object;
1326 // Get the type of the first operand into r2 and compare it with
1327 // FIRST_JS_OBJECT_TYPE.
1328 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
1329 __ b(lt, &first_non_object);
1330
1331 // Return non-zero (r0 is not zero)
1332 Label return_not_equal;
1333 __ bind(&return_not_equal);
1334 __ Ret();
1335
1336 __ bind(&first_non_object);
1337 // Check for oddballs: true, false, null, undefined.
1338 __ cmp(r2, Operand(ODDBALL_TYPE));
1339 __ b(eq, &return_not_equal);
1340
1341 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
1342 __ b(ge, &return_not_equal);
1343
1344 // Check for oddballs: true, false, null, undefined.
1345 __ cmp(r3, Operand(ODDBALL_TYPE));
1346 __ b(eq, &return_not_equal);
1347
1348 // Now that we have the types we might as well check for symbol-symbol.
1349 // Ensure that no non-strings have the symbol bit set.
1350 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
1351 STATIC_ASSERT(kSymbolTag != 0);
1352 __ and_(r2, r2, Operand(r3));
1353 __ tst(r2, Operand(kIsSymbolMask));
1354 __ b(ne, &return_not_equal);
1355}
1356
1357
1358// See comment at call site.
1359static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
1360 Register lhs,
1361 Register rhs,
1362 Label* both_loaded_as_doubles,
1363 Label* not_heap_numbers,
1364 Label* slow) {
1365 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1366 (lhs.is(r1) && rhs.is(r0)));
1367
1368 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
1369 __ b(ne, not_heap_numbers);
1370 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
1371 __ cmp(r2, r3);
1372 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
1373
1374 // Both are heap numbers. Load them up then jump to the code we have
1375 // for that.
Steve Block44f0eee2011-05-26 01:26:41 +01001376 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001377 CpuFeatures::Scope scope(VFP3);
1378 __ sub(r7, rhs, Operand(kHeapObjectTag));
1379 __ vldr(d6, r7, HeapNumber::kValueOffset);
1380 __ sub(r7, lhs, Operand(kHeapObjectTag));
1381 __ vldr(d7, r7, HeapNumber::kValueOffset);
1382 } else {
1383 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1384 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1385 }
1386 __ jmp(both_loaded_as_doubles);
1387}
1388
1389
1390// Fast negative check for symbol-to-symbol equality.
1391static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
1392 Register lhs,
1393 Register rhs,
1394 Label* possible_strings,
1395 Label* not_both_strings) {
1396 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1397 (lhs.is(r1) && rhs.is(r0)));
1398
1399 // r2 is object type of rhs.
1400 // Ensure that no non-strings have the symbol bit set.
1401 Label object_test;
1402 STATIC_ASSERT(kSymbolTag != 0);
1403 __ tst(r2, Operand(kIsNotStringMask));
1404 __ b(ne, &object_test);
1405 __ tst(r2, Operand(kIsSymbolMask));
1406 __ b(eq, possible_strings);
1407 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
1408 __ b(ge, not_both_strings);
1409 __ tst(r3, Operand(kIsSymbolMask));
1410 __ b(eq, possible_strings);
1411
1412 // Both are symbols. We already checked they weren't the same pointer
1413 // so they are not equal.
1414 __ mov(r0, Operand(NOT_EQUAL));
1415 __ Ret();
1416
1417 __ bind(&object_test);
1418 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
1419 __ b(lt, not_both_strings);
1420 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
1421 __ b(lt, not_both_strings);
1422 // If both objects are undetectable, they are equal. Otherwise, they
1423 // are not equal, since they are different objects and an object is not
1424 // equal to undefined.
1425 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
1426 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
1427 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
1428 __ and_(r0, r2, Operand(r3));
1429 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
1430 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
1431 __ Ret();
1432}
1433
1434
1435void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
1436 Register object,
1437 Register result,
1438 Register scratch1,
1439 Register scratch2,
1440 Register scratch3,
1441 bool object_is_smi,
1442 Label* not_found) {
1443 // Use of registers. Register result is used as a temporary.
1444 Register number_string_cache = result;
1445 Register mask = scratch3;
1446
1447 // Load the number string cache.
1448 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
1449
1450 // Make the hash mask from the length of the number string cache. It
1451 // contains two elements (number and string) for each cache entry.
1452 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
1453 // Divide length by two (length is a smi).
1454 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
1455 __ sub(mask, mask, Operand(1)); // Make mask.
1456
1457 // Calculate the entry in the number string cache. The hash value in the
1458 // number string cache for smis is just the smi value, and the hash for
1459 // doubles is the xor of the upper and lower words. See
1460 // Heap::GetNumberStringCache.
Steve Block44f0eee2011-05-26 01:26:41 +01001461 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001462 Label is_smi;
1463 Label load_result_from_cache;
1464 if (!object_is_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01001465 __ JumpIfSmi(object, &is_smi);
Steve Block44f0eee2011-05-26 01:26:41 +01001466 if (isolate->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001467 CpuFeatures::Scope scope(VFP3);
1468 __ CheckMap(object,
1469 scratch1,
1470 Heap::kHeapNumberMapRootIndex,
1471 not_found,
1472 true);
1473
1474 STATIC_ASSERT(8 == kDoubleSize);
1475 __ add(scratch1,
1476 object,
1477 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
1478 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
1479 __ eor(scratch1, scratch1, Operand(scratch2));
1480 __ and_(scratch1, scratch1, Operand(mask));
1481
1482 // Calculate address of entry in string cache: each entry consists
1483 // of two pointer sized fields.
1484 __ add(scratch1,
1485 number_string_cache,
1486 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
1487
1488 Register probe = mask;
1489 __ ldr(probe,
1490 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
Steve Block1e0659c2011-05-24 12:43:12 +01001491 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001492 __ sub(scratch2, object, Operand(kHeapObjectTag));
1493 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
1494 __ sub(probe, probe, Operand(kHeapObjectTag));
1495 __ vldr(d1, probe, HeapNumber::kValueOffset);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001496 __ VFPCompareAndSetFlags(d0, d1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001497 __ b(ne, not_found); // The cache did not contain this value.
1498 __ b(&load_result_from_cache);
1499 } else {
1500 __ b(not_found);
1501 }
1502 }
1503
1504 __ bind(&is_smi);
1505 Register scratch = scratch1;
1506 __ and_(scratch, mask, Operand(object, ASR, 1));
1507 // Calculate address of entry in string cache: each entry consists
1508 // of two pointer sized fields.
1509 __ add(scratch,
1510 number_string_cache,
1511 Operand(scratch, LSL, kPointerSizeLog2 + 1));
1512
1513 // Check if the entry is the smi we are looking for.
1514 Register probe = mask;
1515 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
1516 __ cmp(object, probe);
1517 __ b(ne, not_found);
1518
1519 // Get the result from the cache.
1520 __ bind(&load_result_from_cache);
1521 __ ldr(result,
1522 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01001523 __ IncrementCounter(isolate->counters()->number_to_string_native(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001524 1,
1525 scratch1,
1526 scratch2);
1527}
1528
1529
1530void NumberToStringStub::Generate(MacroAssembler* masm) {
1531 Label runtime;
1532
1533 __ ldr(r1, MemOperand(sp, 0));
1534
1535 // Generate code to lookup number in the number string cache.
1536 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
1537 __ add(sp, sp, Operand(1 * kPointerSize));
1538 __ Ret();
1539
1540 __ bind(&runtime);
1541 // Handle number to string in the runtime system if not found in the cache.
1542 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
1543}
1544
1545
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001546// On entry lhs_ and rhs_ are the values to be compared.
1547// On exit r0 is 0, positive or negative to indicate the result of
1548// the comparison.
1549void CompareStub::Generate(MacroAssembler* masm) {
1550 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
1551 (lhs_.is(r1) && rhs_.is(r0)));
1552
1553 Label slow; // Call builtin.
1554 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
1555
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001556 if (include_smi_compare_) {
1557 Label not_two_smis, smi_done;
1558 __ orr(r2, r1, r0);
1559 __ tst(r2, Operand(kSmiTagMask));
1560 __ b(ne, &not_two_smis);
Ben Murdochf87a2032010-10-22 12:50:53 +01001561 __ mov(r1, Operand(r1, ASR, 1));
1562 __ sub(r0, r1, Operand(r0, ASR, 1));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001563 __ Ret();
1564 __ bind(&not_two_smis);
1565 } else if (FLAG_debug_code) {
1566 __ orr(r2, r1, r0);
1567 __ tst(r2, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001568 __ Assert(ne, "CompareStub: unexpected smi operands.");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001569 }
1570
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001571 // NOTICE! This code is only reached after a smi-fast-case check, so
1572 // it is certain that at least one operand isn't a smi.
1573
1574 // Handle the case where the objects are identical. Either returns the answer
1575 // or goes to slow. Only falls through if the objects were not identical.
1576 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
1577
1578 // If either is a Smi (we know that not both are), then they can only
1579 // be strictly equal if the other is a HeapNumber.
1580 STATIC_ASSERT(kSmiTag == 0);
1581 ASSERT_EQ(0, Smi::FromInt(0));
1582 __ and_(r2, lhs_, Operand(rhs_));
1583 __ tst(r2, Operand(kSmiTagMask));
1584 __ b(ne, &not_smis);
1585 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
1586 // 1) Return the answer.
1587 // 2) Go to slow.
1588 // 3) Fall through to both_loaded_as_doubles.
1589 // 4) Jump to lhs_not_nan.
1590 // In cases 3 and 4 we have found out we were dealing with a number-number
1591 // comparison. If VFP3 is supported the double values of the numbers have
1592 // been loaded into d7 and d6. Otherwise, the double values have been loaded
1593 // into r0, r1, r2, and r3.
1594 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
1595
1596 __ bind(&both_loaded_as_doubles);
1597 // The arguments have been converted to doubles and stored in d6 and d7, if
1598 // VFP3 is supported, or in r0, r1, r2, and r3.
Steve Block44f0eee2011-05-26 01:26:41 +01001599 Isolate* isolate = masm->isolate();
1600 if (isolate->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001601 __ bind(&lhs_not_nan);
1602 CpuFeatures::Scope scope(VFP3);
1603 Label no_nan;
1604 // ARMv7 VFP3 instructions to implement double precision comparison.
Ben Murdochb8e0da22011-05-16 14:20:40 +01001605 __ VFPCompareAndSetFlags(d7, d6);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001606 Label nan;
1607 __ b(vs, &nan);
1608 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
1609 __ mov(r0, Operand(LESS), LeaveCC, lt);
1610 __ mov(r0, Operand(GREATER), LeaveCC, gt);
1611 __ Ret();
1612
1613 __ bind(&nan);
1614 // If one of the sides was a NaN then the v flag is set. Load r0 with
1615 // whatever it takes to make the comparison fail, since comparisons with NaN
1616 // always fail.
1617 if (cc_ == lt || cc_ == le) {
1618 __ mov(r0, Operand(GREATER));
1619 } else {
1620 __ mov(r0, Operand(LESS));
1621 }
1622 __ Ret();
1623 } else {
1624 // Checks for NaN in the doubles we have loaded. Can return the answer or
1625 // fall through if neither is a NaN. Also binds lhs_not_nan.
1626 EmitNanCheck(masm, &lhs_not_nan, cc_);
1627 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
1628 // answer. Never falls through.
1629 EmitTwoNonNanDoubleComparison(masm, cc_);
1630 }
1631
1632 __ bind(&not_smis);
1633 // At this point we know we are dealing with two different objects,
1634 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
1635 if (strict_) {
1636 // This returns non-equal for some object types, or falls through if it
1637 // was not lucky.
1638 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1639 }
1640
1641 Label check_for_symbols;
1642 Label flat_string_check;
1643 // Check for heap-number-heap-number comparison. Can jump to slow case,
1644 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
1645 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1646 // In this case r2 will contain the type of rhs_. Never falls through.
1647 EmitCheckForTwoHeapNumbers(masm,
1648 lhs_,
1649 rhs_,
1650 &both_loaded_as_doubles,
1651 &check_for_symbols,
1652 &flat_string_check);
1653
1654 __ bind(&check_for_symbols);
1655 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
1656 // symbols.
1657 if (cc_ == eq && !strict_) {
1658 // Returns an answer for two symbols or two detectable objects.
1659 // Otherwise jumps to string case or not both strings case.
1660 // Assumes that r2 is the type of rhs_ on entry.
1661 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1662 }
1663
1664 // Check for both being sequential ASCII strings, and inline if that is the
1665 // case.
1666 __ bind(&flat_string_check);
1667
1668 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
1669
Steve Block44f0eee2011-05-26 01:26:41 +01001670 __ IncrementCounter(isolate->counters()->string_compare_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001671 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1672 lhs_,
1673 rhs_,
1674 r2,
1675 r3,
1676 r4,
1677 r5);
1678 // Never falls through to here.
1679
1680 __ bind(&slow);
1681
1682 __ Push(lhs_, rhs_);
1683 // Figure out which native to call and setup the arguments.
1684 Builtins::JavaScript native;
1685 if (cc_ == eq) {
1686 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1687 } else {
1688 native = Builtins::COMPARE;
1689 int ncr; // NaN compare result
1690 if (cc_ == lt || cc_ == le) {
1691 ncr = GREATER;
1692 } else {
1693 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
1694 ncr = LESS;
1695 }
1696 __ mov(r0, Operand(Smi::FromInt(ncr)));
1697 __ push(r0);
1698 }
1699
1700 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1701 // tagged as a small integer.
1702 __ InvokeBuiltin(native, JUMP_JS);
1703}
1704
1705
1706// This stub does not handle the inlined cases (Smis, Booleans, undefined).
1707// The stub returns zero for false, and a non-zero value for true.
1708void ToBooleanStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001709 // This stub uses VFP3 instructions.
Steve Block44f0eee2011-05-26 01:26:41 +01001710 ASSERT(Isolate::Current()->cpu_features()->IsEnabled(VFP3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001711
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001712 Label false_result;
1713 Label not_heap_number;
Steve Block1e0659c2011-05-24 12:43:12 +01001714 Register scratch = r9.is(tos_) ? r7 : r9;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001715
Ben Murdochb0fe1622011-05-05 13:52:32 +01001716 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1717 __ cmp(tos_, ip);
1718 __ b(eq, &false_result);
1719
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001720 // HeapNumber => false iff +0, -0, or NaN.
1721 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1722 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
1723 __ cmp(scratch, ip);
1724 __ b(&not_heap_number, ne);
1725
1726 __ sub(ip, tos_, Operand(kHeapObjectTag));
1727 __ vldr(d1, ip, HeapNumber::kValueOffset);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001728 __ VFPCompareAndSetFlags(d1, 0.0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001729 // "tos_" is a register, and contains a non zero value by default.
1730 // Hence we only need to overwrite "tos_" with zero to return false for
1731 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001732 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, eq); // for FP_ZERO
1733 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, vs); // for FP_NAN
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001734 __ Ret();
1735
1736 __ bind(&not_heap_number);
1737
1738 // Check if the value is 'null'.
1739 // 'null' => false.
1740 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1741 __ cmp(tos_, ip);
1742 __ b(&false_result, eq);
1743
1744 // It can be an undetectable object.
1745 // Undetectable => false.
1746 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
1747 __ ldrb(scratch, FieldMemOperand(ip, Map::kBitFieldOffset));
1748 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
1749 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
1750 __ b(&false_result, eq);
1751
1752 // JavaScript object => true.
1753 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1754 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1755 __ cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
1756 // "tos_" is a register and contains a non-zero value.
1757 // Hence we implicitly return true if the greater than
1758 // condition is satisfied.
1759 __ Ret(gt);
1760
1761 // Check for string
1762 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1763 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1764 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
1765 // "tos_" is a register and contains a non-zero value.
1766 // Hence we implicitly return true if the greater than
1767 // condition is satisfied.
1768 __ Ret(gt);
1769
1770 // String value => false iff empty, i.e., length is zero
1771 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
1772 // If length is zero, "tos_" contains zero ==> false.
1773 // If length is not zero, "tos_" contains a non-zero value ==> true.
1774 __ Ret();
1775
1776 // Return 0 in "tos_" for false .
1777 __ bind(&false_result);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001778 __ mov(tos_, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001779 __ Ret();
1780}
1781
1782
1783// We fall into this code if the operands were Smis, but the result was
1784// not (eg. overflow). We branch into this code (to the not_smi label) if
1785// the operands were not both Smi. The operands are in r0 and r1. In order
1786// to call the C-implemented binary fp operation routines we need to end up
1787// with the double precision floating point operands in r0 and r1 (for the
1788// value in r1) and r2 and r3 (for the value in r0).
1789void GenericBinaryOpStub::HandleBinaryOpSlowCases(
1790 MacroAssembler* masm,
1791 Label* not_smi,
1792 Register lhs,
1793 Register rhs,
1794 const Builtins::JavaScript& builtin) {
1795 Label slow, slow_reverse, do_the_call;
Steve Block44f0eee2011-05-26 01:26:41 +01001796 bool use_fp_registers =
1797 Isolate::Current()->cpu_features()->IsSupported(VFP3) &&
1798 Token::MOD != op_;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001799
1800 ASSERT((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0)));
1801 Register heap_number_map = r6;
1802
1803 if (ShouldGenerateSmiCode()) {
1804 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1805
1806 // Smi-smi case (overflow).
1807 // Since both are Smis there is no heap number to overwrite, so allocate.
1808 // The new heap number is in r5. r3 and r7 are scratch.
1809 __ AllocateHeapNumber(
1810 r5, r3, r7, heap_number_map, lhs.is(r0) ? &slow_reverse : &slow);
1811
1812 // If we have floating point hardware, inline ADD, SUB, MUL, and DIV,
1813 // using registers d7 and d6 for the double values.
Steve Block44f0eee2011-05-26 01:26:41 +01001814 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001815 CpuFeatures::Scope scope(VFP3);
1816 __ mov(r7, Operand(rhs, ASR, kSmiTagSize));
1817 __ vmov(s15, r7);
1818 __ vcvt_f64_s32(d7, s15);
1819 __ mov(r7, Operand(lhs, ASR, kSmiTagSize));
1820 __ vmov(s13, r7);
1821 __ vcvt_f64_s32(d6, s13);
1822 if (!use_fp_registers) {
1823 __ vmov(r2, r3, d7);
1824 __ vmov(r0, r1, d6);
1825 }
1826 } else {
1827 // Write Smi from rhs to r3 and r2 in double format. r9 is scratch.
1828 __ mov(r7, Operand(rhs));
1829 ConvertToDoubleStub stub1(r3, r2, r7, r9);
1830 __ push(lr);
1831 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
1832 // Write Smi from lhs to r1 and r0 in double format. r9 is scratch.
1833 __ mov(r7, Operand(lhs));
1834 ConvertToDoubleStub stub2(r1, r0, r7, r9);
1835 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
1836 __ pop(lr);
1837 }
1838 __ jmp(&do_the_call); // Tail call. No return.
1839 }
1840
1841 // We branch here if at least one of r0 and r1 is not a Smi.
1842 __ bind(not_smi);
1843 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1844
1845 // After this point we have the left hand side in r1 and the right hand side
1846 // in r0.
1847 if (lhs.is(r0)) {
1848 __ Swap(r0, r1, ip);
1849 }
1850
1851 // The type transition also calculates the answer.
1852 bool generate_code_to_calculate_answer = true;
1853
1854 if (ShouldGenerateFPCode()) {
Steve Block9fac8402011-05-12 15:51:54 +01001855 // DIV has neither SmiSmi fast code nor specialized slow code.
1856 // So don't try to patch a DIV Stub.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001857 if (runtime_operands_type_ == BinaryOpIC::DEFAULT) {
1858 switch (op_) {
1859 case Token::ADD:
1860 case Token::SUB:
1861 case Token::MUL:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001862 GenerateTypeTransition(masm); // Tail call.
1863 generate_code_to_calculate_answer = false;
1864 break;
1865
Steve Block9fac8402011-05-12 15:51:54 +01001866 case Token::DIV:
1867 // DIV has neither SmiSmi fast code nor specialized slow code.
1868 // So don't try to patch a DIV Stub.
1869 break;
1870
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001871 default:
1872 break;
1873 }
1874 }
1875
1876 if (generate_code_to_calculate_answer) {
1877 Label r0_is_smi, r1_is_smi, finished_loading_r0, finished_loading_r1;
1878 if (mode_ == NO_OVERWRITE) {
1879 // In the case where there is no chance of an overwritable float we may
1880 // as well do the allocation immediately while r0 and r1 are untouched.
1881 __ AllocateHeapNumber(r5, r3, r7, heap_number_map, &slow);
1882 }
1883
1884 // Move r0 to a double in r2-r3.
1885 __ tst(r0, Operand(kSmiTagMask));
1886 __ b(eq, &r0_is_smi); // It's a Smi so don't check it's a heap number.
1887 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
1888 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1889 __ cmp(r4, heap_number_map);
1890 __ b(ne, &slow);
1891 if (mode_ == OVERWRITE_RIGHT) {
1892 __ mov(r5, Operand(r0)); // Overwrite this heap number.
1893 }
1894 if (use_fp_registers) {
1895 CpuFeatures::Scope scope(VFP3);
1896 // Load the double from tagged HeapNumber r0 to d7.
1897 __ sub(r7, r0, Operand(kHeapObjectTag));
1898 __ vldr(d7, r7, HeapNumber::kValueOffset);
1899 } else {
1900 // Calling convention says that second double is in r2 and r3.
1901 __ Ldrd(r2, r3, FieldMemOperand(r0, HeapNumber::kValueOffset));
1902 }
1903 __ jmp(&finished_loading_r0);
1904 __ bind(&r0_is_smi);
1905 if (mode_ == OVERWRITE_RIGHT) {
1906 // We can't overwrite a Smi so get address of new heap number into r5.
1907 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1908 }
1909
Steve Block44f0eee2011-05-26 01:26:41 +01001910 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001911 CpuFeatures::Scope scope(VFP3);
1912 // Convert smi in r0 to double in d7.
1913 __ mov(r7, Operand(r0, ASR, kSmiTagSize));
1914 __ vmov(s15, r7);
1915 __ vcvt_f64_s32(d7, s15);
1916 if (!use_fp_registers) {
1917 __ vmov(r2, r3, d7);
1918 }
1919 } else {
1920 // Write Smi from r0 to r3 and r2 in double format.
1921 __ mov(r7, Operand(r0));
1922 ConvertToDoubleStub stub3(r3, r2, r7, r4);
1923 __ push(lr);
1924 __ Call(stub3.GetCode(), RelocInfo::CODE_TARGET);
1925 __ pop(lr);
1926 }
1927
1928 // HEAP_NUMBERS stub is slower than GENERIC on a pair of smis.
1929 // r0 is known to be a smi. If r1 is also a smi then switch to GENERIC.
1930 Label r1_is_not_smi;
Steve Block9fac8402011-05-12 15:51:54 +01001931 if ((runtime_operands_type_ == BinaryOpIC::HEAP_NUMBERS) &&
1932 HasSmiSmiFastPath()) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001933 __ tst(r1, Operand(kSmiTagMask));
1934 __ b(ne, &r1_is_not_smi);
1935 GenerateTypeTransition(masm); // Tail call.
1936 }
1937
1938 __ bind(&finished_loading_r0);
1939
1940 // Move r1 to a double in r0-r1.
1941 __ tst(r1, Operand(kSmiTagMask));
1942 __ b(eq, &r1_is_smi); // It's a Smi so don't check it's a heap number.
1943 __ bind(&r1_is_not_smi);
1944 __ ldr(r4, FieldMemOperand(r1, HeapNumber::kMapOffset));
1945 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1946 __ cmp(r4, heap_number_map);
1947 __ b(ne, &slow);
1948 if (mode_ == OVERWRITE_LEFT) {
1949 __ mov(r5, Operand(r1)); // Overwrite this heap number.
1950 }
1951 if (use_fp_registers) {
1952 CpuFeatures::Scope scope(VFP3);
1953 // Load the double from tagged HeapNumber r1 to d6.
1954 __ sub(r7, r1, Operand(kHeapObjectTag));
1955 __ vldr(d6, r7, HeapNumber::kValueOffset);
1956 } else {
1957 // Calling convention says that first double is in r0 and r1.
1958 __ Ldrd(r0, r1, FieldMemOperand(r1, HeapNumber::kValueOffset));
1959 }
1960 __ jmp(&finished_loading_r1);
1961 __ bind(&r1_is_smi);
1962 if (mode_ == OVERWRITE_LEFT) {
1963 // We can't overwrite a Smi so get address of new heap number into r5.
1964 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
1965 }
1966
Steve Block44f0eee2011-05-26 01:26:41 +01001967 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001968 CpuFeatures::Scope scope(VFP3);
1969 // Convert smi in r1 to double in d6.
1970 __ mov(r7, Operand(r1, ASR, kSmiTagSize));
1971 __ vmov(s13, r7);
1972 __ vcvt_f64_s32(d6, s13);
1973 if (!use_fp_registers) {
1974 __ vmov(r0, r1, d6);
1975 }
1976 } else {
1977 // Write Smi from r1 to r1 and r0 in double format.
1978 __ mov(r7, Operand(r1));
1979 ConvertToDoubleStub stub4(r1, r0, r7, r9);
1980 __ push(lr);
1981 __ Call(stub4.GetCode(), RelocInfo::CODE_TARGET);
1982 __ pop(lr);
1983 }
1984
1985 __ bind(&finished_loading_r1);
1986 }
1987
1988 if (generate_code_to_calculate_answer || do_the_call.is_linked()) {
1989 __ bind(&do_the_call);
1990 // If we are inlining the operation using VFP3 instructions for
1991 // add, subtract, multiply, or divide, the arguments are in d6 and d7.
1992 if (use_fp_registers) {
1993 CpuFeatures::Scope scope(VFP3);
1994 // ARMv7 VFP3 instructions to implement
1995 // double precision, add, subtract, multiply, divide.
1996
1997 if (Token::MUL == op_) {
1998 __ vmul(d5, d6, d7);
1999 } else if (Token::DIV == op_) {
2000 __ vdiv(d5, d6, d7);
2001 } else if (Token::ADD == op_) {
2002 __ vadd(d5, d6, d7);
2003 } else if (Token::SUB == op_) {
2004 __ vsub(d5, d6, d7);
2005 } else {
2006 UNREACHABLE();
2007 }
2008 __ sub(r0, r5, Operand(kHeapObjectTag));
2009 __ vstr(d5, r0, HeapNumber::kValueOffset);
2010 __ add(r0, r0, Operand(kHeapObjectTag));
Steve Block1e0659c2011-05-24 12:43:12 +01002011 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002012 } else {
2013 // If we did not inline the operation, then the arguments are in:
2014 // r0: Left value (least significant part of mantissa).
2015 // r1: Left value (sign, exponent, top of mantissa).
2016 // r2: Right value (least significant part of mantissa).
2017 // r3: Right value (sign, exponent, top of mantissa).
2018 // r5: Address of heap number for result.
2019
2020 __ push(lr); // For later.
2021 __ PrepareCallCFunction(4, r4); // Two doubles count as 4 arguments.
2022 // Call C routine that may not cause GC or other trouble. r5 is callee
2023 // save.
Steve Block44f0eee2011-05-26 01:26:41 +01002024 __ CallCFunction(
2025 ExternalReference::double_fp_operation(op_, masm->isolate()), 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002026 // Store answer in the overwritable heap number.
2027 #if !defined(USE_ARM_EABI)
2028 // Double returned in fp coprocessor register 0 and 1, encoded as
2029 // register cr8. Offsets must be divisible by 4 for coprocessor so we
2030 // need to substract the tag from r5.
2031 __ sub(r4, r5, Operand(kHeapObjectTag));
2032 __ stc(p1, cr8, MemOperand(r4, HeapNumber::kValueOffset));
2033 #else
2034 // Double returned in registers 0 and 1.
2035 __ Strd(r0, r1, FieldMemOperand(r5, HeapNumber::kValueOffset));
2036 #endif
2037 __ mov(r0, Operand(r5));
2038 // And we are done.
2039 __ pop(pc);
2040 }
2041 }
2042 }
2043
2044 if (!generate_code_to_calculate_answer &&
2045 !slow_reverse.is_linked() &&
2046 !slow.is_linked()) {
2047 return;
2048 }
2049
2050 if (lhs.is(r0)) {
2051 __ b(&slow);
2052 __ bind(&slow_reverse);
2053 __ Swap(r0, r1, ip);
2054 }
2055
2056 heap_number_map = no_reg; // Don't use this any more from here on.
2057
2058 // We jump to here if something goes wrong (one param is not a number of any
2059 // sort or new-space allocation fails).
2060 __ bind(&slow);
2061
2062 // Push arguments to the stack
2063 __ Push(r1, r0);
2064
2065 if (Token::ADD == op_) {
2066 // Test for string arguments before calling runtime.
2067 // r1 : first argument
2068 // r0 : second argument
2069 // sp[0] : second argument
2070 // sp[4] : first argument
2071
2072 Label not_strings, not_string1, string1, string1_smi2;
2073 __ tst(r1, Operand(kSmiTagMask));
2074 __ b(eq, &not_string1);
2075 __ CompareObjectType(r1, r2, r2, FIRST_NONSTRING_TYPE);
2076 __ b(ge, &not_string1);
2077
2078 // First argument is a a string, test second.
2079 __ tst(r0, Operand(kSmiTagMask));
2080 __ b(eq, &string1_smi2);
2081 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
2082 __ b(ge, &string1);
2083
2084 // First and second argument are strings.
2085 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
2086 __ TailCallStub(&string_add_stub);
2087
2088 __ bind(&string1_smi2);
2089 // First argument is a string, second is a smi. Try to lookup the number
2090 // string for the smi in the number string cache.
2091 NumberToStringStub::GenerateLookupNumberStringCache(
2092 masm, r0, r2, r4, r5, r6, true, &string1);
2093
2094 // Replace second argument on stack and tailcall string add stub to make
2095 // the result.
2096 __ str(r2, MemOperand(sp, 0));
2097 __ TailCallStub(&string_add_stub);
2098
2099 // Only first argument is a string.
2100 __ bind(&string1);
2101 __ InvokeBuiltin(Builtins::STRING_ADD_LEFT, JUMP_JS);
2102
2103 // First argument was not a string, test second.
2104 __ bind(&not_string1);
2105 __ tst(r0, Operand(kSmiTagMask));
2106 __ b(eq, &not_strings);
2107 __ CompareObjectType(r0, r2, r2, FIRST_NONSTRING_TYPE);
2108 __ b(ge, &not_strings);
2109
2110 // Only second argument is a string.
2111 __ InvokeBuiltin(Builtins::STRING_ADD_RIGHT, JUMP_JS);
2112
2113 __ bind(&not_strings);
2114 }
2115
2116 __ InvokeBuiltin(builtin, JUMP_JS); // Tail call. No return.
2117}
2118
2119
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002120// For bitwise ops where the inputs are not both Smis we here try to determine
2121// whether both inputs are either Smis or at least heap numbers that can be
2122// represented by a 32 bit signed value. We truncate towards zero as required
2123// by the ES spec. If this is the case we do the bitwise op and see if the
2124// result is a Smi. If so, great, otherwise we try to find a heap number to
2125// write the answer into (either by allocating or by overwriting).
2126// On entry the operands are in lhs and rhs. On exit the answer is in r0.
2127void GenericBinaryOpStub::HandleNonSmiBitwiseOp(MacroAssembler* masm,
2128 Register lhs,
2129 Register rhs) {
2130 Label slow, result_not_a_smi;
2131 Label rhs_is_smi, lhs_is_smi;
2132 Label done_checking_rhs, done_checking_lhs;
2133
2134 Register heap_number_map = r6;
2135 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2136
2137 __ tst(lhs, Operand(kSmiTagMask));
2138 __ b(eq, &lhs_is_smi); // It's a Smi so don't check it's a heap number.
2139 __ ldr(r4, FieldMemOperand(lhs, HeapNumber::kMapOffset));
2140 __ cmp(r4, heap_number_map);
2141 __ b(ne, &slow);
Steve Block1e0659c2011-05-24 12:43:12 +01002142 __ ConvertToInt32(lhs, r3, r5, r4, d0, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002143 __ jmp(&done_checking_lhs);
2144 __ bind(&lhs_is_smi);
2145 __ mov(r3, Operand(lhs, ASR, 1));
2146 __ bind(&done_checking_lhs);
2147
2148 __ tst(rhs, Operand(kSmiTagMask));
2149 __ b(eq, &rhs_is_smi); // It's a Smi so don't check it's a heap number.
2150 __ ldr(r4, FieldMemOperand(rhs, HeapNumber::kMapOffset));
2151 __ cmp(r4, heap_number_map);
2152 __ b(ne, &slow);
Steve Block1e0659c2011-05-24 12:43:12 +01002153 __ ConvertToInt32(rhs, r2, r5, r4, d0, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002154 __ jmp(&done_checking_rhs);
2155 __ bind(&rhs_is_smi);
2156 __ mov(r2, Operand(rhs, ASR, 1));
2157 __ bind(&done_checking_rhs);
2158
2159 ASSERT(((lhs.is(r0) && rhs.is(r1)) || (lhs.is(r1) && rhs.is(r0))));
2160
2161 // r0 and r1: Original operands (Smi or heap numbers).
2162 // r2 and r3: Signed int32 operands.
2163 switch (op_) {
2164 case Token::BIT_OR: __ orr(r2, r2, Operand(r3)); break;
2165 case Token::BIT_XOR: __ eor(r2, r2, Operand(r3)); break;
2166 case Token::BIT_AND: __ and_(r2, r2, Operand(r3)); break;
2167 case Token::SAR:
2168 // Use only the 5 least significant bits of the shift count.
2169 __ and_(r2, r2, Operand(0x1f));
2170 __ mov(r2, Operand(r3, ASR, r2));
2171 break;
2172 case Token::SHR:
2173 // Use only the 5 least significant bits of the shift count.
2174 __ and_(r2, r2, Operand(0x1f));
2175 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2176 // SHR is special because it is required to produce a positive answer.
2177 // The code below for writing into heap numbers isn't capable of writing
2178 // the register as an unsigned int so we go to slow case if we hit this
2179 // case.
Steve Block44f0eee2011-05-26 01:26:41 +01002180 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002181 __ b(mi, &result_not_a_smi);
2182 } else {
2183 __ b(mi, &slow);
2184 }
2185 break;
2186 case Token::SHL:
2187 // Use only the 5 least significant bits of the shift count.
2188 __ and_(r2, r2, Operand(0x1f));
2189 __ mov(r2, Operand(r3, LSL, r2));
2190 break;
2191 default: UNREACHABLE();
2192 }
2193 // check that the *signed* result fits in a smi
2194 __ add(r3, r2, Operand(0x40000000), SetCC);
2195 __ b(mi, &result_not_a_smi);
2196 __ mov(r0, Operand(r2, LSL, kSmiTagSize));
2197 __ Ret();
2198
2199 Label have_to_allocate, got_a_heap_number;
2200 __ bind(&result_not_a_smi);
2201 switch (mode_) {
2202 case OVERWRITE_RIGHT: {
2203 __ tst(rhs, Operand(kSmiTagMask));
2204 __ b(eq, &have_to_allocate);
2205 __ mov(r5, Operand(rhs));
2206 break;
2207 }
2208 case OVERWRITE_LEFT: {
2209 __ tst(lhs, Operand(kSmiTagMask));
2210 __ b(eq, &have_to_allocate);
2211 __ mov(r5, Operand(lhs));
2212 break;
2213 }
2214 case NO_OVERWRITE: {
2215 // Get a new heap number in r5. r4 and r7 are scratch.
2216 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
2217 }
2218 default: break;
2219 }
2220 __ bind(&got_a_heap_number);
2221 // r2: Answer as signed int32.
2222 // r5: Heap number to write answer into.
2223
2224 // Nothing can go wrong now, so move the heap number to r0, which is the
2225 // result.
2226 __ mov(r0, Operand(r5));
2227
Steve Block44f0eee2011-05-26 01:26:41 +01002228 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002229 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted.
2230 CpuFeatures::Scope scope(VFP3);
2231 __ vmov(s0, r2);
2232 if (op_ == Token::SHR) {
2233 __ vcvt_f64_u32(d0, s0);
2234 } else {
2235 __ vcvt_f64_s32(d0, s0);
2236 }
2237 __ sub(r3, r0, Operand(kHeapObjectTag));
2238 __ vstr(d0, r3, HeapNumber::kValueOffset);
2239 __ Ret();
2240 } else {
2241 // Tail call that writes the int32 in r2 to the heap number in r0, using
2242 // r3 as scratch. r0 is preserved and returned.
2243 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
2244 __ TailCallStub(&stub);
2245 }
2246
2247 if (mode_ != NO_OVERWRITE) {
2248 __ bind(&have_to_allocate);
2249 // Get a new heap number in r5. r4 and r7 are scratch.
2250 __ AllocateHeapNumber(r5, r4, r7, heap_number_map, &slow);
2251 __ jmp(&got_a_heap_number);
2252 }
2253
2254 // If all else failed then we go to the runtime system.
2255 __ bind(&slow);
2256 __ Push(lhs, rhs); // Restore stack.
2257 switch (op_) {
2258 case Token::BIT_OR:
2259 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
2260 break;
2261 case Token::BIT_AND:
2262 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
2263 break;
2264 case Token::BIT_XOR:
2265 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
2266 break;
2267 case Token::SAR:
2268 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
2269 break;
2270 case Token::SHR:
2271 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
2272 break;
2273 case Token::SHL:
2274 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
2275 break;
2276 default:
2277 UNREACHABLE();
2278 }
2279}
2280
2281
2282
2283
2284// This function takes the known int in a register for the cases
2285// where it doesn't know a good trick, and may deliver
2286// a result that needs shifting.
2287static void MultiplyByKnownIntInStub(
2288 MacroAssembler* masm,
2289 Register result,
2290 Register source,
2291 Register known_int_register, // Smi tagged.
2292 int known_int,
2293 int* required_shift) { // Including Smi tag shift
2294 switch (known_int) {
2295 case 3:
2296 __ add(result, source, Operand(source, LSL, 1));
2297 *required_shift = 1;
2298 break;
2299 case 5:
2300 __ add(result, source, Operand(source, LSL, 2));
2301 *required_shift = 1;
2302 break;
2303 case 6:
2304 __ add(result, source, Operand(source, LSL, 1));
2305 *required_shift = 2;
2306 break;
2307 case 7:
2308 __ rsb(result, source, Operand(source, LSL, 3));
2309 *required_shift = 1;
2310 break;
2311 case 9:
2312 __ add(result, source, Operand(source, LSL, 3));
2313 *required_shift = 1;
2314 break;
2315 case 10:
2316 __ add(result, source, Operand(source, LSL, 2));
2317 *required_shift = 2;
2318 break;
2319 default:
2320 ASSERT(!IsPowerOf2(known_int)); // That would be very inefficient.
2321 __ mul(result, source, known_int_register);
2322 *required_shift = 0;
2323 }
2324}
2325
2326
2327// This uses versions of the sum-of-digits-to-see-if-a-number-is-divisible-by-3
2328// trick. See http://en.wikipedia.org/wiki/Divisibility_rule
2329// Takes the sum of the digits base (mask + 1) repeatedly until we have a
2330// number from 0 to mask. On exit the 'eq' condition flags are set if the
2331// answer is exactly the mask.
2332void IntegerModStub::DigitSum(MacroAssembler* masm,
2333 Register lhs,
2334 int mask,
2335 int shift,
2336 Label* entry) {
2337 ASSERT(mask > 0);
2338 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
2339 Label loop;
2340 __ bind(&loop);
2341 __ and_(ip, lhs, Operand(mask));
2342 __ add(lhs, ip, Operand(lhs, LSR, shift));
2343 __ bind(entry);
2344 __ cmp(lhs, Operand(mask));
2345 __ b(gt, &loop);
2346}
2347
2348
2349void IntegerModStub::DigitSum(MacroAssembler* masm,
2350 Register lhs,
2351 Register scratch,
2352 int mask,
2353 int shift1,
2354 int shift2,
2355 Label* entry) {
2356 ASSERT(mask > 0);
2357 ASSERT(mask <= 0xff); // This ensures we don't need ip to use it.
2358 Label loop;
2359 __ bind(&loop);
2360 __ bic(scratch, lhs, Operand(mask));
2361 __ and_(ip, lhs, Operand(mask));
2362 __ add(lhs, ip, Operand(lhs, LSR, shift1));
2363 __ add(lhs, lhs, Operand(scratch, LSR, shift2));
2364 __ bind(entry);
2365 __ cmp(lhs, Operand(mask));
2366 __ b(gt, &loop);
2367}
2368
2369
2370// Splits the number into two halves (bottom half has shift bits). The top
2371// half is subtracted from the bottom half. If the result is negative then
2372// rhs is added.
2373void IntegerModStub::ModGetInRangeBySubtraction(MacroAssembler* masm,
2374 Register lhs,
2375 int shift,
2376 int rhs) {
2377 int mask = (1 << shift) - 1;
2378 __ and_(ip, lhs, Operand(mask));
2379 __ sub(lhs, ip, Operand(lhs, LSR, shift), SetCC);
2380 __ add(lhs, lhs, Operand(rhs), LeaveCC, mi);
2381}
2382
2383
2384void IntegerModStub::ModReduce(MacroAssembler* masm,
2385 Register lhs,
2386 int max,
2387 int denominator) {
2388 int limit = denominator;
2389 while (limit * 2 <= max) limit *= 2;
2390 while (limit >= denominator) {
2391 __ cmp(lhs, Operand(limit));
2392 __ sub(lhs, lhs, Operand(limit), LeaveCC, ge);
2393 limit >>= 1;
2394 }
2395}
2396
2397
2398void IntegerModStub::ModAnswer(MacroAssembler* masm,
2399 Register result,
2400 Register shift_distance,
2401 Register mask_bits,
2402 Register sum_of_digits) {
2403 __ add(result, mask_bits, Operand(sum_of_digits, LSL, shift_distance));
2404 __ Ret();
2405}
2406
2407
2408// See comment for class.
2409void IntegerModStub::Generate(MacroAssembler* masm) {
2410 __ mov(lhs_, Operand(lhs_, LSR, shift_distance_));
2411 __ bic(odd_number_, odd_number_, Operand(1));
2412 __ mov(odd_number_, Operand(odd_number_, LSL, 1));
2413 // We now have (odd_number_ - 1) * 2 in the register.
2414 // Build a switch out of branches instead of data because it avoids
2415 // having to teach the assembler about intra-code-object pointers
2416 // that are not in relative branch instructions.
2417 Label mod3, mod5, mod7, mod9, mod11, mod13, mod15, mod17, mod19;
2418 Label mod21, mod23, mod25;
2419 { Assembler::BlockConstPoolScope block_const_pool(masm);
2420 __ add(pc, pc, Operand(odd_number_));
2421 // When you read pc it is always 8 ahead, but when you write it you always
2422 // write the actual value. So we put in two nops to take up the slack.
2423 __ nop();
2424 __ nop();
2425 __ b(&mod3);
2426 __ b(&mod5);
2427 __ b(&mod7);
2428 __ b(&mod9);
2429 __ b(&mod11);
2430 __ b(&mod13);
2431 __ b(&mod15);
2432 __ b(&mod17);
2433 __ b(&mod19);
2434 __ b(&mod21);
2435 __ b(&mod23);
2436 __ b(&mod25);
2437 }
2438
2439 // For each denominator we find a multiple that is almost only ones
2440 // when expressed in binary. Then we do the sum-of-digits trick for
2441 // that number. If the multiple is not 1 then we have to do a little
2442 // more work afterwards to get the answer into the 0-denominator-1
2443 // range.
2444 DigitSum(masm, lhs_, 3, 2, &mod3); // 3 = b11.
2445 __ sub(lhs_, lhs_, Operand(3), LeaveCC, eq);
2446 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2447
2448 DigitSum(masm, lhs_, 0xf, 4, &mod5); // 5 * 3 = b1111.
2449 ModGetInRangeBySubtraction(masm, lhs_, 2, 5);
2450 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2451
2452 DigitSum(masm, lhs_, 7, 3, &mod7); // 7 = b111.
2453 __ sub(lhs_, lhs_, Operand(7), LeaveCC, eq);
2454 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2455
2456 DigitSum(masm, lhs_, 0x3f, 6, &mod9); // 7 * 9 = b111111.
2457 ModGetInRangeBySubtraction(masm, lhs_, 3, 9);
2458 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2459
2460 DigitSum(masm, lhs_, r5, 0x3f, 6, 3, &mod11); // 5 * 11 = b110111.
2461 ModReduce(masm, lhs_, 0x3f, 11);
2462 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2463
2464 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod13); // 19 * 13 = b11110111.
2465 ModReduce(masm, lhs_, 0xff, 13);
2466 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2467
2468 DigitSum(masm, lhs_, 0xf, 4, &mod15); // 15 = b1111.
2469 __ sub(lhs_, lhs_, Operand(15), LeaveCC, eq);
2470 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2471
2472 DigitSum(masm, lhs_, 0xff, 8, &mod17); // 15 * 17 = b11111111.
2473 ModGetInRangeBySubtraction(masm, lhs_, 4, 17);
2474 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2475
2476 DigitSum(masm, lhs_, r5, 0xff, 8, 5, &mod19); // 13 * 19 = b11110111.
2477 ModReduce(masm, lhs_, 0xff, 19);
2478 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2479
2480 DigitSum(masm, lhs_, 0x3f, 6, &mod21); // 3 * 21 = b111111.
2481 ModReduce(masm, lhs_, 0x3f, 21);
2482 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2483
2484 DigitSum(masm, lhs_, r5, 0xff, 8, 7, &mod23); // 11 * 23 = b11111101.
2485 ModReduce(masm, lhs_, 0xff, 23);
2486 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2487
2488 DigitSum(masm, lhs_, r5, 0x7f, 7, 6, &mod25); // 5 * 25 = b1111101.
2489 ModReduce(masm, lhs_, 0x7f, 25);
2490 ModAnswer(masm, result_, shift_distance_, mask_bits_, lhs_);
2491}
2492
2493
2494void GenericBinaryOpStub::Generate(MacroAssembler* masm) {
2495 // lhs_ : x
2496 // rhs_ : y
2497 // r0 : result
2498
2499 Register result = r0;
2500 Register lhs = lhs_;
2501 Register rhs = rhs_;
2502
2503 // This code can't cope with other register allocations yet.
2504 ASSERT(result.is(r0) &&
2505 ((lhs.is(r0) && rhs.is(r1)) ||
2506 (lhs.is(r1) && rhs.is(r0))));
2507
2508 Register smi_test_reg = r7;
2509 Register scratch = r9;
2510
2511 // All ops need to know whether we are dealing with two Smis. Set up
2512 // smi_test_reg to tell us that.
2513 if (ShouldGenerateSmiCode()) {
2514 __ orr(smi_test_reg, lhs, Operand(rhs));
2515 }
2516
2517 switch (op_) {
2518 case Token::ADD: {
2519 Label not_smi;
2520 // Fast path.
2521 if (ShouldGenerateSmiCode()) {
2522 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
2523 __ tst(smi_test_reg, Operand(kSmiTagMask));
2524 __ b(ne, &not_smi);
2525 __ add(r0, r1, Operand(r0), SetCC); // Add y optimistically.
2526 // Return if no overflow.
2527 __ Ret(vc);
2528 __ sub(r0, r0, Operand(r1)); // Revert optimistic add.
2529 }
2530 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::ADD);
2531 break;
2532 }
2533
2534 case Token::SUB: {
2535 Label not_smi;
2536 // Fast path.
2537 if (ShouldGenerateSmiCode()) {
2538 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
2539 __ tst(smi_test_reg, Operand(kSmiTagMask));
2540 __ b(ne, &not_smi);
2541 if (lhs.is(r1)) {
2542 __ sub(r0, r1, Operand(r0), SetCC); // Subtract y optimistically.
2543 // Return if no overflow.
2544 __ Ret(vc);
2545 __ sub(r0, r1, Operand(r0)); // Revert optimistic subtract.
2546 } else {
2547 __ sub(r0, r0, Operand(r1), SetCC); // Subtract y optimistically.
2548 // Return if no overflow.
2549 __ Ret(vc);
2550 __ add(r0, r0, Operand(r1)); // Revert optimistic subtract.
2551 }
2552 }
2553 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::SUB);
2554 break;
2555 }
2556
2557 case Token::MUL: {
2558 Label not_smi, slow;
2559 if (ShouldGenerateSmiCode()) {
2560 STATIC_ASSERT(kSmiTag == 0); // adjust code below
2561 __ tst(smi_test_reg, Operand(kSmiTagMask));
2562 Register scratch2 = smi_test_reg;
2563 smi_test_reg = no_reg;
2564 __ b(ne, &not_smi);
2565 // Remove tag from one operand (but keep sign), so that result is Smi.
2566 __ mov(ip, Operand(rhs, ASR, kSmiTagSize));
2567 // Do multiplication
2568 // scratch = lower 32 bits of ip * lhs.
2569 __ smull(scratch, scratch2, lhs, ip);
2570 // Go slow on overflows (overflow bit is not set).
2571 __ mov(ip, Operand(scratch, ASR, 31));
2572 // No overflow if higher 33 bits are identical.
2573 __ cmp(ip, Operand(scratch2));
2574 __ b(ne, &slow);
2575 // Go slow on zero result to handle -0.
2576 __ tst(scratch, Operand(scratch));
2577 __ mov(result, Operand(scratch), LeaveCC, ne);
2578 __ Ret(ne);
2579 // We need -0 if we were multiplying a negative number with 0 to get 0.
2580 // We know one of them was zero.
2581 __ add(scratch2, rhs, Operand(lhs), SetCC);
2582 __ mov(result, Operand(Smi::FromInt(0)), LeaveCC, pl);
2583 __ Ret(pl); // Return Smi 0 if the non-zero one was positive.
2584 // Slow case. We fall through here if we multiplied a negative number
2585 // with 0, because that would mean we should produce -0.
2586 __ bind(&slow);
2587 }
2588 HandleBinaryOpSlowCases(masm, &not_smi, lhs, rhs, Builtins::MUL);
2589 break;
2590 }
2591
2592 case Token::DIV:
2593 case Token::MOD: {
2594 Label not_smi;
2595 if (ShouldGenerateSmiCode() && specialized_on_rhs_) {
2596 Label lhs_is_unsuitable;
Steve Block1e0659c2011-05-24 12:43:12 +01002597 __ JumpIfNotSmi(lhs, &not_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002598 if (IsPowerOf2(constant_rhs_)) {
2599 if (op_ == Token::MOD) {
2600 __ and_(rhs,
2601 lhs,
2602 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)),
2603 SetCC);
2604 // We now have the answer, but if the input was negative we also
2605 // have the sign bit. Our work is done if the result is
2606 // positive or zero:
2607 if (!rhs.is(r0)) {
2608 __ mov(r0, rhs, LeaveCC, pl);
2609 }
2610 __ Ret(pl);
2611 // A mod of a negative left hand side must return a negative number.
2612 // Unfortunately if the answer is 0 then we must return -0. And we
2613 // already optimistically trashed rhs so we may need to restore it.
2614 __ eor(rhs, rhs, Operand(0x80000000u), SetCC);
2615 // Next two instructions are conditional on the answer being -0.
2616 __ mov(rhs, Operand(Smi::FromInt(constant_rhs_)), LeaveCC, eq);
2617 __ b(eq, &lhs_is_unsuitable);
2618 // We need to subtract the dividend. Eg. -3 % 4 == -3.
2619 __ sub(result, rhs, Operand(Smi::FromInt(constant_rhs_)));
2620 } else {
2621 ASSERT(op_ == Token::DIV);
2622 __ tst(lhs,
2623 Operand(0x80000000u | ((constant_rhs_ << kSmiTagSize) - 1)));
2624 __ b(ne, &lhs_is_unsuitable); // Go slow on negative or remainder.
2625 int shift = 0;
2626 int d = constant_rhs_;
2627 while ((d & 1) == 0) {
2628 d >>= 1;
2629 shift++;
2630 }
2631 __ mov(r0, Operand(lhs, LSR, shift));
2632 __ bic(r0, r0, Operand(kSmiTagMask));
2633 }
2634 } else {
2635 // Not a power of 2.
2636 __ tst(lhs, Operand(0x80000000u));
2637 __ b(ne, &lhs_is_unsuitable);
2638 // Find a fixed point reciprocal of the divisor so we can divide by
2639 // multiplying.
2640 double divisor = 1.0 / constant_rhs_;
2641 int shift = 32;
2642 double scale = 4294967296.0; // 1 << 32.
2643 uint32_t mul;
2644 // Maximise the precision of the fixed point reciprocal.
2645 while (true) {
2646 mul = static_cast<uint32_t>(scale * divisor);
2647 if (mul >= 0x7fffffff) break;
2648 scale *= 2.0;
2649 shift++;
2650 }
2651 mul++;
2652 Register scratch2 = smi_test_reg;
2653 smi_test_reg = no_reg;
2654 __ mov(scratch2, Operand(mul));
2655 __ umull(scratch, scratch2, scratch2, lhs);
2656 __ mov(scratch2, Operand(scratch2, LSR, shift - 31));
2657 // scratch2 is lhs / rhs. scratch2 is not Smi tagged.
2658 // rhs is still the known rhs. rhs is Smi tagged.
2659 // lhs is still the unkown lhs. lhs is Smi tagged.
2660 int required_scratch_shift = 0; // Including the Smi tag shift of 1.
2661 // scratch = scratch2 * rhs.
2662 MultiplyByKnownIntInStub(masm,
2663 scratch,
2664 scratch2,
2665 rhs,
2666 constant_rhs_,
2667 &required_scratch_shift);
2668 // scratch << required_scratch_shift is now the Smi tagged rhs *
2669 // (lhs / rhs) where / indicates integer division.
2670 if (op_ == Token::DIV) {
2671 __ cmp(lhs, Operand(scratch, LSL, required_scratch_shift));
2672 __ b(ne, &lhs_is_unsuitable); // There was a remainder.
2673 __ mov(result, Operand(scratch2, LSL, kSmiTagSize));
2674 } else {
2675 ASSERT(op_ == Token::MOD);
2676 __ sub(result, lhs, Operand(scratch, LSL, required_scratch_shift));
2677 }
2678 }
2679 __ Ret();
2680 __ bind(&lhs_is_unsuitable);
2681 } else if (op_ == Token::MOD &&
2682 runtime_operands_type_ != BinaryOpIC::HEAP_NUMBERS &&
2683 runtime_operands_type_ != BinaryOpIC::STRINGS) {
2684 // Do generate a bit of smi code for modulus even though the default for
2685 // modulus is not to do it, but as the ARM processor has no coprocessor
2686 // support for modulus checking for smis makes sense. We can handle
2687 // 1 to 25 times any power of 2. This covers over half the numbers from
2688 // 1 to 100 including all of the first 25. (Actually the constants < 10
2689 // are handled above by reciprocal multiplication. We only get here for
2690 // those cases if the right hand side is not a constant or for cases
2691 // like 192 which is 3*2^6 and ends up in the 3 case in the integer mod
2692 // stub.)
2693 Label slow;
2694 Label not_power_of_2;
2695 ASSERT(!ShouldGenerateSmiCode());
2696 STATIC_ASSERT(kSmiTag == 0); // Adjust code below.
2697 // Check for two positive smis.
2698 __ orr(smi_test_reg, lhs, Operand(rhs));
2699 __ tst(smi_test_reg, Operand(0x80000000u | kSmiTagMask));
2700 __ b(ne, &slow);
2701 // Check that rhs is a power of two and not zero.
2702 Register mask_bits = r3;
2703 __ sub(scratch, rhs, Operand(1), SetCC);
2704 __ b(mi, &slow);
2705 __ and_(mask_bits, rhs, Operand(scratch), SetCC);
2706 __ b(ne, &not_power_of_2);
2707 // Calculate power of two modulus.
2708 __ and_(result, lhs, Operand(scratch));
2709 __ Ret();
2710
2711 __ bind(&not_power_of_2);
2712 __ eor(scratch, scratch, Operand(mask_bits));
2713 // At least two bits are set in the modulus. The high one(s) are in
2714 // mask_bits and the low one is scratch + 1.
2715 __ and_(mask_bits, scratch, Operand(lhs));
2716 Register shift_distance = scratch;
2717 scratch = no_reg;
2718
2719 // The rhs consists of a power of 2 multiplied by some odd number.
2720 // The power-of-2 part we handle by putting the corresponding bits
2721 // from the lhs in the mask_bits register, and the power in the
2722 // shift_distance register. Shift distance is never 0 due to Smi
2723 // tagging.
2724 __ CountLeadingZeros(r4, shift_distance, shift_distance);
2725 __ rsb(shift_distance, r4, Operand(32));
2726
2727 // Now we need to find out what the odd number is. The last bit is
2728 // always 1.
2729 Register odd_number = r4;
2730 __ mov(odd_number, Operand(rhs, LSR, shift_distance));
2731 __ cmp(odd_number, Operand(25));
2732 __ b(gt, &slow);
2733
2734 IntegerModStub stub(
2735 result, shift_distance, odd_number, mask_bits, lhs, r5);
2736 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET); // Tail call.
2737
2738 __ bind(&slow);
2739 }
2740 HandleBinaryOpSlowCases(
2741 masm,
2742 &not_smi,
2743 lhs,
2744 rhs,
2745 op_ == Token::MOD ? Builtins::MOD : Builtins::DIV);
2746 break;
2747 }
2748
2749 case Token::BIT_OR:
2750 case Token::BIT_AND:
2751 case Token::BIT_XOR:
2752 case Token::SAR:
2753 case Token::SHR:
2754 case Token::SHL: {
2755 Label slow;
2756 STATIC_ASSERT(kSmiTag == 0); // adjust code below
2757 __ tst(smi_test_reg, Operand(kSmiTagMask));
2758 __ b(ne, &slow);
2759 Register scratch2 = smi_test_reg;
2760 smi_test_reg = no_reg;
2761 switch (op_) {
2762 case Token::BIT_OR: __ orr(result, rhs, Operand(lhs)); break;
2763 case Token::BIT_AND: __ and_(result, rhs, Operand(lhs)); break;
2764 case Token::BIT_XOR: __ eor(result, rhs, Operand(lhs)); break;
2765 case Token::SAR:
2766 // Remove tags from right operand.
2767 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2768 __ mov(result, Operand(lhs, ASR, scratch2));
2769 // Smi tag result.
2770 __ bic(result, result, Operand(kSmiTagMask));
2771 break;
2772 case Token::SHR:
2773 // Remove tags from operands. We can't do this on a 31 bit number
2774 // because then the 0s get shifted into bit 30 instead of bit 31.
2775 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
2776 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2777 __ mov(scratch, Operand(scratch, LSR, scratch2));
2778 // Unsigned shift is not allowed to produce a negative number, so
2779 // check the sign bit and the sign bit after Smi tagging.
2780 __ tst(scratch, Operand(0xc0000000));
2781 __ b(ne, &slow);
2782 // Smi tag result.
2783 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
2784 break;
2785 case Token::SHL:
2786 // Remove tags from operands.
2787 __ mov(scratch, Operand(lhs, ASR, kSmiTagSize)); // x
2788 __ GetLeastBitsFromSmi(scratch2, rhs, 5);
2789 __ mov(scratch, Operand(scratch, LSL, scratch2));
2790 // Check that the signed result fits in a Smi.
2791 __ add(scratch2, scratch, Operand(0x40000000), SetCC);
2792 __ b(mi, &slow);
2793 __ mov(result, Operand(scratch, LSL, kSmiTagSize));
2794 break;
2795 default: UNREACHABLE();
2796 }
2797 __ Ret();
2798 __ bind(&slow);
2799 HandleNonSmiBitwiseOp(masm, lhs, rhs);
2800 break;
2801 }
2802
2803 default: UNREACHABLE();
2804 }
2805 // This code should be unreachable.
2806 __ stop("Unreachable");
2807
2808 // Generate an unreachable reference to the DEFAULT stub so that it can be
2809 // found at the end of this stub when clearing ICs at GC.
2810 // TODO(kaznacheev): Check performance impact and get rid of this.
2811 if (runtime_operands_type_ != BinaryOpIC::DEFAULT) {
2812 GenericBinaryOpStub uninit(MinorKey(), BinaryOpIC::DEFAULT);
2813 __ CallStub(&uninit);
2814 }
2815}
2816
2817
2818void GenericBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
2819 Label get_result;
2820
2821 __ Push(r1, r0);
2822
2823 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
2824 __ mov(r1, Operand(Smi::FromInt(op_)));
2825 __ mov(r0, Operand(Smi::FromInt(runtime_operands_type_)));
2826 __ Push(r2, r1, r0);
2827
2828 __ TailCallExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01002829 ExternalReference(IC_Utility(IC::kBinaryOp_Patch), masm->isolate()),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002830 5,
2831 1);
2832}
2833
2834
2835Handle<Code> GetBinaryOpStub(int key, BinaryOpIC::TypeInfo type_info) {
2836 GenericBinaryOpStub stub(key, type_info);
2837 return stub.GetCode();
2838}
2839
2840
Ben Murdochb0fe1622011-05-05 13:52:32 +01002841Handle<Code> GetTypeRecordingBinaryOpStub(int key,
2842 TRBinaryOpIC::TypeInfo type_info,
2843 TRBinaryOpIC::TypeInfo result_type_info) {
Steve Block1e0659c2011-05-24 12:43:12 +01002844 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
2845 return stub.GetCode();
2846}
2847
2848
2849void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
2850 Label get_result;
2851
2852 __ Push(r1, r0);
2853
2854 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
2855 __ mov(r1, Operand(Smi::FromInt(op_)));
2856 __ mov(r0, Operand(Smi::FromInt(operands_type_)));
2857 __ Push(r2, r1, r0);
2858
2859 __ TailCallExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01002860 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
2861 masm->isolate()),
Steve Block1e0659c2011-05-24 12:43:12 +01002862 5,
2863 1);
2864}
2865
2866
2867void TypeRecordingBinaryOpStub::GenerateTypeTransitionWithSavedArgs(
2868 MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002869 UNIMPLEMENTED();
Steve Block1e0659c2011-05-24 12:43:12 +01002870}
2871
2872
2873void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
2874 switch (operands_type_) {
2875 case TRBinaryOpIC::UNINITIALIZED:
2876 GenerateTypeTransition(masm);
2877 break;
2878 case TRBinaryOpIC::SMI:
2879 GenerateSmiStub(masm);
2880 break;
2881 case TRBinaryOpIC::INT32:
2882 GenerateInt32Stub(masm);
2883 break;
2884 case TRBinaryOpIC::HEAP_NUMBER:
2885 GenerateHeapNumberStub(masm);
2886 break;
Steve Block44f0eee2011-05-26 01:26:41 +01002887 case TRBinaryOpIC::ODDBALL:
2888 GenerateOddballStub(masm);
2889 break;
Steve Block1e0659c2011-05-24 12:43:12 +01002890 case TRBinaryOpIC::STRING:
2891 GenerateStringStub(masm);
2892 break;
2893 case TRBinaryOpIC::GENERIC:
2894 GenerateGeneric(masm);
2895 break;
2896 default:
2897 UNREACHABLE();
2898 }
2899}
2900
2901
2902const char* TypeRecordingBinaryOpStub::GetName() {
2903 if (name_ != NULL) return name_;
2904 const int kMaxNameLength = 100;
Steve Block44f0eee2011-05-26 01:26:41 +01002905 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
2906 kMaxNameLength);
Steve Block1e0659c2011-05-24 12:43:12 +01002907 if (name_ == NULL) return "OOM";
2908 const char* op_name = Token::Name(op_);
2909 const char* overwrite_name;
2910 switch (mode_) {
2911 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
2912 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
2913 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
2914 default: overwrite_name = "UnknownOverwrite"; break;
2915 }
2916
2917 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
2918 "TypeRecordingBinaryOpStub_%s_%s_%s",
2919 op_name,
2920 overwrite_name,
2921 TRBinaryOpIC::GetName(operands_type_));
2922 return name_;
2923}
2924
2925
2926void TypeRecordingBinaryOpStub::GenerateSmiSmiOperation(
2927 MacroAssembler* masm) {
2928 Register left = r1;
2929 Register right = r0;
2930 Register scratch1 = r7;
2931 Register scratch2 = r9;
2932
2933 ASSERT(right.is(r0));
2934 STATIC_ASSERT(kSmiTag == 0);
2935
2936 Label not_smi_result;
2937 switch (op_) {
2938 case Token::ADD:
2939 __ add(right, left, Operand(right), SetCC); // Add optimistically.
2940 __ Ret(vc);
2941 __ sub(right, right, Operand(left)); // Revert optimistic add.
2942 break;
2943 case Token::SUB:
2944 __ sub(right, left, Operand(right), SetCC); // Subtract optimistically.
2945 __ Ret(vc);
2946 __ sub(right, left, Operand(right)); // Revert optimistic subtract.
2947 break;
2948 case Token::MUL:
2949 // Remove tag from one of the operands. This way the multiplication result
2950 // will be a smi if it fits the smi range.
2951 __ SmiUntag(ip, right);
2952 // Do multiplication
2953 // scratch1 = lower 32 bits of ip * left.
2954 // scratch2 = higher 32 bits of ip * left.
2955 __ smull(scratch1, scratch2, left, ip);
2956 // Check for overflowing the smi range - no overflow if higher 33 bits of
2957 // the result are identical.
2958 __ mov(ip, Operand(scratch1, ASR, 31));
2959 __ cmp(ip, Operand(scratch2));
2960 __ b(ne, &not_smi_result);
2961 // Go slow on zero result to handle -0.
2962 __ tst(scratch1, Operand(scratch1));
2963 __ mov(right, Operand(scratch1), LeaveCC, ne);
2964 __ Ret(ne);
2965 // We need -0 if we were multiplying a negative number with 0 to get 0.
2966 // We know one of them was zero.
2967 __ add(scratch2, right, Operand(left), SetCC);
2968 __ mov(right, Operand(Smi::FromInt(0)), LeaveCC, pl);
2969 __ Ret(pl); // Return smi 0 if the non-zero one was positive.
2970 // We fall through here if we multiplied a negative number with 0, because
2971 // that would mean we should produce -0.
2972 break;
2973 case Token::DIV:
2974 // Check for power of two on the right hand side.
2975 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
2976 // Check for positive and no remainder (scratch1 contains right - 1).
2977 __ orr(scratch2, scratch1, Operand(0x80000000u));
2978 __ tst(left, scratch2);
2979 __ b(ne, &not_smi_result);
2980
2981 // Perform division by shifting.
2982 __ CountLeadingZeros(scratch1, scratch1, scratch2);
2983 __ rsb(scratch1, scratch1, Operand(31));
2984 __ mov(right, Operand(left, LSR, scratch1));
2985 __ Ret();
2986 break;
2987 case Token::MOD:
2988 // Check for two positive smis.
2989 __ orr(scratch1, left, Operand(right));
2990 __ tst(scratch1, Operand(0x80000000u | kSmiTagMask));
2991 __ b(ne, &not_smi_result);
2992
2993 // Check for power of two on the right hand side.
2994 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
2995
2996 // Perform modulus by masking.
2997 __ and_(right, left, Operand(scratch1));
2998 __ Ret();
2999 break;
3000 case Token::BIT_OR:
3001 __ orr(right, left, Operand(right));
3002 __ Ret();
3003 break;
3004 case Token::BIT_AND:
3005 __ and_(right, left, Operand(right));
3006 __ Ret();
3007 break;
3008 case Token::BIT_XOR:
3009 __ eor(right, left, Operand(right));
3010 __ Ret();
3011 break;
3012 case Token::SAR:
3013 // Remove tags from right operand.
3014 __ GetLeastBitsFromSmi(scratch1, right, 5);
3015 __ mov(right, Operand(left, ASR, scratch1));
3016 // Smi tag result.
3017 __ bic(right, right, Operand(kSmiTagMask));
3018 __ Ret();
3019 break;
3020 case Token::SHR:
3021 // Remove tags from operands. We can't do this on a 31 bit number
3022 // because then the 0s get shifted into bit 30 instead of bit 31.
3023 __ SmiUntag(scratch1, left);
3024 __ GetLeastBitsFromSmi(scratch2, right, 5);
3025 __ mov(scratch1, Operand(scratch1, LSR, scratch2));
3026 // Unsigned shift is not allowed to produce a negative number, so
3027 // check the sign bit and the sign bit after Smi tagging.
3028 __ tst(scratch1, Operand(0xc0000000));
3029 __ b(ne, &not_smi_result);
3030 // Smi tag result.
3031 __ SmiTag(right, scratch1);
3032 __ Ret();
3033 break;
3034 case Token::SHL:
3035 // Remove tags from operands.
3036 __ SmiUntag(scratch1, left);
3037 __ GetLeastBitsFromSmi(scratch2, right, 5);
3038 __ mov(scratch1, Operand(scratch1, LSL, scratch2));
3039 // Check that the signed result fits in a Smi.
3040 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
3041 __ b(mi, &not_smi_result);
3042 __ SmiTag(right, scratch1);
3043 __ Ret();
3044 break;
3045 default:
3046 UNREACHABLE();
3047 }
3048 __ bind(&not_smi_result);
3049}
3050
3051
3052void TypeRecordingBinaryOpStub::GenerateFPOperation(MacroAssembler* masm,
3053 bool smi_operands,
3054 Label* not_numbers,
3055 Label* gc_required) {
3056 Register left = r1;
3057 Register right = r0;
3058 Register scratch1 = r7;
3059 Register scratch2 = r9;
Steve Block44f0eee2011-05-26 01:26:41 +01003060 Register scratch3 = r4;
Steve Block1e0659c2011-05-24 12:43:12 +01003061
3062 ASSERT(smi_operands || (not_numbers != NULL));
3063 if (smi_operands && FLAG_debug_code) {
3064 __ AbortIfNotSmi(left);
3065 __ AbortIfNotSmi(right);
3066 }
3067
3068 Register heap_number_map = r6;
3069 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
3070
3071 switch (op_) {
3072 case Token::ADD:
3073 case Token::SUB:
3074 case Token::MUL:
3075 case Token::DIV:
3076 case Token::MOD: {
3077 // Load left and right operands into d6 and d7 or r0/r1 and r2/r3
3078 // depending on whether VFP3 is available or not.
3079 FloatingPointHelper::Destination destination =
Steve Block44f0eee2011-05-26 01:26:41 +01003080 Isolate::Current()->cpu_features()->IsSupported(VFP3) &&
3081 op_ != Token::MOD ?
Steve Block1e0659c2011-05-24 12:43:12 +01003082 FloatingPointHelper::kVFPRegisters :
3083 FloatingPointHelper::kCoreRegisters;
3084
3085 // Allocate new heap number for result.
3086 Register result = r5;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003087 GenerateHeapResultAllocation(
3088 masm, result, heap_number_map, scratch1, scratch2, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01003089
3090 // Load the operands.
3091 if (smi_operands) {
3092 FloatingPointHelper::LoadSmis(masm, destination, scratch1, scratch2);
3093 } else {
3094 FloatingPointHelper::LoadOperands(masm,
3095 destination,
3096 heap_number_map,
3097 scratch1,
3098 scratch2,
3099 not_numbers);
3100 }
3101
3102 // Calculate the result.
3103 if (destination == FloatingPointHelper::kVFPRegisters) {
3104 // Using VFP registers:
3105 // d6: Left value
3106 // d7: Right value
3107 CpuFeatures::Scope scope(VFP3);
3108 switch (op_) {
3109 case Token::ADD:
3110 __ vadd(d5, d6, d7);
3111 break;
3112 case Token::SUB:
3113 __ vsub(d5, d6, d7);
3114 break;
3115 case Token::MUL:
3116 __ vmul(d5, d6, d7);
3117 break;
3118 case Token::DIV:
3119 __ vdiv(d5, d6, d7);
3120 break;
3121 default:
3122 UNREACHABLE();
3123 }
3124
3125 __ sub(r0, result, Operand(kHeapObjectTag));
3126 __ vstr(d5, r0, HeapNumber::kValueOffset);
3127 __ add(r0, r0, Operand(kHeapObjectTag));
3128 __ Ret();
3129 } else {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003130 // Call the C function to handle the double operation.
3131 FloatingPointHelper::CallCCodeForDoubleOperation(masm,
3132 op_,
3133 result,
3134 scratch1);
Steve Block1e0659c2011-05-24 12:43:12 +01003135 }
3136 break;
3137 }
3138 case Token::BIT_OR:
3139 case Token::BIT_XOR:
3140 case Token::BIT_AND:
3141 case Token::SAR:
3142 case Token::SHR:
3143 case Token::SHL: {
3144 if (smi_operands) {
3145 __ SmiUntag(r3, left);
3146 __ SmiUntag(r2, right);
3147 } else {
3148 // Convert operands to 32-bit integers. Right in r2 and left in r3.
Steve Block44f0eee2011-05-26 01:26:41 +01003149 FloatingPointHelper::ConvertNumberToInt32(masm,
3150 left,
3151 r3,
3152 heap_number_map,
3153 scratch1,
3154 scratch2,
3155 scratch3,
3156 d0,
3157 not_numbers);
3158 FloatingPointHelper::ConvertNumberToInt32(masm,
3159 right,
3160 r2,
3161 heap_number_map,
3162 scratch1,
3163 scratch2,
3164 scratch3,
3165 d0,
3166 not_numbers);
Steve Block1e0659c2011-05-24 12:43:12 +01003167 }
3168
3169 Label result_not_a_smi;
3170 switch (op_) {
3171 case Token::BIT_OR:
3172 __ orr(r2, r3, Operand(r2));
3173 break;
3174 case Token::BIT_XOR:
3175 __ eor(r2, r3, Operand(r2));
3176 break;
3177 case Token::BIT_AND:
3178 __ and_(r2, r3, Operand(r2));
3179 break;
3180 case Token::SAR:
3181 // Use only the 5 least significant bits of the shift count.
Steve Block1e0659c2011-05-24 12:43:12 +01003182 __ GetLeastBitsFromInt32(r2, r2, 5);
3183 __ mov(r2, Operand(r3, ASR, r2));
3184 break;
3185 case Token::SHR:
3186 // Use only the 5 least significant bits of the shift count.
3187 __ GetLeastBitsFromInt32(r2, r2, 5);
3188 __ mov(r2, Operand(r3, LSR, r2), SetCC);
3189 // SHR is special because it is required to produce a positive answer.
3190 // The code below for writing into heap numbers isn't capable of
3191 // writing the register as an unsigned int so we go to slow case if we
3192 // hit this case.
Steve Block44f0eee2011-05-26 01:26:41 +01003193 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01003194 __ b(mi, &result_not_a_smi);
3195 } else {
3196 __ b(mi, not_numbers);
3197 }
3198 break;
3199 case Token::SHL:
3200 // Use only the 5 least significant bits of the shift count.
3201 __ GetLeastBitsFromInt32(r2, r2, 5);
3202 __ mov(r2, Operand(r3, LSL, r2));
3203 break;
3204 default:
3205 UNREACHABLE();
3206 }
3207
3208 // Check that the *signed* result fits in a smi.
3209 __ add(r3, r2, Operand(0x40000000), SetCC);
3210 __ b(mi, &result_not_a_smi);
3211 __ SmiTag(r0, r2);
3212 __ Ret();
3213
3214 // Allocate new heap number for result.
3215 __ bind(&result_not_a_smi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003216 Register result = r5;
3217 if (smi_operands) {
3218 __ AllocateHeapNumber(
3219 result, scratch1, scratch2, heap_number_map, gc_required);
3220 } else {
3221 GenerateHeapResultAllocation(
3222 masm, result, heap_number_map, scratch1, scratch2, gc_required);
3223 }
Steve Block1e0659c2011-05-24 12:43:12 +01003224
3225 // r2: Answer as signed int32.
3226 // r5: Heap number to write answer into.
3227
3228 // Nothing can go wrong now, so move the heap number to r0, which is the
3229 // result.
3230 __ mov(r0, Operand(r5));
3231
Steve Block44f0eee2011-05-26 01:26:41 +01003232 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01003233 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted. As
3234 // mentioned above SHR needs to always produce a positive result.
3235 CpuFeatures::Scope scope(VFP3);
3236 __ vmov(s0, r2);
3237 if (op_ == Token::SHR) {
3238 __ vcvt_f64_u32(d0, s0);
3239 } else {
3240 __ vcvt_f64_s32(d0, s0);
3241 }
3242 __ sub(r3, r0, Operand(kHeapObjectTag));
3243 __ vstr(d0, r3, HeapNumber::kValueOffset);
3244 __ Ret();
3245 } else {
3246 // Tail call that writes the int32 in r2 to the heap number in r0, using
3247 // r3 as scratch. r0 is preserved and returned.
3248 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
3249 __ TailCallStub(&stub);
3250 }
3251 break;
3252 }
3253 default:
3254 UNREACHABLE();
3255 }
3256}
3257
3258
3259// Generate the smi code. If the operation on smis are successful this return is
3260// generated. If the result is not a smi and heap number allocation is not
3261// requested the code falls through. If number allocation is requested but a
3262// heap number cannot be allocated the code jumps to the lable gc_required.
3263void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
3264 Label* gc_required,
3265 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
3266 Label not_smis;
3267
3268 Register left = r1;
3269 Register right = r0;
3270 Register scratch1 = r7;
3271 Register scratch2 = r9;
3272
3273 // Perform combined smi check on both operands.
3274 __ orr(scratch1, left, Operand(right));
3275 STATIC_ASSERT(kSmiTag == 0);
3276 __ tst(scratch1, Operand(kSmiTagMask));
3277 __ b(ne, &not_smis);
3278
3279 // If the smi-smi operation results in a smi return is generated.
3280 GenerateSmiSmiOperation(masm);
3281
3282 // If heap number results are possible generate the result in an allocated
3283 // heap number.
3284 if (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) {
3285 GenerateFPOperation(masm, true, NULL, gc_required);
3286 }
3287 __ bind(&not_smis);
3288}
3289
3290
3291void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
3292 Label not_smis, call_runtime;
3293
3294 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
3295 result_type_ == TRBinaryOpIC::SMI) {
3296 // Only allow smi results.
3297 GenerateSmiCode(masm, NULL, NO_HEAPNUMBER_RESULTS);
3298 } else {
3299 // Allow heap number result and don't make a transition if a heap number
3300 // cannot be allocated.
3301 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
3302 }
3303
3304 // Code falls through if the result is not returned as either a smi or heap
3305 // number.
3306 GenerateTypeTransition(masm);
3307
3308 __ bind(&call_runtime);
3309 GenerateCallRuntime(masm);
3310}
3311
3312
3313void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
3314 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
3315 ASSERT(op_ == Token::ADD);
3316 // Try to add arguments as strings, otherwise, transition to the generic
3317 // TRBinaryOpIC type.
3318 GenerateAddStrings(masm);
3319 GenerateTypeTransition(masm);
3320}
3321
3322
3323void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
3324 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
3325
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003326 Register left = r1;
3327 Register right = r0;
3328 Register scratch1 = r7;
3329 Register scratch2 = r9;
3330 DwVfpRegister double_scratch = d0;
3331 SwVfpRegister single_scratch = s3;
3332
3333 Register heap_number_result = no_reg;
3334 Register heap_number_map = r6;
3335 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
3336
3337 Label call_runtime;
3338 // Labels for type transition, used for wrong input or output types.
3339 // Both label are currently actually bound to the same position. We use two
3340 // different label to differentiate the cause leading to type transition.
3341 Label transition;
3342
3343 // Smi-smi fast case.
3344 Label skip;
3345 __ orr(scratch1, left, right);
3346 __ JumpIfNotSmi(scratch1, &skip);
3347 GenerateSmiSmiOperation(masm);
3348 // Fall through if the result is not a smi.
3349 __ bind(&skip);
3350
3351 switch (op_) {
3352 case Token::ADD:
3353 case Token::SUB:
3354 case Token::MUL:
3355 case Token::DIV:
3356 case Token::MOD: {
3357 // Load both operands and check that they are 32-bit integer.
3358 // Jump to type transition if they are not. The registers r0 and r1 (right
3359 // and left) are preserved for the runtime call.
3360 FloatingPointHelper::Destination destination =
Steve Block44f0eee2011-05-26 01:26:41 +01003361 Isolate::Current()->cpu_features()->IsSupported(VFP3) &&
3362 op_ != Token::MOD ?
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003363 FloatingPointHelper::kVFPRegisters :
3364 FloatingPointHelper::kCoreRegisters;
3365
3366 FloatingPointHelper::LoadNumberAsInt32Double(masm,
3367 right,
3368 destination,
3369 d7,
3370 r2,
3371 r3,
3372 heap_number_map,
3373 scratch1,
3374 scratch2,
3375 s0,
3376 &transition);
3377 FloatingPointHelper::LoadNumberAsInt32Double(masm,
3378 left,
3379 destination,
3380 d6,
3381 r4,
3382 r5,
3383 heap_number_map,
3384 scratch1,
3385 scratch2,
3386 s0,
3387 &transition);
3388
3389 if (destination == FloatingPointHelper::kVFPRegisters) {
3390 CpuFeatures::Scope scope(VFP3);
3391 Label return_heap_number;
3392 switch (op_) {
3393 case Token::ADD:
3394 __ vadd(d5, d6, d7);
3395 break;
3396 case Token::SUB:
3397 __ vsub(d5, d6, d7);
3398 break;
3399 case Token::MUL:
3400 __ vmul(d5, d6, d7);
3401 break;
3402 case Token::DIV:
3403 __ vdiv(d5, d6, d7);
3404 break;
3405 default:
3406 UNREACHABLE();
3407 }
3408
3409 if (op_ != Token::DIV) {
3410 // These operations produce an integer result.
3411 // Try to return a smi if we can.
3412 // Otherwise return a heap number if allowed, or jump to type
3413 // transition.
3414
3415 __ EmitVFPTruncate(kRoundToZero,
3416 single_scratch,
3417 d5,
3418 scratch1,
3419 scratch2);
3420
3421 if (result_type_ <= TRBinaryOpIC::INT32) {
3422 // If the ne condition is set, result does
3423 // not fit in a 32-bit integer.
3424 __ b(ne, &transition);
3425 }
3426
3427 // Check if the result fits in a smi.
3428 __ vmov(scratch1, single_scratch);
3429 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
3430 // If not try to return a heap number.
3431 __ b(mi, &return_heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +01003432 // Check for minus zero. Return heap number for minus zero.
3433 Label not_zero;
3434 __ cmp(scratch1, Operand(0));
3435 __ b(ne, &not_zero);
3436 __ vmov(scratch2, d5.high());
3437 __ tst(scratch2, Operand(HeapNumber::kSignMask));
3438 __ b(ne, &return_heap_number);
3439 __ bind(&not_zero);
3440
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003441 // Tag the result and return.
3442 __ SmiTag(r0, scratch1);
3443 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +01003444 } else {
3445 // DIV just falls through to allocating a heap number.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003446 }
3447
3448 if (result_type_ >= (op_ == Token::DIV) ? TRBinaryOpIC::HEAP_NUMBER
3449 : TRBinaryOpIC::INT32) {
3450 __ bind(&return_heap_number);
3451 // We are using vfp registers so r5 is available.
3452 heap_number_result = r5;
3453 GenerateHeapResultAllocation(masm,
3454 heap_number_result,
3455 heap_number_map,
3456 scratch1,
3457 scratch2,
3458 &call_runtime);
3459 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
3460 __ vstr(d5, r0, HeapNumber::kValueOffset);
3461 __ mov(r0, heap_number_result);
3462 __ Ret();
3463 }
3464
3465 // A DIV operation expecting an integer result falls through
3466 // to type transition.
3467
3468 } else {
3469 // We preserved r0 and r1 to be able to call runtime.
3470 // Save the left value on the stack.
3471 __ Push(r5, r4);
3472
3473 // Allocate a heap number to store the result.
3474 heap_number_result = r5;
3475 GenerateHeapResultAllocation(masm,
3476 heap_number_result,
3477 heap_number_map,
3478 scratch1,
3479 scratch2,
3480 &call_runtime);
3481
3482 // Load the left value from the value saved on the stack.
3483 __ Pop(r1, r0);
3484
3485 // Call the C function to handle the double operation.
3486 FloatingPointHelper::CallCCodeForDoubleOperation(
3487 masm, op_, heap_number_result, scratch1);
3488 }
3489
3490 break;
3491 }
3492
3493 case Token::BIT_OR:
3494 case Token::BIT_XOR:
3495 case Token::BIT_AND:
3496 case Token::SAR:
3497 case Token::SHR:
3498 case Token::SHL: {
3499 Label return_heap_number;
3500 Register scratch3 = r5;
3501 // Convert operands to 32-bit integers. Right in r2 and left in r3. The
3502 // registers r0 and r1 (right and left) are preserved for the runtime
3503 // call.
3504 FloatingPointHelper::LoadNumberAsInt32(masm,
3505 left,
3506 r3,
3507 heap_number_map,
3508 scratch1,
3509 scratch2,
3510 scratch3,
3511 d0,
3512 &transition);
3513 FloatingPointHelper::LoadNumberAsInt32(masm,
3514 right,
3515 r2,
3516 heap_number_map,
3517 scratch1,
3518 scratch2,
3519 scratch3,
3520 d0,
3521 &transition);
3522
3523 // The ECMA-262 standard specifies that, for shift operations, only the
3524 // 5 least significant bits of the shift value should be used.
3525 switch (op_) {
3526 case Token::BIT_OR:
3527 __ orr(r2, r3, Operand(r2));
3528 break;
3529 case Token::BIT_XOR:
3530 __ eor(r2, r3, Operand(r2));
3531 break;
3532 case Token::BIT_AND:
3533 __ and_(r2, r3, Operand(r2));
3534 break;
3535 case Token::SAR:
3536 __ and_(r2, r2, Operand(0x1f));
3537 __ mov(r2, Operand(r3, ASR, r2));
3538 break;
3539 case Token::SHR:
3540 __ and_(r2, r2, Operand(0x1f));
3541 __ mov(r2, Operand(r3, LSR, r2), SetCC);
3542 // SHR is special because it is required to produce a positive answer.
3543 // We only get a negative result if the shift value (r2) is 0.
3544 // This result cannot be respresented as a signed 32-bit integer, try
3545 // to return a heap number if we can.
3546 // The non vfp3 code does not support this special case, so jump to
3547 // runtime if we don't support it.
Steve Block44f0eee2011-05-26 01:26:41 +01003548 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003549 __ b(mi,
3550 (result_type_ <= TRBinaryOpIC::INT32) ? &transition
3551 : &return_heap_number);
3552 } else {
3553 __ b(mi, (result_type_ <= TRBinaryOpIC::INT32) ? &transition
3554 : &call_runtime);
3555 }
3556 break;
3557 case Token::SHL:
3558 __ and_(r2, r2, Operand(0x1f));
3559 __ mov(r2, Operand(r3, LSL, r2));
3560 break;
3561 default:
3562 UNREACHABLE();
3563 }
3564
3565 // Check if the result fits in a smi.
3566 __ add(scratch1, r2, Operand(0x40000000), SetCC);
3567 // If not try to return a heap number. (We know the result is an int32.)
3568 __ b(mi, &return_heap_number);
3569 // Tag the result and return.
3570 __ SmiTag(r0, r2);
3571 __ Ret();
3572
3573 __ bind(&return_heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +01003574 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003575 CpuFeatures::Scope scope(VFP3);
3576 heap_number_result = r5;
3577 GenerateHeapResultAllocation(masm,
3578 heap_number_result,
3579 heap_number_map,
3580 scratch1,
3581 scratch2,
3582 &call_runtime);
3583
3584 if (op_ != Token::SHR) {
3585 // Convert the result to a floating point value.
3586 __ vmov(double_scratch.low(), r2);
3587 __ vcvt_f64_s32(double_scratch, double_scratch.low());
3588 } else {
3589 // The result must be interpreted as an unsigned 32-bit integer.
3590 __ vmov(double_scratch.low(), r2);
3591 __ vcvt_f64_u32(double_scratch, double_scratch.low());
3592 }
3593
3594 // Store the result.
3595 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
3596 __ vstr(double_scratch, r0, HeapNumber::kValueOffset);
3597 __ mov(r0, heap_number_result);
3598 __ Ret();
3599 } else {
3600 // Tail call that writes the int32 in r2 to the heap number in r0, using
3601 // r3 as scratch. r0 is preserved and returned.
3602 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
3603 __ TailCallStub(&stub);
3604 }
3605
3606 break;
3607 }
3608
3609 default:
3610 UNREACHABLE();
3611 }
3612
3613 if (transition.is_linked()) {
3614 __ bind(&transition);
3615 GenerateTypeTransition(masm);
3616 }
3617
3618 __ bind(&call_runtime);
3619 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01003620}
3621
3622
Steve Block44f0eee2011-05-26 01:26:41 +01003623void TypeRecordingBinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
3624 Label call_runtime;
3625
3626 if (op_ == Token::ADD) {
3627 // Handle string addition here, because it is the only operation
3628 // that does not do a ToNumber conversion on the operands.
3629 GenerateAddStrings(masm);
3630 }
3631
3632 // Convert oddball arguments to numbers.
3633 Label check, done;
3634 __ CompareRoot(r1, Heap::kUndefinedValueRootIndex);
3635 __ b(ne, &check);
3636 if (Token::IsBitOp(op_)) {
3637 __ mov(r1, Operand(Smi::FromInt(0)));
3638 } else {
3639 __ LoadRoot(r1, Heap::kNanValueRootIndex);
3640 }
3641 __ jmp(&done);
3642 __ bind(&check);
3643 __ CompareRoot(r0, Heap::kUndefinedValueRootIndex);
3644 __ b(ne, &done);
3645 if (Token::IsBitOp(op_)) {
3646 __ mov(r0, Operand(Smi::FromInt(0)));
3647 } else {
3648 __ LoadRoot(r0, Heap::kNanValueRootIndex);
3649 }
3650 __ bind(&done);
3651
3652 GenerateHeapNumberStub(masm);
3653}
3654
3655
Steve Block1e0659c2011-05-24 12:43:12 +01003656void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01003657 Label call_runtime;
3658 GenerateFPOperation(masm, false, &call_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01003659
3660 __ bind(&call_runtime);
3661 GenerateCallRuntime(masm);
3662}
3663
3664
3665void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003666 Label call_runtime, call_string_add_or_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01003667
3668 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
3669
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003670 GenerateFPOperation(masm, false, &call_string_add_or_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01003671
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003672 __ bind(&call_string_add_or_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01003673 if (op_ == Token::ADD) {
3674 GenerateAddStrings(masm);
3675 }
3676
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003677 __ bind(&call_runtime);
3678 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01003679}
3680
3681
3682void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
3683 ASSERT(op_ == Token::ADD);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003684 Label left_not_string, call_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01003685
3686 Register left = r1;
3687 Register right = r0;
Steve Block1e0659c2011-05-24 12:43:12 +01003688
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003689 // Check if left argument is a string.
3690 __ JumpIfSmi(left, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01003691 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003692 __ b(ge, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01003693
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003694 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
3695 GenerateRegisterArgsPush(masm);
3696 __ TailCallStub(&string_add_left_stub);
3697
3698 // Left operand is not a string, test right.
3699 __ bind(&left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01003700 __ JumpIfSmi(right, &call_runtime);
3701 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
3702 __ b(ge, &call_runtime);
3703
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003704 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
Steve Block1e0659c2011-05-24 12:43:12 +01003705 GenerateRegisterArgsPush(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003706 __ TailCallStub(&string_add_right_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01003707
3708 // At least one argument is not a string.
3709 __ bind(&call_runtime);
3710}
3711
3712
3713void TypeRecordingBinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) {
3714 GenerateRegisterArgsPush(masm);
3715 switch (op_) {
3716 case Token::ADD:
3717 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
3718 break;
3719 case Token::SUB:
3720 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
3721 break;
3722 case Token::MUL:
3723 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
3724 break;
3725 case Token::DIV:
3726 __ InvokeBuiltin(Builtins::DIV, JUMP_JS);
3727 break;
3728 case Token::MOD:
3729 __ InvokeBuiltin(Builtins::MOD, JUMP_JS);
3730 break;
3731 case Token::BIT_OR:
3732 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
3733 break;
3734 case Token::BIT_AND:
3735 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
3736 break;
3737 case Token::BIT_XOR:
3738 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
3739 break;
3740 case Token::SAR:
3741 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
3742 break;
3743 case Token::SHR:
3744 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
3745 break;
3746 case Token::SHL:
3747 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
3748 break;
3749 default:
3750 UNREACHABLE();
3751 }
3752}
3753
3754
3755void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
3756 MacroAssembler* masm,
3757 Register result,
3758 Register heap_number_map,
3759 Register scratch1,
3760 Register scratch2,
3761 Label* gc_required) {
3762
3763 // Code below will scratch result if allocation fails. To keep both arguments
3764 // intact for the runtime call result cannot be one of these.
3765 ASSERT(!result.is(r0) && !result.is(r1));
3766
3767 if (mode_ == OVERWRITE_LEFT || mode_ == OVERWRITE_RIGHT) {
3768 Label skip_allocation, allocated;
3769 Register overwritable_operand = mode_ == OVERWRITE_LEFT ? r1 : r0;
3770 // If the overwritable operand is already an object, we skip the
3771 // allocation of a heap number.
3772 __ JumpIfNotSmi(overwritable_operand, &skip_allocation);
3773 // Allocate a heap number for the result.
3774 __ AllocateHeapNumber(
3775 result, scratch1, scratch2, heap_number_map, gc_required);
3776 __ b(&allocated);
3777 __ bind(&skip_allocation);
3778 // Use object holding the overwritable operand for result.
3779 __ mov(result, Operand(overwritable_operand));
3780 __ bind(&allocated);
3781 } else {
3782 ASSERT(mode_ == NO_OVERWRITE);
3783 __ AllocateHeapNumber(
3784 result, scratch1, scratch2, heap_number_map, gc_required);
3785 }
3786}
3787
3788
3789void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
3790 __ Push(r1, r0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003791}
3792
3793
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003794void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003795 // Untagged case: double input in d2, double result goes
3796 // into d2.
3797 // Tagged case: tagged input on top of stack and in r0,
3798 // tagged result (heap number) goes into r0.
3799
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003800 Label input_not_smi;
3801 Label loaded;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003802 Label calculate;
3803 Label invalid_cache;
3804 const Register scratch0 = r9;
3805 const Register scratch1 = r7;
3806 const Register cache_entry = r0;
3807 const bool tagged = (argument_type_ == TAGGED);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003808
Steve Block44f0eee2011-05-26 01:26:41 +01003809 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003810 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003811 if (tagged) {
3812 // Argument is a number and is on stack and in r0.
3813 // Load argument and check if it is a smi.
3814 __ JumpIfNotSmi(r0, &input_not_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003815
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003816 // Input is a smi. Convert to double and load the low and high words
3817 // of the double into r2, r3.
3818 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
3819 __ b(&loaded);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003820
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003821 __ bind(&input_not_smi);
3822 // Check if input is a HeapNumber.
3823 __ CheckMap(r0,
3824 r1,
3825 Heap::kHeapNumberMapRootIndex,
3826 &calculate,
3827 true);
3828 // Input is a HeapNumber. Load it to a double register and store the
3829 // low and high words into r2, r3.
3830 __ vldr(d0, FieldMemOperand(r0, HeapNumber::kValueOffset));
3831 __ vmov(r2, r3, d0);
3832 } else {
3833 // Input is untagged double in d2. Output goes to d2.
3834 __ vmov(r2, r3, d2);
3835 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003836 __ bind(&loaded);
3837 // r2 = low 32 bits of double value
3838 // r3 = high 32 bits of double value
3839 // Compute hash (the shifts are arithmetic):
3840 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
3841 __ eor(r1, r2, Operand(r3));
3842 __ eor(r1, r1, Operand(r1, ASR, 16));
3843 __ eor(r1, r1, Operand(r1, ASR, 8));
Steve Block44f0eee2011-05-26 01:26:41 +01003844 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
3845 __ And(r1, r1, Operand(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003846
3847 // r2 = low 32 bits of double value.
3848 // r3 = high 32 bits of double value.
3849 // r1 = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +01003850 Isolate* isolate = masm->isolate();
3851 ExternalReference cache_array =
3852 ExternalReference::transcendental_cache_array_address(isolate);
3853 __ mov(cache_entry, Operand(cache_array));
3854 // cache_entry points to cache array.
3855 int cache_array_index
3856 = type_ * sizeof(isolate->transcendental_cache()->caches_[0]);
3857 __ ldr(cache_entry, MemOperand(cache_entry, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003858 // r0 points to the cache for the type type_.
3859 // If NULL, the cache hasn't been initialized yet, so go through runtime.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003860 __ cmp(cache_entry, Operand(0, RelocInfo::NONE));
3861 __ b(eq, &invalid_cache);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003862
3863#ifdef DEBUG
3864 // Check that the layout of cache elements match expectations.
Steve Block44f0eee2011-05-26 01:26:41 +01003865 { TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003866 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
3867 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
3868 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
3869 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
3870 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
3871 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
3872 CHECK_EQ(0, elem_in0 - elem_start);
3873 CHECK_EQ(kIntSize, elem_in1 - elem_start);
3874 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
3875 }
3876#endif
3877
3878 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
3879 __ add(r1, r1, Operand(r1, LSL, 1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003880 __ add(cache_entry, cache_entry, Operand(r1, LSL, 2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003881 // Check if cache matches: Double value is stored in uint32_t[2] array.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003882 __ ldm(ia, cache_entry, r4.bit() | r5.bit() | r6.bit());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003883 __ cmp(r2, r4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003884 __ b(ne, &calculate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003885 __ cmp(r3, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003886 __ b(ne, &calculate);
3887 // Cache hit. Load result, cleanup and return.
3888 if (tagged) {
3889 // Pop input value from stack and load result into r0.
3890 __ pop();
3891 __ mov(r0, Operand(r6));
3892 } else {
3893 // Load result into d2.
3894 __ vldr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
3895 }
3896 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +01003897 } // if (Isolate::Current()->cpu_features()->IsSupported(VFP3))
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003898
3899 __ bind(&calculate);
3900 if (tagged) {
3901 __ bind(&invalid_cache);
Steve Block44f0eee2011-05-26 01:26:41 +01003902 ExternalReference runtime_function =
3903 ExternalReference(RuntimeFunction(), masm->isolate());
3904 __ TailCallExternalReference(runtime_function, 1, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003905 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003906 if (!Isolate::Current()->cpu_features()->IsSupported(VFP3)) UNREACHABLE();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003907 CpuFeatures::Scope scope(VFP3);
3908
3909 Label no_update;
3910 Label skip_cache;
3911 const Register heap_number_map = r5;
3912
3913 // Call C function to calculate the result and update the cache.
3914 // Register r0 holds precalculated cache entry address; preserve
3915 // it on the stack and pop it into register cache_entry after the
3916 // call.
3917 __ push(cache_entry);
3918 GenerateCallCFunction(masm, scratch0);
3919 __ GetCFunctionDoubleResult(d2);
3920
3921 // Try to update the cache. If we cannot allocate a
3922 // heap number, we return the result without updating.
3923 __ pop(cache_entry);
3924 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
3925 __ AllocateHeapNumber(r6, scratch0, scratch1, r5, &no_update);
3926 __ vstr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
3927 __ stm(ia, cache_entry, r2.bit() | r3.bit() | r6.bit());
3928 __ Ret();
3929
3930 __ bind(&invalid_cache);
3931 // The cache is invalid. Call runtime which will recreate the
3932 // cache.
3933 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
3934 __ AllocateHeapNumber(r0, scratch0, scratch1, r5, &skip_cache);
3935 __ vstr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
3936 __ EnterInternalFrame();
3937 __ push(r0);
3938 __ CallRuntime(RuntimeFunction(), 1);
3939 __ LeaveInternalFrame();
3940 __ vldr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
3941 __ Ret();
3942
3943 __ bind(&skip_cache);
3944 // Call C function to calculate the result and answer directly
3945 // without updating the cache.
3946 GenerateCallCFunction(masm, scratch0);
3947 __ GetCFunctionDoubleResult(d2);
3948 __ bind(&no_update);
3949
3950 // We return the value in d2 without adding it to the cache, but
3951 // we cause a scavenging GC so that future allocations will succeed.
3952 __ EnterInternalFrame();
3953
3954 // Allocate an aligned object larger than a HeapNumber.
3955 ASSERT(4 * kPointerSize >= HeapNumber::kSize);
3956 __ mov(scratch0, Operand(4 * kPointerSize));
3957 __ push(scratch0);
3958 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
3959 __ LeaveInternalFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003960 __ Ret();
3961 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003962}
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003963
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003964
3965void TranscendentalCacheStub::GenerateCallCFunction(MacroAssembler* masm,
3966 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01003967 Isolate* isolate = masm->isolate();
3968
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003969 __ push(lr);
3970 __ PrepareCallCFunction(2, scratch);
3971 __ vmov(r0, r1, d2);
3972 switch (type_) {
3973 case TranscendentalCache::SIN:
Steve Block44f0eee2011-05-26 01:26:41 +01003974 __ CallCFunction(ExternalReference::math_sin_double_function(isolate), 2);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003975 break;
3976 case TranscendentalCache::COS:
Steve Block44f0eee2011-05-26 01:26:41 +01003977 __ CallCFunction(ExternalReference::math_cos_double_function(isolate), 2);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003978 break;
3979 case TranscendentalCache::LOG:
Steve Block44f0eee2011-05-26 01:26:41 +01003980 __ CallCFunction(ExternalReference::math_log_double_function(isolate), 2);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003981 break;
3982 default:
3983 UNIMPLEMENTED();
3984 break;
3985 }
3986 __ pop(lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003987}
3988
3989
3990Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
3991 switch (type_) {
3992 // Add more cases when necessary.
3993 case TranscendentalCache::SIN: return Runtime::kMath_sin;
3994 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003995 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003996 default:
3997 UNIMPLEMENTED();
3998 return Runtime::kAbort;
3999 }
4000}
4001
4002
4003void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01004004 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004005}
4006
4007
4008void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
4009 Label slow, done;
4010
4011 Register heap_number_map = r6;
4012 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4013
4014 if (op_ == Token::SUB) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004015 if (include_smi_code_) {
4016 // Check whether the value is a smi.
4017 Label try_float;
4018 __ tst(r0, Operand(kSmiTagMask));
4019 __ b(ne, &try_float);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004020
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004021 // Go slow case if the value of the expression is zero
4022 // to make sure that we switch between 0 and -0.
4023 if (negative_zero_ == kStrictNegativeZero) {
4024 // If we have to check for zero, then we can check for the max negative
4025 // smi while we are at it.
4026 __ bic(ip, r0, Operand(0x80000000), SetCC);
4027 __ b(eq, &slow);
4028 __ rsb(r0, r0, Operand(0, RelocInfo::NONE));
4029 __ Ret();
4030 } else {
4031 // The value of the expression is a smi and 0 is OK for -0. Try
4032 // optimistic subtraction '0 - value'.
4033 __ rsb(r0, r0, Operand(0, RelocInfo::NONE), SetCC);
4034 __ Ret(vc);
4035 // We don't have to reverse the optimistic neg since the only case
4036 // where we fall through is the minimum negative Smi, which is the case
4037 // where the neg leaves the register unchanged.
4038 __ jmp(&slow); // Go slow on max negative Smi.
4039 }
4040 __ bind(&try_float);
4041 } else if (FLAG_debug_code) {
4042 __ tst(r0, Operand(kSmiTagMask));
4043 __ Assert(ne, "Unexpected smi operand.");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004044 }
4045
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004046 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
4047 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4048 __ cmp(r1, heap_number_map);
4049 __ b(ne, &slow);
4050 // r0 is a heap number. Get a new heap number in r1.
4051 if (overwrite_ == UNARY_OVERWRITE) {
4052 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4053 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
4054 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4055 } else {
4056 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
4057 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
4058 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
4059 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
4060 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
4061 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
4062 __ mov(r0, Operand(r1));
4063 }
4064 } else if (op_ == Token::BIT_NOT) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004065 if (include_smi_code_) {
4066 Label non_smi;
Steve Block1e0659c2011-05-24 12:43:12 +01004067 __ JumpIfNotSmi(r0, &non_smi);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004068 __ mvn(r0, Operand(r0));
4069 // Bit-clear inverted smi-tag.
4070 __ bic(r0, r0, Operand(kSmiTagMask));
4071 __ Ret();
4072 __ bind(&non_smi);
4073 } else if (FLAG_debug_code) {
4074 __ tst(r0, Operand(kSmiTagMask));
4075 __ Assert(ne, "Unexpected smi operand.");
4076 }
4077
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004078 // Check if the operand is a heap number.
4079 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
4080 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
4081 __ cmp(r1, heap_number_map);
4082 __ b(ne, &slow);
4083
4084 // Convert the heap number is r0 to an untagged integer in r1.
Steve Block1e0659c2011-05-24 12:43:12 +01004085 __ ConvertToInt32(r0, r1, r2, r3, d0, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004086
4087 // Do the bitwise operation (move negated) and check if the result
4088 // fits in a smi.
4089 Label try_float;
4090 __ mvn(r1, Operand(r1));
4091 __ add(r2, r1, Operand(0x40000000), SetCC);
4092 __ b(mi, &try_float);
4093 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
4094 __ b(&done);
4095
4096 __ bind(&try_float);
4097 if (!overwrite_ == UNARY_OVERWRITE) {
4098 // Allocate a fresh heap number, but don't overwrite r0 until
4099 // we're sure we can do it without going through the slow case
4100 // that needs the value in r0.
4101 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
4102 __ mov(r0, Operand(r2));
4103 }
4104
Steve Block44f0eee2011-05-26 01:26:41 +01004105 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004106 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
4107 CpuFeatures::Scope scope(VFP3);
4108 __ vmov(s0, r1);
4109 __ vcvt_f64_s32(d0, s0);
4110 __ sub(r2, r0, Operand(kHeapObjectTag));
4111 __ vstr(d0, r2, HeapNumber::kValueOffset);
4112 } else {
4113 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
4114 // have to set up a frame.
4115 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
4116 __ push(lr);
4117 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
4118 __ pop(lr);
4119 }
4120 } else {
4121 UNIMPLEMENTED();
4122 }
4123
4124 __ bind(&done);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004125 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004126
4127 // Handle the slow case by jumping to the JavaScript builtin.
4128 __ bind(&slow);
4129 __ push(r0);
4130 switch (op_) {
4131 case Token::SUB:
4132 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
4133 break;
4134 case Token::BIT_NOT:
4135 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
4136 break;
4137 default:
4138 UNREACHABLE();
4139 }
4140}
4141
4142
Steve Block44f0eee2011-05-26 01:26:41 +01004143void MathPowStub::Generate(MacroAssembler* masm) {
4144 Label call_runtime;
4145
4146 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
4147 CpuFeatures::Scope scope(VFP3);
4148
4149 Label base_not_smi;
4150 Label exponent_not_smi;
4151 Label convert_exponent;
4152
4153 const Register base = r0;
4154 const Register exponent = r1;
4155 const Register heapnumbermap = r5;
4156 const Register heapnumber = r6;
4157 const DoubleRegister double_base = d0;
4158 const DoubleRegister double_exponent = d1;
4159 const DoubleRegister double_result = d2;
4160 const SwVfpRegister single_scratch = s0;
4161 const Register scratch = r9;
4162 const Register scratch2 = r7;
4163
4164 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
4165 __ ldr(base, MemOperand(sp, 1 * kPointerSize));
4166 __ ldr(exponent, MemOperand(sp, 0 * kPointerSize));
4167
4168 // Convert base to double value and store it in d0.
4169 __ JumpIfNotSmi(base, &base_not_smi);
4170 // Base is a Smi. Untag and convert it.
4171 __ SmiUntag(base);
4172 __ vmov(single_scratch, base);
4173 __ vcvt_f64_s32(double_base, single_scratch);
4174 __ b(&convert_exponent);
4175
4176 __ bind(&base_not_smi);
4177 __ ldr(scratch, FieldMemOperand(base, JSObject::kMapOffset));
4178 __ cmp(scratch, heapnumbermap);
4179 __ b(ne, &call_runtime);
4180 // Base is a heapnumber. Load it into double register.
4181 __ vldr(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
4182
4183 __ bind(&convert_exponent);
4184 __ JumpIfNotSmi(exponent, &exponent_not_smi);
4185 __ SmiUntag(exponent);
4186
4187 // The base is in a double register and the exponent is
4188 // an untagged smi. Allocate a heap number and call a
4189 // C function for integer exponents. The register containing
4190 // the heap number is callee-saved.
4191 __ AllocateHeapNumber(heapnumber,
4192 scratch,
4193 scratch2,
4194 heapnumbermap,
4195 &call_runtime);
4196 __ push(lr);
4197 __ PrepareCallCFunction(3, scratch);
4198 __ mov(r2, exponent);
4199 __ vmov(r0, r1, double_base);
4200 __ CallCFunction(
4201 ExternalReference::power_double_int_function(masm->isolate()), 3);
4202 __ pop(lr);
4203 __ GetCFunctionDoubleResult(double_result);
4204 __ vstr(double_result,
4205 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
4206 __ mov(r0, heapnumber);
4207 __ Ret(2 * kPointerSize);
4208
4209 __ bind(&exponent_not_smi);
4210 __ ldr(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
4211 __ cmp(scratch, heapnumbermap);
4212 __ b(ne, &call_runtime);
4213 // Exponent is a heapnumber. Load it into double register.
4214 __ vldr(double_exponent,
4215 FieldMemOperand(exponent, HeapNumber::kValueOffset));
4216
4217 // The base and the exponent are in double registers.
4218 // Allocate a heap number and call a C function for
4219 // double exponents. The register containing
4220 // the heap number is callee-saved.
4221 __ AllocateHeapNumber(heapnumber,
4222 scratch,
4223 scratch2,
4224 heapnumbermap,
4225 &call_runtime);
4226 __ push(lr);
4227 __ PrepareCallCFunction(4, scratch);
4228 __ vmov(r0, r1, double_base);
4229 __ vmov(r2, r3, double_exponent);
4230 __ CallCFunction(
4231 ExternalReference::power_double_double_function(masm->isolate()), 4);
4232 __ pop(lr);
4233 __ GetCFunctionDoubleResult(double_result);
4234 __ vstr(double_result,
4235 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
4236 __ mov(r0, heapnumber);
4237 __ Ret(2 * kPointerSize);
4238 }
4239
4240 __ bind(&call_runtime);
4241 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
4242}
4243
4244
4245bool CEntryStub::NeedsImmovableCode() {
4246 return true;
4247}
4248
4249
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004250void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004251 __ Throw(r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004252}
4253
4254
4255void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4256 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004257 __ ThrowUncatchable(type, r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004258}
4259
4260
4261void CEntryStub::GenerateCore(MacroAssembler* masm,
4262 Label* throw_normal_exception,
4263 Label* throw_termination_exception,
4264 Label* throw_out_of_memory_exception,
4265 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01004266 bool always_allocate) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004267 // r0: result parameter for PerformGC, if any
4268 // r4: number of arguments including receiver (C callee-saved)
4269 // r5: pointer to builtin function (C callee-saved)
4270 // r6: pointer to the first argument (C callee-saved)
Steve Block44f0eee2011-05-26 01:26:41 +01004271 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004272
4273 if (do_gc) {
4274 // Passing r0.
4275 __ PrepareCallCFunction(1, r1);
Steve Block44f0eee2011-05-26 01:26:41 +01004276 __ CallCFunction(ExternalReference::perform_gc_function(isolate), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004277 }
4278
4279 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01004280 ExternalReference::heap_always_allocate_scope_depth(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004281 if (always_allocate) {
4282 __ mov(r0, Operand(scope_depth));
4283 __ ldr(r1, MemOperand(r0));
4284 __ add(r1, r1, Operand(1));
4285 __ str(r1, MemOperand(r0));
4286 }
4287
4288 // Call C built-in.
4289 // r0 = argc, r1 = argv
4290 __ mov(r0, Operand(r4));
4291 __ mov(r1, Operand(r6));
4292
Steve Block1e0659c2011-05-24 12:43:12 +01004293#if defined(V8_HOST_ARCH_ARM)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004294 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
4295 int frame_alignment_mask = frame_alignment - 1;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004296 if (FLAG_debug_code) {
4297 if (frame_alignment > kPointerSize) {
4298 Label alignment_as_expected;
4299 ASSERT(IsPowerOf2(frame_alignment));
Steve Block1e0659c2011-05-24 12:43:12 +01004300 __ tst(sp, Operand(frame_alignment_mask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004301 __ b(eq, &alignment_as_expected);
4302 // Don't use Check here, as it will call Runtime_Abort re-entering here.
4303 __ stop("Unexpected alignment");
4304 __ bind(&alignment_as_expected);
4305 }
4306 }
4307#endif
4308
Steve Block44f0eee2011-05-26 01:26:41 +01004309 __ mov(r2, Operand(ExternalReference::isolate_address()));
4310
4311
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004312 // TODO(1242173): To let the GC traverse the return address of the exit
4313 // frames, we need to know where the return address is. Right now,
Steve Block1e0659c2011-05-24 12:43:12 +01004314 // we store it on the stack to be able to find it again, but we never
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004315 // restore from it in case of changes, which makes it impossible to
4316 // support moving the C entry code stub. This should be fixed, but currently
4317 // this is OK because the CEntryStub gets generated so early in the V8 boot
4318 // sequence that it is not moving ever.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004319
Steve Block1e0659c2011-05-24 12:43:12 +01004320 // Compute the return address in lr to return to after the jump below. Pc is
4321 // already at '+ 8' from the current instruction but return is after three
4322 // instructions so add another 4 to pc to get the return address.
4323 masm->add(lr, pc, Operand(4));
4324 __ str(lr, MemOperand(sp, 0));
4325 masm->Jump(r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004326
4327 if (always_allocate) {
4328 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
4329 // though (contain the result).
4330 __ mov(r2, Operand(scope_depth));
4331 __ ldr(r3, MemOperand(r2));
4332 __ sub(r3, r3, Operand(1));
4333 __ str(r3, MemOperand(r2));
4334 }
4335
4336 // check for failure result
4337 Label failure_returned;
4338 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4339 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
4340 __ add(r2, r0, Operand(1));
4341 __ tst(r2, Operand(kFailureTagMask));
4342 __ b(eq, &failure_returned);
4343
4344 // Exit C frame and return.
4345 // r0:r1: result
4346 // sp: stack pointer
4347 // fp: frame pointer
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004348 // Callee-saved register r4 still holds argc.
4349 __ LeaveExitFrame(save_doubles_, r4);
4350 __ mov(pc, lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004351
4352 // check if we should retry or throw exception
4353 Label retry;
4354 __ bind(&failure_returned);
4355 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4356 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
4357 __ b(eq, &retry);
4358
4359 // Special handling of out of memory exceptions.
4360 Failure* out_of_memory = Failure::OutOfMemoryException();
4361 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
4362 __ b(eq, throw_out_of_memory_exception);
4363
4364 // Retrieve the pending exception and clear the variable.
Steve Block44f0eee2011-05-26 01:26:41 +01004365 __ mov(ip, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004366 __ ldr(r3, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +01004367 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
4368 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004369 __ ldr(r0, MemOperand(ip));
4370 __ str(r3, MemOperand(ip));
4371
4372 // Special handling of termination exceptions which are uncatchable
4373 // by javascript code.
Steve Block44f0eee2011-05-26 01:26:41 +01004374 __ cmp(r0, Operand(isolate->factory()->termination_exception()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004375 __ b(eq, throw_termination_exception);
4376
4377 // Handle normal exception.
4378 __ jmp(throw_normal_exception);
4379
4380 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
4381}
4382
4383
4384void CEntryStub::Generate(MacroAssembler* masm) {
4385 // Called from JavaScript; parameters are on stack as if calling JS function
4386 // r0: number of arguments including receiver
4387 // r1: pointer to builtin function
4388 // fp: frame pointer (restored after C call)
4389 // sp: stack pointer (restored as callee's sp after C call)
4390 // cp: current context (C callee-saved)
4391
4392 // Result returned in r0 or r0+r1 by default.
4393
4394 // NOTE: Invocations of builtins may return failure objects
4395 // instead of a proper result. The builtin entry handles
4396 // this by performing a garbage collection and retrying the
4397 // builtin once.
4398
Steve Block1e0659c2011-05-24 12:43:12 +01004399 // Compute the argv pointer in a callee-saved register.
4400 __ add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
4401 __ sub(r6, r6, Operand(kPointerSize));
4402
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004403 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004404 __ EnterExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004405
Steve Block1e0659c2011-05-24 12:43:12 +01004406 // Setup argc and the builtin function in callee-saved registers.
4407 __ mov(r4, Operand(r0));
4408 __ mov(r5, Operand(r1));
4409
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004410 // r4: number of arguments (C callee-saved)
4411 // r5: pointer to builtin function (C callee-saved)
4412 // r6: pointer to first argument (C callee-saved)
4413
4414 Label throw_normal_exception;
4415 Label throw_termination_exception;
4416 Label throw_out_of_memory_exception;
4417
4418 // Call into the runtime system.
4419 GenerateCore(masm,
4420 &throw_normal_exception,
4421 &throw_termination_exception,
4422 &throw_out_of_memory_exception,
4423 false,
Steve Block1e0659c2011-05-24 12:43:12 +01004424 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004425
4426 // Do space-specific GC and retry runtime call.
4427 GenerateCore(masm,
4428 &throw_normal_exception,
4429 &throw_termination_exception,
4430 &throw_out_of_memory_exception,
4431 true,
Steve Block1e0659c2011-05-24 12:43:12 +01004432 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004433
4434 // Do full GC and retry runtime call one final time.
4435 Failure* failure = Failure::InternalError();
4436 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
4437 GenerateCore(masm,
4438 &throw_normal_exception,
4439 &throw_termination_exception,
4440 &throw_out_of_memory_exception,
4441 true,
Steve Block1e0659c2011-05-24 12:43:12 +01004442 true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004443
4444 __ bind(&throw_out_of_memory_exception);
4445 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4446
4447 __ bind(&throw_termination_exception);
4448 GenerateThrowUncatchable(masm, TERMINATION);
4449
4450 __ bind(&throw_normal_exception);
4451 GenerateThrowTOS(masm);
4452}
4453
4454
4455void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
4456 // r0: code entry
4457 // r1: function
4458 // r2: receiver
4459 // r3: argc
4460 // [sp+0]: argv
4461
4462 Label invoke, exit;
4463
4464 // Called from C, so do not pop argc and args on exit (preserve sp)
4465 // No need to save register-passed args
4466 // Save callee-saved registers (incl. cp and fp), sp, and lr
4467 __ stm(db_w, sp, kCalleeSaved | lr.bit());
4468
4469 // Get address of argv, see stm above.
4470 // r0: code entry
4471 // r1: function
4472 // r2: receiver
4473 // r3: argc
4474 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
4475
4476 // Push a frame with special values setup to mark it as an entry frame.
4477 // r0: code entry
4478 // r1: function
4479 // r2: receiver
4480 // r3: argc
4481 // r4: argv
Steve Block44f0eee2011-05-26 01:26:41 +01004482 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004483 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
4484 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4485 __ mov(r7, Operand(Smi::FromInt(marker)));
4486 __ mov(r6, Operand(Smi::FromInt(marker)));
Steve Block44f0eee2011-05-26 01:26:41 +01004487 __ mov(r5,
4488 Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004489 __ ldr(r5, MemOperand(r5));
4490 __ Push(r8, r7, r6, r5);
4491
4492 // Setup frame pointer for the frame to be pushed.
4493 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4494
Ben Murdochb0fe1622011-05-05 13:52:32 +01004495#ifdef ENABLE_LOGGING_AND_PROFILING
4496 // If this is the outermost JS call, set js_entry_sp value.
Steve Block44f0eee2011-05-26 01:26:41 +01004497 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address, isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004498 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
4499 __ ldr(r6, MemOperand(r5));
4500 __ cmp(r6, Operand(0, RelocInfo::NONE));
4501 __ str(fp, MemOperand(r5), eq);
4502#endif
4503
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004504 // Call a faked try-block that does the invoke.
4505 __ bl(&invoke);
4506
4507 // Caught exception: Store result (exception) in the pending
4508 // exception field in the JSEnv and return a failure sentinel.
4509 // Coming in here the fp will be invalid because the PushTryHandler below
4510 // sets it to 0 to signal the existence of the JSEntry frame.
Steve Block44f0eee2011-05-26 01:26:41 +01004511 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
4512 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004513 __ str(r0, MemOperand(ip));
4514 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
4515 __ b(&exit);
4516
4517 // Invoke: Link this frame into the handler chain.
4518 __ bind(&invoke);
4519 // Must preserve r0-r4, r5-r7 are available.
4520 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
4521 // If an exception not caught by another handler occurs, this handler
4522 // returns control to the code after the bl(&invoke) above, which
4523 // restores all kCalleeSaved registers (including cp and fp) to their
4524 // saved values before returning a failure to C.
4525
4526 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01004527 __ mov(ip, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004528 __ ldr(r5, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +01004529 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
4530 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004531 __ str(r5, MemOperand(ip));
4532
4533 // Invoke the function by calling through JS entry trampoline builtin.
4534 // Notice that we cannot store a reference to the trampoline code directly in
4535 // this stub, because runtime stubs are not traversed when doing GC.
4536
4537 // Expected registers by Builtins::JSEntryTrampoline
4538 // r0: code entry
4539 // r1: function
4540 // r2: receiver
4541 // r3: argc
4542 // r4: argv
4543 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01004544 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
4545 isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004546 __ mov(ip, Operand(construct_entry));
4547 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004548 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004549 __ mov(ip, Operand(entry));
4550 }
4551 __ ldr(ip, MemOperand(ip)); // deref address
4552
4553 // Branch and link to JSEntryTrampoline. We don't use the double underscore
4554 // macro for the add instruction because we don't want the coverage tool
4555 // inserting instructions here after we read the pc.
4556 __ mov(lr, Operand(pc));
4557 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
4558
4559 // Unlink this frame from the handler chain. When reading the
4560 // address of the next handler, there is no need to use the address
4561 // displacement since the current stack pointer (sp) points directly
4562 // to the stack handler.
4563 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01004564 __ mov(ip, Operand(ExternalReference(Isolate::k_handler_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004565 __ str(r3, MemOperand(ip));
4566 // No need to restore registers
4567 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
4568
Ben Murdochb0fe1622011-05-05 13:52:32 +01004569#ifdef ENABLE_LOGGING_AND_PROFILING
4570 // If current FP value is the same as js_entry_sp value, it means that
4571 // the current function is the outermost.
4572 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
4573 __ ldr(r6, MemOperand(r5));
4574 __ cmp(fp, Operand(r6));
4575 __ mov(r6, Operand(0, RelocInfo::NONE), LeaveCC, eq);
4576 __ str(r6, MemOperand(r5), eq);
4577#endif
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004578
4579 __ bind(&exit); // r0 holds result
4580 // Restore the top frame descriptors from the stack.
4581 __ pop(r3);
Steve Block44f0eee2011-05-26 01:26:41 +01004582 __ mov(ip,
4583 Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004584 __ str(r3, MemOperand(ip));
4585
4586 // Reset the stack to the callee saved registers.
4587 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
4588
4589 // Restore callee-saved registers and return.
4590#ifdef DEBUG
4591 if (FLAG_debug_code) {
4592 __ mov(lr, Operand(pc));
4593 }
4594#endif
4595 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
4596}
4597
4598
Steve Block1e0659c2011-05-24 12:43:12 +01004599// Uses registers r0 to r4.
4600// Expected input (depending on whether args are in registers or on the stack):
4601// * object: r0 or at sp + 1 * kPointerSize.
4602// * function: r1 or at sp.
4603//
4604// An inlined call site may have been generated before calling this stub.
4605// In this case the offset to the inline site to patch is passed on the stack,
4606// in the safepoint slot for register r4.
4607// (See LCodeGen::DoInstanceOfKnownGlobal)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004608void InstanceofStub::Generate(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01004609 // Call site inlining and patching implies arguments in registers.
4610 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4611 // ReturnTrueFalse is only implemented for inlined call sites.
4612 ASSERT(!ReturnTrueFalseObject() || HasCallSiteInlineCheck());
4613
Ben Murdochb0fe1622011-05-05 13:52:32 +01004614 // Fixed register usage throughout the stub:
Steve Block9fac8402011-05-12 15:51:54 +01004615 const Register object = r0; // Object (lhs).
Steve Block1e0659c2011-05-24 12:43:12 +01004616 Register map = r3; // Map of the object.
Steve Block9fac8402011-05-12 15:51:54 +01004617 const Register function = r1; // Function (rhs).
Ben Murdochb0fe1622011-05-05 13:52:32 +01004618 const Register prototype = r4; // Prototype of the function.
Steve Block1e0659c2011-05-24 12:43:12 +01004619 const Register inline_site = r9;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004620 const Register scratch = r2;
Steve Block1e0659c2011-05-24 12:43:12 +01004621
4622 const int32_t kDeltaToLoadBoolResult = 3 * kPointerSize;
4623
Ben Murdochb0fe1622011-05-05 13:52:32 +01004624 Label slow, loop, is_instance, is_not_instance, not_js_object;
Steve Block1e0659c2011-05-24 12:43:12 +01004625
Ben Murdoch086aeea2011-05-13 15:57:08 +01004626 if (!HasArgsInRegisters()) {
Steve Block9fac8402011-05-12 15:51:54 +01004627 __ ldr(object, MemOperand(sp, 1 * kPointerSize));
4628 __ ldr(function, MemOperand(sp, 0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004629 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004630
Ben Murdochb0fe1622011-05-05 13:52:32 +01004631 // Check that the left hand is a JS object and load map.
Steve Block1e0659c2011-05-24 12:43:12 +01004632 __ JumpIfSmi(object, &not_js_object);
Steve Block9fac8402011-05-12 15:51:54 +01004633 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004634
Steve Block1e0659c2011-05-24 12:43:12 +01004635 // If there is a call site cache don't look in the global cache, but do the
4636 // real lookup and update the call site cache.
4637 if (!HasCallSiteInlineCheck()) {
4638 Label miss;
4639 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
4640 __ cmp(function, ip);
4641 __ b(ne, &miss);
4642 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
4643 __ cmp(map, ip);
4644 __ b(ne, &miss);
4645 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
4646 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004647
Steve Block1e0659c2011-05-24 12:43:12 +01004648 __ bind(&miss);
4649 }
4650
4651 // Get the prototype of the function.
Steve Block9fac8402011-05-12 15:51:54 +01004652 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004653
4654 // Check that the function prototype is a JS object.
Steve Block1e0659c2011-05-24 12:43:12 +01004655 __ JumpIfSmi(prototype, &slow);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004656 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004657
Steve Block1e0659c2011-05-24 12:43:12 +01004658 // Update the global instanceof or call site inlined cache with the current
4659 // map and function. The cached answer will be set when it is known below.
4660 if (!HasCallSiteInlineCheck()) {
4661 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
4662 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
4663 } else {
4664 ASSERT(HasArgsInRegisters());
4665 // Patch the (relocated) inlined map check.
4666
4667 // The offset was stored in r4 safepoint slot.
4668 // (See LCodeGen::DoDeferredLInstanceOfKnownGlobal)
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004669 __ LoadFromSafepointRegisterSlot(scratch, r4);
Steve Block1e0659c2011-05-24 12:43:12 +01004670 __ sub(inline_site, lr, scratch);
4671 // Get the map location in scratch and patch it.
4672 __ GetRelocatedValueLocation(inline_site, scratch);
4673 __ str(map, MemOperand(scratch));
4674 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004675
4676 // Register mapping: r3 is object map and r4 is function prototype.
4677 // Get prototype of object into r2.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004678 __ ldr(scratch, FieldMemOperand(map, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004679
Steve Block1e0659c2011-05-24 12:43:12 +01004680 // We don't need map any more. Use it as a scratch register.
4681 Register scratch2 = map;
4682 map = no_reg;
4683
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004684 // Loop through the prototype chain looking for the function prototype.
Steve Block1e0659c2011-05-24 12:43:12 +01004685 __ LoadRoot(scratch2, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004686 __ bind(&loop);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004687 __ cmp(scratch, Operand(prototype));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004688 __ b(eq, &is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01004689 __ cmp(scratch, scratch2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004690 __ b(eq, &is_not_instance);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004691 __ ldr(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
4692 __ ldr(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004693 __ jmp(&loop);
4694
4695 __ bind(&is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01004696 if (!HasCallSiteInlineCheck()) {
4697 __ mov(r0, Operand(Smi::FromInt(0)));
4698 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
4699 } else {
4700 // Patch the call site to return true.
4701 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
4702 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
4703 // Get the boolean result location in scratch and patch it.
4704 __ GetRelocatedValueLocation(inline_site, scratch);
4705 __ str(r0, MemOperand(scratch));
4706
4707 if (!ReturnTrueFalseObject()) {
4708 __ mov(r0, Operand(Smi::FromInt(0)));
4709 }
4710 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01004711 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004712
4713 __ bind(&is_not_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01004714 if (!HasCallSiteInlineCheck()) {
4715 __ mov(r0, Operand(Smi::FromInt(1)));
4716 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
4717 } else {
4718 // Patch the call site to return false.
4719 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
4720 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
4721 // Get the boolean result location in scratch and patch it.
4722 __ GetRelocatedValueLocation(inline_site, scratch);
4723 __ str(r0, MemOperand(scratch));
4724
4725 if (!ReturnTrueFalseObject()) {
4726 __ mov(r0, Operand(Smi::FromInt(1)));
4727 }
4728 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01004729 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004730
4731 Label object_not_null, object_not_null_or_smi;
4732 __ bind(&not_js_object);
4733 // Before null, smi and string value checks, check that the rhs is a function
4734 // as for a non-function rhs an exception needs to be thrown.
Steve Block1e0659c2011-05-24 12:43:12 +01004735 __ JumpIfSmi(function, &slow);
4736 __ CompareObjectType(function, scratch2, scratch, JS_FUNCTION_TYPE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004737 __ b(ne, &slow);
4738
4739 // Null is not instance of anything.
Steve Block44f0eee2011-05-26 01:26:41 +01004740 __ cmp(scratch, Operand(FACTORY->null_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004741 __ b(ne, &object_not_null);
4742 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004743 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004744
4745 __ bind(&object_not_null);
4746 // Smi values are not instances of anything.
Steve Block1e0659c2011-05-24 12:43:12 +01004747 __ JumpIfNotSmi(object, &object_not_null_or_smi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004748 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004749 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004750
4751 __ bind(&object_not_null_or_smi);
4752 // String values are not instances of anything.
4753 __ IsObjectJSStringType(object, scratch, &slow);
4754 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004755 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004756
4757 // Slow-case. Tail call builtin.
Ben Murdoch086aeea2011-05-13 15:57:08 +01004758 __ bind(&slow);
Steve Block1e0659c2011-05-24 12:43:12 +01004759 if (!ReturnTrueFalseObject()) {
4760 if (HasArgsInRegisters()) {
4761 __ Push(r0, r1);
4762 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004763 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
Steve Block1e0659c2011-05-24 12:43:12 +01004764 } else {
4765 __ EnterInternalFrame();
4766 __ Push(r0, r1);
4767 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
4768 __ LeaveInternalFrame();
4769 __ cmp(r0, Operand(0));
4770 __ LoadRoot(r0, Heap::kTrueValueRootIndex, eq);
4771 __ LoadRoot(r0, Heap::kFalseValueRootIndex, ne);
4772 __ Ret(HasArgsInRegisters() ? 0 : 2);
4773 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004774}
4775
4776
Steve Block1e0659c2011-05-24 12:43:12 +01004777Register InstanceofStub::left() { return r0; }
4778
4779
4780Register InstanceofStub::right() { return r1; }
4781
4782
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004783void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
4784 // The displacement is the offset of the last parameter (if any)
4785 // relative to the frame pointer.
4786 static const int kDisplacement =
4787 StandardFrameConstants::kCallerSPOffset - kPointerSize;
4788
4789 // Check that the key is a smi.
4790 Label slow;
Steve Block1e0659c2011-05-24 12:43:12 +01004791 __ JumpIfNotSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004792
4793 // Check if the calling frame is an arguments adaptor frame.
4794 Label adaptor;
4795 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4796 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4797 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4798 __ b(eq, &adaptor);
4799
4800 // Check index against formal parameters count limit passed in
4801 // through register r0. Use unsigned comparison to get negative
4802 // check for free.
4803 __ cmp(r1, r0);
Ben Murdoch086aeea2011-05-13 15:57:08 +01004804 __ b(hs, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004805
4806 // Read the argument from the stack and return it.
4807 __ sub(r3, r0, r1);
4808 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4809 __ ldr(r0, MemOperand(r3, kDisplacement));
4810 __ Jump(lr);
4811
4812 // Arguments adaptor case: Check index against actual arguments
4813 // limit found in the arguments adaptor frame. Use unsigned
4814 // comparison to get negative check for free.
4815 __ bind(&adaptor);
4816 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4817 __ cmp(r1, r0);
4818 __ b(cs, &slow);
4819
4820 // Read the argument from the adaptor frame and return it.
4821 __ sub(r3, r0, r1);
4822 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4823 __ ldr(r0, MemOperand(r3, kDisplacement));
4824 __ Jump(lr);
4825
4826 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4827 // by calling the runtime system.
4828 __ bind(&slow);
4829 __ push(r1);
4830 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
4831}
4832
4833
4834void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
4835 // sp[0] : number of parameters
4836 // sp[4] : receiver displacement
4837 // sp[8] : function
4838
4839 // Check if the calling frame is an arguments adaptor frame.
4840 Label adaptor_frame, try_allocate, runtime;
4841 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4842 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4843 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4844 __ b(eq, &adaptor_frame);
4845
4846 // Get the length from the frame.
4847 __ ldr(r1, MemOperand(sp, 0));
4848 __ b(&try_allocate);
4849
4850 // Patch the arguments.length and the parameters pointer.
4851 __ bind(&adaptor_frame);
4852 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4853 __ str(r1, MemOperand(sp, 0));
4854 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
4855 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4856 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4857
4858 // Try the new space allocation. Start out with computing the size
4859 // of the arguments object and the elements array in words.
4860 Label add_arguments_object;
4861 __ bind(&try_allocate);
Iain Merrick9ac36c92010-09-13 15:29:50 +01004862 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004863 __ b(eq, &add_arguments_object);
4864 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
4865 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
4866 __ bind(&add_arguments_object);
Steve Block44f0eee2011-05-26 01:26:41 +01004867 __ add(r1, r1, Operand(GetArgumentsObjectSize() / kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004868
4869 // Do the allocation of both objects in one go.
4870 __ AllocateInNewSpace(
4871 r1,
4872 r0,
4873 r2,
4874 r3,
4875 &runtime,
4876 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
4877
4878 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004879 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4880 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01004881 __ ldr(r4, MemOperand(r4,
4882 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004883
4884 // Copy the JS object part.
4885 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
4886
Steve Block44f0eee2011-05-26 01:26:41 +01004887 if (type_ == NEW_NON_STRICT) {
4888 // Setup the callee in-object property.
4889 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
4890 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
4891 const int kCalleeOffset = JSObject::kHeaderSize +
4892 Heap::kArgumentsCalleeIndex * kPointerSize;
4893 __ str(r3, FieldMemOperand(r0, kCalleeOffset));
4894 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004895
4896 // Get the length (smi tagged) and set that as an in-object property too.
Steve Block44f0eee2011-05-26 01:26:41 +01004897 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004898 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01004899 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize +
4900 Heap::kArgumentsLengthIndex * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004901
4902 // If there are no actual arguments, we're done.
4903 Label done;
Iain Merrick9ac36c92010-09-13 15:29:50 +01004904 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004905 __ b(eq, &done);
4906
4907 // Get the parameters pointer from the stack.
4908 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
4909
4910 // Setup the elements pointer in the allocated arguments object and
4911 // initialize the header in the elements fixed array.
Steve Block44f0eee2011-05-26 01:26:41 +01004912 __ add(r4, r0, Operand(GetArgumentsObjectSize()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004913 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
4914 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
4915 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
4916 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
4917 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
4918
4919 // Copy the fixed array slots.
4920 Label loop;
4921 // Setup r4 to point to the first array slot.
4922 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4923 __ bind(&loop);
4924 // Pre-decrement r2 with kPointerSize on each iteration.
4925 // Pre-decrement in order to skip receiver.
4926 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
4927 // Post-increment r4 with kPointerSize on each iteration.
4928 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
4929 __ sub(r1, r1, Operand(1));
Iain Merrick9ac36c92010-09-13 15:29:50 +01004930 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004931 __ b(ne, &loop);
4932
4933 // Return and remove the on-stack parameters.
4934 __ bind(&done);
4935 __ add(sp, sp, Operand(3 * kPointerSize));
4936 __ Ret();
4937
4938 // Do the runtime call to allocate the arguments object.
4939 __ bind(&runtime);
4940 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
4941}
4942
4943
4944void RegExpExecStub::Generate(MacroAssembler* masm) {
4945 // Just jump directly to runtime if native RegExp is not selected at compile
4946 // time or if regexp entry in generated code is turned off runtime switch or
4947 // at compilation.
4948#ifdef V8_INTERPRETED_REGEXP
4949 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4950#else // V8_INTERPRETED_REGEXP
4951 if (!FLAG_regexp_entry_native) {
4952 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4953 return;
4954 }
4955
4956 // Stack frame on entry.
4957 // sp[0]: last_match_info (expected JSArray)
4958 // sp[4]: previous index
4959 // sp[8]: subject string
4960 // sp[12]: JSRegExp object
4961
4962 static const int kLastMatchInfoOffset = 0 * kPointerSize;
4963 static const int kPreviousIndexOffset = 1 * kPointerSize;
4964 static const int kSubjectOffset = 2 * kPointerSize;
4965 static const int kJSRegExpOffset = 3 * kPointerSize;
4966
4967 Label runtime, invoke_regexp;
4968
4969 // Allocation of registers for this function. These are in callee save
4970 // registers and will be preserved by the call to the native RegExp code, as
4971 // this code is called using the normal C calling convention. When calling
4972 // directly from generated code the native RegExp code will not do a GC and
4973 // therefore the content of these registers are safe to use after the call.
4974 Register subject = r4;
4975 Register regexp_data = r5;
4976 Register last_match_info_elements = r6;
4977
4978 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01004979 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004980 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01004981 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004982 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01004983 ExternalReference::address_of_regexp_stack_memory_size(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004984 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
4985 __ ldr(r0, MemOperand(r0, 0));
4986 __ tst(r0, Operand(r0));
4987 __ b(eq, &runtime);
4988
4989 // Check that the first argument is a JSRegExp object.
4990 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
4991 STATIC_ASSERT(kSmiTag == 0);
4992 __ tst(r0, Operand(kSmiTagMask));
4993 __ b(eq, &runtime);
4994 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
4995 __ b(ne, &runtime);
4996
4997 // Check that the RegExp has been compiled (data contains a fixed array).
4998 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
4999 if (FLAG_debug_code) {
5000 __ tst(regexp_data, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01005001 __ Check(ne, "Unexpected type for RegExp data, FixedArray expected");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005002 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
5003 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
5004 }
5005
5006 // regexp_data: RegExp data (FixedArray)
5007 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
5008 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
5009 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
5010 __ b(ne, &runtime);
5011
5012 // regexp_data: RegExp data (FixedArray)
5013 // Check that the number of captures fit in the static offsets vector buffer.
5014 __ ldr(r2,
5015 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
5016 // Calculate number of capture registers (number_of_captures + 1) * 2. This
5017 // uses the asumption that smis are 2 * their untagged value.
5018 STATIC_ASSERT(kSmiTag == 0);
5019 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5020 __ add(r2, r2, Operand(2)); // r2 was a smi.
5021 // Check that the static offsets vector buffer is large enough.
5022 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
5023 __ b(hi, &runtime);
5024
5025 // r2: Number of capture registers
5026 // regexp_data: RegExp data (FixedArray)
5027 // Check that the second argument is a string.
5028 __ ldr(subject, MemOperand(sp, kSubjectOffset));
5029 __ tst(subject, Operand(kSmiTagMask));
5030 __ b(eq, &runtime);
5031 Condition is_string = masm->IsObjectStringType(subject, r0);
5032 __ b(NegateCondition(is_string), &runtime);
5033 // Get the length of the string to r3.
5034 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
5035
5036 // r2: Number of capture registers
5037 // r3: Length of subject string as a smi
5038 // subject: Subject string
5039 // regexp_data: RegExp data (FixedArray)
5040 // Check that the third argument is a positive smi less than the subject
5041 // string length. A negative value will be greater (unsigned comparison).
5042 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
5043 __ tst(r0, Operand(kSmiTagMask));
5044 __ b(ne, &runtime);
5045 __ cmp(r3, Operand(r0));
5046 __ b(ls, &runtime);
5047
5048 // r2: Number of capture registers
5049 // subject: Subject string
5050 // regexp_data: RegExp data (FixedArray)
5051 // Check that the fourth object is a JSArray object.
5052 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
5053 __ tst(r0, Operand(kSmiTagMask));
5054 __ b(eq, &runtime);
5055 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
5056 __ b(ne, &runtime);
5057 // Check that the JSArray is in fast case.
5058 __ ldr(last_match_info_elements,
5059 FieldMemOperand(r0, JSArray::kElementsOffset));
5060 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
5061 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
5062 __ cmp(r0, ip);
5063 __ b(ne, &runtime);
5064 // Check that the last match info has space for the capture registers and the
5065 // additional information.
5066 __ ldr(r0,
5067 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
5068 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
5069 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
5070 __ b(gt, &runtime);
5071
5072 // subject: Subject string
5073 // regexp_data: RegExp data (FixedArray)
5074 // Check the representation and encoding of the subject string.
5075 Label seq_string;
5076 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
5077 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
5078 // First check for flat string.
5079 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
5080 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
5081 __ b(eq, &seq_string);
5082
5083 // subject: Subject string
5084 // regexp_data: RegExp data (FixedArray)
5085 // Check for flat cons string.
5086 // A flat cons string is a cons string where the second part is the empty
5087 // string. In that case the subject string is just the first part of the cons
5088 // string. Also in this case the first part of the cons string is known to be
5089 // a sequential string or an external string.
5090 STATIC_ASSERT(kExternalStringTag !=0);
5091 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
5092 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
5093 __ b(ne, &runtime);
5094 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
5095 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
5096 __ cmp(r0, r1);
5097 __ b(ne, &runtime);
5098 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
5099 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
5100 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
5101 // Is first part a flat string?
5102 STATIC_ASSERT(kSeqStringTag == 0);
5103 __ tst(r0, Operand(kStringRepresentationMask));
Steve Block1e0659c2011-05-24 12:43:12 +01005104 __ b(ne, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005105
5106 __ bind(&seq_string);
5107 // subject: Subject string
5108 // regexp_data: RegExp data (FixedArray)
5109 // r0: Instance type of subject string
5110 STATIC_ASSERT(4 == kAsciiStringTag);
5111 STATIC_ASSERT(kTwoByteStringTag == 0);
5112 // Find the code object based on the assumptions above.
5113 __ and_(r0, r0, Operand(kStringEncodingMask));
5114 __ mov(r3, Operand(r0, ASR, 2), SetCC);
5115 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
5116 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
5117
5118 // Check that the irregexp code has been generated for the actual string
5119 // encoding. If it has, the field contains a code object otherwise it contains
5120 // the hole.
5121 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
5122 __ b(ne, &runtime);
5123
Steve Block44f0eee2011-05-26 01:26:41 +01005124 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005125 // r7: code
5126 // subject: Subject string
5127 // regexp_data: RegExp data (FixedArray)
5128 // Load used arguments before starting to push arguments for call to native
5129 // RegExp code to avoid handling changing stack height.
5130 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
5131 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
5132
5133 // r1: previous index
Steve Block44f0eee2011-05-26 01:26:41 +01005134 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005135 // r7: code
5136 // subject: Subject string
5137 // regexp_data: RegExp data (FixedArray)
5138 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01005139 __ IncrementCounter(isolate->counters()->regexp_entry_native(), 1, r0, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005140
Steve Block44f0eee2011-05-26 01:26:41 +01005141 // Isolates: note we add an additional parameter here (isolate pointer).
5142 static const int kRegExpExecuteArguments = 8;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005143 static const int kParameterRegisters = 4;
5144 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005145
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005146 // Stack pointer now points to cell where return address is to be written.
5147 // Arguments are before that on the stack or in registers.
5148
Steve Block44f0eee2011-05-26 01:26:41 +01005149 // Argument 8 (sp[16]): Pass current isolate address.
5150 __ mov(r0, Operand(ExternalReference::isolate_address()));
5151 __ str(r0, MemOperand(sp, 4 * kPointerSize));
5152
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005153 // Argument 7 (sp[12]): Indicate that this is a direct call from JavaScript.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005154 __ mov(r0, Operand(1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005155 __ str(r0, MemOperand(sp, 3 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005156
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005157 // Argument 6 (sp[8]): Start (high end) of backtracking stack memory area.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005158 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
5159 __ ldr(r0, MemOperand(r0, 0));
5160 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
5161 __ ldr(r2, MemOperand(r2, 0));
5162 __ add(r0, r0, Operand(r2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005163 __ str(r0, MemOperand(sp, 2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005164
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005165 // Argument 5 (sp[4]): static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01005166 __ mov(r0,
5167 Operand(ExternalReference::address_of_static_offsets_vector(isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005168 __ str(r0, MemOperand(sp, 1 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005169
5170 // For arguments 4 and 3 get string length, calculate start of string data and
5171 // calculate the shift of the index (0 for ASCII and 1 for two byte).
5172 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
5173 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
5174 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
5175 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5176 __ eor(r3, r3, Operand(1));
5177 // Argument 4 (r3): End of string data
5178 // Argument 3 (r2): Start of string data
5179 __ add(r2, r9, Operand(r1, LSL, r3));
5180 __ add(r3, r9, Operand(r0, LSL, r3));
5181
5182 // Argument 2 (r1): Previous index.
5183 // Already there
5184
5185 // Argument 1 (r0): Subject string.
5186 __ mov(r0, subject);
5187
5188 // Locate the code entry and call it.
5189 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005190 DirectCEntryStub stub;
5191 stub.GenerateCall(masm, r7);
5192
5193 __ LeaveExitFrame(false, no_reg);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005194
5195 // r0: result
5196 // subject: subject string (callee saved)
5197 // regexp_data: RegExp data (callee saved)
5198 // last_match_info_elements: Last match info elements (callee saved)
5199
5200 // Check the result.
5201 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005202
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005203 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
5204 __ b(eq, &success);
5205 Label failure;
5206 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
5207 __ b(eq, &failure);
5208 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
5209 // If not exception it can only be retry. Handle that in the runtime system.
5210 __ b(ne, &runtime);
5211 // Result must now be exception. If there is no pending exception already a
5212 // stack overflow (on the backtrack stack) was detected in RegExp code but
5213 // haven't created the exception yet. Handle that in the runtime system.
5214 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01005215 __ mov(r1, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005216 __ ldr(r1, MemOperand(r1, 0));
Steve Block44f0eee2011-05-26 01:26:41 +01005217 __ mov(r2, Operand(ExternalReference(Isolate::k_pending_exception_address,
5218 isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005219 __ ldr(r0, MemOperand(r2, 0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005220 __ cmp(r0, r1);
5221 __ b(eq, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005222
5223 __ str(r1, MemOperand(r2, 0)); // Clear pending exception.
5224
5225 // Check if the exception is a termination. If so, throw as uncatchable.
5226 __ LoadRoot(ip, Heap::kTerminationExceptionRootIndex);
5227 __ cmp(r0, ip);
5228 Label termination_exception;
5229 __ b(eq, &termination_exception);
5230
5231 __ Throw(r0); // Expects thrown value in r0.
5232
5233 __ bind(&termination_exception);
5234 __ ThrowUncatchable(TERMINATION, r0); // Expects thrown value in r0.
5235
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005236 __ bind(&failure);
5237 // For failure and exception return null.
Steve Block44f0eee2011-05-26 01:26:41 +01005238 __ mov(r0, Operand(FACTORY->null_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005239 __ add(sp, sp, Operand(4 * kPointerSize));
5240 __ Ret();
5241
5242 // Process the result from the native regexp code.
5243 __ bind(&success);
5244 __ ldr(r1,
5245 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
5246 // Calculate number of capture registers (number_of_captures + 1) * 2.
5247 STATIC_ASSERT(kSmiTag == 0);
5248 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5249 __ add(r1, r1, Operand(2)); // r1 was a smi.
5250
5251 // r1: number of capture registers
5252 // r4: subject string
5253 // Store the capture count.
5254 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
5255 __ str(r2, FieldMemOperand(last_match_info_elements,
5256 RegExpImpl::kLastCaptureCountOffset));
5257 // Store last subject and last input.
5258 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
5259 __ str(subject,
5260 FieldMemOperand(last_match_info_elements,
5261 RegExpImpl::kLastSubjectOffset));
5262 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
5263 __ str(subject,
5264 FieldMemOperand(last_match_info_elements,
5265 RegExpImpl::kLastInputOffset));
5266 __ mov(r3, last_match_info_elements);
5267 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
5268
5269 // Get the static offsets vector filled by the native regexp code.
5270 ExternalReference address_of_static_offsets_vector =
Steve Block44f0eee2011-05-26 01:26:41 +01005271 ExternalReference::address_of_static_offsets_vector(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005272 __ mov(r2, Operand(address_of_static_offsets_vector));
5273
5274 // r1: number of capture registers
5275 // r2: offsets vector
5276 Label next_capture, done;
5277 // Capture register counter starts from number of capture registers and
5278 // counts down until wraping after zero.
5279 __ add(r0,
5280 last_match_info_elements,
5281 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
5282 __ bind(&next_capture);
5283 __ sub(r1, r1, Operand(1), SetCC);
5284 __ b(mi, &done);
5285 // Read the value from the static offsets vector buffer.
5286 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
5287 // Store the smi value in the last match info.
5288 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
5289 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
5290 __ jmp(&next_capture);
5291 __ bind(&done);
5292
5293 // Return last match info.
5294 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
5295 __ add(sp, sp, Operand(4 * kPointerSize));
5296 __ Ret();
5297
5298 // Do the runtime call to execute the regexp.
5299 __ bind(&runtime);
5300 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
5301#endif // V8_INTERPRETED_REGEXP
5302}
5303
5304
Ben Murdochb0fe1622011-05-05 13:52:32 +01005305void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
5306 const int kMaxInlineLength = 100;
5307 Label slowcase;
5308 Label done;
5309 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5310 STATIC_ASSERT(kSmiTag == 0);
5311 STATIC_ASSERT(kSmiTagSize == 1);
5312 __ tst(r1, Operand(kSmiTagMask));
5313 __ b(ne, &slowcase);
5314 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
5315 __ b(hi, &slowcase);
5316 // Smi-tagging is equivalent to multiplying by 2.
5317 // Allocate RegExpResult followed by FixedArray with size in ebx.
5318 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5319 // Elements: [Map][Length][..elements..]
5320 // Size of JSArray with two in-object properties and the header of a
5321 // FixedArray.
5322 int objects_size =
5323 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5324 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
5325 __ add(r2, r5, Operand(objects_size));
5326 __ AllocateInNewSpace(
5327 r2, // In: Size, in words.
5328 r0, // Out: Start of allocation (tagged).
5329 r3, // Scratch register.
5330 r4, // Scratch register.
5331 &slowcase,
5332 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
5333 // r0: Start of allocated area, object-tagged.
5334 // r1: Number of elements in array, as smi.
5335 // r5: Number of elements, untagged.
5336
5337 // Set JSArray map to global.regexp_result_map().
5338 // Set empty properties FixedArray.
5339 // Set elements to point to FixedArray allocated right after the JSArray.
5340 // Interleave operations for better latency.
5341 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
5342 __ add(r3, r0, Operand(JSRegExpResult::kSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005343 __ mov(r4, Operand(FACTORY->empty_fixed_array()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005344 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
5345 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
5346 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
5347 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
5348 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
5349
5350 // Set input, index and length fields from arguments.
5351 __ ldr(r1, MemOperand(sp, kPointerSize * 0));
5352 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
5353 __ ldr(r1, MemOperand(sp, kPointerSize * 1));
5354 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
5355 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
5356 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
5357
5358 // Fill out the elements FixedArray.
5359 // r0: JSArray, tagged.
5360 // r3: FixedArray, tagged.
5361 // r5: Number of elements in array, untagged.
5362
5363 // Set map.
Steve Block44f0eee2011-05-26 01:26:41 +01005364 __ mov(r2, Operand(FACTORY->fixed_array_map()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005365 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5366 // Set FixedArray length.
5367 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5368 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
5369 // Fill contents of fixed-array with the-hole.
Steve Block44f0eee2011-05-26 01:26:41 +01005370 __ mov(r2, Operand(FACTORY->the_hole_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005371 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5372 // Fill fixed array elements with hole.
5373 // r0: JSArray, tagged.
5374 // r2: the hole.
5375 // r3: Start of elements in FixedArray.
5376 // r5: Number of elements to fill.
5377 Label loop;
5378 __ tst(r5, Operand(r5));
5379 __ bind(&loop);
5380 __ b(le, &done); // Jump if r1 is negative or zero.
5381 __ sub(r5, r5, Operand(1), SetCC);
5382 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5383 __ jmp(&loop);
5384
5385 __ bind(&done);
5386 __ add(sp, sp, Operand(3 * kPointerSize));
5387 __ Ret();
5388
5389 __ bind(&slowcase);
5390 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
5391}
5392
5393
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005394void CallFunctionStub::Generate(MacroAssembler* masm) {
5395 Label slow;
5396
5397 // If the receiver might be a value (string, number or boolean) check for this
5398 // and box it if it is.
5399 if (ReceiverMightBeValue()) {
5400 // Get the receiver from the stack.
5401 // function, receiver [, arguments]
5402 Label receiver_is_value, receiver_is_js_object;
5403 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
5404
5405 // Check if receiver is a smi (which is a number value).
Steve Block1e0659c2011-05-24 12:43:12 +01005406 __ JumpIfSmi(r1, &receiver_is_value);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005407
5408 // Check if the receiver is a valid JS object.
5409 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
5410 __ b(ge, &receiver_is_js_object);
5411
5412 // Call the runtime to box the value.
5413 __ bind(&receiver_is_value);
5414 __ EnterInternalFrame();
5415 __ push(r1);
5416 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
5417 __ LeaveInternalFrame();
5418 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
5419
5420 __ bind(&receiver_is_js_object);
5421 }
5422
5423 // Get the function to call from the stack.
5424 // function, receiver [, arguments]
5425 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
5426
5427 // Check that the function is really a JavaScript function.
5428 // r1: pushed function (to be verified)
Steve Block1e0659c2011-05-24 12:43:12 +01005429 __ JumpIfSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005430 // Get the map of the function object.
5431 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
5432 __ b(ne, &slow);
5433
5434 // Fast-case: Invoke the function now.
5435 // r1: pushed function
5436 ParameterCount actual(argc_);
5437 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
5438
5439 // Slow-case: Non-function called.
5440 __ bind(&slow);
5441 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
5442 // of the original receiver from the call site).
5443 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
5444 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005445 __ mov(r2, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005446 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01005447 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005448 RelocInfo::CODE_TARGET);
5449}
5450
5451
5452// Unfortunately you have to run without snapshots to see most of these
5453// names in the profile since most compare stubs end up in the snapshot.
5454const char* CompareStub::GetName() {
5455 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
5456 (lhs_.is(r1) && rhs_.is(r0)));
5457
5458 if (name_ != NULL) return name_;
5459 const int kMaxNameLength = 100;
Steve Block44f0eee2011-05-26 01:26:41 +01005460 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
5461 kMaxNameLength);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005462 if (name_ == NULL) return "OOM";
5463
5464 const char* cc_name;
5465 switch (cc_) {
5466 case lt: cc_name = "LT"; break;
5467 case gt: cc_name = "GT"; break;
5468 case le: cc_name = "LE"; break;
5469 case ge: cc_name = "GE"; break;
5470 case eq: cc_name = "EQ"; break;
5471 case ne: cc_name = "NE"; break;
5472 default: cc_name = "UnknownCondition"; break;
5473 }
5474
5475 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
5476 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
5477
5478 const char* strict_name = "";
5479 if (strict_ && (cc_ == eq || cc_ == ne)) {
5480 strict_name = "_STRICT";
5481 }
5482
5483 const char* never_nan_nan_name = "";
5484 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
5485 never_nan_nan_name = "_NO_NAN";
5486 }
5487
5488 const char* include_number_compare_name = "";
5489 if (!include_number_compare_) {
5490 include_number_compare_name = "_NO_NUMBER";
5491 }
5492
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005493 const char* include_smi_compare_name = "";
5494 if (!include_smi_compare_) {
5495 include_smi_compare_name = "_NO_SMI";
5496 }
5497
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005498 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
5499 "CompareStub_%s%s%s%s%s%s",
5500 cc_name,
5501 lhs_name,
5502 rhs_name,
5503 strict_name,
5504 never_nan_nan_name,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005505 include_number_compare_name,
5506 include_smi_compare_name);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005507 return name_;
5508}
5509
5510
5511int CompareStub::MinorKey() {
5512 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
5513 // stubs the never NaN NaN condition is only taken into account if the
5514 // condition is equals.
5515 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
5516 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
5517 (lhs_.is(r1) && rhs_.is(r0)));
5518 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
5519 | RegisterField::encode(lhs_.is(r0))
5520 | StrictField::encode(strict_)
5521 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005522 | IncludeNumberCompareField::encode(include_number_compare_)
5523 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005524}
5525
5526
5527// StringCharCodeAtGenerator
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005528void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
5529 Label flat_string;
5530 Label ascii_string;
5531 Label got_char_code;
5532
5533 // If the receiver is a smi trigger the non-string case.
Steve Block1e0659c2011-05-24 12:43:12 +01005534 __ JumpIfSmi(object_, receiver_not_string_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005535
5536 // Fetch the instance type of the receiver into result register.
5537 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5538 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
5539 // If the receiver is not a string trigger the non-string case.
5540 __ tst(result_, Operand(kIsNotStringMask));
5541 __ b(ne, receiver_not_string_);
5542
5543 // If the index is non-smi trigger the non-smi case.
Steve Block1e0659c2011-05-24 12:43:12 +01005544 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005545
5546 // Put smi-tagged index into scratch register.
5547 __ mov(scratch_, index_);
5548 __ bind(&got_smi_index_);
5549
5550 // Check for index out of range.
5551 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
5552 __ cmp(ip, Operand(scratch_));
5553 __ b(ls, index_out_of_range_);
5554
5555 // We need special handling for non-flat strings.
5556 STATIC_ASSERT(kSeqStringTag == 0);
5557 __ tst(result_, Operand(kStringRepresentationMask));
5558 __ b(eq, &flat_string);
5559
5560 // Handle non-flat strings.
5561 __ tst(result_, Operand(kIsConsStringMask));
5562 __ b(eq, &call_runtime_);
5563
5564 // ConsString.
5565 // Check whether the right hand side is the empty string (i.e. if
5566 // this is really a flat string in a cons string). If that is not
5567 // the case we would rather go to the runtime system now to flatten
5568 // the string.
5569 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
5570 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
5571 __ cmp(result_, Operand(ip));
5572 __ b(ne, &call_runtime_);
5573 // Get the first of the two strings and load its instance type.
5574 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
5575 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5576 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
5577 // If the first cons component is also non-flat, then go to runtime.
5578 STATIC_ASSERT(kSeqStringTag == 0);
5579 __ tst(result_, Operand(kStringRepresentationMask));
Steve Block1e0659c2011-05-24 12:43:12 +01005580 __ b(ne, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005581
5582 // Check for 1-byte or 2-byte string.
5583 __ bind(&flat_string);
5584 STATIC_ASSERT(kAsciiStringTag != 0);
5585 __ tst(result_, Operand(kStringEncodingMask));
Steve Block1e0659c2011-05-24 12:43:12 +01005586 __ b(ne, &ascii_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005587
5588 // 2-byte string.
5589 // Load the 2-byte character code into the result register. We can
5590 // add without shifting since the smi tag size is the log2 of the
5591 // number of bytes in a two-byte character.
5592 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
5593 __ add(scratch_, object_, Operand(scratch_));
5594 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
5595 __ jmp(&got_char_code);
5596
5597 // ASCII string.
5598 // Load the byte into the result register.
5599 __ bind(&ascii_string);
5600 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
5601 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
5602
5603 __ bind(&got_char_code);
5604 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
5605 __ bind(&exit_);
5606}
5607
5608
5609void StringCharCodeAtGenerator::GenerateSlow(
5610 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5611 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
5612
5613 // Index is not a smi.
5614 __ bind(&index_not_smi_);
5615 // If index is a heap number, try converting it to an integer.
5616 __ CheckMap(index_,
5617 scratch_,
5618 Heap::kHeapNumberMapRootIndex,
5619 index_not_number_,
5620 true);
5621 call_helper.BeforeCall(masm);
5622 __ Push(object_, index_);
5623 __ push(index_); // Consumed by runtime conversion function.
5624 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
5625 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
5626 } else {
5627 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
5628 // NumberToSmi discards numbers that are not exact integers.
5629 __ CallRuntime(Runtime::kNumberToSmi, 1);
5630 }
5631 // Save the conversion result before the pop instructions below
5632 // have a chance to overwrite it.
5633 __ Move(scratch_, r0);
5634 __ pop(index_);
5635 __ pop(object_);
5636 // Reload the instance type.
5637 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5638 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
5639 call_helper.AfterCall(masm);
5640 // If index is still not a smi, it must be out of range.
Steve Block1e0659c2011-05-24 12:43:12 +01005641 __ JumpIfNotSmi(scratch_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005642 // Otherwise, return to the fast path.
5643 __ jmp(&got_smi_index_);
5644
5645 // Call runtime. We get here when the receiver is a string and the
5646 // index is a number, but the code of getting the actual character
5647 // is too complex (e.g., when the string needs to be flattened).
5648 __ bind(&call_runtime_);
5649 call_helper.BeforeCall(masm);
5650 __ Push(object_, index_);
5651 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
5652 __ Move(result_, r0);
5653 call_helper.AfterCall(masm);
5654 __ jmp(&exit_);
5655
5656 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
5657}
5658
5659
5660// -------------------------------------------------------------------------
5661// StringCharFromCodeGenerator
5662
5663void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
5664 // Fast case of Heap::LookupSingleCharacterStringFromCode.
5665 STATIC_ASSERT(kSmiTag == 0);
5666 STATIC_ASSERT(kSmiShiftSize == 0);
5667 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
5668 __ tst(code_,
5669 Operand(kSmiTagMask |
5670 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
Steve Block1e0659c2011-05-24 12:43:12 +01005671 __ b(ne, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005672
5673 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01005674 // At this point code register contains smi tagged ASCII char code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005675 STATIC_ASSERT(kSmiTag == 0);
5676 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
5677 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
5678 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5679 __ cmp(result_, Operand(ip));
5680 __ b(eq, &slow_case_);
5681 __ bind(&exit_);
5682}
5683
5684
5685void StringCharFromCodeGenerator::GenerateSlow(
5686 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5687 __ Abort("Unexpected fallthrough to CharFromCode slow case");
5688
5689 __ bind(&slow_case_);
5690 call_helper.BeforeCall(masm);
5691 __ push(code_);
5692 __ CallRuntime(Runtime::kCharFromCode, 1);
5693 __ Move(result_, r0);
5694 call_helper.AfterCall(masm);
5695 __ jmp(&exit_);
5696
5697 __ Abort("Unexpected fallthrough from CharFromCode slow case");
5698}
5699
5700
5701// -------------------------------------------------------------------------
5702// StringCharAtGenerator
5703
5704void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
5705 char_code_at_generator_.GenerateFast(masm);
5706 char_from_code_generator_.GenerateFast(masm);
5707}
5708
5709
5710void StringCharAtGenerator::GenerateSlow(
5711 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
5712 char_code_at_generator_.GenerateSlow(masm, call_helper);
5713 char_from_code_generator_.GenerateSlow(masm, call_helper);
5714}
5715
5716
5717class StringHelper : public AllStatic {
5718 public:
5719 // Generate code for copying characters using a simple loop. This should only
5720 // be used in places where the number of characters is small and the
5721 // additional setup and checking in GenerateCopyCharactersLong adds too much
5722 // overhead. Copying of overlapping regions is not supported.
5723 // Dest register ends at the position after the last character written.
5724 static void GenerateCopyCharacters(MacroAssembler* masm,
5725 Register dest,
5726 Register src,
5727 Register count,
5728 Register scratch,
5729 bool ascii);
5730
5731 // Generate code for copying a large number of characters. This function
5732 // is allowed to spend extra time setting up conditions to make copying
5733 // faster. Copying of overlapping regions is not supported.
5734 // Dest register ends at the position after the last character written.
5735 static void GenerateCopyCharactersLong(MacroAssembler* masm,
5736 Register dest,
5737 Register src,
5738 Register count,
5739 Register scratch1,
5740 Register scratch2,
5741 Register scratch3,
5742 Register scratch4,
5743 Register scratch5,
5744 int flags);
5745
5746
5747 // Probe the symbol table for a two character string. If the string is
5748 // not found by probing a jump to the label not_found is performed. This jump
5749 // does not guarantee that the string is not in the symbol table. If the
5750 // string is found the code falls through with the string in register r0.
5751 // Contents of both c1 and c2 registers are modified. At the exit c1 is
5752 // guaranteed to contain halfword with low and high bytes equal to
5753 // initial contents of c1 and c2 respectively.
5754 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5755 Register c1,
5756 Register c2,
5757 Register scratch1,
5758 Register scratch2,
5759 Register scratch3,
5760 Register scratch4,
5761 Register scratch5,
5762 Label* not_found);
5763
5764 // Generate string hash.
5765 static void GenerateHashInit(MacroAssembler* masm,
5766 Register hash,
5767 Register character);
5768
5769 static void GenerateHashAddCharacter(MacroAssembler* masm,
5770 Register hash,
5771 Register character);
5772
5773 static void GenerateHashGetHash(MacroAssembler* masm,
5774 Register hash);
5775
5776 private:
5777 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
5778};
5779
5780
5781void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5782 Register dest,
5783 Register src,
5784 Register count,
5785 Register scratch,
5786 bool ascii) {
5787 Label loop;
5788 Label done;
5789 // This loop just copies one character at a time, as it is only used for very
5790 // short strings.
5791 if (!ascii) {
5792 __ add(count, count, Operand(count), SetCC);
5793 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01005794 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005795 }
5796 __ b(eq, &done);
5797
5798 __ bind(&loop);
5799 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
5800 // Perform sub between load and dependent store to get the load time to
5801 // complete.
5802 __ sub(count, count, Operand(1), SetCC);
5803 __ strb(scratch, MemOperand(dest, 1, PostIndex));
5804 // last iteration.
5805 __ b(gt, &loop);
5806
5807 __ bind(&done);
5808}
5809
5810
5811enum CopyCharactersFlags {
5812 COPY_ASCII = 1,
5813 DEST_ALWAYS_ALIGNED = 2
5814};
5815
5816
5817void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
5818 Register dest,
5819 Register src,
5820 Register count,
5821 Register scratch1,
5822 Register scratch2,
5823 Register scratch3,
5824 Register scratch4,
5825 Register scratch5,
5826 int flags) {
5827 bool ascii = (flags & COPY_ASCII) != 0;
5828 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
5829
5830 if (dest_always_aligned && FLAG_debug_code) {
5831 // Check that destination is actually word aligned if the flag says
5832 // that it is.
5833 __ tst(dest, Operand(kPointerAlignmentMask));
5834 __ Check(eq, "Destination of copy not aligned.");
5835 }
5836
5837 const int kReadAlignment = 4;
5838 const int kReadAlignmentMask = kReadAlignment - 1;
5839 // Ensure that reading an entire aligned word containing the last character
5840 // of a string will not read outside the allocated area (because we pad up
5841 // to kObjectAlignment).
5842 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
5843 // Assumes word reads and writes are little endian.
5844 // Nothing to do for zero characters.
5845 Label done;
5846 if (!ascii) {
5847 __ add(count, count, Operand(count), SetCC);
5848 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01005849 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005850 }
5851 __ b(eq, &done);
5852
5853 // Assume that you cannot read (or write) unaligned.
5854 Label byte_loop;
5855 // Must copy at least eight bytes, otherwise just do it one byte at a time.
5856 __ cmp(count, Operand(8));
5857 __ add(count, dest, Operand(count));
5858 Register limit = count; // Read until src equals this.
5859 __ b(lt, &byte_loop);
5860
5861 if (!dest_always_aligned) {
5862 // Align dest by byte copying. Copies between zero and three bytes.
5863 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
5864 Label dest_aligned;
5865 __ b(eq, &dest_aligned);
5866 __ cmp(scratch4, Operand(2));
5867 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
5868 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
5869 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
5870 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5871 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
5872 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
5873 __ bind(&dest_aligned);
5874 }
5875
5876 Label simple_loop;
5877
5878 __ sub(scratch4, dest, Operand(src));
5879 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
5880 __ b(eq, &simple_loop);
5881 // Shift register is number of bits in a source word that
5882 // must be combined with bits in the next source word in order
5883 // to create a destination word.
5884
5885 // Complex loop for src/dst that are not aligned the same way.
5886 {
5887 Label loop;
5888 __ mov(scratch4, Operand(scratch4, LSL, 3));
5889 Register left_shift = scratch4;
5890 __ and_(src, src, Operand(~3)); // Round down to load previous word.
5891 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
5892 // Store the "shift" most significant bits of scratch in the least
5893 // signficant bits (i.e., shift down by (32-shift)).
5894 __ rsb(scratch2, left_shift, Operand(32));
5895 Register right_shift = scratch2;
5896 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
5897
5898 __ bind(&loop);
5899 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
5900 __ sub(scratch5, limit, Operand(dest));
5901 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
5902 __ str(scratch1, MemOperand(dest, 4, PostIndex));
5903 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
5904 // Loop if four or more bytes left to copy.
5905 // Compare to eight, because we did the subtract before increasing dst.
5906 __ sub(scratch5, scratch5, Operand(8), SetCC);
5907 __ b(ge, &loop);
5908 }
5909 // There is now between zero and three bytes left to copy (negative that
5910 // number is in scratch5), and between one and three bytes already read into
5911 // scratch1 (eight times that number in scratch4). We may have read past
5912 // the end of the string, but because objects are aligned, we have not read
5913 // past the end of the object.
5914 // Find the minimum of remaining characters to move and preloaded characters
5915 // and write those as bytes.
5916 __ add(scratch5, scratch5, Operand(4), SetCC);
5917 __ b(eq, &done);
5918 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
5919 // Move minimum of bytes read and bytes left to copy to scratch4.
5920 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
5921 // Between one and three (value in scratch5) characters already read into
5922 // scratch ready to write.
5923 __ cmp(scratch5, Operand(2));
5924 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5925 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
5926 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
5927 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
5928 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
5929 // Copy any remaining bytes.
5930 __ b(&byte_loop);
5931
5932 // Simple loop.
5933 // Copy words from src to dst, until less than four bytes left.
5934 // Both src and dest are word aligned.
5935 __ bind(&simple_loop);
5936 {
5937 Label loop;
5938 __ bind(&loop);
5939 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
5940 __ sub(scratch3, limit, Operand(dest));
5941 __ str(scratch1, MemOperand(dest, 4, PostIndex));
5942 // Compare to 8, not 4, because we do the substraction before increasing
5943 // dest.
5944 __ cmp(scratch3, Operand(8));
5945 __ b(ge, &loop);
5946 }
5947
5948 // Copy bytes from src to dst until dst hits limit.
5949 __ bind(&byte_loop);
5950 __ cmp(dest, Operand(limit));
5951 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
5952 __ b(ge, &done);
5953 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5954 __ b(&byte_loop);
5955
5956 __ bind(&done);
5957}
5958
5959
5960void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5961 Register c1,
5962 Register c2,
5963 Register scratch1,
5964 Register scratch2,
5965 Register scratch3,
5966 Register scratch4,
5967 Register scratch5,
5968 Label* not_found) {
5969 // Register scratch3 is the general scratch register in this function.
5970 Register scratch = scratch3;
5971
5972 // Make sure that both characters are not digits as such strings has a
5973 // different hash algorithm. Don't try to look for these in the symbol table.
5974 Label not_array_index;
5975 __ sub(scratch, c1, Operand(static_cast<int>('0')));
5976 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
5977 __ b(hi, &not_array_index);
5978 __ sub(scratch, c2, Operand(static_cast<int>('0')));
5979 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
5980
5981 // If check failed combine both characters into single halfword.
5982 // This is required by the contract of the method: code at the
5983 // not_found branch expects this combination in c1 register
5984 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
5985 __ b(ls, not_found);
5986
5987 __ bind(&not_array_index);
5988 // Calculate the two character string hash.
5989 Register hash = scratch1;
5990 StringHelper::GenerateHashInit(masm, hash, c1);
5991 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
5992 StringHelper::GenerateHashGetHash(masm, hash);
5993
5994 // Collect the two characters in a register.
5995 Register chars = c1;
5996 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
5997
5998 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5999 // hash: hash of two character string.
6000
6001 // Load symbol table
6002 // Load address of first element of the symbol table.
6003 Register symbol_table = c2;
6004 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
6005
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006006 Register undefined = scratch4;
6007 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
6008
6009 // Calculate capacity mask from the symbol table capacity.
6010 Register mask = scratch2;
6011 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
6012 __ mov(mask, Operand(mask, ASR, 1));
6013 __ sub(mask, mask, Operand(1));
6014
6015 // Calculate untagged address of the first element of the symbol table.
6016 Register first_symbol_table_element = symbol_table;
6017 __ add(first_symbol_table_element, symbol_table,
6018 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
6019
6020 // Registers
6021 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
6022 // hash: hash of two character string
6023 // mask: capacity mask
6024 // first_symbol_table_element: address of the first element of
6025 // the symbol table
Steve Block44f0eee2011-05-26 01:26:41 +01006026 // undefined: the undefined object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006027 // scratch: -
6028
6029 // Perform a number of probes in the symbol table.
6030 static const int kProbes = 4;
6031 Label found_in_symbol_table;
6032 Label next_probe[kProbes];
6033 for (int i = 0; i < kProbes; i++) {
6034 Register candidate = scratch5; // Scratch register contains candidate.
6035
6036 // Calculate entry in symbol table.
6037 if (i > 0) {
6038 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
6039 } else {
6040 __ mov(candidate, hash);
6041 }
6042
6043 __ and_(candidate, candidate, Operand(mask));
6044
6045 // Load the entry from the symble table.
6046 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
6047 __ ldr(candidate,
6048 MemOperand(first_symbol_table_element,
6049 candidate,
6050 LSL,
6051 kPointerSizeLog2));
6052
6053 // If entry is undefined no string with this hash can be found.
Steve Block44f0eee2011-05-26 01:26:41 +01006054 Label is_string;
6055 __ CompareObjectType(candidate, scratch, scratch, ODDBALL_TYPE);
6056 __ b(ne, &is_string);
6057
6058 __ cmp(undefined, candidate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006059 __ b(eq, not_found);
Steve Block44f0eee2011-05-26 01:26:41 +01006060 // Must be null (deleted entry).
6061 if (FLAG_debug_code) {
6062 __ LoadRoot(ip, Heap::kNullValueRootIndex);
6063 __ cmp(ip, candidate);
6064 __ Assert(eq, "oddball in symbol table is not undefined or null");
6065 }
6066 __ jmp(&next_probe[i]);
6067
6068 __ bind(&is_string);
6069
6070 // Check that the candidate is a non-external ASCII string. The instance
6071 // type is still in the scratch register from the CompareObjectType
6072 // operation.
6073 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, &next_probe[i]);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006074
6075 // If length is not 2 the string is not a candidate.
6076 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
6077 __ cmp(scratch, Operand(Smi::FromInt(2)));
6078 __ b(ne, &next_probe[i]);
6079
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006080 // Check if the two characters match.
6081 // Assumes that word load is little endian.
6082 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
6083 __ cmp(chars, scratch);
6084 __ b(eq, &found_in_symbol_table);
6085 __ bind(&next_probe[i]);
6086 }
6087
6088 // No matching 2 character string found by probing.
6089 __ jmp(not_found);
6090
6091 // Scratch register contains result when we fall through to here.
6092 Register result = scratch;
6093 __ bind(&found_in_symbol_table);
6094 __ Move(r0, result);
6095}
6096
6097
6098void StringHelper::GenerateHashInit(MacroAssembler* masm,
6099 Register hash,
6100 Register character) {
6101 // hash = character + (character << 10);
6102 __ add(hash, character, Operand(character, LSL, 10));
6103 // hash ^= hash >> 6;
6104 __ eor(hash, hash, Operand(hash, ASR, 6));
6105}
6106
6107
6108void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
6109 Register hash,
6110 Register character) {
6111 // hash += character;
6112 __ add(hash, hash, Operand(character));
6113 // hash += hash << 10;
6114 __ add(hash, hash, Operand(hash, LSL, 10));
6115 // hash ^= hash >> 6;
6116 __ eor(hash, hash, Operand(hash, ASR, 6));
6117}
6118
6119
6120void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
6121 Register hash) {
6122 // hash += hash << 3;
6123 __ add(hash, hash, Operand(hash, LSL, 3));
6124 // hash ^= hash >> 11;
6125 __ eor(hash, hash, Operand(hash, ASR, 11));
6126 // hash += hash << 15;
6127 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
6128
6129 // if (hash == 0) hash = 27;
Steve Block1e0659c2011-05-24 12:43:12 +01006130 __ mov(hash, Operand(27), LeaveCC, ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006131}
6132
6133
6134void SubStringStub::Generate(MacroAssembler* masm) {
6135 Label runtime;
6136
6137 // Stack frame on entry.
6138 // lr: return address
6139 // sp[0]: to
6140 // sp[4]: from
6141 // sp[8]: string
6142
6143 // This stub is called from the native-call %_SubString(...), so
6144 // nothing can be assumed about the arguments. It is tested that:
6145 // "string" is a sequential string,
6146 // both "from" and "to" are smis, and
6147 // 0 <= from <= to <= string.length.
6148 // If any of these assumptions fail, we call the runtime system.
6149
6150 static const int kToOffset = 0 * kPointerSize;
6151 static const int kFromOffset = 1 * kPointerSize;
6152 static const int kStringOffset = 2 * kPointerSize;
6153
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006154 // Check bounds and smi-ness.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006155 Register to = r6;
6156 Register from = r7;
6157 __ Ldrd(to, from, MemOperand(sp, kToOffset));
6158 STATIC_ASSERT(kFromOffset == kToOffset + 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006159 STATIC_ASSERT(kSmiTag == 0);
6160 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
6161 // I.e., arithmetic shift right by one un-smi-tags.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006162 __ mov(r2, Operand(to, ASR, 1), SetCC);
6163 __ mov(r3, Operand(from, ASR, 1), SetCC, cc);
6164 // If either to or from had the smi tag bit set, then carry is set now.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006165 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
6166 __ b(mi, &runtime); // From is negative.
6167
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006168 // Both to and from are smis.
6169
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006170 __ sub(r2, r2, Operand(r3), SetCC);
6171 __ b(mi, &runtime); // Fail if from > to.
6172 // Special handling of sub-strings of length 1 and 2. One character strings
6173 // are handled in the runtime system (looked up in the single character
6174 // cache). Two character strings are looked for in the symbol cache.
6175 __ cmp(r2, Operand(2));
6176 __ b(lt, &runtime);
6177
6178 // r2: length
6179 // r3: from index (untaged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006180 // r6 (a.k.a. to): to (smi)
6181 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006182
6183 // Make sure first argument is a sequential (or flat) string.
6184 __ ldr(r5, MemOperand(sp, kStringOffset));
6185 STATIC_ASSERT(kSmiTag == 0);
6186 __ tst(r5, Operand(kSmiTagMask));
6187 __ b(eq, &runtime);
6188 Condition is_string = masm->IsObjectStringType(r5, r1);
6189 __ b(NegateCondition(is_string), &runtime);
6190
6191 // r1: instance type
6192 // r2: length
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006193 // r3: from index (untagged smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006194 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006195 // r6 (a.k.a. to): to (smi)
6196 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006197 Label seq_string;
6198 __ and_(r4, r1, Operand(kStringRepresentationMask));
6199 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
6200 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
6201 __ cmp(r4, Operand(kConsStringTag));
6202 __ b(gt, &runtime); // External strings go to runtime.
6203 __ b(lt, &seq_string); // Sequential strings are handled directly.
6204
6205 // Cons string. Try to recurse (once) on the first substring.
6206 // (This adds a little more generality than necessary to handle flattened
6207 // cons strings, but not much).
6208 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
6209 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
6210 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6211 __ tst(r1, Operand(kStringRepresentationMask));
6212 STATIC_ASSERT(kSeqStringTag == 0);
6213 __ b(ne, &runtime); // Cons and External strings go to runtime.
6214
6215 // Definitly a sequential string.
6216 __ bind(&seq_string);
6217
6218 // r1: instance type.
6219 // r2: length
6220 // r3: from index (untaged smi)
6221 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006222 // r6 (a.k.a. to): to (smi)
6223 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006224 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006225 __ cmp(r4, Operand(to));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006226 __ b(lt, &runtime); // Fail if to > length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006227 to = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006228
6229 // r1: instance type.
6230 // r2: result string length.
6231 // r3: from index (untaged smi)
6232 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006233 // r7 (a.k.a. from): from offset (smi)
Steve Block44f0eee2011-05-26 01:26:41 +01006234 // Check for flat ASCII string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006235 Label non_ascii_flat;
6236 __ tst(r1, Operand(kStringEncodingMask));
6237 STATIC_ASSERT(kTwoByteStringTag == 0);
6238 __ b(eq, &non_ascii_flat);
6239
6240 Label result_longer_than_two;
6241 __ cmp(r2, Operand(2));
6242 __ b(gt, &result_longer_than_two);
6243
6244 // Sub string of length 2 requested.
6245 // Get the two characters forming the sub string.
6246 __ add(r5, r5, Operand(r3));
6247 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
6248 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
6249
6250 // Try to lookup two character string in symbol table.
6251 Label make_two_character_string;
6252 StringHelper::GenerateTwoCharacterSymbolTableProbe(
6253 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01006254 Counters* counters = masm->isolate()->counters();
6255 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006256 __ add(sp, sp, Operand(3 * kPointerSize));
6257 __ Ret();
6258
6259 // r2: result string length.
6260 // r3: two characters combined into halfword in little endian byte order.
6261 __ bind(&make_two_character_string);
6262 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
6263 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01006264 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006265 __ add(sp, sp, Operand(3 * kPointerSize));
6266 __ Ret();
6267
6268 __ bind(&result_longer_than_two);
6269
6270 // Allocate the result.
6271 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
6272
6273 // r0: result string.
6274 // r2: result string length.
6275 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006276 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006277 // Locate first character of result.
6278 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6279 // Locate 'from' character of string.
6280 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006281 __ add(r5, r5, Operand(from, ASR, 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006282
6283 // r0: result string.
6284 // r1: first character of result string.
6285 // r2: result string length.
6286 // r5: first character of sub string to copy.
6287 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
6288 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
6289 COPY_ASCII | DEST_ALWAYS_ALIGNED);
Steve Block44f0eee2011-05-26 01:26:41 +01006290 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006291 __ add(sp, sp, Operand(3 * kPointerSize));
6292 __ Ret();
6293
6294 __ bind(&non_ascii_flat);
6295 // r2: result string length.
6296 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006297 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006298 // Check for flat two byte string.
6299
6300 // Allocate the result.
6301 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
6302
6303 // r0: result string.
6304 // r2: result string length.
6305 // r5: string.
6306 // Locate first character of result.
6307 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6308 // Locate 'from' character of string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006309 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006310 // As "from" is a smi it is 2 times the value which matches the size of a two
6311 // byte character.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006312 __ add(r5, r5, Operand(from));
6313 from = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006314
6315 // r0: result string.
6316 // r1: first character of result.
6317 // r2: result length.
6318 // r5: first character of string to copy.
6319 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006320 StringHelper::GenerateCopyCharactersLong(
6321 masm, r1, r5, r2, r3, r4, r6, r7, r9, DEST_ALWAYS_ALIGNED);
Steve Block44f0eee2011-05-26 01:26:41 +01006322 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006323 __ add(sp, sp, Operand(3 * kPointerSize));
6324 __ Ret();
6325
6326 // Just jump to runtime to create the sub string.
6327 __ bind(&runtime);
6328 __ TailCallRuntime(Runtime::kSubString, 3, 1);
6329}
6330
6331
6332void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
6333 Register left,
6334 Register right,
6335 Register scratch1,
6336 Register scratch2,
6337 Register scratch3,
6338 Register scratch4) {
6339 Label compare_lengths;
6340 // Find minimum length and length difference.
6341 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
6342 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
6343 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
6344 Register length_delta = scratch3;
6345 __ mov(scratch1, scratch2, LeaveCC, gt);
6346 Register min_length = scratch1;
6347 STATIC_ASSERT(kSmiTag == 0);
6348 __ tst(min_length, Operand(min_length));
6349 __ b(eq, &compare_lengths);
6350
6351 // Untag smi.
6352 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
6353
6354 // Setup registers so that we only need to increment one register
6355 // in the loop.
6356 __ add(scratch2, min_length,
6357 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6358 __ add(left, left, Operand(scratch2));
6359 __ add(right, right, Operand(scratch2));
6360 // Registers left and right points to the min_length character of strings.
6361 __ rsb(min_length, min_length, Operand(-1));
6362 Register index = min_length;
6363 // Index starts at -min_length.
6364
6365 {
6366 // Compare loop.
6367 Label loop;
6368 __ bind(&loop);
6369 // Compare characters.
6370 __ add(index, index, Operand(1), SetCC);
6371 __ ldrb(scratch2, MemOperand(left, index), ne);
6372 __ ldrb(scratch4, MemOperand(right, index), ne);
6373 // Skip to compare lengths with eq condition true.
6374 __ b(eq, &compare_lengths);
6375 __ cmp(scratch2, scratch4);
6376 __ b(eq, &loop);
6377 // Fallthrough with eq condition false.
6378 }
6379 // Compare lengths - strings up to min-length are equal.
6380 __ bind(&compare_lengths);
6381 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
6382 // Use zero length_delta as result.
6383 __ mov(r0, Operand(length_delta), SetCC, eq);
6384 // Fall through to here if characters compare not-equal.
6385 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
6386 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
6387 __ Ret();
6388}
6389
6390
6391void StringCompareStub::Generate(MacroAssembler* masm) {
6392 Label runtime;
6393
Steve Block44f0eee2011-05-26 01:26:41 +01006394 Counters* counters = masm->isolate()->counters();
6395
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006396 // Stack frame on entry.
6397 // sp[0]: right string
6398 // sp[4]: left string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006399 __ Ldrd(r0 , r1, MemOperand(sp)); // Load right in r0, left in r1.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006400
6401 Label not_same;
6402 __ cmp(r0, r1);
6403 __ b(ne, &not_same);
6404 STATIC_ASSERT(EQUAL == 0);
6405 STATIC_ASSERT(kSmiTag == 0);
6406 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01006407 __ IncrementCounter(counters->string_compare_native(), 1, r1, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006408 __ add(sp, sp, Operand(2 * kPointerSize));
6409 __ Ret();
6410
6411 __ bind(&not_same);
6412
Steve Block44f0eee2011-05-26 01:26:41 +01006413 // Check that both objects are sequential ASCII strings.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006414 __ JumpIfNotBothSequentialAsciiStrings(r1, r0, r2, r3, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006415
Steve Block44f0eee2011-05-26 01:26:41 +01006416 // Compare flat ASCII strings natively. Remove arguments from stack first.
6417 __ IncrementCounter(counters->string_compare_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006418 __ add(sp, sp, Operand(2 * kPointerSize));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006419 GenerateCompareFlatAsciiStrings(masm, r1, r0, r2, r3, r4, r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006420
6421 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
6422 // tagged as a small integer.
6423 __ bind(&runtime);
6424 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
6425}
6426
6427
6428void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006429 Label string_add_runtime, call_builtin;
6430 Builtins::JavaScript builtin_id = Builtins::ADD;
6431
Steve Block44f0eee2011-05-26 01:26:41 +01006432 Counters* counters = masm->isolate()->counters();
6433
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006434 // Stack on entry:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006435 // sp[0]: second argument (right).
6436 // sp[4]: first argument (left).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006437
6438 // Load the two arguments.
6439 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
6440 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
6441
6442 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006443 if (flags_ == NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006444 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
6445 // Load instance types.
6446 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
6447 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
6448 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6449 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
6450 STATIC_ASSERT(kStringTag == 0);
6451 // If either is not a string, go to runtime.
6452 __ tst(r4, Operand(kIsNotStringMask));
6453 __ tst(r5, Operand(kIsNotStringMask), eq);
6454 __ b(ne, &string_add_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006455 } else {
6456 // Here at least one of the arguments is definitely a string.
6457 // We convert the one that is not known to be a string.
6458 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
6459 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
6460 GenerateConvertArgument(
6461 masm, 1 * kPointerSize, r0, r2, r3, r4, r5, &call_builtin);
6462 builtin_id = Builtins::STRING_ADD_RIGHT;
6463 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
6464 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
6465 GenerateConvertArgument(
6466 masm, 0 * kPointerSize, r1, r2, r3, r4, r5, &call_builtin);
6467 builtin_id = Builtins::STRING_ADD_LEFT;
6468 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006469 }
6470
6471 // Both arguments are strings.
6472 // r0: first string
6473 // r1: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006474 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6475 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006476 {
6477 Label strings_not_empty;
6478 // Check if either of the strings are empty. In that case return the other.
6479 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
6480 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
6481 STATIC_ASSERT(kSmiTag == 0);
6482 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
6483 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
6484 STATIC_ASSERT(kSmiTag == 0);
6485 // Else test if second string is empty.
6486 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
6487 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
6488
Steve Block44f0eee2011-05-26 01:26:41 +01006489 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006490 __ add(sp, sp, Operand(2 * kPointerSize));
6491 __ Ret();
6492
6493 __ bind(&strings_not_empty);
6494 }
6495
6496 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
6497 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
6498 // Both strings are non-empty.
6499 // r0: first string
6500 // r1: second string
6501 // r2: length of first string
6502 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006503 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6504 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006505 // Look at the length of the result of adding the two strings.
6506 Label string_add_flat_result, longer_than_two;
6507 // Adding two lengths can't overflow.
6508 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
6509 __ add(r6, r2, Operand(r3));
Steve Block44f0eee2011-05-26 01:26:41 +01006510 // Use the symbol table when adding two one character strings, as it
6511 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006512 __ cmp(r6, Operand(2));
6513 __ b(ne, &longer_than_two);
6514
Steve Block44f0eee2011-05-26 01:26:41 +01006515 // Check that both strings are non-external ASCII strings.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006516 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006517 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
6518 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
6519 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6520 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
6521 }
6522 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
6523 &string_add_runtime);
6524
6525 // Get the two characters forming the sub string.
6526 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
6527 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
6528
6529 // Try to lookup two character string in symbol table. If it is not found
6530 // just allocate a new one.
6531 Label make_two_character_string;
6532 StringHelper::GenerateTwoCharacterSymbolTableProbe(
6533 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01006534 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006535 __ add(sp, sp, Operand(2 * kPointerSize));
6536 __ Ret();
6537
6538 __ bind(&make_two_character_string);
6539 // Resulting string has length 2 and first chars of two strings
6540 // are combined into single halfword in r2 register.
6541 // So we can fill resulting string without two loops by a single
6542 // halfword store instruction (which assumes that processor is
6543 // in a little endian mode)
6544 __ mov(r6, Operand(2));
6545 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
6546 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01006547 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006548 __ add(sp, sp, Operand(2 * kPointerSize));
6549 __ Ret();
6550
6551 __ bind(&longer_than_two);
6552 // Check if resulting string will be flat.
6553 __ cmp(r6, Operand(String::kMinNonFlatLength));
6554 __ b(lt, &string_add_flat_result);
6555 // Handle exceptionally long strings in the runtime system.
6556 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
6557 ASSERT(IsPowerOf2(String::kMaxLength + 1));
6558 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
6559 __ cmp(r6, Operand(String::kMaxLength + 1));
6560 __ b(hs, &string_add_runtime);
6561
6562 // If result is not supposed to be flat, allocate a cons string object.
Steve Block44f0eee2011-05-26 01:26:41 +01006563 // If both strings are ASCII the result is an ASCII cons string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006564 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006565 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
6566 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
6567 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6568 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
6569 }
6570 Label non_ascii, allocated, ascii_data;
6571 STATIC_ASSERT(kTwoByteStringTag == 0);
6572 __ tst(r4, Operand(kStringEncodingMask));
6573 __ tst(r5, Operand(kStringEncodingMask), ne);
6574 __ b(eq, &non_ascii);
6575
6576 // Allocate an ASCII cons string.
6577 __ bind(&ascii_data);
6578 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
6579 __ bind(&allocated);
6580 // Fill the fields of the cons string.
6581 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
6582 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
6583 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01006584 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006585 __ add(sp, sp, Operand(2 * kPointerSize));
6586 __ Ret();
6587
6588 __ bind(&non_ascii);
6589 // At least one of the strings is two-byte. Check whether it happens
Steve Block44f0eee2011-05-26 01:26:41 +01006590 // to contain only ASCII characters.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006591 // r4: first instance type.
6592 // r5: second instance type.
6593 __ tst(r4, Operand(kAsciiDataHintMask));
6594 __ tst(r5, Operand(kAsciiDataHintMask), ne);
6595 __ b(ne, &ascii_data);
6596 __ eor(r4, r4, Operand(r5));
6597 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
6598 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
6599 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
6600 __ b(eq, &ascii_data);
6601
6602 // Allocate a two byte cons string.
6603 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
6604 __ jmp(&allocated);
6605
6606 // Handle creating a flat result. First check that both strings are
6607 // sequential and that they have the same encoding.
6608 // r0: first string
6609 // r1: second string
6610 // r2: length of first string
6611 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006612 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6613 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006614 // r6: sum of lengths.
6615 __ bind(&string_add_flat_result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006616 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006617 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
6618 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
6619 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6620 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
6621 }
6622 // Check that both strings are sequential.
6623 STATIC_ASSERT(kSeqStringTag == 0);
6624 __ tst(r4, Operand(kStringRepresentationMask));
6625 __ tst(r5, Operand(kStringRepresentationMask), eq);
6626 __ b(ne, &string_add_runtime);
6627 // Now check if both strings have the same encoding (ASCII/Two-byte).
6628 // r0: first string.
6629 // r1: second string.
6630 // r2: length of first string.
6631 // r3: length of second string.
6632 // r6: sum of lengths..
6633 Label non_ascii_string_add_flat_result;
6634 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
6635 __ eor(r7, r4, Operand(r5));
6636 __ tst(r7, Operand(kStringEncodingMask));
6637 __ b(ne, &string_add_runtime);
6638 // And see if it's ASCII or two-byte.
6639 __ tst(r4, Operand(kStringEncodingMask));
6640 __ b(eq, &non_ascii_string_add_flat_result);
6641
6642 // Both strings are sequential ASCII strings. We also know that they are
6643 // short (since the sum of the lengths is less than kMinNonFlatLength).
6644 // r6: length of resulting flat string
6645 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
6646 // Locate first character of result.
6647 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6648 // Locate first character of first argument.
6649 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6650 // r0: first character of first string.
6651 // r1: second string.
6652 // r2: length of first string.
6653 // r3: length of second string.
6654 // r6: first character of result.
6655 // r7: result string.
6656 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
6657
6658 // Load second argument and locate first character.
6659 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6660 // r1: first character of second string.
6661 // r3: length of second string.
6662 // r6: next character of result.
6663 // r7: result string.
6664 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
6665 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01006666 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006667 __ add(sp, sp, Operand(2 * kPointerSize));
6668 __ Ret();
6669
6670 __ bind(&non_ascii_string_add_flat_result);
6671 // Both strings are sequential two byte strings.
6672 // r0: first string.
6673 // r1: second string.
6674 // r2: length of first string.
6675 // r3: length of second string.
6676 // r6: sum of length of strings.
6677 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
6678 // r0: first string.
6679 // r1: second string.
6680 // r2: length of first string.
6681 // r3: length of second string.
6682 // r7: result string.
6683
6684 // Locate first character of result.
6685 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6686 // Locate first character of first argument.
6687 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6688
6689 // r0: first character of first string.
6690 // r1: second string.
6691 // r2: length of first string.
6692 // r3: length of second string.
6693 // r6: first character of result.
6694 // r7: result string.
6695 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
6696
6697 // Locate first character of second argument.
6698 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6699
6700 // r1: first character of second string.
6701 // r3: length of second string.
6702 // r6: next character of result (after copy of first string).
6703 // r7: result string.
6704 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
6705
6706 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01006707 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006708 __ add(sp, sp, Operand(2 * kPointerSize));
6709 __ Ret();
6710
6711 // Just jump to runtime to add the two strings.
6712 __ bind(&string_add_runtime);
6713 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006714
6715 if (call_builtin.is_linked()) {
6716 __ bind(&call_builtin);
6717 __ InvokeBuiltin(builtin_id, JUMP_JS);
6718 }
6719}
6720
6721
6722void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
6723 int stack_offset,
6724 Register arg,
6725 Register scratch1,
6726 Register scratch2,
6727 Register scratch3,
6728 Register scratch4,
6729 Label* slow) {
6730 // First check if the argument is already a string.
6731 Label not_string, done;
6732 __ JumpIfSmi(arg, &not_string);
6733 __ CompareObjectType(arg, scratch1, scratch1, FIRST_NONSTRING_TYPE);
6734 __ b(lt, &done);
6735
6736 // Check the number to string cache.
6737 Label not_cached;
6738 __ bind(&not_string);
6739 // Puts the cached result into scratch1.
6740 NumberToStringStub::GenerateLookupNumberStringCache(masm,
6741 arg,
6742 scratch1,
6743 scratch2,
6744 scratch3,
6745 scratch4,
6746 false,
6747 &not_cached);
6748 __ mov(arg, scratch1);
6749 __ str(arg, MemOperand(sp, stack_offset));
6750 __ jmp(&done);
6751
6752 // Check if the argument is a safe string wrapper.
6753 __ bind(&not_cached);
6754 __ JumpIfSmi(arg, slow);
6755 __ CompareObjectType(
6756 arg, scratch1, scratch2, JS_VALUE_TYPE); // map -> scratch1.
6757 __ b(ne, slow);
6758 __ ldrb(scratch2, FieldMemOperand(scratch1, Map::kBitField2Offset));
6759 __ and_(scratch2,
6760 scratch2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
6761 __ cmp(scratch2,
6762 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
6763 __ b(ne, slow);
6764 __ ldr(arg, FieldMemOperand(arg, JSValue::kValueOffset));
6765 __ str(arg, MemOperand(sp, stack_offset));
6766
6767 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006768}
6769
6770
Ben Murdochb0fe1622011-05-05 13:52:32 +01006771void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
6772 ASSERT(state_ == CompareIC::SMIS);
6773 Label miss;
6774 __ orr(r2, r1, r0);
6775 __ tst(r2, Operand(kSmiTagMask));
6776 __ b(ne, &miss);
6777
6778 if (GetCondition() == eq) {
6779 // For equality we do not care about the sign of the result.
6780 __ sub(r0, r0, r1, SetCC);
6781 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01006782 // Untag before subtracting to avoid handling overflow.
6783 __ SmiUntag(r1);
6784 __ sub(r0, r1, SmiUntagOperand(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01006785 }
6786 __ Ret();
6787
6788 __ bind(&miss);
6789 GenerateMiss(masm);
6790}
6791
6792
6793void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
6794 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
6795
6796 Label generic_stub;
6797 Label unordered;
6798 Label miss;
6799 __ and_(r2, r1, Operand(r0));
6800 __ tst(r2, Operand(kSmiTagMask));
6801 __ b(eq, &generic_stub);
6802
6803 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
6804 __ b(ne, &miss);
6805 __ CompareObjectType(r1, r2, r2, HEAP_NUMBER_TYPE);
6806 __ b(ne, &miss);
6807
6808 // Inlining the double comparison and falling back to the general compare
6809 // stub if NaN is involved or VFP3 is unsupported.
Steve Block44f0eee2011-05-26 01:26:41 +01006810 if (Isolate::Current()->cpu_features()->IsSupported(VFP3)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006811 CpuFeatures::Scope scope(VFP3);
6812
6813 // Load left and right operand
6814 __ sub(r2, r1, Operand(kHeapObjectTag));
6815 __ vldr(d0, r2, HeapNumber::kValueOffset);
6816 __ sub(r2, r0, Operand(kHeapObjectTag));
6817 __ vldr(d1, r2, HeapNumber::kValueOffset);
6818
6819 // Compare operands
Ben Murdochb8e0da22011-05-16 14:20:40 +01006820 __ VFPCompareAndSetFlags(d0, d1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006821
6822 // Don't base result on status bits when a NaN is involved.
6823 __ b(vs, &unordered);
6824
6825 // Return a result of -1, 0, or 1, based on status bits.
6826 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
6827 __ mov(r0, Operand(LESS), LeaveCC, lt);
6828 __ mov(r0, Operand(GREATER), LeaveCC, gt);
6829 __ Ret();
6830
6831 __ bind(&unordered);
6832 }
6833
6834 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS, r1, r0);
6835 __ bind(&generic_stub);
6836 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
6837
6838 __ bind(&miss);
6839 GenerateMiss(masm);
6840}
6841
6842
6843void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6844 ASSERT(state_ == CompareIC::OBJECTS);
6845 Label miss;
6846 __ and_(r2, r1, Operand(r0));
6847 __ tst(r2, Operand(kSmiTagMask));
6848 __ b(eq, &miss);
6849
6850 __ CompareObjectType(r0, r2, r2, JS_OBJECT_TYPE);
6851 __ b(ne, &miss);
6852 __ CompareObjectType(r1, r2, r2, JS_OBJECT_TYPE);
6853 __ b(ne, &miss);
6854
6855 ASSERT(GetCondition() == eq);
6856 __ sub(r0, r0, Operand(r1));
6857 __ Ret();
6858
6859 __ bind(&miss);
6860 GenerateMiss(masm);
6861}
6862
6863
6864void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6865 __ Push(r1, r0);
6866 __ push(lr);
6867
6868 // Call the runtime system in a fresh internal frame.
Steve Block44f0eee2011-05-26 01:26:41 +01006869 ExternalReference miss =
6870 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01006871 __ EnterInternalFrame();
6872 __ Push(r1, r0);
6873 __ mov(ip, Operand(Smi::FromInt(op_)));
6874 __ push(ip);
6875 __ CallExternalReference(miss, 3);
6876 __ LeaveInternalFrame();
6877 // Compute the entry point of the rewritten stub.
6878 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
6879 // Restore registers.
6880 __ pop(lr);
6881 __ pop(r0);
6882 __ pop(r1);
6883 __ Jump(r2);
6884}
6885
6886
Steve Block1e0659c2011-05-24 12:43:12 +01006887void DirectCEntryStub::Generate(MacroAssembler* masm) {
6888 __ ldr(pc, MemOperand(sp, 0));
6889}
6890
6891
6892void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006893 ExternalReference function) {
6894 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
6895 RelocInfo::CODE_TARGET));
6896 __ mov(r2, Operand(function));
6897 // Push return address (accessible to GC through exit frame pc).
6898 __ str(pc, MemOperand(sp, 0));
6899 __ Jump(r2); // Call the api function.
6900}
6901
6902
6903void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
6904 Register target) {
Steve Block1e0659c2011-05-24 12:43:12 +01006905 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
6906 RelocInfo::CODE_TARGET));
6907 // Push return address (accessible to GC through exit frame pc).
Steve Block1e0659c2011-05-24 12:43:12 +01006908 __ str(pc, MemOperand(sp, 0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006909 __ Jump(target); // Call the C++ function.
Steve Block1e0659c2011-05-24 12:43:12 +01006910}
6911
6912
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006913#undef __
6914
6915} } // namespace v8::internal
6916
6917#endif // V8_TARGET_ARCH_ARM