blob: b26e0b435e9e4b0808491d189259b05a0347e873 [file] [log] [blame]
lrn@chromium.org7516f052011-03-30 08:52:27 +00001// Copyright 2011 the V8 project authors. All rights reserved.
2// 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_MIPS)
31
32#include "bootstrapper.h"
33#include "code-stubs.h"
karlklose@chromium.org83a47282011-05-11 11:54:09 +000034#include "codegen.h"
lrn@chromium.org7516f052011-03-30 08:52:27 +000035#include "regexp-macro-assembler.h"
36
37namespace v8 {
38namespace internal {
39
40
41#define __ ACCESS_MASM(masm)
42
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000043static void EmitIdenticalObjectComparison(MacroAssembler* masm,
44 Label* slow,
45 Condition cc,
46 bool never_nan_nan);
47static void EmitSmiNonsmiComparison(MacroAssembler* masm,
48 Register lhs,
49 Register rhs,
50 Label* rhs_not_nan,
51 Label* slow,
52 bool strict);
53static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc);
54static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
55 Register lhs,
56 Register rhs);
57
58
59// Check if the operand is a heap number.
60static void EmitCheckForHeapNumber(MacroAssembler* masm, Register operand,
61 Register scratch1, Register scratch2,
62 Label* not_a_heap_number) {
63 __ lw(scratch1, FieldMemOperand(operand, HeapObject::kMapOffset));
64 __ LoadRoot(scratch2, Heap::kHeapNumberMapRootIndex);
65 __ Branch(not_a_heap_number, ne, scratch1, Operand(scratch2));
66}
67
lrn@chromium.org7516f052011-03-30 08:52:27 +000068
69void ToNumberStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000070 // The ToNumber stub takes one argument in a0.
71 Label check_heap_number, call_builtin;
72 __ JumpIfNotSmi(a0, &check_heap_number);
73 __ mov(v0, a0);
74 __ Ret();
75
76 __ bind(&check_heap_number);
77 EmitCheckForHeapNumber(masm, a0, a1, t0, &call_builtin);
78 __ mov(v0, a0);
79 __ Ret();
80
81 __ bind(&call_builtin);
82 __ push(a0);
83 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
lrn@chromium.org7516f052011-03-30 08:52:27 +000084}
85
86
87void FastNewClosureStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +000088 // Create a new closure from the given function info in new
89 // space. Set the context to the current context in cp.
90 Label gc;
91
92 // Pop the function info from the stack.
93 __ pop(a3);
94
95 // Attempt to allocate new JSFunction in new space.
96 __ AllocateInNewSpace(JSFunction::kSize,
97 v0,
98 a1,
99 a2,
100 &gc,
101 TAG_OBJECT);
102
mstarzinger@chromium.org1b3afd12011-11-29 14:28:56 +0000103 int map_index = (language_mode_ == CLASSIC_MODE)
104 ? Context::FUNCTION_MAP_INDEX
105 : Context::STRICT_MODE_FUNCTION_MAP_INDEX;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000106
107 // Compute the function map in the current global context and set that
108 // as the map of the allocated object.
109 __ lw(a2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
110 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalContextOffset));
111 __ lw(a2, MemOperand(a2, Context::SlotOffset(map_index)));
112 __ sw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
113
114 // Initialize the rest of the function. We don't have to update the
115 // write barrier because the allocated object is in new space.
116 __ LoadRoot(a1, Heap::kEmptyFixedArrayRootIndex);
117 __ LoadRoot(a2, Heap::kTheHoleValueRootIndex);
118 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
119 __ sw(a1, FieldMemOperand(v0, JSObject::kPropertiesOffset));
120 __ sw(a1, FieldMemOperand(v0, JSObject::kElementsOffset));
121 __ sw(a2, FieldMemOperand(v0, JSFunction::kPrototypeOrInitialMapOffset));
122 __ sw(a3, FieldMemOperand(v0, JSFunction::kSharedFunctionInfoOffset));
123 __ sw(cp, FieldMemOperand(v0, JSFunction::kContextOffset));
124 __ sw(a1, FieldMemOperand(v0, JSFunction::kLiteralsOffset));
125 __ sw(t0, FieldMemOperand(v0, JSFunction::kNextFunctionLinkOffset));
126
127 // Initialize the code pointer in the function to be the one
128 // found in the shared function info object.
129 __ lw(a3, FieldMemOperand(a3, SharedFunctionInfo::kCodeOffset));
130 __ Addu(a3, a3, Operand(Code::kHeaderSize - kHeapObjectTag));
131 __ sw(a3, FieldMemOperand(v0, 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);
138 __ LoadRoot(t0, Heap::kFalseValueRootIndex);
139 __ Push(cp, a3, t0);
140 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000141}
142
143
144void FastNewContextStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000145 // 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 v0,
152 a1,
153 a2,
154 &gc,
155 TAG_OBJECT);
156
157 // Load the function from the stack.
158 __ lw(a3, MemOperand(sp, 0));
159
160 // Setup the object header.
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000161 __ LoadRoot(a2, Heap::kFunctionContextMapRootIndex);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000162 __ sw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
163 __ li(a2, Operand(Smi::FromInt(length)));
164 __ sw(a2, FieldMemOperand(v0, FixedArray::kLengthOffset));
165
166 // Setup the fixed slots.
167 __ li(a1, Operand(Smi::FromInt(0)));
168 __ sw(a3, MemOperand(v0, Context::SlotOffset(Context::CLOSURE_INDEX)));
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000169 __ sw(cp, MemOperand(v0, Context::SlotOffset(Context::PREVIOUS_INDEX)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000170 __ sw(a1, MemOperand(v0, Context::SlotOffset(Context::EXTENSION_INDEX)));
171
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000172 // Copy the global object from the previous context.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000173 __ lw(a1, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
174 __ sw(a1, MemOperand(v0, Context::SlotOffset(Context::GLOBAL_INDEX)));
175
176 // Initialize the rest of the slots to undefined.
177 __ LoadRoot(a1, Heap::kUndefinedValueRootIndex);
178 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
179 __ sw(a1, MemOperand(v0, Context::SlotOffset(i)));
180 }
181
182 // Remove the on-stack argument and return.
183 __ mov(cp, v0);
184 __ Pop();
185 __ Ret();
186
187 // Need to collect. Call into runtime system.
188 __ bind(&gc);
svenpanne@chromium.org6d786c92011-06-15 10:58:27 +0000189 __ TailCallRuntime(Runtime::kNewFunctionContext, 1, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000190}
191
192
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000193void FastNewBlockContextStub::Generate(MacroAssembler* masm) {
194 // Stack layout on entry:
195 //
196 // [sp]: function.
197 // [sp + kPointerSize]: serialized scope info
198
199 // Try to allocate the context in new space.
200 Label gc;
201 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
202 __ AllocateInNewSpace(FixedArray::SizeFor(length),
203 v0, a1, a2, &gc, TAG_OBJECT);
204
205 // Load the function from the stack.
206 __ lw(a3, MemOperand(sp, 0));
207
208 // Load the serialized scope info from the stack.
209 __ lw(a1, MemOperand(sp, 1 * kPointerSize));
210
211 // Setup the object header.
212 __ LoadRoot(a2, Heap::kBlockContextMapRootIndex);
213 __ sw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
214 __ li(a2, Operand(Smi::FromInt(length)));
215 __ sw(a2, FieldMemOperand(v0, FixedArray::kLengthOffset));
216
217 // If this block context is nested in the global context we get a smi
218 // sentinel instead of a function. The block context should get the
219 // canonical empty function of the global context as its closure which
220 // we still have to look up.
221 Label after_sentinel;
222 __ JumpIfNotSmi(a3, &after_sentinel);
223 if (FLAG_debug_code) {
224 const char* message = "Expected 0 as a Smi sentinel";
225 __ Assert(eq, message, a3, Operand(zero_reg));
226 }
227 __ lw(a3, GlobalObjectOperand());
228 __ lw(a3, FieldMemOperand(a3, GlobalObject::kGlobalContextOffset));
229 __ lw(a3, ContextOperand(a3, Context::CLOSURE_INDEX));
230 __ bind(&after_sentinel);
231
232 // Setup the fixed slots.
233 __ sw(a3, ContextOperand(v0, Context::CLOSURE_INDEX));
234 __ sw(cp, ContextOperand(v0, Context::PREVIOUS_INDEX));
235 __ sw(a1, ContextOperand(v0, Context::EXTENSION_INDEX));
236
237 // Copy the global object from the previous context.
238 __ lw(a1, ContextOperand(cp, Context::GLOBAL_INDEX));
239 __ sw(a1, ContextOperand(v0, Context::GLOBAL_INDEX));
240
241 // Initialize the rest of the slots to the hole value.
242 __ LoadRoot(a1, Heap::kTheHoleValueRootIndex);
243 for (int i = 0; i < slots_; i++) {
244 __ sw(a1, ContextOperand(v0, i + Context::MIN_CONTEXT_SLOTS));
245 }
246
247 // Remove the on-stack argument and return.
248 __ mov(cp, v0);
249 __ Addu(sp, sp, Operand(2 * kPointerSize));
250 __ Ret();
251
252 // Need to collect. Call into runtime system.
253 __ bind(&gc);
254 __ TailCallRuntime(Runtime::kPushBlockContext, 2, 1);
255}
256
257
lrn@chromium.org7516f052011-03-30 08:52:27 +0000258void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000259 // Stack layout on entry:
260 // [sp]: constant elements.
261 // [sp + kPointerSize]: literal index.
262 // [sp + (2 * kPointerSize)]: literals array.
263
264 // All sizes here are multiples of kPointerSize.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000265 int elements_size = 0;
266 if (length_ > 0) {
267 elements_size = mode_ == CLONE_DOUBLE_ELEMENTS
268 ? FixedDoubleArray::SizeFor(length_)
269 : FixedArray::SizeFor(length_);
270 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000271 int size = JSArray::kSize + elements_size;
272
273 // Load boilerplate object into r3 and check if we need to create a
274 // boilerplate.
275 Label slow_case;
276 __ lw(a3, MemOperand(sp, 2 * kPointerSize));
277 __ lw(a0, MemOperand(sp, 1 * kPointerSize));
278 __ Addu(a3, a3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
279 __ sll(t0, a0, kPointerSizeLog2 - kSmiTagSize);
280 __ Addu(t0, a3, t0);
281 __ lw(a3, MemOperand(t0));
282 __ LoadRoot(t1, Heap::kUndefinedValueRootIndex);
283 __ Branch(&slow_case, eq, a3, Operand(t1));
284
285 if (FLAG_debug_code) {
286 const char* message;
287 Heap::RootListIndex expected_map_index;
288 if (mode_ == CLONE_ELEMENTS) {
289 message = "Expected (writable) fixed array";
290 expected_map_index = Heap::kFixedArrayMapRootIndex;
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000291 } else if (mode_ == CLONE_DOUBLE_ELEMENTS) {
292 message = "Expected (writable) fixed double array";
293 expected_map_index = Heap::kFixedDoubleArrayMapRootIndex;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000294 } else {
295 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
296 message = "Expected copy-on-write fixed array";
297 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
298 }
299 __ push(a3);
300 __ lw(a3, FieldMemOperand(a3, JSArray::kElementsOffset));
301 __ lw(a3, FieldMemOperand(a3, HeapObject::kMapOffset));
302 __ LoadRoot(at, expected_map_index);
303 __ Assert(eq, message, a3, Operand(at));
304 __ pop(a3);
305 }
306
307 // Allocate both the JS array and the elements array in one big
308 // allocation. This avoids multiple limit checks.
309 // Return new object in v0.
310 __ AllocateInNewSpace(size,
311 v0,
312 a1,
313 a2,
314 &slow_case,
315 TAG_OBJECT);
316
317 // Copy the JS array part.
318 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
319 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
320 __ lw(a1, FieldMemOperand(a3, i));
321 __ sw(a1, FieldMemOperand(v0, i));
322 }
323 }
324
325 if (length_ > 0) {
326 // Get hold of the elements array of the boilerplate and setup the
327 // elements pointer in the resulting object.
328 __ lw(a3, FieldMemOperand(a3, JSArray::kElementsOffset));
329 __ Addu(a2, v0, Operand(JSArray::kSize));
330 __ sw(a2, FieldMemOperand(v0, JSArray::kElementsOffset));
331
332 // Copy the elements array.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +0000333 ASSERT((elements_size % kPointerSize) == 0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000334 __ CopyFields(a2, a3, a1.bit(), elements_size / kPointerSize);
335 }
336
337 // Return and remove the on-stack parameters.
338 __ Addu(sp, sp, Operand(3 * kPointerSize));
339 __ Ret();
340
341 __ bind(&slow_case);
342 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000343}
344
345
346// Takes a Smi and converts to an IEEE 64 bit floating point value in two
347// registers. The format is 1 sign bit, 11 exponent bits (biased 1023) and
348// 52 fraction bits (20 in the first word, 32 in the second). Zeros is a
349// scratch register. Destroys the source register. No GC occurs during this
350// stub so you don't have to set up the frame.
351class ConvertToDoubleStub : public CodeStub {
352 public:
353 ConvertToDoubleStub(Register result_reg_1,
354 Register result_reg_2,
355 Register source_reg,
356 Register scratch_reg)
357 : result1_(result_reg_1),
358 result2_(result_reg_2),
359 source_(source_reg),
360 zeros_(scratch_reg) { }
361
362 private:
363 Register result1_;
364 Register result2_;
365 Register source_;
366 Register zeros_;
367
368 // Minor key encoding in 16 bits.
369 class ModeBits: public BitField<OverwriteMode, 0, 2> {};
370 class OpBits: public BitField<Token::Value, 2, 14> {};
371
372 Major MajorKey() { return ConvertToDouble; }
373 int MinorKey() {
374 // Encode the parameters in a unique 16 bit value.
375 return result1_.code() +
376 (result2_.code() << 4) +
377 (source_.code() << 8) +
378 (zeros_.code() << 12);
379 }
380
381 void Generate(MacroAssembler* masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000382};
383
384
385void ConvertToDoubleStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000386#ifndef BIG_ENDIAN_FLOATING_POINT
387 Register exponent = result1_;
388 Register mantissa = result2_;
389#else
390 Register exponent = result2_;
391 Register mantissa = result1_;
392#endif
393 Label not_special;
394 // Convert from Smi to integer.
395 __ sra(source_, source_, kSmiTagSize);
396 // Move sign bit from source to destination. This works because the sign bit
397 // in the exponent word of the double has the same position and polarity as
398 // the 2's complement sign bit in a Smi.
399 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
400 __ And(exponent, source_, Operand(HeapNumber::kSignMask));
401 // Subtract from 0 if source was negative.
402 __ subu(at, zero_reg, source_);
403 __ movn(source_, at, exponent);
404
405 // We have -1, 0 or 1, which we treat specially. Register source_ contains
406 // absolute value: it is either equal to 1 (special case of -1 and 1),
407 // greater than 1 (not a special case) or less than 1 (special case of 0).
408 __ Branch(&not_special, gt, source_, Operand(1));
409
410 // For 1 or -1 we need to or in the 0 exponent (biased to 1023).
411 static const uint32_t exponent_word_for_1 =
412 HeapNumber::kExponentBias << HeapNumber::kExponentShift;
413 // Safe to use 'at' as dest reg here.
414 __ Or(at, exponent, Operand(exponent_word_for_1));
415 __ movn(exponent, at, source_); // Write exp when source not 0.
416 // 1, 0 and -1 all have 0 for the second word.
417 __ mov(mantissa, zero_reg);
418 __ Ret();
419
420 __ bind(&not_special);
421 // Count leading zeros.
422 // Gets the wrong answer for 0, but we already checked for that case above.
423 __ clz(zeros_, source_);
424 // Compute exponent and or it into the exponent register.
425 // We use mantissa as a scratch register here.
426 __ li(mantissa, Operand(31 + HeapNumber::kExponentBias));
427 __ subu(mantissa, mantissa, zeros_);
428 __ sll(mantissa, mantissa, HeapNumber::kExponentShift);
429 __ Or(exponent, exponent, mantissa);
430
431 // Shift up the source chopping the top bit off.
432 __ Addu(zeros_, zeros_, Operand(1));
433 // This wouldn't work for 1.0 or -1.0 as the shift would be 32 which means 0.
434 __ sllv(source_, source_, zeros_);
435 // Compute lower part of fraction (last 12 bits).
436 __ sll(mantissa, source_, HeapNumber::kMantissaBitsInTopWord);
437 // And the top (top 20 bits).
438 __ srl(source_, source_, 32 - HeapNumber::kMantissaBitsInTopWord);
439 __ or_(exponent, exponent, source_);
440
441 __ Ret();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000442}
443
444
lrn@chromium.org7516f052011-03-30 08:52:27 +0000445void FloatingPointHelper::LoadSmis(MacroAssembler* masm,
446 FloatingPointHelper::Destination destination,
447 Register scratch1,
448 Register scratch2) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000449 if (CpuFeatures::IsSupported(FPU)) {
450 CpuFeatures::Scope scope(FPU);
451 __ sra(scratch1, a0, kSmiTagSize);
452 __ mtc1(scratch1, f14);
453 __ cvt_d_w(f14, f14);
454 __ sra(scratch1, a1, kSmiTagSize);
455 __ mtc1(scratch1, f12);
456 __ cvt_d_w(f12, f12);
457 if (destination == kCoreRegisters) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000458 __ Move(a2, a3, f14);
459 __ Move(a0, a1, f12);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000460 }
461 } else {
462 ASSERT(destination == kCoreRegisters);
463 // Write Smi from a0 to a3 and a2 in double format.
464 __ mov(scratch1, a0);
465 ConvertToDoubleStub stub1(a3, a2, scratch1, scratch2);
466 __ push(ra);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000467 __ Call(stub1.GetCode());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000468 // Write Smi from a1 to a1 and a0 in double format.
469 __ mov(scratch1, a1);
470 ConvertToDoubleStub stub2(a1, a0, scratch1, scratch2);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000471 __ Call(stub2.GetCode());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000472 __ pop(ra);
473 }
lrn@chromium.org7516f052011-03-30 08:52:27 +0000474}
475
476
477void FloatingPointHelper::LoadOperands(
478 MacroAssembler* masm,
479 FloatingPointHelper::Destination destination,
480 Register heap_number_map,
481 Register scratch1,
482 Register scratch2,
483 Label* slow) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000484
485 // Load right operand (a0) to f12 or a2/a3.
486 LoadNumber(masm, destination,
487 a0, f14, a2, a3, heap_number_map, scratch1, scratch2, slow);
488
489 // Load left operand (a1) to f14 or a0/a1.
490 LoadNumber(masm, destination,
491 a1, f12, a0, a1, heap_number_map, scratch1, scratch2, slow);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000492}
493
494
495void FloatingPointHelper::LoadNumber(MacroAssembler* masm,
496 Destination destination,
497 Register object,
498 FPURegister dst,
499 Register dst1,
500 Register dst2,
501 Register heap_number_map,
502 Register scratch1,
503 Register scratch2,
504 Label* not_number) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000505 if (FLAG_debug_code) {
506 __ AbortIfNotRootValue(heap_number_map,
507 Heap::kHeapNumberMapRootIndex,
508 "HeapNumberMap register clobbered.");
509 }
510
511 Label is_smi, done;
512
513 __ JumpIfSmi(object, &is_smi);
514 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_number);
515
516 // Handle loading a double from a heap number.
517 if (CpuFeatures::IsSupported(FPU) &&
518 destination == kFPURegisters) {
519 CpuFeatures::Scope scope(FPU);
520 // Load the double from tagged HeapNumber to double register.
521
522 // ARM uses a workaround here because of the unaligned HeapNumber
523 // kValueOffset. On MIPS this workaround is built into ldc1 so there's no
524 // point in generating even more instructions.
525 __ ldc1(dst, FieldMemOperand(object, HeapNumber::kValueOffset));
526 } else {
527 ASSERT(destination == kCoreRegisters);
528 // Load the double from heap number to dst1 and dst2 in double format.
529 __ lw(dst1, FieldMemOperand(object, HeapNumber::kValueOffset));
530 __ lw(dst2, FieldMemOperand(object,
531 HeapNumber::kValueOffset + kPointerSize));
532 }
533 __ Branch(&done);
534
535 // Handle loading a double from a smi.
536 __ bind(&is_smi);
537 if (CpuFeatures::IsSupported(FPU)) {
538 CpuFeatures::Scope scope(FPU);
539 // Convert smi to double using FPU instructions.
540 __ SmiUntag(scratch1, object);
541 __ mtc1(scratch1, dst);
542 __ cvt_d_w(dst, dst);
543 if (destination == kCoreRegisters) {
544 // Load the converted smi to dst1 and dst2 in double format.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000545 __ Move(dst1, dst2, dst);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000546 }
547 } else {
548 ASSERT(destination == kCoreRegisters);
549 // Write smi to dst1 and dst2 double format.
550 __ mov(scratch1, object);
551 ConvertToDoubleStub stub(dst2, dst1, scratch1, scratch2);
552 __ push(ra);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +0000553 __ Call(stub.GetCode());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000554 __ pop(ra);
555 }
556
557 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +0000558}
559
560
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000561void FloatingPointHelper::ConvertNumberToInt32(MacroAssembler* masm,
562 Register object,
563 Register dst,
564 Register heap_number_map,
565 Register scratch1,
566 Register scratch2,
567 Register scratch3,
568 FPURegister double_scratch,
569 Label* not_number) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000570 if (FLAG_debug_code) {
571 __ AbortIfNotRootValue(heap_number_map,
572 Heap::kHeapNumberMapRootIndex,
573 "HeapNumberMap register clobbered.");
574 }
575 Label is_smi;
576 Label done;
577 Label not_in_int32_range;
578
579 __ JumpIfSmi(object, &is_smi);
580 __ lw(scratch1, FieldMemOperand(object, HeapNumber::kMapOffset));
581 __ Branch(not_number, ne, scratch1, Operand(heap_number_map));
582 __ ConvertToInt32(object,
583 dst,
584 scratch1,
585 scratch2,
586 double_scratch,
587 &not_in_int32_range);
588 __ jmp(&done);
589
590 __ bind(&not_in_int32_range);
591 __ lw(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
592 __ lw(scratch2, FieldMemOperand(object, HeapNumber::kMantissaOffset));
593
594 __ EmitOutOfInt32RangeTruncate(dst,
595 scratch1,
596 scratch2,
597 scratch3);
598
599 __ jmp(&done);
600
601 __ bind(&is_smi);
602 __ SmiUntag(dst, object);
603 __ bind(&done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000604}
605
606
607void FloatingPointHelper::ConvertIntToDouble(MacroAssembler* masm,
608 Register int_scratch,
609 Destination destination,
610 FPURegister double_dst,
611 Register dst1,
612 Register dst2,
613 Register scratch2,
614 FPURegister single_scratch) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000615 ASSERT(!int_scratch.is(scratch2));
danno@chromium.org40cb8782011-05-25 07:58:50 +0000616 ASSERT(!int_scratch.is(dst1));
617 ASSERT(!int_scratch.is(dst2));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000618
619 Label done;
620
621 if (CpuFeatures::IsSupported(FPU)) {
622 CpuFeatures::Scope scope(FPU);
623 __ mtc1(int_scratch, single_scratch);
624 __ cvt_d_w(double_dst, single_scratch);
625 if (destination == kCoreRegisters) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000626 __ Move(dst1, dst2, double_dst);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000627 }
628 } else {
629 Label fewer_than_20_useful_bits;
630 // Expected output:
631 // | dst2 | dst1 |
632 // | s | exp | mantissa |
633
634 // Check for zero.
635 __ mov(dst2, int_scratch);
636 __ mov(dst1, int_scratch);
637 __ Branch(&done, eq, int_scratch, Operand(zero_reg));
638
639 // Preload the sign of the value.
640 __ And(dst2, int_scratch, Operand(HeapNumber::kSignMask));
641 // Get the absolute value of the object (as an unsigned integer).
642 Label skip_sub;
643 __ Branch(&skip_sub, ge, dst2, Operand(zero_reg));
644 __ Subu(int_scratch, zero_reg, int_scratch);
645 __ bind(&skip_sub);
646
647 // Get mantisssa[51:20].
648
649 // Get the position of the first set bit.
650 __ clz(dst1, int_scratch);
651 __ li(scratch2, 31);
652 __ Subu(dst1, scratch2, dst1);
653
654 // Set the exponent.
655 __ Addu(scratch2, dst1, Operand(HeapNumber::kExponentBias));
656 __ Ins(dst2, scratch2,
657 HeapNumber::kExponentShift, HeapNumber::kExponentBits);
658
659 // Clear the first non null bit.
660 __ li(scratch2, Operand(1));
661 __ sllv(scratch2, scratch2, dst1);
662 __ li(at, -1);
663 __ Xor(scratch2, scratch2, at);
664 __ And(int_scratch, int_scratch, scratch2);
665
666 // Get the number of bits to set in the lower part of the mantissa.
667 __ Subu(scratch2, dst1, Operand(HeapNumber::kMantissaBitsInTopWord));
668 __ Branch(&fewer_than_20_useful_bits, lt, scratch2, Operand(zero_reg));
669 // Set the higher 20 bits of the mantissa.
670 __ srlv(at, int_scratch, scratch2);
671 __ or_(dst2, dst2, at);
672 __ li(at, 32);
673 __ subu(scratch2, at, scratch2);
674 __ sllv(dst1, int_scratch, scratch2);
675 __ Branch(&done);
676
677 __ bind(&fewer_than_20_useful_bits);
678 __ li(at, HeapNumber::kMantissaBitsInTopWord);
679 __ subu(scratch2, at, dst1);
680 __ sllv(scratch2, int_scratch, scratch2);
681 __ Or(dst2, dst2, scratch2);
682 // Set dst1 to 0.
683 __ mov(dst1, zero_reg);
684 }
685 __ bind(&done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000686}
687
688
689void FloatingPointHelper::LoadNumberAsInt32Double(MacroAssembler* masm,
690 Register object,
691 Destination destination,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000692 DoubleRegister double_dst,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000693 Register dst1,
694 Register dst2,
695 Register heap_number_map,
696 Register scratch1,
697 Register scratch2,
698 FPURegister single_scratch,
699 Label* not_int32) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000700 ASSERT(!scratch1.is(object) && !scratch2.is(object));
701 ASSERT(!scratch1.is(scratch2));
702 ASSERT(!heap_number_map.is(object) &&
703 !heap_number_map.is(scratch1) &&
704 !heap_number_map.is(scratch2));
705
706 Label done, obj_is_not_smi;
707
708 __ JumpIfNotSmi(object, &obj_is_not_smi);
709 __ SmiUntag(scratch1, object);
710 ConvertIntToDouble(masm, scratch1, destination, double_dst, dst1, dst2,
711 scratch2, single_scratch);
712 __ Branch(&done);
713
714 __ bind(&obj_is_not_smi);
715 if (FLAG_debug_code) {
716 __ AbortIfNotRootValue(heap_number_map,
717 Heap::kHeapNumberMapRootIndex,
718 "HeapNumberMap register clobbered.");
719 }
720 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
721
722 // Load the number.
723 if (CpuFeatures::IsSupported(FPU)) {
724 CpuFeatures::Scope scope(FPU);
725 // Load the double value.
726 __ ldc1(double_dst, FieldMemOperand(object, HeapNumber::kValueOffset));
727
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000728 Register except_flag = scratch2;
729 __ EmitFPUTruncate(kRoundToZero,
730 single_scratch,
731 double_dst,
732 scratch1,
733 except_flag,
734 kCheckForInexactConversion);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000735
736 // Jump to not_int32 if the operation did not succeed.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000737 __ Branch(not_int32, ne, except_flag, Operand(zero_reg));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000738
739 if (destination == kCoreRegisters) {
danno@chromium.org40cb8782011-05-25 07:58:50 +0000740 __ Move(dst1, dst2, double_dst);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000741 }
742
743 } else {
744 ASSERT(!scratch1.is(object) && !scratch2.is(object));
745 // Load the double value in the destination registers.
746 __ lw(dst2, FieldMemOperand(object, HeapNumber::kExponentOffset));
747 __ lw(dst1, FieldMemOperand(object, HeapNumber::kMantissaOffset));
748
749 // Check for 0 and -0.
750 __ And(scratch1, dst1, Operand(~HeapNumber::kSignMask));
751 __ Or(scratch1, scratch1, Operand(dst2));
752 __ Branch(&done, eq, scratch1, Operand(zero_reg));
753
754 // Check that the value can be exactly represented by a 32-bit integer.
755 // Jump to not_int32 if that's not the case.
756 DoubleIs32BitInteger(masm, dst1, dst2, scratch1, scratch2, not_int32);
757
758 // dst1 and dst2 were trashed. Reload the double value.
759 __ lw(dst2, FieldMemOperand(object, HeapNumber::kExponentOffset));
760 __ lw(dst1, FieldMemOperand(object, HeapNumber::kMantissaOffset));
761 }
762
763 __ bind(&done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000764}
765
766
767void FloatingPointHelper::LoadNumberAsInt32(MacroAssembler* masm,
768 Register object,
769 Register dst,
770 Register heap_number_map,
771 Register scratch1,
772 Register scratch2,
773 Register scratch3,
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000774 DoubleRegister double_scratch,
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000775 Label* not_int32) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000776 ASSERT(!dst.is(object));
777 ASSERT(!scratch1.is(object) && !scratch2.is(object) && !scratch3.is(object));
778 ASSERT(!scratch1.is(scratch2) &&
779 !scratch1.is(scratch3) &&
780 !scratch2.is(scratch3));
781
782 Label done;
783
784 // Untag the object into the destination register.
785 __ SmiUntag(dst, object);
786 // Just return if the object is a smi.
787 __ JumpIfSmi(object, &done);
788
789 if (FLAG_debug_code) {
790 __ AbortIfNotRootValue(heap_number_map,
791 Heap::kHeapNumberMapRootIndex,
792 "HeapNumberMap register clobbered.");
793 }
794 __ JumpIfNotHeapNumber(object, heap_number_map, scratch1, not_int32);
795
796 // Object is a heap number.
797 // Convert the floating point value to a 32-bit integer.
798 if (CpuFeatures::IsSupported(FPU)) {
799 CpuFeatures::Scope scope(FPU);
800 // Load the double value.
801 __ ldc1(double_scratch, FieldMemOperand(object, HeapNumber::kValueOffset));
802
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000803 FPURegister single_scratch = double_scratch.low();
804 Register except_flag = scratch2;
805 __ EmitFPUTruncate(kRoundToZero,
806 single_scratch,
807 double_scratch,
808 scratch1,
809 except_flag,
810 kCheckForInexactConversion);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000811
812 // Jump to not_int32 if the operation did not succeed.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000813 __ Branch(not_int32, ne, except_flag, Operand(zero_reg));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000814 // Get the result in the destination register.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000815 __ mfc1(dst, single_scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000816
817 } else {
818 // Load the double value in the destination registers.
819 __ lw(scratch2, FieldMemOperand(object, HeapNumber::kExponentOffset));
820 __ lw(scratch1, FieldMemOperand(object, HeapNumber::kMantissaOffset));
821
822 // Check for 0 and -0.
823 __ And(dst, scratch1, Operand(~HeapNumber::kSignMask));
824 __ Or(dst, scratch2, Operand(dst));
825 __ Branch(&done, eq, dst, Operand(zero_reg));
826
827 DoubleIs32BitInteger(masm, scratch1, scratch2, dst, scratch3, not_int32);
828
829 // Registers state after DoubleIs32BitInteger.
830 // dst: mantissa[51:20].
831 // scratch2: 1
832
833 // Shift back the higher bits of the mantissa.
834 __ srlv(dst, dst, scratch3);
835 // Set the implicit first bit.
836 __ li(at, 32);
837 __ subu(scratch3, at, scratch3);
838 __ sllv(scratch2, scratch2, scratch3);
839 __ Or(dst, dst, scratch2);
840 // Set the sign.
841 __ lw(scratch1, FieldMemOperand(object, HeapNumber::kExponentOffset));
842 __ And(scratch1, scratch1, Operand(HeapNumber::kSignMask));
843 Label skip_sub;
844 __ Branch(&skip_sub, ge, scratch1, Operand(zero_reg));
845 __ Subu(dst, zero_reg, dst);
846 __ bind(&skip_sub);
847 }
848
849 __ bind(&done);
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000850}
851
852
853void FloatingPointHelper::DoubleIs32BitInteger(MacroAssembler* masm,
854 Register src1,
855 Register src2,
856 Register dst,
857 Register scratch,
858 Label* not_int32) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000859 // Get exponent alone in scratch.
860 __ Ext(scratch,
861 src1,
862 HeapNumber::kExponentShift,
863 HeapNumber::kExponentBits);
864
865 // Substract the bias from the exponent.
866 __ Subu(scratch, scratch, Operand(HeapNumber::kExponentBias));
867
868 // src1: higher (exponent) part of the double value.
869 // src2: lower (mantissa) part of the double value.
870 // scratch: unbiased exponent.
871
872 // Fast cases. Check for obvious non 32-bit integer values.
873 // Negative exponent cannot yield 32-bit integers.
874 __ Branch(not_int32, lt, scratch, Operand(zero_reg));
875 // Exponent greater than 31 cannot yield 32-bit integers.
876 // Also, a positive value with an exponent equal to 31 is outside of the
877 // signed 32-bit integer range.
878 // Another way to put it is that if (exponent - signbit) > 30 then the
879 // number cannot be represented as an int32.
880 Register tmp = dst;
881 __ srl(at, src1, 31);
882 __ subu(tmp, scratch, at);
883 __ Branch(not_int32, gt, tmp, Operand(30));
884 // - Bits [21:0] in the mantissa are not null.
885 __ And(tmp, src2, 0x3fffff);
886 __ Branch(not_int32, ne, tmp, Operand(zero_reg));
887
888 // Otherwise the exponent needs to be big enough to shift left all the
889 // non zero bits left. So we need the (30 - exponent) last bits of the
890 // 31 higher bits of the mantissa to be null.
891 // Because bits [21:0] are null, we can check instead that the
892 // (32 - exponent) last bits of the 32 higher bits of the mantisssa are null.
893
894 // Get the 32 higher bits of the mantissa in dst.
895 __ Ext(dst,
896 src2,
897 HeapNumber::kMantissaBitsInTopWord,
898 32 - HeapNumber::kMantissaBitsInTopWord);
899 __ sll(at, src1, HeapNumber::kNonMantissaBitsInTopWord);
900 __ or_(dst, dst, at);
901
902 // Create the mask and test the lower bits (of the higher bits).
903 __ li(at, 32);
904 __ subu(scratch, at, scratch);
905 __ li(src2, 1);
906 __ sllv(src1, src2, scratch);
907 __ Subu(src1, src1, Operand(1));
908 __ And(src1, dst, src1);
909 __ Branch(not_int32, ne, src1, Operand(zero_reg));
karlklose@chromium.org83a47282011-05-11 11:54:09 +0000910}
911
912
913void FloatingPointHelper::CallCCodeForDoubleOperation(
914 MacroAssembler* masm,
915 Token::Value op,
916 Register heap_number_result,
917 Register scratch) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000918 // Using core registers:
919 // a0: Left value (least significant part of mantissa).
920 // a1: Left value (sign, exponent, top of mantissa).
921 // a2: Right value (least significant part of mantissa).
922 // a3: Right value (sign, exponent, top of mantissa).
923
924 // Assert that heap_number_result is saved.
925 // We currently always use s0 to pass it.
926 ASSERT(heap_number_result.is(s0));
927
928 // Push the current return address before the C call.
929 __ push(ra);
930 __ PrepareCallCFunction(4, scratch); // Two doubles are 4 arguments.
931 if (!IsMipsSoftFloatABI) {
932 CpuFeatures::Scope scope(FPU);
933 // We are not using MIPS FPU instructions, and parameters for the runtime
934 // function call are prepaired in a0-a3 registers, but function we are
935 // calling is compiled with hard-float flag and expecting hard float ABI
936 // (parameters in f12/f14 registers). We need to copy parameters from
937 // a0-a3 registers to f12/f14 register pairs.
danno@chromium.org40cb8782011-05-25 07:58:50 +0000938 __ Move(f12, a0, a1);
939 __ Move(f14, a2, a3);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000940 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +0000941 {
942 AllowExternalCallThatCantCauseGC scope(masm);
943 __ CallCFunction(
944 ExternalReference::double_fp_operation(op, masm->isolate()), 0, 2);
945 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000946 // Store answer in the overwritable heap number.
947 if (!IsMipsSoftFloatABI) {
948 CpuFeatures::Scope scope(FPU);
949 // Double returned in register f0.
950 __ sdc1(f0, FieldMemOperand(heap_number_result, HeapNumber::kValueOffset));
951 } else {
952 // Double returned in registers v0 and v1.
953 __ sw(v1, FieldMemOperand(heap_number_result, HeapNumber::kExponentOffset));
954 __ sw(v0, FieldMemOperand(heap_number_result, HeapNumber::kMantissaOffset));
955 }
956 // Place heap_number_result in v0 and return to the pushed return address.
957 __ mov(v0, heap_number_result);
958 __ pop(ra);
959 __ Ret();
lrn@chromium.org7516f052011-03-30 08:52:27 +0000960}
961
962
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +0000963bool WriteInt32ToHeapNumberStub::IsPregenerated() {
964 // These variants are compiled ahead of time. See next method.
965 if (the_int_.is(a1) &&
966 the_heap_number_.is(v0) &&
967 scratch_.is(a2) &&
968 sign_.is(a3)) {
969 return true;
970 }
971 if (the_int_.is(a2) &&
972 the_heap_number_.is(v0) &&
973 scratch_.is(a3) &&
974 sign_.is(a0)) {
975 return true;
976 }
977 // Other register combinations are generated as and when they are needed,
978 // so it is unsafe to call them from stubs (we can't generate a stub while
979 // we are generating a stub).
980 return false;
981}
982
983
984void WriteInt32ToHeapNumberStub::GenerateFixedRegStubsAheadOfTime() {
985 WriteInt32ToHeapNumberStub stub1(a1, v0, a2, a3);
986 WriteInt32ToHeapNumberStub stub2(a2, v0, a3, a0);
987 stub1.GetCode()->set_is_pregenerated(true);
988 stub2.GetCode()->set_is_pregenerated(true);
989}
990
991
lrn@chromium.org7516f052011-03-30 08:52:27 +0000992// See comment for class, this does NOT work for int32's that are in Smi range.
993void WriteInt32ToHeapNumberStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +0000994 Label max_negative_int;
995 // the_int_ has the answer which is a signed int32 but not a Smi.
996 // We test for the special value that has a different exponent.
997 STATIC_ASSERT(HeapNumber::kSignMask == 0x80000000u);
998 // Test sign, and save for later conditionals.
999 __ And(sign_, the_int_, Operand(0x80000000u));
1000 __ Branch(&max_negative_int, eq, the_int_, Operand(0x80000000u));
1001
1002 // Set up the correct exponent in scratch_. All non-Smi int32s have the same.
1003 // A non-Smi integer is 1.xxx * 2^30 so the exponent is 30 (biased).
1004 uint32_t non_smi_exponent =
1005 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
1006 __ li(scratch_, Operand(non_smi_exponent));
1007 // Set the sign bit in scratch_ if the value was negative.
1008 __ or_(scratch_, scratch_, sign_);
1009 // Subtract from 0 if the value was negative.
1010 __ subu(at, zero_reg, the_int_);
1011 __ movn(the_int_, at, sign_);
1012 // We should be masking the implict first digit of the mantissa away here,
1013 // but it just ends up combining harmlessly with the last digit of the
1014 // exponent that happens to be 1. The sign bit is 0 so we shift 10 to get
1015 // the most significant 1 to hit the last bit of the 12 bit sign and exponent.
1016 ASSERT(((1 << HeapNumber::kExponentShift) & non_smi_exponent) != 0);
1017 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
1018 __ srl(at, the_int_, shift_distance);
1019 __ or_(scratch_, scratch_, at);
1020 __ sw(scratch_, FieldMemOperand(the_heap_number_,
1021 HeapNumber::kExponentOffset));
1022 __ sll(scratch_, the_int_, 32 - shift_distance);
1023 __ sw(scratch_, FieldMemOperand(the_heap_number_,
1024 HeapNumber::kMantissaOffset));
1025 __ Ret();
1026
1027 __ bind(&max_negative_int);
1028 // The max negative int32 is stored as a positive number in the mantissa of
1029 // a double because it uses a sign bit instead of using two's complement.
1030 // The actual mantissa bits stored are all 0 because the implicit most
1031 // significant 1 bit is not stored.
1032 non_smi_exponent += 1 << HeapNumber::kExponentShift;
1033 __ li(scratch_, Operand(HeapNumber::kSignMask | non_smi_exponent));
1034 __ sw(scratch_,
1035 FieldMemOperand(the_heap_number_, HeapNumber::kExponentOffset));
1036 __ mov(scratch_, zero_reg);
1037 __ sw(scratch_,
1038 FieldMemOperand(the_heap_number_, HeapNumber::kMantissaOffset));
1039 __ Ret();
1040}
1041
1042
1043// Handle the case where the lhs and rhs are the same object.
1044// Equality is almost reflexive (everything but NaN), so this is a test
1045// for "identity and not NaN".
1046static void EmitIdenticalObjectComparison(MacroAssembler* masm,
1047 Label* slow,
1048 Condition cc,
1049 bool never_nan_nan) {
1050 Label not_identical;
1051 Label heap_number, return_equal;
1052 Register exp_mask_reg = t5;
1053
1054 __ Branch(&not_identical, ne, a0, Operand(a1));
1055
1056 // The two objects are identical. If we know that one of them isn't NaN then
1057 // we now know they test equal.
1058 if (cc != eq || !never_nan_nan) {
1059 __ li(exp_mask_reg, Operand(HeapNumber::kExponentMask));
1060
1061 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
1062 // so we do the second best thing - test it ourselves.
1063 // They are both equal and they are not both Smis so both of them are not
1064 // Smis. If it's not a heap number, then return equal.
1065 if (cc == less || cc == greater) {
1066 __ GetObjectType(a0, t4, t4);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001067 __ Branch(slow, greater, t4, Operand(FIRST_SPEC_OBJECT_TYPE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001068 } else {
1069 __ GetObjectType(a0, t4, t4);
1070 __ Branch(&heap_number, eq, t4, Operand(HEAP_NUMBER_TYPE));
1071 // Comparing JS objects with <=, >= is complicated.
1072 if (cc != eq) {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001073 __ Branch(slow, greater, t4, Operand(FIRST_SPEC_OBJECT_TYPE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001074 // Normally here we fall through to return_equal, but undefined is
1075 // special: (undefined == undefined) == true, but
1076 // (undefined <= undefined) == false! See ECMAScript 11.8.5.
1077 if (cc == less_equal || cc == greater_equal) {
1078 __ Branch(&return_equal, ne, t4, Operand(ODDBALL_TYPE));
1079 __ LoadRoot(t2, Heap::kUndefinedValueRootIndex);
1080 __ Branch(&return_equal, ne, a0, Operand(t2));
1081 if (cc == le) {
1082 // undefined <= undefined should fail.
1083 __ li(v0, Operand(GREATER));
1084 } else {
1085 // undefined >= undefined should fail.
1086 __ li(v0, Operand(LESS));
1087 }
1088 __ Ret();
1089 }
1090 }
1091 }
1092 }
1093
1094 __ bind(&return_equal);
1095 if (cc == less) {
1096 __ li(v0, Operand(GREATER)); // Things aren't less than themselves.
1097 } else if (cc == greater) {
1098 __ li(v0, Operand(LESS)); // Things aren't greater than themselves.
1099 } else {
1100 __ mov(v0, zero_reg); // Things are <=, >=, ==, === themselves.
1101 }
1102 __ Ret();
1103
1104 if (cc != eq || !never_nan_nan) {
1105 // For less and greater we don't have to check for NaN since the result of
1106 // x < x is false regardless. For the others here is some code to check
1107 // for NaN.
1108 if (cc != lt && cc != gt) {
1109 __ bind(&heap_number);
1110 // It is a heap number, so return non-equal if it's NaN and equal if it's
1111 // not NaN.
1112
1113 // The representation of NaN values has all exponent bits (52..62) set,
1114 // and not all mantissa bits (0..51) clear.
1115 // Read top bits of double representation (second word of value).
1116 __ lw(t2, FieldMemOperand(a0, HeapNumber::kExponentOffset));
1117 // Test that exponent bits are all set.
1118 __ And(t3, t2, Operand(exp_mask_reg));
1119 // If all bits not set (ne cond), then not a NaN, objects are equal.
1120 __ Branch(&return_equal, ne, t3, Operand(exp_mask_reg));
1121
1122 // Shift out flag and all exponent bits, retaining only mantissa.
1123 __ sll(t2, t2, HeapNumber::kNonMantissaBitsInTopWord);
1124 // Or with all low-bits of mantissa.
1125 __ lw(t3, FieldMemOperand(a0, HeapNumber::kMantissaOffset));
1126 __ Or(v0, t3, Operand(t2));
1127 // For equal we already have the right value in v0: Return zero (equal)
1128 // if all bits in mantissa are zero (it's an Infinity) and non-zero if
1129 // not (it's a NaN). For <= and >= we need to load v0 with the failing
1130 // value if it's a NaN.
1131 if (cc != eq) {
1132 // All-zero means Infinity means equal.
1133 __ Ret(eq, v0, Operand(zero_reg));
1134 if (cc == le) {
1135 __ li(v0, Operand(GREATER)); // NaN <= NaN should fail.
1136 } else {
1137 __ li(v0, Operand(LESS)); // NaN >= NaN should fail.
1138 }
1139 }
1140 __ Ret();
1141 }
1142 // No fall through here.
1143 }
1144
1145 __ bind(&not_identical);
1146}
1147
1148
1149static void EmitSmiNonsmiComparison(MacroAssembler* masm,
1150 Register lhs,
1151 Register rhs,
1152 Label* both_loaded_as_doubles,
1153 Label* slow,
1154 bool strict) {
1155 ASSERT((lhs.is(a0) && rhs.is(a1)) ||
1156 (lhs.is(a1) && rhs.is(a0)));
1157
1158 Label lhs_is_smi;
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00001159 __ JumpIfSmi(lhs, &lhs_is_smi);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001160 // Rhs is a Smi.
1161 // Check whether the non-smi is a heap number.
1162 __ GetObjectType(lhs, t4, t4);
1163 if (strict) {
1164 // If lhs was not a number and rhs was a Smi then strict equality cannot
1165 // succeed. Return non-equal (lhs is already not zero).
1166 __ mov(v0, lhs);
1167 __ Ret(ne, t4, Operand(HEAP_NUMBER_TYPE));
1168 } else {
1169 // Smi compared non-strictly with a non-Smi non-heap-number. Call
1170 // the runtime.
1171 __ Branch(slow, ne, t4, Operand(HEAP_NUMBER_TYPE));
1172 }
1173
1174 // Rhs is a smi, lhs is a number.
1175 // Convert smi rhs to double.
1176 if (CpuFeatures::IsSupported(FPU)) {
1177 CpuFeatures::Scope scope(FPU);
1178 __ sra(at, rhs, kSmiTagSize);
1179 __ mtc1(at, f14);
1180 __ cvt_d_w(f14, f14);
1181 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1182 } else {
1183 // Load lhs to a double in a2, a3.
1184 __ lw(a3, FieldMemOperand(lhs, HeapNumber::kValueOffset + 4));
1185 __ lw(a2, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1186
1187 // Write Smi from rhs to a1 and a0 in double format. t5 is scratch.
1188 __ mov(t6, rhs);
1189 ConvertToDoubleStub stub1(a1, a0, t6, t5);
1190 __ push(ra);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001191 __ Call(stub1.GetCode());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001192
1193 __ pop(ra);
1194 }
1195
1196 // We now have both loaded as doubles.
1197 __ jmp(both_loaded_as_doubles);
1198
1199 __ bind(&lhs_is_smi);
1200 // Lhs is a Smi. Check whether the non-smi is a heap number.
1201 __ GetObjectType(rhs, t4, t4);
1202 if (strict) {
1203 // If lhs was not a number and rhs was a Smi then strict equality cannot
1204 // succeed. Return non-equal.
1205 __ li(v0, Operand(1));
1206 __ Ret(ne, t4, Operand(HEAP_NUMBER_TYPE));
1207 } else {
1208 // Smi compared non-strictly with a non-Smi non-heap-number. Call
1209 // the runtime.
1210 __ Branch(slow, ne, t4, Operand(HEAP_NUMBER_TYPE));
1211 }
1212
1213 // Lhs is a smi, rhs is a number.
1214 // Convert smi lhs to double.
1215 if (CpuFeatures::IsSupported(FPU)) {
1216 CpuFeatures::Scope scope(FPU);
1217 __ sra(at, lhs, kSmiTagSize);
1218 __ mtc1(at, f12);
1219 __ cvt_d_w(f12, f12);
1220 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1221 } else {
1222 // Convert lhs to a double format. t5 is scratch.
1223 __ mov(t6, lhs);
1224 ConvertToDoubleStub stub2(a3, a2, t6, t5);
1225 __ push(ra);
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00001226 __ Call(stub2.GetCode());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001227 __ pop(ra);
1228 // Load rhs to a double in a1, a0.
1229 if (rhs.is(a0)) {
1230 __ lw(a1, FieldMemOperand(rhs, HeapNumber::kValueOffset + 4));
1231 __ lw(a0, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1232 } else {
1233 __ lw(a0, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1234 __ lw(a1, FieldMemOperand(rhs, HeapNumber::kValueOffset + 4));
1235 }
1236 }
1237 // Fall through to both_loaded_as_doubles.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001238}
1239
1240
1241void EmitNanCheck(MacroAssembler* masm, Condition cc) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001242 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1243 if (CpuFeatures::IsSupported(FPU)) {
1244 CpuFeatures::Scope scope(FPU);
1245 // Lhs and rhs are already loaded to f12 and f14 register pairs.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001246 __ Move(t0, t1, f14);
1247 __ Move(t2, t3, f12);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001248 } else {
1249 // Lhs and rhs are already loaded to GP registers.
1250 __ mov(t0, a0); // a0 has LS 32 bits of rhs.
1251 __ mov(t1, a1); // a1 has MS 32 bits of rhs.
1252 __ mov(t2, a2); // a2 has LS 32 bits of lhs.
1253 __ mov(t3, a3); // a3 has MS 32 bits of lhs.
1254 }
1255 Register rhs_exponent = exp_first ? t0 : t1;
1256 Register lhs_exponent = exp_first ? t2 : t3;
1257 Register rhs_mantissa = exp_first ? t1 : t0;
1258 Register lhs_mantissa = exp_first ? t3 : t2;
1259 Label one_is_nan, neither_is_nan;
1260 Label lhs_not_nan_exp_mask_is_loaded;
1261
1262 Register exp_mask_reg = t4;
1263 __ li(exp_mask_reg, HeapNumber::kExponentMask);
1264 __ and_(t5, lhs_exponent, exp_mask_reg);
1265 __ Branch(&lhs_not_nan_exp_mask_is_loaded, ne, t5, Operand(exp_mask_reg));
1266
1267 __ sll(t5, lhs_exponent, HeapNumber::kNonMantissaBitsInTopWord);
1268 __ Branch(&one_is_nan, ne, t5, Operand(zero_reg));
1269
1270 __ Branch(&one_is_nan, ne, lhs_mantissa, Operand(zero_reg));
1271
1272 __ li(exp_mask_reg, HeapNumber::kExponentMask);
1273 __ bind(&lhs_not_nan_exp_mask_is_loaded);
1274 __ and_(t5, rhs_exponent, exp_mask_reg);
1275
1276 __ Branch(&neither_is_nan, ne, t5, Operand(exp_mask_reg));
1277
1278 __ sll(t5, rhs_exponent, HeapNumber::kNonMantissaBitsInTopWord);
1279 __ Branch(&one_is_nan, ne, t5, Operand(zero_reg));
1280
1281 __ Branch(&neither_is_nan, eq, rhs_mantissa, Operand(zero_reg));
1282
1283 __ bind(&one_is_nan);
1284 // NaN comparisons always fail.
1285 // Load whatever we need in v0 to make the comparison fail.
1286 if (cc == lt || cc == le) {
1287 __ li(v0, Operand(GREATER));
1288 } else {
1289 __ li(v0, Operand(LESS));
1290 }
1291 __ Ret(); // Return.
1292
1293 __ bind(&neither_is_nan);
1294}
1295
1296
1297static void EmitTwoNonNanDoubleComparison(MacroAssembler* masm, Condition cc) {
1298 // f12 and f14 have the two doubles. Neither is a NaN.
1299 // Call a native function to do a comparison between two non-NaNs.
1300 // Call C routine that may not cause GC or other trouble.
1301 // We use a call_was and return manually because we need arguments slots to
1302 // be freed.
1303
1304 Label return_result_not_equal, return_result_equal;
1305 if (cc == eq) {
1306 // Doubles are not equal unless they have the same bit pattern.
1307 // Exception: 0 and -0.
1308 bool exp_first = (HeapNumber::kExponentOffset == HeapNumber::kValueOffset);
1309 if (CpuFeatures::IsSupported(FPU)) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00001310 CpuFeatures::Scope scope(FPU);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001311 // Lhs and rhs are already loaded to f12 and f14 register pairs.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001312 __ Move(t0, t1, f14);
1313 __ Move(t2, t3, f12);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001314 } else {
1315 // Lhs and rhs are already loaded to GP registers.
1316 __ mov(t0, a0); // a0 has LS 32 bits of rhs.
1317 __ mov(t1, a1); // a1 has MS 32 bits of rhs.
1318 __ mov(t2, a2); // a2 has LS 32 bits of lhs.
1319 __ mov(t3, a3); // a3 has MS 32 bits of lhs.
1320 }
1321 Register rhs_exponent = exp_first ? t0 : t1;
1322 Register lhs_exponent = exp_first ? t2 : t3;
1323 Register rhs_mantissa = exp_first ? t1 : t0;
1324 Register lhs_mantissa = exp_first ? t3 : t2;
1325
1326 __ xor_(v0, rhs_mantissa, lhs_mantissa);
1327 __ Branch(&return_result_not_equal, ne, v0, Operand(zero_reg));
1328
1329 __ subu(v0, rhs_exponent, lhs_exponent);
1330 __ Branch(&return_result_equal, eq, v0, Operand(zero_reg));
1331 // 0, -0 case.
1332 __ sll(rhs_exponent, rhs_exponent, kSmiTagSize);
1333 __ sll(lhs_exponent, lhs_exponent, kSmiTagSize);
1334 __ or_(t4, rhs_exponent, lhs_exponent);
1335 __ or_(t4, t4, rhs_mantissa);
1336
1337 __ Branch(&return_result_not_equal, ne, t4, Operand(zero_reg));
1338
1339 __ bind(&return_result_equal);
1340 __ li(v0, Operand(EQUAL));
1341 __ Ret();
1342 }
1343
1344 __ bind(&return_result_not_equal);
1345
1346 if (!CpuFeatures::IsSupported(FPU)) {
1347 __ push(ra);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001348 __ PrepareCallCFunction(0, 2, t4);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001349 if (!IsMipsSoftFloatABI) {
1350 // We are not using MIPS FPU instructions, and parameters for the runtime
1351 // function call are prepaired in a0-a3 registers, but function we are
1352 // calling is compiled with hard-float flag and expecting hard float ABI
1353 // (parameters in f12/f14 registers). We need to copy parameters from
1354 // a0-a3 registers to f12/f14 register pairs.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001355 __ Move(f12, a0, a1);
1356 __ Move(f14, a2, a3);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001357 }
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001358
1359 AllowExternalCallThatCantCauseGC scope(masm);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001360 __ CallCFunction(ExternalReference::compare_doubles(masm->isolate()),
1361 0, 2);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001362 __ pop(ra); // Because this function returns int, result is in v0.
1363 __ Ret();
1364 } else {
1365 CpuFeatures::Scope scope(FPU);
1366 Label equal, less_than;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001367 __ BranchF(&equal, NULL, eq, f12, f14);
1368 __ BranchF(&less_than, NULL, lt, f12, f14);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001369
1370 // Not equal, not less, not NaN, must be greater.
1371 __ li(v0, Operand(GREATER));
1372 __ Ret();
1373
1374 __ bind(&equal);
1375 __ li(v0, Operand(EQUAL));
1376 __ Ret();
1377
1378 __ bind(&less_than);
1379 __ li(v0, Operand(LESS));
1380 __ Ret();
1381 }
1382}
1383
1384
1385static void EmitStrictTwoHeapObjectCompare(MacroAssembler* masm,
1386 Register lhs,
1387 Register rhs) {
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001388 // If either operand is a JS object or an oddball value, then they are
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001389 // not equal since their pointers are different.
1390 // There is no test for undetectability in strict equality.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001391 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001392 Label first_non_object;
1393 // Get the type of the first operand into a2 and compare it with
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001394 // FIRST_SPEC_OBJECT_TYPE.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001395 __ GetObjectType(lhs, a2, a2);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001396 __ Branch(&first_non_object, less, a2, Operand(FIRST_SPEC_OBJECT_TYPE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001397
1398 // Return non-zero.
1399 Label return_not_equal;
1400 __ bind(&return_not_equal);
1401 __ li(v0, Operand(1));
1402 __ Ret();
1403
1404 __ bind(&first_non_object);
1405 // Check for oddballs: true, false, null, undefined.
1406 __ Branch(&return_not_equal, eq, a2, Operand(ODDBALL_TYPE));
1407
1408 __ GetObjectType(rhs, a3, a3);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001409 __ Branch(&return_not_equal, greater, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001410
1411 // Check for oddballs: true, false, null, undefined.
1412 __ Branch(&return_not_equal, eq, a3, Operand(ODDBALL_TYPE));
1413
1414 // Now that we have the types we might as well check for symbol-symbol.
1415 // Ensure that no non-strings have the symbol bit set.
1416 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
1417 STATIC_ASSERT(kSymbolTag != 0);
1418 __ And(t2, a2, Operand(a3));
1419 __ And(t0, t2, Operand(kIsSymbolMask));
1420 __ Branch(&return_not_equal, ne, t0, Operand(zero_reg));
1421}
1422
1423
1424static void EmitCheckForTwoHeapNumbers(MacroAssembler* masm,
1425 Register lhs,
1426 Register rhs,
1427 Label* both_loaded_as_doubles,
1428 Label* not_heap_numbers,
1429 Label* slow) {
1430 __ GetObjectType(lhs, a3, a2);
1431 __ Branch(not_heap_numbers, ne, a2, Operand(HEAP_NUMBER_TYPE));
1432 __ lw(a2, FieldMemOperand(rhs, HeapObject::kMapOffset));
1433 // If first was a heap number & second wasn't, go to slow case.
1434 __ Branch(slow, ne, a3, Operand(a2));
1435
1436 // Both are heap numbers. Load them up then jump to the code we have
1437 // for that.
1438 if (CpuFeatures::IsSupported(FPU)) {
1439 CpuFeatures::Scope scope(FPU);
1440 __ ldc1(f12, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1441 __ ldc1(f14, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1442 } else {
1443 __ lw(a2, FieldMemOperand(lhs, HeapNumber::kValueOffset));
1444 __ lw(a3, FieldMemOperand(lhs, HeapNumber::kValueOffset + 4));
1445 if (rhs.is(a0)) {
1446 __ lw(a1, FieldMemOperand(rhs, HeapNumber::kValueOffset + 4));
1447 __ lw(a0, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1448 } else {
1449 __ lw(a0, FieldMemOperand(rhs, HeapNumber::kValueOffset));
1450 __ lw(a1, FieldMemOperand(rhs, HeapNumber::kValueOffset + 4));
1451 }
1452 }
1453 __ jmp(both_loaded_as_doubles);
1454}
1455
1456
1457// Fast negative check for symbol-to-symbol equality.
1458static void EmitCheckForSymbolsOrObjects(MacroAssembler* masm,
1459 Register lhs,
1460 Register rhs,
1461 Label* possible_strings,
1462 Label* not_both_strings) {
1463 ASSERT((lhs.is(a0) && rhs.is(a1)) ||
1464 (lhs.is(a1) && rhs.is(a0)));
1465
1466 // a2 is object type of lhs.
1467 // Ensure that no non-strings have the symbol bit set.
1468 Label object_test;
1469 STATIC_ASSERT(kSymbolTag != 0);
1470 __ And(at, a2, Operand(kIsNotStringMask));
1471 __ Branch(&object_test, ne, at, Operand(zero_reg));
1472 __ And(at, a2, Operand(kIsSymbolMask));
1473 __ Branch(possible_strings, eq, at, Operand(zero_reg));
1474 __ GetObjectType(rhs, a3, a3);
1475 __ Branch(not_both_strings, ge, a3, Operand(FIRST_NONSTRING_TYPE));
1476 __ And(at, a3, Operand(kIsSymbolMask));
1477 __ Branch(possible_strings, eq, at, Operand(zero_reg));
1478
1479 // Both are symbols. We already checked they weren't the same pointer
1480 // so they are not equal.
1481 __ li(v0, Operand(1)); // Non-zero indicates not equal.
1482 __ Ret();
1483
1484 __ bind(&object_test);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001485 __ Branch(not_both_strings, lt, a2, Operand(FIRST_SPEC_OBJECT_TYPE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001486 __ GetObjectType(rhs, a2, a3);
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00001487 __ Branch(not_both_strings, lt, a3, Operand(FIRST_SPEC_OBJECT_TYPE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001488
1489 // If both objects are undetectable, they are equal. Otherwise, they
1490 // are not equal, since they are different objects and an object is not
1491 // equal to undefined.
1492 __ lw(a3, FieldMemOperand(lhs, HeapObject::kMapOffset));
1493 __ lbu(a2, FieldMemOperand(a2, Map::kBitFieldOffset));
1494 __ lbu(a3, FieldMemOperand(a3, Map::kBitFieldOffset));
1495 __ and_(a0, a2, a3);
1496 __ And(a0, a0, Operand(1 << Map::kIsUndetectable));
1497 __ Xor(v0, a0, Operand(1 << Map::kIsUndetectable));
1498 __ Ret();
lrn@chromium.org7516f052011-03-30 08:52:27 +00001499}
1500
1501
1502void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
1503 Register object,
1504 Register result,
1505 Register scratch1,
1506 Register scratch2,
1507 Register scratch3,
1508 bool object_is_smi,
1509 Label* not_found) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001510 // Use of registers. Register result is used as a temporary.
1511 Register number_string_cache = result;
1512 Register mask = scratch3;
1513
1514 // Load the number string cache.
1515 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
1516
1517 // Make the hash mask from the length of the number string cache. It
1518 // contains two elements (number and string) for each cache entry.
1519 __ lw(mask, FieldMemOperand(number_string_cache, FixedArray::kLengthOffset));
1520 // Divide length by two (length is a smi).
1521 __ sra(mask, mask, kSmiTagSize + 1);
1522 __ Addu(mask, mask, -1); // Make mask.
1523
1524 // Calculate the entry in the number string cache. The hash value in the
1525 // number string cache for smis is just the smi value, and the hash for
1526 // doubles is the xor of the upper and lower words. See
1527 // Heap::GetNumberStringCache.
1528 Isolate* isolate = masm->isolate();
1529 Label is_smi;
1530 Label load_result_from_cache;
1531 if (!object_is_smi) {
1532 __ JumpIfSmi(object, &is_smi);
1533 if (CpuFeatures::IsSupported(FPU)) {
1534 CpuFeatures::Scope scope(FPU);
1535 __ CheckMap(object,
1536 scratch1,
1537 Heap::kHeapNumberMapRootIndex,
1538 not_found,
danno@chromium.org40cb8782011-05-25 07:58:50 +00001539 DONT_DO_SMI_CHECK);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001540
1541 STATIC_ASSERT(8 == kDoubleSize);
1542 __ Addu(scratch1,
1543 object,
1544 Operand(HeapNumber::kValueOffset - kHeapObjectTag));
1545 __ lw(scratch2, MemOperand(scratch1, kPointerSize));
1546 __ lw(scratch1, MemOperand(scratch1, 0));
1547 __ Xor(scratch1, scratch1, Operand(scratch2));
1548 __ And(scratch1, scratch1, Operand(mask));
1549
1550 // Calculate address of entry in string cache: each entry consists
1551 // of two pointer sized fields.
1552 __ sll(scratch1, scratch1, kPointerSizeLog2 + 1);
1553 __ Addu(scratch1, number_string_cache, scratch1);
1554
1555 Register probe = mask;
1556 __ lw(probe,
1557 FieldMemOperand(scratch1, FixedArray::kHeaderSize));
1558 __ JumpIfSmi(probe, not_found);
1559 __ ldc1(f12, FieldMemOperand(object, HeapNumber::kValueOffset));
1560 __ ldc1(f14, FieldMemOperand(probe, HeapNumber::kValueOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001561 __ BranchF(&load_result_from_cache, NULL, eq, f12, f14);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001562 __ Branch(not_found);
1563 } else {
1564 // Note that there is no cache check for non-FPU case, even though
1565 // it seems there could be. May be a tiny opimization for non-FPU
1566 // cores.
1567 __ Branch(not_found);
1568 }
1569 }
1570
1571 __ bind(&is_smi);
1572 Register scratch = scratch1;
1573 __ sra(scratch, object, 1); // Shift away the tag.
1574 __ And(scratch, mask, Operand(scratch));
1575
1576 // Calculate address of entry in string cache: each entry consists
1577 // of two pointer sized fields.
1578 __ sll(scratch, scratch, kPointerSizeLog2 + 1);
1579 __ Addu(scratch, number_string_cache, scratch);
1580
1581 // Check if the entry is the smi we are looking for.
1582 Register probe = mask;
1583 __ lw(probe, FieldMemOperand(scratch, FixedArray::kHeaderSize));
1584 __ Branch(not_found, ne, object, Operand(probe));
1585
1586 // Get the result from the cache.
1587 __ bind(&load_result_from_cache);
1588 __ lw(result,
1589 FieldMemOperand(scratch, FixedArray::kHeaderSize + kPointerSize));
1590
1591 __ IncrementCounter(isolate->counters()->number_to_string_native(),
1592 1,
1593 scratch1,
1594 scratch2);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001595}
1596
1597
1598void NumberToStringStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001599 Label runtime;
1600
1601 __ lw(a1, MemOperand(sp, 0));
1602
1603 // Generate code to lookup number in the number string cache.
1604 GenerateLookupNumberStringCache(masm, a1, v0, a2, a3, t0, false, &runtime);
1605 __ Addu(sp, sp, Operand(1 * kPointerSize));
1606 __ Ret();
1607
1608 __ bind(&runtime);
1609 // Handle number to string in the runtime system if not found in the cache.
1610 __ TailCallRuntime(Runtime::kNumberToString, 1, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001611}
1612
1613
1614// On entry lhs_ (lhs) and rhs_ (rhs) are the things to be compared.
1615// On exit, v0 is 0, positive, or negative (smi) to indicate the result
1616// of the comparison.
1617void CompareStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001618 Label slow; // Call builtin.
1619 Label not_smis, both_loaded_as_doubles;
1620
1621
1622 if (include_smi_compare_) {
1623 Label not_two_smis, smi_done;
1624 __ Or(a2, a1, a0);
1625 __ JumpIfNotSmi(a2, &not_two_smis);
1626 __ sra(a1, a1, 1);
1627 __ sra(a0, a0, 1);
1628 __ Subu(v0, a1, a0);
1629 __ Ret();
1630 __ bind(&not_two_smis);
1631 } else if (FLAG_debug_code) {
1632 __ Or(a2, a1, a0);
1633 __ And(a2, a2, kSmiTagMask);
1634 __ Assert(ne, "CompareStub: unexpected smi operands.",
1635 a2, Operand(zero_reg));
1636 }
1637
1638
1639 // NOTICE! This code is only reached after a smi-fast-case check, so
1640 // it is certain that at least one operand isn't a smi.
1641
1642 // Handle the case where the objects are identical. Either returns the answer
1643 // or goes to slow. Only falls through if the objects were not identical.
1644 EmitIdenticalObjectComparison(masm, &slow, cc_, never_nan_nan_);
1645
1646 // If either is a Smi (we know that not both are), then they can only
1647 // be strictly equal if the other is a HeapNumber.
1648 STATIC_ASSERT(kSmiTag == 0);
1649 ASSERT_EQ(0, Smi::FromInt(0));
1650 __ And(t2, lhs_, Operand(rhs_));
1651 __ JumpIfNotSmi(t2, &not_smis, t0);
1652 // One operand is a smi. EmitSmiNonsmiComparison generates code that can:
1653 // 1) Return the answer.
1654 // 2) Go to slow.
1655 // 3) Fall through to both_loaded_as_doubles.
1656 // 4) Jump to rhs_not_nan.
1657 // In cases 3 and 4 we have found out we were dealing with a number-number
1658 // comparison and the numbers have been loaded into f12 and f14 as doubles,
1659 // or in GP registers (a0, a1, a2, a3) depending on the presence of the FPU.
1660 EmitSmiNonsmiComparison(masm, lhs_, rhs_,
1661 &both_loaded_as_doubles, &slow, strict_);
1662
1663 __ bind(&both_loaded_as_doubles);
1664 // f12, f14 are the double representations of the left hand side
1665 // and the right hand side if we have FPU. Otherwise a2, a3 represent
1666 // left hand side and a0, a1 represent right hand side.
1667
1668 Isolate* isolate = masm->isolate();
1669 if (CpuFeatures::IsSupported(FPU)) {
1670 CpuFeatures::Scope scope(FPU);
1671 Label nan;
1672 __ li(t0, Operand(LESS));
1673 __ li(t1, Operand(GREATER));
1674 __ li(t2, Operand(EQUAL));
1675
1676 // Check if either rhs or lhs is NaN.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001677 __ BranchF(NULL, &nan, eq, f12, f14);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001678
1679 // Check if LESS condition is satisfied. If true, move conditionally
1680 // result to v0.
1681 __ c(OLT, D, f12, f14);
1682 __ movt(v0, t0);
1683 // Use previous check to store conditionally to v0 oposite condition
1684 // (GREATER). If rhs is equal to lhs, this will be corrected in next
1685 // check.
1686 __ movf(v0, t1);
1687 // Check if EQUAL condition is satisfied. If true, move conditionally
1688 // result to v0.
1689 __ c(EQ, D, f12, f14);
1690 __ movt(v0, t2);
1691
1692 __ Ret();
1693
1694 __ bind(&nan);
1695 // NaN comparisons always fail.
1696 // Load whatever we need in v0 to make the comparison fail.
1697 if (cc_ == lt || cc_ == le) {
1698 __ li(v0, Operand(GREATER));
1699 } else {
1700 __ li(v0, Operand(LESS));
1701 }
1702 __ Ret();
1703 } else {
1704 // Checks for NaN in the doubles we have loaded. Can return the answer or
1705 // fall through if neither is a NaN. Also binds rhs_not_nan.
1706 EmitNanCheck(masm, cc_);
1707
1708 // Compares two doubles that are not NaNs. Returns the answer.
1709 // Never falls through.
1710 EmitTwoNonNanDoubleComparison(masm, cc_);
1711 }
1712
1713 __ bind(&not_smis);
1714 // At this point we know we are dealing with two different objects,
1715 // and neither of them is a Smi. The objects are in lhs_ and rhs_.
1716 if (strict_) {
1717 // This returns non-equal for some object types, or falls through if it
1718 // was not lucky.
1719 EmitStrictTwoHeapObjectCompare(masm, lhs_, rhs_);
1720 }
1721
1722 Label check_for_symbols;
1723 Label flat_string_check;
1724 // Check for heap-number-heap-number comparison. Can jump to slow case,
1725 // or load both doubles and jump to the code that handles
1726 // that case. If the inputs are not doubles then jumps to check_for_symbols.
1727 // In this case a2 will contain the type of lhs_.
1728 EmitCheckForTwoHeapNumbers(masm,
1729 lhs_,
1730 rhs_,
1731 &both_loaded_as_doubles,
1732 &check_for_symbols,
1733 &flat_string_check);
1734
1735 __ bind(&check_for_symbols);
1736 if (cc_ == eq && !strict_) {
1737 // Returns an answer for two symbols or two detectable objects.
1738 // Otherwise jumps to string case or not both strings case.
1739 // Assumes that a2 is the type of lhs_ on entry.
1740 EmitCheckForSymbolsOrObjects(masm, lhs_, rhs_, &flat_string_check, &slow);
1741 }
1742
1743 // Check for both being sequential ASCII strings, and inline if that is the
1744 // case.
1745 __ bind(&flat_string_check);
1746
1747 __ JumpIfNonSmisNotBothSequentialAsciiStrings(lhs_, rhs_, a2, a3, &slow);
1748
1749 __ IncrementCounter(isolate->counters()->string_compare_native(), 1, a2, a3);
1750 if (cc_ == eq) {
1751 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
1752 lhs_,
1753 rhs_,
1754 a2,
1755 a3,
1756 t0);
1757 } else {
1758 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
1759 lhs_,
1760 rhs_,
1761 a2,
1762 a3,
1763 t0,
1764 t1);
1765 }
1766 // Never falls through to here.
1767
1768 __ bind(&slow);
1769 // Prepare for call to builtin. Push object pointers, a0 (lhs) first,
1770 // a1 (rhs) second.
1771 __ Push(lhs_, rhs_);
1772 // Figure out which native to call and setup the arguments.
1773 Builtins::JavaScript native;
1774 if (cc_ == eq) {
1775 native = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
1776 } else {
1777 native = Builtins::COMPARE;
1778 int ncr; // NaN compare result.
1779 if (cc_ == lt || cc_ == le) {
1780 ncr = GREATER;
1781 } else {
1782 ASSERT(cc_ == gt || cc_ == ge); // Remaining cases.
1783 ncr = LESS;
1784 }
1785 __ li(a0, Operand(Smi::FromInt(ncr)));
1786 __ push(a0);
1787 }
1788
1789 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
1790 // tagged as a small integer.
1791 __ InvokeBuiltin(native, JUMP_FUNCTION);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001792}
1793
1794
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001795// The stub expects its argument in the tos_ register and returns its result in
1796// it, too: zero for false, and a non-zero value for true.
lrn@chromium.org7516f052011-03-30 08:52:27 +00001797void ToBooleanStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001798 // This stub uses FPU instructions.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001799 CpuFeatures::Scope scope(FPU);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001800
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001801 Label patch;
1802 const Register map = t5.is(tos_) ? t3 : t5;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001803
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001804 // undefined -> false.
1805 CheckOddball(masm, UNDEFINED, Heap::kUndefinedValueRootIndex, false);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001806
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001807 // Boolean -> its value.
1808 CheckOddball(masm, BOOLEAN, Heap::kFalseValueRootIndex, false);
1809 CheckOddball(masm, BOOLEAN, Heap::kTrueValueRootIndex, true);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001810
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001811 // 'null' -> false.
1812 CheckOddball(masm, NULL_TYPE, Heap::kNullValueRootIndex, false);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001813
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001814 if (types_.Contains(SMI)) {
1815 // Smis: 0 -> false, all other -> true
1816 __ And(at, tos_, kSmiTagMask);
1817 // tos_ contains the correct return value already
1818 __ Ret(eq, at, Operand(zero_reg));
1819 } else if (types_.NeedsMap()) {
1820 // If we need a map later and have a Smi -> patch.
1821 __ JumpIfSmi(tos_, &patch);
1822 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001823
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001824 if (types_.NeedsMap()) {
1825 __ lw(map, FieldMemOperand(tos_, HeapObject::kMapOffset));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001826
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001827 if (types_.CanBeUndetectable()) {
1828 __ lbu(at, FieldMemOperand(map, Map::kBitFieldOffset));
1829 __ And(at, at, Operand(1 << Map::kIsUndetectable));
1830 // Undetectable -> false.
1831 __ movn(tos_, zero_reg, at);
1832 __ Ret(ne, at, Operand(zero_reg));
1833 }
1834 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001835
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001836 if (types_.Contains(SPEC_OBJECT)) {
1837 // Spec object -> true.
1838 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
1839 // tos_ contains the correct non-zero return value already.
1840 __ Ret(ge, at, Operand(FIRST_SPEC_OBJECT_TYPE));
1841 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001842
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001843 if (types_.Contains(STRING)) {
1844 // String value -> false iff empty.
1845 __ lbu(at, FieldMemOperand(map, Map::kInstanceTypeOffset));
1846 Label skip;
1847 __ Branch(&skip, ge, at, Operand(FIRST_NONSTRING_TYPE));
1848 __ lw(tos_, FieldMemOperand(tos_, String::kLengthOffset));
1849 __ Ret(); // the string length is OK as the return value
1850 __ bind(&skip);
1851 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001852
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001853 if (types_.Contains(HEAP_NUMBER)) {
1854 // Heap number -> false iff +0, -0, or NaN.
1855 Label not_heap_number;
1856 __ LoadRoot(at, Heap::kHeapNumberMapRootIndex);
1857 __ Branch(&not_heap_number, ne, map, Operand(at));
1858 Label zero_or_nan, number;
1859 __ ldc1(f2, FieldMemOperand(tos_, HeapNumber::kValueOffset));
1860 __ BranchF(&number, &zero_or_nan, ne, f2, kDoubleRegZero);
1861 // "tos_" is a register, and contains a non zero value by default.
1862 // Hence we only need to overwrite "tos_" with zero to return false for
1863 // FP_ZERO or FP_NAN cases. Otherwise, by default it returns true.
1864 __ bind(&zero_or_nan);
1865 __ mov(tos_, zero_reg);
1866 __ bind(&number);
1867 __ Ret();
1868 __ bind(&not_heap_number);
1869 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001870
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001871 __ bind(&patch);
1872 GenerateTypeTransition(masm);
1873}
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001874
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001875
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001876void ToBooleanStub::CheckOddball(MacroAssembler* masm,
1877 Type type,
1878 Heap::RootListIndex value,
1879 bool result) {
1880 if (types_.Contains(type)) {
1881 // If we see an expected oddball, return its ToBoolean value tos_.
1882 __ LoadRoot(at, value);
1883 __ Subu(at, at, tos_); // This is a check for equality for the movz below.
1884 // The value of a root is never NULL, so we can avoid loading a non-null
1885 // value into tos_ when we want to return 'true'.
1886 if (!result) {
1887 __ movz(tos_, zero_reg, at);
1888 }
1889 __ Ret(eq, at, Operand(zero_reg));
1890 }
1891}
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001892
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001893
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00001894void ToBooleanStub::GenerateTypeTransition(MacroAssembler* masm) {
1895 __ Move(a3, tos_);
1896 __ li(a2, Operand(Smi::FromInt(tos_.code())));
1897 __ li(a1, Operand(Smi::FromInt(types_.ToByte())));
1898 __ Push(a3, a2, a1);
1899 // Patch the caller to an appropriate specialized stub and return the
1900 // operation result to the caller of the stub.
1901 __ TailCallExternalReference(
1902 ExternalReference(IC_Utility(IC::kToBoolean_Patch), masm->isolate()),
1903 3,
1904 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00001905}
1906
1907
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00001908void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
1909 // We don't allow a GC during a store buffer overflow so there is no need to
1910 // store the registers in any particular way, but we do have to store and
1911 // restore them.
1912 __ MultiPush(kJSCallerSaved | ra.bit());
1913 if (save_doubles_ == kSaveFPRegs) {
1914 CpuFeatures::Scope scope(FPU);
1915 __ MultiPushFPU(kCallerSavedFPU);
1916 }
1917 const int argument_count = 1;
1918 const int fp_argument_count = 0;
1919 const Register scratch = a1;
1920
1921 AllowExternalCallThatCantCauseGC scope(masm);
1922 __ PrepareCallCFunction(argument_count, fp_argument_count, scratch);
1923 __ li(a0, Operand(ExternalReference::isolate_address()));
1924 __ CallCFunction(
1925 ExternalReference::store_buffer_overflow_function(masm->isolate()),
1926 argument_count);
1927 if (save_doubles_ == kSaveFPRegs) {
1928 CpuFeatures::Scope scope(FPU);
1929 __ MultiPopFPU(kCallerSavedFPU);
1930 }
1931
1932 __ MultiPop(kJSCallerSaved | ra.bit());
1933 __ Ret();
1934}
1935
1936
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001937void UnaryOpStub::PrintName(StringStream* stream) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001938 const char* op_name = Token::Name(op_);
1939 const char* overwrite_name = NULL; // Make g++ happy.
1940 switch (mode_) {
1941 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
1942 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
1943 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00001944 stream->Add("UnaryOpStub_%s_%s_%s",
1945 op_name,
1946 overwrite_name,
1947 UnaryOpIC::GetName(operand_type_));
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001948}
1949
1950
1951// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001952void UnaryOpStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001953 switch (operand_type_) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00001954 case UnaryOpIC::UNINITIALIZED:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001955 GenerateTypeTransition(masm);
1956 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001957 case UnaryOpIC::SMI:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001958 GenerateSmiStub(masm);
1959 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001960 case UnaryOpIC::HEAP_NUMBER:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001961 GenerateHeapNumberStub(masm);
1962 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00001963 case UnaryOpIC::GENERIC:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001964 GenerateGenericStub(masm);
1965 break;
1966 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001967}
1968
1969
danno@chromium.org40cb8782011-05-25 07:58:50 +00001970void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001971 // Argument is in a0 and v0 at this point, so we can overwrite a0.
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001972 __ li(a2, Operand(Smi::FromInt(op_)));
1973 __ li(a1, Operand(Smi::FromInt(mode_)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001974 __ li(a0, Operand(Smi::FromInt(operand_type_)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001975 __ Push(v0, a2, a1, a0);
1976
1977 __ TailCallExternalReference(
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001978 ExternalReference(IC_Utility(IC::kUnaryOp_Patch), masm->isolate()), 4, 1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001979}
1980
1981
1982// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +00001983void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001984 switch (op_) {
1985 case Token::SUB:
1986 GenerateSmiStubSub(masm);
1987 break;
1988 case Token::BIT_NOT:
1989 GenerateSmiStubBitNot(masm);
1990 break;
1991 default:
1992 UNREACHABLE();
1993 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00001994}
1995
1996
danno@chromium.org40cb8782011-05-25 07:58:50 +00001997void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00001998 Label non_smi, slow;
1999 GenerateSmiCodeSub(masm, &non_smi, &slow);
2000 __ bind(&non_smi);
2001 __ bind(&slow);
2002 GenerateTypeTransition(masm);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002003}
2004
2005
danno@chromium.org40cb8782011-05-25 07:58:50 +00002006void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002007 Label non_smi;
2008 GenerateSmiCodeBitNot(masm, &non_smi);
2009 __ bind(&non_smi);
2010 GenerateTypeTransition(masm);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002011}
2012
2013
danno@chromium.org40cb8782011-05-25 07:58:50 +00002014void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
2015 Label* non_smi,
2016 Label* slow) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002017 __ JumpIfNotSmi(a0, non_smi);
2018
2019 // The result of negating zero or the smallest negative smi is not a smi.
2020 __ And(t0, a0, ~0x80000000);
2021 __ Branch(slow, eq, t0, Operand(zero_reg));
2022
2023 // Return '0 - value'.
2024 __ Subu(v0, zero_reg, a0);
2025 __ Ret();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002026}
2027
2028
danno@chromium.org40cb8782011-05-25 07:58:50 +00002029void UnaryOpStub::GenerateSmiCodeBitNot(MacroAssembler* masm,
2030 Label* non_smi) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002031 __ JumpIfNotSmi(a0, non_smi);
2032
2033 // Flip bits and revert inverted smi-tag.
2034 __ Neg(v0, a0);
2035 __ And(v0, v0, ~kSmiTagMask);
2036 __ Ret();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002037}
2038
2039
2040// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002041void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002042 switch (op_) {
2043 case Token::SUB:
2044 GenerateHeapNumberStubSub(masm);
2045 break;
2046 case Token::BIT_NOT:
2047 GenerateHeapNumberStubBitNot(masm);
2048 break;
2049 default:
2050 UNREACHABLE();
2051 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002052}
2053
2054
danno@chromium.org40cb8782011-05-25 07:58:50 +00002055void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
2056 Label non_smi, slow, call_builtin;
2057 GenerateSmiCodeSub(masm, &non_smi, &call_builtin);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002058 __ bind(&non_smi);
2059 GenerateHeapNumberCodeSub(masm, &slow);
2060 __ bind(&slow);
2061 GenerateTypeTransition(masm);
danno@chromium.org40cb8782011-05-25 07:58:50 +00002062 __ bind(&call_builtin);
2063 GenerateGenericCodeFallback(masm);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002064}
2065
2066
danno@chromium.org40cb8782011-05-25 07:58:50 +00002067void UnaryOpStub::GenerateHeapNumberStubBitNot(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002068 Label non_smi, slow;
2069 GenerateSmiCodeBitNot(masm, &non_smi);
2070 __ bind(&non_smi);
2071 GenerateHeapNumberCodeBitNot(masm, &slow);
2072 __ bind(&slow);
2073 GenerateTypeTransition(masm);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002074}
2075
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00002076
danno@chromium.org40cb8782011-05-25 07:58:50 +00002077void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
2078 Label* slow) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002079 EmitCheckForHeapNumber(masm, a0, a1, t2, slow);
2080 // a0 is a heap number. Get a new heap number in a1.
2081 if (mode_ == UNARY_OVERWRITE) {
2082 __ lw(a2, FieldMemOperand(a0, HeapNumber::kExponentOffset));
2083 __ Xor(a2, a2, Operand(HeapNumber::kSignMask)); // Flip sign.
2084 __ sw(a2, FieldMemOperand(a0, HeapNumber::kExponentOffset));
2085 } else {
2086 Label slow_allocate_heapnumber, heapnumber_allocated;
2087 __ AllocateHeapNumber(a1, a2, a3, t2, &slow_allocate_heapnumber);
2088 __ jmp(&heapnumber_allocated);
2089
2090 __ bind(&slow_allocate_heapnumber);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002091 {
2092 FrameScope scope(masm, StackFrame::INTERNAL);
2093 __ push(a0);
2094 __ CallRuntime(Runtime::kNumberAlloc, 0);
2095 __ mov(a1, v0);
2096 __ pop(a0);
2097 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002098
2099 __ bind(&heapnumber_allocated);
2100 __ lw(a3, FieldMemOperand(a0, HeapNumber::kMantissaOffset));
2101 __ lw(a2, FieldMemOperand(a0, HeapNumber::kExponentOffset));
2102 __ sw(a3, FieldMemOperand(a1, HeapNumber::kMantissaOffset));
2103 __ Xor(a2, a2, Operand(HeapNumber::kSignMask)); // Flip sign.
2104 __ sw(a2, FieldMemOperand(a1, HeapNumber::kExponentOffset));
2105 __ mov(v0, a1);
2106 }
2107 __ Ret();
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002108}
2109
2110
danno@chromium.org40cb8782011-05-25 07:58:50 +00002111void UnaryOpStub::GenerateHeapNumberCodeBitNot(
2112 MacroAssembler* masm,
2113 Label* slow) {
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00002114 Label impossible;
2115
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002116 EmitCheckForHeapNumber(masm, a0, a1, t2, slow);
2117 // Convert the heap number in a0 to an untagged integer in a1.
2118 __ ConvertToInt32(a0, a1, a2, a3, f0, slow);
2119
2120 // Do the bitwise operation and check if the result fits in a smi.
2121 Label try_float;
2122 __ Neg(a1, a1);
2123 __ Addu(a2, a1, Operand(0x40000000));
2124 __ Branch(&try_float, lt, a2, Operand(zero_reg));
2125
2126 // Tag the result as a smi and we're done.
2127 __ SmiTag(v0, a1);
2128 __ Ret();
2129
2130 // Try to store the result in a heap number.
2131 __ bind(&try_float);
2132 if (mode_ == UNARY_NO_OVERWRITE) {
2133 Label slow_allocate_heapnumber, heapnumber_allocated;
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00002134 // Allocate a new heap number without zapping v0, which we need if it fails.
2135 __ AllocateHeapNumber(a2, a3, t0, t2, &slow_allocate_heapnumber);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002136 __ jmp(&heapnumber_allocated);
2137
2138 __ bind(&slow_allocate_heapnumber);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002139 {
2140 FrameScope scope(masm, StackFrame::INTERNAL);
2141 __ push(v0); // Push the heap number, not the untagged int32.
2142 __ CallRuntime(Runtime::kNumberAlloc, 0);
2143 __ mov(a2, v0); // Move the new heap number into a2.
2144 // Get the heap number into v0, now that the new heap number is in a2.
2145 __ pop(v0);
2146 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002147
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00002148 // Convert the heap number in v0 to an untagged integer in a1.
2149 // This can't go slow-case because it's the same number we already
2150 // converted once again.
2151 __ ConvertToInt32(v0, a1, a3, t0, f0, &impossible);
2152 // Negate the result.
2153 __ Xor(a1, a1, -1);
2154
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002155 __ bind(&heapnumber_allocated);
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00002156 __ mov(v0, a2); // Move newly allocated heap number to v0.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002157 }
2158
2159 if (CpuFeatures::IsSupported(FPU)) {
2160 // Convert the int32 in a1 to the heap number in v0. a2 is corrupted.
2161 CpuFeatures::Scope scope(FPU);
2162 __ mtc1(a1, f0);
2163 __ cvt_d_w(f0, f0);
2164 __ sdc1(f0, FieldMemOperand(v0, HeapNumber::kValueOffset));
2165 __ Ret();
2166 } else {
2167 // WriteInt32ToHeapNumberStub does not trigger GC, so we do not
2168 // have to set up a frame.
2169 WriteInt32ToHeapNumberStub stub(a1, v0, a2, a3);
2170 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
2171 }
ricow@chromium.orgc54d3652011-05-30 09:20:16 +00002172
2173 __ bind(&impossible);
2174 if (FLAG_debug_code) {
2175 __ stop("Incorrect assumption in bit-not stub");
2176 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002177}
2178
2179
2180// TODO(svenpanne): Use virtual functions instead of switch.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002181void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002182 switch (op_) {
2183 case Token::SUB:
2184 GenerateGenericStubSub(masm);
2185 break;
2186 case Token::BIT_NOT:
2187 GenerateGenericStubBitNot(masm);
2188 break;
2189 default:
2190 UNREACHABLE();
2191 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002192}
2193
2194
danno@chromium.org40cb8782011-05-25 07:58:50 +00002195void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002196 Label non_smi, slow;
2197 GenerateSmiCodeSub(masm, &non_smi, &slow);
2198 __ bind(&non_smi);
2199 GenerateHeapNumberCodeSub(masm, &slow);
2200 __ bind(&slow);
2201 GenerateGenericCodeFallback(masm);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002202}
2203
2204
danno@chromium.org40cb8782011-05-25 07:58:50 +00002205void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002206 Label non_smi, slow;
2207 GenerateSmiCodeBitNot(masm, &non_smi);
2208 __ bind(&non_smi);
2209 GenerateHeapNumberCodeBitNot(masm, &slow);
2210 __ bind(&slow);
2211 GenerateGenericCodeFallback(masm);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002212}
2213
2214
danno@chromium.org40cb8782011-05-25 07:58:50 +00002215void UnaryOpStub::GenerateGenericCodeFallback(
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002216 MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002217 // Handle the slow case by jumping to the JavaScript builtin.
2218 __ push(a0);
2219 switch (op_) {
2220 case Token::SUB:
2221 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
2222 break;
2223 case Token::BIT_NOT:
2224 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
2225 break;
2226 default:
2227 UNREACHABLE();
2228 }
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002229}
2230
2231
danno@chromium.org40cb8782011-05-25 07:58:50 +00002232void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002233 Label get_result;
2234
2235 __ Push(a1, a0);
2236
2237 __ li(a2, Operand(Smi::FromInt(MinorKey())));
2238 __ li(a1, Operand(Smi::FromInt(op_)));
2239 __ li(a0, Operand(Smi::FromInt(operands_type_)));
2240 __ Push(a2, a1, a0);
2241
2242 __ TailCallExternalReference(
danno@chromium.org40cb8782011-05-25 07:58:50 +00002243 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002244 masm->isolate()),
2245 5,
2246 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002247}
2248
2249
danno@chromium.org40cb8782011-05-25 07:58:50 +00002250void BinaryOpStub::GenerateTypeTransitionWithSavedArgs(
lrn@chromium.org7516f052011-03-30 08:52:27 +00002251 MacroAssembler* masm) {
2252 UNIMPLEMENTED();
2253}
2254
2255
danno@chromium.org40cb8782011-05-25 07:58:50 +00002256void BinaryOpStub::Generate(MacroAssembler* masm) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00002257 // Explicitly allow generation of nested stubs. It is safe here because
2258 // generation code does not use any raw pointers.
2259 AllowStubCallsScope allow_stub_calls(masm, true);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002260 switch (operands_type_) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00002261 case BinaryOpIC::UNINITIALIZED:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002262 GenerateTypeTransition(masm);
2263 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002264 case BinaryOpIC::SMI:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002265 GenerateSmiStub(masm);
2266 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002267 case BinaryOpIC::INT32:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002268 GenerateInt32Stub(masm);
2269 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002270 case BinaryOpIC::HEAP_NUMBER:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002271 GenerateHeapNumberStub(masm);
2272 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002273 case BinaryOpIC::ODDBALL:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002274 GenerateOddballStub(masm);
2275 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002276 case BinaryOpIC::BOTH_STRING:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002277 GenerateBothStringStub(masm);
2278 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002279 case BinaryOpIC::STRING:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002280 GenerateStringStub(masm);
2281 break;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002282 case BinaryOpIC::GENERIC:
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002283 GenerateGeneric(masm);
2284 break;
2285 default:
2286 UNREACHABLE();
2287 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002288}
2289
2290
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002291void BinaryOpStub::PrintName(StringStream* stream) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002292 const char* op_name = Token::Name(op_);
2293 const char* overwrite_name;
2294 switch (mode_) {
2295 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
2296 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
2297 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
2298 default: overwrite_name = "UnknownOverwrite"; break;
2299 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00002300 stream->Add("BinaryOpStub_%s_%s_%s",
2301 op_name,
2302 overwrite_name,
2303 BinaryOpIC::GetName(operands_type_));
lrn@chromium.org7516f052011-03-30 08:52:27 +00002304}
2305
2306
2307
danno@chromium.org40cb8782011-05-25 07:58:50 +00002308void BinaryOpStub::GenerateSmiSmiOperation(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002309 Register left = a1;
2310 Register right = a0;
2311
2312 Register scratch1 = t0;
2313 Register scratch2 = t1;
2314
2315 ASSERT(right.is(a0));
2316 STATIC_ASSERT(kSmiTag == 0);
2317
2318 Label not_smi_result;
2319 switch (op_) {
2320 case Token::ADD:
2321 __ AdduAndCheckForOverflow(v0, left, right, scratch1);
2322 __ RetOnNoOverflow(scratch1);
2323 // No need to revert anything - right and left are intact.
2324 break;
2325 case Token::SUB:
2326 __ SubuAndCheckForOverflow(v0, left, right, scratch1);
2327 __ RetOnNoOverflow(scratch1);
2328 // No need to revert anything - right and left are intact.
2329 break;
2330 case Token::MUL: {
2331 // Remove tag from one of the operands. This way the multiplication result
2332 // will be a smi if it fits the smi range.
2333 __ SmiUntag(scratch1, right);
2334 // Do multiplication.
2335 // lo = lower 32 bits of scratch1 * left.
2336 // hi = higher 32 bits of scratch1 * left.
2337 __ Mult(left, scratch1);
2338 // Check for overflowing the smi range - no overflow if higher 33 bits of
2339 // the result are identical.
2340 __ mflo(scratch1);
2341 __ mfhi(scratch2);
2342 __ sra(scratch1, scratch1, 31);
2343 __ Branch(&not_smi_result, ne, scratch1, Operand(scratch2));
2344 // Go slow on zero result to handle -0.
2345 __ mflo(v0);
2346 __ Ret(ne, v0, Operand(zero_reg));
2347 // We need -0 if we were multiplying a negative number with 0 to get 0.
2348 // We know one of them was zero.
2349 __ Addu(scratch2, right, left);
2350 Label skip;
2351 // ARM uses the 'pl' condition, which is 'ge'.
2352 // Negating it results in 'lt'.
2353 __ Branch(&skip, lt, scratch2, Operand(zero_reg));
2354 ASSERT(Smi::FromInt(0) == 0);
2355 __ mov(v0, zero_reg);
2356 __ Ret(); // Return smi 0 if the non-zero one was positive.
2357 __ bind(&skip);
2358 // We fall through here if we multiplied a negative number with 0, because
2359 // that would mean we should produce -0.
2360 }
2361 break;
2362 case Token::DIV: {
2363 Label done;
2364 __ SmiUntag(scratch2, right);
2365 __ SmiUntag(scratch1, left);
2366 __ Div(scratch1, scratch2);
2367 // A minor optimization: div may be calculated asynchronously, so we check
2368 // for division by zero before getting the result.
2369 __ Branch(&not_smi_result, eq, scratch2, Operand(zero_reg));
2370 // If the result is 0, we need to make sure the dividsor (right) is
2371 // positive, otherwise it is a -0 case.
2372 // Quotient is in 'lo', remainder is in 'hi'.
2373 // Check for no remainder first.
2374 __ mfhi(scratch1);
2375 __ Branch(&not_smi_result, ne, scratch1, Operand(zero_reg));
2376 __ mflo(scratch1);
2377 __ Branch(&done, ne, scratch1, Operand(zero_reg));
2378 __ Branch(&not_smi_result, lt, scratch2, Operand(zero_reg));
2379 __ bind(&done);
2380 // Check that the signed result fits in a Smi.
2381 __ Addu(scratch2, scratch1, Operand(0x40000000));
2382 __ Branch(&not_smi_result, lt, scratch2, Operand(zero_reg));
2383 __ SmiTag(v0, scratch1);
2384 __ Ret();
2385 }
2386 break;
2387 case Token::MOD: {
2388 Label done;
2389 __ SmiUntag(scratch2, right);
2390 __ SmiUntag(scratch1, left);
2391 __ Div(scratch1, scratch2);
2392 // A minor optimization: div may be calculated asynchronously, so we check
2393 // for division by 0 before calling mfhi.
2394 // Check for zero on the right hand side.
2395 __ Branch(&not_smi_result, eq, scratch2, Operand(zero_reg));
2396 // If the result is 0, we need to make sure the dividend (left) is
2397 // positive (or 0), otherwise it is a -0 case.
2398 // Remainder is in 'hi'.
2399 __ mfhi(scratch2);
2400 __ Branch(&done, ne, scratch2, Operand(zero_reg));
2401 __ Branch(&not_smi_result, lt, scratch1, Operand(zero_reg));
2402 __ bind(&done);
2403 // Check that the signed result fits in a Smi.
2404 __ Addu(scratch1, scratch2, Operand(0x40000000));
2405 __ Branch(&not_smi_result, lt, scratch1, Operand(zero_reg));
2406 __ SmiTag(v0, scratch2);
2407 __ Ret();
2408 }
2409 break;
2410 case Token::BIT_OR:
2411 __ Or(v0, left, Operand(right));
2412 __ Ret();
2413 break;
2414 case Token::BIT_AND:
2415 __ And(v0, left, Operand(right));
2416 __ Ret();
2417 break;
2418 case Token::BIT_XOR:
2419 __ Xor(v0, left, Operand(right));
2420 __ Ret();
2421 break;
2422 case Token::SAR:
2423 // Remove tags from right operand.
2424 __ GetLeastBitsFromSmi(scratch1, right, 5);
2425 __ srav(scratch1, left, scratch1);
2426 // Smi tag result.
2427 __ And(v0, scratch1, Operand(~kSmiTagMask));
2428 __ Ret();
2429 break;
2430 case Token::SHR:
2431 // Remove tags from operands. We can't do this on a 31 bit number
2432 // because then the 0s get shifted into bit 30 instead of bit 31.
2433 __ SmiUntag(scratch1, left);
2434 __ GetLeastBitsFromSmi(scratch2, right, 5);
2435 __ srlv(v0, scratch1, scratch2);
2436 // Unsigned shift is not allowed to produce a negative number, so
2437 // check the sign bit and the sign bit after Smi tagging.
2438 __ And(scratch1, v0, Operand(0xc0000000));
2439 __ Branch(&not_smi_result, ne, scratch1, Operand(zero_reg));
2440 // Smi tag result.
2441 __ SmiTag(v0);
2442 __ Ret();
2443 break;
2444 case Token::SHL:
2445 // Remove tags from operands.
2446 __ SmiUntag(scratch1, left);
2447 __ GetLeastBitsFromSmi(scratch2, right, 5);
2448 __ sllv(scratch1, scratch1, scratch2);
2449 // Check that the signed result fits in a Smi.
2450 __ Addu(scratch2, scratch1, Operand(0x40000000));
2451 __ Branch(&not_smi_result, lt, scratch2, Operand(zero_reg));
2452 __ SmiTag(v0, scratch1);
2453 __ Ret();
2454 break;
2455 default:
2456 UNREACHABLE();
2457 }
2458 __ bind(&not_smi_result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002459}
2460
2461
danno@chromium.org40cb8782011-05-25 07:58:50 +00002462void BinaryOpStub::GenerateFPOperation(MacroAssembler* masm,
2463 bool smi_operands,
2464 Label* not_numbers,
2465 Label* gc_required) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002466 Register left = a1;
2467 Register right = a0;
2468 Register scratch1 = t3;
2469 Register scratch2 = t5;
2470 Register scratch3 = t0;
2471
2472 ASSERT(smi_operands || (not_numbers != NULL));
2473 if (smi_operands && FLAG_debug_code) {
2474 __ AbortIfNotSmi(left);
2475 __ AbortIfNotSmi(right);
2476 }
2477
2478 Register heap_number_map = t2;
2479 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2480
2481 switch (op_) {
2482 case Token::ADD:
2483 case Token::SUB:
2484 case Token::MUL:
2485 case Token::DIV:
2486 case Token::MOD: {
2487 // Load left and right operands into f12 and f14 or a0/a1 and a2/a3
2488 // depending on whether FPU is available or not.
2489 FloatingPointHelper::Destination destination =
2490 CpuFeatures::IsSupported(FPU) &&
2491 op_ != Token::MOD ?
2492 FloatingPointHelper::kFPURegisters :
2493 FloatingPointHelper::kCoreRegisters;
2494
2495 // Allocate new heap number for result.
2496 Register result = s0;
2497 GenerateHeapResultAllocation(
2498 masm, result, heap_number_map, scratch1, scratch2, gc_required);
2499
2500 // Load the operands.
2501 if (smi_operands) {
2502 FloatingPointHelper::LoadSmis(masm, destination, scratch1, scratch2);
2503 } else {
2504 FloatingPointHelper::LoadOperands(masm,
2505 destination,
2506 heap_number_map,
2507 scratch1,
2508 scratch2,
2509 not_numbers);
2510 }
2511
2512 // Calculate the result.
2513 if (destination == FloatingPointHelper::kFPURegisters) {
2514 // Using FPU registers:
2515 // f12: Left value.
2516 // f14: Right value.
2517 CpuFeatures::Scope scope(FPU);
2518 switch (op_) {
2519 case Token::ADD:
2520 __ add_d(f10, f12, f14);
2521 break;
2522 case Token::SUB:
2523 __ sub_d(f10, f12, f14);
2524 break;
2525 case Token::MUL:
2526 __ mul_d(f10, f12, f14);
2527 break;
2528 case Token::DIV:
2529 __ div_d(f10, f12, f14);
2530 break;
2531 default:
2532 UNREACHABLE();
2533 }
2534
2535 // ARM uses a workaround here because of the unaligned HeapNumber
2536 // kValueOffset. On MIPS this workaround is built into sdc1 so
2537 // there's no point in generating even more instructions.
2538 __ sdc1(f10, FieldMemOperand(result, HeapNumber::kValueOffset));
2539 __ mov(v0, result);
2540 __ Ret();
2541 } else {
2542 // Call the C function to handle the double operation.
2543 FloatingPointHelper::CallCCodeForDoubleOperation(masm,
2544 op_,
2545 result,
2546 scratch1);
2547 if (FLAG_debug_code) {
2548 __ stop("Unreachable code.");
2549 }
2550 }
2551 break;
2552 }
2553 case Token::BIT_OR:
2554 case Token::BIT_XOR:
2555 case Token::BIT_AND:
2556 case Token::SAR:
2557 case Token::SHR:
2558 case Token::SHL: {
2559 if (smi_operands) {
2560 __ SmiUntag(a3, left);
2561 __ SmiUntag(a2, right);
2562 } else {
2563 // Convert operands to 32-bit integers. Right in a2 and left in a3.
2564 FloatingPointHelper::ConvertNumberToInt32(masm,
2565 left,
2566 a3,
2567 heap_number_map,
2568 scratch1,
2569 scratch2,
2570 scratch3,
2571 f0,
2572 not_numbers);
2573 FloatingPointHelper::ConvertNumberToInt32(masm,
2574 right,
2575 a2,
2576 heap_number_map,
2577 scratch1,
2578 scratch2,
2579 scratch3,
2580 f0,
2581 not_numbers);
2582 }
2583 Label result_not_a_smi;
2584 switch (op_) {
2585 case Token::BIT_OR:
2586 __ Or(a2, a3, Operand(a2));
2587 break;
2588 case Token::BIT_XOR:
2589 __ Xor(a2, a3, Operand(a2));
2590 break;
2591 case Token::BIT_AND:
2592 __ And(a2, a3, Operand(a2));
2593 break;
2594 case Token::SAR:
2595 // Use only the 5 least significant bits of the shift count.
2596 __ GetLeastBitsFromInt32(a2, a2, 5);
2597 __ srav(a2, a3, a2);
2598 break;
2599 case Token::SHR:
2600 // Use only the 5 least significant bits of the shift count.
2601 __ GetLeastBitsFromInt32(a2, a2, 5);
2602 __ srlv(a2, a3, a2);
2603 // SHR is special because it is required to produce a positive answer.
2604 // The code below for writing into heap numbers isn't capable of
2605 // writing the register as an unsigned int so we go to slow case if we
2606 // hit this case.
2607 if (CpuFeatures::IsSupported(FPU)) {
2608 __ Branch(&result_not_a_smi, lt, a2, Operand(zero_reg));
2609 } else {
2610 __ Branch(not_numbers, lt, a2, Operand(zero_reg));
2611 }
2612 break;
2613 case Token::SHL:
2614 // Use only the 5 least significant bits of the shift count.
2615 __ GetLeastBitsFromInt32(a2, a2, 5);
2616 __ sllv(a2, a3, a2);
2617 break;
2618 default:
2619 UNREACHABLE();
2620 }
2621 // Check that the *signed* result fits in a smi.
2622 __ Addu(a3, a2, Operand(0x40000000));
2623 __ Branch(&result_not_a_smi, lt, a3, Operand(zero_reg));
2624 __ SmiTag(v0, a2);
2625 __ Ret();
2626
2627 // Allocate new heap number for result.
2628 __ bind(&result_not_a_smi);
2629 Register result = t1;
2630 if (smi_operands) {
2631 __ AllocateHeapNumber(
2632 result, scratch1, scratch2, heap_number_map, gc_required);
2633 } else {
2634 GenerateHeapResultAllocation(
2635 masm, result, heap_number_map, scratch1, scratch2, gc_required);
2636 }
2637
2638 // a2: Answer as signed int32.
2639 // t1: Heap number to write answer into.
2640
2641 // Nothing can go wrong now, so move the heap number to v0, which is the
2642 // result.
2643 __ mov(v0, t1);
2644
2645 if (CpuFeatures::IsSupported(FPU)) {
2646 // Convert the int32 in a2 to the heap number in a0. As
2647 // mentioned above SHR needs to always produce a positive result.
2648 CpuFeatures::Scope scope(FPU);
2649 __ mtc1(a2, f0);
2650 if (op_ == Token::SHR) {
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00002651 __ Cvt_d_uw(f0, f0, f22);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002652 } else {
2653 __ cvt_d_w(f0, f0);
2654 }
2655 // ARM uses a workaround here because of the unaligned HeapNumber
2656 // kValueOffset. On MIPS this workaround is built into sdc1 so
2657 // there's no point in generating even more instructions.
2658 __ sdc1(f0, FieldMemOperand(v0, HeapNumber::kValueOffset));
2659 __ Ret();
2660 } else {
2661 // Tail call that writes the int32 in a2 to the heap number in v0, using
2662 // a3 and a0 as scratch. v0 is preserved and returned.
2663 WriteInt32ToHeapNumberStub stub(a2, v0, a3, a0);
2664 __ TailCallStub(&stub);
2665 }
2666 break;
2667 }
2668 default:
2669 UNREACHABLE();
2670 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00002671}
2672
2673
2674// Generate the smi code. If the operation on smis are successful this return is
2675// generated. If the result is not a smi and heap number allocation is not
2676// requested the code falls through. If number allocation is requested but a
2677// heap number cannot be allocated the code jumps to the lable gc_required.
danno@chromium.org40cb8782011-05-25 07:58:50 +00002678void BinaryOpStub::GenerateSmiCode(
2679 MacroAssembler* masm,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00002680 Label* use_runtime,
lrn@chromium.org7516f052011-03-30 08:52:27 +00002681 Label* gc_required,
2682 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002683 Label not_smis;
2684
2685 Register left = a1;
2686 Register right = a0;
2687 Register scratch1 = t3;
2688 Register scratch2 = t5;
2689
2690 // Perform combined smi check on both operands.
2691 __ Or(scratch1, left, Operand(right));
2692 STATIC_ASSERT(kSmiTag == 0);
2693 __ JumpIfNotSmi(scratch1, &not_smis);
2694
2695 // If the smi-smi operation results in a smi return is generated.
2696 GenerateSmiSmiOperation(masm);
2697
2698 // If heap number results are possible generate the result in an allocated
2699 // heap number.
2700 if (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) {
2701 GenerateFPOperation(masm, true, use_runtime, gc_required);
2702 }
2703 __ bind(&not_smis);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002704}
2705
2706
danno@chromium.org40cb8782011-05-25 07:58:50 +00002707void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002708 Label not_smis, call_runtime;
2709
danno@chromium.org40cb8782011-05-25 07:58:50 +00002710 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
2711 result_type_ == BinaryOpIC::SMI) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002712 // Only allow smi results.
2713 GenerateSmiCode(masm, &call_runtime, NULL, NO_HEAPNUMBER_RESULTS);
2714 } else {
2715 // Allow heap number result and don't make a transition if a heap number
2716 // cannot be allocated.
2717 GenerateSmiCode(masm,
2718 &call_runtime,
2719 &call_runtime,
2720 ALLOW_HEAPNUMBER_RESULTS);
2721 }
2722
2723 // Code falls through if the result is not returned as either a smi or heap
2724 // number.
2725 GenerateTypeTransition(masm);
2726
2727 __ bind(&call_runtime);
2728 GenerateCallRuntime(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002729}
2730
2731
danno@chromium.org40cb8782011-05-25 07:58:50 +00002732void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
2733 ASSERT(operands_type_ == BinaryOpIC::STRING);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002734 // Try to add arguments as strings, otherwise, transition to the generic
danno@chromium.org40cb8782011-05-25 07:58:50 +00002735 // BinaryOpIC type.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002736 GenerateAddStrings(masm);
2737 GenerateTypeTransition(masm);
2738}
2739
2740
danno@chromium.org40cb8782011-05-25 07:58:50 +00002741void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002742 Label call_runtime;
danno@chromium.org40cb8782011-05-25 07:58:50 +00002743 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002744 ASSERT(op_ == Token::ADD);
2745 // If both arguments are strings, call the string add stub.
2746 // Otherwise, do a transition.
2747
2748 // Registers containing left and right operands respectively.
2749 Register left = a1;
2750 Register right = a0;
2751
2752 // Test if left operand is a string.
2753 __ JumpIfSmi(left, &call_runtime);
2754 __ GetObjectType(left, a2, a2);
2755 __ Branch(&call_runtime, ge, a2, Operand(FIRST_NONSTRING_TYPE));
2756
2757 // Test if right operand is a string.
2758 __ JumpIfSmi(right, &call_runtime);
2759 __ GetObjectType(right, a2, a2);
2760 __ Branch(&call_runtime, ge, a2, Operand(FIRST_NONSTRING_TYPE));
2761
2762 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
2763 GenerateRegisterArgsPush(masm);
2764 __ TailCallStub(&string_add_stub);
2765
2766 __ bind(&call_runtime);
2767 GenerateTypeTransition(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00002768}
2769
2770
danno@chromium.org40cb8782011-05-25 07:58:50 +00002771void BinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
2772 ASSERT(operands_type_ == BinaryOpIC::INT32);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002773
2774 Register left = a1;
2775 Register right = a0;
2776 Register scratch1 = t3;
2777 Register scratch2 = t5;
2778 FPURegister double_scratch = f0;
2779 FPURegister single_scratch = f6;
2780
2781 Register heap_number_result = no_reg;
2782 Register heap_number_map = t2;
2783 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
2784
2785 Label call_runtime;
2786 // Labels for type transition, used for wrong input or output types.
2787 // Both label are currently actually bound to the same position. We use two
2788 // different label to differentiate the cause leading to type transition.
2789 Label transition;
2790
2791 // Smi-smi fast case.
2792 Label skip;
2793 __ Or(scratch1, left, right);
2794 __ JumpIfNotSmi(scratch1, &skip);
2795 GenerateSmiSmiOperation(masm);
2796 // Fall through if the result is not a smi.
2797 __ bind(&skip);
2798
2799 switch (op_) {
2800 case Token::ADD:
2801 case Token::SUB:
2802 case Token::MUL:
2803 case Token::DIV:
2804 case Token::MOD: {
whesse@chromium.org7b260152011-06-20 15:33:18 +00002805 // Load both operands and check that they are 32-bit integer.
2806 // Jump to type transition if they are not. The registers a0 and a1 (right
2807 // and left) are preserved for the runtime call.
2808 FloatingPointHelper::Destination destination =
2809 (CpuFeatures::IsSupported(FPU) && op_ != Token::MOD)
2810 ? FloatingPointHelper::kFPURegisters
2811 : FloatingPointHelper::kCoreRegisters;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002812
whesse@chromium.org7b260152011-06-20 15:33:18 +00002813 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2814 right,
2815 destination,
2816 f14,
2817 a2,
2818 a3,
2819 heap_number_map,
2820 scratch1,
2821 scratch2,
2822 f2,
2823 &transition);
2824 FloatingPointHelper::LoadNumberAsInt32Double(masm,
2825 left,
2826 destination,
2827 f12,
2828 t0,
2829 t1,
2830 heap_number_map,
2831 scratch1,
2832 scratch2,
2833 f2,
2834 &transition);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002835
2836 if (destination == FloatingPointHelper::kFPURegisters) {
2837 CpuFeatures::Scope scope(FPU);
2838 Label return_heap_number;
2839 switch (op_) {
2840 case Token::ADD:
2841 __ add_d(f10, f12, f14);
2842 break;
2843 case Token::SUB:
2844 __ sub_d(f10, f12, f14);
2845 break;
2846 case Token::MUL:
2847 __ mul_d(f10, f12, f14);
2848 break;
2849 case Token::DIV:
2850 __ div_d(f10, f12, f14);
2851 break;
2852 default:
2853 UNREACHABLE();
2854 }
2855
2856 if (op_ != Token::DIV) {
2857 // These operations produce an integer result.
2858 // Try to return a smi if we can.
2859 // Otherwise return a heap number if allowed, or jump to type
2860 // transition.
2861
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002862 Register except_flag = scratch2;
2863 __ EmitFPUTruncate(kRoundToZero,
2864 single_scratch,
2865 f10,
2866 scratch1,
2867 except_flag);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002868
danno@chromium.org40cb8782011-05-25 07:58:50 +00002869 if (result_type_ <= BinaryOpIC::INT32) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00002870 // If except_flag != 0, result does not fit in a 32-bit integer.
2871 __ Branch(&transition, ne, except_flag, Operand(zero_reg));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002872 }
2873
2874 // Check if the result fits in a smi.
2875 __ mfc1(scratch1, single_scratch);
2876 __ Addu(scratch2, scratch1, Operand(0x40000000));
2877 // If not try to return a heap number.
2878 __ Branch(&return_heap_number, lt, scratch2, Operand(zero_reg));
2879 // Check for minus zero. Return heap number for minus zero.
2880 Label not_zero;
2881 __ Branch(&not_zero, ne, scratch1, Operand(zero_reg));
2882 __ mfc1(scratch2, f11);
2883 __ And(scratch2, scratch2, HeapNumber::kSignMask);
2884 __ Branch(&return_heap_number, ne, scratch2, Operand(zero_reg));
2885 __ bind(&not_zero);
2886
2887 // Tag the result and return.
2888 __ SmiTag(v0, scratch1);
2889 __ Ret();
2890 } else {
2891 // DIV just falls through to allocating a heap number.
2892 }
2893
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00002894 __ bind(&return_heap_number);
2895 // Return a heap number, or fall through to type transition or runtime
2896 // call if we can't.
whesse@chromium.org7b260152011-06-20 15:33:18 +00002897 if (result_type_ >= ((op_ == Token::DIV) ? BinaryOpIC::HEAP_NUMBER
2898 : BinaryOpIC::INT32)) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00002899 // We are using FPU registers so s0 is available.
2900 heap_number_result = s0;
2901 GenerateHeapResultAllocation(masm,
2902 heap_number_result,
2903 heap_number_map,
2904 scratch1,
2905 scratch2,
2906 &call_runtime);
2907 __ mov(v0, heap_number_result);
2908 __ sdc1(f10, FieldMemOperand(v0, HeapNumber::kValueOffset));
2909 __ Ret();
2910 }
2911
2912 // A DIV operation expecting an integer result falls through
2913 // to type transition.
2914
2915 } else {
2916 // We preserved a0 and a1 to be able to call runtime.
2917 // Save the left value on the stack.
2918 __ Push(t1, t0);
2919
2920 Label pop_and_call_runtime;
2921
2922 // Allocate a heap number to store the result.
2923 heap_number_result = s0;
2924 GenerateHeapResultAllocation(masm,
2925 heap_number_result,
2926 heap_number_map,
2927 scratch1,
2928 scratch2,
2929 &pop_and_call_runtime);
2930
2931 // Load the left value from the value saved on the stack.
2932 __ Pop(a1, a0);
2933
2934 // Call the C function to handle the double operation.
2935 FloatingPointHelper::CallCCodeForDoubleOperation(
2936 masm, op_, heap_number_result, scratch1);
2937 if (FLAG_debug_code) {
2938 __ stop("Unreachable code.");
2939 }
2940
2941 __ bind(&pop_and_call_runtime);
2942 __ Drop(2);
2943 __ Branch(&call_runtime);
2944 }
2945
2946 break;
2947 }
2948
2949 case Token::BIT_OR:
2950 case Token::BIT_XOR:
2951 case Token::BIT_AND:
2952 case Token::SAR:
2953 case Token::SHR:
2954 case Token::SHL: {
2955 Label return_heap_number;
2956 Register scratch3 = t1;
2957 // Convert operands to 32-bit integers. Right in a2 and left in a3. The
2958 // registers a0 and a1 (right and left) are preserved for the runtime
2959 // call.
2960 FloatingPointHelper::LoadNumberAsInt32(masm,
2961 left,
2962 a3,
2963 heap_number_map,
2964 scratch1,
2965 scratch2,
2966 scratch3,
2967 f0,
2968 &transition);
2969 FloatingPointHelper::LoadNumberAsInt32(masm,
2970 right,
2971 a2,
2972 heap_number_map,
2973 scratch1,
2974 scratch2,
2975 scratch3,
2976 f0,
2977 &transition);
2978
2979 // The ECMA-262 standard specifies that, for shift operations, only the
2980 // 5 least significant bits of the shift value should be used.
2981 switch (op_) {
2982 case Token::BIT_OR:
2983 __ Or(a2, a3, Operand(a2));
2984 break;
2985 case Token::BIT_XOR:
2986 __ Xor(a2, a3, Operand(a2));
2987 break;
2988 case Token::BIT_AND:
2989 __ And(a2, a3, Operand(a2));
2990 break;
2991 case Token::SAR:
2992 __ And(a2, a2, Operand(0x1f));
2993 __ srav(a2, a3, a2);
2994 break;
2995 case Token::SHR:
2996 __ And(a2, a2, Operand(0x1f));
2997 __ srlv(a2, a3, a2);
2998 // SHR is special because it is required to produce a positive answer.
2999 // We only get a negative result if the shift value (a2) is 0.
3000 // This result cannot be respresented as a signed 32-bit integer, try
3001 // to return a heap number if we can.
3002 // The non FPU code does not support this special case, so jump to
3003 // runtime if we don't support it.
3004 if (CpuFeatures::IsSupported(FPU)) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00003005 __ Branch((result_type_ <= BinaryOpIC::INT32)
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003006 ? &transition
3007 : &return_heap_number,
3008 lt,
3009 a2,
3010 Operand(zero_reg));
3011 } else {
danno@chromium.org40cb8782011-05-25 07:58:50 +00003012 __ Branch((result_type_ <= BinaryOpIC::INT32)
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003013 ? &transition
3014 : &call_runtime,
3015 lt,
3016 a2,
3017 Operand(zero_reg));
3018 }
3019 break;
3020 case Token::SHL:
3021 __ And(a2, a2, Operand(0x1f));
3022 __ sllv(a2, a3, a2);
3023 break;
3024 default:
3025 UNREACHABLE();
3026 }
3027
3028 // Check if the result fits in a smi.
3029 __ Addu(scratch1, a2, Operand(0x40000000));
3030 // If not try to return a heap number. (We know the result is an int32.)
3031 __ Branch(&return_heap_number, lt, scratch1, Operand(zero_reg));
3032 // Tag the result and return.
3033 __ SmiTag(v0, a2);
3034 __ Ret();
3035
3036 __ bind(&return_heap_number);
3037 heap_number_result = t1;
3038 GenerateHeapResultAllocation(masm,
3039 heap_number_result,
3040 heap_number_map,
3041 scratch1,
3042 scratch2,
3043 &call_runtime);
3044
3045 if (CpuFeatures::IsSupported(FPU)) {
3046 CpuFeatures::Scope scope(FPU);
3047
3048 if (op_ != Token::SHR) {
3049 // Convert the result to a floating point value.
3050 __ mtc1(a2, double_scratch);
3051 __ cvt_d_w(double_scratch, double_scratch);
3052 } else {
3053 // The result must be interpreted as an unsigned 32-bit integer.
3054 __ mtc1(a2, double_scratch);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003055 __ Cvt_d_uw(double_scratch, double_scratch, single_scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003056 }
3057
3058 // Store the result.
3059 __ mov(v0, heap_number_result);
3060 __ sdc1(double_scratch, FieldMemOperand(v0, HeapNumber::kValueOffset));
3061 __ Ret();
3062 } else {
3063 // Tail call that writes the int32 in a2 to the heap number in v0, using
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003064 // a3 and a0 as scratch. v0 is preserved and returned.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003065 __ mov(a0, t1);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003066 WriteInt32ToHeapNumberStub stub(a2, v0, a3, a0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003067 __ TailCallStub(&stub);
3068 }
3069
3070 break;
3071 }
3072
3073 default:
3074 UNREACHABLE();
3075 }
3076
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003077 // We never expect DIV to yield an integer result, so we always generate
3078 // type transition code for DIV operations expecting an integer result: the
3079 // code will fall through to this type transition.
3080 if (transition.is_linked() ||
3081 ((op_ == Token::DIV) && (result_type_ <= BinaryOpIC::INT32))) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003082 __ bind(&transition);
3083 GenerateTypeTransition(masm);
3084 }
3085
3086 __ bind(&call_runtime);
3087 GenerateCallRuntime(masm);
3088}
3089
3090
danno@chromium.org40cb8782011-05-25 07:58:50 +00003091void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003092 Label call_runtime;
3093
3094 if (op_ == Token::ADD) {
3095 // Handle string addition here, because it is the only operation
3096 // that does not do a ToNumber conversion on the operands.
3097 GenerateAddStrings(masm);
3098 }
3099
3100 // Convert oddball arguments to numbers.
3101 Label check, done;
3102 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
3103 __ Branch(&check, ne, a1, Operand(t0));
3104 if (Token::IsBitOp(op_)) {
3105 __ li(a1, Operand(Smi::FromInt(0)));
3106 } else {
3107 __ LoadRoot(a1, Heap::kNanValueRootIndex);
3108 }
3109 __ jmp(&done);
3110 __ bind(&check);
3111 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
3112 __ Branch(&done, ne, a0, Operand(t0));
3113 if (Token::IsBitOp(op_)) {
3114 __ li(a0, Operand(Smi::FromInt(0)));
3115 } else {
3116 __ LoadRoot(a0, Heap::kNanValueRootIndex);
3117 }
3118 __ bind(&done);
3119
3120 GenerateHeapNumberStub(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003121}
3122
3123
danno@chromium.org40cb8782011-05-25 07:58:50 +00003124void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003125 Label call_runtime;
3126 GenerateFPOperation(masm, false, &call_runtime, &call_runtime);
3127
3128 __ bind(&call_runtime);
3129 GenerateCallRuntime(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003130}
3131
3132
danno@chromium.org40cb8782011-05-25 07:58:50 +00003133void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003134 Label call_runtime, call_string_add_or_runtime;
3135
3136 GenerateSmiCode(masm, &call_runtime, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
3137
3138 GenerateFPOperation(masm, false, &call_string_add_or_runtime, &call_runtime);
3139
3140 __ bind(&call_string_add_or_runtime);
3141 if (op_ == Token::ADD) {
3142 GenerateAddStrings(masm);
3143 }
3144
3145 __ bind(&call_runtime);
3146 GenerateCallRuntime(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003147}
3148
3149
danno@chromium.org40cb8782011-05-25 07:58:50 +00003150void BinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003151 ASSERT(op_ == Token::ADD);
3152 Label left_not_string, call_runtime;
3153
3154 Register left = a1;
3155 Register right = a0;
3156
3157 // Check if left argument is a string.
3158 __ JumpIfSmi(left, &left_not_string);
3159 __ GetObjectType(left, a2, a2);
3160 __ Branch(&left_not_string, ge, a2, Operand(FIRST_NONSTRING_TYPE));
3161
3162 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
3163 GenerateRegisterArgsPush(masm);
3164 __ TailCallStub(&string_add_left_stub);
3165
3166 // Left operand is not a string, test right.
3167 __ bind(&left_not_string);
3168 __ JumpIfSmi(right, &call_runtime);
3169 __ GetObjectType(right, a2, a2);
3170 __ Branch(&call_runtime, ge, a2, Operand(FIRST_NONSTRING_TYPE));
3171
3172 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
3173 GenerateRegisterArgsPush(masm);
3174 __ TailCallStub(&string_add_right_stub);
3175
3176 // At least one argument is not a string.
3177 __ bind(&call_runtime);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003178}
3179
3180
danno@chromium.org40cb8782011-05-25 07:58:50 +00003181void BinaryOpStub::GenerateCallRuntime(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003182 GenerateRegisterArgsPush(masm);
3183 switch (op_) {
3184 case Token::ADD:
3185 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
3186 break;
3187 case Token::SUB:
3188 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
3189 break;
3190 case Token::MUL:
3191 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
3192 break;
3193 case Token::DIV:
3194 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
3195 break;
3196 case Token::MOD:
3197 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
3198 break;
3199 case Token::BIT_OR:
3200 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
3201 break;
3202 case Token::BIT_AND:
3203 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
3204 break;
3205 case Token::BIT_XOR:
3206 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
3207 break;
3208 case Token::SAR:
3209 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
3210 break;
3211 case Token::SHR:
3212 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
3213 break;
3214 case Token::SHL:
3215 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
3216 break;
3217 default:
3218 UNREACHABLE();
3219 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00003220}
3221
3222
danno@chromium.org40cb8782011-05-25 07:58:50 +00003223void BinaryOpStub::GenerateHeapResultAllocation(
lrn@chromium.org7516f052011-03-30 08:52:27 +00003224 MacroAssembler* masm,
3225 Register result,
3226 Register heap_number_map,
3227 Register scratch1,
3228 Register scratch2,
3229 Label* gc_required) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003230
3231 // Code below will scratch result if allocation fails. To keep both arguments
3232 // intact for the runtime call result cannot be one of these.
3233 ASSERT(!result.is(a0) && !result.is(a1));
3234
3235 if (mode_ == OVERWRITE_LEFT || mode_ == OVERWRITE_RIGHT) {
3236 Label skip_allocation, allocated;
3237 Register overwritable_operand = mode_ == OVERWRITE_LEFT ? a1 : a0;
3238 // If the overwritable operand is already an object, we skip the
3239 // allocation of a heap number.
3240 __ JumpIfNotSmi(overwritable_operand, &skip_allocation);
3241 // Allocate a heap number for the result.
3242 __ AllocateHeapNumber(
3243 result, scratch1, scratch2, heap_number_map, gc_required);
3244 __ Branch(&allocated);
3245 __ bind(&skip_allocation);
3246 // Use object holding the overwritable operand for result.
3247 __ mov(result, overwritable_operand);
3248 __ bind(&allocated);
3249 } else {
3250 ASSERT(mode_ == NO_OVERWRITE);
3251 __ AllocateHeapNumber(
3252 result, scratch1, scratch2, heap_number_map, gc_required);
3253 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00003254}
3255
3256
danno@chromium.org40cb8782011-05-25 07:58:50 +00003257void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003258 __ Push(a1, a0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003259}
3260
3261
3262
3263void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003264 // Untagged case: double input in f4, double result goes
3265 // into f4.
3266 // Tagged case: tagged input on top of stack and in a0,
3267 // tagged result (heap number) goes into v0.
3268
3269 Label input_not_smi;
3270 Label loaded;
3271 Label calculate;
3272 Label invalid_cache;
3273 const Register scratch0 = t5;
3274 const Register scratch1 = t3;
3275 const Register cache_entry = a0;
3276 const bool tagged = (argument_type_ == TAGGED);
3277
3278 if (CpuFeatures::IsSupported(FPU)) {
3279 CpuFeatures::Scope scope(FPU);
3280
3281 if (tagged) {
3282 // Argument is a number and is on stack and in a0.
3283 // Load argument and check if it is a smi.
3284 __ JumpIfNotSmi(a0, &input_not_smi);
3285
3286 // Input is a smi. Convert to double and load the low and high words
3287 // of the double into a2, a3.
3288 __ sra(t0, a0, kSmiTagSize);
3289 __ mtc1(t0, f4);
3290 __ cvt_d_w(f4, f4);
danno@chromium.org40cb8782011-05-25 07:58:50 +00003291 __ Move(a2, a3, f4);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003292 __ Branch(&loaded);
3293
3294 __ bind(&input_not_smi);
3295 // Check if input is a HeapNumber.
3296 __ CheckMap(a0,
3297 a1,
3298 Heap::kHeapNumberMapRootIndex,
3299 &calculate,
danno@chromium.org40cb8782011-05-25 07:58:50 +00003300 DONT_DO_SMI_CHECK);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003301 // Input is a HeapNumber. Store the
3302 // low and high words into a2, a3.
3303 __ lw(a2, FieldMemOperand(a0, HeapNumber::kValueOffset));
3304 __ lw(a3, FieldMemOperand(a0, HeapNumber::kValueOffset + 4));
3305 } else {
3306 // Input is untagged double in f4. Output goes to f4.
danno@chromium.org40cb8782011-05-25 07:58:50 +00003307 __ Move(a2, a3, f4);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003308 }
3309 __ bind(&loaded);
3310 // a2 = low 32 bits of double value.
3311 // a3 = high 32 bits of double value.
3312 // Compute hash (the shifts are arithmetic):
3313 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
3314 __ Xor(a1, a2, a3);
3315 __ sra(t0, a1, 16);
3316 __ Xor(a1, a1, t0);
3317 __ sra(t0, a1, 8);
3318 __ Xor(a1, a1, t0);
3319 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
3320 __ And(a1, a1, Operand(TranscendentalCache::SubCache::kCacheSize - 1));
3321
3322 // a2 = low 32 bits of double value.
3323 // a3 = high 32 bits of double value.
3324 // a1 = TranscendentalCache::hash(double value).
3325 __ li(cache_entry, Operand(
3326 ExternalReference::transcendental_cache_array_address(
3327 masm->isolate())));
3328 // a0 points to cache array.
3329 __ lw(cache_entry, MemOperand(cache_entry, type_ * sizeof(
3330 Isolate::Current()->transcendental_cache()->caches_[0])));
3331 // a0 points to the cache for the type type_.
3332 // If NULL, the cache hasn't been initialized yet, so go through runtime.
3333 __ Branch(&invalid_cache, eq, cache_entry, Operand(zero_reg));
3334
3335#ifdef DEBUG
3336 // Check that the layout of cache elements match expectations.
3337 { TranscendentalCache::SubCache::Element test_elem[2];
3338 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
3339 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
3340 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
3341 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
3342 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
3343 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
3344 CHECK_EQ(0, elem_in0 - elem_start);
3345 CHECK_EQ(kIntSize, elem_in1 - elem_start);
3346 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
3347 }
3348#endif
3349
3350 // Find the address of the a1'st entry in the cache, i.e., &a0[a1*12].
3351 __ sll(t0, a1, 1);
3352 __ Addu(a1, a1, t0);
3353 __ sll(t0, a1, 2);
3354 __ Addu(cache_entry, cache_entry, t0);
3355
3356 // Check if cache matches: Double value is stored in uint32_t[2] array.
3357 __ lw(t0, MemOperand(cache_entry, 0));
3358 __ lw(t1, MemOperand(cache_entry, 4));
3359 __ lw(t2, MemOperand(cache_entry, 8));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003360 __ Branch(&calculate, ne, a2, Operand(t0));
3361 __ Branch(&calculate, ne, a3, Operand(t1));
3362 // Cache hit. Load result, cleanup and return.
3363 if (tagged) {
3364 // Pop input value from stack and load result into v0.
3365 __ Drop(1);
3366 __ mov(v0, t2);
3367 } else {
3368 // Load result into f4.
3369 __ ldc1(f4, FieldMemOperand(t2, HeapNumber::kValueOffset));
3370 }
3371 __ Ret();
3372 } // if (CpuFeatures::IsSupported(FPU))
3373
3374 __ bind(&calculate);
3375 if (tagged) {
3376 __ bind(&invalid_cache);
3377 __ TailCallExternalReference(ExternalReference(RuntimeFunction(),
3378 masm->isolate()),
3379 1,
3380 1);
3381 } else {
3382 if (!CpuFeatures::IsSupported(FPU)) UNREACHABLE();
3383 CpuFeatures::Scope scope(FPU);
3384
3385 Label no_update;
3386 Label skip_cache;
3387 const Register heap_number_map = t2;
3388
3389 // Call C function to calculate the result and update the cache.
3390 // Register a0 holds precalculated cache entry address; preserve
3391 // it on the stack and pop it into register cache_entry after the
3392 // call.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003393 __ Push(cache_entry, a2, a3);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003394 GenerateCallCFunction(masm, scratch0);
3395 __ GetCFunctionDoubleResult(f4);
3396
3397 // Try to update the cache. If we cannot allocate a
3398 // heap number, we return the result without updating.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003399 __ Pop(cache_entry, a2, a3);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003400 __ LoadRoot(t1, Heap::kHeapNumberMapRootIndex);
3401 __ AllocateHeapNumber(t2, scratch0, scratch1, t1, &no_update);
3402 __ sdc1(f4, FieldMemOperand(t2, HeapNumber::kValueOffset));
3403
3404 __ sw(a2, MemOperand(cache_entry, 0 * kPointerSize));
3405 __ sw(a3, MemOperand(cache_entry, 1 * kPointerSize));
3406 __ sw(t2, MemOperand(cache_entry, 2 * kPointerSize));
3407
3408 __ mov(v0, cache_entry);
3409 __ Ret();
3410
3411 __ bind(&invalid_cache);
3412 // The cache is invalid. Call runtime which will recreate the
3413 // cache.
3414 __ LoadRoot(t1, Heap::kHeapNumberMapRootIndex);
3415 __ AllocateHeapNumber(a0, scratch0, scratch1, t1, &skip_cache);
3416 __ sdc1(f4, FieldMemOperand(a0, HeapNumber::kValueOffset));
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003417 {
3418 FrameScope scope(masm, StackFrame::INTERNAL);
3419 __ push(a0);
3420 __ CallRuntime(RuntimeFunction(), 1);
3421 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003422 __ ldc1(f4, FieldMemOperand(v0, HeapNumber::kValueOffset));
3423 __ Ret();
3424
3425 __ bind(&skip_cache);
3426 // Call C function to calculate the result and answer directly
3427 // without updating the cache.
3428 GenerateCallCFunction(masm, scratch0);
3429 __ GetCFunctionDoubleResult(f4);
3430 __ bind(&no_update);
3431
3432 // We return the value in f4 without adding it to the cache, but
3433 // we cause a scavenging GC so that future allocations will succeed.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003434 {
3435 FrameScope scope(masm, StackFrame::INTERNAL);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003436
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003437 // Allocate an aligned object larger than a HeapNumber.
3438 ASSERT(4 * kPointerSize >= HeapNumber::kSize);
3439 __ li(scratch0, Operand(4 * kPointerSize));
3440 __ push(scratch0);
3441 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
3442 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003443 __ Ret();
3444 }
3445}
3446
3447
3448void TranscendentalCacheStub::GenerateCallCFunction(MacroAssembler* masm,
3449 Register scratch) {
3450 __ push(ra);
3451 __ PrepareCallCFunction(2, scratch);
danno@chromium.org40cb8782011-05-25 07:58:50 +00003452 if (IsMipsSoftFloatABI) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003453 __ Move(a0, a1, f4);
danno@chromium.org40cb8782011-05-25 07:58:50 +00003454 } else {
3455 __ mov_d(f12, f4);
3456 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003457 AllowExternalCallThatCantCauseGC scope(masm);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003458 switch (type_) {
3459 case TranscendentalCache::SIN:
3460 __ CallCFunction(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003461 ExternalReference::math_sin_double_function(masm->isolate()),
3462 0, 1);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003463 break;
3464 case TranscendentalCache::COS:
3465 __ CallCFunction(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003466 ExternalReference::math_cos_double_function(masm->isolate()),
3467 0, 1);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003468 break;
3469 case TranscendentalCache::LOG:
3470 __ CallCFunction(
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003471 ExternalReference::math_log_double_function(masm->isolate()),
3472 0, 1);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003473 break;
3474 default:
3475 UNIMPLEMENTED();
3476 break;
3477 }
3478 __ pop(ra);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003479}
3480
3481
3482Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003483 switch (type_) {
3484 // Add more cases when necessary.
3485 case TranscendentalCache::SIN: return Runtime::kMath_sin;
3486 case TranscendentalCache::COS: return Runtime::kMath_cos;
3487 case TranscendentalCache::LOG: return Runtime::kMath_log;
3488 default:
3489 UNIMPLEMENTED();
3490 return Runtime::kAbort;
3491 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00003492}
3493
3494
3495void StackCheckStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003496 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003497}
3498
3499
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003500void MathPowStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003501 Label call_runtime;
3502
3503 if (CpuFeatures::IsSupported(FPU)) {
3504 CpuFeatures::Scope scope(FPU);
3505
3506 Label base_not_smi;
3507 Label exponent_not_smi;
3508 Label convert_exponent;
3509
3510 const Register base = a0;
3511 const Register exponent = a2;
3512 const Register heapnumbermap = t1;
3513 const Register heapnumber = s0; // Callee-saved register.
3514 const Register scratch = t2;
3515 const Register scratch2 = t3;
3516
3517 // Alocate FP values in the ABI-parameter-passing regs.
3518 const DoubleRegister double_base = f12;
3519 const DoubleRegister double_exponent = f14;
3520 const DoubleRegister double_result = f0;
3521 const DoubleRegister double_scratch = f2;
3522
3523 __ LoadRoot(heapnumbermap, Heap::kHeapNumberMapRootIndex);
3524 __ lw(base, MemOperand(sp, 1 * kPointerSize));
3525 __ lw(exponent, MemOperand(sp, 0 * kPointerSize));
3526
3527 // Convert base to double value and store it in f0.
3528 __ JumpIfNotSmi(base, &base_not_smi);
3529 // Base is a Smi. Untag and convert it.
3530 __ SmiUntag(base);
3531 __ mtc1(base, double_scratch);
3532 __ cvt_d_w(double_base, double_scratch);
3533 __ Branch(&convert_exponent);
3534
3535 __ bind(&base_not_smi);
3536 __ lw(scratch, FieldMemOperand(base, JSObject::kMapOffset));
3537 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
3538 // Base is a heapnumber. Load it into double register.
3539 __ ldc1(double_base, FieldMemOperand(base, HeapNumber::kValueOffset));
3540
3541 __ bind(&convert_exponent);
3542 __ JumpIfNotSmi(exponent, &exponent_not_smi);
3543 __ SmiUntag(exponent);
3544
3545 // The base is in a double register and the exponent is
3546 // an untagged smi. Allocate a heap number and call a
3547 // C function for integer exponents. The register containing
3548 // the heap number is callee-saved.
3549 __ AllocateHeapNumber(heapnumber,
3550 scratch,
3551 scratch2,
3552 heapnumbermap,
3553 &call_runtime);
3554 __ push(ra);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003555 __ PrepareCallCFunction(1, 1, scratch);
danno@chromium.org40cb8782011-05-25 07:58:50 +00003556 __ SetCallCDoubleArguments(double_base, exponent);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003557 {
3558 AllowExternalCallThatCantCauseGC scope(masm);
3559 __ CallCFunction(
3560 ExternalReference::power_double_int_function(masm->isolate()), 1, 1);
3561 __ pop(ra);
3562 __ GetCFunctionDoubleResult(double_result);
3563 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003564 __ sdc1(double_result,
3565 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3566 __ mov(v0, heapnumber);
3567 __ DropAndRet(2 * kPointerSize);
3568
3569 __ bind(&exponent_not_smi);
3570 __ lw(scratch, FieldMemOperand(exponent, JSObject::kMapOffset));
3571 __ Branch(&call_runtime, ne, scratch, Operand(heapnumbermap));
3572 // Exponent is a heapnumber. Load it into double register.
3573 __ ldc1(double_exponent,
3574 FieldMemOperand(exponent, HeapNumber::kValueOffset));
3575
3576 // The base and the exponent are in double registers.
3577 // Allocate a heap number and call a C function for
3578 // double exponents. The register containing
3579 // the heap number is callee-saved.
3580 __ AllocateHeapNumber(heapnumber,
3581 scratch,
3582 scratch2,
3583 heapnumbermap,
3584 &call_runtime);
3585 __ push(ra);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003586 __ PrepareCallCFunction(0, 2, scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003587 // ABI (o32) for func(double a, double b): a in f12, b in f14.
3588 ASSERT(double_base.is(f12));
3589 ASSERT(double_exponent.is(f14));
danno@chromium.org40cb8782011-05-25 07:58:50 +00003590 __ SetCallCDoubleArguments(double_base, double_exponent);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003591 {
3592 AllowExternalCallThatCantCauseGC scope(masm);
3593 __ CallCFunction(
3594 ExternalReference::power_double_double_function(masm->isolate()),
3595 0,
3596 2);
3597 __ pop(ra);
3598 __ GetCFunctionDoubleResult(double_result);
3599 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003600 __ sdc1(double_result,
3601 FieldMemOperand(heapnumber, HeapNumber::kValueOffset));
3602 __ mov(v0, heapnumber);
3603 __ DropAndRet(2 * kPointerSize);
3604 }
3605
3606 __ bind(&call_runtime);
3607 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00003608}
3609
3610
lrn@chromium.org7516f052011-03-30 08:52:27 +00003611bool CEntryStub::NeedsImmovableCode() {
3612 return true;
3613}
3614
3615
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003616bool CEntryStub::IsPregenerated() {
3617 return (!save_doubles_ || ISOLATE->fp_stubs_generated()) &&
3618 result_size_ == 1;
3619}
3620
3621
3622void CodeStub::GenerateStubsAheadOfTime() {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003623 CEntryStub::GenerateAheadOfTime();
3624 WriteInt32ToHeapNumberStub::GenerateFixedRegStubsAheadOfTime();
3625 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime();
3626 RecordWriteStub::GenerateFixedRegStubsAheadOfTime();
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003627}
3628
3629
3630void CodeStub::GenerateFPStubs() {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003631 CEntryStub save_doubles(1, kSaveFPRegs);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003632 Handle<Code> code = save_doubles.GetCode();
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003633 code->set_is_pregenerated(true);
3634 StoreBufferOverflowStub stub(kSaveFPRegs);
3635 stub.GetCode()->set_is_pregenerated(true);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003636 code->GetIsolate()->set_fp_stubs_generated(true);
3637}
3638
3639
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003640void CEntryStub::GenerateAheadOfTime() {
3641 CEntryStub stub(1, kDontSaveFPRegs);
3642 Handle<Code> code = stub.GetCode();
3643 code->set_is_pregenerated(true);
3644}
3645
3646
lrn@chromium.org7516f052011-03-30 08:52:27 +00003647void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003648 __ Throw(v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003649}
3650
3651
3652void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3653 UncatchableExceptionType type) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003654 __ ThrowUncatchable(type, v0);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003655}
3656
3657
3658void CEntryStub::GenerateCore(MacroAssembler* masm,
3659 Label* throw_normal_exception,
3660 Label* throw_termination_exception,
3661 Label* throw_out_of_memory_exception,
3662 bool do_gc,
3663 bool always_allocate) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003664 // v0: result parameter for PerformGC, if any
3665 // s0: number of arguments including receiver (C callee-saved)
3666 // s1: pointer to the first argument (C callee-saved)
3667 // s2: pointer to builtin function (C callee-saved)
3668
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003669 Isolate* isolate = masm->isolate();
3670
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003671 if (do_gc) {
3672 // Move result passed in v0 into a0 to call PerformGC.
3673 __ mov(a0, v0);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003674 __ PrepareCallCFunction(1, 0, a1);
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003675 __ CallCFunction(ExternalReference::perform_gc_function(isolate), 1, 0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003676 }
3677
3678 ExternalReference scope_depth =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003679 ExternalReference::heap_always_allocate_scope_depth(isolate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003680 if (always_allocate) {
3681 __ li(a0, Operand(scope_depth));
3682 __ lw(a1, MemOperand(a0));
3683 __ Addu(a1, a1, Operand(1));
3684 __ sw(a1, MemOperand(a0));
3685 }
3686
3687 // Prepare arguments for C routine: a0 = argc, a1 = argv
3688 __ mov(a0, s0);
3689 __ mov(a1, s1);
3690
3691 // We are calling compiled C/C++ code. a0 and a1 hold our two arguments. We
3692 // also need to reserve the 4 argument slots on the stack.
3693
3694 __ AssertStackIsAligned();
3695
3696 __ li(a2, Operand(ExternalReference::isolate_address()));
3697
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00003698 // To let the GC traverse the return address of the exit frames, we need to
3699 // know where the return address is. The CEntryStub is unmovable, so
3700 // we can store the address on the stack to be able to find it again and
3701 // we never have to restore it, because it will not change.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003702 { Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm);
3703 // This branch-and-link sequence is needed to find the current PC on mips,
3704 // saved to the ra register.
3705 // Use masm-> here instead of the double-underscore macro since extra
3706 // coverage code can interfere with the proper calculation of ra.
3707 Label find_ra;
3708 masm->bal(&find_ra); // bal exposes branch delay slot.
3709 masm->nop(); // Branch delay slot nop.
3710 masm->bind(&find_ra);
3711
3712 // Adjust the value in ra to point to the correct return location, 2nd
3713 // instruction past the real call into C code (the jalr(t9)), and push it.
3714 // This is the return address of the exit frame.
3715 const int kNumInstructionsToJump = 6;
3716 masm->Addu(ra, ra, kNumInstructionsToJump * kPointerSize);
3717 masm->sw(ra, MemOperand(sp)); // This spot was reserved in EnterExitFrame.
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003718 masm->Subu(sp, sp, kCArgsSlotsSize);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003719 // Stack is still aligned.
3720
3721 // Call the C routine.
3722 masm->mov(t9, s2); // Function pointer to t9 to conform to ABI for PIC.
3723 masm->jalr(t9);
3724 masm->nop(); // Branch delay slot nop.
3725 // Make sure the stored 'ra' points to this position.
3726 ASSERT_EQ(kNumInstructionsToJump,
3727 masm->InstructionsGeneratedSince(&find_ra));
3728 }
3729
3730 // Restore stack (remove arg slots).
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003731 __ Addu(sp, sp, kCArgsSlotsSize);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003732
3733 if (always_allocate) {
3734 // It's okay to clobber a2 and a3 here. v0 & v1 contain result.
3735 __ li(a2, Operand(scope_depth));
3736 __ lw(a3, MemOperand(a2));
3737 __ Subu(a3, a3, Operand(1));
3738 __ sw(a3, MemOperand(a2));
3739 }
3740
3741 // Check for failure result.
3742 Label failure_returned;
3743 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3744 __ addiu(a2, v0, 1);
3745 __ andi(t0, a2, kFailureTagMask);
3746 __ Branch(&failure_returned, eq, t0, Operand(zero_reg));
3747
3748 // Exit C frame and return.
3749 // v0:v1: result
3750 // sp: stack pointer
3751 // fp: frame pointer
3752 __ LeaveExitFrame(save_doubles_, s0);
3753 __ Ret();
3754
3755 // Check if we should retry or throw exception.
3756 Label retry;
3757 __ bind(&failure_returned);
3758 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3759 __ andi(t0, v0, ((1 << kFailureTypeTagSize) - 1) << kFailureTagSize);
3760 __ Branch(&retry, eq, t0, Operand(zero_reg));
3761
3762 // Special handling of out of memory exceptions.
3763 Failure* out_of_memory = Failure::OutOfMemoryException();
3764 __ Branch(throw_out_of_memory_exception, eq,
3765 v0, Operand(reinterpret_cast<int32_t>(out_of_memory)));
3766
3767 // Retrieve the pending exception and clear the variable.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003768 __ li(a3, Operand(isolate->factory()->the_hole_value()));
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003769 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003770 isolate)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003771 __ lw(v0, MemOperand(t0));
3772 __ sw(a3, MemOperand(t0));
3773
3774 // Special handling of termination exceptions which are uncatchable
3775 // by javascript code.
3776 __ Branch(throw_termination_exception, eq,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003777 v0, Operand(isolate->factory()->termination_exception()));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003778
3779 // Handle normal exception.
3780 __ jmp(throw_normal_exception);
3781
3782 __ bind(&retry);
3783 // Last failure (v0) will be moved to (a0) for parameter when retrying.
lrn@chromium.org7516f052011-03-30 08:52:27 +00003784}
3785
3786
3787void CEntryStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003788 // Called from JavaScript; parameters are on stack as if calling JS function
3789 // a0: number of arguments including receiver
3790 // a1: pointer to builtin function
3791 // fp: frame pointer (restored after C call)
3792 // sp: stack pointer (restored as callee's sp after C call)
3793 // cp: current context (C callee-saved)
3794
3795 // NOTE: Invocations of builtins may return failure objects
3796 // instead of a proper result. The builtin entry handles
3797 // this by performing a garbage collection and retrying the
3798 // builtin once.
3799
3800 // Compute the argv pointer in a callee-saved register.
3801 __ sll(s1, a0, kPointerSizeLog2);
3802 __ Addu(s1, sp, s1);
3803 __ Subu(s1, s1, Operand(kPointerSize));
3804
3805 // Enter the exit frame that transitions from JavaScript to C++.
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003806 FrameScope scope(masm, StackFrame::MANUAL);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003807 __ EnterExitFrame(save_doubles_);
3808
3809 // Setup argc and the builtin function in callee-saved registers.
3810 __ mov(s0, a0);
3811 __ mov(s2, a1);
3812
3813 // s0: number of arguments (C callee-saved)
3814 // s1: pointer to first argument (C callee-saved)
3815 // s2: pointer to builtin function (C callee-saved)
3816
3817 Label throw_normal_exception;
3818 Label throw_termination_exception;
3819 Label throw_out_of_memory_exception;
3820
3821 // Call into the runtime system.
3822 GenerateCore(masm,
3823 &throw_normal_exception,
3824 &throw_termination_exception,
3825 &throw_out_of_memory_exception,
3826 false,
3827 false);
3828
3829 // Do space-specific GC and retry runtime call.
3830 GenerateCore(masm,
3831 &throw_normal_exception,
3832 &throw_termination_exception,
3833 &throw_out_of_memory_exception,
3834 true,
3835 false);
3836
3837 // Do full GC and retry runtime call one final time.
3838 Failure* failure = Failure::InternalError();
3839 __ li(v0, Operand(reinterpret_cast<int32_t>(failure)));
3840 GenerateCore(masm,
3841 &throw_normal_exception,
3842 &throw_termination_exception,
3843 &throw_out_of_memory_exception,
3844 true,
3845 true);
3846
3847 __ bind(&throw_out_of_memory_exception);
3848 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3849
3850 __ bind(&throw_termination_exception);
3851 GenerateThrowUncatchable(masm, TERMINATION);
3852
3853 __ bind(&throw_normal_exception);
3854 GenerateThrowTOS(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00003855}
3856
3857
3858void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00003859 Label invoke, handler_entry, exit;
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003860 Isolate* isolate = masm->isolate();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003861
3862 // Registers:
3863 // a0: entry address
3864 // a1: function
3865 // a2: reveiver
3866 // a3: argc
3867 //
3868 // Stack:
3869 // 4 args slots
3870 // args
3871
3872 // Save callee saved registers on the stack.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00003873 __ MultiPush(kCalleeSaved | ra.bit());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003874
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003875 if (CpuFeatures::IsSupported(FPU)) {
3876 CpuFeatures::Scope scope(FPU);
3877 // Save callee-saved FPU registers.
3878 __ MultiPushFPU(kCalleeSavedFPU);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003879 // Set up the reserved register for 0.0.
3880 __ Move(kDoubleRegZero, 0.0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003881 }
3882
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00003883
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003884 // Load argv in s0 register.
fschneider@chromium.org1805e212011-09-05 10:49:12 +00003885 int offset_to_argv = (kNumCalleeSaved + 1) * kPointerSize;
3886 if (CpuFeatures::IsSupported(FPU)) {
3887 offset_to_argv += kNumCalleeSavedFPU * kDoubleSize;
3888 }
3889
3890 __ lw(s0, MemOperand(sp, offset_to_argv + kCArgsSlotsSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003891
3892 // We build an EntryFrame.
3893 __ li(t3, Operand(-1)); // Push a bad frame pointer to fail if it is used.
3894 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3895 __ li(t2, Operand(Smi::FromInt(marker)));
3896 __ li(t1, Operand(Smi::FromInt(marker)));
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003897 __ li(t0, Operand(ExternalReference(Isolate::kCEntryFPAddress,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003898 isolate)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003899 __ lw(t0, MemOperand(t0));
3900 __ Push(t3, t2, t1, t0);
3901 // Setup frame pointer for the frame to be pushed.
3902 __ addiu(fp, sp, -EntryFrameConstants::kCallerFPOffset);
3903
3904 // Registers:
3905 // a0: entry_address
3906 // a1: function
3907 // a2: reveiver_pointer
3908 // a3: argc
3909 // s0: argv
3910 //
3911 // Stack:
3912 // caller fp |
3913 // function slot | entry frame
3914 // context slot |
3915 // bad fp (0xff...f) |
3916 // callee saved registers + ra
3917 // 4 args slots
3918 // args
3919
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003920 // If this is the outermost JS call, set js_entry_sp value.
3921 Label non_outermost_js;
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003922 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
whesse@chromium.org030d38e2011-07-13 13:23:34 +00003923 __ li(t1, Operand(ExternalReference(js_entry_sp)));
3924 __ lw(t2, MemOperand(t1));
3925 __ Branch(&non_outermost_js, ne, t2, Operand(zero_reg));
3926 __ sw(fp, MemOperand(t1));
3927 __ li(t0, Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
3928 Label cont;
3929 __ b(&cont);
3930 __ nop(); // Branch delay slot nop.
3931 __ bind(&non_outermost_js);
3932 __ li(t0, Operand(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
3933 __ bind(&cont);
3934 __ push(t0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003935
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00003936 // Jump to a faked try block that does the invoke, with a faked catch
3937 // block that sets the pending exception.
3938 __ jmp(&invoke);
3939 __ bind(&handler_entry);
3940 handler_offset_ = handler_entry.pos();
3941 // Caught exception: Store result (exception) in the pending exception
3942 // field in the JSEnv and return a failure sentinel. Coming in here the
3943 // fp will be invalid because the PushTryHandler below sets it to 0 to
3944 // signal the existence of the JSEntry frame.
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003945 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003946 isolate)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003947 __ sw(v0, MemOperand(t0)); // We come back from 'invoke'. result is in v0.
3948 __ li(v0, Operand(reinterpret_cast<int32_t>(Failure::Exception())));
3949 __ b(&exit); // b exposes branch delay slot.
3950 __ nop(); // Branch delay slot nop.
3951
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00003952 // Invoke: Link this frame into the handler chain. There's only one
3953 // handler block in this code object, so its index is 0.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003954 __ bind(&invoke);
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00003955 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER, 0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003956 // If an exception not caught by another handler occurs, this handler
3957 // returns control to the code after the bal(&invoke) above, which
3958 // restores all kCalleeSaved registers (including cp and fp) to their
3959 // saved values before returning a failure to C.
3960
3961 // Clear any pending exceptions.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003962 __ li(t1, Operand(isolate->factory()->the_hole_value()));
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00003963 __ li(t0, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003964 isolate)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003965 __ sw(t1, MemOperand(t0));
3966
3967 // Invoke the function by calling through JS entry trampoline builtin.
3968 // Notice that we cannot store a reference to the trampoline code directly in
3969 // this stub, because runtime stubs are not traversed when doing GC.
3970
3971 // Registers:
3972 // a0: entry_address
3973 // a1: function
3974 // a2: reveiver_pointer
3975 // a3: argc
3976 // s0: argv
3977 //
3978 // Stack:
3979 // handler frame
3980 // entry frame
3981 // callee saved registers + ra
3982 // 4 args slots
3983 // args
3984
3985 if (is_construct) {
3986 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00003987 isolate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00003988 __ li(t0, Operand(construct_entry));
3989 } else {
3990 ExternalReference entry(Builtins::kJSEntryTrampoline, masm->isolate());
3991 __ li(t0, Operand(entry));
3992 }
3993 __ lw(t9, MemOperand(t0)); // Deref address.
3994
3995 // Call JSEntryTrampoline.
3996 __ addiu(t9, t9, Code::kHeaderSize - kHeapObjectTag);
3997 __ Call(t9);
3998
danno@chromium.org40cb8782011-05-25 07:58:50 +00003999 // Unlink this frame from the handler chain.
4000 __ PopTryHandler();
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004001
danno@chromium.org40cb8782011-05-25 07:58:50 +00004002 __ bind(&exit); // v0 holds result
whesse@chromium.org030d38e2011-07-13 13:23:34 +00004003 // Check if the current stack frame is marked as the outermost JS frame.
4004 Label non_outermost_js_2;
4005 __ pop(t1);
4006 __ Branch(&non_outermost_js_2, ne, t1,
4007 Operand(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
4008 __ li(t1, Operand(ExternalReference(js_entry_sp)));
4009 __ sw(zero_reg, MemOperand(t1));
4010 __ bind(&non_outermost_js_2);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004011
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004012 // Restore the top frame descriptors from the stack.
4013 __ pop(t1);
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004014 __ li(t0, Operand(ExternalReference(Isolate::kCEntryFPAddress,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004015 isolate)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004016 __ sw(t1, MemOperand(t0));
4017
4018 // Reset the stack to the callee saved registers.
4019 __ addiu(sp, sp, -EntryFrameConstants::kCallerFPOffset);
4020
fschneider@chromium.org1805e212011-09-05 10:49:12 +00004021 if (CpuFeatures::IsSupported(FPU)) {
4022 CpuFeatures::Scope scope(FPU);
4023 // Restore callee-saved fpu registers.
4024 __ MultiPopFPU(kCalleeSavedFPU);
4025 }
4026
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004027 // Restore callee saved registers from the stack.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004028 __ MultiPop(kCalleeSaved | ra.bit());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004029 // Return.
4030 __ Jump(ra);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004031}
4032
4033
danno@chromium.org40cb8782011-05-25 07:58:50 +00004034// Uses registers a0 to t0.
4035// Expected input (depending on whether args are in registers or on the stack):
4036// * object: a0 or at sp + 1 * kPointerSize.
4037// * function: a1 or at sp.
4038//
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004039// An inlined call site may have been generated before calling this stub.
4040// In this case the offset to the inline site to patch is passed on the stack,
4041// in the safepoint slot for register t0.
lrn@chromium.org7516f052011-03-30 08:52:27 +00004042void InstanceofStub::Generate(MacroAssembler* masm) {
danno@chromium.org40cb8782011-05-25 07:58:50 +00004043 // Call site inlining and patching implies arguments in registers.
4044 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
4045 // ReturnTrueFalse is only implemented for inlined call sites.
4046 ASSERT(!ReturnTrueFalseObject() || HasCallSiteInlineCheck());
4047
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004048 // Fixed register usage throughout the stub:
4049 const Register object = a0; // Object (lhs).
danno@chromium.org40cb8782011-05-25 07:58:50 +00004050 Register map = a3; // Map of the object.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004051 const Register function = a1; // Function (rhs).
4052 const Register prototype = t0; // Prototype of the function.
danno@chromium.org40cb8782011-05-25 07:58:50 +00004053 const Register inline_site = t5;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004054 const Register scratch = a2;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004055
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004056 const int32_t kDeltaToLoadBoolResult = 4 * kPointerSize;
4057
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004058 Label slow, loop, is_instance, is_not_instance, not_js_object;
danno@chromium.org40cb8782011-05-25 07:58:50 +00004059
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004060 if (!HasArgsInRegisters()) {
4061 __ lw(object, MemOperand(sp, 1 * kPointerSize));
4062 __ lw(function, MemOperand(sp, 0));
4063 }
4064
4065 // Check that the left hand is a JS object and load map.
4066 __ JumpIfSmi(object, &not_js_object);
4067 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
4068
danno@chromium.org40cb8782011-05-25 07:58:50 +00004069 // If there is a call site cache don't look in the global cache, but do the
4070 // real lookup and update the call site cache.
4071 if (!HasCallSiteInlineCheck()) {
4072 Label miss;
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004073 __ LoadRoot(at, Heap::kInstanceofCacheFunctionRootIndex);
4074 __ Branch(&miss, ne, function, Operand(at));
4075 __ LoadRoot(at, Heap::kInstanceofCacheMapRootIndex);
4076 __ Branch(&miss, ne, map, Operand(at));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004077 __ LoadRoot(v0, Heap::kInstanceofCacheAnswerRootIndex);
4078 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004079
danno@chromium.org40cb8782011-05-25 07:58:50 +00004080 __ bind(&miss);
4081 }
4082
4083 // Get the prototype of the function.
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00004084 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004085
4086 // Check that the function prototype is a JS object.
4087 __ JumpIfSmi(prototype, &slow);
4088 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
4089
danno@chromium.org40cb8782011-05-25 07:58:50 +00004090 // Update the global instanceof or call site inlined cache with the current
4091 // map and function. The cached answer will be set when it is known below.
4092 if (!HasCallSiteInlineCheck()) {
4093 __ StoreRoot(function, Heap::kInstanceofCacheFunctionRootIndex);
4094 __ StoreRoot(map, Heap::kInstanceofCacheMapRootIndex);
4095 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004096 ASSERT(HasArgsInRegisters());
4097 // Patch the (relocated) inlined map check.
4098
4099 // The offset was stored in t0 safepoint slot.
4100 // (See LCodeGen::DoDeferredLInstanceOfKnownGlobal)
4101 __ LoadFromSafepointRegisterSlot(scratch, t0);
4102 __ Subu(inline_site, ra, scratch);
4103 // Patch the relocated value to map.
4104 __ PatchRelocatedValue(inline_site, scratch, map);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004105 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004106
4107 // Register mapping: a3 is object map and t0 is function prototype.
4108 // Get prototype of object into a2.
4109 __ lw(scratch, FieldMemOperand(map, Map::kPrototypeOffset));
4110
danno@chromium.org40cb8782011-05-25 07:58:50 +00004111 // We don't need map any more. Use it as a scratch register.
4112 Register scratch2 = map;
4113 map = no_reg;
4114
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004115 // Loop through the prototype chain looking for the function prototype.
danno@chromium.org40cb8782011-05-25 07:58:50 +00004116 __ LoadRoot(scratch2, Heap::kNullValueRootIndex);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004117 __ bind(&loop);
4118 __ Branch(&is_instance, eq, scratch, Operand(prototype));
danno@chromium.org40cb8782011-05-25 07:58:50 +00004119 __ Branch(&is_not_instance, eq, scratch, Operand(scratch2));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004120 __ lw(scratch, FieldMemOperand(scratch, HeapObject::kMapOffset));
4121 __ lw(scratch, FieldMemOperand(scratch, Map::kPrototypeOffset));
4122 __ Branch(&loop);
4123
4124 __ bind(&is_instance);
4125 ASSERT(Smi::FromInt(0) == 0);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004126 if (!HasCallSiteInlineCheck()) {
4127 __ mov(v0, zero_reg);
4128 __ StoreRoot(v0, Heap::kInstanceofCacheAnswerRootIndex);
4129 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004130 // Patch the call site to return true.
4131 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4132 __ Addu(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
4133 // Get the boolean result location in scratch and patch it.
4134 __ PatchRelocatedValue(inline_site, scratch, v0);
4135
4136 if (!ReturnTrueFalseObject()) {
4137 ASSERT_EQ(Smi::FromInt(0), 0);
4138 __ mov(v0, zero_reg);
4139 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004140 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004141 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
4142
4143 __ bind(&is_not_instance);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004144 if (!HasCallSiteInlineCheck()) {
4145 __ li(v0, Operand(Smi::FromInt(1)));
4146 __ StoreRoot(v0, Heap::kInstanceofCacheAnswerRootIndex);
4147 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004148 // Patch the call site to return false.
4149 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4150 __ Addu(inline_site, inline_site, Operand(kDeltaToLoadBoolResult));
4151 // Get the boolean result location in scratch and patch it.
4152 __ PatchRelocatedValue(inline_site, scratch, v0);
4153
4154 if (!ReturnTrueFalseObject()) {
4155 __ li(v0, Operand(Smi::FromInt(1)));
4156 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004157 }
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004158
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004159 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
4160
4161 Label object_not_null, object_not_null_or_smi;
4162 __ bind(&not_js_object);
4163 // Before null, smi and string value checks, check that the rhs is a function
4164 // as for a non-function rhs an exception needs to be thrown.
4165 __ JumpIfSmi(function, &slow);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004166 __ GetObjectType(function, scratch2, scratch);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004167 __ Branch(&slow, ne, scratch, Operand(JS_FUNCTION_TYPE));
4168
4169 // Null is not instance of anything.
4170 __ Branch(&object_not_null, ne, scratch,
4171 Operand(masm->isolate()->factory()->null_value()));
4172 __ li(v0, Operand(Smi::FromInt(1)));
4173 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
4174
4175 __ bind(&object_not_null);
4176 // Smi values are not instances of anything.
4177 __ JumpIfNotSmi(object, &object_not_null_or_smi);
4178 __ li(v0, Operand(Smi::FromInt(1)));
4179 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
4180
4181 __ bind(&object_not_null_or_smi);
4182 // String values are not instances of anything.
4183 __ IsObjectJSStringType(object, scratch, &slow);
4184 __ li(v0, Operand(Smi::FromInt(1)));
4185 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
4186
4187 // Slow-case. Tail call builtin.
4188 __ bind(&slow);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004189 if (!ReturnTrueFalseObject()) {
4190 if (HasArgsInRegisters()) {
4191 __ Push(a0, a1);
4192 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004193 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
danno@chromium.org40cb8782011-05-25 07:58:50 +00004194 } else {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004195 {
4196 FrameScope scope(masm, StackFrame::INTERNAL);
4197 __ Push(a0, a1);
4198 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
4199 }
danno@chromium.org40cb8782011-05-25 07:58:50 +00004200 __ mov(a0, v0);
4201 __ LoadRoot(v0, Heap::kTrueValueRootIndex);
4202 __ DropAndRet(HasArgsInRegisters() ? 0 : 2, eq, a0, Operand(zero_reg));
4203 __ LoadRoot(v0, Heap::kFalseValueRootIndex);
4204 __ DropAndRet(HasArgsInRegisters() ? 0 : 2);
4205 }
lrn@chromium.org7516f052011-03-30 08:52:27 +00004206}
4207
4208
danno@chromium.org40cb8782011-05-25 07:58:50 +00004209Register InstanceofStub::left() { return a0; }
4210
4211
4212Register InstanceofStub::right() { return a1; }
4213
4214
lrn@chromium.org7516f052011-03-30 08:52:27 +00004215void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004216 // The displacement is the offset of the last parameter (if any)
4217 // relative to the frame pointer.
4218 static const int kDisplacement =
4219 StandardFrameConstants::kCallerSPOffset - kPointerSize;
4220
4221 // Check that the key is a smiGenerateReadElement.
4222 Label slow;
4223 __ JumpIfNotSmi(a1, &slow);
4224
4225 // Check if the calling frame is an arguments adaptor frame.
4226 Label adaptor;
4227 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4228 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
4229 __ Branch(&adaptor,
4230 eq,
4231 a3,
4232 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4233
4234 // Check index (a1) against formal parameters count limit passed in
4235 // through register a0. Use unsigned comparison to get negative
4236 // check for free.
4237 __ Branch(&slow, hs, a1, Operand(a0));
4238
4239 // Read the argument from the stack and return it.
4240 __ subu(a3, a0, a1);
4241 __ sll(t3, a3, kPointerSizeLog2 - kSmiTagSize);
4242 __ Addu(a3, fp, Operand(t3));
4243 __ lw(v0, MemOperand(a3, kDisplacement));
4244 __ Ret();
4245
4246 // Arguments adaptor case: Check index (a1) against actual arguments
4247 // limit found in the arguments adaptor frame. Use unsigned
4248 // comparison to get negative check for free.
4249 __ bind(&adaptor);
4250 __ lw(a0, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4251 __ Branch(&slow, Ugreater_equal, a1, Operand(a0));
4252
4253 // Read the argument from the adaptor frame and return it.
4254 __ subu(a3, a0, a1);
4255 __ sll(t3, a3, kPointerSizeLog2 - kSmiTagSize);
4256 __ Addu(a3, a2, Operand(t3));
4257 __ lw(v0, MemOperand(a3, kDisplacement));
4258 __ Ret();
4259
4260 // Slow-case: Handle non-smi or out-of-bounds access to arguments
4261 // by calling the runtime system.
4262 __ bind(&slow);
4263 __ push(a1);
4264 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004265}
4266
4267
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004268void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004269 // sp[0] : number of parameters
4270 // sp[4] : receiver displacement
4271 // sp[8] : function
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004272 // Check if the calling frame is an arguments adaptor frame.
4273 Label runtime;
4274 __ lw(a3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4275 __ lw(a2, MemOperand(a3, StandardFrameConstants::kContextOffset));
4276 __ Branch(&runtime, ne,
4277 a2, Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004278
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004279 // Patch the arguments.length and the parameters pointer in the current frame.
4280 __ lw(a2, MemOperand(a3, ArgumentsAdaptorFrameConstants::kLengthOffset));
4281 __ sw(a2, MemOperand(sp, 0 * kPointerSize));
4282 __ sll(t3, a2, 1);
4283 __ Addu(a3, a3, Operand(t3));
4284 __ addiu(a3, a3, StandardFrameConstants::kCallerSPOffset);
4285 __ sw(a3, MemOperand(sp, 1 * kPointerSize));
4286
4287 __ bind(&runtime);
4288 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
4289}
4290
4291
4292void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
4293 // Stack layout:
4294 // sp[0] : number of parameters (tagged)
4295 // sp[4] : address of receiver argument
4296 // sp[8] : function
4297 // Registers used over whole function:
4298 // t2 : allocated object (tagged)
4299 // t5 : mapped parameter count (tagged)
4300
4301 __ lw(a1, MemOperand(sp, 0 * kPointerSize));
4302 // a1 = parameter count (tagged)
4303
4304 // Check if the calling frame is an arguments adaptor frame.
4305 Label runtime;
4306 Label adaptor_frame, try_allocate;
4307 __ lw(a3, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4308 __ lw(a2, MemOperand(a3, StandardFrameConstants::kContextOffset));
4309 __ Branch(&adaptor_frame, eq, a2,
4310 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4311
4312 // No adaptor, parameter count = argument count.
4313 __ mov(a2, a1);
4314 __ b(&try_allocate);
4315 __ nop(); // Branch delay slot nop.
4316
4317 // We have an adaptor frame. Patch the parameters pointer.
4318 __ bind(&adaptor_frame);
4319 __ lw(a2, MemOperand(a3, ArgumentsAdaptorFrameConstants::kLengthOffset));
4320 __ sll(t6, a2, 1);
4321 __ Addu(a3, a3, Operand(t6));
4322 __ Addu(a3, a3, Operand(StandardFrameConstants::kCallerSPOffset));
4323 __ sw(a3, MemOperand(sp, 1 * kPointerSize));
4324
4325 // a1 = parameter count (tagged)
4326 // a2 = argument count (tagged)
4327 // Compute the mapped parameter count = min(a1, a2) in a1.
4328 Label skip_min;
4329 __ Branch(&skip_min, lt, a1, Operand(a2));
4330 __ mov(a1, a2);
4331 __ bind(&skip_min);
4332
4333 __ bind(&try_allocate);
4334
4335 // Compute the sizes of backing store, parameter map, and arguments object.
4336 // 1. Parameter map, has 2 extra words containing context and backing store.
4337 const int kParameterMapHeaderSize =
4338 FixedArray::kHeaderSize + 2 * kPointerSize;
4339 // If there are no mapped parameters, we do not need the parameter_map.
4340 Label param_map_size;
4341 ASSERT_EQ(0, Smi::FromInt(0));
4342 __ Branch(USE_DELAY_SLOT, &param_map_size, eq, a1, Operand(zero_reg));
4343 __ mov(t5, zero_reg); // In delay slot: param map size = 0 when a1 == 0.
4344 __ sll(t5, a1, 1);
4345 __ addiu(t5, t5, kParameterMapHeaderSize);
4346 __ bind(&param_map_size);
4347
4348 // 2. Backing store.
4349 __ sll(t6, a2, 1);
4350 __ Addu(t5, t5, Operand(t6));
4351 __ Addu(t5, t5, Operand(FixedArray::kHeaderSize));
4352
4353 // 3. Arguments object.
4354 __ Addu(t5, t5, Operand(Heap::kArgumentsObjectSize));
4355
4356 // Do the allocation of all three objects in one go.
4357 __ AllocateInNewSpace(t5, v0, a3, t0, &runtime, TAG_OBJECT);
4358
4359 // v0 = address of new object(s) (tagged)
4360 // a2 = argument count (tagged)
4361 // Get the arguments boilerplate from the current (global) context into t0.
4362 const int kNormalOffset =
4363 Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX);
4364 const int kAliasedOffset =
4365 Context::SlotOffset(Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX);
4366
4367 __ lw(t0, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4368 __ lw(t0, FieldMemOperand(t0, GlobalObject::kGlobalContextOffset));
4369 Label skip2_ne, skip2_eq;
4370 __ Branch(&skip2_ne, ne, a1, Operand(zero_reg));
4371 __ lw(t0, MemOperand(t0, kNormalOffset));
4372 __ bind(&skip2_ne);
4373
4374 __ Branch(&skip2_eq, eq, a1, Operand(zero_reg));
4375 __ lw(t0, MemOperand(t0, kAliasedOffset));
4376 __ bind(&skip2_eq);
4377
4378 // v0 = address of new object (tagged)
4379 // a1 = mapped parameter count (tagged)
4380 // a2 = argument count (tagged)
4381 // t0 = address of boilerplate object (tagged)
4382 // Copy the JS object part.
4383 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
4384 __ lw(a3, FieldMemOperand(t0, i));
4385 __ sw(a3, FieldMemOperand(v0, i));
4386 }
4387
4388 // Setup the callee in-object property.
4389 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
4390 __ lw(a3, MemOperand(sp, 2 * kPointerSize));
4391 const int kCalleeOffset = JSObject::kHeaderSize +
4392 Heap::kArgumentsCalleeIndex * kPointerSize;
4393 __ sw(a3, FieldMemOperand(v0, kCalleeOffset));
4394
4395 // Use the length (smi tagged) and set that as an in-object property too.
4396 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
4397 const int kLengthOffset = JSObject::kHeaderSize +
4398 Heap::kArgumentsLengthIndex * kPointerSize;
4399 __ sw(a2, FieldMemOperand(v0, kLengthOffset));
4400
4401 // Setup the elements pointer in the allocated arguments object.
4402 // If we allocated a parameter map, t0 will point there, otherwise
4403 // it will point to the backing store.
4404 __ Addu(t0, v0, Operand(Heap::kArgumentsObjectSize));
4405 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
4406
4407 // v0 = address of new object (tagged)
4408 // a1 = mapped parameter count (tagged)
4409 // a2 = argument count (tagged)
4410 // t0 = address of parameter map or backing store (tagged)
4411 // Initialize parameter map. If there are no mapped arguments, we're done.
4412 Label skip_parameter_map;
4413 Label skip3;
4414 __ Branch(&skip3, ne, a1, Operand(Smi::FromInt(0)));
4415 // Move backing store address to a3, because it is
4416 // expected there when filling in the unmapped arguments.
4417 __ mov(a3, t0);
4418 __ bind(&skip3);
4419
4420 __ Branch(&skip_parameter_map, eq, a1, Operand(Smi::FromInt(0)));
4421
4422 __ LoadRoot(t2, Heap::kNonStrictArgumentsElementsMapRootIndex);
4423 __ sw(t2, FieldMemOperand(t0, FixedArray::kMapOffset));
4424 __ Addu(t2, a1, Operand(Smi::FromInt(2)));
4425 __ sw(t2, FieldMemOperand(t0, FixedArray::kLengthOffset));
4426 __ sw(cp, FieldMemOperand(t0, FixedArray::kHeaderSize + 0 * kPointerSize));
4427 __ sll(t6, a1, 1);
4428 __ Addu(t2, t0, Operand(t6));
4429 __ Addu(t2, t2, Operand(kParameterMapHeaderSize));
4430 __ sw(t2, FieldMemOperand(t0, FixedArray::kHeaderSize + 1 * kPointerSize));
4431
4432 // Copy the parameter slots and the holes in the arguments.
4433 // We need to fill in mapped_parameter_count slots. They index the context,
4434 // where parameters are stored in reverse order, at
4435 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
4436 // The mapped parameter thus need to get indices
4437 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
4438 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
4439 // We loop from right to left.
4440 Label parameters_loop, parameters_test;
4441 __ mov(t2, a1);
4442 __ lw(t5, MemOperand(sp, 0 * kPointerSize));
4443 __ Addu(t5, t5, Operand(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
4444 __ Subu(t5, t5, Operand(a1));
4445 __ LoadRoot(t3, Heap::kTheHoleValueRootIndex);
4446 __ sll(t6, t2, 1);
4447 __ Addu(a3, t0, Operand(t6));
4448 __ Addu(a3, a3, Operand(kParameterMapHeaderSize));
4449
4450 // t2 = loop variable (tagged)
4451 // a1 = mapping index (tagged)
4452 // a3 = address of backing store (tagged)
4453 // t0 = address of parameter map (tagged)
4454 // t1 = temporary scratch (a.o., for address calculation)
4455 // t3 = the hole value
4456 __ jmp(&parameters_test);
4457
4458 __ bind(&parameters_loop);
4459 __ Subu(t2, t2, Operand(Smi::FromInt(1)));
4460 __ sll(t1, t2, 1);
4461 __ Addu(t1, t1, Operand(kParameterMapHeaderSize - kHeapObjectTag));
4462 __ Addu(t6, t0, t1);
4463 __ sw(t5, MemOperand(t6));
4464 __ Subu(t1, t1, Operand(kParameterMapHeaderSize - FixedArray::kHeaderSize));
4465 __ Addu(t6, a3, t1);
4466 __ sw(t3, MemOperand(t6));
4467 __ Addu(t5, t5, Operand(Smi::FromInt(1)));
4468 __ bind(&parameters_test);
4469 __ Branch(&parameters_loop, ne, t2, Operand(Smi::FromInt(0)));
4470
4471 __ bind(&skip_parameter_map);
4472 // a2 = argument count (tagged)
4473 // a3 = address of backing store (tagged)
4474 // t1 = scratch
4475 // Copy arguments header and remaining slots (if there are any).
4476 __ LoadRoot(t1, Heap::kFixedArrayMapRootIndex);
4477 __ sw(t1, FieldMemOperand(a3, FixedArray::kMapOffset));
4478 __ sw(a2, FieldMemOperand(a3, FixedArray::kLengthOffset));
4479
4480 Label arguments_loop, arguments_test;
4481 __ mov(t5, a1);
4482 __ lw(t0, MemOperand(sp, 1 * kPointerSize));
4483 __ sll(t6, t5, 1);
4484 __ Subu(t0, t0, Operand(t6));
4485 __ jmp(&arguments_test);
4486
4487 __ bind(&arguments_loop);
4488 __ Subu(t0, t0, Operand(kPointerSize));
4489 __ lw(t2, MemOperand(t0, 0));
4490 __ sll(t6, t5, 1);
4491 __ Addu(t1, a3, Operand(t6));
4492 __ sw(t2, FieldMemOperand(t1, FixedArray::kHeaderSize));
4493 __ Addu(t5, t5, Operand(Smi::FromInt(1)));
4494
4495 __ bind(&arguments_test);
4496 __ Branch(&arguments_loop, lt, t5, Operand(a2));
4497
4498 // Return and remove the on-stack parameters.
4499 __ Addu(sp, sp, Operand(3 * kPointerSize));
4500 __ Ret();
4501
4502 // Do the runtime call to allocate the arguments object.
4503 // a2 = argument count (taggged)
4504 __ bind(&runtime);
4505 __ sw(a2, MemOperand(sp, 0 * kPointerSize)); // Patch argument count.
4506 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
4507}
4508
4509
4510void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
4511 // sp[0] : number of parameters
4512 // sp[4] : receiver displacement
4513 // sp[8] : function
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004514 // Check if the calling frame is an arguments adaptor frame.
4515 Label adaptor_frame, try_allocate, runtime;
4516 __ lw(a2, MemOperand(fp, StandardFrameConstants::kCallerFPOffset));
4517 __ lw(a3, MemOperand(a2, StandardFrameConstants::kContextOffset));
4518 __ Branch(&adaptor_frame,
4519 eq,
4520 a3,
4521 Operand(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
4522
4523 // Get the length from the frame.
4524 __ lw(a1, MemOperand(sp, 0));
4525 __ Branch(&try_allocate);
4526
4527 // Patch the arguments.length and the parameters pointer.
4528 __ bind(&adaptor_frame);
4529 __ lw(a1, MemOperand(a2, ArgumentsAdaptorFrameConstants::kLengthOffset));
4530 __ sw(a1, MemOperand(sp, 0));
4531 __ sll(at, a1, kPointerSizeLog2 - kSmiTagSize);
4532 __ Addu(a3, a2, Operand(at));
4533
4534 __ Addu(a3, a3, Operand(StandardFrameConstants::kCallerSPOffset));
4535 __ sw(a3, MemOperand(sp, 1 * kPointerSize));
4536
4537 // Try the new space allocation. Start out with computing the size
4538 // of the arguments object and the elements array in words.
4539 Label add_arguments_object;
4540 __ bind(&try_allocate);
4541 __ Branch(&add_arguments_object, eq, a1, Operand(zero_reg));
4542 __ srl(a1, a1, kSmiTagSize);
4543
4544 __ Addu(a1, a1, Operand(FixedArray::kHeaderSize / kPointerSize));
4545 __ bind(&add_arguments_object);
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004546 __ Addu(a1, a1, Operand(Heap::kArgumentsObjectSizeStrict / kPointerSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004547
4548 // Do the allocation of both objects in one go.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004549 __ AllocateInNewSpace(a1,
4550 v0,
4551 a2,
4552 a3,
4553 &runtime,
4554 static_cast<AllocationFlags>(TAG_OBJECT |
4555 SIZE_IN_WORDS));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004556
4557 // Get the arguments boilerplate from the current (global) context.
4558 __ lw(t0, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
4559 __ lw(t0, FieldMemOperand(t0, GlobalObject::kGlobalContextOffset));
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004560 __ lw(t0, MemOperand(t0, Context::SlotOffset(
4561 Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004562
4563 // Copy the JS object part.
4564 __ CopyFields(v0, t0, a3.bit(), JSObject::kHeaderSize / kPointerSize);
4565
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004566 // Get the length (smi tagged) and set that as an in-object property too.
4567 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
4568 __ lw(a1, MemOperand(sp, 0 * kPointerSize));
4569 __ sw(a1, FieldMemOperand(v0, JSObject::kHeaderSize +
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004570 Heap::kArgumentsLengthIndex * kPointerSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004571
4572 Label done;
4573 __ Branch(&done, eq, a1, Operand(zero_reg));
4574
4575 // Get the parameters pointer from the stack.
4576 __ lw(a2, MemOperand(sp, 1 * kPointerSize));
4577
4578 // Setup the elements pointer in the allocated arguments object and
4579 // initialize the header in the elements fixed array.
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004580 __ Addu(t0, v0, Operand(Heap::kArgumentsObjectSizeStrict));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004581 __ sw(t0, FieldMemOperand(v0, JSObject::kElementsOffset));
4582 __ LoadRoot(a3, Heap::kFixedArrayMapRootIndex);
4583 __ sw(a3, FieldMemOperand(t0, FixedArray::kMapOffset));
4584 __ sw(a1, FieldMemOperand(t0, FixedArray::kLengthOffset));
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004585 // Untag the length for the loop.
4586 __ srl(a1, a1, kSmiTagSize);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004587
4588 // Copy the fixed array slots.
4589 Label loop;
4590 // Setup t0 to point to the first array slot.
4591 __ Addu(t0, t0, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
4592 __ bind(&loop);
4593 // Pre-decrement a2 with kPointerSize on each iteration.
4594 // Pre-decrement in order to skip receiver.
4595 __ Addu(a2, a2, Operand(-kPointerSize));
4596 __ lw(a3, MemOperand(a2));
4597 // Post-increment t0 with kPointerSize on each iteration.
4598 __ sw(a3, MemOperand(t0));
4599 __ Addu(t0, t0, Operand(kPointerSize));
4600 __ Subu(a1, a1, Operand(1));
4601 __ Branch(&loop, ne, a1, Operand(zero_reg));
4602
4603 // Return and remove the on-stack parameters.
4604 __ bind(&done);
4605 __ Addu(sp, sp, Operand(3 * kPointerSize));
4606 __ Ret();
4607
4608 // Do the runtime call to allocate the arguments object.
4609 __ bind(&runtime);
lrn@chromium.orgac2828d2011-06-23 06:29:21 +00004610 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00004611}
4612
4613
4614void RegExpExecStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004615 // Just jump directly to runtime if native RegExp is not selected at compile
4616 // time or if regexp entry in generated code is turned off runtime switch or
4617 // at compilation.
4618#ifdef V8_INTERPRETED_REGEXP
4619 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4620#else // V8_INTERPRETED_REGEXP
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004621
4622 // Stack frame on entry.
4623 // sp[0]: last_match_info (expected JSArray)
4624 // sp[4]: previous index
4625 // sp[8]: subject string
4626 // sp[12]: JSRegExp object
4627
4628 static const int kLastMatchInfoOffset = 0 * kPointerSize;
4629 static const int kPreviousIndexOffset = 1 * kPointerSize;
4630 static const int kSubjectOffset = 2 * kPointerSize;
4631 static const int kJSRegExpOffset = 3 * kPointerSize;
4632
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004633 Isolate* isolate = masm->isolate();
4634
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004635 Label runtime, invoke_regexp;
4636
4637 // Allocation of registers for this function. These are in callee save
4638 // registers and will be preserved by the call to the native RegExp code, as
4639 // this code is called using the normal C calling convention. When calling
4640 // directly from generated code the native RegExp code will not do a GC and
4641 // therefore the content of these registers are safe to use after the call.
4642 // MIPS - using s0..s2, since we are not using CEntry Stub.
4643 Register subject = s0;
4644 Register regexp_data = s1;
4645 Register last_match_info_elements = s2;
4646
4647 // Ensure that a RegExp stack is allocated.
4648 ExternalReference address_of_regexp_stack_memory_address =
4649 ExternalReference::address_of_regexp_stack_memory_address(
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004650 isolate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004651 ExternalReference address_of_regexp_stack_memory_size =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004652 ExternalReference::address_of_regexp_stack_memory_size(isolate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004653 __ li(a0, Operand(address_of_regexp_stack_memory_size));
4654 __ lw(a0, MemOperand(a0, 0));
4655 __ Branch(&runtime, eq, a0, Operand(zero_reg));
4656
4657 // Check that the first argument is a JSRegExp object.
4658 __ lw(a0, MemOperand(sp, kJSRegExpOffset));
4659 STATIC_ASSERT(kSmiTag == 0);
4660 __ JumpIfSmi(a0, &runtime);
4661 __ GetObjectType(a0, a1, a1);
4662 __ Branch(&runtime, ne, a1, Operand(JS_REGEXP_TYPE));
4663
4664 // Check that the RegExp has been compiled (data contains a fixed array).
4665 __ lw(regexp_data, FieldMemOperand(a0, JSRegExp::kDataOffset));
4666 if (FLAG_debug_code) {
4667 __ And(t0, regexp_data, Operand(kSmiTagMask));
4668 __ Check(nz,
4669 "Unexpected type for RegExp data, FixedArray expected",
4670 t0,
4671 Operand(zero_reg));
4672 __ GetObjectType(regexp_data, a0, a0);
4673 __ Check(eq,
4674 "Unexpected type for RegExp data, FixedArray expected",
4675 a0,
4676 Operand(FIXED_ARRAY_TYPE));
4677 }
4678
4679 // regexp_data: RegExp data (FixedArray)
4680 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
4681 __ lw(a0, FieldMemOperand(regexp_data, JSRegExp::kDataTagOffset));
4682 __ Branch(&runtime, ne, a0, Operand(Smi::FromInt(JSRegExp::IRREGEXP)));
4683
4684 // regexp_data: RegExp data (FixedArray)
4685 // Check that the number of captures fit in the static offsets vector buffer.
4686 __ lw(a2,
4687 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4688 // Calculate number of capture registers (number_of_captures + 1) * 2. This
4689 // uses the asumption that smis are 2 * their untagged value.
4690 STATIC_ASSERT(kSmiTag == 0);
4691 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4692 __ Addu(a2, a2, Operand(2)); // a2 was a smi.
4693 // Check that the static offsets vector buffer is large enough.
4694 __ Branch(&runtime, hi, a2, Operand(OffsetsVector::kStaticOffsetsVectorSize));
4695
4696 // a2: Number of capture registers
4697 // regexp_data: RegExp data (FixedArray)
4698 // Check that the second argument is a string.
4699 __ lw(subject, MemOperand(sp, kSubjectOffset));
4700 __ JumpIfSmi(subject, &runtime);
4701 __ GetObjectType(subject, a0, a0);
4702 __ And(a0, a0, Operand(kIsNotStringMask));
4703 STATIC_ASSERT(kStringTag == 0);
4704 __ Branch(&runtime, ne, a0, Operand(zero_reg));
4705
4706 // Get the length of the string to r3.
4707 __ lw(a3, FieldMemOperand(subject, String::kLengthOffset));
4708
4709 // a2: Number of capture registers
4710 // a3: Length of subject string as a smi
4711 // subject: Subject string
4712 // regexp_data: RegExp data (FixedArray)
4713 // Check that the third argument is a positive smi less than the subject
4714 // string length. A negative value will be greater (unsigned comparison).
4715 __ lw(a0, MemOperand(sp, kPreviousIndexOffset));
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00004716 __ JumpIfNotSmi(a0, &runtime);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004717 __ Branch(&runtime, ls, a3, Operand(a0));
4718
4719 // a2: Number of capture registers
4720 // subject: Subject string
4721 // regexp_data: RegExp data (FixedArray)
4722 // Check that the fourth object is a JSArray object.
4723 __ lw(a0, MemOperand(sp, kLastMatchInfoOffset));
4724 __ JumpIfSmi(a0, &runtime);
4725 __ GetObjectType(a0, a1, a1);
4726 __ Branch(&runtime, ne, a1, Operand(JS_ARRAY_TYPE));
4727 // Check that the JSArray is in fast case.
4728 __ lw(last_match_info_elements,
4729 FieldMemOperand(a0, JSArray::kElementsOffset));
4730 __ lw(a0, FieldMemOperand(last_match_info_elements, HeapObject::kMapOffset));
4731 __ Branch(&runtime, ne, a0, Operand(
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004732 isolate->factory()->fixed_array_map()));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004733 // Check that the last match info has space for the capture registers and the
4734 // additional information.
4735 __ lw(a0,
4736 FieldMemOperand(last_match_info_elements, FixedArray::kLengthOffset));
4737 __ Addu(a2, a2, Operand(RegExpImpl::kLastMatchOverhead));
4738 __ sra(at, a0, kSmiTagSize); // Untag length for comparison.
4739 __ Branch(&runtime, gt, a2, Operand(at));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004740
4741 // Reset offset for possibly sliced string.
4742 __ mov(t0, zero_reg);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004743 // subject: Subject string
4744 // regexp_data: RegExp data (FixedArray)
4745 // Check the representation and encoding of the subject string.
4746 Label seq_string;
4747 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
4748 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
4749 // First check for flat string.
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00004750 __ And(a1, a0, Operand(kIsNotStringMask | kStringRepresentationMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004751 STATIC_ASSERT((kStringTag | kSeqStringTag) == 0);
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00004752 __ Branch(&seq_string, eq, a1, Operand(zero_reg));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004753
4754 // subject: Subject string
4755 // a0: instance type if Subject string
4756 // regexp_data: RegExp data (FixedArray)
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004757 // Check for flat cons string or sliced string.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004758 // A flat cons string is a cons string where the second part is the empty
4759 // string. In that case the subject string is just the first part of the cons
4760 // string. Also in this case the first part of the cons string is known to be
4761 // a sequential string or an external string.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004762 // In the case of a sliced string its offset has to be taken into account.
4763 Label cons_string, check_encoding;
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00004764 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
4765 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
4766 __ Branch(&cons_string, lt, a1, Operand(kExternalStringTag));
4767 __ Branch(&runtime, eq, a1, Operand(kExternalStringTag));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004768
4769 // String is sliced.
4770 __ lw(t0, FieldMemOperand(subject, SlicedString::kOffsetOffset));
4771 __ sra(t0, t0, kSmiTagSize);
4772 __ lw(subject, FieldMemOperand(subject, SlicedString::kParentOffset));
4773 // t5: offset of sliced string, smi-tagged.
4774 __ jmp(&check_encoding);
4775 // String is a cons string, check whether it is flat.
4776 __ bind(&cons_string);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004777 __ lw(a0, FieldMemOperand(subject, ConsString::kSecondOffset));
4778 __ LoadRoot(a1, Heap::kEmptyStringRootIndex);
4779 __ Branch(&runtime, ne, a0, Operand(a1));
4780 __ lw(subject, FieldMemOperand(subject, ConsString::kFirstOffset));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004781 // Is first part of cons or parent of slice a flat string?
4782 __ bind(&check_encoding);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004783 __ lw(a0, FieldMemOperand(subject, HeapObject::kMapOffset));
4784 __ lbu(a0, FieldMemOperand(a0, Map::kInstanceTypeOffset));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004785 STATIC_ASSERT(kSeqStringTag == 0);
4786 __ And(at, a0, Operand(kStringRepresentationMask));
4787 __ Branch(&runtime, ne, at, Operand(zero_reg));
4788
4789 __ bind(&seq_string);
4790 // subject: Subject string
4791 // regexp_data: RegExp data (FixedArray)
4792 // a0: Instance type of subject string
4793 STATIC_ASSERT(kStringEncodingMask == 4);
4794 STATIC_ASSERT(kAsciiStringTag == 4);
4795 STATIC_ASSERT(kTwoByteStringTag == 0);
4796 // Find the code object based on the assumptions above.
4797 __ And(a0, a0, Operand(kStringEncodingMask)); // Non-zero for ascii.
4798 __ lw(t9, FieldMemOperand(regexp_data, JSRegExp::kDataAsciiCodeOffset));
4799 __ sra(a3, a0, 2); // a3 is 1 for ascii, 0 for UC16 (usyed below).
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004800 __ lw(t1, FieldMemOperand(regexp_data, JSRegExp::kDataUC16CodeOffset));
4801 __ movz(t9, t1, a0); // If UC16 (a0 is 0), replace t9 w/kDataUC16CodeOffset.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004802
4803 // Check that the irregexp code has been generated for the actual string
sgjesse@chromium.org6db88712011-07-11 11:41:22 +00004804 // encoding. If it has, the field contains a code object otherwise it contains
4805 // a smi (code flushing support).
4806 __ JumpIfSmi(t9, &runtime);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004807
4808 // a3: encoding of subject string (1 if ASCII, 0 if two_byte);
4809 // t9: code
4810 // subject: Subject string
4811 // regexp_data: RegExp data (FixedArray)
4812 // Load used arguments before starting to push arguments for call to native
4813 // RegExp code to avoid handling changing stack height.
4814 __ lw(a1, MemOperand(sp, kPreviousIndexOffset));
4815 __ sra(a1, a1, kSmiTagSize); // Untag the Smi.
4816
4817 // a1: previous index
4818 // a3: encoding of subject string (1 if ASCII, 0 if two_byte);
4819 // t9: code
4820 // subject: Subject string
4821 // regexp_data: RegExp data (FixedArray)
4822 // All checks done. Now push arguments for native regexp code.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004823 __ IncrementCounter(isolate->counters()->regexp_entry_native(),
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004824 1, a0, a2);
4825
4826 // Isolates: note we add an additional parameter here (isolate pointer).
4827 static const int kRegExpExecuteArguments = 8;
4828 static const int kParameterRegisters = 4;
4829 __ EnterExitFrame(false, kRegExpExecuteArguments - kParameterRegisters);
4830
4831 // Stack pointer now points to cell where return address is to be written.
4832 // Arguments are before that on the stack or in registers, meaning we
4833 // treat the return address as argument 5. Thus every argument after that
4834 // needs to be shifted back by 1. Since DirectCEntryStub will handle
4835 // allocating space for the c argument slots, we don't need to calculate
4836 // that into the argument positions on the stack. This is how the stack will
4837 // look (sp meaning the value of sp at this moment):
4838 // [sp + 4] - Argument 8
4839 // [sp + 3] - Argument 7
4840 // [sp + 2] - Argument 6
4841 // [sp + 1] - Argument 5
4842 // [sp + 0] - saved ra
4843
4844 // Argument 8: Pass current isolate address.
4845 // CFunctionArgumentOperand handles MIPS stack argument slots.
4846 __ li(a0, Operand(ExternalReference::isolate_address()));
4847 __ sw(a0, MemOperand(sp, 4 * kPointerSize));
4848
4849 // Argument 7: Indicate that this is a direct call from JavaScript.
4850 __ li(a0, Operand(1));
4851 __ sw(a0, MemOperand(sp, 3 * kPointerSize));
4852
4853 // Argument 6: Start (high end) of backtracking stack memory area.
4854 __ li(a0, Operand(address_of_regexp_stack_memory_address));
4855 __ lw(a0, MemOperand(a0, 0));
4856 __ li(a2, Operand(address_of_regexp_stack_memory_size));
4857 __ lw(a2, MemOperand(a2, 0));
4858 __ addu(a0, a0, a2);
4859 __ sw(a0, MemOperand(sp, 2 * kPointerSize));
4860
4861 // Argument 5: static offsets vector buffer.
4862 __ li(a0, Operand(
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004863 ExternalReference::address_of_static_offsets_vector(isolate)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004864 __ sw(a0, MemOperand(sp, 1 * kPointerSize));
4865
4866 // For arguments 4 and 3 get string length, calculate start of string data
4867 // and calculate the shift of the index (0 for ASCII and 1 for two byte).
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00004868 __ Addu(t2, subject, Operand(SeqString::kHeaderSize - kHeapObjectTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004869 __ Xor(a3, a3, Operand(1)); // 1 for 2-byte str, 0 for 1-byte.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004870 // Load the length from the original subject string from the previous stack
4871 // frame. Therefore we have to use fp, which points exactly to two pointer
4872 // sizes below the previous sp. (Because creating a new stack frame pushes
4873 // the previous fp onto the stack and moves up sp by 2 * kPointerSize.)
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004874 __ lw(subject, MemOperand(fp, kSubjectOffset + 2 * kPointerSize));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004875 // If slice offset is not 0, load the length from the original sliced string.
4876 // Argument 4, a3: End of string data
4877 // Argument 3, a2: Start of string data
4878 // Prepare start and end index of the input.
4879 __ sllv(t1, t0, a3);
4880 __ addu(t0, t2, t1);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004881 __ sllv(t1, a1, a3);
4882 __ addu(a2, t0, t1);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004883
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004884 __ lw(t2, FieldMemOperand(subject, String::kLengthOffset));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004885 __ sra(t2, t2, kSmiTagSize);
4886 __ sllv(t1, t2, a3);
4887 __ addu(a3, t0, t1);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004888 // Argument 2 (a1): Previous index.
4889 // Already there
4890
4891 // Argument 1 (a0): Subject string.
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004892 __ mov(a0, subject);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004893
4894 // Locate the code entry and call it.
4895 __ Addu(t9, t9, Operand(Code::kHeaderSize - kHeapObjectTag));
4896 DirectCEntryStub stub;
4897 stub.GenerateCall(masm, t9);
4898
4899 __ LeaveExitFrame(false, no_reg);
4900
4901 // v0: result
4902 // subject: subject string (callee saved)
4903 // regexp_data: RegExp data (callee saved)
4904 // last_match_info_elements: Last match info elements (callee saved)
4905
4906 // Check the result.
4907
4908 Label success;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004909 __ Branch(&success, eq,
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004910 v0, Operand(NativeRegExpMacroAssembler::SUCCESS));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004911 Label failure;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004912 __ Branch(&failure, eq,
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004913 v0, Operand(NativeRegExpMacroAssembler::FAILURE));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004914 // If not exception it can only be retry. Handle that in the runtime system.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00004915 __ Branch(&runtime, ne,
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004916 v0, Operand(NativeRegExpMacroAssembler::EXCEPTION));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004917 // Result must now be exception. If there is no pending exception already a
4918 // stack overflow (on the backtrack stack) was detected in RegExp code but
4919 // haven't created the exception yet. Handle that in the runtime system.
4920 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004921 __ li(a1, Operand(isolate->factory()->the_hole_value()));
kmillikin@chromium.org83e16822011-09-13 08:21:47 +00004922 __ li(a2, Operand(ExternalReference(Isolate::kPendingExceptionAddress,
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004923 isolate)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004924 __ lw(v0, MemOperand(a2, 0));
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004925 __ Branch(&runtime, eq, v0, Operand(a1));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004926
4927 __ sw(a1, MemOperand(a2, 0)); // Clear pending exception.
4928
4929 // Check if the exception is a termination. If so, throw as uncatchable.
4930 __ LoadRoot(a0, Heap::kTerminationExceptionRootIndex);
4931 Label termination_exception;
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004932 __ Branch(&termination_exception, eq, v0, Operand(a0));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004933
jkummerow@chromium.org486075a2011-09-07 12:44:28 +00004934 __ Throw(v0); // Expects thrown value in v0.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004935
4936 __ bind(&termination_exception);
4937 __ ThrowUncatchable(TERMINATION, v0); // Expects thrown value in v0.
4938
4939 __ bind(&failure);
4940 // For failure and exception return null.
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004941 __ li(v0, Operand(isolate->factory()->null_value()));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004942 __ Addu(sp, sp, Operand(4 * kPointerSize));
4943 __ Ret();
4944
4945 // Process the result from the native regexp code.
4946 __ bind(&success);
4947 __ lw(a1,
4948 FieldMemOperand(regexp_data, JSRegExp::kIrregexpCaptureCountOffset));
4949 // Calculate number of capture registers (number_of_captures + 1) * 2.
4950 STATIC_ASSERT(kSmiTag == 0);
4951 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
4952 __ Addu(a1, a1, Operand(2)); // a1 was a smi.
4953
4954 // a1: number of capture registers
4955 // subject: subject string
4956 // Store the capture count.
4957 __ sll(a2, a1, kSmiTagSize + kSmiShiftSize); // To smi.
4958 __ sw(a2, FieldMemOperand(last_match_info_elements,
4959 RegExpImpl::kLastCaptureCountOffset));
4960 // Store last subject and last input.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004961 __ sw(subject,
4962 FieldMemOperand(last_match_info_elements,
4963 RegExpImpl::kLastSubjectOffset));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004964 __ mov(a2, subject);
4965 __ RecordWriteField(last_match_info_elements,
4966 RegExpImpl::kLastSubjectOffset,
4967 a2,
4968 t3,
4969 kRAHasNotBeenSaved,
4970 kDontSaveFPRegs);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004971 __ sw(subject,
4972 FieldMemOperand(last_match_info_elements,
4973 RegExpImpl::kLastInputOffset));
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004974 __ RecordWriteField(last_match_info_elements,
4975 RegExpImpl::kLastInputOffset,
4976 subject,
4977 t3,
4978 kRAHasNotBeenSaved,
4979 kDontSaveFPRegs);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004980
4981 // Get the static offsets vector filled by the native regexp code.
4982 ExternalReference address_of_static_offsets_vector =
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00004983 ExternalReference::address_of_static_offsets_vector(isolate);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00004984 __ li(a2, Operand(address_of_static_offsets_vector));
4985
4986 // a1: number of capture registers
4987 // a2: offsets vector
4988 Label next_capture, done;
4989 // Capture register counter starts from number of capture registers and
4990 // counts down until wrapping after zero.
4991 __ Addu(a0,
4992 last_match_info_elements,
4993 Operand(RegExpImpl::kFirstCaptureOffset - kHeapObjectTag));
4994 __ bind(&next_capture);
4995 __ Subu(a1, a1, Operand(1));
4996 __ Branch(&done, lt, a1, Operand(zero_reg));
4997 // Read the value from the static offsets vector buffer.
4998 __ lw(a3, MemOperand(a2, 0));
4999 __ addiu(a2, a2, kPointerSize);
5000 // Store the smi value in the last match info.
5001 __ sll(a3, a3, kSmiTagSize); // Convert to Smi.
5002 __ sw(a3, MemOperand(a0, 0));
5003 __ Branch(&next_capture, USE_DELAY_SLOT);
5004 __ addiu(a0, a0, kPointerSize); // In branch delay slot.
5005
5006 __ bind(&done);
5007
5008 // Return last match info.
5009 __ lw(v0, MemOperand(sp, kLastMatchInfoOffset));
5010 __ Addu(sp, sp, Operand(4 * kPointerSize));
5011 __ Ret();
5012
5013 // Do the runtime call to execute the regexp.
5014 __ bind(&runtime);
5015 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
5016#endif // V8_INTERPRETED_REGEXP
lrn@chromium.org7516f052011-03-30 08:52:27 +00005017}
5018
5019
5020void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005021 const int kMaxInlineLength = 100;
5022 Label slowcase;
5023 Label done;
5024 __ lw(a1, MemOperand(sp, kPointerSize * 2));
5025 STATIC_ASSERT(kSmiTag == 0);
5026 STATIC_ASSERT(kSmiTagSize == 1);
5027 __ JumpIfNotSmi(a1, &slowcase);
5028 __ Branch(&slowcase, hi, a1, Operand(Smi::FromInt(kMaxInlineLength)));
5029 // Smi-tagging is equivalent to multiplying by 2.
5030 // Allocate RegExpResult followed by FixedArray with size in ebx.
5031 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
5032 // Elements: [Map][Length][..elements..]
5033 // Size of JSArray with two in-object properties and the header of a
5034 // FixedArray.
5035 int objects_size =
5036 (JSRegExpResult::kSize + FixedArray::kHeaderSize) / kPointerSize;
5037 __ srl(t1, a1, kSmiTagSize + kSmiShiftSize);
5038 __ Addu(a2, t1, Operand(objects_size));
5039 __ AllocateInNewSpace(
5040 a2, // In: Size, in words.
5041 v0, // Out: Start of allocation (tagged).
5042 a3, // Scratch register.
5043 t0, // Scratch register.
5044 &slowcase,
5045 static_cast<AllocationFlags>(TAG_OBJECT | SIZE_IN_WORDS));
5046 // v0: Start of allocated area, object-tagged.
5047 // a1: Number of elements in array, as smi.
5048 // t1: Number of elements, untagged.
5049
5050 // Set JSArray map to global.regexp_result_map().
5051 // Set empty properties FixedArray.
5052 // Set elements to point to FixedArray allocated right after the JSArray.
5053 // Interleave operations for better latency.
5054 __ lw(a2, ContextOperand(cp, Context::GLOBAL_INDEX));
5055 __ Addu(a3, v0, Operand(JSRegExpResult::kSize));
5056 __ li(t0, Operand(masm->isolate()->factory()->empty_fixed_array()));
5057 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalContextOffset));
5058 __ sw(a3, FieldMemOperand(v0, JSObject::kElementsOffset));
5059 __ lw(a2, ContextOperand(a2, Context::REGEXP_RESULT_MAP_INDEX));
5060 __ sw(t0, FieldMemOperand(v0, JSObject::kPropertiesOffset));
5061 __ sw(a2, FieldMemOperand(v0, HeapObject::kMapOffset));
5062
5063 // Set input, index and length fields from arguments.
5064 __ lw(a1, MemOperand(sp, kPointerSize * 0));
5065 __ sw(a1, FieldMemOperand(v0, JSRegExpResult::kInputOffset));
5066 __ lw(a1, MemOperand(sp, kPointerSize * 1));
5067 __ sw(a1, FieldMemOperand(v0, JSRegExpResult::kIndexOffset));
5068 __ lw(a1, MemOperand(sp, kPointerSize * 2));
5069 __ sw(a1, FieldMemOperand(v0, JSArray::kLengthOffset));
5070
5071 // Fill out the elements FixedArray.
5072 // v0: JSArray, tagged.
5073 // a3: FixedArray, tagged.
5074 // t1: Number of elements in array, untagged.
5075
5076 // Set map.
5077 __ li(a2, Operand(masm->isolate()->factory()->fixed_array_map()));
5078 __ sw(a2, FieldMemOperand(a3, HeapObject::kMapOffset));
5079 // Set FixedArray length.
5080 __ sll(t2, t1, kSmiTagSize);
5081 __ sw(t2, FieldMemOperand(a3, FixedArray::kLengthOffset));
5082 // Fill contents of fixed-array with the-hole.
5083 __ li(a2, Operand(masm->isolate()->factory()->the_hole_value()));
5084 __ Addu(a3, a3, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
5085 // Fill fixed array elements with hole.
5086 // v0: JSArray, tagged.
5087 // a2: the hole.
5088 // a3: Start of elements in FixedArray.
5089 // t1: Number of elements to fill.
5090 Label loop;
5091 __ sll(t1, t1, kPointerSizeLog2); // Convert num elements to num bytes.
5092 __ addu(t1, t1, a3); // Point past last element to store.
5093 __ bind(&loop);
5094 __ Branch(&done, ge, a3, Operand(t1)); // Break when a3 past end of elem.
5095 __ sw(a2, MemOperand(a3));
5096 __ Branch(&loop, USE_DELAY_SLOT);
5097 __ addiu(a3, a3, kPointerSize); // In branch delay slot.
5098
5099 __ bind(&done);
5100 __ Addu(sp, sp, Operand(3 * kPointerSize));
5101 __ Ret();
5102
5103 __ bind(&slowcase);
5104 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005105}
5106
5107
mstarzinger@chromium.orgf8c6bd52011-11-23 12:13:52 +00005108void CallFunctionStub::FinishCode(Handle<Code> code) {
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00005109 code->set_has_function_cache(false);
5110}
5111
5112
5113void CallFunctionStub::Clear(Heap* heap, Address address) {
5114 UNREACHABLE();
5115}
5116
5117
5118Object* CallFunctionStub::GetCachedValue(Address address) {
5119 UNREACHABLE();
5120 return NULL;
5121}
5122
5123
lrn@chromium.org7516f052011-03-30 08:52:27 +00005124void CallFunctionStub::Generate(MacroAssembler* masm) {
danno@chromium.orgc612e022011-11-10 11:38:15 +00005125 // a1 : the function to call
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00005126 Label slow, non_function;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005127
danno@chromium.org40cb8782011-05-25 07:58:50 +00005128 // The receiver might implicitly be the global object. This is
5129 // indicated by passing the hole as the receiver to the call
5130 // function stub.
5131 if (ReceiverMightBeImplicit()) {
5132 Label call;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005133 // Get the receiver from the stack.
5134 // function, receiver [, arguments]
danno@chromium.org40cb8782011-05-25 07:58:50 +00005135 __ lw(t0, MemOperand(sp, argc_ * kPointerSize));
5136 // Call as function is indicated with the hole.
5137 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
5138 __ Branch(&call, ne, t0, Operand(at));
5139 // Patch the receiver on the stack with the global receiver object.
danno@chromium.orgc612e022011-11-10 11:38:15 +00005140 __ lw(a2, MemOperand(cp, Context::SlotOffset(Context::GLOBAL_INDEX)));
5141 __ lw(a2, FieldMemOperand(a2, GlobalObject::kGlobalReceiverOffset));
5142 __ sw(a2, MemOperand(sp, argc_ * kPointerSize));
danno@chromium.org40cb8782011-05-25 07:58:50 +00005143 __ bind(&call);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005144 }
5145
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005146 // Check that the function is really a JavaScript function.
5147 // a1: pushed function (to be verified)
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00005148 __ JumpIfSmi(a1, &non_function);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005149 // Get the map of the function object.
5150 __ GetObjectType(a1, a2, a2);
5151 __ Branch(&slow, ne, a2, Operand(JS_FUNCTION_TYPE));
5152
5153 // Fast-case: Invoke the function now.
5154 // a1: pushed function
5155 ParameterCount actual(argc_);
danno@chromium.org40cb8782011-05-25 07:58:50 +00005156
5157 if (ReceiverMightBeImplicit()) {
5158 Label call_as_function;
5159 __ LoadRoot(at, Heap::kTheHoleValueRootIndex);
5160 __ Branch(&call_as_function, eq, t0, Operand(at));
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00005161 __ InvokeFunction(a1,
5162 actual,
5163 JUMP_FUNCTION,
5164 NullCallWrapper(),
5165 CALL_AS_METHOD);
danno@chromium.org40cb8782011-05-25 07:58:50 +00005166 __ bind(&call_as_function);
5167 }
5168 __ InvokeFunction(a1,
5169 actual,
5170 JUMP_FUNCTION,
5171 NullCallWrapper(),
5172 CALL_AS_FUNCTION);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005173
5174 // Slow-case: Non-function called.
5175 __ bind(&slow);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00005176 // Check for function proxy.
5177 __ Branch(&non_function, ne, a2, Operand(JS_FUNCTION_PROXY_TYPE));
5178 __ push(a1); // Put proxy as additional argument.
5179 __ li(a0, Operand(argc_ + 1, RelocInfo::NONE));
5180 __ li(a2, Operand(0, RelocInfo::NONE));
5181 __ GetBuiltinEntry(a3, Builtins::CALL_FUNCTION_PROXY);
danno@chromium.orgc612e022011-11-10 11:38:15 +00005182 __ SetCallKind(t1, CALL_AS_METHOD);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00005183 {
5184 Handle<Code> adaptor =
5185 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
5186 __ Jump(adaptor, RelocInfo::CODE_TARGET);
5187 }
5188
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005189 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
5190 // of the original receiver from the call site).
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00005191 __ bind(&non_function);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005192 __ sw(a1, MemOperand(sp, argc_ * kPointerSize));
5193 __ li(a0, Operand(argc_)); // Setup the number of arguments.
5194 __ mov(a2, zero_reg);
5195 __ GetBuiltinEntry(a3, Builtins::CALL_NON_FUNCTION);
rossberg@chromium.org717967f2011-07-20 13:44:42 +00005196 __ SetCallKind(t1, CALL_AS_METHOD);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005197 __ Jump(masm->isolate()->builtins()->ArgumentsAdaptorTrampoline(),
5198 RelocInfo::CODE_TARGET);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005199}
5200
5201
5202// Unfortunately you have to run without snapshots to see most of these
5203// names in the profile since most compare stubs end up in the snapshot.
whesse@chromium.org030d38e2011-07-13 13:23:34 +00005204void CompareStub::PrintName(StringStream* stream) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005205 ASSERT((lhs_.is(a0) && rhs_.is(a1)) ||
5206 (lhs_.is(a1) && rhs_.is(a0)));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005207 const char* cc_name;
5208 switch (cc_) {
5209 case lt: cc_name = "LT"; break;
5210 case gt: cc_name = "GT"; break;
5211 case le: cc_name = "LE"; break;
5212 case ge: cc_name = "GE"; break;
5213 case eq: cc_name = "EQ"; break;
5214 case ne: cc_name = "NE"; break;
5215 default: cc_name = "UnknownCondition"; break;
5216 }
whesse@chromium.org030d38e2011-07-13 13:23:34 +00005217 bool is_equality = cc_ == eq || cc_ == ne;
5218 stream->Add("CompareStub_%s", cc_name);
5219 stream->Add(lhs_.is(a0) ? "_a0" : "_a1");
5220 stream->Add(rhs_.is(a0) ? "_a0" : "_a1");
5221 if (strict_ && is_equality) stream->Add("_STRICT");
5222 if (never_nan_nan_ && is_equality) stream->Add("_NO_NAN");
5223 if (!include_number_compare_) stream->Add("_NO_NUMBER");
5224 if (!include_smi_compare_) stream->Add("_NO_SMI");
lrn@chromium.org7516f052011-03-30 08:52:27 +00005225}
5226
5227
5228int CompareStub::MinorKey() {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005229 // Encode the two parameters in a unique 16 bit value.
5230 ASSERT(static_cast<unsigned>(cc_) < (1 << 14));
5231 ASSERT((lhs_.is(a0) && rhs_.is(a1)) ||
5232 (lhs_.is(a1) && rhs_.is(a0)));
5233 return ConditionField::encode(static_cast<unsigned>(cc_))
5234 | RegisterField::encode(lhs_.is(a0))
5235 | StrictField::encode(strict_)
5236 | NeverNanNanField::encode(cc_ == eq ? never_nan_nan_ : false)
5237 | IncludeSmiCompareField::encode(include_smi_compare_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005238}
5239
5240
karlklose@chromium.org83a47282011-05-11 11:54:09 +00005241// StringCharCodeAtGenerator.
lrn@chromium.org7516f052011-03-30 08:52:27 +00005242void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005243 Label flat_string;
5244 Label ascii_string;
5245 Label got_char_code;
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00005246 Label sliced_string;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005247
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005248 ASSERT(!t0.is(index_));
5249 ASSERT(!t0.is(result_));
5250 ASSERT(!t0.is(object_));
5251
5252 // If the receiver is a smi trigger the non-string case.
5253 __ JumpIfSmi(object_, receiver_not_string_);
5254
5255 // Fetch the instance type of the receiver into result register.
5256 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5257 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
5258 // If the receiver is not a string trigger the non-string case.
5259 __ And(t0, result_, Operand(kIsNotStringMask));
5260 __ Branch(receiver_not_string_, ne, t0, Operand(zero_reg));
5261
5262 // If the index is non-smi trigger the non-smi case.
5263 __ JumpIfNotSmi(index_, &index_not_smi_);
5264
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005265 __ bind(&got_smi_index_);
5266
5267 // Check for index out of range.
5268 __ lw(t0, FieldMemOperand(object_, String::kLengthOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00005269 __ Branch(index_out_of_range_, ls, t0, Operand(index_));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005270
5271 // We need special handling for non-flat strings.
5272 STATIC_ASSERT(kSeqStringTag == 0);
5273 __ And(t0, result_, Operand(kStringRepresentationMask));
5274 __ Branch(&flat_string, eq, t0, Operand(zero_reg));
5275
5276 // Handle non-flat strings.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00005277 __ And(result_, result_, Operand(kStringRepresentationMask));
yangguo@chromium.org80c42ed2011-08-31 09:03:56 +00005278 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
5279 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00005280 __ Branch(&sliced_string, gt, result_, Operand(kExternalStringTag));
5281 __ Branch(&call_runtime_, eq, result_, Operand(kExternalStringTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005282
5283 // ConsString.
5284 // Check whether the right hand side is the empty string (i.e. if
5285 // this is really a flat string in a cons string). If that is not
5286 // the case we would rather go to the runtime system now to flatten
5287 // the string.
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00005288 Label assure_seq_string;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005289 __ lw(result_, FieldMemOperand(object_, ConsString::kSecondOffset));
5290 __ LoadRoot(t0, Heap::kEmptyStringRootIndex);
5291 __ Branch(&call_runtime_, ne, result_, Operand(t0));
5292
danno@chromium.orgc612e022011-11-10 11:38:15 +00005293 // Get the first of the two parts.
5294 __ lw(object_, FieldMemOperand(object_, ConsString::kFirstOffset));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00005295 __ jmp(&assure_seq_string);
5296
5297 // SlicedString, unpack and add offset.
5298 __ bind(&sliced_string);
5299 __ lw(result_, FieldMemOperand(object_, SlicedString::kOffsetOffset));
danno@chromium.orgc612e022011-11-10 11:38:15 +00005300 __ Addu(index_, index_, result_);
5301 __ lw(object_, FieldMemOperand(object_, SlicedString::kParentOffset));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00005302
5303 // Assure that we are dealing with a sequential string. Go to runtime if not.
5304 __ bind(&assure_seq_string);
danno@chromium.orgc612e022011-11-10 11:38:15 +00005305 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005306 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
ricow@chromium.org4668a2c2011-08-29 10:41:00 +00005307 // Check that parent is not an external string. Go to runtime otherwise.
danno@chromium.orgc612e022011-11-10 11:38:15 +00005308 // Note that if the original string is a cons or slice with an external
5309 // string as underlying string, we pass that unpacked underlying string with
5310 // the adjusted index to the runtime function.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005311 STATIC_ASSERT(kSeqStringTag == 0);
5312
5313 __ And(t0, result_, Operand(kStringRepresentationMask));
5314 __ Branch(&call_runtime_, ne, t0, Operand(zero_reg));
5315
5316 // Check for 1-byte or 2-byte string.
5317 __ bind(&flat_string);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005318 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
5319 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005320 __ And(t0, result_, Operand(kStringEncodingMask));
5321 __ Branch(&ascii_string, ne, t0, Operand(zero_reg));
5322
5323 // 2-byte string.
5324 // Load the 2-byte character code into the result register. We can
5325 // add without shifting since the smi tag size is the log2 of the
5326 // number of bytes in a two-byte character.
5327 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1 && kSmiShiftSize == 0);
danno@chromium.orgc612e022011-11-10 11:38:15 +00005328 __ Addu(index_, object_, Operand(index_));
5329 __ lhu(result_, FieldMemOperand(index_, SeqTwoByteString::kHeaderSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005330 __ Branch(&got_char_code);
5331
5332 // ASCII string.
5333 // Load the byte into the result register.
5334 __ bind(&ascii_string);
5335
danno@chromium.orgc612e022011-11-10 11:38:15 +00005336 __ srl(t0, index_, kSmiTagSize);
5337 __ Addu(index_, object_, t0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005338
danno@chromium.orgc612e022011-11-10 11:38:15 +00005339 __ lbu(result_, FieldMemOperand(index_, SeqAsciiString::kHeaderSize));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005340
5341 __ bind(&got_char_code);
5342 __ sll(result_, result_, kSmiTagSize);
5343 __ bind(&exit_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005344}
5345
5346
5347void StringCharCodeAtGenerator::GenerateSlow(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00005348 MacroAssembler* masm,
5349 const RuntimeCallHelper& call_helper) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005350 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
5351
5352 // Index is not a smi.
5353 __ bind(&index_not_smi_);
5354 // If index is a heap number, try converting it to an integer.
5355 __ CheckMap(index_,
danno@chromium.orgc612e022011-11-10 11:38:15 +00005356 result_,
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005357 Heap::kHeapNumberMapRootIndex,
5358 index_not_number_,
danno@chromium.org40cb8782011-05-25 07:58:50 +00005359 DONT_DO_SMI_CHECK);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005360 call_helper.BeforeCall(masm);
5361 // Consumed by runtime conversion function:
danno@chromium.orgc612e022011-11-10 11:38:15 +00005362 __ Push(object_, index_);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005363 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
5364 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
5365 } else {
5366 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
5367 // NumberToSmi discards numbers that are not exact integers.
5368 __ CallRuntime(Runtime::kNumberToSmi, 1);
5369 }
5370
5371 // Save the conversion result before the pop instructions below
5372 // have a chance to overwrite it.
5373
danno@chromium.orgc612e022011-11-10 11:38:15 +00005374 __ Move(index_, v0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005375 __ pop(object_);
5376 // Reload the instance type.
5377 __ lw(result_, FieldMemOperand(object_, HeapObject::kMapOffset));
5378 __ lbu(result_, FieldMemOperand(result_, Map::kInstanceTypeOffset));
5379 call_helper.AfterCall(masm);
5380 // If index is still not a smi, it must be out of range.
danno@chromium.orgc612e022011-11-10 11:38:15 +00005381 __ JumpIfNotSmi(index_, index_out_of_range_);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005382 // Otherwise, return to the fast path.
5383 __ Branch(&got_smi_index_);
5384
5385 // Call runtime. We get here when the receiver is a string and the
5386 // index is a number, but the code of getting the actual character
5387 // is too complex (e.g., when the string needs to be flattened).
5388 __ bind(&call_runtime_);
5389 call_helper.BeforeCall(masm);
5390 __ Push(object_, index_);
5391 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
5392
5393 __ Move(result_, v0);
5394
5395 call_helper.AfterCall(masm);
5396 __ jmp(&exit_);
5397
5398 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
lrn@chromium.org7516f052011-03-30 08:52:27 +00005399}
5400
5401
5402// -------------------------------------------------------------------------
5403// StringCharFromCodeGenerator
5404
5405void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005406 // Fast case of Heap::LookupSingleCharacterStringFromCode.
5407
5408 ASSERT(!t0.is(result_));
5409 ASSERT(!t0.is(code_));
5410
5411 STATIC_ASSERT(kSmiTag == 0);
5412 STATIC_ASSERT(kSmiShiftSize == 0);
5413 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
5414 __ And(t0,
5415 code_,
5416 Operand(kSmiTagMask |
5417 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
5418 __ Branch(&slow_case_, ne, t0, Operand(zero_reg));
5419
5420 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
5421 // At this point code register contains smi tagged ASCII char code.
5422 STATIC_ASSERT(kSmiTag == 0);
5423 __ sll(t0, code_, kPointerSizeLog2 - kSmiTagSize);
5424 __ Addu(result_, result_, t0);
5425 __ lw(result_, FieldMemOperand(result_, FixedArray::kHeaderSize));
5426 __ LoadRoot(t0, Heap::kUndefinedValueRootIndex);
5427 __ Branch(&slow_case_, eq, result_, Operand(t0));
5428 __ bind(&exit_);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005429}
5430
5431
5432void StringCharFromCodeGenerator::GenerateSlow(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00005433 MacroAssembler* masm,
5434 const RuntimeCallHelper& call_helper) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005435 __ Abort("Unexpected fallthrough to CharFromCode slow case");
5436
5437 __ bind(&slow_case_);
5438 call_helper.BeforeCall(masm);
5439 __ push(code_);
5440 __ CallRuntime(Runtime::kCharFromCode, 1);
5441 __ Move(result_, v0);
5442
5443 call_helper.AfterCall(masm);
5444 __ Branch(&exit_);
5445
5446 __ Abort("Unexpected fallthrough from CharFromCode slow case");
lrn@chromium.org7516f052011-03-30 08:52:27 +00005447}
5448
5449
5450// -------------------------------------------------------------------------
5451// StringCharAtGenerator
5452
5453void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005454 char_code_at_generator_.GenerateFast(masm);
5455 char_from_code_generator_.GenerateFast(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005456}
5457
5458
5459void StringCharAtGenerator::GenerateSlow(
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00005460 MacroAssembler* masm,
5461 const RuntimeCallHelper& call_helper) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005462 char_code_at_generator_.GenerateSlow(masm, call_helper);
5463 char_from_code_generator_.GenerateSlow(masm, call_helper);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005464}
5465
5466
lrn@chromium.org7516f052011-03-30 08:52:27 +00005467void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5468 Register dest,
5469 Register src,
5470 Register count,
5471 Register scratch,
5472 bool ascii) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005473 Label loop;
5474 Label done;
5475 // This loop just copies one character at a time, as it is only used for
5476 // very short strings.
5477 if (!ascii) {
5478 __ addu(count, count, count);
5479 }
5480 __ Branch(&done, eq, count, Operand(zero_reg));
5481 __ addu(count, dest, count); // Count now points to the last dest byte.
5482
5483 __ bind(&loop);
5484 __ lbu(scratch, MemOperand(src));
5485 __ addiu(src, src, 1);
5486 __ sb(scratch, MemOperand(dest));
5487 __ addiu(dest, dest, 1);
5488 __ Branch(&loop, lt, dest, Operand(count));
5489
5490 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005491}
5492
5493
5494enum CopyCharactersFlags {
5495 COPY_ASCII = 1,
5496 DEST_ALWAYS_ALIGNED = 2
5497};
5498
5499
5500void StringHelper::GenerateCopyCharactersLong(MacroAssembler* masm,
5501 Register dest,
5502 Register src,
5503 Register count,
5504 Register scratch1,
5505 Register scratch2,
5506 Register scratch3,
5507 Register scratch4,
5508 Register scratch5,
5509 int flags) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005510 bool ascii = (flags & COPY_ASCII) != 0;
5511 bool dest_always_aligned = (flags & DEST_ALWAYS_ALIGNED) != 0;
5512
5513 if (dest_always_aligned && FLAG_debug_code) {
5514 // Check that destination is actually word aligned if the flag says
5515 // that it is.
5516 __ And(scratch4, dest, Operand(kPointerAlignmentMask));
5517 __ Check(eq,
5518 "Destination of copy not aligned.",
5519 scratch4,
5520 Operand(zero_reg));
5521 }
5522
5523 const int kReadAlignment = 4;
5524 const int kReadAlignmentMask = kReadAlignment - 1;
5525 // Ensure that reading an entire aligned word containing the last character
5526 // of a string will not read outside the allocated area (because we pad up
5527 // to kObjectAlignment).
5528 STATIC_ASSERT(kObjectAlignment >= kReadAlignment);
5529 // Assumes word reads and writes are little endian.
5530 // Nothing to do for zero characters.
5531 Label done;
5532
5533 if (!ascii) {
5534 __ addu(count, count, count);
5535 }
5536 __ Branch(&done, eq, count, Operand(zero_reg));
5537
5538 Label byte_loop;
5539 // Must copy at least eight bytes, otherwise just do it one byte at a time.
5540 __ Subu(scratch1, count, Operand(8));
5541 __ Addu(count, dest, Operand(count));
5542 Register limit = count; // Read until src equals this.
5543 __ Branch(&byte_loop, lt, scratch1, Operand(zero_reg));
5544
5545 if (!dest_always_aligned) {
5546 // Align dest by byte copying. Copies between zero and three bytes.
5547 __ And(scratch4, dest, Operand(kReadAlignmentMask));
5548 Label dest_aligned;
5549 __ Branch(&dest_aligned, eq, scratch4, Operand(zero_reg));
5550 Label aligned_loop;
5551 __ bind(&aligned_loop);
5552 __ lbu(scratch1, MemOperand(src));
5553 __ addiu(src, src, 1);
5554 __ sb(scratch1, MemOperand(dest));
5555 __ addiu(dest, dest, 1);
5556 __ addiu(scratch4, scratch4, 1);
5557 __ Branch(&aligned_loop, le, scratch4, Operand(kReadAlignmentMask));
5558 __ bind(&dest_aligned);
5559 }
5560
5561 Label simple_loop;
5562
5563 __ And(scratch4, src, Operand(kReadAlignmentMask));
5564 __ Branch(&simple_loop, eq, scratch4, Operand(zero_reg));
5565
5566 // Loop for src/dst that are not aligned the same way.
5567 // This loop uses lwl and lwr instructions. These instructions
5568 // depend on the endianness, and the implementation assumes little-endian.
5569 {
5570 Label loop;
5571 __ bind(&loop);
5572 __ lwr(scratch1, MemOperand(src));
5573 __ Addu(src, src, Operand(kReadAlignment));
5574 __ lwl(scratch1, MemOperand(src, -1));
5575 __ sw(scratch1, MemOperand(dest));
5576 __ Addu(dest, dest, Operand(kReadAlignment));
5577 __ Subu(scratch2, limit, dest);
5578 __ Branch(&loop, ge, scratch2, Operand(kReadAlignment));
5579 }
5580
5581 __ Branch(&byte_loop);
5582
5583 // Simple loop.
5584 // Copy words from src to dest, until less than four bytes left.
5585 // Both src and dest are word aligned.
5586 __ bind(&simple_loop);
5587 {
5588 Label loop;
5589 __ bind(&loop);
5590 __ lw(scratch1, MemOperand(src));
5591 __ Addu(src, src, Operand(kReadAlignment));
5592 __ sw(scratch1, MemOperand(dest));
5593 __ Addu(dest, dest, Operand(kReadAlignment));
5594 __ Subu(scratch2, limit, dest);
5595 __ Branch(&loop, ge, scratch2, Operand(kReadAlignment));
5596 }
5597
5598 // Copy bytes from src to dest until dest hits limit.
5599 __ bind(&byte_loop);
5600 // Test if dest has already reached the limit.
5601 __ Branch(&done, ge, dest, Operand(limit));
5602 __ lbu(scratch1, MemOperand(src));
5603 __ addiu(src, src, 1);
5604 __ sb(scratch1, MemOperand(dest));
5605 __ addiu(dest, dest, 1);
5606 __ Branch(&byte_loop);
5607
5608 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005609}
5610
5611
5612void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5613 Register c1,
5614 Register c2,
5615 Register scratch1,
5616 Register scratch2,
5617 Register scratch3,
5618 Register scratch4,
5619 Register scratch5,
5620 Label* not_found) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005621 // Register scratch3 is the general scratch register in this function.
5622 Register scratch = scratch3;
5623
5624 // Make sure that both characters are not digits as such strings has a
5625 // different hash algorithm. Don't try to look for these in the symbol table.
5626 Label not_array_index;
5627 __ Subu(scratch, c1, Operand(static_cast<int>('0')));
5628 __ Branch(&not_array_index,
5629 Ugreater,
5630 scratch,
5631 Operand(static_cast<int>('9' - '0')));
5632 __ Subu(scratch, c2, Operand(static_cast<int>('0')));
5633
5634 // If check failed combine both characters into single halfword.
5635 // This is required by the contract of the method: code at the
5636 // not_found branch expects this combination in c1 register.
5637 Label tmp;
5638 __ sll(scratch1, c2, kBitsPerByte);
5639 __ Branch(&tmp, Ugreater, scratch, Operand(static_cast<int>('9' - '0')));
5640 __ Or(c1, c1, scratch1);
5641 __ bind(&tmp);
5642 __ Branch(not_found,
5643 Uless_equal,
5644 scratch,
5645 Operand(static_cast<int>('9' - '0')));
5646
5647 __ bind(&not_array_index);
5648 // Calculate the two character string hash.
5649 Register hash = scratch1;
5650 StringHelper::GenerateHashInit(masm, hash, c1);
5651 StringHelper::GenerateHashAddCharacter(masm, hash, c2);
5652 StringHelper::GenerateHashGetHash(masm, hash);
5653
5654 // Collect the two characters in a register.
5655 Register chars = c1;
5656 __ sll(scratch, c2, kBitsPerByte);
5657 __ Or(chars, chars, scratch);
5658
5659 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5660 // hash: hash of two character string.
5661
5662 // Load symbol table.
5663 // Load address of first element of the symbol table.
5664 Register symbol_table = c2;
5665 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
5666
5667 Register undefined = scratch4;
5668 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
5669
5670 // Calculate capacity mask from the symbol table capacity.
5671 Register mask = scratch2;
5672 __ lw(mask, FieldMemOperand(symbol_table, SymbolTable::kCapacityOffset));
5673 __ sra(mask, mask, 1);
5674 __ Addu(mask, mask, -1);
5675
5676 // Calculate untagged address of the first element of the symbol table.
5677 Register first_symbol_table_element = symbol_table;
5678 __ Addu(first_symbol_table_element, symbol_table,
5679 Operand(SymbolTable::kElementsStartOffset - kHeapObjectTag));
5680
5681 // Registers.
5682 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5683 // hash: hash of two character string
5684 // mask: capacity mask
5685 // first_symbol_table_element: address of the first element of
5686 // the symbol table
5687 // undefined: the undefined object
5688 // scratch: -
5689
5690 // Perform a number of probes in the symbol table.
5691 static const int kProbes = 4;
5692 Label found_in_symbol_table;
5693 Label next_probe[kProbes];
5694 Register candidate = scratch5; // Scratch register contains candidate.
5695 for (int i = 0; i < kProbes; i++) {
5696 // Calculate entry in symbol table.
5697 if (i > 0) {
5698 __ Addu(candidate, hash, Operand(SymbolTable::GetProbeOffset(i)));
5699 } else {
5700 __ mov(candidate, hash);
5701 }
5702
5703 __ And(candidate, candidate, Operand(mask));
5704
5705 // Load the entry from the symble table.
5706 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5707 __ sll(scratch, candidate, kPointerSizeLog2);
5708 __ Addu(scratch, scratch, first_symbol_table_element);
5709 __ lw(candidate, MemOperand(scratch));
5710
5711 // If entry is undefined no string with this hash can be found.
5712 Label is_string;
5713 __ GetObjectType(candidate, scratch, scratch);
5714 __ Branch(&is_string, ne, scratch, Operand(ODDBALL_TYPE));
5715
5716 __ Branch(not_found, eq, undefined, Operand(candidate));
danno@chromium.org2c456792011-11-11 12:00:53 +00005717 // Must be the hole (deleted entry).
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005718 if (FLAG_debug_code) {
danno@chromium.org2c456792011-11-11 12:00:53 +00005719 __ LoadRoot(scratch, Heap::kTheHoleValueRootIndex);
5720 __ Assert(eq, "oddball in symbol table is not undefined or the hole",
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005721 scratch, Operand(candidate));
5722 }
5723 __ jmp(&next_probe[i]);
5724
5725 __ bind(&is_string);
5726
5727 // Check that the candidate is a non-external ASCII string. The instance
5728 // type is still in the scratch register from the CompareObjectType
5729 // operation.
5730 __ JumpIfInstanceTypeIsNotSequentialAscii(scratch, scratch, &next_probe[i]);
5731
5732 // If length is not 2 the string is not a candidate.
5733 __ lw(scratch, FieldMemOperand(candidate, String::kLengthOffset));
5734 __ Branch(&next_probe[i], ne, scratch, Operand(Smi::FromInt(2)));
5735
5736 // Check if the two characters match.
5737 // Assumes that word load is little endian.
5738 __ lhu(scratch, FieldMemOperand(candidate, SeqAsciiString::kHeaderSize));
5739 __ Branch(&found_in_symbol_table, eq, chars, Operand(scratch));
5740 __ bind(&next_probe[i]);
5741 }
5742
5743 // No matching 2 character string found by probing.
5744 __ jmp(not_found);
5745
5746 // Scratch register contains result when we fall through to here.
5747 Register result = candidate;
5748 __ bind(&found_in_symbol_table);
5749 __ mov(v0, result);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005750}
5751
5752
5753void StringHelper::GenerateHashInit(MacroAssembler* masm,
5754 Register hash,
5755 Register character) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005756 // hash = character + (character << 10);
5757 __ sll(hash, character, 10);
5758 __ addu(hash, hash, character);
5759 // hash ^= hash >> 6;
danno@chromium.org2c456792011-11-11 12:00:53 +00005760 __ srl(at, hash, 6);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005761 __ xor_(hash, hash, at);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005762}
5763
5764
5765void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
5766 Register hash,
5767 Register character) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005768 // hash += character;
5769 __ addu(hash, hash, character);
5770 // hash += hash << 10;
5771 __ sll(at, hash, 10);
5772 __ addu(hash, hash, at);
5773 // hash ^= hash >> 6;
danno@chromium.org2c456792011-11-11 12:00:53 +00005774 __ srl(at, hash, 6);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005775 __ xor_(hash, hash, at);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005776}
5777
5778
5779void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
5780 Register hash) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005781 // hash += hash << 3;
5782 __ sll(at, hash, 3);
5783 __ addu(hash, hash, at);
5784 // hash ^= hash >> 11;
danno@chromium.org2c456792011-11-11 12:00:53 +00005785 __ srl(at, hash, 11);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005786 __ xor_(hash, hash, at);
5787 // hash += hash << 15;
5788 __ sll(at, hash, 15);
5789 __ addu(hash, hash, at);
5790
danno@chromium.org2c456792011-11-11 12:00:53 +00005791 uint32_t kHashShiftCutOffMask = (1 << (32 - String::kHashShift)) - 1;
5792 __ li(at, Operand(kHashShiftCutOffMask));
5793 __ and_(hash, hash, at);
5794
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005795 // if (hash == 0) hash = 27;
5796 __ ori(at, zero_reg, 27);
5797 __ movz(hash, at, hash);
lrn@chromium.org7516f052011-03-30 08:52:27 +00005798}
5799
5800
5801void SubStringStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005802 Label sub_string_runtime;
5803 // Stack frame on entry.
5804 // ra: return address
5805 // sp[0]: to
5806 // sp[4]: from
5807 // sp[8]: string
5808
5809 // This stub is called from the native-call %_SubString(...), so
5810 // nothing can be assumed about the arguments. It is tested that:
5811 // "string" is a sequential string,
5812 // both "from" and "to" are smis, and
5813 // 0 <= from <= to <= string.length.
5814 // If any of these assumptions fail, we call the runtime system.
5815
5816 static const int kToOffset = 0 * kPointerSize;
5817 static const int kFromOffset = 1 * kPointerSize;
5818 static const int kStringOffset = 2 * kPointerSize;
5819
5820 Register to = t2;
5821 Register from = t3;
5822
5823 // Check bounds and smi-ness.
5824 __ lw(to, MemOperand(sp, kToOffset));
5825 __ lw(from, MemOperand(sp, kFromOffset));
5826 STATIC_ASSERT(kFromOffset == kToOffset + 4);
5827 STATIC_ASSERT(kSmiTag == 0);
5828 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
5829
5830 __ JumpIfNotSmi(from, &sub_string_runtime);
5831 __ JumpIfNotSmi(to, &sub_string_runtime);
5832
5833 __ sra(a3, from, kSmiTagSize); // Remove smi tag.
5834 __ sra(t5, to, kSmiTagSize); // Remove smi tag.
5835
5836 // a3: from index (untagged smi)
5837 // t5: to index (untagged smi)
5838
5839 __ Branch(&sub_string_runtime, lt, a3, Operand(zero_reg)); // From < 0.
5840
5841 __ subu(a2, t5, a3);
5842 __ Branch(&sub_string_runtime, gt, a3, Operand(t5)); // Fail if from > to.
5843
5844 // Special handling of sub-strings of length 1 and 2. One character strings
5845 // are handled in the runtime system (looked up in the single character
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005846 // cache). Two character strings are looked for in the symbol cache in
5847 // generated code.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005848 __ Branch(&sub_string_runtime, lt, a2, Operand(2));
5849
5850 // Both to and from are smis.
5851
5852 // a2: result string length
5853 // a3: from index (untagged smi)
5854 // t2: (a.k.a. to): to (smi)
5855 // t3: (a.k.a. from): from offset (smi)
5856 // t5: to index (untagged smi)
5857
5858 // Make sure first argument is a sequential (or flat) string.
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005859 __ lw(v0, MemOperand(sp, kStringOffset));
5860 __ Branch(&sub_string_runtime, eq, v0, Operand(kSmiTagMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005861
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005862 __ lw(a1, FieldMemOperand(v0, HeapObject::kMapOffset));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005863 __ lbu(a1, FieldMemOperand(a1, Map::kInstanceTypeOffset));
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005864 __ And(t4, v0, Operand(kIsNotStringMask));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005865
5866 __ Branch(&sub_string_runtime, ne, t4, Operand(zero_reg));
5867
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005868 // Short-cut for the case of trivial substring.
5869 Label return_v0;
5870 // v0: original string
5871 // a2: result string length
5872 __ lw(t0, FieldMemOperand(v0, String::kLengthOffset));
5873 __ sra(t0, t0, 1);
5874 __ Branch(&return_v0, eq, a2, Operand(t0));
5875
5876 Label create_slice;
5877 if (FLAG_string_slices) {
5878 __ Branch(&create_slice, ge, a2, Operand(SlicedString::kMinLength));
5879 }
5880
5881 // v0: original string
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005882 // a1: instance type
5883 // a2: result string length
5884 // a3: from index (untagged smi)
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005885 // t2: (a.k.a. to): to (smi)
5886 // t3: (a.k.a. from): from offset (smi)
5887 // t5: to index (untagged smi)
5888
5889 Label seq_string;
5890 __ And(t0, a1, Operand(kStringRepresentationMask));
5891 STATIC_ASSERT(kSeqStringTag < kConsStringTag);
5892 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005893 STATIC_ASSERT(kConsStringTag < kSlicedStringTag);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005894
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005895 // Slices and external strings go to runtime.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005896 __ Branch(&sub_string_runtime, gt, t0, Operand(kConsStringTag));
5897
5898 // Sequential strings are handled directly.
5899 __ Branch(&seq_string, lt, t0, Operand(kConsStringTag));
5900
5901 // Cons string. Try to recurse (once) on the first substring.
5902 // (This adds a little more generality than necessary to handle flattened
5903 // cons strings, but not much).
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005904 __ lw(v0, FieldMemOperand(v0, ConsString::kFirstOffset));
5905 __ lw(t0, FieldMemOperand(v0, HeapObject::kMapOffset));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005906 __ lbu(a1, FieldMemOperand(t0, Map::kInstanceTypeOffset));
5907 STATIC_ASSERT(kSeqStringTag == 0);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005908 // Cons, slices and external strings go to runtime.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005909 __ Branch(&sub_string_runtime, ne, a1, Operand(kStringRepresentationMask));
5910
5911 // Definitly a sequential string.
5912 __ bind(&seq_string);
5913
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005914 // v0: original string
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005915 // a1: instance type
5916 // a2: result string length
5917 // a3: from index (untagged smi)
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005918 // t2: (a.k.a. to): to (smi)
5919 // t3: (a.k.a. from): from offset (smi)
5920 // t5: to index (untagged smi)
5921
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005922 __ lw(t0, FieldMemOperand(v0, String::kLengthOffset));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005923 __ Branch(&sub_string_runtime, lt, t0, Operand(to)); // Fail if to > length.
5924 to = no_reg;
5925
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005926 // v0: original string or left hand side of the original cons string.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005927 // a1: instance type
5928 // a2: result string length
5929 // a3: from index (untagged smi)
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005930 // t3: (a.k.a. from): from offset (smi)
5931 // t5: to index (untagged smi)
5932
5933 // Check for flat ASCII string.
5934 Label non_ascii_flat;
5935 STATIC_ASSERT(kTwoByteStringTag == 0);
5936
5937 __ And(t4, a1, Operand(kStringEncodingMask));
5938 __ Branch(&non_ascii_flat, eq, t4, Operand(zero_reg));
5939
5940 Label result_longer_than_two;
5941 __ Branch(&result_longer_than_two, gt, a2, Operand(2));
5942
5943 // Sub string of length 2 requested.
5944 // Get the two characters forming the sub string.
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005945 __ Addu(v0, v0, Operand(a3));
5946 __ lbu(a3, FieldMemOperand(v0, SeqAsciiString::kHeaderSize));
5947 __ lbu(t0, FieldMemOperand(v0, SeqAsciiString::kHeaderSize + 1));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005948
5949 // Try to lookup two character string in symbol table.
5950 Label make_two_character_string;
5951 StringHelper::GenerateTwoCharacterSymbolTableProbe(
5952 masm, a3, t0, a1, t1, t2, t3, t4, &make_two_character_string);
5953 Counters* counters = masm->isolate()->counters();
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005954 __ jmp(&return_v0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005955
5956 // a2: result string length.
5957 // a3: two characters combined into halfword in little endian byte order.
5958 __ bind(&make_two_character_string);
5959 __ AllocateAsciiString(v0, a2, t0, t1, t4, &sub_string_runtime);
5960 __ sh(a3, FieldMemOperand(v0, SeqAsciiString::kHeaderSize));
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005961 __ jmp(&return_v0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005962
5963 __ bind(&result_longer_than_two);
5964
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005965 // Locate 'from' character of string.
5966 __ Addu(t1, v0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5967 __ sra(t4, from, 1);
5968 __ Addu(t1, t1, t4);
5969
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005970 // Allocate the result.
5971 __ AllocateAsciiString(v0, a2, t4, t0, a1, &sub_string_runtime);
5972
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005973 // v0: result string
5974 // a2: result string length
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005975 // a3: from index (untagged smi)
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005976 // t1: first character of substring to copy
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005977 // t3: (a.k.a. from): from offset (smi)
5978 // Locate first character of result.
5979 __ Addu(a1, v0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005980
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005981 // v0: result string
5982 // a1: first character of result string
5983 // a2: result string length
5984 // t1: first character of substring to copy
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005985 STATIC_ASSERT((SeqAsciiString::kHeaderSize & kObjectAlignmentMask) == 0);
5986 StringHelper::GenerateCopyCharactersLong(
5987 masm, a1, t1, a2, a3, t0, t2, t3, t4, COPY_ASCII | DEST_ALWAYS_ALIGNED);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005988 __ jmp(&return_v0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005989
5990 __ bind(&non_ascii_flat);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005991 // a2: result string length
5992 // t1: string
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00005993 // t3: (a.k.a. from): from offset (smi)
5994 // Check for flat two byte string.
5995
fschneider@chromium.org1805e212011-09-05 10:49:12 +00005996 // Locate 'from' character of string.
5997 __ Addu(t1, v0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5998 // As "from" is a smi it is 2 times the value which matches the size of a two
5999 // byte character.
6000 STATIC_ASSERT(kSmiTagSize == 1 && kSmiTag == 0);
6001 __ Addu(t1, t1, Operand(from));
6002
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006003 // Allocate the result.
6004 __ AllocateTwoByteString(v0, a2, a1, a3, t0, &sub_string_runtime);
6005
fschneider@chromium.org1805e212011-09-05 10:49:12 +00006006 // v0: result string
6007 // a2: result string length
6008 // t1: first character of substring to copy
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006009 // Locate first character of result.
6010 __ Addu(a1, v0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
fschneider@chromium.org1805e212011-09-05 10:49:12 +00006011
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006012 from = no_reg;
6013
6014 // v0: result string.
6015 // a1: first character of result.
6016 // a2: result length.
fschneider@chromium.org1805e212011-09-05 10:49:12 +00006017 // t1: first character of substring to copy.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006018 STATIC_ASSERT((SeqTwoByteString::kHeaderSize & kObjectAlignmentMask) == 0);
6019 StringHelper::GenerateCopyCharactersLong(
6020 masm, a1, t1, a2, a3, t0, t2, t3, t4, DEST_ALWAYS_ALIGNED);
fschneider@chromium.org1805e212011-09-05 10:49:12 +00006021 __ jmp(&return_v0);
6022
6023 if (FLAG_string_slices) {
6024 __ bind(&create_slice);
6025 // v0: original string
6026 // a1: instance type
6027 // a2: length
6028 // a3: from index (untagged smi)
6029 // t2 (a.k.a. to): to (smi)
6030 // t3 (a.k.a. from): from offset (smi)
erikcorry0ad885c2011-11-21 13:51:57 +00006031 Label allocate_slice, sliced_string, seq_or_external_string;
6032 // If the string is not indirect, it can only be sequential or external.
fschneider@chromium.org1805e212011-09-05 10:49:12 +00006033 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
6034 STATIC_ASSERT(kIsIndirectStringMask != 0);
6035 __ And(t4, a1, Operand(kIsIndirectStringMask));
6036 // External string. Jump to runtime.
erikcorry0ad885c2011-11-21 13:51:57 +00006037 __ Branch(&seq_or_external_string, eq, t4, Operand(zero_reg));
fschneider@chromium.org1805e212011-09-05 10:49:12 +00006038
6039 __ And(t4, a1, Operand(kSlicedNotConsMask));
6040 __ Branch(&sliced_string, ne, t4, Operand(zero_reg));
6041 // Cons string. Check whether it is flat, then fetch first part.
6042 __ lw(t1, FieldMemOperand(v0, ConsString::kSecondOffset));
6043 __ LoadRoot(t5, Heap::kEmptyStringRootIndex);
6044 __ Branch(&sub_string_runtime, ne, t1, Operand(t5));
6045 __ lw(t1, FieldMemOperand(v0, ConsString::kFirstOffset));
6046 __ jmp(&allocate_slice);
6047
6048 __ bind(&sliced_string);
6049 // Sliced string. Fetch parent and correct start index by offset.
6050 __ lw(t1, FieldMemOperand(v0, SlicedString::kOffsetOffset));
6051 __ addu(t3, t3, t1);
6052 __ lw(t1, FieldMemOperand(v0, SlicedString::kParentOffset));
6053 __ jmp(&allocate_slice);
6054
erikcorry0ad885c2011-11-21 13:51:57 +00006055 __ bind(&seq_or_external_string);
6056 // Sequential or external string. Just move string to the correct register.
fschneider@chromium.org1805e212011-09-05 10:49:12 +00006057 __ mov(t1, v0);
6058
6059 __ bind(&allocate_slice);
6060 // a1: instance type of original string
6061 // a2: length
6062 // t1: underlying subject string
6063 // t3 (a.k.a. from): from offset (smi)
6064 // Allocate new sliced string. At this point we do not reload the instance
6065 // type including the string encoding because we simply rely on the info
6066 // provided by the original string. It does not matter if the original
6067 // string's encoding is wrong because we always have to recheck encoding of
6068 // the newly created string's parent anyways due to externalized strings.
6069 Label two_byte_slice, set_slice_header;
6070 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
6071 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
6072 __ And(t4, a1, Operand(kStringEncodingMask));
6073 __ Branch(&two_byte_slice, eq, t4, Operand(zero_reg));
6074 __ AllocateAsciiSlicedString(v0, a2, a3, t0, &sub_string_runtime);
6075 __ jmp(&set_slice_header);
6076 __ bind(&two_byte_slice);
6077 __ AllocateTwoByteSlicedString(v0, a2, a3, t0, &sub_string_runtime);
6078 __ bind(&set_slice_header);
6079 __ sw(t3, FieldMemOperand(v0, SlicedString::kOffsetOffset));
6080 __ sw(t1, FieldMemOperand(v0, SlicedString::kParentOffset));
6081 }
6082
6083 __ bind(&return_v0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006084 __ IncrementCounter(counters->sub_string_native(), 1, a3, t0);
6085 __ Addu(sp, sp, Operand(3 * kPointerSize));
6086 __ Ret();
6087
6088 // Just jump to runtime to create the sub string.
6089 __ bind(&sub_string_runtime);
6090 __ TailCallRuntime(Runtime::kSubString, 3, 1);
6091}
6092
6093
6094void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
6095 Register left,
6096 Register right,
6097 Register scratch1,
6098 Register scratch2,
6099 Register scratch3) {
6100 Register length = scratch1;
6101
6102 // Compare lengths.
6103 Label strings_not_equal, check_zero_length;
6104 __ lw(length, FieldMemOperand(left, String::kLengthOffset));
6105 __ lw(scratch2, FieldMemOperand(right, String::kLengthOffset));
6106 __ Branch(&check_zero_length, eq, length, Operand(scratch2));
6107 __ bind(&strings_not_equal);
6108 __ li(v0, Operand(Smi::FromInt(NOT_EQUAL)));
6109 __ Ret();
6110
6111 // Check if the length is zero.
6112 Label compare_chars;
6113 __ bind(&check_zero_length);
6114 STATIC_ASSERT(kSmiTag == 0);
6115 __ Branch(&compare_chars, ne, length, Operand(zero_reg));
6116 __ li(v0, Operand(Smi::FromInt(EQUAL)));
6117 __ Ret();
6118
6119 // Compare characters.
6120 __ bind(&compare_chars);
6121
6122 GenerateAsciiCharsCompareLoop(masm,
6123 left, right, length, scratch2, scratch3, v0,
6124 &strings_not_equal);
6125
6126 // Characters are equal.
6127 __ li(v0, Operand(Smi::FromInt(EQUAL)));
6128 __ Ret();
lrn@chromium.org7516f052011-03-30 08:52:27 +00006129}
6130
6131
6132void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
lrn@chromium.org7516f052011-03-30 08:52:27 +00006133 Register left,
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006134 Register right,
lrn@chromium.org7516f052011-03-30 08:52:27 +00006135 Register scratch1,
6136 Register scratch2,
6137 Register scratch3,
6138 Register scratch4) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006139 Label result_not_equal, compare_lengths;
6140 // Find minimum length and length difference.
6141 __ lw(scratch1, FieldMemOperand(left, String::kLengthOffset));
6142 __ lw(scratch2, FieldMemOperand(right, String::kLengthOffset));
6143 __ Subu(scratch3, scratch1, Operand(scratch2));
6144 Register length_delta = scratch3;
6145 __ slt(scratch4, scratch2, scratch1);
6146 __ movn(scratch1, scratch2, scratch4);
6147 Register min_length = scratch1;
6148 STATIC_ASSERT(kSmiTag == 0);
6149 __ Branch(&compare_lengths, eq, min_length, Operand(zero_reg));
6150
6151 // Compare loop.
6152 GenerateAsciiCharsCompareLoop(masm,
6153 left, right, min_length, scratch2, scratch4, v0,
6154 &result_not_equal);
6155
6156 // Compare lengths - strings up to min-length are equal.
6157 __ bind(&compare_lengths);
6158 ASSERT(Smi::FromInt(EQUAL) == static_cast<Smi*>(0));
6159 // Use length_delta as result if it's zero.
6160 __ mov(scratch2, length_delta);
6161 __ mov(scratch4, zero_reg);
6162 __ mov(v0, zero_reg);
6163
6164 __ bind(&result_not_equal);
6165 // Conditionally update the result based either on length_delta or
6166 // the last comparion performed in the loop above.
6167 Label ret;
6168 __ Branch(&ret, eq, scratch2, Operand(scratch4));
6169 __ li(v0, Operand(Smi::FromInt(GREATER)));
6170 __ Branch(&ret, gt, scratch2, Operand(scratch4));
6171 __ li(v0, Operand(Smi::FromInt(LESS)));
6172 __ bind(&ret);
6173 __ Ret();
6174}
6175
6176
6177void StringCompareStub::GenerateAsciiCharsCompareLoop(
6178 MacroAssembler* masm,
6179 Register left,
6180 Register right,
6181 Register length,
6182 Register scratch1,
6183 Register scratch2,
6184 Register scratch3,
6185 Label* chars_not_equal) {
6186 // Change index to run from -length to -1 by adding length to string
6187 // start. This means that loop ends when index reaches zero, which
6188 // doesn't need an additional compare.
6189 __ SmiUntag(length);
6190 __ Addu(scratch1, length,
6191 Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6192 __ Addu(left, left, Operand(scratch1));
6193 __ Addu(right, right, Operand(scratch1));
6194 __ Subu(length, zero_reg, length);
6195 Register index = length; // index = -length;
6196
6197
6198 // Compare loop.
6199 Label loop;
6200 __ bind(&loop);
6201 __ Addu(scratch3, left, index);
6202 __ lbu(scratch1, MemOperand(scratch3));
6203 __ Addu(scratch3, right, index);
6204 __ lbu(scratch2, MemOperand(scratch3));
6205 __ Branch(chars_not_equal, ne, scratch1, Operand(scratch2));
6206 __ Addu(index, index, 1);
6207 __ Branch(&loop, ne, index, Operand(zero_reg));
lrn@chromium.org7516f052011-03-30 08:52:27 +00006208}
6209
6210
6211void StringCompareStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006212 Label runtime;
6213
6214 Counters* counters = masm->isolate()->counters();
6215
6216 // Stack frame on entry.
6217 // sp[0]: right string
6218 // sp[4]: left string
6219 __ lw(a1, MemOperand(sp, 1 * kPointerSize)); // Left.
6220 __ lw(a0, MemOperand(sp, 0 * kPointerSize)); // Right.
6221
6222 Label not_same;
6223 __ Branch(&not_same, ne, a0, Operand(a1));
6224 STATIC_ASSERT(EQUAL == 0);
6225 STATIC_ASSERT(kSmiTag == 0);
6226 __ li(v0, Operand(Smi::FromInt(EQUAL)));
6227 __ IncrementCounter(counters->string_compare_native(), 1, a1, a2);
6228 __ Addu(sp, sp, Operand(2 * kPointerSize));
6229 __ Ret();
6230
6231 __ bind(&not_same);
6232
6233 // Check that both objects are sequential ASCII strings.
6234 __ JumpIfNotBothSequentialAsciiStrings(a1, a0, a2, a3, &runtime);
6235
6236 // Compare flat ASCII strings natively. Remove arguments from stack first.
6237 __ IncrementCounter(counters->string_compare_native(), 1, a2, a3);
6238 __ Addu(sp, sp, Operand(2 * kPointerSize));
6239 GenerateCompareFlatAsciiStrings(masm, a1, a0, a2, a3, t0, t1);
6240
6241 __ bind(&runtime);
6242 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
lrn@chromium.org7516f052011-03-30 08:52:27 +00006243}
6244
6245
6246void StringAddStub::Generate(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006247 Label string_add_runtime, call_builtin;
6248 Builtins::JavaScript builtin_id = Builtins::ADD;
6249
6250 Counters* counters = masm->isolate()->counters();
6251
6252 // Stack on entry:
6253 // sp[0]: second argument (right).
6254 // sp[4]: first argument (left).
6255
6256 // Load the two arguments.
6257 __ lw(a0, MemOperand(sp, 1 * kPointerSize)); // First argument.
6258 __ lw(a1, MemOperand(sp, 0 * kPointerSize)); // Second argument.
6259
6260 // Make sure that both arguments are strings if not known in advance.
6261 if (flags_ == NO_STRING_ADD_FLAGS) {
6262 __ JumpIfEitherSmi(a0, a1, &string_add_runtime);
6263 // Load instance types.
6264 __ lw(t0, FieldMemOperand(a0, HeapObject::kMapOffset));
6265 __ lw(t1, FieldMemOperand(a1, HeapObject::kMapOffset));
6266 __ lbu(t0, FieldMemOperand(t0, Map::kInstanceTypeOffset));
6267 __ lbu(t1, FieldMemOperand(t1, Map::kInstanceTypeOffset));
6268 STATIC_ASSERT(kStringTag == 0);
6269 // If either is not a string, go to runtime.
6270 __ Or(t4, t0, Operand(t1));
6271 __ And(t4, t4, Operand(kIsNotStringMask));
6272 __ Branch(&string_add_runtime, ne, t4, Operand(zero_reg));
6273 } else {
6274 // Here at least one of the arguments is definitely a string.
6275 // We convert the one that is not known to be a string.
6276 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
6277 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
6278 GenerateConvertArgument(
6279 masm, 1 * kPointerSize, a0, a2, a3, t0, t1, &call_builtin);
6280 builtin_id = Builtins::STRING_ADD_RIGHT;
6281 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
6282 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
6283 GenerateConvertArgument(
6284 masm, 0 * kPointerSize, a1, a2, a3, t0, t1, &call_builtin);
6285 builtin_id = Builtins::STRING_ADD_LEFT;
6286 }
6287 }
6288
6289 // Both arguments are strings.
6290 // a0: first string
6291 // a1: second string
6292 // t0: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6293 // t1: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6294 {
6295 Label strings_not_empty;
6296 // Check if either of the strings are empty. In that case return the other.
6297 // These tests use zero-length check on string-length whch is an Smi.
6298 // Assert that Smi::FromInt(0) is really 0.
6299 STATIC_ASSERT(kSmiTag == 0);
6300 ASSERT(Smi::FromInt(0) == 0);
6301 __ lw(a2, FieldMemOperand(a0, String::kLengthOffset));
6302 __ lw(a3, FieldMemOperand(a1, String::kLengthOffset));
6303 __ mov(v0, a0); // Assume we'll return first string (from a0).
6304 __ movz(v0, a1, a2); // If first is empty, return second (from a1).
6305 __ slt(t4, zero_reg, a2); // if (a2 > 0) t4 = 1.
6306 __ slt(t5, zero_reg, a3); // if (a3 > 0) t5 = 1.
6307 __ and_(t4, t4, t5); // Branch if both strings were non-empty.
6308 __ Branch(&strings_not_empty, ne, t4, Operand(zero_reg));
6309
6310 __ IncrementCounter(counters->string_add_native(), 1, a2, a3);
6311 __ Addu(sp, sp, Operand(2 * kPointerSize));
6312 __ Ret();
6313
6314 __ bind(&strings_not_empty);
6315 }
6316
6317 // Untag both string-lengths.
6318 __ sra(a2, a2, kSmiTagSize);
6319 __ sra(a3, a3, kSmiTagSize);
6320
6321 // Both strings are non-empty.
6322 // a0: first string
6323 // a1: second string
6324 // a2: length of first string
6325 // a3: length of second string
6326 // t0: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6327 // t1: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6328 // Look at the length of the result of adding the two strings.
6329 Label string_add_flat_result, longer_than_two;
6330 // Adding two lengths can't overflow.
6331 STATIC_ASSERT(String::kMaxLength < String::kMaxLength * 2);
6332 __ Addu(t2, a2, Operand(a3));
6333 // Use the symbol table when adding two one character strings, as it
6334 // helps later optimizations to return a symbol here.
6335 __ Branch(&longer_than_two, ne, t2, Operand(2));
6336
6337 // Check that both strings are non-external ASCII strings.
6338 if (flags_ != NO_STRING_ADD_FLAGS) {
6339 __ lw(t0, FieldMemOperand(a0, HeapObject::kMapOffset));
6340 __ lw(t1, FieldMemOperand(a1, HeapObject::kMapOffset));
6341 __ lbu(t0, FieldMemOperand(t0, Map::kInstanceTypeOffset));
6342 __ lbu(t1, FieldMemOperand(t1, Map::kInstanceTypeOffset));
6343 }
6344 __ JumpIfBothInstanceTypesAreNotSequentialAscii(t0, t1, t2, t3,
6345 &string_add_runtime);
6346
6347 // Get the two characters forming the sub string.
6348 __ lbu(a2, FieldMemOperand(a0, SeqAsciiString::kHeaderSize));
6349 __ lbu(a3, FieldMemOperand(a1, SeqAsciiString::kHeaderSize));
6350
6351 // Try to lookup two character string in symbol table. If it is not found
6352 // just allocate a new one.
6353 Label make_two_character_string;
6354 StringHelper::GenerateTwoCharacterSymbolTableProbe(
6355 masm, a2, a3, t2, t3, t0, t1, t4, &make_two_character_string);
6356 __ IncrementCounter(counters->string_add_native(), 1, a2, a3);
6357 __ Addu(sp, sp, Operand(2 * kPointerSize));
6358 __ Ret();
6359
6360 __ bind(&make_two_character_string);
6361 // Resulting string has length 2 and first chars of two strings
6362 // are combined into single halfword in a2 register.
6363 // So we can fill resulting string without two loops by a single
6364 // halfword store instruction (which assumes that processor is
6365 // in a little endian mode).
6366 __ li(t2, Operand(2));
6367 __ AllocateAsciiString(v0, t2, t0, t1, t4, &string_add_runtime);
6368 __ sh(a2, FieldMemOperand(v0, SeqAsciiString::kHeaderSize));
6369 __ IncrementCounter(counters->string_add_native(), 1, a2, a3);
6370 __ Addu(sp, sp, Operand(2 * kPointerSize));
6371 __ Ret();
6372
6373 __ bind(&longer_than_two);
6374 // Check if resulting string will be flat.
6375 __ Branch(&string_add_flat_result, lt, t2,
6376 Operand(String::kMinNonFlatLength));
6377 // Handle exceptionally long strings in the runtime system.
6378 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
6379 ASSERT(IsPowerOf2(String::kMaxLength + 1));
6380 // kMaxLength + 1 is representable as shifted literal, kMaxLength is not.
6381 __ Branch(&string_add_runtime, hs, t2, Operand(String::kMaxLength + 1));
6382
6383 // If result is not supposed to be flat, allocate a cons string object.
6384 // If both strings are ASCII the result is an ASCII cons string.
6385 if (flags_ != NO_STRING_ADD_FLAGS) {
6386 __ lw(t0, FieldMemOperand(a0, HeapObject::kMapOffset));
6387 __ lw(t1, FieldMemOperand(a1, HeapObject::kMapOffset));
6388 __ lbu(t0, FieldMemOperand(t0, Map::kInstanceTypeOffset));
6389 __ lbu(t1, FieldMemOperand(t1, Map::kInstanceTypeOffset));
6390 }
6391 Label non_ascii, allocated, ascii_data;
6392 STATIC_ASSERT(kTwoByteStringTag == 0);
6393 // Branch to non_ascii if either string-encoding field is zero (non-ascii).
6394 __ And(t4, t0, Operand(t1));
6395 __ And(t4, t4, Operand(kStringEncodingMask));
6396 __ Branch(&non_ascii, eq, t4, Operand(zero_reg));
6397
6398 // Allocate an ASCII cons string.
6399 __ bind(&ascii_data);
6400 __ AllocateAsciiConsString(t3, t2, t0, t1, &string_add_runtime);
6401 __ bind(&allocated);
6402 // Fill the fields of the cons string.
6403 __ sw(a0, FieldMemOperand(t3, ConsString::kFirstOffset));
6404 __ sw(a1, FieldMemOperand(t3, ConsString::kSecondOffset));
6405 __ mov(v0, t3);
6406 __ IncrementCounter(counters->string_add_native(), 1, a2, a3);
6407 __ Addu(sp, sp, Operand(2 * kPointerSize));
6408 __ Ret();
6409
6410 __ bind(&non_ascii);
6411 // At least one of the strings is two-byte. Check whether it happens
6412 // to contain only ASCII characters.
6413 // t0: first instance type.
6414 // t1: second instance type.
6415 // Branch to if _both_ instances have kAsciiDataHintMask set.
6416 __ And(at, t0, Operand(kAsciiDataHintMask));
6417 __ and_(at, at, t1);
6418 __ Branch(&ascii_data, ne, at, Operand(zero_reg));
6419
6420 __ xor_(t0, t0, t1);
6421 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
6422 __ And(t0, t0, Operand(kAsciiStringTag | kAsciiDataHintTag));
6423 __ Branch(&ascii_data, eq, t0, Operand(kAsciiStringTag | kAsciiDataHintTag));
6424
6425 // Allocate a two byte cons string.
6426 __ AllocateTwoByteConsString(t3, t2, t0, t1, &string_add_runtime);
6427 __ Branch(&allocated);
6428
6429 // Handle creating a flat result. First check that both strings are
6430 // sequential and that they have the same encoding.
6431 // a0: first string
6432 // a1: second string
6433 // a2: length of first string
6434 // a3: length of second string
6435 // t0: first string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6436 // t1: second string instance type (if flags_ == NO_STRING_ADD_FLAGS)
6437 // t2: sum of lengths.
6438 __ bind(&string_add_flat_result);
6439 if (flags_ != NO_STRING_ADD_FLAGS) {
6440 __ lw(t0, FieldMemOperand(a0, HeapObject::kMapOffset));
6441 __ lw(t1, FieldMemOperand(a1, HeapObject::kMapOffset));
6442 __ lbu(t0, FieldMemOperand(t0, Map::kInstanceTypeOffset));
6443 __ lbu(t1, FieldMemOperand(t1, Map::kInstanceTypeOffset));
6444 }
6445 // Check that both strings are sequential, meaning that we
6446 // branch to runtime if either string tag is non-zero.
6447 STATIC_ASSERT(kSeqStringTag == 0);
6448 __ Or(t4, t0, Operand(t1));
6449 __ And(t4, t4, Operand(kStringRepresentationMask));
6450 __ Branch(&string_add_runtime, ne, t4, Operand(zero_reg));
6451
6452 // Now check if both strings have the same encoding (ASCII/Two-byte).
6453 // a0: first string
6454 // a1: second string
6455 // a2: length of first string
6456 // a3: length of second string
6457 // t0: first string instance type
6458 // t1: second string instance type
6459 // t2: sum of lengths.
6460 Label non_ascii_string_add_flat_result;
6461 ASSERT(IsPowerOf2(kStringEncodingMask)); // Just one bit to test.
6462 __ xor_(t3, t1, t0);
6463 __ And(t3, t3, Operand(kStringEncodingMask));
6464 __ Branch(&string_add_runtime, ne, t3, Operand(zero_reg));
6465 // And see if it's ASCII (0) or two-byte (1).
6466 __ And(t3, t0, Operand(kStringEncodingMask));
6467 __ Branch(&non_ascii_string_add_flat_result, eq, t3, Operand(zero_reg));
6468
6469 // Both strings are sequential ASCII strings. We also know that they are
6470 // short (since the sum of the lengths is less than kMinNonFlatLength).
6471 // t2: length of resulting flat string
6472 __ AllocateAsciiString(t3, t2, t0, t1, t4, &string_add_runtime);
6473 // Locate first character of result.
6474 __ Addu(t2, t3, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6475 // Locate first character of first argument.
6476 __ Addu(a0, a0, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6477 // a0: first character of first string.
6478 // a1: second string.
6479 // a2: length of first string.
6480 // a3: length of second string.
6481 // t2: first character of result.
6482 // t3: result string.
6483 StringHelper::GenerateCopyCharacters(masm, t2, a0, a2, t0, true);
6484
6485 // Load second argument and locate first character.
6486 __ Addu(a1, a1, Operand(SeqAsciiString::kHeaderSize - kHeapObjectTag));
6487 // a1: first character of second string.
6488 // a3: length of second string.
6489 // t2: next character of result.
6490 // t3: result string.
6491 StringHelper::GenerateCopyCharacters(masm, t2, a1, a3, t0, true);
6492 __ mov(v0, t3);
6493 __ IncrementCounter(counters->string_add_native(), 1, a2, a3);
6494 __ Addu(sp, sp, Operand(2 * kPointerSize));
6495 __ Ret();
6496
6497 __ bind(&non_ascii_string_add_flat_result);
6498 // Both strings are sequential two byte strings.
6499 // a0: first string.
6500 // a1: second string.
6501 // a2: length of first string.
6502 // a3: length of second string.
6503 // t2: sum of length of strings.
6504 __ AllocateTwoByteString(t3, t2, t0, t1, t4, &string_add_runtime);
6505 // a0: first string.
6506 // a1: second string.
6507 // a2: length of first string.
6508 // a3: length of second string.
6509 // t3: result string.
6510
6511 // Locate first character of result.
6512 __ Addu(t2, t3, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6513 // Locate first character of first argument.
6514 __ Addu(a0, a0, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6515
6516 // a0: first character of first string.
6517 // a1: second string.
6518 // a2: length of first string.
6519 // a3: length of second string.
6520 // t2: first character of result.
6521 // t3: result string.
6522 StringHelper::GenerateCopyCharacters(masm, t2, a0, a2, t0, false);
6523
6524 // Locate first character of second argument.
6525 __ Addu(a1, a1, Operand(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6526
6527 // a1: first character of second string.
6528 // a3: length of second string.
6529 // t2: next character of result (after copy of first string).
6530 // t3: result string.
6531 StringHelper::GenerateCopyCharacters(masm, t2, a1, a3, t0, false);
6532
6533 __ mov(v0, t3);
6534 __ IncrementCounter(counters->string_add_native(), 1, a2, a3);
6535 __ Addu(sp, sp, Operand(2 * kPointerSize));
6536 __ Ret();
6537
6538 // Just jump to runtime to add the two strings.
6539 __ bind(&string_add_runtime);
6540 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
6541
6542 if (call_builtin.is_linked()) {
6543 __ bind(&call_builtin);
6544 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
6545 }
6546}
6547
6548
6549void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
6550 int stack_offset,
6551 Register arg,
6552 Register scratch1,
6553 Register scratch2,
6554 Register scratch3,
6555 Register scratch4,
6556 Label* slow) {
6557 // First check if the argument is already a string.
6558 Label not_string, done;
6559 __ JumpIfSmi(arg, &not_string);
6560 __ GetObjectType(arg, scratch1, scratch1);
6561 __ Branch(&done, lt, scratch1, Operand(FIRST_NONSTRING_TYPE));
6562
6563 // Check the number to string cache.
6564 Label not_cached;
6565 __ bind(&not_string);
6566 // Puts the cached result into scratch1.
6567 NumberToStringStub::GenerateLookupNumberStringCache(masm,
6568 arg,
6569 scratch1,
6570 scratch2,
6571 scratch3,
6572 scratch4,
6573 false,
6574 &not_cached);
6575 __ mov(arg, scratch1);
6576 __ sw(arg, MemOperand(sp, stack_offset));
6577 __ jmp(&done);
6578
6579 // Check if the argument is a safe string wrapper.
6580 __ bind(&not_cached);
6581 __ JumpIfSmi(arg, slow);
6582 __ GetObjectType(arg, scratch1, scratch2); // map -> scratch1.
6583 __ Branch(slow, ne, scratch2, Operand(JS_VALUE_TYPE));
6584 __ lbu(scratch2, FieldMemOperand(scratch1, Map::kBitField2Offset));
6585 __ li(scratch4, 1 << Map::kStringWrapperSafeForDefaultValueOf);
6586 __ And(scratch2, scratch2, scratch4);
6587 __ Branch(slow, ne, scratch2, Operand(scratch4));
6588 __ lw(arg, FieldMemOperand(arg, JSValue::kValueOffset));
6589 __ sw(arg, MemOperand(sp, stack_offset));
6590
6591 __ bind(&done);
lrn@chromium.org7516f052011-03-30 08:52:27 +00006592}
6593
6594
6595void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006596 ASSERT(state_ == CompareIC::SMIS);
6597 Label miss;
6598 __ Or(a2, a1, a0);
6599 __ JumpIfNotSmi(a2, &miss);
6600
6601 if (GetCondition() == eq) {
6602 // For equality we do not care about the sign of the result.
6603 __ Subu(v0, a0, a1);
6604 } else {
6605 // Untag before subtracting to avoid handling overflow.
6606 __ SmiUntag(a1);
6607 __ SmiUntag(a0);
6608 __ Subu(v0, a1, a0);
6609 }
6610 __ Ret();
6611
6612 __ bind(&miss);
6613 GenerateMiss(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00006614}
6615
6616
6617void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006618 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
6619
6620 Label generic_stub;
6621 Label unordered;
6622 Label miss;
6623 __ And(a2, a1, Operand(a0));
6624 __ JumpIfSmi(a2, &generic_stub);
6625
6626 __ GetObjectType(a0, a2, a2);
6627 __ Branch(&miss, ne, a2, Operand(HEAP_NUMBER_TYPE));
6628 __ GetObjectType(a1, a2, a2);
6629 __ Branch(&miss, ne, a2, Operand(HEAP_NUMBER_TYPE));
6630
6631 // Inlining the double comparison and falling back to the general compare
6632 // stub if NaN is involved or FPU is unsupported.
6633 if (CpuFeatures::IsSupported(FPU)) {
6634 CpuFeatures::Scope scope(FPU);
6635
6636 // Load left and right operand.
6637 __ Subu(a2, a1, Operand(kHeapObjectTag));
6638 __ ldc1(f0, MemOperand(a2, HeapNumber::kValueOffset));
6639 __ Subu(a2, a0, Operand(kHeapObjectTag));
6640 __ ldc1(f2, MemOperand(a2, HeapNumber::kValueOffset));
6641
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006642 // Return a result of -1, 0, or 1, or use CompareStub for NaNs.
6643 Label fpu_eq, fpu_lt;
6644 // Test if equal, and also handle the unordered/NaN case.
6645 __ BranchF(&fpu_eq, &unordered, eq, f0, f2);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006646
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006647 // Test if less (unordered case is already handled).
6648 __ BranchF(&fpu_lt, NULL, lt, f0, f2);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006649
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006650 // Otherwise it's greater, so just fall thru, and return.
6651 __ Ret(USE_DELAY_SLOT);
6652 __ li(v0, Operand(GREATER)); // In delay slot.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006653
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006654 __ bind(&fpu_eq);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006655 __ Ret(USE_DELAY_SLOT);
6656 __ li(v0, Operand(EQUAL)); // In delay slot.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006657
6658 __ bind(&fpu_lt);
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006659 __ Ret(USE_DELAY_SLOT);
6660 __ li(v0, Operand(LESS)); // In delay slot.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006661
6662 __ bind(&unordered);
6663 }
6664
6665 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS, a1, a0);
6666 __ bind(&generic_stub);
6667 __ Jump(stub.GetCode(), RelocInfo::CODE_TARGET);
6668
6669 __ bind(&miss);
6670 GenerateMiss(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00006671}
6672
6673
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006674void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006675 ASSERT(state_ == CompareIC::SYMBOLS);
6676 Label miss;
6677
6678 // Registers containing left and right operands respectively.
6679 Register left = a1;
6680 Register right = a0;
6681 Register tmp1 = a2;
6682 Register tmp2 = a3;
6683
6684 // Check that both operands are heap objects.
6685 __ JumpIfEitherSmi(left, right, &miss);
6686
6687 // Check that both operands are symbols.
6688 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
6689 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
6690 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
6691 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
6692 STATIC_ASSERT(kSymbolTag != 0);
6693 __ And(tmp1, tmp1, Operand(tmp2));
6694 __ And(tmp1, tmp1, kIsSymbolMask);
6695 __ Branch(&miss, eq, tmp1, Operand(zero_reg));
6696 // Make sure a0 is non-zero. At this point input operands are
6697 // guaranteed to be non-zero.
6698 ASSERT(right.is(a0));
6699 STATIC_ASSERT(EQUAL == 0);
6700 STATIC_ASSERT(kSmiTag == 0);
6701 __ mov(v0, right);
6702 // Symbols are compared by identity.
6703 __ Ret(ne, left, Operand(right));
6704 __ li(v0, Operand(Smi::FromInt(EQUAL)));
6705 __ Ret();
6706
6707 __ bind(&miss);
6708 GenerateMiss(masm);
6709}
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006710
6711
6712void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006713 ASSERT(state_ == CompareIC::STRINGS);
6714 Label miss;
6715
6716 // Registers containing left and right operands respectively.
6717 Register left = a1;
6718 Register right = a0;
6719 Register tmp1 = a2;
6720 Register tmp2 = a3;
6721 Register tmp3 = t0;
6722 Register tmp4 = t1;
6723 Register tmp5 = t2;
6724
6725 // Check that both operands are heap objects.
6726 __ JumpIfEitherSmi(left, right, &miss);
6727
6728 // Check that both operands are strings. This leaves the instance
6729 // types loaded in tmp1 and tmp2.
6730 __ lw(tmp1, FieldMemOperand(left, HeapObject::kMapOffset));
6731 __ lw(tmp2, FieldMemOperand(right, HeapObject::kMapOffset));
6732 __ lbu(tmp1, FieldMemOperand(tmp1, Map::kInstanceTypeOffset));
6733 __ lbu(tmp2, FieldMemOperand(tmp2, Map::kInstanceTypeOffset));
6734 STATIC_ASSERT(kNotStringTag != 0);
6735 __ Or(tmp3, tmp1, tmp2);
6736 __ And(tmp5, tmp3, Operand(kIsNotStringMask));
6737 __ Branch(&miss, ne, tmp5, Operand(zero_reg));
6738
6739 // Fast check for identical strings.
6740 Label left_ne_right;
6741 STATIC_ASSERT(EQUAL == 0);
6742 STATIC_ASSERT(kSmiTag == 0);
6743 __ Branch(&left_ne_right, ne, left, Operand(right), USE_DELAY_SLOT);
6744 __ mov(v0, zero_reg); // In the delay slot.
6745 __ Ret();
6746 __ bind(&left_ne_right);
6747
6748 // Handle not identical strings.
6749
6750 // Check that both strings are symbols. If they are, we're done
6751 // because we already know they are not identical.
6752 ASSERT(GetCondition() == eq);
6753 STATIC_ASSERT(kSymbolTag != 0);
6754 __ And(tmp3, tmp1, Operand(tmp2));
6755 __ And(tmp5, tmp3, Operand(kIsSymbolMask));
6756 Label is_symbol;
6757 __ Branch(&is_symbol, eq, tmp5, Operand(zero_reg), USE_DELAY_SLOT);
6758 __ mov(v0, a0); // In the delay slot.
6759 // Make sure a0 is non-zero. At this point input operands are
6760 // guaranteed to be non-zero.
6761 ASSERT(right.is(a0));
6762 __ Ret();
6763 __ bind(&is_symbol);
6764
6765 // Check that both strings are sequential ASCII.
6766 Label runtime;
6767 __ JumpIfBothInstanceTypesAreNotSequentialAscii(tmp1, tmp2, tmp3, tmp4,
6768 &runtime);
6769
6770 // Compare flat ASCII strings. Returns when done.
6771 StringCompareStub::GenerateFlatAsciiStringEquals(
6772 masm, left, right, tmp1, tmp2, tmp3);
6773
6774 // Handle more complex cases in runtime.
6775 __ bind(&runtime);
6776 __ Push(left, right);
6777 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
6778
6779 __ bind(&miss);
6780 GenerateMiss(masm);
karlklose@chromium.org83a47282011-05-11 11:54:09 +00006781}
6782
6783
lrn@chromium.org7516f052011-03-30 08:52:27 +00006784void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006785 ASSERT(state_ == CompareIC::OBJECTS);
6786 Label miss;
6787 __ And(a2, a1, Operand(a0));
6788 __ JumpIfSmi(a2, &miss);
6789
6790 __ GetObjectType(a0, a2, a2);
6791 __ Branch(&miss, ne, a2, Operand(JS_OBJECT_TYPE));
6792 __ GetObjectType(a1, a2, a2);
6793 __ Branch(&miss, ne, a2, Operand(JS_OBJECT_TYPE));
6794
6795 ASSERT(GetCondition() == eq);
6796 __ Subu(v0, a0, Operand(a1));
6797 __ Ret();
6798
6799 __ bind(&miss);
6800 GenerateMiss(masm);
lrn@chromium.org7516f052011-03-30 08:52:27 +00006801}
6802
6803
6804void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006805 __ Push(a1, a0);
6806 __ push(ra);
6807
6808 // Call the runtime system in a fresh internal frame.
6809 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
6810 masm->isolate());
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00006811 {
6812 FrameScope scope(masm, StackFrame::INTERNAL);
6813 __ Push(a1, a0);
6814 __ li(t0, Operand(Smi::FromInt(op_)));
6815 __ push(t0);
6816 __ CallExternalReference(miss, 3);
6817 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006818 // Compute the entry point of the rewritten stub.
6819 __ Addu(a2, v0, Operand(Code::kHeaderSize - kHeapObjectTag));
6820 // Restore registers.
6821 __ pop(ra);
6822 __ pop(a0);
6823 __ pop(a1);
6824 __ Jump(a2);
6825}
6826
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00006827
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006828void DirectCEntryStub::Generate(MacroAssembler* masm) {
6829 // No need to pop or drop anything, LeaveExitFrame will restore the old
6830 // stack, thus dropping the allocated space for the return value.
6831 // The saved ra is after the reserved stack space for the 4 args.
6832 __ lw(t9, MemOperand(sp, kCArgsSlotsSize));
6833
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00006834 if (FLAG_debug_code && FLAG_enable_slow_asserts) {
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006835 // In case of an error the return address may point to a memory area
6836 // filled with kZapValue by the GC.
6837 // Dereference the address and check for this.
6838 __ lw(t0, MemOperand(t9));
6839 __ Assert(ne, "Received invalid return address.", t0,
6840 Operand(reinterpret_cast<uint32_t>(kZapValue)));
6841 }
6842 __ Jump(t9);
lrn@chromium.org7516f052011-03-30 08:52:27 +00006843}
6844
6845
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006846void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
6847 ExternalReference function) {
6848 __ li(t9, Operand(function));
6849 this->GenerateCall(masm, t9);
6850}
6851
erik.corry@gmail.comd6076d92011-06-06 09:39:18 +00006852
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006853void DirectCEntryStub::GenerateCall(MacroAssembler* masm,
6854 Register target) {
6855 __ Move(t9, target);
6856 __ AssertStackIsAligned();
6857 // Allocate space for arg slots.
6858 __ Subu(sp, sp, kCArgsSlotsSize);
6859
6860 // Block the trampoline pool through the whole function to make sure the
6861 // number of generated instructions is constant.
6862 Assembler::BlockTrampolinePoolScope block_trampoline_pool(masm);
6863
6864 // We need to get the current 'pc' value, which is not available on MIPS.
6865 Label find_ra;
6866 masm->bal(&find_ra); // ra = pc + 8.
6867 masm->nop(); // Branch delay slot nop.
6868 masm->bind(&find_ra);
6869
6870 const int kNumInstructionsToJump = 6;
6871 masm->addiu(ra, ra, kNumInstructionsToJump * kPointerSize);
6872 // Push return address (accessible to GC through exit frame pc).
6873 // This spot for ra was reserved in EnterExitFrame.
6874 masm->sw(ra, MemOperand(sp, kCArgsSlotsSize));
6875 masm->li(ra, Operand(reinterpret_cast<intptr_t>(GetCode().location()),
6876 RelocInfo::CODE_TARGET), true);
6877 // Call the function.
6878 masm->Jump(t9);
6879 // Make sure the stored 'ra' points to this position.
6880 ASSERT_EQ(kNumInstructionsToJump, masm->InstructionsGeneratedSince(&find_ra));
6881}
6882
6883
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00006884void StringDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
6885 Label* miss,
6886 Label* done,
6887 Register receiver,
6888 Register properties,
6889 Handle<String> name,
6890 Register scratch0) {
6891 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6892 // not equal to the name and kProbes-th slot is not used (its name is the
6893 // undefined value), it guarantees the hash table doesn't contain the
6894 // property. It's true even if some slots represent deleted properties
6895 // (their names are the null value).
6896 for (int i = 0; i < kInlinedProbes; i++) {
6897 // scratch0 points to properties hash.
6898 // Compute the masked index: (hash + i + i * i) & mask.
6899 Register index = scratch0;
6900 // Capacity is smi 2^n.
6901 __ lw(index, FieldMemOperand(properties, kCapacityOffset));
6902 __ Subu(index, index, Operand(1));
6903 __ And(index, index, Operand(
6904 Smi::FromInt(name->Hash() + StringDictionary::GetProbeOffset(i))));
6905
6906 // Scale the index by multiplying by the entry size.
6907 ASSERT(StringDictionary::kEntrySize == 3);
6908 __ sll(at, index, 1);
6909 __ Addu(index, index, at);
6910
6911 Register entity_name = scratch0;
6912 // Having undefined at this place means the name is not contained.
6913 ASSERT_EQ(kSmiTagSize, 1);
6914 Register tmp = properties;
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006915 __ sll(scratch0, index, 1);
6916 __ Addu(tmp, properties, scratch0);
6917 __ lw(entity_name, FieldMemOperand(tmp, kElementsStartOffset));
6918
6919 ASSERT(!tmp.is(entity_name));
6920 __ LoadRoot(tmp, Heap::kUndefinedValueRootIndex);
6921 __ Branch(done, eq, entity_name, Operand(tmp));
6922
6923 if (i != kInlinedProbes - 1) {
6924 // Stop if found the property.
6925 __ Branch(miss, eq, entity_name, Operand(Handle<String>(name)));
6926
6927 // Check if the entry name is not a symbol.
6928 __ lw(entity_name, FieldMemOperand(entity_name, HeapObject::kMapOffset));
6929 __ lbu(entity_name,
6930 FieldMemOperand(entity_name, Map::kInstanceTypeOffset));
6931 __ And(scratch0, entity_name, Operand(kIsSymbolMask));
6932 __ Branch(miss, eq, scratch0, Operand(zero_reg));
6933
6934 // Restore the properties.
6935 __ lw(properties,
6936 FieldMemOperand(receiver, JSObject::kPropertiesOffset));
6937 }
6938 }
6939
6940 const int spill_mask =
6941 (ra.bit() | t2.bit() | t1.bit() | t0.bit() | a3.bit() |
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00006942 a2.bit() | a1.bit() | a0.bit() | v0.bit());
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006943
6944 __ MultiPush(spill_mask);
6945 __ lw(a0, FieldMemOperand(receiver, JSObject::kPropertiesOffset));
6946 __ li(a1, Operand(Handle<String>(name)));
6947 StringDictionaryLookupStub stub(NEGATIVE_LOOKUP);
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00006948 __ CallStub(&stub);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00006949 __ mov(at, v0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006950 __ MultiPop(spill_mask);
6951
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00006952 __ Branch(done, eq, at, Operand(zero_reg));
6953 __ Branch(miss, ne, at, Operand(zero_reg));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006954}
6955
6956
6957// Probe the string dictionary in the |elements| register. Jump to the
6958// |done| label if a property with the given name is found. Jump to
6959// the |miss| label otherwise.
6960// If lookup was successful |scratch2| will be equal to elements + 4 * index.
6961void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6962 Label* miss,
6963 Label* done,
6964 Register elements,
6965 Register name,
6966 Register scratch1,
6967 Register scratch2) {
jkummerow@chromium.orgc3b37122011-11-07 10:14:12 +00006968 ASSERT(!elements.is(scratch1));
6969 ASSERT(!elements.is(scratch2));
6970 ASSERT(!name.is(scratch1));
6971 ASSERT(!name.is(scratch2));
6972
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00006973 // Assert that name contains a string.
6974 if (FLAG_debug_code) __ AbortIfNotString(name);
6975
6976 // Compute the capacity mask.
6977 __ lw(scratch1, FieldMemOperand(elements, kCapacityOffset));
6978 __ sra(scratch1, scratch1, kSmiTagSize); // convert smi to int
6979 __ Subu(scratch1, scratch1, Operand(1));
6980
6981 // Generate an unrolled loop that performs a few probes before
6982 // giving up. Measurements done on Gmail indicate that 2 probes
6983 // cover ~93% of loads from dictionaries.
6984 for (int i = 0; i < kInlinedProbes; i++) {
6985 // Compute the masked index: (hash + i + i * i) & mask.
6986 __ lw(scratch2, FieldMemOperand(name, String::kHashFieldOffset));
6987 if (i > 0) {
6988 // Add the probe offset (i + i * i) left shifted to avoid right shifting
6989 // the hash in a separate instruction. The value hash + i + i * i is right
6990 // shifted in the following and instruction.
6991 ASSERT(StringDictionary::GetProbeOffset(i) <
6992 1 << (32 - String::kHashFieldOffset));
6993 __ Addu(scratch2, scratch2, Operand(
6994 StringDictionary::GetProbeOffset(i) << String::kHashShift));
6995 }
6996 __ srl(scratch2, scratch2, String::kHashShift);
6997 __ And(scratch2, scratch1, scratch2);
6998
6999 // Scale the index by multiplying by the element size.
7000 ASSERT(StringDictionary::kEntrySize == 3);
7001 // scratch2 = scratch2 * 3.
7002
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00007003 __ sll(at, scratch2, 1);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00007004 __ Addu(scratch2, scratch2, at);
7005
7006 // Check if the key is identical to the name.
7007 __ sll(at, scratch2, 2);
7008 __ Addu(scratch2, elements, at);
7009 __ lw(at, FieldMemOperand(scratch2, kElementsStartOffset));
7010 __ Branch(done, eq, name, Operand(at));
7011 }
7012
7013 const int spill_mask =
7014 (ra.bit() | t2.bit() | t1.bit() | t0.bit() |
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00007015 a3.bit() | a2.bit() | a1.bit() | a0.bit() | v0.bit()) &
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00007016 ~(scratch1.bit() | scratch2.bit());
7017
7018 __ MultiPush(spill_mask);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00007019 if (name.is(a0)) {
7020 ASSERT(!elements.is(a1));
7021 __ Move(a1, name);
7022 __ Move(a0, elements);
7023 } else {
7024 __ Move(a0, elements);
7025 __ Move(a1, name);
7026 }
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00007027 StringDictionaryLookupStub stub(POSITIVE_LOOKUP);
7028 __ CallStub(&stub);
7029 __ mov(scratch2, a2);
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00007030 __ mov(at, v0);
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00007031 __ MultiPop(spill_mask);
7032
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00007033 __ Branch(done, ne, at, Operand(zero_reg));
7034 __ Branch(miss, eq, at, Operand(zero_reg));
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00007035}
7036
7037
7038void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
erik.corry@gmail.comc3b670f2011-10-05 21:44:48 +00007039 // This stub overrides SometimesSetsUpAFrame() to return false. That means
7040 // we cannot call anything that could cause a GC from this stub.
vegorov@chromium.org7304bca2011-05-16 12:14:13 +00007041 // Registers:
7042 // result: StringDictionary to probe
7043 // a1: key
7044 // : StringDictionary to probe.
7045 // index_: will hold an index of entry if lookup is successful.
7046 // might alias with result_.
7047 // Returns:
7048 // result_ is zero if lookup failed, non zero otherwise.
7049
7050 Register result = v0;
7051 Register dictionary = a0;
7052 Register key = a1;
7053 Register index = a2;
7054 Register mask = a3;
7055 Register hash = t0;
7056 Register undefined = t1;
7057 Register entry_key = t2;
7058
7059 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
7060
7061 __ lw(mask, FieldMemOperand(dictionary, kCapacityOffset));
7062 __ sra(mask, mask, kSmiTagSize);
7063 __ Subu(mask, mask, Operand(1));
7064
7065 __ lw(hash, FieldMemOperand(key, String::kHashFieldOffset));
7066
7067 __ LoadRoot(undefined, Heap::kUndefinedValueRootIndex);
7068
7069 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
7070 // Compute the masked index: (hash + i + i * i) & mask.
7071 // Capacity is smi 2^n.
7072 if (i > 0) {
7073 // Add the probe offset (i + i * i) left shifted to avoid right shifting
7074 // the hash in a separate instruction. The value hash + i + i * i is right
7075 // shifted in the following and instruction.
7076 ASSERT(StringDictionary::GetProbeOffset(i) <
7077 1 << (32 - String::kHashFieldOffset));
7078 __ Addu(index, hash, Operand(
7079 StringDictionary::GetProbeOffset(i) << String::kHashShift));
7080 } else {
7081 __ mov(index, hash);
7082 }
7083 __ srl(index, index, String::kHashShift);
7084 __ And(index, mask, index);
7085
7086 // Scale the index by multiplying by the entry size.
7087 ASSERT(StringDictionary::kEntrySize == 3);
7088 // index *= 3.
7089 __ mov(at, index);
7090 __ sll(index, index, 1);
7091 __ Addu(index, index, at);
7092
7093
7094 ASSERT_EQ(kSmiTagSize, 1);
7095 __ sll(index, index, 2);
7096 __ Addu(index, index, dictionary);
7097 __ lw(entry_key, FieldMemOperand(index, kElementsStartOffset));
7098
7099 // Having undefined at this place means the name is not contained.
7100 __ Branch(&not_in_dictionary, eq, entry_key, Operand(undefined));
7101
7102 // Stop if found the property.
7103 __ Branch(&in_dictionary, eq, entry_key, Operand(key));
7104
7105 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
7106 // Check if the entry name is not a symbol.
7107 __ lw(entry_key, FieldMemOperand(entry_key, HeapObject::kMapOffset));
7108 __ lbu(entry_key,
7109 FieldMemOperand(entry_key, Map::kInstanceTypeOffset));
7110 __ And(result, entry_key, Operand(kIsSymbolMask));
7111 __ Branch(&maybe_in_dictionary, eq, result, Operand(zero_reg));
7112 }
7113 }
7114
7115 __ bind(&maybe_in_dictionary);
7116 // If we are doing negative lookup then probing failure should be
7117 // treated as a lookup success. For positive lookup probing failure
7118 // should be treated as lookup failure.
7119 if (mode_ == POSITIVE_LOOKUP) {
7120 __ mov(result, zero_reg);
7121 __ Ret();
7122 }
7123
7124 __ bind(&in_dictionary);
7125 __ li(result, 1);
7126 __ Ret();
7127
7128 __ bind(&not_in_dictionary);
7129 __ mov(result, zero_reg);
7130 __ Ret();
lrn@chromium.org7516f052011-03-30 08:52:27 +00007131}
7132
7133
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00007134struct AheadOfTimeWriteBarrierStubList {
7135 Register object, value, address;
7136 RememberedSetAction action;
7137};
7138
7139
7140struct AheadOfTimeWriteBarrierStubList kAheadOfTime[] = {
7141 // Used in RegExpExecStub.
7142 { s2, s0, t3, EMIT_REMEMBERED_SET },
7143 { s2, a2, t3, EMIT_REMEMBERED_SET },
7144 // Used in CompileArrayPushCall.
7145 // Also used in StoreIC::GenerateNormal via GenerateDictionaryStore.
7146 // Also used in KeyedStoreIC::GenerateGeneric.
7147 { a3, t0, t1, EMIT_REMEMBERED_SET },
7148 // Used in CompileStoreGlobal.
7149 { t0, a1, a2, OMIT_REMEMBERED_SET },
7150 // Used in StoreStubCompiler::CompileStoreField via GenerateStoreField.
7151 { a1, a2, a3, EMIT_REMEMBERED_SET },
7152 { a3, a2, a1, EMIT_REMEMBERED_SET },
7153 // Used in KeyedStoreStubCompiler::CompileStoreField via GenerateStoreField.
7154 { a2, a1, a3, EMIT_REMEMBERED_SET },
7155 { a3, a1, a2, EMIT_REMEMBERED_SET },
7156 // KeyedStoreStubCompiler::GenerateStoreFastElement.
7157 { t0, a2, a3, EMIT_REMEMBERED_SET },
erik.corry@gmail.com394dbcf2011-10-27 07:38:48 +00007158 // ElementsTransitionGenerator::GenerateSmiOnlyToObject
7159 // and ElementsTransitionGenerator::GenerateSmiOnlyToDouble
7160 // and ElementsTransitionGenerator::GenerateDoubleToObject
7161 { a2, a3, t5, EMIT_REMEMBERED_SET },
7162 // ElementsTransitionGenerator::GenerateDoubleToObject
7163 { t2, a2, a0, EMIT_REMEMBERED_SET },
7164 { a2, t2, t5, EMIT_REMEMBERED_SET },
erikcorry0ad885c2011-11-21 13:51:57 +00007165 // StoreArrayLiteralElementStub::Generate
7166 { t1, a0, t2, EMIT_REMEMBERED_SET },
rossberg@chromium.orgb4b2aa62011-10-13 09:49:59 +00007167 // Null termination.
7168 { no_reg, no_reg, no_reg, EMIT_REMEMBERED_SET}
7169};
7170
7171
7172bool RecordWriteStub::IsPregenerated() {
7173 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
7174 !entry->object.is(no_reg);
7175 entry++) {
7176 if (object_.is(entry->object) &&
7177 value_.is(entry->value) &&
7178 address_.is(entry->address) &&
7179 remembered_set_action_ == entry->action &&
7180 save_fp_regs_mode_ == kDontSaveFPRegs) {
7181 return true;
7182 }
7183 }
7184 return false;
7185}
7186
7187
7188bool StoreBufferOverflowStub::IsPregenerated() {
7189 return save_doubles_ == kDontSaveFPRegs || ISOLATE->fp_stubs_generated();
7190}
7191
7192
7193void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime() {
7194 StoreBufferOverflowStub stub1(kDontSaveFPRegs);
7195 stub1.GetCode()->set_is_pregenerated(true);
7196}
7197
7198
7199void RecordWriteStub::GenerateFixedRegStubsAheadOfTime() {
7200 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
7201 !entry->object.is(no_reg);
7202 entry++) {
7203 RecordWriteStub stub(entry->object,
7204 entry->value,
7205 entry->address,
7206 entry->action,
7207 kDontSaveFPRegs);
7208 stub.GetCode()->set_is_pregenerated(true);
7209 }
7210}
7211
7212
7213// Takes the input in 3 registers: address_ value_ and object_. A pointer to
7214// the value has just been written into the object, now this stub makes sure
7215// we keep the GC informed. The word in the object where the value has been
7216// written is in the address register.
7217void RecordWriteStub::Generate(MacroAssembler* masm) {
7218 Label skip_to_incremental_noncompacting;
7219 Label skip_to_incremental_compacting;
7220
7221 // The first two branch+nop instructions are generated with labels so as to
7222 // get the offset fixed up correctly by the bind(Label*) call. We patch it
7223 // back and forth between a "bne zero_reg, zero_reg, ..." (a nop in this
7224 // position) and the "beq zero_reg, zero_reg, ..." when we start and stop
7225 // incremental heap marking.
7226 // See RecordWriteStub::Patch for details.
7227 __ beq(zero_reg, zero_reg, &skip_to_incremental_noncompacting);
7228 __ nop();
7229 __ beq(zero_reg, zero_reg, &skip_to_incremental_compacting);
7230 __ nop();
7231
7232 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
7233 __ RememberedSetHelper(object_,
7234 address_,
7235 value_,
7236 save_fp_regs_mode_,
7237 MacroAssembler::kReturnAtEnd);
7238 }
7239 __ Ret();
7240
7241 __ bind(&skip_to_incremental_noncompacting);
7242 GenerateIncremental(masm, INCREMENTAL);
7243
7244 __ bind(&skip_to_incremental_compacting);
7245 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
7246
7247 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
7248 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
7249
7250 PatchBranchIntoNop(masm, 0);
7251 PatchBranchIntoNop(masm, 2 * Assembler::kInstrSize);
7252}
7253
7254
7255void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
7256 regs_.Save(masm);
7257
7258 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
7259 Label dont_need_remembered_set;
7260
7261 __ lw(regs_.scratch0(), MemOperand(regs_.address(), 0));
7262 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
7263 regs_.scratch0(),
7264 &dont_need_remembered_set);
7265
7266 __ CheckPageFlag(regs_.object(),
7267 regs_.scratch0(),
7268 1 << MemoryChunk::SCAN_ON_SCAVENGE,
7269 ne,
7270 &dont_need_remembered_set);
7271
7272 // First notify the incremental marker if necessary, then update the
7273 // remembered set.
7274 CheckNeedsToInformIncrementalMarker(
7275 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
7276 InformIncrementalMarker(masm, mode);
7277 regs_.Restore(masm);
7278 __ RememberedSetHelper(object_,
7279 address_,
7280 value_,
7281 save_fp_regs_mode_,
7282 MacroAssembler::kReturnAtEnd);
7283
7284 __ bind(&dont_need_remembered_set);
7285 }
7286
7287 CheckNeedsToInformIncrementalMarker(
7288 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
7289 InformIncrementalMarker(masm, mode);
7290 regs_.Restore(masm);
7291 __ Ret();
7292}
7293
7294
7295void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm, Mode mode) {
7296 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode_);
7297 int argument_count = 3;
7298 __ PrepareCallCFunction(argument_count, regs_.scratch0());
7299 Register address =
7300 a0.is(regs_.address()) ? regs_.scratch0() : regs_.address();
7301 ASSERT(!address.is(regs_.object()));
7302 ASSERT(!address.is(a0));
7303 __ Move(address, regs_.address());
7304 __ Move(a0, regs_.object());
7305 if (mode == INCREMENTAL_COMPACTION) {
7306 __ Move(a1, address);
7307 } else {
7308 ASSERT(mode == INCREMENTAL);
7309 __ lw(a1, MemOperand(address, 0));
7310 }
7311 __ li(a2, Operand(ExternalReference::isolate_address()));
7312
7313 AllowExternalCallThatCantCauseGC scope(masm);
7314 if (mode == INCREMENTAL_COMPACTION) {
7315 __ CallCFunction(
7316 ExternalReference::incremental_evacuation_record_write_function(
7317 masm->isolate()),
7318 argument_count);
7319 } else {
7320 ASSERT(mode == INCREMENTAL);
7321 __ CallCFunction(
7322 ExternalReference::incremental_marking_record_write_function(
7323 masm->isolate()),
7324 argument_count);
7325 }
7326 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode_);
7327}
7328
7329
7330void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
7331 MacroAssembler* masm,
7332 OnNoNeedToInformIncrementalMarker on_no_need,
7333 Mode mode) {
7334 Label on_black;
7335 Label need_incremental;
7336 Label need_incremental_pop_scratch;
7337
7338 // Let's look at the color of the object: If it is not black we don't have
7339 // to inform the incremental marker.
7340 __ JumpIfBlack(regs_.object(), regs_.scratch0(), regs_.scratch1(), &on_black);
7341
7342 regs_.Restore(masm);
7343 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
7344 __ RememberedSetHelper(object_,
7345 address_,
7346 value_,
7347 save_fp_regs_mode_,
7348 MacroAssembler::kReturnAtEnd);
7349 } else {
7350 __ Ret();
7351 }
7352
7353 __ bind(&on_black);
7354
7355 // Get the value from the slot.
7356 __ lw(regs_.scratch0(), MemOperand(regs_.address(), 0));
7357
7358 if (mode == INCREMENTAL_COMPACTION) {
7359 Label ensure_not_white;
7360
7361 __ CheckPageFlag(regs_.scratch0(), // Contains value.
7362 regs_.scratch1(), // Scratch.
7363 MemoryChunk::kEvacuationCandidateMask,
7364 eq,
7365 &ensure_not_white);
7366
7367 __ CheckPageFlag(regs_.object(),
7368 regs_.scratch1(), // Scratch.
7369 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
7370 eq,
7371 &need_incremental);
7372
7373 __ bind(&ensure_not_white);
7374 }
7375
7376 // We need extra registers for this, so we push the object and the address
7377 // register temporarily.
7378 __ Push(regs_.object(), regs_.address());
7379 __ EnsureNotWhite(regs_.scratch0(), // The value.
7380 regs_.scratch1(), // Scratch.
7381 regs_.object(), // Scratch.
7382 regs_.address(), // Scratch.
7383 &need_incremental_pop_scratch);
7384 __ Pop(regs_.object(), regs_.address());
7385
7386 regs_.Restore(masm);
7387 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
7388 __ RememberedSetHelper(object_,
7389 address_,
7390 value_,
7391 save_fp_regs_mode_,
7392 MacroAssembler::kReturnAtEnd);
7393 } else {
7394 __ Ret();
7395 }
7396
7397 __ bind(&need_incremental_pop_scratch);
7398 __ Pop(regs_.object(), regs_.address());
7399
7400 __ bind(&need_incremental);
7401
7402 // Fall through when we need to inform the incremental marker.
7403}
7404
7405
erikcorry0ad885c2011-11-21 13:51:57 +00007406void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
7407 // ----------- S t a t e -------------
7408 // -- a0 : element value to store
7409 // -- a1 : array literal
7410 // -- a2 : map of array literal
7411 // -- a3 : element index as smi
7412 // -- t0 : array literal index in function as smi
7413 // -----------------------------------
7414
7415 Label element_done;
7416 Label double_elements;
7417 Label smi_element;
7418 Label slow_elements;
7419 Label fast_elements;
7420
7421 __ CheckFastElements(a2, t1, &double_elements);
7422 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
7423 __ JumpIfSmi(a0, &smi_element);
7424 __ CheckFastSmiOnlyElements(a2, t1, &fast_elements);
7425
7426 // Store into the array literal requires a elements transition. Call into
7427 // the runtime.
7428 __ bind(&slow_elements);
7429 // call.
7430 __ Push(a1, a3, a0);
7431 __ lw(t1, MemOperand(fp, JavaScriptFrameConstants::kFunctionOffset));
7432 __ lw(t1, FieldMemOperand(t1, JSFunction::kLiteralsOffset));
7433 __ Push(t1, t0);
7434 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
7435
7436 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
7437 __ bind(&fast_elements);
7438 __ lw(t1, FieldMemOperand(a1, JSObject::kElementsOffset));
7439 __ sll(t2, a3, kPointerSizeLog2 - kSmiTagSize);
7440 __ Addu(t2, t1, t2);
7441 __ Addu(t2, t2, Operand(FixedArray::kHeaderSize - kHeapObjectTag));
7442 __ sw(a0, MemOperand(t2, 0));
7443 // Update the write barrier for the array store.
7444 __ RecordWrite(t1, t2, a0, kRAHasNotBeenSaved, kDontSaveFPRegs,
7445 EMIT_REMEMBERED_SET, OMIT_SMI_CHECK);
7446 __ Ret();
7447
7448 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
7449 // FAST_ELEMENTS, and value is Smi.
7450 __ bind(&smi_element);
7451 __ lw(t1, FieldMemOperand(a1, JSObject::kElementsOffset));
7452 __ sll(t2, a3, kPointerSizeLog2 - kSmiTagSize);
7453 __ Addu(t2, t1, t2);
7454 __ sw(a0, FieldMemOperand(t2, FixedArray::kHeaderSize));
7455 __ Ret();
7456
7457 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
7458 __ bind(&double_elements);
7459 __ lw(t1, FieldMemOperand(a1, JSObject::kElementsOffset));
7460 __ StoreNumberToDoubleElements(a0, a3, a1, t1, t2, t3, t5, t6,
7461 &slow_elements);
7462 __ Ret();
7463}
7464
7465
lrn@chromium.org7516f052011-03-30 08:52:27 +00007466#undef __
7467
7468} } // namespace v8::internal
7469
7470#endif // V8_TARGET_ARCH_MIPS