blob: d66daea0a0aa7f751f3854a72a91ac01eb48fa0c [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) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100311 Register exponent = result1_;
312 Register mantissa = result2_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100313
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100314 Label not_special;
315 // Convert from Smi to integer.
316 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
317 // Move sign bit from source to destination. This works because the sign bit
318 // in the exponent word of the double has the same position and polarity as
319 // the 2's complement sign bit in a Smi.
320 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
321 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
322 // Subtract from 0 if source was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100323 __ rsb(source_, source_, Operand(0, RelocInfo::NONE), LeaveCC, ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100324
325 // We have -1, 0 or 1, which we treat specially. Register source_ contains
326 // absolute value: it is either equal to 1 (special case of -1 and 1),
327 // greater than 1 (not a special case) or less than 1 (special case of 0).
328 __ cmp(source_, Operand(1));
329 __ b(gt, &not_special);
330
331 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
332 static const uint32_t exponent_word_for_1 =
333 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
334 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
335 // 1, 0 and -1 all have 0 for the second word.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100336 __ mov(mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100337 __ Ret();
338
339 __ bind(&not_special);
340 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
341 // Gets the wrong answer for 0, but we already checked for that case above.
342 __ CountLeadingZeros(zeros_, source_, mantissa);
343 // Compute exponent and or it into the exponent register.
344 // We use mantissa as a scratch register here. Use a fudge factor to
345 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
346 // that fit in the ARM's constant field.
347 int fudge = 0x400;
348 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
349 __ add(mantissa, mantissa, Operand(fudge));
350 __ orr(exponent,
351 exponent,
352 Operand(mantissa, LSL, HeapNumber::kExponentShift));
353 // Shift up the source chopping the top bit off.
354 __ add(zeros_, zeros_, Operand(1));
355 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
356 __ mov(source_, Operand(source_, LSL, zeros_));
357 // Compute lower part of fraction (last 12 bits).
358 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
359 // And the top (top 20 bits).
360 __ orr(exponent,
361 exponent,
362 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
363 __ Ret();
364}
365
366
Steve Block1e0659c2011-05-24 12:43:12 +0100367class FloatingPointHelper : public AllStatic {
368 public:
369
370 enum Destination {
371 kVFPRegisters,
372 kCoreRegisters
373 };
374
375
376 // Loads smis from r0 and r1 (right and left in binary operations) into
377 // floating point registers. Depending on the destination the values ends up
378 // either d7 and d6 or in r2/r3 and r0/r1 respectively. If the destination is
379 // floating point registers VFP3 must be supported. If core registers are
380 // requested when VFP3 is supported d6 and d7 will be scratched.
381 static void LoadSmis(MacroAssembler* masm,
382 Destination destination,
383 Register scratch1,
384 Register scratch2);
385
386 // Loads objects from r0 and r1 (right and left in binary operations) into
387 // floating point registers. Depending on the destination the values ends up
388 // either d7 and d6 or in r2/r3 and r0/r1 respectively. If the destination is
389 // floating point registers VFP3 must be supported. If core registers are
390 // requested when VFP3 is supported d6 and d7 will still be scratched. If
391 // either r0 or r1 is not a number (not smi and not heap number object) the
392 // not_number label is jumped to with r0 and r1 intact.
393 static void LoadOperands(MacroAssembler* masm,
394 FloatingPointHelper::Destination destination,
395 Register heap_number_map,
396 Register scratch1,
397 Register scratch2,
398 Label* not_number);
399
Steve Block44f0eee2011-05-26 01:26:41 +0100400 // Convert the smi or heap number in object to an int32 using the rules
401 // for ToInt32 as described in ECMAScript 9.5.: the value is truncated
402 // and brought into the range -2^31 .. +2^31 - 1.
403 static void ConvertNumberToInt32(MacroAssembler* masm,
404 Register object,
405 Register dst,
406 Register heap_number_map,
407 Register scratch1,
408 Register scratch2,
409 Register scratch3,
410 DwVfpRegister double_scratch,
411 Label* not_int32);
Steve Block1e0659c2011-05-24 12:43:12 +0100412
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100413 // Load the number from object into double_dst in the double format.
414 // Control will jump to not_int32 if the value cannot be exactly represented
415 // by a 32-bit integer.
416 // Floating point value in the 32-bit integer range that are not exact integer
417 // won't be loaded.
418 static void LoadNumberAsInt32Double(MacroAssembler* masm,
419 Register object,
420 Destination destination,
421 DwVfpRegister double_dst,
422 Register dst1,
423 Register dst2,
424 Register heap_number_map,
425 Register scratch1,
426 Register scratch2,
427 SwVfpRegister single_scratch,
428 Label* not_int32);
429
430 // Loads the number from object into dst as a 32-bit integer.
431 // Control will jump to not_int32 if the object cannot be exactly represented
432 // by a 32-bit integer.
433 // Floating point value in the 32-bit integer range that are not exact integer
434 // won't be converted.
435 // scratch3 is not used when VFP3 is supported.
436 static void LoadNumberAsInt32(MacroAssembler* masm,
437 Register object,
438 Register dst,
439 Register heap_number_map,
440 Register scratch1,
441 Register scratch2,
442 Register scratch3,
443 DwVfpRegister double_scratch,
444 Label* not_int32);
445
446 // Generate non VFP3 code to check if a double can be exactly represented by a
447 // 32-bit integer. This does not check for 0 or -0, which need
448 // to be checked for separately.
449 // Control jumps to not_int32 if the value is not a 32-bit integer, and falls
450 // through otherwise.
451 // src1 and src2 will be cloberred.
452 //
453 // Expected input:
454 // - src1: higher (exponent) part of the double value.
455 // - src2: lower (mantissa) part of the double value.
456 // Output status:
457 // - dst: 32 higher bits of the mantissa. (mantissa[51:20])
458 // - src2: contains 1.
459 // - other registers are clobbered.
460 static void DoubleIs32BitInteger(MacroAssembler* masm,
461 Register src1,
462 Register src2,
463 Register dst,
464 Register scratch,
465 Label* not_int32);
466
467 // Generates code to call a C function to do a double operation using core
468 // registers. (Used when VFP3 is not supported.)
469 // This code never falls through, but returns with a heap number containing
470 // the result in r0.
471 // Register heapnumber_result must be a heap number in which the
472 // result of the operation will be stored.
473 // Requires the following layout on entry:
474 // r0: Left value (least significant part of mantissa).
475 // r1: Left value (sign, exponent, top of mantissa).
476 // r2: Right value (least significant part of mantissa).
477 // r3: Right value (sign, exponent, top of mantissa).
478 static void CallCCodeForDoubleOperation(MacroAssembler* masm,
479 Token::Value op,
480 Register heap_number_result,
481 Register scratch);
482
Steve Block1e0659c2011-05-24 12:43:12 +0100483 private:
484 static void LoadNumber(MacroAssembler* masm,
485 FloatingPointHelper::Destination destination,
486 Register object,
487 DwVfpRegister dst,
488 Register dst1,
489 Register dst2,
490 Register heap_number_map,
491 Register scratch1,
492 Register scratch2,
493 Label* not_number);
494};
495
496
497void FloatingPointHelper::LoadSmis(MacroAssembler* masm,
498 FloatingPointHelper::Destination destination,
499 Register scratch1,
500 Register scratch2) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100501 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100502 CpuFeatures::Scope scope(VFP3);
503 __ mov(scratch1, Operand(r0, ASR, kSmiTagSize));
504 __ vmov(d7.high(), scratch1);
505 __ vcvt_f64_s32(d7, d7.high());
506 __ mov(scratch1, Operand(r1, ASR, kSmiTagSize));
507 __ vmov(d6.high(), scratch1);
508 __ vcvt_f64_s32(d6, d6.high());
509 if (destination == kCoreRegisters) {
510 __ vmov(r2, r3, d7);
511 __ vmov(r0, r1, d6);
512 }
513 } else {
514 ASSERT(destination == kCoreRegisters);
515 // Write Smi from r0 to r3 and r2 in double format.
516 __ mov(scratch1, Operand(r0));
517 ConvertToDoubleStub stub1(r3, r2, scratch1, scratch2);
518 __ push(lr);
519 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100520 // Write Smi from r1 to r1 and r0 in double format.
Steve Block1e0659c2011-05-24 12:43:12 +0100521 __ mov(scratch1, Operand(r1));
522 ConvertToDoubleStub stub2(r1, r0, scratch1, scratch2);
523 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
524 __ pop(lr);
525 }
526}
527
528
529void FloatingPointHelper::LoadOperands(
530 MacroAssembler* masm,
531 FloatingPointHelper::Destination destination,
532 Register heap_number_map,
533 Register scratch1,
534 Register scratch2,
535 Label* slow) {
536
537 // Load right operand (r0) to d6 or r2/r3.
538 LoadNumber(masm, destination,
539 r0, d7, r2, r3, heap_number_map, scratch1, scratch2, slow);
540
541 // Load left operand (r1) to d7 or r0/r1.
542 LoadNumber(masm, destination,
543 r1, d6, r0, r1, heap_number_map, scratch1, scratch2, slow);
544}
545
546
547void FloatingPointHelper::LoadNumber(MacroAssembler* masm,
548 Destination destination,
549 Register object,
550 DwVfpRegister dst,
551 Register dst1,
552 Register dst2,
553 Register heap_number_map,
554 Register scratch1,
555 Register scratch2,
556 Label* not_number) {
557 if (FLAG_debug_code) {
558 __ AbortIfNotRootValue(heap_number_map,
559 Heap::kHeapNumberMapRootIndex,
560 "HeapNumberMap register clobbered.");
561 }
562
563 Label is_smi, done;
564
565 __ JumpIfSmi(object, &is_smi);
566 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_number);
567
568 // Handle loading a double from a heap number.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100569 if (CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100570 destination == kVFPRegisters) {
Steve Block1e0659c2011-05-24 12:43:12 +0100571 CpuFeatures::Scope scope(VFP3);
572 // Load the double from tagged HeapNumber to double register.
573 __ sub(scratch1, object, Operand(kHeapObjectTag));
574 __ vldr(dst, scratch1, HeapNumber::kValueOffset);
575 } else {
576 ASSERT(destination == kCoreRegisters);
577 // Load the double from heap number to dst1 and dst2 in double format.
578 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
579 }
580 __ jmp(&done);
581
582 // Handle loading a double from a smi.
583 __ bind(&is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100584 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100585 CpuFeatures::Scope scope(VFP3);
586 // Convert smi to double using VFP instructions.
587 __ SmiUntag(scratch1, object);
588 __ vmov(dst.high(), scratch1);
589 __ vcvt_f64_s32(dst, dst.high());
590 if (destination == kCoreRegisters) {
591 // Load the converted smi to dst1 and dst2 in double format.
592 __ vmov(dst1, dst2, dst);
593 }
594 } else {
595 ASSERT(destination == kCoreRegisters);
596 // Write smi to dst1 and dst2 double format.
597 __ mov(scratch1, Operand(object));
598 ConvertToDoubleStub stub(dst2, dst1, scratch1, scratch2);
599 __ push(lr);
600 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
601 __ pop(lr);
602 }
603
604 __ bind(&done);
605}
606
607
Steve Block44f0eee2011-05-26 01:26:41 +0100608void FloatingPointHelper::ConvertNumberToInt32(MacroAssembler* masm,
609 Register object,
610 Register dst,
611 Register heap_number_map,
612 Register scratch1,
613 Register scratch2,
614 Register scratch3,
615 DwVfpRegister double_scratch,
616 Label* not_number) {
Steve Block1e0659c2011-05-24 12:43:12 +0100617 if (FLAG_debug_code) {
618 __ AbortIfNotRootValue(heap_number_map,
619 Heap::kHeapNumberMapRootIndex,
620 "HeapNumberMap register clobbered.");
621 }
Steve Block44f0eee2011-05-26 01:26:41 +0100622 Label is_smi;
623 Label done;
624 Label not_in_int32_range;
625
Steve Block1e0659c2011-05-24 12:43:12 +0100626 __ JumpIfSmi(object, &is_smi);
627 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kMapOffset));
628 __ cmp(scratch1, heap_number_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100629 __ b(ne, not_number);
630 __ ConvertToInt32(object,
631 dst,
632 scratch1,
633 scratch2,
634 double_scratch,
635 &not_in_int32_range);
Steve Block1e0659c2011-05-24 12:43:12 +0100636 __ jmp(&done);
Steve Block44f0eee2011-05-26 01:26:41 +0100637
638 __ bind(&not_in_int32_range);
639 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
640 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
641
642 __ EmitOutOfInt32RangeTruncate(dst,
643 scratch1,
644 scratch2,
645 scratch3);
646 __ jmp(&done);
647
Steve Block1e0659c2011-05-24 12:43:12 +0100648 __ bind(&is_smi);
649 __ SmiUntag(dst, object);
650 __ bind(&done);
651}
652
653
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100654void FloatingPointHelper::LoadNumberAsInt32Double(MacroAssembler* masm,
655 Register object,
656 Destination destination,
657 DwVfpRegister double_dst,
658 Register dst1,
659 Register dst2,
660 Register heap_number_map,
661 Register scratch1,
662 Register scratch2,
663 SwVfpRegister single_scratch,
664 Label* not_int32) {
665 ASSERT(!scratch1.is(object) && !scratch2.is(object));
666 ASSERT(!scratch1.is(scratch2));
667 ASSERT(!heap_number_map.is(object) &&
668 !heap_number_map.is(scratch1) &&
669 !heap_number_map.is(scratch2));
670
671 Label done, obj_is_not_smi;
672
673 __ JumpIfNotSmi(object, &obj_is_not_smi);
674 __ SmiUntag(scratch1, object);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100675 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100676 CpuFeatures::Scope scope(VFP3);
677 __ vmov(single_scratch, scratch1);
678 __ vcvt_f64_s32(double_dst, single_scratch);
679 if (destination == kCoreRegisters) {
680 __ vmov(dst1, dst2, double_dst);
681 }
682 } else {
683 Label fewer_than_20_useful_bits;
684 // Expected output:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100685 // | dst2 | dst1 |
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100686 // | s | exp | mantissa |
687
688 // Check for zero.
689 __ cmp(scratch1, Operand(0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100690 __ mov(dst2, scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100691 __ mov(dst1, scratch1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100692 __ b(eq, &done);
693
694 // Preload the sign of the value.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100695 __ and_(dst2, scratch1, Operand(HeapNumber::kSignMask), SetCC);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100696 // Get the absolute value of the object (as an unsigned integer).
697 __ rsb(scratch1, scratch1, Operand(0), SetCC, mi);
698
699 // Get mantisssa[51:20].
700
701 // Get the position of the first set bit.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100702 __ CountLeadingZeros(dst1, scratch1, scratch2);
703 __ rsb(dst1, dst1, Operand(31));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100704
705 // Set the exponent.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100706 __ add(scratch2, dst1, Operand(HeapNumber::kExponentBias));
707 __ Bfi(dst2, scratch2, scratch2,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100708 HeapNumber::kExponentShift, HeapNumber::kExponentBits);
709
710 // Clear the first non null bit.
711 __ mov(scratch2, Operand(1));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100712 __ bic(scratch1, scratch1, Operand(scratch2, LSL, dst1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100713
Ben Murdoch8b112d22011-06-08 16:22:53 +0100714 __ cmp(dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100715 // Get the number of bits to set in the lower part of the mantissa.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100716 __ sub(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord), SetCC);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100717 __ b(mi, &fewer_than_20_useful_bits);
718 // Set the higher 20 bits of the mantissa.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100719 __ orr(dst2, dst2, Operand(scratch1, LSR, scratch2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100720 __ rsb(scratch2, scratch2, Operand(32));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100721 __ mov(dst1, Operand(scratch1, LSL, scratch2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100722 __ b(&done);
723
724 __ bind(&fewer_than_20_useful_bits);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100725 __ rsb(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100726 __ mov(scratch2, Operand(scratch1, LSL, scratch2));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100727 __ orr(dst2, dst2, scratch2);
728 // Set dst1 to 0.
729 __ mov(dst1, Operand(0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100730 }
731
732 __ b(&done);
733
734 __ bind(&obj_is_not_smi);
735 if (FLAG_debug_code) {
736 __ AbortIfNotRootValue(heap_number_map,
737 Heap::kHeapNumberMapRootIndex,
738 "HeapNumberMap register clobbered.");
739 }
740 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
741
742 // Load the number.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100743 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100744 CpuFeatures::Scope scope(VFP3);
745 // Load the double value.
746 __ sub(scratch1, object, Operand(kHeapObjectTag));
747 __ vldr(double_dst, scratch1, HeapNumber::kValueOffset);
748
749 __ EmitVFPTruncate(kRoundToZero,
750 single_scratch,
751 double_dst,
752 scratch1,
753 scratch2,
754 kCheckForInexactConversion);
755
756 // Jump to not_int32 if the operation did not succeed.
757 __ b(ne, not_int32);
758
759 if (destination == kCoreRegisters) {
760 __ vmov(dst1, dst2, double_dst);
761 }
762
763 } else {
764 ASSERT(!scratch1.is(object) && !scratch2.is(object));
765 // Load the double value in the destination registers..
766 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
767
768 // Check for 0 and -0.
769 __ bic(scratch1, dst1, Operand(HeapNumber::kSignMask));
770 __ orr(scratch1, scratch1, Operand(dst2));
771 __ cmp(scratch1, Operand(0));
772 __ b(eq, &done);
773
774 // Check that the value can be exactly represented by a 32-bit integer.
775 // Jump to not_int32 if that's not the case.
776 DoubleIs32BitInteger(masm, dst1, dst2, scratch1, scratch2, not_int32);
777
778 // dst1 and dst2 were trashed. Reload the double value.
779 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
780 }
781
782 __ bind(&done);
783}
784
785
786void FloatingPointHelper::LoadNumberAsInt32(MacroAssembler* masm,
787 Register object,
788 Register dst,
789 Register heap_number_map,
790 Register scratch1,
791 Register scratch2,
792 Register scratch3,
793 DwVfpRegister double_scratch,
794 Label* not_int32) {
795 ASSERT(!dst.is(object));
796 ASSERT(!scratch1.is(object) && !scratch2.is(object) && !scratch3.is(object));
797 ASSERT(!scratch1.is(scratch2) &&
798 !scratch1.is(scratch3) &&
799 !scratch2.is(scratch3));
800
801 Label done;
802
803 // Untag the object into the destination register.
804 __ SmiUntag(dst, object);
805 // Just return if the object is a smi.
806 __ JumpIfSmi(object, &done);
807
808 if (FLAG_debug_code) {
809 __ AbortIfNotRootValue(heap_number_map,
810 Heap::kHeapNumberMapRootIndex,
811 "HeapNumberMap register clobbered.");
812 }
813 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
814
815 // Object is a heap number.
816 // Convert the floating point value to a 32-bit integer.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100817 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100818 CpuFeatures::Scope scope(VFP3);
819 SwVfpRegister single_scratch = double_scratch.low();
820 // Load the double value.
821 __ sub(scratch1, object, Operand(kHeapObjectTag));
822 __ vldr(double_scratch, scratch1, HeapNumber::kValueOffset);
823
824 __ EmitVFPTruncate(kRoundToZero,
825 single_scratch,
826 double_scratch,
827 scratch1,
828 scratch2,
829 kCheckForInexactConversion);
830
831 // Jump to not_int32 if the operation did not succeed.
832 __ b(ne, not_int32);
833 // Get the result in the destination register.
834 __ vmov(dst, single_scratch);
835
836 } else {
837 // Load the double value in the destination registers.
838 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
839 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
840
841 // Check for 0 and -0.
842 __ bic(dst, scratch1, Operand(HeapNumber::kSignMask));
843 __ orr(dst, scratch2, Operand(dst));
844 __ cmp(dst, Operand(0));
845 __ b(eq, &done);
846
847 DoubleIs32BitInteger(masm, scratch1, scratch2, dst, scratch3, not_int32);
848
849 // Registers state after DoubleIs32BitInteger.
850 // dst: mantissa[51:20].
851 // scratch2: 1
852
853 // Shift back the higher bits of the mantissa.
854 __ mov(dst, Operand(dst, LSR, scratch3));
855 // Set the implicit first bit.
856 __ rsb(scratch3, scratch3, Operand(32));
857 __ orr(dst, dst, Operand(scratch2, LSL, scratch3));
858 // Set the sign.
859 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
860 __ tst(scratch1, Operand(HeapNumber::kSignMask));
861 __ rsb(dst, dst, Operand(0), LeaveCC, mi);
862 }
863
864 __ bind(&done);
865}
866
867
868void FloatingPointHelper::DoubleIs32BitInteger(MacroAssembler* masm,
869 Register src1,
870 Register src2,
871 Register dst,
872 Register scratch,
873 Label* not_int32) {
874 // Get exponent alone in scratch.
875 __ Ubfx(scratch,
876 src1,
877 HeapNumber::kExponentShift,
878 HeapNumber::kExponentBits);
879
880 // Substract the bias from the exponent.
881 __ sub(scratch, scratch, Operand(HeapNumber::kExponentBias), SetCC);
882
883 // src1: higher (exponent) part of the double value.
884 // src2: lower (mantissa) part of the double value.
885 // scratch: unbiased exponent.
886
887 // Fast cases. Check for obvious non 32-bit integer values.
888 // Negative exponent cannot yield 32-bit integers.
889 __ b(mi, not_int32);
890 // Exponent greater than 31 cannot yield 32-bit integers.
891 // Also, a positive value with an exponent equal to 31 is outside of the
892 // signed 32-bit integer range.
893 // Another way to put it is that if (exponent - signbit) > 30 then the
894 // number cannot be represented as an int32.
895 Register tmp = dst;
896 __ sub(tmp, scratch, Operand(src1, LSR, 31));
897 __ cmp(tmp, Operand(30));
898 __ b(gt, not_int32);
899 // - Bits [21:0] in the mantissa are not null.
900 __ tst(src2, Operand(0x3fffff));
901 __ b(ne, not_int32);
902
903 // Otherwise the exponent needs to be big enough to shift left all the
904 // non zero bits left. So we need the (30 - exponent) last bits of the
905 // 31 higher bits of the mantissa to be null.
906 // Because bits [21:0] are null, we can check instead that the
907 // (32 - exponent) last bits of the 32 higher bits of the mantisssa are null.
908
909 // Get the 32 higher bits of the mantissa in dst.
910 __ Ubfx(dst,
911 src2,
912 HeapNumber::kMantissaBitsInTopWord,
913 32 - HeapNumber::kMantissaBitsInTopWord);
914 __ orr(dst,
915 dst,
916 Operand(src1, LSL, HeapNumber::kNonMantissaBitsInTopWord));
917
918 // Create the mask and test the lower bits (of the higher bits).
919 __ rsb(scratch, scratch, Operand(32));
920 __ mov(src2, Operand(1));
921 __ mov(src1, Operand(src2, LSL, scratch));
922 __ sub(src1, src1, Operand(1));
923 __ tst(dst, src1);
924 __ b(ne, not_int32);
925}
926
927
928void FloatingPointHelper::CallCCodeForDoubleOperation(
929 MacroAssembler* masm,
930 Token::Value op,
931 Register heap_number_result,
932 Register scratch) {
933 // Using core registers:
934 // r0: Left value (least significant part of mantissa).
935 // r1: Left value (sign, exponent, top of mantissa).
936 // r2: Right value (least significant part of mantissa).
937 // r3: Right value (sign, exponent, top of mantissa).
938
939 // Assert that heap_number_result is callee-saved.
940 // We currently always use r5 to pass it.
941 ASSERT(heap_number_result.is(r5));
942
943 // Push the current return address before the C call. Return will be
944 // through pop(pc) below.
945 __ push(lr);
946 __ PrepareCallCFunction(4, scratch); // Two doubles are 4 arguments.
947 // Call C routine that may not cause GC or other trouble.
Steve Block44f0eee2011-05-26 01:26:41 +0100948 __ CallCFunction(ExternalReference::double_fp_operation(op, masm->isolate()),
949 4);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100950 // Store answer in the overwritable heap number. Double returned in
951 // registers r0 and r1.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100952 __ Strd(r0, r1, FieldMemOperand(heap_number_result,
953 HeapNumber::kValueOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100954 // Place heap_number_result in r0 and return to the pushed return address.
955 __ mov(r0, Operand(heap_number_result));
956 __ pop(pc);
957}
958
Steve Block1e0659c2011-05-24 12:43:12 +0100959
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100960// See comment for class.
961void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
962 Label max_negative_int;
963 // the_int_ has the answer which is a signed int32 but not a Smi.
964 // We test for the special value that has a different exponent. This test
965 // has the neat side effect of setting the flags according to the sign.
966 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
967 __ cmp(the_int_, Operand(0x80000000u));
968 __ b(eq, &max_negative_int);
969 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
970 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
971 uint32_t non_smi_exponent =
972 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
973 __ mov(scratch_, Operand(non_smi_exponent));
974 // Set the sign bit in scratch_ if the value was negative.
975 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
976 // Subtract from 0 if the value was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100977 __ rsb(the_int_, the_int_, Operand(0, RelocInfo::NONE), LeaveCC, cs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100978 // We should be masking the implict first digit of the mantissa away here,
979 // but it just ends up combining harmlessly with the last digit of the
980 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
981 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
982 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
983 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
984 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
985 __ str(scratch_, FieldMemOperand(the_heap_number_,
986 HeapNumber::kExponentOffset));
987 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
988 __ str(scratch_, FieldMemOperand(the_heap_number_,
989 HeapNumber::kMantissaOffset));
990 __ Ret();
991
992 __ bind(&max_negative_int);
993 // The max negative int32 is stored as a positive number in the mantissa of
994 // a double because it uses a sign bit instead of using two's complement.
995 // The actual mantissa bits stored are all 0 because the implicit most
996 // significant 1 bit is not stored.
997 non_smi_exponent += 1 << HeapNumber::kExponentShift;
998 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
999 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
Iain Merrick9ac36c92010-09-13 15:29:50 +01001000 __ mov(ip, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001001 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
1002 __ Ret();
1003}
1004
1005
1006// Handle the case where the lhs and rhs are the same object.
1007// Equality is almost reflexive (everything but NaN), so this is a test
1008// for "identity and not NaN".
1009static void EmitIdenticalObjectComparison(MacroAssembler* masm,
1010 Label* slow,
Steve Block1e0659c2011-05-24 12:43:12 +01001011 Condition cond,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001012 bool never_nan_nan) {
1013 Label not_identical;
1014 Label heap_number, return_equal;
1015 __ cmp(r0, r1);
1016 __ b(ne, &not_identical);
1017
1018 // The two objects are identical. If we know that one of them isn't NaN then
1019 // we now know they test equal.
Steve Block1e0659c2011-05-24 12:43:12 +01001020 if (cond != eq || !never_nan_nan) {
Steve Block44f0eee2011-05-26 01:26:41 +01001021 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001022 // so we do the second best thing - test it ourselves.
1023 // They are both equal and they are not both Smis so both of them are not
1024 // Smis. If it's not a heap number, then return equal.
Steve Block1e0659c2011-05-24 12:43:12 +01001025 if (cond == lt || cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001026 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
1027 __ b(ge, slow);
1028 } else {
1029 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
1030 __ b(eq, &heap_number);
1031 // Comparing JS objects with <=, >= is complicated.
Steve Block1e0659c2011-05-24 12:43:12 +01001032 if (cond != eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001033 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
1034 __ b(ge, slow);
1035 // Normally here we fall through to return_equal, but undefined is
1036 // special: (undefined == undefined) == true, but
1037 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
Steve Block1e0659c2011-05-24 12:43:12 +01001038 if (cond == le || cond == ge) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001039 __ cmp(r4, Operand(ODDBALL_TYPE));
1040 __ b(ne, &return_equal);
1041 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1042 __ cmp(r0, r2);
1043 __ b(ne, &return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +01001044 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001045 // undefined <= undefined should fail.
1046 __ mov(r0, Operand(GREATER));
1047 } else {
1048 // undefined >= undefined should fail.
1049 __ mov(r0, Operand(LESS));
1050 }
1051 __ Ret();
1052 }
1053 }
1054 }
1055 }
1056
1057 __ bind(&return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +01001058 if (cond == lt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001059 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
Steve Block1e0659c2011-05-24 12:43:12 +01001060 } else if (cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001061 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
1062 } else {
1063 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
1064 }
1065 __ Ret();
1066
Steve Block1e0659c2011-05-24 12:43:12 +01001067 if (cond != eq || !never_nan_nan) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001068 // For less and greater we don't have to check for NaN since the result of
1069 // x < x is false regardless. For the others here is some code to check
1070 // for NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001071 if (cond != lt && cond != gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001072 __ bind(&heap_number);
1073 // It is a heap number, so return non-equal if it's NaN and equal if it's
1074 // not NaN.
1075
1076 // The representation of NaN values has all exponent bits (52..62) set,
1077 // and not all mantissa bits (0..51) clear.
1078 // Read top bits of double representation (second word of value).
1079 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1080 // Test that exponent bits are all set.
1081 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
1082 // NaNs have all-one exponents so they sign extend to -1.
1083 __ cmp(r3, Operand(-1));
1084 __ b(ne, &return_equal);
1085
1086 // Shift out flag and all exponent bits, retaining only mantissa.
1087 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
1088 // Or with all low-bits of mantissa.
1089 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
1090 __ orr(r0, r3, Operand(r2), SetCC);
1091 // For equal we already have the right value in r0: Return zero (equal)
1092 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
1093 // not (it's a NaN). For <= and >= we need to load r0 with the failing
1094 // value if it's a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001095 if (cond != eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001096 // All-zero means Infinity means equal.
1097 __ Ret(eq);
Steve Block1e0659c2011-05-24 12:43:12 +01001098 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001099 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
1100 } else {
1101 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
1102 }
1103 }
1104 __ Ret();
1105 }
1106 // No fall through here.
1107 }
1108
1109 __ bind(&not_identical);
1110}
1111
1112
1113// See comment at call site.
1114static void EmitSmiNonsmiComparison(MacroAssembler* masm,
1115 Register lhs,
1116 Register rhs,
1117 Label* lhs_not_nan,
1118 Label* slow,
1119 bool strict) {
1120 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1121 (lhs.is(r1) && rhs.is(r0)));
1122
1123 Label rhs_is_smi;
1124 __ tst(rhs, Operand(kSmiTagMask));
1125 __ b(eq, &rhs_is_smi);
1126
1127 // Lhs is a Smi. Check whether the rhs is a heap number.
1128 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
1129 if (strict) {
1130 // If rhs is not a number and lhs is a Smi then strict equality cannot
1131 // succeed. Return non-equal
1132 // If rhs is r0 then there is already a non zero value in it.
1133 if (!rhs.is(r0)) {
1134 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1135 }
1136 __ Ret(ne);
1137 } else {
1138 // Smi compared non-strictly with a non-Smi non-heap-number. Call
1139 // the runtime.
1140 __ b(ne, slow);
1141 }
1142
1143 // Lhs is a smi, rhs is a number.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001144 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001145 // Convert lhs to a double in d7.
1146 CpuFeatures::Scope scope(VFP3);
1147 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
1148 // Load the double from rhs, tagged HeapNumber r0, to d6.
1149 __ sub(r7, rhs, Operand(kHeapObjectTag));
1150 __ vldr(d6, r7, HeapNumber::kValueOffset);
1151 } else {
1152 __ push(lr);
1153 // Convert lhs to a double in r2, r3.
1154 __ mov(r7, Operand(lhs));
1155 ConvertToDoubleStub stub1(r3, r2, r7, r6);
1156 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
1157 // Load rhs to a double in r0, r1.
1158 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1159 __ pop(lr);
1160 }
1161
1162 // We now have both loaded as doubles but we can skip the lhs nan check
1163 // since it's a smi.
1164 __ jmp(lhs_not_nan);
1165
1166 __ bind(&rhs_is_smi);
1167 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
1168 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
1169 if (strict) {
1170 // If lhs is not a number and rhs is a smi then strict equality cannot
1171 // succeed. Return non-equal.
1172 // If lhs is r0 then there is already a non zero value in it.
1173 if (!lhs.is(r0)) {
1174 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1175 }
1176 __ Ret(ne);
1177 } else {
1178 // Smi compared non-strictly with a non-smi non-heap-number. Call
1179 // the runtime.
1180 __ b(ne, slow);
1181 }
1182
1183 // Rhs is a smi, lhs is a heap number.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001184 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001185 CpuFeatures::Scope scope(VFP3);
1186 // Load the double from lhs, tagged HeapNumber r1, to d7.
1187 __ sub(r7, lhs, Operand(kHeapObjectTag));
1188 __ vldr(d7, r7, HeapNumber::kValueOffset);
1189 // Convert rhs to a double in d6 .
1190 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
1191 } else {
1192 __ push(lr);
1193 // Load lhs to a double in r2, r3.
1194 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1195 // Convert rhs to a double in r0, r1.
1196 __ mov(r7, Operand(rhs));
1197 ConvertToDoubleStub stub2(r1, r0, r7, r6);
1198 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
1199 __ pop(lr);
1200 }
1201 // Fall through to both_loaded_as_doubles.
1202}
1203
1204
Steve Block1e0659c2011-05-24 12:43:12 +01001205void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001206 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1207 Register rhs_exponent = exp_first ? r0 : r1;
1208 Register lhs_exponent = exp_first ? r2 : r3;
1209 Register rhs_mantissa = exp_first ? r1 : r0;
1210 Register lhs_mantissa = exp_first ? r3 : r2;
1211 Label one_is_nan, neither_is_nan;
1212
1213 __ Sbfx(r4,
1214 lhs_exponent,
1215 HeapNumber::kExponentShift,
1216 HeapNumber::kExponentBits);
1217 // NaNs have all-one exponents so they sign extend to -1.
1218 __ cmp(r4, Operand(-1));
1219 __ b(ne, lhs_not_nan);
1220 __ mov(r4,
1221 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1222 SetCC);
1223 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001224 __ cmp(lhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001225 __ b(ne, &one_is_nan);
1226
1227 __ bind(lhs_not_nan);
1228 __ Sbfx(r4,
1229 rhs_exponent,
1230 HeapNumber::kExponentShift,
1231 HeapNumber::kExponentBits);
1232 // NaNs have all-one exponents so they sign extend to -1.
1233 __ cmp(r4, Operand(-1));
1234 __ b(ne, &neither_is_nan);
1235 __ mov(r4,
1236 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1237 SetCC);
1238 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001239 __ cmp(rhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001240 __ b(eq, &neither_is_nan);
1241
1242 __ bind(&one_is_nan);
1243 // NaN comparisons always fail.
1244 // Load whatever we need in r0 to make the comparison fail.
Steve Block1e0659c2011-05-24 12:43:12 +01001245 if (cond == lt || cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001246 __ mov(r0, Operand(GREATER));
1247 } else {
1248 __ mov(r0, Operand(LESS));
1249 }
1250 __ Ret();
1251
1252 __ bind(&neither_is_nan);
1253}
1254
1255
1256// See comment at call site.
Steve Block1e0659c2011-05-24 12:43:12 +01001257static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm,
1258 Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001259 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1260 Register rhs_exponent = exp_first ? r0 : r1;
1261 Register lhs_exponent = exp_first ? r2 : r3;
1262 Register rhs_mantissa = exp_first ? r1 : r0;
1263 Register lhs_mantissa = exp_first ? r3 : r2;
1264
1265 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001266 if (cond == eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001267 // Doubles are not equal unless they have the same bit pattern.
1268 // Exception: 0 and -0.
1269 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
1270 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
1271 // Return non-zero if the numbers are unequal.
1272 __ Ret(ne);
1273
1274 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
1275 // If exponents are equal then return 0.
1276 __ Ret(eq);
1277
1278 // Exponents are unequal. The only way we can return that the numbers
1279 // are equal is if one is -0 and the other is 0. We already dealt
1280 // with the case where both are -0 or both are 0.
1281 // We start by seeing if the mantissas (that are equal) or the bottom
1282 // 31 bits of the rhs exponent are non-zero. If so we return not
1283 // equal.
1284 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
1285 __ mov(r0, Operand(r4), LeaveCC, ne);
1286 __ Ret(ne);
1287 // Now they are equal if and only if the lhs exponent is zero in its
1288 // low 31 bits.
1289 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
1290 __ Ret();
1291 } else {
1292 // Call a native function to do a comparison between two non-NaNs.
1293 // Call C routine that may not cause GC or other trouble.
1294 __ push(lr);
1295 __ PrepareCallCFunction(4, r5); // Two doubles count as 4 arguments.
Steve Block44f0eee2011-05-26 01:26:41 +01001296 __ CallCFunction(ExternalReference::compare_doubles(masm->isolate()), 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001297 __ pop(pc); // Return.
1298 }
1299}
1300
1301
1302// See comment at call site.
1303static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
1304 Register lhs,
1305 Register rhs) {
1306 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1307 (lhs.is(r1) && rhs.is(r0)));
1308
1309 // If either operand is a JSObject or an oddball value, then they are
1310 // not equal since their pointers are different.
1311 // There is no test for undetectability in strict equality.
1312 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1313 Label first_non_object;
1314 // Get the type of the first operand into r2 and compare it with
1315 // FIRST_JS_OBJECT_TYPE.
1316 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
1317 __ b(lt, &first_non_object);
1318
1319 // Return non-zero (r0 is not zero)
1320 Label return_not_equal;
1321 __ bind(&return_not_equal);
1322 __ Ret();
1323
1324 __ bind(&first_non_object);
1325 // Check for oddballs: true, false, null, undefined.
1326 __ cmp(r2, Operand(ODDBALL_TYPE));
1327 __ b(eq, &return_not_equal);
1328
1329 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
1330 __ b(ge, &return_not_equal);
1331
1332 // Check for oddballs: true, false, null, undefined.
1333 __ cmp(r3, Operand(ODDBALL_TYPE));
1334 __ b(eq, &return_not_equal);
1335
1336 // Now that we have the types we might as well check for symbol-symbol.
1337 // Ensure that no non-strings have the symbol bit set.
1338 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
1339 STATIC_ASSERT(kSymbolTag != 0);
1340 __ and_(r2, r2, Operand(r3));
1341 __ tst(r2, Operand(kIsSymbolMask));
1342 __ b(ne, &return_not_equal);
1343}
1344
1345
1346// See comment at call site.
1347static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
1348 Register lhs,
1349 Register rhs,
1350 Label* both_loaded_as_doubles,
1351 Label* not_heap_numbers,
1352 Label* slow) {
1353 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1354 (lhs.is(r1) && rhs.is(r0)));
1355
1356 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
1357 __ b(ne, not_heap_numbers);
1358 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
1359 __ cmp(r2, r3);
1360 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
1361
1362 // Both are heap numbers. Load them up then jump to the code we have
1363 // for that.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001364 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001365 CpuFeatures::Scope scope(VFP3);
1366 __ sub(r7, rhs, Operand(kHeapObjectTag));
1367 __ vldr(d6, r7, HeapNumber::kValueOffset);
1368 __ sub(r7, lhs, Operand(kHeapObjectTag));
1369 __ vldr(d7, r7, HeapNumber::kValueOffset);
1370 } else {
1371 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1372 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1373 }
1374 __ jmp(both_loaded_as_doubles);
1375}
1376
1377
1378// Fast negative check for symbol-to-symbol equality.
1379static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
1380 Register lhs,
1381 Register rhs,
1382 Label* possible_strings,
1383 Label* not_both_strings) {
1384 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1385 (lhs.is(r1) && rhs.is(r0)));
1386
1387 // r2 is object type of rhs.
1388 // Ensure that no non-strings have the symbol bit set.
1389 Label object_test;
1390 STATIC_ASSERT(kSymbolTag != 0);
1391 __ tst(r2, Operand(kIsNotStringMask));
1392 __ b(ne, &object_test);
1393 __ tst(r2, Operand(kIsSymbolMask));
1394 __ b(eq, possible_strings);
1395 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
1396 __ b(ge, not_both_strings);
1397 __ tst(r3, Operand(kIsSymbolMask));
1398 __ b(eq, possible_strings);
1399
1400 // Both are symbols. We already checked they weren't the same pointer
1401 // so they are not equal.
1402 __ mov(r0, Operand(NOT_EQUAL));
1403 __ Ret();
1404
1405 __ bind(&object_test);
1406 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
1407 __ b(lt, not_both_strings);
1408 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
1409 __ b(lt, not_both_strings);
1410 // If both objects are undetectable, they are equal. Otherwise, they
1411 // are not equal, since they are different objects and an object is not
1412 // equal to undefined.
1413 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
1414 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
1415 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
1416 __ and_(r0, r2, Operand(r3));
1417 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
1418 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
1419 __ Ret();
1420}
1421
1422
1423void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
1424 Register object,
1425 Register result,
1426 Register scratch1,
1427 Register scratch2,
1428 Register scratch3,
1429 bool object_is_smi,
1430 Label* not_found) {
1431 // Use of registers. Register result is used as a temporary.
1432 Register number_string_cache = result;
1433 Register mask = scratch3;
1434
1435 // Load the number string cache.
1436 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
1437
1438 // Make the hash mask from the length of the number string cache. It
1439 // contains two elements (number and string) for each cache entry.
1440 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
1441 // Divide length by two (length is a smi).
1442 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
1443 __ sub(mask, mask, Operand(1)); // Make mask.
1444
1445 // Calculate the entry in the number string cache. The hash value in the
1446 // number string cache for smis is just the smi value, and the hash for
1447 // doubles is the xor of the upper and lower words. See
1448 // Heap::GetNumberStringCache.
Steve Block44f0eee2011-05-26 01:26:41 +01001449 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001450 Label is_smi;
1451 Label load_result_from_cache;
1452 if (!object_is_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01001453 __ JumpIfSmi(object, &is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001454 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001455 CpuFeatures::Scope scope(VFP3);
1456 __ CheckMap(object,
1457 scratch1,
1458 Heap::kHeapNumberMapRootIndex,
1459 not_found,
1460 true);
1461
1462 STATIC_ASSERT(8 == kDoubleSize);
1463 __ add(scratch1,
1464 object,
1465 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
1466 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
1467 __ eor(scratch1, scratch1, Operand(scratch2));
1468 __ and_(scratch1, scratch1, Operand(mask));
1469
1470 // Calculate address of entry in string cache: each entry consists
1471 // of two pointer sized fields.
1472 __ add(scratch1,
1473 number_string_cache,
1474 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
1475
1476 Register probe = mask;
1477 __ ldr(probe,
1478 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
Steve Block1e0659c2011-05-24 12:43:12 +01001479 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001480 __ sub(scratch2, object, Operand(kHeapObjectTag));
1481 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
1482 __ sub(probe, probe, Operand(kHeapObjectTag));
1483 __ vldr(d1, probe, HeapNumber::kValueOffset);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001484 __ VFPCompareAndSetFlags(d0, d1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001485 __ b(ne, not_found); // The cache did not contain this value.
1486 __ b(&load_result_from_cache);
1487 } else {
1488 __ b(not_found);
1489 }
1490 }
1491
1492 __ bind(&is_smi);
1493 Register scratch = scratch1;
1494 __ and_(scratch, mask, Operand(object, ASR, 1));
1495 // Calculate address of entry in string cache: each entry consists
1496 // of two pointer sized fields.
1497 __ add(scratch,
1498 number_string_cache,
1499 Operand(scratch, LSL, kPointerSizeLog2 + 1));
1500
1501 // Check if the entry is the smi we are looking for.
1502 Register probe = mask;
1503 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
1504 __ cmp(object, probe);
1505 __ b(ne, not_found);
1506
1507 // Get the result from the cache.
1508 __ bind(&load_result_from_cache);
1509 __ ldr(result,
1510 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01001511 __ IncrementCounter(isolate->counters()->number_to_string_native(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001512 1,
1513 scratch1,
1514 scratch2);
1515}
1516
1517
1518void NumberToStringStub::Generate(MacroAssembler* masm) {
1519 Label runtime;
1520
1521 __ ldr(r1, MemOperand(sp, 0));
1522
1523 // Generate code to lookup number in the number string cache.
1524 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
1525 __ add(sp, sp, Operand(1 * kPointerSize));
1526 __ Ret();
1527
1528 __ bind(&runtime);
1529 // Handle number to string in the runtime system if not found in the cache.
1530 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
1531}
1532
1533
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001534// On entry lhs_ and rhs_ are the values to be compared.
1535// On exit r0 is 0, positive or negative to indicate the result of
1536// the comparison.
1537void CompareStub::Generate(MacroAssembler* masm) {
1538 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
1539 (lhs_.is(r1) && rhs_.is(r0)));
1540
1541 Label slow; // Call builtin.
1542 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
1543
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001544 if (include_smi_compare_) {
1545 Label not_two_smis, smi_done;
1546 __ orr(r2, r1, r0);
1547 __ tst(r2, Operand(kSmiTagMask));
1548 __ b(ne, &not_two_smis);
Ben Murdochf87a2032010-10-22 12:50:53 +01001549 __ mov(r1, Operand(r1, ASR, 1));
1550 __ sub(r0, r1, Operand(r0, ASR, 1));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001551 __ Ret();
1552 __ bind(&not_two_smis);
1553 } else if (FLAG_debug_code) {
1554 __ orr(r2, r1, r0);
1555 __ tst(r2, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001556 __ Assert(ne, "CompareStub: unexpected smi operands.");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001557 }
1558
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001559 // NOTICE! This code is only reached after a smi-fast-case check, so
1560 // it is certain that at least one operand isn't a smi.
1561
1562 // Handle the case where the objects are identical. Either returns the answer
1563 // or goes to slow. Only falls through if the objects were not identical.
1564 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
1565
1566 // If either is a Smi (we know that not both are), then they can only
1567 // be strictly equal if the other is a HeapNumber.
1568 STATIC_ASSERT(kSmiTag == 0);
1569 ASSERT_EQ(0, Smi::FromInt(0));
1570 __ and_(r2, lhs_, Operand(rhs_));
1571 __ tst(r2, Operand(kSmiTagMask));
1572 __ b(ne, &not_smis);
1573 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
1574 // 1) Return the answer.
1575 // 2) Go to slow.
1576 // 3) Fall through to both_loaded_as_doubles.
1577 // 4) Jump to lhs_not_nan.
1578 // In cases 3 and 4 we have found out we were dealing with a number-number
1579 // comparison. If VFP3 is supported the double values of the numbers have
1580 // been loaded into d7 and d6. Otherwise, the double values have been loaded
1581 // into r0, r1, r2, and r3.
1582 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
1583
1584 __ bind(&both_loaded_as_doubles);
1585 // The arguments have been converted to doubles and stored in d6 and d7, if
1586 // VFP3 is supported, or in r0, r1, r2, and r3.
Steve Block44f0eee2011-05-26 01:26:41 +01001587 Isolate* isolate = masm->isolate();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001588 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001589 __ bind(&lhs_not_nan);
1590 CpuFeatures::Scope scope(VFP3);
1591 Label no_nan;
1592 // ARMv7 VFP3 instructions to implement double precision comparison.
Ben Murdochb8e0da22011-05-16 14:20:40 +01001593 __ VFPCompareAndSetFlags(d7, d6);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001594 Label nan;
1595 __ b(vs, &nan);
1596 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
1597 __ mov(r0, Operand(LESS), LeaveCC, lt);
1598 __ mov(r0, Operand(GREATER), LeaveCC, gt);
1599 __ Ret();
1600
1601 __ bind(&nan);
1602 // If one of the sides was a NaN then the v flag is set. Load r0 with
1603 // whatever it takes to make the comparison fail, since comparisons with NaN
1604 // always fail.
1605 if (cc_ == lt || cc_ == le) {
1606 __ mov(r0, Operand(GREATER));
1607 } else {
1608 __ mov(r0, Operand(LESS));
1609 }
1610 __ Ret();
1611 } else {
1612 // Checks for NaN in the doubles we have loaded. Can return the answer or
1613 // fall through if neither is a NaN. Also binds lhs_not_nan.
1614 EmitNanCheck(masm, &lhs_not_nan, cc_);
1615 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
1616 // answer. Never falls through.
1617 EmitTwoNonNanDoubleComparison(masm, cc_);
1618 }
1619
1620 __ bind(&not_smis);
1621 // At this point we know we are dealing with two different objects,
1622 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
1623 if (strict_) {
1624 // This returns non-equal for some object types, or falls through if it
1625 // was not lucky.
1626 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1627 }
1628
1629 Label check_for_symbols;
1630 Label flat_string_check;
1631 // Check for heap-number-heap-number comparison. Can jump to slow case,
1632 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
1633 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1634 // In this case r2 will contain the type of rhs_. Never falls through.
1635 EmitCheckForTwoHeapNumbers(masm,
1636 lhs_,
1637 rhs_,
1638 &both_loaded_as_doubles,
1639 &check_for_symbols,
1640 &flat_string_check);
1641
1642 __ bind(&check_for_symbols);
1643 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
1644 // symbols.
1645 if (cc_ == eq && !strict_) {
1646 // Returns an answer for two symbols or two detectable objects.
1647 // Otherwise jumps to string case or not both strings case.
1648 // Assumes that r2 is the type of rhs_ on entry.
1649 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1650 }
1651
1652 // Check for both being sequential ASCII strings, and inline if that is the
1653 // case.
1654 __ bind(&flat_string_check);
1655
1656 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
1657
Steve Block44f0eee2011-05-26 01:26:41 +01001658 __ IncrementCounter(isolate->counters()->string_compare_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001659 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1660 lhs_,
1661 rhs_,
1662 r2,
1663 r3,
1664 r4,
1665 r5);
1666 // Never falls through to here.
1667
1668 __ bind(&slow);
1669
1670 __ Push(lhs_, rhs_);
1671 // Figure out which native to call and setup the arguments.
1672 Builtins::JavaScript native;
1673 if (cc_ == eq) {
1674 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1675 } else {
1676 native = Builtins::COMPARE;
1677 int ncr; // NaN compare result
1678 if (cc_ == lt || cc_ == le) {
1679 ncr = GREATER;
1680 } else {
1681 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
1682 ncr = LESS;
1683 }
1684 __ mov(r0, Operand(Smi::FromInt(ncr)));
1685 __ push(r0);
1686 }
1687
1688 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1689 // tagged as a small integer.
1690 __ InvokeBuiltin(native, JUMP_JS);
1691}
1692
1693
1694// This stub does not handle the inlined cases (Smis, Booleans, undefined).
1695// The stub returns zero for false, and a non-zero value for true.
1696void ToBooleanStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001697 // This stub uses VFP3 instructions.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001698 ASSERT(CpuFeatures::IsEnabled(VFP3));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001699
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001700 Label false_result;
1701 Label not_heap_number;
Steve Block1e0659c2011-05-24 12:43:12 +01001702 Register scratch = r9.is(tos_) ? r7 : r9;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001703
Ben Murdochb0fe1622011-05-05 13:52:32 +01001704 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1705 __ cmp(tos_, ip);
1706 __ b(eq, &false_result);
1707
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001708 // HeapNumber => false iff +0, -0, or NaN.
1709 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1710 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
1711 __ cmp(scratch, ip);
1712 __ b(&not_heap_number, ne);
1713
1714 __ sub(ip, tos_, Operand(kHeapObjectTag));
1715 __ vldr(d1, ip, HeapNumber::kValueOffset);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001716 __ VFPCompareAndSetFlags(d1, 0.0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001717 // "tos_" is a register, and contains a non zero value by default.
1718 // Hence we only need to overwrite "tos_" with zero to return false for
1719 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001720 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, eq); // for FP_ZERO
1721 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, vs); // for FP_NAN
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001722 __ Ret();
1723
1724 __ bind(&not_heap_number);
1725
1726 // Check if the value is 'null'.
1727 // 'null' => false.
1728 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1729 __ cmp(tos_, ip);
1730 __ b(&false_result, eq);
1731
1732 // It can be an undetectable object.
1733 // Undetectable => false.
1734 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
1735 __ ldrb(scratch, FieldMemOperand(ip, Map::kBitFieldOffset));
1736 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
1737 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
1738 __ b(&false_result, eq);
1739
1740 // JavaScript object => true.
1741 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1742 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1743 __ cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
1744 // "tos_" is a register and contains a non-zero value.
1745 // Hence we implicitly return true if the greater than
1746 // condition is satisfied.
1747 __ Ret(gt);
1748
1749 // Check for string
1750 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1751 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1752 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
1753 // "tos_" is a register and contains a non-zero value.
1754 // Hence we implicitly return true if the greater than
1755 // condition is satisfied.
1756 __ Ret(gt);
1757
1758 // String value => false iff empty, i.e., length is zero
1759 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
1760 // If length is zero, "tos_" contains zero ==> false.
1761 // If length is not zero, "tos_" contains a non-zero value ==> true.
1762 __ Ret();
1763
1764 // Return 0 in "tos_" for false .
1765 __ bind(&false_result);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001766 __ mov(tos_, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001767 __ Ret();
1768}
1769
1770
Ben Murdochb0fe1622011-05-05 13:52:32 +01001771Handle<Code> GetTypeRecordingBinaryOpStub(int key,
1772 TRBinaryOpIC::TypeInfo type_info,
1773 TRBinaryOpIC::TypeInfo result_type_info) {
Steve Block1e0659c2011-05-24 12:43:12 +01001774 TypeRecordingBinaryOpStub stub(key, type_info, result_type_info);
1775 return stub.GetCode();
1776}
1777
1778
1779void TypeRecordingBinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
1780 Label get_result;
1781
1782 __ Push(r1, r0);
1783
1784 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
1785 __ mov(r1, Operand(Smi::FromInt(op_)));
1786 __ mov(r0, Operand(Smi::FromInt(operands_type_)));
1787 __ Push(r2, r1, r0);
1788
1789 __ TailCallExternalReference(
Steve Block44f0eee2011-05-26 01:26:41 +01001790 ExternalReference(IC_Utility(IC::kTypeRecordingBinaryOp_Patch),
1791 masm->isolate()),
Steve Block1e0659c2011-05-24 12:43:12 +01001792 5,
1793 1);
1794}
1795
1796
1797void TypeRecordingBinaryOpStub::GenerateTypeTransitionWithSavedArgs(
1798 MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001799 UNIMPLEMENTED();
Steve Block1e0659c2011-05-24 12:43:12 +01001800}
1801
1802
1803void TypeRecordingBinaryOpStub::Generate(MacroAssembler* masm) {
1804 switch (operands_type_) {
1805 case TRBinaryOpIC::UNINITIALIZED:
1806 GenerateTypeTransition(masm);
1807 break;
1808 case TRBinaryOpIC::SMI:
1809 GenerateSmiStub(masm);
1810 break;
1811 case TRBinaryOpIC::INT32:
1812 GenerateInt32Stub(masm);
1813 break;
1814 case TRBinaryOpIC::HEAP_NUMBER:
1815 GenerateHeapNumberStub(masm);
1816 break;
Steve Block44f0eee2011-05-26 01:26:41 +01001817 case TRBinaryOpIC::ODDBALL:
1818 GenerateOddballStub(masm);
1819 break;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001820 case TRBinaryOpIC::BOTH_STRING:
1821 GenerateBothStringStub(masm);
1822 break;
Steve Block1e0659c2011-05-24 12:43:12 +01001823 case TRBinaryOpIC::STRING:
1824 GenerateStringStub(masm);
1825 break;
1826 case TRBinaryOpIC::GENERIC:
1827 GenerateGeneric(masm);
1828 break;
1829 default:
1830 UNREACHABLE();
1831 }
1832}
1833
1834
1835const char* TypeRecordingBinaryOpStub::GetName() {
1836 if (name_ != NULL) return name_;
1837 const int kMaxNameLength = 100;
Steve Block44f0eee2011-05-26 01:26:41 +01001838 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
1839 kMaxNameLength);
Steve Block1e0659c2011-05-24 12:43:12 +01001840 if (name_ == NULL) return "OOM";
1841 const char* op_name = Token::Name(op_);
1842 const char* overwrite_name;
1843 switch (mode_) {
1844 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
1845 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
1846 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
1847 default: overwrite_name = "UnknownOverwrite"; break;
1848 }
1849
1850 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
1851 "TypeRecordingBinaryOpStub_%s_%s_%s",
1852 op_name,
1853 overwrite_name,
1854 TRBinaryOpIC::GetName(operands_type_));
1855 return name_;
1856}
1857
1858
1859void TypeRecordingBinaryOpStub::GenerateSmiSmiOperation(
1860 MacroAssembler* masm) {
1861 Register left = r1;
1862 Register right = r0;
1863 Register scratch1 = r7;
1864 Register scratch2 = r9;
1865
1866 ASSERT(right.is(r0));
1867 STATIC_ASSERT(kSmiTag == 0);
1868
1869 Label not_smi_result;
1870 switch (op_) {
1871 case Token::ADD:
1872 __ add(right, left, Operand(right), SetCC); // Add optimistically.
1873 __ Ret(vc);
1874 __ sub(right, right, Operand(left)); // Revert optimistic add.
1875 break;
1876 case Token::SUB:
1877 __ sub(right, left, Operand(right), SetCC); // Subtract optimistically.
1878 __ Ret(vc);
1879 __ sub(right, left, Operand(right)); // Revert optimistic subtract.
1880 break;
1881 case Token::MUL:
1882 // Remove tag from one of the operands. This way the multiplication result
1883 // will be a smi if it fits the smi range.
1884 __ SmiUntag(ip, right);
1885 // Do multiplication
1886 // scratch1 = lower 32 bits of ip * left.
1887 // scratch2 = higher 32 bits of ip * left.
1888 __ smull(scratch1, scratch2, left, ip);
1889 // Check for overflowing the smi range - no overflow if higher 33 bits of
1890 // the result are identical.
1891 __ mov(ip, Operand(scratch1, ASR, 31));
1892 __ cmp(ip, Operand(scratch2));
1893 __ b(ne, &not_smi_result);
1894 // Go slow on zero result to handle -0.
1895 __ tst(scratch1, Operand(scratch1));
1896 __ mov(right, Operand(scratch1), LeaveCC, ne);
1897 __ Ret(ne);
1898 // We need -0 if we were multiplying a negative number with 0 to get 0.
1899 // We know one of them was zero.
1900 __ add(scratch2, right, Operand(left), SetCC);
1901 __ mov(right, Operand(Smi::FromInt(0)), LeaveCC, pl);
1902 __ Ret(pl); // Return smi 0 if the non-zero one was positive.
1903 // We fall through here if we multiplied a negative number with 0, because
1904 // that would mean we should produce -0.
1905 break;
1906 case Token::DIV:
1907 // Check for power of two on the right hand side.
1908 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
1909 // Check for positive and no remainder (scratch1 contains right - 1).
1910 __ orr(scratch2, scratch1, Operand(0x80000000u));
1911 __ tst(left, scratch2);
1912 __ b(ne, &not_smi_result);
1913
1914 // Perform division by shifting.
1915 __ CountLeadingZeros(scratch1, scratch1, scratch2);
1916 __ rsb(scratch1, scratch1, Operand(31));
1917 __ mov(right, Operand(left, LSR, scratch1));
1918 __ Ret();
1919 break;
1920 case Token::MOD:
1921 // Check for two positive smis.
1922 __ orr(scratch1, left, Operand(right));
1923 __ tst(scratch1, Operand(0x80000000u | kSmiTagMask));
1924 __ b(ne, &not_smi_result);
1925
1926 // Check for power of two on the right hand side.
1927 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
1928
1929 // Perform modulus by masking.
1930 __ and_(right, left, Operand(scratch1));
1931 __ Ret();
1932 break;
1933 case Token::BIT_OR:
1934 __ orr(right, left, Operand(right));
1935 __ Ret();
1936 break;
1937 case Token::BIT_AND:
1938 __ and_(right, left, Operand(right));
1939 __ Ret();
1940 break;
1941 case Token::BIT_XOR:
1942 __ eor(right, left, Operand(right));
1943 __ Ret();
1944 break;
1945 case Token::SAR:
1946 // Remove tags from right operand.
1947 __ GetLeastBitsFromSmi(scratch1, right, 5);
1948 __ mov(right, Operand(left, ASR, scratch1));
1949 // Smi tag result.
1950 __ bic(right, right, Operand(kSmiTagMask));
1951 __ Ret();
1952 break;
1953 case Token::SHR:
1954 // Remove tags from operands. We can't do this on a 31 bit number
1955 // because then the 0s get shifted into bit 30 instead of bit 31.
1956 __ SmiUntag(scratch1, left);
1957 __ GetLeastBitsFromSmi(scratch2, right, 5);
1958 __ mov(scratch1, Operand(scratch1, LSR, scratch2));
1959 // Unsigned shift is not allowed to produce a negative number, so
1960 // check the sign bit and the sign bit after Smi tagging.
1961 __ tst(scratch1, Operand(0xc0000000));
1962 __ b(ne, &not_smi_result);
1963 // Smi tag result.
1964 __ SmiTag(right, scratch1);
1965 __ Ret();
1966 break;
1967 case Token::SHL:
1968 // Remove tags from operands.
1969 __ SmiUntag(scratch1, left);
1970 __ GetLeastBitsFromSmi(scratch2, right, 5);
1971 __ mov(scratch1, Operand(scratch1, LSL, scratch2));
1972 // Check that the signed result fits in a Smi.
1973 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
1974 __ b(mi, &not_smi_result);
1975 __ SmiTag(right, scratch1);
1976 __ Ret();
1977 break;
1978 default:
1979 UNREACHABLE();
1980 }
1981 __ bind(&not_smi_result);
1982}
1983
1984
1985void TypeRecordingBinaryOpStub::GenerateFPOperation(MacroAssembler* masm,
1986 bool smi_operands,
1987 Label* not_numbers,
1988 Label* gc_required) {
1989 Register left = r1;
1990 Register right = r0;
1991 Register scratch1 = r7;
1992 Register scratch2 = r9;
Steve Block44f0eee2011-05-26 01:26:41 +01001993 Register scratch3 = r4;
Steve Block1e0659c2011-05-24 12:43:12 +01001994
1995 ASSERT(smi_operands || (not_numbers != NULL));
1996 if (smi_operands && FLAG_debug_code) {
1997 __ AbortIfNotSmi(left);
1998 __ AbortIfNotSmi(right);
1999 }
2000
2001 Register heap_number_map = r6;
2002 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2003
2004 switch (op_) {
2005 case Token::ADD:
2006 case Token::SUB:
2007 case Token::MUL:
2008 case Token::DIV:
2009 case Token::MOD: {
2010 // Load left and right operands into d6 and d7 or r0/r1 and r2/r3
2011 // depending on whether VFP3 is available or not.
2012 FloatingPointHelper::Destination destination =
Ben Murdoch8b112d22011-06-08 16:22:53 +01002013 CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +01002014 op_ != Token::MOD ?
Steve Block1e0659c2011-05-24 12:43:12 +01002015 FloatingPointHelper::kVFPRegisters :
2016 FloatingPointHelper::kCoreRegisters;
2017
2018 // Allocate new heap number for result.
2019 Register result = r5;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002020 GenerateHeapResultAllocation(
2021 masm, result, heap_number_map, scratch1, scratch2, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01002022
2023 // Load the operands.
2024 if (smi_operands) {
2025 FloatingPointHelper::LoadSmis(masm, destination, scratch1, scratch2);
2026 } else {
2027 FloatingPointHelper::LoadOperands(masm,
2028 destination,
2029 heap_number_map,
2030 scratch1,
2031 scratch2,
2032 not_numbers);
2033 }
2034
2035 // Calculate the result.
2036 if (destination == FloatingPointHelper::kVFPRegisters) {
2037 // Using VFP registers:
2038 // d6: Left value
2039 // d7: Right value
2040 CpuFeatures::Scope scope(VFP3);
2041 switch (op_) {
2042 case Token::ADD:
2043 __ vadd(d5, d6, d7);
2044 break;
2045 case Token::SUB:
2046 __ vsub(d5, d6, d7);
2047 break;
2048 case Token::MUL:
2049 __ vmul(d5, d6, d7);
2050 break;
2051 case Token::DIV:
2052 __ vdiv(d5, d6, d7);
2053 break;
2054 default:
2055 UNREACHABLE();
2056 }
2057
2058 __ sub(r0, result, Operand(kHeapObjectTag));
2059 __ vstr(d5, r0, HeapNumber::kValueOffset);
2060 __ add(r0, r0, Operand(kHeapObjectTag));
2061 __ Ret();
2062 } else {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002063 // Call the C function to handle the double operation.
2064 FloatingPointHelper::CallCCodeForDoubleOperation(masm,
2065 op_,
2066 result,
2067 scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002068 if (FLAG_debug_code) {
2069 __ stop("Unreachable code.");
2070 }
Steve Block1e0659c2011-05-24 12:43:12 +01002071 }
2072 break;
2073 }
2074 case Token::BIT_OR:
2075 case Token::BIT_XOR:
2076 case Token::BIT_AND:
2077 case Token::SAR:
2078 case Token::SHR:
2079 case Token::SHL: {
2080 if (smi_operands) {
2081 __ SmiUntag(r3, left);
2082 __ SmiUntag(r2, right);
2083 } else {
2084 // Convert operands to 32-bit integers. Right in r2 and left in r3.
Steve Block44f0eee2011-05-26 01:26:41 +01002085 FloatingPointHelper::ConvertNumberToInt32(masm,
2086 left,
2087 r3,
2088 heap_number_map,
2089 scratch1,
2090 scratch2,
2091 scratch3,
2092 d0,
2093 not_numbers);
2094 FloatingPointHelper::ConvertNumberToInt32(masm,
2095 right,
2096 r2,
2097 heap_number_map,
2098 scratch1,
2099 scratch2,
2100 scratch3,
2101 d0,
2102 not_numbers);
Steve Block1e0659c2011-05-24 12:43:12 +01002103 }
2104
2105 Label result_not_a_smi;
2106 switch (op_) {
2107 case Token::BIT_OR:
2108 __ orr(r2, r3, Operand(r2));
2109 break;
2110 case Token::BIT_XOR:
2111 __ eor(r2, r3, Operand(r2));
2112 break;
2113 case Token::BIT_AND:
2114 __ and_(r2, r3, Operand(r2));
2115 break;
2116 case Token::SAR:
2117 // Use only the 5 least significant bits of the shift count.
Steve Block1e0659c2011-05-24 12:43:12 +01002118 __ GetLeastBitsFromInt32(r2, r2, 5);
2119 __ mov(r2, Operand(r3, ASR, r2));
2120 break;
2121 case Token::SHR:
2122 // Use only the 5 least significant bits of the shift count.
2123 __ GetLeastBitsFromInt32(r2, r2, 5);
2124 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2125 // SHR is special because it is required to produce a positive answer.
2126 // The code below for writing into heap numbers isn't capable of
2127 // writing the register as an unsigned int so we go to slow case if we
2128 // hit this case.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002129 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002130 __ b(mi, &result_not_a_smi);
2131 } else {
2132 __ b(mi, not_numbers);
2133 }
2134 break;
2135 case Token::SHL:
2136 // Use only the 5 least significant bits of the shift count.
2137 __ GetLeastBitsFromInt32(r2, r2, 5);
2138 __ mov(r2, Operand(r3, LSL, r2));
2139 break;
2140 default:
2141 UNREACHABLE();
2142 }
2143
2144 // Check that the *signed* result fits in a smi.
2145 __ add(r3, r2, Operand(0x40000000), SetCC);
2146 __ b(mi, &result_not_a_smi);
2147 __ SmiTag(r0, r2);
2148 __ Ret();
2149
2150 // Allocate new heap number for result.
2151 __ bind(&result_not_a_smi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002152 Register result = r5;
2153 if (smi_operands) {
2154 __ AllocateHeapNumber(
2155 result, scratch1, scratch2, heap_number_map, gc_required);
2156 } else {
2157 GenerateHeapResultAllocation(
2158 masm, result, heap_number_map, scratch1, scratch2, gc_required);
2159 }
Steve Block1e0659c2011-05-24 12:43:12 +01002160
2161 // r2: Answer as signed int32.
2162 // r5: Heap number to write answer into.
2163
2164 // Nothing can go wrong now, so move the heap number to r0, which is the
2165 // result.
2166 __ mov(r0, Operand(r5));
2167
Ben Murdoch8b112d22011-06-08 16:22:53 +01002168 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002169 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted. As
2170 // mentioned above SHR needs to always produce a positive result.
2171 CpuFeatures::Scope scope(VFP3);
2172 __ vmov(s0, r2);
2173 if (op_ == Token::SHR) {
2174 __ vcvt_f64_u32(d0, s0);
2175 } else {
2176 __ vcvt_f64_s32(d0, s0);
2177 }
2178 __ sub(r3, r0, Operand(kHeapObjectTag));
2179 __ vstr(d0, r3, HeapNumber::kValueOffset);
2180 __ Ret();
2181 } else {
2182 // Tail call that writes the int32 in r2 to the heap number in r0, using
2183 // r3 as scratch. r0 is preserved and returned.
2184 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
2185 __ TailCallStub(&stub);
2186 }
2187 break;
2188 }
2189 default:
2190 UNREACHABLE();
2191 }
2192}
2193
2194
2195// Generate the smi code. If the operation on smis are successful this return is
2196// generated. If the result is not a smi and heap number allocation is not
2197// requested the code falls through. If number allocation is requested but a
2198// heap number cannot be allocated the code jumps to the lable gc_required.
2199void TypeRecordingBinaryOpStub::GenerateSmiCode(MacroAssembler* masm,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002200 Label* use_runtime,
Steve Block1e0659c2011-05-24 12:43:12 +01002201 Label* gc_required,
2202 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
2203 Label not_smis;
2204
2205 Register left = r1;
2206 Register right = r0;
2207 Register scratch1 = r7;
2208 Register scratch2 = r9;
2209
2210 // Perform combined smi check on both operands.
2211 __ orr(scratch1, left, Operand(right));
2212 STATIC_ASSERT(kSmiTag == 0);
2213 __ tst(scratch1, Operand(kSmiTagMask));
2214 __ b(ne, &not_smis);
2215
2216 // If the smi-smi operation results in a smi return is generated.
2217 GenerateSmiSmiOperation(masm);
2218
2219 // If heap number results are possible generate the result in an allocated
2220 // heap number.
2221 if (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002222 GenerateFPOperation(masm, true, use_runtime, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01002223 }
2224 __ bind(&not_smis);
2225}
2226
2227
2228void TypeRecordingBinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
2229 Label not_smis, call_runtime;
2230
2231 if (result_type_ == TRBinaryOpIC::UNINITIALIZED ||
2232 result_type_ == TRBinaryOpIC::SMI) {
2233 // Only allow smi results.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002234 GenerateSmiCode(masm, &call_runtime, NULL, NO_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002235 } else {
2236 // Allow heap number result and don't make a transition if a heap number
2237 // cannot be allocated.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002238 GenerateSmiCode(masm,
2239 &call_runtime,
2240 &call_runtime,
2241 ALLOW_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002242 }
2243
2244 // Code falls through if the result is not returned as either a smi or heap
2245 // number.
2246 GenerateTypeTransition(masm);
2247
2248 __ bind(&call_runtime);
2249 GenerateCallRuntime(masm);
2250}
2251
2252
2253void TypeRecordingBinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
2254 ASSERT(operands_type_ == TRBinaryOpIC::STRING);
2255 ASSERT(op_ == Token::ADD);
2256 // Try to add arguments as strings, otherwise, transition to the generic
2257 // TRBinaryOpIC type.
2258 GenerateAddStrings(masm);
2259 GenerateTypeTransition(masm);
2260}
2261
2262
Ben Murdoch8b112d22011-06-08 16:22:53 +01002263void TypeRecordingBinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
2264 Label call_runtime;
2265 ASSERT(operands_type_ == TRBinaryOpIC::BOTH_STRING);
2266 ASSERT(op_ == Token::ADD);
2267 // If both arguments are strings, call the string add stub.
2268 // Otherwise, do a transition.
2269
2270 // Registers containing left and right operands respectively.
2271 Register left = r1;
2272 Register right = r0;
2273
2274 // Test if left operand is a string.
2275 __ JumpIfSmi(left, &call_runtime);
2276 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
2277 __ b(ge, &call_runtime);
2278
2279 // Test if right operand is a string.
2280 __ JumpIfSmi(right, &call_runtime);
2281 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
2282 __ b(ge, &call_runtime);
2283
2284 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
2285 GenerateRegisterArgsPush(masm);
2286 __ TailCallStub(&string_add_stub);
2287
2288 __ bind(&call_runtime);
2289 GenerateTypeTransition(masm);
2290}
2291
2292
Steve Block1e0659c2011-05-24 12:43:12 +01002293void TypeRecordingBinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
2294 ASSERT(operands_type_ == TRBinaryOpIC::INT32);
2295
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002296 Register left = r1;
2297 Register right = r0;
2298 Register scratch1 = r7;
2299 Register scratch2 = r9;
2300 DwVfpRegister double_scratch = d0;
2301 SwVfpRegister single_scratch = s3;
2302
2303 Register heap_number_result = no_reg;
2304 Register heap_number_map = r6;
2305 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2306
2307 Label call_runtime;
2308 // Labels for type transition, used for wrong input or output types.
2309 // Both label are currently actually bound to the same position. We use two
2310 // different label to differentiate the cause leading to type transition.
2311 Label transition;
2312
2313 // Smi-smi fast case.
2314 Label skip;
2315 __ orr(scratch1, left, right);
2316 __ JumpIfNotSmi(scratch1, &skip);
2317 GenerateSmiSmiOperation(masm);
2318 // Fall through if the result is not a smi.
2319 __ bind(&skip);
2320
2321 switch (op_) {
2322 case Token::ADD:
2323 case Token::SUB:
2324 case Token::MUL:
2325 case Token::DIV:
2326 case Token::MOD: {
2327 // Load both operands and check that they are 32-bit integer.
2328 // Jump to type transition if they are not. The registers r0 and r1 (right
2329 // and left) are preserved for the runtime call.
2330 FloatingPointHelper::Destination destination =
Ben Murdoch8b112d22011-06-08 16:22:53 +01002331 CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +01002332 op_ != Token::MOD ?
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002333 FloatingPointHelper::kVFPRegisters :
2334 FloatingPointHelper::kCoreRegisters;
2335
2336 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2337 right,
2338 destination,
2339 d7,
2340 r2,
2341 r3,
2342 heap_number_map,
2343 scratch1,
2344 scratch2,
2345 s0,
2346 &transition);
2347 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2348 left,
2349 destination,
2350 d6,
2351 r4,
2352 r5,
2353 heap_number_map,
2354 scratch1,
2355 scratch2,
2356 s0,
2357 &transition);
2358
2359 if (destination == FloatingPointHelper::kVFPRegisters) {
2360 CpuFeatures::Scope scope(VFP3);
2361 Label return_heap_number;
2362 switch (op_) {
2363 case Token::ADD:
2364 __ vadd(d5, d6, d7);
2365 break;
2366 case Token::SUB:
2367 __ vsub(d5, d6, d7);
2368 break;
2369 case Token::MUL:
2370 __ vmul(d5, d6, d7);
2371 break;
2372 case Token::DIV:
2373 __ vdiv(d5, d6, d7);
2374 break;
2375 default:
2376 UNREACHABLE();
2377 }
2378
2379 if (op_ != Token::DIV) {
2380 // These operations produce an integer result.
2381 // Try to return a smi if we can.
2382 // Otherwise return a heap number if allowed, or jump to type
2383 // transition.
2384
2385 __ EmitVFPTruncate(kRoundToZero,
2386 single_scratch,
2387 d5,
2388 scratch1,
2389 scratch2);
2390
2391 if (result_type_ <= TRBinaryOpIC::INT32) {
2392 // If the ne condition is set, result does
2393 // not fit in a 32-bit integer.
2394 __ b(ne, &transition);
2395 }
2396
2397 // Check if the result fits in a smi.
2398 __ vmov(scratch1, single_scratch);
2399 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
2400 // If not try to return a heap number.
2401 __ b(mi, &return_heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +01002402 // Check for minus zero. Return heap number for minus zero.
2403 Label not_zero;
2404 __ cmp(scratch1, Operand(0));
2405 __ b(ne, &not_zero);
2406 __ vmov(scratch2, d5.high());
2407 __ tst(scratch2, Operand(HeapNumber::kSignMask));
2408 __ b(ne, &return_heap_number);
2409 __ bind(&not_zero);
2410
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002411 // Tag the result and return.
2412 __ SmiTag(r0, scratch1);
2413 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +01002414 } else {
2415 // DIV just falls through to allocating a heap number.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002416 }
2417
2418 if (result_type_ >= (op_ == Token::DIV) ? TRBinaryOpIC::HEAP_NUMBER
2419 : TRBinaryOpIC::INT32) {
2420 __ bind(&return_heap_number);
2421 // We are using vfp registers so r5 is available.
2422 heap_number_result = r5;
2423 GenerateHeapResultAllocation(masm,
2424 heap_number_result,
2425 heap_number_map,
2426 scratch1,
2427 scratch2,
2428 &call_runtime);
2429 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
2430 __ vstr(d5, r0, HeapNumber::kValueOffset);
2431 __ mov(r0, heap_number_result);
2432 __ Ret();
2433 }
2434
2435 // A DIV operation expecting an integer result falls through
2436 // to type transition.
2437
2438 } else {
2439 // We preserved r0 and r1 to be able to call runtime.
2440 // Save the left value on the stack.
2441 __ Push(r5, r4);
2442
2443 // Allocate a heap number to store the result.
2444 heap_number_result = r5;
2445 GenerateHeapResultAllocation(masm,
2446 heap_number_result,
2447 heap_number_map,
2448 scratch1,
2449 scratch2,
2450 &call_runtime);
2451
2452 // Load the left value from the value saved on the stack.
2453 __ Pop(r1, r0);
2454
2455 // Call the C function to handle the double operation.
2456 FloatingPointHelper::CallCCodeForDoubleOperation(
2457 masm, op_, heap_number_result, scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002458 if (FLAG_debug_code) {
2459 __ stop("Unreachable code.");
2460 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002461 }
2462
2463 break;
2464 }
2465
2466 case Token::BIT_OR:
2467 case Token::BIT_XOR:
2468 case Token::BIT_AND:
2469 case Token::SAR:
2470 case Token::SHR:
2471 case Token::SHL: {
2472 Label return_heap_number;
2473 Register scratch3 = r5;
2474 // Convert operands to 32-bit integers. Right in r2 and left in r3. The
2475 // registers r0 and r1 (right and left) are preserved for the runtime
2476 // call.
2477 FloatingPointHelper::LoadNumberAsInt32(masm,
2478 left,
2479 r3,
2480 heap_number_map,
2481 scratch1,
2482 scratch2,
2483 scratch3,
2484 d0,
2485 &transition);
2486 FloatingPointHelper::LoadNumberAsInt32(masm,
2487 right,
2488 r2,
2489 heap_number_map,
2490 scratch1,
2491 scratch2,
2492 scratch3,
2493 d0,
2494 &transition);
2495
2496 // The ECMA-262 standard specifies that, for shift operations, only the
2497 // 5 least significant bits of the shift value should be used.
2498 switch (op_) {
2499 case Token::BIT_OR:
2500 __ orr(r2, r3, Operand(r2));
2501 break;
2502 case Token::BIT_XOR:
2503 __ eor(r2, r3, Operand(r2));
2504 break;
2505 case Token::BIT_AND:
2506 __ and_(r2, r3, Operand(r2));
2507 break;
2508 case Token::SAR:
2509 __ and_(r2, r2, Operand(0x1f));
2510 __ mov(r2, Operand(r3, ASR, r2));
2511 break;
2512 case Token::SHR:
2513 __ and_(r2, r2, Operand(0x1f));
2514 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2515 // SHR is special because it is required to produce a positive answer.
2516 // We only get a negative result if the shift value (r2) is 0.
2517 // This result cannot be respresented as a signed 32-bit integer, try
2518 // to return a heap number if we can.
2519 // The non vfp3 code does not support this special case, so jump to
2520 // runtime if we don't support it.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002521 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002522 __ b(mi,
2523 (result_type_ <= TRBinaryOpIC::INT32) ? &transition
2524 : &return_heap_number);
2525 } else {
2526 __ b(mi, (result_type_ <= TRBinaryOpIC::INT32) ? &transition
2527 : &call_runtime);
2528 }
2529 break;
2530 case Token::SHL:
2531 __ and_(r2, r2, Operand(0x1f));
2532 __ mov(r2, Operand(r3, LSL, r2));
2533 break;
2534 default:
2535 UNREACHABLE();
2536 }
2537
2538 // Check if the result fits in a smi.
2539 __ add(scratch1, r2, Operand(0x40000000), SetCC);
2540 // If not try to return a heap number. (We know the result is an int32.)
2541 __ b(mi, &return_heap_number);
2542 // Tag the result and return.
2543 __ SmiTag(r0, r2);
2544 __ Ret();
2545
2546 __ bind(&return_heap_number);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002547 heap_number_result = r5;
2548 GenerateHeapResultAllocation(masm,
2549 heap_number_result,
2550 heap_number_map,
2551 scratch1,
2552 scratch2,
2553 &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002554
Ben Murdoch8b112d22011-06-08 16:22:53 +01002555 if (CpuFeatures::IsSupported(VFP3)) {
2556 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002557 if (op_ != Token::SHR) {
2558 // Convert the result to a floating point value.
2559 __ vmov(double_scratch.low(), r2);
2560 __ vcvt_f64_s32(double_scratch, double_scratch.low());
2561 } else {
2562 // The result must be interpreted as an unsigned 32-bit integer.
2563 __ vmov(double_scratch.low(), r2);
2564 __ vcvt_f64_u32(double_scratch, double_scratch.low());
2565 }
2566
2567 // Store the result.
2568 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
2569 __ vstr(double_scratch, r0, HeapNumber::kValueOffset);
2570 __ mov(r0, heap_number_result);
2571 __ Ret();
2572 } else {
2573 // Tail call that writes the int32 in r2 to the heap number in r0, using
2574 // r3 as scratch. r0 is preserved and returned.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002575 __ mov(r0, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002576 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
2577 __ TailCallStub(&stub);
2578 }
2579
2580 break;
2581 }
2582
2583 default:
2584 UNREACHABLE();
2585 }
2586
2587 if (transition.is_linked()) {
2588 __ bind(&transition);
2589 GenerateTypeTransition(masm);
2590 }
2591
2592 __ bind(&call_runtime);
2593 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01002594}
2595
2596
Steve Block44f0eee2011-05-26 01:26:41 +01002597void TypeRecordingBinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
2598 Label call_runtime;
2599
2600 if (op_ == Token::ADD) {
2601 // Handle string addition here, because it is the only operation
2602 // that does not do a ToNumber conversion on the operands.
2603 GenerateAddStrings(masm);
2604 }
2605
2606 // Convert oddball arguments to numbers.
2607 Label check, done;
2608 __ CompareRoot(r1, Heap::kUndefinedValueRootIndex);
2609 __ b(ne, &check);
2610 if (Token::IsBitOp(op_)) {
2611 __ mov(r1, Operand(Smi::FromInt(0)));
2612 } else {
2613 __ LoadRoot(r1, Heap::kNanValueRootIndex);
2614 }
2615 __ jmp(&done);
2616 __ bind(&check);
2617 __ CompareRoot(r0, Heap::kUndefinedValueRootIndex);
2618 __ b(ne, &done);
2619 if (Token::IsBitOp(op_)) {
2620 __ mov(r0, Operand(Smi::FromInt(0)));
2621 } else {
2622 __ LoadRoot(r0, Heap::kNanValueRootIndex);
2623 }
2624 __ bind(&done);
2625
2626 GenerateHeapNumberStub(masm);
2627}
2628
2629
Steve Block1e0659c2011-05-24 12:43:12 +01002630void TypeRecordingBinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01002631 Label call_runtime;
2632 GenerateFPOperation(masm, false, &call_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002633
2634 __ bind(&call_runtime);
2635 GenerateCallRuntime(masm);
2636}
2637
2638
2639void TypeRecordingBinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002640 Label call_runtime, call_string_add_or_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01002641
Ben Murdoch8b112d22011-06-08 16:22:53 +01002642 GenerateSmiCode(masm, &call_runtime, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002643
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002644 GenerateFPOperation(masm, false, &call_string_add_or_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002645
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002646 __ bind(&call_string_add_or_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002647 if (op_ == Token::ADD) {
2648 GenerateAddStrings(masm);
2649 }
2650
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002651 __ bind(&call_runtime);
2652 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01002653}
2654
2655
2656void TypeRecordingBinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
2657 ASSERT(op_ == Token::ADD);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002658 Label left_not_string, call_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01002659
2660 Register left = r1;
2661 Register right = r0;
Steve Block1e0659c2011-05-24 12:43:12 +01002662
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002663 // Check if left argument is a string.
2664 __ JumpIfSmi(left, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002665 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002666 __ b(ge, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002667
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002668 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2669 GenerateRegisterArgsPush(masm);
2670 __ TailCallStub(&string_add_left_stub);
2671
2672 // Left operand is not a string, test right.
2673 __ bind(&left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002674 __ JumpIfSmi(right, &call_runtime);
2675 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
2676 __ b(ge, &call_runtime);
2677
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002678 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
Steve Block1e0659c2011-05-24 12:43:12 +01002679 GenerateRegisterArgsPush(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002680 __ TailCallStub(&string_add_right_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01002681
2682 // At least one argument is not a string.
2683 __ bind(&call_runtime);
2684}
2685
2686
2687void TypeRecordingBinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) {
2688 GenerateRegisterArgsPush(masm);
2689 switch (op_) {
2690 case Token::ADD:
2691 __ InvokeBuiltin(Builtins::ADD, JUMP_JS);
2692 break;
2693 case Token::SUB:
2694 __ InvokeBuiltin(Builtins::SUB, JUMP_JS);
2695 break;
2696 case Token::MUL:
2697 __ InvokeBuiltin(Builtins::MUL, JUMP_JS);
2698 break;
2699 case Token::DIV:
2700 __ InvokeBuiltin(Builtins::DIV, JUMP_JS);
2701 break;
2702 case Token::MOD:
2703 __ InvokeBuiltin(Builtins::MOD, JUMP_JS);
2704 break;
2705 case Token::BIT_OR:
2706 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_JS);
2707 break;
2708 case Token::BIT_AND:
2709 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_JS);
2710 break;
2711 case Token::BIT_XOR:
2712 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_JS);
2713 break;
2714 case Token::SAR:
2715 __ InvokeBuiltin(Builtins::SAR, JUMP_JS);
2716 break;
2717 case Token::SHR:
2718 __ InvokeBuiltin(Builtins::SHR, JUMP_JS);
2719 break;
2720 case Token::SHL:
2721 __ InvokeBuiltin(Builtins::SHL, JUMP_JS);
2722 break;
2723 default:
2724 UNREACHABLE();
2725 }
2726}
2727
2728
2729void TypeRecordingBinaryOpStub::GenerateHeapResultAllocation(
2730 MacroAssembler* masm,
2731 Register result,
2732 Register heap_number_map,
2733 Register scratch1,
2734 Register scratch2,
2735 Label* gc_required) {
2736
2737 // Code below will scratch result if allocation fails. To keep both arguments
2738 // intact for the runtime call result cannot be one of these.
2739 ASSERT(!result.is(r0) && !result.is(r1));
2740
2741 if (mode_ == OVERWRITE_LEFT || mode_ == OVERWRITE_RIGHT) {
2742 Label skip_allocation, allocated;
2743 Register overwritable_operand = mode_ == OVERWRITE_LEFT ? r1 : r0;
2744 // If the overwritable operand is already an object, we skip the
2745 // allocation of a heap number.
2746 __ JumpIfNotSmi(overwritable_operand, &skip_allocation);
2747 // Allocate a heap number for the result.
2748 __ AllocateHeapNumber(
2749 result, scratch1, scratch2, heap_number_map, gc_required);
2750 __ b(&allocated);
2751 __ bind(&skip_allocation);
2752 // Use object holding the overwritable operand for result.
2753 __ mov(result, Operand(overwritable_operand));
2754 __ bind(&allocated);
2755 } else {
2756 ASSERT(mode_ == NO_OVERWRITE);
2757 __ AllocateHeapNumber(
2758 result, scratch1, scratch2, heap_number_map, gc_required);
2759 }
2760}
2761
2762
2763void TypeRecordingBinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
2764 __ Push(r1, r0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002765}
2766
2767
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002768void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002769 // Untagged case: double input in d2, double result goes
2770 // into d2.
2771 // Tagged case: tagged input on top of stack and in r0,
2772 // tagged result (heap number) goes into r0.
2773
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002774 Label input_not_smi;
2775 Label loaded;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002776 Label calculate;
2777 Label invalid_cache;
2778 const Register scratch0 = r9;
2779 const Register scratch1 = r7;
2780 const Register cache_entry = r0;
2781 const bool tagged = (argument_type_ == TAGGED);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002782
Ben Murdoch8b112d22011-06-08 16:22:53 +01002783 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002784 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002785 if (tagged) {
2786 // Argument is a number and is on stack and in r0.
2787 // Load argument and check if it is a smi.
2788 __ JumpIfNotSmi(r0, &input_not_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002789
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002790 // Input is a smi. Convert to double and load the low and high words
2791 // of the double into r2, r3.
2792 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
2793 __ b(&loaded);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002794
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002795 __ bind(&input_not_smi);
2796 // Check if input is a HeapNumber.
2797 __ CheckMap(r0,
2798 r1,
2799 Heap::kHeapNumberMapRootIndex,
2800 &calculate,
2801 true);
2802 // Input is a HeapNumber. Load it to a double register and store the
2803 // low and high words into r2, r3.
2804 __ vldr(d0, FieldMemOperand(r0, HeapNumber::kValueOffset));
2805 __ vmov(r2, r3, d0);
2806 } else {
2807 // Input is untagged double in d2. Output goes to d2.
2808 __ vmov(r2, r3, d2);
2809 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002810 __ bind(&loaded);
2811 // r2 = low 32 bits of double value
2812 // r3 = high 32 bits of double value
2813 // Compute hash (the shifts are arithmetic):
2814 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2815 __ eor(r1, r2, Operand(r3));
2816 __ eor(r1, r1, Operand(r1, ASR, 16));
2817 __ eor(r1, r1, Operand(r1, ASR, 8));
Steve Block44f0eee2011-05-26 01:26:41 +01002818 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
2819 __ And(r1, r1, Operand(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002820
2821 // r2 = low 32 bits of double value.
2822 // r3 = high 32 bits of double value.
2823 // r1 = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +01002824 Isolate* isolate = masm->isolate();
2825 ExternalReference cache_array =
2826 ExternalReference::transcendental_cache_array_address(isolate);
2827 __ mov(cache_entry, Operand(cache_array));
2828 // cache_entry points to cache array.
2829 int cache_array_index
2830 = type_ * sizeof(isolate->transcendental_cache()->caches_[0]);
2831 __ ldr(cache_entry, MemOperand(cache_entry, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002832 // r0 points to the cache for the type type_.
2833 // If NULL, the cache hasn't been initialized yet, so go through runtime.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002834 __ cmp(cache_entry, Operand(0, RelocInfo::NONE));
2835 __ b(eq, &invalid_cache);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002836
2837#ifdef DEBUG
2838 // Check that the layout of cache elements match expectations.
Steve Block44f0eee2011-05-26 01:26:41 +01002839 { TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002840 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2841 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2842 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2843 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2844 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2845 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2846 CHECK_EQ(0, elem_in0 - elem_start);
2847 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2848 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2849 }
2850#endif
2851
2852 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
2853 __ add(r1, r1, Operand(r1, LSL, 1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002854 __ add(cache_entry, cache_entry, Operand(r1, LSL, 2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002855 // Check if cache matches: Double value is stored in uint32_t[2] array.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002856 __ ldm(ia, cache_entry, r4.bit() | r5.bit() | r6.bit());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002857 __ cmp(r2, r4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002858 __ b(ne, &calculate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002859 __ cmp(r3, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002860 __ b(ne, &calculate);
2861 // Cache hit. Load result, cleanup and return.
2862 if (tagged) {
2863 // Pop input value from stack and load result into r0.
2864 __ pop();
2865 __ mov(r0, Operand(r6));
2866 } else {
2867 // Load result into d2.
2868 __ vldr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
2869 }
2870 __ Ret();
Ben Murdoch8b112d22011-06-08 16:22:53 +01002871 } // if (CpuFeatures::IsSupported(VFP3))
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002872
2873 __ bind(&calculate);
2874 if (tagged) {
2875 __ bind(&invalid_cache);
Steve Block44f0eee2011-05-26 01:26:41 +01002876 ExternalReference runtime_function =
2877 ExternalReference(RuntimeFunction(), masm->isolate());
2878 __ TailCallExternalReference(runtime_function, 1, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002879 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002880 if (!CpuFeatures::IsSupported(VFP3)) UNREACHABLE();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002881 CpuFeatures::Scope scope(VFP3);
2882
2883 Label no_update;
2884 Label skip_cache;
2885 const Register heap_number_map = r5;
2886
2887 // Call C function to calculate the result and update the cache.
2888 // Register r0 holds precalculated cache entry address; preserve
2889 // it on the stack and pop it into register cache_entry after the
2890 // call.
2891 __ push(cache_entry);
2892 GenerateCallCFunction(masm, scratch0);
2893 __ GetCFunctionDoubleResult(d2);
2894
2895 // Try to update the cache. If we cannot allocate a
2896 // heap number, we return the result without updating.
2897 __ pop(cache_entry);
2898 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
2899 __ AllocateHeapNumber(r6, scratch0, scratch1, r5, &no_update);
2900 __ vstr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
2901 __ stm(ia, cache_entry, r2.bit() | r3.bit() | r6.bit());
2902 __ Ret();
2903
2904 __ bind(&invalid_cache);
2905 // The cache is invalid. Call runtime which will recreate the
2906 // cache.
2907 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
2908 __ AllocateHeapNumber(r0, scratch0, scratch1, r5, &skip_cache);
2909 __ vstr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
2910 __ EnterInternalFrame();
2911 __ push(r0);
2912 __ CallRuntime(RuntimeFunction(), 1);
2913 __ LeaveInternalFrame();
2914 __ vldr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
2915 __ Ret();
2916
2917 __ bind(&skip_cache);
2918 // Call C function to calculate the result and answer directly
2919 // without updating the cache.
2920 GenerateCallCFunction(masm, scratch0);
2921 __ GetCFunctionDoubleResult(d2);
2922 __ bind(&no_update);
2923
2924 // We return the value in d2 without adding it to the cache, but
2925 // we cause a scavenging GC so that future allocations will succeed.
2926 __ EnterInternalFrame();
2927
2928 // Allocate an aligned object larger than a HeapNumber.
2929 ASSERT(4 * kPointerSize >= HeapNumber::kSize);
2930 __ mov(scratch0, Operand(4 * kPointerSize));
2931 __ push(scratch0);
2932 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2933 __ LeaveInternalFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002934 __ Ret();
2935 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002936}
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002937
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002938
2939void TranscendentalCacheStub::GenerateCallCFunction(MacroAssembler* masm,
2940 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01002941 Isolate* isolate = masm->isolate();
2942
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002943 __ push(lr);
2944 __ PrepareCallCFunction(2, scratch);
2945 __ vmov(r0, r1, d2);
2946 switch (type_) {
2947 case TranscendentalCache::SIN:
Steve Block44f0eee2011-05-26 01:26:41 +01002948 __ CallCFunction(ExternalReference::math_sin_double_function(isolate), 2);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002949 break;
2950 case TranscendentalCache::COS:
Steve Block44f0eee2011-05-26 01:26:41 +01002951 __ CallCFunction(ExternalReference::math_cos_double_function(isolate), 2);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002952 break;
2953 case TranscendentalCache::LOG:
Steve Block44f0eee2011-05-26 01:26:41 +01002954 __ CallCFunction(ExternalReference::math_log_double_function(isolate), 2);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002955 break;
2956 default:
2957 UNIMPLEMENTED();
2958 break;
2959 }
2960 __ pop(lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002961}
2962
2963
2964Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2965 switch (type_) {
2966 // Add more cases when necessary.
2967 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2968 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002969 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002970 default:
2971 UNIMPLEMENTED();
2972 return Runtime::kAbort;
2973 }
2974}
2975
2976
2977void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01002978 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002979}
2980
2981
2982void GenericUnaryOpStub::Generate(MacroAssembler* masm) {
2983 Label slow, done;
2984
2985 Register heap_number_map = r6;
2986 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2987
2988 if (op_ == Token::SUB) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002989 if (include_smi_code_) {
2990 // Check whether the value is a smi.
2991 Label try_float;
2992 __ tst(r0, Operand(kSmiTagMask));
2993 __ b(ne, &try_float);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002994
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002995 // Go slow case if the value of the expression is zero
2996 // to make sure that we switch between 0 and -0.
2997 if (negative_zero_ == kStrictNegativeZero) {
2998 // If we have to check for zero, then we can check for the max negative
2999 // smi while we are at it.
3000 __ bic(ip, r0, Operand(0x80000000), SetCC);
3001 __ b(eq, &slow);
3002 __ rsb(r0, r0, Operand(0, RelocInfo::NONE));
3003 __ Ret();
3004 } else {
3005 // The value of the expression is a smi and 0 is OK for -0. Try
3006 // optimistic subtraction '0 - value'.
3007 __ rsb(r0, r0, Operand(0, RelocInfo::NONE), SetCC);
3008 __ Ret(vc);
3009 // We don't have to reverse the optimistic neg since the only case
3010 // where we fall through is the minimum negative Smi, which is the case
3011 // where the neg leaves the register unchanged.
3012 __ jmp(&slow); // Go slow on max negative Smi.
3013 }
3014 __ bind(&try_float);
3015 } else if (FLAG_debug_code) {
3016 __ tst(r0, Operand(kSmiTagMask));
3017 __ Assert(ne, "Unexpected smi operand.");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003018 }
3019
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003020 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3021 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
3022 __ cmp(r1, heap_number_map);
3023 __ b(ne, &slow);
3024 // r0 is a heap number. Get a new heap number in r1.
3025 if (overwrite_ == UNARY_OVERWRITE) {
3026 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
3027 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
3028 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
3029 } else {
3030 __ AllocateHeapNumber(r1, r2, r3, r6, &slow);
3031 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
3032 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
3033 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
3034 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
3035 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
3036 __ mov(r0, Operand(r1));
3037 }
3038 } else if (op_ == Token::BIT_NOT) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003039 if (include_smi_code_) {
3040 Label non_smi;
Steve Block1e0659c2011-05-24 12:43:12 +01003041 __ JumpIfNotSmi(r0, &non_smi);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003042 __ mvn(r0, Operand(r0));
3043 // Bit-clear inverted smi-tag.
3044 __ bic(r0, r0, Operand(kSmiTagMask));
3045 __ Ret();
3046 __ bind(&non_smi);
3047 } else if (FLAG_debug_code) {
3048 __ tst(r0, Operand(kSmiTagMask));
3049 __ Assert(ne, "Unexpected smi operand.");
3050 }
3051
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003052 // Check if the operand is a heap number.
3053 __ ldr(r1, FieldMemOperand(r0, HeapObject::kMapOffset));
3054 __ AssertRegisterIsRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
3055 __ cmp(r1, heap_number_map);
3056 __ b(ne, &slow);
3057
3058 // Convert the heap number is r0 to an untagged integer in r1.
Steve Block1e0659c2011-05-24 12:43:12 +01003059 __ ConvertToInt32(r0, r1, r2, r3, d0, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003060
3061 // Do the bitwise operation (move negated) and check if the result
3062 // fits in a smi.
3063 Label try_float;
3064 __ mvn(r1, Operand(r1));
3065 __ add(r2, r1, Operand(0x40000000), SetCC);
3066 __ b(mi, &try_float);
3067 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
3068 __ b(&done);
3069
3070 __ bind(&try_float);
3071 if (!overwrite_ == UNARY_OVERWRITE) {
3072 // Allocate a fresh heap number, but don't overwrite r0 until
3073 // we're sure we can do it without going through the slow case
3074 // that needs the value in r0.
3075 __ AllocateHeapNumber(r2, r3, r4, r6, &slow);
3076 __ mov(r0, Operand(r2));
3077 }
3078
Ben Murdoch8b112d22011-06-08 16:22:53 +01003079 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003080 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
3081 CpuFeatures::Scope scope(VFP3);
3082 __ vmov(s0, r1);
3083 __ vcvt_f64_s32(d0, s0);
3084 __ sub(r2, r0, Operand(kHeapObjectTag));
3085 __ vstr(d0, r2, HeapNumber::kValueOffset);
3086 } else {
3087 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
3088 // have to set up a frame.
3089 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
3090 __ push(lr);
3091 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
3092 __ pop(lr);
3093 }
3094 } else {
3095 UNIMPLEMENTED();
3096 }
3097
3098 __ bind(&done);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003099 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003100
3101 // Handle the slow case by jumping to the JavaScript builtin.
3102 __ bind(&slow);
3103 __ push(r0);
3104 switch (op_) {
3105 case Token::SUB:
3106 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_JS);
3107 break;
3108 case Token::BIT_NOT:
3109 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_JS);
3110 break;
3111 default:
3112 UNREACHABLE();
3113 }
3114}
3115
3116
Steve Block44f0eee2011-05-26 01:26:41 +01003117void MathPowStub::Generate(MacroAssembler* masm) {
3118 Label call_runtime;
3119
Ben Murdoch8b112d22011-06-08 16:22:53 +01003120 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003121 CpuFeatures::Scope scope(VFP3);
3122
3123 Label base_not_smi;
3124 Label exponent_not_smi;
3125 Label convert_exponent;
3126
3127 const Register base = r0;
3128 const Register exponent = r1;
3129 const Register heapnumbermap = r5;
3130 const Register heapnumber = r6;
3131 const DoubleRegister double_base = d0;
3132 const DoubleRegister double_exponent = d1;
3133 const DoubleRegister double_result = d2;
3134 const SwVfpRegister single_scratch = s0;
3135 const Register scratch = r9;
3136 const Register scratch2 = r7;
3137
3138 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
3139 __ ldr(base, MemOperand(sp, 1 * kPointerSize));
3140 __ ldr(exponent, MemOperand(sp, 0 * kPointerSize));
3141
3142 // Convert base to double value and store it in d0.
3143 __ JumpIfNotSmi(base, &base_not_smi);
3144 // Base is a Smi. Untag and convert it.
3145 __ SmiUntag(base);
3146 __ vmov(single_scratch, base);
3147 __ vcvt_f64_s32(double_base, single_scratch);
3148 __ b(&convert_exponent);
3149
3150 __ bind(&base_not_smi);
3151 __ ldr(scratch, FieldMemOperand(base, JSObject::kMapOffset));
3152 __ cmp(scratch, heapnumbermap);
3153 __ b(ne, &call_runtime);
3154 // Base is a heapnumber. Load it into double register.
3155 __ vldr(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
3156
3157 __ bind(&convert_exponent);
3158 __ JumpIfNotSmi(exponent, &exponent_not_smi);
3159 __ SmiUntag(exponent);
3160
3161 // The base is in a double register and the exponent is
3162 // an untagged smi. Allocate a heap number and call a
3163 // C function for integer exponents. The register containing
3164 // the heap number is callee-saved.
3165 __ AllocateHeapNumber(heapnumber,
3166 scratch,
3167 scratch2,
3168 heapnumbermap,
3169 &call_runtime);
3170 __ push(lr);
3171 __ PrepareCallCFunction(3, scratch);
3172 __ mov(r2, exponent);
3173 __ vmov(r0, r1, double_base);
3174 __ CallCFunction(
3175 ExternalReference::power_double_int_function(masm->isolate()), 3);
3176 __ pop(lr);
3177 __ GetCFunctionDoubleResult(double_result);
3178 __ vstr(double_result,
3179 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3180 __ mov(r0, heapnumber);
3181 __ Ret(2 * kPointerSize);
3182
3183 __ bind(&exponent_not_smi);
3184 __ ldr(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
3185 __ cmp(scratch, heapnumbermap);
3186 __ b(ne, &call_runtime);
3187 // Exponent is a heapnumber. Load it into double register.
3188 __ vldr(double_exponent,
3189 FieldMemOperand(exponent, HeapNumber::kValueOffset));
3190
3191 // The base and the exponent are in double registers.
3192 // Allocate a heap number and call a C function for
3193 // double exponents. The register containing
3194 // the heap number is callee-saved.
3195 __ AllocateHeapNumber(heapnumber,
3196 scratch,
3197 scratch2,
3198 heapnumbermap,
3199 &call_runtime);
3200 __ push(lr);
3201 __ PrepareCallCFunction(4, scratch);
3202 __ vmov(r0, r1, double_base);
3203 __ vmov(r2, r3, double_exponent);
3204 __ CallCFunction(
3205 ExternalReference::power_double_double_function(masm->isolate()), 4);
3206 __ pop(lr);
3207 __ GetCFunctionDoubleResult(double_result);
3208 __ vstr(double_result,
3209 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3210 __ mov(r0, heapnumber);
3211 __ Ret(2 * kPointerSize);
3212 }
3213
3214 __ bind(&call_runtime);
3215 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
3216}
3217
3218
3219bool CEntryStub::NeedsImmovableCode() {
3220 return true;
3221}
3222
3223
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003224void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003225 __ Throw(r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003226}
3227
3228
3229void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3230 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003231 __ ThrowUncatchable(type, r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003232}
3233
3234
3235void CEntryStub::GenerateCore(MacroAssembler* masm,
3236 Label* throw_normal_exception,
3237 Label* throw_termination_exception,
3238 Label* throw_out_of_memory_exception,
3239 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01003240 bool always_allocate) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003241 // r0: result parameter for PerformGC, if any
3242 // r4: number of arguments including receiver (C callee-saved)
3243 // r5: pointer to builtin function (C callee-saved)
3244 // r6: pointer to the first argument (C callee-saved)
Steve Block44f0eee2011-05-26 01:26:41 +01003245 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003246
3247 if (do_gc) {
3248 // Passing r0.
3249 __ PrepareCallCFunction(1, r1);
Steve Block44f0eee2011-05-26 01:26:41 +01003250 __ CallCFunction(ExternalReference::perform_gc_function(isolate), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003251 }
3252
3253 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01003254 ExternalReference::heap_always_allocate_scope_depth(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003255 if (always_allocate) {
3256 __ mov(r0, Operand(scope_depth));
3257 __ ldr(r1, MemOperand(r0));
3258 __ add(r1, r1, Operand(1));
3259 __ str(r1, MemOperand(r0));
3260 }
3261
3262 // Call C built-in.
3263 // r0 = argc, r1 = argv
3264 __ mov(r0, Operand(r4));
3265 __ mov(r1, Operand(r6));
3266
Steve Block1e0659c2011-05-24 12:43:12 +01003267#if defined(V8_HOST_ARCH_ARM)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003268 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
3269 int frame_alignment_mask = frame_alignment - 1;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003270 if (FLAG_debug_code) {
3271 if (frame_alignment > kPointerSize) {
3272 Label alignment_as_expected;
3273 ASSERT(IsPowerOf2(frame_alignment));
Steve Block1e0659c2011-05-24 12:43:12 +01003274 __ tst(sp, Operand(frame_alignment_mask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003275 __ b(eq, &alignment_as_expected);
3276 // Don't use Check here, as it will call Runtime_Abort re-entering here.
3277 __ stop("Unexpected alignment");
3278 __ bind(&alignment_as_expected);
3279 }
3280 }
3281#endif
3282
Steve Block44f0eee2011-05-26 01:26:41 +01003283 __ mov(r2, Operand(ExternalReference::isolate_address()));
3284
3285
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003286 // TODO(1242173): To let the GC traverse the return address of the exit
3287 // frames, we need to know where the return address is. Right now,
Steve Block1e0659c2011-05-24 12:43:12 +01003288 // we store it on the stack to be able to find it again, but we never
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003289 // restore from it in case of changes, which makes it impossible to
3290 // support moving the C entry code stub. This should be fixed, but currently
3291 // this is OK because the CEntryStub gets generated so early in the V8 boot
3292 // sequence that it is not moving ever.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003293
Steve Block1e0659c2011-05-24 12:43:12 +01003294 // Compute the return address in lr to return to after the jump below. Pc is
3295 // already at '+ 8' from the current instruction but return is after three
3296 // instructions so add another 4 to pc to get the return address.
3297 masm->add(lr, pc, Operand(4));
3298 __ str(lr, MemOperand(sp, 0));
3299 masm->Jump(r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003300
3301 if (always_allocate) {
3302 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
3303 // though (contain the result).
3304 __ mov(r2, Operand(scope_depth));
3305 __ ldr(r3, MemOperand(r2));
3306 __ sub(r3, r3, Operand(1));
3307 __ str(r3, MemOperand(r2));
3308 }
3309
3310 // check for failure result
3311 Label failure_returned;
3312 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3313 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
3314 __ add(r2, r0, Operand(1));
3315 __ tst(r2, Operand(kFailureTagMask));
3316 __ b(eq, &failure_returned);
3317
3318 // Exit C frame and return.
3319 // r0:r1: result
3320 // sp: stack pointer
3321 // fp: frame pointer
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003322 // Callee-saved register r4 still holds argc.
3323 __ LeaveExitFrame(save_doubles_, r4);
3324 __ mov(pc, lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003325
3326 // check if we should retry or throw exception
3327 Label retry;
3328 __ bind(&failure_returned);
3329 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3330 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
3331 __ b(eq, &retry);
3332
3333 // Special handling of out of memory exceptions.
3334 Failure* out_of_memory = Failure::OutOfMemoryException();
3335 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3336 __ b(eq, throw_out_of_memory_exception);
3337
3338 // Retrieve the pending exception and clear the variable.
Steve Block44f0eee2011-05-26 01:26:41 +01003339 __ mov(ip, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003340 __ ldr(r3, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +01003341 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3342 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003343 __ ldr(r0, MemOperand(ip));
3344 __ str(r3, MemOperand(ip));
3345
3346 // Special handling of termination exceptions which are uncatchable
3347 // by javascript code.
Steve Block44f0eee2011-05-26 01:26:41 +01003348 __ cmp(r0, Operand(isolate->factory()->termination_exception()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003349 __ b(eq, throw_termination_exception);
3350
3351 // Handle normal exception.
3352 __ jmp(throw_normal_exception);
3353
3354 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
3355}
3356
3357
3358void CEntryStub::Generate(MacroAssembler* masm) {
3359 // Called from JavaScript; parameters are on stack as if calling JS function
3360 // r0: number of arguments including receiver
3361 // r1: pointer to builtin function
3362 // fp: frame pointer (restored after C call)
3363 // sp: stack pointer (restored as callee's sp after C call)
3364 // cp: current context (C callee-saved)
3365
3366 // Result returned in r0 or r0+r1 by default.
3367
3368 // NOTE: Invocations of builtins may return failure objects
3369 // instead of a proper result. The builtin entry handles
3370 // this by performing a garbage collection and retrying the
3371 // builtin once.
3372
Steve Block1e0659c2011-05-24 12:43:12 +01003373 // Compute the argv pointer in a callee-saved register.
3374 __ add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
3375 __ sub(r6, r6, Operand(kPointerSize));
3376
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003377 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003378 __ EnterExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003379
Steve Block1e0659c2011-05-24 12:43:12 +01003380 // Setup argc and the builtin function in callee-saved registers.
3381 __ mov(r4, Operand(r0));
3382 __ mov(r5, Operand(r1));
3383
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003384 // r4: number of arguments (C callee-saved)
3385 // r5: pointer to builtin function (C callee-saved)
3386 // r6: pointer to first argument (C callee-saved)
3387
3388 Label throw_normal_exception;
3389 Label throw_termination_exception;
3390 Label throw_out_of_memory_exception;
3391
3392 // Call into the runtime system.
3393 GenerateCore(masm,
3394 &throw_normal_exception,
3395 &throw_termination_exception,
3396 &throw_out_of_memory_exception,
3397 false,
Steve Block1e0659c2011-05-24 12:43:12 +01003398 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003399
3400 // Do space-specific GC and retry runtime call.
3401 GenerateCore(masm,
3402 &throw_normal_exception,
3403 &throw_termination_exception,
3404 &throw_out_of_memory_exception,
3405 true,
Steve Block1e0659c2011-05-24 12:43:12 +01003406 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003407
3408 // Do full GC and retry runtime call one final time.
3409 Failure* failure = Failure::InternalError();
3410 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
3411 GenerateCore(masm,
3412 &throw_normal_exception,
3413 &throw_termination_exception,
3414 &throw_out_of_memory_exception,
3415 true,
Steve Block1e0659c2011-05-24 12:43:12 +01003416 true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003417
3418 __ bind(&throw_out_of_memory_exception);
3419 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3420
3421 __ bind(&throw_termination_exception);
3422 GenerateThrowUncatchable(masm, TERMINATION);
3423
3424 __ bind(&throw_normal_exception);
3425 GenerateThrowTOS(masm);
3426}
3427
3428
3429void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3430 // r0: code entry
3431 // r1: function
3432 // r2: receiver
3433 // r3: argc
3434 // [sp+0]: argv
3435
3436 Label invoke, exit;
3437
3438 // Called from C, so do not pop argc and args on exit (preserve sp)
3439 // No need to save register-passed args
3440 // Save callee-saved registers (incl. cp and fp), sp, and lr
3441 __ stm(db_w, sp, kCalleeSaved | lr.bit());
3442
3443 // Get address of argv, see stm above.
3444 // r0: code entry
3445 // r1: function
3446 // r2: receiver
3447 // r3: argc
3448 __ ldr(r4, MemOperand(sp, (kNumCalleeSaved + 1) * kPointerSize)); // argv
3449
3450 // Push a frame with special values setup to mark it as an entry frame.
3451 // r0: code entry
3452 // r1: function
3453 // r2: receiver
3454 // r3: argc
3455 // r4: argv
Steve Block44f0eee2011-05-26 01:26:41 +01003456 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003457 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
3458 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3459 __ mov(r7, Operand(Smi::FromInt(marker)));
3460 __ mov(r6, Operand(Smi::FromInt(marker)));
Steve Block44f0eee2011-05-26 01:26:41 +01003461 __ mov(r5,
3462 Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003463 __ ldr(r5, MemOperand(r5));
3464 __ Push(r8, r7, r6, r5);
3465
3466 // Setup frame pointer for the frame to be pushed.
3467 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3468
Ben Murdochb0fe1622011-05-05 13:52:32 +01003469#ifdef ENABLE_LOGGING_AND_PROFILING
3470 // If this is the outermost JS call, set js_entry_sp value.
Steve Block44f0eee2011-05-26 01:26:41 +01003471 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address, isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003472 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
3473 __ ldr(r6, MemOperand(r5));
3474 __ cmp(r6, Operand(0, RelocInfo::NONE));
3475 __ str(fp, MemOperand(r5), eq);
3476#endif
3477
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003478 // Call a faked try-block that does the invoke.
3479 __ bl(&invoke);
3480
3481 // Caught exception: Store result (exception) in the pending
3482 // exception field in the JSEnv and return a failure sentinel.
3483 // Coming in here the fp will be invalid because the PushTryHandler below
3484 // sets it to 0 to signal the existence of the JSEntry frame.
Steve Block44f0eee2011-05-26 01:26:41 +01003485 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3486 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003487 __ str(r0, MemOperand(ip));
3488 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
3489 __ b(&exit);
3490
3491 // Invoke: Link this frame into the handler chain.
3492 __ bind(&invoke);
3493 // Must preserve r0-r4, r5-r7 are available.
3494 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
3495 // If an exception not caught by another handler occurs, this handler
3496 // returns control to the code after the bl(&invoke) above, which
3497 // restores all kCalleeSaved registers (including cp and fp) to their
3498 // saved values before returning a failure to C.
3499
3500 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01003501 __ mov(ip, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003502 __ ldr(r5, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +01003503 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3504 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003505 __ str(r5, MemOperand(ip));
3506
3507 // Invoke the function by calling through JS entry trampoline builtin.
3508 // Notice that we cannot store a reference to the trampoline code directly in
3509 // this stub, because runtime stubs are not traversed when doing GC.
3510
3511 // Expected registers by Builtins::JSEntryTrampoline
3512 // r0: code entry
3513 // r1: function
3514 // r2: receiver
3515 // r3: argc
3516 // r4: argv
3517 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01003518 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
3519 isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003520 __ mov(ip, Operand(construct_entry));
3521 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003522 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003523 __ mov(ip, Operand(entry));
3524 }
3525 __ ldr(ip, MemOperand(ip)); // deref address
3526
3527 // Branch and link to JSEntryTrampoline. We don't use the double underscore
3528 // macro for the add instruction because we don't want the coverage tool
3529 // inserting instructions here after we read the pc.
3530 __ mov(lr, Operand(pc));
3531 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
3532
3533 // Unlink this frame from the handler chain. When reading the
3534 // address of the next handler, there is no need to use the address
3535 // displacement since the current stack pointer (sp) points directly
3536 // to the stack handler.
3537 __ ldr(r3, MemOperand(sp, StackHandlerConstants::kNextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01003538 __ mov(ip, Operand(ExternalReference(Isolate::k_handler_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003539 __ str(r3, MemOperand(ip));
3540 // No need to restore registers
3541 __ add(sp, sp, Operand(StackHandlerConstants::kSize));
3542
Ben Murdochb0fe1622011-05-05 13:52:32 +01003543#ifdef ENABLE_LOGGING_AND_PROFILING
3544 // If current FP value is the same as js_entry_sp value, it means that
3545 // the current function is the outermost.
3546 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
3547 __ ldr(r6, MemOperand(r5));
3548 __ cmp(fp, Operand(r6));
3549 __ mov(r6, Operand(0, RelocInfo::NONE), LeaveCC, eq);
3550 __ str(r6, MemOperand(r5), eq);
3551#endif
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003552
3553 __ bind(&exit); // r0 holds result
3554 // Restore the top frame descriptors from the stack.
3555 __ pop(r3);
Steve Block44f0eee2011-05-26 01:26:41 +01003556 __ mov(ip,
3557 Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003558 __ str(r3, MemOperand(ip));
3559
3560 // Reset the stack to the callee saved registers.
3561 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3562
3563 // Restore callee-saved registers and return.
3564#ifdef DEBUG
3565 if (FLAG_debug_code) {
3566 __ mov(lr, Operand(pc));
3567 }
3568#endif
3569 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
3570}
3571
3572
Steve Block1e0659c2011-05-24 12:43:12 +01003573// Uses registers r0 to r4.
3574// Expected input (depending on whether args are in registers or on the stack):
3575// * object: r0 or at sp + 1 * kPointerSize.
3576// * function: r1 or at sp.
3577//
3578// An inlined call site may have been generated before calling this stub.
3579// In this case the offset to the inline site to patch is passed on the stack,
3580// in the safepoint slot for register r4.
3581// (See LCodeGen::DoInstanceOfKnownGlobal)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003582void InstanceofStub::Generate(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01003583 // Call site inlining and patching implies arguments in registers.
3584 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
3585 // ReturnTrueFalse is only implemented for inlined call sites.
3586 ASSERT(!ReturnTrueFalseObject() || HasCallSiteInlineCheck());
3587
Ben Murdochb0fe1622011-05-05 13:52:32 +01003588 // Fixed register usage throughout the stub:
Steve Block9fac8402011-05-12 15:51:54 +01003589 const Register object = r0; // Object (lhs).
Steve Block1e0659c2011-05-24 12:43:12 +01003590 Register map = r3; // Map of the object.
Steve Block9fac8402011-05-12 15:51:54 +01003591 const Register function = r1; // Function (rhs).
Ben Murdochb0fe1622011-05-05 13:52:32 +01003592 const Register prototype = r4; // Prototype of the function.
Steve Block1e0659c2011-05-24 12:43:12 +01003593 const Register inline_site = r9;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003594 const Register scratch = r2;
Steve Block1e0659c2011-05-24 12:43:12 +01003595
3596 const int32_t kDeltaToLoadBoolResult = 3 * kPointerSize;
3597
Ben Murdochb0fe1622011-05-05 13:52:32 +01003598 Label slow, loop, is_instance, is_not_instance, not_js_object;
Steve Block1e0659c2011-05-24 12:43:12 +01003599
Ben Murdoch086aeea2011-05-13 15:57:08 +01003600 if (!HasArgsInRegisters()) {
Steve Block9fac8402011-05-12 15:51:54 +01003601 __ ldr(object, MemOperand(sp, 1 * kPointerSize));
3602 __ ldr(function, MemOperand(sp, 0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003603 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003604
Ben Murdochb0fe1622011-05-05 13:52:32 +01003605 // Check that the left hand is a JS object and load map.
Steve Block1e0659c2011-05-24 12:43:12 +01003606 __ JumpIfSmi(object, &not_js_object);
Steve Block9fac8402011-05-12 15:51:54 +01003607 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003608
Steve Block1e0659c2011-05-24 12:43:12 +01003609 // If there is a call site cache don't look in the global cache, but do the
3610 // real lookup and update the call site cache.
3611 if (!HasCallSiteInlineCheck()) {
3612 Label miss;
3613 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
3614 __ cmp(function, ip);
3615 __ b(ne, &miss);
3616 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
3617 __ cmp(map, ip);
3618 __ b(ne, &miss);
3619 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3620 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003621
Steve Block1e0659c2011-05-24 12:43:12 +01003622 __ bind(&miss);
3623 }
3624
3625 // Get the prototype of the function.
Steve Block9fac8402011-05-12 15:51:54 +01003626 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003627
3628 // Check that the function prototype is a JS object.
Steve Block1e0659c2011-05-24 12:43:12 +01003629 __ JumpIfSmi(prototype, &slow);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003630 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003631
Steve Block1e0659c2011-05-24 12:43:12 +01003632 // Update the global instanceof or call site inlined cache with the current
3633 // map and function. The cached answer will be set when it is known below.
3634 if (!HasCallSiteInlineCheck()) {
3635 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
3636 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
3637 } else {
3638 ASSERT(HasArgsInRegisters());
3639 // Patch the (relocated) inlined map check.
3640
3641 // The offset was stored in r4 safepoint slot.
3642 // (See LCodeGen::DoDeferredLInstanceOfKnownGlobal)
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003643 __ LoadFromSafepointRegisterSlot(scratch, r4);
Steve Block1e0659c2011-05-24 12:43:12 +01003644 __ sub(inline_site, lr, scratch);
3645 // Get the map location in scratch and patch it.
3646 __ GetRelocatedValueLocation(inline_site, scratch);
3647 __ str(map, MemOperand(scratch));
3648 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003649
3650 // Register mapping: r3 is object map and r4 is function prototype.
3651 // Get prototype of object into r2.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003652 __ ldr(scratch, FieldMemOperand(map, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003653
Steve Block1e0659c2011-05-24 12:43:12 +01003654 // We don't need map any more. Use it as a scratch register.
3655 Register scratch2 = map;
3656 map = no_reg;
3657
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003658 // Loop through the prototype chain looking for the function prototype.
Steve Block1e0659c2011-05-24 12:43:12 +01003659 __ LoadRoot(scratch2, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003660 __ bind(&loop);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003661 __ cmp(scratch, Operand(prototype));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003662 __ b(eq, &is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003663 __ cmp(scratch, scratch2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003664 __ b(eq, &is_not_instance);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003665 __ ldr(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
3666 __ ldr(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003667 __ jmp(&loop);
3668
3669 __ bind(&is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003670 if (!HasCallSiteInlineCheck()) {
3671 __ mov(r0, Operand(Smi::FromInt(0)));
3672 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3673 } else {
3674 // Patch the call site to return true.
3675 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
3676 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
3677 // Get the boolean result location in scratch and patch it.
3678 __ GetRelocatedValueLocation(inline_site, scratch);
3679 __ str(r0, MemOperand(scratch));
3680
3681 if (!ReturnTrueFalseObject()) {
3682 __ mov(r0, Operand(Smi::FromInt(0)));
3683 }
3684 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01003685 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003686
3687 __ bind(&is_not_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003688 if (!HasCallSiteInlineCheck()) {
3689 __ mov(r0, Operand(Smi::FromInt(1)));
3690 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3691 } else {
3692 // Patch the call site to return false.
3693 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
3694 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
3695 // Get the boolean result location in scratch and patch it.
3696 __ GetRelocatedValueLocation(inline_site, scratch);
3697 __ str(r0, MemOperand(scratch));
3698
3699 if (!ReturnTrueFalseObject()) {
3700 __ mov(r0, Operand(Smi::FromInt(1)));
3701 }
3702 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01003703 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003704
3705 Label object_not_null, object_not_null_or_smi;
3706 __ bind(&not_js_object);
3707 // Before null, smi and string value checks, check that the rhs is a function
3708 // as for a non-function rhs an exception needs to be thrown.
Steve Block1e0659c2011-05-24 12:43:12 +01003709 __ JumpIfSmi(function, &slow);
3710 __ CompareObjectType(function, scratch2, scratch, JS_FUNCTION_TYPE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003711 __ b(ne, &slow);
3712
3713 // Null is not instance of anything.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003714 __ cmp(scratch, Operand(masm->isolate()->factory()->null_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003715 __ b(ne, &object_not_null);
3716 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003717 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003718
3719 __ bind(&object_not_null);
3720 // Smi values are not instances of anything.
Steve Block1e0659c2011-05-24 12:43:12 +01003721 __ JumpIfNotSmi(object, &object_not_null_or_smi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003722 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003723 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003724
3725 __ bind(&object_not_null_or_smi);
3726 // String values are not instances of anything.
3727 __ IsObjectJSStringType(object, scratch, &slow);
3728 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003729 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003730
3731 // Slow-case. Tail call builtin.
Ben Murdoch086aeea2011-05-13 15:57:08 +01003732 __ bind(&slow);
Steve Block1e0659c2011-05-24 12:43:12 +01003733 if (!ReturnTrueFalseObject()) {
3734 if (HasArgsInRegisters()) {
3735 __ Push(r0, r1);
3736 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003737 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_JS);
Steve Block1e0659c2011-05-24 12:43:12 +01003738 } else {
3739 __ EnterInternalFrame();
3740 __ Push(r0, r1);
3741 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_JS);
3742 __ LeaveInternalFrame();
3743 __ cmp(r0, Operand(0));
3744 __ LoadRoot(r0, Heap::kTrueValueRootIndex, eq);
3745 __ LoadRoot(r0, Heap::kFalseValueRootIndex, ne);
3746 __ Ret(HasArgsInRegisters() ? 0 : 2);
3747 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003748}
3749
3750
Steve Block1e0659c2011-05-24 12:43:12 +01003751Register InstanceofStub::left() { return r0; }
3752
3753
3754Register InstanceofStub::right() { return r1; }
3755
3756
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003757void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
3758 // The displacement is the offset of the last parameter (if any)
3759 // relative to the frame pointer.
3760 static const int kDisplacement =
3761 StandardFrameConstants::kCallerSPOffset - kPointerSize;
3762
3763 // Check that the key is a smi.
3764 Label slow;
Steve Block1e0659c2011-05-24 12:43:12 +01003765 __ JumpIfNotSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003766
3767 // Check if the calling frame is an arguments adaptor frame.
3768 Label adaptor;
3769 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3770 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3771 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3772 __ b(eq, &adaptor);
3773
3774 // Check index against formal parameters count limit passed in
3775 // through register r0. Use unsigned comparison to get negative
3776 // check for free.
3777 __ cmp(r1, r0);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003778 __ b(hs, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003779
3780 // Read the argument from the stack and return it.
3781 __ sub(r3, r0, r1);
3782 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3783 __ ldr(r0, MemOperand(r3, kDisplacement));
3784 __ Jump(lr);
3785
3786 // Arguments adaptor case: Check index against actual arguments
3787 // limit found in the arguments adaptor frame. Use unsigned
3788 // comparison to get negative check for free.
3789 __ bind(&adaptor);
3790 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3791 __ cmp(r1, r0);
3792 __ b(cs, &slow);
3793
3794 // Read the argument from the adaptor frame and return it.
3795 __ sub(r3, r0, r1);
3796 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3797 __ ldr(r0, MemOperand(r3, kDisplacement));
3798 __ Jump(lr);
3799
3800 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3801 // by calling the runtime system.
3802 __ bind(&slow);
3803 __ push(r1);
3804 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3805}
3806
3807
3808void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
3809 // sp[0] : number of parameters
3810 // sp[4] : receiver displacement
3811 // sp[8] : function
3812
3813 // Check if the calling frame is an arguments adaptor frame.
3814 Label adaptor_frame, try_allocate, runtime;
3815 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3816 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3817 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3818 __ b(eq, &adaptor_frame);
3819
3820 // Get the length from the frame.
3821 __ ldr(r1, MemOperand(sp, 0));
3822 __ b(&try_allocate);
3823
3824 // Patch the arguments.length and the parameters pointer.
3825 __ bind(&adaptor_frame);
3826 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3827 __ str(r1, MemOperand(sp, 0));
3828 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3829 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
3830 __ str(r3, MemOperand(sp, 1 * kPointerSize));
3831
3832 // Try the new space allocation. Start out with computing the size
3833 // of the arguments object and the elements array in words.
3834 Label add_arguments_object;
3835 __ bind(&try_allocate);
Iain Merrick9ac36c92010-09-13 15:29:50 +01003836 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003837 __ b(eq, &add_arguments_object);
3838 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
3839 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
3840 __ bind(&add_arguments_object);
Steve Block44f0eee2011-05-26 01:26:41 +01003841 __ add(r1, r1, Operand(GetArgumentsObjectSize() / kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003842
3843 // Do the allocation of both objects in one go.
3844 __ AllocateInNewSpace(
3845 r1,
3846 r0,
3847 r2,
3848 r3,
3849 &runtime,
3850 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
3851
3852 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003853 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
3854 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01003855 __ ldr(r4, MemOperand(r4,
3856 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003857
3858 // Copy the JS object part.
3859 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
3860
Steve Block44f0eee2011-05-26 01:26:41 +01003861 if (type_ == NEW_NON_STRICT) {
3862 // Setup the callee in-object property.
3863 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
3864 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
3865 const int kCalleeOffset = JSObject::kHeaderSize +
3866 Heap::kArgumentsCalleeIndex * kPointerSize;
3867 __ str(r3, FieldMemOperand(r0, kCalleeOffset));
3868 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003869
3870 // Get the length (smi tagged) and set that as an in-object property too.
Steve Block44f0eee2011-05-26 01:26:41 +01003871 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003872 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01003873 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize +
3874 Heap::kArgumentsLengthIndex * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003875
3876 // If there are no actual arguments, we're done.
3877 Label done;
Iain Merrick9ac36c92010-09-13 15:29:50 +01003878 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003879 __ b(eq, &done);
3880
3881 // Get the parameters pointer from the stack.
3882 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
3883
3884 // Setup the elements pointer in the allocated arguments object and
3885 // initialize the header in the elements fixed array.
Steve Block44f0eee2011-05-26 01:26:41 +01003886 __ add(r4, r0, Operand(GetArgumentsObjectSize()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003887 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
3888 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
3889 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
3890 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
3891 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
3892
3893 // Copy the fixed array slots.
3894 Label loop;
3895 // Setup r4 to point to the first array slot.
3896 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
3897 __ bind(&loop);
3898 // Pre-decrement r2 with kPointerSize on each iteration.
3899 // Pre-decrement in order to skip receiver.
3900 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
3901 // Post-increment r4 with kPointerSize on each iteration.
3902 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
3903 __ sub(r1, r1, Operand(1));
Iain Merrick9ac36c92010-09-13 15:29:50 +01003904 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003905 __ b(ne, &loop);
3906
3907 // Return and remove the on-stack parameters.
3908 __ bind(&done);
3909 __ add(sp, sp, Operand(3 * kPointerSize));
3910 __ Ret();
3911
3912 // Do the runtime call to allocate the arguments object.
3913 __ bind(&runtime);
3914 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3915}
3916
3917
3918void RegExpExecStub::Generate(MacroAssembler* masm) {
3919 // Just jump directly to runtime if native RegExp is not selected at compile
3920 // time or if regexp entry in generated code is turned off runtime switch or
3921 // at compilation.
3922#ifdef V8_INTERPRETED_REGEXP
3923 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3924#else // V8_INTERPRETED_REGEXP
3925 if (!FLAG_regexp_entry_native) {
3926 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3927 return;
3928 }
3929
3930 // Stack frame on entry.
3931 // sp[0]: last_match_info (expected JSArray)
3932 // sp[4]: previous index
3933 // sp[8]: subject string
3934 // sp[12]: JSRegExp object
3935
3936 static const int kLastMatchInfoOffset = 0 * kPointerSize;
3937 static const int kPreviousIndexOffset = 1 * kPointerSize;
3938 static const int kSubjectOffset = 2 * kPointerSize;
3939 static const int kJSRegExpOffset = 3 * kPointerSize;
3940
3941 Label runtime, invoke_regexp;
3942
3943 // Allocation of registers for this function. These are in callee save
3944 // registers and will be preserved by the call to the native RegExp code, as
3945 // this code is called using the normal C calling convention. When calling
3946 // directly from generated code the native RegExp code will not do a GC and
3947 // therefore the content of these registers are safe to use after the call.
3948 Register subject = r4;
3949 Register regexp_data = r5;
3950 Register last_match_info_elements = r6;
3951
3952 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01003953 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003954 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01003955 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003956 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01003957 ExternalReference::address_of_regexp_stack_memory_size(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003958 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
3959 __ ldr(r0, MemOperand(r0, 0));
3960 __ tst(r0, Operand(r0));
3961 __ b(eq, &runtime);
3962
3963 // Check that the first argument is a JSRegExp object.
3964 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
3965 STATIC_ASSERT(kSmiTag == 0);
3966 __ tst(r0, Operand(kSmiTagMask));
3967 __ b(eq, &runtime);
3968 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
3969 __ b(ne, &runtime);
3970
3971 // Check that the RegExp has been compiled (data contains a fixed array).
3972 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
3973 if (FLAG_debug_code) {
3974 __ tst(regexp_data, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01003975 __ Check(ne, "Unexpected type for RegExp data, FixedArray expected");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003976 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
3977 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
3978 }
3979
3980 // regexp_data: RegExp data (FixedArray)
3981 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3982 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
3983 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
3984 __ b(ne, &runtime);
3985
3986 // regexp_data: RegExp data (FixedArray)
3987 // Check that the number of captures fit in the static offsets vector buffer.
3988 __ ldr(r2,
3989 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
3990 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3991 // uses the asumption that smis are 2 * their untagged value.
3992 STATIC_ASSERT(kSmiTag == 0);
3993 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
3994 __ add(r2, r2, Operand(2)); // r2 was a smi.
3995 // Check that the static offsets vector buffer is large enough.
3996 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
3997 __ b(hi, &runtime);
3998
3999 // r2: Number of capture registers
4000 // regexp_data: RegExp data (FixedArray)
4001 // Check that the second argument is a string.
4002 __ ldr(subject, MemOperand(sp, kSubjectOffset));
4003 __ tst(subject, Operand(kSmiTagMask));
4004 __ b(eq, &runtime);
4005 Condition is_string = masm->IsObjectStringType(subject, r0);
4006 __ b(NegateCondition(is_string), &runtime);
4007 // Get the length of the string to r3.
4008 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
4009
4010 // r2: Number of capture registers
4011 // r3: Length of subject string as a smi
4012 // subject: Subject string
4013 // regexp_data: RegExp data (FixedArray)
4014 // Check that the third argument is a positive smi less than the subject
4015 // string length. A negative value will be greater (unsigned comparison).
4016 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
4017 __ tst(r0, Operand(kSmiTagMask));
4018 __ b(ne, &runtime);
4019 __ cmp(r3, Operand(r0));
4020 __ b(ls, &runtime);
4021
4022 // r2: Number of capture registers
4023 // subject: Subject string
4024 // regexp_data: RegExp data (FixedArray)
4025 // Check that the fourth object is a JSArray object.
4026 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
4027 __ tst(r0, Operand(kSmiTagMask));
4028 __ b(eq, &runtime);
4029 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
4030 __ b(ne, &runtime);
4031 // Check that the JSArray is in fast case.
4032 __ ldr(last_match_info_elements,
4033 FieldMemOperand(r0, JSArray::kElementsOffset));
4034 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
4035 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
4036 __ cmp(r0, ip);
4037 __ b(ne, &runtime);
4038 // Check that the last match info has space for the capture registers and the
4039 // additional information.
4040 __ ldr(r0,
4041 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
4042 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
4043 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
4044 __ b(gt, &runtime);
4045
4046 // subject: Subject string
4047 // regexp_data: RegExp data (FixedArray)
4048 // Check the representation and encoding of the subject string.
4049 Label seq_string;
4050 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4051 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
4052 // First check for flat string.
4053 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
4054 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
4055 __ b(eq, &seq_string);
4056
4057 // subject: Subject string
4058 // regexp_data: RegExp data (FixedArray)
4059 // Check for flat cons string.
4060 // A flat cons string is a cons string where the second part is the empty
4061 // string. In that case the subject string is just the first part of the cons
4062 // string. Also in this case the first part of the cons string is known to be
4063 // a sequential string or an external string.
4064 STATIC_ASSERT(kExternalStringTag !=0);
4065 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
4066 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
4067 __ b(ne, &runtime);
4068 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
4069 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
4070 __ cmp(r0, r1);
4071 __ b(ne, &runtime);
4072 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
4073 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4074 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
4075 // Is first part a flat string?
4076 STATIC_ASSERT(kSeqStringTag == 0);
4077 __ tst(r0, Operand(kStringRepresentationMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004078 __ b(ne, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004079
4080 __ bind(&seq_string);
4081 // subject: Subject string
4082 // regexp_data: RegExp data (FixedArray)
4083 // r0: Instance type of subject string
4084 STATIC_ASSERT(4 == kAsciiStringTag);
4085 STATIC_ASSERT(kTwoByteStringTag == 0);
4086 // Find the code object based on the assumptions above.
4087 __ and_(r0, r0, Operand(kStringEncodingMask));
4088 __ mov(r3, Operand(r0, ASR, 2), SetCC);
4089 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
4090 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
4091
4092 // Check that the irregexp code has been generated for the actual string
4093 // encoding. If it has, the field contains a code object otherwise it contains
4094 // the hole.
4095 __ CompareObjectType(r7, r0, r0, CODE_TYPE);
4096 __ b(ne, &runtime);
4097
Steve Block44f0eee2011-05-26 01:26:41 +01004098 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004099 // r7: code
4100 // subject: Subject string
4101 // regexp_data: RegExp data (FixedArray)
4102 // Load used arguments before starting to push arguments for call to native
4103 // RegExp code to avoid handling changing stack height.
4104 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
4105 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
4106
4107 // r1: previous index
Steve Block44f0eee2011-05-26 01:26:41 +01004108 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004109 // r7: code
4110 // subject: Subject string
4111 // regexp_data: RegExp data (FixedArray)
4112 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01004113 __ IncrementCounter(isolate->counters()->regexp_entry_native(), 1, r0, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004114
Steve Block44f0eee2011-05-26 01:26:41 +01004115 // Isolates: note we add an additional parameter here (isolate pointer).
4116 static const int kRegExpExecuteArguments = 8;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004117 static const int kParameterRegisters = 4;
4118 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004119
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004120 // Stack pointer now points to cell where return address is to be written.
4121 // Arguments are before that on the stack or in registers.
4122
Steve Block44f0eee2011-05-26 01:26:41 +01004123 // Argument 8 (sp[16]): Pass current isolate address.
4124 __ mov(r0, Operand(ExternalReference::isolate_address()));
4125 __ str(r0, MemOperand(sp, 4 * kPointerSize));
4126
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004127 // Argument 7 (sp[12]): Indicate that this is a direct call from JavaScript.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004128 __ mov(r0, Operand(1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004129 __ str(r0, MemOperand(sp, 3 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004130
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004131 // Argument 6 (sp[8]): Start (high end) of backtracking stack memory area.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004132 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
4133 __ ldr(r0, MemOperand(r0, 0));
4134 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
4135 __ ldr(r2, MemOperand(r2, 0));
4136 __ add(r0, r0, Operand(r2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004137 __ str(r0, MemOperand(sp, 2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004138
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004139 // Argument 5 (sp[4]): static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01004140 __ mov(r0,
4141 Operand(ExternalReference::address_of_static_offsets_vector(isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004142 __ str(r0, MemOperand(sp, 1 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004143
4144 // For arguments 4 and 3 get string length, calculate start of string data and
4145 // calculate the shift of the index (0 for ASCII and 1 for two byte).
4146 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
4147 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
4148 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
4149 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4150 __ eor(r3, r3, Operand(1));
4151 // Argument 4 (r3): End of string data
4152 // Argument 3 (r2): Start of string data
4153 __ add(r2, r9, Operand(r1, LSL, r3));
4154 __ add(r3, r9, Operand(r0, LSL, r3));
4155
4156 // Argument 2 (r1): Previous index.
4157 // Already there
4158
4159 // Argument 1 (r0): Subject string.
4160 __ mov(r0, subject);
4161
4162 // Locate the code entry and call it.
4163 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004164 DirectCEntryStub stub;
4165 stub.GenerateCall(masm, r7);
4166
4167 __ LeaveExitFrame(false, no_reg);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004168
4169 // r0: result
4170 // subject: subject string (callee saved)
4171 // regexp_data: RegExp data (callee saved)
4172 // last_match_info_elements: Last match info elements (callee saved)
4173
4174 // Check the result.
4175 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004176
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004177 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
4178 __ b(eq, &success);
4179 Label failure;
4180 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
4181 __ b(eq, &failure);
4182 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
4183 // If not exception it can only be retry. Handle that in the runtime system.
4184 __ b(ne, &runtime);
4185 // Result must now be exception. If there is no pending exception already a
4186 // stack overflow (on the backtrack stack) was detected in RegExp code but
4187 // haven't created the exception yet. Handle that in the runtime system.
4188 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01004189 __ mov(r1, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004190 __ ldr(r1, MemOperand(r1, 0));
Steve Block44f0eee2011-05-26 01:26:41 +01004191 __ mov(r2, Operand(ExternalReference(Isolate::k_pending_exception_address,
4192 isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004193 __ ldr(r0, MemOperand(r2, 0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004194 __ cmp(r0, r1);
4195 __ b(eq, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004196
4197 __ str(r1, MemOperand(r2, 0)); // Clear pending exception.
4198
4199 // Check if the exception is a termination. If so, throw as uncatchable.
4200 __ LoadRoot(ip, Heap::kTerminationExceptionRootIndex);
4201 __ cmp(r0, ip);
4202 Label termination_exception;
4203 __ b(eq, &termination_exception);
4204
4205 __ Throw(r0); // Expects thrown value in r0.
4206
4207 __ bind(&termination_exception);
4208 __ ThrowUncatchable(TERMINATION, r0); // Expects thrown value in r0.
4209
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004210 __ bind(&failure);
4211 // For failure and exception return null.
Ben Murdoch8b112d22011-06-08 16:22:53 +01004212 __ mov(r0, Operand(masm->isolate()->factory()->null_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004213 __ add(sp, sp, Operand(4 * kPointerSize));
4214 __ Ret();
4215
4216 // Process the result from the native regexp code.
4217 __ bind(&success);
4218 __ ldr(r1,
4219 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4220 // Calculate number of capture registers (number_of_captures + 1) * 2.
4221 STATIC_ASSERT(kSmiTag == 0);
4222 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4223 __ add(r1, r1, Operand(2)); // r1 was a smi.
4224
4225 // r1: number of capture registers
4226 // r4: subject string
4227 // Store the capture count.
4228 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
4229 __ str(r2, FieldMemOperand(last_match_info_elements,
4230 RegExpImpl::kLastCaptureCountOffset));
4231 // Store last subject and last input.
4232 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
4233 __ str(subject,
4234 FieldMemOperand(last_match_info_elements,
4235 RegExpImpl::kLastSubjectOffset));
4236 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
4237 __ str(subject,
4238 FieldMemOperand(last_match_info_elements,
4239 RegExpImpl::kLastInputOffset));
4240 __ mov(r3, last_match_info_elements);
4241 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
4242
4243 // Get the static offsets vector filled by the native regexp code.
4244 ExternalReference address_of_static_offsets_vector =
Steve Block44f0eee2011-05-26 01:26:41 +01004245 ExternalReference::address_of_static_offsets_vector(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004246 __ mov(r2, Operand(address_of_static_offsets_vector));
4247
4248 // r1: number of capture registers
4249 // r2: offsets vector
4250 Label next_capture, done;
4251 // Capture register counter starts from number of capture registers and
4252 // counts down until wraping after zero.
4253 __ add(r0,
4254 last_match_info_elements,
4255 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
4256 __ bind(&next_capture);
4257 __ sub(r1, r1, Operand(1), SetCC);
4258 __ b(mi, &done);
4259 // Read the value from the static offsets vector buffer.
4260 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
4261 // Store the smi value in the last match info.
4262 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
4263 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
4264 __ jmp(&next_capture);
4265 __ bind(&done);
4266
4267 // Return last match info.
4268 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
4269 __ add(sp, sp, Operand(4 * kPointerSize));
4270 __ Ret();
4271
4272 // Do the runtime call to execute the regexp.
4273 __ bind(&runtime);
4274 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4275#endif // V8_INTERPRETED_REGEXP
4276}
4277
4278
Ben Murdochb0fe1622011-05-05 13:52:32 +01004279void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
4280 const int kMaxInlineLength = 100;
4281 Label slowcase;
4282 Label done;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004283 Factory* factory = masm->isolate()->factory();
4284
Ben Murdochb0fe1622011-05-05 13:52:32 +01004285 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4286 STATIC_ASSERT(kSmiTag == 0);
4287 STATIC_ASSERT(kSmiTagSize == 1);
4288 __ tst(r1, Operand(kSmiTagMask));
4289 __ b(ne, &slowcase);
4290 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4291 __ b(hi, &slowcase);
4292 // Smi-tagging is equivalent to multiplying by 2.
4293 // Allocate RegExpResult followed by FixedArray with size in ebx.
4294 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4295 // Elements: [Map][Length][..elements..]
4296 // Size of JSArray with two in-object properties and the header of a
4297 // FixedArray.
4298 int objects_size =
4299 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4300 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4301 __ add(r2, r5, Operand(objects_size));
4302 __ AllocateInNewSpace(
4303 r2, // In: Size, in words.
4304 r0, // Out: Start of allocation (tagged).
4305 r3, // Scratch register.
4306 r4, // Scratch register.
4307 &slowcase,
4308 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
4309 // r0: Start of allocated area, object-tagged.
4310 // r1: Number of elements in array, as smi.
4311 // r5: Number of elements, untagged.
4312
4313 // Set JSArray map to global.regexp_result_map().
4314 // Set empty properties FixedArray.
4315 // Set elements to point to FixedArray allocated right after the JSArray.
4316 // Interleave operations for better latency.
4317 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4318 __ add(r3, r0, Operand(JSRegExpResult::kSize));
Ben Murdoch8b112d22011-06-08 16:22:53 +01004319 __ mov(r4, Operand(factory->empty_fixed_array()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004320 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4321 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4322 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4323 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4324 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4325
4326 // Set input, index and length fields from arguments.
4327 __ ldr(r1, MemOperand(sp, kPointerSize * 0));
4328 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4329 __ ldr(r1, MemOperand(sp, kPointerSize * 1));
4330 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4331 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4332 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4333
4334 // Fill out the elements FixedArray.
4335 // r0: JSArray, tagged.
4336 // r3: FixedArray, tagged.
4337 // r5: Number of elements in array, untagged.
4338
4339 // Set map.
Ben Murdoch8b112d22011-06-08 16:22:53 +01004340 __ mov(r2, Operand(factory->fixed_array_map()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004341 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4342 // Set FixedArray length.
4343 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
4344 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
4345 // Fill contents of fixed-array with the-hole.
Ben Murdoch8b112d22011-06-08 16:22:53 +01004346 __ mov(r2, Operand(factory->the_hole_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004347 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4348 // Fill fixed array elements with hole.
4349 // r0: JSArray, tagged.
4350 // r2: the hole.
4351 // r3: Start of elements in FixedArray.
4352 // r5: Number of elements to fill.
4353 Label loop;
4354 __ tst(r5, Operand(r5));
4355 __ bind(&loop);
4356 __ b(le, &done); // Jump if r1 is negative or zero.
4357 __ sub(r5, r5, Operand(1), SetCC);
4358 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4359 __ jmp(&loop);
4360
4361 __ bind(&done);
4362 __ add(sp, sp, Operand(3 * kPointerSize));
4363 __ Ret();
4364
4365 __ bind(&slowcase);
4366 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
4367}
4368
4369
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004370void CallFunctionStub::Generate(MacroAssembler* masm) {
4371 Label slow;
4372
4373 // If the receiver might be a value (string, number or boolean) check for this
4374 // and box it if it is.
4375 if (ReceiverMightBeValue()) {
4376 // Get the receiver from the stack.
4377 // function, receiver [, arguments]
4378 Label receiver_is_value, receiver_is_js_object;
4379 __ ldr(r1, MemOperand(sp, argc_ * kPointerSize));
4380
4381 // Check if receiver is a smi (which is a number value).
Steve Block1e0659c2011-05-24 12:43:12 +01004382 __ JumpIfSmi(r1, &receiver_is_value);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004383
4384 // Check if the receiver is a valid JS object.
4385 __ CompareObjectType(r1, r2, r2, FIRST_JS_OBJECT_TYPE);
4386 __ b(ge, &receiver_is_js_object);
4387
4388 // Call the runtime to box the value.
4389 __ bind(&receiver_is_value);
4390 __ EnterInternalFrame();
4391 __ push(r1);
4392 __ InvokeBuiltin(Builtins::TO_OBJECT, CALL_JS);
4393 __ LeaveInternalFrame();
4394 __ str(r0, MemOperand(sp, argc_ * kPointerSize));
4395
4396 __ bind(&receiver_is_js_object);
4397 }
4398
4399 // Get the function to call from the stack.
4400 // function, receiver [, arguments]
4401 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4402
4403 // Check that the function is really a JavaScript function.
4404 // r1: pushed function (to be verified)
Steve Block1e0659c2011-05-24 12:43:12 +01004405 __ JumpIfSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004406 // Get the map of the function object.
4407 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
4408 __ b(ne, &slow);
4409
4410 // Fast-case: Invoke the function now.
4411 // r1: pushed function
4412 ParameterCount actual(argc_);
4413 __ InvokeFunction(r1, actual, JUMP_FUNCTION);
4414
4415 // Slow-case: Non-function called.
4416 __ bind(&slow);
4417 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4418 // of the original receiver from the call site).
4419 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
4420 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
Iain Merrick9ac36c92010-09-13 15:29:50 +01004421 __ mov(r2, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004422 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01004423 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004424 RelocInfo::CODE_TARGET);
4425}
4426
4427
4428// Unfortunately you have to run without snapshots to see most of these
4429// names in the profile since most compare stubs end up in the snapshot.
4430const char* CompareStub::GetName() {
4431 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
4432 (lhs_.is(r1) && rhs_.is(r0)));
4433
4434 if (name_ != NULL) return name_;
4435 const int kMaxNameLength = 100;
Steve Block44f0eee2011-05-26 01:26:41 +01004436 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4437 kMaxNameLength);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004438 if (name_ == NULL) return "OOM";
4439
4440 const char* cc_name;
4441 switch (cc_) {
4442 case lt: cc_name = "LT"; break;
4443 case gt: cc_name = "GT"; break;
4444 case le: cc_name = "LE"; break;
4445 case ge: cc_name = "GE"; break;
4446 case eq: cc_name = "EQ"; break;
4447 case ne: cc_name = "NE"; break;
4448 default: cc_name = "UnknownCondition"; break;
4449 }
4450
4451 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
4452 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
4453
4454 const char* strict_name = "";
4455 if (strict_ && (cc_ == eq || cc_ == ne)) {
4456 strict_name = "_STRICT";
4457 }
4458
4459 const char* never_nan_nan_name = "";
4460 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
4461 never_nan_nan_name = "_NO_NAN";
4462 }
4463
4464 const char* include_number_compare_name = "";
4465 if (!include_number_compare_) {
4466 include_number_compare_name = "_NO_NUMBER";
4467 }
4468
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004469 const char* include_smi_compare_name = "";
4470 if (!include_smi_compare_) {
4471 include_smi_compare_name = "_NO_SMI";
4472 }
4473
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004474 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
4475 "CompareStub_%s%s%s%s%s%s",
4476 cc_name,
4477 lhs_name,
4478 rhs_name,
4479 strict_name,
4480 never_nan_nan_name,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004481 include_number_compare_name,
4482 include_smi_compare_name);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004483 return name_;
4484}
4485
4486
4487int CompareStub::MinorKey() {
4488 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4489 // stubs the never NaN NaN condition is only taken into account if the
4490 // condition is equals.
4491 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
4492 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
4493 (lhs_.is(r1) && rhs_.is(r0)));
4494 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
4495 | RegisterField::encode(lhs_.is(r0))
4496 | StrictField::encode(strict_)
4497 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004498 | IncludeNumberCompareField::encode(include_number_compare_)
4499 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004500}
4501
4502
4503// StringCharCodeAtGenerator
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004504void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4505 Label flat_string;
4506 Label ascii_string;
4507 Label got_char_code;
4508
4509 // If the receiver is a smi trigger the non-string case.
Steve Block1e0659c2011-05-24 12:43:12 +01004510 __ JumpIfSmi(object_, receiver_not_string_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004511
4512 // Fetch the instance type of the receiver into result register.
4513 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4514 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4515 // If the receiver is not a string trigger the non-string case.
4516 __ tst(result_, Operand(kIsNotStringMask));
4517 __ b(ne, receiver_not_string_);
4518
4519 // If the index is non-smi trigger the non-smi case.
Steve Block1e0659c2011-05-24 12:43:12 +01004520 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004521
4522 // Put smi-tagged index into scratch register.
4523 __ mov(scratch_, index_);
4524 __ bind(&got_smi_index_);
4525
4526 // Check for index out of range.
4527 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
4528 __ cmp(ip, Operand(scratch_));
4529 __ b(ls, index_out_of_range_);
4530
4531 // We need special handling for non-flat strings.
4532 STATIC_ASSERT(kSeqStringTag == 0);
4533 __ tst(result_, Operand(kStringRepresentationMask));
4534 __ b(eq, &flat_string);
4535
4536 // Handle non-flat strings.
4537 __ tst(result_, Operand(kIsConsStringMask));
4538 __ b(eq, &call_runtime_);
4539
4540 // ConsString.
4541 // Check whether the right hand side is the empty string (i.e. if
4542 // this is really a flat string in a cons string). If that is not
4543 // the case we would rather go to the runtime system now to flatten
4544 // the string.
4545 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
4546 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
4547 __ cmp(result_, Operand(ip));
4548 __ b(ne, &call_runtime_);
4549 // Get the first of the two strings and load its instance type.
4550 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
4551 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4552 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4553 // If the first cons component is also non-flat, then go to runtime.
4554 STATIC_ASSERT(kSeqStringTag == 0);
4555 __ tst(result_, Operand(kStringRepresentationMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004556 __ b(ne, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004557
4558 // Check for 1-byte or 2-byte string.
4559 __ bind(&flat_string);
4560 STATIC_ASSERT(kAsciiStringTag != 0);
4561 __ tst(result_, Operand(kStringEncodingMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004562 __ b(ne, &ascii_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004563
4564 // 2-byte string.
4565 // Load the 2-byte character code into the result register. We can
4566 // add without shifting since the smi tag size is the log2 of the
4567 // number of bytes in a two-byte character.
4568 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
4569 __ add(scratch_, object_, Operand(scratch_));
4570 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
4571 __ jmp(&got_char_code);
4572
4573 // ASCII string.
4574 // Load the byte into the result register.
4575 __ bind(&ascii_string);
4576 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
4577 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
4578
4579 __ bind(&got_char_code);
4580 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
4581 __ bind(&exit_);
4582}
4583
4584
4585void StringCharCodeAtGenerator::GenerateSlow(
4586 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4587 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4588
4589 // Index is not a smi.
4590 __ bind(&index_not_smi_);
4591 // If index is a heap number, try converting it to an integer.
4592 __ CheckMap(index_,
4593 scratch_,
4594 Heap::kHeapNumberMapRootIndex,
4595 index_not_number_,
4596 true);
4597 call_helper.BeforeCall(masm);
4598 __ Push(object_, index_);
4599 __ push(index_); // Consumed by runtime conversion function.
4600 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4601 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4602 } else {
4603 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4604 // NumberToSmi discards numbers that are not exact integers.
4605 __ CallRuntime(Runtime::kNumberToSmi, 1);
4606 }
4607 // Save the conversion result before the pop instructions below
4608 // have a chance to overwrite it.
4609 __ Move(scratch_, r0);
4610 __ pop(index_);
4611 __ pop(object_);
4612 // Reload the instance type.
4613 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4614 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4615 call_helper.AfterCall(masm);
4616 // If index is still not a smi, it must be out of range.
Steve Block1e0659c2011-05-24 12:43:12 +01004617 __ JumpIfNotSmi(scratch_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004618 // Otherwise, return to the fast path.
4619 __ jmp(&got_smi_index_);
4620
4621 // Call runtime. We get here when the receiver is a string and the
4622 // index is a number, but the code of getting the actual character
4623 // is too complex (e.g., when the string needs to be flattened).
4624 __ bind(&call_runtime_);
4625 call_helper.BeforeCall(masm);
4626 __ Push(object_, index_);
4627 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4628 __ Move(result_, r0);
4629 call_helper.AfterCall(masm);
4630 __ jmp(&exit_);
4631
4632 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4633}
4634
4635
4636// -------------------------------------------------------------------------
4637// StringCharFromCodeGenerator
4638
4639void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4640 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4641 STATIC_ASSERT(kSmiTag == 0);
4642 STATIC_ASSERT(kSmiShiftSize == 0);
4643 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4644 __ tst(code_,
4645 Operand(kSmiTagMask |
4646 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
Steve Block1e0659c2011-05-24 12:43:12 +01004647 __ b(ne, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004648
4649 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01004650 // At this point code register contains smi tagged ASCII char code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004651 STATIC_ASSERT(kSmiTag == 0);
4652 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
4653 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
4654 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4655 __ cmp(result_, Operand(ip));
4656 __ b(eq, &slow_case_);
4657 __ bind(&exit_);
4658}
4659
4660
4661void StringCharFromCodeGenerator::GenerateSlow(
4662 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4663 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4664
4665 __ bind(&slow_case_);
4666 call_helper.BeforeCall(masm);
4667 __ push(code_);
4668 __ CallRuntime(Runtime::kCharFromCode, 1);
4669 __ Move(result_, r0);
4670 call_helper.AfterCall(masm);
4671 __ jmp(&exit_);
4672
4673 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4674}
4675
4676
4677// -------------------------------------------------------------------------
4678// StringCharAtGenerator
4679
4680void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4681 char_code_at_generator_.GenerateFast(masm);
4682 char_from_code_generator_.GenerateFast(masm);
4683}
4684
4685
4686void StringCharAtGenerator::GenerateSlow(
4687 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4688 char_code_at_generator_.GenerateSlow(masm, call_helper);
4689 char_from_code_generator_.GenerateSlow(masm, call_helper);
4690}
4691
4692
4693class StringHelper : public AllStatic {
4694 public:
4695 // Generate code for copying characters using a simple loop. This should only
4696 // be used in places where the number of characters is small and the
4697 // additional setup and checking in GenerateCopyCharactersLong adds too much
4698 // overhead. Copying of overlapping regions is not supported.
4699 // Dest register ends at the position after the last character written.
4700 static void GenerateCopyCharacters(MacroAssembler* masm,
4701 Register dest,
4702 Register src,
4703 Register count,
4704 Register scratch,
4705 bool ascii);
4706
4707 // Generate code for copying a large number of characters. This function
4708 // is allowed to spend extra time setting up conditions to make copying
4709 // faster. Copying of overlapping regions is not supported.
4710 // Dest register ends at the position after the last character written.
4711 static void GenerateCopyCharactersLong(MacroAssembler* masm,
4712 Register dest,
4713 Register src,
4714 Register count,
4715 Register scratch1,
4716 Register scratch2,
4717 Register scratch3,
4718 Register scratch4,
4719 Register scratch5,
4720 int flags);
4721
4722
4723 // Probe the symbol table for a two character string. If the string is
4724 // not found by probing a jump to the label not_found is performed. This jump
4725 // does not guarantee that the string is not in the symbol table. If the
4726 // string is found the code falls through with the string in register r0.
4727 // Contents of both c1 and c2 registers are modified. At the exit c1 is
4728 // guaranteed to contain halfword with low and high bytes equal to
4729 // initial contents of c1 and c2 respectively.
4730 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4731 Register c1,
4732 Register c2,
4733 Register scratch1,
4734 Register scratch2,
4735 Register scratch3,
4736 Register scratch4,
4737 Register scratch5,
4738 Label* not_found);
4739
4740 // Generate string hash.
4741 static void GenerateHashInit(MacroAssembler* masm,
4742 Register hash,
4743 Register character);
4744
4745 static void GenerateHashAddCharacter(MacroAssembler* masm,
4746 Register hash,
4747 Register character);
4748
4749 static void GenerateHashGetHash(MacroAssembler* masm,
4750 Register hash);
4751
4752 private:
4753 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
4754};
4755
4756
4757void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
4758 Register dest,
4759 Register src,
4760 Register count,
4761 Register scratch,
4762 bool ascii) {
4763 Label loop;
4764 Label done;
4765 // This loop just copies one character at a time, as it is only used for very
4766 // short strings.
4767 if (!ascii) {
4768 __ add(count, count, Operand(count), SetCC);
4769 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01004770 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004771 }
4772 __ b(eq, &done);
4773
4774 __ bind(&loop);
4775 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
4776 // Perform sub between load and dependent store to get the load time to
4777 // complete.
4778 __ sub(count, count, Operand(1), SetCC);
4779 __ strb(scratch, MemOperand(dest, 1, PostIndex));
4780 // last iteration.
4781 __ b(gt, &loop);
4782
4783 __ bind(&done);
4784}
4785
4786
4787enum CopyCharactersFlags {
4788 COPY_ASCII = 1,
4789 DEST_ALWAYS_ALIGNED = 2
4790};
4791
4792
4793void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
4794 Register dest,
4795 Register src,
4796 Register count,
4797 Register scratch1,
4798 Register scratch2,
4799 Register scratch3,
4800 Register scratch4,
4801 Register scratch5,
4802 int flags) {
4803 bool ascii = (flags & COPY_ASCII) != 0;
4804 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
4805
4806 if (dest_always_aligned && FLAG_debug_code) {
4807 // Check that destination is actually word aligned if the flag says
4808 // that it is.
4809 __ tst(dest, Operand(kPointerAlignmentMask));
4810 __ Check(eq, "Destination of copy not aligned.");
4811 }
4812
4813 const int kReadAlignment = 4;
4814 const int kReadAlignmentMask = kReadAlignment - 1;
4815 // Ensure that reading an entire aligned word containing the last character
4816 // of a string will not read outside the allocated area (because we pad up
4817 // to kObjectAlignment).
4818 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
4819 // Assumes word reads and writes are little endian.
4820 // Nothing to do for zero characters.
4821 Label done;
4822 if (!ascii) {
4823 __ add(count, count, Operand(count), SetCC);
4824 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01004825 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004826 }
4827 __ b(eq, &done);
4828
4829 // Assume that you cannot read (or write) unaligned.
4830 Label byte_loop;
4831 // Must copy at least eight bytes, otherwise just do it one byte at a time.
4832 __ cmp(count, Operand(8));
4833 __ add(count, dest, Operand(count));
4834 Register limit = count; // Read until src equals this.
4835 __ b(lt, &byte_loop);
4836
4837 if (!dest_always_aligned) {
4838 // Align dest by byte copying. Copies between zero and three bytes.
4839 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
4840 Label dest_aligned;
4841 __ b(eq, &dest_aligned);
4842 __ cmp(scratch4, Operand(2));
4843 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
4844 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
4845 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
4846 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4847 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
4848 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
4849 __ bind(&dest_aligned);
4850 }
4851
4852 Label simple_loop;
4853
4854 __ sub(scratch4, dest, Operand(src));
4855 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
4856 __ b(eq, &simple_loop);
4857 // Shift register is number of bits in a source word that
4858 // must be combined with bits in the next source word in order
4859 // to create a destination word.
4860
4861 // Complex loop for src/dst that are not aligned the same way.
4862 {
4863 Label loop;
4864 __ mov(scratch4, Operand(scratch4, LSL, 3));
4865 Register left_shift = scratch4;
4866 __ and_(src, src, Operand(~3)); // Round down to load previous word.
4867 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
4868 // Store the "shift" most significant bits of scratch in the least
4869 // signficant bits (i.e., shift down by (32-shift)).
4870 __ rsb(scratch2, left_shift, Operand(32));
4871 Register right_shift = scratch2;
4872 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
4873
4874 __ bind(&loop);
4875 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
4876 __ sub(scratch5, limit, Operand(dest));
4877 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
4878 __ str(scratch1, MemOperand(dest, 4, PostIndex));
4879 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
4880 // Loop if four or more bytes left to copy.
4881 // Compare to eight, because we did the subtract before increasing dst.
4882 __ sub(scratch5, scratch5, Operand(8), SetCC);
4883 __ b(ge, &loop);
4884 }
4885 // There is now between zero and three bytes left to copy (negative that
4886 // number is in scratch5), and between one and three bytes already read into
4887 // scratch1 (eight times that number in scratch4). We may have read past
4888 // the end of the string, but because objects are aligned, we have not read
4889 // past the end of the object.
4890 // Find the minimum of remaining characters to move and preloaded characters
4891 // and write those as bytes.
4892 __ add(scratch5, scratch5, Operand(4), SetCC);
4893 __ b(eq, &done);
4894 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
4895 // Move minimum of bytes read and bytes left to copy to scratch4.
4896 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
4897 // Between one and three (value in scratch5) characters already read into
4898 // scratch ready to write.
4899 __ cmp(scratch5, Operand(2));
4900 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4901 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
4902 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
4903 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
4904 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
4905 // Copy any remaining bytes.
4906 __ b(&byte_loop);
4907
4908 // Simple loop.
4909 // Copy words from src to dst, until less than four bytes left.
4910 // Both src and dest are word aligned.
4911 __ bind(&simple_loop);
4912 {
4913 Label loop;
4914 __ bind(&loop);
4915 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
4916 __ sub(scratch3, limit, Operand(dest));
4917 __ str(scratch1, MemOperand(dest, 4, PostIndex));
4918 // Compare to 8, not 4, because we do the substraction before increasing
4919 // dest.
4920 __ cmp(scratch3, Operand(8));
4921 __ b(ge, &loop);
4922 }
4923
4924 // Copy bytes from src to dst until dst hits limit.
4925 __ bind(&byte_loop);
4926 __ cmp(dest, Operand(limit));
4927 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
4928 __ b(ge, &done);
4929 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4930 __ b(&byte_loop);
4931
4932 __ bind(&done);
4933}
4934
4935
4936void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4937 Register c1,
4938 Register c2,
4939 Register scratch1,
4940 Register scratch2,
4941 Register scratch3,
4942 Register scratch4,
4943 Register scratch5,
4944 Label* not_found) {
4945 // Register scratch3 is the general scratch register in this function.
4946 Register scratch = scratch3;
4947
4948 // Make sure that both characters are not digits as such strings has a
4949 // different hash algorithm. Don't try to look for these in the symbol table.
4950 Label not_array_index;
4951 __ sub(scratch, c1, Operand(static_cast<int>('0')));
4952 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
4953 __ b(hi, &not_array_index);
4954 __ sub(scratch, c2, Operand(static_cast<int>('0')));
4955 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
4956
4957 // If check failed combine both characters into single halfword.
4958 // This is required by the contract of the method: code at the
4959 // not_found branch expects this combination in c1 register
4960 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
4961 __ b(ls, not_found);
4962
4963 __ bind(&not_array_index);
4964 // Calculate the two character string hash.
4965 Register hash = scratch1;
4966 StringHelper::GenerateHashInit(masm, hash, c1);
4967 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
4968 StringHelper::GenerateHashGetHash(masm, hash);
4969
4970 // Collect the two characters in a register.
4971 Register chars = c1;
4972 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
4973
4974 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4975 // hash: hash of two character string.
4976
4977 // Load symbol table
4978 // Load address of first element of the symbol table.
4979 Register symbol_table = c2;
4980 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
4981
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004982 Register undefined = scratch4;
4983 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
4984
4985 // Calculate capacity mask from the symbol table capacity.
4986 Register mask = scratch2;
4987 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
4988 __ mov(mask, Operand(mask, ASR, 1));
4989 __ sub(mask, mask, Operand(1));
4990
4991 // Calculate untagged address of the first element of the symbol table.
4992 Register first_symbol_table_element = symbol_table;
4993 __ add(first_symbol_table_element, symbol_table,
4994 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
4995
4996 // Registers
4997 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4998 // hash: hash of two character string
4999 // mask: capacity mask
5000 // first_symbol_table_element: address of the first element of
5001 // the symbol table
Steve Block44f0eee2011-05-26 01:26:41 +01005002 // undefined: the undefined object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005003 // scratch: -
5004
5005 // Perform a number of probes in the symbol table.
5006 static const int kProbes = 4;
5007 Label found_in_symbol_table;
5008 Label next_probe[kProbes];
5009 for (int i = 0; i < kProbes; i++) {
5010 Register candidate = scratch5; // Scratch register contains candidate.
5011
5012 // Calculate entry in symbol table.
5013 if (i > 0) {
5014 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
5015 } else {
5016 __ mov(candidate, hash);
5017 }
5018
5019 __ and_(candidate, candidate, Operand(mask));
5020
5021 // Load the entry from the symble table.
5022 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5023 __ ldr(candidate,
5024 MemOperand(first_symbol_table_element,
5025 candidate,
5026 LSL,
5027 kPointerSizeLog2));
5028
5029 // If entry is undefined no string with this hash can be found.
Steve Block44f0eee2011-05-26 01:26:41 +01005030 Label is_string;
5031 __ CompareObjectType(candidate, scratch, scratch, ODDBALL_TYPE);
5032 __ b(ne, &is_string);
5033
5034 __ cmp(undefined, candidate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005035 __ b(eq, not_found);
Steve Block44f0eee2011-05-26 01:26:41 +01005036 // Must be null (deleted entry).
5037 if (FLAG_debug_code) {
5038 __ LoadRoot(ip, Heap::kNullValueRootIndex);
5039 __ cmp(ip, candidate);
5040 __ Assert(eq, "oddball in symbol table is not undefined or null");
5041 }
5042 __ jmp(&next_probe[i]);
5043
5044 __ bind(&is_string);
5045
5046 // Check that the candidate is a non-external ASCII string. The instance
5047 // type is still in the scratch register from the CompareObjectType
5048 // operation.
5049 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, &next_probe[i]);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005050
5051 // If length is not 2 the string is not a candidate.
5052 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
5053 __ cmp(scratch, Operand(Smi::FromInt(2)));
5054 __ b(ne, &next_probe[i]);
5055
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005056 // Check if the two characters match.
5057 // Assumes that word load is little endian.
5058 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
5059 __ cmp(chars, scratch);
5060 __ b(eq, &found_in_symbol_table);
5061 __ bind(&next_probe[i]);
5062 }
5063
5064 // No matching 2 character string found by probing.
5065 __ jmp(not_found);
5066
5067 // Scratch register contains result when we fall through to here.
5068 Register result = scratch;
5069 __ bind(&found_in_symbol_table);
5070 __ Move(r0, result);
5071}
5072
5073
5074void StringHelper::GenerateHashInit(MacroAssembler* masm,
5075 Register hash,
5076 Register character) {
5077 // hash = character + (character << 10);
5078 __ add(hash, character, Operand(character, LSL, 10));
5079 // hash ^= hash >> 6;
5080 __ eor(hash, hash, Operand(hash, ASR, 6));
5081}
5082
5083
5084void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5085 Register hash,
5086 Register character) {
5087 // hash += character;
5088 __ add(hash, hash, Operand(character));
5089 // hash += hash << 10;
5090 __ add(hash, hash, Operand(hash, LSL, 10));
5091 // hash ^= hash >> 6;
5092 __ eor(hash, hash, Operand(hash, ASR, 6));
5093}
5094
5095
5096void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5097 Register hash) {
5098 // hash += hash << 3;
5099 __ add(hash, hash, Operand(hash, LSL, 3));
5100 // hash ^= hash >> 11;
5101 __ eor(hash, hash, Operand(hash, ASR, 11));
5102 // hash += hash << 15;
5103 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
5104
5105 // if (hash == 0) hash = 27;
Steve Block1e0659c2011-05-24 12:43:12 +01005106 __ mov(hash, Operand(27), LeaveCC, ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005107}
5108
5109
5110void SubStringStub::Generate(MacroAssembler* masm) {
5111 Label runtime;
5112
5113 // Stack frame on entry.
5114 // lr: return address
5115 // sp[0]: to
5116 // sp[4]: from
5117 // sp[8]: string
5118
5119 // This stub is called from the native-call %_SubString(...), so
5120 // nothing can be assumed about the arguments. It is tested that:
5121 // "string" is a sequential string,
5122 // both "from" and "to" are smis, and
5123 // 0 <= from <= to <= string.length.
5124 // If any of these assumptions fail, we call the runtime system.
5125
5126 static const int kToOffset = 0 * kPointerSize;
5127 static const int kFromOffset = 1 * kPointerSize;
5128 static const int kStringOffset = 2 * kPointerSize;
5129
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005130 // Check bounds and smi-ness.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005131 Register to = r6;
5132 Register from = r7;
5133 __ Ldrd(to, from, MemOperand(sp, kToOffset));
5134 STATIC_ASSERT(kFromOffset == kToOffset + 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005135 STATIC_ASSERT(kSmiTag == 0);
5136 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5137 // I.e., arithmetic shift right by one un-smi-tags.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005138 __ mov(r2, Operand(to, ASR, 1), SetCC);
5139 __ mov(r3, Operand(from, ASR, 1), SetCC, cc);
5140 // If either to or from had the smi tag bit set, then carry is set now.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005141 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
5142 __ b(mi, &runtime); // From is negative.
5143
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005144 // Both to and from are smis.
5145
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005146 __ sub(r2, r2, Operand(r3), SetCC);
5147 __ b(mi, &runtime); // Fail if from > to.
5148 // Special handling of sub-strings of length 1 and 2. One character strings
5149 // are handled in the runtime system (looked up in the single character
5150 // cache). Two character strings are looked for in the symbol cache.
5151 __ cmp(r2, Operand(2));
5152 __ b(lt, &runtime);
5153
5154 // r2: length
5155 // r3: from index (untaged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005156 // r6 (a.k.a. to): to (smi)
5157 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005158
5159 // Make sure first argument is a sequential (or flat) string.
5160 __ ldr(r5, MemOperand(sp, kStringOffset));
5161 STATIC_ASSERT(kSmiTag == 0);
5162 __ tst(r5, Operand(kSmiTagMask));
5163 __ b(eq, &runtime);
5164 Condition is_string = masm->IsObjectStringType(r5, r1);
5165 __ b(NegateCondition(is_string), &runtime);
5166
5167 // r1: instance type
5168 // r2: length
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005169 // r3: from index (untagged smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005170 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005171 // r6 (a.k.a. to): to (smi)
5172 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005173 Label seq_string;
5174 __ and_(r4, r1, Operand(kStringRepresentationMask));
5175 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
5176 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
5177 __ cmp(r4, Operand(kConsStringTag));
5178 __ b(gt, &runtime); // External strings go to runtime.
5179 __ b(lt, &seq_string); // Sequential strings are handled directly.
5180
5181 // Cons string. Try to recurse (once) on the first substring.
5182 // (This adds a little more generality than necessary to handle flattened
5183 // cons strings, but not much).
5184 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
5185 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
5186 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5187 __ tst(r1, Operand(kStringRepresentationMask));
5188 STATIC_ASSERT(kSeqStringTag == 0);
5189 __ b(ne, &runtime); // Cons and External strings go to runtime.
5190
5191 // Definitly a sequential string.
5192 __ bind(&seq_string);
5193
5194 // r1: instance type.
5195 // r2: length
5196 // r3: from index (untaged smi)
5197 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005198 // r6 (a.k.a. to): to (smi)
5199 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005200 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005201 __ cmp(r4, Operand(to));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005202 __ b(lt, &runtime); // Fail if to > length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005203 to = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005204
5205 // r1: instance type.
5206 // r2: result string length.
5207 // r3: from index (untaged smi)
5208 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005209 // r7 (a.k.a. from): from offset (smi)
Steve Block44f0eee2011-05-26 01:26:41 +01005210 // Check for flat ASCII string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005211 Label non_ascii_flat;
5212 __ tst(r1, Operand(kStringEncodingMask));
5213 STATIC_ASSERT(kTwoByteStringTag == 0);
5214 __ b(eq, &non_ascii_flat);
5215
5216 Label result_longer_than_two;
5217 __ cmp(r2, Operand(2));
5218 __ b(gt, &result_longer_than_two);
5219
5220 // Sub string of length 2 requested.
5221 // Get the two characters forming the sub string.
5222 __ add(r5, r5, Operand(r3));
5223 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
5224 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
5225
5226 // Try to lookup two character string in symbol table.
5227 Label make_two_character_string;
5228 StringHelper::GenerateTwoCharacterSymbolTableProbe(
5229 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01005230 Counters* counters = masm->isolate()->counters();
5231 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005232 __ add(sp, sp, Operand(3 * kPointerSize));
5233 __ Ret();
5234
5235 // r2: result string length.
5236 // r3: two characters combined into halfword in little endian byte order.
5237 __ bind(&make_two_character_string);
5238 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
5239 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005240 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005241 __ add(sp, sp, Operand(3 * kPointerSize));
5242 __ Ret();
5243
5244 __ bind(&result_longer_than_two);
5245
5246 // Allocate the result.
5247 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
5248
5249 // r0: result string.
5250 // r2: result string length.
5251 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005252 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005253 // Locate first character of result.
5254 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5255 // Locate 'from' character of string.
5256 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005257 __ add(r5, r5, Operand(from, ASR, 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005258
5259 // r0: result string.
5260 // r1: first character of result string.
5261 // r2: result string length.
5262 // r5: first character of sub string to copy.
5263 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
5264 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
5265 COPY_ASCII | DEST_ALWAYS_ALIGNED);
Steve Block44f0eee2011-05-26 01:26:41 +01005266 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005267 __ add(sp, sp, Operand(3 * kPointerSize));
5268 __ Ret();
5269
5270 __ bind(&non_ascii_flat);
5271 // r2: result string length.
5272 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005273 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005274 // Check for flat two byte string.
5275
5276 // Allocate the result.
5277 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
5278
5279 // r0: result string.
5280 // r2: result string length.
5281 // r5: string.
5282 // Locate first character of result.
5283 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5284 // Locate 'from' character of string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005285 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005286 // As "from" is a smi it is 2 times the value which matches the size of a two
5287 // byte character.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005288 __ add(r5, r5, Operand(from));
5289 from = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005290
5291 // r0: result string.
5292 // r1: first character of result.
5293 // r2: result length.
5294 // r5: first character of string to copy.
5295 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005296 StringHelper::GenerateCopyCharactersLong(
5297 masm, r1, r5, r2, r3, r4, r6, r7, r9, DEST_ALWAYS_ALIGNED);
Steve Block44f0eee2011-05-26 01:26:41 +01005298 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005299 __ add(sp, sp, Operand(3 * kPointerSize));
5300 __ Ret();
5301
5302 // Just jump to runtime to create the sub string.
5303 __ bind(&runtime);
5304 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5305}
5306
5307
5308void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5309 Register left,
5310 Register right,
5311 Register scratch1,
5312 Register scratch2,
5313 Register scratch3,
5314 Register scratch4) {
5315 Label compare_lengths;
5316 // Find minimum length and length difference.
5317 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
5318 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
5319 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
5320 Register length_delta = scratch3;
5321 __ mov(scratch1, scratch2, LeaveCC, gt);
5322 Register min_length = scratch1;
5323 STATIC_ASSERT(kSmiTag == 0);
5324 __ tst(min_length, Operand(min_length));
5325 __ b(eq, &compare_lengths);
5326
5327 // Untag smi.
5328 __ mov(min_length, Operand(min_length, ASR, kSmiTagSize));
5329
5330 // Setup registers so that we only need to increment one register
5331 // in the loop.
5332 __ add(scratch2, min_length,
5333 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5334 __ add(left, left, Operand(scratch2));
5335 __ add(right, right, Operand(scratch2));
5336 // Registers left and right points to the min_length character of strings.
5337 __ rsb(min_length, min_length, Operand(-1));
5338 Register index = min_length;
5339 // Index starts at -min_length.
5340
5341 {
5342 // Compare loop.
5343 Label loop;
5344 __ bind(&loop);
5345 // Compare characters.
5346 __ add(index, index, Operand(1), SetCC);
5347 __ ldrb(scratch2, MemOperand(left, index), ne);
5348 __ ldrb(scratch4, MemOperand(right, index), ne);
5349 // Skip to compare lengths with eq condition true.
5350 __ b(eq, &compare_lengths);
5351 __ cmp(scratch2, scratch4);
5352 __ b(eq, &loop);
5353 // Fallthrough with eq condition false.
5354 }
5355 // Compare lengths - strings up to min-length are equal.
5356 __ bind(&compare_lengths);
5357 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
5358 // Use zero length_delta as result.
5359 __ mov(r0, Operand(length_delta), SetCC, eq);
5360 // Fall through to here if characters compare not-equal.
5361 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
5362 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
5363 __ Ret();
5364}
5365
5366
5367void StringCompareStub::Generate(MacroAssembler* masm) {
5368 Label runtime;
5369
Steve Block44f0eee2011-05-26 01:26:41 +01005370 Counters* counters = masm->isolate()->counters();
5371
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005372 // Stack frame on entry.
5373 // sp[0]: right string
5374 // sp[4]: left string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005375 __ Ldrd(r0 , r1, MemOperand(sp)); // Load right in r0, left in r1.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005376
5377 Label not_same;
5378 __ cmp(r0, r1);
5379 __ b(ne, &not_same);
5380 STATIC_ASSERT(EQUAL == 0);
5381 STATIC_ASSERT(kSmiTag == 0);
5382 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01005383 __ IncrementCounter(counters->string_compare_native(), 1, r1, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005384 __ add(sp, sp, Operand(2 * kPointerSize));
5385 __ Ret();
5386
5387 __ bind(&not_same);
5388
Steve Block44f0eee2011-05-26 01:26:41 +01005389 // Check that both objects are sequential ASCII strings.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005390 __ JumpIfNotBothSequentialAsciiStrings(r1, r0, r2, r3, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005391
Steve Block44f0eee2011-05-26 01:26:41 +01005392 // Compare flat ASCII strings natively. Remove arguments from stack first.
5393 __ IncrementCounter(counters->string_compare_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005394 __ add(sp, sp, Operand(2 * kPointerSize));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005395 GenerateCompareFlatAsciiStrings(masm, r1, r0, r2, r3, r4, r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005396
5397 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5398 // tagged as a small integer.
5399 __ bind(&runtime);
5400 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5401}
5402
5403
5404void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005405 Label string_add_runtime, call_builtin;
5406 Builtins::JavaScript builtin_id = Builtins::ADD;
5407
Steve Block44f0eee2011-05-26 01:26:41 +01005408 Counters* counters = masm->isolate()->counters();
5409
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005410 // Stack on entry:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005411 // sp[0]: second argument (right).
5412 // sp[4]: first argument (left).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005413
5414 // Load the two arguments.
5415 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
5416 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
5417
5418 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005419 if (flags_ == NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005420 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
5421 // Load instance types.
5422 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5423 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5424 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5425 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5426 STATIC_ASSERT(kStringTag == 0);
5427 // If either is not a string, go to runtime.
5428 __ tst(r4, Operand(kIsNotStringMask));
5429 __ tst(r5, Operand(kIsNotStringMask), eq);
5430 __ b(ne, &string_add_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005431 } else {
5432 // Here at least one of the arguments is definitely a string.
5433 // We convert the one that is not known to be a string.
5434 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5435 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5436 GenerateConvertArgument(
5437 masm, 1 * kPointerSize, r0, r2, r3, r4, r5, &call_builtin);
5438 builtin_id = Builtins::STRING_ADD_RIGHT;
5439 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5440 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5441 GenerateConvertArgument(
5442 masm, 0 * kPointerSize, r1, r2, r3, r4, r5, &call_builtin);
5443 builtin_id = Builtins::STRING_ADD_LEFT;
5444 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005445 }
5446
5447 // Both arguments are strings.
5448 // r0: first string
5449 // r1: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005450 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5451 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005452 {
5453 Label strings_not_empty;
5454 // Check if either of the strings are empty. In that case return the other.
5455 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
5456 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
5457 STATIC_ASSERT(kSmiTag == 0);
5458 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
5459 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
5460 STATIC_ASSERT(kSmiTag == 0);
5461 // Else test if second string is empty.
5462 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
5463 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
5464
Steve Block44f0eee2011-05-26 01:26:41 +01005465 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005466 __ add(sp, sp, Operand(2 * kPointerSize));
5467 __ Ret();
5468
5469 __ bind(&strings_not_empty);
5470 }
5471
5472 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
5473 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
5474 // Both strings are non-empty.
5475 // r0: first string
5476 // r1: second string
5477 // r2: length of first string
5478 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005479 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5480 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005481 // Look at the length of the result of adding the two strings.
5482 Label string_add_flat_result, longer_than_two;
5483 // Adding two lengths can't overflow.
5484 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
5485 __ add(r6, r2, Operand(r3));
Steve Block44f0eee2011-05-26 01:26:41 +01005486 // Use the symbol table when adding two one character strings, as it
5487 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005488 __ cmp(r6, Operand(2));
5489 __ b(ne, &longer_than_two);
5490
Steve Block44f0eee2011-05-26 01:26:41 +01005491 // Check that both strings are non-external ASCII strings.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005492 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005493 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5494 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5495 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5496 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5497 }
5498 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
5499 &string_add_runtime);
5500
5501 // Get the two characters forming the sub string.
5502 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
5503 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
5504
5505 // Try to lookup two character string in symbol table. If it is not found
5506 // just allocate a new one.
5507 Label make_two_character_string;
5508 StringHelper::GenerateTwoCharacterSymbolTableProbe(
5509 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01005510 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005511 __ add(sp, sp, Operand(2 * kPointerSize));
5512 __ Ret();
5513
5514 __ bind(&make_two_character_string);
5515 // Resulting string has length 2 and first chars of two strings
5516 // are combined into single halfword in r2 register.
5517 // So we can fill resulting string without two loops by a single
5518 // halfword store instruction (which assumes that processor is
5519 // in a little endian mode)
5520 __ mov(r6, Operand(2));
5521 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
5522 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005523 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005524 __ add(sp, sp, Operand(2 * kPointerSize));
5525 __ Ret();
5526
5527 __ bind(&longer_than_two);
5528 // Check if resulting string will be flat.
5529 __ cmp(r6, Operand(String::kMinNonFlatLength));
5530 __ b(lt, &string_add_flat_result);
5531 // Handle exceptionally long strings in the runtime system.
5532 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
5533 ASSERT(IsPowerOf2(String::kMaxLength + 1));
5534 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
5535 __ cmp(r6, Operand(String::kMaxLength + 1));
5536 __ b(hs, &string_add_runtime);
5537
5538 // If result is not supposed to be flat, allocate a cons string object.
Steve Block44f0eee2011-05-26 01:26:41 +01005539 // If both strings are ASCII the result is an ASCII cons string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005540 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005541 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5542 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5543 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5544 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5545 }
5546 Label non_ascii, allocated, ascii_data;
5547 STATIC_ASSERT(kTwoByteStringTag == 0);
5548 __ tst(r4, Operand(kStringEncodingMask));
5549 __ tst(r5, Operand(kStringEncodingMask), ne);
5550 __ b(eq, &non_ascii);
5551
5552 // Allocate an ASCII cons string.
5553 __ bind(&ascii_data);
5554 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
5555 __ bind(&allocated);
5556 // Fill the fields of the cons string.
5557 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
5558 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
5559 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01005560 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005561 __ add(sp, sp, Operand(2 * kPointerSize));
5562 __ Ret();
5563
5564 __ bind(&non_ascii);
5565 // At least one of the strings is two-byte. Check whether it happens
Steve Block44f0eee2011-05-26 01:26:41 +01005566 // to contain only ASCII characters.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005567 // r4: first instance type.
5568 // r5: second instance type.
5569 __ tst(r4, Operand(kAsciiDataHintMask));
5570 __ tst(r5, Operand(kAsciiDataHintMask), ne);
5571 __ b(ne, &ascii_data);
5572 __ eor(r4, r4, Operand(r5));
5573 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5574 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
5575 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
5576 __ b(eq, &ascii_data);
5577
5578 // Allocate a two byte cons string.
5579 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
5580 __ jmp(&allocated);
5581
5582 // Handle creating a flat result. First check that both strings are
5583 // sequential and that they have the same encoding.
5584 // r0: first string
5585 // r1: second string
5586 // r2: length of first string
5587 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005588 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5589 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005590 // r6: sum of lengths.
5591 __ bind(&string_add_flat_result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005592 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005593 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5594 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5595 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5596 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5597 }
5598 // Check that both strings are sequential.
5599 STATIC_ASSERT(kSeqStringTag == 0);
5600 __ tst(r4, Operand(kStringRepresentationMask));
5601 __ tst(r5, Operand(kStringRepresentationMask), eq);
5602 __ b(ne, &string_add_runtime);
5603 // Now check if both strings have the same encoding (ASCII/Two-byte).
5604 // r0: first string.
5605 // r1: second string.
5606 // r2: length of first string.
5607 // r3: length of second string.
5608 // r6: sum of lengths..
5609 Label non_ascii_string_add_flat_result;
5610 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
5611 __ eor(r7, r4, Operand(r5));
5612 __ tst(r7, Operand(kStringEncodingMask));
5613 __ b(ne, &string_add_runtime);
5614 // And see if it's ASCII or two-byte.
5615 __ tst(r4, Operand(kStringEncodingMask));
5616 __ b(eq, &non_ascii_string_add_flat_result);
5617
5618 // Both strings are sequential ASCII strings. We also know that they are
5619 // short (since the sum of the lengths is less than kMinNonFlatLength).
5620 // r6: length of resulting flat string
5621 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
5622 // Locate first character of result.
5623 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5624 // Locate first character of first argument.
5625 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5626 // r0: first character of first string.
5627 // r1: second string.
5628 // r2: length of first string.
5629 // r3: length of second string.
5630 // r6: first character of result.
5631 // r7: result string.
5632 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
5633
5634 // Load second argument and locate first character.
5635 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5636 // r1: first character of second string.
5637 // r3: length of second string.
5638 // r6: next character of result.
5639 // r7: result string.
5640 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
5641 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01005642 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005643 __ add(sp, sp, Operand(2 * kPointerSize));
5644 __ Ret();
5645
5646 __ bind(&non_ascii_string_add_flat_result);
5647 // Both strings are sequential two byte strings.
5648 // r0: first string.
5649 // r1: second string.
5650 // r2: length of first string.
5651 // r3: length of second string.
5652 // r6: sum of length of strings.
5653 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
5654 // r0: first string.
5655 // r1: second string.
5656 // r2: length of first string.
5657 // r3: length of second string.
5658 // r7: result string.
5659
5660 // Locate first character of result.
5661 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5662 // Locate first character of first argument.
5663 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5664
5665 // r0: first character of first string.
5666 // r1: second string.
5667 // r2: length of first string.
5668 // r3: length of second string.
5669 // r6: first character of result.
5670 // r7: result string.
5671 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
5672
5673 // Locate first character of second argument.
5674 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5675
5676 // r1: first character of second string.
5677 // r3: length of second string.
5678 // r6: next character of result (after copy of first string).
5679 // r7: result string.
5680 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
5681
5682 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01005683 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005684 __ add(sp, sp, Operand(2 * kPointerSize));
5685 __ Ret();
5686
5687 // Just jump to runtime to add the two strings.
5688 __ bind(&string_add_runtime);
5689 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005690
5691 if (call_builtin.is_linked()) {
5692 __ bind(&call_builtin);
5693 __ InvokeBuiltin(builtin_id, JUMP_JS);
5694 }
5695}
5696
5697
5698void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5699 int stack_offset,
5700 Register arg,
5701 Register scratch1,
5702 Register scratch2,
5703 Register scratch3,
5704 Register scratch4,
5705 Label* slow) {
5706 // First check if the argument is already a string.
5707 Label not_string, done;
5708 __ JumpIfSmi(arg, &not_string);
5709 __ CompareObjectType(arg, scratch1, scratch1, FIRST_NONSTRING_TYPE);
5710 __ b(lt, &done);
5711
5712 // Check the number to string cache.
5713 Label not_cached;
5714 __ bind(&not_string);
5715 // Puts the cached result into scratch1.
5716 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5717 arg,
5718 scratch1,
5719 scratch2,
5720 scratch3,
5721 scratch4,
5722 false,
5723 &not_cached);
5724 __ mov(arg, scratch1);
5725 __ str(arg, MemOperand(sp, stack_offset));
5726 __ jmp(&done);
5727
5728 // Check if the argument is a safe string wrapper.
5729 __ bind(&not_cached);
5730 __ JumpIfSmi(arg, slow);
5731 __ CompareObjectType(
5732 arg, scratch1, scratch2, JS_VALUE_TYPE); // map -> scratch1.
5733 __ b(ne, slow);
5734 __ ldrb(scratch2, FieldMemOperand(scratch1, Map::kBitField2Offset));
5735 __ and_(scratch2,
5736 scratch2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
5737 __ cmp(scratch2,
5738 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
5739 __ b(ne, slow);
5740 __ ldr(arg, FieldMemOperand(arg, JSValue::kValueOffset));
5741 __ str(arg, MemOperand(sp, stack_offset));
5742
5743 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005744}
5745
5746
Ben Murdochb0fe1622011-05-05 13:52:32 +01005747void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5748 ASSERT(state_ == CompareIC::SMIS);
5749 Label miss;
5750 __ orr(r2, r1, r0);
5751 __ tst(r2, Operand(kSmiTagMask));
5752 __ b(ne, &miss);
5753
5754 if (GetCondition() == eq) {
5755 // For equality we do not care about the sign of the result.
5756 __ sub(r0, r0, r1, SetCC);
5757 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01005758 // Untag before subtracting to avoid handling overflow.
5759 __ SmiUntag(r1);
5760 __ sub(r0, r1, SmiUntagOperand(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005761 }
5762 __ Ret();
5763
5764 __ bind(&miss);
5765 GenerateMiss(masm);
5766}
5767
5768
5769void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5770 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5771
5772 Label generic_stub;
5773 Label unordered;
5774 Label miss;
5775 __ and_(r2, r1, Operand(r0));
5776 __ tst(r2, Operand(kSmiTagMask));
5777 __ b(eq, &generic_stub);
5778
5779 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
5780 __ b(ne, &miss);
5781 __ CompareObjectType(r1, r2, r2, HEAP_NUMBER_TYPE);
5782 __ b(ne, &miss);
5783
5784 // Inlining the double comparison and falling back to the general compare
5785 // stub if NaN is involved or VFP3 is unsupported.
Ben Murdoch8b112d22011-06-08 16:22:53 +01005786 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01005787 CpuFeatures::Scope scope(VFP3);
5788
5789 // Load left and right operand
5790 __ sub(r2, r1, Operand(kHeapObjectTag));
5791 __ vldr(d0, r2, HeapNumber::kValueOffset);
5792 __ sub(r2, r0, Operand(kHeapObjectTag));
5793 __ vldr(d1, r2, HeapNumber::kValueOffset);
5794
5795 // Compare operands
Ben Murdochb8e0da22011-05-16 14:20:40 +01005796 __ VFPCompareAndSetFlags(d0, d1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005797
5798 // Don't base result on status bits when a NaN is involved.
5799 __ b(vs, &unordered);
5800
5801 // Return a result of -1, 0, or 1, based on status bits.
5802 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5803 __ mov(r0, Operand(LESS), LeaveCC, lt);
5804 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5805 __ Ret();
5806
5807 __ bind(&unordered);
5808 }
5809
5810 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS, r1, r0);
5811 __ bind(&generic_stub);
5812 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
5813
5814 __ bind(&miss);
5815 GenerateMiss(masm);
5816}
5817
5818
5819void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
5820 ASSERT(state_ == CompareIC::OBJECTS);
5821 Label miss;
5822 __ and_(r2, r1, Operand(r0));
5823 __ tst(r2, Operand(kSmiTagMask));
5824 __ b(eq, &miss);
5825
5826 __ CompareObjectType(r0, r2, r2, JS_OBJECT_TYPE);
5827 __ b(ne, &miss);
5828 __ CompareObjectType(r1, r2, r2, JS_OBJECT_TYPE);
5829 __ b(ne, &miss);
5830
5831 ASSERT(GetCondition() == eq);
5832 __ sub(r0, r0, Operand(r1));
5833 __ Ret();
5834
5835 __ bind(&miss);
5836 GenerateMiss(masm);
5837}
5838
5839
5840void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
5841 __ Push(r1, r0);
5842 __ push(lr);
5843
5844 // Call the runtime system in a fresh internal frame.
Steve Block44f0eee2011-05-26 01:26:41 +01005845 ExternalReference miss =
5846 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01005847 __ EnterInternalFrame();
5848 __ Push(r1, r0);
5849 __ mov(ip, Operand(Smi::FromInt(op_)));
5850 __ push(ip);
5851 __ CallExternalReference(miss, 3);
5852 __ LeaveInternalFrame();
5853 // Compute the entry point of the rewritten stub.
5854 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
5855 // Restore registers.
5856 __ pop(lr);
5857 __ pop(r0);
5858 __ pop(r1);
5859 __ Jump(r2);
5860}
5861
5862
Steve Block1e0659c2011-05-24 12:43:12 +01005863void DirectCEntryStub::Generate(MacroAssembler* masm) {
5864 __ ldr(pc, MemOperand(sp, 0));
5865}
5866
5867
5868void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005869 ExternalReference function) {
5870 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
5871 RelocInfo::CODE_TARGET));
5872 __ mov(r2, Operand(function));
5873 // Push return address (accessible to GC through exit frame pc).
5874 __ str(pc, MemOperand(sp, 0));
5875 __ Jump(r2); // Call the api function.
5876}
5877
5878
5879void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
5880 Register target) {
Steve Block1e0659c2011-05-24 12:43:12 +01005881 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
5882 RelocInfo::CODE_TARGET));
5883 // Push return address (accessible to GC through exit frame pc).
Steve Block1e0659c2011-05-24 12:43:12 +01005884 __ str(pc, MemOperand(sp, 0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005885 __ Jump(target); // Call the C++ function.
Steve Block1e0659c2011-05-24 12:43:12 +01005886}
5887
5888
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005889#undef __
5890
5891} } // namespace v8::internal
5892
5893#endif // V8_TARGET_ARCH_ARM