blob: 98c5c6f327b94c1201292412ef0988d462218883 [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
127 // Setup the object header.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000128 __ LoadRoot(kScratchRegister, Heap::kFunctionContextMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100129 __ movq(FieldOperand(rax, HeapObject::kMapOffset), kScratchRegister);
130 __ Move(FieldOperand(rax, FixedArray::kLengthOffset), Smi::FromInt(length));
131
132 // Setup the fixed slots.
Steve Block9fac8402011-05-12 15:51:54 +0100133 __ Set(rbx, 0); // Set to NULL.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100134 __ movq(Operand(rax, Context::SlotOffset(Context::CLOSURE_INDEX)), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000135 __ movq(Operand(rax, Context::SlotOffset(Context::PREVIOUS_INDEX)), rsi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100136 __ movq(Operand(rax, Context::SlotOffset(Context::EXTENSION_INDEX)), rbx);
137
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000138 // Copy the global object from the previous context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100139 __ movq(rbx, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
140 __ movq(Operand(rax, Context::SlotOffset(Context::GLOBAL_INDEX)), rbx);
141
142 // Initialize the rest of the slots to undefined.
143 __ LoadRoot(rbx, Heap::kUndefinedValueRootIndex);
144 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
145 __ movq(Operand(rax, Context::SlotOffset(i)), rbx);
146 }
147
148 // Return and remove the on-stack parameter.
149 __ movq(rsi, rax);
150 __ ret(1 * kPointerSize);
151
152 // Need to collect. Call into runtime system.
153 __ bind(&gc);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000154 __ TailCallRuntime(Runtime::kNewFunctionContext, 1, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100155}
156
157
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
176 // Setup the object header.
177 __ 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
197 // Setup the fixed slots.
198 __ 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) {
1994 // Registers are used as follows:
1995 // rdx = base
1996 // rax = exponent
1997 // rcx = temporary, result
1998
1999 Label allocate_return, call_runtime;
2000
2001 // Load input parameters.
2002 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
2003 __ movq(rax, Operand(rsp, 1 * kPointerSize));
2004
2005 // Save 1 in xmm3 - we need this several times later on.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002006 __ Set(rcx, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002007 __ cvtlsi2sd(xmm3, rcx);
2008
2009 Label exponent_nonsmi;
2010 Label base_nonsmi;
2011 // If the exponent is a heap number go to that specific case.
2012 __ JumpIfNotSmi(rax, &exponent_nonsmi);
2013 __ JumpIfNotSmi(rdx, &base_nonsmi);
2014
2015 // Optimized version when both exponent and base are smis.
2016 Label powi;
2017 __ SmiToInteger32(rdx, rdx);
2018 __ cvtlsi2sd(xmm0, rdx);
2019 __ jmp(&powi);
2020 // Exponent is a smi and base is a heapnumber.
2021 __ bind(&base_nonsmi);
2022 __ CompareRoot(FieldOperand(rdx, HeapObject::kMapOffset),
2023 Heap::kHeapNumberMapRootIndex);
2024 __ j(not_equal, &call_runtime);
2025
2026 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
2027
2028 // Optimized version of pow if exponent is a smi.
2029 // xmm0 contains the base.
2030 __ bind(&powi);
2031 __ SmiToInteger32(rax, rax);
2032
2033 // Save exponent in base as we need to check if exponent is negative later.
2034 // We know that base and exponent are in different registers.
2035 __ movq(rdx, rax);
2036
2037 // Get absolute value of exponent.
Ben Murdoch257744e2011-11-30 15:57:28 +00002038 Label no_neg;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002039 __ cmpl(rax, Immediate(0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002040 __ j(greater_equal, &no_neg, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002041 __ negl(rax);
2042 __ bind(&no_neg);
2043
2044 // Load xmm1 with 1.
Ben Murdoch257744e2011-11-30 15:57:28 +00002045 __ movaps(xmm1, xmm3);
2046 Label while_true;
2047 Label no_multiply;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002048
2049 __ bind(&while_true);
2050 __ shrl(rax, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00002051 __ j(not_carry, &no_multiply, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002052 __ mulsd(xmm1, xmm0);
2053 __ bind(&no_multiply);
2054 __ mulsd(xmm0, xmm0);
2055 __ j(not_zero, &while_true);
2056
2057 // Base has the original value of the exponent - if the exponent is
2058 // negative return 1/result.
2059 __ testl(rdx, rdx);
2060 __ j(positive, &allocate_return);
2061 // Special case if xmm1 has reached infinity.
2062 __ divsd(xmm3, xmm1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002063 __ movaps(xmm1, xmm3);
2064 __ xorps(xmm0, xmm0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002065 __ ucomisd(xmm0, xmm1);
2066 __ j(equal, &call_runtime);
2067
2068 __ jmp(&allocate_return);
2069
2070 // Exponent (or both) is a heapnumber - no matter what we should now work
2071 // on doubles.
2072 __ bind(&exponent_nonsmi);
2073 __ CompareRoot(FieldOperand(rax, HeapObject::kMapOffset),
2074 Heap::kHeapNumberMapRootIndex);
2075 __ j(not_equal, &call_runtime);
2076 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
2077 // Test if exponent is nan.
2078 __ ucomisd(xmm1, xmm1);
2079 __ j(parity_even, &call_runtime);
2080
Ben Murdoch257744e2011-11-30 15:57:28 +00002081 Label base_not_smi, handle_special_cases;
2082 __ JumpIfNotSmi(rdx, &base_not_smi, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002083 __ SmiToInteger32(rdx, rdx);
2084 __ cvtlsi2sd(xmm0, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00002085 __ jmp(&handle_special_cases, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002086
2087 __ bind(&base_not_smi);
2088 __ CompareRoot(FieldOperand(rdx, HeapObject::kMapOffset),
2089 Heap::kHeapNumberMapRootIndex);
2090 __ j(not_equal, &call_runtime);
2091 __ movl(rcx, FieldOperand(rdx, HeapNumber::kExponentOffset));
2092 __ andl(rcx, Immediate(HeapNumber::kExponentMask));
2093 __ cmpl(rcx, Immediate(HeapNumber::kExponentMask));
2094 // base is NaN or +/-Infinity
2095 __ j(greater_equal, &call_runtime);
2096 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
2097
2098 // base is in xmm0 and exponent is in xmm1.
2099 __ bind(&handle_special_cases);
Ben Murdoch257744e2011-11-30 15:57:28 +00002100 Label not_minus_half;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002101 // Test for -0.5.
2102 // Load xmm2 with -0.5.
2103 __ movq(rcx, V8_UINT64_C(0xBFE0000000000000), RelocInfo::NONE);
2104 __ movq(xmm2, rcx);
2105 // xmm2 now has -0.5.
2106 __ ucomisd(xmm2, xmm1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002107 __ j(not_equal, &not_minus_half, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002108
2109 // Calculates reciprocal of square root.
2110 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
Ben Murdoch257744e2011-11-30 15:57:28 +00002111 __ xorps(xmm1, xmm1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002112 __ addsd(xmm1, xmm0);
2113 __ sqrtsd(xmm1, xmm1);
2114 __ divsd(xmm3, xmm1);
Ben Murdoch257744e2011-11-30 15:57:28 +00002115 __ movaps(xmm1, xmm3);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002116 __ jmp(&allocate_return);
2117
2118 // Test for 0.5.
2119 __ bind(&not_minus_half);
2120 // Load xmm2 with 0.5.
2121 // Since xmm3 is 1 and xmm2 is -0.5 this is simply xmm2 + xmm3.
2122 __ addsd(xmm2, xmm3);
2123 // xmm2 now has 0.5.
2124 __ ucomisd(xmm2, xmm1);
2125 __ j(not_equal, &call_runtime);
2126 // Calculates square root.
2127 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
Ben Murdoch257744e2011-11-30 15:57:28 +00002128 __ xorps(xmm1, xmm1);
2129 __ addsd(xmm1, xmm0); // Convert -0 to 0.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002130 __ sqrtsd(xmm1, xmm1);
2131
2132 __ bind(&allocate_return);
2133 __ AllocateHeapNumber(rcx, rax, &call_runtime);
2134 __ movsd(FieldOperand(rcx, HeapNumber::kValueOffset), xmm1);
2135 __ movq(rax, rcx);
2136 __ ret(2 * kPointerSize);
2137
2138 __ bind(&call_runtime);
2139 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
2140}
2141
2142
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002143void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
2144 // The key is in rdx and the parameter count is in rax.
2145
2146 // The displacement is used for skipping the frame pointer on the
2147 // stack. It is the offset of the last parameter (if any) relative
2148 // to the frame pointer.
2149 static const int kDisplacement = 1 * kPointerSize;
2150
2151 // Check that the key is a smi.
2152 Label slow;
2153 __ JumpIfNotSmi(rdx, &slow);
2154
Steve Block44f0eee2011-05-26 01:26:41 +01002155 // Check if the calling frame is an arguments adaptor frame. We look at the
2156 // context offset, and if the frame is not a regular one, then we find a
2157 // Smi instead of the context. We can't use SmiCompare here, because that
2158 // only works for comparing two smis.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002159 Label adaptor;
2160 __ movq(rbx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002161 __ Cmp(Operand(rbx, StandardFrameConstants::kContextOffset),
2162 Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002163 __ j(equal, &adaptor);
2164
2165 // Check index against formal parameters count limit passed in
2166 // through register rax. Use unsigned comparison to get negative
2167 // check for free.
2168 __ cmpq(rdx, rax);
2169 __ j(above_equal, &slow);
2170
2171 // Read the argument from the stack and return it.
2172 SmiIndex index = masm->SmiToIndex(rax, rax, kPointerSizeLog2);
2173 __ lea(rbx, Operand(rbp, index.reg, index.scale, 0));
2174 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
2175 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
2176 __ Ret();
2177
2178 // Arguments adaptor case: Check index against actual arguments
2179 // limit found in the arguments adaptor frame. Use unsigned
2180 // comparison to get negative check for free.
2181 __ bind(&adaptor);
2182 __ movq(rcx, Operand(rbx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2183 __ cmpq(rdx, rcx);
2184 __ j(above_equal, &slow);
2185
2186 // Read the argument from the stack and return it.
2187 index = masm->SmiToIndex(rax, rcx, kPointerSizeLog2);
2188 __ lea(rbx, Operand(rbx, index.reg, index.scale, 0));
2189 index = masm->SmiToNegativeIndex(rdx, rdx, kPointerSizeLog2);
2190 __ movq(rax, Operand(rbx, index.reg, index.scale, kDisplacement));
2191 __ Ret();
2192
2193 // Slow-case: Handle non-smi or out-of-bounds access to arguments
2194 // by calling the runtime system.
2195 __ bind(&slow);
2196 __ pop(rbx); // Return address.
2197 __ push(rdx);
2198 __ push(rbx);
2199 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
2200}
2201
2202
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002203void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
2204 // Stack layout:
2205 // rsp[0] : return address
2206 // rsp[8] : number of parameters (tagged)
2207 // rsp[16] : receiver displacement
2208 // rsp[24] : function
2209 // Registers used over the whole function:
2210 // rbx: the mapped parameter count (untagged)
2211 // rax: the allocated object (tagged).
2212
2213 Factory* factory = masm->isolate()->factory();
2214
2215 __ SmiToInteger64(rbx, Operand(rsp, 1 * kPointerSize));
2216 // rbx = parameter count (untagged)
2217
2218 // Check if the calling frame is an arguments adaptor frame.
2219 Label runtime;
2220 Label adaptor_frame, try_allocate;
2221 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
2222 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2223 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2224 __ j(equal, &adaptor_frame);
2225
2226 // No adaptor, parameter count = argument count.
2227 __ movq(rcx, rbx);
2228 __ jmp(&try_allocate, Label::kNear);
2229
2230 // We have an adaptor frame. Patch the parameters pointer.
2231 __ bind(&adaptor_frame);
2232 __ SmiToInteger64(rcx,
2233 Operand(rdx,
2234 ArgumentsAdaptorFrameConstants::kLengthOffset));
2235 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2236 StandardFrameConstants::kCallerSPOffset));
2237 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2238
2239 // rbx = parameter count (untagged)
2240 // rcx = argument count (untagged)
2241 // Compute the mapped parameter count = min(rbx, rcx) in rbx.
2242 __ cmpq(rbx, rcx);
2243 __ j(less_equal, &try_allocate, Label::kNear);
2244 __ movq(rbx, rcx);
2245
2246 __ bind(&try_allocate);
2247
2248 // Compute the sizes of backing store, parameter map, and arguments object.
2249 // 1. Parameter map, has 2 extra words containing context and backing store.
2250 const int kParameterMapHeaderSize =
2251 FixedArray::kHeaderSize + 2 * kPointerSize;
2252 Label no_parameter_map;
2253 __ testq(rbx, rbx);
2254 __ j(zero, &no_parameter_map, Label::kNear);
2255 __ lea(r8, Operand(rbx, times_pointer_size, kParameterMapHeaderSize));
2256 __ bind(&no_parameter_map);
2257
2258 // 2. Backing store.
2259 __ lea(r8, Operand(r8, rcx, times_pointer_size, FixedArray::kHeaderSize));
2260
2261 // 3. Arguments object.
2262 __ addq(r8, Immediate(Heap::kArgumentsObjectSize));
2263
2264 // Do the allocation of all three objects in one go.
2265 __ AllocateInNewSpace(r8, rax, rdx, rdi, &runtime, TAG_OBJECT);
2266
2267 // rax = address of new object(s) (tagged)
2268 // rcx = argument count (untagged)
2269 // Get the arguments boilerplate from the current (global) context into rdi.
2270 Label has_mapped_parameters, copy;
2271 __ movq(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2272 __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
2273 __ testq(rbx, rbx);
2274 __ j(not_zero, &has_mapped_parameters, Label::kNear);
2275
2276 const int kIndex = Context::ARGUMENTS_BOILERPLATE_INDEX;
2277 __ movq(rdi, Operand(rdi, Context::SlotOffset(kIndex)));
2278 __ jmp(&copy, Label::kNear);
2279
2280 const int kAliasedIndex = Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX;
2281 __ bind(&has_mapped_parameters);
2282 __ movq(rdi, Operand(rdi, Context::SlotOffset(kAliasedIndex)));
2283 __ bind(&copy);
2284
2285 // rax = address of new object (tagged)
2286 // rbx = mapped parameter count (untagged)
2287 // rcx = argument count (untagged)
2288 // rdi = address of boilerplate object (tagged)
2289 // Copy the JS object part.
2290 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2291 __ movq(rdx, FieldOperand(rdi, i));
2292 __ movq(FieldOperand(rax, i), rdx);
2293 }
2294
2295 // Setup the callee in-object property.
2296 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
2297 __ movq(rdx, Operand(rsp, 3 * kPointerSize));
2298 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
2299 Heap::kArgumentsCalleeIndex * kPointerSize),
2300 rdx);
2301
2302 // Use the length (smi tagged) and set that as an in-object property too.
2303 // Note: rcx is tagged from here on.
2304 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
2305 __ Integer32ToSmi(rcx, rcx);
2306 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
2307 Heap::kArgumentsLengthIndex * kPointerSize),
2308 rcx);
2309
2310 // Setup the elements pointer in the allocated arguments object.
2311 // If we allocated a parameter map, edi will point there, otherwise to the
2312 // backing store.
2313 __ lea(rdi, Operand(rax, Heap::kArgumentsObjectSize));
2314 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rdi);
2315
2316 // rax = address of new object (tagged)
2317 // rbx = mapped parameter count (untagged)
2318 // rcx = argument count (tagged)
2319 // rdi = address of parameter map or backing store (tagged)
2320
2321 // Initialize parameter map. If there are no mapped arguments, we're done.
2322 Label skip_parameter_map;
2323 __ testq(rbx, rbx);
2324 __ j(zero, &skip_parameter_map);
2325
2326 __ LoadRoot(kScratchRegister, Heap::kNonStrictArgumentsElementsMapRootIndex);
2327 // rbx contains the untagged argument count. Add 2 and tag to write.
2328 __ movq(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
2329 __ Integer64PlusConstantToSmi(r9, rbx, 2);
2330 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), r9);
2331 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize + 0 * kPointerSize), rsi);
2332 __ lea(r9, Operand(rdi, rbx, times_pointer_size, kParameterMapHeaderSize));
2333 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize + 1 * kPointerSize), r9);
2334
2335 // Copy the parameter slots and the holes in the arguments.
2336 // We need to fill in mapped_parameter_count slots. They index the context,
2337 // where parameters are stored in reverse order, at
2338 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
2339 // The mapped parameter thus need to get indices
2340 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
2341 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
2342 // We loop from right to left.
2343 Label parameters_loop, parameters_test;
2344
2345 // Load tagged parameter count into r9.
2346 __ movq(r9, Operand(rsp, 1 * kPointerSize));
2347 __ Move(r8, Smi::FromInt(Context::MIN_CONTEXT_SLOTS));
2348 __ addq(r8, Operand(rsp, 3 * kPointerSize));
2349 __ subq(r8, r9);
2350 __ Move(r11, factory->the_hole_value());
2351 __ movq(rdx, rdi);
2352 __ SmiToInteger64(kScratchRegister, r9);
2353 __ lea(rdi, Operand(rdi, kScratchRegister,
2354 times_pointer_size,
2355 kParameterMapHeaderSize));
2356 // r9 = loop variable (tagged)
2357 // r8 = mapping index (tagged)
2358 // r11 = the hole value
2359 // rdx = address of parameter map (tagged)
2360 // rdi = address of backing store (tagged)
2361 __ jmp(&parameters_test, Label::kNear);
2362
2363 __ bind(&parameters_loop);
2364 __ SmiSubConstant(r9, r9, Smi::FromInt(1));
2365 __ SmiToInteger64(kScratchRegister, r9);
2366 __ movq(FieldOperand(rdx, kScratchRegister,
2367 times_pointer_size,
2368 kParameterMapHeaderSize),
2369 r8);
2370 __ movq(FieldOperand(rdi, kScratchRegister,
2371 times_pointer_size,
2372 FixedArray::kHeaderSize),
2373 r11);
2374 __ SmiAddConstant(r8, r8, Smi::FromInt(1));
2375 __ bind(&parameters_test);
2376 __ SmiTest(r9);
2377 __ j(not_zero, &parameters_loop, Label::kNear);
2378
2379 __ bind(&skip_parameter_map);
2380
2381 // rcx = argument count (tagged)
2382 // rdi = address of backing store (tagged)
2383 // Copy arguments header and remaining slots (if there are any).
2384 __ Move(FieldOperand(rdi, FixedArray::kMapOffset),
2385 factory->fixed_array_map());
2386 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
2387
2388 Label arguments_loop, arguments_test;
2389 __ movq(r8, rbx);
2390 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
2391 // Untag rcx and r8 for the loop below.
2392 __ SmiToInteger64(rcx, rcx);
2393 __ SmiToInteger64(r8, r8);
2394 __ lea(kScratchRegister, Operand(r8, times_pointer_size, 0));
2395 __ subq(rdx, kScratchRegister);
2396 __ jmp(&arguments_test, Label::kNear);
2397
2398 __ bind(&arguments_loop);
2399 __ subq(rdx, Immediate(kPointerSize));
2400 __ movq(r9, Operand(rdx, 0));
2401 __ movq(FieldOperand(rdi, r8,
2402 times_pointer_size,
2403 FixedArray::kHeaderSize),
2404 r9);
2405 __ addq(r8, Immediate(1));
2406
2407 __ bind(&arguments_test);
2408 __ cmpq(r8, rcx);
2409 __ j(less, &arguments_loop, Label::kNear);
2410
2411 // Return and remove the on-stack parameters.
2412 __ ret(3 * kPointerSize);
2413
2414 // Do the runtime call to allocate the arguments object.
2415 // rcx = argument count (untagged)
2416 __ bind(&runtime);
2417 __ Integer32ToSmi(rcx, rcx);
2418 __ movq(Operand(rsp, 1 * kPointerSize), rcx); // Patch argument count.
2419 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
2420}
2421
2422
2423void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
2424 // esp[0] : return address
2425 // esp[8] : number of parameters
2426 // esp[16] : receiver displacement
2427 // esp[24] : function
2428
2429 // Check if the calling frame is an arguments adaptor frame.
2430 Label runtime;
2431 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
2432 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2433 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
2434 __ j(not_equal, &runtime);
2435
2436 // Patch the arguments.length and the parameters pointer.
2437 __ movq(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2438 __ movq(Operand(rsp, 1 * kPointerSize), rcx);
2439 __ SmiToInteger64(rcx, rcx);
2440 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2441 StandardFrameConstants::kCallerSPOffset));
2442 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2443
2444 __ bind(&runtime);
2445 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
2446}
2447
2448
2449void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002450 // rsp[0] : return address
2451 // rsp[8] : number of parameters
2452 // rsp[16] : receiver displacement
2453 // rsp[24] : function
2454
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002455 // Check if the calling frame is an arguments adaptor frame.
2456 Label adaptor_frame, try_allocate, runtime;
2457 __ movq(rdx, Operand(rbp, StandardFrameConstants::kCallerFPOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002458 __ movq(rcx, Operand(rdx, StandardFrameConstants::kContextOffset));
2459 __ Cmp(rcx, Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002460 __ j(equal, &adaptor_frame);
2461
2462 // Get the length from the frame.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002463 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
2464 __ SmiToInteger64(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002465 __ jmp(&try_allocate);
2466
2467 // Patch the arguments.length and the parameters pointer.
2468 __ bind(&adaptor_frame);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002469 __ movq(rcx, Operand(rdx, ArgumentsAdaptorFrameConstants::kLengthOffset));
2470 __ movq(Operand(rsp, 1 * kPointerSize), rcx);
2471 __ SmiToInteger64(rcx, rcx);
2472 __ lea(rdx, Operand(rdx, rcx, times_pointer_size,
2473 StandardFrameConstants::kCallerSPOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002474 __ movq(Operand(rsp, 2 * kPointerSize), rdx);
2475
2476 // Try the new space allocation. Start out with computing the size of
2477 // the arguments object and the elements array.
2478 Label add_arguments_object;
2479 __ bind(&try_allocate);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002480 __ testq(rcx, rcx);
2481 __ j(zero, &add_arguments_object, Label::kNear);
2482 __ lea(rcx, Operand(rcx, times_pointer_size, FixedArray::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002483 __ bind(&add_arguments_object);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002484 __ addq(rcx, Immediate(Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002485
2486 // Do the allocation of both objects in one go.
2487 __ AllocateInNewSpace(rcx, rax, rdx, rbx, &runtime, TAG_OBJECT);
2488
2489 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002490 __ movq(rdi, Operand(rsi, Context::SlotOffset(Context::GLOBAL_INDEX)));
2491 __ movq(rdi, FieldOperand(rdi, GlobalObject::kGlobalContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002492 const int offset =
2493 Context::SlotOffset(Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX);
2494 __ movq(rdi, Operand(rdi, offset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002495
2496 // Copy the JS object part.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002497 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
2498 __ movq(rbx, FieldOperand(rdi, i));
2499 __ movq(FieldOperand(rax, i), rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01002500 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002501
2502 // Get the length (smi tagged) and set that as an in-object property too.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002503 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002504 __ movq(rcx, Operand(rsp, 1 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01002505 __ movq(FieldOperand(rax, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002506 Heap::kArgumentsLengthIndex * kPointerSize),
Steve Block44f0eee2011-05-26 01:26:41 +01002507 rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002508
2509 // If there are no actual arguments, we're done.
2510 Label done;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002511 __ testq(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002512 __ j(zero, &done);
2513
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002514 // Get the parameters pointer from the stack.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002515 __ movq(rdx, Operand(rsp, 2 * kPointerSize));
2516
2517 // Setup the elements pointer in the allocated arguments object and
2518 // initialize the header in the elements fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002519 __ lea(rdi, Operand(rax, Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002520 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rdi);
2521 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
2522 __ movq(FieldOperand(rdi, FixedArray::kMapOffset), kScratchRegister);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002523
2524
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002525 __ movq(FieldOperand(rdi, FixedArray::kLengthOffset), rcx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002526 // Untag the length for the loop below.
2527 __ SmiToInteger64(rcx, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002528
2529 // Copy the fixed array slots.
2530 Label loop;
2531 __ bind(&loop);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002532 __ movq(rbx, Operand(rdx, -1 * kPointerSize)); // Skip receiver.
2533 __ movq(FieldOperand(rdi, FixedArray::kHeaderSize), rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002534 __ addq(rdi, Immediate(kPointerSize));
2535 __ subq(rdx, Immediate(kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002536 __ decq(rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002537 __ j(not_zero, &loop);
2538
2539 // Return and remove the on-stack parameters.
2540 __ bind(&done);
2541 __ ret(3 * kPointerSize);
2542
2543 // Do the runtime call to allocate the arguments object.
2544 __ bind(&runtime);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002545 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002546}
2547
2548
2549void RegExpExecStub::Generate(MacroAssembler* masm) {
2550 // Just jump directly to runtime if native RegExp is not selected at compile
2551 // time or if regexp entry in generated code is turned off runtime switch or
2552 // at compilation.
2553#ifdef V8_INTERPRETED_REGEXP
2554 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2555#else // V8_INTERPRETED_REGEXP
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002556
2557 // Stack frame on entry.
Steve Block1e0659c2011-05-24 12:43:12 +01002558 // rsp[0]: return address
2559 // rsp[8]: last_match_info (expected JSArray)
2560 // rsp[16]: previous index
2561 // rsp[24]: subject string
2562 // rsp[32]: JSRegExp object
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002563
2564 static const int kLastMatchInfoOffset = 1 * kPointerSize;
2565 static const int kPreviousIndexOffset = 2 * kPointerSize;
2566 static const int kSubjectOffset = 3 * kPointerSize;
2567 static const int kJSRegExpOffset = 4 * kPointerSize;
2568
2569 Label runtime;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002570 // Ensure that a RegExp stack is allocated.
Steve Block44f0eee2011-05-26 01:26:41 +01002571 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002572 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01002573 ExternalReference::address_of_regexp_stack_memory_address(isolate);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002574 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01002575 ExternalReference::address_of_regexp_stack_memory_size(isolate);
2576 __ Load(kScratchRegister, address_of_regexp_stack_memory_size);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002577 __ testq(kScratchRegister, kScratchRegister);
2578 __ j(zero, &runtime);
2579
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002580 // Check that the first argument is a JSRegExp object.
2581 __ movq(rax, Operand(rsp, kJSRegExpOffset));
2582 __ JumpIfSmi(rax, &runtime);
2583 __ CmpObjectType(rax, JS_REGEXP_TYPE, kScratchRegister);
2584 __ j(not_equal, &runtime);
2585 // Check that the RegExp has been compiled (data contains a fixed array).
Steve Block44f0eee2011-05-26 01:26:41 +01002586 __ movq(rax, FieldOperand(rax, JSRegExp::kDataOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002587 if (FLAG_debug_code) {
Steve Block44f0eee2011-05-26 01:26:41 +01002588 Condition is_smi = masm->CheckSmi(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002589 __ Check(NegateCondition(is_smi),
2590 "Unexpected type for RegExp data, FixedArray expected");
Steve Block44f0eee2011-05-26 01:26:41 +01002591 __ CmpObjectType(rax, FIXED_ARRAY_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002592 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
2593 }
2594
Steve Block44f0eee2011-05-26 01:26:41 +01002595 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002596 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
Steve Block44f0eee2011-05-26 01:26:41 +01002597 __ SmiToInteger32(rbx, FieldOperand(rax, JSRegExp::kDataTagOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002598 __ cmpl(rbx, Immediate(JSRegExp::IRREGEXP));
2599 __ j(not_equal, &runtime);
2600
Steve Block44f0eee2011-05-26 01:26:41 +01002601 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002602 // Check that the number of captures fit in the static offsets vector buffer.
2603 __ SmiToInteger32(rdx,
Steve Block44f0eee2011-05-26 01:26:41 +01002604 FieldOperand(rax, JSRegExp::kIrregexpCaptureCountOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002605 // Calculate number of capture registers (number_of_captures + 1) * 2.
2606 __ leal(rdx, Operand(rdx, rdx, times_1, 2));
2607 // Check that the static offsets vector buffer is large enough.
2608 __ cmpl(rdx, Immediate(OffsetsVector::kStaticOffsetsVectorSize));
2609 __ j(above, &runtime);
2610
Steve Block44f0eee2011-05-26 01:26:41 +01002611 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002612 // rdx: Number of capture registers
2613 // Check that the second argument is a string.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002614 __ movq(rdi, Operand(rsp, kSubjectOffset));
2615 __ JumpIfSmi(rdi, &runtime);
2616 Condition is_string = masm->IsObjectStringType(rdi, rbx, rbx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002617 __ j(NegateCondition(is_string), &runtime);
2618
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002619 // rdi: Subject string.
2620 // rax: RegExp data (FixedArray).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002621 // rdx: Number of capture registers.
2622 // Check that the third argument is a positive smi less than the string
2623 // length. A negative value will be greater (unsigned comparison).
2624 __ movq(rbx, Operand(rsp, kPreviousIndexOffset));
2625 __ JumpIfNotSmi(rbx, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002626 __ SmiCompare(rbx, FieldOperand(rdi, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002627 __ j(above_equal, &runtime);
2628
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002629 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002630 // rdx: Number of capture registers
2631 // Check that the fourth object is a JSArray object.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002632 __ movq(rdi, Operand(rsp, kLastMatchInfoOffset));
2633 __ JumpIfSmi(rdi, &runtime);
2634 __ CmpObjectType(rdi, JS_ARRAY_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002635 __ j(not_equal, &runtime);
2636 // Check that the JSArray is in fast case.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002637 __ movq(rbx, FieldOperand(rdi, JSArray::kElementsOffset));
2638 __ movq(rdi, FieldOperand(rbx, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002639 __ CompareRoot(FieldOperand(rbx, HeapObject::kMapOffset),
2640 Heap::kFixedArrayMapRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002641 __ j(not_equal, &runtime);
2642 // Check that the last match info has space for the capture registers and the
2643 // additional information. Ensure no overflow in add.
2644 STATIC_ASSERT(FixedArray::kMaxLength < kMaxInt - FixedArray::kLengthOffset);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002645 __ SmiToInteger32(rdi, FieldOperand(rbx, FixedArray::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002646 __ addl(rdx, Immediate(RegExpImpl::kLastMatchOverhead));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002647 __ cmpl(rdx, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002648 __ j(greater, &runtime);
2649
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002650 // Reset offset for possibly sliced string.
2651 __ Set(r14, 0);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002652 // rax: RegExp data (FixedArray)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002653 // Check the representation and encoding of the subject string.
Ben Murdoch257744e2011-11-30 15:57:28 +00002654 Label seq_ascii_string, seq_two_byte_string, check_code;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002655 __ movq(rdi, Operand(rsp, kSubjectOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002656 // Make a copy of the original subject string.
2657 __ movq(r15, rdi);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002658 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002659 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
2660 // First check for flat two byte string.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002661 __ andb(rbx, Immediate(kIsNotStringMask |
2662 kStringRepresentationMask |
2663 kStringEncodingMask |
2664 kShortExternalStringMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002665 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002666 __ j(zero, &seq_two_byte_string, Label::kNear);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002667 // Any other flat string must be a flat ascii string. None of the following
2668 // string type tests will succeed if subject is not a string or a short
2669 // external string.
2670 __ andb(rbx, Immediate(kIsNotStringMask |
2671 kStringRepresentationMask |
2672 kShortExternalStringMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00002673 __ j(zero, &seq_ascii_string, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002674
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002675 // rbx: whether subject is a string and if yes, its string representation
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002676 // Check for flat cons string or sliced string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002677 // A flat cons string is a cons string where the second part is the empty
2678 // string. In that case the subject string is just the first part of the cons
2679 // string. Also in this case the first part of the cons string is known to be
2680 // a sequential string or an external string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002681 // In the case of a sliced string its offset has to be taken into account.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002682 Label cons_string, external_string, check_encoding;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002683 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
2684 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002685 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
2686 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002687 __ cmpq(rbx, Immediate(kExternalStringTag));
2688 __ j(less, &cons_string, Label::kNear);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002689 __ j(equal, &external_string);
2690
2691 // Catch non-string subject or short external string.
2692 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
2693 __ testb(rbx, Immediate(kIsNotStringMask | kShortExternalStringMask));
2694 __ j(not_zero, &runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002695
2696 // String is sliced.
2697 __ SmiToInteger32(r14, FieldOperand(rdi, SlicedString::kOffsetOffset));
2698 __ movq(rdi, FieldOperand(rdi, SlicedString::kParentOffset));
2699 // r14: slice offset
2700 // r15: original subject string
2701 // rdi: parent string
2702 __ jmp(&check_encoding, Label::kNear);
2703 // String is a cons string, check whether it is flat.
2704 __ bind(&cons_string);
Steve Block44f0eee2011-05-26 01:26:41 +01002705 __ CompareRoot(FieldOperand(rdi, ConsString::kSecondOffset),
2706 Heap::kEmptyStringRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002707 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002708 __ movq(rdi, FieldOperand(rdi, ConsString::kFirstOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002709 // rdi: first part of cons string or parent of sliced string.
2710 // rbx: map of first part of cons string or map of parent of sliced string.
2711 // Is first part of cons or parent of slice a flat two byte string?
2712 __ bind(&check_encoding);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002713 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002714 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
2715 Immediate(kStringRepresentationMask | kStringEncodingMask));
2716 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdoch257744e2011-11-30 15:57:28 +00002717 __ j(zero, &seq_two_byte_string, Label::kNear);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002718 // Any other flat string must be sequential ascii or external.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002719 __ testb(FieldOperand(rbx, Map::kInstanceTypeOffset),
2720 Immediate(kStringRepresentationMask));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002721 __ j(not_zero, &external_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002722
2723 __ bind(&seq_ascii_string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002724 // rdi: subject string (sequential ascii)
2725 // rax: RegExp data (FixedArray)
2726 __ movq(r11, FieldOperand(rax, JSRegExp::kDataAsciiCodeOffset));
2727 __ Set(rcx, 1); // Type is ascii.
Ben Murdoch257744e2011-11-30 15:57:28 +00002728 __ jmp(&check_code, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002729
2730 __ bind(&seq_two_byte_string);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002731 // rdi: subject string (flat two-byte)
2732 // rax: RegExp data (FixedArray)
2733 __ movq(r11, FieldOperand(rax, JSRegExp::kDataUC16CodeOffset));
2734 __ Set(rcx, 0); // Type is two byte.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002735
2736 __ bind(&check_code);
2737 // Check that the irregexp code has been generated for the actual string
2738 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00002739 // smi (code flushing support)
2740 __ JumpIfSmi(r11, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002741
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002742 // rdi: subject string
2743 // rcx: encoding of subject string (1 if ascii, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002744 // r11: code
2745 // Load used arguments before starting to push arguments for call to native
2746 // RegExp code to avoid handling changing stack height.
2747 __ SmiToInteger64(rbx, Operand(rsp, kPreviousIndexOffset));
2748
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002749 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002750 // rbx: previous index
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002751 // rcx: encoding of subject string (1 if ascii 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002752 // r11: code
2753 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01002754 Counters* counters = masm->isolate()->counters();
2755 __ IncrementCounter(counters->regexp_entry_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002756
Steve Block44f0eee2011-05-26 01:26:41 +01002757 // Isolates: note we add an additional parameter here (isolate pointer).
2758 static const int kRegExpExecuteArguments = 8;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002759 int argument_slots_on_stack =
2760 masm->ArgumentStackSlotsForCFunctionCall(kRegExpExecuteArguments);
Steve Block44f0eee2011-05-26 01:26:41 +01002761 __ EnterApiExitFrame(argument_slots_on_stack);
2762
2763 // Argument 8: Pass current isolate address.
2764 // __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
2765 // Immediate(ExternalReference::isolate_address()));
2766 __ LoadAddress(kScratchRegister, ExternalReference::isolate_address());
2767 __ movq(Operand(rsp, (argument_slots_on_stack - 1) * kPointerSize),
2768 kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002769
2770 // Argument 7: Indicate that this is a direct call from JavaScript.
Steve Block44f0eee2011-05-26 01:26:41 +01002771 __ movq(Operand(rsp, (argument_slots_on_stack - 2) * kPointerSize),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002772 Immediate(1));
2773
2774 // Argument 6: Start (high end) of backtracking stack memory area.
2775 __ movq(kScratchRegister, address_of_regexp_stack_memory_address);
2776 __ movq(r9, Operand(kScratchRegister, 0));
2777 __ movq(kScratchRegister, address_of_regexp_stack_memory_size);
2778 __ addq(r9, Operand(kScratchRegister, 0));
2779 // Argument 6 passed in r9 on Linux and on the stack on Windows.
2780#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01002781 __ movq(Operand(rsp, (argument_slots_on_stack - 3) * kPointerSize), r9);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002782#endif
2783
2784 // Argument 5: static offsets vector buffer.
Steve Block44f0eee2011-05-26 01:26:41 +01002785 __ LoadAddress(r8,
2786 ExternalReference::address_of_static_offsets_vector(isolate));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002787 // Argument 5 passed in r8 on Linux and on the stack on Windows.
2788#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01002789 __ movq(Operand(rsp, (argument_slots_on_stack - 4) * kPointerSize), r8);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002790#endif
2791
2792 // First four arguments are passed in registers on both Linux and Windows.
2793#ifdef _WIN64
2794 Register arg4 = r9;
2795 Register arg3 = r8;
2796 Register arg2 = rdx;
2797 Register arg1 = rcx;
2798#else
2799 Register arg4 = rcx;
2800 Register arg3 = rdx;
2801 Register arg2 = rsi;
2802 Register arg1 = rdi;
2803#endif
2804
2805 // Keep track on aliasing between argX defined above and the registers used.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002806 // rdi: subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002807 // rbx: previous index
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002808 // rcx: encoding of subject string (1 if ascii 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002809 // r11: code
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002810 // r14: slice offset
2811 // r15: original subject string
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002812
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002813 // Argument 2: Previous index.
2814 __ movq(arg2, rbx);
2815
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002816 // Argument 4: End of string data
2817 // Argument 3: Start of string data
2818 Label setup_two_byte, setup_rest, got_length, length_not_from_slice;
2819 // Prepare start and end index of the input.
2820 // Load the length from the original sliced string if that is the case.
2821 __ addq(rbx, r14);
2822 __ SmiToInteger32(arg3, FieldOperand(r15, String::kLengthOffset));
2823 __ addq(r14, arg3); // Using arg3 as scratch.
2824
2825 // rbx: start index of the input
2826 // r14: end index of the input
2827 // r15: original subject string
2828 __ testb(rcx, rcx); // Last use of rcx as encoding of subject string.
2829 __ j(zero, &setup_two_byte, Label::kNear);
2830 __ lea(arg4, FieldOperand(rdi, r14, times_1, SeqAsciiString::kHeaderSize));
2831 __ lea(arg3, FieldOperand(rdi, rbx, times_1, SeqAsciiString::kHeaderSize));
2832 __ jmp(&setup_rest, Label::kNear);
2833 __ bind(&setup_two_byte);
2834 __ lea(arg4, FieldOperand(rdi, r14, times_2, SeqTwoByteString::kHeaderSize));
2835 __ lea(arg3, FieldOperand(rdi, rbx, times_2, SeqTwoByteString::kHeaderSize));
2836 __ bind(&setup_rest);
2837
2838 // Argument 1: Original subject string.
2839 // The original subject is in the previous stack frame. Therefore we have to
2840 // use rbp, which points exactly to one pointer size below the previous rsp.
2841 // (Because creating a new stack frame pushes the previous rbp onto the stack
2842 // and thereby moves up rsp by one kPointerSize.)
2843 __ movq(arg1, r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002844
2845 // Locate the code entry and call it.
2846 __ addq(r11, Immediate(Code::kHeaderSize - kHeapObjectTag));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002847 __ call(r11);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002848
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002849 __ LeaveApiExitFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002850
2851 // Check the result.
Ben Murdoch257744e2011-11-30 15:57:28 +00002852 Label success;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002853 Label exception;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002854 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::SUCCESS));
Ben Murdoch257744e2011-11-30 15:57:28 +00002855 __ j(equal, &success, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002856 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::EXCEPTION));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002857 __ j(equal, &exception);
2858 __ cmpl(rax, Immediate(NativeRegExpMacroAssembler::FAILURE));
2859 // If none of the above, it can only be retry.
2860 // Handle that in the runtime system.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002861 __ j(not_equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002862
2863 // For failure return null.
2864 __ LoadRoot(rax, Heap::kNullValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002865 __ ret(4 * kPointerSize);
2866
2867 // Load RegExp data.
2868 __ bind(&success);
2869 __ movq(rax, Operand(rsp, kJSRegExpOffset));
2870 __ movq(rcx, FieldOperand(rax, JSRegExp::kDataOffset));
2871 __ SmiToInteger32(rax,
2872 FieldOperand(rcx, JSRegExp::kIrregexpCaptureCountOffset));
2873 // Calculate number of capture registers (number_of_captures + 1) * 2.
2874 __ leal(rdx, Operand(rax, rax, times_1, 2));
2875
2876 // rdx: Number of capture registers
2877 // Load last_match_info which is still known to be a fast case JSArray.
2878 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
2879 __ movq(rbx, FieldOperand(rax, JSArray::kElementsOffset));
2880
2881 // rbx: last_match_info backing store (FixedArray)
2882 // rdx: number of capture registers
2883 // Store the capture count.
2884 __ Integer32ToSmi(kScratchRegister, rdx);
2885 __ movq(FieldOperand(rbx, RegExpImpl::kLastCaptureCountOffset),
2886 kScratchRegister);
2887 // Store last subject and last input.
2888 __ movq(rax, Operand(rsp, kSubjectOffset));
2889 __ movq(FieldOperand(rbx, RegExpImpl::kLastSubjectOffset), rax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002890 __ RecordWriteField(rbx,
2891 RegExpImpl::kLastSubjectOffset,
2892 rax,
2893 rdi,
2894 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002895 __ movq(rax, Operand(rsp, kSubjectOffset));
2896 __ movq(FieldOperand(rbx, RegExpImpl::kLastInputOffset), rax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002897 __ RecordWriteField(rbx,
2898 RegExpImpl::kLastInputOffset,
2899 rax,
2900 rdi,
2901 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002902
2903 // Get the static offsets vector filled by the native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01002904 __ LoadAddress(rcx,
2905 ExternalReference::address_of_static_offsets_vector(isolate));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002906
2907 // rbx: last_match_info backing store (FixedArray)
2908 // rcx: offsets vector
2909 // rdx: number of capture registers
Ben Murdoch257744e2011-11-30 15:57:28 +00002910 Label next_capture, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002911 // Capture register counter starts from number of capture registers and
2912 // counts down until wraping after zero.
2913 __ bind(&next_capture);
2914 __ subq(rdx, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00002915 __ j(negative, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002916 // Read the value from the static offsets vector buffer and make it a smi.
2917 __ movl(rdi, Operand(rcx, rdx, times_int_size, 0));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01002918 __ Integer32ToSmi(rdi, rdi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002919 // Store the smi value in the last match info.
2920 __ movq(FieldOperand(rbx,
2921 rdx,
2922 times_pointer_size,
2923 RegExpImpl::kFirstCaptureOffset),
2924 rdi);
2925 __ jmp(&next_capture);
2926 __ bind(&done);
2927
2928 // Return last match info.
2929 __ movq(rax, Operand(rsp, kLastMatchInfoOffset));
2930 __ ret(4 * kPointerSize);
2931
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002932 __ bind(&exception);
2933 // Result must now be exception. If there is no pending exception already a
2934 // stack overflow (on the backtrack stack) was detected in RegExp code but
2935 // haven't created the exception yet. Handle that in the runtime system.
2936 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Steve Block44f0eee2011-05-26 01:26:41 +01002937 ExternalReference pending_exception_address(
Ben Murdoch589d6972011-11-30 16:04:58 +00002938 Isolate::kPendingExceptionAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01002939 Operand pending_exception_operand =
2940 masm->ExternalOperand(pending_exception_address, rbx);
2941 __ movq(rax, pending_exception_operand);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002942 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
2943 __ cmpq(rax, rdx);
2944 __ j(equal, &runtime);
Steve Block44f0eee2011-05-26 01:26:41 +01002945 __ movq(pending_exception_operand, rdx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002946
2947 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00002948 Label termination_exception;
2949 __ j(equal, &termination_exception, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002950 __ Throw(rax);
2951
2952 __ bind(&termination_exception);
2953 __ ThrowUncatchable(TERMINATION, rax);
2954
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002955 // External string. Short external strings have already been ruled out.
2956 // rdi: subject string (expected to be external)
2957 // rbx: scratch
2958 __ bind(&external_string);
2959 __ movq(rbx, FieldOperand(rdi, HeapObject::kMapOffset));
2960 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
2961 if (FLAG_debug_code) {
2962 // Assert that we do not have a cons or slice (indirect strings) here.
2963 // Sequential strings have already been ruled out.
2964 __ testb(rbx, Immediate(kIsIndirectStringMask));
2965 __ Assert(zero, "external string expected, but not found");
2966 }
2967 __ movq(rdi, FieldOperand(rdi, ExternalString::kResourceDataOffset));
2968 // Move the pointer so that offset-wise, it looks like a sequential string.
2969 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize);
2970 __ subq(rdi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
2971 STATIC_ASSERT(kTwoByteStringTag == 0);
2972 __ testb(rbx, Immediate(kStringEncodingMask));
2973 __ j(not_zero, &seq_ascii_string);
2974 __ jmp(&seq_two_byte_string);
2975
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002976 // Do the runtime call to execute the regexp.
2977 __ bind(&runtime);
2978 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
2979#endif // V8_INTERPRETED_REGEXP
2980}
2981
2982
Ben Murdochb0fe1622011-05-05 13:52:32 +01002983void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
2984 const int kMaxInlineLength = 100;
2985 Label slowcase;
2986 Label done;
2987 __ movq(r8, Operand(rsp, kPointerSize * 3));
2988 __ JumpIfNotSmi(r8, &slowcase);
2989 __ SmiToInteger32(rbx, r8);
2990 __ cmpl(rbx, Immediate(kMaxInlineLength));
2991 __ j(above, &slowcase);
2992 // Smi-tagging is equivalent to multiplying by 2.
2993 STATIC_ASSERT(kSmiTag == 0);
2994 STATIC_ASSERT(kSmiTagSize == 1);
Steve Block1e0659c2011-05-24 12:43:12 +01002995 // Allocate RegExpResult followed by FixedArray with size in rbx.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002996 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
2997 // Elements: [Map][Length][..elements..]
2998 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
2999 times_pointer_size,
3000 rbx, // In: Number of elements.
3001 rax, // Out: Start of allocation (tagged).
3002 rcx, // Out: End of allocation.
3003 rdx, // Scratch register
3004 &slowcase,
3005 TAG_OBJECT);
3006 // rax: Start of allocated area, object-tagged.
3007 // rbx: Number of array elements as int32.
3008 // r8: Number of array elements as smi.
3009
3010 // Set JSArray map to global.regexp_result_map().
3011 __ movq(rdx, ContextOperand(rsi, Context::GLOBAL_INDEX));
3012 __ movq(rdx, FieldOperand(rdx, GlobalObject::kGlobalContextOffset));
3013 __ movq(rdx, ContextOperand(rdx, Context::REGEXP_RESULT_MAP_INDEX));
3014 __ movq(FieldOperand(rax, HeapObject::kMapOffset), rdx);
3015
3016 // Set empty properties FixedArray.
Steve Block44f0eee2011-05-26 01:26:41 +01003017 __ LoadRoot(kScratchRegister, Heap::kEmptyFixedArrayRootIndex);
3018 __ movq(FieldOperand(rax, JSObject::kPropertiesOffset), kScratchRegister);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003019
3020 // Set elements to point to FixedArray allocated right after the JSArray.
3021 __ lea(rcx, Operand(rax, JSRegExpResult::kSize));
3022 __ movq(FieldOperand(rax, JSObject::kElementsOffset), rcx);
3023
3024 // Set input, index and length fields from arguments.
3025 __ movq(r8, Operand(rsp, kPointerSize * 1));
3026 __ movq(FieldOperand(rax, JSRegExpResult::kInputOffset), r8);
3027 __ movq(r8, Operand(rsp, kPointerSize * 2));
3028 __ movq(FieldOperand(rax, JSRegExpResult::kIndexOffset), r8);
3029 __ movq(r8, Operand(rsp, kPointerSize * 3));
3030 __ movq(FieldOperand(rax, JSArray::kLengthOffset), r8);
3031
3032 // Fill out the elements FixedArray.
3033 // rax: JSArray.
3034 // rcx: FixedArray.
3035 // rbx: Number of elements in array as int32.
3036
3037 // Set map.
Steve Block44f0eee2011-05-26 01:26:41 +01003038 __ LoadRoot(kScratchRegister, Heap::kFixedArrayMapRootIndex);
3039 __ movq(FieldOperand(rcx, HeapObject::kMapOffset), kScratchRegister);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003040 // Set length.
3041 __ Integer32ToSmi(rdx, rbx);
3042 __ movq(FieldOperand(rcx, FixedArray::kLengthOffset), rdx);
3043 // Fill contents of fixed-array with the-hole.
Steve Block44f0eee2011-05-26 01:26:41 +01003044 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003045 __ lea(rcx, FieldOperand(rcx, FixedArray::kHeaderSize));
3046 // Fill fixed array elements with hole.
3047 // rax: JSArray.
3048 // rbx: Number of elements in array that remains to be filled, as int32.
3049 // rcx: Start of elements in FixedArray.
3050 // rdx: the hole.
3051 Label loop;
3052 __ testl(rbx, rbx);
3053 __ bind(&loop);
Steve Block1e0659c2011-05-24 12:43:12 +01003054 __ j(less_equal, &done); // Jump if rcx is negative or zero.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003055 __ subl(rbx, Immediate(1));
3056 __ movq(Operand(rcx, rbx, times_pointer_size, 0), rdx);
3057 __ jmp(&loop);
3058
3059 __ bind(&done);
3060 __ ret(3 * kPointerSize);
3061
3062 __ bind(&slowcase);
3063 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
3064}
3065
3066
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003067void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
3068 Register object,
3069 Register result,
3070 Register scratch1,
3071 Register scratch2,
3072 bool object_is_smi,
3073 Label* not_found) {
3074 // Use of registers. Register result is used as a temporary.
3075 Register number_string_cache = result;
3076 Register mask = scratch1;
3077 Register scratch = scratch2;
3078
3079 // Load the number string cache.
3080 __ LoadRoot(number_string_cache, Heap::kNumberStringCacheRootIndex);
3081
3082 // Make the hash mask from the length of the number string cache. It
3083 // contains two elements (number and string) for each cache entry.
3084 __ SmiToInteger32(
3085 mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
3086 __ shrl(mask, Immediate(1));
3087 __ subq(mask, Immediate(1)); // Make mask.
3088
3089 // Calculate the entry in the number string cache. The hash value in the
3090 // number string cache for smis is just the smi value, and the hash for
3091 // doubles is the xor of the upper and lower words. See
3092 // Heap::GetNumberStringCache.
3093 Label is_smi;
3094 Label load_result_from_cache;
Ben Murdoch257744e2011-11-30 15:57:28 +00003095 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003096 if (!object_is_smi) {
3097 __ JumpIfSmi(object, &is_smi);
Ben Murdoch257744e2011-11-30 15:57:28 +00003098 __ CheckMap(object,
3099 factory->heap_number_map(),
3100 not_found,
3101 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003102
3103 STATIC_ASSERT(8 == kDoubleSize);
3104 __ movl(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
3105 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset));
3106 GenerateConvertHashCodeToIndex(masm, scratch, mask);
3107
3108 Register index = scratch;
3109 Register probe = mask;
3110 __ movq(probe,
3111 FieldOperand(number_string_cache,
3112 index,
3113 times_1,
3114 FixedArray::kHeaderSize));
3115 __ JumpIfSmi(probe, not_found);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003116 __ movsd(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
3117 __ movsd(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
3118 __ ucomisd(xmm0, xmm1);
3119 __ j(parity_even, not_found); // Bail out if NaN is involved.
3120 __ j(not_equal, not_found); // The cache did not contain this value.
3121 __ jmp(&load_result_from_cache);
3122 }
3123
3124 __ bind(&is_smi);
3125 __ SmiToInteger32(scratch, object);
3126 GenerateConvertHashCodeToIndex(masm, scratch, mask);
3127
3128 Register index = scratch;
3129 // Check if the entry is the smi we are looking for.
3130 __ cmpq(object,
3131 FieldOperand(number_string_cache,
3132 index,
3133 times_1,
3134 FixedArray::kHeaderSize));
3135 __ j(not_equal, not_found);
3136
3137 // Get the result from the cache.
3138 __ bind(&load_result_from_cache);
3139 __ movq(result,
3140 FieldOperand(number_string_cache,
3141 index,
3142 times_1,
3143 FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01003144 Counters* counters = masm->isolate()->counters();
3145 __ IncrementCounter(counters->number_to_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003146}
3147
3148
3149void NumberToStringStub::GenerateConvertHashCodeToIndex(MacroAssembler* masm,
3150 Register hash,
3151 Register mask) {
3152 __ and_(hash, mask);
3153 // Each entry in string cache consists of two pointer sized fields,
3154 // but times_twice_pointer_size (multiplication by 16) scale factor
3155 // is not supported by addrmode on x64 platform.
3156 // So we have to premultiply entry index before lookup.
3157 __ shl(hash, Immediate(kPointerSizeLog2 + 1));
3158}
3159
3160
3161void NumberToStringStub::Generate(MacroAssembler* masm) {
3162 Label runtime;
3163
3164 __ movq(rbx, Operand(rsp, kPointerSize));
3165
3166 // Generate code to lookup number in the number string cache.
3167 GenerateLookupNumberStringCache(masm, rbx, rax, r8, r9, false, &runtime);
3168 __ ret(1 * kPointerSize);
3169
3170 __ bind(&runtime);
3171 // Handle number to string in the runtime system if not found in the cache.
3172 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
3173}
3174
3175
3176static int NegativeComparisonResult(Condition cc) {
3177 ASSERT(cc != equal);
3178 ASSERT((cc == less) || (cc == less_equal)
3179 || (cc == greater) || (cc == greater_equal));
3180 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
3181}
3182
3183
3184void CompareStub::Generate(MacroAssembler* masm) {
3185 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
3186
3187 Label check_unequal_objects, done;
Ben Murdoch257744e2011-11-30 15:57:28 +00003188 Factory* factory = masm->isolate()->factory();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003189
3190 // Compare two smis if required.
3191 if (include_smi_compare_) {
3192 Label non_smi, smi_done;
3193 __ JumpIfNotBothSmi(rax, rdx, &non_smi);
3194 __ subq(rdx, rax);
3195 __ j(no_overflow, &smi_done);
Ben Murdochf87a2032010-10-22 12:50:53 +01003196 __ not_(rdx); // Correct sign in case of overflow. rdx cannot be 0 here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01003197 __ bind(&smi_done);
3198 __ movq(rax, rdx);
3199 __ ret(0);
3200 __ bind(&non_smi);
3201 } else if (FLAG_debug_code) {
3202 Label ok;
3203 __ JumpIfNotSmi(rdx, &ok);
3204 __ JumpIfNotSmi(rax, &ok);
3205 __ Abort("CompareStub: smi operands");
3206 __ bind(&ok);
3207 }
3208
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003209 // The compare stub returns a positive, negative, or zero 64-bit integer
3210 // value in rax, corresponding to result of comparing the two inputs.
3211 // NOTICE! This code is only reached after a smi-fast-case check, so
3212 // it is certain that at least one operand isn't a smi.
3213
3214 // Two identical objects are equal unless they are both NaN or undefined.
3215 {
Ben Murdoch257744e2011-11-30 15:57:28 +00003216 Label not_identical;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003217 __ cmpq(rax, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003218 __ j(not_equal, &not_identical, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003219
3220 if (cc_ != equal) {
3221 // Check for undefined. undefined OP undefined is false even though
3222 // undefined == undefined.
Ben Murdoch257744e2011-11-30 15:57:28 +00003223 Label check_for_nan;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003224 __ CompareRoot(rdx, Heap::kUndefinedValueRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00003225 __ j(not_equal, &check_for_nan, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003226 __ Set(rax, NegativeComparisonResult(cc_));
3227 __ ret(0);
3228 __ bind(&check_for_nan);
3229 }
3230
Steve Block44f0eee2011-05-26 01:26:41 +01003231 // Test for NaN. Sadly, we can't just compare to FACTORY->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003232 // so we do the second best thing - test it ourselves.
3233 // Note: if cc_ != equal, never_nan_nan_ is not used.
3234 // We cannot set rax to EQUAL until just before return because
3235 // rax must be unchanged on jump to not_identical.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003236 if (never_nan_nan_ && (cc_ == equal)) {
3237 __ Set(rax, EQUAL);
3238 __ ret(0);
3239 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00003240 Label heap_number;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003241 // If it's not a heap number, then return equal for (in)equality operator.
3242 __ Cmp(FieldOperand(rdx, HeapObject::kMapOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00003243 factory->heap_number_map());
3244 __ j(equal, &heap_number, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003245 if (cc_ != equal) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003246 // Call runtime on identical objects. Otherwise return equal.
3247 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003248 __ j(above_equal, &not_identical, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003249 }
3250 __ Set(rax, EQUAL);
3251 __ ret(0);
3252
3253 __ bind(&heap_number);
3254 // It is a heap number, so return equal if it's not NaN.
3255 // For NaN, return 1 for every condition except greater and
3256 // greater-equal. Return -1 for them, so the comparison yields
3257 // false for all conditions except not-equal.
3258 __ Set(rax, EQUAL);
3259 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
3260 __ ucomisd(xmm0, xmm0);
3261 __ setcc(parity_even, rax);
3262 // rax is 0 for equal non-NaN heapnumbers, 1 for NaNs.
3263 if (cc_ == greater_equal || cc_ == greater) {
3264 __ neg(rax);
3265 }
3266 __ ret(0);
3267 }
3268
3269 __ bind(&not_identical);
3270 }
3271
3272 if (cc_ == equal) { // Both strict and non-strict.
3273 Label slow; // Fallthrough label.
3274
3275 // If we're doing a strict equality comparison, we don't have to do
3276 // type conversion, so we generate code to do fast comparison for objects
3277 // and oddballs. Non-smi numbers and strings still go through the usual
3278 // slow-case code.
3279 if (strict_) {
3280 // If either is a Smi (we know that not both are), then they can only
3281 // be equal if the other is a HeapNumber. If so, use the slow case.
3282 {
3283 Label not_smis;
3284 __ SelectNonSmi(rbx, rax, rdx, &not_smis);
3285
3286 // Check if the non-smi operand is a heap number.
3287 __ Cmp(FieldOperand(rbx, HeapObject::kMapOffset),
Ben Murdoch257744e2011-11-30 15:57:28 +00003288 factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003289 // If heap number, handle it in the slow case.
3290 __ j(equal, &slow);
3291 // Return non-equal. ebx (the lower half of rbx) is not zero.
3292 __ movq(rax, rbx);
3293 __ ret(0);
3294
3295 __ bind(&not_smis);
3296 }
3297
3298 // If either operand is a JSObject or an oddball value, then they are not
3299 // equal since their pointers are different
3300 // There is no test for undetectability in strict equality.
3301
3302 // If the first object is a JS object, we have done pointer comparison.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003303 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
Ben Murdoch257744e2011-11-30 15:57:28 +00003304 Label first_non_object;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003305 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003306 __ j(below, &first_non_object, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003307 // Return non-zero (eax (not rax) is not zero)
3308 Label return_not_equal;
3309 STATIC_ASSERT(kHeapObjectTag != 0);
3310 __ bind(&return_not_equal);
3311 __ ret(0);
3312
3313 __ bind(&first_non_object);
3314 // Check for oddballs: true, false, null, undefined.
3315 __ CmpInstanceType(rcx, ODDBALL_TYPE);
3316 __ j(equal, &return_not_equal);
3317
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003318 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003319 __ j(above_equal, &return_not_equal);
3320
3321 // Check for oddballs: true, false, null, undefined.
3322 __ CmpInstanceType(rcx, ODDBALL_TYPE);
3323 __ j(equal, &return_not_equal);
3324
3325 // Fall through to the general case.
3326 }
3327 __ bind(&slow);
3328 }
3329
3330 // Generate the number comparison code.
3331 if (include_number_compare_) {
3332 Label non_number_comparison;
Ben Murdoch257744e2011-11-30 15:57:28 +00003333 Label unordered;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003334 FloatingPointHelper::LoadSSE2UnknownOperands(masm, &non_number_comparison);
3335 __ xorl(rax, rax);
3336 __ xorl(rcx, rcx);
3337 __ ucomisd(xmm0, xmm1);
3338
3339 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch257744e2011-11-30 15:57:28 +00003340 __ j(parity_even, &unordered, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003341 // Return a result of -1, 0, or 1, based on EFLAGS.
3342 __ setcc(above, rax);
3343 __ setcc(below, rcx);
3344 __ subq(rax, rcx);
3345 __ ret(0);
3346
3347 // If one of the numbers was NaN, then the result is always false.
3348 // The cc is never not-equal.
3349 __ bind(&unordered);
3350 ASSERT(cc_ != not_equal);
3351 if (cc_ == less || cc_ == less_equal) {
3352 __ Set(rax, 1);
3353 } else {
3354 __ Set(rax, -1);
3355 }
3356 __ ret(0);
3357
3358 // The number comparison code did not provide a valid result.
3359 __ bind(&non_number_comparison);
3360 }
3361
3362 // Fast negative check for symbol-to-symbol equality.
3363 Label check_for_strings;
3364 if (cc_ == equal) {
3365 BranchIfNonSymbol(masm, &check_for_strings, rax, kScratchRegister);
3366 BranchIfNonSymbol(masm, &check_for_strings, rdx, kScratchRegister);
3367
3368 // We've already checked for object identity, so if both operands
3369 // are symbols they aren't equal. Register eax (not rax) already holds a
3370 // non-zero value, which indicates not equal, so just return.
3371 __ ret(0);
3372 }
3373
3374 __ bind(&check_for_strings);
3375
3376 __ JumpIfNotBothSequentialAsciiStrings(
3377 rdx, rax, rcx, rbx, &check_unequal_objects);
3378
3379 // Inline comparison of ascii strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00003380 if (cc_ == equal) {
3381 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003382 rdx,
3383 rax,
3384 rcx,
Ben Murdoch257744e2011-11-30 15:57:28 +00003385 rbx);
3386 } else {
3387 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
3388 rdx,
3389 rax,
3390 rcx,
3391 rbx,
3392 rdi,
3393 r8);
3394 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003395
3396#ifdef DEBUG
3397 __ Abort("Unexpected fall-through from string comparison");
3398#endif
3399
3400 __ bind(&check_unequal_objects);
3401 if (cc_ == equal && !strict_) {
3402 // Not strict equality. Objects are unequal if
3403 // they are both JSObjects and not undetectable,
3404 // and their pointers are different.
Ben Murdoch257744e2011-11-30 15:57:28 +00003405 Label not_both_objects, return_unequal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003406 // At most one is a smi, so we can test for smi by adding the two.
3407 // A smi plus a heap object has the low bit set, a heap object plus
3408 // a heap object has the low bit clear.
3409 STATIC_ASSERT(kSmiTag == 0);
3410 STATIC_ASSERT(kSmiTagMask == 1);
3411 __ lea(rcx, Operand(rax, rdx, times_1, 0));
3412 __ testb(rcx, Immediate(kSmiTagMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00003413 __ j(not_zero, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003414 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003415 __ j(below, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003416 __ CmpObjectType(rdx, FIRST_SPEC_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003417 __ j(below, &not_both_objects, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003418 __ testb(FieldOperand(rbx, Map::kBitFieldOffset),
3419 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00003420 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003421 __ testb(FieldOperand(rcx, Map::kBitFieldOffset),
3422 Immediate(1 << Map::kIsUndetectable));
Ben Murdoch257744e2011-11-30 15:57:28 +00003423 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003424 // The objects are both undetectable, so they both compare as the value
3425 // undefined, and are equal.
3426 __ Set(rax, EQUAL);
3427 __ bind(&return_unequal);
Steve Block1e0659c2011-05-24 12:43:12 +01003428 // Return non-equal by returning the non-zero object pointer in rax,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003429 // or return equal if we fell through to here.
3430 __ ret(0);
3431 __ bind(&not_both_objects);
3432 }
3433
3434 // Push arguments below the return address to prepare jump to builtin.
3435 __ pop(rcx);
3436 __ push(rdx);
3437 __ push(rax);
3438
3439 // Figure out which native to call and setup the arguments.
3440 Builtins::JavaScript builtin;
3441 if (cc_ == equal) {
3442 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
3443 } else {
3444 builtin = Builtins::COMPARE;
3445 __ Push(Smi::FromInt(NegativeComparisonResult(cc_)));
3446 }
3447
3448 // Restore return address on the stack.
3449 __ push(rcx);
3450
3451 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
3452 // tagged as a small integer.
3453 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
3454}
3455
3456
3457void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
3458 Label* label,
3459 Register object,
3460 Register scratch) {
3461 __ JumpIfSmi(object, label);
3462 __ movq(scratch, FieldOperand(object, HeapObject::kMapOffset));
3463 __ movzxbq(scratch,
3464 FieldOperand(scratch, Map::kInstanceTypeOffset));
3465 // Ensure that no non-strings have the symbol bit set.
3466 STATIC_ASSERT(LAST_TYPE < kNotStringTag + kIsSymbolMask);
3467 STATIC_ASSERT(kSymbolTag != 0);
3468 __ testb(scratch, Immediate(kIsSymbolMask));
3469 __ j(zero, label);
3470}
3471
3472
3473void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01003474 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003475}
3476
3477
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003478void CallFunctionStub::FinishCode(Handle<Code> code) {
3479 code->set_has_function_cache(false);
3480}
3481
3482
3483void CallFunctionStub::Clear(Heap* heap, Address address) {
3484 UNREACHABLE();
3485}
3486
3487
3488Object* CallFunctionStub::GetCachedValue(Address address) {
3489 UNREACHABLE();
3490 return NULL;
3491}
3492
3493
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003494void CallFunctionStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003495 // rdi : the function to call
Ben Murdoch589d6972011-11-30 16:04:58 +00003496 Label slow, non_function;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003497
Ben Murdoch257744e2011-11-30 15:57:28 +00003498 // The receiver might implicitly be the global object. This is
3499 // indicated by passing the hole as the receiver to the call
3500 // function stub.
3501 if (ReceiverMightBeImplicit()) {
3502 Label call;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003503 // Get the receiver from the stack.
3504 // +1 ~ return address
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003505 __ movq(rax, Operand(rsp, (argc_ + 1) * kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00003506 // Call as function is indicated with the hole.
3507 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
3508 __ j(not_equal, &call, Label::kNear);
3509 // Patch the receiver on the stack with the global receiver object.
3510 __ movq(rbx, GlobalObjectOperand());
3511 __ movq(rbx, FieldOperand(rbx, GlobalObject::kGlobalReceiverOffset));
3512 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rbx);
3513 __ bind(&call);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003514 }
3515
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003516 // Check that the function really is a JavaScript function.
Ben Murdoch589d6972011-11-30 16:04:58 +00003517 __ JumpIfSmi(rdi, &non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003518 // Goto slow case if we do not have a function.
3519 __ CmpObjectType(rdi, JS_FUNCTION_TYPE, rcx);
3520 __ j(not_equal, &slow);
3521
3522 // Fast-case: Just invoke the function.
3523 ParameterCount actual(argc_);
Ben Murdoch257744e2011-11-30 15:57:28 +00003524
3525 if (ReceiverMightBeImplicit()) {
3526 Label call_as_function;
3527 __ CompareRoot(rax, Heap::kTheHoleValueRootIndex);
3528 __ j(equal, &call_as_function);
3529 __ InvokeFunction(rdi,
3530 actual,
3531 JUMP_FUNCTION,
3532 NullCallWrapper(),
3533 CALL_AS_METHOD);
3534 __ bind(&call_as_function);
3535 }
3536 __ InvokeFunction(rdi,
3537 actual,
3538 JUMP_FUNCTION,
3539 NullCallWrapper(),
3540 CALL_AS_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003541
3542 // Slow-case: Non-function called.
3543 __ bind(&slow);
Ben Murdoch589d6972011-11-30 16:04:58 +00003544 // Check for function proxy.
3545 __ CmpInstanceType(rcx, JS_FUNCTION_PROXY_TYPE);
3546 __ j(not_equal, &non_function);
3547 __ pop(rcx);
3548 __ push(rdi); // put proxy as additional argument under return address
3549 __ push(rcx);
3550 __ Set(rax, argc_ + 1);
3551 __ Set(rbx, 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003552 __ SetCallKind(rcx, CALL_AS_METHOD);
Ben Murdoch589d6972011-11-30 16:04:58 +00003553 __ GetBuiltinEntry(rdx, Builtins::CALL_FUNCTION_PROXY);
3554 {
3555 Handle<Code> adaptor =
3556 masm->isolate()->builtins()->ArgumentsAdaptorTrampoline();
3557 __ jmp(adaptor, RelocInfo::CODE_TARGET);
3558 }
3559
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003560 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
3561 // of the original receiver from the call site).
Ben Murdoch589d6972011-11-30 16:04:58 +00003562 __ bind(&non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003563 __ movq(Operand(rsp, (argc_ + 1) * kPointerSize), rdi);
3564 __ Set(rax, argc_);
3565 __ Set(rbx, 0);
Ben Murdoch589d6972011-11-30 16:04:58 +00003566 __ SetCallKind(rcx, CALL_AS_METHOD);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003567 __ GetBuiltinEntry(rdx, Builtins::CALL_NON_FUNCTION);
Steve Block44f0eee2011-05-26 01:26:41 +01003568 Handle<Code> adaptor =
3569 Isolate::Current()->builtins()->ArgumentsAdaptorTrampoline();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003570 __ Jump(adaptor, RelocInfo::CODE_TARGET);
3571}
3572
3573
Steve Block44f0eee2011-05-26 01:26:41 +01003574bool CEntryStub::NeedsImmovableCode() {
3575 return false;
3576}
3577
3578
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003579bool CEntryStub::IsPregenerated() {
3580#ifdef _WIN64
3581 return result_size_ == 1;
3582#else
3583 return true;
3584#endif
3585}
3586
3587
3588void CodeStub::GenerateStubsAheadOfTime() {
3589 CEntryStub::GenerateAheadOfTime();
3590 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime();
3591 // It is important that the store buffer overflow stubs are generated first.
3592 RecordWriteStub::GenerateFixedRegStubsAheadOfTime();
3593}
3594
3595
3596void CodeStub::GenerateFPStubs() {
3597}
3598
3599
3600void CEntryStub::GenerateAheadOfTime() {
3601 CEntryStub stub(1, kDontSaveFPRegs);
3602 stub.GetCode()->set_is_pregenerated(true);
3603 CEntryStub save_doubles(1, kSaveFPRegs);
3604 save_doubles.GetCode()->set_is_pregenerated(true);
3605}
3606
3607
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003608void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003609 // Throw exception in eax.
3610 __ Throw(rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003611}
3612
3613
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003614void CEntryStub::GenerateCore(MacroAssembler* masm,
3615 Label* throw_normal_exception,
3616 Label* throw_termination_exception,
3617 Label* throw_out_of_memory_exception,
3618 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01003619 bool always_allocate_scope) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003620 // rax: result parameter for PerformGC, if any.
3621 // rbx: pointer to C function (C callee-saved).
3622 // rbp: frame pointer (restored after C call).
3623 // rsp: stack pointer (restored after C call).
3624 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01003625 // r15: pointer to the first argument (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003626 // This pointer is reused in LeaveExitFrame(), so it is stored in a
3627 // callee-saved register.
3628
3629 // Simple results returned in rax (both AMD64 and Win64 calling conventions).
3630 // Complex results must be written to address passed as first argument.
3631 // AMD64 calling convention: a struct of two pointers in rax+rdx
3632
3633 // Check stack alignment.
3634 if (FLAG_debug_code) {
3635 __ CheckStackAlignment();
3636 }
3637
3638 if (do_gc) {
3639 // Pass failure code returned from last attempt as first argument to
3640 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
3641 // stack is known to be aligned. This function takes one argument which is
3642 // passed in register.
3643#ifdef _WIN64
3644 __ movq(rcx, rax);
3645#else // _WIN64
3646 __ movq(rdi, rax);
3647#endif
3648 __ movq(kScratchRegister,
3649 FUNCTION_ADDR(Runtime::PerformGC),
3650 RelocInfo::RUNTIME_ENTRY);
3651 __ call(kScratchRegister);
3652 }
3653
3654 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01003655 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003656 if (always_allocate_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +01003657 Operand scope_depth_operand = masm->ExternalOperand(scope_depth);
3658 __ incl(scope_depth_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003659 }
3660
3661 // Call C function.
3662#ifdef _WIN64
3663 // Windows 64-bit ABI passes arguments in rcx, rdx, r8, r9
3664 // Store Arguments object on stack, below the 4 WIN64 ABI parameter slots.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003665 __ movq(StackSpaceOperand(0), r14); // argc.
Steve Block44f0eee2011-05-26 01:26:41 +01003666 __ movq(StackSpaceOperand(1), r15); // argv.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003667 if (result_size_ < 2) {
3668 // Pass a pointer to the Arguments object as the first argument.
3669 // Return result in single register (rax).
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003670 __ lea(rcx, StackSpaceOperand(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003671 __ LoadAddress(rdx, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003672 } else {
3673 ASSERT_EQ(2, result_size_);
3674 // Pass a pointer to the result location as the first argument.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003675 __ lea(rcx, StackSpaceOperand(2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003676 // Pass a pointer to the Arguments object as the second argument.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003677 __ lea(rdx, StackSpaceOperand(0));
Steve Block44f0eee2011-05-26 01:26:41 +01003678 __ LoadAddress(r8, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003679 }
3680
3681#else // _WIN64
3682 // GCC passes arguments in rdi, rsi, rdx, rcx, r8, r9.
3683 __ movq(rdi, r14); // argc.
Steve Block44f0eee2011-05-26 01:26:41 +01003684 __ movq(rsi, r15); // argv.
3685 __ movq(rdx, ExternalReference::isolate_address());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003686#endif
3687 __ call(rbx);
3688 // Result is in rax - do not destroy this register!
3689
3690 if (always_allocate_scope) {
Steve Block44f0eee2011-05-26 01:26:41 +01003691 Operand scope_depth_operand = masm->ExternalOperand(scope_depth);
3692 __ decl(scope_depth_operand);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003693 }
3694
3695 // Check for failure result.
3696 Label failure_returned;
3697 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
3698#ifdef _WIN64
3699 // If return value is on the stack, pop it to registers.
3700 if (result_size_ > 1) {
3701 ASSERT_EQ(2, result_size_);
3702 // Read result values stored on stack. Result is stored
3703 // above the four argument mirror slots and the two
3704 // Arguments object slots.
3705 __ movq(rax, Operand(rsp, 6 * kPointerSize));
3706 __ movq(rdx, Operand(rsp, 7 * kPointerSize));
3707 }
3708#endif
3709 __ lea(rcx, Operand(rax, 1));
3710 // Lower 2 bits of rcx are 0 iff rax has failure tag.
3711 __ testl(rcx, Immediate(kFailureTagMask));
3712 __ j(zero, &failure_returned);
3713
3714 // Exit the JavaScript to C++ exit frame.
Steve Block1e0659c2011-05-24 12:43:12 +01003715 __ LeaveExitFrame(save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003716 __ ret(0);
3717
3718 // Handling of failure.
3719 __ bind(&failure_returned);
3720
Ben Murdoch257744e2011-11-30 15:57:28 +00003721 Label retry;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003722 // If the returned exception is RETRY_AFTER_GC continue at retry label
3723 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
3724 __ testl(rax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00003725 __ j(zero, &retry, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003726
3727 // Special handling of out of memory exceptions.
3728 __ movq(kScratchRegister, Failure::OutOfMemoryException(), RelocInfo::NONE);
3729 __ cmpq(rax, kScratchRegister);
3730 __ j(equal, throw_out_of_memory_exception);
3731
3732 // Retrieve the pending exception and clear the variable.
Steve Block44f0eee2011-05-26 01:26:41 +01003733 ExternalReference pending_exception_address(
Ben Murdoch589d6972011-11-30 16:04:58 +00003734 Isolate::kPendingExceptionAddress, masm->isolate());
Steve Block44f0eee2011-05-26 01:26:41 +01003735 Operand pending_exception_operand =
3736 masm->ExternalOperand(pending_exception_address);
3737 __ movq(rax, pending_exception_operand);
3738 __ LoadRoot(rdx, Heap::kTheHoleValueRootIndex);
3739 __ movq(pending_exception_operand, rdx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003740
3741 // Special handling of termination exceptions which are uncatchable
3742 // by javascript code.
3743 __ CompareRoot(rax, Heap::kTerminationExceptionRootIndex);
3744 __ j(equal, throw_termination_exception);
3745
3746 // Handle normal exception.
3747 __ jmp(throw_normal_exception);
3748
3749 // Retry.
3750 __ bind(&retry);
3751}
3752
3753
3754void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
3755 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003756 __ ThrowUncatchable(type, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003757}
3758
3759
3760void CEntryStub::Generate(MacroAssembler* masm) {
3761 // rax: number of arguments including receiver
3762 // rbx: pointer to C function (C callee-saved)
3763 // rbp: frame pointer of calling JS frame (restored after C call)
3764 // rsp: stack pointer (restored after C call)
3765 // rsi: current context (restored)
3766
3767 // NOTE: Invocations of builtins may return failure objects
3768 // instead of a proper result. The builtin entry handles
3769 // this by performing a garbage collection and retrying the
3770 // builtin once.
3771
3772 // Enter the exit frame that transitions from JavaScript to C++.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08003773#ifdef _WIN64
3774 int arg_stack_space = (result_size_ < 2 ? 2 : 4);
3775#else
3776 int arg_stack_space = 0;
3777#endif
Steve Block1e0659c2011-05-24 12:43:12 +01003778 __ EnterExitFrame(arg_stack_space, save_doubles_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003779
3780 // rax: Holds the context at this point, but should not be used.
3781 // On entry to code generated by GenerateCore, it must hold
3782 // a failure result if the collect_garbage argument to GenerateCore
3783 // is true. This failure result can be the result of code
3784 // generated by a previous call to GenerateCore. The value
3785 // of rax is then passed to Runtime::PerformGC.
3786 // rbx: pointer to builtin function (C callee-saved).
3787 // rbp: frame pointer of exit frame (restored after C call).
3788 // rsp: stack pointer (restored after C call).
3789 // r14: number of arguments including receiver (C callee-saved).
Steve Block44f0eee2011-05-26 01:26:41 +01003790 // r15: argv pointer (C callee-saved).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003791
3792 Label throw_normal_exception;
3793 Label throw_termination_exception;
3794 Label throw_out_of_memory_exception;
3795
3796 // Call into the runtime system.
3797 GenerateCore(masm,
3798 &throw_normal_exception,
3799 &throw_termination_exception,
3800 &throw_out_of_memory_exception,
3801 false,
3802 false);
3803
3804 // Do space-specific GC and retry runtime call.
3805 GenerateCore(masm,
3806 &throw_normal_exception,
3807 &throw_termination_exception,
3808 &throw_out_of_memory_exception,
3809 true,
3810 false);
3811
3812 // Do full GC and retry runtime call one final time.
3813 Failure* failure = Failure::InternalError();
3814 __ movq(rax, failure, RelocInfo::NONE);
3815 GenerateCore(masm,
3816 &throw_normal_exception,
3817 &throw_termination_exception,
3818 &throw_out_of_memory_exception,
3819 true,
3820 true);
3821
3822 __ bind(&throw_out_of_memory_exception);
3823 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
3824
3825 __ bind(&throw_termination_exception);
3826 GenerateThrowUncatchable(masm, TERMINATION);
3827
3828 __ bind(&throw_normal_exception);
3829 GenerateThrowTOS(masm);
3830}
3831
3832
3833void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003834 Label invoke, handler_entry, exit;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003835 Label not_outermost_js, not_outermost_js_2;
Steve Block44f0eee2011-05-26 01:26:41 +01003836 { // NOLINT. Scope block confuses linter.
3837 MacroAssembler::NoRootArrayScope uninitialized_root_register(masm);
3838 // Setup frame.
3839 __ push(rbp);
3840 __ movq(rbp, rsp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003841
Steve Block44f0eee2011-05-26 01:26:41 +01003842 // Push the stack frame type marker twice.
3843 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
3844 // Scratch register is neither callee-save, nor an argument register on any
3845 // platform. It's free to use at this point.
3846 // Cannot use smi-register for loading yet.
3847 __ movq(kScratchRegister,
3848 reinterpret_cast<uint64_t>(Smi::FromInt(marker)),
3849 RelocInfo::NONE);
3850 __ push(kScratchRegister); // context slot
3851 __ push(kScratchRegister); // function slot
3852 // Save callee-saved registers (X64/Win64 calling conventions).
3853 __ push(r12);
3854 __ push(r13);
3855 __ push(r14);
3856 __ push(r15);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003857#ifdef _WIN64
Steve Block44f0eee2011-05-26 01:26:41 +01003858 __ push(rdi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
3859 __ push(rsi); // Only callee save in Win64 ABI, argument in AMD64 ABI.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003860#endif
Steve Block44f0eee2011-05-26 01:26:41 +01003861 __ push(rbx);
3862 // TODO(X64): On Win64, if we ever use XMM6-XMM15, the low low 64 bits are
3863 // callee save as well.
3864
3865 // Set up the roots and smi constant registers.
3866 // Needs to be done before any further smi loads.
3867 __ InitializeSmiConstantRegister();
3868 __ InitializeRootRegister();
3869 }
3870
3871 Isolate* isolate = masm->isolate();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003872
3873 // Save copies of the top frame descriptor on the stack.
Ben Murdoch589d6972011-11-30 16:04:58 +00003874 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01003875 {
3876 Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
3877 __ push(c_entry_fp_operand);
3878 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003879
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003880 // If this is the outermost JS call, set js_entry_sp value.
Ben Murdoch589d6972011-11-30 16:04:58 +00003881 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress, isolate);
Steve Block44f0eee2011-05-26 01:26:41 +01003882 __ Load(rax, js_entry_sp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003883 __ testq(rax, rax);
3884 __ j(not_zero, &not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01003885 __ Push(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003886 __ movq(rax, rbp);
Steve Block44f0eee2011-05-26 01:26:41 +01003887 __ Store(js_entry_sp, rax);
Steve Block053d10c2011-06-13 19:13:29 +01003888 Label cont;
3889 __ jmp(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003890 __ bind(&not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01003891 __ Push(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME));
3892 __ bind(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003893
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003894 // Jump to a faked try block that does the invoke, with a faked catch
3895 // block that sets the pending exception.
3896 __ jmp(&invoke);
3897 __ bind(&handler_entry);
3898 handler_offset_ = handler_entry.pos();
3899 // Caught exception: Store result (exception) in the pending exception
3900 // field in the JSEnv and return a failure sentinel.
Ben Murdoch589d6972011-11-30 16:04:58 +00003901 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01003902 isolate);
3903 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003904 __ movq(rax, Failure::Exception(), RelocInfo::NONE);
3905 __ jmp(&exit);
3906
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003907 // Invoke: Link this frame into the handler chain. There's only one
3908 // handler block in this code object, so its index is 0.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003909 __ bind(&invoke);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003910 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003911
3912 // Clear any pending exceptions.
Steve Block44f0eee2011-05-26 01:26:41 +01003913 __ LoadRoot(rax, Heap::kTheHoleValueRootIndex);
3914 __ Store(pending_exception, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003915
3916 // Fake a receiver (NULL).
3917 __ push(Immediate(0)); // receiver
3918
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003919 // Invoke the function by calling through JS entry trampoline builtin and
3920 // pop the faked function when we return. We load the address from an
3921 // external reference instead of inlining the call target address directly
3922 // in the code, because the builtin stubs may not have been generated yet
3923 // at the time this code is generated.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003924 if (is_construct) {
Steve Block44f0eee2011-05-26 01:26:41 +01003925 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
3926 isolate);
3927 __ Load(rax, construct_entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003928 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01003929 ExternalReference entry(Builtins::kJSEntryTrampoline, isolate);
3930 __ Load(rax, entry);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003931 }
3932 __ lea(kScratchRegister, FieldOperand(rax, Code::kHeaderSize));
3933 __ call(kScratchRegister);
3934
3935 // Unlink this frame from the handler chain.
Steve Block053d10c2011-06-13 19:13:29 +01003936 __ PopTryHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003937
Steve Block053d10c2011-06-13 19:13:29 +01003938 __ bind(&exit);
Steve Block053d10c2011-06-13 19:13:29 +01003939 // Check if the current stack frame is marked as the outermost JS frame.
3940 __ pop(rbx);
3941 __ Cmp(rbx, Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003942 __ j(not_equal, &not_outermost_js_2);
Steve Block053d10c2011-06-13 19:13:29 +01003943 __ movq(kScratchRegister, js_entry_sp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003944 __ movq(Operand(kScratchRegister, 0), Immediate(0));
3945 __ bind(&not_outermost_js_2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003946
3947 // Restore the top frame descriptor from the stack.
Steve Block053d10c2011-06-13 19:13:29 +01003948 { Operand c_entry_fp_operand = masm->ExternalOperand(c_entry_fp);
Steve Block44f0eee2011-05-26 01:26:41 +01003949 __ pop(c_entry_fp_operand);
3950 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003951
3952 // Restore callee-saved registers (X64 conventions).
3953 __ pop(rbx);
3954#ifdef _WIN64
3955 // Callee save on in Win64 ABI, arguments/volatile in AMD64 ABI.
3956 __ pop(rsi);
3957 __ pop(rdi);
3958#endif
3959 __ pop(r15);
3960 __ pop(r14);
3961 __ pop(r13);
3962 __ pop(r12);
3963 __ addq(rsp, Immediate(2 * kPointerSize)); // remove markers
3964
3965 // Restore frame pointer and return.
3966 __ pop(rbp);
3967 __ ret(0);
3968}
3969
3970
3971void InstanceofStub::Generate(MacroAssembler* masm) {
3972 // Implements "value instanceof function" operator.
Steve Block44f0eee2011-05-26 01:26:41 +01003973 // Expected input state with no inline cache:
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003974 // rsp[0] : return address
3975 // rsp[1] : function pointer
3976 // rsp[2] : value
Steve Block44f0eee2011-05-26 01:26:41 +01003977 // Expected input state with an inline one-element cache:
3978 // rsp[0] : return address
3979 // rsp[1] : offset from return address to location of inline cache
3980 // rsp[2] : function pointer
3981 // rsp[3] : value
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003982 // Returns a bitwise zero to indicate that the value
3983 // is and instance of the function and anything else to
3984 // indicate that the value is not an instance.
3985
Ben Murdoch8b112d22011-06-08 16:22:53 +01003986 static const int kOffsetToMapCheckValue = 2;
3987 static const int kOffsetToResultValue = 18;
Steve Block44f0eee2011-05-26 01:26:41 +01003988 // The last 4 bytes of the instruction sequence
Ben Murdoch8b112d22011-06-08 16:22:53 +01003989 // movq(rdi, FieldOperand(rax, HeapObject::kMapOffset))
Steve Block44f0eee2011-05-26 01:26:41 +01003990 // Move(kScratchRegister, FACTORY->the_hole_value())
3991 // in front of the hole value address.
3992 static const unsigned int kWordBeforeMapCheckValue = 0xBA49FF78;
3993 // The last 4 bytes of the instruction sequence
3994 // __ j(not_equal, &cache_miss);
3995 // __ LoadRoot(ToRegister(instr->result()), Heap::kTheHoleValueRootIndex);
3996 // before the offset of the hole value in the root array.
3997 static const unsigned int kWordBeforeResultValue = 0x458B4909;
3998 // Only the inline check flag is supported on X64.
3999 ASSERT(flags_ == kNoFlags || HasCallSiteInlineCheck());
4000 int extra_stack_space = HasCallSiteInlineCheck() ? kPointerSize : 0;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004001
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004002 // Get the object - go slow case if it's a smi.
4003 Label slow;
Steve Block44f0eee2011-05-26 01:26:41 +01004004
4005 __ movq(rax, Operand(rsp, 2 * kPointerSize + extra_stack_space));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004006 __ JumpIfSmi(rax, &slow);
4007
4008 // Check that the left hand is a JS object. Leave its map in rax.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004009 __ CmpObjectType(rax, FIRST_SPEC_OBJECT_TYPE, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004010 __ j(below, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004011 __ CmpInstanceType(rax, LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004012 __ j(above, &slow);
4013
4014 // Get the prototype of the function.
Steve Block44f0eee2011-05-26 01:26:41 +01004015 __ movq(rdx, Operand(rsp, 1 * kPointerSize + extra_stack_space));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004016 // rdx is function, rax is map.
4017
Steve Block44f0eee2011-05-26 01:26:41 +01004018 // If there is a call site cache don't look in the global cache, but do the
4019 // real lookup and update the call site cache.
4020 if (!HasCallSiteInlineCheck()) {
4021 // Look up the function and the map in the instanceof cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00004022 Label miss;
Steve Block44f0eee2011-05-26 01:26:41 +01004023 __ CompareRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00004024 __ j(not_equal, &miss, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01004025 __ CompareRoot(rax, Heap::kInstanceofCacheMapRootIndex);
Ben Murdoch257744e2011-11-30 15:57:28 +00004026 __ j(not_equal, &miss, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01004027 __ LoadRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
4028 __ ret(2 * kPointerSize);
4029 __ bind(&miss);
4030 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004031
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004032 __ TryGetFunctionPrototype(rdx, rbx, &slow, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004033
4034 // Check that the function prototype is a JS object.
4035 __ JumpIfSmi(rbx, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004036 __ CmpObjectType(rbx, FIRST_SPEC_OBJECT_TYPE, kScratchRegister);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004037 __ j(below, &slow);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004038 __ CmpInstanceType(kScratchRegister, LAST_SPEC_OBJECT_TYPE);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004039 __ j(above, &slow);
4040
4041 // Register mapping:
4042 // rax is object map.
4043 // rdx is function.
4044 // rbx is function prototype.
Steve Block44f0eee2011-05-26 01:26:41 +01004045 if (!HasCallSiteInlineCheck()) {
4046 __ StoreRoot(rdx, Heap::kInstanceofCacheFunctionRootIndex);
4047 __ StoreRoot(rax, Heap::kInstanceofCacheMapRootIndex);
4048 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004049 // Get return address and delta to inlined map check.
Steve Block44f0eee2011-05-26 01:26:41 +01004050 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
4051 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01004052 if (FLAG_debug_code) {
4053 __ movl(rdi, Immediate(kWordBeforeMapCheckValue));
4054 __ cmpl(Operand(kScratchRegister, kOffsetToMapCheckValue - 4), rdi);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004055 __ Assert(equal, "InstanceofStub unexpected call site cache (check).");
Steve Block44f0eee2011-05-26 01:26:41 +01004056 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004057 __ movq(kScratchRegister,
4058 Operand(kScratchRegister, kOffsetToMapCheckValue));
4059 __ movq(Operand(kScratchRegister, 0), rax);
Steve Block44f0eee2011-05-26 01:26:41 +01004060 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004061
4062 __ movq(rcx, FieldOperand(rax, Map::kPrototypeOffset));
4063
4064 // Loop through the prototype chain looking for the function prototype.
Ben Murdoch257744e2011-11-30 15:57:28 +00004065 Label loop, is_instance, is_not_instance;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004066 __ LoadRoot(kScratchRegister, Heap::kNullValueRootIndex);
4067 __ bind(&loop);
4068 __ cmpq(rcx, rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004069 __ j(equal, &is_instance, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004070 __ cmpq(rcx, kScratchRegister);
4071 // The code at is_not_instance assumes that kScratchRegister contains a
4072 // non-zero GCable value (the null object in this case).
Ben Murdoch257744e2011-11-30 15:57:28 +00004073 __ j(equal, &is_not_instance, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004074 __ movq(rcx, FieldOperand(rcx, HeapObject::kMapOffset));
4075 __ movq(rcx, FieldOperand(rcx, Map::kPrototypeOffset));
4076 __ jmp(&loop);
4077
4078 __ bind(&is_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01004079 if (!HasCallSiteInlineCheck()) {
4080 __ xorl(rax, rax);
4081 // Store bitwise zero in the cache. This is a Smi in GC terms.
4082 STATIC_ASSERT(kSmiTag == 0);
4083 __ StoreRoot(rax, Heap::kInstanceofCacheAnswerRootIndex);
4084 } else {
4085 // Store offset of true in the root array at the inline check site.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004086 int true_offset = 0x100 +
4087 (Heap::kTrueValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
4088 // Assert it is a 1-byte signed value.
4089 ASSERT(true_offset >= 0 && true_offset < 0x100);
4090 __ movl(rax, Immediate(true_offset));
Steve Block44f0eee2011-05-26 01:26:41 +01004091 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
4092 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
4093 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
4094 if (FLAG_debug_code) {
4095 __ movl(rax, Immediate(kWordBeforeResultValue));
4096 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004097 __ Assert(equal, "InstanceofStub unexpected call site cache (mov).");
Steve Block44f0eee2011-05-26 01:26:41 +01004098 }
Ben Murdoch8b112d22011-06-08 16:22:53 +01004099 __ Set(rax, 0);
Steve Block44f0eee2011-05-26 01:26:41 +01004100 }
4101 __ ret(2 * kPointerSize + extra_stack_space);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004102
4103 __ bind(&is_not_instance);
Steve Block44f0eee2011-05-26 01:26:41 +01004104 if (!HasCallSiteInlineCheck()) {
4105 // We have to store a non-zero value in the cache.
4106 __ StoreRoot(kScratchRegister, Heap::kInstanceofCacheAnswerRootIndex);
4107 } else {
4108 // Store offset of false in the root array at the inline check site.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004109 int false_offset = 0x100 +
4110 (Heap::kFalseValueRootIndex << kPointerSizeLog2) - kRootRegisterBias;
4111 // Assert it is a 1-byte signed value.
4112 ASSERT(false_offset >= 0 && false_offset < 0x100);
4113 __ movl(rax, Immediate(false_offset));
Steve Block44f0eee2011-05-26 01:26:41 +01004114 __ movq(kScratchRegister, Operand(rsp, 0 * kPointerSize));
4115 __ subq(kScratchRegister, Operand(rsp, 1 * kPointerSize));
4116 __ movb(Operand(kScratchRegister, kOffsetToResultValue), rax);
4117 if (FLAG_debug_code) {
4118 __ movl(rax, Immediate(kWordBeforeResultValue));
4119 __ cmpl(Operand(kScratchRegister, kOffsetToResultValue - 4), rax);
4120 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
4121 }
4122 }
4123 __ ret(2 * kPointerSize + extra_stack_space);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004124
4125 // Slow-case: Go through the JavaScript implementation.
4126 __ bind(&slow);
Steve Block44f0eee2011-05-26 01:26:41 +01004127 if (HasCallSiteInlineCheck()) {
4128 // Remove extra value from the stack.
4129 __ pop(rcx);
4130 __ pop(rax);
4131 __ push(rcx);
4132 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004133 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
4134}
4135
4136
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004137// Passing arguments in registers is not supported.
4138Register InstanceofStub::left() { return no_reg; }
Steve Block1e0659c2011-05-24 12:43:12 +01004139
4140
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004141Register InstanceofStub::right() { return no_reg; }
Steve Block1e0659c2011-05-24 12:43:12 +01004142
4143
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004144int CompareStub::MinorKey() {
4145 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
4146 // stubs the never NaN NaN condition is only taken into account if the
4147 // condition is equals.
4148 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
4149 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4150 return ConditionField::encode(static_cast<unsigned>(cc_))
4151 | RegisterField::encode(false) // lhs_ and rhs_ are not used
4152 | StrictField::encode(strict_)
4153 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004154 | IncludeNumberCompareField::encode(include_number_compare_)
4155 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004156}
4157
4158
4159// Unfortunately you have to run without snapshots to see most of these
4160// names in the profile since most compare stubs end up in the snapshot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004161void CompareStub::PrintName(StringStream* stream) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004162 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004163 const char* cc_name;
4164 switch (cc_) {
4165 case less: cc_name = "LT"; break;
4166 case greater: cc_name = "GT"; break;
4167 case less_equal: cc_name = "LE"; break;
4168 case greater_equal: cc_name = "GE"; break;
4169 case equal: cc_name = "EQ"; break;
4170 case not_equal: cc_name = "NE"; break;
4171 default: cc_name = "UnknownCondition"; break;
4172 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004173 bool is_equality = cc_ == equal || cc_ == not_equal;
4174 stream->Add("CompareStub_%s", cc_name);
4175 if (strict_ && is_equality) stream->Add("_STRICT");
4176 if (never_nan_nan_ && is_equality) stream->Add("_NO_NAN");
4177 if (!include_number_compare_) stream->Add("_NO_NUMBER");
4178 if (!include_smi_compare_) stream->Add("_NO_SMI");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004179}
4180
4181
4182// -------------------------------------------------------------------------
4183// StringCharCodeAtGenerator
4184
4185void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
4186 Label flat_string;
4187 Label ascii_string;
4188 Label got_char_code;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004189 Label sliced_string;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004190
4191 // If the receiver is a smi trigger the non-string case.
4192 __ JumpIfSmi(object_, receiver_not_string_);
4193
4194 // Fetch the instance type of the receiver into result register.
4195 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
4196 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4197 // If the receiver is not a string trigger the non-string case.
4198 __ testb(result_, Immediate(kIsNotStringMask));
4199 __ j(not_zero, receiver_not_string_);
4200
4201 // If the index is non-smi trigger the non-smi case.
4202 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004203 __ bind(&got_smi_index_);
4204
4205 // Check for index out of range.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004206 __ SmiCompare(index_, FieldOperand(object_, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004207 __ j(above_equal, index_out_of_range_);
4208
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004209 __ SmiToInteger32(index_, index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004210
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004211 StringCharLoadGenerator::Generate(
4212 masm, object_, index_, result_, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004213
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004214 __ Integer32ToSmi(result_, result_);
4215 __ bind(&exit_);
4216}
4217
4218
4219void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004220 MacroAssembler* masm,
4221 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004222 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
4223
Ben Murdoch257744e2011-11-30 15:57:28 +00004224 Factory* factory = masm->isolate()->factory();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004225 // Index is not a smi.
4226 __ bind(&index_not_smi_);
4227 // If index is a heap number, try converting it to an integer.
Ben Murdoch257744e2011-11-30 15:57:28 +00004228 __ CheckMap(index_,
4229 factory->heap_number_map(),
4230 index_not_number_,
4231 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004232 call_helper.BeforeCall(masm);
4233 __ push(object_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004234 __ push(index_); // Consumed by runtime conversion function.
4235 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
4236 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
4237 } else {
4238 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
4239 // NumberToSmi discards numbers that are not exact integers.
4240 __ CallRuntime(Runtime::kNumberToSmi, 1);
4241 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004242 if (!index_.is(rax)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004243 // Save the conversion result before the pop instructions below
4244 // have a chance to overwrite it.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004245 __ movq(index_, rax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004246 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004247 __ pop(object_);
4248 // Reload the instance type.
4249 __ movq(result_, FieldOperand(object_, HeapObject::kMapOffset));
4250 __ movzxbl(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
4251 call_helper.AfterCall(masm);
4252 // If index is still not a smi, it must be out of range.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004253 __ JumpIfNotSmi(index_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004254 // Otherwise, return to the fast path.
4255 __ jmp(&got_smi_index_);
4256
4257 // Call runtime. We get here when the receiver is a string and the
4258 // index is a number, but the code of getting the actual character
4259 // is too complex (e.g., when the string needs to be flattened).
4260 __ bind(&call_runtime_);
4261 call_helper.BeforeCall(masm);
4262 __ push(object_);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004263 __ Integer32ToSmi(index_, index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004264 __ push(index_);
4265 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
4266 if (!result_.is(rax)) {
4267 __ movq(result_, rax);
4268 }
4269 call_helper.AfterCall(masm);
4270 __ jmp(&exit_);
4271
4272 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
4273}
4274
4275
4276// -------------------------------------------------------------------------
4277// StringCharFromCodeGenerator
4278
4279void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
4280 // Fast case of Heap::LookupSingleCharacterStringFromCode.
4281 __ JumpIfNotSmi(code_, &slow_case_);
4282 __ SmiCompare(code_, Smi::FromInt(String::kMaxAsciiCharCode));
4283 __ j(above, &slow_case_);
4284
4285 __ LoadRoot(result_, Heap::kSingleCharacterStringCacheRootIndex);
4286 SmiIndex index = masm->SmiToIndex(kScratchRegister, code_, kPointerSizeLog2);
4287 __ movq(result_, FieldOperand(result_, index.reg, index.scale,
4288 FixedArray::kHeaderSize));
4289 __ CompareRoot(result_, Heap::kUndefinedValueRootIndex);
4290 __ j(equal, &slow_case_);
4291 __ bind(&exit_);
4292}
4293
4294
4295void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004296 MacroAssembler* masm,
4297 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004298 __ Abort("Unexpected fallthrough to CharFromCode slow case");
4299
4300 __ bind(&slow_case_);
4301 call_helper.BeforeCall(masm);
4302 __ push(code_);
4303 __ CallRuntime(Runtime::kCharFromCode, 1);
4304 if (!result_.is(rax)) {
4305 __ movq(result_, rax);
4306 }
4307 call_helper.AfterCall(masm);
4308 __ jmp(&exit_);
4309
4310 __ Abort("Unexpected fallthrough from CharFromCode slow case");
4311}
4312
4313
4314// -------------------------------------------------------------------------
4315// StringCharAtGenerator
4316
4317void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
4318 char_code_at_generator_.GenerateFast(masm);
4319 char_from_code_generator_.GenerateFast(masm);
4320}
4321
4322
4323void StringCharAtGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004324 MacroAssembler* masm,
4325 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004326 char_code_at_generator_.GenerateSlow(masm, call_helper);
4327 char_from_code_generator_.GenerateSlow(masm, call_helper);
4328}
4329
4330
4331void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004332 Label string_add_runtime, call_builtin;
4333 Builtins::JavaScript builtin_id = Builtins::ADD;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004334
4335 // Load the two arguments.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004336 __ movq(rax, Operand(rsp, 2 * kPointerSize)); // First argument (left).
4337 __ movq(rdx, Operand(rsp, 1 * kPointerSize)); // Second argument (right).
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004338
4339 // Make sure that both arguments are strings if not known in advance.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004340 if (flags_ == NO_STRING_ADD_FLAGS) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004341 __ JumpIfSmi(rax, &string_add_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004342 __ CmpObjectType(rax, FIRST_NONSTRING_TYPE, r8);
4343 __ j(above_equal, &string_add_runtime);
4344
4345 // First argument is a a string, test second.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004346 __ JumpIfSmi(rdx, &string_add_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004347 __ CmpObjectType(rdx, FIRST_NONSTRING_TYPE, r9);
4348 __ j(above_equal, &string_add_runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004349 } else {
4350 // Here at least one of the arguments is definitely a string.
4351 // We convert the one that is not known to be a string.
4352 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
4353 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
4354 GenerateConvertArgument(masm, 2 * kPointerSize, rax, rbx, rcx, rdi,
4355 &call_builtin);
4356 builtin_id = Builtins::STRING_ADD_RIGHT;
4357 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
4358 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
4359 GenerateConvertArgument(masm, 1 * kPointerSize, rdx, rbx, rcx, rdi,
4360 &call_builtin);
4361 builtin_id = Builtins::STRING_ADD_LEFT;
4362 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004363 }
4364
4365 // Both arguments are strings.
4366 // rax: first string
4367 // rdx: second string
4368 // Check if either of the strings are empty. In that case return the other.
Ben Murdoch257744e2011-11-30 15:57:28 +00004369 Label second_not_zero_length, both_not_zero_length;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004370 __ movq(rcx, FieldOperand(rdx, String::kLengthOffset));
4371 __ SmiTest(rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004372 __ j(not_zero, &second_not_zero_length, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004373 // Second string is empty, result is first string which is already in rax.
Steve Block44f0eee2011-05-26 01:26:41 +01004374 Counters* counters = masm->isolate()->counters();
4375 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004376 __ ret(2 * kPointerSize);
4377 __ bind(&second_not_zero_length);
4378 __ movq(rbx, FieldOperand(rax, String::kLengthOffset));
4379 __ SmiTest(rbx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004380 __ j(not_zero, &both_not_zero_length, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004381 // First string is empty, result is second string which is in rdx.
4382 __ movq(rax, rdx);
Steve Block44f0eee2011-05-26 01:26:41 +01004383 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004384 __ ret(2 * kPointerSize);
4385
4386 // Both strings are non-empty.
4387 // rax: first string
4388 // rbx: length of first string
4389 // rcx: length of second string
4390 // rdx: second string
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004391 // r8: map of first string (if flags_ == NO_STRING_ADD_FLAGS)
4392 // r9: map of second string (if flags_ == NO_STRING_ADD_FLAGS)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004393 Label string_add_flat_result, longer_than_two;
4394 __ bind(&both_not_zero_length);
4395
4396 // If arguments where known to be strings, maps are not loaded to r8 and r9
4397 // by the code above.
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004398 if (flags_ != NO_STRING_ADD_FLAGS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004399 __ movq(r8, FieldOperand(rax, HeapObject::kMapOffset));
4400 __ movq(r9, FieldOperand(rdx, HeapObject::kMapOffset));
4401 }
4402 // Get the instance types of the two strings as they will be needed soon.
4403 __ movzxbl(r8, FieldOperand(r8, Map::kInstanceTypeOffset));
4404 __ movzxbl(r9, FieldOperand(r9, Map::kInstanceTypeOffset));
4405
4406 // Look at the length of the result of adding the two strings.
4407 STATIC_ASSERT(String::kMaxLength <= Smi::kMaxValue / 2);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004408 __ SmiAdd(rbx, rbx, rcx);
Steve Block44f0eee2011-05-26 01:26:41 +01004409 // Use the symbol table when adding two one character strings, as it
4410 // helps later optimizations to return a symbol here.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004411 __ SmiCompare(rbx, Smi::FromInt(2));
4412 __ j(not_equal, &longer_than_two);
4413
4414 // Check that both strings are non-external ascii strings.
4415 __ JumpIfBothInstanceTypesAreNotSequentialAscii(r8, r9, rbx, rcx,
4416 &string_add_runtime);
4417
4418 // Get the two characters forming the sub string.
4419 __ movzxbq(rbx, FieldOperand(rax, SeqAsciiString::kHeaderSize));
4420 __ movzxbq(rcx, FieldOperand(rdx, SeqAsciiString::kHeaderSize));
4421
4422 // Try to lookup two character string in symbol table. If it is not found
4423 // just allocate a new one.
4424 Label make_two_character_string, make_flat_ascii_string;
4425 StringHelper::GenerateTwoCharacterSymbolTableProbe(
Steve Block44f0eee2011-05-26 01:26:41 +01004426 masm, rbx, rcx, r14, r11, rdi, r15, &make_two_character_string);
4427 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004428 __ ret(2 * kPointerSize);
4429
4430 __ bind(&make_two_character_string);
4431 __ Set(rbx, 2);
4432 __ jmp(&make_flat_ascii_string);
4433
4434 __ bind(&longer_than_two);
4435 // Check if resulting string will be flat.
4436 __ SmiCompare(rbx, Smi::FromInt(String::kMinNonFlatLength));
4437 __ j(below, &string_add_flat_result);
4438 // Handle exceptionally long strings in the runtime system.
4439 STATIC_ASSERT((String::kMaxLength & 0x80000000) == 0);
4440 __ SmiCompare(rbx, Smi::FromInt(String::kMaxLength));
4441 __ j(above, &string_add_runtime);
4442
4443 // If result is not supposed to be flat, allocate a cons string object. If
4444 // both strings are ascii the result is an ascii cons string.
4445 // rax: first string
4446 // rbx: length of resulting flat string
4447 // rdx: second string
4448 // r8: instance type of first string
4449 // r9: instance type of second string
4450 Label non_ascii, allocated, ascii_data;
4451 __ movl(rcx, r8);
4452 __ and_(rcx, r9);
Ben Murdoch589d6972011-11-30 16:04:58 +00004453 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
4454 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
4455 __ testl(rcx, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004456 __ j(zero, &non_ascii);
4457 __ bind(&ascii_data);
4458 // Allocate an acsii cons string.
4459 __ AllocateAsciiConsString(rcx, rdi, no_reg, &string_add_runtime);
4460 __ bind(&allocated);
4461 // Fill the fields of the cons string.
4462 __ movq(FieldOperand(rcx, ConsString::kLengthOffset), rbx);
4463 __ movq(FieldOperand(rcx, ConsString::kHashFieldOffset),
4464 Immediate(String::kEmptyHashField));
4465 __ movq(FieldOperand(rcx, ConsString::kFirstOffset), rax);
4466 __ movq(FieldOperand(rcx, ConsString::kSecondOffset), rdx);
4467 __ movq(rax, rcx);
Steve Block44f0eee2011-05-26 01:26:41 +01004468 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004469 __ ret(2 * kPointerSize);
4470 __ bind(&non_ascii);
4471 // At least one of the strings is two-byte. Check whether it happens
4472 // to contain only ascii characters.
4473 // rcx: first instance type AND second instance type.
4474 // r8: first instance type.
4475 // r9: second instance type.
4476 __ testb(rcx, Immediate(kAsciiDataHintMask));
4477 __ j(not_zero, &ascii_data);
4478 __ xor_(r8, r9);
4479 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
4480 __ andb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
4481 __ cmpb(r8, Immediate(kAsciiStringTag | kAsciiDataHintTag));
4482 __ j(equal, &ascii_data);
4483 // Allocate a two byte cons string.
Ben Murdoch589d6972011-11-30 16:04:58 +00004484 __ AllocateTwoByteConsString(rcx, rdi, no_reg, &string_add_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004485 __ jmp(&allocated);
4486
4487 // Handle creating a flat result. First check that both strings are not
4488 // external strings.
4489 // rax: first string
4490 // rbx: length of resulting flat string as smi
4491 // rdx: second string
4492 // r8: instance type of first string
4493 // r9: instance type of first string
4494 __ bind(&string_add_flat_result);
4495 __ SmiToInteger32(rbx, rbx);
4496 __ movl(rcx, r8);
4497 __ and_(rcx, Immediate(kStringRepresentationMask));
4498 __ cmpl(rcx, Immediate(kExternalStringTag));
4499 __ j(equal, &string_add_runtime);
4500 __ movl(rcx, r9);
4501 __ and_(rcx, Immediate(kStringRepresentationMask));
4502 __ cmpl(rcx, Immediate(kExternalStringTag));
4503 __ j(equal, &string_add_runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004504 // We cannot encounter sliced strings here since:
4505 STATIC_ASSERT(SlicedString::kMinLength >= String::kMinNonFlatLength);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004506 // Now check if both strings are ascii strings.
4507 // rax: first string
4508 // rbx: length of resulting flat string
4509 // rdx: second string
4510 // r8: instance type of first string
4511 // r9: instance type of second string
4512 Label non_ascii_string_add_flat_result;
Ben Murdoch589d6972011-11-30 16:04:58 +00004513 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
4514 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
4515 __ testl(r8, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004516 __ j(zero, &non_ascii_string_add_flat_result);
Ben Murdoch589d6972011-11-30 16:04:58 +00004517 __ testl(r9, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004518 __ j(zero, &string_add_runtime);
4519
4520 __ bind(&make_flat_ascii_string);
4521 // Both strings are ascii strings. As they are short they are both flat.
4522 __ AllocateAsciiString(rcx, rbx, rdi, r14, r11, &string_add_runtime);
4523 // rcx: result string
4524 __ movq(rbx, rcx);
4525 // Locate first character of result.
4526 __ addq(rcx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4527 // Locate first character of first argument
4528 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
4529 __ addq(rax, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4530 // rax: first char of first argument
4531 // rbx: result string
4532 // rcx: first character of result
4533 // rdx: second string
4534 // rdi: length of first argument
4535 StringHelper::GenerateCopyCharacters(masm, rcx, rax, rdi, true);
4536 // Locate first character of second argument.
4537 __ SmiToInteger32(rdi, FieldOperand(rdx, String::kLengthOffset));
4538 __ addq(rdx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
4539 // rbx: result string
4540 // rcx: next character of result
4541 // rdx: first char of second argument
4542 // rdi: length of second argument
4543 StringHelper::GenerateCopyCharacters(masm, rcx, rdx, rdi, true);
4544 __ movq(rax, rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01004545 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004546 __ ret(2 * kPointerSize);
4547
4548 // Handle creating a flat two byte result.
4549 // rax: first string - known to be two byte
4550 // rbx: length of resulting flat string
4551 // rdx: second string
4552 // r8: instance type of first string
4553 // r9: instance type of first string
4554 __ bind(&non_ascii_string_add_flat_result);
Ben Murdoch589d6972011-11-30 16:04:58 +00004555 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
4556 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
4557 __ and_(r9, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004558 __ j(not_zero, &string_add_runtime);
4559 // Both strings are two byte strings. As they are short they are both
4560 // flat.
4561 __ AllocateTwoByteString(rcx, rbx, rdi, r14, r11, &string_add_runtime);
4562 // rcx: result string
4563 __ movq(rbx, rcx);
4564 // Locate first character of result.
4565 __ addq(rcx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4566 // Locate first character of first argument.
4567 __ SmiToInteger32(rdi, FieldOperand(rax, String::kLengthOffset));
4568 __ addq(rax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4569 // rax: first char of first argument
4570 // rbx: result string
4571 // rcx: first character of result
4572 // rdx: second argument
4573 // rdi: length of first argument
4574 StringHelper::GenerateCopyCharacters(masm, rcx, rax, rdi, false);
4575 // Locate first character of second argument.
4576 __ SmiToInteger32(rdi, FieldOperand(rdx, String::kLengthOffset));
4577 __ addq(rdx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4578 // rbx: result string
4579 // rcx: next character of result
4580 // rdx: first char of second argument
4581 // rdi: length of second argument
4582 StringHelper::GenerateCopyCharacters(masm, rcx, rdx, rdi, false);
4583 __ movq(rax, rbx);
Steve Block44f0eee2011-05-26 01:26:41 +01004584 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004585 __ ret(2 * kPointerSize);
4586
4587 // Just jump to runtime to add the two strings.
4588 __ bind(&string_add_runtime);
4589 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004590
4591 if (call_builtin.is_linked()) {
4592 __ bind(&call_builtin);
4593 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
4594 }
4595}
4596
4597
4598void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
4599 int stack_offset,
4600 Register arg,
4601 Register scratch1,
4602 Register scratch2,
4603 Register scratch3,
4604 Label* slow) {
4605 // First check if the argument is already a string.
4606 Label not_string, done;
4607 __ JumpIfSmi(arg, &not_string);
4608 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
4609 __ j(below, &done);
4610
4611 // Check the number to string cache.
4612 Label not_cached;
4613 __ bind(&not_string);
4614 // Puts the cached result into scratch1.
4615 NumberToStringStub::GenerateLookupNumberStringCache(masm,
4616 arg,
4617 scratch1,
4618 scratch2,
4619 scratch3,
4620 false,
4621 &not_cached);
4622 __ movq(arg, scratch1);
4623 __ movq(Operand(rsp, stack_offset), arg);
4624 __ jmp(&done);
4625
4626 // Check if the argument is a safe string wrapper.
4627 __ bind(&not_cached);
4628 __ JumpIfSmi(arg, slow);
4629 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
4630 __ j(not_equal, slow);
4631 __ testb(FieldOperand(scratch1, Map::kBitField2Offset),
4632 Immediate(1 << Map::kStringWrapperSafeForDefaultValueOf));
4633 __ j(zero, slow);
4634 __ movq(arg, FieldOperand(arg, JSValue::kValueOffset));
4635 __ movq(Operand(rsp, stack_offset), arg);
4636
4637 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004638}
4639
4640
4641void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
4642 Register dest,
4643 Register src,
4644 Register count,
4645 bool ascii) {
4646 Label loop;
4647 __ bind(&loop);
4648 // This loop just copies one character at a time, as it is only used for very
4649 // short strings.
4650 if (ascii) {
4651 __ movb(kScratchRegister, Operand(src, 0));
4652 __ movb(Operand(dest, 0), kScratchRegister);
4653 __ incq(src);
4654 __ incq(dest);
4655 } else {
4656 __ movzxwl(kScratchRegister, Operand(src, 0));
4657 __ movw(Operand(dest, 0), kScratchRegister);
4658 __ addq(src, Immediate(2));
4659 __ addq(dest, Immediate(2));
4660 }
4661 __ decl(count);
4662 __ j(not_zero, &loop);
4663}
4664
4665
4666void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
4667 Register dest,
4668 Register src,
4669 Register count,
4670 bool ascii) {
4671 // Copy characters using rep movs of doublewords. Align destination on 4 byte
4672 // boundary before starting rep movs. Copy remaining characters after running
4673 // rep movs.
4674 // Count is positive int32, dest and src are character pointers.
4675 ASSERT(dest.is(rdi)); // rep movs destination
4676 ASSERT(src.is(rsi)); // rep movs source
4677 ASSERT(count.is(rcx)); // rep movs count
4678
4679 // Nothing to do for zero characters.
Ben Murdoch257744e2011-11-30 15:57:28 +00004680 Label done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004681 __ testl(count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00004682 __ j(zero, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004683
4684 // Make count the number of bytes to copy.
4685 if (!ascii) {
4686 STATIC_ASSERT(2 == sizeof(uc16));
4687 __ addl(count, count);
4688 }
4689
4690 // Don't enter the rep movs if there are less than 4 bytes to copy.
Ben Murdoch257744e2011-11-30 15:57:28 +00004691 Label last_bytes;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004692 __ testl(count, Immediate(~7));
Ben Murdoch257744e2011-11-30 15:57:28 +00004693 __ j(zero, &last_bytes, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004694
4695 // Copy from edi to esi using rep movs instruction.
4696 __ movl(kScratchRegister, count);
4697 __ shr(count, Immediate(3)); // Number of doublewords to copy.
4698 __ repmovsq();
4699
4700 // Find number of bytes left.
4701 __ movl(count, kScratchRegister);
4702 __ and_(count, Immediate(7));
4703
4704 // Check if there are more bytes to copy.
4705 __ bind(&last_bytes);
4706 __ testl(count, count);
Ben Murdoch257744e2011-11-30 15:57:28 +00004707 __ j(zero, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004708
4709 // Copy remaining characters.
4710 Label loop;
4711 __ bind(&loop);
4712 __ movb(kScratchRegister, Operand(src, 0));
4713 __ movb(Operand(dest, 0), kScratchRegister);
4714 __ incq(src);
4715 __ incq(dest);
4716 __ decl(count);
4717 __ j(not_zero, &loop);
4718
4719 __ bind(&done);
4720}
4721
4722void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
4723 Register c1,
4724 Register c2,
4725 Register scratch1,
4726 Register scratch2,
4727 Register scratch3,
4728 Register scratch4,
4729 Label* not_found) {
4730 // Register scratch3 is the general scratch register in this function.
4731 Register scratch = scratch3;
4732
4733 // Make sure that both characters are not digits as such strings has a
4734 // different hash algorithm. Don't try to look for these in the symbol table.
Ben Murdoch257744e2011-11-30 15:57:28 +00004735 Label not_array_index;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004736 __ leal(scratch, Operand(c1, -'0'));
4737 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
Ben Murdoch257744e2011-11-30 15:57:28 +00004738 __ j(above, &not_array_index, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004739 __ leal(scratch, Operand(c2, -'0'));
4740 __ cmpl(scratch, Immediate(static_cast<int>('9' - '0')));
4741 __ j(below_equal, not_found);
4742
4743 __ bind(&not_array_index);
4744 // Calculate the two character string hash.
4745 Register hash = scratch1;
4746 GenerateHashInit(masm, hash, c1, scratch);
4747 GenerateHashAddCharacter(masm, hash, c2, scratch);
4748 GenerateHashGetHash(masm, hash, scratch);
4749
4750 // Collect the two characters in a register.
4751 Register chars = c1;
4752 __ shl(c2, Immediate(kBitsPerByte));
4753 __ orl(chars, c2);
4754
4755 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4756 // hash: hash of two character string.
4757
4758 // Load the symbol table.
4759 Register symbol_table = c2;
4760 __ LoadRoot(symbol_table, Heap::kSymbolTableRootIndex);
4761
4762 // Calculate capacity mask from the symbol table capacity.
4763 Register mask = scratch2;
4764 __ SmiToInteger32(mask,
4765 FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
4766 __ decl(mask);
4767
Steve Block44f0eee2011-05-26 01:26:41 +01004768 Register map = scratch4;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004769
4770 // Registers
4771 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
4772 // hash: hash of two character string (32-bit int)
4773 // symbol_table: symbol table
4774 // mask: capacity mask (32-bit int)
Steve Block44f0eee2011-05-26 01:26:41 +01004775 // map: -
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004776 // scratch: -
4777
4778 // Perform a number of probes in the symbol table.
4779 static const int kProbes = 4;
4780 Label found_in_symbol_table;
4781 Label next_probe[kProbes];
Ben Murdoch692be652012-01-10 18:47:50 +00004782 Register candidate = scratch; // Scratch register contains candidate.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004783 for (int i = 0; i < kProbes; i++) {
4784 // Calculate entry in symbol table.
4785 __ movl(scratch, hash);
4786 if (i > 0) {
4787 __ addl(scratch, Immediate(SymbolTable::GetProbeOffset(i)));
4788 }
4789 __ andl(scratch, mask);
4790
Steve Block44f0eee2011-05-26 01:26:41 +01004791 // Load the entry from the symbol table.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004792 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
4793 __ movq(candidate,
4794 FieldOperand(symbol_table,
4795 scratch,
4796 times_pointer_size,
4797 SymbolTable::kElementsStartOffset));
4798
4799 // If entry is undefined no string with this hash can be found.
Ben Murdoch257744e2011-11-30 15:57:28 +00004800 Label is_string;
Steve Block44f0eee2011-05-26 01:26:41 +01004801 __ CmpObjectType(candidate, ODDBALL_TYPE, map);
Ben Murdoch257744e2011-11-30 15:57:28 +00004802 __ j(not_equal, &is_string, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01004803
4804 __ CompareRoot(candidate, Heap::kUndefinedValueRootIndex);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004805 __ j(equal, not_found);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004806 // Must be the hole (deleted entry).
4807 if (FLAG_debug_code) {
4808 __ LoadRoot(kScratchRegister, Heap::kTheHoleValueRootIndex);
4809 __ cmpq(kScratchRegister, candidate);
4810 __ Assert(equal, "oddball in symbol table is not undefined or the hole");
4811 }
Steve Block44f0eee2011-05-26 01:26:41 +01004812 __ jmp(&next_probe[i]);
4813
4814 __ bind(&is_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004815
4816 // If length is not 2 the string is not a candidate.
4817 __ SmiCompare(FieldOperand(candidate, String::kLengthOffset),
4818 Smi::FromInt(2));
4819 __ j(not_equal, &next_probe[i]);
4820
4821 // We use kScratchRegister as a temporary register in assumption that
4822 // JumpIfInstanceTypeIsNotSequentialAscii does not use it implicitly
4823 Register temp = kScratchRegister;
4824
4825 // Check that the candidate is a non-external ascii string.
Steve Block44f0eee2011-05-26 01:26:41 +01004826 __ movzxbl(temp, FieldOperand(map, Map::kInstanceTypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004827 __ JumpIfInstanceTypeIsNotSequentialAscii(
4828 temp, temp, &next_probe[i]);
4829
4830 // Check if the two characters match.
4831 __ movl(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
4832 __ andl(temp, Immediate(0x0000ffff));
4833 __ cmpl(chars, temp);
4834 __ j(equal, &found_in_symbol_table);
4835 __ bind(&next_probe[i]);
4836 }
4837
4838 // No matching 2 character string found by probing.
4839 __ jmp(not_found);
4840
4841 // Scratch register contains result when we fall through to here.
Ben Murdoch692be652012-01-10 18:47:50 +00004842 Register result = candidate;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004843 __ bind(&found_in_symbol_table);
4844 if (!result.is(rax)) {
4845 __ movq(rax, result);
4846 }
4847}
4848
4849
4850void StringHelper::GenerateHashInit(MacroAssembler* masm,
4851 Register hash,
4852 Register character,
4853 Register scratch) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004854 // hash = character + (character << 10);
4855 __ movl(hash, character);
4856 __ shll(hash, Immediate(10));
4857 __ addl(hash, character);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004858 // hash ^= hash >> 6;
4859 __ movl(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00004860 __ shrl(scratch, Immediate(6));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004861 __ xorl(hash, scratch);
4862}
4863
4864
4865void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
4866 Register hash,
4867 Register character,
4868 Register scratch) {
4869 // hash += character;
4870 __ addl(hash, character);
4871 // hash += hash << 10;
4872 __ movl(scratch, hash);
4873 __ shll(scratch, Immediate(10));
4874 __ addl(hash, scratch);
4875 // hash ^= hash >> 6;
4876 __ movl(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00004877 __ shrl(scratch, Immediate(6));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004878 __ xorl(hash, scratch);
4879}
4880
4881
4882void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
4883 Register hash,
4884 Register scratch) {
4885 // hash += hash << 3;
4886 __ leal(hash, Operand(hash, hash, times_8, 0));
4887 // hash ^= hash >> 11;
4888 __ movl(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00004889 __ shrl(scratch, Immediate(11));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004890 __ xorl(hash, scratch);
4891 // hash += hash << 15;
4892 __ movl(scratch, hash);
4893 __ shll(scratch, Immediate(15));
4894 __ addl(hash, scratch);
4895
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004896 uint32_t kHashShiftCutOffMask = (1 << (32 - String::kHashShift)) - 1;
4897 __ andl(hash, Immediate(kHashShiftCutOffMask));
Ben Murdoch692be652012-01-10 18:47:50 +00004898
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004899 // if (hash == 0) hash = 27;
4900 Label hash_not_zero;
4901 __ j(not_zero, &hash_not_zero);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004902 __ Set(hash, 27);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004903 __ bind(&hash_not_zero);
4904}
4905
4906void SubStringStub::Generate(MacroAssembler* masm) {
4907 Label runtime;
4908
4909 // Stack frame on entry.
4910 // rsp[0]: return address
4911 // rsp[8]: to
4912 // rsp[16]: from
4913 // rsp[24]: string
4914
4915 const int kToOffset = 1 * kPointerSize;
4916 const int kFromOffset = kToOffset + kPointerSize;
4917 const int kStringOffset = kFromOffset + kPointerSize;
4918 const int kArgumentsSize = (kStringOffset + kPointerSize) - kToOffset;
4919
4920 // Make sure first argument is a string.
4921 __ movq(rax, Operand(rsp, kStringOffset));
4922 STATIC_ASSERT(kSmiTag == 0);
4923 __ testl(rax, Immediate(kSmiTagMask));
4924 __ j(zero, &runtime);
4925 Condition is_string = masm->IsObjectStringType(rax, rbx, rbx);
4926 __ j(NegateCondition(is_string), &runtime);
4927
4928 // rax: string
4929 // rbx: instance type
4930 // Calculate length of sub string using the smi values.
4931 Label result_longer_than_two;
4932 __ movq(rcx, Operand(rsp, kToOffset));
4933 __ movq(rdx, Operand(rsp, kFromOffset));
Ben Murdochf87a2032010-10-22 12:50:53 +01004934 __ JumpUnlessBothNonNegativeSmi(rcx, rdx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004935
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004936 __ SmiSub(rcx, rcx, rdx); // Overflow doesn't happen.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004937 __ cmpq(FieldOperand(rax, String::kLengthOffset), rcx);
4938 Label return_rax;
4939 __ j(equal, &return_rax);
4940 // Special handling of sub-strings of length 1 and 2. One character strings
4941 // are handled in the runtime system (looked up in the single character
4942 // cache). Two character strings are looked for in the symbol cache.
4943 __ SmiToInteger32(rcx, rcx);
4944 __ cmpl(rcx, Immediate(2));
4945 __ j(greater, &result_longer_than_two);
4946 __ j(less, &runtime);
4947
4948 // Sub string of length 2 requested.
4949 // rax: string
4950 // rbx: instance type
4951 // rcx: sub string length (value is 2)
4952 // rdx: from index (smi)
4953 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &runtime);
4954
4955 // Get the two characters forming the sub string.
4956 __ SmiToInteger32(rdx, rdx); // From index is no longer smi.
4957 __ movzxbq(rbx, FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize));
4958 __ movzxbq(rcx,
4959 FieldOperand(rax, rdx, times_1, SeqAsciiString::kHeaderSize + 1));
4960
4961 // Try to lookup two character string in symbol table.
4962 Label make_two_character_string;
4963 StringHelper::GenerateTwoCharacterSymbolTableProbe(
4964 masm, rbx, rcx, rax, rdx, rdi, r14, &make_two_character_string);
4965 __ ret(3 * kPointerSize);
4966
4967 __ bind(&make_two_character_string);
4968 // Setup registers for allocating the two character string.
4969 __ movq(rax, Operand(rsp, kStringOffset));
4970 __ movq(rbx, FieldOperand(rax, HeapObject::kMapOffset));
4971 __ movzxbl(rbx, FieldOperand(rbx, Map::kInstanceTypeOffset));
4972 __ Set(rcx, 2);
4973
Ben Murdoch589d6972011-11-30 16:04:58 +00004974 if (FLAG_string_slices) {
4975 Label copy_routine;
4976 // If coming from the make_two_character_string path, the string
4977 // is too short to be sliced anyways.
4978 STATIC_ASSERT(2 < SlicedString::kMinLength);
4979 __ jmp(&copy_routine);
4980 __ bind(&result_longer_than_two);
4981
4982 // rax: string
4983 // rbx: instance type
4984 // rcx: sub string length
4985 // rdx: from index (smi)
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004986 Label allocate_slice, sliced_string, seq_or_external_string;
Ben Murdoch589d6972011-11-30 16:04:58 +00004987 __ cmpq(rcx, Immediate(SlicedString::kMinLength));
4988 // Short slice. Copy instead of slicing.
4989 __ j(less, &copy_routine);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004990 // If the string is not indirect, it can only be sequential or external.
Ben Murdoch589d6972011-11-30 16:04:58 +00004991 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
4992 STATIC_ASSERT(kIsIndirectStringMask != 0);
4993 __ testb(rbx, Immediate(kIsIndirectStringMask));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004994 __ j(zero, &seq_or_external_string, Label::kNear);
Ben Murdoch589d6972011-11-30 16:04:58 +00004995
4996 __ testb(rbx, Immediate(kSlicedNotConsMask));
4997 __ j(not_zero, &sliced_string, Label::kNear);
4998 // Cons string. Check whether it is flat, then fetch first part.
4999 __ CompareRoot(FieldOperand(rax, ConsString::kSecondOffset),
5000 Heap::kEmptyStringRootIndex);
5001 __ j(not_equal, &runtime);
5002 __ movq(rdi, FieldOperand(rax, ConsString::kFirstOffset));
5003 __ jmp(&allocate_slice, Label::kNear);
5004
5005 __ bind(&sliced_string);
5006 // Sliced string. Fetch parent and correct start index by offset.
5007 __ addq(rdx, FieldOperand(rax, SlicedString::kOffsetOffset));
5008 __ movq(rdi, FieldOperand(rax, SlicedString::kParentOffset));
5009 __ jmp(&allocate_slice, Label::kNear);
5010
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005011 __ bind(&seq_or_external_string);
5012 // Sequential or external string. Just move string to the correct register.
Ben Murdoch589d6972011-11-30 16:04:58 +00005013 __ movq(rdi, rax);
5014
5015 __ bind(&allocate_slice);
5016 // edi: underlying subject string
5017 // ebx: instance type of original subject string
5018 // edx: offset
5019 // ecx: length
5020 // Allocate new sliced string. At this point we do not reload the instance
5021 // type including the string encoding because we simply rely on the info
5022 // provided by the original string. It does not matter if the original
5023 // string's encoding is wrong because we always have to recheck encoding of
5024 // the newly created string's parent anyways due to externalized strings.
5025 Label two_byte_slice, set_slice_header;
5026 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
5027 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
5028 __ testb(rbx, Immediate(kStringEncodingMask));
5029 __ j(zero, &two_byte_slice, Label::kNear);
5030 __ AllocateAsciiSlicedString(rax, rbx, no_reg, &runtime);
5031 __ jmp(&set_slice_header, Label::kNear);
5032 __ bind(&two_byte_slice);
5033 __ AllocateTwoByteSlicedString(rax, rbx, no_reg, &runtime);
5034 __ bind(&set_slice_header);
5035 __ movq(FieldOperand(rax, SlicedString::kOffsetOffset), rdx);
5036 __ Integer32ToSmi(rcx, rcx);
5037 __ movq(FieldOperand(rax, SlicedString::kLengthOffset), rcx);
5038 __ movq(FieldOperand(rax, SlicedString::kParentOffset), rdi);
5039 __ movq(FieldOperand(rax, SlicedString::kHashFieldOffset),
5040 Immediate(String::kEmptyHashField));
5041 __ jmp(&return_rax);
5042
5043 __ bind(&copy_routine);
5044 } else {
5045 __ bind(&result_longer_than_two);
5046 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005047
5048 // rax: string
5049 // rbx: instance type
5050 // rcx: result string length
5051 // Check for flat ascii string
5052 Label non_ascii_flat;
5053 __ JumpIfInstanceTypeIsNotSequentialAscii(rbx, rbx, &non_ascii_flat);
5054
5055 // Allocate the result.
5056 __ AllocateAsciiString(rax, rcx, rbx, rdx, rdi, &runtime);
5057
5058 // rax: result string
5059 // rcx: result string length
5060 __ movq(rdx, rsi); // esi used by following code.
5061 // Locate first character of result.
5062 __ lea(rdi, FieldOperand(rax, SeqAsciiString::kHeaderSize));
5063 // Load string argument and locate character of sub string start.
5064 __ movq(rsi, Operand(rsp, kStringOffset));
5065 __ movq(rbx, Operand(rsp, kFromOffset));
5066 {
5067 SmiIndex smi_as_index = masm->SmiToIndex(rbx, rbx, times_1);
5068 __ lea(rsi, Operand(rsi, smi_as_index.reg, smi_as_index.scale,
5069 SeqAsciiString::kHeaderSize - kHeapObjectTag));
5070 }
5071
5072 // rax: result string
5073 // rcx: result length
5074 // rdx: original value of rsi
5075 // rdi: first character of result
5076 // rsi: character of sub string start
5077 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, true);
5078 __ movq(rsi, rdx); // Restore rsi.
Steve Block44f0eee2011-05-26 01:26:41 +01005079 Counters* counters = masm->isolate()->counters();
5080 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005081 __ ret(kArgumentsSize);
5082
5083 __ bind(&non_ascii_flat);
5084 // rax: string
5085 // rbx: instance type & kStringRepresentationMask | kStringEncodingMask
5086 // rcx: result string length
5087 // Check for sequential two byte string
5088 __ cmpb(rbx, Immediate(kSeqStringTag | kTwoByteStringTag));
5089 __ j(not_equal, &runtime);
5090
5091 // Allocate the result.
5092 __ AllocateTwoByteString(rax, rcx, rbx, rdx, rdi, &runtime);
5093
5094 // rax: result string
5095 // rcx: result string length
5096 __ movq(rdx, rsi); // esi used by following code.
5097 // Locate first character of result.
5098 __ lea(rdi, FieldOperand(rax, SeqTwoByteString::kHeaderSize));
5099 // Load string argument and locate character of sub string start.
5100 __ movq(rsi, Operand(rsp, kStringOffset));
5101 __ movq(rbx, Operand(rsp, kFromOffset));
5102 {
5103 SmiIndex smi_as_index = masm->SmiToIndex(rbx, rbx, times_2);
5104 __ lea(rsi, Operand(rsi, smi_as_index.reg, smi_as_index.scale,
5105 SeqAsciiString::kHeaderSize - kHeapObjectTag));
5106 }
5107
5108 // rax: result string
5109 // rcx: result length
5110 // rdx: original value of rsi
5111 // rdi: first character of result
5112 // rsi: character of sub string start
5113 StringHelper::GenerateCopyCharactersREP(masm, rdi, rsi, rcx, false);
5114 __ movq(rsi, rdx); // Restore esi.
5115
5116 __ bind(&return_rax);
Steve Block44f0eee2011-05-26 01:26:41 +01005117 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005118 __ ret(kArgumentsSize);
5119
5120 // Just jump to runtime to create the sub string.
5121 __ bind(&runtime);
5122 __ TailCallRuntime(Runtime::kSubString, 3, 1);
5123}
5124
5125
Ben Murdoch257744e2011-11-30 15:57:28 +00005126void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
5127 Register left,
5128 Register right,
5129 Register scratch1,
5130 Register scratch2) {
5131 Register length = scratch1;
5132
5133 // Compare lengths.
5134 Label check_zero_length;
5135 __ movq(length, FieldOperand(left, String::kLengthOffset));
5136 __ SmiCompare(length, FieldOperand(right, String::kLengthOffset));
5137 __ j(equal, &check_zero_length, Label::kNear);
5138 __ Move(rax, Smi::FromInt(NOT_EQUAL));
5139 __ ret(0);
5140
5141 // Check if the length is zero.
5142 Label compare_chars;
5143 __ bind(&check_zero_length);
5144 STATIC_ASSERT(kSmiTag == 0);
5145 __ SmiTest(length);
5146 __ j(not_zero, &compare_chars, Label::kNear);
5147 __ Move(rax, Smi::FromInt(EQUAL));
5148 __ ret(0);
5149
5150 // Compare characters.
5151 __ bind(&compare_chars);
5152 Label strings_not_equal;
5153 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
5154 &strings_not_equal, Label::kNear);
5155
5156 // Characters are equal.
5157 __ Move(rax, Smi::FromInt(EQUAL));
5158 __ ret(0);
5159
5160 // Characters are not equal.
5161 __ bind(&strings_not_equal);
5162 __ Move(rax, Smi::FromInt(NOT_EQUAL));
5163 __ ret(0);
5164}
5165
5166
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005167void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
5168 Register left,
5169 Register right,
5170 Register scratch1,
5171 Register scratch2,
5172 Register scratch3,
5173 Register scratch4) {
5174 // Ensure that you can always subtract a string length from a non-negative
5175 // number (e.g. another length).
5176 STATIC_ASSERT(String::kMaxLength < 0x7fffffff);
5177
5178 // Find minimum length and length difference.
5179 __ movq(scratch1, FieldOperand(left, String::kLengthOffset));
5180 __ movq(scratch4, scratch1);
5181 __ SmiSub(scratch4,
5182 scratch4,
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005183 FieldOperand(right, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005184 // Register scratch4 now holds left.length - right.length.
5185 const Register length_difference = scratch4;
Ben Murdoch257744e2011-11-30 15:57:28 +00005186 Label left_shorter;
5187 __ j(less, &left_shorter, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005188 // The right string isn't longer that the left one.
5189 // Get the right string's length by subtracting the (non-negative) difference
5190 // from the left string's length.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005191 __ SmiSub(scratch1, scratch1, length_difference);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005192 __ bind(&left_shorter);
5193 // Register scratch1 now holds Min(left.length, right.length).
5194 const Register min_length = scratch1;
5195
Ben Murdoch257744e2011-11-30 15:57:28 +00005196 Label compare_lengths;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005197 // If min-length is zero, go directly to comparing lengths.
5198 __ SmiTest(min_length);
Ben Murdoch257744e2011-11-30 15:57:28 +00005199 __ j(zero, &compare_lengths, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005200
Ben Murdoch257744e2011-11-30 15:57:28 +00005201 // Compare loop.
5202 Label result_not_equal;
5203 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
5204 &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005205
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005206 // Completed loop without finding different characters.
5207 // Compare lengths (precomputed).
5208 __ bind(&compare_lengths);
5209 __ SmiTest(length_difference);
Ben Murdoch257744e2011-11-30 15:57:28 +00005210 __ j(not_zero, &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005211
5212 // Result is EQUAL.
5213 __ Move(rax, Smi::FromInt(EQUAL));
5214 __ ret(0);
5215
Ben Murdoch257744e2011-11-30 15:57:28 +00005216 Label result_greater;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005217 __ bind(&result_not_equal);
5218 // Unequal comparison of left to right, either character or length.
Ben Murdoch257744e2011-11-30 15:57:28 +00005219 __ j(greater, &result_greater, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005220
5221 // Result is LESS.
5222 __ Move(rax, Smi::FromInt(LESS));
5223 __ ret(0);
5224
5225 // Result is GREATER.
5226 __ bind(&result_greater);
5227 __ Move(rax, Smi::FromInt(GREATER));
5228 __ ret(0);
5229}
5230
5231
Ben Murdoch257744e2011-11-30 15:57:28 +00005232void StringCompareStub::GenerateAsciiCharsCompareLoop(
5233 MacroAssembler* masm,
5234 Register left,
5235 Register right,
5236 Register length,
5237 Register scratch,
5238 Label* chars_not_equal,
5239 Label::Distance near_jump) {
5240 // Change index to run from -length to -1 by adding length to string
5241 // start. This means that loop ends when index reaches zero, which
5242 // doesn't need an additional compare.
5243 __ SmiToInteger32(length, length);
5244 __ lea(left,
5245 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
5246 __ lea(right,
5247 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
5248 __ neg(length);
5249 Register index = length; // index = -length;
5250
5251 // Compare loop.
5252 Label loop;
5253 __ bind(&loop);
5254 __ movb(scratch, Operand(left, index, times_1, 0));
5255 __ cmpb(scratch, Operand(right, index, times_1, 0));
5256 __ j(not_equal, chars_not_equal, near_jump);
5257 __ addq(index, Immediate(1));
5258 __ j(not_zero, &loop);
5259}
5260
5261
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005262void StringCompareStub::Generate(MacroAssembler* masm) {
5263 Label runtime;
5264
5265 // Stack frame on entry.
5266 // rsp[0]: return address
5267 // rsp[8]: right string
5268 // rsp[16]: left string
5269
5270 __ movq(rdx, Operand(rsp, 2 * kPointerSize)); // left
5271 __ movq(rax, Operand(rsp, 1 * kPointerSize)); // right
5272
5273 // Check for identity.
Ben Murdoch257744e2011-11-30 15:57:28 +00005274 Label not_same;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005275 __ cmpq(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005276 __ j(not_equal, &not_same, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005277 __ Move(rax, Smi::FromInt(EQUAL));
Steve Block44f0eee2011-05-26 01:26:41 +01005278 Counters* counters = masm->isolate()->counters();
5279 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005280 __ ret(2 * kPointerSize);
5281
5282 __ bind(&not_same);
5283
5284 // Check that both are sequential ASCII strings.
5285 __ JumpIfNotBothSequentialAsciiStrings(rdx, rax, rcx, rbx, &runtime);
5286
5287 // Inline comparison of ascii strings.
Steve Block44f0eee2011-05-26 01:26:41 +01005288 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005289 // Drop arguments from the stack
5290 __ pop(rcx);
5291 __ addq(rsp, Immediate(2 * kPointerSize));
5292 __ push(rcx);
5293 GenerateCompareFlatAsciiStrings(masm, rdx, rax, rcx, rbx, rdi, r8);
5294
5295 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
5296 // tagged as a small integer.
5297 __ bind(&runtime);
5298 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
5299}
5300
Ben Murdoche0cee9b2011-05-25 10:26:03 +01005301
Ben Murdochb0fe1622011-05-05 13:52:32 +01005302void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005303 ASSERT(state_ == CompareIC::SMIS);
Ben Murdoch257744e2011-11-30 15:57:28 +00005304 Label miss;
5305 __ JumpIfNotBothSmi(rdx, rax, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005306
5307 if (GetCondition() == equal) {
5308 // For equality we do not care about the sign of the result.
5309 __ subq(rax, rdx);
5310 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00005311 Label done;
Steve Block1e0659c2011-05-24 12:43:12 +01005312 __ subq(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005313 __ j(no_overflow, &done, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005314 // Correct sign of result in case of overflow.
5315 __ SmiNot(rdx, rdx);
5316 __ bind(&done);
5317 __ movq(rax, rdx);
5318 }
5319 __ ret(0);
5320
5321 __ bind(&miss);
5322 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005323}
5324
5325
5326void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005327 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
5328
Ben Murdoch257744e2011-11-30 15:57:28 +00005329 Label generic_stub;
5330 Label unordered;
5331 Label miss;
Steve Block1e0659c2011-05-24 12:43:12 +01005332 Condition either_smi = masm->CheckEitherSmi(rax, rdx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005333 __ j(either_smi, &generic_stub, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005334
5335 __ CmpObjectType(rax, HEAP_NUMBER_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005336 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005337 __ CmpObjectType(rdx, HEAP_NUMBER_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005338 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005339
5340 // Load left and right operand
5341 __ movsd(xmm0, FieldOperand(rdx, HeapNumber::kValueOffset));
5342 __ movsd(xmm1, FieldOperand(rax, HeapNumber::kValueOffset));
5343
5344 // Compare operands
5345 __ ucomisd(xmm0, xmm1);
5346
5347 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch257744e2011-11-30 15:57:28 +00005348 __ j(parity_even, &unordered, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005349
5350 // Return a result of -1, 0, or 1, based on EFLAGS.
5351 // Performing mov, because xor would destroy the flag register.
5352 __ movl(rax, Immediate(0));
5353 __ movl(rcx, Immediate(0));
5354 __ setcc(above, rax); // Add one to zero if carry clear and not equal.
5355 __ sbbq(rax, rcx); // Subtract one if below (aka. carry set).
5356 __ ret(0);
5357
5358 __ bind(&unordered);
5359
5360 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
5361 __ bind(&generic_stub);
5362 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
5363
5364 __ bind(&miss);
5365 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005366}
5367
5368
Ben Murdoch257744e2011-11-30 15:57:28 +00005369void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
5370 ASSERT(state_ == CompareIC::SYMBOLS);
5371 ASSERT(GetCondition() == equal);
5372
5373 // Registers containing left and right operands respectively.
5374 Register left = rdx;
5375 Register right = rax;
5376 Register tmp1 = rcx;
5377 Register tmp2 = rbx;
5378
5379 // Check that both operands are heap objects.
5380 Label miss;
5381 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
5382 __ j(cond, &miss, Label::kNear);
5383
5384 // Check that both operands are symbols.
5385 __ movq(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5386 __ movq(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5387 __ movzxbq(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5388 __ movzxbq(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5389 STATIC_ASSERT(kSymbolTag != 0);
5390 __ and_(tmp1, tmp2);
5391 __ testb(tmp1, Immediate(kIsSymbolMask));
5392 __ j(zero, &miss, Label::kNear);
5393
5394 // Symbols are compared by identity.
5395 Label done;
5396 __ cmpq(left, right);
5397 // Make sure rax is non-zero. At this point input operands are
5398 // guaranteed to be non-zero.
5399 ASSERT(right.is(rax));
5400 __ j(not_equal, &done, Label::kNear);
5401 STATIC_ASSERT(EQUAL == 0);
5402 STATIC_ASSERT(kSmiTag == 0);
5403 __ Move(rax, Smi::FromInt(EQUAL));
5404 __ bind(&done);
5405 __ ret(0);
5406
5407 __ bind(&miss);
5408 GenerateMiss(masm);
5409}
5410
5411
5412void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
5413 ASSERT(state_ == CompareIC::STRINGS);
5414 ASSERT(GetCondition() == equal);
5415 Label miss;
5416
5417 // Registers containing left and right operands respectively.
5418 Register left = rdx;
5419 Register right = rax;
5420 Register tmp1 = rcx;
5421 Register tmp2 = rbx;
5422 Register tmp3 = rdi;
5423
5424 // Check that both operands are heap objects.
5425 Condition cond = masm->CheckEitherSmi(left, right, tmp1);
5426 __ j(cond, &miss);
5427
5428 // Check that both operands are strings. This leaves the instance
5429 // types loaded in tmp1 and tmp2.
5430 __ movq(tmp1, FieldOperand(left, HeapObject::kMapOffset));
5431 __ movq(tmp2, FieldOperand(right, HeapObject::kMapOffset));
5432 __ movzxbq(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
5433 __ movzxbq(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
5434 __ movq(tmp3, tmp1);
5435 STATIC_ASSERT(kNotStringTag != 0);
5436 __ or_(tmp3, tmp2);
5437 __ testb(tmp3, Immediate(kIsNotStringMask));
5438 __ j(not_zero, &miss);
5439
5440 // Fast check for identical strings.
5441 Label not_same;
5442 __ cmpq(left, right);
5443 __ j(not_equal, &not_same, Label::kNear);
5444 STATIC_ASSERT(EQUAL == 0);
5445 STATIC_ASSERT(kSmiTag == 0);
5446 __ Move(rax, Smi::FromInt(EQUAL));
5447 __ ret(0);
5448
5449 // Handle not identical strings.
5450 __ bind(&not_same);
5451
5452 // Check that both strings are symbols. If they are, we're done
5453 // because we already know they are not identical.
5454 Label do_compare;
5455 STATIC_ASSERT(kSymbolTag != 0);
5456 __ and_(tmp1, tmp2);
5457 __ testb(tmp1, Immediate(kIsSymbolMask));
5458 __ j(zero, &do_compare, Label::kNear);
5459 // Make sure rax is non-zero. At this point input operands are
5460 // guaranteed to be non-zero.
5461 ASSERT(right.is(rax));
5462 __ ret(0);
5463
5464 // Check that both strings are sequential ASCII.
5465 Label runtime;
5466 __ bind(&do_compare);
5467 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
5468
5469 // Compare flat ASCII strings. Returns when done.
5470 StringCompareStub::GenerateFlatAsciiStringEquals(
5471 masm, left, right, tmp1, tmp2);
5472
5473 // Handle more complex cases in runtime.
5474 __ bind(&runtime);
5475 __ pop(tmp1); // Return address.
5476 __ push(left);
5477 __ push(right);
5478 __ push(tmp1);
5479 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
5480
5481 __ bind(&miss);
5482 GenerateMiss(masm);
5483}
5484
5485
Ben Murdochb0fe1622011-05-05 13:52:32 +01005486void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005487 ASSERT(state_ == CompareIC::OBJECTS);
Ben Murdoch257744e2011-11-30 15:57:28 +00005488 Label miss;
Steve Block1e0659c2011-05-24 12:43:12 +01005489 Condition either_smi = masm->CheckEitherSmi(rdx, rax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005490 __ j(either_smi, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005491
5492 __ CmpObjectType(rax, JS_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005493 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005494 __ CmpObjectType(rdx, JS_OBJECT_TYPE, rcx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005495 __ j(not_equal, &miss, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01005496
5497 ASSERT(GetCondition() == equal);
5498 __ subq(rax, rdx);
5499 __ ret(0);
5500
5501 __ bind(&miss);
5502 GenerateMiss(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005503}
5504
5505
5506void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
Steve Block1e0659c2011-05-24 12:43:12 +01005507 // Save the registers.
5508 __ pop(rcx);
5509 __ push(rdx);
5510 __ push(rax);
5511 __ push(rcx);
5512
5513 // Call the runtime system in a fresh internal frame.
Steve Block44f0eee2011-05-26 01:26:41 +01005514 ExternalReference miss =
5515 ExternalReference(IC_Utility(IC::kCompareIC_Miss), masm->isolate());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005516 {
5517 FrameScope scope(masm, StackFrame::INTERNAL);
5518 __ push(rdx);
5519 __ push(rax);
5520 __ Push(Smi::FromInt(op_));
5521 __ CallExternalReference(miss, 3);
5522 }
Steve Block1e0659c2011-05-24 12:43:12 +01005523
5524 // Compute the entry point of the rewritten stub.
5525 __ lea(rdi, FieldOperand(rax, Code::kHeaderSize));
5526
5527 // Restore registers.
5528 __ pop(rcx);
5529 __ pop(rax);
5530 __ pop(rdx);
5531 __ push(rcx);
5532
5533 // Do a tail call to the rewritten stub.
5534 __ jmp(rdi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005535}
5536
Steve Block1e0659c2011-05-24 12:43:12 +01005537
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005538void StringDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
5539 Label* miss,
5540 Label* done,
5541 Register properties,
5542 Handle<String> name,
5543 Register r0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005544 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5545 // not equal to the name and kProbes-th slot is not used (its name is the
5546 // undefined value), it guarantees the hash table doesn't contain the
5547 // property. It's true even if some slots represent deleted properties
5548 // (their names are the null value).
5549 for (int i = 0; i < kInlinedProbes; i++) {
5550 // r0 points to properties hash.
5551 // Compute the masked index: (hash + i + i * i) & mask.
5552 Register index = r0;
5553 // Capacity is smi 2^n.
5554 __ SmiToInteger32(index, FieldOperand(properties, kCapacityOffset));
5555 __ decl(index);
5556 __ and_(index,
5557 Immediate(name->Hash() + StringDictionary::GetProbeOffset(i)));
5558
5559 // Scale the index by multiplying by the entry size.
5560 ASSERT(StringDictionary::kEntrySize == 3);
5561 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
5562
5563 Register entity_name = r0;
5564 // Having undefined at this place means the name is not contained.
5565 ASSERT_EQ(kSmiTagSize, 1);
5566 __ movq(entity_name, Operand(properties,
5567 index,
5568 times_pointer_size,
5569 kElementsStartOffset - kHeapObjectTag));
5570 __ Cmp(entity_name, masm->isolate()->factory()->undefined_value());
5571 __ j(equal, done);
5572
5573 // Stop if found the property.
5574 __ Cmp(entity_name, Handle<String>(name));
5575 __ j(equal, miss);
5576
5577 // Check if the entry name is not a symbol.
5578 __ movq(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
5579 __ testb(FieldOperand(entity_name, Map::kInstanceTypeOffset),
5580 Immediate(kIsSymbolMask));
5581 __ j(zero, miss);
5582 }
5583
5584 StringDictionaryLookupStub stub(properties,
5585 r0,
5586 r0,
5587 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
5588 __ Push(Handle<Object>(name));
5589 __ push(Immediate(name->Hash()));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005590 __ CallStub(&stub);
Ben Murdoch257744e2011-11-30 15:57:28 +00005591 __ testq(r0, r0);
5592 __ j(not_zero, miss);
5593 __ jmp(done);
Ben Murdoch257744e2011-11-30 15:57:28 +00005594}
5595
5596
5597// Probe the string dictionary in the |elements| register. Jump to the
5598// |done| label if a property with the given name is found leaving the
5599// index into the dictionary in |r1|. Jump to the |miss| label
5600// otherwise.
5601void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
5602 Label* miss,
5603 Label* done,
5604 Register elements,
5605 Register name,
5606 Register r0,
5607 Register r1) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005608 ASSERT(!elements.is(r0));
5609 ASSERT(!elements.is(r1));
5610 ASSERT(!name.is(r0));
5611 ASSERT(!name.is(r1));
5612
Ben Murdoch257744e2011-11-30 15:57:28 +00005613 // Assert that name contains a string.
5614 if (FLAG_debug_code) __ AbortIfNotString(name);
5615
5616 __ SmiToInteger32(r0, FieldOperand(elements, kCapacityOffset));
5617 __ decl(r0);
5618
5619 for (int i = 0; i < kInlinedProbes; i++) {
5620 // Compute the masked index: (hash + i + i * i) & mask.
5621 __ movl(r1, FieldOperand(name, String::kHashFieldOffset));
5622 __ shrl(r1, Immediate(String::kHashShift));
5623 if (i > 0) {
5624 __ addl(r1, Immediate(StringDictionary::GetProbeOffset(i)));
5625 }
5626 __ and_(r1, r0);
5627
5628 // Scale the index by multiplying by the entry size.
5629 ASSERT(StringDictionary::kEntrySize == 3);
5630 __ lea(r1, Operand(r1, r1, times_2, 0)); // r1 = r1 * 3
5631
5632 // Check if the key is identical to the name.
5633 __ cmpq(name, Operand(elements, r1, times_pointer_size,
5634 kElementsStartOffset - kHeapObjectTag));
5635 __ j(equal, done);
5636 }
5637
5638 StringDictionaryLookupStub stub(elements,
5639 r0,
5640 r1,
5641 POSITIVE_LOOKUP);
5642 __ push(name);
5643 __ movl(r0, FieldOperand(name, String::kHashFieldOffset));
5644 __ shrl(r0, Immediate(String::kHashShift));
5645 __ push(r0);
5646 __ CallStub(&stub);
5647
5648 __ testq(r0, r0);
5649 __ j(zero, miss);
5650 __ jmp(done);
5651}
5652
5653
5654void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005655 // This stub overrides SometimesSetsUpAFrame() to return false. That means
5656 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00005657 // Stack frame on entry:
5658 // esp[0 * kPointerSize]: return address.
5659 // esp[1 * kPointerSize]: key's hash.
5660 // esp[2 * kPointerSize]: key.
5661 // Registers:
5662 // dictionary_: StringDictionary to probe.
5663 // result_: used as scratch.
5664 // index_: will hold an index of entry if lookup is successful.
5665 // might alias with result_.
5666 // Returns:
5667 // result_ is zero if lookup failed, non zero otherwise.
5668
5669 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
5670
5671 Register scratch = result_;
5672
5673 __ SmiToInteger32(scratch, FieldOperand(dictionary_, kCapacityOffset));
5674 __ decl(scratch);
5675 __ push(scratch);
5676
5677 // If names of slots in range from 1 to kProbes - 1 for the hash value are
5678 // not equal to the name and kProbes-th slot is not used (its name is the
5679 // undefined value), it guarantees the hash table doesn't contain the
5680 // property. It's true even if some slots represent deleted properties
5681 // (their names are the null value).
5682 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
5683 // Compute the masked index: (hash + i + i * i) & mask.
5684 __ movq(scratch, Operand(rsp, 2 * kPointerSize));
5685 if (i > 0) {
5686 __ addl(scratch, Immediate(StringDictionary::GetProbeOffset(i)));
5687 }
5688 __ and_(scratch, Operand(rsp, 0));
5689
5690 // Scale the index by multiplying by the entry size.
5691 ASSERT(StringDictionary::kEntrySize == 3);
5692 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
5693
5694 // Having undefined at this place means the name is not contained.
5695 __ movq(scratch, Operand(dictionary_,
5696 index_,
5697 times_pointer_size,
5698 kElementsStartOffset - kHeapObjectTag));
5699
5700 __ Cmp(scratch, masm->isolate()->factory()->undefined_value());
5701 __ j(equal, &not_in_dictionary);
5702
5703 // Stop if found the property.
5704 __ cmpq(scratch, Operand(rsp, 3 * kPointerSize));
5705 __ j(equal, &in_dictionary);
5706
5707 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
5708 // If we hit a non symbol key during negative lookup
5709 // we have to bailout as this key might be equal to the
5710 // key we are looking for.
5711
5712 // Check if the entry name is not a symbol.
5713 __ movq(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
5714 __ testb(FieldOperand(scratch, Map::kInstanceTypeOffset),
5715 Immediate(kIsSymbolMask));
5716 __ j(zero, &maybe_in_dictionary);
5717 }
5718 }
5719
5720 __ bind(&maybe_in_dictionary);
5721 // If we are doing negative lookup then probing failure should be
5722 // treated as a lookup success. For positive lookup probing failure
5723 // should be treated as lookup failure.
5724 if (mode_ == POSITIVE_LOOKUP) {
5725 __ movq(scratch, Immediate(0));
5726 __ Drop(1);
5727 __ ret(2 * kPointerSize);
5728 }
5729
5730 __ bind(&in_dictionary);
5731 __ movq(scratch, Immediate(1));
5732 __ Drop(1);
5733 __ ret(2 * kPointerSize);
5734
5735 __ bind(&not_in_dictionary);
5736 __ movq(scratch, Immediate(0));
5737 __ Drop(1);
5738 __ ret(2 * kPointerSize);
5739}
5740
5741
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005742struct AheadOfTimeWriteBarrierStubList {
5743 Register object, value, address;
5744 RememberedSetAction action;
5745};
5746
5747
5748struct AheadOfTimeWriteBarrierStubList kAheadOfTime[] = {
5749 // Used in RegExpExecStub.
5750 { rbx, rax, rdi, EMIT_REMEMBERED_SET },
5751 // Used in CompileArrayPushCall.
5752 { rbx, rcx, rdx, EMIT_REMEMBERED_SET },
5753 // Used in CompileStoreGlobal.
5754 { rbx, rcx, rdx, OMIT_REMEMBERED_SET },
5755 // Used in StoreStubCompiler::CompileStoreField and
5756 // KeyedStoreStubCompiler::CompileStoreField via GenerateStoreField.
5757 { rdx, rcx, rbx, EMIT_REMEMBERED_SET },
5758 // GenerateStoreField calls the stub with two different permutations of
5759 // registers. This is the second.
5760 { rbx, rcx, rdx, EMIT_REMEMBERED_SET },
5761 // StoreIC::GenerateNormal via GenerateDictionaryStore.
5762 { rbx, r8, r9, EMIT_REMEMBERED_SET },
5763 // KeyedStoreIC::GenerateGeneric.
5764 { rbx, rdx, rcx, EMIT_REMEMBERED_SET},
5765 // KeyedStoreStubCompiler::GenerateStoreFastElement.
5766 { rdi, rdx, rcx, EMIT_REMEMBERED_SET},
5767 // ElementsTransitionGenerator::GenerateSmiOnlyToObject
5768 // and ElementsTransitionGenerator::GenerateSmiOnlyToObject
5769 // and ElementsTransitionGenerator::GenerateDoubleToObject
5770 { rdx, rbx, rdi, EMIT_REMEMBERED_SET},
5771 // ElementsTransitionGenerator::GenerateSmiOnlyToDouble
5772 // and ElementsTransitionGenerator::GenerateDoubleToObject
5773 { rdx, r11, r15, EMIT_REMEMBERED_SET},
5774 // ElementsTransitionGenerator::GenerateDoubleToObject
5775 { r11, rax, r15, EMIT_REMEMBERED_SET},
5776 // StoreArrayLiteralElementStub::Generate
5777 { rbx, rax, rcx, EMIT_REMEMBERED_SET},
5778 // Null termination.
5779 { no_reg, no_reg, no_reg, EMIT_REMEMBERED_SET}
5780};
5781
5782
5783bool RecordWriteStub::IsPregenerated() {
5784 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
5785 !entry->object.is(no_reg);
5786 entry++) {
5787 if (object_.is(entry->object) &&
5788 value_.is(entry->value) &&
5789 address_.is(entry->address) &&
5790 remembered_set_action_ == entry->action &&
5791 save_fp_regs_mode_ == kDontSaveFPRegs) {
5792 return true;
5793 }
5794 }
5795 return false;
5796}
5797
5798
5799void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime() {
5800 StoreBufferOverflowStub stub1(kDontSaveFPRegs);
5801 stub1.GetCode()->set_is_pregenerated(true);
5802 StoreBufferOverflowStub stub2(kSaveFPRegs);
5803 stub2.GetCode()->set_is_pregenerated(true);
5804}
5805
5806
5807void RecordWriteStub::GenerateFixedRegStubsAheadOfTime() {
5808 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
5809 !entry->object.is(no_reg);
5810 entry++) {
5811 RecordWriteStub stub(entry->object,
5812 entry->value,
5813 entry->address,
5814 entry->action,
5815 kDontSaveFPRegs);
5816 stub.GetCode()->set_is_pregenerated(true);
5817 }
5818}
5819
5820
5821// Takes the input in 3 registers: address_ value_ and object_. A pointer to
5822// the value has just been written into the object, now this stub makes sure
5823// we keep the GC informed. The word in the object where the value has been
5824// written is in the address register.
5825void RecordWriteStub::Generate(MacroAssembler* masm) {
5826 Label skip_to_incremental_noncompacting;
5827 Label skip_to_incremental_compacting;
5828
5829 // The first two instructions are generated with labels so as to get the
5830 // offset fixed up correctly by the bind(Label*) call. We patch it back and
5831 // forth between a compare instructions (a nop in this position) and the
5832 // real branch when we start and stop incremental heap marking.
5833 // See RecordWriteStub::Patch for details.
5834 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
5835 __ jmp(&skip_to_incremental_compacting, Label::kFar);
5836
5837 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
5838 __ RememberedSetHelper(object_,
5839 address_,
5840 value_,
5841 save_fp_regs_mode_,
5842 MacroAssembler::kReturnAtEnd);
5843 } else {
5844 __ ret(0);
5845 }
5846
5847 __ bind(&skip_to_incremental_noncompacting);
5848 GenerateIncremental(masm, INCREMENTAL);
5849
5850 __ bind(&skip_to_incremental_compacting);
5851 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
5852
5853 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
5854 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
5855 masm->set_byte_at(0, kTwoByteNopInstruction);
5856 masm->set_byte_at(2, kFiveByteNopInstruction);
5857}
5858
5859
5860void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
5861 regs_.Save(masm);
5862
5863 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
5864 Label dont_need_remembered_set;
5865
5866 __ movq(regs_.scratch0(), Operand(regs_.address(), 0));
5867 __ JumpIfNotInNewSpace(regs_.scratch0(),
5868 regs_.scratch0(),
5869 &dont_need_remembered_set);
5870
5871 __ CheckPageFlag(regs_.object(),
5872 regs_.scratch0(),
5873 1 << MemoryChunk::SCAN_ON_SCAVENGE,
5874 not_zero,
5875 &dont_need_remembered_set);
5876
5877 // First notify the incremental marker if necessary, then update the
5878 // remembered set.
5879 CheckNeedsToInformIncrementalMarker(
5880 masm, kUpdateRememberedSetOnNoNeedToInformIncrementalMarker, mode);
5881 InformIncrementalMarker(masm, mode);
5882 regs_.Restore(masm);
5883 __ RememberedSetHelper(object_,
5884 address_,
5885 value_,
5886 save_fp_regs_mode_,
5887 MacroAssembler::kReturnAtEnd);
5888
5889 __ bind(&dont_need_remembered_set);
5890 }
5891
5892 CheckNeedsToInformIncrementalMarker(
5893 masm, kReturnOnNoNeedToInformIncrementalMarker, mode);
5894 InformIncrementalMarker(masm, mode);
5895 regs_.Restore(masm);
5896 __ ret(0);
5897}
5898
5899
5900void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm, Mode mode) {
5901 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode_);
5902#ifdef _WIN64
5903 Register arg3 = r8;
5904 Register arg2 = rdx;
5905 Register arg1 = rcx;
5906#else
5907 Register arg3 = rdx;
5908 Register arg2 = rsi;
5909 Register arg1 = rdi;
5910#endif
5911 Register address =
5912 arg1.is(regs_.address()) ? kScratchRegister : regs_.address();
5913 ASSERT(!address.is(regs_.object()));
5914 ASSERT(!address.is(arg1));
5915 __ Move(address, regs_.address());
5916 __ Move(arg1, regs_.object());
5917 if (mode == INCREMENTAL_COMPACTION) {
5918 // TODO(gc) Can we just set address arg2 in the beginning?
5919 __ Move(arg2, address);
5920 } else {
5921 ASSERT(mode == INCREMENTAL);
5922 __ movq(arg2, Operand(address, 0));
5923 }
5924 __ LoadAddress(arg3, ExternalReference::isolate_address());
5925 int argument_count = 3;
5926
5927 AllowExternalCallThatCantCauseGC scope(masm);
5928 __ PrepareCallCFunction(argument_count);
5929 if (mode == INCREMENTAL_COMPACTION) {
5930 __ CallCFunction(
5931 ExternalReference::incremental_evacuation_record_write_function(
5932 masm->isolate()),
5933 argument_count);
5934 } else {
5935 ASSERT(mode == INCREMENTAL);
5936 __ CallCFunction(
5937 ExternalReference::incremental_marking_record_write_function(
5938 masm->isolate()),
5939 argument_count);
5940 }
5941 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode_);
5942}
5943
5944
5945void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
5946 MacroAssembler* masm,
5947 OnNoNeedToInformIncrementalMarker on_no_need,
5948 Mode mode) {
5949 Label on_black;
5950 Label need_incremental;
5951 Label need_incremental_pop_object;
5952
5953 // Let's look at the color of the object: If it is not black we don't have
5954 // to inform the incremental marker.
5955 __ JumpIfBlack(regs_.object(),
5956 regs_.scratch0(),
5957 regs_.scratch1(),
5958 &on_black,
5959 Label::kNear);
5960
5961 regs_.Restore(masm);
5962 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
5963 __ RememberedSetHelper(object_,
5964 address_,
5965 value_,
5966 save_fp_regs_mode_,
5967 MacroAssembler::kReturnAtEnd);
5968 } else {
5969 __ ret(0);
5970 }
5971
5972 __ bind(&on_black);
5973
5974 // Get the value from the slot.
5975 __ movq(regs_.scratch0(), Operand(regs_.address(), 0));
5976
5977 if (mode == INCREMENTAL_COMPACTION) {
5978 Label ensure_not_white;
5979
5980 __ CheckPageFlag(regs_.scratch0(), // Contains value.
5981 regs_.scratch1(), // Scratch.
5982 MemoryChunk::kEvacuationCandidateMask,
5983 zero,
5984 &ensure_not_white,
5985 Label::kNear);
5986
5987 __ CheckPageFlag(regs_.object(),
5988 regs_.scratch1(), // Scratch.
5989 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
5990 zero,
5991 &need_incremental);
5992
5993 __ bind(&ensure_not_white);
5994 }
5995
5996 // We need an extra register for this, so we push the object register
5997 // temporarily.
5998 __ push(regs_.object());
5999 __ EnsureNotWhite(regs_.scratch0(), // The value.
6000 regs_.scratch1(), // Scratch.
6001 regs_.object(), // Scratch.
6002 &need_incremental_pop_object,
6003 Label::kNear);
6004 __ pop(regs_.object());
6005
6006 regs_.Restore(masm);
6007 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
6008 __ RememberedSetHelper(object_,
6009 address_,
6010 value_,
6011 save_fp_regs_mode_,
6012 MacroAssembler::kReturnAtEnd);
6013 } else {
6014 __ ret(0);
6015 }
6016
6017 __ bind(&need_incremental_pop_object);
6018 __ pop(regs_.object());
6019
6020 __ bind(&need_incremental);
6021
6022 // Fall through when we need to inform the incremental marker.
6023}
6024
6025
6026void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
6027 // ----------- S t a t e -------------
6028 // -- rax : element value to store
6029 // -- rbx : array literal
6030 // -- rdi : map of array literal
6031 // -- rcx : element index as smi
6032 // -- rdx : array literal index in function
6033 // -- rsp[0] : return address
6034 // -----------------------------------
6035
6036 Label element_done;
6037 Label double_elements;
6038 Label smi_element;
6039 Label slow_elements;
6040 Label fast_elements;
6041
6042 __ CheckFastElements(rdi, &double_elements);
6043
6044 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
6045 __ JumpIfSmi(rax, &smi_element);
6046 __ CheckFastSmiOnlyElements(rdi, &fast_elements);
6047
6048 // Store into the array literal requires a elements transition. Call into
6049 // the runtime.
6050
6051 __ bind(&slow_elements);
6052 __ pop(rdi); // Pop return address and remember to put back later for tail
6053 // call.
6054 __ push(rbx);
6055 __ push(rcx);
6056 __ push(rax);
6057 __ movq(rbx, Operand(rbp, JavaScriptFrameConstants::kFunctionOffset));
6058 __ push(FieldOperand(rbx, JSFunction::kLiteralsOffset));
6059 __ push(rdx);
6060 __ push(rdi); // Return return address so that tail call returns to right
6061 // place.
6062 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
6063
6064 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
6065 __ bind(&fast_elements);
6066 __ SmiToInteger32(kScratchRegister, rcx);
6067 __ movq(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
6068 __ lea(rcx, FieldOperand(rbx, kScratchRegister, times_pointer_size,
6069 FixedArrayBase::kHeaderSize));
6070 __ movq(Operand(rcx, 0), rax);
6071 // Update the write barrier for the array store.
6072 __ RecordWrite(rbx, rcx, rax,
6073 kDontSaveFPRegs,
6074 EMIT_REMEMBERED_SET,
6075 OMIT_SMI_CHECK);
6076 __ ret(0);
6077
6078 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
6079 // FAST_ELEMENTS, and value is Smi.
6080 __ bind(&smi_element);
6081 __ SmiToInteger32(kScratchRegister, rcx);
6082 __ movq(rbx, FieldOperand(rbx, JSObject::kElementsOffset));
6083 __ movq(FieldOperand(rbx, kScratchRegister, times_pointer_size,
6084 FixedArrayBase::kHeaderSize), rax);
6085 __ ret(0);
6086
6087 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
6088 __ bind(&double_elements);
6089
6090 __ movq(r9, FieldOperand(rbx, JSObject::kElementsOffset));
6091 __ SmiToInteger32(r11, rcx);
6092 __ StoreNumberToDoubleElements(rax,
6093 r9,
6094 r11,
6095 xmm0,
6096 &slow_elements);
6097 __ ret(0);
6098}
6099
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006100#undef __
6101
6102} } // namespace v8::internal
6103
6104#endif // V8_TARGET_ARCH_X64