blob: df4438b7341c56a622dbde7e3f1154b65e4dcfa6 [file] [log] [blame]
Steve Block1e0659c2011-05-24 12:43:12 +01001// Copyright 2011 the V8 project authors. All rights reserved.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
29
30#if defined(V8_TARGET_ARCH_X64)
31
32#include "bootstrapper.h"
33#include "code-stubs.h"
34#include "regexp-macro-assembler.h"
35
36namespace v8 {
37namespace internal {
38
39#define __ ACCESS_MASM(masm)
Steve Block1e0659c2011-05-24 12:43:12 +010040
41void ToNumberStub::Generate(MacroAssembler* masm) {
42 // The ToNumber stub takes one argument in eax.
Ben Murdoch257744e2011-11-30 15:57:28 +000043 Label check_heap_number, call_builtin;
Steve Block1e0659c2011-05-24 12:43:12 +010044 __ SmiTest(rax);
Ben Murdoch257744e2011-11-30 15:57:28 +000045 __ j(not_zero, &check_heap_number, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +010046 __ Ret();
47
48 __ bind(&check_heap_number);
Steve Block44f0eee2011-05-26 01:26:41 +010049 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
50 Heap::kHeapNumberMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +000051 __ j(not_equal, &call_builtin, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +010052 __ Ret();
53
54 __ bind(&call_builtin);
55 __ pop(rcx); // Pop return address.
56 __ push(rax);
57 __ push(rcx); // Push return address.
58 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
59}
60
61
Kristian Monsen80d68ea2010-09-08 11:05:35 +010062void FastNewClosureStub::Generate(MacroAssembler* masm) {
63 // Create a new closure from the given function info in new
64 // space. Set the context to the current context in rsi.
65 Label gc;
66 __ AllocateInNewSpace(JSFunction::kSize, rax, rbx, rcx, &gc, TAG_OBJECT);
67
68 // Get the function info from the stack.
69 __ movq(rdx, Operand(rsp, 1 * kPointerSize));
70
Steve Block44f0eee2011-05-26 01:26:41 +010071 int map_index = strict_mode_ == kStrictMode
72 ? Context::STRICT_MODE_FUNCTION_MAP_INDEX
73 : Context::FUNCTION_MAP_INDEX;
74
Kristian Monsen80d68ea2010-09-08 11:05:35 +010075 // Compute the function map in the current global context and set that
76 // as the map of the allocated object.
77 __ movq(rcx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
78 __ movq(rcx, FieldOperand(rcx, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +010079 __ movq(rcx, Operand(rcx, Context::SlotOffset(map_index)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +010080 __ movq(FieldOperand(rax, JSObject::kMapOffset), rcx);
81
82 // Initialize the rest of the function. We don't have to update the
83 // write barrier because the allocated object is in new space.
84 __ LoadRoot(rbx, Heap::kEmptyFixedArrayRootIndex);
85 __ LoadRoot(rcx, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +010086 __ LoadRoot(rdi, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010087 __ movq(FieldOperand(rax, JSObject::kPropertiesOffset), rbx);
88 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rbx);
89 __ movq(FieldOperand(rax, JSFunction::kPrototypeOrInitialMapOffset), rcx);
90 __ movq(FieldOperand(rax, JSFunction::kSharedFunctionInfoOffset), rdx);
91 __ movq(FieldOperand(rax, JSFunction::kContextOffset), rsi);
92 __ movq(FieldOperand(rax, JSFunction::kLiteralsOffset), rbx);
Ben Murdochb0fe1622011-05-05 13:52:32 +010093 __ movq(FieldOperand(rax, JSFunction::kNextFunctionLinkOffset), rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +010094
95 // Initialize the code pointer in the function to be the one
96 // found in the shared function info object.
97 __ movq(rdx, FieldOperand(rdx, SharedFunctionInfo::kCodeOffset));
98 __ lea(rdx, FieldOperand(rdx, Code::kHeaderSize));
99 __ movq(FieldOperand(rax, JSFunction::kCodeEntryOffset), rdx);
100
101
102 // Return and remove the on-stack parameter.
103 __ ret(1 * kPointerSize);
104
105 // Create a new closure through the slower runtime call.
106 __ bind(&gc);
107 __ pop(rcx); // Temporarily remove return address.
108 __ pop(rdx);
109 __ push(rsi);
110 __ push(rdx);
Steve Block44f0eee2011-05-26 01:26:41 +0100111 __ PushRoot(Heap::kFalseValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100112 __ push(rcx); // Restore return address.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800113 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100114}
115
116
117void FastNewContextStub::Generate(MacroAssembler* masm) {
118 // Try to allocate the context in new space.
119 Label gc;
120 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
121 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
122 rax, rbx, rcx, &gc, TAG_OBJECT);
123
124 // Get the function from the stack.
125 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
126
127 // Setup the object header.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000128 __ LoadRoot(kScratchRegister, Heap::kFunctionContextMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100129 __ movq(FieldOperand(rax, HeapObject::kMapOffset), kScratchRegister);
130 __ Move(FieldOperand(rax, FixedArray::kLengthOffset), Smi::FromInt(length));
131
132 // Setup the fixed slots.
Steve Block9fac8402011-05-12 15:51:54 +0100133 __ Set(rbx, 0); // Set to NULL.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100134 __ movq(Operand(rax, Context::SlotOffset(Context::CLOSURE_INDEX)), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000135 __ movq(Operand(rax, Context::SlotOffset(Context::PREVIOUS_INDEX)), rsi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100136 __ movq(Operand(rax, Context::SlotOffset(Context::EXTENSION_INDEX)), rbx);
137
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000138 // Copy the global object from the previous context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100139 __ movq(rbx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
140 __ movq(Operand(rax, Context::SlotOffset(Context::GLOBAL_INDEX)), rbx);
141
142 // Initialize the rest of the slots to undefined.
143 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
144 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
145 __ movq(Operand(rax, Context::SlotOffset(i)), rbx);
146 }
147
148 // Return and remove the on-stack parameter.
149 __ movq(rsi, rax);
150 __ ret(1 * kPointerSize);
151
152 // Need to collect. Call into runtime system.
153 __ bind(&gc);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000154 __ TailCallRuntime(Runtime::kNewFunctionContext, 1, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100155}
156
157
158void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
159 // Stack layout on entry:
160 //
161 // [rsp + kPointerSize]: constant elements.
162 // [rsp + (2 * kPointerSize)]: literal index.
163 // [rsp + (3 * kPointerSize)]: literals array.
164
165 // All sizes here are multiples of kPointerSize.
166 int elements_size = (length_ > 0) ? FixedArray::SizeFor(length_) : 0;
167 int size = JSArray::kSize + elements_size;
168
169 // Load boilerplate object into rcx and check if we need to create a
170 // boilerplate.
171 Label slow_case;
172 __ movq(rcx, Operand(rsp, 3 * kPointerSize));
173 __ movq(rax, Operand(rsp, 2 * kPointerSize));
174 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
175 __ movq(rcx,
176 FieldOperand(rcx, index.reg, index.scale, FixedArray::kHeaderSize));
177 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
178 __ j(equal, &slow_case);
179
180 if (FLAG_debug_code) {
181 const char* message;
182 Heap::RootListIndex expected_map_index;
183 if (mode_ == CLONE_ELEMENTS) {
184 message = "Expected (writable) fixed array";
185 expected_map_index = Heap::kFixedArrayMapRootIndex;
186 } else {
187 ASSERT(mode_ == COPY_ON_WRITE_ELEMENTS);
188 message = "Expected copy-on-write fixed array";
189 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
190 }
191 __ push(rcx);
192 __ movq(rcx, FieldOperand(rcx, JSArray::kElementsOffset));
193 __ CompareRoot(FieldOperand(rcx, HeapObject::kMapOffset),
194 expected_map_index);
195 __ Assert(equal, message);
196 __ pop(rcx);
197 }
198
199 // Allocate both the JS array and the elements array in one big
200 // allocation. This avoids multiple limit checks.
201 __ AllocateInNewSpace(size, rax, rbx, rdx, &slow_case, TAG_OBJECT);
202
203 // Copy the JS array part.
204 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
205 if ((i != JSArray::kElementsOffset) || (length_ == 0)) {
206 __ movq(rbx, FieldOperand(rcx, i));
207 __ movq(FieldOperand(rax, i), rbx);
208 }
209 }
210
211 if (length_ > 0) {
212 // Get hold of the elements array of the boilerplate and setup the
213 // elements pointer in the resulting object.
214 __ movq(rcx, FieldOperand(rcx, JSArray::kElementsOffset));
215 __ lea(rdx, Operand(rax, JSArray::kSize));
216 __ movq(FieldOperand(rax, JSArray::kElementsOffset), rdx);
217
218 // Copy the elements array.
219 for (int i = 0; i < elements_size; i += kPointerSize) {
220 __ movq(rbx, FieldOperand(rcx, i));
221 __ movq(FieldOperand(rdx, i), rbx);
222 }
223 }
224
225 // Return and remove the on-stack parameters.
226 __ ret(3 * kPointerSize);
227
228 __ bind(&slow_case);
229 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
230}
231
232
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000233// The stub expects its argument on the stack and returns its result in tos_:
234// zero for false, and a non-zero value for true.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100235void ToBooleanStub::Generate(MacroAssembler* masm) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000236 Label patch;
237 const Register argument = rax;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000238 const Register map = rdx;
239
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000240 if (!types_.IsEmpty()) {
241 __ movq(argument, Operand(rsp, 1 * kPointerSize));
242 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100243
Ben Murdoch257744e2011-11-30 15:57:28 +0000244 // undefined -> false
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000245 CheckOddball(masm, UNDEFINED, Heap::kUndefinedValueRootIndex, false);
Ben Murdoch257744e2011-11-30 15:57:28 +0000246
247 // Boolean -> its value
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000248 CheckOddball(masm, BOOLEAN, Heap::kFalseValueRootIndex, false);
249 CheckOddball(masm, BOOLEAN, Heap::kTrueValueRootIndex, true);
Ben Murdoch257744e2011-11-30 15:57:28 +0000250
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000251 // 'null' -> false.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000252 CheckOddball(masm, NULL_TYPE, Heap::kNullValueRootIndex, false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100253
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000254 if (types_.Contains(SMI)) {
255 // Smis: 0 -> false, all other -> true
256 Label not_smi;
257 __ JumpIfNotSmi(argument, &not_smi, Label::kNear);
258 // argument contains the correct return value already
259 if (!tos_.is(argument)) {
260 __ movq(tos_, argument);
261 }
262 __ ret(1 * kPointerSize);
263 __ bind(&not_smi);
264 } else if (types_.NeedsMap()) {
265 // If we need a map later and have a Smi -> patch.
266 __ JumpIfSmi(argument, &patch, Label::kNear);
267 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100268
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000269 if (types_.NeedsMap()) {
270 __ movq(map, FieldOperand(argument, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100271
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000272 if (types_.CanBeUndetectable()) {
273 __ testb(FieldOperand(map, Map::kBitFieldOffset),
274 Immediate(1 << Map::kIsUndetectable));
275 // Undetectable -> false.
276 Label not_undetectable;
277 __ j(zero, &not_undetectable, Label::kNear);
278 __ Set(tos_, 0);
279 __ ret(1 * kPointerSize);
280 __ bind(&not_undetectable);
281 }
282 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100283
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000284 if (types_.Contains(SPEC_OBJECT)) {
285 // spec object -> true.
286 Label not_js_object;
287 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
288 __ j(below, &not_js_object, Label::kNear);
289 // argument contains the correct return value already.
290 if (!tos_.is(argument)) {
291 __ Set(tos_, 1);
292 }
293 __ ret(1 * kPointerSize);
294 __ bind(&not_js_object);
295 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100296
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000297 if (types_.Contains(STRING)) {
298 // String value -> false iff empty.
299 Label not_string;
300 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
301 __ j(above_equal, &not_string, Label::kNear);
302 __ movq(tos_, FieldOperand(argument, String::kLengthOffset));
303 __ ret(1 * kPointerSize); // the string length is OK as the return value
304 __ bind(&not_string);
305 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100306
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000307 if (types_.Contains(HEAP_NUMBER)) {
308 // heap number -> false iff +0, -0, or NaN.
309 Label not_heap_number, false_result;
310 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
311 __ j(not_equal, &not_heap_number, Label::kNear);
312 __ xorps(xmm0, xmm0);
313 __ ucomisd(xmm0, FieldOperand(argument, HeapNumber::kValueOffset));
314 __ j(zero, &false_result, Label::kNear);
315 // argument contains the correct return value already.
316 if (!tos_.is(argument)) {
317 __ Set(tos_, 1);
318 }
319 __ ret(1 * kPointerSize);
320 __ bind(&false_result);
321 __ Set(tos_, 0);
322 __ ret(1 * kPointerSize);
323 __ bind(&not_heap_number);
324 }
325
326 __ bind(&patch);
327 GenerateTypeTransition(masm);
328}
329
330
331void ToBooleanStub::CheckOddball(MacroAssembler* masm,
332 Type type,
333 Heap::RootListIndex value,
334 bool result) {
335 const Register argument = rax;
336 if (types_.Contains(type)) {
337 // If we see an expected oddball, return its ToBoolean value tos_.
338 Label different_value;
339 __ CompareRoot(argument, value);
340 __ j(not_equal, &different_value, Label::kNear);
341 if (!result) {
342 // If we have to return zero, there is no way around clearing tos_.
343 __ Set(tos_, 0);
344 } else if (!tos_.is(argument)) {
345 // If we have to return non-zero, we can re-use the argument if it is the
346 // same register as the result, because we never see Smi-zero here.
347 __ Set(tos_, 1);
348 }
349 __ ret(1 * kPointerSize);
350 __ bind(&different_value);
351 }
352}
353
354
355void ToBooleanStub::GenerateTypeTransition(MacroAssembler* masm) {
356 __ pop(rcx); // Get return address, operand is now on top of stack.
357 __ Push(Smi::FromInt(tos_.code()));
358 __ Push(Smi::FromInt(types_.ToByte()));
359 __ push(rcx); // Push return address.
360 // Patch the caller to an appropriate specialized stub and return the
361 // operation result to the caller of the stub.
362 __ TailCallExternalReference(
363 ExternalReference(IC_Utility(IC::kToBoolean_Patch), masm->isolate()),
364 3,
365 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100366}
367
368
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100369class FloatingPointHelper : public AllStatic {
370 public:
371 // Load the operands from rdx and rax into xmm0 and xmm1, as doubles.
372 // If the operands are not both numbers, jump to not_numbers.
373 // Leaves rdx and rax unchanged. SmiOperands assumes both are smis.
374 // NumberOperands assumes both are smis or heap numbers.
375 static void LoadSSE2SmiOperands(MacroAssembler* masm);
376 static void LoadSSE2NumberOperands(MacroAssembler* masm);
377 static void LoadSSE2UnknownOperands(MacroAssembler* masm,
378 Label* not_numbers);
379
380 // Takes the operands in rdx and rax and loads them as integers in rax
381 // and rcx.
382 static void LoadAsIntegers(MacroAssembler* masm,
383 Label* operand_conversion_failure,
384 Register heap_number_map);
385 // As above, but we know the operands to be numbers. In that case,
386 // conversion can't fail.
387 static void LoadNumbersAsIntegers(MacroAssembler* masm);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100388
389 // Tries to convert two values to smis losslessly.
390 // This fails if either argument is not a Smi nor a HeapNumber,
391 // or if it's a HeapNumber with a value that can't be converted
392 // losslessly to a Smi. In that case, control transitions to the
393 // on_not_smis label.
394 // On success, either control goes to the on_success label (if one is
395 // provided), or it falls through at the end of the code (if on_success
396 // is NULL).
397 // On success, both first and second holds Smi tagged values.
398 // One of first or second must be non-Smi when entering.
399 static void NumbersToSmis(MacroAssembler* masm,
400 Register first,
401 Register second,
402 Register scratch1,
403 Register scratch2,
404 Register scratch3,
405 Label* on_success,
406 Label* on_not_smis);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100407};
408
409
Ben Murdoch257744e2011-11-30 15:57:28 +0000410// Get the integer part of a heap number.
411// Overwrites the contents of rdi, rbx and rcx. Result cannot be rdi or rbx.
412void IntegerConvert(MacroAssembler* masm,
413 Register result,
414 Register source) {
415 // Result may be rcx. If result and source are the same register, source will
416 // be overwritten.
417 ASSERT(!result.is(rdi) && !result.is(rbx));
418 // TODO(lrn): When type info reaches here, if value is a 32-bit integer, use
419 // cvttsd2si (32-bit version) directly.
420 Register double_exponent = rbx;
421 Register double_value = rdi;
422 Label done, exponent_63_plus;
423 // Get double and extract exponent.
424 __ movq(double_value, FieldOperand(source, HeapNumber::kValueOffset));
425 // Clear result preemptively, in case we need to return zero.
426 __ xorl(result, result);
427 __ movq(xmm0, double_value); // Save copy in xmm0 in case we need it there.
428 // Double to remove sign bit, shift exponent down to least significant bits.
429 // and subtract bias to get the unshifted, unbiased exponent.
430 __ lea(double_exponent, Operand(double_value, double_value, times_1, 0));
431 __ shr(double_exponent, Immediate(64 - HeapNumber::kExponentBits));
432 __ subl(double_exponent, Immediate(HeapNumber::kExponentBias));
433 // Check whether the exponent is too big for a 63 bit unsigned integer.
434 __ cmpl(double_exponent, Immediate(63));
435 __ j(above_equal, &exponent_63_plus, Label::kNear);
436 // Handle exponent range 0..62.
437 __ cvttsd2siq(result, xmm0);
438 __ jmp(&done, Label::kNear);
439
440 __ bind(&exponent_63_plus);
441 // Exponent negative or 63+.
442 __ cmpl(double_exponent, Immediate(83));
443 // If exponent negative or above 83, number contains no significant bits in
444 // the range 0..2^31, so result is zero, and rcx already holds zero.
445 __ j(above, &done, Label::kNear);
446
447 // Exponent in rage 63..83.
448 // Mantissa * 2^exponent contains bits in the range 2^0..2^31, namely
449 // the least significant exponent-52 bits.
450
451 // Negate low bits of mantissa if value is negative.
452 __ addq(double_value, double_value); // Move sign bit to carry.
453 __ sbbl(result, result); // And convert carry to -1 in result register.
454 // if scratch2 is negative, do (scratch2-1)^-1, otherwise (scratch2-0)^0.
455 __ addl(double_value, result);
456 // Do xor in opposite directions depending on where we want the result
457 // (depending on whether result is rcx or not).
458
459 if (result.is(rcx)) {
460 __ xorl(double_value, result);
461 // Left shift mantissa by (exponent - mantissabits - 1) to save the
462 // bits that have positional values below 2^32 (the extra -1 comes from the
463 // doubling done above to move the sign bit into the carry flag).
464 __ leal(rcx, Operand(double_exponent, -HeapNumber::kMantissaBits - 1));
465 __ shll_cl(double_value);
466 __ movl(result, double_value);
467 } else {
468 // As the then-branch, but move double-value to result before shifting.
469 __ xorl(result, double_value);
470 __ leal(rcx, Operand(double_exponent, -HeapNumber::kMantissaBits - 1));
471 __ shll_cl(result);
472 }
473
474 __ bind(&done);
475}
476
477
Ben Murdoch257744e2011-11-30 15:57:28 +0000478void UnaryOpStub::Generate(MacroAssembler* masm) {
479 switch (operand_type_) {
480 case UnaryOpIC::UNINITIALIZED:
481 GenerateTypeTransition(masm);
482 break;
483 case UnaryOpIC::SMI:
484 GenerateSmiStub(masm);
485 break;
486 case UnaryOpIC::HEAP_NUMBER:
487 GenerateHeapNumberStub(masm);
488 break;
489 case UnaryOpIC::GENERIC:
490 GenerateGenericStub(masm);
491 break;
492 }
493}
494
495
496void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
497 __ pop(rcx); // Save return address.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000498
499 __ push(rax); // the operand
Ben Murdoch257744e2011-11-30 15:57:28 +0000500 __ Push(Smi::FromInt(op_));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000501 __ Push(Smi::FromInt(mode_));
Ben Murdoch257744e2011-11-30 15:57:28 +0000502 __ Push(Smi::FromInt(operand_type_));
503
504 __ push(rcx); // Push return address.
505
506 // Patch the caller to an appropriate specialized stub and return the
507 // operation result to the caller of the stub.
508 __ TailCallExternalReference(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000509 ExternalReference(IC_Utility(IC::kUnaryOp_Patch), masm->isolate()), 4, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000510}
511
512
513// TODO(svenpanne): Use virtual functions instead of switch.
514void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
515 switch (op_) {
516 case Token::SUB:
517 GenerateSmiStubSub(masm);
518 break;
519 case Token::BIT_NOT:
520 GenerateSmiStubBitNot(masm);
521 break;
522 default:
523 UNREACHABLE();
524 }
525}
526
527
528void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
529 Label slow;
530 GenerateSmiCodeSub(masm, &slow, &slow, Label::kNear, Label::kNear);
531 __ bind(&slow);
532 GenerateTypeTransition(masm);
533}
534
535
536void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
537 Label non_smi;
538 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
539 __ bind(&non_smi);
540 GenerateTypeTransition(masm);
541}
542
543
544void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
545 Label* non_smi,
546 Label* slow,
547 Label::Distance non_smi_near,
548 Label::Distance slow_near) {
549 Label done;
550 __ JumpIfNotSmi(rax, non_smi, non_smi_near);
551 __ SmiNeg(rax, rax, &done, Label::kNear);
552 __ jmp(slow, slow_near);
553 __ bind(&done);
554 __ ret(0);
555}
556
557
558void UnaryOpStub::GenerateSmiCodeBitNot(MacroAssembler* masm,
559 Label* non_smi,
560 Label::Distance non_smi_near) {
561 __ JumpIfNotSmi(rax, non_smi, non_smi_near);
562 __ SmiNot(rax, rax);
563 __ ret(0);
564}
565
566
567// TODO(svenpanne): Use virtual functions instead of switch.
568void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
569 switch (op_) {
570 case Token::SUB:
571 GenerateHeapNumberStubSub(masm);
572 break;
573 case Token::BIT_NOT:
574 GenerateHeapNumberStubBitNot(masm);
575 break;
576 default:
577 UNREACHABLE();
578 }
579}
580
581
582void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
583 Label non_smi, slow, call_builtin;
584 GenerateSmiCodeSub(masm, &non_smi, &call_builtin, Label::kNear);
585 __ bind(&non_smi);
586 GenerateHeapNumberCodeSub(masm, &slow);
587 __ bind(&slow);
588 GenerateTypeTransition(masm);
589 __ bind(&call_builtin);
590 GenerateGenericCodeFallback(masm);
591}
592
593
594void UnaryOpStub::GenerateHeapNumberStubBitNot(
595 MacroAssembler* masm) {
596 Label non_smi, slow;
597 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
598 __ bind(&non_smi);
599 GenerateHeapNumberCodeBitNot(masm, &slow);
600 __ bind(&slow);
601 GenerateTypeTransition(masm);
602}
603
604
605void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
606 Label* slow) {
607 // Check if the operand is a heap number.
608 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
609 Heap::kHeapNumberMapRootIndex);
610 __ j(not_equal, slow);
611
612 // Operand is a float, negate its value by flipping the sign bit.
613 if (mode_ == UNARY_OVERWRITE) {
614 __ Set(kScratchRegister, 0x01);
615 __ shl(kScratchRegister, Immediate(63));
616 __ xor_(FieldOperand(rax, HeapNumber::kValueOffset), kScratchRegister);
617 } else {
618 // Allocate a heap number before calculating the answer,
619 // so we don't have an untagged double around during GC.
620 Label slow_allocate_heapnumber, heapnumber_allocated;
621 __ AllocateHeapNumber(rcx, rbx, &slow_allocate_heapnumber);
622 __ jmp(&heapnumber_allocated);
623
624 __ bind(&slow_allocate_heapnumber);
625 __ EnterInternalFrame();
626 __ push(rax);
627 __ CallRuntime(Runtime::kNumberAlloc, 0);
628 __ movq(rcx, rax);
629 __ pop(rax);
630 __ LeaveInternalFrame();
631 __ bind(&heapnumber_allocated);
632 // rcx: allocated 'empty' number
633
634 // Copy the double value to the new heap number, flipping the sign.
635 __ movq(rdx, FieldOperand(rax, HeapNumber::kValueOffset));
636 __ Set(kScratchRegister, 0x01);
637 __ shl(kScratchRegister, Immediate(63));
638 __ xor_(rdx, kScratchRegister); // Flip sign.
639 __ movq(FieldOperand(rcx, HeapNumber::kValueOffset), rdx);
640 __ movq(rax, rcx);
641 }
642 __ ret(0);
643}
644
645
646void UnaryOpStub::GenerateHeapNumberCodeBitNot(MacroAssembler* masm,
647 Label* slow) {
648 // Check if the operand is a heap number.
649 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
650 Heap::kHeapNumberMapRootIndex);
651 __ j(not_equal, slow);
652
653 // Convert the heap number in rax to an untagged integer in rcx.
654 IntegerConvert(masm, rax, rax);
655
656 // Do the bitwise operation and smi tag the result.
657 __ notl(rax);
658 __ Integer32ToSmi(rax, rax);
659 __ ret(0);
660}
661
662
663// TODO(svenpanne): Use virtual functions instead of switch.
664void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
665 switch (op_) {
666 case Token::SUB:
667 GenerateGenericStubSub(masm);
668 break;
669 case Token::BIT_NOT:
670 GenerateGenericStubBitNot(masm);
671 break;
672 default:
673 UNREACHABLE();
674 }
675}
676
677
678void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
679 Label non_smi, slow;
680 GenerateSmiCodeSub(masm, &non_smi, &slow, Label::kNear);
681 __ bind(&non_smi);
682 GenerateHeapNumberCodeSub(masm, &slow);
683 __ bind(&slow);
684 GenerateGenericCodeFallback(masm);
685}
686
687
688void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
689 Label non_smi, slow;
690 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
691 __ bind(&non_smi);
692 GenerateHeapNumberCodeBitNot(masm, &slow);
693 __ bind(&slow);
694 GenerateGenericCodeFallback(masm);
695}
696
697
698void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
699 // Handle the slow case by jumping to the JavaScript builtin.
700 __ pop(rcx); // pop return address
701 __ push(rax);
702 __ push(rcx); // push return address
703 switch (op_) {
704 case Token::SUB:
705 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
706 break;
707 case Token::BIT_NOT:
708 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
709 break;
710 default:
711 UNREACHABLE();
712 }
713}
714
715
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000716void UnaryOpStub::PrintName(StringStream* stream) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000717 const char* op_name = Token::Name(op_);
718 const char* overwrite_name = NULL; // Make g++ happy.
719 switch (mode_) {
720 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
721 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
722 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000723 stream->Add("UnaryOpStub_%s_%s_%s",
724 op_name,
725 overwrite_name,
726 UnaryOpIC::GetName(operand_type_));
Ben Murdoch257744e2011-11-30 15:57:28 +0000727}
728
729
730void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100731 __ pop(rcx); // Save return address.
732 __ push(rdx);
733 __ push(rax);
734 // Left and right arguments are now on top.
735 // Push this stub's key. Although the operation and the type info are
736 // encoded into the key, the encoding is opaque, so push them too.
737 __ Push(Smi::FromInt(MinorKey()));
738 __ Push(Smi::FromInt(op_));
739 __ Push(Smi::FromInt(operands_type_));
740
741 __ push(rcx); // Push return address.
742
743 // Patch the caller to an appropriate specialized stub and return the
744 // operation result to the caller of the stub.
745 __ TailCallExternalReference(
Ben Murdoch257744e2011-11-30 15:57:28 +0000746 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
Steve Block44f0eee2011-05-26 01:26:41 +0100747 masm->isolate()),
Ben Murdoch086aeea2011-05-13 15:57:08 +0100748 5,
749 1);
750}
751
752
Ben Murdoch257744e2011-11-30 15:57:28 +0000753void BinaryOpStub::Generate(MacroAssembler* masm) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100754 switch (operands_type_) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000755 case BinaryOpIC::UNINITIALIZED:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100756 GenerateTypeTransition(masm);
757 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000758 case BinaryOpIC::SMI:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100759 GenerateSmiStub(masm);
760 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000761 case BinaryOpIC::INT32:
Steve Block1e0659c2011-05-24 12:43:12 +0100762 UNREACHABLE();
763 // The int32 case is identical to the Smi case. We avoid creating this
764 // ic state on x64.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100765 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000766 case BinaryOpIC::HEAP_NUMBER:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100767 GenerateHeapNumberStub(masm);
768 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000769 case BinaryOpIC::ODDBALL:
Steve Block44f0eee2011-05-26 01:26:41 +0100770 GenerateOddballStub(masm);
771 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000772 case BinaryOpIC::BOTH_STRING:
773 GenerateBothStringStub(masm);
774 break;
775 case BinaryOpIC::STRING:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100776 GenerateStringStub(masm);
777 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000778 case BinaryOpIC::GENERIC:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100779 GenerateGeneric(masm);
780 break;
781 default:
782 UNREACHABLE();
783 }
784}
785
786
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000787void BinaryOpStub::PrintName(StringStream* stream) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100788 const char* op_name = Token::Name(op_);
789 const char* overwrite_name;
790 switch (mode_) {
791 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
792 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
793 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
794 default: overwrite_name = "UnknownOverwrite"; break;
795 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000796 stream->Add("BinaryOpStub_%s_%s_%s",
797 op_name,
798 overwrite_name,
799 BinaryOpIC::GetName(operands_type_));
Ben Murdoch086aeea2011-05-13 15:57:08 +0100800}
801
802
Ben Murdoch257744e2011-11-30 15:57:28 +0000803void BinaryOpStub::GenerateSmiCode(
804 MacroAssembler* masm,
Ben Murdoch086aeea2011-05-13 15:57:08 +0100805 Label* slow,
806 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
Steve Block1e0659c2011-05-24 12:43:12 +0100807
Ben Murdoch257744e2011-11-30 15:57:28 +0000808 // Arguments to BinaryOpStub are in rdx and rax.
Steve Block1e0659c2011-05-24 12:43:12 +0100809 Register left = rdx;
810 Register right = rax;
811
Ben Murdoch8b112d22011-06-08 16:22:53 +0100812 // We only generate heapnumber answers for overflowing calculations
813 // for the four basic arithmetic operations and logical right shift by 0.
814 bool generate_inline_heapnumber_results =
815 (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) &&
816 (op_ == Token::ADD || op_ == Token::SUB ||
817 op_ == Token::MUL || op_ == Token::DIV || op_ == Token::SHR);
Steve Block1e0659c2011-05-24 12:43:12 +0100818
819 // Smi check of both operands. If op is BIT_OR, the check is delayed
820 // until after the OR operation.
821 Label not_smis;
822 Label use_fp_on_smis;
Ben Murdoch8b112d22011-06-08 16:22:53 +0100823 Label fail;
Steve Block1e0659c2011-05-24 12:43:12 +0100824
825 if (op_ != Token::BIT_OR) {
826 Comment smi_check_comment(masm, "-- Smi check arguments");
827 __ JumpIfNotBothSmi(left, right, &not_smis);
828 }
829
Ben Murdoch8b112d22011-06-08 16:22:53 +0100830 Label smi_values;
831 __ bind(&smi_values);
Steve Block1e0659c2011-05-24 12:43:12 +0100832 // Perform the operation.
833 Comment perform_smi(masm, "-- Perform smi operation");
834 switch (op_) {
835 case Token::ADD:
836 ASSERT(right.is(rax));
837 __ SmiAdd(right, right, left, &use_fp_on_smis); // ADD is commutative.
838 break;
839
840 case Token::SUB:
841 __ SmiSub(left, left, right, &use_fp_on_smis);
842 __ movq(rax, left);
843 break;
844
845 case Token::MUL:
846 ASSERT(right.is(rax));
847 __ SmiMul(right, right, left, &use_fp_on_smis); // MUL is commutative.
848 break;
849
850 case Token::DIV:
851 // SmiDiv will not accept left in rdx or right in rax.
852 left = rcx;
853 right = rbx;
854 __ movq(rbx, rax);
855 __ movq(rcx, rdx);
856 __ SmiDiv(rax, left, right, &use_fp_on_smis);
857 break;
858
859 case Token::MOD:
860 // SmiMod will not accept left in rdx or right in rax.
861 left = rcx;
862 right = rbx;
863 __ movq(rbx, rax);
864 __ movq(rcx, rdx);
865 __ SmiMod(rax, left, right, &use_fp_on_smis);
866 break;
867
868 case Token::BIT_OR: {
869 ASSERT(right.is(rax));
Ben Murdoch8b112d22011-06-08 16:22:53 +0100870 __ SmiOrIfSmis(right, right, left, &not_smis); // BIT_OR is commutative.
Steve Block1e0659c2011-05-24 12:43:12 +0100871 break;
872 }
873 case Token::BIT_XOR:
874 ASSERT(right.is(rax));
875 __ SmiXor(right, right, left); // BIT_XOR is commutative.
876 break;
877
878 case Token::BIT_AND:
879 ASSERT(right.is(rax));
880 __ SmiAnd(right, right, left); // BIT_AND is commutative.
881 break;
882
883 case Token::SHL:
884 __ SmiShiftLeft(left, left, right);
885 __ movq(rax, left);
886 break;
887
888 case Token::SAR:
889 __ SmiShiftArithmeticRight(left, left, right);
890 __ movq(rax, left);
891 break;
892
893 case Token::SHR:
Ben Murdoch8b112d22011-06-08 16:22:53 +0100894 __ SmiShiftLogicalRight(left, left, right, &use_fp_on_smis);
Steve Block1e0659c2011-05-24 12:43:12 +0100895 __ movq(rax, left);
896 break;
897
898 default:
899 UNREACHABLE();
900 }
901
902 // 5. Emit return of result in rax. Some operations have registers pushed.
903 __ ret(0);
904
Ben Murdoch8b112d22011-06-08 16:22:53 +0100905 if (use_fp_on_smis.is_linked()) {
906 // 6. For some operations emit inline code to perform floating point
907 // operations on known smis (e.g., if the result of the operation
908 // overflowed the smi range).
909 __ bind(&use_fp_on_smis);
910 if (op_ == Token::DIV || op_ == Token::MOD) {
911 // Restore left and right to rdx and rax.
912 __ movq(rdx, rcx);
913 __ movq(rax, rbx);
Steve Block1e0659c2011-05-24 12:43:12 +0100914 }
Ben Murdoch8b112d22011-06-08 16:22:53 +0100915
916 if (generate_inline_heapnumber_results) {
917 __ AllocateHeapNumber(rcx, rbx, slow);
918 Comment perform_float(masm, "-- Perform float operation on smis");
919 if (op_ == Token::SHR) {
920 __ SmiToInteger32(left, left);
921 __ cvtqsi2sd(xmm0, left);
922 } else {
923 FloatingPointHelper::LoadSSE2SmiOperands(masm);
924 switch (op_) {
925 case Token::ADD: __ addsd(xmm0, xmm1); break;
926 case Token::SUB: __ subsd(xmm0, xmm1); break;
927 case Token::MUL: __ mulsd(xmm0, xmm1); break;
928 case Token::DIV: __ divsd(xmm0, xmm1); break;
929 default: UNREACHABLE();
930 }
931 }
932 __ movsd(FieldOperand(rcx, HeapNumber::kValueOffset), xmm0);
933 __ movq(rax, rcx);
934 __ ret(0);
935 } else {
936 __ jmp(&fail);
937 }
Steve Block1e0659c2011-05-24 12:43:12 +0100938 }
939
940 // 7. Non-smi operands reach the end of the code generated by
941 // GenerateSmiCode, and fall through to subsequent code,
942 // with the operands in rdx and rax.
Ben Murdoch8b112d22011-06-08 16:22:53 +0100943 // But first we check if non-smi values are HeapNumbers holding
944 // values that could be smi.
Steve Block1e0659c2011-05-24 12:43:12 +0100945 __ bind(&not_smis);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100946 Comment done_comment(masm, "-- Enter non-smi code");
947 FloatingPointHelper::NumbersToSmis(masm, left, right, rbx, rdi, rcx,
948 &smi_values, &fail);
949 __ jmp(&smi_values);
950 __ bind(&fail);
Steve Block1e0659c2011-05-24 12:43:12 +0100951}
952
953
Ben Murdoch257744e2011-11-30 15:57:28 +0000954void BinaryOpStub::GenerateFloatingPointCode(MacroAssembler* masm,
955 Label* allocation_failure,
956 Label* non_numeric_failure) {
Steve Block1e0659c2011-05-24 12:43:12 +0100957 switch (op_) {
958 case Token::ADD:
959 case Token::SUB:
960 case Token::MUL:
961 case Token::DIV: {
962 FloatingPointHelper::LoadSSE2UnknownOperands(masm, non_numeric_failure);
963
964 switch (op_) {
965 case Token::ADD: __ addsd(xmm0, xmm1); break;
966 case Token::SUB: __ subsd(xmm0, xmm1); break;
967 case Token::MUL: __ mulsd(xmm0, xmm1); break;
968 case Token::DIV: __ divsd(xmm0, xmm1); break;
969 default: UNREACHABLE();
970 }
971 GenerateHeapResultAllocation(masm, allocation_failure);
972 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm0);
973 __ ret(0);
974 break;
975 }
976 case Token::MOD: {
977 // For MOD we jump to the allocation_failure label, to call runtime.
978 __ jmp(allocation_failure);
979 break;
980 }
981 case Token::BIT_OR:
982 case Token::BIT_AND:
983 case Token::BIT_XOR:
984 case Token::SAR:
985 case Token::SHL:
986 case Token::SHR: {
987 Label non_smi_shr_result;
988 Register heap_number_map = r9;
989 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
990 FloatingPointHelper::LoadAsIntegers(masm, non_numeric_failure,
991 heap_number_map);
992 switch (op_) {
993 case Token::BIT_OR: __ orl(rax, rcx); break;
994 case Token::BIT_AND: __ andl(rax, rcx); break;
995 case Token::BIT_XOR: __ xorl(rax, rcx); break;
996 case Token::SAR: __ sarl_cl(rax); break;
997 case Token::SHL: __ shll_cl(rax); break;
998 case Token::SHR: {
999 __ shrl_cl(rax);
1000 // Check if result is negative. This can only happen for a shift
1001 // by zero.
1002 __ testl(rax, rax);
1003 __ j(negative, &non_smi_shr_result);
1004 break;
1005 }
1006 default: UNREACHABLE();
1007 }
1008 STATIC_ASSERT(kSmiValueSize == 32);
1009 // Tag smi result and return.
1010 __ Integer32ToSmi(rax, rax);
1011 __ Ret();
1012
1013 // Logical shift right can produce an unsigned int32 that is not
1014 // an int32, and so is not in the smi range. Allocate a heap number
1015 // in that case.
1016 if (op_ == Token::SHR) {
1017 __ bind(&non_smi_shr_result);
1018 Label allocation_failed;
1019 __ movl(rbx, rax); // rbx holds result value (uint32 value as int64).
1020 // Allocate heap number in new space.
1021 // Not using AllocateHeapNumber macro in order to reuse
1022 // already loaded heap_number_map.
1023 __ AllocateInNewSpace(HeapNumber::kSize,
1024 rax,
Steve Block053d10c2011-06-13 19:13:29 +01001025 rdx,
Steve Block1e0659c2011-05-24 12:43:12 +01001026 no_reg,
1027 &allocation_failed,
1028 TAG_OBJECT);
1029 // Set the map.
1030 if (FLAG_debug_code) {
1031 __ AbortIfNotRootValue(heap_number_map,
1032 Heap::kHeapNumberMapRootIndex,
1033 "HeapNumberMap register clobbered.");
1034 }
1035 __ movq(FieldOperand(rax, HeapObject::kMapOffset),
1036 heap_number_map);
1037 __ cvtqsi2sd(xmm0, rbx);
1038 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm0);
1039 __ Ret();
1040
1041 __ bind(&allocation_failed);
1042 // We need tagged values in rdx and rax for the following code,
1043 // not int32 in rax and rcx.
1044 __ Integer32ToSmi(rax, rcx);
Steve Block053d10c2011-06-13 19:13:29 +01001045 __ Integer32ToSmi(rdx, rbx);
Steve Block1e0659c2011-05-24 12:43:12 +01001046 __ jmp(allocation_failure);
1047 }
1048 break;
1049 }
1050 default: UNREACHABLE(); break;
1051 }
1052 // No fall-through from this generated code.
1053 if (FLAG_debug_code) {
1054 __ Abort("Unexpected fall-through in "
Ben Murdoch257744e2011-11-30 15:57:28 +00001055 "BinaryStub::GenerateFloatingPointCode.");
Steve Block1e0659c2011-05-24 12:43:12 +01001056 }
1057}
1058
1059
Ben Murdoch257744e2011-11-30 15:57:28 +00001060void BinaryOpStub::GenerateStringAddCode(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001061 ASSERT(op_ == Token::ADD);
Ben Murdoch257744e2011-11-30 15:57:28 +00001062 Label left_not_string, call_runtime;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001063
Steve Block1e0659c2011-05-24 12:43:12 +01001064 // Registers containing left and right operands respectively.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001065 Register left = rdx;
1066 Register right = rax;
Steve Block1e0659c2011-05-24 12:43:12 +01001067
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001068 // Test if left operand is a string.
Ben Murdoch257744e2011-11-30 15:57:28 +00001069 __ JumpIfSmi(left, &left_not_string, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001070 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001071 __ j(above_equal, &left_not_string, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001072 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
1073 GenerateRegisterArgsPush(masm);
1074 __ TailCallStub(&string_add_left_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01001075
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001076 // Left operand is not a string, test right.
1077 __ bind(&left_not_string);
Ben Murdoch257744e2011-11-30 15:57:28 +00001078 __ JumpIfSmi(right, &call_runtime, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001079 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001080 __ j(above_equal, &call_runtime, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01001081
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001082 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
1083 GenerateRegisterArgsPush(masm);
1084 __ TailCallStub(&string_add_right_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01001085
Steve Block1e0659c2011-05-24 12:43:12 +01001086 // Neither argument is a string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001087 __ bind(&call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01001088}
1089
1090
Ben Murdoch257744e2011-11-30 15:57:28 +00001091void BinaryOpStub::GenerateCallRuntimeCode(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001092 GenerateRegisterArgsPush(masm);
1093 switch (op_) {
1094 case Token::ADD:
1095 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1096 break;
1097 case Token::SUB:
1098 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1099 break;
1100 case Token::MUL:
1101 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1102 break;
1103 case Token::DIV:
1104 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1105 break;
1106 case Token::MOD:
1107 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1108 break;
1109 case Token::BIT_OR:
1110 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1111 break;
1112 case Token::BIT_AND:
1113 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1114 break;
1115 case Token::BIT_XOR:
1116 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1117 break;
1118 case Token::SAR:
1119 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1120 break;
1121 case Token::SHL:
1122 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1123 break;
1124 case Token::SHR:
1125 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1126 break;
1127 default:
1128 UNREACHABLE();
1129 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001130}
1131
1132
Ben Murdoch257744e2011-11-30 15:57:28 +00001133void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001134 Label call_runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00001135 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
1136 result_type_ == BinaryOpIC::SMI) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001137 // Only allow smi results.
1138 GenerateSmiCode(masm, NULL, NO_HEAPNUMBER_RESULTS);
1139 } else {
1140 // Allow heap number result and don't make a transition if a heap number
1141 // cannot be allocated.
1142 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1143 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001144
Ben Murdoch8b112d22011-06-08 16:22:53 +01001145 // Code falls through if the result is not returned as either a smi or heap
1146 // number.
Steve Block1e0659c2011-05-24 12:43:12 +01001147 GenerateTypeTransition(masm);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001148
1149 if (call_runtime.is_linked()) {
1150 __ bind(&call_runtime);
1151 GenerateCallRuntimeCode(masm);
1152 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001153}
1154
1155
Ben Murdoch257744e2011-11-30 15:57:28 +00001156void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
1157 ASSERT(operands_type_ == BinaryOpIC::STRING);
Steve Block1e0659c2011-05-24 12:43:12 +01001158 ASSERT(op_ == Token::ADD);
1159 GenerateStringAddCode(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001160 // Try to add arguments as strings, otherwise, transition to the generic
Ben Murdoch257744e2011-11-30 15:57:28 +00001161 // BinaryOpIC type.
Steve Block1e0659c2011-05-24 12:43:12 +01001162 GenerateTypeTransition(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001163}
1164
1165
Ben Murdoch257744e2011-11-30 15:57:28 +00001166void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
1167 Label call_runtime;
1168 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
1169 ASSERT(op_ == Token::ADD);
1170 // If both arguments are strings, call the string add stub.
1171 // Otherwise, do a transition.
1172
1173 // Registers containing left and right operands respectively.
1174 Register left = rdx;
1175 Register right = rax;
1176
1177 // Test if left operand is a string.
1178 __ JumpIfSmi(left, &call_runtime);
1179 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, rcx);
1180 __ j(above_equal, &call_runtime);
1181
1182 // Test if right operand is a string.
1183 __ JumpIfSmi(right, &call_runtime);
1184 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, rcx);
1185 __ j(above_equal, &call_runtime);
1186
1187 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1188 GenerateRegisterArgsPush(masm);
1189 __ TailCallStub(&string_add_stub);
1190
1191 __ bind(&call_runtime);
1192 GenerateTypeTransition(masm);
1193}
1194
1195
1196void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01001197 Label call_runtime;
1198
1199 if (op_ == Token::ADD) {
1200 // Handle string addition here, because it is the only operation
1201 // that does not do a ToNumber conversion on the operands.
1202 GenerateStringAddCode(masm);
1203 }
1204
1205 // Convert oddball arguments to numbers.
Ben Murdoch257744e2011-11-30 15:57:28 +00001206 Label check, done;
Steve Block44f0eee2011-05-26 01:26:41 +01001207 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001208 __ j(not_equal, &check, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001209 if (Token::IsBitOp(op_)) {
1210 __ xor_(rdx, rdx);
1211 } else {
1212 __ LoadRoot(rdx, Heap::kNanValueRootIndex);
1213 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001214 __ jmp(&done, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001215 __ bind(&check);
1216 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001217 __ j(not_equal, &done, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001218 if (Token::IsBitOp(op_)) {
1219 __ xor_(rax, rax);
1220 } else {
1221 __ LoadRoot(rax, Heap::kNanValueRootIndex);
1222 }
1223 __ bind(&done);
1224
1225 GenerateHeapNumberStub(masm);
1226}
1227
1228
Ben Murdoch257744e2011-11-30 15:57:28 +00001229void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001230 Label gc_required, not_number;
1231 GenerateFloatingPointCode(masm, &gc_required, &not_number);
1232
1233 __ bind(&not_number);
1234 GenerateTypeTransition(masm);
1235
1236 __ bind(&gc_required);
1237 GenerateCallRuntimeCode(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001238}
1239
1240
Ben Murdoch257744e2011-11-30 15:57:28 +00001241void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001242 Label call_runtime, call_string_add_or_runtime;
1243
1244 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1245
1246 GenerateFloatingPointCode(masm, &call_runtime, &call_string_add_or_runtime);
1247
1248 __ bind(&call_string_add_or_runtime);
1249 if (op_ == Token::ADD) {
1250 GenerateStringAddCode(masm);
1251 }
1252
1253 __ bind(&call_runtime);
1254 GenerateCallRuntimeCode(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001255}
1256
1257
Ben Murdoch257744e2011-11-30 15:57:28 +00001258void BinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
1259 Label* alloc_failure) {
Steve Block1e0659c2011-05-24 12:43:12 +01001260 Label skip_allocation;
1261 OverwriteMode mode = mode_;
1262 switch (mode) {
1263 case OVERWRITE_LEFT: {
1264 // If the argument in rdx is already an object, we skip the
1265 // allocation of a heap number.
1266 __ JumpIfNotSmi(rdx, &skip_allocation);
1267 // Allocate a heap number for the result. Keep eax and edx intact
1268 // for the possible runtime call.
1269 __ AllocateHeapNumber(rbx, rcx, alloc_failure);
1270 // Now rdx can be overwritten losing one of the arguments as we are
1271 // now done and will not need it any more.
1272 __ movq(rdx, rbx);
1273 __ bind(&skip_allocation);
1274 // Use object in rdx as a result holder
1275 __ movq(rax, rdx);
1276 break;
1277 }
1278 case OVERWRITE_RIGHT:
1279 // If the argument in rax is already an object, we skip the
1280 // allocation of a heap number.
1281 __ JumpIfNotSmi(rax, &skip_allocation);
1282 // Fall through!
1283 case NO_OVERWRITE:
1284 // Allocate a heap number for the result. Keep rax and rdx intact
1285 // for the possible runtime call.
1286 __ AllocateHeapNumber(rbx, rcx, alloc_failure);
1287 // Now rax can be overwritten losing one of the arguments as we are
1288 // now done and will not need it any more.
1289 __ movq(rax, rbx);
1290 __ bind(&skip_allocation);
1291 break;
1292 default: UNREACHABLE();
1293 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001294}
1295
1296
Ben Murdoch257744e2011-11-30 15:57:28 +00001297void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01001298 __ pop(rcx);
1299 __ push(rdx);
1300 __ push(rax);
1301 __ push(rcx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001302}
1303
1304
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001305void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001306 // TAGGED case:
1307 // Input:
1308 // rsp[8]: argument (should be number).
1309 // rsp[0]: return address.
1310 // Output:
1311 // rax: tagged double result.
1312 // UNTAGGED case:
1313 // Input::
1314 // rsp[0]: return address.
1315 // xmm1: untagged double input argument
1316 // Output:
1317 // xmm1: untagged double result.
1318
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001319 Label runtime_call;
1320 Label runtime_call_clear_stack;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001321 Label skip_cache;
1322 const bool tagged = (argument_type_ == TAGGED);
1323 if (tagged) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001324 Label input_not_smi, loaded;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001325 // Test that rax is a number.
1326 __ movq(rax, Operand(rsp, kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001327 __ JumpIfNotSmi(rax, &input_not_smi, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001328 // Input is a smi. Untag and load it onto the FPU stack.
1329 // Then load the bits of the double into rbx.
1330 __ SmiToInteger32(rax, rax);
1331 __ subq(rsp, Immediate(kDoubleSize));
1332 __ cvtlsi2sd(xmm1, rax);
1333 __ movsd(Operand(rsp, 0), xmm1);
1334 __ movq(rbx, xmm1);
1335 __ movq(rdx, xmm1);
1336 __ fld_d(Operand(rsp, 0));
1337 __ addq(rsp, Immediate(kDoubleSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001338 __ jmp(&loaded, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001339
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001340 __ bind(&input_not_smi);
1341 // Check if input is a HeapNumber.
1342 __ LoadRoot(rbx, Heap::kHeapNumberMapRootIndex);
1343 __ cmpq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
1344 __ j(not_equal, &runtime_call);
1345 // Input is a HeapNumber. Push it on the FPU stack and load its
1346 // bits into rbx.
1347 __ fld_d(FieldOperand(rax, HeapNumber::kValueOffset));
1348 __ movq(rbx, FieldOperand(rax, HeapNumber::kValueOffset));
1349 __ movq(rdx, rbx);
1350
1351 __ bind(&loaded);
1352 } else { // UNTAGGED.
1353 __ movq(rbx, xmm1);
1354 __ movq(rdx, xmm1);
1355 }
1356
1357 // ST[0] == double value, if TAGGED.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001358 // rbx = bits of double value.
1359 // rdx = also bits of double value.
1360 // Compute hash (h is 32 bits, bits are 64 and the shifts are arithmetic):
1361 // h = h0 = bits ^ (bits >> 32);
1362 // h ^= h >> 16;
1363 // h ^= h >> 8;
1364 // h = h & (cacheSize - 1);
1365 // or h = (h0 ^ (h0 >> 8) ^ (h0 >> 16) ^ (h0 >> 24)) & (cacheSize - 1)
1366 __ sar(rdx, Immediate(32));
1367 __ xorl(rdx, rbx);
1368 __ movl(rcx, rdx);
1369 __ movl(rax, rdx);
1370 __ movl(rdi, rdx);
1371 __ sarl(rdx, Immediate(8));
1372 __ sarl(rcx, Immediate(16));
1373 __ sarl(rax, Immediate(24));
1374 __ xorl(rcx, rdx);
1375 __ xorl(rax, rdi);
1376 __ xorl(rcx, rax);
Steve Block44f0eee2011-05-26 01:26:41 +01001377 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
1378 __ andl(rcx, Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001379
1380 // ST[0] == double value.
1381 // rbx = bits of double value.
1382 // rcx = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +01001383 ExternalReference cache_array =
1384 ExternalReference::transcendental_cache_array_address(masm->isolate());
1385 __ movq(rax, cache_array);
1386 int cache_array_index =
1387 type_ * sizeof(Isolate::Current()->transcendental_cache()->caches_[0]);
1388 __ movq(rax, Operand(rax, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001389 // rax points to the cache for the type type_.
1390 // If NULL, the cache hasn't been initialized yet, so go through runtime.
1391 __ testq(rax, rax);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001392 __ j(zero, &runtime_call_clear_stack); // Only clears stack if TAGGED.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001393#ifdef DEBUG
1394 // Check that the layout of cache elements match expectations.
1395 { // NOLINT - doesn't like a single brace on a line.
Steve Block44f0eee2011-05-26 01:26:41 +01001396 TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001397 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
1398 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
1399 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
1400 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
1401 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
1402 // Two uint_32's and a pointer per element.
1403 CHECK_EQ(16, static_cast<int>(elem2_start - elem_start));
1404 CHECK_EQ(0, static_cast<int>(elem_in0 - elem_start));
1405 CHECK_EQ(kIntSize, static_cast<int>(elem_in1 - elem_start));
1406 CHECK_EQ(2 * kIntSize, static_cast<int>(elem_out - elem_start));
1407 }
1408#endif
1409 // Find the address of the rcx'th entry in the cache, i.e., &rax[rcx*16].
1410 __ addl(rcx, rcx);
1411 __ lea(rcx, Operand(rax, rcx, times_8, 0));
1412 // Check if cache matches: Double value is stored in uint32_t[2] array.
Ben Murdoch257744e2011-11-30 15:57:28 +00001413 Label cache_miss;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001414 __ cmpq(rbx, Operand(rcx, 0));
Ben Murdoch257744e2011-11-30 15:57:28 +00001415 __ j(not_equal, &cache_miss, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001416 // Cache hit!
1417 __ movq(rax, Operand(rcx, 2 * kIntSize));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001418 if (tagged) {
1419 __ fstp(0); // Clear FPU stack.
1420 __ ret(kPointerSize);
1421 } else { // UNTAGGED.
1422 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1423 __ Ret();
1424 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001425
1426 __ bind(&cache_miss);
1427 // Update cache with new value.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001428 if (tagged) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001429 __ AllocateHeapNumber(rax, rdi, &runtime_call_clear_stack);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001430 } else { // UNTAGGED.
1431 __ AllocateHeapNumber(rax, rdi, &skip_cache);
1432 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm1);
1433 __ fld_d(FieldOperand(rax, HeapNumber::kValueOffset));
1434 }
1435 GenerateOperation(masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001436 __ movq(Operand(rcx, 0), rbx);
1437 __ movq(Operand(rcx, 2 * kIntSize), rax);
1438 __ fstp_d(FieldOperand(rax, HeapNumber::kValueOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001439 if (tagged) {
1440 __ ret(kPointerSize);
1441 } else { // UNTAGGED.
1442 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1443 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001444
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001445 // Skip cache and return answer directly, only in untagged case.
1446 __ bind(&skip_cache);
1447 __ subq(rsp, Immediate(kDoubleSize));
1448 __ movsd(Operand(rsp, 0), xmm1);
1449 __ fld_d(Operand(rsp, 0));
1450 GenerateOperation(masm);
1451 __ fstp_d(Operand(rsp, 0));
1452 __ movsd(xmm1, Operand(rsp, 0));
1453 __ addq(rsp, Immediate(kDoubleSize));
1454 // We return the value in xmm1 without adding it to the cache, but
1455 // we cause a scavenging GC so that future allocations will succeed.
1456 __ EnterInternalFrame();
1457 // Allocate an unused object bigger than a HeapNumber.
1458 __ Push(Smi::FromInt(2 * kDoubleSize));
1459 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
1460 __ LeaveInternalFrame();
1461 __ Ret();
1462 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001463
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001464 // Call runtime, doing whatever allocation and cleanup is necessary.
1465 if (tagged) {
1466 __ bind(&runtime_call_clear_stack);
1467 __ fstp(0);
1468 __ bind(&runtime_call);
Steve Block44f0eee2011-05-26 01:26:41 +01001469 __ TailCallExternalReference(
1470 ExternalReference(RuntimeFunction(), masm->isolate()), 1, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001471 } else { // UNTAGGED.
1472 __ bind(&runtime_call_clear_stack);
1473 __ bind(&runtime_call);
1474 __ AllocateHeapNumber(rax, rdi, &skip_cache);
1475 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm1);
1476 __ EnterInternalFrame();
1477 __ push(rax);
1478 __ CallRuntime(RuntimeFunction(), 1);
1479 __ LeaveInternalFrame();
1480 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1481 __ Ret();
1482 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001483}
1484
1485
1486Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
1487 switch (type_) {
1488 // Add more cases when necessary.
1489 case TranscendentalCache::SIN: return Runtime::kMath_sin;
1490 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001491 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001492 default:
1493 UNIMPLEMENTED();
1494 return Runtime::kAbort;
1495 }
1496}
1497
1498
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001499void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001500 // Registers:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001501 // rax: Newly allocated HeapNumber, which must be preserved.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001502 // rbx: Bits of input double. Must be preserved.
1503 // rcx: Pointer to cache entry. Must be preserved.
1504 // st(0): Input double
1505 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001506 if (type_ == TranscendentalCache::SIN || type_ == TranscendentalCache::COS) {
1507 // Both fsin and fcos require arguments in the range +/-2^63 and
1508 // return NaN for infinities and NaN. They can share all code except
1509 // the actual fsin/fcos operation.
1510 Label in_range;
1511 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
1512 // work. We must reduce it to the appropriate range.
1513 __ movq(rdi, rbx);
1514 // Move exponent and sign bits to low bits.
1515 __ shr(rdi, Immediate(HeapNumber::kMantissaBits));
1516 // Remove sign bit.
1517 __ andl(rdi, Immediate((1 << HeapNumber::kExponentBits) - 1));
1518 int supported_exponent_limit = (63 + HeapNumber::kExponentBias);
1519 __ cmpl(rdi, Immediate(supported_exponent_limit));
1520 __ j(below, &in_range);
1521 // Check for infinity and NaN. Both return NaN for sin.
1522 __ cmpl(rdi, Immediate(0x7ff));
Ben Murdoch257744e2011-11-30 15:57:28 +00001523 Label non_nan_result;
1524 __ j(not_equal, &non_nan_result, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001525 // Input is +/-Infinity or NaN. Result is NaN.
1526 __ fstp(0);
1527 __ LoadRoot(kScratchRegister, Heap::kNanValueRootIndex);
1528 __ fld_d(FieldOperand(kScratchRegister, HeapNumber::kValueOffset));
1529 __ jmp(&done);
1530
1531 __ bind(&non_nan_result);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001532
Ben Murdochb0fe1622011-05-05 13:52:32 +01001533 // Use fpmod to restrict argument to the range +/-2*PI.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001534 __ movq(rdi, rax); // Save rax before using fnstsw_ax.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001535 __ fldpi();
1536 __ fadd(0);
1537 __ fld(1);
1538 // FPU Stack: input, 2*pi, input.
1539 {
1540 Label no_exceptions;
1541 __ fwait();
1542 __ fnstsw_ax();
1543 // Clear if Illegal Operand or Zero Division exceptions are set.
1544 __ testl(rax, Immediate(5)); // #IO and #ZD flags of FPU status word.
1545 __ j(zero, &no_exceptions);
1546 __ fnclex();
1547 __ bind(&no_exceptions);
1548 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001549
Ben Murdochb0fe1622011-05-05 13:52:32 +01001550 // Compute st(0) % st(1)
1551 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001552 Label partial_remainder_loop;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001553 __ bind(&partial_remainder_loop);
1554 __ fprem1();
1555 __ fwait();
1556 __ fnstsw_ax();
1557 __ testl(rax, Immediate(0x400)); // Check C2 bit of FPU status word.
1558 // If C2 is set, computation only has partial result. Loop to
1559 // continue computation.
1560 __ j(not_zero, &partial_remainder_loop);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001561 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001562 // FPU Stack: input, 2*pi, input % 2*pi
1563 __ fstp(2);
1564 // FPU Stack: input % 2*pi, 2*pi,
1565 __ fstp(0);
1566 // FPU Stack: input % 2*pi
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001567 __ movq(rax, rdi); // Restore rax, pointer to the new HeapNumber.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001568 __ bind(&in_range);
1569 switch (type_) {
1570 case TranscendentalCache::SIN:
1571 __ fsin();
1572 break;
1573 case TranscendentalCache::COS:
1574 __ fcos();
1575 break;
1576 default:
1577 UNREACHABLE();
1578 }
1579 __ bind(&done);
1580 } else {
1581 ASSERT(type_ == TranscendentalCache::LOG);
1582 __ fldln2();
1583 __ fxch();
1584 __ fyl2x();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001585 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001586}
1587
1588
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001589// Input: rdx, rax are the left and right objects of a bit op.
1590// Output: rax, rcx are left and right integers for a bit op.
1591void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm) {
1592 // Check float operands.
1593 Label done;
1594 Label rax_is_smi;
1595 Label rax_is_object;
1596 Label rdx_is_object;
1597
1598 __ JumpIfNotSmi(rdx, &rdx_is_object);
1599 __ SmiToInteger32(rdx, rdx);
1600 __ JumpIfSmi(rax, &rax_is_smi);
1601
1602 __ bind(&rax_is_object);
1603 IntegerConvert(masm, rcx, rax); // Uses rdi, rcx and rbx.
1604 __ jmp(&done);
1605
1606 __ bind(&rdx_is_object);
1607 IntegerConvert(masm, rdx, rdx); // Uses rdi, rcx and rbx.
1608 __ JumpIfNotSmi(rax, &rax_is_object);
1609 __ bind(&rax_is_smi);
1610 __ SmiToInteger32(rcx, rax);
1611
1612 __ bind(&done);
1613 __ movl(rax, rdx);
1614}
1615
1616
1617// Input: rdx, rax are the left and right objects of a bit op.
1618// Output: rax, rcx are left and right integers for a bit op.
Steve Block1e0659c2011-05-24 12:43:12 +01001619// Jump to conversion_failure: rdx and rax are unchanged.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001620void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
1621 Label* conversion_failure,
1622 Register heap_number_map) {
1623 // Check float operands.
1624 Label arg1_is_object, check_undefined_arg1;
1625 Label arg2_is_object, check_undefined_arg2;
1626 Label load_arg2, done;
1627
1628 __ JumpIfNotSmi(rdx, &arg1_is_object);
Steve Block1e0659c2011-05-24 12:43:12 +01001629 __ SmiToInteger32(r8, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001630 __ jmp(&load_arg2);
1631
1632 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
1633 __ bind(&check_undefined_arg1);
1634 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
1635 __ j(not_equal, conversion_failure);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001636 __ Set(r8, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001637 __ jmp(&load_arg2);
1638
1639 __ bind(&arg1_is_object);
1640 __ cmpq(FieldOperand(rdx, HeapObject::kMapOffset), heap_number_map);
1641 __ j(not_equal, &check_undefined_arg1);
Steve Block1e0659c2011-05-24 12:43:12 +01001642 // Get the untagged integer version of the rdx heap number in rcx.
1643 IntegerConvert(masm, r8, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001644
Steve Block1e0659c2011-05-24 12:43:12 +01001645 // Here r8 has the untagged integer, rax has a Smi or a heap number.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001646 __ bind(&load_arg2);
1647 // Test if arg2 is a Smi.
1648 __ JumpIfNotSmi(rax, &arg2_is_object);
Steve Block1e0659c2011-05-24 12:43:12 +01001649 __ SmiToInteger32(rcx, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001650 __ jmp(&done);
1651
1652 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
1653 __ bind(&check_undefined_arg2);
1654 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
1655 __ j(not_equal, conversion_failure);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001656 __ Set(rcx, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001657 __ jmp(&done);
1658
1659 __ bind(&arg2_is_object);
1660 __ cmpq(FieldOperand(rax, HeapObject::kMapOffset), heap_number_map);
1661 __ j(not_equal, &check_undefined_arg2);
1662 // Get the untagged integer version of the rax heap number in rcx.
1663 IntegerConvert(masm, rcx, rax);
1664 __ bind(&done);
Steve Block1e0659c2011-05-24 12:43:12 +01001665 __ movl(rax, r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001666}
1667
1668
1669void FloatingPointHelper::LoadSSE2SmiOperands(MacroAssembler* masm) {
1670 __ SmiToInteger32(kScratchRegister, rdx);
1671 __ cvtlsi2sd(xmm0, kScratchRegister);
1672 __ SmiToInteger32(kScratchRegister, rax);
1673 __ cvtlsi2sd(xmm1, kScratchRegister);
1674}
1675
1676
1677void FloatingPointHelper::LoadSSE2NumberOperands(MacroAssembler* masm) {
1678 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, done;
1679 // Load operand in rdx into xmm0.
1680 __ JumpIfSmi(rdx, &load_smi_rdx);
1681 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1682 // Load operand in rax into xmm1.
1683 __ JumpIfSmi(rax, &load_smi_rax);
1684 __ bind(&load_nonsmi_rax);
1685 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1686 __ jmp(&done);
1687
1688 __ bind(&load_smi_rdx);
1689 __ SmiToInteger32(kScratchRegister, rdx);
1690 __ cvtlsi2sd(xmm0, kScratchRegister);
1691 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
1692
1693 __ bind(&load_smi_rax);
1694 __ SmiToInteger32(kScratchRegister, rax);
1695 __ cvtlsi2sd(xmm1, kScratchRegister);
1696
1697 __ bind(&done);
1698}
1699
1700
1701void FloatingPointHelper::LoadSSE2UnknownOperands(MacroAssembler* masm,
1702 Label* not_numbers) {
1703 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, load_float_rax, done;
1704 // Load operand in rdx into xmm0, or branch to not_numbers.
1705 __ LoadRoot(rcx, Heap::kHeapNumberMapRootIndex);
1706 __ JumpIfSmi(rdx, &load_smi_rdx);
1707 __ cmpq(FieldOperand(rdx, HeapObject::kMapOffset), rcx);
1708 __ j(not_equal, not_numbers); // Argument in rdx is not a number.
1709 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1710 // Load operand in rax into xmm1, or branch to not_numbers.
1711 __ JumpIfSmi(rax, &load_smi_rax);
1712
1713 __ bind(&load_nonsmi_rax);
1714 __ cmpq(FieldOperand(rax, HeapObject::kMapOffset), rcx);
1715 __ j(not_equal, not_numbers);
1716 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1717 __ jmp(&done);
1718
1719 __ bind(&load_smi_rdx);
1720 __ SmiToInteger32(kScratchRegister, rdx);
1721 __ cvtlsi2sd(xmm0, kScratchRegister);
1722 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
1723
1724 __ bind(&load_smi_rax);
1725 __ SmiToInteger32(kScratchRegister, rax);
1726 __ cvtlsi2sd(xmm1, kScratchRegister);
1727 __ bind(&done);
1728}
1729
1730
Ben Murdoch8b112d22011-06-08 16:22:53 +01001731void FloatingPointHelper::NumbersToSmis(MacroAssembler* masm,
1732 Register first,
1733 Register second,
1734 Register scratch1,
1735 Register scratch2,
1736 Register scratch3,
1737 Label* on_success,
1738 Label* on_not_smis) {
1739 Register heap_number_map = scratch3;
1740 Register smi_result = scratch1;
1741 Label done;
1742
1743 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1744
Ben Murdoch257744e2011-11-30 15:57:28 +00001745 Label first_smi;
1746 __ JumpIfSmi(first, &first_smi, Label::kNear);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001747 __ cmpq(FieldOperand(first, HeapObject::kMapOffset), heap_number_map);
1748 __ j(not_equal, on_not_smis);
1749 // Convert HeapNumber to smi if possible.
1750 __ movsd(xmm0, FieldOperand(first, HeapNumber::kValueOffset));
1751 __ movq(scratch2, xmm0);
1752 __ cvttsd2siq(smi_result, xmm0);
1753 // Check if conversion was successful by converting back and
1754 // comparing to the original double's bits.
1755 __ cvtlsi2sd(xmm1, smi_result);
1756 __ movq(kScratchRegister, xmm1);
1757 __ cmpq(scratch2, kScratchRegister);
1758 __ j(not_equal, on_not_smis);
1759 __ Integer32ToSmi(first, smi_result);
1760
Ben Murdoch8b112d22011-06-08 16:22:53 +01001761 __ JumpIfSmi(second, (on_success != NULL) ? on_success : &done);
1762 __ bind(&first_smi);
1763 if (FLAG_debug_code) {
1764 // Second should be non-smi if we get here.
1765 __ AbortIfSmi(second);
1766 }
1767 __ cmpq(FieldOperand(second, HeapObject::kMapOffset), heap_number_map);
1768 __ j(not_equal, on_not_smis);
1769 // Convert second to smi, if possible.
1770 __ movsd(xmm0, FieldOperand(second, HeapNumber::kValueOffset));
1771 __ movq(scratch2, xmm0);
1772 __ cvttsd2siq(smi_result, xmm0);
1773 __ cvtlsi2sd(xmm1, smi_result);
1774 __ movq(kScratchRegister, xmm1);
1775 __ cmpq(scratch2, kScratchRegister);
1776 __ j(not_equal, on_not_smis);
1777 __ Integer32ToSmi(second, smi_result);
1778 if (on_success != NULL) {
1779 __ jmp(on_success);
1780 } else {
1781 __ bind(&done);
1782 }
1783}
1784
1785
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001786void MathPowStub::Generate(MacroAssembler* masm) {
1787 // Registers are used as follows:
1788 // rdx = base
1789 // rax = exponent
1790 // rcx = temporary, result
1791
1792 Label allocate_return, call_runtime;
1793
1794 // Load input parameters.
1795 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
1796 __ movq(rax, Operand(rsp, 1 * kPointerSize));
1797
1798 // Save 1 in xmm3 - we need this several times later on.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001799 __ Set(rcx, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001800 __ cvtlsi2sd(xmm3, rcx);
1801
1802 Label exponent_nonsmi;
1803 Label base_nonsmi;
1804 // If the exponent is a heap number go to that specific case.
1805 __ JumpIfNotSmi(rax, &exponent_nonsmi);
1806 __ JumpIfNotSmi(rdx, &base_nonsmi);
1807
1808 // Optimized version when both exponent and base are smis.
1809 Label powi;
1810 __ SmiToInteger32(rdx, rdx);
1811 __ cvtlsi2sd(xmm0, rdx);
1812 __ jmp(&powi);
1813 // Exponent is a smi and base is a heapnumber.
1814 __ bind(&base_nonsmi);
1815 __ CompareRoot(FieldOperand(rdx, HeapObject::kMapOffset),
1816 Heap::kHeapNumberMapRootIndex);
1817 __ j(not_equal, &call_runtime);
1818
1819 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1820
1821 // Optimized version of pow if exponent is a smi.
1822 // xmm0 contains the base.
1823 __ bind(&powi);
1824 __ SmiToInteger32(rax, rax);
1825
1826 // Save exponent in base as we need to check if exponent is negative later.
1827 // We know that base and exponent are in different registers.
1828 __ movq(rdx, rax);
1829
1830 // Get absolute value of exponent.
Ben Murdoch257744e2011-11-30 15:57:28 +00001831 Label no_neg;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001832 __ cmpl(rax, Immediate(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00001833 __ j(greater_equal, &no_neg, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001834 __ negl(rax);
1835 __ bind(&no_neg);
1836
1837 // Load xmm1 with 1.
Ben Murdoch257744e2011-11-30 15:57:28 +00001838 __ movaps(xmm1, xmm3);
1839 Label while_true;
1840 Label no_multiply;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001841
1842 __ bind(&while_true);
1843 __ shrl(rax, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00001844 __ j(not_carry, &no_multiply, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001845 __ mulsd(xmm1, xmm0);
1846 __ bind(&no_multiply);
1847 __ mulsd(xmm0, xmm0);
1848 __ j(not_zero, &while_true);
1849
1850 // Base has the original value of the exponent - if the exponent is
1851 // negative return 1/result.
1852 __ testl(rdx, rdx);
1853 __ j(positive, &allocate_return);
1854 // Special case if xmm1 has reached infinity.
1855 __ divsd(xmm3, xmm1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001856 __ movaps(xmm1, xmm3);
1857 __ xorps(xmm0, xmm0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001858 __ ucomisd(xmm0, xmm1);
1859 __ j(equal, &call_runtime);
1860
1861 __ jmp(&allocate_return);
1862
1863 // Exponent (or both) is a heapnumber - no matter what we should now work
1864 // on doubles.
1865 __ bind(&exponent_nonsmi);
1866 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
1867 Heap::kHeapNumberMapRootIndex);
1868 __ j(not_equal, &call_runtime);
1869 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1870 // Test if exponent is nan.
1871 __ ucomisd(xmm1, xmm1);
1872 __ j(parity_even, &call_runtime);
1873
Ben Murdoch257744e2011-11-30 15:57:28 +00001874 Label base_not_smi, handle_special_cases;
1875 __ JumpIfNotSmi(rdx, &base_not_smi, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001876 __ SmiToInteger32(rdx, rdx);
1877 __ cvtlsi2sd(xmm0, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001878 __ jmp(&handle_special_cases, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001879
1880 __ bind(&base_not_smi);
1881 __ CompareRoot(FieldOperand(rdx, HeapObject::kMapOffset),
1882 Heap::kHeapNumberMapRootIndex);
1883 __ j(not_equal, &call_runtime);
1884 __ movl(rcx, FieldOperand(rdx, HeapNumber::kExponentOffset));
1885 __ andl(rcx, Immediate(HeapNumber::kExponentMask));
1886 __ cmpl(rcx, Immediate(HeapNumber::kExponentMask));
1887 // base is NaN or +/-Infinity
1888 __ j(greater_equal, &call_runtime);
1889 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1890
1891 // base is in xmm0 and exponent is in xmm1.
1892 __ bind(&handle_special_cases);
Ben Murdoch257744e2011-11-30 15:57:28 +00001893 Label not_minus_half;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001894 // Test for -0.5.
1895 // Load xmm2 with -0.5.
1896 __ movq(rcx, V8_UINT64_C(0xBFE0000000000000), RelocInfo::NONE);
1897 __ movq(xmm2, rcx);
1898 // xmm2 now has -0.5.
1899 __ ucomisd(xmm2, xmm1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001900 __ j(not_equal, &not_minus_half, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001901
1902 // Calculates reciprocal of square root.
1903 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
Ben Murdoch257744e2011-11-30 15:57:28 +00001904 __ xorps(xmm1, xmm1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001905 __ addsd(xmm1, xmm0);
1906 __ sqrtsd(xmm1, xmm1);
1907 __ divsd(xmm3, xmm1);
Ben Murdoch257744e2011-11-30 15:57:28 +00001908 __ movaps(xmm1, xmm3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001909 __ jmp(&allocate_return);
1910
1911 // Test for 0.5.
1912 __ bind(&not_minus_half);
1913 // Load xmm2 with 0.5.
1914 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
1915 __ addsd(xmm2, xmm3);
1916 // xmm2 now has 0.5.
1917 __ ucomisd(xmm2, xmm1);
1918 __ j(not_equal, &call_runtime);
1919 // Calculates square root.
1920 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
Ben Murdoch257744e2011-11-30 15:57:28 +00001921 __ xorps(xmm1, xmm1);
1922 __ addsd(xmm1, xmm0); // Convert -0 to 0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001923 __ sqrtsd(xmm1, xmm1);
1924
1925 __ bind(&allocate_return);
1926 __ AllocateHeapNumber(rcx, rax, &call_runtime);
1927 __ movsd(FieldOperand(rcx, HeapNumber::kValueOffset), xmm1);
1928 __ movq(rax, rcx);
1929 __ ret(2 * kPointerSize);
1930
1931 __ bind(&call_runtime);
1932 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
1933}
1934
1935
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001936void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
1937 // The key is in rdx and the parameter count is in rax.
1938
1939 // The displacement is used for skipping the frame pointer on the
1940 // stack. It is the offset of the last parameter (if any) relative
1941 // to the frame pointer.
1942 static const int kDisplacement = 1 * kPointerSize;
1943
1944 // Check that the key is a smi.
1945 Label slow;
1946 __ JumpIfNotSmi(rdx, &slow);
1947
Steve Block44f0eee2011-05-26 01:26:41 +01001948 // Check if the calling frame is an arguments adaptor frame. We look at the
1949 // context offset, and if the frame is not a regular one, then we find a
1950 // Smi instead of the context. We can't use SmiCompare here, because that
1951 // only works for comparing two smis.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001952 Label adaptor;
1953 __ movq(rbx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01001954 __ Cmp(Operand(rbx, StandardFrameConstants::kContextOffset),
1955 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001956 __ j(equal, &adaptor);
1957
1958 // Check index against formal parameters count limit passed in
1959 // through register rax. Use unsigned comparison to get negative
1960 // check for free.
1961 __ cmpq(rdx, rax);
1962 __ j(above_equal, &slow);
1963
1964 // Read the argument from the stack and return it.
1965 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
1966 __ lea(rbx, Operand(rbp, index.reg, index.scale, 0));
1967 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
1968 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
1969 __ Ret();
1970
1971 // Arguments adaptor case: Check index against actual arguments
1972 // limit found in the arguments adaptor frame. Use unsigned
1973 // comparison to get negative check for free.
1974 __ bind(&adaptor);
1975 __ movq(rcx, Operand(rbx, ArgumentsAdaptorFrameConstants::kLengthOffset));
1976 __ cmpq(rdx, rcx);
1977 __ j(above_equal, &slow);
1978
1979 // Read the argument from the stack and return it.
1980 index = masm->SmiToIndex(rax, rcx, kPointerSizeLog2);
1981 __ lea(rbx, Operand(rbx, index.reg, index.scale, 0));
1982 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
1983 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
1984 __ Ret();
1985
1986 // Slow-case: Handle non-smi or out-of-bounds access to arguments
1987 // by calling the runtime system.
1988 __ bind(&slow);
1989 __ pop(rbx); // Return address.
1990 __ push(rdx);
1991 __ push(rbx);
1992 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
1993}
1994
1995
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001996void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
1997 // Stack layout:
1998 // rsp[0] : return address
1999 // rsp[8] : number of parameters (tagged)
2000 // rsp[16] : receiver displacement
2001 // rsp[24] : function
2002 // Registers used over the whole function:
2003 // rbx: the mapped parameter count (untagged)
2004 // rax: the allocated object (tagged).
2005
2006 Factory* factory = masm->isolate()->factory();
2007
2008 __ SmiToInteger64(rbx, Operand(rsp, 1 * kPointerSize));
2009 // rbx = parameter count (untagged)
2010
2011 // Check if the calling frame is an arguments adaptor frame.
2012 Label runtime;
2013 Label adaptor_frame, try_allocate;
2014 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
2015 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2016 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2017 __ j(equal, &adaptor_frame);
2018
2019 // No adaptor, parameter count = argument count.
2020 __ movq(rcx, rbx);
2021 __ jmp(&try_allocate, Label::kNear);
2022
2023 // We have an adaptor frame. Patch the parameters pointer.
2024 __ bind(&adaptor_frame);
2025 __ SmiToInteger64(rcx,
2026 Operand(rdx,
2027 ArgumentsAdaptorFrameConstants::kLengthOffset));
2028 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2029 StandardFrameConstants::kCallerSPOffset));
2030 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2031
2032 // rbx = parameter count (untagged)
2033 // rcx = argument count (untagged)
2034 // Compute the mapped parameter count = min(rbx, rcx) in rbx.
2035 __ cmpq(rbx, rcx);
2036 __ j(less_equal, &try_allocate, Label::kNear);
2037 __ movq(rbx, rcx);
2038
2039 __ bind(&try_allocate);
2040
2041 // Compute the sizes of backing store, parameter map, and arguments object.
2042 // 1. Parameter map, has 2 extra words containing context and backing store.
2043 const int kParameterMapHeaderSize =
2044 FixedArray::kHeaderSize + 2 * kPointerSize;
2045 Label no_parameter_map;
2046 __ testq(rbx, rbx);
2047 __ j(zero, &no_parameter_map, Label::kNear);
2048 __ lea(r8, Operand(rbx, times_pointer_size, kParameterMapHeaderSize));
2049 __ bind(&no_parameter_map);
2050
2051 // 2. Backing store.
2052 __ lea(r8, Operand(r8, rcx, times_pointer_size, FixedArray::kHeaderSize));
2053
2054 // 3. Arguments object.
2055 __ addq(r8, Immediate(Heap::kArgumentsObjectSize));
2056
2057 // Do the allocation of all three objects in one go.
2058 __ AllocateInNewSpace(r8, rax, rdx, rdi, &runtime, TAG_OBJECT);
2059
2060 // rax = address of new object(s) (tagged)
2061 // rcx = argument count (untagged)
2062 // Get the arguments boilerplate from the current (global) context into rdi.
2063 Label has_mapped_parameters, copy;
2064 __ movq(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2065 __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
2066 __ testq(rbx, rbx);
2067 __ j(not_zero, &has_mapped_parameters, Label::kNear);
2068
2069 const int kIndex = Context::ARGUMENTS_BOILERPLATE_INDEX;
2070 __ movq(rdi, Operand(rdi, Context::SlotOffset(kIndex)));
2071 __ jmp(&copy, Label::kNear);
2072
2073 const int kAliasedIndex = Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX;
2074 __ bind(&has_mapped_parameters);
2075 __ movq(rdi, Operand(rdi, Context::SlotOffset(kAliasedIndex)));
2076 __ bind(&copy);
2077
2078 // rax = address of new object (tagged)
2079 // rbx = mapped parameter count (untagged)
2080 // rcx = argument count (untagged)
2081 // rdi = address of boilerplate object (tagged)
2082 // Copy the JS object part.
2083 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2084 __ movq(rdx, FieldOperand(rdi, i));
2085 __ movq(FieldOperand(rax, i), rdx);
2086 }
2087
2088 // Setup the callee in-object property.
2089 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
2090 __ movq(rdx, Operand(rsp, 3 * kPointerSize));
2091 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
2092 Heap::kArgumentsCalleeIndex * kPointerSize),
2093 rdx);
2094
2095 // Use the length (smi tagged) and set that as an in-object property too.
2096 // Note: rcx is tagged from here on.
2097 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
2098 __ Integer32ToSmi(rcx, rcx);
2099 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
2100 Heap::kArgumentsLengthIndex * kPointerSize),
2101 rcx);
2102
2103 // Setup the elements pointer in the allocated arguments object.
2104 // If we allocated a parameter map, edi will point there, otherwise to the
2105 // backing store.
2106 __ lea(rdi, Operand(rax, Heap::kArgumentsObjectSize));
2107 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rdi);
2108
2109 // rax = address of new object (tagged)
2110 // rbx = mapped parameter count (untagged)
2111 // rcx = argument count (tagged)
2112 // rdi = address of parameter map or backing store (tagged)
2113
2114 // Initialize parameter map. If there are no mapped arguments, we're done.
2115 Label skip_parameter_map;
2116 __ testq(rbx, rbx);
2117 __ j(zero, &skip_parameter_map);
2118
2119 __ LoadRoot(kScratchRegister, Heap::kNonStrictArgumentsElementsMapRootIndex);
2120 // rbx contains the untagged argument count. Add 2 and tag to write.
2121 __ movq(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
2122 __ Integer64PlusConstantToSmi(r9, rbx, 2);
2123 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), r9);
2124 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize + 0 * kPointerSize), rsi);
2125 __ lea(r9, Operand(rdi, rbx, times_pointer_size, kParameterMapHeaderSize));
2126 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize + 1 * kPointerSize), r9);
2127
2128 // Copy the parameter slots and the holes in the arguments.
2129 // We need to fill in mapped_parameter_count slots. They index the context,
2130 // where parameters are stored in reverse order, at
2131 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
2132 // The mapped parameter thus need to get indices
2133 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
2134 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
2135 // We loop from right to left.
2136 Label parameters_loop, parameters_test;
2137
2138 // Load tagged parameter count into r9.
2139 __ movq(r9, Operand(rsp, 1 * kPointerSize));
2140 __ Move(r8, Smi::FromInt(Context::MIN_CONTEXT_SLOTS));
2141 __ addq(r8, Operand(rsp, 3 * kPointerSize));
2142 __ subq(r8, r9);
2143 __ Move(r11, factory->the_hole_value());
2144 __ movq(rdx, rdi);
2145 __ SmiToInteger64(kScratchRegister, r9);
2146 __ lea(rdi, Operand(rdi, kScratchRegister,
2147 times_pointer_size,
2148 kParameterMapHeaderSize));
2149 // r9 = loop variable (tagged)
2150 // r8 = mapping index (tagged)
2151 // r11 = the hole value
2152 // rdx = address of parameter map (tagged)
2153 // rdi = address of backing store (tagged)
2154 __ jmp(&parameters_test, Label::kNear);
2155
2156 __ bind(&parameters_loop);
2157 __ SmiSubConstant(r9, r9, Smi::FromInt(1));
2158 __ SmiToInteger64(kScratchRegister, r9);
2159 __ movq(FieldOperand(rdx, kScratchRegister,
2160 times_pointer_size,
2161 kParameterMapHeaderSize),
2162 r8);
2163 __ movq(FieldOperand(rdi, kScratchRegister,
2164 times_pointer_size,
2165 FixedArray::kHeaderSize),
2166 r11);
2167 __ SmiAddConstant(r8, r8, Smi::FromInt(1));
2168 __ bind(&parameters_test);
2169 __ SmiTest(r9);
2170 __ j(not_zero, &parameters_loop, Label::kNear);
2171
2172 __ bind(&skip_parameter_map);
2173
2174 // rcx = argument count (tagged)
2175 // rdi = address of backing store (tagged)
2176 // Copy arguments header and remaining slots (if there are any).
2177 __ Move(FieldOperand(rdi, FixedArray::kMapOffset),
2178 factory->fixed_array_map());
2179 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
2180
2181 Label arguments_loop, arguments_test;
2182 __ movq(r8, rbx);
2183 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
2184 // Untag rcx and r8 for the loop below.
2185 __ SmiToInteger64(rcx, rcx);
2186 __ SmiToInteger64(r8, r8);
2187 __ lea(kScratchRegister, Operand(r8, times_pointer_size, 0));
2188 __ subq(rdx, kScratchRegister);
2189 __ jmp(&arguments_test, Label::kNear);
2190
2191 __ bind(&arguments_loop);
2192 __ subq(rdx, Immediate(kPointerSize));
2193 __ movq(r9, Operand(rdx, 0));
2194 __ movq(FieldOperand(rdi, r8,
2195 times_pointer_size,
2196 FixedArray::kHeaderSize),
2197 r9);
2198 __ addq(r8, Immediate(1));
2199
2200 __ bind(&arguments_test);
2201 __ cmpq(r8, rcx);
2202 __ j(less, &arguments_loop, Label::kNear);
2203
2204 // Return and remove the on-stack parameters.
2205 __ ret(3 * kPointerSize);
2206
2207 // Do the runtime call to allocate the arguments object.
2208 // rcx = argument count (untagged)
2209 __ bind(&runtime);
2210 __ Integer32ToSmi(rcx, rcx);
2211 __ movq(Operand(rsp, 1 * kPointerSize), rcx); // Patch argument count.
2212 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
2213}
2214
2215
2216void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
2217 // esp[0] : return address
2218 // esp[8] : number of parameters
2219 // esp[16] : receiver displacement
2220 // esp[24] : function
2221
2222 // Check if the calling frame is an arguments adaptor frame.
2223 Label runtime;
2224 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
2225 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2226 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2227 __ j(not_equal, &runtime);
2228
2229 // Patch the arguments.length and the parameters pointer.
2230 __ movq(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2231 __ movq(Operand(rsp, 1 * kPointerSize), rcx);
2232 __ SmiToInteger64(rcx, rcx);
2233 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2234 StandardFrameConstants::kCallerSPOffset));
2235 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2236
2237 __ bind(&runtime);
2238 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2239}
2240
2241
2242void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002243 // rsp[0] : return address
2244 // rsp[8] : number of parameters
2245 // rsp[16] : receiver displacement
2246 // rsp[24] : function
2247
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002248 // Check if the calling frame is an arguments adaptor frame.
2249 Label adaptor_frame, try_allocate, runtime;
2250 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002251 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2252 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002253 __ j(equal, &adaptor_frame);
2254
2255 // Get the length from the frame.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002256 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
2257 __ SmiToInteger64(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002258 __ jmp(&try_allocate);
2259
2260 // Patch the arguments.length and the parameters pointer.
2261 __ bind(&adaptor_frame);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002262 __ movq(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2263 __ movq(Operand(rsp, 1 * kPointerSize), rcx);
2264 __ SmiToInteger64(rcx, rcx);
2265 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2266 StandardFrameConstants::kCallerSPOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002267 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2268
2269 // Try the new space allocation. Start out with computing the size of
2270 // the arguments object and the elements array.
2271 Label add_arguments_object;
2272 __ bind(&try_allocate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002273 __ testq(rcx, rcx);
2274 __ j(zero, &add_arguments_object, Label::kNear);
2275 __ lea(rcx, Operand(rcx, times_pointer_size, FixedArray::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002276 __ bind(&add_arguments_object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002277 __ addq(rcx, Immediate(Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002278
2279 // Do the allocation of both objects in one go.
2280 __ AllocateInNewSpace(rcx, rax, rdx, rbx, &runtime, TAG_OBJECT);
2281
2282 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002283 __ movq(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2284 __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002285 const int offset =
2286 Context::SlotOffset(Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX);
2287 __ movq(rdi, Operand(rdi, offset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002288
2289 // Copy the JS object part.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002290 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2291 __ movq(rbx, FieldOperand(rdi, i));
2292 __ movq(FieldOperand(rax, i), rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01002293 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002294
2295 // Get the length (smi tagged) and set that as an in-object property too.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002296 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002297 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01002298 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002299 Heap::kArgumentsLengthIndex * kPointerSize),
Steve Block44f0eee2011-05-26 01:26:41 +01002300 rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002301
2302 // If there are no actual arguments, we're done.
2303 Label done;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002304 __ testq(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002305 __ j(zero, &done);
2306
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002307 // Get the parameters pointer from the stack.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002308 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
2309
2310 // Setup the elements pointer in the allocated arguments object and
2311 // initialize the header in the elements fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002312 __ lea(rdi, Operand(rax, Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002313 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rdi);
2314 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
2315 __ movq(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002316
2317
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002318 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002319 // Untag the length for the loop below.
2320 __ SmiToInteger64(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002321
2322 // Copy the fixed array slots.
2323 Label loop;
2324 __ bind(&loop);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002325 __ movq(rbx, Operand(rdx, -1 * kPointerSize)); // Skip receiver.
2326 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize), rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002327 __ addq(rdi, Immediate(kPointerSize));
2328 __ subq(rdx, Immediate(kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002329 __ decq(rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002330 __ j(not_zero, &loop);
2331
2332 // Return and remove the on-stack parameters.
2333 __ bind(&done);
2334 __ ret(3 * kPointerSize);
2335
2336 // Do the runtime call to allocate the arguments object.
2337 __ bind(&runtime);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002338 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002339}
2340
2341
2342void RegExpExecStub::Generate(MacroAssembler* masm) {
2343 // Just jump directly to runtime if native RegExp is not selected at compile
2344 // time or if regexp entry in generated code is turned off runtime switch or
2345 // at compilation.
2346#ifdef V8_INTERPRETED_REGEXP
2347 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2348#else // V8_INTERPRETED_REGEXP
2349 if (!FLAG_regexp_entry_native) {
2350 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2351 return;
2352 }
2353
2354 // Stack frame on entry.
Steve Block1e0659c2011-05-24 12:43:12 +01002355 // rsp[0]: return address
2356 // rsp[8]: last_match_info (expected JSArray)
2357 // rsp[16]: previous index
2358 // rsp[24]: subject string
2359 // rsp[32]: JSRegExp object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002360
2361 static const int kLastMatchInfoOffset = 1 * kPointerSize;
2362 static const int kPreviousIndexOffset = 2 * kPointerSize;
2363 static const int kSubjectOffset = 3 * kPointerSize;
2364 static const int kJSRegExpOffset = 4 * kPointerSize;
2365
2366 Label runtime;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002367 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01002368 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002369 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01002370 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002371 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01002372 ExternalReference::address_of_regexp_stack_memory_size(isolate);
2373 __ Load(kScratchRegister, address_of_regexp_stack_memory_size);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002374 __ testq(kScratchRegister, kScratchRegister);
2375 __ j(zero, &runtime);
2376
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002377 // Check that the first argument is a JSRegExp object.
2378 __ movq(rax, Operand(rsp, kJSRegExpOffset));
2379 __ JumpIfSmi(rax, &runtime);
2380 __ CmpObjectType(rax, JS_REGEXP_TYPE, kScratchRegister);
2381 __ j(not_equal, &runtime);
2382 // Check that the RegExp has been compiled (data contains a fixed array).
Steve Block44f0eee2011-05-26 01:26:41 +01002383 __ movq(rax, FieldOperand(rax, JSRegExp::kDataOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002384 if (FLAG_debug_code) {
Steve Block44f0eee2011-05-26 01:26:41 +01002385 Condition is_smi = masm->CheckSmi(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002386 __ Check(NegateCondition(is_smi),
2387 "Unexpected type for RegExp data, FixedArray expected");
Steve Block44f0eee2011-05-26 01:26:41 +01002388 __ CmpObjectType(rax, FIXED_ARRAY_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002389 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
2390 }
2391
Steve Block44f0eee2011-05-26 01:26:41 +01002392 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002393 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
Steve Block44f0eee2011-05-26 01:26:41 +01002394 __ SmiToInteger32(rbx, FieldOperand(rax, JSRegExp::kDataTagOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002395 __ cmpl(rbx, Immediate(JSRegExp::IRREGEXP));
2396 __ j(not_equal, &runtime);
2397
Steve Block44f0eee2011-05-26 01:26:41 +01002398 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002399 // Check that the number of captures fit in the static offsets vector buffer.
2400 __ SmiToInteger32(rdx,
Steve Block44f0eee2011-05-26 01:26:41 +01002401 FieldOperand(rax, JSRegExp::kIrregexpCaptureCountOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002402 // Calculate number of capture registers (number_of_captures + 1) * 2.
2403 __ leal(rdx, Operand(rdx, rdx, times_1, 2));
2404 // Check that the static offsets vector buffer is large enough.
2405 __ cmpl(rdx, Immediate(OffsetsVector::kStaticOffsetsVectorSize));
2406 __ j(above, &runtime);
2407
Steve Block44f0eee2011-05-26 01:26:41 +01002408 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002409 // rdx: Number of capture registers
2410 // Check that the second argument is a string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002411 __ movq(rdi, Operand(rsp, kSubjectOffset));
2412 __ JumpIfSmi(rdi, &runtime);
2413 Condition is_string = masm->IsObjectStringType(rdi, rbx, rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002414 __ j(NegateCondition(is_string), &runtime);
2415
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002416 // rdi: Subject string.
2417 // rax: RegExp data (FixedArray).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002418 // rdx: Number of capture registers.
2419 // Check that the third argument is a positive smi less than the string
2420 // length. A negative value will be greater (unsigned comparison).
2421 __ movq(rbx, Operand(rsp, kPreviousIndexOffset));
2422 __ JumpIfNotSmi(rbx, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002423 __ SmiCompare(rbx, FieldOperand(rdi, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002424 __ j(above_equal, &runtime);
2425
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002426 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002427 // rdx: Number of capture registers
2428 // Check that the fourth object is a JSArray object.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002429 __ movq(rdi, Operand(rsp, kLastMatchInfoOffset));
2430 __ JumpIfSmi(rdi, &runtime);
2431 __ CmpObjectType(rdi, JS_ARRAY_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002432 __ j(not_equal, &runtime);
2433 // Check that the JSArray is in fast case.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002434 __ movq(rbx, FieldOperand(rdi, JSArray::kElementsOffset));
2435 __ movq(rdi, FieldOperand(rbx, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002436 __ CompareRoot(FieldOperand(rbx, HeapObject::kMapOffset),
2437 Heap::kFixedArrayMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002438 __ j(not_equal, &runtime);
2439 // Check that the last match info has space for the capture registers and the
2440 // additional information. Ensure no overflow in add.
2441 STATIC_ASSERT(FixedArray::kMaxLength < kMaxInt - FixedArray::kLengthOffset);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002442 __ SmiToInteger32(rdi, FieldOperand(rbx, FixedArray::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002443 __ addl(rdx, Immediate(RegExpImpl::kLastMatchOverhead));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002444 __ cmpl(rdx, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002445 __ j(greater, &runtime);
2446
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002447 // Reset offset for possibly sliced string.
2448 __ Set(r14, 0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002449 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002450 // Check the representation and encoding of the subject string.
Ben Murdoch257744e2011-11-30 15:57:28 +00002451 Label seq_ascii_string, seq_two_byte_string, check_code;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002452 __ movq(rdi, Operand(rsp, kSubjectOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002453 // Make a copy of the original subject string.
2454 __ movq(r15, rdi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002455 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002456 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
2457 // First check for flat two byte string.
2458 __ andb(rbx, Immediate(
2459 kIsNotStringMask | kStringRepresentationMask | kStringEncodingMask));
2460 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002461 __ j(zero, &seq_two_byte_string, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002462 // Any other flat string must be a flat ascii string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002463 __ andb(rbx, Immediate(kIsNotStringMask | kStringRepresentationMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00002464 __ j(zero, &seq_ascii_string, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002465
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002466 // Check for flat cons string or sliced string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002467 // A flat cons string is a cons string where the second part is the empty
2468 // string. In that case the subject string is just the first part of the cons
2469 // string. Also in this case the first part of the cons string is known to be
2470 // a sequential string or an external string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002471 // In the case of a sliced string its offset has to be taken into account.
2472 Label cons_string, check_encoding;
2473 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
2474 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
2475 __ cmpq(rbx, Immediate(kExternalStringTag));
2476 __ j(less, &cons_string, Label::kNear);
2477 __ j(equal, &runtime);
2478
2479 // String is sliced.
2480 __ SmiToInteger32(r14, FieldOperand(rdi, SlicedString::kOffsetOffset));
2481 __ movq(rdi, FieldOperand(rdi, SlicedString::kParentOffset));
2482 // r14: slice offset
2483 // r15: original subject string
2484 // rdi: parent string
2485 __ jmp(&check_encoding, Label::kNear);
2486 // String is a cons string, check whether it is flat.
2487 __ bind(&cons_string);
Steve Block44f0eee2011-05-26 01:26:41 +01002488 __ CompareRoot(FieldOperand(rdi, ConsString::kSecondOffset),
2489 Heap::kEmptyStringRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002490 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002491 __ movq(rdi, FieldOperand(rdi, ConsString::kFirstOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002492 // rdi: first part of cons string or parent of sliced string.
2493 // rbx: map of first part of cons string or map of parent of sliced string.
2494 // Is first part of cons or parent of slice a flat two byte string?
2495 __ bind(&check_encoding);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002496 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002497 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
2498 Immediate(kStringRepresentationMask | kStringEncodingMask));
2499 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002500 __ j(zero, &seq_two_byte_string, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002501 // Any other flat string must be ascii.
2502 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
2503 Immediate(kStringRepresentationMask));
2504 __ j(not_zero, &runtime);
2505
2506 __ bind(&seq_ascii_string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002507 // rdi: subject string (sequential ascii)
2508 // rax: RegExp data (FixedArray)
2509 __ movq(r11, FieldOperand(rax, JSRegExp::kDataAsciiCodeOffset));
2510 __ Set(rcx, 1); // Type is ascii.
Ben Murdoch257744e2011-11-30 15:57:28 +00002511 __ jmp(&check_code, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002512
2513 __ bind(&seq_two_byte_string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002514 // rdi: subject string (flat two-byte)
2515 // rax: RegExp data (FixedArray)
2516 __ movq(r11, FieldOperand(rax, JSRegExp::kDataUC16CodeOffset));
2517 __ Set(rcx, 0); // Type is two byte.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002518
2519 __ bind(&check_code);
2520 // Check that the irregexp code has been generated for the actual string
2521 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00002522 // smi (code flushing support)
2523 __ JumpIfSmi(r11, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002524
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002525 // rdi: subject string
2526 // rcx: encoding of subject string (1 if ascii, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002527 // r11: code
2528 // Load used arguments before starting to push arguments for call to native
2529 // RegExp code to avoid handling changing stack height.
2530 __ SmiToInteger64(rbx, Operand(rsp, kPreviousIndexOffset));
2531
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002532 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002533 // rbx: previous index
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002534 // rcx: encoding of subject string (1 if ascii 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002535 // r11: code
2536 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01002537 Counters* counters = masm->isolate()->counters();
2538 __ IncrementCounter(counters->regexp_entry_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002539
Steve Block44f0eee2011-05-26 01:26:41 +01002540 // Isolates: note we add an additional parameter here (isolate pointer).
2541 static const int kRegExpExecuteArguments = 8;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002542 int argument_slots_on_stack =
2543 masm->ArgumentStackSlotsForCFunctionCall(kRegExpExecuteArguments);
Steve Block44f0eee2011-05-26 01:26:41 +01002544 __ EnterApiExitFrame(argument_slots_on_stack);
2545
2546 // Argument 8: Pass current isolate address.
2547 // __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
2548 // Immediate(ExternalReference::isolate_address()));
2549 __ LoadAddress(kScratchRegister, ExternalReference::isolate_address());
2550 __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
2551 kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002552
2553 // Argument 7: Indicate that this is a direct call from JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01002554 __ movq(Operand(rsp, (argument_slots_on_stack - 2) * kPointerSize),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002555 Immediate(1));
2556
2557 // Argument 6: Start (high end) of backtracking stack memory area.
2558 __ movq(kScratchRegister, address_of_regexp_stack_memory_address);
2559 __ movq(r9, Operand(kScratchRegister, 0));
2560 __ movq(kScratchRegister, address_of_regexp_stack_memory_size);
2561 __ addq(r9, Operand(kScratchRegister, 0));
2562 // Argument 6 passed in r9 on Linux and on the stack on Windows.
2563#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01002564 __ movq(Operand(rsp, (argument_slots_on_stack - 3) * kPointerSize), r9);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002565#endif
2566
2567 // Argument 5: static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01002568 __ LoadAddress(r8,
2569 ExternalReference::address_of_static_offsets_vector(isolate));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002570 // Argument 5 passed in r8 on Linux and on the stack on Windows.
2571#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01002572 __ movq(Operand(rsp, (argument_slots_on_stack - 4) * kPointerSize), r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002573#endif
2574
2575 // First four arguments are passed in registers on both Linux and Windows.
2576#ifdef _WIN64
2577 Register arg4 = r9;
2578 Register arg3 = r8;
2579 Register arg2 = rdx;
2580 Register arg1 = rcx;
2581#else
2582 Register arg4 = rcx;
2583 Register arg3 = rdx;
2584 Register arg2 = rsi;
2585 Register arg1 = rdi;
2586#endif
2587
2588 // Keep track on aliasing between argX defined above and the registers used.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002589 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002590 // rbx: previous index
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002591 // rcx: encoding of subject string (1 if ascii 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002592 // r11: code
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002593 // r14: slice offset
2594 // r15: original subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002595
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002596 // Argument 2: Previous index.
2597 __ movq(arg2, rbx);
2598
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002599 // Argument 4: End of string data
2600 // Argument 3: Start of string data
2601 Label setup_two_byte, setup_rest, got_length, length_not_from_slice;
2602 // Prepare start and end index of the input.
2603 // Load the length from the original sliced string if that is the case.
2604 __ addq(rbx, r14);
2605 __ SmiToInteger32(arg3, FieldOperand(r15, String::kLengthOffset));
2606 __ addq(r14, arg3); // Using arg3 as scratch.
2607
2608 // rbx: start index of the input
2609 // r14: end index of the input
2610 // r15: original subject string
2611 __ testb(rcx, rcx); // Last use of rcx as encoding of subject string.
2612 __ j(zero, &setup_two_byte, Label::kNear);
2613 __ lea(arg4, FieldOperand(rdi, r14, times_1, SeqAsciiString::kHeaderSize));
2614 __ lea(arg3, FieldOperand(rdi, rbx, times_1, SeqAsciiString::kHeaderSize));
2615 __ jmp(&setup_rest, Label::kNear);
2616 __ bind(&setup_two_byte);
2617 __ lea(arg4, FieldOperand(rdi, r14, times_2, SeqTwoByteString::kHeaderSize));
2618 __ lea(arg3, FieldOperand(rdi, rbx, times_2, SeqTwoByteString::kHeaderSize));
2619 __ bind(&setup_rest);
2620
2621 // Argument 1: Original subject string.
2622 // The original subject is in the previous stack frame. Therefore we have to
2623 // use rbp, which points exactly to one pointer size below the previous rsp.
2624 // (Because creating a new stack frame pushes the previous rbp onto the stack
2625 // and thereby moves up rsp by one kPointerSize.)
2626 __ movq(arg1, r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002627
2628 // Locate the code entry and call it.
2629 __ addq(r11, Immediate(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002630 __ call(r11);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002631
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002632 __ LeaveApiExitFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002633
2634 // Check the result.
Ben Murdoch257744e2011-11-30 15:57:28 +00002635 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002636 Label exception;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002637 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::SUCCESS));
Ben Murdoch257744e2011-11-30 15:57:28 +00002638 __ j(equal, &success, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002639 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::EXCEPTION));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002640 __ j(equal, &exception);
2641 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::FAILURE));
2642 // If none of the above, it can only be retry.
2643 // Handle that in the runtime system.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002644 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002645
2646 // For failure return null.
2647 __ LoadRoot(rax, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002648 __ ret(4 * kPointerSize);
2649
2650 // Load RegExp data.
2651 __ bind(&success);
2652 __ movq(rax, Operand(rsp, kJSRegExpOffset));
2653 __ movq(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
2654 __ SmiToInteger32(rax,
2655 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
2656 // Calculate number of capture registers (number_of_captures + 1) * 2.
2657 __ leal(rdx, Operand(rax, rax, times_1, 2));
2658
2659 // rdx: Number of capture registers
2660 // Load last_match_info which is still known to be a fast case JSArray.
2661 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
2662 __ movq(rbx, FieldOperand(rax, JSArray::kElementsOffset));
2663
2664 // rbx: last_match_info backing store (FixedArray)
2665 // rdx: number of capture registers
2666 // Store the capture count.
2667 __ Integer32ToSmi(kScratchRegister, rdx);
2668 __ movq(FieldOperand(rbx, RegExpImpl::kLastCaptureCountOffset),
2669 kScratchRegister);
2670 // Store last subject and last input.
2671 __ movq(rax, Operand(rsp, kSubjectOffset));
2672 __ movq(FieldOperand(rbx, RegExpImpl::kLastSubjectOffset), rax);
2673 __ movq(rcx, rbx);
2674 __ RecordWrite(rcx, RegExpImpl::kLastSubjectOffset, rax, rdi);
2675 __ movq(rax, Operand(rsp, kSubjectOffset));
2676 __ movq(FieldOperand(rbx, RegExpImpl::kLastInputOffset), rax);
2677 __ movq(rcx, rbx);
2678 __ RecordWrite(rcx, RegExpImpl::kLastInputOffset, rax, rdi);
2679
2680 // Get the static offsets vector filled by the native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01002681 __ LoadAddress(rcx,
2682 ExternalReference::address_of_static_offsets_vector(isolate));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002683
2684 // rbx: last_match_info backing store (FixedArray)
2685 // rcx: offsets vector
2686 // rdx: number of capture registers
Ben Murdoch257744e2011-11-30 15:57:28 +00002687 Label next_capture, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002688 // Capture register counter starts from number of capture registers and
2689 // counts down until wraping after zero.
2690 __ bind(&next_capture);
2691 __ subq(rdx, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00002692 __ j(negative, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002693 // Read the value from the static offsets vector buffer and make it a smi.
2694 __ movl(rdi, Operand(rcx, rdx, times_int_size, 0));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002695 __ Integer32ToSmi(rdi, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002696 // Store the smi value in the last match info.
2697 __ movq(FieldOperand(rbx,
2698 rdx,
2699 times_pointer_size,
2700 RegExpImpl::kFirstCaptureOffset),
2701 rdi);
2702 __ jmp(&next_capture);
2703 __ bind(&done);
2704
2705 // Return last match info.
2706 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
2707 __ ret(4 * kPointerSize);
2708
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002709 __ bind(&exception);
2710 // Result must now be exception. If there is no pending exception already a
2711 // stack overflow (on the backtrack stack) was detected in RegExp code but
2712 // haven't created the exception yet. Handle that in the runtime system.
2713 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01002714 ExternalReference pending_exception_address(
Ben Murdoch589d6972011-11-30 16:04:58 +00002715 Isolate::kPendingExceptionAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002716 Operand pending_exception_operand =
2717 masm->ExternalOperand(pending_exception_address, rbx);
2718 __ movq(rax, pending_exception_operand);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002719 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
2720 __ cmpq(rax, rdx);
2721 __ j(equal, &runtime);
Steve Block44f0eee2011-05-26 01:26:41 +01002722 __ movq(pending_exception_operand, rdx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002723
2724 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002725 Label termination_exception;
2726 __ j(equal, &termination_exception, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002727 __ Throw(rax);
2728
2729 __ bind(&termination_exception);
2730 __ ThrowUncatchable(TERMINATION, rax);
2731
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002732 // Do the runtime call to execute the regexp.
2733 __ bind(&runtime);
2734 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2735#endif // V8_INTERPRETED_REGEXP
2736}
2737
2738
Ben Murdochb0fe1622011-05-05 13:52:32 +01002739void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
2740 const int kMaxInlineLength = 100;
2741 Label slowcase;
2742 Label done;
2743 __ movq(r8, Operand(rsp, kPointerSize * 3));
2744 __ JumpIfNotSmi(r8, &slowcase);
2745 __ SmiToInteger32(rbx, r8);
2746 __ cmpl(rbx, Immediate(kMaxInlineLength));
2747 __ j(above, &slowcase);
2748 // Smi-tagging is equivalent to multiplying by 2.
2749 STATIC_ASSERT(kSmiTag == 0);
2750 STATIC_ASSERT(kSmiTagSize == 1);
Steve Block1e0659c2011-05-24 12:43:12 +01002751 // Allocate RegExpResult followed by FixedArray with size in rbx.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002752 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
2753 // Elements: [Map][Length][..elements..]
2754 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
2755 times_pointer_size,
2756 rbx, // In: Number of elements.
2757 rax, // Out: Start of allocation (tagged).
2758 rcx, // Out: End of allocation.
2759 rdx, // Scratch register
2760 &slowcase,
2761 TAG_OBJECT);
2762 // rax: Start of allocated area, object-tagged.
2763 // rbx: Number of array elements as int32.
2764 // r8: Number of array elements as smi.
2765
2766 // Set JSArray map to global.regexp_result_map().
2767 __ movq(rdx, ContextOperand(rsi, Context::GLOBAL_INDEX));
2768 __ movq(rdx, FieldOperand(rdx, GlobalObject::kGlobalContextOffset));
2769 __ movq(rdx, ContextOperand(rdx, Context::REGEXP_RESULT_MAP_INDEX));
2770 __ movq(FieldOperand(rax, HeapObject::kMapOffset), rdx);
2771
2772 // Set empty properties FixedArray.
Steve Block44f0eee2011-05-26 01:26:41 +01002773 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
2774 __ movq(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002775
2776 // Set elements to point to FixedArray allocated right after the JSArray.
2777 __ lea(rcx, Operand(rax, JSRegExpResult::kSize));
2778 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rcx);
2779
2780 // Set input, index and length fields from arguments.
2781 __ movq(r8, Operand(rsp, kPointerSize * 1));
2782 __ movq(FieldOperand(rax, JSRegExpResult::kInputOffset), r8);
2783 __ movq(r8, Operand(rsp, kPointerSize * 2));
2784 __ movq(FieldOperand(rax, JSRegExpResult::kIndexOffset), r8);
2785 __ movq(r8, Operand(rsp, kPointerSize * 3));
2786 __ movq(FieldOperand(rax, JSArray::kLengthOffset), r8);
2787
2788 // Fill out the elements FixedArray.
2789 // rax: JSArray.
2790 // rcx: FixedArray.
2791 // rbx: Number of elements in array as int32.
2792
2793 // Set map.
Steve Block44f0eee2011-05-26 01:26:41 +01002794 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
2795 __ movq(FieldOperand(rcx, HeapObject::kMapOffset), kScratchRegister);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002796 // Set length.
2797 __ Integer32ToSmi(rdx, rbx);
2798 __ movq(FieldOperand(rcx, FixedArray::kLengthOffset), rdx);
2799 // Fill contents of fixed-array with the-hole.
Steve Block44f0eee2011-05-26 01:26:41 +01002800 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002801 __ lea(rcx, FieldOperand(rcx, FixedArray::kHeaderSize));
2802 // Fill fixed array elements with hole.
2803 // rax: JSArray.
2804 // rbx: Number of elements in array that remains to be filled, as int32.
2805 // rcx: Start of elements in FixedArray.
2806 // rdx: the hole.
2807 Label loop;
2808 __ testl(rbx, rbx);
2809 __ bind(&loop);
Steve Block1e0659c2011-05-24 12:43:12 +01002810 __ j(less_equal, &done); // Jump if rcx is negative or zero.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002811 __ subl(rbx, Immediate(1));
2812 __ movq(Operand(rcx, rbx, times_pointer_size, 0), rdx);
2813 __ jmp(&loop);
2814
2815 __ bind(&done);
2816 __ ret(3 * kPointerSize);
2817
2818 __ bind(&slowcase);
2819 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
2820}
2821
2822
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002823void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
2824 Register object,
2825 Register result,
2826 Register scratch1,
2827 Register scratch2,
2828 bool object_is_smi,
2829 Label* not_found) {
2830 // Use of registers. Register result is used as a temporary.
2831 Register number_string_cache = result;
2832 Register mask = scratch1;
2833 Register scratch = scratch2;
2834
2835 // Load the number string cache.
2836 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
2837
2838 // Make the hash mask from the length of the number string cache. It
2839 // contains two elements (number and string) for each cache entry.
2840 __ SmiToInteger32(
2841 mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
2842 __ shrl(mask, Immediate(1));
2843 __ subq(mask, Immediate(1)); // Make mask.
2844
2845 // Calculate the entry in the number string cache. The hash value in the
2846 // number string cache for smis is just the smi value, and the hash for
2847 // doubles is the xor of the upper and lower words. See
2848 // Heap::GetNumberStringCache.
2849 Label is_smi;
2850 Label load_result_from_cache;
Ben Murdoch257744e2011-11-30 15:57:28 +00002851 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002852 if (!object_is_smi) {
2853 __ JumpIfSmi(object, &is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +00002854 __ CheckMap(object,
2855 factory->heap_number_map(),
2856 not_found,
2857 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002858
2859 STATIC_ASSERT(8 == kDoubleSize);
2860 __ movl(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
2861 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset));
2862 GenerateConvertHashCodeToIndex(masm, scratch, mask);
2863
2864 Register index = scratch;
2865 Register probe = mask;
2866 __ movq(probe,
2867 FieldOperand(number_string_cache,
2868 index,
2869 times_1,
2870 FixedArray::kHeaderSize));
2871 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002872 __ movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
2873 __ movsd(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
2874 __ ucomisd(xmm0, xmm1);
2875 __ j(parity_even, not_found); // Bail out if NaN is involved.
2876 __ j(not_equal, not_found); // The cache did not contain this value.
2877 __ jmp(&load_result_from_cache);
2878 }
2879
2880 __ bind(&is_smi);
2881 __ SmiToInteger32(scratch, object);
2882 GenerateConvertHashCodeToIndex(masm, scratch, mask);
2883
2884 Register index = scratch;
2885 // Check if the entry is the smi we are looking for.
2886 __ cmpq(object,
2887 FieldOperand(number_string_cache,
2888 index,
2889 times_1,
2890 FixedArray::kHeaderSize));
2891 __ j(not_equal, not_found);
2892
2893 // Get the result from the cache.
2894 __ bind(&load_result_from_cache);
2895 __ movq(result,
2896 FieldOperand(number_string_cache,
2897 index,
2898 times_1,
2899 FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01002900 Counters* counters = masm->isolate()->counters();
2901 __ IncrementCounter(counters->number_to_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002902}
2903
2904
2905void NumberToStringStub::GenerateConvertHashCodeToIndex(MacroAssembler* masm,
2906 Register hash,
2907 Register mask) {
2908 __ and_(hash, mask);
2909 // Each entry in string cache consists of two pointer sized fields,
2910 // but times_twice_pointer_size (multiplication by 16) scale factor
2911 // is not supported by addrmode on x64 platform.
2912 // So we have to premultiply entry index before lookup.
2913 __ shl(hash, Immediate(kPointerSizeLog2 + 1));
2914}
2915
2916
2917void NumberToStringStub::Generate(MacroAssembler* masm) {
2918 Label runtime;
2919
2920 __ movq(rbx, Operand(rsp, kPointerSize));
2921
2922 // Generate code to lookup number in the number string cache.
2923 GenerateLookupNumberStringCache(masm, rbx, rax, r8, r9, false, &runtime);
2924 __ ret(1 * kPointerSize);
2925
2926 __ bind(&runtime);
2927 // Handle number to string in the runtime system if not found in the cache.
2928 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
2929}
2930
2931
2932static int NegativeComparisonResult(Condition cc) {
2933 ASSERT(cc != equal);
2934 ASSERT((cc == less) || (cc == less_equal)
2935 || (cc == greater) || (cc == greater_equal));
2936 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
2937}
2938
2939
2940void CompareStub::Generate(MacroAssembler* masm) {
2941 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
2942
2943 Label check_unequal_objects, done;
Ben Murdoch257744e2011-11-30 15:57:28 +00002944 Factory* factory = masm->isolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002945
2946 // Compare two smis if required.
2947 if (include_smi_compare_) {
2948 Label non_smi, smi_done;
2949 __ JumpIfNotBothSmi(rax, rdx, &non_smi);
2950 __ subq(rdx, rax);
2951 __ j(no_overflow, &smi_done);
Ben Murdochf87a2032010-10-22 12:50:53 +01002952 __ not_(rdx); // Correct sign in case of overflow. rdx cannot be 0 here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002953 __ bind(&smi_done);
2954 __ movq(rax, rdx);
2955 __ ret(0);
2956 __ bind(&non_smi);
2957 } else if (FLAG_debug_code) {
2958 Label ok;
2959 __ JumpIfNotSmi(rdx, &ok);
2960 __ JumpIfNotSmi(rax, &ok);
2961 __ Abort("CompareStub: smi operands");
2962 __ bind(&ok);
2963 }
2964
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002965 // The compare stub returns a positive, negative, or zero 64-bit integer
2966 // value in rax, corresponding to result of comparing the two inputs.
2967 // NOTICE! This code is only reached after a smi-fast-case check, so
2968 // it is certain that at least one operand isn't a smi.
2969
2970 // Two identical objects are equal unless they are both NaN or undefined.
2971 {
Ben Murdoch257744e2011-11-30 15:57:28 +00002972 Label not_identical;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002973 __ cmpq(rax, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00002974 __ j(not_equal, &not_identical, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002975
2976 if (cc_ != equal) {
2977 // Check for undefined. undefined OP undefined is false even though
2978 // undefined == undefined.
Ben Murdoch257744e2011-11-30 15:57:28 +00002979 Label check_for_nan;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002980 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002981 __ j(not_equal, &check_for_nan, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002982 __ Set(rax, NegativeComparisonResult(cc_));
2983 __ ret(0);
2984 __ bind(&check_for_nan);
2985 }
2986
Steve Block44f0eee2011-05-26 01:26:41 +01002987 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002988 // so we do the second best thing - test it ourselves.
2989 // Note: if cc_ != equal, never_nan_nan_ is not used.
2990 // We cannot set rax to EQUAL until just before return because
2991 // rax must be unchanged on jump to not_identical.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002992 if (never_nan_nan_ && (cc_ == equal)) {
2993 __ Set(rax, EQUAL);
2994 __ ret(0);
2995 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00002996 Label heap_number;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002997 // If it's not a heap number, then return equal for (in)equality operator.
2998 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00002999 factory->heap_number_map());
3000 __ j(equal, &heap_number, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003001 if (cc_ != equal) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003002 // Call runtime on identical objects. Otherwise return equal.
3003 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003004 __ j(above_equal, &not_identical, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003005 }
3006 __ Set(rax, EQUAL);
3007 __ ret(0);
3008
3009 __ bind(&heap_number);
3010 // It is a heap number, so return equal if it's not NaN.
3011 // For NaN, return 1 for every condition except greater and
3012 // greater-equal. Return -1 for them, so the comparison yields
3013 // false for all conditions except not-equal.
3014 __ Set(rax, EQUAL);
3015 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
3016 __ ucomisd(xmm0, xmm0);
3017 __ setcc(parity_even, rax);
3018 // rax is 0 for equal non-NaN heapnumbers, 1 for NaNs.
3019 if (cc_ == greater_equal || cc_ == greater) {
3020 __ neg(rax);
3021 }
3022 __ ret(0);
3023 }
3024
3025 __ bind(&not_identical);
3026 }
3027
3028 if (cc_ == equal) { // Both strict and non-strict.
3029 Label slow; // Fallthrough label.
3030
3031 // If we're doing a strict equality comparison, we don't have to do
3032 // type conversion, so we generate code to do fast comparison for objects
3033 // and oddballs. Non-smi numbers and strings still go through the usual
3034 // slow-case code.
3035 if (strict_) {
3036 // If either is a Smi (we know that not both are), then they can only
3037 // be equal if the other is a HeapNumber. If so, use the slow case.
3038 {
3039 Label not_smis;
3040 __ SelectNonSmi(rbx, rax, rdx, &not_smis);
3041
3042 // Check if the non-smi operand is a heap number.
3043 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00003044 factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003045 // If heap number, handle it in the slow case.
3046 __ j(equal, &slow);
3047 // Return non-equal. ebx (the lower half of rbx) is not zero.
3048 __ movq(rax, rbx);
3049 __ ret(0);
3050
3051 __ bind(&not_smis);
3052 }
3053
3054 // If either operand is a JSObject or an oddball value, then they are not
3055 // equal since their pointers are different
3056 // There is no test for undetectability in strict equality.
3057
3058 // If the first object is a JS object, we have done pointer comparison.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003059 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +00003060 Label first_non_object;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003061 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003062 __ j(below, &first_non_object, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003063 // Return non-zero (eax (not rax) is not zero)
3064 Label return_not_equal;
3065 STATIC_ASSERT(kHeapObjectTag != 0);
3066 __ bind(&return_not_equal);
3067 __ ret(0);
3068
3069 __ bind(&first_non_object);
3070 // Check for oddballs: true, false, null, undefined.
3071 __ CmpInstanceType(rcx, ODDBALL_TYPE);
3072 __ j(equal, &return_not_equal);
3073
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003074 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003075 __ j(above_equal, &return_not_equal);
3076
3077 // Check for oddballs: true, false, null, undefined.
3078 __ CmpInstanceType(rcx, ODDBALL_TYPE);
3079 __ j(equal, &return_not_equal);
3080
3081 // Fall through to the general case.
3082 }
3083 __ bind(&slow);
3084 }
3085
3086 // Generate the number comparison code.
3087 if (include_number_compare_) {
3088 Label non_number_comparison;
Ben Murdoch257744e2011-11-30 15:57:28 +00003089 Label unordered;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003090 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &non_number_comparison);
3091 __ xorl(rax, rax);
3092 __ xorl(rcx, rcx);
3093 __ ucomisd(xmm0, xmm1);
3094
3095 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch257744e2011-11-30 15:57:28 +00003096 __ j(parity_even, &unordered, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003097 // Return a result of -1, 0, or 1, based on EFLAGS.
3098 __ setcc(above, rax);
3099 __ setcc(below, rcx);
3100 __ subq(rax, rcx);
3101 __ ret(0);
3102
3103 // If one of the numbers was NaN, then the result is always false.
3104 // The cc is never not-equal.
3105 __ bind(&unordered);
3106 ASSERT(cc_ != not_equal);
3107 if (cc_ == less || cc_ == less_equal) {
3108 __ Set(rax, 1);
3109 } else {
3110 __ Set(rax, -1);
3111 }
3112 __ ret(0);
3113
3114 // The number comparison code did not provide a valid result.
3115 __ bind(&non_number_comparison);
3116 }
3117
3118 // Fast negative check for symbol-to-symbol equality.
3119 Label check_for_strings;
3120 if (cc_ == equal) {
3121 BranchIfNonSymbol(masm, &check_for_strings, rax, kScratchRegister);
3122 BranchIfNonSymbol(masm, &check_for_strings, rdx, kScratchRegister);
3123
3124 // We've already checked for object identity, so if both operands
3125 // are symbols they aren't equal. Register eax (not rax) already holds a
3126 // non-zero value, which indicates not equal, so just return.
3127 __ ret(0);
3128 }
3129
3130 __ bind(&check_for_strings);
3131
3132 __ JumpIfNotBothSequentialAsciiStrings(
3133 rdx, rax, rcx, rbx, &check_unequal_objects);
3134
3135 // Inline comparison of ascii strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00003136 if (cc_ == equal) {
3137 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003138 rdx,
3139 rax,
3140 rcx,
Ben Murdoch257744e2011-11-30 15:57:28 +00003141 rbx);
3142 } else {
3143 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
3144 rdx,
3145 rax,
3146 rcx,
3147 rbx,
3148 rdi,
3149 r8);
3150 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003151
3152#ifdef DEBUG
3153 __ Abort("Unexpected fall-through from string comparison");
3154#endif
3155
3156 __ bind(&check_unequal_objects);
3157 if (cc_ == equal && !strict_) {
3158 // Not strict equality. Objects are unequal if
3159 // they are both JSObjects and not undetectable,
3160 // and their pointers are different.
Ben Murdoch257744e2011-11-30 15:57:28 +00003161 Label not_both_objects, return_unequal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003162 // At most one is a smi, so we can test for smi by adding the two.
3163 // A smi plus a heap object has the low bit set, a heap object plus
3164 // a heap object has the low bit clear.
3165 STATIC_ASSERT(kSmiTag == 0);
3166 STATIC_ASSERT(kSmiTagMask == 1);
3167 __ lea(rcx, Operand(rax, rdx, times_1, 0));
3168 __ testb(rcx, Immediate(kSmiTagMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00003169 __ j(not_zero, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003170 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003171 __ j(below, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003172 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003173 __ j(below, &not_both_objects, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003174 __ testb(FieldOperand(rbx, Map::kBitFieldOffset),
3175 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00003176 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003177 __ testb(FieldOperand(rcx, Map::kBitFieldOffset),
3178 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00003179 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003180 // The objects are both undetectable, so they both compare as the value
3181 // undefined, and are equal.
3182 __ Set(rax, EQUAL);
3183 __ bind(&return_unequal);
Steve Block1e0659c2011-05-24 12:43:12 +01003184 // Return non-equal by returning the non-zero object pointer in rax,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003185 // or return equal if we fell through to here.
3186 __ ret(0);
3187 __ bind(&not_both_objects);
3188 }
3189
3190 // Push arguments below the return address to prepare jump to builtin.
3191 __ pop(rcx);
3192 __ push(rdx);
3193 __ push(rax);
3194
3195 // Figure out which native to call and setup the arguments.
3196 Builtins::JavaScript builtin;
3197 if (cc_ == equal) {
3198 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
3199 } else {
3200 builtin = Builtins::COMPARE;
3201 __ Push(Smi::FromInt(NegativeComparisonResult(cc_)));
3202 }
3203
3204 // Restore return address on the stack.
3205 __ push(rcx);
3206
3207 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
3208 // tagged as a small integer.
3209 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
3210}
3211
3212
3213void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
3214 Label* label,
3215 Register object,
3216 Register scratch) {
3217 __ JumpIfSmi(object, label);
3218 __ movq(scratch, FieldOperand(object, HeapObject::kMapOffset));
3219 __ movzxbq(scratch,
3220 FieldOperand(scratch, Map::kInstanceTypeOffset));
3221 // Ensure that no non-strings have the symbol bit set.
3222 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
3223 STATIC_ASSERT(kSymbolTag != 0);
3224 __ testb(scratch, Immediate(kIsSymbolMask));
3225 __ j(zero, label);
3226}
3227
3228
3229void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01003230 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003231}
3232
3233
3234void CallFunctionStub::Generate(MacroAssembler* masm) {
Ben Murdoch589d6972011-11-30 16:04:58 +00003235 Label slow, non_function;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003236
Ben Murdoch257744e2011-11-30 15:57:28 +00003237 // The receiver might implicitly be the global object. This is
3238 // indicated by passing the hole as the receiver to the call
3239 // function stub.
3240 if (ReceiverMightBeImplicit()) {
3241 Label call;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003242 // Get the receiver from the stack.
3243 // +1 ~ return address
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003244 __ movq(rax, Operand(rsp, (argc_ + 1) * kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00003245 // Call as function is indicated with the hole.
3246 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
3247 __ j(not_equal, &call, Label::kNear);
3248 // Patch the receiver on the stack with the global receiver object.
3249 __ movq(rbx, GlobalObjectOperand());
3250 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalReceiverOffset));
3251 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rbx);
3252 __ bind(&call);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003253 }
3254
3255 // Get the function to call from the stack.
3256 // +2 ~ receiver, return address
3257 __ movq(rdi, Operand(rsp, (argc_ + 2) * kPointerSize));
3258
3259 // Check that the function really is a JavaScript function.
Ben Murdoch589d6972011-11-30 16:04:58 +00003260 __ JumpIfSmi(rdi, &non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003261 // Goto slow case if we do not have a function.
3262 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
3263 __ j(not_equal, &slow);
3264
3265 // Fast-case: Just invoke the function.
3266 ParameterCount actual(argc_);
Ben Murdoch257744e2011-11-30 15:57:28 +00003267
3268 if (ReceiverMightBeImplicit()) {
3269 Label call_as_function;
3270 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
3271 __ j(equal, &call_as_function);
3272 __ InvokeFunction(rdi,
3273 actual,
3274 JUMP_FUNCTION,
3275 NullCallWrapper(),
3276 CALL_AS_METHOD);
3277 __ bind(&call_as_function);
3278 }
3279 __ InvokeFunction(rdi,
3280 actual,
3281 JUMP_FUNCTION,
3282 NullCallWrapper(),
3283 CALL_AS_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003284
3285 // Slow-case: Non-function called.
3286 __ bind(&slow);
Ben Murdoch589d6972011-11-30 16:04:58 +00003287 // Check for function proxy.
3288 __ CmpInstanceType(rcx, JS_FUNCTION_PROXY_TYPE);
3289 __ j(not_equal, &non_function);
3290 __ pop(rcx);
3291 __ push(rdi); // put proxy as additional argument under return address
3292 __ push(rcx);
3293 __ Set(rax, argc_ + 1);
3294 __ Set(rbx, 0);
3295 __ SetCallKind(rcx, CALL_AS_FUNCTION);
3296 __ GetBuiltinEntry(rdx, Builtins::CALL_FUNCTION_PROXY);
3297 {
3298 Handle<Code> adaptor =
3299 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
3300 __ jmp(adaptor, RelocInfo::CODE_TARGET);
3301 }
3302
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003303 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3304 // of the original receiver from the call site).
Ben Murdoch589d6972011-11-30 16:04:58 +00003305 __ bind(&non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003306 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rdi);
3307 __ Set(rax, argc_);
3308 __ Set(rbx, 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00003309 __ SetCallKind(rcx, CALL_AS_METHOD);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003310 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01003311 Handle<Code> adaptor =
3312 Isolate::Current()->builtins()->ArgumentsAdaptorTrampoline();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003313 __ Jump(adaptor, RelocInfo::CODE_TARGET);
3314}
3315
3316
Steve Block44f0eee2011-05-26 01:26:41 +01003317bool CEntryStub::NeedsImmovableCode() {
3318 return false;
3319}
3320
3321
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003322void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003323 // Throw exception in eax.
3324 __ Throw(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003325}
3326
3327
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003328void CEntryStub::GenerateCore(MacroAssembler* masm,
3329 Label* throw_normal_exception,
3330 Label* throw_termination_exception,
3331 Label* throw_out_of_memory_exception,
3332 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01003333 bool always_allocate_scope) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003334 // rax: result parameter for PerformGC, if any.
3335 // rbx: pointer to C function (C callee-saved).
3336 // rbp: frame pointer (restored after C call).
3337 // rsp: stack pointer (restored after C call).
3338 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01003339 // r15: pointer to the first argument (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003340 // This pointer is reused in LeaveExitFrame(), so it is stored in a
3341 // callee-saved register.
3342
3343 // Simple results returned in rax (both AMD64 and Win64 calling conventions).
3344 // Complex results must be written to address passed as first argument.
3345 // AMD64 calling convention: a struct of two pointers in rax+rdx
3346
3347 // Check stack alignment.
3348 if (FLAG_debug_code) {
3349 __ CheckStackAlignment();
3350 }
3351
3352 if (do_gc) {
3353 // Pass failure code returned from last attempt as first argument to
3354 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
3355 // stack is known to be aligned. This function takes one argument which is
3356 // passed in register.
3357#ifdef _WIN64
3358 __ movq(rcx, rax);
3359#else // _WIN64
3360 __ movq(rdi, rax);
3361#endif
3362 __ movq(kScratchRegister,
3363 FUNCTION_ADDR(Runtime::PerformGC),
3364 RelocInfo::RUNTIME_ENTRY);
3365 __ call(kScratchRegister);
3366 }
3367
3368 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01003369 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003370 if (always_allocate_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +01003371 Operand scope_depth_operand = masm->ExternalOperand(scope_depth);
3372 __ incl(scope_depth_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003373 }
3374
3375 // Call C function.
3376#ifdef _WIN64
3377 // Windows 64-bit ABI passes arguments in rcx, rdx, r8, r9
3378 // Store Arguments object on stack, below the 4 WIN64 ABI parameter slots.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003379 __ movq(StackSpaceOperand(0), r14); // argc.
Steve Block44f0eee2011-05-26 01:26:41 +01003380 __ movq(StackSpaceOperand(1), r15); // argv.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003381 if (result_size_ < 2) {
3382 // Pass a pointer to the Arguments object as the first argument.
3383 // Return result in single register (rax).
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003384 __ lea(rcx, StackSpaceOperand(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003385 __ LoadAddress(rdx, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003386 } else {
3387 ASSERT_EQ(2, result_size_);
3388 // Pass a pointer to the result location as the first argument.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003389 __ lea(rcx, StackSpaceOperand(2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003390 // Pass a pointer to the Arguments object as the second argument.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003391 __ lea(rdx, StackSpaceOperand(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003392 __ LoadAddress(r8, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003393 }
3394
3395#else // _WIN64
3396 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9.
3397 __ movq(rdi, r14); // argc.
Steve Block44f0eee2011-05-26 01:26:41 +01003398 __ movq(rsi, r15); // argv.
3399 __ movq(rdx, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003400#endif
3401 __ call(rbx);
3402 // Result is in rax - do not destroy this register!
3403
3404 if (always_allocate_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +01003405 Operand scope_depth_operand = masm->ExternalOperand(scope_depth);
3406 __ decl(scope_depth_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003407 }
3408
3409 // Check for failure result.
3410 Label failure_returned;
3411 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3412#ifdef _WIN64
3413 // If return value is on the stack, pop it to registers.
3414 if (result_size_ > 1) {
3415 ASSERT_EQ(2, result_size_);
3416 // Read result values stored on stack. Result is stored
3417 // above the four argument mirror slots and the two
3418 // Arguments object slots.
3419 __ movq(rax, Operand(rsp, 6 * kPointerSize));
3420 __ movq(rdx, Operand(rsp, 7 * kPointerSize));
3421 }
3422#endif
3423 __ lea(rcx, Operand(rax, 1));
3424 // Lower 2 bits of rcx are 0 iff rax has failure tag.
3425 __ testl(rcx, Immediate(kFailureTagMask));
3426 __ j(zero, &failure_returned);
3427
3428 // Exit the JavaScript to C++ exit frame.
Steve Block1e0659c2011-05-24 12:43:12 +01003429 __ LeaveExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003430 __ ret(0);
3431
3432 // Handling of failure.
3433 __ bind(&failure_returned);
3434
Ben Murdoch257744e2011-11-30 15:57:28 +00003435 Label retry;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003436 // If the returned exception is RETRY_AFTER_GC continue at retry label
3437 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3438 __ testl(rax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00003439 __ j(zero, &retry, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003440
3441 // Special handling of out of memory exceptions.
3442 __ movq(kScratchRegister, Failure::OutOfMemoryException(), RelocInfo::NONE);
3443 __ cmpq(rax, kScratchRegister);
3444 __ j(equal, throw_out_of_memory_exception);
3445
3446 // Retrieve the pending exception and clear the variable.
Steve Block44f0eee2011-05-26 01:26:41 +01003447 ExternalReference pending_exception_address(
Ben Murdoch589d6972011-11-30 16:04:58 +00003448 Isolate::kPendingExceptionAddress, masm->isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01003449 Operand pending_exception_operand =
3450 masm->ExternalOperand(pending_exception_address);
3451 __ movq(rax, pending_exception_operand);
3452 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
3453 __ movq(pending_exception_operand, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003454
3455 // Special handling of termination exceptions which are uncatchable
3456 // by javascript code.
3457 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
3458 __ j(equal, throw_termination_exception);
3459
3460 // Handle normal exception.
3461 __ jmp(throw_normal_exception);
3462
3463 // Retry.
3464 __ bind(&retry);
3465}
3466
3467
3468void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3469 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003470 __ ThrowUncatchable(type, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003471}
3472
3473
3474void CEntryStub::Generate(MacroAssembler* masm) {
3475 // rax: number of arguments including receiver
3476 // rbx: pointer to C function (C callee-saved)
3477 // rbp: frame pointer of calling JS frame (restored after C call)
3478 // rsp: stack pointer (restored after C call)
3479 // rsi: current context (restored)
3480
3481 // NOTE: Invocations of builtins may return failure objects
3482 // instead of a proper result. The builtin entry handles
3483 // this by performing a garbage collection and retrying the
3484 // builtin once.
3485
3486 // Enter the exit frame that transitions from JavaScript to C++.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003487#ifdef _WIN64
3488 int arg_stack_space = (result_size_ < 2 ? 2 : 4);
3489#else
3490 int arg_stack_space = 0;
3491#endif
Steve Block1e0659c2011-05-24 12:43:12 +01003492 __ EnterExitFrame(arg_stack_space, save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003493
3494 // rax: Holds the context at this point, but should not be used.
3495 // On entry to code generated by GenerateCore, it must hold
3496 // a failure result if the collect_garbage argument to GenerateCore
3497 // is true. This failure result can be the result of code
3498 // generated by a previous call to GenerateCore. The value
3499 // of rax is then passed to Runtime::PerformGC.
3500 // rbx: pointer to builtin function (C callee-saved).
3501 // rbp: frame pointer of exit frame (restored after C call).
3502 // rsp: stack pointer (restored after C call).
3503 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01003504 // r15: argv pointer (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003505
3506 Label throw_normal_exception;
3507 Label throw_termination_exception;
3508 Label throw_out_of_memory_exception;
3509
3510 // Call into the runtime system.
3511 GenerateCore(masm,
3512 &throw_normal_exception,
3513 &throw_termination_exception,
3514 &throw_out_of_memory_exception,
3515 false,
3516 false);
3517
3518 // Do space-specific GC and retry runtime call.
3519 GenerateCore(masm,
3520 &throw_normal_exception,
3521 &throw_termination_exception,
3522 &throw_out_of_memory_exception,
3523 true,
3524 false);
3525
3526 // Do full GC and retry runtime call one final time.
3527 Failure* failure = Failure::InternalError();
3528 __ movq(rax, failure, RelocInfo::NONE);
3529 GenerateCore(masm,
3530 &throw_normal_exception,
3531 &throw_termination_exception,
3532 &throw_out_of_memory_exception,
3533 true,
3534 true);
3535
3536 __ bind(&throw_out_of_memory_exception);
3537 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3538
3539 __ bind(&throw_termination_exception);
3540 GenerateThrowUncatchable(masm, TERMINATION);
3541
3542 __ bind(&throw_normal_exception);
3543 GenerateThrowTOS(masm);
3544}
3545
3546
3547void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
3548 Label invoke, exit;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003549 Label not_outermost_js, not_outermost_js_2;
Steve Block44f0eee2011-05-26 01:26:41 +01003550 { // NOLINT. Scope block confuses linter.
3551 MacroAssembler::NoRootArrayScope uninitialized_root_register(masm);
3552 // Setup frame.
3553 __ push(rbp);
3554 __ movq(rbp, rsp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003555
Steve Block44f0eee2011-05-26 01:26:41 +01003556 // Push the stack frame type marker twice.
3557 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3558 // Scratch register is neither callee-save, nor an argument register on any
3559 // platform. It's free to use at this point.
3560 // Cannot use smi-register for loading yet.
3561 __ movq(kScratchRegister,
3562 reinterpret_cast<uint64_t>(Smi::FromInt(marker)),
3563 RelocInfo::NONE);
3564 __ push(kScratchRegister); // context slot
3565 __ push(kScratchRegister); // function slot
3566 // Save callee-saved registers (X64/Win64 calling conventions).
3567 __ push(r12);
3568 __ push(r13);
3569 __ push(r14);
3570 __ push(r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003571#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01003572 __ push(rdi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
3573 __ push(rsi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003574#endif
Steve Block44f0eee2011-05-26 01:26:41 +01003575 __ push(rbx);
3576 // TODO(X64): On Win64, if we ever use XMM6-XMM15, the low low 64 bits are
3577 // callee save as well.
3578
3579 // Set up the roots and smi constant registers.
3580 // Needs to be done before any further smi loads.
3581 __ InitializeSmiConstantRegister();
3582 __ InitializeRootRegister();
3583 }
3584
3585 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003586
3587 // Save copies of the top frame descriptor on the stack.
Ben Murdoch589d6972011-11-30 16:04:58 +00003588 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01003589 {
3590 Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
3591 __ push(c_entry_fp_operand);
3592 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003593
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003594 // If this is the outermost JS call, set js_entry_sp value.
Ben Murdoch589d6972011-11-30 16:04:58 +00003595 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01003596 __ Load(rax, js_entry_sp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003597 __ testq(rax, rax);
3598 __ j(not_zero, &not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01003599 __ Push(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003600 __ movq(rax, rbp);
Steve Block44f0eee2011-05-26 01:26:41 +01003601 __ Store(js_entry_sp, rax);
Steve Block053d10c2011-06-13 19:13:29 +01003602 Label cont;
3603 __ jmp(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003604 __ bind(&not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01003605 __ Push(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME));
3606 __ bind(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003607
3608 // Call a faked try-block that does the invoke.
3609 __ call(&invoke);
3610
3611 // Caught exception: Store result (exception) in the pending
3612 // exception field in the JSEnv and return a failure sentinel.
Ben Murdoch589d6972011-11-30 16:04:58 +00003613 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01003614 isolate);
3615 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003616 __ movq(rax, Failure::Exception(), RelocInfo::NONE);
3617 __ jmp(&exit);
3618
3619 // Invoke: Link this frame into the handler chain.
3620 __ bind(&invoke);
3621 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER);
3622
3623 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01003624 __ LoadRoot(rax, Heap::kTheHoleValueRootIndex);
3625 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003626
3627 // Fake a receiver (NULL).
3628 __ push(Immediate(0)); // receiver
3629
3630 // Invoke the function by calling through JS entry trampoline
3631 // builtin and pop the faked function when we return. We load the address
3632 // from an external reference instead of inlining the call target address
3633 // directly in the code, because the builtin stubs may not have been
3634 // generated yet at the time this code is generated.
3635 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01003636 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
3637 isolate);
3638 __ Load(rax, construct_entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003639 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003640 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
3641 __ Load(rax, entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003642 }
3643 __ lea(kScratchRegister, FieldOperand(rax, Code::kHeaderSize));
3644 __ call(kScratchRegister);
3645
3646 // Unlink this frame from the handler chain.
Steve Block053d10c2011-06-13 19:13:29 +01003647 __ PopTryHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003648
Steve Block053d10c2011-06-13 19:13:29 +01003649 __ bind(&exit);
Steve Block053d10c2011-06-13 19:13:29 +01003650 // Check if the current stack frame is marked as the outermost JS frame.
3651 __ pop(rbx);
3652 __ Cmp(rbx, Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003653 __ j(not_equal, &not_outermost_js_2);
Steve Block053d10c2011-06-13 19:13:29 +01003654 __ movq(kScratchRegister, js_entry_sp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003655 __ movq(Operand(kScratchRegister, 0), Immediate(0));
3656 __ bind(&not_outermost_js_2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003657
3658 // Restore the top frame descriptor from the stack.
Steve Block053d10c2011-06-13 19:13:29 +01003659 { Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
Steve Block44f0eee2011-05-26 01:26:41 +01003660 __ pop(c_entry_fp_operand);
3661 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003662
3663 // Restore callee-saved registers (X64 conventions).
3664 __ pop(rbx);
3665#ifdef _WIN64
3666 // Callee save on in Win64 ABI, arguments/volatile in AMD64 ABI.
3667 __ pop(rsi);
3668 __ pop(rdi);
3669#endif
3670 __ pop(r15);
3671 __ pop(r14);
3672 __ pop(r13);
3673 __ pop(r12);
3674 __ addq(rsp, Immediate(2 * kPointerSize)); // remove markers
3675
3676 // Restore frame pointer and return.
3677 __ pop(rbp);
3678 __ ret(0);
3679}
3680
3681
3682void InstanceofStub::Generate(MacroAssembler* masm) {
3683 // Implements "value instanceof function" operator.
Steve Block44f0eee2011-05-26 01:26:41 +01003684 // Expected input state with no inline cache:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003685 // rsp[0] : return address
3686 // rsp[1] : function pointer
3687 // rsp[2] : value
Steve Block44f0eee2011-05-26 01:26:41 +01003688 // Expected input state with an inline one-element cache:
3689 // rsp[0] : return address
3690 // rsp[1] : offset from return address to location of inline cache
3691 // rsp[2] : function pointer
3692 // rsp[3] : value
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003693 // Returns a bitwise zero to indicate that the value
3694 // is and instance of the function and anything else to
3695 // indicate that the value is not an instance.
3696
Ben Murdoch8b112d22011-06-08 16:22:53 +01003697 static const int kOffsetToMapCheckValue = 2;
3698 static const int kOffsetToResultValue = 18;
Steve Block44f0eee2011-05-26 01:26:41 +01003699 // The last 4 bytes of the instruction sequence
Ben Murdoch8b112d22011-06-08 16:22:53 +01003700 // movq(rdi, FieldOperand(rax, HeapObject::kMapOffset))
Steve Block44f0eee2011-05-26 01:26:41 +01003701 // Move(kScratchRegister, FACTORY->the_hole_value())
3702 // in front of the hole value address.
3703 static const unsigned int kWordBeforeMapCheckValue = 0xBA49FF78;
3704 // The last 4 bytes of the instruction sequence
3705 // __ j(not_equal, &cache_miss);
3706 // __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
3707 // before the offset of the hole value in the root array.
3708 static const unsigned int kWordBeforeResultValue = 0x458B4909;
3709 // Only the inline check flag is supported on X64.
3710 ASSERT(flags_ == kNoFlags || HasCallSiteInlineCheck());
3711 int extra_stack_space = HasCallSiteInlineCheck() ? kPointerSize : 0;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003712
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003713 // Get the object - go slow case if it's a smi.
3714 Label slow;
Steve Block44f0eee2011-05-26 01:26:41 +01003715
3716 __ movq(rax, Operand(rsp, 2 * kPointerSize + extra_stack_space));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003717 __ JumpIfSmi(rax, &slow);
3718
3719 // Check that the left hand is a JS object. Leave its map in rax.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003720 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003721 __ j(below, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003722 __ CmpInstanceType(rax, LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003723 __ j(above, &slow);
3724
3725 // Get the prototype of the function.
Steve Block44f0eee2011-05-26 01:26:41 +01003726 __ movq(rdx, Operand(rsp, 1 * kPointerSize + extra_stack_space));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003727 // rdx is function, rax is map.
3728
Steve Block44f0eee2011-05-26 01:26:41 +01003729 // If there is a call site cache don't look in the global cache, but do the
3730 // real lookup and update the call site cache.
3731 if (!HasCallSiteInlineCheck()) {
3732 // Look up the function and the map in the instanceof cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00003733 Label miss;
Steve Block44f0eee2011-05-26 01:26:41 +01003734 __ CompareRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00003735 __ j(not_equal, &miss, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01003736 __ CompareRoot(rax, Heap::kInstanceofCacheMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00003737 __ j(not_equal, &miss, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01003738 __ LoadRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
3739 __ ret(2 * kPointerSize);
3740 __ bind(&miss);
3741 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003742
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003743 __ TryGetFunctionPrototype(rdx, rbx, &slow);
3744
3745 // Check that the function prototype is a JS object.
3746 __ JumpIfSmi(rbx, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003747 __ CmpObjectType(rbx, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003748 __ j(below, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003749 __ CmpInstanceType(kScratchRegister, LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003750 __ j(above, &slow);
3751
3752 // Register mapping:
3753 // rax is object map.
3754 // rdx is function.
3755 // rbx is function prototype.
Steve Block44f0eee2011-05-26 01:26:41 +01003756 if (!HasCallSiteInlineCheck()) {
3757 __ StoreRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
3758 __ StoreRoot(rax, Heap::kInstanceofCacheMapRootIndex);
3759 } else {
3760 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
3761 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
3762 __ movq(Operand(kScratchRegister, kOffsetToMapCheckValue), rax);
3763 if (FLAG_debug_code) {
3764 __ movl(rdi, Immediate(kWordBeforeMapCheckValue));
3765 __ cmpl(Operand(kScratchRegister, kOffsetToMapCheckValue - 4), rdi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003766 __ Assert(equal, "InstanceofStub unexpected call site cache (check).");
Steve Block44f0eee2011-05-26 01:26:41 +01003767 }
3768 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003769
3770 __ movq(rcx, FieldOperand(rax, Map::kPrototypeOffset));
3771
3772 // Loop through the prototype chain looking for the function prototype.
Ben Murdoch257744e2011-11-30 15:57:28 +00003773 Label loop, is_instance, is_not_instance;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003774 __ LoadRoot(kScratchRegister, Heap::kNullValueRootIndex);
3775 __ bind(&loop);
3776 __ cmpq(rcx, rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003777 __ j(equal, &is_instance, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003778 __ cmpq(rcx, kScratchRegister);
3779 // The code at is_not_instance assumes that kScratchRegister contains a
3780 // non-zero GCable value (the null object in this case).
Ben Murdoch257744e2011-11-30 15:57:28 +00003781 __ j(equal, &is_not_instance, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003782 __ movq(rcx, FieldOperand(rcx, HeapObject::kMapOffset));
3783 __ movq(rcx, FieldOperand(rcx, Map::kPrototypeOffset));
3784 __ jmp(&loop);
3785
3786 __ bind(&is_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01003787 if (!HasCallSiteInlineCheck()) {
3788 __ xorl(rax, rax);
3789 // Store bitwise zero in the cache. This is a Smi in GC terms.
3790 STATIC_ASSERT(kSmiTag == 0);
3791 __ StoreRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
3792 } else {
3793 // Store offset of true in the root array at the inline check site.
3794 ASSERT((Heap::kTrueValueRootIndex << kPointerSizeLog2) - kRootRegisterBias
3795 == 0xB0 - 0x100);
3796 __ movl(rax, Immediate(0xB0)); // TrueValue is at -10 * kPointerSize.
3797 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
3798 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
3799 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
3800 if (FLAG_debug_code) {
3801 __ movl(rax, Immediate(kWordBeforeResultValue));
3802 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
Ben Murdoch8b112d22011-06-08 16:22:53 +01003803 __ Assert(equal, "InstanceofStub unexpected call site cache (mov).");
Steve Block44f0eee2011-05-26 01:26:41 +01003804 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01003805 __ Set(rax, 0);
Steve Block44f0eee2011-05-26 01:26:41 +01003806 }
3807 __ ret(2 * kPointerSize + extra_stack_space);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003808
3809 __ bind(&is_not_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01003810 if (!HasCallSiteInlineCheck()) {
3811 // We have to store a non-zero value in the cache.
3812 __ StoreRoot(kScratchRegister, Heap::kInstanceofCacheAnswerRootIndex);
3813 } else {
3814 // Store offset of false in the root array at the inline check site.
3815 ASSERT((Heap::kFalseValueRootIndex << kPointerSizeLog2) - kRootRegisterBias
3816 == 0xB8 - 0x100);
3817 __ movl(rax, Immediate(0xB8)); // FalseValue is at -9 * kPointerSize.
3818 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
3819 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
3820 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
3821 if (FLAG_debug_code) {
3822 __ movl(rax, Immediate(kWordBeforeResultValue));
3823 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
3824 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
3825 }
3826 }
3827 __ ret(2 * kPointerSize + extra_stack_space);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003828
3829 // Slow-case: Go through the JavaScript implementation.
3830 __ bind(&slow);
Steve Block44f0eee2011-05-26 01:26:41 +01003831 if (HasCallSiteInlineCheck()) {
3832 // Remove extra value from the stack.
3833 __ pop(rcx);
3834 __ pop(rax);
3835 __ push(rcx);
3836 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003837 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
3838}
3839
3840
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003841// Passing arguments in registers is not supported.
3842Register InstanceofStub::left() { return no_reg; }
Steve Block1e0659c2011-05-24 12:43:12 +01003843
3844
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003845Register InstanceofStub::right() { return no_reg; }
Steve Block1e0659c2011-05-24 12:43:12 +01003846
3847
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003848int CompareStub::MinorKey() {
3849 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
3850 // stubs the never NaN NaN condition is only taken into account if the
3851 // condition is equals.
3852 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
3853 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3854 return ConditionField::encode(static_cast<unsigned>(cc_))
3855 | RegisterField::encode(false) // lhs_ and rhs_ are not used
3856 | StrictField::encode(strict_)
3857 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003858 | IncludeNumberCompareField::encode(include_number_compare_)
3859 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003860}
3861
3862
3863// Unfortunately you have to run without snapshots to see most of these
3864// names in the profile since most compare stubs end up in the snapshot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003865void CompareStub::PrintName(StringStream* stream) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003866 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003867 const char* cc_name;
3868 switch (cc_) {
3869 case less: cc_name = "LT"; break;
3870 case greater: cc_name = "GT"; break;
3871 case less_equal: cc_name = "LE"; break;
3872 case greater_equal: cc_name = "GE"; break;
3873 case equal: cc_name = "EQ"; break;
3874 case not_equal: cc_name = "NE"; break;
3875 default: cc_name = "UnknownCondition"; break;
3876 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003877 bool is_equality = cc_ == equal || cc_ == not_equal;
3878 stream->Add("CompareStub_%s", cc_name);
3879 if (strict_ && is_equality) stream->Add("_STRICT");
3880 if (never_nan_nan_ && is_equality) stream->Add("_NO_NAN");
3881 if (!include_number_compare_) stream->Add("_NO_NUMBER");
3882 if (!include_smi_compare_) stream->Add("_NO_SMI");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003883}
3884
3885
3886// -------------------------------------------------------------------------
3887// StringCharCodeAtGenerator
3888
3889void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
3890 Label flat_string;
3891 Label ascii_string;
3892 Label got_char_code;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003893 Label sliced_string;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003894
3895 // If the receiver is a smi trigger the non-string case.
3896 __ JumpIfSmi(object_, receiver_not_string_);
3897
3898 // Fetch the instance type of the receiver into result register.
3899 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
3900 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3901 // If the receiver is not a string trigger the non-string case.
3902 __ testb(result_, Immediate(kIsNotStringMask));
3903 __ j(not_zero, receiver_not_string_);
3904
3905 // If the index is non-smi trigger the non-smi case.
3906 __ JumpIfNotSmi(index_, &index_not_smi_);
3907
3908 // Put smi-tagged index into scratch register.
3909 __ movq(scratch_, index_);
3910 __ bind(&got_smi_index_);
3911
3912 // Check for index out of range.
3913 __ SmiCompare(scratch_, FieldOperand(object_, String::kLengthOffset));
3914 __ j(above_equal, index_out_of_range_);
3915
3916 // We need special handling for non-flat strings.
3917 STATIC_ASSERT(kSeqStringTag == 0);
3918 __ testb(result_, Immediate(kStringRepresentationMask));
3919 __ j(zero, &flat_string);
3920
3921 // Handle non-flat strings.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003922 __ and_(result_, Immediate(kStringRepresentationMask));
3923 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
3924 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
3925 __ cmpb(result_, Immediate(kExternalStringTag));
3926 __ j(greater, &sliced_string);
3927 __ j(equal, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003928
3929 // ConsString.
3930 // Check whether the right hand side is the empty string (i.e. if
3931 // this is really a flat string in a cons string). If that is not
3932 // the case we would rather go to the runtime system now to flatten
3933 // the string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003934 Label assure_seq_string;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003935 __ CompareRoot(FieldOperand(object_, ConsString::kSecondOffset),
3936 Heap::kEmptyStringRootIndex);
3937 __ j(not_equal, &call_runtime_);
3938 // Get the first of the two strings and load its instance type.
3939 __ movq(object_, FieldOperand(object_, ConsString::kFirstOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003940 __ jmp(&assure_seq_string, Label::kNear);
3941
3942 // SlicedString, unpack and add offset.
3943 __ bind(&sliced_string);
3944 __ addq(scratch_, FieldOperand(object_, SlicedString::kOffsetOffset));
3945 __ movq(object_, FieldOperand(object_, SlicedString::kParentOffset));
3946
3947 __ bind(&assure_seq_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003948 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
3949 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
3950 // If the first cons component is also non-flat, then go to runtime.
3951 STATIC_ASSERT(kSeqStringTag == 0);
3952 __ testb(result_, Immediate(kStringRepresentationMask));
3953 __ j(not_zero, &call_runtime_);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003954 __ jmp(&flat_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003955
3956 // Check for 1-byte or 2-byte string.
3957 __ bind(&flat_string);
Ben Murdoch589d6972011-11-30 16:04:58 +00003958 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
3959 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003960 __ testb(result_, Immediate(kStringEncodingMask));
3961 __ j(not_zero, &ascii_string);
3962
3963 // 2-byte string.
3964 // Load the 2-byte character code into the result register.
3965 __ SmiToInteger32(scratch_, scratch_);
3966 __ movzxwl(result_, FieldOperand(object_,
3967 scratch_, times_2,
3968 SeqTwoByteString::kHeaderSize));
3969 __ jmp(&got_char_code);
3970
3971 // ASCII string.
3972 // Load the byte into the result register.
3973 __ bind(&ascii_string);
3974 __ SmiToInteger32(scratch_, scratch_);
3975 __ movzxbl(result_, FieldOperand(object_,
3976 scratch_, times_1,
3977 SeqAsciiString::kHeaderSize));
3978 __ bind(&got_char_code);
3979 __ Integer32ToSmi(result_, result_);
3980 __ bind(&exit_);
3981}
3982
3983
3984void StringCharCodeAtGenerator::GenerateSlow(
3985 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
3986 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
3987
Ben Murdoch257744e2011-11-30 15:57:28 +00003988 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003989 // Index is not a smi.
3990 __ bind(&index_not_smi_);
3991 // If index is a heap number, try converting it to an integer.
Ben Murdoch257744e2011-11-30 15:57:28 +00003992 __ CheckMap(index_,
3993 factory->heap_number_map(),
3994 index_not_number_,
3995 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003996 call_helper.BeforeCall(masm);
3997 __ push(object_);
3998 __ push(index_);
3999 __ push(index_); // Consumed by runtime conversion function.
4000 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4001 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4002 } else {
4003 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4004 // NumberToSmi discards numbers that are not exact integers.
4005 __ CallRuntime(Runtime::kNumberToSmi, 1);
4006 }
4007 if (!scratch_.is(rax)) {
4008 // Save the conversion result before the pop instructions below
4009 // have a chance to overwrite it.
4010 __ movq(scratch_, rax);
4011 }
4012 __ pop(index_);
4013 __ pop(object_);
4014 // Reload the instance type.
4015 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
4016 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4017 call_helper.AfterCall(masm);
4018 // If index is still not a smi, it must be out of range.
4019 __ JumpIfNotSmi(scratch_, index_out_of_range_);
4020 // Otherwise, return to the fast path.
4021 __ jmp(&got_smi_index_);
4022
4023 // Call runtime. We get here when the receiver is a string and the
4024 // index is a number, but the code of getting the actual character
4025 // is too complex (e.g., when the string needs to be flattened).
4026 __ bind(&call_runtime_);
4027 call_helper.BeforeCall(masm);
4028 __ push(object_);
4029 __ push(index_);
4030 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4031 if (!result_.is(rax)) {
4032 __ movq(result_, rax);
4033 }
4034 call_helper.AfterCall(masm);
4035 __ jmp(&exit_);
4036
4037 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4038}
4039
4040
4041// -------------------------------------------------------------------------
4042// StringCharFromCodeGenerator
4043
4044void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4045 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4046 __ JumpIfNotSmi(code_, &slow_case_);
4047 __ SmiCompare(code_, Smi::FromInt(String::kMaxAsciiCharCode));
4048 __ j(above, &slow_case_);
4049
4050 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
4051 SmiIndex index = masm->SmiToIndex(kScratchRegister, code_, kPointerSizeLog2);
4052 __ movq(result_, FieldOperand(result_, index.reg, index.scale,
4053 FixedArray::kHeaderSize));
4054 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
4055 __ j(equal, &slow_case_);
4056 __ bind(&exit_);
4057}
4058
4059
4060void StringCharFromCodeGenerator::GenerateSlow(
4061 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4062 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4063
4064 __ bind(&slow_case_);
4065 call_helper.BeforeCall(masm);
4066 __ push(code_);
4067 __ CallRuntime(Runtime::kCharFromCode, 1);
4068 if (!result_.is(rax)) {
4069 __ movq(result_, rax);
4070 }
4071 call_helper.AfterCall(masm);
4072 __ jmp(&exit_);
4073
4074 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4075}
4076
4077
4078// -------------------------------------------------------------------------
4079// StringCharAtGenerator
4080
4081void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4082 char_code_at_generator_.GenerateFast(masm);
4083 char_from_code_generator_.GenerateFast(masm);
4084}
4085
4086
4087void StringCharAtGenerator::GenerateSlow(
4088 MacroAssembler* masm, const RuntimeCallHelper& call_helper) {
4089 char_code_at_generator_.GenerateSlow(masm, call_helper);
4090 char_from_code_generator_.GenerateSlow(masm, call_helper);
4091}
4092
4093
4094void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004095 Label string_add_runtime, call_builtin;
4096 Builtins::JavaScript builtin_id = Builtins::ADD;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004097
4098 // Load the two arguments.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004099 __ movq(rax, Operand(rsp, 2 * kPointerSize)); // First argument (left).
4100 __ movq(rdx, Operand(rsp, 1 * kPointerSize)); // Second argument (right).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004101
4102 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004103 if (flags_ == NO_STRING_ADD_FLAGS) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004104 __ JumpIfSmi(rax, &string_add_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004105 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, r8);
4106 __ j(above_equal, &string_add_runtime);
4107
4108 // First argument is a a string, test second.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004109 __ JumpIfSmi(rdx, &string_add_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004110 __ CmpObjectType(rdx, FIRST_NONSTRING_TYPE, r9);
4111 __ j(above_equal, &string_add_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004112 } else {
4113 // Here at least one of the arguments is definitely a string.
4114 // We convert the one that is not known to be a string.
4115 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
4116 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
4117 GenerateConvertArgument(masm, 2 * kPointerSize, rax, rbx, rcx, rdi,
4118 &call_builtin);
4119 builtin_id = Builtins::STRING_ADD_RIGHT;
4120 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
4121 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
4122 GenerateConvertArgument(masm, 1 * kPointerSize, rdx, rbx, rcx, rdi,
4123 &call_builtin);
4124 builtin_id = Builtins::STRING_ADD_LEFT;
4125 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004126 }
4127
4128 // Both arguments are strings.
4129 // rax: first string
4130 // rdx: second string
4131 // Check if either of the strings are empty. In that case return the other.
Ben Murdoch257744e2011-11-30 15:57:28 +00004132 Label second_not_zero_length, both_not_zero_length;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004133 __ movq(rcx, FieldOperand(rdx, String::kLengthOffset));
4134 __ SmiTest(rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004135 __ j(not_zero, &second_not_zero_length, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004136 // Second string is empty, result is first string which is already in rax.
Steve Block44f0eee2011-05-26 01:26:41 +01004137 Counters* counters = masm->isolate()->counters();
4138 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004139 __ ret(2 * kPointerSize);
4140 __ bind(&second_not_zero_length);
4141 __ movq(rbx, FieldOperand(rax, String::kLengthOffset));
4142 __ SmiTest(rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004143 __ j(not_zero, &both_not_zero_length, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004144 // First string is empty, result is second string which is in rdx.
4145 __ movq(rax, rdx);
Steve Block44f0eee2011-05-26 01:26:41 +01004146 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004147 __ ret(2 * kPointerSize);
4148
4149 // Both strings are non-empty.
4150 // rax: first string
4151 // rbx: length of first string
4152 // rcx: length of second string
4153 // rdx: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004154 // r8: map of first string (if flags_ == NO_STRING_ADD_FLAGS)
4155 // r9: map of second string (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004156 Label string_add_flat_result, longer_than_two;
4157 __ bind(&both_not_zero_length);
4158
4159 // If arguments where known to be strings, maps are not loaded to r8 and r9
4160 // by the code above.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004161 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004162 __ movq(r8, FieldOperand(rax, HeapObject::kMapOffset));
4163 __ movq(r9, FieldOperand(rdx, HeapObject::kMapOffset));
4164 }
4165 // Get the instance types of the two strings as they will be needed soon.
4166 __ movzxbl(r8, FieldOperand(r8, Map::kInstanceTypeOffset));
4167 __ movzxbl(r9, FieldOperand(r9, Map::kInstanceTypeOffset));
4168
4169 // Look at the length of the result of adding the two strings.
4170 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue / 2);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004171 __ SmiAdd(rbx, rbx, rcx);
Steve Block44f0eee2011-05-26 01:26:41 +01004172 // Use the symbol table when adding two one character strings, as it
4173 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004174 __ SmiCompare(rbx, Smi::FromInt(2));
4175 __ j(not_equal, &longer_than_two);
4176
4177 // Check that both strings are non-external ascii strings.
4178 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r8, r9, rbx, rcx,
4179 &string_add_runtime);
4180
4181 // Get the two characters forming the sub string.
4182 __ movzxbq(rbx, FieldOperand(rax, SeqAsciiString::kHeaderSize));
4183 __ movzxbq(rcx, FieldOperand(rdx, SeqAsciiString::kHeaderSize));
4184
4185 // Try to lookup two character string in symbol table. If it is not found
4186 // just allocate a new one.
4187 Label make_two_character_string, make_flat_ascii_string;
4188 StringHelper::GenerateTwoCharacterSymbolTableProbe(
Steve Block44f0eee2011-05-26 01:26:41 +01004189 masm, rbx, rcx, r14, r11, rdi, r15, &make_two_character_string);
4190 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004191 __ ret(2 * kPointerSize);
4192
4193 __ bind(&make_two_character_string);
4194 __ Set(rbx, 2);
4195 __ jmp(&make_flat_ascii_string);
4196
4197 __ bind(&longer_than_two);
4198 // Check if resulting string will be flat.
4199 __ SmiCompare(rbx, Smi::FromInt(String::kMinNonFlatLength));
4200 __ j(below, &string_add_flat_result);
4201 // Handle exceptionally long strings in the runtime system.
4202 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
4203 __ SmiCompare(rbx, Smi::FromInt(String::kMaxLength));
4204 __ j(above, &string_add_runtime);
4205
4206 // If result is not supposed to be flat, allocate a cons string object. If
4207 // both strings are ascii the result is an ascii cons string.
4208 // rax: first string
4209 // rbx: length of resulting flat string
4210 // rdx: second string
4211 // r8: instance type of first string
4212 // r9: instance type of second string
4213 Label non_ascii, allocated, ascii_data;
4214 __ movl(rcx, r8);
4215 __ and_(rcx, r9);
Ben Murdoch589d6972011-11-30 16:04:58 +00004216 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
4217 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
4218 __ testl(rcx, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004219 __ j(zero, &non_ascii);
4220 __ bind(&ascii_data);
4221 // Allocate an acsii cons string.
4222 __ AllocateAsciiConsString(rcx, rdi, no_reg, &string_add_runtime);
4223 __ bind(&allocated);
4224 // Fill the fields of the cons string.
4225 __ movq(FieldOperand(rcx, ConsString::kLengthOffset), rbx);
4226 __ movq(FieldOperand(rcx, ConsString::kHashFieldOffset),
4227 Immediate(String::kEmptyHashField));
4228 __ movq(FieldOperand(rcx, ConsString::kFirstOffset), rax);
4229 __ movq(FieldOperand(rcx, ConsString::kSecondOffset), rdx);
4230 __ movq(rax, rcx);
Steve Block44f0eee2011-05-26 01:26:41 +01004231 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004232 __ ret(2 * kPointerSize);
4233 __ bind(&non_ascii);
4234 // At least one of the strings is two-byte. Check whether it happens
4235 // to contain only ascii characters.
4236 // rcx: first instance type AND second instance type.
4237 // r8: first instance type.
4238 // r9: second instance type.
4239 __ testb(rcx, Immediate(kAsciiDataHintMask));
4240 __ j(not_zero, &ascii_data);
4241 __ xor_(r8, r9);
4242 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4243 __ andb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
4244 __ cmpb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
4245 __ j(equal, &ascii_data);
4246 // Allocate a two byte cons string.
Ben Murdoch589d6972011-11-30 16:04:58 +00004247 __ AllocateTwoByteConsString(rcx, rdi, no_reg, &string_add_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004248 __ jmp(&allocated);
4249
4250 // Handle creating a flat result. First check that both strings are not
4251 // external strings.
4252 // rax: first string
4253 // rbx: length of resulting flat string as smi
4254 // rdx: second string
4255 // r8: instance type of first string
4256 // r9: instance type of first string
4257 __ bind(&string_add_flat_result);
4258 __ SmiToInteger32(rbx, rbx);
4259 __ movl(rcx, r8);
4260 __ and_(rcx, Immediate(kStringRepresentationMask));
4261 __ cmpl(rcx, Immediate(kExternalStringTag));
4262 __ j(equal, &string_add_runtime);
4263 __ movl(rcx, r9);
4264 __ and_(rcx, Immediate(kStringRepresentationMask));
4265 __ cmpl(rcx, Immediate(kExternalStringTag));
4266 __ j(equal, &string_add_runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004267 // We cannot encounter sliced strings here since:
4268 STATIC_ASSERT(SlicedString::kMinLength >= String::kMinNonFlatLength);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004269 // Now check if both strings are ascii strings.
4270 // rax: first string
4271 // rbx: length of resulting flat string
4272 // rdx: second string
4273 // r8: instance type of first string
4274 // r9: instance type of second string
4275 Label non_ascii_string_add_flat_result;
Ben Murdoch589d6972011-11-30 16:04:58 +00004276 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
4277 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
4278 __ testl(r8, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004279 __ j(zero, &non_ascii_string_add_flat_result);
Ben Murdoch589d6972011-11-30 16:04:58 +00004280 __ testl(r9, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004281 __ j(zero, &string_add_runtime);
4282
4283 __ bind(&make_flat_ascii_string);
4284 // Both strings are ascii strings. As they are short they are both flat.
4285 __ AllocateAsciiString(rcx, rbx, rdi, r14, r11, &string_add_runtime);
4286 // rcx: result string
4287 __ movq(rbx, rcx);
4288 // Locate first character of result.
4289 __ addq(rcx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4290 // Locate first character of first argument
4291 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
4292 __ addq(rax, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4293 // rax: first char of first argument
4294 // rbx: result string
4295 // rcx: first character of result
4296 // rdx: second string
4297 // rdi: length of first argument
4298 StringHelper::GenerateCopyCharacters(masm, rcx, rax, rdi, true);
4299 // Locate first character of second argument.
4300 __ SmiToInteger32(rdi, FieldOperand(rdx, String::kLengthOffset));
4301 __ addq(rdx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4302 // rbx: result string
4303 // rcx: next character of result
4304 // rdx: first char of second argument
4305 // rdi: length of second argument
4306 StringHelper::GenerateCopyCharacters(masm, rcx, rdx, rdi, true);
4307 __ movq(rax, rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01004308 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004309 __ ret(2 * kPointerSize);
4310
4311 // Handle creating a flat two byte result.
4312 // rax: first string - known to be two byte
4313 // rbx: length of resulting flat string
4314 // rdx: second string
4315 // r8: instance type of first string
4316 // r9: instance type of first string
4317 __ bind(&non_ascii_string_add_flat_result);
Ben Murdoch589d6972011-11-30 16:04:58 +00004318 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
4319 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
4320 __ and_(r9, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004321 __ j(not_zero, &string_add_runtime);
4322 // Both strings are two byte strings. As they are short they are both
4323 // flat.
4324 __ AllocateTwoByteString(rcx, rbx, rdi, r14, r11, &string_add_runtime);
4325 // rcx: result string
4326 __ movq(rbx, rcx);
4327 // Locate first character of result.
4328 __ addq(rcx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4329 // Locate first character of first argument.
4330 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
4331 __ addq(rax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4332 // rax: first char of first argument
4333 // rbx: result string
4334 // rcx: first character of result
4335 // rdx: second argument
4336 // rdi: length of first argument
4337 StringHelper::GenerateCopyCharacters(masm, rcx, rax, rdi, false);
4338 // Locate first character of second argument.
4339 __ SmiToInteger32(rdi, FieldOperand(rdx, String::kLengthOffset));
4340 __ addq(rdx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4341 // rbx: result string
4342 // rcx: next character of result
4343 // rdx: first char of second argument
4344 // rdi: length of second argument
4345 StringHelper::GenerateCopyCharacters(masm, rcx, rdx, rdi, false);
4346 __ movq(rax, rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01004347 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004348 __ ret(2 * kPointerSize);
4349
4350 // Just jump to runtime to add the two strings.
4351 __ bind(&string_add_runtime);
4352 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004353
4354 if (call_builtin.is_linked()) {
4355 __ bind(&call_builtin);
4356 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
4357 }
4358}
4359
4360
4361void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
4362 int stack_offset,
4363 Register arg,
4364 Register scratch1,
4365 Register scratch2,
4366 Register scratch3,
4367 Label* slow) {
4368 // First check if the argument is already a string.
4369 Label not_string, done;
4370 __ JumpIfSmi(arg, &not_string);
4371 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
4372 __ j(below, &done);
4373
4374 // Check the number to string cache.
4375 Label not_cached;
4376 __ bind(&not_string);
4377 // Puts the cached result into scratch1.
4378 NumberToStringStub::GenerateLookupNumberStringCache(masm,
4379 arg,
4380 scratch1,
4381 scratch2,
4382 scratch3,
4383 false,
4384 &not_cached);
4385 __ movq(arg, scratch1);
4386 __ movq(Operand(rsp, stack_offset), arg);
4387 __ jmp(&done);
4388
4389 // Check if the argument is a safe string wrapper.
4390 __ bind(&not_cached);
4391 __ JumpIfSmi(arg, slow);
4392 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
4393 __ j(not_equal, slow);
4394 __ testb(FieldOperand(scratch1, Map::kBitField2Offset),
4395 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
4396 __ j(zero, slow);
4397 __ movq(arg, FieldOperand(arg, JSValue::kValueOffset));
4398 __ movq(Operand(rsp, stack_offset), arg);
4399
4400 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004401}
4402
4403
4404void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
4405 Register dest,
4406 Register src,
4407 Register count,
4408 bool ascii) {
4409 Label loop;
4410 __ bind(&loop);
4411 // This loop just copies one character at a time, as it is only used for very
4412 // short strings.
4413 if (ascii) {
4414 __ movb(kScratchRegister, Operand(src, 0));
4415 __ movb(Operand(dest, 0), kScratchRegister);
4416 __ incq(src);
4417 __ incq(dest);
4418 } else {
4419 __ movzxwl(kScratchRegister, Operand(src, 0));
4420 __ movw(Operand(dest, 0), kScratchRegister);
4421 __ addq(src, Immediate(2));
4422 __ addq(dest, Immediate(2));
4423 }
4424 __ decl(count);
4425 __ j(not_zero, &loop);
4426}
4427
4428
4429void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
4430 Register dest,
4431 Register src,
4432 Register count,
4433 bool ascii) {
4434 // Copy characters using rep movs of doublewords. Align destination on 4 byte
4435 // boundary before starting rep movs. Copy remaining characters after running
4436 // rep movs.
4437 // Count is positive int32, dest and src are character pointers.
4438 ASSERT(dest.is(rdi)); // rep movs destination
4439 ASSERT(src.is(rsi)); // rep movs source
4440 ASSERT(count.is(rcx)); // rep movs count
4441
4442 // Nothing to do for zero characters.
Ben Murdoch257744e2011-11-30 15:57:28 +00004443 Label done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004444 __ testl(count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00004445 __ j(zero, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004446
4447 // Make count the number of bytes to copy.
4448 if (!ascii) {
4449 STATIC_ASSERT(2 == sizeof(uc16));
4450 __ addl(count, count);
4451 }
4452
4453 // Don't enter the rep movs if there are less than 4 bytes to copy.
Ben Murdoch257744e2011-11-30 15:57:28 +00004454 Label last_bytes;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004455 __ testl(count, Immediate(~7));
Ben Murdoch257744e2011-11-30 15:57:28 +00004456 __ j(zero, &last_bytes, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004457
4458 // Copy from edi to esi using rep movs instruction.
4459 __ movl(kScratchRegister, count);
4460 __ shr(count, Immediate(3)); // Number of doublewords to copy.
4461 __ repmovsq();
4462
4463 // Find number of bytes left.
4464 __ movl(count, kScratchRegister);
4465 __ and_(count, Immediate(7));
4466
4467 // Check if there are more bytes to copy.
4468 __ bind(&last_bytes);
4469 __ testl(count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00004470 __ j(zero, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004471
4472 // Copy remaining characters.
4473 Label loop;
4474 __ bind(&loop);
4475 __ movb(kScratchRegister, Operand(src, 0));
4476 __ movb(Operand(dest, 0), kScratchRegister);
4477 __ incq(src);
4478 __ incq(dest);
4479 __ decl(count);
4480 __ j(not_zero, &loop);
4481
4482 __ bind(&done);
4483}
4484
4485void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4486 Register c1,
4487 Register c2,
4488 Register scratch1,
4489 Register scratch2,
4490 Register scratch3,
4491 Register scratch4,
4492 Label* not_found) {
4493 // Register scratch3 is the general scratch register in this function.
4494 Register scratch = scratch3;
4495
4496 // Make sure that both characters are not digits as such strings has a
4497 // different hash algorithm. Don't try to look for these in the symbol table.
Ben Murdoch257744e2011-11-30 15:57:28 +00004498 Label not_array_index;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004499 __ leal(scratch, Operand(c1, -'0'));
4500 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
Ben Murdoch257744e2011-11-30 15:57:28 +00004501 __ j(above, &not_array_index, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004502 __ leal(scratch, Operand(c2, -'0'));
4503 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
4504 __ j(below_equal, not_found);
4505
4506 __ bind(&not_array_index);
4507 // Calculate the two character string hash.
4508 Register hash = scratch1;
4509 GenerateHashInit(masm, hash, c1, scratch);
4510 GenerateHashAddCharacter(masm, hash, c2, scratch);
4511 GenerateHashGetHash(masm, hash, scratch);
4512
4513 // Collect the two characters in a register.
4514 Register chars = c1;
4515 __ shl(c2, Immediate(kBitsPerByte));
4516 __ orl(chars, c2);
4517
4518 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4519 // hash: hash of two character string.
4520
4521 // Load the symbol table.
4522 Register symbol_table = c2;
4523 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
4524
4525 // Calculate capacity mask from the symbol table capacity.
4526 Register mask = scratch2;
4527 __ SmiToInteger32(mask,
4528 FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
4529 __ decl(mask);
4530
Steve Block44f0eee2011-05-26 01:26:41 +01004531 Register map = scratch4;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004532
4533 // Registers
4534 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4535 // hash: hash of two character string (32-bit int)
4536 // symbol_table: symbol table
4537 // mask: capacity mask (32-bit int)
Steve Block44f0eee2011-05-26 01:26:41 +01004538 // map: -
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004539 // scratch: -
4540
4541 // Perform a number of probes in the symbol table.
4542 static const int kProbes = 4;
4543 Label found_in_symbol_table;
4544 Label next_probe[kProbes];
4545 for (int i = 0; i < kProbes; i++) {
4546 // Calculate entry in symbol table.
4547 __ movl(scratch, hash);
4548 if (i > 0) {
4549 __ addl(scratch, Immediate(SymbolTable::GetProbeOffset(i)));
4550 }
4551 __ andl(scratch, mask);
4552
Steve Block44f0eee2011-05-26 01:26:41 +01004553 // Load the entry from the symbol table.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004554 Register candidate = scratch; // Scratch register contains candidate.
4555 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4556 __ movq(candidate,
4557 FieldOperand(symbol_table,
4558 scratch,
4559 times_pointer_size,
4560 SymbolTable::kElementsStartOffset));
4561
4562 // If entry is undefined no string with this hash can be found.
Ben Murdoch257744e2011-11-30 15:57:28 +00004563 Label is_string;
Steve Block44f0eee2011-05-26 01:26:41 +01004564 __ CmpObjectType(candidate, ODDBALL_TYPE, map);
Ben Murdoch257744e2011-11-30 15:57:28 +00004565 __ j(not_equal, &is_string, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01004566
4567 __ CompareRoot(candidate, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004568 __ j(equal, not_found);
Steve Block44f0eee2011-05-26 01:26:41 +01004569 // Must be null (deleted entry).
4570 __ jmp(&next_probe[i]);
4571
4572 __ bind(&is_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004573
4574 // If length is not 2 the string is not a candidate.
4575 __ SmiCompare(FieldOperand(candidate, String::kLengthOffset),
4576 Smi::FromInt(2));
4577 __ j(not_equal, &next_probe[i]);
4578
4579 // We use kScratchRegister as a temporary register in assumption that
4580 // JumpIfInstanceTypeIsNotSequentialAscii does not use it implicitly
4581 Register temp = kScratchRegister;
4582
4583 // Check that the candidate is a non-external ascii string.
Steve Block44f0eee2011-05-26 01:26:41 +01004584 __ movzxbl(temp, FieldOperand(map, Map::kInstanceTypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004585 __ JumpIfInstanceTypeIsNotSequentialAscii(
4586 temp, temp, &next_probe[i]);
4587
4588 // Check if the two characters match.
4589 __ movl(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
4590 __ andl(temp, Immediate(0x0000ffff));
4591 __ cmpl(chars, temp);
4592 __ j(equal, &found_in_symbol_table);
4593 __ bind(&next_probe[i]);
4594 }
4595
4596 // No matching 2 character string found by probing.
4597 __ jmp(not_found);
4598
4599 // Scratch register contains result when we fall through to here.
4600 Register result = scratch;
4601 __ bind(&found_in_symbol_table);
4602 if (!result.is(rax)) {
4603 __ movq(rax, result);
4604 }
4605}
4606
4607
4608void StringHelper::GenerateHashInit(MacroAssembler* masm,
4609 Register hash,
4610 Register character,
4611 Register scratch) {
4612 // hash = character + (character << 10);
4613 __ movl(hash, character);
4614 __ shll(hash, Immediate(10));
4615 __ addl(hash, character);
4616 // hash ^= hash >> 6;
4617 __ movl(scratch, hash);
4618 __ sarl(scratch, Immediate(6));
4619 __ xorl(hash, scratch);
4620}
4621
4622
4623void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4624 Register hash,
4625 Register character,
4626 Register scratch) {
4627 // hash += character;
4628 __ addl(hash, character);
4629 // hash += hash << 10;
4630 __ movl(scratch, hash);
4631 __ shll(scratch, Immediate(10));
4632 __ addl(hash, scratch);
4633 // hash ^= hash >> 6;
4634 __ movl(scratch, hash);
4635 __ sarl(scratch, Immediate(6));
4636 __ xorl(hash, scratch);
4637}
4638
4639
4640void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4641 Register hash,
4642 Register scratch) {
4643 // hash += hash << 3;
4644 __ leal(hash, Operand(hash, hash, times_8, 0));
4645 // hash ^= hash >> 11;
4646 __ movl(scratch, hash);
4647 __ sarl(scratch, Immediate(11));
4648 __ xorl(hash, scratch);
4649 // hash += hash << 15;
4650 __ movl(scratch, hash);
4651 __ shll(scratch, Immediate(15));
4652 __ addl(hash, scratch);
4653
4654 // if (hash == 0) hash = 27;
4655 Label hash_not_zero;
4656 __ j(not_zero, &hash_not_zero);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004657 __ Set(hash, 27);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004658 __ bind(&hash_not_zero);
4659}
4660
4661void SubStringStub::Generate(MacroAssembler* masm) {
4662 Label runtime;
4663
4664 // Stack frame on entry.
4665 // rsp[0]: return address
4666 // rsp[8]: to
4667 // rsp[16]: from
4668 // rsp[24]: string
4669
4670 const int kToOffset = 1 * kPointerSize;
4671 const int kFromOffset = kToOffset + kPointerSize;
4672 const int kStringOffset = kFromOffset + kPointerSize;
4673 const int kArgumentsSize = (kStringOffset + kPointerSize) - kToOffset;
4674
4675 // Make sure first argument is a string.
4676 __ movq(rax, Operand(rsp, kStringOffset));
4677 STATIC_ASSERT(kSmiTag == 0);
4678 __ testl(rax, Immediate(kSmiTagMask));
4679 __ j(zero, &runtime);
4680 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
4681 __ j(NegateCondition(is_string), &runtime);
4682
4683 // rax: string
4684 // rbx: instance type
4685 // Calculate length of sub string using the smi values.
4686 Label result_longer_than_two;
4687 __ movq(rcx, Operand(rsp, kToOffset));
4688 __ movq(rdx, Operand(rsp, kFromOffset));
Ben Murdochf87a2032010-10-22 12:50:53 +01004689 __ JumpUnlessBothNonNegativeSmi(rcx, rdx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004690
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004691 __ SmiSub(rcx, rcx, rdx); // Overflow doesn't happen.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004692 __ cmpq(FieldOperand(rax, String::kLengthOffset), rcx);
4693 Label return_rax;
4694 __ j(equal, &return_rax);
4695 // Special handling of sub-strings of length 1 and 2. One character strings
4696 // are handled in the runtime system (looked up in the single character
4697 // cache). Two character strings are looked for in the symbol cache.
4698 __ SmiToInteger32(rcx, rcx);
4699 __ cmpl(rcx, Immediate(2));
4700 __ j(greater, &result_longer_than_two);
4701 __ j(less, &runtime);
4702
4703 // Sub string of length 2 requested.
4704 // rax: string
4705 // rbx: instance type
4706 // rcx: sub string length (value is 2)
4707 // rdx: from index (smi)
4708 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &runtime);
4709
4710 // Get the two characters forming the sub string.
4711 __ SmiToInteger32(rdx, rdx); // From index is no longer smi.
4712 __ movzxbq(rbx, FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize));
4713 __ movzxbq(rcx,
4714 FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize + 1));
4715
4716 // Try to lookup two character string in symbol table.
4717 Label make_two_character_string;
4718 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4719 masm, rbx, rcx, rax, rdx, rdi, r14, &make_two_character_string);
4720 __ ret(3 * kPointerSize);
4721
4722 __ bind(&make_two_character_string);
4723 // Setup registers for allocating the two character string.
4724 __ movq(rax, Operand(rsp, kStringOffset));
4725 __ movq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
4726 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
4727 __ Set(rcx, 2);
4728
Ben Murdoch589d6972011-11-30 16:04:58 +00004729 if (FLAG_string_slices) {
4730 Label copy_routine;
4731 // If coming from the make_two_character_string path, the string
4732 // is too short to be sliced anyways.
4733 STATIC_ASSERT(2 < SlicedString::kMinLength);
4734 __ jmp(&copy_routine);
4735 __ bind(&result_longer_than_two);
4736
4737 // rax: string
4738 // rbx: instance type
4739 // rcx: sub string length
4740 // rdx: from index (smi)
4741 Label allocate_slice, sliced_string, seq_string;
4742 __ cmpq(rcx, Immediate(SlicedString::kMinLength));
4743 // Short slice. Copy instead of slicing.
4744 __ j(less, &copy_routine);
4745 STATIC_ASSERT(kSeqStringTag == 0);
4746 __ testb(rbx, Immediate(kStringRepresentationMask));
4747 __ j(zero, &seq_string, Label::kNear);
4748 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
4749 STATIC_ASSERT(kIsIndirectStringMask != 0);
4750 __ testb(rbx, Immediate(kIsIndirectStringMask));
4751 // External string. Jump to runtime.
4752 __ j(zero, &runtime);
4753
4754 __ testb(rbx, Immediate(kSlicedNotConsMask));
4755 __ j(not_zero, &sliced_string, Label::kNear);
4756 // Cons string. Check whether it is flat, then fetch first part.
4757 __ CompareRoot(FieldOperand(rax, ConsString::kSecondOffset),
4758 Heap::kEmptyStringRootIndex);
4759 __ j(not_equal, &runtime);
4760 __ movq(rdi, FieldOperand(rax, ConsString::kFirstOffset));
4761 __ jmp(&allocate_slice, Label::kNear);
4762
4763 __ bind(&sliced_string);
4764 // Sliced string. Fetch parent and correct start index by offset.
4765 __ addq(rdx, FieldOperand(rax, SlicedString::kOffsetOffset));
4766 __ movq(rdi, FieldOperand(rax, SlicedString::kParentOffset));
4767 __ jmp(&allocate_slice, Label::kNear);
4768
4769 __ bind(&seq_string);
4770 // Sequential string. Just move string to the right register.
4771 __ movq(rdi, rax);
4772
4773 __ bind(&allocate_slice);
4774 // edi: underlying subject string
4775 // ebx: instance type of original subject string
4776 // edx: offset
4777 // ecx: length
4778 // Allocate new sliced string. At this point we do not reload the instance
4779 // type including the string encoding because we simply rely on the info
4780 // provided by the original string. It does not matter if the original
4781 // string's encoding is wrong because we always have to recheck encoding of
4782 // the newly created string's parent anyways due to externalized strings.
4783 Label two_byte_slice, set_slice_header;
4784 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
4785 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
4786 __ testb(rbx, Immediate(kStringEncodingMask));
4787 __ j(zero, &two_byte_slice, Label::kNear);
4788 __ AllocateAsciiSlicedString(rax, rbx, no_reg, &runtime);
4789 __ jmp(&set_slice_header, Label::kNear);
4790 __ bind(&two_byte_slice);
4791 __ AllocateTwoByteSlicedString(rax, rbx, no_reg, &runtime);
4792 __ bind(&set_slice_header);
4793 __ movq(FieldOperand(rax, SlicedString::kOffsetOffset), rdx);
4794 __ Integer32ToSmi(rcx, rcx);
4795 __ movq(FieldOperand(rax, SlicedString::kLengthOffset), rcx);
4796 __ movq(FieldOperand(rax, SlicedString::kParentOffset), rdi);
4797 __ movq(FieldOperand(rax, SlicedString::kHashFieldOffset),
4798 Immediate(String::kEmptyHashField));
4799 __ jmp(&return_rax);
4800
4801 __ bind(&copy_routine);
4802 } else {
4803 __ bind(&result_longer_than_two);
4804 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004805
4806 // rax: string
4807 // rbx: instance type
4808 // rcx: result string length
4809 // Check for flat ascii string
4810 Label non_ascii_flat;
4811 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &non_ascii_flat);
4812
4813 // Allocate the result.
4814 __ AllocateAsciiString(rax, rcx, rbx, rdx, rdi, &runtime);
4815
4816 // rax: result string
4817 // rcx: result string length
4818 __ movq(rdx, rsi); // esi used by following code.
4819 // Locate first character of result.
4820 __ lea(rdi, FieldOperand(rax, SeqAsciiString::kHeaderSize));
4821 // Load string argument and locate character of sub string start.
4822 __ movq(rsi, Operand(rsp, kStringOffset));
4823 __ movq(rbx, Operand(rsp, kFromOffset));
4824 {
4825 SmiIndex smi_as_index = masm->SmiToIndex(rbx, rbx, times_1);
4826 __ lea(rsi, Operand(rsi, smi_as_index.reg, smi_as_index.scale,
4827 SeqAsciiString::kHeaderSize - kHeapObjectTag));
4828 }
4829
4830 // rax: result string
4831 // rcx: result length
4832 // rdx: original value of rsi
4833 // rdi: first character of result
4834 // rsi: character of sub string start
4835 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, true);
4836 __ movq(rsi, rdx); // Restore rsi.
Steve Block44f0eee2011-05-26 01:26:41 +01004837 Counters* counters = masm->isolate()->counters();
4838 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004839 __ ret(kArgumentsSize);
4840
4841 __ bind(&non_ascii_flat);
4842 // rax: string
4843 // rbx: instance type & kStringRepresentationMask | kStringEncodingMask
4844 // rcx: result string length
4845 // Check for sequential two byte string
4846 __ cmpb(rbx, Immediate(kSeqStringTag | kTwoByteStringTag));
4847 __ j(not_equal, &runtime);
4848
4849 // Allocate the result.
4850 __ AllocateTwoByteString(rax, rcx, rbx, rdx, rdi, &runtime);
4851
4852 // rax: result string
4853 // rcx: result string length
4854 __ movq(rdx, rsi); // esi used by following code.
4855 // Locate first character of result.
4856 __ lea(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
4857 // Load string argument and locate character of sub string start.
4858 __ movq(rsi, Operand(rsp, kStringOffset));
4859 __ movq(rbx, Operand(rsp, kFromOffset));
4860 {
4861 SmiIndex smi_as_index = masm->SmiToIndex(rbx, rbx, times_2);
4862 __ lea(rsi, Operand(rsi, smi_as_index.reg, smi_as_index.scale,
4863 SeqAsciiString::kHeaderSize - kHeapObjectTag));
4864 }
4865
4866 // rax: result string
4867 // rcx: result length
4868 // rdx: original value of rsi
4869 // rdi: first character of result
4870 // rsi: character of sub string start
4871 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, false);
4872 __ movq(rsi, rdx); // Restore esi.
4873
4874 __ bind(&return_rax);
Steve Block44f0eee2011-05-26 01:26:41 +01004875 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004876 __ ret(kArgumentsSize);
4877
4878 // Just jump to runtime to create the sub string.
4879 __ bind(&runtime);
4880 __ TailCallRuntime(Runtime::kSubString, 3, 1);
4881}
4882
4883
Ben Murdoch257744e2011-11-30 15:57:28 +00004884void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
4885 Register left,
4886 Register right,
4887 Register scratch1,
4888 Register scratch2) {
4889 Register length = scratch1;
4890
4891 // Compare lengths.
4892 Label check_zero_length;
4893 __ movq(length, FieldOperand(left, String::kLengthOffset));
4894 __ SmiCompare(length, FieldOperand(right, String::kLengthOffset));
4895 __ j(equal, &check_zero_length, Label::kNear);
4896 __ Move(rax, Smi::FromInt(NOT_EQUAL));
4897 __ ret(0);
4898
4899 // Check if the length is zero.
4900 Label compare_chars;
4901 __ bind(&check_zero_length);
4902 STATIC_ASSERT(kSmiTag == 0);
4903 __ SmiTest(length);
4904 __ j(not_zero, &compare_chars, Label::kNear);
4905 __ Move(rax, Smi::FromInt(EQUAL));
4906 __ ret(0);
4907
4908 // Compare characters.
4909 __ bind(&compare_chars);
4910 Label strings_not_equal;
4911 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
4912 &strings_not_equal, Label::kNear);
4913
4914 // Characters are equal.
4915 __ Move(rax, Smi::FromInt(EQUAL));
4916 __ ret(0);
4917
4918 // Characters are not equal.
4919 __ bind(&strings_not_equal);
4920 __ Move(rax, Smi::FromInt(NOT_EQUAL));
4921 __ ret(0);
4922}
4923
4924
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004925void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
4926 Register left,
4927 Register right,
4928 Register scratch1,
4929 Register scratch2,
4930 Register scratch3,
4931 Register scratch4) {
4932 // Ensure that you can always subtract a string length from a non-negative
4933 // number (e.g. another length).
4934 STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
4935
4936 // Find minimum length and length difference.
4937 __ movq(scratch1, FieldOperand(left, String::kLengthOffset));
4938 __ movq(scratch4, scratch1);
4939 __ SmiSub(scratch4,
4940 scratch4,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004941 FieldOperand(right, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004942 // Register scratch4 now holds left.length - right.length.
4943 const Register length_difference = scratch4;
Ben Murdoch257744e2011-11-30 15:57:28 +00004944 Label left_shorter;
4945 __ j(less, &left_shorter, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004946 // The right string isn't longer that the left one.
4947 // Get the right string's length by subtracting the (non-negative) difference
4948 // from the left string's length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004949 __ SmiSub(scratch1, scratch1, length_difference);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004950 __ bind(&left_shorter);
4951 // Register scratch1 now holds Min(left.length, right.length).
4952 const Register min_length = scratch1;
4953
Ben Murdoch257744e2011-11-30 15:57:28 +00004954 Label compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004955 // If min-length is zero, go directly to comparing lengths.
4956 __ SmiTest(min_length);
Ben Murdoch257744e2011-11-30 15:57:28 +00004957 __ j(zero, &compare_lengths, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004958
Ben Murdoch257744e2011-11-30 15:57:28 +00004959 // Compare loop.
4960 Label result_not_equal;
4961 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
4962 &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004963
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004964 // Completed loop without finding different characters.
4965 // Compare lengths (precomputed).
4966 __ bind(&compare_lengths);
4967 __ SmiTest(length_difference);
Ben Murdoch257744e2011-11-30 15:57:28 +00004968 __ j(not_zero, &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004969
4970 // Result is EQUAL.
4971 __ Move(rax, Smi::FromInt(EQUAL));
4972 __ ret(0);
4973
Ben Murdoch257744e2011-11-30 15:57:28 +00004974 Label result_greater;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004975 __ bind(&result_not_equal);
4976 // Unequal comparison of left to right, either character or length.
Ben Murdoch257744e2011-11-30 15:57:28 +00004977 __ j(greater, &result_greater, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004978
4979 // Result is LESS.
4980 __ Move(rax, Smi::FromInt(LESS));
4981 __ ret(0);
4982
4983 // Result is GREATER.
4984 __ bind(&result_greater);
4985 __ Move(rax, Smi::FromInt(GREATER));
4986 __ ret(0);
4987}
4988
4989
Ben Murdoch257744e2011-11-30 15:57:28 +00004990void StringCompareStub::GenerateAsciiCharsCompareLoop(
4991 MacroAssembler* masm,
4992 Register left,
4993 Register right,
4994 Register length,
4995 Register scratch,
4996 Label* chars_not_equal,
4997 Label::Distance near_jump) {
4998 // Change index to run from -length to -1 by adding length to string
4999 // start. This means that loop ends when index reaches zero, which
5000 // doesn't need an additional compare.
5001 __ SmiToInteger32(length, length);
5002 __ lea(left,
5003 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5004 __ lea(right,
5005 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5006 __ neg(length);
5007 Register index = length; // index = -length;
5008
5009 // Compare loop.
5010 Label loop;
5011 __ bind(&loop);
5012 __ movb(scratch, Operand(left, index, times_1, 0));
5013 __ cmpb(scratch, Operand(right, index, times_1, 0));
5014 __ j(not_equal, chars_not_equal, near_jump);
5015 __ addq(index, Immediate(1));
5016 __ j(not_zero, &loop);
5017}
5018
5019
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005020void StringCompareStub::Generate(MacroAssembler* masm) {
5021 Label runtime;
5022
5023 // Stack frame on entry.
5024 // rsp[0]: return address
5025 // rsp[8]: right string
5026 // rsp[16]: left string
5027
5028 __ movq(rdx, Operand(rsp, 2 * kPointerSize)); // left
5029 __ movq(rax, Operand(rsp, 1 * kPointerSize)); // right
5030
5031 // Check for identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00005032 Label not_same;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005033 __ cmpq(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005034 __ j(not_equal, &not_same, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005035 __ Move(rax, Smi::FromInt(EQUAL));
Steve Block44f0eee2011-05-26 01:26:41 +01005036 Counters* counters = masm->isolate()->counters();
5037 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005038 __ ret(2 * kPointerSize);
5039
5040 __ bind(&not_same);
5041
5042 // Check that both are sequential ASCII strings.
5043 __ JumpIfNotBothSequentialAsciiStrings(rdx, rax, rcx, rbx, &runtime);
5044
5045 // Inline comparison of ascii strings.
Steve Block44f0eee2011-05-26 01:26:41 +01005046 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005047 // Drop arguments from the stack
5048 __ pop(rcx);
5049 __ addq(rsp, Immediate(2 * kPointerSize));
5050 __ push(rcx);
5051 GenerateCompareFlatAsciiStrings(masm, rdx, rax, rcx, rbx, rdi, r8);
5052
5053 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5054 // tagged as a small integer.
5055 __ bind(&runtime);
5056 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5057}
5058
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005059
Ben Murdochb0fe1622011-05-05 13:52:32 +01005060void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005061 ASSERT(state_ == CompareIC::SMIS);
Ben Murdoch257744e2011-11-30 15:57:28 +00005062 Label miss;
5063 __ JumpIfNotBothSmi(rdx, rax, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005064
5065 if (GetCondition() == equal) {
5066 // For equality we do not care about the sign of the result.
5067 __ subq(rax, rdx);
5068 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00005069 Label done;
Steve Block1e0659c2011-05-24 12:43:12 +01005070 __ subq(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005071 __ j(no_overflow, &done, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005072 // Correct sign of result in case of overflow.
5073 __ SmiNot(rdx, rdx);
5074 __ bind(&done);
5075 __ movq(rax, rdx);
5076 }
5077 __ ret(0);
5078
5079 __ bind(&miss);
5080 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005081}
5082
5083
5084void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005085 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5086
Ben Murdoch257744e2011-11-30 15:57:28 +00005087 Label generic_stub;
5088 Label unordered;
5089 Label miss;
Steve Block1e0659c2011-05-24 12:43:12 +01005090 Condition either_smi = masm->CheckEitherSmi(rax, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005091 __ j(either_smi, &generic_stub, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005092
5093 __ CmpObjectType(rax, HEAP_NUMBER_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005094 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005095 __ CmpObjectType(rdx, HEAP_NUMBER_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005096 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005097
5098 // Load left and right operand
5099 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
5100 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
5101
5102 // Compare operands
5103 __ ucomisd(xmm0, xmm1);
5104
5105 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch257744e2011-11-30 15:57:28 +00005106 __ j(parity_even, &unordered, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005107
5108 // Return a result of -1, 0, or 1, based on EFLAGS.
5109 // Performing mov, because xor would destroy the flag register.
5110 __ movl(rax, Immediate(0));
5111 __ movl(rcx, Immediate(0));
5112 __ setcc(above, rax); // Add one to zero if carry clear and not equal.
5113 __ sbbq(rax, rcx); // Subtract one if below (aka. carry set).
5114 __ ret(0);
5115
5116 __ bind(&unordered);
5117
5118 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5119 __ bind(&generic_stub);
5120 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5121
5122 __ bind(&miss);
5123 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005124}
5125
5126
Ben Murdoch257744e2011-11-30 15:57:28 +00005127void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5128 ASSERT(state_ == CompareIC::SYMBOLS);
5129 ASSERT(GetCondition() == equal);
5130
5131 // Registers containing left and right operands respectively.
5132 Register left = rdx;
5133 Register right = rax;
5134 Register tmp1 = rcx;
5135 Register tmp2 = rbx;
5136
5137 // Check that both operands are heap objects.
5138 Label miss;
5139 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
5140 __ j(cond, &miss, Label::kNear);
5141
5142 // Check that both operands are symbols.
5143 __ movq(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5144 __ movq(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5145 __ movzxbq(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5146 __ movzxbq(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5147 STATIC_ASSERT(kSymbolTag != 0);
5148 __ and_(tmp1, tmp2);
5149 __ testb(tmp1, Immediate(kIsSymbolMask));
5150 __ j(zero, &miss, Label::kNear);
5151
5152 // Symbols are compared by identity.
5153 Label done;
5154 __ cmpq(left, right);
5155 // Make sure rax is non-zero. At this point input operands are
5156 // guaranteed to be non-zero.
5157 ASSERT(right.is(rax));
5158 __ j(not_equal, &done, Label::kNear);
5159 STATIC_ASSERT(EQUAL == 0);
5160 STATIC_ASSERT(kSmiTag == 0);
5161 __ Move(rax, Smi::FromInt(EQUAL));
5162 __ bind(&done);
5163 __ ret(0);
5164
5165 __ bind(&miss);
5166 GenerateMiss(masm);
5167}
5168
5169
5170void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
5171 ASSERT(state_ == CompareIC::STRINGS);
5172 ASSERT(GetCondition() == equal);
5173 Label miss;
5174
5175 // Registers containing left and right operands respectively.
5176 Register left = rdx;
5177 Register right = rax;
5178 Register tmp1 = rcx;
5179 Register tmp2 = rbx;
5180 Register tmp3 = rdi;
5181
5182 // Check that both operands are heap objects.
5183 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
5184 __ j(cond, &miss);
5185
5186 // Check that both operands are strings. This leaves the instance
5187 // types loaded in tmp1 and tmp2.
5188 __ movq(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5189 __ movq(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5190 __ movzxbq(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5191 __ movzxbq(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5192 __ movq(tmp3, tmp1);
5193 STATIC_ASSERT(kNotStringTag != 0);
5194 __ or_(tmp3, tmp2);
5195 __ testb(tmp3, Immediate(kIsNotStringMask));
5196 __ j(not_zero, &miss);
5197
5198 // Fast check for identical strings.
5199 Label not_same;
5200 __ cmpq(left, right);
5201 __ j(not_equal, &not_same, Label::kNear);
5202 STATIC_ASSERT(EQUAL == 0);
5203 STATIC_ASSERT(kSmiTag == 0);
5204 __ Move(rax, Smi::FromInt(EQUAL));
5205 __ ret(0);
5206
5207 // Handle not identical strings.
5208 __ bind(&not_same);
5209
5210 // Check that both strings are symbols. If they are, we're done
5211 // because we already know they are not identical.
5212 Label do_compare;
5213 STATIC_ASSERT(kSymbolTag != 0);
5214 __ and_(tmp1, tmp2);
5215 __ testb(tmp1, Immediate(kIsSymbolMask));
5216 __ j(zero, &do_compare, Label::kNear);
5217 // Make sure rax is non-zero. At this point input operands are
5218 // guaranteed to be non-zero.
5219 ASSERT(right.is(rax));
5220 __ ret(0);
5221
5222 // Check that both strings are sequential ASCII.
5223 Label runtime;
5224 __ bind(&do_compare);
5225 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
5226
5227 // Compare flat ASCII strings. Returns when done.
5228 StringCompareStub::GenerateFlatAsciiStringEquals(
5229 masm, left, right, tmp1, tmp2);
5230
5231 // Handle more complex cases in runtime.
5232 __ bind(&runtime);
5233 __ pop(tmp1); // Return address.
5234 __ push(left);
5235 __ push(right);
5236 __ push(tmp1);
5237 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
5238
5239 __ bind(&miss);
5240 GenerateMiss(masm);
5241}
5242
5243
Ben Murdochb0fe1622011-05-05 13:52:32 +01005244void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005245 ASSERT(state_ == CompareIC::OBJECTS);
Ben Murdoch257744e2011-11-30 15:57:28 +00005246 Label miss;
Steve Block1e0659c2011-05-24 12:43:12 +01005247 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005248 __ j(either_smi, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005249
5250 __ CmpObjectType(rax, JS_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005251 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005252 __ CmpObjectType(rdx, JS_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005253 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005254
5255 ASSERT(GetCondition() == equal);
5256 __ subq(rax, rdx);
5257 __ ret(0);
5258
5259 __ bind(&miss);
5260 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005261}
5262
5263
5264void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005265 // Save the registers.
5266 __ pop(rcx);
5267 __ push(rdx);
5268 __ push(rax);
5269 __ push(rcx);
5270
5271 // Call the runtime system in a fresh internal frame.
Steve Block44f0eee2011-05-26 01:26:41 +01005272 ExternalReference miss =
5273 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
Steve Block1e0659c2011-05-24 12:43:12 +01005274 __ EnterInternalFrame();
5275 __ push(rdx);
5276 __ push(rax);
5277 __ Push(Smi::FromInt(op_));
5278 __ CallExternalReference(miss, 3);
5279 __ LeaveInternalFrame();
5280
5281 // Compute the entry point of the rewritten stub.
5282 __ lea(rdi, FieldOperand(rax, Code::kHeaderSize));
5283
5284 // Restore registers.
5285 __ pop(rcx);
5286 __ pop(rax);
5287 __ pop(rdx);
5288 __ push(rcx);
5289
5290 // Do a tail call to the rewritten stub.
5291 __ jmp(rdi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005292}
5293
Steve Block1e0659c2011-05-24 12:43:12 +01005294
Ben Murdoch257744e2011-11-30 15:57:28 +00005295MaybeObject* StringDictionaryLookupStub::GenerateNegativeLookup(
5296 MacroAssembler* masm,
5297 Label* miss,
5298 Label* done,
5299 Register properties,
5300 String* name,
5301 Register r0) {
5302 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5303 // not equal to the name and kProbes-th slot is not used (its name is the
5304 // undefined value), it guarantees the hash table doesn't contain the
5305 // property. It's true even if some slots represent deleted properties
5306 // (their names are the null value).
5307 for (int i = 0; i < kInlinedProbes; i++) {
5308 // r0 points to properties hash.
5309 // Compute the masked index: (hash + i + i * i) & mask.
5310 Register index = r0;
5311 // Capacity is smi 2^n.
5312 __ SmiToInteger32(index, FieldOperand(properties, kCapacityOffset));
5313 __ decl(index);
5314 __ and_(index,
5315 Immediate(name->Hash() + StringDictionary::GetProbeOffset(i)));
5316
5317 // Scale the index by multiplying by the entry size.
5318 ASSERT(StringDictionary::kEntrySize == 3);
5319 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
5320
5321 Register entity_name = r0;
5322 // Having undefined at this place means the name is not contained.
5323 ASSERT_EQ(kSmiTagSize, 1);
5324 __ movq(entity_name, Operand(properties,
5325 index,
5326 times_pointer_size,
5327 kElementsStartOffset - kHeapObjectTag));
5328 __ Cmp(entity_name, masm->isolate()->factory()->undefined_value());
5329 __ j(equal, done);
5330
5331 // Stop if found the property.
5332 __ Cmp(entity_name, Handle<String>(name));
5333 __ j(equal, miss);
5334
5335 // Check if the entry name is not a symbol.
5336 __ movq(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
5337 __ testb(FieldOperand(entity_name, Map::kInstanceTypeOffset),
5338 Immediate(kIsSymbolMask));
5339 __ j(zero, miss);
5340 }
5341
5342 StringDictionaryLookupStub stub(properties,
5343 r0,
5344 r0,
5345 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
5346 __ Push(Handle<Object>(name));
5347 __ push(Immediate(name->Hash()));
5348 MaybeObject* result = masm->TryCallStub(&stub);
5349 if (result->IsFailure()) return result;
5350 __ testq(r0, r0);
5351 __ j(not_zero, miss);
5352 __ jmp(done);
5353 return result;
5354}
5355
5356
5357// Probe the string dictionary in the |elements| register. Jump to the
5358// |done| label if a property with the given name is found leaving the
5359// index into the dictionary in |r1|. Jump to the |miss| label
5360// otherwise.
5361void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
5362 Label* miss,
5363 Label* done,
5364 Register elements,
5365 Register name,
5366 Register r0,
5367 Register r1) {
5368 // Assert that name contains a string.
5369 if (FLAG_debug_code) __ AbortIfNotString(name);
5370
5371 __ SmiToInteger32(r0, FieldOperand(elements, kCapacityOffset));
5372 __ decl(r0);
5373
5374 for (int i = 0; i < kInlinedProbes; i++) {
5375 // Compute the masked index: (hash + i + i * i) & mask.
5376 __ movl(r1, FieldOperand(name, String::kHashFieldOffset));
5377 __ shrl(r1, Immediate(String::kHashShift));
5378 if (i > 0) {
5379 __ addl(r1, Immediate(StringDictionary::GetProbeOffset(i)));
5380 }
5381 __ and_(r1, r0);
5382
5383 // Scale the index by multiplying by the entry size.
5384 ASSERT(StringDictionary::kEntrySize == 3);
5385 __ lea(r1, Operand(r1, r1, times_2, 0)); // r1 = r1 * 3
5386
5387 // Check if the key is identical to the name.
5388 __ cmpq(name, Operand(elements, r1, times_pointer_size,
5389 kElementsStartOffset - kHeapObjectTag));
5390 __ j(equal, done);
5391 }
5392
5393 StringDictionaryLookupStub stub(elements,
5394 r0,
5395 r1,
5396 POSITIVE_LOOKUP);
5397 __ push(name);
5398 __ movl(r0, FieldOperand(name, String::kHashFieldOffset));
5399 __ shrl(r0, Immediate(String::kHashShift));
5400 __ push(r0);
5401 __ CallStub(&stub);
5402
5403 __ testq(r0, r0);
5404 __ j(zero, miss);
5405 __ jmp(done);
5406}
5407
5408
5409void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
5410 // Stack frame on entry:
5411 // esp[0 * kPointerSize]: return address.
5412 // esp[1 * kPointerSize]: key's hash.
5413 // esp[2 * kPointerSize]: key.
5414 // Registers:
5415 // dictionary_: StringDictionary to probe.
5416 // result_: used as scratch.
5417 // index_: will hold an index of entry if lookup is successful.
5418 // might alias with result_.
5419 // Returns:
5420 // result_ is zero if lookup failed, non zero otherwise.
5421
5422 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
5423
5424 Register scratch = result_;
5425
5426 __ SmiToInteger32(scratch, FieldOperand(dictionary_, kCapacityOffset));
5427 __ decl(scratch);
5428 __ push(scratch);
5429
5430 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5431 // not equal to the name and kProbes-th slot is not used (its name is the
5432 // undefined value), it guarantees the hash table doesn't contain the
5433 // property. It's true even if some slots represent deleted properties
5434 // (their names are the null value).
5435 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
5436 // Compute the masked index: (hash + i + i * i) & mask.
5437 __ movq(scratch, Operand(rsp, 2 * kPointerSize));
5438 if (i > 0) {
5439 __ addl(scratch, Immediate(StringDictionary::GetProbeOffset(i)));
5440 }
5441 __ and_(scratch, Operand(rsp, 0));
5442
5443 // Scale the index by multiplying by the entry size.
5444 ASSERT(StringDictionary::kEntrySize == 3);
5445 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
5446
5447 // Having undefined at this place means the name is not contained.
5448 __ movq(scratch, Operand(dictionary_,
5449 index_,
5450 times_pointer_size,
5451 kElementsStartOffset - kHeapObjectTag));
5452
5453 __ Cmp(scratch, masm->isolate()->factory()->undefined_value());
5454 __ j(equal, &not_in_dictionary);
5455
5456 // Stop if found the property.
5457 __ cmpq(scratch, Operand(rsp, 3 * kPointerSize));
5458 __ j(equal, &in_dictionary);
5459
5460 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
5461 // If we hit a non symbol key during negative lookup
5462 // we have to bailout as this key might be equal to the
5463 // key we are looking for.
5464
5465 // Check if the entry name is not a symbol.
5466 __ movq(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
5467 __ testb(FieldOperand(scratch, Map::kInstanceTypeOffset),
5468 Immediate(kIsSymbolMask));
5469 __ j(zero, &maybe_in_dictionary);
5470 }
5471 }
5472
5473 __ bind(&maybe_in_dictionary);
5474 // If we are doing negative lookup then probing failure should be
5475 // treated as a lookup success. For positive lookup probing failure
5476 // should be treated as lookup failure.
5477 if (mode_ == POSITIVE_LOOKUP) {
5478 __ movq(scratch, Immediate(0));
5479 __ Drop(1);
5480 __ ret(2 * kPointerSize);
5481 }
5482
5483 __ bind(&in_dictionary);
5484 __ movq(scratch, Immediate(1));
5485 __ Drop(1);
5486 __ ret(2 * kPointerSize);
5487
5488 __ bind(&not_in_dictionary);
5489 __ movq(scratch, Immediate(0));
5490 __ Drop(1);
5491 __ ret(2 * kPointerSize);
5492}
5493
5494
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005495#undef __
5496
5497} } // namespace v8::internal
5498
5499#endif // V8_TARGET_ARCH_X64