blob: eded335e7dd13402c961ef841f03b6fff18adfa5 [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_IA32)
31
Kristian Monsen80d68ea2010-09-08 11:05:35 +010032#include "bootstrapper.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000033#include "code-stubs.h"
Steve Block44f0eee2011-05-26 01:26:41 +010034#include "isolate.h"
Ben Murdoch257744e2011-11-30 15:57:28 +000035#include "jsregexp.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010036#include "regexp-macro-assembler.h"
Ben Murdoch592a9fc2012-03-05 11:04:45 +000037#include "stub-cache.h"
38#include "codegen.h"
Kristian Monsen80d68ea2010-09-08 11:05:35 +010039
40namespace v8 {
41namespace internal {
42
43#define __ ACCESS_MASM(masm)
Steve Block1e0659c2011-05-24 12:43:12 +010044
45void ToNumberStub::Generate(MacroAssembler* masm) {
46 // The ToNumber stub takes one argument in eax.
Ben Murdoch257744e2011-11-30 15:57:28 +000047 Label check_heap_number, call_builtin;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +000048 __ JumpIfNotSmi(eax, &check_heap_number, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +010049 __ ret(0);
50
51 __ bind(&check_heap_number);
52 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +010053 Factory* factory = masm->isolate()->factory();
Ben Murdoch592a9fc2012-03-05 11:04:45 +000054 __ cmp(ebx, Immediate(factory->heap_number_map()));
Ben Murdoch257744e2011-11-30 15:57:28 +000055 __ j(not_equal, &call_builtin, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +010056 __ ret(0);
57
58 __ bind(&call_builtin);
59 __ pop(ecx); // Pop return address.
60 __ push(eax);
61 __ push(ecx); // Push return address.
62 __ InvokeBuiltin(Builtins::TO_NUMBER, JUMP_FUNCTION);
63}
64
65
Kristian Monsen80d68ea2010-09-08 11:05:35 +010066void FastNewClosureStub::Generate(MacroAssembler* masm) {
67 // Create a new closure from the given function info in new
68 // space. Set the context to the current context in esi.
69 Label gc;
70 __ AllocateInNewSpace(JSFunction::kSize, eax, ebx, ecx, &gc, TAG_OBJECT);
71
72 // Get the function info from the stack.
73 __ mov(edx, Operand(esp, 1 * kPointerSize));
74
Ben Murdoch592a9fc2012-03-05 11:04:45 +000075 int map_index = (language_mode_ == CLASSIC_MODE)
76 ? Context::FUNCTION_MAP_INDEX
77 : Context::STRICT_MODE_FUNCTION_MAP_INDEX;
Steve Block44f0eee2011-05-26 01:26:41 +010078
Kristian Monsen80d68ea2010-09-08 11:05:35 +010079 // Compute the function map in the current global context and set that
80 // as the map of the allocated object.
81 __ mov(ecx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
82 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalContextOffset));
Steve Block44f0eee2011-05-26 01:26:41 +010083 __ mov(ecx, Operand(ecx, Context::SlotOffset(map_index)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +010084 __ mov(FieldOperand(eax, JSObject::kMapOffset), ecx);
85
86 // Initialize the rest of the function. We don't have to update the
87 // write barrier because the allocated object is in new space.
Steve Block44f0eee2011-05-26 01:26:41 +010088 Factory* factory = masm->isolate()->factory();
89 __ mov(ebx, Immediate(factory->empty_fixed_array()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +010090 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ebx);
91 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
92 __ mov(FieldOperand(eax, JSFunction::kPrototypeOrInitialMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +010093 Immediate(factory->the_hole_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +010094 __ mov(FieldOperand(eax, JSFunction::kSharedFunctionInfoOffset), edx);
95 __ mov(FieldOperand(eax, JSFunction::kContextOffset), esi);
96 __ mov(FieldOperand(eax, JSFunction::kLiteralsOffset), ebx);
Ben Murdochb0fe1622011-05-05 13:52:32 +010097 __ mov(FieldOperand(eax, JSFunction::kNextFunctionLinkOffset),
Steve Block44f0eee2011-05-26 01:26:41 +010098 Immediate(factory->undefined_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +010099
100 // Initialize the code pointer in the function to be the one
101 // found in the shared function info object.
102 __ mov(edx, FieldOperand(edx, SharedFunctionInfo::kCodeOffset));
103 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
104 __ mov(FieldOperand(eax, JSFunction::kCodeEntryOffset), edx);
105
106 // Return and remove the on-stack parameter.
107 __ ret(1 * kPointerSize);
108
109 // Create a new closure through the slower runtime call.
110 __ bind(&gc);
111 __ pop(ecx); // Temporarily remove return address.
112 __ pop(edx);
113 __ push(esi);
114 __ push(edx);
Steve Block44f0eee2011-05-26 01:26:41 +0100115 __ push(Immediate(factory->false_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100116 __ push(ecx); // Restore return address.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800117 __ TailCallRuntime(Runtime::kNewClosure, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100118}
119
120
121void FastNewContextStub::Generate(MacroAssembler* masm) {
122 // Try to allocate the context in new space.
123 Label gc;
124 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
125 __ AllocateInNewSpace((length * kPointerSize) + FixedArray::kHeaderSize,
126 eax, ebx, ecx, &gc, TAG_OBJECT);
127
128 // Get the function from the stack.
129 __ mov(ecx, Operand(esp, 1 * kPointerSize));
130
Ben Murdochc7cc0282012-03-05 14:35:55 +0000131 // Set up the object header.
Steve Block44f0eee2011-05-26 01:26:41 +0100132 Factory* factory = masm->isolate()->factory();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000133 __ mov(FieldOperand(eax, HeapObject::kMapOffset),
134 factory->function_context_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100135 __ mov(FieldOperand(eax, Context::kLengthOffset),
136 Immediate(Smi::FromInt(length)));
137
Ben Murdochc7cc0282012-03-05 14:35:55 +0000138 // Set up the fixed slots.
Steve Block9fac8402011-05-12 15:51:54 +0100139 __ Set(ebx, Immediate(0)); // Set to NULL.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100140 __ mov(Operand(eax, Context::SlotOffset(Context::CLOSURE_INDEX)), ecx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000141 __ mov(Operand(eax, Context::SlotOffset(Context::PREVIOUS_INDEX)), esi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100142 __ mov(Operand(eax, Context::SlotOffset(Context::EXTENSION_INDEX)), ebx);
143
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000144 // Copy the global object from the previous context.
145 __ mov(ebx, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100146 __ mov(Operand(eax, Context::SlotOffset(Context::GLOBAL_INDEX)), ebx);
147
148 // Initialize the rest of the slots to undefined.
Steve Block44f0eee2011-05-26 01:26:41 +0100149 __ mov(ebx, factory->undefined_value());
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100150 for (int i = Context::MIN_CONTEXT_SLOTS; i < length; i++) {
151 __ mov(Operand(eax, Context::SlotOffset(i)), ebx);
152 }
153
154 // Return and remove the on-stack parameter.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000155 __ mov(esi, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100156 __ ret(1 * kPointerSize);
157
158 // Need to collect. Call into runtime system.
159 __ bind(&gc);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000160 __ TailCallRuntime(Runtime::kNewFunctionContext, 1, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100161}
162
163
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000164void FastNewBlockContextStub::Generate(MacroAssembler* masm) {
165 // Stack layout on entry:
166 //
167 // [esp + (1 * kPointerSize)]: function
168 // [esp + (2 * kPointerSize)]: serialized scope info
169
170 // Try to allocate the context in new space.
171 Label gc;
172 int length = slots_ + Context::MIN_CONTEXT_SLOTS;
173 __ AllocateInNewSpace(FixedArray::SizeFor(length),
174 eax, ebx, ecx, &gc, TAG_OBJECT);
175
176 // Get the function or sentinel from the stack.
177 __ mov(ecx, Operand(esp, 1 * kPointerSize));
178
179 // Get the serialized scope info from the stack.
180 __ mov(ebx, Operand(esp, 2 * kPointerSize));
181
Ben Murdochc7cc0282012-03-05 14:35:55 +0000182 // Set up the object header.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000183 Factory* factory = masm->isolate()->factory();
184 __ mov(FieldOperand(eax, HeapObject::kMapOffset),
185 factory->block_context_map());
186 __ mov(FieldOperand(eax, Context::kLengthOffset),
187 Immediate(Smi::FromInt(length)));
188
189 // If this block context is nested in the global context we get a smi
190 // sentinel instead of a function. The block context should get the
191 // canonical empty function of the global context as its closure which
192 // we still have to look up.
193 Label after_sentinel;
194 __ JumpIfNotSmi(ecx, &after_sentinel, Label::kNear);
195 if (FLAG_debug_code) {
196 const char* message = "Expected 0 as a Smi sentinel";
197 __ cmp(ecx, 0);
198 __ Assert(equal, message);
199 }
200 __ mov(ecx, GlobalObjectOperand());
201 __ mov(ecx, FieldOperand(ecx, GlobalObject::kGlobalContextOffset));
202 __ mov(ecx, ContextOperand(ecx, Context::CLOSURE_INDEX));
203 __ bind(&after_sentinel);
204
Ben Murdochc7cc0282012-03-05 14:35:55 +0000205 // Set up the fixed slots.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000206 __ mov(ContextOperand(eax, Context::CLOSURE_INDEX), ecx);
207 __ mov(ContextOperand(eax, Context::PREVIOUS_INDEX), esi);
208 __ mov(ContextOperand(eax, Context::EXTENSION_INDEX), ebx);
209
210 // Copy the global object from the previous context.
211 __ mov(ebx, ContextOperand(esi, Context::GLOBAL_INDEX));
212 __ mov(ContextOperand(eax, Context::GLOBAL_INDEX), ebx);
213
214 // Initialize the rest of the slots to the hole value.
215 if (slots_ == 1) {
216 __ mov(ContextOperand(eax, Context::MIN_CONTEXT_SLOTS),
217 factory->the_hole_value());
218 } else {
219 __ mov(ebx, factory->the_hole_value());
220 for (int i = 0; i < slots_; i++) {
221 __ mov(ContextOperand(eax, i + Context::MIN_CONTEXT_SLOTS), ebx);
222 }
223 }
224
225 // Return and remove the on-stack parameters.
226 __ mov(esi, eax);
227 __ ret(2 * kPointerSize);
228
229 // Need to collect. Call into runtime system.
230 __ bind(&gc);
231 __ TailCallRuntime(Runtime::kPushBlockContext, 2, 1);
232}
233
234
235static void GenerateFastCloneShallowArrayCommon(
236 MacroAssembler* masm,
237 int length,
238 FastCloneShallowArrayStub::Mode mode,
239 Label* fail) {
240 // Registers on entry:
241 //
242 // ecx: boilerplate literal array.
243 ASSERT(mode != FastCloneShallowArrayStub::CLONE_ANY_ELEMENTS);
244
245 // All sizes here are multiples of kPointerSize.
246 int elements_size = 0;
247 if (length > 0) {
248 elements_size = mode == FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS
249 ? FixedDoubleArray::SizeFor(length)
250 : FixedArray::SizeFor(length);
251 }
252 int size = JSArray::kSize + elements_size;
253
254 // Allocate both the JS array and the elements array in one big
255 // allocation. This avoids multiple limit checks.
256 __ AllocateInNewSpace(size, eax, ebx, edx, fail, TAG_OBJECT);
257
258 // Copy the JS array part.
259 for (int i = 0; i < JSArray::kSize; i += kPointerSize) {
260 if ((i != JSArray::kElementsOffset) || (length == 0)) {
261 __ mov(ebx, FieldOperand(ecx, i));
262 __ mov(FieldOperand(eax, i), ebx);
263 }
264 }
265
266 if (length > 0) {
267 // Get hold of the elements array of the boilerplate and setup the
268 // elements pointer in the resulting object.
269 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
270 __ lea(edx, Operand(eax, JSArray::kSize));
271 __ mov(FieldOperand(eax, JSArray::kElementsOffset), edx);
272
273 // Copy the elements array.
274 if (mode == FastCloneShallowArrayStub::CLONE_ELEMENTS) {
275 for (int i = 0; i < elements_size; i += kPointerSize) {
276 __ mov(ebx, FieldOperand(ecx, i));
277 __ mov(FieldOperand(edx, i), ebx);
278 }
279 } else {
280 ASSERT(mode == FastCloneShallowArrayStub::CLONE_DOUBLE_ELEMENTS);
281 int i;
282 for (i = 0; i < FixedDoubleArray::kHeaderSize; i += kPointerSize) {
283 __ mov(ebx, FieldOperand(ecx, i));
284 __ mov(FieldOperand(edx, i), ebx);
285 }
286 while (i < elements_size) {
287 __ fld_d(FieldOperand(ecx, i));
288 __ fstp_d(FieldOperand(edx, i));
289 i += kDoubleSize;
290 }
291 ASSERT(i == elements_size);
292 }
293 }
294}
295
296
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100297void FastCloneShallowArrayStub::Generate(MacroAssembler* masm) {
298 // Stack layout on entry:
299 //
300 // [esp + kPointerSize]: constant elements.
301 // [esp + (2 * kPointerSize)]: literal index.
302 // [esp + (3 * kPointerSize)]: literals array.
303
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100304 // Load boilerplate object into ecx and check if we need to create a
305 // boilerplate.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100306 __ mov(ecx, Operand(esp, 3 * kPointerSize));
307 __ mov(eax, Operand(esp, 2 * kPointerSize));
308 STATIC_ASSERT(kPointerSize == 4);
309 STATIC_ASSERT(kSmiTagSize == 1);
310 STATIC_ASSERT(kSmiTag == 0);
311 __ mov(ecx, FieldOperand(ecx, eax, times_half_pointer_size,
312 FixedArray::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +0100313 Factory* factory = masm->isolate()->factory();
314 __ cmp(ecx, factory->undefined_value());
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000315 Label slow_case;
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100316 __ j(equal, &slow_case);
317
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000318 FastCloneShallowArrayStub::Mode mode = mode_;
319 // ecx is boilerplate object.
320 if (mode == CLONE_ANY_ELEMENTS) {
321 Label double_elements, check_fast_elements;
322 __ mov(ebx, FieldOperand(ecx, JSArray::kElementsOffset));
323 __ CheckMap(ebx, factory->fixed_cow_array_map(),
324 &check_fast_elements, DONT_DO_SMI_CHECK);
325 GenerateFastCloneShallowArrayCommon(masm, 0,
326 COPY_ON_WRITE_ELEMENTS, &slow_case);
327 __ ret(3 * kPointerSize);
328
329 __ bind(&check_fast_elements);
330 __ CheckMap(ebx, factory->fixed_array_map(),
331 &double_elements, DONT_DO_SMI_CHECK);
332 GenerateFastCloneShallowArrayCommon(masm, length_,
333 CLONE_ELEMENTS, &slow_case);
334 __ ret(3 * kPointerSize);
335
336 __ bind(&double_elements);
337 mode = CLONE_DOUBLE_ELEMENTS;
338 // Fall through to generate the code to handle double elements.
339 }
340
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100341 if (FLAG_debug_code) {
342 const char* message;
343 Handle<Map> expected_map;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000344 if (mode == CLONE_ELEMENTS) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100345 message = "Expected (writable) fixed array";
Steve Block44f0eee2011-05-26 01:26:41 +0100346 expected_map = factory->fixed_array_map();
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000347 } else if (mode == CLONE_DOUBLE_ELEMENTS) {
348 message = "Expected (writable) fixed double array";
349 expected_map = factory->fixed_double_array_map();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100350 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000351 ASSERT(mode == COPY_ON_WRITE_ELEMENTS);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100352 message = "Expected copy-on-write fixed array";
Steve Block44f0eee2011-05-26 01:26:41 +0100353 expected_map = factory->fixed_cow_array_map();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100354 }
355 __ push(ecx);
356 __ mov(ecx, FieldOperand(ecx, JSArray::kElementsOffset));
357 __ cmp(FieldOperand(ecx, HeapObject::kMapOffset), expected_map);
358 __ Assert(equal, message);
359 __ pop(ecx);
360 }
361
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000362 GenerateFastCloneShallowArrayCommon(masm, length_, mode, &slow_case);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100363 // Return and remove the on-stack parameters.
364 __ ret(3 * kPointerSize);
365
366 __ bind(&slow_case);
367 __ TailCallRuntime(Runtime::kCreateArrayLiteralShallow, 3, 1);
368}
369
370
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000371void FastCloneShallowObjectStub::Generate(MacroAssembler* masm) {
372 // Stack layout on entry:
373 //
374 // [esp + kPointerSize]: object literal flags.
375 // [esp + (2 * kPointerSize)]: constant properties.
376 // [esp + (3 * kPointerSize)]: literal index.
377 // [esp + (4 * kPointerSize)]: literals array.
378
379 // Load boilerplate object into ecx and check if we need to create a
380 // boilerplate.
381 Label slow_case;
382 __ mov(ecx, Operand(esp, 4 * kPointerSize));
383 __ mov(eax, Operand(esp, 3 * kPointerSize));
384 STATIC_ASSERT(kPointerSize == 4);
385 STATIC_ASSERT(kSmiTagSize == 1);
386 STATIC_ASSERT(kSmiTag == 0);
387 __ mov(ecx, FieldOperand(ecx, eax, times_half_pointer_size,
388 FixedArray::kHeaderSize));
389 Factory* factory = masm->isolate()->factory();
390 __ cmp(ecx, factory->undefined_value());
391 __ j(equal, &slow_case);
392
393 // Check that the boilerplate contains only fast properties and we can
394 // statically determine the instance size.
395 int size = JSObject::kHeaderSize + length_ * kPointerSize;
396 __ mov(eax, FieldOperand(ecx, HeapObject::kMapOffset));
397 __ movzx_b(eax, FieldOperand(eax, Map::kInstanceSizeOffset));
398 __ cmp(eax, Immediate(size >> kPointerSizeLog2));
399 __ j(not_equal, &slow_case);
400
401 // Allocate the JS object and copy header together with all in-object
402 // properties from the boilerplate.
403 __ AllocateInNewSpace(size, eax, ebx, edx, &slow_case, TAG_OBJECT);
404 for (int i = 0; i < size; i += kPointerSize) {
405 __ mov(ebx, FieldOperand(ecx, i));
406 __ mov(FieldOperand(eax, i), ebx);
407 }
408
409 // Return and remove the on-stack parameters.
410 __ ret(4 * kPointerSize);
411
412 __ bind(&slow_case);
413 __ TailCallRuntime(Runtime::kCreateObjectLiteralShallow, 4, 1);
414}
415
416
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000417// The stub expects its argument on the stack and returns its result in tos_:
418// zero for false, and a non-zero value for true.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100419void ToBooleanStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000420 // This stub overrides SometimesSetsUpAFrame() to return false. That means
421 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000422 Label patch;
Ben Murdoch257744e2011-11-30 15:57:28 +0000423 Factory* factory = masm->isolate()->factory();
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000424 const Register argument = eax;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000425 const Register map = edx;
426
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000427 if (!types_.IsEmpty()) {
428 __ mov(argument, Operand(esp, 1 * kPointerSize));
429 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000430
431 // undefined -> false
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000432 CheckOddball(masm, UNDEFINED, Heap::kUndefinedValueRootIndex, false);
Ben Murdoch257744e2011-11-30 15:57:28 +0000433
434 // Boolean -> its value
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000435 CheckOddball(masm, BOOLEAN, Heap::kFalseValueRootIndex, false);
436 CheckOddball(masm, BOOLEAN, Heap::kTrueValueRootIndex, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100437
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000438 // 'null' -> false.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000439 CheckOddball(masm, NULL_TYPE, Heap::kNullValueRootIndex, false);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100440
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000441 if (types_.Contains(SMI)) {
442 // Smis: 0 -> false, all other -> true
443 Label not_smi;
444 __ JumpIfNotSmi(argument, &not_smi, Label::kNear);
445 // argument contains the correct return value already.
446 if (!tos_.is(argument)) {
447 __ mov(tos_, argument);
448 }
449 __ ret(1 * kPointerSize);
450 __ bind(&not_smi);
451 } else if (types_.NeedsMap()) {
452 // If we need a map later and have a Smi -> patch.
453 __ JumpIfSmi(argument, &patch, Label::kNear);
454 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100455
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000456 if (types_.NeedsMap()) {
457 __ mov(map, FieldOperand(argument, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100458
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000459 if (types_.CanBeUndetectable()) {
460 __ test_b(FieldOperand(map, Map::kBitFieldOffset),
461 1 << Map::kIsUndetectable);
462 // Undetectable -> false.
463 Label not_undetectable;
464 __ j(zero, &not_undetectable, Label::kNear);
465 __ Set(tos_, Immediate(0));
466 __ ret(1 * kPointerSize);
467 __ bind(&not_undetectable);
468 }
469 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100470
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000471 if (types_.Contains(SPEC_OBJECT)) {
472 // spec object -> true.
473 Label not_js_object;
474 __ CmpInstanceType(map, FIRST_SPEC_OBJECT_TYPE);
475 __ j(below, &not_js_object, Label::kNear);
476 // argument contains the correct return value already.
477 if (!tos_.is(argument)) {
478 __ Set(tos_, Immediate(1));
479 }
480 __ ret(1 * kPointerSize);
481 __ bind(&not_js_object);
482 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100483
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000484 if (types_.Contains(STRING)) {
485 // String value -> false iff empty.
486 Label not_string;
487 __ CmpInstanceType(map, FIRST_NONSTRING_TYPE);
488 __ j(above_equal, &not_string, Label::kNear);
489 __ mov(tos_, FieldOperand(argument, String::kLengthOffset));
490 __ ret(1 * kPointerSize); // the string length is OK as the return value
491 __ bind(&not_string);
492 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100493
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000494 if (types_.Contains(HEAP_NUMBER)) {
495 // heap number -> false iff +0, -0, or NaN.
496 Label not_heap_number, false_result;
497 __ cmp(map, factory->heap_number_map());
498 __ j(not_equal, &not_heap_number, Label::kNear);
499 __ fldz();
500 __ fld_d(FieldOperand(argument, HeapNumber::kValueOffset));
501 __ FCmp();
502 __ j(zero, &false_result, Label::kNear);
503 // argument contains the correct return value already.
504 if (!tos_.is(argument)) {
505 __ Set(tos_, Immediate(1));
506 }
507 __ ret(1 * kPointerSize);
508 __ bind(&false_result);
509 __ Set(tos_, Immediate(0));
510 __ ret(1 * kPointerSize);
511 __ bind(&not_heap_number);
512 }
513
514 __ bind(&patch);
515 GenerateTypeTransition(masm);
516}
517
518
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000519void StoreBufferOverflowStub::Generate(MacroAssembler* masm) {
520 // We don't allow a GC during a store buffer overflow so there is no need to
521 // store the registers in any particular way, but we do have to store and
522 // restore them.
523 __ pushad();
524 if (save_doubles_ == kSaveFPRegs) {
525 CpuFeatures::Scope scope(SSE2);
526 __ sub(esp, Immediate(kDoubleSize * XMMRegister::kNumRegisters));
527 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
528 XMMRegister reg = XMMRegister::from_code(i);
529 __ movdbl(Operand(esp, i * kDoubleSize), reg);
530 }
531 }
532 const int argument_count = 1;
533
534 AllowExternalCallThatCantCauseGC scope(masm);
535 __ PrepareCallCFunction(argument_count, ecx);
536 __ mov(Operand(esp, 0 * kPointerSize),
537 Immediate(ExternalReference::isolate_address()));
538 __ CallCFunction(
539 ExternalReference::store_buffer_overflow_function(masm->isolate()),
540 argument_count);
541 if (save_doubles_ == kSaveFPRegs) {
542 CpuFeatures::Scope scope(SSE2);
543 for (int i = 0; i < XMMRegister::kNumRegisters; i++) {
544 XMMRegister reg = XMMRegister::from_code(i);
545 __ movdbl(reg, Operand(esp, i * kDoubleSize));
546 }
547 __ add(esp, Immediate(kDoubleSize * XMMRegister::kNumRegisters));
548 }
549 __ popad();
550 __ ret(0);
551}
552
553
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000554void ToBooleanStub::CheckOddball(MacroAssembler* masm,
555 Type type,
556 Heap::RootListIndex value,
557 bool result) {
558 const Register argument = eax;
559 if (types_.Contains(type)) {
560 // If we see an expected oddball, return its ToBoolean value tos_.
561 Label different_value;
562 __ CompareRoot(argument, value);
563 __ j(not_equal, &different_value, Label::kNear);
564 if (!result) {
565 // If we have to return zero, there is no way around clearing tos_.
566 __ Set(tos_, Immediate(0));
567 } else if (!tos_.is(argument)) {
568 // If we have to return non-zero, we can re-use the argument if it is the
569 // same register as the result, because we never see Smi-zero here.
570 __ Set(tos_, Immediate(1));
571 }
572 __ ret(1 * kPointerSize);
573 __ bind(&different_value);
574 }
575}
576
577
578void ToBooleanStub::GenerateTypeTransition(MacroAssembler* masm) {
579 __ pop(ecx); // Get return address, operand is now on top of stack.
580 __ push(Immediate(Smi::FromInt(tos_.code())));
581 __ push(Immediate(Smi::FromInt(types_.ToByte())));
582 __ push(ecx); // Push return address.
583 // Patch the caller to an appropriate specialized stub and return the
584 // operation result to the caller of the stub.
585 __ TailCallExternalReference(
586 ExternalReference(IC_Utility(IC::kToBoolean_Patch), masm->isolate()),
587 3,
588 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100589}
590
591
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100592class FloatingPointHelper : public AllStatic {
593 public:
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100594 enum ArgLocation {
595 ARGS_ON_STACK,
596 ARGS_IN_REGISTERS
597 };
598
599 // Code pattern for loading a floating point value. Input value must
600 // be either a smi or a heap number object (fp value). Requirements:
601 // operand in register number. Returns operand as floating point number
602 // on FPU stack.
603 static void LoadFloatOperand(MacroAssembler* masm, Register number);
604
605 // Code pattern for loading floating point values. Input values must
606 // be either smi or heap number objects (fp values). Requirements:
607 // operand_1 on TOS+1 or in edx, operand_2 on TOS+2 or in eax.
608 // Returns operands as floating point numbers on FPU stack.
609 static void LoadFloatOperands(MacroAssembler* masm,
610 Register scratch,
611 ArgLocation arg_location = ARGS_ON_STACK);
612
613 // Similar to LoadFloatOperand but assumes that both operands are smis.
614 // Expects operands in edx, eax.
615 static void LoadFloatSmis(MacroAssembler* masm, Register scratch);
616
617 // Test if operands are smi or number objects (fp). Requirements:
618 // operand_1 in eax, operand_2 in edx; falls through on float
619 // operands, jumps to the non_float label otherwise.
620 static void CheckFloatOperands(MacroAssembler* masm,
621 Label* non_float,
622 Register scratch);
623
Ben Murdochb0fe1622011-05-05 13:52:32 +0100624 // Checks that the two floating point numbers on top of the FPU stack
625 // have int32 values.
626 static void CheckFloatOperandsAreInt32(MacroAssembler* masm,
627 Label* non_int32);
628
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100629 // Takes the operands in edx and eax and loads them as integers in eax
630 // and ecx.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100631 static void LoadUnknownsAsIntegers(MacroAssembler* masm,
632 bool use_sse3,
633 Label* operand_conversion_failure);
634
Ben Murdochb0fe1622011-05-05 13:52:32 +0100635 // Must only be called after LoadUnknownsAsIntegers. Assumes that the
636 // operands are pushed on the stack, and that their conversions to int32
637 // are in eax and ecx. Checks that the original numbers were in the int32
638 // range.
639 static void CheckLoadedIntegersWereInt32(MacroAssembler* masm,
640 bool use_sse3,
641 Label* not_int32);
642
643 // Assumes that operands are smis or heap numbers and loads them
644 // into xmm0 and xmm1. Operands are in edx and eax.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100645 // Leaves operands unchanged.
646 static void LoadSSE2Operands(MacroAssembler* masm);
647
648 // Test if operands are numbers (smi or HeapNumber objects), and load
649 // them into xmm0 and xmm1 if they are. Jump to label not_numbers if
650 // either operand is not a number. Operands are in edx and eax.
651 // Leaves operands unchanged.
652 static void LoadSSE2Operands(MacroAssembler* masm, Label* not_numbers);
653
654 // Similar to LoadSSE2Operands but assumes that both operands are smis.
655 // Expects operands in edx, eax.
656 static void LoadSSE2Smis(MacroAssembler* masm, Register scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100657
658 // Checks that the two floating point numbers loaded into xmm0 and xmm1
659 // have int32 values.
660 static void CheckSSE2OperandsAreInt32(MacroAssembler* masm,
661 Label* non_int32,
662 Register scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100663};
664
665
Ben Murdoch257744e2011-11-30 15:57:28 +0000666// Get the integer part of a heap number. Surprisingly, all this bit twiddling
667// is faster than using the built-in instructions on floating point registers.
668// Trashes edi and ebx. Dest is ecx. Source cannot be ecx or one of the
669// trashed registers.
670static void IntegerConvert(MacroAssembler* masm,
671 Register source,
672 bool use_sse3,
673 Label* conversion_failure) {
674 ASSERT(!source.is(ecx) && !source.is(edi) && !source.is(ebx));
675 Label done, right_exponent, normal_exponent;
676 Register scratch = ebx;
677 Register scratch2 = edi;
678 // Get exponent word.
679 __ mov(scratch, FieldOperand(source, HeapNumber::kExponentOffset));
680 // Get exponent alone in scratch2.
681 __ mov(scratch2, scratch);
682 __ and_(scratch2, HeapNumber::kExponentMask);
683 if (use_sse3) {
684 CpuFeatures::Scope scope(SSE3);
685 // Check whether the exponent is too big for a 64 bit signed integer.
686 static const uint32_t kTooBigExponent =
687 (HeapNumber::kExponentBias + 63) << HeapNumber::kExponentShift;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000688 __ cmp(scratch2, Immediate(kTooBigExponent));
Ben Murdoch257744e2011-11-30 15:57:28 +0000689 __ j(greater_equal, conversion_failure);
690 // Load x87 register with heap number.
691 __ fld_d(FieldOperand(source, HeapNumber::kValueOffset));
692 // Reserve space for 64 bit answer.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000693 __ sub(esp, Immediate(sizeof(uint64_t))); // Nolint.
Ben Murdoch257744e2011-11-30 15:57:28 +0000694 // Do conversion, which cannot fail because we checked the exponent.
695 __ fisttp_d(Operand(esp, 0));
696 __ mov(ecx, Operand(esp, 0)); // Load low word of answer into ecx.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000697 __ add(esp, Immediate(sizeof(uint64_t))); // Nolint.
Ben Murdoch257744e2011-11-30 15:57:28 +0000698 } else {
699 // Load ecx with zero. We use this either for the final shift or
700 // for the answer.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000701 __ xor_(ecx, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +0000702 // Check whether the exponent matches a 32 bit signed int that cannot be
703 // represented by a Smi. A non-smi 32 bit integer is 1.xxx * 2^30 so the
704 // exponent is 30 (biased). This is the exponent that we are fastest at and
705 // also the highest exponent we can handle here.
706 const uint32_t non_smi_exponent =
707 (HeapNumber::kExponentBias + 30) << HeapNumber::kExponentShift;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000708 __ cmp(scratch2, Immediate(non_smi_exponent));
Ben Murdoch257744e2011-11-30 15:57:28 +0000709 // If we have a match of the int32-but-not-Smi exponent then skip some
710 // logic.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000711 __ j(equal, &right_exponent, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +0000712 // If the exponent is higher than that then go to slow case. This catches
713 // numbers that don't fit in a signed int32, infinities and NaNs.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000714 __ j(less, &normal_exponent, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +0000715
716 {
717 // Handle a big exponent. The only reason we have this code is that the
718 // >>> operator has a tendency to generate numbers with an exponent of 31.
719 const uint32_t big_non_smi_exponent =
720 (HeapNumber::kExponentBias + 31) << HeapNumber::kExponentShift;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000721 __ cmp(scratch2, Immediate(big_non_smi_exponent));
Ben Murdoch257744e2011-11-30 15:57:28 +0000722 __ j(not_equal, conversion_failure);
723 // We have the big exponent, typically from >>>. This means the number is
724 // in the range 2^31 to 2^32 - 1. Get the top bits of the mantissa.
725 __ mov(scratch2, scratch);
726 __ and_(scratch2, HeapNumber::kMantissaMask);
727 // Put back the implicit 1.
728 __ or_(scratch2, 1 << HeapNumber::kExponentShift);
729 // Shift up the mantissa bits to take up the space the exponent used to
730 // take. We just orred in the implicit bit so that took care of one and
731 // we want to use the full unsigned range so we subtract 1 bit from the
732 // shift distance.
733 const int big_shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 1;
734 __ shl(scratch2, big_shift_distance);
735 // Get the second half of the double.
736 __ mov(ecx, FieldOperand(source, HeapNumber::kMantissaOffset));
737 // Shift down 21 bits to get the most significant 11 bits or the low
738 // mantissa word.
739 __ shr(ecx, 32 - big_shift_distance);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000740 __ or_(ecx, scratch2);
Ben Murdoch257744e2011-11-30 15:57:28 +0000741 // We have the answer in ecx, but we may need to negate it.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000742 __ test(scratch, scratch);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000743 __ j(positive, &done, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +0000744 __ neg(ecx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000745 __ jmp(&done, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +0000746 }
747
748 __ bind(&normal_exponent);
749 // Exponent word in scratch, exponent part of exponent word in scratch2.
750 // Zero in ecx.
751 // We know the exponent is smaller than 30 (biased). If it is less than
Ben Murdochc7cc0282012-03-05 14:35:55 +0000752 // 0 (biased) then the number is smaller in magnitude than 1.0 * 2^0, i.e.
Ben Murdoch257744e2011-11-30 15:57:28 +0000753 // it rounds to zero.
754 const uint32_t zero_exponent =
755 (HeapNumber::kExponentBias + 0) << HeapNumber::kExponentShift;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000756 __ sub(scratch2, Immediate(zero_exponent));
Ben Murdoch257744e2011-11-30 15:57:28 +0000757 // ecx already has a Smi zero.
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000758 __ j(less, &done, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +0000759
760 // We have a shifted exponent between 0 and 30 in scratch2.
761 __ shr(scratch2, HeapNumber::kExponentShift);
762 __ mov(ecx, Immediate(30));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000763 __ sub(ecx, scratch2);
Ben Murdoch257744e2011-11-30 15:57:28 +0000764
765 __ bind(&right_exponent);
766 // Here ecx is the shift, scratch is the exponent word.
767 // Get the top bits of the mantissa.
768 __ and_(scratch, HeapNumber::kMantissaMask);
769 // Put back the implicit 1.
770 __ or_(scratch, 1 << HeapNumber::kExponentShift);
771 // Shift up the mantissa bits to take up the space the exponent used to
772 // take. We have kExponentShift + 1 significant bits int he low end of the
773 // word. Shift them to the top bits.
774 const int shift_distance = HeapNumber::kNonMantissaBitsInTopWord - 2;
775 __ shl(scratch, shift_distance);
776 // Get the second half of the double. For some exponents we don't
777 // actually need this because the bits get shifted out again, but
778 // it's probably slower to test than just to do it.
779 __ mov(scratch2, FieldOperand(source, HeapNumber::kMantissaOffset));
780 // Shift down 22 bits to get the most significant 10 bits or the low
781 // mantissa word.
782 __ shr(scratch2, 32 - shift_distance);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000783 __ or_(scratch2, scratch);
Ben Murdoch257744e2011-11-30 15:57:28 +0000784 // Move down according to the exponent.
785 __ shr_cl(scratch2);
786 // Now the unsigned answer is in scratch2. We need to move it to ecx and
787 // we may need to fix the sign.
788 Label negative;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000789 __ xor_(ecx, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +0000790 __ cmp(ecx, FieldOperand(source, HeapNumber::kExponentOffset));
791 __ j(greater, &negative, Label::kNear);
792 __ mov(ecx, scratch2);
793 __ jmp(&done, Label::kNear);
794 __ bind(&negative);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000795 __ sub(ecx, scratch2);
Ben Murdoch257744e2011-11-30 15:57:28 +0000796 __ bind(&done);
797 }
798}
799
800
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000801void UnaryOpStub::PrintName(StringStream* stream) {
Ben Murdoch257744e2011-11-30 15:57:28 +0000802 const char* op_name = Token::Name(op_);
803 const char* overwrite_name = NULL; // Make g++ happy.
804 switch (mode_) {
805 case UNARY_NO_OVERWRITE: overwrite_name = "Alloc"; break;
806 case UNARY_OVERWRITE: overwrite_name = "Overwrite"; break;
807 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000808 stream->Add("UnaryOpStub_%s_%s_%s",
809 op_name,
810 overwrite_name,
811 UnaryOpIC::GetName(operand_type_));
Ben Murdoch257744e2011-11-30 15:57:28 +0000812}
813
814
815// TODO(svenpanne): Use virtual functions instead of switch.
816void UnaryOpStub::Generate(MacroAssembler* masm) {
817 switch (operand_type_) {
818 case UnaryOpIC::UNINITIALIZED:
819 GenerateTypeTransition(masm);
820 break;
821 case UnaryOpIC::SMI:
822 GenerateSmiStub(masm);
823 break;
824 case UnaryOpIC::HEAP_NUMBER:
825 GenerateHeapNumberStub(masm);
826 break;
827 case UnaryOpIC::GENERIC:
828 GenerateGenericStub(masm);
829 break;
830 }
831}
832
833
834void UnaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
835 __ pop(ecx); // Save return address.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000836
837 __ push(eax); // the operand
Ben Murdoch257744e2011-11-30 15:57:28 +0000838 __ push(Immediate(Smi::FromInt(op_)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000839 __ push(Immediate(Smi::FromInt(mode_)));
Ben Murdoch257744e2011-11-30 15:57:28 +0000840 __ push(Immediate(Smi::FromInt(operand_type_)));
841
842 __ push(ecx); // Push return address.
843
844 // Patch the caller to an appropriate specialized stub and return the
845 // operation result to the caller of the stub.
846 __ TailCallExternalReference(
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000847 ExternalReference(IC_Utility(IC::kUnaryOp_Patch), masm->isolate()), 4, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +0000848}
849
850
851// TODO(svenpanne): Use virtual functions instead of switch.
852void UnaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
853 switch (op_) {
854 case Token::SUB:
855 GenerateSmiStubSub(masm);
856 break;
857 case Token::BIT_NOT:
858 GenerateSmiStubBitNot(masm);
859 break;
860 default:
861 UNREACHABLE();
862 }
863}
864
865
866void UnaryOpStub::GenerateSmiStubSub(MacroAssembler* masm) {
867 Label non_smi, undo, slow;
868 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow,
869 Label::kNear, Label::kNear, Label::kNear);
870 __ bind(&undo);
871 GenerateSmiCodeUndo(masm);
872 __ bind(&non_smi);
873 __ bind(&slow);
874 GenerateTypeTransition(masm);
875}
876
877
878void UnaryOpStub::GenerateSmiStubBitNot(MacroAssembler* masm) {
879 Label non_smi;
880 GenerateSmiCodeBitNot(masm, &non_smi);
881 __ bind(&non_smi);
882 GenerateTypeTransition(masm);
883}
884
885
886void UnaryOpStub::GenerateSmiCodeSub(MacroAssembler* masm,
887 Label* non_smi,
888 Label* undo,
889 Label* slow,
890 Label::Distance non_smi_near,
891 Label::Distance undo_near,
892 Label::Distance slow_near) {
893 // Check whether the value is a smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000894 __ JumpIfNotSmi(eax, non_smi, non_smi_near);
Ben Murdoch257744e2011-11-30 15:57:28 +0000895
896 // We can't handle -0 with smis, so use a type transition for that case.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000897 __ test(eax, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +0000898 __ j(zero, slow, slow_near);
899
900 // Try optimistic subtraction '0 - value', saving operand in eax for undo.
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000901 __ mov(edx, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +0000902 __ Set(eax, Immediate(0));
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000903 __ sub(eax, edx);
Ben Murdoch257744e2011-11-30 15:57:28 +0000904 __ j(overflow, undo, undo_near);
905 __ ret(0);
906}
907
908
909void UnaryOpStub::GenerateSmiCodeBitNot(
910 MacroAssembler* masm,
911 Label* non_smi,
912 Label::Distance non_smi_near) {
913 // Check whether the value is a smi.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000914 __ JumpIfNotSmi(eax, non_smi, non_smi_near);
Ben Murdoch257744e2011-11-30 15:57:28 +0000915
916 // Flip bits and revert inverted smi-tag.
917 __ not_(eax);
918 __ and_(eax, ~kSmiTagMask);
919 __ ret(0);
920}
921
922
923void UnaryOpStub::GenerateSmiCodeUndo(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000924 __ mov(eax, edx);
Ben Murdoch257744e2011-11-30 15:57:28 +0000925}
926
927
928// TODO(svenpanne): Use virtual functions instead of switch.
929void UnaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
930 switch (op_) {
931 case Token::SUB:
932 GenerateHeapNumberStubSub(masm);
933 break;
934 case Token::BIT_NOT:
935 GenerateHeapNumberStubBitNot(masm);
936 break;
937 default:
938 UNREACHABLE();
939 }
940}
941
942
943void UnaryOpStub::GenerateHeapNumberStubSub(MacroAssembler* masm) {
944 Label non_smi, undo, slow, call_builtin;
945 GenerateSmiCodeSub(masm, &non_smi, &undo, &call_builtin, Label::kNear);
946 __ bind(&non_smi);
947 GenerateHeapNumberCodeSub(masm, &slow);
948 __ bind(&undo);
949 GenerateSmiCodeUndo(masm);
950 __ bind(&slow);
951 GenerateTypeTransition(masm);
952 __ bind(&call_builtin);
953 GenerateGenericCodeFallback(masm);
954}
955
956
957void UnaryOpStub::GenerateHeapNumberStubBitNot(
958 MacroAssembler* masm) {
959 Label non_smi, slow;
960 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
961 __ bind(&non_smi);
962 GenerateHeapNumberCodeBitNot(masm, &slow);
963 __ bind(&slow);
964 GenerateTypeTransition(masm);
965}
966
967
968void UnaryOpStub::GenerateHeapNumberCodeSub(MacroAssembler* masm,
969 Label* slow) {
970 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
971 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
972 __ j(not_equal, slow);
973
974 if (mode_ == UNARY_OVERWRITE) {
975 __ xor_(FieldOperand(eax, HeapNumber::kExponentOffset),
976 Immediate(HeapNumber::kSignMask)); // Flip sign.
977 } else {
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000978 __ mov(edx, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +0000979 // edx: operand
980
981 Label slow_allocate_heapnumber, heapnumber_allocated;
982 __ AllocateHeapNumber(eax, ebx, ecx, &slow_allocate_heapnumber);
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000983 __ jmp(&heapnumber_allocated, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +0000984
985 __ bind(&slow_allocate_heapnumber);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000986 {
987 FrameScope scope(masm, StackFrame::INTERNAL);
988 __ push(edx);
989 __ CallRuntime(Runtime::kNumberAlloc, 0);
990 __ pop(edx);
991 }
Ben Murdoch257744e2011-11-30 15:57:28 +0000992
993 __ bind(&heapnumber_allocated);
994 // eax: allocated 'empty' number
995 __ mov(ecx, FieldOperand(edx, HeapNumber::kExponentOffset));
996 __ xor_(ecx, HeapNumber::kSignMask); // Flip sign.
997 __ mov(FieldOperand(eax, HeapNumber::kExponentOffset), ecx);
998 __ mov(ecx, FieldOperand(edx, HeapNumber::kMantissaOffset));
999 __ mov(FieldOperand(eax, HeapNumber::kMantissaOffset), ecx);
1000 }
1001 __ ret(0);
1002}
1003
1004
1005void UnaryOpStub::GenerateHeapNumberCodeBitNot(MacroAssembler* masm,
1006 Label* slow) {
1007 __ mov(edx, FieldOperand(eax, HeapObject::kMapOffset));
1008 __ cmp(edx, masm->isolate()->factory()->heap_number_map());
1009 __ j(not_equal, slow);
1010
1011 // Convert the heap number in eax to an untagged integer in ecx.
1012 IntegerConvert(masm, eax, CpuFeatures::IsSupported(SSE3), slow);
1013
1014 // Do the bitwise operation and check if the result fits in a smi.
1015 Label try_float;
1016 __ not_(ecx);
1017 __ cmp(ecx, 0xc0000000);
1018 __ j(sign, &try_float, Label::kNear);
1019
1020 // Tag the result as a smi and we're done.
1021 STATIC_ASSERT(kSmiTagSize == 1);
1022 __ lea(eax, Operand(ecx, times_2, kSmiTag));
1023 __ ret(0);
1024
1025 // Try to store the result in a heap number.
1026 __ bind(&try_float);
1027 if (mode_ == UNARY_NO_OVERWRITE) {
1028 Label slow_allocate_heapnumber, heapnumber_allocated;
1029 __ mov(ebx, eax);
1030 __ AllocateHeapNumber(eax, edx, edi, &slow_allocate_heapnumber);
1031 __ jmp(&heapnumber_allocated);
1032
1033 __ bind(&slow_allocate_heapnumber);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001034 {
1035 FrameScope scope(masm, StackFrame::INTERNAL);
1036 // Push the original HeapNumber on the stack. The integer value can't
1037 // be stored since it's untagged and not in the smi range (so we can't
1038 // smi-tag it). We'll recalculate the value after the GC instead.
1039 __ push(ebx);
1040 __ CallRuntime(Runtime::kNumberAlloc, 0);
1041 // New HeapNumber is in eax.
1042 __ pop(edx);
1043 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001044 // IntegerConvert uses ebx and edi as scratch registers.
1045 // This conversion won't go slow-case.
1046 IntegerConvert(masm, edx, CpuFeatures::IsSupported(SSE3), slow);
1047 __ not_(ecx);
1048
1049 __ bind(&heapnumber_allocated);
1050 }
1051 if (CpuFeatures::IsSupported(SSE2)) {
1052 CpuFeatures::Scope use_sse2(SSE2);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001053 __ cvtsi2sd(xmm0, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00001054 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1055 } else {
1056 __ push(ecx);
1057 __ fild_s(Operand(esp, 0));
1058 __ pop(ecx);
1059 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1060 }
1061 __ ret(0);
1062}
1063
1064
1065// TODO(svenpanne): Use virtual functions instead of switch.
1066void UnaryOpStub::GenerateGenericStub(MacroAssembler* masm) {
1067 switch (op_) {
1068 case Token::SUB:
1069 GenerateGenericStubSub(masm);
1070 break;
1071 case Token::BIT_NOT:
1072 GenerateGenericStubBitNot(masm);
1073 break;
1074 default:
1075 UNREACHABLE();
1076 }
1077}
1078
1079
1080void UnaryOpStub::GenerateGenericStubSub(MacroAssembler* masm) {
1081 Label non_smi, undo, slow;
1082 GenerateSmiCodeSub(masm, &non_smi, &undo, &slow, Label::kNear);
1083 __ bind(&non_smi);
1084 GenerateHeapNumberCodeSub(masm, &slow);
1085 __ bind(&undo);
1086 GenerateSmiCodeUndo(masm);
1087 __ bind(&slow);
1088 GenerateGenericCodeFallback(masm);
1089}
1090
1091
1092void UnaryOpStub::GenerateGenericStubBitNot(MacroAssembler* masm) {
1093 Label non_smi, slow;
1094 GenerateSmiCodeBitNot(masm, &non_smi, Label::kNear);
1095 __ bind(&non_smi);
1096 GenerateHeapNumberCodeBitNot(masm, &slow);
1097 __ bind(&slow);
1098 GenerateGenericCodeFallback(masm);
1099}
1100
1101
1102void UnaryOpStub::GenerateGenericCodeFallback(MacroAssembler* masm) {
1103 // Handle the slow case by jumping to the corresponding JavaScript builtin.
1104 __ pop(ecx); // pop return address.
1105 __ push(eax);
1106 __ push(ecx); // push return address
1107 switch (op_) {
1108 case Token::SUB:
1109 __ InvokeBuiltin(Builtins::UNARY_MINUS, JUMP_FUNCTION);
1110 break;
1111 case Token::BIT_NOT:
1112 __ InvokeBuiltin(Builtins::BIT_NOT, JUMP_FUNCTION);
1113 break;
1114 default:
1115 UNREACHABLE();
1116 }
1117}
1118
1119
Ben Murdoch257744e2011-11-30 15:57:28 +00001120void BinaryOpStub::GenerateTypeTransition(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001121 __ pop(ecx); // Save return address.
1122 __ push(edx);
1123 __ push(eax);
1124 // Left and right arguments are now on top.
1125 // Push this stub's key. Although the operation and the type info are
1126 // encoded into the key, the encoding is opaque, so push them too.
1127 __ push(Immediate(Smi::FromInt(MinorKey())));
1128 __ push(Immediate(Smi::FromInt(op_)));
1129 __ push(Immediate(Smi::FromInt(operands_type_)));
1130
1131 __ push(ecx); // Push return address.
1132
1133 // Patch the caller to an appropriate specialized stub and return the
1134 // operation result to the caller of the stub.
1135 __ TailCallExternalReference(
Ben Murdoch257744e2011-11-30 15:57:28 +00001136 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
Steve Block44f0eee2011-05-26 01:26:41 +01001137 masm->isolate()),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001138 5,
1139 1);
1140}
1141
1142
1143// Prepare for a type transition runtime call when the args are already on
1144// the stack, under the return address.
Ben Murdoch257744e2011-11-30 15:57:28 +00001145void BinaryOpStub::GenerateTypeTransitionWithSavedArgs(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001146 __ pop(ecx); // Save return address.
1147 // Left and right arguments are already on top of the stack.
1148 // Push this stub's key. Although the operation and the type info are
1149 // encoded into the key, the encoding is opaque, so push them too.
1150 __ push(Immediate(Smi::FromInt(MinorKey())));
1151 __ push(Immediate(Smi::FromInt(op_)));
1152 __ push(Immediate(Smi::FromInt(operands_type_)));
1153
1154 __ push(ecx); // Push return address.
1155
1156 // Patch the caller to an appropriate specialized stub and return the
1157 // operation result to the caller of the stub.
1158 __ TailCallExternalReference(
Ben Murdoch257744e2011-11-30 15:57:28 +00001159 ExternalReference(IC_Utility(IC::kBinaryOp_Patch),
Steve Block44f0eee2011-05-26 01:26:41 +01001160 masm->isolate()),
Ben Murdochb0fe1622011-05-05 13:52:32 +01001161 5,
1162 1);
1163}
1164
1165
Ben Murdoch257744e2011-11-30 15:57:28 +00001166void BinaryOpStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001167 // Explicitly allow generation of nested stubs. It is safe here because
1168 // generation code does not use any raw pointers.
1169 AllowStubCallsScope allow_stub_calls(masm, true);
1170
Ben Murdochb0fe1622011-05-05 13:52:32 +01001171 switch (operands_type_) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001172 case BinaryOpIC::UNINITIALIZED:
Ben Murdochb0fe1622011-05-05 13:52:32 +01001173 GenerateTypeTransition(masm);
1174 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001175 case BinaryOpIC::SMI:
Ben Murdochb0fe1622011-05-05 13:52:32 +01001176 GenerateSmiStub(masm);
1177 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001178 case BinaryOpIC::INT32:
Ben Murdochb0fe1622011-05-05 13:52:32 +01001179 GenerateInt32Stub(masm);
1180 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001181 case BinaryOpIC::HEAP_NUMBER:
Ben Murdochb0fe1622011-05-05 13:52:32 +01001182 GenerateHeapNumberStub(masm);
1183 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001184 case BinaryOpIC::ODDBALL:
Steve Block44f0eee2011-05-26 01:26:41 +01001185 GenerateOddballStub(masm);
1186 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001187 case BinaryOpIC::BOTH_STRING:
1188 GenerateBothStringStub(masm);
1189 break;
1190 case BinaryOpIC::STRING:
Ben Murdochb0fe1622011-05-05 13:52:32 +01001191 GenerateStringStub(masm);
1192 break;
Ben Murdoch257744e2011-11-30 15:57:28 +00001193 case BinaryOpIC::GENERIC:
Ben Murdochb0fe1622011-05-05 13:52:32 +01001194 GenerateGeneric(masm);
1195 break;
1196 default:
1197 UNREACHABLE();
1198 }
1199}
1200
1201
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001202void BinaryOpStub::PrintName(StringStream* stream) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001203 const char* op_name = Token::Name(op_);
1204 const char* overwrite_name;
1205 switch (mode_) {
1206 case NO_OVERWRITE: overwrite_name = "Alloc"; break;
1207 case OVERWRITE_RIGHT: overwrite_name = "OverwriteRight"; break;
1208 case OVERWRITE_LEFT: overwrite_name = "OverwriteLeft"; break;
1209 default: overwrite_name = "UnknownOverwrite"; break;
1210 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001211 stream->Add("BinaryOpStub_%s_%s_%s",
1212 op_name,
1213 overwrite_name,
1214 BinaryOpIC::GetName(operands_type_));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001215}
1216
1217
Ben Murdoch257744e2011-11-30 15:57:28 +00001218void BinaryOpStub::GenerateSmiCode(
1219 MacroAssembler* masm,
Ben Murdochb0fe1622011-05-05 13:52:32 +01001220 Label* slow,
1221 SmiCodeGenerateHeapNumberResults allow_heapnumber_results) {
1222 // 1. Move arguments into edx, eax except for DIV and MOD, which need the
1223 // dividend in eax and edx free for the division. Use eax, ebx for those.
1224 Comment load_comment(masm, "-- Load arguments");
1225 Register left = edx;
1226 Register right = eax;
1227 if (op_ == Token::DIV || op_ == Token::MOD) {
1228 left = eax;
1229 right = ebx;
1230 __ mov(ebx, eax);
1231 __ mov(eax, edx);
1232 }
1233
1234
1235 // 2. Prepare the smi check of both operands by oring them together.
1236 Comment smi_check_comment(masm, "-- Smi check arguments");
1237 Label not_smis;
1238 Register combined = ecx;
1239 ASSERT(!left.is(combined) && !right.is(combined));
1240 switch (op_) {
1241 case Token::BIT_OR:
1242 // Perform the operation into eax and smi check the result. Preserve
1243 // eax in case the result is not a smi.
1244 ASSERT(!left.is(ecx) && !right.is(ecx));
1245 __ mov(ecx, right);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001246 __ or_(right, left); // Bitwise or is commutative.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001247 combined = right;
1248 break;
1249
1250 case Token::BIT_XOR:
1251 case Token::BIT_AND:
1252 case Token::ADD:
1253 case Token::SUB:
1254 case Token::MUL:
1255 case Token::DIV:
1256 case Token::MOD:
1257 __ mov(combined, right);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001258 __ or_(combined, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001259 break;
1260
1261 case Token::SHL:
1262 case Token::SAR:
1263 case Token::SHR:
1264 // Move the right operand into ecx for the shift operation, use eax
1265 // for the smi check register.
1266 ASSERT(!left.is(ecx) && !right.is(ecx));
1267 __ mov(ecx, right);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001268 __ or_(right, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001269 combined = right;
1270 break;
1271
1272 default:
1273 break;
1274 }
1275
1276 // 3. Perform the smi check of the operands.
1277 STATIC_ASSERT(kSmiTag == 0); // Adjust zero check if not the case.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001278 __ JumpIfNotSmi(combined, &not_smis);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001279
1280 // 4. Operands are both smis, perform the operation leaving the result in
1281 // eax and check the result if necessary.
1282 Comment perform_smi(masm, "-- Perform smi operation");
1283 Label use_fp_on_smis;
1284 switch (op_) {
1285 case Token::BIT_OR:
1286 // Nothing to do.
1287 break;
1288
1289 case Token::BIT_XOR:
1290 ASSERT(right.is(eax));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001291 __ xor_(right, left); // Bitwise xor is commutative.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001292 break;
1293
1294 case Token::BIT_AND:
1295 ASSERT(right.is(eax));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001296 __ and_(right, left); // Bitwise and is commutative.
Ben Murdochb0fe1622011-05-05 13:52:32 +01001297 break;
1298
1299 case Token::SHL:
1300 // Remove tags from operands (but keep sign).
1301 __ SmiUntag(left);
1302 __ SmiUntag(ecx);
1303 // Perform the operation.
1304 __ shl_cl(left);
1305 // Check that the *signed* result fits in a smi.
1306 __ cmp(left, 0xc0000000);
Ben Murdoch257744e2011-11-30 15:57:28 +00001307 __ j(sign, &use_fp_on_smis);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001308 // Tag the result and store it in register eax.
1309 __ SmiTag(left);
1310 __ mov(eax, left);
1311 break;
1312
1313 case Token::SAR:
1314 // Remove tags from operands (but keep sign).
1315 __ SmiUntag(left);
1316 __ SmiUntag(ecx);
1317 // Perform the operation.
1318 __ sar_cl(left);
1319 // Tag the result and store it in register eax.
1320 __ SmiTag(left);
1321 __ mov(eax, left);
1322 break;
1323
1324 case Token::SHR:
1325 // Remove tags from operands (but keep sign).
1326 __ SmiUntag(left);
1327 __ SmiUntag(ecx);
1328 // Perform the operation.
1329 __ shr_cl(left);
1330 // Check that the *unsigned* result fits in a smi.
1331 // Neither of the two high-order bits can be set:
1332 // - 0x80000000: high bit would be lost when smi tagging.
1333 // - 0x40000000: this number would convert to negative when
1334 // Smi tagging these two cases can only happen with shifts
1335 // by 0 or 1 when handed a valid smi.
1336 __ test(left, Immediate(0xc0000000));
Ben Murdoch257744e2011-11-30 15:57:28 +00001337 __ j(not_zero, &use_fp_on_smis);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001338 // Tag the result and store it in register eax.
1339 __ SmiTag(left);
1340 __ mov(eax, left);
1341 break;
1342
1343 case Token::ADD:
1344 ASSERT(right.is(eax));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001345 __ add(right, left); // Addition is commutative.
Ben Murdoch257744e2011-11-30 15:57:28 +00001346 __ j(overflow, &use_fp_on_smis);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001347 break;
1348
1349 case Token::SUB:
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001350 __ sub(left, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00001351 __ j(overflow, &use_fp_on_smis);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001352 __ mov(eax, left);
1353 break;
1354
1355 case Token::MUL:
1356 // If the smi tag is 0 we can just leave the tag on one operand.
1357 STATIC_ASSERT(kSmiTag == 0); // Adjust code below if not the case.
1358 // We can't revert the multiplication if the result is not a smi
1359 // so save the right operand.
1360 __ mov(ebx, right);
1361 // Remove tag from one of the operands (but keep sign).
1362 __ SmiUntag(right);
1363 // Do multiplication.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001364 __ imul(right, left); // Multiplication is commutative.
Ben Murdoch257744e2011-11-30 15:57:28 +00001365 __ j(overflow, &use_fp_on_smis);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001366 // Check for negative zero result. Use combined = left | right.
1367 __ NegativeZeroTest(right, combined, &use_fp_on_smis);
1368 break;
1369
1370 case Token::DIV:
1371 // We can't revert the division if the result is not a smi so
1372 // save the left operand.
1373 __ mov(edi, left);
1374 // Check for 0 divisor.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001375 __ test(right, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00001376 __ j(zero, &use_fp_on_smis);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001377 // Sign extend left into edx:eax.
1378 ASSERT(left.is(eax));
1379 __ cdq();
1380 // Divide edx:eax by right.
1381 __ idiv(right);
1382 // Check for the corner case of dividing the most negative smi by
1383 // -1. We cannot use the overflow flag, since it is not set by idiv
1384 // instruction.
1385 STATIC_ASSERT(kSmiTag == 0 && kSmiTagSize == 1);
1386 __ cmp(eax, 0x40000000);
1387 __ j(equal, &use_fp_on_smis);
1388 // Check for negative zero result. Use combined = left | right.
1389 __ NegativeZeroTest(eax, combined, &use_fp_on_smis);
1390 // Check that the remainder is zero.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001391 __ test(edx, edx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001392 __ j(not_zero, &use_fp_on_smis);
1393 // Tag the result and store it in register eax.
1394 __ SmiTag(eax);
1395 break;
1396
1397 case Token::MOD:
1398 // Check for 0 divisor.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001399 __ test(right, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00001400 __ j(zero, &not_smis);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001401
1402 // Sign extend left into edx:eax.
1403 ASSERT(left.is(eax));
1404 __ cdq();
1405 // Divide edx:eax by right.
1406 __ idiv(right);
1407 // Check for negative zero result. Use combined = left | right.
1408 __ NegativeZeroTest(edx, combined, slow);
1409 // Move remainder to register eax.
1410 __ mov(eax, edx);
1411 break;
1412
1413 default:
1414 UNREACHABLE();
1415 }
1416
1417 // 5. Emit return of result in eax. Some operations have registers pushed.
1418 switch (op_) {
1419 case Token::ADD:
1420 case Token::SUB:
1421 case Token::MUL:
1422 case Token::DIV:
1423 __ ret(0);
1424 break;
1425 case Token::MOD:
1426 case Token::BIT_OR:
1427 case Token::BIT_AND:
1428 case Token::BIT_XOR:
1429 case Token::SAR:
1430 case Token::SHL:
1431 case Token::SHR:
1432 __ ret(2 * kPointerSize);
1433 break;
1434 default:
1435 UNREACHABLE();
1436 }
1437
1438 // 6. For some operations emit inline code to perform floating point
1439 // operations on known smis (e.g., if the result of the operation
1440 // overflowed the smi range).
1441 if (allow_heapnumber_results == NO_HEAPNUMBER_RESULTS) {
1442 __ bind(&use_fp_on_smis);
1443 switch (op_) {
1444 // Undo the effects of some operations, and some register moves.
1445 case Token::SHL:
1446 // The arguments are saved on the stack, and only used from there.
1447 break;
1448 case Token::ADD:
1449 // Revert right = right + left.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001450 __ sub(right, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001451 break;
1452 case Token::SUB:
1453 // Revert left = left - right.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001454 __ add(left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001455 break;
1456 case Token::MUL:
1457 // Right was clobbered but a copy is in ebx.
1458 __ mov(right, ebx);
1459 break;
1460 case Token::DIV:
1461 // Left was clobbered but a copy is in edi. Right is in ebx for
1462 // division. They should be in eax, ebx for jump to not_smi.
1463 __ mov(eax, edi);
1464 break;
1465 default:
1466 // No other operators jump to use_fp_on_smis.
1467 break;
1468 }
1469 __ jmp(&not_smis);
1470 } else {
1471 ASSERT(allow_heapnumber_results == ALLOW_HEAPNUMBER_RESULTS);
1472 switch (op_) {
Ben Murdoch257744e2011-11-30 15:57:28 +00001473 case Token::SHL:
1474 case Token::SHR: {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001475 Comment perform_float(masm, "-- Perform float operation on smis");
1476 __ bind(&use_fp_on_smis);
1477 // Result we want is in left == edx, so we can put the allocated heap
1478 // number in eax.
1479 __ AllocateHeapNumber(eax, ecx, ebx, slow);
1480 // Store the result in the HeapNumber and return.
Ben Murdoch257744e2011-11-30 15:57:28 +00001481 // It's OK to overwrite the arguments on the stack because we
1482 // are about to return.
1483 if (op_ == Token::SHR) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001484 __ mov(Operand(esp, 1 * kPointerSize), left);
Ben Murdoch257744e2011-11-30 15:57:28 +00001485 __ mov(Operand(esp, 2 * kPointerSize), Immediate(0));
1486 __ fild_d(Operand(esp, 1 * kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001487 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00001488 } else {
1489 ASSERT_EQ(Token::SHL, op_);
1490 if (CpuFeatures::IsSupported(SSE2)) {
1491 CpuFeatures::Scope use_sse2(SSE2);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001492 __ cvtsi2sd(xmm0, left);
Ben Murdoch257744e2011-11-30 15:57:28 +00001493 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1494 } else {
1495 __ mov(Operand(esp, 1 * kPointerSize), left);
1496 __ fild_s(Operand(esp, 1 * kPointerSize));
1497 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1498 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01001499 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001500 __ ret(2 * kPointerSize);
1501 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001502 }
1503
1504 case Token::ADD:
1505 case Token::SUB:
1506 case Token::MUL:
1507 case Token::DIV: {
1508 Comment perform_float(masm, "-- Perform float operation on smis");
1509 __ bind(&use_fp_on_smis);
1510 // Restore arguments to edx, eax.
1511 switch (op_) {
1512 case Token::ADD:
1513 // Revert right = right + left.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001514 __ sub(right, left);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001515 break;
1516 case Token::SUB:
1517 // Revert left = left - right.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001518 __ add(left, right);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001519 break;
1520 case Token::MUL:
1521 // Right was clobbered but a copy is in ebx.
1522 __ mov(right, ebx);
1523 break;
1524 case Token::DIV:
1525 // Left was clobbered but a copy is in edi. Right is in ebx for
1526 // division.
1527 __ mov(edx, edi);
1528 __ mov(eax, right);
1529 break;
1530 default: UNREACHABLE();
1531 break;
1532 }
1533 __ AllocateHeapNumber(ecx, ebx, no_reg, slow);
Ben Murdoch8b112d22011-06-08 16:22:53 +01001534 if (CpuFeatures::IsSupported(SSE2)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001535 CpuFeatures::Scope use_sse2(SSE2);
1536 FloatingPointHelper::LoadSSE2Smis(masm, ebx);
1537 switch (op_) {
1538 case Token::ADD: __ addsd(xmm0, xmm1); break;
1539 case Token::SUB: __ subsd(xmm0, xmm1); break;
1540 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1541 case Token::DIV: __ divsd(xmm0, xmm1); break;
1542 default: UNREACHABLE();
1543 }
1544 __ movdbl(FieldOperand(ecx, HeapNumber::kValueOffset), xmm0);
1545 } else { // SSE2 not available, use FPU.
1546 FloatingPointHelper::LoadFloatSmis(masm, ebx);
1547 switch (op_) {
1548 case Token::ADD: __ faddp(1); break;
1549 case Token::SUB: __ fsubp(1); break;
1550 case Token::MUL: __ fmulp(1); break;
1551 case Token::DIV: __ fdivp(1); break;
1552 default: UNREACHABLE();
1553 }
1554 __ fstp_d(FieldOperand(ecx, HeapNumber::kValueOffset));
1555 }
1556 __ mov(eax, ecx);
1557 __ ret(0);
1558 break;
1559 }
1560
1561 default:
1562 break;
1563 }
1564 }
1565
1566 // 7. Non-smi operands, fall out to the non-smi code with the operands in
1567 // edx and eax.
1568 Comment done_comment(masm, "-- Enter non-smi code");
1569 __ bind(&not_smis);
1570 switch (op_) {
1571 case Token::BIT_OR:
1572 case Token::SHL:
1573 case Token::SAR:
1574 case Token::SHR:
1575 // Right operand is saved in ecx and eax was destroyed by the smi
1576 // check.
1577 __ mov(eax, ecx);
1578 break;
1579
1580 case Token::DIV:
1581 case Token::MOD:
1582 // Operands are in eax, ebx at this point.
1583 __ mov(edx, eax);
1584 __ mov(eax, ebx);
1585 break;
1586
1587 default:
1588 break;
1589 }
1590}
1591
1592
Ben Murdoch257744e2011-11-30 15:57:28 +00001593void BinaryOpStub::GenerateSmiStub(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001594 Label call_runtime;
1595
1596 switch (op_) {
1597 case Token::ADD:
1598 case Token::SUB:
1599 case Token::MUL:
1600 case Token::DIV:
1601 break;
1602 case Token::MOD:
1603 case Token::BIT_OR:
1604 case Token::BIT_AND:
1605 case Token::BIT_XOR:
1606 case Token::SAR:
1607 case Token::SHL:
1608 case Token::SHR:
1609 GenerateRegisterArgsPush(masm);
1610 break;
1611 default:
1612 UNREACHABLE();
1613 }
1614
Ben Murdoch257744e2011-11-30 15:57:28 +00001615 if (result_type_ == BinaryOpIC::UNINITIALIZED ||
1616 result_type_ == BinaryOpIC::SMI) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001617 GenerateSmiCode(masm, &call_runtime, NO_HEAPNUMBER_RESULTS);
1618 } else {
1619 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
1620 }
1621 __ bind(&call_runtime);
1622 switch (op_) {
1623 case Token::ADD:
1624 case Token::SUB:
1625 case Token::MUL:
1626 case Token::DIV:
1627 GenerateTypeTransition(masm);
1628 break;
1629 case Token::MOD:
1630 case Token::BIT_OR:
1631 case Token::BIT_AND:
1632 case Token::BIT_XOR:
1633 case Token::SAR:
1634 case Token::SHL:
1635 case Token::SHR:
1636 GenerateTypeTransitionWithSavedArgs(masm);
1637 break;
1638 default:
1639 UNREACHABLE();
1640 }
1641}
1642
1643
Ben Murdoch257744e2011-11-30 15:57:28 +00001644void BinaryOpStub::GenerateStringStub(MacroAssembler* masm) {
1645 ASSERT(operands_type_ == BinaryOpIC::STRING);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001646 ASSERT(op_ == Token::ADD);
Steve Block1e0659c2011-05-24 12:43:12 +01001647 // Try to add arguments as strings, otherwise, transition to the generic
Ben Murdoch257744e2011-11-30 15:57:28 +00001648 // BinaryOpIC type.
Steve Block1e0659c2011-05-24 12:43:12 +01001649 GenerateAddStrings(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001650 GenerateTypeTransition(masm);
1651}
1652
1653
Ben Murdoch257744e2011-11-30 15:57:28 +00001654void BinaryOpStub::GenerateBothStringStub(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001655 Label call_runtime;
Ben Murdoch257744e2011-11-30 15:57:28 +00001656 ASSERT(operands_type_ == BinaryOpIC::BOTH_STRING);
1657 ASSERT(op_ == Token::ADD);
1658 // If both arguments are strings, call the string add stub.
1659 // Otherwise, do a transition.
1660
1661 // Registers containing left and right operands respectively.
1662 Register left = edx;
1663 Register right = eax;
1664
1665 // Test if left operand is a string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001666 __ JumpIfSmi(left, &call_runtime, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00001667 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001668 __ j(above_equal, &call_runtime, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00001669
1670 // Test if right operand is a string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001671 __ JumpIfSmi(right, &call_runtime, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00001672 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001673 __ j(above_equal, &call_runtime, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00001674
1675 StringAddStub string_add_stub(NO_STRING_CHECK_IN_STUB);
1676 GenerateRegisterArgsPush(masm);
1677 __ TailCallStub(&string_add_stub);
1678
1679 __ bind(&call_runtime);
1680 GenerateTypeTransition(masm);
1681}
1682
1683
1684void BinaryOpStub::GenerateInt32Stub(MacroAssembler* masm) {
1685 Label call_runtime;
1686 ASSERT(operands_type_ == BinaryOpIC::INT32);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001687
1688 // Floating point case.
1689 switch (op_) {
1690 case Token::ADD:
1691 case Token::SUB:
1692 case Token::MUL:
1693 case Token::DIV: {
1694 Label not_floats;
1695 Label not_int32;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001696 if (CpuFeatures::IsSupported(SSE2)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001697 CpuFeatures::Scope use_sse2(SSE2);
1698 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1699 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1700 switch (op_) {
1701 case Token::ADD: __ addsd(xmm0, xmm1); break;
1702 case Token::SUB: __ subsd(xmm0, xmm1); break;
1703 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1704 case Token::DIV: __ divsd(xmm0, xmm1); break;
1705 default: UNREACHABLE();
1706 }
1707 // Check result type if it is currently Int32.
Ben Murdoch257744e2011-11-30 15:57:28 +00001708 if (result_type_ <= BinaryOpIC::INT32) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001709 __ cvttsd2si(ecx, Operand(xmm0));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001710 __ cvtsi2sd(xmm2, ecx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001711 __ ucomisd(xmm0, xmm2);
1712 __ j(not_zero, &not_int32);
1713 __ j(carry, &not_int32);
1714 }
1715 GenerateHeapResultAllocation(masm, &call_runtime);
1716 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1717 __ ret(0);
1718 } else { // SSE2 not available, use FPU.
1719 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1720 FloatingPointHelper::LoadFloatOperands(
1721 masm,
1722 ecx,
1723 FloatingPointHelper::ARGS_IN_REGISTERS);
1724 FloatingPointHelper::CheckFloatOperandsAreInt32(masm, &not_int32);
1725 switch (op_) {
1726 case Token::ADD: __ faddp(1); break;
1727 case Token::SUB: __ fsubp(1); break;
1728 case Token::MUL: __ fmulp(1); break;
1729 case Token::DIV: __ fdivp(1); break;
1730 default: UNREACHABLE();
1731 }
1732 Label after_alloc_failure;
1733 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1734 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1735 __ ret(0);
1736 __ bind(&after_alloc_failure);
1737 __ ffree();
1738 __ jmp(&call_runtime);
1739 }
1740
1741 __ bind(&not_floats);
1742 __ bind(&not_int32);
1743 GenerateTypeTransition(masm);
1744 break;
1745 }
1746
1747 case Token::MOD: {
1748 // For MOD we go directly to runtime in the non-smi case.
1749 break;
1750 }
1751 case Token::BIT_OR:
1752 case Token::BIT_AND:
1753 case Token::BIT_XOR:
1754 case Token::SAR:
1755 case Token::SHL:
1756 case Token::SHR: {
1757 GenerateRegisterArgsPush(masm);
1758 Label not_floats;
1759 Label not_int32;
1760 Label non_smi_result;
1761 /* {
1762 CpuFeatures::Scope use_sse2(SSE2);
1763 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1764 FloatingPointHelper::CheckSSE2OperandsAreInt32(masm, &not_int32, ecx);
1765 }*/
1766 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1767 use_sse3_,
1768 &not_floats);
1769 FloatingPointHelper::CheckLoadedIntegersWereInt32(masm, use_sse3_,
1770 &not_int32);
1771 switch (op_) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001772 case Token::BIT_OR: __ or_(eax, ecx); break;
1773 case Token::BIT_AND: __ and_(eax, ecx); break;
1774 case Token::BIT_XOR: __ xor_(eax, ecx); break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001775 case Token::SAR: __ sar_cl(eax); break;
1776 case Token::SHL: __ shl_cl(eax); break;
1777 case Token::SHR: __ shr_cl(eax); break;
1778 default: UNREACHABLE();
1779 }
1780 if (op_ == Token::SHR) {
1781 // Check if result is non-negative and fits in a smi.
1782 __ test(eax, Immediate(0xc0000000));
1783 __ j(not_zero, &call_runtime);
1784 } else {
1785 // Check if result fits in a smi.
1786 __ cmp(eax, 0xc0000000);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001787 __ j(negative, &non_smi_result, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001788 }
1789 // Tag smi result and return.
1790 __ SmiTag(eax);
1791 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1792
1793 // All ops except SHR return a signed int32 that we load in
1794 // a HeapNumber.
1795 if (op_ != Token::SHR) {
1796 __ bind(&non_smi_result);
1797 // Allocate a heap number if needed.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001798 __ mov(ebx, eax); // ebx: result
Ben Murdoch257744e2011-11-30 15:57:28 +00001799 Label skip_allocation;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001800 switch (mode_) {
1801 case OVERWRITE_LEFT:
1802 case OVERWRITE_RIGHT:
1803 // If the operand was an object, we skip the
1804 // allocation of a heap number.
1805 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
1806 1 * kPointerSize : 2 * kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001807 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001808 // Fall through!
1809 case NO_OVERWRITE:
1810 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
1811 __ bind(&skip_allocation);
1812 break;
1813 default: UNREACHABLE();
1814 }
1815 // Store the result in the HeapNumber and return.
Ben Murdoch8b112d22011-06-08 16:22:53 +01001816 if (CpuFeatures::IsSupported(SSE2)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001817 CpuFeatures::Scope use_sse2(SSE2);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001818 __ cvtsi2sd(xmm0, ebx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001819 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1820 } else {
1821 __ mov(Operand(esp, 1 * kPointerSize), ebx);
1822 __ fild_s(Operand(esp, 1 * kPointerSize));
1823 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1824 }
1825 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
1826 }
1827
1828 __ bind(&not_floats);
1829 __ bind(&not_int32);
1830 GenerateTypeTransitionWithSavedArgs(masm);
1831 break;
1832 }
1833 default: UNREACHABLE(); break;
1834 }
1835
1836 // If an allocation fails, or SHR or MOD hit a hard case,
1837 // use the runtime system to get the correct result.
1838 __ bind(&call_runtime);
1839
1840 switch (op_) {
1841 case Token::ADD:
1842 GenerateRegisterArgsPush(masm);
1843 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
1844 break;
1845 case Token::SUB:
1846 GenerateRegisterArgsPush(masm);
1847 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
1848 break;
1849 case Token::MUL:
1850 GenerateRegisterArgsPush(masm);
1851 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
1852 break;
1853 case Token::DIV:
1854 GenerateRegisterArgsPush(masm);
1855 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
1856 break;
1857 case Token::MOD:
1858 GenerateRegisterArgsPush(masm);
1859 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
1860 break;
1861 case Token::BIT_OR:
1862 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
1863 break;
1864 case Token::BIT_AND:
1865 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
1866 break;
1867 case Token::BIT_XOR:
1868 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
1869 break;
1870 case Token::SAR:
1871 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
1872 break;
1873 case Token::SHL:
1874 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
1875 break;
1876 case Token::SHR:
1877 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
1878 break;
1879 default:
1880 UNREACHABLE();
1881 }
1882}
1883
1884
Ben Murdoch257744e2011-11-30 15:57:28 +00001885void BinaryOpStub::GenerateOddballStub(MacroAssembler* masm) {
Steve Block44f0eee2011-05-26 01:26:41 +01001886 if (op_ == Token::ADD) {
1887 // Handle string addition here, because it is the only operation
1888 // that does not do a ToNumber conversion on the operands.
1889 GenerateAddStrings(masm);
1890 }
1891
Ben Murdoch257744e2011-11-30 15:57:28 +00001892 Factory* factory = masm->isolate()->factory();
1893
Steve Block44f0eee2011-05-26 01:26:41 +01001894 // Convert odd ball arguments to numbers.
Ben Murdoch257744e2011-11-30 15:57:28 +00001895 Label check, done;
1896 __ cmp(edx, factory->undefined_value());
1897 __ j(not_equal, &check, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001898 if (Token::IsBitOp(op_)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001899 __ xor_(edx, edx);
Steve Block44f0eee2011-05-26 01:26:41 +01001900 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00001901 __ mov(edx, Immediate(factory->nan_value()));
Steve Block44f0eee2011-05-26 01:26:41 +01001902 }
Ben Murdoch257744e2011-11-30 15:57:28 +00001903 __ jmp(&done, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001904 __ bind(&check);
Ben Murdoch257744e2011-11-30 15:57:28 +00001905 __ cmp(eax, factory->undefined_value());
1906 __ j(not_equal, &done, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01001907 if (Token::IsBitOp(op_)) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001908 __ xor_(eax, eax);
Steve Block44f0eee2011-05-26 01:26:41 +01001909 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00001910 __ mov(eax, Immediate(factory->nan_value()));
Steve Block44f0eee2011-05-26 01:26:41 +01001911 }
1912 __ bind(&done);
1913
1914 GenerateHeapNumberStub(masm);
1915}
1916
1917
Ben Murdoch257744e2011-11-30 15:57:28 +00001918void BinaryOpStub::GenerateHeapNumberStub(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001919 Label call_runtime;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001920
1921 // Floating point case.
1922 switch (op_) {
1923 case Token::ADD:
1924 case Token::SUB:
1925 case Token::MUL:
1926 case Token::DIV: {
1927 Label not_floats;
Ben Murdoch8b112d22011-06-08 16:22:53 +01001928 if (CpuFeatures::IsSupported(SSE2)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001929 CpuFeatures::Scope use_sse2(SSE2);
1930 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
1931
1932 switch (op_) {
1933 case Token::ADD: __ addsd(xmm0, xmm1); break;
1934 case Token::SUB: __ subsd(xmm0, xmm1); break;
1935 case Token::MUL: __ mulsd(xmm0, xmm1); break;
1936 case Token::DIV: __ divsd(xmm0, xmm1); break;
1937 default: UNREACHABLE();
1938 }
1939 GenerateHeapResultAllocation(masm, &call_runtime);
1940 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
1941 __ ret(0);
1942 } else { // SSE2 not available, use FPU.
1943 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
1944 FloatingPointHelper::LoadFloatOperands(
1945 masm,
1946 ecx,
1947 FloatingPointHelper::ARGS_IN_REGISTERS);
1948 switch (op_) {
1949 case Token::ADD: __ faddp(1); break;
1950 case Token::SUB: __ fsubp(1); break;
1951 case Token::MUL: __ fmulp(1); break;
1952 case Token::DIV: __ fdivp(1); break;
1953 default: UNREACHABLE();
1954 }
1955 Label after_alloc_failure;
1956 GenerateHeapResultAllocation(masm, &after_alloc_failure);
1957 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
1958 __ ret(0);
1959 __ bind(&after_alloc_failure);
1960 __ ffree();
1961 __ jmp(&call_runtime);
1962 }
1963
1964 __ bind(&not_floats);
1965 GenerateTypeTransition(masm);
1966 break;
1967 }
1968
1969 case Token::MOD: {
1970 // For MOD we go directly to runtime in the non-smi case.
1971 break;
1972 }
1973 case Token::BIT_OR:
1974 case Token::BIT_AND:
1975 case Token::BIT_XOR:
1976 case Token::SAR:
1977 case Token::SHL:
1978 case Token::SHR: {
1979 GenerateRegisterArgsPush(masm);
1980 Label not_floats;
1981 Label non_smi_result;
1982 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
1983 use_sse3_,
1984 &not_floats);
1985 switch (op_) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001986 case Token::BIT_OR: __ or_(eax, ecx); break;
1987 case Token::BIT_AND: __ and_(eax, ecx); break;
1988 case Token::BIT_XOR: __ xor_(eax, ecx); break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001989 case Token::SAR: __ sar_cl(eax); break;
1990 case Token::SHL: __ shl_cl(eax); break;
1991 case Token::SHR: __ shr_cl(eax); break;
1992 default: UNREACHABLE();
1993 }
1994 if (op_ == Token::SHR) {
1995 // Check if result is non-negative and fits in a smi.
1996 __ test(eax, Immediate(0xc0000000));
1997 __ j(not_zero, &call_runtime);
1998 } else {
1999 // Check if result fits in a smi.
2000 __ cmp(eax, 0xc0000000);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002001 __ j(negative, &non_smi_result, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002002 }
2003 // Tag smi result and return.
2004 __ SmiTag(eax);
2005 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
2006
2007 // All ops except SHR return a signed int32 that we load in
2008 // a HeapNumber.
2009 if (op_ != Token::SHR) {
2010 __ bind(&non_smi_result);
2011 // Allocate a heap number if needed.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002012 __ mov(ebx, eax); // ebx: result
Ben Murdoch257744e2011-11-30 15:57:28 +00002013 Label skip_allocation;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002014 switch (mode_) {
2015 case OVERWRITE_LEFT:
2016 case OVERWRITE_RIGHT:
2017 // If the operand was an object, we skip the
2018 // allocation of a heap number.
2019 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
2020 1 * kPointerSize : 2 * kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002021 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002022 // Fall through!
2023 case NO_OVERWRITE:
2024 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
2025 __ bind(&skip_allocation);
2026 break;
2027 default: UNREACHABLE();
2028 }
2029 // Store the result in the HeapNumber and return.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002030 if (CpuFeatures::IsSupported(SSE2)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002031 CpuFeatures::Scope use_sse2(SSE2);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002032 __ cvtsi2sd(xmm0, ebx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002033 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2034 } else {
2035 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2036 __ fild_s(Operand(esp, 1 * kPointerSize));
2037 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2038 }
2039 __ ret(2 * kPointerSize); // Drop two pushed arguments from the stack.
2040 }
2041
2042 __ bind(&not_floats);
2043 GenerateTypeTransitionWithSavedArgs(masm);
2044 break;
2045 }
2046 default: UNREACHABLE(); break;
2047 }
2048
2049 // If an allocation fails, or SHR or MOD hit a hard case,
2050 // use the runtime system to get the correct result.
2051 __ bind(&call_runtime);
2052
2053 switch (op_) {
2054 case Token::ADD:
2055 GenerateRegisterArgsPush(masm);
2056 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2057 break;
2058 case Token::SUB:
2059 GenerateRegisterArgsPush(masm);
2060 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2061 break;
2062 case Token::MUL:
2063 GenerateRegisterArgsPush(masm);
2064 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2065 break;
2066 case Token::DIV:
2067 GenerateRegisterArgsPush(masm);
2068 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2069 break;
2070 case Token::MOD:
2071 GenerateRegisterArgsPush(masm);
2072 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2073 break;
2074 case Token::BIT_OR:
2075 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2076 break;
2077 case Token::BIT_AND:
2078 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2079 break;
2080 case Token::BIT_XOR:
2081 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2082 break;
2083 case Token::SAR:
2084 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2085 break;
2086 case Token::SHL:
2087 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2088 break;
2089 case Token::SHR:
2090 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2091 break;
2092 default:
2093 UNREACHABLE();
2094 }
2095}
2096
2097
Ben Murdoch257744e2011-11-30 15:57:28 +00002098void BinaryOpStub::GenerateGeneric(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002099 Label call_runtime;
2100
Steve Block44f0eee2011-05-26 01:26:41 +01002101 Counters* counters = masm->isolate()->counters();
2102 __ IncrementCounter(counters->generic_binary_stub_calls(), 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002103
2104 switch (op_) {
2105 case Token::ADD:
2106 case Token::SUB:
2107 case Token::MUL:
2108 case Token::DIV:
2109 break;
2110 case Token::MOD:
2111 case Token::BIT_OR:
2112 case Token::BIT_AND:
2113 case Token::BIT_XOR:
2114 case Token::SAR:
2115 case Token::SHL:
2116 case Token::SHR:
2117 GenerateRegisterArgsPush(masm);
2118 break;
2119 default:
2120 UNREACHABLE();
2121 }
2122
2123 GenerateSmiCode(masm, &call_runtime, ALLOW_HEAPNUMBER_RESULTS);
2124
2125 // Floating point case.
2126 switch (op_) {
2127 case Token::ADD:
2128 case Token::SUB:
2129 case Token::MUL:
2130 case Token::DIV: {
2131 Label not_floats;
Ben Murdoch8b112d22011-06-08 16:22:53 +01002132 if (CpuFeatures::IsSupported(SSE2)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002133 CpuFeatures::Scope use_sse2(SSE2);
2134 FloatingPointHelper::LoadSSE2Operands(masm, &not_floats);
2135
2136 switch (op_) {
2137 case Token::ADD: __ addsd(xmm0, xmm1); break;
2138 case Token::SUB: __ subsd(xmm0, xmm1); break;
2139 case Token::MUL: __ mulsd(xmm0, xmm1); break;
2140 case Token::DIV: __ divsd(xmm0, xmm1); break;
2141 default: UNREACHABLE();
2142 }
2143 GenerateHeapResultAllocation(masm, &call_runtime);
2144 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2145 __ ret(0);
2146 } else { // SSE2 not available, use FPU.
2147 FloatingPointHelper::CheckFloatOperands(masm, &not_floats, ebx);
2148 FloatingPointHelper::LoadFloatOperands(
2149 masm,
2150 ecx,
2151 FloatingPointHelper::ARGS_IN_REGISTERS);
2152 switch (op_) {
2153 case Token::ADD: __ faddp(1); break;
2154 case Token::SUB: __ fsubp(1); break;
2155 case Token::MUL: __ fmulp(1); break;
2156 case Token::DIV: __ fdivp(1); break;
2157 default: UNREACHABLE();
2158 }
2159 Label after_alloc_failure;
2160 GenerateHeapResultAllocation(masm, &after_alloc_failure);
2161 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2162 __ ret(0);
2163 __ bind(&after_alloc_failure);
2164 __ ffree();
2165 __ jmp(&call_runtime);
2166 }
2167 __ bind(&not_floats);
2168 break;
2169 }
2170 case Token::MOD: {
2171 // For MOD we go directly to runtime in the non-smi case.
2172 break;
2173 }
2174 case Token::BIT_OR:
2175 case Token::BIT_AND:
2176 case Token::BIT_XOR:
2177 case Token::SAR:
2178 case Token::SHL:
2179 case Token::SHR: {
2180 Label non_smi_result;
2181 FloatingPointHelper::LoadUnknownsAsIntegers(masm,
2182 use_sse3_,
2183 &call_runtime);
2184 switch (op_) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002185 case Token::BIT_OR: __ or_(eax, ecx); break;
2186 case Token::BIT_AND: __ and_(eax, ecx); break;
2187 case Token::BIT_XOR: __ xor_(eax, ecx); break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002188 case Token::SAR: __ sar_cl(eax); break;
2189 case Token::SHL: __ shl_cl(eax); break;
2190 case Token::SHR: __ shr_cl(eax); break;
2191 default: UNREACHABLE();
2192 }
2193 if (op_ == Token::SHR) {
2194 // Check if result is non-negative and fits in a smi.
2195 __ test(eax, Immediate(0xc0000000));
2196 __ j(not_zero, &call_runtime);
2197 } else {
2198 // Check if result fits in a smi.
2199 __ cmp(eax, 0xc0000000);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002200 __ j(negative, &non_smi_result, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002201 }
2202 // Tag smi result and return.
2203 __ SmiTag(eax);
2204 __ ret(2 * kPointerSize); // Drop the arguments from the stack.
2205
2206 // All ops except SHR return a signed int32 that we load in
2207 // a HeapNumber.
2208 if (op_ != Token::SHR) {
2209 __ bind(&non_smi_result);
2210 // Allocate a heap number if needed.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002211 __ mov(ebx, eax); // ebx: result
Ben Murdoch257744e2011-11-30 15:57:28 +00002212 Label skip_allocation;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002213 switch (mode_) {
2214 case OVERWRITE_LEFT:
2215 case OVERWRITE_RIGHT:
2216 // If the operand was an object, we skip the
2217 // allocation of a heap number.
2218 __ mov(eax, Operand(esp, mode_ == OVERWRITE_RIGHT ?
2219 1 * kPointerSize : 2 * kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002220 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002221 // Fall through!
2222 case NO_OVERWRITE:
2223 __ AllocateHeapNumber(eax, ecx, edx, &call_runtime);
2224 __ bind(&skip_allocation);
2225 break;
2226 default: UNREACHABLE();
2227 }
2228 // Store the result in the HeapNumber and return.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002229 if (CpuFeatures::IsSupported(SSE2)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002230 CpuFeatures::Scope use_sse2(SSE2);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002231 __ cvtsi2sd(xmm0, ebx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002232 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm0);
2233 } else {
2234 __ mov(Operand(esp, 1 * kPointerSize), ebx);
2235 __ fild_s(Operand(esp, 1 * kPointerSize));
2236 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
2237 }
2238 __ ret(2 * kPointerSize);
2239 }
2240 break;
2241 }
2242 default: UNREACHABLE(); break;
2243 }
2244
2245 // If all else fails, use the runtime system to get the correct
2246 // result.
2247 __ bind(&call_runtime);
2248 switch (op_) {
2249 case Token::ADD: {
Steve Block1e0659c2011-05-24 12:43:12 +01002250 GenerateAddStrings(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002251 GenerateRegisterArgsPush(masm);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002252 __ InvokeBuiltin(Builtins::ADD, JUMP_FUNCTION);
2253 break;
2254 }
2255 case Token::SUB:
2256 GenerateRegisterArgsPush(masm);
2257 __ InvokeBuiltin(Builtins::SUB, JUMP_FUNCTION);
2258 break;
2259 case Token::MUL:
2260 GenerateRegisterArgsPush(masm);
2261 __ InvokeBuiltin(Builtins::MUL, JUMP_FUNCTION);
2262 break;
2263 case Token::DIV:
2264 GenerateRegisterArgsPush(masm);
2265 __ InvokeBuiltin(Builtins::DIV, JUMP_FUNCTION);
2266 break;
2267 case Token::MOD:
2268 __ InvokeBuiltin(Builtins::MOD, JUMP_FUNCTION);
2269 break;
2270 case Token::BIT_OR:
2271 __ InvokeBuiltin(Builtins::BIT_OR, JUMP_FUNCTION);
2272 break;
2273 case Token::BIT_AND:
2274 __ InvokeBuiltin(Builtins::BIT_AND, JUMP_FUNCTION);
2275 break;
2276 case Token::BIT_XOR:
2277 __ InvokeBuiltin(Builtins::BIT_XOR, JUMP_FUNCTION);
2278 break;
2279 case Token::SAR:
2280 __ InvokeBuiltin(Builtins::SAR, JUMP_FUNCTION);
2281 break;
2282 case Token::SHL:
2283 __ InvokeBuiltin(Builtins::SHL, JUMP_FUNCTION);
2284 break;
2285 case Token::SHR:
2286 __ InvokeBuiltin(Builtins::SHR, JUMP_FUNCTION);
2287 break;
2288 default:
2289 UNREACHABLE();
2290 }
2291}
2292
2293
Ben Murdoch257744e2011-11-30 15:57:28 +00002294void BinaryOpStub::GenerateAddStrings(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01002295 ASSERT(op_ == Token::ADD);
Ben Murdoch257744e2011-11-30 15:57:28 +00002296 Label left_not_string, call_runtime;
Steve Block1e0659c2011-05-24 12:43:12 +01002297
2298 // Registers containing left and right operands respectively.
2299 Register left = edx;
2300 Register right = eax;
2301
2302 // Test if left operand is a string.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002303 __ JumpIfSmi(left, &left_not_string, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01002304 __ CmpObjectType(left, FIRST_NONSTRING_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00002305 __ j(above_equal, &left_not_string, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01002306
2307 StringAddStub string_add_left_stub(NO_STRING_CHECK_LEFT_IN_STUB);
2308 GenerateRegisterArgsPush(masm);
2309 __ TailCallStub(&string_add_left_stub);
2310
2311 // Left operand is not a string, test right.
2312 __ bind(&left_not_string);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002313 __ JumpIfSmi(right, &call_runtime, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01002314 __ CmpObjectType(right, FIRST_NONSTRING_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00002315 __ j(above_equal, &call_runtime, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01002316
2317 StringAddStub string_add_right_stub(NO_STRING_CHECK_RIGHT_IN_STUB);
2318 GenerateRegisterArgsPush(masm);
2319 __ TailCallStub(&string_add_right_stub);
2320
2321 // Neither argument is a string.
2322 __ bind(&call_runtime);
2323}
2324
2325
Ben Murdoch257744e2011-11-30 15:57:28 +00002326void BinaryOpStub::GenerateHeapResultAllocation(
Ben Murdochb0fe1622011-05-05 13:52:32 +01002327 MacroAssembler* masm,
2328 Label* alloc_failure) {
2329 Label skip_allocation;
2330 OverwriteMode mode = mode_;
2331 switch (mode) {
2332 case OVERWRITE_LEFT: {
2333 // If the argument in edx is already an object, we skip the
2334 // allocation of a heap number.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002335 __ JumpIfNotSmi(edx, &skip_allocation, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002336 // Allocate a heap number for the result. Keep eax and edx intact
2337 // for the possible runtime call.
2338 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2339 // Now edx can be overwritten losing one of the arguments as we are
2340 // now done and will not need it any more.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002341 __ mov(edx, ebx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002342 __ bind(&skip_allocation);
2343 // Use object in edx as a result holder
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002344 __ mov(eax, edx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002345 break;
2346 }
2347 case OVERWRITE_RIGHT:
2348 // If the argument in eax is already an object, we skip the
2349 // allocation of a heap number.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002350 __ JumpIfNotSmi(eax, &skip_allocation, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002351 // Fall through!
2352 case NO_OVERWRITE:
2353 // Allocate a heap number for the result. Keep eax and edx intact
2354 // for the possible runtime call.
2355 __ AllocateHeapNumber(ebx, ecx, no_reg, alloc_failure);
2356 // Now eax can be overwritten losing one of the arguments as we are
2357 // now done and will not need it any more.
2358 __ mov(eax, ebx);
2359 __ bind(&skip_allocation);
2360 break;
2361 default: UNREACHABLE();
2362 }
2363}
2364
2365
Ben Murdoch257744e2011-11-30 15:57:28 +00002366void BinaryOpStub::GenerateRegisterArgsPush(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002367 __ pop(ecx);
2368 __ push(edx);
2369 __ push(eax);
2370 __ push(ecx);
2371}
2372
2373
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002374void TranscendentalCacheStub::Generate(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002375 // TAGGED case:
2376 // Input:
2377 // esp[4]: tagged number input argument (should be number).
2378 // esp[0]: return address.
2379 // Output:
2380 // eax: tagged double result.
2381 // UNTAGGED case:
2382 // Input::
2383 // esp[0]: return address.
2384 // xmm1: untagged double input argument
2385 // Output:
2386 // xmm1: untagged double result.
2387
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002388 Label runtime_call;
2389 Label runtime_call_clear_stack;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002390 Label skip_cache;
2391 const bool tagged = (argument_type_ == TAGGED);
2392 if (tagged) {
2393 // Test that eax is a number.
Ben Murdoch257744e2011-11-30 15:57:28 +00002394 Label input_not_smi;
2395 Label loaded;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002396 __ mov(eax, Operand(esp, kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002397 __ JumpIfNotSmi(eax, &input_not_smi, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002398 // Input is a smi. Untag and load it onto the FPU stack.
2399 // Then load the low and high words of the double into ebx, edx.
2400 STATIC_ASSERT(kSmiTagSize == 1);
2401 __ sar(eax, 1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002402 __ sub(esp, Immediate(2 * kPointerSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002403 __ mov(Operand(esp, 0), eax);
2404 __ fild_s(Operand(esp, 0));
2405 __ fst_d(Operand(esp, 0));
2406 __ pop(edx);
2407 __ pop(ebx);
Ben Murdoch257744e2011-11-30 15:57:28 +00002408 __ jmp(&loaded, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002409 __ bind(&input_not_smi);
2410 // Check if input is a HeapNumber.
2411 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002412 Factory* factory = masm->isolate()->factory();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002413 __ cmp(ebx, Immediate(factory->heap_number_map()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002414 __ j(not_equal, &runtime_call);
2415 // Input is a HeapNumber. Push it on the FPU stack and load its
2416 // low and high words into ebx, edx.
2417 __ fld_d(FieldOperand(eax, HeapNumber::kValueOffset));
2418 __ mov(edx, FieldOperand(eax, HeapNumber::kExponentOffset));
2419 __ mov(ebx, FieldOperand(eax, HeapNumber::kMantissaOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002420
Ben Murdochb0fe1622011-05-05 13:52:32 +01002421 __ bind(&loaded);
2422 } else { // UNTAGGED.
Ben Murdoch8b112d22011-06-08 16:22:53 +01002423 if (CpuFeatures::IsSupported(SSE4_1)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002424 CpuFeatures::Scope sse4_scope(SSE4_1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002425 __ pextrd(edx, xmm1, 0x1); // copy xmm1[63..32] to edx.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002426 } else {
2427 __ pshufd(xmm0, xmm1, 0x1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002428 __ movd(edx, xmm0);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002429 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002430 __ movd(ebx, xmm1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002431 }
2432
2433 // ST[0] or xmm1 == double value
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002434 // ebx = low 32 bits of double value
2435 // edx = high 32 bits of double value
2436 // Compute hash (the shifts are arithmetic):
2437 // h = (low ^ high); h ^= h >> 16; h ^= h >> 8; h = h & (cacheSize - 1);
2438 __ mov(ecx, ebx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002439 __ xor_(ecx, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002440 __ mov(eax, ecx);
2441 __ sar(eax, 16);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002442 __ xor_(ecx, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002443 __ mov(eax, ecx);
2444 __ sar(eax, 8);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002445 __ xor_(ecx, eax);
Steve Block44f0eee2011-05-26 01:26:41 +01002446 ASSERT(IsPowerOf2(TranscendentalCache::SubCache::kCacheSize));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002447 __ and_(ecx,
Steve Block44f0eee2011-05-26 01:26:41 +01002448 Immediate(TranscendentalCache::SubCache::kCacheSize - 1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002449
Ben Murdochb0fe1622011-05-05 13:52:32 +01002450 // ST[0] or xmm1 == double value.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002451 // ebx = low 32 bits of double value.
2452 // edx = high 32 bits of double value.
2453 // ecx = TranscendentalCache::hash(double value).
Steve Block44f0eee2011-05-26 01:26:41 +01002454 ExternalReference cache_array =
2455 ExternalReference::transcendental_cache_array_address(masm->isolate());
2456 __ mov(eax, Immediate(cache_array));
2457 int cache_array_index =
2458 type_ * sizeof(masm->isolate()->transcendental_cache()->caches_[0]);
2459 __ mov(eax, Operand(eax, cache_array_index));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002460 // Eax points to the cache for the type type_.
2461 // If NULL, the cache hasn't been initialized yet, so go through runtime.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002462 __ test(eax, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002463 __ j(zero, &runtime_call_clear_stack);
2464#ifdef DEBUG
2465 // Check that the layout of cache elements match expectations.
Steve Block44f0eee2011-05-26 01:26:41 +01002466 { TranscendentalCache::SubCache::Element test_elem[2];
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002467 char* elem_start = reinterpret_cast<char*>(&test_elem[0]);
2468 char* elem2_start = reinterpret_cast<char*>(&test_elem[1]);
2469 char* elem_in0 = reinterpret_cast<char*>(&(test_elem[0].in[0]));
2470 char* elem_in1 = reinterpret_cast<char*>(&(test_elem[0].in[1]));
2471 char* elem_out = reinterpret_cast<char*>(&(test_elem[0].output));
2472 CHECK_EQ(12, elem2_start - elem_start); // Two uint_32's and a pointer.
2473 CHECK_EQ(0, elem_in0 - elem_start);
2474 CHECK_EQ(kIntSize, elem_in1 - elem_start);
2475 CHECK_EQ(2 * kIntSize, elem_out - elem_start);
2476 }
2477#endif
2478 // Find the address of the ecx'th entry in the cache, i.e., &eax[ecx*12].
2479 __ lea(ecx, Operand(ecx, ecx, times_2, 0));
2480 __ lea(ecx, Operand(eax, ecx, times_4, 0));
2481 // Check if cache matches: Double value is stored in uint32_t[2] array.
Ben Murdoch257744e2011-11-30 15:57:28 +00002482 Label cache_miss;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002483 __ cmp(ebx, Operand(ecx, 0));
Ben Murdoch257744e2011-11-30 15:57:28 +00002484 __ j(not_equal, &cache_miss, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002485 __ cmp(edx, Operand(ecx, kIntSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00002486 __ j(not_equal, &cache_miss, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002487 // Cache hit!
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002488 Counters* counters = masm->isolate()->counters();
2489 __ IncrementCounter(counters->transcendental_cache_hit(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002490 __ mov(eax, Operand(ecx, 2 * kIntSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002491 if (tagged) {
2492 __ fstp(0);
2493 __ ret(kPointerSize);
2494 } else { // UNTAGGED.
2495 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2496 __ Ret();
2497 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002498
2499 __ bind(&cache_miss);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002500 __ IncrementCounter(counters->transcendental_cache_miss(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002501 // Update cache with new value.
2502 // We are short on registers, so use no_reg as scratch.
2503 // This gives slightly larger code.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002504 if (tagged) {
2505 __ AllocateHeapNumber(eax, edi, no_reg, &runtime_call_clear_stack);
2506 } else { // UNTAGGED.
2507 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002508 __ sub(esp, Immediate(kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002509 __ movdbl(Operand(esp, 0), xmm1);
2510 __ fld_d(Operand(esp, 0));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002511 __ add(esp, Immediate(kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002512 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002513 GenerateOperation(masm);
2514 __ mov(Operand(ecx, 0), ebx);
2515 __ mov(Operand(ecx, kIntSize), edx);
2516 __ mov(Operand(ecx, 2 * kIntSize), eax);
2517 __ fstp_d(FieldOperand(eax, HeapNumber::kValueOffset));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002518 if (tagged) {
2519 __ ret(kPointerSize);
2520 } else { // UNTAGGED.
2521 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2522 __ Ret();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002523
Ben Murdochb0fe1622011-05-05 13:52:32 +01002524 // Skip cache and return answer directly, only in untagged case.
2525 __ bind(&skip_cache);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002526 __ sub(esp, Immediate(kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002527 __ movdbl(Operand(esp, 0), xmm1);
2528 __ fld_d(Operand(esp, 0));
2529 GenerateOperation(masm);
2530 __ fstp_d(Operand(esp, 0));
2531 __ movdbl(xmm1, Operand(esp, 0));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002532 __ add(esp, Immediate(kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002533 // We return the value in xmm1 without adding it to the cache, but
2534 // we cause a scavenging GC so that future allocations will succeed.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002535 {
2536 FrameScope scope(masm, StackFrame::INTERNAL);
2537 // Allocate an unused object bigger than a HeapNumber.
2538 __ push(Immediate(Smi::FromInt(2 * kDoubleSize)));
2539 __ CallRuntimeSaveDoubles(Runtime::kAllocateInNewSpace);
2540 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002541 __ Ret();
2542 }
2543
2544 // Call runtime, doing whatever allocation and cleanup is necessary.
2545 if (tagged) {
2546 __ bind(&runtime_call_clear_stack);
2547 __ fstp(0);
2548 __ bind(&runtime_call);
Steve Block44f0eee2011-05-26 01:26:41 +01002549 ExternalReference runtime =
2550 ExternalReference(RuntimeFunction(), masm->isolate());
2551 __ TailCallExternalReference(runtime, 1, 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002552 } else { // UNTAGGED.
2553 __ bind(&runtime_call_clear_stack);
2554 __ bind(&runtime_call);
2555 __ AllocateHeapNumber(eax, edi, no_reg, &skip_cache);
2556 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), xmm1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002557 {
2558 FrameScope scope(masm, StackFrame::INTERNAL);
2559 __ push(eax);
2560 __ CallRuntime(RuntimeFunction(), 1);
2561 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01002562 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2563 __ Ret();
2564 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002565}
2566
2567
2568Runtime::FunctionId TranscendentalCacheStub::RuntimeFunction() {
2569 switch (type_) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002570 case TranscendentalCache::SIN: return Runtime::kMath_sin;
2571 case TranscendentalCache::COS: return Runtime::kMath_cos;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002572 case TranscendentalCache::TAN: return Runtime::kMath_tan;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002573 case TranscendentalCache::LOG: return Runtime::kMath_log;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002574 default:
2575 UNIMPLEMENTED();
2576 return Runtime::kAbort;
2577 }
2578}
2579
2580
2581void TranscendentalCacheStub::GenerateOperation(MacroAssembler* masm) {
2582 // Only free register is edi.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002583 // Input value is on FP stack, and also in ebx/edx.
2584 // Input value is possibly in xmm1.
2585 // Address of result (a newly allocated HeapNumber) may be in eax.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002586 if (type_ == TranscendentalCache::SIN ||
2587 type_ == TranscendentalCache::COS ||
2588 type_ == TranscendentalCache::TAN) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002589 // Both fsin and fcos require arguments in the range +/-2^63 and
2590 // return NaN for infinities and NaN. They can share all code except
2591 // the actual fsin/fcos operation.
Ben Murdoch257744e2011-11-30 15:57:28 +00002592 Label in_range, done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002593 // If argument is outside the range -2^63..2^63, fsin/cos doesn't
2594 // work. We must reduce it to the appropriate range.
2595 __ mov(edi, edx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002596 __ and_(edi, Immediate(0x7ff00000)); // Exponent only.
Ben Murdochb0fe1622011-05-05 13:52:32 +01002597 int supported_exponent_limit =
2598 (63 + HeapNumber::kExponentBias) << HeapNumber::kExponentShift;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002599 __ cmp(edi, Immediate(supported_exponent_limit));
Ben Murdoch257744e2011-11-30 15:57:28 +00002600 __ j(below, &in_range, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002601 // Check for infinity and NaN. Both return NaN for sin.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002602 __ cmp(edi, Immediate(0x7ff00000));
Ben Murdoch257744e2011-11-30 15:57:28 +00002603 Label non_nan_result;
2604 __ j(not_equal, &non_nan_result, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002605 // Input is +/-Infinity or NaN. Result is NaN.
2606 __ fstp(0);
2607 // NaN is represented by 0x7ff8000000000000.
2608 __ push(Immediate(0x7ff80000));
2609 __ push(Immediate(0));
2610 __ fld_d(Operand(esp, 0));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002611 __ add(esp, Immediate(2 * kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00002612 __ jmp(&done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002613
Ben Murdochb0fe1622011-05-05 13:52:32 +01002614 __ bind(&non_nan_result);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002615
Ben Murdochb0fe1622011-05-05 13:52:32 +01002616 // Use fpmod to restrict argument to the range +/-2*PI.
2617 __ mov(edi, eax); // Save eax before using fnstsw_ax.
2618 __ fldpi();
2619 __ fadd(0);
2620 __ fld(1);
2621 // FPU Stack: input, 2*pi, input.
2622 {
Ben Murdoch257744e2011-11-30 15:57:28 +00002623 Label no_exceptions;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002624 __ fwait();
2625 __ fnstsw_ax();
2626 // Clear if Illegal Operand or Zero Division exceptions are set.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002627 __ test(eax, Immediate(5));
Ben Murdoch257744e2011-11-30 15:57:28 +00002628 __ j(zero, &no_exceptions, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002629 __ fnclex();
2630 __ bind(&no_exceptions);
2631 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002632
Ben Murdochb0fe1622011-05-05 13:52:32 +01002633 // Compute st(0) % st(1)
2634 {
Ben Murdoch257744e2011-11-30 15:57:28 +00002635 Label partial_remainder_loop;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002636 __ bind(&partial_remainder_loop);
2637 __ fprem1();
2638 __ fwait();
2639 __ fnstsw_ax();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002640 __ test(eax, Immediate(0x400 /* C2 */));
Ben Murdochb0fe1622011-05-05 13:52:32 +01002641 // If C2 is set, computation only has partial result. Loop to
2642 // continue computation.
2643 __ j(not_zero, &partial_remainder_loop);
2644 }
2645 // FPU Stack: input, 2*pi, input % 2*pi
2646 __ fstp(2);
2647 __ fstp(0);
2648 __ mov(eax, edi); // Restore eax (allocated HeapNumber pointer).
2649
2650 // FPU Stack: input % 2*pi
2651 __ bind(&in_range);
2652 switch (type_) {
2653 case TranscendentalCache::SIN:
2654 __ fsin();
2655 break;
2656 case TranscendentalCache::COS:
2657 __ fcos();
2658 break;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002659 case TranscendentalCache::TAN:
2660 // FPTAN calculates tangent onto st(0) and pushes 1.0 onto the
2661 // FP register stack.
2662 __ fptan();
2663 __ fstp(0); // Pop FP register stack.
2664 break;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002665 default:
2666 UNREACHABLE();
2667 }
2668 __ bind(&done);
2669 } else {
2670 ASSERT(type_ == TranscendentalCache::LOG);
2671 __ fldln2();
2672 __ fxch();
2673 __ fyl2x();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002674 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002675}
2676
2677
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002678// Input: edx, eax are the left and right objects of a bit op.
2679// Output: eax, ecx are left and right integers for a bit op.
2680void FloatingPointHelper::LoadUnknownsAsIntegers(MacroAssembler* masm,
2681 bool use_sse3,
2682 Label* conversion_failure) {
2683 // Check float operands.
2684 Label arg1_is_object, check_undefined_arg1;
2685 Label arg2_is_object, check_undefined_arg2;
2686 Label load_arg2, done;
2687
2688 // Test if arg1 is a Smi.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002689 __ JumpIfNotSmi(edx, &arg1_is_object, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002690
2691 __ SmiUntag(edx);
2692 __ jmp(&load_arg2);
2693
2694 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2695 __ bind(&check_undefined_arg1);
Steve Block44f0eee2011-05-26 01:26:41 +01002696 Factory* factory = masm->isolate()->factory();
2697 __ cmp(edx, factory->undefined_value());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002698 __ j(not_equal, conversion_failure);
2699 __ mov(edx, Immediate(0));
2700 __ jmp(&load_arg2);
2701
2702 __ bind(&arg1_is_object);
2703 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002704 __ cmp(ebx, factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002705 __ j(not_equal, &check_undefined_arg1);
2706
2707 // Get the untagged integer version of the edx heap number in ecx.
Ben Murdoch257744e2011-11-30 15:57:28 +00002708 IntegerConvert(masm, edx, use_sse3, conversion_failure);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002709 __ mov(edx, ecx);
2710
2711 // Here edx has the untagged integer, eax has a Smi or a heap number.
2712 __ bind(&load_arg2);
2713
2714 // Test if arg2 is a Smi.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00002715 __ JumpIfNotSmi(eax, &arg2_is_object, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002716
2717 __ SmiUntag(eax);
2718 __ mov(ecx, eax);
2719 __ jmp(&done);
2720
2721 // If the argument is undefined it converts to zero (ECMA-262, section 9.5).
2722 __ bind(&check_undefined_arg2);
Steve Block44f0eee2011-05-26 01:26:41 +01002723 __ cmp(eax, factory->undefined_value());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002724 __ j(not_equal, conversion_failure);
2725 __ mov(ecx, Immediate(0));
2726 __ jmp(&done);
2727
2728 __ bind(&arg2_is_object);
2729 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002730 __ cmp(ebx, factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002731 __ j(not_equal, &check_undefined_arg2);
2732
2733 // Get the untagged integer version of the eax heap number in ecx.
Ben Murdoch257744e2011-11-30 15:57:28 +00002734 IntegerConvert(masm, eax, use_sse3, conversion_failure);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002735 __ bind(&done);
2736 __ mov(eax, edx);
2737}
2738
2739
Ben Murdochb0fe1622011-05-05 13:52:32 +01002740void FloatingPointHelper::CheckLoadedIntegersWereInt32(MacroAssembler* masm,
2741 bool use_sse3,
2742 Label* not_int32) {
2743 return;
2744}
2745
2746
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002747void FloatingPointHelper::LoadFloatOperand(MacroAssembler* masm,
2748 Register number) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002749 Label load_smi, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002750
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002751 __ JumpIfSmi(number, &load_smi, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002752 __ fld_d(FieldOperand(number, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002753 __ jmp(&done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002754
2755 __ bind(&load_smi);
2756 __ SmiUntag(number);
2757 __ push(number);
2758 __ fild_s(Operand(esp, 0));
2759 __ pop(number);
2760
2761 __ bind(&done);
2762}
2763
2764
2765void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002766 Label load_smi_edx, load_eax, load_smi_eax, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002767 // Load operand in edx into xmm0.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002768 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002769 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2770
2771 __ bind(&load_eax);
2772 // Load operand in eax into xmm1.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002773 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002774 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002775 __ jmp(&done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002776
2777 __ bind(&load_smi_edx);
2778 __ SmiUntag(edx); // Untag smi before converting to float.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002779 __ cvtsi2sd(xmm0, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002780 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2781 __ jmp(&load_eax);
2782
2783 __ bind(&load_smi_eax);
2784 __ SmiUntag(eax); // Untag smi before converting to float.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002785 __ cvtsi2sd(xmm1, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002786 __ SmiTag(eax); // Retag smi for heap number overwriting test.
2787
2788 __ bind(&done);
2789}
2790
2791
2792void FloatingPointHelper::LoadSSE2Operands(MacroAssembler* masm,
2793 Label* not_numbers) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002794 Label load_smi_edx, load_eax, load_smi_eax, load_float_eax, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002795 // Load operand in edx into xmm0, or branch to not_numbers.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002796 __ JumpIfSmi(edx, &load_smi_edx, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01002797 Factory* factory = masm->isolate()->factory();
2798 __ cmp(FieldOperand(edx, HeapObject::kMapOffset), factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002799 __ j(not_equal, not_numbers); // Argument in edx is not a number.
2800 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
2801 __ bind(&load_eax);
2802 // Load operand in eax into xmm1, or branch to not_numbers.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002803 __ JumpIfSmi(eax, &load_smi_eax, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01002804 __ cmp(FieldOperand(eax, HeapObject::kMapOffset), factory->heap_number_map());
Ben Murdoch257744e2011-11-30 15:57:28 +00002805 __ j(equal, &load_float_eax, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002806 __ jmp(not_numbers); // Argument in eax is not a number.
2807 __ bind(&load_smi_edx);
2808 __ SmiUntag(edx); // Untag smi before converting to float.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002809 __ cvtsi2sd(xmm0, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002810 __ SmiTag(edx); // Retag smi for heap number overwriting test.
2811 __ jmp(&load_eax);
2812 __ bind(&load_smi_eax);
2813 __ SmiUntag(eax); // Untag smi before converting to float.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002814 __ cvtsi2sd(xmm1, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002815 __ SmiTag(eax); // Retag smi for heap number overwriting test.
Ben Murdoch257744e2011-11-30 15:57:28 +00002816 __ jmp(&done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002817 __ bind(&load_float_eax);
2818 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
2819 __ bind(&done);
2820}
2821
2822
2823void FloatingPointHelper::LoadSSE2Smis(MacroAssembler* masm,
2824 Register scratch) {
2825 const Register left = edx;
2826 const Register right = eax;
2827 __ mov(scratch, left);
2828 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2829 __ SmiUntag(scratch);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002830 __ cvtsi2sd(xmm0, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002831
2832 __ mov(scratch, right);
2833 __ SmiUntag(scratch);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002834 __ cvtsi2sd(xmm1, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002835}
2836
2837
Ben Murdochb0fe1622011-05-05 13:52:32 +01002838void FloatingPointHelper::CheckSSE2OperandsAreInt32(MacroAssembler* masm,
2839 Label* non_int32,
2840 Register scratch) {
2841 __ cvttsd2si(scratch, Operand(xmm0));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002842 __ cvtsi2sd(xmm2, scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002843 __ ucomisd(xmm0, xmm2);
2844 __ j(not_zero, non_int32);
2845 __ j(carry, non_int32);
2846 __ cvttsd2si(scratch, Operand(xmm1));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00002847 __ cvtsi2sd(xmm2, scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002848 __ ucomisd(xmm1, xmm2);
2849 __ j(not_zero, non_int32);
2850 __ j(carry, non_int32);
2851}
2852
2853
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002854void FloatingPointHelper::LoadFloatOperands(MacroAssembler* masm,
2855 Register scratch,
2856 ArgLocation arg_location) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002857 Label load_smi_1, load_smi_2, done_load_1, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002858 if (arg_location == ARGS_IN_REGISTERS) {
2859 __ mov(scratch, edx);
2860 } else {
2861 __ mov(scratch, Operand(esp, 2 * kPointerSize));
2862 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002863 __ JumpIfSmi(scratch, &load_smi_1, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002864 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
2865 __ bind(&done_load_1);
2866
2867 if (arg_location == ARGS_IN_REGISTERS) {
2868 __ mov(scratch, eax);
2869 } else {
2870 __ mov(scratch, Operand(esp, 1 * kPointerSize));
2871 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002872 __ JumpIfSmi(scratch, &load_smi_2, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002873 __ fld_d(FieldOperand(scratch, HeapNumber::kValueOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00002874 __ jmp(&done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002875
2876 __ bind(&load_smi_1);
2877 __ SmiUntag(scratch);
2878 __ push(scratch);
2879 __ fild_s(Operand(esp, 0));
2880 __ pop(scratch);
2881 __ jmp(&done_load_1);
2882
2883 __ bind(&load_smi_2);
2884 __ SmiUntag(scratch);
2885 __ push(scratch);
2886 __ fild_s(Operand(esp, 0));
2887 __ pop(scratch);
2888
2889 __ bind(&done);
2890}
2891
2892
2893void FloatingPointHelper::LoadFloatSmis(MacroAssembler* masm,
2894 Register scratch) {
2895 const Register left = edx;
2896 const Register right = eax;
2897 __ mov(scratch, left);
2898 ASSERT(!scratch.is(right)); // We're about to clobber scratch.
2899 __ SmiUntag(scratch);
2900 __ push(scratch);
2901 __ fild_s(Operand(esp, 0));
2902
2903 __ mov(scratch, right);
2904 __ SmiUntag(scratch);
2905 __ mov(Operand(esp, 0), scratch);
2906 __ fild_s(Operand(esp, 0));
2907 __ pop(scratch);
2908}
2909
2910
2911void FloatingPointHelper::CheckFloatOperands(MacroAssembler* masm,
2912 Label* non_float,
2913 Register scratch) {
Ben Murdoch257744e2011-11-30 15:57:28 +00002914 Label test_other, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002915 // Test if both operands are floats or smi -> scratch=k_is_float;
2916 // Otherwise scratch = k_not_float.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002917 __ JumpIfSmi(edx, &test_other, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002918 __ mov(scratch, FieldOperand(edx, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002919 Factory* factory = masm->isolate()->factory();
2920 __ cmp(scratch, factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002921 __ j(not_equal, non_float); // argument in edx is not a number -> NaN
2922
2923 __ bind(&test_other);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00002924 __ JumpIfSmi(eax, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002925 __ mov(scratch, FieldOperand(eax, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01002926 __ cmp(scratch, factory->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01002927 __ j(not_equal, non_float); // argument in eax is not a number -> NaN
2928
2929 // Fall-through: Both operands are numbers.
2930 __ bind(&done);
2931}
2932
2933
Ben Murdochb0fe1622011-05-05 13:52:32 +01002934void FloatingPointHelper::CheckFloatOperandsAreInt32(MacroAssembler* masm,
2935 Label* non_int32) {
2936 return;
2937}
2938
2939
Ben Murdochb0fe1622011-05-05 13:52:32 +01002940void MathPowStub::Generate(MacroAssembler* masm) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01002941 CpuFeatures::Scope use_sse2(SSE2);
Steve Block44f0eee2011-05-26 01:26:41 +01002942 Factory* factory = masm->isolate()->factory();
Ben Murdochc7cc0282012-03-05 14:35:55 +00002943 const Register exponent = eax;
2944 const Register base = edx;
2945 const Register scratch = ecx;
2946 const XMMRegister double_result = xmm3;
2947 const XMMRegister double_base = xmm2;
2948 const XMMRegister double_exponent = xmm1;
2949 const XMMRegister double_scratch = xmm4;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002950
Ben Murdochc7cc0282012-03-05 14:35:55 +00002951 Label call_runtime, done, exponent_not_smi, int_exponent;
Ben Murdochb0fe1622011-05-05 13:52:32 +01002952
Ben Murdochc7cc0282012-03-05 14:35:55 +00002953 // Save 1 in double_result - we need this several times later on.
2954 __ mov(scratch, Immediate(1));
2955 __ cvtsi2sd(double_result, scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01002956
Ben Murdochc7cc0282012-03-05 14:35:55 +00002957 if (exponent_type_ == ON_STACK) {
2958 Label base_is_smi, unpack_exponent;
2959 // The exponent and base are supplied as arguments on the stack.
2960 // This can only happen if the stub is called from non-optimized code.
2961 // Load input parameters from stack.
2962 __ mov(base, Operand(esp, 2 * kPointerSize));
2963 __ mov(exponent, Operand(esp, 1 * kPointerSize));
2964
2965 __ JumpIfSmi(base, &base_is_smi, Label::kNear);
2966 __ cmp(FieldOperand(base, HeapObject::kMapOffset),
2967 factory->heap_number_map());
2968 __ j(not_equal, &call_runtime);
2969
2970 __ movdbl(double_base, FieldOperand(base, HeapNumber::kValueOffset));
2971 __ jmp(&unpack_exponent, Label::kNear);
2972
2973 __ bind(&base_is_smi);
2974 __ SmiUntag(base);
2975 __ cvtsi2sd(double_base, base);
2976
2977 __ bind(&unpack_exponent);
2978 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
2979 __ SmiUntag(exponent);
2980 __ jmp(&int_exponent);
2981
2982 __ bind(&exponent_not_smi);
2983 __ cmp(FieldOperand(exponent, HeapObject::kMapOffset),
2984 factory->heap_number_map());
2985 __ j(not_equal, &call_runtime);
2986 __ movdbl(double_exponent,
2987 FieldOperand(exponent, HeapNumber::kValueOffset));
2988 } else if (exponent_type_ == TAGGED) {
2989 __ JumpIfNotSmi(exponent, &exponent_not_smi, Label::kNear);
2990 __ SmiUntag(exponent);
2991 __ jmp(&int_exponent);
2992
2993 __ bind(&exponent_not_smi);
2994 __ movdbl(double_exponent,
2995 FieldOperand(exponent, HeapNumber::kValueOffset));
2996 }
2997
2998 if (exponent_type_ != INTEGER) {
2999 Label fast_power;
3000 // Detect integer exponents stored as double.
3001 __ cvttsd2si(exponent, Operand(double_exponent));
3002 // Skip to runtime if possibly NaN (indicated by the indefinite integer).
3003 __ cmp(exponent, Immediate(0x80000000u));
3004 __ j(equal, &call_runtime);
3005 __ cvtsi2sd(double_scratch, exponent);
3006 // Already ruled out NaNs for exponent.
3007 __ ucomisd(double_exponent, double_scratch);
3008 __ j(equal, &int_exponent);
3009
3010 if (exponent_type_ == ON_STACK) {
3011 // Detect square root case. Crankshaft detects constant +/-0.5 at
3012 // compile time and uses DoMathPowHalf instead. We then skip this check
3013 // for non-constant cases of +/-0.5 as these hardly occur.
3014 Label continue_sqrt, continue_rsqrt, not_plus_half;
3015 // Test for 0.5.
3016 // Load double_scratch with 0.5.
3017 __ mov(scratch, Immediate(0x3F000000u));
3018 __ movd(double_scratch, scratch);
3019 __ cvtss2sd(double_scratch, double_scratch);
3020 // Already ruled out NaNs for exponent.
3021 __ ucomisd(double_scratch, double_exponent);
3022 __ j(not_equal, &not_plus_half, Label::kNear);
3023
3024 // Calculates square root of base. Check for the special case of
3025 // Math.pow(-Infinity, 0.5) == Infinity (ECMA spec, 15.8.2.13).
3026 // According to IEEE-754, single-precision -Infinity has the highest
3027 // 9 bits set and the lowest 23 bits cleared.
3028 __ mov(scratch, 0xFF800000u);
3029 __ movd(double_scratch, scratch);
3030 __ cvtss2sd(double_scratch, double_scratch);
3031 __ ucomisd(double_base, double_scratch);
3032 // Comparing -Infinity with NaN results in "unordered", which sets the
3033 // zero flag as if both were equal. However, it also sets the carry flag.
3034 __ j(not_equal, &continue_sqrt, Label::kNear);
3035 __ j(carry, &continue_sqrt, Label::kNear);
3036
3037 // Set result to Infinity in the special case.
3038 __ xorps(double_result, double_result);
3039 __ subsd(double_result, double_scratch);
3040 __ jmp(&done);
3041
3042 __ bind(&continue_sqrt);
3043 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
3044 __ xorps(double_scratch, double_scratch);
3045 __ addsd(double_scratch, double_base); // Convert -0 to +0.
3046 __ sqrtsd(double_result, double_scratch);
3047 __ jmp(&done);
3048
3049 // Test for -0.5.
3050 __ bind(&not_plus_half);
3051 // Load double_exponent with -0.5 by substracting 1.
3052 __ subsd(double_scratch, double_result);
3053 // Already ruled out NaNs for exponent.
3054 __ ucomisd(double_scratch, double_exponent);
3055 __ j(not_equal, &fast_power, Label::kNear);
3056
3057 // Calculates reciprocal of square root of base. Check for the special
3058 // case of Math.pow(-Infinity, -0.5) == 0 (ECMA spec, 15.8.2.13).
3059 // According to IEEE-754, single-precision -Infinity has the highest
3060 // 9 bits set and the lowest 23 bits cleared.
3061 __ mov(scratch, 0xFF800000u);
3062 __ movd(double_scratch, scratch);
3063 __ cvtss2sd(double_scratch, double_scratch);
3064 __ ucomisd(double_base, double_scratch);
3065 // Comparing -Infinity with NaN results in "unordered", which sets the
3066 // zero flag as if both were equal. However, it also sets the carry flag.
3067 __ j(not_equal, &continue_rsqrt, Label::kNear);
3068 __ j(carry, &continue_rsqrt, Label::kNear);
3069
3070 // Set result to 0 in the special case.
3071 __ xorps(double_result, double_result);
3072 __ jmp(&done);
3073
3074 __ bind(&continue_rsqrt);
3075 // sqrtsd returns -0 when input is -0. ECMA spec requires +0.
3076 __ xorps(double_exponent, double_exponent);
3077 __ addsd(double_exponent, double_base); // Convert -0 to +0.
3078 __ sqrtsd(double_exponent, double_exponent);
3079 __ divsd(double_result, double_exponent);
3080 __ jmp(&done);
3081 }
3082
3083 // Using FPU instructions to calculate power.
3084 Label fast_power_failed;
3085 __ bind(&fast_power);
3086 __ fnclex(); // Clear flags to catch exceptions later.
3087 // Transfer (B)ase and (E)xponent onto the FPU register stack.
3088 __ sub(esp, Immediate(kDoubleSize));
3089 __ movdbl(Operand(esp, 0), double_exponent);
3090 __ fld_d(Operand(esp, 0)); // E
3091 __ movdbl(Operand(esp, 0), double_base);
3092 __ fld_d(Operand(esp, 0)); // B, E
3093
3094 // Exponent is in st(1) and base is in st(0)
3095 // B ^ E = (2^(E * log2(B)) - 1) + 1 = (2^X - 1) + 1 for X = E * log2(B)
3096 // FYL2X calculates st(1) * log2(st(0))
3097 __ fyl2x(); // X
3098 __ fld(0); // X, X
3099 __ frndint(); // rnd(X), X
3100 __ fsub(1); // rnd(X), X-rnd(X)
3101 __ fxch(1); // X - rnd(X), rnd(X)
3102 // F2XM1 calculates 2^st(0) - 1 for -1 < st(0) < 1
3103 __ f2xm1(); // 2^(X-rnd(X)) - 1, rnd(X)
3104 __ fld1(); // 1, 2^(X-rnd(X)) - 1, rnd(X)
3105 __ faddp(1); // 1, 2^(X-rnd(X)), rnd(X)
3106 // FSCALE calculates st(0) * 2^st(1)
3107 __ fscale(); // 2^X, rnd(X)
3108 __ fstp(1);
3109 // Bail out to runtime in case of exceptions in the status word.
3110 __ fnstsw_ax();
3111 __ test_b(eax, 0x5F); // We check for all but precision exception.
3112 __ j(not_zero, &fast_power_failed, Label::kNear);
3113 __ fstp_d(Operand(esp, 0));
3114 __ movdbl(double_result, Operand(esp, 0));
3115 __ add(esp, Immediate(kDoubleSize));
3116 __ jmp(&done);
3117
3118 __ bind(&fast_power_failed);
3119 __ fninit();
3120 __ add(esp, Immediate(kDoubleSize));
3121 __ jmp(&call_runtime);
3122 }
3123
3124 // Calculate power with integer exponent.
3125 __ bind(&int_exponent);
3126 const XMMRegister double_scratch2 = double_exponent;
3127 __ mov(scratch, exponent); // Back up exponent.
3128 __ movsd(double_scratch, double_base); // Back up base.
3129 __ movsd(double_scratch2, double_result); // Load double_exponent with 1.
Ben Murdochb0fe1622011-05-05 13:52:32 +01003130
3131 // Get absolute value of exponent.
Ben Murdochc7cc0282012-03-05 14:35:55 +00003132 Label no_neg, while_true, no_multiply;
3133 __ test(scratch, scratch);
3134 __ j(positive, &no_neg, Label::kNear);
3135 __ neg(scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003136 __ bind(&no_neg);
3137
Ben Murdochb0fe1622011-05-05 13:52:32 +01003138 __ bind(&while_true);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003139 __ shr(scratch, 1);
Ben Murdoch257744e2011-11-30 15:57:28 +00003140 __ j(not_carry, &no_multiply, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003141 __ mulsd(double_result, double_scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003142 __ bind(&no_multiply);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003143
3144 __ mulsd(double_scratch, double_scratch);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003145 __ j(not_zero, &while_true);
3146
Ben Murdochc7cc0282012-03-05 14:35:55 +00003147 // scratch has the original value of the exponent - if the exponent is
3148 // negative, return 1/result.
3149 __ test(exponent, exponent);
3150 __ j(positive, &done);
3151 __ divsd(double_scratch2, double_result);
3152 __ movsd(double_result, double_scratch2);
3153 // Test whether result is zero. Bail out to check for subnormal result.
3154 // Due to subnormals, x^-y == (1/x)^y does not hold in all cases.
3155 __ xorps(double_scratch2, double_scratch2);
3156 __ ucomisd(double_scratch2, double_result); // Result cannot be NaN.
3157 // double_exponent aliased as double_scratch2 has already been overwritten
3158 // and may not have contained the exponent value in the first place when the
3159 // exponent is a smi. We reset it with exponent value before bailing out.
3160 __ j(not_equal, &done);
3161 __ cvtsi2sd(double_exponent, exponent);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003162
Ben Murdochc7cc0282012-03-05 14:35:55 +00003163 // Returning or bailing out.
3164 Counters* counters = masm->isolate()->counters();
3165 if (exponent_type_ == ON_STACK) {
3166 // The arguments are still on the stack.
3167 __ bind(&call_runtime);
3168 __ TailCallRuntime(Runtime::kMath_pow_cfunction, 2, 1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01003169
Ben Murdochc7cc0282012-03-05 14:35:55 +00003170 // The stub is called from non-optimized code, which expects the result
3171 // as heap number in exponent.
3172 __ bind(&done);
3173 __ AllocateHeapNumber(eax, scratch, base, &call_runtime);
3174 __ movdbl(FieldOperand(eax, HeapNumber::kValueOffset), double_result);
3175 __ IncrementCounter(counters->math_pow(), 1);
3176 __ ret(2 * kPointerSize);
3177 } else {
3178 __ bind(&call_runtime);
3179 {
3180 AllowExternalCallThatCantCauseGC scope(masm);
3181 __ PrepareCallCFunction(4, scratch);
3182 __ movdbl(Operand(esp, 0 * kDoubleSize), double_base);
3183 __ movdbl(Operand(esp, 1 * kDoubleSize), double_exponent);
3184 __ CallCFunction(
3185 ExternalReference::power_double_double_function(masm->isolate()), 4);
3186 }
3187 // Return value is in st(0) on ia32.
3188 // Store it into the (fixed) result register.
3189 __ sub(esp, Immediate(kDoubleSize));
3190 __ fstp_d(Operand(esp, 0));
3191 __ movdbl(double_result, Operand(esp, 0));
3192 __ add(esp, Immediate(kDoubleSize));
Ben Murdochb0fe1622011-05-05 13:52:32 +01003193
Ben Murdochc7cc0282012-03-05 14:35:55 +00003194 __ bind(&done);
3195 __ IncrementCounter(counters->math_pow(), 1);
3196 __ ret(0);
3197 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01003198}
3199
3200
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003201void ArgumentsAccessStub::GenerateReadElement(MacroAssembler* masm) {
3202 // The key is in edx and the parameter count is in eax.
3203
3204 // The displacement is used for skipping the frame pointer on the
3205 // stack. It is the offset of the last parameter (if any) relative
3206 // to the frame pointer.
3207 static const int kDisplacement = 1 * kPointerSize;
3208
3209 // Check that the key is a smi.
3210 Label slow;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003211 __ JumpIfNotSmi(edx, &slow, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003212
3213 // Check if the calling frame is an arguments adaptor frame.
Ben Murdoch257744e2011-11-30 15:57:28 +00003214 Label adaptor;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003215 __ mov(ebx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3216 __ mov(ecx, Operand(ebx, StandardFrameConstants::kContextOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003217 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdoch257744e2011-11-30 15:57:28 +00003218 __ j(equal, &adaptor, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003219
3220 // Check index against formal parameters count limit passed in
3221 // through register eax. Use unsigned comparison to get negative
3222 // check for free.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003223 __ cmp(edx, eax);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003224 __ j(above_equal, &slow, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003225
3226 // Read the argument from the stack and return it.
3227 STATIC_ASSERT(kSmiTagSize == 1);
3228 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
3229 __ lea(ebx, Operand(ebp, eax, times_2, 0));
3230 __ neg(edx);
3231 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
3232 __ ret(0);
3233
3234 // Arguments adaptor case: Check index against actual arguments
3235 // limit found in the arguments adaptor frame. Use unsigned
3236 // comparison to get negative check for free.
3237 __ bind(&adaptor);
3238 __ mov(ecx, Operand(ebx, ArgumentsAdaptorFrameConstants::kLengthOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003239 __ cmp(edx, ecx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003240 __ j(above_equal, &slow, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003241
3242 // Read the argument from the stack and return it.
3243 STATIC_ASSERT(kSmiTagSize == 1);
3244 STATIC_ASSERT(kSmiTag == 0); // Shifting code depends on these.
3245 __ lea(ebx, Operand(ebx, ecx, times_2, 0));
3246 __ neg(edx);
3247 __ mov(eax, Operand(ebx, edx, times_2, kDisplacement));
3248 __ ret(0);
3249
3250 // Slow-case: Handle non-smi or out-of-bounds access to arguments
3251 // by calling the runtime system.
3252 __ bind(&slow);
3253 __ pop(ebx); // Return address.
3254 __ push(edx);
3255 __ push(ebx);
3256 __ TailCallRuntime(Runtime::kGetArgumentsProperty, 1, 1);
3257}
3258
3259
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003260void ArgumentsAccessStub::GenerateNewNonStrictSlow(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003261 // esp[0] : return address
3262 // esp[4] : number of parameters
3263 // esp[8] : receiver displacement
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003264 // esp[12] : function
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003265
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003266 // Check if the calling frame is an arguments adaptor frame.
3267 Label runtime;
3268 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3269 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003270 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003271 __ j(not_equal, &runtime, Label::kNear);
3272
3273 // Patch the arguments.length and the parameters pointer.
3274 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3275 __ mov(Operand(esp, 1 * kPointerSize), ecx);
3276 __ lea(edx, Operand(edx, ecx, times_2,
3277 StandardFrameConstants::kCallerSPOffset));
3278 __ mov(Operand(esp, 2 * kPointerSize), edx);
3279
3280 __ bind(&runtime);
3281 __ TailCallRuntime(Runtime::kNewArgumentsFast, 3, 1);
3282}
3283
3284
3285void ArgumentsAccessStub::GenerateNewNonStrictFast(MacroAssembler* masm) {
3286 // esp[0] : return address
3287 // esp[4] : number of parameters (tagged)
3288 // esp[8] : receiver displacement
3289 // esp[12] : function
3290
3291 // ebx = parameter count (tagged)
3292 __ mov(ebx, Operand(esp, 1 * kPointerSize));
3293
3294 // Check if the calling frame is an arguments adaptor frame.
3295 // TODO(rossberg): Factor out some of the bits that are shared with the other
3296 // Generate* functions.
3297 Label runtime;
3298 Label adaptor_frame, try_allocate;
3299 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3300 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003301 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003302 __ j(equal, &adaptor_frame, Label::kNear);
3303
3304 // No adaptor, parameter count = argument count.
3305 __ mov(ecx, ebx);
3306 __ jmp(&try_allocate, Label::kNear);
3307
3308 // We have an adaptor frame. Patch the parameters pointer.
3309 __ bind(&adaptor_frame);
3310 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3311 __ lea(edx, Operand(edx, ecx, times_2,
3312 StandardFrameConstants::kCallerSPOffset));
3313 __ mov(Operand(esp, 2 * kPointerSize), edx);
3314
3315 // ebx = parameter count (tagged)
3316 // ecx = argument count (tagged)
3317 // esp[4] = parameter count (tagged)
3318 // esp[8] = address of receiver argument
3319 // Compute the mapped parameter count = min(ebx, ecx) in ebx.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003320 __ cmp(ebx, ecx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003321 __ j(less_equal, &try_allocate, Label::kNear);
3322 __ mov(ebx, ecx);
3323
3324 __ bind(&try_allocate);
3325
3326 // Save mapped parameter count.
3327 __ push(ebx);
3328
3329 // Compute the sizes of backing store, parameter map, and arguments object.
3330 // 1. Parameter map, has 2 extra words containing context and backing store.
3331 const int kParameterMapHeaderSize =
3332 FixedArray::kHeaderSize + 2 * kPointerSize;
3333 Label no_parameter_map;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003334 __ test(ebx, ebx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003335 __ j(zero, &no_parameter_map, Label::kNear);
3336 __ lea(ebx, Operand(ebx, times_2, kParameterMapHeaderSize));
3337 __ bind(&no_parameter_map);
3338
3339 // 2. Backing store.
3340 __ lea(ebx, Operand(ebx, ecx, times_2, FixedArray::kHeaderSize));
3341
3342 // 3. Arguments object.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003343 __ add(ebx, Immediate(Heap::kArgumentsObjectSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003344
3345 // Do the allocation of all three objects in one go.
3346 __ AllocateInNewSpace(ebx, eax, edx, edi, &runtime, TAG_OBJECT);
3347
3348 // eax = address of new object(s) (tagged)
3349 // ecx = argument count (tagged)
3350 // esp[0] = mapped parameter count (tagged)
3351 // esp[8] = parameter count (tagged)
3352 // esp[12] = address of receiver argument
3353 // Get the arguments boilerplate from the current (global) context into edi.
3354 Label has_mapped_parameters, copy;
3355 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3356 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
3357 __ mov(ebx, Operand(esp, 0 * kPointerSize));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003358 __ test(ebx, ebx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003359 __ j(not_zero, &has_mapped_parameters, Label::kNear);
3360 __ mov(edi, Operand(edi,
3361 Context::SlotOffset(Context::ARGUMENTS_BOILERPLATE_INDEX)));
3362 __ jmp(&copy, Label::kNear);
3363
3364 __ bind(&has_mapped_parameters);
3365 __ mov(edi, Operand(edi,
3366 Context::SlotOffset(Context::ALIASED_ARGUMENTS_BOILERPLATE_INDEX)));
3367 __ bind(&copy);
3368
3369 // eax = address of new object (tagged)
3370 // ebx = mapped parameter count (tagged)
3371 // ecx = argument count (tagged)
3372 // edi = address of boilerplate object (tagged)
3373 // esp[0] = mapped parameter count (tagged)
3374 // esp[8] = parameter count (tagged)
3375 // esp[12] = address of receiver argument
3376 // Copy the JS object part.
3377 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3378 __ mov(edx, FieldOperand(edi, i));
3379 __ mov(FieldOperand(eax, i), edx);
3380 }
3381
Ben Murdochc7cc0282012-03-05 14:35:55 +00003382 // Set up the callee in-object property.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003383 STATIC_ASSERT(Heap::kArgumentsCalleeIndex == 1);
3384 __ mov(edx, Operand(esp, 4 * kPointerSize));
3385 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3386 Heap::kArgumentsCalleeIndex * kPointerSize),
3387 edx);
3388
3389 // Use the length (smi tagged) and set that as an in-object property too.
3390 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
3391 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
3392 Heap::kArgumentsLengthIndex * kPointerSize),
3393 ecx);
3394
Ben Murdochc7cc0282012-03-05 14:35:55 +00003395 // Set up the elements pointer in the allocated arguments object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003396 // If we allocated a parameter map, edi will point there, otherwise to the
3397 // backing store.
3398 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSize));
3399 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3400
3401 // eax = address of new object (tagged)
3402 // ebx = mapped parameter count (tagged)
3403 // ecx = argument count (tagged)
3404 // edi = address of parameter map or backing store (tagged)
3405 // esp[0] = mapped parameter count (tagged)
3406 // esp[8] = parameter count (tagged)
3407 // esp[12] = address of receiver argument
3408 // Free a register.
3409 __ push(eax);
3410
3411 // Initialize parameter map. If there are no mapped arguments, we're done.
3412 Label skip_parameter_map;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003413 __ test(ebx, ebx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003414 __ j(zero, &skip_parameter_map);
3415
3416 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
3417 Immediate(FACTORY->non_strict_arguments_elements_map()));
3418 __ lea(eax, Operand(ebx, reinterpret_cast<intptr_t>(Smi::FromInt(2))));
3419 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), eax);
3420 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 0 * kPointerSize), esi);
3421 __ lea(eax, Operand(edi, ebx, times_2, kParameterMapHeaderSize));
3422 __ mov(FieldOperand(edi, FixedArray::kHeaderSize + 1 * kPointerSize), eax);
3423
3424 // Copy the parameter slots and the holes in the arguments.
3425 // We need to fill in mapped_parameter_count slots. They index the context,
3426 // where parameters are stored in reverse order, at
3427 // MIN_CONTEXT_SLOTS .. MIN_CONTEXT_SLOTS+parameter_count-1
3428 // The mapped parameter thus need to get indices
3429 // MIN_CONTEXT_SLOTS+parameter_count-1 ..
3430 // MIN_CONTEXT_SLOTS+parameter_count-mapped_parameter_count
3431 // We loop from right to left.
3432 Label parameters_loop, parameters_test;
3433 __ push(ecx);
3434 __ mov(eax, Operand(esp, 2 * kPointerSize));
3435 __ mov(ebx, Immediate(Smi::FromInt(Context::MIN_CONTEXT_SLOTS)));
3436 __ add(ebx, Operand(esp, 4 * kPointerSize));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003437 __ sub(ebx, eax);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003438 __ mov(ecx, FACTORY->the_hole_value());
3439 __ mov(edx, edi);
3440 __ lea(edi, Operand(edi, eax, times_2, kParameterMapHeaderSize));
3441 // eax = loop variable (tagged)
3442 // ebx = mapping index (tagged)
3443 // ecx = the hole value
3444 // edx = address of parameter map (tagged)
3445 // edi = address of backing store (tagged)
3446 // esp[0] = argument count (tagged)
3447 // esp[4] = address of new object (tagged)
3448 // esp[8] = mapped parameter count (tagged)
3449 // esp[16] = parameter count (tagged)
3450 // esp[20] = address of receiver argument
3451 __ jmp(&parameters_test, Label::kNear);
3452
3453 __ bind(&parameters_loop);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003454 __ sub(eax, Immediate(Smi::FromInt(1)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003455 __ mov(FieldOperand(edx, eax, times_2, kParameterMapHeaderSize), ebx);
3456 __ mov(FieldOperand(edi, eax, times_2, FixedArray::kHeaderSize), ecx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003457 __ add(ebx, Immediate(Smi::FromInt(1)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003458 __ bind(&parameters_test);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003459 __ test(eax, eax);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003460 __ j(not_zero, &parameters_loop, Label::kNear);
3461 __ pop(ecx);
3462
3463 __ bind(&skip_parameter_map);
3464
3465 // ecx = argument count (tagged)
3466 // edi = address of backing store (tagged)
3467 // esp[0] = address of new object (tagged)
3468 // esp[4] = mapped parameter count (tagged)
3469 // esp[12] = parameter count (tagged)
3470 // esp[16] = address of receiver argument
3471 // Copy arguments header and remaining slots (if there are any).
3472 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
3473 Immediate(FACTORY->fixed_array_map()));
3474 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3475
3476 Label arguments_loop, arguments_test;
3477 __ mov(ebx, Operand(esp, 1 * kPointerSize));
3478 __ mov(edx, Operand(esp, 4 * kPointerSize));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003479 __ sub(edx, ebx); // Is there a smarter way to do negative scaling?
3480 __ sub(edx, ebx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003481 __ jmp(&arguments_test, Label::kNear);
3482
3483 __ bind(&arguments_loop);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003484 __ sub(edx, Immediate(kPointerSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003485 __ mov(eax, Operand(edx, 0));
3486 __ mov(FieldOperand(edi, ebx, times_2, FixedArray::kHeaderSize), eax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003487 __ add(ebx, Immediate(Smi::FromInt(1)));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003488
3489 __ bind(&arguments_test);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003490 __ cmp(ebx, ecx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003491 __ j(less, &arguments_loop, Label::kNear);
3492
3493 // Restore.
3494 __ pop(eax); // Address of arguments object.
3495 __ pop(ebx); // Parameter count.
3496
3497 // Return and remove the on-stack parameters.
3498 __ ret(3 * kPointerSize);
3499
3500 // Do the runtime call to allocate the arguments object.
3501 __ bind(&runtime);
3502 __ pop(eax); // Remove saved parameter count.
3503 __ mov(Operand(esp, 1 * kPointerSize), ecx); // Patch argument count.
3504 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
3505}
3506
3507
3508void ArgumentsAccessStub::GenerateNewStrict(MacroAssembler* masm) {
3509 // esp[0] : return address
3510 // esp[4] : number of parameters
3511 // esp[8] : receiver displacement
3512 // esp[12] : function
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003513
3514 // Check if the calling frame is an arguments adaptor frame.
3515 Label adaptor_frame, try_allocate, runtime;
3516 __ mov(edx, Operand(ebp, StandardFrameConstants::kCallerFPOffset));
3517 __ mov(ecx, Operand(edx, StandardFrameConstants::kContextOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003518 __ cmp(ecx, Immediate(Smi::FromInt(StackFrame::ARGUMENTS_ADAPTOR)));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003519 __ j(equal, &adaptor_frame, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003520
3521 // Get the length from the frame.
3522 __ mov(ecx, Operand(esp, 1 * kPointerSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003523 __ jmp(&try_allocate, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003524
3525 // Patch the arguments.length and the parameters pointer.
3526 __ bind(&adaptor_frame);
3527 __ mov(ecx, Operand(edx, ArgumentsAdaptorFrameConstants::kLengthOffset));
3528 __ mov(Operand(esp, 1 * kPointerSize), ecx);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003529 __ lea(edx, Operand(edx, ecx, times_2,
3530 StandardFrameConstants::kCallerSPOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003531 __ mov(Operand(esp, 2 * kPointerSize), edx);
3532
3533 // Try the new space allocation. Start out with computing the size of
3534 // the arguments object and the elements array.
Ben Murdoch257744e2011-11-30 15:57:28 +00003535 Label add_arguments_object;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003536 __ bind(&try_allocate);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003537 __ test(ecx, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003538 __ j(zero, &add_arguments_object, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003539 __ lea(ecx, Operand(ecx, times_2, FixedArray::kHeaderSize));
3540 __ bind(&add_arguments_object);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003541 __ add(ecx, Immediate(Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003542
3543 // Do the allocation of both objects in one go.
3544 __ AllocateInNewSpace(ecx, eax, edx, ebx, &runtime, TAG_OBJECT);
3545
3546 // Get the arguments boilerplate from the current (global) context.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003547 __ mov(edi, Operand(esi, Context::SlotOffset(Context::GLOBAL_INDEX)));
3548 __ mov(edi, FieldOperand(edi, GlobalObject::kGlobalContextOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003549 const int offset =
3550 Context::SlotOffset(Context::STRICT_MODE_ARGUMENTS_BOILERPLATE_INDEX);
3551 __ mov(edi, Operand(edi, offset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003552
3553 // Copy the JS object part.
3554 for (int i = 0; i < JSObject::kHeaderSize; i += kPointerSize) {
3555 __ mov(ebx, FieldOperand(edi, i));
3556 __ mov(FieldOperand(eax, i), ebx);
3557 }
3558
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003559 // Get the length (smi tagged) and set that as an in-object property too.
Steve Block44f0eee2011-05-26 01:26:41 +01003560 STATIC_ASSERT(Heap::kArgumentsLengthIndex == 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003561 __ mov(ecx, Operand(esp, 1 * kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01003562 __ mov(FieldOperand(eax, JSObject::kHeaderSize +
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003563 Heap::kArgumentsLengthIndex * kPointerSize),
Steve Block44f0eee2011-05-26 01:26:41 +01003564 ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003565
3566 // If there are no actual arguments, we're done.
3567 Label done;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003568 __ test(ecx, ecx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003569 __ j(zero, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003570
3571 // Get the parameters pointer from the stack.
3572 __ mov(edx, Operand(esp, 2 * kPointerSize));
3573
Ben Murdochc7cc0282012-03-05 14:35:55 +00003574 // Set up the elements pointer in the allocated arguments object and
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003575 // initialize the header in the elements fixed array.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003576 __ lea(edi, Operand(eax, Heap::kArgumentsObjectSizeStrict));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003577 __ mov(FieldOperand(eax, JSObject::kElementsOffset), edi);
3578 __ mov(FieldOperand(edi, FixedArray::kMapOffset),
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003579 Immediate(FACTORY->fixed_array_map()));
Steve Block44f0eee2011-05-26 01:26:41 +01003580
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003581 __ mov(FieldOperand(edi, FixedArray::kLengthOffset), ecx);
3582 // Untag the length for the loop below.
3583 __ SmiUntag(ecx);
3584
3585 // Copy the fixed array slots.
Ben Murdoch257744e2011-11-30 15:57:28 +00003586 Label loop;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003587 __ bind(&loop);
3588 __ mov(ebx, Operand(edx, -1 * kPointerSize)); // Skip receiver.
3589 __ mov(FieldOperand(edi, FixedArray::kHeaderSize), ebx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003590 __ add(edi, Immediate(kPointerSize));
3591 __ sub(edx, Immediate(kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003592 __ dec(ecx);
3593 __ j(not_zero, &loop);
3594
3595 // Return and remove the on-stack parameters.
3596 __ bind(&done);
3597 __ ret(3 * kPointerSize);
3598
3599 // Do the runtime call to allocate the arguments object.
3600 __ bind(&runtime);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003601 __ TailCallRuntime(Runtime::kNewStrictArgumentsFast, 3, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003602}
3603
3604
3605void RegExpExecStub::Generate(MacroAssembler* masm) {
3606 // Just jump directly to runtime if native RegExp is not selected at compile
3607 // time or if regexp entry in generated code is turned off runtime switch or
3608 // at compilation.
3609#ifdef V8_INTERPRETED_REGEXP
3610 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
3611#else // V8_INTERPRETED_REGEXP
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003612
3613 // Stack frame on entry.
3614 // esp[0]: return address
3615 // esp[4]: last_match_info (expected JSArray)
3616 // esp[8]: previous index
3617 // esp[12]: subject string
3618 // esp[16]: JSRegExp object
3619
3620 static const int kLastMatchInfoOffset = 1 * kPointerSize;
3621 static const int kPreviousIndexOffset = 2 * kPointerSize;
3622 static const int kSubjectOffset = 3 * kPointerSize;
3623 static const int kJSRegExpOffset = 4 * kPointerSize;
3624
3625 Label runtime, invoke_regexp;
3626
3627 // Ensure that a RegExp stack is allocated.
3628 ExternalReference address_of_regexp_stack_memory_address =
Steve Block44f0eee2011-05-26 01:26:41 +01003629 ExternalReference::address_of_regexp_stack_memory_address(
3630 masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003631 ExternalReference address_of_regexp_stack_memory_size =
Steve Block44f0eee2011-05-26 01:26:41 +01003632 ExternalReference::address_of_regexp_stack_memory_size(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003633 __ mov(ebx, Operand::StaticVariable(address_of_regexp_stack_memory_size));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003634 __ test(ebx, ebx);
Ben Murdoch257744e2011-11-30 15:57:28 +00003635 __ j(zero, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003636
3637 // Check that the first argument is a JSRegExp object.
3638 __ mov(eax, Operand(esp, kJSRegExpOffset));
3639 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003640 __ JumpIfSmi(eax, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003641 __ CmpObjectType(eax, JS_REGEXP_TYPE, ecx);
3642 __ j(not_equal, &runtime);
3643 // Check that the RegExp has been compiled (data contains a fixed array).
3644 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3645 if (FLAG_debug_code) {
3646 __ test(ecx, Immediate(kSmiTagMask));
3647 __ Check(not_zero, "Unexpected type for RegExp data, FixedArray expected");
3648 __ CmpObjectType(ecx, FIXED_ARRAY_TYPE, ebx);
3649 __ Check(equal, "Unexpected type for RegExp data, FixedArray expected");
3650 }
3651
3652 // ecx: RegExp data (FixedArray)
3653 // Check the type of the RegExp. Only continue if type is JSRegExp::IRREGEXP.
3654 __ mov(ebx, FieldOperand(ecx, JSRegExp::kDataTagOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003655 __ cmp(ebx, Immediate(Smi::FromInt(JSRegExp::IRREGEXP)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003656 __ j(not_equal, &runtime);
3657
3658 // ecx: RegExp data (FixedArray)
3659 // Check that the number of captures fit in the static offsets vector buffer.
3660 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3661 // Calculate number of capture registers (number_of_captures + 1) * 2. This
3662 // uses the asumption that smis are 2 * their untagged value.
3663 STATIC_ASSERT(kSmiTag == 0);
3664 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003665 __ add(edx, Immediate(2)); // edx was a smi.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003666 // Check that the static offsets vector buffer is large enough.
3667 __ cmp(edx, OffsetsVector::kStaticOffsetsVectorSize);
3668 __ j(above, &runtime);
3669
3670 // ecx: RegExp data (FixedArray)
3671 // edx: Number of capture registers
3672 // Check that the second argument is a string.
3673 __ mov(eax, Operand(esp, kSubjectOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003674 __ JumpIfSmi(eax, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003675 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
3676 __ j(NegateCondition(is_string), &runtime);
3677 // Get the length of the string to ebx.
3678 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
3679
3680 // ebx: Length of subject string as a smi
3681 // ecx: RegExp data (FixedArray)
3682 // edx: Number of capture registers
3683 // Check that the third argument is a positive smi less than the subject
3684 // string length. A negative value will be greater (unsigned comparison).
3685 __ mov(eax, Operand(esp, kPreviousIndexOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003686 __ JumpIfNotSmi(eax, &runtime);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003687 __ cmp(eax, ebx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003688 __ j(above_equal, &runtime);
3689
3690 // ecx: RegExp data (FixedArray)
3691 // edx: Number of capture registers
3692 // Check that the fourth object is a JSArray object.
3693 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00003694 __ JumpIfSmi(eax, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003695 __ CmpObjectType(eax, JS_ARRAY_TYPE, ebx);
3696 __ j(not_equal, &runtime);
3697 // Check that the JSArray is in fast case.
3698 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3699 __ mov(eax, FieldOperand(ebx, HeapObject::kMapOffset));
Steve Block44f0eee2011-05-26 01:26:41 +01003700 Factory* factory = masm->isolate()->factory();
3701 __ cmp(eax, factory->fixed_array_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003702 __ j(not_equal, &runtime);
3703 // Check that the last match info has space for the capture registers and the
3704 // additional information.
3705 __ mov(eax, FieldOperand(ebx, FixedArray::kLengthOffset));
3706 __ SmiUntag(eax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003707 __ add(edx, Immediate(RegExpImpl::kLastMatchOverhead));
3708 __ cmp(edx, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003709 __ j(greater, &runtime);
3710
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003711 // Reset offset for possibly sliced string.
3712 __ Set(edi, Immediate(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003713 // ecx: RegExp data (FixedArray)
3714 // Check the representation and encoding of the subject string.
3715 Label seq_ascii_string, seq_two_byte_string, check_code;
3716 __ mov(eax, Operand(esp, kSubjectOffset));
3717 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
3718 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
3719 // First check for flat two byte string.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003720 __ and_(ebx, kIsNotStringMask |
3721 kStringRepresentationMask |
3722 kStringEncodingMask |
3723 kShortExternalStringMask);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003724 STATIC_ASSERT((kStringTag | kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003725 __ j(zero, &seq_two_byte_string, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003726 // Any other flat string must be a flat ASCII string. None of the following
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003727 // string type tests will succeed if subject is not a string or a short
3728 // external string.
3729 __ and_(ebx, Immediate(kIsNotStringMask |
3730 kStringRepresentationMask |
3731 kShortExternalStringMask));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003732 __ j(zero, &seq_ascii_string, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003733
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003734 // ebx: whether subject is a string and if yes, its string representation
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003735 // Check for flat cons string or sliced string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003736 // A flat cons string is a cons string where the second part is the empty
3737 // string. In that case the subject string is just the first part of the cons
3738 // string. Also in this case the first part of the cons string is known to be
3739 // a sequential string or an external string.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003740 // In the case of a sliced string its offset has to be taken into account.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003741 Label cons_string, external_string, check_encoding;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003742 STATIC_ASSERT(kConsStringTag < kExternalStringTag);
3743 STATIC_ASSERT(kSlicedStringTag > kExternalStringTag);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003744 STATIC_ASSERT(kIsNotStringMask > kExternalStringTag);
3745 STATIC_ASSERT(kShortExternalStringTag > kExternalStringTag);
3746 __ cmp(ebx, Immediate(kExternalStringTag));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003747 __ j(less, &cons_string);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003748 __ j(equal, &external_string);
3749
3750 // Catch non-string subject or short external string.
3751 STATIC_ASSERT(kNotStringTag != 0 && kShortExternalStringTag !=0);
3752 __ test(ebx, Immediate(kIsNotStringMask | kShortExternalStringTag));
3753 __ j(not_zero, &runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003754
3755 // String is sliced.
3756 __ mov(edi, FieldOperand(eax, SlicedString::kOffsetOffset));
3757 __ mov(eax, FieldOperand(eax, SlicedString::kParentOffset));
3758 // edi: offset of sliced string, smi-tagged.
3759 // eax: parent string.
3760 __ jmp(&check_encoding, Label::kNear);
3761 // String is a cons string, check whether it is flat.
3762 __ bind(&cons_string);
3763 __ cmp(FieldOperand(eax, ConsString::kSecondOffset), factory->empty_string());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003764 __ j(not_equal, &runtime);
3765 __ mov(eax, FieldOperand(eax, ConsString::kFirstOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003766 __ bind(&check_encoding);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003767 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003768 // eax: first part of cons string or parent of sliced string.
3769 // ebx: map of first part of cons string or map of parent of sliced string.
3770 // Is first part of cons or parent of slice a flat two byte string?
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003771 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3772 kStringRepresentationMask | kStringEncodingMask);
3773 STATIC_ASSERT((kSeqStringTag | kTwoByteStringTag) == 0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003774 __ j(zero, &seq_two_byte_string, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003775 // Any other flat string must be sequential ASCII or external.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003776 __ test_b(FieldOperand(ebx, Map::kInstanceTypeOffset),
3777 kStringRepresentationMask);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003778 __ j(not_zero, &external_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003779
3780 __ bind(&seq_ascii_string);
Ben Murdochc7cc0282012-03-05 14:35:55 +00003781 // eax: subject string (flat ASCII)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003782 // ecx: RegExp data (FixedArray)
3783 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataAsciiCodeOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +00003784 __ Set(ecx, Immediate(1)); // Type is ASCII.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003785 __ jmp(&check_code, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003786
3787 __ bind(&seq_two_byte_string);
3788 // eax: subject string (flat two byte)
3789 // ecx: RegExp data (FixedArray)
3790 __ mov(edx, FieldOperand(ecx, JSRegExp::kDataUC16CodeOffset));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003791 __ Set(ecx, Immediate(0)); // Type is two byte.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003792
3793 __ bind(&check_code);
3794 // Check that the irregexp code has been generated for the actual string
3795 // encoding. If it has, the field contains a code object otherwise it contains
Ben Murdoch257744e2011-11-30 15:57:28 +00003796 // a smi (code flushing support).
3797 __ JumpIfSmi(edx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003798
3799 // eax: subject string
3800 // edx: code
Ben Murdochc7cc0282012-03-05 14:35:55 +00003801 // ecx: encoding of subject string (1 if ASCII, 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003802 // Load used arguments before starting to push arguments for call to native
3803 // RegExp code to avoid handling changing stack height.
3804 __ mov(ebx, Operand(esp, kPreviousIndexOffset));
3805 __ SmiUntag(ebx); // Previous index from smi.
3806
3807 // eax: subject string
3808 // ebx: previous index
3809 // edx: code
Ben Murdochc7cc0282012-03-05 14:35:55 +00003810 // ecx: encoding of subject string (1 if ASCII 0 if two_byte);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003811 // All checks done. Now push arguments for native regexp code.
Steve Block44f0eee2011-05-26 01:26:41 +01003812 Counters* counters = masm->isolate()->counters();
3813 __ IncrementCounter(counters->regexp_entry_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003814
Steve Block44f0eee2011-05-26 01:26:41 +01003815 // Isolates: note we add an additional parameter here (isolate pointer).
3816 static const int kRegExpExecuteArguments = 8;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003817 __ EnterApiExitFrame(kRegExpExecuteArguments);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003818
Steve Block44f0eee2011-05-26 01:26:41 +01003819 // Argument 8: Pass current isolate address.
3820 __ mov(Operand(esp, 7 * kPointerSize),
3821 Immediate(ExternalReference::isolate_address()));
3822
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003823 // Argument 7: Indicate that this is a direct call from JavaScript.
3824 __ mov(Operand(esp, 6 * kPointerSize), Immediate(1));
3825
3826 // Argument 6: Start (high end) of backtracking stack memory area.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003827 __ mov(esi, Operand::StaticVariable(address_of_regexp_stack_memory_address));
3828 __ add(esi, Operand::StaticVariable(address_of_regexp_stack_memory_size));
3829 __ mov(Operand(esp, 5 * kPointerSize), esi);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003830
3831 // Argument 5: static offsets vector buffer.
3832 __ mov(Operand(esp, 4 * kPointerSize),
Steve Block44f0eee2011-05-26 01:26:41 +01003833 Immediate(ExternalReference::address_of_static_offsets_vector(
3834 masm->isolate())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003835
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003836 // Argument 2: Previous index.
3837 __ mov(Operand(esp, 1 * kPointerSize), ebx);
3838
3839 // Argument 1: Original subject string.
3840 // The original subject is in the previous stack frame. Therefore we have to
3841 // use ebp, which points exactly to one pointer size below the previous esp.
3842 // (Because creating a new stack frame pushes the previous ebp onto the stack
3843 // and thereby moves up esp by one kPointerSize.)
3844 __ mov(esi, Operand(ebp, kSubjectOffset + kPointerSize));
3845 __ mov(Operand(esp, 0 * kPointerSize), esi);
3846
3847 // esi: original subject string
3848 // eax: underlying subject string
3849 // ebx: previous index
Ben Murdochc7cc0282012-03-05 14:35:55 +00003850 // ecx: encoding of subject string (1 if ASCII 0 if two_byte);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003851 // edx: code
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003852 // Argument 4: End of string data
3853 // Argument 3: Start of string data
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003854 // Prepare start and end index of the input.
3855 // Load the length from the original sliced string if that is the case.
3856 __ mov(esi, FieldOperand(esi, String::kLengthOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003857 __ add(esi, edi); // Calculate input end wrt offset.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003858 __ SmiUntag(edi);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003859 __ add(ebx, edi); // Calculate input start wrt offset.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003860
3861 // ebx: start index of the input string
3862 // esi: end index of the input string
3863 Label setup_two_byte, setup_rest;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003864 __ test(ecx, ecx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003865 __ j(zero, &setup_two_byte, Label::kNear);
3866 __ SmiUntag(esi);
3867 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqAsciiString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003868 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3869 __ lea(ecx, FieldOperand(eax, ebx, times_1, SeqAsciiString::kHeaderSize));
3870 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
Ben Murdoch257744e2011-11-30 15:57:28 +00003871 __ jmp(&setup_rest, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003872
3873 __ bind(&setup_two_byte);
3874 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003875 STATIC_ASSERT(kSmiTagSize == 1); // esi is smi (powered by 2).
3876 __ lea(ecx, FieldOperand(eax, esi, times_1, SeqTwoByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003877 __ mov(Operand(esp, 3 * kPointerSize), ecx); // Argument 4.
3878 __ lea(ecx, FieldOperand(eax, ebx, times_2, SeqTwoByteString::kHeaderSize));
3879 __ mov(Operand(esp, 2 * kPointerSize), ecx); // Argument 3.
3880
3881 __ bind(&setup_rest);
3882
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003883 // Locate the code entry and call it.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003884 __ add(edx, Immediate(Code::kHeaderSize - kHeapObjectTag));
3885 __ call(edx);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003886
3887 // Drop arguments and come back to JS mode.
3888 __ LeaveApiExitFrame();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003889
3890 // Check the result.
3891 Label success;
3892 __ cmp(eax, NativeRegExpMacroAssembler::SUCCESS);
Ben Murdoch257744e2011-11-30 15:57:28 +00003893 __ j(equal, &success);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003894 Label failure;
3895 __ cmp(eax, NativeRegExpMacroAssembler::FAILURE);
Ben Murdoch257744e2011-11-30 15:57:28 +00003896 __ j(equal, &failure);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003897 __ cmp(eax, NativeRegExpMacroAssembler::EXCEPTION);
3898 // If not exception it can only be retry. Handle that in the runtime system.
3899 __ j(not_equal, &runtime);
3900 // Result must now be exception. If there is no pending exception already a
3901 // stack overflow (on the backtrack stack) was detected in RegExp code but
3902 // haven't created the exception yet. Handle that in the runtime system.
3903 // TODO(592): Rerunning the RegExp to get the stack overflow exception.
Ben Murdoch589d6972011-11-30 16:04:58 +00003904 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01003905 masm->isolate());
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003906 __ mov(edx, Immediate(masm->isolate()->factory()->the_hole_value()));
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003907 __ mov(eax, Operand::StaticVariable(pending_exception));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003908 __ cmp(edx, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003909 __ j(equal, &runtime);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003910 // For exception, throw the exception again.
3911
3912 // Clear the pending exception variable.
3913 __ mov(Operand::StaticVariable(pending_exception), edx);
3914
3915 // Special handling of termination exceptions which are uncatchable
3916 // by javascript code.
Steve Block44f0eee2011-05-26 01:26:41 +01003917 __ cmp(eax, factory->termination_exception());
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003918 Label throw_termination_exception;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00003919 __ j(equal, &throw_termination_exception, Label::kNear);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003920
3921 // Handle normal exception by following handler chain.
3922 __ Throw(eax);
3923
3924 __ bind(&throw_termination_exception);
3925 __ ThrowUncatchable(TERMINATION, eax);
3926
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003927 __ bind(&failure);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01003928 // For failure to match, return null.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003929 __ mov(eax, factory->null_value());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003930 __ ret(4 * kPointerSize);
3931
3932 // Load RegExp data.
3933 __ bind(&success);
3934 __ mov(eax, Operand(esp, kJSRegExpOffset));
3935 __ mov(ecx, FieldOperand(eax, JSRegExp::kDataOffset));
3936 __ mov(edx, FieldOperand(ecx, JSRegExp::kIrregexpCaptureCountOffset));
3937 // Calculate number of capture registers (number_of_captures + 1) * 2.
3938 STATIC_ASSERT(kSmiTag == 0);
3939 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003940 __ add(edx, Immediate(2)); // edx was a smi.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003941
3942 // edx: Number of capture registers
3943 // Load last_match_info which is still known to be a fast case JSArray.
3944 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3945 __ mov(ebx, FieldOperand(eax, JSArray::kElementsOffset));
3946
3947 // ebx: last_match_info backing store (FixedArray)
3948 // edx: number of capture registers
3949 // Store the capture count.
3950 __ SmiTag(edx); // Number of capture registers to smi.
3951 __ mov(FieldOperand(ebx, RegExpImpl::kLastCaptureCountOffset), edx);
3952 __ SmiUntag(edx); // Number of capture registers back from smi.
3953 // Store last subject and last input.
3954 __ mov(eax, Operand(esp, kSubjectOffset));
3955 __ mov(FieldOperand(ebx, RegExpImpl::kLastSubjectOffset), eax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003956 __ RecordWriteField(ebx,
3957 RegExpImpl::kLastSubjectOffset,
3958 eax,
3959 edi,
3960 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003961 __ mov(eax, Operand(esp, kSubjectOffset));
3962 __ mov(FieldOperand(ebx, RegExpImpl::kLastInputOffset), eax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003963 __ RecordWriteField(ebx,
3964 RegExpImpl::kLastInputOffset,
3965 eax,
3966 edi,
3967 kDontSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003968
3969 // Get the static offsets vector filled by the native regexp code.
3970 ExternalReference address_of_static_offsets_vector =
Steve Block44f0eee2011-05-26 01:26:41 +01003971 ExternalReference::address_of_static_offsets_vector(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003972 __ mov(ecx, Immediate(address_of_static_offsets_vector));
3973
3974 // ebx: last_match_info backing store (FixedArray)
3975 // ecx: offsets vector
3976 // edx: number of capture registers
Ben Murdoch257744e2011-11-30 15:57:28 +00003977 Label next_capture, done;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003978 // Capture register counter starts from number of capture registers and
3979 // counts down until wraping after zero.
3980 __ bind(&next_capture);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003981 __ sub(edx, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00003982 __ j(negative, &done, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01003983 // Read the value from the static offsets vector buffer.
3984 __ mov(edi, Operand(ecx, edx, times_int_size, 0));
3985 __ SmiTag(edi);
3986 // Store the smi value in the last match info.
3987 __ mov(FieldOperand(ebx,
3988 edx,
3989 times_pointer_size,
3990 RegExpImpl::kFirstCaptureOffset),
3991 edi);
3992 __ jmp(&next_capture);
3993 __ bind(&done);
3994
3995 // Return last match info.
3996 __ mov(eax, Operand(esp, kLastMatchInfoOffset));
3997 __ ret(4 * kPointerSize);
3998
Ben Murdoch592a9fc2012-03-05 11:04:45 +00003999 // External string. Short external strings have already been ruled out.
4000 // eax: subject string (expected to be external)
4001 // ebx: scratch
4002 __ bind(&external_string);
4003 __ mov(ebx, FieldOperand(eax, HeapObject::kMapOffset));
4004 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
4005 if (FLAG_debug_code) {
4006 // Assert that we do not have a cons or slice (indirect strings) here.
4007 // Sequential strings have already been ruled out.
4008 __ test_b(ebx, kIsIndirectStringMask);
4009 __ Assert(zero, "external string expected, but not found");
4010 }
4011 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
4012 // Move the pointer so that offset-wise, it looks like a sequential string.
4013 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize);
4014 __ sub(eax, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
4015 STATIC_ASSERT(kTwoByteStringTag == 0);
4016 __ test_b(ebx, kStringEncodingMask);
4017 __ j(not_zero, &seq_ascii_string);
4018 __ jmp(&seq_two_byte_string);
4019
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004020 // Do the runtime call to execute the regexp.
4021 __ bind(&runtime);
4022 __ TailCallRuntime(Runtime::kRegExpExec, 4, 1);
4023#endif // V8_INTERPRETED_REGEXP
4024}
4025
4026
Ben Murdochb0fe1622011-05-05 13:52:32 +01004027void RegExpConstructResultStub::Generate(MacroAssembler* masm) {
4028 const int kMaxInlineLength = 100;
4029 Label slowcase;
Ben Murdoch257744e2011-11-30 15:57:28 +00004030 Label done;
Ben Murdochb0fe1622011-05-05 13:52:32 +01004031 __ mov(ebx, Operand(esp, kPointerSize * 3));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004032 __ JumpIfNotSmi(ebx, &slowcase);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004033 __ cmp(ebx, Immediate(Smi::FromInt(kMaxInlineLength)));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004034 __ j(above, &slowcase);
4035 // Smi-tagging is equivalent to multiplying by 2.
4036 STATIC_ASSERT(kSmiTag == 0);
4037 STATIC_ASSERT(kSmiTagSize == 1);
4038 // Allocate RegExpResult followed by FixedArray with size in ebx.
4039 // JSArray: [Map][empty properties][Elements][Length-smi][index][input]
4040 // Elements: [Map][Length][..elements..]
4041 __ AllocateInNewSpace(JSRegExpResult::kSize + FixedArray::kHeaderSize,
4042 times_half_pointer_size,
4043 ebx, // In: Number of elements (times 2, being a smi)
4044 eax, // Out: Start of allocation (tagged).
4045 ecx, // Out: End of allocation.
4046 edx, // Scratch register
4047 &slowcase,
4048 TAG_OBJECT);
4049 // eax: Start of allocated area, object-tagged.
4050
4051 // Set JSArray map to global.regexp_result_map().
4052 // Set empty properties FixedArray.
4053 // Set elements to point to FixedArray allocated right after the JSArray.
4054 // Interleave operations for better latency.
4055 __ mov(edx, ContextOperand(esi, Context::GLOBAL_INDEX));
Steve Block44f0eee2011-05-26 01:26:41 +01004056 Factory* factory = masm->isolate()->factory();
4057 __ mov(ecx, Immediate(factory->empty_fixed_array()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004058 __ lea(ebx, Operand(eax, JSRegExpResult::kSize));
4059 __ mov(edx, FieldOperand(edx, GlobalObject::kGlobalContextOffset));
4060 __ mov(FieldOperand(eax, JSObject::kElementsOffset), ebx);
4061 __ mov(FieldOperand(eax, JSObject::kPropertiesOffset), ecx);
4062 __ mov(edx, ContextOperand(edx, Context::REGEXP_RESULT_MAP_INDEX));
4063 __ mov(FieldOperand(eax, HeapObject::kMapOffset), edx);
4064
4065 // Set input, index and length fields from arguments.
4066 __ mov(ecx, Operand(esp, kPointerSize * 1));
4067 __ mov(FieldOperand(eax, JSRegExpResult::kInputOffset), ecx);
4068 __ mov(ecx, Operand(esp, kPointerSize * 2));
4069 __ mov(FieldOperand(eax, JSRegExpResult::kIndexOffset), ecx);
4070 __ mov(ecx, Operand(esp, kPointerSize * 3));
4071 __ mov(FieldOperand(eax, JSArray::kLengthOffset), ecx);
4072
4073 // Fill out the elements FixedArray.
4074 // eax: JSArray.
4075 // ebx: FixedArray.
4076 // ecx: Number of elements in array, as smi.
4077
4078 // Set map.
4079 __ mov(FieldOperand(ebx, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01004080 Immediate(factory->fixed_array_map()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004081 // Set length.
4082 __ mov(FieldOperand(ebx, FixedArray::kLengthOffset), ecx);
4083 // Fill contents of fixed-array with the-hole.
4084 __ SmiUntag(ecx);
Steve Block44f0eee2011-05-26 01:26:41 +01004085 __ mov(edx, Immediate(factory->the_hole_value()));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004086 __ lea(ebx, FieldOperand(ebx, FixedArray::kHeaderSize));
4087 // Fill fixed array elements with hole.
4088 // eax: JSArray.
4089 // ecx: Number of elements to fill.
4090 // ebx: Start of elements in FixedArray.
4091 // edx: the hole.
4092 Label loop;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004093 __ test(ecx, ecx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01004094 __ bind(&loop);
Ben Murdoch257744e2011-11-30 15:57:28 +00004095 __ j(less_equal, &done, Label::kNear); // Jump if ecx is negative or zero.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004096 __ sub(ecx, Immediate(1));
Ben Murdochb0fe1622011-05-05 13:52:32 +01004097 __ mov(Operand(ebx, ecx, times_pointer_size, 0), edx);
4098 __ jmp(&loop);
4099
4100 __ bind(&done);
4101 __ ret(3 * kPointerSize);
4102
4103 __ bind(&slowcase);
4104 __ TailCallRuntime(Runtime::kRegExpConstructResult, 3, 1);
4105}
4106
4107
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004108void NumberToStringStub::GenerateLookupNumberStringCache(MacroAssembler* masm,
4109 Register object,
4110 Register result,
4111 Register scratch1,
4112 Register scratch2,
4113 bool object_is_smi,
4114 Label* not_found) {
4115 // Use of registers. Register result is used as a temporary.
4116 Register number_string_cache = result;
4117 Register mask = scratch1;
4118 Register scratch = scratch2;
4119
4120 // Load the number string cache.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004121 ExternalReference roots_array_start =
4122 ExternalReference::roots_array_start(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004123 __ mov(scratch, Immediate(Heap::kNumberStringCacheRootIndex));
4124 __ mov(number_string_cache,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004125 Operand::StaticArray(scratch, times_pointer_size, roots_array_start));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004126 // Make the hash mask from the length of the number string cache. It
4127 // contains two elements (number and string) for each cache entry.
4128 __ mov(mask, FieldOperand(number_string_cache, FixedArray::kLengthOffset));
4129 __ shr(mask, kSmiTagSize + 1); // Untag length and divide it by two.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004130 __ sub(mask, Immediate(1)); // Make mask.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004131
4132 // Calculate the entry in the number string cache. The hash value in the
4133 // number string cache for smis is just the smi value, and the hash for
4134 // doubles is the xor of the upper and lower words. See
4135 // Heap::GetNumberStringCache.
Ben Murdoch257744e2011-11-30 15:57:28 +00004136 Label smi_hash_calculated;
4137 Label load_result_from_cache;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004138 if (object_is_smi) {
4139 __ mov(scratch, object);
4140 __ SmiUntag(scratch);
4141 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00004142 Label not_smi;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004143 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004144 __ JumpIfNotSmi(object, &not_smi, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004145 __ mov(scratch, object);
4146 __ SmiUntag(scratch);
Ben Murdoch257744e2011-11-30 15:57:28 +00004147 __ jmp(&smi_hash_calculated, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004148 __ bind(&not_smi);
4149 __ cmp(FieldOperand(object, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01004150 masm->isolate()->factory()->heap_number_map());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004151 __ j(not_equal, not_found);
4152 STATIC_ASSERT(8 == kDoubleSize);
4153 __ mov(scratch, FieldOperand(object, HeapNumber::kValueOffset));
4154 __ xor_(scratch, FieldOperand(object, HeapNumber::kValueOffset + 4));
4155 // Object is heap number and hash is now in scratch. Calculate cache index.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004156 __ and_(scratch, mask);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004157 Register index = scratch;
4158 Register probe = mask;
4159 __ mov(probe,
4160 FieldOperand(number_string_cache,
4161 index,
4162 times_twice_pointer_size,
4163 FixedArray::kHeaderSize));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004164 __ JumpIfSmi(probe, not_found);
Ben Murdoch8b112d22011-06-08 16:22:53 +01004165 if (CpuFeatures::IsSupported(SSE2)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004166 CpuFeatures::Scope fscope(SSE2);
4167 __ movdbl(xmm0, FieldOperand(object, HeapNumber::kValueOffset));
4168 __ movdbl(xmm1, FieldOperand(probe, HeapNumber::kValueOffset));
4169 __ ucomisd(xmm0, xmm1);
4170 } else {
4171 __ fld_d(FieldOperand(object, HeapNumber::kValueOffset));
4172 __ fld_d(FieldOperand(probe, HeapNumber::kValueOffset));
4173 __ FCmp();
4174 }
4175 __ j(parity_even, not_found); // Bail out if NaN is involved.
4176 __ j(not_equal, not_found); // The cache did not contain this value.
Ben Murdoch257744e2011-11-30 15:57:28 +00004177 __ jmp(&load_result_from_cache, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004178 }
4179
4180 __ bind(&smi_hash_calculated);
4181 // Object is smi and hash is now in scratch. Calculate cache index.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004182 __ and_(scratch, mask);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004183 Register index = scratch;
4184 // Check if the entry is the smi we are looking for.
4185 __ cmp(object,
4186 FieldOperand(number_string_cache,
4187 index,
4188 times_twice_pointer_size,
4189 FixedArray::kHeaderSize));
4190 __ j(not_equal, not_found);
4191
4192 // Get the result from the cache.
4193 __ bind(&load_result_from_cache);
4194 __ mov(result,
4195 FieldOperand(number_string_cache,
4196 index,
4197 times_twice_pointer_size,
4198 FixedArray::kHeaderSize + kPointerSize));
Steve Block44f0eee2011-05-26 01:26:41 +01004199 Counters* counters = masm->isolate()->counters();
4200 __ IncrementCounter(counters->number_to_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004201}
4202
4203
4204void NumberToStringStub::Generate(MacroAssembler* masm) {
4205 Label runtime;
4206
4207 __ mov(ebx, Operand(esp, kPointerSize));
4208
4209 // Generate code to lookup number in the number string cache.
4210 GenerateLookupNumberStringCache(masm, ebx, eax, ecx, edx, false, &runtime);
4211 __ ret(1 * kPointerSize);
4212
4213 __ bind(&runtime);
4214 // Handle number to string in the runtime system if not found in the cache.
4215 __ TailCallRuntime(Runtime::kNumberToStringSkipCache, 1, 1);
4216}
4217
4218
4219static int NegativeComparisonResult(Condition cc) {
4220 ASSERT(cc != equal);
4221 ASSERT((cc == less) || (cc == less_equal)
4222 || (cc == greater) || (cc == greater_equal));
4223 return (cc == greater || cc == greater_equal) ? LESS : GREATER;
4224}
4225
4226void CompareStub::Generate(MacroAssembler* masm) {
4227 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
4228
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004229 Label check_unequal_objects;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004230
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004231 // Compare two smis if required.
4232 if (include_smi_compare_) {
4233 Label non_smi, smi_done;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004234 __ mov(ecx, edx);
4235 __ or_(ecx, eax);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004236 __ JumpIfNotSmi(ecx, &non_smi, Label::kNear);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004237 __ sub(edx, eax); // Return on the result of the subtraction.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004238 __ j(no_overflow, &smi_done, Label::kNear);
Ben Murdochf87a2032010-10-22 12:50:53 +01004239 __ not_(edx); // Correct sign in case of overflow. edx is never 0 here.
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004240 __ bind(&smi_done);
4241 __ mov(eax, edx);
4242 __ ret(0);
4243 __ bind(&non_smi);
4244 } else if (FLAG_debug_code) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004245 __ mov(ecx, edx);
4246 __ or_(ecx, eax);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01004247 __ test(ecx, Immediate(kSmiTagMask));
4248 __ Assert(not_zero, "Unexpected smi operands.");
4249 }
4250
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004251 // NOTICE! This code is only reached after a smi-fast-case check, so
4252 // it is certain that at least one operand isn't a smi.
4253
4254 // Identical objects can be compared fast, but there are some tricky cases
4255 // for NaN and undefined.
4256 {
4257 Label not_identical;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004258 __ cmp(eax, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004259 __ j(not_equal, &not_identical);
4260
4261 if (cc_ != equal) {
4262 // Check for undefined. undefined OP undefined is false even though
4263 // undefined == undefined.
Ben Murdoch257744e2011-11-30 15:57:28 +00004264 Label check_for_nan;
Steve Block44f0eee2011-05-26 01:26:41 +01004265 __ cmp(edx, masm->isolate()->factory()->undefined_value());
Ben Murdoch257744e2011-11-30 15:57:28 +00004266 __ j(not_equal, &check_for_nan, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004267 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4268 __ ret(0);
4269 __ bind(&check_for_nan);
4270 }
4271
Steve Block44f0eee2011-05-26 01:26:41 +01004272 // Test for NaN. Sadly, we can't just compare to factory->nan_value(),
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004273 // so we do the second best thing - test it ourselves.
4274 // Note: if cc_ != equal, never_nan_nan_ is not used.
4275 if (never_nan_nan_ && (cc_ == equal)) {
4276 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4277 __ ret(0);
4278 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00004279 Label heap_number;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004280 __ cmp(FieldOperand(edx, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01004281 Immediate(masm->isolate()->factory()->heap_number_map()));
Ben Murdoch257744e2011-11-30 15:57:28 +00004282 __ j(equal, &heap_number, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004283 if (cc_ != equal) {
4284 // Call runtime on identical JSObjects. Otherwise return equal.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004285 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004286 __ j(above_equal, &not_identical);
4287 }
4288 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4289 __ ret(0);
4290
4291 __ bind(&heap_number);
4292 // It is a heap number, so return non-equal if it's NaN and equal if
4293 // it's not NaN.
4294 // The representation of NaN values has all exponent bits (52..62) set,
4295 // and not all mantissa bits (0..51) clear.
4296 // We only accept QNaNs, which have bit 51 set.
4297 // Read top bits of double representation (second word of value).
4298
4299 // Value is a QNaN if value & kQuietNaNMask == kQuietNaNMask, i.e.,
4300 // all bits in the mask are set. We only need to check the word
4301 // that contains the exponent and high bit of the mantissa.
4302 STATIC_ASSERT(((kQuietNaNHighBitsMask << 1) & 0x80000000u) != 0);
4303 __ mov(edx, FieldOperand(edx, HeapNumber::kExponentOffset));
Steve Block9fac8402011-05-12 15:51:54 +01004304 __ Set(eax, Immediate(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004305 // Shift value and mask so kQuietNaNHighBitsMask applies to topmost
4306 // bits.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004307 __ add(edx, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004308 __ cmp(edx, kQuietNaNHighBitsMask << 1);
4309 if (cc_ == equal) {
4310 STATIC_ASSERT(EQUAL != 1);
4311 __ setcc(above_equal, eax);
4312 __ ret(0);
4313 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00004314 Label nan;
4315 __ j(above_equal, &nan, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004316 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
4317 __ ret(0);
4318 __ bind(&nan);
4319 __ Set(eax, Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4320 __ ret(0);
4321 }
4322 }
4323
4324 __ bind(&not_identical);
4325 }
4326
4327 // Strict equality can quickly decide whether objects are equal.
4328 // Non-strict object equality is slower, so it is handled later in the stub.
4329 if (cc_ == equal && strict_) {
4330 Label slow; // Fallthrough label.
Ben Murdoch257744e2011-11-30 15:57:28 +00004331 Label not_smis;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004332 // If we're doing a strict equality comparison, we don't have to do
4333 // type conversion, so we generate code to do fast comparison for objects
4334 // and oddballs. Non-smi numbers and strings still go through the usual
4335 // slow-case code.
4336 // If either is a Smi (we know that not both are), then they can only
4337 // be equal if the other is a HeapNumber. If so, use the slow case.
4338 STATIC_ASSERT(kSmiTag == 0);
4339 ASSERT_EQ(0, Smi::FromInt(0));
4340 __ mov(ecx, Immediate(kSmiTagMask));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004341 __ and_(ecx, eax);
4342 __ test(ecx, edx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004343 __ j(not_zero, &not_smis, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004344 // One operand is a smi.
4345
4346 // Check whether the non-smi is a heap number.
4347 STATIC_ASSERT(kSmiTagMask == 1);
4348 // ecx still holds eax & kSmiTag, which is either zero or one.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004349 __ sub(ecx, Immediate(0x01));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004350 __ mov(ebx, edx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004351 __ xor_(ebx, eax);
4352 __ and_(ebx, ecx); // ebx holds either 0 or eax ^ edx.
4353 __ xor_(ebx, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004354 // if eax was smi, ebx is now edx, else eax.
4355
4356 // Check if the non-smi operand is a heap number.
4357 __ cmp(FieldOperand(ebx, HeapObject::kMapOffset),
Steve Block44f0eee2011-05-26 01:26:41 +01004358 Immediate(masm->isolate()->factory()->heap_number_map()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004359 // If heap number, handle it in the slow case.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004360 __ j(equal, &slow, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004361 // Return non-equal (ebx is not zero)
4362 __ mov(eax, ebx);
4363 __ ret(0);
4364
4365 __ bind(&not_smis);
4366 // If either operand is a JSObject or an oddball value, then they are not
4367 // equal since their pointers are different
4368 // There is no test for undetectability in strict equality.
4369
4370 // Get the type of the first operand.
4371 // If the first object is a JS object, we have done pointer comparison.
Ben Murdoch257744e2011-11-30 15:57:28 +00004372 Label first_non_object;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004373 STATIC_ASSERT(LAST_TYPE == LAST_SPEC_OBJECT_TYPE);
4374 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004375 __ j(below, &first_non_object, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004376
4377 // Return non-zero (eax is not zero)
Ben Murdoch257744e2011-11-30 15:57:28 +00004378 Label return_not_equal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004379 STATIC_ASSERT(kHeapObjectTag != 0);
4380 __ bind(&return_not_equal);
4381 __ ret(0);
4382
4383 __ bind(&first_non_object);
4384 // Check for oddballs: true, false, null, undefined.
4385 __ CmpInstanceType(ecx, ODDBALL_TYPE);
4386 __ j(equal, &return_not_equal);
4387
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004388 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004389 __ j(above_equal, &return_not_equal);
4390
4391 // Check for oddballs: true, false, null, undefined.
4392 __ CmpInstanceType(ecx, ODDBALL_TYPE);
4393 __ j(equal, &return_not_equal);
4394
4395 // Fall through to the general case.
4396 __ bind(&slow);
4397 }
4398
4399 // Generate the number comparison code.
4400 if (include_number_compare_) {
4401 Label non_number_comparison;
4402 Label unordered;
Ben Murdoch8b112d22011-06-08 16:22:53 +01004403 if (CpuFeatures::IsSupported(SSE2)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004404 CpuFeatures::Scope use_sse2(SSE2);
4405 CpuFeatures::Scope use_cmov(CMOV);
4406
4407 FloatingPointHelper::LoadSSE2Operands(masm, &non_number_comparison);
4408 __ ucomisd(xmm0, xmm1);
4409
4410 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004411 __ j(parity_even, &unordered, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004412 // Return a result of -1, 0, or 1, based on EFLAGS.
4413 __ mov(eax, 0); // equal
4414 __ mov(ecx, Immediate(Smi::FromInt(1)));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004415 __ cmov(above, eax, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004416 __ mov(ecx, Immediate(Smi::FromInt(-1)));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004417 __ cmov(below, eax, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004418 __ ret(0);
4419 } else {
4420 FloatingPointHelper::CheckFloatOperands(
4421 masm, &non_number_comparison, ebx);
4422 FloatingPointHelper::LoadFloatOperand(masm, eax);
4423 FloatingPointHelper::LoadFloatOperand(masm, edx);
4424 __ FCmp();
4425
4426 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004427 __ j(parity_even, &unordered, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004428
Ben Murdoch257744e2011-11-30 15:57:28 +00004429 Label below_label, above_label;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004430 // Return a result of -1, 0, or 1, based on EFLAGS.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004431 __ j(below, &below_label, Label::kNear);
4432 __ j(above, &above_label, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004433
Steve Block9fac8402011-05-12 15:51:54 +01004434 __ Set(eax, Immediate(0));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004435 __ ret(0);
4436
4437 __ bind(&below_label);
4438 __ mov(eax, Immediate(Smi::FromInt(-1)));
4439 __ ret(0);
4440
4441 __ bind(&above_label);
4442 __ mov(eax, Immediate(Smi::FromInt(1)));
4443 __ ret(0);
4444 }
4445
4446 // If one of the numbers was NaN, then the result is always false.
4447 // The cc is never not-equal.
4448 __ bind(&unordered);
4449 ASSERT(cc_ != not_equal);
4450 if (cc_ == less || cc_ == less_equal) {
4451 __ mov(eax, Immediate(Smi::FromInt(1)));
4452 } else {
4453 __ mov(eax, Immediate(Smi::FromInt(-1)));
4454 }
4455 __ ret(0);
4456
4457 // The number comparison code did not provide a valid result.
4458 __ bind(&non_number_comparison);
4459 }
4460
4461 // Fast negative check for symbol-to-symbol equality.
4462 Label check_for_strings;
4463 if (cc_ == equal) {
4464 BranchIfNonSymbol(masm, &check_for_strings, eax, ecx);
4465 BranchIfNonSymbol(masm, &check_for_strings, edx, ecx);
4466
4467 // We've already checked for object identity, so if both operands
4468 // are symbols they aren't equal. Register eax already holds a
4469 // non-zero value, which indicates not equal, so just return.
4470 __ ret(0);
4471 }
4472
4473 __ bind(&check_for_strings);
4474
4475 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx,
4476 &check_unequal_objects);
4477
Ben Murdochc7cc0282012-03-05 14:35:55 +00004478 // Inline comparison of ASCII strings.
Ben Murdoch257744e2011-11-30 15:57:28 +00004479 if (cc_ == equal) {
4480 StringCompareStub::GenerateFlatAsciiStringEquals(masm,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004481 edx,
4482 eax,
4483 ecx,
Ben Murdoch257744e2011-11-30 15:57:28 +00004484 ebx);
4485 } else {
4486 StringCompareStub::GenerateCompareFlatAsciiStrings(masm,
4487 edx,
4488 eax,
4489 ecx,
4490 ebx,
4491 edi);
4492 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004493#ifdef DEBUG
4494 __ Abort("Unexpected fall-through from string comparison");
4495#endif
4496
4497 __ bind(&check_unequal_objects);
4498 if (cc_ == equal && !strict_) {
4499 // Non-strict equality. Objects are unequal if
4500 // they are both JSObjects and not undetectable,
4501 // and their pointers are different.
Ben Murdoch257744e2011-11-30 15:57:28 +00004502 Label not_both_objects;
4503 Label return_unequal;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004504 // At most one is a smi, so we can test for smi by adding the two.
4505 // A smi plus a heap object has the low bit set, a heap object plus
4506 // a heap object has the low bit clear.
4507 STATIC_ASSERT(kSmiTag == 0);
4508 STATIC_ASSERT(kSmiTagMask == 1);
4509 __ lea(ecx, Operand(eax, edx, times_1, 0));
4510 __ test(ecx, Immediate(kSmiTagMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00004511 __ j(not_zero, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004512 __ CmpObjectType(eax, FIRST_SPEC_OBJECT_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004513 __ j(below, &not_both_objects, Label::kNear);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004514 __ CmpObjectType(edx, FIRST_SPEC_OBJECT_TYPE, ebx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004515 __ j(below, &not_both_objects, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004516 // We do not bail out after this point. Both are JSObjects, and
4517 // they are equal if and only if both are undetectable.
4518 // The and of the undetectable flags is 1 if and only if they are equal.
4519 __ test_b(FieldOperand(ecx, Map::kBitFieldOffset),
4520 1 << Map::kIsUndetectable);
Ben Murdoch257744e2011-11-30 15:57:28 +00004521 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004522 __ test_b(FieldOperand(ebx, Map::kBitFieldOffset),
4523 1 << Map::kIsUndetectable);
Ben Murdoch257744e2011-11-30 15:57:28 +00004524 __ j(zero, &return_unequal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004525 // The objects are both undetectable, so they both compare as the value
4526 // undefined, and are equal.
4527 __ Set(eax, Immediate(EQUAL));
4528 __ bind(&return_unequal);
4529 // Return non-equal by returning the non-zero object pointer in eax,
4530 // or return equal if we fell through to here.
4531 __ ret(0); // rax, rdx were pushed
4532 __ bind(&not_both_objects);
4533 }
4534
4535 // Push arguments below the return address.
4536 __ pop(ecx);
4537 __ push(edx);
4538 __ push(eax);
4539
4540 // Figure out which native to call and setup the arguments.
4541 Builtins::JavaScript builtin;
4542 if (cc_ == equal) {
4543 builtin = strict_ ? Builtins::STRICT_EQUALS : Builtins::EQUALS;
4544 } else {
4545 builtin = Builtins::COMPARE;
4546 __ push(Immediate(Smi::FromInt(NegativeComparisonResult(cc_))));
4547 }
4548
4549 // Restore return address on the stack.
4550 __ push(ecx);
4551
4552 // Call the native; it returns -1 (less), 0 (equal), or 1 (greater)
4553 // tagged as a small integer.
4554 __ InvokeBuiltin(builtin, JUMP_FUNCTION);
4555}
4556
4557
4558void CompareStub::BranchIfNonSymbol(MacroAssembler* masm,
4559 Label* label,
4560 Register object,
4561 Register scratch) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00004562 __ JumpIfSmi(object, label);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004563 __ mov(scratch, FieldOperand(object, HeapObject::kMapOffset));
4564 __ movzx_b(scratch, FieldOperand(scratch, Map::kInstanceTypeOffset));
4565 __ and_(scratch, kIsSymbolMask | kIsNotStringMask);
4566 __ cmp(scratch, kSymbolTag | kStringTag);
4567 __ j(not_equal, label);
4568}
4569
4570
4571void StackCheckStub::Generate(MacroAssembler* masm) {
Ben Murdochf87a2032010-10-22 12:50:53 +01004572 __ TailCallRuntime(Runtime::kStackGuard, 0, 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004573}
4574
4575
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004576void CallFunctionStub::FinishCode(Handle<Code> code) {
4577 code->set_has_function_cache(RecordCallTarget());
4578}
4579
4580
4581void CallFunctionStub::Clear(Heap* heap, Address address) {
4582 ASSERT(Memory::uint8_at(address + kPointerSize) == Assembler::kTestEaxByte);
4583 // 1 ~ size of the test eax opcode.
4584 Object* cell = Memory::Object_at(address + kPointerSize + 1);
4585 // Low-level because clearing happens during GC.
4586 reinterpret_cast<JSGlobalPropertyCell*>(cell)->set_value(
4587 RawUninitializedSentinel(heap));
4588}
4589
4590
4591Object* CallFunctionStub::GetCachedValue(Address address) {
4592 ASSERT(Memory::uint8_at(address + kPointerSize) == Assembler::kTestEaxByte);
4593 // 1 ~ size of the test eax opcode.
4594 Object* cell = Memory::Object_at(address + kPointerSize + 1);
4595 return JSGlobalPropertyCell::cast(cell)->value();
4596}
4597
4598
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004599void CallFunctionStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004600 // edi : the function to call
4601 Isolate* isolate = masm->isolate();
Ben Murdoch589d6972011-11-30 16:04:58 +00004602 Label slow, non_function;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004603
Ben Murdoch257744e2011-11-30 15:57:28 +00004604 // The receiver might implicitly be the global object. This is
4605 // indicated by passing the hole as the receiver to the call
4606 // function stub.
4607 if (ReceiverMightBeImplicit()) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004608 Label receiver_ok;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004609 // Get the receiver from the stack.
4610 // +1 ~ return address
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004611 __ mov(eax, Operand(esp, (argc_ + 1) * kPointerSize));
Ben Murdoch257744e2011-11-30 15:57:28 +00004612 // Call as function is indicated with the hole.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004613 __ cmp(eax, isolate->factory()->the_hole_value());
4614 __ j(not_equal, &receiver_ok, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00004615 // Patch the receiver on the stack with the global receiver object.
4616 __ mov(ebx, GlobalObjectOperand());
4617 __ mov(ebx, FieldOperand(ebx, GlobalObject::kGlobalReceiverOffset));
4618 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), ebx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004619 __ bind(&receiver_ok);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004620 }
4621
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004622 // Check that the function really is a JavaScript function.
Ben Murdoch589d6972011-11-30 16:04:58 +00004623 __ JumpIfSmi(edi, &non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004624 // Goto slow case if we do not have a function.
4625 __ CmpObjectType(edi, JS_FUNCTION_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00004626 __ j(not_equal, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004627
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004628 if (RecordCallTarget()) {
4629 // Cache the called function in a global property cell in the
4630 // instruction stream after the call. Cache states are uninitialized,
4631 // monomorphic (indicated by a JSFunction), and megamorphic.
4632 Label initialize, call;
4633 // Load the cache cell address into ebx and the cache state into ecx.
4634 __ mov(ebx, Operand(esp, 0)); // Return address.
4635 __ mov(ebx, Operand(ebx, 1)); // 1 ~ sizeof 'test eax' opcode in bytes.
4636 __ mov(ecx, FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset));
4637
4638 // A monomorphic cache hit or an already megamorphic state: invoke the
4639 // function without changing the state.
4640 __ cmp(ecx, edi);
4641 __ j(equal, &call, Label::kNear);
4642 __ cmp(ecx, Immediate(MegamorphicSentinel(isolate)));
4643 __ j(equal, &call, Label::kNear);
4644
4645 // A monomorphic miss (i.e, here the cache is not uninitialized) goes
4646 // megamorphic.
4647 __ cmp(ecx, Immediate(UninitializedSentinel(isolate)));
4648 __ j(equal, &initialize, Label::kNear);
4649 // MegamorphicSentinel is an immortal immovable object (undefined) so no
4650 // write-barrier is needed.
4651 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
4652 Immediate(MegamorphicSentinel(isolate)));
4653 __ jmp(&call, Label::kNear);
4654
4655 // An uninitialized cache is patched with the function.
4656 __ bind(&initialize);
4657 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset), edi);
4658 // No need for a write barrier here - cells are rescanned.
4659
4660 __ bind(&call);
4661 }
4662
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004663 // Fast-case: Just invoke the function.
4664 ParameterCount actual(argc_);
Ben Murdoch257744e2011-11-30 15:57:28 +00004665
4666 if (ReceiverMightBeImplicit()) {
4667 Label call_as_function;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004668 __ cmp(eax, isolate->factory()->the_hole_value());
Ben Murdoch257744e2011-11-30 15:57:28 +00004669 __ j(equal, &call_as_function);
4670 __ InvokeFunction(edi,
4671 actual,
4672 JUMP_FUNCTION,
4673 NullCallWrapper(),
4674 CALL_AS_METHOD);
4675 __ bind(&call_as_function);
4676 }
4677 __ InvokeFunction(edi,
4678 actual,
4679 JUMP_FUNCTION,
4680 NullCallWrapper(),
4681 CALL_AS_FUNCTION);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004682
4683 // Slow-case: Non-function called.
4684 __ bind(&slow);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004685 if (RecordCallTarget()) {
4686 // If there is a call target cache, mark it megamorphic in the
4687 // non-function case.
4688 __ mov(ebx, Operand(esp, 0));
4689 __ mov(ebx, Operand(ebx, 1));
4690 // MegamorphicSentinel is an immortal immovable object (undefined) so no
4691 // write barrier is needed.
4692 __ mov(FieldOperand(ebx, JSGlobalPropertyCell::kValueOffset),
4693 Immediate(MegamorphicSentinel(isolate)));
4694 }
Ben Murdoch589d6972011-11-30 16:04:58 +00004695 // Check for function proxy.
4696 __ CmpInstanceType(ecx, JS_FUNCTION_PROXY_TYPE);
4697 __ j(not_equal, &non_function);
4698 __ pop(ecx);
4699 __ push(edi); // put proxy as additional argument under return address
4700 __ push(ecx);
4701 __ Set(eax, Immediate(argc_ + 1));
4702 __ Set(ebx, Immediate(0));
4703 __ SetCallKind(ecx, CALL_AS_FUNCTION);
4704 __ GetBuiltinEntry(edx, Builtins::CALL_FUNCTION_PROXY);
4705 {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004706 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
Ben Murdoch589d6972011-11-30 16:04:58 +00004707 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4708 }
4709
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004710 // CALL_NON_FUNCTION expects the non-function callee as receiver (instead
4711 // of the original receiver from the call site).
Ben Murdoch589d6972011-11-30 16:04:58 +00004712 __ bind(&non_function);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004713 __ mov(Operand(esp, (argc_ + 1) * kPointerSize), edi);
4714 __ Set(eax, Immediate(argc_));
4715 __ Set(ebx, Immediate(0));
Ben Murdoch589d6972011-11-30 16:04:58 +00004716 __ SetCallKind(ecx, CALL_AS_METHOD);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004717 __ GetBuiltinEntry(edx, Builtins::CALL_NON_FUNCTION);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004718 Handle<Code> adaptor = isolate->builtins()->ArgumentsAdaptorTrampoline();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004719 __ jmp(adaptor, RelocInfo::CODE_TARGET);
4720}
4721
4722
Steve Block44f0eee2011-05-26 01:26:41 +01004723bool CEntryStub::NeedsImmovableCode() {
4724 return false;
4725}
4726
4727
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004728bool CEntryStub::IsPregenerated() {
4729 return (!save_doubles_ || ISOLATE->fp_stubs_generated()) &&
4730 result_size_ == 1;
4731}
4732
4733
4734void CodeStub::GenerateStubsAheadOfTime() {
4735 CEntryStub::GenerateAheadOfTime();
4736 StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime();
4737 // It is important that the store buffer overflow stubs are generated first.
4738 RecordWriteStub::GenerateFixedRegStubsAheadOfTime();
4739}
4740
4741
4742void CodeStub::GenerateFPStubs() {
4743 CEntryStub save_doubles(1, kSaveFPRegs);
4744 Handle<Code> code = save_doubles.GetCode();
4745 code->set_is_pregenerated(true);
4746 code->GetIsolate()->set_fp_stubs_generated(true);
4747}
4748
4749
4750void CEntryStub::GenerateAheadOfTime() {
4751 CEntryStub stub(1, kDontSaveFPRegs);
4752 Handle<Code> code = stub.GetCode();
4753 code->set_is_pregenerated(true);
4754}
4755
4756
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004757void CEntryStub::GenerateThrowTOS(MacroAssembler* masm) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004758 __ Throw(eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004759}
4760
4761
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004762void CEntryStub::GenerateCore(MacroAssembler* masm,
4763 Label* throw_normal_exception,
4764 Label* throw_termination_exception,
4765 Label* throw_out_of_memory_exception,
4766 bool do_gc,
Steve Block1e0659c2011-05-24 12:43:12 +01004767 bool always_allocate_scope) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004768 // eax: result parameter for PerformGC, if any
4769 // ebx: pointer to C function (C callee-saved)
4770 // ebp: frame pointer (restored after C call)
4771 // esp: stack pointer (restored after C call)
4772 // edi: number of arguments including receiver (C callee-saved)
4773 // esi: pointer to the first argument (C callee-saved)
4774
4775 // Result returned in eax, or eax+edx if result_size_ is 2.
4776
4777 // Check stack alignment.
4778 if (FLAG_debug_code) {
4779 __ CheckStackAlignment();
4780 }
4781
4782 if (do_gc) {
4783 // Pass failure code returned from last attempt as first argument to
4784 // PerformGC. No need to use PrepareCallCFunction/CallCFunction here as the
4785 // stack alignment is known to be correct. This function takes one argument
4786 // which is passed on the stack, and we know that the stack has been
4787 // prepared to pass at least one argument.
4788 __ mov(Operand(esp, 0 * kPointerSize), eax); // Result.
4789 __ call(FUNCTION_ADDR(Runtime::PerformGC), RelocInfo::RUNTIME_ENTRY);
4790 }
4791
4792 ExternalReference scope_depth =
Steve Block44f0eee2011-05-26 01:26:41 +01004793 ExternalReference::heap_always_allocate_scope_depth(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004794 if (always_allocate_scope) {
4795 __ inc(Operand::StaticVariable(scope_depth));
4796 }
4797
4798 // Call C function.
4799 __ mov(Operand(esp, 0 * kPointerSize), edi); // argc.
4800 __ mov(Operand(esp, 1 * kPointerSize), esi); // argv.
Steve Block44f0eee2011-05-26 01:26:41 +01004801 __ mov(Operand(esp, 2 * kPointerSize),
4802 Immediate(ExternalReference::isolate_address()));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004803 __ call(ebx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004804 // Result is in eax or edx:eax - do not destroy these registers!
4805
4806 if (always_allocate_scope) {
4807 __ dec(Operand::StaticVariable(scope_depth));
4808 }
4809
4810 // Make sure we're not trying to return 'the hole' from the runtime
4811 // call as this may lead to crashes in the IC code later.
4812 if (FLAG_debug_code) {
Ben Murdoch257744e2011-11-30 15:57:28 +00004813 Label okay;
Steve Block44f0eee2011-05-26 01:26:41 +01004814 __ cmp(eax, masm->isolate()->factory()->the_hole_value());
Ben Murdoch257744e2011-11-30 15:57:28 +00004815 __ j(not_equal, &okay, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004816 __ int3();
4817 __ bind(&okay);
4818 }
4819
4820 // Check for failure result.
4821 Label failure_returned;
4822 STATIC_ASSERT(((kFailureTag + 1) & kFailureTagMask) == 0);
4823 __ lea(ecx, Operand(eax, 1));
4824 // Lower 2 bits of ecx are 0 iff eax has failure tag.
4825 __ test(ecx, Immediate(kFailureTagMask));
Ben Murdoch257744e2011-11-30 15:57:28 +00004826 __ j(zero, &failure_returned);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004827
Steve Block44f0eee2011-05-26 01:26:41 +01004828 ExternalReference pending_exception_address(
Ben Murdoch589d6972011-11-30 16:04:58 +00004829 Isolate::kPendingExceptionAddress, masm->isolate());
Steve Block1e0659c2011-05-24 12:43:12 +01004830
4831 // Check that there is no pending exception, otherwise we
4832 // should have returned some failure value.
4833 if (FLAG_debug_code) {
4834 __ push(edx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004835 __ mov(edx, Immediate(masm->isolate()->factory()->the_hole_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00004836 Label okay;
Steve Block1e0659c2011-05-24 12:43:12 +01004837 __ cmp(edx, Operand::StaticVariable(pending_exception_address));
4838 // Cannot use check here as it attempts to generate call into runtime.
Ben Murdoch257744e2011-11-30 15:57:28 +00004839 __ j(equal, &okay, Label::kNear);
Steve Block1e0659c2011-05-24 12:43:12 +01004840 __ int3();
4841 __ bind(&okay);
4842 __ pop(edx);
4843 }
4844
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004845 // Exit the JavaScript to C++ exit frame.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004846 __ LeaveExitFrame(save_doubles_ == kSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004847 __ ret(0);
4848
4849 // Handling of failure.
4850 __ bind(&failure_returned);
4851
4852 Label retry;
4853 // If the returned exception is RETRY_AFTER_GC continue at retry label
4854 STATIC_ASSERT(Failure::RETRY_AFTER_GC == 0);
4855 __ test(eax, Immediate(((1 << kFailureTypeTagSize) - 1) << kFailureTagSize));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004856 __ j(zero, &retry, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004857
4858 // Special handling of out of memory exceptions.
4859 __ cmp(eax, reinterpret_cast<int32_t>(Failure::OutOfMemoryException()));
4860 __ j(equal, throw_out_of_memory_exception);
4861
4862 // Retrieve the pending exception and clear the variable.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004863 __ mov(eax, Operand::StaticVariable(pending_exception_address));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004864 __ mov(edx, Immediate(masm->isolate()->factory()->the_hole_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004865 __ mov(Operand::StaticVariable(pending_exception_address), edx);
4866
4867 // Special handling of termination exceptions which are uncatchable
4868 // by javascript code.
Steve Block44f0eee2011-05-26 01:26:41 +01004869 __ cmp(eax, masm->isolate()->factory()->termination_exception());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004870 __ j(equal, throw_termination_exception);
4871
4872 // Handle normal exception.
4873 __ jmp(throw_normal_exception);
4874
4875 // Retry.
4876 __ bind(&retry);
4877}
4878
4879
4880void CEntryStub::GenerateThrowUncatchable(MacroAssembler* masm,
4881 UncatchableExceptionType type) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +01004882 __ ThrowUncatchable(type, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004883}
4884
4885
4886void CEntryStub::Generate(MacroAssembler* masm) {
4887 // eax: number of arguments including receiver
4888 // ebx: pointer to C function (C callee-saved)
4889 // ebp: frame pointer (restored after C call)
4890 // esp: stack pointer (restored after C call)
4891 // esi: current context (C callee-saved)
4892 // edi: JS function of the caller (C callee-saved)
4893
4894 // NOTE: Invocations of builtins may return failure objects instead
4895 // of a proper result. The builtin entry handles this by performing
4896 // a garbage collection and retrying the builtin (twice).
4897
4898 // Enter the exit frame that transitions from JavaScript to C++.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004899 __ EnterExitFrame(save_doubles_ == kSaveFPRegs);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004900
4901 // eax: result parameter for PerformGC, if any (setup below)
4902 // ebx: pointer to builtin function (C callee-saved)
4903 // ebp: frame pointer (restored after C call)
4904 // esp: stack pointer (restored after C call)
4905 // edi: number of arguments including receiver (C callee-saved)
4906 // esi: argv pointer (C callee-saved)
4907
4908 Label throw_normal_exception;
4909 Label throw_termination_exception;
4910 Label throw_out_of_memory_exception;
4911
4912 // Call into the runtime system.
4913 GenerateCore(masm,
4914 &throw_normal_exception,
4915 &throw_termination_exception,
4916 &throw_out_of_memory_exception,
4917 false,
4918 false);
4919
4920 // Do space-specific GC and retry runtime call.
4921 GenerateCore(masm,
4922 &throw_normal_exception,
4923 &throw_termination_exception,
4924 &throw_out_of_memory_exception,
4925 true,
4926 false);
4927
4928 // Do full GC and retry runtime call one final time.
4929 Failure* failure = Failure::InternalError();
4930 __ mov(eax, Immediate(reinterpret_cast<int32_t>(failure)));
4931 GenerateCore(masm,
4932 &throw_normal_exception,
4933 &throw_termination_exception,
4934 &throw_out_of_memory_exception,
4935 true,
4936 true);
4937
4938 __ bind(&throw_out_of_memory_exception);
4939 GenerateThrowUncatchable(masm, OUT_OF_MEMORY);
4940
4941 __ bind(&throw_termination_exception);
4942 GenerateThrowUncatchable(masm, TERMINATION);
4943
4944 __ bind(&throw_normal_exception);
4945 GenerateThrowTOS(masm);
4946}
4947
4948
4949void JSEntryStub::GenerateBody(MacroAssembler* masm, bool is_construct) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004950 Label invoke, handler_entry, exit;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004951 Label not_outermost_js, not_outermost_js_2;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004952
Ben Murdochc7cc0282012-03-05 14:35:55 +00004953 // Set up frame.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004954 __ push(ebp);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004955 __ mov(ebp, esp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004956
4957 // Push marker in two places.
4958 int marker = is_construct ? StackFrame::ENTRY_CONSTRUCT : StackFrame::ENTRY;
4959 __ push(Immediate(Smi::FromInt(marker))); // context slot
4960 __ push(Immediate(Smi::FromInt(marker))); // function slot
4961 // Save callee-saved registers (C calling conventions).
4962 __ push(edi);
4963 __ push(esi);
4964 __ push(ebx);
4965
4966 // Save copies of the top frame descriptor on the stack.
Ben Murdoch589d6972011-11-30 16:04:58 +00004967 ExternalReference c_entry_fp(Isolate::kCEntryFPAddress, masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004968 __ push(Operand::StaticVariable(c_entry_fp));
4969
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004970 // If this is the outermost JS call, set js_entry_sp value.
Ben Murdoch589d6972011-11-30 16:04:58 +00004971 ExternalReference js_entry_sp(Isolate::kJSEntrySPAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01004972 masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004973 __ cmp(Operand::StaticVariable(js_entry_sp), Immediate(0));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004974 __ j(not_equal, &not_outermost_js, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004975 __ mov(Operand::StaticVariable(js_entry_sp), ebp);
Steve Block053d10c2011-06-13 19:13:29 +01004976 __ push(Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
4977 Label cont;
Ben Murdoch69a99ed2011-11-30 16:03:39 +00004978 __ jmp(&cont, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004979 __ bind(&not_outermost_js);
Steve Block053d10c2011-06-13 19:13:29 +01004980 __ push(Immediate(Smi::FromInt(StackFrame::INNER_JSENTRY_FRAME)));
4981 __ bind(&cont);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004982
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004983 // Jump to a faked try block that does the invoke, with a faked catch
4984 // block that sets the pending exception.
4985 __ jmp(&invoke);
4986 __ bind(&handler_entry);
4987 handler_offset_ = handler_entry.pos();
4988 // Caught exception: Store result (exception) in the pending exception
4989 // field in the JSEnv and return a failure sentinel.
Ben Murdoch589d6972011-11-30 16:04:58 +00004990 ExternalReference pending_exception(Isolate::kPendingExceptionAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01004991 masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004992 __ mov(Operand::StaticVariable(pending_exception), eax);
4993 __ mov(eax, reinterpret_cast<int32_t>(Failure::Exception()));
4994 __ jmp(&exit);
4995
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004996 // Invoke: Link this frame into the handler chain. There's only one
4997 // handler block in this code object, so its index is 0.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01004998 __ bind(&invoke);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00004999 __ PushTryHandler(IN_JS_ENTRY, JS_ENTRY_HANDLER, 0);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005000
5001 // Clear any pending exceptions.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005002 __ mov(edx, Immediate(masm->isolate()->factory()->the_hole_value()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005003 __ mov(Operand::StaticVariable(pending_exception), edx);
5004
5005 // Fake a receiver (NULL).
5006 __ push(Immediate(0)); // receiver
5007
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005008 // Invoke the function by calling through JS entry trampoline builtin and
5009 // pop the faked function when we return. Notice that we cannot store a
5010 // reference to the trampoline code directly in this stub, because the
5011 // builtin stubs may not have been generated yet.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005012 if (is_construct) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005013 ExternalReference construct_entry(Builtins::kJSConstructEntryTrampoline,
5014 masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005015 __ mov(edx, Immediate(construct_entry));
5016 } else {
Steve Block44f0eee2011-05-26 01:26:41 +01005017 ExternalReference entry(Builtins::kJSEntryTrampoline,
5018 masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005019 __ mov(edx, Immediate(entry));
5020 }
5021 __ mov(edx, Operand(edx, 0)); // deref address
5022 __ lea(edx, FieldOperand(edx, Code::kHeaderSize));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005023 __ call(edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005024
5025 // Unlink this frame from the handler chain.
Steve Block053d10c2011-06-13 19:13:29 +01005026 __ PopTryHandler();
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005027
Steve Block053d10c2011-06-13 19:13:29 +01005028 __ bind(&exit);
Steve Block053d10c2011-06-13 19:13:29 +01005029 // Check if the current stack frame is marked as the outermost JS frame.
5030 __ pop(ebx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005031 __ cmp(ebx, Immediate(Smi::FromInt(StackFrame::OUTERMOST_JSENTRY_FRAME)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005032 __ j(not_equal, &not_outermost_js_2);
5033 __ mov(Operand::StaticVariable(js_entry_sp), Immediate(0));
5034 __ bind(&not_outermost_js_2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005035
5036 // Restore the top frame descriptor from the stack.
Steve Block44f0eee2011-05-26 01:26:41 +01005037 __ pop(Operand::StaticVariable(ExternalReference(
Ben Murdoch589d6972011-11-30 16:04:58 +00005038 Isolate::kCEntryFPAddress,
Steve Block44f0eee2011-05-26 01:26:41 +01005039 masm->isolate())));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005040
5041 // Restore callee-saved registers (C calling conventions).
5042 __ pop(ebx);
5043 __ pop(esi);
5044 __ pop(edi);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005045 __ add(esp, Immediate(2 * kPointerSize)); // remove markers
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005046
5047 // Restore frame pointer and return.
5048 __ pop(ebp);
5049 __ ret(0);
5050}
5051
5052
Ben Murdoch086aeea2011-05-13 15:57:08 +01005053// Generate stub code for instanceof.
5054// This code can patch a call site inlined cache of the instance of check,
5055// which looks like this.
5056//
5057// 81 ff XX XX XX XX cmp edi, <the hole, patched to a map>
5058// 75 0a jne <some near label>
5059// b8 XX XX XX XX mov eax, <the hole, patched to either true or false>
5060//
5061// If call site patching is requested the stack will have the delta from the
5062// return address to the cmp instruction just below the return address. This
5063// also means that call site patching can only take place with arguments in
5064// registers. TOS looks like this when call site patching is requested
5065//
5066// esp[0] : return address
5067// esp[4] : delta from return address to cmp instruction
5068//
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005069void InstanceofStub::Generate(MacroAssembler* masm) {
Ben Murdoch086aeea2011-05-13 15:57:08 +01005070 // Call site inlining and patching implies arguments in registers.
5071 ASSERT(HasArgsInRegisters() || !HasCallSiteInlineCheck());
5072
Ben Murdochb0fe1622011-05-05 13:52:32 +01005073 // Fixed register usage throughout the stub.
5074 Register object = eax; // Object (lhs).
5075 Register map = ebx; // Map of the object.
5076 Register function = edx; // Function (rhs).
5077 Register prototype = edi; // Prototype of the function.
5078 Register scratch = ecx;
5079
Ben Murdoch086aeea2011-05-13 15:57:08 +01005080 // Constants describing the call site code to patch.
5081 static const int kDeltaToCmpImmediate = 2;
5082 static const int kDeltaToMov = 8;
5083 static const int kDeltaToMovImmediate = 9;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005084 static const int8_t kCmpEdiOperandByte1 = BitCast<int8_t, uint8_t>(0x3b);
5085 static const int8_t kCmpEdiOperandByte2 = BitCast<int8_t, uint8_t>(0x3d);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005086 static const int8_t kMovEaxImmediateByte = BitCast<int8_t, uint8_t>(0xb8);
5087
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005088 ExternalReference roots_array_start =
5089 ExternalReference::roots_array_start(masm->isolate());
Ben Murdoch086aeea2011-05-13 15:57:08 +01005090
5091 ASSERT_EQ(object.code(), InstanceofStub::left().code());
5092 ASSERT_EQ(function.code(), InstanceofStub::right().code());
5093
Ben Murdochb0fe1622011-05-05 13:52:32 +01005094 // Get the object and function - they are always both needed.
5095 Label slow, not_js_object;
Ben Murdoch086aeea2011-05-13 15:57:08 +01005096 if (!HasArgsInRegisters()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01005097 __ mov(object, Operand(esp, 2 * kPointerSize));
5098 __ mov(function, Operand(esp, 1 * kPointerSize));
5099 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005100
5101 // Check that the left hand is a JS object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005102 __ JumpIfSmi(object, &not_js_object);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005103 __ IsObjectJSObjectType(object, map, scratch, &not_js_object);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005104
Ben Murdoch086aeea2011-05-13 15:57:08 +01005105 // If there is a call site cache don't look in the global cache, but do the
5106 // real lookup and update the call site cache.
5107 if (!HasCallSiteInlineCheck()) {
5108 // Look up the function and the map in the instanceof cache.
Ben Murdoch257744e2011-11-30 15:57:28 +00005109 Label miss;
Ben Murdoch086aeea2011-05-13 15:57:08 +01005110 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005111 __ cmp(function, Operand::StaticArray(scratch,
5112 times_pointer_size,
5113 roots_array_start));
Ben Murdoch257744e2011-11-30 15:57:28 +00005114 __ j(not_equal, &miss, Label::kNear);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005115 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
5116 __ cmp(map, Operand::StaticArray(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005117 scratch, times_pointer_size, roots_array_start));
Ben Murdoch257744e2011-11-30 15:57:28 +00005118 __ j(not_equal, &miss, Label::kNear);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005119 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5120 __ mov(eax, Operand::StaticArray(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005121 scratch, times_pointer_size, roots_array_start));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005122 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
5123 __ bind(&miss);
5124 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005125
Ben Murdochb0fe1622011-05-05 13:52:32 +01005126 // Get the prototype of the function.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005127 __ TryGetFunctionPrototype(function, prototype, scratch, &slow, true);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005128
5129 // Check that the function prototype is a JS object.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005130 __ JumpIfSmi(prototype, &slow);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005131 __ IsObjectJSObjectType(prototype, scratch, scratch, &slow);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005132
Ben Murdoch086aeea2011-05-13 15:57:08 +01005133 // Update the global instanceof or call site inlined cache with the current
5134 // map and function. The cached answer will be set when it is known below.
5135 if (!HasCallSiteInlineCheck()) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01005136 __ mov(scratch, Immediate(Heap::kInstanceofCacheMapRootIndex));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005137 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_array_start),
5138 map);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005139 __ mov(scratch, Immediate(Heap::kInstanceofCacheFunctionRootIndex));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005140 __ mov(Operand::StaticArray(scratch, times_pointer_size, roots_array_start),
Ben Murdochb0fe1622011-05-05 13:52:32 +01005141 function);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005142 } else {
5143 // The constants for the code patching are based on no push instructions
5144 // at the call site.
5145 ASSERT(HasArgsInRegisters());
5146 // Get return address and delta to inlined map check.
5147 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5148 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5149 if (FLAG_debug_code) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005150 __ cmpb(Operand(scratch, 0), kCmpEdiOperandByte1);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005151 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 1)");
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005152 __ cmpb(Operand(scratch, 1), kCmpEdiOperandByte2);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005153 __ Assert(equal, "InstanceofStub unexpected call site cache (cmp 2)");
5154 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005155 __ mov(scratch, Operand(scratch, kDeltaToCmpImmediate));
5156 __ mov(Operand(scratch, 0), map);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005157 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005158
Ben Murdochb0fe1622011-05-05 13:52:32 +01005159 // Loop through the prototype chain of the object looking for the function
5160 // prototype.
5161 __ mov(scratch, FieldOperand(map, Map::kPrototypeOffset));
Ben Murdoch257744e2011-11-30 15:57:28 +00005162 Label loop, is_instance, is_not_instance;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005163 __ bind(&loop);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005164 __ cmp(scratch, prototype);
Ben Murdoch257744e2011-11-30 15:57:28 +00005165 __ j(equal, &is_instance, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01005166 Factory* factory = masm->isolate()->factory();
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005167 __ cmp(scratch, Immediate(factory->null_value()));
Ben Murdoch257744e2011-11-30 15:57:28 +00005168 __ j(equal, &is_not_instance, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005169 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
5170 __ mov(scratch, FieldOperand(scratch, Map::kPrototypeOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005171 __ jmp(&loop);
5172
5173 __ bind(&is_instance);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005174 if (!HasCallSiteInlineCheck()) {
5175 __ Set(eax, Immediate(0));
5176 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5177 __ mov(Operand::StaticArray(scratch,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005178 times_pointer_size, roots_array_start), eax);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005179 } else {
5180 // Get return address and delta to inlined map check.
Steve Block44f0eee2011-05-26 01:26:41 +01005181 __ mov(eax, factory->true_value());
Ben Murdoch086aeea2011-05-13 15:57:08 +01005182 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5183 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5184 if (FLAG_debug_code) {
5185 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
5186 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
5187 }
5188 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
5189 if (!ReturnTrueFalseObject()) {
5190 __ Set(eax, Immediate(0));
5191 }
5192 }
5193 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005194
5195 __ bind(&is_not_instance);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005196 if (!HasCallSiteInlineCheck()) {
5197 __ Set(eax, Immediate(Smi::FromInt(1)));
5198 __ mov(scratch, Immediate(Heap::kInstanceofCacheAnswerRootIndex));
5199 __ mov(Operand::StaticArray(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005200 scratch, times_pointer_size, roots_array_start), eax);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005201 } else {
5202 // Get return address and delta to inlined map check.
Steve Block44f0eee2011-05-26 01:26:41 +01005203 __ mov(eax, factory->false_value());
Ben Murdoch086aeea2011-05-13 15:57:08 +01005204 __ mov(scratch, Operand(esp, 0 * kPointerSize));
5205 __ sub(scratch, Operand(esp, 1 * kPointerSize));
5206 if (FLAG_debug_code) {
5207 __ cmpb(Operand(scratch, kDeltaToMov), kMovEaxImmediateByte);
5208 __ Assert(equal, "InstanceofStub unexpected call site cache (mov)");
5209 }
5210 __ mov(Operand(scratch, kDeltaToMovImmediate), eax);
5211 if (!ReturnTrueFalseObject()) {
5212 __ Set(eax, Immediate(Smi::FromInt(1)));
5213 }
5214 }
5215 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005216
5217 Label object_not_null, object_not_null_or_smi;
5218 __ bind(&not_js_object);
5219 // Before null, smi and string value checks, check that the rhs is a function
5220 // as for a non-function rhs an exception needs to be thrown.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005221 __ JumpIfSmi(function, &slow, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005222 __ CmpObjectType(function, JS_FUNCTION_TYPE, scratch);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005223 __ j(not_equal, &slow, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005224
5225 // Null is not instance of anything.
Steve Block44f0eee2011-05-26 01:26:41 +01005226 __ cmp(object, factory->null_value());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005227 __ j(not_equal, &object_not_null, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005228 __ Set(eax, Immediate(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005229 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005230
5231 __ bind(&object_not_null);
5232 // Smi values is not instance of anything.
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005233 __ JumpIfNotSmi(object, &object_not_null_or_smi, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005234 __ Set(eax, Immediate(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005235 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005236
5237 __ bind(&object_not_null_or_smi);
5238 // String values is not instance of anything.
5239 Condition is_string = masm->IsObjectStringType(object, scratch, scratch);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00005240 __ j(NegateCondition(is_string), &slow, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005241 __ Set(eax, Immediate(Smi::FromInt(1)));
Ben Murdoch086aeea2011-05-13 15:57:08 +01005242 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005243
5244 // Slow-case: Go through the JavaScript implementation.
5245 __ bind(&slow);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005246 if (!ReturnTrueFalseObject()) {
5247 // Tail call the builtin which returns 0 or 1.
5248 if (HasArgsInRegisters()) {
5249 // Push arguments below return address.
5250 __ pop(scratch);
5251 __ push(object);
5252 __ push(function);
5253 __ push(scratch);
5254 }
5255 __ InvokeBuiltin(Builtins::INSTANCE_OF, JUMP_FUNCTION);
5256 } else {
5257 // Call the builtin and convert 0/1 to true/false.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005258 {
5259 FrameScope scope(masm, StackFrame::INTERNAL);
5260 __ push(object);
5261 __ push(function);
5262 __ InvokeBuiltin(Builtins::INSTANCE_OF, CALL_FUNCTION);
5263 }
Ben Murdoch257744e2011-11-30 15:57:28 +00005264 Label true_value, done;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005265 __ test(eax, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00005266 __ j(zero, &true_value, Label::kNear);
Steve Block44f0eee2011-05-26 01:26:41 +01005267 __ mov(eax, factory->false_value());
Ben Murdoch257744e2011-11-30 15:57:28 +00005268 __ jmp(&done, Label::kNear);
Ben Murdoch086aeea2011-05-13 15:57:08 +01005269 __ bind(&true_value);
Steve Block44f0eee2011-05-26 01:26:41 +01005270 __ mov(eax, factory->true_value());
Ben Murdoch086aeea2011-05-13 15:57:08 +01005271 __ bind(&done);
5272 __ ret((HasArgsInRegisters() ? 0 : 2) * kPointerSize);
Ben Murdochb0fe1622011-05-05 13:52:32 +01005273 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005274}
5275
5276
Ben Murdoch086aeea2011-05-13 15:57:08 +01005277Register InstanceofStub::left() { return eax; }
5278
5279
5280Register InstanceofStub::right() { return edx; }
5281
5282
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005283int CompareStub::MinorKey() {
5284 // Encode the three parameters in a unique 16 bit value. To avoid duplicate
5285 // stubs the never NaN NaN condition is only taken into account if the
5286 // condition is equals.
5287 ASSERT(static_cast<unsigned>(cc_) < (1 << 12));
5288 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
5289 return ConditionField::encode(static_cast<unsigned>(cc_))
5290 | RegisterField::encode(false) // lhs_ and rhs_ are not used
5291 | StrictField::encode(strict_)
5292 | NeverNanNanField::encode(cc_ == equal ? never_nan_nan_ : false)
Kristian Monsen0d5e1162010-09-30 15:31:59 +01005293 | IncludeNumberCompareField::encode(include_number_compare_)
5294 | IncludeSmiCompareField::encode(include_smi_compare_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005295}
5296
5297
5298// Unfortunately you have to run without snapshots to see most of these
5299// names in the profile since most compare stubs end up in the snapshot.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005300void CompareStub::PrintName(StringStream* stream) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005301 ASSERT(lhs_.is(no_reg) && rhs_.is(no_reg));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005302 const char* cc_name;
5303 switch (cc_) {
5304 case less: cc_name = "LT"; break;
5305 case greater: cc_name = "GT"; break;
5306 case less_equal: cc_name = "LE"; break;
5307 case greater_equal: cc_name = "GE"; break;
5308 case equal: cc_name = "EQ"; break;
5309 case not_equal: cc_name = "NE"; break;
5310 default: cc_name = "UnknownCondition"; break;
5311 }
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005312 bool is_equality = cc_ == equal || cc_ == not_equal;
5313 stream->Add("CompareStub_%s", cc_name);
5314 if (strict_ && is_equality) stream->Add("_STRICT");
5315 if (never_nan_nan_ && is_equality) stream->Add("_NO_NAN");
5316 if (!include_number_compare_) stream->Add("_NO_NUMBER");
5317 if (!include_smi_compare_) stream->Add("_NO_SMI");
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005318}
5319
5320
5321// -------------------------------------------------------------------------
5322// StringCharCodeAtGenerator
5323
5324void StringCharCodeAtGenerator::GenerateFast(MacroAssembler* masm) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005325 // If the receiver is a smi trigger the non-string case.
5326 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005327 __ JumpIfSmi(object_, receiver_not_string_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005328
5329 // Fetch the instance type of the receiver into result register.
5330 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5331 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5332 // If the receiver is not a string trigger the non-string case.
5333 __ test(result_, Immediate(kIsNotStringMask));
5334 __ j(not_zero, receiver_not_string_);
5335
5336 // If the index is non-smi trigger the non-smi case.
5337 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005338 __ JumpIfNotSmi(index_, &index_not_smi_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005339 __ bind(&got_smi_index_);
5340
5341 // Check for index out of range.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005342 __ cmp(index_, FieldOperand(object_, String::kLengthOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005343 __ j(above_equal, index_out_of_range_);
5344
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005345 __ SmiUntag(index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005346
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005347 Factory* factory = masm->isolate()->factory();
5348 StringCharLoadGenerator::Generate(
5349 masm, factory, object_, index_, result_, &call_runtime_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005350
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005351 __ SmiTag(result_);
5352 __ bind(&exit_);
5353}
5354
5355
5356void StringCharCodeAtGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005357 MacroAssembler* masm,
5358 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005359 __ Abort("Unexpected fallthrough to CharCodeAt slow case");
5360
5361 // Index is not a smi.
5362 __ bind(&index_not_smi_);
5363 // If index is a heap number, try converting it to an integer.
Steve Block44f0eee2011-05-26 01:26:41 +01005364 __ CheckMap(index_,
5365 masm->isolate()->factory()->heap_number_map(),
5366 index_not_number_,
Ben Murdoch257744e2011-11-30 15:57:28 +00005367 DONT_DO_SMI_CHECK);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005368 call_helper.BeforeCall(masm);
5369 __ push(object_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005370 __ push(index_); // Consumed by runtime conversion function.
5371 if (index_flags_ == STRING_INDEX_IS_NUMBER) {
5372 __ CallRuntime(Runtime::kNumberToIntegerMapMinusZero, 1);
5373 } else {
5374 ASSERT(index_flags_ == STRING_INDEX_IS_ARRAY_INDEX);
5375 // NumberToSmi discards numbers that are not exact integers.
5376 __ CallRuntime(Runtime::kNumberToSmi, 1);
5377 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005378 if (!index_.is(eax)) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005379 // Save the conversion result before the pop instructions below
5380 // have a chance to overwrite it.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005381 __ mov(index_, eax);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005382 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005383 __ pop(object_);
5384 // Reload the instance type.
5385 __ mov(result_, FieldOperand(object_, HeapObject::kMapOffset));
5386 __ movzx_b(result_, FieldOperand(result_, Map::kInstanceTypeOffset));
5387 call_helper.AfterCall(masm);
5388 // If index is still not a smi, it must be out of range.
5389 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005390 __ JumpIfNotSmi(index_, index_out_of_range_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005391 // Otherwise, return to the fast path.
5392 __ jmp(&got_smi_index_);
5393
5394 // Call runtime. We get here when the receiver is a string and the
5395 // index is a number, but the code of getting the actual character
5396 // is too complex (e.g., when the string needs to be flattened).
5397 __ bind(&call_runtime_);
5398 call_helper.BeforeCall(masm);
5399 __ push(object_);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005400 __ SmiTag(index_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005401 __ push(index_);
5402 __ CallRuntime(Runtime::kStringCharCodeAt, 2);
5403 if (!result_.is(eax)) {
5404 __ mov(result_, eax);
5405 }
5406 call_helper.AfterCall(masm);
5407 __ jmp(&exit_);
5408
5409 __ Abort("Unexpected fallthrough from CharCodeAt slow case");
5410}
5411
5412
5413// -------------------------------------------------------------------------
5414// StringCharFromCodeGenerator
5415
5416void StringCharFromCodeGenerator::GenerateFast(MacroAssembler* masm) {
5417 // Fast case of Heap::LookupSingleCharacterStringFromCode.
5418 STATIC_ASSERT(kSmiTag == 0);
5419 STATIC_ASSERT(kSmiShiftSize == 0);
5420 ASSERT(IsPowerOf2(String::kMaxAsciiCharCode + 1));
5421 __ test(code_,
5422 Immediate(kSmiTagMask |
5423 ((~String::kMaxAsciiCharCode) << kSmiTagSize)));
Ben Murdoch257744e2011-11-30 15:57:28 +00005424 __ j(not_zero, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005425
Steve Block44f0eee2011-05-26 01:26:41 +01005426 Factory* factory = masm->isolate()->factory();
5427 __ Set(result_, Immediate(factory->single_character_string_cache()));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005428 STATIC_ASSERT(kSmiTag == 0);
5429 STATIC_ASSERT(kSmiTagSize == 1);
5430 STATIC_ASSERT(kSmiShiftSize == 0);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005431 // At this point code register contains smi tagged ASCII char code.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005432 __ mov(result_, FieldOperand(result_,
5433 code_, times_half_pointer_size,
5434 FixedArray::kHeaderSize));
Steve Block44f0eee2011-05-26 01:26:41 +01005435 __ cmp(result_, factory->undefined_value());
Ben Murdoch257744e2011-11-30 15:57:28 +00005436 __ j(equal, &slow_case_);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005437 __ bind(&exit_);
5438}
5439
5440
5441void StringCharFromCodeGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005442 MacroAssembler* masm,
5443 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005444 __ Abort("Unexpected fallthrough to CharFromCode slow case");
5445
5446 __ bind(&slow_case_);
5447 call_helper.BeforeCall(masm);
5448 __ push(code_);
5449 __ CallRuntime(Runtime::kCharFromCode, 1);
5450 if (!result_.is(eax)) {
5451 __ mov(result_, eax);
5452 }
5453 call_helper.AfterCall(masm);
5454 __ jmp(&exit_);
5455
5456 __ Abort("Unexpected fallthrough from CharFromCode slow case");
5457}
5458
5459
5460// -------------------------------------------------------------------------
5461// StringCharAtGenerator
5462
5463void StringCharAtGenerator::GenerateFast(MacroAssembler* masm) {
5464 char_code_at_generator_.GenerateFast(masm);
5465 char_from_code_generator_.GenerateFast(masm);
5466}
5467
5468
5469void StringCharAtGenerator::GenerateSlow(
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005470 MacroAssembler* masm,
5471 const RuntimeCallHelper& call_helper) {
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005472 char_code_at_generator_.GenerateSlow(masm, call_helper);
5473 char_from_code_generator_.GenerateSlow(masm, call_helper);
5474}
5475
5476
5477void StringAddStub::Generate(MacroAssembler* masm) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00005478 Label call_runtime, call_builtin;
Iain Merrick9ac36c92010-09-13 15:29:50 +01005479 Builtins::JavaScript builtin_id = Builtins::ADD;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005480
5481 // Load the two arguments.
5482 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5483 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5484
5485 // Make sure that both arguments are strings if not known in advance.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005486 if (flags_ == NO_STRING_ADD_FLAGS) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00005487 __ JumpIfSmi(eax, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005488 __ CmpObjectType(eax, FIRST_NONSTRING_TYPE, ebx);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005489 __ j(above_equal, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005490
5491 // First argument is a a string, test second.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005492 __ JumpIfSmi(edx, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005493 __ CmpObjectType(edx, FIRST_NONSTRING_TYPE, ebx);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005494 __ j(above_equal, &call_runtime);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005495 } else {
5496 // Here at least one of the arguments is definitely a string.
5497 // We convert the one that is not known to be a string.
5498 if ((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) == 0) {
5499 ASSERT((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) != 0);
5500 GenerateConvertArgument(masm, 2 * kPointerSize, eax, ebx, ecx, edi,
5501 &call_builtin);
5502 builtin_id = Builtins::STRING_ADD_RIGHT;
5503 } else if ((flags_ & NO_STRING_CHECK_RIGHT_IN_STUB) == 0) {
5504 ASSERT((flags_ & NO_STRING_CHECK_LEFT_IN_STUB) != 0);
5505 GenerateConvertArgument(masm, 1 * kPointerSize, edx, ebx, ecx, edi,
5506 &call_builtin);
5507 builtin_id = Builtins::STRING_ADD_LEFT;
5508 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005509 }
5510
5511 // Both arguments are strings.
5512 // eax: first string
5513 // edx: second string
5514 // Check if either of the strings are empty. In that case return the other.
Ben Murdoch257744e2011-11-30 15:57:28 +00005515 Label second_not_zero_length, both_not_zero_length;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005516 __ mov(ecx, FieldOperand(edx, String::kLengthOffset));
5517 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005518 __ test(ecx, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005519 __ j(not_zero, &second_not_zero_length, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005520 // Second string is empty, result is first string which is already in eax.
Steve Block44f0eee2011-05-26 01:26:41 +01005521 Counters* counters = masm->isolate()->counters();
5522 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005523 __ ret(2 * kPointerSize);
5524 __ bind(&second_not_zero_length);
5525 __ mov(ebx, FieldOperand(eax, String::kLengthOffset));
5526 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005527 __ test(ebx, ebx);
Ben Murdoch257744e2011-11-30 15:57:28 +00005528 __ j(not_zero, &both_not_zero_length, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005529 // First string is empty, result is second string which is in edx.
5530 __ mov(eax, edx);
Steve Block44f0eee2011-05-26 01:26:41 +01005531 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005532 __ ret(2 * kPointerSize);
5533
5534 // Both strings are non-empty.
5535 // eax: first string
5536 // ebx: length of first string as a smi
5537 // ecx: length of second string as a smi
5538 // edx: second string
5539 // Look at the length of the result of adding the two strings.
5540 Label string_add_flat_result, longer_than_two;
5541 __ bind(&both_not_zero_length);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005542 __ add(ebx, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005543 STATIC_ASSERT(Smi::kMaxValue == String::kMaxLength);
5544 // Handle exceptionally long strings in the runtime system.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005545 __ j(overflow, &call_runtime);
Steve Block44f0eee2011-05-26 01:26:41 +01005546 // Use the symbol table when adding two one character strings, as it
5547 // helps later optimizations to return a symbol here.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005548 __ cmp(ebx, Immediate(Smi::FromInt(2)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005549 __ j(not_equal, &longer_than_two);
5550
Ben Murdochc7cc0282012-03-05 14:35:55 +00005551 // Check that both strings are non-external ASCII strings.
5552 __ JumpIfNotBothSequentialAsciiStrings(eax, edx, ebx, ecx, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005553
Iain Merrick9ac36c92010-09-13 15:29:50 +01005554 // Get the two characters forming the new string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005555 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5556 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5557
5558 // Try to lookup two character string in symbol table. If it is not found
5559 // just allocate a new one.
Iain Merrick9ac36c92010-09-13 15:29:50 +01005560 Label make_two_character_string, make_two_character_string_no_reload;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005561 StringHelper::GenerateTwoCharacterSymbolTableProbe(
Iain Merrick9ac36c92010-09-13 15:29:50 +01005562 masm, ebx, ecx, eax, edx, edi,
5563 &make_two_character_string_no_reload, &make_two_character_string);
Steve Block44f0eee2011-05-26 01:26:41 +01005564 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005565 __ ret(2 * kPointerSize);
5566
Iain Merrick9ac36c92010-09-13 15:29:50 +01005567 // Allocate a two character string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005568 __ bind(&make_two_character_string);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005569 // Reload the arguments.
5570 __ mov(eax, Operand(esp, 2 * kPointerSize)); // First argument.
5571 __ mov(edx, Operand(esp, 1 * kPointerSize)); // Second argument.
5572 // Get the two characters forming the new string.
5573 __ movzx_b(ebx, FieldOperand(eax, SeqAsciiString::kHeaderSize));
5574 __ movzx_b(ecx, FieldOperand(edx, SeqAsciiString::kHeaderSize));
5575 __ bind(&make_two_character_string_no_reload);
Steve Block44f0eee2011-05-26 01:26:41 +01005576 __ IncrementCounter(counters->string_add_make_two_char(), 1);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005577 __ AllocateAsciiString(eax, 2, edi, edx, &call_runtime);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005578 // Pack both characters in ebx.
5579 __ shl(ecx, kBitsPerByte);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005580 __ or_(ebx, ecx);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005581 // Set the characters in the new string.
5582 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
Steve Block44f0eee2011-05-26 01:26:41 +01005583 __ IncrementCounter(counters->string_add_native(), 1);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005584 __ ret(2 * kPointerSize);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005585
5586 __ bind(&longer_than_two);
5587 // Check if resulting string will be flat.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005588 __ cmp(ebx, Immediate(Smi::FromInt(ConsString::kMinLength)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005589 __ j(below, &string_add_flat_result);
5590
5591 // If result is not supposed to be flat allocate a cons string object. If both
Ben Murdochc7cc0282012-03-05 14:35:55 +00005592 // strings are ASCII the result is an ASCII cons string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005593 Label non_ascii, allocated, ascii_data;
5594 __ mov(edi, FieldOperand(eax, HeapObject::kMapOffset));
5595 __ movzx_b(ecx, FieldOperand(edi, Map::kInstanceTypeOffset));
5596 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
5597 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005598 __ and_(ecx, edi);
Ben Murdoch589d6972011-11-30 16:04:58 +00005599 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
5600 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
5601 __ test(ecx, Immediate(kStringEncodingMask));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005602 __ j(zero, &non_ascii);
5603 __ bind(&ascii_data);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005604 // Allocate an ASCII cons string.
5605 __ AllocateAsciiConsString(ecx, edi, no_reg, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005606 __ bind(&allocated);
5607 // Fill the fields of the cons string.
5608 if (FLAG_debug_code) __ AbortIfNotSmi(ebx);
5609 __ mov(FieldOperand(ecx, ConsString::kLengthOffset), ebx);
5610 __ mov(FieldOperand(ecx, ConsString::kHashFieldOffset),
5611 Immediate(String::kEmptyHashField));
5612 __ mov(FieldOperand(ecx, ConsString::kFirstOffset), eax);
5613 __ mov(FieldOperand(ecx, ConsString::kSecondOffset), edx);
5614 __ mov(eax, ecx);
Steve Block44f0eee2011-05-26 01:26:41 +01005615 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005616 __ ret(2 * kPointerSize);
5617 __ bind(&non_ascii);
5618 // At least one of the strings is two-byte. Check whether it happens
Ben Murdochc7cc0282012-03-05 14:35:55 +00005619 // to contain only ASCII characters.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005620 // ecx: first instance type AND second instance type.
5621 // edi: second instance type.
5622 __ test(ecx, Immediate(kAsciiDataHintMask));
5623 __ j(not_zero, &ascii_data);
5624 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5625 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005626 __ xor_(edi, ecx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005627 STATIC_ASSERT(kAsciiStringTag != 0 && kAsciiDataHintTag != 0);
5628 __ and_(edi, kAsciiStringTag | kAsciiDataHintTag);
5629 __ cmp(edi, kAsciiStringTag | kAsciiDataHintTag);
5630 __ j(equal, &ascii_data);
5631 // Allocate a two byte cons string.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005632 __ AllocateTwoByteConsString(ecx, edi, no_reg, &call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005633 __ jmp(&allocated);
5634
Ben Murdochc7cc0282012-03-05 14:35:55 +00005635 // We cannot encounter sliced strings or cons strings here since:
5636 STATIC_ASSERT(SlicedString::kMinLength >= ConsString::kMinLength);
5637 // Handle creating a flat result from either external or sequential strings.
5638 // Locate the first characters' locations.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005639 // eax: first string
5640 // ebx: length of resulting flat string as a smi
5641 // edx: second string
Ben Murdochc7cc0282012-03-05 14:35:55 +00005642 Label first_prepared, second_prepared;
5643 Label first_is_sequential, second_is_sequential;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005644 __ bind(&string_add_flat_result);
5645 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
5646 __ movzx_b(ecx, FieldOperand(ecx, Map::kInstanceTypeOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +00005647 // ecx: instance type of first string
5648 STATIC_ASSERT(kSeqStringTag == 0);
5649 __ test_b(ecx, kStringRepresentationMask);
5650 __ j(zero, &first_is_sequential, Label::kNear);
5651 // Rule out short external string and load string resource.
5652 STATIC_ASSERT(kShortExternalStringTag != 0);
5653 __ test_b(ecx, kShortExternalStringMask);
5654 __ j(not_zero, &call_runtime);
5655 __ mov(eax, FieldOperand(eax, ExternalString::kResourceDataOffset));
5656 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
5657 __ jmp(&first_prepared, Label::kNear);
5658 __ bind(&first_is_sequential);
5659 __ add(eax, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5660 __ bind(&first_prepared);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005661
Ben Murdochc7cc0282012-03-05 14:35:55 +00005662 __ mov(edi, FieldOperand(edx, HeapObject::kMapOffset));
5663 __ movzx_b(edi, FieldOperand(edi, Map::kInstanceTypeOffset));
5664 // Check whether both strings have same encoding.
5665 // edi: instance type of second string
5666 __ xor_(ecx, edi);
5667 __ test_b(ecx, kStringEncodingMask);
5668 __ j(not_zero, &call_runtime);
5669 STATIC_ASSERT(kSeqStringTag == 0);
5670 __ test_b(edi, kStringRepresentationMask);
5671 __ j(zero, &second_is_sequential, Label::kNear);
5672 // Rule out short external string and load string resource.
5673 STATIC_ASSERT(kShortExternalStringTag != 0);
5674 __ test_b(edi, kShortExternalStringMask);
5675 __ j(not_zero, &call_runtime);
5676 __ mov(edx, FieldOperand(edx, ExternalString::kResourceDataOffset));
5677 STATIC_ASSERT(SeqAsciiString::kHeaderSize == SeqTwoByteString::kHeaderSize);
5678 __ jmp(&second_prepared, Label::kNear);
5679 __ bind(&second_is_sequential);
5680 __ add(edx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
5681 __ bind(&second_prepared);
5682
5683 // Push the addresses of both strings' first characters onto the stack.
5684 __ push(edx);
5685 __ push(eax);
5686
5687 Label non_ascii_string_add_flat_result, call_runtime_drop_two;
5688 // edi: instance type of second string
5689 // First string and second string have the same encoding.
5690 STATIC_ASSERT(kTwoByteStringTag == 0);
5691 __ test_b(edi, kStringEncodingMask);
5692 __ j(zero, &non_ascii_string_add_flat_result);
5693
5694 // Both strings are ASCII strings.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005695 // ebx: length of resulting flat string as a smi
5696 __ SmiUntag(ebx);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005697 __ AllocateAsciiString(eax, ebx, ecx, edx, edi, &call_runtime_drop_two);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005698 // eax: result string
5699 __ mov(ecx, eax);
5700 // Locate first character of result.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005701 __ add(ecx, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Ben Murdochc7cc0282012-03-05 14:35:55 +00005702 // Load first argument's length and first character location. Account for
5703 // values currently on the stack when fetching arguments from it.
5704 __ mov(edx, Operand(esp, 4 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005705 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5706 __ SmiUntag(edi);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005707 __ pop(edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005708 // eax: result string
5709 // ecx: first character of result
5710 // edx: first char of first argument
5711 // edi: length of first argument
5712 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005713 // Load second argument's length and first character location. Account for
5714 // values currently on the stack when fetching arguments from it.
5715 __ mov(edx, Operand(esp, 2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005716 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5717 __ SmiUntag(edi);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005718 __ pop(edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005719 // eax: result string
5720 // ecx: next character of result
5721 // edx: first char of second argument
5722 // edi: length of second argument
5723 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, true);
Steve Block44f0eee2011-05-26 01:26:41 +01005724 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005725 __ ret(2 * kPointerSize);
5726
5727 // Handle creating a flat two byte result.
5728 // eax: first string - known to be two byte
5729 // ebx: length of resulting flat string as a smi
5730 // edx: second string
5731 __ bind(&non_ascii_string_add_flat_result);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005732 // Both strings are two byte strings.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005733 __ SmiUntag(ebx);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005734 __ AllocateTwoByteString(eax, ebx, ecx, edx, edi, &call_runtime_drop_two);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005735 // eax: result string
5736 __ mov(ecx, eax);
5737 // Locate first character of result.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005738 __ add(ecx, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
5739 // Load second argument's length and first character location. Account for
5740 // values currently on the stack when fetching arguments from it.
5741 __ mov(edx, Operand(esp, 4 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005742 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5743 __ SmiUntag(edi);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005744 __ pop(edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005745 // eax: result string
5746 // ecx: first character of result
5747 // edx: first char of first argument
5748 // edi: length of first argument
5749 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005750 // Load second argument's length and first character location. Account for
5751 // values currently on the stack when fetching arguments from it.
5752 __ mov(edx, Operand(esp, 2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005753 __ mov(edi, FieldOperand(edx, String::kLengthOffset));
5754 __ SmiUntag(edi);
Ben Murdochc7cc0282012-03-05 14:35:55 +00005755 __ pop(edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005756 // eax: result string
5757 // ecx: next character of result
5758 // edx: first char of second argument
5759 // edi: length of second argument
5760 StringHelper::GenerateCopyCharacters(masm, ecx, edx, edi, ebx, false);
Steve Block44f0eee2011-05-26 01:26:41 +01005761 __ IncrementCounter(counters->string_add_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005762 __ ret(2 * kPointerSize);
5763
Ben Murdochc7cc0282012-03-05 14:35:55 +00005764 // Recover stack pointer before jumping to runtime.
5765 __ bind(&call_runtime_drop_two);
5766 __ Drop(2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005767 // Just jump to runtime to add the two strings.
Ben Murdochc7cc0282012-03-05 14:35:55 +00005768 __ bind(&call_runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005769 __ TailCallRuntime(Runtime::kStringAdd, 2, 1);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005770
5771 if (call_builtin.is_linked()) {
5772 __ bind(&call_builtin);
5773 __ InvokeBuiltin(builtin_id, JUMP_FUNCTION);
5774 }
5775}
5776
5777
5778void StringAddStub::GenerateConvertArgument(MacroAssembler* masm,
5779 int stack_offset,
5780 Register arg,
5781 Register scratch1,
5782 Register scratch2,
5783 Register scratch3,
5784 Label* slow) {
5785 // First check if the argument is already a string.
5786 Label not_string, done;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005787 __ JumpIfSmi(arg, &not_string);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005788 __ CmpObjectType(arg, FIRST_NONSTRING_TYPE, scratch1);
5789 __ j(below, &done);
5790
5791 // Check the number to string cache.
5792 Label not_cached;
5793 __ bind(&not_string);
5794 // Puts the cached result into scratch1.
5795 NumberToStringStub::GenerateLookupNumberStringCache(masm,
5796 arg,
5797 scratch1,
5798 scratch2,
5799 scratch3,
5800 false,
5801 &not_cached);
5802 __ mov(arg, scratch1);
5803 __ mov(Operand(esp, stack_offset), arg);
5804 __ jmp(&done);
5805
5806 // Check if the argument is a safe string wrapper.
5807 __ bind(&not_cached);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00005808 __ JumpIfSmi(arg, slow);
Iain Merrick9ac36c92010-09-13 15:29:50 +01005809 __ CmpObjectType(arg, JS_VALUE_TYPE, scratch1); // map -> scratch1.
5810 __ j(not_equal, slow);
5811 __ test_b(FieldOperand(scratch1, Map::kBitField2Offset),
5812 1 << Map::kStringWrapperSafeForDefaultValueOf);
5813 __ j(zero, slow);
5814 __ mov(arg, FieldOperand(arg, JSValue::kValueOffset));
5815 __ mov(Operand(esp, stack_offset), arg);
5816
5817 __ bind(&done);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005818}
5819
5820
5821void StringHelper::GenerateCopyCharacters(MacroAssembler* masm,
5822 Register dest,
5823 Register src,
5824 Register count,
5825 Register scratch,
5826 bool ascii) {
Ben Murdoch257744e2011-11-30 15:57:28 +00005827 Label loop;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005828 __ bind(&loop);
5829 // This loop just copies one character at a time, as it is only used for very
5830 // short strings.
5831 if (ascii) {
5832 __ mov_b(scratch, Operand(src, 0));
5833 __ mov_b(Operand(dest, 0), scratch);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005834 __ add(src, Immediate(1));
5835 __ add(dest, Immediate(1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005836 } else {
5837 __ mov_w(scratch, Operand(src, 0));
5838 __ mov_w(Operand(dest, 0), scratch);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005839 __ add(src, Immediate(2));
5840 __ add(dest, Immediate(2));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005841 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005842 __ sub(count, Immediate(1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005843 __ j(not_zero, &loop);
5844}
5845
5846
5847void StringHelper::GenerateCopyCharactersREP(MacroAssembler* masm,
5848 Register dest,
5849 Register src,
5850 Register count,
5851 Register scratch,
5852 bool ascii) {
5853 // Copy characters using rep movs of doublewords.
5854 // The destination is aligned on a 4 byte boundary because we are
5855 // copying to the beginning of a newly allocated string.
5856 ASSERT(dest.is(edi)); // rep movs destination
5857 ASSERT(src.is(esi)); // rep movs source
5858 ASSERT(count.is(ecx)); // rep movs count
5859 ASSERT(!scratch.is(dest));
5860 ASSERT(!scratch.is(src));
5861 ASSERT(!scratch.is(count));
5862
5863 // Nothing to do for zero characters.
5864 Label done;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005865 __ test(count, count);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005866 __ j(zero, &done);
5867
5868 // Make count the number of bytes to copy.
5869 if (!ascii) {
5870 __ shl(count, 1);
5871 }
5872
5873 // Don't enter the rep movs if there are less than 4 bytes to copy.
Ben Murdoch257744e2011-11-30 15:57:28 +00005874 Label last_bytes;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005875 __ test(count, Immediate(~3));
Ben Murdoch257744e2011-11-30 15:57:28 +00005876 __ j(zero, &last_bytes, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005877
5878 // Copy from edi to esi using rep movs instruction.
5879 __ mov(scratch, count);
5880 __ sar(count, 2); // Number of doublewords to copy.
5881 __ cld();
5882 __ rep_movs();
5883
5884 // Find number of bytes left.
5885 __ mov(count, scratch);
5886 __ and_(count, 3);
5887
5888 // Check if there are more bytes to copy.
5889 __ bind(&last_bytes);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005890 __ test(count, count);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005891 __ j(zero, &done);
5892
5893 // Copy remaining characters.
Ben Murdoch257744e2011-11-30 15:57:28 +00005894 Label loop;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005895 __ bind(&loop);
5896 __ mov_b(scratch, Operand(src, 0));
5897 __ mov_b(Operand(dest, 0), scratch);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005898 __ add(src, Immediate(1));
5899 __ add(dest, Immediate(1));
5900 __ sub(count, Immediate(1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005901 __ j(not_zero, &loop);
5902
5903 __ bind(&done);
5904}
5905
5906
5907void StringHelper::GenerateTwoCharacterSymbolTableProbe(MacroAssembler* masm,
5908 Register c1,
5909 Register c2,
5910 Register scratch1,
5911 Register scratch2,
5912 Register scratch3,
Iain Merrick9ac36c92010-09-13 15:29:50 +01005913 Label* not_probed,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005914 Label* not_found) {
5915 // Register scratch3 is the general scratch register in this function.
5916 Register scratch = scratch3;
5917
5918 // Make sure that both characters are not digits as such strings has a
5919 // different hash algorithm. Don't try to look for these in the symbol table.
Ben Murdoch257744e2011-11-30 15:57:28 +00005920 Label not_array_index;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005921 __ mov(scratch, c1);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005922 __ sub(scratch, Immediate(static_cast<int>('0')));
5923 __ cmp(scratch, Immediate(static_cast<int>('9' - '0')));
Ben Murdoch257744e2011-11-30 15:57:28 +00005924 __ j(above, &not_array_index, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005925 __ mov(scratch, c2);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005926 __ sub(scratch, Immediate(static_cast<int>('0')));
5927 __ cmp(scratch, Immediate(static_cast<int>('9' - '0')));
Iain Merrick9ac36c92010-09-13 15:29:50 +01005928 __ j(below_equal, not_probed);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005929
5930 __ bind(&not_array_index);
5931 // Calculate the two character string hash.
5932 Register hash = scratch1;
5933 GenerateHashInit(masm, hash, c1, scratch);
5934 GenerateHashAddCharacter(masm, hash, c2, scratch);
5935 GenerateHashGetHash(masm, hash, scratch);
5936
5937 // Collect the two characters in a register.
5938 Register chars = c1;
5939 __ shl(c2, kBitsPerByte);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005940 __ or_(chars, c2);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005941
5942 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5943 // hash: hash of two character string.
5944
5945 // Load the symbol table.
5946 Register symbol_table = c2;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005947 ExternalReference roots_array_start =
5948 ExternalReference::roots_array_start(masm->isolate());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005949 __ mov(scratch, Immediate(Heap::kSymbolTableRootIndex));
5950 __ mov(symbol_table,
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005951 Operand::StaticArray(scratch, times_pointer_size, roots_array_start));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005952
5953 // Calculate capacity mask from the symbol table capacity.
5954 Register mask = scratch2;
5955 __ mov(mask, FieldOperand(symbol_table, SymbolTable::kCapacityOffset));
5956 __ SmiUntag(mask);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005957 __ sub(mask, Immediate(1));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005958
5959 // Registers
5960 // chars: two character string, char 1 in byte 0 and char 2 in byte 1.
5961 // hash: hash of two character string
5962 // symbol_table: symbol table
5963 // mask: capacity mask
5964 // scratch: -
5965
5966 // Perform a number of probes in the symbol table.
5967 static const int kProbes = 4;
5968 Label found_in_symbol_table;
5969 Label next_probe[kProbes], next_probe_pop_mask[kProbes];
Ben Murdoch692be652012-01-10 18:47:50 +00005970 Register candidate = scratch; // Scratch register contains candidate.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005971 for (int i = 0; i < kProbes; i++) {
5972 // Calculate entry in symbol table.
5973 __ mov(scratch, hash);
5974 if (i > 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005975 __ add(scratch, Immediate(SymbolTable::GetProbeOffset(i)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005976 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005977 __ and_(scratch, mask);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005978
5979 // Load the entry from the symbol table.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005980 STATIC_ASSERT(SymbolTable::kEntrySize == 1);
5981 __ mov(candidate,
5982 FieldOperand(symbol_table,
5983 scratch,
5984 times_pointer_size,
5985 SymbolTable::kElementsStartOffset));
5986
5987 // If entry is undefined no string with this hash can be found.
Steve Block44f0eee2011-05-26 01:26:41 +01005988 Factory* factory = masm->isolate()->factory();
5989 __ cmp(candidate, factory->undefined_value());
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005990 __ j(equal, not_found);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00005991 __ cmp(candidate, factory->the_hole_value());
Steve Block44f0eee2011-05-26 01:26:41 +01005992 __ j(equal, &next_probe[i]);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01005993
5994 // If length is not 2 the string is not a candidate.
5995 __ cmp(FieldOperand(candidate, String::kLengthOffset),
5996 Immediate(Smi::FromInt(2)));
5997 __ j(not_equal, &next_probe[i]);
5998
5999 // As we are out of registers save the mask on the stack and use that
6000 // register as a temporary.
6001 __ push(mask);
6002 Register temp = mask;
6003
Ben Murdochc7cc0282012-03-05 14:35:55 +00006004 // Check that the candidate is a non-external ASCII string.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006005 __ mov(temp, FieldOperand(candidate, HeapObject::kMapOffset));
6006 __ movzx_b(temp, FieldOperand(temp, Map::kInstanceTypeOffset));
6007 __ JumpIfInstanceTypeIsNotSequentialAscii(
6008 temp, temp, &next_probe_pop_mask[i]);
6009
6010 // Check if the two characters match.
6011 __ mov(temp, FieldOperand(candidate, SeqAsciiString::kHeaderSize));
6012 __ and_(temp, 0x0000ffff);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006013 __ cmp(chars, temp);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006014 __ j(equal, &found_in_symbol_table);
6015 __ bind(&next_probe_pop_mask[i]);
6016 __ pop(mask);
6017 __ bind(&next_probe[i]);
6018 }
6019
6020 // No matching 2 character string found by probing.
6021 __ jmp(not_found);
6022
6023 // Scratch register contains result when we fall through to here.
Ben Murdoch692be652012-01-10 18:47:50 +00006024 Register result = candidate;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006025 __ bind(&found_in_symbol_table);
6026 __ pop(mask); // Pop saved mask from the stack.
6027 if (!result.is(eax)) {
6028 __ mov(eax, result);
6029 }
6030}
6031
6032
6033void StringHelper::GenerateHashInit(MacroAssembler* masm,
6034 Register hash,
6035 Register character,
6036 Register scratch) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00006037 // hash = (seed + character) + ((seed + character) << 10);
6038 if (Serializer::enabled()) {
6039 ExternalReference roots_array_start =
6040 ExternalReference::roots_array_start(masm->isolate());
6041 __ mov(scratch, Immediate(Heap::kHashSeedRootIndex));
6042 __ mov(scratch, Operand::StaticArray(scratch,
6043 times_pointer_size,
6044 roots_array_start));
6045 __ SmiUntag(scratch);
6046 __ add(scratch, character);
6047 __ mov(hash, scratch);
6048 __ shl(scratch, 10);
6049 __ add(hash, scratch);
6050 } else {
6051 int32_t seed = masm->isolate()->heap()->HashSeed();
6052 __ lea(scratch, Operand(character, seed));
6053 __ shl(scratch, 10);
6054 __ lea(hash, Operand(scratch, character, times_1, seed));
6055 }
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006056 // hash ^= hash >> 6;
6057 __ mov(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00006058 __ shr(scratch, 6);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006059 __ xor_(hash, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006060}
6061
6062
6063void StringHelper::GenerateHashAddCharacter(MacroAssembler* masm,
6064 Register hash,
6065 Register character,
6066 Register scratch) {
6067 // hash += character;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006068 __ add(hash, character);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006069 // hash += hash << 10;
6070 __ mov(scratch, hash);
6071 __ shl(scratch, 10);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006072 __ add(hash, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006073 // hash ^= hash >> 6;
6074 __ mov(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00006075 __ shr(scratch, 6);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006076 __ xor_(hash, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006077}
6078
6079
6080void StringHelper::GenerateHashGetHash(MacroAssembler* masm,
6081 Register hash,
6082 Register scratch) {
6083 // hash += hash << 3;
6084 __ mov(scratch, hash);
6085 __ shl(scratch, 3);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006086 __ add(hash, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006087 // hash ^= hash >> 11;
6088 __ mov(scratch, hash);
Ben Murdoch692be652012-01-10 18:47:50 +00006089 __ shr(scratch, 11);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006090 __ xor_(hash, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006091 // hash += hash << 15;
6092 __ mov(scratch, hash);
6093 __ shl(scratch, 15);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006094 __ add(hash, scratch);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006095
Ben Murdochc7cc0282012-03-05 14:35:55 +00006096 __ and_(hash, String::kHashBitMask);
Ben Murdoch692be652012-01-10 18:47:50 +00006097
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006098 // if (hash == 0) hash = 27;
Ben Murdoch257744e2011-11-30 15:57:28 +00006099 Label hash_not_zero;
Ben Murdoch257744e2011-11-30 15:57:28 +00006100 __ j(not_zero, &hash_not_zero, Label::kNear);
Ben Murdochc7cc0282012-03-05 14:35:55 +00006101 __ mov(hash, Immediate(StringHasher::kZeroHash));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006102 __ bind(&hash_not_zero);
6103}
6104
6105
6106void SubStringStub::Generate(MacroAssembler* masm) {
6107 Label runtime;
6108
6109 // Stack frame on entry.
6110 // esp[0]: return address
6111 // esp[4]: to
6112 // esp[8]: from
6113 // esp[12]: string
6114
6115 // Make sure first argument is a string.
6116 __ mov(eax, Operand(esp, 3 * kPointerSize));
6117 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006118 __ JumpIfSmi(eax, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006119 Condition is_string = masm->IsObjectStringType(eax, ebx, ebx);
6120 __ j(NegateCondition(is_string), &runtime);
6121
6122 // eax: string
6123 // ebx: instance type
6124
6125 // Calculate length of sub string using the smi values.
6126 Label result_longer_than_two;
6127 __ mov(ecx, Operand(esp, 1 * kPointerSize)); // To index.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006128 __ JumpIfNotSmi(ecx, &runtime);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006129 __ mov(edx, Operand(esp, 2 * kPointerSize)); // From index.
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006130 __ JumpIfNotSmi(edx, &runtime);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006131 __ sub(ecx, edx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006132 __ cmp(ecx, FieldOperand(eax, String::kLengthOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +00006133 Label not_original_string;
6134 __ j(not_equal, &not_original_string, Label::kNear);
6135 Counters* counters = masm->isolate()->counters();
6136 __ IncrementCounter(counters->sub_string_native(), 1);
6137 __ ret(3 * kPointerSize);
6138 __ bind(&not_original_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006139 // Special handling of sub-strings of length 1 and 2. One character strings
6140 // are handled in the runtime system (looked up in the single character
6141 // cache). Two character strings are looked for in the symbol cache.
Ben Murdochc7cc0282012-03-05 14:35:55 +00006142 __ cmp(ecx, Immediate(Smi::FromInt(2)));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006143 __ j(greater, &result_longer_than_two);
6144 __ j(less, &runtime);
6145
6146 // Sub string of length 2 requested.
6147 // eax: string
6148 // ebx: instance type
Ben Murdochc7cc0282012-03-05 14:35:55 +00006149 // ecx: sub string length (smi, value is 2)
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006150 // edx: from index (smi)
6151 __ JumpIfInstanceTypeIsNotSequentialAscii(ebx, ebx, &runtime);
6152
6153 // Get the two characters forming the sub string.
6154 __ SmiUntag(edx); // From index is no longer smi.
6155 __ movzx_b(ebx, FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize));
6156 __ movzx_b(ecx,
6157 FieldOperand(eax, edx, times_1, SeqAsciiString::kHeaderSize + 1));
6158
6159 // Try to lookup two character string in symbol table.
Ben Murdochc7cc0282012-03-05 14:35:55 +00006160 Label combine_two_char, save_two_char;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006161 StringHelper::GenerateTwoCharacterSymbolTableProbe(
Ben Murdochc7cc0282012-03-05 14:35:55 +00006162 masm, ebx, ecx, eax, edx, edi, &combine_two_char, &save_two_char);
6163 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006164 __ ret(3 * kPointerSize);
6165
Ben Murdochc7cc0282012-03-05 14:35:55 +00006166 __ bind(&combine_two_char);
6167 __ shl(ecx, kBitsPerByte);
6168 __ or_(ebx, ecx);
6169 __ bind(&save_two_char);
6170 __ AllocateAsciiString(eax, 2, ecx, edx, &runtime);
6171 __ mov_w(FieldOperand(eax, SeqAsciiString::kHeaderSize), ebx);
6172 __ IncrementCounter(counters->sub_string_native(), 1);
6173 __ ret(3 * kPointerSize);
6174
6175 __ bind(&result_longer_than_two);
6176 // eax: string
6177 // ebx: instance type
6178 // ecx: sub string length (smi)
6179 // edx: from index (smi)
6180 // Deal with different string types: update the index if necessary
6181 // and put the underlying string into edi.
6182 Label underlying_unpacked, sliced_string, seq_or_external_string;
6183 // If the string is not indirect, it can only be sequential or external.
6184 STATIC_ASSERT(kIsIndirectStringMask == (kSlicedStringTag & kConsStringTag));
6185 STATIC_ASSERT(kIsIndirectStringMask != 0);
6186 __ test(ebx, Immediate(kIsIndirectStringMask));
6187 __ j(zero, &seq_or_external_string, Label::kNear);
6188
6189 Factory* factory = masm->isolate()->factory();
6190 __ test(ebx, Immediate(kSlicedNotConsMask));
6191 __ j(not_zero, &sliced_string, Label::kNear);
6192 // Cons string. Check whether it is flat, then fetch first part.
6193 // Flat cons strings have an empty second part.
6194 __ cmp(FieldOperand(eax, ConsString::kSecondOffset),
6195 factory->empty_string());
6196 __ j(not_equal, &runtime);
6197 __ mov(edi, FieldOperand(eax, ConsString::kFirstOffset));
6198 // Update instance type.
6199 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006200 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
Ben Murdochc7cc0282012-03-05 14:35:55 +00006201 __ jmp(&underlying_unpacked, Label::kNear);
6202
6203 __ bind(&sliced_string);
6204 // Sliced string. Fetch parent and adjust start index by offset.
6205 __ add(edx, FieldOperand(eax, SlicedString::kOffsetOffset));
6206 __ mov(edi, FieldOperand(eax, SlicedString::kParentOffset));
6207 // Update instance type.
6208 __ mov(ebx, FieldOperand(edi, HeapObject::kMapOffset));
6209 __ movzx_b(ebx, FieldOperand(ebx, Map::kInstanceTypeOffset));
6210 __ jmp(&underlying_unpacked, Label::kNear);
6211
6212 __ bind(&seq_or_external_string);
6213 // Sequential or external string. Just move string to the expected register.
6214 __ mov(edi, eax);
6215
6216 __ bind(&underlying_unpacked);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006217
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006218 if (FLAG_string_slices) {
6219 Label copy_routine;
Ben Murdochc7cc0282012-03-05 14:35:55 +00006220 // edi: underlying subject string
6221 // ebx: instance type of underlying subject string
6222 // edx: adjusted start index (smi)
6223 // ecx: length (smi)
6224 __ cmp(ecx, Immediate(Smi::FromInt(SlicedString::kMinLength)));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006225 // Short slice. Copy instead of slicing.
6226 __ j(less, &copy_routine);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006227 // Allocate new sliced string. At this point we do not reload the instance
6228 // type including the string encoding because we simply rely on the info
6229 // provided by the original string. It does not matter if the original
6230 // string's encoding is wrong because we always have to recheck encoding of
6231 // the newly created string's parent anyways due to externalized strings.
6232 Label two_byte_slice, set_slice_header;
Ben Murdoch589d6972011-11-30 16:04:58 +00006233 STATIC_ASSERT((kStringEncodingMask & kAsciiStringTag) != 0);
6234 STATIC_ASSERT((kStringEncodingMask & kTwoByteStringTag) == 0);
6235 __ test(ebx, Immediate(kStringEncodingMask));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006236 __ j(zero, &two_byte_slice, Label::kNear);
6237 __ AllocateAsciiSlicedString(eax, ebx, no_reg, &runtime);
6238 __ jmp(&set_slice_header, Label::kNear);
6239 __ bind(&two_byte_slice);
Ben Murdoch589d6972011-11-30 16:04:58 +00006240 __ AllocateTwoByteSlicedString(eax, ebx, no_reg, &runtime);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006241 __ bind(&set_slice_header);
6242 __ mov(FieldOperand(eax, SlicedString::kOffsetOffset), edx);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006243 __ mov(FieldOperand(eax, SlicedString::kLengthOffset), ecx);
6244 __ mov(FieldOperand(eax, SlicedString::kParentOffset), edi);
6245 __ mov(FieldOperand(eax, SlicedString::kHashFieldOffset),
6246 Immediate(String::kEmptyHashField));
Ben Murdochc7cc0282012-03-05 14:35:55 +00006247 __ IncrementCounter(counters->sub_string_native(), 1);
6248 __ ret(3 * kPointerSize);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006249
6250 __ bind(&copy_routine);
Ben Murdoch69a99ed2011-11-30 16:03:39 +00006251 }
6252
Ben Murdochc7cc0282012-03-05 14:35:55 +00006253 // edi: underlying subject string
6254 // ebx: instance type of underlying subject string
6255 // edx: adjusted start index (smi)
6256 // ecx: length (smi)
6257 // The subject string can only be external or sequential string of either
6258 // encoding at this point.
6259 Label two_byte_sequential, runtime_drop_two, sequential_string;
6260 STATIC_ASSERT(kExternalStringTag != 0);
6261 STATIC_ASSERT(kSeqStringTag == 0);
6262 __ test_b(ebx, kExternalStringTag);
6263 __ j(zero, &sequential_string);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006264
Ben Murdochc7cc0282012-03-05 14:35:55 +00006265 // Handle external string.
6266 // Rule out short external strings.
6267 STATIC_CHECK(kShortExternalStringTag != 0);
6268 __ test_b(ebx, kShortExternalStringMask);
6269 __ j(not_zero, &runtime);
6270 __ mov(edi, FieldOperand(edi, ExternalString::kResourceDataOffset));
6271 // Move the pointer so that offset-wise, it looks like a sequential string.
6272 STATIC_ASSERT(SeqTwoByteString::kHeaderSize == SeqAsciiString::kHeaderSize);
6273 __ sub(edi, Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6274
6275 __ bind(&sequential_string);
6276 // Stash away (adjusted) index and (underlying) string.
6277 __ push(edx);
6278 __ push(edi);
6279 __ SmiUntag(ecx);
6280 STATIC_ASSERT((kAsciiStringTag & kStringEncodingMask) != 0);
6281 __ test_b(ebx, kStringEncodingMask);
6282 __ j(zero, &two_byte_sequential);
6283
6284 // Sequential ASCII string. Allocate the result.
6285 __ AllocateAsciiString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006286
6287 // eax: result string
6288 // ecx: result string length
6289 __ mov(edx, esi); // esi used by following code.
6290 // Locate first character of result.
6291 __ mov(edi, eax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006292 __ add(edi, Immediate(SeqAsciiString::kHeaderSize - kHeapObjectTag));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006293 // Load string argument and locate character of sub string start.
Ben Murdochc7cc0282012-03-05 14:35:55 +00006294 __ pop(esi);
6295 __ pop(ebx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006296 __ SmiUntag(ebx);
Ben Murdochc7cc0282012-03-05 14:35:55 +00006297 __ lea(esi, FieldOperand(esi, ebx, times_1, SeqAsciiString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006298
6299 // eax: result string
6300 // ecx: result length
6301 // edx: original value of esi
6302 // edi: first character of result
6303 // esi: character of sub string start
6304 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, true);
6305 __ mov(esi, edx); // Restore esi.
Steve Block44f0eee2011-05-26 01:26:41 +01006306 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006307 __ ret(3 * kPointerSize);
6308
Ben Murdochc7cc0282012-03-05 14:35:55 +00006309 __ bind(&two_byte_sequential);
6310 // Sequential two-byte string. Allocate the result.
6311 __ AllocateTwoByteString(eax, ecx, ebx, edx, edi, &runtime_drop_two);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006312
6313 // eax: result string
6314 // ecx: result string length
6315 __ mov(edx, esi); // esi used by following code.
6316 // Locate first character of result.
6317 __ mov(edi, eax);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006318 __ add(edi,
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006319 Immediate(SeqTwoByteString::kHeaderSize - kHeapObjectTag));
6320 // Load string argument and locate character of sub string start.
Ben Murdochc7cc0282012-03-05 14:35:55 +00006321 __ pop(esi);
6322 __ pop(ebx);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006323 // As from is a smi it is 2 times the value which matches the size of a two
6324 // byte character.
6325 STATIC_ASSERT(kSmiTag == 0);
6326 STATIC_ASSERT(kSmiTagSize + kSmiShiftSize == 1);
Ben Murdochc7cc0282012-03-05 14:35:55 +00006327 __ lea(esi, FieldOperand(esi, ebx, times_1, SeqTwoByteString::kHeaderSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006328
6329 // eax: result string
6330 // ecx: result length
6331 // edx: original value of esi
6332 // edi: first character of result
6333 // esi: character of sub string start
6334 StringHelper::GenerateCopyCharactersREP(masm, edi, esi, ecx, ebx, false);
6335 __ mov(esi, edx); // Restore esi.
Steve Block44f0eee2011-05-26 01:26:41 +01006336 __ IncrementCounter(counters->sub_string_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006337 __ ret(3 * kPointerSize);
6338
Ben Murdochc7cc0282012-03-05 14:35:55 +00006339 // Drop pushed values on the stack before tail call.
6340 __ bind(&runtime_drop_two);
6341 __ Drop(2);
6342
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006343 // Just jump to runtime to create the sub string.
6344 __ bind(&runtime);
6345 __ TailCallRuntime(Runtime::kSubString, 3, 1);
6346}
6347
6348
Ben Murdoch257744e2011-11-30 15:57:28 +00006349void StringCompareStub::GenerateFlatAsciiStringEquals(MacroAssembler* masm,
6350 Register left,
6351 Register right,
6352 Register scratch1,
6353 Register scratch2) {
6354 Register length = scratch1;
6355
6356 // Compare lengths.
6357 Label strings_not_equal, check_zero_length;
6358 __ mov(length, FieldOperand(left, String::kLengthOffset));
6359 __ cmp(length, FieldOperand(right, String::kLengthOffset));
6360 __ j(equal, &check_zero_length, Label::kNear);
6361 __ bind(&strings_not_equal);
6362 __ Set(eax, Immediate(Smi::FromInt(NOT_EQUAL)));
6363 __ ret(0);
6364
6365 // Check if the length is zero.
6366 Label compare_chars;
6367 __ bind(&check_zero_length);
6368 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006369 __ test(length, length);
Ben Murdoch257744e2011-11-30 15:57:28 +00006370 __ j(not_zero, &compare_chars, Label::kNear);
6371 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6372 __ ret(0);
6373
6374 // Compare characters.
6375 __ bind(&compare_chars);
6376 GenerateAsciiCharsCompareLoop(masm, left, right, length, scratch2,
6377 &strings_not_equal, Label::kNear);
6378
6379 // Characters are equal.
6380 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6381 __ ret(0);
6382}
6383
6384
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006385void StringCompareStub::GenerateCompareFlatAsciiStrings(MacroAssembler* masm,
6386 Register left,
6387 Register right,
6388 Register scratch1,
6389 Register scratch2,
6390 Register scratch3) {
Steve Block44f0eee2011-05-26 01:26:41 +01006391 Counters* counters = masm->isolate()->counters();
6392 __ IncrementCounter(counters->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006393
6394 // Find minimum length.
Ben Murdoch257744e2011-11-30 15:57:28 +00006395 Label left_shorter;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006396 __ mov(scratch1, FieldOperand(left, String::kLengthOffset));
6397 __ mov(scratch3, scratch1);
6398 __ sub(scratch3, FieldOperand(right, String::kLengthOffset));
6399
6400 Register length_delta = scratch3;
6401
Ben Murdoch257744e2011-11-30 15:57:28 +00006402 __ j(less_equal, &left_shorter, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006403 // Right string is shorter. Change scratch1 to be length of right string.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006404 __ sub(scratch1, length_delta);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006405 __ bind(&left_shorter);
6406
6407 Register min_length = scratch1;
6408
6409 // If either length is zero, just compare lengths.
Ben Murdoch257744e2011-11-30 15:57:28 +00006410 Label compare_lengths;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006411 __ test(min_length, min_length);
Ben Murdoch257744e2011-11-30 15:57:28 +00006412 __ j(zero, &compare_lengths, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006413
Ben Murdoch257744e2011-11-30 15:57:28 +00006414 // Compare characters.
6415 Label result_not_equal;
6416 GenerateAsciiCharsCompareLoop(masm, left, right, min_length, scratch2,
6417 &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006418
6419 // Compare lengths - strings up to min-length are equal.
6420 __ bind(&compare_lengths);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006421 __ test(length_delta, length_delta);
Ben Murdoch257744e2011-11-30 15:57:28 +00006422 __ j(not_zero, &result_not_equal, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006423
6424 // Result is EQUAL.
6425 STATIC_ASSERT(EQUAL == 0);
6426 STATIC_ASSERT(kSmiTag == 0);
6427 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6428 __ ret(0);
6429
Ben Murdoch257744e2011-11-30 15:57:28 +00006430 Label result_greater;
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006431 __ bind(&result_not_equal);
Ben Murdoch257744e2011-11-30 15:57:28 +00006432 __ j(greater, &result_greater, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006433
6434 // Result is LESS.
6435 __ Set(eax, Immediate(Smi::FromInt(LESS)));
6436 __ ret(0);
6437
6438 // Result is GREATER.
6439 __ bind(&result_greater);
6440 __ Set(eax, Immediate(Smi::FromInt(GREATER)));
6441 __ ret(0);
6442}
6443
6444
Ben Murdoch257744e2011-11-30 15:57:28 +00006445void StringCompareStub::GenerateAsciiCharsCompareLoop(
6446 MacroAssembler* masm,
6447 Register left,
6448 Register right,
6449 Register length,
6450 Register scratch,
6451 Label* chars_not_equal,
6452 Label::Distance chars_not_equal_near) {
6453 // Change index to run from -length to -1 by adding length to string
6454 // start. This means that loop ends when index reaches zero, which
6455 // doesn't need an additional compare.
6456 __ SmiUntag(length);
6457 __ lea(left,
6458 FieldOperand(left, length, times_1, SeqAsciiString::kHeaderSize));
6459 __ lea(right,
6460 FieldOperand(right, length, times_1, SeqAsciiString::kHeaderSize));
6461 __ neg(length);
6462 Register index = length; // index = -length;
6463
6464 // Compare loop.
6465 Label loop;
6466 __ bind(&loop);
6467 __ mov_b(scratch, Operand(left, index, times_1, 0));
6468 __ cmpb(scratch, Operand(right, index, times_1, 0));
6469 __ j(not_equal, chars_not_equal, chars_not_equal_near);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006470 __ add(index, Immediate(1));
Ben Murdoch257744e2011-11-30 15:57:28 +00006471 __ j(not_zero, &loop);
6472}
6473
6474
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006475void StringCompareStub::Generate(MacroAssembler* masm) {
6476 Label runtime;
6477
6478 // Stack frame on entry.
6479 // esp[0]: return address
6480 // esp[4]: right string
6481 // esp[8]: left string
6482
6483 __ mov(edx, Operand(esp, 2 * kPointerSize)); // left
6484 __ mov(eax, Operand(esp, 1 * kPointerSize)); // right
6485
Ben Murdoch257744e2011-11-30 15:57:28 +00006486 Label not_same;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006487 __ cmp(edx, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00006488 __ j(not_equal, &not_same, Label::kNear);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006489 STATIC_ASSERT(EQUAL == 0);
6490 STATIC_ASSERT(kSmiTag == 0);
6491 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
Steve Block44f0eee2011-05-26 01:26:41 +01006492 __ IncrementCounter(masm->isolate()->counters()->string_compare_native(), 1);
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006493 __ ret(2 * kPointerSize);
6494
6495 __ bind(&not_same);
6496
Ben Murdochc7cc0282012-03-05 14:35:55 +00006497 // Check that both objects are sequential ASCII strings.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006498 __ JumpIfNotBothSequentialAsciiStrings(edx, eax, ecx, ebx, &runtime);
6499
Ben Murdochc7cc0282012-03-05 14:35:55 +00006500 // Compare flat ASCII strings.
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006501 // Drop arguments from the stack.
6502 __ pop(ecx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006503 __ add(esp, Immediate(2 * kPointerSize));
Kristian Monsen80d68ea2010-09-08 11:05:35 +01006504 __ push(ecx);
6505 GenerateCompareFlatAsciiStrings(masm, edx, eax, ecx, ebx, edi);
6506
6507 // Call the runtime; it returns -1 (less), 0 (equal), or 1 (greater)
6508 // tagged as a small integer.
6509 __ bind(&runtime);
6510 __ TailCallRuntime(Runtime::kStringCompare, 2, 1);
6511}
6512
Ben Murdochb0fe1622011-05-05 13:52:32 +01006513
Ben Murdochb0fe1622011-05-05 13:52:32 +01006514void ICCompareStub::GenerateSmis(MacroAssembler* masm) {
6515 ASSERT(state_ == CompareIC::SMIS);
Ben Murdoch257744e2011-11-30 15:57:28 +00006516 Label miss;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006517 __ mov(ecx, edx);
6518 __ or_(ecx, eax);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006519 __ JumpIfNotSmi(ecx, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006520
6521 if (GetCondition() == equal) {
6522 // For equality we do not care about the sign of the result.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006523 __ sub(eax, edx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006524 } else {
Ben Murdoch257744e2011-11-30 15:57:28 +00006525 Label done;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006526 __ sub(edx, eax);
Ben Murdoch257744e2011-11-30 15:57:28 +00006527 __ j(no_overflow, &done, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006528 // Correct sign of result in case of overflow.
6529 __ not_(edx);
6530 __ bind(&done);
6531 __ mov(eax, edx);
6532 }
6533 __ ret(0);
6534
6535 __ bind(&miss);
6536 GenerateMiss(masm);
6537}
6538
6539
6540void ICCompareStub::GenerateHeapNumbers(MacroAssembler* masm) {
6541 ASSERT(state_ == CompareIC::HEAP_NUMBERS);
6542
Ben Murdoch257744e2011-11-30 15:57:28 +00006543 Label generic_stub;
6544 Label unordered;
6545 Label miss;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006546 __ mov(ecx, edx);
6547 __ and_(ecx, eax);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006548 __ JumpIfSmi(ecx, &generic_stub, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006549
6550 __ CmpObjectType(eax, HEAP_NUMBER_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00006551 __ j(not_equal, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006552 __ CmpObjectType(edx, HEAP_NUMBER_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00006553 __ j(not_equal, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006554
6555 // Inlining the double comparison and falling back to the general compare
6556 // stub if NaN is involved or SS2 or CMOV is unsupported.
Ben Murdoch8b112d22011-06-08 16:22:53 +01006557 if (CpuFeatures::IsSupported(SSE2) && CpuFeatures::IsSupported(CMOV)) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01006558 CpuFeatures::Scope scope1(SSE2);
6559 CpuFeatures::Scope scope2(CMOV);
6560
6561 // Load left and right operand
6562 __ movdbl(xmm0, FieldOperand(edx, HeapNumber::kValueOffset));
6563 __ movdbl(xmm1, FieldOperand(eax, HeapNumber::kValueOffset));
6564
6565 // Compare operands
6566 __ ucomisd(xmm0, xmm1);
6567
6568 // Don't base result on EFLAGS when a NaN is involved.
Ben Murdoch257744e2011-11-30 15:57:28 +00006569 __ j(parity_even, &unordered, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006570
6571 // Return a result of -1, 0, or 1, based on EFLAGS.
6572 // Performing mov, because xor would destroy the flag register.
6573 __ mov(eax, 0); // equal
6574 __ mov(ecx, Immediate(Smi::FromInt(1)));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006575 __ cmov(above, eax, ecx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006576 __ mov(ecx, Immediate(Smi::FromInt(-1)));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006577 __ cmov(below, eax, ecx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006578 __ ret(0);
6579
6580 __ bind(&unordered);
6581 }
6582
6583 CompareStub stub(GetCondition(), strict(), NO_COMPARE_FLAGS);
6584 __ bind(&generic_stub);
6585 __ jmp(stub.GetCode(), RelocInfo::CODE_TARGET);
6586
6587 __ bind(&miss);
6588 GenerateMiss(masm);
6589}
6590
6591
Ben Murdoch257744e2011-11-30 15:57:28 +00006592void ICCompareStub::GenerateSymbols(MacroAssembler* masm) {
6593 ASSERT(state_ == CompareIC::SYMBOLS);
6594 ASSERT(GetCondition() == equal);
6595
6596 // Registers containing left and right operands respectively.
6597 Register left = edx;
6598 Register right = eax;
6599 Register tmp1 = ecx;
6600 Register tmp2 = ebx;
6601
6602 // Check that both operands are heap objects.
6603 Label miss;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006604 __ mov(tmp1, left);
Ben Murdoch257744e2011-11-30 15:57:28 +00006605 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006606 __ and_(tmp1, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006607 __ JumpIfSmi(tmp1, &miss, Label::kNear);
Ben Murdoch257744e2011-11-30 15:57:28 +00006608
6609 // Check that both operands are symbols.
6610 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
6611 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
6612 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
6613 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
6614 STATIC_ASSERT(kSymbolTag != 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006615 __ and_(tmp1, tmp2);
Ben Murdoch257744e2011-11-30 15:57:28 +00006616 __ test(tmp1, Immediate(kIsSymbolMask));
6617 __ j(zero, &miss, Label::kNear);
6618
6619 // Symbols are compared by identity.
6620 Label done;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006621 __ cmp(left, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00006622 // Make sure eax is non-zero. At this point input operands are
6623 // guaranteed to be non-zero.
6624 ASSERT(right.is(eax));
6625 __ j(not_equal, &done, Label::kNear);
6626 STATIC_ASSERT(EQUAL == 0);
6627 STATIC_ASSERT(kSmiTag == 0);
6628 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6629 __ bind(&done);
6630 __ ret(0);
6631
6632 __ bind(&miss);
6633 GenerateMiss(masm);
6634}
6635
6636
6637void ICCompareStub::GenerateStrings(MacroAssembler* masm) {
6638 ASSERT(state_ == CompareIC::STRINGS);
6639 ASSERT(GetCondition() == equal);
6640 Label miss;
6641
6642 // Registers containing left and right operands respectively.
6643 Register left = edx;
6644 Register right = eax;
6645 Register tmp1 = ecx;
6646 Register tmp2 = ebx;
6647 Register tmp3 = edi;
6648
6649 // Check that both operands are heap objects.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006650 __ mov(tmp1, left);
Ben Murdoch257744e2011-11-30 15:57:28 +00006651 STATIC_ASSERT(kSmiTag == 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006652 __ and_(tmp1, right);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006653 __ JumpIfSmi(tmp1, &miss);
Ben Murdoch257744e2011-11-30 15:57:28 +00006654
6655 // Check that both operands are strings. This leaves the instance
6656 // types loaded in tmp1 and tmp2.
6657 __ mov(tmp1, FieldOperand(left, HeapObject::kMapOffset));
6658 __ mov(tmp2, FieldOperand(right, HeapObject::kMapOffset));
6659 __ movzx_b(tmp1, FieldOperand(tmp1, Map::kInstanceTypeOffset));
6660 __ movzx_b(tmp2, FieldOperand(tmp2, Map::kInstanceTypeOffset));
6661 __ mov(tmp3, tmp1);
6662 STATIC_ASSERT(kNotStringTag != 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006663 __ or_(tmp3, tmp2);
Ben Murdoch257744e2011-11-30 15:57:28 +00006664 __ test(tmp3, Immediate(kIsNotStringMask));
6665 __ j(not_zero, &miss);
6666
6667 // Fast check for identical strings.
6668 Label not_same;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006669 __ cmp(left, right);
Ben Murdoch257744e2011-11-30 15:57:28 +00006670 __ j(not_equal, &not_same, Label::kNear);
6671 STATIC_ASSERT(EQUAL == 0);
6672 STATIC_ASSERT(kSmiTag == 0);
6673 __ Set(eax, Immediate(Smi::FromInt(EQUAL)));
6674 __ ret(0);
6675
6676 // Handle not identical strings.
6677 __ bind(&not_same);
6678
6679 // Check that both strings are symbols. If they are, we're done
6680 // because we already know they are not identical.
6681 Label do_compare;
6682 STATIC_ASSERT(kSymbolTag != 0);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006683 __ and_(tmp1, tmp2);
Ben Murdoch257744e2011-11-30 15:57:28 +00006684 __ test(tmp1, Immediate(kIsSymbolMask));
6685 __ j(zero, &do_compare, Label::kNear);
6686 // Make sure eax is non-zero. At this point input operands are
6687 // guaranteed to be non-zero.
6688 ASSERT(right.is(eax));
6689 __ ret(0);
6690
6691 // Check that both strings are sequential ASCII.
6692 Label runtime;
6693 __ bind(&do_compare);
6694 __ JumpIfNotBothSequentialAsciiStrings(left, right, tmp1, tmp2, &runtime);
6695
6696 // Compare flat ASCII strings. Returns when done.
6697 StringCompareStub::GenerateFlatAsciiStringEquals(
6698 masm, left, right, tmp1, tmp2);
6699
6700 // Handle more complex cases in runtime.
6701 __ bind(&runtime);
6702 __ pop(tmp1); // Return address.
6703 __ push(left);
6704 __ push(right);
6705 __ push(tmp1);
6706 __ TailCallRuntime(Runtime::kStringEquals, 2, 1);
6707
6708 __ bind(&miss);
6709 GenerateMiss(masm);
6710}
6711
6712
Ben Murdochb0fe1622011-05-05 13:52:32 +01006713void ICCompareStub::GenerateObjects(MacroAssembler* masm) {
6714 ASSERT(state_ == CompareIC::OBJECTS);
Ben Murdoch257744e2011-11-30 15:57:28 +00006715 Label miss;
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006716 __ mov(ecx, edx);
6717 __ and_(ecx, eax);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00006718 __ JumpIfSmi(ecx, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006719
6720 __ CmpObjectType(eax, JS_OBJECT_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00006721 __ j(not_equal, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006722 __ CmpObjectType(edx, JS_OBJECT_TYPE, ecx);
Ben Murdoch257744e2011-11-30 15:57:28 +00006723 __ j(not_equal, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006724
6725 ASSERT(GetCondition() == equal);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006726 __ sub(eax, edx);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006727 __ ret(0);
6728
6729 __ bind(&miss);
6730 GenerateMiss(masm);
6731}
6732
6733
Ben Murdochc7cc0282012-03-05 14:35:55 +00006734void ICCompareStub::GenerateKnownObjects(MacroAssembler* masm) {
6735 Label miss;
6736 __ mov(ecx, edx);
6737 __ and_(ecx, eax);
6738 __ JumpIfSmi(ecx, &miss, Label::kNear);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006739
Ben Murdochc7cc0282012-03-05 14:35:55 +00006740 __ mov(ecx, FieldOperand(eax, HeapObject::kMapOffset));
6741 __ mov(ebx, FieldOperand(edx, HeapObject::kMapOffset));
6742 __ cmp(ecx, known_map_);
6743 __ j(not_equal, &miss, Label::kNear);
6744 __ cmp(ebx, known_map_);
6745 __ j(not_equal, &miss, Label::kNear);
6746
6747 __ sub(eax, edx);
6748 __ ret(0);
6749
6750 __ bind(&miss);
6751 GenerateMiss(masm);
6752}
6753
6754
6755void ICCompareStub::GenerateMiss(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006756 {
6757 // Call the runtime system in a fresh internal frame.
6758 ExternalReference miss = ExternalReference(IC_Utility(IC::kCompareIC_Miss),
6759 masm->isolate());
6760 FrameScope scope(masm, StackFrame::INTERNAL);
Ben Murdochc7cc0282012-03-05 14:35:55 +00006761 __ push(edx); // Preserve edx and eax.
6762 __ push(eax);
6763 __ push(edx); // And also use them as the arguments.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006764 __ push(eax);
6765 __ push(Immediate(Smi::FromInt(op_)));
6766 __ CallExternalReference(miss, 3);
Ben Murdochc7cc0282012-03-05 14:35:55 +00006767 // Compute the entry point of the rewritten stub.
6768 __ lea(edi, FieldOperand(eax, Code::kHeaderSize));
6769 __ pop(eax);
6770 __ pop(edx);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006771 }
Ben Murdochb0fe1622011-05-05 13:52:32 +01006772
Ben Murdochb0fe1622011-05-05 13:52:32 +01006773 // Do a tail call to the rewritten stub.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006774 __ jmp(edi);
Ben Murdochb0fe1622011-05-05 13:52:32 +01006775}
6776
6777
Ben Murdoch257744e2011-11-30 15:57:28 +00006778// Helper function used to check that the dictionary doesn't contain
6779// the property. This function may return false negatives, so miss_label
6780// must always call a backup property check that is complete.
6781// This function is safe to call if the receiver has fast properties.
6782// Name must be a symbol and receiver must be a heap object.
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006783void StringDictionaryLookupStub::GenerateNegativeLookup(MacroAssembler* masm,
6784 Label* miss,
6785 Label* done,
6786 Register properties,
6787 Handle<String> name,
6788 Register r0) {
Ben Murdoch257744e2011-11-30 15:57:28 +00006789 ASSERT(name->IsSymbol());
6790
6791 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6792 // not equal to the name and kProbes-th slot is not used (its name is the
6793 // undefined value), it guarantees the hash table doesn't contain the
6794 // property. It's true even if some slots represent deleted properties
6795 // (their names are the null value).
6796 for (int i = 0; i < kInlinedProbes; i++) {
6797 // Compute the masked index: (hash + i + i * i) & mask.
6798 Register index = r0;
6799 // Capacity is smi 2^n.
6800 __ mov(index, FieldOperand(properties, kCapacityOffset));
6801 __ dec(index);
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006802 __ and_(index,
6803 Immediate(Smi::FromInt(name->Hash() +
Ben Murdoch257744e2011-11-30 15:57:28 +00006804 StringDictionary::GetProbeOffset(i))));
6805
6806 // Scale the index by multiplying by the entry size.
6807 ASSERT(StringDictionary::kEntrySize == 3);
6808 __ lea(index, Operand(index, index, times_2, 0)); // index *= 3.
6809 Register entity_name = r0;
6810 // Having undefined at this place means the name is not contained.
6811 ASSERT_EQ(kSmiTagSize, 1);
6812 __ mov(entity_name, Operand(properties, index, times_half_pointer_size,
6813 kElementsStartOffset - kHeapObjectTag));
6814 __ cmp(entity_name, masm->isolate()->factory()->undefined_value());
6815 __ j(equal, done);
6816
6817 // Stop if found the property.
6818 __ cmp(entity_name, Handle<String>(name));
6819 __ j(equal, miss);
6820
6821 // Check if the entry name is not a symbol.
6822 __ mov(entity_name, FieldOperand(entity_name, HeapObject::kMapOffset));
6823 __ test_b(FieldOperand(entity_name, Map::kInstanceTypeOffset),
6824 kIsSymbolMask);
6825 __ j(zero, miss);
6826 }
6827
6828 StringDictionaryLookupStub stub(properties,
6829 r0,
6830 r0,
6831 StringDictionaryLookupStub::NEGATIVE_LOOKUP);
6832 __ push(Immediate(Handle<Object>(name)));
6833 __ push(Immediate(name->Hash()));
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006834 __ CallStub(&stub);
6835 __ test(r0, r0);
Ben Murdoch257744e2011-11-30 15:57:28 +00006836 __ j(not_zero, miss);
6837 __ jmp(done);
Ben Murdoch257744e2011-11-30 15:57:28 +00006838}
6839
6840
6841// Probe the string dictionary in the |elements| register. Jump to the
6842// |done| label if a property with the given name is found leaving the
6843// index into the dictionary in |r0|. Jump to the |miss| label
6844// otherwise.
6845void StringDictionaryLookupStub::GeneratePositiveLookup(MacroAssembler* masm,
6846 Label* miss,
6847 Label* done,
6848 Register elements,
6849 Register name,
6850 Register r0,
6851 Register r1) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006852 ASSERT(!elements.is(r0));
6853 ASSERT(!elements.is(r1));
6854 ASSERT(!name.is(r0));
6855 ASSERT(!name.is(r1));
6856
Ben Murdoch257744e2011-11-30 15:57:28 +00006857 // Assert that name contains a string.
6858 if (FLAG_debug_code) __ AbortIfNotString(name);
6859
6860 __ mov(r1, FieldOperand(elements, kCapacityOffset));
6861 __ shr(r1, kSmiTagSize); // convert smi to int
6862 __ dec(r1);
6863
6864 // Generate an unrolled loop that performs a few probes before
6865 // giving up. Measurements done on Gmail indicate that 2 probes
6866 // cover ~93% of loads from dictionaries.
6867 for (int i = 0; i < kInlinedProbes; i++) {
6868 // Compute the masked index: (hash + i + i * i) & mask.
6869 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6870 __ shr(r0, String::kHashShift);
6871 if (i > 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006872 __ add(r0, Immediate(StringDictionary::GetProbeOffset(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +00006873 }
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006874 __ and_(r0, r1);
Ben Murdoch257744e2011-11-30 15:57:28 +00006875
6876 // Scale the index by multiplying by the entry size.
6877 ASSERT(StringDictionary::kEntrySize == 3);
6878 __ lea(r0, Operand(r0, r0, times_2, 0)); // r0 = r0 * 3
6879
6880 // Check if the key is identical to the name.
6881 __ cmp(name, Operand(elements,
6882 r0,
6883 times_4,
6884 kElementsStartOffset - kHeapObjectTag));
6885 __ j(equal, done);
6886 }
6887
6888 StringDictionaryLookupStub stub(elements,
6889 r1,
6890 r0,
6891 POSITIVE_LOOKUP);
6892 __ push(name);
6893 __ mov(r0, FieldOperand(name, String::kHashFieldOffset));
6894 __ shr(r0, String::kHashShift);
6895 __ push(r0);
6896 __ CallStub(&stub);
6897
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006898 __ test(r1, r1);
Ben Murdoch257744e2011-11-30 15:57:28 +00006899 __ j(zero, miss);
6900 __ jmp(done);
6901}
6902
6903
6904void StringDictionaryLookupStub::Generate(MacroAssembler* masm) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006905 // This stub overrides SometimesSetsUpAFrame() to return false. That means
6906 // we cannot call anything that could cause a GC from this stub.
Ben Murdoch257744e2011-11-30 15:57:28 +00006907 // Stack frame on entry:
6908 // esp[0 * kPointerSize]: return address.
6909 // esp[1 * kPointerSize]: key's hash.
6910 // esp[2 * kPointerSize]: key.
6911 // Registers:
6912 // dictionary_: StringDictionary to probe.
6913 // result_: used as scratch.
6914 // index_: will hold an index of entry if lookup is successful.
6915 // might alias with result_.
6916 // Returns:
6917 // result_ is zero if lookup failed, non zero otherwise.
6918
6919 Label in_dictionary, maybe_in_dictionary, not_in_dictionary;
6920
6921 Register scratch = result_;
6922
6923 __ mov(scratch, FieldOperand(dictionary_, kCapacityOffset));
6924 __ dec(scratch);
6925 __ SmiUntag(scratch);
6926 __ push(scratch);
6927
6928 // If names of slots in range from 1 to kProbes - 1 for the hash value are
6929 // not equal to the name and kProbes-th slot is not used (its name is the
6930 // undefined value), it guarantees the hash table doesn't contain the
6931 // property. It's true even if some slots represent deleted properties
6932 // (their names are the null value).
6933 for (int i = kInlinedProbes; i < kTotalProbes; i++) {
6934 // Compute the masked index: (hash + i + i * i) & mask.
6935 __ mov(scratch, Operand(esp, 2 * kPointerSize));
6936 if (i > 0) {
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006937 __ add(scratch, Immediate(StringDictionary::GetProbeOffset(i)));
Ben Murdoch257744e2011-11-30 15:57:28 +00006938 }
6939 __ and_(scratch, Operand(esp, 0));
6940
6941 // Scale the index by multiplying by the entry size.
6942 ASSERT(StringDictionary::kEntrySize == 3);
6943 __ lea(index_, Operand(scratch, scratch, times_2, 0)); // index *= 3.
6944
6945 // Having undefined at this place means the name is not contained.
6946 ASSERT_EQ(kSmiTagSize, 1);
6947 __ mov(scratch, Operand(dictionary_,
6948 index_,
6949 times_pointer_size,
6950 kElementsStartOffset - kHeapObjectTag));
6951 __ cmp(scratch, masm->isolate()->factory()->undefined_value());
6952 __ j(equal, &not_in_dictionary);
6953
6954 // Stop if found the property.
6955 __ cmp(scratch, Operand(esp, 3 * kPointerSize));
6956 __ j(equal, &in_dictionary);
6957
6958 if (i != kTotalProbes - 1 && mode_ == NEGATIVE_LOOKUP) {
6959 // If we hit a non symbol key during negative lookup
6960 // we have to bailout as this key might be equal to the
6961 // key we are looking for.
6962
6963 // Check if the entry name is not a symbol.
6964 __ mov(scratch, FieldOperand(scratch, HeapObject::kMapOffset));
6965 __ test_b(FieldOperand(scratch, Map::kInstanceTypeOffset),
6966 kIsSymbolMask);
6967 __ j(zero, &maybe_in_dictionary);
6968 }
6969 }
6970
6971 __ bind(&maybe_in_dictionary);
6972 // If we are doing negative lookup then probing failure should be
6973 // treated as a lookup success. For positive lookup probing failure
6974 // should be treated as lookup failure.
6975 if (mode_ == POSITIVE_LOOKUP) {
6976 __ mov(result_, Immediate(0));
6977 __ Drop(1);
6978 __ ret(2 * kPointerSize);
6979 }
6980
6981 __ bind(&in_dictionary);
6982 __ mov(result_, Immediate(1));
6983 __ Drop(1);
6984 __ ret(2 * kPointerSize);
6985
6986 __ bind(&not_in_dictionary);
6987 __ mov(result_, Immediate(0));
6988 __ Drop(1);
6989 __ ret(2 * kPointerSize);
6990}
6991
6992
Ben Murdoch592a9fc2012-03-05 11:04:45 +00006993struct AheadOfTimeWriteBarrierStubList {
6994 Register object, value, address;
6995 RememberedSetAction action;
6996};
6997
6998
6999struct AheadOfTimeWriteBarrierStubList kAheadOfTime[] = {
7000 // Used in RegExpExecStub.
7001 { ebx, eax, edi, EMIT_REMEMBERED_SET },
7002 // Used in CompileArrayPushCall.
7003 { ebx, ecx, edx, EMIT_REMEMBERED_SET },
7004 { ebx, edi, edx, OMIT_REMEMBERED_SET },
7005 // Used in CompileStoreGlobal and CallFunctionStub.
7006 { ebx, ecx, edx, OMIT_REMEMBERED_SET },
7007 // Used in StoreStubCompiler::CompileStoreField and
7008 // KeyedStoreStubCompiler::CompileStoreField via GenerateStoreField.
7009 { edx, ecx, ebx, EMIT_REMEMBERED_SET },
7010 // GenerateStoreField calls the stub with two different permutations of
7011 // registers. This is the second.
7012 { ebx, ecx, edx, EMIT_REMEMBERED_SET },
7013 // StoreIC::GenerateNormal via GenerateDictionaryStore
7014 { ebx, edi, edx, EMIT_REMEMBERED_SET },
7015 // KeyedStoreIC::GenerateGeneric.
7016 { ebx, edx, ecx, EMIT_REMEMBERED_SET},
7017 // KeyedStoreStubCompiler::GenerateStoreFastElement.
7018 { edi, edx, ecx, EMIT_REMEMBERED_SET},
7019 // ElementsTransitionGenerator::GenerateSmiOnlyToObject
7020 // and ElementsTransitionGenerator::GenerateSmiOnlyToDouble
7021 // and ElementsTransitionGenerator::GenerateDoubleToObject
7022 { edx, ebx, edi, EMIT_REMEMBERED_SET},
7023 // ElementsTransitionGenerator::GenerateDoubleToObject
7024 { eax, edx, esi, EMIT_REMEMBERED_SET},
7025 { edx, eax, edi, EMIT_REMEMBERED_SET},
7026 // StoreArrayLiteralElementStub::Generate
7027 { ebx, eax, ecx, EMIT_REMEMBERED_SET},
7028 // Null termination.
7029 { no_reg, no_reg, no_reg, EMIT_REMEMBERED_SET}
7030};
7031
7032
7033bool RecordWriteStub::IsPregenerated() {
7034 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
7035 !entry->object.is(no_reg);
7036 entry++) {
7037 if (object_.is(entry->object) &&
7038 value_.is(entry->value) &&
7039 address_.is(entry->address) &&
7040 remembered_set_action_ == entry->action &&
7041 save_fp_regs_mode_ == kDontSaveFPRegs) {
7042 return true;
7043 }
7044 }
7045 return false;
7046}
7047
7048
7049void StoreBufferOverflowStub::GenerateFixedRegStubsAheadOfTime() {
7050 StoreBufferOverflowStub stub1(kDontSaveFPRegs);
7051 stub1.GetCode()->set_is_pregenerated(true);
7052
7053 CpuFeatures::TryForceFeatureScope scope(SSE2);
7054 if (CpuFeatures::IsSupported(SSE2)) {
7055 StoreBufferOverflowStub stub2(kSaveFPRegs);
7056 stub2.GetCode()->set_is_pregenerated(true);
7057 }
7058}
7059
7060
7061void RecordWriteStub::GenerateFixedRegStubsAheadOfTime() {
7062 for (AheadOfTimeWriteBarrierStubList* entry = kAheadOfTime;
7063 !entry->object.is(no_reg);
7064 entry++) {
7065 RecordWriteStub stub(entry->object,
7066 entry->value,
7067 entry->address,
7068 entry->action,
7069 kDontSaveFPRegs);
7070 stub.GetCode()->set_is_pregenerated(true);
7071 }
7072}
7073
7074
7075// Takes the input in 3 registers: address_ value_ and object_. A pointer to
7076// the value has just been written into the object, now this stub makes sure
7077// we keep the GC informed. The word in the object where the value has been
7078// written is in the address register.
7079void RecordWriteStub::Generate(MacroAssembler* masm) {
7080 Label skip_to_incremental_noncompacting;
7081 Label skip_to_incremental_compacting;
7082
7083 // The first two instructions are generated with labels so as to get the
7084 // offset fixed up correctly by the bind(Label*) call. We patch it back and
7085 // forth between a compare instructions (a nop in this position) and the
7086 // real branch when we start and stop incremental heap marking.
7087 __ jmp(&skip_to_incremental_noncompacting, Label::kNear);
7088 __ jmp(&skip_to_incremental_compacting, Label::kFar);
7089
7090 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
7091 __ RememberedSetHelper(object_,
7092 address_,
7093 value_,
7094 save_fp_regs_mode_,
7095 MacroAssembler::kReturnAtEnd);
7096 } else {
7097 __ ret(0);
7098 }
7099
7100 __ bind(&skip_to_incremental_noncompacting);
7101 GenerateIncremental(masm, INCREMENTAL);
7102
7103 __ bind(&skip_to_incremental_compacting);
7104 GenerateIncremental(masm, INCREMENTAL_COMPACTION);
7105
7106 // Initial mode of the stub is expected to be STORE_BUFFER_ONLY.
7107 // Will be checked in IncrementalMarking::ActivateGeneratedStub.
7108 masm->set_byte_at(0, kTwoByteNopInstruction);
7109 masm->set_byte_at(2, kFiveByteNopInstruction);
7110}
7111
7112
7113void RecordWriteStub::GenerateIncremental(MacroAssembler* masm, Mode mode) {
7114 regs_.Save(masm);
7115
7116 if (remembered_set_action_ == EMIT_REMEMBERED_SET) {
7117 Label dont_need_remembered_set;
7118
7119 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
7120 __ JumpIfNotInNewSpace(regs_.scratch0(), // Value.
7121 regs_.scratch0(),
7122 &dont_need_remembered_set);
7123
7124 __ CheckPageFlag(regs_.object(),
7125 regs_.scratch0(),
7126 1 << MemoryChunk::SCAN_ON_SCAVENGE,
7127 not_zero,
7128 &dont_need_remembered_set);
7129
7130 // First notify the incremental marker if necessary, then update the
7131 // remembered set.
7132 CheckNeedsToInformIncrementalMarker(
7133 masm,
7134 kUpdateRememberedSetOnNoNeedToInformIncrementalMarker,
7135 mode);
7136 InformIncrementalMarker(masm, mode);
7137 regs_.Restore(masm);
7138 __ RememberedSetHelper(object_,
7139 address_,
7140 value_,
7141 save_fp_regs_mode_,
7142 MacroAssembler::kReturnAtEnd);
7143
7144 __ bind(&dont_need_remembered_set);
7145 }
7146
7147 CheckNeedsToInformIncrementalMarker(
7148 masm,
7149 kReturnOnNoNeedToInformIncrementalMarker,
7150 mode);
7151 InformIncrementalMarker(masm, mode);
7152 regs_.Restore(masm);
7153 __ ret(0);
7154}
7155
7156
7157void RecordWriteStub::InformIncrementalMarker(MacroAssembler* masm, Mode mode) {
7158 regs_.SaveCallerSaveRegisters(masm, save_fp_regs_mode_);
7159 int argument_count = 3;
7160 __ PrepareCallCFunction(argument_count, regs_.scratch0());
7161 __ mov(Operand(esp, 0 * kPointerSize), regs_.object());
7162 if (mode == INCREMENTAL_COMPACTION) {
7163 __ mov(Operand(esp, 1 * kPointerSize), regs_.address()); // Slot.
7164 } else {
7165 ASSERT(mode == INCREMENTAL);
7166 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
7167 __ mov(Operand(esp, 1 * kPointerSize), regs_.scratch0()); // Value.
7168 }
7169 __ mov(Operand(esp, 2 * kPointerSize),
7170 Immediate(ExternalReference::isolate_address()));
7171
7172 AllowExternalCallThatCantCauseGC scope(masm);
7173 if (mode == INCREMENTAL_COMPACTION) {
7174 __ CallCFunction(
7175 ExternalReference::incremental_evacuation_record_write_function(
7176 masm->isolate()),
7177 argument_count);
7178 } else {
7179 ASSERT(mode == INCREMENTAL);
7180 __ CallCFunction(
7181 ExternalReference::incremental_marking_record_write_function(
7182 masm->isolate()),
7183 argument_count);
7184 }
7185 regs_.RestoreCallerSaveRegisters(masm, save_fp_regs_mode_);
7186}
7187
7188
7189void RecordWriteStub::CheckNeedsToInformIncrementalMarker(
7190 MacroAssembler* masm,
7191 OnNoNeedToInformIncrementalMarker on_no_need,
7192 Mode mode) {
7193 Label object_is_black, need_incremental, need_incremental_pop_object;
7194
7195 // Let's look at the color of the object: If it is not black we don't have
7196 // to inform the incremental marker.
7197 __ JumpIfBlack(regs_.object(),
7198 regs_.scratch0(),
7199 regs_.scratch1(),
7200 &object_is_black,
7201 Label::kNear);
7202
7203 regs_.Restore(masm);
7204 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
7205 __ RememberedSetHelper(object_,
7206 address_,
7207 value_,
7208 save_fp_regs_mode_,
7209 MacroAssembler::kReturnAtEnd);
7210 } else {
7211 __ ret(0);
7212 }
7213
7214 __ bind(&object_is_black);
7215
7216 // Get the value from the slot.
7217 __ mov(regs_.scratch0(), Operand(regs_.address(), 0));
7218
7219 if (mode == INCREMENTAL_COMPACTION) {
7220 Label ensure_not_white;
7221
7222 __ CheckPageFlag(regs_.scratch0(), // Contains value.
7223 regs_.scratch1(), // Scratch.
7224 MemoryChunk::kEvacuationCandidateMask,
7225 zero,
7226 &ensure_not_white,
7227 Label::kNear);
7228
7229 __ CheckPageFlag(regs_.object(),
7230 regs_.scratch1(), // Scratch.
7231 MemoryChunk::kSkipEvacuationSlotsRecordingMask,
7232 not_zero,
7233 &ensure_not_white,
7234 Label::kNear);
7235
7236 __ jmp(&need_incremental);
7237
7238 __ bind(&ensure_not_white);
7239 }
7240
7241 // We need an extra register for this, so we push the object register
7242 // temporarily.
7243 __ push(regs_.object());
7244 __ EnsureNotWhite(regs_.scratch0(), // The value.
7245 regs_.scratch1(), // Scratch.
7246 regs_.object(), // Scratch.
7247 &need_incremental_pop_object,
7248 Label::kNear);
7249 __ pop(regs_.object());
7250
7251 regs_.Restore(masm);
7252 if (on_no_need == kUpdateRememberedSetOnNoNeedToInformIncrementalMarker) {
7253 __ RememberedSetHelper(object_,
7254 address_,
7255 value_,
7256 save_fp_regs_mode_,
7257 MacroAssembler::kReturnAtEnd);
7258 } else {
7259 __ ret(0);
7260 }
7261
7262 __ bind(&need_incremental_pop_object);
7263 __ pop(regs_.object());
7264
7265 __ bind(&need_incremental);
7266
7267 // Fall through when we need to inform the incremental marker.
7268}
7269
7270
7271void StoreArrayLiteralElementStub::Generate(MacroAssembler* masm) {
7272 // ----------- S t a t e -------------
7273 // -- eax : element value to store
7274 // -- ebx : array literal
7275 // -- edi : map of array literal
7276 // -- ecx : element index as smi
7277 // -- edx : array literal index in function
7278 // -- esp[0] : return address
7279 // -----------------------------------
7280
7281 Label element_done;
7282 Label double_elements;
7283 Label smi_element;
7284 Label slow_elements;
7285 Label slow_elements_from_double;
7286 Label fast_elements;
7287
7288 __ CheckFastElements(edi, &double_elements);
7289
7290 // FAST_SMI_ONLY_ELEMENTS or FAST_ELEMENTS
7291 __ JumpIfSmi(eax, &smi_element);
7292 __ CheckFastSmiOnlyElements(edi, &fast_elements, Label::kNear);
7293
7294 // Store into the array literal requires a elements transition. Call into
7295 // the runtime.
7296
7297 __ bind(&slow_elements);
7298 __ pop(edi); // Pop return address and remember to put back later for tail
7299 // call.
7300 __ push(ebx);
7301 __ push(ecx);
7302 __ push(eax);
7303 __ mov(ebx, Operand(ebp, JavaScriptFrameConstants::kFunctionOffset));
7304 __ push(FieldOperand(ebx, JSFunction::kLiteralsOffset));
7305 __ push(edx);
7306 __ push(edi); // Return return address so that tail call returns to right
7307 // place.
7308 __ TailCallRuntime(Runtime::kStoreArrayLiteralElement, 5, 1);
7309
7310 __ bind(&slow_elements_from_double);
7311 __ pop(edx);
7312 __ jmp(&slow_elements);
7313
7314 // Array literal has ElementsKind of FAST_ELEMENTS and value is an object.
7315 __ bind(&fast_elements);
7316 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
7317 __ lea(ecx, FieldOperand(ebx, ecx, times_half_pointer_size,
7318 FixedArrayBase::kHeaderSize));
7319 __ mov(Operand(ecx, 0), eax);
7320 // Update the write barrier for the array store.
7321 __ RecordWrite(ebx, ecx, eax,
7322 kDontSaveFPRegs,
7323 EMIT_REMEMBERED_SET,
7324 OMIT_SMI_CHECK);
7325 __ ret(0);
7326
7327 // Array literal has ElementsKind of FAST_SMI_ONLY_ELEMENTS or
7328 // FAST_ELEMENTS, and value is Smi.
7329 __ bind(&smi_element);
7330 __ mov(ebx, FieldOperand(ebx, JSObject::kElementsOffset));
7331 __ mov(FieldOperand(ebx, ecx, times_half_pointer_size,
7332 FixedArrayBase::kHeaderSize), eax);
7333 __ ret(0);
7334
7335 // Array literal has ElementsKind of FAST_DOUBLE_ELEMENTS.
7336 __ bind(&double_elements);
7337
7338 __ push(edx);
7339 __ mov(edx, FieldOperand(ebx, JSObject::kElementsOffset));
7340 __ StoreNumberToDoubleElements(eax,
7341 edx,
7342 ecx,
7343 edi,
7344 xmm0,
7345 &slow_elements_from_double,
7346 false);
7347 __ pop(edx);
7348 __ ret(0);
7349}
7350
Kristian Monsen80d68ea2010-09-08 11:05:35 +01007351#undef __
7352
7353} } // namespace v8::internal
7354
7355#endif // V8_TARGET_ARCH_IA32