blob: eaad9f293b330eb0936a5dceeb45ae51bb5f840b [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
Ben Murdoch257744e2011-11-30 15:57:28 +000058// Check if the operand is a heap number.
59static void EmitCheckForHeapNumber(MacroAssembler* masm, Register operand,
60 Register scratch1, Register scratch2,
61 Label* not_a_heap_number) {
62 __ ldr(scratch1, FieldMemOperand(operand, HeapObject::kMapOffset));
63 __ LoadRoot(scratch2, Heap::kHeapNumberMapRootIndex);
64 __ cmp(scratch1, scratch2);
65 __ b(ne, not_a_heap_number);
66}
67
68
Steve Block1e0659c2011-05-24 12:43:12 +010069void ToNumberStub::Generate(MacroAssembler* masm) {
70 // The ToNumber stub takes one argument in eax.
71 Label check_heap_number, call_builtin;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000072 __ JumpIfNotSmi(r0, &check_heap_number);
Steve Block1e0659c2011-05-24 12:43:12 +010073 __ Ret();
74
75 __ bind(&check_heap_number);
Ben Murdoch257744e2011-11-30 15:57:28 +000076 EmitCheckForHeapNumber(masm, r0, r1, ip, &call_builtin);
Steve Block1e0659c2011-05-24 12:43:12 +010077 __ Ret();
78
79 __ bind(&call_builtin);
80 __ push(r0);
Ben Murdoch257744e2011-11-30 15:57:28 +000081 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +010082}
83
84
Kristian Monsen80d68ea2010-09-08 11:05:35 +010085void FastNewClosureStub::Generate(MacroAssembler* masm) {
86 // Create a new closure from the given function info in new
87 // space. Set the context to the current context in cp.
88 Label gc;
89
90 // Pop the function info from the stack.
91 __ pop(r3);
92
93 // Attempt to allocate new JSFunction in new space.
94 __ AllocateInNewSpace(JSFunction::kSize,
95 r0,
96 r1,
97 r2,
98 &gc,
99 TAG_OBJECT);
100
Steve Block44f0eee2011-05-26 01:26:41 +0100101 int map_index = strict_mode_ == kStrictMode
102 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
103 : Context::FUNCTION_MAP_INDEX;
104
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100105 // Compute the function map in the current global context and set that
106 // as the map of the allocated object.
107 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
108 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100109 __ ldr(r2, MemOperand(r2, Context::SlotOffset(map_index)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100110 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
111
112 // Initialize the rest of the function. We don't have to update the
113 // write barrier because the allocated object is in new space.
114 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
115 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100116 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100117 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
118 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
119 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
120 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
121 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
122 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100123 __ str(r4, FieldMemOperand(r0, JSFunction::kNextFunctionLinkOffset));
124
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100125
126 // Initialize the code pointer in the function to be the one
127 // found in the shared function info object.
128 __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset));
129 __ add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
130 __ str(r3, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
131
132 // Return result. The argument function info has been popped already.
133 __ Ret();
134
135 // Create a new closure through the slower runtime call.
136 __ bind(&gc);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800137 __ LoadRoot(r4, Heap::kFalseValueRootIndex);
138 __ Push(cp, r3, r4);
139 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100140}
141
142
143void FastNewContextStub::Generate(MacroAssembler* masm) {
144 // Try to allocate the context in new space.
145 Label gc;
146 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
147
148 // Attempt to allocate the context in new space.
149 __ AllocateInNewSpace(FixedArray::SizeFor(length),
150 r0,
151 r1,
152 r2,
153 &gc,
154 TAG_OBJECT);
155
156 // Load the function from the stack.
157 __ ldr(r3, MemOperand(sp, 0));
158
159 // Setup the object header.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000160 __ LoadRoot(r2, Heap::kFunctionContextMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100161 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
162 __ mov(r2, Operand(Smi::FromInt(length)));
163 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
164
165 // Setup the fixed slots.
166 __ mov(r1, Operand(Smi::FromInt(0)));
167 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000168 __ str(cp, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100169 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
170
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000171 // Copy the global object from the previous context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100172 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
173 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
174
175 // Initialize the rest of the slots to undefined.
176 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
177 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
178 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
179 }
180
181 // Remove the on-stack argument and return.
182 __ mov(cp, r0);
183 __ pop();
184 __ Ret();
185
186 // Need to collect. Call into runtime system.
187 __ bind(&gc);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000188 __ TailCallRuntime(Runtime::kNewFunctionContext, 1, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100189}
190
191
192void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
193 // Stack layout on entry:
194 //
195 // [sp]: constant elements.
196 // [sp + kPointerSize]: literal index.
197 // [sp + (2 * kPointerSize)]: literals array.
198
199 // All sizes here are multiples of kPointerSize.
200 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
201 int size = JSArray::kSize + elements_size;
202
203 // Load boilerplate object into r3 and check if we need to create a
204 // boilerplate.
205 Label slow_case;
206 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
207 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
208 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
209 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
210 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
211 __ cmp(r3, ip);
212 __ b(eq, &slow_case);
213
214 if (FLAG_debug_code) {
215 const char* message;
216 Heap::RootListIndex expected_map_index;
217 if (mode_ == CLONE_ELEMENTS) {
218 message = "Expected (writable) fixed array";
219 expected_map_index = Heap::kFixedArrayMapRootIndex;
220 } else {
221 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
222 message = "Expected copy-on-write fixed array";
223 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
224 }
225 __ push(r3);
226 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
227 __ ldr(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
228 __ LoadRoot(ip, expected_map_index);
229 __ cmp(r3, ip);
230 __ Assert(eq, message);
231 __ pop(r3);
232 }
233
234 // Allocate both the JS array and the elements array in one big
235 // allocation. This avoids multiple limit checks.
236 __ AllocateInNewSpace(size,
237 r0,
238 r1,
239 r2,
240 &slow_case,
241 TAG_OBJECT);
242
243 // Copy the JS array part.
244 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
245 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
246 __ ldr(r1, FieldMemOperand(r3, i));
247 __ str(r1, FieldMemOperand(r0, i));
248 }
249 }
250
251 if (length_ > 0) {
252 // Get hold of the elements array of the boilerplate and setup the
253 // elements pointer in the resulting object.
254 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
255 __ add(r2, r0, Operand(JSArray::kSize));
256 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
257
258 // Copy the elements array.
259 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
260 }
261
262 // Return and remove the on-stack parameters.
263 __ add(sp, sp, Operand(3 * kPointerSize));
264 __ Ret();
265
266 __ bind(&slow_case);
267 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
268}
269
270
271// Takes a Smi and converts to an IEEE 64 bit floating point value in two
272// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
273// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
274// scratch register. Destroys the source register. No GC occurs during this
275// stub so you don't have to set up the frame.
276class ConvertToDoubleStub : public CodeStub {
277 public:
278 ConvertToDoubleStub(Register result_reg_1,
279 Register result_reg_2,
280 Register source_reg,
281 Register scratch_reg)
282 : result1_(result_reg_1),
283 result2_(result_reg_2),
284 source_(source_reg),
285 zeros_(scratch_reg) { }
286
287 private:
288 Register result1_;
289 Register result2_;
290 Register source_;
291 Register zeros_;
292
293 // Minor key encoding in 16 bits.
294 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
295 class OpBits: public BitField<Token::Value, 2, 14> {};
296
297 Major MajorKey() { return ConvertToDouble; }
298 int MinorKey() {
299 // Encode the parameters in a unique 16 bit value.
300 return result1_.code() +
301 (result2_.code() << 4) +
302 (source_.code() << 8) +
303 (zeros_.code() << 12);
304 }
305
306 void Generate(MacroAssembler* masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100307};
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 +0100367void FloatingPointHelper::LoadSmis(MacroAssembler* masm,
368 FloatingPointHelper::Destination destination,
369 Register scratch1,
370 Register scratch2) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100371 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100372 CpuFeatures::Scope scope(VFP3);
373 __ mov(scratch1, Operand(r0, ASR, kSmiTagSize));
374 __ vmov(d7.high(), scratch1);
375 __ vcvt_f64_s32(d7, d7.high());
376 __ mov(scratch1, Operand(r1, ASR, kSmiTagSize));
377 __ vmov(d6.high(), scratch1);
378 __ vcvt_f64_s32(d6, d6.high());
379 if (destination == kCoreRegisters) {
380 __ vmov(r2, r3, d7);
381 __ vmov(r0, r1, d6);
382 }
383 } else {
384 ASSERT(destination == kCoreRegisters);
385 // Write Smi from r0 to r3 and r2 in double format.
386 __ mov(scratch1, Operand(r0));
387 ConvertToDoubleStub stub1(r3, r2, scratch1, scratch2);
388 __ push(lr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000389 __ Call(stub1.GetCode());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100390 // Write Smi from r1 to r1 and r0 in double format.
Steve Block1e0659c2011-05-24 12:43:12 +0100391 __ mov(scratch1, Operand(r1));
392 ConvertToDoubleStub stub2(r1, r0, scratch1, scratch2);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000393 __ Call(stub2.GetCode());
Steve Block1e0659c2011-05-24 12:43:12 +0100394 __ pop(lr);
395 }
396}
397
398
399void FloatingPointHelper::LoadOperands(
400 MacroAssembler* masm,
401 FloatingPointHelper::Destination destination,
402 Register heap_number_map,
403 Register scratch1,
404 Register scratch2,
405 Label* slow) {
406
407 // Load right operand (r0) to d6 or r2/r3.
408 LoadNumber(masm, destination,
409 r0, d7, r2, r3, heap_number_map, scratch1, scratch2, slow);
410
411 // Load left operand (r1) to d7 or r0/r1.
412 LoadNumber(masm, destination,
413 r1, d6, r0, r1, heap_number_map, scratch1, scratch2, slow);
414}
415
416
417void FloatingPointHelper::LoadNumber(MacroAssembler* masm,
418 Destination destination,
419 Register object,
420 DwVfpRegister dst,
421 Register dst1,
422 Register dst2,
423 Register heap_number_map,
424 Register scratch1,
425 Register scratch2,
426 Label* not_number) {
427 if (FLAG_debug_code) {
428 __ AbortIfNotRootValue(heap_number_map,
429 Heap::kHeapNumberMapRootIndex,
430 "HeapNumberMap register clobbered.");
431 }
432
433 Label is_smi, done;
434
435 __ JumpIfSmi(object, &is_smi);
436 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_number);
437
438 // Handle loading a double from a heap number.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100439 if (CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100440 destination == kVFPRegisters) {
Steve Block1e0659c2011-05-24 12:43:12 +0100441 CpuFeatures::Scope scope(VFP3);
442 // Load the double from tagged HeapNumber to double register.
443 __ sub(scratch1, object, Operand(kHeapObjectTag));
444 __ vldr(dst, scratch1, HeapNumber::kValueOffset);
445 } else {
446 ASSERT(destination == kCoreRegisters);
447 // Load the double from heap number to dst1 and dst2 in double format.
448 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
449 }
450 __ jmp(&done);
451
452 // Handle loading a double from a smi.
453 __ bind(&is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100454 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100455 CpuFeatures::Scope scope(VFP3);
456 // Convert smi to double using VFP instructions.
457 __ SmiUntag(scratch1, object);
458 __ vmov(dst.high(), scratch1);
459 __ vcvt_f64_s32(dst, dst.high());
460 if (destination == kCoreRegisters) {
461 // Load the converted smi to dst1 and dst2 in double format.
462 __ vmov(dst1, dst2, dst);
463 }
464 } else {
465 ASSERT(destination == kCoreRegisters);
466 // Write smi to dst1 and dst2 double format.
467 __ mov(scratch1, Operand(object));
468 ConvertToDoubleStub stub(dst2, dst1, scratch1, scratch2);
469 __ push(lr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000470 __ Call(stub.GetCode());
Steve Block1e0659c2011-05-24 12:43:12 +0100471 __ pop(lr);
472 }
473
474 __ bind(&done);
475}
476
477
Steve Block44f0eee2011-05-26 01:26:41 +0100478void FloatingPointHelper::ConvertNumberToInt32(MacroAssembler* masm,
479 Register object,
480 Register dst,
481 Register heap_number_map,
482 Register scratch1,
483 Register scratch2,
484 Register scratch3,
485 DwVfpRegister double_scratch,
486 Label* not_number) {
Steve Block1e0659c2011-05-24 12:43:12 +0100487 if (FLAG_debug_code) {
488 __ AbortIfNotRootValue(heap_number_map,
489 Heap::kHeapNumberMapRootIndex,
490 "HeapNumberMap register clobbered.");
491 }
Steve Block44f0eee2011-05-26 01:26:41 +0100492 Label is_smi;
493 Label done;
494 Label not_in_int32_range;
495
Steve Block1e0659c2011-05-24 12:43:12 +0100496 __ JumpIfSmi(object, &is_smi);
497 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kMapOffset));
498 __ cmp(scratch1, heap_number_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100499 __ b(ne, not_number);
500 __ ConvertToInt32(object,
501 dst,
502 scratch1,
503 scratch2,
504 double_scratch,
505 &not_in_int32_range);
Steve Block1e0659c2011-05-24 12:43:12 +0100506 __ jmp(&done);
Steve Block44f0eee2011-05-26 01:26:41 +0100507
508 __ bind(&not_in_int32_range);
509 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
510 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
511
512 __ EmitOutOfInt32RangeTruncate(dst,
513 scratch1,
514 scratch2,
515 scratch3);
516 __ jmp(&done);
517
Steve Block1e0659c2011-05-24 12:43:12 +0100518 __ bind(&is_smi);
519 __ SmiUntag(dst, object);
520 __ bind(&done);
521}
522
523
Ben Murdoch257744e2011-11-30 15:57:28 +0000524void FloatingPointHelper::ConvertIntToDouble(MacroAssembler* masm,
525 Register int_scratch,
526 Destination destination,
527 DwVfpRegister double_dst,
528 Register dst1,
529 Register dst2,
530 Register scratch2,
531 SwVfpRegister single_scratch) {
532 ASSERT(!int_scratch.is(scratch2));
533 ASSERT(!int_scratch.is(dst1));
534 ASSERT(!int_scratch.is(dst2));
535
536 Label done;
537
538 if (CpuFeatures::IsSupported(VFP3)) {
539 CpuFeatures::Scope scope(VFP3);
540 __ vmov(single_scratch, int_scratch);
541 __ vcvt_f64_s32(double_dst, single_scratch);
542 if (destination == kCoreRegisters) {
543 __ vmov(dst1, dst2, double_dst);
544 }
545 } else {
546 Label fewer_than_20_useful_bits;
547 // Expected output:
548 // | dst2 | dst1 |
549 // | s | exp | mantissa |
550
551 // Check for zero.
552 __ cmp(int_scratch, Operand(0));
553 __ mov(dst2, int_scratch);
554 __ mov(dst1, int_scratch);
555 __ b(eq, &done);
556
557 // Preload the sign of the value.
558 __ and_(dst2, int_scratch, Operand(HeapNumber::kSignMask), SetCC);
559 // Get the absolute value of the object (as an unsigned integer).
560 __ rsb(int_scratch, int_scratch, Operand(0), SetCC, mi);
561
562 // Get mantisssa[51:20].
563
564 // Get the position of the first set bit.
565 __ CountLeadingZeros(dst1, int_scratch, scratch2);
566 __ rsb(dst1, dst1, Operand(31));
567
568 // Set the exponent.
569 __ add(scratch2, dst1, Operand(HeapNumber::kExponentBias));
570 __ Bfi(dst2, scratch2, scratch2,
571 HeapNumber::kExponentShift, HeapNumber::kExponentBits);
572
573 // Clear the first non null bit.
574 __ mov(scratch2, Operand(1));
575 __ bic(int_scratch, int_scratch, Operand(scratch2, LSL, dst1));
576
577 __ cmp(dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
578 // Get the number of bits to set in the lower part of the mantissa.
579 __ sub(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord), SetCC);
580 __ b(mi, &fewer_than_20_useful_bits);
581 // Set the higher 20 bits of the mantissa.
582 __ orr(dst2, dst2, Operand(int_scratch, LSR, scratch2));
583 __ rsb(scratch2, scratch2, Operand(32));
584 __ mov(dst1, Operand(int_scratch, LSL, scratch2));
585 __ b(&done);
586
587 __ bind(&fewer_than_20_useful_bits);
588 __ rsb(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
589 __ mov(scratch2, Operand(int_scratch, LSL, scratch2));
590 __ orr(dst2, dst2, scratch2);
591 // Set dst1 to 0.
592 __ mov(dst1, Operand(0));
593 }
594 __ bind(&done);
595}
596
597
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100598void FloatingPointHelper::LoadNumberAsInt32Double(MacroAssembler* masm,
599 Register object,
600 Destination destination,
601 DwVfpRegister double_dst,
602 Register dst1,
603 Register dst2,
604 Register heap_number_map,
605 Register scratch1,
606 Register scratch2,
607 SwVfpRegister single_scratch,
608 Label* not_int32) {
609 ASSERT(!scratch1.is(object) && !scratch2.is(object));
610 ASSERT(!scratch1.is(scratch2));
611 ASSERT(!heap_number_map.is(object) &&
612 !heap_number_map.is(scratch1) &&
613 !heap_number_map.is(scratch2));
614
615 Label done, obj_is_not_smi;
616
617 __ JumpIfNotSmi(object, &obj_is_not_smi);
618 __ SmiUntag(scratch1, object);
Ben Murdoch257744e2011-11-30 15:57:28 +0000619 ConvertIntToDouble(masm, scratch1, destination, double_dst, dst1, dst2,
620 scratch2, single_scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100621 __ b(&done);
622
623 __ bind(&obj_is_not_smi);
624 if (FLAG_debug_code) {
625 __ AbortIfNotRootValue(heap_number_map,
626 Heap::kHeapNumberMapRootIndex,
627 "HeapNumberMap register clobbered.");
628 }
629 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
630
631 // Load the number.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100632 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100633 CpuFeatures::Scope scope(VFP3);
634 // Load the double value.
635 __ sub(scratch1, object, Operand(kHeapObjectTag));
636 __ vldr(double_dst, scratch1, HeapNumber::kValueOffset);
637
638 __ EmitVFPTruncate(kRoundToZero,
639 single_scratch,
640 double_dst,
641 scratch1,
642 scratch2,
643 kCheckForInexactConversion);
644
645 // Jump to not_int32 if the operation did not succeed.
646 __ b(ne, not_int32);
647
648 if (destination == kCoreRegisters) {
649 __ vmov(dst1, dst2, double_dst);
650 }
651
652 } else {
653 ASSERT(!scratch1.is(object) && !scratch2.is(object));
654 // Load the double value in the destination registers..
655 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
656
657 // Check for 0 and -0.
658 __ bic(scratch1, dst1, Operand(HeapNumber::kSignMask));
659 __ orr(scratch1, scratch1, Operand(dst2));
660 __ cmp(scratch1, Operand(0));
661 __ b(eq, &done);
662
663 // Check that the value can be exactly represented by a 32-bit integer.
664 // Jump to not_int32 if that's not the case.
665 DoubleIs32BitInteger(masm, dst1, dst2, scratch1, scratch2, not_int32);
666
667 // dst1 and dst2 were trashed. Reload the double value.
668 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
669 }
670
671 __ bind(&done);
672}
673
674
675void FloatingPointHelper::LoadNumberAsInt32(MacroAssembler* masm,
676 Register object,
677 Register dst,
678 Register heap_number_map,
679 Register scratch1,
680 Register scratch2,
681 Register scratch3,
682 DwVfpRegister double_scratch,
683 Label* not_int32) {
684 ASSERT(!dst.is(object));
685 ASSERT(!scratch1.is(object) && !scratch2.is(object) && !scratch3.is(object));
686 ASSERT(!scratch1.is(scratch2) &&
687 !scratch1.is(scratch3) &&
688 !scratch2.is(scratch3));
689
690 Label done;
691
692 // Untag the object into the destination register.
693 __ SmiUntag(dst, object);
694 // Just return if the object is a smi.
695 __ JumpIfSmi(object, &done);
696
697 if (FLAG_debug_code) {
698 __ AbortIfNotRootValue(heap_number_map,
699 Heap::kHeapNumberMapRootIndex,
700 "HeapNumberMap register clobbered.");
701 }
702 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
703
704 // Object is a heap number.
705 // Convert the floating point value to a 32-bit integer.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100706 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100707 CpuFeatures::Scope scope(VFP3);
708 SwVfpRegister single_scratch = double_scratch.low();
709 // Load the double value.
710 __ sub(scratch1, object, Operand(kHeapObjectTag));
711 __ vldr(double_scratch, scratch1, HeapNumber::kValueOffset);
712
713 __ EmitVFPTruncate(kRoundToZero,
714 single_scratch,
715 double_scratch,
716 scratch1,
717 scratch2,
718 kCheckForInexactConversion);
719
720 // Jump to not_int32 if the operation did not succeed.
721 __ b(ne, not_int32);
722 // Get the result in the destination register.
723 __ vmov(dst, single_scratch);
724
725 } else {
726 // Load the double value in the destination registers.
727 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
728 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
729
730 // Check for 0 and -0.
731 __ bic(dst, scratch1, Operand(HeapNumber::kSignMask));
732 __ orr(dst, scratch2, Operand(dst));
733 __ cmp(dst, Operand(0));
734 __ b(eq, &done);
735
736 DoubleIs32BitInteger(masm, scratch1, scratch2, dst, scratch3, not_int32);
737
738 // Registers state after DoubleIs32BitInteger.
739 // dst: mantissa[51:20].
740 // scratch2: 1
741
742 // Shift back the higher bits of the mantissa.
743 __ mov(dst, Operand(dst, LSR, scratch3));
744 // Set the implicit first bit.
745 __ rsb(scratch3, scratch3, Operand(32));
746 __ orr(dst, dst, Operand(scratch2, LSL, scratch3));
747 // Set the sign.
748 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
749 __ tst(scratch1, Operand(HeapNumber::kSignMask));
750 __ rsb(dst, dst, Operand(0), LeaveCC, mi);
751 }
752
753 __ bind(&done);
754}
755
756
757void FloatingPointHelper::DoubleIs32BitInteger(MacroAssembler* masm,
758 Register src1,
759 Register src2,
760 Register dst,
761 Register scratch,
762 Label* not_int32) {
763 // Get exponent alone in scratch.
764 __ Ubfx(scratch,
765 src1,
766 HeapNumber::kExponentShift,
767 HeapNumber::kExponentBits);
768
769 // Substract the bias from the exponent.
770 __ sub(scratch, scratch, Operand(HeapNumber::kExponentBias), SetCC);
771
772 // src1: higher (exponent) part of the double value.
773 // src2: lower (mantissa) part of the double value.
774 // scratch: unbiased exponent.
775
776 // Fast cases. Check for obvious non 32-bit integer values.
777 // Negative exponent cannot yield 32-bit integers.
778 __ b(mi, not_int32);
779 // Exponent greater than 31 cannot yield 32-bit integers.
780 // Also, a positive value with an exponent equal to 31 is outside of the
781 // signed 32-bit integer range.
782 // Another way to put it is that if (exponent - signbit) > 30 then the
783 // number cannot be represented as an int32.
784 Register tmp = dst;
785 __ sub(tmp, scratch, Operand(src1, LSR, 31));
786 __ cmp(tmp, Operand(30));
787 __ b(gt, not_int32);
788 // - Bits [21:0] in the mantissa are not null.
789 __ tst(src2, Operand(0x3fffff));
790 __ b(ne, not_int32);
791
792 // Otherwise the exponent needs to be big enough to shift left all the
793 // non zero bits left. So we need the (30 - exponent) last bits of the
794 // 31 higher bits of the mantissa to be null.
795 // Because bits [21:0] are null, we can check instead that the
796 // (32 - exponent) last bits of the 32 higher bits of the mantisssa are null.
797
798 // Get the 32 higher bits of the mantissa in dst.
799 __ Ubfx(dst,
800 src2,
801 HeapNumber::kMantissaBitsInTopWord,
802 32 - HeapNumber::kMantissaBitsInTopWord);
803 __ orr(dst,
804 dst,
805 Operand(src1, LSL, HeapNumber::kNonMantissaBitsInTopWord));
806
807 // Create the mask and test the lower bits (of the higher bits).
808 __ rsb(scratch, scratch, Operand(32));
809 __ mov(src2, Operand(1));
810 __ mov(src1, Operand(src2, LSL, scratch));
811 __ sub(src1, src1, Operand(1));
812 __ tst(dst, src1);
813 __ b(ne, not_int32);
814}
815
816
817void FloatingPointHelper::CallCCodeForDoubleOperation(
818 MacroAssembler* masm,
819 Token::Value op,
820 Register heap_number_result,
821 Register scratch) {
822 // Using core registers:
823 // r0: Left value (least significant part of mantissa).
824 // r1: Left value (sign, exponent, top of mantissa).
825 // r2: Right value (least significant part of mantissa).
826 // r3: Right value (sign, exponent, top of mantissa).
827
828 // Assert that heap_number_result is callee-saved.
829 // We currently always use r5 to pass it.
830 ASSERT(heap_number_result.is(r5));
831
832 // Push the current return address before the C call. Return will be
833 // through pop(pc) below.
834 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +0000835 __ PrepareCallCFunction(0, 2, scratch);
836 if (masm->use_eabi_hardfloat()) {
837 CpuFeatures::Scope scope(VFP3);
838 __ vmov(d0, r0, r1);
839 __ vmov(d1, r2, r3);
840 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100841 // Call C routine that may not cause GC or other trouble.
Steve Block44f0eee2011-05-26 01:26:41 +0100842 __ CallCFunction(ExternalReference::double_fp_operation(op, masm->isolate()),
Ben Murdoch257744e2011-11-30 15:57:28 +0000843 0, 2);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100844 // Store answer in the overwritable heap number. Double returned in
Ben Murdoch257744e2011-11-30 15:57:28 +0000845 // registers r0 and r1 or in d0.
846 if (masm->use_eabi_hardfloat()) {
847 CpuFeatures::Scope scope(VFP3);
848 __ vstr(d0,
849 FieldMemOperand(heap_number_result, HeapNumber::kValueOffset));
850 } else {
851 __ Strd(r0, r1, FieldMemOperand(heap_number_result,
852 HeapNumber::kValueOffset));
853 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100854 // Place heap_number_result in r0 and return to the pushed return address.
855 __ mov(r0, Operand(heap_number_result));
856 __ pop(pc);
857}
858
Steve Block1e0659c2011-05-24 12:43:12 +0100859
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100860// See comment for class.
861void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
862 Label max_negative_int;
863 // the_int_ has the answer which is a signed int32 but not a Smi.
864 // We test for the special value that has a different exponent. This test
865 // has the neat side effect of setting the flags according to the sign.
866 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
867 __ cmp(the_int_, Operand(0x80000000u));
868 __ b(eq, &max_negative_int);
869 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
870 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
871 uint32_t non_smi_exponent =
872 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
873 __ mov(scratch_, Operand(non_smi_exponent));
874 // Set the sign bit in scratch_ if the value was negative.
875 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
876 // Subtract from 0 if the value was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100877 __ rsb(the_int_, the_int_, Operand(0, RelocInfo::NONE), LeaveCC, cs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100878 // We should be masking the implict first digit of the mantissa away here,
879 // but it just ends up combining harmlessly with the last digit of the
880 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
881 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
882 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
883 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
884 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
885 __ str(scratch_, FieldMemOperand(the_heap_number_,
886 HeapNumber::kExponentOffset));
887 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
888 __ str(scratch_, FieldMemOperand(the_heap_number_,
889 HeapNumber::kMantissaOffset));
890 __ Ret();
891
892 __ bind(&max_negative_int);
893 // The max negative int32 is stored as a positive number in the mantissa of
894 // a double because it uses a sign bit instead of using two's complement.
895 // The actual mantissa bits stored are all 0 because the implicit most
896 // significant 1 bit is not stored.
897 non_smi_exponent += 1 << HeapNumber::kExponentShift;
898 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
899 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100900 __ mov(ip, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100901 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
902 __ Ret();
903}
904
905
906// Handle the case where the lhs and rhs are the same object.
907// Equality is almost reflexive (everything but NaN), so this is a test
908// for "identity and not NaN".
909static void EmitIdenticalObjectComparison(MacroAssembler* masm,
910 Label* slow,
Steve Block1e0659c2011-05-24 12:43:12 +0100911 Condition cond,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100912 bool never_nan_nan) {
913 Label not_identical;
914 Label heap_number, return_equal;
915 __ cmp(r0, r1);
916 __ b(ne, &not_identical);
917
918 // The two objects are identical. If we know that one of them isn't NaN then
919 // we now know they test equal.
Steve Block1e0659c2011-05-24 12:43:12 +0100920 if (cond != eq || !never_nan_nan) {
Steve Block44f0eee2011-05-26 01:26:41 +0100921 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100922 // so we do the second best thing - test it ourselves.
923 // They are both equal and they are not both Smis so both of them are not
924 // Smis. If it's not a heap number, then return equal.
Steve Block1e0659c2011-05-24 12:43:12 +0100925 if (cond == lt || cond == gt) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000926 __ CompareObjectType(r0, r4, r4, FIRST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100927 __ b(ge, slow);
928 } else {
929 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
930 __ b(eq, &heap_number);
931 // Comparing JS objects with <=, >= is complicated.
Steve Block1e0659c2011-05-24 12:43:12 +0100932 if (cond != eq) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000933 __ cmp(r4, Operand(FIRST_SPEC_OBJECT_TYPE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100934 __ b(ge, slow);
935 // Normally here we fall through to return_equal, but undefined is
936 // special: (undefined == undefined) == true, but
937 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
Steve Block1e0659c2011-05-24 12:43:12 +0100938 if (cond == le || cond == ge) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100939 __ cmp(r4, Operand(ODDBALL_TYPE));
940 __ b(ne, &return_equal);
941 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
942 __ cmp(r0, r2);
943 __ b(ne, &return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +0100944 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100945 // undefined <= undefined should fail.
946 __ mov(r0, Operand(GREATER));
947 } else {
948 // undefined >= undefined should fail.
949 __ mov(r0, Operand(LESS));
950 }
951 __ Ret();
952 }
953 }
954 }
955 }
956
957 __ bind(&return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +0100958 if (cond == lt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100959 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
Steve Block1e0659c2011-05-24 12:43:12 +0100960 } else if (cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100961 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
962 } else {
963 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
964 }
965 __ Ret();
966
Steve Block1e0659c2011-05-24 12:43:12 +0100967 if (cond != eq || !never_nan_nan) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100968 // For less and greater we don't have to check for NaN since the result of
969 // x < x is false regardless. For the others here is some code to check
970 // for NaN.
Steve Block1e0659c2011-05-24 12:43:12 +0100971 if (cond != lt && cond != gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100972 __ bind(&heap_number);
973 // It is a heap number, so return non-equal if it's NaN and equal if it's
974 // not NaN.
975
976 // The representation of NaN values has all exponent bits (52..62) set,
977 // and not all mantissa bits (0..51) clear.
978 // Read top bits of double representation (second word of value).
979 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
980 // Test that exponent bits are all set.
981 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
982 // NaNs have all-one exponents so they sign extend to -1.
983 __ cmp(r3, Operand(-1));
984 __ b(ne, &return_equal);
985
986 // Shift out flag and all exponent bits, retaining only mantissa.
987 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
988 // Or with all low-bits of mantissa.
989 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
990 __ orr(r0, r3, Operand(r2), SetCC);
991 // For equal we already have the right value in r0: Return zero (equal)
992 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
993 // not (it's a NaN). For <= and >= we need to load r0 with the failing
994 // value if it's a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +0100995 if (cond != eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100996 // All-zero means Infinity means equal.
997 __ Ret(eq);
Steve Block1e0659c2011-05-24 12:43:12 +0100998 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100999 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
1000 } else {
1001 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
1002 }
1003 }
1004 __ Ret();
1005 }
1006 // No fall through here.
1007 }
1008
1009 __ bind(&not_identical);
1010}
1011
1012
1013// See comment at call site.
1014static void EmitSmiNonsmiComparison(MacroAssembler* masm,
1015 Register lhs,
1016 Register rhs,
1017 Label* lhs_not_nan,
1018 Label* slow,
1019 bool strict) {
1020 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1021 (lhs.is(r1) && rhs.is(r0)));
1022
1023 Label rhs_is_smi;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001024 __ JumpIfSmi(rhs, &rhs_is_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001025
1026 // Lhs is a Smi. Check whether the rhs is a heap number.
1027 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
1028 if (strict) {
1029 // If rhs is not a number and lhs is a Smi then strict equality cannot
1030 // succeed. Return non-equal
1031 // If rhs is r0 then there is already a non zero value in it.
1032 if (!rhs.is(r0)) {
1033 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1034 }
1035 __ Ret(ne);
1036 } else {
1037 // Smi compared non-strictly with a non-Smi non-heap-number. Call
1038 // the runtime.
1039 __ b(ne, slow);
1040 }
1041
1042 // Lhs is a smi, rhs is a number.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001043 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001044 // Convert lhs to a double in d7.
1045 CpuFeatures::Scope scope(VFP3);
1046 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
1047 // Load the double from rhs, tagged HeapNumber r0, to d6.
1048 __ sub(r7, rhs, Operand(kHeapObjectTag));
1049 __ vldr(d6, r7, HeapNumber::kValueOffset);
1050 } else {
1051 __ push(lr);
1052 // Convert lhs to a double in r2, r3.
1053 __ mov(r7, Operand(lhs));
1054 ConvertToDoubleStub stub1(r3, r2, r7, r6);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001055 __ Call(stub1.GetCode());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001056 // Load rhs to a double in r0, r1.
1057 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1058 __ pop(lr);
1059 }
1060
1061 // We now have both loaded as doubles but we can skip the lhs nan check
1062 // since it's a smi.
1063 __ jmp(lhs_not_nan);
1064
1065 __ bind(&rhs_is_smi);
1066 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
1067 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
1068 if (strict) {
1069 // If lhs is not a number and rhs is a smi then strict equality cannot
1070 // succeed. Return non-equal.
1071 // If lhs is r0 then there is already a non zero value in it.
1072 if (!lhs.is(r0)) {
1073 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1074 }
1075 __ Ret(ne);
1076 } else {
1077 // Smi compared non-strictly with a non-smi non-heap-number. Call
1078 // the runtime.
1079 __ b(ne, slow);
1080 }
1081
1082 // Rhs is a smi, lhs is a heap number.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001083 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001084 CpuFeatures::Scope scope(VFP3);
1085 // Load the double from lhs, tagged HeapNumber r1, to d7.
1086 __ sub(r7, lhs, Operand(kHeapObjectTag));
1087 __ vldr(d7, r7, HeapNumber::kValueOffset);
1088 // Convert rhs to a double in d6 .
1089 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
1090 } else {
1091 __ push(lr);
1092 // Load lhs to a double in r2, r3.
1093 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1094 // Convert rhs to a double in r0, r1.
1095 __ mov(r7, Operand(rhs));
1096 ConvertToDoubleStub stub2(r1, r0, r7, r6);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001097 __ Call(stub2.GetCode());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001098 __ pop(lr);
1099 }
1100 // Fall through to both_loaded_as_doubles.
1101}
1102
1103
Steve Block1e0659c2011-05-24 12:43:12 +01001104void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001105 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1106 Register rhs_exponent = exp_first ? r0 : r1;
1107 Register lhs_exponent = exp_first ? r2 : r3;
1108 Register rhs_mantissa = exp_first ? r1 : r0;
1109 Register lhs_mantissa = exp_first ? r3 : r2;
1110 Label one_is_nan, neither_is_nan;
1111
1112 __ Sbfx(r4,
1113 lhs_exponent,
1114 HeapNumber::kExponentShift,
1115 HeapNumber::kExponentBits);
1116 // NaNs have all-one exponents so they sign extend to -1.
1117 __ cmp(r4, Operand(-1));
1118 __ b(ne, lhs_not_nan);
1119 __ mov(r4,
1120 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1121 SetCC);
1122 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001123 __ cmp(lhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001124 __ b(ne, &one_is_nan);
1125
1126 __ bind(lhs_not_nan);
1127 __ Sbfx(r4,
1128 rhs_exponent,
1129 HeapNumber::kExponentShift,
1130 HeapNumber::kExponentBits);
1131 // NaNs have all-one exponents so they sign extend to -1.
1132 __ cmp(r4, Operand(-1));
1133 __ b(ne, &neither_is_nan);
1134 __ mov(r4,
1135 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1136 SetCC);
1137 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001138 __ cmp(rhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001139 __ b(eq, &neither_is_nan);
1140
1141 __ bind(&one_is_nan);
1142 // NaN comparisons always fail.
1143 // Load whatever we need in r0 to make the comparison fail.
Steve Block1e0659c2011-05-24 12:43:12 +01001144 if (cond == lt || cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001145 __ mov(r0, Operand(GREATER));
1146 } else {
1147 __ mov(r0, Operand(LESS));
1148 }
1149 __ Ret();
1150
1151 __ bind(&neither_is_nan);
1152}
1153
1154
1155// See comment at call site.
Steve Block1e0659c2011-05-24 12:43:12 +01001156static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm,
1157 Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001158 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1159 Register rhs_exponent = exp_first ? r0 : r1;
1160 Register lhs_exponent = exp_first ? r2 : r3;
1161 Register rhs_mantissa = exp_first ? r1 : r0;
1162 Register lhs_mantissa = exp_first ? r3 : r2;
1163
1164 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001165 if (cond == eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001166 // Doubles are not equal unless they have the same bit pattern.
1167 // Exception: 0 and -0.
1168 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
1169 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
1170 // Return non-zero if the numbers are unequal.
1171 __ Ret(ne);
1172
1173 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
1174 // If exponents are equal then return 0.
1175 __ Ret(eq);
1176
1177 // Exponents are unequal. The only way we can return that the numbers
1178 // are equal is if one is -0 and the other is 0. We already dealt
1179 // with the case where both are -0 or both are 0.
1180 // We start by seeing if the mantissas (that are equal) or the bottom
1181 // 31 bits of the rhs exponent are non-zero. If so we return not
1182 // equal.
1183 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
1184 __ mov(r0, Operand(r4), LeaveCC, ne);
1185 __ Ret(ne);
1186 // Now they are equal if and only if the lhs exponent is zero in its
1187 // low 31 bits.
1188 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
1189 __ Ret();
1190 } else {
1191 // Call a native function to do a comparison between two non-NaNs.
1192 // Call C routine that may not cause GC or other trouble.
1193 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00001194 __ PrepareCallCFunction(0, 2, r5);
1195 if (masm->use_eabi_hardfloat()) {
1196 CpuFeatures::Scope scope(VFP3);
1197 __ vmov(d0, r0, r1);
1198 __ vmov(d1, r2, r3);
1199 }
1200 __ CallCFunction(ExternalReference::compare_doubles(masm->isolate()),
1201 0, 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001202 __ pop(pc); // Return.
1203 }
1204}
1205
1206
1207// See comment at call site.
1208static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
1209 Register lhs,
1210 Register rhs) {
1211 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1212 (lhs.is(r1) && rhs.is(r0)));
1213
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001214 // If either operand is a JS object or an oddball value, then they are
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001215 // not equal since their pointers are different.
1216 // There is no test for undetectability in strict equality.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001217 STATIC_ASSERT(LAST_TYPE == LAST_CALLABLE_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001218 Label first_non_object;
1219 // Get the type of the first operand into r2 and compare it with
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001220 // FIRST_SPEC_OBJECT_TYPE.
1221 __ CompareObjectType(rhs, r2, r2, FIRST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001222 __ b(lt, &first_non_object);
1223
1224 // Return non-zero (r0 is not zero)
1225 Label return_not_equal;
1226 __ bind(&return_not_equal);
1227 __ Ret();
1228
1229 __ bind(&first_non_object);
1230 // Check for oddballs: true, false, null, undefined.
1231 __ cmp(r2, Operand(ODDBALL_TYPE));
1232 __ b(eq, &return_not_equal);
1233
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001234 __ CompareObjectType(lhs, r3, r3, FIRST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001235 __ b(ge, &return_not_equal);
1236
1237 // Check for oddballs: true, false, null, undefined.
1238 __ cmp(r3, Operand(ODDBALL_TYPE));
1239 __ b(eq, &return_not_equal);
1240
1241 // Now that we have the types we might as well check for symbol-symbol.
1242 // Ensure that no non-strings have the symbol bit set.
1243 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
1244 STATIC_ASSERT(kSymbolTag != 0);
1245 __ and_(r2, r2, Operand(r3));
1246 __ tst(r2, Operand(kIsSymbolMask));
1247 __ b(ne, &return_not_equal);
1248}
1249
1250
1251// See comment at call site.
1252static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
1253 Register lhs,
1254 Register rhs,
1255 Label* both_loaded_as_doubles,
1256 Label* not_heap_numbers,
1257 Label* slow) {
1258 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1259 (lhs.is(r1) && rhs.is(r0)));
1260
1261 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
1262 __ b(ne, not_heap_numbers);
1263 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
1264 __ cmp(r2, r3);
1265 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
1266
1267 // Both are heap numbers. Load them up then jump to the code we have
1268 // for that.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001269 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001270 CpuFeatures::Scope scope(VFP3);
1271 __ sub(r7, rhs, Operand(kHeapObjectTag));
1272 __ vldr(d6, r7, HeapNumber::kValueOffset);
1273 __ sub(r7, lhs, Operand(kHeapObjectTag));
1274 __ vldr(d7, r7, HeapNumber::kValueOffset);
1275 } else {
1276 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1277 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1278 }
1279 __ jmp(both_loaded_as_doubles);
1280}
1281
1282
1283// Fast negative check for symbol-to-symbol equality.
1284static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
1285 Register lhs,
1286 Register rhs,
1287 Label* possible_strings,
1288 Label* not_both_strings) {
1289 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1290 (lhs.is(r1) && rhs.is(r0)));
1291
1292 // r2 is object type of rhs.
1293 // Ensure that no non-strings have the symbol bit set.
1294 Label object_test;
1295 STATIC_ASSERT(kSymbolTag != 0);
1296 __ tst(r2, Operand(kIsNotStringMask));
1297 __ b(ne, &object_test);
1298 __ tst(r2, Operand(kIsSymbolMask));
1299 __ b(eq, possible_strings);
1300 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
1301 __ b(ge, not_both_strings);
1302 __ tst(r3, Operand(kIsSymbolMask));
1303 __ b(eq, possible_strings);
1304
1305 // Both are symbols. We already checked they weren't the same pointer
1306 // so they are not equal.
1307 __ mov(r0, Operand(NOT_EQUAL));
1308 __ Ret();
1309
1310 __ bind(&object_test);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001311 __ cmp(r2, Operand(FIRST_SPEC_OBJECT_TYPE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001312 __ b(lt, not_both_strings);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001313 __ CompareObjectType(lhs, r2, r3, FIRST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001314 __ b(lt, not_both_strings);
1315 // If both objects are undetectable, they are equal. Otherwise, they
1316 // are not equal, since they are different objects and an object is not
1317 // equal to undefined.
1318 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
1319 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
1320 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
1321 __ and_(r0, r2, Operand(r3));
1322 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
1323 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
1324 __ Ret();
1325}
1326
1327
1328void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
1329 Register object,
1330 Register result,
1331 Register scratch1,
1332 Register scratch2,
1333 Register scratch3,
1334 bool object_is_smi,
1335 Label* not_found) {
1336 // Use of registers. Register result is used as a temporary.
1337 Register number_string_cache = result;
1338 Register mask = scratch3;
1339
1340 // Load the number string cache.
1341 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
1342
1343 // Make the hash mask from the length of the number string cache. It
1344 // contains two elements (number and string) for each cache entry.
1345 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
1346 // Divide length by two (length is a smi).
1347 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
1348 __ sub(mask, mask, Operand(1)); // Make mask.
1349
1350 // Calculate the entry in the number string cache. The hash value in the
1351 // number string cache for smis is just the smi value, and the hash for
1352 // doubles is the xor of the upper and lower words. See
1353 // Heap::GetNumberStringCache.
Steve Block44f0eee2011-05-26 01:26:41 +01001354 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001355 Label is_smi;
1356 Label load_result_from_cache;
1357 if (!object_is_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01001358 __ JumpIfSmi(object, &is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001359 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001360 CpuFeatures::Scope scope(VFP3);
1361 __ CheckMap(object,
1362 scratch1,
1363 Heap::kHeapNumberMapRootIndex,
1364 not_found,
Ben Murdoch257744e2011-11-30 15:57:28 +00001365 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001366
1367 STATIC_ASSERT(8 == kDoubleSize);
1368 __ add(scratch1,
1369 object,
1370 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
1371 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
1372 __ eor(scratch1, scratch1, Operand(scratch2));
1373 __ and_(scratch1, scratch1, Operand(mask));
1374
1375 // Calculate address of entry in string cache: each entry consists
1376 // of two pointer sized fields.
1377 __ add(scratch1,
1378 number_string_cache,
1379 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
1380
1381 Register probe = mask;
1382 __ ldr(probe,
1383 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
Steve Block1e0659c2011-05-24 12:43:12 +01001384 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001385 __ sub(scratch2, object, Operand(kHeapObjectTag));
1386 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
1387 __ sub(probe, probe, Operand(kHeapObjectTag));
1388 __ vldr(d1, probe, HeapNumber::kValueOffset);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001389 __ VFPCompareAndSetFlags(d0, d1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001390 __ b(ne, not_found); // The cache did not contain this value.
1391 __ b(&load_result_from_cache);
1392 } else {
1393 __ b(not_found);
1394 }
1395 }
1396
1397 __ bind(&is_smi);
1398 Register scratch = scratch1;
1399 __ and_(scratch, mask, Operand(object, ASR, 1));
1400 // Calculate address of entry in string cache: each entry consists
1401 // of two pointer sized fields.
1402 __ add(scratch,
1403 number_string_cache,
1404 Operand(scratch, LSL, kPointerSizeLog2 + 1));
1405
1406 // Check if the entry is the smi we are looking for.
1407 Register probe = mask;
1408 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
1409 __ cmp(object, probe);
1410 __ b(ne, not_found);
1411
1412 // Get the result from the cache.
1413 __ bind(&load_result_from_cache);
1414 __ ldr(result,
1415 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01001416 __ IncrementCounter(isolate->counters()->number_to_string_native(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001417 1,
1418 scratch1,
1419 scratch2);
1420}
1421
1422
1423void NumberToStringStub::Generate(MacroAssembler* masm) {
1424 Label runtime;
1425
1426 __ ldr(r1, MemOperand(sp, 0));
1427
1428 // Generate code to lookup number in the number string cache.
1429 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
1430 __ add(sp, sp, Operand(1 * kPointerSize));
1431 __ Ret();
1432
1433 __ bind(&runtime);
1434 // Handle number to string in the runtime system if not found in the cache.
1435 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
1436}
1437
1438
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001439// On entry lhs_ and rhs_ are the values to be compared.
1440// On exit r0 is 0, positive or negative to indicate the result of
1441// the comparison.
1442void CompareStub::Generate(MacroAssembler* masm) {
1443 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
1444 (lhs_.is(r1) && rhs_.is(r0)));
1445
1446 Label slow; // Call builtin.
1447 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
1448
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001449 if (include_smi_compare_) {
1450 Label not_two_smis, smi_done;
1451 __ orr(r2, r1, r0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001452 __ JumpIfNotSmi(r2, &not_two_smis);
Ben Murdochf87a2032010-10-22 12:50:53 +01001453 __ mov(r1, Operand(r1, ASR, 1));
1454 __ sub(r0, r1, Operand(r0, ASR, 1));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001455 __ Ret();
1456 __ bind(&not_two_smis);
1457 } else if (FLAG_debug_code) {
1458 __ orr(r2, r1, r0);
1459 __ tst(r2, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001460 __ Assert(ne, "CompareStub: unexpected smi operands.");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001461 }
1462
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001463 // NOTICE! This code is only reached after a smi-fast-case check, so
1464 // it is certain that at least one operand isn't a smi.
1465
1466 // Handle the case where the objects are identical. Either returns the answer
1467 // or goes to slow. Only falls through if the objects were not identical.
1468 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
1469
1470 // If either is a Smi (we know that not both are), then they can only
1471 // be strictly equal if the other is a HeapNumber.
1472 STATIC_ASSERT(kSmiTag == 0);
1473 ASSERT_EQ(0, Smi::FromInt(0));
1474 __ and_(r2, lhs_, Operand(rhs_));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001475 __ JumpIfNotSmi(r2, &not_smis);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001476 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
1477 // 1) Return the answer.
1478 // 2) Go to slow.
1479 // 3) Fall through to both_loaded_as_doubles.
1480 // 4) Jump to lhs_not_nan.
1481 // In cases 3 and 4 we have found out we were dealing with a number-number
1482 // comparison. If VFP3 is supported the double values of the numbers have
1483 // been loaded into d7 and d6. Otherwise, the double values have been loaded
1484 // into r0, r1, r2, and r3.
1485 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
1486
1487 __ bind(&both_loaded_as_doubles);
1488 // The arguments have been converted to doubles and stored in d6 and d7, if
1489 // VFP3 is supported, or in r0, r1, r2, and r3.
Steve Block44f0eee2011-05-26 01:26:41 +01001490 Isolate* isolate = masm->isolate();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001491 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001492 __ bind(&lhs_not_nan);
1493 CpuFeatures::Scope scope(VFP3);
1494 Label no_nan;
1495 // ARMv7 VFP3 instructions to implement double precision comparison.
Ben Murdochb8e0da22011-05-16 14:20:40 +01001496 __ VFPCompareAndSetFlags(d7, d6);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001497 Label nan;
1498 __ b(vs, &nan);
1499 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
1500 __ mov(r0, Operand(LESS), LeaveCC, lt);
1501 __ mov(r0, Operand(GREATER), LeaveCC, gt);
1502 __ Ret();
1503
1504 __ bind(&nan);
1505 // If one of the sides was a NaN then the v flag is set. Load r0 with
1506 // whatever it takes to make the comparison fail, since comparisons with NaN
1507 // always fail.
1508 if (cc_ == lt || cc_ == le) {
1509 __ mov(r0, Operand(GREATER));
1510 } else {
1511 __ mov(r0, Operand(LESS));
1512 }
1513 __ Ret();
1514 } else {
1515 // Checks for NaN in the doubles we have loaded. Can return the answer or
1516 // fall through if neither is a NaN. Also binds lhs_not_nan.
1517 EmitNanCheck(masm, &lhs_not_nan, cc_);
1518 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
1519 // answer. Never falls through.
1520 EmitTwoNonNanDoubleComparison(masm, cc_);
1521 }
1522
1523 __ bind(&not_smis);
1524 // At this point we know we are dealing with two different objects,
1525 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
1526 if (strict_) {
1527 // This returns non-equal for some object types, or falls through if it
1528 // was not lucky.
1529 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1530 }
1531
1532 Label check_for_symbols;
1533 Label flat_string_check;
1534 // Check for heap-number-heap-number comparison. Can jump to slow case,
1535 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
1536 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1537 // In this case r2 will contain the type of rhs_. Never falls through.
1538 EmitCheckForTwoHeapNumbers(masm,
1539 lhs_,
1540 rhs_,
1541 &both_loaded_as_doubles,
1542 &check_for_symbols,
1543 &flat_string_check);
1544
1545 __ bind(&check_for_symbols);
1546 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
1547 // symbols.
1548 if (cc_ == eq && !strict_) {
1549 // Returns an answer for two symbols or two detectable objects.
1550 // Otherwise jumps to string case or not both strings case.
1551 // Assumes that r2 is the type of rhs_ on entry.
1552 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1553 }
1554
1555 // Check for both being sequential ASCII strings, and inline if that is the
1556 // case.
1557 __ bind(&flat_string_check);
1558
1559 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
1560
Steve Block44f0eee2011-05-26 01:26:41 +01001561 __ IncrementCounter(isolate->counters()->string_compare_native(), 1, r2, r3);
Ben Murdoch257744e2011-11-30 15:57:28 +00001562 if (cc_ == eq) {
1563 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001564 lhs_,
1565 rhs_,
1566 r2,
1567 r3,
Ben Murdoch257744e2011-11-30 15:57:28 +00001568 r4);
1569 } else {
1570 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1571 lhs_,
1572 rhs_,
1573 r2,
1574 r3,
1575 r4,
1576 r5);
1577 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001578 // Never falls through to here.
1579
1580 __ bind(&slow);
1581
1582 __ Push(lhs_, rhs_);
1583 // Figure out which native to call and setup the arguments.
1584 Builtins::JavaScript native;
1585 if (cc_ == eq) {
1586 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1587 } else {
1588 native = Builtins::COMPARE;
1589 int ncr; // NaN compare result
1590 if (cc_ == lt || cc_ == le) {
1591 ncr = GREATER;
1592 } else {
1593 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
1594 ncr = LESS;
1595 }
1596 __ mov(r0, Operand(Smi::FromInt(ncr)));
1597 __ push(r0);
1598 }
1599
1600 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1601 // tagged as a small integer.
Ben Murdoch257744e2011-11-30 15:57:28 +00001602 __ InvokeBuiltin(native, JUMP_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001603}
1604
1605
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001606// The stub returns zero for false, and a non-zero value for true.
1607void ToBooleanStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001608 // This stub uses VFP3 instructions.
Ben Murdoch257744e2011-11-30 15:57:28 +00001609 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001610
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001611 Label false_result, true_result, not_string;
1612 const Register map = r9.is(tos_) ? r7 : r9;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001613
Ben Murdoch257744e2011-11-30 15:57:28 +00001614 // undefined -> false
1615 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1616 __ cmp(tos_, ip);
1617 __ b(eq, &false_result);
1618
1619 // Boolean -> its value
1620 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
1621 __ cmp(tos_, ip);
1622 __ b(eq, &false_result);
1623 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
1624 __ cmp(tos_, ip);
1625 // "tos_" is a register and contains a non-zero value. Hence we implicitly
1626 // return true if the equal condition is satisfied.
1627 __ Ret(eq);
1628
1629 // Smis: 0 -> false, all other -> true
1630 __ tst(tos_, tos_);
1631 __ b(eq, &false_result);
1632 __ tst(tos_, Operand(kSmiTagMask));
1633 // "tos_" is a register and contains a non-zero value. Hence we implicitly
1634 // return true if the not equal condition is satisfied.
1635 __ Ret(eq);
1636
1637 // 'null' -> false
Ben Murdochb0fe1622011-05-05 13:52:32 +01001638 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1639 __ cmp(tos_, ip);
1640 __ b(eq, &false_result);
1641
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001642 // Get the map of the heap object.
1643 __ ldr(map, FieldMemOperand(tos_, HeapObject::kMapOffset));
1644
1645 // Undetectable -> false.
1646 __ ldrb(ip, FieldMemOperand(map, Map::kBitFieldOffset));
1647 __ tst(ip, Operand(1 << Map::kIsUndetectable));
1648 __ b(&false_result, ne);
1649
1650 // JavaScript object -> true.
1651 __ CompareInstanceType(map, ip, FIRST_SPEC_OBJECT_TYPE);
1652 // "tos_" is a register and contains a non-zero value. Hence we implicitly
1653 // return true if the greater than condition is satisfied.
1654 __ Ret(ge);
1655
1656 // String value -> false iff empty.
1657 __ CompareInstanceType(map, ip, FIRST_NONSTRING_TYPE);
1658 __ b(&not_string, ge);
1659 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
1660 // Return string length as boolean value, i.e. return false iff length is 0.
1661 __ Ret();
1662
1663 __ bind(&not_string);
1664 // HeapNumber -> false iff +0, -0, or NaN.
1665 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
1666 __ b(&true_result, ne);
Ben Murdoch257744e2011-11-30 15:57:28 +00001667 __ vldr(d1, FieldMemOperand(tos_, HeapNumber::kValueOffset));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001668 __ VFPCompareAndSetFlags(d1, 0.0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001669 // "tos_" is a register, and contains a non zero value by default.
1670 // Hence we only need to overwrite "tos_" with zero to return false for
1671 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001672 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, eq); // for FP_ZERO
1673 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, vs); // for FP_NAN
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001674 __ Ret();
1675
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001676 // Return 1/0 for true/false in tos_.
1677 __ bind(&true_result);
1678 __ mov(tos_, Operand(1, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001679 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001680 __ bind(&false_result);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001681 __ mov(tos_, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001682 __ Ret();
1683}
1684
1685
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001686void UnaryOpStub::PrintName(StringStream* stream) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001687 const char* op_name = Token::Name(op_);
1688 const char* overwrite_name = NULL; // Make g++ happy.
1689 switch (mode_) {
1690 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
1691 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
1692 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001693 stream->Add("UnaryOpStub_%s_%s_%s",
1694 op_name,
1695 overwrite_name,
1696 UnaryOpIC::GetName(operand_type_));
Ben Murdoch257744e2011-11-30 15:57:28 +00001697}
1698
1699
1700// TODO(svenpanne): Use virtual functions instead of switch.
1701void UnaryOpStub::Generate(MacroAssembler* masm) {
1702 switch (operand_type_) {
1703 case UnaryOpIC::UNINITIALIZED:
1704 GenerateTypeTransition(masm);
1705 break;
1706 case UnaryOpIC::SMI:
1707 GenerateSmiStub(masm);
1708 break;
1709 case UnaryOpIC::HEAP_NUMBER:
1710 GenerateHeapNumberStub(masm);
1711 break;
1712 case UnaryOpIC::GENERIC:
1713 GenerateGenericStub(masm);
1714 break;
1715 }
1716}
1717
1718
1719void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001720 __ mov(r3, Operand(r0)); // the operand
1721 __ mov(r2, Operand(Smi::FromInt(op_)));
1722 __ mov(r1, Operand(Smi::FromInt(mode_)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001723 __ mov(r0, Operand(Smi::FromInt(operand_type_)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001724 __ Push(r3, r2, r1, r0);
1725
1726 __ TailCallExternalReference(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001727 ExternalReference(IC_Utility(IC::kUnaryOp_Patch), masm->isolate()), 4, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001728}
1729
1730
1731// TODO(svenpanne): Use virtual functions instead of switch.
1732void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1733 switch (op_) {
1734 case Token::SUB:
1735 GenerateSmiStubSub(masm);
1736 break;
1737 case Token::BIT_NOT:
1738 GenerateSmiStubBitNot(masm);
1739 break;
1740 default:
1741 UNREACHABLE();
1742 }
1743}
1744
1745
1746void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
1747 Label non_smi, slow;
1748 GenerateSmiCodeSub(masm, &non_smi, &slow);
1749 __ bind(&non_smi);
1750 __ bind(&slow);
1751 GenerateTypeTransition(masm);
1752}
1753
1754
1755void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
1756 Label non_smi;
1757 GenerateSmiCodeBitNot(masm, &non_smi);
1758 __ bind(&non_smi);
1759 GenerateTypeTransition(masm);
1760}
1761
1762
1763void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
1764 Label* non_smi,
1765 Label* slow) {
1766 __ JumpIfNotSmi(r0, non_smi);
1767
1768 // The result of negating zero or the smallest negative smi is not a smi.
1769 __ bic(ip, r0, Operand(0x80000000), SetCC);
1770 __ b(eq, slow);
1771
1772 // Return '0 - value'.
1773 __ rsb(r0, r0, Operand(0, RelocInfo::NONE));
1774 __ Ret();
1775}
1776
1777
1778void UnaryOpStub::GenerateSmiCodeBitNot(MacroAssembler* masm,
1779 Label* non_smi) {
1780 __ JumpIfNotSmi(r0, non_smi);
1781
1782 // Flip bits and revert inverted smi-tag.
1783 __ mvn(r0, Operand(r0));
1784 __ bic(r0, r0, Operand(kSmiTagMask));
1785 __ Ret();
1786}
1787
1788
1789// TODO(svenpanne): Use virtual functions instead of switch.
1790void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
1791 switch (op_) {
1792 case Token::SUB:
1793 GenerateHeapNumberStubSub(masm);
1794 break;
1795 case Token::BIT_NOT:
1796 GenerateHeapNumberStubBitNot(masm);
1797 break;
1798 default:
1799 UNREACHABLE();
1800 }
1801}
1802
1803
1804void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
1805 Label non_smi, slow, call_builtin;
1806 GenerateSmiCodeSub(masm, &non_smi, &call_builtin);
1807 __ bind(&non_smi);
1808 GenerateHeapNumberCodeSub(masm, &slow);
1809 __ bind(&slow);
1810 GenerateTypeTransition(masm);
1811 __ bind(&call_builtin);
1812 GenerateGenericCodeFallback(masm);
1813}
1814
1815
1816void UnaryOpStub::GenerateHeapNumberStubBitNot(MacroAssembler* masm) {
1817 Label non_smi, slow;
1818 GenerateSmiCodeBitNot(masm, &non_smi);
1819 __ bind(&non_smi);
1820 GenerateHeapNumberCodeBitNot(masm, &slow);
1821 __ bind(&slow);
1822 GenerateTypeTransition(masm);
1823}
1824
1825void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
1826 Label* slow) {
1827 EmitCheckForHeapNumber(masm, r0, r1, r6, slow);
1828 // r0 is a heap number. Get a new heap number in r1.
1829 if (mode_ == UNARY_OVERWRITE) {
1830 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1831 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
1832 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1833 } else {
1834 Label slow_allocate_heapnumber, heapnumber_allocated;
1835 __ AllocateHeapNumber(r1, r2, r3, r6, &slow_allocate_heapnumber);
1836 __ jmp(&heapnumber_allocated);
1837
1838 __ bind(&slow_allocate_heapnumber);
1839 __ EnterInternalFrame();
1840 __ push(r0);
1841 __ CallRuntime(Runtime::kNumberAlloc, 0);
1842 __ mov(r1, Operand(r0));
1843 __ pop(r0);
1844 __ LeaveInternalFrame();
1845
1846 __ bind(&heapnumber_allocated);
1847 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
1848 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1849 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
1850 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
1851 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
1852 __ mov(r0, Operand(r1));
1853 }
1854 __ Ret();
1855}
1856
1857
1858void UnaryOpStub::GenerateHeapNumberCodeBitNot(
1859 MacroAssembler* masm, Label* slow) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001860 Label impossible;
1861
Ben Murdoch257744e2011-11-30 15:57:28 +00001862 EmitCheckForHeapNumber(masm, r0, r1, r6, slow);
1863 // Convert the heap number is r0 to an untagged integer in r1.
1864 __ ConvertToInt32(r0, r1, r2, r3, d0, slow);
1865
1866 // Do the bitwise operation and check if the result fits in a smi.
1867 Label try_float;
1868 __ mvn(r1, Operand(r1));
1869 __ add(r2, r1, Operand(0x40000000), SetCC);
1870 __ b(mi, &try_float);
1871
1872 // Tag the result as a smi and we're done.
1873 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
1874 __ Ret();
1875
1876 // Try to store the result in a heap number.
1877 __ bind(&try_float);
1878 if (mode_ == UNARY_NO_OVERWRITE) {
1879 Label slow_allocate_heapnumber, heapnumber_allocated;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001880 // Allocate a new heap number without zapping r0, which we need if it fails.
1881 __ AllocateHeapNumber(r2, r3, r4, r6, &slow_allocate_heapnumber);
Ben Murdoch257744e2011-11-30 15:57:28 +00001882 __ jmp(&heapnumber_allocated);
1883
1884 __ bind(&slow_allocate_heapnumber);
1885 __ EnterInternalFrame();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001886 __ push(r0); // Push the heap number, not the untagged int32.
1887 __ CallRuntime(Runtime::kNumberAlloc, 0);
1888 __ mov(r2, r0); // Move the new heap number into r2.
1889 // Get the heap number into r0, now that the new heap number is in r2.
1890 __ pop(r0);
Ben Murdoch257744e2011-11-30 15:57:28 +00001891 __ LeaveInternalFrame();
1892
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001893 // Convert the heap number in r0 to an untagged integer in r1.
1894 // This can't go slow-case because it's the same number we already
1895 // converted once again.
1896 __ ConvertToInt32(r0, r1, r3, r4, d0, &impossible);
1897 __ mvn(r1, Operand(r1));
1898
Ben Murdoch257744e2011-11-30 15:57:28 +00001899 __ bind(&heapnumber_allocated);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001900 __ mov(r0, r2); // Move newly allocated heap number to r0.
Ben Murdoch257744e2011-11-30 15:57:28 +00001901 }
1902
1903 if (CpuFeatures::IsSupported(VFP3)) {
1904 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
1905 CpuFeatures::Scope scope(VFP3);
1906 __ vmov(s0, r1);
1907 __ vcvt_f64_s32(d0, s0);
1908 __ sub(r2, r0, Operand(kHeapObjectTag));
1909 __ vstr(d0, r2, HeapNumber::kValueOffset);
1910 __ Ret();
1911 } else {
1912 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
1913 // have to set up a frame.
1914 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
1915 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1916 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001917
1918 __ bind(&impossible);
1919 if (FLAG_debug_code) {
1920 __ stop("Incorrect assumption in bit-not stub");
1921 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001922}
1923
1924
1925// TODO(svenpanne): Use virtual functions instead of switch.
1926void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
1927 switch (op_) {
1928 case Token::SUB:
1929 GenerateGenericStubSub(masm);
1930 break;
1931 case Token::BIT_NOT:
1932 GenerateGenericStubBitNot(masm);
1933 break;
1934 default:
1935 UNREACHABLE();
1936 }
1937}
1938
1939
1940void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
1941 Label non_smi, slow;
1942 GenerateSmiCodeSub(masm, &non_smi, &slow);
1943 __ bind(&non_smi);
1944 GenerateHeapNumberCodeSub(masm, &slow);
1945 __ bind(&slow);
1946 GenerateGenericCodeFallback(masm);
1947}
1948
1949
1950void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
1951 Label non_smi, slow;
1952 GenerateSmiCodeBitNot(masm, &non_smi);
1953 __ bind(&non_smi);
1954 GenerateHeapNumberCodeBitNot(masm, &slow);
1955 __ bind(&slow);
1956 GenerateGenericCodeFallback(masm);
1957}
1958
1959
1960void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
1961 // Handle the slow case by jumping to the JavaScript builtin.
1962 __ push(r0);
1963 switch (op_) {
1964 case Token::SUB:
1965 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
1966 break;
1967 case Token::BIT_NOT:
1968 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
1969 break;
1970 default:
1971 UNREACHABLE();
1972 }
1973}
1974
1975
Ben Murdoch257744e2011-11-30 15:57:28 +00001976void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001977 Label get_result;
1978
1979 __ Push(r1, r0);
1980
1981 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
1982 __ mov(r1, Operand(Smi::FromInt(op_)));
1983 __ mov(r0, Operand(Smi::FromInt(operands_type_)));
1984 __ Push(r2, r1, r0);
1985
1986 __ TailCallExternalReference(
Ben Murdoch257744e2011-11-30 15:57:28 +00001987 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
Steve Block44f0eee2011-05-26 01:26:41 +01001988 masm->isolate()),
Steve Block1e0659c2011-05-24 12:43:12 +01001989 5,
1990 1);
1991}
1992
1993
Ben Murdoch257744e2011-11-30 15:57:28 +00001994void BinaryOpStub::GenerateTypeTransitionWithSavedArgs(
Steve Block1e0659c2011-05-24 12:43:12 +01001995 MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001996 UNIMPLEMENTED();
Steve Block1e0659c2011-05-24 12:43:12 +01001997}
1998
1999
Ben Murdoch257744e2011-11-30 15:57:28 +00002000void BinaryOpStub::Generate(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002001 switch (operands_type_) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002002 case BinaryOpIC::UNINITIALIZED:
Steve Block1e0659c2011-05-24 12:43:12 +01002003 GenerateTypeTransition(masm);
2004 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002005 case BinaryOpIC::SMI:
Steve Block1e0659c2011-05-24 12:43:12 +01002006 GenerateSmiStub(masm);
2007 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002008 case BinaryOpIC::INT32:
Steve Block1e0659c2011-05-24 12:43:12 +01002009 GenerateInt32Stub(masm);
2010 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002011 case BinaryOpIC::HEAP_NUMBER:
Steve Block1e0659c2011-05-24 12:43:12 +01002012 GenerateHeapNumberStub(masm);
2013 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002014 case BinaryOpIC::ODDBALL:
Steve Block44f0eee2011-05-26 01:26:41 +01002015 GenerateOddballStub(masm);
2016 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002017 case BinaryOpIC::BOTH_STRING:
2018 GenerateBothStringStub(masm);
2019 break;
2020 case BinaryOpIC::STRING:
Steve Block1e0659c2011-05-24 12:43:12 +01002021 GenerateStringStub(masm);
2022 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002023 case BinaryOpIC::GENERIC:
Steve Block1e0659c2011-05-24 12:43:12 +01002024 GenerateGeneric(masm);
2025 break;
2026 default:
2027 UNREACHABLE();
2028 }
2029}
2030
2031
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002032void BinaryOpStub::PrintName(StringStream* stream) {
Steve Block1e0659c2011-05-24 12:43:12 +01002033 const char* op_name = Token::Name(op_);
2034 const char* overwrite_name;
2035 switch (mode_) {
2036 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
2037 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
2038 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
2039 default: overwrite_name = "UnknownOverwrite"; break;
2040 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002041 stream->Add("BinaryOpStub_%s_%s_%s",
2042 op_name,
2043 overwrite_name,
2044 BinaryOpIC::GetName(operands_type_));
Steve Block1e0659c2011-05-24 12:43:12 +01002045}
2046
2047
Ben Murdoch257744e2011-11-30 15:57:28 +00002048void BinaryOpStub::GenerateSmiSmiOperation(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002049 Register left = r1;
2050 Register right = r0;
2051 Register scratch1 = r7;
2052 Register scratch2 = r9;
2053
2054 ASSERT(right.is(r0));
2055 STATIC_ASSERT(kSmiTag == 0);
2056
2057 Label not_smi_result;
2058 switch (op_) {
2059 case Token::ADD:
2060 __ add(right, left, Operand(right), SetCC); // Add optimistically.
2061 __ Ret(vc);
2062 __ sub(right, right, Operand(left)); // Revert optimistic add.
2063 break;
2064 case Token::SUB:
2065 __ sub(right, left, Operand(right), SetCC); // Subtract optimistically.
2066 __ Ret(vc);
2067 __ sub(right, left, Operand(right)); // Revert optimistic subtract.
2068 break;
2069 case Token::MUL:
2070 // Remove tag from one of the operands. This way the multiplication result
2071 // will be a smi if it fits the smi range.
2072 __ SmiUntag(ip, right);
2073 // Do multiplication
2074 // scratch1 = lower 32 bits of ip * left.
2075 // scratch2 = higher 32 bits of ip * left.
2076 __ smull(scratch1, scratch2, left, ip);
2077 // Check for overflowing the smi range - no overflow if higher 33 bits of
2078 // the result are identical.
2079 __ mov(ip, Operand(scratch1, ASR, 31));
2080 __ cmp(ip, Operand(scratch2));
2081 __ b(ne, &not_smi_result);
2082 // Go slow on zero result to handle -0.
2083 __ tst(scratch1, Operand(scratch1));
2084 __ mov(right, Operand(scratch1), LeaveCC, ne);
2085 __ Ret(ne);
2086 // We need -0 if we were multiplying a negative number with 0 to get 0.
2087 // We know one of them was zero.
2088 __ add(scratch2, right, Operand(left), SetCC);
2089 __ mov(right, Operand(Smi::FromInt(0)), LeaveCC, pl);
2090 __ Ret(pl); // Return smi 0 if the non-zero one was positive.
2091 // We fall through here if we multiplied a negative number with 0, because
2092 // that would mean we should produce -0.
2093 break;
2094 case Token::DIV:
2095 // Check for power of two on the right hand side.
2096 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
2097 // Check for positive and no remainder (scratch1 contains right - 1).
2098 __ orr(scratch2, scratch1, Operand(0x80000000u));
2099 __ tst(left, scratch2);
2100 __ b(ne, &not_smi_result);
2101
2102 // Perform division by shifting.
2103 __ CountLeadingZeros(scratch1, scratch1, scratch2);
2104 __ rsb(scratch1, scratch1, Operand(31));
2105 __ mov(right, Operand(left, LSR, scratch1));
2106 __ Ret();
2107 break;
2108 case Token::MOD:
2109 // Check for two positive smis.
2110 __ orr(scratch1, left, Operand(right));
2111 __ tst(scratch1, Operand(0x80000000u | kSmiTagMask));
2112 __ b(ne, &not_smi_result);
2113
2114 // Check for power of two on the right hand side.
2115 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
2116
2117 // Perform modulus by masking.
2118 __ and_(right, left, Operand(scratch1));
2119 __ Ret();
2120 break;
2121 case Token::BIT_OR:
2122 __ orr(right, left, Operand(right));
2123 __ Ret();
2124 break;
2125 case Token::BIT_AND:
2126 __ and_(right, left, Operand(right));
2127 __ Ret();
2128 break;
2129 case Token::BIT_XOR:
2130 __ eor(right, left, Operand(right));
2131 __ Ret();
2132 break;
2133 case Token::SAR:
2134 // Remove tags from right operand.
2135 __ GetLeastBitsFromSmi(scratch1, right, 5);
2136 __ mov(right, Operand(left, ASR, scratch1));
2137 // Smi tag result.
2138 __ bic(right, right, Operand(kSmiTagMask));
2139 __ Ret();
2140 break;
2141 case Token::SHR:
2142 // Remove tags from operands. We can't do this on a 31 bit number
2143 // because then the 0s get shifted into bit 30 instead of bit 31.
2144 __ SmiUntag(scratch1, left);
2145 __ GetLeastBitsFromSmi(scratch2, right, 5);
2146 __ mov(scratch1, Operand(scratch1, LSR, scratch2));
2147 // Unsigned shift is not allowed to produce a negative number, so
2148 // check the sign bit and the sign bit after Smi tagging.
2149 __ tst(scratch1, Operand(0xc0000000));
2150 __ b(ne, &not_smi_result);
2151 // Smi tag result.
2152 __ SmiTag(right, scratch1);
2153 __ Ret();
2154 break;
2155 case Token::SHL:
2156 // Remove tags from operands.
2157 __ SmiUntag(scratch1, left);
2158 __ GetLeastBitsFromSmi(scratch2, right, 5);
2159 __ mov(scratch1, Operand(scratch1, LSL, scratch2));
2160 // Check that the signed result fits in a Smi.
2161 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
2162 __ b(mi, &not_smi_result);
2163 __ SmiTag(right, scratch1);
2164 __ Ret();
2165 break;
2166 default:
2167 UNREACHABLE();
2168 }
2169 __ bind(&not_smi_result);
2170}
2171
2172
Ben Murdoch257744e2011-11-30 15:57:28 +00002173void BinaryOpStub::GenerateFPOperation(MacroAssembler* masm,
2174 bool smi_operands,
2175 Label* not_numbers,
2176 Label* gc_required) {
Steve Block1e0659c2011-05-24 12:43:12 +01002177 Register left = r1;
2178 Register right = r0;
2179 Register scratch1 = r7;
2180 Register scratch2 = r9;
Steve Block44f0eee2011-05-26 01:26:41 +01002181 Register scratch3 = r4;
Steve Block1e0659c2011-05-24 12:43:12 +01002182
2183 ASSERT(smi_operands || (not_numbers != NULL));
2184 if (smi_operands && FLAG_debug_code) {
2185 __ AbortIfNotSmi(left);
2186 __ AbortIfNotSmi(right);
2187 }
2188
2189 Register heap_number_map = r6;
2190 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2191
2192 switch (op_) {
2193 case Token::ADD:
2194 case Token::SUB:
2195 case Token::MUL:
2196 case Token::DIV:
2197 case Token::MOD: {
2198 // Load left and right operands into d6 and d7 or r0/r1 and r2/r3
2199 // depending on whether VFP3 is available or not.
2200 FloatingPointHelper::Destination destination =
Ben Murdoch8b112d22011-06-08 16:22:53 +01002201 CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +01002202 op_ != Token::MOD ?
Steve Block1e0659c2011-05-24 12:43:12 +01002203 FloatingPointHelper::kVFPRegisters :
2204 FloatingPointHelper::kCoreRegisters;
2205
2206 // Allocate new heap number for result.
2207 Register result = r5;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002208 GenerateHeapResultAllocation(
2209 masm, result, heap_number_map, scratch1, scratch2, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01002210
2211 // Load the operands.
2212 if (smi_operands) {
2213 FloatingPointHelper::LoadSmis(masm, destination, scratch1, scratch2);
2214 } else {
2215 FloatingPointHelper::LoadOperands(masm,
2216 destination,
2217 heap_number_map,
2218 scratch1,
2219 scratch2,
2220 not_numbers);
2221 }
2222
2223 // Calculate the result.
2224 if (destination == FloatingPointHelper::kVFPRegisters) {
2225 // Using VFP registers:
2226 // d6: Left value
2227 // d7: Right value
2228 CpuFeatures::Scope scope(VFP3);
2229 switch (op_) {
2230 case Token::ADD:
2231 __ vadd(d5, d6, d7);
2232 break;
2233 case Token::SUB:
2234 __ vsub(d5, d6, d7);
2235 break;
2236 case Token::MUL:
2237 __ vmul(d5, d6, d7);
2238 break;
2239 case Token::DIV:
2240 __ vdiv(d5, d6, d7);
2241 break;
2242 default:
2243 UNREACHABLE();
2244 }
2245
2246 __ sub(r0, result, Operand(kHeapObjectTag));
2247 __ vstr(d5, r0, HeapNumber::kValueOffset);
2248 __ add(r0, r0, Operand(kHeapObjectTag));
2249 __ Ret();
2250 } else {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002251 // Call the C function to handle the double operation.
2252 FloatingPointHelper::CallCCodeForDoubleOperation(masm,
2253 op_,
2254 result,
2255 scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002256 if (FLAG_debug_code) {
2257 __ stop("Unreachable code.");
2258 }
Steve Block1e0659c2011-05-24 12:43:12 +01002259 }
2260 break;
2261 }
2262 case Token::BIT_OR:
2263 case Token::BIT_XOR:
2264 case Token::BIT_AND:
2265 case Token::SAR:
2266 case Token::SHR:
2267 case Token::SHL: {
2268 if (smi_operands) {
2269 __ SmiUntag(r3, left);
2270 __ SmiUntag(r2, right);
2271 } else {
2272 // Convert operands to 32-bit integers. Right in r2 and left in r3.
Steve Block44f0eee2011-05-26 01:26:41 +01002273 FloatingPointHelper::ConvertNumberToInt32(masm,
2274 left,
2275 r3,
2276 heap_number_map,
2277 scratch1,
2278 scratch2,
2279 scratch3,
2280 d0,
2281 not_numbers);
2282 FloatingPointHelper::ConvertNumberToInt32(masm,
2283 right,
2284 r2,
2285 heap_number_map,
2286 scratch1,
2287 scratch2,
2288 scratch3,
2289 d0,
2290 not_numbers);
Steve Block1e0659c2011-05-24 12:43:12 +01002291 }
2292
2293 Label result_not_a_smi;
2294 switch (op_) {
2295 case Token::BIT_OR:
2296 __ orr(r2, r3, Operand(r2));
2297 break;
2298 case Token::BIT_XOR:
2299 __ eor(r2, r3, Operand(r2));
2300 break;
2301 case Token::BIT_AND:
2302 __ and_(r2, r3, Operand(r2));
2303 break;
2304 case Token::SAR:
2305 // Use only the 5 least significant bits of the shift count.
Steve Block1e0659c2011-05-24 12:43:12 +01002306 __ GetLeastBitsFromInt32(r2, r2, 5);
2307 __ mov(r2, Operand(r3, ASR, r2));
2308 break;
2309 case Token::SHR:
2310 // Use only the 5 least significant bits of the shift count.
2311 __ GetLeastBitsFromInt32(r2, r2, 5);
2312 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2313 // SHR is special because it is required to produce a positive answer.
2314 // The code below for writing into heap numbers isn't capable of
2315 // writing the register as an unsigned int so we go to slow case if we
2316 // hit this case.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002317 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002318 __ b(mi, &result_not_a_smi);
2319 } else {
2320 __ b(mi, not_numbers);
2321 }
2322 break;
2323 case Token::SHL:
2324 // Use only the 5 least significant bits of the shift count.
2325 __ GetLeastBitsFromInt32(r2, r2, 5);
2326 __ mov(r2, Operand(r3, LSL, r2));
2327 break;
2328 default:
2329 UNREACHABLE();
2330 }
2331
2332 // Check that the *signed* result fits in a smi.
2333 __ add(r3, r2, Operand(0x40000000), SetCC);
2334 __ b(mi, &result_not_a_smi);
2335 __ SmiTag(r0, r2);
2336 __ Ret();
2337
2338 // Allocate new heap number for result.
2339 __ bind(&result_not_a_smi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002340 Register result = r5;
2341 if (smi_operands) {
2342 __ AllocateHeapNumber(
2343 result, scratch1, scratch2, heap_number_map, gc_required);
2344 } else {
2345 GenerateHeapResultAllocation(
2346 masm, result, heap_number_map, scratch1, scratch2, gc_required);
2347 }
Steve Block1e0659c2011-05-24 12:43:12 +01002348
2349 // r2: Answer as signed int32.
2350 // r5: Heap number to write answer into.
2351
2352 // Nothing can go wrong now, so move the heap number to r0, which is the
2353 // result.
2354 __ mov(r0, Operand(r5));
2355
Ben Murdoch8b112d22011-06-08 16:22:53 +01002356 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002357 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted. As
2358 // mentioned above SHR needs to always produce a positive result.
2359 CpuFeatures::Scope scope(VFP3);
2360 __ vmov(s0, r2);
2361 if (op_ == Token::SHR) {
2362 __ vcvt_f64_u32(d0, s0);
2363 } else {
2364 __ vcvt_f64_s32(d0, s0);
2365 }
2366 __ sub(r3, r0, Operand(kHeapObjectTag));
2367 __ vstr(d0, r3, HeapNumber::kValueOffset);
2368 __ Ret();
2369 } else {
2370 // Tail call that writes the int32 in r2 to the heap number in r0, using
2371 // r3 as scratch. r0 is preserved and returned.
2372 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
2373 __ TailCallStub(&stub);
2374 }
2375 break;
2376 }
2377 default:
2378 UNREACHABLE();
2379 }
2380}
2381
2382
2383// Generate the smi code. If the operation on smis are successful this return is
2384// generated. If the result is not a smi and heap number allocation is not
2385// requested the code falls through. If number allocation is requested but a
2386// heap number cannot be allocated the code jumps to the lable gc_required.
Ben Murdoch257744e2011-11-30 15:57:28 +00002387void BinaryOpStub::GenerateSmiCode(
2388 MacroAssembler* masm,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002389 Label* use_runtime,
Steve Block1e0659c2011-05-24 12:43:12 +01002390 Label* gc_required,
2391 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
2392 Label not_smis;
2393
2394 Register left = r1;
2395 Register right = r0;
2396 Register scratch1 = r7;
2397 Register scratch2 = r9;
2398
2399 // Perform combined smi check on both operands.
2400 __ orr(scratch1, left, Operand(right));
2401 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002402 __ JumpIfNotSmi(scratch1, &not_smis);
Steve Block1e0659c2011-05-24 12:43:12 +01002403
2404 // If the smi-smi operation results in a smi return is generated.
2405 GenerateSmiSmiOperation(masm);
2406
2407 // If heap number results are possible generate the result in an allocated
2408 // heap number.
2409 if (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002410 GenerateFPOperation(masm, true, use_runtime, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01002411 }
2412 __ bind(&not_smis);
2413}
2414
2415
Ben Murdoch257744e2011-11-30 15:57:28 +00002416void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002417 Label not_smis, call_runtime;
2418
Ben Murdoch257744e2011-11-30 15:57:28 +00002419 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
2420 result_type_ == BinaryOpIC::SMI) {
Steve Block1e0659c2011-05-24 12:43:12 +01002421 // Only allow smi results.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002422 GenerateSmiCode(masm, &call_runtime, NULL, NO_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002423 } else {
2424 // Allow heap number result and don't make a transition if a heap number
2425 // cannot be allocated.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002426 GenerateSmiCode(masm,
2427 &call_runtime,
2428 &call_runtime,
2429 ALLOW_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002430 }
2431
2432 // Code falls through if the result is not returned as either a smi or heap
2433 // number.
2434 GenerateTypeTransition(masm);
2435
2436 __ bind(&call_runtime);
2437 GenerateCallRuntime(masm);
2438}
2439
2440
Ben Murdoch257744e2011-11-30 15:57:28 +00002441void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
2442 ASSERT(operands_type_ == BinaryOpIC::STRING);
Steve Block1e0659c2011-05-24 12:43:12 +01002443 ASSERT(op_ == Token::ADD);
2444 // Try to add arguments as strings, otherwise, transition to the generic
Ben Murdoch257744e2011-11-30 15:57:28 +00002445 // BinaryOpIC type.
Steve Block1e0659c2011-05-24 12:43:12 +01002446 GenerateAddStrings(masm);
2447 GenerateTypeTransition(masm);
2448}
2449
2450
Ben Murdoch257744e2011-11-30 15:57:28 +00002451void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
2452 Label call_runtime;
2453 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
2454 ASSERT(op_ == Token::ADD);
2455 // If both arguments are strings, call the string add stub.
2456 // Otherwise, do a transition.
2457
2458 // Registers containing left and right operands respectively.
2459 Register left = r1;
2460 Register right = r0;
2461
2462 // Test if left operand is a string.
2463 __ JumpIfSmi(left, &call_runtime);
2464 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
2465 __ b(ge, &call_runtime);
2466
2467 // Test if right operand is a string.
2468 __ JumpIfSmi(right, &call_runtime);
2469 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
2470 __ b(ge, &call_runtime);
2471
2472 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
2473 GenerateRegisterArgsPush(masm);
2474 __ TailCallStub(&string_add_stub);
2475
2476 __ bind(&call_runtime);
2477 GenerateTypeTransition(masm);
2478}
2479
2480
2481void BinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
2482 ASSERT(operands_type_ == BinaryOpIC::INT32);
Steve Block1e0659c2011-05-24 12:43:12 +01002483
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002484 Register left = r1;
2485 Register right = r0;
2486 Register scratch1 = r7;
2487 Register scratch2 = r9;
2488 DwVfpRegister double_scratch = d0;
2489 SwVfpRegister single_scratch = s3;
2490
2491 Register heap_number_result = no_reg;
2492 Register heap_number_map = r6;
2493 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2494
2495 Label call_runtime;
2496 // Labels for type transition, used for wrong input or output types.
2497 // Both label are currently actually bound to the same position. We use two
2498 // different label to differentiate the cause leading to type transition.
2499 Label transition;
2500
2501 // Smi-smi fast case.
2502 Label skip;
2503 __ orr(scratch1, left, right);
2504 __ JumpIfNotSmi(scratch1, &skip);
2505 GenerateSmiSmiOperation(masm);
2506 // Fall through if the result is not a smi.
2507 __ bind(&skip);
2508
2509 switch (op_) {
2510 case Token::ADD:
2511 case Token::SUB:
2512 case Token::MUL:
2513 case Token::DIV:
2514 case Token::MOD: {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002515 // Load both operands and check that they are 32-bit integer.
2516 // Jump to type transition if they are not. The registers r0 and r1 (right
2517 // and left) are preserved for the runtime call.
2518 FloatingPointHelper::Destination destination =
2519 (CpuFeatures::IsSupported(VFP3) && op_ != Token::MOD)
2520 ? FloatingPointHelper::kVFPRegisters
2521 : FloatingPointHelper::kCoreRegisters;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002522
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002523 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2524 right,
2525 destination,
2526 d7,
2527 r2,
2528 r3,
2529 heap_number_map,
2530 scratch1,
2531 scratch2,
2532 s0,
2533 &transition);
2534 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2535 left,
2536 destination,
2537 d6,
2538 r4,
2539 r5,
2540 heap_number_map,
2541 scratch1,
2542 scratch2,
2543 s0,
2544 &transition);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002545
2546 if (destination == FloatingPointHelper::kVFPRegisters) {
2547 CpuFeatures::Scope scope(VFP3);
2548 Label return_heap_number;
2549 switch (op_) {
2550 case Token::ADD:
2551 __ vadd(d5, d6, d7);
2552 break;
2553 case Token::SUB:
2554 __ vsub(d5, d6, d7);
2555 break;
2556 case Token::MUL:
2557 __ vmul(d5, d6, d7);
2558 break;
2559 case Token::DIV:
2560 __ vdiv(d5, d6, d7);
2561 break;
2562 default:
2563 UNREACHABLE();
2564 }
2565
2566 if (op_ != Token::DIV) {
2567 // These operations produce an integer result.
2568 // Try to return a smi if we can.
2569 // Otherwise return a heap number if allowed, or jump to type
2570 // transition.
2571
2572 __ EmitVFPTruncate(kRoundToZero,
2573 single_scratch,
2574 d5,
2575 scratch1,
2576 scratch2);
2577
Ben Murdoch257744e2011-11-30 15:57:28 +00002578 if (result_type_ <= BinaryOpIC::INT32) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002579 // If the ne condition is set, result does
2580 // not fit in a 32-bit integer.
2581 __ b(ne, &transition);
2582 }
2583
2584 // Check if the result fits in a smi.
2585 __ vmov(scratch1, single_scratch);
2586 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
2587 // If not try to return a heap number.
2588 __ b(mi, &return_heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +01002589 // Check for minus zero. Return heap number for minus zero.
2590 Label not_zero;
2591 __ cmp(scratch1, Operand(0));
2592 __ b(ne, &not_zero);
2593 __ vmov(scratch2, d5.high());
2594 __ tst(scratch2, Operand(HeapNumber::kSignMask));
2595 __ b(ne, &return_heap_number);
2596 __ bind(&not_zero);
2597
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002598 // Tag the result and return.
2599 __ SmiTag(r0, scratch1);
2600 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +01002601 } else {
2602 // DIV just falls through to allocating a heap number.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002603 }
2604
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002605 __ bind(&return_heap_number);
2606 // Return a heap number, or fall through to type transition or runtime
2607 // call if we can't.
2608 if (result_type_ >= ((op_ == Token::DIV) ? BinaryOpIC::HEAP_NUMBER
2609 : BinaryOpIC::INT32)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002610 // We are using vfp registers so r5 is available.
2611 heap_number_result = r5;
2612 GenerateHeapResultAllocation(masm,
2613 heap_number_result,
2614 heap_number_map,
2615 scratch1,
2616 scratch2,
2617 &call_runtime);
2618 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
2619 __ vstr(d5, r0, HeapNumber::kValueOffset);
2620 __ mov(r0, heap_number_result);
2621 __ Ret();
2622 }
2623
2624 // A DIV operation expecting an integer result falls through
2625 // to type transition.
2626
2627 } else {
2628 // We preserved r0 and r1 to be able to call runtime.
2629 // Save the left value on the stack.
2630 __ Push(r5, r4);
2631
Steve Block053d10c2011-06-13 19:13:29 +01002632 Label pop_and_call_runtime;
2633
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002634 // Allocate a heap number to store the result.
2635 heap_number_result = r5;
2636 GenerateHeapResultAllocation(masm,
2637 heap_number_result,
2638 heap_number_map,
2639 scratch1,
2640 scratch2,
Steve Block053d10c2011-06-13 19:13:29 +01002641 &pop_and_call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002642
2643 // Load the left value from the value saved on the stack.
2644 __ Pop(r1, r0);
2645
2646 // Call the C function to handle the double operation.
2647 FloatingPointHelper::CallCCodeForDoubleOperation(
2648 masm, op_, heap_number_result, scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002649 if (FLAG_debug_code) {
2650 __ stop("Unreachable code.");
2651 }
Steve Block053d10c2011-06-13 19:13:29 +01002652
2653 __ bind(&pop_and_call_runtime);
2654 __ Drop(2);
2655 __ b(&call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002656 }
2657
2658 break;
2659 }
2660
2661 case Token::BIT_OR:
2662 case Token::BIT_XOR:
2663 case Token::BIT_AND:
2664 case Token::SAR:
2665 case Token::SHR:
2666 case Token::SHL: {
2667 Label return_heap_number;
2668 Register scratch3 = r5;
2669 // Convert operands to 32-bit integers. Right in r2 and left in r3. The
2670 // registers r0 and r1 (right and left) are preserved for the runtime
2671 // call.
2672 FloatingPointHelper::LoadNumberAsInt32(masm,
2673 left,
2674 r3,
2675 heap_number_map,
2676 scratch1,
2677 scratch2,
2678 scratch3,
2679 d0,
2680 &transition);
2681 FloatingPointHelper::LoadNumberAsInt32(masm,
2682 right,
2683 r2,
2684 heap_number_map,
2685 scratch1,
2686 scratch2,
2687 scratch3,
2688 d0,
2689 &transition);
2690
2691 // The ECMA-262 standard specifies that, for shift operations, only the
2692 // 5 least significant bits of the shift value should be used.
2693 switch (op_) {
2694 case Token::BIT_OR:
2695 __ orr(r2, r3, Operand(r2));
2696 break;
2697 case Token::BIT_XOR:
2698 __ eor(r2, r3, Operand(r2));
2699 break;
2700 case Token::BIT_AND:
2701 __ and_(r2, r3, Operand(r2));
2702 break;
2703 case Token::SAR:
2704 __ and_(r2, r2, Operand(0x1f));
2705 __ mov(r2, Operand(r3, ASR, r2));
2706 break;
2707 case Token::SHR:
2708 __ and_(r2, r2, Operand(0x1f));
2709 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2710 // SHR is special because it is required to produce a positive answer.
2711 // We only get a negative result if the shift value (r2) is 0.
2712 // This result cannot be respresented as a signed 32-bit integer, try
2713 // to return a heap number if we can.
2714 // The non vfp3 code does not support this special case, so jump to
2715 // runtime if we don't support it.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002716 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002717 __ b(mi, (result_type_ <= BinaryOpIC::INT32)
2718 ? &transition
2719 : &return_heap_number);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002720 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002721 __ b(mi, (result_type_ <= BinaryOpIC::INT32)
2722 ? &transition
2723 : &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002724 }
2725 break;
2726 case Token::SHL:
2727 __ and_(r2, r2, Operand(0x1f));
2728 __ mov(r2, Operand(r3, LSL, r2));
2729 break;
2730 default:
2731 UNREACHABLE();
2732 }
2733
2734 // Check if the result fits in a smi.
2735 __ add(scratch1, r2, Operand(0x40000000), SetCC);
2736 // If not try to return a heap number. (We know the result is an int32.)
2737 __ b(mi, &return_heap_number);
2738 // Tag the result and return.
2739 __ SmiTag(r0, r2);
2740 __ Ret();
2741
2742 __ bind(&return_heap_number);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002743 heap_number_result = r5;
2744 GenerateHeapResultAllocation(masm,
2745 heap_number_result,
2746 heap_number_map,
2747 scratch1,
2748 scratch2,
2749 &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002750
Ben Murdoch8b112d22011-06-08 16:22:53 +01002751 if (CpuFeatures::IsSupported(VFP3)) {
2752 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002753 if (op_ != Token::SHR) {
2754 // Convert the result to a floating point value.
2755 __ vmov(double_scratch.low(), r2);
2756 __ vcvt_f64_s32(double_scratch, double_scratch.low());
2757 } else {
2758 // The result must be interpreted as an unsigned 32-bit integer.
2759 __ vmov(double_scratch.low(), r2);
2760 __ vcvt_f64_u32(double_scratch, double_scratch.low());
2761 }
2762
2763 // Store the result.
2764 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
2765 __ vstr(double_scratch, r0, HeapNumber::kValueOffset);
2766 __ mov(r0, heap_number_result);
2767 __ Ret();
2768 } else {
2769 // Tail call that writes the int32 in r2 to the heap number in r0, using
2770 // r3 as scratch. r0 is preserved and returned.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002771 __ mov(r0, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002772 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
2773 __ TailCallStub(&stub);
2774 }
2775
2776 break;
2777 }
2778
2779 default:
2780 UNREACHABLE();
2781 }
2782
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002783 // We never expect DIV to yield an integer result, so we always generate
2784 // type transition code for DIV operations expecting an integer result: the
2785 // code will fall through to this type transition.
2786 if (transition.is_linked() ||
2787 ((op_ == Token::DIV) && (result_type_ <= BinaryOpIC::INT32))) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002788 __ bind(&transition);
2789 GenerateTypeTransition(masm);
2790 }
2791
2792 __ bind(&call_runtime);
2793 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01002794}
2795
2796
Ben Murdoch257744e2011-11-30 15:57:28 +00002797void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01002798 Label call_runtime;
2799
2800 if (op_ == Token::ADD) {
2801 // Handle string addition here, because it is the only operation
2802 // that does not do a ToNumber conversion on the operands.
2803 GenerateAddStrings(masm);
2804 }
2805
2806 // Convert oddball arguments to numbers.
2807 Label check, done;
2808 __ CompareRoot(r1, Heap::kUndefinedValueRootIndex);
2809 __ b(ne, &check);
2810 if (Token::IsBitOp(op_)) {
2811 __ mov(r1, Operand(Smi::FromInt(0)));
2812 } else {
2813 __ LoadRoot(r1, Heap::kNanValueRootIndex);
2814 }
2815 __ jmp(&done);
2816 __ bind(&check);
2817 __ CompareRoot(r0, Heap::kUndefinedValueRootIndex);
2818 __ b(ne, &done);
2819 if (Token::IsBitOp(op_)) {
2820 __ mov(r0, Operand(Smi::FromInt(0)));
2821 } else {
2822 __ LoadRoot(r0, Heap::kNanValueRootIndex);
2823 }
2824 __ bind(&done);
2825
2826 GenerateHeapNumberStub(masm);
2827}
2828
2829
Ben Murdoch257744e2011-11-30 15:57:28 +00002830void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01002831 Label call_runtime;
2832 GenerateFPOperation(masm, false, &call_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002833
2834 __ bind(&call_runtime);
2835 GenerateCallRuntime(masm);
2836}
2837
2838
Ben Murdoch257744e2011-11-30 15:57:28 +00002839void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002840 Label call_runtime, call_string_add_or_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01002841
Ben Murdoch8b112d22011-06-08 16:22:53 +01002842 GenerateSmiCode(masm, &call_runtime, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002843
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002844 GenerateFPOperation(masm, false, &call_string_add_or_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002845
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002846 __ bind(&call_string_add_or_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002847 if (op_ == Token::ADD) {
2848 GenerateAddStrings(masm);
2849 }
2850
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002851 __ bind(&call_runtime);
2852 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01002853}
2854
2855
Ben Murdoch257744e2011-11-30 15:57:28 +00002856void BinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002857 ASSERT(op_ == Token::ADD);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002858 Label left_not_string, call_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01002859
2860 Register left = r1;
2861 Register right = r0;
Steve Block1e0659c2011-05-24 12:43:12 +01002862
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002863 // Check if left argument is a string.
2864 __ JumpIfSmi(left, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002865 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002866 __ b(ge, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002867
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002868 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2869 GenerateRegisterArgsPush(masm);
2870 __ TailCallStub(&string_add_left_stub);
2871
2872 // Left operand is not a string, test right.
2873 __ bind(&left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002874 __ JumpIfSmi(right, &call_runtime);
2875 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
2876 __ b(ge, &call_runtime);
2877
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002878 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
Steve Block1e0659c2011-05-24 12:43:12 +01002879 GenerateRegisterArgsPush(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002880 __ TailCallStub(&string_add_right_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01002881
2882 // At least one argument is not a string.
2883 __ bind(&call_runtime);
2884}
2885
2886
Ben Murdoch257744e2011-11-30 15:57:28 +00002887void BinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002888 GenerateRegisterArgsPush(masm);
2889 switch (op_) {
2890 case Token::ADD:
Ben Murdoch257744e2011-11-30 15:57:28 +00002891 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002892 break;
2893 case Token::SUB:
Ben Murdoch257744e2011-11-30 15:57:28 +00002894 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002895 break;
2896 case Token::MUL:
Ben Murdoch257744e2011-11-30 15:57:28 +00002897 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002898 break;
2899 case Token::DIV:
Ben Murdoch257744e2011-11-30 15:57:28 +00002900 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002901 break;
2902 case Token::MOD:
Ben Murdoch257744e2011-11-30 15:57:28 +00002903 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002904 break;
2905 case Token::BIT_OR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002906 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002907 break;
2908 case Token::BIT_AND:
Ben Murdoch257744e2011-11-30 15:57:28 +00002909 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002910 break;
2911 case Token::BIT_XOR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002912 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002913 break;
2914 case Token::SAR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002915 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002916 break;
2917 case Token::SHR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002918 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002919 break;
2920 case Token::SHL:
Ben Murdoch257744e2011-11-30 15:57:28 +00002921 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002922 break;
2923 default:
2924 UNREACHABLE();
2925 }
2926}
2927
2928
Ben Murdoch257744e2011-11-30 15:57:28 +00002929void BinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
2930 Register result,
2931 Register heap_number_map,
2932 Register scratch1,
2933 Register scratch2,
2934 Label* gc_required) {
Steve Block1e0659c2011-05-24 12:43:12 +01002935 // Code below will scratch result if allocation fails. To keep both arguments
2936 // intact for the runtime call result cannot be one of these.
2937 ASSERT(!result.is(r0) && !result.is(r1));
2938
2939 if (mode_ == OVERWRITE_LEFT || mode_ == OVERWRITE_RIGHT) {
2940 Label skip_allocation, allocated;
2941 Register overwritable_operand = mode_ == OVERWRITE_LEFT ? r1 : r0;
2942 // If the overwritable operand is already an object, we skip the
2943 // allocation of a heap number.
2944 __ JumpIfNotSmi(overwritable_operand, &skip_allocation);
2945 // Allocate a heap number for the result.
2946 __ AllocateHeapNumber(
2947 result, scratch1, scratch2, heap_number_map, gc_required);
2948 __ b(&allocated);
2949 __ bind(&skip_allocation);
2950 // Use object holding the overwritable operand for result.
2951 __ mov(result, Operand(overwritable_operand));
2952 __ bind(&allocated);
2953 } else {
2954 ASSERT(mode_ == NO_OVERWRITE);
2955 __ AllocateHeapNumber(
2956 result, scratch1, scratch2, heap_number_map, gc_required);
2957 }
2958}
2959
2960
Ben Murdoch257744e2011-11-30 15:57:28 +00002961void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002962 __ Push(r1, r0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002963}
2964
2965
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002966void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002967 // Untagged case: double input in d2, double result goes
2968 // into d2.
2969 // Tagged case: tagged input on top of stack and in r0,
2970 // tagged result (heap number) goes into r0.
2971
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002972 Label input_not_smi;
2973 Label loaded;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002974 Label calculate;
2975 Label invalid_cache;
2976 const Register scratch0 = r9;
2977 const Register scratch1 = r7;
2978 const Register cache_entry = r0;
2979 const bool tagged = (argument_type_ == TAGGED);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002980
Ben Murdoch8b112d22011-06-08 16:22:53 +01002981 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002982 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002983 if (tagged) {
2984 // Argument is a number and is on stack and in r0.
2985 // Load argument and check if it is a smi.
2986 __ JumpIfNotSmi(r0, &input_not_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002987
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002988 // Input is a smi. Convert to double and load the low and high words
2989 // of the double into r2, r3.
2990 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
2991 __ b(&loaded);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002992
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002993 __ bind(&input_not_smi);
2994 // Check if input is a HeapNumber.
2995 __ CheckMap(r0,
2996 r1,
2997 Heap::kHeapNumberMapRootIndex,
2998 &calculate,
Ben Murdoch257744e2011-11-30 15:57:28 +00002999 DONT_DO_SMI_CHECK);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003000 // Input is a HeapNumber. Load it to a double register and store the
3001 // low and high words into r2, r3.
3002 __ vldr(d0, FieldMemOperand(r0, HeapNumber::kValueOffset));
3003 __ vmov(r2, r3, d0);
3004 } else {
3005 // Input is untagged double in d2. Output goes to d2.
3006 __ vmov(r2, r3, d2);
3007 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003008 __ bind(&loaded);
3009 // r2 = low 32 bits of double value
3010 // r3 = high 32 bits of double value
3011 // Compute hash (the shifts are arithmetic):
3012 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
3013 __ eor(r1, r2, Operand(r3));
3014 __ eor(r1, r1, Operand(r1, ASR, 16));
3015 __ eor(r1, r1, Operand(r1, ASR, 8));
Steve Block44f0eee2011-05-26 01:26:41 +01003016 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
3017 __ And(r1, r1, Operand(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003018
3019 // r2 = low 32 bits of double value.
3020 // r3 = high 32 bits of double value.
3021 // r1 = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +01003022 Isolate* isolate = masm->isolate();
3023 ExternalReference cache_array =
3024 ExternalReference::transcendental_cache_array_address(isolate);
3025 __ mov(cache_entry, Operand(cache_array));
3026 // cache_entry points to cache array.
3027 int cache_array_index
3028 = type_ * sizeof(isolate->transcendental_cache()->caches_[0]);
3029 __ ldr(cache_entry, MemOperand(cache_entry, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003030 // r0 points to the cache for the type type_.
3031 // If NULL, the cache hasn't been initialized yet, so go through runtime.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003032 __ cmp(cache_entry, Operand(0, RelocInfo::NONE));
3033 __ b(eq, &invalid_cache);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003034
3035#ifdef DEBUG
3036 // Check that the layout of cache elements match expectations.
Steve Block44f0eee2011-05-26 01:26:41 +01003037 { TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003038 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
3039 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
3040 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
3041 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
3042 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
3043 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
3044 CHECK_EQ(0, elem_in0 - elem_start);
3045 CHECK_EQ(kIntSize, elem_in1 - elem_start);
3046 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
3047 }
3048#endif
3049
3050 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
3051 __ add(r1, r1, Operand(r1, LSL, 1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003052 __ add(cache_entry, cache_entry, Operand(r1, LSL, 2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003053 // Check if cache matches: Double value is stored in uint32_t[2] array.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003054 __ ldm(ia, cache_entry, r4.bit() | r5.bit() | r6.bit());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003055 __ cmp(r2, r4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003056 __ b(ne, &calculate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003057 __ cmp(r3, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003058 __ b(ne, &calculate);
3059 // Cache hit. Load result, cleanup and return.
3060 if (tagged) {
3061 // Pop input value from stack and load result into r0.
3062 __ pop();
3063 __ mov(r0, Operand(r6));
3064 } else {
3065 // Load result into d2.
3066 __ vldr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
3067 }
3068 __ Ret();
Ben Murdoch8b112d22011-06-08 16:22:53 +01003069 } // if (CpuFeatures::IsSupported(VFP3))
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003070
3071 __ bind(&calculate);
3072 if (tagged) {
3073 __ bind(&invalid_cache);
Steve Block44f0eee2011-05-26 01:26:41 +01003074 ExternalReference runtime_function =
3075 ExternalReference(RuntimeFunction(), masm->isolate());
3076 __ TailCallExternalReference(runtime_function, 1, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003077 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003078 if (!CpuFeatures::IsSupported(VFP3)) UNREACHABLE();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003079 CpuFeatures::Scope scope(VFP3);
3080
3081 Label no_update;
3082 Label skip_cache;
3083 const Register heap_number_map = r5;
3084
3085 // Call C function to calculate the result and update the cache.
3086 // Register r0 holds precalculated cache entry address; preserve
3087 // it on the stack and pop it into register cache_entry after the
3088 // call.
3089 __ push(cache_entry);
3090 GenerateCallCFunction(masm, scratch0);
3091 __ GetCFunctionDoubleResult(d2);
3092
3093 // Try to update the cache. If we cannot allocate a
3094 // heap number, we return the result without updating.
3095 __ pop(cache_entry);
3096 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
3097 __ AllocateHeapNumber(r6, scratch0, scratch1, r5, &no_update);
3098 __ vstr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
3099 __ stm(ia, cache_entry, r2.bit() | r3.bit() | r6.bit());
3100 __ Ret();
3101
3102 __ bind(&invalid_cache);
3103 // The cache is invalid. Call runtime which will recreate the
3104 // cache.
3105 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
3106 __ AllocateHeapNumber(r0, scratch0, scratch1, r5, &skip_cache);
3107 __ vstr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
3108 __ EnterInternalFrame();
3109 __ push(r0);
3110 __ CallRuntime(RuntimeFunction(), 1);
3111 __ LeaveInternalFrame();
3112 __ vldr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
3113 __ Ret();
3114
3115 __ bind(&skip_cache);
3116 // Call C function to calculate the result and answer directly
3117 // without updating the cache.
3118 GenerateCallCFunction(masm, scratch0);
3119 __ GetCFunctionDoubleResult(d2);
3120 __ bind(&no_update);
3121
3122 // We return the value in d2 without adding it to the cache, but
3123 // we cause a scavenging GC so that future allocations will succeed.
3124 __ EnterInternalFrame();
3125
3126 // Allocate an aligned object larger than a HeapNumber.
3127 ASSERT(4 * kPointerSize >= HeapNumber::kSize);
3128 __ mov(scratch0, Operand(4 * kPointerSize));
3129 __ push(scratch0);
3130 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
3131 __ LeaveInternalFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003132 __ Ret();
3133 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003134}
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003135
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003136
3137void TranscendentalCacheStub::GenerateCallCFunction(MacroAssembler* masm,
3138 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01003139 Isolate* isolate = masm->isolate();
3140
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003141 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003142 __ PrepareCallCFunction(0, 1, scratch);
3143 if (masm->use_eabi_hardfloat()) {
3144 __ vmov(d0, d2);
3145 } else {
3146 __ vmov(r0, r1, d2);
3147 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003148 switch (type_) {
3149 case TranscendentalCache::SIN:
Ben Murdoch257744e2011-11-30 15:57:28 +00003150 __ CallCFunction(ExternalReference::math_sin_double_function(isolate),
3151 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003152 break;
3153 case TranscendentalCache::COS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003154 __ CallCFunction(ExternalReference::math_cos_double_function(isolate),
3155 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003156 break;
3157 case TranscendentalCache::LOG:
Ben Murdoch257744e2011-11-30 15:57:28 +00003158 __ CallCFunction(ExternalReference::math_log_double_function(isolate),
3159 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003160 break;
3161 default:
3162 UNIMPLEMENTED();
3163 break;
3164 }
3165 __ pop(lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003166}
3167
3168
3169Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
3170 switch (type_) {
3171 // Add more cases when necessary.
3172 case TranscendentalCache::SIN: return Runtime::kMath_sin;
3173 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003174 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003175 default:
3176 UNIMPLEMENTED();
3177 return Runtime::kAbort;
3178 }
3179}
3180
3181
3182void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01003183 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003184}
3185
3186
Steve Block44f0eee2011-05-26 01:26:41 +01003187void MathPowStub::Generate(MacroAssembler* masm) {
3188 Label call_runtime;
3189
Ben Murdoch8b112d22011-06-08 16:22:53 +01003190 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003191 CpuFeatures::Scope scope(VFP3);
3192
3193 Label base_not_smi;
3194 Label exponent_not_smi;
3195 Label convert_exponent;
3196
3197 const Register base = r0;
3198 const Register exponent = r1;
3199 const Register heapnumbermap = r5;
3200 const Register heapnumber = r6;
3201 const DoubleRegister double_base = d0;
3202 const DoubleRegister double_exponent = d1;
3203 const DoubleRegister double_result = d2;
3204 const SwVfpRegister single_scratch = s0;
3205 const Register scratch = r9;
3206 const Register scratch2 = r7;
3207
3208 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
3209 __ ldr(base, MemOperand(sp, 1 * kPointerSize));
3210 __ ldr(exponent, MemOperand(sp, 0 * kPointerSize));
3211
3212 // Convert base to double value and store it in d0.
3213 __ JumpIfNotSmi(base, &base_not_smi);
3214 // Base is a Smi. Untag and convert it.
3215 __ SmiUntag(base);
3216 __ vmov(single_scratch, base);
3217 __ vcvt_f64_s32(double_base, single_scratch);
3218 __ b(&convert_exponent);
3219
3220 __ bind(&base_not_smi);
3221 __ ldr(scratch, FieldMemOperand(base, JSObject::kMapOffset));
3222 __ cmp(scratch, heapnumbermap);
3223 __ b(ne, &call_runtime);
3224 // Base is a heapnumber. Load it into double register.
3225 __ vldr(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
3226
3227 __ bind(&convert_exponent);
3228 __ JumpIfNotSmi(exponent, &exponent_not_smi);
3229 __ SmiUntag(exponent);
3230
3231 // The base is in a double register and the exponent is
3232 // an untagged smi. Allocate a heap number and call a
3233 // C function for integer exponents. The register containing
3234 // the heap number is callee-saved.
3235 __ AllocateHeapNumber(heapnumber,
3236 scratch,
3237 scratch2,
3238 heapnumbermap,
3239 &call_runtime);
3240 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003241 __ PrepareCallCFunction(1, 1, scratch);
3242 __ SetCallCDoubleArguments(double_base, exponent);
Steve Block44f0eee2011-05-26 01:26:41 +01003243 __ CallCFunction(
Ben Murdoch257744e2011-11-30 15:57:28 +00003244 ExternalReference::power_double_int_function(masm->isolate()),
3245 1, 1);
Steve Block44f0eee2011-05-26 01:26:41 +01003246 __ pop(lr);
3247 __ GetCFunctionDoubleResult(double_result);
3248 __ vstr(double_result,
3249 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3250 __ mov(r0, heapnumber);
3251 __ Ret(2 * kPointerSize);
3252
3253 __ bind(&exponent_not_smi);
3254 __ ldr(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
3255 __ cmp(scratch, heapnumbermap);
3256 __ b(ne, &call_runtime);
3257 // Exponent is a heapnumber. Load it into double register.
3258 __ vldr(double_exponent,
3259 FieldMemOperand(exponent, HeapNumber::kValueOffset));
3260
3261 // The base and the exponent are in double registers.
3262 // Allocate a heap number and call a C function for
3263 // double exponents. The register containing
3264 // the heap number is callee-saved.
3265 __ AllocateHeapNumber(heapnumber,
3266 scratch,
3267 scratch2,
3268 heapnumbermap,
3269 &call_runtime);
3270 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003271 __ PrepareCallCFunction(0, 2, scratch);
3272 __ SetCallCDoubleArguments(double_base, double_exponent);
Steve Block44f0eee2011-05-26 01:26:41 +01003273 __ CallCFunction(
Ben Murdoch257744e2011-11-30 15:57:28 +00003274 ExternalReference::power_double_double_function(masm->isolate()),
3275 0, 2);
Steve Block44f0eee2011-05-26 01:26:41 +01003276 __ pop(lr);
3277 __ GetCFunctionDoubleResult(double_result);
3278 __ vstr(double_result,
3279 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3280 __ mov(r0, heapnumber);
3281 __ Ret(2 * kPointerSize);
3282 }
3283
3284 __ bind(&call_runtime);
3285 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
3286}
3287
3288
3289bool CEntryStub::NeedsImmovableCode() {
3290 return true;
3291}
3292
3293
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003294void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003295 __ Throw(r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003296}
3297
3298
3299void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3300 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003301 __ ThrowUncatchable(type, r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003302}
3303
3304
3305void CEntryStub::GenerateCore(MacroAssembler* masm,
3306 Label* throw_normal_exception,
3307 Label* throw_termination_exception,
3308 Label* throw_out_of_memory_exception,
3309 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01003310 bool always_allocate) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003311 // r0: result parameter for PerformGC, if any
3312 // r4: number of arguments including receiver (C callee-saved)
3313 // r5: pointer to builtin function (C callee-saved)
3314 // r6: pointer to the first argument (C callee-saved)
Steve Block44f0eee2011-05-26 01:26:41 +01003315 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003316
3317 if (do_gc) {
3318 // Passing r0.
Ben Murdoch257744e2011-11-30 15:57:28 +00003319 __ PrepareCallCFunction(1, 0, r1);
3320 __ CallCFunction(ExternalReference::perform_gc_function(isolate),
3321 1, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003322 }
3323
3324 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01003325 ExternalReference::heap_always_allocate_scope_depth(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003326 if (always_allocate) {
3327 __ mov(r0, Operand(scope_depth));
3328 __ ldr(r1, MemOperand(r0));
3329 __ add(r1, r1, Operand(1));
3330 __ str(r1, MemOperand(r0));
3331 }
3332
3333 // Call C built-in.
3334 // r0 = argc, r1 = argv
3335 __ mov(r0, Operand(r4));
3336 __ mov(r1, Operand(r6));
3337
Steve Block1e0659c2011-05-24 12:43:12 +01003338#if defined(V8_HOST_ARCH_ARM)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003339 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
3340 int frame_alignment_mask = frame_alignment - 1;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003341 if (FLAG_debug_code) {
3342 if (frame_alignment > kPointerSize) {
3343 Label alignment_as_expected;
3344 ASSERT(IsPowerOf2(frame_alignment));
Steve Block1e0659c2011-05-24 12:43:12 +01003345 __ tst(sp, Operand(frame_alignment_mask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003346 __ b(eq, &alignment_as_expected);
3347 // Don't use Check here, as it will call Runtime_Abort re-entering here.
3348 __ stop("Unexpected alignment");
3349 __ bind(&alignment_as_expected);
3350 }
3351 }
3352#endif
3353
Steve Block44f0eee2011-05-26 01:26:41 +01003354 __ mov(r2, Operand(ExternalReference::isolate_address()));
3355
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003356 // To let the GC traverse the return address of the exit frames, we need to
3357 // know where the return address is. The CEntryStub is unmovable, so
3358 // we can store the address on the stack to be able to find it again and
3359 // we never have to restore it, because it will not change.
Steve Block1e0659c2011-05-24 12:43:12 +01003360 // Compute the return address in lr to return to after the jump below. Pc is
3361 // already at '+ 8' from the current instruction but return is after three
3362 // instructions so add another 4 to pc to get the return address.
3363 masm->add(lr, pc, Operand(4));
3364 __ str(lr, MemOperand(sp, 0));
3365 masm->Jump(r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003366
3367 if (always_allocate) {
3368 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
3369 // though (contain the result).
3370 __ mov(r2, Operand(scope_depth));
3371 __ ldr(r3, MemOperand(r2));
3372 __ sub(r3, r3, Operand(1));
3373 __ str(r3, MemOperand(r2));
3374 }
3375
3376 // check for failure result
3377 Label failure_returned;
3378 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3379 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
3380 __ add(r2, r0, Operand(1));
3381 __ tst(r2, Operand(kFailureTagMask));
3382 __ b(eq, &failure_returned);
3383
3384 // Exit C frame and return.
3385 // r0:r1: result
3386 // sp: stack pointer
3387 // fp: frame pointer
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003388 // Callee-saved register r4 still holds argc.
3389 __ LeaveExitFrame(save_doubles_, r4);
3390 __ mov(pc, lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003391
3392 // check if we should retry or throw exception
3393 Label retry;
3394 __ bind(&failure_returned);
3395 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3396 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
3397 __ b(eq, &retry);
3398
3399 // Special handling of out of memory exceptions.
3400 Failure* out_of_memory = Failure::OutOfMemoryException();
3401 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3402 __ b(eq, throw_out_of_memory_exception);
3403
3404 // Retrieve the pending exception and clear the variable.
Steve Block44f0eee2011-05-26 01:26:41 +01003405 __ mov(ip, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003406 __ ldr(r3, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +01003407 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3408 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003409 __ ldr(r0, MemOperand(ip));
3410 __ str(r3, MemOperand(ip));
3411
3412 // Special handling of termination exceptions which are uncatchable
3413 // by javascript code.
Steve Block44f0eee2011-05-26 01:26:41 +01003414 __ cmp(r0, Operand(isolate->factory()->termination_exception()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003415 __ b(eq, throw_termination_exception);
3416
3417 // Handle normal exception.
3418 __ jmp(throw_normal_exception);
3419
3420 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
3421}
3422
3423
3424void CEntryStub::Generate(MacroAssembler* masm) {
3425 // Called from JavaScript; parameters are on stack as if calling JS function
3426 // r0: number of arguments including receiver
3427 // r1: pointer to builtin function
3428 // fp: frame pointer (restored after C call)
3429 // sp: stack pointer (restored as callee's sp after C call)
3430 // cp: current context (C callee-saved)
3431
3432 // Result returned in r0 or r0+r1 by default.
3433
3434 // NOTE: Invocations of builtins may return failure objects
3435 // instead of a proper result. The builtin entry handles
3436 // this by performing a garbage collection and retrying the
3437 // builtin once.
3438
Steve Block1e0659c2011-05-24 12:43:12 +01003439 // Compute the argv pointer in a callee-saved register.
3440 __ add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
3441 __ sub(r6, r6, Operand(kPointerSize));
3442
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003443 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003444 __ EnterExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003445
Steve Block1e0659c2011-05-24 12:43:12 +01003446 // Setup argc and the builtin function in callee-saved registers.
3447 __ mov(r4, Operand(r0));
3448 __ mov(r5, Operand(r1));
3449
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003450 // r4: number of arguments (C callee-saved)
3451 // r5: pointer to builtin function (C callee-saved)
3452 // r6: pointer to first argument (C callee-saved)
3453
3454 Label throw_normal_exception;
3455 Label throw_termination_exception;
3456 Label throw_out_of_memory_exception;
3457
3458 // Call into the runtime system.
3459 GenerateCore(masm,
3460 &throw_normal_exception,
3461 &throw_termination_exception,
3462 &throw_out_of_memory_exception,
3463 false,
Steve Block1e0659c2011-05-24 12:43:12 +01003464 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003465
3466 // Do space-specific GC and retry runtime call.
3467 GenerateCore(masm,
3468 &throw_normal_exception,
3469 &throw_termination_exception,
3470 &throw_out_of_memory_exception,
3471 true,
Steve Block1e0659c2011-05-24 12:43:12 +01003472 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003473
3474 // Do full GC and retry runtime call one final time.
3475 Failure* failure = Failure::InternalError();
3476 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
3477 GenerateCore(masm,
3478 &throw_normal_exception,
3479 &throw_termination_exception,
3480 &throw_out_of_memory_exception,
3481 true,
Steve Block1e0659c2011-05-24 12:43:12 +01003482 true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003483
3484 __ bind(&throw_out_of_memory_exception);
3485 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3486
3487 __ bind(&throw_termination_exception);
3488 GenerateThrowUncatchable(masm, TERMINATION);
3489
3490 __ bind(&throw_normal_exception);
3491 GenerateThrowTOS(masm);
3492}
3493
3494
3495void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3496 // r0: code entry
3497 // r1: function
3498 // r2: receiver
3499 // r3: argc
3500 // [sp+0]: argv
3501
3502 Label invoke, exit;
3503
3504 // Called from C, so do not pop argc and args on exit (preserve sp)
3505 // No need to save register-passed args
3506 // Save callee-saved registers (incl. cp and fp), sp, and lr
3507 __ stm(db_w, sp, kCalleeSaved | lr.bit());
3508
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003509 if (CpuFeatures::IsSupported(VFP3)) {
3510 CpuFeatures::Scope scope(VFP3);
3511 // Save callee-saved vfp registers.
3512 __ vstm(db_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003513 // Set up the reserved register for 0.0.
3514 __ vmov(kDoubleRegZero, 0.0);
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003515 }
3516
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003517 // Get address of argv, see stm above.
3518 // r0: code entry
3519 // r1: function
3520 // r2: receiver
3521 // r3: argc
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003522
3523 // Setup argv in r4.
3524 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
3525 if (CpuFeatures::IsSupported(VFP3)) {
3526 offset_to_argv += kNumDoubleCalleeSaved * kDoubleSize;
3527 }
3528 __ ldr(r4, MemOperand(sp, offset_to_argv));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003529
3530 // Push a frame with special values setup to mark it as an entry frame.
3531 // r0: code entry
3532 // r1: function
3533 // r2: receiver
3534 // r3: argc
3535 // r4: argv
Steve Block44f0eee2011-05-26 01:26:41 +01003536 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003537 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
3538 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3539 __ mov(r7, Operand(Smi::FromInt(marker)));
3540 __ mov(r6, Operand(Smi::FromInt(marker)));
Steve Block44f0eee2011-05-26 01:26:41 +01003541 __ mov(r5,
3542 Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003543 __ ldr(r5, MemOperand(r5));
3544 __ Push(r8, r7, r6, r5);
3545
3546 // Setup frame pointer for the frame to be pushed.
3547 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3548
Ben Murdochb0fe1622011-05-05 13:52:32 +01003549 // If this is the outermost JS call, set js_entry_sp value.
Steve Block053d10c2011-06-13 19:13:29 +01003550 Label non_outermost_js;
Steve Block44f0eee2011-05-26 01:26:41 +01003551 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address, isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003552 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
3553 __ ldr(r6, MemOperand(r5));
Steve Block053d10c2011-06-13 19:13:29 +01003554 __ cmp(r6, Operand(0));
3555 __ b(ne, &non_outermost_js);
3556 __ str(fp, MemOperand(r5));
3557 __ mov(ip, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
3558 Label cont;
3559 __ b(&cont);
3560 __ bind(&non_outermost_js);
3561 __ mov(ip, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
3562 __ bind(&cont);
3563 __ push(ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003564
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003565 // Call a faked try-block that does the invoke.
3566 __ bl(&invoke);
3567
3568 // Caught exception: Store result (exception) in the pending
3569 // exception field in the JSEnv and return a failure sentinel.
3570 // Coming in here the fp will be invalid because the PushTryHandler below
3571 // sets it to 0 to signal the existence of the JSEntry frame.
Steve Block44f0eee2011-05-26 01:26:41 +01003572 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3573 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003574 __ str(r0, MemOperand(ip));
3575 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
3576 __ b(&exit);
3577
3578 // Invoke: Link this frame into the handler chain.
3579 __ bind(&invoke);
3580 // Must preserve r0-r4, r5-r7 are available.
3581 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
3582 // If an exception not caught by another handler occurs, this handler
3583 // returns control to the code after the bl(&invoke) above, which
3584 // restores all kCalleeSaved registers (including cp and fp) to their
3585 // saved values before returning a failure to C.
3586
3587 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01003588 __ mov(ip, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003589 __ ldr(r5, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +01003590 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3591 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003592 __ str(r5, MemOperand(ip));
3593
3594 // Invoke the function by calling through JS entry trampoline builtin.
3595 // Notice that we cannot store a reference to the trampoline code directly in
3596 // this stub, because runtime stubs are not traversed when doing GC.
3597
3598 // Expected registers by Builtins::JSEntryTrampoline
3599 // r0: code entry
3600 // r1: function
3601 // r2: receiver
3602 // r3: argc
3603 // r4: argv
3604 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01003605 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
3606 isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003607 __ mov(ip, Operand(construct_entry));
3608 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003609 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003610 __ mov(ip, Operand(entry));
3611 }
3612 __ ldr(ip, MemOperand(ip)); // deref address
3613
3614 // Branch and link to JSEntryTrampoline. We don't use the double underscore
3615 // macro for the add instruction because we don't want the coverage tool
3616 // inserting instructions here after we read the pc.
3617 __ mov(lr, Operand(pc));
3618 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
3619
Steve Block053d10c2011-06-13 19:13:29 +01003620 // Unlink this frame from the handler chain.
3621 __ PopTryHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003622
3623 __ bind(&exit); // r0 holds result
Steve Block053d10c2011-06-13 19:13:29 +01003624 // Check if the current stack frame is marked as the outermost JS frame.
3625 Label non_outermost_js_2;
3626 __ pop(r5);
3627 __ cmp(r5, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
3628 __ b(ne, &non_outermost_js_2);
3629 __ mov(r6, Operand(0));
3630 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
3631 __ str(r6, MemOperand(r5));
3632 __ bind(&non_outermost_js_2);
Steve Block053d10c2011-06-13 19:13:29 +01003633
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003634 // Restore the top frame descriptors from the stack.
3635 __ pop(r3);
Steve Block44f0eee2011-05-26 01:26:41 +01003636 __ mov(ip,
3637 Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003638 __ str(r3, MemOperand(ip));
3639
3640 // Reset the stack to the callee saved registers.
3641 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3642
3643 // Restore callee-saved registers and return.
3644#ifdef DEBUG
3645 if (FLAG_debug_code) {
3646 __ mov(lr, Operand(pc));
3647 }
3648#endif
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003649
3650 if (CpuFeatures::IsSupported(VFP3)) {
3651 CpuFeatures::Scope scope(VFP3);
3652 // Restore callee-saved vfp registers.
3653 __ vldm(ia_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
3654 }
3655
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003656 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
3657}
3658
3659
Steve Block1e0659c2011-05-24 12:43:12 +01003660// Uses registers r0 to r4.
3661// Expected input (depending on whether args are in registers or on the stack):
3662// * object: r0 or at sp + 1 * kPointerSize.
3663// * function: r1 or at sp.
3664//
3665// An inlined call site may have been generated before calling this stub.
3666// In this case the offset to the inline site to patch is passed on the stack,
3667// in the safepoint slot for register r4.
3668// (See LCodeGen::DoInstanceOfKnownGlobal)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003669void InstanceofStub::Generate(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01003670 // Call site inlining and patching implies arguments in registers.
3671 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
3672 // ReturnTrueFalse is only implemented for inlined call sites.
3673 ASSERT(!ReturnTrueFalseObject() || HasCallSiteInlineCheck());
3674
Ben Murdochb0fe1622011-05-05 13:52:32 +01003675 // Fixed register usage throughout the stub:
Steve Block9fac8402011-05-12 15:51:54 +01003676 const Register object = r0; // Object (lhs).
Steve Block1e0659c2011-05-24 12:43:12 +01003677 Register map = r3; // Map of the object.
Steve Block9fac8402011-05-12 15:51:54 +01003678 const Register function = r1; // Function (rhs).
Ben Murdochb0fe1622011-05-05 13:52:32 +01003679 const Register prototype = r4; // Prototype of the function.
Steve Block1e0659c2011-05-24 12:43:12 +01003680 const Register inline_site = r9;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003681 const Register scratch = r2;
Steve Block1e0659c2011-05-24 12:43:12 +01003682
3683 const int32_t kDeltaToLoadBoolResult = 3 * kPointerSize;
3684
Ben Murdochb0fe1622011-05-05 13:52:32 +01003685 Label slow, loop, is_instance, is_not_instance, not_js_object;
Steve Block1e0659c2011-05-24 12:43:12 +01003686
Ben Murdoch086aeea2011-05-13 15:57:08 +01003687 if (!HasArgsInRegisters()) {
Steve Block9fac8402011-05-12 15:51:54 +01003688 __ ldr(object, MemOperand(sp, 1 * kPointerSize));
3689 __ ldr(function, MemOperand(sp, 0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003690 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003691
Ben Murdochb0fe1622011-05-05 13:52:32 +01003692 // Check that the left hand is a JS object and load map.
Steve Block1e0659c2011-05-24 12:43:12 +01003693 __ JumpIfSmi(object, &not_js_object);
Steve Block9fac8402011-05-12 15:51:54 +01003694 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003695
Steve Block1e0659c2011-05-24 12:43:12 +01003696 // If there is a call site cache don't look in the global cache, but do the
3697 // real lookup and update the call site cache.
3698 if (!HasCallSiteInlineCheck()) {
3699 Label miss;
3700 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
3701 __ cmp(function, ip);
3702 __ b(ne, &miss);
3703 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
3704 __ cmp(map, ip);
3705 __ b(ne, &miss);
3706 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3707 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003708
Steve Block1e0659c2011-05-24 12:43:12 +01003709 __ bind(&miss);
3710 }
3711
3712 // Get the prototype of the function.
Steve Block9fac8402011-05-12 15:51:54 +01003713 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003714
3715 // Check that the function prototype is a JS object.
Steve Block1e0659c2011-05-24 12:43:12 +01003716 __ JumpIfSmi(prototype, &slow);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003717 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003718
Steve Block1e0659c2011-05-24 12:43:12 +01003719 // Update the global instanceof or call site inlined cache with the current
3720 // map and function. The cached answer will be set when it is known below.
3721 if (!HasCallSiteInlineCheck()) {
3722 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
3723 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
3724 } else {
3725 ASSERT(HasArgsInRegisters());
3726 // Patch the (relocated) inlined map check.
3727
3728 // The offset was stored in r4 safepoint slot.
3729 // (See LCodeGen::DoDeferredLInstanceOfKnownGlobal)
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003730 __ LoadFromSafepointRegisterSlot(scratch, r4);
Steve Block1e0659c2011-05-24 12:43:12 +01003731 __ sub(inline_site, lr, scratch);
3732 // Get the map location in scratch and patch it.
3733 __ GetRelocatedValueLocation(inline_site, scratch);
3734 __ str(map, MemOperand(scratch));
3735 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003736
3737 // Register mapping: r3 is object map and r4 is function prototype.
3738 // Get prototype of object into r2.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003739 __ ldr(scratch, FieldMemOperand(map, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003740
Steve Block1e0659c2011-05-24 12:43:12 +01003741 // We don't need map any more. Use it as a scratch register.
3742 Register scratch2 = map;
3743 map = no_reg;
3744
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003745 // Loop through the prototype chain looking for the function prototype.
Steve Block1e0659c2011-05-24 12:43:12 +01003746 __ LoadRoot(scratch2, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003747 __ bind(&loop);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003748 __ cmp(scratch, Operand(prototype));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003749 __ b(eq, &is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003750 __ cmp(scratch, scratch2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003751 __ b(eq, &is_not_instance);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003752 __ ldr(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
3753 __ ldr(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003754 __ jmp(&loop);
3755
3756 __ bind(&is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003757 if (!HasCallSiteInlineCheck()) {
3758 __ mov(r0, Operand(Smi::FromInt(0)));
3759 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3760 } else {
3761 // Patch the call site to return true.
3762 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
3763 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
3764 // Get the boolean result location in scratch and patch it.
3765 __ GetRelocatedValueLocation(inline_site, scratch);
3766 __ str(r0, MemOperand(scratch));
3767
3768 if (!ReturnTrueFalseObject()) {
3769 __ mov(r0, Operand(Smi::FromInt(0)));
3770 }
3771 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01003772 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003773
3774 __ bind(&is_not_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003775 if (!HasCallSiteInlineCheck()) {
3776 __ mov(r0, Operand(Smi::FromInt(1)));
3777 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3778 } else {
3779 // Patch the call site to return false.
3780 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
3781 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
3782 // Get the boolean result location in scratch and patch it.
3783 __ GetRelocatedValueLocation(inline_site, scratch);
3784 __ str(r0, MemOperand(scratch));
3785
3786 if (!ReturnTrueFalseObject()) {
3787 __ mov(r0, Operand(Smi::FromInt(1)));
3788 }
3789 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01003790 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003791
3792 Label object_not_null, object_not_null_or_smi;
3793 __ bind(&not_js_object);
3794 // Before null, smi and string value checks, check that the rhs is a function
3795 // as for a non-function rhs an exception needs to be thrown.
Steve Block1e0659c2011-05-24 12:43:12 +01003796 __ JumpIfSmi(function, &slow);
3797 __ CompareObjectType(function, scratch2, scratch, JS_FUNCTION_TYPE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003798 __ b(ne, &slow);
3799
3800 // Null is not instance of anything.
Ben Murdoch257744e2011-11-30 15:57:28 +00003801 __ cmp(scratch, Operand(masm->isolate()->factory()->null_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003802 __ b(ne, &object_not_null);
3803 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003804 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003805
3806 __ bind(&object_not_null);
3807 // Smi values are not instances of anything.
Steve Block1e0659c2011-05-24 12:43:12 +01003808 __ JumpIfNotSmi(object, &object_not_null_or_smi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003809 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003810 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003811
3812 __ bind(&object_not_null_or_smi);
3813 // String values are not instances of anything.
3814 __ IsObjectJSStringType(object, scratch, &slow);
3815 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003816 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003817
3818 // Slow-case. Tail call builtin.
Ben Murdoch086aeea2011-05-13 15:57:08 +01003819 __ bind(&slow);
Steve Block1e0659c2011-05-24 12:43:12 +01003820 if (!ReturnTrueFalseObject()) {
3821 if (HasArgsInRegisters()) {
3822 __ Push(r0, r1);
3823 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003824 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003825 } else {
3826 __ EnterInternalFrame();
3827 __ Push(r0, r1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003828 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003829 __ LeaveInternalFrame();
3830 __ cmp(r0, Operand(0));
3831 __ LoadRoot(r0, Heap::kTrueValueRootIndex, eq);
3832 __ LoadRoot(r0, Heap::kFalseValueRootIndex, ne);
3833 __ Ret(HasArgsInRegisters() ? 0 : 2);
3834 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003835}
3836
3837
Steve Block1e0659c2011-05-24 12:43:12 +01003838Register InstanceofStub::left() { return r0; }
3839
3840
3841Register InstanceofStub::right() { return r1; }
3842
3843
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003844void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
3845 // The displacement is the offset of the last parameter (if any)
3846 // relative to the frame pointer.
3847 static const int kDisplacement =
3848 StandardFrameConstants::kCallerSPOffset - kPointerSize;
3849
3850 // Check that the key is a smi.
3851 Label slow;
Steve Block1e0659c2011-05-24 12:43:12 +01003852 __ JumpIfNotSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003853
3854 // Check if the calling frame is an arguments adaptor frame.
3855 Label adaptor;
3856 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3857 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3858 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3859 __ b(eq, &adaptor);
3860
3861 // Check index against formal parameters count limit passed in
3862 // through register r0. Use unsigned comparison to get negative
3863 // check for free.
3864 __ cmp(r1, r0);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003865 __ b(hs, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003866
3867 // Read the argument from the stack and return it.
3868 __ sub(r3, r0, r1);
3869 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3870 __ ldr(r0, MemOperand(r3, kDisplacement));
3871 __ Jump(lr);
3872
3873 // Arguments adaptor case: Check index against actual arguments
3874 // limit found in the arguments adaptor frame. Use unsigned
3875 // comparison to get negative check for free.
3876 __ bind(&adaptor);
3877 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3878 __ cmp(r1, r0);
3879 __ b(cs, &slow);
3880
3881 // Read the argument from the adaptor frame and return it.
3882 __ sub(r3, r0, r1);
3883 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3884 __ ldr(r0, MemOperand(r3, kDisplacement));
3885 __ Jump(lr);
3886
3887 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3888 // by calling the runtime system.
3889 __ bind(&slow);
3890 __ push(r1);
3891 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3892}
3893
3894
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003895void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003896 // sp[0] : number of parameters
3897 // sp[4] : receiver displacement
3898 // sp[8] : function
3899
3900 // Check if the calling frame is an arguments adaptor frame.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003901 Label runtime;
3902 __ ldr(r3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3903 __ ldr(r2, MemOperand(r3, StandardFrameConstants::kContextOffset));
3904 __ cmp(r2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3905 __ b(ne, &runtime);
3906
3907 // Patch the arguments.length and the parameters pointer in the current frame.
3908 __ ldr(r2, MemOperand(r3, ArgumentsAdaptorFrameConstants::kLengthOffset));
3909 __ str(r2, MemOperand(sp, 0 * kPointerSize));
3910 __ add(r3, r3, Operand(r2, LSL, 1));
3911 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
3912 __ str(r3, MemOperand(sp, 1 * kPointerSize));
3913
3914 __ bind(&runtime);
3915 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3916}
3917
3918
3919void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
3920 // Stack layout:
3921 // sp[0] : number of parameters (tagged)
3922 // sp[4] : address of receiver argument
3923 // sp[8] : function
3924 // Registers used over whole function:
3925 // r6 : allocated object (tagged)
3926 // r9 : mapped parameter count (tagged)
3927
3928 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
3929 // r1 = parameter count (tagged)
3930
3931 // Check if the calling frame is an arguments adaptor frame.
3932 Label runtime;
3933 Label adaptor_frame, try_allocate;
3934 __ ldr(r3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3935 __ ldr(r2, MemOperand(r3, StandardFrameConstants::kContextOffset));
3936 __ cmp(r2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3937 __ b(eq, &adaptor_frame);
3938
3939 // No adaptor, parameter count = argument count.
3940 __ mov(r2, r1);
3941 __ b(&try_allocate);
3942
3943 // We have an adaptor frame. Patch the parameters pointer.
3944 __ bind(&adaptor_frame);
3945 __ ldr(r2, MemOperand(r3, ArgumentsAdaptorFrameConstants::kLengthOffset));
3946 __ add(r3, r3, Operand(r2, LSL, 1));
3947 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
3948 __ str(r3, MemOperand(sp, 1 * kPointerSize));
3949
3950 // r1 = parameter count (tagged)
3951 // r2 = argument count (tagged)
3952 // Compute the mapped parameter count = min(r1, r2) in r1.
3953 __ cmp(r1, Operand(r2));
3954 __ mov(r1, Operand(r2), LeaveCC, gt);
3955
3956 __ bind(&try_allocate);
3957
3958 // Compute the sizes of backing store, parameter map, and arguments object.
3959 // 1. Parameter map, has 2 extra words containing context and backing store.
3960 const int kParameterMapHeaderSize =
3961 FixedArray::kHeaderSize + 2 * kPointerSize;
3962 // If there are no mapped parameters, we do not need the parameter_map.
3963 __ cmp(r1, Operand(Smi::FromInt(0)));
3964 __ mov(r9, Operand(0), LeaveCC, eq);
3965 __ mov(r9, Operand(r1, LSL, 1), LeaveCC, ne);
3966 __ add(r9, r9, Operand(kParameterMapHeaderSize), LeaveCC, ne);
3967
3968 // 2. Backing store.
3969 __ add(r9, r9, Operand(r2, LSL, 1));
3970 __ add(r9, r9, Operand(FixedArray::kHeaderSize));
3971
3972 // 3. Arguments object.
3973 __ add(r9, r9, Operand(Heap::kArgumentsObjectSize));
3974
3975 // Do the allocation of all three objects in one go.
3976 __ AllocateInNewSpace(r9, r0, r3, r4, &runtime, TAG_OBJECT);
3977
3978 // r0 = address of new object(s) (tagged)
3979 // r2 = argument count (tagged)
3980 // Get the arguments boilerplate from the current (global) context into r4.
3981 const int kNormalOffset =
3982 Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
3983 const int kAliasedOffset =
3984 Context::SlotOffset(Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX);
3985
3986 __ ldr(r4, MemOperand(r8, Context::SlotOffset(Context::GLOBAL_INDEX)));
3987 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
3988 __ cmp(r1, Operand(0));
3989 __ ldr(r4, MemOperand(r4, kNormalOffset), eq);
3990 __ ldr(r4, MemOperand(r4, kAliasedOffset), ne);
3991
3992 // r0 = address of new object (tagged)
3993 // r1 = mapped parameter count (tagged)
3994 // r2 = argument count (tagged)
3995 // r4 = address of boilerplate object (tagged)
3996 // Copy the JS object part.
3997 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3998 __ ldr(r3, FieldMemOperand(r4, i));
3999 __ str(r3, FieldMemOperand(r0, i));
4000 }
4001
4002 // Setup the callee in-object property.
4003 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
4004 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
4005 const int kCalleeOffset = JSObject::kHeaderSize +
4006 Heap::kArgumentsCalleeIndex * kPointerSize;
4007 __ str(r3, FieldMemOperand(r0, kCalleeOffset));
4008
4009 // Use the length (smi tagged) and set that as an in-object property too.
4010 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
4011 const int kLengthOffset = JSObject::kHeaderSize +
4012 Heap::kArgumentsLengthIndex * kPointerSize;
4013 __ str(r2, FieldMemOperand(r0, kLengthOffset));
4014
4015 // Setup the elements pointer in the allocated arguments object.
4016 // If we allocated a parameter map, r4 will point there, otherwise
4017 // it will point to the backing store.
4018 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
4019 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
4020
4021 // r0 = address of new object (tagged)
4022 // r1 = mapped parameter count (tagged)
4023 // r2 = argument count (tagged)
4024 // r4 = address of parameter map or backing store (tagged)
4025 // Initialize parameter map. If there are no mapped arguments, we're done.
4026 Label skip_parameter_map;
4027 __ cmp(r1, Operand(Smi::FromInt(0)));
4028 // Move backing store address to r3, because it is
4029 // expected there when filling in the unmapped arguments.
4030 __ mov(r3, r4, LeaveCC, eq);
4031 __ b(eq, &skip_parameter_map);
4032
4033 __ LoadRoot(r6, Heap::kNonStrictArgumentsElementsMapRootIndex);
4034 __ str(r6, FieldMemOperand(r4, FixedArray::kMapOffset));
4035 __ add(r6, r1, Operand(Smi::FromInt(2)));
4036 __ str(r6, FieldMemOperand(r4, FixedArray::kLengthOffset));
4037 __ str(r8, FieldMemOperand(r4, FixedArray::kHeaderSize + 0 * kPointerSize));
4038 __ add(r6, r4, Operand(r1, LSL, 1));
4039 __ add(r6, r6, Operand(kParameterMapHeaderSize));
4040 __ str(r6, FieldMemOperand(r4, FixedArray::kHeaderSize + 1 * kPointerSize));
4041
4042 // Copy the parameter slots and the holes in the arguments.
4043 // We need to fill in mapped_parameter_count slots. They index the context,
4044 // where parameters are stored in reverse order, at
4045 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
4046 // The mapped parameter thus need to get indices
4047 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
4048 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
4049 // We loop from right to left.
4050 Label parameters_loop, parameters_test;
4051 __ mov(r6, r1);
4052 __ ldr(r9, MemOperand(sp, 0 * kPointerSize));
4053 __ add(r9, r9, Operand(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
4054 __ sub(r9, r9, Operand(r1));
4055 __ LoadRoot(r7, Heap::kTheHoleValueRootIndex);
4056 __ add(r3, r4, Operand(r6, LSL, 1));
4057 __ add(r3, r3, Operand(kParameterMapHeaderSize));
4058
4059 // r6 = loop variable (tagged)
4060 // r1 = mapping index (tagged)
4061 // r3 = address of backing store (tagged)
4062 // r4 = address of parameter map (tagged)
4063 // r5 = temporary scratch (a.o., for address calculation)
4064 // r7 = the hole value
4065 __ jmp(&parameters_test);
4066
4067 __ bind(&parameters_loop);
4068 __ sub(r6, r6, Operand(Smi::FromInt(1)));
4069 __ mov(r5, Operand(r6, LSL, 1));
4070 __ add(r5, r5, Operand(kParameterMapHeaderSize - kHeapObjectTag));
4071 __ str(r9, MemOperand(r4, r5));
4072 __ sub(r5, r5, Operand(kParameterMapHeaderSize - FixedArray::kHeaderSize));
4073 __ str(r7, MemOperand(r3, r5));
4074 __ add(r9, r9, Operand(Smi::FromInt(1)));
4075 __ bind(&parameters_test);
4076 __ cmp(r6, Operand(Smi::FromInt(0)));
4077 __ b(ne, &parameters_loop);
4078
4079 __ bind(&skip_parameter_map);
4080 // r2 = argument count (tagged)
4081 // r3 = address of backing store (tagged)
4082 // r5 = scratch
4083 // Copy arguments header and remaining slots (if there are any).
4084 __ LoadRoot(r5, Heap::kFixedArrayMapRootIndex);
4085 __ str(r5, FieldMemOperand(r3, FixedArray::kMapOffset));
4086 __ str(r2, FieldMemOperand(r3, FixedArray::kLengthOffset));
4087
4088 Label arguments_loop, arguments_test;
4089 __ mov(r9, r1);
4090 __ ldr(r4, MemOperand(sp, 1 * kPointerSize));
4091 __ sub(r4, r4, Operand(r9, LSL, 1));
4092 __ jmp(&arguments_test);
4093
4094 __ bind(&arguments_loop);
4095 __ sub(r4, r4, Operand(kPointerSize));
4096 __ ldr(r6, MemOperand(r4, 0));
4097 __ add(r5, r3, Operand(r9, LSL, 1));
4098 __ str(r6, FieldMemOperand(r5, FixedArray::kHeaderSize));
4099 __ add(r9, r9, Operand(Smi::FromInt(1)));
4100
4101 __ bind(&arguments_test);
4102 __ cmp(r9, Operand(r2));
4103 __ b(lt, &arguments_loop);
4104
4105 // Return and remove the on-stack parameters.
4106 __ add(sp, sp, Operand(3 * kPointerSize));
4107 __ Ret();
4108
4109 // Do the runtime call to allocate the arguments object.
4110 // r2 = argument count (taggged)
4111 __ bind(&runtime);
4112 __ str(r2, MemOperand(sp, 0 * kPointerSize)); // Patch argument count.
4113 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
4114}
4115
4116
4117void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
4118 // sp[0] : number of parameters
4119 // sp[4] : receiver displacement
4120 // sp[8] : function
4121 // Check if the calling frame is an arguments adaptor frame.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004122 Label adaptor_frame, try_allocate, runtime;
4123 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4124 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4125 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4126 __ b(eq, &adaptor_frame);
4127
4128 // Get the length from the frame.
4129 __ ldr(r1, MemOperand(sp, 0));
4130 __ b(&try_allocate);
4131
4132 // Patch the arguments.length and the parameters pointer.
4133 __ bind(&adaptor_frame);
4134 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4135 __ str(r1, MemOperand(sp, 0));
4136 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
4137 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4138 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4139
4140 // Try the new space allocation. Start out with computing the size
4141 // of the arguments object and the elements array in words.
4142 Label add_arguments_object;
4143 __ bind(&try_allocate);
Iain Merrick9ac36c92010-09-13 15:29:50 +01004144 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004145 __ b(eq, &add_arguments_object);
4146 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
4147 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
4148 __ bind(&add_arguments_object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004149 __ add(r1, r1, Operand(Heap::kArgumentsObjectSizeStrict / kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004150
4151 // Do the allocation of both objects in one go.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004152 __ AllocateInNewSpace(r1,
4153 r0,
4154 r2,
4155 r3,
4156 &runtime,
4157 static_cast<AllocationFlags>(TAG_OBJECT |
4158 SIZE_IN_WORDS));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004159
4160 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004161 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4162 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004163 __ ldr(r4, MemOperand(r4, Context::SlotOffset(
4164 Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004165
4166 // Copy the JS object part.
4167 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
4168
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004169 // Get the length (smi tagged) and set that as an in-object property too.
Steve Block44f0eee2011-05-26 01:26:41 +01004170 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004171 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01004172 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004173 Heap::kArgumentsLengthIndex * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004174
4175 // If there are no actual arguments, we're done.
4176 Label done;
Iain Merrick9ac36c92010-09-13 15:29:50 +01004177 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004178 __ b(eq, &done);
4179
4180 // Get the parameters pointer from the stack.
4181 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
4182
4183 // Setup the elements pointer in the allocated arguments object and
4184 // initialize the header in the elements fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004185 __ add(r4, r0, Operand(Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004186 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
4187 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
4188 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
4189 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004190 // Untag the length for the loop.
4191 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004192
4193 // Copy the fixed array slots.
4194 Label loop;
4195 // Setup r4 to point to the first array slot.
4196 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4197 __ bind(&loop);
4198 // Pre-decrement r2 with kPointerSize on each iteration.
4199 // Pre-decrement in order to skip receiver.
4200 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
4201 // Post-increment r4 with kPointerSize on each iteration.
4202 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
4203 __ sub(r1, r1, Operand(1));
Iain Merrick9ac36c92010-09-13 15:29:50 +01004204 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004205 __ b(ne, &loop);
4206
4207 // Return and remove the on-stack parameters.
4208 __ bind(&done);
4209 __ add(sp, sp, Operand(3 * kPointerSize));
4210 __ Ret();
4211
4212 // Do the runtime call to allocate the arguments object.
4213 __ bind(&runtime);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004214 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004215}
4216
4217
4218void RegExpExecStub::Generate(MacroAssembler* masm) {
4219 // Just jump directly to runtime if native RegExp is not selected at compile
4220 // time or if regexp entry in generated code is turned off runtime switch or
4221 // at compilation.
4222#ifdef V8_INTERPRETED_REGEXP
4223 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4224#else // V8_INTERPRETED_REGEXP
4225 if (!FLAG_regexp_entry_native) {
4226 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4227 return;
4228 }
4229
4230 // Stack frame on entry.
4231 // sp[0]: last_match_info (expected JSArray)
4232 // sp[4]: previous index
4233 // sp[8]: subject string
4234 // sp[12]: JSRegExp object
4235
4236 static const int kLastMatchInfoOffset = 0 * kPointerSize;
4237 static const int kPreviousIndexOffset = 1 * kPointerSize;
4238 static const int kSubjectOffset = 2 * kPointerSize;
4239 static const int kJSRegExpOffset = 3 * kPointerSize;
4240
4241 Label runtime, invoke_regexp;
4242
4243 // Allocation of registers for this function. These are in callee save
4244 // registers and will be preserved by the call to the native RegExp code, as
4245 // this code is called using the normal C calling convention. When calling
4246 // directly from generated code the native RegExp code will not do a GC and
4247 // therefore the content of these registers are safe to use after the call.
4248 Register subject = r4;
4249 Register regexp_data = r5;
4250 Register last_match_info_elements = r6;
4251
4252 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01004253 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004254 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01004255 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004256 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01004257 ExternalReference::address_of_regexp_stack_memory_size(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004258 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
4259 __ ldr(r0, MemOperand(r0, 0));
4260 __ tst(r0, Operand(r0));
4261 __ b(eq, &runtime);
4262
4263 // Check that the first argument is a JSRegExp object.
4264 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
4265 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004266 __ JumpIfSmi(r0, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004267 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
4268 __ b(ne, &runtime);
4269
4270 // Check that the RegExp has been compiled (data contains a fixed array).
4271 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
4272 if (FLAG_debug_code) {
4273 __ tst(regexp_data, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004274 __ Check(ne, "Unexpected type for RegExp data, FixedArray expected");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004275 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
4276 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
4277 }
4278
4279 // regexp_data: RegExp data (FixedArray)
4280 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
4281 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
4282 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
4283 __ b(ne, &runtime);
4284
4285 // regexp_data: RegExp data (FixedArray)
4286 // Check that the number of captures fit in the static offsets vector buffer.
4287 __ ldr(r2,
4288 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4289 // Calculate number of capture registers (number_of_captures + 1) * 2. This
4290 // uses the asumption that smis are 2 * their untagged value.
4291 STATIC_ASSERT(kSmiTag == 0);
4292 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4293 __ add(r2, r2, Operand(2)); // r2 was a smi.
4294 // Check that the static offsets vector buffer is large enough.
4295 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
4296 __ b(hi, &runtime);
4297
4298 // r2: Number of capture registers
4299 // regexp_data: RegExp data (FixedArray)
4300 // Check that the second argument is a string.
4301 __ ldr(subject, MemOperand(sp, kSubjectOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004302 __ JumpIfSmi(subject, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004303 Condition is_string = masm->IsObjectStringType(subject, r0);
4304 __ b(NegateCondition(is_string), &runtime);
4305 // Get the length of the string to r3.
4306 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
4307
4308 // r2: Number of capture registers
4309 // r3: Length of subject string as a smi
4310 // subject: Subject string
4311 // regexp_data: RegExp data (FixedArray)
4312 // Check that the third argument is a positive smi less than the subject
4313 // string length. A negative value will be greater (unsigned comparison).
4314 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004315 __ JumpIfNotSmi(r0, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004316 __ cmp(r3, Operand(r0));
4317 __ b(ls, &runtime);
4318
4319 // r2: Number of capture registers
4320 // subject: Subject string
4321 // regexp_data: RegExp data (FixedArray)
4322 // Check that the fourth object is a JSArray object.
4323 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004324 __ JumpIfSmi(r0, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004325 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
4326 __ b(ne, &runtime);
4327 // Check that the JSArray is in fast case.
4328 __ ldr(last_match_info_elements,
4329 FieldMemOperand(r0, JSArray::kElementsOffset));
4330 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
4331 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
4332 __ cmp(r0, ip);
4333 __ b(ne, &runtime);
4334 // Check that the last match info has space for the capture registers and the
4335 // additional information.
4336 __ ldr(r0,
4337 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
4338 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
4339 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
4340 __ b(gt, &runtime);
4341
4342 // subject: Subject string
4343 // regexp_data: RegExp data (FixedArray)
4344 // Check the representation and encoding of the subject string.
4345 Label seq_string;
4346 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4347 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
4348 // First check for flat string.
4349 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
4350 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
4351 __ b(eq, &seq_string);
4352
4353 // subject: Subject string
4354 // regexp_data: RegExp data (FixedArray)
4355 // Check for flat cons string.
4356 // A flat cons string is a cons string where the second part is the empty
4357 // string. In that case the subject string is just the first part of the cons
4358 // string. Also in this case the first part of the cons string is known to be
4359 // a sequential string or an external string.
4360 STATIC_ASSERT(kExternalStringTag !=0);
4361 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
4362 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
4363 __ b(ne, &runtime);
4364 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
4365 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
4366 __ cmp(r0, r1);
4367 __ b(ne, &runtime);
4368 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
4369 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4370 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
4371 // Is first part a flat string?
4372 STATIC_ASSERT(kSeqStringTag == 0);
4373 __ tst(r0, Operand(kStringRepresentationMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004374 __ b(ne, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004375
4376 __ bind(&seq_string);
4377 // subject: Subject string
4378 // regexp_data: RegExp data (FixedArray)
4379 // r0: Instance type of subject string
4380 STATIC_ASSERT(4 == kAsciiStringTag);
4381 STATIC_ASSERT(kTwoByteStringTag == 0);
4382 // Find the code object based on the assumptions above.
4383 __ and_(r0, r0, Operand(kStringEncodingMask));
4384 __ mov(r3, Operand(r0, ASR, 2), SetCC);
4385 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
4386 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
4387
4388 // Check that the irregexp code has been generated for the actual string
4389 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00004390 // a smi (code flushing support).
4391 __ JumpIfSmi(r7, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004392
Steve Block44f0eee2011-05-26 01:26:41 +01004393 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004394 // r7: code
4395 // subject: Subject string
4396 // regexp_data: RegExp data (FixedArray)
4397 // Load used arguments before starting to push arguments for call to native
4398 // RegExp code to avoid handling changing stack height.
4399 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
4400 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
4401
4402 // r1: previous index
Steve Block44f0eee2011-05-26 01:26:41 +01004403 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004404 // r7: code
4405 // subject: Subject string
4406 // regexp_data: RegExp data (FixedArray)
4407 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01004408 __ IncrementCounter(isolate->counters()->regexp_entry_native(), 1, r0, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004409
Steve Block44f0eee2011-05-26 01:26:41 +01004410 // Isolates: note we add an additional parameter here (isolate pointer).
4411 static const int kRegExpExecuteArguments = 8;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004412 static const int kParameterRegisters = 4;
4413 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004414
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004415 // Stack pointer now points to cell where return address is to be written.
4416 // Arguments are before that on the stack or in registers.
4417
Steve Block44f0eee2011-05-26 01:26:41 +01004418 // Argument 8 (sp[16]): Pass current isolate address.
4419 __ mov(r0, Operand(ExternalReference::isolate_address()));
4420 __ str(r0, MemOperand(sp, 4 * kPointerSize));
4421
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004422 // Argument 7 (sp[12]): Indicate that this is a direct call from JavaScript.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004423 __ mov(r0, Operand(1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004424 __ str(r0, MemOperand(sp, 3 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004425
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004426 // Argument 6 (sp[8]): Start (high end) of backtracking stack memory area.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004427 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
4428 __ ldr(r0, MemOperand(r0, 0));
4429 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
4430 __ ldr(r2, MemOperand(r2, 0));
4431 __ add(r0, r0, Operand(r2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004432 __ str(r0, MemOperand(sp, 2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004433
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004434 // Argument 5 (sp[4]): static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01004435 __ mov(r0,
4436 Operand(ExternalReference::address_of_static_offsets_vector(isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004437 __ str(r0, MemOperand(sp, 1 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004438
4439 // For arguments 4 and 3 get string length, calculate start of string data and
4440 // calculate the shift of the index (0 for ASCII and 1 for two byte).
4441 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
4442 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
4443 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
4444 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4445 __ eor(r3, r3, Operand(1));
4446 // Argument 4 (r3): End of string data
4447 // Argument 3 (r2): Start of string data
4448 __ add(r2, r9, Operand(r1, LSL, r3));
4449 __ add(r3, r9, Operand(r0, LSL, r3));
4450
4451 // Argument 2 (r1): Previous index.
4452 // Already there
4453
4454 // Argument 1 (r0): Subject string.
4455 __ mov(r0, subject);
4456
4457 // Locate the code entry and call it.
4458 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004459 DirectCEntryStub stub;
4460 stub.GenerateCall(masm, r7);
4461
4462 __ LeaveExitFrame(false, no_reg);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004463
4464 // r0: result
4465 // subject: subject string (callee saved)
4466 // regexp_data: RegExp data (callee saved)
4467 // last_match_info_elements: Last match info elements (callee saved)
4468
4469 // Check the result.
4470 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004471
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004472 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
4473 __ b(eq, &success);
4474 Label failure;
4475 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
4476 __ b(eq, &failure);
4477 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
4478 // If not exception it can only be retry. Handle that in the runtime system.
4479 __ b(ne, &runtime);
4480 // Result must now be exception. If there is no pending exception already a
4481 // stack overflow (on the backtrack stack) was detected in RegExp code but
4482 // haven't created the exception yet. Handle that in the runtime system.
4483 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01004484 __ mov(r1, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004485 __ ldr(r1, MemOperand(r1, 0));
Steve Block44f0eee2011-05-26 01:26:41 +01004486 __ mov(r2, Operand(ExternalReference(Isolate::k_pending_exception_address,
4487 isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004488 __ ldr(r0, MemOperand(r2, 0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004489 __ cmp(r0, r1);
4490 __ b(eq, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004491
4492 __ str(r1, MemOperand(r2, 0)); // Clear pending exception.
4493
4494 // Check if the exception is a termination. If so, throw as uncatchable.
4495 __ LoadRoot(ip, Heap::kTerminationExceptionRootIndex);
4496 __ cmp(r0, ip);
4497 Label termination_exception;
4498 __ b(eq, &termination_exception);
4499
4500 __ Throw(r0); // Expects thrown value in r0.
4501
4502 __ bind(&termination_exception);
4503 __ ThrowUncatchable(TERMINATION, r0); // Expects thrown value in r0.
4504
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004505 __ bind(&failure);
4506 // For failure and exception return null.
Ben Murdoch257744e2011-11-30 15:57:28 +00004507 __ mov(r0, Operand(masm->isolate()->factory()->null_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004508 __ add(sp, sp, Operand(4 * kPointerSize));
4509 __ Ret();
4510
4511 // Process the result from the native regexp code.
4512 __ bind(&success);
4513 __ ldr(r1,
4514 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4515 // Calculate number of capture registers (number_of_captures + 1) * 2.
4516 STATIC_ASSERT(kSmiTag == 0);
4517 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4518 __ add(r1, r1, Operand(2)); // r1 was a smi.
4519
4520 // r1: number of capture registers
4521 // r4: subject string
4522 // Store the capture count.
4523 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
4524 __ str(r2, FieldMemOperand(last_match_info_elements,
4525 RegExpImpl::kLastCaptureCountOffset));
4526 // Store last subject and last input.
4527 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
4528 __ str(subject,
4529 FieldMemOperand(last_match_info_elements,
4530 RegExpImpl::kLastSubjectOffset));
4531 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
4532 __ str(subject,
4533 FieldMemOperand(last_match_info_elements,
4534 RegExpImpl::kLastInputOffset));
4535 __ mov(r3, last_match_info_elements);
4536 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
4537
4538 // Get the static offsets vector filled by the native regexp code.
4539 ExternalReference address_of_static_offsets_vector =
Steve Block44f0eee2011-05-26 01:26:41 +01004540 ExternalReference::address_of_static_offsets_vector(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004541 __ mov(r2, Operand(address_of_static_offsets_vector));
4542
4543 // r1: number of capture registers
4544 // r2: offsets vector
4545 Label next_capture, done;
4546 // Capture register counter starts from number of capture registers and
4547 // counts down until wraping after zero.
4548 __ add(r0,
4549 last_match_info_elements,
4550 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
4551 __ bind(&next_capture);
4552 __ sub(r1, r1, Operand(1), SetCC);
4553 __ b(mi, &done);
4554 // Read the value from the static offsets vector buffer.
4555 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
4556 // Store the smi value in the last match info.
4557 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
4558 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
4559 __ jmp(&next_capture);
4560 __ bind(&done);
4561
4562 // Return last match info.
4563 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
4564 __ add(sp, sp, Operand(4 * kPointerSize));
4565 __ Ret();
4566
4567 // Do the runtime call to execute the regexp.
4568 __ bind(&runtime);
4569 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4570#endif // V8_INTERPRETED_REGEXP
4571}
4572
4573
Ben Murdochb0fe1622011-05-05 13:52:32 +01004574void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
4575 const int kMaxInlineLength = 100;
4576 Label slowcase;
4577 Label done;
Ben Murdoch257744e2011-11-30 15:57:28 +00004578 Factory* factory = masm->isolate()->factory();
4579
Ben Murdochb0fe1622011-05-05 13:52:32 +01004580 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4581 STATIC_ASSERT(kSmiTag == 0);
4582 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004583 __ JumpIfNotSmi(r1, &slowcase);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004584 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4585 __ b(hi, &slowcase);
4586 // Smi-tagging is equivalent to multiplying by 2.
4587 // Allocate RegExpResult followed by FixedArray with size in ebx.
4588 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4589 // Elements: [Map][Length][..elements..]
4590 // Size of JSArray with two in-object properties and the header of a
4591 // FixedArray.
4592 int objects_size =
4593 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4594 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4595 __ add(r2, r5, Operand(objects_size));
4596 __ AllocateInNewSpace(
4597 r2, // In: Size, in words.
4598 r0, // Out: Start of allocation (tagged).
4599 r3, // Scratch register.
4600 r4, // Scratch register.
4601 &slowcase,
4602 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
4603 // r0: Start of allocated area, object-tagged.
4604 // r1: Number of elements in array, as smi.
4605 // r5: Number of elements, untagged.
4606
4607 // Set JSArray map to global.regexp_result_map().
4608 // Set empty properties FixedArray.
4609 // Set elements to point to FixedArray allocated right after the JSArray.
4610 // Interleave operations for better latency.
4611 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4612 __ add(r3, r0, Operand(JSRegExpResult::kSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00004613 __ mov(r4, Operand(factory->empty_fixed_array()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004614 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4615 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4616 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4617 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4618 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4619
4620 // Set input, index and length fields from arguments.
4621 __ ldr(r1, MemOperand(sp, kPointerSize * 0));
4622 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4623 __ ldr(r1, MemOperand(sp, kPointerSize * 1));
4624 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4625 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4626 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4627
4628 // Fill out the elements FixedArray.
4629 // r0: JSArray, tagged.
4630 // r3: FixedArray, tagged.
4631 // r5: Number of elements in array, untagged.
4632
4633 // Set map.
Ben Murdoch257744e2011-11-30 15:57:28 +00004634 __ mov(r2, Operand(factory->fixed_array_map()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004635 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4636 // Set FixedArray length.
4637 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
4638 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
4639 // Fill contents of fixed-array with the-hole.
Ben Murdoch257744e2011-11-30 15:57:28 +00004640 __ mov(r2, Operand(factory->the_hole_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004641 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4642 // Fill fixed array elements with hole.
4643 // r0: JSArray, tagged.
4644 // r2: the hole.
4645 // r3: Start of elements in FixedArray.
4646 // r5: Number of elements to fill.
4647 Label loop;
4648 __ tst(r5, Operand(r5));
4649 __ bind(&loop);
4650 __ b(le, &done); // Jump if r1 is negative or zero.
4651 __ sub(r5, r5, Operand(1), SetCC);
4652 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4653 __ jmp(&loop);
4654
4655 __ bind(&done);
4656 __ add(sp, sp, Operand(3 * kPointerSize));
4657 __ Ret();
4658
4659 __ bind(&slowcase);
4660 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
4661}
4662
4663
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004664void CallFunctionStub::Generate(MacroAssembler* masm) {
4665 Label slow;
4666
Ben Murdoch257744e2011-11-30 15:57:28 +00004667 // The receiver might implicitly be the global object. This is
4668 // indicated by passing the hole as the receiver to the call
4669 // function stub.
4670 if (ReceiverMightBeImplicit()) {
4671 Label call;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004672 // Get the receiver from the stack.
4673 // function, receiver [, arguments]
Ben Murdoch257744e2011-11-30 15:57:28 +00004674 __ ldr(r4, MemOperand(sp, argc_ * kPointerSize));
4675 // Call as function is indicated with the hole.
4676 __ CompareRoot(r4, Heap::kTheHoleValueRootIndex);
4677 __ b(ne, &call);
4678 // Patch the receiver on the stack with the global receiver object.
4679 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4680 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
4681 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
4682 __ bind(&call);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004683 }
4684
4685 // Get the function to call from the stack.
4686 // function, receiver [, arguments]
4687 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4688
4689 // Check that the function is really a JavaScript function.
4690 // r1: pushed function (to be verified)
Steve Block1e0659c2011-05-24 12:43:12 +01004691 __ JumpIfSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004692 // Get the map of the function object.
4693 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
4694 __ b(ne, &slow);
4695
4696 // Fast-case: Invoke the function now.
4697 // r1: pushed function
4698 ParameterCount actual(argc_);
Ben Murdoch257744e2011-11-30 15:57:28 +00004699
4700 if (ReceiverMightBeImplicit()) {
4701 Label call_as_function;
4702 __ CompareRoot(r4, Heap::kTheHoleValueRootIndex);
4703 __ b(eq, &call_as_function);
4704 __ InvokeFunction(r1,
4705 actual,
4706 JUMP_FUNCTION,
4707 NullCallWrapper(),
4708 CALL_AS_METHOD);
4709 __ bind(&call_as_function);
4710 }
4711 __ InvokeFunction(r1,
4712 actual,
4713 JUMP_FUNCTION,
4714 NullCallWrapper(),
4715 CALL_AS_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004716
4717 // Slow-case: Non-function called.
4718 __ bind(&slow);
4719 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4720 // of the original receiver from the call site).
4721 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
4722 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
Iain Merrick9ac36c92010-09-13 15:29:50 +01004723 __ mov(r2, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004724 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004725 __ SetCallKind(r5, CALL_AS_METHOD);
Steve Block44f0eee2011-05-26 01:26:41 +01004726 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004727 RelocInfo::CODE_TARGET);
4728}
4729
4730
4731// Unfortunately you have to run without snapshots to see most of these
4732// names in the profile since most compare stubs end up in the snapshot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004733void CompareStub::PrintName(StringStream* stream) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004734 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
4735 (lhs_.is(r1) && rhs_.is(r0)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004736 const char* cc_name;
4737 switch (cc_) {
4738 case lt: cc_name = "LT"; break;
4739 case gt: cc_name = "GT"; break;
4740 case le: cc_name = "LE"; break;
4741 case ge: cc_name = "GE"; break;
4742 case eq: cc_name = "EQ"; break;
4743 case ne: cc_name = "NE"; break;
4744 default: cc_name = "UnknownCondition"; break;
4745 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004746 bool is_equality = cc_ == eq || cc_ == ne;
4747 stream->Add("CompareStub_%s", cc_name);
4748 stream->Add(lhs_.is(r0) ? "_r0" : "_r1");
4749 stream->Add(rhs_.is(r0) ? "_r0" : "_r1");
4750 if (strict_ && is_equality) stream->Add("_STRICT");
4751 if (never_nan_nan_ && is_equality) stream->Add("_NO_NAN");
4752 if (!include_number_compare_) stream->Add("_NO_NUMBER");
4753 if (!include_smi_compare_) stream->Add("_NO_SMI");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004754}
4755
4756
4757int CompareStub::MinorKey() {
4758 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4759 // stubs the never NaN NaN condition is only taken into account if the
4760 // condition is equals.
4761 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
4762 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
4763 (lhs_.is(r1) && rhs_.is(r0)));
4764 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
4765 | RegisterField::encode(lhs_.is(r0))
4766 | StrictField::encode(strict_)
4767 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004768 | IncludeNumberCompareField::encode(include_number_compare_)
4769 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004770}
4771
4772
4773// StringCharCodeAtGenerator
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004774void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4775 Label flat_string;
4776 Label ascii_string;
4777 Label got_char_code;
4778
4779 // If the receiver is a smi trigger the non-string case.
Steve Block1e0659c2011-05-24 12:43:12 +01004780 __ JumpIfSmi(object_, receiver_not_string_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004781
4782 // Fetch the instance type of the receiver into result register.
4783 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4784 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4785 // If the receiver is not a string trigger the non-string case.
4786 __ tst(result_, Operand(kIsNotStringMask));
4787 __ b(ne, receiver_not_string_);
4788
4789 // If the index is non-smi trigger the non-smi case.
Steve Block1e0659c2011-05-24 12:43:12 +01004790 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004791
4792 // Put smi-tagged index into scratch register.
4793 __ mov(scratch_, index_);
4794 __ bind(&got_smi_index_);
4795
4796 // Check for index out of range.
4797 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
4798 __ cmp(ip, Operand(scratch_));
4799 __ b(ls, index_out_of_range_);
4800
4801 // We need special handling for non-flat strings.
4802 STATIC_ASSERT(kSeqStringTag == 0);
4803 __ tst(result_, Operand(kStringRepresentationMask));
4804 __ b(eq, &flat_string);
4805
4806 // Handle non-flat strings.
4807 __ tst(result_, Operand(kIsConsStringMask));
4808 __ b(eq, &call_runtime_);
4809
4810 // ConsString.
4811 // Check whether the right hand side is the empty string (i.e. if
4812 // this is really a flat string in a cons string). If that is not
4813 // the case we would rather go to the runtime system now to flatten
4814 // the string.
4815 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
4816 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
4817 __ cmp(result_, Operand(ip));
4818 __ b(ne, &call_runtime_);
4819 // Get the first of the two strings and load its instance type.
4820 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
4821 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4822 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4823 // If the first cons component is also non-flat, then go to runtime.
4824 STATIC_ASSERT(kSeqStringTag == 0);
4825 __ tst(result_, Operand(kStringRepresentationMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004826 __ b(ne, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004827
4828 // Check for 1-byte or 2-byte string.
4829 __ bind(&flat_string);
4830 STATIC_ASSERT(kAsciiStringTag != 0);
4831 __ tst(result_, Operand(kStringEncodingMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004832 __ b(ne, &ascii_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004833
4834 // 2-byte string.
4835 // Load the 2-byte character code into the result register. We can
4836 // add without shifting since the smi tag size is the log2 of the
4837 // number of bytes in a two-byte character.
4838 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
4839 __ add(scratch_, object_, Operand(scratch_));
4840 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
4841 __ jmp(&got_char_code);
4842
4843 // ASCII string.
4844 // Load the byte into the result register.
4845 __ bind(&ascii_string);
4846 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
4847 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
4848
4849 __ bind(&got_char_code);
4850 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
4851 __ bind(&exit_);
4852}
4853
4854
4855void StringCharCodeAtGenerator::GenerateSlow(
4856 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4857 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4858
4859 // Index is not a smi.
4860 __ bind(&index_not_smi_);
4861 // If index is a heap number, try converting it to an integer.
4862 __ CheckMap(index_,
4863 scratch_,
4864 Heap::kHeapNumberMapRootIndex,
4865 index_not_number_,
Ben Murdoch257744e2011-11-30 15:57:28 +00004866 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004867 call_helper.BeforeCall(masm);
4868 __ Push(object_, index_);
4869 __ push(index_); // Consumed by runtime conversion function.
4870 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4871 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4872 } else {
4873 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4874 // NumberToSmi discards numbers that are not exact integers.
4875 __ CallRuntime(Runtime::kNumberToSmi, 1);
4876 }
4877 // Save the conversion result before the pop instructions below
4878 // have a chance to overwrite it.
4879 __ Move(scratch_, r0);
4880 __ pop(index_);
4881 __ pop(object_);
4882 // Reload the instance type.
4883 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4884 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4885 call_helper.AfterCall(masm);
4886 // If index is still not a smi, it must be out of range.
Steve Block1e0659c2011-05-24 12:43:12 +01004887 __ JumpIfNotSmi(scratch_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004888 // Otherwise, return to the fast path.
4889 __ jmp(&got_smi_index_);
4890
4891 // Call runtime. We get here when the receiver is a string and the
4892 // index is a number, but the code of getting the actual character
4893 // is too complex (e.g., when the string needs to be flattened).
4894 __ bind(&call_runtime_);
4895 call_helper.BeforeCall(masm);
4896 __ Push(object_, index_);
4897 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4898 __ Move(result_, r0);
4899 call_helper.AfterCall(masm);
4900 __ jmp(&exit_);
4901
4902 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4903}
4904
4905
4906// -------------------------------------------------------------------------
4907// StringCharFromCodeGenerator
4908
4909void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4910 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4911 STATIC_ASSERT(kSmiTag == 0);
4912 STATIC_ASSERT(kSmiShiftSize == 0);
4913 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4914 __ tst(code_,
4915 Operand(kSmiTagMask |
4916 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
Steve Block1e0659c2011-05-24 12:43:12 +01004917 __ b(ne, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004918
4919 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01004920 // At this point code register contains smi tagged ASCII char code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004921 STATIC_ASSERT(kSmiTag == 0);
4922 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
4923 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
4924 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4925 __ cmp(result_, Operand(ip));
4926 __ b(eq, &slow_case_);
4927 __ bind(&exit_);
4928}
4929
4930
4931void StringCharFromCodeGenerator::GenerateSlow(
4932 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4933 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4934
4935 __ bind(&slow_case_);
4936 call_helper.BeforeCall(masm);
4937 __ push(code_);
4938 __ CallRuntime(Runtime::kCharFromCode, 1);
4939 __ Move(result_, r0);
4940 call_helper.AfterCall(masm);
4941 __ jmp(&exit_);
4942
4943 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4944}
4945
4946
4947// -------------------------------------------------------------------------
4948// StringCharAtGenerator
4949
4950void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4951 char_code_at_generator_.GenerateFast(masm);
4952 char_from_code_generator_.GenerateFast(masm);
4953}
4954
4955
4956void StringCharAtGenerator::GenerateSlow(
4957 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4958 char_code_at_generator_.GenerateSlow(masm, call_helper);
4959 char_from_code_generator_.GenerateSlow(masm, call_helper);
4960}
4961
4962
4963class StringHelper : public AllStatic {
4964 public:
4965 // Generate code for copying characters using a simple loop. This should only
4966 // be used in places where the number of characters is small and the
4967 // additional setup and checking in GenerateCopyCharactersLong adds too much
4968 // overhead. Copying of overlapping regions is not supported.
4969 // Dest register ends at the position after the last character written.
4970 static void GenerateCopyCharacters(MacroAssembler* masm,
4971 Register dest,
4972 Register src,
4973 Register count,
4974 Register scratch,
4975 bool ascii);
4976
4977 // Generate code for copying a large number of characters. This function
4978 // is allowed to spend extra time setting up conditions to make copying
4979 // faster. Copying of overlapping regions is not supported.
4980 // Dest register ends at the position after the last character written.
4981 static void GenerateCopyCharactersLong(MacroAssembler* masm,
4982 Register dest,
4983 Register src,
4984 Register count,
4985 Register scratch1,
4986 Register scratch2,
4987 Register scratch3,
4988 Register scratch4,
4989 Register scratch5,
4990 int flags);
4991
4992
4993 // Probe the symbol table for a two character string. If the string is
4994 // not found by probing a jump to the label not_found is performed. This jump
4995 // does not guarantee that the string is not in the symbol table. If the
4996 // string is found the code falls through with the string in register r0.
4997 // Contents of both c1 and c2 registers are modified. At the exit c1 is
4998 // guaranteed to contain halfword with low and high bytes equal to
4999 // initial contents of c1 and c2 respectively.
5000 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5001 Register c1,
5002 Register c2,
5003 Register scratch1,
5004 Register scratch2,
5005 Register scratch3,
5006 Register scratch4,
5007 Register scratch5,
5008 Label* not_found);
5009
5010 // Generate string hash.
5011 static void GenerateHashInit(MacroAssembler* masm,
5012 Register hash,
5013 Register character);
5014
5015 static void GenerateHashAddCharacter(MacroAssembler* masm,
5016 Register hash,
5017 Register character);
5018
5019 static void GenerateHashGetHash(MacroAssembler* masm,
5020 Register hash);
5021
5022 private:
5023 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
5024};
5025
5026
5027void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5028 Register dest,
5029 Register src,
5030 Register count,
5031 Register scratch,
5032 bool ascii) {
5033 Label loop;
5034 Label done;
5035 // This loop just copies one character at a time, as it is only used for very
5036 // short strings.
5037 if (!ascii) {
5038 __ add(count, count, Operand(count), SetCC);
5039 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01005040 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005041 }
5042 __ b(eq, &done);
5043
5044 __ bind(&loop);
5045 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
5046 // Perform sub between load and dependent store to get the load time to
5047 // complete.
5048 __ sub(count, count, Operand(1), SetCC);
5049 __ strb(scratch, MemOperand(dest, 1, PostIndex));
5050 // last iteration.
5051 __ b(gt, &loop);
5052
5053 __ bind(&done);
5054}
5055
5056
5057enum CopyCharactersFlags {
5058 COPY_ASCII = 1,
5059 DEST_ALWAYS_ALIGNED = 2
5060};
5061
5062
5063void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
5064 Register dest,
5065 Register src,
5066 Register count,
5067 Register scratch1,
5068 Register scratch2,
5069 Register scratch3,
5070 Register scratch4,
5071 Register scratch5,
5072 int flags) {
5073 bool ascii = (flags & COPY_ASCII) != 0;
5074 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
5075
5076 if (dest_always_aligned && FLAG_debug_code) {
5077 // Check that destination is actually word aligned if the flag says
5078 // that it is.
5079 __ tst(dest, Operand(kPointerAlignmentMask));
5080 __ Check(eq, "Destination of copy not aligned.");
5081 }
5082
5083 const int kReadAlignment = 4;
5084 const int kReadAlignmentMask = kReadAlignment - 1;
5085 // Ensure that reading an entire aligned word containing the last character
5086 // of a string will not read outside the allocated area (because we pad up
5087 // to kObjectAlignment).
5088 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
5089 // Assumes word reads and writes are little endian.
5090 // Nothing to do for zero characters.
5091 Label done;
5092 if (!ascii) {
5093 __ add(count, count, Operand(count), SetCC);
5094 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01005095 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005096 }
5097 __ b(eq, &done);
5098
5099 // Assume that you cannot read (or write) unaligned.
5100 Label byte_loop;
5101 // Must copy at least eight bytes, otherwise just do it one byte at a time.
5102 __ cmp(count, Operand(8));
5103 __ add(count, dest, Operand(count));
5104 Register limit = count; // Read until src equals this.
5105 __ b(lt, &byte_loop);
5106
5107 if (!dest_always_aligned) {
5108 // Align dest by byte copying. Copies between zero and three bytes.
5109 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
5110 Label dest_aligned;
5111 __ b(eq, &dest_aligned);
5112 __ cmp(scratch4, Operand(2));
5113 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
5114 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
5115 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
5116 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5117 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
5118 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
5119 __ bind(&dest_aligned);
5120 }
5121
5122 Label simple_loop;
5123
5124 __ sub(scratch4, dest, Operand(src));
5125 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
5126 __ b(eq, &simple_loop);
5127 // Shift register is number of bits in a source word that
5128 // must be combined with bits in the next source word in order
5129 // to create a destination word.
5130
5131 // Complex loop for src/dst that are not aligned the same way.
5132 {
5133 Label loop;
5134 __ mov(scratch4, Operand(scratch4, LSL, 3));
5135 Register left_shift = scratch4;
5136 __ and_(src, src, Operand(~3)); // Round down to load previous word.
5137 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
5138 // Store the "shift" most significant bits of scratch in the least
5139 // signficant bits (i.e., shift down by (32-shift)).
5140 __ rsb(scratch2, left_shift, Operand(32));
5141 Register right_shift = scratch2;
5142 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
5143
5144 __ bind(&loop);
5145 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
5146 __ sub(scratch5, limit, Operand(dest));
5147 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
5148 __ str(scratch1, MemOperand(dest, 4, PostIndex));
5149 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
5150 // Loop if four or more bytes left to copy.
5151 // Compare to eight, because we did the subtract before increasing dst.
5152 __ sub(scratch5, scratch5, Operand(8), SetCC);
5153 __ b(ge, &loop);
5154 }
5155 // There is now between zero and three bytes left to copy (negative that
5156 // number is in scratch5), and between one and three bytes already read into
5157 // scratch1 (eight times that number in scratch4). We may have read past
5158 // the end of the string, but because objects are aligned, we have not read
5159 // past the end of the object.
5160 // Find the minimum of remaining characters to move and preloaded characters
5161 // and write those as bytes.
5162 __ add(scratch5, scratch5, Operand(4), SetCC);
5163 __ b(eq, &done);
5164 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
5165 // Move minimum of bytes read and bytes left to copy to scratch4.
5166 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
5167 // Between one and three (value in scratch5) characters already read into
5168 // scratch ready to write.
5169 __ cmp(scratch5, Operand(2));
5170 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5171 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
5172 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
5173 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
5174 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
5175 // Copy any remaining bytes.
5176 __ b(&byte_loop);
5177
5178 // Simple loop.
5179 // Copy words from src to dst, until less than four bytes left.
5180 // Both src and dest are word aligned.
5181 __ bind(&simple_loop);
5182 {
5183 Label loop;
5184 __ bind(&loop);
5185 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
5186 __ sub(scratch3, limit, Operand(dest));
5187 __ str(scratch1, MemOperand(dest, 4, PostIndex));
5188 // Compare to 8, not 4, because we do the substraction before increasing
5189 // dest.
5190 __ cmp(scratch3, Operand(8));
5191 __ b(ge, &loop);
5192 }
5193
5194 // Copy bytes from src to dst until dst hits limit.
5195 __ bind(&byte_loop);
5196 __ cmp(dest, Operand(limit));
5197 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
5198 __ b(ge, &done);
5199 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5200 __ b(&byte_loop);
5201
5202 __ bind(&done);
5203}
5204
5205
5206void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5207 Register c1,
5208 Register c2,
5209 Register scratch1,
5210 Register scratch2,
5211 Register scratch3,
5212 Register scratch4,
5213 Register scratch5,
5214 Label* not_found) {
5215 // Register scratch3 is the general scratch register in this function.
5216 Register scratch = scratch3;
5217
5218 // Make sure that both characters are not digits as such strings has a
5219 // different hash algorithm. Don't try to look for these in the symbol table.
5220 Label not_array_index;
5221 __ sub(scratch, c1, Operand(static_cast<int>('0')));
5222 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
5223 __ b(hi, &not_array_index);
5224 __ sub(scratch, c2, Operand(static_cast<int>('0')));
5225 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
5226
5227 // If check failed combine both characters into single halfword.
5228 // This is required by the contract of the method: code at the
5229 // not_found branch expects this combination in c1 register
5230 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
5231 __ b(ls, not_found);
5232
5233 __ bind(&not_array_index);
5234 // Calculate the two character string hash.
5235 Register hash = scratch1;
5236 StringHelper::GenerateHashInit(masm, hash, c1);
5237 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
5238 StringHelper::GenerateHashGetHash(masm, hash);
5239
5240 // Collect the two characters in a register.
5241 Register chars = c1;
5242 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
5243
5244 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5245 // hash: hash of two character string.
5246
5247 // Load symbol table
5248 // Load address of first element of the symbol table.
5249 Register symbol_table = c2;
5250 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
5251
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005252 Register undefined = scratch4;
5253 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
5254
5255 // Calculate capacity mask from the symbol table capacity.
5256 Register mask = scratch2;
5257 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
5258 __ mov(mask, Operand(mask, ASR, 1));
5259 __ sub(mask, mask, Operand(1));
5260
5261 // Calculate untagged address of the first element of the symbol table.
5262 Register first_symbol_table_element = symbol_table;
5263 __ add(first_symbol_table_element, symbol_table,
5264 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
5265
5266 // Registers
5267 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5268 // hash: hash of two character string
5269 // mask: capacity mask
5270 // first_symbol_table_element: address of the first element of
5271 // the symbol table
Steve Block44f0eee2011-05-26 01:26:41 +01005272 // undefined: the undefined object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005273 // scratch: -
5274
5275 // Perform a number of probes in the symbol table.
5276 static const int kProbes = 4;
5277 Label found_in_symbol_table;
5278 Label next_probe[kProbes];
5279 for (int i = 0; i < kProbes; i++) {
5280 Register candidate = scratch5; // Scratch register contains candidate.
5281
5282 // Calculate entry in symbol table.
5283 if (i > 0) {
5284 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
5285 } else {
5286 __ mov(candidate, hash);
5287 }
5288
5289 __ and_(candidate, candidate, Operand(mask));
5290
5291 // Load the entry from the symble table.
5292 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5293 __ ldr(candidate,
5294 MemOperand(first_symbol_table_element,
5295 candidate,
5296 LSL,
5297 kPointerSizeLog2));
5298
5299 // If entry is undefined no string with this hash can be found.
Steve Block44f0eee2011-05-26 01:26:41 +01005300 Label is_string;
5301 __ CompareObjectType(candidate, scratch, scratch, ODDBALL_TYPE);
5302 __ b(ne, &is_string);
5303
5304 __ cmp(undefined, candidate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005305 __ b(eq, not_found);
Steve Block44f0eee2011-05-26 01:26:41 +01005306 // Must be null (deleted entry).
5307 if (FLAG_debug_code) {
5308 __ LoadRoot(ip, Heap::kNullValueRootIndex);
5309 __ cmp(ip, candidate);
5310 __ Assert(eq, "oddball in symbol table is not undefined or null");
5311 }
5312 __ jmp(&next_probe[i]);
5313
5314 __ bind(&is_string);
5315
5316 // Check that the candidate is a non-external ASCII string. The instance
5317 // type is still in the scratch register from the CompareObjectType
5318 // operation.
5319 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, &next_probe[i]);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005320
5321 // If length is not 2 the string is not a candidate.
5322 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
5323 __ cmp(scratch, Operand(Smi::FromInt(2)));
5324 __ b(ne, &next_probe[i]);
5325
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005326 // Check if the two characters match.
5327 // Assumes that word load is little endian.
5328 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
5329 __ cmp(chars, scratch);
5330 __ b(eq, &found_in_symbol_table);
5331 __ bind(&next_probe[i]);
5332 }
5333
5334 // No matching 2 character string found by probing.
5335 __ jmp(not_found);
5336
5337 // Scratch register contains result when we fall through to here.
5338 Register result = scratch;
5339 __ bind(&found_in_symbol_table);
5340 __ Move(r0, result);
5341}
5342
5343
5344void StringHelper::GenerateHashInit(MacroAssembler* masm,
5345 Register hash,
5346 Register character) {
5347 // hash = character + (character << 10);
5348 __ add(hash, character, Operand(character, LSL, 10));
5349 // hash ^= hash >> 6;
5350 __ eor(hash, hash, Operand(hash, ASR, 6));
5351}
5352
5353
5354void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5355 Register hash,
5356 Register character) {
5357 // hash += character;
5358 __ add(hash, hash, Operand(character));
5359 // hash += hash << 10;
5360 __ add(hash, hash, Operand(hash, LSL, 10));
5361 // hash ^= hash >> 6;
5362 __ eor(hash, hash, Operand(hash, ASR, 6));
5363}
5364
5365
5366void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5367 Register hash) {
5368 // hash += hash << 3;
5369 __ add(hash, hash, Operand(hash, LSL, 3));
5370 // hash ^= hash >> 11;
5371 __ eor(hash, hash, Operand(hash, ASR, 11));
5372 // hash += hash << 15;
5373 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
5374
5375 // if (hash == 0) hash = 27;
Steve Block1e0659c2011-05-24 12:43:12 +01005376 __ mov(hash, Operand(27), LeaveCC, ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005377}
5378
5379
5380void SubStringStub::Generate(MacroAssembler* masm) {
5381 Label runtime;
5382
5383 // Stack frame on entry.
5384 // lr: return address
5385 // sp[0]: to
5386 // sp[4]: from
5387 // sp[8]: string
5388
5389 // This stub is called from the native-call %_SubString(...), so
5390 // nothing can be assumed about the arguments. It is tested that:
5391 // "string" is a sequential string,
5392 // both "from" and "to" are smis, and
5393 // 0 <= from <= to <= string.length.
5394 // If any of these assumptions fail, we call the runtime system.
5395
5396 static const int kToOffset = 0 * kPointerSize;
5397 static const int kFromOffset = 1 * kPointerSize;
5398 static const int kStringOffset = 2 * kPointerSize;
5399
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005400 // Check bounds and smi-ness.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005401 Register to = r6;
5402 Register from = r7;
5403 __ Ldrd(to, from, MemOperand(sp, kToOffset));
5404 STATIC_ASSERT(kFromOffset == kToOffset + 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005405 STATIC_ASSERT(kSmiTag == 0);
5406 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5407 // I.e., arithmetic shift right by one un-smi-tags.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005408 __ mov(r2, Operand(to, ASR, 1), SetCC);
5409 __ mov(r3, Operand(from, ASR, 1), SetCC, cc);
5410 // If either to or from had the smi tag bit set, then carry is set now.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005411 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
5412 __ b(mi, &runtime); // From is negative.
5413
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005414 // Both to and from are smis.
5415
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005416 __ sub(r2, r2, Operand(r3), SetCC);
5417 __ b(mi, &runtime); // Fail if from > to.
5418 // Special handling of sub-strings of length 1 and 2. One character strings
5419 // are handled in the runtime system (looked up in the single character
5420 // cache). Two character strings are looked for in the symbol cache.
5421 __ cmp(r2, Operand(2));
5422 __ b(lt, &runtime);
5423
5424 // r2: length
5425 // r3: from index (untaged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005426 // r6 (a.k.a. to): to (smi)
5427 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005428
5429 // Make sure first argument is a sequential (or flat) string.
5430 __ ldr(r5, MemOperand(sp, kStringOffset));
5431 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005432 __ JumpIfSmi(r5, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005433 Condition is_string = masm->IsObjectStringType(r5, r1);
5434 __ b(NegateCondition(is_string), &runtime);
5435
5436 // r1: instance type
5437 // r2: length
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005438 // r3: from index (untagged smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005439 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005440 // r6 (a.k.a. to): to (smi)
5441 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005442 Label seq_string;
5443 __ and_(r4, r1, Operand(kStringRepresentationMask));
5444 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
5445 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
5446 __ cmp(r4, Operand(kConsStringTag));
5447 __ b(gt, &runtime); // External strings go to runtime.
5448 __ b(lt, &seq_string); // Sequential strings are handled directly.
5449
5450 // Cons string. Try to recurse (once) on the first substring.
5451 // (This adds a little more generality than necessary to handle flattened
5452 // cons strings, but not much).
5453 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
5454 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
5455 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5456 __ tst(r1, Operand(kStringRepresentationMask));
5457 STATIC_ASSERT(kSeqStringTag == 0);
5458 __ b(ne, &runtime); // Cons and External strings go to runtime.
5459
5460 // Definitly a sequential string.
5461 __ bind(&seq_string);
5462
5463 // r1: instance type.
5464 // r2: length
5465 // r3: from index (untaged smi)
5466 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005467 // r6 (a.k.a. to): to (smi)
5468 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005469 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005470 __ cmp(r4, Operand(to));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005471 __ b(lt, &runtime); // Fail if to > length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005472 to = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005473
5474 // r1: instance type.
5475 // r2: result string length.
5476 // r3: from index (untaged smi)
5477 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005478 // r7 (a.k.a. from): from offset (smi)
Steve Block44f0eee2011-05-26 01:26:41 +01005479 // Check for flat ASCII string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005480 Label non_ascii_flat;
5481 __ tst(r1, Operand(kStringEncodingMask));
5482 STATIC_ASSERT(kTwoByteStringTag == 0);
5483 __ b(eq, &non_ascii_flat);
5484
5485 Label result_longer_than_two;
5486 __ cmp(r2, Operand(2));
5487 __ b(gt, &result_longer_than_two);
5488
5489 // Sub string of length 2 requested.
5490 // Get the two characters forming the sub string.
5491 __ add(r5, r5, Operand(r3));
5492 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
5493 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
5494
5495 // Try to lookup two character string in symbol table.
5496 Label make_two_character_string;
5497 StringHelper::GenerateTwoCharacterSymbolTableProbe(
5498 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01005499 Counters* counters = masm->isolate()->counters();
5500 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005501 __ add(sp, sp, Operand(3 * kPointerSize));
5502 __ Ret();
5503
5504 // r2: result string length.
5505 // r3: two characters combined into halfword in little endian byte order.
5506 __ bind(&make_two_character_string);
5507 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
5508 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005509 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005510 __ add(sp, sp, Operand(3 * kPointerSize));
5511 __ Ret();
5512
5513 __ bind(&result_longer_than_two);
5514
5515 // Allocate the result.
5516 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
5517
5518 // r0: result string.
5519 // r2: result string length.
5520 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005521 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005522 // Locate first character of result.
5523 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5524 // Locate 'from' character of string.
5525 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005526 __ add(r5, r5, Operand(from, ASR, 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005527
5528 // r0: result string.
5529 // r1: first character of result string.
5530 // r2: result string length.
5531 // r5: first character of sub string to copy.
5532 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
5533 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
5534 COPY_ASCII | DEST_ALWAYS_ALIGNED);
Steve Block44f0eee2011-05-26 01:26:41 +01005535 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005536 __ add(sp, sp, Operand(3 * kPointerSize));
5537 __ Ret();
5538
5539 __ bind(&non_ascii_flat);
5540 // r2: result string length.
5541 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005542 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005543 // Check for flat two byte string.
5544
5545 // Allocate the result.
5546 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
5547
5548 // r0: result string.
5549 // r2: result string length.
5550 // r5: string.
5551 // Locate first character of result.
5552 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5553 // Locate 'from' character of string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005554 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005555 // As "from" is a smi it is 2 times the value which matches the size of a two
5556 // byte character.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005557 __ add(r5, r5, Operand(from));
5558 from = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005559
5560 // r0: result string.
5561 // r1: first character of result.
5562 // r2: result length.
5563 // r5: first character of string to copy.
5564 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005565 StringHelper::GenerateCopyCharactersLong(
5566 masm, r1, r5, r2, r3, r4, r6, r7, r9, DEST_ALWAYS_ALIGNED);
Steve Block44f0eee2011-05-26 01:26:41 +01005567 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005568 __ add(sp, sp, Operand(3 * kPointerSize));
5569 __ Ret();
5570
5571 // Just jump to runtime to create the sub string.
5572 __ bind(&runtime);
5573 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5574}
5575
5576
Ben Murdoch257744e2011-11-30 15:57:28 +00005577void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5578 Register left,
5579 Register right,
5580 Register scratch1,
5581 Register scratch2,
5582 Register scratch3) {
5583 Register length = scratch1;
5584
5585 // Compare lengths.
5586 Label strings_not_equal, check_zero_length;
5587 __ ldr(length, FieldMemOperand(left, String::kLengthOffset));
5588 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
5589 __ cmp(length, scratch2);
5590 __ b(eq, &check_zero_length);
5591 __ bind(&strings_not_equal);
5592 __ mov(r0, Operand(Smi::FromInt(NOT_EQUAL)));
5593 __ Ret();
5594
5595 // Check if the length is zero.
5596 Label compare_chars;
5597 __ bind(&check_zero_length);
5598 STATIC_ASSERT(kSmiTag == 0);
5599 __ tst(length, Operand(length));
5600 __ b(ne, &compare_chars);
5601 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
5602 __ Ret();
5603
5604 // Compare characters.
5605 __ bind(&compare_chars);
5606 GenerateAsciiCharsCompareLoop(masm,
5607 left, right, length, scratch2, scratch3,
5608 &strings_not_equal);
5609
5610 // Characters are equal.
5611 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
5612 __ Ret();
5613}
5614
5615
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005616void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5617 Register left,
5618 Register right,
5619 Register scratch1,
5620 Register scratch2,
5621 Register scratch3,
5622 Register scratch4) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005623 Label result_not_equal, compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005624 // Find minimum length and length difference.
5625 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
5626 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
5627 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
5628 Register length_delta = scratch3;
5629 __ mov(scratch1, scratch2, LeaveCC, gt);
5630 Register min_length = scratch1;
5631 STATIC_ASSERT(kSmiTag == 0);
5632 __ tst(min_length, Operand(min_length));
5633 __ b(eq, &compare_lengths);
5634
Ben Murdoch257744e2011-11-30 15:57:28 +00005635 // Compare loop.
5636 GenerateAsciiCharsCompareLoop(masm,
5637 left, right, min_length, scratch2, scratch4,
5638 &result_not_equal);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005639
Ben Murdoch257744e2011-11-30 15:57:28 +00005640 // Compare lengths - strings up to min-length are equal.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005641 __ bind(&compare_lengths);
5642 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00005643 // Use length_delta as result if it's zero.
5644 __ mov(r0, Operand(length_delta), SetCC);
5645 __ bind(&result_not_equal);
5646 // Conditionally update the result based either on length_delta or
5647 // the last comparion performed in the loop above.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005648 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
5649 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
5650 __ Ret();
5651}
5652
5653
Ben Murdoch257744e2011-11-30 15:57:28 +00005654void StringCompareStub::GenerateAsciiCharsCompareLoop(
5655 MacroAssembler* masm,
5656 Register left,
5657 Register right,
5658 Register length,
5659 Register scratch1,
5660 Register scratch2,
5661 Label* chars_not_equal) {
5662 // Change index to run from -length to -1 by adding length to string
5663 // start. This means that loop ends when index reaches zero, which
5664 // doesn't need an additional compare.
5665 __ SmiUntag(length);
5666 __ add(scratch1, length,
5667 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5668 __ add(left, left, Operand(scratch1));
5669 __ add(right, right, Operand(scratch1));
5670 __ rsb(length, length, Operand(0));
5671 Register index = length; // index = -length;
5672
5673 // Compare loop.
5674 Label loop;
5675 __ bind(&loop);
5676 __ ldrb(scratch1, MemOperand(left, index));
5677 __ ldrb(scratch2, MemOperand(right, index));
5678 __ cmp(scratch1, scratch2);
5679 __ b(ne, chars_not_equal);
5680 __ add(index, index, Operand(1), SetCC);
5681 __ b(ne, &loop);
5682}
5683
5684
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005685void StringCompareStub::Generate(MacroAssembler* masm) {
5686 Label runtime;
5687
Steve Block44f0eee2011-05-26 01:26:41 +01005688 Counters* counters = masm->isolate()->counters();
5689
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005690 // Stack frame on entry.
5691 // sp[0]: right string
5692 // sp[4]: left string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005693 __ Ldrd(r0 , r1, MemOperand(sp)); // Load right in r0, left in r1.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005694
5695 Label not_same;
5696 __ cmp(r0, r1);
5697 __ b(ne, &not_same);
5698 STATIC_ASSERT(EQUAL == 0);
5699 STATIC_ASSERT(kSmiTag == 0);
5700 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01005701 __ IncrementCounter(counters->string_compare_native(), 1, r1, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005702 __ add(sp, sp, Operand(2 * kPointerSize));
5703 __ Ret();
5704
5705 __ bind(&not_same);
5706
Steve Block44f0eee2011-05-26 01:26:41 +01005707 // Check that both objects are sequential ASCII strings.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005708 __ JumpIfNotBothSequentialAsciiStrings(r1, r0, r2, r3, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005709
Steve Block44f0eee2011-05-26 01:26:41 +01005710 // Compare flat ASCII strings natively. Remove arguments from stack first.
5711 __ IncrementCounter(counters->string_compare_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005712 __ add(sp, sp, Operand(2 * kPointerSize));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005713 GenerateCompareFlatAsciiStrings(masm, r1, r0, r2, r3, r4, r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005714
5715 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5716 // tagged as a small integer.
5717 __ bind(&runtime);
5718 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5719}
5720
5721
5722void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005723 Label string_add_runtime, call_builtin;
5724 Builtins::JavaScript builtin_id = Builtins::ADD;
5725
Steve Block44f0eee2011-05-26 01:26:41 +01005726 Counters* counters = masm->isolate()->counters();
5727
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005728 // Stack on entry:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005729 // sp[0]: second argument (right).
5730 // sp[4]: first argument (left).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005731
5732 // Load the two arguments.
5733 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
5734 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
5735
5736 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005737 if (flags_ == NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005738 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
5739 // Load instance types.
5740 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5741 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5742 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5743 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5744 STATIC_ASSERT(kStringTag == 0);
5745 // If either is not a string, go to runtime.
5746 __ tst(r4, Operand(kIsNotStringMask));
5747 __ tst(r5, Operand(kIsNotStringMask), eq);
5748 __ b(ne, &string_add_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005749 } else {
5750 // Here at least one of the arguments is definitely a string.
5751 // We convert the one that is not known to be a string.
5752 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5753 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5754 GenerateConvertArgument(
5755 masm, 1 * kPointerSize, r0, r2, r3, r4, r5, &call_builtin);
5756 builtin_id = Builtins::STRING_ADD_RIGHT;
5757 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5758 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5759 GenerateConvertArgument(
5760 masm, 0 * kPointerSize, r1, r2, r3, r4, r5, &call_builtin);
5761 builtin_id = Builtins::STRING_ADD_LEFT;
5762 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005763 }
5764
5765 // Both arguments are strings.
5766 // r0: first string
5767 // r1: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005768 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5769 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005770 {
5771 Label strings_not_empty;
5772 // Check if either of the strings are empty. In that case return the other.
5773 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
5774 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
5775 STATIC_ASSERT(kSmiTag == 0);
5776 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
5777 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
5778 STATIC_ASSERT(kSmiTag == 0);
5779 // Else test if second string is empty.
5780 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
5781 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
5782
Steve Block44f0eee2011-05-26 01:26:41 +01005783 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005784 __ add(sp, sp, Operand(2 * kPointerSize));
5785 __ Ret();
5786
5787 __ bind(&strings_not_empty);
5788 }
5789
5790 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
5791 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
5792 // Both strings are non-empty.
5793 // r0: first string
5794 // r1: second string
5795 // r2: length of first string
5796 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005797 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5798 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005799 // Look at the length of the result of adding the two strings.
5800 Label string_add_flat_result, longer_than_two;
5801 // Adding two lengths can't overflow.
5802 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
5803 __ add(r6, r2, Operand(r3));
Steve Block44f0eee2011-05-26 01:26:41 +01005804 // Use the symbol table when adding two one character strings, as it
5805 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005806 __ cmp(r6, Operand(2));
5807 __ b(ne, &longer_than_two);
5808
Steve Block44f0eee2011-05-26 01:26:41 +01005809 // Check that both strings are non-external ASCII strings.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005810 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005811 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5812 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5813 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5814 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5815 }
5816 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
5817 &string_add_runtime);
5818
5819 // Get the two characters forming the sub string.
5820 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
5821 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
5822
5823 // Try to lookup two character string in symbol table. If it is not found
5824 // just allocate a new one.
5825 Label make_two_character_string;
5826 StringHelper::GenerateTwoCharacterSymbolTableProbe(
5827 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01005828 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005829 __ add(sp, sp, Operand(2 * kPointerSize));
5830 __ Ret();
5831
5832 __ bind(&make_two_character_string);
5833 // Resulting string has length 2 and first chars of two strings
5834 // are combined into single halfword in r2 register.
5835 // So we can fill resulting string without two loops by a single
5836 // halfword store instruction (which assumes that processor is
5837 // in a little endian mode)
5838 __ mov(r6, Operand(2));
5839 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
5840 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005841 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005842 __ add(sp, sp, Operand(2 * kPointerSize));
5843 __ Ret();
5844
5845 __ bind(&longer_than_two);
5846 // Check if resulting string will be flat.
5847 __ cmp(r6, Operand(String::kMinNonFlatLength));
5848 __ b(lt, &string_add_flat_result);
5849 // Handle exceptionally long strings in the runtime system.
5850 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
5851 ASSERT(IsPowerOf2(String::kMaxLength + 1));
5852 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
5853 __ cmp(r6, Operand(String::kMaxLength + 1));
5854 __ b(hs, &string_add_runtime);
5855
5856 // If result is not supposed to be flat, allocate a cons string object.
Steve Block44f0eee2011-05-26 01:26:41 +01005857 // If both strings are ASCII the result is an ASCII cons string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005858 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005859 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5860 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5861 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5862 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5863 }
5864 Label non_ascii, allocated, ascii_data;
5865 STATIC_ASSERT(kTwoByteStringTag == 0);
5866 __ tst(r4, Operand(kStringEncodingMask));
5867 __ tst(r5, Operand(kStringEncodingMask), ne);
5868 __ b(eq, &non_ascii);
5869
5870 // Allocate an ASCII cons string.
5871 __ bind(&ascii_data);
5872 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
5873 __ bind(&allocated);
5874 // Fill the fields of the cons string.
5875 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
5876 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
5877 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01005878 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005879 __ add(sp, sp, Operand(2 * kPointerSize));
5880 __ Ret();
5881
5882 __ bind(&non_ascii);
5883 // At least one of the strings is two-byte. Check whether it happens
Steve Block44f0eee2011-05-26 01:26:41 +01005884 // to contain only ASCII characters.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005885 // r4: first instance type.
5886 // r5: second instance type.
5887 __ tst(r4, Operand(kAsciiDataHintMask));
5888 __ tst(r5, Operand(kAsciiDataHintMask), ne);
5889 __ b(ne, &ascii_data);
5890 __ eor(r4, r4, Operand(r5));
5891 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5892 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
5893 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
5894 __ b(eq, &ascii_data);
5895
5896 // Allocate a two byte cons string.
5897 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
5898 __ jmp(&allocated);
5899
5900 // Handle creating a flat result. First check that both strings are
5901 // sequential and that they have the same encoding.
5902 // r0: first string
5903 // r1: second string
5904 // r2: length of first string
5905 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005906 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5907 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005908 // r6: sum of lengths.
5909 __ bind(&string_add_flat_result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005910 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005911 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5912 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5913 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5914 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5915 }
5916 // Check that both strings are sequential.
5917 STATIC_ASSERT(kSeqStringTag == 0);
5918 __ tst(r4, Operand(kStringRepresentationMask));
5919 __ tst(r5, Operand(kStringRepresentationMask), eq);
5920 __ b(ne, &string_add_runtime);
5921 // Now check if both strings have the same encoding (ASCII/Two-byte).
5922 // r0: first string.
5923 // r1: second string.
5924 // r2: length of first string.
5925 // r3: length of second string.
5926 // r6: sum of lengths..
5927 Label non_ascii_string_add_flat_result;
5928 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
5929 __ eor(r7, r4, Operand(r5));
5930 __ tst(r7, Operand(kStringEncodingMask));
5931 __ b(ne, &string_add_runtime);
5932 // And see if it's ASCII or two-byte.
5933 __ tst(r4, Operand(kStringEncodingMask));
5934 __ b(eq, &non_ascii_string_add_flat_result);
5935
5936 // Both strings are sequential ASCII strings. We also know that they are
5937 // short (since the sum of the lengths is less than kMinNonFlatLength).
5938 // r6: length of resulting flat string
5939 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
5940 // Locate first character of result.
5941 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5942 // Locate first character of first argument.
5943 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5944 // r0: first character of first string.
5945 // r1: second string.
5946 // r2: length of first string.
5947 // r3: length of second string.
5948 // r6: first character of result.
5949 // r7: result string.
5950 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
5951
5952 // Load second argument and locate first character.
5953 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5954 // r1: first character of second string.
5955 // r3: length of second string.
5956 // r6: next character of result.
5957 // r7: result string.
5958 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
5959 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01005960 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005961 __ add(sp, sp, Operand(2 * kPointerSize));
5962 __ Ret();
5963
5964 __ bind(&non_ascii_string_add_flat_result);
5965 // Both strings are sequential two byte strings.
5966 // r0: first string.
5967 // r1: second string.
5968 // r2: length of first string.
5969 // r3: length of second string.
5970 // r6: sum of length of strings.
5971 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
5972 // r0: first string.
5973 // r1: second string.
5974 // r2: length of first string.
5975 // r3: length of second string.
5976 // r7: result string.
5977
5978 // Locate first character of result.
5979 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5980 // Locate first character of first argument.
5981 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5982
5983 // r0: first character of first string.
5984 // r1: second string.
5985 // r2: length of first string.
5986 // r3: length of second string.
5987 // r6: first character of result.
5988 // r7: result string.
5989 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
5990
5991 // Locate first character of second argument.
5992 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5993
5994 // r1: first character of second string.
5995 // r3: length of second string.
5996 // r6: next character of result (after copy of first string).
5997 // r7: result string.
5998 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
5999
6000 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01006001 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006002 __ add(sp, sp, Operand(2 * kPointerSize));
6003 __ Ret();
6004
6005 // Just jump to runtime to add the two strings.
6006 __ bind(&string_add_runtime);
6007 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006008
6009 if (call_builtin.is_linked()) {
6010 __ bind(&call_builtin);
Ben Murdoch257744e2011-11-30 15:57:28 +00006011 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006012 }
6013}
6014
6015
6016void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
6017 int stack_offset,
6018 Register arg,
6019 Register scratch1,
6020 Register scratch2,
6021 Register scratch3,
6022 Register scratch4,
6023 Label* slow) {
6024 // First check if the argument is already a string.
6025 Label not_string, done;
6026 __ JumpIfSmi(arg, &not_string);
6027 __ CompareObjectType(arg, scratch1, scratch1, FIRST_NONSTRING_TYPE);
6028 __ b(lt, &done);
6029
6030 // Check the number to string cache.
6031 Label not_cached;
6032 __ bind(&not_string);
6033 // Puts the cached result into scratch1.
6034 NumberToStringStub::GenerateLookupNumberStringCache(masm,
6035 arg,
6036 scratch1,
6037 scratch2,
6038 scratch3,
6039 scratch4,
6040 false,
6041 &not_cached);
6042 __ mov(arg, scratch1);
6043 __ str(arg, MemOperand(sp, stack_offset));
6044 __ jmp(&done);
6045
6046 // Check if the argument is a safe string wrapper.
6047 __ bind(&not_cached);
6048 __ JumpIfSmi(arg, slow);
6049 __ CompareObjectType(
6050 arg, scratch1, scratch2, JS_VALUE_TYPE); // map -> scratch1.
6051 __ b(ne, slow);
6052 __ ldrb(scratch2, FieldMemOperand(scratch1, Map::kBitField2Offset));
6053 __ and_(scratch2,
6054 scratch2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
6055 __ cmp(scratch2,
6056 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
6057 __ b(ne, slow);
6058 __ ldr(arg, FieldMemOperand(arg, JSValue::kValueOffset));
6059 __ str(arg, MemOperand(sp, stack_offset));
6060
6061 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006062}
6063
6064
Ben Murdochb0fe1622011-05-05 13:52:32 +01006065void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
6066 ASSERT(state_ == CompareIC::SMIS);
6067 Label miss;
6068 __ orr(r2, r1, r0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006069 __ JumpIfNotSmi(r2, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006070
6071 if (GetCondition() == eq) {
6072 // For equality we do not care about the sign of the result.
6073 __ sub(r0, r0, r1, SetCC);
6074 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01006075 // Untag before subtracting to avoid handling overflow.
6076 __ SmiUntag(r1);
6077 __ sub(r0, r1, SmiUntagOperand(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01006078 }
6079 __ Ret();
6080
6081 __ bind(&miss);
6082 GenerateMiss(masm);
6083}
6084
6085
6086void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
6087 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
6088
6089 Label generic_stub;
6090 Label unordered;
6091 Label miss;
6092 __ and_(r2, r1, Operand(r0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006093 __ JumpIfSmi(r2, &generic_stub);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006094
6095 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
6096 __ b(ne, &miss);
6097 __ CompareObjectType(r1, r2, r2, HEAP_NUMBER_TYPE);
6098 __ b(ne, &miss);
6099
6100 // Inlining the double comparison and falling back to the general compare
6101 // stub if NaN is involved or VFP3 is unsupported.
Ben Murdoch8b112d22011-06-08 16:22:53 +01006102 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006103 CpuFeatures::Scope scope(VFP3);
6104
6105 // Load left and right operand
6106 __ sub(r2, r1, Operand(kHeapObjectTag));
6107 __ vldr(d0, r2, HeapNumber::kValueOffset);
6108 __ sub(r2, r0, Operand(kHeapObjectTag));
6109 __ vldr(d1, r2, HeapNumber::kValueOffset);
6110
6111 // Compare operands
Ben Murdochb8e0da22011-05-16 14:20:40 +01006112 __ VFPCompareAndSetFlags(d0, d1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006113
6114 // Don't base result on status bits when a NaN is involved.
6115 __ b(vs, &unordered);
6116
6117 // Return a result of -1, 0, or 1, based on status bits.
6118 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
6119 __ mov(r0, Operand(LESS), LeaveCC, lt);
6120 __ mov(r0, Operand(GREATER), LeaveCC, gt);
6121 __ Ret();
6122
6123 __ bind(&unordered);
6124 }
6125
6126 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS, r1, r0);
6127 __ bind(&generic_stub);
6128 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
6129
6130 __ bind(&miss);
6131 GenerateMiss(masm);
6132}
6133
6134
Ben Murdoch257744e2011-11-30 15:57:28 +00006135void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
6136 ASSERT(state_ == CompareIC::SYMBOLS);
6137 Label miss;
6138
6139 // Registers containing left and right operands respectively.
6140 Register left = r1;
6141 Register right = r0;
6142 Register tmp1 = r2;
6143 Register tmp2 = r3;
6144
6145 // Check that both operands are heap objects.
6146 __ JumpIfEitherSmi(left, right, &miss);
6147
6148 // Check that both operands are symbols.
6149 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
6150 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
6151 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
6152 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
6153 STATIC_ASSERT(kSymbolTag != 0);
6154 __ and_(tmp1, tmp1, Operand(tmp2));
6155 __ tst(tmp1, Operand(kIsSymbolMask));
6156 __ b(eq, &miss);
6157
6158 // Symbols are compared by identity.
6159 __ cmp(left, right);
6160 // Make sure r0 is non-zero. At this point input operands are
6161 // guaranteed to be non-zero.
6162 ASSERT(right.is(r0));
6163 STATIC_ASSERT(EQUAL == 0);
6164 STATIC_ASSERT(kSmiTag == 0);
6165 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
6166 __ Ret();
6167
6168 __ bind(&miss);
6169 GenerateMiss(masm);
6170}
6171
6172
6173void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
6174 ASSERT(state_ == CompareIC::STRINGS);
6175 Label miss;
6176
6177 // Registers containing left and right operands respectively.
6178 Register left = r1;
6179 Register right = r0;
6180 Register tmp1 = r2;
6181 Register tmp2 = r3;
6182 Register tmp3 = r4;
6183 Register tmp4 = r5;
6184
6185 // Check that both operands are heap objects.
6186 __ JumpIfEitherSmi(left, right, &miss);
6187
6188 // Check that both operands are strings. This leaves the instance
6189 // types loaded in tmp1 and tmp2.
6190 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
6191 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
6192 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
6193 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
6194 STATIC_ASSERT(kNotStringTag != 0);
6195 __ orr(tmp3, tmp1, tmp2);
6196 __ tst(tmp3, Operand(kIsNotStringMask));
6197 __ b(ne, &miss);
6198
6199 // Fast check for identical strings.
6200 __ cmp(left, right);
6201 STATIC_ASSERT(EQUAL == 0);
6202 STATIC_ASSERT(kSmiTag == 0);
6203 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
6204 __ Ret(eq);
6205
6206 // Handle not identical strings.
6207
6208 // Check that both strings are symbols. If they are, we're done
6209 // because we already know they are not identical.
6210 ASSERT(GetCondition() == eq);
6211 STATIC_ASSERT(kSymbolTag != 0);
6212 __ and_(tmp3, tmp1, Operand(tmp2));
6213 __ tst(tmp3, Operand(kIsSymbolMask));
6214 // Make sure r0 is non-zero. At this point input operands are
6215 // guaranteed to be non-zero.
6216 ASSERT(right.is(r0));
6217 __ Ret(ne);
6218
6219 // Check that both strings are sequential ASCII.
6220 Label runtime;
6221 __ JumpIfBothInstanceTypesAreNotSequentialAscii(tmp1, tmp2, tmp3, tmp4,
6222 &runtime);
6223
6224 // Compare flat ASCII strings. Returns when done.
6225 StringCompareStub::GenerateFlatAsciiStringEquals(
6226 masm, left, right, tmp1, tmp2, tmp3);
6227
6228 // Handle more complex cases in runtime.
6229 __ bind(&runtime);
6230 __ Push(left, right);
6231 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
6232
6233 __ bind(&miss);
6234 GenerateMiss(masm);
6235}
6236
6237
Ben Murdochb0fe1622011-05-05 13:52:32 +01006238void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6239 ASSERT(state_ == CompareIC::OBJECTS);
6240 Label miss;
6241 __ and_(r2, r1, Operand(r0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006242 __ JumpIfSmi(r2, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006243
6244 __ CompareObjectType(r0, r2, r2, JS_OBJECT_TYPE);
6245 __ b(ne, &miss);
6246 __ CompareObjectType(r1, r2, r2, JS_OBJECT_TYPE);
6247 __ b(ne, &miss);
6248
6249 ASSERT(GetCondition() == eq);
6250 __ sub(r0, r0, Operand(r1));
6251 __ Ret();
6252
6253 __ bind(&miss);
6254 GenerateMiss(masm);
6255}
6256
6257
6258void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6259 __ Push(r1, r0);
6260 __ push(lr);
6261
6262 // Call the runtime system in a fresh internal frame.
Steve Block44f0eee2011-05-26 01:26:41 +01006263 ExternalReference miss =
6264 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01006265 __ EnterInternalFrame();
6266 __ Push(r1, r0);
6267 __ mov(ip, Operand(Smi::FromInt(op_)));
6268 __ push(ip);
6269 __ CallExternalReference(miss, 3);
6270 __ LeaveInternalFrame();
6271 // Compute the entry point of the rewritten stub.
6272 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
6273 // Restore registers.
6274 __ pop(lr);
6275 __ pop(r0);
6276 __ pop(r1);
6277 __ Jump(r2);
6278}
6279
6280
Steve Block1e0659c2011-05-24 12:43:12 +01006281void DirectCEntryStub::Generate(MacroAssembler* masm) {
6282 __ ldr(pc, MemOperand(sp, 0));
6283}
6284
6285
6286void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006287 ExternalReference function) {
6288 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
6289 RelocInfo::CODE_TARGET));
6290 __ mov(r2, Operand(function));
6291 // Push return address (accessible to GC through exit frame pc).
6292 __ str(pc, MemOperand(sp, 0));
6293 __ Jump(r2); // Call the api function.
6294}
6295
6296
6297void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
6298 Register target) {
Steve Block1e0659c2011-05-24 12:43:12 +01006299 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
6300 RelocInfo::CODE_TARGET));
6301 // Push return address (accessible to GC through exit frame pc).
Steve Block1e0659c2011-05-24 12:43:12 +01006302 __ str(pc, MemOperand(sp, 0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006303 __ Jump(target); // Call the C++ function.
Steve Block1e0659c2011-05-24 12:43:12 +01006304}
6305
6306
Ben Murdoch257744e2011-11-30 15:57:28 +00006307MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
6308 MacroAssembler* masm,
6309 Label* miss,
6310 Label* done,
6311 Register receiver,
6312 Register properties,
6313 String* name,
6314 Register scratch0) {
6315 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6316 // not equal to the name and kProbes-th slot is not used (its name is the
6317 // undefined value), it guarantees the hash table doesn't contain the
6318 // property. It's true even if some slots represent deleted properties
6319 // (their names are the null value).
6320 for (int i = 0; i < kInlinedProbes; i++) {
6321 // scratch0 points to properties hash.
6322 // Compute the masked index: (hash + i + i * i) & mask.
6323 Register index = scratch0;
6324 // Capacity is smi 2^n.
6325 __ ldr(index, FieldMemOperand(properties, kCapacityOffset));
6326 __ sub(index, index, Operand(1));
6327 __ and_(index, index, Operand(
6328 Smi::FromInt(name->Hash() + StringDictionary::GetProbeOffset(i))));
6329
6330 // Scale the index by multiplying by the entry size.
6331 ASSERT(StringDictionary::kEntrySize == 3);
6332 __ add(index, index, Operand(index, LSL, 1)); // index *= 3.
6333
6334 Register entity_name = scratch0;
6335 // Having undefined at this place means the name is not contained.
6336 ASSERT_EQ(kSmiTagSize, 1);
6337 Register tmp = properties;
6338 __ add(tmp, properties, Operand(index, LSL, 1));
6339 __ ldr(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
6340
6341 ASSERT(!tmp.is(entity_name));
6342 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
6343 __ cmp(entity_name, tmp);
6344 __ b(eq, done);
6345
6346 if (i != kInlinedProbes - 1) {
6347 // Stop if found the property.
6348 __ cmp(entity_name, Operand(Handle<String>(name)));
6349 __ b(eq, miss);
6350
6351 // Check if the entry name is not a symbol.
6352 __ ldr(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
6353 __ ldrb(entity_name,
6354 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
6355 __ tst(entity_name, Operand(kIsSymbolMask));
6356 __ b(eq, miss);
6357
6358 // Restore the properties.
6359 __ ldr(properties,
6360 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
6361 }
6362 }
6363
6364 const int spill_mask =
6365 (lr.bit() | r6.bit() | r5.bit() | r4.bit() | r3.bit() |
6366 r2.bit() | r1.bit() | r0.bit());
6367
6368 __ stm(db_w, sp, spill_mask);
6369 __ ldr(r0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
6370 __ mov(r1, Operand(Handle<String>(name)));
6371 StringDictionaryLookupStub stub(NEGATIVE_LOOKUP);
6372 MaybeObject* result = masm->TryCallStub(&stub);
6373 if (result->IsFailure()) return result;
6374 __ tst(r0, Operand(r0));
6375 __ ldm(ia_w, sp, spill_mask);
6376
6377 __ b(eq, done);
6378 __ b(ne, miss);
6379 return result;
6380}
6381
6382
6383// Probe the string dictionary in the |elements| register. Jump to the
6384// |done| label if a property with the given name is found. Jump to
6385// the |miss| label otherwise.
6386// If lookup was successful |scratch2| will be equal to elements + 4 * index.
6387void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6388 Label* miss,
6389 Label* done,
6390 Register elements,
6391 Register name,
6392 Register scratch1,
6393 Register scratch2) {
6394 // Assert that name contains a string.
6395 if (FLAG_debug_code) __ AbortIfNotString(name);
6396
6397 // Compute the capacity mask.
6398 __ ldr(scratch1, FieldMemOperand(elements, kCapacityOffset));
6399 __ mov(scratch1, Operand(scratch1, ASR, kSmiTagSize)); // convert smi to int
6400 __ sub(scratch1, scratch1, Operand(1));
6401
6402 // Generate an unrolled loop that performs a few probes before
6403 // giving up. Measurements done on Gmail indicate that 2 probes
6404 // cover ~93% of loads from dictionaries.
6405 for (int i = 0; i < kInlinedProbes; i++) {
6406 // Compute the masked index: (hash + i + i * i) & mask.
6407 __ ldr(scratch2, FieldMemOperand(name, String::kHashFieldOffset));
6408 if (i > 0) {
6409 // Add the probe offset (i + i * i) left shifted to avoid right shifting
6410 // the hash in a separate instruction. The value hash + i + i * i is right
6411 // shifted in the following and instruction.
6412 ASSERT(StringDictionary::GetProbeOffset(i) <
6413 1 << (32 - String::kHashFieldOffset));
6414 __ add(scratch2, scratch2, Operand(
6415 StringDictionary::GetProbeOffset(i) << String::kHashShift));
6416 }
6417 __ and_(scratch2, scratch1, Operand(scratch2, LSR, String::kHashShift));
6418
6419 // Scale the index by multiplying by the element size.
6420 ASSERT(StringDictionary::kEntrySize == 3);
6421 // scratch2 = scratch2 * 3.
6422 __ add(scratch2, scratch2, Operand(scratch2, LSL, 1));
6423
6424 // Check if the key is identical to the name.
6425 __ add(scratch2, elements, Operand(scratch2, LSL, 2));
6426 __ ldr(ip, FieldMemOperand(scratch2, kElementsStartOffset));
6427 __ cmp(name, Operand(ip));
6428 __ b(eq, done);
6429 }
6430
6431 const int spill_mask =
6432 (lr.bit() | r6.bit() | r5.bit() | r4.bit() |
6433 r3.bit() | r2.bit() | r1.bit() | r0.bit()) &
6434 ~(scratch1.bit() | scratch2.bit());
6435
6436 __ stm(db_w, sp, spill_mask);
6437 __ Move(r0, elements);
6438 __ Move(r1, name);
6439 StringDictionaryLookupStub stub(POSITIVE_LOOKUP);
6440 __ CallStub(&stub);
6441 __ tst(r0, Operand(r0));
6442 __ mov(scratch2, Operand(r2));
6443 __ ldm(ia_w, sp, spill_mask);
6444
6445 __ b(ne, done);
6446 __ b(eq, miss);
6447}
6448
6449
6450void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
6451 // Registers:
6452 // result: StringDictionary to probe
6453 // r1: key
6454 // : StringDictionary to probe.
6455 // index_: will hold an index of entry if lookup is successful.
6456 // might alias with result_.
6457 // Returns:
6458 // result_ is zero if lookup failed, non zero otherwise.
6459
6460 Register result = r0;
6461 Register dictionary = r0;
6462 Register key = r1;
6463 Register index = r2;
6464 Register mask = r3;
6465 Register hash = r4;
6466 Register undefined = r5;
6467 Register entry_key = r6;
6468
6469 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6470
6471 __ ldr(mask, FieldMemOperand(dictionary, kCapacityOffset));
6472 __ mov(mask, Operand(mask, ASR, kSmiTagSize));
6473 __ sub(mask, mask, Operand(1));
6474
6475 __ ldr(hash, FieldMemOperand(key, String::kHashFieldOffset));
6476
6477 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
6478
6479 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6480 // Compute the masked index: (hash + i + i * i) & mask.
6481 // Capacity is smi 2^n.
6482 if (i > 0) {
6483 // Add the probe offset (i + i * i) left shifted to avoid right shifting
6484 // the hash in a separate instruction. The value hash + i + i * i is right
6485 // shifted in the following and instruction.
6486 ASSERT(StringDictionary::GetProbeOffset(i) <
6487 1 << (32 - String::kHashFieldOffset));
6488 __ add(index, hash, Operand(
6489 StringDictionary::GetProbeOffset(i) << String::kHashShift));
6490 } else {
6491 __ mov(index, Operand(hash));
6492 }
6493 __ and_(index, mask, Operand(index, LSR, String::kHashShift));
6494
6495 // Scale the index by multiplying by the entry size.
6496 ASSERT(StringDictionary::kEntrySize == 3);
6497 __ add(index, index, Operand(index, LSL, 1)); // index *= 3.
6498
6499 ASSERT_EQ(kSmiTagSize, 1);
6500 __ add(index, dictionary, Operand(index, LSL, 2));
6501 __ ldr(entry_key, FieldMemOperand(index, kElementsStartOffset));
6502
6503 // Having undefined at this place means the name is not contained.
6504 __ cmp(entry_key, Operand(undefined));
6505 __ b(eq, &not_in_dictionary);
6506
6507 // Stop if found the property.
6508 __ cmp(entry_key, Operand(key));
6509 __ b(eq, &in_dictionary);
6510
6511 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6512 // Check if the entry name is not a symbol.
6513 __ ldr(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
6514 __ ldrb(entry_key,
6515 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
6516 __ tst(entry_key, Operand(kIsSymbolMask));
6517 __ b(eq, &maybe_in_dictionary);
6518 }
6519 }
6520
6521 __ bind(&maybe_in_dictionary);
6522 // If we are doing negative lookup then probing failure should be
6523 // treated as a lookup success. For positive lookup probing failure
6524 // should be treated as lookup failure.
6525 if (mode_ == POSITIVE_LOOKUP) {
6526 __ mov(result, Operand(0));
6527 __ Ret();
6528 }
6529
6530 __ bind(&in_dictionary);
6531 __ mov(result, Operand(1));
6532 __ Ret();
6533
6534 __ bind(&not_in_dictionary);
6535 __ mov(result, Operand(0));
6536 __ Ret();
6537}
6538
6539
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006540#undef __
6541
6542} } // namespace v8::internal
6543
6544#endif // V8_TARGET_ARCH_ARM