blob: f2c0f992167315fc937e4b2cf7e76147d28c021c [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
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000101 int map_index = (language_mode_ == CLASSIC_MODE)
102 ? Context::FUNCTION_MAP_INDEX
103 : Context::STRICT_MODE_FUNCTION_MAP_INDEX;
Steve Block44f0eee2011-05-26 01:26:41 +0100104
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
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000192void FastNewBlockContextStub::Generate(MacroAssembler* masm) {
193 // Stack layout on entry:
194 //
195 // [sp]: function.
196 // [sp + kPointerSize]: serialized scope info
197
198 // Try to allocate the context in new space.
199 Label gc;
200 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
201 __ AllocateInNewSpace(FixedArray::SizeFor(length),
202 r0, r1, r2, &gc, TAG_OBJECT);
203
204 // Load the function from the stack.
205 __ ldr(r3, MemOperand(sp, 0));
206
207 // Load the serialized scope info from the stack.
208 __ ldr(r1, MemOperand(sp, 1 * kPointerSize));
209
210 // Setup the object header.
211 __ LoadRoot(r2, Heap::kBlockContextMapRootIndex);
212 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
213 __ mov(r2, Operand(Smi::FromInt(length)));
214 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
215
216 // If this block context is nested in the global context we get a smi
217 // sentinel instead of a function. The block context should get the
218 // canonical empty function of the global context as its closure which
219 // we still have to look up.
220 Label after_sentinel;
221 __ JumpIfNotSmi(r3, &after_sentinel);
222 if (FLAG_debug_code) {
223 const char* message = "Expected 0 as a Smi sentinel";
224 __ cmp(r3, Operand::Zero());
225 __ Assert(eq, message);
226 }
227 __ ldr(r3, GlobalObjectOperand());
228 __ ldr(r3, FieldMemOperand(r3, GlobalObject::kGlobalContextOffset));
229 __ ldr(r3, ContextOperand(r3, Context::CLOSURE_INDEX));
230 __ bind(&after_sentinel);
231
232 // Setup the fixed slots.
233 __ str(r3, ContextOperand(r0, Context::CLOSURE_INDEX));
234 __ str(cp, ContextOperand(r0, Context::PREVIOUS_INDEX));
235 __ str(r1, ContextOperand(r0, Context::EXTENSION_INDEX));
236
237 // Copy the global object from the previous context.
238 __ ldr(r1, ContextOperand(cp, Context::GLOBAL_INDEX));
239 __ str(r1, ContextOperand(r0, Context::GLOBAL_INDEX));
240
241 // Initialize the rest of the slots to the hole value.
242 __ LoadRoot(r1, Heap::kTheHoleValueRootIndex);
243 for (int i = 0; i < slots_; i++) {
244 __ str(r1, ContextOperand(r0, i + Context::MIN_CONTEXT_SLOTS));
245 }
246
247 // Remove the on-stack argument and return.
248 __ mov(cp, r0);
249 __ add(sp, sp, Operand(2 * kPointerSize));
250 __ Ret();
251
252 // Need to collect. Call into runtime system.
253 __ bind(&gc);
254 __ TailCallRuntime(Runtime::kPushBlockContext, 2, 1);
255}
256
257
258static void GenerateFastCloneShallowArrayCommon(
259 MacroAssembler* masm,
260 int length,
261 FastCloneShallowArrayStub::Mode mode,
262 Label* fail) {
263 // Registers on entry:
264 //
265 // r3: boilerplate literal array.
266 ASSERT(mode != FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS);
267
268 // All sizes here are multiples of kPointerSize.
269 int elements_size = 0;
270 if (length > 0) {
271 elements_size = mode == FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS
272 ? FixedDoubleArray::SizeFor(length)
273 : FixedArray::SizeFor(length);
274 }
275 int size = JSArray::kSize + elements_size;
276
277 // Allocate both the JS array and the elements array in one big
278 // allocation. This avoids multiple limit checks.
279 __ AllocateInNewSpace(size,
280 r0,
281 r1,
282 r2,
283 fail,
284 TAG_OBJECT);
285
286 // Copy the JS array part.
287 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
288 if ((i != JSArray::kElementsOffset) || (length == 0)) {
289 __ ldr(r1, FieldMemOperand(r3, i));
290 __ str(r1, FieldMemOperand(r0, i));
291 }
292 }
293
294 if (length > 0) {
295 // Get hold of the elements array of the boilerplate and setup the
296 // elements pointer in the resulting object.
297 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
298 __ add(r2, r0, Operand(JSArray::kSize));
299 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
300
301 // Copy the elements array.
302 ASSERT((elements_size % kPointerSize) == 0);
303 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
304 }
305}
306
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100307void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
308 // Stack layout on entry:
309 //
310 // [sp]: constant elements.
311 // [sp + kPointerSize]: literal index.
312 // [sp + (2 * kPointerSize)]: literals array.
313
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100314 // Load boilerplate object into r3 and check if we need to create a
315 // boilerplate.
316 Label slow_case;
317 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
318 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
319 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
320 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000321 __ CompareRoot(r3, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100322 __ b(eq, &slow_case);
323
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000324 FastCloneShallowArrayStub::Mode mode = mode_;
325 if (mode == CLONE_ANY_ELEMENTS) {
326 Label double_elements, check_fast_elements;
327 __ ldr(r0, FieldMemOperand(r3, JSArray::kElementsOffset));
328 __ ldr(r0, FieldMemOperand(r0, HeapObject::kMapOffset));
329 __ LoadRoot(ip, Heap::kFixedCOWArrayMapRootIndex);
330 __ cmp(r0, ip);
331 __ b(ne, &check_fast_elements);
332 GenerateFastCloneShallowArrayCommon(masm, 0,
333 COPY_ON_WRITE_ELEMENTS, &slow_case);
334 // Return and remove the on-stack parameters.
335 __ add(sp, sp, Operand(3 * kPointerSize));
336 __ Ret();
337
338 __ bind(&check_fast_elements);
339 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
340 __ cmp(r0, ip);
341 __ b(ne, &double_elements);
342 GenerateFastCloneShallowArrayCommon(masm, length_,
343 CLONE_ELEMENTS, &slow_case);
344 // Return and remove the on-stack parameters.
345 __ add(sp, sp, Operand(3 * kPointerSize));
346 __ Ret();
347
348 __ bind(&double_elements);
349 mode = CLONE_DOUBLE_ELEMENTS;
350 // Fall through to generate the code to handle double elements.
351 }
352
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100353 if (FLAG_debug_code) {
354 const char* message;
355 Heap::RootListIndex expected_map_index;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000356 if (mode == CLONE_ELEMENTS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100357 message = "Expected (writable) fixed array";
358 expected_map_index = Heap::kFixedArrayMapRootIndex;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000359 } else if (mode == CLONE_DOUBLE_ELEMENTS) {
360 message = "Expected (writable) fixed double array";
361 expected_map_index = Heap::kFixedDoubleArrayMapRootIndex;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100362 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000363 ASSERT(mode == COPY_ON_WRITE_ELEMENTS);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100364 message = "Expected copy-on-write fixed array";
365 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
366 }
367 __ push(r3);
368 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
369 __ ldr(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000370 __ CompareRoot(r3, expected_map_index);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100371 __ Assert(eq, message);
372 __ pop(r3);
373 }
374
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000375 GenerateFastCloneShallowArrayCommon(masm, length_, mode, &slow_case);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100376
377 // Return and remove the on-stack parameters.
378 __ add(sp, sp, Operand(3 * kPointerSize));
379 __ Ret();
380
381 __ bind(&slow_case);
382 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
383}
384
385
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000386void FastCloneShallowObjectStub::Generate(MacroAssembler* masm) {
387 // Stack layout on entry:
388 //
389 // [sp]: object literal flags.
390 // [sp + kPointerSize]: constant properties.
391 // [sp + (2 * kPointerSize)]: literal index.
392 // [sp + (3 * kPointerSize)]: literals array.
393
394 // Load boilerplate object into r3 and check if we need to create a
395 // boilerplate.
396 Label slow_case;
397 __ ldr(r3, MemOperand(sp, 3 * kPointerSize));
398 __ ldr(r0, MemOperand(sp, 2 * kPointerSize));
399 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
400 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
401 __ CompareRoot(r3, Heap::kUndefinedValueRootIndex);
402 __ b(eq, &slow_case);
403
404 // Check that the boilerplate contains only fast properties and we can
405 // statically determine the instance size.
406 int size = JSObject::kHeaderSize + length_ * kPointerSize;
407 __ ldr(r0, FieldMemOperand(r3, HeapObject::kMapOffset));
408 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceSizeOffset));
409 __ cmp(r0, Operand(size >> kPointerSizeLog2));
410 __ b(ne, &slow_case);
411
412 // Allocate the JS object and copy header together with all in-object
413 // properties from the boilerplate.
414 __ AllocateInNewSpace(size, r0, r1, r2, &slow_case, TAG_OBJECT);
415 for (int i = 0; i < size; i += kPointerSize) {
416 __ ldr(r1, FieldMemOperand(r3, i));
417 __ str(r1, FieldMemOperand(r0, i));
418 }
419
420 // Return and remove the on-stack parameters.
421 __ add(sp, sp, Operand(4 * kPointerSize));
422 __ Ret();
423
424 __ bind(&slow_case);
425 __ TailCallRuntime(Runtime::kCreateObjectLiteralShallow, 4, 1);
426}
427
428
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100429// Takes a Smi and converts to an IEEE 64 bit floating point value in two
430// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
431// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
432// scratch register. Destroys the source register. No GC occurs during this
433// stub so you don't have to set up the frame.
434class ConvertToDoubleStub : public CodeStub {
435 public:
436 ConvertToDoubleStub(Register result_reg_1,
437 Register result_reg_2,
438 Register source_reg,
439 Register scratch_reg)
440 : result1_(result_reg_1),
441 result2_(result_reg_2),
442 source_(source_reg),
443 zeros_(scratch_reg) { }
444
445 private:
446 Register result1_;
447 Register result2_;
448 Register source_;
449 Register zeros_;
450
451 // Minor key encoding in 16 bits.
452 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
453 class OpBits: public BitField<Token::Value, 2, 14> {};
454
455 Major MajorKey() { return ConvertToDouble; }
456 int MinorKey() {
457 // Encode the parameters in a unique 16 bit value.
458 return result1_.code() +
459 (result2_.code() << 4) +
460 (source_.code() << 8) +
461 (zeros_.code() << 12);
462 }
463
464 void Generate(MacroAssembler* masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100465};
466
467
468void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100469 Register exponent = result1_;
470 Register mantissa = result2_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100471
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100472 Label not_special;
473 // Convert from Smi to integer.
474 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
475 // Move sign bit from source to destination. This works because the sign bit
476 // in the exponent word of the double has the same position and polarity as
477 // the 2's complement sign bit in a Smi.
478 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
479 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
480 // Subtract from 0 if source was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100481 __ rsb(source_, source_, Operand(0, RelocInfo::NONE), LeaveCC, ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100482
483 // We have -1, 0 or 1, which we treat specially. Register source_ contains
484 // absolute value: it is either equal to 1 (special case of -1 and 1),
485 // greater than 1 (not a special case) or less than 1 (special case of 0).
486 __ cmp(source_, Operand(1));
487 __ b(gt, &not_special);
488
489 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
490 static const uint32_t exponent_word_for_1 =
491 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
492 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
493 // 1, 0 and -1 all have 0 for the second word.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100494 __ mov(mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100495 __ Ret();
496
497 __ bind(&not_special);
498 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
499 // Gets the wrong answer for 0, but we already checked for that case above.
500 __ CountLeadingZeros(zeros_, source_, mantissa);
501 // Compute exponent and or it into the exponent register.
502 // We use mantissa as a scratch register here. Use a fudge factor to
503 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
504 // that fit in the ARM's constant field.
505 int fudge = 0x400;
506 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
507 __ add(mantissa, mantissa, Operand(fudge));
508 __ orr(exponent,
509 exponent,
510 Operand(mantissa, LSL, HeapNumber::kExponentShift));
511 // Shift up the source chopping the top bit off.
512 __ add(zeros_, zeros_, Operand(1));
513 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
514 __ mov(source_, Operand(source_, LSL, zeros_));
515 // Compute lower part of fraction (last 12 bits).
516 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
517 // And the top (top 20 bits).
518 __ orr(exponent,
519 exponent,
520 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
521 __ Ret();
522}
523
524
Steve Block1e0659c2011-05-24 12:43:12 +0100525void FloatingPointHelper::LoadSmis(MacroAssembler* masm,
526 FloatingPointHelper::Destination destination,
527 Register scratch1,
528 Register scratch2) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100529 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100530 CpuFeatures::Scope scope(VFP3);
531 __ mov(scratch1, Operand(r0, ASR, kSmiTagSize));
532 __ vmov(d7.high(), scratch1);
533 __ vcvt_f64_s32(d7, d7.high());
534 __ mov(scratch1, Operand(r1, ASR, kSmiTagSize));
535 __ vmov(d6.high(), scratch1);
536 __ vcvt_f64_s32(d6, d6.high());
537 if (destination == kCoreRegisters) {
538 __ vmov(r2, r3, d7);
539 __ vmov(r0, r1, d6);
540 }
541 } else {
542 ASSERT(destination == kCoreRegisters);
543 // Write Smi from r0 to r3 and r2 in double format.
544 __ mov(scratch1, Operand(r0));
545 ConvertToDoubleStub stub1(r3, r2, scratch1, scratch2);
546 __ push(lr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000547 __ Call(stub1.GetCode());
Ben Murdoch8b112d22011-06-08 16:22:53 +0100548 // Write Smi from r1 to r1 and r0 in double format.
Steve Block1e0659c2011-05-24 12:43:12 +0100549 __ mov(scratch1, Operand(r1));
550 ConvertToDoubleStub stub2(r1, r0, scratch1, scratch2);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000551 __ Call(stub2.GetCode());
Steve Block1e0659c2011-05-24 12:43:12 +0100552 __ pop(lr);
553 }
554}
555
556
557void FloatingPointHelper::LoadOperands(
558 MacroAssembler* masm,
559 FloatingPointHelper::Destination destination,
560 Register heap_number_map,
561 Register scratch1,
562 Register scratch2,
563 Label* slow) {
564
565 // Load right operand (r0) to d6 or r2/r3.
566 LoadNumber(masm, destination,
567 r0, d7, r2, r3, heap_number_map, scratch1, scratch2, slow);
568
569 // Load left operand (r1) to d7 or r0/r1.
570 LoadNumber(masm, destination,
571 r1, d6, r0, r1, heap_number_map, scratch1, scratch2, slow);
572}
573
574
575void FloatingPointHelper::LoadNumber(MacroAssembler* masm,
576 Destination destination,
577 Register object,
578 DwVfpRegister dst,
579 Register dst1,
580 Register dst2,
581 Register heap_number_map,
582 Register scratch1,
583 Register scratch2,
584 Label* not_number) {
585 if (FLAG_debug_code) {
586 __ AbortIfNotRootValue(heap_number_map,
587 Heap::kHeapNumberMapRootIndex,
588 "HeapNumberMap register clobbered.");
589 }
590
591 Label is_smi, done;
592
593 __ JumpIfSmi(object, &is_smi);
594 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_number);
595
596 // Handle loading a double from a heap number.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100597 if (CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100598 destination == kVFPRegisters) {
Steve Block1e0659c2011-05-24 12:43:12 +0100599 CpuFeatures::Scope scope(VFP3);
600 // Load the double from tagged HeapNumber to double register.
601 __ sub(scratch1, object, Operand(kHeapObjectTag));
602 __ vldr(dst, scratch1, HeapNumber::kValueOffset);
603 } else {
604 ASSERT(destination == kCoreRegisters);
605 // Load the double from heap number to dst1 and dst2 in double format.
606 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
607 }
608 __ jmp(&done);
609
610 // Handle loading a double from a smi.
611 __ bind(&is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100612 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100613 CpuFeatures::Scope scope(VFP3);
614 // Convert smi to double using VFP instructions.
615 __ SmiUntag(scratch1, object);
616 __ vmov(dst.high(), scratch1);
617 __ vcvt_f64_s32(dst, dst.high());
618 if (destination == kCoreRegisters) {
619 // Load the converted smi to dst1 and dst2 in double format.
620 __ vmov(dst1, dst2, dst);
621 }
622 } else {
623 ASSERT(destination == kCoreRegisters);
624 // Write smi to dst1 and dst2 double format.
625 __ mov(scratch1, Operand(object));
626 ConvertToDoubleStub stub(dst2, dst1, scratch1, scratch2);
627 __ push(lr);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000628 __ Call(stub.GetCode());
Steve Block1e0659c2011-05-24 12:43:12 +0100629 __ pop(lr);
630 }
631
632 __ bind(&done);
633}
634
635
Steve Block44f0eee2011-05-26 01:26:41 +0100636void FloatingPointHelper::ConvertNumberToInt32(MacroAssembler* masm,
637 Register object,
638 Register dst,
639 Register heap_number_map,
640 Register scratch1,
641 Register scratch2,
642 Register scratch3,
643 DwVfpRegister double_scratch,
644 Label* not_number) {
Steve Block1e0659c2011-05-24 12:43:12 +0100645 if (FLAG_debug_code) {
646 __ AbortIfNotRootValue(heap_number_map,
647 Heap::kHeapNumberMapRootIndex,
648 "HeapNumberMap register clobbered.");
649 }
Steve Block44f0eee2011-05-26 01:26:41 +0100650 Label is_smi;
651 Label done;
652 Label not_in_int32_range;
653
Steve Block1e0659c2011-05-24 12:43:12 +0100654 __ JumpIfSmi(object, &is_smi);
655 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kMapOffset));
656 __ cmp(scratch1, heap_number_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100657 __ b(ne, not_number);
658 __ ConvertToInt32(object,
659 dst,
660 scratch1,
661 scratch2,
662 double_scratch,
663 &not_in_int32_range);
Steve Block1e0659c2011-05-24 12:43:12 +0100664 __ jmp(&done);
Steve Block44f0eee2011-05-26 01:26:41 +0100665
666 __ bind(&not_in_int32_range);
667 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
668 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
669
670 __ EmitOutOfInt32RangeTruncate(dst,
671 scratch1,
672 scratch2,
673 scratch3);
674 __ jmp(&done);
675
Steve Block1e0659c2011-05-24 12:43:12 +0100676 __ bind(&is_smi);
677 __ SmiUntag(dst, object);
678 __ bind(&done);
679}
680
681
Ben Murdoch257744e2011-11-30 15:57:28 +0000682void FloatingPointHelper::ConvertIntToDouble(MacroAssembler* masm,
683 Register int_scratch,
684 Destination destination,
685 DwVfpRegister double_dst,
686 Register dst1,
687 Register dst2,
688 Register scratch2,
689 SwVfpRegister single_scratch) {
690 ASSERT(!int_scratch.is(scratch2));
691 ASSERT(!int_scratch.is(dst1));
692 ASSERT(!int_scratch.is(dst2));
693
694 Label done;
695
696 if (CpuFeatures::IsSupported(VFP3)) {
697 CpuFeatures::Scope scope(VFP3);
698 __ vmov(single_scratch, int_scratch);
699 __ vcvt_f64_s32(double_dst, single_scratch);
700 if (destination == kCoreRegisters) {
701 __ vmov(dst1, dst2, double_dst);
702 }
703 } else {
704 Label fewer_than_20_useful_bits;
705 // Expected output:
706 // | dst2 | dst1 |
707 // | s | exp | mantissa |
708
709 // Check for zero.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000710 __ cmp(int_scratch, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +0000711 __ mov(dst2, int_scratch);
712 __ mov(dst1, int_scratch);
713 __ b(eq, &done);
714
715 // Preload the sign of the value.
716 __ and_(dst2, int_scratch, Operand(HeapNumber::kSignMask), SetCC);
717 // Get the absolute value of the object (as an unsigned integer).
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000718 __ rsb(int_scratch, int_scratch, Operand::Zero(), SetCC, mi);
Ben Murdoch257744e2011-11-30 15:57:28 +0000719
720 // Get mantisssa[51:20].
721
722 // Get the position of the first set bit.
723 __ CountLeadingZeros(dst1, int_scratch, scratch2);
724 __ rsb(dst1, dst1, Operand(31));
725
726 // Set the exponent.
727 __ add(scratch2, dst1, Operand(HeapNumber::kExponentBias));
728 __ Bfi(dst2, scratch2, scratch2,
729 HeapNumber::kExponentShift, HeapNumber::kExponentBits);
730
731 // Clear the first non null bit.
732 __ mov(scratch2, Operand(1));
733 __ bic(int_scratch, int_scratch, Operand(scratch2, LSL, dst1));
734
735 __ cmp(dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
736 // Get the number of bits to set in the lower part of the mantissa.
737 __ sub(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord), SetCC);
738 __ b(mi, &fewer_than_20_useful_bits);
739 // Set the higher 20 bits of the mantissa.
740 __ orr(dst2, dst2, Operand(int_scratch, LSR, scratch2));
741 __ rsb(scratch2, scratch2, Operand(32));
742 __ mov(dst1, Operand(int_scratch, LSL, scratch2));
743 __ b(&done);
744
745 __ bind(&fewer_than_20_useful_bits);
746 __ rsb(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
747 __ mov(scratch2, Operand(int_scratch, LSL, scratch2));
748 __ orr(dst2, dst2, scratch2);
749 // Set dst1 to 0.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000750 __ mov(dst1, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +0000751 }
752 __ bind(&done);
753}
754
755
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100756void FloatingPointHelper::LoadNumberAsInt32Double(MacroAssembler* masm,
757 Register object,
758 Destination destination,
759 DwVfpRegister double_dst,
760 Register dst1,
761 Register dst2,
762 Register heap_number_map,
763 Register scratch1,
764 Register scratch2,
765 SwVfpRegister single_scratch,
766 Label* not_int32) {
767 ASSERT(!scratch1.is(object) && !scratch2.is(object));
768 ASSERT(!scratch1.is(scratch2));
769 ASSERT(!heap_number_map.is(object) &&
770 !heap_number_map.is(scratch1) &&
771 !heap_number_map.is(scratch2));
772
773 Label done, obj_is_not_smi;
774
775 __ JumpIfNotSmi(object, &obj_is_not_smi);
776 __ SmiUntag(scratch1, object);
Ben Murdoch257744e2011-11-30 15:57:28 +0000777 ConvertIntToDouble(masm, scratch1, destination, double_dst, dst1, dst2,
778 scratch2, single_scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100779 __ b(&done);
780
781 __ bind(&obj_is_not_smi);
782 if (FLAG_debug_code) {
783 __ AbortIfNotRootValue(heap_number_map,
784 Heap::kHeapNumberMapRootIndex,
785 "HeapNumberMap register clobbered.");
786 }
787 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
788
789 // Load the number.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100790 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100791 CpuFeatures::Scope scope(VFP3);
792 // Load the double value.
793 __ sub(scratch1, object, Operand(kHeapObjectTag));
794 __ vldr(double_dst, scratch1, HeapNumber::kValueOffset);
795
796 __ EmitVFPTruncate(kRoundToZero,
797 single_scratch,
798 double_dst,
799 scratch1,
800 scratch2,
801 kCheckForInexactConversion);
802
803 // Jump to not_int32 if the operation did not succeed.
804 __ b(ne, not_int32);
805
806 if (destination == kCoreRegisters) {
807 __ vmov(dst1, dst2, double_dst);
808 }
809
810 } else {
811 ASSERT(!scratch1.is(object) && !scratch2.is(object));
812 // Load the double value in the destination registers..
813 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
814
815 // Check for 0 and -0.
816 __ bic(scratch1, dst1, Operand(HeapNumber::kSignMask));
817 __ orr(scratch1, scratch1, Operand(dst2));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000818 __ cmp(scratch1, Operand::Zero());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100819 __ b(eq, &done);
820
821 // Check that the value can be exactly represented by a 32-bit integer.
822 // Jump to not_int32 if that's not the case.
823 DoubleIs32BitInteger(masm, dst1, dst2, scratch1, scratch2, not_int32);
824
825 // dst1 and dst2 were trashed. Reload the double value.
826 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
827 }
828
829 __ bind(&done);
830}
831
832
833void FloatingPointHelper::LoadNumberAsInt32(MacroAssembler* masm,
834 Register object,
835 Register dst,
836 Register heap_number_map,
837 Register scratch1,
838 Register scratch2,
839 Register scratch3,
840 DwVfpRegister double_scratch,
841 Label* not_int32) {
842 ASSERT(!dst.is(object));
843 ASSERT(!scratch1.is(object) && !scratch2.is(object) && !scratch3.is(object));
844 ASSERT(!scratch1.is(scratch2) &&
845 !scratch1.is(scratch3) &&
846 !scratch2.is(scratch3));
847
848 Label done;
849
850 // Untag the object into the destination register.
851 __ SmiUntag(dst, object);
852 // Just return if the object is a smi.
853 __ JumpIfSmi(object, &done);
854
855 if (FLAG_debug_code) {
856 __ AbortIfNotRootValue(heap_number_map,
857 Heap::kHeapNumberMapRootIndex,
858 "HeapNumberMap register clobbered.");
859 }
860 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
861
862 // Object is a heap number.
863 // Convert the floating point value to a 32-bit integer.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100864 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100865 CpuFeatures::Scope scope(VFP3);
866 SwVfpRegister single_scratch = double_scratch.low();
867 // Load the double value.
868 __ sub(scratch1, object, Operand(kHeapObjectTag));
869 __ vldr(double_scratch, scratch1, HeapNumber::kValueOffset);
870
871 __ EmitVFPTruncate(kRoundToZero,
872 single_scratch,
873 double_scratch,
874 scratch1,
875 scratch2,
876 kCheckForInexactConversion);
877
878 // Jump to not_int32 if the operation did not succeed.
879 __ b(ne, not_int32);
880 // Get the result in the destination register.
881 __ vmov(dst, single_scratch);
882
883 } else {
884 // Load the double value in the destination registers.
885 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
886 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
887
888 // Check for 0 and -0.
889 __ bic(dst, scratch1, Operand(HeapNumber::kSignMask));
890 __ orr(dst, scratch2, Operand(dst));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000891 __ cmp(dst, Operand::Zero());
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100892 __ b(eq, &done);
893
894 DoubleIs32BitInteger(masm, scratch1, scratch2, dst, scratch3, not_int32);
895
896 // Registers state after DoubleIs32BitInteger.
897 // dst: mantissa[51:20].
898 // scratch2: 1
899
900 // Shift back the higher bits of the mantissa.
901 __ mov(dst, Operand(dst, LSR, scratch3));
902 // Set the implicit first bit.
903 __ rsb(scratch3, scratch3, Operand(32));
904 __ orr(dst, dst, Operand(scratch2, LSL, scratch3));
905 // Set the sign.
906 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
907 __ tst(scratch1, Operand(HeapNumber::kSignMask));
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000908 __ rsb(dst, dst, Operand::Zero(), LeaveCC, mi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100909 }
910
911 __ bind(&done);
912}
913
914
915void FloatingPointHelper::DoubleIs32BitInteger(MacroAssembler* masm,
916 Register src1,
917 Register src2,
918 Register dst,
919 Register scratch,
920 Label* not_int32) {
921 // Get exponent alone in scratch.
922 __ Ubfx(scratch,
923 src1,
924 HeapNumber::kExponentShift,
925 HeapNumber::kExponentBits);
926
927 // Substract the bias from the exponent.
928 __ sub(scratch, scratch, Operand(HeapNumber::kExponentBias), SetCC);
929
930 // src1: higher (exponent) part of the double value.
931 // src2: lower (mantissa) part of the double value.
932 // scratch: unbiased exponent.
933
934 // Fast cases. Check for obvious non 32-bit integer values.
935 // Negative exponent cannot yield 32-bit integers.
936 __ b(mi, not_int32);
937 // Exponent greater than 31 cannot yield 32-bit integers.
938 // Also, a positive value with an exponent equal to 31 is outside of the
939 // signed 32-bit integer range.
940 // Another way to put it is that if (exponent - signbit) > 30 then the
941 // number cannot be represented as an int32.
942 Register tmp = dst;
943 __ sub(tmp, scratch, Operand(src1, LSR, 31));
944 __ cmp(tmp, Operand(30));
945 __ b(gt, not_int32);
946 // - Bits [21:0] in the mantissa are not null.
947 __ tst(src2, Operand(0x3fffff));
948 __ b(ne, not_int32);
949
950 // Otherwise the exponent needs to be big enough to shift left all the
951 // non zero bits left. So we need the (30 - exponent) last bits of the
952 // 31 higher bits of the mantissa to be null.
953 // Because bits [21:0] are null, we can check instead that the
954 // (32 - exponent) last bits of the 32 higher bits of the mantisssa are null.
955
956 // Get the 32 higher bits of the mantissa in dst.
957 __ Ubfx(dst,
958 src2,
959 HeapNumber::kMantissaBitsInTopWord,
960 32 - HeapNumber::kMantissaBitsInTopWord);
961 __ orr(dst,
962 dst,
963 Operand(src1, LSL, HeapNumber::kNonMantissaBitsInTopWord));
964
965 // Create the mask and test the lower bits (of the higher bits).
966 __ rsb(scratch, scratch, Operand(32));
967 __ mov(src2, Operand(1));
968 __ mov(src1, Operand(src2, LSL, scratch));
969 __ sub(src1, src1, Operand(1));
970 __ tst(dst, src1);
971 __ b(ne, not_int32);
972}
973
974
975void FloatingPointHelper::CallCCodeForDoubleOperation(
976 MacroAssembler* masm,
977 Token::Value op,
978 Register heap_number_result,
979 Register scratch) {
980 // Using core registers:
981 // r0: Left value (least significant part of mantissa).
982 // r1: Left value (sign, exponent, top of mantissa).
983 // r2: Right value (least significant part of mantissa).
984 // r3: Right value (sign, exponent, top of mantissa).
985
986 // Assert that heap_number_result is callee-saved.
987 // We currently always use r5 to pass it.
988 ASSERT(heap_number_result.is(r5));
989
990 // Push the current return address before the C call. Return will be
991 // through pop(pc) below.
992 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +0000993 __ PrepareCallCFunction(0, 2, scratch);
994 if (masm->use_eabi_hardfloat()) {
995 CpuFeatures::Scope scope(VFP3);
996 __ vmov(d0, r0, r1);
997 __ vmov(d1, r2, r3);
998 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000999 {
1000 AllowExternalCallThatCantCauseGC scope(masm);
1001 __ CallCFunction(
1002 ExternalReference::double_fp_operation(op, masm->isolate()), 0, 2);
1003 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01001004 // Store answer in the overwritable heap number. Double returned in
Ben Murdoch257744e2011-11-30 15:57:28 +00001005 // registers r0 and r1 or in d0.
1006 if (masm->use_eabi_hardfloat()) {
1007 CpuFeatures::Scope scope(VFP3);
1008 __ vstr(d0,
1009 FieldMemOperand(heap_number_result, HeapNumber::kValueOffset));
1010 } else {
1011 __ Strd(r0, r1, FieldMemOperand(heap_number_result,
1012 HeapNumber::kValueOffset));
1013 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001014 // Place heap_number_result in r0 and return to the pushed return address.
1015 __ mov(r0, Operand(heap_number_result));
1016 __ pop(pc);
1017}
1018
Steve Block1e0659c2011-05-24 12:43:12 +01001019
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001020bool WriteInt32ToHeapNumberStub::IsPregenerated() {
1021 // These variants are compiled ahead of time. See next method.
1022 if (the_int_.is(r1) && the_heap_number_.is(r0) && scratch_.is(r2)) {
1023 return true;
1024 }
1025 if (the_int_.is(r2) && the_heap_number_.is(r0) && scratch_.is(r3)) {
1026 return true;
1027 }
1028 // Other register combinations are generated as and when they are needed,
1029 // so it is unsafe to call them from stubs (we can't generate a stub while
1030 // we are generating a stub).
1031 return false;
1032}
1033
1034
1035void WriteInt32ToHeapNumberStub::GenerateFixedRegStubsAheadOfTime() {
1036 WriteInt32ToHeapNumberStub stub1(r1, r0, r2);
1037 WriteInt32ToHeapNumberStub stub2(r2, r0, r3);
1038 stub1.GetCode()->set_is_pregenerated(true);
1039 stub2.GetCode()->set_is_pregenerated(true);
1040}
1041
1042
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001043// See comment for class.
1044void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
1045 Label max_negative_int;
1046 // the_int_ has the answer which is a signed int32 but not a Smi.
1047 // We test for the special value that has a different exponent. This test
1048 // has the neat side effect of setting the flags according to the sign.
1049 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
1050 __ cmp(the_int_, Operand(0x80000000u));
1051 __ b(eq, &max_negative_int);
1052 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
1053 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
1054 uint32_t non_smi_exponent =
1055 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
1056 __ mov(scratch_, Operand(non_smi_exponent));
1057 // Set the sign bit in scratch_ if the value was negative.
1058 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
1059 // Subtract from 0 if the value was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001060 __ rsb(the_int_, the_int_, Operand(0, RelocInfo::NONE), LeaveCC, cs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001061 // We should be masking the implict first digit of the mantissa away here,
1062 // but it just ends up combining harmlessly with the last digit of the
1063 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
1064 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
1065 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
1066 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1067 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
1068 __ str(scratch_, FieldMemOperand(the_heap_number_,
1069 HeapNumber::kExponentOffset));
1070 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
1071 __ str(scratch_, FieldMemOperand(the_heap_number_,
1072 HeapNumber::kMantissaOffset));
1073 __ Ret();
1074
1075 __ bind(&max_negative_int);
1076 // The max negative int32 is stored as a positive number in the mantissa of
1077 // a double because it uses a sign bit instead of using two's complement.
1078 // The actual mantissa bits stored are all 0 because the implicit most
1079 // significant 1 bit is not stored.
1080 non_smi_exponent += 1 << HeapNumber::kExponentShift;
1081 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
1082 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
Iain Merrick9ac36c92010-09-13 15:29:50 +01001083 __ mov(ip, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001084 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
1085 __ Ret();
1086}
1087
1088
1089// Handle the case where the lhs and rhs are the same object.
1090// Equality is almost reflexive (everything but NaN), so this is a test
1091// for "identity and not NaN".
1092static void EmitIdenticalObjectComparison(MacroAssembler* masm,
1093 Label* slow,
Steve Block1e0659c2011-05-24 12:43:12 +01001094 Condition cond,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001095 bool never_nan_nan) {
1096 Label not_identical;
1097 Label heap_number, return_equal;
1098 __ cmp(r0, r1);
1099 __ b(ne, &not_identical);
1100
1101 // The two objects are identical. If we know that one of them isn't NaN then
1102 // we now know they test equal.
Steve Block1e0659c2011-05-24 12:43:12 +01001103 if (cond != eq || !never_nan_nan) {
Steve Block44f0eee2011-05-26 01:26:41 +01001104 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001105 // so we do the second best thing - test it ourselves.
1106 // They are both equal and they are not both Smis so both of them are not
1107 // Smis. If it's not a heap number, then return equal.
Steve Block1e0659c2011-05-24 12:43:12 +01001108 if (cond == lt || cond == gt) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001109 __ CompareObjectType(r0, r4, r4, FIRST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001110 __ b(ge, slow);
1111 } else {
1112 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
1113 __ b(eq, &heap_number);
1114 // Comparing JS objects with <=, >= is complicated.
Steve Block1e0659c2011-05-24 12:43:12 +01001115 if (cond != eq) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001116 __ cmp(r4, Operand(FIRST_SPEC_OBJECT_TYPE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001117 __ b(ge, slow);
1118 // Normally here we fall through to return_equal, but undefined is
1119 // special: (undefined == undefined) == true, but
1120 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
Steve Block1e0659c2011-05-24 12:43:12 +01001121 if (cond == le || cond == ge) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001122 __ cmp(r4, Operand(ODDBALL_TYPE));
1123 __ b(ne, &return_equal);
1124 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
1125 __ cmp(r0, r2);
1126 __ b(ne, &return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +01001127 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001128 // undefined <= undefined should fail.
1129 __ mov(r0, Operand(GREATER));
1130 } else {
1131 // undefined >= undefined should fail.
1132 __ mov(r0, Operand(LESS));
1133 }
1134 __ Ret();
1135 }
1136 }
1137 }
1138 }
1139
1140 __ bind(&return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +01001141 if (cond == lt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001142 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
Steve Block1e0659c2011-05-24 12:43:12 +01001143 } else if (cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001144 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
1145 } else {
1146 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
1147 }
1148 __ Ret();
1149
Steve Block1e0659c2011-05-24 12:43:12 +01001150 if (cond != eq || !never_nan_nan) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001151 // For less and greater we don't have to check for NaN since the result of
1152 // x < x is false regardless. For the others here is some code to check
1153 // for NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001154 if (cond != lt && cond != gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001155 __ bind(&heap_number);
1156 // It is a heap number, so return non-equal if it's NaN and equal if it's
1157 // not NaN.
1158
1159 // The representation of NaN values has all exponent bits (52..62) set,
1160 // and not all mantissa bits (0..51) clear.
1161 // Read top bits of double representation (second word of value).
1162 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1163 // Test that exponent bits are all set.
1164 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
1165 // NaNs have all-one exponents so they sign extend to -1.
1166 __ cmp(r3, Operand(-1));
1167 __ b(ne, &return_equal);
1168
1169 // Shift out flag and all exponent bits, retaining only mantissa.
1170 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
1171 // Or with all low-bits of mantissa.
1172 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
1173 __ orr(r0, r3, Operand(r2), SetCC);
1174 // For equal we already have the right value in r0: Return zero (equal)
1175 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
1176 // not (it's a NaN). For <= and >= we need to load r0 with the failing
1177 // value if it's a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001178 if (cond != eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001179 // All-zero means Infinity means equal.
1180 __ Ret(eq);
Steve Block1e0659c2011-05-24 12:43:12 +01001181 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001182 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
1183 } else {
1184 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
1185 }
1186 }
1187 __ Ret();
1188 }
1189 // No fall through here.
1190 }
1191
1192 __ bind(&not_identical);
1193}
1194
1195
1196// See comment at call site.
1197static void EmitSmiNonsmiComparison(MacroAssembler* masm,
1198 Register lhs,
1199 Register rhs,
1200 Label* lhs_not_nan,
1201 Label* slow,
1202 bool strict) {
1203 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1204 (lhs.is(r1) && rhs.is(r0)));
1205
1206 Label rhs_is_smi;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001207 __ JumpIfSmi(rhs, &rhs_is_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001208
1209 // Lhs is a Smi. Check whether the rhs is a heap number.
1210 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
1211 if (strict) {
1212 // If rhs is not a number and lhs is a Smi then strict equality cannot
1213 // succeed. Return non-equal
1214 // If rhs is r0 then there is already a non zero value in it.
1215 if (!rhs.is(r0)) {
1216 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1217 }
1218 __ Ret(ne);
1219 } else {
1220 // Smi compared non-strictly with a non-Smi non-heap-number. Call
1221 // the runtime.
1222 __ b(ne, slow);
1223 }
1224
1225 // Lhs is a smi, rhs is a number.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001226 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001227 // Convert lhs to a double in d7.
1228 CpuFeatures::Scope scope(VFP3);
1229 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
1230 // Load the double from rhs, tagged HeapNumber r0, to d6.
1231 __ sub(r7, rhs, Operand(kHeapObjectTag));
1232 __ vldr(d6, r7, HeapNumber::kValueOffset);
1233 } else {
1234 __ push(lr);
1235 // Convert lhs to a double in r2, r3.
1236 __ mov(r7, Operand(lhs));
1237 ConvertToDoubleStub stub1(r3, r2, r7, r6);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001238 __ Call(stub1.GetCode());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001239 // Load rhs to a double in r0, r1.
1240 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1241 __ pop(lr);
1242 }
1243
1244 // We now have both loaded as doubles but we can skip the lhs nan check
1245 // since it's a smi.
1246 __ jmp(lhs_not_nan);
1247
1248 __ bind(&rhs_is_smi);
1249 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
1250 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
1251 if (strict) {
1252 // If lhs is not a number and rhs is a smi then strict equality cannot
1253 // succeed. Return non-equal.
1254 // If lhs is r0 then there is already a non zero value in it.
1255 if (!lhs.is(r0)) {
1256 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1257 }
1258 __ Ret(ne);
1259 } else {
1260 // Smi compared non-strictly with a non-smi non-heap-number. Call
1261 // the runtime.
1262 __ b(ne, slow);
1263 }
1264
1265 // Rhs is a smi, lhs is a heap number.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001266 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001267 CpuFeatures::Scope scope(VFP3);
1268 // Load the double from lhs, tagged HeapNumber r1, to d7.
1269 __ sub(r7, lhs, Operand(kHeapObjectTag));
1270 __ vldr(d7, r7, HeapNumber::kValueOffset);
1271 // Convert rhs to a double in d6 .
1272 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
1273 } else {
1274 __ push(lr);
1275 // Load lhs to a double in r2, r3.
1276 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1277 // Convert rhs to a double in r0, r1.
1278 __ mov(r7, Operand(rhs));
1279 ConvertToDoubleStub stub2(r1, r0, r7, r6);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001280 __ Call(stub2.GetCode());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001281 __ pop(lr);
1282 }
1283 // Fall through to both_loaded_as_doubles.
1284}
1285
1286
Steve Block1e0659c2011-05-24 12:43:12 +01001287void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001288 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1289 Register rhs_exponent = exp_first ? r0 : r1;
1290 Register lhs_exponent = exp_first ? r2 : r3;
1291 Register rhs_mantissa = exp_first ? r1 : r0;
1292 Register lhs_mantissa = exp_first ? r3 : r2;
1293 Label one_is_nan, neither_is_nan;
1294
1295 __ Sbfx(r4,
1296 lhs_exponent,
1297 HeapNumber::kExponentShift,
1298 HeapNumber::kExponentBits);
1299 // NaNs have all-one exponents so they sign extend to -1.
1300 __ cmp(r4, Operand(-1));
1301 __ b(ne, lhs_not_nan);
1302 __ mov(r4,
1303 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1304 SetCC);
1305 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001306 __ cmp(lhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001307 __ b(ne, &one_is_nan);
1308
1309 __ bind(lhs_not_nan);
1310 __ Sbfx(r4,
1311 rhs_exponent,
1312 HeapNumber::kExponentShift,
1313 HeapNumber::kExponentBits);
1314 // NaNs have all-one exponents so they sign extend to -1.
1315 __ cmp(r4, Operand(-1));
1316 __ b(ne, &neither_is_nan);
1317 __ mov(r4,
1318 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1319 SetCC);
1320 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001321 __ cmp(rhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001322 __ b(eq, &neither_is_nan);
1323
1324 __ bind(&one_is_nan);
1325 // NaN comparisons always fail.
1326 // Load whatever we need in r0 to make the comparison fail.
Steve Block1e0659c2011-05-24 12:43:12 +01001327 if (cond == lt || cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001328 __ mov(r0, Operand(GREATER));
1329 } else {
1330 __ mov(r0, Operand(LESS));
1331 }
1332 __ Ret();
1333
1334 __ bind(&neither_is_nan);
1335}
1336
1337
1338// See comment at call site.
Steve Block1e0659c2011-05-24 12:43:12 +01001339static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm,
1340 Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001341 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1342 Register rhs_exponent = exp_first ? r0 : r1;
1343 Register lhs_exponent = exp_first ? r2 : r3;
1344 Register rhs_mantissa = exp_first ? r1 : r0;
1345 Register lhs_mantissa = exp_first ? r3 : r2;
1346
1347 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001348 if (cond == eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001349 // Doubles are not equal unless they have the same bit pattern.
1350 // Exception: 0 and -0.
1351 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
1352 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
1353 // Return non-zero if the numbers are unequal.
1354 __ Ret(ne);
1355
1356 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
1357 // If exponents are equal then return 0.
1358 __ Ret(eq);
1359
1360 // Exponents are unequal. The only way we can return that the numbers
1361 // are equal is if one is -0 and the other is 0. We already dealt
1362 // with the case where both are -0 or both are 0.
1363 // We start by seeing if the mantissas (that are equal) or the bottom
1364 // 31 bits of the rhs exponent are non-zero. If so we return not
1365 // equal.
1366 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
1367 __ mov(r0, Operand(r4), LeaveCC, ne);
1368 __ Ret(ne);
1369 // Now they are equal if and only if the lhs exponent is zero in its
1370 // low 31 bits.
1371 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
1372 __ Ret();
1373 } else {
1374 // Call a native function to do a comparison between two non-NaNs.
1375 // Call C routine that may not cause GC or other trouble.
1376 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00001377 __ PrepareCallCFunction(0, 2, r5);
1378 if (masm->use_eabi_hardfloat()) {
1379 CpuFeatures::Scope scope(VFP3);
1380 __ vmov(d0, r0, r1);
1381 __ vmov(d1, r2, r3);
1382 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001383
1384 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdoch257744e2011-11-30 15:57:28 +00001385 __ CallCFunction(ExternalReference::compare_doubles(masm->isolate()),
1386 0, 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001387 __ pop(pc); // Return.
1388 }
1389}
1390
1391
1392// See comment at call site.
1393static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
1394 Register lhs,
1395 Register rhs) {
1396 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1397 (lhs.is(r1) && rhs.is(r0)));
1398
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001399 // If either operand is a JS object or an oddball value, then they are
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001400 // not equal since their pointers are different.
1401 // There is no test for undetectability in strict equality.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001402 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001403 Label first_non_object;
1404 // Get the type of the first operand into r2 and compare it with
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001405 // FIRST_SPEC_OBJECT_TYPE.
1406 __ CompareObjectType(rhs, r2, r2, FIRST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001407 __ b(lt, &first_non_object);
1408
1409 // Return non-zero (r0 is not zero)
1410 Label return_not_equal;
1411 __ bind(&return_not_equal);
1412 __ Ret();
1413
1414 __ bind(&first_non_object);
1415 // Check for oddballs: true, false, null, undefined.
1416 __ cmp(r2, Operand(ODDBALL_TYPE));
1417 __ b(eq, &return_not_equal);
1418
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001419 __ CompareObjectType(lhs, r3, r3, FIRST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001420 __ b(ge, &return_not_equal);
1421
1422 // Check for oddballs: true, false, null, undefined.
1423 __ cmp(r3, Operand(ODDBALL_TYPE));
1424 __ b(eq, &return_not_equal);
1425
1426 // Now that we have the types we might as well check for symbol-symbol.
1427 // Ensure that no non-strings have the symbol bit set.
1428 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
1429 STATIC_ASSERT(kSymbolTag != 0);
1430 __ and_(r2, r2, Operand(r3));
1431 __ tst(r2, Operand(kIsSymbolMask));
1432 __ b(ne, &return_not_equal);
1433}
1434
1435
1436// See comment at call site.
1437static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
1438 Register lhs,
1439 Register rhs,
1440 Label* both_loaded_as_doubles,
1441 Label* not_heap_numbers,
1442 Label* slow) {
1443 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1444 (lhs.is(r1) && rhs.is(r0)));
1445
1446 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
1447 __ b(ne, not_heap_numbers);
1448 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
1449 __ cmp(r2, r3);
1450 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
1451
1452 // Both are heap numbers. Load them up then jump to the code we have
1453 // for that.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001454 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001455 CpuFeatures::Scope scope(VFP3);
1456 __ sub(r7, rhs, Operand(kHeapObjectTag));
1457 __ vldr(d6, r7, HeapNumber::kValueOffset);
1458 __ sub(r7, lhs, Operand(kHeapObjectTag));
1459 __ vldr(d7, r7, HeapNumber::kValueOffset);
1460 } else {
1461 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1462 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1463 }
1464 __ jmp(both_loaded_as_doubles);
1465}
1466
1467
1468// Fast negative check for symbol-to-symbol equality.
1469static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
1470 Register lhs,
1471 Register rhs,
1472 Label* possible_strings,
1473 Label* not_both_strings) {
1474 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1475 (lhs.is(r1) && rhs.is(r0)));
1476
1477 // r2 is object type of rhs.
1478 // Ensure that no non-strings have the symbol bit set.
1479 Label object_test;
1480 STATIC_ASSERT(kSymbolTag != 0);
1481 __ tst(r2, Operand(kIsNotStringMask));
1482 __ b(ne, &object_test);
1483 __ tst(r2, Operand(kIsSymbolMask));
1484 __ b(eq, possible_strings);
1485 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
1486 __ b(ge, not_both_strings);
1487 __ tst(r3, Operand(kIsSymbolMask));
1488 __ b(eq, possible_strings);
1489
1490 // Both are symbols. We already checked they weren't the same pointer
1491 // so they are not equal.
1492 __ mov(r0, Operand(NOT_EQUAL));
1493 __ Ret();
1494
1495 __ bind(&object_test);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001496 __ cmp(r2, Operand(FIRST_SPEC_OBJECT_TYPE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001497 __ b(lt, not_both_strings);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001498 __ CompareObjectType(lhs, r2, r3, FIRST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001499 __ b(lt, not_both_strings);
1500 // If both objects are undetectable, they are equal. Otherwise, they
1501 // are not equal, since they are different objects and an object is not
1502 // equal to undefined.
1503 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
1504 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
1505 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
1506 __ and_(r0, r2, Operand(r3));
1507 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
1508 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
1509 __ Ret();
1510}
1511
1512
1513void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
1514 Register object,
1515 Register result,
1516 Register scratch1,
1517 Register scratch2,
1518 Register scratch3,
1519 bool object_is_smi,
1520 Label* not_found) {
1521 // Use of registers. Register result is used as a temporary.
1522 Register number_string_cache = result;
1523 Register mask = scratch3;
1524
1525 // Load the number string cache.
1526 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
1527
1528 // Make the hash mask from the length of the number string cache. It
1529 // contains two elements (number and string) for each cache entry.
1530 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
1531 // Divide length by two (length is a smi).
1532 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
1533 __ sub(mask, mask, Operand(1)); // Make mask.
1534
1535 // Calculate the entry in the number string cache. The hash value in the
1536 // number string cache for smis is just the smi value, and the hash for
1537 // doubles is the xor of the upper and lower words. See
1538 // Heap::GetNumberStringCache.
Steve Block44f0eee2011-05-26 01:26:41 +01001539 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001540 Label is_smi;
1541 Label load_result_from_cache;
1542 if (!object_is_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01001543 __ JumpIfSmi(object, &is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001544 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001545 CpuFeatures::Scope scope(VFP3);
1546 __ CheckMap(object,
1547 scratch1,
1548 Heap::kHeapNumberMapRootIndex,
1549 not_found,
Ben Murdoch257744e2011-11-30 15:57:28 +00001550 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001551
1552 STATIC_ASSERT(8 == kDoubleSize);
1553 __ add(scratch1,
1554 object,
1555 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
1556 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
1557 __ eor(scratch1, scratch1, Operand(scratch2));
1558 __ and_(scratch1, scratch1, Operand(mask));
1559
1560 // Calculate address of entry in string cache: each entry consists
1561 // of two pointer sized fields.
1562 __ add(scratch1,
1563 number_string_cache,
1564 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
1565
1566 Register probe = mask;
1567 __ ldr(probe,
1568 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
Steve Block1e0659c2011-05-24 12:43:12 +01001569 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001570 __ sub(scratch2, object, Operand(kHeapObjectTag));
1571 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
1572 __ sub(probe, probe, Operand(kHeapObjectTag));
1573 __ vldr(d1, probe, HeapNumber::kValueOffset);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001574 __ VFPCompareAndSetFlags(d0, d1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001575 __ b(ne, not_found); // The cache did not contain this value.
1576 __ b(&load_result_from_cache);
1577 } else {
1578 __ b(not_found);
1579 }
1580 }
1581
1582 __ bind(&is_smi);
1583 Register scratch = scratch1;
1584 __ and_(scratch, mask, Operand(object, ASR, 1));
1585 // Calculate address of entry in string cache: each entry consists
1586 // of two pointer sized fields.
1587 __ add(scratch,
1588 number_string_cache,
1589 Operand(scratch, LSL, kPointerSizeLog2 + 1));
1590
1591 // Check if the entry is the smi we are looking for.
1592 Register probe = mask;
1593 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
1594 __ cmp(object, probe);
1595 __ b(ne, not_found);
1596
1597 // Get the result from the cache.
1598 __ bind(&load_result_from_cache);
1599 __ ldr(result,
1600 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01001601 __ IncrementCounter(isolate->counters()->number_to_string_native(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001602 1,
1603 scratch1,
1604 scratch2);
1605}
1606
1607
1608void NumberToStringStub::Generate(MacroAssembler* masm) {
1609 Label runtime;
1610
1611 __ ldr(r1, MemOperand(sp, 0));
1612
1613 // Generate code to lookup number in the number string cache.
1614 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
1615 __ add(sp, sp, Operand(1 * kPointerSize));
1616 __ Ret();
1617
1618 __ bind(&runtime);
1619 // Handle number to string in the runtime system if not found in the cache.
1620 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
1621}
1622
1623
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001624// On entry lhs_ and rhs_ are the values to be compared.
1625// On exit r0 is 0, positive or negative to indicate the result of
1626// the comparison.
1627void CompareStub::Generate(MacroAssembler* masm) {
1628 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
1629 (lhs_.is(r1) && rhs_.is(r0)));
1630
1631 Label slow; // Call builtin.
1632 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
1633
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001634 if (include_smi_compare_) {
1635 Label not_two_smis, smi_done;
1636 __ orr(r2, r1, r0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001637 __ JumpIfNotSmi(r2, &not_two_smis);
Ben Murdochf87a2032010-10-22 12:50:53 +01001638 __ mov(r1, Operand(r1, ASR, 1));
1639 __ sub(r0, r1, Operand(r0, ASR, 1));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001640 __ Ret();
1641 __ bind(&not_two_smis);
1642 } else if (FLAG_debug_code) {
1643 __ orr(r2, r1, r0);
1644 __ tst(r2, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001645 __ Assert(ne, "CompareStub: unexpected smi operands.");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001646 }
1647
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001648 // NOTICE! This code is only reached after a smi-fast-case check, so
1649 // it is certain that at least one operand isn't a smi.
1650
1651 // Handle the case where the objects are identical. Either returns the answer
1652 // or goes to slow. Only falls through if the objects were not identical.
1653 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
1654
1655 // If either is a Smi (we know that not both are), then they can only
1656 // be strictly equal if the other is a HeapNumber.
1657 STATIC_ASSERT(kSmiTag == 0);
1658 ASSERT_EQ(0, Smi::FromInt(0));
1659 __ and_(r2, lhs_, Operand(rhs_));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001660 __ JumpIfNotSmi(r2, &not_smis);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001661 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
1662 // 1) Return the answer.
1663 // 2) Go to slow.
1664 // 3) Fall through to both_loaded_as_doubles.
1665 // 4) Jump to lhs_not_nan.
1666 // In cases 3 and 4 we have found out we were dealing with a number-number
1667 // comparison. If VFP3 is supported the double values of the numbers have
1668 // been loaded into d7 and d6. Otherwise, the double values have been loaded
1669 // into r0, r1, r2, and r3.
1670 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
1671
1672 __ bind(&both_loaded_as_doubles);
1673 // The arguments have been converted to doubles and stored in d6 and d7, if
1674 // VFP3 is supported, or in r0, r1, r2, and r3.
Steve Block44f0eee2011-05-26 01:26:41 +01001675 Isolate* isolate = masm->isolate();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001676 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001677 __ bind(&lhs_not_nan);
1678 CpuFeatures::Scope scope(VFP3);
1679 Label no_nan;
1680 // ARMv7 VFP3 instructions to implement double precision comparison.
Ben Murdochb8e0da22011-05-16 14:20:40 +01001681 __ VFPCompareAndSetFlags(d7, d6);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001682 Label nan;
1683 __ b(vs, &nan);
1684 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
1685 __ mov(r0, Operand(LESS), LeaveCC, lt);
1686 __ mov(r0, Operand(GREATER), LeaveCC, gt);
1687 __ Ret();
1688
1689 __ bind(&nan);
1690 // If one of the sides was a NaN then the v flag is set. Load r0 with
1691 // whatever it takes to make the comparison fail, since comparisons with NaN
1692 // always fail.
1693 if (cc_ == lt || cc_ == le) {
1694 __ mov(r0, Operand(GREATER));
1695 } else {
1696 __ mov(r0, Operand(LESS));
1697 }
1698 __ Ret();
1699 } else {
1700 // Checks for NaN in the doubles we have loaded. Can return the answer or
1701 // fall through if neither is a NaN. Also binds lhs_not_nan.
1702 EmitNanCheck(masm, &lhs_not_nan, cc_);
1703 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
1704 // answer. Never falls through.
1705 EmitTwoNonNanDoubleComparison(masm, cc_);
1706 }
1707
1708 __ bind(&not_smis);
1709 // At this point we know we are dealing with two different objects,
1710 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
1711 if (strict_) {
1712 // This returns non-equal for some object types, or falls through if it
1713 // was not lucky.
1714 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1715 }
1716
1717 Label check_for_symbols;
1718 Label flat_string_check;
1719 // Check for heap-number-heap-number comparison. Can jump to slow case,
1720 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
1721 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1722 // In this case r2 will contain the type of rhs_. Never falls through.
1723 EmitCheckForTwoHeapNumbers(masm,
1724 lhs_,
1725 rhs_,
1726 &both_loaded_as_doubles,
1727 &check_for_symbols,
1728 &flat_string_check);
1729
1730 __ bind(&check_for_symbols);
1731 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
1732 // symbols.
1733 if (cc_ == eq && !strict_) {
1734 // Returns an answer for two symbols or two detectable objects.
1735 // Otherwise jumps to string case or not both strings case.
1736 // Assumes that r2 is the type of rhs_ on entry.
1737 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1738 }
1739
1740 // Check for both being sequential ASCII strings, and inline if that is the
1741 // case.
1742 __ bind(&flat_string_check);
1743
1744 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
1745
Steve Block44f0eee2011-05-26 01:26:41 +01001746 __ IncrementCounter(isolate->counters()->string_compare_native(), 1, r2, r3);
Ben Murdoch257744e2011-11-30 15:57:28 +00001747 if (cc_ == eq) {
1748 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001749 lhs_,
1750 rhs_,
1751 r2,
1752 r3,
Ben Murdoch257744e2011-11-30 15:57:28 +00001753 r4);
1754 } else {
1755 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1756 lhs_,
1757 rhs_,
1758 r2,
1759 r3,
1760 r4,
1761 r5);
1762 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001763 // Never falls through to here.
1764
1765 __ bind(&slow);
1766
1767 __ Push(lhs_, rhs_);
1768 // Figure out which native to call and setup the arguments.
1769 Builtins::JavaScript native;
1770 if (cc_ == eq) {
1771 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1772 } else {
1773 native = Builtins::COMPARE;
1774 int ncr; // NaN compare result
1775 if (cc_ == lt || cc_ == le) {
1776 ncr = GREATER;
1777 } else {
1778 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
1779 ncr = LESS;
1780 }
1781 __ mov(r0, Operand(Smi::FromInt(ncr)));
1782 __ push(r0);
1783 }
1784
1785 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1786 // tagged as a small integer.
Ben Murdoch257744e2011-11-30 15:57:28 +00001787 __ InvokeBuiltin(native, JUMP_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001788}
1789
1790
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001791// The stub expects its argument in the tos_ register and returns its result in
1792// it, too: zero for false, and a non-zero value for true.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001793void ToBooleanStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001794 // This stub overrides SometimesSetsUpAFrame() to return false. That means
1795 // we cannot call anything that could cause a GC from this stub.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001796 // This stub uses VFP3 instructions.
Ben Murdoch257744e2011-11-30 15:57:28 +00001797 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001798
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001799 Label patch;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001800 const Register map = r9.is(tos_) ? r7 : r9;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001801
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001802 // undefined -> false.
1803 CheckOddball(masm, UNDEFINED, Heap::kUndefinedValueRootIndex, false);
Ben Murdoch257744e2011-11-30 15:57:28 +00001804
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001805 // Boolean -> its value.
1806 CheckOddball(masm, BOOLEAN, Heap::kFalseValueRootIndex, false);
1807 CheckOddball(masm, BOOLEAN, Heap::kTrueValueRootIndex, true);
Ben Murdoch257744e2011-11-30 15:57:28 +00001808
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001809 // 'null' -> false.
1810 CheckOddball(masm, NULL_TYPE, Heap::kNullValueRootIndex, false);
Ben Murdoch257744e2011-11-30 15:57:28 +00001811
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001812 if (types_.Contains(SMI)) {
1813 // Smis: 0 -> false, all other -> true
1814 __ tst(tos_, Operand(kSmiTagMask));
1815 // tos_ contains the correct return value already
1816 __ Ret(eq);
1817 } else if (types_.NeedsMap()) {
1818 // If we need a map later and have a Smi -> patch.
1819 __ JumpIfSmi(tos_, &patch);
1820 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001821
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001822 if (types_.NeedsMap()) {
1823 __ ldr(map, FieldMemOperand(tos_, HeapObject::kMapOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001824
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001825 if (types_.CanBeUndetectable()) {
1826 __ ldrb(ip, FieldMemOperand(map, Map::kBitFieldOffset));
1827 __ tst(ip, Operand(1 << Map::kIsUndetectable));
1828 // Undetectable -> false.
1829 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, ne);
1830 __ Ret(ne);
1831 }
1832 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001833
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001834 if (types_.Contains(SPEC_OBJECT)) {
1835 // Spec object -> true.
1836 __ CompareInstanceType(map, ip, FIRST_SPEC_OBJECT_TYPE);
1837 // tos_ contains the correct non-zero return value already.
1838 __ Ret(ge);
1839 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001840
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001841 if (types_.Contains(STRING)) {
1842 // String value -> false iff empty.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001843 __ CompareInstanceType(map, ip, FIRST_NONSTRING_TYPE);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001844 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset), lt);
1845 __ Ret(lt); // the string length is OK as the return value
1846 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001847
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001848 if (types_.Contains(HEAP_NUMBER)) {
1849 // Heap number -> false iff +0, -0, or NaN.
1850 Label not_heap_number;
1851 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
1852 __ b(ne, &not_heap_number);
1853 __ vldr(d1, FieldMemOperand(tos_, HeapNumber::kValueOffset));
1854 __ VFPCompareAndSetFlags(d1, 0.0);
1855 // "tos_" is a register, and contains a non zero value by default.
1856 // Hence we only need to overwrite "tos_" with zero to return false for
1857 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
1858 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, eq); // for FP_ZERO
1859 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, vs); // for FP_NAN
1860 __ Ret();
1861 __ bind(&not_heap_number);
1862 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001863
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001864 __ bind(&patch);
1865 GenerateTypeTransition(masm);
1866}
1867
1868
1869void ToBooleanStub::CheckOddball(MacroAssembler* masm,
1870 Type type,
1871 Heap::RootListIndex value,
1872 bool result) {
1873 if (types_.Contains(type)) {
1874 // If we see an expected oddball, return its ToBoolean value tos_.
1875 __ LoadRoot(ip, value);
1876 __ cmp(tos_, ip);
1877 // The value of a root is never NULL, so we can avoid loading a non-null
1878 // value into tos_ when we want to return 'true'.
1879 if (!result) {
1880 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, eq);
1881 }
1882 __ Ret(eq);
1883 }
1884}
1885
1886
1887void ToBooleanStub::GenerateTypeTransition(MacroAssembler* masm) {
1888 if (!tos_.is(r3)) {
1889 __ mov(r3, Operand(tos_));
1890 }
1891 __ mov(r2, Operand(Smi::FromInt(tos_.code())));
1892 __ mov(r1, Operand(Smi::FromInt(types_.ToByte())));
1893 __ Push(r3, r2, r1);
1894 // Patch the caller to an appropriate specialized stub and return the
1895 // operation result to the caller of the stub.
1896 __ TailCallExternalReference(
1897 ExternalReference(IC_Utility(IC::kToBoolean_Patch), masm->isolate()),
1898 3,
1899 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001900}
1901
1902
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001903void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
1904 // We don't allow a GC during a store buffer overflow so there is no need to
1905 // store the registers in any particular way, but we do have to store and
1906 // restore them.
1907 __ stm(db_w, sp, kCallerSaved | lr.bit());
1908 if (save_doubles_ == kSaveFPRegs) {
1909 CpuFeatures::Scope scope(VFP3);
1910 __ sub(sp, sp, Operand(kDoubleSize * DwVfpRegister::kNumRegisters));
1911 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
1912 DwVfpRegister reg = DwVfpRegister::from_code(i);
1913 __ vstr(reg, MemOperand(sp, i * kDoubleSize));
1914 }
1915 }
1916 const int argument_count = 1;
1917 const int fp_argument_count = 0;
1918 const Register scratch = r1;
1919
1920 AllowExternalCallThatCantCauseGC scope(masm);
1921 __ PrepareCallCFunction(argument_count, fp_argument_count, scratch);
1922 __ mov(r0, Operand(ExternalReference::isolate_address()));
1923 __ CallCFunction(
1924 ExternalReference::store_buffer_overflow_function(masm->isolate()),
1925 argument_count);
1926 if (save_doubles_ == kSaveFPRegs) {
1927 CpuFeatures::Scope scope(VFP3);
1928 for (int i = 0; i < DwVfpRegister::kNumRegisters; i++) {
1929 DwVfpRegister reg = DwVfpRegister::from_code(i);
1930 __ vldr(reg, MemOperand(sp, i * kDoubleSize));
1931 }
1932 __ add(sp, sp, Operand(kDoubleSize * DwVfpRegister::kNumRegisters));
1933 }
1934 __ ldm(ia_w, sp, kCallerSaved | pc.bit()); // Also pop pc to get Ret(0).
1935}
1936
1937
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001938void UnaryOpStub::PrintName(StringStream* stream) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001939 const char* op_name = Token::Name(op_);
1940 const char* overwrite_name = NULL; // Make g++ happy.
1941 switch (mode_) {
1942 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
1943 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
1944 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001945 stream->Add("UnaryOpStub_%s_%s_%s",
1946 op_name,
1947 overwrite_name,
1948 UnaryOpIC::GetName(operand_type_));
Ben Murdoch257744e2011-11-30 15:57:28 +00001949}
1950
1951
1952// TODO(svenpanne): Use virtual functions instead of switch.
1953void UnaryOpStub::Generate(MacroAssembler* masm) {
1954 switch (operand_type_) {
1955 case UnaryOpIC::UNINITIALIZED:
1956 GenerateTypeTransition(masm);
1957 break;
1958 case UnaryOpIC::SMI:
1959 GenerateSmiStub(masm);
1960 break;
1961 case UnaryOpIC::HEAP_NUMBER:
1962 GenerateHeapNumberStub(masm);
1963 break;
1964 case UnaryOpIC::GENERIC:
1965 GenerateGenericStub(masm);
1966 break;
1967 }
1968}
1969
1970
1971void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001972 __ mov(r3, Operand(r0)); // the operand
1973 __ mov(r2, Operand(Smi::FromInt(op_)));
1974 __ mov(r1, Operand(Smi::FromInt(mode_)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001975 __ mov(r0, Operand(Smi::FromInt(operand_type_)));
Ben Murdoch257744e2011-11-30 15:57:28 +00001976 __ Push(r3, r2, r1, r0);
1977
1978 __ TailCallExternalReference(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001979 ExternalReference(IC_Utility(IC::kUnaryOp_Patch), masm->isolate()), 4, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001980}
1981
1982
1983// TODO(svenpanne): Use virtual functions instead of switch.
1984void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1985 switch (op_) {
1986 case Token::SUB:
1987 GenerateSmiStubSub(masm);
1988 break;
1989 case Token::BIT_NOT:
1990 GenerateSmiStubBitNot(masm);
1991 break;
1992 default:
1993 UNREACHABLE();
1994 }
1995}
1996
1997
1998void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
1999 Label non_smi, slow;
2000 GenerateSmiCodeSub(masm, &non_smi, &slow);
2001 __ bind(&non_smi);
2002 __ bind(&slow);
2003 GenerateTypeTransition(masm);
2004}
2005
2006
2007void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
2008 Label non_smi;
2009 GenerateSmiCodeBitNot(masm, &non_smi);
2010 __ bind(&non_smi);
2011 GenerateTypeTransition(masm);
2012}
2013
2014
2015void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
2016 Label* non_smi,
2017 Label* slow) {
2018 __ JumpIfNotSmi(r0, non_smi);
2019
2020 // The result of negating zero or the smallest negative smi is not a smi.
2021 __ bic(ip, r0, Operand(0x80000000), SetCC);
2022 __ b(eq, slow);
2023
2024 // Return '0 - value'.
2025 __ rsb(r0, r0, Operand(0, RelocInfo::NONE));
2026 __ Ret();
2027}
2028
2029
2030void UnaryOpStub::GenerateSmiCodeBitNot(MacroAssembler* masm,
2031 Label* non_smi) {
2032 __ JumpIfNotSmi(r0, non_smi);
2033
2034 // Flip bits and revert inverted smi-tag.
2035 __ mvn(r0, Operand(r0));
2036 __ bic(r0, r0, Operand(kSmiTagMask));
2037 __ Ret();
2038}
2039
2040
2041// TODO(svenpanne): Use virtual functions instead of switch.
2042void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
2043 switch (op_) {
2044 case Token::SUB:
2045 GenerateHeapNumberStubSub(masm);
2046 break;
2047 case Token::BIT_NOT:
2048 GenerateHeapNumberStubBitNot(masm);
2049 break;
2050 default:
2051 UNREACHABLE();
2052 }
2053}
2054
2055
2056void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
2057 Label non_smi, slow, call_builtin;
2058 GenerateSmiCodeSub(masm, &non_smi, &call_builtin);
2059 __ bind(&non_smi);
2060 GenerateHeapNumberCodeSub(masm, &slow);
2061 __ bind(&slow);
2062 GenerateTypeTransition(masm);
2063 __ bind(&call_builtin);
2064 GenerateGenericCodeFallback(masm);
2065}
2066
2067
2068void UnaryOpStub::GenerateHeapNumberStubBitNot(MacroAssembler* masm) {
2069 Label non_smi, slow;
2070 GenerateSmiCodeBitNot(masm, &non_smi);
2071 __ bind(&non_smi);
2072 GenerateHeapNumberCodeBitNot(masm, &slow);
2073 __ bind(&slow);
2074 GenerateTypeTransition(masm);
2075}
2076
2077void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
2078 Label* slow) {
2079 EmitCheckForHeapNumber(masm, r0, r1, r6, slow);
2080 // r0 is a heap number. Get a new heap number in r1.
2081 if (mode_ == UNARY_OVERWRITE) {
2082 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2083 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
2084 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2085 } else {
2086 Label slow_allocate_heapnumber, heapnumber_allocated;
2087 __ AllocateHeapNumber(r1, r2, r3, r6, &slow_allocate_heapnumber);
2088 __ jmp(&heapnumber_allocated);
2089
2090 __ bind(&slow_allocate_heapnumber);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002091 {
2092 FrameScope scope(masm, StackFrame::INTERNAL);
2093 __ push(r0);
2094 __ CallRuntime(Runtime::kNumberAlloc, 0);
2095 __ mov(r1, Operand(r0));
2096 __ pop(r0);
2097 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002098
2099 __ bind(&heapnumber_allocated);
2100 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
2101 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
2102 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
2103 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
2104 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
2105 __ mov(r0, Operand(r1));
2106 }
2107 __ Ret();
2108}
2109
2110
2111void UnaryOpStub::GenerateHeapNumberCodeBitNot(
2112 MacroAssembler* masm, Label* slow) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002113 Label impossible;
2114
Ben Murdoch257744e2011-11-30 15:57:28 +00002115 EmitCheckForHeapNumber(masm, r0, r1, r6, slow);
2116 // Convert the heap number is r0 to an untagged integer in r1.
2117 __ ConvertToInt32(r0, r1, r2, r3, d0, slow);
2118
2119 // Do the bitwise operation and check if the result fits in a smi.
2120 Label try_float;
2121 __ mvn(r1, Operand(r1));
2122 __ add(r2, r1, Operand(0x40000000), SetCC);
2123 __ b(mi, &try_float);
2124
2125 // Tag the result as a smi and we're done.
2126 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
2127 __ Ret();
2128
2129 // Try to store the result in a heap number.
2130 __ bind(&try_float);
2131 if (mode_ == UNARY_NO_OVERWRITE) {
2132 Label slow_allocate_heapnumber, heapnumber_allocated;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002133 // Allocate a new heap number without zapping r0, which we need if it fails.
2134 __ AllocateHeapNumber(r2, r3, r4, r6, &slow_allocate_heapnumber);
Ben Murdoch257744e2011-11-30 15:57:28 +00002135 __ jmp(&heapnumber_allocated);
2136
2137 __ bind(&slow_allocate_heapnumber);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002138 {
2139 FrameScope scope(masm, StackFrame::INTERNAL);
2140 __ push(r0); // Push the heap number, not the untagged int32.
2141 __ CallRuntime(Runtime::kNumberAlloc, 0);
2142 __ mov(r2, r0); // Move the new heap number into r2.
2143 // Get the heap number into r0, now that the new heap number is in r2.
2144 __ pop(r0);
2145 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002146
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002147 // Convert the heap number in r0 to an untagged integer in r1.
2148 // This can't go slow-case because it's the same number we already
2149 // converted once again.
2150 __ ConvertToInt32(r0, r1, r3, r4, d0, &impossible);
2151 __ mvn(r1, Operand(r1));
2152
Ben Murdoch257744e2011-11-30 15:57:28 +00002153 __ bind(&heapnumber_allocated);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002154 __ mov(r0, r2); // Move newly allocated heap number to r0.
Ben Murdoch257744e2011-11-30 15:57:28 +00002155 }
2156
2157 if (CpuFeatures::IsSupported(VFP3)) {
2158 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
2159 CpuFeatures::Scope scope(VFP3);
2160 __ vmov(s0, r1);
2161 __ vcvt_f64_s32(d0, s0);
2162 __ sub(r2, r0, Operand(kHeapObjectTag));
2163 __ vstr(d0, r2, HeapNumber::kValueOffset);
2164 __ Ret();
2165 } else {
2166 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
2167 // have to set up a frame.
2168 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
2169 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
2170 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002171
2172 __ bind(&impossible);
2173 if (FLAG_debug_code) {
2174 __ stop("Incorrect assumption in bit-not stub");
2175 }
Ben Murdoch257744e2011-11-30 15:57:28 +00002176}
2177
2178
2179// TODO(svenpanne): Use virtual functions instead of switch.
2180void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
2181 switch (op_) {
2182 case Token::SUB:
2183 GenerateGenericStubSub(masm);
2184 break;
2185 case Token::BIT_NOT:
2186 GenerateGenericStubBitNot(masm);
2187 break;
2188 default:
2189 UNREACHABLE();
2190 }
2191}
2192
2193
2194void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
2195 Label non_smi, slow;
2196 GenerateSmiCodeSub(masm, &non_smi, &slow);
2197 __ bind(&non_smi);
2198 GenerateHeapNumberCodeSub(masm, &slow);
2199 __ bind(&slow);
2200 GenerateGenericCodeFallback(masm);
2201}
2202
2203
2204void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
2205 Label non_smi, slow;
2206 GenerateSmiCodeBitNot(masm, &non_smi);
2207 __ bind(&non_smi);
2208 GenerateHeapNumberCodeBitNot(masm, &slow);
2209 __ bind(&slow);
2210 GenerateGenericCodeFallback(masm);
2211}
2212
2213
2214void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
2215 // Handle the slow case by jumping to the JavaScript builtin.
2216 __ push(r0);
2217 switch (op_) {
2218 case Token::SUB:
2219 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
2220 break;
2221 case Token::BIT_NOT:
2222 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
2223 break;
2224 default:
2225 UNREACHABLE();
2226 }
2227}
2228
2229
Ben Murdoch257744e2011-11-30 15:57:28 +00002230void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002231 Label get_result;
2232
2233 __ Push(r1, r0);
2234
2235 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
2236 __ mov(r1, Operand(Smi::FromInt(op_)));
2237 __ mov(r0, Operand(Smi::FromInt(operands_type_)));
2238 __ Push(r2, r1, r0);
2239
2240 __ TailCallExternalReference(
Ben Murdoch257744e2011-11-30 15:57:28 +00002241 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
Steve Block44f0eee2011-05-26 01:26:41 +01002242 masm->isolate()),
Steve Block1e0659c2011-05-24 12:43:12 +01002243 5,
2244 1);
2245}
2246
2247
Ben Murdoch257744e2011-11-30 15:57:28 +00002248void BinaryOpStub::GenerateTypeTransitionWithSavedArgs(
Steve Block1e0659c2011-05-24 12:43:12 +01002249 MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002250 UNIMPLEMENTED();
Steve Block1e0659c2011-05-24 12:43:12 +01002251}
2252
2253
Ben Murdoch257744e2011-11-30 15:57:28 +00002254void BinaryOpStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002255 // Explicitly allow generation of nested stubs. It is safe here because
2256 // generation code does not use any raw pointers.
2257 AllowStubCallsScope allow_stub_calls(masm, true);
2258
Steve Block1e0659c2011-05-24 12:43:12 +01002259 switch (operands_type_) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002260 case BinaryOpIC::UNINITIALIZED:
Steve Block1e0659c2011-05-24 12:43:12 +01002261 GenerateTypeTransition(masm);
2262 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002263 case BinaryOpIC::SMI:
Steve Block1e0659c2011-05-24 12:43:12 +01002264 GenerateSmiStub(masm);
2265 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002266 case BinaryOpIC::INT32:
Steve Block1e0659c2011-05-24 12:43:12 +01002267 GenerateInt32Stub(masm);
2268 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002269 case BinaryOpIC::HEAP_NUMBER:
Steve Block1e0659c2011-05-24 12:43:12 +01002270 GenerateHeapNumberStub(masm);
2271 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002272 case BinaryOpIC::ODDBALL:
Steve Block44f0eee2011-05-26 01:26:41 +01002273 GenerateOddballStub(masm);
2274 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002275 case BinaryOpIC::BOTH_STRING:
2276 GenerateBothStringStub(masm);
2277 break;
2278 case BinaryOpIC::STRING:
Steve Block1e0659c2011-05-24 12:43:12 +01002279 GenerateStringStub(masm);
2280 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002281 case BinaryOpIC::GENERIC:
Steve Block1e0659c2011-05-24 12:43:12 +01002282 GenerateGeneric(masm);
2283 break;
2284 default:
2285 UNREACHABLE();
2286 }
2287}
2288
2289
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002290void BinaryOpStub::PrintName(StringStream* stream) {
Steve Block1e0659c2011-05-24 12:43:12 +01002291 const char* op_name = Token::Name(op_);
2292 const char* overwrite_name;
2293 switch (mode_) {
2294 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
2295 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
2296 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
2297 default: overwrite_name = "UnknownOverwrite"; break;
2298 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002299 stream->Add("BinaryOpStub_%s_%s_%s",
2300 op_name,
2301 overwrite_name,
2302 BinaryOpIC::GetName(operands_type_));
Steve Block1e0659c2011-05-24 12:43:12 +01002303}
2304
2305
Ben Murdoch257744e2011-11-30 15:57:28 +00002306void BinaryOpStub::GenerateSmiSmiOperation(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002307 Register left = r1;
2308 Register right = r0;
2309 Register scratch1 = r7;
2310 Register scratch2 = r9;
2311
2312 ASSERT(right.is(r0));
2313 STATIC_ASSERT(kSmiTag == 0);
2314
2315 Label not_smi_result;
2316 switch (op_) {
2317 case Token::ADD:
2318 __ add(right, left, Operand(right), SetCC); // Add optimistically.
2319 __ Ret(vc);
2320 __ sub(right, right, Operand(left)); // Revert optimistic add.
2321 break;
2322 case Token::SUB:
2323 __ sub(right, left, Operand(right), SetCC); // Subtract optimistically.
2324 __ Ret(vc);
2325 __ sub(right, left, Operand(right)); // Revert optimistic subtract.
2326 break;
2327 case Token::MUL:
2328 // Remove tag from one of the operands. This way the multiplication result
2329 // will be a smi if it fits the smi range.
2330 __ SmiUntag(ip, right);
2331 // Do multiplication
2332 // scratch1 = lower 32 bits of ip * left.
2333 // scratch2 = higher 32 bits of ip * left.
2334 __ smull(scratch1, scratch2, left, ip);
2335 // Check for overflowing the smi range - no overflow if higher 33 bits of
2336 // the result are identical.
2337 __ mov(ip, Operand(scratch1, ASR, 31));
2338 __ cmp(ip, Operand(scratch2));
2339 __ b(ne, &not_smi_result);
2340 // Go slow on zero result to handle -0.
2341 __ tst(scratch1, Operand(scratch1));
2342 __ mov(right, Operand(scratch1), LeaveCC, ne);
2343 __ Ret(ne);
2344 // We need -0 if we were multiplying a negative number with 0 to get 0.
2345 // We know one of them was zero.
2346 __ add(scratch2, right, Operand(left), SetCC);
2347 __ mov(right, Operand(Smi::FromInt(0)), LeaveCC, pl);
2348 __ Ret(pl); // Return smi 0 if the non-zero one was positive.
2349 // We fall through here if we multiplied a negative number with 0, because
2350 // that would mean we should produce -0.
2351 break;
2352 case Token::DIV:
2353 // Check for power of two on the right hand side.
2354 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
2355 // Check for positive and no remainder (scratch1 contains right - 1).
2356 __ orr(scratch2, scratch1, Operand(0x80000000u));
2357 __ tst(left, scratch2);
2358 __ b(ne, &not_smi_result);
2359
2360 // Perform division by shifting.
2361 __ CountLeadingZeros(scratch1, scratch1, scratch2);
2362 __ rsb(scratch1, scratch1, Operand(31));
2363 __ mov(right, Operand(left, LSR, scratch1));
2364 __ Ret();
2365 break;
2366 case Token::MOD:
2367 // Check for two positive smis.
2368 __ orr(scratch1, left, Operand(right));
2369 __ tst(scratch1, Operand(0x80000000u | kSmiTagMask));
2370 __ b(ne, &not_smi_result);
2371
2372 // Check for power of two on the right hand side.
2373 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
2374
2375 // Perform modulus by masking.
2376 __ and_(right, left, Operand(scratch1));
2377 __ Ret();
2378 break;
2379 case Token::BIT_OR:
2380 __ orr(right, left, Operand(right));
2381 __ Ret();
2382 break;
2383 case Token::BIT_AND:
2384 __ and_(right, left, Operand(right));
2385 __ Ret();
2386 break;
2387 case Token::BIT_XOR:
2388 __ eor(right, left, Operand(right));
2389 __ Ret();
2390 break;
2391 case Token::SAR:
2392 // Remove tags from right operand.
2393 __ GetLeastBitsFromSmi(scratch1, right, 5);
2394 __ mov(right, Operand(left, ASR, scratch1));
2395 // Smi tag result.
2396 __ bic(right, right, Operand(kSmiTagMask));
2397 __ Ret();
2398 break;
2399 case Token::SHR:
2400 // Remove tags from operands. We can't do this on a 31 bit number
2401 // because then the 0s get shifted into bit 30 instead of bit 31.
2402 __ SmiUntag(scratch1, left);
2403 __ GetLeastBitsFromSmi(scratch2, right, 5);
2404 __ mov(scratch1, Operand(scratch1, LSR, scratch2));
2405 // Unsigned shift is not allowed to produce a negative number, so
2406 // check the sign bit and the sign bit after Smi tagging.
2407 __ tst(scratch1, Operand(0xc0000000));
2408 __ b(ne, &not_smi_result);
2409 // Smi tag result.
2410 __ SmiTag(right, scratch1);
2411 __ Ret();
2412 break;
2413 case Token::SHL:
2414 // Remove tags from operands.
2415 __ SmiUntag(scratch1, left);
2416 __ GetLeastBitsFromSmi(scratch2, right, 5);
2417 __ mov(scratch1, Operand(scratch1, LSL, scratch2));
2418 // Check that the signed result fits in a Smi.
2419 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
2420 __ b(mi, &not_smi_result);
2421 __ SmiTag(right, scratch1);
2422 __ Ret();
2423 break;
2424 default:
2425 UNREACHABLE();
2426 }
2427 __ bind(&not_smi_result);
2428}
2429
2430
Ben Murdoch257744e2011-11-30 15:57:28 +00002431void BinaryOpStub::GenerateFPOperation(MacroAssembler* masm,
2432 bool smi_operands,
2433 Label* not_numbers,
2434 Label* gc_required) {
Steve Block1e0659c2011-05-24 12:43:12 +01002435 Register left = r1;
2436 Register right = r0;
2437 Register scratch1 = r7;
2438 Register scratch2 = r9;
Steve Block44f0eee2011-05-26 01:26:41 +01002439 Register scratch3 = r4;
Steve Block1e0659c2011-05-24 12:43:12 +01002440
2441 ASSERT(smi_operands || (not_numbers != NULL));
2442 if (smi_operands && FLAG_debug_code) {
2443 __ AbortIfNotSmi(left);
2444 __ AbortIfNotSmi(right);
2445 }
2446
2447 Register heap_number_map = r6;
2448 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2449
2450 switch (op_) {
2451 case Token::ADD:
2452 case Token::SUB:
2453 case Token::MUL:
2454 case Token::DIV:
2455 case Token::MOD: {
2456 // Load left and right operands into d6 and d7 or r0/r1 and r2/r3
2457 // depending on whether VFP3 is available or not.
2458 FloatingPointHelper::Destination destination =
Ben Murdoch8b112d22011-06-08 16:22:53 +01002459 CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +01002460 op_ != Token::MOD ?
Steve Block1e0659c2011-05-24 12:43:12 +01002461 FloatingPointHelper::kVFPRegisters :
2462 FloatingPointHelper::kCoreRegisters;
2463
2464 // Allocate new heap number for result.
2465 Register result = r5;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002466 GenerateHeapResultAllocation(
2467 masm, result, heap_number_map, scratch1, scratch2, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01002468
2469 // Load the operands.
2470 if (smi_operands) {
2471 FloatingPointHelper::LoadSmis(masm, destination, scratch1, scratch2);
2472 } else {
2473 FloatingPointHelper::LoadOperands(masm,
2474 destination,
2475 heap_number_map,
2476 scratch1,
2477 scratch2,
2478 not_numbers);
2479 }
2480
2481 // Calculate the result.
2482 if (destination == FloatingPointHelper::kVFPRegisters) {
2483 // Using VFP registers:
2484 // d6: Left value
2485 // d7: Right value
2486 CpuFeatures::Scope scope(VFP3);
2487 switch (op_) {
2488 case Token::ADD:
2489 __ vadd(d5, d6, d7);
2490 break;
2491 case Token::SUB:
2492 __ vsub(d5, d6, d7);
2493 break;
2494 case Token::MUL:
2495 __ vmul(d5, d6, d7);
2496 break;
2497 case Token::DIV:
2498 __ vdiv(d5, d6, d7);
2499 break;
2500 default:
2501 UNREACHABLE();
2502 }
2503
2504 __ sub(r0, result, Operand(kHeapObjectTag));
2505 __ vstr(d5, r0, HeapNumber::kValueOffset);
2506 __ add(r0, r0, Operand(kHeapObjectTag));
2507 __ Ret();
2508 } else {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002509 // Call the C function to handle the double operation.
2510 FloatingPointHelper::CallCCodeForDoubleOperation(masm,
2511 op_,
2512 result,
2513 scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002514 if (FLAG_debug_code) {
2515 __ stop("Unreachable code.");
2516 }
Steve Block1e0659c2011-05-24 12:43:12 +01002517 }
2518 break;
2519 }
2520 case Token::BIT_OR:
2521 case Token::BIT_XOR:
2522 case Token::BIT_AND:
2523 case Token::SAR:
2524 case Token::SHR:
2525 case Token::SHL: {
2526 if (smi_operands) {
2527 __ SmiUntag(r3, left);
2528 __ SmiUntag(r2, right);
2529 } else {
2530 // Convert operands to 32-bit integers. Right in r2 and left in r3.
Steve Block44f0eee2011-05-26 01:26:41 +01002531 FloatingPointHelper::ConvertNumberToInt32(masm,
2532 left,
2533 r3,
2534 heap_number_map,
2535 scratch1,
2536 scratch2,
2537 scratch3,
2538 d0,
2539 not_numbers);
2540 FloatingPointHelper::ConvertNumberToInt32(masm,
2541 right,
2542 r2,
2543 heap_number_map,
2544 scratch1,
2545 scratch2,
2546 scratch3,
2547 d0,
2548 not_numbers);
Steve Block1e0659c2011-05-24 12:43:12 +01002549 }
2550
2551 Label result_not_a_smi;
2552 switch (op_) {
2553 case Token::BIT_OR:
2554 __ orr(r2, r3, Operand(r2));
2555 break;
2556 case Token::BIT_XOR:
2557 __ eor(r2, r3, Operand(r2));
2558 break;
2559 case Token::BIT_AND:
2560 __ and_(r2, r3, Operand(r2));
2561 break;
2562 case Token::SAR:
2563 // Use only the 5 least significant bits of the shift count.
Steve Block1e0659c2011-05-24 12:43:12 +01002564 __ GetLeastBitsFromInt32(r2, r2, 5);
2565 __ mov(r2, Operand(r3, ASR, r2));
2566 break;
2567 case Token::SHR:
2568 // Use only the 5 least significant bits of the shift count.
2569 __ GetLeastBitsFromInt32(r2, r2, 5);
2570 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2571 // SHR is special because it is required to produce a positive answer.
2572 // The code below for writing into heap numbers isn't capable of
2573 // writing the register as an unsigned int so we go to slow case if we
2574 // hit this case.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002575 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002576 __ b(mi, &result_not_a_smi);
2577 } else {
2578 __ b(mi, not_numbers);
2579 }
2580 break;
2581 case Token::SHL:
2582 // Use only the 5 least significant bits of the shift count.
2583 __ GetLeastBitsFromInt32(r2, r2, 5);
2584 __ mov(r2, Operand(r3, LSL, r2));
2585 break;
2586 default:
2587 UNREACHABLE();
2588 }
2589
2590 // Check that the *signed* result fits in a smi.
2591 __ add(r3, r2, Operand(0x40000000), SetCC);
2592 __ b(mi, &result_not_a_smi);
2593 __ SmiTag(r0, r2);
2594 __ Ret();
2595
2596 // Allocate new heap number for result.
2597 __ bind(&result_not_a_smi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002598 Register result = r5;
2599 if (smi_operands) {
2600 __ AllocateHeapNumber(
2601 result, scratch1, scratch2, heap_number_map, gc_required);
2602 } else {
2603 GenerateHeapResultAllocation(
2604 masm, result, heap_number_map, scratch1, scratch2, gc_required);
2605 }
Steve Block1e0659c2011-05-24 12:43:12 +01002606
2607 // r2: Answer as signed int32.
2608 // r5: Heap number to write answer into.
2609
2610 // Nothing can go wrong now, so move the heap number to r0, which is the
2611 // result.
2612 __ mov(r0, Operand(r5));
2613
Ben Murdoch8b112d22011-06-08 16:22:53 +01002614 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002615 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted. As
2616 // mentioned above SHR needs to always produce a positive result.
2617 CpuFeatures::Scope scope(VFP3);
2618 __ vmov(s0, r2);
2619 if (op_ == Token::SHR) {
2620 __ vcvt_f64_u32(d0, s0);
2621 } else {
2622 __ vcvt_f64_s32(d0, s0);
2623 }
2624 __ sub(r3, r0, Operand(kHeapObjectTag));
2625 __ vstr(d0, r3, HeapNumber::kValueOffset);
2626 __ Ret();
2627 } else {
2628 // Tail call that writes the int32 in r2 to the heap number in r0, using
2629 // r3 as scratch. r0 is preserved and returned.
2630 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
2631 __ TailCallStub(&stub);
2632 }
2633 break;
2634 }
2635 default:
2636 UNREACHABLE();
2637 }
2638}
2639
2640
2641// Generate the smi code. If the operation on smis are successful this return is
2642// generated. If the result is not a smi and heap number allocation is not
2643// requested the code falls through. If number allocation is requested but a
2644// heap number cannot be allocated the code jumps to the lable gc_required.
Ben Murdoch257744e2011-11-30 15:57:28 +00002645void BinaryOpStub::GenerateSmiCode(
2646 MacroAssembler* masm,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002647 Label* use_runtime,
Steve Block1e0659c2011-05-24 12:43:12 +01002648 Label* gc_required,
2649 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
2650 Label not_smis;
2651
2652 Register left = r1;
2653 Register right = r0;
2654 Register scratch1 = r7;
Steve Block1e0659c2011-05-24 12:43:12 +01002655
2656 // Perform combined smi check on both operands.
2657 __ orr(scratch1, left, Operand(right));
2658 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002659 __ JumpIfNotSmi(scratch1, &not_smis);
Steve Block1e0659c2011-05-24 12:43:12 +01002660
2661 // If the smi-smi operation results in a smi return is generated.
2662 GenerateSmiSmiOperation(masm);
2663
2664 // If heap number results are possible generate the result in an allocated
2665 // heap number.
2666 if (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002667 GenerateFPOperation(masm, true, use_runtime, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01002668 }
2669 __ bind(&not_smis);
2670}
2671
2672
Ben Murdoch257744e2011-11-30 15:57:28 +00002673void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002674 Label not_smis, call_runtime;
2675
Ben Murdoch257744e2011-11-30 15:57:28 +00002676 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
2677 result_type_ == BinaryOpIC::SMI) {
Steve Block1e0659c2011-05-24 12:43:12 +01002678 // Only allow smi results.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002679 GenerateSmiCode(masm, &call_runtime, NULL, NO_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002680 } else {
2681 // Allow heap number result and don't make a transition if a heap number
2682 // cannot be allocated.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002683 GenerateSmiCode(masm,
2684 &call_runtime,
2685 &call_runtime,
2686 ALLOW_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002687 }
2688
2689 // Code falls through if the result is not returned as either a smi or heap
2690 // number.
2691 GenerateTypeTransition(masm);
2692
2693 __ bind(&call_runtime);
2694 GenerateCallRuntime(masm);
2695}
2696
2697
Ben Murdoch257744e2011-11-30 15:57:28 +00002698void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
2699 ASSERT(operands_type_ == BinaryOpIC::STRING);
Steve Block1e0659c2011-05-24 12:43:12 +01002700 ASSERT(op_ == Token::ADD);
2701 // Try to add arguments as strings, otherwise, transition to the generic
Ben Murdoch257744e2011-11-30 15:57:28 +00002702 // BinaryOpIC type.
Steve Block1e0659c2011-05-24 12:43:12 +01002703 GenerateAddStrings(masm);
2704 GenerateTypeTransition(masm);
2705}
2706
2707
Ben Murdoch257744e2011-11-30 15:57:28 +00002708void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
2709 Label call_runtime;
2710 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
2711 ASSERT(op_ == Token::ADD);
2712 // If both arguments are strings, call the string add stub.
2713 // Otherwise, do a transition.
2714
2715 // Registers containing left and right operands respectively.
2716 Register left = r1;
2717 Register right = r0;
2718
2719 // Test if left operand is a string.
2720 __ JumpIfSmi(left, &call_runtime);
2721 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
2722 __ b(ge, &call_runtime);
2723
2724 // Test if right operand is a string.
2725 __ JumpIfSmi(right, &call_runtime);
2726 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
2727 __ b(ge, &call_runtime);
2728
2729 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
2730 GenerateRegisterArgsPush(masm);
2731 __ TailCallStub(&string_add_stub);
2732
2733 __ bind(&call_runtime);
2734 GenerateTypeTransition(masm);
2735}
2736
2737
2738void BinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
2739 ASSERT(operands_type_ == BinaryOpIC::INT32);
Steve Block1e0659c2011-05-24 12:43:12 +01002740
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002741 Register left = r1;
2742 Register right = r0;
2743 Register scratch1 = r7;
2744 Register scratch2 = r9;
2745 DwVfpRegister double_scratch = d0;
2746 SwVfpRegister single_scratch = s3;
2747
2748 Register heap_number_result = no_reg;
2749 Register heap_number_map = r6;
2750 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2751
2752 Label call_runtime;
2753 // Labels for type transition, used for wrong input or output types.
2754 // Both label are currently actually bound to the same position. We use two
2755 // different label to differentiate the cause leading to type transition.
2756 Label transition;
2757
2758 // Smi-smi fast case.
2759 Label skip;
2760 __ orr(scratch1, left, right);
2761 __ JumpIfNotSmi(scratch1, &skip);
2762 GenerateSmiSmiOperation(masm);
2763 // Fall through if the result is not a smi.
2764 __ bind(&skip);
2765
2766 switch (op_) {
2767 case Token::ADD:
2768 case Token::SUB:
2769 case Token::MUL:
2770 case Token::DIV:
2771 case Token::MOD: {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002772 // Load both operands and check that they are 32-bit integer.
2773 // Jump to type transition if they are not. The registers r0 and r1 (right
2774 // and left) are preserved for the runtime call.
2775 FloatingPointHelper::Destination destination =
2776 (CpuFeatures::IsSupported(VFP3) && op_ != Token::MOD)
2777 ? FloatingPointHelper::kVFPRegisters
2778 : FloatingPointHelper::kCoreRegisters;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002779
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002780 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2781 right,
2782 destination,
2783 d7,
2784 r2,
2785 r3,
2786 heap_number_map,
2787 scratch1,
2788 scratch2,
2789 s0,
2790 &transition);
2791 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2792 left,
2793 destination,
2794 d6,
2795 r4,
2796 r5,
2797 heap_number_map,
2798 scratch1,
2799 scratch2,
2800 s0,
2801 &transition);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002802
2803 if (destination == FloatingPointHelper::kVFPRegisters) {
2804 CpuFeatures::Scope scope(VFP3);
2805 Label return_heap_number;
2806 switch (op_) {
2807 case Token::ADD:
2808 __ vadd(d5, d6, d7);
2809 break;
2810 case Token::SUB:
2811 __ vsub(d5, d6, d7);
2812 break;
2813 case Token::MUL:
2814 __ vmul(d5, d6, d7);
2815 break;
2816 case Token::DIV:
2817 __ vdiv(d5, d6, d7);
2818 break;
2819 default:
2820 UNREACHABLE();
2821 }
2822
2823 if (op_ != Token::DIV) {
2824 // These operations produce an integer result.
2825 // Try to return a smi if we can.
2826 // Otherwise return a heap number if allowed, or jump to type
2827 // transition.
2828
2829 __ EmitVFPTruncate(kRoundToZero,
2830 single_scratch,
2831 d5,
2832 scratch1,
2833 scratch2);
2834
Ben Murdoch257744e2011-11-30 15:57:28 +00002835 if (result_type_ <= BinaryOpIC::INT32) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002836 // If the ne condition is set, result does
2837 // not fit in a 32-bit integer.
2838 __ b(ne, &transition);
2839 }
2840
2841 // Check if the result fits in a smi.
2842 __ vmov(scratch1, single_scratch);
2843 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
2844 // If not try to return a heap number.
2845 __ b(mi, &return_heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +01002846 // Check for minus zero. Return heap number for minus zero.
2847 Label not_zero;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002848 __ cmp(scratch1, Operand::Zero());
Steve Block44f0eee2011-05-26 01:26:41 +01002849 __ b(ne, &not_zero);
2850 __ vmov(scratch2, d5.high());
2851 __ tst(scratch2, Operand(HeapNumber::kSignMask));
2852 __ b(ne, &return_heap_number);
2853 __ bind(&not_zero);
2854
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002855 // Tag the result and return.
2856 __ SmiTag(r0, scratch1);
2857 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +01002858 } else {
2859 // DIV just falls through to allocating a heap number.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002860 }
2861
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002862 __ bind(&return_heap_number);
2863 // Return a heap number, or fall through to type transition or runtime
2864 // call if we can't.
2865 if (result_type_ >= ((op_ == Token::DIV) ? BinaryOpIC::HEAP_NUMBER
2866 : BinaryOpIC::INT32)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002867 // We are using vfp registers so r5 is available.
2868 heap_number_result = r5;
2869 GenerateHeapResultAllocation(masm,
2870 heap_number_result,
2871 heap_number_map,
2872 scratch1,
2873 scratch2,
2874 &call_runtime);
2875 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
2876 __ vstr(d5, r0, HeapNumber::kValueOffset);
2877 __ mov(r0, heap_number_result);
2878 __ Ret();
2879 }
2880
2881 // A DIV operation expecting an integer result falls through
2882 // to type transition.
2883
2884 } else {
2885 // We preserved r0 and r1 to be able to call runtime.
2886 // Save the left value on the stack.
2887 __ Push(r5, r4);
2888
Steve Block053d10c2011-06-13 19:13:29 +01002889 Label pop_and_call_runtime;
2890
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002891 // Allocate a heap number to store the result.
2892 heap_number_result = r5;
2893 GenerateHeapResultAllocation(masm,
2894 heap_number_result,
2895 heap_number_map,
2896 scratch1,
2897 scratch2,
Steve Block053d10c2011-06-13 19:13:29 +01002898 &pop_and_call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002899
2900 // Load the left value from the value saved on the stack.
2901 __ Pop(r1, r0);
2902
2903 // Call the C function to handle the double operation.
2904 FloatingPointHelper::CallCCodeForDoubleOperation(
2905 masm, op_, heap_number_result, scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002906 if (FLAG_debug_code) {
2907 __ stop("Unreachable code.");
2908 }
Steve Block053d10c2011-06-13 19:13:29 +01002909
2910 __ bind(&pop_and_call_runtime);
2911 __ Drop(2);
2912 __ b(&call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002913 }
2914
2915 break;
2916 }
2917
2918 case Token::BIT_OR:
2919 case Token::BIT_XOR:
2920 case Token::BIT_AND:
2921 case Token::SAR:
2922 case Token::SHR:
2923 case Token::SHL: {
2924 Label return_heap_number;
2925 Register scratch3 = r5;
2926 // Convert operands to 32-bit integers. Right in r2 and left in r3. The
2927 // registers r0 and r1 (right and left) are preserved for the runtime
2928 // call.
2929 FloatingPointHelper::LoadNumberAsInt32(masm,
2930 left,
2931 r3,
2932 heap_number_map,
2933 scratch1,
2934 scratch2,
2935 scratch3,
2936 d0,
2937 &transition);
2938 FloatingPointHelper::LoadNumberAsInt32(masm,
2939 right,
2940 r2,
2941 heap_number_map,
2942 scratch1,
2943 scratch2,
2944 scratch3,
2945 d0,
2946 &transition);
2947
2948 // The ECMA-262 standard specifies that, for shift operations, only the
2949 // 5 least significant bits of the shift value should be used.
2950 switch (op_) {
2951 case Token::BIT_OR:
2952 __ orr(r2, r3, Operand(r2));
2953 break;
2954 case Token::BIT_XOR:
2955 __ eor(r2, r3, Operand(r2));
2956 break;
2957 case Token::BIT_AND:
2958 __ and_(r2, r3, Operand(r2));
2959 break;
2960 case Token::SAR:
2961 __ and_(r2, r2, Operand(0x1f));
2962 __ mov(r2, Operand(r3, ASR, r2));
2963 break;
2964 case Token::SHR:
2965 __ and_(r2, r2, Operand(0x1f));
2966 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2967 // SHR is special because it is required to produce a positive answer.
2968 // We only get a negative result if the shift value (r2) is 0.
2969 // This result cannot be respresented as a signed 32-bit integer, try
2970 // to return a heap number if we can.
2971 // The non vfp3 code does not support this special case, so jump to
2972 // runtime if we don't support it.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002973 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002974 __ b(mi, (result_type_ <= BinaryOpIC::INT32)
2975 ? &transition
2976 : &return_heap_number);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002977 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002978 __ b(mi, (result_type_ <= BinaryOpIC::INT32)
2979 ? &transition
2980 : &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002981 }
2982 break;
2983 case Token::SHL:
2984 __ and_(r2, r2, Operand(0x1f));
2985 __ mov(r2, Operand(r3, LSL, r2));
2986 break;
2987 default:
2988 UNREACHABLE();
2989 }
2990
2991 // Check if the result fits in a smi.
2992 __ add(scratch1, r2, Operand(0x40000000), SetCC);
2993 // If not try to return a heap number. (We know the result is an int32.)
2994 __ b(mi, &return_heap_number);
2995 // Tag the result and return.
2996 __ SmiTag(r0, r2);
2997 __ Ret();
2998
2999 __ bind(&return_heap_number);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003000 heap_number_result = r5;
3001 GenerateHeapResultAllocation(masm,
3002 heap_number_result,
3003 heap_number_map,
3004 scratch1,
3005 scratch2,
3006 &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003007
Ben Murdoch8b112d22011-06-08 16:22:53 +01003008 if (CpuFeatures::IsSupported(VFP3)) {
3009 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003010 if (op_ != Token::SHR) {
3011 // Convert the result to a floating point value.
3012 __ vmov(double_scratch.low(), r2);
3013 __ vcvt_f64_s32(double_scratch, double_scratch.low());
3014 } else {
3015 // The result must be interpreted as an unsigned 32-bit integer.
3016 __ vmov(double_scratch.low(), r2);
3017 __ vcvt_f64_u32(double_scratch, double_scratch.low());
3018 }
3019
3020 // Store the result.
3021 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
3022 __ vstr(double_scratch, r0, HeapNumber::kValueOffset);
3023 __ mov(r0, heap_number_result);
3024 __ Ret();
3025 } else {
3026 // Tail call that writes the int32 in r2 to the heap number in r0, using
3027 // r3 as scratch. r0 is preserved and returned.
Ben Murdoch8b112d22011-06-08 16:22:53 +01003028 __ mov(r0, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003029 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
3030 __ TailCallStub(&stub);
3031 }
3032
3033 break;
3034 }
3035
3036 default:
3037 UNREACHABLE();
3038 }
3039
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003040 // We never expect DIV to yield an integer result, so we always generate
3041 // type transition code for DIV operations expecting an integer result: the
3042 // code will fall through to this type transition.
3043 if (transition.is_linked() ||
3044 ((op_ == Token::DIV) && (result_type_ <= BinaryOpIC::INT32))) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003045 __ bind(&transition);
3046 GenerateTypeTransition(masm);
3047 }
3048
3049 __ bind(&call_runtime);
3050 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01003051}
3052
3053
Ben Murdoch257744e2011-11-30 15:57:28 +00003054void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01003055 Label call_runtime;
3056
3057 if (op_ == Token::ADD) {
3058 // Handle string addition here, because it is the only operation
3059 // that does not do a ToNumber conversion on the operands.
3060 GenerateAddStrings(masm);
3061 }
3062
3063 // Convert oddball arguments to numbers.
3064 Label check, done;
3065 __ CompareRoot(r1, Heap::kUndefinedValueRootIndex);
3066 __ b(ne, &check);
3067 if (Token::IsBitOp(op_)) {
3068 __ mov(r1, Operand(Smi::FromInt(0)));
3069 } else {
3070 __ LoadRoot(r1, Heap::kNanValueRootIndex);
3071 }
3072 __ jmp(&done);
3073 __ bind(&check);
3074 __ CompareRoot(r0, Heap::kUndefinedValueRootIndex);
3075 __ b(ne, &done);
3076 if (Token::IsBitOp(op_)) {
3077 __ mov(r0, Operand(Smi::FromInt(0)));
3078 } else {
3079 __ LoadRoot(r0, Heap::kNanValueRootIndex);
3080 }
3081 __ bind(&done);
3082
3083 GenerateHeapNumberStub(masm);
3084}
3085
3086
Ben Murdoch257744e2011-11-30 15:57:28 +00003087void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01003088 Label call_runtime;
3089 GenerateFPOperation(masm, false, &call_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01003090
3091 __ bind(&call_runtime);
3092 GenerateCallRuntime(masm);
3093}
3094
3095
Ben Murdoch257744e2011-11-30 15:57:28 +00003096void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003097 Label call_runtime, call_string_add_or_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01003098
Ben Murdoch8b112d22011-06-08 16:22:53 +01003099 GenerateSmiCode(masm, &call_runtime, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01003100
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003101 GenerateFPOperation(masm, false, &call_string_add_or_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01003102
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003103 __ bind(&call_string_add_or_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01003104 if (op_ == Token::ADD) {
3105 GenerateAddStrings(masm);
3106 }
3107
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003108 __ bind(&call_runtime);
3109 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01003110}
3111
3112
Ben Murdoch257744e2011-11-30 15:57:28 +00003113void BinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01003114 ASSERT(op_ == Token::ADD);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003115 Label left_not_string, call_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01003116
3117 Register left = r1;
3118 Register right = r0;
Steve Block1e0659c2011-05-24 12:43:12 +01003119
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003120 // Check if left argument is a string.
3121 __ JumpIfSmi(left, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01003122 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003123 __ b(ge, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01003124
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003125 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
3126 GenerateRegisterArgsPush(masm);
3127 __ TailCallStub(&string_add_left_stub);
3128
3129 // Left operand is not a string, test right.
3130 __ bind(&left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01003131 __ JumpIfSmi(right, &call_runtime);
3132 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
3133 __ b(ge, &call_runtime);
3134
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003135 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
Steve Block1e0659c2011-05-24 12:43:12 +01003136 GenerateRegisterArgsPush(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003137 __ TailCallStub(&string_add_right_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01003138
3139 // At least one argument is not a string.
3140 __ bind(&call_runtime);
3141}
3142
3143
Ben Murdoch257744e2011-11-30 15:57:28 +00003144void BinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01003145 GenerateRegisterArgsPush(masm);
3146 switch (op_) {
3147 case Token::ADD:
Ben Murdoch257744e2011-11-30 15:57:28 +00003148 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003149 break;
3150 case Token::SUB:
Ben Murdoch257744e2011-11-30 15:57:28 +00003151 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003152 break;
3153 case Token::MUL:
Ben Murdoch257744e2011-11-30 15:57:28 +00003154 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003155 break;
3156 case Token::DIV:
Ben Murdoch257744e2011-11-30 15:57:28 +00003157 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003158 break;
3159 case Token::MOD:
Ben Murdoch257744e2011-11-30 15:57:28 +00003160 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003161 break;
3162 case Token::BIT_OR:
Ben Murdoch257744e2011-11-30 15:57:28 +00003163 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003164 break;
3165 case Token::BIT_AND:
Ben Murdoch257744e2011-11-30 15:57:28 +00003166 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003167 break;
3168 case Token::BIT_XOR:
Ben Murdoch257744e2011-11-30 15:57:28 +00003169 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003170 break;
3171 case Token::SAR:
Ben Murdoch257744e2011-11-30 15:57:28 +00003172 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003173 break;
3174 case Token::SHR:
Ben Murdoch257744e2011-11-30 15:57:28 +00003175 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003176 break;
3177 case Token::SHL:
Ben Murdoch257744e2011-11-30 15:57:28 +00003178 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003179 break;
3180 default:
3181 UNREACHABLE();
3182 }
3183}
3184
3185
Ben Murdoch257744e2011-11-30 15:57:28 +00003186void BinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
3187 Register result,
3188 Register heap_number_map,
3189 Register scratch1,
3190 Register scratch2,
3191 Label* gc_required) {
Steve Block1e0659c2011-05-24 12:43:12 +01003192 // Code below will scratch result if allocation fails. To keep both arguments
3193 // intact for the runtime call result cannot be one of these.
3194 ASSERT(!result.is(r0) && !result.is(r1));
3195
3196 if (mode_ == OVERWRITE_LEFT || mode_ == OVERWRITE_RIGHT) {
3197 Label skip_allocation, allocated;
3198 Register overwritable_operand = mode_ == OVERWRITE_LEFT ? r1 : r0;
3199 // If the overwritable operand is already an object, we skip the
3200 // allocation of a heap number.
3201 __ JumpIfNotSmi(overwritable_operand, &skip_allocation);
3202 // Allocate a heap number for the result.
3203 __ AllocateHeapNumber(
3204 result, scratch1, scratch2, heap_number_map, gc_required);
3205 __ b(&allocated);
3206 __ bind(&skip_allocation);
3207 // Use object holding the overwritable operand for result.
3208 __ mov(result, Operand(overwritable_operand));
3209 __ bind(&allocated);
3210 } else {
3211 ASSERT(mode_ == NO_OVERWRITE);
3212 __ AllocateHeapNumber(
3213 result, scratch1, scratch2, heap_number_map, gc_required);
3214 }
3215}
3216
3217
Ben Murdoch257744e2011-11-30 15:57:28 +00003218void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01003219 __ Push(r1, r0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003220}
3221
3222
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003223void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003224 // Untagged case: double input in d2, double result goes
3225 // into d2.
3226 // Tagged case: tagged input on top of stack and in r0,
3227 // tagged result (heap number) goes into r0.
3228
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003229 Label input_not_smi;
3230 Label loaded;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003231 Label calculate;
3232 Label invalid_cache;
3233 const Register scratch0 = r9;
3234 const Register scratch1 = r7;
3235 const Register cache_entry = r0;
3236 const bool tagged = (argument_type_ == TAGGED);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003237
Ben Murdoch8b112d22011-06-08 16:22:53 +01003238 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003239 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003240 if (tagged) {
3241 // Argument is a number and is on stack and in r0.
3242 // Load argument and check if it is a smi.
3243 __ JumpIfNotSmi(r0, &input_not_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003244
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003245 // Input is a smi. Convert to double and load the low and high words
3246 // of the double into r2, r3.
3247 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
3248 __ b(&loaded);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003249
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003250 __ bind(&input_not_smi);
3251 // Check if input is a HeapNumber.
3252 __ CheckMap(r0,
3253 r1,
3254 Heap::kHeapNumberMapRootIndex,
3255 &calculate,
Ben Murdoch257744e2011-11-30 15:57:28 +00003256 DONT_DO_SMI_CHECK);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003257 // Input is a HeapNumber. Load it to a double register and store the
3258 // low and high words into r2, r3.
3259 __ vldr(d0, FieldMemOperand(r0, HeapNumber::kValueOffset));
3260 __ vmov(r2, r3, d0);
3261 } else {
3262 // Input is untagged double in d2. Output goes to d2.
3263 __ vmov(r2, r3, d2);
3264 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003265 __ bind(&loaded);
3266 // r2 = low 32 bits of double value
3267 // r3 = high 32 bits of double value
3268 // Compute hash (the shifts are arithmetic):
3269 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
3270 __ eor(r1, r2, Operand(r3));
3271 __ eor(r1, r1, Operand(r1, ASR, 16));
3272 __ eor(r1, r1, Operand(r1, ASR, 8));
Steve Block44f0eee2011-05-26 01:26:41 +01003273 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
3274 __ And(r1, r1, Operand(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003275
3276 // r2 = low 32 bits of double value.
3277 // r3 = high 32 bits of double value.
3278 // r1 = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +01003279 Isolate* isolate = masm->isolate();
3280 ExternalReference cache_array =
3281 ExternalReference::transcendental_cache_array_address(isolate);
3282 __ mov(cache_entry, Operand(cache_array));
3283 // cache_entry points to cache array.
3284 int cache_array_index
3285 = type_ * sizeof(isolate->transcendental_cache()->caches_[0]);
3286 __ ldr(cache_entry, MemOperand(cache_entry, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003287 // r0 points to the cache for the type type_.
3288 // If NULL, the cache hasn't been initialized yet, so go through runtime.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003289 __ cmp(cache_entry, Operand(0, RelocInfo::NONE));
3290 __ b(eq, &invalid_cache);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003291
3292#ifdef DEBUG
3293 // Check that the layout of cache elements match expectations.
Steve Block44f0eee2011-05-26 01:26:41 +01003294 { TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003295 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
3296 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
3297 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
3298 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
3299 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
3300 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
3301 CHECK_EQ(0, elem_in0 - elem_start);
3302 CHECK_EQ(kIntSize, elem_in1 - elem_start);
3303 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
3304 }
3305#endif
3306
3307 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
3308 __ add(r1, r1, Operand(r1, LSL, 1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003309 __ add(cache_entry, cache_entry, Operand(r1, LSL, 2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003310 // Check if cache matches: Double value is stored in uint32_t[2] array.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003311 __ ldm(ia, cache_entry, r4.bit() | r5.bit() | r6.bit());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003312 __ cmp(r2, r4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003313 __ b(ne, &calculate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003314 __ cmp(r3, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003315 __ b(ne, &calculate);
3316 // Cache hit. Load result, cleanup and return.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003317 Counters* counters = masm->isolate()->counters();
3318 __ IncrementCounter(
3319 counters->transcendental_cache_hit(), 1, scratch0, scratch1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003320 if (tagged) {
3321 // Pop input value from stack and load result into r0.
3322 __ pop();
3323 __ mov(r0, Operand(r6));
3324 } else {
3325 // Load result into d2.
3326 __ vldr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
3327 }
3328 __ Ret();
Ben Murdoch8b112d22011-06-08 16:22:53 +01003329 } // if (CpuFeatures::IsSupported(VFP3))
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003330
3331 __ bind(&calculate);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003332 Counters* counters = masm->isolate()->counters();
3333 __ IncrementCounter(
3334 counters->transcendental_cache_miss(), 1, scratch0, scratch1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003335 if (tagged) {
3336 __ bind(&invalid_cache);
Steve Block44f0eee2011-05-26 01:26:41 +01003337 ExternalReference runtime_function =
3338 ExternalReference(RuntimeFunction(), masm->isolate());
3339 __ TailCallExternalReference(runtime_function, 1, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003340 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003341 if (!CpuFeatures::IsSupported(VFP3)) UNREACHABLE();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003342 CpuFeatures::Scope scope(VFP3);
3343
3344 Label no_update;
3345 Label skip_cache;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003346
3347 // Call C function to calculate the result and update the cache.
3348 // Register r0 holds precalculated cache entry address; preserve
3349 // it on the stack and pop it into register cache_entry after the
3350 // call.
3351 __ push(cache_entry);
3352 GenerateCallCFunction(masm, scratch0);
3353 __ GetCFunctionDoubleResult(d2);
3354
3355 // Try to update the cache. If we cannot allocate a
3356 // heap number, we return the result without updating.
3357 __ pop(cache_entry);
3358 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
3359 __ AllocateHeapNumber(r6, scratch0, scratch1, r5, &no_update);
3360 __ vstr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
3361 __ stm(ia, cache_entry, r2.bit() | r3.bit() | r6.bit());
3362 __ Ret();
3363
3364 __ bind(&invalid_cache);
3365 // The cache is invalid. Call runtime which will recreate the
3366 // cache.
3367 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
3368 __ AllocateHeapNumber(r0, scratch0, scratch1, r5, &skip_cache);
3369 __ vstr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003370 {
3371 FrameScope scope(masm, StackFrame::INTERNAL);
3372 __ push(r0);
3373 __ CallRuntime(RuntimeFunction(), 1);
3374 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003375 __ vldr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
3376 __ Ret();
3377
3378 __ bind(&skip_cache);
3379 // Call C function to calculate the result and answer directly
3380 // without updating the cache.
3381 GenerateCallCFunction(masm, scratch0);
3382 __ GetCFunctionDoubleResult(d2);
3383 __ bind(&no_update);
3384
3385 // We return the value in d2 without adding it to the cache, but
3386 // we cause a scavenging GC so that future allocations will succeed.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003387 {
3388 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003389
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003390 // Allocate an aligned object larger than a HeapNumber.
3391 ASSERT(4 * kPointerSize >= HeapNumber::kSize);
3392 __ mov(scratch0, Operand(4 * kPointerSize));
3393 __ push(scratch0);
3394 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
3395 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003396 __ Ret();
3397 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003398}
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003399
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003400
3401void TranscendentalCacheStub::GenerateCallCFunction(MacroAssembler* masm,
3402 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01003403 Isolate* isolate = masm->isolate();
3404
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003405 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003406 __ PrepareCallCFunction(0, 1, scratch);
3407 if (masm->use_eabi_hardfloat()) {
3408 __ vmov(d0, d2);
3409 } else {
3410 __ vmov(r0, r1, d2);
3411 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003412 AllowExternalCallThatCantCauseGC scope(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003413 switch (type_) {
3414 case TranscendentalCache::SIN:
Ben Murdoch257744e2011-11-30 15:57:28 +00003415 __ CallCFunction(ExternalReference::math_sin_double_function(isolate),
3416 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003417 break;
3418 case TranscendentalCache::COS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003419 __ CallCFunction(ExternalReference::math_cos_double_function(isolate),
3420 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003421 break;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003422 case TranscendentalCache::TAN:
3423 __ CallCFunction(ExternalReference::math_tan_double_function(isolate),
3424 0, 1);
3425 break;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003426 case TranscendentalCache::LOG:
Ben Murdoch257744e2011-11-30 15:57:28 +00003427 __ CallCFunction(ExternalReference::math_log_double_function(isolate),
3428 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003429 break;
3430 default:
3431 UNIMPLEMENTED();
3432 break;
3433 }
3434 __ pop(lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003435}
3436
3437
3438Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
3439 switch (type_) {
3440 // Add more cases when necessary.
3441 case TranscendentalCache::SIN: return Runtime::kMath_sin;
3442 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003443 case TranscendentalCache::TAN: return Runtime::kMath_tan;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003444 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003445 default:
3446 UNIMPLEMENTED();
3447 return Runtime::kAbort;
3448 }
3449}
3450
3451
3452void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01003453 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003454}
3455
3456
Steve Block44f0eee2011-05-26 01:26:41 +01003457void MathPowStub::Generate(MacroAssembler* masm) {
3458 Label call_runtime;
3459
Ben Murdoch8b112d22011-06-08 16:22:53 +01003460 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003461 CpuFeatures::Scope scope(VFP3);
3462
3463 Label base_not_smi;
3464 Label exponent_not_smi;
3465 Label convert_exponent;
3466
3467 const Register base = r0;
3468 const Register exponent = r1;
3469 const Register heapnumbermap = r5;
3470 const Register heapnumber = r6;
3471 const DoubleRegister double_base = d0;
3472 const DoubleRegister double_exponent = d1;
3473 const DoubleRegister double_result = d2;
3474 const SwVfpRegister single_scratch = s0;
3475 const Register scratch = r9;
3476 const Register scratch2 = r7;
3477
3478 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
3479 __ ldr(base, MemOperand(sp, 1 * kPointerSize));
3480 __ ldr(exponent, MemOperand(sp, 0 * kPointerSize));
3481
3482 // Convert base to double value and store it in d0.
3483 __ JumpIfNotSmi(base, &base_not_smi);
3484 // Base is a Smi. Untag and convert it.
3485 __ SmiUntag(base);
3486 __ vmov(single_scratch, base);
3487 __ vcvt_f64_s32(double_base, single_scratch);
3488 __ b(&convert_exponent);
3489
3490 __ bind(&base_not_smi);
3491 __ ldr(scratch, FieldMemOperand(base, JSObject::kMapOffset));
3492 __ cmp(scratch, heapnumbermap);
3493 __ b(ne, &call_runtime);
3494 // Base is a heapnumber. Load it into double register.
3495 __ vldr(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
3496
3497 __ bind(&convert_exponent);
3498 __ JumpIfNotSmi(exponent, &exponent_not_smi);
3499 __ SmiUntag(exponent);
3500
3501 // The base is in a double register and the exponent is
3502 // an untagged smi. Allocate a heap number and call a
3503 // C function for integer exponents. The register containing
3504 // the heap number is callee-saved.
3505 __ AllocateHeapNumber(heapnumber,
3506 scratch,
3507 scratch2,
3508 heapnumbermap,
3509 &call_runtime);
3510 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003511 __ PrepareCallCFunction(1, 1, scratch);
3512 __ SetCallCDoubleArguments(double_base, exponent);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003513 {
3514 AllowExternalCallThatCantCauseGC scope(masm);
3515 __ CallCFunction(
3516 ExternalReference::power_double_int_function(masm->isolate()),
3517 1, 1);
3518 __ pop(lr);
3519 __ GetCFunctionDoubleResult(double_result);
3520 }
Steve Block44f0eee2011-05-26 01:26:41 +01003521 __ vstr(double_result,
3522 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3523 __ mov(r0, heapnumber);
3524 __ Ret(2 * kPointerSize);
3525
3526 __ bind(&exponent_not_smi);
3527 __ ldr(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
3528 __ cmp(scratch, heapnumbermap);
3529 __ b(ne, &call_runtime);
3530 // Exponent is a heapnumber. Load it into double register.
3531 __ vldr(double_exponent,
3532 FieldMemOperand(exponent, HeapNumber::kValueOffset));
3533
3534 // The base and the exponent are in double registers.
3535 // Allocate a heap number and call a C function for
3536 // double exponents. The register containing
3537 // the heap number is callee-saved.
3538 __ AllocateHeapNumber(heapnumber,
3539 scratch,
3540 scratch2,
3541 heapnumbermap,
3542 &call_runtime);
3543 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003544 __ PrepareCallCFunction(0, 2, scratch);
3545 __ SetCallCDoubleArguments(double_base, double_exponent);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003546 {
3547 AllowExternalCallThatCantCauseGC scope(masm);
3548 __ CallCFunction(
3549 ExternalReference::power_double_double_function(masm->isolate()),
3550 0, 2);
3551 __ pop(lr);
3552 __ GetCFunctionDoubleResult(double_result);
3553 }
Steve Block44f0eee2011-05-26 01:26:41 +01003554 __ vstr(double_result,
3555 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3556 __ mov(r0, heapnumber);
3557 __ Ret(2 * kPointerSize);
3558 }
3559
3560 __ bind(&call_runtime);
3561 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
3562}
3563
3564
3565bool CEntryStub::NeedsImmovableCode() {
3566 return true;
3567}
3568
3569
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003570bool CEntryStub::IsPregenerated() {
3571 return (!save_doubles_ || ISOLATE->fp_stubs_generated()) &&
3572 result_size_ == 1;
3573}
3574
3575
3576void CodeStub::GenerateStubsAheadOfTime() {
3577 CEntryStub::GenerateAheadOfTime();
3578 WriteInt32ToHeapNumberStub::GenerateFixedRegStubsAheadOfTime();
3579 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime();
3580 RecordWriteStub::GenerateFixedRegStubsAheadOfTime();
3581}
3582
3583
3584void CodeStub::GenerateFPStubs() {
3585 CEntryStub save_doubles(1, kSaveFPRegs);
3586 Handle<Code> code = save_doubles.GetCode();
3587 code->set_is_pregenerated(true);
3588 StoreBufferOverflowStub stub(kSaveFPRegs);
3589 stub.GetCode()->set_is_pregenerated(true);
3590 code->GetIsolate()->set_fp_stubs_generated(true);
3591}
3592
3593
3594void CEntryStub::GenerateAheadOfTime() {
3595 CEntryStub stub(1, kDontSaveFPRegs);
3596 Handle<Code> code = stub.GetCode();
3597 code->set_is_pregenerated(true);
3598}
3599
3600
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003601void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003602 __ Throw(r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003603}
3604
3605
3606void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3607 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003608 __ ThrowUncatchable(type, r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003609}
3610
3611
3612void CEntryStub::GenerateCore(MacroAssembler* masm,
3613 Label* throw_normal_exception,
3614 Label* throw_termination_exception,
3615 Label* throw_out_of_memory_exception,
3616 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01003617 bool always_allocate) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003618 // r0: result parameter for PerformGC, if any
3619 // r4: number of arguments including receiver (C callee-saved)
3620 // r5: pointer to builtin function (C callee-saved)
3621 // r6: pointer to the first argument (C callee-saved)
Steve Block44f0eee2011-05-26 01:26:41 +01003622 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003623
3624 if (do_gc) {
3625 // Passing r0.
Ben Murdoch257744e2011-11-30 15:57:28 +00003626 __ PrepareCallCFunction(1, 0, r1);
3627 __ CallCFunction(ExternalReference::perform_gc_function(isolate),
3628 1, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003629 }
3630
3631 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01003632 ExternalReference::heap_always_allocate_scope_depth(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003633 if (always_allocate) {
3634 __ mov(r0, Operand(scope_depth));
3635 __ ldr(r1, MemOperand(r0));
3636 __ add(r1, r1, Operand(1));
3637 __ str(r1, MemOperand(r0));
3638 }
3639
3640 // Call C built-in.
3641 // r0 = argc, r1 = argv
3642 __ mov(r0, Operand(r4));
3643 __ mov(r1, Operand(r6));
3644
Steve Block1e0659c2011-05-24 12:43:12 +01003645#if defined(V8_HOST_ARCH_ARM)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003646 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
3647 int frame_alignment_mask = frame_alignment - 1;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003648 if (FLAG_debug_code) {
3649 if (frame_alignment > kPointerSize) {
3650 Label alignment_as_expected;
3651 ASSERT(IsPowerOf2(frame_alignment));
Steve Block1e0659c2011-05-24 12:43:12 +01003652 __ tst(sp, Operand(frame_alignment_mask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003653 __ b(eq, &alignment_as_expected);
3654 // Don't use Check here, as it will call Runtime_Abort re-entering here.
3655 __ stop("Unexpected alignment");
3656 __ bind(&alignment_as_expected);
3657 }
3658 }
3659#endif
3660
Steve Block44f0eee2011-05-26 01:26:41 +01003661 __ mov(r2, Operand(ExternalReference::isolate_address()));
3662
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003663 // To let the GC traverse the return address of the exit frames, we need to
3664 // know where the return address is. The CEntryStub is unmovable, so
3665 // we can store the address on the stack to be able to find it again and
3666 // we never have to restore it, because it will not change.
Steve Block1e0659c2011-05-24 12:43:12 +01003667 // Compute the return address in lr to return to after the jump below. Pc is
3668 // already at '+ 8' from the current instruction but return is after three
3669 // instructions so add another 4 to pc to get the return address.
3670 masm->add(lr, pc, Operand(4));
3671 __ str(lr, MemOperand(sp, 0));
3672 masm->Jump(r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003673
3674 if (always_allocate) {
3675 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
3676 // though (contain the result).
3677 __ mov(r2, Operand(scope_depth));
3678 __ ldr(r3, MemOperand(r2));
3679 __ sub(r3, r3, Operand(1));
3680 __ str(r3, MemOperand(r2));
3681 }
3682
3683 // check for failure result
3684 Label failure_returned;
3685 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3686 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
3687 __ add(r2, r0, Operand(1));
3688 __ tst(r2, Operand(kFailureTagMask));
3689 __ b(eq, &failure_returned);
3690
3691 // Exit C frame and return.
3692 // r0:r1: result
3693 // sp: stack pointer
3694 // fp: frame pointer
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003695 // Callee-saved register r4 still holds argc.
3696 __ LeaveExitFrame(save_doubles_, r4);
3697 __ mov(pc, lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003698
3699 // check if we should retry or throw exception
3700 Label retry;
3701 __ bind(&failure_returned);
3702 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3703 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
3704 __ b(eq, &retry);
3705
3706 // Special handling of out of memory exceptions.
3707 Failure* out_of_memory = Failure::OutOfMemoryException();
3708 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3709 __ b(eq, throw_out_of_memory_exception);
3710
3711 // Retrieve the pending exception and clear the variable.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003712 __ mov(r3, Operand(isolate->factory()->the_hole_value()));
Ben Murdoch589d6972011-11-30 16:04:58 +00003713 __ mov(ip, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01003714 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003715 __ ldr(r0, MemOperand(ip));
3716 __ str(r3, MemOperand(ip));
3717
3718 // Special handling of termination exceptions which are uncatchable
3719 // by javascript code.
Steve Block44f0eee2011-05-26 01:26:41 +01003720 __ cmp(r0, Operand(isolate->factory()->termination_exception()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003721 __ b(eq, throw_termination_exception);
3722
3723 // Handle normal exception.
3724 __ jmp(throw_normal_exception);
3725
3726 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
3727}
3728
3729
3730void CEntryStub::Generate(MacroAssembler* masm) {
3731 // Called from JavaScript; parameters are on stack as if calling JS function
3732 // r0: number of arguments including receiver
3733 // r1: pointer to builtin function
3734 // fp: frame pointer (restored after C call)
3735 // sp: stack pointer (restored as callee's sp after C call)
3736 // cp: current context (C callee-saved)
3737
3738 // Result returned in r0 or r0+r1 by default.
3739
3740 // NOTE: Invocations of builtins may return failure objects
3741 // instead of a proper result. The builtin entry handles
3742 // this by performing a garbage collection and retrying the
3743 // builtin once.
3744
Steve Block1e0659c2011-05-24 12:43:12 +01003745 // Compute the argv pointer in a callee-saved register.
3746 __ add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
3747 __ sub(r6, r6, Operand(kPointerSize));
3748
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003749 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003750 FrameScope scope(masm, StackFrame::MANUAL);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003751 __ EnterExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003752
Steve Block1e0659c2011-05-24 12:43:12 +01003753 // Setup argc and the builtin function in callee-saved registers.
3754 __ mov(r4, Operand(r0));
3755 __ mov(r5, Operand(r1));
3756
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003757 // r4: number of arguments (C callee-saved)
3758 // r5: pointer to builtin function (C callee-saved)
3759 // r6: pointer to first argument (C callee-saved)
3760
3761 Label throw_normal_exception;
3762 Label throw_termination_exception;
3763 Label throw_out_of_memory_exception;
3764
3765 // Call into the runtime system.
3766 GenerateCore(masm,
3767 &throw_normal_exception,
3768 &throw_termination_exception,
3769 &throw_out_of_memory_exception,
3770 false,
Steve Block1e0659c2011-05-24 12:43:12 +01003771 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003772
3773 // Do space-specific GC and retry runtime call.
3774 GenerateCore(masm,
3775 &throw_normal_exception,
3776 &throw_termination_exception,
3777 &throw_out_of_memory_exception,
3778 true,
Steve Block1e0659c2011-05-24 12:43:12 +01003779 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003780
3781 // Do full GC and retry runtime call one final time.
3782 Failure* failure = Failure::InternalError();
3783 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
3784 GenerateCore(masm,
3785 &throw_normal_exception,
3786 &throw_termination_exception,
3787 &throw_out_of_memory_exception,
3788 true,
Steve Block1e0659c2011-05-24 12:43:12 +01003789 true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003790
3791 __ bind(&throw_out_of_memory_exception);
3792 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3793
3794 __ bind(&throw_termination_exception);
3795 GenerateThrowUncatchable(masm, TERMINATION);
3796
3797 __ bind(&throw_normal_exception);
3798 GenerateThrowTOS(masm);
3799}
3800
3801
3802void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3803 // r0: code entry
3804 // r1: function
3805 // r2: receiver
3806 // r3: argc
3807 // [sp+0]: argv
3808
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003809 Label invoke, handler_entry, exit;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003810
3811 // Called from C, so do not pop argc and args on exit (preserve sp)
3812 // No need to save register-passed args
3813 // Save callee-saved registers (incl. cp and fp), sp, and lr
3814 __ stm(db_w, sp, kCalleeSaved | lr.bit());
3815
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003816 if (CpuFeatures::IsSupported(VFP3)) {
3817 CpuFeatures::Scope scope(VFP3);
3818 // Save callee-saved vfp registers.
3819 __ vstm(db_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003820 // Set up the reserved register for 0.0.
3821 __ vmov(kDoubleRegZero, 0.0);
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003822 }
3823
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003824 // Get address of argv, see stm above.
3825 // r0: code entry
3826 // r1: function
3827 // r2: receiver
3828 // r3: argc
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003829
3830 // Setup argv in r4.
3831 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
3832 if (CpuFeatures::IsSupported(VFP3)) {
3833 offset_to_argv += kNumDoubleCalleeSaved * kDoubleSize;
3834 }
3835 __ ldr(r4, MemOperand(sp, offset_to_argv));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003836
3837 // Push a frame with special values setup to mark it as an entry frame.
3838 // r0: code entry
3839 // r1: function
3840 // r2: receiver
3841 // r3: argc
3842 // r4: argv
Steve Block44f0eee2011-05-26 01:26:41 +01003843 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003844 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
3845 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3846 __ mov(r7, Operand(Smi::FromInt(marker)));
3847 __ mov(r6, Operand(Smi::FromInt(marker)));
Steve Block44f0eee2011-05-26 01:26:41 +01003848 __ mov(r5,
Ben Murdoch589d6972011-11-30 16:04:58 +00003849 Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003850 __ ldr(r5, MemOperand(r5));
3851 __ Push(r8, r7, r6, r5);
3852
3853 // Setup frame pointer for the frame to be pushed.
3854 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3855
Ben Murdochb0fe1622011-05-05 13:52:32 +01003856 // If this is the outermost JS call, set js_entry_sp value.
Steve Block053d10c2011-06-13 19:13:29 +01003857 Label non_outermost_js;
Ben Murdoch589d6972011-11-30 16:04:58 +00003858 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003859 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
3860 __ ldr(r6, MemOperand(r5));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003861 __ cmp(r6, Operand::Zero());
Steve Block053d10c2011-06-13 19:13:29 +01003862 __ b(ne, &non_outermost_js);
3863 __ str(fp, MemOperand(r5));
3864 __ mov(ip, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
3865 Label cont;
3866 __ b(&cont);
3867 __ bind(&non_outermost_js);
3868 __ mov(ip, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
3869 __ bind(&cont);
3870 __ push(ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003871
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003872 // Jump to a faked try block that does the invoke, with a faked catch
3873 // block that sets the pending exception.
3874 __ jmp(&invoke);
3875 __ bind(&handler_entry);
3876 handler_offset_ = handler_entry.pos();
3877 // Caught exception: Store result (exception) in the pending exception
3878 // field in the JSEnv and return a failure sentinel. Coming in here the
3879 // fp will be invalid because the PushTryHandler below sets it to 0 to
3880 // signal the existence of the JSEntry frame.
Ben Murdoch589d6972011-11-30 16:04:58 +00003881 __ mov(ip, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01003882 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003883 __ str(r0, MemOperand(ip));
3884 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
3885 __ b(&exit);
3886
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003887 // Invoke: Link this frame into the handler chain. There's only one
3888 // handler block in this code object, so its index is 0.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003889 __ bind(&invoke);
3890 // Must preserve r0-r4, r5-r7 are available.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003891 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003892 // If an exception not caught by another handler occurs, this handler
3893 // returns control to the code after the bl(&invoke) above, which
3894 // restores all kCalleeSaved registers (including cp and fp) to their
3895 // saved values before returning a failure to C.
3896
3897 // Clear any pending exceptions.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003898 __ mov(r5, Operand(isolate->factory()->the_hole_value()));
Ben Murdoch589d6972011-11-30 16:04:58 +00003899 __ mov(ip, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01003900 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003901 __ str(r5, MemOperand(ip));
3902
3903 // Invoke the function by calling through JS entry trampoline builtin.
3904 // Notice that we cannot store a reference to the trampoline code directly in
3905 // this stub, because runtime stubs are not traversed when doing GC.
3906
3907 // Expected registers by Builtins::JSEntryTrampoline
3908 // r0: code entry
3909 // r1: function
3910 // r2: receiver
3911 // r3: argc
3912 // r4: argv
3913 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01003914 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
3915 isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003916 __ mov(ip, Operand(construct_entry));
3917 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003918 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003919 __ mov(ip, Operand(entry));
3920 }
3921 __ ldr(ip, MemOperand(ip)); // deref address
3922
3923 // Branch and link to JSEntryTrampoline. We don't use the double underscore
3924 // macro for the add instruction because we don't want the coverage tool
3925 // inserting instructions here after we read the pc.
3926 __ mov(lr, Operand(pc));
3927 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
3928
Steve Block053d10c2011-06-13 19:13:29 +01003929 // Unlink this frame from the handler chain.
3930 __ PopTryHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003931
3932 __ bind(&exit); // r0 holds result
Steve Block053d10c2011-06-13 19:13:29 +01003933 // Check if the current stack frame is marked as the outermost JS frame.
3934 Label non_outermost_js_2;
3935 __ pop(r5);
3936 __ cmp(r5, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
3937 __ b(ne, &non_outermost_js_2);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003938 __ mov(r6, Operand::Zero());
Steve Block053d10c2011-06-13 19:13:29 +01003939 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
3940 __ str(r6, MemOperand(r5));
3941 __ bind(&non_outermost_js_2);
Steve Block053d10c2011-06-13 19:13:29 +01003942
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003943 // Restore the top frame descriptors from the stack.
3944 __ pop(r3);
Steve Block44f0eee2011-05-26 01:26:41 +01003945 __ mov(ip,
Ben Murdoch589d6972011-11-30 16:04:58 +00003946 Operand(ExternalReference(Isolate::kCEntryFPAddress, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003947 __ str(r3, MemOperand(ip));
3948
3949 // Reset the stack to the callee saved registers.
3950 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3951
3952 // Restore callee-saved registers and return.
3953#ifdef DEBUG
3954 if (FLAG_debug_code) {
3955 __ mov(lr, Operand(pc));
3956 }
3957#endif
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003958
3959 if (CpuFeatures::IsSupported(VFP3)) {
3960 CpuFeatures::Scope scope(VFP3);
3961 // Restore callee-saved vfp registers.
3962 __ vldm(ia_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
3963 }
3964
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003965 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
3966}
3967
3968
Steve Block1e0659c2011-05-24 12:43:12 +01003969// Uses registers r0 to r4.
3970// Expected input (depending on whether args are in registers or on the stack):
3971// * object: r0 or at sp + 1 * kPointerSize.
3972// * function: r1 or at sp.
3973//
3974// An inlined call site may have been generated before calling this stub.
3975// In this case the offset to the inline site to patch is passed on the stack,
3976// in the safepoint slot for register r4.
3977// (See LCodeGen::DoInstanceOfKnownGlobal)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003978void InstanceofStub::Generate(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01003979 // Call site inlining and patching implies arguments in registers.
3980 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
3981 // ReturnTrueFalse is only implemented for inlined call sites.
3982 ASSERT(!ReturnTrueFalseObject() || HasCallSiteInlineCheck());
3983
Ben Murdochb0fe1622011-05-05 13:52:32 +01003984 // Fixed register usage throughout the stub:
Steve Block9fac8402011-05-12 15:51:54 +01003985 const Register object = r0; // Object (lhs).
Steve Block1e0659c2011-05-24 12:43:12 +01003986 Register map = r3; // Map of the object.
Steve Block9fac8402011-05-12 15:51:54 +01003987 const Register function = r1; // Function (rhs).
Ben Murdochb0fe1622011-05-05 13:52:32 +01003988 const Register prototype = r4; // Prototype of the function.
Steve Block1e0659c2011-05-24 12:43:12 +01003989 const Register inline_site = r9;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003990 const Register scratch = r2;
Steve Block1e0659c2011-05-24 12:43:12 +01003991
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003992 const int32_t kDeltaToLoadBoolResult = 4 * kPointerSize;
Steve Block1e0659c2011-05-24 12:43:12 +01003993
Ben Murdochb0fe1622011-05-05 13:52:32 +01003994 Label slow, loop, is_instance, is_not_instance, not_js_object;
Steve Block1e0659c2011-05-24 12:43:12 +01003995
Ben Murdoch086aeea2011-05-13 15:57:08 +01003996 if (!HasArgsInRegisters()) {
Steve Block9fac8402011-05-12 15:51:54 +01003997 __ ldr(object, MemOperand(sp, 1 * kPointerSize));
3998 __ ldr(function, MemOperand(sp, 0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003999 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004000
Ben Murdochb0fe1622011-05-05 13:52:32 +01004001 // Check that the left hand is a JS object and load map.
Steve Block1e0659c2011-05-24 12:43:12 +01004002 __ JumpIfSmi(object, &not_js_object);
Steve Block9fac8402011-05-12 15:51:54 +01004003 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004004
Steve Block1e0659c2011-05-24 12:43:12 +01004005 // If there is a call site cache don't look in the global cache, but do the
4006 // real lookup and update the call site cache.
4007 if (!HasCallSiteInlineCheck()) {
4008 Label miss;
4009 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
4010 __ cmp(function, ip);
4011 __ b(ne, &miss);
4012 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
4013 __ cmp(map, ip);
4014 __ b(ne, &miss);
4015 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
4016 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004017
Steve Block1e0659c2011-05-24 12:43:12 +01004018 __ bind(&miss);
4019 }
4020
4021 // Get the prototype of the function.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004022 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004023
4024 // Check that the function prototype is a JS object.
Steve Block1e0659c2011-05-24 12:43:12 +01004025 __ JumpIfSmi(prototype, &slow);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004026 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004027
Steve Block1e0659c2011-05-24 12:43:12 +01004028 // Update the global instanceof or call site inlined cache with the current
4029 // map and function. The cached answer will be set when it is known below.
4030 if (!HasCallSiteInlineCheck()) {
4031 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
4032 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
4033 } else {
4034 ASSERT(HasArgsInRegisters());
4035 // Patch the (relocated) inlined map check.
4036
4037 // The offset was stored in r4 safepoint slot.
4038 // (See LCodeGen::DoDeferredLInstanceOfKnownGlobal)
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004039 __ LoadFromSafepointRegisterSlot(scratch, r4);
Steve Block1e0659c2011-05-24 12:43:12 +01004040 __ sub(inline_site, lr, scratch);
4041 // Get the map location in scratch and patch it.
4042 __ GetRelocatedValueLocation(inline_site, scratch);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004043 __ ldr(scratch, MemOperand(scratch));
4044 __ str(map, FieldMemOperand(scratch, JSGlobalPropertyCell::kValueOffset));
Steve Block1e0659c2011-05-24 12:43:12 +01004045 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004046
4047 // Register mapping: r3 is object map and r4 is function prototype.
4048 // Get prototype of object into r2.
Ben Murdochb0fe1622011-05-05 13:52:32 +01004049 __ ldr(scratch, FieldMemOperand(map, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004050
Steve Block1e0659c2011-05-24 12:43:12 +01004051 // We don't need map any more. Use it as a scratch register.
4052 Register scratch2 = map;
4053 map = no_reg;
4054
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004055 // Loop through the prototype chain looking for the function prototype.
Steve Block1e0659c2011-05-24 12:43:12 +01004056 __ LoadRoot(scratch2, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004057 __ bind(&loop);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004058 __ cmp(scratch, Operand(prototype));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004059 __ b(eq, &is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01004060 __ cmp(scratch, scratch2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004061 __ b(eq, &is_not_instance);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004062 __ ldr(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
4063 __ ldr(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004064 __ jmp(&loop);
4065
4066 __ bind(&is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01004067 if (!HasCallSiteInlineCheck()) {
4068 __ mov(r0, Operand(Smi::FromInt(0)));
4069 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
4070 } else {
4071 // Patch the call site to return true.
4072 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
4073 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
4074 // Get the boolean result location in scratch and patch it.
4075 __ GetRelocatedValueLocation(inline_site, scratch);
4076 __ str(r0, MemOperand(scratch));
4077
4078 if (!ReturnTrueFalseObject()) {
4079 __ mov(r0, Operand(Smi::FromInt(0)));
4080 }
4081 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01004082 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004083
4084 __ bind(&is_not_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01004085 if (!HasCallSiteInlineCheck()) {
4086 __ mov(r0, Operand(Smi::FromInt(1)));
4087 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
4088 } else {
4089 // Patch the call site to return false.
4090 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
4091 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
4092 // Get the boolean result location in scratch and patch it.
4093 __ GetRelocatedValueLocation(inline_site, scratch);
4094 __ str(r0, MemOperand(scratch));
4095
4096 if (!ReturnTrueFalseObject()) {
4097 __ mov(r0, Operand(Smi::FromInt(1)));
4098 }
4099 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01004100 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004101
4102 Label object_not_null, object_not_null_or_smi;
4103 __ bind(&not_js_object);
4104 // Before null, smi and string value checks, check that the rhs is a function
4105 // as for a non-function rhs an exception needs to be thrown.
Steve Block1e0659c2011-05-24 12:43:12 +01004106 __ JumpIfSmi(function, &slow);
4107 __ CompareObjectType(function, scratch2, scratch, JS_FUNCTION_TYPE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004108 __ b(ne, &slow);
4109
4110 // Null is not instance of anything.
Ben Murdoch257744e2011-11-30 15:57:28 +00004111 __ cmp(scratch, Operand(masm->isolate()->factory()->null_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004112 __ b(ne, &object_not_null);
4113 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004114 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004115
4116 __ bind(&object_not_null);
4117 // Smi values are not instances of anything.
Steve Block1e0659c2011-05-24 12:43:12 +01004118 __ JumpIfNotSmi(object, &object_not_null_or_smi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004119 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004120 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004121
4122 __ bind(&object_not_null_or_smi);
4123 // String values are not instances of anything.
4124 __ IsObjectJSStringType(object, scratch, &slow);
4125 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01004126 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004127
4128 // Slow-case. Tail call builtin.
Ben Murdoch086aeea2011-05-13 15:57:08 +01004129 __ bind(&slow);
Steve Block1e0659c2011-05-24 12:43:12 +01004130 if (!ReturnTrueFalseObject()) {
4131 if (HasArgsInRegisters()) {
4132 __ Push(r0, r1);
4133 }
Ben Murdoch257744e2011-11-30 15:57:28 +00004134 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01004135 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004136 {
4137 FrameScope scope(masm, StackFrame::INTERNAL);
4138 __ Push(r0, r1);
4139 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4140 }
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004141 __ cmp(r0, Operand::Zero());
Steve Block1e0659c2011-05-24 12:43:12 +01004142 __ LoadRoot(r0, Heap::kTrueValueRootIndex, eq);
4143 __ LoadRoot(r0, Heap::kFalseValueRootIndex, ne);
4144 __ Ret(HasArgsInRegisters() ? 0 : 2);
4145 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004146}
4147
4148
Steve Block1e0659c2011-05-24 12:43:12 +01004149Register InstanceofStub::left() { return r0; }
4150
4151
4152Register InstanceofStub::right() { return r1; }
4153
4154
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004155void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
4156 // The displacement is the offset of the last parameter (if any)
4157 // relative to the frame pointer.
4158 static const int kDisplacement =
4159 StandardFrameConstants::kCallerSPOffset - kPointerSize;
4160
4161 // Check that the key is a smi.
4162 Label slow;
Steve Block1e0659c2011-05-24 12:43:12 +01004163 __ JumpIfNotSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004164
4165 // Check if the calling frame is an arguments adaptor frame.
4166 Label adaptor;
4167 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4168 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4169 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4170 __ b(eq, &adaptor);
4171
4172 // Check index against formal parameters count limit passed in
4173 // through register r0. Use unsigned comparison to get negative
4174 // check for free.
4175 __ cmp(r1, r0);
Ben Murdoch086aeea2011-05-13 15:57:08 +01004176 __ b(hs, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004177
4178 // Read the argument from the stack and return it.
4179 __ sub(r3, r0, r1);
4180 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4181 __ ldr(r0, MemOperand(r3, kDisplacement));
4182 __ Jump(lr);
4183
4184 // Arguments adaptor case: Check index against actual arguments
4185 // limit found in the arguments adaptor frame. Use unsigned
4186 // comparison to get negative check for free.
4187 __ bind(&adaptor);
4188 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4189 __ cmp(r1, r0);
4190 __ b(cs, &slow);
4191
4192 // Read the argument from the adaptor frame and return it.
4193 __ sub(r3, r0, r1);
4194 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
4195 __ ldr(r0, MemOperand(r3, kDisplacement));
4196 __ Jump(lr);
4197
4198 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4199 // by calling the runtime system.
4200 __ bind(&slow);
4201 __ push(r1);
4202 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
4203}
4204
4205
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004206void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004207 // sp[0] : number of parameters
4208 // sp[4] : receiver displacement
4209 // sp[8] : function
4210
4211 // Check if the calling frame is an arguments adaptor frame.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004212 Label runtime;
4213 __ ldr(r3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4214 __ ldr(r2, MemOperand(r3, StandardFrameConstants::kContextOffset));
4215 __ cmp(r2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4216 __ b(ne, &runtime);
4217
4218 // Patch the arguments.length and the parameters pointer in the current frame.
4219 __ ldr(r2, MemOperand(r3, ArgumentsAdaptorFrameConstants::kLengthOffset));
4220 __ str(r2, MemOperand(sp, 0 * kPointerSize));
4221 __ add(r3, r3, Operand(r2, LSL, 1));
4222 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4223 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4224
4225 __ bind(&runtime);
4226 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
4227}
4228
4229
4230void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
4231 // Stack layout:
4232 // sp[0] : number of parameters (tagged)
4233 // sp[4] : address of receiver argument
4234 // sp[8] : function
4235 // Registers used over whole function:
4236 // r6 : allocated object (tagged)
4237 // r9 : mapped parameter count (tagged)
4238
4239 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
4240 // r1 = parameter count (tagged)
4241
4242 // Check if the calling frame is an arguments adaptor frame.
4243 Label runtime;
4244 Label adaptor_frame, try_allocate;
4245 __ ldr(r3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4246 __ ldr(r2, MemOperand(r3, StandardFrameConstants::kContextOffset));
4247 __ cmp(r2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4248 __ b(eq, &adaptor_frame);
4249
4250 // No adaptor, parameter count = argument count.
4251 __ mov(r2, r1);
4252 __ b(&try_allocate);
4253
4254 // We have an adaptor frame. Patch the parameters pointer.
4255 __ bind(&adaptor_frame);
4256 __ ldr(r2, MemOperand(r3, ArgumentsAdaptorFrameConstants::kLengthOffset));
4257 __ add(r3, r3, Operand(r2, LSL, 1));
4258 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4259 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4260
4261 // r1 = parameter count (tagged)
4262 // r2 = argument count (tagged)
4263 // Compute the mapped parameter count = min(r1, r2) in r1.
4264 __ cmp(r1, Operand(r2));
4265 __ mov(r1, Operand(r2), LeaveCC, gt);
4266
4267 __ bind(&try_allocate);
4268
4269 // Compute the sizes of backing store, parameter map, and arguments object.
4270 // 1. Parameter map, has 2 extra words containing context and backing store.
4271 const int kParameterMapHeaderSize =
4272 FixedArray::kHeaderSize + 2 * kPointerSize;
4273 // If there are no mapped parameters, we do not need the parameter_map.
4274 __ cmp(r1, Operand(Smi::FromInt(0)));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004275 __ mov(r9, Operand::Zero(), LeaveCC, eq);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004276 __ mov(r9, Operand(r1, LSL, 1), LeaveCC, ne);
4277 __ add(r9, r9, Operand(kParameterMapHeaderSize), LeaveCC, ne);
4278
4279 // 2. Backing store.
4280 __ add(r9, r9, Operand(r2, LSL, 1));
4281 __ add(r9, r9, Operand(FixedArray::kHeaderSize));
4282
4283 // 3. Arguments object.
4284 __ add(r9, r9, Operand(Heap::kArgumentsObjectSize));
4285
4286 // Do the allocation of all three objects in one go.
4287 __ AllocateInNewSpace(r9, r0, r3, r4, &runtime, TAG_OBJECT);
4288
4289 // r0 = address of new object(s) (tagged)
4290 // r2 = argument count (tagged)
4291 // Get the arguments boilerplate from the current (global) context into r4.
4292 const int kNormalOffset =
4293 Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
4294 const int kAliasedOffset =
4295 Context::SlotOffset(Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX);
4296
4297 __ ldr(r4, MemOperand(r8, Context::SlotOffset(Context::GLOBAL_INDEX)));
4298 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004299 __ cmp(r1, Operand::Zero());
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004300 __ ldr(r4, MemOperand(r4, kNormalOffset), eq);
4301 __ ldr(r4, MemOperand(r4, kAliasedOffset), ne);
4302
4303 // r0 = address of new object (tagged)
4304 // r1 = mapped parameter count (tagged)
4305 // r2 = argument count (tagged)
4306 // r4 = address of boilerplate object (tagged)
4307 // Copy the JS object part.
4308 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
4309 __ ldr(r3, FieldMemOperand(r4, i));
4310 __ str(r3, FieldMemOperand(r0, i));
4311 }
4312
4313 // Setup the callee in-object property.
4314 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
4315 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
4316 const int kCalleeOffset = JSObject::kHeaderSize +
4317 Heap::kArgumentsCalleeIndex * kPointerSize;
4318 __ str(r3, FieldMemOperand(r0, kCalleeOffset));
4319
4320 // Use the length (smi tagged) and set that as an in-object property too.
4321 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
4322 const int kLengthOffset = JSObject::kHeaderSize +
4323 Heap::kArgumentsLengthIndex * kPointerSize;
4324 __ str(r2, FieldMemOperand(r0, kLengthOffset));
4325
4326 // Setup the elements pointer in the allocated arguments object.
4327 // If we allocated a parameter map, r4 will point there, otherwise
4328 // it will point to the backing store.
4329 __ add(r4, r0, Operand(Heap::kArgumentsObjectSize));
4330 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
4331
4332 // r0 = address of new object (tagged)
4333 // r1 = mapped parameter count (tagged)
4334 // r2 = argument count (tagged)
4335 // r4 = address of parameter map or backing store (tagged)
4336 // Initialize parameter map. If there are no mapped arguments, we're done.
4337 Label skip_parameter_map;
4338 __ cmp(r1, Operand(Smi::FromInt(0)));
4339 // Move backing store address to r3, because it is
4340 // expected there when filling in the unmapped arguments.
4341 __ mov(r3, r4, LeaveCC, eq);
4342 __ b(eq, &skip_parameter_map);
4343
4344 __ LoadRoot(r6, Heap::kNonStrictArgumentsElementsMapRootIndex);
4345 __ str(r6, FieldMemOperand(r4, FixedArray::kMapOffset));
4346 __ add(r6, r1, Operand(Smi::FromInt(2)));
4347 __ str(r6, FieldMemOperand(r4, FixedArray::kLengthOffset));
4348 __ str(r8, FieldMemOperand(r4, FixedArray::kHeaderSize + 0 * kPointerSize));
4349 __ add(r6, r4, Operand(r1, LSL, 1));
4350 __ add(r6, r6, Operand(kParameterMapHeaderSize));
4351 __ str(r6, FieldMemOperand(r4, FixedArray::kHeaderSize + 1 * kPointerSize));
4352
4353 // Copy the parameter slots and the holes in the arguments.
4354 // We need to fill in mapped_parameter_count slots. They index the context,
4355 // where parameters are stored in reverse order, at
4356 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
4357 // The mapped parameter thus need to get indices
4358 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
4359 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
4360 // We loop from right to left.
4361 Label parameters_loop, parameters_test;
4362 __ mov(r6, r1);
4363 __ ldr(r9, MemOperand(sp, 0 * kPointerSize));
4364 __ add(r9, r9, Operand(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
4365 __ sub(r9, r9, Operand(r1));
4366 __ LoadRoot(r7, Heap::kTheHoleValueRootIndex);
4367 __ add(r3, r4, Operand(r6, LSL, 1));
4368 __ add(r3, r3, Operand(kParameterMapHeaderSize));
4369
4370 // r6 = loop variable (tagged)
4371 // r1 = mapping index (tagged)
4372 // r3 = address of backing store (tagged)
4373 // r4 = address of parameter map (tagged)
4374 // r5 = temporary scratch (a.o., for address calculation)
4375 // r7 = the hole value
4376 __ jmp(&parameters_test);
4377
4378 __ bind(&parameters_loop);
4379 __ sub(r6, r6, Operand(Smi::FromInt(1)));
4380 __ mov(r5, Operand(r6, LSL, 1));
4381 __ add(r5, r5, Operand(kParameterMapHeaderSize - kHeapObjectTag));
4382 __ str(r9, MemOperand(r4, r5));
4383 __ sub(r5, r5, Operand(kParameterMapHeaderSize - FixedArray::kHeaderSize));
4384 __ str(r7, MemOperand(r3, r5));
4385 __ add(r9, r9, Operand(Smi::FromInt(1)));
4386 __ bind(&parameters_test);
4387 __ cmp(r6, Operand(Smi::FromInt(0)));
4388 __ b(ne, &parameters_loop);
4389
4390 __ bind(&skip_parameter_map);
4391 // r2 = argument count (tagged)
4392 // r3 = address of backing store (tagged)
4393 // r5 = scratch
4394 // Copy arguments header and remaining slots (if there are any).
4395 __ LoadRoot(r5, Heap::kFixedArrayMapRootIndex);
4396 __ str(r5, FieldMemOperand(r3, FixedArray::kMapOffset));
4397 __ str(r2, FieldMemOperand(r3, FixedArray::kLengthOffset));
4398
4399 Label arguments_loop, arguments_test;
4400 __ mov(r9, r1);
4401 __ ldr(r4, MemOperand(sp, 1 * kPointerSize));
4402 __ sub(r4, r4, Operand(r9, LSL, 1));
4403 __ jmp(&arguments_test);
4404
4405 __ bind(&arguments_loop);
4406 __ sub(r4, r4, Operand(kPointerSize));
4407 __ ldr(r6, MemOperand(r4, 0));
4408 __ add(r5, r3, Operand(r9, LSL, 1));
4409 __ str(r6, FieldMemOperand(r5, FixedArray::kHeaderSize));
4410 __ add(r9, r9, Operand(Smi::FromInt(1)));
4411
4412 __ bind(&arguments_test);
4413 __ cmp(r9, Operand(r2));
4414 __ b(lt, &arguments_loop);
4415
4416 // Return and remove the on-stack parameters.
4417 __ add(sp, sp, Operand(3 * kPointerSize));
4418 __ Ret();
4419
4420 // Do the runtime call to allocate the arguments object.
4421 // r2 = argument count (taggged)
4422 __ bind(&runtime);
4423 __ str(r2, MemOperand(sp, 0 * kPointerSize)); // Patch argument count.
4424 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
4425}
4426
4427
4428void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
4429 // sp[0] : number of parameters
4430 // sp[4] : receiver displacement
4431 // sp[8] : function
4432 // Check if the calling frame is an arguments adaptor frame.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004433 Label adaptor_frame, try_allocate, runtime;
4434 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4435 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
4436 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4437 __ b(eq, &adaptor_frame);
4438
4439 // Get the length from the frame.
4440 __ ldr(r1, MemOperand(sp, 0));
4441 __ b(&try_allocate);
4442
4443 // Patch the arguments.length and the parameters pointer.
4444 __ bind(&adaptor_frame);
4445 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4446 __ str(r1, MemOperand(sp, 0));
4447 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
4448 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
4449 __ str(r3, MemOperand(sp, 1 * kPointerSize));
4450
4451 // Try the new space allocation. Start out with computing the size
4452 // of the arguments object and the elements array in words.
4453 Label add_arguments_object;
4454 __ bind(&try_allocate);
Iain Merrick9ac36c92010-09-13 15:29:50 +01004455 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004456 __ b(eq, &add_arguments_object);
4457 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
4458 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
4459 __ bind(&add_arguments_object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004460 __ add(r1, r1, Operand(Heap::kArgumentsObjectSizeStrict / kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004461
4462 // Do the allocation of both objects in one go.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004463 __ AllocateInNewSpace(r1,
4464 r0,
4465 r2,
4466 r3,
4467 &runtime,
4468 static_cast<AllocationFlags>(TAG_OBJECT |
4469 SIZE_IN_WORDS));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004470
4471 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004472 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4473 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004474 __ ldr(r4, MemOperand(r4, Context::SlotOffset(
4475 Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004476
4477 // Copy the JS object part.
4478 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
4479
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004480 // Get the length (smi tagged) and set that as an in-object property too.
Steve Block44f0eee2011-05-26 01:26:41 +01004481 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004482 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01004483 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004484 Heap::kArgumentsLengthIndex * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004485
4486 // If there are no actual arguments, we're done.
4487 Label done;
Iain Merrick9ac36c92010-09-13 15:29:50 +01004488 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004489 __ b(eq, &done);
4490
4491 // Get the parameters pointer from the stack.
4492 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
4493
4494 // Setup the elements pointer in the allocated arguments object and
4495 // initialize the header in the elements fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004496 __ add(r4, r0, Operand(Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004497 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
4498 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
4499 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
4500 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004501 // Untag the length for the loop.
4502 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004503
4504 // Copy the fixed array slots.
4505 Label loop;
4506 // Setup r4 to point to the first array slot.
4507 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4508 __ bind(&loop);
4509 // Pre-decrement r2 with kPointerSize on each iteration.
4510 // Pre-decrement in order to skip receiver.
4511 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
4512 // Post-increment r4 with kPointerSize on each iteration.
4513 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
4514 __ sub(r1, r1, Operand(1));
Iain Merrick9ac36c92010-09-13 15:29:50 +01004515 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004516 __ b(ne, &loop);
4517
4518 // Return and remove the on-stack parameters.
4519 __ bind(&done);
4520 __ add(sp, sp, Operand(3 * kPointerSize));
4521 __ Ret();
4522
4523 // Do the runtime call to allocate the arguments object.
4524 __ bind(&runtime);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004525 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004526}
4527
4528
4529void RegExpExecStub::Generate(MacroAssembler* masm) {
4530 // Just jump directly to runtime if native RegExp is not selected at compile
4531 // time or if regexp entry in generated code is turned off runtime switch or
4532 // at compilation.
4533#ifdef V8_INTERPRETED_REGEXP
4534 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4535#else // V8_INTERPRETED_REGEXP
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004536
4537 // Stack frame on entry.
4538 // sp[0]: last_match_info (expected JSArray)
4539 // sp[4]: previous index
4540 // sp[8]: subject string
4541 // sp[12]: JSRegExp object
4542
4543 static const int kLastMatchInfoOffset = 0 * kPointerSize;
4544 static const int kPreviousIndexOffset = 1 * kPointerSize;
4545 static const int kSubjectOffset = 2 * kPointerSize;
4546 static const int kJSRegExpOffset = 3 * kPointerSize;
4547
4548 Label runtime, invoke_regexp;
4549
4550 // Allocation of registers for this function. These are in callee save
4551 // registers and will be preserved by the call to the native RegExp code, as
4552 // this code is called using the normal C calling convention. When calling
4553 // directly from generated code the native RegExp code will not do a GC and
4554 // therefore the content of these registers are safe to use after the call.
4555 Register subject = r4;
4556 Register regexp_data = r5;
4557 Register last_match_info_elements = r6;
4558
4559 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01004560 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004561 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01004562 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004563 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01004564 ExternalReference::address_of_regexp_stack_memory_size(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004565 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
4566 __ ldr(r0, MemOperand(r0, 0));
4567 __ tst(r0, Operand(r0));
4568 __ b(eq, &runtime);
4569
4570 // Check that the first argument is a JSRegExp object.
4571 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
4572 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004573 __ JumpIfSmi(r0, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004574 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
4575 __ b(ne, &runtime);
4576
4577 // Check that the RegExp has been compiled (data contains a fixed array).
4578 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
4579 if (FLAG_debug_code) {
4580 __ tst(regexp_data, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004581 __ Check(ne, "Unexpected type for RegExp data, FixedArray expected");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004582 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
4583 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
4584 }
4585
4586 // regexp_data: RegExp data (FixedArray)
4587 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
4588 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
4589 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
4590 __ b(ne, &runtime);
4591
4592 // regexp_data: RegExp data (FixedArray)
4593 // Check that the number of captures fit in the static offsets vector buffer.
4594 __ ldr(r2,
4595 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4596 // Calculate number of capture registers (number_of_captures + 1) * 2. This
4597 // uses the asumption that smis are 2 * their untagged value.
4598 STATIC_ASSERT(kSmiTag == 0);
4599 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4600 __ add(r2, r2, Operand(2)); // r2 was a smi.
4601 // Check that the static offsets vector buffer is large enough.
4602 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
4603 __ b(hi, &runtime);
4604
4605 // r2: Number of capture registers
4606 // regexp_data: RegExp data (FixedArray)
4607 // Check that the second argument is a string.
4608 __ ldr(subject, MemOperand(sp, kSubjectOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004609 __ JumpIfSmi(subject, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004610 Condition is_string = masm->IsObjectStringType(subject, r0);
4611 __ b(NegateCondition(is_string), &runtime);
4612 // Get the length of the string to r3.
4613 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
4614
4615 // r2: Number of capture registers
4616 // r3: Length of subject string as a smi
4617 // subject: Subject string
4618 // regexp_data: RegExp data (FixedArray)
4619 // Check that the third argument is a positive smi less than the subject
4620 // string length. A negative value will be greater (unsigned comparison).
4621 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004622 __ JumpIfNotSmi(r0, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004623 __ cmp(r3, Operand(r0));
4624 __ b(ls, &runtime);
4625
4626 // r2: Number of capture registers
4627 // subject: Subject string
4628 // regexp_data: RegExp data (FixedArray)
4629 // Check that the fourth object is a JSArray object.
4630 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004631 __ JumpIfSmi(r0, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004632 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
4633 __ b(ne, &runtime);
4634 // Check that the JSArray is in fast case.
4635 __ ldr(last_match_info_elements,
4636 FieldMemOperand(r0, JSArray::kElementsOffset));
4637 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
4638 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
4639 __ cmp(r0, ip);
4640 __ b(ne, &runtime);
4641 // Check that the last match info has space for the capture registers and the
4642 // additional information.
4643 __ ldr(r0,
4644 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
4645 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
4646 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
4647 __ b(gt, &runtime);
4648
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004649 // Reset offset for possibly sliced string.
4650 __ mov(r9, Operand(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004651 // subject: Subject string
4652 // regexp_data: RegExp data (FixedArray)
4653 // Check the representation and encoding of the subject string.
4654 Label seq_string;
4655 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4656 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004657 // First check for flat string. None of the following string type tests will
4658 // succeed if subject is not a string or a short external string.
4659 __ and_(r1,
4660 r0,
4661 Operand(kIsNotStringMask |
4662 kStringRepresentationMask |
4663 kShortExternalStringMask),
4664 SetCC);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004665 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
4666 __ b(eq, &seq_string);
4667
4668 // subject: Subject string
4669 // regexp_data: RegExp data (FixedArray)
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004670 // r1: whether subject is a string and if yes, its string representation
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004671 // Check for flat cons string or sliced string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004672 // A flat cons string is a cons string where the second part is the empty
4673 // string. In that case the subject string is just the first part of the cons
4674 // string. Also in this case the first part of the cons string is known to be
4675 // a sequential string or an external string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004676 // In the case of a sliced string its offset has to be taken into account.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004677 Label cons_string, external_string, check_encoding;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004678 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
4679 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004680 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
4681 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004682 __ cmp(r1, Operand(kExternalStringTag));
4683 __ b(lt, &cons_string);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004684 __ b(eq, &external_string);
4685
4686 // Catch non-string subject or short external string.
4687 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
4688 __ tst(r1, Operand(kIsNotStringMask | kShortExternalStringMask));
4689 __ b(ne, &runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004690
4691 // String is sliced.
4692 __ ldr(r9, FieldMemOperand(subject, SlicedString::kOffsetOffset));
4693 __ mov(r9, Operand(r9, ASR, kSmiTagSize));
4694 __ ldr(subject, FieldMemOperand(subject, SlicedString::kParentOffset));
4695 // r9: offset of sliced string, smi-tagged.
4696 __ jmp(&check_encoding);
4697 // String is a cons string, check whether it is flat.
4698 __ bind(&cons_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004699 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004700 __ CompareRoot(r0, Heap::kEmptyStringRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004701 __ b(ne, &runtime);
4702 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004703 // Is first part of cons or parent of slice a flat string?
4704 __ bind(&check_encoding);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004705 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4706 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004707 STATIC_ASSERT(kSeqStringTag == 0);
4708 __ tst(r0, Operand(kStringRepresentationMask));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004709 __ b(ne, &external_string);
4710
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004711 __ bind(&seq_string);
4712 // subject: Subject string
4713 // regexp_data: RegExp data (FixedArray)
4714 // r0: Instance type of subject string
4715 STATIC_ASSERT(4 == kAsciiStringTag);
4716 STATIC_ASSERT(kTwoByteStringTag == 0);
4717 // Find the code object based on the assumptions above.
4718 __ and_(r0, r0, Operand(kStringEncodingMask));
4719 __ mov(r3, Operand(r0, ASR, 2), SetCC);
4720 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
4721 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
4722
4723 // Check that the irregexp code has been generated for the actual string
4724 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00004725 // a smi (code flushing support).
4726 __ JumpIfSmi(r7, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004727
Steve Block44f0eee2011-05-26 01:26:41 +01004728 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004729 // r7: code
4730 // subject: Subject string
4731 // regexp_data: RegExp data (FixedArray)
4732 // Load used arguments before starting to push arguments for call to native
4733 // RegExp code to avoid handling changing stack height.
4734 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
4735 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
4736
4737 // r1: previous index
Steve Block44f0eee2011-05-26 01:26:41 +01004738 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004739 // r7: code
4740 // subject: Subject string
4741 // regexp_data: RegExp data (FixedArray)
4742 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01004743 __ IncrementCounter(isolate->counters()->regexp_entry_native(), 1, r0, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004744
Steve Block44f0eee2011-05-26 01:26:41 +01004745 // Isolates: note we add an additional parameter here (isolate pointer).
4746 static const int kRegExpExecuteArguments = 8;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004747 static const int kParameterRegisters = 4;
4748 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004749
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004750 // Stack pointer now points to cell where return address is to be written.
4751 // Arguments are before that on the stack or in registers.
4752
Steve Block44f0eee2011-05-26 01:26:41 +01004753 // Argument 8 (sp[16]): Pass current isolate address.
4754 __ mov(r0, Operand(ExternalReference::isolate_address()));
4755 __ str(r0, MemOperand(sp, 4 * kPointerSize));
4756
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004757 // Argument 7 (sp[12]): Indicate that this is a direct call from JavaScript.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004758 __ mov(r0, Operand(1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004759 __ str(r0, MemOperand(sp, 3 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004760
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004761 // Argument 6 (sp[8]): Start (high end) of backtracking stack memory area.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004762 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
4763 __ ldr(r0, MemOperand(r0, 0));
4764 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
4765 __ ldr(r2, MemOperand(r2, 0));
4766 __ add(r0, r0, Operand(r2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004767 __ str(r0, MemOperand(sp, 2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004768
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004769 // Argument 5 (sp[4]): static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01004770 __ mov(r0,
4771 Operand(ExternalReference::address_of_static_offsets_vector(isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004772 __ str(r0, MemOperand(sp, 1 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004773
4774 // For arguments 4 and 3 get string length, calculate start of string data and
4775 // calculate the shift of the index (0 for ASCII and 1 for two byte).
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004776 __ add(r8, subject, Operand(SeqString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004777 __ eor(r3, r3, Operand(1));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004778 // Load the length from the original subject string from the previous stack
4779 // frame. Therefore we have to use fp, which points exactly to two pointer
4780 // sizes below the previous sp. (Because creating a new stack frame pushes
4781 // the previous fp onto the stack and moves up sp by 2 * kPointerSize.)
Ben Murdoch589d6972011-11-30 16:04:58 +00004782 __ ldr(subject, MemOperand(fp, kSubjectOffset + 2 * kPointerSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004783 // If slice offset is not 0, load the length from the original sliced string.
4784 // Argument 4, r3: End of string data
4785 // Argument 3, r2: Start of string data
4786 // Prepare start and end index of the input.
4787 __ add(r9, r8, Operand(r9, LSL, r3));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004788 __ add(r2, r9, Operand(r1, LSL, r3));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004789
Ben Murdoch589d6972011-11-30 16:04:58 +00004790 __ ldr(r8, FieldMemOperand(subject, String::kLengthOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004791 __ mov(r8, Operand(r8, ASR, kSmiTagSize));
4792 __ add(r3, r9, Operand(r8, LSL, r3));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004793
4794 // Argument 2 (r1): Previous index.
4795 // Already there
4796
4797 // Argument 1 (r0): Subject string.
Ben Murdoch589d6972011-11-30 16:04:58 +00004798 __ mov(r0, subject);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004799
4800 // Locate the code entry and call it.
4801 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004802 DirectCEntryStub stub;
4803 stub.GenerateCall(masm, r7);
4804
4805 __ LeaveExitFrame(false, no_reg);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004806
4807 // r0: result
4808 // subject: subject string (callee saved)
4809 // regexp_data: RegExp data (callee saved)
4810 // last_match_info_elements: Last match info elements (callee saved)
4811
4812 // Check the result.
4813 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004814
Ben Murdoch589d6972011-11-30 16:04:58 +00004815 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004816 __ b(eq, &success);
4817 Label failure;
Ben Murdoch589d6972011-11-30 16:04:58 +00004818 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004819 __ b(eq, &failure);
Ben Murdoch589d6972011-11-30 16:04:58 +00004820 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004821 // If not exception it can only be retry. Handle that in the runtime system.
4822 __ b(ne, &runtime);
4823 // Result must now be exception. If there is no pending exception already a
4824 // stack overflow (on the backtrack stack) was detected in RegExp code but
4825 // haven't created the exception yet. Handle that in the runtime system.
4826 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004827 __ mov(r1, Operand(isolate->factory()->the_hole_value()));
Ben Murdoch589d6972011-11-30 16:04:58 +00004828 __ mov(r2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01004829 isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004830 __ ldr(r0, MemOperand(r2, 0));
Ben Murdoch589d6972011-11-30 16:04:58 +00004831 __ cmp(r0, r1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004832 __ b(eq, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004833
4834 __ str(r1, MemOperand(r2, 0)); // Clear pending exception.
4835
4836 // Check if the exception is a termination. If so, throw as uncatchable.
Ben Murdoch589d6972011-11-30 16:04:58 +00004837 __ CompareRoot(r0, Heap::kTerminationExceptionRootIndex);
4838
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004839 Label termination_exception;
4840 __ b(eq, &termination_exception);
4841
Ben Murdoch589d6972011-11-30 16:04:58 +00004842 __ Throw(r0); // Expects thrown value in r0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004843
4844 __ bind(&termination_exception);
4845 __ ThrowUncatchable(TERMINATION, r0); // Expects thrown value in r0.
4846
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004847 __ bind(&failure);
4848 // For failure and exception return null.
Ben Murdoch257744e2011-11-30 15:57:28 +00004849 __ mov(r0, Operand(masm->isolate()->factory()->null_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004850 __ add(sp, sp, Operand(4 * kPointerSize));
4851 __ Ret();
4852
4853 // Process the result from the native regexp code.
4854 __ bind(&success);
4855 __ ldr(r1,
4856 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4857 // Calculate number of capture registers (number_of_captures + 1) * 2.
4858 STATIC_ASSERT(kSmiTag == 0);
4859 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4860 __ add(r1, r1, Operand(2)); // r1 was a smi.
4861
4862 // r1: number of capture registers
4863 // r4: subject string
4864 // Store the capture count.
4865 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
4866 __ str(r2, FieldMemOperand(last_match_info_elements,
4867 RegExpImpl::kLastCaptureCountOffset));
4868 // Store last subject and last input.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004869 __ str(subject,
4870 FieldMemOperand(last_match_info_elements,
4871 RegExpImpl::kLastSubjectOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004872 __ mov(r2, subject);
4873 __ RecordWriteField(last_match_info_elements,
4874 RegExpImpl::kLastSubjectOffset,
4875 r2,
4876 r7,
4877 kLRHasNotBeenSaved,
4878 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004879 __ str(subject,
4880 FieldMemOperand(last_match_info_elements,
4881 RegExpImpl::kLastInputOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004882 __ RecordWriteField(last_match_info_elements,
4883 RegExpImpl::kLastInputOffset,
4884 subject,
4885 r7,
4886 kLRHasNotBeenSaved,
4887 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004888
4889 // Get the static offsets vector filled by the native regexp code.
4890 ExternalReference address_of_static_offsets_vector =
Steve Block44f0eee2011-05-26 01:26:41 +01004891 ExternalReference::address_of_static_offsets_vector(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004892 __ mov(r2, Operand(address_of_static_offsets_vector));
4893
4894 // r1: number of capture registers
4895 // r2: offsets vector
4896 Label next_capture, done;
4897 // Capture register counter starts from number of capture registers and
4898 // counts down until wraping after zero.
4899 __ add(r0,
4900 last_match_info_elements,
4901 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
4902 __ bind(&next_capture);
4903 __ sub(r1, r1, Operand(1), SetCC);
4904 __ b(mi, &done);
4905 // Read the value from the static offsets vector buffer.
4906 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
4907 // Store the smi value in the last match info.
4908 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
4909 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
4910 __ jmp(&next_capture);
4911 __ bind(&done);
4912
4913 // Return last match info.
4914 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
4915 __ add(sp, sp, Operand(4 * kPointerSize));
4916 __ Ret();
4917
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004918 // External string. Short external strings have already been ruled out.
4919 // r0: scratch
4920 __ bind(&external_string);
4921 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4922 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
4923 if (FLAG_debug_code) {
4924 // Assert that we do not have a cons or slice (indirect strings) here.
4925 // Sequential strings have already been ruled out.
4926 __ tst(r0, Operand(kIsIndirectStringMask));
4927 __ Assert(eq, "external string expected, but not found");
4928 }
4929 __ ldr(subject,
4930 FieldMemOperand(subject, ExternalString::kResourceDataOffset));
4931 // Move the pointer so that offset-wise, it looks like a sequential string.
4932 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize);
4933 __ sub(subject,
4934 subject,
4935 Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4936 __ jmp(&seq_string);
4937
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004938 // Do the runtime call to execute the regexp.
4939 __ bind(&runtime);
4940 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4941#endif // V8_INTERPRETED_REGEXP
4942}
4943
4944
Ben Murdochb0fe1622011-05-05 13:52:32 +01004945void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
4946 const int kMaxInlineLength = 100;
4947 Label slowcase;
4948 Label done;
Ben Murdoch257744e2011-11-30 15:57:28 +00004949 Factory* factory = masm->isolate()->factory();
4950
Ben Murdochb0fe1622011-05-05 13:52:32 +01004951 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4952 STATIC_ASSERT(kSmiTag == 0);
4953 STATIC_ASSERT(kSmiTagSize == 1);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004954 __ JumpIfNotSmi(r1, &slowcase);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004955 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4956 __ b(hi, &slowcase);
4957 // Smi-tagging is equivalent to multiplying by 2.
4958 // Allocate RegExpResult followed by FixedArray with size in ebx.
4959 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4960 // Elements: [Map][Length][..elements..]
4961 // Size of JSArray with two in-object properties and the header of a
4962 // FixedArray.
4963 int objects_size =
4964 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4965 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4966 __ add(r2, r5, Operand(objects_size));
4967 __ AllocateInNewSpace(
4968 r2, // In: Size, in words.
4969 r0, // Out: Start of allocation (tagged).
4970 r3, // Scratch register.
4971 r4, // Scratch register.
4972 &slowcase,
4973 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
4974 // r0: Start of allocated area, object-tagged.
4975 // r1: Number of elements in array, as smi.
4976 // r5: Number of elements, untagged.
4977
4978 // Set JSArray map to global.regexp_result_map().
4979 // Set empty properties FixedArray.
4980 // Set elements to point to FixedArray allocated right after the JSArray.
4981 // Interleave operations for better latency.
4982 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4983 __ add(r3, r0, Operand(JSRegExpResult::kSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00004984 __ mov(r4, Operand(factory->empty_fixed_array()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004985 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4986 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4987 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4988 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4989 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4990
4991 // Set input, index and length fields from arguments.
4992 __ ldr(r1, MemOperand(sp, kPointerSize * 0));
4993 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4994 __ ldr(r1, MemOperand(sp, kPointerSize * 1));
4995 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4996 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4997 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4998
4999 // Fill out the elements FixedArray.
5000 // r0: JSArray, tagged.
5001 // r3: FixedArray, tagged.
5002 // r5: Number of elements in array, untagged.
5003
5004 // Set map.
Ben Murdoch257744e2011-11-30 15:57:28 +00005005 __ mov(r2, Operand(factory->fixed_array_map()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005006 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
5007 // Set FixedArray length.
5008 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
5009 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
5010 // Fill contents of fixed-array with the-hole.
Ben Murdoch257744e2011-11-30 15:57:28 +00005011 __ mov(r2, Operand(factory->the_hole_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005012 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5013 // Fill fixed array elements with hole.
5014 // r0: JSArray, tagged.
5015 // r2: the hole.
5016 // r3: Start of elements in FixedArray.
5017 // r5: Number of elements to fill.
5018 Label loop;
5019 __ tst(r5, Operand(r5));
5020 __ bind(&loop);
5021 __ b(le, &done); // Jump if r1 is negative or zero.
5022 __ sub(r5, r5, Operand(1), SetCC);
5023 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
5024 __ jmp(&loop);
5025
5026 __ bind(&done);
5027 __ add(sp, sp, Operand(3 * kPointerSize));
5028 __ Ret();
5029
5030 __ bind(&slowcase);
5031 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
5032}
5033
5034
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005035void CallFunctionStub::FinishCode(Handle<Code> code) {
5036 code->set_has_function_cache(false);
5037}
5038
5039
5040void CallFunctionStub::Clear(Heap* heap, Address address) {
5041 UNREACHABLE();
5042}
5043
5044
5045Object* CallFunctionStub::GetCachedValue(Address address) {
5046 UNREACHABLE();
5047 return NULL;
5048}
5049
5050
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005051void CallFunctionStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005052 // r1 : the function to call
Ben Murdoch589d6972011-11-30 16:04:58 +00005053 Label slow, non_function;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005054
Ben Murdoch257744e2011-11-30 15:57:28 +00005055 // The receiver might implicitly be the global object. This is
5056 // indicated by passing the hole as the receiver to the call
5057 // function stub.
5058 if (ReceiverMightBeImplicit()) {
5059 Label call;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005060 // Get the receiver from the stack.
5061 // function, receiver [, arguments]
Ben Murdoch257744e2011-11-30 15:57:28 +00005062 __ ldr(r4, MemOperand(sp, argc_ * kPointerSize));
5063 // Call as function is indicated with the hole.
5064 __ CompareRoot(r4, Heap::kTheHoleValueRootIndex);
5065 __ b(ne, &call);
5066 // Patch the receiver on the stack with the global receiver object.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005067 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5068 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalReceiverOffset));
5069 __ str(r2, MemOperand(sp, argc_ * kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00005070 __ bind(&call);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005071 }
5072
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005073 // Check that the function is really a JavaScript function.
5074 // r1: pushed function (to be verified)
Ben Murdoch589d6972011-11-30 16:04:58 +00005075 __ JumpIfSmi(r1, &non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005076 // Get the map of the function object.
5077 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
5078 __ b(ne, &slow);
5079
5080 // Fast-case: Invoke the function now.
5081 // r1: pushed function
5082 ParameterCount actual(argc_);
Ben Murdoch257744e2011-11-30 15:57:28 +00005083
5084 if (ReceiverMightBeImplicit()) {
5085 Label call_as_function;
5086 __ CompareRoot(r4, Heap::kTheHoleValueRootIndex);
5087 __ b(eq, &call_as_function);
5088 __ InvokeFunction(r1,
5089 actual,
5090 JUMP_FUNCTION,
5091 NullCallWrapper(),
5092 CALL_AS_METHOD);
5093 __ bind(&call_as_function);
5094 }
5095 __ InvokeFunction(r1,
5096 actual,
5097 JUMP_FUNCTION,
5098 NullCallWrapper(),
5099 CALL_AS_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005100
5101 // Slow-case: Non-function called.
5102 __ bind(&slow);
Ben Murdoch589d6972011-11-30 16:04:58 +00005103 // Check for function proxy.
5104 __ cmp(r2, Operand(JS_FUNCTION_PROXY_TYPE));
5105 __ b(ne, &non_function);
5106 __ push(r1); // put proxy as additional argument
5107 __ mov(r0, Operand(argc_ + 1, RelocInfo::NONE));
5108 __ mov(r2, Operand(0, RelocInfo::NONE));
5109 __ GetBuiltinEntry(r3, Builtins::CALL_FUNCTION_PROXY);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005110 __ SetCallKind(r5, CALL_AS_METHOD);
Ben Murdoch589d6972011-11-30 16:04:58 +00005111 {
5112 Handle<Code> adaptor =
5113 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
5114 __ Jump(adaptor, RelocInfo::CODE_TARGET);
5115 }
5116
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005117 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
5118 // of the original receiver from the call site).
Ben Murdoch589d6972011-11-30 16:04:58 +00005119 __ bind(&non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005120 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
5121 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005122 __ mov(r2, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005123 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005124 __ SetCallKind(r5, CALL_AS_METHOD);
Steve Block44f0eee2011-05-26 01:26:41 +01005125 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005126 RelocInfo::CODE_TARGET);
5127}
5128
5129
5130// Unfortunately you have to run without snapshots to see most of these
5131// names in the profile since most compare stubs end up in the snapshot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005132void CompareStub::PrintName(StringStream* stream) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005133 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
5134 (lhs_.is(r1) && rhs_.is(r0)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005135 const char* cc_name;
5136 switch (cc_) {
5137 case lt: cc_name = "LT"; break;
5138 case gt: cc_name = "GT"; break;
5139 case le: cc_name = "LE"; break;
5140 case ge: cc_name = "GE"; break;
5141 case eq: cc_name = "EQ"; break;
5142 case ne: cc_name = "NE"; break;
5143 default: cc_name = "UnknownCondition"; break;
5144 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005145 bool is_equality = cc_ == eq || cc_ == ne;
5146 stream->Add("CompareStub_%s", cc_name);
5147 stream->Add(lhs_.is(r0) ? "_r0" : "_r1");
5148 stream->Add(rhs_.is(r0) ? "_r0" : "_r1");
5149 if (strict_ && is_equality) stream->Add("_STRICT");
5150 if (never_nan_nan_ && is_equality) stream->Add("_NO_NAN");
5151 if (!include_number_compare_) stream->Add("_NO_NUMBER");
5152 if (!include_smi_compare_) stream->Add("_NO_SMI");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005153}
5154
5155
5156int CompareStub::MinorKey() {
5157 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
5158 // stubs the never NaN NaN condition is only taken into account if the
5159 // condition is equals.
5160 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
5161 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
5162 (lhs_.is(r1) && rhs_.is(r0)));
5163 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
5164 | RegisterField::encode(lhs_.is(r0))
5165 | StrictField::encode(strict_)
5166 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005167 | IncludeNumberCompareField::encode(include_number_compare_)
5168 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005169}
5170
5171
5172// StringCharCodeAtGenerator
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005173void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
5174 Label flat_string;
5175 Label ascii_string;
5176 Label got_char_code;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005177 Label sliced_string;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005178
5179 // If the receiver is a smi trigger the non-string case.
Steve Block1e0659c2011-05-24 12:43:12 +01005180 __ JumpIfSmi(object_, receiver_not_string_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005181
5182 // Fetch the instance type of the receiver into result register.
5183 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5184 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
5185 // If the receiver is not a string trigger the non-string case.
5186 __ tst(result_, Operand(kIsNotStringMask));
5187 __ b(ne, receiver_not_string_);
5188
5189 // If the index is non-smi trigger the non-smi case.
Steve Block1e0659c2011-05-24 12:43:12 +01005190 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005191 __ bind(&got_smi_index_);
5192
5193 // Check for index out of range.
5194 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005195 __ cmp(ip, Operand(index_));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005196 __ b(ls, index_out_of_range_);
5197
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005198 __ mov(index_, Operand(index_, ASR, kSmiTagSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005199
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005200 StringCharLoadGenerator::Generate(masm,
5201 object_,
5202 index_,
5203 result_,
5204 &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005205
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005206 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
5207 __ bind(&exit_);
5208}
5209
5210
5211void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005212 MacroAssembler* masm,
5213 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005214 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
5215
5216 // Index is not a smi.
5217 __ bind(&index_not_smi_);
5218 // If index is a heap number, try converting it to an integer.
5219 __ CheckMap(index_,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005220 result_,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005221 Heap::kHeapNumberMapRootIndex,
5222 index_not_number_,
Ben Murdoch257744e2011-11-30 15:57:28 +00005223 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005224 call_helper.BeforeCall(masm);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005225 __ push(object_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005226 __ push(index_); // Consumed by runtime conversion function.
5227 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
5228 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
5229 } else {
5230 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
5231 // NumberToSmi discards numbers that are not exact integers.
5232 __ CallRuntime(Runtime::kNumberToSmi, 1);
5233 }
5234 // Save the conversion result before the pop instructions below
5235 // have a chance to overwrite it.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005236 __ Move(index_, r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005237 __ pop(object_);
5238 // Reload the instance type.
5239 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5240 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
5241 call_helper.AfterCall(masm);
5242 // If index is still not a smi, it must be out of range.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005243 __ JumpIfNotSmi(index_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005244 // Otherwise, return to the fast path.
5245 __ jmp(&got_smi_index_);
5246
5247 // Call runtime. We get here when the receiver is a string and the
5248 // index is a number, but the code of getting the actual character
5249 // is too complex (e.g., when the string needs to be flattened).
5250 __ bind(&call_runtime_);
5251 call_helper.BeforeCall(masm);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005252 __ mov(index_, Operand(index_, LSL, kSmiTagSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005253 __ Push(object_, index_);
5254 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
5255 __ Move(result_, r0);
5256 call_helper.AfterCall(masm);
5257 __ jmp(&exit_);
5258
5259 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
5260}
5261
5262
5263// -------------------------------------------------------------------------
5264// StringCharFromCodeGenerator
5265
5266void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
5267 // Fast case of Heap::LookupSingleCharacterStringFromCode.
5268 STATIC_ASSERT(kSmiTag == 0);
5269 STATIC_ASSERT(kSmiShiftSize == 0);
5270 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
5271 __ tst(code_,
5272 Operand(kSmiTagMask |
5273 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
Steve Block1e0659c2011-05-24 12:43:12 +01005274 __ b(ne, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005275
5276 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01005277 // At this point code register contains smi tagged ASCII char code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005278 STATIC_ASSERT(kSmiTag == 0);
5279 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
5280 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
5281 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
5282 __ cmp(result_, Operand(ip));
5283 __ b(eq, &slow_case_);
5284 __ bind(&exit_);
5285}
5286
5287
5288void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005289 MacroAssembler* masm,
5290 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005291 __ Abort("Unexpected fallthrough to CharFromCode slow case");
5292
5293 __ bind(&slow_case_);
5294 call_helper.BeforeCall(masm);
5295 __ push(code_);
5296 __ CallRuntime(Runtime::kCharFromCode, 1);
5297 __ Move(result_, r0);
5298 call_helper.AfterCall(masm);
5299 __ jmp(&exit_);
5300
5301 __ Abort("Unexpected fallthrough from CharFromCode slow case");
5302}
5303
5304
5305// -------------------------------------------------------------------------
5306// StringCharAtGenerator
5307
5308void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
5309 char_code_at_generator_.GenerateFast(masm);
5310 char_from_code_generator_.GenerateFast(masm);
5311}
5312
5313
5314void StringCharAtGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005315 MacroAssembler* masm,
5316 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005317 char_code_at_generator_.GenerateSlow(masm, call_helper);
5318 char_from_code_generator_.GenerateSlow(masm, call_helper);
5319}
5320
5321
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005322void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5323 Register dest,
5324 Register src,
5325 Register count,
5326 Register scratch,
5327 bool ascii) {
5328 Label loop;
5329 Label done;
5330 // This loop just copies one character at a time, as it is only used for very
5331 // short strings.
5332 if (!ascii) {
5333 __ add(count, count, Operand(count), SetCC);
5334 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01005335 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005336 }
5337 __ b(eq, &done);
5338
5339 __ bind(&loop);
5340 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
5341 // Perform sub between load and dependent store to get the load time to
5342 // complete.
5343 __ sub(count, count, Operand(1), SetCC);
5344 __ strb(scratch, MemOperand(dest, 1, PostIndex));
5345 // last iteration.
5346 __ b(gt, &loop);
5347
5348 __ bind(&done);
5349}
5350
5351
5352enum CopyCharactersFlags {
5353 COPY_ASCII = 1,
5354 DEST_ALWAYS_ALIGNED = 2
5355};
5356
5357
5358void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
5359 Register dest,
5360 Register src,
5361 Register count,
5362 Register scratch1,
5363 Register scratch2,
5364 Register scratch3,
5365 Register scratch4,
5366 Register scratch5,
5367 int flags) {
5368 bool ascii = (flags & COPY_ASCII) != 0;
5369 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
5370
5371 if (dest_always_aligned && FLAG_debug_code) {
5372 // Check that destination is actually word aligned if the flag says
5373 // that it is.
5374 __ tst(dest, Operand(kPointerAlignmentMask));
5375 __ Check(eq, "Destination of copy not aligned.");
5376 }
5377
5378 const int kReadAlignment = 4;
5379 const int kReadAlignmentMask = kReadAlignment - 1;
5380 // Ensure that reading an entire aligned word containing the last character
5381 // of a string will not read outside the allocated area (because we pad up
5382 // to kObjectAlignment).
5383 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
5384 // Assumes word reads and writes are little endian.
5385 // Nothing to do for zero characters.
5386 Label done;
5387 if (!ascii) {
5388 __ add(count, count, Operand(count), SetCC);
5389 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01005390 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005391 }
5392 __ b(eq, &done);
5393
5394 // Assume that you cannot read (or write) unaligned.
5395 Label byte_loop;
5396 // Must copy at least eight bytes, otherwise just do it one byte at a time.
5397 __ cmp(count, Operand(8));
5398 __ add(count, dest, Operand(count));
5399 Register limit = count; // Read until src equals this.
5400 __ b(lt, &byte_loop);
5401
5402 if (!dest_always_aligned) {
5403 // Align dest by byte copying. Copies between zero and three bytes.
5404 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
5405 Label dest_aligned;
5406 __ b(eq, &dest_aligned);
5407 __ cmp(scratch4, Operand(2));
5408 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
5409 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
5410 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
5411 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5412 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
5413 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
5414 __ bind(&dest_aligned);
5415 }
5416
5417 Label simple_loop;
5418
5419 __ sub(scratch4, dest, Operand(src));
5420 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
5421 __ b(eq, &simple_loop);
5422 // Shift register is number of bits in a source word that
5423 // must be combined with bits in the next source word in order
5424 // to create a destination word.
5425
5426 // Complex loop for src/dst that are not aligned the same way.
5427 {
5428 Label loop;
5429 __ mov(scratch4, Operand(scratch4, LSL, 3));
5430 Register left_shift = scratch4;
5431 __ and_(src, src, Operand(~3)); // Round down to load previous word.
5432 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
5433 // Store the "shift" most significant bits of scratch in the least
5434 // signficant bits (i.e., shift down by (32-shift)).
5435 __ rsb(scratch2, left_shift, Operand(32));
5436 Register right_shift = scratch2;
5437 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
5438
5439 __ bind(&loop);
5440 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
5441 __ sub(scratch5, limit, Operand(dest));
5442 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
5443 __ str(scratch1, MemOperand(dest, 4, PostIndex));
5444 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
5445 // Loop if four or more bytes left to copy.
5446 // Compare to eight, because we did the subtract before increasing dst.
5447 __ sub(scratch5, scratch5, Operand(8), SetCC);
5448 __ b(ge, &loop);
5449 }
5450 // There is now between zero and three bytes left to copy (negative that
5451 // number is in scratch5), and between one and three bytes already read into
5452 // scratch1 (eight times that number in scratch4). We may have read past
5453 // the end of the string, but because objects are aligned, we have not read
5454 // past the end of the object.
5455 // Find the minimum of remaining characters to move and preloaded characters
5456 // and write those as bytes.
5457 __ add(scratch5, scratch5, Operand(4), SetCC);
5458 __ b(eq, &done);
5459 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
5460 // Move minimum of bytes read and bytes left to copy to scratch4.
5461 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
5462 // Between one and three (value in scratch5) characters already read into
5463 // scratch ready to write.
5464 __ cmp(scratch5, Operand(2));
5465 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5466 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
5467 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
5468 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
5469 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
5470 // Copy any remaining bytes.
5471 __ b(&byte_loop);
5472
5473 // Simple loop.
5474 // Copy words from src to dst, until less than four bytes left.
5475 // Both src and dest are word aligned.
5476 __ bind(&simple_loop);
5477 {
5478 Label loop;
5479 __ bind(&loop);
5480 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
5481 __ sub(scratch3, limit, Operand(dest));
5482 __ str(scratch1, MemOperand(dest, 4, PostIndex));
5483 // Compare to 8, not 4, because we do the substraction before increasing
5484 // dest.
5485 __ cmp(scratch3, Operand(8));
5486 __ b(ge, &loop);
5487 }
5488
5489 // Copy bytes from src to dst until dst hits limit.
5490 __ bind(&byte_loop);
5491 __ cmp(dest, Operand(limit));
5492 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
5493 __ b(ge, &done);
5494 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5495 __ b(&byte_loop);
5496
5497 __ bind(&done);
5498}
5499
5500
5501void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5502 Register c1,
5503 Register c2,
5504 Register scratch1,
5505 Register scratch2,
5506 Register scratch3,
5507 Register scratch4,
5508 Register scratch5,
5509 Label* not_found) {
5510 // Register scratch3 is the general scratch register in this function.
5511 Register scratch = scratch3;
5512
5513 // Make sure that both characters are not digits as such strings has a
5514 // different hash algorithm. Don't try to look for these in the symbol table.
5515 Label not_array_index;
5516 __ sub(scratch, c1, Operand(static_cast<int>('0')));
5517 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
5518 __ b(hi, &not_array_index);
5519 __ sub(scratch, c2, Operand(static_cast<int>('0')));
5520 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
5521
5522 // If check failed combine both characters into single halfword.
5523 // This is required by the contract of the method: code at the
5524 // not_found branch expects this combination in c1 register
5525 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
5526 __ b(ls, not_found);
5527
5528 __ bind(&not_array_index);
5529 // Calculate the two character string hash.
5530 Register hash = scratch1;
5531 StringHelper::GenerateHashInit(masm, hash, c1);
5532 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
5533 StringHelper::GenerateHashGetHash(masm, hash);
5534
5535 // Collect the two characters in a register.
5536 Register chars = c1;
5537 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
5538
5539 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5540 // hash: hash of two character string.
5541
5542 // Load symbol table
5543 // Load address of first element of the symbol table.
5544 Register symbol_table = c2;
5545 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
5546
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005547 Register undefined = scratch4;
5548 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
5549
5550 // Calculate capacity mask from the symbol table capacity.
5551 Register mask = scratch2;
5552 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
5553 __ mov(mask, Operand(mask, ASR, 1));
5554 __ sub(mask, mask, Operand(1));
5555
5556 // Calculate untagged address of the first element of the symbol table.
5557 Register first_symbol_table_element = symbol_table;
5558 __ add(first_symbol_table_element, symbol_table,
5559 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
5560
5561 // Registers
5562 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5563 // hash: hash of two character string
5564 // mask: capacity mask
5565 // first_symbol_table_element: address of the first element of
5566 // the symbol table
Steve Block44f0eee2011-05-26 01:26:41 +01005567 // undefined: the undefined object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005568 // scratch: -
5569
5570 // Perform a number of probes in the symbol table.
5571 static const int kProbes = 4;
5572 Label found_in_symbol_table;
5573 Label next_probe[kProbes];
Ben Murdoch692be652012-01-10 18:47:50 +00005574 Register candidate = scratch5; // Scratch register contains candidate.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005575 for (int i = 0; i < kProbes; i++) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005576 // Calculate entry in symbol table.
5577 if (i > 0) {
5578 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
5579 } else {
5580 __ mov(candidate, hash);
5581 }
5582
5583 __ and_(candidate, candidate, Operand(mask));
5584
5585 // Load the entry from the symble table.
5586 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5587 __ ldr(candidate,
5588 MemOperand(first_symbol_table_element,
5589 candidate,
5590 LSL,
5591 kPointerSizeLog2));
5592
5593 // If entry is undefined no string with this hash can be found.
Steve Block44f0eee2011-05-26 01:26:41 +01005594 Label is_string;
5595 __ CompareObjectType(candidate, scratch, scratch, ODDBALL_TYPE);
5596 __ b(ne, &is_string);
5597
5598 __ cmp(undefined, candidate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005599 __ b(eq, not_found);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005600 // Must be the hole (deleted entry).
Steve Block44f0eee2011-05-26 01:26:41 +01005601 if (FLAG_debug_code) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005602 __ LoadRoot(ip, Heap::kTheHoleValueRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01005603 __ cmp(ip, candidate);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005604 __ Assert(eq, "oddball in symbol table is not undefined or the hole");
Steve Block44f0eee2011-05-26 01:26:41 +01005605 }
5606 __ jmp(&next_probe[i]);
5607
5608 __ bind(&is_string);
5609
5610 // Check that the candidate is a non-external ASCII string. The instance
5611 // type is still in the scratch register from the CompareObjectType
5612 // operation.
5613 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, &next_probe[i]);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005614
5615 // If length is not 2 the string is not a candidate.
5616 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
5617 __ cmp(scratch, Operand(Smi::FromInt(2)));
5618 __ b(ne, &next_probe[i]);
5619
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005620 // Check if the two characters match.
5621 // Assumes that word load is little endian.
5622 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
5623 __ cmp(chars, scratch);
5624 __ b(eq, &found_in_symbol_table);
5625 __ bind(&next_probe[i]);
5626 }
5627
5628 // No matching 2 character string found by probing.
5629 __ jmp(not_found);
5630
5631 // Scratch register contains result when we fall through to here.
Ben Murdoch692be652012-01-10 18:47:50 +00005632 Register result = candidate;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005633 __ bind(&found_in_symbol_table);
5634 __ Move(r0, result);
5635}
5636
5637
5638void StringHelper::GenerateHashInit(MacroAssembler* masm,
5639 Register hash,
5640 Register character) {
5641 // hash = character + (character << 10);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005642 __ add(hash, character, Operand(character, LSL, 10));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005643 // hash ^= hash >> 6;
Ben Murdoch692be652012-01-10 18:47:50 +00005644 __ eor(hash, hash, Operand(hash, LSR, 6));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005645}
5646
5647
5648void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5649 Register hash,
5650 Register character) {
5651 // hash += character;
5652 __ add(hash, hash, Operand(character));
5653 // hash += hash << 10;
5654 __ add(hash, hash, Operand(hash, LSL, 10));
5655 // hash ^= hash >> 6;
Ben Murdoch692be652012-01-10 18:47:50 +00005656 __ eor(hash, hash, Operand(hash, LSR, 6));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005657}
5658
5659
5660void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5661 Register hash) {
5662 // hash += hash << 3;
5663 __ add(hash, hash, Operand(hash, LSL, 3));
5664 // hash ^= hash >> 11;
Ben Murdoch692be652012-01-10 18:47:50 +00005665 __ eor(hash, hash, Operand(hash, LSR, 11));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005666 // hash += hash << 15;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005667 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
Ben Murdoch692be652012-01-10 18:47:50 +00005668
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005669 uint32_t kHashShiftCutOffMask = (1 << (32 - String::kHashShift)) - 1;
5670 __ and_(hash, hash, Operand(kHashShiftCutOffMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005671
5672 // if (hash == 0) hash = 27;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005673 __ mov(hash, Operand(27), LeaveCC, eq);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005674}
5675
5676
5677void SubStringStub::Generate(MacroAssembler* masm) {
5678 Label runtime;
5679
5680 // Stack frame on entry.
5681 // lr: return address
5682 // sp[0]: to
5683 // sp[4]: from
5684 // sp[8]: string
5685
5686 // This stub is called from the native-call %_SubString(...), so
5687 // nothing can be assumed about the arguments. It is tested that:
5688 // "string" is a sequential string,
5689 // both "from" and "to" are smis, and
5690 // 0 <= from <= to <= string.length.
5691 // If any of these assumptions fail, we call the runtime system.
5692
5693 static const int kToOffset = 0 * kPointerSize;
5694 static const int kFromOffset = 1 * kPointerSize;
5695 static const int kStringOffset = 2 * kPointerSize;
5696
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005697 // Check bounds and smi-ness.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005698 Register to = r6;
5699 Register from = r7;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005700
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005701 __ Ldrd(to, from, MemOperand(sp, kToOffset));
5702 STATIC_ASSERT(kFromOffset == kToOffset + 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005703 STATIC_ASSERT(kSmiTag == 0);
5704 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005705
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005706 // I.e., arithmetic shift right by one un-smi-tags.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005707 __ mov(r2, Operand(to, ASR, 1), SetCC);
5708 __ mov(r3, Operand(from, ASR, 1), SetCC, cc);
5709 // If either to or from had the smi tag bit set, then carry is set now.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005710 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
5711 __ b(mi, &runtime); // From is negative.
5712
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005713 // Both to and from are smis.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005714 __ sub(r2, r2, Operand(r3), SetCC);
5715 __ b(mi, &runtime); // Fail if from > to.
5716 // Special handling of sub-strings of length 1 and 2. One character strings
5717 // are handled in the runtime system (looked up in the single character
Ben Murdoch589d6972011-11-30 16:04:58 +00005718 // cache). Two character strings are looked for in the symbol cache in
5719 // generated code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005720 __ cmp(r2, Operand(2));
5721 __ b(lt, &runtime);
5722
Ben Murdoch589d6972011-11-30 16:04:58 +00005723 // r2: result string length
5724 // r3: from index (untagged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005725 // r6 (a.k.a. to): to (smi)
5726 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005727 // Make sure first argument is a sequential (or flat) string.
Ben Murdoch589d6972011-11-30 16:04:58 +00005728 __ ldr(r0, MemOperand(sp, kStringOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005729 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00005730 __ JumpIfSmi(r0, &runtime);
5731 Condition is_string = masm->IsObjectStringType(r0, r1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005732 __ b(NegateCondition(is_string), &runtime);
5733
Ben Murdoch589d6972011-11-30 16:04:58 +00005734 // Short-cut for the case of trivial substring.
5735 Label return_r0;
5736 // r0: original string
5737 // r2: result string length
5738 __ ldr(r4, FieldMemOperand(r0, String::kLengthOffset));
5739 __ cmp(r2, Operand(r4, ASR, 1));
5740 __ b(eq, &return_r0);
5741
5742 Label create_slice;
5743 if (FLAG_string_slices) {
5744 __ cmp(r2, Operand(SlicedString::kMinLength));
5745 __ b(ge, &create_slice);
5746 }
5747
5748 // r0: original string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005749 // r1: instance type
Ben Murdoch589d6972011-11-30 16:04:58 +00005750 // r2: result string length
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005751 // r3: from index (untagged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005752 // r6 (a.k.a. to): to (smi)
5753 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005754 Label seq_string;
5755 __ and_(r4, r1, Operand(kStringRepresentationMask));
5756 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
5757 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
Ben Murdoch589d6972011-11-30 16:04:58 +00005758 STATIC_ASSERT(kConsStringTag < kSlicedStringTag);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005759 __ cmp(r4, Operand(kConsStringTag));
Ben Murdoch589d6972011-11-30 16:04:58 +00005760 __ b(gt, &runtime); // Slices and external strings go to runtime.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005761 __ b(lt, &seq_string); // Sequential strings are handled directly.
5762
5763 // Cons string. Try to recurse (once) on the first substring.
5764 // (This adds a little more generality than necessary to handle flattened
5765 // cons strings, but not much).
Ben Murdoch589d6972011-11-30 16:04:58 +00005766 __ ldr(r0, FieldMemOperand(r0, ConsString::kFirstOffset));
5767 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005768 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5769 __ tst(r1, Operand(kStringRepresentationMask));
5770 STATIC_ASSERT(kSeqStringTag == 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00005771 __ b(ne, &runtime); // Cons, slices and external strings go to runtime.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005772
5773 // Definitly a sequential string.
5774 __ bind(&seq_string);
5775
Ben Murdoch589d6972011-11-30 16:04:58 +00005776 // r0: original string
5777 // r1: instance type
5778 // r2: result string length
5779 // r3: from index (untagged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005780 // r6 (a.k.a. to): to (smi)
5781 // r7 (a.k.a. from): from offset (smi)
Ben Murdoch589d6972011-11-30 16:04:58 +00005782 __ ldr(r4, FieldMemOperand(r0, String::kLengthOffset));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005783 __ cmp(r4, Operand(to));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005784 __ b(lt, &runtime); // Fail if to > length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005785 to = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005786
Ben Murdoch589d6972011-11-30 16:04:58 +00005787 // r0: original string or left hand side of the original cons string.
5788 // r1: instance type
5789 // r2: result string length
5790 // r3: from index (untagged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005791 // r7 (a.k.a. from): from offset (smi)
Steve Block44f0eee2011-05-26 01:26:41 +01005792 // Check for flat ASCII string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005793 Label non_ascii_flat;
5794 __ tst(r1, Operand(kStringEncodingMask));
5795 STATIC_ASSERT(kTwoByteStringTag == 0);
5796 __ b(eq, &non_ascii_flat);
5797
5798 Label result_longer_than_two;
5799 __ cmp(r2, Operand(2));
5800 __ b(gt, &result_longer_than_two);
5801
5802 // Sub string of length 2 requested.
5803 // Get the two characters forming the sub string.
Ben Murdoch589d6972011-11-30 16:04:58 +00005804 __ add(r0, r0, Operand(r3));
5805 __ ldrb(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
5806 __ ldrb(r4, FieldMemOperand(r0, SeqAsciiString::kHeaderSize + 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005807
5808 // Try to lookup two character string in symbol table.
5809 Label make_two_character_string;
5810 StringHelper::GenerateTwoCharacterSymbolTableProbe(
5811 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01005812 Counters* counters = masm->isolate()->counters();
Ben Murdoch589d6972011-11-30 16:04:58 +00005813 __ jmp(&return_r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005814
5815 // r2: result string length.
5816 // r3: two characters combined into halfword in little endian byte order.
5817 __ bind(&make_two_character_string);
5818 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
5819 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Ben Murdoch589d6972011-11-30 16:04:58 +00005820 __ jmp(&return_r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005821
5822 __ bind(&result_longer_than_two);
5823
Ben Murdoch589d6972011-11-30 16:04:58 +00005824 // Locate 'from' character of string.
5825 __ add(r5, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5826 __ add(r5, r5, Operand(from, ASR, 1));
5827
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005828 // Allocate the result.
5829 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
5830
Ben Murdoch589d6972011-11-30 16:04:58 +00005831 // r0: result string
5832 // r2: result string length
5833 // r5: first character of substring to copy
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005834 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005835 // Locate first character of result.
5836 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005837
Ben Murdoch589d6972011-11-30 16:04:58 +00005838 // r0: result string
5839 // r1: first character of result string
5840 // r2: result string length
5841 // r5: first character of substring to copy
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005842 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
5843 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
5844 COPY_ASCII | DEST_ALWAYS_ALIGNED);
Ben Murdoch589d6972011-11-30 16:04:58 +00005845 __ jmp(&return_r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005846
5847 __ bind(&non_ascii_flat);
Ben Murdoch589d6972011-11-30 16:04:58 +00005848 // r0: original string
5849 // r2: result string length
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005850 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005851 // Check for flat two byte string.
5852
Ben Murdoch589d6972011-11-30 16:04:58 +00005853 // Locate 'from' character of string.
5854 __ add(r5, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5855 // As "from" is a smi it is 2 times the value which matches the size of a two
5856 // byte character.
5857 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
5858 __ add(r5, r5, Operand(from));
5859
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005860 // Allocate the result.
5861 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
5862
Ben Murdoch589d6972011-11-30 16:04:58 +00005863 // r0: result string
5864 // r2: result string length
5865 // r5: first character of substring to copy
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005866 // Locate first character of result.
5867 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Ben Murdoch589d6972011-11-30 16:04:58 +00005868
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005869 from = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005870
5871 // r0: result string.
5872 // r1: first character of result.
5873 // r2: result length.
Ben Murdoch589d6972011-11-30 16:04:58 +00005874 // r5: first character of substring to copy.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005875 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005876 StringHelper::GenerateCopyCharactersLong(
5877 masm, r1, r5, r2, r3, r4, r6, r7, r9, DEST_ALWAYS_ALIGNED);
Ben Murdoch589d6972011-11-30 16:04:58 +00005878 __ jmp(&return_r0);
5879
5880 if (FLAG_string_slices) {
5881 __ bind(&create_slice);
5882 // r0: original string
5883 // r1: instance type
5884 // r2: length
5885 // r3: from index (untagged smi)
5886 // r6 (a.k.a. to): to (smi)
5887 // r7 (a.k.a. from): from offset (smi)
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005888 Label allocate_slice, sliced_string, seq_or_external_string;
5889 // If the string is not indirect, it can only be sequential or external.
Ben Murdoch589d6972011-11-30 16:04:58 +00005890 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
5891 STATIC_ASSERT(kIsIndirectStringMask != 0);
5892 __ tst(r1, Operand(kIsIndirectStringMask));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005893 __ b(eq, &seq_or_external_string);
Ben Murdoch589d6972011-11-30 16:04:58 +00005894
5895 __ tst(r1, Operand(kSlicedNotConsMask));
5896 __ b(ne, &sliced_string);
5897 // Cons string. Check whether it is flat, then fetch first part.
5898 __ ldr(r5, FieldMemOperand(r0, ConsString::kSecondOffset));
5899 __ LoadRoot(r9, Heap::kEmptyStringRootIndex);
5900 __ cmp(r5, r9);
5901 __ b(ne, &runtime);
5902 __ ldr(r5, FieldMemOperand(r0, ConsString::kFirstOffset));
5903 __ jmp(&allocate_slice);
5904
5905 __ bind(&sliced_string);
5906 // Sliced string. Fetch parent and correct start index by offset.
5907 __ ldr(r5, FieldMemOperand(r0, SlicedString::kOffsetOffset));
5908 __ add(r7, r7, r5);
5909 __ ldr(r5, FieldMemOperand(r0, SlicedString::kParentOffset));
5910 __ jmp(&allocate_slice);
5911
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005912 __ bind(&seq_or_external_string);
5913 // Sequential or external string. Just move string to the correct register.
Ben Murdoch589d6972011-11-30 16:04:58 +00005914 __ mov(r5, r0);
5915
5916 __ bind(&allocate_slice);
5917 // r1: instance type of original string
5918 // r2: length
5919 // r5: underlying subject string
5920 // r7 (a.k.a. from): from offset (smi)
5921 // Allocate new sliced string. At this point we do not reload the instance
5922 // type including the string encoding because we simply rely on the info
5923 // provided by the original string. It does not matter if the original
5924 // string's encoding is wrong because we always have to recheck encoding of
5925 // the newly created string's parent anyways due to externalized strings.
5926 Label two_byte_slice, set_slice_header;
5927 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
5928 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
5929 __ tst(r1, Operand(kStringEncodingMask));
5930 __ b(eq, &two_byte_slice);
5931 __ AllocateAsciiSlicedString(r0, r2, r3, r4, &runtime);
5932 __ jmp(&set_slice_header);
5933 __ bind(&two_byte_slice);
5934 __ AllocateTwoByteSlicedString(r0, r2, r3, r4, &runtime);
5935 __ bind(&set_slice_header);
5936 __ str(r7, FieldMemOperand(r0, SlicedString::kOffsetOffset));
5937 __ str(r5, FieldMemOperand(r0, SlicedString::kParentOffset));
5938 }
5939
5940 __ bind(&return_r0);
Steve Block44f0eee2011-05-26 01:26:41 +01005941 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005942 __ add(sp, sp, Operand(3 * kPointerSize));
5943 __ Ret();
5944
5945 // Just jump to runtime to create the sub string.
5946 __ bind(&runtime);
5947 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5948}
5949
5950
Ben Murdoch257744e2011-11-30 15:57:28 +00005951void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5952 Register left,
5953 Register right,
5954 Register scratch1,
5955 Register scratch2,
5956 Register scratch3) {
5957 Register length = scratch1;
5958
5959 // Compare lengths.
5960 Label strings_not_equal, check_zero_length;
5961 __ ldr(length, FieldMemOperand(left, String::kLengthOffset));
5962 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
5963 __ cmp(length, scratch2);
5964 __ b(eq, &check_zero_length);
5965 __ bind(&strings_not_equal);
5966 __ mov(r0, Operand(Smi::FromInt(NOT_EQUAL)));
5967 __ Ret();
5968
5969 // Check if the length is zero.
5970 Label compare_chars;
5971 __ bind(&check_zero_length);
5972 STATIC_ASSERT(kSmiTag == 0);
5973 __ tst(length, Operand(length));
5974 __ b(ne, &compare_chars);
5975 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
5976 __ Ret();
5977
5978 // Compare characters.
5979 __ bind(&compare_chars);
5980 GenerateAsciiCharsCompareLoop(masm,
5981 left, right, length, scratch2, scratch3,
5982 &strings_not_equal);
5983
5984 // Characters are equal.
5985 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
5986 __ Ret();
5987}
5988
5989
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005990void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5991 Register left,
5992 Register right,
5993 Register scratch1,
5994 Register scratch2,
5995 Register scratch3,
5996 Register scratch4) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005997 Label result_not_equal, compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005998 // Find minimum length and length difference.
5999 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
6000 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
6001 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
6002 Register length_delta = scratch3;
6003 __ mov(scratch1, scratch2, LeaveCC, gt);
6004 Register min_length = scratch1;
6005 STATIC_ASSERT(kSmiTag == 0);
6006 __ tst(min_length, Operand(min_length));
6007 __ b(eq, &compare_lengths);
6008
Ben Murdoch257744e2011-11-30 15:57:28 +00006009 // Compare loop.
6010 GenerateAsciiCharsCompareLoop(masm,
6011 left, right, min_length, scratch2, scratch4,
6012 &result_not_equal);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006013
Ben Murdoch257744e2011-11-30 15:57:28 +00006014 // Compare lengths - strings up to min-length are equal.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006015 __ bind(&compare_lengths);
6016 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00006017 // Use length_delta as result if it's zero.
6018 __ mov(r0, Operand(length_delta), SetCC);
6019 __ bind(&result_not_equal);
6020 // Conditionally update the result based either on length_delta or
6021 // the last comparion performed in the loop above.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006022 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
6023 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
6024 __ Ret();
6025}
6026
6027
Ben Murdoch257744e2011-11-30 15:57:28 +00006028void StringCompareStub::GenerateAsciiCharsCompareLoop(
6029 MacroAssembler* masm,
6030 Register left,
6031 Register right,
6032 Register length,
6033 Register scratch1,
6034 Register scratch2,
6035 Label* chars_not_equal) {
6036 // Change index to run from -length to -1 by adding length to string
6037 // start. This means that loop ends when index reaches zero, which
6038 // doesn't need an additional compare.
6039 __ SmiUntag(length);
6040 __ add(scratch1, length,
6041 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6042 __ add(left, left, Operand(scratch1));
6043 __ add(right, right, Operand(scratch1));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006044 __ rsb(length, length, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00006045 Register index = length; // index = -length;
6046
6047 // Compare loop.
6048 Label loop;
6049 __ bind(&loop);
6050 __ ldrb(scratch1, MemOperand(left, index));
6051 __ ldrb(scratch2, MemOperand(right, index));
6052 __ cmp(scratch1, scratch2);
6053 __ b(ne, chars_not_equal);
6054 __ add(index, index, Operand(1), SetCC);
6055 __ b(ne, &loop);
6056}
6057
6058
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006059void StringCompareStub::Generate(MacroAssembler* masm) {
6060 Label runtime;
6061
Steve Block44f0eee2011-05-26 01:26:41 +01006062 Counters* counters = masm->isolate()->counters();
6063
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006064 // Stack frame on entry.
6065 // sp[0]: right string
6066 // sp[4]: left string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006067 __ Ldrd(r0 , r1, MemOperand(sp)); // Load right in r0, left in r1.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006068
6069 Label not_same;
6070 __ cmp(r0, r1);
6071 __ b(ne, &not_same);
6072 STATIC_ASSERT(EQUAL == 0);
6073 STATIC_ASSERT(kSmiTag == 0);
6074 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01006075 __ IncrementCounter(counters->string_compare_native(), 1, r1, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006076 __ add(sp, sp, Operand(2 * kPointerSize));
6077 __ Ret();
6078
6079 __ bind(&not_same);
6080
Steve Block44f0eee2011-05-26 01:26:41 +01006081 // Check that both objects are sequential ASCII strings.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006082 __ JumpIfNotBothSequentialAsciiStrings(r1, r0, r2, r3, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006083
Steve Block44f0eee2011-05-26 01:26:41 +01006084 // Compare flat ASCII strings natively. Remove arguments from stack first.
6085 __ IncrementCounter(counters->string_compare_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006086 __ add(sp, sp, Operand(2 * kPointerSize));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01006087 GenerateCompareFlatAsciiStrings(masm, r1, r0, r2, r3, r4, r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006088
6089 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
6090 // tagged as a small integer.
6091 __ bind(&runtime);
6092 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
6093}
6094
6095
6096void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006097 Label string_add_runtime, call_builtin;
6098 Builtins::JavaScript builtin_id = Builtins::ADD;
6099
Steve Block44f0eee2011-05-26 01:26:41 +01006100 Counters* counters = masm->isolate()->counters();
6101
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006102 // Stack on entry:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006103 // sp[0]: second argument (right).
6104 // sp[4]: first argument (left).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006105
6106 // Load the two arguments.
6107 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
6108 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
6109
6110 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006111 if (flags_ == NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006112 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
6113 // Load instance types.
6114 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
6115 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
6116 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6117 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
6118 STATIC_ASSERT(kStringTag == 0);
6119 // If either is not a string, go to runtime.
6120 __ tst(r4, Operand(kIsNotStringMask));
6121 __ tst(r5, Operand(kIsNotStringMask), eq);
6122 __ b(ne, &string_add_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006123 } else {
6124 // Here at least one of the arguments is definitely a string.
6125 // We convert the one that is not known to be a string.
6126 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
6127 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
6128 GenerateConvertArgument(
6129 masm, 1 * kPointerSize, r0, r2, r3, r4, r5, &call_builtin);
6130 builtin_id = Builtins::STRING_ADD_RIGHT;
6131 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
6132 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
6133 GenerateConvertArgument(
6134 masm, 0 * kPointerSize, r1, r2, r3, r4, r5, &call_builtin);
6135 builtin_id = Builtins::STRING_ADD_LEFT;
6136 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006137 }
6138
6139 // Both arguments are strings.
6140 // r0: first string
6141 // r1: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006142 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6143 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006144 {
6145 Label strings_not_empty;
6146 // Check if either of the strings are empty. In that case return the other.
6147 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
6148 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
6149 STATIC_ASSERT(kSmiTag == 0);
6150 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
6151 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
6152 STATIC_ASSERT(kSmiTag == 0);
6153 // Else test if second string is empty.
6154 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
6155 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
6156
Steve Block44f0eee2011-05-26 01:26:41 +01006157 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006158 __ add(sp, sp, Operand(2 * kPointerSize));
6159 __ Ret();
6160
6161 __ bind(&strings_not_empty);
6162 }
6163
6164 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
6165 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
6166 // Both strings are non-empty.
6167 // r0: first string
6168 // r1: second string
6169 // r2: length of first string
6170 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006171 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6172 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006173 // Look at the length of the result of adding the two strings.
6174 Label string_add_flat_result, longer_than_two;
6175 // Adding two lengths can't overflow.
6176 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
6177 __ add(r6, r2, Operand(r3));
Steve Block44f0eee2011-05-26 01:26:41 +01006178 // Use the symbol table when adding two one character strings, as it
6179 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006180 __ cmp(r6, Operand(2));
6181 __ b(ne, &longer_than_two);
6182
Steve Block44f0eee2011-05-26 01:26:41 +01006183 // Check that both strings are non-external ASCII strings.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006184 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006185 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
6186 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
6187 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6188 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
6189 }
6190 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
6191 &string_add_runtime);
6192
6193 // Get the two characters forming the sub string.
6194 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
6195 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
6196
6197 // Try to lookup two character string in symbol table. If it is not found
6198 // just allocate a new one.
6199 Label make_two_character_string;
6200 StringHelper::GenerateTwoCharacterSymbolTableProbe(
6201 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01006202 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006203 __ add(sp, sp, Operand(2 * kPointerSize));
6204 __ Ret();
6205
6206 __ bind(&make_two_character_string);
6207 // Resulting string has length 2 and first chars of two strings
6208 // are combined into single halfword in r2 register.
6209 // So we can fill resulting string without two loops by a single
6210 // halfword store instruction (which assumes that processor is
6211 // in a little endian mode)
6212 __ mov(r6, Operand(2));
6213 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
6214 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01006215 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006216 __ add(sp, sp, Operand(2 * kPointerSize));
6217 __ Ret();
6218
6219 __ bind(&longer_than_two);
6220 // Check if resulting string will be flat.
6221 __ cmp(r6, Operand(String::kMinNonFlatLength));
6222 __ b(lt, &string_add_flat_result);
6223 // Handle exceptionally long strings in the runtime system.
6224 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
6225 ASSERT(IsPowerOf2(String::kMaxLength + 1));
6226 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
6227 __ cmp(r6, Operand(String::kMaxLength + 1));
6228 __ b(hs, &string_add_runtime);
6229
6230 // If result is not supposed to be flat, allocate a cons string object.
Steve Block44f0eee2011-05-26 01:26:41 +01006231 // If both strings are ASCII the result is an ASCII cons string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006232 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006233 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
6234 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
6235 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6236 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
6237 }
6238 Label non_ascii, allocated, ascii_data;
6239 STATIC_ASSERT(kTwoByteStringTag == 0);
6240 __ tst(r4, Operand(kStringEncodingMask));
6241 __ tst(r5, Operand(kStringEncodingMask), ne);
6242 __ b(eq, &non_ascii);
6243
6244 // Allocate an ASCII cons string.
6245 __ bind(&ascii_data);
6246 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
6247 __ bind(&allocated);
6248 // Fill the fields of the cons string.
6249 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
6250 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
6251 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01006252 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006253 __ add(sp, sp, Operand(2 * kPointerSize));
6254 __ Ret();
6255
6256 __ bind(&non_ascii);
6257 // At least one of the strings is two-byte. Check whether it happens
Steve Block44f0eee2011-05-26 01:26:41 +01006258 // to contain only ASCII characters.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006259 // r4: first instance type.
6260 // r5: second instance type.
6261 __ tst(r4, Operand(kAsciiDataHintMask));
6262 __ tst(r5, Operand(kAsciiDataHintMask), ne);
6263 __ b(ne, &ascii_data);
6264 __ eor(r4, r4, Operand(r5));
6265 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
6266 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
6267 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
6268 __ b(eq, &ascii_data);
6269
6270 // Allocate a two byte cons string.
6271 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
6272 __ jmp(&allocated);
6273
6274 // Handle creating a flat result. First check that both strings are
6275 // sequential and that they have the same encoding.
6276 // r0: first string
6277 // r1: second string
6278 // r2: length of first string
6279 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006280 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6281 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006282 // r6: sum of lengths.
6283 __ bind(&string_add_flat_result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006284 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006285 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
6286 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
6287 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
6288 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
6289 }
6290 // Check that both strings are sequential.
6291 STATIC_ASSERT(kSeqStringTag == 0);
6292 __ tst(r4, Operand(kStringRepresentationMask));
6293 __ tst(r5, Operand(kStringRepresentationMask), eq);
6294 __ b(ne, &string_add_runtime);
6295 // Now check if both strings have the same encoding (ASCII/Two-byte).
6296 // r0: first string.
6297 // r1: second string.
6298 // r2: length of first string.
6299 // r3: length of second string.
6300 // r6: sum of lengths..
6301 Label non_ascii_string_add_flat_result;
6302 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
6303 __ eor(r7, r4, Operand(r5));
6304 __ tst(r7, Operand(kStringEncodingMask));
6305 __ b(ne, &string_add_runtime);
6306 // And see if it's ASCII or two-byte.
6307 __ tst(r4, Operand(kStringEncodingMask));
6308 __ b(eq, &non_ascii_string_add_flat_result);
6309
6310 // Both strings are sequential ASCII strings. We also know that they are
6311 // short (since the sum of the lengths is less than kMinNonFlatLength).
6312 // r6: length of resulting flat string
6313 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
6314 // Locate first character of result.
6315 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6316 // Locate first character of first argument.
6317 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6318 // r0: first character of first string.
6319 // r1: second string.
6320 // r2: length of first string.
6321 // r3: length of second string.
6322 // r6: first character of result.
6323 // r7: result string.
6324 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
6325
6326 // Load second argument and locate first character.
6327 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6328 // r1: first character of second string.
6329 // r3: length of second string.
6330 // r6: next character of result.
6331 // r7: result string.
6332 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
6333 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01006334 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006335 __ add(sp, sp, Operand(2 * kPointerSize));
6336 __ Ret();
6337
6338 __ bind(&non_ascii_string_add_flat_result);
6339 // Both strings are sequential two byte strings.
6340 // r0: first string.
6341 // r1: second string.
6342 // r2: length of first string.
6343 // r3: length of second string.
6344 // r6: sum of length of strings.
6345 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
6346 // r0: first string.
6347 // r1: second string.
6348 // r2: length of first string.
6349 // r3: length of second string.
6350 // r7: result string.
6351
6352 // Locate first character of result.
6353 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6354 // Locate first character of first argument.
6355 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6356
6357 // r0: first character of first string.
6358 // r1: second string.
6359 // r2: length of first string.
6360 // r3: length of second string.
6361 // r6: first character of result.
6362 // r7: result string.
6363 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
6364
6365 // Locate first character of second argument.
6366 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6367
6368 // r1: first character of second string.
6369 // r3: length of second string.
6370 // r6: next character of result (after copy of first string).
6371 // r7: result string.
6372 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
6373
6374 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01006375 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006376 __ add(sp, sp, Operand(2 * kPointerSize));
6377 __ Ret();
6378
6379 // Just jump to runtime to add the two strings.
6380 __ bind(&string_add_runtime);
6381 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006382
6383 if (call_builtin.is_linked()) {
6384 __ bind(&call_builtin);
Ben Murdoch257744e2011-11-30 15:57:28 +00006385 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006386 }
6387}
6388
6389
6390void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
6391 int stack_offset,
6392 Register arg,
6393 Register scratch1,
6394 Register scratch2,
6395 Register scratch3,
6396 Register scratch4,
6397 Label* slow) {
6398 // First check if the argument is already a string.
6399 Label not_string, done;
6400 __ JumpIfSmi(arg, &not_string);
6401 __ CompareObjectType(arg, scratch1, scratch1, FIRST_NONSTRING_TYPE);
6402 __ b(lt, &done);
6403
6404 // Check the number to string cache.
6405 Label not_cached;
6406 __ bind(&not_string);
6407 // Puts the cached result into scratch1.
6408 NumberToStringStub::GenerateLookupNumberStringCache(masm,
6409 arg,
6410 scratch1,
6411 scratch2,
6412 scratch3,
6413 scratch4,
6414 false,
6415 &not_cached);
6416 __ mov(arg, scratch1);
6417 __ str(arg, MemOperand(sp, stack_offset));
6418 __ jmp(&done);
6419
6420 // Check if the argument is a safe string wrapper.
6421 __ bind(&not_cached);
6422 __ JumpIfSmi(arg, slow);
6423 __ CompareObjectType(
6424 arg, scratch1, scratch2, JS_VALUE_TYPE); // map -> scratch1.
6425 __ b(ne, slow);
6426 __ ldrb(scratch2, FieldMemOperand(scratch1, Map::kBitField2Offset));
6427 __ and_(scratch2,
6428 scratch2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
6429 __ cmp(scratch2,
6430 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
6431 __ b(ne, slow);
6432 __ ldr(arg, FieldMemOperand(arg, JSValue::kValueOffset));
6433 __ str(arg, MemOperand(sp, stack_offset));
6434
6435 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006436}
6437
6438
Ben Murdochb0fe1622011-05-05 13:52:32 +01006439void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
6440 ASSERT(state_ == CompareIC::SMIS);
6441 Label miss;
6442 __ orr(r2, r1, r0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006443 __ JumpIfNotSmi(r2, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006444
6445 if (GetCondition() == eq) {
6446 // For equality we do not care about the sign of the result.
6447 __ sub(r0, r0, r1, SetCC);
6448 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01006449 // Untag before subtracting to avoid handling overflow.
6450 __ SmiUntag(r1);
6451 __ sub(r0, r1, SmiUntagOperand(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01006452 }
6453 __ Ret();
6454
6455 __ bind(&miss);
6456 GenerateMiss(masm);
6457}
6458
6459
6460void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
6461 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
6462
6463 Label generic_stub;
6464 Label unordered;
6465 Label miss;
6466 __ and_(r2, r1, Operand(r0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006467 __ JumpIfSmi(r2, &generic_stub);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006468
6469 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
6470 __ b(ne, &miss);
6471 __ CompareObjectType(r1, r2, r2, HEAP_NUMBER_TYPE);
6472 __ b(ne, &miss);
6473
6474 // Inlining the double comparison and falling back to the general compare
6475 // stub if NaN is involved or VFP3 is unsupported.
Ben Murdoch8b112d22011-06-08 16:22:53 +01006476 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006477 CpuFeatures::Scope scope(VFP3);
6478
6479 // Load left and right operand
6480 __ sub(r2, r1, Operand(kHeapObjectTag));
6481 __ vldr(d0, r2, HeapNumber::kValueOffset);
6482 __ sub(r2, r0, Operand(kHeapObjectTag));
6483 __ vldr(d1, r2, HeapNumber::kValueOffset);
6484
6485 // Compare operands
Ben Murdochb8e0da22011-05-16 14:20:40 +01006486 __ VFPCompareAndSetFlags(d0, d1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006487
6488 // Don't base result on status bits when a NaN is involved.
6489 __ b(vs, &unordered);
6490
6491 // Return a result of -1, 0, or 1, based on status bits.
6492 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
6493 __ mov(r0, Operand(LESS), LeaveCC, lt);
6494 __ mov(r0, Operand(GREATER), LeaveCC, gt);
6495 __ Ret();
6496
6497 __ bind(&unordered);
6498 }
6499
6500 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS, r1, r0);
6501 __ bind(&generic_stub);
6502 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
6503
6504 __ bind(&miss);
6505 GenerateMiss(masm);
6506}
6507
6508
Ben Murdoch257744e2011-11-30 15:57:28 +00006509void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
6510 ASSERT(state_ == CompareIC::SYMBOLS);
6511 Label miss;
6512
6513 // Registers containing left and right operands respectively.
6514 Register left = r1;
6515 Register right = r0;
6516 Register tmp1 = r2;
6517 Register tmp2 = r3;
6518
6519 // Check that both operands are heap objects.
6520 __ JumpIfEitherSmi(left, right, &miss);
6521
6522 // Check that both operands are symbols.
6523 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
6524 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
6525 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
6526 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
6527 STATIC_ASSERT(kSymbolTag != 0);
6528 __ and_(tmp1, tmp1, Operand(tmp2));
6529 __ tst(tmp1, Operand(kIsSymbolMask));
6530 __ b(eq, &miss);
6531
6532 // Symbols are compared by identity.
6533 __ cmp(left, right);
6534 // Make sure r0 is non-zero. At this point input operands are
6535 // guaranteed to be non-zero.
6536 ASSERT(right.is(r0));
6537 STATIC_ASSERT(EQUAL == 0);
6538 STATIC_ASSERT(kSmiTag == 0);
6539 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
6540 __ Ret();
6541
6542 __ bind(&miss);
6543 GenerateMiss(masm);
6544}
6545
6546
6547void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
6548 ASSERT(state_ == CompareIC::STRINGS);
6549 Label miss;
6550
6551 // Registers containing left and right operands respectively.
6552 Register left = r1;
6553 Register right = r0;
6554 Register tmp1 = r2;
6555 Register tmp2 = r3;
6556 Register tmp3 = r4;
6557 Register tmp4 = r5;
6558
6559 // Check that both operands are heap objects.
6560 __ JumpIfEitherSmi(left, right, &miss);
6561
6562 // Check that both operands are strings. This leaves the instance
6563 // types loaded in tmp1 and tmp2.
6564 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
6565 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
6566 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
6567 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
6568 STATIC_ASSERT(kNotStringTag != 0);
6569 __ orr(tmp3, tmp1, tmp2);
6570 __ tst(tmp3, Operand(kIsNotStringMask));
6571 __ b(ne, &miss);
6572
6573 // Fast check for identical strings.
6574 __ cmp(left, right);
6575 STATIC_ASSERT(EQUAL == 0);
6576 STATIC_ASSERT(kSmiTag == 0);
6577 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
6578 __ Ret(eq);
6579
6580 // Handle not identical strings.
6581
6582 // Check that both strings are symbols. If they are, we're done
6583 // because we already know they are not identical.
6584 ASSERT(GetCondition() == eq);
6585 STATIC_ASSERT(kSymbolTag != 0);
6586 __ and_(tmp3, tmp1, Operand(tmp2));
6587 __ tst(tmp3, Operand(kIsSymbolMask));
6588 // Make sure r0 is non-zero. At this point input operands are
6589 // guaranteed to be non-zero.
6590 ASSERT(right.is(r0));
6591 __ Ret(ne);
6592
6593 // Check that both strings are sequential ASCII.
6594 Label runtime;
6595 __ JumpIfBothInstanceTypesAreNotSequentialAscii(tmp1, tmp2, tmp3, tmp4,
6596 &runtime);
6597
6598 // Compare flat ASCII strings. Returns when done.
6599 StringCompareStub::GenerateFlatAsciiStringEquals(
6600 masm, left, right, tmp1, tmp2, tmp3);
6601
6602 // Handle more complex cases in runtime.
6603 __ bind(&runtime);
6604 __ Push(left, right);
6605 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
6606
6607 __ bind(&miss);
6608 GenerateMiss(masm);
6609}
6610
6611
Ben Murdochb0fe1622011-05-05 13:52:32 +01006612void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6613 ASSERT(state_ == CompareIC::OBJECTS);
6614 Label miss;
6615 __ and_(r2, r1, Operand(r0));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006616 __ JumpIfSmi(r2, &miss);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006617
6618 __ CompareObjectType(r0, r2, r2, JS_OBJECT_TYPE);
6619 __ b(ne, &miss);
6620 __ CompareObjectType(r1, r2, r2, JS_OBJECT_TYPE);
6621 __ b(ne, &miss);
6622
6623 ASSERT(GetCondition() == eq);
6624 __ sub(r0, r0, Operand(r1));
6625 __ Ret();
6626
6627 __ bind(&miss);
6628 GenerateMiss(masm);
6629}
6630
6631
6632void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6633 __ Push(r1, r0);
6634 __ push(lr);
6635
6636 // Call the runtime system in a fresh internal frame.
Steve Block44f0eee2011-05-26 01:26:41 +01006637 ExternalReference miss =
6638 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006639 {
6640 FrameScope scope(masm, StackFrame::INTERNAL);
6641 __ Push(r1, r0);
6642 __ mov(ip, Operand(Smi::FromInt(op_)));
6643 __ push(ip);
6644 __ CallExternalReference(miss, 3);
6645 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01006646 // Compute the entry point of the rewritten stub.
6647 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
6648 // Restore registers.
6649 __ pop(lr);
6650 __ pop(r0);
6651 __ pop(r1);
6652 __ Jump(r2);
6653}
6654
6655
Steve Block1e0659c2011-05-24 12:43:12 +01006656void DirectCEntryStub::Generate(MacroAssembler* masm) {
6657 __ ldr(pc, MemOperand(sp, 0));
6658}
6659
6660
6661void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006662 ExternalReference function) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006663 __ mov(r2, Operand(function));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006664 GenerateCall(masm, r2);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006665}
6666
6667
6668void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
6669 Register target) {
Steve Block1e0659c2011-05-24 12:43:12 +01006670 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
6671 RelocInfo::CODE_TARGET));
6672 // Push return address (accessible to GC through exit frame pc).
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006673 // Note that using pc with str is deprecated.
6674 Label start;
6675 __ bind(&start);
6676 __ add(ip, pc, Operand(Assembler::kInstrSize));
6677 __ str(ip, MemOperand(sp, 0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006678 __ Jump(target); // Call the C++ function.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006679 ASSERT_EQ(Assembler::kInstrSize + Assembler::kPcLoadDelta,
6680 masm->SizeOfCodeGeneratedSince(&start));
Steve Block1e0659c2011-05-24 12:43:12 +01006681}
6682
6683
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006684void StringDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
6685 Label* miss,
6686 Label* done,
6687 Register receiver,
6688 Register properties,
6689 Handle<String> name,
6690 Register scratch0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00006691 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6692 // not equal to the name and kProbes-th slot is not used (its name is the
6693 // undefined value), it guarantees the hash table doesn't contain the
6694 // property. It's true even if some slots represent deleted properties
6695 // (their names are the null value).
6696 for (int i = 0; i < kInlinedProbes; i++) {
6697 // scratch0 points to properties hash.
6698 // Compute the masked index: (hash + i + i * i) & mask.
6699 Register index = scratch0;
6700 // Capacity is smi 2^n.
6701 __ ldr(index, FieldMemOperand(properties, kCapacityOffset));
6702 __ sub(index, index, Operand(1));
6703 __ and_(index, index, Operand(
6704 Smi::FromInt(name->Hash() + StringDictionary::GetProbeOffset(i))));
6705
6706 // Scale the index by multiplying by the entry size.
6707 ASSERT(StringDictionary::kEntrySize == 3);
6708 __ add(index, index, Operand(index, LSL, 1)); // index *= 3.
6709
6710 Register entity_name = scratch0;
6711 // Having undefined at this place means the name is not contained.
6712 ASSERT_EQ(kSmiTagSize, 1);
6713 Register tmp = properties;
6714 __ add(tmp, properties, Operand(index, LSL, 1));
6715 __ ldr(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
6716
6717 ASSERT(!tmp.is(entity_name));
6718 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
6719 __ cmp(entity_name, tmp);
6720 __ b(eq, done);
6721
6722 if (i != kInlinedProbes - 1) {
6723 // Stop if found the property.
6724 __ cmp(entity_name, Operand(Handle<String>(name)));
6725 __ b(eq, miss);
6726
6727 // Check if the entry name is not a symbol.
6728 __ ldr(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
6729 __ ldrb(entity_name,
6730 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
6731 __ tst(entity_name, Operand(kIsSymbolMask));
6732 __ b(eq, miss);
6733
6734 // Restore the properties.
6735 __ ldr(properties,
6736 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
6737 }
6738 }
6739
6740 const int spill_mask =
6741 (lr.bit() | r6.bit() | r5.bit() | r4.bit() | r3.bit() |
6742 r2.bit() | r1.bit() | r0.bit());
6743
6744 __ stm(db_w, sp, spill_mask);
6745 __ ldr(r0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
6746 __ mov(r1, Operand(Handle<String>(name)));
6747 StringDictionaryLookupStub stub(NEGATIVE_LOOKUP);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006748 __ CallStub(&stub);
Ben Murdoch257744e2011-11-30 15:57:28 +00006749 __ tst(r0, Operand(r0));
6750 __ ldm(ia_w, sp, spill_mask);
6751
6752 __ b(eq, done);
6753 __ b(ne, miss);
Ben Murdoch257744e2011-11-30 15:57:28 +00006754}
6755
6756
6757// Probe the string dictionary in the |elements| register. Jump to the
6758// |done| label if a property with the given name is found. Jump to
6759// the |miss| label otherwise.
6760// If lookup was successful |scratch2| will be equal to elements + 4 * index.
6761void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6762 Label* miss,
6763 Label* done,
6764 Register elements,
6765 Register name,
6766 Register scratch1,
6767 Register scratch2) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006768 ASSERT(!elements.is(scratch1));
6769 ASSERT(!elements.is(scratch2));
6770 ASSERT(!name.is(scratch1));
6771 ASSERT(!name.is(scratch2));
6772
Ben Murdoch257744e2011-11-30 15:57:28 +00006773 // Assert that name contains a string.
6774 if (FLAG_debug_code) __ AbortIfNotString(name);
6775
6776 // Compute the capacity mask.
6777 __ ldr(scratch1, FieldMemOperand(elements, kCapacityOffset));
6778 __ mov(scratch1, Operand(scratch1, ASR, kSmiTagSize)); // convert smi to int
6779 __ sub(scratch1, scratch1, Operand(1));
6780
6781 // Generate an unrolled loop that performs a few probes before
6782 // giving up. Measurements done on Gmail indicate that 2 probes
6783 // cover ~93% of loads from dictionaries.
6784 for (int i = 0; i < kInlinedProbes; i++) {
6785 // Compute the masked index: (hash + i + i * i) & mask.
6786 __ ldr(scratch2, FieldMemOperand(name, String::kHashFieldOffset));
6787 if (i > 0) {
6788 // Add the probe offset (i + i * i) left shifted to avoid right shifting
6789 // the hash in a separate instruction. The value hash + i + i * i is right
6790 // shifted in the following and instruction.
6791 ASSERT(StringDictionary::GetProbeOffset(i) <
6792 1 << (32 - String::kHashFieldOffset));
6793 __ add(scratch2, scratch2, Operand(
6794 StringDictionary::GetProbeOffset(i) << String::kHashShift));
6795 }
6796 __ and_(scratch2, scratch1, Operand(scratch2, LSR, String::kHashShift));
6797
6798 // Scale the index by multiplying by the element size.
6799 ASSERT(StringDictionary::kEntrySize == 3);
6800 // scratch2 = scratch2 * 3.
6801 __ add(scratch2, scratch2, Operand(scratch2, LSL, 1));
6802
6803 // Check if the key is identical to the name.
6804 __ add(scratch2, elements, Operand(scratch2, LSL, 2));
6805 __ ldr(ip, FieldMemOperand(scratch2, kElementsStartOffset));
6806 __ cmp(name, Operand(ip));
6807 __ b(eq, done);
6808 }
6809
6810 const int spill_mask =
6811 (lr.bit() | r6.bit() | r5.bit() | r4.bit() |
6812 r3.bit() | r2.bit() | r1.bit() | r0.bit()) &
6813 ~(scratch1.bit() | scratch2.bit());
6814
6815 __ stm(db_w, sp, spill_mask);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006816 if (name.is(r0)) {
6817 ASSERT(!elements.is(r1));
6818 __ Move(r1, name);
6819 __ Move(r0, elements);
6820 } else {
6821 __ Move(r0, elements);
6822 __ Move(r1, name);
6823 }
Ben Murdoch257744e2011-11-30 15:57:28 +00006824 StringDictionaryLookupStub stub(POSITIVE_LOOKUP);
6825 __ CallStub(&stub);
6826 __ tst(r0, Operand(r0));
6827 __ mov(scratch2, Operand(r2));
6828 __ ldm(ia_w, sp, spill_mask);
6829
6830 __ b(ne, done);
6831 __ b(eq, miss);
6832}
6833
6834
6835void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006836 // This stub overrides SometimesSetsUpAFrame() to return false. That means
6837 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00006838 // Registers:
6839 // result: StringDictionary to probe
6840 // r1: key
6841 // : StringDictionary to probe.
6842 // index_: will hold an index of entry if lookup is successful.
6843 // might alias with result_.
6844 // Returns:
6845 // result_ is zero if lookup failed, non zero otherwise.
6846
6847 Register result = r0;
6848 Register dictionary = r0;
6849 Register key = r1;
6850 Register index = r2;
6851 Register mask = r3;
6852 Register hash = r4;
6853 Register undefined = r5;
6854 Register entry_key = r6;
6855
6856 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6857
6858 __ ldr(mask, FieldMemOperand(dictionary, kCapacityOffset));
6859 __ mov(mask, Operand(mask, ASR, kSmiTagSize));
6860 __ sub(mask, mask, Operand(1));
6861
6862 __ ldr(hash, FieldMemOperand(key, String::kHashFieldOffset));
6863
6864 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
6865
6866 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6867 // Compute the masked index: (hash + i + i * i) & mask.
6868 // Capacity is smi 2^n.
6869 if (i > 0) {
6870 // Add the probe offset (i + i * i) left shifted to avoid right shifting
6871 // the hash in a separate instruction. The value hash + i + i * i is right
6872 // shifted in the following and instruction.
6873 ASSERT(StringDictionary::GetProbeOffset(i) <
6874 1 << (32 - String::kHashFieldOffset));
6875 __ add(index, hash, Operand(
6876 StringDictionary::GetProbeOffset(i) << String::kHashShift));
6877 } else {
6878 __ mov(index, Operand(hash));
6879 }
6880 __ and_(index, mask, Operand(index, LSR, String::kHashShift));
6881
6882 // Scale the index by multiplying by the entry size.
6883 ASSERT(StringDictionary::kEntrySize == 3);
6884 __ add(index, index, Operand(index, LSL, 1)); // index *= 3.
6885
6886 ASSERT_EQ(kSmiTagSize, 1);
6887 __ add(index, dictionary, Operand(index, LSL, 2));
6888 __ ldr(entry_key, FieldMemOperand(index, kElementsStartOffset));
6889
6890 // Having undefined at this place means the name is not contained.
6891 __ cmp(entry_key, Operand(undefined));
6892 __ b(eq, &not_in_dictionary);
6893
6894 // Stop if found the property.
6895 __ cmp(entry_key, Operand(key));
6896 __ b(eq, &in_dictionary);
6897
6898 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6899 // Check if the entry name is not a symbol.
6900 __ ldr(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
6901 __ ldrb(entry_key,
6902 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
6903 __ tst(entry_key, Operand(kIsSymbolMask));
6904 __ b(eq, &maybe_in_dictionary);
6905 }
6906 }
6907
6908 __ bind(&maybe_in_dictionary);
6909 // If we are doing negative lookup then probing failure should be
6910 // treated as a lookup success. For positive lookup probing failure
6911 // should be treated as lookup failure.
6912 if (mode_ == POSITIVE_LOOKUP) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006913 __ mov(result, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00006914 __ Ret();
6915 }
6916
6917 __ bind(&in_dictionary);
6918 __ mov(result, Operand(1));
6919 __ Ret();
6920
6921 __ bind(&not_in_dictionary);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006922 __ mov(result, Operand::Zero());
Ben Murdoch257744e2011-11-30 15:57:28 +00006923 __ Ret();
6924}
6925
6926
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006927struct AheadOfTimeWriteBarrierStubList {
6928 Register object, value, address;
6929 RememberedSetAction action;
6930};
6931
6932
6933struct AheadOfTimeWriteBarrierStubList kAheadOfTime[] = {
6934 // Used in RegExpExecStub.
6935 { r6, r4, r7, EMIT_REMEMBERED_SET },
6936 { r6, r2, r7, EMIT_REMEMBERED_SET },
6937 // Used in CompileArrayPushCall.
6938 // Also used in StoreIC::GenerateNormal via GenerateDictionaryStore.
6939 // Also used in KeyedStoreIC::GenerateGeneric.
6940 { r3, r4, r5, EMIT_REMEMBERED_SET },
6941 // Used in CompileStoreGlobal.
6942 { r4, r1, r2, OMIT_REMEMBERED_SET },
6943 // Used in StoreStubCompiler::CompileStoreField via GenerateStoreField.
6944 { r1, r2, r3, EMIT_REMEMBERED_SET },
6945 { r3, r2, r1, EMIT_REMEMBERED_SET },
6946 // Used in KeyedStoreStubCompiler::CompileStoreField via GenerateStoreField.
6947 { r2, r1, r3, EMIT_REMEMBERED_SET },
6948 { r3, r1, r2, EMIT_REMEMBERED_SET },
6949 // KeyedStoreStubCompiler::GenerateStoreFastElement.
6950 { r4, r2, r3, EMIT_REMEMBERED_SET },
6951 // ElementsTransitionGenerator::GenerateSmiOnlyToObject
6952 // and ElementsTransitionGenerator::GenerateSmiOnlyToDouble
6953 // and ElementsTransitionGenerator::GenerateDoubleToObject
6954 { r2, r3, r9, EMIT_REMEMBERED_SET },
6955 // ElementsTransitionGenerator::GenerateDoubleToObject
6956 { r6, r2, r0, EMIT_REMEMBERED_SET },
6957 { r2, r6, r9, EMIT_REMEMBERED_SET },
6958 // StoreArrayLiteralElementStub::Generate
6959 { r5, r0, r6, EMIT_REMEMBERED_SET },
6960 // Null termination.
6961 { no_reg, no_reg, no_reg, EMIT_REMEMBERED_SET}
6962};
6963
6964
6965bool RecordWriteStub::IsPregenerated() {
6966 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
6967 !entry->object.is(no_reg);
6968 entry++) {
6969 if (object_.is(entry->object) &&
6970 value_.is(entry->value) &&
6971 address_.is(entry->address) &&
6972 remembered_set_action_ == entry->action &&
6973 save_fp_regs_mode_ == kDontSaveFPRegs) {
6974 return true;
6975 }
6976 }
6977 return false;
6978}
6979
6980
6981bool StoreBufferOverflowStub::IsPregenerated() {
6982 return save_doubles_ == kDontSaveFPRegs || ISOLATE->fp_stubs_generated();
6983}
6984
6985
6986void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime() {
6987 StoreBufferOverflowStub stub1(kDontSaveFPRegs);
6988 stub1.GetCode()->set_is_pregenerated(true);
6989}
6990
6991
6992void RecordWriteStub::GenerateFixedRegStubsAheadOfTime() {
6993 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
6994 !entry->object.is(no_reg);
6995 entry++) {
6996 RecordWriteStub stub(entry->object,
6997 entry->value,
6998 entry->address,
6999 entry->action,
7000 kDontSaveFPRegs);
7001 stub.GetCode()->set_is_pregenerated(true);
7002 }
7003}
7004
7005
7006// Takes the input in 3 registers: address_ value_ and object_. A pointer to
7007// the value has just been written into the object, now this stub makes sure
7008// we keep the GC informed. The word in the object where the value has been
7009// written is in the address register.
7010void RecordWriteStub::Generate(MacroAssembler* masm) {
7011 Label skip_to_incremental_noncompacting;
7012 Label skip_to_incremental_compacting;
7013
7014 // The first two instructions are generated with labels so as to get the
7015 // offset fixed up correctly by the bind(Label*) call. We patch it back and
7016 // forth between a compare instructions (a nop in this position) and the
7017 // real branch when we start and stop incremental heap marking.
7018 // See RecordWriteStub::Patch for details.
7019 __ b(&skip_to_incremental_noncompacting);
7020 __ b(&skip_to_incremental_compacting);
7021
7022 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
7023 __ RememberedSetHelper(object_,
7024 address_,
7025 value_,
7026 save_fp_regs_mode_,
7027 MacroAssembler::kReturnAtEnd);
7028 }
7029 __ Ret();
7030
7031 __ bind(&skip_to_incremental_noncompacting);
7032 GenerateIncremental(masm, INCREMENTAL);
7033
7034 __ bind(&skip_to_incremental_compacting);
7035 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
7036
7037 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
7038 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
7039 ASSERT(Assembler::GetBranchOffset(masm->instr_at(0)) < (1 << 12));
7040 ASSERT(Assembler::GetBranchOffset(masm->instr_at(4)) < (1 << 12));
7041 PatchBranchIntoNop(masm, 0);
7042 PatchBranchIntoNop(masm, Assembler::kInstrSize);
7043}
7044
7045
7046void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
7047 regs_.Save(masm);
7048
7049 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
7050 Label dont_need_remembered_set;
7051
7052 __ ldr(regs_.scratch0(), MemOperand(regs_.address(), 0));
7053 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
7054 regs_.scratch0(),
7055 &dont_need_remembered_set);
7056
7057 __ CheckPageFlag(regs_.object(),
7058 regs_.scratch0(),
7059 1 << MemoryChunk::SCAN_ON_SCAVENGE,
7060 ne,
7061 &dont_need_remembered_set);
7062
7063 // First notify the incremental marker if necessary, then update the
7064 // remembered set.
7065 CheckNeedsToInformIncrementalMarker(
7066 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
7067 InformIncrementalMarker(masm, mode);
7068 regs_.Restore(masm);
7069 __ RememberedSetHelper(object_,
7070 address_,
7071 value_,
7072 save_fp_regs_mode_,
7073 MacroAssembler::kReturnAtEnd);
7074
7075 __ bind(&dont_need_remembered_set);
7076 }
7077
7078 CheckNeedsToInformIncrementalMarker(
7079 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
7080 InformIncrementalMarker(masm, mode);
7081 regs_.Restore(masm);
7082 __ Ret();
7083}
7084
7085
7086void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm, Mode mode) {
7087 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode_);
7088 int argument_count = 3;
7089 __ PrepareCallCFunction(argument_count, regs_.scratch0());
7090 Register address =
7091 r0.is(regs_.address()) ? regs_.scratch0() : regs_.address();
7092 ASSERT(!address.is(regs_.object()));
7093 ASSERT(!address.is(r0));
7094 __ Move(address, regs_.address());
7095 __ Move(r0, regs_.object());
7096 if (mode == INCREMENTAL_COMPACTION) {
7097 __ Move(r1, address);
7098 } else {
7099 ASSERT(mode == INCREMENTAL);
7100 __ ldr(r1, MemOperand(address, 0));
7101 }
7102 __ mov(r2, Operand(ExternalReference::isolate_address()));
7103
7104 AllowExternalCallThatCantCauseGC scope(masm);
7105 if (mode == INCREMENTAL_COMPACTION) {
7106 __ CallCFunction(
7107 ExternalReference::incremental_evacuation_record_write_function(
7108 masm->isolate()),
7109 argument_count);
7110 } else {
7111 ASSERT(mode == INCREMENTAL);
7112 __ CallCFunction(
7113 ExternalReference::incremental_marking_record_write_function(
7114 masm->isolate()),
7115 argument_count);
7116 }
7117 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode_);
7118}
7119
7120
7121void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
7122 MacroAssembler* masm,
7123 OnNoNeedToInformIncrementalMarker on_no_need,
7124 Mode mode) {
7125 Label on_black;
7126 Label need_incremental;
7127 Label need_incremental_pop_scratch;
7128
7129 // Let's look at the color of the object: If it is not black we don't have
7130 // to inform the incremental marker.
7131 __ JumpIfBlack(regs_.object(), regs_.scratch0(), regs_.scratch1(), &on_black);
7132
7133 regs_.Restore(masm);
7134 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
7135 __ RememberedSetHelper(object_,
7136 address_,
7137 value_,
7138 save_fp_regs_mode_,
7139 MacroAssembler::kReturnAtEnd);
7140 } else {
7141 __ Ret();
7142 }
7143
7144 __ bind(&on_black);
7145
7146 // Get the value from the slot.
7147 __ ldr(regs_.scratch0(), MemOperand(regs_.address(), 0));
7148
7149 if (mode == INCREMENTAL_COMPACTION) {
7150 Label ensure_not_white;
7151
7152 __ CheckPageFlag(regs_.scratch0(), // Contains value.
7153 regs_.scratch1(), // Scratch.
7154 MemoryChunk::kEvacuationCandidateMask,
7155 eq,
7156 &ensure_not_white);
7157
7158 __ CheckPageFlag(regs_.object(),
7159 regs_.scratch1(), // Scratch.
7160 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
7161 eq,
7162 &need_incremental);
7163
7164 __ bind(&ensure_not_white);
7165 }
7166
7167 // We need extra registers for this, so we push the object and the address
7168 // register temporarily.
7169 __ Push(regs_.object(), regs_.address());
7170 __ EnsureNotWhite(regs_.scratch0(), // The value.
7171 regs_.scratch1(), // Scratch.
7172 regs_.object(), // Scratch.
7173 regs_.address(), // Scratch.
7174 &need_incremental_pop_scratch);
7175 __ Pop(regs_.object(), regs_.address());
7176
7177 regs_.Restore(masm);
7178 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
7179 __ RememberedSetHelper(object_,
7180 address_,
7181 value_,
7182 save_fp_regs_mode_,
7183 MacroAssembler::kReturnAtEnd);
7184 } else {
7185 __ Ret();
7186 }
7187
7188 __ bind(&need_incremental_pop_scratch);
7189 __ Pop(regs_.object(), regs_.address());
7190
7191 __ bind(&need_incremental);
7192
7193 // Fall through when we need to inform the incremental marker.
7194}
7195
7196
7197void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
7198 // ----------- S t a t e -------------
7199 // -- r0 : element value to store
7200 // -- r1 : array literal
7201 // -- r2 : map of array literal
7202 // -- r3 : element index as smi
7203 // -- r4 : array literal index in function as smi
7204 // -----------------------------------
7205
7206 Label element_done;
7207 Label double_elements;
7208 Label smi_element;
7209 Label slow_elements;
7210 Label fast_elements;
7211
7212 __ CheckFastElements(r2, r5, &double_elements);
7213 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
7214 __ JumpIfSmi(r0, &smi_element);
7215 __ CheckFastSmiOnlyElements(r2, r5, &fast_elements);
7216
7217 // Store into the array literal requires a elements transition. Call into
7218 // the runtime.
7219 __ bind(&slow_elements);
7220 // call.
7221 __ Push(r1, r3, r0);
7222 __ ldr(r5, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
7223 __ ldr(r5, FieldMemOperand(r5, JSFunction::kLiteralsOffset));
7224 __ Push(r5, r4);
7225 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
7226
7227 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
7228 __ bind(&fast_elements);
7229 __ ldr(r5, FieldMemOperand(r1, JSObject::kElementsOffset));
7230 __ add(r6, r5, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
7231 __ add(r6, r6, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
7232 __ str(r0, MemOperand(r6, 0));
7233 // Update the write barrier for the array store.
7234 __ RecordWrite(r5, r6, r0, kLRHasNotBeenSaved, kDontSaveFPRegs,
7235 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
7236 __ Ret();
7237
7238 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
7239 // FAST_ELEMENTS, and value is Smi.
7240 __ bind(&smi_element);
7241 __ ldr(r5, FieldMemOperand(r1, JSObject::kElementsOffset));
7242 __ add(r6, r5, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
7243 __ str(r0, FieldMemOperand(r6, FixedArray::kHeaderSize));
7244 __ Ret();
7245
7246 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
7247 __ bind(&double_elements);
7248 __ ldr(r5, FieldMemOperand(r1, JSObject::kElementsOffset));
7249 __ StoreNumberToDoubleElements(r0, r3, r1, r5, r6, r7, r9, r10,
7250 &slow_elements);
7251 __ Ret();
7252}
7253
Kristian Monsen80d68ea2010-09-08 11:05:35 +01007254#undef __
7255
7256} } // namespace v8::internal
7257
7258#endif // V8_TARGET_ARCH_ARM