blob: 5e6c0c384a3b344b996f64c31bf17fb1af5edb6d [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;
72 __ tst(r0, Operand(kSmiTagMask));
73 __ b(ne, &check_heap_number);
74 __ Ret();
75
76 __ bind(&check_heap_number);
Ben Murdoch257744e2011-11-30 15:57:28 +000077 EmitCheckForHeapNumber(masm, r0, r1, ip, &call_builtin);
Steve Block1e0659c2011-05-24 12:43:12 +010078 __ Ret();
79
80 __ bind(&call_builtin);
81 __ push(r0);
Ben Murdoch257744e2011-11-30 15:57:28 +000082 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +010083}
84
85
Kristian Monsen80d68ea2010-09-08 11:05:35 +010086void FastNewClosureStub::Generate(MacroAssembler* masm) {
87 // Create a new closure from the given function info in new
88 // space. Set the context to the current context in cp.
89 Label gc;
90
91 // Pop the function info from the stack.
92 __ pop(r3);
93
94 // Attempt to allocate new JSFunction in new space.
95 __ AllocateInNewSpace(JSFunction::kSize,
96 r0,
97 r1,
98 r2,
99 &gc,
100 TAG_OBJECT);
101
Steve Block44f0eee2011-05-26 01:26:41 +0100102 int map_index = strict_mode_ == kStrictMode
103 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
104 : Context::FUNCTION_MAP_INDEX;
105
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100106 // Compute the function map in the current global context and set that
107 // as the map of the allocated object.
108 __ ldr(r2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
109 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +0100110 __ ldr(r2, MemOperand(r2, Context::SlotOffset(map_index)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100111 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
112
113 // Initialize the rest of the function. We don't have to update the
114 // write barrier because the allocated object is in new space.
115 __ LoadRoot(r1, Heap::kEmptyFixedArrayRootIndex);
116 __ LoadRoot(r2, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100117 __ LoadRoot(r4, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100118 __ str(r1, FieldMemOperand(r0, JSObject::kPropertiesOffset));
119 __ str(r1, FieldMemOperand(r0, JSObject::kElementsOffset));
120 __ str(r2, FieldMemOperand(r0, JSFunction::kPrototypeOrInitialMapOffset));
121 __ str(r3, FieldMemOperand(r0, JSFunction::kSharedFunctionInfoOffset));
122 __ str(cp, FieldMemOperand(r0, JSFunction::kContextOffset));
123 __ str(r1, FieldMemOperand(r0, JSFunction::kLiteralsOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +0100124 __ str(r4, FieldMemOperand(r0, JSFunction::kNextFunctionLinkOffset));
125
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100126
127 // Initialize the code pointer in the function to be the one
128 // found in the shared function info object.
129 __ ldr(r3, FieldMemOperand(r3, SharedFunctionInfo::kCodeOffset));
130 __ add(r3, r3, Operand(Code::kHeaderSize - kHeapObjectTag));
131 __ str(r3, FieldMemOperand(r0, JSFunction::kCodeEntryOffset));
132
133 // Return result. The argument function info has been popped already.
134 __ Ret();
135
136 // Create a new closure through the slower runtime call.
137 __ bind(&gc);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800138 __ LoadRoot(r4, Heap::kFalseValueRootIndex);
139 __ Push(cp, r3, r4);
140 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100141}
142
143
144void FastNewContextStub::Generate(MacroAssembler* masm) {
145 // Try to allocate the context in new space.
146 Label gc;
147 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
148
149 // Attempt to allocate the context in new space.
150 __ AllocateInNewSpace(FixedArray::SizeFor(length),
151 r0,
152 r1,
153 r2,
154 &gc,
155 TAG_OBJECT);
156
157 // Load the function from the stack.
158 __ ldr(r3, MemOperand(sp, 0));
159
160 // Setup the object header.
161 __ LoadRoot(r2, Heap::kContextMapRootIndex);
162 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
163 __ mov(r2, Operand(Smi::FromInt(length)));
164 __ str(r2, FieldMemOperand(r0, FixedArray::kLengthOffset));
165
166 // Setup the fixed slots.
167 __ mov(r1, Operand(Smi::FromInt(0)));
168 __ str(r3, MemOperand(r0, Context::SlotOffset(Context::CLOSURE_INDEX)));
169 __ str(r0, MemOperand(r0, Context::SlotOffset(Context::FCONTEXT_INDEX)));
170 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
171 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::EXTENSION_INDEX)));
172
173 // Copy the global object from the surrounding context.
174 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
175 __ str(r1, MemOperand(r0, Context::SlotOffset(Context::GLOBAL_INDEX)));
176
177 // Initialize the rest of the slots to undefined.
178 __ LoadRoot(r1, Heap::kUndefinedValueRootIndex);
179 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
180 __ str(r1, MemOperand(r0, Context::SlotOffset(i)));
181 }
182
183 // Remove the on-stack argument and return.
184 __ mov(cp, r0);
185 __ pop();
186 __ Ret();
187
188 // Need to collect. Call into runtime system.
189 __ bind(&gc);
190 __ TailCallRuntime(Runtime::kNewContext, 1, 1);
191}
192
193
194void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
195 // Stack layout on entry:
196 //
197 // [sp]: constant elements.
198 // [sp + kPointerSize]: literal index.
199 // [sp + (2 * kPointerSize)]: literals array.
200
201 // All sizes here are multiples of kPointerSize.
202 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
203 int size = JSArray::kSize + elements_size;
204
205 // Load boilerplate object into r3 and check if we need to create a
206 // boilerplate.
207 Label slow_case;
208 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
209 __ ldr(r0, MemOperand(sp, 1 * kPointerSize));
210 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
211 __ ldr(r3, MemOperand(r3, r0, LSL, kPointerSizeLog2 - kSmiTagSize));
212 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
213 __ cmp(r3, ip);
214 __ b(eq, &slow_case);
215
216 if (FLAG_debug_code) {
217 const char* message;
218 Heap::RootListIndex expected_map_index;
219 if (mode_ == CLONE_ELEMENTS) {
220 message = "Expected (writable) fixed array";
221 expected_map_index = Heap::kFixedArrayMapRootIndex;
222 } else {
223 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
224 message = "Expected copy-on-write fixed array";
225 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
226 }
227 __ push(r3);
228 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
229 __ ldr(r3, FieldMemOperand(r3, HeapObject::kMapOffset));
230 __ LoadRoot(ip, expected_map_index);
231 __ cmp(r3, ip);
232 __ Assert(eq, message);
233 __ pop(r3);
234 }
235
236 // Allocate both the JS array and the elements array in one big
237 // allocation. This avoids multiple limit checks.
238 __ AllocateInNewSpace(size,
239 r0,
240 r1,
241 r2,
242 &slow_case,
243 TAG_OBJECT);
244
245 // Copy the JS array part.
246 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
247 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
248 __ ldr(r1, FieldMemOperand(r3, i));
249 __ str(r1, FieldMemOperand(r0, i));
250 }
251 }
252
253 if (length_ > 0) {
254 // Get hold of the elements array of the boilerplate and setup the
255 // elements pointer in the resulting object.
256 __ ldr(r3, FieldMemOperand(r3, JSArray::kElementsOffset));
257 __ add(r2, r0, Operand(JSArray::kSize));
258 __ str(r2, FieldMemOperand(r0, JSArray::kElementsOffset));
259
260 // Copy the elements array.
261 __ CopyFields(r2, r3, r1.bit(), elements_size / kPointerSize);
262 }
263
264 // Return and remove the on-stack parameters.
265 __ add(sp, sp, Operand(3 * kPointerSize));
266 __ Ret();
267
268 __ bind(&slow_case);
269 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
270}
271
272
273// Takes a Smi and converts to an IEEE 64 bit floating point value in two
274// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
275// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
276// scratch register. Destroys the source register. No GC occurs during this
277// stub so you don't have to set up the frame.
278class ConvertToDoubleStub : public CodeStub {
279 public:
280 ConvertToDoubleStub(Register result_reg_1,
281 Register result_reg_2,
282 Register source_reg,
283 Register scratch_reg)
284 : result1_(result_reg_1),
285 result2_(result_reg_2),
286 source_(source_reg),
287 zeros_(scratch_reg) { }
288
289 private:
290 Register result1_;
291 Register result2_;
292 Register source_;
293 Register zeros_;
294
295 // Minor key encoding in 16 bits.
296 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
297 class OpBits: public BitField<Token::Value, 2, 14> {};
298
299 Major MajorKey() { return ConvertToDouble; }
300 int MinorKey() {
301 // Encode the parameters in a unique 16 bit value.
302 return result1_.code() +
303 (result2_.code() << 4) +
304 (source_.code() << 8) +
305 (zeros_.code() << 12);
306 }
307
308 void Generate(MacroAssembler* masm);
309
310 const char* GetName() { return "ConvertToDoubleStub"; }
311
312#ifdef DEBUG
313 void Print() { PrintF("ConvertToDoubleStub\n"); }
314#endif
315};
316
317
318void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100319 Register exponent = result1_;
320 Register mantissa = result2_;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100321
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100322 Label not_special;
323 // Convert from Smi to integer.
324 __ mov(source_, Operand(source_, ASR, kSmiTagSize));
325 // Move sign bit from source to destination. This works because the sign bit
326 // in the exponent word of the double has the same position and polarity as
327 // the 2's complement sign bit in a Smi.
328 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
329 __ and_(exponent, source_, Operand(HeapNumber::kSignMask), SetCC);
330 // Subtract from 0 if source was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100331 __ rsb(source_, source_, Operand(0, RelocInfo::NONE), LeaveCC, ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100332
333 // We have -1, 0 or 1, which we treat specially. Register source_ contains
334 // absolute value: it is either equal to 1 (special case of -1 and 1),
335 // greater than 1 (not a special case) or less than 1 (special case of 0).
336 __ cmp(source_, Operand(1));
337 __ b(gt, &not_special);
338
339 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
340 static const uint32_t exponent_word_for_1 =
341 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
342 __ orr(exponent, exponent, Operand(exponent_word_for_1), LeaveCC, eq);
343 // 1, 0 and -1 all have 0 for the second word.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100344 __ mov(mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100345 __ Ret();
346
347 __ bind(&not_special);
348 // Count leading zeros. Uses mantissa for a scratch register on pre-ARM5.
349 // Gets the wrong answer for 0, but we already checked for that case above.
350 __ CountLeadingZeros(zeros_, source_, mantissa);
351 // Compute exponent and or it into the exponent register.
352 // We use mantissa as a scratch register here. Use a fudge factor to
353 // divide the constant 31 + HeapNumber::kExponentBias, 0x41d, into two parts
354 // that fit in the ARM's constant field.
355 int fudge = 0x400;
356 __ rsb(mantissa, zeros_, Operand(31 + HeapNumber::kExponentBias - fudge));
357 __ add(mantissa, mantissa, Operand(fudge));
358 __ orr(exponent,
359 exponent,
360 Operand(mantissa, LSL, HeapNumber::kExponentShift));
361 // Shift up the source chopping the top bit off.
362 __ add(zeros_, zeros_, Operand(1));
363 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
364 __ mov(source_, Operand(source_, LSL, zeros_));
365 // Compute lower part of fraction (last 12 bits).
366 __ mov(mantissa, Operand(source_, LSL, HeapNumber::kMantissaBitsInTopWord));
367 // And the top (top 20 bits).
368 __ orr(exponent,
369 exponent,
370 Operand(source_, LSR, 32 - HeapNumber::kMantissaBitsInTopWord));
371 __ Ret();
372}
373
374
Steve Block1e0659c2011-05-24 12:43:12 +0100375void FloatingPointHelper::LoadSmis(MacroAssembler* masm,
376 FloatingPointHelper::Destination destination,
377 Register scratch1,
378 Register scratch2) {
Ben Murdoch8b112d22011-06-08 16:22:53 +0100379 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100380 CpuFeatures::Scope scope(VFP3);
381 __ mov(scratch1, Operand(r0, ASR, kSmiTagSize));
382 __ vmov(d7.high(), scratch1);
383 __ vcvt_f64_s32(d7, d7.high());
384 __ mov(scratch1, Operand(r1, ASR, kSmiTagSize));
385 __ vmov(d6.high(), scratch1);
386 __ vcvt_f64_s32(d6, d6.high());
387 if (destination == kCoreRegisters) {
388 __ vmov(r2, r3, d7);
389 __ vmov(r0, r1, d6);
390 }
391 } else {
392 ASSERT(destination == kCoreRegisters);
393 // Write Smi from r0 to r3 and r2 in double format.
394 __ mov(scratch1, Operand(r0));
395 ConvertToDoubleStub stub1(r3, r2, scratch1, scratch2);
396 __ push(lr);
397 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100398 // Write Smi from r1 to r1 and r0 in double format.
Steve Block1e0659c2011-05-24 12:43:12 +0100399 __ mov(scratch1, Operand(r1));
400 ConvertToDoubleStub stub2(r1, r0, scratch1, scratch2);
401 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
402 __ pop(lr);
403 }
404}
405
406
407void FloatingPointHelper::LoadOperands(
408 MacroAssembler* masm,
409 FloatingPointHelper::Destination destination,
410 Register heap_number_map,
411 Register scratch1,
412 Register scratch2,
413 Label* slow) {
414
415 // Load right operand (r0) to d6 or r2/r3.
416 LoadNumber(masm, destination,
417 r0, d7, r2, r3, heap_number_map, scratch1, scratch2, slow);
418
419 // Load left operand (r1) to d7 or r0/r1.
420 LoadNumber(masm, destination,
421 r1, d6, r0, r1, heap_number_map, scratch1, scratch2, slow);
422}
423
424
425void FloatingPointHelper::LoadNumber(MacroAssembler* masm,
426 Destination destination,
427 Register object,
428 DwVfpRegister dst,
429 Register dst1,
430 Register dst2,
431 Register heap_number_map,
432 Register scratch1,
433 Register scratch2,
434 Label* not_number) {
435 if (FLAG_debug_code) {
436 __ AbortIfNotRootValue(heap_number_map,
437 Heap::kHeapNumberMapRootIndex,
438 "HeapNumberMap register clobbered.");
439 }
440
441 Label is_smi, done;
442
443 __ JumpIfSmi(object, &is_smi);
444 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_number);
445
446 // Handle loading a double from a heap number.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100447 if (CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +0100448 destination == kVFPRegisters) {
Steve Block1e0659c2011-05-24 12:43:12 +0100449 CpuFeatures::Scope scope(VFP3);
450 // Load the double from tagged HeapNumber to double register.
451 __ sub(scratch1, object, Operand(kHeapObjectTag));
452 __ vldr(dst, scratch1, HeapNumber::kValueOffset);
453 } else {
454 ASSERT(destination == kCoreRegisters);
455 // Load the double from heap number to dst1 and dst2 in double format.
456 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
457 }
458 __ jmp(&done);
459
460 // Handle loading a double from a smi.
461 __ bind(&is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100462 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +0100463 CpuFeatures::Scope scope(VFP3);
464 // Convert smi to double using VFP instructions.
465 __ SmiUntag(scratch1, object);
466 __ vmov(dst.high(), scratch1);
467 __ vcvt_f64_s32(dst, dst.high());
468 if (destination == kCoreRegisters) {
469 // Load the converted smi to dst1 and dst2 in double format.
470 __ vmov(dst1, dst2, dst);
471 }
472 } else {
473 ASSERT(destination == kCoreRegisters);
474 // Write smi to dst1 and dst2 double format.
475 __ mov(scratch1, Operand(object));
476 ConvertToDoubleStub stub(dst2, dst1, scratch1, scratch2);
477 __ push(lr);
478 __ Call(stub.GetCode(), RelocInfo::CODE_TARGET);
479 __ pop(lr);
480 }
481
482 __ bind(&done);
483}
484
485
Steve Block44f0eee2011-05-26 01:26:41 +0100486void FloatingPointHelper::ConvertNumberToInt32(MacroAssembler* masm,
487 Register object,
488 Register dst,
489 Register heap_number_map,
490 Register scratch1,
491 Register scratch2,
492 Register scratch3,
493 DwVfpRegister double_scratch,
494 Label* not_number) {
Steve Block1e0659c2011-05-24 12:43:12 +0100495 if (FLAG_debug_code) {
496 __ AbortIfNotRootValue(heap_number_map,
497 Heap::kHeapNumberMapRootIndex,
498 "HeapNumberMap register clobbered.");
499 }
Steve Block44f0eee2011-05-26 01:26:41 +0100500 Label is_smi;
501 Label done;
502 Label not_in_int32_range;
503
Steve Block1e0659c2011-05-24 12:43:12 +0100504 __ JumpIfSmi(object, &is_smi);
505 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kMapOffset));
506 __ cmp(scratch1, heap_number_map);
Steve Block44f0eee2011-05-26 01:26:41 +0100507 __ b(ne, not_number);
508 __ ConvertToInt32(object,
509 dst,
510 scratch1,
511 scratch2,
512 double_scratch,
513 &not_in_int32_range);
Steve Block1e0659c2011-05-24 12:43:12 +0100514 __ jmp(&done);
Steve Block44f0eee2011-05-26 01:26:41 +0100515
516 __ bind(&not_in_int32_range);
517 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
518 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
519
520 __ EmitOutOfInt32RangeTruncate(dst,
521 scratch1,
522 scratch2,
523 scratch3);
524 __ jmp(&done);
525
Steve Block1e0659c2011-05-24 12:43:12 +0100526 __ bind(&is_smi);
527 __ SmiUntag(dst, object);
528 __ bind(&done);
529}
530
531
Ben Murdoch257744e2011-11-30 15:57:28 +0000532void FloatingPointHelper::ConvertIntToDouble(MacroAssembler* masm,
533 Register int_scratch,
534 Destination destination,
535 DwVfpRegister double_dst,
536 Register dst1,
537 Register dst2,
538 Register scratch2,
539 SwVfpRegister single_scratch) {
540 ASSERT(!int_scratch.is(scratch2));
541 ASSERT(!int_scratch.is(dst1));
542 ASSERT(!int_scratch.is(dst2));
543
544 Label done;
545
546 if (CpuFeatures::IsSupported(VFP3)) {
547 CpuFeatures::Scope scope(VFP3);
548 __ vmov(single_scratch, int_scratch);
549 __ vcvt_f64_s32(double_dst, single_scratch);
550 if (destination == kCoreRegisters) {
551 __ vmov(dst1, dst2, double_dst);
552 }
553 } else {
554 Label fewer_than_20_useful_bits;
555 // Expected output:
556 // | dst2 | dst1 |
557 // | s | exp | mantissa |
558
559 // Check for zero.
560 __ cmp(int_scratch, Operand(0));
561 __ mov(dst2, int_scratch);
562 __ mov(dst1, int_scratch);
563 __ b(eq, &done);
564
565 // Preload the sign of the value.
566 __ and_(dst2, int_scratch, Operand(HeapNumber::kSignMask), SetCC);
567 // Get the absolute value of the object (as an unsigned integer).
568 __ rsb(int_scratch, int_scratch, Operand(0), SetCC, mi);
569
570 // Get mantisssa[51:20].
571
572 // Get the position of the first set bit.
573 __ CountLeadingZeros(dst1, int_scratch, scratch2);
574 __ rsb(dst1, dst1, Operand(31));
575
576 // Set the exponent.
577 __ add(scratch2, dst1, Operand(HeapNumber::kExponentBias));
578 __ Bfi(dst2, scratch2, scratch2,
579 HeapNumber::kExponentShift, HeapNumber::kExponentBits);
580
581 // Clear the first non null bit.
582 __ mov(scratch2, Operand(1));
583 __ bic(int_scratch, int_scratch, Operand(scratch2, LSL, dst1));
584
585 __ cmp(dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
586 // Get the number of bits to set in the lower part of the mantissa.
587 __ sub(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord), SetCC);
588 __ b(mi, &fewer_than_20_useful_bits);
589 // Set the higher 20 bits of the mantissa.
590 __ orr(dst2, dst2, Operand(int_scratch, LSR, scratch2));
591 __ rsb(scratch2, scratch2, Operand(32));
592 __ mov(dst1, Operand(int_scratch, LSL, scratch2));
593 __ b(&done);
594
595 __ bind(&fewer_than_20_useful_bits);
596 __ rsb(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
597 __ mov(scratch2, Operand(int_scratch, LSL, scratch2));
598 __ orr(dst2, dst2, scratch2);
599 // Set dst1 to 0.
600 __ mov(dst1, Operand(0));
601 }
602 __ bind(&done);
603}
604
605
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100606void FloatingPointHelper::LoadNumberAsInt32Double(MacroAssembler* masm,
607 Register object,
608 Destination destination,
609 DwVfpRegister double_dst,
610 Register dst1,
611 Register dst2,
612 Register heap_number_map,
613 Register scratch1,
614 Register scratch2,
615 SwVfpRegister single_scratch,
616 Label* not_int32) {
617 ASSERT(!scratch1.is(object) && !scratch2.is(object));
618 ASSERT(!scratch1.is(scratch2));
619 ASSERT(!heap_number_map.is(object) &&
620 !heap_number_map.is(scratch1) &&
621 !heap_number_map.is(scratch2));
622
623 Label done, obj_is_not_smi;
624
625 __ JumpIfNotSmi(object, &obj_is_not_smi);
626 __ SmiUntag(scratch1, object);
Ben Murdoch257744e2011-11-30 15:57:28 +0000627 ConvertIntToDouble(masm, scratch1, destination, double_dst, dst1, dst2,
628 scratch2, single_scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100629 __ b(&done);
630
631 __ bind(&obj_is_not_smi);
632 if (FLAG_debug_code) {
633 __ AbortIfNotRootValue(heap_number_map,
634 Heap::kHeapNumberMapRootIndex,
635 "HeapNumberMap register clobbered.");
636 }
637 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
638
639 // Load the number.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100640 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100641 CpuFeatures::Scope scope(VFP3);
642 // Load the double value.
643 __ sub(scratch1, object, Operand(kHeapObjectTag));
644 __ vldr(double_dst, scratch1, HeapNumber::kValueOffset);
645
646 __ EmitVFPTruncate(kRoundToZero,
647 single_scratch,
648 double_dst,
649 scratch1,
650 scratch2,
651 kCheckForInexactConversion);
652
653 // Jump to not_int32 if the operation did not succeed.
654 __ b(ne, not_int32);
655
656 if (destination == kCoreRegisters) {
657 __ vmov(dst1, dst2, double_dst);
658 }
659
660 } else {
661 ASSERT(!scratch1.is(object) && !scratch2.is(object));
662 // Load the double value in the destination registers..
663 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
664
665 // Check for 0 and -0.
666 __ bic(scratch1, dst1, Operand(HeapNumber::kSignMask));
667 __ orr(scratch1, scratch1, Operand(dst2));
668 __ cmp(scratch1, Operand(0));
669 __ b(eq, &done);
670
671 // Check that the value can be exactly represented by a 32-bit integer.
672 // Jump to not_int32 if that's not the case.
673 DoubleIs32BitInteger(masm, dst1, dst2, scratch1, scratch2, not_int32);
674
675 // dst1 and dst2 were trashed. Reload the double value.
676 __ Ldrd(dst1, dst2, FieldMemOperand(object, HeapNumber::kValueOffset));
677 }
678
679 __ bind(&done);
680}
681
682
683void FloatingPointHelper::LoadNumberAsInt32(MacroAssembler* masm,
684 Register object,
685 Register dst,
686 Register heap_number_map,
687 Register scratch1,
688 Register scratch2,
689 Register scratch3,
690 DwVfpRegister double_scratch,
691 Label* not_int32) {
692 ASSERT(!dst.is(object));
693 ASSERT(!scratch1.is(object) && !scratch2.is(object) && !scratch3.is(object));
694 ASSERT(!scratch1.is(scratch2) &&
695 !scratch1.is(scratch3) &&
696 !scratch2.is(scratch3));
697
698 Label done;
699
700 // Untag the object into the destination register.
701 __ SmiUntag(dst, object);
702 // Just return if the object is a smi.
703 __ JumpIfSmi(object, &done);
704
705 if (FLAG_debug_code) {
706 __ AbortIfNotRootValue(heap_number_map,
707 Heap::kHeapNumberMapRootIndex,
708 "HeapNumberMap register clobbered.");
709 }
710 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
711
712 // Object is a heap number.
713 // Convert the floating point value to a 32-bit integer.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100714 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100715 CpuFeatures::Scope scope(VFP3);
716 SwVfpRegister single_scratch = double_scratch.low();
717 // Load the double value.
718 __ sub(scratch1, object, Operand(kHeapObjectTag));
719 __ vldr(double_scratch, scratch1, HeapNumber::kValueOffset);
720
721 __ EmitVFPTruncate(kRoundToZero,
722 single_scratch,
723 double_scratch,
724 scratch1,
725 scratch2,
726 kCheckForInexactConversion);
727
728 // Jump to not_int32 if the operation did not succeed.
729 __ b(ne, not_int32);
730 // Get the result in the destination register.
731 __ vmov(dst, single_scratch);
732
733 } else {
734 // Load the double value in the destination registers.
735 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
736 __ ldr(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
737
738 // Check for 0 and -0.
739 __ bic(dst, scratch1, Operand(HeapNumber::kSignMask));
740 __ orr(dst, scratch2, Operand(dst));
741 __ cmp(dst, Operand(0));
742 __ b(eq, &done);
743
744 DoubleIs32BitInteger(masm, scratch1, scratch2, dst, scratch3, not_int32);
745
746 // Registers state after DoubleIs32BitInteger.
747 // dst: mantissa[51:20].
748 // scratch2: 1
749
750 // Shift back the higher bits of the mantissa.
751 __ mov(dst, Operand(dst, LSR, scratch3));
752 // Set the implicit first bit.
753 __ rsb(scratch3, scratch3, Operand(32));
754 __ orr(dst, dst, Operand(scratch2, LSL, scratch3));
755 // Set the sign.
756 __ ldr(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
757 __ tst(scratch1, Operand(HeapNumber::kSignMask));
758 __ rsb(dst, dst, Operand(0), LeaveCC, mi);
759 }
760
761 __ bind(&done);
762}
763
764
765void FloatingPointHelper::DoubleIs32BitInteger(MacroAssembler* masm,
766 Register src1,
767 Register src2,
768 Register dst,
769 Register scratch,
770 Label* not_int32) {
771 // Get exponent alone in scratch.
772 __ Ubfx(scratch,
773 src1,
774 HeapNumber::kExponentShift,
775 HeapNumber::kExponentBits);
776
777 // Substract the bias from the exponent.
778 __ sub(scratch, scratch, Operand(HeapNumber::kExponentBias), SetCC);
779
780 // src1: higher (exponent) part of the double value.
781 // src2: lower (mantissa) part of the double value.
782 // scratch: unbiased exponent.
783
784 // Fast cases. Check for obvious non 32-bit integer values.
785 // Negative exponent cannot yield 32-bit integers.
786 __ b(mi, not_int32);
787 // Exponent greater than 31 cannot yield 32-bit integers.
788 // Also, a positive value with an exponent equal to 31 is outside of the
789 // signed 32-bit integer range.
790 // Another way to put it is that if (exponent - signbit) > 30 then the
791 // number cannot be represented as an int32.
792 Register tmp = dst;
793 __ sub(tmp, scratch, Operand(src1, LSR, 31));
794 __ cmp(tmp, Operand(30));
795 __ b(gt, not_int32);
796 // - Bits [21:0] in the mantissa are not null.
797 __ tst(src2, Operand(0x3fffff));
798 __ b(ne, not_int32);
799
800 // Otherwise the exponent needs to be big enough to shift left all the
801 // non zero bits left. So we need the (30 - exponent) last bits of the
802 // 31 higher bits of the mantissa to be null.
803 // Because bits [21:0] are null, we can check instead that the
804 // (32 - exponent) last bits of the 32 higher bits of the mantisssa are null.
805
806 // Get the 32 higher bits of the mantissa in dst.
807 __ Ubfx(dst,
808 src2,
809 HeapNumber::kMantissaBitsInTopWord,
810 32 - HeapNumber::kMantissaBitsInTopWord);
811 __ orr(dst,
812 dst,
813 Operand(src1, LSL, HeapNumber::kNonMantissaBitsInTopWord));
814
815 // Create the mask and test the lower bits (of the higher bits).
816 __ rsb(scratch, scratch, Operand(32));
817 __ mov(src2, Operand(1));
818 __ mov(src1, Operand(src2, LSL, scratch));
819 __ sub(src1, src1, Operand(1));
820 __ tst(dst, src1);
821 __ b(ne, not_int32);
822}
823
824
825void FloatingPointHelper::CallCCodeForDoubleOperation(
826 MacroAssembler* masm,
827 Token::Value op,
828 Register heap_number_result,
829 Register scratch) {
830 // Using core registers:
831 // r0: Left value (least significant part of mantissa).
832 // r1: Left value (sign, exponent, top of mantissa).
833 // r2: Right value (least significant part of mantissa).
834 // r3: Right value (sign, exponent, top of mantissa).
835
836 // Assert that heap_number_result is callee-saved.
837 // We currently always use r5 to pass it.
838 ASSERT(heap_number_result.is(r5));
839
840 // Push the current return address before the C call. Return will be
841 // through pop(pc) below.
842 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +0000843 __ PrepareCallCFunction(0, 2, scratch);
844 if (masm->use_eabi_hardfloat()) {
845 CpuFeatures::Scope scope(VFP3);
846 __ vmov(d0, r0, r1);
847 __ vmov(d1, r2, r3);
848 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100849 // Call C routine that may not cause GC or other trouble.
Steve Block44f0eee2011-05-26 01:26:41 +0100850 __ CallCFunction(ExternalReference::double_fp_operation(op, masm->isolate()),
Ben Murdoch257744e2011-11-30 15:57:28 +0000851 0, 2);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100852 // Store answer in the overwritable heap number. Double returned in
Ben Murdoch257744e2011-11-30 15:57:28 +0000853 // registers r0 and r1 or in d0.
854 if (masm->use_eabi_hardfloat()) {
855 CpuFeatures::Scope scope(VFP3);
856 __ vstr(d0,
857 FieldMemOperand(heap_number_result, HeapNumber::kValueOffset));
858 } else {
859 __ Strd(r0, r1, FieldMemOperand(heap_number_result,
860 HeapNumber::kValueOffset));
861 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100862 // Place heap_number_result in r0 and return to the pushed return address.
863 __ mov(r0, Operand(heap_number_result));
864 __ pop(pc);
865}
866
Steve Block1e0659c2011-05-24 12:43:12 +0100867
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100868// See comment for class.
869void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
870 Label max_negative_int;
871 // the_int_ has the answer which is a signed int32 but not a Smi.
872 // We test for the special value that has a different exponent. This test
873 // has the neat side effect of setting the flags according to the sign.
874 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
875 __ cmp(the_int_, Operand(0x80000000u));
876 __ b(eq, &max_negative_int);
877 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
878 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
879 uint32_t non_smi_exponent =
880 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
881 __ mov(scratch_, Operand(non_smi_exponent));
882 // Set the sign bit in scratch_ if the value was negative.
883 __ orr(scratch_, scratch_, Operand(HeapNumber::kSignMask), LeaveCC, cs);
884 // Subtract from 0 if the value was negative.
Iain Merrick9ac36c92010-09-13 15:29:50 +0100885 __ rsb(the_int_, the_int_, Operand(0, RelocInfo::NONE), LeaveCC, cs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100886 // We should be masking the implict first digit of the mantissa away here,
887 // but it just ends up combining harmlessly with the last digit of the
888 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
889 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
890 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
891 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
892 __ orr(scratch_, scratch_, Operand(the_int_, LSR, shift_distance));
893 __ str(scratch_, FieldMemOperand(the_heap_number_,
894 HeapNumber::kExponentOffset));
895 __ mov(scratch_, Operand(the_int_, LSL, 32 - shift_distance));
896 __ str(scratch_, FieldMemOperand(the_heap_number_,
897 HeapNumber::kMantissaOffset));
898 __ Ret();
899
900 __ bind(&max_negative_int);
901 // The max negative int32 is stored as a positive number in the mantissa of
902 // a double because it uses a sign bit instead of using two's complement.
903 // The actual mantissa bits stored are all 0 because the implicit most
904 // significant 1 bit is not stored.
905 non_smi_exponent += 1 << HeapNumber::kExponentShift;
906 __ mov(ip, Operand(HeapNumber::kSignMask | non_smi_exponent));
907 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
Iain Merrick9ac36c92010-09-13 15:29:50 +0100908 __ mov(ip, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100909 __ str(ip, FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
910 __ Ret();
911}
912
913
914// Handle the case where the lhs and rhs are the same object.
915// Equality is almost reflexive (everything but NaN), so this is a test
916// for "identity and not NaN".
917static void EmitIdenticalObjectComparison(MacroAssembler* masm,
918 Label* slow,
Steve Block1e0659c2011-05-24 12:43:12 +0100919 Condition cond,
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100920 bool never_nan_nan) {
921 Label not_identical;
922 Label heap_number, return_equal;
923 __ cmp(r0, r1);
924 __ b(ne, &not_identical);
925
926 // The two objects are identical. If we know that one of them isn't NaN then
927 // we now know they test equal.
Steve Block1e0659c2011-05-24 12:43:12 +0100928 if (cond != eq || !never_nan_nan) {
Steve Block44f0eee2011-05-26 01:26:41 +0100929 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100930 // so we do the second best thing - test it ourselves.
931 // They are both equal and they are not both Smis so both of them are not
932 // Smis. If it's not a heap number, then return equal.
Steve Block1e0659c2011-05-24 12:43:12 +0100933 if (cond == lt || cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100934 __ CompareObjectType(r0, r4, r4, FIRST_JS_OBJECT_TYPE);
935 __ b(ge, slow);
936 } else {
937 __ CompareObjectType(r0, r4, r4, HEAP_NUMBER_TYPE);
938 __ b(eq, &heap_number);
939 // Comparing JS objects with <=, >= is complicated.
Steve Block1e0659c2011-05-24 12:43:12 +0100940 if (cond != eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100941 __ cmp(r4, Operand(FIRST_JS_OBJECT_TYPE));
942 __ b(ge, slow);
943 // Normally here we fall through to return_equal, but undefined is
944 // special: (undefined == undefined) == true, but
945 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
Steve Block1e0659c2011-05-24 12:43:12 +0100946 if (cond == le || cond == ge) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100947 __ cmp(r4, Operand(ODDBALL_TYPE));
948 __ b(ne, &return_equal);
949 __ LoadRoot(r2, Heap::kUndefinedValueRootIndex);
950 __ cmp(r0, r2);
951 __ b(ne, &return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +0100952 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100953 // undefined <= undefined should fail.
954 __ mov(r0, Operand(GREATER));
955 } else {
956 // undefined >= undefined should fail.
957 __ mov(r0, Operand(LESS));
958 }
959 __ Ret();
960 }
961 }
962 }
963 }
964
965 __ bind(&return_equal);
Steve Block1e0659c2011-05-24 12:43:12 +0100966 if (cond == lt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100967 __ mov(r0, Operand(GREATER)); // Things aren't less than themselves.
Steve Block1e0659c2011-05-24 12:43:12 +0100968 } else if (cond == gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100969 __ mov(r0, Operand(LESS)); // Things aren't greater than themselves.
970 } else {
971 __ mov(r0, Operand(EQUAL)); // Things are <=, >=, ==, === themselves.
972 }
973 __ Ret();
974
Steve Block1e0659c2011-05-24 12:43:12 +0100975 if (cond != eq || !never_nan_nan) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100976 // For less and greater we don't have to check for NaN since the result of
977 // x < x is false regardless. For the others here is some code to check
978 // for NaN.
Steve Block1e0659c2011-05-24 12:43:12 +0100979 if (cond != lt && cond != gt) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100980 __ bind(&heap_number);
981 // It is a heap number, so return non-equal if it's NaN and equal if it's
982 // not NaN.
983
984 // The representation of NaN values has all exponent bits (52..62) set,
985 // and not all mantissa bits (0..51) clear.
986 // Read top bits of double representation (second word of value).
987 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
988 // Test that exponent bits are all set.
989 __ Sbfx(r3, r2, HeapNumber::kExponentShift, HeapNumber::kExponentBits);
990 // NaNs have all-one exponents so they sign extend to -1.
991 __ cmp(r3, Operand(-1));
992 __ b(ne, &return_equal);
993
994 // Shift out flag and all exponent bits, retaining only mantissa.
995 __ mov(r2, Operand(r2, LSL, HeapNumber::kNonMantissaBitsInTopWord));
996 // Or with all low-bits of mantissa.
997 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
998 __ orr(r0, r3, Operand(r2), SetCC);
999 // For equal we already have the right value in r0: Return zero (equal)
1000 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
1001 // not (it's a NaN). For <= and >= we need to load r0 with the failing
1002 // value if it's a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001003 if (cond != eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001004 // All-zero means Infinity means equal.
1005 __ Ret(eq);
Steve Block1e0659c2011-05-24 12:43:12 +01001006 if (cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001007 __ mov(r0, Operand(GREATER)); // NaN <= NaN should fail.
1008 } else {
1009 __ mov(r0, Operand(LESS)); // NaN >= NaN should fail.
1010 }
1011 }
1012 __ Ret();
1013 }
1014 // No fall through here.
1015 }
1016
1017 __ bind(&not_identical);
1018}
1019
1020
1021// See comment at call site.
1022static void EmitSmiNonsmiComparison(MacroAssembler* masm,
1023 Register lhs,
1024 Register rhs,
1025 Label* lhs_not_nan,
1026 Label* slow,
1027 bool strict) {
1028 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1029 (lhs.is(r1) && rhs.is(r0)));
1030
1031 Label rhs_is_smi;
1032 __ tst(rhs, Operand(kSmiTagMask));
1033 __ b(eq, &rhs_is_smi);
1034
1035 // Lhs is a Smi. Check whether the rhs is a heap number.
1036 __ CompareObjectType(rhs, r4, r4, HEAP_NUMBER_TYPE);
1037 if (strict) {
1038 // If rhs is not a number and lhs is a Smi then strict equality cannot
1039 // succeed. Return non-equal
1040 // If rhs is r0 then there is already a non zero value in it.
1041 if (!rhs.is(r0)) {
1042 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1043 }
1044 __ Ret(ne);
1045 } else {
1046 // Smi compared non-strictly with a non-Smi non-heap-number. Call
1047 // the runtime.
1048 __ b(ne, slow);
1049 }
1050
1051 // Lhs is a smi, rhs is a number.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001052 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001053 // Convert lhs to a double in d7.
1054 CpuFeatures::Scope scope(VFP3);
1055 __ SmiToDoubleVFPRegister(lhs, d7, r7, s15);
1056 // Load the double from rhs, tagged HeapNumber r0, to d6.
1057 __ sub(r7, rhs, Operand(kHeapObjectTag));
1058 __ vldr(d6, r7, HeapNumber::kValueOffset);
1059 } else {
1060 __ push(lr);
1061 // Convert lhs to a double in r2, r3.
1062 __ mov(r7, Operand(lhs));
1063 ConvertToDoubleStub stub1(r3, r2, r7, r6);
1064 __ Call(stub1.GetCode(), RelocInfo::CODE_TARGET);
1065 // Load rhs to a double in r0, r1.
1066 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1067 __ pop(lr);
1068 }
1069
1070 // We now have both loaded as doubles but we can skip the lhs nan check
1071 // since it's a smi.
1072 __ jmp(lhs_not_nan);
1073
1074 __ bind(&rhs_is_smi);
1075 // Rhs is a smi. Check whether the non-smi lhs is a heap number.
1076 __ CompareObjectType(lhs, r4, r4, HEAP_NUMBER_TYPE);
1077 if (strict) {
1078 // If lhs is not a number and rhs is a smi then strict equality cannot
1079 // succeed. Return non-equal.
1080 // If lhs is r0 then there is already a non zero value in it.
1081 if (!lhs.is(r0)) {
1082 __ mov(r0, Operand(NOT_EQUAL), LeaveCC, ne);
1083 }
1084 __ Ret(ne);
1085 } else {
1086 // Smi compared non-strictly with a non-smi non-heap-number. Call
1087 // the runtime.
1088 __ b(ne, slow);
1089 }
1090
1091 // Rhs is a smi, lhs is a heap number.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001092 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001093 CpuFeatures::Scope scope(VFP3);
1094 // Load the double from lhs, tagged HeapNumber r1, to d7.
1095 __ sub(r7, lhs, Operand(kHeapObjectTag));
1096 __ vldr(d7, r7, HeapNumber::kValueOffset);
1097 // Convert rhs to a double in d6 .
1098 __ SmiToDoubleVFPRegister(rhs, d6, r7, s13);
1099 } else {
1100 __ push(lr);
1101 // Load lhs to a double in r2, r3.
1102 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1103 // Convert rhs to a double in r0, r1.
1104 __ mov(r7, Operand(rhs));
1105 ConvertToDoubleStub stub2(r1, r0, r7, r6);
1106 __ Call(stub2.GetCode(), RelocInfo::CODE_TARGET);
1107 __ pop(lr);
1108 }
1109 // Fall through to both_loaded_as_doubles.
1110}
1111
1112
Steve Block1e0659c2011-05-24 12:43:12 +01001113void EmitNanCheck(MacroAssembler* masm, Label* lhs_not_nan, Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001114 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1115 Register rhs_exponent = exp_first ? r0 : r1;
1116 Register lhs_exponent = exp_first ? r2 : r3;
1117 Register rhs_mantissa = exp_first ? r1 : r0;
1118 Register lhs_mantissa = exp_first ? r3 : r2;
1119 Label one_is_nan, neither_is_nan;
1120
1121 __ Sbfx(r4,
1122 lhs_exponent,
1123 HeapNumber::kExponentShift,
1124 HeapNumber::kExponentBits);
1125 // NaNs have all-one exponents so they sign extend to -1.
1126 __ cmp(r4, Operand(-1));
1127 __ b(ne, lhs_not_nan);
1128 __ mov(r4,
1129 Operand(lhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1130 SetCC);
1131 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001132 __ cmp(lhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001133 __ b(ne, &one_is_nan);
1134
1135 __ bind(lhs_not_nan);
1136 __ Sbfx(r4,
1137 rhs_exponent,
1138 HeapNumber::kExponentShift,
1139 HeapNumber::kExponentBits);
1140 // NaNs have all-one exponents so they sign extend to -1.
1141 __ cmp(r4, Operand(-1));
1142 __ b(ne, &neither_is_nan);
1143 __ mov(r4,
1144 Operand(rhs_exponent, LSL, HeapNumber::kNonMantissaBitsInTopWord),
1145 SetCC);
1146 __ b(ne, &one_is_nan);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001147 __ cmp(rhs_mantissa, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001148 __ b(eq, &neither_is_nan);
1149
1150 __ bind(&one_is_nan);
1151 // NaN comparisons always fail.
1152 // Load whatever we need in r0 to make the comparison fail.
Steve Block1e0659c2011-05-24 12:43:12 +01001153 if (cond == lt || cond == le) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001154 __ mov(r0, Operand(GREATER));
1155 } else {
1156 __ mov(r0, Operand(LESS));
1157 }
1158 __ Ret();
1159
1160 __ bind(&neither_is_nan);
1161}
1162
1163
1164// See comment at call site.
Steve Block1e0659c2011-05-24 12:43:12 +01001165static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm,
1166 Condition cond) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001167 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1168 Register rhs_exponent = exp_first ? r0 : r1;
1169 Register lhs_exponent = exp_first ? r2 : r3;
1170 Register rhs_mantissa = exp_first ? r1 : r0;
1171 Register lhs_mantissa = exp_first ? r3 : r2;
1172
1173 // r0, r1, r2, r3 have the two doubles. Neither is a NaN.
Steve Block1e0659c2011-05-24 12:43:12 +01001174 if (cond == eq) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001175 // Doubles are not equal unless they have the same bit pattern.
1176 // Exception: 0 and -0.
1177 __ cmp(rhs_mantissa, Operand(lhs_mantissa));
1178 __ orr(r0, rhs_mantissa, Operand(lhs_mantissa), LeaveCC, ne);
1179 // Return non-zero if the numbers are unequal.
1180 __ Ret(ne);
1181
1182 __ sub(r0, rhs_exponent, Operand(lhs_exponent), SetCC);
1183 // If exponents are equal then return 0.
1184 __ Ret(eq);
1185
1186 // Exponents are unequal. The only way we can return that the numbers
1187 // are equal is if one is -0 and the other is 0. We already dealt
1188 // with the case where both are -0 or both are 0.
1189 // We start by seeing if the mantissas (that are equal) or the bottom
1190 // 31 bits of the rhs exponent are non-zero. If so we return not
1191 // equal.
1192 __ orr(r4, lhs_mantissa, Operand(lhs_exponent, LSL, kSmiTagSize), SetCC);
1193 __ mov(r0, Operand(r4), LeaveCC, ne);
1194 __ Ret(ne);
1195 // Now they are equal if and only if the lhs exponent is zero in its
1196 // low 31 bits.
1197 __ mov(r0, Operand(rhs_exponent, LSL, kSmiTagSize));
1198 __ Ret();
1199 } else {
1200 // Call a native function to do a comparison between two non-NaNs.
1201 // Call C routine that may not cause GC or other trouble.
1202 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00001203 __ PrepareCallCFunction(0, 2, r5);
1204 if (masm->use_eabi_hardfloat()) {
1205 CpuFeatures::Scope scope(VFP3);
1206 __ vmov(d0, r0, r1);
1207 __ vmov(d1, r2, r3);
1208 }
1209 __ CallCFunction(ExternalReference::compare_doubles(masm->isolate()),
1210 0, 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001211 __ pop(pc); // Return.
1212 }
1213}
1214
1215
1216// See comment at call site.
1217static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
1218 Register lhs,
1219 Register rhs) {
1220 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1221 (lhs.is(r1) && rhs.is(r0)));
1222
1223 // If either operand is a JSObject or an oddball value, then they are
1224 // not equal since their pointers are different.
1225 // There is no test for undetectability in strict equality.
1226 STATIC_ASSERT(LAST_TYPE == JS_FUNCTION_TYPE);
1227 Label first_non_object;
1228 // Get the type of the first operand into r2 and compare it with
1229 // FIRST_JS_OBJECT_TYPE.
1230 __ CompareObjectType(rhs, r2, r2, FIRST_JS_OBJECT_TYPE);
1231 __ b(lt, &first_non_object);
1232
1233 // Return non-zero (r0 is not zero)
1234 Label return_not_equal;
1235 __ bind(&return_not_equal);
1236 __ Ret();
1237
1238 __ bind(&first_non_object);
1239 // Check for oddballs: true, false, null, undefined.
1240 __ cmp(r2, Operand(ODDBALL_TYPE));
1241 __ b(eq, &return_not_equal);
1242
1243 __ CompareObjectType(lhs, r3, r3, FIRST_JS_OBJECT_TYPE);
1244 __ b(ge, &return_not_equal);
1245
1246 // Check for oddballs: true, false, null, undefined.
1247 __ cmp(r3, Operand(ODDBALL_TYPE));
1248 __ b(eq, &return_not_equal);
1249
1250 // Now that we have the types we might as well check for symbol-symbol.
1251 // Ensure that no non-strings have the symbol bit set.
1252 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
1253 STATIC_ASSERT(kSymbolTag != 0);
1254 __ and_(r2, r2, Operand(r3));
1255 __ tst(r2, Operand(kIsSymbolMask));
1256 __ b(ne, &return_not_equal);
1257}
1258
1259
1260// See comment at call site.
1261static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
1262 Register lhs,
1263 Register rhs,
1264 Label* both_loaded_as_doubles,
1265 Label* not_heap_numbers,
1266 Label* slow) {
1267 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1268 (lhs.is(r1) && rhs.is(r0)));
1269
1270 __ CompareObjectType(rhs, r3, r2, HEAP_NUMBER_TYPE);
1271 __ b(ne, not_heap_numbers);
1272 __ ldr(r2, FieldMemOperand(lhs, HeapObject::kMapOffset));
1273 __ cmp(r2, r3);
1274 __ b(ne, slow); // First was a heap number, second wasn't. Go slow case.
1275
1276 // Both are heap numbers. Load them up then jump to the code we have
1277 // for that.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001278 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001279 CpuFeatures::Scope scope(VFP3);
1280 __ sub(r7, rhs, Operand(kHeapObjectTag));
1281 __ vldr(d6, r7, HeapNumber::kValueOffset);
1282 __ sub(r7, lhs, Operand(kHeapObjectTag));
1283 __ vldr(d7, r7, HeapNumber::kValueOffset);
1284 } else {
1285 __ Ldrd(r2, r3, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1286 __ Ldrd(r0, r1, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1287 }
1288 __ jmp(both_loaded_as_doubles);
1289}
1290
1291
1292// Fast negative check for symbol-to-symbol equality.
1293static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
1294 Register lhs,
1295 Register rhs,
1296 Label* possible_strings,
1297 Label* not_both_strings) {
1298 ASSERT((lhs.is(r0) && rhs.is(r1)) ||
1299 (lhs.is(r1) && rhs.is(r0)));
1300
1301 // r2 is object type of rhs.
1302 // Ensure that no non-strings have the symbol bit set.
1303 Label object_test;
1304 STATIC_ASSERT(kSymbolTag != 0);
1305 __ tst(r2, Operand(kIsNotStringMask));
1306 __ b(ne, &object_test);
1307 __ tst(r2, Operand(kIsSymbolMask));
1308 __ b(eq, possible_strings);
1309 __ CompareObjectType(lhs, r3, r3, FIRST_NONSTRING_TYPE);
1310 __ b(ge, not_both_strings);
1311 __ tst(r3, Operand(kIsSymbolMask));
1312 __ b(eq, possible_strings);
1313
1314 // Both are symbols. We already checked they weren't the same pointer
1315 // so they are not equal.
1316 __ mov(r0, Operand(NOT_EQUAL));
1317 __ Ret();
1318
1319 __ bind(&object_test);
1320 __ cmp(r2, Operand(FIRST_JS_OBJECT_TYPE));
1321 __ b(lt, not_both_strings);
1322 __ CompareObjectType(lhs, r2, r3, FIRST_JS_OBJECT_TYPE);
1323 __ b(lt, not_both_strings);
1324 // If both objects are undetectable, they are equal. Otherwise, they
1325 // are not equal, since they are different objects and an object is not
1326 // equal to undefined.
1327 __ ldr(r3, FieldMemOperand(rhs, HeapObject::kMapOffset));
1328 __ ldrb(r2, FieldMemOperand(r2, Map::kBitFieldOffset));
1329 __ ldrb(r3, FieldMemOperand(r3, Map::kBitFieldOffset));
1330 __ and_(r0, r2, Operand(r3));
1331 __ and_(r0, r0, Operand(1 << Map::kIsUndetectable));
1332 __ eor(r0, r0, Operand(1 << Map::kIsUndetectable));
1333 __ Ret();
1334}
1335
1336
1337void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
1338 Register object,
1339 Register result,
1340 Register scratch1,
1341 Register scratch2,
1342 Register scratch3,
1343 bool object_is_smi,
1344 Label* not_found) {
1345 // Use of registers. Register result is used as a temporary.
1346 Register number_string_cache = result;
1347 Register mask = scratch3;
1348
1349 // Load the number string cache.
1350 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
1351
1352 // Make the hash mask from the length of the number string cache. It
1353 // contains two elements (number and string) for each cache entry.
1354 __ ldr(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
1355 // Divide length by two (length is a smi).
1356 __ mov(mask, Operand(mask, ASR, kSmiTagSize + 1));
1357 __ sub(mask, mask, Operand(1)); // Make mask.
1358
1359 // Calculate the entry in the number string cache. The hash value in the
1360 // number string cache for smis is just the smi value, and the hash for
1361 // doubles is the xor of the upper and lower words. See
1362 // Heap::GetNumberStringCache.
Steve Block44f0eee2011-05-26 01:26:41 +01001363 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001364 Label is_smi;
1365 Label load_result_from_cache;
1366 if (!object_is_smi) {
Steve Block1e0659c2011-05-24 12:43:12 +01001367 __ JumpIfSmi(object, &is_smi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001368 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001369 CpuFeatures::Scope scope(VFP3);
1370 __ CheckMap(object,
1371 scratch1,
1372 Heap::kHeapNumberMapRootIndex,
1373 not_found,
Ben Murdoch257744e2011-11-30 15:57:28 +00001374 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001375
1376 STATIC_ASSERT(8 == kDoubleSize);
1377 __ add(scratch1,
1378 object,
1379 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
1380 __ ldm(ia, scratch1, scratch1.bit() | scratch2.bit());
1381 __ eor(scratch1, scratch1, Operand(scratch2));
1382 __ and_(scratch1, scratch1, Operand(mask));
1383
1384 // Calculate address of entry in string cache: each entry consists
1385 // of two pointer sized fields.
1386 __ add(scratch1,
1387 number_string_cache,
1388 Operand(scratch1, LSL, kPointerSizeLog2 + 1));
1389
1390 Register probe = mask;
1391 __ ldr(probe,
1392 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
Steve Block1e0659c2011-05-24 12:43:12 +01001393 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001394 __ sub(scratch2, object, Operand(kHeapObjectTag));
1395 __ vldr(d0, scratch2, HeapNumber::kValueOffset);
1396 __ sub(probe, probe, Operand(kHeapObjectTag));
1397 __ vldr(d1, probe, HeapNumber::kValueOffset);
Ben Murdochb8e0da22011-05-16 14:20:40 +01001398 __ VFPCompareAndSetFlags(d0, d1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001399 __ b(ne, not_found); // The cache did not contain this value.
1400 __ b(&load_result_from_cache);
1401 } else {
1402 __ b(not_found);
1403 }
1404 }
1405
1406 __ bind(&is_smi);
1407 Register scratch = scratch1;
1408 __ and_(scratch, mask, Operand(object, ASR, 1));
1409 // Calculate address of entry in string cache: each entry consists
1410 // of two pointer sized fields.
1411 __ add(scratch,
1412 number_string_cache,
1413 Operand(scratch, LSL, kPointerSizeLog2 + 1));
1414
1415 // Check if the entry is the smi we are looking for.
1416 Register probe = mask;
1417 __ ldr(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
1418 __ cmp(object, probe);
1419 __ b(ne, not_found);
1420
1421 // Get the result from the cache.
1422 __ bind(&load_result_from_cache);
1423 __ ldr(result,
1424 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01001425 __ IncrementCounter(isolate->counters()->number_to_string_native(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001426 1,
1427 scratch1,
1428 scratch2);
1429}
1430
1431
1432void NumberToStringStub::Generate(MacroAssembler* masm) {
1433 Label runtime;
1434
1435 __ ldr(r1, MemOperand(sp, 0));
1436
1437 // Generate code to lookup number in the number string cache.
1438 GenerateLookupNumberStringCache(masm, r1, r0, r2, r3, r4, false, &runtime);
1439 __ add(sp, sp, Operand(1 * kPointerSize));
1440 __ Ret();
1441
1442 __ bind(&runtime);
1443 // Handle number to string in the runtime system if not found in the cache.
1444 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
1445}
1446
1447
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001448// On entry lhs_ and rhs_ are the values to be compared.
1449// On exit r0 is 0, positive or negative to indicate the result of
1450// the comparison.
1451void CompareStub::Generate(MacroAssembler* masm) {
1452 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
1453 (lhs_.is(r1) && rhs_.is(r0)));
1454
1455 Label slow; // Call builtin.
1456 Label not_smis, both_loaded_as_doubles, lhs_not_nan;
1457
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001458 if (include_smi_compare_) {
1459 Label not_two_smis, smi_done;
1460 __ orr(r2, r1, r0);
1461 __ tst(r2, Operand(kSmiTagMask));
1462 __ b(ne, &not_two_smis);
Ben Murdochf87a2032010-10-22 12:50:53 +01001463 __ mov(r1, Operand(r1, ASR, 1));
1464 __ sub(r0, r1, Operand(r0, ASR, 1));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001465 __ Ret();
1466 __ bind(&not_two_smis);
1467 } else if (FLAG_debug_code) {
1468 __ orr(r2, r1, r0);
1469 __ tst(r2, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01001470 __ Assert(ne, "CompareStub: unexpected smi operands.");
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001471 }
1472
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001473 // NOTICE! This code is only reached after a smi-fast-case check, so
1474 // it is certain that at least one operand isn't a smi.
1475
1476 // Handle the case where the objects are identical. Either returns the answer
1477 // or goes to slow. Only falls through if the objects were not identical.
1478 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
1479
1480 // If either is a Smi (we know that not both are), then they can only
1481 // be strictly equal if the other is a HeapNumber.
1482 STATIC_ASSERT(kSmiTag == 0);
1483 ASSERT_EQ(0, Smi::FromInt(0));
1484 __ and_(r2, lhs_, Operand(rhs_));
1485 __ tst(r2, Operand(kSmiTagMask));
1486 __ b(ne, &not_smis);
1487 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
1488 // 1) Return the answer.
1489 // 2) Go to slow.
1490 // 3) Fall through to both_loaded_as_doubles.
1491 // 4) Jump to lhs_not_nan.
1492 // In cases 3 and 4 we have found out we were dealing with a number-number
1493 // comparison. If VFP3 is supported the double values of the numbers have
1494 // been loaded into d7 and d6. Otherwise, the double values have been loaded
1495 // into r0, r1, r2, and r3.
1496 EmitSmiNonsmiComparison(masm, lhs_, rhs_, &lhs_not_nan, &slow, strict_);
1497
1498 __ bind(&both_loaded_as_doubles);
1499 // The arguments have been converted to doubles and stored in d6 and d7, if
1500 // VFP3 is supported, or in r0, r1, r2, and r3.
Steve Block44f0eee2011-05-26 01:26:41 +01001501 Isolate* isolate = masm->isolate();
Ben Murdoch8b112d22011-06-08 16:22:53 +01001502 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001503 __ bind(&lhs_not_nan);
1504 CpuFeatures::Scope scope(VFP3);
1505 Label no_nan;
1506 // ARMv7 VFP3 instructions to implement double precision comparison.
Ben Murdochb8e0da22011-05-16 14:20:40 +01001507 __ VFPCompareAndSetFlags(d7, d6);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001508 Label nan;
1509 __ b(vs, &nan);
1510 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
1511 __ mov(r0, Operand(LESS), LeaveCC, lt);
1512 __ mov(r0, Operand(GREATER), LeaveCC, gt);
1513 __ Ret();
1514
1515 __ bind(&nan);
1516 // If one of the sides was a NaN then the v flag is set. Load r0 with
1517 // whatever it takes to make the comparison fail, since comparisons with NaN
1518 // always fail.
1519 if (cc_ == lt || cc_ == le) {
1520 __ mov(r0, Operand(GREATER));
1521 } else {
1522 __ mov(r0, Operand(LESS));
1523 }
1524 __ Ret();
1525 } else {
1526 // Checks for NaN in the doubles we have loaded. Can return the answer or
1527 // fall through if neither is a NaN. Also binds lhs_not_nan.
1528 EmitNanCheck(masm, &lhs_not_nan, cc_);
1529 // Compares two doubles in r0, r1, r2, r3 that are not NaNs. Returns the
1530 // answer. Never falls through.
1531 EmitTwoNonNanDoubleComparison(masm, cc_);
1532 }
1533
1534 __ bind(&not_smis);
1535 // At this point we know we are dealing with two different objects,
1536 // and neither of them is a Smi. The objects are in rhs_ and lhs_.
1537 if (strict_) {
1538 // This returns non-equal for some object types, or falls through if it
1539 // was not lucky.
1540 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1541 }
1542
1543 Label check_for_symbols;
1544 Label flat_string_check;
1545 // Check for heap-number-heap-number comparison. Can jump to slow case,
1546 // or load both doubles into r0, r1, r2, r3 and jump to the code that handles
1547 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1548 // In this case r2 will contain the type of rhs_. Never falls through.
1549 EmitCheckForTwoHeapNumbers(masm,
1550 lhs_,
1551 rhs_,
1552 &both_loaded_as_doubles,
1553 &check_for_symbols,
1554 &flat_string_check);
1555
1556 __ bind(&check_for_symbols);
1557 // In the strict case the EmitStrictTwoHeapObjectCompare already took care of
1558 // symbols.
1559 if (cc_ == eq && !strict_) {
1560 // Returns an answer for two symbols or two detectable objects.
1561 // Otherwise jumps to string case or not both strings case.
1562 // Assumes that r2 is the type of rhs_ on entry.
1563 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1564 }
1565
1566 // Check for both being sequential ASCII strings, and inline if that is the
1567 // case.
1568 __ bind(&flat_string_check);
1569
1570 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, r2, r3, &slow);
1571
Steve Block44f0eee2011-05-26 01:26:41 +01001572 __ IncrementCounter(isolate->counters()->string_compare_native(), 1, r2, r3);
Ben Murdoch257744e2011-11-30 15:57:28 +00001573 if (cc_ == eq) {
1574 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001575 lhs_,
1576 rhs_,
1577 r2,
1578 r3,
Ben Murdoch257744e2011-11-30 15:57:28 +00001579 r4);
1580 } else {
1581 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1582 lhs_,
1583 rhs_,
1584 r2,
1585 r3,
1586 r4,
1587 r5);
1588 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001589 // Never falls through to here.
1590
1591 __ bind(&slow);
1592
1593 __ Push(lhs_, rhs_);
1594 // Figure out which native to call and setup the arguments.
1595 Builtins::JavaScript native;
1596 if (cc_ == eq) {
1597 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1598 } else {
1599 native = Builtins::COMPARE;
1600 int ncr; // NaN compare result
1601 if (cc_ == lt || cc_ == le) {
1602 ncr = GREATER;
1603 } else {
1604 ASSERT(cc_ == gt || cc_ == ge); // remaining cases
1605 ncr = LESS;
1606 }
1607 __ mov(r0, Operand(Smi::FromInt(ncr)));
1608 __ push(r0);
1609 }
1610
1611 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1612 // tagged as a small integer.
Ben Murdoch257744e2011-11-30 15:57:28 +00001613 __ InvokeBuiltin(native, JUMP_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001614}
1615
1616
1617// This stub does not handle the inlined cases (Smis, Booleans, undefined).
1618// The stub returns zero for false, and a non-zero value for true.
1619void ToBooleanStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001620 // This stub uses VFP3 instructions.
Ben Murdoch257744e2011-11-30 15:57:28 +00001621 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001622
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001623 Label false_result;
1624 Label not_heap_number;
Steve Block1e0659c2011-05-24 12:43:12 +01001625 Register scratch = r9.is(tos_) ? r7 : r9;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001626
Ben Murdoch257744e2011-11-30 15:57:28 +00001627 // undefined -> false
1628 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
1629 __ cmp(tos_, ip);
1630 __ b(eq, &false_result);
1631
1632 // Boolean -> its value
1633 __ LoadRoot(ip, Heap::kFalseValueRootIndex);
1634 __ cmp(tos_, ip);
1635 __ b(eq, &false_result);
1636 __ LoadRoot(ip, Heap::kTrueValueRootIndex);
1637 __ cmp(tos_, ip);
1638 // "tos_" is a register and contains a non-zero value. Hence we implicitly
1639 // return true if the equal condition is satisfied.
1640 __ Ret(eq);
1641
1642 // Smis: 0 -> false, all other -> true
1643 __ tst(tos_, tos_);
1644 __ b(eq, &false_result);
1645 __ tst(tos_, Operand(kSmiTagMask));
1646 // "tos_" is a register and contains a non-zero value. Hence we implicitly
1647 // return true if the not equal condition is satisfied.
1648 __ Ret(eq);
1649
1650 // 'null' -> false
Ben Murdochb0fe1622011-05-05 13:52:32 +01001651 __ LoadRoot(ip, Heap::kNullValueRootIndex);
1652 __ cmp(tos_, ip);
1653 __ b(eq, &false_result);
1654
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001655 // HeapNumber => false iff +0, -0, or NaN.
1656 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1657 __ LoadRoot(ip, Heap::kHeapNumberMapRootIndex);
1658 __ cmp(scratch, ip);
1659 __ b(&not_heap_number, ne);
Ben Murdoch257744e2011-11-30 15:57:28 +00001660 __ vldr(d1, FieldMemOperand(tos_, HeapNumber::kValueOffset));
Ben Murdochb8e0da22011-05-16 14:20:40 +01001661 __ VFPCompareAndSetFlags(d1, 0.0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001662 // "tos_" is a register, and contains a non zero value by default.
1663 // Hence we only need to overwrite "tos_" with zero to return false for
1664 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
Iain Merrick9ac36c92010-09-13 15:29:50 +01001665 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, eq); // for FP_ZERO
1666 __ mov(tos_, Operand(0, RelocInfo::NONE), LeaveCC, vs); // for FP_NAN
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001667 __ Ret();
1668
1669 __ bind(&not_heap_number);
1670
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001671 // It can be an undetectable object.
1672 // Undetectable => false.
1673 __ ldr(ip, FieldMemOperand(tos_, HeapObject::kMapOffset));
1674 __ ldrb(scratch, FieldMemOperand(ip, Map::kBitFieldOffset));
1675 __ and_(scratch, scratch, Operand(1 << Map::kIsUndetectable));
1676 __ cmp(scratch, Operand(1 << Map::kIsUndetectable));
1677 __ b(&false_result, eq);
1678
1679 // JavaScript object => true.
1680 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1681 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1682 __ cmp(scratch, Operand(FIRST_JS_OBJECT_TYPE));
1683 // "tos_" is a register and contains a non-zero value.
1684 // Hence we implicitly return true if the greater than
1685 // condition is satisfied.
1686 __ Ret(gt);
1687
1688 // Check for string
1689 __ ldr(scratch, FieldMemOperand(tos_, HeapObject::kMapOffset));
1690 __ ldrb(scratch, FieldMemOperand(scratch, Map::kInstanceTypeOffset));
1691 __ cmp(scratch, Operand(FIRST_NONSTRING_TYPE));
1692 // "tos_" is a register and contains a non-zero value.
1693 // Hence we implicitly return true if the greater than
1694 // condition is satisfied.
1695 __ Ret(gt);
1696
1697 // String value => false iff empty, i.e., length is zero
1698 __ ldr(tos_, FieldMemOperand(tos_, String::kLengthOffset));
1699 // If length is zero, "tos_" contains zero ==> false.
1700 // If length is not zero, "tos_" contains a non-zero value ==> true.
1701 __ Ret();
1702
1703 // Return 0 in "tos_" for false .
1704 __ bind(&false_result);
Iain Merrick9ac36c92010-09-13 15:29:50 +01001705 __ mov(tos_, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001706 __ Ret();
1707}
1708
1709
Ben Murdoch257744e2011-11-30 15:57:28 +00001710Handle<Code> GetUnaryOpStub(int key, UnaryOpIC::TypeInfo type_info) {
1711 UnaryOpStub stub(key, type_info);
Steve Block1e0659c2011-05-24 12:43:12 +01001712 return stub.GetCode();
1713}
1714
1715
Ben Murdoch257744e2011-11-30 15:57:28 +00001716const char* UnaryOpStub::GetName() {
1717 if (name_ != NULL) return name_;
1718 const int kMaxNameLength = 100;
1719 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
1720 kMaxNameLength);
1721 if (name_ == NULL) return "OOM";
1722 const char* op_name = Token::Name(op_);
1723 const char* overwrite_name = NULL; // Make g++ happy.
1724 switch (mode_) {
1725 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
1726 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
1727 }
1728
1729 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
1730 "UnaryOpStub_%s_%s_%s",
1731 op_name,
1732 overwrite_name,
1733 UnaryOpIC::GetName(operand_type_));
1734 return name_;
1735}
1736
1737
1738// TODO(svenpanne): Use virtual functions instead of switch.
1739void UnaryOpStub::Generate(MacroAssembler* masm) {
1740 switch (operand_type_) {
1741 case UnaryOpIC::UNINITIALIZED:
1742 GenerateTypeTransition(masm);
1743 break;
1744 case UnaryOpIC::SMI:
1745 GenerateSmiStub(masm);
1746 break;
1747 case UnaryOpIC::HEAP_NUMBER:
1748 GenerateHeapNumberStub(masm);
1749 break;
1750 case UnaryOpIC::GENERIC:
1751 GenerateGenericStub(masm);
1752 break;
1753 }
1754}
1755
1756
1757void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
1758 // Prepare to push argument.
1759 __ mov(r3, Operand(r0));
1760
1761 // Push this stub's key. Although the operation and the type info are
1762 // encoded into the key, the encoding is opaque, so push them too.
1763 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
1764 __ mov(r1, Operand(Smi::FromInt(op_)));
1765 __ mov(r0, Operand(Smi::FromInt(operand_type_)));
1766
1767 __ Push(r3, r2, r1, r0);
1768
1769 __ TailCallExternalReference(
1770 ExternalReference(IC_Utility(IC::kUnaryOp_Patch),
1771 masm->isolate()),
1772 4,
1773 1);
1774}
1775
1776
1777// TODO(svenpanne): Use virtual functions instead of switch.
1778void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
1779 switch (op_) {
1780 case Token::SUB:
1781 GenerateSmiStubSub(masm);
1782 break;
1783 case Token::BIT_NOT:
1784 GenerateSmiStubBitNot(masm);
1785 break;
1786 default:
1787 UNREACHABLE();
1788 }
1789}
1790
1791
1792void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
1793 Label non_smi, slow;
1794 GenerateSmiCodeSub(masm, &non_smi, &slow);
1795 __ bind(&non_smi);
1796 __ bind(&slow);
1797 GenerateTypeTransition(masm);
1798}
1799
1800
1801void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
1802 Label non_smi;
1803 GenerateSmiCodeBitNot(masm, &non_smi);
1804 __ bind(&non_smi);
1805 GenerateTypeTransition(masm);
1806}
1807
1808
1809void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
1810 Label* non_smi,
1811 Label* slow) {
1812 __ JumpIfNotSmi(r0, non_smi);
1813
1814 // The result of negating zero or the smallest negative smi is not a smi.
1815 __ bic(ip, r0, Operand(0x80000000), SetCC);
1816 __ b(eq, slow);
1817
1818 // Return '0 - value'.
1819 __ rsb(r0, r0, Operand(0, RelocInfo::NONE));
1820 __ Ret();
1821}
1822
1823
1824void UnaryOpStub::GenerateSmiCodeBitNot(MacroAssembler* masm,
1825 Label* non_smi) {
1826 __ JumpIfNotSmi(r0, non_smi);
1827
1828 // Flip bits and revert inverted smi-tag.
1829 __ mvn(r0, Operand(r0));
1830 __ bic(r0, r0, Operand(kSmiTagMask));
1831 __ Ret();
1832}
1833
1834
1835// TODO(svenpanne): Use virtual functions instead of switch.
1836void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
1837 switch (op_) {
1838 case Token::SUB:
1839 GenerateHeapNumberStubSub(masm);
1840 break;
1841 case Token::BIT_NOT:
1842 GenerateHeapNumberStubBitNot(masm);
1843 break;
1844 default:
1845 UNREACHABLE();
1846 }
1847}
1848
1849
1850void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
1851 Label non_smi, slow, call_builtin;
1852 GenerateSmiCodeSub(masm, &non_smi, &call_builtin);
1853 __ bind(&non_smi);
1854 GenerateHeapNumberCodeSub(masm, &slow);
1855 __ bind(&slow);
1856 GenerateTypeTransition(masm);
1857 __ bind(&call_builtin);
1858 GenerateGenericCodeFallback(masm);
1859}
1860
1861
1862void UnaryOpStub::GenerateHeapNumberStubBitNot(MacroAssembler* masm) {
1863 Label non_smi, slow;
1864 GenerateSmiCodeBitNot(masm, &non_smi);
1865 __ bind(&non_smi);
1866 GenerateHeapNumberCodeBitNot(masm, &slow);
1867 __ bind(&slow);
1868 GenerateTypeTransition(masm);
1869}
1870
1871void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
1872 Label* slow) {
1873 EmitCheckForHeapNumber(masm, r0, r1, r6, slow);
1874 // r0 is a heap number. Get a new heap number in r1.
1875 if (mode_ == UNARY_OVERWRITE) {
1876 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1877 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
1878 __ str(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1879 } else {
1880 Label slow_allocate_heapnumber, heapnumber_allocated;
1881 __ AllocateHeapNumber(r1, r2, r3, r6, &slow_allocate_heapnumber);
1882 __ jmp(&heapnumber_allocated);
1883
1884 __ bind(&slow_allocate_heapnumber);
1885 __ EnterInternalFrame();
1886 __ push(r0);
1887 __ CallRuntime(Runtime::kNumberAlloc, 0);
1888 __ mov(r1, Operand(r0));
1889 __ pop(r0);
1890 __ LeaveInternalFrame();
1891
1892 __ bind(&heapnumber_allocated);
1893 __ ldr(r3, FieldMemOperand(r0, HeapNumber::kMantissaOffset));
1894 __ ldr(r2, FieldMemOperand(r0, HeapNumber::kExponentOffset));
1895 __ str(r3, FieldMemOperand(r1, HeapNumber::kMantissaOffset));
1896 __ eor(r2, r2, Operand(HeapNumber::kSignMask)); // Flip sign.
1897 __ str(r2, FieldMemOperand(r1, HeapNumber::kExponentOffset));
1898 __ mov(r0, Operand(r1));
1899 }
1900 __ Ret();
1901}
1902
1903
1904void UnaryOpStub::GenerateHeapNumberCodeBitNot(
1905 MacroAssembler* masm, Label* slow) {
1906 EmitCheckForHeapNumber(masm, r0, r1, r6, slow);
1907 // Convert the heap number is r0 to an untagged integer in r1.
1908 __ ConvertToInt32(r0, r1, r2, r3, d0, slow);
1909
1910 // Do the bitwise operation and check if the result fits in a smi.
1911 Label try_float;
1912 __ mvn(r1, Operand(r1));
1913 __ add(r2, r1, Operand(0x40000000), SetCC);
1914 __ b(mi, &try_float);
1915
1916 // Tag the result as a smi and we're done.
1917 __ mov(r0, Operand(r1, LSL, kSmiTagSize));
1918 __ Ret();
1919
1920 // Try to store the result in a heap number.
1921 __ bind(&try_float);
1922 if (mode_ == UNARY_NO_OVERWRITE) {
1923 Label slow_allocate_heapnumber, heapnumber_allocated;
1924 __ AllocateHeapNumber(r0, r2, r3, r6, &slow_allocate_heapnumber);
1925 __ jmp(&heapnumber_allocated);
1926
1927 __ bind(&slow_allocate_heapnumber);
1928 __ EnterInternalFrame();
1929 __ push(r1);
1930 __ CallRuntime(Runtime::kNumberAlloc, 0);
1931 __ pop(r1);
1932 __ LeaveInternalFrame();
1933
1934 __ bind(&heapnumber_allocated);
1935 }
1936
1937 if (CpuFeatures::IsSupported(VFP3)) {
1938 // Convert the int32 in r1 to the heap number in r0. r2 is corrupted.
1939 CpuFeatures::Scope scope(VFP3);
1940 __ vmov(s0, r1);
1941 __ vcvt_f64_s32(d0, s0);
1942 __ sub(r2, r0, Operand(kHeapObjectTag));
1943 __ vstr(d0, r2, HeapNumber::kValueOffset);
1944 __ Ret();
1945 } else {
1946 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
1947 // have to set up a frame.
1948 WriteInt32ToHeapNumberStub stub(r1, r0, r2);
1949 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
1950 }
1951}
1952
1953
1954// TODO(svenpanne): Use virtual functions instead of switch.
1955void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
1956 switch (op_) {
1957 case Token::SUB:
1958 GenerateGenericStubSub(masm);
1959 break;
1960 case Token::BIT_NOT:
1961 GenerateGenericStubBitNot(masm);
1962 break;
1963 default:
1964 UNREACHABLE();
1965 }
1966}
1967
1968
1969void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
1970 Label non_smi, slow;
1971 GenerateSmiCodeSub(masm, &non_smi, &slow);
1972 __ bind(&non_smi);
1973 GenerateHeapNumberCodeSub(masm, &slow);
1974 __ bind(&slow);
1975 GenerateGenericCodeFallback(masm);
1976}
1977
1978
1979void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
1980 Label non_smi, slow;
1981 GenerateSmiCodeBitNot(masm, &non_smi);
1982 __ bind(&non_smi);
1983 GenerateHeapNumberCodeBitNot(masm, &slow);
1984 __ bind(&slow);
1985 GenerateGenericCodeFallback(masm);
1986}
1987
1988
1989void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
1990 // Handle the slow case by jumping to the JavaScript builtin.
1991 __ push(r0);
1992 switch (op_) {
1993 case Token::SUB:
1994 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
1995 break;
1996 case Token::BIT_NOT:
1997 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
1998 break;
1999 default:
2000 UNREACHABLE();
2001 }
2002}
2003
2004
2005Handle<Code> GetBinaryOpStub(int key,
2006 BinaryOpIC::TypeInfo type_info,
2007 BinaryOpIC::TypeInfo result_type_info) {
2008 BinaryOpStub stub(key, type_info, result_type_info);
2009 return stub.GetCode();
2010}
2011
2012
2013void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002014 Label get_result;
2015
2016 __ Push(r1, r0);
2017
2018 __ mov(r2, Operand(Smi::FromInt(MinorKey())));
2019 __ mov(r1, Operand(Smi::FromInt(op_)));
2020 __ mov(r0, Operand(Smi::FromInt(operands_type_)));
2021 __ Push(r2, r1, r0);
2022
2023 __ TailCallExternalReference(
Ben Murdoch257744e2011-11-30 15:57:28 +00002024 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
Steve Block44f0eee2011-05-26 01:26:41 +01002025 masm->isolate()),
Steve Block1e0659c2011-05-24 12:43:12 +01002026 5,
2027 1);
2028}
2029
2030
Ben Murdoch257744e2011-11-30 15:57:28 +00002031void BinaryOpStub::GenerateTypeTransitionWithSavedArgs(
Steve Block1e0659c2011-05-24 12:43:12 +01002032 MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002033 UNIMPLEMENTED();
Steve Block1e0659c2011-05-24 12:43:12 +01002034}
2035
2036
Ben Murdoch257744e2011-11-30 15:57:28 +00002037void BinaryOpStub::Generate(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002038 switch (operands_type_) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002039 case BinaryOpIC::UNINITIALIZED:
Steve Block1e0659c2011-05-24 12:43:12 +01002040 GenerateTypeTransition(masm);
2041 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002042 case BinaryOpIC::SMI:
Steve Block1e0659c2011-05-24 12:43:12 +01002043 GenerateSmiStub(masm);
2044 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002045 case BinaryOpIC::INT32:
Steve Block1e0659c2011-05-24 12:43:12 +01002046 GenerateInt32Stub(masm);
2047 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002048 case BinaryOpIC::HEAP_NUMBER:
Steve Block1e0659c2011-05-24 12:43:12 +01002049 GenerateHeapNumberStub(masm);
2050 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002051 case BinaryOpIC::ODDBALL:
Steve Block44f0eee2011-05-26 01:26:41 +01002052 GenerateOddballStub(masm);
2053 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002054 case BinaryOpIC::BOTH_STRING:
2055 GenerateBothStringStub(masm);
2056 break;
2057 case BinaryOpIC::STRING:
Steve Block1e0659c2011-05-24 12:43:12 +01002058 GenerateStringStub(masm);
2059 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00002060 case BinaryOpIC::GENERIC:
Steve Block1e0659c2011-05-24 12:43:12 +01002061 GenerateGeneric(masm);
2062 break;
2063 default:
2064 UNREACHABLE();
2065 }
2066}
2067
2068
Ben Murdoch257744e2011-11-30 15:57:28 +00002069const char* BinaryOpStub::GetName() {
Steve Block1e0659c2011-05-24 12:43:12 +01002070 if (name_ != NULL) return name_;
2071 const int kMaxNameLength = 100;
Steve Block44f0eee2011-05-26 01:26:41 +01002072 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
2073 kMaxNameLength);
Steve Block1e0659c2011-05-24 12:43:12 +01002074 if (name_ == NULL) return "OOM";
2075 const char* op_name = Token::Name(op_);
2076 const char* overwrite_name;
2077 switch (mode_) {
2078 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
2079 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
2080 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
2081 default: overwrite_name = "UnknownOverwrite"; break;
2082 }
2083
2084 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
Ben Murdoch257744e2011-11-30 15:57:28 +00002085 "BinaryOpStub_%s_%s_%s",
Steve Block1e0659c2011-05-24 12:43:12 +01002086 op_name,
2087 overwrite_name,
Ben Murdoch257744e2011-11-30 15:57:28 +00002088 BinaryOpIC::GetName(operands_type_));
Steve Block1e0659c2011-05-24 12:43:12 +01002089 return name_;
2090}
2091
2092
Ben Murdoch257744e2011-11-30 15:57:28 +00002093void BinaryOpStub::GenerateSmiSmiOperation(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002094 Register left = r1;
2095 Register right = r0;
2096 Register scratch1 = r7;
2097 Register scratch2 = r9;
2098
2099 ASSERT(right.is(r0));
2100 STATIC_ASSERT(kSmiTag == 0);
2101
2102 Label not_smi_result;
2103 switch (op_) {
2104 case Token::ADD:
2105 __ add(right, left, Operand(right), SetCC); // Add optimistically.
2106 __ Ret(vc);
2107 __ sub(right, right, Operand(left)); // Revert optimistic add.
2108 break;
2109 case Token::SUB:
2110 __ sub(right, left, Operand(right), SetCC); // Subtract optimistically.
2111 __ Ret(vc);
2112 __ sub(right, left, Operand(right)); // Revert optimistic subtract.
2113 break;
2114 case Token::MUL:
2115 // Remove tag from one of the operands. This way the multiplication result
2116 // will be a smi if it fits the smi range.
2117 __ SmiUntag(ip, right);
2118 // Do multiplication
2119 // scratch1 = lower 32 bits of ip * left.
2120 // scratch2 = higher 32 bits of ip * left.
2121 __ smull(scratch1, scratch2, left, ip);
2122 // Check for overflowing the smi range - no overflow if higher 33 bits of
2123 // the result are identical.
2124 __ mov(ip, Operand(scratch1, ASR, 31));
2125 __ cmp(ip, Operand(scratch2));
2126 __ b(ne, &not_smi_result);
2127 // Go slow on zero result to handle -0.
2128 __ tst(scratch1, Operand(scratch1));
2129 __ mov(right, Operand(scratch1), LeaveCC, ne);
2130 __ Ret(ne);
2131 // We need -0 if we were multiplying a negative number with 0 to get 0.
2132 // We know one of them was zero.
2133 __ add(scratch2, right, Operand(left), SetCC);
2134 __ mov(right, Operand(Smi::FromInt(0)), LeaveCC, pl);
2135 __ Ret(pl); // Return smi 0 if the non-zero one was positive.
2136 // We fall through here if we multiplied a negative number with 0, because
2137 // that would mean we should produce -0.
2138 break;
2139 case Token::DIV:
2140 // Check for power of two on the right hand side.
2141 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
2142 // Check for positive and no remainder (scratch1 contains right - 1).
2143 __ orr(scratch2, scratch1, Operand(0x80000000u));
2144 __ tst(left, scratch2);
2145 __ b(ne, &not_smi_result);
2146
2147 // Perform division by shifting.
2148 __ CountLeadingZeros(scratch1, scratch1, scratch2);
2149 __ rsb(scratch1, scratch1, Operand(31));
2150 __ mov(right, Operand(left, LSR, scratch1));
2151 __ Ret();
2152 break;
2153 case Token::MOD:
2154 // Check for two positive smis.
2155 __ orr(scratch1, left, Operand(right));
2156 __ tst(scratch1, Operand(0x80000000u | kSmiTagMask));
2157 __ b(ne, &not_smi_result);
2158
2159 // Check for power of two on the right hand side.
2160 __ JumpIfNotPowerOfTwoOrZero(right, scratch1, &not_smi_result);
2161
2162 // Perform modulus by masking.
2163 __ and_(right, left, Operand(scratch1));
2164 __ Ret();
2165 break;
2166 case Token::BIT_OR:
2167 __ orr(right, left, Operand(right));
2168 __ Ret();
2169 break;
2170 case Token::BIT_AND:
2171 __ and_(right, left, Operand(right));
2172 __ Ret();
2173 break;
2174 case Token::BIT_XOR:
2175 __ eor(right, left, Operand(right));
2176 __ Ret();
2177 break;
2178 case Token::SAR:
2179 // Remove tags from right operand.
2180 __ GetLeastBitsFromSmi(scratch1, right, 5);
2181 __ mov(right, Operand(left, ASR, scratch1));
2182 // Smi tag result.
2183 __ bic(right, right, Operand(kSmiTagMask));
2184 __ Ret();
2185 break;
2186 case Token::SHR:
2187 // Remove tags from operands. We can't do this on a 31 bit number
2188 // because then the 0s get shifted into bit 30 instead of bit 31.
2189 __ SmiUntag(scratch1, left);
2190 __ GetLeastBitsFromSmi(scratch2, right, 5);
2191 __ mov(scratch1, Operand(scratch1, LSR, scratch2));
2192 // Unsigned shift is not allowed to produce a negative number, so
2193 // check the sign bit and the sign bit after Smi tagging.
2194 __ tst(scratch1, Operand(0xc0000000));
2195 __ b(ne, &not_smi_result);
2196 // Smi tag result.
2197 __ SmiTag(right, scratch1);
2198 __ Ret();
2199 break;
2200 case Token::SHL:
2201 // Remove tags from operands.
2202 __ SmiUntag(scratch1, left);
2203 __ GetLeastBitsFromSmi(scratch2, right, 5);
2204 __ mov(scratch1, Operand(scratch1, LSL, scratch2));
2205 // Check that the signed result fits in a Smi.
2206 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
2207 __ b(mi, &not_smi_result);
2208 __ SmiTag(right, scratch1);
2209 __ Ret();
2210 break;
2211 default:
2212 UNREACHABLE();
2213 }
2214 __ bind(&not_smi_result);
2215}
2216
2217
Ben Murdoch257744e2011-11-30 15:57:28 +00002218void BinaryOpStub::GenerateFPOperation(MacroAssembler* masm,
2219 bool smi_operands,
2220 Label* not_numbers,
2221 Label* gc_required) {
Steve Block1e0659c2011-05-24 12:43:12 +01002222 Register left = r1;
2223 Register right = r0;
2224 Register scratch1 = r7;
2225 Register scratch2 = r9;
Steve Block44f0eee2011-05-26 01:26:41 +01002226 Register scratch3 = r4;
Steve Block1e0659c2011-05-24 12:43:12 +01002227
2228 ASSERT(smi_operands || (not_numbers != NULL));
2229 if (smi_operands && FLAG_debug_code) {
2230 __ AbortIfNotSmi(left);
2231 __ AbortIfNotSmi(right);
2232 }
2233
2234 Register heap_number_map = r6;
2235 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2236
2237 switch (op_) {
2238 case Token::ADD:
2239 case Token::SUB:
2240 case Token::MUL:
2241 case Token::DIV:
2242 case Token::MOD: {
2243 // Load left and right operands into d6 and d7 or r0/r1 and r2/r3
2244 // depending on whether VFP3 is available or not.
2245 FloatingPointHelper::Destination destination =
Ben Murdoch8b112d22011-06-08 16:22:53 +01002246 CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +01002247 op_ != Token::MOD ?
Steve Block1e0659c2011-05-24 12:43:12 +01002248 FloatingPointHelper::kVFPRegisters :
2249 FloatingPointHelper::kCoreRegisters;
2250
2251 // Allocate new heap number for result.
2252 Register result = r5;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002253 GenerateHeapResultAllocation(
2254 masm, result, heap_number_map, scratch1, scratch2, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01002255
2256 // Load the operands.
2257 if (smi_operands) {
2258 FloatingPointHelper::LoadSmis(masm, destination, scratch1, scratch2);
2259 } else {
2260 FloatingPointHelper::LoadOperands(masm,
2261 destination,
2262 heap_number_map,
2263 scratch1,
2264 scratch2,
2265 not_numbers);
2266 }
2267
2268 // Calculate the result.
2269 if (destination == FloatingPointHelper::kVFPRegisters) {
2270 // Using VFP registers:
2271 // d6: Left value
2272 // d7: Right value
2273 CpuFeatures::Scope scope(VFP3);
2274 switch (op_) {
2275 case Token::ADD:
2276 __ vadd(d5, d6, d7);
2277 break;
2278 case Token::SUB:
2279 __ vsub(d5, d6, d7);
2280 break;
2281 case Token::MUL:
2282 __ vmul(d5, d6, d7);
2283 break;
2284 case Token::DIV:
2285 __ vdiv(d5, d6, d7);
2286 break;
2287 default:
2288 UNREACHABLE();
2289 }
2290
2291 __ sub(r0, result, Operand(kHeapObjectTag));
2292 __ vstr(d5, r0, HeapNumber::kValueOffset);
2293 __ add(r0, r0, Operand(kHeapObjectTag));
2294 __ Ret();
2295 } else {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002296 // Call the C function to handle the double operation.
2297 FloatingPointHelper::CallCCodeForDoubleOperation(masm,
2298 op_,
2299 result,
2300 scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002301 if (FLAG_debug_code) {
2302 __ stop("Unreachable code.");
2303 }
Steve Block1e0659c2011-05-24 12:43:12 +01002304 }
2305 break;
2306 }
2307 case Token::BIT_OR:
2308 case Token::BIT_XOR:
2309 case Token::BIT_AND:
2310 case Token::SAR:
2311 case Token::SHR:
2312 case Token::SHL: {
2313 if (smi_operands) {
2314 __ SmiUntag(r3, left);
2315 __ SmiUntag(r2, right);
2316 } else {
2317 // Convert operands to 32-bit integers. Right in r2 and left in r3.
Steve Block44f0eee2011-05-26 01:26:41 +01002318 FloatingPointHelper::ConvertNumberToInt32(masm,
2319 left,
2320 r3,
2321 heap_number_map,
2322 scratch1,
2323 scratch2,
2324 scratch3,
2325 d0,
2326 not_numbers);
2327 FloatingPointHelper::ConvertNumberToInt32(masm,
2328 right,
2329 r2,
2330 heap_number_map,
2331 scratch1,
2332 scratch2,
2333 scratch3,
2334 d0,
2335 not_numbers);
Steve Block1e0659c2011-05-24 12:43:12 +01002336 }
2337
2338 Label result_not_a_smi;
2339 switch (op_) {
2340 case Token::BIT_OR:
2341 __ orr(r2, r3, Operand(r2));
2342 break;
2343 case Token::BIT_XOR:
2344 __ eor(r2, r3, Operand(r2));
2345 break;
2346 case Token::BIT_AND:
2347 __ and_(r2, r3, Operand(r2));
2348 break;
2349 case Token::SAR:
2350 // Use only the 5 least significant bits of the shift count.
Steve Block1e0659c2011-05-24 12:43:12 +01002351 __ GetLeastBitsFromInt32(r2, r2, 5);
2352 __ mov(r2, Operand(r3, ASR, r2));
2353 break;
2354 case Token::SHR:
2355 // Use only the 5 least significant bits of the shift count.
2356 __ GetLeastBitsFromInt32(r2, r2, 5);
2357 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2358 // SHR is special because it is required to produce a positive answer.
2359 // The code below for writing into heap numbers isn't capable of
2360 // writing the register as an unsigned int so we go to slow case if we
2361 // hit this case.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002362 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002363 __ b(mi, &result_not_a_smi);
2364 } else {
2365 __ b(mi, not_numbers);
2366 }
2367 break;
2368 case Token::SHL:
2369 // Use only the 5 least significant bits of the shift count.
2370 __ GetLeastBitsFromInt32(r2, r2, 5);
2371 __ mov(r2, Operand(r3, LSL, r2));
2372 break;
2373 default:
2374 UNREACHABLE();
2375 }
2376
2377 // Check that the *signed* result fits in a smi.
2378 __ add(r3, r2, Operand(0x40000000), SetCC);
2379 __ b(mi, &result_not_a_smi);
2380 __ SmiTag(r0, r2);
2381 __ Ret();
2382
2383 // Allocate new heap number for result.
2384 __ bind(&result_not_a_smi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002385 Register result = r5;
2386 if (smi_operands) {
2387 __ AllocateHeapNumber(
2388 result, scratch1, scratch2, heap_number_map, gc_required);
2389 } else {
2390 GenerateHeapResultAllocation(
2391 masm, result, heap_number_map, scratch1, scratch2, gc_required);
2392 }
Steve Block1e0659c2011-05-24 12:43:12 +01002393
2394 // r2: Answer as signed int32.
2395 // r5: Heap number to write answer into.
2396
2397 // Nothing can go wrong now, so move the heap number to r0, which is the
2398 // result.
2399 __ mov(r0, Operand(r5));
2400
Ben Murdoch8b112d22011-06-08 16:22:53 +01002401 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block1e0659c2011-05-24 12:43:12 +01002402 // Convert the int32 in r2 to the heap number in r0. r3 is corrupted. As
2403 // mentioned above SHR needs to always produce a positive result.
2404 CpuFeatures::Scope scope(VFP3);
2405 __ vmov(s0, r2);
2406 if (op_ == Token::SHR) {
2407 __ vcvt_f64_u32(d0, s0);
2408 } else {
2409 __ vcvt_f64_s32(d0, s0);
2410 }
2411 __ sub(r3, r0, Operand(kHeapObjectTag));
2412 __ vstr(d0, r3, HeapNumber::kValueOffset);
2413 __ Ret();
2414 } else {
2415 // Tail call that writes the int32 in r2 to the heap number in r0, using
2416 // r3 as scratch. r0 is preserved and returned.
2417 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
2418 __ TailCallStub(&stub);
2419 }
2420 break;
2421 }
2422 default:
2423 UNREACHABLE();
2424 }
2425}
2426
2427
2428// Generate the smi code. If the operation on smis are successful this return is
2429// generated. If the result is not a smi and heap number allocation is not
2430// requested the code falls through. If number allocation is requested but a
2431// heap number cannot be allocated the code jumps to the lable gc_required.
Ben Murdoch257744e2011-11-30 15:57:28 +00002432void BinaryOpStub::GenerateSmiCode(
2433 MacroAssembler* masm,
Ben Murdoch8b112d22011-06-08 16:22:53 +01002434 Label* use_runtime,
Steve Block1e0659c2011-05-24 12:43:12 +01002435 Label* gc_required,
2436 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
2437 Label not_smis;
2438
2439 Register left = r1;
2440 Register right = r0;
2441 Register scratch1 = r7;
2442 Register scratch2 = r9;
2443
2444 // Perform combined smi check on both operands.
2445 __ orr(scratch1, left, Operand(right));
2446 STATIC_ASSERT(kSmiTag == 0);
2447 __ tst(scratch1, Operand(kSmiTagMask));
2448 __ b(ne, &not_smis);
2449
2450 // If the smi-smi operation results in a smi return is generated.
2451 GenerateSmiSmiOperation(masm);
2452
2453 // If heap number results are possible generate the result in an allocated
2454 // heap number.
2455 if (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01002456 GenerateFPOperation(masm, true, use_runtime, gc_required);
Steve Block1e0659c2011-05-24 12:43:12 +01002457 }
2458 __ bind(&not_smis);
2459}
2460
2461
Ben Murdoch257744e2011-11-30 15:57:28 +00002462void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002463 Label not_smis, call_runtime;
2464
Ben Murdoch257744e2011-11-30 15:57:28 +00002465 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
2466 result_type_ == BinaryOpIC::SMI) {
Steve Block1e0659c2011-05-24 12:43:12 +01002467 // Only allow smi results.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002468 GenerateSmiCode(masm, &call_runtime, NULL, NO_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002469 } else {
2470 // Allow heap number result and don't make a transition if a heap number
2471 // cannot be allocated.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002472 GenerateSmiCode(masm,
2473 &call_runtime,
2474 &call_runtime,
2475 ALLOW_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002476 }
2477
2478 // Code falls through if the result is not returned as either a smi or heap
2479 // number.
2480 GenerateTypeTransition(masm);
2481
2482 __ bind(&call_runtime);
2483 GenerateCallRuntime(masm);
2484}
2485
2486
Ben Murdoch257744e2011-11-30 15:57:28 +00002487void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
2488 ASSERT(operands_type_ == BinaryOpIC::STRING);
Steve Block1e0659c2011-05-24 12:43:12 +01002489 ASSERT(op_ == Token::ADD);
2490 // Try to add arguments as strings, otherwise, transition to the generic
Ben Murdoch257744e2011-11-30 15:57:28 +00002491 // BinaryOpIC type.
Steve Block1e0659c2011-05-24 12:43:12 +01002492 GenerateAddStrings(masm);
2493 GenerateTypeTransition(masm);
2494}
2495
2496
Ben Murdoch257744e2011-11-30 15:57:28 +00002497void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
2498 Label call_runtime;
2499 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
2500 ASSERT(op_ == Token::ADD);
2501 // If both arguments are strings, call the string add stub.
2502 // Otherwise, do a transition.
2503
2504 // Registers containing left and right operands respectively.
2505 Register left = r1;
2506 Register right = r0;
2507
2508 // Test if left operand is a string.
2509 __ JumpIfSmi(left, &call_runtime);
2510 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
2511 __ b(ge, &call_runtime);
2512
2513 // Test if right operand is a string.
2514 __ JumpIfSmi(right, &call_runtime);
2515 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
2516 __ b(ge, &call_runtime);
2517
2518 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
2519 GenerateRegisterArgsPush(masm);
2520 __ TailCallStub(&string_add_stub);
2521
2522 __ bind(&call_runtime);
2523 GenerateTypeTransition(masm);
2524}
2525
2526
2527void BinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
2528 ASSERT(operands_type_ == BinaryOpIC::INT32);
Steve Block1e0659c2011-05-24 12:43:12 +01002529
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002530 Register left = r1;
2531 Register right = r0;
2532 Register scratch1 = r7;
2533 Register scratch2 = r9;
2534 DwVfpRegister double_scratch = d0;
2535 SwVfpRegister single_scratch = s3;
2536
2537 Register heap_number_result = no_reg;
2538 Register heap_number_map = r6;
2539 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2540
2541 Label call_runtime;
2542 // Labels for type transition, used for wrong input or output types.
2543 // Both label are currently actually bound to the same position. We use two
2544 // different label to differentiate the cause leading to type transition.
2545 Label transition;
2546
2547 // Smi-smi fast case.
2548 Label skip;
2549 __ orr(scratch1, left, right);
2550 __ JumpIfNotSmi(scratch1, &skip);
2551 GenerateSmiSmiOperation(masm);
2552 // Fall through if the result is not a smi.
2553 __ bind(&skip);
2554
2555 switch (op_) {
2556 case Token::ADD:
2557 case Token::SUB:
2558 case Token::MUL:
2559 case Token::DIV:
2560 case Token::MOD: {
2561 // Load both operands and check that they are 32-bit integer.
2562 // Jump to type transition if they are not. The registers r0 and r1 (right
2563 // and left) are preserved for the runtime call.
2564 FloatingPointHelper::Destination destination =
Ben Murdoch8b112d22011-06-08 16:22:53 +01002565 CpuFeatures::IsSupported(VFP3) &&
Steve Block44f0eee2011-05-26 01:26:41 +01002566 op_ != Token::MOD ?
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002567 FloatingPointHelper::kVFPRegisters :
2568 FloatingPointHelper::kCoreRegisters;
2569
2570 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2571 right,
2572 destination,
2573 d7,
2574 r2,
2575 r3,
2576 heap_number_map,
2577 scratch1,
2578 scratch2,
2579 s0,
2580 &transition);
2581 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2582 left,
2583 destination,
2584 d6,
2585 r4,
2586 r5,
2587 heap_number_map,
2588 scratch1,
2589 scratch2,
2590 s0,
2591 &transition);
2592
2593 if (destination == FloatingPointHelper::kVFPRegisters) {
2594 CpuFeatures::Scope scope(VFP3);
2595 Label return_heap_number;
2596 switch (op_) {
2597 case Token::ADD:
2598 __ vadd(d5, d6, d7);
2599 break;
2600 case Token::SUB:
2601 __ vsub(d5, d6, d7);
2602 break;
2603 case Token::MUL:
2604 __ vmul(d5, d6, d7);
2605 break;
2606 case Token::DIV:
2607 __ vdiv(d5, d6, d7);
2608 break;
2609 default:
2610 UNREACHABLE();
2611 }
2612
2613 if (op_ != Token::DIV) {
2614 // These operations produce an integer result.
2615 // Try to return a smi if we can.
2616 // Otherwise return a heap number if allowed, or jump to type
2617 // transition.
2618
2619 __ EmitVFPTruncate(kRoundToZero,
2620 single_scratch,
2621 d5,
2622 scratch1,
2623 scratch2);
2624
Ben Murdoch257744e2011-11-30 15:57:28 +00002625 if (result_type_ <= BinaryOpIC::INT32) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002626 // If the ne condition is set, result does
2627 // not fit in a 32-bit integer.
2628 __ b(ne, &transition);
2629 }
2630
2631 // Check if the result fits in a smi.
2632 __ vmov(scratch1, single_scratch);
2633 __ add(scratch2, scratch1, Operand(0x40000000), SetCC);
2634 // If not try to return a heap number.
2635 __ b(mi, &return_heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +01002636 // Check for minus zero. Return heap number for minus zero.
2637 Label not_zero;
2638 __ cmp(scratch1, Operand(0));
2639 __ b(ne, &not_zero);
2640 __ vmov(scratch2, d5.high());
2641 __ tst(scratch2, Operand(HeapNumber::kSignMask));
2642 __ b(ne, &return_heap_number);
2643 __ bind(&not_zero);
2644
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002645 // Tag the result and return.
2646 __ SmiTag(r0, scratch1);
2647 __ Ret();
Steve Block44f0eee2011-05-26 01:26:41 +01002648 } else {
2649 // DIV just falls through to allocating a heap number.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002650 }
2651
Ben Murdoch257744e2011-11-30 15:57:28 +00002652 if (result_type_ >= (op_ == Token::DIV) ? BinaryOpIC::HEAP_NUMBER
2653 : BinaryOpIC::INT32) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002654 __ bind(&return_heap_number);
2655 // We are using vfp registers so r5 is available.
2656 heap_number_result = r5;
2657 GenerateHeapResultAllocation(masm,
2658 heap_number_result,
2659 heap_number_map,
2660 scratch1,
2661 scratch2,
2662 &call_runtime);
2663 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
2664 __ vstr(d5, r0, HeapNumber::kValueOffset);
2665 __ mov(r0, heap_number_result);
2666 __ Ret();
2667 }
2668
2669 // A DIV operation expecting an integer result falls through
2670 // to type transition.
2671
2672 } else {
2673 // We preserved r0 and r1 to be able to call runtime.
2674 // Save the left value on the stack.
2675 __ Push(r5, r4);
2676
Steve Block053d10c2011-06-13 19:13:29 +01002677 Label pop_and_call_runtime;
2678
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002679 // Allocate a heap number to store the result.
2680 heap_number_result = r5;
2681 GenerateHeapResultAllocation(masm,
2682 heap_number_result,
2683 heap_number_map,
2684 scratch1,
2685 scratch2,
Steve Block053d10c2011-06-13 19:13:29 +01002686 &pop_and_call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002687
2688 // Load the left value from the value saved on the stack.
2689 __ Pop(r1, r0);
2690
2691 // Call the C function to handle the double operation.
2692 FloatingPointHelper::CallCCodeForDoubleOperation(
2693 masm, op_, heap_number_result, scratch1);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002694 if (FLAG_debug_code) {
2695 __ stop("Unreachable code.");
2696 }
Steve Block053d10c2011-06-13 19:13:29 +01002697
2698 __ bind(&pop_and_call_runtime);
2699 __ Drop(2);
2700 __ b(&call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002701 }
2702
2703 break;
2704 }
2705
2706 case Token::BIT_OR:
2707 case Token::BIT_XOR:
2708 case Token::BIT_AND:
2709 case Token::SAR:
2710 case Token::SHR:
2711 case Token::SHL: {
2712 Label return_heap_number;
2713 Register scratch3 = r5;
2714 // Convert operands to 32-bit integers. Right in r2 and left in r3. The
2715 // registers r0 and r1 (right and left) are preserved for the runtime
2716 // call.
2717 FloatingPointHelper::LoadNumberAsInt32(masm,
2718 left,
2719 r3,
2720 heap_number_map,
2721 scratch1,
2722 scratch2,
2723 scratch3,
2724 d0,
2725 &transition);
2726 FloatingPointHelper::LoadNumberAsInt32(masm,
2727 right,
2728 r2,
2729 heap_number_map,
2730 scratch1,
2731 scratch2,
2732 scratch3,
2733 d0,
2734 &transition);
2735
2736 // The ECMA-262 standard specifies that, for shift operations, only the
2737 // 5 least significant bits of the shift value should be used.
2738 switch (op_) {
2739 case Token::BIT_OR:
2740 __ orr(r2, r3, Operand(r2));
2741 break;
2742 case Token::BIT_XOR:
2743 __ eor(r2, r3, Operand(r2));
2744 break;
2745 case Token::BIT_AND:
2746 __ and_(r2, r3, Operand(r2));
2747 break;
2748 case Token::SAR:
2749 __ and_(r2, r2, Operand(0x1f));
2750 __ mov(r2, Operand(r3, ASR, r2));
2751 break;
2752 case Token::SHR:
2753 __ and_(r2, r2, Operand(0x1f));
2754 __ mov(r2, Operand(r3, LSR, r2), SetCC);
2755 // SHR is special because it is required to produce a positive answer.
2756 // We only get a negative result if the shift value (r2) is 0.
2757 // This result cannot be respresented as a signed 32-bit integer, try
2758 // to return a heap number if we can.
2759 // The non vfp3 code does not support this special case, so jump to
2760 // runtime if we don't support it.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002761 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002762 __ b(mi, (result_type_ <= BinaryOpIC::INT32)
2763 ? &transition
2764 : &return_heap_number);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002765 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002766 __ b(mi, (result_type_ <= BinaryOpIC::INT32)
2767 ? &transition
2768 : &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002769 }
2770 break;
2771 case Token::SHL:
2772 __ and_(r2, r2, Operand(0x1f));
2773 __ mov(r2, Operand(r3, LSL, r2));
2774 break;
2775 default:
2776 UNREACHABLE();
2777 }
2778
2779 // Check if the result fits in a smi.
2780 __ add(scratch1, r2, Operand(0x40000000), SetCC);
2781 // If not try to return a heap number. (We know the result is an int32.)
2782 __ b(mi, &return_heap_number);
2783 // Tag the result and return.
2784 __ SmiTag(r0, r2);
2785 __ Ret();
2786
2787 __ bind(&return_heap_number);
Ben Murdoch8b112d22011-06-08 16:22:53 +01002788 heap_number_result = r5;
2789 GenerateHeapResultAllocation(masm,
2790 heap_number_result,
2791 heap_number_map,
2792 scratch1,
2793 scratch2,
2794 &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002795
Ben Murdoch8b112d22011-06-08 16:22:53 +01002796 if (CpuFeatures::IsSupported(VFP3)) {
2797 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002798 if (op_ != Token::SHR) {
2799 // Convert the result to a floating point value.
2800 __ vmov(double_scratch.low(), r2);
2801 __ vcvt_f64_s32(double_scratch, double_scratch.low());
2802 } else {
2803 // The result must be interpreted as an unsigned 32-bit integer.
2804 __ vmov(double_scratch.low(), r2);
2805 __ vcvt_f64_u32(double_scratch, double_scratch.low());
2806 }
2807
2808 // Store the result.
2809 __ sub(r0, heap_number_result, Operand(kHeapObjectTag));
2810 __ vstr(double_scratch, r0, HeapNumber::kValueOffset);
2811 __ mov(r0, heap_number_result);
2812 __ Ret();
2813 } else {
2814 // Tail call that writes the int32 in r2 to the heap number in r0, using
2815 // r3 as scratch. r0 is preserved and returned.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002816 __ mov(r0, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002817 WriteInt32ToHeapNumberStub stub(r2, r0, r3);
2818 __ TailCallStub(&stub);
2819 }
2820
2821 break;
2822 }
2823
2824 default:
2825 UNREACHABLE();
2826 }
2827
2828 if (transition.is_linked()) {
2829 __ bind(&transition);
2830 GenerateTypeTransition(masm);
2831 }
2832
2833 __ bind(&call_runtime);
2834 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01002835}
2836
2837
Ben Murdoch257744e2011-11-30 15:57:28 +00002838void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01002839 Label call_runtime;
2840
2841 if (op_ == Token::ADD) {
2842 // Handle string addition here, because it is the only operation
2843 // that does not do a ToNumber conversion on the operands.
2844 GenerateAddStrings(masm);
2845 }
2846
2847 // Convert oddball arguments to numbers.
2848 Label check, done;
2849 __ CompareRoot(r1, Heap::kUndefinedValueRootIndex);
2850 __ b(ne, &check);
2851 if (Token::IsBitOp(op_)) {
2852 __ mov(r1, Operand(Smi::FromInt(0)));
2853 } else {
2854 __ LoadRoot(r1, Heap::kNanValueRootIndex);
2855 }
2856 __ jmp(&done);
2857 __ bind(&check);
2858 __ CompareRoot(r0, Heap::kUndefinedValueRootIndex);
2859 __ b(ne, &done);
2860 if (Token::IsBitOp(op_)) {
2861 __ mov(r0, Operand(Smi::FromInt(0)));
2862 } else {
2863 __ LoadRoot(r0, Heap::kNanValueRootIndex);
2864 }
2865 __ bind(&done);
2866
2867 GenerateHeapNumberStub(masm);
2868}
2869
2870
Ben Murdoch257744e2011-11-30 15:57:28 +00002871void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01002872 Label call_runtime;
2873 GenerateFPOperation(masm, false, &call_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002874
2875 __ bind(&call_runtime);
2876 GenerateCallRuntime(masm);
2877}
2878
2879
Ben Murdoch257744e2011-11-30 15:57:28 +00002880void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002881 Label call_runtime, call_string_add_or_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01002882
Ben Murdoch8b112d22011-06-08 16:22:53 +01002883 GenerateSmiCode(masm, &call_runtime, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
Steve Block1e0659c2011-05-24 12:43:12 +01002884
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002885 GenerateFPOperation(masm, false, &call_string_add_or_runtime, &call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002886
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002887 __ bind(&call_string_add_or_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01002888 if (op_ == Token::ADD) {
2889 GenerateAddStrings(masm);
2890 }
2891
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002892 __ bind(&call_runtime);
2893 GenerateCallRuntime(masm);
Steve Block1e0659c2011-05-24 12:43:12 +01002894}
2895
2896
Ben Murdoch257744e2011-11-30 15:57:28 +00002897void BinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002898 ASSERT(op_ == Token::ADD);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002899 Label left_not_string, call_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01002900
2901 Register left = r1;
2902 Register right = r0;
Steve Block1e0659c2011-05-24 12:43:12 +01002903
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002904 // Check if left argument is a string.
2905 __ JumpIfSmi(left, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002906 __ CompareObjectType(left, r2, r2, FIRST_NONSTRING_TYPE);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002907 __ b(ge, &left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002908
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002909 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2910 GenerateRegisterArgsPush(masm);
2911 __ TailCallStub(&string_add_left_stub);
2912
2913 // Left operand is not a string, test right.
2914 __ bind(&left_not_string);
Steve Block1e0659c2011-05-24 12:43:12 +01002915 __ JumpIfSmi(right, &call_runtime);
2916 __ CompareObjectType(right, r2, r2, FIRST_NONSTRING_TYPE);
2917 __ b(ge, &call_runtime);
2918
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002919 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
Steve Block1e0659c2011-05-24 12:43:12 +01002920 GenerateRegisterArgsPush(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002921 __ TailCallStub(&string_add_right_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01002922
2923 // At least one argument is not a string.
2924 __ bind(&call_runtime);
2925}
2926
2927
Ben Murdoch257744e2011-11-30 15:57:28 +00002928void BinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01002929 GenerateRegisterArgsPush(masm);
2930 switch (op_) {
2931 case Token::ADD:
Ben Murdoch257744e2011-11-30 15:57:28 +00002932 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002933 break;
2934 case Token::SUB:
Ben Murdoch257744e2011-11-30 15:57:28 +00002935 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002936 break;
2937 case Token::MUL:
Ben Murdoch257744e2011-11-30 15:57:28 +00002938 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002939 break;
2940 case Token::DIV:
Ben Murdoch257744e2011-11-30 15:57:28 +00002941 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002942 break;
2943 case Token::MOD:
Ben Murdoch257744e2011-11-30 15:57:28 +00002944 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002945 break;
2946 case Token::BIT_OR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002947 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002948 break;
2949 case Token::BIT_AND:
Ben Murdoch257744e2011-11-30 15:57:28 +00002950 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002951 break;
2952 case Token::BIT_XOR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002953 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002954 break;
2955 case Token::SAR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002956 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002957 break;
2958 case Token::SHR:
Ben Murdoch257744e2011-11-30 15:57:28 +00002959 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002960 break;
2961 case Token::SHL:
Ben Murdoch257744e2011-11-30 15:57:28 +00002962 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01002963 break;
2964 default:
2965 UNREACHABLE();
2966 }
2967}
2968
2969
Ben Murdoch257744e2011-11-30 15:57:28 +00002970void BinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
2971 Register result,
2972 Register heap_number_map,
2973 Register scratch1,
2974 Register scratch2,
2975 Label* gc_required) {
Steve Block1e0659c2011-05-24 12:43:12 +01002976 // Code below will scratch result if allocation fails. To keep both arguments
2977 // intact for the runtime call result cannot be one of these.
2978 ASSERT(!result.is(r0) && !result.is(r1));
2979
2980 if (mode_ == OVERWRITE_LEFT || mode_ == OVERWRITE_RIGHT) {
2981 Label skip_allocation, allocated;
2982 Register overwritable_operand = mode_ == OVERWRITE_LEFT ? r1 : r0;
2983 // If the overwritable operand is already an object, we skip the
2984 // allocation of a heap number.
2985 __ JumpIfNotSmi(overwritable_operand, &skip_allocation);
2986 // Allocate a heap number for the result.
2987 __ AllocateHeapNumber(
2988 result, scratch1, scratch2, heap_number_map, gc_required);
2989 __ b(&allocated);
2990 __ bind(&skip_allocation);
2991 // Use object holding the overwritable operand for result.
2992 __ mov(result, Operand(overwritable_operand));
2993 __ bind(&allocated);
2994 } else {
2995 ASSERT(mode_ == NO_OVERWRITE);
2996 __ AllocateHeapNumber(
2997 result, scratch1, scratch2, heap_number_map, gc_required);
2998 }
2999}
3000
3001
Ben Murdoch257744e2011-11-30 15:57:28 +00003002void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01003003 __ Push(r1, r0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003004}
3005
3006
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003007void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003008 // Untagged case: double input in d2, double result goes
3009 // into d2.
3010 // Tagged case: tagged input on top of stack and in r0,
3011 // tagged result (heap number) goes into r0.
3012
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003013 Label input_not_smi;
3014 Label loaded;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003015 Label calculate;
3016 Label invalid_cache;
3017 const Register scratch0 = r9;
3018 const Register scratch1 = r7;
3019 const Register cache_entry = r0;
3020 const bool tagged = (argument_type_ == TAGGED);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003021
Ben Murdoch8b112d22011-06-08 16:22:53 +01003022 if (CpuFeatures::IsSupported(VFP3)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003023 CpuFeatures::Scope scope(VFP3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003024 if (tagged) {
3025 // Argument is a number and is on stack and in r0.
3026 // Load argument and check if it is a smi.
3027 __ JumpIfNotSmi(r0, &input_not_smi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003028
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003029 // Input is a smi. Convert to double and load the low and high words
3030 // of the double into r2, r3.
3031 __ IntegerToDoubleConversionWithVFP3(r0, r3, r2);
3032 __ b(&loaded);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003033
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003034 __ bind(&input_not_smi);
3035 // Check if input is a HeapNumber.
3036 __ CheckMap(r0,
3037 r1,
3038 Heap::kHeapNumberMapRootIndex,
3039 &calculate,
Ben Murdoch257744e2011-11-30 15:57:28 +00003040 DONT_DO_SMI_CHECK);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003041 // Input is a HeapNumber. Load it to a double register and store the
3042 // low and high words into r2, r3.
3043 __ vldr(d0, FieldMemOperand(r0, HeapNumber::kValueOffset));
3044 __ vmov(r2, r3, d0);
3045 } else {
3046 // Input is untagged double in d2. Output goes to d2.
3047 __ vmov(r2, r3, d2);
3048 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003049 __ bind(&loaded);
3050 // r2 = low 32 bits of double value
3051 // r3 = high 32 bits of double value
3052 // Compute hash (the shifts are arithmetic):
3053 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
3054 __ eor(r1, r2, Operand(r3));
3055 __ eor(r1, r1, Operand(r1, ASR, 16));
3056 __ eor(r1, r1, Operand(r1, ASR, 8));
Steve Block44f0eee2011-05-26 01:26:41 +01003057 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
3058 __ And(r1, r1, Operand(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003059
3060 // r2 = low 32 bits of double value.
3061 // r3 = high 32 bits of double value.
3062 // r1 = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +01003063 Isolate* isolate = masm->isolate();
3064 ExternalReference cache_array =
3065 ExternalReference::transcendental_cache_array_address(isolate);
3066 __ mov(cache_entry, Operand(cache_array));
3067 // cache_entry points to cache array.
3068 int cache_array_index
3069 = type_ * sizeof(isolate->transcendental_cache()->caches_[0]);
3070 __ ldr(cache_entry, MemOperand(cache_entry, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003071 // r0 points to the cache for the type type_.
3072 // If NULL, the cache hasn't been initialized yet, so go through runtime.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003073 __ cmp(cache_entry, Operand(0, RelocInfo::NONE));
3074 __ b(eq, &invalid_cache);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003075
3076#ifdef DEBUG
3077 // Check that the layout of cache elements match expectations.
Steve Block44f0eee2011-05-26 01:26:41 +01003078 { TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003079 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
3080 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
3081 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
3082 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
3083 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
3084 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
3085 CHECK_EQ(0, elem_in0 - elem_start);
3086 CHECK_EQ(kIntSize, elem_in1 - elem_start);
3087 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
3088 }
3089#endif
3090
3091 // Find the address of the r1'st entry in the cache, i.e., &r0[r1*12].
3092 __ add(r1, r1, Operand(r1, LSL, 1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003093 __ add(cache_entry, cache_entry, Operand(r1, LSL, 2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003094 // Check if cache matches: Double value is stored in uint32_t[2] array.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003095 __ ldm(ia, cache_entry, r4.bit() | r5.bit() | r6.bit());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003096 __ cmp(r2, r4);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003097 __ b(ne, &calculate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003098 __ cmp(r3, r5);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003099 __ b(ne, &calculate);
3100 // Cache hit. Load result, cleanup and return.
3101 if (tagged) {
3102 // Pop input value from stack and load result into r0.
3103 __ pop();
3104 __ mov(r0, Operand(r6));
3105 } else {
3106 // Load result into d2.
3107 __ vldr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
3108 }
3109 __ Ret();
Ben Murdoch8b112d22011-06-08 16:22:53 +01003110 } // if (CpuFeatures::IsSupported(VFP3))
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003111
3112 __ bind(&calculate);
3113 if (tagged) {
3114 __ bind(&invalid_cache);
Steve Block44f0eee2011-05-26 01:26:41 +01003115 ExternalReference runtime_function =
3116 ExternalReference(RuntimeFunction(), masm->isolate());
3117 __ TailCallExternalReference(runtime_function, 1, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003118 } else {
Ben Murdoch8b112d22011-06-08 16:22:53 +01003119 if (!CpuFeatures::IsSupported(VFP3)) UNREACHABLE();
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003120 CpuFeatures::Scope scope(VFP3);
3121
3122 Label no_update;
3123 Label skip_cache;
3124 const Register heap_number_map = r5;
3125
3126 // Call C function to calculate the result and update the cache.
3127 // Register r0 holds precalculated cache entry address; preserve
3128 // it on the stack and pop it into register cache_entry after the
3129 // call.
3130 __ push(cache_entry);
3131 GenerateCallCFunction(masm, scratch0);
3132 __ GetCFunctionDoubleResult(d2);
3133
3134 // Try to update the cache. If we cannot allocate a
3135 // heap number, we return the result without updating.
3136 __ pop(cache_entry);
3137 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
3138 __ AllocateHeapNumber(r6, scratch0, scratch1, r5, &no_update);
3139 __ vstr(d2, FieldMemOperand(r6, HeapNumber::kValueOffset));
3140 __ stm(ia, cache_entry, r2.bit() | r3.bit() | r6.bit());
3141 __ Ret();
3142
3143 __ bind(&invalid_cache);
3144 // The cache is invalid. Call runtime which will recreate the
3145 // cache.
3146 __ LoadRoot(r5, Heap::kHeapNumberMapRootIndex);
3147 __ AllocateHeapNumber(r0, scratch0, scratch1, r5, &skip_cache);
3148 __ vstr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
3149 __ EnterInternalFrame();
3150 __ push(r0);
3151 __ CallRuntime(RuntimeFunction(), 1);
3152 __ LeaveInternalFrame();
3153 __ vldr(d2, FieldMemOperand(r0, HeapNumber::kValueOffset));
3154 __ Ret();
3155
3156 __ bind(&skip_cache);
3157 // Call C function to calculate the result and answer directly
3158 // without updating the cache.
3159 GenerateCallCFunction(masm, scratch0);
3160 __ GetCFunctionDoubleResult(d2);
3161 __ bind(&no_update);
3162
3163 // We return the value in d2 without adding it to the cache, but
3164 // we cause a scavenging GC so that future allocations will succeed.
3165 __ EnterInternalFrame();
3166
3167 // Allocate an aligned object larger than a HeapNumber.
3168 ASSERT(4 * kPointerSize >= HeapNumber::kSize);
3169 __ mov(scratch0, Operand(4 * kPointerSize));
3170 __ push(scratch0);
3171 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
3172 __ LeaveInternalFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003173 __ Ret();
3174 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003175}
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003176
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003177
3178void TranscendentalCacheStub::GenerateCallCFunction(MacroAssembler* masm,
3179 Register scratch) {
Steve Block44f0eee2011-05-26 01:26:41 +01003180 Isolate* isolate = masm->isolate();
3181
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003182 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003183 __ PrepareCallCFunction(0, 1, scratch);
3184 if (masm->use_eabi_hardfloat()) {
3185 __ vmov(d0, d2);
3186 } else {
3187 __ vmov(r0, r1, d2);
3188 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003189 switch (type_) {
3190 case TranscendentalCache::SIN:
Ben Murdoch257744e2011-11-30 15:57:28 +00003191 __ CallCFunction(ExternalReference::math_sin_double_function(isolate),
3192 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003193 break;
3194 case TranscendentalCache::COS:
Ben Murdoch257744e2011-11-30 15:57:28 +00003195 __ CallCFunction(ExternalReference::math_cos_double_function(isolate),
3196 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003197 break;
3198 case TranscendentalCache::LOG:
Ben Murdoch257744e2011-11-30 15:57:28 +00003199 __ CallCFunction(ExternalReference::math_log_double_function(isolate),
3200 0, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003201 break;
3202 default:
3203 UNIMPLEMENTED();
3204 break;
3205 }
3206 __ pop(lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003207}
3208
3209
3210Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
3211 switch (type_) {
3212 // Add more cases when necessary.
3213 case TranscendentalCache::SIN: return Runtime::kMath_sin;
3214 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003215 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003216 default:
3217 UNIMPLEMENTED();
3218 return Runtime::kAbort;
3219 }
3220}
3221
3222
3223void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01003224 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003225}
3226
3227
Steve Block44f0eee2011-05-26 01:26:41 +01003228void MathPowStub::Generate(MacroAssembler* masm) {
3229 Label call_runtime;
3230
Ben Murdoch8b112d22011-06-08 16:22:53 +01003231 if (CpuFeatures::IsSupported(VFP3)) {
Steve Block44f0eee2011-05-26 01:26:41 +01003232 CpuFeatures::Scope scope(VFP3);
3233
3234 Label base_not_smi;
3235 Label exponent_not_smi;
3236 Label convert_exponent;
3237
3238 const Register base = r0;
3239 const Register exponent = r1;
3240 const Register heapnumbermap = r5;
3241 const Register heapnumber = r6;
3242 const DoubleRegister double_base = d0;
3243 const DoubleRegister double_exponent = d1;
3244 const DoubleRegister double_result = d2;
3245 const SwVfpRegister single_scratch = s0;
3246 const Register scratch = r9;
3247 const Register scratch2 = r7;
3248
3249 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
3250 __ ldr(base, MemOperand(sp, 1 * kPointerSize));
3251 __ ldr(exponent, MemOperand(sp, 0 * kPointerSize));
3252
3253 // Convert base to double value and store it in d0.
3254 __ JumpIfNotSmi(base, &base_not_smi);
3255 // Base is a Smi. Untag and convert it.
3256 __ SmiUntag(base);
3257 __ vmov(single_scratch, base);
3258 __ vcvt_f64_s32(double_base, single_scratch);
3259 __ b(&convert_exponent);
3260
3261 __ bind(&base_not_smi);
3262 __ ldr(scratch, FieldMemOperand(base, JSObject::kMapOffset));
3263 __ cmp(scratch, heapnumbermap);
3264 __ b(ne, &call_runtime);
3265 // Base is a heapnumber. Load it into double register.
3266 __ vldr(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
3267
3268 __ bind(&convert_exponent);
3269 __ JumpIfNotSmi(exponent, &exponent_not_smi);
3270 __ SmiUntag(exponent);
3271
3272 // The base is in a double register and the exponent is
3273 // an untagged smi. Allocate a heap number and call a
3274 // C function for integer exponents. The register containing
3275 // the heap number is callee-saved.
3276 __ AllocateHeapNumber(heapnumber,
3277 scratch,
3278 scratch2,
3279 heapnumbermap,
3280 &call_runtime);
3281 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003282 __ PrepareCallCFunction(1, 1, scratch);
3283 __ SetCallCDoubleArguments(double_base, exponent);
Steve Block44f0eee2011-05-26 01:26:41 +01003284 __ CallCFunction(
Ben Murdoch257744e2011-11-30 15:57:28 +00003285 ExternalReference::power_double_int_function(masm->isolate()),
3286 1, 1);
Steve Block44f0eee2011-05-26 01:26:41 +01003287 __ pop(lr);
3288 __ GetCFunctionDoubleResult(double_result);
3289 __ vstr(double_result,
3290 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3291 __ mov(r0, heapnumber);
3292 __ Ret(2 * kPointerSize);
3293
3294 __ bind(&exponent_not_smi);
3295 __ ldr(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
3296 __ cmp(scratch, heapnumbermap);
3297 __ b(ne, &call_runtime);
3298 // Exponent is a heapnumber. Load it into double register.
3299 __ vldr(double_exponent,
3300 FieldMemOperand(exponent, HeapNumber::kValueOffset));
3301
3302 // The base and the exponent are in double registers.
3303 // Allocate a heap number and call a C function for
3304 // double exponents. The register containing
3305 // the heap number is callee-saved.
3306 __ AllocateHeapNumber(heapnumber,
3307 scratch,
3308 scratch2,
3309 heapnumbermap,
3310 &call_runtime);
3311 __ push(lr);
Ben Murdoch257744e2011-11-30 15:57:28 +00003312 __ PrepareCallCFunction(0, 2, scratch);
3313 __ SetCallCDoubleArguments(double_base, double_exponent);
Steve Block44f0eee2011-05-26 01:26:41 +01003314 __ CallCFunction(
Ben Murdoch257744e2011-11-30 15:57:28 +00003315 ExternalReference::power_double_double_function(masm->isolate()),
3316 0, 2);
Steve Block44f0eee2011-05-26 01:26:41 +01003317 __ pop(lr);
3318 __ GetCFunctionDoubleResult(double_result);
3319 __ vstr(double_result,
3320 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3321 __ mov(r0, heapnumber);
3322 __ Ret(2 * kPointerSize);
3323 }
3324
3325 __ bind(&call_runtime);
3326 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
3327}
3328
3329
3330bool CEntryStub::NeedsImmovableCode() {
3331 return true;
3332}
3333
3334
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003335void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003336 __ Throw(r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003337}
3338
3339
3340void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3341 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003342 __ ThrowUncatchable(type, r0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003343}
3344
3345
3346void CEntryStub::GenerateCore(MacroAssembler* masm,
3347 Label* throw_normal_exception,
3348 Label* throw_termination_exception,
3349 Label* throw_out_of_memory_exception,
3350 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01003351 bool always_allocate) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003352 // r0: result parameter for PerformGC, if any
3353 // r4: number of arguments including receiver (C callee-saved)
3354 // r5: pointer to builtin function (C callee-saved)
3355 // r6: pointer to the first argument (C callee-saved)
Steve Block44f0eee2011-05-26 01:26:41 +01003356 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003357
3358 if (do_gc) {
3359 // Passing r0.
Ben Murdoch257744e2011-11-30 15:57:28 +00003360 __ PrepareCallCFunction(1, 0, r1);
3361 __ CallCFunction(ExternalReference::perform_gc_function(isolate),
3362 1, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003363 }
3364
3365 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01003366 ExternalReference::heap_always_allocate_scope_depth(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003367 if (always_allocate) {
3368 __ mov(r0, Operand(scope_depth));
3369 __ ldr(r1, MemOperand(r0));
3370 __ add(r1, r1, Operand(1));
3371 __ str(r1, MemOperand(r0));
3372 }
3373
3374 // Call C built-in.
3375 // r0 = argc, r1 = argv
3376 __ mov(r0, Operand(r4));
3377 __ mov(r1, Operand(r6));
3378
Steve Block1e0659c2011-05-24 12:43:12 +01003379#if defined(V8_HOST_ARCH_ARM)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003380 int frame_alignment = MacroAssembler::ActivationFrameAlignment();
3381 int frame_alignment_mask = frame_alignment - 1;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003382 if (FLAG_debug_code) {
3383 if (frame_alignment > kPointerSize) {
3384 Label alignment_as_expected;
3385 ASSERT(IsPowerOf2(frame_alignment));
Steve Block1e0659c2011-05-24 12:43:12 +01003386 __ tst(sp, Operand(frame_alignment_mask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003387 __ b(eq, &alignment_as_expected);
3388 // Don't use Check here, as it will call Runtime_Abort re-entering here.
3389 __ stop("Unexpected alignment");
3390 __ bind(&alignment_as_expected);
3391 }
3392 }
3393#endif
3394
Steve Block44f0eee2011-05-26 01:26:41 +01003395 __ mov(r2, Operand(ExternalReference::isolate_address()));
3396
3397
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003398 // TODO(1242173): To let the GC traverse the return address of the exit
3399 // frames, we need to know where the return address is. Right now,
Steve Block1e0659c2011-05-24 12:43:12 +01003400 // we store it on the stack to be able to find it again, but we never
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003401 // restore from it in case of changes, which makes it impossible to
3402 // support moving the C entry code stub. This should be fixed, but currently
3403 // this is OK because the CEntryStub gets generated so early in the V8 boot
3404 // sequence that it is not moving ever.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003405
Steve Block1e0659c2011-05-24 12:43:12 +01003406 // Compute the return address in lr to return to after the jump below. Pc is
3407 // already at '+ 8' from the current instruction but return is after three
3408 // instructions so add another 4 to pc to get the return address.
3409 masm->add(lr, pc, Operand(4));
3410 __ str(lr, MemOperand(sp, 0));
3411 masm->Jump(r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003412
3413 if (always_allocate) {
3414 // It's okay to clobber r2 and r3 here. Don't mess with r0 and r1
3415 // though (contain the result).
3416 __ mov(r2, Operand(scope_depth));
3417 __ ldr(r3, MemOperand(r2));
3418 __ sub(r3, r3, Operand(1));
3419 __ str(r3, MemOperand(r2));
3420 }
3421
3422 // check for failure result
3423 Label failure_returned;
3424 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3425 // Lower 2 bits of r2 are 0 iff r0 has failure tag.
3426 __ add(r2, r0, Operand(1));
3427 __ tst(r2, Operand(kFailureTagMask));
3428 __ b(eq, &failure_returned);
3429
3430 // Exit C frame and return.
3431 // r0:r1: result
3432 // sp: stack pointer
3433 // fp: frame pointer
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003434 // Callee-saved register r4 still holds argc.
3435 __ LeaveExitFrame(save_doubles_, r4);
3436 __ mov(pc, lr);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003437
3438 // check if we should retry or throw exception
3439 Label retry;
3440 __ bind(&failure_returned);
3441 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3442 __ tst(r0, Operand(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
3443 __ b(eq, &retry);
3444
3445 // Special handling of out of memory exceptions.
3446 Failure* out_of_memory = Failure::OutOfMemoryException();
3447 __ cmp(r0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3448 __ b(eq, throw_out_of_memory_exception);
3449
3450 // Retrieve the pending exception and clear the variable.
Steve Block44f0eee2011-05-26 01:26:41 +01003451 __ mov(ip, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003452 __ ldr(r3, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +01003453 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3454 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003455 __ ldr(r0, MemOperand(ip));
3456 __ str(r3, MemOperand(ip));
3457
3458 // Special handling of termination exceptions which are uncatchable
3459 // by javascript code.
Steve Block44f0eee2011-05-26 01:26:41 +01003460 __ cmp(r0, Operand(isolate->factory()->termination_exception()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003461 __ b(eq, throw_termination_exception);
3462
3463 // Handle normal exception.
3464 __ jmp(throw_normal_exception);
3465
3466 __ bind(&retry); // pass last failure (r0) as parameter (r0) when retrying
3467}
3468
3469
3470void CEntryStub::Generate(MacroAssembler* masm) {
3471 // Called from JavaScript; parameters are on stack as if calling JS function
3472 // r0: number of arguments including receiver
3473 // r1: pointer to builtin function
3474 // fp: frame pointer (restored after C call)
3475 // sp: stack pointer (restored as callee's sp after C call)
3476 // cp: current context (C callee-saved)
3477
3478 // Result returned in r0 or r0+r1 by default.
3479
3480 // NOTE: Invocations of builtins may return failure objects
3481 // instead of a proper result. The builtin entry handles
3482 // this by performing a garbage collection and retrying the
3483 // builtin once.
3484
Steve Block1e0659c2011-05-24 12:43:12 +01003485 // Compute the argv pointer in a callee-saved register.
3486 __ add(r6, sp, Operand(r0, LSL, kPointerSizeLog2));
3487 __ sub(r6, r6, Operand(kPointerSize));
3488
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003489 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003490 __ EnterExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003491
Steve Block1e0659c2011-05-24 12:43:12 +01003492 // Setup argc and the builtin function in callee-saved registers.
3493 __ mov(r4, Operand(r0));
3494 __ mov(r5, Operand(r1));
3495
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003496 // r4: number of arguments (C callee-saved)
3497 // r5: pointer to builtin function (C callee-saved)
3498 // r6: pointer to first argument (C callee-saved)
3499
3500 Label throw_normal_exception;
3501 Label throw_termination_exception;
3502 Label throw_out_of_memory_exception;
3503
3504 // Call into the runtime system.
3505 GenerateCore(masm,
3506 &throw_normal_exception,
3507 &throw_termination_exception,
3508 &throw_out_of_memory_exception,
3509 false,
Steve Block1e0659c2011-05-24 12:43:12 +01003510 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003511
3512 // Do space-specific GC and retry runtime call.
3513 GenerateCore(masm,
3514 &throw_normal_exception,
3515 &throw_termination_exception,
3516 &throw_out_of_memory_exception,
3517 true,
Steve Block1e0659c2011-05-24 12:43:12 +01003518 false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003519
3520 // Do full GC and retry runtime call one final time.
3521 Failure* failure = Failure::InternalError();
3522 __ mov(r0, Operand(reinterpret_cast<int32_t>(failure)));
3523 GenerateCore(masm,
3524 &throw_normal_exception,
3525 &throw_termination_exception,
3526 &throw_out_of_memory_exception,
3527 true,
Steve Block1e0659c2011-05-24 12:43:12 +01003528 true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003529
3530 __ bind(&throw_out_of_memory_exception);
3531 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3532
3533 __ bind(&throw_termination_exception);
3534 GenerateThrowUncatchable(masm, TERMINATION);
3535
3536 __ bind(&throw_normal_exception);
3537 GenerateThrowTOS(masm);
3538}
3539
3540
3541void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3542 // r0: code entry
3543 // r1: function
3544 // r2: receiver
3545 // r3: argc
3546 // [sp+0]: argv
3547
3548 Label invoke, exit;
3549
3550 // Called from C, so do not pop argc and args on exit (preserve sp)
3551 // No need to save register-passed args
3552 // Save callee-saved registers (incl. cp and fp), sp, and lr
3553 __ stm(db_w, sp, kCalleeSaved | lr.bit());
3554
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003555 if (CpuFeatures::IsSupported(VFP3)) {
3556 CpuFeatures::Scope scope(VFP3);
3557 // Save callee-saved vfp registers.
3558 __ vstm(db_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
3559 }
3560
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003561 // Get address of argv, see stm above.
3562 // r0: code entry
3563 // r1: function
3564 // r2: receiver
3565 // r3: argc
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003566
3567 // Setup argv in r4.
3568 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
3569 if (CpuFeatures::IsSupported(VFP3)) {
3570 offset_to_argv += kNumDoubleCalleeSaved * kDoubleSize;
3571 }
3572 __ ldr(r4, MemOperand(sp, offset_to_argv));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003573
3574 // Push a frame with special values setup to mark it as an entry frame.
3575 // r0: code entry
3576 // r1: function
3577 // r2: receiver
3578 // r3: argc
3579 // r4: argv
Steve Block44f0eee2011-05-26 01:26:41 +01003580 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003581 __ mov(r8, Operand(-1)); // Push a bad frame pointer to fail if it is used.
3582 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3583 __ mov(r7, Operand(Smi::FromInt(marker)));
3584 __ mov(r6, Operand(Smi::FromInt(marker)));
Steve Block44f0eee2011-05-26 01:26:41 +01003585 __ mov(r5,
3586 Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003587 __ ldr(r5, MemOperand(r5));
3588 __ Push(r8, r7, r6, r5);
3589
3590 // Setup frame pointer for the frame to be pushed.
3591 __ add(fp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3592
Ben Murdochb0fe1622011-05-05 13:52:32 +01003593#ifdef ENABLE_LOGGING_AND_PROFILING
3594 // If this is the outermost JS call, set js_entry_sp value.
Steve Block053d10c2011-06-13 19:13:29 +01003595 Label non_outermost_js;
Steve Block44f0eee2011-05-26 01:26:41 +01003596 ExternalReference js_entry_sp(Isolate::k_js_entry_sp_address, isolate);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003597 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
3598 __ ldr(r6, MemOperand(r5));
Steve Block053d10c2011-06-13 19:13:29 +01003599 __ cmp(r6, Operand(0));
3600 __ b(ne, &non_outermost_js);
3601 __ str(fp, MemOperand(r5));
3602 __ mov(ip, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
3603 Label cont;
3604 __ b(&cont);
3605 __ bind(&non_outermost_js);
3606 __ mov(ip, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
3607 __ bind(&cont);
3608 __ push(ip);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003609#endif
3610
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003611 // Call a faked try-block that does the invoke.
3612 __ bl(&invoke);
3613
3614 // Caught exception: Store result (exception) in the pending
3615 // exception field in the JSEnv and return a failure sentinel.
3616 // Coming in here the fp will be invalid because the PushTryHandler below
3617 // sets it to 0 to signal the existence of the JSEntry frame.
Steve Block44f0eee2011-05-26 01:26:41 +01003618 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3619 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003620 __ str(r0, MemOperand(ip));
3621 __ mov(r0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
3622 __ b(&exit);
3623
3624 // Invoke: Link this frame into the handler chain.
3625 __ bind(&invoke);
3626 // Must preserve r0-r4, r5-r7 are available.
3627 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
3628 // If an exception not caught by another handler occurs, this handler
3629 // returns control to the code after the bl(&invoke) above, which
3630 // restores all kCalleeSaved registers (including cp and fp) to their
3631 // saved values before returning a failure to C.
3632
3633 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01003634 __ mov(ip, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003635 __ ldr(r5, MemOperand(ip));
Steve Block44f0eee2011-05-26 01:26:41 +01003636 __ mov(ip, Operand(ExternalReference(Isolate::k_pending_exception_address,
3637 isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003638 __ str(r5, MemOperand(ip));
3639
3640 // Invoke the function by calling through JS entry trampoline builtin.
3641 // Notice that we cannot store a reference to the trampoline code directly in
3642 // this stub, because runtime stubs are not traversed when doing GC.
3643
3644 // Expected registers by Builtins::JSEntryTrampoline
3645 // r0: code entry
3646 // r1: function
3647 // r2: receiver
3648 // r3: argc
3649 // r4: argv
3650 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01003651 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
3652 isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003653 __ mov(ip, Operand(construct_entry));
3654 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003655 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003656 __ mov(ip, Operand(entry));
3657 }
3658 __ ldr(ip, MemOperand(ip)); // deref address
3659
3660 // Branch and link to JSEntryTrampoline. We don't use the double underscore
3661 // macro for the add instruction because we don't want the coverage tool
3662 // inserting instructions here after we read the pc.
3663 __ mov(lr, Operand(pc));
3664 masm->add(pc, ip, Operand(Code::kHeaderSize - kHeapObjectTag));
3665
Steve Block053d10c2011-06-13 19:13:29 +01003666 // Unlink this frame from the handler chain.
3667 __ PopTryHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003668
3669 __ bind(&exit); // r0 holds result
Steve Block053d10c2011-06-13 19:13:29 +01003670#ifdef ENABLE_LOGGING_AND_PROFILING
3671 // Check if the current stack frame is marked as the outermost JS frame.
3672 Label non_outermost_js_2;
3673 __ pop(r5);
3674 __ cmp(r5, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
3675 __ b(ne, &non_outermost_js_2);
3676 __ mov(r6, Operand(0));
3677 __ mov(r5, Operand(ExternalReference(js_entry_sp)));
3678 __ str(r6, MemOperand(r5));
3679 __ bind(&non_outermost_js_2);
3680#endif
3681
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003682 // Restore the top frame descriptors from the stack.
3683 __ pop(r3);
Steve Block44f0eee2011-05-26 01:26:41 +01003684 __ mov(ip,
3685 Operand(ExternalReference(Isolate::k_c_entry_fp_address, isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003686 __ str(r3, MemOperand(ip));
3687
3688 // Reset the stack to the callee saved registers.
3689 __ add(sp, sp, Operand(-EntryFrameConstants::kCallerFPOffset));
3690
3691 // Restore callee-saved registers and return.
3692#ifdef DEBUG
3693 if (FLAG_debug_code) {
3694 __ mov(lr, Operand(pc));
3695 }
3696#endif
Ben Murdoch7d3e7fc2011-07-12 16:37:06 +01003697
3698 if (CpuFeatures::IsSupported(VFP3)) {
3699 CpuFeatures::Scope scope(VFP3);
3700 // Restore callee-saved vfp registers.
3701 __ vldm(ia_w, sp, kFirstCalleeSavedDoubleReg, kLastCalleeSavedDoubleReg);
3702 }
3703
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003704 __ ldm(ia_w, sp, kCalleeSaved | pc.bit());
3705}
3706
3707
Steve Block1e0659c2011-05-24 12:43:12 +01003708// Uses registers r0 to r4.
3709// Expected input (depending on whether args are in registers or on the stack):
3710// * object: r0 or at sp + 1 * kPointerSize.
3711// * function: r1 or at sp.
3712//
3713// An inlined call site may have been generated before calling this stub.
3714// In this case the offset to the inline site to patch is passed on the stack,
3715// in the safepoint slot for register r4.
3716// (See LCodeGen::DoInstanceOfKnownGlobal)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003717void InstanceofStub::Generate(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01003718 // Call site inlining and patching implies arguments in registers.
3719 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
3720 // ReturnTrueFalse is only implemented for inlined call sites.
3721 ASSERT(!ReturnTrueFalseObject() || HasCallSiteInlineCheck());
3722
Ben Murdochb0fe1622011-05-05 13:52:32 +01003723 // Fixed register usage throughout the stub:
Steve Block9fac8402011-05-12 15:51:54 +01003724 const Register object = r0; // Object (lhs).
Steve Block1e0659c2011-05-24 12:43:12 +01003725 Register map = r3; // Map of the object.
Steve Block9fac8402011-05-12 15:51:54 +01003726 const Register function = r1; // Function (rhs).
Ben Murdochb0fe1622011-05-05 13:52:32 +01003727 const Register prototype = r4; // Prototype of the function.
Steve Block1e0659c2011-05-24 12:43:12 +01003728 const Register inline_site = r9;
Ben Murdochb0fe1622011-05-05 13:52:32 +01003729 const Register scratch = r2;
Steve Block1e0659c2011-05-24 12:43:12 +01003730
3731 const int32_t kDeltaToLoadBoolResult = 3 * kPointerSize;
3732
Ben Murdochb0fe1622011-05-05 13:52:32 +01003733 Label slow, loop, is_instance, is_not_instance, not_js_object;
Steve Block1e0659c2011-05-24 12:43:12 +01003734
Ben Murdoch086aeea2011-05-13 15:57:08 +01003735 if (!HasArgsInRegisters()) {
Steve Block9fac8402011-05-12 15:51:54 +01003736 __ ldr(object, MemOperand(sp, 1 * kPointerSize));
3737 __ ldr(function, MemOperand(sp, 0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003738 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003739
Ben Murdochb0fe1622011-05-05 13:52:32 +01003740 // Check that the left hand is a JS object and load map.
Steve Block1e0659c2011-05-24 12:43:12 +01003741 __ JumpIfSmi(object, &not_js_object);
Steve Block9fac8402011-05-12 15:51:54 +01003742 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003743
Steve Block1e0659c2011-05-24 12:43:12 +01003744 // If there is a call site cache don't look in the global cache, but do the
3745 // real lookup and update the call site cache.
3746 if (!HasCallSiteInlineCheck()) {
3747 Label miss;
3748 __ LoadRoot(ip, Heap::kInstanceofCacheFunctionRootIndex);
3749 __ cmp(function, ip);
3750 __ b(ne, &miss);
3751 __ LoadRoot(ip, Heap::kInstanceofCacheMapRootIndex);
3752 __ cmp(map, ip);
3753 __ b(ne, &miss);
3754 __ LoadRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3755 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003756
Steve Block1e0659c2011-05-24 12:43:12 +01003757 __ bind(&miss);
3758 }
3759
3760 // Get the prototype of the function.
Steve Block9fac8402011-05-12 15:51:54 +01003761 __ TryGetFunctionPrototype(function, prototype, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003762
3763 // Check that the function prototype is a JS object.
Steve Block1e0659c2011-05-24 12:43:12 +01003764 __ JumpIfSmi(prototype, &slow);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003765 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003766
Steve Block1e0659c2011-05-24 12:43:12 +01003767 // Update the global instanceof or call site inlined cache with the current
3768 // map and function. The cached answer will be set when it is known below.
3769 if (!HasCallSiteInlineCheck()) {
3770 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
3771 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
3772 } else {
3773 ASSERT(HasArgsInRegisters());
3774 // Patch the (relocated) inlined map check.
3775
3776 // The offset was stored in r4 safepoint slot.
3777 // (See LCodeGen::DoDeferredLInstanceOfKnownGlobal)
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003778 __ LoadFromSafepointRegisterSlot(scratch, r4);
Steve Block1e0659c2011-05-24 12:43:12 +01003779 __ sub(inline_site, lr, scratch);
3780 // Get the map location in scratch and patch it.
3781 __ GetRelocatedValueLocation(inline_site, scratch);
3782 __ str(map, MemOperand(scratch));
3783 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003784
3785 // Register mapping: r3 is object map and r4 is function prototype.
3786 // Get prototype of object into r2.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003787 __ ldr(scratch, FieldMemOperand(map, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003788
Steve Block1e0659c2011-05-24 12:43:12 +01003789 // We don't need map any more. Use it as a scratch register.
3790 Register scratch2 = map;
3791 map = no_reg;
3792
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003793 // Loop through the prototype chain looking for the function prototype.
Steve Block1e0659c2011-05-24 12:43:12 +01003794 __ LoadRoot(scratch2, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003795 __ bind(&loop);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003796 __ cmp(scratch, Operand(prototype));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003797 __ b(eq, &is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003798 __ cmp(scratch, scratch2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003799 __ b(eq, &is_not_instance);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003800 __ ldr(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
3801 __ ldr(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003802 __ jmp(&loop);
3803
3804 __ bind(&is_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003805 if (!HasCallSiteInlineCheck()) {
3806 __ mov(r0, Operand(Smi::FromInt(0)));
3807 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3808 } else {
3809 // Patch the call site to return true.
3810 __ LoadRoot(r0, Heap::kTrueValueRootIndex);
3811 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
3812 // Get the boolean result location in scratch and patch it.
3813 __ GetRelocatedValueLocation(inline_site, scratch);
3814 __ str(r0, MemOperand(scratch));
3815
3816 if (!ReturnTrueFalseObject()) {
3817 __ mov(r0, Operand(Smi::FromInt(0)));
3818 }
3819 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01003820 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003821
3822 __ bind(&is_not_instance);
Steve Block1e0659c2011-05-24 12:43:12 +01003823 if (!HasCallSiteInlineCheck()) {
3824 __ mov(r0, Operand(Smi::FromInt(1)));
3825 __ StoreRoot(r0, Heap::kInstanceofCacheAnswerRootIndex);
3826 } else {
3827 // Patch the call site to return false.
3828 __ LoadRoot(r0, Heap::kFalseValueRootIndex);
3829 __ add(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
3830 // Get the boolean result location in scratch and patch it.
3831 __ GetRelocatedValueLocation(inline_site, scratch);
3832 __ str(r0, MemOperand(scratch));
3833
3834 if (!ReturnTrueFalseObject()) {
3835 __ mov(r0, Operand(Smi::FromInt(1)));
3836 }
3837 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01003838 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003839
3840 Label object_not_null, object_not_null_or_smi;
3841 __ bind(&not_js_object);
3842 // Before null, smi and string value checks, check that the rhs is a function
3843 // as for a non-function rhs an exception needs to be thrown.
Steve Block1e0659c2011-05-24 12:43:12 +01003844 __ JumpIfSmi(function, &slow);
3845 __ CompareObjectType(function, scratch2, scratch, JS_FUNCTION_TYPE);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003846 __ b(ne, &slow);
3847
3848 // Null is not instance of anything.
Ben Murdoch257744e2011-11-30 15:57:28 +00003849 __ cmp(scratch, Operand(masm->isolate()->factory()->null_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003850 __ b(ne, &object_not_null);
3851 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003852 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003853
3854 __ bind(&object_not_null);
3855 // Smi values are not instances of anything.
Steve Block1e0659c2011-05-24 12:43:12 +01003856 __ JumpIfNotSmi(object, &object_not_null_or_smi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003857 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003858 __ Ret(HasArgsInRegisters() ? 0 : 2);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003859
3860 __ bind(&object_not_null_or_smi);
3861 // String values are not instances of anything.
3862 __ IsObjectJSStringType(object, scratch, &slow);
3863 __ mov(r0, Operand(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01003864 __ Ret(HasArgsInRegisters() ? 0 : 2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003865
3866 // Slow-case. Tail call builtin.
Ben Murdoch086aeea2011-05-13 15:57:08 +01003867 __ bind(&slow);
Steve Block1e0659c2011-05-24 12:43:12 +01003868 if (!ReturnTrueFalseObject()) {
3869 if (HasArgsInRegisters()) {
3870 __ Push(r0, r1);
3871 }
Ben Murdoch257744e2011-11-30 15:57:28 +00003872 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003873 } else {
3874 __ EnterInternalFrame();
3875 __ Push(r0, r1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003876 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
Steve Block1e0659c2011-05-24 12:43:12 +01003877 __ LeaveInternalFrame();
3878 __ cmp(r0, Operand(0));
3879 __ LoadRoot(r0, Heap::kTrueValueRootIndex, eq);
3880 __ LoadRoot(r0, Heap::kFalseValueRootIndex, ne);
3881 __ Ret(HasArgsInRegisters() ? 0 : 2);
3882 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003883}
3884
3885
Steve Block1e0659c2011-05-24 12:43:12 +01003886Register InstanceofStub::left() { return r0; }
3887
3888
3889Register InstanceofStub::right() { return r1; }
3890
3891
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003892void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
3893 // The displacement is the offset of the last parameter (if any)
3894 // relative to the frame pointer.
3895 static const int kDisplacement =
3896 StandardFrameConstants::kCallerSPOffset - kPointerSize;
3897
3898 // Check that the key is a smi.
3899 Label slow;
Steve Block1e0659c2011-05-24 12:43:12 +01003900 __ JumpIfNotSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003901
3902 // Check if the calling frame is an arguments adaptor frame.
3903 Label adaptor;
3904 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3905 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3906 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3907 __ b(eq, &adaptor);
3908
3909 // Check index against formal parameters count limit passed in
3910 // through register r0. Use unsigned comparison to get negative
3911 // check for free.
3912 __ cmp(r1, r0);
Ben Murdoch086aeea2011-05-13 15:57:08 +01003913 __ b(hs, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003914
3915 // Read the argument from the stack and return it.
3916 __ sub(r3, r0, r1);
3917 __ add(r3, fp, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3918 __ ldr(r0, MemOperand(r3, kDisplacement));
3919 __ Jump(lr);
3920
3921 // Arguments adaptor case: Check index against actual arguments
3922 // limit found in the arguments adaptor frame. Use unsigned
3923 // comparison to get negative check for free.
3924 __ bind(&adaptor);
3925 __ ldr(r0, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3926 __ cmp(r1, r0);
3927 __ b(cs, &slow);
3928
3929 // Read the argument from the adaptor frame and return it.
3930 __ sub(r3, r0, r1);
3931 __ add(r3, r2, Operand(r3, LSL, kPointerSizeLog2 - kSmiTagSize));
3932 __ ldr(r0, MemOperand(r3, kDisplacement));
3933 __ Jump(lr);
3934
3935 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3936 // by calling the runtime system.
3937 __ bind(&slow);
3938 __ push(r1);
3939 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3940}
3941
3942
3943void ArgumentsAccessStub::GenerateNewObject(MacroAssembler* masm) {
3944 // sp[0] : number of parameters
3945 // sp[4] : receiver displacement
3946 // sp[8] : function
3947
3948 // Check if the calling frame is an arguments adaptor frame.
3949 Label adaptor_frame, try_allocate, runtime;
3950 __ ldr(r2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
3951 __ ldr(r3, MemOperand(r2, StandardFrameConstants::kContextOffset));
3952 __ cmp(r3, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
3953 __ b(eq, &adaptor_frame);
3954
3955 // Get the length from the frame.
3956 __ ldr(r1, MemOperand(sp, 0));
3957 __ b(&try_allocate);
3958
3959 // Patch the arguments.length and the parameters pointer.
3960 __ bind(&adaptor_frame);
3961 __ ldr(r1, MemOperand(r2, ArgumentsAdaptorFrameConstants::kLengthOffset));
3962 __ str(r1, MemOperand(sp, 0));
3963 __ add(r3, r2, Operand(r1, LSL, kPointerSizeLog2 - kSmiTagSize));
3964 __ add(r3, r3, Operand(StandardFrameConstants::kCallerSPOffset));
3965 __ str(r3, MemOperand(sp, 1 * kPointerSize));
3966
3967 // Try the new space allocation. Start out with computing the size
3968 // of the arguments object and the elements array in words.
3969 Label add_arguments_object;
3970 __ bind(&try_allocate);
Iain Merrick9ac36c92010-09-13 15:29:50 +01003971 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003972 __ b(eq, &add_arguments_object);
3973 __ mov(r1, Operand(r1, LSR, kSmiTagSize));
3974 __ add(r1, r1, Operand(FixedArray::kHeaderSize / kPointerSize));
3975 __ bind(&add_arguments_object);
Steve Block44f0eee2011-05-26 01:26:41 +01003976 __ add(r1, r1, Operand(GetArgumentsObjectSize() / kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003977
3978 // Do the allocation of both objects in one go.
3979 __ AllocateInNewSpace(
3980 r1,
3981 r0,
3982 r2,
3983 r3,
3984 &runtime,
3985 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
3986
3987 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003988 __ ldr(r4, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
3989 __ ldr(r4, FieldMemOperand(r4, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01003990 __ ldr(r4, MemOperand(r4,
3991 Context::SlotOffset(GetArgumentsBoilerplateIndex())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003992
3993 // Copy the JS object part.
3994 __ CopyFields(r0, r4, r3.bit(), JSObject::kHeaderSize / kPointerSize);
3995
Steve Block44f0eee2011-05-26 01:26:41 +01003996 if (type_ == NEW_NON_STRICT) {
3997 // Setup the callee in-object property.
3998 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
3999 __ ldr(r3, MemOperand(sp, 2 * kPointerSize));
4000 const int kCalleeOffset = JSObject::kHeaderSize +
4001 Heap::kArgumentsCalleeIndex * kPointerSize;
4002 __ str(r3, FieldMemOperand(r0, kCalleeOffset));
4003 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004004
4005 // Get the length (smi tagged) and set that as an in-object property too.
Steve Block44f0eee2011-05-26 01:26:41 +01004006 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004007 __ ldr(r1, MemOperand(sp, 0 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01004008 __ str(r1, FieldMemOperand(r0, JSObject::kHeaderSize +
4009 Heap::kArgumentsLengthIndex * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004010
4011 // If there are no actual arguments, we're done.
4012 Label done;
Iain Merrick9ac36c92010-09-13 15:29:50 +01004013 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004014 __ b(eq, &done);
4015
4016 // Get the parameters pointer from the stack.
4017 __ ldr(r2, MemOperand(sp, 1 * kPointerSize));
4018
4019 // Setup the elements pointer in the allocated arguments object and
4020 // initialize the header in the elements fixed array.
Steve Block44f0eee2011-05-26 01:26:41 +01004021 __ add(r4, r0, Operand(GetArgumentsObjectSize()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004022 __ str(r4, FieldMemOperand(r0, JSObject::kElementsOffset));
4023 __ LoadRoot(r3, Heap::kFixedArrayMapRootIndex);
4024 __ str(r3, FieldMemOperand(r4, FixedArray::kMapOffset));
4025 __ str(r1, FieldMemOperand(r4, FixedArray::kLengthOffset));
4026 __ mov(r1, Operand(r1, LSR, kSmiTagSize)); // Untag the length for the loop.
4027
4028 // Copy the fixed array slots.
4029 Label loop;
4030 // Setup r4 to point to the first array slot.
4031 __ add(r4, r4, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4032 __ bind(&loop);
4033 // Pre-decrement r2 with kPointerSize on each iteration.
4034 // Pre-decrement in order to skip receiver.
4035 __ ldr(r3, MemOperand(r2, kPointerSize, NegPreIndex));
4036 // Post-increment r4 with kPointerSize on each iteration.
4037 __ str(r3, MemOperand(r4, kPointerSize, PostIndex));
4038 __ sub(r1, r1, Operand(1));
Iain Merrick9ac36c92010-09-13 15:29:50 +01004039 __ cmp(r1, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004040 __ b(ne, &loop);
4041
4042 // Return and remove the on-stack parameters.
4043 __ bind(&done);
4044 __ add(sp, sp, Operand(3 * kPointerSize));
4045 __ Ret();
4046
4047 // Do the runtime call to allocate the arguments object.
4048 __ bind(&runtime);
4049 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
4050}
4051
4052
4053void RegExpExecStub::Generate(MacroAssembler* masm) {
4054 // Just jump directly to runtime if native RegExp is not selected at compile
4055 // time or if regexp entry in generated code is turned off runtime switch or
4056 // at compilation.
4057#ifdef V8_INTERPRETED_REGEXP
4058 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4059#else // V8_INTERPRETED_REGEXP
4060 if (!FLAG_regexp_entry_native) {
4061 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4062 return;
4063 }
4064
4065 // Stack frame on entry.
4066 // sp[0]: last_match_info (expected JSArray)
4067 // sp[4]: previous index
4068 // sp[8]: subject string
4069 // sp[12]: JSRegExp object
4070
4071 static const int kLastMatchInfoOffset = 0 * kPointerSize;
4072 static const int kPreviousIndexOffset = 1 * kPointerSize;
4073 static const int kSubjectOffset = 2 * kPointerSize;
4074 static const int kJSRegExpOffset = 3 * kPointerSize;
4075
4076 Label runtime, invoke_regexp;
4077
4078 // Allocation of registers for this function. These are in callee save
4079 // registers and will be preserved by the call to the native RegExp code, as
4080 // this code is called using the normal C calling convention. When calling
4081 // directly from generated code the native RegExp code will not do a GC and
4082 // therefore the content of these registers are safe to use after the call.
4083 Register subject = r4;
4084 Register regexp_data = r5;
4085 Register last_match_info_elements = r6;
4086
4087 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01004088 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004089 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01004090 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004091 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01004092 ExternalReference::address_of_regexp_stack_memory_size(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004093 __ mov(r0, Operand(address_of_regexp_stack_memory_size));
4094 __ ldr(r0, MemOperand(r0, 0));
4095 __ tst(r0, Operand(r0));
4096 __ b(eq, &runtime);
4097
4098 // Check that the first argument is a JSRegExp object.
4099 __ ldr(r0, MemOperand(sp, kJSRegExpOffset));
4100 STATIC_ASSERT(kSmiTag == 0);
4101 __ tst(r0, Operand(kSmiTagMask));
4102 __ b(eq, &runtime);
4103 __ CompareObjectType(r0, r1, r1, JS_REGEXP_TYPE);
4104 __ b(ne, &runtime);
4105
4106 // Check that the RegExp has been compiled (data contains a fixed array).
4107 __ ldr(regexp_data, FieldMemOperand(r0, JSRegExp::kDataOffset));
4108 if (FLAG_debug_code) {
4109 __ tst(regexp_data, Operand(kSmiTagMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004110 __ Check(ne, "Unexpected type for RegExp data, FixedArray expected");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004111 __ CompareObjectType(regexp_data, r0, r0, FIXED_ARRAY_TYPE);
4112 __ Check(eq, "Unexpected type for RegExp data, FixedArray expected");
4113 }
4114
4115 // regexp_data: RegExp data (FixedArray)
4116 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
4117 __ ldr(r0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
4118 __ cmp(r0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
4119 __ b(ne, &runtime);
4120
4121 // regexp_data: RegExp data (FixedArray)
4122 // Check that the number of captures fit in the static offsets vector buffer.
4123 __ ldr(r2,
4124 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4125 // Calculate number of capture registers (number_of_captures + 1) * 2. This
4126 // uses the asumption that smis are 2 * their untagged value.
4127 STATIC_ASSERT(kSmiTag == 0);
4128 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4129 __ add(r2, r2, Operand(2)); // r2 was a smi.
4130 // Check that the static offsets vector buffer is large enough.
4131 __ cmp(r2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
4132 __ b(hi, &runtime);
4133
4134 // r2: Number of capture registers
4135 // regexp_data: RegExp data (FixedArray)
4136 // Check that the second argument is a string.
4137 __ ldr(subject, MemOperand(sp, kSubjectOffset));
4138 __ tst(subject, Operand(kSmiTagMask));
4139 __ b(eq, &runtime);
4140 Condition is_string = masm->IsObjectStringType(subject, r0);
4141 __ b(NegateCondition(is_string), &runtime);
4142 // Get the length of the string to r3.
4143 __ ldr(r3, FieldMemOperand(subject, String::kLengthOffset));
4144
4145 // r2: Number of capture registers
4146 // r3: Length of subject string as a smi
4147 // subject: Subject string
4148 // regexp_data: RegExp data (FixedArray)
4149 // Check that the third argument is a positive smi less than the subject
4150 // string length. A negative value will be greater (unsigned comparison).
4151 __ ldr(r0, MemOperand(sp, kPreviousIndexOffset));
4152 __ tst(r0, Operand(kSmiTagMask));
4153 __ b(ne, &runtime);
4154 __ cmp(r3, Operand(r0));
4155 __ b(ls, &runtime);
4156
4157 // r2: Number of capture registers
4158 // subject: Subject string
4159 // regexp_data: RegExp data (FixedArray)
4160 // Check that the fourth object is a JSArray object.
4161 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
4162 __ tst(r0, Operand(kSmiTagMask));
4163 __ b(eq, &runtime);
4164 __ CompareObjectType(r0, r1, r1, JS_ARRAY_TYPE);
4165 __ b(ne, &runtime);
4166 // Check that the JSArray is in fast case.
4167 __ ldr(last_match_info_elements,
4168 FieldMemOperand(r0, JSArray::kElementsOffset));
4169 __ ldr(r0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
4170 __ LoadRoot(ip, Heap::kFixedArrayMapRootIndex);
4171 __ cmp(r0, ip);
4172 __ b(ne, &runtime);
4173 // Check that the last match info has space for the capture registers and the
4174 // additional information.
4175 __ ldr(r0,
4176 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
4177 __ add(r2, r2, Operand(RegExpImpl::kLastMatchOverhead));
4178 __ cmp(r2, Operand(r0, ASR, kSmiTagSize));
4179 __ b(gt, &runtime);
4180
4181 // subject: Subject string
4182 // regexp_data: RegExp data (FixedArray)
4183 // Check the representation and encoding of the subject string.
4184 Label seq_string;
4185 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4186 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
4187 // First check for flat string.
4188 __ tst(r0, Operand(kIsNotStringMask | kStringRepresentationMask));
4189 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
4190 __ b(eq, &seq_string);
4191
4192 // subject: Subject string
4193 // regexp_data: RegExp data (FixedArray)
4194 // Check for flat cons string.
4195 // A flat cons string is a cons string where the second part is the empty
4196 // string. In that case the subject string is just the first part of the cons
4197 // string. Also in this case the first part of the cons string is known to be
4198 // a sequential string or an external string.
4199 STATIC_ASSERT(kExternalStringTag !=0);
4200 STATIC_ASSERT((kConsStringTag & kExternalStringTag) == 0);
4201 __ tst(r0, Operand(kIsNotStringMask | kExternalStringTag));
4202 __ b(ne, &runtime);
4203 __ ldr(r0, FieldMemOperand(subject, ConsString::kSecondOffset));
4204 __ LoadRoot(r1, Heap::kEmptyStringRootIndex);
4205 __ cmp(r0, r1);
4206 __ b(ne, &runtime);
4207 __ ldr(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
4208 __ ldr(r0, FieldMemOperand(subject, HeapObject::kMapOffset));
4209 __ ldrb(r0, FieldMemOperand(r0, Map::kInstanceTypeOffset));
4210 // Is first part a flat string?
4211 STATIC_ASSERT(kSeqStringTag == 0);
4212 __ tst(r0, Operand(kStringRepresentationMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004213 __ b(ne, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004214
4215 __ bind(&seq_string);
4216 // subject: Subject string
4217 // regexp_data: RegExp data (FixedArray)
4218 // r0: Instance type of subject string
4219 STATIC_ASSERT(4 == kAsciiStringTag);
4220 STATIC_ASSERT(kTwoByteStringTag == 0);
4221 // Find the code object based on the assumptions above.
4222 __ and_(r0, r0, Operand(kStringEncodingMask));
4223 __ mov(r3, Operand(r0, ASR, 2), SetCC);
4224 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset), ne);
4225 __ ldr(r7, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset), eq);
4226
4227 // Check that the irregexp code has been generated for the actual string
4228 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00004229 // a smi (code flushing support).
4230 __ JumpIfSmi(r7, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004231
Steve Block44f0eee2011-05-26 01:26:41 +01004232 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004233 // r7: code
4234 // subject: Subject string
4235 // regexp_data: RegExp data (FixedArray)
4236 // Load used arguments before starting to push arguments for call to native
4237 // RegExp code to avoid handling changing stack height.
4238 __ ldr(r1, MemOperand(sp, kPreviousIndexOffset));
4239 __ mov(r1, Operand(r1, ASR, kSmiTagSize));
4240
4241 // r1: previous index
Steve Block44f0eee2011-05-26 01:26:41 +01004242 // r3: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004243 // r7: code
4244 // subject: Subject string
4245 // regexp_data: RegExp data (FixedArray)
4246 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01004247 __ IncrementCounter(isolate->counters()->regexp_entry_native(), 1, r0, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004248
Steve Block44f0eee2011-05-26 01:26:41 +01004249 // Isolates: note we add an additional parameter here (isolate pointer).
4250 static const int kRegExpExecuteArguments = 8;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004251 static const int kParameterRegisters = 4;
4252 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004253
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004254 // Stack pointer now points to cell where return address is to be written.
4255 // Arguments are before that on the stack or in registers.
4256
Steve Block44f0eee2011-05-26 01:26:41 +01004257 // Argument 8 (sp[16]): Pass current isolate address.
4258 __ mov(r0, Operand(ExternalReference::isolate_address()));
4259 __ str(r0, MemOperand(sp, 4 * kPointerSize));
4260
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004261 // Argument 7 (sp[12]): Indicate that this is a direct call from JavaScript.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004262 __ mov(r0, Operand(1));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004263 __ str(r0, MemOperand(sp, 3 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004264
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004265 // Argument 6 (sp[8]): Start (high end) of backtracking stack memory area.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004266 __ mov(r0, Operand(address_of_regexp_stack_memory_address));
4267 __ ldr(r0, MemOperand(r0, 0));
4268 __ mov(r2, Operand(address_of_regexp_stack_memory_size));
4269 __ ldr(r2, MemOperand(r2, 0));
4270 __ add(r0, r0, Operand(r2));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004271 __ str(r0, MemOperand(sp, 2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004272
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004273 // Argument 5 (sp[4]): static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01004274 __ mov(r0,
4275 Operand(ExternalReference::address_of_static_offsets_vector(isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004276 __ str(r0, MemOperand(sp, 1 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004277
4278 // For arguments 4 and 3 get string length, calculate start of string data and
4279 // calculate the shift of the index (0 for ASCII and 1 for two byte).
4280 __ ldr(r0, FieldMemOperand(subject, String::kLengthOffset));
4281 __ mov(r0, Operand(r0, ASR, kSmiTagSize));
4282 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
4283 __ add(r9, subject, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4284 __ eor(r3, r3, Operand(1));
4285 // Argument 4 (r3): End of string data
4286 // Argument 3 (r2): Start of string data
4287 __ add(r2, r9, Operand(r1, LSL, r3));
4288 __ add(r3, r9, Operand(r0, LSL, r3));
4289
4290 // Argument 2 (r1): Previous index.
4291 // Already there
4292
4293 // Argument 1 (r0): Subject string.
4294 __ mov(r0, subject);
4295
4296 // Locate the code entry and call it.
4297 __ add(r7, r7, Operand(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004298 DirectCEntryStub stub;
4299 stub.GenerateCall(masm, r7);
4300
4301 __ LeaveExitFrame(false, no_reg);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004302
4303 // r0: result
4304 // subject: subject string (callee saved)
4305 // regexp_data: RegExp data (callee saved)
4306 // last_match_info_elements: Last match info elements (callee saved)
4307
4308 // Check the result.
4309 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004310
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004311 __ cmp(r0, Operand(NativeRegExpMacroAssembler::SUCCESS));
4312 __ b(eq, &success);
4313 Label failure;
4314 __ cmp(r0, Operand(NativeRegExpMacroAssembler::FAILURE));
4315 __ b(eq, &failure);
4316 __ cmp(r0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
4317 // If not exception it can only be retry. Handle that in the runtime system.
4318 __ b(ne, &runtime);
4319 // Result must now be exception. If there is no pending exception already a
4320 // stack overflow (on the backtrack stack) was detected in RegExp code but
4321 // haven't created the exception yet. Handle that in the runtime system.
4322 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01004323 __ mov(r1, Operand(ExternalReference::the_hole_value_location(isolate)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004324 __ ldr(r1, MemOperand(r1, 0));
Steve Block44f0eee2011-05-26 01:26:41 +01004325 __ mov(r2, Operand(ExternalReference(Isolate::k_pending_exception_address,
4326 isolate)));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004327 __ ldr(r0, MemOperand(r2, 0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004328 __ cmp(r0, r1);
4329 __ b(eq, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004330
4331 __ str(r1, MemOperand(r2, 0)); // Clear pending exception.
4332
4333 // Check if the exception is a termination. If so, throw as uncatchable.
4334 __ LoadRoot(ip, Heap::kTerminationExceptionRootIndex);
4335 __ cmp(r0, ip);
4336 Label termination_exception;
4337 __ b(eq, &termination_exception);
4338
4339 __ Throw(r0); // Expects thrown value in r0.
4340
4341 __ bind(&termination_exception);
4342 __ ThrowUncatchable(TERMINATION, r0); // Expects thrown value in r0.
4343
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004344 __ bind(&failure);
4345 // For failure and exception return null.
Ben Murdoch257744e2011-11-30 15:57:28 +00004346 __ mov(r0, Operand(masm->isolate()->factory()->null_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004347 __ add(sp, sp, Operand(4 * kPointerSize));
4348 __ Ret();
4349
4350 // Process the result from the native regexp code.
4351 __ bind(&success);
4352 __ ldr(r1,
4353 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4354 // Calculate number of capture registers (number_of_captures + 1) * 2.
4355 STATIC_ASSERT(kSmiTag == 0);
4356 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4357 __ add(r1, r1, Operand(2)); // r1 was a smi.
4358
4359 // r1: number of capture registers
4360 // r4: subject string
4361 // Store the capture count.
4362 __ mov(r2, Operand(r1, LSL, kSmiTagSize + kSmiShiftSize)); // To smi.
4363 __ str(r2, FieldMemOperand(last_match_info_elements,
4364 RegExpImpl::kLastCaptureCountOffset));
4365 // Store last subject and last input.
4366 __ mov(r3, last_match_info_elements); // Moved up to reduce latency.
4367 __ str(subject,
4368 FieldMemOperand(last_match_info_elements,
4369 RegExpImpl::kLastSubjectOffset));
4370 __ RecordWrite(r3, Operand(RegExpImpl::kLastSubjectOffset), r2, r7);
4371 __ str(subject,
4372 FieldMemOperand(last_match_info_elements,
4373 RegExpImpl::kLastInputOffset));
4374 __ mov(r3, last_match_info_elements);
4375 __ RecordWrite(r3, Operand(RegExpImpl::kLastInputOffset), r2, r7);
4376
4377 // Get the static offsets vector filled by the native regexp code.
4378 ExternalReference address_of_static_offsets_vector =
Steve Block44f0eee2011-05-26 01:26:41 +01004379 ExternalReference::address_of_static_offsets_vector(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004380 __ mov(r2, Operand(address_of_static_offsets_vector));
4381
4382 // r1: number of capture registers
4383 // r2: offsets vector
4384 Label next_capture, done;
4385 // Capture register counter starts from number of capture registers and
4386 // counts down until wraping after zero.
4387 __ add(r0,
4388 last_match_info_elements,
4389 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
4390 __ bind(&next_capture);
4391 __ sub(r1, r1, Operand(1), SetCC);
4392 __ b(mi, &done);
4393 // Read the value from the static offsets vector buffer.
4394 __ ldr(r3, MemOperand(r2, kPointerSize, PostIndex));
4395 // Store the smi value in the last match info.
4396 __ mov(r3, Operand(r3, LSL, kSmiTagSize));
4397 __ str(r3, MemOperand(r0, kPointerSize, PostIndex));
4398 __ jmp(&next_capture);
4399 __ bind(&done);
4400
4401 // Return last match info.
4402 __ ldr(r0, MemOperand(sp, kLastMatchInfoOffset));
4403 __ add(sp, sp, Operand(4 * kPointerSize));
4404 __ Ret();
4405
4406 // Do the runtime call to execute the regexp.
4407 __ bind(&runtime);
4408 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4409#endif // V8_INTERPRETED_REGEXP
4410}
4411
4412
Ben Murdochb0fe1622011-05-05 13:52:32 +01004413void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
4414 const int kMaxInlineLength = 100;
4415 Label slowcase;
4416 Label done;
Ben Murdoch257744e2011-11-30 15:57:28 +00004417 Factory* factory = masm->isolate()->factory();
4418
Ben Murdochb0fe1622011-05-05 13:52:32 +01004419 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4420 STATIC_ASSERT(kSmiTag == 0);
4421 STATIC_ASSERT(kSmiTagSize == 1);
4422 __ tst(r1, Operand(kSmiTagMask));
4423 __ b(ne, &slowcase);
4424 __ cmp(r1, Operand(Smi::FromInt(kMaxInlineLength)));
4425 __ b(hi, &slowcase);
4426 // Smi-tagging is equivalent to multiplying by 2.
4427 // Allocate RegExpResult followed by FixedArray with size in ebx.
4428 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4429 // Elements: [Map][Length][..elements..]
4430 // Size of JSArray with two in-object properties and the header of a
4431 // FixedArray.
4432 int objects_size =
4433 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
4434 __ mov(r5, Operand(r1, LSR, kSmiTagSize + kSmiShiftSize));
4435 __ add(r2, r5, Operand(objects_size));
4436 __ AllocateInNewSpace(
4437 r2, // In: Size, in words.
4438 r0, // Out: Start of allocation (tagged).
4439 r3, // Scratch register.
4440 r4, // Scratch register.
4441 &slowcase,
4442 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
4443 // r0: Start of allocated area, object-tagged.
4444 // r1: Number of elements in array, as smi.
4445 // r5: Number of elements, untagged.
4446
4447 // Set JSArray map to global.regexp_result_map().
4448 // Set empty properties FixedArray.
4449 // Set elements to point to FixedArray allocated right after the JSArray.
4450 // Interleave operations for better latency.
4451 __ ldr(r2, ContextOperand(cp, Context::GLOBAL_INDEX));
4452 __ add(r3, r0, Operand(JSRegExpResult::kSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00004453 __ mov(r4, Operand(factory->empty_fixed_array()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004454 __ ldr(r2, FieldMemOperand(r2, GlobalObject::kGlobalContextOffset));
4455 __ str(r3, FieldMemOperand(r0, JSObject::kElementsOffset));
4456 __ ldr(r2, ContextOperand(r2, Context::REGEXP_RESULT_MAP_INDEX));
4457 __ str(r4, FieldMemOperand(r0, JSObject::kPropertiesOffset));
4458 __ str(r2, FieldMemOperand(r0, HeapObject::kMapOffset));
4459
4460 // Set input, index and length fields from arguments.
4461 __ ldr(r1, MemOperand(sp, kPointerSize * 0));
4462 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kInputOffset));
4463 __ ldr(r1, MemOperand(sp, kPointerSize * 1));
4464 __ str(r1, FieldMemOperand(r0, JSRegExpResult::kIndexOffset));
4465 __ ldr(r1, MemOperand(sp, kPointerSize * 2));
4466 __ str(r1, FieldMemOperand(r0, JSArray::kLengthOffset));
4467
4468 // Fill out the elements FixedArray.
4469 // r0: JSArray, tagged.
4470 // r3: FixedArray, tagged.
4471 // r5: Number of elements in array, untagged.
4472
4473 // Set map.
Ben Murdoch257744e2011-11-30 15:57:28 +00004474 __ mov(r2, Operand(factory->fixed_array_map()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004475 __ str(r2, FieldMemOperand(r3, HeapObject::kMapOffset));
4476 // Set FixedArray length.
4477 __ mov(r6, Operand(r5, LSL, kSmiTagSize));
4478 __ str(r6, FieldMemOperand(r3, FixedArray::kLengthOffset));
4479 // Fill contents of fixed-array with the-hole.
Ben Murdoch257744e2011-11-30 15:57:28 +00004480 __ mov(r2, Operand(factory->the_hole_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004481 __ add(r3, r3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4482 // Fill fixed array elements with hole.
4483 // r0: JSArray, tagged.
4484 // r2: the hole.
4485 // r3: Start of elements in FixedArray.
4486 // r5: Number of elements to fill.
4487 Label loop;
4488 __ tst(r5, Operand(r5));
4489 __ bind(&loop);
4490 __ b(le, &done); // Jump if r1 is negative or zero.
4491 __ sub(r5, r5, Operand(1), SetCC);
4492 __ str(r2, MemOperand(r3, r5, LSL, kPointerSizeLog2));
4493 __ jmp(&loop);
4494
4495 __ bind(&done);
4496 __ add(sp, sp, Operand(3 * kPointerSize));
4497 __ Ret();
4498
4499 __ bind(&slowcase);
4500 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
4501}
4502
4503
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004504void CallFunctionStub::Generate(MacroAssembler* masm) {
4505 Label slow;
4506
Ben Murdoch257744e2011-11-30 15:57:28 +00004507 // The receiver might implicitly be the global object. This is
4508 // indicated by passing the hole as the receiver to the call
4509 // function stub.
4510 if (ReceiverMightBeImplicit()) {
4511 Label call;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004512 // Get the receiver from the stack.
4513 // function, receiver [, arguments]
Ben Murdoch257744e2011-11-30 15:57:28 +00004514 __ ldr(r4, MemOperand(sp, argc_ * kPointerSize));
4515 // Call as function is indicated with the hole.
4516 __ CompareRoot(r4, Heap::kTheHoleValueRootIndex);
4517 __ b(ne, &call);
4518 // Patch the receiver on the stack with the global receiver object.
4519 __ ldr(r1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4520 __ ldr(r1, FieldMemOperand(r1, GlobalObject::kGlobalReceiverOffset));
4521 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
4522 __ bind(&call);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004523 }
4524
4525 // Get the function to call from the stack.
4526 // function, receiver [, arguments]
4527 __ ldr(r1, MemOperand(sp, (argc_ + 1) * kPointerSize));
4528
4529 // Check that the function is really a JavaScript function.
4530 // r1: pushed function (to be verified)
Steve Block1e0659c2011-05-24 12:43:12 +01004531 __ JumpIfSmi(r1, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004532 // Get the map of the function object.
4533 __ CompareObjectType(r1, r2, r2, JS_FUNCTION_TYPE);
4534 __ b(ne, &slow);
4535
4536 // Fast-case: Invoke the function now.
4537 // r1: pushed function
4538 ParameterCount actual(argc_);
Ben Murdoch257744e2011-11-30 15:57:28 +00004539
4540 if (ReceiverMightBeImplicit()) {
4541 Label call_as_function;
4542 __ CompareRoot(r4, Heap::kTheHoleValueRootIndex);
4543 __ b(eq, &call_as_function);
4544 __ InvokeFunction(r1,
4545 actual,
4546 JUMP_FUNCTION,
4547 NullCallWrapper(),
4548 CALL_AS_METHOD);
4549 __ bind(&call_as_function);
4550 }
4551 __ InvokeFunction(r1,
4552 actual,
4553 JUMP_FUNCTION,
4554 NullCallWrapper(),
4555 CALL_AS_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004556
4557 // Slow-case: Non-function called.
4558 __ bind(&slow);
4559 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4560 // of the original receiver from the call site).
4561 __ str(r1, MemOperand(sp, argc_ * kPointerSize));
4562 __ mov(r0, Operand(argc_)); // Setup the number of arguments.
Iain Merrick9ac36c92010-09-13 15:29:50 +01004563 __ mov(r2, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004564 __ GetBuiltinEntry(r3, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01004565 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004566 RelocInfo::CODE_TARGET);
4567}
4568
4569
4570// Unfortunately you have to run without snapshots to see most of these
4571// names in the profile since most compare stubs end up in the snapshot.
4572const char* CompareStub::GetName() {
4573 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
4574 (lhs_.is(r1) && rhs_.is(r0)));
4575
4576 if (name_ != NULL) return name_;
4577 const int kMaxNameLength = 100;
Steve Block44f0eee2011-05-26 01:26:41 +01004578 name_ = Isolate::Current()->bootstrapper()->AllocateAutoDeletedArray(
4579 kMaxNameLength);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004580 if (name_ == NULL) return "OOM";
4581
4582 const char* cc_name;
4583 switch (cc_) {
4584 case lt: cc_name = "LT"; break;
4585 case gt: cc_name = "GT"; break;
4586 case le: cc_name = "LE"; break;
4587 case ge: cc_name = "GE"; break;
4588 case eq: cc_name = "EQ"; break;
4589 case ne: cc_name = "NE"; break;
4590 default: cc_name = "UnknownCondition"; break;
4591 }
4592
4593 const char* lhs_name = lhs_.is(r0) ? "_r0" : "_r1";
4594 const char* rhs_name = rhs_.is(r0) ? "_r0" : "_r1";
4595
4596 const char* strict_name = "";
4597 if (strict_ && (cc_ == eq || cc_ == ne)) {
4598 strict_name = "_STRICT";
4599 }
4600
4601 const char* never_nan_nan_name = "";
4602 if (never_nan_nan_ && (cc_ == eq || cc_ == ne)) {
4603 never_nan_nan_name = "_NO_NAN";
4604 }
4605
4606 const char* include_number_compare_name = "";
4607 if (!include_number_compare_) {
4608 include_number_compare_name = "_NO_NUMBER";
4609 }
4610
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004611 const char* include_smi_compare_name = "";
4612 if (!include_smi_compare_) {
4613 include_smi_compare_name = "_NO_SMI";
4614 }
4615
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004616 OS::SNPrintF(Vector<char>(name_, kMaxNameLength),
4617 "CompareStub_%s%s%s%s%s%s",
4618 cc_name,
4619 lhs_name,
4620 rhs_name,
4621 strict_name,
4622 never_nan_nan_name,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004623 include_number_compare_name,
4624 include_smi_compare_name);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004625 return name_;
4626}
4627
4628
4629int CompareStub::MinorKey() {
4630 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4631 // stubs the never NaN NaN condition is only taken into account if the
4632 // condition is equals.
4633 ASSERT((static_cast<unsigned>(cc_) >> 28) < (1 << 12));
4634 ASSERT((lhs_.is(r0) && rhs_.is(r1)) ||
4635 (lhs_.is(r1) && rhs_.is(r0)));
4636 return ConditionField::encode(static_cast<unsigned>(cc_) >> 28)
4637 | RegisterField::encode(lhs_.is(r0))
4638 | StrictField::encode(strict_)
4639 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004640 | IncludeNumberCompareField::encode(include_number_compare_)
4641 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004642}
4643
4644
4645// StringCharCodeAtGenerator
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004646void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4647 Label flat_string;
4648 Label ascii_string;
4649 Label got_char_code;
4650
4651 // If the receiver is a smi trigger the non-string case.
Steve Block1e0659c2011-05-24 12:43:12 +01004652 __ JumpIfSmi(object_, receiver_not_string_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004653
4654 // Fetch the instance type of the receiver into result register.
4655 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4656 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4657 // If the receiver is not a string trigger the non-string case.
4658 __ tst(result_, Operand(kIsNotStringMask));
4659 __ b(ne, receiver_not_string_);
4660
4661 // If the index is non-smi trigger the non-smi case.
Steve Block1e0659c2011-05-24 12:43:12 +01004662 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004663
4664 // Put smi-tagged index into scratch register.
4665 __ mov(scratch_, index_);
4666 __ bind(&got_smi_index_);
4667
4668 // Check for index out of range.
4669 __ ldr(ip, FieldMemOperand(object_, String::kLengthOffset));
4670 __ cmp(ip, Operand(scratch_));
4671 __ b(ls, index_out_of_range_);
4672
4673 // We need special handling for non-flat strings.
4674 STATIC_ASSERT(kSeqStringTag == 0);
4675 __ tst(result_, Operand(kStringRepresentationMask));
4676 __ b(eq, &flat_string);
4677
4678 // Handle non-flat strings.
4679 __ tst(result_, Operand(kIsConsStringMask));
4680 __ b(eq, &call_runtime_);
4681
4682 // ConsString.
4683 // Check whether the right hand side is the empty string (i.e. if
4684 // this is really a flat string in a cons string). If that is not
4685 // the case we would rather go to the runtime system now to flatten
4686 // the string.
4687 __ ldr(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
4688 __ LoadRoot(ip, Heap::kEmptyStringRootIndex);
4689 __ cmp(result_, Operand(ip));
4690 __ b(ne, &call_runtime_);
4691 // Get the first of the two strings and load its instance type.
4692 __ ldr(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
4693 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4694 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4695 // If the first cons component is also non-flat, then go to runtime.
4696 STATIC_ASSERT(kSeqStringTag == 0);
4697 __ tst(result_, Operand(kStringRepresentationMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004698 __ b(ne, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004699
4700 // Check for 1-byte or 2-byte string.
4701 __ bind(&flat_string);
4702 STATIC_ASSERT(kAsciiStringTag != 0);
4703 __ tst(result_, Operand(kStringEncodingMask));
Steve Block1e0659c2011-05-24 12:43:12 +01004704 __ b(ne, &ascii_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004705
4706 // 2-byte string.
4707 // Load the 2-byte character code into the result register. We can
4708 // add without shifting since the smi tag size is the log2 of the
4709 // number of bytes in a two-byte character.
4710 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
4711 __ add(scratch_, object_, Operand(scratch_));
4712 __ ldrh(result_, FieldMemOperand(scratch_, SeqTwoByteString::kHeaderSize));
4713 __ jmp(&got_char_code);
4714
4715 // ASCII string.
4716 // Load the byte into the result register.
4717 __ bind(&ascii_string);
4718 __ add(scratch_, object_, Operand(scratch_, LSR, kSmiTagSize));
4719 __ ldrb(result_, FieldMemOperand(scratch_, SeqAsciiString::kHeaderSize));
4720
4721 __ bind(&got_char_code);
4722 __ mov(result_, Operand(result_, LSL, kSmiTagSize));
4723 __ bind(&exit_);
4724}
4725
4726
4727void StringCharCodeAtGenerator::GenerateSlow(
4728 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4729 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4730
4731 // Index is not a smi.
4732 __ bind(&index_not_smi_);
4733 // If index is a heap number, try converting it to an integer.
4734 __ CheckMap(index_,
4735 scratch_,
4736 Heap::kHeapNumberMapRootIndex,
4737 index_not_number_,
Ben Murdoch257744e2011-11-30 15:57:28 +00004738 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004739 call_helper.BeforeCall(masm);
4740 __ Push(object_, index_);
4741 __ push(index_); // Consumed by runtime conversion function.
4742 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4743 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4744 } else {
4745 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4746 // NumberToSmi discards numbers that are not exact integers.
4747 __ CallRuntime(Runtime::kNumberToSmi, 1);
4748 }
4749 // Save the conversion result before the pop instructions below
4750 // have a chance to overwrite it.
4751 __ Move(scratch_, r0);
4752 __ pop(index_);
4753 __ pop(object_);
4754 // Reload the instance type.
4755 __ ldr(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
4756 __ ldrb(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
4757 call_helper.AfterCall(masm);
4758 // If index is still not a smi, it must be out of range.
Steve Block1e0659c2011-05-24 12:43:12 +01004759 __ JumpIfNotSmi(scratch_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004760 // Otherwise, return to the fast path.
4761 __ jmp(&got_smi_index_);
4762
4763 // Call runtime. We get here when the receiver is a string and the
4764 // index is a number, but the code of getting the actual character
4765 // is too complex (e.g., when the string needs to be flattened).
4766 __ bind(&call_runtime_);
4767 call_helper.BeforeCall(masm);
4768 __ Push(object_, index_);
4769 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4770 __ Move(result_, r0);
4771 call_helper.AfterCall(masm);
4772 __ jmp(&exit_);
4773
4774 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4775}
4776
4777
4778// -------------------------------------------------------------------------
4779// StringCharFromCodeGenerator
4780
4781void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4782 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4783 STATIC_ASSERT(kSmiTag == 0);
4784 STATIC_ASSERT(kSmiShiftSize == 0);
4785 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
4786 __ tst(code_,
4787 Operand(kSmiTagMask |
4788 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
Steve Block1e0659c2011-05-24 12:43:12 +01004789 __ b(ne, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004790
4791 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
Steve Block44f0eee2011-05-26 01:26:41 +01004792 // At this point code register contains smi tagged ASCII char code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004793 STATIC_ASSERT(kSmiTag == 0);
4794 __ add(result_, result_, Operand(code_, LSL, kPointerSizeLog2 - kSmiTagSize));
4795 __ ldr(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
4796 __ LoadRoot(ip, Heap::kUndefinedValueRootIndex);
4797 __ cmp(result_, Operand(ip));
4798 __ b(eq, &slow_case_);
4799 __ bind(&exit_);
4800}
4801
4802
4803void StringCharFromCodeGenerator::GenerateSlow(
4804 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4805 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4806
4807 __ bind(&slow_case_);
4808 call_helper.BeforeCall(masm);
4809 __ push(code_);
4810 __ CallRuntime(Runtime::kCharFromCode, 1);
4811 __ Move(result_, r0);
4812 call_helper.AfterCall(masm);
4813 __ jmp(&exit_);
4814
4815 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4816}
4817
4818
4819// -------------------------------------------------------------------------
4820// StringCharAtGenerator
4821
4822void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4823 char_code_at_generator_.GenerateFast(masm);
4824 char_from_code_generator_.GenerateFast(masm);
4825}
4826
4827
4828void StringCharAtGenerator::GenerateSlow(
4829 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4830 char_code_at_generator_.GenerateSlow(masm, call_helper);
4831 char_from_code_generator_.GenerateSlow(masm, call_helper);
4832}
4833
4834
4835class StringHelper : public AllStatic {
4836 public:
4837 // Generate code for copying characters using a simple loop. This should only
4838 // be used in places where the number of characters is small and the
4839 // additional setup and checking in GenerateCopyCharactersLong adds too much
4840 // overhead. Copying of overlapping regions is not supported.
4841 // Dest register ends at the position after the last character written.
4842 static void GenerateCopyCharacters(MacroAssembler* masm,
4843 Register dest,
4844 Register src,
4845 Register count,
4846 Register scratch,
4847 bool ascii);
4848
4849 // Generate code for copying a large number of characters. This function
4850 // is allowed to spend extra time setting up conditions to make copying
4851 // faster. Copying of overlapping regions is not supported.
4852 // Dest register ends at the position after the last character written.
4853 static void GenerateCopyCharactersLong(MacroAssembler* masm,
4854 Register dest,
4855 Register src,
4856 Register count,
4857 Register scratch1,
4858 Register scratch2,
4859 Register scratch3,
4860 Register scratch4,
4861 Register scratch5,
4862 int flags);
4863
4864
4865 // Probe the symbol table for a two character string. If the string is
4866 // not found by probing a jump to the label not_found is performed. This jump
4867 // does not guarantee that the string is not in the symbol table. If the
4868 // string is found the code falls through with the string in register r0.
4869 // Contents of both c1 and c2 registers are modified. At the exit c1 is
4870 // guaranteed to contain halfword with low and high bytes equal to
4871 // initial contents of c1 and c2 respectively.
4872 static void GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4873 Register c1,
4874 Register c2,
4875 Register scratch1,
4876 Register scratch2,
4877 Register scratch3,
4878 Register scratch4,
4879 Register scratch5,
4880 Label* not_found);
4881
4882 // Generate string hash.
4883 static void GenerateHashInit(MacroAssembler* masm,
4884 Register hash,
4885 Register character);
4886
4887 static void GenerateHashAddCharacter(MacroAssembler* masm,
4888 Register hash,
4889 Register character);
4890
4891 static void GenerateHashGetHash(MacroAssembler* masm,
4892 Register hash);
4893
4894 private:
4895 DISALLOW_IMPLICIT_CONSTRUCTORS(StringHelper);
4896};
4897
4898
4899void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
4900 Register dest,
4901 Register src,
4902 Register count,
4903 Register scratch,
4904 bool ascii) {
4905 Label loop;
4906 Label done;
4907 // This loop just copies one character at a time, as it is only used for very
4908 // short strings.
4909 if (!ascii) {
4910 __ add(count, count, Operand(count), SetCC);
4911 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01004912 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004913 }
4914 __ b(eq, &done);
4915
4916 __ bind(&loop);
4917 __ ldrb(scratch, MemOperand(src, 1, PostIndex));
4918 // Perform sub between load and dependent store to get the load time to
4919 // complete.
4920 __ sub(count, count, Operand(1), SetCC);
4921 __ strb(scratch, MemOperand(dest, 1, PostIndex));
4922 // last iteration.
4923 __ b(gt, &loop);
4924
4925 __ bind(&done);
4926}
4927
4928
4929enum CopyCharactersFlags {
4930 COPY_ASCII = 1,
4931 DEST_ALWAYS_ALIGNED = 2
4932};
4933
4934
4935void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
4936 Register dest,
4937 Register src,
4938 Register count,
4939 Register scratch1,
4940 Register scratch2,
4941 Register scratch3,
4942 Register scratch4,
4943 Register scratch5,
4944 int flags) {
4945 bool ascii = (flags & COPY_ASCII) != 0;
4946 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
4947
4948 if (dest_always_aligned && FLAG_debug_code) {
4949 // Check that destination is actually word aligned if the flag says
4950 // that it is.
4951 __ tst(dest, Operand(kPointerAlignmentMask));
4952 __ Check(eq, "Destination of copy not aligned.");
4953 }
4954
4955 const int kReadAlignment = 4;
4956 const int kReadAlignmentMask = kReadAlignment - 1;
4957 // Ensure that reading an entire aligned word containing the last character
4958 // of a string will not read outside the allocated area (because we pad up
4959 // to kObjectAlignment).
4960 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
4961 // Assumes word reads and writes are little endian.
4962 // Nothing to do for zero characters.
4963 Label done;
4964 if (!ascii) {
4965 __ add(count, count, Operand(count), SetCC);
4966 } else {
Iain Merrick9ac36c92010-09-13 15:29:50 +01004967 __ cmp(count, Operand(0, RelocInfo::NONE));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004968 }
4969 __ b(eq, &done);
4970
4971 // Assume that you cannot read (or write) unaligned.
4972 Label byte_loop;
4973 // Must copy at least eight bytes, otherwise just do it one byte at a time.
4974 __ cmp(count, Operand(8));
4975 __ add(count, dest, Operand(count));
4976 Register limit = count; // Read until src equals this.
4977 __ b(lt, &byte_loop);
4978
4979 if (!dest_always_aligned) {
4980 // Align dest by byte copying. Copies between zero and three bytes.
4981 __ and_(scratch4, dest, Operand(kReadAlignmentMask), SetCC);
4982 Label dest_aligned;
4983 __ b(eq, &dest_aligned);
4984 __ cmp(scratch4, Operand(2));
4985 __ ldrb(scratch1, MemOperand(src, 1, PostIndex));
4986 __ ldrb(scratch2, MemOperand(src, 1, PostIndex), le);
4987 __ ldrb(scratch3, MemOperand(src, 1, PostIndex), lt);
4988 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
4989 __ strb(scratch2, MemOperand(dest, 1, PostIndex), le);
4990 __ strb(scratch3, MemOperand(dest, 1, PostIndex), lt);
4991 __ bind(&dest_aligned);
4992 }
4993
4994 Label simple_loop;
4995
4996 __ sub(scratch4, dest, Operand(src));
4997 __ and_(scratch4, scratch4, Operand(0x03), SetCC);
4998 __ b(eq, &simple_loop);
4999 // Shift register is number of bits in a source word that
5000 // must be combined with bits in the next source word in order
5001 // to create a destination word.
5002
5003 // Complex loop for src/dst that are not aligned the same way.
5004 {
5005 Label loop;
5006 __ mov(scratch4, Operand(scratch4, LSL, 3));
5007 Register left_shift = scratch4;
5008 __ and_(src, src, Operand(~3)); // Round down to load previous word.
5009 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
5010 // Store the "shift" most significant bits of scratch in the least
5011 // signficant bits (i.e., shift down by (32-shift)).
5012 __ rsb(scratch2, left_shift, Operand(32));
5013 Register right_shift = scratch2;
5014 __ mov(scratch1, Operand(scratch1, LSR, right_shift));
5015
5016 __ bind(&loop);
5017 __ ldr(scratch3, MemOperand(src, 4, PostIndex));
5018 __ sub(scratch5, limit, Operand(dest));
5019 __ orr(scratch1, scratch1, Operand(scratch3, LSL, left_shift));
5020 __ str(scratch1, MemOperand(dest, 4, PostIndex));
5021 __ mov(scratch1, Operand(scratch3, LSR, right_shift));
5022 // Loop if four or more bytes left to copy.
5023 // Compare to eight, because we did the subtract before increasing dst.
5024 __ sub(scratch5, scratch5, Operand(8), SetCC);
5025 __ b(ge, &loop);
5026 }
5027 // There is now between zero and three bytes left to copy (negative that
5028 // number is in scratch5), and between one and three bytes already read into
5029 // scratch1 (eight times that number in scratch4). We may have read past
5030 // the end of the string, but because objects are aligned, we have not read
5031 // past the end of the object.
5032 // Find the minimum of remaining characters to move and preloaded characters
5033 // and write those as bytes.
5034 __ add(scratch5, scratch5, Operand(4), SetCC);
5035 __ b(eq, &done);
5036 __ cmp(scratch4, Operand(scratch5, LSL, 3), ne);
5037 // Move minimum of bytes read and bytes left to copy to scratch4.
5038 __ mov(scratch5, Operand(scratch4, LSR, 3), LeaveCC, lt);
5039 // Between one and three (value in scratch5) characters already read into
5040 // scratch ready to write.
5041 __ cmp(scratch5, Operand(2));
5042 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5043 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, ge);
5044 __ strb(scratch1, MemOperand(dest, 1, PostIndex), ge);
5045 __ mov(scratch1, Operand(scratch1, LSR, 8), LeaveCC, gt);
5046 __ strb(scratch1, MemOperand(dest, 1, PostIndex), gt);
5047 // Copy any remaining bytes.
5048 __ b(&byte_loop);
5049
5050 // Simple loop.
5051 // Copy words from src to dst, until less than four bytes left.
5052 // Both src and dest are word aligned.
5053 __ bind(&simple_loop);
5054 {
5055 Label loop;
5056 __ bind(&loop);
5057 __ ldr(scratch1, MemOperand(src, 4, PostIndex));
5058 __ sub(scratch3, limit, Operand(dest));
5059 __ str(scratch1, MemOperand(dest, 4, PostIndex));
5060 // Compare to 8, not 4, because we do the substraction before increasing
5061 // dest.
5062 __ cmp(scratch3, Operand(8));
5063 __ b(ge, &loop);
5064 }
5065
5066 // Copy bytes from src to dst until dst hits limit.
5067 __ bind(&byte_loop);
5068 __ cmp(dest, Operand(limit));
5069 __ ldrb(scratch1, MemOperand(src, 1, PostIndex), lt);
5070 __ b(ge, &done);
5071 __ strb(scratch1, MemOperand(dest, 1, PostIndex));
5072 __ b(&byte_loop);
5073
5074 __ bind(&done);
5075}
5076
5077
5078void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5079 Register c1,
5080 Register c2,
5081 Register scratch1,
5082 Register scratch2,
5083 Register scratch3,
5084 Register scratch4,
5085 Register scratch5,
5086 Label* not_found) {
5087 // Register scratch3 is the general scratch register in this function.
5088 Register scratch = scratch3;
5089
5090 // Make sure that both characters are not digits as such strings has a
5091 // different hash algorithm. Don't try to look for these in the symbol table.
5092 Label not_array_index;
5093 __ sub(scratch, c1, Operand(static_cast<int>('0')));
5094 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
5095 __ b(hi, &not_array_index);
5096 __ sub(scratch, c2, Operand(static_cast<int>('0')));
5097 __ cmp(scratch, Operand(static_cast<int>('9' - '0')));
5098
5099 // If check failed combine both characters into single halfword.
5100 // This is required by the contract of the method: code at the
5101 // not_found branch expects this combination in c1 register
5102 __ orr(c1, c1, Operand(c2, LSL, kBitsPerByte), LeaveCC, ls);
5103 __ b(ls, not_found);
5104
5105 __ bind(&not_array_index);
5106 // Calculate the two character string hash.
5107 Register hash = scratch1;
5108 StringHelper::GenerateHashInit(masm, hash, c1);
5109 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
5110 StringHelper::GenerateHashGetHash(masm, hash);
5111
5112 // Collect the two characters in a register.
5113 Register chars = c1;
5114 __ orr(chars, chars, Operand(c2, LSL, kBitsPerByte));
5115
5116 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5117 // hash: hash of two character string.
5118
5119 // Load symbol table
5120 // Load address of first element of the symbol table.
5121 Register symbol_table = c2;
5122 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
5123
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005124 Register undefined = scratch4;
5125 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
5126
5127 // Calculate capacity mask from the symbol table capacity.
5128 Register mask = scratch2;
5129 __ ldr(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
5130 __ mov(mask, Operand(mask, ASR, 1));
5131 __ sub(mask, mask, Operand(1));
5132
5133 // Calculate untagged address of the first element of the symbol table.
5134 Register first_symbol_table_element = symbol_table;
5135 __ add(first_symbol_table_element, symbol_table,
5136 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
5137
5138 // Registers
5139 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5140 // hash: hash of two character string
5141 // mask: capacity mask
5142 // first_symbol_table_element: address of the first element of
5143 // the symbol table
Steve Block44f0eee2011-05-26 01:26:41 +01005144 // undefined: the undefined object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005145 // scratch: -
5146
5147 // Perform a number of probes in the symbol table.
5148 static const int kProbes = 4;
5149 Label found_in_symbol_table;
5150 Label next_probe[kProbes];
5151 for (int i = 0; i < kProbes; i++) {
5152 Register candidate = scratch5; // Scratch register contains candidate.
5153
5154 // Calculate entry in symbol table.
5155 if (i > 0) {
5156 __ add(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
5157 } else {
5158 __ mov(candidate, hash);
5159 }
5160
5161 __ and_(candidate, candidate, Operand(mask));
5162
5163 // Load the entry from the symble table.
5164 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5165 __ ldr(candidate,
5166 MemOperand(first_symbol_table_element,
5167 candidate,
5168 LSL,
5169 kPointerSizeLog2));
5170
5171 // If entry is undefined no string with this hash can be found.
Steve Block44f0eee2011-05-26 01:26:41 +01005172 Label is_string;
5173 __ CompareObjectType(candidate, scratch, scratch, ODDBALL_TYPE);
5174 __ b(ne, &is_string);
5175
5176 __ cmp(undefined, candidate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005177 __ b(eq, not_found);
Steve Block44f0eee2011-05-26 01:26:41 +01005178 // Must be null (deleted entry).
5179 if (FLAG_debug_code) {
5180 __ LoadRoot(ip, Heap::kNullValueRootIndex);
5181 __ cmp(ip, candidate);
5182 __ Assert(eq, "oddball in symbol table is not undefined or null");
5183 }
5184 __ jmp(&next_probe[i]);
5185
5186 __ bind(&is_string);
5187
5188 // Check that the candidate is a non-external ASCII string. The instance
5189 // type is still in the scratch register from the CompareObjectType
5190 // operation.
5191 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, &next_probe[i]);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005192
5193 // If length is not 2 the string is not a candidate.
5194 __ ldr(scratch, FieldMemOperand(candidate, String::kLengthOffset));
5195 __ cmp(scratch, Operand(Smi::FromInt(2)));
5196 __ b(ne, &next_probe[i]);
5197
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005198 // Check if the two characters match.
5199 // Assumes that word load is little endian.
5200 __ ldrh(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
5201 __ cmp(chars, scratch);
5202 __ b(eq, &found_in_symbol_table);
5203 __ bind(&next_probe[i]);
5204 }
5205
5206 // No matching 2 character string found by probing.
5207 __ jmp(not_found);
5208
5209 // Scratch register contains result when we fall through to here.
5210 Register result = scratch;
5211 __ bind(&found_in_symbol_table);
5212 __ Move(r0, result);
5213}
5214
5215
5216void StringHelper::GenerateHashInit(MacroAssembler* masm,
5217 Register hash,
5218 Register character) {
5219 // hash = character + (character << 10);
5220 __ add(hash, character, Operand(character, LSL, 10));
5221 // hash ^= hash >> 6;
5222 __ eor(hash, hash, Operand(hash, ASR, 6));
5223}
5224
5225
5226void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5227 Register hash,
5228 Register character) {
5229 // hash += character;
5230 __ add(hash, hash, Operand(character));
5231 // hash += hash << 10;
5232 __ add(hash, hash, Operand(hash, LSL, 10));
5233 // hash ^= hash >> 6;
5234 __ eor(hash, hash, Operand(hash, ASR, 6));
5235}
5236
5237
5238void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5239 Register hash) {
5240 // hash += hash << 3;
5241 __ add(hash, hash, Operand(hash, LSL, 3));
5242 // hash ^= hash >> 11;
5243 __ eor(hash, hash, Operand(hash, ASR, 11));
5244 // hash += hash << 15;
5245 __ add(hash, hash, Operand(hash, LSL, 15), SetCC);
5246
5247 // if (hash == 0) hash = 27;
Steve Block1e0659c2011-05-24 12:43:12 +01005248 __ mov(hash, Operand(27), LeaveCC, ne);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005249}
5250
5251
5252void SubStringStub::Generate(MacroAssembler* masm) {
5253 Label runtime;
5254
5255 // Stack frame on entry.
5256 // lr: return address
5257 // sp[0]: to
5258 // sp[4]: from
5259 // sp[8]: string
5260
5261 // This stub is called from the native-call %_SubString(...), so
5262 // nothing can be assumed about the arguments. It is tested that:
5263 // "string" is a sequential string,
5264 // both "from" and "to" are smis, and
5265 // 0 <= from <= to <= string.length.
5266 // If any of these assumptions fail, we call the runtime system.
5267
5268 static const int kToOffset = 0 * kPointerSize;
5269 static const int kFromOffset = 1 * kPointerSize;
5270 static const int kStringOffset = 2 * kPointerSize;
5271
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005272 // Check bounds and smi-ness.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005273 Register to = r6;
5274 Register from = r7;
5275 __ Ldrd(to, from, MemOperand(sp, kToOffset));
5276 STATIC_ASSERT(kFromOffset == kToOffset + 4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005277 STATIC_ASSERT(kSmiTag == 0);
5278 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5279 // I.e., arithmetic shift right by one un-smi-tags.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005280 __ mov(r2, Operand(to, ASR, 1), SetCC);
5281 __ mov(r3, Operand(from, ASR, 1), SetCC, cc);
5282 // If either to or from had the smi tag bit set, then carry is set now.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005283 __ b(cs, &runtime); // Either "from" or "to" is not a smi.
5284 __ b(mi, &runtime); // From is negative.
5285
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005286 // Both to and from are smis.
5287
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005288 __ sub(r2, r2, Operand(r3), SetCC);
5289 __ b(mi, &runtime); // Fail if from > to.
5290 // Special handling of sub-strings of length 1 and 2. One character strings
5291 // are handled in the runtime system (looked up in the single character
5292 // cache). Two character strings are looked for in the symbol cache.
5293 __ cmp(r2, Operand(2));
5294 __ b(lt, &runtime);
5295
5296 // r2: length
5297 // r3: from index (untaged smi)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005298 // r6 (a.k.a. to): to (smi)
5299 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005300
5301 // Make sure first argument is a sequential (or flat) string.
5302 __ ldr(r5, MemOperand(sp, kStringOffset));
5303 STATIC_ASSERT(kSmiTag == 0);
5304 __ tst(r5, Operand(kSmiTagMask));
5305 __ b(eq, &runtime);
5306 Condition is_string = masm->IsObjectStringType(r5, r1);
5307 __ b(NegateCondition(is_string), &runtime);
5308
5309 // r1: instance type
5310 // r2: length
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005311 // r3: from index (untagged smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005312 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005313 // r6 (a.k.a. to): to (smi)
5314 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005315 Label seq_string;
5316 __ and_(r4, r1, Operand(kStringRepresentationMask));
5317 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
5318 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
5319 __ cmp(r4, Operand(kConsStringTag));
5320 __ b(gt, &runtime); // External strings go to runtime.
5321 __ b(lt, &seq_string); // Sequential strings are handled directly.
5322
5323 // Cons string. Try to recurse (once) on the first substring.
5324 // (This adds a little more generality than necessary to handle flattened
5325 // cons strings, but not much).
5326 __ ldr(r5, FieldMemOperand(r5, ConsString::kFirstOffset));
5327 __ ldr(r4, FieldMemOperand(r5, HeapObject::kMapOffset));
5328 __ ldrb(r1, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5329 __ tst(r1, Operand(kStringRepresentationMask));
5330 STATIC_ASSERT(kSeqStringTag == 0);
5331 __ b(ne, &runtime); // Cons and External strings go to runtime.
5332
5333 // Definitly a sequential string.
5334 __ bind(&seq_string);
5335
5336 // r1: instance type.
5337 // r2: length
5338 // r3: from index (untaged smi)
5339 // r5: string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005340 // r6 (a.k.a. to): to (smi)
5341 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005342 __ ldr(r4, FieldMemOperand(r5, String::kLengthOffset));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005343 __ cmp(r4, Operand(to));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005344 __ b(lt, &runtime); // Fail if to > length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005345 to = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005346
5347 // r1: instance type.
5348 // r2: result string length.
5349 // r3: from index (untaged smi)
5350 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005351 // r7 (a.k.a. from): from offset (smi)
Steve Block44f0eee2011-05-26 01:26:41 +01005352 // Check for flat ASCII string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005353 Label non_ascii_flat;
5354 __ tst(r1, Operand(kStringEncodingMask));
5355 STATIC_ASSERT(kTwoByteStringTag == 0);
5356 __ b(eq, &non_ascii_flat);
5357
5358 Label result_longer_than_two;
5359 __ cmp(r2, Operand(2));
5360 __ b(gt, &result_longer_than_two);
5361
5362 // Sub string of length 2 requested.
5363 // Get the two characters forming the sub string.
5364 __ add(r5, r5, Operand(r3));
5365 __ ldrb(r3, FieldMemOperand(r5, SeqAsciiString::kHeaderSize));
5366 __ ldrb(r4, FieldMemOperand(r5, SeqAsciiString::kHeaderSize + 1));
5367
5368 // Try to lookup two character string in symbol table.
5369 Label make_two_character_string;
5370 StringHelper::GenerateTwoCharacterSymbolTableProbe(
5371 masm, r3, r4, r1, r5, r6, r7, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01005372 Counters* counters = masm->isolate()->counters();
5373 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005374 __ add(sp, sp, Operand(3 * kPointerSize));
5375 __ Ret();
5376
5377 // r2: result string length.
5378 // r3: two characters combined into halfword in little endian byte order.
5379 __ bind(&make_two_character_string);
5380 __ AllocateAsciiString(r0, r2, r4, r5, r9, &runtime);
5381 __ strh(r3, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005382 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005383 __ add(sp, sp, Operand(3 * kPointerSize));
5384 __ Ret();
5385
5386 __ bind(&result_longer_than_two);
5387
5388 // Allocate the result.
5389 __ AllocateAsciiString(r0, r2, r3, r4, r1, &runtime);
5390
5391 // r0: result string.
5392 // r2: result string length.
5393 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005394 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005395 // Locate first character of result.
5396 __ add(r1, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5397 // Locate 'from' character of string.
5398 __ add(r5, r5, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005399 __ add(r5, r5, Operand(from, ASR, 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005400
5401 // r0: result string.
5402 // r1: first character of result string.
5403 // r2: result string length.
5404 // r5: first character of sub string to copy.
5405 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
5406 StringHelper::GenerateCopyCharactersLong(masm, r1, r5, r2, r3, r4, r6, r7, r9,
5407 COPY_ASCII | DEST_ALWAYS_ALIGNED);
Steve Block44f0eee2011-05-26 01:26:41 +01005408 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005409 __ add(sp, sp, Operand(3 * kPointerSize));
5410 __ Ret();
5411
5412 __ bind(&non_ascii_flat);
5413 // r2: result string length.
5414 // r5: string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005415 // r7 (a.k.a. from): from offset (smi)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005416 // Check for flat two byte string.
5417
5418 // Allocate the result.
5419 __ AllocateTwoByteString(r0, r2, r1, r3, r4, &runtime);
5420
5421 // r0: result string.
5422 // r2: result string length.
5423 // r5: string.
5424 // Locate first character of result.
5425 __ add(r1, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5426 // Locate 'from' character of string.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005427 __ add(r5, r5, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005428 // As "from" is a smi it is 2 times the value which matches the size of a two
5429 // byte character.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005430 __ add(r5, r5, Operand(from));
5431 from = no_reg;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005432
5433 // r0: result string.
5434 // r1: first character of result.
5435 // r2: result length.
5436 // r5: first character of string to copy.
5437 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005438 StringHelper::GenerateCopyCharactersLong(
5439 masm, r1, r5, r2, r3, r4, r6, r7, r9, DEST_ALWAYS_ALIGNED);
Steve Block44f0eee2011-05-26 01:26:41 +01005440 __ IncrementCounter(counters->sub_string_native(), 1, r3, r4);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005441 __ add(sp, sp, Operand(3 * kPointerSize));
5442 __ Ret();
5443
5444 // Just jump to runtime to create the sub string.
5445 __ bind(&runtime);
5446 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5447}
5448
5449
Ben Murdoch257744e2011-11-30 15:57:28 +00005450void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5451 Register left,
5452 Register right,
5453 Register scratch1,
5454 Register scratch2,
5455 Register scratch3) {
5456 Register length = scratch1;
5457
5458 // Compare lengths.
5459 Label strings_not_equal, check_zero_length;
5460 __ ldr(length, FieldMemOperand(left, String::kLengthOffset));
5461 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
5462 __ cmp(length, scratch2);
5463 __ b(eq, &check_zero_length);
5464 __ bind(&strings_not_equal);
5465 __ mov(r0, Operand(Smi::FromInt(NOT_EQUAL)));
5466 __ Ret();
5467
5468 // Check if the length is zero.
5469 Label compare_chars;
5470 __ bind(&check_zero_length);
5471 STATIC_ASSERT(kSmiTag == 0);
5472 __ tst(length, Operand(length));
5473 __ b(ne, &compare_chars);
5474 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
5475 __ Ret();
5476
5477 // Compare characters.
5478 __ bind(&compare_chars);
5479 GenerateAsciiCharsCompareLoop(masm,
5480 left, right, length, scratch2, scratch3,
5481 &strings_not_equal);
5482
5483 // Characters are equal.
5484 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
5485 __ Ret();
5486}
5487
5488
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005489void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5490 Register left,
5491 Register right,
5492 Register scratch1,
5493 Register scratch2,
5494 Register scratch3,
5495 Register scratch4) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005496 Label result_not_equal, compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005497 // Find minimum length and length difference.
5498 __ ldr(scratch1, FieldMemOperand(left, String::kLengthOffset));
5499 __ ldr(scratch2, FieldMemOperand(right, String::kLengthOffset));
5500 __ sub(scratch3, scratch1, Operand(scratch2), SetCC);
5501 Register length_delta = scratch3;
5502 __ mov(scratch1, scratch2, LeaveCC, gt);
5503 Register min_length = scratch1;
5504 STATIC_ASSERT(kSmiTag == 0);
5505 __ tst(min_length, Operand(min_length));
5506 __ b(eq, &compare_lengths);
5507
Ben Murdoch257744e2011-11-30 15:57:28 +00005508 // Compare loop.
5509 GenerateAsciiCharsCompareLoop(masm,
5510 left, right, min_length, scratch2, scratch4,
5511 &result_not_equal);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005512
Ben Murdoch257744e2011-11-30 15:57:28 +00005513 // Compare lengths - strings up to min-length are equal.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005514 __ bind(&compare_lengths);
5515 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00005516 // Use length_delta as result if it's zero.
5517 __ mov(r0, Operand(length_delta), SetCC);
5518 __ bind(&result_not_equal);
5519 // Conditionally update the result based either on length_delta or
5520 // the last comparion performed in the loop above.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005521 __ mov(r0, Operand(Smi::FromInt(GREATER)), LeaveCC, gt);
5522 __ mov(r0, Operand(Smi::FromInt(LESS)), LeaveCC, lt);
5523 __ Ret();
5524}
5525
5526
Ben Murdoch257744e2011-11-30 15:57:28 +00005527void StringCompareStub::GenerateAsciiCharsCompareLoop(
5528 MacroAssembler* masm,
5529 Register left,
5530 Register right,
5531 Register length,
5532 Register scratch1,
5533 Register scratch2,
5534 Label* chars_not_equal) {
5535 // Change index to run from -length to -1 by adding length to string
5536 // start. This means that loop ends when index reaches zero, which
5537 // doesn't need an additional compare.
5538 __ SmiUntag(length);
5539 __ add(scratch1, length,
5540 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5541 __ add(left, left, Operand(scratch1));
5542 __ add(right, right, Operand(scratch1));
5543 __ rsb(length, length, Operand(0));
5544 Register index = length; // index = -length;
5545
5546 // Compare loop.
5547 Label loop;
5548 __ bind(&loop);
5549 __ ldrb(scratch1, MemOperand(left, index));
5550 __ ldrb(scratch2, MemOperand(right, index));
5551 __ cmp(scratch1, scratch2);
5552 __ b(ne, chars_not_equal);
5553 __ add(index, index, Operand(1), SetCC);
5554 __ b(ne, &loop);
5555}
5556
5557
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005558void StringCompareStub::Generate(MacroAssembler* masm) {
5559 Label runtime;
5560
Steve Block44f0eee2011-05-26 01:26:41 +01005561 Counters* counters = masm->isolate()->counters();
5562
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005563 // Stack frame on entry.
5564 // sp[0]: right string
5565 // sp[4]: left string
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005566 __ Ldrd(r0 , r1, MemOperand(sp)); // Load right in r0, left in r1.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005567
5568 Label not_same;
5569 __ cmp(r0, r1);
5570 __ b(ne, &not_same);
5571 STATIC_ASSERT(EQUAL == 0);
5572 STATIC_ASSERT(kSmiTag == 0);
5573 __ mov(r0, Operand(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01005574 __ IncrementCounter(counters->string_compare_native(), 1, r1, r2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005575 __ add(sp, sp, Operand(2 * kPointerSize));
5576 __ Ret();
5577
5578 __ bind(&not_same);
5579
Steve Block44f0eee2011-05-26 01:26:41 +01005580 // Check that both objects are sequential ASCII strings.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005581 __ JumpIfNotBothSequentialAsciiStrings(r1, r0, r2, r3, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005582
Steve Block44f0eee2011-05-26 01:26:41 +01005583 // Compare flat ASCII strings natively. Remove arguments from stack first.
5584 __ IncrementCounter(counters->string_compare_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005585 __ add(sp, sp, Operand(2 * kPointerSize));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005586 GenerateCompareFlatAsciiStrings(masm, r1, r0, r2, r3, r4, r5);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005587
5588 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5589 // tagged as a small integer.
5590 __ bind(&runtime);
5591 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5592}
5593
5594
5595void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005596 Label string_add_runtime, call_builtin;
5597 Builtins::JavaScript builtin_id = Builtins::ADD;
5598
Steve Block44f0eee2011-05-26 01:26:41 +01005599 Counters* counters = masm->isolate()->counters();
5600
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005601 // Stack on entry:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005602 // sp[0]: second argument (right).
5603 // sp[4]: first argument (left).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005604
5605 // Load the two arguments.
5606 __ ldr(r0, MemOperand(sp, 1 * kPointerSize)); // First argument.
5607 __ ldr(r1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
5608
5609 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005610 if (flags_ == NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005611 __ JumpIfEitherSmi(r0, r1, &string_add_runtime);
5612 // Load instance types.
5613 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5614 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5615 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5616 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5617 STATIC_ASSERT(kStringTag == 0);
5618 // If either is not a string, go to runtime.
5619 __ tst(r4, Operand(kIsNotStringMask));
5620 __ tst(r5, Operand(kIsNotStringMask), eq);
5621 __ b(ne, &string_add_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005622 } else {
5623 // Here at least one of the arguments is definitely a string.
5624 // We convert the one that is not known to be a string.
5625 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5626 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5627 GenerateConvertArgument(
5628 masm, 1 * kPointerSize, r0, r2, r3, r4, r5, &call_builtin);
5629 builtin_id = Builtins::STRING_ADD_RIGHT;
5630 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5631 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5632 GenerateConvertArgument(
5633 masm, 0 * kPointerSize, r1, r2, r3, r4, r5, &call_builtin);
5634 builtin_id = Builtins::STRING_ADD_LEFT;
5635 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005636 }
5637
5638 // Both arguments are strings.
5639 // r0: first string
5640 // r1: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005641 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5642 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005643 {
5644 Label strings_not_empty;
5645 // Check if either of the strings are empty. In that case return the other.
5646 __ ldr(r2, FieldMemOperand(r0, String::kLengthOffset));
5647 __ ldr(r3, FieldMemOperand(r1, String::kLengthOffset));
5648 STATIC_ASSERT(kSmiTag == 0);
5649 __ cmp(r2, Operand(Smi::FromInt(0))); // Test if first string is empty.
5650 __ mov(r0, Operand(r1), LeaveCC, eq); // If first is empty, return second.
5651 STATIC_ASSERT(kSmiTag == 0);
5652 // Else test if second string is empty.
5653 __ cmp(r3, Operand(Smi::FromInt(0)), ne);
5654 __ b(ne, &strings_not_empty); // If either string was empty, return r0.
5655
Steve Block44f0eee2011-05-26 01:26:41 +01005656 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005657 __ add(sp, sp, Operand(2 * kPointerSize));
5658 __ Ret();
5659
5660 __ bind(&strings_not_empty);
5661 }
5662
5663 __ mov(r2, Operand(r2, ASR, kSmiTagSize));
5664 __ mov(r3, Operand(r3, ASR, kSmiTagSize));
5665 // Both strings are non-empty.
5666 // r0: first string
5667 // r1: second string
5668 // r2: length of first string
5669 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005670 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5671 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005672 // Look at the length of the result of adding the two strings.
5673 Label string_add_flat_result, longer_than_two;
5674 // Adding two lengths can't overflow.
5675 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
5676 __ add(r6, r2, Operand(r3));
Steve Block44f0eee2011-05-26 01:26:41 +01005677 // Use the symbol table when adding two one character strings, as it
5678 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005679 __ cmp(r6, Operand(2));
5680 __ b(ne, &longer_than_two);
5681
Steve Block44f0eee2011-05-26 01:26:41 +01005682 // Check that both strings are non-external ASCII strings.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005683 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005684 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5685 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5686 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5687 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5688 }
5689 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r4, r5, r6, r7,
5690 &string_add_runtime);
5691
5692 // Get the two characters forming the sub string.
5693 __ ldrb(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
5694 __ ldrb(r3, FieldMemOperand(r1, SeqAsciiString::kHeaderSize));
5695
5696 // Try to lookup two character string in symbol table. If it is not found
5697 // just allocate a new one.
5698 Label make_two_character_string;
5699 StringHelper::GenerateTwoCharacterSymbolTableProbe(
5700 masm, r2, r3, r6, r7, r4, r5, r9, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01005701 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005702 __ add(sp, sp, Operand(2 * kPointerSize));
5703 __ Ret();
5704
5705 __ bind(&make_two_character_string);
5706 // Resulting string has length 2 and first chars of two strings
5707 // are combined into single halfword in r2 register.
5708 // So we can fill resulting string without two loops by a single
5709 // halfword store instruction (which assumes that processor is
5710 // in a little endian mode)
5711 __ mov(r6, Operand(2));
5712 __ AllocateAsciiString(r0, r6, r4, r5, r9, &string_add_runtime);
5713 __ strh(r2, FieldMemOperand(r0, SeqAsciiString::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005714 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005715 __ add(sp, sp, Operand(2 * kPointerSize));
5716 __ Ret();
5717
5718 __ bind(&longer_than_two);
5719 // Check if resulting string will be flat.
5720 __ cmp(r6, Operand(String::kMinNonFlatLength));
5721 __ b(lt, &string_add_flat_result);
5722 // Handle exceptionally long strings in the runtime system.
5723 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
5724 ASSERT(IsPowerOf2(String::kMaxLength + 1));
5725 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
5726 __ cmp(r6, Operand(String::kMaxLength + 1));
5727 __ b(hs, &string_add_runtime);
5728
5729 // If result is not supposed to be flat, allocate a cons string object.
Steve Block44f0eee2011-05-26 01:26:41 +01005730 // If both strings are ASCII the result is an ASCII cons string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005731 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005732 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5733 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5734 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5735 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5736 }
5737 Label non_ascii, allocated, ascii_data;
5738 STATIC_ASSERT(kTwoByteStringTag == 0);
5739 __ tst(r4, Operand(kStringEncodingMask));
5740 __ tst(r5, Operand(kStringEncodingMask), ne);
5741 __ b(eq, &non_ascii);
5742
5743 // Allocate an ASCII cons string.
5744 __ bind(&ascii_data);
5745 __ AllocateAsciiConsString(r7, r6, r4, r5, &string_add_runtime);
5746 __ bind(&allocated);
5747 // Fill the fields of the cons string.
5748 __ str(r0, FieldMemOperand(r7, ConsString::kFirstOffset));
5749 __ str(r1, FieldMemOperand(r7, ConsString::kSecondOffset));
5750 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01005751 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005752 __ add(sp, sp, Operand(2 * kPointerSize));
5753 __ Ret();
5754
5755 __ bind(&non_ascii);
5756 // At least one of the strings is two-byte. Check whether it happens
Steve Block44f0eee2011-05-26 01:26:41 +01005757 // to contain only ASCII characters.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005758 // r4: first instance type.
5759 // r5: second instance type.
5760 __ tst(r4, Operand(kAsciiDataHintMask));
5761 __ tst(r5, Operand(kAsciiDataHintMask), ne);
5762 __ b(ne, &ascii_data);
5763 __ eor(r4, r4, Operand(r5));
5764 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5765 __ and_(r4, r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
5766 __ cmp(r4, Operand(kAsciiStringTag | kAsciiDataHintTag));
5767 __ b(eq, &ascii_data);
5768
5769 // Allocate a two byte cons string.
5770 __ AllocateTwoByteConsString(r7, r6, r4, r5, &string_add_runtime);
5771 __ jmp(&allocated);
5772
5773 // Handle creating a flat result. First check that both strings are
5774 // sequential and that they have the same encoding.
5775 // r0: first string
5776 // r1: second string
5777 // r2: length of first string
5778 // r3: length of second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005779 // r4: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
5780 // r5: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005781 // r6: sum of lengths.
5782 __ bind(&string_add_flat_result);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005783 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005784 __ ldr(r4, FieldMemOperand(r0, HeapObject::kMapOffset));
5785 __ ldr(r5, FieldMemOperand(r1, HeapObject::kMapOffset));
5786 __ ldrb(r4, FieldMemOperand(r4, Map::kInstanceTypeOffset));
5787 __ ldrb(r5, FieldMemOperand(r5, Map::kInstanceTypeOffset));
5788 }
5789 // Check that both strings are sequential.
5790 STATIC_ASSERT(kSeqStringTag == 0);
5791 __ tst(r4, Operand(kStringRepresentationMask));
5792 __ tst(r5, Operand(kStringRepresentationMask), eq);
5793 __ b(ne, &string_add_runtime);
5794 // Now check if both strings have the same encoding (ASCII/Two-byte).
5795 // r0: first string.
5796 // r1: second string.
5797 // r2: length of first string.
5798 // r3: length of second string.
5799 // r6: sum of lengths..
5800 Label non_ascii_string_add_flat_result;
5801 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
5802 __ eor(r7, r4, Operand(r5));
5803 __ tst(r7, Operand(kStringEncodingMask));
5804 __ b(ne, &string_add_runtime);
5805 // And see if it's ASCII or two-byte.
5806 __ tst(r4, Operand(kStringEncodingMask));
5807 __ b(eq, &non_ascii_string_add_flat_result);
5808
5809 // Both strings are sequential ASCII strings. We also know that they are
5810 // short (since the sum of the lengths is less than kMinNonFlatLength).
5811 // r6: length of resulting flat string
5812 __ AllocateAsciiString(r7, r6, r4, r5, r9, &string_add_runtime);
5813 // Locate first character of result.
5814 __ add(r6, r7, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5815 // Locate first character of first argument.
5816 __ add(r0, r0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5817 // r0: first character of first string.
5818 // r1: second string.
5819 // r2: length of first string.
5820 // r3: length of second string.
5821 // r6: first character of result.
5822 // r7: result string.
5823 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, true);
5824
5825 // Load second argument and locate first character.
5826 __ add(r1, r1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5827 // r1: first character of second string.
5828 // r3: length of second string.
5829 // r6: next character of result.
5830 // r7: result string.
5831 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, true);
5832 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01005833 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005834 __ add(sp, sp, Operand(2 * kPointerSize));
5835 __ Ret();
5836
5837 __ bind(&non_ascii_string_add_flat_result);
5838 // Both strings are sequential two byte strings.
5839 // r0: first string.
5840 // r1: second string.
5841 // r2: length of first string.
5842 // r3: length of second string.
5843 // r6: sum of length of strings.
5844 __ AllocateTwoByteString(r7, r6, r4, r5, r9, &string_add_runtime);
5845 // r0: first string.
5846 // r1: second string.
5847 // r2: length of first string.
5848 // r3: length of second string.
5849 // r7: result string.
5850
5851 // Locate first character of result.
5852 __ add(r6, r7, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5853 // Locate first character of first argument.
5854 __ add(r0, r0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5855
5856 // r0: first character of first string.
5857 // r1: second string.
5858 // r2: length of first string.
5859 // r3: length of second string.
5860 // r6: first character of result.
5861 // r7: result string.
5862 StringHelper::GenerateCopyCharacters(masm, r6, r0, r2, r4, false);
5863
5864 // Locate first character of second argument.
5865 __ add(r1, r1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5866
5867 // r1: first character of second string.
5868 // r3: length of second string.
5869 // r6: next character of result (after copy of first string).
5870 // r7: result string.
5871 StringHelper::GenerateCopyCharacters(masm, r6, r1, r3, r4, false);
5872
5873 __ mov(r0, Operand(r7));
Steve Block44f0eee2011-05-26 01:26:41 +01005874 __ IncrementCounter(counters->string_add_native(), 1, r2, r3);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005875 __ add(sp, sp, Operand(2 * kPointerSize));
5876 __ Ret();
5877
5878 // Just jump to runtime to add the two strings.
5879 __ bind(&string_add_runtime);
5880 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005881
5882 if (call_builtin.is_linked()) {
5883 __ bind(&call_builtin);
Ben Murdoch257744e2011-11-30 15:57:28 +00005884 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005885 }
5886}
5887
5888
5889void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5890 int stack_offset,
5891 Register arg,
5892 Register scratch1,
5893 Register scratch2,
5894 Register scratch3,
5895 Register scratch4,
5896 Label* slow) {
5897 // First check if the argument is already a string.
5898 Label not_string, done;
5899 __ JumpIfSmi(arg, &not_string);
5900 __ CompareObjectType(arg, scratch1, scratch1, FIRST_NONSTRING_TYPE);
5901 __ b(lt, &done);
5902
5903 // Check the number to string cache.
5904 Label not_cached;
5905 __ bind(&not_string);
5906 // Puts the cached result into scratch1.
5907 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5908 arg,
5909 scratch1,
5910 scratch2,
5911 scratch3,
5912 scratch4,
5913 false,
5914 &not_cached);
5915 __ mov(arg, scratch1);
5916 __ str(arg, MemOperand(sp, stack_offset));
5917 __ jmp(&done);
5918
5919 // Check if the argument is a safe string wrapper.
5920 __ bind(&not_cached);
5921 __ JumpIfSmi(arg, slow);
5922 __ CompareObjectType(
5923 arg, scratch1, scratch2, JS_VALUE_TYPE); // map -> scratch1.
5924 __ b(ne, slow);
5925 __ ldrb(scratch2, FieldMemOperand(scratch1, Map::kBitField2Offset));
5926 __ and_(scratch2,
5927 scratch2, Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
5928 __ cmp(scratch2,
5929 Operand(1 << Map::kStringWrapperSafeForDefaultValueOf));
5930 __ b(ne, slow);
5931 __ ldr(arg, FieldMemOperand(arg, JSValue::kValueOffset));
5932 __ str(arg, MemOperand(sp, stack_offset));
5933
5934 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005935}
5936
5937
Ben Murdochb0fe1622011-05-05 13:52:32 +01005938void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
5939 ASSERT(state_ == CompareIC::SMIS);
5940 Label miss;
5941 __ orr(r2, r1, r0);
5942 __ tst(r2, Operand(kSmiTagMask));
5943 __ b(ne, &miss);
5944
5945 if (GetCondition() == eq) {
5946 // For equality we do not care about the sign of the result.
5947 __ sub(r0, r0, r1, SetCC);
5948 } else {
Steve Block1e0659c2011-05-24 12:43:12 +01005949 // Untag before subtracting to avoid handling overflow.
5950 __ SmiUntag(r1);
5951 __ sub(r0, r1, SmiUntagOperand(r0));
Ben Murdochb0fe1622011-05-05 13:52:32 +01005952 }
5953 __ Ret();
5954
5955 __ bind(&miss);
5956 GenerateMiss(masm);
5957}
5958
5959
5960void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
5961 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5962
5963 Label generic_stub;
5964 Label unordered;
5965 Label miss;
5966 __ and_(r2, r1, Operand(r0));
5967 __ tst(r2, Operand(kSmiTagMask));
5968 __ b(eq, &generic_stub);
5969
5970 __ CompareObjectType(r0, r2, r2, HEAP_NUMBER_TYPE);
5971 __ b(ne, &miss);
5972 __ CompareObjectType(r1, r2, r2, HEAP_NUMBER_TYPE);
5973 __ b(ne, &miss);
5974
5975 // Inlining the double comparison and falling back to the general compare
5976 // stub if NaN is involved or VFP3 is unsupported.
Ben Murdoch8b112d22011-06-08 16:22:53 +01005977 if (CpuFeatures::IsSupported(VFP3)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01005978 CpuFeatures::Scope scope(VFP3);
5979
5980 // Load left and right operand
5981 __ sub(r2, r1, Operand(kHeapObjectTag));
5982 __ vldr(d0, r2, HeapNumber::kValueOffset);
5983 __ sub(r2, r0, Operand(kHeapObjectTag));
5984 __ vldr(d1, r2, HeapNumber::kValueOffset);
5985
5986 // Compare operands
Ben Murdochb8e0da22011-05-16 14:20:40 +01005987 __ VFPCompareAndSetFlags(d0, d1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005988
5989 // Don't base result on status bits when a NaN is involved.
5990 __ b(vs, &unordered);
5991
5992 // Return a result of -1, 0, or 1, based on status bits.
5993 __ mov(r0, Operand(EQUAL), LeaveCC, eq);
5994 __ mov(r0, Operand(LESS), LeaveCC, lt);
5995 __ mov(r0, Operand(GREATER), LeaveCC, gt);
5996 __ Ret();
5997
5998 __ bind(&unordered);
5999 }
6000
6001 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS, r1, r0);
6002 __ bind(&generic_stub);
6003 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
6004
6005 __ bind(&miss);
6006 GenerateMiss(masm);
6007}
6008
6009
Ben Murdoch257744e2011-11-30 15:57:28 +00006010void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
6011 ASSERT(state_ == CompareIC::SYMBOLS);
6012 Label miss;
6013
6014 // Registers containing left and right operands respectively.
6015 Register left = r1;
6016 Register right = r0;
6017 Register tmp1 = r2;
6018 Register tmp2 = r3;
6019
6020 // Check that both operands are heap objects.
6021 __ JumpIfEitherSmi(left, right, &miss);
6022
6023 // Check that both operands are symbols.
6024 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
6025 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
6026 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
6027 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
6028 STATIC_ASSERT(kSymbolTag != 0);
6029 __ and_(tmp1, tmp1, Operand(tmp2));
6030 __ tst(tmp1, Operand(kIsSymbolMask));
6031 __ b(eq, &miss);
6032
6033 // Symbols are compared by identity.
6034 __ cmp(left, right);
6035 // Make sure r0 is non-zero. At this point input operands are
6036 // guaranteed to be non-zero.
6037 ASSERT(right.is(r0));
6038 STATIC_ASSERT(EQUAL == 0);
6039 STATIC_ASSERT(kSmiTag == 0);
6040 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
6041 __ Ret();
6042
6043 __ bind(&miss);
6044 GenerateMiss(masm);
6045}
6046
6047
6048void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
6049 ASSERT(state_ == CompareIC::STRINGS);
6050 Label miss;
6051
6052 // Registers containing left and right operands respectively.
6053 Register left = r1;
6054 Register right = r0;
6055 Register tmp1 = r2;
6056 Register tmp2 = r3;
6057 Register tmp3 = r4;
6058 Register tmp4 = r5;
6059
6060 // Check that both operands are heap objects.
6061 __ JumpIfEitherSmi(left, right, &miss);
6062
6063 // Check that both operands are strings. This leaves the instance
6064 // types loaded in tmp1 and tmp2.
6065 __ ldr(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
6066 __ ldr(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
6067 __ ldrb(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
6068 __ ldrb(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
6069 STATIC_ASSERT(kNotStringTag != 0);
6070 __ orr(tmp3, tmp1, tmp2);
6071 __ tst(tmp3, Operand(kIsNotStringMask));
6072 __ b(ne, &miss);
6073
6074 // Fast check for identical strings.
6075 __ cmp(left, right);
6076 STATIC_ASSERT(EQUAL == 0);
6077 STATIC_ASSERT(kSmiTag == 0);
6078 __ mov(r0, Operand(Smi::FromInt(EQUAL)), LeaveCC, eq);
6079 __ Ret(eq);
6080
6081 // Handle not identical strings.
6082
6083 // Check that both strings are symbols. If they are, we're done
6084 // because we already know they are not identical.
6085 ASSERT(GetCondition() == eq);
6086 STATIC_ASSERT(kSymbolTag != 0);
6087 __ and_(tmp3, tmp1, Operand(tmp2));
6088 __ tst(tmp3, Operand(kIsSymbolMask));
6089 // Make sure r0 is non-zero. At this point input operands are
6090 // guaranteed to be non-zero.
6091 ASSERT(right.is(r0));
6092 __ Ret(ne);
6093
6094 // Check that both strings are sequential ASCII.
6095 Label runtime;
6096 __ JumpIfBothInstanceTypesAreNotSequentialAscii(tmp1, tmp2, tmp3, tmp4,
6097 &runtime);
6098
6099 // Compare flat ASCII strings. Returns when done.
6100 StringCompareStub::GenerateFlatAsciiStringEquals(
6101 masm, left, right, tmp1, tmp2, tmp3);
6102
6103 // Handle more complex cases in runtime.
6104 __ bind(&runtime);
6105 __ Push(left, right);
6106 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
6107
6108 __ bind(&miss);
6109 GenerateMiss(masm);
6110}
6111
6112
Ben Murdochb0fe1622011-05-05 13:52:32 +01006113void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6114 ASSERT(state_ == CompareIC::OBJECTS);
6115 Label miss;
6116 __ and_(r2, r1, Operand(r0));
6117 __ tst(r2, Operand(kSmiTagMask));
6118 __ b(eq, &miss);
6119
6120 __ CompareObjectType(r0, r2, r2, JS_OBJECT_TYPE);
6121 __ b(ne, &miss);
6122 __ CompareObjectType(r1, r2, r2, JS_OBJECT_TYPE);
6123 __ b(ne, &miss);
6124
6125 ASSERT(GetCondition() == eq);
6126 __ sub(r0, r0, Operand(r1));
6127 __ Ret();
6128
6129 __ bind(&miss);
6130 GenerateMiss(masm);
6131}
6132
6133
6134void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
6135 __ Push(r1, r0);
6136 __ push(lr);
6137
6138 // Call the runtime system in a fresh internal frame.
Steve Block44f0eee2011-05-26 01:26:41 +01006139 ExternalReference miss =
6140 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
Ben Murdochb0fe1622011-05-05 13:52:32 +01006141 __ EnterInternalFrame();
6142 __ Push(r1, r0);
6143 __ mov(ip, Operand(Smi::FromInt(op_)));
6144 __ push(ip);
6145 __ CallExternalReference(miss, 3);
6146 __ LeaveInternalFrame();
6147 // Compute the entry point of the rewritten stub.
6148 __ add(r2, r0, Operand(Code::kHeaderSize - kHeapObjectTag));
6149 // Restore registers.
6150 __ pop(lr);
6151 __ pop(r0);
6152 __ pop(r1);
6153 __ Jump(r2);
6154}
6155
6156
Steve Block1e0659c2011-05-24 12:43:12 +01006157void DirectCEntryStub::Generate(MacroAssembler* masm) {
6158 __ ldr(pc, MemOperand(sp, 0));
6159}
6160
6161
6162void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006163 ExternalReference function) {
6164 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
6165 RelocInfo::CODE_TARGET));
6166 __ mov(r2, Operand(function));
6167 // Push return address (accessible to GC through exit frame pc).
6168 __ str(pc, MemOperand(sp, 0));
6169 __ Jump(r2); // Call the api function.
6170}
6171
6172
6173void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
6174 Register target) {
Steve Block1e0659c2011-05-24 12:43:12 +01006175 __ mov(lr, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
6176 RelocInfo::CODE_TARGET));
6177 // Push return address (accessible to GC through exit frame pc).
Steve Block1e0659c2011-05-24 12:43:12 +01006178 __ str(pc, MemOperand(sp, 0));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01006179 __ Jump(target); // Call the C++ function.
Steve Block1e0659c2011-05-24 12:43:12 +01006180}
6181
6182
Ben Murdoch257744e2011-11-30 15:57:28 +00006183MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
6184 MacroAssembler* masm,
6185 Label* miss,
6186 Label* done,
6187 Register receiver,
6188 Register properties,
6189 String* name,
6190 Register scratch0) {
6191 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6192 // not equal to the name and kProbes-th slot is not used (its name is the
6193 // undefined value), it guarantees the hash table doesn't contain the
6194 // property. It's true even if some slots represent deleted properties
6195 // (their names are the null value).
6196 for (int i = 0; i < kInlinedProbes; i++) {
6197 // scratch0 points to properties hash.
6198 // Compute the masked index: (hash + i + i * i) & mask.
6199 Register index = scratch0;
6200 // Capacity is smi 2^n.
6201 __ ldr(index, FieldMemOperand(properties, kCapacityOffset));
6202 __ sub(index, index, Operand(1));
6203 __ and_(index, index, Operand(
6204 Smi::FromInt(name->Hash() + StringDictionary::GetProbeOffset(i))));
6205
6206 // Scale the index by multiplying by the entry size.
6207 ASSERT(StringDictionary::kEntrySize == 3);
6208 __ add(index, index, Operand(index, LSL, 1)); // index *= 3.
6209
6210 Register entity_name = scratch0;
6211 // Having undefined at this place means the name is not contained.
6212 ASSERT_EQ(kSmiTagSize, 1);
6213 Register tmp = properties;
6214 __ add(tmp, properties, Operand(index, LSL, 1));
6215 __ ldr(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
6216
6217 ASSERT(!tmp.is(entity_name));
6218 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
6219 __ cmp(entity_name, tmp);
6220 __ b(eq, done);
6221
6222 if (i != kInlinedProbes - 1) {
6223 // Stop if found the property.
6224 __ cmp(entity_name, Operand(Handle<String>(name)));
6225 __ b(eq, miss);
6226
6227 // Check if the entry name is not a symbol.
6228 __ ldr(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
6229 __ ldrb(entity_name,
6230 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
6231 __ tst(entity_name, Operand(kIsSymbolMask));
6232 __ b(eq, miss);
6233
6234 // Restore the properties.
6235 __ ldr(properties,
6236 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
6237 }
6238 }
6239
6240 const int spill_mask =
6241 (lr.bit() | r6.bit() | r5.bit() | r4.bit() | r3.bit() |
6242 r2.bit() | r1.bit() | r0.bit());
6243
6244 __ stm(db_w, sp, spill_mask);
6245 __ ldr(r0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
6246 __ mov(r1, Operand(Handle<String>(name)));
6247 StringDictionaryLookupStub stub(NEGATIVE_LOOKUP);
6248 MaybeObject* result = masm->TryCallStub(&stub);
6249 if (result->IsFailure()) return result;
6250 __ tst(r0, Operand(r0));
6251 __ ldm(ia_w, sp, spill_mask);
6252
6253 __ b(eq, done);
6254 __ b(ne, miss);
6255 return result;
6256}
6257
6258
6259// Probe the string dictionary in the |elements| register. Jump to the
6260// |done| label if a property with the given name is found. Jump to
6261// the |miss| label otherwise.
6262// If lookup was successful |scratch2| will be equal to elements + 4 * index.
6263void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6264 Label* miss,
6265 Label* done,
6266 Register elements,
6267 Register name,
6268 Register scratch1,
6269 Register scratch2) {
6270 // Assert that name contains a string.
6271 if (FLAG_debug_code) __ AbortIfNotString(name);
6272
6273 // Compute the capacity mask.
6274 __ ldr(scratch1, FieldMemOperand(elements, kCapacityOffset));
6275 __ mov(scratch1, Operand(scratch1, ASR, kSmiTagSize)); // convert smi to int
6276 __ sub(scratch1, scratch1, Operand(1));
6277
6278 // Generate an unrolled loop that performs a few probes before
6279 // giving up. Measurements done on Gmail indicate that 2 probes
6280 // cover ~93% of loads from dictionaries.
6281 for (int i = 0; i < kInlinedProbes; i++) {
6282 // Compute the masked index: (hash + i + i * i) & mask.
6283 __ ldr(scratch2, FieldMemOperand(name, String::kHashFieldOffset));
6284 if (i > 0) {
6285 // Add the probe offset (i + i * i) left shifted to avoid right shifting
6286 // the hash in a separate instruction. The value hash + i + i * i is right
6287 // shifted in the following and instruction.
6288 ASSERT(StringDictionary::GetProbeOffset(i) <
6289 1 << (32 - String::kHashFieldOffset));
6290 __ add(scratch2, scratch2, Operand(
6291 StringDictionary::GetProbeOffset(i) << String::kHashShift));
6292 }
6293 __ and_(scratch2, scratch1, Operand(scratch2, LSR, String::kHashShift));
6294
6295 // Scale the index by multiplying by the element size.
6296 ASSERT(StringDictionary::kEntrySize == 3);
6297 // scratch2 = scratch2 * 3.
6298 __ add(scratch2, scratch2, Operand(scratch2, LSL, 1));
6299
6300 // Check if the key is identical to the name.
6301 __ add(scratch2, elements, Operand(scratch2, LSL, 2));
6302 __ ldr(ip, FieldMemOperand(scratch2, kElementsStartOffset));
6303 __ cmp(name, Operand(ip));
6304 __ b(eq, done);
6305 }
6306
6307 const int spill_mask =
6308 (lr.bit() | r6.bit() | r5.bit() | r4.bit() |
6309 r3.bit() | r2.bit() | r1.bit() | r0.bit()) &
6310 ~(scratch1.bit() | scratch2.bit());
6311
6312 __ stm(db_w, sp, spill_mask);
6313 __ Move(r0, elements);
6314 __ Move(r1, name);
6315 StringDictionaryLookupStub stub(POSITIVE_LOOKUP);
6316 __ CallStub(&stub);
6317 __ tst(r0, Operand(r0));
6318 __ mov(scratch2, Operand(r2));
6319 __ ldm(ia_w, sp, spill_mask);
6320
6321 __ b(ne, done);
6322 __ b(eq, miss);
6323}
6324
6325
6326void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
6327 // Registers:
6328 // result: StringDictionary to probe
6329 // r1: key
6330 // : StringDictionary to probe.
6331 // index_: will hold an index of entry if lookup is successful.
6332 // might alias with result_.
6333 // Returns:
6334 // result_ is zero if lookup failed, non zero otherwise.
6335
6336 Register result = r0;
6337 Register dictionary = r0;
6338 Register key = r1;
6339 Register index = r2;
6340 Register mask = r3;
6341 Register hash = r4;
6342 Register undefined = r5;
6343 Register entry_key = r6;
6344
6345 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6346
6347 __ ldr(mask, FieldMemOperand(dictionary, kCapacityOffset));
6348 __ mov(mask, Operand(mask, ASR, kSmiTagSize));
6349 __ sub(mask, mask, Operand(1));
6350
6351 __ ldr(hash, FieldMemOperand(key, String::kHashFieldOffset));
6352
6353 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
6354
6355 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6356 // Compute the masked index: (hash + i + i * i) & mask.
6357 // Capacity is smi 2^n.
6358 if (i > 0) {
6359 // Add the probe offset (i + i * i) left shifted to avoid right shifting
6360 // the hash in a separate instruction. The value hash + i + i * i is right
6361 // shifted in the following and instruction.
6362 ASSERT(StringDictionary::GetProbeOffset(i) <
6363 1 << (32 - String::kHashFieldOffset));
6364 __ add(index, hash, Operand(
6365 StringDictionary::GetProbeOffset(i) << String::kHashShift));
6366 } else {
6367 __ mov(index, Operand(hash));
6368 }
6369 __ and_(index, mask, Operand(index, LSR, String::kHashShift));
6370
6371 // Scale the index by multiplying by the entry size.
6372 ASSERT(StringDictionary::kEntrySize == 3);
6373 __ add(index, index, Operand(index, LSL, 1)); // index *= 3.
6374
6375 ASSERT_EQ(kSmiTagSize, 1);
6376 __ add(index, dictionary, Operand(index, LSL, 2));
6377 __ ldr(entry_key, FieldMemOperand(index, kElementsStartOffset));
6378
6379 // Having undefined at this place means the name is not contained.
6380 __ cmp(entry_key, Operand(undefined));
6381 __ b(eq, &not_in_dictionary);
6382
6383 // Stop if found the property.
6384 __ cmp(entry_key, Operand(key));
6385 __ b(eq, &in_dictionary);
6386
6387 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6388 // Check if the entry name is not a symbol.
6389 __ ldr(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
6390 __ ldrb(entry_key,
6391 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
6392 __ tst(entry_key, Operand(kIsSymbolMask));
6393 __ b(eq, &maybe_in_dictionary);
6394 }
6395 }
6396
6397 __ bind(&maybe_in_dictionary);
6398 // If we are doing negative lookup then probing failure should be
6399 // treated as a lookup success. For positive lookup probing failure
6400 // should be treated as lookup failure.
6401 if (mode_ == POSITIVE_LOOKUP) {
6402 __ mov(result, Operand(0));
6403 __ Ret();
6404 }
6405
6406 __ bind(&in_dictionary);
6407 __ mov(result, Operand(1));
6408 __ Ret();
6409
6410 __ bind(&not_in_dictionary);
6411 __ mov(result, Operand(0));
6412 __ Ret();
6413}
6414
6415
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006416#undef __
6417
6418} } // namespace v8::internal
6419
6420#endif // V8_TARGET_ARCH_ARM