blob: d30610101cab50d0b6d87bbd7d3ff5ed5ea5547d [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
Ben Murdoch592a9fc2012-03-05 11:04:45 +000071 int map_index = (language_mode_ == CLASSIC_MODE)
72 ? Context::FUNCTION_MAP_INDEX
73 : Context::STRICT_MODE_FUNCTION_MAP_INDEX;
Steve Block44f0eee2011-05-26 01:26:41 +010074
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
Ben Murdochc7cc0282012-03-05 14:35:55 +0000127 // Set up 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
Ben Murdochc7cc0282012-03-05 14:35:55 +0000132 // Set up 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
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000158void FastNewBlockContextStub::Generate(MacroAssembler* masm) {
159 // Stack layout on entry:
160 //
161 // [rsp + (1 * kPointerSize)]: function
162 // [rsp + (2 * kPointerSize)]: serialized scope info
163
164 // Try to allocate the context in new space.
165 Label gc;
166 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
167 __ AllocateInNewSpace(FixedArray::SizeFor(length),
168 rax, rbx, rcx, &gc, TAG_OBJECT);
169
170 // Get the function from the stack.
171 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
172
173 // Get the serialized scope info from the stack.
174 __ movq(rbx, Operand(rsp, 2 * kPointerSize));
175
Ben Murdochc7cc0282012-03-05 14:35:55 +0000176 // Set up the object header.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000177 __ LoadRoot(kScratchRegister, Heap::kBlockContextMapRootIndex);
178 __ movq(FieldOperand(rax, HeapObject::kMapOffset), kScratchRegister);
179 __ Move(FieldOperand(rax, FixedArray::kLengthOffset), Smi::FromInt(length));
180
181 // If this block context is nested in the global context we get a smi
182 // sentinel instead of a function. The block context should get the
183 // canonical empty function of the global context as its closure which
184 // we still have to look up.
185 Label after_sentinel;
186 __ JumpIfNotSmi(rcx, &after_sentinel, Label::kNear);
187 if (FLAG_debug_code) {
188 const char* message = "Expected 0 as a Smi sentinel";
189 __ cmpq(rcx, Immediate(0));
190 __ Assert(equal, message);
191 }
192 __ movq(rcx, GlobalObjectOperand());
193 __ movq(rcx, FieldOperand(rcx, GlobalObject::kGlobalContextOffset));
194 __ movq(rcx, ContextOperand(rcx, Context::CLOSURE_INDEX));
195 __ bind(&after_sentinel);
196
Ben Murdochc7cc0282012-03-05 14:35:55 +0000197 // Set up the fixed slots.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000198 __ movq(ContextOperand(rax, Context::CLOSURE_INDEX), rcx);
199 __ movq(ContextOperand(rax, Context::PREVIOUS_INDEX), rsi);
200 __ movq(ContextOperand(rax, Context::EXTENSION_INDEX), rbx);
201
202 // Copy the global object from the previous context.
203 __ movq(rbx, ContextOperand(rsi, Context::GLOBAL_INDEX));
204 __ movq(ContextOperand(rax, Context::GLOBAL_INDEX), rbx);
205
206 // Initialize the rest of the slots to the hole value.
207 __ LoadRoot(rbx, Heap::kTheHoleValueRootIndex);
208 for (int i = 0; i < slots_; i++) {
209 __ movq(ContextOperand(rax, i + Context::MIN_CONTEXT_SLOTS), rbx);
210 }
211
212 // Return and remove the on-stack parameter.
213 __ movq(rsi, rax);
214 __ ret(2 * kPointerSize);
215
216 // Need to collect. Call into runtime system.
217 __ bind(&gc);
218 __ TailCallRuntime(Runtime::kPushBlockContext, 2, 1);
219}
220
221
222static void GenerateFastCloneShallowArrayCommon(
223 MacroAssembler* masm,
224 int length,
225 FastCloneShallowArrayStub::Mode mode,
226 Label* fail) {
227 // Registers on entry:
228 //
229 // rcx: boilerplate literal array.
230 ASSERT(mode != FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS);
231
232 // All sizes here are multiples of kPointerSize.
233 int elements_size = 0;
234 if (length > 0) {
235 elements_size = mode == FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS
236 ? FixedDoubleArray::SizeFor(length)
237 : FixedArray::SizeFor(length);
238 }
239 int size = JSArray::kSize + elements_size;
240
241 // Allocate both the JS array and the elements array in one big
242 // allocation. This avoids multiple limit checks.
243 __ AllocateInNewSpace(size, rax, rbx, rdx, fail, TAG_OBJECT);
244
245 // Copy the JS array part.
246 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
247 if ((i != JSArray::kElementsOffset) || (length == 0)) {
248 __ movq(rbx, FieldOperand(rcx, i));
249 __ movq(FieldOperand(rax, i), rbx);
250 }
251 }
252
253 if (length > 0) {
254 // Get hold of the elements array of the boilerplate and setup the
255 // elements pointer in the resulting object.
256 __ movq(rcx, FieldOperand(rcx, JSArray::kElementsOffset));
257 __ lea(rdx, Operand(rax, JSArray::kSize));
258 __ movq(FieldOperand(rax, JSArray::kElementsOffset), rdx);
259
260 // Copy the elements array.
261 if (mode == FastCloneShallowArrayStub::CLONE_ELEMENTS) {
262 for (int i = 0; i < elements_size; i += kPointerSize) {
263 __ movq(rbx, FieldOperand(rcx, i));
264 __ movq(FieldOperand(rdx, i), rbx);
265 }
266 } else {
267 ASSERT(mode == FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS);
268 int i;
269 for (i = 0; i < FixedDoubleArray::kHeaderSize; i += kPointerSize) {
270 __ movq(rbx, FieldOperand(rcx, i));
271 __ movq(FieldOperand(rdx, i), rbx);
272 }
273 while (i < elements_size) {
274 __ movsd(xmm0, FieldOperand(rcx, i));
275 __ movsd(FieldOperand(rdx, i), xmm0);
276 i += kDoubleSize;
277 }
278 ASSERT(i == elements_size);
279 }
280 }
281}
282
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100283void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
284 // Stack layout on entry:
285 //
286 // [rsp + kPointerSize]: constant elements.
287 // [rsp + (2 * kPointerSize)]: literal index.
288 // [rsp + (3 * kPointerSize)]: literals array.
289
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100290 // Load boilerplate object into rcx and check if we need to create a
291 // boilerplate.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100292 __ movq(rcx, Operand(rsp, 3 * kPointerSize));
293 __ movq(rax, Operand(rsp, 2 * kPointerSize));
294 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
295 __ movq(rcx,
296 FieldOperand(rcx, index.reg, index.scale, FixedArray::kHeaderSize));
297 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000298 Label slow_case;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100299 __ j(equal, &slow_case);
300
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000301 FastCloneShallowArrayStub::Mode mode = mode_;
302 // rcx is boilerplate object.
303 Factory* factory = masm->isolate()->factory();
304 if (mode == CLONE_ANY_ELEMENTS) {
305 Label double_elements, check_fast_elements;
306 __ movq(rbx, FieldOperand(rcx, JSArray::kElementsOffset));
307 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
308 factory->fixed_cow_array_map());
309 __ j(not_equal, &check_fast_elements);
310 GenerateFastCloneShallowArrayCommon(masm, 0,
311 COPY_ON_WRITE_ELEMENTS, &slow_case);
312 __ ret(3 * kPointerSize);
313
314 __ bind(&check_fast_elements);
315 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
316 factory->fixed_array_map());
317 __ j(not_equal, &double_elements);
318 GenerateFastCloneShallowArrayCommon(masm, length_,
319 CLONE_ELEMENTS, &slow_case);
320 __ ret(3 * kPointerSize);
321
322 __ bind(&double_elements);
323 mode = CLONE_DOUBLE_ELEMENTS;
324 // Fall through to generate the code to handle double elements.
325 }
326
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100327 if (FLAG_debug_code) {
328 const char* message;
329 Heap::RootListIndex expected_map_index;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000330 if (mode == CLONE_ELEMENTS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100331 message = "Expected (writable) fixed array";
332 expected_map_index = Heap::kFixedArrayMapRootIndex;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000333 } else if (mode == CLONE_DOUBLE_ELEMENTS) {
334 message = "Expected (writable) fixed double array";
335 expected_map_index = Heap::kFixedDoubleArrayMapRootIndex;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100336 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000337 ASSERT(mode == COPY_ON_WRITE_ELEMENTS);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100338 message = "Expected copy-on-write fixed array";
339 expected_map_index = Heap::kFixedCOWArrayMapRootIndex;
340 }
341 __ push(rcx);
342 __ movq(rcx, FieldOperand(rcx, JSArray::kElementsOffset));
343 __ CompareRoot(FieldOperand(rcx, HeapObject::kMapOffset),
344 expected_map_index);
345 __ Assert(equal, message);
346 __ pop(rcx);
347 }
348
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000349 GenerateFastCloneShallowArrayCommon(masm, length_, mode, &slow_case);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100350 __ ret(3 * kPointerSize);
351
352 __ bind(&slow_case);
353 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
354}
355
356
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000357void FastCloneShallowObjectStub::Generate(MacroAssembler* masm) {
358 // Stack layout on entry:
359 //
360 // [rsp + kPointerSize]: object literal flags.
361 // [rsp + (2 * kPointerSize)]: constant properties.
362 // [rsp + (3 * kPointerSize)]: literal index.
363 // [rsp + (4 * kPointerSize)]: literals array.
364
365 // Load boilerplate object into ecx and check if we need to create a
366 // boilerplate.
367 Label slow_case;
368 __ movq(rcx, Operand(rsp, 4 * kPointerSize));
369 __ movq(rax, Operand(rsp, 3 * kPointerSize));
370 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
371 __ movq(rcx,
372 FieldOperand(rcx, index.reg, index.scale, FixedArray::kHeaderSize));
373 __ CompareRoot(rcx, Heap::kUndefinedValueRootIndex);
374 __ j(equal, &slow_case);
375
376 // Check that the boilerplate contains only fast properties and we can
377 // statically determine the instance size.
378 int size = JSObject::kHeaderSize + length_ * kPointerSize;
379 __ movq(rax, FieldOperand(rcx, HeapObject::kMapOffset));
380 __ movzxbq(rax, FieldOperand(rax, Map::kInstanceSizeOffset));
381 __ cmpq(rax, Immediate(size >> kPointerSizeLog2));
382 __ j(not_equal, &slow_case);
383
384 // Allocate the JS object and copy header together with all in-object
385 // properties from the boilerplate.
386 __ AllocateInNewSpace(size, rax, rbx, rdx, &slow_case, TAG_OBJECT);
387 for (int i = 0; i < size; i += kPointerSize) {
388 __ movq(rbx, FieldOperand(rcx, i));
389 __ movq(FieldOperand(rax, i), rbx);
390 }
391
392 // Return and remove the on-stack parameters.
393 __ ret(4 * kPointerSize);
394
395 __ bind(&slow_case);
396 __ TailCallRuntime(Runtime::kCreateObjectLiteralShallow, 4, 1);
397}
398
399
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000400// The stub expects its argument on the stack and returns its result in tos_:
401// zero for false, and a non-zero value for true.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100402void ToBooleanStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000403 // This stub overrides SometimesSetsUpAFrame() to return false. That means
404 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000405 Label patch;
406 const Register argument = rax;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000407 const Register map = rdx;
408
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000409 if (!types_.IsEmpty()) {
410 __ movq(argument, Operand(rsp, 1 * kPointerSize));
411 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100412
Ben Murdoch257744e2011-11-30 15:57:28 +0000413 // undefined -> false
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000414 CheckOddball(masm, UNDEFINED, Heap::kUndefinedValueRootIndex, false);
Ben Murdoch257744e2011-11-30 15:57:28 +0000415
416 // Boolean -> its value
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000417 CheckOddball(masm, BOOLEAN, Heap::kFalseValueRootIndex, false);
418 CheckOddball(masm, BOOLEAN, Heap::kTrueValueRootIndex, true);
Ben Murdoch257744e2011-11-30 15:57:28 +0000419
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000420 // 'null' -> false.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000421 CheckOddball(masm, NULL_TYPE, Heap::kNullValueRootIndex, false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100422
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000423 if (types_.Contains(SMI)) {
424 // Smis: 0 -> false, all other -> true
425 Label not_smi;
426 __ JumpIfNotSmi(argument, &not_smi, Label::kNear);
427 // argument contains the correct return value already
428 if (!tos_.is(argument)) {
429 __ movq(tos_, argument);
430 }
431 __ ret(1 * kPointerSize);
432 __ bind(&not_smi);
433 } else if (types_.NeedsMap()) {
434 // If we need a map later and have a Smi -> patch.
435 __ JumpIfSmi(argument, &patch, Label::kNear);
436 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100437
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000438 if (types_.NeedsMap()) {
439 __ movq(map, FieldOperand(argument, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100440
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000441 if (types_.CanBeUndetectable()) {
442 __ testb(FieldOperand(map, Map::kBitFieldOffset),
443 Immediate(1 << Map::kIsUndetectable));
444 // Undetectable -> false.
445 Label not_undetectable;
446 __ j(zero, &not_undetectable, Label::kNear);
447 __ Set(tos_, 0);
448 __ ret(1 * kPointerSize);
449 __ bind(&not_undetectable);
450 }
451 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100452
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000453 if (types_.Contains(SPEC_OBJECT)) {
454 // spec object -> true.
455 Label not_js_object;
456 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
457 __ j(below, &not_js_object, Label::kNear);
458 // argument contains the correct return value already.
459 if (!tos_.is(argument)) {
460 __ Set(tos_, 1);
461 }
462 __ ret(1 * kPointerSize);
463 __ bind(&not_js_object);
464 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100465
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000466 if (types_.Contains(STRING)) {
467 // String value -> false iff empty.
468 Label not_string;
469 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
470 __ j(above_equal, &not_string, Label::kNear);
471 __ movq(tos_, FieldOperand(argument, String::kLengthOffset));
472 __ ret(1 * kPointerSize); // the string length is OK as the return value
473 __ bind(&not_string);
474 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100475
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000476 if (types_.Contains(HEAP_NUMBER)) {
477 // heap number -> false iff +0, -0, or NaN.
478 Label not_heap_number, false_result;
479 __ CompareRoot(map, Heap::kHeapNumberMapRootIndex);
480 __ j(not_equal, &not_heap_number, Label::kNear);
481 __ xorps(xmm0, xmm0);
482 __ ucomisd(xmm0, FieldOperand(argument, HeapNumber::kValueOffset));
483 __ j(zero, &false_result, Label::kNear);
484 // argument contains the correct return value already.
485 if (!tos_.is(argument)) {
486 __ Set(tos_, 1);
487 }
488 __ ret(1 * kPointerSize);
489 __ bind(&false_result);
490 __ Set(tos_, 0);
491 __ ret(1 * kPointerSize);
492 __ bind(&not_heap_number);
493 }
494
495 __ bind(&patch);
496 GenerateTypeTransition(masm);
497}
498
499
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000500void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
501 __ PushCallerSaved(save_doubles_);
502 const int argument_count = 1;
503 __ PrepareCallCFunction(argument_count);
504#ifdef _WIN64
505 __ LoadAddress(rcx, ExternalReference::isolate_address());
506#else
507 __ LoadAddress(rdi, ExternalReference::isolate_address());
508#endif
509
510 AllowExternalCallThatCantCauseGC scope(masm);
511 __ CallCFunction(
512 ExternalReference::store_buffer_overflow_function(masm->isolate()),
513 argument_count);
514 __ PopCallerSaved(save_doubles_);
515 __ ret(0);
516}
517
518
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000519void ToBooleanStub::CheckOddball(MacroAssembler* masm,
520 Type type,
521 Heap::RootListIndex value,
522 bool result) {
523 const Register argument = rax;
524 if (types_.Contains(type)) {
525 // If we see an expected oddball, return its ToBoolean value tos_.
526 Label different_value;
527 __ CompareRoot(argument, value);
528 __ j(not_equal, &different_value, Label::kNear);
529 if (!result) {
530 // If we have to return zero, there is no way around clearing tos_.
531 __ Set(tos_, 0);
532 } else if (!tos_.is(argument)) {
533 // If we have to return non-zero, we can re-use the argument if it is the
534 // same register as the result, because we never see Smi-zero here.
535 __ Set(tos_, 1);
536 }
537 __ ret(1 * kPointerSize);
538 __ bind(&different_value);
539 }
540}
541
542
543void ToBooleanStub::GenerateTypeTransition(MacroAssembler* masm) {
544 __ pop(rcx); // Get return address, operand is now on top of stack.
545 __ Push(Smi::FromInt(tos_.code()));
546 __ Push(Smi::FromInt(types_.ToByte()));
547 __ push(rcx); // Push return address.
548 // Patch the caller to an appropriate specialized stub and return the
549 // operation result to the caller of the stub.
550 __ TailCallExternalReference(
551 ExternalReference(IC_Utility(IC::kToBoolean_Patch), masm->isolate()),
552 3,
553 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100554}
555
556
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100557class FloatingPointHelper : public AllStatic {
558 public:
559 // Load the operands from rdx and rax into xmm0 and xmm1, as doubles.
560 // If the operands are not both numbers, jump to not_numbers.
561 // Leaves rdx and rax unchanged. SmiOperands assumes both are smis.
562 // NumberOperands assumes both are smis or heap numbers.
563 static void LoadSSE2SmiOperands(MacroAssembler* masm);
564 static void LoadSSE2NumberOperands(MacroAssembler* masm);
565 static void LoadSSE2UnknownOperands(MacroAssembler* masm,
566 Label* not_numbers);
567
568 // Takes the operands in rdx and rax and loads them as integers in rax
569 // and rcx.
570 static void LoadAsIntegers(MacroAssembler* masm,
571 Label* operand_conversion_failure,
572 Register heap_number_map);
573 // As above, but we know the operands to be numbers. In that case,
574 // conversion can't fail.
575 static void LoadNumbersAsIntegers(MacroAssembler* masm);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100576
577 // Tries to convert two values to smis losslessly.
578 // This fails if either argument is not a Smi nor a HeapNumber,
579 // or if it's a HeapNumber with a value that can't be converted
580 // losslessly to a Smi. In that case, control transitions to the
581 // on_not_smis label.
582 // On success, either control goes to the on_success label (if one is
583 // provided), or it falls through at the end of the code (if on_success
584 // is NULL).
585 // On success, both first and second holds Smi tagged values.
586 // One of first or second must be non-Smi when entering.
587 static void NumbersToSmis(MacroAssembler* masm,
588 Register first,
589 Register second,
590 Register scratch1,
591 Register scratch2,
592 Register scratch3,
593 Label* on_success,
594 Label* on_not_smis);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100595};
596
597
Ben Murdoch257744e2011-11-30 15:57:28 +0000598// Get the integer part of a heap number.
599// Overwrites the contents of rdi, rbx and rcx. Result cannot be rdi or rbx.
600void IntegerConvert(MacroAssembler* masm,
601 Register result,
602 Register source) {
603 // Result may be rcx. If result and source are the same register, source will
604 // be overwritten.
605 ASSERT(!result.is(rdi) && !result.is(rbx));
606 // TODO(lrn): When type info reaches here, if value is a 32-bit integer, use
607 // cvttsd2si (32-bit version) directly.
608 Register double_exponent = rbx;
609 Register double_value = rdi;
610 Label done, exponent_63_plus;
611 // Get double and extract exponent.
612 __ movq(double_value, FieldOperand(source, HeapNumber::kValueOffset));
613 // Clear result preemptively, in case we need to return zero.
614 __ xorl(result, result);
615 __ movq(xmm0, double_value); // Save copy in xmm0 in case we need it there.
616 // Double to remove sign bit, shift exponent down to least significant bits.
617 // and subtract bias to get the unshifted, unbiased exponent.
618 __ lea(double_exponent, Operand(double_value, double_value, times_1, 0));
619 __ shr(double_exponent, Immediate(64 - HeapNumber::kExponentBits));
620 __ subl(double_exponent, Immediate(HeapNumber::kExponentBias));
621 // Check whether the exponent is too big for a 63 bit unsigned integer.
622 __ cmpl(double_exponent, Immediate(63));
623 __ j(above_equal, &exponent_63_plus, Label::kNear);
624 // Handle exponent range 0..62.
625 __ cvttsd2siq(result, xmm0);
626 __ jmp(&done, Label::kNear);
627
628 __ bind(&exponent_63_plus);
629 // Exponent negative or 63+.
630 __ cmpl(double_exponent, Immediate(83));
631 // If exponent negative or above 83, number contains no significant bits in
632 // the range 0..2^31, so result is zero, and rcx already holds zero.
633 __ j(above, &done, Label::kNear);
634
635 // Exponent in rage 63..83.
636 // Mantissa * 2^exponent contains bits in the range 2^0..2^31, namely
637 // the least significant exponent-52 bits.
638
639 // Negate low bits of mantissa if value is negative.
640 __ addq(double_value, double_value); // Move sign bit to carry.
641 __ sbbl(result, result); // And convert carry to -1 in result register.
642 // if scratch2 is negative, do (scratch2-1)^-1, otherwise (scratch2-0)^0.
643 __ addl(double_value, result);
644 // Do xor in opposite directions depending on where we want the result
645 // (depending on whether result is rcx or not).
646
647 if (result.is(rcx)) {
648 __ xorl(double_value, result);
649 // Left shift mantissa by (exponent - mantissabits - 1) to save the
650 // bits that have positional values below 2^32 (the extra -1 comes from the
651 // doubling done above to move the sign bit into the carry flag).
652 __ leal(rcx, Operand(double_exponent, -HeapNumber::kMantissaBits - 1));
653 __ shll_cl(double_value);
654 __ movl(result, double_value);
655 } else {
656 // As the then-branch, but move double-value to result before shifting.
657 __ xorl(result, double_value);
658 __ leal(rcx, Operand(double_exponent, -HeapNumber::kMantissaBits - 1));
659 __ shll_cl(result);
660 }
661
662 __ bind(&done);
663}
664
665
Ben Murdoch257744e2011-11-30 15:57:28 +0000666void UnaryOpStub::Generate(MacroAssembler* masm) {
667 switch (operand_type_) {
668 case UnaryOpIC::UNINITIALIZED:
669 GenerateTypeTransition(masm);
670 break;
671 case UnaryOpIC::SMI:
672 GenerateSmiStub(masm);
673 break;
674 case UnaryOpIC::HEAP_NUMBER:
675 GenerateHeapNumberStub(masm);
676 break;
677 case UnaryOpIC::GENERIC:
678 GenerateGenericStub(masm);
679 break;
680 }
681}
682
683
684void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
685 __ pop(rcx); // Save return address.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000686
687 __ push(rax); // the operand
Ben Murdoch257744e2011-11-30 15:57:28 +0000688 __ Push(Smi::FromInt(op_));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000689 __ Push(Smi::FromInt(mode_));
Ben Murdoch257744e2011-11-30 15:57:28 +0000690 __ Push(Smi::FromInt(operand_type_));
691
692 __ push(rcx); // Push return address.
693
694 // Patch the caller to an appropriate specialized stub and return the
695 // operation result to the caller of the stub.
696 __ TailCallExternalReference(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000697 ExternalReference(IC_Utility(IC::kUnaryOp_Patch), masm->isolate()), 4, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000698}
699
700
701// TODO(svenpanne): Use virtual functions instead of switch.
702void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
703 switch (op_) {
704 case Token::SUB:
705 GenerateSmiStubSub(masm);
706 break;
707 case Token::BIT_NOT:
708 GenerateSmiStubBitNot(masm);
709 break;
710 default:
711 UNREACHABLE();
712 }
713}
714
715
716void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
717 Label slow;
718 GenerateSmiCodeSub(masm, &slow, &slow, Label::kNear, Label::kNear);
719 __ bind(&slow);
720 GenerateTypeTransition(masm);
721}
722
723
724void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
725 Label non_smi;
726 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
727 __ bind(&non_smi);
728 GenerateTypeTransition(masm);
729}
730
731
732void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
733 Label* non_smi,
734 Label* slow,
735 Label::Distance non_smi_near,
736 Label::Distance slow_near) {
737 Label done;
738 __ JumpIfNotSmi(rax, non_smi, non_smi_near);
739 __ SmiNeg(rax, rax, &done, Label::kNear);
740 __ jmp(slow, slow_near);
741 __ bind(&done);
742 __ ret(0);
743}
744
745
746void UnaryOpStub::GenerateSmiCodeBitNot(MacroAssembler* masm,
747 Label* non_smi,
748 Label::Distance non_smi_near) {
749 __ JumpIfNotSmi(rax, non_smi, non_smi_near);
750 __ SmiNot(rax, rax);
751 __ ret(0);
752}
753
754
755// TODO(svenpanne): Use virtual functions instead of switch.
756void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
757 switch (op_) {
758 case Token::SUB:
759 GenerateHeapNumberStubSub(masm);
760 break;
761 case Token::BIT_NOT:
762 GenerateHeapNumberStubBitNot(masm);
763 break;
764 default:
765 UNREACHABLE();
766 }
767}
768
769
770void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
771 Label non_smi, slow, call_builtin;
772 GenerateSmiCodeSub(masm, &non_smi, &call_builtin, Label::kNear);
773 __ bind(&non_smi);
774 GenerateHeapNumberCodeSub(masm, &slow);
775 __ bind(&slow);
776 GenerateTypeTransition(masm);
777 __ bind(&call_builtin);
778 GenerateGenericCodeFallback(masm);
779}
780
781
782void UnaryOpStub::GenerateHeapNumberStubBitNot(
783 MacroAssembler* masm) {
784 Label non_smi, slow;
785 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
786 __ bind(&non_smi);
787 GenerateHeapNumberCodeBitNot(masm, &slow);
788 __ bind(&slow);
789 GenerateTypeTransition(masm);
790}
791
792
793void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
794 Label* slow) {
795 // Check if the operand is a heap number.
796 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
797 Heap::kHeapNumberMapRootIndex);
798 __ j(not_equal, slow);
799
800 // Operand is a float, negate its value by flipping the sign bit.
801 if (mode_ == UNARY_OVERWRITE) {
802 __ Set(kScratchRegister, 0x01);
803 __ shl(kScratchRegister, Immediate(63));
804 __ xor_(FieldOperand(rax, HeapNumber::kValueOffset), kScratchRegister);
805 } else {
806 // Allocate a heap number before calculating the answer,
807 // so we don't have an untagged double around during GC.
808 Label slow_allocate_heapnumber, heapnumber_allocated;
809 __ AllocateHeapNumber(rcx, rbx, &slow_allocate_heapnumber);
810 __ jmp(&heapnumber_allocated);
811
812 __ bind(&slow_allocate_heapnumber);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000813 {
814 FrameScope scope(masm, StackFrame::INTERNAL);
815 __ push(rax);
816 __ CallRuntime(Runtime::kNumberAlloc, 0);
817 __ movq(rcx, rax);
818 __ pop(rax);
819 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000820 __ bind(&heapnumber_allocated);
821 // rcx: allocated 'empty' number
822
823 // Copy the double value to the new heap number, flipping the sign.
824 __ movq(rdx, FieldOperand(rax, HeapNumber::kValueOffset));
825 __ Set(kScratchRegister, 0x01);
826 __ shl(kScratchRegister, Immediate(63));
827 __ xor_(rdx, kScratchRegister); // Flip sign.
828 __ movq(FieldOperand(rcx, HeapNumber::kValueOffset), rdx);
829 __ movq(rax, rcx);
830 }
831 __ ret(0);
832}
833
834
835void UnaryOpStub::GenerateHeapNumberCodeBitNot(MacroAssembler* masm,
836 Label* slow) {
837 // Check if the operand is a heap number.
838 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
839 Heap::kHeapNumberMapRootIndex);
840 __ j(not_equal, slow);
841
842 // Convert the heap number in rax to an untagged integer in rcx.
843 IntegerConvert(masm, rax, rax);
844
845 // Do the bitwise operation and smi tag the result.
846 __ notl(rax);
847 __ Integer32ToSmi(rax, rax);
848 __ ret(0);
849}
850
851
852// TODO(svenpanne): Use virtual functions instead of switch.
853void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
854 switch (op_) {
855 case Token::SUB:
856 GenerateGenericStubSub(masm);
857 break;
858 case Token::BIT_NOT:
859 GenerateGenericStubBitNot(masm);
860 break;
861 default:
862 UNREACHABLE();
863 }
864}
865
866
867void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
868 Label non_smi, slow;
869 GenerateSmiCodeSub(masm, &non_smi, &slow, Label::kNear);
870 __ bind(&non_smi);
871 GenerateHeapNumberCodeSub(masm, &slow);
872 __ bind(&slow);
873 GenerateGenericCodeFallback(masm);
874}
875
876
877void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
878 Label non_smi, slow;
879 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
880 __ bind(&non_smi);
881 GenerateHeapNumberCodeBitNot(masm, &slow);
882 __ bind(&slow);
883 GenerateGenericCodeFallback(masm);
884}
885
886
887void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
888 // Handle the slow case by jumping to the JavaScript builtin.
889 __ pop(rcx); // pop return address
890 __ push(rax);
891 __ push(rcx); // push return address
892 switch (op_) {
893 case Token::SUB:
894 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
895 break;
896 case Token::BIT_NOT:
897 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
898 break;
899 default:
900 UNREACHABLE();
901 }
902}
903
904
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000905void UnaryOpStub::PrintName(StringStream* stream) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000906 const char* op_name = Token::Name(op_);
907 const char* overwrite_name = NULL; // Make g++ happy.
908 switch (mode_) {
909 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
910 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
911 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000912 stream->Add("UnaryOpStub_%s_%s_%s",
913 op_name,
914 overwrite_name,
915 UnaryOpIC::GetName(operand_type_));
Ben Murdoch257744e2011-11-30 15:57:28 +0000916}
917
918
919void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100920 __ pop(rcx); // Save return address.
921 __ push(rdx);
922 __ push(rax);
923 // Left and right arguments are now on top.
924 // Push this stub's key. Although the operation and the type info are
925 // encoded into the key, the encoding is opaque, so push them too.
926 __ Push(Smi::FromInt(MinorKey()));
927 __ Push(Smi::FromInt(op_));
928 __ Push(Smi::FromInt(operands_type_));
929
930 __ push(rcx); // Push return address.
931
932 // Patch the caller to an appropriate specialized stub and return the
933 // operation result to the caller of the stub.
934 __ TailCallExternalReference(
Ben Murdoch257744e2011-11-30 15:57:28 +0000935 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
Steve Block44f0eee2011-05-26 01:26:41 +0100936 masm->isolate()),
Ben Murdoch086aeea2011-05-13 15:57:08 +0100937 5,
938 1);
939}
940
941
Ben Murdoch257744e2011-11-30 15:57:28 +0000942void BinaryOpStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000943 // Explicitly allow generation of nested stubs. It is safe here because
944 // generation code does not use any raw pointers.
945 AllowStubCallsScope allow_stub_calls(masm, true);
946
Ben Murdoch086aeea2011-05-13 15:57:08 +0100947 switch (operands_type_) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000948 case BinaryOpIC::UNINITIALIZED:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100949 GenerateTypeTransition(masm);
950 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000951 case BinaryOpIC::SMI:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100952 GenerateSmiStub(masm);
953 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000954 case BinaryOpIC::INT32:
Steve Block1e0659c2011-05-24 12:43:12 +0100955 UNREACHABLE();
956 // The int32 case is identical to the Smi case. We avoid creating this
957 // ic state on x64.
Ben Murdoch086aeea2011-05-13 15:57:08 +0100958 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000959 case BinaryOpIC::HEAP_NUMBER:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100960 GenerateHeapNumberStub(masm);
961 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000962 case BinaryOpIC::ODDBALL:
Steve Block44f0eee2011-05-26 01:26:41 +0100963 GenerateOddballStub(masm);
964 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000965 case BinaryOpIC::BOTH_STRING:
966 GenerateBothStringStub(masm);
967 break;
968 case BinaryOpIC::STRING:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100969 GenerateStringStub(masm);
970 break;
Ben Murdoch257744e2011-11-30 15:57:28 +0000971 case BinaryOpIC::GENERIC:
Ben Murdoch086aeea2011-05-13 15:57:08 +0100972 GenerateGeneric(masm);
973 break;
974 default:
975 UNREACHABLE();
976 }
977}
978
979
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000980void BinaryOpStub::PrintName(StringStream* stream) {
Ben Murdoch086aeea2011-05-13 15:57:08 +0100981 const char* op_name = Token::Name(op_);
982 const char* overwrite_name;
983 switch (mode_) {
984 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
985 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
986 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
987 default: overwrite_name = "UnknownOverwrite"; break;
988 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000989 stream->Add("BinaryOpStub_%s_%s_%s",
990 op_name,
991 overwrite_name,
992 BinaryOpIC::GetName(operands_type_));
Ben Murdoch086aeea2011-05-13 15:57:08 +0100993}
994
995
Ben Murdoch257744e2011-11-30 15:57:28 +0000996void BinaryOpStub::GenerateSmiCode(
997 MacroAssembler* masm,
Ben Murdoch086aeea2011-05-13 15:57:08 +0100998 Label* slow,
999 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
Steve Block1e0659c2011-05-24 12:43:12 +01001000
Ben Murdoch257744e2011-11-30 15:57:28 +00001001 // Arguments to BinaryOpStub are in rdx and rax.
Steve Block1e0659c2011-05-24 12:43:12 +01001002 Register left = rdx;
1003 Register right = rax;
1004
Ben Murdoch8b112d22011-06-08 16:22:53 +01001005 // We only generate heapnumber answers for overflowing calculations
1006 // for the four basic arithmetic operations and logical right shift by 0.
1007 bool generate_inline_heapnumber_results =
1008 (allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS) &&
1009 (op_ == Token::ADD || op_ == Token::SUB ||
1010 op_ == Token::MUL || op_ == Token::DIV || op_ == Token::SHR);
Steve Block1e0659c2011-05-24 12:43:12 +01001011
1012 // Smi check of both operands. If op is BIT_OR, the check is delayed
1013 // until after the OR operation.
1014 Label not_smis;
1015 Label use_fp_on_smis;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001016 Label fail;
Steve Block1e0659c2011-05-24 12:43:12 +01001017
1018 if (op_ != Token::BIT_OR) {
1019 Comment smi_check_comment(masm, "-- Smi check arguments");
1020 __ JumpIfNotBothSmi(left, right, &not_smis);
1021 }
1022
Ben Murdoch8b112d22011-06-08 16:22:53 +01001023 Label smi_values;
1024 __ bind(&smi_values);
Steve Block1e0659c2011-05-24 12:43:12 +01001025 // Perform the operation.
1026 Comment perform_smi(masm, "-- Perform smi operation");
1027 switch (op_) {
1028 case Token::ADD:
1029 ASSERT(right.is(rax));
1030 __ SmiAdd(right, right, left, &use_fp_on_smis); // ADD is commutative.
1031 break;
1032
1033 case Token::SUB:
1034 __ SmiSub(left, left, right, &use_fp_on_smis);
1035 __ movq(rax, left);
1036 break;
1037
1038 case Token::MUL:
1039 ASSERT(right.is(rax));
1040 __ SmiMul(right, right, left, &use_fp_on_smis); // MUL is commutative.
1041 break;
1042
1043 case Token::DIV:
1044 // SmiDiv will not accept left in rdx or right in rax.
1045 left = rcx;
1046 right = rbx;
1047 __ movq(rbx, rax);
1048 __ movq(rcx, rdx);
1049 __ SmiDiv(rax, left, right, &use_fp_on_smis);
1050 break;
1051
1052 case Token::MOD:
1053 // SmiMod will not accept left in rdx or right in rax.
1054 left = rcx;
1055 right = rbx;
1056 __ movq(rbx, rax);
1057 __ movq(rcx, rdx);
1058 __ SmiMod(rax, left, right, &use_fp_on_smis);
1059 break;
1060
1061 case Token::BIT_OR: {
1062 ASSERT(right.is(rax));
Ben Murdoch8b112d22011-06-08 16:22:53 +01001063 __ SmiOrIfSmis(right, right, left, &not_smis); // BIT_OR is commutative.
Steve Block1e0659c2011-05-24 12:43:12 +01001064 break;
1065 }
1066 case Token::BIT_XOR:
1067 ASSERT(right.is(rax));
1068 __ SmiXor(right, right, left); // BIT_XOR is commutative.
1069 break;
1070
1071 case Token::BIT_AND:
1072 ASSERT(right.is(rax));
1073 __ SmiAnd(right, right, left); // BIT_AND is commutative.
1074 break;
1075
1076 case Token::SHL:
1077 __ SmiShiftLeft(left, left, right);
1078 __ movq(rax, left);
1079 break;
1080
1081 case Token::SAR:
1082 __ SmiShiftArithmeticRight(left, left, right);
1083 __ movq(rax, left);
1084 break;
1085
1086 case Token::SHR:
Ben Murdoch8b112d22011-06-08 16:22:53 +01001087 __ SmiShiftLogicalRight(left, left, right, &use_fp_on_smis);
Steve Block1e0659c2011-05-24 12:43:12 +01001088 __ movq(rax, left);
1089 break;
1090
1091 default:
1092 UNREACHABLE();
1093 }
1094
1095 // 5. Emit return of result in rax. Some operations have registers pushed.
1096 __ ret(0);
1097
Ben Murdoch8b112d22011-06-08 16:22:53 +01001098 if (use_fp_on_smis.is_linked()) {
1099 // 6. For some operations emit inline code to perform floating point
1100 // operations on known smis (e.g., if the result of the operation
1101 // overflowed the smi range).
1102 __ bind(&use_fp_on_smis);
1103 if (op_ == Token::DIV || op_ == Token::MOD) {
1104 // Restore left and right to rdx and rax.
1105 __ movq(rdx, rcx);
1106 __ movq(rax, rbx);
Steve Block1e0659c2011-05-24 12:43:12 +01001107 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01001108
1109 if (generate_inline_heapnumber_results) {
1110 __ AllocateHeapNumber(rcx, rbx, slow);
1111 Comment perform_float(masm, "-- Perform float operation on smis");
1112 if (op_ == Token::SHR) {
1113 __ SmiToInteger32(left, left);
1114 __ cvtqsi2sd(xmm0, left);
1115 } else {
1116 FloatingPointHelper::LoadSSE2SmiOperands(masm);
1117 switch (op_) {
1118 case Token::ADD: __ addsd(xmm0, xmm1); break;
1119 case Token::SUB: __ subsd(xmm0, xmm1); break;
1120 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1121 case Token::DIV: __ divsd(xmm0, xmm1); break;
1122 default: UNREACHABLE();
1123 }
1124 }
1125 __ movsd(FieldOperand(rcx, HeapNumber::kValueOffset), xmm0);
1126 __ movq(rax, rcx);
1127 __ ret(0);
1128 } else {
1129 __ jmp(&fail);
1130 }
Steve Block1e0659c2011-05-24 12:43:12 +01001131 }
1132
1133 // 7. Non-smi operands reach the end of the code generated by
1134 // GenerateSmiCode, and fall through to subsequent code,
1135 // with the operands in rdx and rax.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001136 // But first we check if non-smi values are HeapNumbers holding
1137 // values that could be smi.
Steve Block1e0659c2011-05-24 12:43:12 +01001138 __ bind(&not_smis);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001139 Comment done_comment(masm, "-- Enter non-smi code");
1140 FloatingPointHelper::NumbersToSmis(masm, left, right, rbx, rdi, rcx,
1141 &smi_values, &fail);
1142 __ jmp(&smi_values);
1143 __ bind(&fail);
Steve Block1e0659c2011-05-24 12:43:12 +01001144}
1145
1146
Ben Murdoch257744e2011-11-30 15:57:28 +00001147void BinaryOpStub::GenerateFloatingPointCode(MacroAssembler* masm,
1148 Label* allocation_failure,
1149 Label* non_numeric_failure) {
Steve Block1e0659c2011-05-24 12:43:12 +01001150 switch (op_) {
1151 case Token::ADD:
1152 case Token::SUB:
1153 case Token::MUL:
1154 case Token::DIV: {
1155 FloatingPointHelper::LoadSSE2UnknownOperands(masm, non_numeric_failure);
1156
1157 switch (op_) {
1158 case Token::ADD: __ addsd(xmm0, xmm1); break;
1159 case Token::SUB: __ subsd(xmm0, xmm1); break;
1160 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1161 case Token::DIV: __ divsd(xmm0, xmm1); break;
1162 default: UNREACHABLE();
1163 }
1164 GenerateHeapResultAllocation(masm, allocation_failure);
1165 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm0);
1166 __ ret(0);
1167 break;
1168 }
1169 case Token::MOD: {
1170 // For MOD we jump to the allocation_failure label, to call runtime.
1171 __ jmp(allocation_failure);
1172 break;
1173 }
1174 case Token::BIT_OR:
1175 case Token::BIT_AND:
1176 case Token::BIT_XOR:
1177 case Token::SAR:
1178 case Token::SHL:
1179 case Token::SHR: {
1180 Label non_smi_shr_result;
1181 Register heap_number_map = r9;
1182 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1183 FloatingPointHelper::LoadAsIntegers(masm, non_numeric_failure,
1184 heap_number_map);
1185 switch (op_) {
1186 case Token::BIT_OR: __ orl(rax, rcx); break;
1187 case Token::BIT_AND: __ andl(rax, rcx); break;
1188 case Token::BIT_XOR: __ xorl(rax, rcx); break;
1189 case Token::SAR: __ sarl_cl(rax); break;
1190 case Token::SHL: __ shll_cl(rax); break;
1191 case Token::SHR: {
1192 __ shrl_cl(rax);
1193 // Check if result is negative. This can only happen for a shift
1194 // by zero.
1195 __ testl(rax, rax);
1196 __ j(negative, &non_smi_shr_result);
1197 break;
1198 }
1199 default: UNREACHABLE();
1200 }
1201 STATIC_ASSERT(kSmiValueSize == 32);
1202 // Tag smi result and return.
1203 __ Integer32ToSmi(rax, rax);
1204 __ Ret();
1205
1206 // Logical shift right can produce an unsigned int32 that is not
1207 // an int32, and so is not in the smi range. Allocate a heap number
1208 // in that case.
1209 if (op_ == Token::SHR) {
1210 __ bind(&non_smi_shr_result);
1211 Label allocation_failed;
1212 __ movl(rbx, rax); // rbx holds result value (uint32 value as int64).
1213 // Allocate heap number in new space.
1214 // Not using AllocateHeapNumber macro in order to reuse
1215 // already loaded heap_number_map.
1216 __ AllocateInNewSpace(HeapNumber::kSize,
1217 rax,
Steve Block053d10c2011-06-13 19:13:29 +01001218 rdx,
Steve Block1e0659c2011-05-24 12:43:12 +01001219 no_reg,
1220 &allocation_failed,
1221 TAG_OBJECT);
1222 // Set the map.
1223 if (FLAG_debug_code) {
1224 __ AbortIfNotRootValue(heap_number_map,
1225 Heap::kHeapNumberMapRootIndex,
1226 "HeapNumberMap register clobbered.");
1227 }
1228 __ movq(FieldOperand(rax, HeapObject::kMapOffset),
1229 heap_number_map);
1230 __ cvtqsi2sd(xmm0, rbx);
1231 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm0);
1232 __ Ret();
1233
1234 __ bind(&allocation_failed);
1235 // We need tagged values in rdx and rax for the following code,
1236 // not int32 in rax and rcx.
1237 __ Integer32ToSmi(rax, rcx);
Steve Block053d10c2011-06-13 19:13:29 +01001238 __ Integer32ToSmi(rdx, rbx);
Steve Block1e0659c2011-05-24 12:43:12 +01001239 __ jmp(allocation_failure);
1240 }
1241 break;
1242 }
1243 default: UNREACHABLE(); break;
1244 }
1245 // No fall-through from this generated code.
1246 if (FLAG_debug_code) {
1247 __ Abort("Unexpected fall-through in "
Ben Murdoch257744e2011-11-30 15:57:28 +00001248 "BinaryStub::GenerateFloatingPointCode.");
Steve Block1e0659c2011-05-24 12:43:12 +01001249 }
1250}
1251
1252
Ben Murdoch257744e2011-11-30 15:57:28 +00001253void BinaryOpStub::GenerateStringAddCode(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001254 ASSERT(op_ == Token::ADD);
Ben Murdoch257744e2011-11-30 15:57:28 +00001255 Label left_not_string, call_runtime;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001256
Steve Block1e0659c2011-05-24 12:43:12 +01001257 // Registers containing left and right operands respectively.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001258 Register left = rdx;
1259 Register right = rax;
Steve Block1e0659c2011-05-24 12:43:12 +01001260
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001261 // Test if left operand is a string.
Ben Murdoch257744e2011-11-30 15:57:28 +00001262 __ JumpIfSmi(left, &left_not_string, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001263 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001264 __ j(above_equal, &left_not_string, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001265 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
1266 GenerateRegisterArgsPush(masm);
1267 __ TailCallStub(&string_add_left_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01001268
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001269 // Left operand is not a string, test right.
1270 __ bind(&left_not_string);
Ben Murdoch257744e2011-11-30 15:57:28 +00001271 __ JumpIfSmi(right, &call_runtime, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001272 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001273 __ j(above_equal, &call_runtime, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01001274
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001275 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
1276 GenerateRegisterArgsPush(masm);
1277 __ TailCallStub(&string_add_right_stub);
Steve Block1e0659c2011-05-24 12:43:12 +01001278
Steve Block1e0659c2011-05-24 12:43:12 +01001279 // Neither argument is a string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001280 __ bind(&call_runtime);
Steve Block1e0659c2011-05-24 12:43:12 +01001281}
1282
1283
Ben Murdoch257744e2011-11-30 15:57:28 +00001284void BinaryOpStub::GenerateCallRuntimeCode(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001285 GenerateRegisterArgsPush(masm);
1286 switch (op_) {
1287 case Token::ADD:
1288 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1289 break;
1290 case Token::SUB:
1291 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1292 break;
1293 case Token::MUL:
1294 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1295 break;
1296 case Token::DIV:
1297 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1298 break;
1299 case Token::MOD:
1300 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1301 break;
1302 case Token::BIT_OR:
1303 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1304 break;
1305 case Token::BIT_AND:
1306 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1307 break;
1308 case Token::BIT_XOR:
1309 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1310 break;
1311 case Token::SAR:
1312 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1313 break;
1314 case Token::SHL:
1315 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1316 break;
1317 case Token::SHR:
1318 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1319 break;
1320 default:
1321 UNREACHABLE();
1322 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001323}
1324
1325
Ben Murdoch257744e2011-11-30 15:57:28 +00001326void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001327 Label call_runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00001328 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
1329 result_type_ == BinaryOpIC::SMI) {
Ben Murdoch8b112d22011-06-08 16:22:53 +01001330 // Only allow smi results.
1331 GenerateSmiCode(masm, NULL, NO_HEAPNUMBER_RESULTS);
1332 } else {
1333 // Allow heap number result and don't make a transition if a heap number
1334 // cannot be allocated.
1335 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1336 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001337
Ben Murdoch8b112d22011-06-08 16:22:53 +01001338 // Code falls through if the result is not returned as either a smi or heap
1339 // number.
Steve Block1e0659c2011-05-24 12:43:12 +01001340 GenerateTypeTransition(masm);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001341
1342 if (call_runtime.is_linked()) {
1343 __ bind(&call_runtime);
1344 GenerateCallRuntimeCode(masm);
1345 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001346}
1347
1348
Ben Murdoch257744e2011-11-30 15:57:28 +00001349void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
1350 ASSERT(operands_type_ == BinaryOpIC::STRING);
Steve Block1e0659c2011-05-24 12:43:12 +01001351 ASSERT(op_ == Token::ADD);
1352 GenerateStringAddCode(masm);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001353 // Try to add arguments as strings, otherwise, transition to the generic
Ben Murdoch257744e2011-11-30 15:57:28 +00001354 // BinaryOpIC type.
Steve Block1e0659c2011-05-24 12:43:12 +01001355 GenerateTypeTransition(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001356}
1357
1358
Ben Murdoch257744e2011-11-30 15:57:28 +00001359void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
1360 Label call_runtime;
1361 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
1362 ASSERT(op_ == Token::ADD);
1363 // If both arguments are strings, call the string add stub.
1364 // Otherwise, do a transition.
1365
1366 // Registers containing left and right operands respectively.
1367 Register left = rdx;
1368 Register right = rax;
1369
1370 // Test if left operand is a string.
1371 __ JumpIfSmi(left, &call_runtime);
1372 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, rcx);
1373 __ j(above_equal, &call_runtime);
1374
1375 // Test if right operand is a string.
1376 __ JumpIfSmi(right, &call_runtime);
1377 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, rcx);
1378 __ j(above_equal, &call_runtime);
1379
1380 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1381 GenerateRegisterArgsPush(masm);
1382 __ TailCallStub(&string_add_stub);
1383
1384 __ bind(&call_runtime);
1385 GenerateTypeTransition(masm);
1386}
1387
1388
1389void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01001390 Label call_runtime;
1391
1392 if (op_ == Token::ADD) {
1393 // Handle string addition here, because it is the only operation
1394 // that does not do a ToNumber conversion on the operands.
1395 GenerateStringAddCode(masm);
1396 }
1397
1398 // Convert oddball arguments to numbers.
Ben Murdoch257744e2011-11-30 15:57:28 +00001399 Label check, done;
Steve Block44f0eee2011-05-26 01:26:41 +01001400 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001401 __ j(not_equal, &check, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001402 if (Token::IsBitOp(op_)) {
1403 __ xor_(rdx, rdx);
1404 } else {
1405 __ LoadRoot(rdx, Heap::kNanValueRootIndex);
1406 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001407 __ jmp(&done, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001408 __ bind(&check);
1409 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00001410 __ j(not_equal, &done, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001411 if (Token::IsBitOp(op_)) {
1412 __ xor_(rax, rax);
1413 } else {
1414 __ LoadRoot(rax, Heap::kNanValueRootIndex);
1415 }
1416 __ bind(&done);
1417
1418 GenerateHeapNumberStub(masm);
1419}
1420
1421
Ben Murdoch257744e2011-11-30 15:57:28 +00001422void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001423 Label gc_required, not_number;
1424 GenerateFloatingPointCode(masm, &gc_required, &not_number);
1425
1426 __ bind(&not_number);
1427 GenerateTypeTransition(masm);
1428
1429 __ bind(&gc_required);
1430 GenerateCallRuntimeCode(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001431}
1432
1433
Ben Murdoch257744e2011-11-30 15:57:28 +00001434void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01001435 Label call_runtime, call_string_add_or_runtime;
1436
1437 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1438
1439 GenerateFloatingPointCode(masm, &call_runtime, &call_string_add_or_runtime);
1440
1441 __ bind(&call_string_add_or_runtime);
1442 if (op_ == Token::ADD) {
1443 GenerateStringAddCode(masm);
1444 }
1445
1446 __ bind(&call_runtime);
1447 GenerateCallRuntimeCode(masm);
Ben Murdoch086aeea2011-05-13 15:57:08 +01001448}
1449
1450
Ben Murdoch257744e2011-11-30 15:57:28 +00001451void BinaryOpStub::GenerateHeapResultAllocation(MacroAssembler* masm,
1452 Label* alloc_failure) {
Steve Block1e0659c2011-05-24 12:43:12 +01001453 Label skip_allocation;
1454 OverwriteMode mode = mode_;
1455 switch (mode) {
1456 case OVERWRITE_LEFT: {
1457 // If the argument in rdx is already an object, we skip the
1458 // allocation of a heap number.
1459 __ JumpIfNotSmi(rdx, &skip_allocation);
1460 // Allocate a heap number for the result. Keep eax and edx intact
1461 // for the possible runtime call.
1462 __ AllocateHeapNumber(rbx, rcx, alloc_failure);
1463 // Now rdx can be overwritten losing one of the arguments as we are
1464 // now done and will not need it any more.
1465 __ movq(rdx, rbx);
1466 __ bind(&skip_allocation);
1467 // Use object in rdx as a result holder
1468 __ movq(rax, rdx);
1469 break;
1470 }
1471 case OVERWRITE_RIGHT:
1472 // If the argument in rax is already an object, we skip the
1473 // allocation of a heap number.
1474 __ JumpIfNotSmi(rax, &skip_allocation);
1475 // Fall through!
1476 case NO_OVERWRITE:
1477 // Allocate a heap number for the result. Keep rax and rdx intact
1478 // for the possible runtime call.
1479 __ AllocateHeapNumber(rbx, rcx, alloc_failure);
1480 // Now rax can be overwritten losing one of the arguments as we are
1481 // now done and will not need it any more.
1482 __ movq(rax, rbx);
1483 __ bind(&skip_allocation);
1484 break;
1485 default: UNREACHABLE();
1486 }
Ben Murdoch086aeea2011-05-13 15:57:08 +01001487}
1488
1489
Ben Murdoch257744e2011-11-30 15:57:28 +00001490void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01001491 __ pop(rcx);
1492 __ push(rdx);
1493 __ push(rax);
1494 __ push(rcx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001495}
1496
1497
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001498void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001499 // TAGGED case:
1500 // Input:
1501 // rsp[8]: argument (should be number).
1502 // rsp[0]: return address.
1503 // Output:
1504 // rax: tagged double result.
1505 // UNTAGGED case:
1506 // Input::
1507 // rsp[0]: return address.
1508 // xmm1: untagged double input argument
1509 // Output:
1510 // xmm1: untagged double result.
1511
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001512 Label runtime_call;
1513 Label runtime_call_clear_stack;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001514 Label skip_cache;
1515 const bool tagged = (argument_type_ == TAGGED);
1516 if (tagged) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001517 Label input_not_smi, loaded;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001518 // Test that rax is a number.
1519 __ movq(rax, Operand(rsp, kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001520 __ JumpIfNotSmi(rax, &input_not_smi, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001521 // Input is a smi. Untag and load it onto the FPU stack.
1522 // Then load the bits of the double into rbx.
1523 __ SmiToInteger32(rax, rax);
1524 __ subq(rsp, Immediate(kDoubleSize));
1525 __ cvtlsi2sd(xmm1, rax);
1526 __ movsd(Operand(rsp, 0), xmm1);
1527 __ movq(rbx, xmm1);
1528 __ movq(rdx, xmm1);
1529 __ fld_d(Operand(rsp, 0));
1530 __ addq(rsp, Immediate(kDoubleSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00001531 __ jmp(&loaded, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001532
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001533 __ bind(&input_not_smi);
1534 // Check if input is a HeapNumber.
1535 __ LoadRoot(rbx, Heap::kHeapNumberMapRootIndex);
1536 __ cmpq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
1537 __ j(not_equal, &runtime_call);
1538 // Input is a HeapNumber. Push it on the FPU stack and load its
1539 // bits into rbx.
1540 __ fld_d(FieldOperand(rax, HeapNumber::kValueOffset));
1541 __ movq(rbx, FieldOperand(rax, HeapNumber::kValueOffset));
1542 __ movq(rdx, rbx);
1543
1544 __ bind(&loaded);
1545 } else { // UNTAGGED.
1546 __ movq(rbx, xmm1);
1547 __ movq(rdx, xmm1);
1548 }
1549
1550 // ST[0] == double value, if TAGGED.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001551 // rbx = bits of double value.
1552 // rdx = also bits of double value.
1553 // Compute hash (h is 32 bits, bits are 64 and the shifts are arithmetic):
1554 // h = h0 = bits ^ (bits >> 32);
1555 // h ^= h >> 16;
1556 // h ^= h >> 8;
1557 // h = h & (cacheSize - 1);
1558 // or h = (h0 ^ (h0 >> 8) ^ (h0 >> 16) ^ (h0 >> 24)) & (cacheSize - 1)
1559 __ sar(rdx, Immediate(32));
1560 __ xorl(rdx, rbx);
1561 __ movl(rcx, rdx);
1562 __ movl(rax, rdx);
1563 __ movl(rdi, rdx);
1564 __ sarl(rdx, Immediate(8));
1565 __ sarl(rcx, Immediate(16));
1566 __ sarl(rax, Immediate(24));
1567 __ xorl(rcx, rdx);
1568 __ xorl(rax, rdi);
1569 __ xorl(rcx, rax);
Steve Block44f0eee2011-05-26 01:26:41 +01001570 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
1571 __ andl(rcx, Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001572
1573 // ST[0] == double value.
1574 // rbx = bits of double value.
1575 // rcx = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +01001576 ExternalReference cache_array =
1577 ExternalReference::transcendental_cache_array_address(masm->isolate());
1578 __ movq(rax, cache_array);
1579 int cache_array_index =
1580 type_ * sizeof(Isolate::Current()->transcendental_cache()->caches_[0]);
1581 __ movq(rax, Operand(rax, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001582 // rax points to the cache for the type type_.
1583 // If NULL, the cache hasn't been initialized yet, so go through runtime.
1584 __ testq(rax, rax);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001585 __ j(zero, &runtime_call_clear_stack); // Only clears stack if TAGGED.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001586#ifdef DEBUG
1587 // Check that the layout of cache elements match expectations.
1588 { // NOLINT - doesn't like a single brace on a line.
Steve Block44f0eee2011-05-26 01:26:41 +01001589 TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001590 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
1591 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
1592 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
1593 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
1594 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
1595 // Two uint_32's and a pointer per element.
1596 CHECK_EQ(16, static_cast<int>(elem2_start - elem_start));
1597 CHECK_EQ(0, static_cast<int>(elem_in0 - elem_start));
1598 CHECK_EQ(kIntSize, static_cast<int>(elem_in1 - elem_start));
1599 CHECK_EQ(2 * kIntSize, static_cast<int>(elem_out - elem_start));
1600 }
1601#endif
1602 // Find the address of the rcx'th entry in the cache, i.e., &rax[rcx*16].
1603 __ addl(rcx, rcx);
1604 __ lea(rcx, Operand(rax, rcx, times_8, 0));
1605 // Check if cache matches: Double value is stored in uint32_t[2] array.
Ben Murdoch257744e2011-11-30 15:57:28 +00001606 Label cache_miss;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001607 __ cmpq(rbx, Operand(rcx, 0));
Ben Murdoch257744e2011-11-30 15:57:28 +00001608 __ j(not_equal, &cache_miss, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001609 // Cache hit!
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001610 Counters* counters = masm->isolate()->counters();
1611 __ IncrementCounter(counters->transcendental_cache_hit(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001612 __ movq(rax, Operand(rcx, 2 * kIntSize));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001613 if (tagged) {
1614 __ fstp(0); // Clear FPU stack.
1615 __ ret(kPointerSize);
1616 } else { // UNTAGGED.
1617 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1618 __ Ret();
1619 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001620
1621 __ bind(&cache_miss);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001622 __ IncrementCounter(counters->transcendental_cache_miss(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001623 // Update cache with new value.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001624 if (tagged) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001625 __ AllocateHeapNumber(rax, rdi, &runtime_call_clear_stack);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001626 } else { // UNTAGGED.
1627 __ AllocateHeapNumber(rax, rdi, &skip_cache);
1628 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm1);
1629 __ fld_d(FieldOperand(rax, HeapNumber::kValueOffset));
1630 }
1631 GenerateOperation(masm);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001632 __ movq(Operand(rcx, 0), rbx);
1633 __ movq(Operand(rcx, 2 * kIntSize), rax);
1634 __ fstp_d(FieldOperand(rax, HeapNumber::kValueOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001635 if (tagged) {
1636 __ ret(kPointerSize);
1637 } else { // UNTAGGED.
1638 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1639 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001640
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001641 // Skip cache and return answer directly, only in untagged case.
1642 __ bind(&skip_cache);
1643 __ subq(rsp, Immediate(kDoubleSize));
1644 __ movsd(Operand(rsp, 0), xmm1);
1645 __ fld_d(Operand(rsp, 0));
1646 GenerateOperation(masm);
1647 __ fstp_d(Operand(rsp, 0));
1648 __ movsd(xmm1, Operand(rsp, 0));
1649 __ addq(rsp, Immediate(kDoubleSize));
1650 // We return the value in xmm1 without adding it to the cache, but
1651 // we cause a scavenging GC so that future allocations will succeed.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001652 {
1653 FrameScope scope(masm, StackFrame::INTERNAL);
1654 // Allocate an unused object bigger than a HeapNumber.
1655 __ Push(Smi::FromInt(2 * kDoubleSize));
1656 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
1657 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001658 __ Ret();
1659 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001660
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001661 // Call runtime, doing whatever allocation and cleanup is necessary.
1662 if (tagged) {
1663 __ bind(&runtime_call_clear_stack);
1664 __ fstp(0);
1665 __ bind(&runtime_call);
Steve Block44f0eee2011-05-26 01:26:41 +01001666 __ TailCallExternalReference(
1667 ExternalReference(RuntimeFunction(), masm->isolate()), 1, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001668 } else { // UNTAGGED.
1669 __ bind(&runtime_call_clear_stack);
1670 __ bind(&runtime_call);
1671 __ AllocateHeapNumber(rax, rdi, &skip_cache);
1672 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), xmm1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001673 {
1674 FrameScope scope(masm, StackFrame::INTERNAL);
1675 __ push(rax);
1676 __ CallRuntime(RuntimeFunction(), 1);
1677 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001678 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1679 __ Ret();
1680 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001681}
1682
1683
1684Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
1685 switch (type_) {
1686 // Add more cases when necessary.
1687 case TranscendentalCache::SIN: return Runtime::kMath_sin;
1688 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001689 case TranscendentalCache::TAN: return Runtime::kMath_tan;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001690 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001691 default:
1692 UNIMPLEMENTED();
1693 return Runtime::kAbort;
1694 }
1695}
1696
1697
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001698void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001699 // Registers:
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001700 // rax: Newly allocated HeapNumber, which must be preserved.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001701 // rbx: Bits of input double. Must be preserved.
1702 // rcx: Pointer to cache entry. Must be preserved.
1703 // st(0): Input double
1704 Label done;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001705 if (type_ == TranscendentalCache::SIN ||
1706 type_ == TranscendentalCache::COS ||
1707 type_ == TranscendentalCache::TAN) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001708 // Both fsin and fcos require arguments in the range +/-2^63 and
1709 // return NaN for infinities and NaN. They can share all code except
1710 // the actual fsin/fcos operation.
1711 Label in_range;
1712 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
1713 // work. We must reduce it to the appropriate range.
1714 __ movq(rdi, rbx);
1715 // Move exponent and sign bits to low bits.
1716 __ shr(rdi, Immediate(HeapNumber::kMantissaBits));
1717 // Remove sign bit.
1718 __ andl(rdi, Immediate((1 << HeapNumber::kExponentBits) - 1));
1719 int supported_exponent_limit = (63 + HeapNumber::kExponentBias);
1720 __ cmpl(rdi, Immediate(supported_exponent_limit));
1721 __ j(below, &in_range);
1722 // Check for infinity and NaN. Both return NaN for sin.
1723 __ cmpl(rdi, Immediate(0x7ff));
Ben Murdoch257744e2011-11-30 15:57:28 +00001724 Label non_nan_result;
1725 __ j(not_equal, &non_nan_result, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001726 // Input is +/-Infinity or NaN. Result is NaN.
1727 __ fstp(0);
1728 __ LoadRoot(kScratchRegister, Heap::kNanValueRootIndex);
1729 __ fld_d(FieldOperand(kScratchRegister, HeapNumber::kValueOffset));
1730 __ jmp(&done);
1731
1732 __ bind(&non_nan_result);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001733
Ben Murdochb0fe1622011-05-05 13:52:32 +01001734 // Use fpmod to restrict argument to the range +/-2*PI.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001735 __ movq(rdi, rax); // Save rax before using fnstsw_ax.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001736 __ fldpi();
1737 __ fadd(0);
1738 __ fld(1);
1739 // FPU Stack: input, 2*pi, input.
1740 {
1741 Label no_exceptions;
1742 __ fwait();
1743 __ fnstsw_ax();
1744 // Clear if Illegal Operand or Zero Division exceptions are set.
1745 __ testl(rax, Immediate(5)); // #IO and #ZD flags of FPU status word.
1746 __ j(zero, &no_exceptions);
1747 __ fnclex();
1748 __ bind(&no_exceptions);
1749 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001750
Ben Murdochb0fe1622011-05-05 13:52:32 +01001751 // Compute st(0) % st(1)
1752 {
Ben Murdoch257744e2011-11-30 15:57:28 +00001753 Label partial_remainder_loop;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001754 __ bind(&partial_remainder_loop);
1755 __ fprem1();
1756 __ fwait();
1757 __ fnstsw_ax();
1758 __ testl(rax, Immediate(0x400)); // Check C2 bit of FPU status word.
1759 // If C2 is set, computation only has partial result. Loop to
1760 // continue computation.
1761 __ j(not_zero, &partial_remainder_loop);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001762 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001763 // FPU Stack: input, 2*pi, input % 2*pi
1764 __ fstp(2);
1765 // FPU Stack: input % 2*pi, 2*pi,
1766 __ fstp(0);
1767 // FPU Stack: input % 2*pi
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001768 __ movq(rax, rdi); // Restore rax, pointer to the new HeapNumber.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001769 __ bind(&in_range);
1770 switch (type_) {
1771 case TranscendentalCache::SIN:
1772 __ fsin();
1773 break;
1774 case TranscendentalCache::COS:
1775 __ fcos();
1776 break;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001777 case TranscendentalCache::TAN:
1778 // FPTAN calculates tangent onto st(0) and pushes 1.0 onto the
1779 // FP register stack.
1780 __ fptan();
1781 __ fstp(0); // Pop FP register stack.
1782 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001783 default:
1784 UNREACHABLE();
1785 }
1786 __ bind(&done);
1787 } else {
1788 ASSERT(type_ == TranscendentalCache::LOG);
1789 __ fldln2();
1790 __ fxch();
1791 __ fyl2x();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001792 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001793}
1794
1795
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001796// Input: rdx, rax are the left and right objects of a bit op.
1797// Output: rax, rcx are left and right integers for a bit op.
1798void FloatingPointHelper::LoadNumbersAsIntegers(MacroAssembler* masm) {
1799 // Check float operands.
1800 Label done;
1801 Label rax_is_smi;
1802 Label rax_is_object;
1803 Label rdx_is_object;
1804
1805 __ JumpIfNotSmi(rdx, &rdx_is_object);
1806 __ SmiToInteger32(rdx, rdx);
1807 __ JumpIfSmi(rax, &rax_is_smi);
1808
1809 __ bind(&rax_is_object);
1810 IntegerConvert(masm, rcx, rax); // Uses rdi, rcx and rbx.
1811 __ jmp(&done);
1812
1813 __ bind(&rdx_is_object);
1814 IntegerConvert(masm, rdx, rdx); // Uses rdi, rcx and rbx.
1815 __ JumpIfNotSmi(rax, &rax_is_object);
1816 __ bind(&rax_is_smi);
1817 __ SmiToInteger32(rcx, rax);
1818
1819 __ bind(&done);
1820 __ movl(rax, rdx);
1821}
1822
1823
1824// Input: rdx, rax are the left and right objects of a bit op.
1825// Output: rax, rcx are left and right integers for a bit op.
Steve Block1e0659c2011-05-24 12:43:12 +01001826// Jump to conversion_failure: rdx and rax are unchanged.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001827void FloatingPointHelper::LoadAsIntegers(MacroAssembler* masm,
1828 Label* conversion_failure,
1829 Register heap_number_map) {
1830 // Check float operands.
1831 Label arg1_is_object, check_undefined_arg1;
1832 Label arg2_is_object, check_undefined_arg2;
1833 Label load_arg2, done;
1834
1835 __ JumpIfNotSmi(rdx, &arg1_is_object);
Steve Block1e0659c2011-05-24 12:43:12 +01001836 __ SmiToInteger32(r8, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001837 __ jmp(&load_arg2);
1838
1839 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
1840 __ bind(&check_undefined_arg1);
1841 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
1842 __ j(not_equal, conversion_failure);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001843 __ Set(r8, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001844 __ jmp(&load_arg2);
1845
1846 __ bind(&arg1_is_object);
1847 __ cmpq(FieldOperand(rdx, HeapObject::kMapOffset), heap_number_map);
1848 __ j(not_equal, &check_undefined_arg1);
Steve Block1e0659c2011-05-24 12:43:12 +01001849 // Get the untagged integer version of the rdx heap number in rcx.
1850 IntegerConvert(masm, r8, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001851
Steve Block1e0659c2011-05-24 12:43:12 +01001852 // Here r8 has the untagged integer, rax has a Smi or a heap number.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001853 __ bind(&load_arg2);
1854 // Test if arg2 is a Smi.
1855 __ JumpIfNotSmi(rax, &arg2_is_object);
Steve Block1e0659c2011-05-24 12:43:12 +01001856 __ SmiToInteger32(rcx, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001857 __ jmp(&done);
1858
1859 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
1860 __ bind(&check_undefined_arg2);
1861 __ CompareRoot(rax, Heap::kUndefinedValueRootIndex);
1862 __ j(not_equal, conversion_failure);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001863 __ Set(rcx, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001864 __ jmp(&done);
1865
1866 __ bind(&arg2_is_object);
1867 __ cmpq(FieldOperand(rax, HeapObject::kMapOffset), heap_number_map);
1868 __ j(not_equal, &check_undefined_arg2);
1869 // Get the untagged integer version of the rax heap number in rcx.
1870 IntegerConvert(masm, rcx, rax);
1871 __ bind(&done);
Steve Block1e0659c2011-05-24 12:43:12 +01001872 __ movl(rax, r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01001873}
1874
1875
1876void FloatingPointHelper::LoadSSE2SmiOperands(MacroAssembler* masm) {
1877 __ SmiToInteger32(kScratchRegister, rdx);
1878 __ cvtlsi2sd(xmm0, kScratchRegister);
1879 __ SmiToInteger32(kScratchRegister, rax);
1880 __ cvtlsi2sd(xmm1, kScratchRegister);
1881}
1882
1883
1884void FloatingPointHelper::LoadSSE2NumberOperands(MacroAssembler* masm) {
1885 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, done;
1886 // Load operand in rdx into xmm0.
1887 __ JumpIfSmi(rdx, &load_smi_rdx);
1888 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1889 // Load operand in rax into xmm1.
1890 __ JumpIfSmi(rax, &load_smi_rax);
1891 __ bind(&load_nonsmi_rax);
1892 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1893 __ jmp(&done);
1894
1895 __ bind(&load_smi_rdx);
1896 __ SmiToInteger32(kScratchRegister, rdx);
1897 __ cvtlsi2sd(xmm0, kScratchRegister);
1898 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
1899
1900 __ bind(&load_smi_rax);
1901 __ SmiToInteger32(kScratchRegister, rax);
1902 __ cvtlsi2sd(xmm1, kScratchRegister);
1903
1904 __ bind(&done);
1905}
1906
1907
1908void FloatingPointHelper::LoadSSE2UnknownOperands(MacroAssembler* masm,
1909 Label* not_numbers) {
1910 Label load_smi_rdx, load_nonsmi_rax, load_smi_rax, load_float_rax, done;
1911 // Load operand in rdx into xmm0, or branch to not_numbers.
1912 __ LoadRoot(rcx, Heap::kHeapNumberMapRootIndex);
1913 __ JumpIfSmi(rdx, &load_smi_rdx);
1914 __ cmpq(FieldOperand(rdx, HeapObject::kMapOffset), rcx);
1915 __ j(not_equal, not_numbers); // Argument in rdx is not a number.
1916 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
1917 // Load operand in rax into xmm1, or branch to not_numbers.
1918 __ JumpIfSmi(rax, &load_smi_rax);
1919
1920 __ bind(&load_nonsmi_rax);
1921 __ cmpq(FieldOperand(rax, HeapObject::kMapOffset), rcx);
1922 __ j(not_equal, not_numbers);
1923 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
1924 __ jmp(&done);
1925
1926 __ bind(&load_smi_rdx);
1927 __ SmiToInteger32(kScratchRegister, rdx);
1928 __ cvtlsi2sd(xmm0, kScratchRegister);
1929 __ JumpIfNotSmi(rax, &load_nonsmi_rax);
1930
1931 __ bind(&load_smi_rax);
1932 __ SmiToInteger32(kScratchRegister, rax);
1933 __ cvtlsi2sd(xmm1, kScratchRegister);
1934 __ bind(&done);
1935}
1936
1937
Ben Murdoch8b112d22011-06-08 16:22:53 +01001938void FloatingPointHelper::NumbersToSmis(MacroAssembler* masm,
1939 Register first,
1940 Register second,
1941 Register scratch1,
1942 Register scratch2,
1943 Register scratch3,
1944 Label* on_success,
1945 Label* on_not_smis) {
1946 Register heap_number_map = scratch3;
1947 Register smi_result = scratch1;
1948 Label done;
1949
1950 __ LoadRoot(heap_number_map, Heap::kHeapNumberMapRootIndex);
1951
Ben Murdoch257744e2011-11-30 15:57:28 +00001952 Label first_smi;
1953 __ JumpIfSmi(first, &first_smi, Label::kNear);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001954 __ cmpq(FieldOperand(first, HeapObject::kMapOffset), heap_number_map);
1955 __ j(not_equal, on_not_smis);
1956 // Convert HeapNumber to smi if possible.
1957 __ movsd(xmm0, FieldOperand(first, HeapNumber::kValueOffset));
1958 __ movq(scratch2, xmm0);
1959 __ cvttsd2siq(smi_result, xmm0);
1960 // Check if conversion was successful by converting back and
1961 // comparing to the original double's bits.
1962 __ cvtlsi2sd(xmm1, smi_result);
1963 __ movq(kScratchRegister, xmm1);
1964 __ cmpq(scratch2, kScratchRegister);
1965 __ j(not_equal, on_not_smis);
1966 __ Integer32ToSmi(first, smi_result);
1967
Ben Murdoch8b112d22011-06-08 16:22:53 +01001968 __ JumpIfSmi(second, (on_success != NULL) ? on_success : &done);
1969 __ bind(&first_smi);
1970 if (FLAG_debug_code) {
1971 // Second should be non-smi if we get here.
1972 __ AbortIfSmi(second);
1973 }
1974 __ cmpq(FieldOperand(second, HeapObject::kMapOffset), heap_number_map);
1975 __ j(not_equal, on_not_smis);
1976 // Convert second to smi, if possible.
1977 __ movsd(xmm0, FieldOperand(second, HeapNumber::kValueOffset));
1978 __ movq(scratch2, xmm0);
1979 __ cvttsd2siq(smi_result, xmm0);
1980 __ cvtlsi2sd(xmm1, smi_result);
1981 __ movq(kScratchRegister, xmm1);
1982 __ cmpq(scratch2, kScratchRegister);
1983 __ j(not_equal, on_not_smis);
1984 __ Integer32ToSmi(second, smi_result);
1985 if (on_success != NULL) {
1986 __ jmp(on_success);
1987 } else {
1988 __ bind(&done);
1989 }
1990}
1991
1992
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001993void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00001994 // Choose register conforming to calling convention (when bailing out).
1995#ifdef _WIN64
1996 const Register exponent = rdx;
1997#else
1998 const Register exponent = rdi;
1999#endif
2000 const Register base = rax;
2001 const Register scratch = rcx;
2002 const XMMRegister double_result = xmm3;
2003 const XMMRegister double_base = xmm2;
2004 const XMMRegister double_exponent = xmm1;
2005 const XMMRegister double_scratch = xmm4;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002006
Ben Murdochc7cc0282012-03-05 14:35:55 +00002007 Label call_runtime, done, exponent_not_smi, int_exponent;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002008
Ben Murdochc7cc0282012-03-05 14:35:55 +00002009 // Save 1 in double_result - we need this several times later on.
2010 __ movq(scratch, Immediate(1));
2011 __ cvtlsi2sd(double_result, scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002012
Ben Murdochc7cc0282012-03-05 14:35:55 +00002013 if (exponent_type_ == ON_STACK) {
2014 Label base_is_smi, unpack_exponent;
2015 // The exponent and base are supplied as arguments on the stack.
2016 // This can only happen if the stub is called from non-optimized code.
2017 // Load input parameters from stack.
2018 __ movq(base, Operand(rsp, 2 * kPointerSize));
2019 __ movq(exponent, Operand(rsp, 1 * kPointerSize));
2020 __ JumpIfSmi(base, &base_is_smi, Label::kNear);
2021 __ CompareRoot(FieldOperand(base, HeapObject::kMapOffset),
2022 Heap::kHeapNumberMapRootIndex);
2023 __ j(not_equal, &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002024
Ben Murdochc7cc0282012-03-05 14:35:55 +00002025 __ movsd(double_base, FieldOperand(base, HeapNumber::kValueOffset));
2026 __ jmp(&unpack_exponent, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002027
Ben Murdochc7cc0282012-03-05 14:35:55 +00002028 __ bind(&base_is_smi);
2029 __ SmiToInteger32(base, base);
2030 __ cvtlsi2sd(double_base, base);
2031 __ bind(&unpack_exponent);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002032
Ben Murdochc7cc0282012-03-05 14:35:55 +00002033 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
2034 __ SmiToInteger32(exponent, exponent);
2035 __ jmp(&int_exponent);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002036
Ben Murdochc7cc0282012-03-05 14:35:55 +00002037 __ bind(&exponent_not_smi);
2038 __ CompareRoot(FieldOperand(exponent, HeapObject::kMapOffset),
2039 Heap::kHeapNumberMapRootIndex);
2040 __ j(not_equal, &call_runtime);
2041 __ movsd(double_exponent, FieldOperand(exponent, HeapNumber::kValueOffset));
2042 } else if (exponent_type_ == TAGGED) {
2043 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
2044 __ SmiToInteger32(exponent, exponent);
2045 __ jmp(&int_exponent);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002046
Ben Murdochc7cc0282012-03-05 14:35:55 +00002047 __ bind(&exponent_not_smi);
2048 __ movsd(double_exponent, FieldOperand(exponent, HeapNumber::kValueOffset));
2049 }
2050
2051 if (exponent_type_ != INTEGER) {
2052 Label fast_power;
2053 // Detect integer exponents stored as double.
2054 __ cvttsd2si(exponent, double_exponent);
2055 // Skip to runtime if possibly NaN (indicated by the indefinite integer).
2056 __ cmpl(exponent, Immediate(0x80000000u));
2057 __ j(equal, &call_runtime);
2058 __ cvtlsi2sd(double_scratch, exponent);
2059 // Already ruled out NaNs for exponent.
2060 __ ucomisd(double_exponent, double_scratch);
2061 __ j(equal, &int_exponent);
2062
2063 if (exponent_type_ == ON_STACK) {
2064 // Detect square root case. Crankshaft detects constant +/-0.5 at
2065 // compile time and uses DoMathPowHalf instead. We then skip this check
2066 // for non-constant cases of +/-0.5 as these hardly occur.
2067 Label continue_sqrt, continue_rsqrt, not_plus_half;
2068 // Test for 0.5.
2069 // Load double_scratch with 0.5.
2070 __ movq(scratch, V8_UINT64_C(0x3FE0000000000000), RelocInfo::NONE);
2071 __ movq(double_scratch, scratch);
2072 // Already ruled out NaNs for exponent.
2073 __ ucomisd(double_scratch, double_exponent);
2074 __ j(not_equal, &not_plus_half, Label::kNear);
2075
2076 // Calculates square root of base. Check for the special case of
2077 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
2078 // According to IEEE-754, double-precision -Infinity has the highest
2079 // 12 bits set and the lowest 52 bits cleared.
2080 __ movq(scratch, V8_UINT64_C(0xFFF0000000000000), RelocInfo::NONE);
2081 __ movq(double_scratch, scratch);
2082 __ ucomisd(double_scratch, double_base);
2083 // Comparing -Infinity with NaN results in "unordered", which sets the
2084 // zero flag as if both were equal. However, it also sets the carry flag.
2085 __ j(not_equal, &continue_sqrt, Label::kNear);
2086 __ j(carry, &continue_sqrt, Label::kNear);
2087
2088 // Set result to Infinity in the special case.
2089 __ xorps(double_result, double_result);
2090 __ subsd(double_result, double_scratch);
2091 __ jmp(&done);
2092
2093 __ bind(&continue_sqrt);
2094 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
2095 __ xorps(double_scratch, double_scratch);
2096 __ addsd(double_scratch, double_base); // Convert -0 to 0.
2097 __ sqrtsd(double_result, double_scratch);
2098 __ jmp(&done);
2099
2100 // Test for -0.5.
2101 __ bind(&not_plus_half);
2102 // Load double_scratch with -0.5 by substracting 1.
2103 __ subsd(double_scratch, double_result);
2104 // Already ruled out NaNs for exponent.
2105 __ ucomisd(double_scratch, double_exponent);
2106 __ j(not_equal, &fast_power, Label::kNear);
2107
2108 // Calculates reciprocal of square root of base. Check for the special
2109 // case of Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
2110 // According to IEEE-754, double-precision -Infinity has the highest
2111 // 12 bits set and the lowest 52 bits cleared.
2112 __ movq(scratch, V8_UINT64_C(0xFFF0000000000000), RelocInfo::NONE);
2113 __ movq(double_scratch, scratch);
2114 __ ucomisd(double_scratch, double_base);
2115 // Comparing -Infinity with NaN results in "unordered", which sets the
2116 // zero flag as if both were equal. However, it also sets the carry flag.
2117 __ j(not_equal, &continue_rsqrt, Label::kNear);
2118 __ j(carry, &continue_rsqrt, Label::kNear);
2119
2120 // Set result to 0 in the special case.
2121 __ xorps(double_result, double_result);
2122 __ jmp(&done);
2123
2124 __ bind(&continue_rsqrt);
2125 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
2126 __ xorps(double_exponent, double_exponent);
2127 __ addsd(double_exponent, double_base); // Convert -0 to +0.
2128 __ sqrtsd(double_exponent, double_exponent);
2129 __ divsd(double_result, double_exponent);
2130 __ jmp(&done);
2131 }
2132
2133 // Using FPU instructions to calculate power.
2134 Label fast_power_failed;
2135 __ bind(&fast_power);
2136 __ fnclex(); // Clear flags to catch exceptions later.
2137 // Transfer (B)ase and (E)xponent onto the FPU register stack.
2138 __ subq(rsp, Immediate(kDoubleSize));
2139 __ movsd(Operand(rsp, 0), double_exponent);
2140 __ fld_d(Operand(rsp, 0)); // E
2141 __ movsd(Operand(rsp, 0), double_base);
2142 __ fld_d(Operand(rsp, 0)); // B, E
2143
2144 // Exponent is in st(1) and base is in st(0)
2145 // B ^ E = (2^(E * log2(B)) - 1) + 1 = (2^X - 1) + 1 for X = E * log2(B)
2146 // FYL2X calculates st(1) * log2(st(0))
2147 __ fyl2x(); // X
2148 __ fld(0); // X, X
2149 __ frndint(); // rnd(X), X
2150 __ fsub(1); // rnd(X), X-rnd(X)
2151 __ fxch(1); // X - rnd(X), rnd(X)
2152 // F2XM1 calculates 2^st(0) - 1 for -1 < st(0) < 1
2153 __ f2xm1(); // 2^(X-rnd(X)) - 1, rnd(X)
2154 __ fld1(); // 1, 2^(X-rnd(X)) - 1, rnd(X)
2155 __ faddp(1); // 1, 2^(X-rnd(X)), rnd(X)
2156 // FSCALE calculates st(0) * 2^st(1)
2157 __ fscale(); // 2^X, rnd(X)
2158 __ fstp(1);
2159 // Bail out to runtime in case of exceptions in the status word.
2160 __ fnstsw_ax();
2161 __ testb(rax, Immediate(0x5F)); // Check for all but precision exception.
2162 __ j(not_zero, &fast_power_failed, Label::kNear);
2163 __ fstp_d(Operand(rsp, 0));
2164 __ movsd(double_result, Operand(rsp, 0));
2165 __ addq(rsp, Immediate(kDoubleSize));
2166 __ jmp(&done);
2167
2168 __ bind(&fast_power_failed);
2169 __ fninit();
2170 __ addq(rsp, Immediate(kDoubleSize));
2171 __ jmp(&call_runtime);
2172 }
2173
2174 // Calculate power with integer exponent.
2175 __ bind(&int_exponent);
2176 const XMMRegister double_scratch2 = double_exponent;
2177 // Back up exponent as we need to check if exponent is negative later.
2178 __ movq(scratch, exponent); // Back up exponent.
2179 __ movsd(double_scratch, double_base); // Back up base.
2180 __ movsd(double_scratch2, double_result); // Load double_exponent with 1.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002181
2182 // Get absolute value of exponent.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002183 Label no_neg, while_true, no_multiply;
2184 __ testl(scratch, scratch);
2185 __ j(positive, &no_neg, Label::kNear);
2186 __ negl(scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002187 __ bind(&no_neg);
2188
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002189 __ bind(&while_true);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002190 __ shrl(scratch, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00002191 __ j(not_carry, &no_multiply, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002192 __ mulsd(double_result, double_scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002193 __ bind(&no_multiply);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002194
2195 __ mulsd(double_scratch, double_scratch);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002196 __ j(not_zero, &while_true);
2197
Ben Murdochc7cc0282012-03-05 14:35:55 +00002198 // If the exponent is negative, return 1/result.
2199 __ testl(exponent, exponent);
2200 __ j(greater, &done);
2201 __ divsd(double_scratch2, double_result);
2202 __ movsd(double_result, double_scratch2);
2203 // Test whether result is zero. Bail out to check for subnormal result.
2204 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
2205 __ xorps(double_scratch2, double_scratch2);
2206 __ ucomisd(double_scratch2, double_result);
2207 // double_exponent aliased as double_scratch2 has already been overwritten
2208 // and may not have contained the exponent value in the first place when the
2209 // input was a smi. We reset it with exponent value before bailing out.
2210 __ j(not_equal, &done);
2211 __ cvtlsi2sd(double_exponent, exponent);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002212
Ben Murdochc7cc0282012-03-05 14:35:55 +00002213 // Returning or bailing out.
2214 Counters* counters = masm->isolate()->counters();
2215 if (exponent_type_ == ON_STACK) {
2216 // The arguments are still on the stack.
2217 __ bind(&call_runtime);
2218 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002219
Ben Murdochc7cc0282012-03-05 14:35:55 +00002220 // The stub is called from non-optimized code, which expects the result
2221 // as heap number in eax.
2222 __ bind(&done);
2223 __ AllocateHeapNumber(rax, rcx, &call_runtime);
2224 __ movsd(FieldOperand(rax, HeapNumber::kValueOffset), double_result);
2225 __ IncrementCounter(counters->math_pow(), 1);
2226 __ ret(2 * kPointerSize);
2227 } else {
2228 __ bind(&call_runtime);
2229 // Move base to the correct argument register. Exponent is already in xmm1.
2230 __ movsd(xmm0, double_base);
2231 ASSERT(double_exponent.is(xmm1));
2232 {
2233 AllowExternalCallThatCantCauseGC scope(masm);
2234 __ PrepareCallCFunction(2);
2235 __ CallCFunction(
2236 ExternalReference::power_double_double_function(masm->isolate()), 2);
2237 }
2238 // Return value is in xmm0.
2239 __ movsd(double_result, xmm0);
2240 // Restore context register.
2241 __ movq(rsi, Operand(rbp, StandardFrameConstants::kContextOffset));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002242
Ben Murdochc7cc0282012-03-05 14:35:55 +00002243 __ bind(&done);
2244 __ IncrementCounter(counters->math_pow(), 1);
2245 __ ret(0);
2246 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002247}
2248
2249
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002250void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2251 // The key is in rdx and the parameter count is in rax.
2252
2253 // The displacement is used for skipping the frame pointer on the
2254 // stack. It is the offset of the last parameter (if any) relative
2255 // to the frame pointer.
2256 static const int kDisplacement = 1 * kPointerSize;
2257
2258 // Check that the key is a smi.
2259 Label slow;
2260 __ JumpIfNotSmi(rdx, &slow);
2261
Steve Block44f0eee2011-05-26 01:26:41 +01002262 // Check if the calling frame is an arguments adaptor frame. We look at the
2263 // context offset, and if the frame is not a regular one, then we find a
2264 // Smi instead of the context. We can't use SmiCompare here, because that
2265 // only works for comparing two smis.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002266 Label adaptor;
2267 __ movq(rbx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002268 __ Cmp(Operand(rbx, StandardFrameConstants::kContextOffset),
2269 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002270 __ j(equal, &adaptor);
2271
2272 // Check index against formal parameters count limit passed in
2273 // through register rax. Use unsigned comparison to get negative
2274 // check for free.
2275 __ cmpq(rdx, rax);
2276 __ j(above_equal, &slow);
2277
2278 // Read the argument from the stack and return it.
2279 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
2280 __ lea(rbx, Operand(rbp, index.reg, index.scale, 0));
2281 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
2282 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
2283 __ Ret();
2284
2285 // Arguments adaptor case: Check index against actual arguments
2286 // limit found in the arguments adaptor frame. Use unsigned
2287 // comparison to get negative check for free.
2288 __ bind(&adaptor);
2289 __ movq(rcx, Operand(rbx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2290 __ cmpq(rdx, rcx);
2291 __ j(above_equal, &slow);
2292
2293 // Read the argument from the stack and return it.
2294 index = masm->SmiToIndex(rax, rcx, kPointerSizeLog2);
2295 __ lea(rbx, Operand(rbx, index.reg, index.scale, 0));
2296 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
2297 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
2298 __ Ret();
2299
2300 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2301 // by calling the runtime system.
2302 __ bind(&slow);
2303 __ pop(rbx); // Return address.
2304 __ push(rdx);
2305 __ push(rbx);
2306 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2307}
2308
2309
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002310void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
2311 // Stack layout:
2312 // rsp[0] : return address
2313 // rsp[8] : number of parameters (tagged)
2314 // rsp[16] : receiver displacement
2315 // rsp[24] : function
2316 // Registers used over the whole function:
2317 // rbx: the mapped parameter count (untagged)
2318 // rax: the allocated object (tagged).
2319
2320 Factory* factory = masm->isolate()->factory();
2321
2322 __ SmiToInteger64(rbx, Operand(rsp, 1 * kPointerSize));
2323 // rbx = parameter count (untagged)
2324
2325 // Check if the calling frame is an arguments adaptor frame.
2326 Label runtime;
2327 Label adaptor_frame, try_allocate;
2328 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
2329 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2330 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2331 __ j(equal, &adaptor_frame);
2332
2333 // No adaptor, parameter count = argument count.
2334 __ movq(rcx, rbx);
2335 __ jmp(&try_allocate, Label::kNear);
2336
2337 // We have an adaptor frame. Patch the parameters pointer.
2338 __ bind(&adaptor_frame);
2339 __ SmiToInteger64(rcx,
2340 Operand(rdx,
2341 ArgumentsAdaptorFrameConstants::kLengthOffset));
2342 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2343 StandardFrameConstants::kCallerSPOffset));
2344 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2345
2346 // rbx = parameter count (untagged)
2347 // rcx = argument count (untagged)
2348 // Compute the mapped parameter count = min(rbx, rcx) in rbx.
2349 __ cmpq(rbx, rcx);
2350 __ j(less_equal, &try_allocate, Label::kNear);
2351 __ movq(rbx, rcx);
2352
2353 __ bind(&try_allocate);
2354
2355 // Compute the sizes of backing store, parameter map, and arguments object.
2356 // 1. Parameter map, has 2 extra words containing context and backing store.
2357 const int kParameterMapHeaderSize =
2358 FixedArray::kHeaderSize + 2 * kPointerSize;
2359 Label no_parameter_map;
Ben Murdochc7cc0282012-03-05 14:35:55 +00002360 __ xor_(r8, r8);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002361 __ testq(rbx, rbx);
2362 __ j(zero, &no_parameter_map, Label::kNear);
2363 __ lea(r8, Operand(rbx, times_pointer_size, kParameterMapHeaderSize));
2364 __ bind(&no_parameter_map);
2365
2366 // 2. Backing store.
2367 __ lea(r8, Operand(r8, rcx, times_pointer_size, FixedArray::kHeaderSize));
2368
2369 // 3. Arguments object.
2370 __ addq(r8, Immediate(Heap::kArgumentsObjectSize));
2371
2372 // Do the allocation of all three objects in one go.
2373 __ AllocateInNewSpace(r8, rax, rdx, rdi, &runtime, TAG_OBJECT);
2374
2375 // rax = address of new object(s) (tagged)
2376 // rcx = argument count (untagged)
2377 // Get the arguments boilerplate from the current (global) context into rdi.
2378 Label has_mapped_parameters, copy;
2379 __ movq(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2380 __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
2381 __ testq(rbx, rbx);
2382 __ j(not_zero, &has_mapped_parameters, Label::kNear);
2383
2384 const int kIndex = Context::ARGUMENTS_BOILERPLATE_INDEX;
2385 __ movq(rdi, Operand(rdi, Context::SlotOffset(kIndex)));
2386 __ jmp(&copy, Label::kNear);
2387
2388 const int kAliasedIndex = Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX;
2389 __ bind(&has_mapped_parameters);
2390 __ movq(rdi, Operand(rdi, Context::SlotOffset(kAliasedIndex)));
2391 __ bind(&copy);
2392
2393 // rax = address of new object (tagged)
2394 // rbx = mapped parameter count (untagged)
2395 // rcx = argument count (untagged)
2396 // rdi = address of boilerplate object (tagged)
2397 // Copy the JS object part.
2398 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2399 __ movq(rdx, FieldOperand(rdi, i));
2400 __ movq(FieldOperand(rax, i), rdx);
2401 }
2402
Ben Murdochc7cc0282012-03-05 14:35:55 +00002403 // Set up the callee in-object property.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002404 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
2405 __ movq(rdx, Operand(rsp, 3 * kPointerSize));
2406 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
2407 Heap::kArgumentsCalleeIndex * kPointerSize),
2408 rdx);
2409
2410 // Use the length (smi tagged) and set that as an in-object property too.
2411 // Note: rcx is tagged from here on.
2412 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
2413 __ Integer32ToSmi(rcx, rcx);
2414 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
2415 Heap::kArgumentsLengthIndex * kPointerSize),
2416 rcx);
2417
Ben Murdochc7cc0282012-03-05 14:35:55 +00002418 // Set up the elements pointer in the allocated arguments object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002419 // If we allocated a parameter map, edi will point there, otherwise to the
2420 // backing store.
2421 __ lea(rdi, Operand(rax, Heap::kArgumentsObjectSize));
2422 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rdi);
2423
2424 // rax = address of new object (tagged)
2425 // rbx = mapped parameter count (untagged)
2426 // rcx = argument count (tagged)
2427 // rdi = address of parameter map or backing store (tagged)
2428
2429 // Initialize parameter map. If there are no mapped arguments, we're done.
2430 Label skip_parameter_map;
2431 __ testq(rbx, rbx);
2432 __ j(zero, &skip_parameter_map);
2433
2434 __ LoadRoot(kScratchRegister, Heap::kNonStrictArgumentsElementsMapRootIndex);
2435 // rbx contains the untagged argument count. Add 2 and tag to write.
2436 __ movq(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
2437 __ Integer64PlusConstantToSmi(r9, rbx, 2);
2438 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), r9);
2439 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize + 0 * kPointerSize), rsi);
2440 __ lea(r9, Operand(rdi, rbx, times_pointer_size, kParameterMapHeaderSize));
2441 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize + 1 * kPointerSize), r9);
2442
2443 // Copy the parameter slots and the holes in the arguments.
2444 // We need to fill in mapped_parameter_count slots. They index the context,
2445 // where parameters are stored in reverse order, at
2446 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
2447 // The mapped parameter thus need to get indices
2448 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
2449 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
2450 // We loop from right to left.
2451 Label parameters_loop, parameters_test;
2452
2453 // Load tagged parameter count into r9.
Ben Murdochc7cc0282012-03-05 14:35:55 +00002454 __ Integer32ToSmi(r9, rbx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002455 __ Move(r8, Smi::FromInt(Context::MIN_CONTEXT_SLOTS));
Ben Murdochc7cc0282012-03-05 14:35:55 +00002456 __ addq(r8, Operand(rsp, 1 * kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002457 __ subq(r8, r9);
2458 __ Move(r11, factory->the_hole_value());
2459 __ movq(rdx, rdi);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002460 __ lea(rdi, Operand(rdi, rbx, times_pointer_size, kParameterMapHeaderSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002461 // r9 = loop variable (tagged)
2462 // r8 = mapping index (tagged)
2463 // r11 = the hole value
2464 // rdx = address of parameter map (tagged)
2465 // rdi = address of backing store (tagged)
2466 __ jmp(&parameters_test, Label::kNear);
2467
2468 __ bind(&parameters_loop);
2469 __ SmiSubConstant(r9, r9, Smi::FromInt(1));
2470 __ SmiToInteger64(kScratchRegister, r9);
2471 __ movq(FieldOperand(rdx, kScratchRegister,
2472 times_pointer_size,
2473 kParameterMapHeaderSize),
2474 r8);
2475 __ movq(FieldOperand(rdi, kScratchRegister,
2476 times_pointer_size,
2477 FixedArray::kHeaderSize),
2478 r11);
2479 __ SmiAddConstant(r8, r8, Smi::FromInt(1));
2480 __ bind(&parameters_test);
2481 __ SmiTest(r9);
2482 __ j(not_zero, &parameters_loop, Label::kNear);
2483
2484 __ bind(&skip_parameter_map);
2485
2486 // rcx = argument count (tagged)
2487 // rdi = address of backing store (tagged)
2488 // Copy arguments header and remaining slots (if there are any).
2489 __ Move(FieldOperand(rdi, FixedArray::kMapOffset),
2490 factory->fixed_array_map());
2491 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
2492
2493 Label arguments_loop, arguments_test;
2494 __ movq(r8, rbx);
2495 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
Ben Murdochc7cc0282012-03-05 14:35:55 +00002496 // Untag rcx for the loop below.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002497 __ SmiToInteger64(rcx, rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002498 __ lea(kScratchRegister, Operand(r8, times_pointer_size, 0));
2499 __ subq(rdx, kScratchRegister);
2500 __ jmp(&arguments_test, Label::kNear);
2501
2502 __ bind(&arguments_loop);
2503 __ subq(rdx, Immediate(kPointerSize));
2504 __ movq(r9, Operand(rdx, 0));
2505 __ movq(FieldOperand(rdi, r8,
2506 times_pointer_size,
2507 FixedArray::kHeaderSize),
2508 r9);
2509 __ addq(r8, Immediate(1));
2510
2511 __ bind(&arguments_test);
2512 __ cmpq(r8, rcx);
2513 __ j(less, &arguments_loop, Label::kNear);
2514
2515 // Return and remove the on-stack parameters.
2516 __ ret(3 * kPointerSize);
2517
2518 // Do the runtime call to allocate the arguments object.
2519 // rcx = argument count (untagged)
2520 __ bind(&runtime);
2521 __ Integer32ToSmi(rcx, rcx);
2522 __ movq(Operand(rsp, 1 * kPointerSize), rcx); // Patch argument count.
2523 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
2524}
2525
2526
2527void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
2528 // esp[0] : return address
2529 // esp[8] : number of parameters
2530 // esp[16] : receiver displacement
2531 // esp[24] : function
2532
2533 // Check if the calling frame is an arguments adaptor frame.
2534 Label runtime;
2535 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
2536 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2537 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2538 __ j(not_equal, &runtime);
2539
2540 // Patch the arguments.length and the parameters pointer.
2541 __ movq(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2542 __ movq(Operand(rsp, 1 * kPointerSize), rcx);
2543 __ SmiToInteger64(rcx, rcx);
2544 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2545 StandardFrameConstants::kCallerSPOffset));
2546 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2547
2548 __ bind(&runtime);
2549 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2550}
2551
2552
2553void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002554 // rsp[0] : return address
2555 // rsp[8] : number of parameters
2556 // rsp[16] : receiver displacement
2557 // rsp[24] : function
2558
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002559 // Check if the calling frame is an arguments adaptor frame.
2560 Label adaptor_frame, try_allocate, runtime;
2561 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002562 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2563 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002564 __ j(equal, &adaptor_frame);
2565
2566 // Get the length from the frame.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002567 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
2568 __ SmiToInteger64(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002569 __ jmp(&try_allocate);
2570
2571 // Patch the arguments.length and the parameters pointer.
2572 __ bind(&adaptor_frame);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002573 __ movq(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2574 __ movq(Operand(rsp, 1 * kPointerSize), rcx);
2575 __ SmiToInteger64(rcx, rcx);
2576 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2577 StandardFrameConstants::kCallerSPOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002578 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2579
2580 // Try the new space allocation. Start out with computing the size of
2581 // the arguments object and the elements array.
2582 Label add_arguments_object;
2583 __ bind(&try_allocate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002584 __ testq(rcx, rcx);
2585 __ j(zero, &add_arguments_object, Label::kNear);
2586 __ lea(rcx, Operand(rcx, times_pointer_size, FixedArray::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002587 __ bind(&add_arguments_object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002588 __ addq(rcx, Immediate(Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002589
2590 // Do the allocation of both objects in one go.
2591 __ AllocateInNewSpace(rcx, rax, rdx, rbx, &runtime, TAG_OBJECT);
2592
2593 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002594 __ movq(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2595 __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002596 const int offset =
2597 Context::SlotOffset(Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX);
2598 __ movq(rdi, Operand(rdi, offset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002599
2600 // Copy the JS object part.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002601 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2602 __ movq(rbx, FieldOperand(rdi, i));
2603 __ movq(FieldOperand(rax, i), rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01002604 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002605
2606 // Get the length (smi tagged) and set that as an in-object property too.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002607 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002608 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01002609 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002610 Heap::kArgumentsLengthIndex * kPointerSize),
Steve Block44f0eee2011-05-26 01:26:41 +01002611 rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002612
2613 // If there are no actual arguments, we're done.
2614 Label done;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002615 __ testq(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002616 __ j(zero, &done);
2617
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002618 // Get the parameters pointer from the stack.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002619 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
2620
Ben Murdochc7cc0282012-03-05 14:35:55 +00002621 // Set up the elements pointer in the allocated arguments object and
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002622 // initialize the header in the elements fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002623 __ lea(rdi, Operand(rax, Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002624 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rdi);
2625 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
2626 __ movq(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002627
2628
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002629 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002630 // Untag the length for the loop below.
2631 __ SmiToInteger64(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002632
2633 // Copy the fixed array slots.
2634 Label loop;
2635 __ bind(&loop);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002636 __ movq(rbx, Operand(rdx, -1 * kPointerSize)); // Skip receiver.
2637 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize), rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002638 __ addq(rdi, Immediate(kPointerSize));
2639 __ subq(rdx, Immediate(kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002640 __ decq(rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002641 __ j(not_zero, &loop);
2642
2643 // Return and remove the on-stack parameters.
2644 __ bind(&done);
2645 __ ret(3 * kPointerSize);
2646
2647 // Do the runtime call to allocate the arguments object.
2648 __ bind(&runtime);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002649 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002650}
2651
2652
2653void RegExpExecStub::Generate(MacroAssembler* masm) {
2654 // Just jump directly to runtime if native RegExp is not selected at compile
2655 // time or if regexp entry in generated code is turned off runtime switch or
2656 // at compilation.
2657#ifdef V8_INTERPRETED_REGEXP
2658 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2659#else // V8_INTERPRETED_REGEXP
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002660
2661 // Stack frame on entry.
Steve Block1e0659c2011-05-24 12:43:12 +01002662 // rsp[0]: return address
2663 // rsp[8]: last_match_info (expected JSArray)
2664 // rsp[16]: previous index
2665 // rsp[24]: subject string
2666 // rsp[32]: JSRegExp object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002667
2668 static const int kLastMatchInfoOffset = 1 * kPointerSize;
2669 static const int kPreviousIndexOffset = 2 * kPointerSize;
2670 static const int kSubjectOffset = 3 * kPointerSize;
2671 static const int kJSRegExpOffset = 4 * kPointerSize;
2672
2673 Label runtime;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002674 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01002675 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002676 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01002677 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002678 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01002679 ExternalReference::address_of_regexp_stack_memory_size(isolate);
2680 __ Load(kScratchRegister, address_of_regexp_stack_memory_size);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002681 __ testq(kScratchRegister, kScratchRegister);
2682 __ j(zero, &runtime);
2683
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002684 // Check that the first argument is a JSRegExp object.
2685 __ movq(rax, Operand(rsp, kJSRegExpOffset));
2686 __ JumpIfSmi(rax, &runtime);
2687 __ CmpObjectType(rax, JS_REGEXP_TYPE, kScratchRegister);
2688 __ j(not_equal, &runtime);
2689 // Check that the RegExp has been compiled (data contains a fixed array).
Steve Block44f0eee2011-05-26 01:26:41 +01002690 __ movq(rax, FieldOperand(rax, JSRegExp::kDataOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002691 if (FLAG_debug_code) {
Steve Block44f0eee2011-05-26 01:26:41 +01002692 Condition is_smi = masm->CheckSmi(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002693 __ Check(NegateCondition(is_smi),
2694 "Unexpected type for RegExp data, FixedArray expected");
Steve Block44f0eee2011-05-26 01:26:41 +01002695 __ CmpObjectType(rax, FIXED_ARRAY_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002696 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
2697 }
2698
Steve Block44f0eee2011-05-26 01:26:41 +01002699 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002700 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
Steve Block44f0eee2011-05-26 01:26:41 +01002701 __ SmiToInteger32(rbx, FieldOperand(rax, JSRegExp::kDataTagOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002702 __ cmpl(rbx, Immediate(JSRegExp::IRREGEXP));
2703 __ j(not_equal, &runtime);
2704
Steve Block44f0eee2011-05-26 01:26:41 +01002705 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002706 // Check that the number of captures fit in the static offsets vector buffer.
2707 __ SmiToInteger32(rdx,
Steve Block44f0eee2011-05-26 01:26:41 +01002708 FieldOperand(rax, JSRegExp::kIrregexpCaptureCountOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002709 // Calculate number of capture registers (number_of_captures + 1) * 2.
2710 __ leal(rdx, Operand(rdx, rdx, times_1, 2));
2711 // Check that the static offsets vector buffer is large enough.
2712 __ cmpl(rdx, Immediate(OffsetsVector::kStaticOffsetsVectorSize));
2713 __ j(above, &runtime);
2714
Steve Block44f0eee2011-05-26 01:26:41 +01002715 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002716 // rdx: Number of capture registers
2717 // Check that the second argument is a string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002718 __ movq(rdi, Operand(rsp, kSubjectOffset));
2719 __ JumpIfSmi(rdi, &runtime);
2720 Condition is_string = masm->IsObjectStringType(rdi, rbx, rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002721 __ j(NegateCondition(is_string), &runtime);
2722
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002723 // rdi: Subject string.
2724 // rax: RegExp data (FixedArray).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002725 // rdx: Number of capture registers.
2726 // Check that the third argument is a positive smi less than the string
2727 // length. A negative value will be greater (unsigned comparison).
2728 __ movq(rbx, Operand(rsp, kPreviousIndexOffset));
2729 __ JumpIfNotSmi(rbx, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002730 __ SmiCompare(rbx, FieldOperand(rdi, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002731 __ j(above_equal, &runtime);
2732
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002733 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002734 // rdx: Number of capture registers
2735 // Check that the fourth object is a JSArray object.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002736 __ movq(rdi, Operand(rsp, kLastMatchInfoOffset));
2737 __ JumpIfSmi(rdi, &runtime);
2738 __ CmpObjectType(rdi, JS_ARRAY_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002739 __ j(not_equal, &runtime);
2740 // Check that the JSArray is in fast case.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002741 __ movq(rbx, FieldOperand(rdi, JSArray::kElementsOffset));
2742 __ movq(rdi, FieldOperand(rbx, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002743 __ CompareRoot(FieldOperand(rbx, HeapObject::kMapOffset),
2744 Heap::kFixedArrayMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002745 __ j(not_equal, &runtime);
2746 // Check that the last match info has space for the capture registers and the
2747 // additional information. Ensure no overflow in add.
2748 STATIC_ASSERT(FixedArray::kMaxLength < kMaxInt - FixedArray::kLengthOffset);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002749 __ SmiToInteger32(rdi, FieldOperand(rbx, FixedArray::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002750 __ addl(rdx, Immediate(RegExpImpl::kLastMatchOverhead));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002751 __ cmpl(rdx, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002752 __ j(greater, &runtime);
2753
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002754 // Reset offset for possibly sliced string.
2755 __ Set(r14, 0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002756 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002757 // Check the representation and encoding of the subject string.
Ben Murdoch257744e2011-11-30 15:57:28 +00002758 Label seq_ascii_string, seq_two_byte_string, check_code;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002759 __ movq(rdi, Operand(rsp, kSubjectOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002760 // Make a copy of the original subject string.
2761 __ movq(r15, rdi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002762 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002763 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
2764 // First check for flat two byte string.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002765 __ andb(rbx, Immediate(kIsNotStringMask |
2766 kStringRepresentationMask |
2767 kStringEncodingMask |
2768 kShortExternalStringMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002769 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002770 __ j(zero, &seq_two_byte_string, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002771 // Any other flat string must be a flat ASCII string. None of the following
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002772 // string type tests will succeed if subject is not a string or a short
2773 // external string.
2774 __ andb(rbx, Immediate(kIsNotStringMask |
2775 kStringRepresentationMask |
2776 kShortExternalStringMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00002777 __ j(zero, &seq_ascii_string, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002778
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002779 // rbx: whether subject is a string and if yes, its string representation
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002780 // Check for flat cons string or sliced string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002781 // A flat cons string is a cons string where the second part is the empty
2782 // string. In that case the subject string is just the first part of the cons
2783 // string. Also in this case the first part of the cons string is known to be
2784 // a sequential string or an external string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002785 // In the case of a sliced string its offset has to be taken into account.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002786 Label cons_string, external_string, check_encoding;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002787 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
2788 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002789 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
2790 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002791 __ cmpq(rbx, Immediate(kExternalStringTag));
2792 __ j(less, &cons_string, Label::kNear);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002793 __ j(equal, &external_string);
2794
2795 // Catch non-string subject or short external string.
2796 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
2797 __ testb(rbx, Immediate(kIsNotStringMask | kShortExternalStringMask));
2798 __ j(not_zero, &runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002799
2800 // String is sliced.
2801 __ SmiToInteger32(r14, FieldOperand(rdi, SlicedString::kOffsetOffset));
2802 __ movq(rdi, FieldOperand(rdi, SlicedString::kParentOffset));
2803 // r14: slice offset
2804 // r15: original subject string
2805 // rdi: parent string
2806 __ jmp(&check_encoding, Label::kNear);
2807 // String is a cons string, check whether it is flat.
2808 __ bind(&cons_string);
Steve Block44f0eee2011-05-26 01:26:41 +01002809 __ CompareRoot(FieldOperand(rdi, ConsString::kSecondOffset),
2810 Heap::kEmptyStringRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002811 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002812 __ movq(rdi, FieldOperand(rdi, ConsString::kFirstOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002813 // rdi: first part of cons string or parent of sliced string.
2814 // rbx: map of first part of cons string or map of parent of sliced string.
2815 // Is first part of cons or parent of slice a flat two byte string?
2816 __ bind(&check_encoding);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002817 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002818 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
2819 Immediate(kStringRepresentationMask | kStringEncodingMask));
2820 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002821 __ j(zero, &seq_two_byte_string, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002822 // Any other flat string must be sequential ASCII or external.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002823 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
2824 Immediate(kStringRepresentationMask));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002825 __ j(not_zero, &external_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002826
2827 __ bind(&seq_ascii_string);
Ben Murdochc7cc0282012-03-05 14:35:55 +00002828 // rdi: subject string (sequential ASCII)
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002829 // rax: RegExp data (FixedArray)
2830 __ movq(r11, FieldOperand(rax, JSRegExp::kDataAsciiCodeOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +00002831 __ Set(rcx, 1); // Type is ASCII.
Ben Murdoch257744e2011-11-30 15:57:28 +00002832 __ jmp(&check_code, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002833
2834 __ bind(&seq_two_byte_string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002835 // rdi: subject string (flat two-byte)
2836 // rax: RegExp data (FixedArray)
2837 __ movq(r11, FieldOperand(rax, JSRegExp::kDataUC16CodeOffset));
2838 __ Set(rcx, 0); // Type is two byte.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002839
2840 __ bind(&check_code);
2841 // Check that the irregexp code has been generated for the actual string
2842 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00002843 // smi (code flushing support)
2844 __ JumpIfSmi(r11, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002845
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002846 // rdi: subject string
Ben Murdochc7cc0282012-03-05 14:35:55 +00002847 // rcx: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002848 // r11: code
2849 // Load used arguments before starting to push arguments for call to native
2850 // RegExp code to avoid handling changing stack height.
2851 __ SmiToInteger64(rbx, Operand(rsp, kPreviousIndexOffset));
2852
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002853 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002854 // rbx: previous index
Ben Murdochc7cc0282012-03-05 14:35:55 +00002855 // rcx: encoding of subject string (1 if ASCII 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002856 // r11: code
2857 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01002858 Counters* counters = masm->isolate()->counters();
2859 __ IncrementCounter(counters->regexp_entry_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002860
Steve Block44f0eee2011-05-26 01:26:41 +01002861 // Isolates: note we add an additional parameter here (isolate pointer).
2862 static const int kRegExpExecuteArguments = 8;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002863 int argument_slots_on_stack =
2864 masm->ArgumentStackSlotsForCFunctionCall(kRegExpExecuteArguments);
Steve Block44f0eee2011-05-26 01:26:41 +01002865 __ EnterApiExitFrame(argument_slots_on_stack);
2866
2867 // Argument 8: Pass current isolate address.
2868 // __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
2869 // Immediate(ExternalReference::isolate_address()));
2870 __ LoadAddress(kScratchRegister, ExternalReference::isolate_address());
2871 __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
2872 kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002873
2874 // Argument 7: Indicate that this is a direct call from JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01002875 __ movq(Operand(rsp, (argument_slots_on_stack - 2) * kPointerSize),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002876 Immediate(1));
2877
2878 // Argument 6: Start (high end) of backtracking stack memory area.
2879 __ movq(kScratchRegister, address_of_regexp_stack_memory_address);
2880 __ movq(r9, Operand(kScratchRegister, 0));
2881 __ movq(kScratchRegister, address_of_regexp_stack_memory_size);
2882 __ addq(r9, Operand(kScratchRegister, 0));
2883 // Argument 6 passed in r9 on Linux and on the stack on Windows.
2884#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01002885 __ movq(Operand(rsp, (argument_slots_on_stack - 3) * kPointerSize), r9);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002886#endif
2887
2888 // Argument 5: static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01002889 __ LoadAddress(r8,
2890 ExternalReference::address_of_static_offsets_vector(isolate));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002891 // Argument 5 passed in r8 on Linux and on the stack on Windows.
2892#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01002893 __ movq(Operand(rsp, (argument_slots_on_stack - 4) * kPointerSize), r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002894#endif
2895
2896 // First four arguments are passed in registers on both Linux and Windows.
2897#ifdef _WIN64
2898 Register arg4 = r9;
2899 Register arg3 = r8;
2900 Register arg2 = rdx;
2901 Register arg1 = rcx;
2902#else
2903 Register arg4 = rcx;
2904 Register arg3 = rdx;
2905 Register arg2 = rsi;
2906 Register arg1 = rdi;
2907#endif
2908
2909 // Keep track on aliasing between argX defined above and the registers used.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002910 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002911 // rbx: previous index
Ben Murdochc7cc0282012-03-05 14:35:55 +00002912 // rcx: encoding of subject string (1 if ASCII 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002913 // r11: code
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002914 // r14: slice offset
2915 // r15: original subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002916
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002917 // Argument 2: Previous index.
2918 __ movq(arg2, rbx);
2919
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002920 // Argument 4: End of string data
2921 // Argument 3: Start of string data
2922 Label setup_two_byte, setup_rest, got_length, length_not_from_slice;
2923 // Prepare start and end index of the input.
2924 // Load the length from the original sliced string if that is the case.
2925 __ addq(rbx, r14);
2926 __ SmiToInteger32(arg3, FieldOperand(r15, String::kLengthOffset));
2927 __ addq(r14, arg3); // Using arg3 as scratch.
2928
2929 // rbx: start index of the input
2930 // r14: end index of the input
2931 // r15: original subject string
2932 __ testb(rcx, rcx); // Last use of rcx as encoding of subject string.
2933 __ j(zero, &setup_two_byte, Label::kNear);
2934 __ lea(arg4, FieldOperand(rdi, r14, times_1, SeqAsciiString::kHeaderSize));
2935 __ lea(arg3, FieldOperand(rdi, rbx, times_1, SeqAsciiString::kHeaderSize));
2936 __ jmp(&setup_rest, Label::kNear);
2937 __ bind(&setup_two_byte);
2938 __ lea(arg4, FieldOperand(rdi, r14, times_2, SeqTwoByteString::kHeaderSize));
2939 __ lea(arg3, FieldOperand(rdi, rbx, times_2, SeqTwoByteString::kHeaderSize));
2940 __ bind(&setup_rest);
2941
2942 // Argument 1: Original subject string.
2943 // The original subject is in the previous stack frame. Therefore we have to
2944 // use rbp, which points exactly to one pointer size below the previous rsp.
2945 // (Because creating a new stack frame pushes the previous rbp onto the stack
2946 // and thereby moves up rsp by one kPointerSize.)
2947 __ movq(arg1, r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002948
2949 // Locate the code entry and call it.
2950 __ addq(r11, Immediate(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002951 __ call(r11);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002952
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002953 __ LeaveApiExitFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002954
2955 // Check the result.
Ben Murdoch257744e2011-11-30 15:57:28 +00002956 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002957 Label exception;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002958 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::SUCCESS));
Ben Murdoch257744e2011-11-30 15:57:28 +00002959 __ j(equal, &success, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002960 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::EXCEPTION));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002961 __ j(equal, &exception);
2962 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::FAILURE));
2963 // If none of the above, it can only be retry.
2964 // Handle that in the runtime system.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002965 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002966
2967 // For failure return null.
2968 __ LoadRoot(rax, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002969 __ ret(4 * kPointerSize);
2970
2971 // Load RegExp data.
2972 __ bind(&success);
2973 __ movq(rax, Operand(rsp, kJSRegExpOffset));
2974 __ movq(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
2975 __ SmiToInteger32(rax,
2976 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
2977 // Calculate number of capture registers (number_of_captures + 1) * 2.
2978 __ leal(rdx, Operand(rax, rax, times_1, 2));
2979
2980 // rdx: Number of capture registers
2981 // Load last_match_info which is still known to be a fast case JSArray.
2982 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
2983 __ movq(rbx, FieldOperand(rax, JSArray::kElementsOffset));
2984
2985 // rbx: last_match_info backing store (FixedArray)
2986 // rdx: number of capture registers
2987 // Store the capture count.
2988 __ Integer32ToSmi(kScratchRegister, rdx);
2989 __ movq(FieldOperand(rbx, RegExpImpl::kLastCaptureCountOffset),
2990 kScratchRegister);
2991 // Store last subject and last input.
2992 __ movq(rax, Operand(rsp, kSubjectOffset));
2993 __ movq(FieldOperand(rbx, RegExpImpl::kLastSubjectOffset), rax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002994 __ RecordWriteField(rbx,
2995 RegExpImpl::kLastSubjectOffset,
2996 rax,
2997 rdi,
2998 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002999 __ movq(rax, Operand(rsp, kSubjectOffset));
3000 __ movq(FieldOperand(rbx, RegExpImpl::kLastInputOffset), rax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003001 __ RecordWriteField(rbx,
3002 RegExpImpl::kLastInputOffset,
3003 rax,
3004 rdi,
3005 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003006
3007 // Get the static offsets vector filled by the native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01003008 __ LoadAddress(rcx,
3009 ExternalReference::address_of_static_offsets_vector(isolate));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003010
3011 // rbx: last_match_info backing store (FixedArray)
3012 // rcx: offsets vector
3013 // rdx: number of capture registers
Ben Murdoch257744e2011-11-30 15:57:28 +00003014 Label next_capture, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003015 // Capture register counter starts from number of capture registers and
3016 // counts down until wraping after zero.
3017 __ bind(&next_capture);
3018 __ subq(rdx, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00003019 __ j(negative, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003020 // Read the value from the static offsets vector buffer and make it a smi.
3021 __ movl(rdi, Operand(rcx, rdx, times_int_size, 0));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003022 __ Integer32ToSmi(rdi, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003023 // Store the smi value in the last match info.
3024 __ movq(FieldOperand(rbx,
3025 rdx,
3026 times_pointer_size,
3027 RegExpImpl::kFirstCaptureOffset),
3028 rdi);
3029 __ jmp(&next_capture);
3030 __ bind(&done);
3031
3032 // Return last match info.
3033 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
3034 __ ret(4 * kPointerSize);
3035
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003036 __ bind(&exception);
3037 // Result must now be exception. If there is no pending exception already a
3038 // stack overflow (on the backtrack stack) was detected in RegExp code but
3039 // haven't created the exception yet. Handle that in the runtime system.
3040 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01003041 ExternalReference pending_exception_address(
Ben Murdoch589d6972011-11-30 16:04:58 +00003042 Isolate::kPendingExceptionAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01003043 Operand pending_exception_operand =
3044 masm->ExternalOperand(pending_exception_address, rbx);
3045 __ movq(rax, pending_exception_operand);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003046 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
3047 __ cmpq(rax, rdx);
3048 __ j(equal, &runtime);
Steve Block44f0eee2011-05-26 01:26:41 +01003049 __ movq(pending_exception_operand, rdx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003050
3051 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00003052 Label termination_exception;
3053 __ j(equal, &termination_exception, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003054 __ Throw(rax);
3055
3056 __ bind(&termination_exception);
3057 __ ThrowUncatchable(TERMINATION, rax);
3058
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003059 // External string. Short external strings have already been ruled out.
3060 // rdi: subject string (expected to be external)
3061 // rbx: scratch
3062 __ bind(&external_string);
3063 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
3064 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
3065 if (FLAG_debug_code) {
3066 // Assert that we do not have a cons or slice (indirect strings) here.
3067 // Sequential strings have already been ruled out.
3068 __ testb(rbx, Immediate(kIsIndirectStringMask));
3069 __ Assert(zero, "external string expected, but not found");
3070 }
3071 __ movq(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
3072 // Move the pointer so that offset-wise, it looks like a sequential string.
3073 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize);
3074 __ subq(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
3075 STATIC_ASSERT(kTwoByteStringTag == 0);
3076 __ testb(rbx, Immediate(kStringEncodingMask));
3077 __ j(not_zero, &seq_ascii_string);
3078 __ jmp(&seq_two_byte_string);
3079
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003080 // Do the runtime call to execute the regexp.
3081 __ bind(&runtime);
3082 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3083#endif // V8_INTERPRETED_REGEXP
3084}
3085
3086
Ben Murdochb0fe1622011-05-05 13:52:32 +01003087void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
3088 const int kMaxInlineLength = 100;
3089 Label slowcase;
3090 Label done;
3091 __ movq(r8, Operand(rsp, kPointerSize * 3));
3092 __ JumpIfNotSmi(r8, &slowcase);
3093 __ SmiToInteger32(rbx, r8);
3094 __ cmpl(rbx, Immediate(kMaxInlineLength));
3095 __ j(above, &slowcase);
3096 // Smi-tagging is equivalent to multiplying by 2.
3097 STATIC_ASSERT(kSmiTag == 0);
3098 STATIC_ASSERT(kSmiTagSize == 1);
Steve Block1e0659c2011-05-24 12:43:12 +01003099 // Allocate RegExpResult followed by FixedArray with size in rbx.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003100 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
3101 // Elements: [Map][Length][..elements..]
3102 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
3103 times_pointer_size,
3104 rbx, // In: Number of elements.
3105 rax, // Out: Start of allocation (tagged).
3106 rcx, // Out: End of allocation.
3107 rdx, // Scratch register
3108 &slowcase,
3109 TAG_OBJECT);
3110 // rax: Start of allocated area, object-tagged.
3111 // rbx: Number of array elements as int32.
3112 // r8: Number of array elements as smi.
3113
3114 // Set JSArray map to global.regexp_result_map().
3115 __ movq(rdx, ContextOperand(rsi, Context::GLOBAL_INDEX));
3116 __ movq(rdx, FieldOperand(rdx, GlobalObject::kGlobalContextOffset));
3117 __ movq(rdx, ContextOperand(rdx, Context::REGEXP_RESULT_MAP_INDEX));
3118 __ movq(FieldOperand(rax, HeapObject::kMapOffset), rdx);
3119
3120 // Set empty properties FixedArray.
Steve Block44f0eee2011-05-26 01:26:41 +01003121 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
3122 __ movq(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003123
3124 // Set elements to point to FixedArray allocated right after the JSArray.
3125 __ lea(rcx, Operand(rax, JSRegExpResult::kSize));
3126 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rcx);
3127
3128 // Set input, index and length fields from arguments.
3129 __ movq(r8, Operand(rsp, kPointerSize * 1));
3130 __ movq(FieldOperand(rax, JSRegExpResult::kInputOffset), r8);
3131 __ movq(r8, Operand(rsp, kPointerSize * 2));
3132 __ movq(FieldOperand(rax, JSRegExpResult::kIndexOffset), r8);
3133 __ movq(r8, Operand(rsp, kPointerSize * 3));
3134 __ movq(FieldOperand(rax, JSArray::kLengthOffset), r8);
3135
3136 // Fill out the elements FixedArray.
3137 // rax: JSArray.
3138 // rcx: FixedArray.
3139 // rbx: Number of elements in array as int32.
3140
3141 // Set map.
Steve Block44f0eee2011-05-26 01:26:41 +01003142 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
3143 __ movq(FieldOperand(rcx, HeapObject::kMapOffset), kScratchRegister);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003144 // Set length.
3145 __ Integer32ToSmi(rdx, rbx);
3146 __ movq(FieldOperand(rcx, FixedArray::kLengthOffset), rdx);
3147 // Fill contents of fixed-array with the-hole.
Steve Block44f0eee2011-05-26 01:26:41 +01003148 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003149 __ lea(rcx, FieldOperand(rcx, FixedArray::kHeaderSize));
3150 // Fill fixed array elements with hole.
3151 // rax: JSArray.
3152 // rbx: Number of elements in array that remains to be filled, as int32.
3153 // rcx: Start of elements in FixedArray.
3154 // rdx: the hole.
3155 Label loop;
3156 __ testl(rbx, rbx);
3157 __ bind(&loop);
Steve Block1e0659c2011-05-24 12:43:12 +01003158 __ j(less_equal, &done); // Jump if rcx is negative or zero.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003159 __ subl(rbx, Immediate(1));
3160 __ movq(Operand(rcx, rbx, times_pointer_size, 0), rdx);
3161 __ jmp(&loop);
3162
3163 __ bind(&done);
3164 __ ret(3 * kPointerSize);
3165
3166 __ bind(&slowcase);
3167 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3168}
3169
3170
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003171void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3172 Register object,
3173 Register result,
3174 Register scratch1,
3175 Register scratch2,
3176 bool object_is_smi,
3177 Label* not_found) {
3178 // Use of registers. Register result is used as a temporary.
3179 Register number_string_cache = result;
3180 Register mask = scratch1;
3181 Register scratch = scratch2;
3182
3183 // Load the number string cache.
3184 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
3185
3186 // Make the hash mask from the length of the number string cache. It
3187 // contains two elements (number and string) for each cache entry.
3188 __ SmiToInteger32(
3189 mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3190 __ shrl(mask, Immediate(1));
3191 __ subq(mask, Immediate(1)); // Make mask.
3192
3193 // Calculate the entry in the number string cache. The hash value in the
3194 // number string cache for smis is just the smi value, and the hash for
3195 // doubles is the xor of the upper and lower words. See
3196 // Heap::GetNumberStringCache.
3197 Label is_smi;
3198 Label load_result_from_cache;
Ben Murdoch257744e2011-11-30 15:57:28 +00003199 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003200 if (!object_is_smi) {
3201 __ JumpIfSmi(object, &is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +00003202 __ CheckMap(object,
3203 factory->heap_number_map(),
3204 not_found,
3205 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003206
3207 STATIC_ASSERT(8 == kDoubleSize);
3208 __ movl(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3209 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3210 GenerateConvertHashCodeToIndex(masm, scratch, mask);
3211
3212 Register index = scratch;
3213 Register probe = mask;
3214 __ movq(probe,
3215 FieldOperand(number_string_cache,
3216 index,
3217 times_1,
3218 FixedArray::kHeaderSize));
3219 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003220 __ movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3221 __ movsd(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3222 __ ucomisd(xmm0, xmm1);
3223 __ j(parity_even, not_found); // Bail out if NaN is involved.
3224 __ j(not_equal, not_found); // The cache did not contain this value.
3225 __ jmp(&load_result_from_cache);
3226 }
3227
3228 __ bind(&is_smi);
3229 __ SmiToInteger32(scratch, object);
3230 GenerateConvertHashCodeToIndex(masm, scratch, mask);
3231
3232 Register index = scratch;
3233 // Check if the entry is the smi we are looking for.
3234 __ cmpq(object,
3235 FieldOperand(number_string_cache,
3236 index,
3237 times_1,
3238 FixedArray::kHeaderSize));
3239 __ j(not_equal, not_found);
3240
3241 // Get the result from the cache.
3242 __ bind(&load_result_from_cache);
3243 __ movq(result,
3244 FieldOperand(number_string_cache,
3245 index,
3246 times_1,
3247 FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01003248 Counters* counters = masm->isolate()->counters();
3249 __ IncrementCounter(counters->number_to_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003250}
3251
3252
3253void NumberToStringStub::GenerateConvertHashCodeToIndex(MacroAssembler* masm,
3254 Register hash,
3255 Register mask) {
3256 __ and_(hash, mask);
3257 // Each entry in string cache consists of two pointer sized fields,
3258 // but times_twice_pointer_size (multiplication by 16) scale factor
3259 // is not supported by addrmode on x64 platform.
3260 // So we have to premultiply entry index before lookup.
3261 __ shl(hash, Immediate(kPointerSizeLog2 + 1));
3262}
3263
3264
3265void NumberToStringStub::Generate(MacroAssembler* masm) {
3266 Label runtime;
3267
3268 __ movq(rbx, Operand(rsp, kPointerSize));
3269
3270 // Generate code to lookup number in the number string cache.
3271 GenerateLookupNumberStringCache(masm, rbx, rax, r8, r9, false, &runtime);
3272 __ ret(1 * kPointerSize);
3273
3274 __ bind(&runtime);
3275 // Handle number to string in the runtime system if not found in the cache.
3276 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3277}
3278
3279
3280static int NegativeComparisonResult(Condition cc) {
3281 ASSERT(cc != equal);
3282 ASSERT((cc == less) || (cc == less_equal)
3283 || (cc == greater) || (cc == greater_equal));
3284 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3285}
3286
3287
3288void CompareStub::Generate(MacroAssembler* masm) {
3289 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3290
3291 Label check_unequal_objects, done;
Ben Murdoch257744e2011-11-30 15:57:28 +00003292 Factory* factory = masm->isolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003293
3294 // Compare two smis if required.
3295 if (include_smi_compare_) {
3296 Label non_smi, smi_done;
3297 __ JumpIfNotBothSmi(rax, rdx, &non_smi);
3298 __ subq(rdx, rax);
3299 __ j(no_overflow, &smi_done);
Ben Murdochf87a2032010-10-22 12:50:53 +01003300 __ not_(rdx); // Correct sign in case of overflow. rdx cannot be 0 here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003301 __ bind(&smi_done);
3302 __ movq(rax, rdx);
3303 __ ret(0);
3304 __ bind(&non_smi);
3305 } else if (FLAG_debug_code) {
3306 Label ok;
3307 __ JumpIfNotSmi(rdx, &ok);
3308 __ JumpIfNotSmi(rax, &ok);
3309 __ Abort("CompareStub: smi operands");
3310 __ bind(&ok);
3311 }
3312
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003313 // The compare stub returns a positive, negative, or zero 64-bit integer
3314 // value in rax, corresponding to result of comparing the two inputs.
3315 // NOTICE! This code is only reached after a smi-fast-case check, so
3316 // it is certain that at least one operand isn't a smi.
3317
3318 // Two identical objects are equal unless they are both NaN or undefined.
3319 {
Ben Murdoch257744e2011-11-30 15:57:28 +00003320 Label not_identical;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003321 __ cmpq(rax, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003322 __ j(not_equal, &not_identical, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003323
3324 if (cc_ != equal) {
3325 // Check for undefined. undefined OP undefined is false even though
3326 // undefined == undefined.
Ben Murdoch257744e2011-11-30 15:57:28 +00003327 Label check_for_nan;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003328 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00003329 __ j(not_equal, &check_for_nan, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003330 __ Set(rax, NegativeComparisonResult(cc_));
3331 __ ret(0);
3332 __ bind(&check_for_nan);
3333 }
3334
Steve Block44f0eee2011-05-26 01:26:41 +01003335 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003336 // so we do the second best thing - test it ourselves.
3337 // Note: if cc_ != equal, never_nan_nan_ is not used.
3338 // We cannot set rax to EQUAL until just before return because
3339 // rax must be unchanged on jump to not_identical.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003340 if (never_nan_nan_ && (cc_ == equal)) {
3341 __ Set(rax, EQUAL);
3342 __ ret(0);
3343 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00003344 Label heap_number;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003345 // If it's not a heap number, then return equal for (in)equality operator.
3346 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00003347 factory->heap_number_map());
3348 __ j(equal, &heap_number, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003349 if (cc_ != equal) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003350 // Call runtime on identical objects. Otherwise return equal.
3351 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003352 __ j(above_equal, &not_identical, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003353 }
3354 __ Set(rax, EQUAL);
3355 __ ret(0);
3356
3357 __ bind(&heap_number);
3358 // It is a heap number, so return equal if it's not NaN.
3359 // For NaN, return 1 for every condition except greater and
3360 // greater-equal. Return -1 for them, so the comparison yields
3361 // false for all conditions except not-equal.
3362 __ Set(rax, EQUAL);
3363 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
3364 __ ucomisd(xmm0, xmm0);
3365 __ setcc(parity_even, rax);
3366 // rax is 0 for equal non-NaN heapnumbers, 1 for NaNs.
3367 if (cc_ == greater_equal || cc_ == greater) {
3368 __ neg(rax);
3369 }
3370 __ ret(0);
3371 }
3372
3373 __ bind(&not_identical);
3374 }
3375
3376 if (cc_ == equal) { // Both strict and non-strict.
3377 Label slow; // Fallthrough label.
3378
3379 // If we're doing a strict equality comparison, we don't have to do
3380 // type conversion, so we generate code to do fast comparison for objects
3381 // and oddballs. Non-smi numbers and strings still go through the usual
3382 // slow-case code.
3383 if (strict_) {
3384 // If either is a Smi (we know that not both are), then they can only
3385 // be equal if the other is a HeapNumber. If so, use the slow case.
3386 {
3387 Label not_smis;
3388 __ SelectNonSmi(rbx, rax, rdx, &not_smis);
3389
3390 // Check if the non-smi operand is a heap number.
3391 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00003392 factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003393 // If heap number, handle it in the slow case.
3394 __ j(equal, &slow);
3395 // Return non-equal. ebx (the lower half of rbx) is not zero.
3396 __ movq(rax, rbx);
3397 __ ret(0);
3398
3399 __ bind(&not_smis);
3400 }
3401
3402 // If either operand is a JSObject or an oddball value, then they are not
3403 // equal since their pointers are different
3404 // There is no test for undetectability in strict equality.
3405
3406 // If the first object is a JS object, we have done pointer comparison.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003407 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +00003408 Label first_non_object;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003409 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003410 __ j(below, &first_non_object, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003411 // Return non-zero (eax (not rax) is not zero)
3412 Label return_not_equal;
3413 STATIC_ASSERT(kHeapObjectTag != 0);
3414 __ bind(&return_not_equal);
3415 __ ret(0);
3416
3417 __ bind(&first_non_object);
3418 // Check for oddballs: true, false, null, undefined.
3419 __ CmpInstanceType(rcx, ODDBALL_TYPE);
3420 __ j(equal, &return_not_equal);
3421
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003422 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003423 __ j(above_equal, &return_not_equal);
3424
3425 // Check for oddballs: true, false, null, undefined.
3426 __ CmpInstanceType(rcx, ODDBALL_TYPE);
3427 __ j(equal, &return_not_equal);
3428
3429 // Fall through to the general case.
3430 }
3431 __ bind(&slow);
3432 }
3433
3434 // Generate the number comparison code.
3435 if (include_number_compare_) {
3436 Label non_number_comparison;
Ben Murdoch257744e2011-11-30 15:57:28 +00003437 Label unordered;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003438 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &non_number_comparison);
3439 __ xorl(rax, rax);
3440 __ xorl(rcx, rcx);
3441 __ ucomisd(xmm0, xmm1);
3442
3443 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch257744e2011-11-30 15:57:28 +00003444 __ j(parity_even, &unordered, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003445 // Return a result of -1, 0, or 1, based on EFLAGS.
3446 __ setcc(above, rax);
3447 __ setcc(below, rcx);
3448 __ subq(rax, rcx);
3449 __ ret(0);
3450
3451 // If one of the numbers was NaN, then the result is always false.
3452 // The cc is never not-equal.
3453 __ bind(&unordered);
3454 ASSERT(cc_ != not_equal);
3455 if (cc_ == less || cc_ == less_equal) {
3456 __ Set(rax, 1);
3457 } else {
3458 __ Set(rax, -1);
3459 }
3460 __ ret(0);
3461
3462 // The number comparison code did not provide a valid result.
3463 __ bind(&non_number_comparison);
3464 }
3465
3466 // Fast negative check for symbol-to-symbol equality.
3467 Label check_for_strings;
3468 if (cc_ == equal) {
3469 BranchIfNonSymbol(masm, &check_for_strings, rax, kScratchRegister);
3470 BranchIfNonSymbol(masm, &check_for_strings, rdx, kScratchRegister);
3471
3472 // We've already checked for object identity, so if both operands
3473 // are symbols they aren't equal. Register eax (not rax) already holds a
3474 // non-zero value, which indicates not equal, so just return.
3475 __ ret(0);
3476 }
3477
3478 __ bind(&check_for_strings);
3479
3480 __ JumpIfNotBothSequentialAsciiStrings(
3481 rdx, rax, rcx, rbx, &check_unequal_objects);
3482
Ben Murdochc7cc0282012-03-05 14:35:55 +00003483 // Inline comparison of ASCII strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00003484 if (cc_ == equal) {
3485 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003486 rdx,
3487 rax,
3488 rcx,
Ben Murdoch257744e2011-11-30 15:57:28 +00003489 rbx);
3490 } else {
3491 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
3492 rdx,
3493 rax,
3494 rcx,
3495 rbx,
3496 rdi,
3497 r8);
3498 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003499
3500#ifdef DEBUG
3501 __ Abort("Unexpected fall-through from string comparison");
3502#endif
3503
3504 __ bind(&check_unequal_objects);
3505 if (cc_ == equal && !strict_) {
3506 // Not strict equality. Objects are unequal if
3507 // they are both JSObjects and not undetectable,
3508 // and their pointers are different.
Ben Murdoch257744e2011-11-30 15:57:28 +00003509 Label not_both_objects, return_unequal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003510 // At most one is a smi, so we can test for smi by adding the two.
3511 // A smi plus a heap object has the low bit set, a heap object plus
3512 // a heap object has the low bit clear.
3513 STATIC_ASSERT(kSmiTag == 0);
3514 STATIC_ASSERT(kSmiTagMask == 1);
3515 __ lea(rcx, Operand(rax, rdx, times_1, 0));
3516 __ testb(rcx, Immediate(kSmiTagMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00003517 __ j(not_zero, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003518 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003519 __ j(below, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003520 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003521 __ j(below, &not_both_objects, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003522 __ testb(FieldOperand(rbx, Map::kBitFieldOffset),
3523 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00003524 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003525 __ testb(FieldOperand(rcx, Map::kBitFieldOffset),
3526 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00003527 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003528 // The objects are both undetectable, so they both compare as the value
3529 // undefined, and are equal.
3530 __ Set(rax, EQUAL);
3531 __ bind(&return_unequal);
Steve Block1e0659c2011-05-24 12:43:12 +01003532 // Return non-equal by returning the non-zero object pointer in rax,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003533 // or return equal if we fell through to here.
3534 __ ret(0);
3535 __ bind(&not_both_objects);
3536 }
3537
3538 // Push arguments below the return address to prepare jump to builtin.
3539 __ pop(rcx);
3540 __ push(rdx);
3541 __ push(rax);
3542
3543 // Figure out which native to call and setup the arguments.
3544 Builtins::JavaScript builtin;
3545 if (cc_ == equal) {
3546 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
3547 } else {
3548 builtin = Builtins::COMPARE;
3549 __ Push(Smi::FromInt(NegativeComparisonResult(cc_)));
3550 }
3551
3552 // Restore return address on the stack.
3553 __ push(rcx);
3554
3555 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
3556 // tagged as a small integer.
3557 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
3558}
3559
3560
3561void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
3562 Label* label,
3563 Register object,
3564 Register scratch) {
3565 __ JumpIfSmi(object, label);
3566 __ movq(scratch, FieldOperand(object, HeapObject::kMapOffset));
3567 __ movzxbq(scratch,
3568 FieldOperand(scratch, Map::kInstanceTypeOffset));
3569 // Ensure that no non-strings have the symbol bit set.
3570 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
3571 STATIC_ASSERT(kSymbolTag != 0);
3572 __ testb(scratch, Immediate(kIsSymbolMask));
3573 __ j(zero, label);
3574}
3575
3576
3577void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01003578 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003579}
3580
3581
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003582void CallFunctionStub::FinishCode(Handle<Code> code) {
3583 code->set_has_function_cache(false);
3584}
3585
3586
3587void CallFunctionStub::Clear(Heap* heap, Address address) {
3588 UNREACHABLE();
3589}
3590
3591
3592Object* CallFunctionStub::GetCachedValue(Address address) {
3593 UNREACHABLE();
3594 return NULL;
3595}
3596
3597
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003598void CallFunctionStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003599 // rdi : the function to call
Ben Murdoch589d6972011-11-30 16:04:58 +00003600 Label slow, non_function;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003601
Ben Murdoch257744e2011-11-30 15:57:28 +00003602 // The receiver might implicitly be the global object. This is
3603 // indicated by passing the hole as the receiver to the call
3604 // function stub.
3605 if (ReceiverMightBeImplicit()) {
3606 Label call;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003607 // Get the receiver from the stack.
3608 // +1 ~ return address
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003609 __ movq(rax, Operand(rsp, (argc_ + 1) * kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00003610 // Call as function is indicated with the hole.
3611 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
3612 __ j(not_equal, &call, Label::kNear);
3613 // Patch the receiver on the stack with the global receiver object.
3614 __ movq(rbx, GlobalObjectOperand());
3615 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalReceiverOffset));
3616 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rbx);
3617 __ bind(&call);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003618 }
3619
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003620 // Check that the function really is a JavaScript function.
Ben Murdoch589d6972011-11-30 16:04:58 +00003621 __ JumpIfSmi(rdi, &non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003622 // Goto slow case if we do not have a function.
3623 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
3624 __ j(not_equal, &slow);
3625
3626 // Fast-case: Just invoke the function.
3627 ParameterCount actual(argc_);
Ben Murdoch257744e2011-11-30 15:57:28 +00003628
3629 if (ReceiverMightBeImplicit()) {
3630 Label call_as_function;
3631 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
3632 __ j(equal, &call_as_function);
3633 __ InvokeFunction(rdi,
3634 actual,
3635 JUMP_FUNCTION,
3636 NullCallWrapper(),
3637 CALL_AS_METHOD);
3638 __ bind(&call_as_function);
3639 }
3640 __ InvokeFunction(rdi,
3641 actual,
3642 JUMP_FUNCTION,
3643 NullCallWrapper(),
3644 CALL_AS_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003645
3646 // Slow-case: Non-function called.
3647 __ bind(&slow);
Ben Murdoch589d6972011-11-30 16:04:58 +00003648 // Check for function proxy.
3649 __ CmpInstanceType(rcx, JS_FUNCTION_PROXY_TYPE);
3650 __ j(not_equal, &non_function);
3651 __ pop(rcx);
3652 __ push(rdi); // put proxy as additional argument under return address
3653 __ push(rcx);
3654 __ Set(rax, argc_ + 1);
3655 __ Set(rbx, 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003656 __ SetCallKind(rcx, CALL_AS_METHOD);
Ben Murdoch589d6972011-11-30 16:04:58 +00003657 __ GetBuiltinEntry(rdx, Builtins::CALL_FUNCTION_PROXY);
3658 {
3659 Handle<Code> adaptor =
3660 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
3661 __ jmp(adaptor, RelocInfo::CODE_TARGET);
3662 }
3663
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003664 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3665 // of the original receiver from the call site).
Ben Murdoch589d6972011-11-30 16:04:58 +00003666 __ bind(&non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003667 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rdi);
3668 __ Set(rax, argc_);
3669 __ Set(rbx, 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00003670 __ SetCallKind(rcx, CALL_AS_METHOD);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003671 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01003672 Handle<Code> adaptor =
3673 Isolate::Current()->builtins()->ArgumentsAdaptorTrampoline();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003674 __ Jump(adaptor, RelocInfo::CODE_TARGET);
3675}
3676
3677
Steve Block44f0eee2011-05-26 01:26:41 +01003678bool CEntryStub::NeedsImmovableCode() {
3679 return false;
3680}
3681
3682
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003683bool CEntryStub::IsPregenerated() {
3684#ifdef _WIN64
3685 return result_size_ == 1;
3686#else
3687 return true;
3688#endif
3689}
3690
3691
3692void CodeStub::GenerateStubsAheadOfTime() {
3693 CEntryStub::GenerateAheadOfTime();
3694 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime();
3695 // It is important that the store buffer overflow stubs are generated first.
3696 RecordWriteStub::GenerateFixedRegStubsAheadOfTime();
3697}
3698
3699
3700void CodeStub::GenerateFPStubs() {
3701}
3702
3703
3704void CEntryStub::GenerateAheadOfTime() {
3705 CEntryStub stub(1, kDontSaveFPRegs);
3706 stub.GetCode()->set_is_pregenerated(true);
3707 CEntryStub save_doubles(1, kSaveFPRegs);
3708 save_doubles.GetCode()->set_is_pregenerated(true);
3709}
3710
3711
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003712void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003713 // Throw exception in eax.
3714 __ Throw(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003715}
3716
3717
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003718void CEntryStub::GenerateCore(MacroAssembler* masm,
3719 Label* throw_normal_exception,
3720 Label* throw_termination_exception,
3721 Label* throw_out_of_memory_exception,
3722 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01003723 bool always_allocate_scope) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003724 // rax: result parameter for PerformGC, if any.
3725 // rbx: pointer to C function (C callee-saved).
3726 // rbp: frame pointer (restored after C call).
3727 // rsp: stack pointer (restored after C call).
3728 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01003729 // r15: pointer to the first argument (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003730 // This pointer is reused in LeaveExitFrame(), so it is stored in a
3731 // callee-saved register.
3732
3733 // Simple results returned in rax (both AMD64 and Win64 calling conventions).
3734 // Complex results must be written to address passed as first argument.
3735 // AMD64 calling convention: a struct of two pointers in rax+rdx
3736
3737 // Check stack alignment.
3738 if (FLAG_debug_code) {
3739 __ CheckStackAlignment();
3740 }
3741
3742 if (do_gc) {
3743 // Pass failure code returned from last attempt as first argument to
3744 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
3745 // stack is known to be aligned. This function takes one argument which is
3746 // passed in register.
3747#ifdef _WIN64
3748 __ movq(rcx, rax);
3749#else // _WIN64
3750 __ movq(rdi, rax);
3751#endif
3752 __ movq(kScratchRegister,
3753 FUNCTION_ADDR(Runtime::PerformGC),
3754 RelocInfo::RUNTIME_ENTRY);
3755 __ call(kScratchRegister);
3756 }
3757
3758 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01003759 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003760 if (always_allocate_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +01003761 Operand scope_depth_operand = masm->ExternalOperand(scope_depth);
3762 __ incl(scope_depth_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003763 }
3764
3765 // Call C function.
3766#ifdef _WIN64
3767 // Windows 64-bit ABI passes arguments in rcx, rdx, r8, r9
3768 // Store Arguments object on stack, below the 4 WIN64 ABI parameter slots.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003769 __ movq(StackSpaceOperand(0), r14); // argc.
Steve Block44f0eee2011-05-26 01:26:41 +01003770 __ movq(StackSpaceOperand(1), r15); // argv.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003771 if (result_size_ < 2) {
3772 // Pass a pointer to the Arguments object as the first argument.
3773 // Return result in single register (rax).
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003774 __ lea(rcx, StackSpaceOperand(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003775 __ LoadAddress(rdx, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003776 } else {
3777 ASSERT_EQ(2, result_size_);
3778 // Pass a pointer to the result location as the first argument.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003779 __ lea(rcx, StackSpaceOperand(2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003780 // Pass a pointer to the Arguments object as the second argument.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003781 __ lea(rdx, StackSpaceOperand(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003782 __ LoadAddress(r8, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003783 }
3784
3785#else // _WIN64
3786 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9.
3787 __ movq(rdi, r14); // argc.
Steve Block44f0eee2011-05-26 01:26:41 +01003788 __ movq(rsi, r15); // argv.
3789 __ movq(rdx, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003790#endif
3791 __ call(rbx);
3792 // Result is in rax - do not destroy this register!
3793
3794 if (always_allocate_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +01003795 Operand scope_depth_operand = masm->ExternalOperand(scope_depth);
3796 __ decl(scope_depth_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003797 }
3798
3799 // Check for failure result.
3800 Label failure_returned;
3801 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3802#ifdef _WIN64
3803 // If return value is on the stack, pop it to registers.
3804 if (result_size_ > 1) {
3805 ASSERT_EQ(2, result_size_);
3806 // Read result values stored on stack. Result is stored
3807 // above the four argument mirror slots and the two
3808 // Arguments object slots.
3809 __ movq(rax, Operand(rsp, 6 * kPointerSize));
3810 __ movq(rdx, Operand(rsp, 7 * kPointerSize));
3811 }
3812#endif
3813 __ lea(rcx, Operand(rax, 1));
3814 // Lower 2 bits of rcx are 0 iff rax has failure tag.
3815 __ testl(rcx, Immediate(kFailureTagMask));
3816 __ j(zero, &failure_returned);
3817
3818 // Exit the JavaScript to C++ exit frame.
Steve Block1e0659c2011-05-24 12:43:12 +01003819 __ LeaveExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003820 __ ret(0);
3821
3822 // Handling of failure.
3823 __ bind(&failure_returned);
3824
Ben Murdoch257744e2011-11-30 15:57:28 +00003825 Label retry;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003826 // If the returned exception is RETRY_AFTER_GC continue at retry label
3827 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3828 __ testl(rax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00003829 __ j(zero, &retry, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003830
3831 // Special handling of out of memory exceptions.
3832 __ movq(kScratchRegister, Failure::OutOfMemoryException(), RelocInfo::NONE);
3833 __ cmpq(rax, kScratchRegister);
3834 __ j(equal, throw_out_of_memory_exception);
3835
3836 // Retrieve the pending exception and clear the variable.
Steve Block44f0eee2011-05-26 01:26:41 +01003837 ExternalReference pending_exception_address(
Ben Murdoch589d6972011-11-30 16:04:58 +00003838 Isolate::kPendingExceptionAddress, masm->isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01003839 Operand pending_exception_operand =
3840 masm->ExternalOperand(pending_exception_address);
3841 __ movq(rax, pending_exception_operand);
3842 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
3843 __ movq(pending_exception_operand, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003844
3845 // Special handling of termination exceptions which are uncatchable
3846 // by javascript code.
3847 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
3848 __ j(equal, throw_termination_exception);
3849
3850 // Handle normal exception.
3851 __ jmp(throw_normal_exception);
3852
3853 // Retry.
3854 __ bind(&retry);
3855}
3856
3857
3858void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3859 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003860 __ ThrowUncatchable(type, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003861}
3862
3863
3864void CEntryStub::Generate(MacroAssembler* masm) {
3865 // rax: number of arguments including receiver
3866 // rbx: pointer to C function (C callee-saved)
3867 // rbp: frame pointer of calling JS frame (restored after C call)
3868 // rsp: stack pointer (restored after C call)
3869 // rsi: current context (restored)
3870
3871 // NOTE: Invocations of builtins may return failure objects
3872 // instead of a proper result. The builtin entry handles
3873 // this by performing a garbage collection and retrying the
3874 // builtin once.
3875
3876 // Enter the exit frame that transitions from JavaScript to C++.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003877#ifdef _WIN64
3878 int arg_stack_space = (result_size_ < 2 ? 2 : 4);
3879#else
3880 int arg_stack_space = 0;
3881#endif
Steve Block1e0659c2011-05-24 12:43:12 +01003882 __ EnterExitFrame(arg_stack_space, save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003883
3884 // rax: Holds the context at this point, but should not be used.
3885 // On entry to code generated by GenerateCore, it must hold
3886 // a failure result if the collect_garbage argument to GenerateCore
3887 // is true. This failure result can be the result of code
3888 // generated by a previous call to GenerateCore. The value
3889 // of rax is then passed to Runtime::PerformGC.
3890 // rbx: pointer to builtin function (C callee-saved).
3891 // rbp: frame pointer of exit frame (restored after C call).
3892 // rsp: stack pointer (restored after C call).
3893 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01003894 // r15: argv pointer (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003895
3896 Label throw_normal_exception;
3897 Label throw_termination_exception;
3898 Label throw_out_of_memory_exception;
3899
3900 // Call into the runtime system.
3901 GenerateCore(masm,
3902 &throw_normal_exception,
3903 &throw_termination_exception,
3904 &throw_out_of_memory_exception,
3905 false,
3906 false);
3907
3908 // Do space-specific GC and retry runtime call.
3909 GenerateCore(masm,
3910 &throw_normal_exception,
3911 &throw_termination_exception,
3912 &throw_out_of_memory_exception,
3913 true,
3914 false);
3915
3916 // Do full GC and retry runtime call one final time.
3917 Failure* failure = Failure::InternalError();
3918 __ movq(rax, failure, RelocInfo::NONE);
3919 GenerateCore(masm,
3920 &throw_normal_exception,
3921 &throw_termination_exception,
3922 &throw_out_of_memory_exception,
3923 true,
3924 true);
3925
3926 __ bind(&throw_out_of_memory_exception);
3927 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3928
3929 __ bind(&throw_termination_exception);
3930 GenerateThrowUncatchable(masm, TERMINATION);
3931
3932 __ bind(&throw_normal_exception);
3933 GenerateThrowTOS(masm);
3934}
3935
3936
3937void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003938 Label invoke, handler_entry, exit;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003939 Label not_outermost_js, not_outermost_js_2;
Steve Block44f0eee2011-05-26 01:26:41 +01003940 { // NOLINT. Scope block confuses linter.
3941 MacroAssembler::NoRootArrayScope uninitialized_root_register(masm);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003942 // Set up frame.
Steve Block44f0eee2011-05-26 01:26:41 +01003943 __ push(rbp);
3944 __ movq(rbp, rsp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003945
Steve Block44f0eee2011-05-26 01:26:41 +01003946 // Push the stack frame type marker twice.
3947 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3948 // Scratch register is neither callee-save, nor an argument register on any
3949 // platform. It's free to use at this point.
3950 // Cannot use smi-register for loading yet.
3951 __ movq(kScratchRegister,
3952 reinterpret_cast<uint64_t>(Smi::FromInt(marker)),
3953 RelocInfo::NONE);
3954 __ push(kScratchRegister); // context slot
3955 __ push(kScratchRegister); // function slot
3956 // Save callee-saved registers (X64/Win64 calling conventions).
3957 __ push(r12);
3958 __ push(r13);
3959 __ push(r14);
3960 __ push(r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003961#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01003962 __ push(rdi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
3963 __ push(rsi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003964#endif
Steve Block44f0eee2011-05-26 01:26:41 +01003965 __ push(rbx);
3966 // TODO(X64): On Win64, if we ever use XMM6-XMM15, the low low 64 bits are
3967 // callee save as well.
3968
3969 // Set up the roots and smi constant registers.
3970 // Needs to be done before any further smi loads.
3971 __ InitializeSmiConstantRegister();
3972 __ InitializeRootRegister();
3973 }
3974
3975 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003976
3977 // Save copies of the top frame descriptor on the stack.
Ben Murdoch589d6972011-11-30 16:04:58 +00003978 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01003979 {
3980 Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
3981 __ push(c_entry_fp_operand);
3982 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003983
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003984 // If this is the outermost JS call, set js_entry_sp value.
Ben Murdoch589d6972011-11-30 16:04:58 +00003985 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01003986 __ Load(rax, js_entry_sp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003987 __ testq(rax, rax);
3988 __ j(not_zero, &not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01003989 __ Push(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003990 __ movq(rax, rbp);
Steve Block44f0eee2011-05-26 01:26:41 +01003991 __ Store(js_entry_sp, rax);
Steve Block053d10c2011-06-13 19:13:29 +01003992 Label cont;
3993 __ jmp(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003994 __ bind(&not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01003995 __ Push(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME));
3996 __ bind(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003997
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003998 // Jump to a faked try block that does the invoke, with a faked catch
3999 // block that sets the pending exception.
4000 __ jmp(&invoke);
4001 __ bind(&handler_entry);
4002 handler_offset_ = handler_entry.pos();
4003 // Caught exception: Store result (exception) in the pending exception
4004 // field in the JSEnv and return a failure sentinel.
Ben Murdoch589d6972011-11-30 16:04:58 +00004005 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01004006 isolate);
4007 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004008 __ movq(rax, Failure::Exception(), RelocInfo::NONE);
4009 __ jmp(&exit);
4010
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004011 // Invoke: Link this frame into the handler chain. There's only one
4012 // handler block in this code object, so its index is 0.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004013 __ bind(&invoke);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004014 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004015
4016 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01004017 __ LoadRoot(rax, Heap::kTheHoleValueRootIndex);
4018 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004019
4020 // Fake a receiver (NULL).
4021 __ push(Immediate(0)); // receiver
4022
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004023 // Invoke the function by calling through JS entry trampoline builtin and
4024 // pop the faked function when we return. We load the address from an
4025 // external reference instead of inlining the call target address directly
4026 // in the code, because the builtin stubs may not have been generated yet
4027 // at the time this code is generated.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004028 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01004029 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
4030 isolate);
4031 __ Load(rax, construct_entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004032 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01004033 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
4034 __ Load(rax, entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004035 }
4036 __ lea(kScratchRegister, FieldOperand(rax, Code::kHeaderSize));
4037 __ call(kScratchRegister);
4038
4039 // Unlink this frame from the handler chain.
Steve Block053d10c2011-06-13 19:13:29 +01004040 __ PopTryHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004041
Steve Block053d10c2011-06-13 19:13:29 +01004042 __ bind(&exit);
Steve Block053d10c2011-06-13 19:13:29 +01004043 // Check if the current stack frame is marked as the outermost JS frame.
4044 __ pop(rbx);
4045 __ Cmp(rbx, Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004046 __ j(not_equal, &not_outermost_js_2);
Steve Block053d10c2011-06-13 19:13:29 +01004047 __ movq(kScratchRegister, js_entry_sp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004048 __ movq(Operand(kScratchRegister, 0), Immediate(0));
4049 __ bind(&not_outermost_js_2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004050
4051 // Restore the top frame descriptor from the stack.
Steve Block053d10c2011-06-13 19:13:29 +01004052 { Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
Steve Block44f0eee2011-05-26 01:26:41 +01004053 __ pop(c_entry_fp_operand);
4054 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004055
4056 // Restore callee-saved registers (X64 conventions).
4057 __ pop(rbx);
4058#ifdef _WIN64
4059 // Callee save on in Win64 ABI, arguments/volatile in AMD64 ABI.
4060 __ pop(rsi);
4061 __ pop(rdi);
4062#endif
4063 __ pop(r15);
4064 __ pop(r14);
4065 __ pop(r13);
4066 __ pop(r12);
4067 __ addq(rsp, Immediate(2 * kPointerSize)); // remove markers
4068
4069 // Restore frame pointer and return.
4070 __ pop(rbp);
4071 __ ret(0);
4072}
4073
4074
4075void InstanceofStub::Generate(MacroAssembler* masm) {
4076 // Implements "value instanceof function" operator.
Steve Block44f0eee2011-05-26 01:26:41 +01004077 // Expected input state with no inline cache:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004078 // rsp[0] : return address
4079 // rsp[1] : function pointer
4080 // rsp[2] : value
Steve Block44f0eee2011-05-26 01:26:41 +01004081 // Expected input state with an inline one-element cache:
4082 // rsp[0] : return address
4083 // rsp[1] : offset from return address to location of inline cache
4084 // rsp[2] : function pointer
4085 // rsp[3] : value
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004086 // Returns a bitwise zero to indicate that the value
4087 // is and instance of the function and anything else to
4088 // indicate that the value is not an instance.
4089
Ben Murdoch8b112d22011-06-08 16:22:53 +01004090 static const int kOffsetToMapCheckValue = 2;
4091 static const int kOffsetToResultValue = 18;
Steve Block44f0eee2011-05-26 01:26:41 +01004092 // The last 4 bytes of the instruction sequence
Ben Murdoch8b112d22011-06-08 16:22:53 +01004093 // movq(rdi, FieldOperand(rax, HeapObject::kMapOffset))
Steve Block44f0eee2011-05-26 01:26:41 +01004094 // Move(kScratchRegister, FACTORY->the_hole_value())
4095 // in front of the hole value address.
4096 static const unsigned int kWordBeforeMapCheckValue = 0xBA49FF78;
4097 // The last 4 bytes of the instruction sequence
4098 // __ j(not_equal, &cache_miss);
4099 // __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
4100 // before the offset of the hole value in the root array.
4101 static const unsigned int kWordBeforeResultValue = 0x458B4909;
4102 // Only the inline check flag is supported on X64.
4103 ASSERT(flags_ == kNoFlags || HasCallSiteInlineCheck());
4104 int extra_stack_space = HasCallSiteInlineCheck() ? kPointerSize : 0;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004105
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004106 // Get the object - go slow case if it's a smi.
4107 Label slow;
Steve Block44f0eee2011-05-26 01:26:41 +01004108
4109 __ movq(rax, Operand(rsp, 2 * kPointerSize + extra_stack_space));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004110 __ JumpIfSmi(rax, &slow);
4111
4112 // Check that the left hand is a JS object. Leave its map in rax.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004113 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004114 __ j(below, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004115 __ CmpInstanceType(rax, LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004116 __ j(above, &slow);
4117
4118 // Get the prototype of the function.
Steve Block44f0eee2011-05-26 01:26:41 +01004119 __ movq(rdx, Operand(rsp, 1 * kPointerSize + extra_stack_space));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004120 // rdx is function, rax is map.
4121
Steve Block44f0eee2011-05-26 01:26:41 +01004122 // If there is a call site cache don't look in the global cache, but do the
4123 // real lookup and update the call site cache.
4124 if (!HasCallSiteInlineCheck()) {
4125 // Look up the function and the map in the instanceof cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00004126 Label miss;
Steve Block44f0eee2011-05-26 01:26:41 +01004127 __ CompareRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00004128 __ j(not_equal, &miss, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01004129 __ CompareRoot(rax, Heap::kInstanceofCacheMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00004130 __ j(not_equal, &miss, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01004131 __ LoadRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
4132 __ ret(2 * kPointerSize);
4133 __ bind(&miss);
4134 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004135
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004136 __ TryGetFunctionPrototype(rdx, rbx, &slow, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004137
4138 // Check that the function prototype is a JS object.
4139 __ JumpIfSmi(rbx, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004140 __ CmpObjectType(rbx, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004141 __ j(below, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004142 __ CmpInstanceType(kScratchRegister, LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004143 __ j(above, &slow);
4144
4145 // Register mapping:
4146 // rax is object map.
4147 // rdx is function.
4148 // rbx is function prototype.
Steve Block44f0eee2011-05-26 01:26:41 +01004149 if (!HasCallSiteInlineCheck()) {
4150 __ StoreRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
4151 __ StoreRoot(rax, Heap::kInstanceofCacheMapRootIndex);
4152 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004153 // Get return address and delta to inlined map check.
Steve Block44f0eee2011-05-26 01:26:41 +01004154 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
4155 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01004156 if (FLAG_debug_code) {
4157 __ movl(rdi, Immediate(kWordBeforeMapCheckValue));
4158 __ cmpl(Operand(kScratchRegister, kOffsetToMapCheckValue - 4), rdi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004159 __ Assert(equal, "InstanceofStub unexpected call site cache (check).");
Steve Block44f0eee2011-05-26 01:26:41 +01004160 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004161 __ movq(kScratchRegister,
4162 Operand(kScratchRegister, kOffsetToMapCheckValue));
4163 __ movq(Operand(kScratchRegister, 0), rax);
Steve Block44f0eee2011-05-26 01:26:41 +01004164 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004165
4166 __ movq(rcx, FieldOperand(rax, Map::kPrototypeOffset));
4167
4168 // Loop through the prototype chain looking for the function prototype.
Ben Murdoch257744e2011-11-30 15:57:28 +00004169 Label loop, is_instance, is_not_instance;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004170 __ LoadRoot(kScratchRegister, Heap::kNullValueRootIndex);
4171 __ bind(&loop);
4172 __ cmpq(rcx, rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004173 __ j(equal, &is_instance, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004174 __ cmpq(rcx, kScratchRegister);
4175 // The code at is_not_instance assumes that kScratchRegister contains a
4176 // non-zero GCable value (the null object in this case).
Ben Murdoch257744e2011-11-30 15:57:28 +00004177 __ j(equal, &is_not_instance, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004178 __ movq(rcx, FieldOperand(rcx, HeapObject::kMapOffset));
4179 __ movq(rcx, FieldOperand(rcx, Map::kPrototypeOffset));
4180 __ jmp(&loop);
4181
4182 __ bind(&is_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01004183 if (!HasCallSiteInlineCheck()) {
4184 __ xorl(rax, rax);
4185 // Store bitwise zero in the cache. This is a Smi in GC terms.
4186 STATIC_ASSERT(kSmiTag == 0);
4187 __ StoreRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
4188 } else {
4189 // Store offset of true in the root array at the inline check site.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004190 int true_offset = 0x100 +
4191 (Heap::kTrueValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
4192 // Assert it is a 1-byte signed value.
4193 ASSERT(true_offset >= 0 && true_offset < 0x100);
4194 __ movl(rax, Immediate(true_offset));
Steve Block44f0eee2011-05-26 01:26:41 +01004195 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
4196 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
4197 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
4198 if (FLAG_debug_code) {
4199 __ movl(rax, Immediate(kWordBeforeResultValue));
4200 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004201 __ Assert(equal, "InstanceofStub unexpected call site cache (mov).");
Steve Block44f0eee2011-05-26 01:26:41 +01004202 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01004203 __ Set(rax, 0);
Steve Block44f0eee2011-05-26 01:26:41 +01004204 }
4205 __ ret(2 * kPointerSize + extra_stack_space);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004206
4207 __ bind(&is_not_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01004208 if (!HasCallSiteInlineCheck()) {
4209 // We have to store a non-zero value in the cache.
4210 __ StoreRoot(kScratchRegister, Heap::kInstanceofCacheAnswerRootIndex);
4211 } else {
4212 // Store offset of false in the root array at the inline check site.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004213 int false_offset = 0x100 +
4214 (Heap::kFalseValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
4215 // Assert it is a 1-byte signed value.
4216 ASSERT(false_offset >= 0 && false_offset < 0x100);
4217 __ movl(rax, Immediate(false_offset));
Steve Block44f0eee2011-05-26 01:26:41 +01004218 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
4219 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
4220 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
4221 if (FLAG_debug_code) {
4222 __ movl(rax, Immediate(kWordBeforeResultValue));
4223 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
4224 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4225 }
4226 }
4227 __ ret(2 * kPointerSize + extra_stack_space);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004228
4229 // Slow-case: Go through the JavaScript implementation.
4230 __ bind(&slow);
Steve Block44f0eee2011-05-26 01:26:41 +01004231 if (HasCallSiteInlineCheck()) {
4232 // Remove extra value from the stack.
4233 __ pop(rcx);
4234 __ pop(rax);
4235 __ push(rcx);
4236 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004237 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4238}
4239
4240
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004241// Passing arguments in registers is not supported.
4242Register InstanceofStub::left() { return no_reg; }
Steve Block1e0659c2011-05-24 12:43:12 +01004243
4244
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004245Register InstanceofStub::right() { return no_reg; }
Steve Block1e0659c2011-05-24 12:43:12 +01004246
4247
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004248int CompareStub::MinorKey() {
4249 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4250 // stubs the never NaN NaN condition is only taken into account if the
4251 // condition is equals.
4252 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4253 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4254 return ConditionField::encode(static_cast<unsigned>(cc_))
4255 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4256 | StrictField::encode(strict_)
4257 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004258 | IncludeNumberCompareField::encode(include_number_compare_)
4259 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004260}
4261
4262
4263// Unfortunately you have to run without snapshots to see most of these
4264// names in the profile since most compare stubs end up in the snapshot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004265void CompareStub::PrintName(StringStream* stream) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004266 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004267 const char* cc_name;
4268 switch (cc_) {
4269 case less: cc_name = "LT"; break;
4270 case greater: cc_name = "GT"; break;
4271 case less_equal: cc_name = "LE"; break;
4272 case greater_equal: cc_name = "GE"; break;
4273 case equal: cc_name = "EQ"; break;
4274 case not_equal: cc_name = "NE"; break;
4275 default: cc_name = "UnknownCondition"; break;
4276 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004277 bool is_equality = cc_ == equal || cc_ == not_equal;
4278 stream->Add("CompareStub_%s", cc_name);
4279 if (strict_ && is_equality) stream->Add("_STRICT");
4280 if (never_nan_nan_ && is_equality) stream->Add("_NO_NAN");
4281 if (!include_number_compare_) stream->Add("_NO_NUMBER");
4282 if (!include_smi_compare_) stream->Add("_NO_SMI");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004283}
4284
4285
4286// -------------------------------------------------------------------------
4287// StringCharCodeAtGenerator
4288
4289void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4290 Label flat_string;
4291 Label ascii_string;
4292 Label got_char_code;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004293 Label sliced_string;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004294
4295 // If the receiver is a smi trigger the non-string case.
4296 __ JumpIfSmi(object_, receiver_not_string_);
4297
4298 // Fetch the instance type of the receiver into result register.
4299 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
4300 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4301 // If the receiver is not a string trigger the non-string case.
4302 __ testb(result_, Immediate(kIsNotStringMask));
4303 __ j(not_zero, receiver_not_string_);
4304
4305 // If the index is non-smi trigger the non-smi case.
4306 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004307 __ bind(&got_smi_index_);
4308
4309 // Check for index out of range.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004310 __ SmiCompare(index_, FieldOperand(object_, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004311 __ j(above_equal, index_out_of_range_);
4312
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004313 __ SmiToInteger32(index_, index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004314
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004315 StringCharLoadGenerator::Generate(
4316 masm, object_, index_, result_, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004317
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004318 __ Integer32ToSmi(result_, result_);
4319 __ bind(&exit_);
4320}
4321
4322
4323void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004324 MacroAssembler* masm,
4325 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004326 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4327
Ben Murdoch257744e2011-11-30 15:57:28 +00004328 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004329 // Index is not a smi.
4330 __ bind(&index_not_smi_);
4331 // If index is a heap number, try converting it to an integer.
Ben Murdoch257744e2011-11-30 15:57:28 +00004332 __ CheckMap(index_,
4333 factory->heap_number_map(),
4334 index_not_number_,
4335 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004336 call_helper.BeforeCall(masm);
4337 __ push(object_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004338 __ push(index_); // Consumed by runtime conversion function.
4339 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4340 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4341 } else {
4342 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4343 // NumberToSmi discards numbers that are not exact integers.
4344 __ CallRuntime(Runtime::kNumberToSmi, 1);
4345 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004346 if (!index_.is(rax)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004347 // Save the conversion result before the pop instructions below
4348 // have a chance to overwrite it.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004349 __ movq(index_, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004350 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004351 __ pop(object_);
4352 // Reload the instance type.
4353 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
4354 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4355 call_helper.AfterCall(masm);
4356 // If index is still not a smi, it must be out of range.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004357 __ JumpIfNotSmi(index_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004358 // Otherwise, return to the fast path.
4359 __ jmp(&got_smi_index_);
4360
4361 // Call runtime. We get here when the receiver is a string and the
4362 // index is a number, but the code of getting the actual character
4363 // is too complex (e.g., when the string needs to be flattened).
4364 __ bind(&call_runtime_);
4365 call_helper.BeforeCall(masm);
4366 __ push(object_);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004367 __ Integer32ToSmi(index_, index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004368 __ push(index_);
4369 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4370 if (!result_.is(rax)) {
4371 __ movq(result_, rax);
4372 }
4373 call_helper.AfterCall(masm);
4374 __ jmp(&exit_);
4375
4376 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4377}
4378
4379
4380// -------------------------------------------------------------------------
4381// StringCharFromCodeGenerator
4382
4383void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4384 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4385 __ JumpIfNotSmi(code_, &slow_case_);
4386 __ SmiCompare(code_, Smi::FromInt(String::kMaxAsciiCharCode));
4387 __ j(above, &slow_case_);
4388
4389 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
4390 SmiIndex index = masm->SmiToIndex(kScratchRegister, code_, kPointerSizeLog2);
4391 __ movq(result_, FieldOperand(result_, index.reg, index.scale,
4392 FixedArray::kHeaderSize));
4393 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
4394 __ j(equal, &slow_case_);
4395 __ bind(&exit_);
4396}
4397
4398
4399void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004400 MacroAssembler* masm,
4401 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004402 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4403
4404 __ bind(&slow_case_);
4405 call_helper.BeforeCall(masm);
4406 __ push(code_);
4407 __ CallRuntime(Runtime::kCharFromCode, 1);
4408 if (!result_.is(rax)) {
4409 __ movq(result_, rax);
4410 }
4411 call_helper.AfterCall(masm);
4412 __ jmp(&exit_);
4413
4414 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4415}
4416
4417
4418// -------------------------------------------------------------------------
4419// StringCharAtGenerator
4420
4421void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4422 char_code_at_generator_.GenerateFast(masm);
4423 char_from_code_generator_.GenerateFast(masm);
4424}
4425
4426
4427void StringCharAtGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004428 MacroAssembler* masm,
4429 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004430 char_code_at_generator_.GenerateSlow(masm, call_helper);
4431 char_from_code_generator_.GenerateSlow(masm, call_helper);
4432}
4433
4434
4435void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004436 Label call_runtime, call_builtin;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004437 Builtins::JavaScript builtin_id = Builtins::ADD;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004438
4439 // Load the two arguments.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004440 __ movq(rax, Operand(rsp, 2 * kPointerSize)); // First argument (left).
4441 __ movq(rdx, Operand(rsp, 1 * kPointerSize)); // Second argument (right).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004442
4443 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004444 if (flags_ == NO_STRING_ADD_FLAGS) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004445 __ JumpIfSmi(rax, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004446 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, r8);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004447 __ j(above_equal, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004448
4449 // First argument is a a string, test second.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004450 __ JumpIfSmi(rdx, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004451 __ CmpObjectType(rdx, FIRST_NONSTRING_TYPE, r9);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004452 __ j(above_equal, &call_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004453 } else {
4454 // Here at least one of the arguments is definitely a string.
4455 // We convert the one that is not known to be a string.
4456 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
4457 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
4458 GenerateConvertArgument(masm, 2 * kPointerSize, rax, rbx, rcx, rdi,
4459 &call_builtin);
4460 builtin_id = Builtins::STRING_ADD_RIGHT;
4461 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
4462 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
4463 GenerateConvertArgument(masm, 1 * kPointerSize, rdx, rbx, rcx, rdi,
4464 &call_builtin);
4465 builtin_id = Builtins::STRING_ADD_LEFT;
4466 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004467 }
4468
4469 // Both arguments are strings.
4470 // rax: first string
4471 // rdx: second string
4472 // Check if either of the strings are empty. In that case return the other.
Ben Murdoch257744e2011-11-30 15:57:28 +00004473 Label second_not_zero_length, both_not_zero_length;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004474 __ movq(rcx, FieldOperand(rdx, String::kLengthOffset));
4475 __ SmiTest(rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004476 __ j(not_zero, &second_not_zero_length, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004477 // Second string is empty, result is first string which is already in rax.
Steve Block44f0eee2011-05-26 01:26:41 +01004478 Counters* counters = masm->isolate()->counters();
4479 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004480 __ ret(2 * kPointerSize);
4481 __ bind(&second_not_zero_length);
4482 __ movq(rbx, FieldOperand(rax, String::kLengthOffset));
4483 __ SmiTest(rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004484 __ j(not_zero, &both_not_zero_length, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004485 // First string is empty, result is second string which is in rdx.
4486 __ movq(rax, rdx);
Steve Block44f0eee2011-05-26 01:26:41 +01004487 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004488 __ ret(2 * kPointerSize);
4489
4490 // Both strings are non-empty.
4491 // rax: first string
4492 // rbx: length of first string
4493 // rcx: length of second string
4494 // rdx: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004495 // r8: map of first string (if flags_ == NO_STRING_ADD_FLAGS)
4496 // r9: map of second string (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004497 Label string_add_flat_result, longer_than_two;
4498 __ bind(&both_not_zero_length);
4499
4500 // If arguments where known to be strings, maps are not loaded to r8 and r9
4501 // by the code above.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004502 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004503 __ movq(r8, FieldOperand(rax, HeapObject::kMapOffset));
4504 __ movq(r9, FieldOperand(rdx, HeapObject::kMapOffset));
4505 }
4506 // Get the instance types of the two strings as they will be needed soon.
4507 __ movzxbl(r8, FieldOperand(r8, Map::kInstanceTypeOffset));
4508 __ movzxbl(r9, FieldOperand(r9, Map::kInstanceTypeOffset));
4509
4510 // Look at the length of the result of adding the two strings.
4511 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue / 2);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004512 __ SmiAdd(rbx, rbx, rcx);
Steve Block44f0eee2011-05-26 01:26:41 +01004513 // Use the symbol table when adding two one character strings, as it
4514 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004515 __ SmiCompare(rbx, Smi::FromInt(2));
4516 __ j(not_equal, &longer_than_two);
4517
Ben Murdochc7cc0282012-03-05 14:35:55 +00004518 // Check that both strings are non-external ASCII strings.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004519 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r8, r9, rbx, rcx,
Ben Murdochc7cc0282012-03-05 14:35:55 +00004520 &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004521
4522 // Get the two characters forming the sub string.
4523 __ movzxbq(rbx, FieldOperand(rax, SeqAsciiString::kHeaderSize));
4524 __ movzxbq(rcx, FieldOperand(rdx, SeqAsciiString::kHeaderSize));
4525
4526 // Try to lookup two character string in symbol table. If it is not found
4527 // just allocate a new one.
4528 Label make_two_character_string, make_flat_ascii_string;
4529 StringHelper::GenerateTwoCharacterSymbolTableProbe(
Steve Block44f0eee2011-05-26 01:26:41 +01004530 masm, rbx, rcx, r14, r11, rdi, r15, &make_two_character_string);
4531 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004532 __ ret(2 * kPointerSize);
4533
4534 __ bind(&make_two_character_string);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004535 __ Set(rdi, 2);
4536 __ AllocateAsciiString(rax, rdi, r8, r9, r11, &call_runtime);
4537 // rbx - first byte: first character
4538 // rbx - second byte: *maybe* second character
4539 // Make sure that the second byte of rbx contains the second character.
4540 __ movzxbq(rcx, FieldOperand(rdx, SeqAsciiString::kHeaderSize));
4541 __ shll(rcx, Immediate(kBitsPerByte));
4542 __ orl(rbx, rcx);
4543 // Write both characters to the new string.
4544 __ movw(FieldOperand(rax, SeqAsciiString::kHeaderSize), rbx);
4545 __ IncrementCounter(counters->string_add_native(), 1);
4546 __ ret(2 * kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004547
4548 __ bind(&longer_than_two);
4549 // Check if resulting string will be flat.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004550 __ SmiCompare(rbx, Smi::FromInt(ConsString::kMinLength));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004551 __ j(below, &string_add_flat_result);
4552 // Handle exceptionally long strings in the runtime system.
4553 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
4554 __ SmiCompare(rbx, Smi::FromInt(String::kMaxLength));
Ben Murdochc7cc0282012-03-05 14:35:55 +00004555 __ j(above, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004556
4557 // If result is not supposed to be flat, allocate a cons string object. If
Ben Murdochc7cc0282012-03-05 14:35:55 +00004558 // both strings are ASCII the result is an ASCII cons string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004559 // rax: first string
4560 // rbx: length of resulting flat string
4561 // rdx: second string
4562 // r8: instance type of first string
4563 // r9: instance type of second string
4564 Label non_ascii, allocated, ascii_data;
4565 __ movl(rcx, r8);
4566 __ and_(rcx, r9);
Ben Murdoch589d6972011-11-30 16:04:58 +00004567 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
4568 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
4569 __ testl(rcx, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004570 __ j(zero, &non_ascii);
4571 __ bind(&ascii_data);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004572 // Allocate an ASCII cons string.
4573 __ AllocateAsciiConsString(rcx, rdi, no_reg, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004574 __ bind(&allocated);
4575 // Fill the fields of the cons string.
4576 __ movq(FieldOperand(rcx, ConsString::kLengthOffset), rbx);
4577 __ movq(FieldOperand(rcx, ConsString::kHashFieldOffset),
4578 Immediate(String::kEmptyHashField));
4579 __ movq(FieldOperand(rcx, ConsString::kFirstOffset), rax);
4580 __ movq(FieldOperand(rcx, ConsString::kSecondOffset), rdx);
4581 __ movq(rax, rcx);
Steve Block44f0eee2011-05-26 01:26:41 +01004582 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004583 __ ret(2 * kPointerSize);
4584 __ bind(&non_ascii);
4585 // At least one of the strings is two-byte. Check whether it happens
Ben Murdochc7cc0282012-03-05 14:35:55 +00004586 // to contain only ASCII characters.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004587 // rcx: first instance type AND second instance type.
4588 // r8: first instance type.
4589 // r9: second instance type.
4590 __ testb(rcx, Immediate(kAsciiDataHintMask));
4591 __ j(not_zero, &ascii_data);
4592 __ xor_(r8, r9);
4593 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4594 __ andb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
4595 __ cmpb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
4596 __ j(equal, &ascii_data);
4597 // Allocate a two byte cons string.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004598 __ AllocateTwoByteConsString(rcx, rdi, no_reg, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004599 __ jmp(&allocated);
4600
Ben Murdochc7cc0282012-03-05 14:35:55 +00004601 // We cannot encounter sliced strings or cons strings here since:
4602 STATIC_ASSERT(SlicedString::kMinLength >= ConsString::kMinLength);
4603 // Handle creating a flat result from either external or sequential strings.
4604 // Locate the first characters' locations.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004605 // rax: first string
4606 // rbx: length of resulting flat string as smi
4607 // rdx: second string
4608 // r8: instance type of first string
4609 // r9: instance type of first string
Ben Murdochc7cc0282012-03-05 14:35:55 +00004610 Label first_prepared, second_prepared;
4611 Label first_is_sequential, second_is_sequential;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004612 __ bind(&string_add_flat_result);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004613
4614 __ SmiToInteger32(r14, FieldOperand(rax, SeqString::kLengthOffset));
4615 // r14: length of first string
4616 STATIC_ASSERT(kSeqStringTag == 0);
4617 __ testb(r8, Immediate(kStringRepresentationMask));
4618 __ j(zero, &first_is_sequential, Label::kNear);
4619 // Rule out short external string and load string resource.
4620 STATIC_ASSERT(kShortExternalStringTag != 0);
4621 __ testb(r8, Immediate(kShortExternalStringMask));
4622 __ j(not_zero, &call_runtime);
4623 __ movq(rcx, FieldOperand(rax, ExternalString::kResourceDataOffset));
4624 __ jmp(&first_prepared, Label::kNear);
4625 __ bind(&first_is_sequential);
4626 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
4627 __ lea(rcx, FieldOperand(rax, SeqAsciiString::kHeaderSize));
4628 __ bind(&first_prepared);
4629
4630 // Check whether both strings have same encoding.
4631 __ xorl(r8, r9);
4632 __ testb(r8, Immediate(kStringEncodingMask));
4633 __ j(not_zero, &call_runtime);
4634
4635 __ SmiToInteger32(r15, FieldOperand(rdx, SeqString::kLengthOffset));
4636 // r15: length of second string
4637 STATIC_ASSERT(kSeqStringTag == 0);
4638 __ testb(r9, Immediate(kStringRepresentationMask));
4639 __ j(zero, &second_is_sequential, Label::kNear);
4640 // Rule out short external string and load string resource.
4641 STATIC_ASSERT(kShortExternalStringTag != 0);
4642 __ testb(r9, Immediate(kShortExternalStringMask));
4643 __ j(not_zero, &call_runtime);
4644 __ movq(rdx, FieldOperand(rdx, ExternalString::kResourceDataOffset));
4645 __ jmp(&second_prepared, Label::kNear);
4646 __ bind(&second_is_sequential);
4647 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
4648 __ lea(rdx, FieldOperand(rdx, SeqAsciiString::kHeaderSize));
4649 __ bind(&second_prepared);
4650
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004651 Label non_ascii_string_add_flat_result;
Ben Murdochc7cc0282012-03-05 14:35:55 +00004652 // r9: instance type of second string
4653 // First string and second string have the same encoding.
4654 STATIC_ASSERT(kTwoByteStringTag == 0);
4655 __ SmiToInteger32(rbx, rbx);
4656 __ testb(r9, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004657 __ j(zero, &non_ascii_string_add_flat_result);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004658
4659 __ bind(&make_flat_ascii_string);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004660 // Both strings are ASCII strings. As they are short they are both flat.
4661 __ AllocateAsciiString(rax, rbx, rdi, r8, r9, &call_runtime);
4662 // rax: result string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004663 // Locate first character of result.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004664 __ lea(rbx, FieldOperand(rax, SeqAsciiString::kHeaderSize));
4665 // rcx: first char of first string
4666 // rbx: first character of result
4667 // r14: length of first string
4668 StringHelper::GenerateCopyCharacters(masm, rbx, rcx, r14, true);
4669 // rbx: next character of result
4670 // rdx: first char of second string
4671 // r15: length of second string
4672 StringHelper::GenerateCopyCharacters(masm, rbx, rdx, r15, true);
Steve Block44f0eee2011-05-26 01:26:41 +01004673 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004674 __ ret(2 * kPointerSize);
4675
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004676 __ bind(&non_ascii_string_add_flat_result);
Ben Murdochc7cc0282012-03-05 14:35:55 +00004677 // Both strings are ASCII strings. As they are short they are both flat.
4678 __ AllocateTwoByteString(rax, rbx, rdi, r8, r9, &call_runtime);
4679 // rax: result string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004680 // Locate first character of result.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004681 __ lea(rbx, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
4682 // rcx: first char of first string
4683 // rbx: first character of result
4684 // r14: length of first string
4685 StringHelper::GenerateCopyCharacters(masm, rbx, rcx, r14, false);
4686 // rbx: next character of result
4687 // rdx: first char of second string
4688 // r15: length of second string
4689 StringHelper::GenerateCopyCharacters(masm, rbx, rdx, r15, false);
Steve Block44f0eee2011-05-26 01:26:41 +01004690 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004691 __ ret(2 * kPointerSize);
4692
4693 // Just jump to runtime to add the two strings.
Ben Murdochc7cc0282012-03-05 14:35:55 +00004694 __ bind(&call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004695 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004696
4697 if (call_builtin.is_linked()) {
4698 __ bind(&call_builtin);
4699 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
4700 }
4701}
4702
4703
4704void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
4705 int stack_offset,
4706 Register arg,
4707 Register scratch1,
4708 Register scratch2,
4709 Register scratch3,
4710 Label* slow) {
4711 // First check if the argument is already a string.
4712 Label not_string, done;
4713 __ JumpIfSmi(arg, &not_string);
4714 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
4715 __ j(below, &done);
4716
4717 // Check the number to string cache.
4718 Label not_cached;
4719 __ bind(&not_string);
4720 // Puts the cached result into scratch1.
4721 NumberToStringStub::GenerateLookupNumberStringCache(masm,
4722 arg,
4723 scratch1,
4724 scratch2,
4725 scratch3,
4726 false,
4727 &not_cached);
4728 __ movq(arg, scratch1);
4729 __ movq(Operand(rsp, stack_offset), arg);
4730 __ jmp(&done);
4731
4732 // Check if the argument is a safe string wrapper.
4733 __ bind(&not_cached);
4734 __ JumpIfSmi(arg, slow);
4735 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
4736 __ j(not_equal, slow);
4737 __ testb(FieldOperand(scratch1, Map::kBitField2Offset),
4738 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
4739 __ j(zero, slow);
4740 __ movq(arg, FieldOperand(arg, JSValue::kValueOffset));
4741 __ movq(Operand(rsp, stack_offset), arg);
4742
4743 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004744}
4745
4746
4747void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
4748 Register dest,
4749 Register src,
4750 Register count,
4751 bool ascii) {
4752 Label loop;
4753 __ bind(&loop);
4754 // This loop just copies one character at a time, as it is only used for very
4755 // short strings.
4756 if (ascii) {
4757 __ movb(kScratchRegister, Operand(src, 0));
4758 __ movb(Operand(dest, 0), kScratchRegister);
4759 __ incq(src);
4760 __ incq(dest);
4761 } else {
4762 __ movzxwl(kScratchRegister, Operand(src, 0));
4763 __ movw(Operand(dest, 0), kScratchRegister);
4764 __ addq(src, Immediate(2));
4765 __ addq(dest, Immediate(2));
4766 }
4767 __ decl(count);
4768 __ j(not_zero, &loop);
4769}
4770
4771
4772void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
4773 Register dest,
4774 Register src,
4775 Register count,
4776 bool ascii) {
4777 // Copy characters using rep movs of doublewords. Align destination on 4 byte
4778 // boundary before starting rep movs. Copy remaining characters after running
4779 // rep movs.
4780 // Count is positive int32, dest and src are character pointers.
4781 ASSERT(dest.is(rdi)); // rep movs destination
4782 ASSERT(src.is(rsi)); // rep movs source
4783 ASSERT(count.is(rcx)); // rep movs count
4784
4785 // Nothing to do for zero characters.
Ben Murdoch257744e2011-11-30 15:57:28 +00004786 Label done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004787 __ testl(count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00004788 __ j(zero, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004789
4790 // Make count the number of bytes to copy.
4791 if (!ascii) {
4792 STATIC_ASSERT(2 == sizeof(uc16));
4793 __ addl(count, count);
4794 }
4795
4796 // Don't enter the rep movs if there are less than 4 bytes to copy.
Ben Murdoch257744e2011-11-30 15:57:28 +00004797 Label last_bytes;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004798 __ testl(count, Immediate(~7));
Ben Murdoch257744e2011-11-30 15:57:28 +00004799 __ j(zero, &last_bytes, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004800
4801 // Copy from edi to esi using rep movs instruction.
4802 __ movl(kScratchRegister, count);
4803 __ shr(count, Immediate(3)); // Number of doublewords to copy.
4804 __ repmovsq();
4805
4806 // Find number of bytes left.
4807 __ movl(count, kScratchRegister);
4808 __ and_(count, Immediate(7));
4809
4810 // Check if there are more bytes to copy.
4811 __ bind(&last_bytes);
4812 __ testl(count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00004813 __ j(zero, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004814
4815 // Copy remaining characters.
4816 Label loop;
4817 __ bind(&loop);
4818 __ movb(kScratchRegister, Operand(src, 0));
4819 __ movb(Operand(dest, 0), kScratchRegister);
4820 __ incq(src);
4821 __ incq(dest);
4822 __ decl(count);
4823 __ j(not_zero, &loop);
4824
4825 __ bind(&done);
4826}
4827
4828void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4829 Register c1,
4830 Register c2,
4831 Register scratch1,
4832 Register scratch2,
4833 Register scratch3,
4834 Register scratch4,
4835 Label* not_found) {
4836 // Register scratch3 is the general scratch register in this function.
4837 Register scratch = scratch3;
4838
4839 // Make sure that both characters are not digits as such strings has a
4840 // different hash algorithm. Don't try to look for these in the symbol table.
Ben Murdoch257744e2011-11-30 15:57:28 +00004841 Label not_array_index;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004842 __ leal(scratch, Operand(c1, -'0'));
4843 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
Ben Murdoch257744e2011-11-30 15:57:28 +00004844 __ j(above, &not_array_index, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004845 __ leal(scratch, Operand(c2, -'0'));
4846 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
4847 __ j(below_equal, not_found);
4848
4849 __ bind(&not_array_index);
4850 // Calculate the two character string hash.
4851 Register hash = scratch1;
4852 GenerateHashInit(masm, hash, c1, scratch);
4853 GenerateHashAddCharacter(masm, hash, c2, scratch);
4854 GenerateHashGetHash(masm, hash, scratch);
4855
4856 // Collect the two characters in a register.
4857 Register chars = c1;
4858 __ shl(c2, Immediate(kBitsPerByte));
4859 __ orl(chars, c2);
4860
4861 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4862 // hash: hash of two character string.
4863
4864 // Load the symbol table.
4865 Register symbol_table = c2;
4866 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
4867
4868 // Calculate capacity mask from the symbol table capacity.
4869 Register mask = scratch2;
4870 __ SmiToInteger32(mask,
4871 FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
4872 __ decl(mask);
4873
Steve Block44f0eee2011-05-26 01:26:41 +01004874 Register map = scratch4;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004875
4876 // Registers
4877 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4878 // hash: hash of two character string (32-bit int)
4879 // symbol_table: symbol table
4880 // mask: capacity mask (32-bit int)
Steve Block44f0eee2011-05-26 01:26:41 +01004881 // map: -
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004882 // scratch: -
4883
4884 // Perform a number of probes in the symbol table.
4885 static const int kProbes = 4;
4886 Label found_in_symbol_table;
4887 Label next_probe[kProbes];
Ben Murdoch692be652012-01-10 18:47:50 +00004888 Register candidate = scratch; // Scratch register contains candidate.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004889 for (int i = 0; i < kProbes; i++) {
4890 // Calculate entry in symbol table.
4891 __ movl(scratch, hash);
4892 if (i > 0) {
4893 __ addl(scratch, Immediate(SymbolTable::GetProbeOffset(i)));
4894 }
4895 __ andl(scratch, mask);
4896
Steve Block44f0eee2011-05-26 01:26:41 +01004897 // Load the entry from the symbol table.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004898 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4899 __ movq(candidate,
4900 FieldOperand(symbol_table,
4901 scratch,
4902 times_pointer_size,
4903 SymbolTable::kElementsStartOffset));
4904
4905 // If entry is undefined no string with this hash can be found.
Ben Murdoch257744e2011-11-30 15:57:28 +00004906 Label is_string;
Steve Block44f0eee2011-05-26 01:26:41 +01004907 __ CmpObjectType(candidate, ODDBALL_TYPE, map);
Ben Murdoch257744e2011-11-30 15:57:28 +00004908 __ j(not_equal, &is_string, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01004909
4910 __ CompareRoot(candidate, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004911 __ j(equal, not_found);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004912 // Must be the hole (deleted entry).
4913 if (FLAG_debug_code) {
4914 __ LoadRoot(kScratchRegister, Heap::kTheHoleValueRootIndex);
4915 __ cmpq(kScratchRegister, candidate);
4916 __ Assert(equal, "oddball in symbol table is not undefined or the hole");
4917 }
Steve Block44f0eee2011-05-26 01:26:41 +01004918 __ jmp(&next_probe[i]);
4919
4920 __ bind(&is_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004921
4922 // If length is not 2 the string is not a candidate.
4923 __ SmiCompare(FieldOperand(candidate, String::kLengthOffset),
4924 Smi::FromInt(2));
4925 __ j(not_equal, &next_probe[i]);
4926
4927 // We use kScratchRegister as a temporary register in assumption that
4928 // JumpIfInstanceTypeIsNotSequentialAscii does not use it implicitly
4929 Register temp = kScratchRegister;
4930
Ben Murdochc7cc0282012-03-05 14:35:55 +00004931 // Check that the candidate is a non-external ASCII string.
Steve Block44f0eee2011-05-26 01:26:41 +01004932 __ movzxbl(temp, FieldOperand(map, Map::kInstanceTypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004933 __ JumpIfInstanceTypeIsNotSequentialAscii(
4934 temp, temp, &next_probe[i]);
4935
4936 // Check if the two characters match.
4937 __ movl(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
4938 __ andl(temp, Immediate(0x0000ffff));
4939 __ cmpl(chars, temp);
4940 __ j(equal, &found_in_symbol_table);
4941 __ bind(&next_probe[i]);
4942 }
4943
4944 // No matching 2 character string found by probing.
4945 __ jmp(not_found);
4946
4947 // Scratch register contains result when we fall through to here.
Ben Murdoch692be652012-01-10 18:47:50 +00004948 Register result = candidate;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004949 __ bind(&found_in_symbol_table);
4950 if (!result.is(rax)) {
4951 __ movq(rax, result);
4952 }
4953}
4954
4955
4956void StringHelper::GenerateHashInit(MacroAssembler* masm,
4957 Register hash,
4958 Register character,
4959 Register scratch) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00004960 // hash = (seed + character) + ((seed + character) << 10);
4961 __ LoadRoot(scratch, Heap::kHashSeedRootIndex);
4962 __ SmiToInteger32(scratch, scratch);
4963 __ addl(scratch, character);
4964 __ movl(hash, scratch);
4965 __ shll(scratch, Immediate(10));
4966 __ addl(hash, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004967 // hash ^= hash >> 6;
4968 __ movl(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00004969 __ shrl(scratch, Immediate(6));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004970 __ xorl(hash, scratch);
4971}
4972
4973
4974void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4975 Register hash,
4976 Register character,
4977 Register scratch) {
4978 // hash += character;
4979 __ addl(hash, character);
4980 // hash += hash << 10;
4981 __ movl(scratch, hash);
4982 __ shll(scratch, Immediate(10));
4983 __ addl(hash, scratch);
4984 // hash ^= hash >> 6;
4985 __ movl(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00004986 __ shrl(scratch, Immediate(6));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004987 __ xorl(hash, scratch);
4988}
4989
4990
4991void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4992 Register hash,
4993 Register scratch) {
4994 // hash += hash << 3;
4995 __ leal(hash, Operand(hash, hash, times_8, 0));
4996 // hash ^= hash >> 11;
4997 __ movl(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00004998 __ shrl(scratch, Immediate(11));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004999 __ xorl(hash, scratch);
5000 // hash += hash << 15;
5001 __ movl(scratch, hash);
5002 __ shll(scratch, Immediate(15));
5003 __ addl(hash, scratch);
5004
Ben Murdochc7cc0282012-03-05 14:35:55 +00005005 __ andl(hash, Immediate(String::kHashBitMask));
Ben Murdoch692be652012-01-10 18:47:50 +00005006
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005007 // if (hash == 0) hash = 27;
5008 Label hash_not_zero;
5009 __ j(not_zero, &hash_not_zero);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005010 __ Set(hash, StringHasher::kZeroHash);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005011 __ bind(&hash_not_zero);
5012}
5013
5014void SubStringStub::Generate(MacroAssembler* masm) {
5015 Label runtime;
5016
5017 // Stack frame on entry.
5018 // rsp[0]: return address
5019 // rsp[8]: to
5020 // rsp[16]: from
5021 // rsp[24]: string
5022
5023 const int kToOffset = 1 * kPointerSize;
5024 const int kFromOffset = kToOffset + kPointerSize;
5025 const int kStringOffset = kFromOffset + kPointerSize;
5026 const int kArgumentsSize = (kStringOffset + kPointerSize) - kToOffset;
5027
5028 // Make sure first argument is a string.
5029 __ movq(rax, Operand(rsp, kStringOffset));
5030 STATIC_ASSERT(kSmiTag == 0);
5031 __ testl(rax, Immediate(kSmiTagMask));
5032 __ j(zero, &runtime);
5033 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
5034 __ j(NegateCondition(is_string), &runtime);
5035
5036 // rax: string
5037 // rbx: instance type
5038 // Calculate length of sub string using the smi values.
5039 Label result_longer_than_two;
5040 __ movq(rcx, Operand(rsp, kToOffset));
5041 __ movq(rdx, Operand(rsp, kFromOffset));
Ben Murdochf87a2032010-10-22 12:50:53 +01005042 __ JumpUnlessBothNonNegativeSmi(rcx, rdx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005043
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005044 __ SmiSub(rcx, rcx, rdx); // Overflow doesn't happen.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005045 __ cmpq(FieldOperand(rax, String::kLengthOffset), rcx);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005046 Label not_original_string;
5047 __ j(not_equal, &not_original_string, Label::kNear);
5048 Counters* counters = masm->isolate()->counters();
5049 __ IncrementCounter(counters->sub_string_native(), 1);
5050 __ ret(kArgumentsSize);
5051 __ bind(&not_original_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005052 // Special handling of sub-strings of length 1 and 2. One character strings
5053 // are handled in the runtime system (looked up in the single character
5054 // cache). Two character strings are looked for in the symbol cache.
5055 __ SmiToInteger32(rcx, rcx);
5056 __ cmpl(rcx, Immediate(2));
5057 __ j(greater, &result_longer_than_two);
5058 __ j(less, &runtime);
5059
5060 // Sub string of length 2 requested.
5061 // rax: string
5062 // rbx: instance type
5063 // rcx: sub string length (value is 2)
5064 // rdx: from index (smi)
5065 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &runtime);
5066
5067 // Get the two characters forming the sub string.
5068 __ SmiToInteger32(rdx, rdx); // From index is no longer smi.
5069 __ movzxbq(rbx, FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize));
Ben Murdochc7cc0282012-03-05 14:35:55 +00005070 __ movzxbq(rdi,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005071 FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize + 1));
5072
5073 // Try to lookup two character string in symbol table.
5074 Label make_two_character_string;
5075 StringHelper::GenerateTwoCharacterSymbolTableProbe(
Ben Murdochc7cc0282012-03-05 14:35:55 +00005076 masm, rbx, rdi, r9, r11, r14, r15, &make_two_character_string);
5077 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005078 __ ret(3 * kPointerSize);
5079
5080 __ bind(&make_two_character_string);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005081 // Set up registers for allocating the two character string.
5082 __ movzxwq(rbx, FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize));
5083 __ AllocateAsciiString(rax, rcx, r11, r14, r15, &runtime);
5084 __ movw(FieldOperand(rax, SeqAsciiString::kHeaderSize), rbx);
5085 __ IncrementCounter(counters->sub_string_native(), 1);
5086 __ ret(3 * kPointerSize);
5087
5088 __ bind(&result_longer_than_two);
5089 // rax: string
5090 // rbx: instance type
5091 // rcx: sub string length
5092 // rdx: from index (smi)
5093 // Deal with different string types: update the index if necessary
5094 // and put the underlying string into edi.
5095 Label underlying_unpacked, sliced_string, seq_or_external_string;
5096 // If the string is not indirect, it can only be sequential or external.
5097 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
5098 STATIC_ASSERT(kIsIndirectStringMask != 0);
5099 __ testb(rbx, Immediate(kIsIndirectStringMask));
5100 __ j(zero, &seq_or_external_string, Label::kNear);
5101
5102 __ testb(rbx, Immediate(kSlicedNotConsMask));
5103 __ j(not_zero, &sliced_string, Label::kNear);
5104 // Cons string. Check whether it is flat, then fetch first part.
5105 // Flat cons strings have an empty second part.
5106 __ CompareRoot(FieldOperand(rax, ConsString::kSecondOffset),
5107 Heap::kEmptyStringRootIndex);
5108 __ j(not_equal, &runtime);
5109 __ movq(rdi, FieldOperand(rax, ConsString::kFirstOffset));
5110 // Update instance type.
5111 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005112 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +00005113 __ jmp(&underlying_unpacked, Label::kNear);
5114
5115 __ bind(&sliced_string);
5116 // Sliced string. Fetch parent and correct start index by offset.
5117 __ addq(rdx, FieldOperand(rax, SlicedString::kOffsetOffset));
5118 __ movq(rdi, FieldOperand(rax, SlicedString::kParentOffset));
5119 // Update instance type.
5120 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
5121 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
5122 __ jmp(&underlying_unpacked, Label::kNear);
5123
5124 __ bind(&seq_or_external_string);
5125 // Sequential or external string. Just move string to the correct register.
5126 __ movq(rdi, rax);
5127
5128 __ bind(&underlying_unpacked);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005129
Ben Murdoch589d6972011-11-30 16:04:58 +00005130 if (FLAG_string_slices) {
5131 Label copy_routine;
Ben Murdochc7cc0282012-03-05 14:35:55 +00005132 // rdi: underlying subject string
5133 // rbx: instance type of underlying subject string
5134 // rdx: adjusted start index (smi)
5135 // rcx: length
Ben Murdoch589d6972011-11-30 16:04:58 +00005136 // If coming from the make_two_character_string path, the string
5137 // is too short to be sliced anyways.
Ben Murdoch589d6972011-11-30 16:04:58 +00005138 __ cmpq(rcx, Immediate(SlicedString::kMinLength));
5139 // Short slice. Copy instead of slicing.
5140 __ j(less, &copy_routine);
Ben Murdoch589d6972011-11-30 16:04:58 +00005141 // Allocate new sliced string. At this point we do not reload the instance
5142 // type including the string encoding because we simply rely on the info
5143 // provided by the original string. It does not matter if the original
5144 // string's encoding is wrong because we always have to recheck encoding of
5145 // the newly created string's parent anyways due to externalized strings.
5146 Label two_byte_slice, set_slice_header;
5147 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
5148 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
5149 __ testb(rbx, Immediate(kStringEncodingMask));
5150 __ j(zero, &two_byte_slice, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005151 __ AllocateAsciiSlicedString(rax, rbx, r14, &runtime);
Ben Murdoch589d6972011-11-30 16:04:58 +00005152 __ jmp(&set_slice_header, Label::kNear);
5153 __ bind(&two_byte_slice);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005154 __ AllocateTwoByteSlicedString(rax, rbx, r14, &runtime);
Ben Murdoch589d6972011-11-30 16:04:58 +00005155 __ bind(&set_slice_header);
5156 __ movq(FieldOperand(rax, SlicedString::kOffsetOffset), rdx);
5157 __ Integer32ToSmi(rcx, rcx);
5158 __ movq(FieldOperand(rax, SlicedString::kLengthOffset), rcx);
5159 __ movq(FieldOperand(rax, SlicedString::kParentOffset), rdi);
5160 __ movq(FieldOperand(rax, SlicedString::kHashFieldOffset),
5161 Immediate(String::kEmptyHashField));
Ben Murdochc7cc0282012-03-05 14:35:55 +00005162 __ IncrementCounter(counters->sub_string_native(), 1);
5163 __ ret(kArgumentsSize);
Ben Murdoch589d6972011-11-30 16:04:58 +00005164
5165 __ bind(&copy_routine);
Ben Murdoch589d6972011-11-30 16:04:58 +00005166 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005167
Ben Murdochc7cc0282012-03-05 14:35:55 +00005168 // rdi: underlying subject string
5169 // rbx: instance type of underlying subject string
5170 // rdx: adjusted start index (smi)
5171 // rcx: length
5172 // The subject string can only be external or sequential string of either
5173 // encoding at this point.
5174 Label two_byte_sequential, sequential_string;
5175 STATIC_ASSERT(kExternalStringTag != 0);
5176 STATIC_ASSERT(kSeqStringTag == 0);
5177 __ testb(rbx, Immediate(kExternalStringTag));
5178 __ j(zero, &sequential_string);
5179
5180 // Handle external string.
5181 // Rule out short external strings.
5182 STATIC_CHECK(kShortExternalStringTag != 0);
5183 __ testb(rbx, Immediate(kShortExternalStringMask));
5184 __ j(not_zero, &runtime);
5185 __ movq(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
5186 // Move the pointer so that offset-wise, it looks like a sequential string.
5187 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize);
5188 __ subq(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5189
5190 __ bind(&sequential_string);
5191 STATIC_ASSERT((kAsciiStringTag & kStringEncodingMask) != 0);
5192 __ testb(rbx, Immediate(kStringEncodingMask));
5193 __ j(zero, &two_byte_sequential);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005194
5195 // Allocate the result.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005196 __ AllocateAsciiString(rax, rcx, r11, r14, r15, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005197
5198 // rax: result string
5199 // rcx: result string length
Ben Murdochc7cc0282012-03-05 14:35:55 +00005200 __ movq(r14, rsi); // esi used by following code.
5201 { // Locate character of sub string start.
5202 SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_1);
5203 __ lea(rsi, Operand(rdi, smi_as_index.reg, smi_as_index.scale,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005204 SeqAsciiString::kHeaderSize - kHeapObjectTag));
5205 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00005206 // Locate first character of result.
5207 __ lea(rdi, FieldOperand(rax, SeqAsciiString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005208
5209 // rax: result string
5210 // rcx: result length
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005211 // rdi: first character of result
5212 // rsi: character of sub string start
Ben Murdochc7cc0282012-03-05 14:35:55 +00005213 // r14: original value of rsi
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005214 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, true);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005215 __ movq(rsi, r14); // Restore rsi.
Steve Block44f0eee2011-05-26 01:26:41 +01005216 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005217 __ ret(kArgumentsSize);
5218
Ben Murdochc7cc0282012-03-05 14:35:55 +00005219 __ bind(&two_byte_sequential);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005220 // Allocate the result.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005221 __ AllocateTwoByteString(rax, rcx, r11, r14, r15, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005222
5223 // rax: result string
5224 // rcx: result string length
Ben Murdochc7cc0282012-03-05 14:35:55 +00005225 __ movq(r14, rsi); // esi used by following code.
5226 { // Locate character of sub string start.
5227 SmiIndex smi_as_index = masm->SmiToIndex(rdx, rdx, times_2);
5228 __ lea(rsi, Operand(rdi, smi_as_index.reg, smi_as_index.scale,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005229 SeqAsciiString::kHeaderSize - kHeapObjectTag));
5230 }
Ben Murdochc7cc0282012-03-05 14:35:55 +00005231 // Locate first character of result.
5232 __ lea(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005233
5234 // rax: result string
5235 // rcx: result length
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005236 // rdi: first character of result
5237 // rsi: character of sub string start
Ben Murdochc7cc0282012-03-05 14:35:55 +00005238 // r14: original value of rsi
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005239 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, false);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005240 __ movq(rsi, r14); // Restore esi.
Steve Block44f0eee2011-05-26 01:26:41 +01005241 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005242 __ ret(kArgumentsSize);
5243
5244 // Just jump to runtime to create the sub string.
5245 __ bind(&runtime);
5246 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5247}
5248
5249
Ben Murdoch257744e2011-11-30 15:57:28 +00005250void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5251 Register left,
5252 Register right,
5253 Register scratch1,
5254 Register scratch2) {
5255 Register length = scratch1;
5256
5257 // Compare lengths.
5258 Label check_zero_length;
5259 __ movq(length, FieldOperand(left, String::kLengthOffset));
5260 __ SmiCompare(length, FieldOperand(right, String::kLengthOffset));
5261 __ j(equal, &check_zero_length, Label::kNear);
5262 __ Move(rax, Smi::FromInt(NOT_EQUAL));
5263 __ ret(0);
5264
5265 // Check if the length is zero.
5266 Label compare_chars;
5267 __ bind(&check_zero_length);
5268 STATIC_ASSERT(kSmiTag == 0);
5269 __ SmiTest(length);
5270 __ j(not_zero, &compare_chars, Label::kNear);
5271 __ Move(rax, Smi::FromInt(EQUAL));
5272 __ ret(0);
5273
5274 // Compare characters.
5275 __ bind(&compare_chars);
5276 Label strings_not_equal;
5277 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
5278 &strings_not_equal, Label::kNear);
5279
5280 // Characters are equal.
5281 __ Move(rax, Smi::FromInt(EQUAL));
5282 __ ret(0);
5283
5284 // Characters are not equal.
5285 __ bind(&strings_not_equal);
5286 __ Move(rax, Smi::FromInt(NOT_EQUAL));
5287 __ ret(0);
5288}
5289
5290
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005291void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5292 Register left,
5293 Register right,
5294 Register scratch1,
5295 Register scratch2,
5296 Register scratch3,
5297 Register scratch4) {
5298 // Ensure that you can always subtract a string length from a non-negative
5299 // number (e.g. another length).
5300 STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
5301
5302 // Find minimum length and length difference.
5303 __ movq(scratch1, FieldOperand(left, String::kLengthOffset));
5304 __ movq(scratch4, scratch1);
5305 __ SmiSub(scratch4,
5306 scratch4,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005307 FieldOperand(right, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005308 // Register scratch4 now holds left.length - right.length.
5309 const Register length_difference = scratch4;
Ben Murdoch257744e2011-11-30 15:57:28 +00005310 Label left_shorter;
5311 __ j(less, &left_shorter, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005312 // The right string isn't longer that the left one.
5313 // Get the right string's length by subtracting the (non-negative) difference
5314 // from the left string's length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005315 __ SmiSub(scratch1, scratch1, length_difference);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005316 __ bind(&left_shorter);
5317 // Register scratch1 now holds Min(left.length, right.length).
5318 const Register min_length = scratch1;
5319
Ben Murdoch257744e2011-11-30 15:57:28 +00005320 Label compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005321 // If min-length is zero, go directly to comparing lengths.
5322 __ SmiTest(min_length);
Ben Murdoch257744e2011-11-30 15:57:28 +00005323 __ j(zero, &compare_lengths, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005324
Ben Murdoch257744e2011-11-30 15:57:28 +00005325 // Compare loop.
5326 Label result_not_equal;
5327 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
5328 &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005329
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005330 // Completed loop without finding different characters.
5331 // Compare lengths (precomputed).
5332 __ bind(&compare_lengths);
5333 __ SmiTest(length_difference);
Ben Murdoch257744e2011-11-30 15:57:28 +00005334 __ j(not_zero, &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005335
5336 // Result is EQUAL.
5337 __ Move(rax, Smi::FromInt(EQUAL));
5338 __ ret(0);
5339
Ben Murdoch257744e2011-11-30 15:57:28 +00005340 Label result_greater;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005341 __ bind(&result_not_equal);
5342 // Unequal comparison of left to right, either character or length.
Ben Murdoch257744e2011-11-30 15:57:28 +00005343 __ j(greater, &result_greater, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005344
5345 // Result is LESS.
5346 __ Move(rax, Smi::FromInt(LESS));
5347 __ ret(0);
5348
5349 // Result is GREATER.
5350 __ bind(&result_greater);
5351 __ Move(rax, Smi::FromInt(GREATER));
5352 __ ret(0);
5353}
5354
5355
Ben Murdoch257744e2011-11-30 15:57:28 +00005356void StringCompareStub::GenerateAsciiCharsCompareLoop(
5357 MacroAssembler* masm,
5358 Register left,
5359 Register right,
5360 Register length,
5361 Register scratch,
5362 Label* chars_not_equal,
5363 Label::Distance near_jump) {
5364 // Change index to run from -length to -1 by adding length to string
5365 // start. This means that loop ends when index reaches zero, which
5366 // doesn't need an additional compare.
5367 __ SmiToInteger32(length, length);
5368 __ lea(left,
5369 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5370 __ lea(right,
5371 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5372 __ neg(length);
5373 Register index = length; // index = -length;
5374
5375 // Compare loop.
5376 Label loop;
5377 __ bind(&loop);
5378 __ movb(scratch, Operand(left, index, times_1, 0));
5379 __ cmpb(scratch, Operand(right, index, times_1, 0));
5380 __ j(not_equal, chars_not_equal, near_jump);
5381 __ addq(index, Immediate(1));
5382 __ j(not_zero, &loop);
5383}
5384
5385
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005386void StringCompareStub::Generate(MacroAssembler* masm) {
5387 Label runtime;
5388
5389 // Stack frame on entry.
5390 // rsp[0]: return address
5391 // rsp[8]: right string
5392 // rsp[16]: left string
5393
5394 __ movq(rdx, Operand(rsp, 2 * kPointerSize)); // left
5395 __ movq(rax, Operand(rsp, 1 * kPointerSize)); // right
5396
5397 // Check for identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00005398 Label not_same;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005399 __ cmpq(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005400 __ j(not_equal, &not_same, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005401 __ Move(rax, Smi::FromInt(EQUAL));
Steve Block44f0eee2011-05-26 01:26:41 +01005402 Counters* counters = masm->isolate()->counters();
5403 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005404 __ ret(2 * kPointerSize);
5405
5406 __ bind(&not_same);
5407
5408 // Check that both are sequential ASCII strings.
5409 __ JumpIfNotBothSequentialAsciiStrings(rdx, rax, rcx, rbx, &runtime);
5410
Ben Murdochc7cc0282012-03-05 14:35:55 +00005411 // Inline comparison of ASCII strings.
Steve Block44f0eee2011-05-26 01:26:41 +01005412 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005413 // Drop arguments from the stack
5414 __ pop(rcx);
5415 __ addq(rsp, Immediate(2 * kPointerSize));
5416 __ push(rcx);
5417 GenerateCompareFlatAsciiStrings(masm, rdx, rax, rcx, rbx, rdi, r8);
5418
5419 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5420 // tagged as a small integer.
5421 __ bind(&runtime);
5422 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5423}
5424
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005425
Ben Murdochb0fe1622011-05-05 13:52:32 +01005426void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005427 ASSERT(state_ == CompareIC::SMIS);
Ben Murdoch257744e2011-11-30 15:57:28 +00005428 Label miss;
5429 __ JumpIfNotBothSmi(rdx, rax, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005430
5431 if (GetCondition() == equal) {
5432 // For equality we do not care about the sign of the result.
5433 __ subq(rax, rdx);
5434 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00005435 Label done;
Steve Block1e0659c2011-05-24 12:43:12 +01005436 __ subq(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005437 __ j(no_overflow, &done, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005438 // Correct sign of result in case of overflow.
5439 __ SmiNot(rdx, rdx);
5440 __ bind(&done);
5441 __ movq(rax, rdx);
5442 }
5443 __ ret(0);
5444
5445 __ bind(&miss);
5446 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005447}
5448
5449
5450void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005451 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5452
Ben Murdoch257744e2011-11-30 15:57:28 +00005453 Label generic_stub;
5454 Label unordered;
5455 Label miss;
Steve Block1e0659c2011-05-24 12:43:12 +01005456 Condition either_smi = masm->CheckEitherSmi(rax, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005457 __ j(either_smi, &generic_stub, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005458
5459 __ CmpObjectType(rax, HEAP_NUMBER_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005460 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005461 __ CmpObjectType(rdx, HEAP_NUMBER_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005462 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005463
5464 // Load left and right operand
5465 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
5466 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
5467
5468 // Compare operands
5469 __ ucomisd(xmm0, xmm1);
5470
5471 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch257744e2011-11-30 15:57:28 +00005472 __ j(parity_even, &unordered, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005473
5474 // Return a result of -1, 0, or 1, based on EFLAGS.
5475 // Performing mov, because xor would destroy the flag register.
5476 __ movl(rax, Immediate(0));
5477 __ movl(rcx, Immediate(0));
5478 __ setcc(above, rax); // Add one to zero if carry clear and not equal.
5479 __ sbbq(rax, rcx); // Subtract one if below (aka. carry set).
5480 __ ret(0);
5481
5482 __ bind(&unordered);
5483
5484 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5485 __ bind(&generic_stub);
5486 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5487
5488 __ bind(&miss);
5489 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005490}
5491
5492
Ben Murdoch257744e2011-11-30 15:57:28 +00005493void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5494 ASSERT(state_ == CompareIC::SYMBOLS);
5495 ASSERT(GetCondition() == equal);
5496
5497 // Registers containing left and right operands respectively.
5498 Register left = rdx;
5499 Register right = rax;
5500 Register tmp1 = rcx;
5501 Register tmp2 = rbx;
5502
5503 // Check that both operands are heap objects.
5504 Label miss;
5505 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
5506 __ j(cond, &miss, Label::kNear);
5507
5508 // Check that both operands are symbols.
5509 __ movq(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5510 __ movq(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5511 __ movzxbq(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5512 __ movzxbq(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5513 STATIC_ASSERT(kSymbolTag != 0);
5514 __ and_(tmp1, tmp2);
5515 __ testb(tmp1, Immediate(kIsSymbolMask));
5516 __ j(zero, &miss, Label::kNear);
5517
5518 // Symbols are compared by identity.
5519 Label done;
5520 __ cmpq(left, right);
5521 // Make sure rax is non-zero. At this point input operands are
5522 // guaranteed to be non-zero.
5523 ASSERT(right.is(rax));
5524 __ j(not_equal, &done, Label::kNear);
5525 STATIC_ASSERT(EQUAL == 0);
5526 STATIC_ASSERT(kSmiTag == 0);
5527 __ Move(rax, Smi::FromInt(EQUAL));
5528 __ bind(&done);
5529 __ ret(0);
5530
5531 __ bind(&miss);
5532 GenerateMiss(masm);
5533}
5534
5535
5536void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
5537 ASSERT(state_ == CompareIC::STRINGS);
5538 ASSERT(GetCondition() == equal);
5539 Label miss;
5540
5541 // Registers containing left and right operands respectively.
5542 Register left = rdx;
5543 Register right = rax;
5544 Register tmp1 = rcx;
5545 Register tmp2 = rbx;
5546 Register tmp3 = rdi;
5547
5548 // Check that both operands are heap objects.
5549 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
5550 __ j(cond, &miss);
5551
5552 // Check that both operands are strings. This leaves the instance
5553 // types loaded in tmp1 and tmp2.
5554 __ movq(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5555 __ movq(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5556 __ movzxbq(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5557 __ movzxbq(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5558 __ movq(tmp3, tmp1);
5559 STATIC_ASSERT(kNotStringTag != 0);
5560 __ or_(tmp3, tmp2);
5561 __ testb(tmp3, Immediate(kIsNotStringMask));
5562 __ j(not_zero, &miss);
5563
5564 // Fast check for identical strings.
5565 Label not_same;
5566 __ cmpq(left, right);
5567 __ j(not_equal, &not_same, Label::kNear);
5568 STATIC_ASSERT(EQUAL == 0);
5569 STATIC_ASSERT(kSmiTag == 0);
5570 __ Move(rax, Smi::FromInt(EQUAL));
5571 __ ret(0);
5572
5573 // Handle not identical strings.
5574 __ bind(&not_same);
5575
5576 // Check that both strings are symbols. If they are, we're done
5577 // because we already know they are not identical.
5578 Label do_compare;
5579 STATIC_ASSERT(kSymbolTag != 0);
5580 __ and_(tmp1, tmp2);
5581 __ testb(tmp1, Immediate(kIsSymbolMask));
5582 __ j(zero, &do_compare, Label::kNear);
5583 // Make sure rax is non-zero. At this point input operands are
5584 // guaranteed to be non-zero.
5585 ASSERT(right.is(rax));
5586 __ ret(0);
5587
5588 // Check that both strings are sequential ASCII.
5589 Label runtime;
5590 __ bind(&do_compare);
5591 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
5592
5593 // Compare flat ASCII strings. Returns when done.
5594 StringCompareStub::GenerateFlatAsciiStringEquals(
5595 masm, left, right, tmp1, tmp2);
5596
5597 // Handle more complex cases in runtime.
5598 __ bind(&runtime);
5599 __ pop(tmp1); // Return address.
5600 __ push(left);
5601 __ push(right);
5602 __ push(tmp1);
5603 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
5604
5605 __ bind(&miss);
5606 GenerateMiss(masm);
5607}
5608
5609
Ben Murdochb0fe1622011-05-05 13:52:32 +01005610void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005611 ASSERT(state_ == CompareIC::OBJECTS);
Ben Murdoch257744e2011-11-30 15:57:28 +00005612 Label miss;
Steve Block1e0659c2011-05-24 12:43:12 +01005613 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005614 __ j(either_smi, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005615
5616 __ CmpObjectType(rax, JS_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005617 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005618 __ CmpObjectType(rdx, JS_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005619 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005620
5621 ASSERT(GetCondition() == equal);
5622 __ subq(rax, rdx);
5623 __ ret(0);
5624
5625 __ bind(&miss);
5626 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005627}
5628
5629
Ben Murdochc7cc0282012-03-05 14:35:55 +00005630void ICCompareStub::GenerateKnownObjects(MacroAssembler* masm) {
5631 Label miss;
5632 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
5633 __ j(either_smi, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005634
Ben Murdochc7cc0282012-03-05 14:35:55 +00005635 __ movq(rcx, FieldOperand(rax, HeapObject::kMapOffset));
5636 __ movq(rbx, FieldOperand(rdx, HeapObject::kMapOffset));
5637 __ Cmp(rcx, known_map_);
5638 __ j(not_equal, &miss, Label::kNear);
5639 __ Cmp(rbx, known_map_);
5640 __ j(not_equal, &miss, Label::kNear);
5641
5642 __ subq(rax, rdx);
5643 __ ret(0);
5644
5645 __ bind(&miss);
5646 GenerateMiss(masm);
5647}
5648
5649
5650void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005651 {
Ben Murdochc7cc0282012-03-05 14:35:55 +00005652 // Call the runtime system in a fresh internal frame.
5653 ExternalReference miss =
5654 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
5655
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005656 FrameScope scope(masm, StackFrame::INTERNAL);
5657 __ push(rdx);
5658 __ push(rax);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005659 __ push(rdx);
5660 __ push(rax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005661 __ Push(Smi::FromInt(op_));
5662 __ CallExternalReference(miss, 3);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005663
5664 // Compute the entry point of the rewritten stub.
5665 __ lea(rdi, FieldOperand(rax, Code::kHeaderSize));
5666 __ pop(rax);
5667 __ pop(rdx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005668 }
Steve Block1e0659c2011-05-24 12:43:12 +01005669
Steve Block1e0659c2011-05-24 12:43:12 +01005670 // Do a tail call to the rewritten stub.
5671 __ jmp(rdi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005672}
5673
Steve Block1e0659c2011-05-24 12:43:12 +01005674
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005675void StringDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
5676 Label* miss,
5677 Label* done,
5678 Register properties,
5679 Handle<String> name,
5680 Register r0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005681 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5682 // not equal to the name and kProbes-th slot is not used (its name is the
5683 // undefined value), it guarantees the hash table doesn't contain the
5684 // property. It's true even if some slots represent deleted properties
5685 // (their names are the null value).
5686 for (int i = 0; i < kInlinedProbes; i++) {
5687 // r0 points to properties hash.
5688 // Compute the masked index: (hash + i + i * i) & mask.
5689 Register index = r0;
5690 // Capacity is smi 2^n.
5691 __ SmiToInteger32(index, FieldOperand(properties, kCapacityOffset));
5692 __ decl(index);
5693 __ and_(index,
5694 Immediate(name->Hash() + StringDictionary::GetProbeOffset(i)));
5695
5696 // Scale the index by multiplying by the entry size.
5697 ASSERT(StringDictionary::kEntrySize == 3);
5698 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
5699
5700 Register entity_name = r0;
5701 // Having undefined at this place means the name is not contained.
5702 ASSERT_EQ(kSmiTagSize, 1);
5703 __ movq(entity_name, Operand(properties,
5704 index,
5705 times_pointer_size,
5706 kElementsStartOffset - kHeapObjectTag));
5707 __ Cmp(entity_name, masm->isolate()->factory()->undefined_value());
5708 __ j(equal, done);
5709
5710 // Stop if found the property.
5711 __ Cmp(entity_name, Handle<String>(name));
5712 __ j(equal, miss);
5713
5714 // Check if the entry name is not a symbol.
5715 __ movq(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
5716 __ testb(FieldOperand(entity_name, Map::kInstanceTypeOffset),
5717 Immediate(kIsSymbolMask));
5718 __ j(zero, miss);
5719 }
5720
5721 StringDictionaryLookupStub stub(properties,
5722 r0,
5723 r0,
5724 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
5725 __ Push(Handle<Object>(name));
5726 __ push(Immediate(name->Hash()));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005727 __ CallStub(&stub);
Ben Murdoch257744e2011-11-30 15:57:28 +00005728 __ testq(r0, r0);
5729 __ j(not_zero, miss);
5730 __ jmp(done);
Ben Murdoch257744e2011-11-30 15:57:28 +00005731}
5732
5733
5734// Probe the string dictionary in the |elements| register. Jump to the
5735// |done| label if a property with the given name is found leaving the
5736// index into the dictionary in |r1|. Jump to the |miss| label
5737// otherwise.
5738void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
5739 Label* miss,
5740 Label* done,
5741 Register elements,
5742 Register name,
5743 Register r0,
5744 Register r1) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005745 ASSERT(!elements.is(r0));
5746 ASSERT(!elements.is(r1));
5747 ASSERT(!name.is(r0));
5748 ASSERT(!name.is(r1));
5749
Ben Murdoch257744e2011-11-30 15:57:28 +00005750 // Assert that name contains a string.
5751 if (FLAG_debug_code) __ AbortIfNotString(name);
5752
5753 __ SmiToInteger32(r0, FieldOperand(elements, kCapacityOffset));
5754 __ decl(r0);
5755
5756 for (int i = 0; i < kInlinedProbes; i++) {
5757 // Compute the masked index: (hash + i + i * i) & mask.
5758 __ movl(r1, FieldOperand(name, String::kHashFieldOffset));
5759 __ shrl(r1, Immediate(String::kHashShift));
5760 if (i > 0) {
5761 __ addl(r1, Immediate(StringDictionary::GetProbeOffset(i)));
5762 }
5763 __ and_(r1, r0);
5764
5765 // Scale the index by multiplying by the entry size.
5766 ASSERT(StringDictionary::kEntrySize == 3);
5767 __ lea(r1, Operand(r1, r1, times_2, 0)); // r1 = r1 * 3
5768
5769 // Check if the key is identical to the name.
5770 __ cmpq(name, Operand(elements, r1, times_pointer_size,
5771 kElementsStartOffset - kHeapObjectTag));
5772 __ j(equal, done);
5773 }
5774
5775 StringDictionaryLookupStub stub(elements,
5776 r0,
5777 r1,
5778 POSITIVE_LOOKUP);
5779 __ push(name);
5780 __ movl(r0, FieldOperand(name, String::kHashFieldOffset));
5781 __ shrl(r0, Immediate(String::kHashShift));
5782 __ push(r0);
5783 __ CallStub(&stub);
5784
5785 __ testq(r0, r0);
5786 __ j(zero, miss);
5787 __ jmp(done);
5788}
5789
5790
5791void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005792 // This stub overrides SometimesSetsUpAFrame() to return false. That means
5793 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00005794 // Stack frame on entry:
5795 // esp[0 * kPointerSize]: return address.
5796 // esp[1 * kPointerSize]: key's hash.
5797 // esp[2 * kPointerSize]: key.
5798 // Registers:
5799 // dictionary_: StringDictionary to probe.
5800 // result_: used as scratch.
5801 // index_: will hold an index of entry if lookup is successful.
5802 // might alias with result_.
5803 // Returns:
5804 // result_ is zero if lookup failed, non zero otherwise.
5805
5806 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
5807
5808 Register scratch = result_;
5809
5810 __ SmiToInteger32(scratch, FieldOperand(dictionary_, kCapacityOffset));
5811 __ decl(scratch);
5812 __ push(scratch);
5813
5814 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5815 // not equal to the name and kProbes-th slot is not used (its name is the
5816 // undefined value), it guarantees the hash table doesn't contain the
5817 // property. It's true even if some slots represent deleted properties
5818 // (their names are the null value).
5819 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
5820 // Compute the masked index: (hash + i + i * i) & mask.
5821 __ movq(scratch, Operand(rsp, 2 * kPointerSize));
5822 if (i > 0) {
5823 __ addl(scratch, Immediate(StringDictionary::GetProbeOffset(i)));
5824 }
5825 __ and_(scratch, Operand(rsp, 0));
5826
5827 // Scale the index by multiplying by the entry size.
5828 ASSERT(StringDictionary::kEntrySize == 3);
5829 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
5830
5831 // Having undefined at this place means the name is not contained.
5832 __ movq(scratch, Operand(dictionary_,
5833 index_,
5834 times_pointer_size,
5835 kElementsStartOffset - kHeapObjectTag));
5836
5837 __ Cmp(scratch, masm->isolate()->factory()->undefined_value());
5838 __ j(equal, &not_in_dictionary);
5839
5840 // Stop if found the property.
5841 __ cmpq(scratch, Operand(rsp, 3 * kPointerSize));
5842 __ j(equal, &in_dictionary);
5843
5844 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
5845 // If we hit a non symbol key during negative lookup
5846 // we have to bailout as this key might be equal to the
5847 // key we are looking for.
5848
5849 // Check if the entry name is not a symbol.
5850 __ movq(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
5851 __ testb(FieldOperand(scratch, Map::kInstanceTypeOffset),
5852 Immediate(kIsSymbolMask));
5853 __ j(zero, &maybe_in_dictionary);
5854 }
5855 }
5856
5857 __ bind(&maybe_in_dictionary);
5858 // If we are doing negative lookup then probing failure should be
5859 // treated as a lookup success. For positive lookup probing failure
5860 // should be treated as lookup failure.
5861 if (mode_ == POSITIVE_LOOKUP) {
5862 __ movq(scratch, Immediate(0));
5863 __ Drop(1);
5864 __ ret(2 * kPointerSize);
5865 }
5866
5867 __ bind(&in_dictionary);
5868 __ movq(scratch, Immediate(1));
5869 __ Drop(1);
5870 __ ret(2 * kPointerSize);
5871
5872 __ bind(&not_in_dictionary);
5873 __ movq(scratch, Immediate(0));
5874 __ Drop(1);
5875 __ ret(2 * kPointerSize);
5876}
5877
5878
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005879struct AheadOfTimeWriteBarrierStubList {
5880 Register object, value, address;
5881 RememberedSetAction action;
5882};
5883
5884
5885struct AheadOfTimeWriteBarrierStubList kAheadOfTime[] = {
5886 // Used in RegExpExecStub.
5887 { rbx, rax, rdi, EMIT_REMEMBERED_SET },
5888 // Used in CompileArrayPushCall.
5889 { rbx, rcx, rdx, EMIT_REMEMBERED_SET },
5890 // Used in CompileStoreGlobal.
5891 { rbx, rcx, rdx, OMIT_REMEMBERED_SET },
5892 // Used in StoreStubCompiler::CompileStoreField and
5893 // KeyedStoreStubCompiler::CompileStoreField via GenerateStoreField.
5894 { rdx, rcx, rbx, EMIT_REMEMBERED_SET },
5895 // GenerateStoreField calls the stub with two different permutations of
5896 // registers. This is the second.
5897 { rbx, rcx, rdx, EMIT_REMEMBERED_SET },
5898 // StoreIC::GenerateNormal via GenerateDictionaryStore.
5899 { rbx, r8, r9, EMIT_REMEMBERED_SET },
5900 // KeyedStoreIC::GenerateGeneric.
5901 { rbx, rdx, rcx, EMIT_REMEMBERED_SET},
5902 // KeyedStoreStubCompiler::GenerateStoreFastElement.
5903 { rdi, rdx, rcx, EMIT_REMEMBERED_SET},
5904 // ElementsTransitionGenerator::GenerateSmiOnlyToObject
5905 // and ElementsTransitionGenerator::GenerateSmiOnlyToObject
5906 // and ElementsTransitionGenerator::GenerateDoubleToObject
5907 { rdx, rbx, rdi, EMIT_REMEMBERED_SET},
5908 // ElementsTransitionGenerator::GenerateSmiOnlyToDouble
5909 // and ElementsTransitionGenerator::GenerateDoubleToObject
5910 { rdx, r11, r15, EMIT_REMEMBERED_SET},
5911 // ElementsTransitionGenerator::GenerateDoubleToObject
5912 { r11, rax, r15, EMIT_REMEMBERED_SET},
5913 // StoreArrayLiteralElementStub::Generate
5914 { rbx, rax, rcx, EMIT_REMEMBERED_SET},
5915 // Null termination.
5916 { no_reg, no_reg, no_reg, EMIT_REMEMBERED_SET}
5917};
5918
5919
5920bool RecordWriteStub::IsPregenerated() {
5921 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
5922 !entry->object.is(no_reg);
5923 entry++) {
5924 if (object_.is(entry->object) &&
5925 value_.is(entry->value) &&
5926 address_.is(entry->address) &&
5927 remembered_set_action_ == entry->action &&
5928 save_fp_regs_mode_ == kDontSaveFPRegs) {
5929 return true;
5930 }
5931 }
5932 return false;
5933}
5934
5935
5936void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime() {
5937 StoreBufferOverflowStub stub1(kDontSaveFPRegs);
5938 stub1.GetCode()->set_is_pregenerated(true);
5939 StoreBufferOverflowStub stub2(kSaveFPRegs);
5940 stub2.GetCode()->set_is_pregenerated(true);
5941}
5942
5943
5944void RecordWriteStub::GenerateFixedRegStubsAheadOfTime() {
5945 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
5946 !entry->object.is(no_reg);
5947 entry++) {
5948 RecordWriteStub stub(entry->object,
5949 entry->value,
5950 entry->address,
5951 entry->action,
5952 kDontSaveFPRegs);
5953 stub.GetCode()->set_is_pregenerated(true);
5954 }
5955}
5956
5957
5958// Takes the input in 3 registers: address_ value_ and object_. A pointer to
5959// the value has just been written into the object, now this stub makes sure
5960// we keep the GC informed. The word in the object where the value has been
5961// written is in the address register.
5962void RecordWriteStub::Generate(MacroAssembler* masm) {
5963 Label skip_to_incremental_noncompacting;
5964 Label skip_to_incremental_compacting;
5965
5966 // The first two instructions are generated with labels so as to get the
5967 // offset fixed up correctly by the bind(Label*) call. We patch it back and
5968 // forth between a compare instructions (a nop in this position) and the
5969 // real branch when we start and stop incremental heap marking.
5970 // See RecordWriteStub::Patch for details.
5971 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
5972 __ jmp(&skip_to_incremental_compacting, Label::kFar);
5973
5974 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
5975 __ RememberedSetHelper(object_,
5976 address_,
5977 value_,
5978 save_fp_regs_mode_,
5979 MacroAssembler::kReturnAtEnd);
5980 } else {
5981 __ ret(0);
5982 }
5983
5984 __ bind(&skip_to_incremental_noncompacting);
5985 GenerateIncremental(masm, INCREMENTAL);
5986
5987 __ bind(&skip_to_incremental_compacting);
5988 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
5989
5990 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
5991 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
5992 masm->set_byte_at(0, kTwoByteNopInstruction);
5993 masm->set_byte_at(2, kFiveByteNopInstruction);
5994}
5995
5996
5997void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
5998 regs_.Save(masm);
5999
6000 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
6001 Label dont_need_remembered_set;
6002
6003 __ movq(regs_.scratch0(), Operand(regs_.address(), 0));
6004 __ JumpIfNotInNewSpace(regs_.scratch0(),
6005 regs_.scratch0(),
6006 &dont_need_remembered_set);
6007
6008 __ CheckPageFlag(regs_.object(),
6009 regs_.scratch0(),
6010 1 << MemoryChunk::SCAN_ON_SCAVENGE,
6011 not_zero,
6012 &dont_need_remembered_set);
6013
6014 // First notify the incremental marker if necessary, then update the
6015 // remembered set.
6016 CheckNeedsToInformIncrementalMarker(
6017 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
6018 InformIncrementalMarker(masm, mode);
6019 regs_.Restore(masm);
6020 __ RememberedSetHelper(object_,
6021 address_,
6022 value_,
6023 save_fp_regs_mode_,
6024 MacroAssembler::kReturnAtEnd);
6025
6026 __ bind(&dont_need_remembered_set);
6027 }
6028
6029 CheckNeedsToInformIncrementalMarker(
6030 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
6031 InformIncrementalMarker(masm, mode);
6032 regs_.Restore(masm);
6033 __ ret(0);
6034}
6035
6036
6037void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm, Mode mode) {
6038 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode_);
6039#ifdef _WIN64
6040 Register arg3 = r8;
6041 Register arg2 = rdx;
6042 Register arg1 = rcx;
6043#else
6044 Register arg3 = rdx;
6045 Register arg2 = rsi;
6046 Register arg1 = rdi;
6047#endif
6048 Register address =
6049 arg1.is(regs_.address()) ? kScratchRegister : regs_.address();
6050 ASSERT(!address.is(regs_.object()));
6051 ASSERT(!address.is(arg1));
6052 __ Move(address, regs_.address());
6053 __ Move(arg1, regs_.object());
6054 if (mode == INCREMENTAL_COMPACTION) {
6055 // TODO(gc) Can we just set address arg2 in the beginning?
6056 __ Move(arg2, address);
6057 } else {
6058 ASSERT(mode == INCREMENTAL);
6059 __ movq(arg2, Operand(address, 0));
6060 }
6061 __ LoadAddress(arg3, ExternalReference::isolate_address());
6062 int argument_count = 3;
6063
6064 AllowExternalCallThatCantCauseGC scope(masm);
6065 __ PrepareCallCFunction(argument_count);
6066 if (mode == INCREMENTAL_COMPACTION) {
6067 __ CallCFunction(
6068 ExternalReference::incremental_evacuation_record_write_function(
6069 masm->isolate()),
6070 argument_count);
6071 } else {
6072 ASSERT(mode == INCREMENTAL);
6073 __ CallCFunction(
6074 ExternalReference::incremental_marking_record_write_function(
6075 masm->isolate()),
6076 argument_count);
6077 }
6078 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode_);
6079}
6080
6081
6082void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
6083 MacroAssembler* masm,
6084 OnNoNeedToInformIncrementalMarker on_no_need,
6085 Mode mode) {
6086 Label on_black;
6087 Label need_incremental;
6088 Label need_incremental_pop_object;
6089
6090 // Let's look at the color of the object: If it is not black we don't have
6091 // to inform the incremental marker.
6092 __ JumpIfBlack(regs_.object(),
6093 regs_.scratch0(),
6094 regs_.scratch1(),
6095 &on_black,
6096 Label::kNear);
6097
6098 regs_.Restore(masm);
6099 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
6100 __ RememberedSetHelper(object_,
6101 address_,
6102 value_,
6103 save_fp_regs_mode_,
6104 MacroAssembler::kReturnAtEnd);
6105 } else {
6106 __ ret(0);
6107 }
6108
6109 __ bind(&on_black);
6110
6111 // Get the value from the slot.
6112 __ movq(regs_.scratch0(), Operand(regs_.address(), 0));
6113
6114 if (mode == INCREMENTAL_COMPACTION) {
6115 Label ensure_not_white;
6116
6117 __ CheckPageFlag(regs_.scratch0(), // Contains value.
6118 regs_.scratch1(), // Scratch.
6119 MemoryChunk::kEvacuationCandidateMask,
6120 zero,
6121 &ensure_not_white,
6122 Label::kNear);
6123
6124 __ CheckPageFlag(regs_.object(),
6125 regs_.scratch1(), // Scratch.
6126 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
6127 zero,
6128 &need_incremental);
6129
6130 __ bind(&ensure_not_white);
6131 }
6132
6133 // We need an extra register for this, so we push the object register
6134 // temporarily.
6135 __ push(regs_.object());
6136 __ EnsureNotWhite(regs_.scratch0(), // The value.
6137 regs_.scratch1(), // Scratch.
6138 regs_.object(), // Scratch.
6139 &need_incremental_pop_object,
6140 Label::kNear);
6141 __ pop(regs_.object());
6142
6143 regs_.Restore(masm);
6144 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
6145 __ RememberedSetHelper(object_,
6146 address_,
6147 value_,
6148 save_fp_regs_mode_,
6149 MacroAssembler::kReturnAtEnd);
6150 } else {
6151 __ ret(0);
6152 }
6153
6154 __ bind(&need_incremental_pop_object);
6155 __ pop(regs_.object());
6156
6157 __ bind(&need_incremental);
6158
6159 // Fall through when we need to inform the incremental marker.
6160}
6161
6162
6163void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
6164 // ----------- S t a t e -------------
6165 // -- rax : element value to store
6166 // -- rbx : array literal
6167 // -- rdi : map of array literal
6168 // -- rcx : element index as smi
6169 // -- rdx : array literal index in function
6170 // -- rsp[0] : return address
6171 // -----------------------------------
6172
6173 Label element_done;
6174 Label double_elements;
6175 Label smi_element;
6176 Label slow_elements;
6177 Label fast_elements;
6178
6179 __ CheckFastElements(rdi, &double_elements);
6180
6181 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
6182 __ JumpIfSmi(rax, &smi_element);
6183 __ CheckFastSmiOnlyElements(rdi, &fast_elements);
6184
6185 // Store into the array literal requires a elements transition. Call into
6186 // the runtime.
6187
6188 __ bind(&slow_elements);
6189 __ pop(rdi); // Pop return address and remember to put back later for tail
6190 // call.
6191 __ push(rbx);
6192 __ push(rcx);
6193 __ push(rax);
6194 __ movq(rbx, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
6195 __ push(FieldOperand(rbx, JSFunction::kLiteralsOffset));
6196 __ push(rdx);
6197 __ push(rdi); // Return return address so that tail call returns to right
6198 // place.
6199 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
6200
6201 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
6202 __ bind(&fast_elements);
6203 __ SmiToInteger32(kScratchRegister, rcx);
6204 __ movq(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
6205 __ lea(rcx, FieldOperand(rbx, kScratchRegister, times_pointer_size,
6206 FixedArrayBase::kHeaderSize));
6207 __ movq(Operand(rcx, 0), rax);
6208 // Update the write barrier for the array store.
6209 __ RecordWrite(rbx, rcx, rax,
6210 kDontSaveFPRegs,
6211 EMIT_REMEMBERED_SET,
6212 OMIT_SMI_CHECK);
6213 __ ret(0);
6214
6215 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
6216 // FAST_ELEMENTS, and value is Smi.
6217 __ bind(&smi_element);
6218 __ SmiToInteger32(kScratchRegister, rcx);
6219 __ movq(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
6220 __ movq(FieldOperand(rbx, kScratchRegister, times_pointer_size,
6221 FixedArrayBase::kHeaderSize), rax);
6222 __ ret(0);
6223
6224 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
6225 __ bind(&double_elements);
6226
6227 __ movq(r9, FieldOperand(rbx, JSObject::kElementsOffset));
6228 __ SmiToInteger32(r11, rcx);
6229 __ StoreNumberToDoubleElements(rax,
6230 r9,
6231 r11,
6232 xmm0,
6233 &slow_elements);
6234 __ ret(0);
6235}
6236
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006237#undef __
6238
6239} } // namespace v8::internal
6240
6241#endif // V8_TARGET_ARCH_X64